xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ipseckey.c (revision 4867:dd3be85f40a2)
14235Smarkfen /*
24235Smarkfen  * CDDL HEADER START
34235Smarkfen  *
44235Smarkfen  * The contents of this file are subject to the terms of the
54235Smarkfen  * Common Development and Distribution License (the "License").
64235Smarkfen  * You may not use this file except in compliance with the License.
74235Smarkfen  *
84235Smarkfen  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94235Smarkfen  * or http://www.opensolaris.org/os/licensing.
104235Smarkfen  * See the License for the specific language governing permissions
114235Smarkfen  * and limitations under the License.
124235Smarkfen  *
134235Smarkfen  * When distributing Covered Code, include this CDDL HEADER in each
144235Smarkfen  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154235Smarkfen  * If applicable, add the following below this CDDL HEADER, with the
164235Smarkfen  * fields enclosed by brackets "[]" replaced with your own identifying
174235Smarkfen  * information: Portions Copyright [yyyy] [name of copyright owner]
184235Smarkfen  *
194235Smarkfen  * CDDL HEADER END
204235Smarkfen  */
214235Smarkfen /*
224235Smarkfen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
234235Smarkfen  * Use is subject to license terms.
244235Smarkfen  */
254235Smarkfen 
264235Smarkfen /*
274235Smarkfen  * NOTE:I'm trying to use "struct sadb_foo" instead of "sadb_foo_t"
284235Smarkfen  *	as a maximal PF_KEY portability test.
294235Smarkfen  *
304235Smarkfen  *	Also, this is a deliberately single-threaded app, also for portability
314235Smarkfen  *	to systems without POSIX threads.
324235Smarkfen  */
334235Smarkfen 
344235Smarkfen #pragma ident	"%Z%%M%	%I%	%E% SMI"
354235Smarkfen 
364235Smarkfen 
374235Smarkfen #include <sys/types.h>
384235Smarkfen #include <sys/stat.h>
394235Smarkfen #include <sys/socket.h>
404235Smarkfen #include <sys/sysmacros.h>
414235Smarkfen #include <sys/fcntl.h>
424235Smarkfen #include <net/pfkeyv2.h>
434235Smarkfen #include <arpa/inet.h>
444235Smarkfen #include <netinet/in.h>
454235Smarkfen #include <sys/uio.h>
464235Smarkfen 
474235Smarkfen #include <syslog.h>
484235Smarkfen #include <signal.h>
494235Smarkfen #include <unistd.h>
504235Smarkfen #include <limits.h>
514235Smarkfen #include <stdlib.h>
524235Smarkfen #include <stdio.h>
534235Smarkfen #include <stdarg.h>
544235Smarkfen #include <netdb.h>
554235Smarkfen #include <pwd.h>
564235Smarkfen #include <errno.h>
574235Smarkfen #include <libintl.h>
584235Smarkfen #include <locale.h>
594235Smarkfen #include <fcntl.h>
604235Smarkfen #include <strings.h>
614235Smarkfen #include <ctype.h>
624235Smarkfen 
634235Smarkfen #include <ipsec_util.h>
644235Smarkfen 
654235Smarkfen static int keysock;
664235Smarkfen static uint32_t seq;
674235Smarkfen static pid_t mypid;
684235Smarkfen static boolean_t vflag = B_FALSE;	/* Verbose? */
694235Smarkfen static boolean_t cflag = B_FALSE;	/* Check Only */
704235Smarkfen 
714235Smarkfen char *my_fmri = NULL;
724235Smarkfen FILE *debugfile = stdout;
734235Smarkfen 
744235Smarkfen #define	MAX_GET_SIZE	1024
754235Smarkfen /*
764573Spwernau  * WARN() and ERROR() do the same thing really, with ERROR() the function
774235Smarkfen  * that prints the error buffer needs to be called at the end of a code block
784235Smarkfen  * This will print out all accumulated errors before bailing. The WARN()
794235Smarkfen  * macro calls handle_errors() in such a way that it prints the message
804235Smarkfen  * then continues.
814235Smarkfen  * If the FATAL() macro used call handle_errors() immediately.
824235Smarkfen  */
834235Smarkfen #define	ERROR(x, y, z)  x = record_error(x, y, z)
844235Smarkfen #define	ERROR1(w, x, y, z)  w = record_error(w, x, y, z)
854235Smarkfen #define	ERROR2(v, w, x, y, z)  v = record_error(v, w, x, y, z)
864235Smarkfen #define	WARN(x, y, z) ERROR(x, y, z);\
874235Smarkfen 	handle_errors(x, NULL, B_FALSE, B_FALSE); x = NULL
884235Smarkfen #define	WARN1(w, x, y, z) ERROR1(w, x, y, z);\
894235Smarkfen 	handle_errors(w, NULL, B_FALSE, B_FALSE); w = NULL
904235Smarkfen #define	WARN2(v, w, x, y, z) ERROR2(v, w, x, y, z);\
914235Smarkfen 	handle_errors(v, NULL, B_FALSE, B_FALSE); v = NULL
924235Smarkfen #define	FATAL(x, y, z) ERROR(x, y, z);\
934235Smarkfen 	handle_errors(x, y, B_TRUE, B_TRUE)
944235Smarkfen #define	FATAL1(w, x, y, z) ERROR1(w, x, y, z);\
954235Smarkfen 	handle_errors(w, x, B_TRUE, B_TRUE)
964235Smarkfen 
974235Smarkfen /* Defined as a uint64_t array for alignment purposes. */
984235Smarkfen static uint64_t get_buffer[MAX_GET_SIZE];
994235Smarkfen 
1004235Smarkfen /*
1014235Smarkfen  * Create/Grow a buffer large enough to hold error messages. If *ebuf
1024235Smarkfen  * is not NULL then it will contain a copy of the command line that
1034235Smarkfen  * triggered the error/warning, copy this into a new buffer or
1044235Smarkfen  * append new messages to the existing buffer.
1054235Smarkfen  */
1064235Smarkfen /*PRINTFLIKE1*/
1074235Smarkfen char *
1084235Smarkfen record_error(char *ep, char *ebuf, char *fmt, ...)
1094235Smarkfen {
1104235Smarkfen 	char *err_ptr;
1114235Smarkfen 	char tmp_buff[1024];
1124235Smarkfen 	va_list ap;
1134235Smarkfen 	int length = 0;
1144235Smarkfen 	err_ptr = ep;
1154235Smarkfen 
1164235Smarkfen 	va_start(ap, fmt);
1174235Smarkfen 	(void) vsnprintf(tmp_buff, sizeof (tmp_buff), fmt, ap);
1184235Smarkfen 	va_end(ap);
1194235Smarkfen 
1204235Smarkfen 	if (ep == NULL) {
1214235Smarkfen 		/*
1224235Smarkfen 		 * This is the first error to record, get a
1234235Smarkfen 		 * new buffer, copy in the command line that
1244235Smarkfen 		 * triggered this error/warning.
1254235Smarkfen 		 */
1264235Smarkfen 		if (ebuf != NULL) {
1274235Smarkfen 			length = strlen(ebuf);
1284235Smarkfen 			err_ptr = calloc(length, sizeof (char));
1294235Smarkfen 			if (err_ptr == NULL)
1304235Smarkfen 				Bail("calloc() failed");
1314235Smarkfen 			(void) strlcpy(err_ptr, ebuf, length);
1324235Smarkfen 		}
1334235Smarkfen 	} else {
1344235Smarkfen 		length = strlen(ep);
1354235Smarkfen 	}
1364235Smarkfen 	length += strlen(tmp_buff);
1374235Smarkfen 	/* There is a new line character */
1384235Smarkfen 	length++;
1394235Smarkfen 	err_ptr = realloc(err_ptr, length);
1404235Smarkfen 	if (err_ptr == NULL)
1414235Smarkfen 		Bail("realloc() failure");
1424235Smarkfen 	(void) strlcat(err_ptr, tmp_buff, length);
1434235Smarkfen 	return (err_ptr);
1444235Smarkfen }
1454235Smarkfen 
1464235Smarkfen /*
1474235Smarkfen  * Print usage message.
1484235Smarkfen  */
1494235Smarkfen static void
1504235Smarkfen usage(void)
1514235Smarkfen {
1524235Smarkfen 	if (!interactive) {
1534235Smarkfen 		(void) fprintf(stderr, gettext("Usage:\t"
1544235Smarkfen 		    "ipseckey [ -nvp ] | cmd [sa_type] [extfield value]*\n"));
1554235Smarkfen 		(void) fprintf(stderr,
1564235Smarkfen 		    gettext("\tipseckey [ -nvp ] -f infile\n"));
1574235Smarkfen 		(void) fprintf(stderr,
1584235Smarkfen 		    gettext("\tipseckey [ -nvp ] -s outfile\n"));
1594235Smarkfen 	}
1604235Smarkfen 	EXIT_FATAL(NULL);
1614235Smarkfen }
1624235Smarkfen 
1634235Smarkfen 
1644235Smarkfen /*
1654235Smarkfen  * Print out any errors, tidy up as required.
1664235Smarkfen  * error pointer ep will be free()'d
1674235Smarkfen  */
1684235Smarkfen void
1694235Smarkfen handle_errors(char *ep, char *ebuf, boolean_t fatal, boolean_t done)
1704235Smarkfen {
1714235Smarkfen 	if (ep != NULL) {
1724235Smarkfen 		if (my_fmri == NULL) {
1734235Smarkfen 			/*
1744235Smarkfen 			 * For now suppress the errors when run from smf(5)
1754235Smarkfen 			 * because potentially sensitive information could
1764235Smarkfen 			 * end up in a publicly readable logfile.
1774235Smarkfen 			 */
1784235Smarkfen 			(void) fprintf(stdout, "%s\n", ep);
1794235Smarkfen 			(void) fflush(stdout);
1804235Smarkfen 		}
1814235Smarkfen 		free(ep);
1824235Smarkfen 		if (fatal) {
1834235Smarkfen 			if (ebuf != NULL) {
1844235Smarkfen 				free(ebuf);
1854235Smarkfen 			}
1864235Smarkfen 			/* reset command buffer */
1874235Smarkfen 			if (interactive)
1884235Smarkfen 				longjmp(env, 1);
1894235Smarkfen 		} else {
1904235Smarkfen 			return;
1914235Smarkfen 		}
1924235Smarkfen 	} else {
1934235Smarkfen 		/*
1944235Smarkfen 		 * No errors, if this is the last time that this function
1954235Smarkfen 		 * is called, free(ebuf) and reset command buffer.
1964235Smarkfen 		 */
1974235Smarkfen 		if (done) {
1984235Smarkfen 			if (ebuf != NULL) {
1994235Smarkfen 				free(ebuf);
2004235Smarkfen 			}
2014235Smarkfen 			/* reset command buffer */
2024235Smarkfen 			if (interactive)
2034235Smarkfen 				longjmp(env, 1);
2044235Smarkfen 		}
2054235Smarkfen 		return;
2064235Smarkfen 	}
2074235Smarkfen }
2084235Smarkfen 
2094235Smarkfen /*
2104235Smarkfen  * Initialize a PF_KEY base message.
2114235Smarkfen  */
2124235Smarkfen static void
2134235Smarkfen msg_init(struct sadb_msg *msg, uint8_t type, uint8_t satype)
2144235Smarkfen {
2154235Smarkfen 	msg->sadb_msg_version = PF_KEY_V2;
2164235Smarkfen 	msg->sadb_msg_type = type;
2174235Smarkfen 	msg->sadb_msg_errno = 0;
2184235Smarkfen 	msg->sadb_msg_satype = satype;
2194235Smarkfen 	/* For starters... */
2204235Smarkfen 	msg->sadb_msg_len = SADB_8TO64(sizeof (*msg));
2214235Smarkfen 	msg->sadb_msg_reserved = 0;
2224235Smarkfen 	msg->sadb_msg_seq = ++seq;
2234235Smarkfen 	msg->sadb_msg_pid = mypid;
2244235Smarkfen }
2254235Smarkfen 
2264235Smarkfen /*
2274235Smarkfen  * parseXXX and rparseXXX commands parse input and convert them to PF_KEY
2284235Smarkfen  * field values, or do the reverse for the purposes of saving the SA tables.
2294235Smarkfen  * (See the save_XXX functions.)
2304235Smarkfen  */
2314235Smarkfen 
2324235Smarkfen #define	CMD_NONE	0
2334235Smarkfen #define	CMD_UPDATE	2
2344235Smarkfen #define	CMD_ADD		3
2354235Smarkfen #define	CMD_DELETE	4
2364235Smarkfen #define	CMD_GET		5
2374235Smarkfen #define	CMD_FLUSH	9
2384235Smarkfen #define	CMD_DUMP	10
2394235Smarkfen #define	CMD_MONITOR	11
2404235Smarkfen #define	CMD_PMONITOR	12
2414235Smarkfen #define	CMD_QUIT	13
2424235Smarkfen #define	CMD_SAVE	14
2434235Smarkfen #define	CMD_HELP	15
2444235Smarkfen 
2454235Smarkfen /*
2464235Smarkfen  * Parse the command.
2474235Smarkfen  */
2484235Smarkfen static int
2494235Smarkfen parsecmd(char *cmdstr)
2504235Smarkfen {
2514235Smarkfen 	static struct cmdtable {
2524235Smarkfen 		char *cmd;
2534235Smarkfen 		int token;
2544235Smarkfen 	} table[] = {
2554235Smarkfen 		/*
2564235Smarkfen 		 * Q: Do we want to do GETSPI?
2574235Smarkfen 		 * A: No, it's for automated key mgmt. only.  Either that,
2584235Smarkfen 		 *    or it isn't relevant until we support non IPsec SA types.
2594235Smarkfen 		 */
2604235Smarkfen 		{"update",		CMD_UPDATE},
2614235Smarkfen 		{"add",			CMD_ADD},
2624235Smarkfen 		{"delete", 		CMD_DELETE},
2634235Smarkfen 		{"get", 		CMD_GET},
2644235Smarkfen 		/*
2654235Smarkfen 		 * Q: And ACQUIRE and REGISTER and EXPIRE?
2664235Smarkfen 		 * A: not until we support non IPsec SA types.
2674235Smarkfen 		 */
2684235Smarkfen 		{"flush",		CMD_FLUSH},
2694235Smarkfen 		{"dump",		CMD_DUMP},
2704235Smarkfen 		{"monitor",		CMD_MONITOR},
2714235Smarkfen 		{"passive_monitor",	CMD_PMONITOR},
2724235Smarkfen 		{"pmonitor",		CMD_PMONITOR},
2734235Smarkfen 		{"quit",		CMD_QUIT},
2744235Smarkfen 		{"exit",		CMD_QUIT},
2754235Smarkfen 		{"save",		CMD_SAVE},
2764235Smarkfen 		{"help",		CMD_HELP},
2774235Smarkfen 		{"?",			CMD_HELP},
2784235Smarkfen 		{NULL,			CMD_NONE}
2794235Smarkfen 	};
2804235Smarkfen 	struct cmdtable *ct = table;
2814235Smarkfen 
2824235Smarkfen 	while (ct->cmd != NULL && strcmp(ct->cmd, cmdstr) != 0)
2834235Smarkfen 		ct++;
2844235Smarkfen 	return (ct->token);
2854235Smarkfen }
2864235Smarkfen 
2874235Smarkfen /*
2884235Smarkfen  * Convert a number from a command line.  I picked "u_longlong_t" for the
2894235Smarkfen  * number because we need the largest number available.  Also, the strto<num>
2904235Smarkfen  * calls don't deal in units of uintNN_t.
2914235Smarkfen  */
2924235Smarkfen static u_longlong_t
2934235Smarkfen parsenum(char *num, boolean_t bail, char *ebuf)
2944235Smarkfen {
2954235Smarkfen 	u_longlong_t rc = 0;
2964235Smarkfen 	char *end = NULL;
2974235Smarkfen 	char *ep = NULL;
2984235Smarkfen 
2994235Smarkfen 	if (num == NULL) {
3004235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line,"
3014235Smarkfen 		    " was expecting a number.\n"));
3024235Smarkfen 		/* NOTREACHED */
3034235Smarkfen 	}
3044235Smarkfen 
3054235Smarkfen 	errno = 0;
3064235Smarkfen 	rc = strtoull(num, &end, 0);
3074235Smarkfen 	if (errno != 0 || end == num || *end != '\0') {
3084235Smarkfen 		if (bail) {
3094235Smarkfen 			ERROR1(ep, ebuf, gettext(
3104235Smarkfen 			"Expecting a number, not \"%s\"!\n"), num);
3114235Smarkfen 		} else {
3124235Smarkfen 			/*
3134235Smarkfen 			 * -1, while not optimal, is sufficiently out of range
3144235Smarkfen 			 * for most of this function's applications when
3154235Smarkfen 			 * we don't just bail.
3164235Smarkfen 			 */
3174235Smarkfen 			return ((u_longlong_t)-1);
3184235Smarkfen 		}
3194235Smarkfen 	}
3204235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
3214235Smarkfen 	return (rc);
3224235Smarkfen }
3234235Smarkfen 
3244235Smarkfen /*
3254235Smarkfen  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
3264235Smarkfen  */
3274235Smarkfen static struct typetable {
3284235Smarkfen 	char *type;
3294235Smarkfen 	int token;
3304235Smarkfen } type_table[] = {
3314235Smarkfen 	{"all",	SADB_SATYPE_UNSPEC},
3324235Smarkfen 	{"ah",	SADB_SATYPE_AH},
3334235Smarkfen 	{"esp",	SADB_SATYPE_ESP},
3344235Smarkfen 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
3354235Smarkfen 	{NULL,	0}	/* Token value is irrelevant for this entry. */
3364235Smarkfen };
3374235Smarkfen 
3384235Smarkfen 
3394235Smarkfen static int
3404235Smarkfen parsesatype(char *type, char *ebuf)
3414235Smarkfen {
3424235Smarkfen 	struct typetable *tt = type_table;
3434235Smarkfen 	char *ep = NULL;
3444235Smarkfen 
3454235Smarkfen 	if (type == NULL)
3464235Smarkfen 		return (SADB_SATYPE_UNSPEC);
3474235Smarkfen 
3484235Smarkfen 	while (tt->type != NULL && strcasecmp(tt->type, type) != 0)
3494235Smarkfen 		tt++;
3504235Smarkfen 
3514235Smarkfen 	/*
3524235Smarkfen 	 * New SA types (including ones keysock maintains for user-land
3534235Smarkfen 	 * protocols) may be added, so parse a numeric value if possible.
3544235Smarkfen 	 */
3554235Smarkfen 	if (tt->type == NULL) {
3564235Smarkfen 		tt->token = (int)parsenum(type, B_FALSE, ebuf);
3574235Smarkfen 		if (tt->token == -1) {
3584235Smarkfen 			ERROR1(ep, ebuf, gettext(
3594235Smarkfen 			    "Unknown SA type (%s).\n"), type);
3604235Smarkfen 			tt->token = SADB_SATYPE_UNSPEC;
3614235Smarkfen 		}
3624235Smarkfen 	}
3634235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
3644235Smarkfen 	return (tt->token);
3654235Smarkfen }
3664235Smarkfen 
3674235Smarkfen #define	NEXTEOF		0
3684235Smarkfen #define	NEXTNONE	1
3694235Smarkfen #define	NEXTNUM		2
3704235Smarkfen #define	NEXTSTR		3
3714235Smarkfen #define	NEXTNUMSTR	4
3724235Smarkfen #define	NEXTADDR	5
3734235Smarkfen #define	NEXTHEX		6
3744235Smarkfen #define	NEXTIDENT	7
3754235Smarkfen #define	NEXTADDR4	8
3764235Smarkfen #define	NEXTADDR6	9
3774235Smarkfen 
3784235Smarkfen #define	TOK_EOF			0
3794235Smarkfen #define	TOK_UNKNOWN		1
3804235Smarkfen #define	TOK_SPI			2
3814235Smarkfen #define	TOK_REPLAY		3
3824235Smarkfen #define	TOK_STATE		4
3834235Smarkfen #define	TOK_AUTHALG		5
3844235Smarkfen #define	TOK_ENCRALG		6
3854235Smarkfen #define	TOK_FLAGS		7
3864235Smarkfen #define	TOK_SOFT_ALLOC		8
3874235Smarkfen #define	TOK_SOFT_BYTES		9
3884235Smarkfen #define	TOK_SOFT_ADDTIME	10
3894235Smarkfen #define	TOK_SOFT_USETIME	11
3904235Smarkfen #define	TOK_HARD_ALLOC		12
3914235Smarkfen #define	TOK_HARD_BYTES		13
3924235Smarkfen #define	TOK_HARD_ADDTIME	14
3934235Smarkfen #define	TOK_HARD_USETIME	15
3944235Smarkfen #define	TOK_CURRENT_ALLOC	16
3954235Smarkfen #define	TOK_CURRENT_BYTES	17
3964235Smarkfen #define	TOK_CURRENT_ADDTIME	18
3974235Smarkfen #define	TOK_CURRENT_USETIME	19
3984235Smarkfen #define	TOK_SRCADDR		20
3994235Smarkfen #define	TOK_DSTADDR		21
4004235Smarkfen #define	TOK_PROXYADDR		22
4014235Smarkfen #define	TOK_AUTHKEY		23
4024235Smarkfen #define	TOK_ENCRKEY		24
4034235Smarkfen #define	TOK_SRCIDTYPE		25
4044235Smarkfen #define	TOK_DSTIDTYPE		26
4054235Smarkfen #define	TOK_DPD			27
4064235Smarkfen #define	TOK_SENS_LEVEL		28
4074235Smarkfen #define	TOK_SENS_MAP		29
4084235Smarkfen #define	TOK_INTEG_LEVEL		30
4094235Smarkfen #define	TOK_INTEG_MAP		31
4104235Smarkfen #define	TOK_SRCADDR6		32
4114235Smarkfen #define	TOK_DSTADDR6		33
4124235Smarkfen #define	TOK_PROXYADDR6		34
4134235Smarkfen #define	TOK_SRCPORT		35
4144235Smarkfen #define	TOK_DSTPORT		36
4154235Smarkfen #define	TOK_PROTO		37
4164235Smarkfen #define	TOK_ENCAP		38
4174235Smarkfen #define	TOK_NATLOC		39
4184235Smarkfen #define	TOK_NATREM		40
4194235Smarkfen #define	TOK_NATLPORT		41
4204235Smarkfen #define	TOK_NATRPORT		42
4214235Smarkfen #define	TOK_IPROTO		43
4224235Smarkfen #define	TOK_IDSTADDR		44
4234235Smarkfen #define	TOK_IDSTADDR6		45
4244235Smarkfen #define	TOK_ISRCPORT		46
4254235Smarkfen #define	TOK_IDSTPORT		47
4264235Smarkfen 
4274235Smarkfen static struct toktable {
4284235Smarkfen 	char *string;
4294235Smarkfen 	int token;
4304235Smarkfen 	int next;
4314235Smarkfen } tokens[] = {
4324235Smarkfen 	/* "String",		token value,		next arg is */
4334235Smarkfen 	{"spi",			TOK_SPI,		NEXTNUM},
4344235Smarkfen 	{"replay",		TOK_REPLAY,		NEXTNUM},
4354235Smarkfen 	{"state",		TOK_STATE,		NEXTNUMSTR},
4364235Smarkfen 	{"auth_alg",		TOK_AUTHALG,		NEXTNUMSTR},
4374235Smarkfen 	{"authalg",		TOK_AUTHALG,		NEXTNUMSTR},
4384235Smarkfen 	{"encr_alg",		TOK_ENCRALG,		NEXTNUMSTR},
4394235Smarkfen 	{"encralg",		TOK_ENCRALG,		NEXTNUMSTR},
4404235Smarkfen 	{"flags",		TOK_FLAGS,		NEXTNUM},
4414235Smarkfen 	{"soft_alloc",		TOK_SOFT_ALLOC,		NEXTNUM},
4424235Smarkfen 	{"soft_bytes",		TOK_SOFT_BYTES,		NEXTNUM},
4434235Smarkfen 	{"soft_addtime",	TOK_SOFT_ADDTIME,	NEXTNUM},
4444235Smarkfen 	{"soft_usetime",	TOK_SOFT_USETIME,	NEXTNUM},
4454235Smarkfen 	{"hard_alloc",		TOK_HARD_ALLOC,		NEXTNUM},
4464235Smarkfen 	{"hard_bytes",		TOK_HARD_BYTES,		NEXTNUM},
4474235Smarkfen 	{"hard_addtime",	TOK_HARD_ADDTIME,	NEXTNUM},
4484235Smarkfen 	{"hard_usetime",	TOK_HARD_USETIME,	NEXTNUM},
4494235Smarkfen 	{"current_alloc",	TOK_CURRENT_ALLOC,	NEXTNUM},
4504235Smarkfen 	{"current_bytes",	TOK_CURRENT_BYTES,	NEXTNUM},
4514235Smarkfen 	{"current_addtime",	TOK_CURRENT_ADDTIME,	NEXTNUM},
4524235Smarkfen 	{"current_usetime",	TOK_CURRENT_USETIME,	NEXTNUM},
4534235Smarkfen 
4544235Smarkfen 	{"saddr",		TOK_SRCADDR,		NEXTADDR},
4554235Smarkfen 	{"srcaddr",		TOK_SRCADDR,		NEXTADDR},
4564235Smarkfen 	{"src",			TOK_SRCADDR,		NEXTADDR},
4574235Smarkfen 	{"daddr",		TOK_DSTADDR,		NEXTADDR},
4584235Smarkfen 	{"dstaddr",		TOK_DSTADDR,		NEXTADDR},
4594235Smarkfen 	{"dst",			TOK_DSTADDR,		NEXTADDR},
4604235Smarkfen 	{"proxyaddr",		TOK_PROXYADDR,		NEXTADDR},
4614235Smarkfen 	{"proxy",		TOK_PROXYADDR,		NEXTADDR},
4624235Smarkfen 	{"innersrc",		TOK_PROXYADDR,		NEXTADDR},
4634235Smarkfen 	{"isrc",		TOK_PROXYADDR,		NEXTADDR},
4644235Smarkfen 	{"innerdst",		TOK_IDSTADDR,		NEXTADDR},
4654235Smarkfen 	{"idst",		TOK_IDSTADDR,		NEXTADDR},
4664235Smarkfen 
4674235Smarkfen 	{"sport",		TOK_SRCPORT,		NEXTNUM},
4684235Smarkfen 	{"dport",		TOK_DSTPORT,		NEXTNUM},
4694235Smarkfen 	{"innersport",		TOK_ISRCPORT,		NEXTNUM},
4704235Smarkfen 	{"isport",		TOK_ISRCPORT,		NEXTNUM},
4714235Smarkfen 	{"innerdport",		TOK_IDSTPORT,		NEXTNUM},
4724235Smarkfen 	{"idport",		TOK_IDSTPORT,		NEXTNUM},
4734235Smarkfen 	{"proto",		TOK_PROTO,		NEXTNUM},
4744235Smarkfen 	{"ulp",			TOK_PROTO,		NEXTNUM},
4754235Smarkfen 	{"iproto",		TOK_IPROTO,		NEXTNUM},
4764235Smarkfen 	{"iulp",		TOK_IPROTO,		NEXTNUM},
4774235Smarkfen 
4784235Smarkfen 	{"saddr6",		TOK_SRCADDR6,		NEXTADDR},
4794235Smarkfen 	{"srcaddr6",		TOK_SRCADDR6,		NEXTADDR},
4804235Smarkfen 	{"src6",		TOK_SRCADDR6,		NEXTADDR},
4814235Smarkfen 	{"daddr6",		TOK_DSTADDR6,		NEXTADDR},
4824235Smarkfen 	{"dstaddr6",		TOK_DSTADDR6,		NEXTADDR},
4834235Smarkfen 	{"dst6",		TOK_DSTADDR6,		NEXTADDR},
4844235Smarkfen 	{"proxyaddr6",		TOK_PROXYADDR6,		NEXTADDR},
4854235Smarkfen 	{"proxy6",		TOK_PROXYADDR6,		NEXTADDR},
4864235Smarkfen 	{"innersrc6",		TOK_PROXYADDR6,		NEXTADDR},
4874235Smarkfen 	{"isrc6",		TOK_PROXYADDR6,		NEXTADDR},
4884235Smarkfen 	{"innerdst6",		TOK_IDSTADDR6,		NEXTADDR},
4894235Smarkfen 	{"idst6",		TOK_IDSTADDR6,		NEXTADDR},
4904235Smarkfen 
4914235Smarkfen 	{"authkey",		TOK_AUTHKEY,		NEXTHEX},
4924235Smarkfen 	{"encrkey",		TOK_ENCRKEY,		NEXTHEX},
4934235Smarkfen 	{"srcidtype",		TOK_SRCIDTYPE,		NEXTIDENT},
4944235Smarkfen 	{"dstidtype",		TOK_DSTIDTYPE,		NEXTIDENT},
4954235Smarkfen 	{"dpd",			TOK_DPD,		NEXTNUM},
4964235Smarkfen 	{"sens_level",		TOK_SENS_LEVEL,		NEXTNUM},
4974235Smarkfen 	{"sens_map",		TOK_SENS_MAP,		NEXTHEX},
4984235Smarkfen 	{"integ_level",		TOK_INTEG_LEVEL,	NEXTNUM},
4994235Smarkfen 	{"integ_map",		TOK_INTEG_MAP,		NEXTHEX},
5004235Smarkfen 	{"nat_loc",		TOK_NATLOC,		NEXTADDR},
5014235Smarkfen 	{"nat_rem",		TOK_NATREM,		NEXTADDR},
5024235Smarkfen 	{"nat_lport",		TOK_NATLPORT,		NEXTNUM},
5034235Smarkfen 	{"nat_rport",		TOK_NATRPORT,		NEXTNUM},
5044235Smarkfen 	{"encap",		TOK_ENCAP,		NEXTNUMSTR},
5054235Smarkfen 	{NULL,			TOK_UNKNOWN,		NEXTEOF}
5064235Smarkfen };
5074235Smarkfen 
5084235Smarkfen /*
5094235Smarkfen  * Q:	Do I need stuff for proposals, combinations, supported algorithms,
5104235Smarkfen  *	or SPI ranges?
5114235Smarkfen  *
5124235Smarkfen  * A:	Probably not, but you never know.
5134235Smarkfen  *
5144235Smarkfen  * Parse out extension header type values.
5154235Smarkfen  */
5164235Smarkfen static int
5174235Smarkfen parseextval(char *value, int *next)
5184235Smarkfen {
5194235Smarkfen 	struct toktable *tp;
5204235Smarkfen 
5214235Smarkfen 	if (value == NULL)
5224235Smarkfen 		return (TOK_EOF);
5234235Smarkfen 
5244235Smarkfen 	for (tp = tokens; tp->string != NULL; tp++)
5254235Smarkfen 		if (strcmp(value, tp->string) == 0)
5264235Smarkfen 			break;
5274235Smarkfen 
5284235Smarkfen 	/*
5294235Smarkfen 	 * Since the OS controls what extensions are available, we don't have
5304235Smarkfen 	 * to parse numeric values here.
5314235Smarkfen 	 */
5324235Smarkfen 
5334235Smarkfen 	*next = tp->next;
5344235Smarkfen 	return (tp->token);
5354235Smarkfen }
5364235Smarkfen 
5374235Smarkfen /*
5384235Smarkfen  * Parse possible state values.
5394235Smarkfen  */
5404235Smarkfen static uint8_t
5414235Smarkfen parsestate(char *state, char *ebuf)
5424235Smarkfen {
5434235Smarkfen 	struct states {
5444235Smarkfen 		char *state;
5454235Smarkfen 		uint8_t retval;
5464235Smarkfen 	} states[] = {
5474235Smarkfen 		{"larval",	SADB_SASTATE_LARVAL},
5484235Smarkfen 		{"mature",	SADB_SASTATE_MATURE},
5494235Smarkfen 		{"dying",	SADB_SASTATE_DYING},
5504235Smarkfen 		{"dead",	SADB_SASTATE_DEAD},
5514235Smarkfen 		{NULL,		0}
5524235Smarkfen 	};
5534235Smarkfen 	struct states *sp;
5544235Smarkfen 	char *ep = NULL;
5554235Smarkfen 
5564235Smarkfen 	if (state == NULL) {
5574235Smarkfen 		FATAL(ep, ebuf, "Unexpected end of command line "
5584235Smarkfen 		    "was expecting a state.\n");
5594235Smarkfen 	}
5604235Smarkfen 
5614235Smarkfen 	for (sp = states; sp->state != NULL; sp++) {
5624235Smarkfen 		if (strcmp(sp->state, state) == 0)
5634235Smarkfen 			return (sp->retval);
5644235Smarkfen 	}
5654235Smarkfen 	ERROR1(ep, ebuf, gettext("Unknown state type \"%s\"\n"), state);
5664235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
5674235Smarkfen 	return (0);
5684235Smarkfen }
5694235Smarkfen 
5704235Smarkfen /*
5714235Smarkfen  * Return the numerical algorithm identifier corresponding to the specified
5724235Smarkfen  * algorithm name.
5734235Smarkfen  */
5744235Smarkfen static uint8_t
5754235Smarkfen parsealg(char *alg, int proto_num, char *ebuf)
5764235Smarkfen {
5774235Smarkfen 	u_longlong_t invalue;
5784235Smarkfen 	struct ipsecalgent *algent;
5794235Smarkfen 	char *ep = NULL;
5804235Smarkfen 
5814235Smarkfen 	if (alg == NULL) {
5824235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
5834235Smarkfen 		    "was expecting an algorithm name.\n"));
5844235Smarkfen 	}
5854235Smarkfen 
5864235Smarkfen 	algent = getipsecalgbyname(alg, proto_num, NULL);
5874235Smarkfen 	if (algent != NULL) {
5884235Smarkfen 		uint8_t alg_num;
5894235Smarkfen 
5904235Smarkfen 		alg_num = algent->a_alg_num;
5914235Smarkfen 		freeipsecalgent(algent);
5924235Smarkfen 
5934235Smarkfen 		return (alg_num);
5944235Smarkfen 	}
5954235Smarkfen 
5964235Smarkfen 	/*
5974235Smarkfen 	 * Since algorithms can be loaded during kernel run-time, check for
5984235Smarkfen 	 * numeric algorithm values too.  PF_KEY can catch bad ones with EINVAL.
5994235Smarkfen 	 */
6004235Smarkfen 	invalue = parsenum(alg, B_FALSE, ebuf);
6014235Smarkfen 	if (invalue != (u_longlong_t)-1 &&
6024235Smarkfen 	    (u_longlong_t)(invalue & (u_longlong_t)0xff) == invalue)
6034235Smarkfen 		return ((uint8_t)invalue);
6044235Smarkfen 
6054235Smarkfen 	if (proto_num == IPSEC_PROTO_ESP) {
6064235Smarkfen 		ERROR1(ep, ebuf, gettext(
6074235Smarkfen 		    "Unknown encryption algorithm type \"%s\"\n"), alg);
6084235Smarkfen 	} else {
6094235Smarkfen 		ERROR1(ep, ebuf, gettext(
6104235Smarkfen 		    "Unknown authentication algorithm type \"%s\"\n"), alg);
6114235Smarkfen 	}
6124235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
6134235Smarkfen 	return (0);
6144235Smarkfen }
6154235Smarkfen 
6164235Smarkfen /*
6174235Smarkfen  * Parse and reverse parse out a source/destination ID type.
6184235Smarkfen  */
6194235Smarkfen static struct idtypes {
6204235Smarkfen 	char *idtype;
6214235Smarkfen 	uint8_t retval;
6224235Smarkfen } idtypes[] = {
6234235Smarkfen 	{"prefix",	SADB_IDENTTYPE_PREFIX},
6244235Smarkfen 	{"fqdn",	SADB_IDENTTYPE_FQDN},
6254235Smarkfen 	{"domain",	SADB_IDENTTYPE_FQDN},
6264235Smarkfen 	{"domainname",	SADB_IDENTTYPE_FQDN},
6274235Smarkfen 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
6284235Smarkfen 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
6294235Smarkfen 	{"der_dn",	SADB_X_IDENTTYPE_DN},
6304235Smarkfen 	{"der_gn",	SADB_X_IDENTTYPE_GN},
6314235Smarkfen 	{NULL,		0}
6324235Smarkfen };
6334235Smarkfen 
6344235Smarkfen static uint16_t
6354235Smarkfen parseidtype(char *type, char *ebuf)
6364235Smarkfen {
6374235Smarkfen 	struct idtypes *idp;
6384235Smarkfen 	u_longlong_t invalue;
6394235Smarkfen 	char *ep = NULL;
6404235Smarkfen 
6414235Smarkfen 	if (type == NULL) {
6424235Smarkfen 		/* Shouldn't reach here, see callers for why. */
6434235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
6444235Smarkfen 		    "was expecting a type.\n"));
6454235Smarkfen 	}
6464235Smarkfen 
6474235Smarkfen 	for (idp = idtypes; idp->idtype != NULL; idp++) {
6484235Smarkfen 		if (strcasecmp(idp->idtype, type) == 0)
6494235Smarkfen 			return (idp->retval);
6504235Smarkfen 	}
6514235Smarkfen 	/*
6524235Smarkfen 	 * Since identity types are almost arbitrary, check for numeric
6534235Smarkfen 	 * algorithm values too.  PF_KEY can catch bad ones with EINVAL.
6544235Smarkfen 	 */
6554235Smarkfen 	invalue = parsenum(type, B_FALSE, ebuf);
6564235Smarkfen 	if (invalue != (u_longlong_t)-1 &&
6574235Smarkfen 	    (u_longlong_t)(invalue & (u_longlong_t)0xffff) == invalue)
6584235Smarkfen 		return ((uint16_t)invalue);
6594235Smarkfen 
6604235Smarkfen 
6614235Smarkfen 	ERROR1(ep, ebuf, gettext("Unknown identity type \"%s\"\n"), type);
6624235Smarkfen 
6634235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
6644235Smarkfen 	return (0);
6654235Smarkfen }
6664235Smarkfen 
6674235Smarkfen /*
6684235Smarkfen  * Parse an address off the command line.  Return length of sockaddr,
6694235Smarkfen  * and either return a hostent pointer (caller frees).  The new
6704235Smarkfen  * getipnodebyname() call does the Right Thing (TM), even with
6714235Smarkfen  * raw addresses (colon-separated IPv6 or dotted decimal IPv4).
6724235Smarkfen  */
6734235Smarkfen 
6744235Smarkfen static struct {
6754235Smarkfen 	struct hostent he;
6764235Smarkfen 	char *addtl[2];
6774235Smarkfen 	} dummy;
6784235Smarkfen static union {
6794235Smarkfen 	struct in6_addr ipv6;
6804235Smarkfen 	struct in_addr ipv4;
6814235Smarkfen 	uint64_t aligner;
6824235Smarkfen } addr1;
6834235Smarkfen 
6844235Smarkfen static int
6854235Smarkfen parseaddr(char *addr, struct hostent **hpp, boolean_t v6only, char *ebuf)
6864235Smarkfen {
6874235Smarkfen 	int hp_errno;
6884235Smarkfen 	struct hostent *hp = NULL;
6894235Smarkfen 	char *ep = NULL;
6904235Smarkfen 
6914235Smarkfen 	if (addr == NULL) {
6924235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
6934235Smarkfen 		    "was expecting an address.\n"));
6944235Smarkfen 	}
6954235Smarkfen 
6964235Smarkfen 	if (!nflag) {
6974235Smarkfen 		/*
6984235Smarkfen 		 * Try name->address first.  Assume AF_INET6, and
6994235Smarkfen 		 * get IPv4's, plus IPv6's if and only if IPv6 is configured.
7004235Smarkfen 		 * This means to add IPv6 SAs, you must have IPv6
7014235Smarkfen 		 * up-and-running.  (AI_DEFAULT works here.)
7024235Smarkfen 		 */
7034235Smarkfen 		hp = getipnodebyname(addr, AF_INET6,
7044235Smarkfen 		    (v6only ? AI_ADDRCONFIG : (AI_DEFAULT | AI_ALL)),
7054235Smarkfen 		    &hp_errno);
7064235Smarkfen 	} else {
7074235Smarkfen 		/*
7084235Smarkfen 		 * Try a normal address conversion only.  Use "dummy"
7094235Smarkfen 		 * to construct a fake hostent.  Caller will know not
7104235Smarkfen 		 * to free this one.
7114235Smarkfen 		 */
7124235Smarkfen 		if (inet_pton(AF_INET6, addr, &addr1) == 1) {
7134235Smarkfen 			dummy.he.h_addr_list = dummy.addtl;
7144235Smarkfen 			dummy.addtl[0] = (char *)&addr1;
7154235Smarkfen 			dummy.addtl[1] = NULL;
7164235Smarkfen 			hp = &dummy.he;
7174235Smarkfen 			dummy.he.h_addrtype = AF_INET6;
7184235Smarkfen 			dummy.he.h_length = sizeof (struct in6_addr);
7194235Smarkfen 		} else if (inet_pton(AF_INET, addr, &addr1) == 1) {
7204235Smarkfen 			/*
7214235Smarkfen 			 * Remap to AF_INET6 anyway.
7224235Smarkfen 			 */
7234235Smarkfen 			dummy.he.h_addr_list = dummy.addtl;
7244235Smarkfen 			dummy.addtl[0] = (char *)&addr1;
7254235Smarkfen 			dummy.addtl[1] = NULL;
7264235Smarkfen 			hp = &dummy.he;
7274235Smarkfen 			dummy.he.h_addrtype = AF_INET6;
7284235Smarkfen 			dummy.he.h_length = sizeof (struct in6_addr);
7294235Smarkfen 			/*
7304235Smarkfen 			 * NOTE:  If macro changes to disallow in-place
7314235Smarkfen 			 * conversion, rewhack this.
7324235Smarkfen 			 */
7334235Smarkfen 			IN6_INADDR_TO_V4MAPPED(&addr1.ipv4, &addr1.ipv6);
7344235Smarkfen 		} else {
7354235Smarkfen 			hp = NULL;
7364235Smarkfen 		}
7374235Smarkfen 	}
7384235Smarkfen 
7394235Smarkfen 	if (hp == NULL)
7404235Smarkfen 		WARN1(ep, ebuf, gettext("Unknown address %s."), addr);
7414235Smarkfen 
7424235Smarkfen 	*hpp = hp;
7434235Smarkfen 	/* Always return sockaddr_in6 for now. */
7444235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
7454235Smarkfen 	return (sizeof (struct sockaddr_in6));
7464235Smarkfen }
7474235Smarkfen 
7484235Smarkfen /*
7494235Smarkfen  * Parse a hex character for a key.  A string will take the form:
7504235Smarkfen  *	xxxxxxxxx/nn
7514235Smarkfen  * where
7524235Smarkfen  *	xxxxxxxxx == a string of hex characters ([0-9][a-f][A-F])
7534235Smarkfen  *	nn == an optional decimal "mask".  If it is not present, it
7544235Smarkfen  *	is assumed that the hex string will be rounded to the nearest
7554235Smarkfen  *	byte, where odd nibbles, like 123 will become 0x0123.
7564235Smarkfen  *
7574235Smarkfen  * NOTE:Unlike the expression of IP addresses, I will not allow an
7584235Smarkfen  *	excessive "mask".  For example 2112/50 is very illegal.
7594235Smarkfen  * NOTE2:	This key should be in canonical order.  Consult your man
7604235Smarkfen  *		pages per algorithm about said order.
7614235Smarkfen  */
7624235Smarkfen 
7634235Smarkfen #define	hd2num(hd) (((hd) >= '0' && (hd) <= '9') ? ((hd) - '0') : \
7644235Smarkfen 	(((hd) >= 'a' && (hd) <= 'f') ? ((hd) - 'a' + 10) : ((hd) - 'A' + 10)))
7654235Smarkfen 
7664235Smarkfen static struct sadb_key *
7674235Smarkfen parsekey(char *input, char *ebuf)
7684235Smarkfen {
7694235Smarkfen 	struct sadb_key *retval;
7704235Smarkfen 	uint_t i, hexlen = 0, bits, alloclen;
7714235Smarkfen 	uint8_t *key;
7724235Smarkfen 	char *ep = NULL;
7734235Smarkfen 
7744235Smarkfen 	if (input == NULL) {
7754235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
7764235Smarkfen 		    "was expecting a key.\n"));
7774235Smarkfen 	}
7784573Spwernau 	/* Allow hex values prepended with 0x convention */
7794573Spwernau 	if ((strnlen(input, sizeof (hexlen)) > 2) &&
7804573Spwernau 	    (strncasecmp(input, "0x", 2) == 0))
7814573Spwernau 		input += 2;
7824235Smarkfen 
7834235Smarkfen 	for (i = 0; input[i] != '\0' && input[i] != '/'; i++)
7844235Smarkfen 		hexlen++;
7854235Smarkfen 
7864235Smarkfen 	if (input[i] == '\0') {
7874235Smarkfen 		bits = 0;
7884235Smarkfen 	} else {
7894235Smarkfen 		/* Have /nn. */
7904235Smarkfen 		input[i] = '\0';
7914235Smarkfen 		if (sscanf((input + i + 1), "%u", &bits) != 1) {
7924235Smarkfen 			FATAL1(ep, ebuf, gettext(
7934235Smarkfen 			    "\"%s\" is not a bit specifier.\n"),
7944235Smarkfen 			    (input + i + 1));
7954235Smarkfen 		}
7964235Smarkfen 		/* hexlen in nibbles */
7974235Smarkfen 		if (((bits + 3) >> 2) > hexlen) {
7984235Smarkfen 			ERROR2(ep, ebuf, gettext(
7994235Smarkfen 			    "bit length %d is too big for %s.\n"), bits, input);
8004235Smarkfen 		}
8014235Smarkfen 		/*
8024235Smarkfen 		 * Adjust hexlen down if user gave us too small of a bit
8034235Smarkfen 		 * count.
8044235Smarkfen 		 */
8054235Smarkfen 		if ((hexlen << 2) > bits + 3) {
8064235Smarkfen 			WARN2(ep, ebuf, gettext(
8074235Smarkfen 			    "WARNING: Lower bits will be truncated "
8084235Smarkfen 			    "for:\n\t%s/%d.\n"), input, bits);
8094235Smarkfen 			hexlen = (bits + 3) >> 2;
8104235Smarkfen 			input[hexlen] = '\0';
8114235Smarkfen 		}
8124235Smarkfen 	}
8134235Smarkfen 
8144235Smarkfen 	/*
8154235Smarkfen 	 * Allocate.  Remember, hexlen is in nibbles.
8164235Smarkfen 	 */
8174235Smarkfen 
8184235Smarkfen 	alloclen = sizeof (*retval) + roundup((hexlen/2 + (hexlen & 0x1)), 8);
8194235Smarkfen 	retval = malloc(alloclen);
8204235Smarkfen 
8214235Smarkfen 	if (retval == NULL)
8224235Smarkfen 		Bail("malloc(parsekey)");
8234235Smarkfen 	retval->sadb_key_len = SADB_8TO64(alloclen);
8244235Smarkfen 	retval->sadb_key_reserved = 0;
8254235Smarkfen 	if (bits == 0)
8264235Smarkfen 		retval->sadb_key_bits = (hexlen + (hexlen & 0x1)) << 2;
8274235Smarkfen 	else
8284235Smarkfen 		retval->sadb_key_bits = bits;
8294235Smarkfen 
8304235Smarkfen 	/*
8314235Smarkfen 	 * Read in nibbles.  Read in odd-numbered as shifted high.
8324235Smarkfen 	 * (e.g. 123 becomes 0x1230).
8334235Smarkfen 	 */
8344235Smarkfen 
8354235Smarkfen 	key = (uint8_t *)(retval + 1);
8364235Smarkfen 	for (i = 0; input[i] != '\0'; i += 2) {
8374235Smarkfen 		boolean_t second = (input[i + 1] != '\0');
8384235Smarkfen 
8394235Smarkfen 		if (!isxdigit(input[i]) ||
8404235Smarkfen 		    (!isxdigit(input[i + 1]) && second)) {
8414235Smarkfen 			ERROR1(ep, ebuf, gettext(
8424235Smarkfen 			    "string '%s' not a hex value.\n"), input);
8434235Smarkfen 			free(retval);
8444235Smarkfen 			retval = NULL;
8454235Smarkfen 			break;
8464235Smarkfen 		}
8474235Smarkfen 		*key = (hd2num(input[i]) << 4);
8484235Smarkfen 		if (second)
8494235Smarkfen 			*key |= hd2num(input[i + 1]);
8504235Smarkfen 		else
8514235Smarkfen 			break;	/* out of for loop. */
8524235Smarkfen 		key++;
8534235Smarkfen 	}
8544235Smarkfen 
8554235Smarkfen 	/* bzero the remaining bits if we're a non-octet amount. */
8564235Smarkfen 	if (bits & 0x7)
8574235Smarkfen 		*((input[i] == '\0') ? key - 1 : key) &=
8584235Smarkfen 		    0xff << (8 - (bits & 0x7));
8594235Smarkfen 
8604235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
8614235Smarkfen 	return (retval);
8624235Smarkfen }
8634235Smarkfen 
8644235Smarkfen /*
8654235Smarkfen  * Write a message to the PF_KEY socket.  If verbose, print the message
8664235Smarkfen  * heading into the kernel.
8674235Smarkfen  */
8684235Smarkfen static int
8694235Smarkfen key_write(int fd, void *msg, size_t len)
8704235Smarkfen {
8714235Smarkfen 	if (vflag) {
8724235Smarkfen 		(void) printf(
8734235Smarkfen 		    gettext("VERBOSE ON:  Message to kernel looks like:\n"));
8744235Smarkfen 		(void) printf("==========================================\n");
875*4867Spwernau 		print_samsg(stdout, msg, B_FALSE, vflag, nflag);
8764235Smarkfen 		(void) printf("==========================================\n");
8774235Smarkfen 	}
8784235Smarkfen 
8794235Smarkfen 	return (write(fd, msg, len));
8804235Smarkfen }
8814235Smarkfen 
8824235Smarkfen /*
8834235Smarkfen  * SIGALRM handler for time_critical_enter.
8844235Smarkfen  */
8854235Smarkfen static void
8864235Smarkfen time_critical_catch(int signal)
8874235Smarkfen {
8884235Smarkfen 	if (signal == SIGALRM) {
8894235Smarkfen 		errx(1, gettext("Reply message from PF_KEY timed out."));
8904235Smarkfen 	} else {
8914235Smarkfen 		errx(1, gettext("Caught signal %d while trying to receive"
8924342Spwernau 		    "PF_KEY reply message"), signal);
8934235Smarkfen 	}
8944235Smarkfen 	/* errx() calls exit. */
8954235Smarkfen }
8964235Smarkfen 
8974235Smarkfen #define	TIME_CRITICAL_TIME 10	/* In seconds */
8984235Smarkfen 
8994235Smarkfen /*
9004235Smarkfen  * Enter a "time critical" section where key is waiting for a return message.
9014235Smarkfen  */
9024235Smarkfen static void
9034235Smarkfen time_critical_enter(void)
9044235Smarkfen {
9054235Smarkfen 	(void) signal(SIGALRM, time_critical_catch);
9064235Smarkfen 	(void) alarm(TIME_CRITICAL_TIME);
9074235Smarkfen }
9084235Smarkfen 
9094235Smarkfen /*
9104235Smarkfen  * Exit the "time critical" section after getting an appropriate return
9114235Smarkfen  * message.
9124235Smarkfen  */
9134235Smarkfen static void
9144235Smarkfen time_critical_exit(void)
9154235Smarkfen {
9164235Smarkfen 	(void) alarm(0);
9174235Smarkfen 	(void) signal(SIGALRM, SIG_DFL);
9184235Smarkfen }
9194235Smarkfen 
9204235Smarkfen /*
9214235Smarkfen  * Construct a PF_KEY FLUSH message for the SA type specified.
9224235Smarkfen  */
9234235Smarkfen static void
9244235Smarkfen doflush(int satype)
9254235Smarkfen {
9264235Smarkfen 	struct sadb_msg msg;
9274235Smarkfen 	int rc;
9284235Smarkfen 
9294235Smarkfen 	msg_init(&msg, SADB_FLUSH, (uint8_t)satype);
9304235Smarkfen 	rc = key_write(keysock, &msg, sizeof (msg));
9314235Smarkfen 	if (rc == -1)
9324235Smarkfen 		Bail("write() to PF_KEY socket failed (in doflush)");
9334235Smarkfen 
9344235Smarkfen 	time_critical_enter();
9354235Smarkfen 	do {
9364235Smarkfen 		rc = read(keysock, &msg, sizeof (msg));
9374235Smarkfen 		if (rc == -1)
9384235Smarkfen 			Bail("read (in doflush)");
9394235Smarkfen 	} while (msg.sadb_msg_seq != seq || msg.sadb_msg_pid != mypid);
9404235Smarkfen 	time_critical_exit();
9414235Smarkfen 
9424235Smarkfen 	/*
9434235Smarkfen 	 * I should _never_ hit the following unless:
9444235Smarkfen 	 *
9454235Smarkfen 	 * 1. There is a kernel bug.
9464235Smarkfen 	 * 2. There is another process filling in its pid with mine, and
9474235Smarkfen 	 *    issuing a different message that would cause a different result.
9484235Smarkfen 	 */
9494235Smarkfen 	if (msg.sadb_msg_type != SADB_FLUSH ||
9504235Smarkfen 	    msg.sadb_msg_satype != (uint8_t)satype) {
9514235Smarkfen 		syslog((LOG_NOTICE|LOG_AUTH),
9524235Smarkfen 		    gettext("doflush: Return message not of type SADB_FLUSH!"));
9534235Smarkfen 		Bail("doflush: Return message not of type SADB_FLUSH!");
9544235Smarkfen 	}
9554235Smarkfen 
9564235Smarkfen 	if (msg.sadb_msg_errno != 0) {
9574235Smarkfen 		errno = msg.sadb_msg_errno;
9584235Smarkfen 		if (errno == EINVAL) {
9594235Smarkfen 			print_diagnostic(stderr, msg.sadb_x_msg_diagnostic);
9604235Smarkfen 			warnx(gettext("Cannot flush SA type %d."), satype);
9614235Smarkfen 		}
9624235Smarkfen 		Bail("return message (in doflush)");
9634235Smarkfen 	}
9644235Smarkfen }
9654235Smarkfen 
9664235Smarkfen /*
9674235Smarkfen  * save_XXX functions are used when "saving" the SA tables to either a
9684235Smarkfen  * file or standard output.  They use the dump_XXX functions where needed,
9694235Smarkfen  * but mostly they use the rparseXXX functions.
9704235Smarkfen  */
9714235Smarkfen 
9724235Smarkfen /*
9734235Smarkfen  * Because "save" and "dump" both use the SADB_DUMP message, fold both
9744235Smarkfen  * into the same function.
9754235Smarkfen  */
9764235Smarkfen static void
9774235Smarkfen dodump(int satype, FILE *ofile)
9784235Smarkfen {
9794235Smarkfen 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
9804235Smarkfen 	int rc;
9814235Smarkfen 
9824235Smarkfen 	if (ofile != NULL) {
9834235Smarkfen 		(void) fprintf(ofile,
9844235Smarkfen 		    gettext("# This key file was generated by the"));
9854235Smarkfen 		(void) fprintf(ofile,
9864235Smarkfen 		    gettext(" ipseckey(1m) command's 'save' feature.\n\n"));
9874235Smarkfen 	}
9884235Smarkfen 	msg_init(msg, SADB_DUMP, (uint8_t)satype);
9894235Smarkfen 	rc = key_write(keysock, msg, sizeof (*msg));
9904235Smarkfen 	if (rc == -1)
9914235Smarkfen 		Bail("write to PF_KEY socket failed (in dodump)");
9924235Smarkfen 
9934235Smarkfen 	do {
9944235Smarkfen 		/*
9954235Smarkfen 		 * For DUMP, do only the read as a time critical section.
9964235Smarkfen 		 */
9974235Smarkfen 		time_critical_enter();
9984235Smarkfen 		rc = read(keysock, get_buffer, sizeof (get_buffer));
9994235Smarkfen 		time_critical_exit();
10004235Smarkfen 		if (rc == -1)
10014235Smarkfen 			Bail("read (in dodump)");
10024235Smarkfen 		if (msg->sadb_msg_pid == mypid &&
10034235Smarkfen 		    msg->sadb_msg_type == SADB_DUMP &&
10044235Smarkfen 		    msg->sadb_msg_seq != 0 &&
10054235Smarkfen 		    msg->sadb_msg_errno == 0) {
10064235Smarkfen 			if (ofile == NULL) {
1007*4867Spwernau 				print_samsg(stdout, get_buffer, B_FALSE, vflag,
1008*4867Spwernau 				    nflag);
10094235Smarkfen 				(void) putchar('\n');
10104235Smarkfen 			} else {
10114235Smarkfen 				save_assoc(get_buffer, ofile);
10124235Smarkfen 			}
10134235Smarkfen 		}
10144235Smarkfen 	} while (msg->sadb_msg_pid != mypid ||
10154235Smarkfen 	    (msg->sadb_msg_errno == 0 && msg->sadb_msg_seq != 0));
10164235Smarkfen 
10174235Smarkfen 	if (ofile != NULL && ofile != stdout)
10184235Smarkfen 		(void) fclose(ofile);
10194235Smarkfen 
10204235Smarkfen 	if (msg->sadb_msg_errno == 0) {
10214235Smarkfen 		if (ofile == NULL)
10224235Smarkfen 			(void) printf(
10234235Smarkfen 			    gettext("Dump succeeded for SA type %d.\n"),
10244235Smarkfen 			    satype);
10254235Smarkfen 	} else {
10264235Smarkfen 		print_diagnostic(stderr, msg->sadb_x_msg_diagnostic);
10274235Smarkfen 		errno = msg->sadb_msg_errno;
10284235Smarkfen 		Bail("Dump failed");
10294235Smarkfen 	}
10304235Smarkfen }
10314235Smarkfen 
10324235Smarkfen #define	SCOPE_UNSPEC 0
10334235Smarkfen #define	SCOPE_LINKLOCAL 1
10344235Smarkfen #define	SCOPE_SITELOCAL 2
10354235Smarkfen #define	SCOPE_GLOBAL 3
10364235Smarkfen #define	SCOPE_V4COMPAT 4
10374235Smarkfen #define	SCOPE_LOOPBACK 5	/* Pedantic, yes, but necessary. */
10384235Smarkfen 
10394235Smarkfen static int
10404235Smarkfen ipv6_addr_scope(struct in6_addr *addr)
10414235Smarkfen {
10424235Smarkfen 	/* Don't return anything regarding multicast for now... */
10434235Smarkfen 
10444235Smarkfen 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
10454235Smarkfen 		return (SCOPE_UNSPEC);
10464235Smarkfen 
10474235Smarkfen 	if (IN6_IS_ADDR_LINKLOCAL(addr))
10484235Smarkfen 		return (SCOPE_LINKLOCAL);
10494235Smarkfen 
10504235Smarkfen 	if (IN6_IS_ADDR_SITELOCAL(addr))
10514235Smarkfen 		return (SCOPE_SITELOCAL);
10524235Smarkfen 
10534235Smarkfen 	if (IN6_IS_ADDR_V4COMPAT(addr))
10544235Smarkfen 		return (SCOPE_V4COMPAT);
10554235Smarkfen 
10564235Smarkfen 	if (IN6_IS_ADDR_LOOPBACK(addr))
10574235Smarkfen 		return (SCOPE_LOOPBACK);
10584235Smarkfen 
10594235Smarkfen 	/* For now, return global by default. */
10604235Smarkfen 	return (SCOPE_GLOBAL);
10614235Smarkfen }
10624235Smarkfen 
10634235Smarkfen /*
10644235Smarkfen  * doaddresses():
10654235Smarkfen  *
10664235Smarkfen  * Used by doaddup() and dodelget() to create new SA's based on the
10674235Smarkfen  * provided source and destination addresses hostent.
10684235Smarkfen  *
10694235Smarkfen  * sadb_msg_type: expected PF_KEY reply message type
10704235Smarkfen  * sadb_msg_satype: expected PF_KEY reply satype
10714235Smarkfen  * cmd: user command
10724235Smarkfen  * srchp: hostent for the source address(es)
10734235Smarkfen  * dsthp: hostent for the destination address(es)
10744235Smarkfen  * src: points to the SADB source address extension
10754235Smarkfen  * dst: points to the SADB destination address extension
10764235Smarkfen  * unspec_src: indicates an unspecified source address.
10774235Smarkfen  * buffer: pointer to the SADB buffer to use with PF_KEY
10784235Smarkfen  * buffer_size: size of buffer
10794235Smarkfen  * spi: spi for this message (set by caller)
10804235Smarkfen  * srcport: source port if specified
10814235Smarkfen  * dstport: destination port if specified
10824235Smarkfen  * proto: IP protocol number if specified
10834235Smarkfen  * iproto: Inner (tunnel mode) IP protocol number if specified
10844235Smarkfen  * NATT note: we are going to assume a semi-sane world where NAT
10854235Smarkfen  * boxen don't explode to multiple addresses.
10864235Smarkfen  */
10874235Smarkfen static void
10884235Smarkfen doaddresses(uint8_t sadb_msg_type, uint8_t sadb_msg_satype, int cmd,
10894235Smarkfen     struct hostent *srchp, struct hostent *dsthp,
10904235Smarkfen     struct sadb_address *src, struct sadb_address *dst,
10914235Smarkfen     boolean_t unspec_src, uint64_t *buffer, int buffer_size, uint32_t spi,
10924235Smarkfen     char *ebuf)
10934235Smarkfen {
10944235Smarkfen 	boolean_t single_dst;
10954235Smarkfen 	struct sockaddr_in6 *sin6;
10964235Smarkfen 	struct sadb_msg *msgp;
10974235Smarkfen 	int i, rc;
10984235Smarkfen 	char **walker;	/* For the SRC and PROXY walking functions. */
10994235Smarkfen 	char *first_match;
11004235Smarkfen 	uint64_t savebuf[MAX_GET_SIZE];
11014235Smarkfen 	uint16_t srcport = 0, dstport = 0;
11024235Smarkfen 	char *ep = NULL;
11034235Smarkfen 
11044235Smarkfen 	/*
11054235Smarkfen 	 * Okay, now we have "src", "dst", and maybe "proxy" reassigned
11064235Smarkfen 	 * to point into the buffer to be written to PF_KEY, we can do
11074235Smarkfen 	 * potentially several writes based on destination address.
11084235Smarkfen 	 *
11094235Smarkfen 	 * First, obtain port numbers from passed-in extensions.
11104235Smarkfen 	 */
11114235Smarkfen 
11124235Smarkfen 	if (src != NULL) {
11134235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
11144235Smarkfen 		srcport = ntohs(sin6->sin6_port);
11154235Smarkfen 	}
11164235Smarkfen 	if (dst != NULL) {
11174235Smarkfen 		sin6 = (struct sockaddr_in6 *)(dst + 1);
11184235Smarkfen 		dstport = ntohs(sin6->sin6_port);
11194235Smarkfen 	}
11204235Smarkfen 
11214235Smarkfen 	/*
11224235Smarkfen 	 * The rules for ADD, GET, and UPDATE: (NOTE:  This assumes IPsec.
11234235Smarkfen 	 * If other consumers of PF_KEY happen, this will have to be
11244235Smarkfen 	 * rewhacked.):
11254235Smarkfen 	 *
11264235Smarkfen 	 *	Do a message for every possible DST address.
11274235Smarkfen 	 *
11284235Smarkfen 	 *	If a source or proxy address explodes, keep unspecified
11294235Smarkfen 	 *	(and mention unspecified).
11304235Smarkfen 	 *
11314235Smarkfen 	 * If dsthp is == dummy.he, then go through the loop once.
11324235Smarkfen 	 * If any other hp is == dummy.he, then you don't have to apply any
11334235Smarkfen 	 * silly rules.
11344235Smarkfen 	 *
11354235Smarkfen 	 * DELETE is different, because you can leave either "src" or "dst"
11364235Smarkfen 	 * blank!  You need to explode if one of them is full, and not assume
11374235Smarkfen 	 * that the other is set.
11384235Smarkfen 	 */
11394235Smarkfen 
11404235Smarkfen 	if (dsthp == NULL) {
11414235Smarkfen 		/*
11424235Smarkfen 		 * No destination address specified.
11434235Smarkfen 		 * With extended diagnostics, we don't have to bail the
11444235Smarkfen 		 * non-DELETE cases here.  The EINVAL diagnostics will be
11454235Smarkfen 		 * enough to inform the user(s) what happened.
11464235Smarkfen 		 */
11474235Smarkfen 		i = 0;
11484235Smarkfen 		do {
11494235Smarkfen 			if (srchp == &dummy.he) {
11504235Smarkfen 				/* Just to be sure... */
11514235Smarkfen 				srchp->h_addr_list[1] = NULL;
11524235Smarkfen 			} else if (srchp != NULL) {
11534235Smarkfen 				/* Degenerate case, h_addr_list[0] == NULL. */
11544235Smarkfen 				if (srchp->h_addr_list[i] == NULL)
11554235Smarkfen 					Bail("Empty source address list");
11564235Smarkfen 
11574235Smarkfen 				/*
11584235Smarkfen 				 * Fill in the src sockaddr.
11594235Smarkfen 				 */
11604235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
11614235Smarkfen 				bzero(sin6, sizeof (*sin6));
11624235Smarkfen 				bcopy(srchp->h_addr_list[i], &sin6->sin6_addr,
11634235Smarkfen 				    sizeof (struct in6_addr));
11644235Smarkfen 				sin6->sin6_family = AF_INET6;
11654235Smarkfen 				sin6->sin6_port = htons(srcport);
11664235Smarkfen 			}
11674235Smarkfen 
11684235Smarkfen 			/* Save off a copy for later writing... */
11694235Smarkfen 			msgp = (struct sadb_msg *)buffer;
11704235Smarkfen 			bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
11714235Smarkfen 
11724235Smarkfen 			rc = key_write(keysock, buffer,
11734235Smarkfen 			    SADB_64TO8(msgp->sadb_msg_len));
11744235Smarkfen 			if (rc == -1)
11754235Smarkfen 				Bail("write() to PF_KEY socket "
11764235Smarkfen 				    "(in doaddresses)");
11774235Smarkfen 
11784235Smarkfen 			time_critical_enter();
11794235Smarkfen 			do {
11804235Smarkfen 				rc = read(keysock, buffer, buffer_size);
11814235Smarkfen 				if (rc == -1)
11824235Smarkfen 					Bail("read (in doaddresses)");
11834235Smarkfen 			} while (msgp->sadb_msg_seq != seq ||
11844235Smarkfen 			    msgp->sadb_msg_pid != mypid);
11854235Smarkfen 			time_critical_exit();
11864235Smarkfen 
11874235Smarkfen 			if (msgp->sadb_msg_type != sadb_msg_type ||
11884235Smarkfen 			    msgp->sadb_msg_satype != sadb_msg_satype) {
11894235Smarkfen 				syslog((LOG_NOTICE|LOG_AUTH), gettext(
11904235Smarkfen 				    "doaddresses: Unexpected returned message "
11914235Smarkfen 				    "(%d exp %d)\n"), msgp->sadb_msg_type,
11924235Smarkfen 				    sadb_msg_type);
11934235Smarkfen 				Bail("doaddresses: Unexpected returned "
11944235Smarkfen 				    "message");
11954235Smarkfen 			}
11964235Smarkfen 
11974235Smarkfen 			errno = msgp->sadb_msg_errno;
11984235Smarkfen 			if (errno != 0) {
11994235Smarkfen 				if (errno == EINVAL) {
12004235Smarkfen 					WARN(ep, ebuf, gettext(
12014235Smarkfen 					    "One of the entered "
12024235Smarkfen 					    "values is incorrect."));
12034235Smarkfen 					print_diagnostic(stderr,
12044235Smarkfen 					    msgp->sadb_x_msg_diagnostic);
12054235Smarkfen 				} else {
12064235Smarkfen 					Bail("return message (in doaddresses)");
12074235Smarkfen 				}
12084235Smarkfen 			}
12094235Smarkfen 
12104235Smarkfen 			/* ...and then restore the saved buffer. */
12114235Smarkfen 			msgp = (struct sadb_msg *)savebuf;
12124235Smarkfen 			bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
12134235Smarkfen 		} while (srchp != NULL && srchp->h_addr_list[++i] != NULL);
12144235Smarkfen 		return;
12154235Smarkfen 	}
12164235Smarkfen 
12174235Smarkfen 	single_dst = (dsthp == &dummy.he || dsthp->h_addr_list[1] == NULL);
12184235Smarkfen 
12194235Smarkfen 	for (i = 0; dsthp->h_addr_list[i] != NULL; i++) {
12204235Smarkfen 		if (dsthp == &dummy.he) {
12214235Smarkfen 			/* Just to be sure... */
12224235Smarkfen 			dsthp->h_addr_list[1] = NULL;
12234235Smarkfen 		} else {
12244235Smarkfen 			/*
12254235Smarkfen 			 * Fill in the dst sockaddr.
12264235Smarkfen 			 */
12274235Smarkfen 			sin6 = (struct sockaddr_in6 *)(dst + 1);
12284235Smarkfen 			bzero(sin6, sizeof (*sin6));
12294235Smarkfen 			bcopy(dsthp->h_addr_list[i], &sin6->sin6_addr,
12304235Smarkfen 			    sizeof (struct in6_addr));
12314235Smarkfen 			sin6->sin6_family = AF_INET6;
12324235Smarkfen 			sin6->sin6_port = htons(dstport);
12334235Smarkfen 		}
12344235Smarkfen 
12354235Smarkfen 		/*
12364235Smarkfen 		 * Try and assign src, if there's any ambiguity.
12374235Smarkfen 		 */
12384235Smarkfen 		if (!unspec_src && srchp != &dummy.he) {
12394235Smarkfen 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12404235Smarkfen 				/*
12414235Smarkfen 				 * IPv4 address.  Find an IPv4 address, then
12424235Smarkfen 				 * keep looking for a second one.  If a second
12434235Smarkfen 				 * exists, print a message, and fill in the
12444235Smarkfen 				 * unspecified address.
12454235Smarkfen 				 */
12464235Smarkfen 				first_match = NULL;
12474235Smarkfen 
12484235Smarkfen 				for (walker = srchp->h_addr_list;
12494235Smarkfen 				    *walker != NULL; walker++) {
12504235Smarkfen 					/* LINTED E_BAD_PTR_CAST_ALIGN */
12514235Smarkfen 					if (IN6_IS_ADDR_V4MAPPED(
12524235Smarkfen 					    (struct in6_addr *)*walker)) {
12534235Smarkfen 						if (first_match != NULL)
12544235Smarkfen 							break;
12554235Smarkfen 						else
12564235Smarkfen 							first_match = *walker;
12574235Smarkfen 					}
12584235Smarkfen 				}
12594235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
12604235Smarkfen 				bzero(sin6, sizeof (*sin6));
12614235Smarkfen 
12624235Smarkfen 				if (first_match == NULL) {
12634235Smarkfen 					/*
12644235Smarkfen 					 * No IPv4 hits.  Is this a single
12654235Smarkfen 					 * dest?
12664235Smarkfen 					 */
12674235Smarkfen 					WARN1(ep, ebuf, gettext(
12684235Smarkfen 					    "No IPv4 source address "
12694235Smarkfen 					    "for name %s.\n"), srchp->h_name);
12704235Smarkfen 					if (single_dst) {
12714235Smarkfen 						ERROR(ep, ebuf, gettext(
12724235Smarkfen 						    "Only single destination "
12734235Smarkfen 						    "IP address.\n"));
12744235Smarkfen 					} else {
12754235Smarkfen 						/* Continue, but do I print? */
12764235Smarkfen 						continue;  /* for loop */
12774235Smarkfen 					}
12784235Smarkfen 
12794235Smarkfen 					/* I should never reach here. */
12804235Smarkfen 				}
12814235Smarkfen 
12824235Smarkfen 				sin6->sin6_family = AF_INET6;
12834235Smarkfen 				sin6->sin6_port = htons(srcport);
12844235Smarkfen 				if (*walker != NULL) {
12854235Smarkfen 					/*
12864235Smarkfen 					 * Early loop exit.  It must've been
12874235Smarkfen 					 * multiple hits...
12884235Smarkfen 					 *
12894235Smarkfen 					 * Issue a null-source warning?
12904235Smarkfen 					 */
12914235Smarkfen 					WARN1(ep, ebuf, gettext(
12924235Smarkfen 					    "Multiple IPv4 source addresses "
12934235Smarkfen 					    "for %s, using unspecified source "
12944235Smarkfen 					    "instead."), srchp->h_name);
12954235Smarkfen 				} else {
12964235Smarkfen 					/*
12974235Smarkfen 					 * If I reach here w/o hitting the
12984235Smarkfen 					 * previous if statements, I have a
12994235Smarkfen 					 * single source address for this
13004235Smarkfen 					 * destination.
13014235Smarkfen 					 */
13024235Smarkfen 					bcopy(first_match, &sin6->sin6_addr,
13034235Smarkfen 					    sizeof (struct in6_addr));
13044235Smarkfen 				}
13054235Smarkfen 			} else {
13064235Smarkfen 				/*
13074235Smarkfen 				 * IPv6 address.  Find an IPv6 address.
13084235Smarkfen 				 * Unlike IPv4 addresses, things can get a
13094235Smarkfen 				 * little more sticky with scopes, etc.
13104235Smarkfen 				 */
13114235Smarkfen 				int dst_scope, src_scope;
13124235Smarkfen 
13134235Smarkfen 				dst_scope = ipv6_addr_scope(&sin6->sin6_addr);
13144235Smarkfen 
13154235Smarkfen 				first_match = NULL;
13164235Smarkfen 				for (walker = srchp->h_addr_list;
13174235Smarkfen 				    *walker != NULL; walker++) {
13184235Smarkfen 					/* LINTED E_BAD_PTR_CAST_ALIGN */
13194235Smarkfen 					if (!IN6_IS_ADDR_V4MAPPED(
13204235Smarkfen 					    (struct in6_addr *)*walker)) {
13214235Smarkfen 						/*
13224235Smarkfen 						 * Set first-match, etc.
13234235Smarkfen 						 * Take into account scopes,
13244235Smarkfen 						 * and other IPv6 thingies.
13254235Smarkfen 						 */
13264235Smarkfen 						src_scope = ipv6_addr_scope(
13274235Smarkfen 						    /* LINTED E_BAD_PTR_CAST */
13284235Smarkfen 						    (struct in6_addr *)*walker);
13294235Smarkfen 						if (src_scope == SCOPE_UNSPEC ||
13304235Smarkfen 						    src_scope == dst_scope) {
13314235Smarkfen 							if (first_match !=
13324235Smarkfen 							    NULL)
13334235Smarkfen 								break;
13344235Smarkfen 							else
13354235Smarkfen 								first_match =
13364235Smarkfen 								    *walker;
13374235Smarkfen 						}
13384235Smarkfen 					}
13394235Smarkfen 				}
13404235Smarkfen 
13414235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
13424235Smarkfen 				bzero(sin6, sizeof (*sin6));
13434235Smarkfen 				sin6->sin6_port = htons(srcport);
13444235Smarkfen 				if (first_match == NULL) {
13454235Smarkfen 					/*
13464235Smarkfen 					 * No IPv6 hits.  Is this a single
13474235Smarkfen 					 * dest?
13484235Smarkfen 					 */
13494235Smarkfen 					WARN1(ep, ebuf, gettext(
13504235Smarkfen 					    "No IPv6 source address of "
13514235Smarkfen 					    "matching scope for name %s.\n"),
13524235Smarkfen 					    srchp->h_name);
13534235Smarkfen 					if (single_dst) {
13544235Smarkfen 						ERROR(ep, ebuf, gettext(
13554235Smarkfen 						    "Only a single IPV6 "
13564235Smarkfen 						    "destination "
13574235Smarkfen 						    "address.\n"));
13584235Smarkfen 					} else {
13594235Smarkfen 						/* Continue, but do I print? */
13604235Smarkfen 						continue;  /* for loop */
13614235Smarkfen 					}
13624235Smarkfen 
13634235Smarkfen 					/* I should never reach here. */
13644235Smarkfen 				}
13654235Smarkfen 				sin6->sin6_family = AF_INET6;
13664235Smarkfen 				if (*walker != NULL) {
13674235Smarkfen 					/*
13684235Smarkfen 					 * Early loop exit.  Issue a
13694235Smarkfen 					 * null-source warning?
13704235Smarkfen 					 */
13714235Smarkfen 					WARN1(ep, ebuf, gettext(
13724235Smarkfen 					    "Multiple IPv6 source addresses "
13734235Smarkfen 					    "for %s of the same scope, using "
13744235Smarkfen 					    "unspecified source instead.\n"),
13754235Smarkfen 					    srchp->h_name);
13764235Smarkfen 				} else {
13774235Smarkfen 					/*
13784235Smarkfen 					 * If I reach here w/o hitting the
13794235Smarkfen 					 * previous if statements, I have a
13804235Smarkfen 					 * single source address for this
13814235Smarkfen 					 * destination.
13824235Smarkfen 					 */
13834235Smarkfen 					bcopy(first_match, &sin6->sin6_addr,
13844235Smarkfen 					    sizeof (struct in6_addr));
13854235Smarkfen 				}
13864235Smarkfen 			}
13874235Smarkfen 		}
13884235Smarkfen 
13894235Smarkfen 		/*
13904235Smarkfen 		 * If there are errors at this point there is no
13914235Smarkfen 		 * point sending anything to PF_KEY.
13924235Smarkfen 		 */
13934235Smarkfen 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
13944235Smarkfen 
13954235Smarkfen 		/* Save off a copy for later writing... */
13964235Smarkfen 		msgp = (struct sadb_msg *)buffer;
13974235Smarkfen 		bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
13984235Smarkfen 
13994235Smarkfen 		rc = key_write(keysock, buffer, SADB_64TO8(msgp->sadb_msg_len));
14004235Smarkfen 		if (rc == -1)
14014235Smarkfen 			Bail("write() to PF_KEY socket (in doaddresses)");
14024235Smarkfen 
14034235Smarkfen 		/* Blank the key for paranoia's sake. */
14044235Smarkfen 		bzero(buffer, buffer_size);
14054235Smarkfen 		time_critical_enter();
14064235Smarkfen 		do {
14074235Smarkfen 			rc = read(keysock, buffer, buffer_size);
14084235Smarkfen 			if (rc == -1)
14094235Smarkfen 				Bail("read (in doaddresses)");
14104235Smarkfen 		} while (msgp->sadb_msg_seq != seq ||
14114235Smarkfen 		    msgp->sadb_msg_pid != mypid);
14124235Smarkfen 		time_critical_exit();
14134235Smarkfen 
14144235Smarkfen 		/*
14154235Smarkfen 		 * I should _never_ hit the following unless:
14164235Smarkfen 		 *
14174235Smarkfen 		 * 1. There is a kernel bug.
14184235Smarkfen 		 * 2. Another process is mistakenly using my pid in a PF_KEY
14194235Smarkfen 		 *    message.
14204235Smarkfen 		 */
14214235Smarkfen 		if (msgp->sadb_msg_type != sadb_msg_type ||
14224235Smarkfen 		    msgp->sadb_msg_satype != sadb_msg_satype) {
14234235Smarkfen 			syslog((LOG_NOTICE|LOG_AUTH), gettext(
14244235Smarkfen 			    "doaddresses: Unexpected returned message "
14254235Smarkfen 			    "(%d exp %d)\n"), msgp->sadb_msg_type,
14264235Smarkfen 			    sadb_msg_type);
14274235Smarkfen 			Bail("doaddresses: Unexpected returned message");
14284235Smarkfen 		}
14294235Smarkfen 
14304235Smarkfen 		if (msgp->sadb_msg_errno != 0) {
14314235Smarkfen 			char addrprint[INET6_ADDRSTRLEN];
14324235Smarkfen 			int on_errno = 0;
14334235Smarkfen 			char *on_errno_msg;
14344235Smarkfen 
14354235Smarkfen 			/*
14364235Smarkfen 			 * Print different error messages depending
14374235Smarkfen 			 * on the SADB message type being processed.
14384235Smarkfen 			 * If we get a ESRCH error for a GET/DELETE
14394235Smarkfen 			 * messages, we report that the SA does not
14404235Smarkfen 			 * exist. If we get a EEXIST error for a
14414235Smarkfen 			 * ADD/UPDATE message, we report that the
14424235Smarkfen 			 * SA already exists.
14434235Smarkfen 			 */
14444235Smarkfen 			if (sadb_msg_type == SADB_GET ||
14454235Smarkfen 			    sadb_msg_type == SADB_DELETE) {
14464235Smarkfen 				on_errno = ESRCH;
14474235Smarkfen 				on_errno_msg = "does not exist";
14484235Smarkfen 			} else if (sadb_msg_type == SADB_ADD ||
14494235Smarkfen 			    sadb_msg_type == SADB_UPDATE) {
14504235Smarkfen 				on_errno = EEXIST;
14514235Smarkfen 				on_errno_msg = "already exists";
14524235Smarkfen 			}
14534235Smarkfen 
14544235Smarkfen 			errno = msgp->sadb_msg_errno;
14554235Smarkfen 			if (errno == on_errno) {
14564235Smarkfen 				ERROR2(ep, ebuf, gettext(
14574235Smarkfen 				    "Association (type = %s) "
14584235Smarkfen 				    "with spi 0x%x and addr\n"),
14594235Smarkfen 				    rparsesatype(msgp->sadb_msg_satype),
14604235Smarkfen 				    ntohl(spi));
14614235Smarkfen 				ERROR2(ep, ebuf, "%s %s.\n",
14624235Smarkfen 				    do_inet_ntop(dsthp->h_addr_list[i],
14634342Spwernau 				    addrprint, sizeof (addrprint)),
14644235Smarkfen 				    on_errno_msg);
14654235Smarkfen 				msgp = (struct sadb_msg *)savebuf;
14664235Smarkfen 				bcopy(savebuf, buffer,
14674235Smarkfen 				    SADB_64TO8(msgp->sadb_msg_len));
14684235Smarkfen 				continue;
14694235Smarkfen 			} else {
14704235Smarkfen 				if (errno == EINVAL) {
14714235Smarkfen 					ERROR2(ep, ebuf, gettext(
14724235Smarkfen 					    "PF_KEY Diagnostic code %u: %s.\n"),
14734235Smarkfen 					    msgp->sadb_x_msg_diagnostic,
14744235Smarkfen 					    keysock_diag(
14754235Smarkfen 					    msgp->sadb_x_msg_diagnostic));
14764235Smarkfen 				} else {
14774235Smarkfen 					Bail("return message (in doaddresses)");
14784235Smarkfen 				}
14794235Smarkfen 			}
14804235Smarkfen 		}
14814235Smarkfen 
14824235Smarkfen 		if (cmd == CMD_GET) {
14834235Smarkfen 			if (msgp->sadb_msg_len > MAX_GET_SIZE) {
14844235Smarkfen 				WARN1(ep, ebuf, gettext("WARNING:  "
14854235Smarkfen 				    "SA information bigger than %d bytes.\n"),
14864235Smarkfen 				    SADB_64TO8(MAX_GET_SIZE));
14874235Smarkfen 			}
1488*4867Spwernau 			print_samsg(stdout, buffer, B_FALSE, vflag, nflag);
14894235Smarkfen 		}
14904235Smarkfen 
14914235Smarkfen 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
14924235Smarkfen 
14934235Smarkfen 		/* ...and then restore the saved buffer. */
14944235Smarkfen 		msgp = (struct sadb_msg *)savebuf;
14954235Smarkfen 		bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
14964235Smarkfen 		lines_added++;
14974235Smarkfen 	}
14984235Smarkfen 
14994235Smarkfen 	/* Degenerate case, h_addr_list[0] == NULL. */
15004235Smarkfen 	if (i == 0)
15014235Smarkfen 		Bail("Empty destination address list");
15024235Smarkfen 
15034235Smarkfen 	/*
15044235Smarkfen 	 * free(ebuf) even if there are no errors.
15054235Smarkfen 	 * handle_errors() won't return here.
15064235Smarkfen 	 */
15074235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_TRUE);
15084235Smarkfen }
15094235Smarkfen 
15104235Smarkfen /*
15114235Smarkfen  * Perform an add or an update.  ADD and UPDATE are similar in the extensions
15124235Smarkfen  * they need.
15134235Smarkfen  */
15144235Smarkfen static void
15154235Smarkfen doaddup(int cmd, int satype, char *argv[], char *ebuf)
15164235Smarkfen {
15174235Smarkfen 	uint64_t *buffer, *nexthdr;
15184235Smarkfen 	struct sadb_msg msg;
15194235Smarkfen 	struct sadb_sa *assoc = NULL;
15204235Smarkfen 	struct sadb_address *src = NULL, *dst = NULL;
15214235Smarkfen 	struct sadb_address *isrc = NULL, *idst = NULL;
15224235Smarkfen 	struct sadb_address *natt_local = NULL, *natt_remote = NULL;
15234235Smarkfen 	struct sadb_key *encrypt = NULL, *auth = NULL;
15244235Smarkfen 	struct sadb_ident *srcid = NULL, *dstid = NULL;
15254235Smarkfen 	struct sadb_lifetime *hard = NULL, *soft = NULL;  /* Current? */
15264235Smarkfen 	struct sockaddr_in6 *sin6;
15274235Smarkfen 	/* MLS TODO:  Need sensitivity eventually. */
15284235Smarkfen 	int next, token, sa_len, alloclen, totallen = sizeof (msg), prefix;
15294235Smarkfen 	uint32_t spi;
15304235Smarkfen 	char *thiscmd, *pstr;
15314235Smarkfen 	boolean_t readstate = B_FALSE, unspec_src = B_FALSE;
15324235Smarkfen 	boolean_t alloc_inner = B_FALSE, use_natt = B_FALSE;
15334235Smarkfen 	struct hostent *srchp = NULL, *dsthp = NULL, *isrchp = NULL,
15344235Smarkfen 	    *idsthp = NULL;
15354235Smarkfen 	struct hostent *natt_lhp = NULL, *natt_rhp = NULL;
15364235Smarkfen 	uint16_t srcport = 0, dstport = 0, natt_lport = 0, natt_rport = 0,
15374235Smarkfen 	    isrcport = 0, idstport = 0;
15384235Smarkfen 	uint8_t proto = 0, iproto = 0;
15394235Smarkfen 	char *ep = NULL;
15404235Smarkfen 
15414235Smarkfen 	thiscmd = (cmd == CMD_ADD) ? "add" : "update";
15424235Smarkfen 
15434235Smarkfen 	msg_init(&msg, ((cmd == CMD_ADD) ? SADB_ADD : SADB_UPDATE),
15444235Smarkfen 	    (uint8_t)satype);
15454235Smarkfen 
15464235Smarkfen 	/* Assume last element in argv is set to NULL. */
15474235Smarkfen 	do {
15484235Smarkfen 		token = parseextval(*argv, &next);
15494235Smarkfen 		argv++;
15504235Smarkfen 		switch (token) {
15514235Smarkfen 		case TOK_EOF:
15524235Smarkfen 			/* Do nothing, I'm done. */
15534235Smarkfen 			break;
15544235Smarkfen 		case TOK_UNKNOWN:
15554235Smarkfen 			ERROR1(ep, ebuf, gettext(
15564235Smarkfen 			    "Unknown extension field \"%s\" \n"), *(argv - 1));
15574235Smarkfen 			break;
15584235Smarkfen 		case TOK_SPI:
15594235Smarkfen 		case TOK_REPLAY:
15604235Smarkfen 		case TOK_STATE:
15614235Smarkfen 		case TOK_AUTHALG:
15624235Smarkfen 		case TOK_ENCRALG:
15634235Smarkfen 		case TOK_ENCAP:
15644235Smarkfen 			/*
15654235Smarkfen 			 * May want to place this chunk of code in a function.
15664235Smarkfen 			 *
15674235Smarkfen 			 * This code checks for duplicate entries on a command
15684235Smarkfen 			 * line.
15694235Smarkfen 			 */
15704235Smarkfen 
15714235Smarkfen 			/* Allocate the SADB_EXT_SA extension. */
15724235Smarkfen 			if (assoc == NULL) {
15734235Smarkfen 				assoc = malloc(sizeof (*assoc));
15744235Smarkfen 				if (assoc == NULL)
15754235Smarkfen 					Bail("malloc(assoc)");
15764235Smarkfen 				bzero(assoc, sizeof (*assoc));
15774235Smarkfen 				assoc->sadb_sa_exttype = SADB_EXT_SA;
15784235Smarkfen 				assoc->sadb_sa_len =
15794235Smarkfen 				    SADB_8TO64(sizeof (*assoc));
15804235Smarkfen 				totallen += sizeof (*assoc);
15814235Smarkfen 			}
15824235Smarkfen 			switch (token) {
15834235Smarkfen 			case TOK_SPI:
15844235Smarkfen 				/*
15854235Smarkfen 				 * If some cretin types in "spi 0" then he/she
15864235Smarkfen 				 * can type in another SPI.
15874235Smarkfen 				 */
15884235Smarkfen 				if (assoc->sadb_sa_spi != 0) {
15894235Smarkfen 					ERROR(ep, ebuf, gettext(
15904235Smarkfen 					    "Can only specify "
15914235Smarkfen 					    "single SPI value.\n"));
15924235Smarkfen 					break;
15934235Smarkfen 				}
15944235Smarkfen 				/* Must convert SPI to network order! */
15954235Smarkfen 				assoc->sadb_sa_spi =
15964235Smarkfen 				    htonl((uint32_t)parsenum(*argv, B_TRUE,
15974235Smarkfen 				    ebuf));
15984235Smarkfen 				if (assoc->sadb_sa_spi == 0) {
15994235Smarkfen 					ERROR(ep, ebuf, gettext(
16004235Smarkfen 					    "Invalid SPI value \"0\" .\n"));
16014235Smarkfen 				}
16024235Smarkfen 				break;
16034235Smarkfen 			case TOK_REPLAY:
16044235Smarkfen 				/*
16054235Smarkfen 				 * That same cretin can do the same with
16064235Smarkfen 				 * replay.
16074235Smarkfen 				 */
16084235Smarkfen 				if (assoc->sadb_sa_replay != 0) {
16094235Smarkfen 					ERROR(ep, ebuf, gettext(
16104235Smarkfen 					    "Can only specify "
16114235Smarkfen 					    "single replay window size.\n"));
16124235Smarkfen 					break;
16134235Smarkfen 				}
16144235Smarkfen 				assoc->sadb_sa_replay =
16154235Smarkfen 				    (uint8_t)parsenum(*argv, B_TRUE, ebuf);
16164235Smarkfen 				if (assoc->sadb_sa_replay != 0) {
16174235Smarkfen 					WARN(ep, ebuf, gettext(
16184235Smarkfen 					    "WARNING:  Replay with manual"
16194235Smarkfen 					    " keying considered harmful.\n"));
16204235Smarkfen 				}
16214235Smarkfen 				break;
16224235Smarkfen 			case TOK_STATE:
16234235Smarkfen 				/*
16244235Smarkfen 				 * 0 is an actual state value, LARVAL.  This
16254235Smarkfen 				 * means that one can type in the larval state
16264235Smarkfen 				 * and then type in another state on the same
16274235Smarkfen 				 * command line.
16284235Smarkfen 				 */
16294235Smarkfen 				if (assoc->sadb_sa_state != 0) {
16304235Smarkfen 					ERROR(ep, ebuf, gettext(
16314235Smarkfen 					    "Can only specify "
16324235Smarkfen 					    "single SA state.\n"));
16334235Smarkfen 					break;
16344235Smarkfen 				}
16354235Smarkfen 				assoc->sadb_sa_state = parsestate(*argv,
16364235Smarkfen 				    ebuf);
16374235Smarkfen 				readstate = B_TRUE;
16384235Smarkfen 				break;
16394235Smarkfen 			case TOK_AUTHALG:
16404235Smarkfen 				if (assoc->sadb_sa_auth != 0) {
16414235Smarkfen 					ERROR(ep, ebuf, gettext(
16424235Smarkfen 					    "Can only specify "
16434235Smarkfen 					    "single auth algorithm.\n"));
16444235Smarkfen 					break;
16454235Smarkfen 				}
16464235Smarkfen 				assoc->sadb_sa_auth = parsealg(*argv,
16474235Smarkfen 				    IPSEC_PROTO_AH, ebuf);
16484235Smarkfen 				break;
16494235Smarkfen 			case TOK_ENCRALG:
16504235Smarkfen 				if (satype == SADB_SATYPE_AH) {
16514235Smarkfen 					ERROR(ep, ebuf, gettext("Cannot specify"
16524235Smarkfen 					    " encryption with SA type ah.\n"));
16534235Smarkfen 					break;
16544235Smarkfen 				}
16554235Smarkfen 				if (assoc->sadb_sa_encrypt != 0) {
16564235Smarkfen 					ERROR(ep, ebuf, gettext(
16574235Smarkfen 					    "Can only specify "
16584235Smarkfen 					    "single encryption algorithm.\n"));
16594235Smarkfen 					break;
16604235Smarkfen 				}
16614235Smarkfen 				assoc->sadb_sa_encrypt = parsealg(*argv,
16624235Smarkfen 				    IPSEC_PROTO_ESP, ebuf);
16634235Smarkfen 				break;
16644235Smarkfen 			case TOK_ENCAP:
16654235Smarkfen 				if (use_natt) {
16664235Smarkfen 					ERROR(ep, ebuf, gettext(
16674235Smarkfen 					    "Can only specify single"
16684235Smarkfen 					    " encapsulation.\n"));
16694235Smarkfen 					break;
16704235Smarkfen 				}
16714235Smarkfen 				if (strncmp(*argv, "udp", 3)) {
16724235Smarkfen 					ERROR(ep, ebuf, gettext(
16734235Smarkfen 					    "Can only specify udp"
16744235Smarkfen 					    " encapsulation.\n"));
16754235Smarkfen 					break;
16764235Smarkfen 				}
16774235Smarkfen 				use_natt = B_TRUE;
16784235Smarkfen 				/* set assoc flags later */
16794235Smarkfen 				break;
16804235Smarkfen 			}
16814235Smarkfen 			argv++;
16824235Smarkfen 			break;
16834235Smarkfen 		case TOK_SRCPORT:
16844235Smarkfen 			if (srcport != 0) {
16854235Smarkfen 				ERROR(ep, ebuf,  gettext("Can only specify "
16864235Smarkfen 				    "single source port.\n"));
16874235Smarkfen 				break;
16884235Smarkfen 			}
16894235Smarkfen 			srcport = parsenum(*argv, B_TRUE, ebuf);
16904235Smarkfen 			argv++;
16914235Smarkfen 			break;
16924235Smarkfen 		case TOK_DSTPORT:
16934235Smarkfen 			if (dstport != 0) {
16944235Smarkfen 				ERROR(ep, ebuf, gettext("Can only specify "
16954235Smarkfen 				    "single destination port.\n"));
16964235Smarkfen 				break;
16974235Smarkfen 			}
16984235Smarkfen 			dstport = parsenum(*argv, B_TRUE, ebuf);
16994235Smarkfen 			argv++;
17004235Smarkfen 			break;
17014235Smarkfen 		case TOK_ISRCPORT:
17024235Smarkfen 			alloc_inner = B_TRUE;
17034235Smarkfen 			if (isrcport != 0) {
17044235Smarkfen 				ERROR(ep, ebuf, gettext(
17054235Smarkfen 				    "Can only specify "
17064235Smarkfen 				    "single inner-source port.\n"));
17074235Smarkfen 				break;
17084235Smarkfen 			}
17094235Smarkfen 			isrcport = parsenum(*argv, B_TRUE, ebuf);
17104235Smarkfen 			argv++;
17114235Smarkfen 			break;
17124235Smarkfen 		case TOK_IDSTPORT:
17134235Smarkfen 			alloc_inner = B_TRUE;
17144235Smarkfen 			if (idstport != 0) {
17154235Smarkfen 				ERROR(ep, ebuf, gettext(
17164235Smarkfen 				    "Can only specify "
17174235Smarkfen 				    "single inner-destination port.\n"));
17184235Smarkfen 				break;
17194235Smarkfen 			}
17204235Smarkfen 			idstport = parsenum(*argv, B_TRUE, ebuf);
17214235Smarkfen 			argv++;
17224235Smarkfen 			break;
17234235Smarkfen 		case TOK_NATLPORT:
17244235Smarkfen 			if (natt_lport != 0) {
17254235Smarkfen 				ERROR(ep, ebuf, gettext(
17264235Smarkfen 				    "Can only specify "
17274235Smarkfen 				    "single NAT-T local port.\n"));
17284235Smarkfen 				break;
17294235Smarkfen 			}
17304235Smarkfen 
17314235Smarkfen 			if (natt_rport != 0) {
17324235Smarkfen 				ERROR(ep, ebuf, gettext(
17334235Smarkfen 				    "Can only specify "
17344235Smarkfen 				    "one of NAT-T remote and local port.\n"));
17354235Smarkfen 				break;
17364235Smarkfen 			}
17374235Smarkfen 			natt_lport = parsenum(*argv, B_TRUE, ebuf);
17384235Smarkfen 			argv++;
17394235Smarkfen 			break;
17404235Smarkfen 		case TOK_NATRPORT:
17414235Smarkfen 			if (natt_rport != 0) {
17424235Smarkfen 				ERROR(ep, ebuf, gettext(
17434235Smarkfen 				    "Can only specify "
17444235Smarkfen 				    "single NAT-T remote port.\n"));
17454235Smarkfen 				break;
17464235Smarkfen 			}
17474235Smarkfen 
17484235Smarkfen 			if (natt_lport != 0) {
17494235Smarkfen 				ERROR(ep, ebuf, gettext(
17504235Smarkfen 				    "Can only specify "
17514235Smarkfen 				    "one of NAT-T remote and local port.\n"));
17524235Smarkfen 				break;
17534235Smarkfen 			}
17544235Smarkfen 			natt_rport = parsenum(*argv, B_TRUE, ebuf);
17554235Smarkfen 			argv++;
17564235Smarkfen 			break;
17574235Smarkfen 
17584235Smarkfen 		case TOK_PROTO:
17594235Smarkfen 			if (proto != 0) {
17604235Smarkfen 				ERROR(ep, ebuf, gettext(
17614235Smarkfen 				    "Can only specify "
17624235Smarkfen 				    "single protocol.\n"));
17634235Smarkfen 				break;
17644235Smarkfen 			}
17654235Smarkfen 			proto = parsenum(*argv, B_TRUE, ebuf);
17664235Smarkfen 			argv++;
17674235Smarkfen 			break;
17684235Smarkfen 		case TOK_IPROTO:
17694235Smarkfen 			alloc_inner = B_TRUE;
17704235Smarkfen 			if (iproto != 0) {
17714235Smarkfen 				ERROR(ep, ebuf, gettext(
17724235Smarkfen 				    "Can only specify "
17734235Smarkfen 				    "single inner protocol.\n"));
17744235Smarkfen 				break;
17754235Smarkfen 			}
17764235Smarkfen 			iproto = parsenum(*argv, B_TRUE, ebuf);
17774235Smarkfen 			argv++;
17784235Smarkfen 			break;
17794235Smarkfen 		case TOK_SRCADDR:
17804235Smarkfen 		case TOK_SRCADDR6:
17814235Smarkfen 			if (src != NULL) {
17824235Smarkfen 				ERROR(ep, ebuf, gettext(
17834235Smarkfen 				    "Can only specify "
17844235Smarkfen 				    "single source address.\n"));
17854235Smarkfen 				break;
17864235Smarkfen 			}
17874235Smarkfen 			sa_len = parseaddr(*argv, &srchp,
17884235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
17894235Smarkfen 			if (srchp == NULL) {
17904235Smarkfen 				ERROR1(ep, ebuf, gettext(
17914235Smarkfen 				    "Unknown src address \"%s\"\n"), *argv);
17924235Smarkfen 				break;
17934235Smarkfen 			}
17944235Smarkfen 			argv++;
17954235Smarkfen 			/*
17964235Smarkfen 			 * Round of the sockaddr length to an 8 byte
17974235Smarkfen 			 * boundary to make PF_KEY happy.
17984235Smarkfen 			 */
17994235Smarkfen 			alloclen = sizeof (*src) + roundup(sa_len, 8);
18004235Smarkfen 			src = malloc(alloclen);
18014235Smarkfen 			if (src == NULL)
18024235Smarkfen 				Bail("malloc(src)");
18034235Smarkfen 			totallen += alloclen;
18044235Smarkfen 			src->sadb_address_len = SADB_8TO64(alloclen);
18054235Smarkfen 			src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
18064235Smarkfen 			src->sadb_address_reserved = 0;
18074235Smarkfen 			src->sadb_address_prefixlen = 0;
18084235Smarkfen 			src->sadb_address_proto = 0;
18094235Smarkfen 			if (srchp == &dummy.he) {
18104235Smarkfen 				/*
18114235Smarkfen 				 * Single address with -n flag.
18124235Smarkfen 				 */
18134235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
18144235Smarkfen 				bzero(sin6, sizeof (*sin6));
18154235Smarkfen 				sin6->sin6_family = AF_INET6;
18164235Smarkfen 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
18174235Smarkfen 				    sizeof (struct in6_addr));
18184235Smarkfen 			}
18194235Smarkfen 			break;
18204235Smarkfen 		case TOK_DSTADDR:
18214235Smarkfen 		case TOK_DSTADDR6:
18224235Smarkfen 			if (dst != NULL) {
18234235Smarkfen 				ERROR(ep, ebuf, gettext(
18244235Smarkfen 				    "Can only specify single "
18254235Smarkfen 				    "destination address.\n"));
18264235Smarkfen 				break;
18274235Smarkfen 			}
18284235Smarkfen 			sa_len = parseaddr(*argv, &dsthp,
18294235Smarkfen 			    (token == TOK_DSTADDR6), ebuf);
18304235Smarkfen 			if (dsthp == NULL) {
18314235Smarkfen 				ERROR1(ep, ebuf, gettext(
18324235Smarkfen 				    "Unknown dst address \"%s\"\n"), *argv);
18334235Smarkfen 				break;
18344235Smarkfen 			}
18354235Smarkfen 			argv++;
18364235Smarkfen 			alloclen = sizeof (*dst) + roundup(sa_len, 8);
18374235Smarkfen 			dst = malloc(alloclen);
18384235Smarkfen 			if (dst == NULL)
18394235Smarkfen 				Bail("malloc(dst)");
18404235Smarkfen 			totallen += alloclen;
18414235Smarkfen 			dst->sadb_address_len = SADB_8TO64(alloclen);
18424235Smarkfen 			dst->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
18434235Smarkfen 			dst->sadb_address_reserved = 0;
18444235Smarkfen 			dst->sadb_address_prefixlen = 0;
18454235Smarkfen 			dst->sadb_address_proto = 0;
18464235Smarkfen 			if (dsthp == &dummy.he) {
18474235Smarkfen 				/*
18484235Smarkfen 				 * Single address with -n flag.
18494235Smarkfen 				 */
18504235Smarkfen 				sin6 = (struct sockaddr_in6 *)(dst + 1);
18514235Smarkfen 				bzero(sin6, sizeof (*sin6));
18524235Smarkfen 				sin6->sin6_family = AF_INET6;
18534235Smarkfen 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
18544235Smarkfen 				    sizeof (struct in6_addr));
18554235Smarkfen 			}
18564235Smarkfen 			break;
18574235Smarkfen 		case TOK_PROXYADDR:
18584235Smarkfen 		case TOK_PROXYADDR6:
18594235Smarkfen 			if (isrc != NULL) {
18604235Smarkfen 				ERROR(ep, ebuf, gettext(
18614235Smarkfen 				    "Can only specify single "
18624235Smarkfen 				    "proxy/inner-source address.\n"));
18634235Smarkfen 				break;
18644235Smarkfen 			}
18654235Smarkfen 			if ((pstr = strchr(*argv, '/')) != NULL) {
18664235Smarkfen 				/* Parse out the prefix. */
18674235Smarkfen 				errno = 0;
18684235Smarkfen 				prefix = strtol(pstr + 1, NULL, 10);
18694235Smarkfen 				if (errno != 0) {
18704235Smarkfen 					ERROR1(ep, ebuf, gettext(
18714235Smarkfen 					    "Invalid prefix %s."), pstr);
18724235Smarkfen 					break;
18734235Smarkfen 				}
18744235Smarkfen 				/* Recycle pstr */
18754235Smarkfen 				alloclen = (int)(pstr - *argv);
18764235Smarkfen 				pstr = malloc(alloclen + 1);
18774235Smarkfen 				if (pstr == NULL) {
18784235Smarkfen 					Bail("malloc(pstr)");
18794235Smarkfen 				}
18804235Smarkfen 				(void) strlcpy(pstr, *argv, alloclen + 1);
18814235Smarkfen 			} else {
18824235Smarkfen 				pstr = *argv;
18834235Smarkfen 				/*
18844235Smarkfen 				 * Assume mapping to AF_INET6, and we're a host.
18854235Smarkfen 				 * XXX some miscreants may still make classful
18864235Smarkfen 				 * assumptions.  If this is a problem, fix it
18874235Smarkfen 				 * here.
18884235Smarkfen 				 */
18894235Smarkfen 				prefix = 128;
18904235Smarkfen 			}
18914235Smarkfen 			sa_len = parseaddr(pstr, &isrchp,
18924235Smarkfen 			    (token == TOK_PROXYADDR6), ebuf);
18934235Smarkfen 			if (isrchp == NULL) {
18944235Smarkfen 				ERROR1(ep, ebuf, gettext(
18954235Smarkfen 				    "Unknown proxy/inner-source address "
18964235Smarkfen 				    "\"%s\"\n"), *argv);
18974235Smarkfen 				break;
18984235Smarkfen 			}
18994235Smarkfen 			if (pstr != *argv)
19004235Smarkfen 				free(pstr);
19014235Smarkfen 			argv++;
19024235Smarkfen 			alloclen = sizeof (*isrc) + roundup(sa_len, 8);
19034235Smarkfen 			isrc = malloc(alloclen);
19044235Smarkfen 			if (isrc == NULL)
19054235Smarkfen 				Bail("malloc(isrc)");
19064235Smarkfen 			totallen += alloclen;
19074235Smarkfen 			isrc->sadb_address_len = SADB_8TO64(alloclen);
19084235Smarkfen 			isrc->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
19094235Smarkfen 			isrc->sadb_address_reserved = 0;
19104235Smarkfen 			isrc->sadb_address_prefixlen = prefix;
19114235Smarkfen 			isrc->sadb_address_proto = 0;
19124235Smarkfen 			if (isrchp == &dummy.he ||
19134235Smarkfen 			    isrchp->h_addr_list[1] == NULL) {
19144235Smarkfen 				/*
19154235Smarkfen 				 * Single address with -n flag or single name.
19164235Smarkfen 				 */
19174235Smarkfen 				sin6 = (struct sockaddr_in6 *)(isrc + 1);
19184235Smarkfen 				bzero(sin6, sizeof (*sin6));
19194235Smarkfen 				sin6->sin6_family = AF_INET6;
19204235Smarkfen 				bcopy(isrchp->h_addr_list[0], &sin6->sin6_addr,
19214235Smarkfen 				    sizeof (struct in6_addr));
19224235Smarkfen 				/*
19234235Smarkfen 				 * normalize prefixlen for IPv4-mapped
19244235Smarkfen 				 * addresses.
19254235Smarkfen 				 */
19264235Smarkfen 				if (prefix <= 32 &&
19274235Smarkfen 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
19284235Smarkfen 					isrc->sadb_address_prefixlen += 96;
19294235Smarkfen 				alloc_inner = B_TRUE;
19304235Smarkfen 			} else {
19314235Smarkfen 				/*
19324235Smarkfen 				 * If the proxy/isrc address is vague, don't
19334235Smarkfen 				 * bother.
19344235Smarkfen 				 */
19354235Smarkfen 				totallen -= alloclen;
19364235Smarkfen 				free(isrc);
19374235Smarkfen 				isrc = NULL;
19384235Smarkfen 				WARN1(ep, ebuf, gettext(
19394235Smarkfen 				    "Proxy/inner-source address %s "
19404235Smarkfen 				    "is vague, not using.\n"), isrchp->h_name);
19414235Smarkfen 				freehostent(isrchp);
19424235Smarkfen 				isrchp = NULL;
19434235Smarkfen 				break;
19444235Smarkfen 			}
19454235Smarkfen 			break;
19464235Smarkfen 		case TOK_IDSTADDR:
19474235Smarkfen 		case TOK_IDSTADDR6:
19484235Smarkfen 			if (idst != NULL) {
19494235Smarkfen 				ERROR(ep, ebuf, gettext(
19504235Smarkfen 				    "Can only specify single "
19514235Smarkfen 				    "inner-destination address.\n"));
19524235Smarkfen 				break;
19534235Smarkfen 			}
19544235Smarkfen 			if ((pstr = strchr(*argv, '/')) != NULL) {
19554235Smarkfen 				/* Parse out the prefix. */
19564235Smarkfen 				errno = 0;
19574235Smarkfen 				prefix = strtol(pstr + 1, NULL, 10);
19584235Smarkfen 				if (errno != 0) {
19594235Smarkfen 					ERROR1(ep, ebuf, gettext(
19604235Smarkfen 					    "Invalid prefix %s.\n"), pstr);
19614235Smarkfen 					break;
19624235Smarkfen 				}
19634235Smarkfen 				/* Recycle pstr */
19644235Smarkfen 				alloclen = (int)(pstr - *argv);
19654235Smarkfen 				pstr = malloc(alloclen + 1);
19664235Smarkfen 				if (pstr == NULL) {
19674235Smarkfen 					Bail("malloc(pstr)");
19684235Smarkfen 				}
19694235Smarkfen 				(void) strlcpy(pstr, *argv, alloclen + 1);
19704235Smarkfen 			} else {
19714235Smarkfen 				pstr = *argv;
19724235Smarkfen 				/*
19734235Smarkfen 				 * Assume mapping to AF_INET6, and we're a host.
19744235Smarkfen 				 * XXX some miscreants may still make classful
19754235Smarkfen 				 * assumptions.  If this is a problem, fix it
19764235Smarkfen 				 * here.
19774235Smarkfen 				 */
19784235Smarkfen 				prefix = 128;
19794235Smarkfen 			}
19804235Smarkfen 			sa_len = parseaddr(pstr, &idsthp,
19814235Smarkfen 			    (token == TOK_IDSTADDR6), ebuf);
19824235Smarkfen 			if (idsthp == NULL) {
19834235Smarkfen 				ERROR1(ep, ebuf, gettext(
19844235Smarkfen 				    "Unknown Inner Src address "
19854235Smarkfen 				    " \"%s\"\n"), *argv);
19864235Smarkfen 				break;
19874235Smarkfen 			}
19884235Smarkfen 			if (pstr != *argv)
19894235Smarkfen 				free(pstr);
19904235Smarkfen 			argv++;
19914235Smarkfen 			alloclen = sizeof (*idst) + roundup(sa_len, 8);
19924235Smarkfen 			idst = malloc(alloclen);
19934235Smarkfen 			if (idst == NULL)
19944235Smarkfen 				Bail("malloc(idst)");
19954235Smarkfen 			totallen += alloclen;
19964235Smarkfen 			idst->sadb_address_len = SADB_8TO64(alloclen);
19974235Smarkfen 			idst->sadb_address_exttype =
19984235Smarkfen 			    SADB_X_EXT_ADDRESS_INNER_DST;
19994235Smarkfen 			idst->sadb_address_reserved = 0;
20004235Smarkfen 			idst->sadb_address_prefixlen = prefix;
20014235Smarkfen 			idst->sadb_address_proto = 0;
20024235Smarkfen 			if (idsthp == &dummy.he ||
20034235Smarkfen 			    idsthp->h_addr_list[1] == NULL) {
20044235Smarkfen 				/*
20054235Smarkfen 				 * Single address with -n flag or single name.
20064235Smarkfen 				 */
20074235Smarkfen 				sin6 = (struct sockaddr_in6 *)(idst + 1);
20084235Smarkfen 				bzero(sin6, sizeof (*sin6));
20094235Smarkfen 				sin6->sin6_family = AF_INET6;
20104235Smarkfen 				bcopy(idsthp->h_addr_list[0], &sin6->sin6_addr,
20114235Smarkfen 				    sizeof (struct in6_addr));
20124235Smarkfen 				/*
20134235Smarkfen 				 * normalize prefixlen for IPv4-mapped
20144235Smarkfen 				 * addresses.
20154235Smarkfen 				 */
20164235Smarkfen 				if (prefix <= 32 &&
20174235Smarkfen 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
20184235Smarkfen 					idst->sadb_address_prefixlen += 96;
20194235Smarkfen 				alloc_inner = B_TRUE;
20204235Smarkfen 			} else {
20214235Smarkfen 				/*
20224235Smarkfen 				 * If the idst address is vague, don't bother.
20234235Smarkfen 				 */
20244235Smarkfen 				totallen -= alloclen;
20254235Smarkfen 				free(idst);
20264235Smarkfen 				idst = NULL;
20274235Smarkfen 				WARN1(ep, ebuf, gettext(
20284235Smarkfen 				    "Inner destination address %s "
20294235Smarkfen 				    "is vague, not using.\n"), idsthp->h_name);
20304235Smarkfen 				freehostent(idsthp);
20314235Smarkfen 				idsthp = NULL;
20324235Smarkfen 				break;
20334235Smarkfen 			}
20344235Smarkfen 			break;
20354235Smarkfen 		case TOK_NATLOC:
20364235Smarkfen 			if (natt_local != NULL) {
20374235Smarkfen 				ERROR(ep, ebuf, gettext(
20384235Smarkfen 				    "Can only specify "
20394235Smarkfen 				    "single NAT-T local address.\n"));
20404235Smarkfen 				break;
20414235Smarkfen 			}
20424235Smarkfen 			sa_len = parseaddr(*argv, &natt_lhp, 0, ebuf);
20434235Smarkfen 			if (natt_lhp == NULL) {
20444235Smarkfen 				ERROR1(ep, ebuf, gettext(
20454235Smarkfen 				    "Unknown NAT-T local address \"%s\"\n"),
20464235Smarkfen 				    *argv);
20474235Smarkfen 				break;
20484235Smarkfen 			}
20494235Smarkfen 			argv++;
20504235Smarkfen 			/*
20514235Smarkfen 			 * Round of the sockaddr length to an 8 byte
20524235Smarkfen 			 * boundary to make PF_KEY happy.
20534235Smarkfen 			 */
20544235Smarkfen 			alloclen = sizeof (*natt_local) + roundup(sa_len, 8);
20554235Smarkfen 			natt_local = malloc(alloclen);
20564235Smarkfen 			if (natt_local == NULL)
20574235Smarkfen 				Bail("malloc(natt_local)");
20584235Smarkfen 			totallen += alloclen;
20594235Smarkfen 			natt_local->sadb_address_len = SADB_8TO64(alloclen);
20604235Smarkfen 			natt_local->sadb_address_exttype =
20614235Smarkfen 			    SADB_X_EXT_ADDRESS_NATT_LOC;
20624235Smarkfen 			natt_local->sadb_address_reserved = 0;
20634235Smarkfen 			natt_local->sadb_address_prefixlen = 0;
20644235Smarkfen 			natt_local->sadb_address_proto = 0;
20654235Smarkfen 			if (natt_lhp == &dummy.he ||
20664235Smarkfen 			    natt_lhp->h_addr_list[1] == NULL) {
20674235Smarkfen 				/*
20684235Smarkfen 				 * Single address with -n flag or single name.
20694235Smarkfen 				 */
20704235Smarkfen 				sin6 = (struct sockaddr_in6 *)(natt_local + 1);
20714235Smarkfen 				bzero(sin6, sizeof (*sin6));
20724235Smarkfen 				sin6->sin6_family = AF_INET6;
20734235Smarkfen 				bcopy(natt_lhp->h_addr_list[0],
20744235Smarkfen 				    &sin6->sin6_addr, sizeof (struct in6_addr));
20754235Smarkfen 			} else {
20764235Smarkfen 				/*
20774235Smarkfen 				 * If the nat-local address is vague, don't
20784235Smarkfen 				 * bother.
20794235Smarkfen 				 */
20804235Smarkfen 				totallen -= alloclen;
20814235Smarkfen 				free(natt_local);
20824235Smarkfen 				natt_local = NULL;
20834235Smarkfen 				WARN1(ep, ebuf, gettext(
20844235Smarkfen 				    "NAT-T local address %s "
20854235Smarkfen 				    "is vague, not using.\n"),
20864235Smarkfen 				    natt_lhp->h_name);
20874235Smarkfen 				freehostent(natt_lhp);
20884235Smarkfen 				natt_lhp = NULL;
20894235Smarkfen 				break;
20904235Smarkfen 			}
20914235Smarkfen 			break;
20924235Smarkfen 		case TOK_NATREM:
20934235Smarkfen 			if (natt_remote != NULL) {
20944235Smarkfen 				ERROR(ep, ebuf, gettext(
20954235Smarkfen 				    "Can only specify "
20964235Smarkfen 				    "single NAT-T remote address.\n"));
20974235Smarkfen 				break;
20984235Smarkfen 			}
20994235Smarkfen 			sa_len = parseaddr(*argv, &natt_rhp, 0, ebuf);
21004235Smarkfen 			if (natt_rhp == NULL) {
21014235Smarkfen 				ERROR1(ep, ebuf, gettext(
21024235Smarkfen 				    "Unknown NAT-T remote address \"%s\"\n"),
21034235Smarkfen 				    *argv);
21044235Smarkfen 				break;
21054235Smarkfen 			}
21064235Smarkfen 			argv++;
21074235Smarkfen 			/*
21084235Smarkfen 			 * Round of the sockaddr length to an 8 byte
21094235Smarkfen 			 * boundary to make PF_KEY happy.
21104235Smarkfen 			 */
21114235Smarkfen 			alloclen = sizeof (*natt_remote) + roundup(sa_len, 8);
21124235Smarkfen 			natt_remote = malloc(alloclen);
21134235Smarkfen 			if (natt_remote == NULL)
21144235Smarkfen 				Bail("malloc(natt_remote)");
21154235Smarkfen 			totallen += alloclen;
21164235Smarkfen 			natt_remote->sadb_address_len = SADB_8TO64(alloclen);
21174235Smarkfen 			natt_remote->sadb_address_exttype =
21184235Smarkfen 			    SADB_X_EXT_ADDRESS_NATT_REM;
21194235Smarkfen 			natt_remote->sadb_address_reserved = 0;
21204235Smarkfen 			natt_remote->sadb_address_prefixlen = 0;
21214235Smarkfen 			natt_remote->sadb_address_proto = 0;
21224235Smarkfen 			if (natt_rhp == &dummy.he ||
21234235Smarkfen 			    natt_rhp->h_addr_list[1] == NULL) {
21244235Smarkfen 				/*
21254235Smarkfen 				 * Single address with -n flag or single name.
21264235Smarkfen 				 */
21274235Smarkfen 				sin6 = (struct sockaddr_in6 *)(natt_remote + 1);
21284235Smarkfen 				bzero(sin6, sizeof (*sin6));
21294235Smarkfen 				sin6->sin6_family = AF_INET6;
21304235Smarkfen 				bcopy(natt_rhp->h_addr_list[0],
21314235Smarkfen 				    &sin6->sin6_addr, sizeof (struct in6_addr));
21324235Smarkfen 			} else {
21334235Smarkfen 				/*
21344235Smarkfen 				 * If the nat-renote address is vague, don't
21354235Smarkfen 				 * bother.
21364235Smarkfen 				 */
21374235Smarkfen 				totallen -= alloclen;
21384235Smarkfen 				free(natt_remote);
21394235Smarkfen 				natt_remote = NULL;
21404235Smarkfen 				WARN1(ep, ebuf, gettext(
21414235Smarkfen 				    "NAT-T remote address %s "
21424235Smarkfen 				    "is vague, not using.\n"),
21434235Smarkfen 				    natt_rhp->h_name);
21444235Smarkfen 				freehostent(natt_rhp);
21454235Smarkfen 				natt_rhp = NULL;
21464235Smarkfen 				break;
21474235Smarkfen 			}
21484235Smarkfen 			break;
21494235Smarkfen 		case TOK_ENCRKEY:
21504235Smarkfen 			if (encrypt != NULL) {
21514235Smarkfen 				ERROR(ep, ebuf, gettext(
21524235Smarkfen 				    "Can only specify "
21534235Smarkfen 				    "single encryption key.\n"));
21544235Smarkfen 				break;
21554235Smarkfen 			}
21564573Spwernau 			if (assoc->sadb_sa_encrypt == SADB_EALG_NULL) {
21574573Spwernau 				FATAL(ep, ebuf, gettext(
21584573Spwernau 				    "Cannot specify a key with NULL "
21594573Spwernau 				    "encryption algorithm.\n"));
21604573Spwernau 				break;
21614573Spwernau 			}
21624235Smarkfen 			encrypt = parsekey(*argv, ebuf);
21634235Smarkfen 			argv++;
21644235Smarkfen 			if (encrypt == NULL) {
21654235Smarkfen 				ERROR(ep, ebuf, gettext(
21664235Smarkfen 				    "Invalid encryption key.\n"));
21674235Smarkfen 				break;
21684235Smarkfen 			}
21694235Smarkfen 			totallen += SADB_64TO8(encrypt->sadb_key_len);
21704235Smarkfen 			encrypt->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
21714235Smarkfen 			break;
21724235Smarkfen 		case TOK_AUTHKEY:
21734235Smarkfen 			if (auth != NULL) {
21744235Smarkfen 				ERROR(ep, ebuf, gettext(
21754235Smarkfen 				    "Can only specify single"
21764235Smarkfen 				    " authentication key.\n"));
21774235Smarkfen 				break;
21784235Smarkfen 			}
21794235Smarkfen 			auth = parsekey(*argv, ebuf);
21804235Smarkfen 			argv++;
21814235Smarkfen 			if (auth == NULL) {
21824235Smarkfen 				ERROR(ep, ebuf, gettext(
21834235Smarkfen 				    "Invalid authentication key.\n"));
21844235Smarkfen 				break;
21854235Smarkfen 			}
21864235Smarkfen 			totallen += SADB_64TO8(auth->sadb_key_len);
21874235Smarkfen 			auth->sadb_key_exttype = SADB_EXT_KEY_AUTH;
21884235Smarkfen 			break;
21894235Smarkfen 		case TOK_SRCIDTYPE:
21904235Smarkfen 			if (*argv == NULL || *(argv + 1) == NULL) {
21914235Smarkfen 				FATAL(ep, ebuf, gettext(
21924235Smarkfen 				    "Unexpected end of command "
21934235Smarkfen 				    "line - Expecting Src Type.\n"));
21944235Smarkfen 				/* NOTREACHED */
21954235Smarkfen 				break;
21964235Smarkfen 			}
21974235Smarkfen 			if (srcid != NULL) {
21984235Smarkfen 				ERROR(ep, ebuf, gettext(
21994235Smarkfen 				    "Can only specify single"
22004235Smarkfen 				    " source certificate identity.\n"));
22014235Smarkfen 				break;
22024235Smarkfen 			}
22034235Smarkfen 			alloclen = sizeof (*srcid) +
22044235Smarkfen 			    roundup(strlen(*(argv + 1)) + 1, 8);
22054235Smarkfen 			srcid = malloc(alloclen);
22064235Smarkfen 			if (srcid == NULL)
22074235Smarkfen 				Bail("malloc(srcid)");
22084235Smarkfen 			totallen += alloclen;
22094235Smarkfen 			srcid->sadb_ident_type = parseidtype(*argv, ebuf);
22104235Smarkfen 			argv++;
22114235Smarkfen 			srcid->sadb_ident_len = SADB_8TO64(alloclen);
22124235Smarkfen 			srcid->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC;
22134235Smarkfen 			srcid->sadb_ident_reserved = 0;
22144235Smarkfen 			srcid->sadb_ident_id = 0;  /* Not useful here. */
22154235Smarkfen 			(void) strlcpy((char *)(srcid + 1), *argv, alloclen);
22164235Smarkfen 			argv++;
22174235Smarkfen 			break;
22184235Smarkfen 		case TOK_DSTIDTYPE:
22194235Smarkfen 			if (*argv == NULL || *(argv + 1) == NULL) {
22204235Smarkfen 				ERROR(ep, ebuf, gettext(
22214235Smarkfen 				    "Unexpected end of command"
22224235Smarkfen 				    " line - expecting dst type.\n"));
22234235Smarkfen 				break;
22244235Smarkfen 			}
22254235Smarkfen 			if (dstid != NULL) {
22264235Smarkfen 				ERROR(ep, ebuf, gettext(
22274235Smarkfen 				    "Can only specify single destination "
22284342Spwernau 				    "certificate identity.\n"));
22294235Smarkfen 				break;
22304235Smarkfen 			}
22314235Smarkfen 			alloclen = sizeof (*dstid) +
22324235Smarkfen 			    roundup(strlen(*(argv + 1)) + 1, 8);
22334235Smarkfen 			dstid = malloc(alloclen);
22344235Smarkfen 			if (dstid == NULL)
22354235Smarkfen 				Bail("malloc(dstid)");
22364235Smarkfen 			totallen += alloclen;
22374235Smarkfen 			dstid->sadb_ident_type = parseidtype(*argv, ebuf);
22384235Smarkfen 			argv++;
22394235Smarkfen 			dstid->sadb_ident_len = SADB_8TO64(alloclen);
22404235Smarkfen 			dstid->sadb_ident_exttype = SADB_EXT_IDENTITY_DST;
22414235Smarkfen 			dstid->sadb_ident_reserved = 0;
22424235Smarkfen 			dstid->sadb_ident_id = 0;  /* Not useful here. */
22434235Smarkfen 			(void) strlcpy((char *)(dstid + 1), *argv, alloclen);
22444235Smarkfen 			argv++;
22454235Smarkfen 			break;
22464235Smarkfen 		case TOK_HARD_ALLOC:
22474235Smarkfen 		case TOK_HARD_BYTES:
22484235Smarkfen 		case TOK_HARD_ADDTIME:
22494235Smarkfen 		case TOK_HARD_USETIME:
22504235Smarkfen 			if (hard == NULL) {
22514235Smarkfen 				hard = malloc(sizeof (*hard));
22524235Smarkfen 				if (hard == NULL)
22534235Smarkfen 					Bail("malloc(hard_lifetime)");
22544235Smarkfen 				bzero(hard, sizeof (*hard));
22554235Smarkfen 				hard->sadb_lifetime_exttype =
22564235Smarkfen 				    SADB_EXT_LIFETIME_HARD;
22574235Smarkfen 				hard->sadb_lifetime_len =
22584235Smarkfen 				    SADB_8TO64(sizeof (*hard));
22594235Smarkfen 				totallen += sizeof (*hard);
22604235Smarkfen 			}
22614235Smarkfen 			switch (token) {
22624235Smarkfen 			case TOK_HARD_ALLOC:
22634235Smarkfen 				if (hard->sadb_lifetime_allocations != 0) {
22644235Smarkfen 					ERROR(ep, ebuf, gettext(
22654235Smarkfen 					    "Can only specify single"
22664235Smarkfen 					    " hard allocation limit.\n"));
22674235Smarkfen 					break;
22684235Smarkfen 				}
22694235Smarkfen 				hard->sadb_lifetime_allocations =
22704235Smarkfen 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
22714235Smarkfen 				break;
22724235Smarkfen 			case TOK_HARD_BYTES:
22734235Smarkfen 				if (hard->sadb_lifetime_bytes != 0) {
22744235Smarkfen 					ERROR(ep, ebuf, gettext(
22754235Smarkfen 					    "Can only specify "
22764235Smarkfen 					    "single hard byte limit.\n"));
22774235Smarkfen 					break;
22784235Smarkfen 				}
22794235Smarkfen 				hard->sadb_lifetime_bytes = parsenum(*argv,
22804235Smarkfen 				    B_TRUE, ebuf);
22814235Smarkfen 				break;
22824235Smarkfen 			case TOK_HARD_ADDTIME:
22834235Smarkfen 				if (hard->sadb_lifetime_addtime != 0) {
22844235Smarkfen 					ERROR(ep, ebuf, gettext(
22854235Smarkfen 					    "Can only specify "
22864235Smarkfen 					    "single past-add lifetime.\n"));
22874235Smarkfen 					break;
22884235Smarkfen 				}
22894235Smarkfen 				hard->sadb_lifetime_addtime = parsenum(*argv,
22904235Smarkfen 				    B_TRUE, ebuf);
22914235Smarkfen 				break;
22924235Smarkfen 			case TOK_HARD_USETIME:
22934235Smarkfen 				if (hard->sadb_lifetime_usetime != 0) {
22944235Smarkfen 					ERROR(ep, ebuf, gettext(
22954235Smarkfen 					    "Can only specify "
22964235Smarkfen 					    "single past-use lifetime.\n"));
22974235Smarkfen 					break;
22984235Smarkfen 				}
22994235Smarkfen 				hard->sadb_lifetime_usetime = parsenum(*argv,
23004235Smarkfen 				    B_TRUE, ebuf);
23014235Smarkfen 				break;
23024235Smarkfen 			}
23034235Smarkfen 			argv++;
23044235Smarkfen 			break;
23054235Smarkfen 		case TOK_SOFT_ALLOC:
23064235Smarkfen 		case TOK_SOFT_BYTES:
23074235Smarkfen 		case TOK_SOFT_ADDTIME:
23084235Smarkfen 		case TOK_SOFT_USETIME:
23094235Smarkfen 			if (soft == NULL) {
23104235Smarkfen 				soft = malloc(sizeof (*soft));
23114235Smarkfen 				if (soft == NULL)
23124235Smarkfen 					Bail("malloc(soft_lifetime)");
23134235Smarkfen 				bzero(soft, sizeof (*soft));
23144235Smarkfen 				soft->sadb_lifetime_exttype =
23154235Smarkfen 				    SADB_EXT_LIFETIME_SOFT;
23164235Smarkfen 				soft->sadb_lifetime_len =
23174235Smarkfen 				    SADB_8TO64(sizeof (*soft));
23184235Smarkfen 				totallen += sizeof (*soft);
23194235Smarkfen 			}
23204235Smarkfen 			switch (token) {
23214235Smarkfen 			case TOK_SOFT_ALLOC:
23224235Smarkfen 				if (soft->sadb_lifetime_allocations != 0) {
23234235Smarkfen 					ERROR(ep, ebuf, gettext(
23244235Smarkfen 					    "Can only specify single"
23254235Smarkfen 					    " soft allocation limit.\n"));
23264235Smarkfen 					break;
23274235Smarkfen 				}
23284235Smarkfen 				soft->sadb_lifetime_allocations =
23294235Smarkfen 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
23304235Smarkfen 				break;
23314235Smarkfen 			case TOK_SOFT_BYTES:
23324235Smarkfen 				if (soft->sadb_lifetime_bytes != 0) {
23334235Smarkfen 					ERROR(ep, ebuf, gettext(
23344235Smarkfen 					    "Can only specify single"
23354235Smarkfen 					    " soft byte limit.\n"));
23364235Smarkfen 					break;
23374235Smarkfen 				}
23384235Smarkfen 				soft->sadb_lifetime_bytes = parsenum(*argv,
23394235Smarkfen 				    B_TRUE, ebuf);
23404235Smarkfen 				break;
23414235Smarkfen 			case TOK_SOFT_ADDTIME:
23424235Smarkfen 				if (soft->sadb_lifetime_addtime != 0) {
23434235Smarkfen 					ERROR(ep, ebuf, gettext(
23444235Smarkfen 					    "Can only specify single"
23454235Smarkfen 					    " past-add lifetime.\n"));
23464235Smarkfen 					break;
23474235Smarkfen 				}
23484235Smarkfen 				soft->sadb_lifetime_addtime = parsenum(*argv,
23494235Smarkfen 				    B_TRUE, ebuf);
23504235Smarkfen 				break;
23514235Smarkfen 			case TOK_SOFT_USETIME:
23524235Smarkfen 				if (soft->sadb_lifetime_usetime != 0) {
23534235Smarkfen 					ERROR(ep, ebuf, gettext(
23544235Smarkfen 					    "Can only specify single"
23554235Smarkfen 					    " past-use lifetime.\n"));
23564235Smarkfen 					break;
23574235Smarkfen 				}
23584235Smarkfen 				soft->sadb_lifetime_usetime = parsenum(*argv,
23594235Smarkfen 				    B_TRUE, ebuf);
23604235Smarkfen 				break;
23614235Smarkfen 			}
23624235Smarkfen 			argv++;
23634235Smarkfen 			break;
23644235Smarkfen 		default:
23654235Smarkfen 			ERROR1(ep, ebuf, gettext(
23664235Smarkfen 			    "Don't use extension %s for add/update.\n"),
23674235Smarkfen 			    *(argv - 1));
23684235Smarkfen 			break;
23694235Smarkfen 		}
23704235Smarkfen 	} while (token != TOK_EOF);
23714235Smarkfen 
23724235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
23734235Smarkfen 
23744235Smarkfen 	/*
23754235Smarkfen 	 * If we specify inner ports w/o addresses, we still need to
23764235Smarkfen 	 * allocate.  Also, if we have one inner address, we need the
23774235Smarkfen 	 * other, even if we don't specify anything.
23784235Smarkfen 	 */
23794235Smarkfen 	if (alloc_inner && idst == NULL) {
23804235Smarkfen 		/* Allocate zeroed-out. */
23814235Smarkfen 		alloclen = sizeof (*idst) + sizeof (struct sockaddr_in6);
23824235Smarkfen 		idst = calloc(1, alloclen);
23834235Smarkfen 		if (idst == NULL) {
23844235Smarkfen 			Bail("malloc(implicit idst)");
23854235Smarkfen 		}
23864235Smarkfen 		totallen += alloclen;
23874235Smarkfen 		idst->sadb_address_len = SADB_8TO64(alloclen);
23884235Smarkfen 		idst->sadb_address_exttype = SADB_X_EXT_ADDRESS_INNER_DST;
23894235Smarkfen 		sin6 = (struct sockaddr_in6 *)(idst + 1);
23904235Smarkfen 		sin6->sin6_family = AF_INET6;
23914235Smarkfen 	}
23924235Smarkfen 
23934235Smarkfen 	if (alloc_inner && isrc == NULL) {
23944235Smarkfen 		/* Allocate zeroed-out. */
23954235Smarkfen 		alloclen = sizeof (*isrc) + sizeof (struct sockaddr_in6);
23964235Smarkfen 		isrc = calloc(1, alloclen);
23974235Smarkfen 		if (isrc == NULL) {
23984235Smarkfen 			Bail("malloc(implicit isrc)");
23994235Smarkfen 		}
24004235Smarkfen 		totallen += alloclen;
24014235Smarkfen 		isrc->sadb_address_len = SADB_8TO64(alloclen);
24024235Smarkfen 		isrc->sadb_address_exttype = SADB_X_EXT_ADDRESS_INNER_SRC;
24034235Smarkfen 		sin6 = (struct sockaddr_in6 *)(isrc + 1);
24044235Smarkfen 		sin6->sin6_family = AF_INET6;
24054235Smarkfen 	}
24064235Smarkfen 
24074235Smarkfen 	/*
24084235Smarkfen 	 * Okay, so now I have all of the potential extensions!
24094235Smarkfen 	 * Allocate a single contiguous buffer.  Keep in mind that it'll
24104235Smarkfen 	 * be enough because the key itself will be yanked.
24114235Smarkfen 	 */
24124235Smarkfen 
24134235Smarkfen 	if (src == NULL && dst != NULL) {
24144235Smarkfen 		/*
24154235Smarkfen 		 * Set explicit unspecified source address.
24164235Smarkfen 		 */
24174235Smarkfen 		size_t lenbytes = SADB_64TO8(dst->sadb_address_len);
24184235Smarkfen 
24194235Smarkfen 		unspec_src = B_TRUE;
24204235Smarkfen 		totallen += lenbytes;
24214235Smarkfen 		src = malloc(lenbytes);
24224235Smarkfen 		if (src == NULL)
24234235Smarkfen 			Bail("malloc(implicit src)");
24244235Smarkfen 		/* Confusing, but we're copying from DST to SRC.  :) */
24254235Smarkfen 		bcopy(dst, src, lenbytes);
24264235Smarkfen 		src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
24274235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
24284235Smarkfen 		bzero(sin6, sizeof (*sin6));
24294235Smarkfen 		sin6->sin6_family = AF_INET6;
24304235Smarkfen 	}
24314235Smarkfen 
24324235Smarkfen 	msg.sadb_msg_len = SADB_8TO64(totallen);
24334235Smarkfen 
24344235Smarkfen 	buffer = malloc(totallen);
24354235Smarkfen 	nexthdr = buffer;
24364235Smarkfen 	bcopy(&msg, nexthdr, sizeof (msg));
24374235Smarkfen 	nexthdr += SADB_8TO64(sizeof (msg));
24384235Smarkfen 	if (assoc != NULL) {
24394235Smarkfen 		if (assoc->sadb_sa_spi == 0) {
24404235Smarkfen 			ERROR1(ep, ebuf, gettext(
24414235Smarkfen 			    "The SPI value is missing for "
24424235Smarkfen 			    "the association you wish to %s.\n"), thiscmd);
24434235Smarkfen 		}
24444235Smarkfen 		if (assoc->sadb_sa_auth == 0 && assoc->sadb_sa_encrypt == 0 &&
24454342Spwernau 		    cmd == CMD_ADD) {
24464235Smarkfen 			free(assoc);
24474235Smarkfen 			FATAL(ep, ebuf, gettext(
24484235Smarkfen 			    "Select at least one algorithm "
24494235Smarkfen 			    "for this add.\n"));
24504235Smarkfen 		}
24514235Smarkfen 
24524235Smarkfen 		/* Hack to let user specify NULL ESP implicitly. */
24534235Smarkfen 		if (msg.sadb_msg_satype == SADB_SATYPE_ESP &&
24544235Smarkfen 		    assoc->sadb_sa_encrypt == 0)
24554235Smarkfen 			assoc->sadb_sa_encrypt = SADB_EALG_NULL;
24564235Smarkfen 
24574235Smarkfen 		/* 0 is an actual value.  Print a warning if it was entered. */
24584235Smarkfen 		if (assoc->sadb_sa_state == 0) {
24594235Smarkfen 			if (readstate) {
24604235Smarkfen 				ERROR(ep, ebuf, gettext(
24614235Smarkfen 				    "WARNING: Cannot set LARVAL SA state.\n"));
24624235Smarkfen 			}
24634235Smarkfen 			assoc->sadb_sa_state = SADB_SASTATE_MATURE;
24644235Smarkfen 		}
24654235Smarkfen 
24664235Smarkfen 		if (use_natt) {
24674235Smarkfen 			if (natt_remote != NULL)
24684235Smarkfen 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_REM;
24694235Smarkfen 			if (natt_local != NULL)
24704235Smarkfen 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_LOC;
24714235Smarkfen 		}
24724235Smarkfen 
24734235Smarkfen 		if (alloc_inner) {
24744235Smarkfen 			/*
24754235Smarkfen 			 * For now, assume RFC 3884's dream of transport-mode
24764235Smarkfen 			 * SAs with inner IP address selectors will not
24774235Smarkfen 			 * happen.
24784235Smarkfen 			 */
24794235Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL;
24804235Smarkfen 			if (proto != 0 && proto != IPPROTO_ENCAP &&
24814235Smarkfen 			    proto != IPPROTO_IPV6) {
24824235Smarkfen 				ERROR1(ep, ebuf, gettext(
24834235Smarkfen 				    "WARNING: Protocol type %d not "
24844235Smarkfen 				    "for use with Tunnel-Mode SA.\n"), proto);
24854235Smarkfen 				/* Continue and let PF_KEY scream... */
24864235Smarkfen 			}
24874235Smarkfen 		}
24884235Smarkfen 
24894235Smarkfen 		bcopy(assoc, nexthdr, SADB_64TO8(assoc->sadb_sa_len));
24904235Smarkfen 		nexthdr += assoc->sadb_sa_len;
24914235Smarkfen 		/* Save the SPI for the case of an error. */
24924235Smarkfen 		spi = assoc->sadb_sa_spi;
24934235Smarkfen 		free(assoc);
24944235Smarkfen 	} else {
24954235Smarkfen 		ERROR1(ep, ebuf, gettext(
24964235Smarkfen 		    "Need SA parameters for %s.\n"), thiscmd);
24974235Smarkfen 	}
24984235Smarkfen 
24994235Smarkfen 	if (hard != NULL) {
25004235Smarkfen 		bcopy(hard, nexthdr, SADB_64TO8(hard->sadb_lifetime_len));
25014235Smarkfen 		nexthdr += hard->sadb_lifetime_len;
25024235Smarkfen 		free(hard);
25034235Smarkfen 	}
25044235Smarkfen 
25054235Smarkfen 	if (soft != NULL) {
25064235Smarkfen 		bcopy(soft, nexthdr, SADB_64TO8(soft->sadb_lifetime_len));
25074235Smarkfen 		nexthdr += soft->sadb_lifetime_len;
25084235Smarkfen 		free(soft);
25094235Smarkfen 	}
25104235Smarkfen 
25114235Smarkfen 	if (encrypt == NULL && auth == NULL && cmd == CMD_ADD) {
25124235Smarkfen 		ERROR(ep, ebuf, gettext(
25134235Smarkfen 		    "Must have at least one key for an add.\n"));
25144235Smarkfen 	}
25154235Smarkfen 
25164235Smarkfen 	if (encrypt != NULL) {
25174235Smarkfen 		bcopy(encrypt, nexthdr, SADB_64TO8(encrypt->sadb_key_len));
25184235Smarkfen 		nexthdr += encrypt->sadb_key_len;
25194235Smarkfen 		bzero(encrypt, SADB_64TO8(encrypt->sadb_key_len));
25204235Smarkfen 		free(encrypt);
25214235Smarkfen 	}
25224235Smarkfen 
25234235Smarkfen 	if (auth != NULL) {
25244235Smarkfen 		bcopy(auth, nexthdr, SADB_64TO8(auth->sadb_key_len));
25254235Smarkfen 		nexthdr += auth->sadb_key_len;
25264235Smarkfen 		bzero(auth, SADB_64TO8(auth->sadb_key_len));
25274235Smarkfen 		free(auth);
25284235Smarkfen 	}
25294235Smarkfen 
25304235Smarkfen 	if (srcid != NULL) {
25314235Smarkfen 		bcopy(srcid, nexthdr, SADB_64TO8(srcid->sadb_ident_len));
25324235Smarkfen 		nexthdr += srcid->sadb_ident_len;
25334235Smarkfen 		free(srcid);
25344235Smarkfen 	}
25354235Smarkfen 
25364235Smarkfen 	if (dstid != NULL) {
25374235Smarkfen 		bcopy(dstid, nexthdr, SADB_64TO8(dstid->sadb_ident_len));
25384235Smarkfen 		nexthdr += dstid->sadb_ident_len;
25394235Smarkfen 		free(dstid);
25404235Smarkfen 	}
25414235Smarkfen 
25424235Smarkfen 	if (dst != NULL) {
25434235Smarkfen 		bcopy(dst, nexthdr, SADB_64TO8(dst->sadb_address_len));
25444235Smarkfen 		free(dst);
25454235Smarkfen 		dst = (struct sadb_address *)nexthdr;
25464235Smarkfen 		dst->sadb_address_proto = proto;
25474235Smarkfen 		((struct sockaddr_in6 *)(dst + 1))->sin6_port = htons(dstport);
25484235Smarkfen 		nexthdr += dst->sadb_address_len;
25494235Smarkfen 	} else {
25504235Smarkfen 		FATAL1(ep, ebuf, gettext(
25514235Smarkfen 		    "Need destination address for %s.\n"), thiscmd);
25524235Smarkfen 	}
25534235Smarkfen 
25544235Smarkfen 	if (use_natt) {
25554235Smarkfen 		if (natt_remote == NULL && natt_local == NULL) {
25564235Smarkfen 			ERROR(ep, ebuf, gettext(
25574235Smarkfen 			    "Must specify NAT-T remote or local address "
25584235Smarkfen 			    "for UDP encapsulation.\n"));
25594235Smarkfen 		}
25604235Smarkfen 
25614235Smarkfen 		if (natt_lport != 0 && natt_local == NULL) {
25624235Smarkfen 			ERROR(ep, ebuf, gettext(
25634235Smarkfen 			    "If NAT-T local port is specified, NAT-T "
25644235Smarkfen 			    "local address must also be specified.\n"));
25654235Smarkfen 		}
25664235Smarkfen 
25674235Smarkfen 		if (natt_rport != 0 && natt_remote == NULL) {
25684235Smarkfen 			ERROR(ep, ebuf, gettext(
25694235Smarkfen 			    "If NAT-T remote port is specified, NAT-T "
25704235Smarkfen 			    "remote address must also be specified.\n"));
25714235Smarkfen 		}
25724235Smarkfen 
25734235Smarkfen 		if (natt_remote != NULL) {
25744235Smarkfen 			bcopy(natt_remote, nexthdr,
25754235Smarkfen 			    SADB_64TO8(natt_remote->sadb_address_len));
25764235Smarkfen 			free(natt_remote);
25774235Smarkfen 			natt_remote = (struct sadb_address *)nexthdr;
25784235Smarkfen 			nexthdr += natt_remote->sadb_address_len;
25794235Smarkfen 			((struct sockaddr_in6 *)(natt_remote + 1))->sin6_port =
25804235Smarkfen 			    htons(natt_rport);
25814235Smarkfen 		}
25824235Smarkfen 
25834235Smarkfen 		if (natt_local != NULL) {
25844235Smarkfen 			bcopy(natt_local, nexthdr,
25854235Smarkfen 			    SADB_64TO8(natt_local->sadb_address_len));
25864235Smarkfen 			free(natt_local);
25874235Smarkfen 			natt_local = (struct sadb_address *)nexthdr;
25884235Smarkfen 			nexthdr += natt_local->sadb_address_len;
25894235Smarkfen 			((struct sockaddr_in6 *)(natt_local + 1))->sin6_port =
25904235Smarkfen 			    htons(natt_lport);
25914235Smarkfen 		}
25924235Smarkfen 	}
25934235Smarkfen 
25944235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
25954235Smarkfen 
25964235Smarkfen 	/*
25974235Smarkfen 	 * PF_KEY requires a source address extension, even if the source
25984235Smarkfen 	 * address itself is unspecified. (See "Set explicit unspecified..."
25994235Smarkfen 	 * code fragment above. Destination reality check was above.)
26004235Smarkfen 	 */
26014235Smarkfen 	bcopy(src, nexthdr, SADB_64TO8(src->sadb_address_len));
26024235Smarkfen 	free(src);
26034235Smarkfen 	src = (struct sadb_address *)nexthdr;
26044235Smarkfen 	src->sadb_address_proto = proto;
26054235Smarkfen 	((struct sockaddr_in6 *)(src + 1))->sin6_port = htons(srcport);
26064235Smarkfen 	nexthdr += src->sadb_address_len;
26074235Smarkfen 
26084235Smarkfen 	if (isrc != NULL) {
26094235Smarkfen 		bcopy(isrc, nexthdr, SADB_64TO8(isrc->sadb_address_len));
26104235Smarkfen 		free(isrc);
26114235Smarkfen 		isrc = (struct sadb_address *)nexthdr;
26124235Smarkfen 		isrc->sadb_address_proto = iproto;
26134235Smarkfen 		((struct sockaddr_in6 *)(isrc + 1))->sin6_port =
26144235Smarkfen 		    htons(isrcport);
26154235Smarkfen 		nexthdr += isrc->sadb_address_len;
26164235Smarkfen 	}
26174235Smarkfen 
26184235Smarkfen 	if (idst != NULL) {
26194235Smarkfen 		bcopy(idst, nexthdr, SADB_64TO8(idst->sadb_address_len));
26204235Smarkfen 		free(idst);
26214235Smarkfen 		idst = (struct sadb_address *)nexthdr;
26224235Smarkfen 		idst->sadb_address_proto = iproto;
26234235Smarkfen 		((struct sockaddr_in6 *)(idst + 1))->sin6_port =
26244235Smarkfen 		    htons(idstport);
26254235Smarkfen 		nexthdr += idst->sadb_address_len;
26264235Smarkfen 	}
26274235Smarkfen 
26284342Spwernau 	if (cflag) {
26294342Spwernau 		/*
26304342Spwernau 		 * Assume the checked cmd would have worked if it was actually
26314342Spwernau 		 * used. doaddresses() will increment lines_added if it
26324342Spwernau 		 * succeeds.
26334342Spwernau 		 */
26344342Spwernau 		lines_added++;
26354342Spwernau 	} else {
26364235Smarkfen 		doaddresses((cmd == CMD_ADD) ? SADB_ADD : SADB_UPDATE, satype,
26374235Smarkfen 		    cmd, srchp, dsthp, src, dst, unspec_src, buffer, totallen,
26384235Smarkfen 		    spi, ebuf);
26394235Smarkfen 	}
26404235Smarkfen 
26414235Smarkfen 	if (isrchp != NULL && isrchp != &dummy.he)
26424342Spwernau 		freehostent(isrchp);
26434235Smarkfen 	if (idsthp != NULL && idsthp != &dummy.he)
26444342Spwernau 		freehostent(idsthp);
26454235Smarkfen 	if (srchp != NULL && srchp != &dummy.he)
26464342Spwernau 		freehostent(srchp);
26474235Smarkfen 	if (dsthp != NULL && dsthp != &dummy.he)
26484342Spwernau 		freehostent(dsthp);
26494235Smarkfen 	if (natt_lhp != NULL && natt_lhp != &dummy.he)
26504342Spwernau 		freehostent(natt_lhp);
26514235Smarkfen 	if (natt_rhp != NULL && natt_rhp != &dummy.he)
26524342Spwernau 		freehostent(natt_rhp);
26534235Smarkfen 
26544235Smarkfen 	free(ebuf);
26554235Smarkfen 	free(buffer);
26564235Smarkfen }
26574235Smarkfen 
26584235Smarkfen /*
26594235Smarkfen  * DELETE and GET are similar, in that they only need the extensions
26604235Smarkfen  * required to _find_ an SA, and then either delete it or obtain its
26614235Smarkfen  * information.
26624235Smarkfen  */
26634235Smarkfen static void
26644235Smarkfen dodelget(int cmd, int satype, char *argv[], char *ebuf)
26654235Smarkfen {
26664235Smarkfen 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
26674235Smarkfen 	uint64_t *nextext;
26684235Smarkfen 	struct sadb_sa *assoc = NULL;
26694235Smarkfen 	struct sadb_address *src = NULL, *dst = NULL;
26704235Smarkfen 	int next, token, sa_len;
26714235Smarkfen 	char *thiscmd;
26724235Smarkfen 	uint32_t spi;
26734235Smarkfen 	struct hostent *srchp = NULL, *dsthp = NULL;
26744235Smarkfen 	struct sockaddr_in6 *sin6;
26754235Smarkfen 	boolean_t unspec_src = B_TRUE;
26764235Smarkfen 	uint16_t srcport = 0, dstport = 0;
26774235Smarkfen 	uint8_t proto = 0;
26784235Smarkfen 	char *ep = NULL;
26794235Smarkfen 
26804235Smarkfen 	msg_init(msg, ((cmd == CMD_GET) ? SADB_GET : SADB_DELETE),
26814235Smarkfen 	    (uint8_t)satype);
26824235Smarkfen 	/* Set the first extension header to right past the base message. */
26834235Smarkfen 	nextext = (uint64_t *)(msg + 1);
26844235Smarkfen 	bzero(nextext, sizeof (get_buffer) - sizeof (*msg));
26854235Smarkfen 
26864235Smarkfen 	thiscmd = (cmd == CMD_GET) ? "get" : "delete";
26874235Smarkfen 
26884235Smarkfen #define	ALLOC_ADDR_EXT(ext, exttype)			\
26894235Smarkfen 	(ext) = (struct sadb_address *)nextext;		\
26904235Smarkfen 	nextext = (uint64_t *)((ext) + 1);		\
26914235Smarkfen 	nextext += SADB_8TO64(roundup(sa_len, 8));	\
26924235Smarkfen 	(ext)->sadb_address_exttype = exttype;		\
26934235Smarkfen 	(ext)->sadb_address_len = nextext - ((uint64_t *)ext);
26944235Smarkfen 
26954235Smarkfen 	/* Assume last element in argv is set to NULL. */
26964235Smarkfen 	do {
26974235Smarkfen 		token = parseextval(*argv, &next);
26984235Smarkfen 		argv++;
26994235Smarkfen 		switch (token) {
27004235Smarkfen 		case TOK_EOF:
27014235Smarkfen 			/* Do nothing, I'm done. */
27024235Smarkfen 			break;
27034235Smarkfen 		case TOK_UNKNOWN:
27044235Smarkfen 			ERROR1(ep, ebuf, gettext(
27054235Smarkfen 			    "Unknown extension field \"%s\"\n"), *(argv - 1));
27064235Smarkfen 			break;
27074235Smarkfen 		case TOK_SPI:
27084235Smarkfen 			if (assoc != NULL) {
27094235Smarkfen 				ERROR(ep, ebuf, gettext(
27104235Smarkfen 				    "Can only specify single SPI value.\n"));
27114235Smarkfen 				break;
27124235Smarkfen 			}
27134235Smarkfen 			assoc = (struct sadb_sa *)nextext;
27144235Smarkfen 			nextext = (uint64_t *)(assoc + 1);
27154235Smarkfen 			assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc));
27164235Smarkfen 			assoc->sadb_sa_exttype = SADB_EXT_SA;
27174235Smarkfen 			assoc->sadb_sa_spi = htonl((uint32_t)parsenum(*argv,
27184235Smarkfen 			    B_TRUE, ebuf));
27194235Smarkfen 			spi = assoc->sadb_sa_spi;
27204235Smarkfen 			argv++;
27214235Smarkfen 			break;
27224235Smarkfen 		case TOK_SRCPORT:
27234235Smarkfen 			if (srcport != 0) {
27244235Smarkfen 				ERROR(ep, ebuf, gettext(
27254235Smarkfen 				    "Can only specify single source port.\n"));
27264235Smarkfen 				break;
27274235Smarkfen 			}
27284235Smarkfen 			srcport = parsenum(*argv, B_TRUE, ebuf);
27294235Smarkfen 			argv++;
27304235Smarkfen 			break;
27314235Smarkfen 		case TOK_DSTPORT:
27324235Smarkfen 			if (dstport != 0) {
27334235Smarkfen 				ERROR(ep, ebuf, gettext(
27344235Smarkfen 				    "Can only "
27354235Smarkfen 				    "specify single destination port.\n"));
27364235Smarkfen 				break;
27374235Smarkfen 			}
27384235Smarkfen 			dstport = parsenum(*argv, B_TRUE, ebuf);
27394235Smarkfen 			argv++;
27404235Smarkfen 			break;
27414235Smarkfen 		case TOK_PROTO:
27424235Smarkfen 			if (proto != 0) {
27434235Smarkfen 				ERROR(ep, ebuf, gettext(
27444235Smarkfen 				    "Can only specify single protocol.\n"));
27454235Smarkfen 				break;
27464235Smarkfen 			}
27474235Smarkfen 			proto = parsenum(*argv, B_TRUE, ebuf);
27484235Smarkfen 			argv++;
27494235Smarkfen 			break;
27504235Smarkfen 		case TOK_SRCADDR:
27514235Smarkfen 		case TOK_SRCADDR6:
27524235Smarkfen 			if (src != NULL) {
27534235Smarkfen 				ERROR(ep, ebuf, gettext(
27544235Smarkfen 				    "Can only specify single source addr.\n"));
27554235Smarkfen 				break;
27564235Smarkfen 			}
27574235Smarkfen 			sa_len = parseaddr(*argv, &srchp,
27584235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
27594235Smarkfen 			if (srchp == NULL) {
27604235Smarkfen 				ERROR1(ep, ebuf, gettext(
27614235Smarkfen 				    "Unknown source address \"%s\"\n"), *argv);
27624235Smarkfen 				break;
27634235Smarkfen 			}
27644235Smarkfen 			argv++;
27654235Smarkfen 
27664235Smarkfen 			unspec_src = B_FALSE;
27674235Smarkfen 
27684235Smarkfen 			ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
27694235Smarkfen 
27704235Smarkfen 			if (srchp == &dummy.he) {
27714235Smarkfen 				/*
27724235Smarkfen 				 * Single address with -n flag.
27734235Smarkfen 				 */
27744235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
27754235Smarkfen 				bzero(sin6, sizeof (*sin6));
27764235Smarkfen 				sin6->sin6_family = AF_INET6;
27774235Smarkfen 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
27784235Smarkfen 				    sizeof (struct in6_addr));
27794235Smarkfen 			}
27804235Smarkfen 			/* The rest is pre-bzeroed for us. */
27814235Smarkfen 			break;
27824235Smarkfen 		case TOK_DSTADDR:
27834235Smarkfen 		case TOK_DSTADDR6:
27844235Smarkfen 			if (dst != NULL) {
27854235Smarkfen 				ERROR(ep, ebuf, gettext(
27864235Smarkfen 				    "Can only specify single destination "
27874235Smarkfen 				    "address.\n"));
27884235Smarkfen 				break;
27894235Smarkfen 			}
27904235Smarkfen 			sa_len = parseaddr(*argv, &dsthp,
27914235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
27924235Smarkfen 			if (dsthp == NULL) {
27934235Smarkfen 				ERROR1(ep, ebuf, gettext(
27944235Smarkfen 				    "Unknown destination address \"%s\"\n"),
27954235Smarkfen 				    *argv);
27964235Smarkfen 				break;
27974235Smarkfen 			}
27984235Smarkfen 			argv++;
27994235Smarkfen 
28004235Smarkfen 			ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
28014235Smarkfen 
28024235Smarkfen 			if (dsthp == &dummy.he) {
28034235Smarkfen 				/*
28044235Smarkfen 				 * Single address with -n flag.
28054235Smarkfen 				 */
28064235Smarkfen 				sin6 = (struct sockaddr_in6 *)(dst + 1);
28074235Smarkfen 				bzero(sin6, sizeof (*sin6));
28084235Smarkfen 				sin6->sin6_family = AF_INET6;
28094235Smarkfen 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
28104235Smarkfen 				    sizeof (struct in6_addr));
28114235Smarkfen 			}
28124235Smarkfen 			/* The rest is pre-bzeroed for us. */
28134235Smarkfen 			break;
28144235Smarkfen 		default:
28154235Smarkfen 			ERROR2(ep, ebuf, gettext(
28164235Smarkfen 			    "Don't use extension %s for '%s' command.\n"),
28174235Smarkfen 			    *(argv - 1), thiscmd);
28184235Smarkfen 			break;
28194235Smarkfen 		}
28204235Smarkfen 	} while (token != TOK_EOF);
28214235Smarkfen 
28224235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
28234235Smarkfen 
28244235Smarkfen 	if ((srcport != 0) && (src == NULL)) {
28254235Smarkfen 		ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
28264235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
28274235Smarkfen 		src->sadb_address_proto = proto;
28284235Smarkfen 		bzero(sin6, sizeof (*sin6));
28294235Smarkfen 		sin6->sin6_family = AF_INET6;
28304235Smarkfen 		sin6->sin6_port = htons(srcport);
28314235Smarkfen 	}
28324235Smarkfen 
28334235Smarkfen 	if ((dstport != 0) && (dst == NULL)) {
28344235Smarkfen 		ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
28354235Smarkfen 		sin6 = (struct sockaddr_in6 *)(dst + 1);
28364235Smarkfen 		src->sadb_address_proto = proto;
28374235Smarkfen 		bzero(sin6, sizeof (*sin6));
28384235Smarkfen 		sin6->sin6_family = AF_INET6;
28394235Smarkfen 		sin6->sin6_port = htons(dstport);
28404235Smarkfen 	}
28414235Smarkfen 
28424235Smarkfen 	/* So I have enough of the message to send it down! */
28434235Smarkfen 	msg->sadb_msg_len = nextext - get_buffer;
28444235Smarkfen 
28454235Smarkfen 	if (assoc == NULL) {
28464235Smarkfen 		FATAL1(ep, ebuf, gettext(
28474235Smarkfen 		    "Need SA parameters for %s.\n"), thiscmd);
28484235Smarkfen 	}
28494235Smarkfen 
28504342Spwernau 	if (cflag) {
28514342Spwernau 		/*
28524342Spwernau 		 * Assume the checked cmd would have worked if it was actually
28534342Spwernau 		 * used. doaddresses() will increment lines_added if it
28544342Spwernau 		 * succeeds.
28554342Spwernau 		 */
28564342Spwernau 		lines_added++;
28574342Spwernau 	} else {
28584235Smarkfen 		doaddresses((cmd == CMD_GET) ? SADB_GET : SADB_DELETE, satype,
28594235Smarkfen 		    cmd, srchp, dsthp, src, dst, unspec_src, get_buffer,
28604235Smarkfen 		    sizeof (get_buffer), spi, NULL);
28614235Smarkfen 	}
28624235Smarkfen 
28634235Smarkfen 	if (srchp != NULL && srchp != &dummy.he)
28644235Smarkfen 		freehostent(srchp);
28654235Smarkfen 	if (dsthp != NULL && dsthp != &dummy.he)
28664235Smarkfen 		freehostent(dsthp);
28674235Smarkfen }
28684235Smarkfen 
28694235Smarkfen /*
28704235Smarkfen  * "ipseckey monitor" should exit very gracefully if ^C is tapped.
28714235Smarkfen  */
28724235Smarkfen static void
28734235Smarkfen monitor_catch(int signal)
28744235Smarkfen {
28754235Smarkfen 	errx(signal, gettext("Bailing on signal %d."), signal);
28764235Smarkfen }
28774235Smarkfen 
28784235Smarkfen /*
28794235Smarkfen  * Loop forever, listening on PF_KEY messages.
28804235Smarkfen  */
28814235Smarkfen static void
28824235Smarkfen domonitor(boolean_t passive)
28834235Smarkfen {
28844235Smarkfen 	struct sadb_msg *samsg;
28854235Smarkfen 	int rc;
28864235Smarkfen 
28874235Smarkfen 	/* Catch ^C. */
28884235Smarkfen 	(void) signal(SIGINT, monitor_catch);
28894235Smarkfen 
28904235Smarkfen 	samsg = (struct sadb_msg *)get_buffer;
28914235Smarkfen 	if (!passive) {
28924235Smarkfen 		(void) printf(gettext("Actively"));
28934235Smarkfen 		msg_init(samsg, SADB_X_PROMISC, 1);	/* Turn ON promisc. */
28944235Smarkfen 		rc = key_write(keysock, samsg, sizeof (*samsg));
28954235Smarkfen 		if (rc == -1)
28964235Smarkfen 			Bail("write (SADB_X_PROMISC)");
28974235Smarkfen 	} else {
28984235Smarkfen 		(void) printf(gettext("Passively"));
28994235Smarkfen 	}
29004235Smarkfen 	(void) printf(gettext(" monitoring the PF_KEY socket.\n"));
29014235Smarkfen 
29024235Smarkfen 	for (; ; ) {
29034235Smarkfen 		/*
29044235Smarkfen 		 * I assume that read() is non-blocking, and will never
29054235Smarkfen 		 * return 0.
29064235Smarkfen 		 */
29074235Smarkfen 		rc = read(keysock, samsg, sizeof (get_buffer));
29084235Smarkfen 		if (rc == -1)
29094235Smarkfen 			Bail("read (in domonitor)");
29104235Smarkfen 		(void) printf(gettext("Read %d bytes.\n"), rc);
29114235Smarkfen 		/*
29124235Smarkfen 		 * Q:  Should I use the same method of printing as GET does?
29134235Smarkfen 		 * A:  For now, yes.
29144235Smarkfen 		 */
2915*4867Spwernau 		print_samsg(stdout, get_buffer, B_TRUE, vflag, nflag);
29164235Smarkfen 		(void) putchar('\n');
29174235Smarkfen 	}
29184235Smarkfen }
29194235Smarkfen 
29204235Smarkfen /*
29214235Smarkfen  * Either mask or unmask all relevant signals.
29224235Smarkfen  */
29234235Smarkfen static void
29244235Smarkfen mask_signals(boolean_t unmask)
29254235Smarkfen {
29264235Smarkfen 	sigset_t set;
29274235Smarkfen 	static sigset_t oset;
29284235Smarkfen 
29294235Smarkfen 	if (unmask) {
29304235Smarkfen 		(void) sigprocmask(SIG_SETMASK, &oset, NULL);
29314235Smarkfen 	} else {
29324235Smarkfen 		(void) sigfillset(&set);
29334235Smarkfen 		(void) sigprocmask(SIG_SETMASK, &set, &oset);
29344235Smarkfen 	}
29354235Smarkfen }
29364235Smarkfen 
29374235Smarkfen /*
29384235Smarkfen  * Assorted functions to print help text.
29394235Smarkfen  */
29404235Smarkfen #define	puts_tr(s) (void) puts(gettext(s))
29414235Smarkfen 
29424235Smarkfen static void
29434235Smarkfen doattrhelp()
29444235Smarkfen {
29454235Smarkfen 	int i;
29464235Smarkfen 
29474235Smarkfen 	puts_tr("\nSA attributes:");
29484235Smarkfen 
29494235Smarkfen 	for (i = 0; tokens[i].string != NULL; i++) {
29504235Smarkfen 		if (i%3 == 0)
29514235Smarkfen 			(void) printf("\n");
29524235Smarkfen 		(void) printf("    %-15.15s", tokens[i].string);
29534235Smarkfen 	}
29544235Smarkfen 	(void) printf("\n");
29554235Smarkfen }
29564235Smarkfen 
29574235Smarkfen static void
29584235Smarkfen dohelpcmd(char *cmds)
29594235Smarkfen {
29604235Smarkfen 	int cmd;
29614235Smarkfen 
29624235Smarkfen 	if (strcmp(cmds, "attr") == 0) {
29634235Smarkfen 		doattrhelp();
29644235Smarkfen 		return;
29654235Smarkfen 	}
29664235Smarkfen 
29674235Smarkfen 	cmd = parsecmd(cmds);
29684235Smarkfen 	switch (cmd) {
29694235Smarkfen 	case CMD_UPDATE:
29704235Smarkfen 		puts_tr("update	 - Update an existing SA");
29714235Smarkfen 		break;
29724235Smarkfen 	case CMD_ADD:
29734235Smarkfen 		puts_tr("add	 - Add a new security association (SA)");
29744235Smarkfen 		break;
29754235Smarkfen 	case CMD_DELETE:
29764235Smarkfen 		puts_tr("delete - Delete an SA");
29774235Smarkfen 		break;
29784235Smarkfen 	case CMD_GET:
29794235Smarkfen 		puts_tr("get - Display an SA");
29804235Smarkfen 		break;
29814235Smarkfen 	case CMD_FLUSH:
29824235Smarkfen 		puts_tr("flush - Delete all SAs");
29834235Smarkfen 		break;
29844235Smarkfen 	case CMD_DUMP:
29854235Smarkfen 		puts_tr("dump - Display all SAs");
29864235Smarkfen 		break;
29874235Smarkfen 	case CMD_MONITOR:
29884235Smarkfen 		puts_tr("monitor - Monitor all PF_KEY reply messages.");
29894235Smarkfen 		break;
29904235Smarkfen 	case CMD_PMONITOR:
29914235Smarkfen 		puts_tr(
29924235Smarkfen "pmonitor, passive_monitor - Monitor PF_KEY messages that");
29934235Smarkfen 		puts_tr(
29944235Smarkfen "                            reply to all PF_KEY sockets.");
29954235Smarkfen 		break;
29964235Smarkfen 
29974235Smarkfen 	case CMD_QUIT:
29984235Smarkfen 		puts_tr("quit, exit - Exit the program");
29994235Smarkfen 		break;
30004235Smarkfen 	case CMD_SAVE:
30014235Smarkfen 		puts_tr("save	    - Saves all SAs to a file");
30024235Smarkfen 		break;
30034235Smarkfen 	case CMD_HELP:
30044235Smarkfen 		puts_tr("help	    - Display list of commands");
30054235Smarkfen 		puts_tr("help <cmd> - Display help for command");
30064235Smarkfen 		puts_tr("help attr  - Display possible SA attributes");
30074235Smarkfen 		break;
30084235Smarkfen 	default:
30094235Smarkfen 		(void) printf(gettext("%s: Unknown command\n"), cmds);
30104235Smarkfen 		break;
30114235Smarkfen 	}
30124235Smarkfen }
30134235Smarkfen 
30144235Smarkfen 
30154235Smarkfen static void
30164235Smarkfen dohelp(char *cmds)
30174235Smarkfen {
30184235Smarkfen 	if (cmds != NULL) {
30194235Smarkfen 		dohelpcmd(cmds);
30204235Smarkfen 		return;
30214235Smarkfen 	}
30224235Smarkfen 	puts_tr("Commands");
30234235Smarkfen 	puts_tr("--------");
30244235Smarkfen 	puts_tr("?, help  - Display this list");
30254235Smarkfen 	puts_tr("help <cmd> - Display help for command");
30264235Smarkfen 	puts_tr("help attr  - Display possible SA attributes");
30274235Smarkfen 	puts_tr("quit, exit - Exit the program");
30284235Smarkfen 	puts_tr("monitor - Monitor all PF_KEY reply messages.");
30294235Smarkfen 	puts_tr("pmonitor, passive_monitor - Monitor PF_KEY messages that");
30304235Smarkfen 	puts_tr("                            reply to all PF_KEY sockets.");
30314235Smarkfen 	puts_tr("");
30324235Smarkfen 	puts_tr("The following commands are of the form:");
30334235Smarkfen 	puts_tr("    <command> {SA type} {attribute value}*");
30344235Smarkfen 	puts_tr("");
30354235Smarkfen 	puts_tr("add (interactive only) - Add a new security association (SA)");
30364235Smarkfen 	puts_tr("update (interactive only) - Update an existing SA");
30374235Smarkfen 	puts_tr("delete - Delete an SA");
30384235Smarkfen 	puts_tr("get - Display an SA");
30394235Smarkfen 	puts_tr("flush - Delete all SAs");
30404235Smarkfen 	puts_tr("dump - Display all SAs");
30414235Smarkfen 	puts_tr("save - Saves all SAs to a file");
30424235Smarkfen }
30434235Smarkfen 
30444235Smarkfen /*
30454235Smarkfen  * "Parse" a command line from argv.
30464235Smarkfen  */
30474235Smarkfen static void
30484342Spwernau parseit(int argc, char *argv[], char *ebuf, boolean_t read_cmdfile)
30494235Smarkfen {
30504235Smarkfen 	int cmd, satype;
30514235Smarkfen 	char *ep = NULL;
30524235Smarkfen 
30534235Smarkfen 	if (argc == 0)
30544235Smarkfen 		return;
30554235Smarkfen 	cmd = parsecmd(*argv++);
30564235Smarkfen 
30574342Spwernau 	/*
30584342Spwernau 	 * Some commands loop forever and should only be run from the command
30594342Spwernau 	 * line, they should never be run from a command file as this may
30604342Spwernau 	 * be used at boot time.
30614342Spwernau 	 */
30624235Smarkfen 	switch (cmd) {
30634235Smarkfen 	case CMD_HELP:
30644342Spwernau 		if (read_cmdfile)
30654342Spwernau 			ERROR(ep, ebuf, gettext("Help not appropriate in "
30664342Spwernau 			    "config file."));
30674342Spwernau 		else
30684342Spwernau 			dohelp(*argv);
30694235Smarkfen 		return;
30704235Smarkfen 	case CMD_MONITOR:
30714342Spwernau 		if (read_cmdfile)
30724342Spwernau 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
30734342Spwernau 			    "config file."));
30744342Spwernau 		else
30754342Spwernau 			domonitor(B_FALSE);
30764235Smarkfen 		break;
30774235Smarkfen 	case CMD_PMONITOR:
30784342Spwernau 		if (read_cmdfile)
30794342Spwernau 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
30804342Spwernau 			    "config file."));
30814342Spwernau 		else
30824342Spwernau 			domonitor(B_TRUE);
30834235Smarkfen 		break;
30844235Smarkfen 	case CMD_QUIT:
30854235Smarkfen 		EXIT_OK(NULL);
30864235Smarkfen 	}
30874235Smarkfen 
30884342Spwernau 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
30894342Spwernau 
30904235Smarkfen 	satype = parsesatype(*argv, ebuf);
30914235Smarkfen 
30924235Smarkfen 	if (satype != SADB_SATYPE_UNSPEC) {
30934235Smarkfen 		argv++;
30944235Smarkfen 	} else {
30954235Smarkfen 		/*
30964235Smarkfen 		 * You must specify either "all" or a specific SA type
30974235Smarkfen 		 * for the "save" command.
30984235Smarkfen 		 */
30994235Smarkfen 		if (cmd == CMD_SAVE)
31004235Smarkfen 			if (*argv == NULL) {
31014235Smarkfen 				FATAL(ep, ebuf, gettext(
31024235Smarkfen 				    "Must specify a specific "
31034235Smarkfen 				    "SA type for save.\n"));
31044235Smarkfen 			} else {
31054235Smarkfen 				argv++;
31064235Smarkfen 			}
31074235Smarkfen 	}
31084235Smarkfen 
31094235Smarkfen 	switch (cmd) {
31104235Smarkfen 	case CMD_FLUSH:
31114342Spwernau 		if (!cflag)
31124342Spwernau 			doflush(satype);
31134342Spwernau 		/*
31144342Spwernau 		 * If this was called because of an entry in a cmd file
31154342Spwernau 		 * then this action needs to be counted to prevent
31164342Spwernau 		 * do_interactive() treating this as an error.
31174342Spwernau 		 */
31184342Spwernau 		lines_added++;
31194235Smarkfen 		break;
31204235Smarkfen 	case CMD_ADD:
31214235Smarkfen 	case CMD_UPDATE:
31224235Smarkfen 		/*
31234235Smarkfen 		 * NOTE: Shouldn't allow ADDs or UPDATEs with keying material
31244235Smarkfen 		 * from the command line.
31254235Smarkfen 		 */
31264235Smarkfen 		if (!interactive) {
31274235Smarkfen 			errx(1, gettext(
31284235Smarkfen 			    "can't do ADD or UPDATE from the command line.\n"));
31294235Smarkfen 		}
31304235Smarkfen 		if (satype == SADB_SATYPE_UNSPEC) {
31314235Smarkfen 			FATAL(ep, ebuf, gettext(
31324235Smarkfen 			    "Must specify a specific SA type."));
31334235Smarkfen 			/* NOTREACHED */
31344235Smarkfen 		}
31354235Smarkfen 		/* Parse for extensions, including keying material. */
31364235Smarkfen 		doaddup(cmd, satype, argv, ebuf);
31374235Smarkfen 		break;
31384235Smarkfen 	case CMD_DELETE:
31394235Smarkfen 	case CMD_GET:
31404235Smarkfen 		if (satype == SADB_SATYPE_UNSPEC) {
31414235Smarkfen 			FATAL(ep, ebuf, gettext(
31424235Smarkfen 			    "Must specify a single SA type."));
31434235Smarkfen 			/* NOTREACHED */
31444235Smarkfen 		}
31454235Smarkfen 		/* Parse for bare minimum to locate an SA. */
31464235Smarkfen 		dodelget(cmd, satype, argv, ebuf);
31474235Smarkfen 		break;
31484235Smarkfen 	case CMD_DUMP:
31494342Spwernau 		if (read_cmdfile)
31504342Spwernau 			ERROR(ep, ebuf, gettext("Dump not appropriate in "
31514342Spwernau 			    "config file."));
31524342Spwernau 		else
31534342Spwernau 			dodump(satype, NULL);
31544235Smarkfen 		break;
31554235Smarkfen 	case CMD_SAVE:
31564342Spwernau 		if (read_cmdfile) {
31574342Spwernau 			ERROR(ep, ebuf, gettext("Save not appropriate in "
31584342Spwernau 			    "config file."));
31594342Spwernau 		} else {
31604342Spwernau 			mask_signals(B_FALSE);	/* Mask signals */
31614342Spwernau 			dodump(satype, opensavefile(argv[0]));
31624342Spwernau 			mask_signals(B_TRUE);	/* Unmask signals */
31634342Spwernau 		}
31644235Smarkfen 		break;
31654235Smarkfen 	default:
31664235Smarkfen 		warnx(gettext("Unknown command (%s).\n"),
31674235Smarkfen 		    *(argv - ((satype == SADB_SATYPE_UNSPEC) ? 1 : 2)));
31684235Smarkfen 		usage();
31694235Smarkfen 	}
31704342Spwernau 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
31714235Smarkfen }
31724235Smarkfen 
31734235Smarkfen int
31744235Smarkfen main(int argc, char *argv[])
31754235Smarkfen {
31764235Smarkfen 	int ch;
31774235Smarkfen 	FILE *infile = stdin, *savefile;
31784235Smarkfen 	boolean_t dosave = B_FALSE, readfile = B_FALSE;
31794235Smarkfen 	char *configfile = NULL;
31804235Smarkfen 
31814235Smarkfen 	(void) setlocale(LC_ALL, "");
31824235Smarkfen #if !defined(TEXT_DOMAIN)
31834235Smarkfen #define	TEXT_DOMAIN "SYS_TEST"
31844235Smarkfen #endif
31854235Smarkfen 	(void) textdomain(TEXT_DOMAIN);
31864235Smarkfen 
31874235Smarkfen 	/*
31884235Smarkfen 	 * Check to see if the command is being run from smf(5).
31894235Smarkfen 	 */
31904235Smarkfen 	my_fmri = getenv("SMF_FMRI");
31914235Smarkfen 
31924235Smarkfen 	openlog("ipseckey", LOG_CONS, LOG_AUTH);
31934235Smarkfen 	if (getuid() != 0) {
31944235Smarkfen 		errx(1, "Insufficient privileges to run ipseckey.");
31954235Smarkfen 	}
31964235Smarkfen 
31974235Smarkfen 	/* umask me to paranoid, I only want to create files read-only */
31984235Smarkfen 	(void) umask((mode_t)00377);
31994235Smarkfen 
32004235Smarkfen 	while ((ch = getopt(argc, argv, "pnvf:s:c:")) != EOF)
32014235Smarkfen 		switch (ch) {
32024235Smarkfen 		case 'p':
32034235Smarkfen 			pflag = B_TRUE;
32044235Smarkfen 			break;
32054235Smarkfen 		case 'n':
32064235Smarkfen 			nflag = B_TRUE;
32074235Smarkfen 			break;
32084235Smarkfen 		case 'v':
32094235Smarkfen 			vflag = B_TRUE;
32104235Smarkfen 			break;
32114235Smarkfen 		case 'c':
32124235Smarkfen 			cflag = B_TRUE;
32134235Smarkfen 			/* FALLTHRU */
32144235Smarkfen 		case 'f':
32154235Smarkfen 			if (dosave)
32164235Smarkfen 				usage();
32174235Smarkfen 			infile = fopen(optarg, "r");
32184235Smarkfen 			if (infile == NULL) {
32194235Smarkfen 				EXIT_BADCONFIG2("Unable to open configuration "
32204235Smarkfen 				    "file: %s\n", optarg);
32214235Smarkfen 			}
32224235Smarkfen 			configfile = strdup(optarg);
32234235Smarkfen 			readfile = B_TRUE;
32244235Smarkfen 			break;
32254235Smarkfen 		case 's':
32264235Smarkfen 			if (readfile)
32274235Smarkfen 				usage();
32284235Smarkfen 			dosave = B_TRUE;
32294235Smarkfen 			savefile = opensavefile(optarg);
32304235Smarkfen 			break;
32314235Smarkfen 		default:
32324235Smarkfen 			usage();
32334235Smarkfen 		}
32344235Smarkfen 
32354235Smarkfen 	argc -= optind;
32364235Smarkfen 	argv += optind;
32374235Smarkfen 
32384235Smarkfen 	mypid = getpid();
32394235Smarkfen 
32404235Smarkfen 	keysock = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
32414235Smarkfen 
32424235Smarkfen 	if (keysock == -1) {
32434235Smarkfen 		if (errno == EPERM) {
32444235Smarkfen 			EXIT_BADPERM("Insufficient privileges to open "
32454235Smarkfen 			    "PF_KEY socket.\n");
32464235Smarkfen 		} else {
32474235Smarkfen 			/* some other reason */
32484235Smarkfen 			EXIT_FATAL("Opening PF_KEY socket");
32494235Smarkfen 		}
32504235Smarkfen 	}
32514235Smarkfen 
32524235Smarkfen 	if (dosave) {
32534235Smarkfen 		mask_signals(B_FALSE);	/* Mask signals */
32544235Smarkfen 		dodump(SADB_SATYPE_UNSPEC, savefile);
32554235Smarkfen 		mask_signals(B_TRUE);	/* Unmask signals */
32564235Smarkfen 		EXIT_OK(NULL);
32574235Smarkfen 	}
32584235Smarkfen 
32594235Smarkfen 	/*
32604235Smarkfen 	 * When run from smf(5) flush any existing SA's first
32614235Smarkfen 	 * otherwise you will end up in maintenance mode.
32624235Smarkfen 	 */
32634235Smarkfen 	if ((my_fmri != NULL) && readfile) {
32644235Smarkfen 		(void) fprintf(stdout, gettext(
32654235Smarkfen 		    "Flushing existing SA's before adding new SA's\n"));
32664235Smarkfen 		(void) fflush(stdout);
32674235Smarkfen 		doflush(SADB_SATYPE_UNSPEC);
32684235Smarkfen 	}
32694235Smarkfen 	if (infile != stdin || *argv == NULL) {
32704235Smarkfen 		/* Go into interactive mode here. */
32714235Smarkfen 		do_interactive(infile, configfile, "ipseckey> ", my_fmri,
32724235Smarkfen 		    parseit);
32734235Smarkfen 	}
32744342Spwernau 	parseit(argc, argv, NULL, B_FALSE);
32754235Smarkfen 
32764235Smarkfen 	return (0);
32774235Smarkfen }
3278