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>
48*4235Smarkfen #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
550Sstevel@tonic-gate  * utilities including ipseckey(1m) and ikeadm(1m).
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /* Set standard default/initial values for globals... */
590Sstevel@tonic-gate boolean_t pflag = B_FALSE;	/* paranoid w.r.t. printing keying material */
600Sstevel@tonic-gate boolean_t nflag = B_FALSE;	/* avoid nameservice? */
610Sstevel@tonic-gate boolean_t interactive = B_FALSE;	/* util not running on cmdline */
620Sstevel@tonic-gate boolean_t readfile = B_FALSE;	/* cmds are being read from a file */
630Sstevel@tonic-gate uint_t	lineno = 0;		/* track location if reading cmds from file */
64*4235Smarkfen uint_t	lines_added = 0;
65*4235Smarkfen uint_t	lines_parsed = 0;
660Sstevel@tonic-gate jmp_buf	env;		/* for error recovery in interactive/readfile modes */
67*4235Smarkfen char *my_fmri = NULL;
68*4235Smarkfen FILE *debugfile = stderr;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Print errno and exit if cmdline or readfile, reset state if interactive
724064Smarkfen  * The error string *what should be dgettext()'d before calling bail().
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate void
750Sstevel@tonic-gate bail(char *what)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	if (errno != 0)
780Sstevel@tonic-gate 		warn(what);
790Sstevel@tonic-gate 	else
80*4235Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what);
810Sstevel@tonic-gate 	if (readfile) {
82*4235Smarkfen 		return;
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	if (interactive && !readfile)
850Sstevel@tonic-gate 		longjmp(env, 2);
86*4235Smarkfen 	EXIT_FATAL(NULL);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Print caller-supplied variable-arg error msg, then exit if cmdline or
910Sstevel@tonic-gate  * readfile, or reset state if interactive.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate /*PRINTFLIKE1*/
940Sstevel@tonic-gate void
950Sstevel@tonic-gate bail_msg(char *fmt, ...)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	va_list	ap;
980Sstevel@tonic-gate 	char	msgbuf[BUFSIZ];
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	va_start(ap, fmt);
1010Sstevel@tonic-gate 	(void) vsnprintf(msgbuf, BUFSIZ, fmt, ap);
1020Sstevel@tonic-gate 	va_end(ap);
1030Sstevel@tonic-gate 	if (readfile)
1044064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
1054064Smarkfen 		    "ERROR on line %u:\n%s\n"), lineno,  msgbuf);
1060Sstevel@tonic-gate 	else
1074064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (interactive && !readfile)
1100Sstevel@tonic-gate 		longjmp(env, 1);
1110Sstevel@tonic-gate 
112*4235Smarkfen 	EXIT_FATAL(NULL);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * dump_XXX functions produce ASCII output from various structures.
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  * Because certain errors need to do this to stderr, dump_XXX functions
1200Sstevel@tonic-gate  * take a FILE pointer.
1210Sstevel@tonic-gate  *
1220Sstevel@tonic-gate  * If an error occured while writing to the specified file, these
1230Sstevel@tonic-gate  * functions return -1, zero otherwise.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate int
1273055Sdanmcd dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only,
1283055Sdanmcd     FILE *where)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	struct sockaddr_in	*sin;
1310Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
1320Sstevel@tonic-gate 	char			*printable_addr, *protocol;
1330Sstevel@tonic-gate 	uint8_t			*addrptr;
1343055Sdanmcd 	/* Add 4 chars to hold '/nnn' for prefixes. */
1353055Sdanmcd 	char			storage[INET6_ADDRSTRLEN + 4];
1360Sstevel@tonic-gate 	uint16_t		port;
1370Sstevel@tonic-gate 	boolean_t		unspec;
1380Sstevel@tonic-gate 	struct hostent		*hp;
1390Sstevel@tonic-gate 	int			getipnode_errno, addrlen;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	switch (sa->sa_family) {
1420Sstevel@tonic-gate 	case AF_INET:
1430Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1440Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sa;
1450Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin->sin_addr;
1460Sstevel@tonic-gate 		port = sin->sin_port;
1470Sstevel@tonic-gate 		protocol = "AF_INET";
1480Sstevel@tonic-gate 		unspec = (sin->sin_addr.s_addr == 0);
1490Sstevel@tonic-gate 		addrlen = sizeof (sin->sin_addr);
1500Sstevel@tonic-gate 		break;
1510Sstevel@tonic-gate 	case AF_INET6:
1520Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1530Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sa;
1540Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin6->sin6_addr;
1550Sstevel@tonic-gate 		port = sin6->sin6_port;
1560Sstevel@tonic-gate 		protocol = "AF_INET6";
1570Sstevel@tonic-gate 		unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr);
1580Sstevel@tonic-gate 		addrlen = sizeof (sin6->sin6_addr);
1590Sstevel@tonic-gate 		break;
1600Sstevel@tonic-gate 	default:
1610Sstevel@tonic-gate 		return (0);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) ==
1650Sstevel@tonic-gate 	    NULL) {
1664064Smarkfen 		printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address.");
1670Sstevel@tonic-gate 	} else {
1683055Sdanmcd 		char prefix[5];	/* "/nnn" with terminator. */
1693055Sdanmcd 
1703055Sdanmcd 		(void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen);
1710Sstevel@tonic-gate 		printable_addr = storage;
1723055Sdanmcd 		if (prefixlen != 0) {
1733055Sdanmcd 			(void) strlcat(printable_addr, prefix,
1743055Sdanmcd 			    sizeof (storage));
1753055Sdanmcd 		}
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 	if (addr_only) {
1780Sstevel@tonic-gate 		if (fprintf(where, "%s", printable_addr) < 0)
1790Sstevel@tonic-gate 			return (-1);
1800Sstevel@tonic-gate 	} else {
1814064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
1824064Smarkfen 		    "%s: port %d, %s"), protocol,
1830Sstevel@tonic-gate 		    ntohs(port), printable_addr) < 0)
1840Sstevel@tonic-gate 			return (-1);
1850Sstevel@tonic-gate 		if (!nflag) {
1860Sstevel@tonic-gate 			/*
1870Sstevel@tonic-gate 			 * Do AF_independent reverse hostname lookup here.
1880Sstevel@tonic-gate 			 */
1890Sstevel@tonic-gate 			if (unspec) {
1900Sstevel@tonic-gate 				if (fprintf(where,
1914064Smarkfen 				    dgettext(TEXT_DOMAIN,
1924064Smarkfen 				    " <unspecified>")) < 0)
1930Sstevel@tonic-gate 					return (-1);
1940Sstevel@tonic-gate 			} else {
1950Sstevel@tonic-gate 				hp = getipnodebyaddr((char *)addrptr, addrlen,
1960Sstevel@tonic-gate 				    sa->sa_family, &getipnode_errno);
1970Sstevel@tonic-gate 				if (hp != NULL) {
1980Sstevel@tonic-gate 					if (fprintf(where,
1990Sstevel@tonic-gate 					    " (%s)", hp->h_name) < 0)
2000Sstevel@tonic-gate 						return (-1);
2010Sstevel@tonic-gate 					freehostent(hp);
2020Sstevel@tonic-gate 				} else {
2030Sstevel@tonic-gate 					if (fprintf(where,
2044064Smarkfen 					    dgettext(TEXT_DOMAIN,
2054064Smarkfen 					    " <unknown>")) < 0)
2060Sstevel@tonic-gate 						return (-1);
2070Sstevel@tonic-gate 				}
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 		if (fputs(".\n", where) == EOF)
2110Sstevel@tonic-gate 			return (-1);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	return (0);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * Dump a key and bitlen
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate int
2200Sstevel@tonic-gate dump_key(uint8_t *keyp, uint_t bitlen, FILE *where)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	int	numbytes;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	numbytes = SADB_1TO8(bitlen);
2250Sstevel@tonic-gate 	/* The & 0x7 is to check for leftover bits. */
2260Sstevel@tonic-gate 	if ((bitlen & 0x7) != 0)
2270Sstevel@tonic-gate 		numbytes++;
2280Sstevel@tonic-gate 	while (numbytes-- != 0) {
2290Sstevel@tonic-gate 		if (pflag) {
2300Sstevel@tonic-gate 			/* Print no keys if paranoid */
2310Sstevel@tonic-gate 			if (fprintf(where, "XX") < 0)
2320Sstevel@tonic-gate 				return (-1);
2330Sstevel@tonic-gate 		} else {
2340Sstevel@tonic-gate 			if (fprintf(where, "%02x", *keyp++) < 0)
2350Sstevel@tonic-gate 				return (-1);
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	if (fprintf(where, "/%u", bitlen) < 0)
2390Sstevel@tonic-gate 		return (-1);
2400Sstevel@tonic-gate 	return (0);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Print an authentication or encryption algorithm
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate static int
2470Sstevel@tonic-gate dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	struct ipsecalgent *alg;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_num, proto_num, NULL);
2520Sstevel@tonic-gate 	if (alg == NULL) {
2534064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
2544064Smarkfen 		    "<unknown %u>"), alg_num) < 0)
2550Sstevel@tonic-gate 			return (-1);
2560Sstevel@tonic-gate 		return (0);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * Special-case <none> for backward output compat.
2610Sstevel@tonic-gate 	 * Assume that SADB_AALG_NONE == SADB_EALG_NONE.
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 	if (alg_num == SADB_AALG_NONE) {
2644064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
2654064Smarkfen 		    "<none>"), where) == EOF)
2660Sstevel@tonic-gate 			return (-1);
2670Sstevel@tonic-gate 	} else {
2680Sstevel@tonic-gate 		if (fputs(alg->a_names[0], where) == EOF)
2690Sstevel@tonic-gate 			return (-1);
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	freeipsecalgent(alg);
2730Sstevel@tonic-gate 	return (0);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate int
2770Sstevel@tonic-gate dump_aalg(uint8_t aalg, FILE *where)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where));
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate int
2830Sstevel@tonic-gate dump_ealg(uint8_t ealg, FILE *where)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where));
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Print an SADB_IDENTTYPE string
2900Sstevel@tonic-gate  *
2910Sstevel@tonic-gate  * Also return TRUE if the actual ident may be printed, FALSE if not.
2920Sstevel@tonic-gate  *
2930Sstevel@tonic-gate  * If rc is not NULL, set its value to -1 if an error occured while writing
2940Sstevel@tonic-gate  * to the specified file, zero otherwise.
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate boolean_t
2970Sstevel@tonic-gate dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	boolean_t canprint = B_TRUE;
3000Sstevel@tonic-gate 	int rc_val = 0;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	switch (idtype) {
3030Sstevel@tonic-gate 	case SADB_IDENTTYPE_PREFIX:
3044064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF)
3050Sstevel@tonic-gate 			rc_val = -1;
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 	case SADB_IDENTTYPE_FQDN:
3084064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF)
3090Sstevel@tonic-gate 			rc_val = -1;
3100Sstevel@tonic-gate 		break;
3110Sstevel@tonic-gate 	case SADB_IDENTTYPE_USER_FQDN:
3124064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
3134064Smarkfen 		    "user-FQDN (mbox)"), where) == EOF)
3140Sstevel@tonic-gate 			rc_val = -1;
3150Sstevel@tonic-gate 		break;
3160Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_DN:
3174064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"),
3180Sstevel@tonic-gate 		    where) == EOF)
3190Sstevel@tonic-gate 			rc_val = -1;
3200Sstevel@tonic-gate 		canprint = B_FALSE;
3210Sstevel@tonic-gate 		break;
3220Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_GN:
3234064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"),
3244064Smarkfen 		    where) == EOF)
3250Sstevel@tonic-gate 			rc_val = -1;
3260Sstevel@tonic-gate 		canprint = B_FALSE;
3270Sstevel@tonic-gate 		break;
3280Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_KEY_ID:
3294064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"),
3304064Smarkfen 		    where) == EOF)
3310Sstevel@tonic-gate 			rc_val = -1;
3320Sstevel@tonic-gate 		break;
3330Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_ADDR_RANGE:
3344064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF)
3350Sstevel@tonic-gate 			rc_val = -1;
3360Sstevel@tonic-gate 		break;
3370Sstevel@tonic-gate 	default:
3384064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
3394064Smarkfen 		    "<unknown %u>"), idtype) < 0)
3400Sstevel@tonic-gate 			rc_val = -1;
3410Sstevel@tonic-gate 		break;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (rc != NULL)
3450Sstevel@tonic-gate 		*rc = rc_val;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	return (canprint);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate  * Slice an argv/argc vector from an interactive line or a read-file line.
3520Sstevel@tonic-gate  */
3530Sstevel@tonic-gate static int
3540Sstevel@tonic-gate create_argv(char *ibuf, int *newargc, char ***thisargv)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	unsigned int argvlen = START_ARG;
3570Sstevel@tonic-gate 	char **current;
3580Sstevel@tonic-gate 	boolean_t firstchar = B_TRUE;
3590Sstevel@tonic-gate 	boolean_t inquotes = B_FALSE;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	*thisargv = malloc(sizeof (char *) * argvlen);
3620Sstevel@tonic-gate 	if ((*thisargv) == NULL)
3630Sstevel@tonic-gate 		return (MEMORY_ALLOCATION);
3640Sstevel@tonic-gate 	current = *thisargv;
3650Sstevel@tonic-gate 	*current = NULL;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	for (; *ibuf != '\0'; ibuf++) {
3680Sstevel@tonic-gate 		if (isspace(*ibuf)) {
3690Sstevel@tonic-gate 			if (inquotes) {
3700Sstevel@tonic-gate 				continue;
3710Sstevel@tonic-gate 			}
3720Sstevel@tonic-gate 			if (*current != NULL) {
3730Sstevel@tonic-gate 				*ibuf = '\0';
3740Sstevel@tonic-gate 				current++;
3750Sstevel@tonic-gate 				if (*thisargv + argvlen == current) {
3760Sstevel@tonic-gate 					/* Regrow ***thisargv. */
3770Sstevel@tonic-gate 					if (argvlen == TOO_MANY_ARGS) {
3780Sstevel@tonic-gate 						free(*thisargv);
3790Sstevel@tonic-gate 						return (TOO_MANY_TOKENS);
3800Sstevel@tonic-gate 					}
3810Sstevel@tonic-gate 					/* Double the allocation. */
3820Sstevel@tonic-gate 					current = realloc(*thisargv,
3830Sstevel@tonic-gate 					    sizeof (char *) * (argvlen << 1));
3840Sstevel@tonic-gate 					if (current == NULL) {
3850Sstevel@tonic-gate 						free(*thisargv);
3860Sstevel@tonic-gate 						return (MEMORY_ALLOCATION);
3870Sstevel@tonic-gate 					}
3880Sstevel@tonic-gate 					*thisargv = current;
3890Sstevel@tonic-gate 					current += argvlen;
3900Sstevel@tonic-gate 					argvlen <<= 1;	/* Double the size. */
3910Sstevel@tonic-gate 				}
3920Sstevel@tonic-gate 				*current = NULL;
3930Sstevel@tonic-gate 			}
3940Sstevel@tonic-gate 		} else {
3950Sstevel@tonic-gate 			if (firstchar) {
3960Sstevel@tonic-gate 				firstchar = B_FALSE;
397*4235Smarkfen 				if (*ibuf == COMMENT_CHAR || *ibuf == '\n') {
3980Sstevel@tonic-gate 					free(*thisargv);
3990Sstevel@tonic-gate 					return (COMMENT_LINE);
4000Sstevel@tonic-gate 				}
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 			if (*ibuf == QUOTE_CHAR) {
4030Sstevel@tonic-gate 				if (inquotes) {
4040Sstevel@tonic-gate 					inquotes = B_FALSE;
4050Sstevel@tonic-gate 					*ibuf = '\0';
4060Sstevel@tonic-gate 				} else {
4070Sstevel@tonic-gate 					inquotes = B_TRUE;
4080Sstevel@tonic-gate 				}
4090Sstevel@tonic-gate 				continue;
4100Sstevel@tonic-gate 			}
4110Sstevel@tonic-gate 			if (*current == NULL) {
4120Sstevel@tonic-gate 				*current = ibuf;
4130Sstevel@tonic-gate 				(*newargc)++;
4140Sstevel@tonic-gate 			}
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * Tricky corner case...
4200Sstevel@tonic-gate 	 * I've parsed _exactly_ the amount of args as I have space.  It
4210Sstevel@tonic-gate 	 * won't return NULL-terminated, and bad things will happen to
4220Sstevel@tonic-gate 	 * the caller.
4230Sstevel@tonic-gate 	 */
4240Sstevel@tonic-gate 	if (argvlen == *newargc) {
4250Sstevel@tonic-gate 		current = realloc(*thisargv, sizeof (char *) * (argvlen + 1));
4260Sstevel@tonic-gate 		if (current == NULL) {
4270Sstevel@tonic-gate 			free(*thisargv);
4280Sstevel@tonic-gate 			return (MEMORY_ALLOCATION);
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 		*thisargv = current;
4310Sstevel@tonic-gate 		current[argvlen] = NULL;
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	return (SUCCESS);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate  * Enter a mode where commands are read from a file.  Treat stdin special.
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate void
441*4235Smarkfen do_interactive(FILE *infile, char *configfile, char *promptstring,
442*4235Smarkfen     char *my_fmri, parse_cmdln_fn parseit)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	char		ibuf[IBUF_SIZE], holder[IBUF_SIZE];
445*4235Smarkfen 	char		*hptr, **thisargv, *ebuf;
4460Sstevel@tonic-gate 	int		thisargc;
4470Sstevel@tonic-gate 	boolean_t	continue_in_progress = B_FALSE;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	(void) setjmp(env);
4500Sstevel@tonic-gate 
451*4235Smarkfen 	ebuf = NULL;
4520Sstevel@tonic-gate 	interactive = B_TRUE;
4530Sstevel@tonic-gate 	bzero(ibuf, IBUF_SIZE);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (infile == stdin) {
4560Sstevel@tonic-gate 		(void) printf("%s", promptstring);
4570Sstevel@tonic-gate 		(void) fflush(stdout);
4580Sstevel@tonic-gate 	} else {
4590Sstevel@tonic-gate 		readfile = B_TRUE;
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	while (fgets(ibuf, IBUF_SIZE, infile) != NULL) {
4630Sstevel@tonic-gate 		if (readfile)
4640Sstevel@tonic-gate 			lineno++;
4650Sstevel@tonic-gate 		thisargc = 0;
4660Sstevel@tonic-gate 		thisargv = NULL;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		/*
4690Sstevel@tonic-gate 		 * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will
4700Sstevel@tonic-gate 		 * be null-terminated because of fgets().
4710Sstevel@tonic-gate 		 */
4720Sstevel@tonic-gate 		if (ibuf[IBUF_SIZE - 2] != '\0') {
473*4235Smarkfen 			ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile,
474*4235Smarkfen 			    dgettext(TEXT_DOMAIN, "Line %d too big."), lineno);
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		if (!continue_in_progress) {
4780Sstevel@tonic-gate 			/* Use -2 because of \n from fgets. */
4790Sstevel@tonic-gate 			if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) {
4800Sstevel@tonic-gate 				/*
4810Sstevel@tonic-gate 				 * Can use strcpy here, I've checked the
4820Sstevel@tonic-gate 				 * length already.
4830Sstevel@tonic-gate 				 */
4840Sstevel@tonic-gate 				(void) strcpy(holder, ibuf);
4850Sstevel@tonic-gate 				hptr = &(holder[strlen(holder)]);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
4880Sstevel@tonic-gate 				hptr[-2] = ' ';
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 				continue_in_progress = B_TRUE;
4910Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
4920Sstevel@tonic-gate 				continue;
4930Sstevel@tonic-gate 			}
4940Sstevel@tonic-gate 		} else {
4950Sstevel@tonic-gate 			/* Handle continuations... */
4960Sstevel@tonic-gate 			(void) strncpy(hptr, ibuf,
4970Sstevel@tonic-gate 			    (size_t)(&(holder[IBUF_SIZE]) - hptr));
4980Sstevel@tonic-gate 			if (holder[IBUF_SIZE - 1] != '\0') {
499*4235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
500*4235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
501*4235Smarkfen 				    "Command buffer overrun."));
5020Sstevel@tonic-gate 			}
5030Sstevel@tonic-gate 			/* Use - 2 because of \n from fgets. */
5040Sstevel@tonic-gate 			if (hptr[strlen(hptr) - 2] == CONT_CHAR) {
5050Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
5060Sstevel@tonic-gate 				hptr += strlen(hptr);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
5090Sstevel@tonic-gate 				hptr[-2] = ' ';
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 				continue;
5120Sstevel@tonic-gate 			} else {
5130Sstevel@tonic-gate 				continue_in_progress = B_FALSE;
5140Sstevel@tonic-gate 				/*
5150Sstevel@tonic-gate 				 * I've already checked the length...
5160Sstevel@tonic-gate 				 */
5170Sstevel@tonic-gate 				(void) strcpy(ibuf, holder);
5180Sstevel@tonic-gate 			}
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 
521*4235Smarkfen 		/*
522*4235Smarkfen 		 * Just in case the command fails keep a copy of the
523*4235Smarkfen 		 * command buffer for diagnostic output.
524*4235Smarkfen 		 */
525*4235Smarkfen 		if (readfile) {
526*4235Smarkfen 			/*
527*4235Smarkfen 			 * The error buffer needs to be big enough to
528*4235Smarkfen 			 * hold the longest command string, plus
529*4235Smarkfen 			 * some extra text, see below.
530*4235Smarkfen 			 */
531*4235Smarkfen 			ebuf = calloc((IBUF_SIZE * 2), sizeof (char));
532*4235Smarkfen 			if (ebuf == NULL) {
533*4235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
534*4235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
535*4235Smarkfen 				    "Memory allocation error."));
536*4235Smarkfen 			} else {
537*4235Smarkfen 				(void) snprintf(ebuf, (IBUF_SIZE * 2),
538*4235Smarkfen 				    dgettext(TEXT_DOMAIN,
539*4235Smarkfen 				    "Config file entry near line %u "
540*4235Smarkfen 				    "caused error(s) or warnings:\n\n%s\n\n"),
541*4235Smarkfen 				    lineno, ibuf);
542*4235Smarkfen 			}
543*4235Smarkfen 		}
544*4235Smarkfen 
5450Sstevel@tonic-gate 		switch (create_argv(ibuf, &thisargc, &thisargv)) {
5460Sstevel@tonic-gate 		case TOO_MANY_TOKENS:
547*4235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
548*4235Smarkfen 			    dgettext(TEXT_DOMAIN, "Too many input tokens."));
5490Sstevel@tonic-gate 			break;
5500Sstevel@tonic-gate 		case MEMORY_ALLOCATION:
551*4235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
552*4235Smarkfen 			    dgettext(TEXT_DOMAIN, "Memory allocation error."));
5530Sstevel@tonic-gate 			break;
5540Sstevel@tonic-gate 		case COMMENT_LINE:
5550Sstevel@tonic-gate 			/* Comment line. */
556*4235Smarkfen 			free(ebuf);
5570Sstevel@tonic-gate 			break;
5580Sstevel@tonic-gate 		default:
559*4235Smarkfen 			if (thisargc != 0) {
560*4235Smarkfen 				lines_parsed++;
561*4235Smarkfen 				/* ebuf consumed */
562*4235Smarkfen 				parseit(thisargc, thisargv, ebuf);
563*4235Smarkfen 			} else {
564*4235Smarkfen 				free(ebuf);
565*4235Smarkfen 			}
5660Sstevel@tonic-gate 			free(thisargv);
5670Sstevel@tonic-gate 			if (infile == stdin) {
5680Sstevel@tonic-gate 				(void) printf("%s", promptstring);
5690Sstevel@tonic-gate 				(void) fflush(stdout);
5700Sstevel@tonic-gate 			}
5710Sstevel@tonic-gate 			break;
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 		bzero(ibuf, IBUF_SIZE);
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 	if (!readfile) {
5760Sstevel@tonic-gate 		(void) putchar('\n');
5770Sstevel@tonic-gate 		(void) fflush(stdout);
5780Sstevel@tonic-gate 	}
579*4235Smarkfen 	if (lines_added == 0)
580*4235Smarkfen 		ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
581*4235Smarkfen 		    dgettext(TEXT_DOMAIN, "Configuration file did not "
582*4235Smarkfen 		    "contain any valid SAs"));
583*4235Smarkfen 
584*4235Smarkfen 	/*
585*4235Smarkfen 	 * There were some errors. Putting the service in maintenance mode.
586*4235Smarkfen 	 * When svc.startd(1M) allows services to degrade themselves,
587*4235Smarkfen 	 * this should be revisited.
588*4235Smarkfen 	 *
589*4235Smarkfen 	 * If this function was called from a program running as a
590*4235Smarkfen 	 * smf_method(5), print a warning message. Don't spew out the
591*4235Smarkfen 	 * errors as these will end up in the smf(5) log file which is
592*4235Smarkfen 	 * publically readable, the errors may contain sensitive information.
593*4235Smarkfen 	 */
594*4235Smarkfen 	if ((lines_added < lines_parsed) && (configfile != NULL)) {
595*4235Smarkfen 		if (my_fmri != NULL) {
596*4235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
597*4235Smarkfen 			dgettext(TEXT_DOMAIN,
598*4235Smarkfen 			    "The configuration file contained %d errors.\n"
599*4235Smarkfen 			    "Manually check the configuration with:\n"
600*4235Smarkfen 			    "ipseckey -c %s\n"
601*4235Smarkfen 			    "Use svcadm(1M) to clear maintenance condition "
602*4235Smarkfen 			    "when errors are resolved.\n"),
603*4235Smarkfen 			    lines_parsed - lines_added, configfile);
604*4235Smarkfen 		} else {
605*4235Smarkfen 			EXIT_BADCONFIG(NULL);
606*4235Smarkfen 		}
607*4235Smarkfen 	} else {
608*4235Smarkfen 		if (my_fmri != NULL)
609*4235Smarkfen 			ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile,
610*4235Smarkfen 			    dgettext(TEXT_DOMAIN,
611*4235Smarkfen 			    "%d SA's successfullly added."), lines_added);
612*4235Smarkfen 	}
613*4235Smarkfen 	EXIT_OK(NULL);
6140Sstevel@tonic-gate 	exit(0);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate /*
6180Sstevel@tonic-gate  * Functions to parse strings that represent a debug or privilege level.
6190Sstevel@tonic-gate  * These functions are copied from main.c and door.c in usr.lib/in.iked/common.
6200Sstevel@tonic-gate  * If this file evolves into a common library that may be used by in.iked
6210Sstevel@tonic-gate  * as well as the usr.sbin utilities, those duplicate functions should be
6220Sstevel@tonic-gate  * deleted.
6230Sstevel@tonic-gate  *
6240Sstevel@tonic-gate  * A privilege level may be represented by a simple keyword, corresponding
6250Sstevel@tonic-gate  * to one of the possible levels.  A debug level may be represented by a
6260Sstevel@tonic-gate  * series of keywords, separated by '+' or '-', indicating categories to
6270Sstevel@tonic-gate  * be added or removed from the set of categories in the debug level.
6280Sstevel@tonic-gate  * For example, +all-op corresponds to level 0xfffffffb (all flags except
6290Sstevel@tonic-gate  * for D_OP set); while p1+p2+pfkey corresponds to level 0x38.  Note that
6300Sstevel@tonic-gate  * the leading '+' is implicit; the first keyword in the list must be for
6310Sstevel@tonic-gate  * a category that is to be added.
6320Sstevel@tonic-gate  *
6330Sstevel@tonic-gate  * These parsing functions make use of a local version of strtok, strtok_d,
6340Sstevel@tonic-gate  * which includes an additional parameter, char *delim.  This param is filled
6350Sstevel@tonic-gate  * in with the character which ends the returned token.  In other words,
6360Sstevel@tonic-gate  * this version of strtok, in addition to returning the token, also returns
6370Sstevel@tonic-gate  * the single character delimiter from the original string which marked the
6380Sstevel@tonic-gate  * end of the token.
6390Sstevel@tonic-gate  */
6400Sstevel@tonic-gate static char *
6410Sstevel@tonic-gate strtok_d(char *string, const char *sepset, char *delim)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 	static char	*lasts;
6440Sstevel@tonic-gate 	char		*q, *r;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	/* first or subsequent call */
6470Sstevel@tonic-gate 	if (string == NULL)
6480Sstevel@tonic-gate 		string = lasts;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if (string == 0)		/* return if no tokens remaining */
6510Sstevel@tonic-gate 		return (NULL);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	q = string + strspn(string, sepset);	/* skip leading separators */
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if (*q == '\0')			/* return if no tokens remaining */
6560Sstevel@tonic-gate 		return (NULL);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if ((r = strpbrk(q, sepset)) == NULL) {		/* move past token */
6590Sstevel@tonic-gate 		lasts = 0;	/* indicate that this is last token */
6600Sstevel@tonic-gate 	} else {
6610Sstevel@tonic-gate 		*delim = *r;	/* save delimitor */
6620Sstevel@tonic-gate 		*r = '\0';
6630Sstevel@tonic-gate 		lasts = r + 1;
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 	return (q);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate static keywdtab_t	privtab[] = {
6690Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"base" },
6700Sstevel@tonic-gate 	{ IKE_PRIV_MODKEYS,	"modkeys" },
6710Sstevel@tonic-gate 	{ IKE_PRIV_KEYMAT,	"keymat" },
6720Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"0" },
6730Sstevel@tonic-gate };
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate int
6760Sstevel@tonic-gate privstr2num(char *str)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate 	keywdtab_t	*pp;
6790Sstevel@tonic-gate 	char		*endp;
6800Sstevel@tonic-gate 	int		 priv;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	for (pp = privtab; pp < A_END(privtab); pp++) {
6830Sstevel@tonic-gate 		if (strcasecmp(str, pp->kw_str) == 0)
6840Sstevel@tonic-gate 			return (pp->kw_tag);
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	priv = strtol(str, &endp, 0);
6880Sstevel@tonic-gate 	if (*endp == '\0')
6890Sstevel@tonic-gate 		return (priv);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	return (-1);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate static keywdtab_t	dbgtab[] = {
6950Sstevel@tonic-gate 	{ D_CERT,	"cert" },
6960Sstevel@tonic-gate 	{ D_KEY,	"key" },
6970Sstevel@tonic-gate 	{ D_OP,		"op" },
6980Sstevel@tonic-gate 	{ D_P1,		"p1" },
6990Sstevel@tonic-gate 	{ D_P1,		"phase1" },
7000Sstevel@tonic-gate 	{ D_P2,		"p2" },
7010Sstevel@tonic-gate 	{ D_P2,		"phase2" },
7020Sstevel@tonic-gate 	{ D_PFKEY,	"pfkey" },
7030Sstevel@tonic-gate 	{ D_POL,	"pol" },
7040Sstevel@tonic-gate 	{ D_POL,	"policy" },
7050Sstevel@tonic-gate 	{ D_PROP,	"prop" },
7060Sstevel@tonic-gate 	{ D_DOOR,	"door" },
7070Sstevel@tonic-gate 	{ D_CONFIG,	"config" },
7080Sstevel@tonic-gate 	{ D_ALL,	"all" },
7090Sstevel@tonic-gate 	{ 0,		"0" },
7100Sstevel@tonic-gate };
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate int
7130Sstevel@tonic-gate dbgstr2num(char *str)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate 	keywdtab_t	*dp;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	for (dp = dbgtab; dp < A_END(dbgtab); dp++) {
7180Sstevel@tonic-gate 		if (strcasecmp(str, dp->kw_str) == 0)
7190Sstevel@tonic-gate 			return (dp->kw_tag);
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 	return (D_INVALID);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate int
7250Sstevel@tonic-gate parsedbgopts(char *optarg)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	char	*argp, *endp, op, nextop;
7280Sstevel@tonic-gate 	int	mask = 0, new;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	mask = strtol(optarg, &endp, 0);
7310Sstevel@tonic-gate 	if (*endp == '\0')
7320Sstevel@tonic-gate 		return (mask);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	op = optarg[0];
7350Sstevel@tonic-gate 	if (op != '-')
7360Sstevel@tonic-gate 		op = '+';
7370Sstevel@tonic-gate 	argp = strtok_d(optarg, "+-", &nextop);
7380Sstevel@tonic-gate 	do {
7390Sstevel@tonic-gate 		new = dbgstr2num(argp);
7400Sstevel@tonic-gate 		if (new == D_INVALID) {
7410Sstevel@tonic-gate 			/* we encountered an invalid keywd */
7420Sstevel@tonic-gate 			return (new);
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 		if (op == '+') {
7450Sstevel@tonic-gate 			mask |= new;
7460Sstevel@tonic-gate 		} else {
7470Sstevel@tonic-gate 			mask &= ~new;
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate 		op = nextop;
7500Sstevel@tonic-gate 	} while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	return (mask);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate  * functions to manipulate the kmcookie-label mapping file
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate /*
7610Sstevel@tonic-gate  * Open, lockf, fdopen the given file, returning a FILE * on success,
7620Sstevel@tonic-gate  * or NULL on failure.
7630Sstevel@tonic-gate  */
7640Sstevel@tonic-gate FILE *
7650Sstevel@tonic-gate kmc_open_and_lock(char *name)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	int	fd, rtnerr;
7680Sstevel@tonic-gate 	FILE	*fp;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
7710Sstevel@tonic-gate 		return (NULL);
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 	if (lockf(fd, F_LOCK, 0) < 0) {
7740Sstevel@tonic-gate 		return (NULL);
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 	if ((fp = fdopen(fd, "a+")) == NULL) {
7770Sstevel@tonic-gate 		return (NULL);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 	if (fseek(fp, 0, SEEK_SET) < 0) {
7800Sstevel@tonic-gate 		/* save errno in case fclose changes it */
7810Sstevel@tonic-gate 		rtnerr = errno;
7820Sstevel@tonic-gate 		(void) fclose(fp);
7830Sstevel@tonic-gate 		errno = rtnerr;
7840Sstevel@tonic-gate 		return (NULL);
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 	return (fp);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate  * Extract an integer cookie and string label from a line from the
7910Sstevel@tonic-gate  * kmcookie-label file.  Return -1 on failure, 0 on success.
7920Sstevel@tonic-gate  */
7930Sstevel@tonic-gate int
7940Sstevel@tonic-gate kmc_parse_line(char *line, int *cookie, char **label)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	char	*cookiestr;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	*cookie = 0;
7990Sstevel@tonic-gate 	*label = NULL;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	cookiestr = strtok(line, " \t\n");
8020Sstevel@tonic-gate 	if (cookiestr == NULL) {
8030Sstevel@tonic-gate 		return (-1);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/* Everything that follows, up to the newline, is the label. */
8070Sstevel@tonic-gate 	*label = strtok(NULL, "\n");
8080Sstevel@tonic-gate 	if (*label == NULL) {
8090Sstevel@tonic-gate 		return (-1);
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	*cookie = atoi(cookiestr);
8130Sstevel@tonic-gate 	return (0);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate /*
8170Sstevel@tonic-gate  * Insert a mapping into the file (if it's not already there), given the
8180Sstevel@tonic-gate  * new label.  Return the assigned cookie, or -1 on error.
8190Sstevel@tonic-gate  */
8200Sstevel@tonic-gate int
8210Sstevel@tonic-gate kmc_insert_mapping(char *label)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	FILE	*map;
8240Sstevel@tonic-gate 	char	linebuf[MAXLINESIZE];
8250Sstevel@tonic-gate 	char	*cur_label;
8260Sstevel@tonic-gate 	int	max_cookie = 0, cur_cookie, rtn_cookie;
8270Sstevel@tonic-gate 	int	rtnerr = 0;
8280Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	/* open and lock the file; will sleep until lock is available */
8310Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
8320Sstevel@tonic-gate 		/* kmc_open_and_lock() sets errno appropriately */
8330Sstevel@tonic-gate 		return (-1);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
8390Sstevel@tonic-gate 			rtnerr = EINVAL;
8400Sstevel@tonic-gate 			goto error;
8410Sstevel@tonic-gate 		}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 		if (cur_cookie > max_cookie)
8440Sstevel@tonic-gate 			max_cookie = cur_cookie;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		if ((!found) && (strcmp(cur_label, label) == 0)) {
8470Sstevel@tonic-gate 			found = B_TRUE;
8480Sstevel@tonic-gate 			rtn_cookie = cur_cookie;
8490Sstevel@tonic-gate 		}
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	if (!found) {
8530Sstevel@tonic-gate 		rtn_cookie = ++max_cookie;
8540Sstevel@tonic-gate 		if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) ||
8550Sstevel@tonic-gate 		    (fflush(map) < 0)) {
8560Sstevel@tonic-gate 			rtnerr = errno;
8570Sstevel@tonic-gate 			goto error;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 	(void) fclose(map);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	return (rtn_cookie);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate error:
8650Sstevel@tonic-gate 	(void) fclose(map);
8660Sstevel@tonic-gate 	errno = rtnerr;
8670Sstevel@tonic-gate 	return (-1);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate  * Lookup the given cookie and return its corresponding label.  Return
8720Sstevel@tonic-gate  * a pointer to the label on success, NULL on error (or if the label is
8730Sstevel@tonic-gate  * not found).  Note that the returned label pointer points to a static
8740Sstevel@tonic-gate  * string, so the label will be overwritten by a subsequent call to the
8750Sstevel@tonic-gate  * function; the function is also not thread-safe as a result.
8760Sstevel@tonic-gate  */
8770Sstevel@tonic-gate char *
8780Sstevel@tonic-gate kmc_lookup_by_cookie(int cookie)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	FILE		*map;
8810Sstevel@tonic-gate 	static char	linebuf[MAXLINESIZE];
8820Sstevel@tonic-gate 	char		*cur_label;
8830Sstevel@tonic-gate 	int		cur_cookie;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
8860Sstevel@tonic-gate 		return (NULL);
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
8920Sstevel@tonic-gate 			(void) fclose(map);
8930Sstevel@tonic-gate 			return (NULL);
8940Sstevel@tonic-gate 		}
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 		if (cookie == cur_cookie) {
8970Sstevel@tonic-gate 			(void) fclose(map);
8980Sstevel@tonic-gate 			return (cur_label);
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 	(void) fclose(map);
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	return (NULL);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate /*
9070Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
9080Sstevel@tonic-gate  * Return values include:
9090Sstevel@tonic-gate  *
9100Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
9110Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
9120Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
9130Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
9140Sstevel@tonic-gate  *		extv[0].
9150Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
9160Sstevel@tonic-gate  *		unknown header.
9170Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
9180Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
9190Sstevel@tonic-gate  *
9200Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
9210Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
9220Sstevel@tonic-gate  */
9230Sstevel@tonic-gate int
9240Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize,
9250Sstevel@tonic-gate     char *diag_buf, uint_t diag_buf_len)
9260Sstevel@tonic-gate {
9270Sstevel@tonic-gate 	int i;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if (diag_buf != NULL)
9300Sstevel@tonic-gate 		diag_buf[0] = '\0';
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	for (i = 1; i <= SPD_EXT_MAX; i++)
9330Sstevel@tonic-gate 		extv[i] = NULL;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	i = 0;
9360Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
9390Sstevel@tonic-gate 	msgsize = SPD_64TO8(msgsize);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	while ((char *)extv[0] < ((char *)basehdr + msgsize)) {
9420Sstevel@tonic-gate 		/* Check for unknown headers. */
9430Sstevel@tonic-gate 		i++;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
9460Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX) {
9470Sstevel@tonic-gate 			if (diag_buf != NULL) {
9480Sstevel@tonic-gate 				(void) snprintf(diag_buf, diag_buf_len,
9490Sstevel@tonic-gate 				    "spdsock ext 0x%X unknown: 0x%X",
9500Sstevel@tonic-gate 				    i, extv[0]->spd_ext_type);
9510Sstevel@tonic-gate 			}
9520Sstevel@tonic-gate 			return (KGE_UNK);
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 		/*
9560Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
9570Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
9580Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
9590Sstevel@tonic-gate 		 */
9600Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
9610Sstevel@tonic-gate 		    (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
9620Sstevel@tonic-gate 		    (uint8_t *)((uint8_t *)basehdr + msgsize))
9630Sstevel@tonic-gate 			return (KGE_LEN);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		/* Check for redundant headers. */
9660Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
9670Sstevel@tonic-gate 			return (KGE_DUP);
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
9700Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
9730Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
9740Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	/* Everything's cool. */
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	/*
9800Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
9810Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
9820Sstevel@tonic-gate 	 */
9830Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
9840Sstevel@tonic-gate 		extv[0] = NULL;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	return (KGE_OK);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate const char *
9900Sstevel@tonic-gate spdsock_diag(int diagnostic)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate 	switch (diagnostic) {
9930Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NONE:
9944064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no error"));
9950Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNKNOWN_EXT:
9964064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown extension"));
9970Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_EXTLEN:
9984064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad extension length"));
9990Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_RULE_EXT:
10004064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no rule extension"));
10010Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_ADDR_LEN:
10024064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad address len"));
10030Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MIXED_AF:
10044064Smarkfen 		return (dgettext(TEXT_DOMAIN, "mixed address family"));
10050Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_NO_MEM:
10064064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: no memory"));
10070Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT:
10084064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: wrong action count"));
10090Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_TYPE:
10104064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad type"));
10110Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_FLAGS:
10124064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad flags"));
10130Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_INCON_FLAGS:
10144064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: inconsistent flags"));
10150Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLPORT:
10164064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local port"));
10170Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT:
10184064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local port"));
10190Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMPORT:
10204064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote port"));
10210Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMPORT:
10224064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote port"));
10230Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_PROTO:
10244064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed proto"));
10250Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_PROTO:
10264064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate proto"));
10270Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLADDR:
10284064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local address"));
10290Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR:
10304064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local address"));
10310Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMADDR:
10324064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote address"));
10330Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMADDR:
10344064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote address"));
10350Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_ACTION:
10364064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed action"));
10370Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_ACTION:
10384064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate action"));
10390Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULE:
10404064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed rule"));
10410Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULE:
10424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate rule"));
10430Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULESET:
10444064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed ruleset"));
10450Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULESET:
10464064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate ruleset"));
10470Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_INVALID_RULE_INDEX:
10484064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid rule index"));
10490Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_SPDID:
10504064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad spdid"));
10510Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_MSG_TYPE:
10524064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad message type"));
10530Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_ALG:
10544064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm"));
10550Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG:
10564064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10574064Smarkfen 		    "unsupported ESP encryption algorithm"));
10580Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG:
10594064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10604064Smarkfen 		    "unsupported ESP authentication algorithm"));
10610Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE:
10624064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH key size"));
10630Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE:
10644064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10654064Smarkfen 		    "unsupported ESP encryption key size"));
10660Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE:
10674064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10684064Smarkfen 		    "unsupported ESP authentication key size"));
10690Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_ACTION_EXT:
10704064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No ACTION extension"));
10710Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_ID_RANGE:
10724064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer"));
10730Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES:
10744064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10754064Smarkfen 		    "number of key sizes inconsistent"));
10760Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES:
10774064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10784064Smarkfen 		    "number of block sizes inconsistent"));
10790Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN:
10804064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid mechanism name length"));
10813055Sdanmcd 	case SPD_DIAGNOSTIC_NOT_GLOBAL_OP:
10824064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10834064Smarkfen 		    "operation not applicable to all policies"));
10843055Sdanmcd 	case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS:
10854064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10864064Smarkfen 		    "using selectors on a transport-mode tunnel"));
10870Sstevel@tonic-gate 	default:
10884064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown diagnostic"));
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate  * PF_KEY Diagnostic table.
10940Sstevel@tonic-gate  *
10950Sstevel@tonic-gate  * PF_KEY NOTE:  If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is
10960Sstevel@tonic-gate  * where you need to add new messages.
10970Sstevel@tonic-gate  */
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate const char *
11000Sstevel@tonic-gate keysock_diag(int diagnostic)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate 	switch (diagnostic) {
11033055Sdanmcd 	case SADB_X_DIAGNOSTIC_NONE:
11044064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No diagnostic"));
11050Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_MSG:
11064064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown message type"));
11070Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_EXT:
11084064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown extension type"));
11090Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EXTLEN:
11104064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad extension length"));
11110Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE:
11124064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11134064Smarkfen 		    "Unknown Security Association type"));
11140Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SATYPE_NEEDED:
11154064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11164064Smarkfen 		    "Specific Security Association type needed"));
11170Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_SADBS:
11184064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11194064Smarkfen 		    "No Security Association Databases present"));
11200Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_EXT:
11214064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11224064Smarkfen 		    "No extensions needed for message"));
11230Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC_AF:
11244064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address family"));
11250Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST_AF:
11264064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11274064Smarkfen 		    "Bad destination address family"));
11280Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_PROXY_AF:
11294064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11304064Smarkfen 		    "Bad inner-source address family"));
11310Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AF_MISMATCH:
11324064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11334064Smarkfen 		    "Source/destination address family mismatch"));
11340Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC:
11354064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address value"));
11360Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST:
11374064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad destination address value"));
11380Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ALLOC_HSERR:
11394064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11404064Smarkfen 		    "Soft allocations limit more than hard limit"));
11410Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BYTES_HSERR:
11424064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11434064Smarkfen 		    "Soft bytes limit more than hard limit"));
11440Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ADDTIME_HSERR:
11454064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft add expiration time later "
11460Sstevel@tonic-gate 		    "than hard expiration time"));
11470Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_USETIME_HSERR:
11484064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft use expiration time later "
11490Sstevel@tonic-gate 		    "than hard expiration time"));
11500Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SRC:
11514064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing source address"));
11520Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_DST:
11534064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing destination address"));
11540Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SA:
11554064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SA extension"));
11560Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_EKEY:
11574064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing encryption key"));
11580Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_AKEY:
11594064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing authentication key"));
11600Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_RANGE:
11614064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SPI range"));
11620Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SRC:
11634064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate source address"));
11640Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_DST:
11654064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate destination address"));
11660Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SA:
11674064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SA extension"));
11680Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY:
11694064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate encryption key"));
11700Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY:
11714064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate authentication key"));
11720Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE:
11734064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SPI range"));
11740Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SRC:
11754064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed source address"));
11760Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_DST:
11774064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed destination address"));
11780Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SA:
11794064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SA extension"));
11800Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_EKEY:
11814064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed encryption key"));
11820Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_AKEY:
11834064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed authentication key"));
11840Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_RANGE:
11854064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SPI range"));
11860Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AKEY_PRESENT:
11874064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Authentication key not needed"));
11880Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_EKEY_PRESENT:
11894064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Encryption key not needed"));
11900Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_PROP_PRESENT:
11914064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Proposal extension not needed"));
11920Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SUPP_PRESENT:
11934064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11944064Smarkfen 		    "Supported algorithms extension not needed"));
11950Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AALG:
11964064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11974064Smarkfen 		    "Unsupported authentication algorithm"));
11980Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EALG:
11994064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12004064Smarkfen 		    "Unsupported encryption algorithm"));
12010Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SAFLAGS:
12024064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA flags"));
12030Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SASTATE:
12044064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA state"));
12050Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AKEYBITS:
12064064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12074064Smarkfen 		    "Bad number of authentication bits"));
12080Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EKEYBITS:
12094064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12104064Smarkfen 		    "Bad number of encryption bits"));
12110Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP:
12124064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12134064Smarkfen 		    "Encryption not supported for this SA type"));
12140Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_EKEY:
12154064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak encryption key"));
12160Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_AKEY:
12174064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak authentication key"));
12180Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMP:
12194064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12204064Smarkfen 		    "Duplicate key management protocol"));
12210Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMC:
12224064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12234064Smarkfen 		    "Duplicate key management cookie"));
12240Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC:
12254064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address"));
12260Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_REM:
12274064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address"));
12280Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC:
12294064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address"));
12300Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM:
12314064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12324064Smarkfen 		    "Duplicate NAT-T remote address"));
12330Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC:
12344064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address"));
12350Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM:
12364064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12374064Smarkfen 		    "Malformed NAT-T remote address"));
12380Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS:
12394064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports"));
12403055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC:
12414064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing inner source address"));
12423055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_DST:
12434064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12444064Smarkfen 		    "Missing inner destination address"));
12453055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC:
12464064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12474064Smarkfen 		    "Duplicate inner source address"));
12483055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST:
12494064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12504064Smarkfen 		    "Duplicate inner destination address"));
12513055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC:
12524064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12534064Smarkfen 		    "Malformed inner source address"));
12543055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST:
12554064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12564064Smarkfen 		    "Malformed inner destination address"));
12573055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC:
12584064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12594064Smarkfen 		    "Invalid inner-source prefix length "));
12603055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST:
12614064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12624064Smarkfen 		    "Invalid inner-destination prefix length"));
12633055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF:
12644064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12654064Smarkfen 		    "Bad inner-destination address family"));
12663055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH:
12674064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12683055Sdanmcd 		    "Inner source/destination address family mismatch"));
12693055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF:
12704064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12714064Smarkfen 		    "Bad NAT-T remote address family"));
12723055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF:
12734064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12744064Smarkfen 		    "Bad NAT-T local address family"));
12753055Sdanmcd 	case SADB_X_DIAGNOSTIC_PROTO_MISMATCH:
12764064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12774064Smarkfen 		    "Source/desination protocol mismatch"));
12783055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH:
12794064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12804064Smarkfen 		    "Inner source/desination protocol mismatch"));
12813055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS:
12824064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12834064Smarkfen 		    "Both inner ports and outer ports are set"));
12840Sstevel@tonic-gate 	default:
12854064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code"));
12860Sstevel@tonic-gate 	}
12870Sstevel@tonic-gate }
12883055Sdanmcd 
12893055Sdanmcd /*
12903055Sdanmcd  * Convert an IPv6 mask to a prefix len.  I assume all IPv6 masks are
12913055Sdanmcd  * contiguous, so I stop at the first zero bit!
12923055Sdanmcd  */
12933055Sdanmcd int
12943055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped)
12953055Sdanmcd {
12963055Sdanmcd 	int rc = 0;
12973055Sdanmcd 	uint8_t last;
12983055Sdanmcd 	int limit = IPV6_ABITS;
12993055Sdanmcd 
13003055Sdanmcd 	if (is_v4mapped) {
13013055Sdanmcd 		mask += ((IPV6_ABITS - IP_ABITS)/8);
13023055Sdanmcd 		limit = IP_ABITS;
13033055Sdanmcd 	}
13043055Sdanmcd 
13053055Sdanmcd 	while (*mask == 0xff) {
13063055Sdanmcd 		rc += 8;
13073055Sdanmcd 		if (rc == limit)
13083055Sdanmcd 			return (limit);
13093055Sdanmcd 		mask++;
13103055Sdanmcd 	}
13113055Sdanmcd 
13123055Sdanmcd 	last = *mask;
13133055Sdanmcd 	while (last != 0) {
13143055Sdanmcd 		rc++;
13153055Sdanmcd 		last = (last << 1) & 0xff;
13163055Sdanmcd 	}
13173055Sdanmcd 
13183055Sdanmcd 	return (rc);
13193055Sdanmcd }
13203055Sdanmcd 
13213055Sdanmcd /*
13223055Sdanmcd  * Expand the diagnostic code into a message.
13233055Sdanmcd  */
13243055Sdanmcd void
13253055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic)
13263055Sdanmcd {
13273055Sdanmcd 	/* Use two spaces so above strings can fit on the line. */
13284064Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
13294064Smarkfen 	    "  Diagnostic code %u:  %s.\n"),
13303055Sdanmcd 	    diagnostic, keysock_diag(diagnostic));
13313055Sdanmcd }
13323055Sdanmcd 
13333055Sdanmcd /*
13343055Sdanmcd  * Prints the base PF_KEY message.
13353055Sdanmcd  */
13363055Sdanmcd void
13373055Sdanmcd print_sadb_msg(struct sadb_msg *samsg, time_t wallclock, boolean_t vflag)
13383055Sdanmcd {
13393055Sdanmcd 	if (wallclock != 0)
13404064Smarkfen 		printsatime(wallclock, dgettext(TEXT_DOMAIN,
13414064Smarkfen 		    "%sTimestamp: %s\n"), "", NULL,
13423055Sdanmcd 		    vflag);
13433055Sdanmcd 
13444064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, "Base message (version %u) type "),
13453055Sdanmcd 	    samsg->sadb_msg_version);
13463055Sdanmcd 	switch (samsg->sadb_msg_type) {
13473055Sdanmcd 	case SADB_RESERVED:
13484064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
13494064Smarkfen 		    "RESERVED (warning: set to 0)"));
13503055Sdanmcd 		break;
13513055Sdanmcd 	case SADB_GETSPI:
13523055Sdanmcd 		(void) printf("GETSPI");
13533055Sdanmcd 		break;
13543055Sdanmcd 	case SADB_UPDATE:
13553055Sdanmcd 		(void) printf("UPDATE");
13563055Sdanmcd 		break;
13573055Sdanmcd 	case SADB_ADD:
13583055Sdanmcd 		(void) printf("ADD");
13593055Sdanmcd 		break;
13603055Sdanmcd 	case SADB_DELETE:
13613055Sdanmcd 		(void) printf("DELETE");
13623055Sdanmcd 		break;
13633055Sdanmcd 	case SADB_GET:
13643055Sdanmcd 		(void) printf("GET");
13653055Sdanmcd 		break;
13663055Sdanmcd 	case SADB_ACQUIRE:
13673055Sdanmcd 		(void) printf("ACQUIRE");
13683055Sdanmcd 		break;
13693055Sdanmcd 	case SADB_REGISTER:
13703055Sdanmcd 		(void) printf("REGISTER");
13713055Sdanmcd 		break;
13723055Sdanmcd 	case SADB_EXPIRE:
13733055Sdanmcd 		(void) printf("EXPIRE");
13743055Sdanmcd 		break;
13753055Sdanmcd 	case SADB_FLUSH:
13763055Sdanmcd 		(void) printf("FLUSH");
13773055Sdanmcd 		break;
13783055Sdanmcd 	case SADB_DUMP:
13793055Sdanmcd 		(void) printf("DUMP");
13803055Sdanmcd 		break;
13813055Sdanmcd 	case SADB_X_PROMISC:
13823055Sdanmcd 		(void) printf("X_PROMISC");
13833055Sdanmcd 		break;
13843055Sdanmcd 	case SADB_X_INVERSE_ACQUIRE:
13853055Sdanmcd 		(void) printf("X_INVERSE_ACQUIRE");
13863055Sdanmcd 		break;
13873055Sdanmcd 	default:
13884064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
13894064Smarkfen 		    "Unknown (%u)"), samsg->sadb_msg_type);
13903055Sdanmcd 		break;
13913055Sdanmcd 	}
13924064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, ", SA type "));
13933055Sdanmcd 
13943055Sdanmcd 	switch (samsg->sadb_msg_satype) {
13953055Sdanmcd 	case SADB_SATYPE_UNSPEC:
13964064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "<unspecified/all>"));
13973055Sdanmcd 		break;
13983055Sdanmcd 	case SADB_SATYPE_AH:
13993055Sdanmcd 		(void) printf("AH");
14003055Sdanmcd 		break;
14013055Sdanmcd 	case SADB_SATYPE_ESP:
14023055Sdanmcd 		(void) printf("ESP");
14033055Sdanmcd 		break;
14043055Sdanmcd 	case SADB_SATYPE_RSVP:
14053055Sdanmcd 		(void) printf("RSVP");
14063055Sdanmcd 		break;
14073055Sdanmcd 	case SADB_SATYPE_OSPFV2:
14083055Sdanmcd 		(void) printf("OSPFv2");
14093055Sdanmcd 		break;
14103055Sdanmcd 	case SADB_SATYPE_RIPV2:
14113055Sdanmcd 		(void) printf("RIPv2");
14123055Sdanmcd 		break;
14133055Sdanmcd 	case SADB_SATYPE_MIP:
14144064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Mobile IP"));
14153055Sdanmcd 		break;
14163055Sdanmcd 	default:
14174064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14184064Smarkfen 		    "<unknown %u>"), samsg->sadb_msg_satype);
14193055Sdanmcd 		break;
14203055Sdanmcd 	}
14213055Sdanmcd 
14223055Sdanmcd 	(void) printf(".\n");
14233055Sdanmcd 
14243055Sdanmcd 	if (samsg->sadb_msg_errno != 0) {
14254064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Error %s from PF_KEY.\n"),
14263055Sdanmcd 		    strerror(samsg->sadb_msg_errno));
14273055Sdanmcd 		print_diagnostic(stdout, samsg->sadb_x_msg_diagnostic);
14283055Sdanmcd 	}
14293055Sdanmcd 
14304064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
14314064Smarkfen 	    "Message length %u bytes, seq=%u, pid=%u.\n"),
14323055Sdanmcd 	    SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq,
14333055Sdanmcd 	    samsg->sadb_msg_pid);
14343055Sdanmcd }
14353055Sdanmcd 
14363055Sdanmcd /*
14373055Sdanmcd  * Print the SA extension for PF_KEY.
14383055Sdanmcd  */
14393055Sdanmcd void
14403055Sdanmcd print_sa(char *prefix, struct sadb_sa *assoc)
14413055Sdanmcd {
14423055Sdanmcd 	if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) {
14434064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
14444064Smarkfen 		    "WARNING: SA info extension length (%u) is bad."),
14453055Sdanmcd 		    SADB_64TO8(assoc->sadb_sa_len));
14463055Sdanmcd 	}
14473055Sdanmcd 
14484064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
14494064Smarkfen 	    "%sSADB_ASSOC spi=0x%x, replay=%u, state="),
14503055Sdanmcd 	    prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay);
14513055Sdanmcd 	switch (assoc->sadb_sa_state) {
14523055Sdanmcd 	case SADB_SASTATE_LARVAL:
14534064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "LARVAL"));
14543055Sdanmcd 		break;
14553055Sdanmcd 	case SADB_SASTATE_MATURE:
14564064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "MATURE"));
14573055Sdanmcd 		break;
14583055Sdanmcd 	case SADB_SASTATE_DYING:
14594064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "DYING"));
14603055Sdanmcd 		break;
14613055Sdanmcd 	case SADB_SASTATE_DEAD:
14624064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "DEAD"));
14633055Sdanmcd 		break;
14643055Sdanmcd 	default:
14654064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14664064Smarkfen 		    "<unknown %u>"), assoc->sadb_sa_state);
14673055Sdanmcd 	}
14683055Sdanmcd 
14693055Sdanmcd 	if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
14704064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14714064Smarkfen 		    "\n%sAuthentication algorithm = "),
14723055Sdanmcd 		    prefix);
14733055Sdanmcd 		(void) dump_aalg(assoc->sadb_sa_auth, stdout);
14743055Sdanmcd 	}
14753055Sdanmcd 
14763055Sdanmcd 	if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
14774064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
14784064Smarkfen 		    "\n%sEncryption algorithm = "), prefix);
14793055Sdanmcd 		(void) dump_ealg(assoc->sadb_sa_encrypt, stdout);
14803055Sdanmcd 	}
14813055Sdanmcd 
14824064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix,
14833055Sdanmcd 	    assoc->sadb_sa_flags);
14843055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS)
14853055Sdanmcd 		(void) printf("PFS ");
14863055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY)
14873055Sdanmcd 		(void) printf("NOREPLAY ");
14883055Sdanmcd 
14893055Sdanmcd 	/* BEGIN Solaris-specific flags. */
14903055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED)
14913055Sdanmcd 		(void) printf("X_USED ");
14923055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE)
14933055Sdanmcd 		(void) printf("X_UNIQUE ");
14943055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1)
14953055Sdanmcd 		(void) printf("X_AALG1 ");
14963055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2)
14973055Sdanmcd 		(void) printf("X_AALG2 ");
14983055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1)
14993055Sdanmcd 		(void) printf("X_EALG1 ");
15003055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2)
15013055Sdanmcd 		(void) printf("X_EALG2 ");
15023055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC)
15033055Sdanmcd 		(void) printf("X_NATT_LOC ");
15043055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM)
15053055Sdanmcd 		(void) printf("X_NATT_REM ");
15063055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
15073055Sdanmcd 		(void) printf("X_TUNNEL ");
15083055Sdanmcd 	/* END Solaris-specific flags. */
15093055Sdanmcd 
15103055Sdanmcd 	(void) printf(">\n");
15113055Sdanmcd }
15123055Sdanmcd 
15133055Sdanmcd void
15143055Sdanmcd printsatime(int64_t lt, const char *msg, const char *pfx, const char *pfx2,
15153055Sdanmcd     boolean_t vflag)
15163055Sdanmcd {
15173055Sdanmcd 	char tbuf[TBUF_SIZE]; /* For strftime() call. */
15183055Sdanmcd 	const char *tp = tbuf;
15193055Sdanmcd 	time_t t = lt;
15203055Sdanmcd 	struct tm res;
15213055Sdanmcd 
15223055Sdanmcd 	if (t != lt) {
15233055Sdanmcd 		if (lt > 0)
15243055Sdanmcd 			t = LONG_MAX;
15253055Sdanmcd 		else
15263055Sdanmcd 			t = LONG_MIN;
15273055Sdanmcd 	}
15283055Sdanmcd 
15293055Sdanmcd 	if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0)
15304064Smarkfen 		tp = dgettext(TEXT_DOMAIN, "<time conversion failed>");
15313055Sdanmcd 	(void) printf(msg, pfx, tp);
15323055Sdanmcd 	if (vflag && (pfx2 != NULL))
15334064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
15344064Smarkfen 		    "%s\t(raw time value %llu)\n"), pfx2, lt);
15353055Sdanmcd }
15363055Sdanmcd 
15373055Sdanmcd /*
15383055Sdanmcd  * Print the SA lifetime information.  (An SADB_EXT_LIFETIME_* extension.)
15393055Sdanmcd  */
15403055Sdanmcd void
15413055Sdanmcd print_lifetimes(time_t wallclock, struct sadb_lifetime *current,
15423055Sdanmcd     struct sadb_lifetime *hard, struct sadb_lifetime *soft, boolean_t vflag)
15433055Sdanmcd {
15443055Sdanmcd 	int64_t scratch;
15454064Smarkfen 	char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: ");
15464064Smarkfen 	char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: ");
15474064Smarkfen 	char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: ");
15483055Sdanmcd 
15493055Sdanmcd 	if (current != NULL &&
15503055Sdanmcd 	    current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) {
15514064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
15524064Smarkfen 		    "WARNING: CURRENT lifetime extension length (%u) is bad."),
15533055Sdanmcd 		    SADB_64TO8(current->sadb_lifetime_len));
15543055Sdanmcd 	}
15553055Sdanmcd 
15563055Sdanmcd 	if (hard != NULL &&
15573055Sdanmcd 	    hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) {
15584064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "WARNING: HARD lifetime "
15593055Sdanmcd 			"extension length (%u) is bad."),
15603055Sdanmcd 		    SADB_64TO8(hard->sadb_lifetime_len));
15613055Sdanmcd 	}
15623055Sdanmcd 
15633055Sdanmcd 	if (soft != NULL &&
15643055Sdanmcd 	    soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) {
15654064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "WARNING: SOFT lifetime "
15663055Sdanmcd 		    "extension length (%u) is bad."),
15673055Sdanmcd 		    SADB_64TO8(soft->sadb_lifetime_len));
15683055Sdanmcd 	}
15693055Sdanmcd 
15703055Sdanmcd 	(void) printf(" LT: Lifetime information\n");
15713055Sdanmcd 
15723055Sdanmcd 	if (current != NULL) {
15733055Sdanmcd 		/* Express values as current values. */
15744064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
15753055Sdanmcd 		    "%s%llu bytes protected, %u allocations used.\n"),
15763055Sdanmcd 		    current_prefix, current->sadb_lifetime_bytes,
15773055Sdanmcd 		    current->sadb_lifetime_allocations);
15783055Sdanmcd 		printsatime(current->sadb_lifetime_addtime,
15794064Smarkfen 		    dgettext(TEXT_DOMAIN, "%sSA added at time %s\n"),
15803055Sdanmcd 		    current_prefix, current_prefix, vflag);
15813055Sdanmcd 		if (current->sadb_lifetime_usetime != 0) {
15823055Sdanmcd 			printsatime(current->sadb_lifetime_usetime,
15834064Smarkfen 			    dgettext(TEXT_DOMAIN,
15844064Smarkfen 			    "%sSA first used at time %s\n"),
15853055Sdanmcd 			    current_prefix, current_prefix, vflag);
15863055Sdanmcd 		}
15874064Smarkfen 		printsatime(wallclock, dgettext(TEXT_DOMAIN,
15884064Smarkfen 		    "%sTime now is %s\n"), current_prefix, current_prefix,
15894064Smarkfen 		    vflag);
15903055Sdanmcd 	}
15913055Sdanmcd 
15923055Sdanmcd 	if (soft != NULL) {
15934064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
15944064Smarkfen 		    "%sSoft lifetime information:  "),
15953055Sdanmcd 		    soft_prefix);
15964064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
15974064Smarkfen 		    "%llu bytes of lifetime, %u "
15983055Sdanmcd 		    "allocations.\n"), soft->sadb_lifetime_bytes,
15993055Sdanmcd 		    soft->sadb_lifetime_allocations);
16004064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16014064Smarkfen 		    "%s%llu seconds of post-add lifetime.\n"),
16023055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_addtime);
16034064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16044064Smarkfen 		    "%s%llu seconds of post-use lifetime.\n"),
16053055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_usetime);
16063055Sdanmcd 		/* If possible, express values as time remaining. */
16073055Sdanmcd 		if (current != NULL) {
16083055Sdanmcd 			if (soft->sadb_lifetime_bytes != 0)
16094064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
16103055Sdanmcd 				    "%s%llu more bytes can be protected.\n"),
16113055Sdanmcd 				    soft_prefix,
16123055Sdanmcd 				    (soft->sadb_lifetime_bytes >
16133055Sdanmcd 					current->sadb_lifetime_bytes) ?
16143055Sdanmcd 				    (soft->sadb_lifetime_bytes -
16153055Sdanmcd 					current->sadb_lifetime_bytes) : (0));
16163055Sdanmcd 			if (soft->sadb_lifetime_addtime != 0 ||
16173055Sdanmcd 			    (soft->sadb_lifetime_usetime != 0 &&
16183055Sdanmcd 				current->sadb_lifetime_usetime != 0)) {
16193055Sdanmcd 				int64_t adddelta, usedelta;
16203055Sdanmcd 
16213055Sdanmcd 				if (soft->sadb_lifetime_addtime != 0) {
16223055Sdanmcd 					adddelta =
16233055Sdanmcd 					    current->sadb_lifetime_addtime +
16243055Sdanmcd 					    soft->sadb_lifetime_addtime -
16253055Sdanmcd 					    wallclock;
16263055Sdanmcd 				} else {
16273055Sdanmcd 					adddelta = TIME_MAX;
16283055Sdanmcd 				}
16293055Sdanmcd 
16303055Sdanmcd 				if (soft->sadb_lifetime_usetime != 0 &&
16313055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
16323055Sdanmcd 					usedelta =
16333055Sdanmcd 					    current->sadb_lifetime_usetime +
16343055Sdanmcd 					    soft->sadb_lifetime_usetime -
16353055Sdanmcd 					    wallclock;
16363055Sdanmcd 				} else {
16373055Sdanmcd 					usedelta = TIME_MAX;
16383055Sdanmcd 				}
16393055Sdanmcd 				(void) printf("%s", soft_prefix);
16403055Sdanmcd 				scratch = MIN(adddelta, usedelta);
16413055Sdanmcd 				if (scratch >= 0) {
16424064Smarkfen 					(void) printf(dgettext(TEXT_DOMAIN,
16434064Smarkfen 					    "Soft expiration occurs in %lld "
16444064Smarkfen 					    "seconds, "), scratch);
16453055Sdanmcd 				} else {
16464064Smarkfen 					(void) printf(dgettext(TEXT_DOMAIN,
16473055Sdanmcd 					    "Soft expiration occurred "));
16483055Sdanmcd 				}
16493055Sdanmcd 				scratch += wallclock;
16504064Smarkfen 				printsatime(scratch, dgettext(TEXT_DOMAIN,
16514064Smarkfen 				    "%sat %s.\n"), "", soft_prefix, vflag);
16523055Sdanmcd 			}
16533055Sdanmcd 		}
16543055Sdanmcd 	}
16553055Sdanmcd 
16563055Sdanmcd 	if (hard != NULL) {
16574064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16584064Smarkfen 		    "%sHard lifetime information:  "), hard_prefix);
16594064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "%llu bytes of lifetime, "
16603055Sdanmcd 		    "%u allocations.\n"), hard->sadb_lifetime_bytes,
16613055Sdanmcd 		    hard->sadb_lifetime_allocations);
16624064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16634064Smarkfen 		    "%s%llu seconds of post-add lifetime.\n"),
16643055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_addtime);
16654064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
16664064Smarkfen 		    "%s%llu seconds of post-use lifetime.\n"),
16673055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_usetime);
16683055Sdanmcd 		/* If possible, express values as time remaining. */
16693055Sdanmcd 		if (current != NULL) {
16703055Sdanmcd 			if (hard->sadb_lifetime_bytes != 0)
16714064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
16723055Sdanmcd 				    "%s%llu more bytes can be protected.\n"),
16733055Sdanmcd 				    hard_prefix,
16743055Sdanmcd 				    (hard->sadb_lifetime_bytes >
16753055Sdanmcd 					current->sadb_lifetime_bytes) ?
16763055Sdanmcd 				    (hard->sadb_lifetime_bytes -
16773055Sdanmcd 					current->sadb_lifetime_bytes) : (0));
16783055Sdanmcd 			if (hard->sadb_lifetime_addtime != 0 ||
16793055Sdanmcd 			    (hard->sadb_lifetime_usetime != 0 &&
16803055Sdanmcd 				current->sadb_lifetime_usetime != 0)) {
16813055Sdanmcd 				int64_t adddelta, usedelta;
16823055Sdanmcd 
16833055Sdanmcd 				if (hard->sadb_lifetime_addtime != 0) {
16843055Sdanmcd 					adddelta =
16853055Sdanmcd 					    current->sadb_lifetime_addtime +
16863055Sdanmcd 					    hard->sadb_lifetime_addtime -
16873055Sdanmcd 					    wallclock;
16883055Sdanmcd 				} else {
16893055Sdanmcd 					adddelta = TIME_MAX;
16903055Sdanmcd 				}
16913055Sdanmcd 
16923055Sdanmcd 				if (hard->sadb_lifetime_usetime != 0 &&
16933055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
16943055Sdanmcd 					usedelta =
16953055Sdanmcd 					    current->sadb_lifetime_usetime +
16963055Sdanmcd 					    hard->sadb_lifetime_usetime -
16973055Sdanmcd 					    wallclock;
16983055Sdanmcd 				} else {
16993055Sdanmcd 					usedelta = TIME_MAX;
17003055Sdanmcd 				}
17013055Sdanmcd 				(void) printf("%s", hard_prefix);
17023055Sdanmcd 				scratch = MIN(adddelta, usedelta);
17033055Sdanmcd 				if (scratch >= 0) {
17044064Smarkfen 					(void) printf(dgettext(TEXT_DOMAIN,
17054064Smarkfen 					    "Hard expiration occurs in %lld "
17064064Smarkfen 					    "seconds, "), scratch);
17073055Sdanmcd 				} else {
17084064Smarkfen 					(void) printf(dgettext(TEXT_DOMAIN,
17093055Sdanmcd 					    "Hard expiration occured "));
17103055Sdanmcd 				}
17113055Sdanmcd 				scratch += wallclock;
17124064Smarkfen 				printsatime(scratch, dgettext(TEXT_DOMAIN,
17134064Smarkfen 				    "%sat %s.\n"), "", hard_prefix, vflag);
17143055Sdanmcd 			}
17153055Sdanmcd 		}
17163055Sdanmcd 	}
17173055Sdanmcd }
17183055Sdanmcd 
17193055Sdanmcd /*
17203055Sdanmcd  * Print an SADB_EXT_ADDRESS_* extension.
17213055Sdanmcd  */
17223055Sdanmcd void
17233055Sdanmcd print_address(char *prefix, struct sadb_address *addr)
17243055Sdanmcd {
17253055Sdanmcd 	struct protoent *pe;
17263055Sdanmcd 
17273055Sdanmcd 	(void) printf("%s", prefix);
17283055Sdanmcd 	switch (addr->sadb_address_exttype) {
17293055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
17304064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Source address "));
17313055Sdanmcd 		break;
17323055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
17334064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Inner source address "));
17343055Sdanmcd 		break;
17353055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
17364064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Destination address "));
17373055Sdanmcd 		break;
17383055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
17394064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
17404064Smarkfen 		    "Inner destination address "));
17413055Sdanmcd 		break;
17423055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
17434064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "NAT-T local address "));
17443055Sdanmcd 		break;
17453055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
17464064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "NAT-T remote address "));
17473055Sdanmcd 		break;
17483055Sdanmcd 	}
17493055Sdanmcd 
17504064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
17514064Smarkfen 	    "(proto=%d"), addr->sadb_address_proto);
17523055Sdanmcd 	if (!nflag) {
17533055Sdanmcd 		if (addr->sadb_address_proto == 0) {
17544064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "/<unspecified>"));
17553055Sdanmcd 		} else if ((pe = getprotobynumber(addr->sadb_address_proto))
17563055Sdanmcd 		    != NULL) {
17573055Sdanmcd 			(void) printf("/%s", pe->p_name);
17583055Sdanmcd 		} else {
17594064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "/<unknown>"));
17603055Sdanmcd 		}
17613055Sdanmcd 	}
17624064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, ")\n%s"), prefix);
17633055Sdanmcd 	(void) dump_sockaddr((struct sockaddr *)(addr + 1),
17643055Sdanmcd 	    addr->sadb_address_prefixlen, B_FALSE, stdout);
17653055Sdanmcd }
17663055Sdanmcd 
17673055Sdanmcd /*
17683055Sdanmcd  * Print an SADB_EXT_KEY extension.
17693055Sdanmcd  */
17703055Sdanmcd void
17713055Sdanmcd print_key(char *prefix, struct sadb_key *key)
17723055Sdanmcd {
17733055Sdanmcd 	(void) printf("%s", prefix);
17743055Sdanmcd 
17753055Sdanmcd 	switch (key->sadb_key_exttype) {
17763055Sdanmcd 	case SADB_EXT_KEY_AUTH:
17774064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Authentication"));
17783055Sdanmcd 		break;
17793055Sdanmcd 	case SADB_EXT_KEY_ENCRYPT:
17804064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Encryption"));
17813055Sdanmcd 		break;
17823055Sdanmcd 	}
17833055Sdanmcd 
17844064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, " key.\n%s"), prefix);
17853055Sdanmcd 	(void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits, stdout);
17863055Sdanmcd 	(void) putchar('\n');
17873055Sdanmcd }
17883055Sdanmcd 
17893055Sdanmcd /*
17903055Sdanmcd  * Print an SADB_EXT_IDENTITY_* extension.
17913055Sdanmcd  */
17923055Sdanmcd void
17933055Sdanmcd print_ident(char *prefix, struct sadb_ident *id)
17943055Sdanmcd {
17953055Sdanmcd 	boolean_t canprint = B_TRUE;
17963055Sdanmcd 
17973055Sdanmcd 	(void) printf("%s", prefix);
17983055Sdanmcd 	switch (id->sadb_ident_exttype) {
17993055Sdanmcd 	case SADB_EXT_IDENTITY_SRC:
18004064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Source"));
18013055Sdanmcd 		break;
18023055Sdanmcd 	case SADB_EXT_IDENTITY_DST:
18034064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "Destination"));
18043055Sdanmcd 		break;
18053055Sdanmcd 	}
18063055Sdanmcd 
18074064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
18084064Smarkfen 	    " identity, uid=%d, type "), id->sadb_ident_id);
18093055Sdanmcd 	canprint = dump_sadb_idtype(id->sadb_ident_type, stdout, NULL);
18103055Sdanmcd 	(void) printf("\n%s", prefix);
18113055Sdanmcd 	if (canprint)
18123055Sdanmcd 		(void) printf("%s\n", (char *)(id + 1));
18133055Sdanmcd 	else
18144064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "<cannot print>\n"));
18153055Sdanmcd }
18163055Sdanmcd 
18173055Sdanmcd /*
18183055Sdanmcd  * Print an SADB_SENSITIVITY extension.
18193055Sdanmcd  */
18203055Sdanmcd void
18213055Sdanmcd print_sens(char *prefix, struct sadb_sens *sens)
18223055Sdanmcd {
18233055Sdanmcd 	uint64_t *bitmap = (uint64_t *)(sens + 1);
18243055Sdanmcd 	int i;
18253055Sdanmcd 
18263055Sdanmcd 	(void) printf(
18274064Smarkfen 	    dgettext(TEXT_DOMAIN,
18284064Smarkfen 	    "%sSensitivity DPD %d, sens level=%d, integ level=%d\n"),
18293055Sdanmcd 	    prefix, sens->sadb_sens_dpd, sens->sadb_sens_sens_level,
18303055Sdanmcd 	    sens->sadb_sens_integ_level);
18313055Sdanmcd 	for (i = 0; sens->sadb_sens_sens_len-- > 0; i++, bitmap++)
18323055Sdanmcd 		(void) printf(
18334064Smarkfen 		    dgettext(TEXT_DOMAIN,
18344064Smarkfen 		    "%s Sensitivity BM extended word %d 0x%llx\n"), i, *bitmap);
18353055Sdanmcd 	for (i = 0; sens->sadb_sens_integ_len-- > 0; i++, bitmap++)
18363055Sdanmcd 		(void) printf(
18374064Smarkfen 		    dgettext(TEXT_DOMAIN,
18384064Smarkfen 		    "%s Integrity BM extended word %d 0x%llx\n"), i, *bitmap);
18393055Sdanmcd }
18403055Sdanmcd 
18413055Sdanmcd /*
18423055Sdanmcd  * Print an SADB_EXT_PROPOSAL extension.
18433055Sdanmcd  */
18443055Sdanmcd void
18453055Sdanmcd print_prop(char *prefix, struct sadb_prop *prop)
18463055Sdanmcd {
18473055Sdanmcd 	struct sadb_comb *combs;
18483055Sdanmcd 	int i, numcombs;
18493055Sdanmcd 
18504064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
18514064Smarkfen 	    "%sProposal, replay counter = %u.\n"), prefix,
18523055Sdanmcd 	    prop->sadb_prop_replay);
18533055Sdanmcd 
18543055Sdanmcd 	numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop));
18553055Sdanmcd 	numcombs /= SADB_8TO64(sizeof (*combs));
18563055Sdanmcd 
18573055Sdanmcd 	combs = (struct sadb_comb *)(prop + 1);
18583055Sdanmcd 
18593055Sdanmcd 	for (i = 0; i < numcombs; i++) {
18604064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
18614064Smarkfen 		    "%s Combination #%u "), prefix, i + 1);
18623055Sdanmcd 		if (combs[i].sadb_comb_auth != SADB_AALG_NONE) {
18634064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
18644064Smarkfen 			    "Authentication = "));
18653055Sdanmcd 			(void) dump_aalg(combs[i].sadb_comb_auth, stdout);
18664064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
18674064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
18683055Sdanmcd 			    combs[i].sadb_comb_auth_minbits,
18693055Sdanmcd 			    combs[i].sadb_comb_auth_maxbits, prefix);
18703055Sdanmcd 		}
18713055Sdanmcd 
18723055Sdanmcd 		if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) {
18734064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "Encryption = "));
18743055Sdanmcd 			(void) dump_ealg(combs[i].sadb_comb_encrypt, stdout);
18754064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
18764064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
18773055Sdanmcd 			    combs[i].sadb_comb_encrypt_minbits,
18783055Sdanmcd 			    combs[i].sadb_comb_encrypt_maxbits, prefix);
18793055Sdanmcd 		}
18803055Sdanmcd 
18814064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "HARD: "));
18823055Sdanmcd 		if (combs[i].sadb_comb_hard_allocations)
18834064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "alloc=%u "),
18843055Sdanmcd 			    combs[i].sadb_comb_hard_allocations);
18853055Sdanmcd 		if (combs[i].sadb_comb_hard_bytes)
18864064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "bytes=%llu "),
18873055Sdanmcd 			    combs[i].sadb_comb_hard_bytes);
18883055Sdanmcd 		if (combs[i].sadb_comb_hard_addtime)
18894064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
18904064Smarkfen 			    "post-add secs=%llu "),
18913055Sdanmcd 			    combs[i].sadb_comb_hard_addtime);
18923055Sdanmcd 		if (combs[i].sadb_comb_hard_usetime)
18934064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
18944064Smarkfen 			    "post-use secs=%llu"),
18953055Sdanmcd 			    combs[i].sadb_comb_hard_usetime);
18963055Sdanmcd 
18974064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "\n%s SOFT: "), prefix);
18983055Sdanmcd 		if (combs[i].sadb_comb_soft_allocations)
18994064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "alloc=%u "),
19003055Sdanmcd 			    combs[i].sadb_comb_soft_allocations);
19013055Sdanmcd 		if (combs[i].sadb_comb_soft_bytes)
19024064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN, "bytes=%llu "),
19033055Sdanmcd 			    combs[i].sadb_comb_soft_bytes);
19043055Sdanmcd 		if (combs[i].sadb_comb_soft_addtime)
19054064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
19064064Smarkfen 			    "post-add secs=%llu "),
19073055Sdanmcd 			    combs[i].sadb_comb_soft_addtime);
19083055Sdanmcd 		if (combs[i].sadb_comb_soft_usetime)
19094064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
19104064Smarkfen 			    "post-use secs=%llu"),
19113055Sdanmcd 			    combs[i].sadb_comb_soft_usetime);
19123055Sdanmcd 		(void) putchar('\n');
19133055Sdanmcd 	}
19143055Sdanmcd }
19153055Sdanmcd 
19163055Sdanmcd /*
19173055Sdanmcd  * Print an extended proposal (SADB_X_EXT_EPROP).
19183055Sdanmcd  */
19193055Sdanmcd void
19203055Sdanmcd print_eprop(char *prefix, struct sadb_prop *eprop)
19213055Sdanmcd {
19223055Sdanmcd 	uint64_t *sofar;
19233055Sdanmcd 	struct sadb_x_ecomb *ecomb;
19243055Sdanmcd 	struct sadb_x_algdesc *algdesc;
19253055Sdanmcd 	int i, j;
19263055Sdanmcd 
19274064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
19284064Smarkfen 	    "%sExtended Proposal, replay counter = %u, "), prefix,
19294064Smarkfen 	    eprop->sadb_prop_replay);
19304064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, "number of combinations = %u.\n"),
19313055Sdanmcd 	    eprop->sadb_x_prop_numecombs);
19323055Sdanmcd 
19333055Sdanmcd 	sofar = (uint64_t *)(eprop + 1);
19343055Sdanmcd 	ecomb = (struct sadb_x_ecomb *)sofar;
19353055Sdanmcd 
19363055Sdanmcd 	for (i = 0; i < eprop->sadb_x_prop_numecombs; ) {
19374064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
19384064Smarkfen 		    "%s Extended combination #%u:\n"), prefix, ++i);
19393055Sdanmcd 
19404064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "%s HARD: "), prefix);
19414064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "alloc=%u, "),
19423055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_allocations);
19434064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "bytes=%llu, "),
19443055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_bytes);
19454064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "post-add secs=%llu, "),
19463055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_addtime);
19474064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "post-use secs=%llu\n"),
19483055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_usetime);
19493055Sdanmcd 
19504064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "%s SOFT: "), prefix);
19514064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "alloc=%u, "),
19523055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_allocations);
19534064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "bytes=%llu, "),
19543055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_bytes);
19554064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "post-add secs=%llu, "),
19563055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_addtime);
19574064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "post-use secs=%llu\n"),
19583055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_usetime);
19593055Sdanmcd 
19603055Sdanmcd 		sofar = (uint64_t *)(ecomb + 1);
19613055Sdanmcd 		algdesc = (struct sadb_x_algdesc *)sofar;
19623055Sdanmcd 
19633055Sdanmcd 		for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) {
19644064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
19654064Smarkfen 			    "%s Alg #%u "), prefix, ++j);
19663055Sdanmcd 			switch (algdesc->sadb_x_algdesc_satype) {
19673055Sdanmcd 			case SADB_SATYPE_ESP:
19684064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
19694064Smarkfen 				    "for ESP "));
19703055Sdanmcd 				break;
19713055Sdanmcd 			case SADB_SATYPE_AH:
19724064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN, "for AH "));
19733055Sdanmcd 				break;
19743055Sdanmcd 			default:
19754064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
19764064Smarkfen 				    "for satype=%d "),
19773055Sdanmcd 				    algdesc->sadb_x_algdesc_satype);
19783055Sdanmcd 			}
19793055Sdanmcd 			switch (algdesc->sadb_x_algdesc_algtype) {
19803055Sdanmcd 			case SADB_X_ALGTYPE_CRYPT:
19814064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
19824064Smarkfen 				    "Encryption = "));
19833055Sdanmcd 				(void) dump_ealg(algdesc->sadb_x_algdesc_alg,
19843055Sdanmcd 				    stdout);
19853055Sdanmcd 				break;
19863055Sdanmcd 			case SADB_X_ALGTYPE_AUTH:
19874064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
19884064Smarkfen 				    "Authentication = "));
19893055Sdanmcd 				(void) dump_aalg(algdesc->sadb_x_algdesc_alg,
19903055Sdanmcd 				    stdout);
19913055Sdanmcd 				break;
19923055Sdanmcd 			default:
19934064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
19944064Smarkfen 				    "algtype(%d) = alg(%d)"),
19953055Sdanmcd 				    algdesc->sadb_x_algdesc_algtype,
19963055Sdanmcd 				    algdesc->sadb_x_algdesc_alg);
19973055Sdanmcd 				break;
19983055Sdanmcd 			}
19993055Sdanmcd 
20004064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
20014064Smarkfen 			    "  minbits=%u, maxbits=%u.\n"),
20023055Sdanmcd 			    algdesc->sadb_x_algdesc_minbits,
20033055Sdanmcd 			    algdesc->sadb_x_algdesc_maxbits);
20043055Sdanmcd 
20053055Sdanmcd 			sofar = (uint64_t *)(++algdesc);
20063055Sdanmcd 		}
20073055Sdanmcd 		ecomb = (struct sadb_x_ecomb *)sofar;
20083055Sdanmcd 	}
20093055Sdanmcd }
20103055Sdanmcd 
20113055Sdanmcd /*
20123055Sdanmcd  * Print an SADB_EXT_SUPPORTED extension.
20133055Sdanmcd  */
20143055Sdanmcd void
20153055Sdanmcd print_supp(char *prefix, struct sadb_supported *supp)
20163055Sdanmcd {
20173055Sdanmcd 	struct sadb_alg *algs;
20183055Sdanmcd 	int i, numalgs;
20193055Sdanmcd 
20204064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, "%sSupported "), prefix);
20213055Sdanmcd 	switch (supp->sadb_supported_exttype) {
20223055Sdanmcd 	case SADB_EXT_SUPPORTED_AUTH:
20234064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "authentication"));
20243055Sdanmcd 		break;
20253055Sdanmcd 	case SADB_EXT_SUPPORTED_ENCRYPT:
20264064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN, "encryption"));
20273055Sdanmcd 		break;
20283055Sdanmcd 	}
20294064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN, " algorithms.\n"));
20303055Sdanmcd 
20313055Sdanmcd 	algs = (struct sadb_alg *)(supp + 1);
20323055Sdanmcd 	numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp));
20333055Sdanmcd 	numalgs /= SADB_8TO64(sizeof (*algs));
20343055Sdanmcd 	for (i = 0; i < numalgs; i++) {
20353055Sdanmcd 		(void) printf("%s", prefix);
20363055Sdanmcd 		switch (supp->sadb_supported_exttype) {
20373055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
20383055Sdanmcd 			(void) dump_aalg(algs[i].sadb_alg_id, stdout);
20393055Sdanmcd 			break;
20403055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
20413055Sdanmcd 			(void) dump_ealg(algs[i].sadb_alg_id, stdout);
20423055Sdanmcd 			break;
20433055Sdanmcd 		}
20444064Smarkfen 		(void) printf(dgettext(TEXT_DOMAIN,
20454064Smarkfen 		    " minbits=%u, maxbits=%u, ivlen=%u.\n"),
20463055Sdanmcd 		    algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits,
20473055Sdanmcd 		    algs[i].sadb_alg_ivlen);
20483055Sdanmcd 	}
20493055Sdanmcd }
20503055Sdanmcd 
20513055Sdanmcd /*
20523055Sdanmcd  * Print an SADB_EXT_SPIRANGE extension.
20533055Sdanmcd  */
20543055Sdanmcd void
20553055Sdanmcd print_spirange(char *prefix, struct sadb_spirange *range)
20563055Sdanmcd {
20574064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
20584064Smarkfen 	    "%sSPI Range, min=0x%x, max=0x%x\n"), prefix,
20593055Sdanmcd 	    htonl(range->sadb_spirange_min),
20603055Sdanmcd 	    htonl(range->sadb_spirange_max));
20613055Sdanmcd }
20623055Sdanmcd 
20633055Sdanmcd /*
20643055Sdanmcd  * Print an SADB_X_EXT_KM_COOKIE extension.
20653055Sdanmcd  */
20663055Sdanmcd 
20673055Sdanmcd void
20683055Sdanmcd print_kmc(char *prefix, struct sadb_x_kmc *kmc)
20693055Sdanmcd {
20703055Sdanmcd 	char *cookie_label;
20713055Sdanmcd 
20723055Sdanmcd 	if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) ==
20733055Sdanmcd 	    NULL)
20744064Smarkfen 		cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>");
20753055Sdanmcd 
20764064Smarkfen 	(void) printf(dgettext(TEXT_DOMAIN,
20774064Smarkfen 	    "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix,
20783055Sdanmcd 	    kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie);
20793055Sdanmcd }
20803055Sdanmcd 
20813055Sdanmcd /*
20823055Sdanmcd  * Take a PF_KEY message pointed to buffer and print it.  Useful for DUMP
20833055Sdanmcd  * and GET.
20843055Sdanmcd  */
20853055Sdanmcd void
20863055Sdanmcd print_samsg(uint64_t *buffer, boolean_t want_timestamp, boolean_t vflag)
20873055Sdanmcd {
20883055Sdanmcd 	uint64_t *current;
20893055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
20903055Sdanmcd 	struct sadb_ext *ext;
20913055Sdanmcd 	struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL;
20923055Sdanmcd 	int i;
20933055Sdanmcd 	time_t wallclock;
20943055Sdanmcd 
20953055Sdanmcd 	(void) time(&wallclock);
20963055Sdanmcd 
20973055Sdanmcd 	print_sadb_msg(samsg, want_timestamp ? wallclock : 0, vflag);
20983055Sdanmcd 	current = (uint64_t *)(samsg + 1);
20993055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
21003055Sdanmcd 		int lenbytes;
21013055Sdanmcd 
21023055Sdanmcd 		ext = (struct sadb_ext *)current;
21033055Sdanmcd 		lenbytes = SADB_64TO8(ext->sadb_ext_len);
21043055Sdanmcd 		switch (ext->sadb_ext_type) {
21053055Sdanmcd 		case SADB_EXT_SA:
21064064Smarkfen 			print_sa(dgettext(TEXT_DOMAIN,
21074064Smarkfen 			    "SA: "), (struct sadb_sa *)current);
21083055Sdanmcd 			break;
21093055Sdanmcd 		/*
21103055Sdanmcd 		 * Pluck out lifetimes and print them at the end.  This is
21113055Sdanmcd 		 * to show relative lifetimes.
21123055Sdanmcd 		 */
21133055Sdanmcd 		case SADB_EXT_LIFETIME_CURRENT:
21143055Sdanmcd 			currentlt = (struct sadb_lifetime *)current;
21153055Sdanmcd 			break;
21163055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
21173055Sdanmcd 			hardlt = (struct sadb_lifetime *)current;
21183055Sdanmcd 			break;
21193055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
21203055Sdanmcd 			softlt = (struct sadb_lifetime *)current;
21213055Sdanmcd 			break;
21223055Sdanmcd 
21233055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
21244064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "SRC: "),
21253055Sdanmcd 			    (struct sadb_address *)current);
21263055Sdanmcd 			break;
21273055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
21284064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "INS: "),
21293055Sdanmcd 			    (struct sadb_address *)current);
21303055Sdanmcd 			break;
21313055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
21324064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "DST: "),
21333055Sdanmcd 			    (struct sadb_address *)current);
21343055Sdanmcd 			break;
21353055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
21364064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "IND: "),
21373055Sdanmcd 			    (struct sadb_address *)current);
21383055Sdanmcd 			break;
21393055Sdanmcd 		case SADB_EXT_KEY_AUTH:
21404064Smarkfen 			print_key(dgettext(TEXT_DOMAIN,
21414064Smarkfen 			    "AKY: "), (struct sadb_key *)current);
21423055Sdanmcd 			break;
21433055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
21444064Smarkfen 			print_key(dgettext(TEXT_DOMAIN,
21454064Smarkfen 			    "EKY: "), (struct sadb_key *)current);
21463055Sdanmcd 			break;
21473055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
21484064Smarkfen 			print_ident(dgettext(TEXT_DOMAIN, "SID: "),
21493055Sdanmcd 			    (struct sadb_ident *)current);
21503055Sdanmcd 			break;
21513055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
21524064Smarkfen 			print_ident(dgettext(TEXT_DOMAIN, "DID: "),
21533055Sdanmcd 			    (struct sadb_ident *)current);
21543055Sdanmcd 			break;
21553055Sdanmcd 		case SADB_EXT_SENSITIVITY:
21564064Smarkfen 			print_sens(dgettext(TEXT_DOMAIN, "SNS: "),
21573055Sdanmcd 			    (struct sadb_sens *)current);
21583055Sdanmcd 			break;
21593055Sdanmcd 		case SADB_EXT_PROPOSAL:
21604064Smarkfen 			print_prop(dgettext(TEXT_DOMAIN, "PRP: "),
21613055Sdanmcd 			    (struct sadb_prop *)current);
21623055Sdanmcd 			break;
21633055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
21644064Smarkfen 			print_supp(dgettext(TEXT_DOMAIN, "SUA: "),
21653055Sdanmcd 			    (struct sadb_supported *)current);
21663055Sdanmcd 			break;
21673055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
21684064Smarkfen 			print_supp(dgettext(TEXT_DOMAIN, "SUE: "),
21693055Sdanmcd 			    (struct sadb_supported *)current);
21703055Sdanmcd 			break;
21713055Sdanmcd 		case SADB_EXT_SPIRANGE:
21724064Smarkfen 			print_spirange(dgettext(TEXT_DOMAIN, "SPR: "),
21733055Sdanmcd 			    (struct sadb_spirange *)current);
21743055Sdanmcd 			break;
21753055Sdanmcd 		case SADB_X_EXT_EPROP:
21764064Smarkfen 			print_eprop(dgettext(TEXT_DOMAIN, "EPR: "),
21773055Sdanmcd 			    (struct sadb_prop *)current);
21783055Sdanmcd 			break;
21793055Sdanmcd 		case SADB_X_EXT_KM_COOKIE:
21804064Smarkfen 			print_kmc(dgettext(TEXT_DOMAIN, "KMC: "),
21813055Sdanmcd 			    (struct sadb_x_kmc *)current);
21823055Sdanmcd 			break;
21833055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
21844064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "NRM: "),
21853055Sdanmcd 			    (struct sadb_address *)current);
21863055Sdanmcd 			break;
21873055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
21884064Smarkfen 			print_address(dgettext(TEXT_DOMAIN, "NLC: "),
21893055Sdanmcd 			    (struct sadb_address *)current);
21903055Sdanmcd 			break;
21913055Sdanmcd 		default:
21924064Smarkfen 			(void) printf(dgettext(TEXT_DOMAIN,
21933055Sdanmcd 			    "UNK: Unknown ext. %d, len %d.\n"),
21943055Sdanmcd 			    ext->sadb_ext_type, lenbytes);
21953055Sdanmcd 			for (i = 0; i < ext->sadb_ext_len; i++)
21964064Smarkfen 				(void) printf(dgettext(TEXT_DOMAIN,
21974064Smarkfen 				    "UNK: 0x%llx\n"), ((uint64_t *)ext)[i]);
21983055Sdanmcd 			break;
21993055Sdanmcd 		}
22003055Sdanmcd 		current += (lenbytes == 0) ?
22013055Sdanmcd 		    SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len;
22023055Sdanmcd 	}
22033055Sdanmcd 	/*
22043055Sdanmcd 	 * Print lifetimes NOW.
22053055Sdanmcd 	 */
22063055Sdanmcd 	if (currentlt != NULL || hardlt != NULL || softlt != NULL)
22073055Sdanmcd 		print_lifetimes(wallclock, currentlt, hardlt, softlt, vflag);
22083055Sdanmcd 
22093055Sdanmcd 	if (current - buffer != samsg->sadb_msg_len) {
22104064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "WARNING: insufficient buffer "
22113055Sdanmcd 			"space or corrupt message."));
22123055Sdanmcd 	}
22133055Sdanmcd 
22143055Sdanmcd 	(void) fflush(stdout);	/* Make sure our message is out there. */
22153055Sdanmcd }
22163055Sdanmcd 
22173055Sdanmcd /*
22183055Sdanmcd  * save_XXX functions are used when "saving" the SA tables to either a
22193055Sdanmcd  * file or standard output.  They use the dump_XXX functions where needed,
22203055Sdanmcd  * but mostly they use the rparseXXX functions.
22213055Sdanmcd  */
22223055Sdanmcd 
22233055Sdanmcd /*
22243055Sdanmcd  * Print save information for a lifetime extension.
22253055Sdanmcd  *
22263055Sdanmcd  * NOTE : It saves the lifetime in absolute terms.  For example, if you
22273055Sdanmcd  * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though
22283055Sdanmcd  * there may have been 59 seconds burned off the clock.
22293055Sdanmcd  */
22303055Sdanmcd boolean_t
22313055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile)
22323055Sdanmcd {
22333055Sdanmcd 	char *prefix;
22343055Sdanmcd 
22353055Sdanmcd 	prefix = (lifetime->sadb_lifetime_exttype == SADB_EXT_LIFETIME_SOFT) ?
22363055Sdanmcd 	    "soft" : "hard";
22373055Sdanmcd 
22383055Sdanmcd 	if (putc('\t', ofile) == EOF)
22393055Sdanmcd 		return (B_FALSE);
22403055Sdanmcd 
22413055Sdanmcd 	if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile,
22423055Sdanmcd 	    "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0)
22433055Sdanmcd 		return (B_FALSE);
22443055Sdanmcd 
22453055Sdanmcd 	if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile,
22463055Sdanmcd 	    "%s_bytes %llu ", prefix, lifetime->sadb_lifetime_bytes) < 0)
22473055Sdanmcd 		return (B_FALSE);
22483055Sdanmcd 
22493055Sdanmcd 	if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile,
22503055Sdanmcd 	    "%s_addtime %llu ", prefix, lifetime->sadb_lifetime_addtime) < 0)
22513055Sdanmcd 		return (B_FALSE);
22523055Sdanmcd 
22533055Sdanmcd 	if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile,
22543055Sdanmcd 	    "%s_usetime %llu ", prefix, lifetime->sadb_lifetime_usetime) < 0)
22553055Sdanmcd 		return (B_FALSE);
22563055Sdanmcd 
22573055Sdanmcd 	return (B_TRUE);
22583055Sdanmcd }
22593055Sdanmcd 
22603055Sdanmcd /*
22613055Sdanmcd  * Print save information for an address extension.
22623055Sdanmcd  */
22633055Sdanmcd boolean_t
22643055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile)
22653055Sdanmcd {
22663055Sdanmcd 	char *printable_addr, buf[INET6_ADDRSTRLEN];
22673055Sdanmcd 	const char *prefix, *pprefix;
22683055Sdanmcd 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1);
22693055Sdanmcd 	struct sockaddr_in *sin = (struct sockaddr_in *)sin6;
22703055Sdanmcd 	int af = sin->sin_family;
22713055Sdanmcd 
22723055Sdanmcd 	/*
22733055Sdanmcd 	 * Address-family reality check.
22743055Sdanmcd 	 */
22753055Sdanmcd 	if (af != AF_INET6 && af != AF_INET)
22763055Sdanmcd 		return (B_FALSE);
22773055Sdanmcd 
22783055Sdanmcd 	switch (addr->sadb_address_exttype) {
22793055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
22803055Sdanmcd 		prefix = "src";
22813055Sdanmcd 		pprefix = "sport";
22823055Sdanmcd 		break;
22833055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
22843055Sdanmcd 		prefix = "isrc";
22853055Sdanmcd 		pprefix = "isport";
22863055Sdanmcd 		break;
22873055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
22883055Sdanmcd 		prefix = "dst";
22893055Sdanmcd 		pprefix = "dport";
22903055Sdanmcd 		break;
22913055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
22923055Sdanmcd 		prefix = "idst";
22933055Sdanmcd 		pprefix = "idport";
22943055Sdanmcd 		break;
22953055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
22963055Sdanmcd 		prefix = "nat_loc ";
22973055Sdanmcd 		pprefix = "nat_lport";
22983055Sdanmcd 		break;
22993055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
23003055Sdanmcd 		prefix = "nat_rem ";
23013055Sdanmcd 		pprefix = "nat_rport";
23023055Sdanmcd 		break;
23033055Sdanmcd 	}
23043055Sdanmcd 
23053055Sdanmcd 	if (fprintf(ofile, "    %s ", prefix) < 0)
23063055Sdanmcd 		return (B_FALSE);
23073055Sdanmcd 
23083055Sdanmcd 	/*
23093055Sdanmcd 	 * Do not do address-to-name translation, given that we live in
23103055Sdanmcd 	 * an age of names that explode into many addresses.
23113055Sdanmcd 	 */
23123055Sdanmcd 	printable_addr = (char *)inet_ntop(af,
23133055Sdanmcd 	    (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr,
23143055Sdanmcd 	    buf, sizeof (buf));
23153055Sdanmcd 	if (printable_addr == NULL)
23164064Smarkfen 		printable_addr = "Invalid IP address.";
23173055Sdanmcd 	if (fprintf(ofile, "%s", printable_addr) < 0)
23183055Sdanmcd 		return (B_FALSE);
23193055Sdanmcd 	if (addr->sadb_address_prefixlen != 0 &&
23203055Sdanmcd 	    !((addr->sadb_address_prefixlen == 32 && af == AF_INET) ||
23213055Sdanmcd 		(addr->sadb_address_prefixlen == 128 && af == AF_INET6))) {
23223055Sdanmcd 		if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0)
23233055Sdanmcd 			return (B_FALSE);
23243055Sdanmcd 	}
23253055Sdanmcd 
23263055Sdanmcd 	/*
23273055Sdanmcd 	 * The port is in the same position for struct sockaddr_in and
23283055Sdanmcd 	 * struct sockaddr_in6.  We exploit that property here.
23293055Sdanmcd 	 */
23303055Sdanmcd 	if ((pprefix != NULL) && (sin->sin_port != 0))
23313055Sdanmcd 		(void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port));
23323055Sdanmcd 
23333055Sdanmcd 	return (B_TRUE);
23343055Sdanmcd }
23353055Sdanmcd 
23363055Sdanmcd /*
23373055Sdanmcd  * Print save information for a key extension. Returns whether writing
23383055Sdanmcd  * to the specified output file was successful or not.
23393055Sdanmcd  */
23403055Sdanmcd boolean_t
23413055Sdanmcd save_key(struct sadb_key *key, FILE *ofile)
23423055Sdanmcd {
23433055Sdanmcd 	char *prefix;
23443055Sdanmcd 
23453055Sdanmcd 	if (putc('\t', ofile) == EOF)
23463055Sdanmcd 		return (B_FALSE);
23473055Sdanmcd 
23483055Sdanmcd 	prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr";
23493055Sdanmcd 
23503055Sdanmcd 	if (fprintf(ofile, "%skey ", prefix) < 0)
23513055Sdanmcd 		return (B_FALSE);
23523055Sdanmcd 
23533055Sdanmcd 	if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits, ofile) == -1)
23543055Sdanmcd 		return (B_FALSE);
23553055Sdanmcd 
23563055Sdanmcd 	return (B_TRUE);
23573055Sdanmcd }
23583055Sdanmcd 
23593055Sdanmcd /*
23603055Sdanmcd  * Print save information for an identity extension.
23613055Sdanmcd  */
23623055Sdanmcd boolean_t
23633055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile)
23643055Sdanmcd {
23653055Sdanmcd 	char *prefix;
23663055Sdanmcd 
23673055Sdanmcd 	if (putc('\t', ofile) == EOF)
23683055Sdanmcd 		return (B_FALSE);
23693055Sdanmcd 
23703055Sdanmcd 	prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" :
23713055Sdanmcd 	    "dst";
23723055Sdanmcd 
23733055Sdanmcd 	if (fprintf(ofile, "%sidtype %s ", prefix,
23743055Sdanmcd 	    rparseidtype(ident->sadb_ident_type)) < 0)
23753055Sdanmcd 		return (B_FALSE);
23763055Sdanmcd 
23773055Sdanmcd 	if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN ||
23783055Sdanmcd 	    ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) {
23794064Smarkfen 		if (fprintf(ofile, dgettext(TEXT_DOMAIN,
23804064Smarkfen 		    "<can-not-print>")) < 0)
23813055Sdanmcd 			return (B_FALSE);
23823055Sdanmcd 	} else {
23833055Sdanmcd 		if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0)
23843055Sdanmcd 			return (B_FALSE);
23853055Sdanmcd 	}
23863055Sdanmcd 
23873055Sdanmcd 	return (B_TRUE);
23883055Sdanmcd }
23893055Sdanmcd 
23903055Sdanmcd /*
23913055Sdanmcd  * "Save" a security association to an output file.
23923055Sdanmcd  *
23934064Smarkfen  * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff.
23943055Sdanmcd  * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to
23953055Sdanmcd  * change them here as well.
23963055Sdanmcd  */
23973055Sdanmcd void
23983055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile)
23993055Sdanmcd {
24004064Smarkfen 	int terrno;
24013055Sdanmcd 	int seen_proto = 0;
24023055Sdanmcd 	uint64_t *current;
24033055Sdanmcd 	struct sadb_address *addr;
24043055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
24053055Sdanmcd 	struct sadb_ext *ext;
24063055Sdanmcd 
24074064Smarkfen #define	tidyup() \
24084064Smarkfen 	terrno = errno; (void) fclose(ofile); errno = terrno; \
24094064Smarkfen 	interactive = B_FALSE
24104064Smarkfen 
24114064Smarkfen #define	savenl() if (fputs(" \\\n", ofile) == EOF) \
24124064Smarkfen 	{ bail(dgettext(TEXT_DOMAIN, "savenl")); }
24133055Sdanmcd 
24143055Sdanmcd 	if (fputs("# begin assoc\n", ofile) == EOF)
24154064Smarkfen 		bail(dgettext(TEXT_DOMAIN,
24164064Smarkfen 		    "save_assoc: Opening comment of SA"));
24173055Sdanmcd 	if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0)
24184064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA"));
24193055Sdanmcd 	savenl();
24203055Sdanmcd 
24213055Sdanmcd 	current = (uint64_t *)(samsg + 1);
24223055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
24233055Sdanmcd 		struct sadb_sa *assoc;
24243055Sdanmcd 
24253055Sdanmcd 		ext = (struct sadb_ext *)current;
24263055Sdanmcd 		switch (ext->sadb_ext_type) {
24273055Sdanmcd 		case SADB_EXT_SA:
24283055Sdanmcd 			assoc = (struct sadb_sa *)ext;
24293055Sdanmcd 			if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
24303055Sdanmcd 				if (fprintf(ofile, "# WARNING: SA was dying "
24313055Sdanmcd 				    "or dead.\n") < 0) {
24324064Smarkfen 					tidyup();
24334064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24344064Smarkfen 					    "save_assoc: fprintf not mature"));
24353055Sdanmcd 				}
24363055Sdanmcd 			}
24373055Sdanmcd 			if (fprintf(ofile, "    spi 0x%x ",
24384064Smarkfen 			    ntohl(assoc->sadb_sa_spi)) < 0) {
24394064Smarkfen 				tidyup();
24404064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
24414064Smarkfen 				    "save_assoc: fprintf spi"));
24424064Smarkfen 			}
24433055Sdanmcd 			if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
24443055Sdanmcd 				if (fprintf(ofile, "encr_alg %s ",
24453055Sdanmcd 				    rparsealg(assoc->sadb_sa_encrypt,
24464064Smarkfen 					IPSEC_PROTO_ESP)) < 0) {
24474064Smarkfen 					tidyup();
24484064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24494064Smarkfen 					    "save_assoc: fprintf encrypt"));
24504064Smarkfen 				}
24513055Sdanmcd 			}
24523055Sdanmcd 			if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
24533055Sdanmcd 				if (fprintf(ofile, "auth_alg %s ",
24543055Sdanmcd 				    rparsealg(assoc->sadb_sa_auth,
24554064Smarkfen 					IPSEC_PROTO_AH)) < 0) {
24564064Smarkfen 					tidyup();
24574064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24584064Smarkfen 					    "save_assoc: fprintf auth"));
24594064Smarkfen 				}
24603055Sdanmcd 			}
24613055Sdanmcd 			if (fprintf(ofile, "replay %d ",
24624064Smarkfen 			    assoc->sadb_sa_replay) < 0) {
24634064Smarkfen 				tidyup();
24644064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
24654064Smarkfen 				    "save_assoc: fprintf replay"));
24664064Smarkfen 			}
24673055Sdanmcd 			if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC |
24683055Sdanmcd 			    SADB_X_SAFLAGS_NATT_REM)) {
24694064Smarkfen 				if (fprintf(ofile, "encap udp") < 0) {
24704064Smarkfen 					tidyup();
24714064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24724064Smarkfen 					    "save_assoc: fprintf encap"));
24734064Smarkfen 				}
24743055Sdanmcd 			}
24753055Sdanmcd 			savenl();
24763055Sdanmcd 			break;
24773055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
24783055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
24794064Smarkfen 			if (!save_lifetime((struct sadb_lifetime *)ext,
24804064Smarkfen 			    ofile)) {
24814064Smarkfen 				tidyup();
24824064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_lifetime"));
24834064Smarkfen 			}
24843055Sdanmcd 			savenl();
24853055Sdanmcd 			break;
24863055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
24873055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
24883055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
24893055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
24903055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
24913055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
24923055Sdanmcd 			addr = (struct sadb_address *)ext;
24933055Sdanmcd 			if (!seen_proto && addr->sadb_address_proto) {
24943055Sdanmcd 				(void) fprintf(ofile, "    proto %d",
24953055Sdanmcd 				    addr->sadb_address_proto);
24963055Sdanmcd 				savenl();
24973055Sdanmcd 				seen_proto = 1;
24983055Sdanmcd 			}
24994064Smarkfen 			if (!save_address(addr, ofile)) {
25004064Smarkfen 				tidyup();
25014064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25024064Smarkfen 			}
25033055Sdanmcd 			savenl();
25043055Sdanmcd 			break;
25053055Sdanmcd 		case SADB_EXT_KEY_AUTH:
25063055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
25074064Smarkfen 			if (!save_key((struct sadb_key *)ext, ofile)) {
25084064Smarkfen 				tidyup();
25094064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25104064Smarkfen 			}
25113055Sdanmcd 			savenl();
25123055Sdanmcd 			break;
25133055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
25143055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
25154064Smarkfen 			if (!save_ident((struct sadb_ident *)ext, ofile)) {
25164064Smarkfen 				tidyup();
25174064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25184064Smarkfen 			}
25193055Sdanmcd 			savenl();
25203055Sdanmcd 			break;
25213055Sdanmcd 		case SADB_EXT_SENSITIVITY:
25223055Sdanmcd 		default:
25233055Sdanmcd 			/* Skip over irrelevant extensions. */
25243055Sdanmcd 			break;
25253055Sdanmcd 		}
25263055Sdanmcd 		current += ext->sadb_ext_len;
25273055Sdanmcd 	}
25283055Sdanmcd 
25294064Smarkfen 	if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) {
25304064Smarkfen 		tidyup();
25314064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs"));
25324064Smarkfen 	}
25333055Sdanmcd }
25343055Sdanmcd 
25353055Sdanmcd /*
25363055Sdanmcd  * Open the output file for the "save" command.
25373055Sdanmcd  */
25383055Sdanmcd FILE *
25393055Sdanmcd opensavefile(char *filename)
25403055Sdanmcd {
25413055Sdanmcd 	int fd;
25423055Sdanmcd 	FILE *retval;
25433055Sdanmcd 	struct stat buf;
25443055Sdanmcd 
25453055Sdanmcd 	/*
25463055Sdanmcd 	 * If the user specifies "-" or doesn't give a filename, then
25473055Sdanmcd 	 * dump to stdout.  Make sure to document the dangers of files
25483055Sdanmcd 	 * that are NFS, directing your output to strange places, etc.
25493055Sdanmcd 	 */
25503055Sdanmcd 	if (filename == NULL || strcmp("-", filename) == 0)
25513055Sdanmcd 		return (stdout);
25523055Sdanmcd 
25533055Sdanmcd 	/*
25543055Sdanmcd 	 * open the file with the create bits set.  Since I check for
25553055Sdanmcd 	 * real UID == root in main(), I won't worry about the ownership
25563055Sdanmcd 	 * problem.
25573055Sdanmcd 	 */
25583055Sdanmcd 	fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR);
25593055Sdanmcd 	if (fd == -1) {
25603055Sdanmcd 		if (errno != EEXIST)
25614064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
25624064Smarkfen 			    "open error"),
25633055Sdanmcd 			    strerror(errno));
25643055Sdanmcd 		fd = open(filename, O_WRONLY | O_TRUNC, 0);
25653055Sdanmcd 		if (fd == -1)
25664064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
25674064Smarkfen 			    "open error"), strerror(errno));
25683055Sdanmcd 		if (fstat(fd, &buf) == -1) {
25693055Sdanmcd 			(void) close(fd);
25703055Sdanmcd 			bail_msg("%s fstat: %s", filename, strerror(errno));
25713055Sdanmcd 		}
25723055Sdanmcd 		if (S_ISREG(buf.st_mode) &&
25733055Sdanmcd 		    ((buf.st_mode & S_IAMB) != S_IRUSR)) {
25744064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
25754064Smarkfen 			    "WARNING: Save file already exists with "
25764064Smarkfen 			    "permission %o."), buf.st_mode & S_IAMB);
25774064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
25784064Smarkfen 			    "Normal users may be able to read IPsec "
25794064Smarkfen 			    "keying material."));
25803055Sdanmcd 		}
25813055Sdanmcd 	}
25823055Sdanmcd 
25833055Sdanmcd 	/* Okay, we have an FD.  Assign it to a stdio FILE pointer. */
25843055Sdanmcd 	retval = fdopen(fd, "w");
25853055Sdanmcd 	if (retval == NULL) {
25863055Sdanmcd 		(void) close(fd);
25874064Smarkfen 		bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
25884064Smarkfen 		    "fdopen error"), strerror(errno));
25893055Sdanmcd 	}
25903055Sdanmcd 	return (retval);
25913055Sdanmcd }
25923055Sdanmcd 
25933055Sdanmcd const char *
25943055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size)
25953055Sdanmcd {
25963055Sdanmcd 	boolean_t isv4;
25973055Sdanmcd 	struct in6_addr *inaddr6 = (struct in6_addr *)addr;
25983055Sdanmcd 	struct in_addr inaddr;
25993055Sdanmcd 
26003055Sdanmcd 	if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) {
26013055Sdanmcd 		IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr);
26023055Sdanmcd 	}
26033055Sdanmcd 
26043055Sdanmcd 	return (inet_ntop(isv4 ? AF_INET : AF_INET6,
26053055Sdanmcd 	    isv4 ? (void *)&inaddr : inaddr6, cp, size));
26063055Sdanmcd }
26073055Sdanmcd 
26083055Sdanmcd char numprint[NBUF_SIZE];
26093055Sdanmcd 
26103055Sdanmcd /*
26113055Sdanmcd  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
26123055Sdanmcd  */
26133055Sdanmcd static struct typetable {
26143055Sdanmcd 	char *type;
26153055Sdanmcd 	int token;
26163055Sdanmcd } type_table[] = {
26173055Sdanmcd 	{"all", SADB_SATYPE_UNSPEC},
26183055Sdanmcd 	{"ah",  SADB_SATYPE_AH},
26193055Sdanmcd 	{"esp", SADB_SATYPE_ESP},
26203055Sdanmcd 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
26213055Sdanmcd 	{NULL, 0}	/* Token value is irrelevant for this entry. */
26223055Sdanmcd };
26233055Sdanmcd 
26243055Sdanmcd char *
26253055Sdanmcd rparsesatype(int type)
26263055Sdanmcd {
26273055Sdanmcd 	struct typetable *tt = type_table;
26283055Sdanmcd 
26293055Sdanmcd 	while (tt->type != NULL && type != tt->token)
26303055Sdanmcd 		tt++;
26313055Sdanmcd 
26323055Sdanmcd 	if (tt->type == NULL) {
26333055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", type);
26343055Sdanmcd 	} else {
26353055Sdanmcd 		return (tt->type);
26363055Sdanmcd 	}
26373055Sdanmcd 
26383055Sdanmcd 	return (numprint);
26393055Sdanmcd }
26403055Sdanmcd 
26413055Sdanmcd 
26423055Sdanmcd /*
26433055Sdanmcd  * Return a string containing the name of the specified numerical algorithm
26443055Sdanmcd  * identifier.
26453055Sdanmcd  */
26463055Sdanmcd char *
26473055Sdanmcd rparsealg(uint8_t alg, int proto_num)
26483055Sdanmcd {
26493055Sdanmcd 	static struct ipsecalgent *holder = NULL; /* we're single-threaded */
26503055Sdanmcd 
26513055Sdanmcd 	if (holder != NULL)
26523055Sdanmcd 		freeipsecalgent(holder);
26533055Sdanmcd 
26543055Sdanmcd 	holder = getipsecalgbynum(alg, proto_num, NULL);
26553055Sdanmcd 	if (holder == NULL) {
26563055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", alg);
26573055Sdanmcd 		return (numprint);
26583055Sdanmcd 	}
26593055Sdanmcd 
26603055Sdanmcd 	return (*(holder->a_names));
26613055Sdanmcd }
26623055Sdanmcd 
26633055Sdanmcd /*
26643055Sdanmcd  * Parse and reverse parse out a source/destination ID type.
26653055Sdanmcd  */
26663055Sdanmcd static struct idtypes {
26673055Sdanmcd 	char *idtype;
26683055Sdanmcd 	uint8_t retval;
26693055Sdanmcd } idtypes[] = {
26703055Sdanmcd 	{"prefix",	SADB_IDENTTYPE_PREFIX},
26713055Sdanmcd 	{"fqdn",	SADB_IDENTTYPE_FQDN},
26723055Sdanmcd 	{"domain",	SADB_IDENTTYPE_FQDN},
26733055Sdanmcd 	{"domainname",	SADB_IDENTTYPE_FQDN},
26743055Sdanmcd 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
26753055Sdanmcd 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
26763055Sdanmcd 	{"der_dn",	SADB_X_IDENTTYPE_DN},
26773055Sdanmcd 	{"der_gn",	SADB_X_IDENTTYPE_GN},
26783055Sdanmcd 	{NULL,		0}
26793055Sdanmcd };
26803055Sdanmcd 
26813055Sdanmcd char *
26823055Sdanmcd rparseidtype(uint16_t type)
26833055Sdanmcd {
26843055Sdanmcd 	struct idtypes *idp;
26853055Sdanmcd 
26863055Sdanmcd 	for (idp = idtypes; idp->idtype != NULL; idp++) {
26873055Sdanmcd 		if (type == idp->retval)
26883055Sdanmcd 			return (idp->idtype);
26893055Sdanmcd 	}
26903055Sdanmcd 
26913055Sdanmcd 	(void) snprintf(numprint, NBUF_SIZE, "%d", type);
26923055Sdanmcd 	return (numprint);
26933055Sdanmcd }
2694*4235Smarkfen 
2695*4235Smarkfen /*
2696*4235Smarkfen  * This is a general purpose exit function, calling functions can specify an
2697*4235Smarkfen  * error type. If the command calling this function was started by smf(5) the
2698*4235Smarkfen  * error type could be used as a hint to the restarter. In the future this
2699*4235Smarkfen  * function could be used to do something more intelligent with a process that
2700*4235Smarkfen  * encounters an error.
2701*4235Smarkfen  *
2702*4235Smarkfen  * The function will handle an optional variable args error message, this
2703*4235Smarkfen  * will be written to the error stream, typically a log file or stderr.
2704*4235Smarkfen  */
2705*4235Smarkfen void
2706*4235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...)
2707*4235Smarkfen {
2708*4235Smarkfen 	int exit_status;
2709*4235Smarkfen 	va_list args;
2710*4235Smarkfen 
2711*4235Smarkfen 	if (fp == NULL)
2712*4235Smarkfen 		fp = stderr;
2713*4235Smarkfen 	if (fmt != NULL) {
2714*4235Smarkfen 		va_start(args, fmt);
2715*4235Smarkfen 		vwarnxfp(fp, fmt, args);
2716*4235Smarkfen 		va_end(args);
2717*4235Smarkfen 	}
2718*4235Smarkfen 
2719*4235Smarkfen 	if (fmri == NULL) {
2720*4235Smarkfen 		/* Command being run directly from a shell. */
2721*4235Smarkfen 		switch (type) {
2722*4235Smarkfen 		case SERVICE_EXIT_OK:
2723*4235Smarkfen 			exit_status = 0;
2724*4235Smarkfen 			break;
2725*4235Smarkfen 		case SERVICE_DEGRADE:
2726*4235Smarkfen 			return;
2727*4235Smarkfen 			break;
2728*4235Smarkfen 		case SERVICE_BADPERM:
2729*4235Smarkfen 		case SERVICE_BADCONF:
2730*4235Smarkfen 		case SERVICE_MAINTAIN:
2731*4235Smarkfen 		case SERVICE_DISABLE:
2732*4235Smarkfen 		case SERVICE_FATAL:
2733*4235Smarkfen 		case SERVICE_RESTART:
2734*4235Smarkfen 			warnxfp(fp, "Fatal error - exiting.");
2735*4235Smarkfen 			exit_status = 1;
2736*4235Smarkfen 			break;
2737*4235Smarkfen 		}
2738*4235Smarkfen 	} else {
2739*4235Smarkfen 		/* Command being run as a smf(5) method. */
2740*4235Smarkfen 		switch (type) {
2741*4235Smarkfen 		case SERVICE_EXIT_OK:
2742*4235Smarkfen 			exit_status = SMF_EXIT_OK;
2743*4235Smarkfen 			break;
2744*4235Smarkfen 		case SERVICE_DEGRADE:
2745*4235Smarkfen 			return;
2746*4235Smarkfen 			break;
2747*4235Smarkfen 		case SERVICE_BADPERM:
2748*4235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
2749*4235Smarkfen 			    "Permission error with %s."), fmri);
2750*4235Smarkfen 			exit_status = SMF_EXIT_ERR_PERM;
2751*4235Smarkfen 			break;
2752*4235Smarkfen 		case SERVICE_BADCONF:
2753*4235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
2754*4235Smarkfen 			    "Bad configuration of service %s."), fmri);
2755*4235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
2756*4235Smarkfen 			break;
2757*4235Smarkfen 		case SERVICE_MAINTAIN:
2758*4235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
2759*4235Smarkfen 			    "Service %s needs maintenance."), fmri);
2760*4235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
2761*4235Smarkfen 			break;
2762*4235Smarkfen 		case SERVICE_DISABLE:
2763*4235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
2764*4235Smarkfen 			break;
2765*4235Smarkfen 		case SERVICE_FATAL:
2766*4235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
2767*4235Smarkfen 			    "Service %s fatal error."), fmri);
2768*4235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
2769*4235Smarkfen 			break;
2770*4235Smarkfen 		case SERVICE_RESTART:
2771*4235Smarkfen 			exit_status = 1;
2772*4235Smarkfen 			break;
2773*4235Smarkfen 		}
2774*4235Smarkfen 	}
2775*4235Smarkfen 	(void) fflush(fp);
2776*4235Smarkfen 	(void) fclose(fp);
2777*4235Smarkfen 	exit(exit_status);
2778*4235Smarkfen }
2779