xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ipseckey.c (revision 9086:d01fbcc0eff6)
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 /*
22*9086SVladimir.Kotal@Sun.COM  * Copyright 2009 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 #include <sys/types.h>
354235Smarkfen #include <sys/stat.h>
364235Smarkfen #include <sys/socket.h>
374235Smarkfen #include <sys/sysmacros.h>
384235Smarkfen #include <sys/fcntl.h>
394235Smarkfen #include <net/pfkeyv2.h>
404235Smarkfen #include <arpa/inet.h>
414235Smarkfen #include <netinet/in.h>
424235Smarkfen #include <sys/uio.h>
434235Smarkfen 
444235Smarkfen #include <syslog.h>
454235Smarkfen #include <signal.h>
464235Smarkfen #include <unistd.h>
474235Smarkfen #include <limits.h>
484235Smarkfen #include <stdlib.h>
494235Smarkfen #include <stdio.h>
504235Smarkfen #include <stdarg.h>
514235Smarkfen #include <netdb.h>
524235Smarkfen #include <pwd.h>
534235Smarkfen #include <errno.h>
544235Smarkfen #include <libintl.h>
554235Smarkfen #include <locale.h>
564235Smarkfen #include <fcntl.h>
574235Smarkfen #include <strings.h>
584235Smarkfen #include <ctype.h>
597749SThejaswini.Singarajipura@Sun.COM #include <sys/cladm.h>
604235Smarkfen 
614235Smarkfen #include <ipsec_util.h>
624235Smarkfen 
634235Smarkfen static int keysock;
647749SThejaswini.Singarajipura@Sun.COM static int cluster_socket;
654235Smarkfen static uint32_t seq;
664235Smarkfen static pid_t mypid;
674235Smarkfen static boolean_t vflag = B_FALSE;	/* Verbose? */
684235Smarkfen static boolean_t cflag = B_FALSE;	/* Check Only */
694235Smarkfen 
704235Smarkfen char *my_fmri = NULL;
714235Smarkfen FILE *debugfile = stdout;
727749SThejaswini.Singarajipura@Sun.COM static struct sockaddr_in cli_addr;
737749SThejaswini.Singarajipura@Sun.COM static boolean_t in_cluster_mode = B_FALSE;
744235Smarkfen 
754235Smarkfen #define	MAX_GET_SIZE	1024
764235Smarkfen /*
774573Spwernau  * WARN() and ERROR() do the same thing really, with ERROR() the function
784235Smarkfen  * that prints the error buffer needs to be called at the end of a code block
794235Smarkfen  * This will print out all accumulated errors before bailing. The WARN()
804235Smarkfen  * macro calls handle_errors() in such a way that it prints the message
814235Smarkfen  * then continues.
824235Smarkfen  * If the FATAL() macro used call handle_errors() immediately.
834235Smarkfen  */
844235Smarkfen #define	ERROR(x, y, z)  x = record_error(x, y, z)
854235Smarkfen #define	ERROR1(w, x, y, z)  w = record_error(w, x, y, z)
864235Smarkfen #define	ERROR2(v, w, x, y, z)  v = record_error(v, w, x, y, z)
874235Smarkfen #define	WARN(x, y, z) ERROR(x, y, z);\
884235Smarkfen 	handle_errors(x, NULL, B_FALSE, B_FALSE); x = NULL
894235Smarkfen #define	WARN1(w, x, y, z) ERROR1(w, x, y, z);\
904235Smarkfen 	handle_errors(w, NULL, B_FALSE, B_FALSE); w = NULL
914235Smarkfen #define	WARN2(v, w, x, y, z) ERROR2(v, w, x, y, z);\
924235Smarkfen 	handle_errors(v, NULL, B_FALSE, B_FALSE); v = NULL
934235Smarkfen #define	FATAL(x, y, z) ERROR(x, y, z);\
944235Smarkfen 	handle_errors(x, y, B_TRUE, B_TRUE)
954235Smarkfen #define	FATAL1(w, x, y, z) ERROR1(w, x, y, z);\
964235Smarkfen 	handle_errors(w, x, B_TRUE, B_TRUE)
974235Smarkfen 
984235Smarkfen /* Defined as a uint64_t array for alignment purposes. */
994235Smarkfen static uint64_t get_buffer[MAX_GET_SIZE];
1004235Smarkfen 
1014235Smarkfen /*
102*9086SVladimir.Kotal@Sun.COM  * Disable default TAB completion for now (until some brave soul tackles it).
103*9086SVladimir.Kotal@Sun.COM  */
104*9086SVladimir.Kotal@Sun.COM /* ARGSUSED */
105*9086SVladimir.Kotal@Sun.COM static
106*9086SVladimir.Kotal@Sun.COM CPL_MATCH_FN(no_match)
107*9086SVladimir.Kotal@Sun.COM {
108*9086SVladimir.Kotal@Sun.COM 	return (0);
109*9086SVladimir.Kotal@Sun.COM }
110*9086SVladimir.Kotal@Sun.COM 
111*9086SVladimir.Kotal@Sun.COM /*
1124235Smarkfen  * Create/Grow a buffer large enough to hold error messages. If *ebuf
1134235Smarkfen  * is not NULL then it will contain a copy of the command line that
1144235Smarkfen  * triggered the error/warning, copy this into a new buffer or
1154235Smarkfen  * append new messages to the existing buffer.
1164235Smarkfen  */
1174235Smarkfen /*PRINTFLIKE1*/
1184235Smarkfen char *
1194235Smarkfen record_error(char *ep, char *ebuf, char *fmt, ...)
1204235Smarkfen {
1214235Smarkfen 	char *err_ptr;
1224235Smarkfen 	char tmp_buff[1024];
1234235Smarkfen 	va_list ap;
1244235Smarkfen 	int length = 0;
1254235Smarkfen 	err_ptr = ep;
1264235Smarkfen 
1274235Smarkfen 	va_start(ap, fmt);
1285166Smarkfen 	length = vsnprintf(tmp_buff, sizeof (tmp_buff), fmt, ap);
1294235Smarkfen 	va_end(ap);
1304235Smarkfen 
1315166Smarkfen 	/* There is a new line character */
1325166Smarkfen 	length++;
1335166Smarkfen 
1344235Smarkfen 	if (ep == NULL) {
1355166Smarkfen 		if (ebuf != NULL)
1365166Smarkfen 			length += strlen(ebuf);
1375166Smarkfen 	} else  {
1385166Smarkfen 		length += strlen(ep);
1394235Smarkfen 	}
1405166Smarkfen 
1415166Smarkfen 	if (err_ptr == NULL)
1425166Smarkfen 		err_ptr = calloc(length, sizeof (char));
1435166Smarkfen 	else
1445166Smarkfen 		err_ptr = realloc(err_ptr, length);
1455166Smarkfen 
1464235Smarkfen 	if (err_ptr == NULL)
1474235Smarkfen 		Bail("realloc() failure");
1485166Smarkfen 
1495166Smarkfen 	/*
1505166Smarkfen 	 * If (ep == NULL) then this is the first error to record,
1515166Smarkfen 	 * copy in the command line that triggered this error/warning.
1525166Smarkfen 	 */
1535166Smarkfen 	if (ep == NULL && ebuf != NULL)
1545166Smarkfen 		(void) strlcpy(err_ptr, ebuf, length);
1555166Smarkfen 
1565166Smarkfen 	/*
1575166Smarkfen 	 * Now the actual error.
1585166Smarkfen 	 */
1594235Smarkfen 	(void) strlcat(err_ptr, tmp_buff, length);
1604235Smarkfen 	return (err_ptr);
1614235Smarkfen }
1624235Smarkfen 
1634235Smarkfen /*
164*9086SVladimir.Kotal@Sun.COM  * If not in interactive mode print usage message and exit.
1654235Smarkfen  */
1664235Smarkfen static void
1674235Smarkfen usage(void)
1684235Smarkfen {
1694235Smarkfen 	if (!interactive) {
1704235Smarkfen 		(void) fprintf(stderr, gettext("Usage:\t"
1714235Smarkfen 		    "ipseckey [ -nvp ] | cmd [sa_type] [extfield value]*\n"));
1724235Smarkfen 		(void) fprintf(stderr,
1734235Smarkfen 		    gettext("\tipseckey [ -nvp ] -f infile\n"));
1744235Smarkfen 		(void) fprintf(stderr,
1754235Smarkfen 		    gettext("\tipseckey [ -nvp ] -s outfile\n"));
176*9086SVladimir.Kotal@Sun.COM 		EXIT_FATAL(NULL);
177*9086SVladimir.Kotal@Sun.COM 	} else {
178*9086SVladimir.Kotal@Sun.COM 		(void) fprintf(stderr,
179*9086SVladimir.Kotal@Sun.COM 		    gettext("Type help or ? for usage info\n"));
1804235Smarkfen 	}
1814235Smarkfen }
1824235Smarkfen 
1834235Smarkfen 
1844235Smarkfen /*
1854235Smarkfen  * Print out any errors, tidy up as required.
1864235Smarkfen  * error pointer ep will be free()'d
1874235Smarkfen  */
1884235Smarkfen void
1894235Smarkfen handle_errors(char *ep, char *ebuf, boolean_t fatal, boolean_t done)
1904235Smarkfen {
1914235Smarkfen 	if (ep != NULL) {
1924235Smarkfen 		if (my_fmri == NULL) {
1934235Smarkfen 			/*
1944235Smarkfen 			 * For now suppress the errors when run from smf(5)
1954235Smarkfen 			 * because potentially sensitive information could
1964235Smarkfen 			 * end up in a publicly readable logfile.
1974235Smarkfen 			 */
1984235Smarkfen 			(void) fprintf(stdout, "%s\n", ep);
1994235Smarkfen 			(void) fflush(stdout);
2004235Smarkfen 		}
2014235Smarkfen 		free(ep);
2024235Smarkfen 		if (fatal) {
2034235Smarkfen 			if (ebuf != NULL) {
2044235Smarkfen 				free(ebuf);
2054235Smarkfen 			}
2064235Smarkfen 			/* reset command buffer */
2074235Smarkfen 			if (interactive)
2084235Smarkfen 				longjmp(env, 1);
2094235Smarkfen 		} else {
2104235Smarkfen 			return;
2114235Smarkfen 		}
2124235Smarkfen 	} else {
2134235Smarkfen 		/*
2144235Smarkfen 		 * No errors, if this is the last time that this function
2154235Smarkfen 		 * is called, free(ebuf) and reset command buffer.
2164235Smarkfen 		 */
2174235Smarkfen 		if (done) {
2184235Smarkfen 			if (ebuf != NULL) {
2194235Smarkfen 				free(ebuf);
2204235Smarkfen 			}
2214235Smarkfen 			/* reset command buffer */
2224235Smarkfen 			if (interactive)
2234235Smarkfen 				longjmp(env, 1);
2244235Smarkfen 		}
2254235Smarkfen 		return;
2264235Smarkfen 	}
2275166Smarkfen 	EXIT_FATAL(NULL);
2284235Smarkfen }
2294235Smarkfen 
2304235Smarkfen /*
2314235Smarkfen  * Initialize a PF_KEY base message.
2324235Smarkfen  */
2334235Smarkfen static void
2344235Smarkfen msg_init(struct sadb_msg *msg, uint8_t type, uint8_t satype)
2354235Smarkfen {
2364235Smarkfen 	msg->sadb_msg_version = PF_KEY_V2;
2374235Smarkfen 	msg->sadb_msg_type = type;
2384235Smarkfen 	msg->sadb_msg_errno = 0;
2394235Smarkfen 	msg->sadb_msg_satype = satype;
2404235Smarkfen 	/* For starters... */
2414235Smarkfen 	msg->sadb_msg_len = SADB_8TO64(sizeof (*msg));
2424235Smarkfen 	msg->sadb_msg_reserved = 0;
2434235Smarkfen 	msg->sadb_msg_seq = ++seq;
2444235Smarkfen 	msg->sadb_msg_pid = mypid;
2454235Smarkfen }
2464235Smarkfen 
2474235Smarkfen /*
2484235Smarkfen  * parseXXX and rparseXXX commands parse input and convert them to PF_KEY
2494235Smarkfen  * field values, or do the reverse for the purposes of saving the SA tables.
2504235Smarkfen  * (See the save_XXX functions.)
2514235Smarkfen  */
2524235Smarkfen 
2534235Smarkfen #define	CMD_NONE	0
2544235Smarkfen #define	CMD_UPDATE	2
2556668Smarkfen #define	CMD_UPDATE_PAIR	3
2566668Smarkfen #define	CMD_ADD		4
2576668Smarkfen #define	CMD_DELETE	5
2586668Smarkfen #define	CMD_DELETE_PAIR	6
2596668Smarkfen #define	CMD_GET		7
2604235Smarkfen #define	CMD_FLUSH	9
2614235Smarkfen #define	CMD_DUMP	10
2624235Smarkfen #define	CMD_MONITOR	11
2634235Smarkfen #define	CMD_PMONITOR	12
2644235Smarkfen #define	CMD_QUIT	13
2654235Smarkfen #define	CMD_SAVE	14
2664235Smarkfen #define	CMD_HELP	15
2674235Smarkfen 
2684235Smarkfen /*
2694235Smarkfen  * Parse the command.
2704235Smarkfen  */
2714235Smarkfen static int
2724235Smarkfen parsecmd(char *cmdstr)
2734235Smarkfen {
2744235Smarkfen 	static struct cmdtable {
2754235Smarkfen 		char *cmd;
2764235Smarkfen 		int token;
2774235Smarkfen 	} table[] = {
2784235Smarkfen 		/*
2794235Smarkfen 		 * Q: Do we want to do GETSPI?
2804235Smarkfen 		 * A: No, it's for automated key mgmt. only.  Either that,
2814235Smarkfen 		 *    or it isn't relevant until we support non IPsec SA types.
2824235Smarkfen 		 */
2834235Smarkfen 		{"update",		CMD_UPDATE},
2846668Smarkfen 		{"update-pair",		CMD_UPDATE_PAIR},
2854235Smarkfen 		{"add",			CMD_ADD},
2864235Smarkfen 		{"delete", 		CMD_DELETE},
2876668Smarkfen 		{"delete-pair",		CMD_DELETE_PAIR},
2884235Smarkfen 		{"get", 		CMD_GET},
2894235Smarkfen 		/*
2904235Smarkfen 		 * Q: And ACQUIRE and REGISTER and EXPIRE?
2914235Smarkfen 		 * A: not until we support non IPsec SA types.
2924235Smarkfen 		 */
2934235Smarkfen 		{"flush",		CMD_FLUSH},
2944235Smarkfen 		{"dump",		CMD_DUMP},
2954235Smarkfen 		{"monitor",		CMD_MONITOR},
2964235Smarkfen 		{"passive_monitor",	CMD_PMONITOR},
2974235Smarkfen 		{"pmonitor",		CMD_PMONITOR},
2984235Smarkfen 		{"quit",		CMD_QUIT},
2994235Smarkfen 		{"exit",		CMD_QUIT},
3004235Smarkfen 		{"save",		CMD_SAVE},
3014235Smarkfen 		{"help",		CMD_HELP},
3024235Smarkfen 		{"?",			CMD_HELP},
3034235Smarkfen 		{NULL,			CMD_NONE}
3044235Smarkfen 	};
3054235Smarkfen 	struct cmdtable *ct = table;
3064235Smarkfen 
3074235Smarkfen 	while (ct->cmd != NULL && strcmp(ct->cmd, cmdstr) != 0)
3084235Smarkfen 		ct++;
3094235Smarkfen 	return (ct->token);
3104235Smarkfen }
3114235Smarkfen 
3124235Smarkfen /*
3134235Smarkfen  * Convert a number from a command line.  I picked "u_longlong_t" for the
3144235Smarkfen  * number because we need the largest number available.  Also, the strto<num>
3154235Smarkfen  * calls don't deal in units of uintNN_t.
3164235Smarkfen  */
3174235Smarkfen static u_longlong_t
3184235Smarkfen parsenum(char *num, boolean_t bail, char *ebuf)
3194235Smarkfen {
3204235Smarkfen 	u_longlong_t rc = 0;
3214235Smarkfen 	char *end = NULL;
3224235Smarkfen 	char *ep = NULL;
3234235Smarkfen 
3244235Smarkfen 	if (num == NULL) {
3254235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line,"
3264235Smarkfen 		    " was expecting a number.\n"));
3274235Smarkfen 		/* NOTREACHED */
3284235Smarkfen 	}
3294235Smarkfen 
3304235Smarkfen 	errno = 0;
3314235Smarkfen 	rc = strtoull(num, &end, 0);
3324235Smarkfen 	if (errno != 0 || end == num || *end != '\0') {
3334235Smarkfen 		if (bail) {
3346668Smarkfen 			FATAL1(ep, ebuf, gettext(
3354235Smarkfen 			"Expecting a number, not \"%s\"!\n"), num);
3364235Smarkfen 		} else {
3374235Smarkfen 			/*
3384235Smarkfen 			 * -1, while not optimal, is sufficiently out of range
3394235Smarkfen 			 * for most of this function's applications when
3404235Smarkfen 			 * we don't just bail.
3414235Smarkfen 			 */
3424235Smarkfen 			return ((u_longlong_t)-1);
3434235Smarkfen 		}
3444235Smarkfen 	}
3454235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
3464235Smarkfen 	return (rc);
3474235Smarkfen }
3484235Smarkfen 
3494235Smarkfen /*
3504235Smarkfen  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
3514235Smarkfen  */
3524235Smarkfen static struct typetable {
3534235Smarkfen 	char *type;
3544235Smarkfen 	int token;
3554235Smarkfen } type_table[] = {
3564235Smarkfen 	{"all",	SADB_SATYPE_UNSPEC},
3574235Smarkfen 	{"ah",	SADB_SATYPE_AH},
3584235Smarkfen 	{"esp",	SADB_SATYPE_ESP},
3594235Smarkfen 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
3604235Smarkfen 	{NULL,	0}	/* Token value is irrelevant for this entry. */
3614235Smarkfen };
3624235Smarkfen 
3634235Smarkfen 
3644235Smarkfen static int
3654235Smarkfen parsesatype(char *type, char *ebuf)
3664235Smarkfen {
3674235Smarkfen 	struct typetable *tt = type_table;
3684235Smarkfen 	char *ep = NULL;
3694235Smarkfen 
3704235Smarkfen 	if (type == NULL)
3714235Smarkfen 		return (SADB_SATYPE_UNSPEC);
3724235Smarkfen 
3734235Smarkfen 	while (tt->type != NULL && strcasecmp(tt->type, type) != 0)
3744235Smarkfen 		tt++;
3754235Smarkfen 
3764235Smarkfen 	/*
3774235Smarkfen 	 * New SA types (including ones keysock maintains for user-land
3784235Smarkfen 	 * protocols) may be added, so parse a numeric value if possible.
3794235Smarkfen 	 */
3804235Smarkfen 	if (tt->type == NULL) {
3814235Smarkfen 		tt->token = (int)parsenum(type, B_FALSE, ebuf);
3824235Smarkfen 		if (tt->token == -1) {
3834235Smarkfen 			ERROR1(ep, ebuf, gettext(
3844235Smarkfen 			    "Unknown SA type (%s).\n"), type);
3854235Smarkfen 			tt->token = SADB_SATYPE_UNSPEC;
3864235Smarkfen 		}
3874235Smarkfen 	}
388*9086SVladimir.Kotal@Sun.COM 	handle_errors(ep, NULL, interactive ? B_TRUE : B_FALSE, B_FALSE);
3894235Smarkfen 	return (tt->token);
3904235Smarkfen }
3914235Smarkfen 
3924235Smarkfen #define	NEXTEOF		0
3934235Smarkfen #define	NEXTNONE	1
3944235Smarkfen #define	NEXTNUM		2
3954235Smarkfen #define	NEXTSTR		3
3964235Smarkfen #define	NEXTNUMSTR	4
3974235Smarkfen #define	NEXTADDR	5
3984235Smarkfen #define	NEXTHEX		6
3994235Smarkfen #define	NEXTIDENT	7
4004235Smarkfen #define	NEXTADDR4	8
4014235Smarkfen #define	NEXTADDR6	9
4024235Smarkfen 
4034235Smarkfen #define	TOK_EOF			0
4044235Smarkfen #define	TOK_UNKNOWN		1
4054235Smarkfen #define	TOK_SPI			2
4064235Smarkfen #define	TOK_REPLAY		3
4074235Smarkfen #define	TOK_STATE		4
4084235Smarkfen #define	TOK_AUTHALG		5
4094235Smarkfen #define	TOK_ENCRALG		6
4104235Smarkfen #define	TOK_FLAGS		7
4114235Smarkfen #define	TOK_SOFT_ALLOC		8
4124235Smarkfen #define	TOK_SOFT_BYTES		9
4134235Smarkfen #define	TOK_SOFT_ADDTIME	10
4144235Smarkfen #define	TOK_SOFT_USETIME	11
4154235Smarkfen #define	TOK_HARD_ALLOC		12
4164235Smarkfen #define	TOK_HARD_BYTES		13
4174235Smarkfen #define	TOK_HARD_ADDTIME	14
4184235Smarkfen #define	TOK_HARD_USETIME	15
4194235Smarkfen #define	TOK_CURRENT_ALLOC	16
4204235Smarkfen #define	TOK_CURRENT_BYTES	17
4214235Smarkfen #define	TOK_CURRENT_ADDTIME	18
4224235Smarkfen #define	TOK_CURRENT_USETIME	19
4234235Smarkfen #define	TOK_SRCADDR		20
4244235Smarkfen #define	TOK_DSTADDR		21
4254235Smarkfen #define	TOK_PROXYADDR		22
4264235Smarkfen #define	TOK_AUTHKEY		23
4274235Smarkfen #define	TOK_ENCRKEY		24
4284235Smarkfen #define	TOK_SRCIDTYPE		25
4294235Smarkfen #define	TOK_DSTIDTYPE		26
4304235Smarkfen #define	TOK_DPD			27
4314235Smarkfen #define	TOK_SENS_LEVEL		28
4324235Smarkfen #define	TOK_SENS_MAP		29
4334235Smarkfen #define	TOK_INTEG_LEVEL		30
4344235Smarkfen #define	TOK_INTEG_MAP		31
4354235Smarkfen #define	TOK_SRCADDR6		32
4364235Smarkfen #define	TOK_DSTADDR6		33
4374235Smarkfen #define	TOK_PROXYADDR6		34
4384235Smarkfen #define	TOK_SRCPORT		35
4394235Smarkfen #define	TOK_DSTPORT		36
4404235Smarkfen #define	TOK_PROTO		37
4414235Smarkfen #define	TOK_ENCAP		38
4424235Smarkfen #define	TOK_NATLOC		39
4434235Smarkfen #define	TOK_NATREM		40
4444235Smarkfen #define	TOK_NATLPORT		41
4454235Smarkfen #define	TOK_NATRPORT		42
4464235Smarkfen #define	TOK_IPROTO		43
4474235Smarkfen #define	TOK_IDSTADDR		44
4484235Smarkfen #define	TOK_IDSTADDR6		45
4494235Smarkfen #define	TOK_ISRCPORT		46
4504235Smarkfen #define	TOK_IDSTPORT		47
4516668Smarkfen #define	TOK_PAIR_SPI		48
4526668Smarkfen #define	TOK_FLAG_INBOUND	49
4536668Smarkfen #define	TOK_FLAG_OUTBOUND	50
4547749SThejaswini.Singarajipura@Sun.COM #define	TOK_REPLAY_VALUE	51
4557749SThejaswini.Singarajipura@Sun.COM #define	TOK_IDLE_ADDTIME	52
4567749SThejaswini.Singarajipura@Sun.COM #define	TOK_IDLE_USETIME	53
4574235Smarkfen 
4584235Smarkfen static struct toktable {
4594235Smarkfen 	char *string;
4604235Smarkfen 	int token;
4614235Smarkfen 	int next;
4624235Smarkfen } tokens[] = {
4634235Smarkfen 	/* "String",		token value,		next arg is */
4644235Smarkfen 	{"spi",			TOK_SPI,		NEXTNUM},
4656668Smarkfen 	{"pair-spi",		TOK_PAIR_SPI,		NEXTNUM},
4664235Smarkfen 	{"replay",		TOK_REPLAY,		NEXTNUM},
4674235Smarkfen 	{"state",		TOK_STATE,		NEXTNUMSTR},
4684235Smarkfen 	{"auth_alg",		TOK_AUTHALG,		NEXTNUMSTR},
4694235Smarkfen 	{"authalg",		TOK_AUTHALG,		NEXTNUMSTR},
4704235Smarkfen 	{"encr_alg",		TOK_ENCRALG,		NEXTNUMSTR},
4714235Smarkfen 	{"encralg",		TOK_ENCRALG,		NEXTNUMSTR},
4724235Smarkfen 	{"flags",		TOK_FLAGS,		NEXTNUM},
4734235Smarkfen 	{"soft_alloc",		TOK_SOFT_ALLOC,		NEXTNUM},
4744235Smarkfen 	{"soft_bytes",		TOK_SOFT_BYTES,		NEXTNUM},
4754235Smarkfen 	{"soft_addtime",	TOK_SOFT_ADDTIME,	NEXTNUM},
4764235Smarkfen 	{"soft_usetime",	TOK_SOFT_USETIME,	NEXTNUM},
4774235Smarkfen 	{"hard_alloc",		TOK_HARD_ALLOC,		NEXTNUM},
4784235Smarkfen 	{"hard_bytes",		TOK_HARD_BYTES,		NEXTNUM},
4794235Smarkfen 	{"hard_addtime",	TOK_HARD_ADDTIME,	NEXTNUM},
4804235Smarkfen 	{"hard_usetime",	TOK_HARD_USETIME,	NEXTNUM},
4814235Smarkfen 	{"current_alloc",	TOK_CURRENT_ALLOC,	NEXTNUM},
4824235Smarkfen 	{"current_bytes",	TOK_CURRENT_BYTES,	NEXTNUM},
4834235Smarkfen 	{"current_addtime",	TOK_CURRENT_ADDTIME,	NEXTNUM},
4844235Smarkfen 	{"current_usetime",	TOK_CURRENT_USETIME,	NEXTNUM},
4854235Smarkfen 
4864235Smarkfen 	{"saddr",		TOK_SRCADDR,		NEXTADDR},
4874235Smarkfen 	{"srcaddr",		TOK_SRCADDR,		NEXTADDR},
4884235Smarkfen 	{"src",			TOK_SRCADDR,		NEXTADDR},
4894235Smarkfen 	{"daddr",		TOK_DSTADDR,		NEXTADDR},
4904235Smarkfen 	{"dstaddr",		TOK_DSTADDR,		NEXTADDR},
4914235Smarkfen 	{"dst",			TOK_DSTADDR,		NEXTADDR},
4924235Smarkfen 	{"proxyaddr",		TOK_PROXYADDR,		NEXTADDR},
4934235Smarkfen 	{"proxy",		TOK_PROXYADDR,		NEXTADDR},
4944235Smarkfen 	{"innersrc",		TOK_PROXYADDR,		NEXTADDR},
4954235Smarkfen 	{"isrc",		TOK_PROXYADDR,		NEXTADDR},
4964235Smarkfen 	{"innerdst",		TOK_IDSTADDR,		NEXTADDR},
4974235Smarkfen 	{"idst",		TOK_IDSTADDR,		NEXTADDR},
4984235Smarkfen 
4994235Smarkfen 	{"sport",		TOK_SRCPORT,		NEXTNUM},
5004235Smarkfen 	{"dport",		TOK_DSTPORT,		NEXTNUM},
5014235Smarkfen 	{"innersport",		TOK_ISRCPORT,		NEXTNUM},
5024235Smarkfen 	{"isport",		TOK_ISRCPORT,		NEXTNUM},
5034235Smarkfen 	{"innerdport",		TOK_IDSTPORT,		NEXTNUM},
5044235Smarkfen 	{"idport",		TOK_IDSTPORT,		NEXTNUM},
5054235Smarkfen 	{"proto",		TOK_PROTO,		NEXTNUM},
5064235Smarkfen 	{"ulp",			TOK_PROTO,		NEXTNUM},
5074235Smarkfen 	{"iproto",		TOK_IPROTO,		NEXTNUM},
5084235Smarkfen 	{"iulp",		TOK_IPROTO,		NEXTNUM},
5094235Smarkfen 
5104235Smarkfen 	{"saddr6",		TOK_SRCADDR6,		NEXTADDR},
5114235Smarkfen 	{"srcaddr6",		TOK_SRCADDR6,		NEXTADDR},
5124235Smarkfen 	{"src6",		TOK_SRCADDR6,		NEXTADDR},
5134235Smarkfen 	{"daddr6",		TOK_DSTADDR6,		NEXTADDR},
5144235Smarkfen 	{"dstaddr6",		TOK_DSTADDR6,		NEXTADDR},
5154235Smarkfen 	{"dst6",		TOK_DSTADDR6,		NEXTADDR},
5164235Smarkfen 	{"proxyaddr6",		TOK_PROXYADDR6,		NEXTADDR},
5174235Smarkfen 	{"proxy6",		TOK_PROXYADDR6,		NEXTADDR},
5184235Smarkfen 	{"innersrc6",		TOK_PROXYADDR6,		NEXTADDR},
5194235Smarkfen 	{"isrc6",		TOK_PROXYADDR6,		NEXTADDR},
5204235Smarkfen 	{"innerdst6",		TOK_IDSTADDR6,		NEXTADDR},
5214235Smarkfen 	{"idst6",		TOK_IDSTADDR6,		NEXTADDR},
5224235Smarkfen 
5234235Smarkfen 	{"authkey",		TOK_AUTHKEY,		NEXTHEX},
5244235Smarkfen 	{"encrkey",		TOK_ENCRKEY,		NEXTHEX},
5254235Smarkfen 	{"srcidtype",		TOK_SRCIDTYPE,		NEXTIDENT},
5264235Smarkfen 	{"dstidtype",		TOK_DSTIDTYPE,		NEXTIDENT},
5274235Smarkfen 	{"dpd",			TOK_DPD,		NEXTNUM},
5284235Smarkfen 	{"sens_level",		TOK_SENS_LEVEL,		NEXTNUM},
5294235Smarkfen 	{"sens_map",		TOK_SENS_MAP,		NEXTHEX},
5304235Smarkfen 	{"integ_level",		TOK_INTEG_LEVEL,	NEXTNUM},
5314235Smarkfen 	{"integ_map",		TOK_INTEG_MAP,		NEXTHEX},
5324235Smarkfen 	{"nat_loc",		TOK_NATLOC,		NEXTADDR},
5334235Smarkfen 	{"nat_rem",		TOK_NATREM,		NEXTADDR},
5344235Smarkfen 	{"nat_lport",		TOK_NATLPORT,		NEXTNUM},
5354235Smarkfen 	{"nat_rport",		TOK_NATRPORT,		NEXTNUM},
5364235Smarkfen 	{"encap",		TOK_ENCAP,		NEXTNUMSTR},
5376668Smarkfen 
5386668Smarkfen 	{"outbound",		TOK_FLAG_OUTBOUND,	NULL},
5396668Smarkfen 	{"inbound",		TOK_FLAG_INBOUND,	NULL},
5407749SThejaswini.Singarajipura@Sun.COM 
5417749SThejaswini.Singarajipura@Sun.COM 	{"replay_value",	TOK_REPLAY_VALUE,	NEXTNUM},
5427749SThejaswini.Singarajipura@Sun.COM 	{"idle_addtime",	TOK_IDLE_ADDTIME,	NEXTNUM},
5437749SThejaswini.Singarajipura@Sun.COM 	{"idle_usetime",	TOK_IDLE_USETIME,	NEXTNUM},
5444235Smarkfen 	{NULL,			TOK_UNKNOWN,		NEXTEOF}
5454235Smarkfen };
5464235Smarkfen 
5474235Smarkfen /*
5484235Smarkfen  * Q:	Do I need stuff for proposals, combinations, supported algorithms,
5494235Smarkfen  *	or SPI ranges?
5504235Smarkfen  *
5514235Smarkfen  * A:	Probably not, but you never know.
5524235Smarkfen  *
5534235Smarkfen  * Parse out extension header type values.
5544235Smarkfen  */
5554235Smarkfen static int
5564235Smarkfen parseextval(char *value, int *next)
5574235Smarkfen {
5584235Smarkfen 	struct toktable *tp;
5594235Smarkfen 
5604235Smarkfen 	if (value == NULL)
5614235Smarkfen 		return (TOK_EOF);
5624235Smarkfen 
5634235Smarkfen 	for (tp = tokens; tp->string != NULL; tp++)
5644235Smarkfen 		if (strcmp(value, tp->string) == 0)
5654235Smarkfen 			break;
5664235Smarkfen 
5674235Smarkfen 	/*
5684235Smarkfen 	 * Since the OS controls what extensions are available, we don't have
5694235Smarkfen 	 * to parse numeric values here.
5704235Smarkfen 	 */
5714235Smarkfen 
5724235Smarkfen 	*next = tp->next;
5734235Smarkfen 	return (tp->token);
5744235Smarkfen }
5754235Smarkfen 
5764235Smarkfen /*
5774235Smarkfen  * Parse possible state values.
5784235Smarkfen  */
5794235Smarkfen static uint8_t
5804235Smarkfen parsestate(char *state, char *ebuf)
5814235Smarkfen {
5824235Smarkfen 	struct states {
5834235Smarkfen 		char *state;
5844235Smarkfen 		uint8_t retval;
5854235Smarkfen 	} states[] = {
5864235Smarkfen 		{"larval",	SADB_SASTATE_LARVAL},
5874235Smarkfen 		{"mature",	SADB_SASTATE_MATURE},
5884235Smarkfen 		{"dying",	SADB_SASTATE_DYING},
5894235Smarkfen 		{"dead",	SADB_SASTATE_DEAD},
5904235Smarkfen 		{NULL,		0}
5914235Smarkfen 	};
5924235Smarkfen 	struct states *sp;
5934235Smarkfen 	char *ep = NULL;
5944235Smarkfen 
5954235Smarkfen 	if (state == NULL) {
5964235Smarkfen 		FATAL(ep, ebuf, "Unexpected end of command line "
5974235Smarkfen 		    "was expecting a state.\n");
5984235Smarkfen 	}
5994235Smarkfen 
6004235Smarkfen 	for (sp = states; sp->state != NULL; sp++) {
6014235Smarkfen 		if (strcmp(sp->state, state) == 0)
6024235Smarkfen 			return (sp->retval);
6034235Smarkfen 	}
6044235Smarkfen 	ERROR1(ep, ebuf, gettext("Unknown state type \"%s\"\n"), state);
6054235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
6064235Smarkfen 	return (0);
6074235Smarkfen }
6084235Smarkfen 
6094235Smarkfen /*
6104235Smarkfen  * Return the numerical algorithm identifier corresponding to the specified
6114235Smarkfen  * algorithm name.
6124235Smarkfen  */
6134235Smarkfen static uint8_t
6144235Smarkfen parsealg(char *alg, int proto_num, char *ebuf)
6154235Smarkfen {
6164235Smarkfen 	u_longlong_t invalue;
6174235Smarkfen 	struct ipsecalgent *algent;
6184235Smarkfen 	char *ep = NULL;
6194235Smarkfen 
6204235Smarkfen 	if (alg == NULL) {
6214235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
6224235Smarkfen 		    "was expecting an algorithm name.\n"));
6234235Smarkfen 	}
6244235Smarkfen 
6254235Smarkfen 	algent = getipsecalgbyname(alg, proto_num, NULL);
6264235Smarkfen 	if (algent != NULL) {
6274235Smarkfen 		uint8_t alg_num;
6284235Smarkfen 
6294235Smarkfen 		alg_num = algent->a_alg_num;
6304235Smarkfen 		freeipsecalgent(algent);
6314235Smarkfen 
6324235Smarkfen 		return (alg_num);
6334235Smarkfen 	}
6344235Smarkfen 
6354235Smarkfen 	/*
6364235Smarkfen 	 * Since algorithms can be loaded during kernel run-time, check for
6374235Smarkfen 	 * numeric algorithm values too.  PF_KEY can catch bad ones with EINVAL.
6384235Smarkfen 	 */
6394235Smarkfen 	invalue = parsenum(alg, B_FALSE, ebuf);
6404235Smarkfen 	if (invalue != (u_longlong_t)-1 &&
6414235Smarkfen 	    (u_longlong_t)(invalue & (u_longlong_t)0xff) == invalue)
6424235Smarkfen 		return ((uint8_t)invalue);
6434235Smarkfen 
6444235Smarkfen 	if (proto_num == IPSEC_PROTO_ESP) {
6454235Smarkfen 		ERROR1(ep, ebuf, gettext(
6464235Smarkfen 		    "Unknown encryption algorithm type \"%s\"\n"), alg);
6474235Smarkfen 	} else {
6484235Smarkfen 		ERROR1(ep, ebuf, gettext(
6494235Smarkfen 		    "Unknown authentication algorithm type \"%s\"\n"), alg);
6504235Smarkfen 	}
6514235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
6524235Smarkfen 	return (0);
6534235Smarkfen }
6544235Smarkfen 
6554235Smarkfen /*
6564235Smarkfen  * Parse and reverse parse out a source/destination ID type.
6574235Smarkfen  */
6584235Smarkfen static struct idtypes {
6594235Smarkfen 	char *idtype;
6604235Smarkfen 	uint8_t retval;
6614235Smarkfen } idtypes[] = {
6624235Smarkfen 	{"prefix",	SADB_IDENTTYPE_PREFIX},
6634235Smarkfen 	{"fqdn",	SADB_IDENTTYPE_FQDN},
6644235Smarkfen 	{"domain",	SADB_IDENTTYPE_FQDN},
6654235Smarkfen 	{"domainname",	SADB_IDENTTYPE_FQDN},
6664235Smarkfen 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
6674235Smarkfen 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
6684235Smarkfen 	{"der_dn",	SADB_X_IDENTTYPE_DN},
6694235Smarkfen 	{"der_gn",	SADB_X_IDENTTYPE_GN},
6704235Smarkfen 	{NULL,		0}
6714235Smarkfen };
6724235Smarkfen 
6734235Smarkfen static uint16_t
6744235Smarkfen parseidtype(char *type, char *ebuf)
6754235Smarkfen {
6764235Smarkfen 	struct idtypes *idp;
6774235Smarkfen 	u_longlong_t invalue;
6784235Smarkfen 	char *ep = NULL;
6794235Smarkfen 
6804235Smarkfen 	if (type == NULL) {
6814235Smarkfen 		/* Shouldn't reach here, see callers for why. */
6824235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
6834235Smarkfen 		    "was expecting a type.\n"));
6844235Smarkfen 	}
6854235Smarkfen 
6864235Smarkfen 	for (idp = idtypes; idp->idtype != NULL; idp++) {
6874235Smarkfen 		if (strcasecmp(idp->idtype, type) == 0)
6884235Smarkfen 			return (idp->retval);
6894235Smarkfen 	}
6904235Smarkfen 	/*
6914235Smarkfen 	 * Since identity types are almost arbitrary, check for numeric
6924235Smarkfen 	 * algorithm values too.  PF_KEY can catch bad ones with EINVAL.
6934235Smarkfen 	 */
6944235Smarkfen 	invalue = parsenum(type, B_FALSE, ebuf);
6954235Smarkfen 	if (invalue != (u_longlong_t)-1 &&
6964235Smarkfen 	    (u_longlong_t)(invalue & (u_longlong_t)0xffff) == invalue)
6974235Smarkfen 		return ((uint16_t)invalue);
6984235Smarkfen 
6994235Smarkfen 
7004235Smarkfen 	ERROR1(ep, ebuf, gettext("Unknown identity type \"%s\"\n"), type);
7014235Smarkfen 
7024235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
7034235Smarkfen 	return (0);
7044235Smarkfen }
7054235Smarkfen 
7064235Smarkfen /*
7074235Smarkfen  * Parse an address off the command line.  Return length of sockaddr,
7084235Smarkfen  * and either return a hostent pointer (caller frees).  The new
7094235Smarkfen  * getipnodebyname() call does the Right Thing (TM), even with
7104235Smarkfen  * raw addresses (colon-separated IPv6 or dotted decimal IPv4).
7114235Smarkfen  */
7124235Smarkfen 
7134235Smarkfen static struct {
7144235Smarkfen 	struct hostent he;
7154235Smarkfen 	char *addtl[2];
7164235Smarkfen 	} dummy;
7174235Smarkfen static union {
7184235Smarkfen 	struct in6_addr ipv6;
7194235Smarkfen 	struct in_addr ipv4;
7204235Smarkfen 	uint64_t aligner;
7214235Smarkfen } addr1;
7224235Smarkfen 
7234235Smarkfen static int
7244235Smarkfen parseaddr(char *addr, struct hostent **hpp, boolean_t v6only, char *ebuf)
7254235Smarkfen {
7264235Smarkfen 	int hp_errno;
7274235Smarkfen 	struct hostent *hp = NULL;
7284235Smarkfen 	char *ep = NULL;
7294235Smarkfen 
7304235Smarkfen 	if (addr == NULL) {
7314235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
7324235Smarkfen 		    "was expecting an address.\n"));
7334235Smarkfen 	}
7344235Smarkfen 
7354235Smarkfen 	if (!nflag) {
7364235Smarkfen 		/*
7374235Smarkfen 		 * Try name->address first.  Assume AF_INET6, and
7384235Smarkfen 		 * get IPv4's, plus IPv6's if and only if IPv6 is configured.
7394235Smarkfen 		 * This means to add IPv6 SAs, you must have IPv6
7404235Smarkfen 		 * up-and-running.  (AI_DEFAULT works here.)
7414235Smarkfen 		 */
7424235Smarkfen 		hp = getipnodebyname(addr, AF_INET6,
7434235Smarkfen 		    (v6only ? AI_ADDRCONFIG : (AI_DEFAULT | AI_ALL)),
7444235Smarkfen 		    &hp_errno);
7454235Smarkfen 	} else {
7464235Smarkfen 		/*
7474235Smarkfen 		 * Try a normal address conversion only.  Use "dummy"
7484235Smarkfen 		 * to construct a fake hostent.  Caller will know not
7494235Smarkfen 		 * to free this one.
7504235Smarkfen 		 */
7514235Smarkfen 		if (inet_pton(AF_INET6, addr, &addr1) == 1) {
7524235Smarkfen 			dummy.he.h_addr_list = dummy.addtl;
7534235Smarkfen 			dummy.addtl[0] = (char *)&addr1;
7544235Smarkfen 			dummy.addtl[1] = NULL;
7554235Smarkfen 			hp = &dummy.he;
7564235Smarkfen 			dummy.he.h_addrtype = AF_INET6;
7574235Smarkfen 			dummy.he.h_length = sizeof (struct in6_addr);
7584235Smarkfen 		} else if (inet_pton(AF_INET, addr, &addr1) == 1) {
7594235Smarkfen 			/*
7604235Smarkfen 			 * Remap to AF_INET6 anyway.
7614235Smarkfen 			 */
7624235Smarkfen 			dummy.he.h_addr_list = dummy.addtl;
7634235Smarkfen 			dummy.addtl[0] = (char *)&addr1;
7644235Smarkfen 			dummy.addtl[1] = NULL;
7654235Smarkfen 			hp = &dummy.he;
7664235Smarkfen 			dummy.he.h_addrtype = AF_INET6;
7674235Smarkfen 			dummy.he.h_length = sizeof (struct in6_addr);
7684235Smarkfen 			/*
7694235Smarkfen 			 * NOTE:  If macro changes to disallow in-place
7704235Smarkfen 			 * conversion, rewhack this.
7714235Smarkfen 			 */
7724235Smarkfen 			IN6_INADDR_TO_V4MAPPED(&addr1.ipv4, &addr1.ipv6);
7734235Smarkfen 		} else {
7744235Smarkfen 			hp = NULL;
7754235Smarkfen 		}
7764235Smarkfen 	}
7774235Smarkfen 
7784235Smarkfen 	if (hp == NULL)
7794235Smarkfen 		WARN1(ep, ebuf, gettext("Unknown address %s."), addr);
7804235Smarkfen 
7814235Smarkfen 	*hpp = hp;
7824235Smarkfen 	/* Always return sockaddr_in6 for now. */
7834235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
7844235Smarkfen 	return (sizeof (struct sockaddr_in6));
7854235Smarkfen }
7864235Smarkfen 
7874235Smarkfen /*
7884235Smarkfen  * Parse a hex character for a key.  A string will take the form:
7894235Smarkfen  *	xxxxxxxxx/nn
7904235Smarkfen  * where
7914235Smarkfen  *	xxxxxxxxx == a string of hex characters ([0-9][a-f][A-F])
7924235Smarkfen  *	nn == an optional decimal "mask".  If it is not present, it
7934235Smarkfen  *	is assumed that the hex string will be rounded to the nearest
7944235Smarkfen  *	byte, where odd nibbles, like 123 will become 0x0123.
7954235Smarkfen  *
7964235Smarkfen  * NOTE:Unlike the expression of IP addresses, I will not allow an
7974235Smarkfen  *	excessive "mask".  For example 2112/50 is very illegal.
7984235Smarkfen  * NOTE2:	This key should be in canonical order.  Consult your man
7994235Smarkfen  *		pages per algorithm about said order.
8004235Smarkfen  */
8014235Smarkfen 
8024235Smarkfen #define	hd2num(hd) (((hd) >= '0' && (hd) <= '9') ? ((hd) - '0') : \
8034235Smarkfen 	(((hd) >= 'a' && (hd) <= 'f') ? ((hd) - 'a' + 10) : ((hd) - 'A' + 10)))
8044235Smarkfen 
8054235Smarkfen static struct sadb_key *
8064235Smarkfen parsekey(char *input, char *ebuf)
8074235Smarkfen {
8084235Smarkfen 	struct sadb_key *retval;
8094235Smarkfen 	uint_t i, hexlen = 0, bits, alloclen;
8104235Smarkfen 	uint8_t *key;
8114235Smarkfen 	char *ep = NULL;
8124235Smarkfen 
8134235Smarkfen 	if (input == NULL) {
8144235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
8154235Smarkfen 		    "was expecting a key.\n"));
8164235Smarkfen 	}
8174573Spwernau 	/* Allow hex values prepended with 0x convention */
8184573Spwernau 	if ((strnlen(input, sizeof (hexlen)) > 2) &&
8194573Spwernau 	    (strncasecmp(input, "0x", 2) == 0))
8204573Spwernau 		input += 2;
8214235Smarkfen 
8224235Smarkfen 	for (i = 0; input[i] != '\0' && input[i] != '/'; i++)
8234235Smarkfen 		hexlen++;
8244235Smarkfen 
8254235Smarkfen 	if (input[i] == '\0') {
8264235Smarkfen 		bits = 0;
8274235Smarkfen 	} else {
8284235Smarkfen 		/* Have /nn. */
8294235Smarkfen 		input[i] = '\0';
8304235Smarkfen 		if (sscanf((input + i + 1), "%u", &bits) != 1) {
8314235Smarkfen 			FATAL1(ep, ebuf, gettext(
8324235Smarkfen 			    "\"%s\" is not a bit specifier.\n"),
8334235Smarkfen 			    (input + i + 1));
8344235Smarkfen 		}
8354235Smarkfen 		/* hexlen in nibbles */
8364235Smarkfen 		if (((bits + 3) >> 2) > hexlen) {
8374235Smarkfen 			ERROR2(ep, ebuf, gettext(
8384235Smarkfen 			    "bit length %d is too big for %s.\n"), bits, input);
8394235Smarkfen 		}
8404235Smarkfen 		/*
8414235Smarkfen 		 * Adjust hexlen down if user gave us too small of a bit
8424235Smarkfen 		 * count.
8434235Smarkfen 		 */
8444235Smarkfen 		if ((hexlen << 2) > bits + 3) {
8454235Smarkfen 			WARN2(ep, ebuf, gettext(
8464235Smarkfen 			    "WARNING: Lower bits will be truncated "
8474235Smarkfen 			    "for:\n\t%s/%d.\n"), input, bits);
8484235Smarkfen 			hexlen = (bits + 3) >> 2;
8494235Smarkfen 			input[hexlen] = '\0';
8504235Smarkfen 		}
8514235Smarkfen 	}
8524235Smarkfen 
8534235Smarkfen 	/*
8544235Smarkfen 	 * Allocate.  Remember, hexlen is in nibbles.
8554235Smarkfen 	 */
8564235Smarkfen 
8574235Smarkfen 	alloclen = sizeof (*retval) + roundup((hexlen/2 + (hexlen & 0x1)), 8);
8584235Smarkfen 	retval = malloc(alloclen);
8594235Smarkfen 
8604235Smarkfen 	if (retval == NULL)
8614235Smarkfen 		Bail("malloc(parsekey)");
8624235Smarkfen 	retval->sadb_key_len = SADB_8TO64(alloclen);
8634235Smarkfen 	retval->sadb_key_reserved = 0;
8644235Smarkfen 	if (bits == 0)
8654235Smarkfen 		retval->sadb_key_bits = (hexlen + (hexlen & 0x1)) << 2;
8664235Smarkfen 	else
8674235Smarkfen 		retval->sadb_key_bits = bits;
8684235Smarkfen 
8694235Smarkfen 	/*
8704235Smarkfen 	 * Read in nibbles.  Read in odd-numbered as shifted high.
8714235Smarkfen 	 * (e.g. 123 becomes 0x1230).
8724235Smarkfen 	 */
8734235Smarkfen 
8744235Smarkfen 	key = (uint8_t *)(retval + 1);
8754235Smarkfen 	for (i = 0; input[i] != '\0'; i += 2) {
8764235Smarkfen 		boolean_t second = (input[i + 1] != '\0');
8774235Smarkfen 
8784235Smarkfen 		if (!isxdigit(input[i]) ||
8794235Smarkfen 		    (!isxdigit(input[i + 1]) && second)) {
8804235Smarkfen 			ERROR1(ep, ebuf, gettext(
8814235Smarkfen 			    "string '%s' not a hex value.\n"), input);
8824235Smarkfen 			free(retval);
8834235Smarkfen 			retval = NULL;
8844235Smarkfen 			break;
8854235Smarkfen 		}
8864235Smarkfen 		*key = (hd2num(input[i]) << 4);
8874235Smarkfen 		if (second)
8884235Smarkfen 			*key |= hd2num(input[i + 1]);
8894235Smarkfen 		else
8904235Smarkfen 			break;	/* out of for loop. */
8914235Smarkfen 		key++;
8924235Smarkfen 	}
8934235Smarkfen 
8944235Smarkfen 	/* bzero the remaining bits if we're a non-octet amount. */
8954235Smarkfen 	if (bits & 0x7)
8964235Smarkfen 		*((input[i] == '\0') ? key - 1 : key) &=
8974235Smarkfen 		    0xff << (8 - (bits & 0x7));
8984235Smarkfen 
8994235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
9004235Smarkfen 	return (retval);
9014235Smarkfen }
9024235Smarkfen 
9034235Smarkfen /*
9044235Smarkfen  * Write a message to the PF_KEY socket.  If verbose, print the message
9054235Smarkfen  * heading into the kernel.
9064235Smarkfen  */
9074235Smarkfen static int
9084235Smarkfen key_write(int fd, void *msg, size_t len)
9094235Smarkfen {
9104235Smarkfen 	if (vflag) {
9114235Smarkfen 		(void) printf(
9124235Smarkfen 		    gettext("VERBOSE ON:  Message to kernel looks like:\n"));
9134235Smarkfen 		(void) printf("==========================================\n");
9144867Spwernau 		print_samsg(stdout, msg, B_FALSE, vflag, nflag);
9154235Smarkfen 		(void) printf("==========================================\n");
9164235Smarkfen 	}
9174235Smarkfen 
9184235Smarkfen 	return (write(fd, msg, len));
9194235Smarkfen }
9204235Smarkfen 
9214235Smarkfen /*
9224235Smarkfen  * SIGALRM handler for time_critical_enter.
9234235Smarkfen  */
9244235Smarkfen static void
9254235Smarkfen time_critical_catch(int signal)
9264235Smarkfen {
9274235Smarkfen 	if (signal == SIGALRM) {
9284235Smarkfen 		errx(1, gettext("Reply message from PF_KEY timed out."));
9294235Smarkfen 	} else {
9304235Smarkfen 		errx(1, gettext("Caught signal %d while trying to receive"
9314342Spwernau 		    "PF_KEY reply message"), signal);
9324235Smarkfen 	}
9334235Smarkfen 	/* errx() calls exit. */
9344235Smarkfen }
9354235Smarkfen 
9364235Smarkfen #define	TIME_CRITICAL_TIME 10	/* In seconds */
9374235Smarkfen 
9384235Smarkfen /*
9394235Smarkfen  * Enter a "time critical" section where key is waiting for a return message.
9404235Smarkfen  */
9414235Smarkfen static void
9424235Smarkfen time_critical_enter(void)
9434235Smarkfen {
9444235Smarkfen 	(void) signal(SIGALRM, time_critical_catch);
9454235Smarkfen 	(void) alarm(TIME_CRITICAL_TIME);
9464235Smarkfen }
9474235Smarkfen 
9484235Smarkfen /*
9494235Smarkfen  * Exit the "time critical" section after getting an appropriate return
9504235Smarkfen  * message.
9514235Smarkfen  */
9524235Smarkfen static void
9534235Smarkfen time_critical_exit(void)
9544235Smarkfen {
9554235Smarkfen 	(void) alarm(0);
9564235Smarkfen 	(void) signal(SIGALRM, SIG_DFL);
9574235Smarkfen }
9584235Smarkfen 
9594235Smarkfen /*
9604235Smarkfen  * Construct a PF_KEY FLUSH message for the SA type specified.
9614235Smarkfen  */
9624235Smarkfen static void
9634235Smarkfen doflush(int satype)
9644235Smarkfen {
9654235Smarkfen 	struct sadb_msg msg;
9664235Smarkfen 	int rc;
9674235Smarkfen 
9684235Smarkfen 	msg_init(&msg, SADB_FLUSH, (uint8_t)satype);
9694235Smarkfen 	rc = key_write(keysock, &msg, sizeof (msg));
9704235Smarkfen 	if (rc == -1)
9714235Smarkfen 		Bail("write() to PF_KEY socket failed (in doflush)");
9724235Smarkfen 
9734235Smarkfen 	time_critical_enter();
9744235Smarkfen 	do {
9754235Smarkfen 		rc = read(keysock, &msg, sizeof (msg));
9764235Smarkfen 		if (rc == -1)
9774235Smarkfen 			Bail("read (in doflush)");
9784235Smarkfen 	} while (msg.sadb_msg_seq != seq || msg.sadb_msg_pid != mypid);
9794235Smarkfen 	time_critical_exit();
9804235Smarkfen 
9814235Smarkfen 	/*
9824235Smarkfen 	 * I should _never_ hit the following unless:
9834235Smarkfen 	 *
9844235Smarkfen 	 * 1. There is a kernel bug.
9854235Smarkfen 	 * 2. There is another process filling in its pid with mine, and
9864235Smarkfen 	 *    issuing a different message that would cause a different result.
9874235Smarkfen 	 */
9884235Smarkfen 	if (msg.sadb_msg_type != SADB_FLUSH ||
9894235Smarkfen 	    msg.sadb_msg_satype != (uint8_t)satype) {
9904235Smarkfen 		syslog((LOG_NOTICE|LOG_AUTH),
9914235Smarkfen 		    gettext("doflush: Return message not of type SADB_FLUSH!"));
9924235Smarkfen 		Bail("doflush: Return message not of type SADB_FLUSH!");
9934235Smarkfen 	}
9944235Smarkfen 
9954235Smarkfen 	if (msg.sadb_msg_errno != 0) {
9964235Smarkfen 		errno = msg.sadb_msg_errno;
9974235Smarkfen 		if (errno == EINVAL) {
9984235Smarkfen 			print_diagnostic(stderr, msg.sadb_x_msg_diagnostic);
9994235Smarkfen 			warnx(gettext("Cannot flush SA type %d."), satype);
10004235Smarkfen 		}
10014235Smarkfen 		Bail("return message (in doflush)");
10024235Smarkfen 	}
10034235Smarkfen }
10044235Smarkfen 
10054235Smarkfen /*
10064235Smarkfen  * save_XXX functions are used when "saving" the SA tables to either a
10074235Smarkfen  * file or standard output.  They use the dump_XXX functions where needed,
10084235Smarkfen  * but mostly they use the rparseXXX functions.
10094235Smarkfen  */
10104235Smarkfen 
10114235Smarkfen /*
10124235Smarkfen  * Because "save" and "dump" both use the SADB_DUMP message, fold both
10134235Smarkfen  * into the same function.
10144235Smarkfen  */
10154235Smarkfen static void
10164235Smarkfen dodump(int satype, FILE *ofile)
10174235Smarkfen {
10184235Smarkfen 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
10194235Smarkfen 	int rc;
10204235Smarkfen 
10214235Smarkfen 	if (ofile != NULL) {
10224235Smarkfen 		(void) fprintf(ofile,
10234235Smarkfen 		    gettext("# This key file was generated by the"));
10244235Smarkfen 		(void) fprintf(ofile,
10254235Smarkfen 		    gettext(" ipseckey(1m) command's 'save' feature.\n\n"));
10264235Smarkfen 	}
10274235Smarkfen 	msg_init(msg, SADB_DUMP, (uint8_t)satype);
10284235Smarkfen 	rc = key_write(keysock, msg, sizeof (*msg));
10294235Smarkfen 	if (rc == -1)
10304235Smarkfen 		Bail("write to PF_KEY socket failed (in dodump)");
10314235Smarkfen 
10324235Smarkfen 	do {
10334235Smarkfen 		/*
10344235Smarkfen 		 * For DUMP, do only the read as a time critical section.
10354235Smarkfen 		 */
10364235Smarkfen 		time_critical_enter();
10374235Smarkfen 		rc = read(keysock, get_buffer, sizeof (get_buffer));
10384235Smarkfen 		time_critical_exit();
10394235Smarkfen 		if (rc == -1)
10404235Smarkfen 			Bail("read (in dodump)");
10414235Smarkfen 		if (msg->sadb_msg_pid == mypid &&
10424235Smarkfen 		    msg->sadb_msg_type == SADB_DUMP &&
10434235Smarkfen 		    msg->sadb_msg_seq != 0 &&
10444235Smarkfen 		    msg->sadb_msg_errno == 0) {
10454235Smarkfen 			if (ofile == NULL) {
10464867Spwernau 				print_samsg(stdout, get_buffer, B_FALSE, vflag,
10474867Spwernau 				    nflag);
10484235Smarkfen 				(void) putchar('\n');
10494235Smarkfen 			} else {
10504235Smarkfen 				save_assoc(get_buffer, ofile);
10514235Smarkfen 			}
10524235Smarkfen 		}
10534235Smarkfen 	} while (msg->sadb_msg_pid != mypid ||
10544235Smarkfen 	    (msg->sadb_msg_errno == 0 && msg->sadb_msg_seq != 0));
10554235Smarkfen 
10564235Smarkfen 	if (ofile != NULL && ofile != stdout)
10574235Smarkfen 		(void) fclose(ofile);
10584235Smarkfen 
10594235Smarkfen 	if (msg->sadb_msg_errno == 0) {
10604235Smarkfen 		if (ofile == NULL)
10614235Smarkfen 			(void) printf(
10624235Smarkfen 			    gettext("Dump succeeded for SA type %d.\n"),
10634235Smarkfen 			    satype);
10644235Smarkfen 	} else {
10654235Smarkfen 		print_diagnostic(stderr, msg->sadb_x_msg_diagnostic);
10664235Smarkfen 		errno = msg->sadb_msg_errno;
10674235Smarkfen 		Bail("Dump failed");
10684235Smarkfen 	}
10694235Smarkfen }
10704235Smarkfen 
10714235Smarkfen #define	SCOPE_UNSPEC 0
10724235Smarkfen #define	SCOPE_LINKLOCAL 1
10734235Smarkfen #define	SCOPE_SITELOCAL 2
10744235Smarkfen #define	SCOPE_GLOBAL 3
10754235Smarkfen #define	SCOPE_V4COMPAT 4
10764235Smarkfen #define	SCOPE_LOOPBACK 5	/* Pedantic, yes, but necessary. */
10774235Smarkfen 
10784235Smarkfen static int
10794235Smarkfen ipv6_addr_scope(struct in6_addr *addr)
10804235Smarkfen {
10814235Smarkfen 	/* Don't return anything regarding multicast for now... */
10824235Smarkfen 
10834235Smarkfen 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
10844235Smarkfen 		return (SCOPE_UNSPEC);
10854235Smarkfen 
10864235Smarkfen 	if (IN6_IS_ADDR_LINKLOCAL(addr))
10874235Smarkfen 		return (SCOPE_LINKLOCAL);
10884235Smarkfen 
10894235Smarkfen 	if (IN6_IS_ADDR_SITELOCAL(addr))
10904235Smarkfen 		return (SCOPE_SITELOCAL);
10914235Smarkfen 
10924235Smarkfen 	if (IN6_IS_ADDR_V4COMPAT(addr))
10934235Smarkfen 		return (SCOPE_V4COMPAT);
10944235Smarkfen 
10954235Smarkfen 	if (IN6_IS_ADDR_LOOPBACK(addr))
10964235Smarkfen 		return (SCOPE_LOOPBACK);
10974235Smarkfen 
10984235Smarkfen 	/* For now, return global by default. */
10994235Smarkfen 	return (SCOPE_GLOBAL);
11004235Smarkfen }
11014235Smarkfen 
11024235Smarkfen /*
11034235Smarkfen  * doaddresses():
11044235Smarkfen  *
11054235Smarkfen  * Used by doaddup() and dodelget() to create new SA's based on the
11064235Smarkfen  * provided source and destination addresses hostent.
11074235Smarkfen  *
11084235Smarkfen  * sadb_msg_type: expected PF_KEY reply message type
11094235Smarkfen  * sadb_msg_satype: expected PF_KEY reply satype
11104235Smarkfen  * cmd: user command
11114235Smarkfen  * srchp: hostent for the source address(es)
11124235Smarkfen  * dsthp: hostent for the destination address(es)
11134235Smarkfen  * src: points to the SADB source address extension
11144235Smarkfen  * dst: points to the SADB destination address extension
11154235Smarkfen  * unspec_src: indicates an unspecified source address.
11164235Smarkfen  * buffer: pointer to the SADB buffer to use with PF_KEY
11174235Smarkfen  * buffer_size: size of buffer
11184235Smarkfen  * spi: spi for this message (set by caller)
11194235Smarkfen  * srcport: source port if specified
11204235Smarkfen  * dstport: destination port if specified
11214235Smarkfen  * proto: IP protocol number if specified
11224235Smarkfen  * iproto: Inner (tunnel mode) IP protocol number if specified
11234235Smarkfen  * NATT note: we are going to assume a semi-sane world where NAT
11244235Smarkfen  * boxen don't explode to multiple addresses.
11254235Smarkfen  */
11264235Smarkfen static void
11274235Smarkfen doaddresses(uint8_t sadb_msg_type, uint8_t sadb_msg_satype, int cmd,
11284235Smarkfen     struct hostent *srchp, struct hostent *dsthp,
11294235Smarkfen     struct sadb_address *src, struct sadb_address *dst,
11304235Smarkfen     boolean_t unspec_src, uint64_t *buffer, int buffer_size, uint32_t spi,
11314235Smarkfen     char *ebuf)
11324235Smarkfen {
11334235Smarkfen 	boolean_t single_dst;
11344235Smarkfen 	struct sockaddr_in6 *sin6;
11354235Smarkfen 	struct sadb_msg *msgp;
11364235Smarkfen 	int i, rc;
11374235Smarkfen 	char **walker;	/* For the SRC and PROXY walking functions. */
11384235Smarkfen 	char *first_match;
11394235Smarkfen 	uint64_t savebuf[MAX_GET_SIZE];
11404235Smarkfen 	uint16_t srcport = 0, dstport = 0;
11414235Smarkfen 	char *ep = NULL;
11424235Smarkfen 
11434235Smarkfen 	/*
11444235Smarkfen 	 * Okay, now we have "src", "dst", and maybe "proxy" reassigned
11454235Smarkfen 	 * to point into the buffer to be written to PF_KEY, we can do
11464235Smarkfen 	 * potentially several writes based on destination address.
11474235Smarkfen 	 *
11484235Smarkfen 	 * First, obtain port numbers from passed-in extensions.
11494235Smarkfen 	 */
11504235Smarkfen 
11514235Smarkfen 	if (src != NULL) {
11524235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
11534235Smarkfen 		srcport = ntohs(sin6->sin6_port);
11544235Smarkfen 	}
11554235Smarkfen 	if (dst != NULL) {
11564235Smarkfen 		sin6 = (struct sockaddr_in6 *)(dst + 1);
11574235Smarkfen 		dstport = ntohs(sin6->sin6_port);
11584235Smarkfen 	}
11594235Smarkfen 
11604235Smarkfen 	/*
11614235Smarkfen 	 * The rules for ADD, GET, and UPDATE: (NOTE:  This assumes IPsec.
11624235Smarkfen 	 * If other consumers of PF_KEY happen, this will have to be
11634235Smarkfen 	 * rewhacked.):
11644235Smarkfen 	 *
11654235Smarkfen 	 *	Do a message for every possible DST address.
11664235Smarkfen 	 *
11674235Smarkfen 	 *	If a source or proxy address explodes, keep unspecified
11684235Smarkfen 	 *	(and mention unspecified).
11694235Smarkfen 	 *
11704235Smarkfen 	 * If dsthp is == dummy.he, then go through the loop once.
11714235Smarkfen 	 * If any other hp is == dummy.he, then you don't have to apply any
11724235Smarkfen 	 * silly rules.
11734235Smarkfen 	 *
11744235Smarkfen 	 * DELETE is different, because you can leave either "src" or "dst"
11754235Smarkfen 	 * blank!  You need to explode if one of them is full, and not assume
11764235Smarkfen 	 * that the other is set.
11774235Smarkfen 	 */
11784235Smarkfen 
11794235Smarkfen 	if (dsthp == NULL) {
11804235Smarkfen 		/*
11814235Smarkfen 		 * No destination address specified.
11824235Smarkfen 		 * With extended diagnostics, we don't have to bail the
11834235Smarkfen 		 * non-DELETE cases here.  The EINVAL diagnostics will be
11844235Smarkfen 		 * enough to inform the user(s) what happened.
11854235Smarkfen 		 */
11864235Smarkfen 		i = 0;
11874235Smarkfen 		do {
11884235Smarkfen 			if (srchp == &dummy.he) {
11894235Smarkfen 				/* Just to be sure... */
11904235Smarkfen 				srchp->h_addr_list[1] = NULL;
11914235Smarkfen 			} else if (srchp != NULL) {
11924235Smarkfen 				/* Degenerate case, h_addr_list[0] == NULL. */
11934235Smarkfen 				if (srchp->h_addr_list[i] == NULL)
11944235Smarkfen 					Bail("Empty source address list");
11954235Smarkfen 
11964235Smarkfen 				/*
11974235Smarkfen 				 * Fill in the src sockaddr.
11984235Smarkfen 				 */
11994235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
12004235Smarkfen 				bzero(sin6, sizeof (*sin6));
12014235Smarkfen 				bcopy(srchp->h_addr_list[i], &sin6->sin6_addr,
12024235Smarkfen 				    sizeof (struct in6_addr));
12034235Smarkfen 				sin6->sin6_family = AF_INET6;
12044235Smarkfen 				sin6->sin6_port = htons(srcport);
12054235Smarkfen 			}
12064235Smarkfen 
12074235Smarkfen 			/* Save off a copy for later writing... */
12084235Smarkfen 			msgp = (struct sadb_msg *)buffer;
12094235Smarkfen 			bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
12104235Smarkfen 
12114235Smarkfen 			rc = key_write(keysock, buffer,
12124235Smarkfen 			    SADB_64TO8(msgp->sadb_msg_len));
12134235Smarkfen 			if (rc == -1)
12144235Smarkfen 				Bail("write() to PF_KEY socket "
12154235Smarkfen 				    "(in doaddresses)");
12167749SThejaswini.Singarajipura@Sun.COM 			/*
12177749SThejaswini.Singarajipura@Sun.COM 			 * Sends the message to the Solaris Cluster daemon
12187749SThejaswini.Singarajipura@Sun.COM 			 */
12197749SThejaswini.Singarajipura@Sun.COM 
12207749SThejaswini.Singarajipura@Sun.COM 			if (in_cluster_mode) {
12217749SThejaswini.Singarajipura@Sun.COM 				(void) sendto(cluster_socket, buffer,
12227749SThejaswini.Singarajipura@Sun.COM 				    SADB_64TO8(msgp->sadb_msg_len), 0,
12237749SThejaswini.Singarajipura@Sun.COM 				    (struct sockaddr *)&cli_addr,
12247749SThejaswini.Singarajipura@Sun.COM 				    sizeof (cli_addr));
12257749SThejaswini.Singarajipura@Sun.COM 			}
12264235Smarkfen 
12274235Smarkfen 			time_critical_enter();
12284235Smarkfen 			do {
12294235Smarkfen 				rc = read(keysock, buffer, buffer_size);
12304235Smarkfen 				if (rc == -1)
12314235Smarkfen 					Bail("read (in doaddresses)");
12324235Smarkfen 			} while (msgp->sadb_msg_seq != seq ||
12334235Smarkfen 			    msgp->sadb_msg_pid != mypid);
12344235Smarkfen 			time_critical_exit();
12354235Smarkfen 
12364235Smarkfen 			if (msgp->sadb_msg_type != sadb_msg_type ||
12374235Smarkfen 			    msgp->sadb_msg_satype != sadb_msg_satype) {
12384235Smarkfen 				syslog((LOG_NOTICE|LOG_AUTH), gettext(
12394235Smarkfen 				    "doaddresses: Unexpected returned message "
12404235Smarkfen 				    "(%d exp %d)\n"), msgp->sadb_msg_type,
12414235Smarkfen 				    sadb_msg_type);
12424235Smarkfen 				Bail("doaddresses: Unexpected returned "
12434235Smarkfen 				    "message");
12444235Smarkfen 			}
12454235Smarkfen 
12464235Smarkfen 			errno = msgp->sadb_msg_errno;
12474235Smarkfen 			if (errno != 0) {
12484235Smarkfen 				if (errno == EINVAL) {
12494235Smarkfen 					WARN(ep, ebuf, gettext(
12504235Smarkfen 					    "One of the entered "
12514235Smarkfen 					    "values is incorrect."));
12524235Smarkfen 					print_diagnostic(stderr,
12534235Smarkfen 					    msgp->sadb_x_msg_diagnostic);
12544235Smarkfen 				} else {
12554235Smarkfen 					Bail("return message (in doaddresses)");
12564235Smarkfen 				}
12574235Smarkfen 			}
12584235Smarkfen 
12594235Smarkfen 			/* ...and then restore the saved buffer. */
12604235Smarkfen 			msgp = (struct sadb_msg *)savebuf;
12614235Smarkfen 			bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
12624235Smarkfen 		} while (srchp != NULL && srchp->h_addr_list[++i] != NULL);
12634235Smarkfen 		return;
12644235Smarkfen 	}
12654235Smarkfen 
12664235Smarkfen 	single_dst = (dsthp == &dummy.he || dsthp->h_addr_list[1] == NULL);
12674235Smarkfen 
12684235Smarkfen 	for (i = 0; dsthp->h_addr_list[i] != NULL; i++) {
12694235Smarkfen 		if (dsthp == &dummy.he) {
12704235Smarkfen 			/* Just to be sure... */
12714235Smarkfen 			dsthp->h_addr_list[1] = NULL;
12724235Smarkfen 		} else {
12734235Smarkfen 			/*
12744235Smarkfen 			 * Fill in the dst sockaddr.
12754235Smarkfen 			 */
12764235Smarkfen 			sin6 = (struct sockaddr_in6 *)(dst + 1);
12774235Smarkfen 			bzero(sin6, sizeof (*sin6));
12784235Smarkfen 			bcopy(dsthp->h_addr_list[i], &sin6->sin6_addr,
12794235Smarkfen 			    sizeof (struct in6_addr));
12804235Smarkfen 			sin6->sin6_family = AF_INET6;
12814235Smarkfen 			sin6->sin6_port = htons(dstport);
12824235Smarkfen 		}
12834235Smarkfen 
12844235Smarkfen 		/*
12854235Smarkfen 		 * Try and assign src, if there's any ambiguity.
12864235Smarkfen 		 */
12874235Smarkfen 		if (!unspec_src && srchp != &dummy.he) {
12884235Smarkfen 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12894235Smarkfen 				/*
12904235Smarkfen 				 * IPv4 address.  Find an IPv4 address, then
12914235Smarkfen 				 * keep looking for a second one.  If a second
12924235Smarkfen 				 * exists, print a message, and fill in the
12934235Smarkfen 				 * unspecified address.
12944235Smarkfen 				 */
12954235Smarkfen 				first_match = NULL;
12964235Smarkfen 
12974235Smarkfen 				for (walker = srchp->h_addr_list;
12984235Smarkfen 				    *walker != NULL; walker++) {
12994235Smarkfen 					/* LINTED E_BAD_PTR_CAST_ALIGN */
13004235Smarkfen 					if (IN6_IS_ADDR_V4MAPPED(
13014235Smarkfen 					    (struct in6_addr *)*walker)) {
13024235Smarkfen 						if (first_match != NULL)
13034235Smarkfen 							break;
13044235Smarkfen 						else
13054235Smarkfen 							first_match = *walker;
13064235Smarkfen 					}
13074235Smarkfen 				}
13084235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
13094235Smarkfen 				bzero(sin6, sizeof (*sin6));
13104235Smarkfen 
13114235Smarkfen 				if (first_match == NULL) {
13124235Smarkfen 					/*
13134235Smarkfen 					 * No IPv4 hits.  Is this a single
13144235Smarkfen 					 * dest?
13154235Smarkfen 					 */
13164235Smarkfen 					WARN1(ep, ebuf, gettext(
13174235Smarkfen 					    "No IPv4 source address "
13184235Smarkfen 					    "for name %s.\n"), srchp->h_name);
13194235Smarkfen 					if (single_dst) {
13204235Smarkfen 						ERROR(ep, ebuf, gettext(
13214235Smarkfen 						    "Only single destination "
13224235Smarkfen 						    "IP address.\n"));
13234235Smarkfen 					} else {
13244235Smarkfen 						/* Continue, but do I print? */
13254235Smarkfen 						continue;  /* for loop */
13264235Smarkfen 					}
13274235Smarkfen 
13284235Smarkfen 					/* I should never reach here. */
13294235Smarkfen 				}
13304235Smarkfen 
13314235Smarkfen 				sin6->sin6_family = AF_INET6;
13324235Smarkfen 				sin6->sin6_port = htons(srcport);
13334235Smarkfen 				if (*walker != NULL) {
13344235Smarkfen 					/*
13354235Smarkfen 					 * Early loop exit.  It must've been
13364235Smarkfen 					 * multiple hits...
13374235Smarkfen 					 *
13384235Smarkfen 					 * Issue a null-source warning?
13394235Smarkfen 					 */
13404235Smarkfen 					WARN1(ep, ebuf, gettext(
13414235Smarkfen 					    "Multiple IPv4 source addresses "
13424235Smarkfen 					    "for %s, using unspecified source "
13434235Smarkfen 					    "instead."), srchp->h_name);
13444235Smarkfen 				} else {
13454235Smarkfen 					/*
13464235Smarkfen 					 * If I reach here w/o hitting the
13474235Smarkfen 					 * previous if statements, I have a
13484235Smarkfen 					 * single source address for this
13494235Smarkfen 					 * destination.
13504235Smarkfen 					 */
13514235Smarkfen 					bcopy(first_match, &sin6->sin6_addr,
13524235Smarkfen 					    sizeof (struct in6_addr));
13534235Smarkfen 				}
13544235Smarkfen 			} else {
13554235Smarkfen 				/*
13564235Smarkfen 				 * IPv6 address.  Find an IPv6 address.
13574235Smarkfen 				 * Unlike IPv4 addresses, things can get a
13584235Smarkfen 				 * little more sticky with scopes, etc.
13594235Smarkfen 				 */
13604235Smarkfen 				int dst_scope, src_scope;
13614235Smarkfen 
13624235Smarkfen 				dst_scope = ipv6_addr_scope(&sin6->sin6_addr);
13634235Smarkfen 
13644235Smarkfen 				first_match = NULL;
13654235Smarkfen 				for (walker = srchp->h_addr_list;
13664235Smarkfen 				    *walker != NULL; walker++) {
13674235Smarkfen 					/* LINTED E_BAD_PTR_CAST_ALIGN */
13684235Smarkfen 					if (!IN6_IS_ADDR_V4MAPPED(
13694235Smarkfen 					    (struct in6_addr *)*walker)) {
13704235Smarkfen 						/*
13714235Smarkfen 						 * Set first-match, etc.
13724235Smarkfen 						 * Take into account scopes,
13734235Smarkfen 						 * and other IPv6 thingies.
13744235Smarkfen 						 */
13754235Smarkfen 						src_scope = ipv6_addr_scope(
13764235Smarkfen 						    /* LINTED E_BAD_PTR_CAST */
13774235Smarkfen 						    (struct in6_addr *)*walker);
13784235Smarkfen 						if (src_scope == SCOPE_UNSPEC ||
13794235Smarkfen 						    src_scope == dst_scope) {
13804235Smarkfen 							if (first_match !=
13814235Smarkfen 							    NULL)
13824235Smarkfen 								break;
13834235Smarkfen 							else
13844235Smarkfen 								first_match =
13854235Smarkfen 								    *walker;
13864235Smarkfen 						}
13874235Smarkfen 					}
13884235Smarkfen 				}
13894235Smarkfen 
13904235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
13914235Smarkfen 				bzero(sin6, sizeof (*sin6));
13924235Smarkfen 				sin6->sin6_port = htons(srcport);
13934235Smarkfen 				if (first_match == NULL) {
13944235Smarkfen 					/*
13954235Smarkfen 					 * No IPv6 hits.  Is this a single
13964235Smarkfen 					 * dest?
13974235Smarkfen 					 */
13984235Smarkfen 					WARN1(ep, ebuf, gettext(
13994235Smarkfen 					    "No IPv6 source address of "
14004235Smarkfen 					    "matching scope for name %s.\n"),
14014235Smarkfen 					    srchp->h_name);
14024235Smarkfen 					if (single_dst) {
14034235Smarkfen 						ERROR(ep, ebuf, gettext(
14044235Smarkfen 						    "Only a single IPV6 "
14054235Smarkfen 						    "destination "
14064235Smarkfen 						    "address.\n"));
14074235Smarkfen 					} else {
14084235Smarkfen 						/* Continue, but do I print? */
14094235Smarkfen 						continue;  /* for loop */
14104235Smarkfen 					}
14114235Smarkfen 
14124235Smarkfen 					/* I should never reach here. */
14134235Smarkfen 				}
14144235Smarkfen 				sin6->sin6_family = AF_INET6;
14154235Smarkfen 				if (*walker != NULL) {
14164235Smarkfen 					/*
14174235Smarkfen 					 * Early loop exit.  Issue a
14184235Smarkfen 					 * null-source warning?
14194235Smarkfen 					 */
14204235Smarkfen 					WARN1(ep, ebuf, gettext(
14214235Smarkfen 					    "Multiple IPv6 source addresses "
14224235Smarkfen 					    "for %s of the same scope, using "
14234235Smarkfen 					    "unspecified source instead.\n"),
14244235Smarkfen 					    srchp->h_name);
14254235Smarkfen 				} else {
14264235Smarkfen 					/*
14274235Smarkfen 					 * If I reach here w/o hitting the
14284235Smarkfen 					 * previous if statements, I have a
14294235Smarkfen 					 * single source address for this
14304235Smarkfen 					 * destination.
14314235Smarkfen 					 */
14324235Smarkfen 					bcopy(first_match, &sin6->sin6_addr,
14334235Smarkfen 					    sizeof (struct in6_addr));
14344235Smarkfen 				}
14354235Smarkfen 			}
14364235Smarkfen 		}
14374235Smarkfen 
14384235Smarkfen 		/*
14394235Smarkfen 		 * If there are errors at this point there is no
14404235Smarkfen 		 * point sending anything to PF_KEY.
14414235Smarkfen 		 */
14424235Smarkfen 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
14434235Smarkfen 
14444235Smarkfen 		/* Save off a copy for later writing... */
14454235Smarkfen 		msgp = (struct sadb_msg *)buffer;
14464235Smarkfen 		bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
14474235Smarkfen 
14484235Smarkfen 		rc = key_write(keysock, buffer, SADB_64TO8(msgp->sadb_msg_len));
14494235Smarkfen 		if (rc == -1)
14504235Smarkfen 			Bail("write() to PF_KEY socket (in doaddresses)");
14514235Smarkfen 
14527749SThejaswini.Singarajipura@Sun.COM 		if (in_cluster_mode) {
14537749SThejaswini.Singarajipura@Sun.COM 			(void) sendto(cluster_socket, buffer,
14547749SThejaswini.Singarajipura@Sun.COM 			    SADB_64TO8(msgp->sadb_msg_len), 0,
14557749SThejaswini.Singarajipura@Sun.COM 			    (struct sockaddr *)&cli_addr,
14567749SThejaswini.Singarajipura@Sun.COM 			    sizeof (cli_addr));
14577749SThejaswini.Singarajipura@Sun.COM 		}
14584235Smarkfen 		/* Blank the key for paranoia's sake. */
14594235Smarkfen 		bzero(buffer, buffer_size);
14604235Smarkfen 		time_critical_enter();
14614235Smarkfen 		do {
14624235Smarkfen 			rc = read(keysock, buffer, buffer_size);
14634235Smarkfen 			if (rc == -1)
14644235Smarkfen 				Bail("read (in doaddresses)");
14654235Smarkfen 		} while (msgp->sadb_msg_seq != seq ||
14664235Smarkfen 		    msgp->sadb_msg_pid != mypid);
14674235Smarkfen 		time_critical_exit();
14684235Smarkfen 
14694235Smarkfen 		/*
14704235Smarkfen 		 * I should _never_ hit the following unless:
14714235Smarkfen 		 *
14724235Smarkfen 		 * 1. There is a kernel bug.
14734235Smarkfen 		 * 2. Another process is mistakenly using my pid in a PF_KEY
14744235Smarkfen 		 *    message.
14754235Smarkfen 		 */
14764235Smarkfen 		if (msgp->sadb_msg_type != sadb_msg_type ||
14774235Smarkfen 		    msgp->sadb_msg_satype != sadb_msg_satype) {
14784235Smarkfen 			syslog((LOG_NOTICE|LOG_AUTH), gettext(
14794235Smarkfen 			    "doaddresses: Unexpected returned message "
14804235Smarkfen 			    "(%d exp %d)\n"), msgp->sadb_msg_type,
14814235Smarkfen 			    sadb_msg_type);
14824235Smarkfen 			Bail("doaddresses: Unexpected returned message");
14834235Smarkfen 		}
14844235Smarkfen 
14854235Smarkfen 		if (msgp->sadb_msg_errno != 0) {
14864235Smarkfen 			char addrprint[INET6_ADDRSTRLEN];
14874235Smarkfen 			int on_errno = 0;
14884235Smarkfen 			char *on_errno_msg;
14894235Smarkfen 
14904235Smarkfen 			/*
14914235Smarkfen 			 * Print different error messages depending
14924235Smarkfen 			 * on the SADB message type being processed.
14934235Smarkfen 			 * If we get a ESRCH error for a GET/DELETE
14944235Smarkfen 			 * messages, we report that the SA does not
14954235Smarkfen 			 * exist. If we get a EEXIST error for a
14964235Smarkfen 			 * ADD/UPDATE message, we report that the
14974235Smarkfen 			 * SA already exists.
14984235Smarkfen 			 */
14994235Smarkfen 			if (sadb_msg_type == SADB_GET ||
15004235Smarkfen 			    sadb_msg_type == SADB_DELETE) {
15014235Smarkfen 				on_errno = ESRCH;
15024235Smarkfen 				on_errno_msg = "does not exist";
15034235Smarkfen 			} else if (sadb_msg_type == SADB_ADD ||
15044235Smarkfen 			    sadb_msg_type == SADB_UPDATE) {
15054235Smarkfen 				on_errno = EEXIST;
15064235Smarkfen 				on_errno_msg = "already exists";
15074235Smarkfen 			}
15084235Smarkfen 
15094235Smarkfen 			errno = msgp->sadb_msg_errno;
15104235Smarkfen 			if (errno == on_errno) {
15114235Smarkfen 				ERROR2(ep, ebuf, gettext(
15124235Smarkfen 				    "Association (type = %s) "
15134235Smarkfen 				    "with spi 0x%x and addr\n"),
15144235Smarkfen 				    rparsesatype(msgp->sadb_msg_satype),
15154235Smarkfen 				    ntohl(spi));
15164235Smarkfen 				ERROR2(ep, ebuf, "%s %s.\n",
15174235Smarkfen 				    do_inet_ntop(dsthp->h_addr_list[i],
15184342Spwernau 				    addrprint, sizeof (addrprint)),
15194235Smarkfen 				    on_errno_msg);
15204235Smarkfen 				msgp = (struct sadb_msg *)savebuf;
15214235Smarkfen 				bcopy(savebuf, buffer,
15224235Smarkfen 				    SADB_64TO8(msgp->sadb_msg_len));
15234235Smarkfen 				continue;
15244235Smarkfen 			} else {
15256668Smarkfen 				if (errno == EINVAL || errno == ESRCH) {
15264235Smarkfen 					ERROR2(ep, ebuf, gettext(
15274235Smarkfen 					    "PF_KEY Diagnostic code %u: %s.\n"),
15284235Smarkfen 					    msgp->sadb_x_msg_diagnostic,
15294235Smarkfen 					    keysock_diag(
15304235Smarkfen 					    msgp->sadb_x_msg_diagnostic));
15314235Smarkfen 				} else {
15324235Smarkfen 					Bail("return message (in doaddresses)");
15334235Smarkfen 				}
15344235Smarkfen 			}
15354235Smarkfen 		}
15364235Smarkfen 
15374235Smarkfen 		if (cmd == CMD_GET) {
15384235Smarkfen 			if (msgp->sadb_msg_len > MAX_GET_SIZE) {
15394235Smarkfen 				WARN1(ep, ebuf, gettext("WARNING:  "
15404235Smarkfen 				    "SA information bigger than %d bytes.\n"),
15414235Smarkfen 				    SADB_64TO8(MAX_GET_SIZE));
15424235Smarkfen 			}
15434867Spwernau 			print_samsg(stdout, buffer, B_FALSE, vflag, nflag);
15444235Smarkfen 		}
15454235Smarkfen 
15464235Smarkfen 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
15474235Smarkfen 
15484235Smarkfen 		/* ...and then restore the saved buffer. */
15494235Smarkfen 		msgp = (struct sadb_msg *)savebuf;
15504235Smarkfen 		bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
15514235Smarkfen 		lines_added++;
15524235Smarkfen 	}
15534235Smarkfen 
15544235Smarkfen 	/* Degenerate case, h_addr_list[0] == NULL. */
15554235Smarkfen 	if (i == 0)
15564235Smarkfen 		Bail("Empty destination address list");
15574235Smarkfen 
15584235Smarkfen 	/*
15594235Smarkfen 	 * free(ebuf) even if there are no errors.
15604235Smarkfen 	 * handle_errors() won't return here.
15614235Smarkfen 	 */
15624235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_TRUE);
15634235Smarkfen }
15644235Smarkfen 
15654235Smarkfen /*
15664235Smarkfen  * Perform an add or an update.  ADD and UPDATE are similar in the extensions
15674235Smarkfen  * they need.
15684235Smarkfen  */
15694235Smarkfen static void
15704235Smarkfen doaddup(int cmd, int satype, char *argv[], char *ebuf)
15714235Smarkfen {
15724235Smarkfen 	uint64_t *buffer, *nexthdr;
15734235Smarkfen 	struct sadb_msg msg;
15744235Smarkfen 	struct sadb_sa *assoc = NULL;
15756668Smarkfen 	struct sadb_x_pair *sadb_pair = NULL;
15764235Smarkfen 	struct sadb_address *src = NULL, *dst = NULL;
15774235Smarkfen 	struct sadb_address *isrc = NULL, *idst = NULL;
15784235Smarkfen 	struct sadb_address *natt_local = NULL, *natt_remote = NULL;
15794235Smarkfen 	struct sadb_key *encrypt = NULL, *auth = NULL;
15804235Smarkfen 	struct sadb_ident *srcid = NULL, *dstid = NULL;
15814235Smarkfen 	struct sadb_lifetime *hard = NULL, *soft = NULL;  /* Current? */
15827749SThejaswini.Singarajipura@Sun.COM 	struct sadb_lifetime *idle = NULL;
15837749SThejaswini.Singarajipura@Sun.COM 	struct sadb_x_replay_ctr *replay_ctr = NULL;
15844235Smarkfen 	struct sockaddr_in6 *sin6;
15854235Smarkfen 	/* MLS TODO:  Need sensitivity eventually. */
15864235Smarkfen 	int next, token, sa_len, alloclen, totallen = sizeof (msg), prefix;
15875989Spwernau 	uint32_t spi = 0;
15886668Smarkfen 	uint8_t	sadb_msg_type;
15894235Smarkfen 	char *thiscmd, *pstr;
15904235Smarkfen 	boolean_t readstate = B_FALSE, unspec_src = B_FALSE;
15914235Smarkfen 	boolean_t alloc_inner = B_FALSE, use_natt = B_FALSE;
15924235Smarkfen 	struct hostent *srchp = NULL, *dsthp = NULL, *isrchp = NULL,
15934235Smarkfen 	    *idsthp = NULL;
15944235Smarkfen 	struct hostent *natt_lhp = NULL, *natt_rhp = NULL;
15954235Smarkfen 	uint16_t srcport = 0, dstport = 0, natt_lport = 0, natt_rport = 0,
15964235Smarkfen 	    isrcport = 0, idstport = 0;
15974235Smarkfen 	uint8_t proto = 0, iproto = 0;
15984235Smarkfen 	char *ep = NULL;
15994235Smarkfen 
16006668Smarkfen 	switch (cmd) {
16016668Smarkfen 	case CMD_ADD:
16026668Smarkfen 		thiscmd = "add";
16036668Smarkfen 		sadb_msg_type = SADB_ADD;
16046668Smarkfen 		break;
16056668Smarkfen 	case CMD_UPDATE:
16066668Smarkfen 		thiscmd = "update";
16076668Smarkfen 		sadb_msg_type = SADB_UPDATE;
16086668Smarkfen 		break;
16096668Smarkfen 	case CMD_UPDATE_PAIR:
16106668Smarkfen 		thiscmd = "update-pair";
16116668Smarkfen 		sadb_msg_type = SADB_X_UPDATEPAIR;
16126668Smarkfen 		break;
16136668Smarkfen 	}
16144235Smarkfen 
16156668Smarkfen 	msg_init(&msg, sadb_msg_type, (uint8_t)satype);
16164235Smarkfen 	/* Assume last element in argv is set to NULL. */
16174235Smarkfen 	do {
16184235Smarkfen 		token = parseextval(*argv, &next);
16194235Smarkfen 		argv++;
16204235Smarkfen 		switch (token) {
16214235Smarkfen 		case TOK_EOF:
16224235Smarkfen 			/* Do nothing, I'm done. */
16234235Smarkfen 			break;
16244235Smarkfen 		case TOK_UNKNOWN:
16254235Smarkfen 			ERROR1(ep, ebuf, gettext(
16264235Smarkfen 			    "Unknown extension field \"%s\" \n"), *(argv - 1));
16274235Smarkfen 			break;
16284235Smarkfen 		case TOK_SPI:
16296668Smarkfen 		case TOK_PAIR_SPI:
16304235Smarkfen 		case TOK_REPLAY:
16314235Smarkfen 		case TOK_STATE:
16324235Smarkfen 		case TOK_AUTHALG:
16334235Smarkfen 		case TOK_ENCRALG:
16344235Smarkfen 		case TOK_ENCAP:
16354235Smarkfen 			/*
16364235Smarkfen 			 * May want to place this chunk of code in a function.
16374235Smarkfen 			 *
16384235Smarkfen 			 * This code checks for duplicate entries on a command
16394235Smarkfen 			 * line.
16404235Smarkfen 			 */
16414235Smarkfen 
16424235Smarkfen 			/* Allocate the SADB_EXT_SA extension. */
16434235Smarkfen 			if (assoc == NULL) {
16444235Smarkfen 				assoc = malloc(sizeof (*assoc));
16454235Smarkfen 				if (assoc == NULL)
16464235Smarkfen 					Bail("malloc(assoc)");
16474235Smarkfen 				bzero(assoc, sizeof (*assoc));
16484235Smarkfen 				assoc->sadb_sa_exttype = SADB_EXT_SA;
16494235Smarkfen 				assoc->sadb_sa_len =
16504235Smarkfen 				    SADB_8TO64(sizeof (*assoc));
16514235Smarkfen 				totallen += sizeof (*assoc);
16524235Smarkfen 			}
16534235Smarkfen 			switch (token) {
16544235Smarkfen 			case TOK_SPI:
16554235Smarkfen 				/*
16564235Smarkfen 				 * If some cretin types in "spi 0" then he/she
16574235Smarkfen 				 * can type in another SPI.
16584235Smarkfen 				 */
16594235Smarkfen 				if (assoc->sadb_sa_spi != 0) {
16604235Smarkfen 					ERROR(ep, ebuf, gettext(
16614235Smarkfen 					    "Can only specify "
16624235Smarkfen 					    "single SPI value.\n"));
16634235Smarkfen 					break;
16644235Smarkfen 				}
16654235Smarkfen 				/* Must convert SPI to network order! */
16664235Smarkfen 				assoc->sadb_sa_spi =
16674235Smarkfen 				    htonl((uint32_t)parsenum(*argv, B_TRUE,
16684235Smarkfen 				    ebuf));
16694235Smarkfen 				if (assoc->sadb_sa_spi == 0) {
16704235Smarkfen 					ERROR(ep, ebuf, gettext(
16714235Smarkfen 					    "Invalid SPI value \"0\" .\n"));
16724235Smarkfen 				}
16734235Smarkfen 				break;
16746668Smarkfen 			case TOK_PAIR_SPI:
16756668Smarkfen 				if (cmd == CMD_UPDATE_PAIR) {
16766668Smarkfen 					ERROR(ep, ebuf, gettext(
16776668Smarkfen 					    "pair-spi can not be used with the "
16786668Smarkfen 					    "\"update-pair\" command.\n"));
16796668Smarkfen 				}
16806668Smarkfen 				if (sadb_pair == NULL) {
16816668Smarkfen 					sadb_pair = malloc(sizeof (*sadb_pair));
16826668Smarkfen 					if (assoc == NULL)
16836668Smarkfen 						Bail("malloc(assoc)");
16846668Smarkfen 					bzero(sadb_pair, sizeof (*sadb_pair));
16856668Smarkfen 					totallen += sizeof (*sadb_pair);
16866668Smarkfen 				}
16876668Smarkfen 				if (sadb_pair->sadb_x_pair_spi != 0) {
16886668Smarkfen 					ERROR(ep, ebuf, gettext(
16896668Smarkfen 					    "Can only specify "
16906668Smarkfen 					    "single pair SPI value.\n"));
16916668Smarkfen 					break;
16926668Smarkfen 				}
16936668Smarkfen 				/* Must convert SPI to network order! */
16946668Smarkfen 				sadb_pair->sadb_x_pair_len =
16956668Smarkfen 				    SADB_8TO64(sizeof (*sadb_pair));
16966668Smarkfen 				sadb_pair->sadb_x_pair_exttype =
16976668Smarkfen 				    SADB_X_EXT_PAIR;
16986668Smarkfen 				sadb_pair->sadb_x_pair_spi =
16996668Smarkfen 				    htonl((uint32_t)parsenum(*argv, B_TRUE,
17006668Smarkfen 				    ebuf));
17016668Smarkfen 				if (sadb_pair->sadb_x_pair_spi == 0) {
17026668Smarkfen 					ERROR(ep, ebuf, gettext(
17036668Smarkfen 					    "Invalid SPI value \"0\" .\n"));
17046668Smarkfen 				}
17056668Smarkfen 				assoc->sadb_sa_flags |=
17066668Smarkfen 				    SADB_X_SAFLAGS_PAIRED;
17076668Smarkfen 				break;
17084235Smarkfen 			case TOK_REPLAY:
17094235Smarkfen 				/*
17104235Smarkfen 				 * That same cretin can do the same with
17114235Smarkfen 				 * replay.
17124235Smarkfen 				 */
17134235Smarkfen 				if (assoc->sadb_sa_replay != 0) {
17144235Smarkfen 					ERROR(ep, ebuf, gettext(
17154235Smarkfen 					    "Can only specify "
17164235Smarkfen 					    "single replay window size.\n"));
17174235Smarkfen 					break;
17184235Smarkfen 				}
17194235Smarkfen 				assoc->sadb_sa_replay =
17204235Smarkfen 				    (uint8_t)parsenum(*argv, B_TRUE, ebuf);
17214235Smarkfen 				if (assoc->sadb_sa_replay != 0) {
17224235Smarkfen 					WARN(ep, ebuf, gettext(
17234235Smarkfen 					    "WARNING:  Replay with manual"
17244235Smarkfen 					    " keying considered harmful.\n"));
17254235Smarkfen 				}
17264235Smarkfen 				break;
17274235Smarkfen 			case TOK_STATE:
17284235Smarkfen 				/*
17294235Smarkfen 				 * 0 is an actual state value, LARVAL.  This
17304235Smarkfen 				 * means that one can type in the larval state
17314235Smarkfen 				 * and then type in another state on the same
17324235Smarkfen 				 * command line.
17334235Smarkfen 				 */
17344235Smarkfen 				if (assoc->sadb_sa_state != 0) {
17354235Smarkfen 					ERROR(ep, ebuf, gettext(
17364235Smarkfen 					    "Can only specify "
17374235Smarkfen 					    "single SA state.\n"));
17384235Smarkfen 					break;
17394235Smarkfen 				}
17404235Smarkfen 				assoc->sadb_sa_state = parsestate(*argv,
17414235Smarkfen 				    ebuf);
17424235Smarkfen 				readstate = B_TRUE;
17434235Smarkfen 				break;
17444235Smarkfen 			case TOK_AUTHALG:
17454235Smarkfen 				if (assoc->sadb_sa_auth != 0) {
17464235Smarkfen 					ERROR(ep, ebuf, gettext(
17474235Smarkfen 					    "Can only specify "
17484235Smarkfen 					    "single auth algorithm.\n"));
17494235Smarkfen 					break;
17504235Smarkfen 				}
17514235Smarkfen 				assoc->sadb_sa_auth = parsealg(*argv,
17524235Smarkfen 				    IPSEC_PROTO_AH, ebuf);
17534235Smarkfen 				break;
17544235Smarkfen 			case TOK_ENCRALG:
17554235Smarkfen 				if (satype == SADB_SATYPE_AH) {
17564235Smarkfen 					ERROR(ep, ebuf, gettext("Cannot specify"
17574235Smarkfen 					    " encryption with SA type ah.\n"));
17584235Smarkfen 					break;
17594235Smarkfen 				}
17604235Smarkfen 				if (assoc->sadb_sa_encrypt != 0) {
17614235Smarkfen 					ERROR(ep, ebuf, gettext(
17624235Smarkfen 					    "Can only specify "
17634235Smarkfen 					    "single encryption algorithm.\n"));
17644235Smarkfen 					break;
17654235Smarkfen 				}
17664235Smarkfen 				assoc->sadb_sa_encrypt = parsealg(*argv,
17674235Smarkfen 				    IPSEC_PROTO_ESP, ebuf);
17684235Smarkfen 				break;
17694235Smarkfen 			case TOK_ENCAP:
17704235Smarkfen 				if (use_natt) {
17714235Smarkfen 					ERROR(ep, ebuf, gettext(
17724235Smarkfen 					    "Can only specify single"
17734235Smarkfen 					    " encapsulation.\n"));
17744235Smarkfen 					break;
17754235Smarkfen 				}
17764235Smarkfen 				if (strncmp(*argv, "udp", 3)) {
17774235Smarkfen 					ERROR(ep, ebuf, gettext(
17784235Smarkfen 					    "Can only specify udp"
17794235Smarkfen 					    " encapsulation.\n"));
17804235Smarkfen 					break;
17814235Smarkfen 				}
17824235Smarkfen 				use_natt = B_TRUE;
17834235Smarkfen 				/* set assoc flags later */
17844235Smarkfen 				break;
17854235Smarkfen 			}
17864235Smarkfen 			argv++;
17874235Smarkfen 			break;
17884235Smarkfen 		case TOK_SRCPORT:
17894235Smarkfen 			if (srcport != 0) {
17904235Smarkfen 				ERROR(ep, ebuf,  gettext("Can only specify "
17914235Smarkfen 				    "single source port.\n"));
17924235Smarkfen 				break;
17934235Smarkfen 			}
17944235Smarkfen 			srcport = parsenum(*argv, B_TRUE, ebuf);
17954235Smarkfen 			argv++;
17964235Smarkfen 			break;
17974235Smarkfen 		case TOK_DSTPORT:
17984235Smarkfen 			if (dstport != 0) {
17994235Smarkfen 				ERROR(ep, ebuf, gettext("Can only specify "
18004235Smarkfen 				    "single destination port.\n"));
18014235Smarkfen 				break;
18024235Smarkfen 			}
18034235Smarkfen 			dstport = parsenum(*argv, B_TRUE, ebuf);
18044235Smarkfen 			argv++;
18054235Smarkfen 			break;
18064235Smarkfen 		case TOK_ISRCPORT:
18074235Smarkfen 			alloc_inner = B_TRUE;
18084235Smarkfen 			if (isrcport != 0) {
18094235Smarkfen 				ERROR(ep, ebuf, gettext(
18104235Smarkfen 				    "Can only specify "
18114235Smarkfen 				    "single inner-source port.\n"));
18124235Smarkfen 				break;
18134235Smarkfen 			}
18144235Smarkfen 			isrcport = parsenum(*argv, B_TRUE, ebuf);
18154235Smarkfen 			argv++;
18164235Smarkfen 			break;
18174235Smarkfen 		case TOK_IDSTPORT:
18184235Smarkfen 			alloc_inner = B_TRUE;
18194235Smarkfen 			if (idstport != 0) {
18204235Smarkfen 				ERROR(ep, ebuf, gettext(
18214235Smarkfen 				    "Can only specify "
18224235Smarkfen 				    "single inner-destination port.\n"));
18234235Smarkfen 				break;
18244235Smarkfen 			}
18254235Smarkfen 			idstport = parsenum(*argv, B_TRUE, ebuf);
18264235Smarkfen 			argv++;
18274235Smarkfen 			break;
18284235Smarkfen 		case TOK_NATLPORT:
18294235Smarkfen 			if (natt_lport != 0) {
18304235Smarkfen 				ERROR(ep, ebuf, gettext(
18314235Smarkfen 				    "Can only specify "
18324235Smarkfen 				    "single NAT-T local port.\n"));
18334235Smarkfen 				break;
18344235Smarkfen 			}
18354235Smarkfen 			natt_lport = parsenum(*argv, B_TRUE, ebuf);
18364235Smarkfen 			argv++;
18374235Smarkfen 			break;
18384235Smarkfen 		case TOK_NATRPORT:
18394235Smarkfen 			if (natt_rport != 0) {
18404235Smarkfen 				ERROR(ep, ebuf, gettext(
18414235Smarkfen 				    "Can only specify "
18424235Smarkfen 				    "single NAT-T remote port.\n"));
18434235Smarkfen 				break;
18444235Smarkfen 			}
18454235Smarkfen 			natt_rport = parsenum(*argv, B_TRUE, ebuf);
18464235Smarkfen 			argv++;
18474235Smarkfen 			break;
18484235Smarkfen 
18494235Smarkfen 		case TOK_PROTO:
18504235Smarkfen 			if (proto != 0) {
18514235Smarkfen 				ERROR(ep, ebuf, gettext(
18524235Smarkfen 				    "Can only specify "
18534235Smarkfen 				    "single protocol.\n"));
18544235Smarkfen 				break;
18554235Smarkfen 			}
18564235Smarkfen 			proto = parsenum(*argv, B_TRUE, ebuf);
18574235Smarkfen 			argv++;
18584235Smarkfen 			break;
18594235Smarkfen 		case TOK_IPROTO:
18604235Smarkfen 			alloc_inner = B_TRUE;
18614235Smarkfen 			if (iproto != 0) {
18624235Smarkfen 				ERROR(ep, ebuf, gettext(
18634235Smarkfen 				    "Can only specify "
18644235Smarkfen 				    "single inner protocol.\n"));
18654235Smarkfen 				break;
18664235Smarkfen 			}
18674235Smarkfen 			iproto = parsenum(*argv, B_TRUE, ebuf);
18684235Smarkfen 			argv++;
18694235Smarkfen 			break;
18704235Smarkfen 		case TOK_SRCADDR:
18714235Smarkfen 		case TOK_SRCADDR6:
18724235Smarkfen 			if (src != NULL) {
18734235Smarkfen 				ERROR(ep, ebuf, gettext(
18744235Smarkfen 				    "Can only specify "
18754235Smarkfen 				    "single source address.\n"));
18764235Smarkfen 				break;
18774235Smarkfen 			}
18784235Smarkfen 			sa_len = parseaddr(*argv, &srchp,
18794235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
18804235Smarkfen 			if (srchp == NULL) {
18814235Smarkfen 				ERROR1(ep, ebuf, gettext(
18824235Smarkfen 				    "Unknown src address \"%s\"\n"), *argv);
18834235Smarkfen 				break;
18844235Smarkfen 			}
18854235Smarkfen 			argv++;
18864235Smarkfen 			/*
18874235Smarkfen 			 * Round of the sockaddr length to an 8 byte
18884235Smarkfen 			 * boundary to make PF_KEY happy.
18894235Smarkfen 			 */
18904235Smarkfen 			alloclen = sizeof (*src) + roundup(sa_len, 8);
18914235Smarkfen 			src = malloc(alloclen);
18924235Smarkfen 			if (src == NULL)
18934235Smarkfen 				Bail("malloc(src)");
18944235Smarkfen 			totallen += alloclen;
18954235Smarkfen 			src->sadb_address_len = SADB_8TO64(alloclen);
18964235Smarkfen 			src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
18974235Smarkfen 			src->sadb_address_reserved = 0;
18984235Smarkfen 			src->sadb_address_prefixlen = 0;
18994235Smarkfen 			src->sadb_address_proto = 0;
19004235Smarkfen 			if (srchp == &dummy.he) {
19014235Smarkfen 				/*
19024235Smarkfen 				 * Single address with -n flag.
19034235Smarkfen 				 */
19044235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
19054235Smarkfen 				bzero(sin6, sizeof (*sin6));
19064235Smarkfen 				sin6->sin6_family = AF_INET6;
19074235Smarkfen 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
19084235Smarkfen 				    sizeof (struct in6_addr));
19094235Smarkfen 			}
19104235Smarkfen 			break;
19114235Smarkfen 		case TOK_DSTADDR:
19124235Smarkfen 		case TOK_DSTADDR6:
19134235Smarkfen 			if (dst != NULL) {
19144235Smarkfen 				ERROR(ep, ebuf, gettext(
19154235Smarkfen 				    "Can only specify single "
19164235Smarkfen 				    "destination address.\n"));
19174235Smarkfen 				break;
19184235Smarkfen 			}
19194235Smarkfen 			sa_len = parseaddr(*argv, &dsthp,
19204235Smarkfen 			    (token == TOK_DSTADDR6), ebuf);
19214235Smarkfen 			if (dsthp == NULL) {
19224235Smarkfen 				ERROR1(ep, ebuf, gettext(
19234235Smarkfen 				    "Unknown dst address \"%s\"\n"), *argv);
19244235Smarkfen 				break;
19254235Smarkfen 			}
19264235Smarkfen 			argv++;
19274235Smarkfen 			alloclen = sizeof (*dst) + roundup(sa_len, 8);
19284235Smarkfen 			dst = malloc(alloclen);
19294235Smarkfen 			if (dst == NULL)
19304235Smarkfen 				Bail("malloc(dst)");
19314235Smarkfen 			totallen += alloclen;
19324235Smarkfen 			dst->sadb_address_len = SADB_8TO64(alloclen);
19334235Smarkfen 			dst->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
19344235Smarkfen 			dst->sadb_address_reserved = 0;
19354235Smarkfen 			dst->sadb_address_prefixlen = 0;
19364235Smarkfen 			dst->sadb_address_proto = 0;
19374235Smarkfen 			if (dsthp == &dummy.he) {
19384235Smarkfen 				/*
19394235Smarkfen 				 * Single address with -n flag.
19404235Smarkfen 				 */
19414235Smarkfen 				sin6 = (struct sockaddr_in6 *)(dst + 1);
19424235Smarkfen 				bzero(sin6, sizeof (*sin6));
19434235Smarkfen 				sin6->sin6_family = AF_INET6;
19444235Smarkfen 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
19454235Smarkfen 				    sizeof (struct in6_addr));
19464235Smarkfen 			}
19474235Smarkfen 			break;
19484235Smarkfen 		case TOK_PROXYADDR:
19494235Smarkfen 		case TOK_PROXYADDR6:
19504235Smarkfen 			if (isrc != NULL) {
19514235Smarkfen 				ERROR(ep, ebuf, gettext(
19524235Smarkfen 				    "Can only specify single "
19534235Smarkfen 				    "proxy/inner-source address.\n"));
19544235Smarkfen 				break;
19554235Smarkfen 			}
19564235Smarkfen 			if ((pstr = strchr(*argv, '/')) != NULL) {
19574235Smarkfen 				/* Parse out the prefix. */
19584235Smarkfen 				errno = 0;
19594235Smarkfen 				prefix = strtol(pstr + 1, NULL, 10);
19604235Smarkfen 				if (errno != 0) {
19614235Smarkfen 					ERROR1(ep, ebuf, gettext(
19624235Smarkfen 					    "Invalid prefix %s."), pstr);
19634235Smarkfen 					break;
19644235Smarkfen 				}
19654235Smarkfen 				/* Recycle pstr */
19664235Smarkfen 				alloclen = (int)(pstr - *argv);
19674235Smarkfen 				pstr = malloc(alloclen + 1);
19684235Smarkfen 				if (pstr == NULL) {
19694235Smarkfen 					Bail("malloc(pstr)");
19704235Smarkfen 				}
19714235Smarkfen 				(void) strlcpy(pstr, *argv, alloclen + 1);
19724235Smarkfen 			} else {
19734235Smarkfen 				pstr = *argv;
19744235Smarkfen 				/*
19754235Smarkfen 				 * Assume mapping to AF_INET6, and we're a host.
19764235Smarkfen 				 * XXX some miscreants may still make classful
19774235Smarkfen 				 * assumptions.  If this is a problem, fix it
19784235Smarkfen 				 * here.
19794235Smarkfen 				 */
19804235Smarkfen 				prefix = 128;
19814235Smarkfen 			}
19824235Smarkfen 			sa_len = parseaddr(pstr, &isrchp,
19834235Smarkfen 			    (token == TOK_PROXYADDR6), ebuf);
19844235Smarkfen 			if (isrchp == NULL) {
19854235Smarkfen 				ERROR1(ep, ebuf, gettext(
19864235Smarkfen 				    "Unknown proxy/inner-source address "
19874235Smarkfen 				    "\"%s\"\n"), *argv);
19884235Smarkfen 				break;
19894235Smarkfen 			}
19904235Smarkfen 			if (pstr != *argv)
19914235Smarkfen 				free(pstr);
19924235Smarkfen 			argv++;
19934235Smarkfen 			alloclen = sizeof (*isrc) + roundup(sa_len, 8);
19944235Smarkfen 			isrc = malloc(alloclen);
19954235Smarkfen 			if (isrc == NULL)
19964235Smarkfen 				Bail("malloc(isrc)");
19974235Smarkfen 			totallen += alloclen;
19984235Smarkfen 			isrc->sadb_address_len = SADB_8TO64(alloclen);
19994235Smarkfen 			isrc->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
20004235Smarkfen 			isrc->sadb_address_reserved = 0;
20014235Smarkfen 			isrc->sadb_address_prefixlen = prefix;
20024235Smarkfen 			isrc->sadb_address_proto = 0;
20034235Smarkfen 			if (isrchp == &dummy.he ||
20044235Smarkfen 			    isrchp->h_addr_list[1] == NULL) {
20054235Smarkfen 				/*
20064235Smarkfen 				 * Single address with -n flag or single name.
20074235Smarkfen 				 */
20084235Smarkfen 				sin6 = (struct sockaddr_in6 *)(isrc + 1);
20094235Smarkfen 				bzero(sin6, sizeof (*sin6));
20104235Smarkfen 				sin6->sin6_family = AF_INET6;
20114235Smarkfen 				bcopy(isrchp->h_addr_list[0], &sin6->sin6_addr,
20124235Smarkfen 				    sizeof (struct in6_addr));
20134235Smarkfen 				/*
20144235Smarkfen 				 * normalize prefixlen for IPv4-mapped
20154235Smarkfen 				 * addresses.
20164235Smarkfen 				 */
20174235Smarkfen 				if (prefix <= 32 &&
20184235Smarkfen 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
20194235Smarkfen 					isrc->sadb_address_prefixlen += 96;
20204235Smarkfen 				alloc_inner = B_TRUE;
20214235Smarkfen 			} else {
20224235Smarkfen 				/*
20234235Smarkfen 				 * If the proxy/isrc address is vague, don't
20244235Smarkfen 				 * bother.
20254235Smarkfen 				 */
20264235Smarkfen 				totallen -= alloclen;
20274235Smarkfen 				free(isrc);
20284235Smarkfen 				isrc = NULL;
20294235Smarkfen 				WARN1(ep, ebuf, gettext(
20304235Smarkfen 				    "Proxy/inner-source address %s "
20314235Smarkfen 				    "is vague, not using.\n"), isrchp->h_name);
20324235Smarkfen 				freehostent(isrchp);
20334235Smarkfen 				isrchp = NULL;
20344235Smarkfen 				break;
20354235Smarkfen 			}
20364235Smarkfen 			break;
20374235Smarkfen 		case TOK_IDSTADDR:
20384235Smarkfen 		case TOK_IDSTADDR6:
20394235Smarkfen 			if (idst != NULL) {
20404235Smarkfen 				ERROR(ep, ebuf, gettext(
20414235Smarkfen 				    "Can only specify single "
20424235Smarkfen 				    "inner-destination address.\n"));
20434235Smarkfen 				break;
20444235Smarkfen 			}
20454235Smarkfen 			if ((pstr = strchr(*argv, '/')) != NULL) {
20464235Smarkfen 				/* Parse out the prefix. */
20474235Smarkfen 				errno = 0;
20484235Smarkfen 				prefix = strtol(pstr + 1, NULL, 10);
20494235Smarkfen 				if (errno != 0) {
20504235Smarkfen 					ERROR1(ep, ebuf, gettext(
20514235Smarkfen 					    "Invalid prefix %s.\n"), pstr);
20524235Smarkfen 					break;
20534235Smarkfen 				}
20544235Smarkfen 				/* Recycle pstr */
20554235Smarkfen 				alloclen = (int)(pstr - *argv);
20564235Smarkfen 				pstr = malloc(alloclen + 1);
20574235Smarkfen 				if (pstr == NULL) {
20584235Smarkfen 					Bail("malloc(pstr)");
20594235Smarkfen 				}
20604235Smarkfen 				(void) strlcpy(pstr, *argv, alloclen + 1);
20614235Smarkfen 			} else {
20624235Smarkfen 				pstr = *argv;
20634235Smarkfen 				/*
20644235Smarkfen 				 * Assume mapping to AF_INET6, and we're a host.
20654235Smarkfen 				 * XXX some miscreants may still make classful
20664235Smarkfen 				 * assumptions.  If this is a problem, fix it
20674235Smarkfen 				 * here.
20684235Smarkfen 				 */
20694235Smarkfen 				prefix = 128;
20704235Smarkfen 			}
20714235Smarkfen 			sa_len = parseaddr(pstr, &idsthp,
20724235Smarkfen 			    (token == TOK_IDSTADDR6), ebuf);
20734235Smarkfen 			if (idsthp == NULL) {
20744235Smarkfen 				ERROR1(ep, ebuf, gettext(
20754235Smarkfen 				    "Unknown Inner Src address "
20764235Smarkfen 				    " \"%s\"\n"), *argv);
20774235Smarkfen 				break;
20784235Smarkfen 			}
20794235Smarkfen 			if (pstr != *argv)
20804235Smarkfen 				free(pstr);
20814235Smarkfen 			argv++;
20824235Smarkfen 			alloclen = sizeof (*idst) + roundup(sa_len, 8);
20834235Smarkfen 			idst = malloc(alloclen);
20844235Smarkfen 			if (idst == NULL)
20854235Smarkfen 				Bail("malloc(idst)");
20864235Smarkfen 			totallen += alloclen;
20874235Smarkfen 			idst->sadb_address_len = SADB_8TO64(alloclen);
20884235Smarkfen 			idst->sadb_address_exttype =
20894235Smarkfen 			    SADB_X_EXT_ADDRESS_INNER_DST;
20904235Smarkfen 			idst->sadb_address_reserved = 0;
20914235Smarkfen 			idst->sadb_address_prefixlen = prefix;
20924235Smarkfen 			idst->sadb_address_proto = 0;
20934235Smarkfen 			if (idsthp == &dummy.he ||
20944235Smarkfen 			    idsthp->h_addr_list[1] == NULL) {
20954235Smarkfen 				/*
20964235Smarkfen 				 * Single address with -n flag or single name.
20974235Smarkfen 				 */
20984235Smarkfen 				sin6 = (struct sockaddr_in6 *)(idst + 1);
20994235Smarkfen 				bzero(sin6, sizeof (*sin6));
21004235Smarkfen 				sin6->sin6_family = AF_INET6;
21014235Smarkfen 				bcopy(idsthp->h_addr_list[0], &sin6->sin6_addr,
21024235Smarkfen 				    sizeof (struct in6_addr));
21034235Smarkfen 				/*
21044235Smarkfen 				 * normalize prefixlen for IPv4-mapped
21054235Smarkfen 				 * addresses.
21064235Smarkfen 				 */
21074235Smarkfen 				if (prefix <= 32 &&
21084235Smarkfen 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
21094235Smarkfen 					idst->sadb_address_prefixlen += 96;
21104235Smarkfen 				alloc_inner = B_TRUE;
21114235Smarkfen 			} else {
21124235Smarkfen 				/*
21134235Smarkfen 				 * If the idst address is vague, don't bother.
21144235Smarkfen 				 */
21154235Smarkfen 				totallen -= alloclen;
21164235Smarkfen 				free(idst);
21174235Smarkfen 				idst = NULL;
21184235Smarkfen 				WARN1(ep, ebuf, gettext(
21194235Smarkfen 				    "Inner destination address %s "
21204235Smarkfen 				    "is vague, not using.\n"), idsthp->h_name);
21214235Smarkfen 				freehostent(idsthp);
21224235Smarkfen 				idsthp = NULL;
21234235Smarkfen 				break;
21244235Smarkfen 			}
21254235Smarkfen 			break;
21264235Smarkfen 		case TOK_NATLOC:
21274235Smarkfen 			if (natt_local != NULL) {
21284235Smarkfen 				ERROR(ep, ebuf, gettext(
21294235Smarkfen 				    "Can only specify "
21304235Smarkfen 				    "single NAT-T local address.\n"));
21314235Smarkfen 				break;
21324235Smarkfen 			}
21334235Smarkfen 			sa_len = parseaddr(*argv, &natt_lhp, 0, ebuf);
21344235Smarkfen 			if (natt_lhp == NULL) {
21354235Smarkfen 				ERROR1(ep, ebuf, gettext(
21364235Smarkfen 				    "Unknown NAT-T local address \"%s\"\n"),
21374235Smarkfen 				    *argv);
21384235Smarkfen 				break;
21394235Smarkfen 			}
21404235Smarkfen 			argv++;
21414235Smarkfen 			/*
21424235Smarkfen 			 * Round of the sockaddr length to an 8 byte
21434235Smarkfen 			 * boundary to make PF_KEY happy.
21444235Smarkfen 			 */
21454235Smarkfen 			alloclen = sizeof (*natt_local) + roundup(sa_len, 8);
21464235Smarkfen 			natt_local = malloc(alloclen);
21474235Smarkfen 			if (natt_local == NULL)
21484235Smarkfen 				Bail("malloc(natt_local)");
21494235Smarkfen 			totallen += alloclen;
21504235Smarkfen 			natt_local->sadb_address_len = SADB_8TO64(alloclen);
21514235Smarkfen 			natt_local->sadb_address_exttype =
21524235Smarkfen 			    SADB_X_EXT_ADDRESS_NATT_LOC;
21534235Smarkfen 			natt_local->sadb_address_reserved = 0;
21544235Smarkfen 			natt_local->sadb_address_prefixlen = 0;
21554235Smarkfen 			natt_local->sadb_address_proto = 0;
21564235Smarkfen 			if (natt_lhp == &dummy.he ||
21574235Smarkfen 			    natt_lhp->h_addr_list[1] == NULL) {
21584235Smarkfen 				/*
21594235Smarkfen 				 * Single address with -n flag or single name.
21604235Smarkfen 				 */
21614235Smarkfen 				sin6 = (struct sockaddr_in6 *)(natt_local + 1);
21624235Smarkfen 				bzero(sin6, sizeof (*sin6));
21634235Smarkfen 				sin6->sin6_family = AF_INET6;
21644235Smarkfen 				bcopy(natt_lhp->h_addr_list[0],
21654235Smarkfen 				    &sin6->sin6_addr, sizeof (struct in6_addr));
21664235Smarkfen 			} else {
21674235Smarkfen 				/*
21684235Smarkfen 				 * If the nat-local address is vague, don't
21694235Smarkfen 				 * bother.
21704235Smarkfen 				 */
21714235Smarkfen 				totallen -= alloclen;
21724235Smarkfen 				free(natt_local);
21734235Smarkfen 				natt_local = NULL;
21744235Smarkfen 				WARN1(ep, ebuf, gettext(
21754235Smarkfen 				    "NAT-T local address %s "
21764235Smarkfen 				    "is vague, not using.\n"),
21774235Smarkfen 				    natt_lhp->h_name);
21784235Smarkfen 				freehostent(natt_lhp);
21794235Smarkfen 				natt_lhp = NULL;
21804235Smarkfen 				break;
21814235Smarkfen 			}
21824235Smarkfen 			break;
21834235Smarkfen 		case TOK_NATREM:
21844235Smarkfen 			if (natt_remote != NULL) {
21854235Smarkfen 				ERROR(ep, ebuf, gettext(
21864235Smarkfen 				    "Can only specify "
21874235Smarkfen 				    "single NAT-T remote address.\n"));
21884235Smarkfen 				break;
21894235Smarkfen 			}
21904235Smarkfen 			sa_len = parseaddr(*argv, &natt_rhp, 0, ebuf);
21914235Smarkfen 			if (natt_rhp == NULL) {
21924235Smarkfen 				ERROR1(ep, ebuf, gettext(
21934235Smarkfen 				    "Unknown NAT-T remote address \"%s\"\n"),
21944235Smarkfen 				    *argv);
21954235Smarkfen 				break;
21964235Smarkfen 			}
21974235Smarkfen 			argv++;
21984235Smarkfen 			/*
21994235Smarkfen 			 * Round of the sockaddr length to an 8 byte
22004235Smarkfen 			 * boundary to make PF_KEY happy.
22014235Smarkfen 			 */
22024235Smarkfen 			alloclen = sizeof (*natt_remote) + roundup(sa_len, 8);
22034235Smarkfen 			natt_remote = malloc(alloclen);
22044235Smarkfen 			if (natt_remote == NULL)
22054235Smarkfen 				Bail("malloc(natt_remote)");
22064235Smarkfen 			totallen += alloclen;
22074235Smarkfen 			natt_remote->sadb_address_len = SADB_8TO64(alloclen);
22084235Smarkfen 			natt_remote->sadb_address_exttype =
22094235Smarkfen 			    SADB_X_EXT_ADDRESS_NATT_REM;
22104235Smarkfen 			natt_remote->sadb_address_reserved = 0;
22114235Smarkfen 			natt_remote->sadb_address_prefixlen = 0;
22124235Smarkfen 			natt_remote->sadb_address_proto = 0;
22134235Smarkfen 			if (natt_rhp == &dummy.he ||
22144235Smarkfen 			    natt_rhp->h_addr_list[1] == NULL) {
22154235Smarkfen 				/*
22164235Smarkfen 				 * Single address with -n flag or single name.
22174235Smarkfen 				 */
22184235Smarkfen 				sin6 = (struct sockaddr_in6 *)(natt_remote + 1);
22194235Smarkfen 				bzero(sin6, sizeof (*sin6));
22204235Smarkfen 				sin6->sin6_family = AF_INET6;
22214235Smarkfen 				bcopy(natt_rhp->h_addr_list[0],
22224235Smarkfen 				    &sin6->sin6_addr, sizeof (struct in6_addr));
22234235Smarkfen 			} else {
22244235Smarkfen 				/*
22254235Smarkfen 				 * If the nat-renote address is vague, don't
22264235Smarkfen 				 * bother.
22274235Smarkfen 				 */
22284235Smarkfen 				totallen -= alloclen;
22294235Smarkfen 				free(natt_remote);
22304235Smarkfen 				natt_remote = NULL;
22314235Smarkfen 				WARN1(ep, ebuf, gettext(
22324235Smarkfen 				    "NAT-T remote address %s "
22334235Smarkfen 				    "is vague, not using.\n"),
22344235Smarkfen 				    natt_rhp->h_name);
22354235Smarkfen 				freehostent(natt_rhp);
22364235Smarkfen 				natt_rhp = NULL;
22374235Smarkfen 				break;
22384235Smarkfen 			}
22394235Smarkfen 			break;
22404235Smarkfen 		case TOK_ENCRKEY:
22414235Smarkfen 			if (encrypt != NULL) {
22424235Smarkfen 				ERROR(ep, ebuf, gettext(
22434235Smarkfen 				    "Can only specify "
22444235Smarkfen 				    "single encryption key.\n"));
22454235Smarkfen 				break;
22464235Smarkfen 			}
22475989Spwernau 			if (assoc != NULL &&
22485989Spwernau 			    assoc->sadb_sa_encrypt == SADB_EALG_NULL) {
22494573Spwernau 				FATAL(ep, ebuf, gettext(
22504573Spwernau 				    "Cannot specify a key with NULL "
22514573Spwernau 				    "encryption algorithm.\n"));
22524573Spwernau 				break;
22534573Spwernau 			}
22544235Smarkfen 			encrypt = parsekey(*argv, ebuf);
22554235Smarkfen 			argv++;
22564235Smarkfen 			if (encrypt == NULL) {
22574235Smarkfen 				ERROR(ep, ebuf, gettext(
22584235Smarkfen 				    "Invalid encryption key.\n"));
22594235Smarkfen 				break;
22604235Smarkfen 			}
22614235Smarkfen 			totallen += SADB_64TO8(encrypt->sadb_key_len);
22624235Smarkfen 			encrypt->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
22634235Smarkfen 			break;
22644235Smarkfen 		case TOK_AUTHKEY:
22654235Smarkfen 			if (auth != NULL) {
22664235Smarkfen 				ERROR(ep, ebuf, gettext(
22674235Smarkfen 				    "Can only specify single"
22684235Smarkfen 				    " authentication key.\n"));
22694235Smarkfen 				break;
22704235Smarkfen 			}
22714235Smarkfen 			auth = parsekey(*argv, ebuf);
22724235Smarkfen 			argv++;
22734235Smarkfen 			if (auth == NULL) {
22744235Smarkfen 				ERROR(ep, ebuf, gettext(
22754235Smarkfen 				    "Invalid authentication key.\n"));
22764235Smarkfen 				break;
22774235Smarkfen 			}
22784235Smarkfen 			totallen += SADB_64TO8(auth->sadb_key_len);
22794235Smarkfen 			auth->sadb_key_exttype = SADB_EXT_KEY_AUTH;
22804235Smarkfen 			break;
22814235Smarkfen 		case TOK_SRCIDTYPE:
22824235Smarkfen 			if (*argv == NULL || *(argv + 1) == NULL) {
22834235Smarkfen 				FATAL(ep, ebuf, gettext(
22844235Smarkfen 				    "Unexpected end of command "
22854235Smarkfen 				    "line - Expecting Src Type.\n"));
22864235Smarkfen 				/* NOTREACHED */
22874235Smarkfen 				break;
22884235Smarkfen 			}
22894235Smarkfen 			if (srcid != NULL) {
22904235Smarkfen 				ERROR(ep, ebuf, gettext(
22914235Smarkfen 				    "Can only specify single"
22924235Smarkfen 				    " source certificate identity.\n"));
22934235Smarkfen 				break;
22944235Smarkfen 			}
22954235Smarkfen 			alloclen = sizeof (*srcid) +
22964235Smarkfen 			    roundup(strlen(*(argv + 1)) + 1, 8);
22974235Smarkfen 			srcid = malloc(alloclen);
22984235Smarkfen 			if (srcid == NULL)
22994235Smarkfen 				Bail("malloc(srcid)");
23004235Smarkfen 			totallen += alloclen;
23014235Smarkfen 			srcid->sadb_ident_type = parseidtype(*argv, ebuf);
23024235Smarkfen 			argv++;
23034235Smarkfen 			srcid->sadb_ident_len = SADB_8TO64(alloclen);
23044235Smarkfen 			srcid->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC;
23054235Smarkfen 			srcid->sadb_ident_reserved = 0;
23064235Smarkfen 			srcid->sadb_ident_id = 0;  /* Not useful here. */
23074235Smarkfen 			(void) strlcpy((char *)(srcid + 1), *argv, alloclen);
23084235Smarkfen 			argv++;
23094235Smarkfen 			break;
23104235Smarkfen 		case TOK_DSTIDTYPE:
23114235Smarkfen 			if (*argv == NULL || *(argv + 1) == NULL) {
23124235Smarkfen 				ERROR(ep, ebuf, gettext(
23134235Smarkfen 				    "Unexpected end of command"
23144235Smarkfen 				    " line - expecting dst type.\n"));
23154235Smarkfen 				break;
23164235Smarkfen 			}
23174235Smarkfen 			if (dstid != NULL) {
23184235Smarkfen 				ERROR(ep, ebuf, gettext(
23194235Smarkfen 				    "Can only specify single destination "
23204342Spwernau 				    "certificate identity.\n"));
23214235Smarkfen 				break;
23224235Smarkfen 			}
23234235Smarkfen 			alloclen = sizeof (*dstid) +
23244235Smarkfen 			    roundup(strlen(*(argv + 1)) + 1, 8);
23254235Smarkfen 			dstid = malloc(alloclen);
23264235Smarkfen 			if (dstid == NULL)
23274235Smarkfen 				Bail("malloc(dstid)");
23284235Smarkfen 			totallen += alloclen;
23294235Smarkfen 			dstid->sadb_ident_type = parseidtype(*argv, ebuf);
23304235Smarkfen 			argv++;
23314235Smarkfen 			dstid->sadb_ident_len = SADB_8TO64(alloclen);
23324235Smarkfen 			dstid->sadb_ident_exttype = SADB_EXT_IDENTITY_DST;
23334235Smarkfen 			dstid->sadb_ident_reserved = 0;
23344235Smarkfen 			dstid->sadb_ident_id = 0;  /* Not useful here. */
23354235Smarkfen 			(void) strlcpy((char *)(dstid + 1), *argv, alloclen);
23364235Smarkfen 			argv++;
23374235Smarkfen 			break;
23384235Smarkfen 		case TOK_HARD_ALLOC:
23394235Smarkfen 		case TOK_HARD_BYTES:
23404235Smarkfen 		case TOK_HARD_ADDTIME:
23414235Smarkfen 		case TOK_HARD_USETIME:
23424235Smarkfen 			if (hard == NULL) {
23434235Smarkfen 				hard = malloc(sizeof (*hard));
23444235Smarkfen 				if (hard == NULL)
23454235Smarkfen 					Bail("malloc(hard_lifetime)");
23464235Smarkfen 				bzero(hard, sizeof (*hard));
23474235Smarkfen 				hard->sadb_lifetime_exttype =
23484235Smarkfen 				    SADB_EXT_LIFETIME_HARD;
23494235Smarkfen 				hard->sadb_lifetime_len =
23504235Smarkfen 				    SADB_8TO64(sizeof (*hard));
23514235Smarkfen 				totallen += sizeof (*hard);
23524235Smarkfen 			}
23534235Smarkfen 			switch (token) {
23544235Smarkfen 			case TOK_HARD_ALLOC:
23554235Smarkfen 				if (hard->sadb_lifetime_allocations != 0) {
23564235Smarkfen 					ERROR(ep, ebuf, gettext(
23574235Smarkfen 					    "Can only specify single"
23584235Smarkfen 					    " hard allocation limit.\n"));
23594235Smarkfen 					break;
23604235Smarkfen 				}
23614235Smarkfen 				hard->sadb_lifetime_allocations =
23624235Smarkfen 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
23634235Smarkfen 				break;
23644235Smarkfen 			case TOK_HARD_BYTES:
23654235Smarkfen 				if (hard->sadb_lifetime_bytes != 0) {
23664235Smarkfen 					ERROR(ep, ebuf, gettext(
23674235Smarkfen 					    "Can only specify "
23684235Smarkfen 					    "single hard byte limit.\n"));
23694235Smarkfen 					break;
23704235Smarkfen 				}
23714235Smarkfen 				hard->sadb_lifetime_bytes = parsenum(*argv,
23724235Smarkfen 				    B_TRUE, ebuf);
23734235Smarkfen 				break;
23744235Smarkfen 			case TOK_HARD_ADDTIME:
23754235Smarkfen 				if (hard->sadb_lifetime_addtime != 0) {
23764235Smarkfen 					ERROR(ep, ebuf, gettext(
23774235Smarkfen 					    "Can only specify "
23784235Smarkfen 					    "single past-add lifetime.\n"));
23794235Smarkfen 					break;
23804235Smarkfen 				}
23814235Smarkfen 				hard->sadb_lifetime_addtime = parsenum(*argv,
23824235Smarkfen 				    B_TRUE, ebuf);
23834235Smarkfen 				break;
23844235Smarkfen 			case TOK_HARD_USETIME:
23854235Smarkfen 				if (hard->sadb_lifetime_usetime != 0) {
23864235Smarkfen 					ERROR(ep, ebuf, gettext(
23874235Smarkfen 					    "Can only specify "
23884235Smarkfen 					    "single past-use lifetime.\n"));
23894235Smarkfen 					break;
23904235Smarkfen 				}
23914235Smarkfen 				hard->sadb_lifetime_usetime = parsenum(*argv,
23924235Smarkfen 				    B_TRUE, ebuf);
23934235Smarkfen 				break;
23944235Smarkfen 			}
23954235Smarkfen 			argv++;
23964235Smarkfen 			break;
23974235Smarkfen 		case TOK_SOFT_ALLOC:
23984235Smarkfen 		case TOK_SOFT_BYTES:
23994235Smarkfen 		case TOK_SOFT_ADDTIME:
24004235Smarkfen 		case TOK_SOFT_USETIME:
24014235Smarkfen 			if (soft == NULL) {
24024235Smarkfen 				soft = malloc(sizeof (*soft));
24034235Smarkfen 				if (soft == NULL)
24044235Smarkfen 					Bail("malloc(soft_lifetime)");
24054235Smarkfen 				bzero(soft, sizeof (*soft));
24064235Smarkfen 				soft->sadb_lifetime_exttype =
24074235Smarkfen 				    SADB_EXT_LIFETIME_SOFT;
24084235Smarkfen 				soft->sadb_lifetime_len =
24094235Smarkfen 				    SADB_8TO64(sizeof (*soft));
24104235Smarkfen 				totallen += sizeof (*soft);
24114235Smarkfen 			}
24124235Smarkfen 			switch (token) {
24134235Smarkfen 			case TOK_SOFT_ALLOC:
24144235Smarkfen 				if (soft->sadb_lifetime_allocations != 0) {
24154235Smarkfen 					ERROR(ep, ebuf, gettext(
24164235Smarkfen 					    "Can only specify single"
24174235Smarkfen 					    " soft allocation limit.\n"));
24184235Smarkfen 					break;
24194235Smarkfen 				}
24204235Smarkfen 				soft->sadb_lifetime_allocations =
24214235Smarkfen 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
24224235Smarkfen 				break;
24234235Smarkfen 			case TOK_SOFT_BYTES:
24244235Smarkfen 				if (soft->sadb_lifetime_bytes != 0) {
24254235Smarkfen 					ERROR(ep, ebuf, gettext(
24264235Smarkfen 					    "Can only specify single"
24274235Smarkfen 					    " soft byte limit.\n"));
24284235Smarkfen 					break;
24294235Smarkfen 				}
24304235Smarkfen 				soft->sadb_lifetime_bytes = parsenum(*argv,
24314235Smarkfen 				    B_TRUE, ebuf);
24324235Smarkfen 				break;
24334235Smarkfen 			case TOK_SOFT_ADDTIME:
24344235Smarkfen 				if (soft->sadb_lifetime_addtime != 0) {
24354235Smarkfen 					ERROR(ep, ebuf, gettext(
24364235Smarkfen 					    "Can only specify single"
24374235Smarkfen 					    " past-add lifetime.\n"));
24384235Smarkfen 					break;
24394235Smarkfen 				}
24404235Smarkfen 				soft->sadb_lifetime_addtime = parsenum(*argv,
24414235Smarkfen 				    B_TRUE, ebuf);
24424235Smarkfen 				break;
24434235Smarkfen 			case TOK_SOFT_USETIME:
24444235Smarkfen 				if (soft->sadb_lifetime_usetime != 0) {
24454235Smarkfen 					ERROR(ep, ebuf, gettext(
24464235Smarkfen 					    "Can only specify single"
24474235Smarkfen 					    " past-use lifetime.\n"));
24484235Smarkfen 					break;
24494235Smarkfen 				}
24504235Smarkfen 				soft->sadb_lifetime_usetime = parsenum(*argv,
24514235Smarkfen 				    B_TRUE, ebuf);
24524235Smarkfen 				break;
24534235Smarkfen 			}
24544235Smarkfen 			argv++;
24554235Smarkfen 			break;
24566668Smarkfen 		case TOK_FLAG_INBOUND:
24576668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_INBOUND;
24586668Smarkfen 			break;
24596668Smarkfen 		case TOK_FLAG_OUTBOUND:
24606668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_OUTBOUND;
24616668Smarkfen 			break;
24627749SThejaswini.Singarajipura@Sun.COM 		case TOK_REPLAY_VALUE:
24637749SThejaswini.Singarajipura@Sun.COM 			if (replay_ctr != NULL) {
24647749SThejaswini.Singarajipura@Sun.COM 				ERROR(ep, ebuf, gettext(
24657749SThejaswini.Singarajipura@Sun.COM 				    "Can only specify single "
24667749SThejaswini.Singarajipura@Sun.COM 				    "replay value."));
24677749SThejaswini.Singarajipura@Sun.COM 				break;
24687749SThejaswini.Singarajipura@Sun.COM 			}
24697749SThejaswini.Singarajipura@Sun.COM 			replay_ctr = calloc(1, sizeof (*replay_ctr));
24707749SThejaswini.Singarajipura@Sun.COM 			if (replay_ctr == NULL) {
24717749SThejaswini.Singarajipura@Sun.COM 				Bail("malloc(replay value)");
24727749SThejaswini.Singarajipura@Sun.COM 			}
24737749SThejaswini.Singarajipura@Sun.COM 			/*
24747749SThejaswini.Singarajipura@Sun.COM 			 * We currently do not support a 64-bit
24757749SThejaswini.Singarajipura@Sun.COM 			 * replay value.  RFC 4301 will require one,
24767749SThejaswini.Singarajipura@Sun.COM 			 * however, and we have a field in place when
24777749SThejaswini.Singarajipura@Sun.COM 			 * 4301 is built.
24787749SThejaswini.Singarajipura@Sun.COM 			 */
24797749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_exttype = SADB_X_EXT_REPLAY_VALUE;
24807749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_len =
24817749SThejaswini.Singarajipura@Sun.COM 			    SADB_8TO64(sizeof (*replay_ctr));
24827749SThejaswini.Singarajipura@Sun.COM 			totallen += sizeof (*replay_ctr);
24837749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_replay32 = (uint32_t)parsenum(
24847749SThejaswini.Singarajipura@Sun.COM 			    *argv, B_TRUE, ebuf);
24857749SThejaswini.Singarajipura@Sun.COM 			argv++;
24867749SThejaswini.Singarajipura@Sun.COM 			break;
24877749SThejaswini.Singarajipura@Sun.COM 		case TOK_IDLE_ADDTIME:
24887749SThejaswini.Singarajipura@Sun.COM 		case TOK_IDLE_USETIME:
24897749SThejaswini.Singarajipura@Sun.COM 			if (idle == NULL) {
24907749SThejaswini.Singarajipura@Sun.COM 				idle = calloc(1, sizeof (*idle));
24917749SThejaswini.Singarajipura@Sun.COM 				if (idle == NULL) {
24927749SThejaswini.Singarajipura@Sun.COM 					Bail("malloc idle lifetime");
24937749SThejaswini.Singarajipura@Sun.COM 				}
24947749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_exttype =
24957749SThejaswini.Singarajipura@Sun.COM 				    SADB_X_EXT_LIFETIME_IDLE;
24967749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_len =
24977749SThejaswini.Singarajipura@Sun.COM 				    SADB_8TO64(sizeof (*idle));
24987749SThejaswini.Singarajipura@Sun.COM 				totallen += sizeof (*idle);
24997749SThejaswini.Singarajipura@Sun.COM 			}
25007749SThejaswini.Singarajipura@Sun.COM 			switch (token) {
25017749SThejaswini.Singarajipura@Sun.COM 			case TOK_IDLE_ADDTIME:
25027749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_addtime =
25037749SThejaswini.Singarajipura@Sun.COM 				    (uint32_t)parsenum(*argv,
25047749SThejaswini.Singarajipura@Sun.COM 				    B_TRUE, ebuf);
25057749SThejaswini.Singarajipura@Sun.COM 				break;
25067749SThejaswini.Singarajipura@Sun.COM 			case TOK_IDLE_USETIME:
25077749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_usetime =
25087749SThejaswini.Singarajipura@Sun.COM 				    (uint32_t)parsenum(*argv,
25097749SThejaswini.Singarajipura@Sun.COM 				    B_TRUE, ebuf);
25107749SThejaswini.Singarajipura@Sun.COM 				break;
25117749SThejaswini.Singarajipura@Sun.COM 			}
25127749SThejaswini.Singarajipura@Sun.COM 			argv++;
25137749SThejaswini.Singarajipura@Sun.COM 			break;
25144235Smarkfen 		default:
25154235Smarkfen 			ERROR1(ep, ebuf, gettext(
25164235Smarkfen 			    "Don't use extension %s for add/update.\n"),
25174235Smarkfen 			    *(argv - 1));
25184235Smarkfen 			break;
25194235Smarkfen 		}
25204235Smarkfen 	} while (token != TOK_EOF);
25214235Smarkfen 
25224235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
25234235Smarkfen 
25244987Sdanmcd #define	PORT_ONLY_ALLOCATE(af, socktype, exttype, extvar, port) {  \
25254987Sdanmcd 	alloclen = sizeof (sadb_address_t) + roundup(sizeof (socktype), 8); \
25264987Sdanmcd 	(extvar) = calloc(1, alloclen); \
25274987Sdanmcd 	if ((extvar) == NULL) { \
25284987Sdanmcd 		Bail("malloc(implicit port)"); \
25294987Sdanmcd 	} \
25304987Sdanmcd 	totallen += alloclen; \
25314987Sdanmcd 	(extvar)->sadb_address_len = SADB_8TO64(alloclen); \
25324987Sdanmcd 	(extvar)->sadb_address_exttype = (exttype); \
25334987Sdanmcd 	/* sin/sin6 has equivalent offsets for ports! */ \
25344987Sdanmcd 	sin6 = (struct sockaddr_in6 *)((extvar) + 1); \
25354987Sdanmcd 	sin6->sin6_family = (af); \
25364987Sdanmcd 	sin6->sin6_port = (port); \
25374987Sdanmcd 	}
25384987Sdanmcd 
25394235Smarkfen 	/*
25404987Sdanmcd 	 * If we specify inner ports or NAT ports w/o addresses, we still need
25414987Sdanmcd 	 * to allocate.  Also, if we have one inner address, we need the
25424235Smarkfen 	 * other, even if we don't specify anything.
25434235Smarkfen 	 */
25444987Sdanmcd 	if (use_natt) {
25454987Sdanmcd 		if (natt_lport != 0 && natt_local == NULL) {
25464987Sdanmcd 			PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in,
25474987Sdanmcd 			    SADB_X_EXT_ADDRESS_NATT_LOC, natt_local,
25484987Sdanmcd 			    natt_lport);
25494987Sdanmcd 		}
25504987Sdanmcd 
25514987Sdanmcd 		if (natt_rport != 0 && natt_remote == NULL) {
25524987Sdanmcd 			PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in,
25534987Sdanmcd 			    SADB_X_EXT_ADDRESS_NATT_REM, natt_remote,
25544987Sdanmcd 			    natt_rport);
25554987Sdanmcd 		}
25564987Sdanmcd 	} else {
25574987Sdanmcd 		if (natt_lport != 0 || natt_rport != 0) {
25584987Sdanmcd 			ERROR(ep, ebuf, gettext("Must specify 'encap udp' "
25594987Sdanmcd 			    "with any NAT-T port.\n"));
25604987Sdanmcd 		} else if (natt_local != NULL || natt_remote != NULL) {
25614987Sdanmcd 			ERROR(ep, ebuf, gettext("Must specify 'encap udp' "
25624987Sdanmcd 			    "with any NAT-T address.\n"));
25634987Sdanmcd 		}
25644987Sdanmcd 	}
25654987Sdanmcd 
25664235Smarkfen 	if (alloc_inner && idst == NULL) {
25674987Sdanmcd 		PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6,
25684987Sdanmcd 		    SADB_X_EXT_ADDRESS_INNER_DST, idst, 0);
25694235Smarkfen 	}
25704235Smarkfen 
25714235Smarkfen 	if (alloc_inner && isrc == NULL) {
25724987Sdanmcd 		PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6,
25734987Sdanmcd 		    SADB_X_EXT_ADDRESS_INNER_SRC, isrc, 0);
25744235Smarkfen 	}
25754987Sdanmcd #undef PORT_ONLY_ALLOCATE
25764235Smarkfen 
25774235Smarkfen 	/*
25784235Smarkfen 	 * Okay, so now I have all of the potential extensions!
25794235Smarkfen 	 * Allocate a single contiguous buffer.  Keep in mind that it'll
25804235Smarkfen 	 * be enough because the key itself will be yanked.
25814235Smarkfen 	 */
25824235Smarkfen 
25834235Smarkfen 	if (src == NULL && dst != NULL) {
25844235Smarkfen 		/*
25854235Smarkfen 		 * Set explicit unspecified source address.
25864235Smarkfen 		 */
25874235Smarkfen 		size_t lenbytes = SADB_64TO8(dst->sadb_address_len);
25884235Smarkfen 
25894235Smarkfen 		unspec_src = B_TRUE;
25904235Smarkfen 		totallen += lenbytes;
25914235Smarkfen 		src = malloc(lenbytes);
25924235Smarkfen 		if (src == NULL)
25934235Smarkfen 			Bail("malloc(implicit src)");
25944235Smarkfen 		/* Confusing, but we're copying from DST to SRC.  :) */
25954235Smarkfen 		bcopy(dst, src, lenbytes);
25964235Smarkfen 		src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
25974235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
25984235Smarkfen 		bzero(sin6, sizeof (*sin6));
25994235Smarkfen 		sin6->sin6_family = AF_INET6;
26004235Smarkfen 	}
26014235Smarkfen 
26024235Smarkfen 	msg.sadb_msg_len = SADB_8TO64(totallen);
26034235Smarkfen 
26044235Smarkfen 	buffer = malloc(totallen);
26054235Smarkfen 	nexthdr = buffer;
26064235Smarkfen 	bcopy(&msg, nexthdr, sizeof (msg));
26074235Smarkfen 	nexthdr += SADB_8TO64(sizeof (msg));
26084235Smarkfen 	if (assoc != NULL) {
26094235Smarkfen 		if (assoc->sadb_sa_spi == 0) {
26104235Smarkfen 			ERROR1(ep, ebuf, gettext(
26114235Smarkfen 			    "The SPI value is missing for "
26124235Smarkfen 			    "the association you wish to %s.\n"), thiscmd);
26134235Smarkfen 		}
26144235Smarkfen 		if (assoc->sadb_sa_auth == 0 && assoc->sadb_sa_encrypt == 0 &&
26154342Spwernau 		    cmd == CMD_ADD) {
26164235Smarkfen 			free(assoc);
26174235Smarkfen 			FATAL(ep, ebuf, gettext(
26184235Smarkfen 			    "Select at least one algorithm "
26194235Smarkfen 			    "for this add.\n"));
26204235Smarkfen 		}
26214235Smarkfen 
26224235Smarkfen 		/* Hack to let user specify NULL ESP implicitly. */
26234235Smarkfen 		if (msg.sadb_msg_satype == SADB_SATYPE_ESP &&
26244235Smarkfen 		    assoc->sadb_sa_encrypt == 0)
26254235Smarkfen 			assoc->sadb_sa_encrypt = SADB_EALG_NULL;
26264235Smarkfen 
26274235Smarkfen 		/* 0 is an actual value.  Print a warning if it was entered. */
26284235Smarkfen 		if (assoc->sadb_sa_state == 0) {
26294235Smarkfen 			if (readstate) {
26304235Smarkfen 				ERROR(ep, ebuf, gettext(
26314235Smarkfen 				    "WARNING: Cannot set LARVAL SA state.\n"));
26324235Smarkfen 			}
26334235Smarkfen 			assoc->sadb_sa_state = SADB_SASTATE_MATURE;
26344235Smarkfen 		}
26354235Smarkfen 
26364235Smarkfen 		if (use_natt) {
26374235Smarkfen 			if (natt_remote != NULL)
26384235Smarkfen 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_REM;
26394235Smarkfen 			if (natt_local != NULL)
26404235Smarkfen 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_LOC;
26414235Smarkfen 		}
26424235Smarkfen 
26434235Smarkfen 		if (alloc_inner) {
26444235Smarkfen 			/*
26454235Smarkfen 			 * For now, assume RFC 3884's dream of transport-mode
26464235Smarkfen 			 * SAs with inner IP address selectors will not
26474235Smarkfen 			 * happen.
26484235Smarkfen 			 */
26494235Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL;
26504235Smarkfen 			if (proto != 0 && proto != IPPROTO_ENCAP &&
26514235Smarkfen 			    proto != IPPROTO_IPV6) {
26524235Smarkfen 				ERROR1(ep, ebuf, gettext(
26534235Smarkfen 				    "WARNING: Protocol type %d not "
26544235Smarkfen 				    "for use with Tunnel-Mode SA.\n"), proto);
26554235Smarkfen 				/* Continue and let PF_KEY scream... */
26564235Smarkfen 			}
26574235Smarkfen 		}
26584235Smarkfen 
26594235Smarkfen 		bcopy(assoc, nexthdr, SADB_64TO8(assoc->sadb_sa_len));
26604235Smarkfen 		nexthdr += assoc->sadb_sa_len;
26614235Smarkfen 		/* Save the SPI for the case of an error. */
26624235Smarkfen 		spi = assoc->sadb_sa_spi;
26634235Smarkfen 		free(assoc);
26644235Smarkfen 	} else {
26655989Spwernau 		if (spi == 0)
26665989Spwernau 			ERROR1(ep, ebuf, gettext(
26675989Spwernau 			    "Need to define SPI for %s.\n"), thiscmd);
26684235Smarkfen 		ERROR1(ep, ebuf, gettext(
26694235Smarkfen 		    "Need SA parameters for %s.\n"), thiscmd);
26704235Smarkfen 	}
26714235Smarkfen 
26726668Smarkfen 	if (sadb_pair != NULL) {
26736668Smarkfen 		if (sadb_pair->sadb_x_pair_spi == 0) {
26746668Smarkfen 			ERROR1(ep, ebuf, gettext(
26756668Smarkfen 			    "The SPI value is missing for the "
26766668Smarkfen 			    "association you wish to %s.\n"), thiscmd);
26776668Smarkfen 		}
26786668Smarkfen 		bcopy(sadb_pair, nexthdr,
26796668Smarkfen 		    SADB_64TO8(sadb_pair->sadb_x_pair_len));
26806668Smarkfen 		nexthdr += sadb_pair->sadb_x_pair_len;
26816668Smarkfen 		free(sadb_pair);
26826668Smarkfen 	}
26836668Smarkfen 
26844235Smarkfen 	if (hard != NULL) {
26854235Smarkfen 		bcopy(hard, nexthdr, SADB_64TO8(hard->sadb_lifetime_len));
26864235Smarkfen 		nexthdr += hard->sadb_lifetime_len;
26874235Smarkfen 		free(hard);
26884235Smarkfen 	}
26894235Smarkfen 
26904235Smarkfen 	if (soft != NULL) {
26914235Smarkfen 		bcopy(soft, nexthdr, SADB_64TO8(soft->sadb_lifetime_len));
26924235Smarkfen 		nexthdr += soft->sadb_lifetime_len;
26934235Smarkfen 		free(soft);
26944235Smarkfen 	}
26954235Smarkfen 
26967749SThejaswini.Singarajipura@Sun.COM 	if (idle != NULL) {
26977749SThejaswini.Singarajipura@Sun.COM 		bcopy(idle, nexthdr, SADB_64TO8(idle->sadb_lifetime_len));
26987749SThejaswini.Singarajipura@Sun.COM 		nexthdr += idle->sadb_lifetime_len;
26997749SThejaswini.Singarajipura@Sun.COM 		free(idle);
27007749SThejaswini.Singarajipura@Sun.COM 	}
27017749SThejaswini.Singarajipura@Sun.COM 
27024235Smarkfen 	if (encrypt == NULL && auth == NULL && cmd == CMD_ADD) {
27034235Smarkfen 		ERROR(ep, ebuf, gettext(
27044235Smarkfen 		    "Must have at least one key for an add.\n"));
27054235Smarkfen 	}
27064235Smarkfen 
27074235Smarkfen 	if (encrypt != NULL) {
27084235Smarkfen 		bcopy(encrypt, nexthdr, SADB_64TO8(encrypt->sadb_key_len));
27094235Smarkfen 		nexthdr += encrypt->sadb_key_len;
27104235Smarkfen 		bzero(encrypt, SADB_64TO8(encrypt->sadb_key_len));
27114235Smarkfen 		free(encrypt);
27124235Smarkfen 	}
27134235Smarkfen 
27144235Smarkfen 	if (auth != NULL) {
27154235Smarkfen 		bcopy(auth, nexthdr, SADB_64TO8(auth->sadb_key_len));
27164235Smarkfen 		nexthdr += auth->sadb_key_len;
27174235Smarkfen 		bzero(auth, SADB_64TO8(auth->sadb_key_len));
27184235Smarkfen 		free(auth);
27194235Smarkfen 	}
27204235Smarkfen 
27214235Smarkfen 	if (srcid != NULL) {
27224235Smarkfen 		bcopy(srcid, nexthdr, SADB_64TO8(srcid->sadb_ident_len));
27234235Smarkfen 		nexthdr += srcid->sadb_ident_len;
27244235Smarkfen 		free(srcid);
27254235Smarkfen 	}
27264235Smarkfen 
27274235Smarkfen 	if (dstid != NULL) {
27284235Smarkfen 		bcopy(dstid, nexthdr, SADB_64TO8(dstid->sadb_ident_len));
27294235Smarkfen 		nexthdr += dstid->sadb_ident_len;
27304235Smarkfen 		free(dstid);
27314235Smarkfen 	}
27324235Smarkfen 
27334235Smarkfen 	if (dst != NULL) {
27344235Smarkfen 		bcopy(dst, nexthdr, SADB_64TO8(dst->sadb_address_len));
27354235Smarkfen 		free(dst);
27364235Smarkfen 		dst = (struct sadb_address *)nexthdr;
27374235Smarkfen 		dst->sadb_address_proto = proto;
27384235Smarkfen 		((struct sockaddr_in6 *)(dst + 1))->sin6_port = htons(dstport);
27394235Smarkfen 		nexthdr += dst->sadb_address_len;
27404235Smarkfen 	} else {
27414235Smarkfen 		FATAL1(ep, ebuf, gettext(
27424235Smarkfen 		    "Need destination address for %s.\n"), thiscmd);
27434235Smarkfen 	}
27444235Smarkfen 
27454235Smarkfen 	if (use_natt) {
27464235Smarkfen 		if (natt_remote == NULL && natt_local == NULL) {
27474235Smarkfen 			ERROR(ep, ebuf, gettext(
27484235Smarkfen 			    "Must specify NAT-T remote or local address "
27494235Smarkfen 			    "for UDP encapsulation.\n"));
27504235Smarkfen 		}
27514235Smarkfen 
27524235Smarkfen 		if (natt_remote != NULL) {
27534235Smarkfen 			bcopy(natt_remote, nexthdr,
27544235Smarkfen 			    SADB_64TO8(natt_remote->sadb_address_len));
27554235Smarkfen 			free(natt_remote);
27564235Smarkfen 			natt_remote = (struct sadb_address *)nexthdr;
27574235Smarkfen 			nexthdr += natt_remote->sadb_address_len;
27584235Smarkfen 			((struct sockaddr_in6 *)(natt_remote + 1))->sin6_port =
27594235Smarkfen 			    htons(natt_rport);
27604235Smarkfen 		}
27614235Smarkfen 
27624235Smarkfen 		if (natt_local != NULL) {
27634235Smarkfen 			bcopy(natt_local, nexthdr,
27644235Smarkfen 			    SADB_64TO8(natt_local->sadb_address_len));
27654235Smarkfen 			free(natt_local);
27664235Smarkfen 			natt_local = (struct sadb_address *)nexthdr;
27674235Smarkfen 			nexthdr += natt_local->sadb_address_len;
27684235Smarkfen 			((struct sockaddr_in6 *)(natt_local + 1))->sin6_port =
27694235Smarkfen 			    htons(natt_lport);
27704235Smarkfen 		}
27714235Smarkfen 	}
27724235Smarkfen 
27734235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
27744235Smarkfen 
27754235Smarkfen 	/*
27764235Smarkfen 	 * PF_KEY requires a source address extension, even if the source
27774235Smarkfen 	 * address itself is unspecified. (See "Set explicit unspecified..."
27784235Smarkfen 	 * code fragment above. Destination reality check was above.)
27794235Smarkfen 	 */
27804235Smarkfen 	bcopy(src, nexthdr, SADB_64TO8(src->sadb_address_len));
27814235Smarkfen 	free(src);
27824235Smarkfen 	src = (struct sadb_address *)nexthdr;
27834235Smarkfen 	src->sadb_address_proto = proto;
27844235Smarkfen 	((struct sockaddr_in6 *)(src + 1))->sin6_port = htons(srcport);
27854235Smarkfen 	nexthdr += src->sadb_address_len;
27864235Smarkfen 
27874235Smarkfen 	if (isrc != NULL) {
27884235Smarkfen 		bcopy(isrc, nexthdr, SADB_64TO8(isrc->sadb_address_len));
27894235Smarkfen 		free(isrc);
27904235Smarkfen 		isrc = (struct sadb_address *)nexthdr;
27914235Smarkfen 		isrc->sadb_address_proto = iproto;
27924235Smarkfen 		((struct sockaddr_in6 *)(isrc + 1))->sin6_port =
27934235Smarkfen 		    htons(isrcport);
27944235Smarkfen 		nexthdr += isrc->sadb_address_len;
27954235Smarkfen 	}
27964235Smarkfen 
27974235Smarkfen 	if (idst != NULL) {
27984235Smarkfen 		bcopy(idst, nexthdr, SADB_64TO8(idst->sadb_address_len));
27994235Smarkfen 		free(idst);
28004235Smarkfen 		idst = (struct sadb_address *)nexthdr;
28014235Smarkfen 		idst->sadb_address_proto = iproto;
28024235Smarkfen 		((struct sockaddr_in6 *)(idst + 1))->sin6_port =
28034235Smarkfen 		    htons(idstport);
28044235Smarkfen 		nexthdr += idst->sadb_address_len;
28054235Smarkfen 	}
28064235Smarkfen 
28077749SThejaswini.Singarajipura@Sun.COM 	if (replay_ctr != NULL) {
28087749SThejaswini.Singarajipura@Sun.COM 		bcopy(replay_ctr, nexthdr,
28097749SThejaswini.Singarajipura@Sun.COM 		    SADB_64TO8(replay_ctr->sadb_x_rc_len));
28107749SThejaswini.Singarajipura@Sun.COM 		nexthdr += replay_ctr->sadb_x_rc_len;
28117749SThejaswini.Singarajipura@Sun.COM 		free(replay_ctr);
28127749SThejaswini.Singarajipura@Sun.COM 	}
28137749SThejaswini.Singarajipura@Sun.COM 
28144342Spwernau 	if (cflag) {
28154342Spwernau 		/*
28164342Spwernau 		 * Assume the checked cmd would have worked if it was actually
28174342Spwernau 		 * used. doaddresses() will increment lines_added if it
28184342Spwernau 		 * succeeds.
28194342Spwernau 		 */
28204342Spwernau 		lines_added++;
28214342Spwernau 	} else {
28226668Smarkfen 		doaddresses(sadb_msg_type, satype,
28234235Smarkfen 		    cmd, srchp, dsthp, src, dst, unspec_src, buffer, totallen,
28244235Smarkfen 		    spi, ebuf);
28254235Smarkfen 	}
28264235Smarkfen 
28274235Smarkfen 	if (isrchp != NULL && isrchp != &dummy.he)
28284342Spwernau 		freehostent(isrchp);
28294235Smarkfen 	if (idsthp != NULL && idsthp != &dummy.he)
28304342Spwernau 		freehostent(idsthp);
28314235Smarkfen 	if (srchp != NULL && srchp != &dummy.he)
28324342Spwernau 		freehostent(srchp);
28334235Smarkfen 	if (dsthp != NULL && dsthp != &dummy.he)
28344342Spwernau 		freehostent(dsthp);
28354235Smarkfen 	if (natt_lhp != NULL && natt_lhp != &dummy.he)
28364342Spwernau 		freehostent(natt_lhp);
28374235Smarkfen 	if (natt_rhp != NULL && natt_rhp != &dummy.he)
28384342Spwernau 		freehostent(natt_rhp);
28394235Smarkfen 
28404235Smarkfen 	free(ebuf);
28414235Smarkfen 	free(buffer);
28424235Smarkfen }
28434235Smarkfen 
28444235Smarkfen /*
28454235Smarkfen  * DELETE and GET are similar, in that they only need the extensions
28464235Smarkfen  * required to _find_ an SA, and then either delete it or obtain its
28474235Smarkfen  * information.
28484235Smarkfen  */
28494235Smarkfen static void
28504235Smarkfen dodelget(int cmd, int satype, char *argv[], char *ebuf)
28514235Smarkfen {
28524235Smarkfen 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
28534235Smarkfen 	uint64_t *nextext;
28544235Smarkfen 	struct sadb_sa *assoc = NULL;
28554235Smarkfen 	struct sadb_address *src = NULL, *dst = NULL;
28564235Smarkfen 	int next, token, sa_len;
28574235Smarkfen 	char *thiscmd;
28584235Smarkfen 	uint32_t spi;
28596668Smarkfen 	uint8_t	sadb_msg_type;
28604235Smarkfen 	struct hostent *srchp = NULL, *dsthp = NULL;
28614235Smarkfen 	struct sockaddr_in6 *sin6;
28624235Smarkfen 	boolean_t unspec_src = B_TRUE;
28634235Smarkfen 	uint16_t srcport = 0, dstport = 0;
28644235Smarkfen 	uint8_t proto = 0;
28654235Smarkfen 	char *ep = NULL;
28664235Smarkfen 
28674235Smarkfen 	/* Set the first extension header to right past the base message. */
28684235Smarkfen 	nextext = (uint64_t *)(msg + 1);
28694235Smarkfen 	bzero(nextext, sizeof (get_buffer) - sizeof (*msg));
28704235Smarkfen 
28716668Smarkfen 	switch (cmd) {
28726668Smarkfen 	case CMD_GET:
28736668Smarkfen 		thiscmd = "get";
28746668Smarkfen 		sadb_msg_type = SADB_GET;
28756668Smarkfen 		break;
28766668Smarkfen 	case CMD_DELETE:
28776668Smarkfen 		thiscmd = "delete";
28786668Smarkfen 		sadb_msg_type = SADB_DELETE;
28796668Smarkfen 		break;
28806668Smarkfen 	case CMD_DELETE_PAIR:
28816668Smarkfen 		thiscmd = "delete-pair";
28826668Smarkfen 		sadb_msg_type = SADB_X_DELPAIR;
28836668Smarkfen 		break;
28846668Smarkfen 	}
28856668Smarkfen 
28866668Smarkfen 	msg_init(msg, sadb_msg_type, (uint8_t)satype);
28874235Smarkfen 
28884235Smarkfen #define	ALLOC_ADDR_EXT(ext, exttype)			\
28894235Smarkfen 	(ext) = (struct sadb_address *)nextext;		\
28904235Smarkfen 	nextext = (uint64_t *)((ext) + 1);		\
28914235Smarkfen 	nextext += SADB_8TO64(roundup(sa_len, 8));	\
28924235Smarkfen 	(ext)->sadb_address_exttype = exttype;		\
28934235Smarkfen 	(ext)->sadb_address_len = nextext - ((uint64_t *)ext);
28944235Smarkfen 
28954235Smarkfen 	/* Assume last element in argv is set to NULL. */
28964235Smarkfen 	do {
28974235Smarkfen 		token = parseextval(*argv, &next);
28984235Smarkfen 		argv++;
28994235Smarkfen 		switch (token) {
29004235Smarkfen 		case TOK_EOF:
29014235Smarkfen 			/* Do nothing, I'm done. */
29024235Smarkfen 			break;
29034235Smarkfen 		case TOK_UNKNOWN:
29044235Smarkfen 			ERROR1(ep, ebuf, gettext(
29054235Smarkfen 			    "Unknown extension field \"%s\"\n"), *(argv - 1));
29064235Smarkfen 			break;
29074235Smarkfen 		case TOK_SPI:
29084235Smarkfen 			if (assoc != NULL) {
29094235Smarkfen 				ERROR(ep, ebuf, gettext(
29104235Smarkfen 				    "Can only specify single SPI value.\n"));
29114235Smarkfen 				break;
29124235Smarkfen 			}
29134235Smarkfen 			assoc = (struct sadb_sa *)nextext;
29144235Smarkfen 			nextext = (uint64_t *)(assoc + 1);
29154235Smarkfen 			assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc));
29164235Smarkfen 			assoc->sadb_sa_exttype = SADB_EXT_SA;
29174235Smarkfen 			assoc->sadb_sa_spi = htonl((uint32_t)parsenum(*argv,
29184235Smarkfen 			    B_TRUE, ebuf));
29194235Smarkfen 			spi = assoc->sadb_sa_spi;
29204235Smarkfen 			argv++;
29214235Smarkfen 			break;
29224235Smarkfen 		case TOK_SRCPORT:
29234235Smarkfen 			if (srcport != 0) {
29244235Smarkfen 				ERROR(ep, ebuf, gettext(
29254235Smarkfen 				    "Can only specify single source port.\n"));
29264235Smarkfen 				break;
29274235Smarkfen 			}
29284235Smarkfen 			srcport = parsenum(*argv, B_TRUE, ebuf);
29294235Smarkfen 			argv++;
29304235Smarkfen 			break;
29314235Smarkfen 		case TOK_DSTPORT:
29324235Smarkfen 			if (dstport != 0) {
29334235Smarkfen 				ERROR(ep, ebuf, gettext(
29344235Smarkfen 				    "Can only "
29354235Smarkfen 				    "specify single destination port.\n"));
29364235Smarkfen 				break;
29374235Smarkfen 			}
29384235Smarkfen 			dstport = parsenum(*argv, B_TRUE, ebuf);
29394235Smarkfen 			argv++;
29404235Smarkfen 			break;
29414235Smarkfen 		case TOK_PROTO:
29424235Smarkfen 			if (proto != 0) {
29434235Smarkfen 				ERROR(ep, ebuf, gettext(
29444235Smarkfen 				    "Can only specify single protocol.\n"));
29454235Smarkfen 				break;
29464235Smarkfen 			}
29474235Smarkfen 			proto = parsenum(*argv, B_TRUE, ebuf);
29484235Smarkfen 			argv++;
29494235Smarkfen 			break;
29504235Smarkfen 		case TOK_SRCADDR:
29514235Smarkfen 		case TOK_SRCADDR6:
29524235Smarkfen 			if (src != NULL) {
29534235Smarkfen 				ERROR(ep, ebuf, gettext(
29544235Smarkfen 				    "Can only specify single source addr.\n"));
29554235Smarkfen 				break;
29564235Smarkfen 			}
29574235Smarkfen 			sa_len = parseaddr(*argv, &srchp,
29584235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
29594235Smarkfen 			if (srchp == NULL) {
29604235Smarkfen 				ERROR1(ep, ebuf, gettext(
29614235Smarkfen 				    "Unknown source address \"%s\"\n"), *argv);
29624235Smarkfen 				break;
29634235Smarkfen 			}
29644235Smarkfen 			argv++;
29654235Smarkfen 
29664235Smarkfen 			unspec_src = B_FALSE;
29674235Smarkfen 
29684235Smarkfen 			ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
29694235Smarkfen 
29704235Smarkfen 			if (srchp == &dummy.he) {
29714235Smarkfen 				/*
29724235Smarkfen 				 * Single address with -n flag.
29734235Smarkfen 				 */
29744235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
29754235Smarkfen 				bzero(sin6, sizeof (*sin6));
29764235Smarkfen 				sin6->sin6_family = AF_INET6;
29774235Smarkfen 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
29784235Smarkfen 				    sizeof (struct in6_addr));
29794235Smarkfen 			}
29804235Smarkfen 			/* The rest is pre-bzeroed for us. */
29814235Smarkfen 			break;
29824235Smarkfen 		case TOK_DSTADDR:
29834235Smarkfen 		case TOK_DSTADDR6:
29844235Smarkfen 			if (dst != NULL) {
29854235Smarkfen 				ERROR(ep, ebuf, gettext(
29864235Smarkfen 				    "Can only specify single destination "
29874235Smarkfen 				    "address.\n"));
29884235Smarkfen 				break;
29894235Smarkfen 			}
29904235Smarkfen 			sa_len = parseaddr(*argv, &dsthp,
29914235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
29924235Smarkfen 			if (dsthp == NULL) {
29934235Smarkfen 				ERROR1(ep, ebuf, gettext(
29944235Smarkfen 				    "Unknown destination address \"%s\"\n"),
29954235Smarkfen 				    *argv);
29964235Smarkfen 				break;
29974235Smarkfen 			}
29984235Smarkfen 			argv++;
29994235Smarkfen 
30004235Smarkfen 			ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
30014235Smarkfen 
30024235Smarkfen 			if (dsthp == &dummy.he) {
30034235Smarkfen 				/*
30044235Smarkfen 				 * Single address with -n flag.
30054235Smarkfen 				 */
30064235Smarkfen 				sin6 = (struct sockaddr_in6 *)(dst + 1);
30074235Smarkfen 				bzero(sin6, sizeof (*sin6));
30084235Smarkfen 				sin6->sin6_family = AF_INET6;
30094235Smarkfen 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
30104235Smarkfen 				    sizeof (struct in6_addr));
30114235Smarkfen 			}
30124235Smarkfen 			/* The rest is pre-bzeroed for us. */
30134235Smarkfen 			break;
30146668Smarkfen 		case TOK_FLAG_INBOUND:
30156668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_INBOUND;
30166668Smarkfen 			break;
30176668Smarkfen 		case TOK_FLAG_OUTBOUND:
30186668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_OUTBOUND;
30196668Smarkfen 			break;
30204235Smarkfen 		default:
30214235Smarkfen 			ERROR2(ep, ebuf, gettext(
30224235Smarkfen 			    "Don't use extension %s for '%s' command.\n"),
30234235Smarkfen 			    *(argv - 1), thiscmd);
30244235Smarkfen 			break;
30254235Smarkfen 		}
30264235Smarkfen 	} while (token != TOK_EOF);
30274235Smarkfen 
30284235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
30294235Smarkfen 
30304235Smarkfen 	if ((srcport != 0) && (src == NULL)) {
30314235Smarkfen 		ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
30324235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
30334235Smarkfen 		src->sadb_address_proto = proto;
30344235Smarkfen 		bzero(sin6, sizeof (*sin6));
30354235Smarkfen 		sin6->sin6_family = AF_INET6;
30364235Smarkfen 		sin6->sin6_port = htons(srcport);
30374235Smarkfen 	}
30384235Smarkfen 
30394235Smarkfen 	if ((dstport != 0) && (dst == NULL)) {
30404235Smarkfen 		ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
30414235Smarkfen 		sin6 = (struct sockaddr_in6 *)(dst + 1);
30424235Smarkfen 		src->sadb_address_proto = proto;
30434235Smarkfen 		bzero(sin6, sizeof (*sin6));
30444235Smarkfen 		sin6->sin6_family = AF_INET6;
30454235Smarkfen 		sin6->sin6_port = htons(dstport);
30464235Smarkfen 	}
30474235Smarkfen 
30484235Smarkfen 	/* So I have enough of the message to send it down! */
30494235Smarkfen 	msg->sadb_msg_len = nextext - get_buffer;
30504235Smarkfen 
30514235Smarkfen 	if (assoc == NULL) {
30524235Smarkfen 		FATAL1(ep, ebuf, gettext(
30534235Smarkfen 		    "Need SA parameters for %s.\n"), thiscmd);
30544235Smarkfen 	}
30554235Smarkfen 
30564342Spwernau 	if (cflag) {
30574342Spwernau 		/*
30584342Spwernau 		 * Assume the checked cmd would have worked if it was actually
30594342Spwernau 		 * used. doaddresses() will increment lines_added if it
30604342Spwernau 		 * succeeds.
30614342Spwernau 		 */
30624342Spwernau 		lines_added++;
30634342Spwernau 	} else {
30646668Smarkfen 		doaddresses(sadb_msg_type, satype,
30654235Smarkfen 		    cmd, srchp, dsthp, src, dst, unspec_src, get_buffer,
30664235Smarkfen 		    sizeof (get_buffer), spi, NULL);
30674235Smarkfen 	}
30684235Smarkfen 
30694235Smarkfen 	if (srchp != NULL && srchp != &dummy.he)
30704235Smarkfen 		freehostent(srchp);
30714235Smarkfen 	if (dsthp != NULL && dsthp != &dummy.he)
30724235Smarkfen 		freehostent(dsthp);
30734235Smarkfen }
30744235Smarkfen 
30754235Smarkfen /*
3076*9086SVladimir.Kotal@Sun.COM  * "ipseckey monitor" should exit very gracefully if ^C is tapped provided
3077*9086SVladimir.Kotal@Sun.COM  * it is not running in interactive mode.
30784235Smarkfen  */
30794235Smarkfen static void
30804235Smarkfen monitor_catch(int signal)
30814235Smarkfen {
3082*9086SVladimir.Kotal@Sun.COM 	if (!interactive)
3083*9086SVladimir.Kotal@Sun.COM 		errx(signal, gettext("Bailing on signal %d."), signal);
30844235Smarkfen }
30854235Smarkfen 
30864235Smarkfen /*
30874235Smarkfen  * Loop forever, listening on PF_KEY messages.
30884235Smarkfen  */
30894235Smarkfen static void
30904235Smarkfen domonitor(boolean_t passive)
30914235Smarkfen {
30924235Smarkfen 	struct sadb_msg *samsg;
3093*9086SVladimir.Kotal@Sun.COM 	struct sigaction newsig, oldsig;
30944235Smarkfen 	int rc;
30954235Smarkfen 
30964235Smarkfen 	/* Catch ^C. */
3097*9086SVladimir.Kotal@Sun.COM 	newsig.sa_handler = monitor_catch;
3098*9086SVladimir.Kotal@Sun.COM 	newsig.sa_flags = 0;
3099*9086SVladimir.Kotal@Sun.COM 	(void) sigemptyset(&newsig.sa_mask);
3100*9086SVladimir.Kotal@Sun.COM 	(void) sigaddset(&newsig.sa_mask, SIGINT);
3101*9086SVladimir.Kotal@Sun.COM 	(void) sigaction(SIGINT, &newsig, &oldsig);
31024235Smarkfen 
31034235Smarkfen 	samsg = (struct sadb_msg *)get_buffer;
31044235Smarkfen 	if (!passive) {
31054235Smarkfen 		(void) printf(gettext("Actively"));
31064235Smarkfen 		msg_init(samsg, SADB_X_PROMISC, 1);	/* Turn ON promisc. */
31074235Smarkfen 		rc = key_write(keysock, samsg, sizeof (*samsg));
31084235Smarkfen 		if (rc == -1)
31094235Smarkfen 			Bail("write (SADB_X_PROMISC)");
31104235Smarkfen 	} else {
31114235Smarkfen 		(void) printf(gettext("Passively"));
31124235Smarkfen 	}
31134235Smarkfen 	(void) printf(gettext(" monitoring the PF_KEY socket.\n"));
31144235Smarkfen 
31154235Smarkfen 	for (; ; ) {
31164235Smarkfen 		/*
31174235Smarkfen 		 * I assume that read() is non-blocking, and will never
31184235Smarkfen 		 * return 0.
31194235Smarkfen 		 */
31204235Smarkfen 		rc = read(keysock, samsg, sizeof (get_buffer));
3121*9086SVladimir.Kotal@Sun.COM 		if (rc == -1) {
3122*9086SVladimir.Kotal@Sun.COM 			if (errno == EINTR && interactive)
3123*9086SVladimir.Kotal@Sun.COM 				goto out;
3124*9086SVladimir.Kotal@Sun.COM 			else
3125*9086SVladimir.Kotal@Sun.COM 				Bail("read (in domonitor)");
3126*9086SVladimir.Kotal@Sun.COM 		}
31274235Smarkfen 		(void) printf(gettext("Read %d bytes.\n"), rc);
31284235Smarkfen 		/*
31294235Smarkfen 		 * Q:  Should I use the same method of printing as GET does?
31304235Smarkfen 		 * A:  For now, yes.
31314235Smarkfen 		 */
31324867Spwernau 		print_samsg(stdout, get_buffer, B_TRUE, vflag, nflag);
31334235Smarkfen 		(void) putchar('\n');
31344235Smarkfen 	}
3135*9086SVladimir.Kotal@Sun.COM 
3136*9086SVladimir.Kotal@Sun.COM out:
3137*9086SVladimir.Kotal@Sun.COM 	if (interactive)
3138*9086SVladimir.Kotal@Sun.COM 		/* restore SIGINT behavior */
3139*9086SVladimir.Kotal@Sun.COM 		(void) sigaction(SIGINT, &oldsig, NULL);
31404235Smarkfen }
31414235Smarkfen 
31424235Smarkfen /*
31434235Smarkfen  * Either mask or unmask all relevant signals.
31444235Smarkfen  */
31454235Smarkfen static void
31464235Smarkfen mask_signals(boolean_t unmask)
31474235Smarkfen {
31484235Smarkfen 	sigset_t set;
31494235Smarkfen 	static sigset_t oset;
31504235Smarkfen 
31514235Smarkfen 	if (unmask) {
31524235Smarkfen 		(void) sigprocmask(SIG_SETMASK, &oset, NULL);
31534235Smarkfen 	} else {
31544235Smarkfen 		(void) sigfillset(&set);
31554235Smarkfen 		(void) sigprocmask(SIG_SETMASK, &set, &oset);
31564235Smarkfen 	}
31574235Smarkfen }
31584235Smarkfen 
31594235Smarkfen /*
31604235Smarkfen  * Assorted functions to print help text.
31614235Smarkfen  */
31624235Smarkfen #define	puts_tr(s) (void) puts(gettext(s))
31634235Smarkfen 
31644235Smarkfen static void
31654235Smarkfen doattrhelp()
31664235Smarkfen {
31674235Smarkfen 	int i;
31684235Smarkfen 
31694235Smarkfen 	puts_tr("\nSA attributes:");
31704235Smarkfen 
31714235Smarkfen 	for (i = 0; tokens[i].string != NULL; i++) {
31724235Smarkfen 		if (i%3 == 0)
31734235Smarkfen 			(void) printf("\n");
31744235Smarkfen 		(void) printf("    %-15.15s", tokens[i].string);
31754235Smarkfen 	}
31764235Smarkfen 	(void) printf("\n");
31774235Smarkfen }
31784235Smarkfen 
31794235Smarkfen static void
31804235Smarkfen dohelpcmd(char *cmds)
31814235Smarkfen {
31824235Smarkfen 	int cmd;
31834235Smarkfen 
31844235Smarkfen 	if (strcmp(cmds, "attr") == 0) {
31854235Smarkfen 		doattrhelp();
31864235Smarkfen 		return;
31874235Smarkfen 	}
31884235Smarkfen 
31894235Smarkfen 	cmd = parsecmd(cmds);
31904235Smarkfen 	switch (cmd) {
31914235Smarkfen 	case CMD_UPDATE:
31924235Smarkfen 		puts_tr("update	 - Update an existing SA");
31934235Smarkfen 		break;
31946668Smarkfen 	case CMD_UPDATE_PAIR:
31956668Smarkfen 		puts_tr("update-pair - Update an existing pair of SA's");
31966668Smarkfen 		break;
31974235Smarkfen 	case CMD_ADD:
31984235Smarkfen 		puts_tr("add	 - Add a new security association (SA)");
31994235Smarkfen 		break;
32004235Smarkfen 	case CMD_DELETE:
32014235Smarkfen 		puts_tr("delete - Delete an SA");
32024235Smarkfen 		break;
32036668Smarkfen 	case CMD_DELETE_PAIR:
32046668Smarkfen 		puts_tr("delete-pair - Delete a pair of SA's");
32056668Smarkfen 		break;
32064235Smarkfen 	case CMD_GET:
32074235Smarkfen 		puts_tr("get - Display an SA");
32084235Smarkfen 		break;
32094235Smarkfen 	case CMD_FLUSH:
32104235Smarkfen 		puts_tr("flush - Delete all SAs");
3211*9086SVladimir.Kotal@Sun.COM 		puts_tr("");
3212*9086SVladimir.Kotal@Sun.COM 		puts_tr("Optional arguments:");
3213*9086SVladimir.Kotal@Sun.COM 		puts_tr("all        delete all SAs");
3214*9086SVladimir.Kotal@Sun.COM 		puts_tr("esp        delete just ESP SAs");
3215*9086SVladimir.Kotal@Sun.COM 		puts_tr("ah         delete just AH SAs");
3216*9086SVladimir.Kotal@Sun.COM 		puts_tr("<number>   delete just SAs with type given by number");
3217*9086SVladimir.Kotal@Sun.COM 		puts_tr("");
32184235Smarkfen 		break;
32194235Smarkfen 	case CMD_DUMP:
32204235Smarkfen 		puts_tr("dump - Display all SAs");
3221*9086SVladimir.Kotal@Sun.COM 		puts_tr("");
3222*9086SVladimir.Kotal@Sun.COM 		puts_tr("Optional arguments:");
3223*9086SVladimir.Kotal@Sun.COM 		puts_tr("all        display all SAs");
3224*9086SVladimir.Kotal@Sun.COM 		puts_tr("esp        display just ESP SAs");
3225*9086SVladimir.Kotal@Sun.COM 		puts_tr("ah         display just AH SAs");
3226*9086SVladimir.Kotal@Sun.COM 		puts_tr("<number>   display just SAs with type "
3227*9086SVladimir.Kotal@Sun.COM 		    "given by number");
3228*9086SVladimir.Kotal@Sun.COM 		puts_tr("");
32294235Smarkfen 		break;
32304235Smarkfen 	case CMD_MONITOR:
32314235Smarkfen 		puts_tr("monitor - Monitor all PF_KEY reply messages.");
32324235Smarkfen 		break;
32334235Smarkfen 	case CMD_PMONITOR:
32344235Smarkfen 		puts_tr(
32354235Smarkfen "pmonitor, passive_monitor - Monitor PF_KEY messages that");
32364235Smarkfen 		puts_tr(
32374235Smarkfen "                            reply to all PF_KEY sockets.");
32384235Smarkfen 		break;
32394235Smarkfen 
32404235Smarkfen 	case CMD_QUIT:
32414235Smarkfen 		puts_tr("quit, exit - Exit the program");
32424235Smarkfen 		break;
32434235Smarkfen 	case CMD_SAVE:
32444235Smarkfen 		puts_tr("save	    - Saves all SAs to a file");
32454235Smarkfen 		break;
32464235Smarkfen 	case CMD_HELP:
32474235Smarkfen 		puts_tr("help	    - Display list of commands");
32484235Smarkfen 		puts_tr("help <cmd> - Display help for command");
32494235Smarkfen 		puts_tr("help attr  - Display possible SA attributes");
32504235Smarkfen 		break;
32514235Smarkfen 	default:
32524235Smarkfen 		(void) printf(gettext("%s: Unknown command\n"), cmds);
32534235Smarkfen 		break;
32544235Smarkfen 	}
32554235Smarkfen }
32564235Smarkfen 
32574235Smarkfen 
32584235Smarkfen static void
32594235Smarkfen dohelp(char *cmds)
32604235Smarkfen {
32614235Smarkfen 	if (cmds != NULL) {
32624235Smarkfen 		dohelpcmd(cmds);
32634235Smarkfen 		return;
32644235Smarkfen 	}
32654235Smarkfen 	puts_tr("Commands");
32664235Smarkfen 	puts_tr("--------");
32674235Smarkfen 	puts_tr("?, help  - Display this list");
32684235Smarkfen 	puts_tr("help <cmd> - Display help for command");
32694235Smarkfen 	puts_tr("help attr  - Display possible SA attributes");
32704235Smarkfen 	puts_tr("quit, exit - Exit the program");
32714235Smarkfen 	puts_tr("monitor - Monitor all PF_KEY reply messages.");
32724235Smarkfen 	puts_tr("pmonitor, passive_monitor - Monitor PF_KEY messages that");
32734235Smarkfen 	puts_tr("                            reply to all PF_KEY sockets.");
32744235Smarkfen 	puts_tr("");
32754235Smarkfen 	puts_tr("The following commands are of the form:");
32764235Smarkfen 	puts_tr("    <command> {SA type} {attribute value}*");
32774235Smarkfen 	puts_tr("");
32784235Smarkfen 	puts_tr("add (interactive only) - Add a new security association (SA)");
32794235Smarkfen 	puts_tr("update (interactive only) - Update an existing SA");
32806668Smarkfen 	puts_tr("update-pair (interactive only) - Update an existing SA pair");
32814235Smarkfen 	puts_tr("delete - Delete an SA");
32826668Smarkfen 	puts_tr("delete-pair - Delete an SA pair");
32834235Smarkfen 	puts_tr("get - Display an SA");
32844235Smarkfen 	puts_tr("flush - Delete all SAs");
32854235Smarkfen 	puts_tr("dump - Display all SAs");
32864235Smarkfen 	puts_tr("save - Saves all SAs to a file");
32874235Smarkfen }
32884235Smarkfen 
32894235Smarkfen /*
32904235Smarkfen  * "Parse" a command line from argv.
32914235Smarkfen  */
32924235Smarkfen static void
32934342Spwernau parseit(int argc, char *argv[], char *ebuf, boolean_t read_cmdfile)
32944235Smarkfen {
32954235Smarkfen 	int cmd, satype;
32964235Smarkfen 	char *ep = NULL;
32974235Smarkfen 
32984235Smarkfen 	if (argc == 0)
32994235Smarkfen 		return;
33004235Smarkfen 	cmd = parsecmd(*argv++);
33014235Smarkfen 
33024342Spwernau 	/*
33034342Spwernau 	 * Some commands loop forever and should only be run from the command
33044342Spwernau 	 * line, they should never be run from a command file as this may
33054342Spwernau 	 * be used at boot time.
33064342Spwernau 	 */
33074235Smarkfen 	switch (cmd) {
33084235Smarkfen 	case CMD_HELP:
33094342Spwernau 		if (read_cmdfile)
33104342Spwernau 			ERROR(ep, ebuf, gettext("Help not appropriate in "
33114342Spwernau 			    "config file."));
33124342Spwernau 		else
33134342Spwernau 			dohelp(*argv);
33144235Smarkfen 		return;
33154235Smarkfen 	case CMD_MONITOR:
33164342Spwernau 		if (read_cmdfile)
33174342Spwernau 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
33184342Spwernau 			    "config file."));
3319*9086SVladimir.Kotal@Sun.COM 		else {
33204342Spwernau 			domonitor(B_FALSE);
3321*9086SVladimir.Kotal@Sun.COM 			/*
3322*9086SVladimir.Kotal@Sun.COM 			 * Return from the function in interactive mode to
3323*9086SVladimir.Kotal@Sun.COM 			 * avoid error message in the next switch statement.
3324*9086SVladimir.Kotal@Sun.COM 			 * Also print newline to prevent prompt clobbering.
3325*9086SVladimir.Kotal@Sun.COM 			 * The same is done for CMD_PMONITOR.
3326*9086SVladimir.Kotal@Sun.COM 			 */
3327*9086SVladimir.Kotal@Sun.COM 			if (interactive) {
3328*9086SVladimir.Kotal@Sun.COM 				(void) printf("\n");
3329*9086SVladimir.Kotal@Sun.COM 				return;
3330*9086SVladimir.Kotal@Sun.COM 			}
3331*9086SVladimir.Kotal@Sun.COM 		}
33324235Smarkfen 		break;
33334235Smarkfen 	case CMD_PMONITOR:
33344342Spwernau 		if (read_cmdfile)
33354342Spwernau 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
33364342Spwernau 			    "config file."));
3337*9086SVladimir.Kotal@Sun.COM 		else {
33384342Spwernau 			domonitor(B_TRUE);
3339*9086SVladimir.Kotal@Sun.COM 			if (interactive) {
3340*9086SVladimir.Kotal@Sun.COM 				(void) printf("\n");
3341*9086SVladimir.Kotal@Sun.COM 				return;
3342*9086SVladimir.Kotal@Sun.COM 			}
3343*9086SVladimir.Kotal@Sun.COM 		}
33444235Smarkfen 		break;
33454235Smarkfen 	case CMD_QUIT:
33464235Smarkfen 		EXIT_OK(NULL);
33474235Smarkfen 	}
33484235Smarkfen 
33494342Spwernau 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
33504342Spwernau 
33514235Smarkfen 	satype = parsesatype(*argv, ebuf);
33524235Smarkfen 
33534235Smarkfen 	if (satype != SADB_SATYPE_UNSPEC) {
33544235Smarkfen 		argv++;
33554235Smarkfen 	} else {
33564235Smarkfen 		/*
33574235Smarkfen 		 * You must specify either "all" or a specific SA type
33584235Smarkfen 		 * for the "save" command.
33594235Smarkfen 		 */
33604235Smarkfen 		if (cmd == CMD_SAVE)
33614235Smarkfen 			if (*argv == NULL) {
33624235Smarkfen 				FATAL(ep, ebuf, gettext(
33634235Smarkfen 				    "Must specify a specific "
33644235Smarkfen 				    "SA type for save.\n"));
33654235Smarkfen 			} else {
33664235Smarkfen 				argv++;
33674235Smarkfen 			}
33684235Smarkfen 	}
33694235Smarkfen 
33704235Smarkfen 	switch (cmd) {
33714235Smarkfen 	case CMD_FLUSH:
3372*9086SVladimir.Kotal@Sun.COM 		if (argc > 2) {
3373*9086SVladimir.Kotal@Sun.COM 			ERROR(ep, ebuf, gettext("Too many arguments for "
3374*9086SVladimir.Kotal@Sun.COM 			    "flush command"));
3375*9086SVladimir.Kotal@Sun.COM 			handle_errors(ep, ebuf,
3376*9086SVladimir.Kotal@Sun.COM 			    interactive ? B_TRUE : B_FALSE, B_FALSE);
3377*9086SVladimir.Kotal@Sun.COM 		}
33784342Spwernau 		if (!cflag)
33794342Spwernau 			doflush(satype);
33804342Spwernau 		/*
33814342Spwernau 		 * If this was called because of an entry in a cmd file
33824342Spwernau 		 * then this action needs to be counted to prevent
33834342Spwernau 		 * do_interactive() treating this as an error.
33844342Spwernau 		 */
33854342Spwernau 		lines_added++;
33864235Smarkfen 		break;
33874235Smarkfen 	case CMD_ADD:
33884235Smarkfen 	case CMD_UPDATE:
33896668Smarkfen 	case CMD_UPDATE_PAIR:
33904235Smarkfen 		/*
33914235Smarkfen 		 * NOTE: Shouldn't allow ADDs or UPDATEs with keying material
33924235Smarkfen 		 * from the command line.
33934235Smarkfen 		 */
33944235Smarkfen 		if (!interactive) {
33954235Smarkfen 			errx(1, gettext(
33964235Smarkfen 			    "can't do ADD or UPDATE from the command line.\n"));
33974235Smarkfen 		}
33984235Smarkfen 		if (satype == SADB_SATYPE_UNSPEC) {
33994235Smarkfen 			FATAL(ep, ebuf, gettext(
34004235Smarkfen 			    "Must specify a specific SA type."));
34014235Smarkfen 			/* NOTREACHED */
34024235Smarkfen 		}
34034235Smarkfen 		/* Parse for extensions, including keying material. */
34044235Smarkfen 		doaddup(cmd, satype, argv, ebuf);
34054235Smarkfen 		break;
34064235Smarkfen 	case CMD_DELETE:
34076668Smarkfen 	case CMD_DELETE_PAIR:
34084235Smarkfen 	case CMD_GET:
34094235Smarkfen 		if (satype == SADB_SATYPE_UNSPEC) {
34104235Smarkfen 			FATAL(ep, ebuf, gettext(
34114235Smarkfen 			    "Must specify a single SA type."));
34124235Smarkfen 			/* NOTREACHED */
34134235Smarkfen 		}
34144235Smarkfen 		/* Parse for bare minimum to locate an SA. */
34154235Smarkfen 		dodelget(cmd, satype, argv, ebuf);
34164235Smarkfen 		break;
34174235Smarkfen 	case CMD_DUMP:
34184342Spwernau 		if (read_cmdfile)
34194342Spwernau 			ERROR(ep, ebuf, gettext("Dump not appropriate in "
34204342Spwernau 			    "config file."));
3421*9086SVladimir.Kotal@Sun.COM 		else {
3422*9086SVladimir.Kotal@Sun.COM 			if (argc > 2) {
3423*9086SVladimir.Kotal@Sun.COM 				ERROR(ep, ebuf, gettext("Too many arguments "
3424*9086SVladimir.Kotal@Sun.COM 				    "for dump command"));
3425*9086SVladimir.Kotal@Sun.COM 				handle_errors(ep, ebuf,
3426*9086SVladimir.Kotal@Sun.COM 				    interactive ? B_TRUE : B_FALSE, B_FALSE);
3427*9086SVladimir.Kotal@Sun.COM 			}
34284342Spwernau 			dodump(satype, NULL);
3429*9086SVladimir.Kotal@Sun.COM 		}
34304235Smarkfen 		break;
34314235Smarkfen 	case CMD_SAVE:
34324342Spwernau 		if (read_cmdfile) {
34334342Spwernau 			ERROR(ep, ebuf, gettext("Save not appropriate in "
34344342Spwernau 			    "config file."));
34354342Spwernau 		} else {
34364342Spwernau 			mask_signals(B_FALSE);	/* Mask signals */
34374342Spwernau 			dodump(satype, opensavefile(argv[0]));
34384342Spwernau 			mask_signals(B_TRUE);	/* Unmask signals */
34394342Spwernau 		}
34404235Smarkfen 		break;
34414235Smarkfen 	default:
34424235Smarkfen 		warnx(gettext("Unknown command (%s).\n"),
34434235Smarkfen 		    *(argv - ((satype == SADB_SATYPE_UNSPEC) ? 1 : 2)));
34444235Smarkfen 		usage();
34454235Smarkfen 	}
34464342Spwernau 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
34474235Smarkfen }
34484235Smarkfen 
34494235Smarkfen int
34504235Smarkfen main(int argc, char *argv[])
34514235Smarkfen {
34524235Smarkfen 	int ch;
34534235Smarkfen 	FILE *infile = stdin, *savefile;
34544235Smarkfen 	boolean_t dosave = B_FALSE, readfile = B_FALSE;
34554235Smarkfen 	char *configfile = NULL;
34565321Spwernau 	struct stat sbuf;
34577749SThejaswini.Singarajipura@Sun.COM 	int bootflags;
34584235Smarkfen 
34594235Smarkfen 	(void) setlocale(LC_ALL, "");
34604235Smarkfen #if !defined(TEXT_DOMAIN)
34614235Smarkfen #define	TEXT_DOMAIN "SYS_TEST"
34624235Smarkfen #endif
34634235Smarkfen 	(void) textdomain(TEXT_DOMAIN);
34644235Smarkfen 
34654235Smarkfen 	/*
34664235Smarkfen 	 * Check to see if the command is being run from smf(5).
34674235Smarkfen 	 */
34684235Smarkfen 	my_fmri = getenv("SMF_FMRI");
34694235Smarkfen 
34704235Smarkfen 	openlog("ipseckey", LOG_CONS, LOG_AUTH);
34714235Smarkfen 	if (getuid() != 0) {
34724235Smarkfen 		errx(1, "Insufficient privileges to run ipseckey.");
34734235Smarkfen 	}
34744235Smarkfen 
34754235Smarkfen 	/* umask me to paranoid, I only want to create files read-only */
34764235Smarkfen 	(void) umask((mode_t)00377);
34774235Smarkfen 
34784235Smarkfen 	while ((ch = getopt(argc, argv, "pnvf:s:c:")) != EOF)
34794235Smarkfen 		switch (ch) {
34804235Smarkfen 		case 'p':
34814235Smarkfen 			pflag = B_TRUE;
34824235Smarkfen 			break;
34834235Smarkfen 		case 'n':
34844235Smarkfen 			nflag = B_TRUE;
34854235Smarkfen 			break;
34864235Smarkfen 		case 'v':
34874235Smarkfen 			vflag = B_TRUE;
34884235Smarkfen 			break;
34894235Smarkfen 		case 'c':
34904235Smarkfen 			cflag = B_TRUE;
34914235Smarkfen 			/* FALLTHRU */
34924235Smarkfen 		case 'f':
34934235Smarkfen 			if (dosave)
34944235Smarkfen 				usage();
34954235Smarkfen 			infile = fopen(optarg, "r");
34964235Smarkfen 			if (infile == NULL) {
34974235Smarkfen 				EXIT_BADCONFIG2("Unable to open configuration "
34984235Smarkfen 				    "file: %s\n", optarg);
34994235Smarkfen 			}
35005321Spwernau 			/*
35015321Spwernau 			 * Check file permissions/ownership and warn or
35025321Spwernau 			 * fail depending on state of SMF control.
35035321Spwernau 			 */
35045321Spwernau 			if (fstat(fileno(infile), &sbuf) == -1) {
35055321Spwernau 				(void) fclose(infile);
35065321Spwernau 				EXIT_BADCONFIG2("Unable to stat configuration "
35075321Spwernau 				    "file: %s\n", optarg);
35085321Spwernau 			}
35095321Spwernau 			if (INSECURE_PERMS(sbuf)) {
35105321Spwernau 				if (my_fmri != NULL) {
35115321Spwernau 					(void) fclose(infile);
35125321Spwernau 					EXIT_BADCONFIG2("Config file "
35135321Spwernau 					    "%s has insecure permissions.",
35145321Spwernau 					    optarg);
35155321Spwernau 				} else 	{
35165321Spwernau 					(void) fprintf(stderr, "%s %s\n",
35175321Spwernau 					    optarg, gettext(
35185321Spwernau 					    "has insecure permissions, will be "
35195321Spwernau 					    "rejected in permanent config."));
35205321Spwernau 				}
35215321Spwernau 			}
35224235Smarkfen 			configfile = strdup(optarg);
35234235Smarkfen 			readfile = B_TRUE;
35244235Smarkfen 			break;
35254235Smarkfen 		case 's':
35264235Smarkfen 			if (readfile)
35274235Smarkfen 				usage();
35284235Smarkfen 			dosave = B_TRUE;
35294235Smarkfen 			savefile = opensavefile(optarg);
35304235Smarkfen 			break;
35314235Smarkfen 		default:
35324235Smarkfen 			usage();
35334235Smarkfen 		}
35344235Smarkfen 
35354235Smarkfen 	argc -= optind;
35364235Smarkfen 	argv += optind;
35374235Smarkfen 
35384235Smarkfen 	mypid = getpid();
35394235Smarkfen 
35404235Smarkfen 	keysock = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
35414235Smarkfen 
35424235Smarkfen 	if (keysock == -1) {
35434235Smarkfen 		if (errno == EPERM) {
35444235Smarkfen 			EXIT_BADPERM("Insufficient privileges to open "
35454235Smarkfen 			    "PF_KEY socket.\n");
35464235Smarkfen 		} else {
35474235Smarkfen 			/* some other reason */
35484235Smarkfen 			EXIT_FATAL("Opening PF_KEY socket");
35494235Smarkfen 		}
35504235Smarkfen 	}
35514235Smarkfen 
35527749SThejaswini.Singarajipura@Sun.COM 	if ((_cladm(CL_INITIALIZE, CL_GET_BOOTFLAG, &bootflags) != 0) ||
35537749SThejaswini.Singarajipura@Sun.COM 	    (bootflags & CLUSTER_BOOTED)) {
35547749SThejaswini.Singarajipura@Sun.COM 		in_cluster_mode = B_TRUE;
35557749SThejaswini.Singarajipura@Sun.COM 		cluster_socket = socket(AF_INET, SOCK_DGRAM, 0);
35567749SThejaswini.Singarajipura@Sun.COM 		cli_addr.sin_family = AF_INET;
35577749SThejaswini.Singarajipura@Sun.COM 		cli_addr.sin_addr.s_addr = INADDR_LOOPBACK;
35587749SThejaswini.Singarajipura@Sun.COM 		cli_addr.sin_port = htons(CLUSTER_UDP_PORT);
35597749SThejaswini.Singarajipura@Sun.COM 	}
35607749SThejaswini.Singarajipura@Sun.COM 
35614235Smarkfen 	if (dosave) {
35624235Smarkfen 		mask_signals(B_FALSE);	/* Mask signals */
35634235Smarkfen 		dodump(SADB_SATYPE_UNSPEC, savefile);
35644235Smarkfen 		mask_signals(B_TRUE);	/* Unmask signals */
35654235Smarkfen 		EXIT_OK(NULL);
35664235Smarkfen 	}
35674235Smarkfen 
35684235Smarkfen 	/*
35694235Smarkfen 	 * When run from smf(5) flush any existing SA's first
35704235Smarkfen 	 * otherwise you will end up in maintenance mode.
35714235Smarkfen 	 */
35724235Smarkfen 	if ((my_fmri != NULL) && readfile) {
35734235Smarkfen 		(void) fprintf(stdout, gettext(
35744235Smarkfen 		    "Flushing existing SA's before adding new SA's\n"));
35754235Smarkfen 		(void) fflush(stdout);
35764235Smarkfen 		doflush(SADB_SATYPE_UNSPEC);
35774235Smarkfen 	}
3578*9086SVladimir.Kotal@Sun.COM 	if (infile != stdin || argc == 0) {
35794235Smarkfen 		/* Go into interactive mode here. */
35804235Smarkfen 		do_interactive(infile, configfile, "ipseckey> ", my_fmri,
3581*9086SVladimir.Kotal@Sun.COM 		    parseit, no_match);
35824235Smarkfen 	}
35834342Spwernau 	parseit(argc, argv, NULL, B_FALSE);
35844235Smarkfen 
35854235Smarkfen 	return (0);
35864235Smarkfen }
3587