xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ipseckey.c (revision 10824:c47254a96e5d)
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 /*
229086SVladimir.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 /*
1029086SVladimir.Kotal@Sun.COM  * Disable default TAB completion for now (until some brave soul tackles it).
1039086SVladimir.Kotal@Sun.COM  */
1049086SVladimir.Kotal@Sun.COM /* ARGSUSED */
1059086SVladimir.Kotal@Sun.COM static
1069086SVladimir.Kotal@Sun.COM CPL_MATCH_FN(no_match)
1079086SVladimir.Kotal@Sun.COM {
1089086SVladimir.Kotal@Sun.COM 	return (0);
1099086SVladimir.Kotal@Sun.COM }
1109086SVladimir.Kotal@Sun.COM 
1119086SVladimir.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 /*
1649086SVladimir.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"));
1769086SVladimir.Kotal@Sun.COM 		EXIT_FATAL(NULL);
1779086SVladimir.Kotal@Sun.COM 	} else {
1789086SVladimir.Kotal@Sun.COM 		(void) fprintf(stderr,
1799086SVladimir.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 	}
3889086SVladimir.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
457*10824SMark.Fenwick@Sun.COM #define	TOK_RESERVED		54
4584235Smarkfen 
4594235Smarkfen static struct toktable {
4604235Smarkfen 	char *string;
4614235Smarkfen 	int token;
4624235Smarkfen 	int next;
4634235Smarkfen } tokens[] = {
4644235Smarkfen 	/* "String",		token value,		next arg is */
4654235Smarkfen 	{"spi",			TOK_SPI,		NEXTNUM},
4666668Smarkfen 	{"pair-spi",		TOK_PAIR_SPI,		NEXTNUM},
4674235Smarkfen 	{"replay",		TOK_REPLAY,		NEXTNUM},
4684235Smarkfen 	{"state",		TOK_STATE,		NEXTNUMSTR},
4694235Smarkfen 	{"auth_alg",		TOK_AUTHALG,		NEXTNUMSTR},
4704235Smarkfen 	{"authalg",		TOK_AUTHALG,		NEXTNUMSTR},
4714235Smarkfen 	{"encr_alg",		TOK_ENCRALG,		NEXTNUMSTR},
4724235Smarkfen 	{"encralg",		TOK_ENCRALG,		NEXTNUMSTR},
4734235Smarkfen 	{"flags",		TOK_FLAGS,		NEXTNUM},
4744235Smarkfen 	{"soft_alloc",		TOK_SOFT_ALLOC,		NEXTNUM},
4754235Smarkfen 	{"soft_bytes",		TOK_SOFT_BYTES,		NEXTNUM},
4764235Smarkfen 	{"soft_addtime",	TOK_SOFT_ADDTIME,	NEXTNUM},
4774235Smarkfen 	{"soft_usetime",	TOK_SOFT_USETIME,	NEXTNUM},
4784235Smarkfen 	{"hard_alloc",		TOK_HARD_ALLOC,		NEXTNUM},
4794235Smarkfen 	{"hard_bytes",		TOK_HARD_BYTES,		NEXTNUM},
4804235Smarkfen 	{"hard_addtime",	TOK_HARD_ADDTIME,	NEXTNUM},
4814235Smarkfen 	{"hard_usetime",	TOK_HARD_USETIME,	NEXTNUM},
4824235Smarkfen 	{"current_alloc",	TOK_CURRENT_ALLOC,	NEXTNUM},
4834235Smarkfen 	{"current_bytes",	TOK_CURRENT_BYTES,	NEXTNUM},
4844235Smarkfen 	{"current_addtime",	TOK_CURRENT_ADDTIME,	NEXTNUM},
4854235Smarkfen 	{"current_usetime",	TOK_CURRENT_USETIME,	NEXTNUM},
4864235Smarkfen 
4874235Smarkfen 	{"saddr",		TOK_SRCADDR,		NEXTADDR},
4884235Smarkfen 	{"srcaddr",		TOK_SRCADDR,		NEXTADDR},
4894235Smarkfen 	{"src",			TOK_SRCADDR,		NEXTADDR},
4904235Smarkfen 	{"daddr",		TOK_DSTADDR,		NEXTADDR},
4914235Smarkfen 	{"dstaddr",		TOK_DSTADDR,		NEXTADDR},
4924235Smarkfen 	{"dst",			TOK_DSTADDR,		NEXTADDR},
4934235Smarkfen 	{"proxyaddr",		TOK_PROXYADDR,		NEXTADDR},
4944235Smarkfen 	{"proxy",		TOK_PROXYADDR,		NEXTADDR},
4954235Smarkfen 	{"innersrc",		TOK_PROXYADDR,		NEXTADDR},
4964235Smarkfen 	{"isrc",		TOK_PROXYADDR,		NEXTADDR},
4974235Smarkfen 	{"innerdst",		TOK_IDSTADDR,		NEXTADDR},
4984235Smarkfen 	{"idst",		TOK_IDSTADDR,		NEXTADDR},
4994235Smarkfen 
5004235Smarkfen 	{"sport",		TOK_SRCPORT,		NEXTNUM},
5014235Smarkfen 	{"dport",		TOK_DSTPORT,		NEXTNUM},
5024235Smarkfen 	{"innersport",		TOK_ISRCPORT,		NEXTNUM},
5034235Smarkfen 	{"isport",		TOK_ISRCPORT,		NEXTNUM},
5044235Smarkfen 	{"innerdport",		TOK_IDSTPORT,		NEXTNUM},
5054235Smarkfen 	{"idport",		TOK_IDSTPORT,		NEXTNUM},
5064235Smarkfen 	{"proto",		TOK_PROTO,		NEXTNUM},
5074235Smarkfen 	{"ulp",			TOK_PROTO,		NEXTNUM},
5084235Smarkfen 	{"iproto",		TOK_IPROTO,		NEXTNUM},
5094235Smarkfen 	{"iulp",		TOK_IPROTO,		NEXTNUM},
5104235Smarkfen 
5114235Smarkfen 	{"saddr6",		TOK_SRCADDR6,		NEXTADDR},
5124235Smarkfen 	{"srcaddr6",		TOK_SRCADDR6,		NEXTADDR},
5134235Smarkfen 	{"src6",		TOK_SRCADDR6,		NEXTADDR},
5144235Smarkfen 	{"daddr6",		TOK_DSTADDR6,		NEXTADDR},
5154235Smarkfen 	{"dstaddr6",		TOK_DSTADDR6,		NEXTADDR},
5164235Smarkfen 	{"dst6",		TOK_DSTADDR6,		NEXTADDR},
5174235Smarkfen 	{"proxyaddr6",		TOK_PROXYADDR6,		NEXTADDR},
5184235Smarkfen 	{"proxy6",		TOK_PROXYADDR6,		NEXTADDR},
5194235Smarkfen 	{"innersrc6",		TOK_PROXYADDR6,		NEXTADDR},
5204235Smarkfen 	{"isrc6",		TOK_PROXYADDR6,		NEXTADDR},
5214235Smarkfen 	{"innerdst6",		TOK_IDSTADDR6,		NEXTADDR},
5224235Smarkfen 	{"idst6",		TOK_IDSTADDR6,		NEXTADDR},
5234235Smarkfen 
5244235Smarkfen 	{"authkey",		TOK_AUTHKEY,		NEXTHEX},
5254235Smarkfen 	{"encrkey",		TOK_ENCRKEY,		NEXTHEX},
5264235Smarkfen 	{"srcidtype",		TOK_SRCIDTYPE,		NEXTIDENT},
5274235Smarkfen 	{"dstidtype",		TOK_DSTIDTYPE,		NEXTIDENT},
5284235Smarkfen 	{"dpd",			TOK_DPD,		NEXTNUM},
5294235Smarkfen 	{"sens_level",		TOK_SENS_LEVEL,		NEXTNUM},
5304235Smarkfen 	{"sens_map",		TOK_SENS_MAP,		NEXTHEX},
5314235Smarkfen 	{"integ_level",		TOK_INTEG_LEVEL,	NEXTNUM},
5324235Smarkfen 	{"integ_map",		TOK_INTEG_MAP,		NEXTHEX},
5334235Smarkfen 	{"nat_loc",		TOK_NATLOC,		NEXTADDR},
5344235Smarkfen 	{"nat_rem",		TOK_NATREM,		NEXTADDR},
5354235Smarkfen 	{"nat_lport",		TOK_NATLPORT,		NEXTNUM},
5364235Smarkfen 	{"nat_rport",		TOK_NATRPORT,		NEXTNUM},
5374235Smarkfen 	{"encap",		TOK_ENCAP,		NEXTNUMSTR},
5386668Smarkfen 
5396668Smarkfen 	{"outbound",		TOK_FLAG_OUTBOUND,	NULL},
5406668Smarkfen 	{"inbound",		TOK_FLAG_INBOUND,	NULL},
5417749SThejaswini.Singarajipura@Sun.COM 
542*10824SMark.Fenwick@Sun.COM 	{"reserved_bits",	TOK_RESERVED,		NEXTNUM},
5437749SThejaswini.Singarajipura@Sun.COM 	{"replay_value",	TOK_REPLAY_VALUE,	NEXTNUM},
5447749SThejaswini.Singarajipura@Sun.COM 	{"idle_addtime",	TOK_IDLE_ADDTIME,	NEXTNUM},
5457749SThejaswini.Singarajipura@Sun.COM 	{"idle_usetime",	TOK_IDLE_USETIME,	NEXTNUM},
5464235Smarkfen 	{NULL,			TOK_UNKNOWN,		NEXTEOF}
5474235Smarkfen };
5484235Smarkfen 
5494235Smarkfen /*
5504235Smarkfen  * Q:	Do I need stuff for proposals, combinations, supported algorithms,
5514235Smarkfen  *	or SPI ranges?
5524235Smarkfen  *
5534235Smarkfen  * A:	Probably not, but you never know.
5544235Smarkfen  *
5554235Smarkfen  * Parse out extension header type values.
5564235Smarkfen  */
5574235Smarkfen static int
5584235Smarkfen parseextval(char *value, int *next)
5594235Smarkfen {
5604235Smarkfen 	struct toktable *tp;
5614235Smarkfen 
5624235Smarkfen 	if (value == NULL)
5634235Smarkfen 		return (TOK_EOF);
5644235Smarkfen 
5654235Smarkfen 	for (tp = tokens; tp->string != NULL; tp++)
5664235Smarkfen 		if (strcmp(value, tp->string) == 0)
5674235Smarkfen 			break;
5684235Smarkfen 
5694235Smarkfen 	/*
5704235Smarkfen 	 * Since the OS controls what extensions are available, we don't have
5714235Smarkfen 	 * to parse numeric values here.
5724235Smarkfen 	 */
5734235Smarkfen 
5744235Smarkfen 	*next = tp->next;
5754235Smarkfen 	return (tp->token);
5764235Smarkfen }
5774235Smarkfen 
5784235Smarkfen /*
5794235Smarkfen  * Parse possible state values.
5804235Smarkfen  */
5814235Smarkfen static uint8_t
5824235Smarkfen parsestate(char *state, char *ebuf)
5834235Smarkfen {
5844235Smarkfen 	struct states {
5854235Smarkfen 		char *state;
5864235Smarkfen 		uint8_t retval;
5874235Smarkfen 	} states[] = {
5884235Smarkfen 		{"larval",	SADB_SASTATE_LARVAL},
5894235Smarkfen 		{"mature",	SADB_SASTATE_MATURE},
5904235Smarkfen 		{"dying",	SADB_SASTATE_DYING},
5914235Smarkfen 		{"dead",	SADB_SASTATE_DEAD},
5924235Smarkfen 		{NULL,		0}
5934235Smarkfen 	};
5944235Smarkfen 	struct states *sp;
5954235Smarkfen 	char *ep = NULL;
5964235Smarkfen 
5974235Smarkfen 	if (state == NULL) {
5984235Smarkfen 		FATAL(ep, ebuf, "Unexpected end of command line "
5994235Smarkfen 		    "was expecting a state.\n");
6004235Smarkfen 	}
6014235Smarkfen 
6024235Smarkfen 	for (sp = states; sp->state != NULL; sp++) {
6034235Smarkfen 		if (strcmp(sp->state, state) == 0)
6044235Smarkfen 			return (sp->retval);
6054235Smarkfen 	}
6064235Smarkfen 	ERROR1(ep, ebuf, gettext("Unknown state type \"%s\"\n"), state);
6074235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
6084235Smarkfen 	return (0);
6094235Smarkfen }
6104235Smarkfen 
6114235Smarkfen /*
6124235Smarkfen  * Return the numerical algorithm identifier corresponding to the specified
6134235Smarkfen  * algorithm name.
6144235Smarkfen  */
6154235Smarkfen static uint8_t
6164235Smarkfen parsealg(char *alg, int proto_num, char *ebuf)
6174235Smarkfen {
6184235Smarkfen 	u_longlong_t invalue;
6194235Smarkfen 	struct ipsecalgent *algent;
6204235Smarkfen 	char *ep = NULL;
6214235Smarkfen 
6224235Smarkfen 	if (alg == NULL) {
6234235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
6244235Smarkfen 		    "was expecting an algorithm name.\n"));
6254235Smarkfen 	}
6264235Smarkfen 
6274235Smarkfen 	algent = getipsecalgbyname(alg, proto_num, NULL);
6284235Smarkfen 	if (algent != NULL) {
6294235Smarkfen 		uint8_t alg_num;
6304235Smarkfen 
6314235Smarkfen 		alg_num = algent->a_alg_num;
632*10824SMark.Fenwick@Sun.COM 		if (ALG_FLAG_COUNTERMODE & algent->a_alg_flags)
633*10824SMark.Fenwick@Sun.COM 			WARN1(ep, ebuf, gettext(
634*10824SMark.Fenwick@Sun.COM 			    "Using manual keying with a Counter mode algorithm "
635*10824SMark.Fenwick@Sun.COM 			    "such as \"%s\" may be insecure!\n"),
636*10824SMark.Fenwick@Sun.COM 			    algent->a_names[0]);
6374235Smarkfen 		freeipsecalgent(algent);
6384235Smarkfen 
6394235Smarkfen 		return (alg_num);
6404235Smarkfen 	}
6414235Smarkfen 
6424235Smarkfen 	/*
6434235Smarkfen 	 * Since algorithms can be loaded during kernel run-time, check for
6444235Smarkfen 	 * numeric algorithm values too.  PF_KEY can catch bad ones with EINVAL.
6454235Smarkfen 	 */
6464235Smarkfen 	invalue = parsenum(alg, B_FALSE, ebuf);
6474235Smarkfen 	if (invalue != (u_longlong_t)-1 &&
6484235Smarkfen 	    (u_longlong_t)(invalue & (u_longlong_t)0xff) == invalue)
6494235Smarkfen 		return ((uint8_t)invalue);
6504235Smarkfen 
6514235Smarkfen 	if (proto_num == IPSEC_PROTO_ESP) {
6524235Smarkfen 		ERROR1(ep, ebuf, gettext(
6534235Smarkfen 		    "Unknown encryption algorithm type \"%s\"\n"), alg);
6544235Smarkfen 	} else {
6554235Smarkfen 		ERROR1(ep, ebuf, gettext(
6564235Smarkfen 		    "Unknown authentication algorithm type \"%s\"\n"), alg);
6574235Smarkfen 	}
6584235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
6594235Smarkfen 	return (0);
6604235Smarkfen }
6614235Smarkfen 
6624235Smarkfen /*
6634235Smarkfen  * Parse and reverse parse out a source/destination ID type.
6644235Smarkfen  */
6654235Smarkfen static struct idtypes {
6664235Smarkfen 	char *idtype;
6674235Smarkfen 	uint8_t retval;
6684235Smarkfen } idtypes[] = {
6694235Smarkfen 	{"prefix",	SADB_IDENTTYPE_PREFIX},
6704235Smarkfen 	{"fqdn",	SADB_IDENTTYPE_FQDN},
6714235Smarkfen 	{"domain",	SADB_IDENTTYPE_FQDN},
6724235Smarkfen 	{"domainname",	SADB_IDENTTYPE_FQDN},
6734235Smarkfen 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
6744235Smarkfen 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
6754235Smarkfen 	{"der_dn",	SADB_X_IDENTTYPE_DN},
6764235Smarkfen 	{"der_gn",	SADB_X_IDENTTYPE_GN},
6774235Smarkfen 	{NULL,		0}
6784235Smarkfen };
6794235Smarkfen 
6804235Smarkfen static uint16_t
6814235Smarkfen parseidtype(char *type, char *ebuf)
6824235Smarkfen {
6834235Smarkfen 	struct idtypes *idp;
6844235Smarkfen 	u_longlong_t invalue;
6854235Smarkfen 	char *ep = NULL;
6864235Smarkfen 
6874235Smarkfen 	if (type == NULL) {
6884235Smarkfen 		/* Shouldn't reach here, see callers for why. */
6894235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
6904235Smarkfen 		    "was expecting a type.\n"));
6914235Smarkfen 	}
6924235Smarkfen 
6934235Smarkfen 	for (idp = idtypes; idp->idtype != NULL; idp++) {
6944235Smarkfen 		if (strcasecmp(idp->idtype, type) == 0)
6954235Smarkfen 			return (idp->retval);
6964235Smarkfen 	}
6974235Smarkfen 	/*
6984235Smarkfen 	 * Since identity types are almost arbitrary, check for numeric
6994235Smarkfen 	 * algorithm values too.  PF_KEY can catch bad ones with EINVAL.
7004235Smarkfen 	 */
7014235Smarkfen 	invalue = parsenum(type, B_FALSE, ebuf);
7024235Smarkfen 	if (invalue != (u_longlong_t)-1 &&
7034235Smarkfen 	    (u_longlong_t)(invalue & (u_longlong_t)0xffff) == invalue)
7044235Smarkfen 		return ((uint16_t)invalue);
7054235Smarkfen 
7064235Smarkfen 
7074235Smarkfen 	ERROR1(ep, ebuf, gettext("Unknown identity type \"%s\"\n"), type);
7084235Smarkfen 
7094235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
7104235Smarkfen 	return (0);
7114235Smarkfen }
7124235Smarkfen 
7134235Smarkfen /*
7144235Smarkfen  * Parse an address off the command line.  Return length of sockaddr,
7154235Smarkfen  * and either return a hostent pointer (caller frees).  The new
7164235Smarkfen  * getipnodebyname() call does the Right Thing (TM), even with
7174235Smarkfen  * raw addresses (colon-separated IPv6 or dotted decimal IPv4).
7184235Smarkfen  */
7194235Smarkfen 
7204235Smarkfen static struct {
7214235Smarkfen 	struct hostent he;
7224235Smarkfen 	char *addtl[2];
7234235Smarkfen 	} dummy;
7244235Smarkfen static union {
7254235Smarkfen 	struct in6_addr ipv6;
7264235Smarkfen 	struct in_addr ipv4;
7274235Smarkfen 	uint64_t aligner;
7284235Smarkfen } addr1;
7294235Smarkfen 
7304235Smarkfen static int
7314235Smarkfen parseaddr(char *addr, struct hostent **hpp, boolean_t v6only, char *ebuf)
7324235Smarkfen {
7334235Smarkfen 	int hp_errno;
7344235Smarkfen 	struct hostent *hp = NULL;
7354235Smarkfen 	char *ep = NULL;
7364235Smarkfen 
7374235Smarkfen 	if (addr == NULL) {
7384235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
7394235Smarkfen 		    "was expecting an address.\n"));
7404235Smarkfen 	}
7414235Smarkfen 
7424235Smarkfen 	if (!nflag) {
7434235Smarkfen 		/*
7444235Smarkfen 		 * Try name->address first.  Assume AF_INET6, and
7454235Smarkfen 		 * get IPv4's, plus IPv6's if and only if IPv6 is configured.
7464235Smarkfen 		 * This means to add IPv6 SAs, you must have IPv6
7474235Smarkfen 		 * up-and-running.  (AI_DEFAULT works here.)
7484235Smarkfen 		 */
7494235Smarkfen 		hp = getipnodebyname(addr, AF_INET6,
7504235Smarkfen 		    (v6only ? AI_ADDRCONFIG : (AI_DEFAULT | AI_ALL)),
7514235Smarkfen 		    &hp_errno);
7524235Smarkfen 	} else {
7534235Smarkfen 		/*
7544235Smarkfen 		 * Try a normal address conversion only.  Use "dummy"
7554235Smarkfen 		 * to construct a fake hostent.  Caller will know not
7564235Smarkfen 		 * to free this one.
7574235Smarkfen 		 */
7584235Smarkfen 		if (inet_pton(AF_INET6, addr, &addr1) == 1) {
7594235Smarkfen 			dummy.he.h_addr_list = dummy.addtl;
7604235Smarkfen 			dummy.addtl[0] = (char *)&addr1;
7614235Smarkfen 			dummy.addtl[1] = NULL;
7624235Smarkfen 			hp = &dummy.he;
7634235Smarkfen 			dummy.he.h_addrtype = AF_INET6;
7644235Smarkfen 			dummy.he.h_length = sizeof (struct in6_addr);
7654235Smarkfen 		} else if (inet_pton(AF_INET, addr, &addr1) == 1) {
7664235Smarkfen 			/*
7674235Smarkfen 			 * Remap to AF_INET6 anyway.
7684235Smarkfen 			 */
7694235Smarkfen 			dummy.he.h_addr_list = dummy.addtl;
7704235Smarkfen 			dummy.addtl[0] = (char *)&addr1;
7714235Smarkfen 			dummy.addtl[1] = NULL;
7724235Smarkfen 			hp = &dummy.he;
7734235Smarkfen 			dummy.he.h_addrtype = AF_INET6;
7744235Smarkfen 			dummy.he.h_length = sizeof (struct in6_addr);
7754235Smarkfen 			/*
7764235Smarkfen 			 * NOTE:  If macro changes to disallow in-place
7774235Smarkfen 			 * conversion, rewhack this.
7784235Smarkfen 			 */
7794235Smarkfen 			IN6_INADDR_TO_V4MAPPED(&addr1.ipv4, &addr1.ipv6);
7804235Smarkfen 		} else {
7814235Smarkfen 			hp = NULL;
7824235Smarkfen 		}
7834235Smarkfen 	}
7844235Smarkfen 
7854235Smarkfen 	if (hp == NULL)
7864235Smarkfen 		WARN1(ep, ebuf, gettext("Unknown address %s."), addr);
7874235Smarkfen 
7884235Smarkfen 	*hpp = hp;
7894235Smarkfen 	/* Always return sockaddr_in6 for now. */
7904235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
7914235Smarkfen 	return (sizeof (struct sockaddr_in6));
7924235Smarkfen }
7934235Smarkfen 
7944235Smarkfen /*
7954235Smarkfen  * Parse a hex character for a key.  A string will take the form:
7964235Smarkfen  *	xxxxxxxxx/nn
7974235Smarkfen  * where
7984235Smarkfen  *	xxxxxxxxx == a string of hex characters ([0-9][a-f][A-F])
7994235Smarkfen  *	nn == an optional decimal "mask".  If it is not present, it
8004235Smarkfen  *	is assumed that the hex string will be rounded to the nearest
8014235Smarkfen  *	byte, where odd nibbles, like 123 will become 0x0123.
8024235Smarkfen  *
8034235Smarkfen  * NOTE:Unlike the expression of IP addresses, I will not allow an
8044235Smarkfen  *	excessive "mask".  For example 2112/50 is very illegal.
8054235Smarkfen  * NOTE2:	This key should be in canonical order.  Consult your man
8064235Smarkfen  *		pages per algorithm about said order.
8074235Smarkfen  */
8084235Smarkfen 
8094235Smarkfen #define	hd2num(hd) (((hd) >= '0' && (hd) <= '9') ? ((hd) - '0') : \
8104235Smarkfen 	(((hd) >= 'a' && (hd) <= 'f') ? ((hd) - 'a' + 10) : ((hd) - 'A' + 10)))
8114235Smarkfen 
8124235Smarkfen static struct sadb_key *
813*10824SMark.Fenwick@Sun.COM parsekey(char *input, char *ebuf, uint_t reserved_bits)
8144235Smarkfen {
8154235Smarkfen 	struct sadb_key *retval;
8164235Smarkfen 	uint_t i, hexlen = 0, bits, alloclen;
8174235Smarkfen 	uint8_t *key;
8184235Smarkfen 	char *ep = NULL;
8194235Smarkfen 
8204235Smarkfen 	if (input == NULL) {
8214235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
8224235Smarkfen 		    "was expecting a key.\n"));
8234235Smarkfen 	}
8244573Spwernau 	/* Allow hex values prepended with 0x convention */
8254573Spwernau 	if ((strnlen(input, sizeof (hexlen)) > 2) &&
8264573Spwernau 	    (strncasecmp(input, "0x", 2) == 0))
8274573Spwernau 		input += 2;
8284235Smarkfen 
8294235Smarkfen 	for (i = 0; input[i] != '\0' && input[i] != '/'; i++)
8304235Smarkfen 		hexlen++;
8314235Smarkfen 
8324235Smarkfen 	if (input[i] == '\0') {
8334235Smarkfen 		bits = 0;
8344235Smarkfen 	} else {
8354235Smarkfen 		/* Have /nn. */
8364235Smarkfen 		input[i] = '\0';
8374235Smarkfen 		if (sscanf((input + i + 1), "%u", &bits) != 1) {
8384235Smarkfen 			FATAL1(ep, ebuf, gettext(
8394235Smarkfen 			    "\"%s\" is not a bit specifier.\n"),
8404235Smarkfen 			    (input + i + 1));
8414235Smarkfen 		}
8424235Smarkfen 		/* hexlen in nibbles */
8434235Smarkfen 		if (((bits + 3) >> 2) > hexlen) {
8444235Smarkfen 			ERROR2(ep, ebuf, gettext(
8454235Smarkfen 			    "bit length %d is too big for %s.\n"), bits, input);
8464235Smarkfen 		}
8474235Smarkfen 		/*
8484235Smarkfen 		 * Adjust hexlen down if user gave us too small of a bit
8494235Smarkfen 		 * count.
8504235Smarkfen 		 */
8514235Smarkfen 		if ((hexlen << 2) > bits + 3) {
8524235Smarkfen 			WARN2(ep, ebuf, gettext(
8534235Smarkfen 			    "WARNING: Lower bits will be truncated "
8544235Smarkfen 			    "for:\n\t%s/%d.\n"), input, bits);
8554235Smarkfen 			hexlen = (bits + 3) >> 2;
8564235Smarkfen 			input[hexlen] = '\0';
8574235Smarkfen 		}
8584235Smarkfen 	}
8594235Smarkfen 
8604235Smarkfen 	/*
8614235Smarkfen 	 * Allocate.  Remember, hexlen is in nibbles.
8624235Smarkfen 	 */
8634235Smarkfen 
8644235Smarkfen 	alloclen = sizeof (*retval) + roundup((hexlen/2 + (hexlen & 0x1)), 8);
8654235Smarkfen 	retval = malloc(alloclen);
8664235Smarkfen 
8674235Smarkfen 	if (retval == NULL)
8684235Smarkfen 		Bail("malloc(parsekey)");
8694235Smarkfen 	retval->sadb_key_len = SADB_8TO64(alloclen);
870*10824SMark.Fenwick@Sun.COM 
871*10824SMark.Fenwick@Sun.COM 	retval->sadb_key_reserved = reserved_bits;
872*10824SMark.Fenwick@Sun.COM 
8734235Smarkfen 	if (bits == 0)
8744235Smarkfen 		retval->sadb_key_bits = (hexlen + (hexlen & 0x1)) << 2;
8754235Smarkfen 	else
8764235Smarkfen 		retval->sadb_key_bits = bits;
8774235Smarkfen 
8784235Smarkfen 	/*
8794235Smarkfen 	 * Read in nibbles.  Read in odd-numbered as shifted high.
8804235Smarkfen 	 * (e.g. 123 becomes 0x1230).
8814235Smarkfen 	 */
8824235Smarkfen 
8834235Smarkfen 	key = (uint8_t *)(retval + 1);
8844235Smarkfen 	for (i = 0; input[i] != '\0'; i += 2) {
8854235Smarkfen 		boolean_t second = (input[i + 1] != '\0');
8864235Smarkfen 
8874235Smarkfen 		if (!isxdigit(input[i]) ||
8884235Smarkfen 		    (!isxdigit(input[i + 1]) && second)) {
8894235Smarkfen 			ERROR1(ep, ebuf, gettext(
8904235Smarkfen 			    "string '%s' not a hex value.\n"), input);
8914235Smarkfen 			free(retval);
8924235Smarkfen 			retval = NULL;
8934235Smarkfen 			break;
8944235Smarkfen 		}
8954235Smarkfen 		*key = (hd2num(input[i]) << 4);
8964235Smarkfen 		if (second)
8974235Smarkfen 			*key |= hd2num(input[i + 1]);
8984235Smarkfen 		else
8994235Smarkfen 			break;	/* out of for loop. */
9004235Smarkfen 		key++;
9014235Smarkfen 	}
9024235Smarkfen 
9034235Smarkfen 	/* bzero the remaining bits if we're a non-octet amount. */
9044235Smarkfen 	if (bits & 0x7)
9054235Smarkfen 		*((input[i] == '\0') ? key - 1 : key) &=
9064235Smarkfen 		    0xff << (8 - (bits & 0x7));
9074235Smarkfen 
9084235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
9094235Smarkfen 	return (retval);
9104235Smarkfen }
9114235Smarkfen 
9124235Smarkfen /*
9134235Smarkfen  * Write a message to the PF_KEY socket.  If verbose, print the message
9144235Smarkfen  * heading into the kernel.
9154235Smarkfen  */
9164235Smarkfen static int
9174235Smarkfen key_write(int fd, void *msg, size_t len)
9184235Smarkfen {
9194235Smarkfen 	if (vflag) {
9204235Smarkfen 		(void) printf(
9214235Smarkfen 		    gettext("VERBOSE ON:  Message to kernel looks like:\n"));
9224235Smarkfen 		(void) printf("==========================================\n");
9234867Spwernau 		print_samsg(stdout, msg, B_FALSE, vflag, nflag);
9244235Smarkfen 		(void) printf("==========================================\n");
9254235Smarkfen 	}
9264235Smarkfen 
9274235Smarkfen 	return (write(fd, msg, len));
9284235Smarkfen }
9294235Smarkfen 
9304235Smarkfen /*
9314235Smarkfen  * SIGALRM handler for time_critical_enter.
9324235Smarkfen  */
9334235Smarkfen static void
9344235Smarkfen time_critical_catch(int signal)
9354235Smarkfen {
9364235Smarkfen 	if (signal == SIGALRM) {
9374235Smarkfen 		errx(1, gettext("Reply message from PF_KEY timed out."));
9384235Smarkfen 	} else {
9394235Smarkfen 		errx(1, gettext("Caught signal %d while trying to receive"
9404342Spwernau 		    "PF_KEY reply message"), signal);
9414235Smarkfen 	}
9424235Smarkfen 	/* errx() calls exit. */
9434235Smarkfen }
9444235Smarkfen 
9454235Smarkfen #define	TIME_CRITICAL_TIME 10	/* In seconds */
9464235Smarkfen 
9474235Smarkfen /*
9484235Smarkfen  * Enter a "time critical" section where key is waiting for a return message.
9494235Smarkfen  */
9504235Smarkfen static void
9514235Smarkfen time_critical_enter(void)
9524235Smarkfen {
9534235Smarkfen 	(void) signal(SIGALRM, time_critical_catch);
9544235Smarkfen 	(void) alarm(TIME_CRITICAL_TIME);
9554235Smarkfen }
9564235Smarkfen 
9574235Smarkfen /*
9584235Smarkfen  * Exit the "time critical" section after getting an appropriate return
9594235Smarkfen  * message.
9604235Smarkfen  */
9614235Smarkfen static void
9624235Smarkfen time_critical_exit(void)
9634235Smarkfen {
9644235Smarkfen 	(void) alarm(0);
9654235Smarkfen 	(void) signal(SIGALRM, SIG_DFL);
9664235Smarkfen }
9674235Smarkfen 
9684235Smarkfen /*
9694235Smarkfen  * Construct a PF_KEY FLUSH message for the SA type specified.
9704235Smarkfen  */
9714235Smarkfen static void
9724235Smarkfen doflush(int satype)
9734235Smarkfen {
9744235Smarkfen 	struct sadb_msg msg;
9754235Smarkfen 	int rc;
9764235Smarkfen 
9774235Smarkfen 	msg_init(&msg, SADB_FLUSH, (uint8_t)satype);
9784235Smarkfen 	rc = key_write(keysock, &msg, sizeof (msg));
9794235Smarkfen 	if (rc == -1)
9804235Smarkfen 		Bail("write() to PF_KEY socket failed (in doflush)");
9814235Smarkfen 
9824235Smarkfen 	time_critical_enter();
9834235Smarkfen 	do {
9844235Smarkfen 		rc = read(keysock, &msg, sizeof (msg));
9854235Smarkfen 		if (rc == -1)
9864235Smarkfen 			Bail("read (in doflush)");
9874235Smarkfen 	} while (msg.sadb_msg_seq != seq || msg.sadb_msg_pid != mypid);
9884235Smarkfen 	time_critical_exit();
9894235Smarkfen 
9904235Smarkfen 	/*
9914235Smarkfen 	 * I should _never_ hit the following unless:
9924235Smarkfen 	 *
9934235Smarkfen 	 * 1. There is a kernel bug.
9944235Smarkfen 	 * 2. There is another process filling in its pid with mine, and
9954235Smarkfen 	 *    issuing a different message that would cause a different result.
9964235Smarkfen 	 */
9974235Smarkfen 	if (msg.sadb_msg_type != SADB_FLUSH ||
9984235Smarkfen 	    msg.sadb_msg_satype != (uint8_t)satype) {
9994235Smarkfen 		syslog((LOG_NOTICE|LOG_AUTH),
10004235Smarkfen 		    gettext("doflush: Return message not of type SADB_FLUSH!"));
10014235Smarkfen 		Bail("doflush: Return message not of type SADB_FLUSH!");
10024235Smarkfen 	}
10034235Smarkfen 
10044235Smarkfen 	if (msg.sadb_msg_errno != 0) {
10054235Smarkfen 		errno = msg.sadb_msg_errno;
10064235Smarkfen 		if (errno == EINVAL) {
10074235Smarkfen 			print_diagnostic(stderr, msg.sadb_x_msg_diagnostic);
10084235Smarkfen 			warnx(gettext("Cannot flush SA type %d."), satype);
10094235Smarkfen 		}
10104235Smarkfen 		Bail("return message (in doflush)");
10114235Smarkfen 	}
10124235Smarkfen }
10134235Smarkfen 
10144235Smarkfen /*
10154235Smarkfen  * save_XXX functions are used when "saving" the SA tables to either a
10164235Smarkfen  * file or standard output.  They use the dump_XXX functions where needed,
10174235Smarkfen  * but mostly they use the rparseXXX functions.
10184235Smarkfen  */
10194235Smarkfen 
10204235Smarkfen /*
10214235Smarkfen  * Because "save" and "dump" both use the SADB_DUMP message, fold both
10224235Smarkfen  * into the same function.
10234235Smarkfen  */
10244235Smarkfen static void
10254235Smarkfen dodump(int satype, FILE *ofile)
10264235Smarkfen {
10274235Smarkfen 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
10284235Smarkfen 	int rc;
10294235Smarkfen 
10304235Smarkfen 	if (ofile != NULL) {
10314235Smarkfen 		(void) fprintf(ofile,
10324235Smarkfen 		    gettext("# This key file was generated by the"));
10334235Smarkfen 		(void) fprintf(ofile,
10344235Smarkfen 		    gettext(" ipseckey(1m) command's 'save' feature.\n\n"));
10354235Smarkfen 	}
10364235Smarkfen 	msg_init(msg, SADB_DUMP, (uint8_t)satype);
10374235Smarkfen 	rc = key_write(keysock, msg, sizeof (*msg));
10384235Smarkfen 	if (rc == -1)
10394235Smarkfen 		Bail("write to PF_KEY socket failed (in dodump)");
10404235Smarkfen 
10414235Smarkfen 	do {
10424235Smarkfen 		/*
10434235Smarkfen 		 * For DUMP, do only the read as a time critical section.
10444235Smarkfen 		 */
10454235Smarkfen 		time_critical_enter();
10464235Smarkfen 		rc = read(keysock, get_buffer, sizeof (get_buffer));
10474235Smarkfen 		time_critical_exit();
10484235Smarkfen 		if (rc == -1)
10494235Smarkfen 			Bail("read (in dodump)");
10504235Smarkfen 		if (msg->sadb_msg_pid == mypid &&
10514235Smarkfen 		    msg->sadb_msg_type == SADB_DUMP &&
10524235Smarkfen 		    msg->sadb_msg_seq != 0 &&
10534235Smarkfen 		    msg->sadb_msg_errno == 0) {
10544235Smarkfen 			if (ofile == NULL) {
10554867Spwernau 				print_samsg(stdout, get_buffer, B_FALSE, vflag,
10564867Spwernau 				    nflag);
10574235Smarkfen 				(void) putchar('\n');
10584235Smarkfen 			} else {
10594235Smarkfen 				save_assoc(get_buffer, ofile);
10604235Smarkfen 			}
10614235Smarkfen 		}
10624235Smarkfen 	} while (msg->sadb_msg_pid != mypid ||
10634235Smarkfen 	    (msg->sadb_msg_errno == 0 && msg->sadb_msg_seq != 0));
10644235Smarkfen 
10654235Smarkfen 	if (ofile != NULL && ofile != stdout)
10664235Smarkfen 		(void) fclose(ofile);
10674235Smarkfen 
10684235Smarkfen 	if (msg->sadb_msg_errno == 0) {
10694235Smarkfen 		if (ofile == NULL)
10704235Smarkfen 			(void) printf(
10714235Smarkfen 			    gettext("Dump succeeded for SA type %d.\n"),
10724235Smarkfen 			    satype);
10734235Smarkfen 	} else {
10744235Smarkfen 		print_diagnostic(stderr, msg->sadb_x_msg_diagnostic);
10754235Smarkfen 		errno = msg->sadb_msg_errno;
10764235Smarkfen 		Bail("Dump failed");
10774235Smarkfen 	}
10784235Smarkfen }
10794235Smarkfen 
10804235Smarkfen #define	SCOPE_UNSPEC 0
10814235Smarkfen #define	SCOPE_LINKLOCAL 1
10824235Smarkfen #define	SCOPE_SITELOCAL 2
10834235Smarkfen #define	SCOPE_GLOBAL 3
10844235Smarkfen #define	SCOPE_V4COMPAT 4
10854235Smarkfen #define	SCOPE_LOOPBACK 5	/* Pedantic, yes, but necessary. */
10864235Smarkfen 
10874235Smarkfen static int
10884235Smarkfen ipv6_addr_scope(struct in6_addr *addr)
10894235Smarkfen {
10904235Smarkfen 	/* Don't return anything regarding multicast for now... */
10914235Smarkfen 
10924235Smarkfen 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
10934235Smarkfen 		return (SCOPE_UNSPEC);
10944235Smarkfen 
10954235Smarkfen 	if (IN6_IS_ADDR_LINKLOCAL(addr))
10964235Smarkfen 		return (SCOPE_LINKLOCAL);
10974235Smarkfen 
10984235Smarkfen 	if (IN6_IS_ADDR_SITELOCAL(addr))
10994235Smarkfen 		return (SCOPE_SITELOCAL);
11004235Smarkfen 
11014235Smarkfen 	if (IN6_IS_ADDR_V4COMPAT(addr))
11024235Smarkfen 		return (SCOPE_V4COMPAT);
11034235Smarkfen 
11044235Smarkfen 	if (IN6_IS_ADDR_LOOPBACK(addr))
11054235Smarkfen 		return (SCOPE_LOOPBACK);
11064235Smarkfen 
11074235Smarkfen 	/* For now, return global by default. */
11084235Smarkfen 	return (SCOPE_GLOBAL);
11094235Smarkfen }
11104235Smarkfen 
11114235Smarkfen /*
11124235Smarkfen  * doaddresses():
11134235Smarkfen  *
11144235Smarkfen  * Used by doaddup() and dodelget() to create new SA's based on the
11154235Smarkfen  * provided source and destination addresses hostent.
11164235Smarkfen  *
11174235Smarkfen  * sadb_msg_type: expected PF_KEY reply message type
11184235Smarkfen  * sadb_msg_satype: expected PF_KEY reply satype
11194235Smarkfen  * cmd: user command
11204235Smarkfen  * srchp: hostent for the source address(es)
11214235Smarkfen  * dsthp: hostent for the destination address(es)
11224235Smarkfen  * src: points to the SADB source address extension
11234235Smarkfen  * dst: points to the SADB destination address extension
11244235Smarkfen  * unspec_src: indicates an unspecified source address.
11254235Smarkfen  * buffer: pointer to the SADB buffer to use with PF_KEY
11264235Smarkfen  * buffer_size: size of buffer
11274235Smarkfen  * spi: spi for this message (set by caller)
11284235Smarkfen  * srcport: source port if specified
11294235Smarkfen  * dstport: destination port if specified
11304235Smarkfen  * proto: IP protocol number if specified
11314235Smarkfen  * iproto: Inner (tunnel mode) IP protocol number if specified
11324235Smarkfen  * NATT note: we are going to assume a semi-sane world where NAT
11334235Smarkfen  * boxen don't explode to multiple addresses.
11344235Smarkfen  */
11354235Smarkfen static void
11364235Smarkfen doaddresses(uint8_t sadb_msg_type, uint8_t sadb_msg_satype, int cmd,
11374235Smarkfen     struct hostent *srchp, struct hostent *dsthp,
11384235Smarkfen     struct sadb_address *src, struct sadb_address *dst,
11394235Smarkfen     boolean_t unspec_src, uint64_t *buffer, int buffer_size, uint32_t spi,
11404235Smarkfen     char *ebuf)
11414235Smarkfen {
11424235Smarkfen 	boolean_t single_dst;
11434235Smarkfen 	struct sockaddr_in6 *sin6;
11444235Smarkfen 	struct sadb_msg *msgp;
11454235Smarkfen 	int i, rc;
11464235Smarkfen 	char **walker;	/* For the SRC and PROXY walking functions. */
11474235Smarkfen 	char *first_match;
11484235Smarkfen 	uint64_t savebuf[MAX_GET_SIZE];
11494235Smarkfen 	uint16_t srcport = 0, dstport = 0;
11504235Smarkfen 	char *ep = NULL;
11514235Smarkfen 
11524235Smarkfen 	/*
11534235Smarkfen 	 * Okay, now we have "src", "dst", and maybe "proxy" reassigned
11544235Smarkfen 	 * to point into the buffer to be written to PF_KEY, we can do
11554235Smarkfen 	 * potentially several writes based on destination address.
11564235Smarkfen 	 *
11574235Smarkfen 	 * First, obtain port numbers from passed-in extensions.
11584235Smarkfen 	 */
11594235Smarkfen 
11604235Smarkfen 	if (src != NULL) {
11614235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
11624235Smarkfen 		srcport = ntohs(sin6->sin6_port);
11634235Smarkfen 	}
11644235Smarkfen 	if (dst != NULL) {
11654235Smarkfen 		sin6 = (struct sockaddr_in6 *)(dst + 1);
11664235Smarkfen 		dstport = ntohs(sin6->sin6_port);
11674235Smarkfen 	}
11684235Smarkfen 
11694235Smarkfen 	/*
11704235Smarkfen 	 * The rules for ADD, GET, and UPDATE: (NOTE:  This assumes IPsec.
11714235Smarkfen 	 * If other consumers of PF_KEY happen, this will have to be
11724235Smarkfen 	 * rewhacked.):
11734235Smarkfen 	 *
11744235Smarkfen 	 *	Do a message for every possible DST address.
11754235Smarkfen 	 *
11764235Smarkfen 	 *	If a source or proxy address explodes, keep unspecified
11774235Smarkfen 	 *	(and mention unspecified).
11784235Smarkfen 	 *
11794235Smarkfen 	 * If dsthp is == dummy.he, then go through the loop once.
11804235Smarkfen 	 * If any other hp is == dummy.he, then you don't have to apply any
11814235Smarkfen 	 * silly rules.
11824235Smarkfen 	 *
11834235Smarkfen 	 * DELETE is different, because you can leave either "src" or "dst"
11844235Smarkfen 	 * blank!  You need to explode if one of them is full, and not assume
11854235Smarkfen 	 * that the other is set.
11864235Smarkfen 	 */
11874235Smarkfen 
11884235Smarkfen 	if (dsthp == NULL) {
11894235Smarkfen 		/*
11904235Smarkfen 		 * No destination address specified.
11914235Smarkfen 		 * With extended diagnostics, we don't have to bail the
11924235Smarkfen 		 * non-DELETE cases here.  The EINVAL diagnostics will be
11934235Smarkfen 		 * enough to inform the user(s) what happened.
11944235Smarkfen 		 */
11954235Smarkfen 		i = 0;
11964235Smarkfen 		do {
11974235Smarkfen 			if (srchp == &dummy.he) {
11984235Smarkfen 				/* Just to be sure... */
11994235Smarkfen 				srchp->h_addr_list[1] = NULL;
12004235Smarkfen 			} else if (srchp != NULL) {
12014235Smarkfen 				/* Degenerate case, h_addr_list[0] == NULL. */
12024235Smarkfen 				if (srchp->h_addr_list[i] == NULL)
12034235Smarkfen 					Bail("Empty source address list");
12044235Smarkfen 
12054235Smarkfen 				/*
12064235Smarkfen 				 * Fill in the src sockaddr.
12074235Smarkfen 				 */
12084235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
12094235Smarkfen 				bzero(sin6, sizeof (*sin6));
12104235Smarkfen 				bcopy(srchp->h_addr_list[i], &sin6->sin6_addr,
12114235Smarkfen 				    sizeof (struct in6_addr));
12124235Smarkfen 				sin6->sin6_family = AF_INET6;
12134235Smarkfen 				sin6->sin6_port = htons(srcport);
12144235Smarkfen 			}
12154235Smarkfen 
12164235Smarkfen 			/* Save off a copy for later writing... */
12174235Smarkfen 			msgp = (struct sadb_msg *)buffer;
12184235Smarkfen 			bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
12194235Smarkfen 
12204235Smarkfen 			rc = key_write(keysock, buffer,
12214235Smarkfen 			    SADB_64TO8(msgp->sadb_msg_len));
12224235Smarkfen 			if (rc == -1)
12234235Smarkfen 				Bail("write() to PF_KEY socket "
12244235Smarkfen 				    "(in doaddresses)");
12257749SThejaswini.Singarajipura@Sun.COM 			/*
12267749SThejaswini.Singarajipura@Sun.COM 			 * Sends the message to the Solaris Cluster daemon
12277749SThejaswini.Singarajipura@Sun.COM 			 */
12287749SThejaswini.Singarajipura@Sun.COM 
12297749SThejaswini.Singarajipura@Sun.COM 			if (in_cluster_mode) {
12307749SThejaswini.Singarajipura@Sun.COM 				(void) sendto(cluster_socket, buffer,
12317749SThejaswini.Singarajipura@Sun.COM 				    SADB_64TO8(msgp->sadb_msg_len), 0,
12327749SThejaswini.Singarajipura@Sun.COM 				    (struct sockaddr *)&cli_addr,
12337749SThejaswini.Singarajipura@Sun.COM 				    sizeof (cli_addr));
12347749SThejaswini.Singarajipura@Sun.COM 			}
12354235Smarkfen 
12364235Smarkfen 			time_critical_enter();
12374235Smarkfen 			do {
12384235Smarkfen 				rc = read(keysock, buffer, buffer_size);
12394235Smarkfen 				if (rc == -1)
12404235Smarkfen 					Bail("read (in doaddresses)");
12414235Smarkfen 			} while (msgp->sadb_msg_seq != seq ||
12424235Smarkfen 			    msgp->sadb_msg_pid != mypid);
12434235Smarkfen 			time_critical_exit();
12444235Smarkfen 
12454235Smarkfen 			if (msgp->sadb_msg_type != sadb_msg_type ||
12464235Smarkfen 			    msgp->sadb_msg_satype != sadb_msg_satype) {
12474235Smarkfen 				syslog((LOG_NOTICE|LOG_AUTH), gettext(
12484235Smarkfen 				    "doaddresses: Unexpected returned message "
12494235Smarkfen 				    "(%d exp %d)\n"), msgp->sadb_msg_type,
12504235Smarkfen 				    sadb_msg_type);
12514235Smarkfen 				Bail("doaddresses: Unexpected returned "
12524235Smarkfen 				    "message");
12534235Smarkfen 			}
12544235Smarkfen 
12554235Smarkfen 			errno = msgp->sadb_msg_errno;
12564235Smarkfen 			if (errno != 0) {
12574235Smarkfen 				if (errno == EINVAL) {
12584235Smarkfen 					WARN(ep, ebuf, gettext(
12594235Smarkfen 					    "One of the entered "
12604235Smarkfen 					    "values is incorrect."));
12614235Smarkfen 					print_diagnostic(stderr,
12624235Smarkfen 					    msgp->sadb_x_msg_diagnostic);
12634235Smarkfen 				} else {
12644235Smarkfen 					Bail("return message (in doaddresses)");
12654235Smarkfen 				}
12664235Smarkfen 			}
12674235Smarkfen 
12684235Smarkfen 			/* ...and then restore the saved buffer. */
12694235Smarkfen 			msgp = (struct sadb_msg *)savebuf;
12704235Smarkfen 			bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
12714235Smarkfen 		} while (srchp != NULL && srchp->h_addr_list[++i] != NULL);
12724235Smarkfen 		return;
12734235Smarkfen 	}
12744235Smarkfen 
12754235Smarkfen 	single_dst = (dsthp == &dummy.he || dsthp->h_addr_list[1] == NULL);
12764235Smarkfen 
12774235Smarkfen 	for (i = 0; dsthp->h_addr_list[i] != NULL; i++) {
12784235Smarkfen 		if (dsthp == &dummy.he) {
12794235Smarkfen 			/* Just to be sure... */
12804235Smarkfen 			dsthp->h_addr_list[1] = NULL;
12814235Smarkfen 		} else {
12824235Smarkfen 			/*
12834235Smarkfen 			 * Fill in the dst sockaddr.
12844235Smarkfen 			 */
12854235Smarkfen 			sin6 = (struct sockaddr_in6 *)(dst + 1);
12864235Smarkfen 			bzero(sin6, sizeof (*sin6));
12874235Smarkfen 			bcopy(dsthp->h_addr_list[i], &sin6->sin6_addr,
12884235Smarkfen 			    sizeof (struct in6_addr));
12894235Smarkfen 			sin6->sin6_family = AF_INET6;
12904235Smarkfen 			sin6->sin6_port = htons(dstport);
12914235Smarkfen 		}
12924235Smarkfen 
12934235Smarkfen 		/*
12944235Smarkfen 		 * Try and assign src, if there's any ambiguity.
12954235Smarkfen 		 */
12964235Smarkfen 		if (!unspec_src && srchp != &dummy.he) {
12974235Smarkfen 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12984235Smarkfen 				/*
12994235Smarkfen 				 * IPv4 address.  Find an IPv4 address, then
13004235Smarkfen 				 * keep looking for a second one.  If a second
13014235Smarkfen 				 * exists, print a message, and fill in the
13024235Smarkfen 				 * unspecified address.
13034235Smarkfen 				 */
13044235Smarkfen 				first_match = NULL;
13054235Smarkfen 
13064235Smarkfen 				for (walker = srchp->h_addr_list;
13074235Smarkfen 				    *walker != NULL; walker++) {
13084235Smarkfen 					/* LINTED E_BAD_PTR_CAST_ALIGN */
13094235Smarkfen 					if (IN6_IS_ADDR_V4MAPPED(
13104235Smarkfen 					    (struct in6_addr *)*walker)) {
13114235Smarkfen 						if (first_match != NULL)
13124235Smarkfen 							break;
13134235Smarkfen 						else
13144235Smarkfen 							first_match = *walker;
13154235Smarkfen 					}
13164235Smarkfen 				}
13174235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
13184235Smarkfen 				bzero(sin6, sizeof (*sin6));
13194235Smarkfen 
13204235Smarkfen 				if (first_match == NULL) {
13214235Smarkfen 					/*
13224235Smarkfen 					 * No IPv4 hits.  Is this a single
13234235Smarkfen 					 * dest?
13244235Smarkfen 					 */
13254235Smarkfen 					WARN1(ep, ebuf, gettext(
13264235Smarkfen 					    "No IPv4 source address "
13274235Smarkfen 					    "for name %s.\n"), srchp->h_name);
13284235Smarkfen 					if (single_dst) {
13294235Smarkfen 						ERROR(ep, ebuf, gettext(
13304235Smarkfen 						    "Only single destination "
13314235Smarkfen 						    "IP address.\n"));
13324235Smarkfen 					} else {
13334235Smarkfen 						/* Continue, but do I print? */
13344235Smarkfen 						continue;  /* for loop */
13354235Smarkfen 					}
13364235Smarkfen 
13374235Smarkfen 					/* I should never reach here. */
13384235Smarkfen 				}
13394235Smarkfen 
13404235Smarkfen 				sin6->sin6_family = AF_INET6;
13414235Smarkfen 				sin6->sin6_port = htons(srcport);
13424235Smarkfen 				if (*walker != NULL) {
13434235Smarkfen 					/*
13444235Smarkfen 					 * Early loop exit.  It must've been
13454235Smarkfen 					 * multiple hits...
13464235Smarkfen 					 *
13474235Smarkfen 					 * Issue a null-source warning?
13484235Smarkfen 					 */
13494235Smarkfen 					WARN1(ep, ebuf, gettext(
13504235Smarkfen 					    "Multiple IPv4 source addresses "
13514235Smarkfen 					    "for %s, using unspecified source "
13524235Smarkfen 					    "instead."), srchp->h_name);
13534235Smarkfen 				} else {
13544235Smarkfen 					/*
13554235Smarkfen 					 * If I reach here w/o hitting the
13564235Smarkfen 					 * previous if statements, I have a
13574235Smarkfen 					 * single source address for this
13584235Smarkfen 					 * destination.
13594235Smarkfen 					 */
13604235Smarkfen 					bcopy(first_match, &sin6->sin6_addr,
13614235Smarkfen 					    sizeof (struct in6_addr));
13624235Smarkfen 				}
13634235Smarkfen 			} else {
13644235Smarkfen 				/*
13654235Smarkfen 				 * IPv6 address.  Find an IPv6 address.
13664235Smarkfen 				 * Unlike IPv4 addresses, things can get a
13674235Smarkfen 				 * little more sticky with scopes, etc.
13684235Smarkfen 				 */
13694235Smarkfen 				int dst_scope, src_scope;
13704235Smarkfen 
13714235Smarkfen 				dst_scope = ipv6_addr_scope(&sin6->sin6_addr);
13724235Smarkfen 
13734235Smarkfen 				first_match = NULL;
13744235Smarkfen 				for (walker = srchp->h_addr_list;
13754235Smarkfen 				    *walker != NULL; walker++) {
13764235Smarkfen 					/* LINTED E_BAD_PTR_CAST_ALIGN */
13774235Smarkfen 					if (!IN6_IS_ADDR_V4MAPPED(
13784235Smarkfen 					    (struct in6_addr *)*walker)) {
13794235Smarkfen 						/*
13804235Smarkfen 						 * Set first-match, etc.
13814235Smarkfen 						 * Take into account scopes,
13824235Smarkfen 						 * and other IPv6 thingies.
13834235Smarkfen 						 */
13844235Smarkfen 						src_scope = ipv6_addr_scope(
13854235Smarkfen 						    /* LINTED E_BAD_PTR_CAST */
13864235Smarkfen 						    (struct in6_addr *)*walker);
13874235Smarkfen 						if (src_scope == SCOPE_UNSPEC ||
13884235Smarkfen 						    src_scope == dst_scope) {
13894235Smarkfen 							if (first_match !=
13904235Smarkfen 							    NULL)
13914235Smarkfen 								break;
13924235Smarkfen 							else
13934235Smarkfen 								first_match =
13944235Smarkfen 								    *walker;
13954235Smarkfen 						}
13964235Smarkfen 					}
13974235Smarkfen 				}
13984235Smarkfen 
13994235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
14004235Smarkfen 				bzero(sin6, sizeof (*sin6));
14014235Smarkfen 				sin6->sin6_port = htons(srcport);
14024235Smarkfen 				if (first_match == NULL) {
14034235Smarkfen 					/*
14044235Smarkfen 					 * No IPv6 hits.  Is this a single
14054235Smarkfen 					 * dest?
14064235Smarkfen 					 */
14074235Smarkfen 					WARN1(ep, ebuf, gettext(
14084235Smarkfen 					    "No IPv6 source address of "
14094235Smarkfen 					    "matching scope for name %s.\n"),
14104235Smarkfen 					    srchp->h_name);
14114235Smarkfen 					if (single_dst) {
14124235Smarkfen 						ERROR(ep, ebuf, gettext(
14134235Smarkfen 						    "Only a single IPV6 "
14144235Smarkfen 						    "destination "
14154235Smarkfen 						    "address.\n"));
14164235Smarkfen 					} else {
14174235Smarkfen 						/* Continue, but do I print? */
14184235Smarkfen 						continue;  /* for loop */
14194235Smarkfen 					}
14204235Smarkfen 
14214235Smarkfen 					/* I should never reach here. */
14224235Smarkfen 				}
14234235Smarkfen 				sin6->sin6_family = AF_INET6;
14244235Smarkfen 				if (*walker != NULL) {
14254235Smarkfen 					/*
14264235Smarkfen 					 * Early loop exit.  Issue a
14274235Smarkfen 					 * null-source warning?
14284235Smarkfen 					 */
14294235Smarkfen 					WARN1(ep, ebuf, gettext(
14304235Smarkfen 					    "Multiple IPv6 source addresses "
14314235Smarkfen 					    "for %s of the same scope, using "
14324235Smarkfen 					    "unspecified source instead.\n"),
14334235Smarkfen 					    srchp->h_name);
14344235Smarkfen 				} else {
14354235Smarkfen 					/*
14364235Smarkfen 					 * If I reach here w/o hitting the
14374235Smarkfen 					 * previous if statements, I have a
14384235Smarkfen 					 * single source address for this
14394235Smarkfen 					 * destination.
14404235Smarkfen 					 */
14414235Smarkfen 					bcopy(first_match, &sin6->sin6_addr,
14424235Smarkfen 					    sizeof (struct in6_addr));
14434235Smarkfen 				}
14444235Smarkfen 			}
14454235Smarkfen 		}
14464235Smarkfen 
14474235Smarkfen 		/*
14484235Smarkfen 		 * If there are errors at this point there is no
14494235Smarkfen 		 * point sending anything to PF_KEY.
14504235Smarkfen 		 */
14514235Smarkfen 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
14524235Smarkfen 
14534235Smarkfen 		/* Save off a copy for later writing... */
14544235Smarkfen 		msgp = (struct sadb_msg *)buffer;
14554235Smarkfen 		bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
14564235Smarkfen 
14574235Smarkfen 		rc = key_write(keysock, buffer, SADB_64TO8(msgp->sadb_msg_len));
14584235Smarkfen 		if (rc == -1)
14594235Smarkfen 			Bail("write() to PF_KEY socket (in doaddresses)");
14604235Smarkfen 
14617749SThejaswini.Singarajipura@Sun.COM 		if (in_cluster_mode) {
14627749SThejaswini.Singarajipura@Sun.COM 			(void) sendto(cluster_socket, buffer,
14637749SThejaswini.Singarajipura@Sun.COM 			    SADB_64TO8(msgp->sadb_msg_len), 0,
14647749SThejaswini.Singarajipura@Sun.COM 			    (struct sockaddr *)&cli_addr,
14657749SThejaswini.Singarajipura@Sun.COM 			    sizeof (cli_addr));
14667749SThejaswini.Singarajipura@Sun.COM 		}
14674235Smarkfen 		/* Blank the key for paranoia's sake. */
14684235Smarkfen 		bzero(buffer, buffer_size);
14694235Smarkfen 		time_critical_enter();
14704235Smarkfen 		do {
14714235Smarkfen 			rc = read(keysock, buffer, buffer_size);
14724235Smarkfen 			if (rc == -1)
14734235Smarkfen 				Bail("read (in doaddresses)");
14744235Smarkfen 		} while (msgp->sadb_msg_seq != seq ||
14754235Smarkfen 		    msgp->sadb_msg_pid != mypid);
14764235Smarkfen 		time_critical_exit();
14774235Smarkfen 
14784235Smarkfen 		/*
14794235Smarkfen 		 * I should _never_ hit the following unless:
14804235Smarkfen 		 *
14814235Smarkfen 		 * 1. There is a kernel bug.
14824235Smarkfen 		 * 2. Another process is mistakenly using my pid in a PF_KEY
14834235Smarkfen 		 *    message.
14844235Smarkfen 		 */
14854235Smarkfen 		if (msgp->sadb_msg_type != sadb_msg_type ||
14864235Smarkfen 		    msgp->sadb_msg_satype != sadb_msg_satype) {
14874235Smarkfen 			syslog((LOG_NOTICE|LOG_AUTH), gettext(
14884235Smarkfen 			    "doaddresses: Unexpected returned message "
14894235Smarkfen 			    "(%d exp %d)\n"), msgp->sadb_msg_type,
14904235Smarkfen 			    sadb_msg_type);
14914235Smarkfen 			Bail("doaddresses: Unexpected returned message");
14924235Smarkfen 		}
14934235Smarkfen 
14944235Smarkfen 		if (msgp->sadb_msg_errno != 0) {
14954235Smarkfen 			char addrprint[INET6_ADDRSTRLEN];
14964235Smarkfen 			int on_errno = 0;
14974235Smarkfen 			char *on_errno_msg;
14984235Smarkfen 
14994235Smarkfen 			/*
15004235Smarkfen 			 * Print different error messages depending
15014235Smarkfen 			 * on the SADB message type being processed.
15024235Smarkfen 			 * If we get a ESRCH error for a GET/DELETE
15034235Smarkfen 			 * messages, we report that the SA does not
15044235Smarkfen 			 * exist. If we get a EEXIST error for a
15054235Smarkfen 			 * ADD/UPDATE message, we report that the
15064235Smarkfen 			 * SA already exists.
15074235Smarkfen 			 */
15084235Smarkfen 			if (sadb_msg_type == SADB_GET ||
15094235Smarkfen 			    sadb_msg_type == SADB_DELETE) {
15104235Smarkfen 				on_errno = ESRCH;
15114235Smarkfen 				on_errno_msg = "does not exist";
15124235Smarkfen 			} else if (sadb_msg_type == SADB_ADD ||
15134235Smarkfen 			    sadb_msg_type == SADB_UPDATE) {
15144235Smarkfen 				on_errno = EEXIST;
15154235Smarkfen 				on_errno_msg = "already exists";
15164235Smarkfen 			}
15174235Smarkfen 
15184235Smarkfen 			errno = msgp->sadb_msg_errno;
15194235Smarkfen 			if (errno == on_errno) {
15204235Smarkfen 				ERROR2(ep, ebuf, gettext(
15214235Smarkfen 				    "Association (type = %s) "
15224235Smarkfen 				    "with spi 0x%x and addr\n"),
15234235Smarkfen 				    rparsesatype(msgp->sadb_msg_satype),
15244235Smarkfen 				    ntohl(spi));
15254235Smarkfen 				ERROR2(ep, ebuf, "%s %s.\n",
15264235Smarkfen 				    do_inet_ntop(dsthp->h_addr_list[i],
15274342Spwernau 				    addrprint, sizeof (addrprint)),
15284235Smarkfen 				    on_errno_msg);
15294235Smarkfen 				msgp = (struct sadb_msg *)savebuf;
15304235Smarkfen 				bcopy(savebuf, buffer,
15314235Smarkfen 				    SADB_64TO8(msgp->sadb_msg_len));
15324235Smarkfen 				continue;
15334235Smarkfen 			} else {
15346668Smarkfen 				if (errno == EINVAL || errno == ESRCH) {
15354235Smarkfen 					ERROR2(ep, ebuf, gettext(
15364235Smarkfen 					    "PF_KEY Diagnostic code %u: %s.\n"),
15374235Smarkfen 					    msgp->sadb_x_msg_diagnostic,
15384235Smarkfen 					    keysock_diag(
15394235Smarkfen 					    msgp->sadb_x_msg_diagnostic));
15404235Smarkfen 				} else {
15414235Smarkfen 					Bail("return message (in doaddresses)");
15424235Smarkfen 				}
15434235Smarkfen 			}
15444235Smarkfen 		}
15454235Smarkfen 
15464235Smarkfen 		if (cmd == CMD_GET) {
15474235Smarkfen 			if (msgp->sadb_msg_len > MAX_GET_SIZE) {
15484235Smarkfen 				WARN1(ep, ebuf, gettext("WARNING:  "
15494235Smarkfen 				    "SA information bigger than %d bytes.\n"),
15504235Smarkfen 				    SADB_64TO8(MAX_GET_SIZE));
15514235Smarkfen 			}
15524867Spwernau 			print_samsg(stdout, buffer, B_FALSE, vflag, nflag);
15534235Smarkfen 		}
15544235Smarkfen 
15554235Smarkfen 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
15564235Smarkfen 
15574235Smarkfen 		/* ...and then restore the saved buffer. */
15584235Smarkfen 		msgp = (struct sadb_msg *)savebuf;
15594235Smarkfen 		bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
15604235Smarkfen 		lines_added++;
15614235Smarkfen 	}
15624235Smarkfen 
15634235Smarkfen 	/* Degenerate case, h_addr_list[0] == NULL. */
15644235Smarkfen 	if (i == 0)
15654235Smarkfen 		Bail("Empty destination address list");
15664235Smarkfen 
15674235Smarkfen 	/*
15684235Smarkfen 	 * free(ebuf) even if there are no errors.
15694235Smarkfen 	 * handle_errors() won't return here.
15704235Smarkfen 	 */
15714235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_TRUE);
15724235Smarkfen }
15734235Smarkfen 
15744235Smarkfen /*
15754235Smarkfen  * Perform an add or an update.  ADD and UPDATE are similar in the extensions
15764235Smarkfen  * they need.
15774235Smarkfen  */
15784235Smarkfen static void
15794235Smarkfen doaddup(int cmd, int satype, char *argv[], char *ebuf)
15804235Smarkfen {
15814235Smarkfen 	uint64_t *buffer, *nexthdr;
15824235Smarkfen 	struct sadb_msg msg;
15834235Smarkfen 	struct sadb_sa *assoc = NULL;
15846668Smarkfen 	struct sadb_x_pair *sadb_pair = NULL;
15854235Smarkfen 	struct sadb_address *src = NULL, *dst = NULL;
15864235Smarkfen 	struct sadb_address *isrc = NULL, *idst = NULL;
15874235Smarkfen 	struct sadb_address *natt_local = NULL, *natt_remote = NULL;
15884235Smarkfen 	struct sadb_key *encrypt = NULL, *auth = NULL;
15894235Smarkfen 	struct sadb_ident *srcid = NULL, *dstid = NULL;
15904235Smarkfen 	struct sadb_lifetime *hard = NULL, *soft = NULL;  /* Current? */
15917749SThejaswini.Singarajipura@Sun.COM 	struct sadb_lifetime *idle = NULL;
15927749SThejaswini.Singarajipura@Sun.COM 	struct sadb_x_replay_ctr *replay_ctr = NULL;
15934235Smarkfen 	struct sockaddr_in6 *sin6;
15944235Smarkfen 	/* MLS TODO:  Need sensitivity eventually. */
15954235Smarkfen 	int next, token, sa_len, alloclen, totallen = sizeof (msg), prefix;
15965989Spwernau 	uint32_t spi = 0;
1597*10824SMark.Fenwick@Sun.COM 	uint_t reserved_bits = 0;
15986668Smarkfen 	uint8_t	sadb_msg_type;
15994235Smarkfen 	char *thiscmd, *pstr;
16004235Smarkfen 	boolean_t readstate = B_FALSE, unspec_src = B_FALSE;
16014235Smarkfen 	boolean_t alloc_inner = B_FALSE, use_natt = B_FALSE;
16024235Smarkfen 	struct hostent *srchp = NULL, *dsthp = NULL, *isrchp = NULL,
16034235Smarkfen 	    *idsthp = NULL;
16044235Smarkfen 	struct hostent *natt_lhp = NULL, *natt_rhp = NULL;
16054235Smarkfen 	uint16_t srcport = 0, dstport = 0, natt_lport = 0, natt_rport = 0,
16064235Smarkfen 	    isrcport = 0, idstport = 0;
16074235Smarkfen 	uint8_t proto = 0, iproto = 0;
16084235Smarkfen 	char *ep = NULL;
16094235Smarkfen 
16106668Smarkfen 	switch (cmd) {
16116668Smarkfen 	case CMD_ADD:
16126668Smarkfen 		thiscmd = "add";
16136668Smarkfen 		sadb_msg_type = SADB_ADD;
16146668Smarkfen 		break;
16156668Smarkfen 	case CMD_UPDATE:
16166668Smarkfen 		thiscmd = "update";
16176668Smarkfen 		sadb_msg_type = SADB_UPDATE;
16186668Smarkfen 		break;
16196668Smarkfen 	case CMD_UPDATE_PAIR:
16206668Smarkfen 		thiscmd = "update-pair";
16216668Smarkfen 		sadb_msg_type = SADB_X_UPDATEPAIR;
16226668Smarkfen 		break;
16236668Smarkfen 	}
16244235Smarkfen 
16256668Smarkfen 	msg_init(&msg, sadb_msg_type, (uint8_t)satype);
16264235Smarkfen 	/* Assume last element in argv is set to NULL. */
16274235Smarkfen 	do {
16284235Smarkfen 		token = parseextval(*argv, &next);
16294235Smarkfen 		argv++;
16304235Smarkfen 		switch (token) {
16314235Smarkfen 		case TOK_EOF:
16324235Smarkfen 			/* Do nothing, I'm done. */
16334235Smarkfen 			break;
16344235Smarkfen 		case TOK_UNKNOWN:
16354235Smarkfen 			ERROR1(ep, ebuf, gettext(
16364235Smarkfen 			    "Unknown extension field \"%s\" \n"), *(argv - 1));
16374235Smarkfen 			break;
16384235Smarkfen 		case TOK_SPI:
16396668Smarkfen 		case TOK_PAIR_SPI:
16404235Smarkfen 		case TOK_REPLAY:
16414235Smarkfen 		case TOK_STATE:
16424235Smarkfen 		case TOK_AUTHALG:
16434235Smarkfen 		case TOK_ENCRALG:
16444235Smarkfen 		case TOK_ENCAP:
16454235Smarkfen 			/*
16464235Smarkfen 			 * May want to place this chunk of code in a function.
16474235Smarkfen 			 *
16484235Smarkfen 			 * This code checks for duplicate entries on a command
16494235Smarkfen 			 * line.
16504235Smarkfen 			 */
16514235Smarkfen 
16524235Smarkfen 			/* Allocate the SADB_EXT_SA extension. */
16534235Smarkfen 			if (assoc == NULL) {
16544235Smarkfen 				assoc = malloc(sizeof (*assoc));
16554235Smarkfen 				if (assoc == NULL)
16564235Smarkfen 					Bail("malloc(assoc)");
16574235Smarkfen 				bzero(assoc, sizeof (*assoc));
16584235Smarkfen 				assoc->sadb_sa_exttype = SADB_EXT_SA;
16594235Smarkfen 				assoc->sadb_sa_len =
16604235Smarkfen 				    SADB_8TO64(sizeof (*assoc));
16614235Smarkfen 				totallen += sizeof (*assoc);
16624235Smarkfen 			}
16634235Smarkfen 			switch (token) {
16644235Smarkfen 			case TOK_SPI:
16654235Smarkfen 				/*
16664235Smarkfen 				 * If some cretin types in "spi 0" then he/she
16674235Smarkfen 				 * can type in another SPI.
16684235Smarkfen 				 */
16694235Smarkfen 				if (assoc->sadb_sa_spi != 0) {
16704235Smarkfen 					ERROR(ep, ebuf, gettext(
16714235Smarkfen 					    "Can only specify "
16724235Smarkfen 					    "single SPI value.\n"));
16734235Smarkfen 					break;
16744235Smarkfen 				}
16754235Smarkfen 				/* Must convert SPI to network order! */
16764235Smarkfen 				assoc->sadb_sa_spi =
16774235Smarkfen 				    htonl((uint32_t)parsenum(*argv, B_TRUE,
16784235Smarkfen 				    ebuf));
16794235Smarkfen 				if (assoc->sadb_sa_spi == 0) {
16804235Smarkfen 					ERROR(ep, ebuf, gettext(
16814235Smarkfen 					    "Invalid SPI value \"0\" .\n"));
16824235Smarkfen 				}
16834235Smarkfen 				break;
16846668Smarkfen 			case TOK_PAIR_SPI:
16856668Smarkfen 				if (cmd == CMD_UPDATE_PAIR) {
16866668Smarkfen 					ERROR(ep, ebuf, gettext(
16876668Smarkfen 					    "pair-spi can not be used with the "
16886668Smarkfen 					    "\"update-pair\" command.\n"));
16896668Smarkfen 				}
16906668Smarkfen 				if (sadb_pair == NULL) {
16916668Smarkfen 					sadb_pair = malloc(sizeof (*sadb_pair));
16926668Smarkfen 					if (assoc == NULL)
16936668Smarkfen 						Bail("malloc(assoc)");
16946668Smarkfen 					bzero(sadb_pair, sizeof (*sadb_pair));
16956668Smarkfen 					totallen += sizeof (*sadb_pair);
16966668Smarkfen 				}
16976668Smarkfen 				if (sadb_pair->sadb_x_pair_spi != 0) {
16986668Smarkfen 					ERROR(ep, ebuf, gettext(
16996668Smarkfen 					    "Can only specify "
17006668Smarkfen 					    "single pair SPI value.\n"));
17016668Smarkfen 					break;
17026668Smarkfen 				}
17036668Smarkfen 				/* Must convert SPI to network order! */
17046668Smarkfen 				sadb_pair->sadb_x_pair_len =
17056668Smarkfen 				    SADB_8TO64(sizeof (*sadb_pair));
17066668Smarkfen 				sadb_pair->sadb_x_pair_exttype =
17076668Smarkfen 				    SADB_X_EXT_PAIR;
17086668Smarkfen 				sadb_pair->sadb_x_pair_spi =
17096668Smarkfen 				    htonl((uint32_t)parsenum(*argv, B_TRUE,
17106668Smarkfen 				    ebuf));
17116668Smarkfen 				if (sadb_pair->sadb_x_pair_spi == 0) {
17126668Smarkfen 					ERROR(ep, ebuf, gettext(
17136668Smarkfen 					    "Invalid SPI value \"0\" .\n"));
17146668Smarkfen 				}
17156668Smarkfen 				assoc->sadb_sa_flags |=
17166668Smarkfen 				    SADB_X_SAFLAGS_PAIRED;
17176668Smarkfen 				break;
17184235Smarkfen 			case TOK_REPLAY:
17194235Smarkfen 				/*
17204235Smarkfen 				 * That same cretin can do the same with
17214235Smarkfen 				 * replay.
17224235Smarkfen 				 */
17234235Smarkfen 				if (assoc->sadb_sa_replay != 0) {
17244235Smarkfen 					ERROR(ep, ebuf, gettext(
17254235Smarkfen 					    "Can only specify "
17264235Smarkfen 					    "single replay window size.\n"));
17274235Smarkfen 					break;
17284235Smarkfen 				}
17294235Smarkfen 				assoc->sadb_sa_replay =
17304235Smarkfen 				    (uint8_t)parsenum(*argv, B_TRUE, ebuf);
17314235Smarkfen 				if (assoc->sadb_sa_replay != 0) {
17324235Smarkfen 					WARN(ep, ebuf, gettext(
17334235Smarkfen 					    "WARNING:  Replay with manual"
17344235Smarkfen 					    " keying considered harmful.\n"));
17354235Smarkfen 				}
17364235Smarkfen 				break;
17374235Smarkfen 			case TOK_STATE:
17384235Smarkfen 				/*
17394235Smarkfen 				 * 0 is an actual state value, LARVAL.  This
17404235Smarkfen 				 * means that one can type in the larval state
17414235Smarkfen 				 * and then type in another state on the same
17424235Smarkfen 				 * command line.
17434235Smarkfen 				 */
17444235Smarkfen 				if (assoc->sadb_sa_state != 0) {
17454235Smarkfen 					ERROR(ep, ebuf, gettext(
17464235Smarkfen 					    "Can only specify "
17474235Smarkfen 					    "single SA state.\n"));
17484235Smarkfen 					break;
17494235Smarkfen 				}
17504235Smarkfen 				assoc->sadb_sa_state = parsestate(*argv,
17514235Smarkfen 				    ebuf);
17524235Smarkfen 				readstate = B_TRUE;
17534235Smarkfen 				break;
17544235Smarkfen 			case TOK_AUTHALG:
17554235Smarkfen 				if (assoc->sadb_sa_auth != 0) {
17564235Smarkfen 					ERROR(ep, ebuf, gettext(
17574235Smarkfen 					    "Can only specify "
17584235Smarkfen 					    "single auth algorithm.\n"));
17594235Smarkfen 					break;
17604235Smarkfen 				}
17614235Smarkfen 				assoc->sadb_sa_auth = parsealg(*argv,
17624235Smarkfen 				    IPSEC_PROTO_AH, ebuf);
17634235Smarkfen 				break;
17644235Smarkfen 			case TOK_ENCRALG:
17654235Smarkfen 				if (satype == SADB_SATYPE_AH) {
17664235Smarkfen 					ERROR(ep, ebuf, gettext("Cannot specify"
17674235Smarkfen 					    " encryption with SA type ah.\n"));
17684235Smarkfen 					break;
17694235Smarkfen 				}
17704235Smarkfen 				if (assoc->sadb_sa_encrypt != 0) {
17714235Smarkfen 					ERROR(ep, ebuf, gettext(
17724235Smarkfen 					    "Can only specify "
17734235Smarkfen 					    "single encryption algorithm.\n"));
17744235Smarkfen 					break;
17754235Smarkfen 				}
17764235Smarkfen 				assoc->sadb_sa_encrypt = parsealg(*argv,
17774235Smarkfen 				    IPSEC_PROTO_ESP, ebuf);
17784235Smarkfen 				break;
17794235Smarkfen 			case TOK_ENCAP:
17804235Smarkfen 				if (use_natt) {
17814235Smarkfen 					ERROR(ep, ebuf, gettext(
17824235Smarkfen 					    "Can only specify single"
17834235Smarkfen 					    " encapsulation.\n"));
17844235Smarkfen 					break;
17854235Smarkfen 				}
17864235Smarkfen 				if (strncmp(*argv, "udp", 3)) {
17874235Smarkfen 					ERROR(ep, ebuf, gettext(
17884235Smarkfen 					    "Can only specify udp"
17894235Smarkfen 					    " encapsulation.\n"));
17904235Smarkfen 					break;
17914235Smarkfen 				}
17924235Smarkfen 				use_natt = B_TRUE;
17934235Smarkfen 				/* set assoc flags later */
17944235Smarkfen 				break;
17954235Smarkfen 			}
17964235Smarkfen 			argv++;
17974235Smarkfen 			break;
17984235Smarkfen 		case TOK_SRCPORT:
17994235Smarkfen 			if (srcport != 0) {
18004235Smarkfen 				ERROR(ep, ebuf,  gettext("Can only specify "
18014235Smarkfen 				    "single source port.\n"));
18024235Smarkfen 				break;
18034235Smarkfen 			}
18044235Smarkfen 			srcport = parsenum(*argv, B_TRUE, ebuf);
18054235Smarkfen 			argv++;
18064235Smarkfen 			break;
18074235Smarkfen 		case TOK_DSTPORT:
18084235Smarkfen 			if (dstport != 0) {
18094235Smarkfen 				ERROR(ep, ebuf, gettext("Can only specify "
18104235Smarkfen 				    "single destination port.\n"));
18114235Smarkfen 				break;
18124235Smarkfen 			}
18134235Smarkfen 			dstport = parsenum(*argv, B_TRUE, ebuf);
18144235Smarkfen 			argv++;
18154235Smarkfen 			break;
18164235Smarkfen 		case TOK_ISRCPORT:
18174235Smarkfen 			alloc_inner = B_TRUE;
18184235Smarkfen 			if (isrcport != 0) {
18194235Smarkfen 				ERROR(ep, ebuf, gettext(
18204235Smarkfen 				    "Can only specify "
18214235Smarkfen 				    "single inner-source port.\n"));
18224235Smarkfen 				break;
18234235Smarkfen 			}
18244235Smarkfen 			isrcport = parsenum(*argv, B_TRUE, ebuf);
18254235Smarkfen 			argv++;
18264235Smarkfen 			break;
18274235Smarkfen 		case TOK_IDSTPORT:
18284235Smarkfen 			alloc_inner = B_TRUE;
18294235Smarkfen 			if (idstport != 0) {
18304235Smarkfen 				ERROR(ep, ebuf, gettext(
18314235Smarkfen 				    "Can only specify "
18324235Smarkfen 				    "single inner-destination port.\n"));
18334235Smarkfen 				break;
18344235Smarkfen 			}
18354235Smarkfen 			idstport = parsenum(*argv, B_TRUE, ebuf);
18364235Smarkfen 			argv++;
18374235Smarkfen 			break;
18384235Smarkfen 		case TOK_NATLPORT:
18394235Smarkfen 			if (natt_lport != 0) {
18404235Smarkfen 				ERROR(ep, ebuf, gettext(
18414235Smarkfen 				    "Can only specify "
18424235Smarkfen 				    "single NAT-T local port.\n"));
18434235Smarkfen 				break;
18444235Smarkfen 			}
18454235Smarkfen 			natt_lport = parsenum(*argv, B_TRUE, ebuf);
18464235Smarkfen 			argv++;
18474235Smarkfen 			break;
18484235Smarkfen 		case TOK_NATRPORT:
18494235Smarkfen 			if (natt_rport != 0) {
18504235Smarkfen 				ERROR(ep, ebuf, gettext(
18514235Smarkfen 				    "Can only specify "
18524235Smarkfen 				    "single NAT-T remote port.\n"));
18534235Smarkfen 				break;
18544235Smarkfen 			}
18554235Smarkfen 			natt_rport = parsenum(*argv, B_TRUE, ebuf);
18564235Smarkfen 			argv++;
18574235Smarkfen 			break;
18584235Smarkfen 
18594235Smarkfen 		case TOK_PROTO:
18604235Smarkfen 			if (proto != 0) {
18614235Smarkfen 				ERROR(ep, ebuf, gettext(
18624235Smarkfen 				    "Can only specify "
18634235Smarkfen 				    "single protocol.\n"));
18644235Smarkfen 				break;
18654235Smarkfen 			}
18664235Smarkfen 			proto = parsenum(*argv, B_TRUE, ebuf);
18674235Smarkfen 			argv++;
18684235Smarkfen 			break;
18694235Smarkfen 		case TOK_IPROTO:
18704235Smarkfen 			alloc_inner = B_TRUE;
18714235Smarkfen 			if (iproto != 0) {
18724235Smarkfen 				ERROR(ep, ebuf, gettext(
18734235Smarkfen 				    "Can only specify "
18744235Smarkfen 				    "single inner protocol.\n"));
18754235Smarkfen 				break;
18764235Smarkfen 			}
18774235Smarkfen 			iproto = parsenum(*argv, B_TRUE, ebuf);
18784235Smarkfen 			argv++;
18794235Smarkfen 			break;
18804235Smarkfen 		case TOK_SRCADDR:
18814235Smarkfen 		case TOK_SRCADDR6:
18824235Smarkfen 			if (src != NULL) {
18834235Smarkfen 				ERROR(ep, ebuf, gettext(
18844235Smarkfen 				    "Can only specify "
18854235Smarkfen 				    "single source address.\n"));
18864235Smarkfen 				break;
18874235Smarkfen 			}
18884235Smarkfen 			sa_len = parseaddr(*argv, &srchp,
18894235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
18904235Smarkfen 			if (srchp == NULL) {
18914235Smarkfen 				ERROR1(ep, ebuf, gettext(
18924235Smarkfen 				    "Unknown src address \"%s\"\n"), *argv);
18934235Smarkfen 				break;
18944235Smarkfen 			}
18954235Smarkfen 			argv++;
18964235Smarkfen 			/*
18974235Smarkfen 			 * Round of the sockaddr length to an 8 byte
18984235Smarkfen 			 * boundary to make PF_KEY happy.
18994235Smarkfen 			 */
19004235Smarkfen 			alloclen = sizeof (*src) + roundup(sa_len, 8);
19014235Smarkfen 			src = malloc(alloclen);
19024235Smarkfen 			if (src == NULL)
19034235Smarkfen 				Bail("malloc(src)");
19044235Smarkfen 			totallen += alloclen;
19054235Smarkfen 			src->sadb_address_len = SADB_8TO64(alloclen);
19064235Smarkfen 			src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
19074235Smarkfen 			src->sadb_address_reserved = 0;
19084235Smarkfen 			src->sadb_address_prefixlen = 0;
19094235Smarkfen 			src->sadb_address_proto = 0;
19104235Smarkfen 			if (srchp == &dummy.he) {
19114235Smarkfen 				/*
19124235Smarkfen 				 * Single address with -n flag.
19134235Smarkfen 				 */
19144235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
19154235Smarkfen 				bzero(sin6, sizeof (*sin6));
19164235Smarkfen 				sin6->sin6_family = AF_INET6;
19174235Smarkfen 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
19184235Smarkfen 				    sizeof (struct in6_addr));
19194235Smarkfen 			}
19204235Smarkfen 			break;
19214235Smarkfen 		case TOK_DSTADDR:
19224235Smarkfen 		case TOK_DSTADDR6:
19234235Smarkfen 			if (dst != NULL) {
19244235Smarkfen 				ERROR(ep, ebuf, gettext(
19254235Smarkfen 				    "Can only specify single "
19264235Smarkfen 				    "destination address.\n"));
19274235Smarkfen 				break;
19284235Smarkfen 			}
19294235Smarkfen 			sa_len = parseaddr(*argv, &dsthp,
19304235Smarkfen 			    (token == TOK_DSTADDR6), ebuf);
19314235Smarkfen 			if (dsthp == NULL) {
19324235Smarkfen 				ERROR1(ep, ebuf, gettext(
19334235Smarkfen 				    "Unknown dst address \"%s\"\n"), *argv);
19344235Smarkfen 				break;
19354235Smarkfen 			}
19364235Smarkfen 			argv++;
19374235Smarkfen 			alloclen = sizeof (*dst) + roundup(sa_len, 8);
19384235Smarkfen 			dst = malloc(alloclen);
19394235Smarkfen 			if (dst == NULL)
19404235Smarkfen 				Bail("malloc(dst)");
19414235Smarkfen 			totallen += alloclen;
19424235Smarkfen 			dst->sadb_address_len = SADB_8TO64(alloclen);
19434235Smarkfen 			dst->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
19444235Smarkfen 			dst->sadb_address_reserved = 0;
19454235Smarkfen 			dst->sadb_address_prefixlen = 0;
19464235Smarkfen 			dst->sadb_address_proto = 0;
19474235Smarkfen 			if (dsthp == &dummy.he) {
19484235Smarkfen 				/*
19494235Smarkfen 				 * Single address with -n flag.
19504235Smarkfen 				 */
19514235Smarkfen 				sin6 = (struct sockaddr_in6 *)(dst + 1);
19524235Smarkfen 				bzero(sin6, sizeof (*sin6));
19534235Smarkfen 				sin6->sin6_family = AF_INET6;
19544235Smarkfen 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
19554235Smarkfen 				    sizeof (struct in6_addr));
19564235Smarkfen 			}
19574235Smarkfen 			break;
19584235Smarkfen 		case TOK_PROXYADDR:
19594235Smarkfen 		case TOK_PROXYADDR6:
19604235Smarkfen 			if (isrc != NULL) {
19614235Smarkfen 				ERROR(ep, ebuf, gettext(
19624235Smarkfen 				    "Can only specify single "
19634235Smarkfen 				    "proxy/inner-source address.\n"));
19644235Smarkfen 				break;
19654235Smarkfen 			}
19664235Smarkfen 			if ((pstr = strchr(*argv, '/')) != NULL) {
19674235Smarkfen 				/* Parse out the prefix. */
19684235Smarkfen 				errno = 0;
19694235Smarkfen 				prefix = strtol(pstr + 1, NULL, 10);
19704235Smarkfen 				if (errno != 0) {
19714235Smarkfen 					ERROR1(ep, ebuf, gettext(
19724235Smarkfen 					    "Invalid prefix %s."), pstr);
19734235Smarkfen 					break;
19744235Smarkfen 				}
19754235Smarkfen 				/* Recycle pstr */
19764235Smarkfen 				alloclen = (int)(pstr - *argv);
19774235Smarkfen 				pstr = malloc(alloclen + 1);
19784235Smarkfen 				if (pstr == NULL) {
19794235Smarkfen 					Bail("malloc(pstr)");
19804235Smarkfen 				}
19814235Smarkfen 				(void) strlcpy(pstr, *argv, alloclen + 1);
19824235Smarkfen 			} else {
19834235Smarkfen 				pstr = *argv;
19844235Smarkfen 				/*
19854235Smarkfen 				 * Assume mapping to AF_INET6, and we're a host.
19864235Smarkfen 				 * XXX some miscreants may still make classful
19874235Smarkfen 				 * assumptions.  If this is a problem, fix it
19884235Smarkfen 				 * here.
19894235Smarkfen 				 */
19904235Smarkfen 				prefix = 128;
19914235Smarkfen 			}
19924235Smarkfen 			sa_len = parseaddr(pstr, &isrchp,
19934235Smarkfen 			    (token == TOK_PROXYADDR6), ebuf);
19944235Smarkfen 			if (isrchp == NULL) {
19954235Smarkfen 				ERROR1(ep, ebuf, gettext(
19964235Smarkfen 				    "Unknown proxy/inner-source address "
19974235Smarkfen 				    "\"%s\"\n"), *argv);
19984235Smarkfen 				break;
19994235Smarkfen 			}
20004235Smarkfen 			if (pstr != *argv)
20014235Smarkfen 				free(pstr);
20024235Smarkfen 			argv++;
20034235Smarkfen 			alloclen = sizeof (*isrc) + roundup(sa_len, 8);
20044235Smarkfen 			isrc = malloc(alloclen);
20054235Smarkfen 			if (isrc == NULL)
20064235Smarkfen 				Bail("malloc(isrc)");
20074235Smarkfen 			totallen += alloclen;
20084235Smarkfen 			isrc->sadb_address_len = SADB_8TO64(alloclen);
20094235Smarkfen 			isrc->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
20104235Smarkfen 			isrc->sadb_address_reserved = 0;
20114235Smarkfen 			isrc->sadb_address_prefixlen = prefix;
20124235Smarkfen 			isrc->sadb_address_proto = 0;
20134235Smarkfen 			if (isrchp == &dummy.he ||
20144235Smarkfen 			    isrchp->h_addr_list[1] == NULL) {
20154235Smarkfen 				/*
20164235Smarkfen 				 * Single address with -n flag or single name.
20174235Smarkfen 				 */
20184235Smarkfen 				sin6 = (struct sockaddr_in6 *)(isrc + 1);
20194235Smarkfen 				bzero(sin6, sizeof (*sin6));
20204235Smarkfen 				sin6->sin6_family = AF_INET6;
20214235Smarkfen 				bcopy(isrchp->h_addr_list[0], &sin6->sin6_addr,
20224235Smarkfen 				    sizeof (struct in6_addr));
20234235Smarkfen 				/*
20244235Smarkfen 				 * normalize prefixlen for IPv4-mapped
20254235Smarkfen 				 * addresses.
20264235Smarkfen 				 */
20274235Smarkfen 				if (prefix <= 32 &&
20284235Smarkfen 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
20294235Smarkfen 					isrc->sadb_address_prefixlen += 96;
20304235Smarkfen 				alloc_inner = B_TRUE;
20314235Smarkfen 			} else {
20324235Smarkfen 				/*
20334235Smarkfen 				 * If the proxy/isrc address is vague, don't
20344235Smarkfen 				 * bother.
20354235Smarkfen 				 */
20364235Smarkfen 				totallen -= alloclen;
20374235Smarkfen 				free(isrc);
20384235Smarkfen 				isrc = NULL;
20394235Smarkfen 				WARN1(ep, ebuf, gettext(
20404235Smarkfen 				    "Proxy/inner-source address %s "
20414235Smarkfen 				    "is vague, not using.\n"), isrchp->h_name);
20424235Smarkfen 				freehostent(isrchp);
20434235Smarkfen 				isrchp = NULL;
20444235Smarkfen 				break;
20454235Smarkfen 			}
20464235Smarkfen 			break;
20474235Smarkfen 		case TOK_IDSTADDR:
20484235Smarkfen 		case TOK_IDSTADDR6:
20494235Smarkfen 			if (idst != NULL) {
20504235Smarkfen 				ERROR(ep, ebuf, gettext(
20514235Smarkfen 				    "Can only specify single "
20524235Smarkfen 				    "inner-destination address.\n"));
20534235Smarkfen 				break;
20544235Smarkfen 			}
20554235Smarkfen 			if ((pstr = strchr(*argv, '/')) != NULL) {
20564235Smarkfen 				/* Parse out the prefix. */
20574235Smarkfen 				errno = 0;
20584235Smarkfen 				prefix = strtol(pstr + 1, NULL, 10);
20594235Smarkfen 				if (errno != 0) {
20604235Smarkfen 					ERROR1(ep, ebuf, gettext(
20614235Smarkfen 					    "Invalid prefix %s.\n"), pstr);
20624235Smarkfen 					break;
20634235Smarkfen 				}
20644235Smarkfen 				/* Recycle pstr */
20654235Smarkfen 				alloclen = (int)(pstr - *argv);
20664235Smarkfen 				pstr = malloc(alloclen + 1);
20674235Smarkfen 				if (pstr == NULL) {
20684235Smarkfen 					Bail("malloc(pstr)");
20694235Smarkfen 				}
20704235Smarkfen 				(void) strlcpy(pstr, *argv, alloclen + 1);
20714235Smarkfen 			} else {
20724235Smarkfen 				pstr = *argv;
20734235Smarkfen 				/*
20744235Smarkfen 				 * Assume mapping to AF_INET6, and we're a host.
20754235Smarkfen 				 * XXX some miscreants may still make classful
20764235Smarkfen 				 * assumptions.  If this is a problem, fix it
20774235Smarkfen 				 * here.
20784235Smarkfen 				 */
20794235Smarkfen 				prefix = 128;
20804235Smarkfen 			}
20814235Smarkfen 			sa_len = parseaddr(pstr, &idsthp,
20824235Smarkfen 			    (token == TOK_IDSTADDR6), ebuf);
20834235Smarkfen 			if (idsthp == NULL) {
20844235Smarkfen 				ERROR1(ep, ebuf, gettext(
20854235Smarkfen 				    "Unknown Inner Src address "
20864235Smarkfen 				    " \"%s\"\n"), *argv);
20874235Smarkfen 				break;
20884235Smarkfen 			}
20894235Smarkfen 			if (pstr != *argv)
20904235Smarkfen 				free(pstr);
20914235Smarkfen 			argv++;
20924235Smarkfen 			alloclen = sizeof (*idst) + roundup(sa_len, 8);
20934235Smarkfen 			idst = malloc(alloclen);
20944235Smarkfen 			if (idst == NULL)
20954235Smarkfen 				Bail("malloc(idst)");
20964235Smarkfen 			totallen += alloclen;
20974235Smarkfen 			idst->sadb_address_len = SADB_8TO64(alloclen);
20984235Smarkfen 			idst->sadb_address_exttype =
20994235Smarkfen 			    SADB_X_EXT_ADDRESS_INNER_DST;
21004235Smarkfen 			idst->sadb_address_reserved = 0;
21014235Smarkfen 			idst->sadb_address_prefixlen = prefix;
21024235Smarkfen 			idst->sadb_address_proto = 0;
21034235Smarkfen 			if (idsthp == &dummy.he ||
21044235Smarkfen 			    idsthp->h_addr_list[1] == NULL) {
21054235Smarkfen 				/*
21064235Smarkfen 				 * Single address with -n flag or single name.
21074235Smarkfen 				 */
21084235Smarkfen 				sin6 = (struct sockaddr_in6 *)(idst + 1);
21094235Smarkfen 				bzero(sin6, sizeof (*sin6));
21104235Smarkfen 				sin6->sin6_family = AF_INET6;
21114235Smarkfen 				bcopy(idsthp->h_addr_list[0], &sin6->sin6_addr,
21124235Smarkfen 				    sizeof (struct in6_addr));
21134235Smarkfen 				/*
21144235Smarkfen 				 * normalize prefixlen for IPv4-mapped
21154235Smarkfen 				 * addresses.
21164235Smarkfen 				 */
21174235Smarkfen 				if (prefix <= 32 &&
21184235Smarkfen 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
21194235Smarkfen 					idst->sadb_address_prefixlen += 96;
21204235Smarkfen 				alloc_inner = B_TRUE;
21214235Smarkfen 			} else {
21224235Smarkfen 				/*
21234235Smarkfen 				 * If the idst address is vague, don't bother.
21244235Smarkfen 				 */
21254235Smarkfen 				totallen -= alloclen;
21264235Smarkfen 				free(idst);
21274235Smarkfen 				idst = NULL;
21284235Smarkfen 				WARN1(ep, ebuf, gettext(
21294235Smarkfen 				    "Inner destination address %s "
21304235Smarkfen 				    "is vague, not using.\n"), idsthp->h_name);
21314235Smarkfen 				freehostent(idsthp);
21324235Smarkfen 				idsthp = NULL;
21334235Smarkfen 				break;
21344235Smarkfen 			}
21354235Smarkfen 			break;
21364235Smarkfen 		case TOK_NATLOC:
21374235Smarkfen 			if (natt_local != NULL) {
21384235Smarkfen 				ERROR(ep, ebuf, gettext(
21394235Smarkfen 				    "Can only specify "
21404235Smarkfen 				    "single NAT-T local address.\n"));
21414235Smarkfen 				break;
21424235Smarkfen 			}
21434235Smarkfen 			sa_len = parseaddr(*argv, &natt_lhp, 0, ebuf);
21444235Smarkfen 			if (natt_lhp == NULL) {
21454235Smarkfen 				ERROR1(ep, ebuf, gettext(
21464235Smarkfen 				    "Unknown NAT-T local address \"%s\"\n"),
21474235Smarkfen 				    *argv);
21484235Smarkfen 				break;
21494235Smarkfen 			}
21504235Smarkfen 			argv++;
21514235Smarkfen 			/*
21524235Smarkfen 			 * Round of the sockaddr length to an 8 byte
21534235Smarkfen 			 * boundary to make PF_KEY happy.
21544235Smarkfen 			 */
21554235Smarkfen 			alloclen = sizeof (*natt_local) + roundup(sa_len, 8);
21564235Smarkfen 			natt_local = malloc(alloclen);
21574235Smarkfen 			if (natt_local == NULL)
21584235Smarkfen 				Bail("malloc(natt_local)");
21594235Smarkfen 			totallen += alloclen;
21604235Smarkfen 			natt_local->sadb_address_len = SADB_8TO64(alloclen);
21614235Smarkfen 			natt_local->sadb_address_exttype =
21624235Smarkfen 			    SADB_X_EXT_ADDRESS_NATT_LOC;
21634235Smarkfen 			natt_local->sadb_address_reserved = 0;
21644235Smarkfen 			natt_local->sadb_address_prefixlen = 0;
21654235Smarkfen 			natt_local->sadb_address_proto = 0;
21664235Smarkfen 			if (natt_lhp == &dummy.he ||
21674235Smarkfen 			    natt_lhp->h_addr_list[1] == NULL) {
21684235Smarkfen 				/*
21694235Smarkfen 				 * Single address with -n flag or single name.
21704235Smarkfen 				 */
21714235Smarkfen 				sin6 = (struct sockaddr_in6 *)(natt_local + 1);
21724235Smarkfen 				bzero(sin6, sizeof (*sin6));
21734235Smarkfen 				sin6->sin6_family = AF_INET6;
21744235Smarkfen 				bcopy(natt_lhp->h_addr_list[0],
21754235Smarkfen 				    &sin6->sin6_addr, sizeof (struct in6_addr));
21764235Smarkfen 			} else {
21774235Smarkfen 				/*
21784235Smarkfen 				 * If the nat-local address is vague, don't
21794235Smarkfen 				 * bother.
21804235Smarkfen 				 */
21814235Smarkfen 				totallen -= alloclen;
21824235Smarkfen 				free(natt_local);
21834235Smarkfen 				natt_local = NULL;
21844235Smarkfen 				WARN1(ep, ebuf, gettext(
21854235Smarkfen 				    "NAT-T local address %s "
21864235Smarkfen 				    "is vague, not using.\n"),
21874235Smarkfen 				    natt_lhp->h_name);
21884235Smarkfen 				freehostent(natt_lhp);
21894235Smarkfen 				natt_lhp = NULL;
21904235Smarkfen 				break;
21914235Smarkfen 			}
21924235Smarkfen 			break;
21934235Smarkfen 		case TOK_NATREM:
21944235Smarkfen 			if (natt_remote != NULL) {
21954235Smarkfen 				ERROR(ep, ebuf, gettext(
21964235Smarkfen 				    "Can only specify "
21974235Smarkfen 				    "single NAT-T remote address.\n"));
21984235Smarkfen 				break;
21994235Smarkfen 			}
22004235Smarkfen 			sa_len = parseaddr(*argv, &natt_rhp, 0, ebuf);
22014235Smarkfen 			if (natt_rhp == NULL) {
22024235Smarkfen 				ERROR1(ep, ebuf, gettext(
22034235Smarkfen 				    "Unknown NAT-T remote address \"%s\"\n"),
22044235Smarkfen 				    *argv);
22054235Smarkfen 				break;
22064235Smarkfen 			}
22074235Smarkfen 			argv++;
22084235Smarkfen 			/*
22094235Smarkfen 			 * Round of the sockaddr length to an 8 byte
22104235Smarkfen 			 * boundary to make PF_KEY happy.
22114235Smarkfen 			 */
22124235Smarkfen 			alloclen = sizeof (*natt_remote) + roundup(sa_len, 8);
22134235Smarkfen 			natt_remote = malloc(alloclen);
22144235Smarkfen 			if (natt_remote == NULL)
22154235Smarkfen 				Bail("malloc(natt_remote)");
22164235Smarkfen 			totallen += alloclen;
22174235Smarkfen 			natt_remote->sadb_address_len = SADB_8TO64(alloclen);
22184235Smarkfen 			natt_remote->sadb_address_exttype =
22194235Smarkfen 			    SADB_X_EXT_ADDRESS_NATT_REM;
22204235Smarkfen 			natt_remote->sadb_address_reserved = 0;
22214235Smarkfen 			natt_remote->sadb_address_prefixlen = 0;
22224235Smarkfen 			natt_remote->sadb_address_proto = 0;
22234235Smarkfen 			if (natt_rhp == &dummy.he ||
22244235Smarkfen 			    natt_rhp->h_addr_list[1] == NULL) {
22254235Smarkfen 				/*
22264235Smarkfen 				 * Single address with -n flag or single name.
22274235Smarkfen 				 */
22284235Smarkfen 				sin6 = (struct sockaddr_in6 *)(natt_remote + 1);
22294235Smarkfen 				bzero(sin6, sizeof (*sin6));
22304235Smarkfen 				sin6->sin6_family = AF_INET6;
22314235Smarkfen 				bcopy(natt_rhp->h_addr_list[0],
22324235Smarkfen 				    &sin6->sin6_addr, sizeof (struct in6_addr));
22334235Smarkfen 			} else {
22344235Smarkfen 				/*
22354235Smarkfen 				 * If the nat-renote address is vague, don't
22364235Smarkfen 				 * bother.
22374235Smarkfen 				 */
22384235Smarkfen 				totallen -= alloclen;
22394235Smarkfen 				free(natt_remote);
22404235Smarkfen 				natt_remote = NULL;
22414235Smarkfen 				WARN1(ep, ebuf, gettext(
22424235Smarkfen 				    "NAT-T remote address %s "
22434235Smarkfen 				    "is vague, not using.\n"),
22444235Smarkfen 				    natt_rhp->h_name);
22454235Smarkfen 				freehostent(natt_rhp);
22464235Smarkfen 				natt_rhp = NULL;
22474235Smarkfen 				break;
22484235Smarkfen 			}
22494235Smarkfen 			break;
22504235Smarkfen 		case TOK_ENCRKEY:
22514235Smarkfen 			if (encrypt != NULL) {
22524235Smarkfen 				ERROR(ep, ebuf, gettext(
22534235Smarkfen 				    "Can only specify "
22544235Smarkfen 				    "single encryption key.\n"));
22554235Smarkfen 				break;
22564235Smarkfen 			}
22575989Spwernau 			if (assoc != NULL &&
22585989Spwernau 			    assoc->sadb_sa_encrypt == SADB_EALG_NULL) {
22594573Spwernau 				FATAL(ep, ebuf, gettext(
22604573Spwernau 				    "Cannot specify a key with NULL "
22614573Spwernau 				    "encryption algorithm.\n"));
22624573Spwernau 				break;
22634573Spwernau 			}
2264*10824SMark.Fenwick@Sun.COM 			encrypt = parsekey(*argv, ebuf, reserved_bits);
22654235Smarkfen 			argv++;
22664235Smarkfen 			if (encrypt == NULL) {
22674235Smarkfen 				ERROR(ep, ebuf, gettext(
22684235Smarkfen 				    "Invalid encryption key.\n"));
22694235Smarkfen 				break;
22704235Smarkfen 			}
22714235Smarkfen 			totallen += SADB_64TO8(encrypt->sadb_key_len);
22724235Smarkfen 			encrypt->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
22734235Smarkfen 			break;
22744235Smarkfen 		case TOK_AUTHKEY:
22754235Smarkfen 			if (auth != NULL) {
22764235Smarkfen 				ERROR(ep, ebuf, gettext(
22774235Smarkfen 				    "Can only specify single"
22784235Smarkfen 				    " authentication key.\n"));
22794235Smarkfen 				break;
22804235Smarkfen 			}
2281*10824SMark.Fenwick@Sun.COM 			auth = parsekey(*argv, ebuf, 0);
22824235Smarkfen 			argv++;
22834235Smarkfen 			if (auth == NULL) {
22844235Smarkfen 				ERROR(ep, ebuf, gettext(
22854235Smarkfen 				    "Invalid authentication key.\n"));
22864235Smarkfen 				break;
22874235Smarkfen 			}
22884235Smarkfen 			totallen += SADB_64TO8(auth->sadb_key_len);
22894235Smarkfen 			auth->sadb_key_exttype = SADB_EXT_KEY_AUTH;
22904235Smarkfen 			break;
22914235Smarkfen 		case TOK_SRCIDTYPE:
22924235Smarkfen 			if (*argv == NULL || *(argv + 1) == NULL) {
22934235Smarkfen 				FATAL(ep, ebuf, gettext(
22944235Smarkfen 				    "Unexpected end of command "
22954235Smarkfen 				    "line - Expecting Src Type.\n"));
22964235Smarkfen 				/* NOTREACHED */
22974235Smarkfen 				break;
22984235Smarkfen 			}
22994235Smarkfen 			if (srcid != NULL) {
23004235Smarkfen 				ERROR(ep, ebuf, gettext(
23014235Smarkfen 				    "Can only specify single"
23024235Smarkfen 				    " source certificate identity.\n"));
23034235Smarkfen 				break;
23044235Smarkfen 			}
23054235Smarkfen 			alloclen = sizeof (*srcid) +
23064235Smarkfen 			    roundup(strlen(*(argv + 1)) + 1, 8);
23074235Smarkfen 			srcid = malloc(alloclen);
23084235Smarkfen 			if (srcid == NULL)
23094235Smarkfen 				Bail("malloc(srcid)");
23104235Smarkfen 			totallen += alloclen;
23114235Smarkfen 			srcid->sadb_ident_type = parseidtype(*argv, ebuf);
23124235Smarkfen 			argv++;
23134235Smarkfen 			srcid->sadb_ident_len = SADB_8TO64(alloclen);
23144235Smarkfen 			srcid->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC;
23154235Smarkfen 			srcid->sadb_ident_reserved = 0;
23164235Smarkfen 			srcid->sadb_ident_id = 0;  /* Not useful here. */
23174235Smarkfen 			(void) strlcpy((char *)(srcid + 1), *argv, alloclen);
23184235Smarkfen 			argv++;
23194235Smarkfen 			break;
23204235Smarkfen 		case TOK_DSTIDTYPE:
23214235Smarkfen 			if (*argv == NULL || *(argv + 1) == NULL) {
23224235Smarkfen 				ERROR(ep, ebuf, gettext(
23234235Smarkfen 				    "Unexpected end of command"
23244235Smarkfen 				    " line - expecting dst type.\n"));
23254235Smarkfen 				break;
23264235Smarkfen 			}
23274235Smarkfen 			if (dstid != NULL) {
23284235Smarkfen 				ERROR(ep, ebuf, gettext(
23294235Smarkfen 				    "Can only specify single destination "
23304342Spwernau 				    "certificate identity.\n"));
23314235Smarkfen 				break;
23324235Smarkfen 			}
23334235Smarkfen 			alloclen = sizeof (*dstid) +
23344235Smarkfen 			    roundup(strlen(*(argv + 1)) + 1, 8);
23354235Smarkfen 			dstid = malloc(alloclen);
23364235Smarkfen 			if (dstid == NULL)
23374235Smarkfen 				Bail("malloc(dstid)");
23384235Smarkfen 			totallen += alloclen;
23394235Smarkfen 			dstid->sadb_ident_type = parseidtype(*argv, ebuf);
23404235Smarkfen 			argv++;
23414235Smarkfen 			dstid->sadb_ident_len = SADB_8TO64(alloclen);
23424235Smarkfen 			dstid->sadb_ident_exttype = SADB_EXT_IDENTITY_DST;
23434235Smarkfen 			dstid->sadb_ident_reserved = 0;
23444235Smarkfen 			dstid->sadb_ident_id = 0;  /* Not useful here. */
23454235Smarkfen 			(void) strlcpy((char *)(dstid + 1), *argv, alloclen);
23464235Smarkfen 			argv++;
23474235Smarkfen 			break;
23484235Smarkfen 		case TOK_HARD_ALLOC:
23494235Smarkfen 		case TOK_HARD_BYTES:
23504235Smarkfen 		case TOK_HARD_ADDTIME:
23514235Smarkfen 		case TOK_HARD_USETIME:
23524235Smarkfen 			if (hard == NULL) {
23534235Smarkfen 				hard = malloc(sizeof (*hard));
23544235Smarkfen 				if (hard == NULL)
23554235Smarkfen 					Bail("malloc(hard_lifetime)");
23564235Smarkfen 				bzero(hard, sizeof (*hard));
23574235Smarkfen 				hard->sadb_lifetime_exttype =
23584235Smarkfen 				    SADB_EXT_LIFETIME_HARD;
23594235Smarkfen 				hard->sadb_lifetime_len =
23604235Smarkfen 				    SADB_8TO64(sizeof (*hard));
23614235Smarkfen 				totallen += sizeof (*hard);
23624235Smarkfen 			}
23634235Smarkfen 			switch (token) {
23644235Smarkfen 			case TOK_HARD_ALLOC:
23654235Smarkfen 				if (hard->sadb_lifetime_allocations != 0) {
23664235Smarkfen 					ERROR(ep, ebuf, gettext(
23674235Smarkfen 					    "Can only specify single"
23684235Smarkfen 					    " hard allocation limit.\n"));
23694235Smarkfen 					break;
23704235Smarkfen 				}
23714235Smarkfen 				hard->sadb_lifetime_allocations =
23724235Smarkfen 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
23734235Smarkfen 				break;
23744235Smarkfen 			case TOK_HARD_BYTES:
23754235Smarkfen 				if (hard->sadb_lifetime_bytes != 0) {
23764235Smarkfen 					ERROR(ep, ebuf, gettext(
23774235Smarkfen 					    "Can only specify "
23784235Smarkfen 					    "single hard byte limit.\n"));
23794235Smarkfen 					break;
23804235Smarkfen 				}
23814235Smarkfen 				hard->sadb_lifetime_bytes = parsenum(*argv,
23824235Smarkfen 				    B_TRUE, ebuf);
23834235Smarkfen 				break;
23844235Smarkfen 			case TOK_HARD_ADDTIME:
23854235Smarkfen 				if (hard->sadb_lifetime_addtime != 0) {
23864235Smarkfen 					ERROR(ep, ebuf, gettext(
23874235Smarkfen 					    "Can only specify "
23884235Smarkfen 					    "single past-add lifetime.\n"));
23894235Smarkfen 					break;
23904235Smarkfen 				}
23914235Smarkfen 				hard->sadb_lifetime_addtime = parsenum(*argv,
23924235Smarkfen 				    B_TRUE, ebuf);
23934235Smarkfen 				break;
23944235Smarkfen 			case TOK_HARD_USETIME:
23954235Smarkfen 				if (hard->sadb_lifetime_usetime != 0) {
23964235Smarkfen 					ERROR(ep, ebuf, gettext(
23974235Smarkfen 					    "Can only specify "
23984235Smarkfen 					    "single past-use lifetime.\n"));
23994235Smarkfen 					break;
24004235Smarkfen 				}
24014235Smarkfen 				hard->sadb_lifetime_usetime = parsenum(*argv,
24024235Smarkfen 				    B_TRUE, ebuf);
24034235Smarkfen 				break;
24044235Smarkfen 			}
24054235Smarkfen 			argv++;
24064235Smarkfen 			break;
24074235Smarkfen 		case TOK_SOFT_ALLOC:
24084235Smarkfen 		case TOK_SOFT_BYTES:
24094235Smarkfen 		case TOK_SOFT_ADDTIME:
24104235Smarkfen 		case TOK_SOFT_USETIME:
24114235Smarkfen 			if (soft == NULL) {
24124235Smarkfen 				soft = malloc(sizeof (*soft));
24134235Smarkfen 				if (soft == NULL)
24144235Smarkfen 					Bail("malloc(soft_lifetime)");
24154235Smarkfen 				bzero(soft, sizeof (*soft));
24164235Smarkfen 				soft->sadb_lifetime_exttype =
24174235Smarkfen 				    SADB_EXT_LIFETIME_SOFT;
24184235Smarkfen 				soft->sadb_lifetime_len =
24194235Smarkfen 				    SADB_8TO64(sizeof (*soft));
24204235Smarkfen 				totallen += sizeof (*soft);
24214235Smarkfen 			}
24224235Smarkfen 			switch (token) {
24234235Smarkfen 			case TOK_SOFT_ALLOC:
24244235Smarkfen 				if (soft->sadb_lifetime_allocations != 0) {
24254235Smarkfen 					ERROR(ep, ebuf, gettext(
24264235Smarkfen 					    "Can only specify single"
24274235Smarkfen 					    " soft allocation limit.\n"));
24284235Smarkfen 					break;
24294235Smarkfen 				}
24304235Smarkfen 				soft->sadb_lifetime_allocations =
24314235Smarkfen 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
24324235Smarkfen 				break;
24334235Smarkfen 			case TOK_SOFT_BYTES:
24344235Smarkfen 				if (soft->sadb_lifetime_bytes != 0) {
24354235Smarkfen 					ERROR(ep, ebuf, gettext(
24364235Smarkfen 					    "Can only specify single"
24374235Smarkfen 					    " soft byte limit.\n"));
24384235Smarkfen 					break;
24394235Smarkfen 				}
24404235Smarkfen 				soft->sadb_lifetime_bytes = parsenum(*argv,
24414235Smarkfen 				    B_TRUE, ebuf);
24424235Smarkfen 				break;
24434235Smarkfen 			case TOK_SOFT_ADDTIME:
24444235Smarkfen 				if (soft->sadb_lifetime_addtime != 0) {
24454235Smarkfen 					ERROR(ep, ebuf, gettext(
24464235Smarkfen 					    "Can only specify single"
24474235Smarkfen 					    " past-add lifetime.\n"));
24484235Smarkfen 					break;
24494235Smarkfen 				}
24504235Smarkfen 				soft->sadb_lifetime_addtime = parsenum(*argv,
24514235Smarkfen 				    B_TRUE, ebuf);
24524235Smarkfen 				break;
24534235Smarkfen 			case TOK_SOFT_USETIME:
24544235Smarkfen 				if (soft->sadb_lifetime_usetime != 0) {
24554235Smarkfen 					ERROR(ep, ebuf, gettext(
24564235Smarkfen 					    "Can only specify single"
24574235Smarkfen 					    " past-use lifetime.\n"));
24584235Smarkfen 					break;
24594235Smarkfen 				}
24604235Smarkfen 				soft->sadb_lifetime_usetime = parsenum(*argv,
24614235Smarkfen 				    B_TRUE, ebuf);
24624235Smarkfen 				break;
24634235Smarkfen 			}
24644235Smarkfen 			argv++;
24654235Smarkfen 			break;
24666668Smarkfen 		case TOK_FLAG_INBOUND:
24676668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_INBOUND;
24686668Smarkfen 			break;
24696668Smarkfen 		case TOK_FLAG_OUTBOUND:
24706668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_OUTBOUND;
24716668Smarkfen 			break;
24727749SThejaswini.Singarajipura@Sun.COM 		case TOK_REPLAY_VALUE:
24737749SThejaswini.Singarajipura@Sun.COM 			if (replay_ctr != NULL) {
24747749SThejaswini.Singarajipura@Sun.COM 				ERROR(ep, ebuf, gettext(
24757749SThejaswini.Singarajipura@Sun.COM 				    "Can only specify single "
24767749SThejaswini.Singarajipura@Sun.COM 				    "replay value."));
24777749SThejaswini.Singarajipura@Sun.COM 				break;
24787749SThejaswini.Singarajipura@Sun.COM 			}
24797749SThejaswini.Singarajipura@Sun.COM 			replay_ctr = calloc(1, sizeof (*replay_ctr));
24807749SThejaswini.Singarajipura@Sun.COM 			if (replay_ctr == NULL) {
24817749SThejaswini.Singarajipura@Sun.COM 				Bail("malloc(replay value)");
24827749SThejaswini.Singarajipura@Sun.COM 			}
24837749SThejaswini.Singarajipura@Sun.COM 			/*
24847749SThejaswini.Singarajipura@Sun.COM 			 * We currently do not support a 64-bit
24857749SThejaswini.Singarajipura@Sun.COM 			 * replay value.  RFC 4301 will require one,
24867749SThejaswini.Singarajipura@Sun.COM 			 * however, and we have a field in place when
24877749SThejaswini.Singarajipura@Sun.COM 			 * 4301 is built.
24887749SThejaswini.Singarajipura@Sun.COM 			 */
24897749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_exttype = SADB_X_EXT_REPLAY_VALUE;
24907749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_len =
24917749SThejaswini.Singarajipura@Sun.COM 			    SADB_8TO64(sizeof (*replay_ctr));
24927749SThejaswini.Singarajipura@Sun.COM 			totallen += sizeof (*replay_ctr);
24937749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_replay32 = (uint32_t)parsenum(
24947749SThejaswini.Singarajipura@Sun.COM 			    *argv, B_TRUE, ebuf);
24957749SThejaswini.Singarajipura@Sun.COM 			argv++;
24967749SThejaswini.Singarajipura@Sun.COM 			break;
24977749SThejaswini.Singarajipura@Sun.COM 		case TOK_IDLE_ADDTIME:
24987749SThejaswini.Singarajipura@Sun.COM 		case TOK_IDLE_USETIME:
24997749SThejaswini.Singarajipura@Sun.COM 			if (idle == NULL) {
25007749SThejaswini.Singarajipura@Sun.COM 				idle = calloc(1, sizeof (*idle));
25017749SThejaswini.Singarajipura@Sun.COM 				if (idle == NULL) {
25027749SThejaswini.Singarajipura@Sun.COM 					Bail("malloc idle lifetime");
25037749SThejaswini.Singarajipura@Sun.COM 				}
25047749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_exttype =
25057749SThejaswini.Singarajipura@Sun.COM 				    SADB_X_EXT_LIFETIME_IDLE;
25067749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_len =
25077749SThejaswini.Singarajipura@Sun.COM 				    SADB_8TO64(sizeof (*idle));
25087749SThejaswini.Singarajipura@Sun.COM 				totallen += sizeof (*idle);
25097749SThejaswini.Singarajipura@Sun.COM 			}
25107749SThejaswini.Singarajipura@Sun.COM 			switch (token) {
25117749SThejaswini.Singarajipura@Sun.COM 			case TOK_IDLE_ADDTIME:
25127749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_addtime =
25137749SThejaswini.Singarajipura@Sun.COM 				    (uint32_t)parsenum(*argv,
25147749SThejaswini.Singarajipura@Sun.COM 				    B_TRUE, ebuf);
25157749SThejaswini.Singarajipura@Sun.COM 				break;
25167749SThejaswini.Singarajipura@Sun.COM 			case TOK_IDLE_USETIME:
25177749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_usetime =
25187749SThejaswini.Singarajipura@Sun.COM 				    (uint32_t)parsenum(*argv,
25197749SThejaswini.Singarajipura@Sun.COM 				    B_TRUE, ebuf);
25207749SThejaswini.Singarajipura@Sun.COM 				break;
25217749SThejaswini.Singarajipura@Sun.COM 			}
25227749SThejaswini.Singarajipura@Sun.COM 			argv++;
25237749SThejaswini.Singarajipura@Sun.COM 			break;
2524*10824SMark.Fenwick@Sun.COM 		case TOK_RESERVED:
2525*10824SMark.Fenwick@Sun.COM 			if (encrypt != NULL)
2526*10824SMark.Fenwick@Sun.COM 				ERROR(ep, ebuf, gettext(
2527*10824SMark.Fenwick@Sun.COM 				    "Reserved bits need to be "
2528*10824SMark.Fenwick@Sun.COM 				    "specified before key.\n"));
2529*10824SMark.Fenwick@Sun.COM 			reserved_bits = (uint_t)parsenum(*argv,
2530*10824SMark.Fenwick@Sun.COM 			    B_TRUE, ebuf);
2531*10824SMark.Fenwick@Sun.COM 			argv++;
2532*10824SMark.Fenwick@Sun.COM 			break;
25334235Smarkfen 		default:
25344235Smarkfen 			ERROR1(ep, ebuf, gettext(
25354235Smarkfen 			    "Don't use extension %s for add/update.\n"),
25364235Smarkfen 			    *(argv - 1));
25374235Smarkfen 			break;
25384235Smarkfen 		}
25394235Smarkfen 	} while (token != TOK_EOF);
25404235Smarkfen 
25414235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
25424235Smarkfen 
25434987Sdanmcd #define	PORT_ONLY_ALLOCATE(af, socktype, exttype, extvar, port) {  \
25444987Sdanmcd 	alloclen = sizeof (sadb_address_t) + roundup(sizeof (socktype), 8); \
25454987Sdanmcd 	(extvar) = calloc(1, alloclen); \
25464987Sdanmcd 	if ((extvar) == NULL) { \
25474987Sdanmcd 		Bail("malloc(implicit port)"); \
25484987Sdanmcd 	} \
25494987Sdanmcd 	totallen += alloclen; \
25504987Sdanmcd 	(extvar)->sadb_address_len = SADB_8TO64(alloclen); \
25514987Sdanmcd 	(extvar)->sadb_address_exttype = (exttype); \
25524987Sdanmcd 	/* sin/sin6 has equivalent offsets for ports! */ \
25534987Sdanmcd 	sin6 = (struct sockaddr_in6 *)((extvar) + 1); \
25544987Sdanmcd 	sin6->sin6_family = (af); \
25554987Sdanmcd 	sin6->sin6_port = (port); \
25564987Sdanmcd 	}
25574987Sdanmcd 
25584235Smarkfen 	/*
25594987Sdanmcd 	 * If we specify inner ports or NAT ports w/o addresses, we still need
25604987Sdanmcd 	 * to allocate.  Also, if we have one inner address, we need the
25614235Smarkfen 	 * other, even if we don't specify anything.
25624235Smarkfen 	 */
25634987Sdanmcd 	if (use_natt) {
25644987Sdanmcd 		if (natt_lport != 0 && natt_local == NULL) {
25654987Sdanmcd 			PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in,
25664987Sdanmcd 			    SADB_X_EXT_ADDRESS_NATT_LOC, natt_local,
25674987Sdanmcd 			    natt_lport);
25684987Sdanmcd 		}
25694987Sdanmcd 
25704987Sdanmcd 		if (natt_rport != 0 && natt_remote == NULL) {
25714987Sdanmcd 			PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in,
25724987Sdanmcd 			    SADB_X_EXT_ADDRESS_NATT_REM, natt_remote,
25734987Sdanmcd 			    natt_rport);
25744987Sdanmcd 		}
25754987Sdanmcd 	} else {
25764987Sdanmcd 		if (natt_lport != 0 || natt_rport != 0) {
25774987Sdanmcd 			ERROR(ep, ebuf, gettext("Must specify 'encap udp' "
25784987Sdanmcd 			    "with any NAT-T port.\n"));
25794987Sdanmcd 		} else if (natt_local != NULL || natt_remote != NULL) {
25804987Sdanmcd 			ERROR(ep, ebuf, gettext("Must specify 'encap udp' "
25814987Sdanmcd 			    "with any NAT-T address.\n"));
25824987Sdanmcd 		}
25834987Sdanmcd 	}
25844987Sdanmcd 
25854235Smarkfen 	if (alloc_inner && idst == NULL) {
25864987Sdanmcd 		PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6,
25874987Sdanmcd 		    SADB_X_EXT_ADDRESS_INNER_DST, idst, 0);
25884235Smarkfen 	}
25894235Smarkfen 
25904235Smarkfen 	if (alloc_inner && isrc == NULL) {
25914987Sdanmcd 		PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6,
25924987Sdanmcd 		    SADB_X_EXT_ADDRESS_INNER_SRC, isrc, 0);
25934235Smarkfen 	}
25944987Sdanmcd #undef PORT_ONLY_ALLOCATE
25954235Smarkfen 
25964235Smarkfen 	/*
25974235Smarkfen 	 * Okay, so now I have all of the potential extensions!
25984235Smarkfen 	 * Allocate a single contiguous buffer.  Keep in mind that it'll
25994235Smarkfen 	 * be enough because the key itself will be yanked.
26004235Smarkfen 	 */
26014235Smarkfen 
26024235Smarkfen 	if (src == NULL && dst != NULL) {
26034235Smarkfen 		/*
26044235Smarkfen 		 * Set explicit unspecified source address.
26054235Smarkfen 		 */
26064235Smarkfen 		size_t lenbytes = SADB_64TO8(dst->sadb_address_len);
26074235Smarkfen 
26084235Smarkfen 		unspec_src = B_TRUE;
26094235Smarkfen 		totallen += lenbytes;
26104235Smarkfen 		src = malloc(lenbytes);
26114235Smarkfen 		if (src == NULL)
26124235Smarkfen 			Bail("malloc(implicit src)");
26134235Smarkfen 		/* Confusing, but we're copying from DST to SRC.  :) */
26144235Smarkfen 		bcopy(dst, src, lenbytes);
26154235Smarkfen 		src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
26164235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
26174235Smarkfen 		bzero(sin6, sizeof (*sin6));
26184235Smarkfen 		sin6->sin6_family = AF_INET6;
26194235Smarkfen 	}
26204235Smarkfen 
26214235Smarkfen 	msg.sadb_msg_len = SADB_8TO64(totallen);
26224235Smarkfen 
26234235Smarkfen 	buffer = malloc(totallen);
26244235Smarkfen 	nexthdr = buffer;
26254235Smarkfen 	bcopy(&msg, nexthdr, sizeof (msg));
26264235Smarkfen 	nexthdr += SADB_8TO64(sizeof (msg));
26274235Smarkfen 	if (assoc != NULL) {
26284235Smarkfen 		if (assoc->sadb_sa_spi == 0) {
26294235Smarkfen 			ERROR1(ep, ebuf, gettext(
26304235Smarkfen 			    "The SPI value is missing for "
26314235Smarkfen 			    "the association you wish to %s.\n"), thiscmd);
26324235Smarkfen 		}
26334235Smarkfen 		if (assoc->sadb_sa_auth == 0 && assoc->sadb_sa_encrypt == 0 &&
26344342Spwernau 		    cmd == CMD_ADD) {
26354235Smarkfen 			free(assoc);
26364235Smarkfen 			FATAL(ep, ebuf, gettext(
26374235Smarkfen 			    "Select at least one algorithm "
26384235Smarkfen 			    "for this add.\n"));
26394235Smarkfen 		}
26404235Smarkfen 
26414235Smarkfen 		/* Hack to let user specify NULL ESP implicitly. */
26424235Smarkfen 		if (msg.sadb_msg_satype == SADB_SATYPE_ESP &&
26434235Smarkfen 		    assoc->sadb_sa_encrypt == 0)
26444235Smarkfen 			assoc->sadb_sa_encrypt = SADB_EALG_NULL;
26454235Smarkfen 
26464235Smarkfen 		/* 0 is an actual value.  Print a warning if it was entered. */
26474235Smarkfen 		if (assoc->sadb_sa_state == 0) {
26484235Smarkfen 			if (readstate) {
26494235Smarkfen 				ERROR(ep, ebuf, gettext(
26504235Smarkfen 				    "WARNING: Cannot set LARVAL SA state.\n"));
26514235Smarkfen 			}
26524235Smarkfen 			assoc->sadb_sa_state = SADB_SASTATE_MATURE;
26534235Smarkfen 		}
26544235Smarkfen 
26554235Smarkfen 		if (use_natt) {
26564235Smarkfen 			if (natt_remote != NULL)
26574235Smarkfen 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_REM;
26584235Smarkfen 			if (natt_local != NULL)
26594235Smarkfen 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_LOC;
26604235Smarkfen 		}
26614235Smarkfen 
26624235Smarkfen 		if (alloc_inner) {
26634235Smarkfen 			/*
26644235Smarkfen 			 * For now, assume RFC 3884's dream of transport-mode
26654235Smarkfen 			 * SAs with inner IP address selectors will not
26664235Smarkfen 			 * happen.
26674235Smarkfen 			 */
26684235Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL;
26694235Smarkfen 			if (proto != 0 && proto != IPPROTO_ENCAP &&
26704235Smarkfen 			    proto != IPPROTO_IPV6) {
26714235Smarkfen 				ERROR1(ep, ebuf, gettext(
26724235Smarkfen 				    "WARNING: Protocol type %d not "
26734235Smarkfen 				    "for use with Tunnel-Mode SA.\n"), proto);
26744235Smarkfen 				/* Continue and let PF_KEY scream... */
26754235Smarkfen 			}
26764235Smarkfen 		}
26774235Smarkfen 
26784235Smarkfen 		bcopy(assoc, nexthdr, SADB_64TO8(assoc->sadb_sa_len));
26794235Smarkfen 		nexthdr += assoc->sadb_sa_len;
26804235Smarkfen 		/* Save the SPI for the case of an error. */
26814235Smarkfen 		spi = assoc->sadb_sa_spi;
26824235Smarkfen 		free(assoc);
26834235Smarkfen 	} else {
26845989Spwernau 		if (spi == 0)
26855989Spwernau 			ERROR1(ep, ebuf, gettext(
26865989Spwernau 			    "Need to define SPI for %s.\n"), thiscmd);
26874235Smarkfen 		ERROR1(ep, ebuf, gettext(
26884235Smarkfen 		    "Need SA parameters for %s.\n"), thiscmd);
26894235Smarkfen 	}
26904235Smarkfen 
26916668Smarkfen 	if (sadb_pair != NULL) {
26926668Smarkfen 		if (sadb_pair->sadb_x_pair_spi == 0) {
26936668Smarkfen 			ERROR1(ep, ebuf, gettext(
26946668Smarkfen 			    "The SPI value is missing for the "
26956668Smarkfen 			    "association you wish to %s.\n"), thiscmd);
26966668Smarkfen 		}
26976668Smarkfen 		bcopy(sadb_pair, nexthdr,
26986668Smarkfen 		    SADB_64TO8(sadb_pair->sadb_x_pair_len));
26996668Smarkfen 		nexthdr += sadb_pair->sadb_x_pair_len;
27006668Smarkfen 		free(sadb_pair);
27016668Smarkfen 	}
27026668Smarkfen 
27034235Smarkfen 	if (hard != NULL) {
27044235Smarkfen 		bcopy(hard, nexthdr, SADB_64TO8(hard->sadb_lifetime_len));
27054235Smarkfen 		nexthdr += hard->sadb_lifetime_len;
27064235Smarkfen 		free(hard);
27074235Smarkfen 	}
27084235Smarkfen 
27094235Smarkfen 	if (soft != NULL) {
27104235Smarkfen 		bcopy(soft, nexthdr, SADB_64TO8(soft->sadb_lifetime_len));
27114235Smarkfen 		nexthdr += soft->sadb_lifetime_len;
27124235Smarkfen 		free(soft);
27134235Smarkfen 	}
27144235Smarkfen 
27157749SThejaswini.Singarajipura@Sun.COM 	if (idle != NULL) {
27167749SThejaswini.Singarajipura@Sun.COM 		bcopy(idle, nexthdr, SADB_64TO8(idle->sadb_lifetime_len));
27177749SThejaswini.Singarajipura@Sun.COM 		nexthdr += idle->sadb_lifetime_len;
27187749SThejaswini.Singarajipura@Sun.COM 		free(idle);
27197749SThejaswini.Singarajipura@Sun.COM 	}
27207749SThejaswini.Singarajipura@Sun.COM 
27214235Smarkfen 	if (encrypt == NULL && auth == NULL && cmd == CMD_ADD) {
27224235Smarkfen 		ERROR(ep, ebuf, gettext(
27234235Smarkfen 		    "Must have at least one key for an add.\n"));
27244235Smarkfen 	}
27254235Smarkfen 
27264235Smarkfen 	if (encrypt != NULL) {
27274235Smarkfen 		bcopy(encrypt, nexthdr, SADB_64TO8(encrypt->sadb_key_len));
27284235Smarkfen 		nexthdr += encrypt->sadb_key_len;
27294235Smarkfen 		bzero(encrypt, SADB_64TO8(encrypt->sadb_key_len));
27304235Smarkfen 		free(encrypt);
27314235Smarkfen 	}
27324235Smarkfen 
27334235Smarkfen 	if (auth != NULL) {
27344235Smarkfen 		bcopy(auth, nexthdr, SADB_64TO8(auth->sadb_key_len));
27354235Smarkfen 		nexthdr += auth->sadb_key_len;
27364235Smarkfen 		bzero(auth, SADB_64TO8(auth->sadb_key_len));
27374235Smarkfen 		free(auth);
27384235Smarkfen 	}
27394235Smarkfen 
27404235Smarkfen 	if (srcid != NULL) {
27414235Smarkfen 		bcopy(srcid, nexthdr, SADB_64TO8(srcid->sadb_ident_len));
27424235Smarkfen 		nexthdr += srcid->sadb_ident_len;
27434235Smarkfen 		free(srcid);
27444235Smarkfen 	}
27454235Smarkfen 
27464235Smarkfen 	if (dstid != NULL) {
27474235Smarkfen 		bcopy(dstid, nexthdr, SADB_64TO8(dstid->sadb_ident_len));
27484235Smarkfen 		nexthdr += dstid->sadb_ident_len;
27494235Smarkfen 		free(dstid);
27504235Smarkfen 	}
27514235Smarkfen 
27524235Smarkfen 	if (dst != NULL) {
27534235Smarkfen 		bcopy(dst, nexthdr, SADB_64TO8(dst->sadb_address_len));
27544235Smarkfen 		free(dst);
27554235Smarkfen 		dst = (struct sadb_address *)nexthdr;
27564235Smarkfen 		dst->sadb_address_proto = proto;
27574235Smarkfen 		((struct sockaddr_in6 *)(dst + 1))->sin6_port = htons(dstport);
27584235Smarkfen 		nexthdr += dst->sadb_address_len;
27594235Smarkfen 	} else {
27604235Smarkfen 		FATAL1(ep, ebuf, gettext(
27614235Smarkfen 		    "Need destination address for %s.\n"), thiscmd);
27624235Smarkfen 	}
27634235Smarkfen 
27644235Smarkfen 	if (use_natt) {
27654235Smarkfen 		if (natt_remote == NULL && natt_local == NULL) {
27664235Smarkfen 			ERROR(ep, ebuf, gettext(
27674235Smarkfen 			    "Must specify NAT-T remote or local address "
27684235Smarkfen 			    "for UDP encapsulation.\n"));
27694235Smarkfen 		}
27704235Smarkfen 
27714235Smarkfen 		if (natt_remote != NULL) {
27724235Smarkfen 			bcopy(natt_remote, nexthdr,
27734235Smarkfen 			    SADB_64TO8(natt_remote->sadb_address_len));
27744235Smarkfen 			free(natt_remote);
27754235Smarkfen 			natt_remote = (struct sadb_address *)nexthdr;
27764235Smarkfen 			nexthdr += natt_remote->sadb_address_len;
27774235Smarkfen 			((struct sockaddr_in6 *)(natt_remote + 1))->sin6_port =
27784235Smarkfen 			    htons(natt_rport);
27794235Smarkfen 		}
27804235Smarkfen 
27814235Smarkfen 		if (natt_local != NULL) {
27824235Smarkfen 			bcopy(natt_local, nexthdr,
27834235Smarkfen 			    SADB_64TO8(natt_local->sadb_address_len));
27844235Smarkfen 			free(natt_local);
27854235Smarkfen 			natt_local = (struct sadb_address *)nexthdr;
27864235Smarkfen 			nexthdr += natt_local->sadb_address_len;
27874235Smarkfen 			((struct sockaddr_in6 *)(natt_local + 1))->sin6_port =
27884235Smarkfen 			    htons(natt_lport);
27894235Smarkfen 		}
27904235Smarkfen 	}
27914235Smarkfen 
27924235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
27934235Smarkfen 
27944235Smarkfen 	/*
27954235Smarkfen 	 * PF_KEY requires a source address extension, even if the source
27964235Smarkfen 	 * address itself is unspecified. (See "Set explicit unspecified..."
27974235Smarkfen 	 * code fragment above. Destination reality check was above.)
27984235Smarkfen 	 */
27994235Smarkfen 	bcopy(src, nexthdr, SADB_64TO8(src->sadb_address_len));
28004235Smarkfen 	free(src);
28014235Smarkfen 	src = (struct sadb_address *)nexthdr;
28024235Smarkfen 	src->sadb_address_proto = proto;
28034235Smarkfen 	((struct sockaddr_in6 *)(src + 1))->sin6_port = htons(srcport);
28044235Smarkfen 	nexthdr += src->sadb_address_len;
28054235Smarkfen 
28064235Smarkfen 	if (isrc != NULL) {
28074235Smarkfen 		bcopy(isrc, nexthdr, SADB_64TO8(isrc->sadb_address_len));
28084235Smarkfen 		free(isrc);
28094235Smarkfen 		isrc = (struct sadb_address *)nexthdr;
28104235Smarkfen 		isrc->sadb_address_proto = iproto;
28114235Smarkfen 		((struct sockaddr_in6 *)(isrc + 1))->sin6_port =
28124235Smarkfen 		    htons(isrcport);
28134235Smarkfen 		nexthdr += isrc->sadb_address_len;
28144235Smarkfen 	}
28154235Smarkfen 
28164235Smarkfen 	if (idst != NULL) {
28174235Smarkfen 		bcopy(idst, nexthdr, SADB_64TO8(idst->sadb_address_len));
28184235Smarkfen 		free(idst);
28194235Smarkfen 		idst = (struct sadb_address *)nexthdr;
28204235Smarkfen 		idst->sadb_address_proto = iproto;
28214235Smarkfen 		((struct sockaddr_in6 *)(idst + 1))->sin6_port =
28224235Smarkfen 		    htons(idstport);
28234235Smarkfen 		nexthdr += idst->sadb_address_len;
28244235Smarkfen 	}
28254235Smarkfen 
28267749SThejaswini.Singarajipura@Sun.COM 	if (replay_ctr != NULL) {
28277749SThejaswini.Singarajipura@Sun.COM 		bcopy(replay_ctr, nexthdr,
28287749SThejaswini.Singarajipura@Sun.COM 		    SADB_64TO8(replay_ctr->sadb_x_rc_len));
28297749SThejaswini.Singarajipura@Sun.COM 		nexthdr += replay_ctr->sadb_x_rc_len;
28307749SThejaswini.Singarajipura@Sun.COM 		free(replay_ctr);
28317749SThejaswini.Singarajipura@Sun.COM 	}
28327749SThejaswini.Singarajipura@Sun.COM 
28334342Spwernau 	if (cflag) {
28344342Spwernau 		/*
28354342Spwernau 		 * Assume the checked cmd would have worked if it was actually
28364342Spwernau 		 * used. doaddresses() will increment lines_added if it
28374342Spwernau 		 * succeeds.
28384342Spwernau 		 */
28394342Spwernau 		lines_added++;
28404342Spwernau 	} else {
28416668Smarkfen 		doaddresses(sadb_msg_type, satype,
28424235Smarkfen 		    cmd, srchp, dsthp, src, dst, unspec_src, buffer, totallen,
28434235Smarkfen 		    spi, ebuf);
28444235Smarkfen 	}
28454235Smarkfen 
28464235Smarkfen 	if (isrchp != NULL && isrchp != &dummy.he)
28474342Spwernau 		freehostent(isrchp);
28484235Smarkfen 	if (idsthp != NULL && idsthp != &dummy.he)
28494342Spwernau 		freehostent(idsthp);
28504235Smarkfen 	if (srchp != NULL && srchp != &dummy.he)
28514342Spwernau 		freehostent(srchp);
28524235Smarkfen 	if (dsthp != NULL && dsthp != &dummy.he)
28534342Spwernau 		freehostent(dsthp);
28544235Smarkfen 	if (natt_lhp != NULL && natt_lhp != &dummy.he)
28554342Spwernau 		freehostent(natt_lhp);
28564235Smarkfen 	if (natt_rhp != NULL && natt_rhp != &dummy.he)
28574342Spwernau 		freehostent(natt_rhp);
28584235Smarkfen 
28594235Smarkfen 	free(ebuf);
28604235Smarkfen 	free(buffer);
28614235Smarkfen }
28624235Smarkfen 
28634235Smarkfen /*
28644235Smarkfen  * DELETE and GET are similar, in that they only need the extensions
28654235Smarkfen  * required to _find_ an SA, and then either delete it or obtain its
28664235Smarkfen  * information.
28674235Smarkfen  */
28684235Smarkfen static void
28694235Smarkfen dodelget(int cmd, int satype, char *argv[], char *ebuf)
28704235Smarkfen {
28714235Smarkfen 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
28724235Smarkfen 	uint64_t *nextext;
28734235Smarkfen 	struct sadb_sa *assoc = NULL;
28744235Smarkfen 	struct sadb_address *src = NULL, *dst = NULL;
28754235Smarkfen 	int next, token, sa_len;
28764235Smarkfen 	char *thiscmd;
28774235Smarkfen 	uint32_t spi;
28786668Smarkfen 	uint8_t	sadb_msg_type;
28794235Smarkfen 	struct hostent *srchp = NULL, *dsthp = NULL;
28804235Smarkfen 	struct sockaddr_in6 *sin6;
28814235Smarkfen 	boolean_t unspec_src = B_TRUE;
28824235Smarkfen 	uint16_t srcport = 0, dstport = 0;
28834235Smarkfen 	uint8_t proto = 0;
28844235Smarkfen 	char *ep = NULL;
28854235Smarkfen 
28864235Smarkfen 	/* Set the first extension header to right past the base message. */
28874235Smarkfen 	nextext = (uint64_t *)(msg + 1);
28884235Smarkfen 	bzero(nextext, sizeof (get_buffer) - sizeof (*msg));
28894235Smarkfen 
28906668Smarkfen 	switch (cmd) {
28916668Smarkfen 	case CMD_GET:
28926668Smarkfen 		thiscmd = "get";
28936668Smarkfen 		sadb_msg_type = SADB_GET;
28946668Smarkfen 		break;
28956668Smarkfen 	case CMD_DELETE:
28966668Smarkfen 		thiscmd = "delete";
28976668Smarkfen 		sadb_msg_type = SADB_DELETE;
28986668Smarkfen 		break;
28996668Smarkfen 	case CMD_DELETE_PAIR:
29006668Smarkfen 		thiscmd = "delete-pair";
29016668Smarkfen 		sadb_msg_type = SADB_X_DELPAIR;
29026668Smarkfen 		break;
29036668Smarkfen 	}
29046668Smarkfen 
29056668Smarkfen 	msg_init(msg, sadb_msg_type, (uint8_t)satype);
29064235Smarkfen 
29074235Smarkfen #define	ALLOC_ADDR_EXT(ext, exttype)			\
29084235Smarkfen 	(ext) = (struct sadb_address *)nextext;		\
29094235Smarkfen 	nextext = (uint64_t *)((ext) + 1);		\
29104235Smarkfen 	nextext += SADB_8TO64(roundup(sa_len, 8));	\
29114235Smarkfen 	(ext)->sadb_address_exttype = exttype;		\
29124235Smarkfen 	(ext)->sadb_address_len = nextext - ((uint64_t *)ext);
29134235Smarkfen 
29144235Smarkfen 	/* Assume last element in argv is set to NULL. */
29154235Smarkfen 	do {
29164235Smarkfen 		token = parseextval(*argv, &next);
29174235Smarkfen 		argv++;
29184235Smarkfen 		switch (token) {
29194235Smarkfen 		case TOK_EOF:
29204235Smarkfen 			/* Do nothing, I'm done. */
29214235Smarkfen 			break;
29224235Smarkfen 		case TOK_UNKNOWN:
29234235Smarkfen 			ERROR1(ep, ebuf, gettext(
29244235Smarkfen 			    "Unknown extension field \"%s\"\n"), *(argv - 1));
29254235Smarkfen 			break;
29264235Smarkfen 		case TOK_SPI:
29274235Smarkfen 			if (assoc != NULL) {
29284235Smarkfen 				ERROR(ep, ebuf, gettext(
29294235Smarkfen 				    "Can only specify single SPI value.\n"));
29304235Smarkfen 				break;
29314235Smarkfen 			}
29324235Smarkfen 			assoc = (struct sadb_sa *)nextext;
29334235Smarkfen 			nextext = (uint64_t *)(assoc + 1);
29344235Smarkfen 			assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc));
29354235Smarkfen 			assoc->sadb_sa_exttype = SADB_EXT_SA;
29364235Smarkfen 			assoc->sadb_sa_spi = htonl((uint32_t)parsenum(*argv,
29374235Smarkfen 			    B_TRUE, ebuf));
29384235Smarkfen 			spi = assoc->sadb_sa_spi;
29394235Smarkfen 			argv++;
29404235Smarkfen 			break;
29414235Smarkfen 		case TOK_SRCPORT:
29424235Smarkfen 			if (srcport != 0) {
29434235Smarkfen 				ERROR(ep, ebuf, gettext(
29444235Smarkfen 				    "Can only specify single source port.\n"));
29454235Smarkfen 				break;
29464235Smarkfen 			}
29474235Smarkfen 			srcport = parsenum(*argv, B_TRUE, ebuf);
29484235Smarkfen 			argv++;
29494235Smarkfen 			break;
29504235Smarkfen 		case TOK_DSTPORT:
29514235Smarkfen 			if (dstport != 0) {
29524235Smarkfen 				ERROR(ep, ebuf, gettext(
29534235Smarkfen 				    "Can only "
29544235Smarkfen 				    "specify single destination port.\n"));
29554235Smarkfen 				break;
29564235Smarkfen 			}
29574235Smarkfen 			dstport = parsenum(*argv, B_TRUE, ebuf);
29584235Smarkfen 			argv++;
29594235Smarkfen 			break;
29604235Smarkfen 		case TOK_PROTO:
29614235Smarkfen 			if (proto != 0) {
29624235Smarkfen 				ERROR(ep, ebuf, gettext(
29634235Smarkfen 				    "Can only specify single protocol.\n"));
29644235Smarkfen 				break;
29654235Smarkfen 			}
29664235Smarkfen 			proto = parsenum(*argv, B_TRUE, ebuf);
29674235Smarkfen 			argv++;
29684235Smarkfen 			break;
29694235Smarkfen 		case TOK_SRCADDR:
29704235Smarkfen 		case TOK_SRCADDR6:
29714235Smarkfen 			if (src != NULL) {
29724235Smarkfen 				ERROR(ep, ebuf, gettext(
29734235Smarkfen 				    "Can only specify single source addr.\n"));
29744235Smarkfen 				break;
29754235Smarkfen 			}
29764235Smarkfen 			sa_len = parseaddr(*argv, &srchp,
29774235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
29784235Smarkfen 			if (srchp == NULL) {
29794235Smarkfen 				ERROR1(ep, ebuf, gettext(
29804235Smarkfen 				    "Unknown source address \"%s\"\n"), *argv);
29814235Smarkfen 				break;
29824235Smarkfen 			}
29834235Smarkfen 			argv++;
29844235Smarkfen 
29854235Smarkfen 			unspec_src = B_FALSE;
29864235Smarkfen 
29874235Smarkfen 			ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
29884235Smarkfen 
29894235Smarkfen 			if (srchp == &dummy.he) {
29904235Smarkfen 				/*
29914235Smarkfen 				 * Single address with -n flag.
29924235Smarkfen 				 */
29934235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
29944235Smarkfen 				bzero(sin6, sizeof (*sin6));
29954235Smarkfen 				sin6->sin6_family = AF_INET6;
29964235Smarkfen 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
29974235Smarkfen 				    sizeof (struct in6_addr));
29984235Smarkfen 			}
29994235Smarkfen 			/* The rest is pre-bzeroed for us. */
30004235Smarkfen 			break;
30014235Smarkfen 		case TOK_DSTADDR:
30024235Smarkfen 		case TOK_DSTADDR6:
30034235Smarkfen 			if (dst != NULL) {
30044235Smarkfen 				ERROR(ep, ebuf, gettext(
30054235Smarkfen 				    "Can only specify single destination "
30064235Smarkfen 				    "address.\n"));
30074235Smarkfen 				break;
30084235Smarkfen 			}
30094235Smarkfen 			sa_len = parseaddr(*argv, &dsthp,
30104235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
30114235Smarkfen 			if (dsthp == NULL) {
30124235Smarkfen 				ERROR1(ep, ebuf, gettext(
30134235Smarkfen 				    "Unknown destination address \"%s\"\n"),
30144235Smarkfen 				    *argv);
30154235Smarkfen 				break;
30164235Smarkfen 			}
30174235Smarkfen 			argv++;
30184235Smarkfen 
30194235Smarkfen 			ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
30204235Smarkfen 
30214235Smarkfen 			if (dsthp == &dummy.he) {
30224235Smarkfen 				/*
30234235Smarkfen 				 * Single address with -n flag.
30244235Smarkfen 				 */
30254235Smarkfen 				sin6 = (struct sockaddr_in6 *)(dst + 1);
30264235Smarkfen 				bzero(sin6, sizeof (*sin6));
30274235Smarkfen 				sin6->sin6_family = AF_INET6;
30284235Smarkfen 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
30294235Smarkfen 				    sizeof (struct in6_addr));
30304235Smarkfen 			}
30314235Smarkfen 			/* The rest is pre-bzeroed for us. */
30324235Smarkfen 			break;
30336668Smarkfen 		case TOK_FLAG_INBOUND:
30346668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_INBOUND;
30356668Smarkfen 			break;
30366668Smarkfen 		case TOK_FLAG_OUTBOUND:
30376668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_OUTBOUND;
30386668Smarkfen 			break;
30394235Smarkfen 		default:
30404235Smarkfen 			ERROR2(ep, ebuf, gettext(
30414235Smarkfen 			    "Don't use extension %s for '%s' command.\n"),
30424235Smarkfen 			    *(argv - 1), thiscmd);
30434235Smarkfen 			break;
30444235Smarkfen 		}
30454235Smarkfen 	} while (token != TOK_EOF);
30464235Smarkfen 
30474235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
30484235Smarkfen 
30494235Smarkfen 	if ((srcport != 0) && (src == NULL)) {
30504235Smarkfen 		ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
30514235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
30524235Smarkfen 		src->sadb_address_proto = proto;
30534235Smarkfen 		bzero(sin6, sizeof (*sin6));
30544235Smarkfen 		sin6->sin6_family = AF_INET6;
30554235Smarkfen 		sin6->sin6_port = htons(srcport);
30564235Smarkfen 	}
30574235Smarkfen 
30584235Smarkfen 	if ((dstport != 0) && (dst == NULL)) {
30594235Smarkfen 		ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
30604235Smarkfen 		sin6 = (struct sockaddr_in6 *)(dst + 1);
30614235Smarkfen 		src->sadb_address_proto = proto;
30624235Smarkfen 		bzero(sin6, sizeof (*sin6));
30634235Smarkfen 		sin6->sin6_family = AF_INET6;
30644235Smarkfen 		sin6->sin6_port = htons(dstport);
30654235Smarkfen 	}
30664235Smarkfen 
30674235Smarkfen 	/* So I have enough of the message to send it down! */
30684235Smarkfen 	msg->sadb_msg_len = nextext - get_buffer;
30694235Smarkfen 
30704235Smarkfen 	if (assoc == NULL) {
30714235Smarkfen 		FATAL1(ep, ebuf, gettext(
30724235Smarkfen 		    "Need SA parameters for %s.\n"), thiscmd);
30734235Smarkfen 	}
30744235Smarkfen 
30754342Spwernau 	if (cflag) {
30764342Spwernau 		/*
30774342Spwernau 		 * Assume the checked cmd would have worked if it was actually
30784342Spwernau 		 * used. doaddresses() will increment lines_added if it
30794342Spwernau 		 * succeeds.
30804342Spwernau 		 */
30814342Spwernau 		lines_added++;
30824342Spwernau 	} else {
30836668Smarkfen 		doaddresses(sadb_msg_type, satype,
30844235Smarkfen 		    cmd, srchp, dsthp, src, dst, unspec_src, get_buffer,
30854235Smarkfen 		    sizeof (get_buffer), spi, NULL);
30864235Smarkfen 	}
30874235Smarkfen 
30884235Smarkfen 	if (srchp != NULL && srchp != &dummy.he)
30894235Smarkfen 		freehostent(srchp);
30904235Smarkfen 	if (dsthp != NULL && dsthp != &dummy.he)
30914235Smarkfen 		freehostent(dsthp);
30924235Smarkfen }
30934235Smarkfen 
30944235Smarkfen /*
30959086SVladimir.Kotal@Sun.COM  * "ipseckey monitor" should exit very gracefully if ^C is tapped provided
30969086SVladimir.Kotal@Sun.COM  * it is not running in interactive mode.
30974235Smarkfen  */
30984235Smarkfen static void
30994235Smarkfen monitor_catch(int signal)
31004235Smarkfen {
31019086SVladimir.Kotal@Sun.COM 	if (!interactive)
31029086SVladimir.Kotal@Sun.COM 		errx(signal, gettext("Bailing on signal %d."), signal);
31034235Smarkfen }
31044235Smarkfen 
31054235Smarkfen /*
31064235Smarkfen  * Loop forever, listening on PF_KEY messages.
31074235Smarkfen  */
31084235Smarkfen static void
31094235Smarkfen domonitor(boolean_t passive)
31104235Smarkfen {
31114235Smarkfen 	struct sadb_msg *samsg;
31129086SVladimir.Kotal@Sun.COM 	struct sigaction newsig, oldsig;
31134235Smarkfen 	int rc;
31144235Smarkfen 
31154235Smarkfen 	/* Catch ^C. */
31169086SVladimir.Kotal@Sun.COM 	newsig.sa_handler = monitor_catch;
31179086SVladimir.Kotal@Sun.COM 	newsig.sa_flags = 0;
31189086SVladimir.Kotal@Sun.COM 	(void) sigemptyset(&newsig.sa_mask);
31199086SVladimir.Kotal@Sun.COM 	(void) sigaddset(&newsig.sa_mask, SIGINT);
31209086SVladimir.Kotal@Sun.COM 	(void) sigaction(SIGINT, &newsig, &oldsig);
31214235Smarkfen 
31224235Smarkfen 	samsg = (struct sadb_msg *)get_buffer;
31234235Smarkfen 	if (!passive) {
31244235Smarkfen 		(void) printf(gettext("Actively"));
31254235Smarkfen 		msg_init(samsg, SADB_X_PROMISC, 1);	/* Turn ON promisc. */
31264235Smarkfen 		rc = key_write(keysock, samsg, sizeof (*samsg));
31274235Smarkfen 		if (rc == -1)
31284235Smarkfen 			Bail("write (SADB_X_PROMISC)");
31294235Smarkfen 	} else {
31304235Smarkfen 		(void) printf(gettext("Passively"));
31314235Smarkfen 	}
31324235Smarkfen 	(void) printf(gettext(" monitoring the PF_KEY socket.\n"));
31334235Smarkfen 
31344235Smarkfen 	for (; ; ) {
31354235Smarkfen 		/*
31364235Smarkfen 		 * I assume that read() is non-blocking, and will never
31374235Smarkfen 		 * return 0.
31384235Smarkfen 		 */
31394235Smarkfen 		rc = read(keysock, samsg, sizeof (get_buffer));
31409086SVladimir.Kotal@Sun.COM 		if (rc == -1) {
31419086SVladimir.Kotal@Sun.COM 			if (errno == EINTR && interactive)
31429086SVladimir.Kotal@Sun.COM 				goto out;
31439086SVladimir.Kotal@Sun.COM 			else
31449086SVladimir.Kotal@Sun.COM 				Bail("read (in domonitor)");
31459086SVladimir.Kotal@Sun.COM 		}
31464235Smarkfen 		(void) printf(gettext("Read %d bytes.\n"), rc);
31474235Smarkfen 		/*
31484235Smarkfen 		 * Q:  Should I use the same method of printing as GET does?
31494235Smarkfen 		 * A:  For now, yes.
31504235Smarkfen 		 */
31514867Spwernau 		print_samsg(stdout, get_buffer, B_TRUE, vflag, nflag);
31524235Smarkfen 		(void) putchar('\n');
31534235Smarkfen 	}
31549086SVladimir.Kotal@Sun.COM 
31559086SVladimir.Kotal@Sun.COM out:
31569086SVladimir.Kotal@Sun.COM 	if (interactive)
31579086SVladimir.Kotal@Sun.COM 		/* restore SIGINT behavior */
31589086SVladimir.Kotal@Sun.COM 		(void) sigaction(SIGINT, &oldsig, NULL);
31594235Smarkfen }
31604235Smarkfen 
31614235Smarkfen /*
31624235Smarkfen  * Either mask or unmask all relevant signals.
31634235Smarkfen  */
31644235Smarkfen static void
31654235Smarkfen mask_signals(boolean_t unmask)
31664235Smarkfen {
31674235Smarkfen 	sigset_t set;
31684235Smarkfen 	static sigset_t oset;
31694235Smarkfen 
31704235Smarkfen 	if (unmask) {
31714235Smarkfen 		(void) sigprocmask(SIG_SETMASK, &oset, NULL);
31724235Smarkfen 	} else {
31734235Smarkfen 		(void) sigfillset(&set);
31744235Smarkfen 		(void) sigprocmask(SIG_SETMASK, &set, &oset);
31754235Smarkfen 	}
31764235Smarkfen }
31774235Smarkfen 
31784235Smarkfen /*
31794235Smarkfen  * Assorted functions to print help text.
31804235Smarkfen  */
31814235Smarkfen #define	puts_tr(s) (void) puts(gettext(s))
31824235Smarkfen 
31834235Smarkfen static void
31844235Smarkfen doattrhelp()
31854235Smarkfen {
31864235Smarkfen 	int i;
31874235Smarkfen 
31884235Smarkfen 	puts_tr("\nSA attributes:");
31894235Smarkfen 
31904235Smarkfen 	for (i = 0; tokens[i].string != NULL; i++) {
31914235Smarkfen 		if (i%3 == 0)
31924235Smarkfen 			(void) printf("\n");
31934235Smarkfen 		(void) printf("    %-15.15s", tokens[i].string);
31944235Smarkfen 	}
31954235Smarkfen 	(void) printf("\n");
31964235Smarkfen }
31974235Smarkfen 
31984235Smarkfen static void
31994235Smarkfen dohelpcmd(char *cmds)
32004235Smarkfen {
32014235Smarkfen 	int cmd;
32024235Smarkfen 
32034235Smarkfen 	if (strcmp(cmds, "attr") == 0) {
32044235Smarkfen 		doattrhelp();
32054235Smarkfen 		return;
32064235Smarkfen 	}
32074235Smarkfen 
32084235Smarkfen 	cmd = parsecmd(cmds);
32094235Smarkfen 	switch (cmd) {
32104235Smarkfen 	case CMD_UPDATE:
32114235Smarkfen 		puts_tr("update	 - Update an existing SA");
32124235Smarkfen 		break;
32136668Smarkfen 	case CMD_UPDATE_PAIR:
32146668Smarkfen 		puts_tr("update-pair - Update an existing pair of SA's");
32156668Smarkfen 		break;
32164235Smarkfen 	case CMD_ADD:
32174235Smarkfen 		puts_tr("add	 - Add a new security association (SA)");
32184235Smarkfen 		break;
32194235Smarkfen 	case CMD_DELETE:
32204235Smarkfen 		puts_tr("delete - Delete an SA");
32214235Smarkfen 		break;
32226668Smarkfen 	case CMD_DELETE_PAIR:
32236668Smarkfen 		puts_tr("delete-pair - Delete a pair of SA's");
32246668Smarkfen 		break;
32254235Smarkfen 	case CMD_GET:
32264235Smarkfen 		puts_tr("get - Display an SA");
32274235Smarkfen 		break;
32284235Smarkfen 	case CMD_FLUSH:
32294235Smarkfen 		puts_tr("flush - Delete all SAs");
32309086SVladimir.Kotal@Sun.COM 		puts_tr("");
32319086SVladimir.Kotal@Sun.COM 		puts_tr("Optional arguments:");
32329086SVladimir.Kotal@Sun.COM 		puts_tr("all        delete all SAs");
32339086SVladimir.Kotal@Sun.COM 		puts_tr("esp        delete just ESP SAs");
32349086SVladimir.Kotal@Sun.COM 		puts_tr("ah         delete just AH SAs");
32359086SVladimir.Kotal@Sun.COM 		puts_tr("<number>   delete just SAs with type given by number");
32369086SVladimir.Kotal@Sun.COM 		puts_tr("");
32374235Smarkfen 		break;
32384235Smarkfen 	case CMD_DUMP:
32394235Smarkfen 		puts_tr("dump - Display all SAs");
32409086SVladimir.Kotal@Sun.COM 		puts_tr("");
32419086SVladimir.Kotal@Sun.COM 		puts_tr("Optional arguments:");
32429086SVladimir.Kotal@Sun.COM 		puts_tr("all        display all SAs");
32439086SVladimir.Kotal@Sun.COM 		puts_tr("esp        display just ESP SAs");
32449086SVladimir.Kotal@Sun.COM 		puts_tr("ah         display just AH SAs");
32459086SVladimir.Kotal@Sun.COM 		puts_tr("<number>   display just SAs with type "
32469086SVladimir.Kotal@Sun.COM 		    "given by number");
32479086SVladimir.Kotal@Sun.COM 		puts_tr("");
32484235Smarkfen 		break;
32494235Smarkfen 	case CMD_MONITOR:
32504235Smarkfen 		puts_tr("monitor - Monitor all PF_KEY reply messages.");
32514235Smarkfen 		break;
32524235Smarkfen 	case CMD_PMONITOR:
32534235Smarkfen 		puts_tr(
32544235Smarkfen "pmonitor, passive_monitor - Monitor PF_KEY messages that");
32554235Smarkfen 		puts_tr(
32564235Smarkfen "                            reply to all PF_KEY sockets.");
32574235Smarkfen 		break;
32584235Smarkfen 
32594235Smarkfen 	case CMD_QUIT:
32604235Smarkfen 		puts_tr("quit, exit - Exit the program");
32614235Smarkfen 		break;
32624235Smarkfen 	case CMD_SAVE:
32634235Smarkfen 		puts_tr("save	    - Saves all SAs to a file");
32644235Smarkfen 		break;
32654235Smarkfen 	case CMD_HELP:
32664235Smarkfen 		puts_tr("help	    - Display list of commands");
32674235Smarkfen 		puts_tr("help <cmd> - Display help for command");
32684235Smarkfen 		puts_tr("help attr  - Display possible SA attributes");
32694235Smarkfen 		break;
32704235Smarkfen 	default:
32714235Smarkfen 		(void) printf(gettext("%s: Unknown command\n"), cmds);
32724235Smarkfen 		break;
32734235Smarkfen 	}
32744235Smarkfen }
32754235Smarkfen 
32764235Smarkfen 
32774235Smarkfen static void
32784235Smarkfen dohelp(char *cmds)
32794235Smarkfen {
32804235Smarkfen 	if (cmds != NULL) {
32814235Smarkfen 		dohelpcmd(cmds);
32824235Smarkfen 		return;
32834235Smarkfen 	}
32844235Smarkfen 	puts_tr("Commands");
32854235Smarkfen 	puts_tr("--------");
32864235Smarkfen 	puts_tr("?, help  - Display this list");
32874235Smarkfen 	puts_tr("help <cmd> - Display help for command");
32884235Smarkfen 	puts_tr("help attr  - Display possible SA attributes");
32894235Smarkfen 	puts_tr("quit, exit - Exit the program");
32904235Smarkfen 	puts_tr("monitor - Monitor all PF_KEY reply messages.");
32914235Smarkfen 	puts_tr("pmonitor, passive_monitor - Monitor PF_KEY messages that");
32924235Smarkfen 	puts_tr("                            reply to all PF_KEY sockets.");
32934235Smarkfen 	puts_tr("");
32944235Smarkfen 	puts_tr("The following commands are of the form:");
32954235Smarkfen 	puts_tr("    <command> {SA type} {attribute value}*");
32964235Smarkfen 	puts_tr("");
32974235Smarkfen 	puts_tr("add (interactive only) - Add a new security association (SA)");
32984235Smarkfen 	puts_tr("update (interactive only) - Update an existing SA");
32996668Smarkfen 	puts_tr("update-pair (interactive only) - Update an existing SA pair");
33004235Smarkfen 	puts_tr("delete - Delete an SA");
33016668Smarkfen 	puts_tr("delete-pair - Delete an SA pair");
33024235Smarkfen 	puts_tr("get - Display an SA");
33034235Smarkfen 	puts_tr("flush - Delete all SAs");
33044235Smarkfen 	puts_tr("dump - Display all SAs");
33054235Smarkfen 	puts_tr("save - Saves all SAs to a file");
33064235Smarkfen }
33074235Smarkfen 
33084235Smarkfen /*
33094235Smarkfen  * "Parse" a command line from argv.
33104235Smarkfen  */
33114235Smarkfen static void
33124342Spwernau parseit(int argc, char *argv[], char *ebuf, boolean_t read_cmdfile)
33134235Smarkfen {
33144235Smarkfen 	int cmd, satype;
33154235Smarkfen 	char *ep = NULL;
33164235Smarkfen 
33174235Smarkfen 	if (argc == 0)
33184235Smarkfen 		return;
33194235Smarkfen 	cmd = parsecmd(*argv++);
33204235Smarkfen 
33214342Spwernau 	/*
33224342Spwernau 	 * Some commands loop forever and should only be run from the command
33234342Spwernau 	 * line, they should never be run from a command file as this may
33244342Spwernau 	 * be used at boot time.
33254342Spwernau 	 */
33264235Smarkfen 	switch (cmd) {
33274235Smarkfen 	case CMD_HELP:
33284342Spwernau 		if (read_cmdfile)
33294342Spwernau 			ERROR(ep, ebuf, gettext("Help not appropriate in "
33304342Spwernau 			    "config file."));
33314342Spwernau 		else
33324342Spwernau 			dohelp(*argv);
33334235Smarkfen 		return;
33344235Smarkfen 	case CMD_MONITOR:
33354342Spwernau 		if (read_cmdfile)
33364342Spwernau 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
33374342Spwernau 			    "config file."));
33389086SVladimir.Kotal@Sun.COM 		else {
33394342Spwernau 			domonitor(B_FALSE);
33409086SVladimir.Kotal@Sun.COM 			/*
33419086SVladimir.Kotal@Sun.COM 			 * Return from the function in interactive mode to
33429086SVladimir.Kotal@Sun.COM 			 * avoid error message in the next switch statement.
33439086SVladimir.Kotal@Sun.COM 			 * Also print newline to prevent prompt clobbering.
33449086SVladimir.Kotal@Sun.COM 			 * The same is done for CMD_PMONITOR.
33459086SVladimir.Kotal@Sun.COM 			 */
33469086SVladimir.Kotal@Sun.COM 			if (interactive) {
33479086SVladimir.Kotal@Sun.COM 				(void) printf("\n");
33489086SVladimir.Kotal@Sun.COM 				return;
33499086SVladimir.Kotal@Sun.COM 			}
33509086SVladimir.Kotal@Sun.COM 		}
33514235Smarkfen 		break;
33524235Smarkfen 	case CMD_PMONITOR:
33534342Spwernau 		if (read_cmdfile)
33544342Spwernau 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
33554342Spwernau 			    "config file."));
33569086SVladimir.Kotal@Sun.COM 		else {
33574342Spwernau 			domonitor(B_TRUE);
33589086SVladimir.Kotal@Sun.COM 			if (interactive) {
33599086SVladimir.Kotal@Sun.COM 				(void) printf("\n");
33609086SVladimir.Kotal@Sun.COM 				return;
33619086SVladimir.Kotal@Sun.COM 			}
33629086SVladimir.Kotal@Sun.COM 		}
33634235Smarkfen 		break;
33644235Smarkfen 	case CMD_QUIT:
33654235Smarkfen 		EXIT_OK(NULL);
33664235Smarkfen 	}
33674235Smarkfen 
33684342Spwernau 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
33694342Spwernau 
33704235Smarkfen 	satype = parsesatype(*argv, ebuf);
33714235Smarkfen 
33724235Smarkfen 	if (satype != SADB_SATYPE_UNSPEC) {
33734235Smarkfen 		argv++;
33744235Smarkfen 	} else {
33754235Smarkfen 		/*
33764235Smarkfen 		 * You must specify either "all" or a specific SA type
33774235Smarkfen 		 * for the "save" command.
33784235Smarkfen 		 */
33794235Smarkfen 		if (cmd == CMD_SAVE)
33804235Smarkfen 			if (*argv == NULL) {
33814235Smarkfen 				FATAL(ep, ebuf, gettext(
33824235Smarkfen 				    "Must specify a specific "
33834235Smarkfen 				    "SA type for save.\n"));
33844235Smarkfen 			} else {
33854235Smarkfen 				argv++;
33864235Smarkfen 			}
33874235Smarkfen 	}
33884235Smarkfen 
33894235Smarkfen 	switch (cmd) {
33904235Smarkfen 	case CMD_FLUSH:
33919086SVladimir.Kotal@Sun.COM 		if (argc > 2) {
33929086SVladimir.Kotal@Sun.COM 			ERROR(ep, ebuf, gettext("Too many arguments for "
33939086SVladimir.Kotal@Sun.COM 			    "flush command"));
33949086SVladimir.Kotal@Sun.COM 			handle_errors(ep, ebuf,
33959086SVladimir.Kotal@Sun.COM 			    interactive ? B_TRUE : B_FALSE, B_FALSE);
33969086SVladimir.Kotal@Sun.COM 		}
33974342Spwernau 		if (!cflag)
33984342Spwernau 			doflush(satype);
33994342Spwernau 		/*
34004342Spwernau 		 * If this was called because of an entry in a cmd file
34014342Spwernau 		 * then this action needs to be counted to prevent
34024342Spwernau 		 * do_interactive() treating this as an error.
34034342Spwernau 		 */
34044342Spwernau 		lines_added++;
34054235Smarkfen 		break;
34064235Smarkfen 	case CMD_ADD:
34074235Smarkfen 	case CMD_UPDATE:
34086668Smarkfen 	case CMD_UPDATE_PAIR:
34094235Smarkfen 		/*
34104235Smarkfen 		 * NOTE: Shouldn't allow ADDs or UPDATEs with keying material
34114235Smarkfen 		 * from the command line.
34124235Smarkfen 		 */
34134235Smarkfen 		if (!interactive) {
34144235Smarkfen 			errx(1, gettext(
34154235Smarkfen 			    "can't do ADD or UPDATE from the command line.\n"));
34164235Smarkfen 		}
34174235Smarkfen 		if (satype == SADB_SATYPE_UNSPEC) {
34184235Smarkfen 			FATAL(ep, ebuf, gettext(
34194235Smarkfen 			    "Must specify a specific SA type."));
34204235Smarkfen 			/* NOTREACHED */
34214235Smarkfen 		}
34224235Smarkfen 		/* Parse for extensions, including keying material. */
34234235Smarkfen 		doaddup(cmd, satype, argv, ebuf);
34244235Smarkfen 		break;
34254235Smarkfen 	case CMD_DELETE:
34266668Smarkfen 	case CMD_DELETE_PAIR:
34274235Smarkfen 	case CMD_GET:
34284235Smarkfen 		if (satype == SADB_SATYPE_UNSPEC) {
34294235Smarkfen 			FATAL(ep, ebuf, gettext(
34304235Smarkfen 			    "Must specify a single SA type."));
34314235Smarkfen 			/* NOTREACHED */
34324235Smarkfen 		}
34334235Smarkfen 		/* Parse for bare minimum to locate an SA. */
34344235Smarkfen 		dodelget(cmd, satype, argv, ebuf);
34354235Smarkfen 		break;
34364235Smarkfen 	case CMD_DUMP:
34374342Spwernau 		if (read_cmdfile)
34384342Spwernau 			ERROR(ep, ebuf, gettext("Dump not appropriate in "
34394342Spwernau 			    "config file."));
34409086SVladimir.Kotal@Sun.COM 		else {
34419086SVladimir.Kotal@Sun.COM 			if (argc > 2) {
34429086SVladimir.Kotal@Sun.COM 				ERROR(ep, ebuf, gettext("Too many arguments "
34439086SVladimir.Kotal@Sun.COM 				    "for dump command"));
34449086SVladimir.Kotal@Sun.COM 				handle_errors(ep, ebuf,
34459086SVladimir.Kotal@Sun.COM 				    interactive ? B_TRUE : B_FALSE, B_FALSE);
34469086SVladimir.Kotal@Sun.COM 			}
34474342Spwernau 			dodump(satype, NULL);
34489086SVladimir.Kotal@Sun.COM 		}
34494235Smarkfen 		break;
34504235Smarkfen 	case CMD_SAVE:
34514342Spwernau 		if (read_cmdfile) {
34524342Spwernau 			ERROR(ep, ebuf, gettext("Save not appropriate in "
34534342Spwernau 			    "config file."));
34544342Spwernau 		} else {
34554342Spwernau 			mask_signals(B_FALSE);	/* Mask signals */
34564342Spwernau 			dodump(satype, opensavefile(argv[0]));
34574342Spwernau 			mask_signals(B_TRUE);	/* Unmask signals */
34584342Spwernau 		}
34594235Smarkfen 		break;
34604235Smarkfen 	default:
34614235Smarkfen 		warnx(gettext("Unknown command (%s).\n"),
34624235Smarkfen 		    *(argv - ((satype == SADB_SATYPE_UNSPEC) ? 1 : 2)));
34634235Smarkfen 		usage();
34644235Smarkfen 	}
34654342Spwernau 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
34664235Smarkfen }
34674235Smarkfen 
34684235Smarkfen int
34694235Smarkfen main(int argc, char *argv[])
34704235Smarkfen {
34714235Smarkfen 	int ch;
34724235Smarkfen 	FILE *infile = stdin, *savefile;
34734235Smarkfen 	boolean_t dosave = B_FALSE, readfile = B_FALSE;
34744235Smarkfen 	char *configfile = NULL;
34755321Spwernau 	struct stat sbuf;
34767749SThejaswini.Singarajipura@Sun.COM 	int bootflags;
34774235Smarkfen 
34784235Smarkfen 	(void) setlocale(LC_ALL, "");
34794235Smarkfen #if !defined(TEXT_DOMAIN)
34804235Smarkfen #define	TEXT_DOMAIN "SYS_TEST"
34814235Smarkfen #endif
34824235Smarkfen 	(void) textdomain(TEXT_DOMAIN);
34834235Smarkfen 
34844235Smarkfen 	/*
34854235Smarkfen 	 * Check to see if the command is being run from smf(5).
34864235Smarkfen 	 */
34874235Smarkfen 	my_fmri = getenv("SMF_FMRI");
34884235Smarkfen 
34894235Smarkfen 	openlog("ipseckey", LOG_CONS, LOG_AUTH);
34904235Smarkfen 	if (getuid() != 0) {
34914235Smarkfen 		errx(1, "Insufficient privileges to run ipseckey.");
34924235Smarkfen 	}
34934235Smarkfen 
34944235Smarkfen 	/* umask me to paranoid, I only want to create files read-only */
34954235Smarkfen 	(void) umask((mode_t)00377);
34964235Smarkfen 
34974235Smarkfen 	while ((ch = getopt(argc, argv, "pnvf:s:c:")) != EOF)
34984235Smarkfen 		switch (ch) {
34994235Smarkfen 		case 'p':
35004235Smarkfen 			pflag = B_TRUE;
35014235Smarkfen 			break;
35024235Smarkfen 		case 'n':
35034235Smarkfen 			nflag = B_TRUE;
35044235Smarkfen 			break;
35054235Smarkfen 		case 'v':
35064235Smarkfen 			vflag = B_TRUE;
35074235Smarkfen 			break;
35084235Smarkfen 		case 'c':
35094235Smarkfen 			cflag = B_TRUE;
35104235Smarkfen 			/* FALLTHRU */
35114235Smarkfen 		case 'f':
35124235Smarkfen 			if (dosave)
35134235Smarkfen 				usage();
35144235Smarkfen 			infile = fopen(optarg, "r");
35154235Smarkfen 			if (infile == NULL) {
35164235Smarkfen 				EXIT_BADCONFIG2("Unable to open configuration "
35174235Smarkfen 				    "file: %s\n", optarg);
35184235Smarkfen 			}
35195321Spwernau 			/*
35205321Spwernau 			 * Check file permissions/ownership and warn or
35215321Spwernau 			 * fail depending on state of SMF control.
35225321Spwernau 			 */
35235321Spwernau 			if (fstat(fileno(infile), &sbuf) == -1) {
35245321Spwernau 				(void) fclose(infile);
35255321Spwernau 				EXIT_BADCONFIG2("Unable to stat configuration "
35265321Spwernau 				    "file: %s\n", optarg);
35275321Spwernau 			}
35285321Spwernau 			if (INSECURE_PERMS(sbuf)) {
35295321Spwernau 				if (my_fmri != NULL) {
35305321Spwernau 					(void) fclose(infile);
35315321Spwernau 					EXIT_BADCONFIG2("Config file "
35325321Spwernau 					    "%s has insecure permissions.",
35335321Spwernau 					    optarg);
35345321Spwernau 				} else 	{
35355321Spwernau 					(void) fprintf(stderr, "%s %s\n",
35365321Spwernau 					    optarg, gettext(
35375321Spwernau 					    "has insecure permissions, will be "
35385321Spwernau 					    "rejected in permanent config."));
35395321Spwernau 				}
35405321Spwernau 			}
35414235Smarkfen 			configfile = strdup(optarg);
35424235Smarkfen 			readfile = B_TRUE;
35434235Smarkfen 			break;
35444235Smarkfen 		case 's':
35454235Smarkfen 			if (readfile)
35464235Smarkfen 				usage();
35474235Smarkfen 			dosave = B_TRUE;
35484235Smarkfen 			savefile = opensavefile(optarg);
35494235Smarkfen 			break;
35504235Smarkfen 		default:
35514235Smarkfen 			usage();
35524235Smarkfen 		}
35534235Smarkfen 
35544235Smarkfen 	argc -= optind;
35554235Smarkfen 	argv += optind;
35564235Smarkfen 
35574235Smarkfen 	mypid = getpid();
35584235Smarkfen 
35594235Smarkfen 	keysock = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
35604235Smarkfen 
35614235Smarkfen 	if (keysock == -1) {
35624235Smarkfen 		if (errno == EPERM) {
35634235Smarkfen 			EXIT_BADPERM("Insufficient privileges to open "
35644235Smarkfen 			    "PF_KEY socket.\n");
35654235Smarkfen 		} else {
35664235Smarkfen 			/* some other reason */
35674235Smarkfen 			EXIT_FATAL("Opening PF_KEY socket");
35684235Smarkfen 		}
35694235Smarkfen 	}
35704235Smarkfen 
35717749SThejaswini.Singarajipura@Sun.COM 	if ((_cladm(CL_INITIALIZE, CL_GET_BOOTFLAG, &bootflags) != 0) ||
35727749SThejaswini.Singarajipura@Sun.COM 	    (bootflags & CLUSTER_BOOTED)) {
35737749SThejaswini.Singarajipura@Sun.COM 		in_cluster_mode = B_TRUE;
35747749SThejaswini.Singarajipura@Sun.COM 		cluster_socket = socket(AF_INET, SOCK_DGRAM, 0);
35757749SThejaswini.Singarajipura@Sun.COM 		cli_addr.sin_family = AF_INET;
35767749SThejaswini.Singarajipura@Sun.COM 		cli_addr.sin_addr.s_addr = INADDR_LOOPBACK;
35777749SThejaswini.Singarajipura@Sun.COM 		cli_addr.sin_port = htons(CLUSTER_UDP_PORT);
35787749SThejaswini.Singarajipura@Sun.COM 	}
35797749SThejaswini.Singarajipura@Sun.COM 
35804235Smarkfen 	if (dosave) {
35814235Smarkfen 		mask_signals(B_FALSE);	/* Mask signals */
35824235Smarkfen 		dodump(SADB_SATYPE_UNSPEC, savefile);
35834235Smarkfen 		mask_signals(B_TRUE);	/* Unmask signals */
35844235Smarkfen 		EXIT_OK(NULL);
35854235Smarkfen 	}
35864235Smarkfen 
35874235Smarkfen 	/*
35884235Smarkfen 	 * When run from smf(5) flush any existing SA's first
35894235Smarkfen 	 * otherwise you will end up in maintenance mode.
35904235Smarkfen 	 */
35914235Smarkfen 	if ((my_fmri != NULL) && readfile) {
35924235Smarkfen 		(void) fprintf(stdout, gettext(
35934235Smarkfen 		    "Flushing existing SA's before adding new SA's\n"));
35944235Smarkfen 		(void) fflush(stdout);
35954235Smarkfen 		doflush(SADB_SATYPE_UNSPEC);
35964235Smarkfen 	}
35979086SVladimir.Kotal@Sun.COM 	if (infile != stdin || argc == 0) {
35984235Smarkfen 		/* Go into interactive mode here. */
35994235Smarkfen 		do_interactive(infile, configfile, "ipseckey> ", my_fmri,
36009086SVladimir.Kotal@Sun.COM 		    parseit, no_match);
36014235Smarkfen 	}
36024342Spwernau 	parseit(argc, argv, NULL, B_FALSE);
36034235Smarkfen 
36044235Smarkfen 	return (0);
36054235Smarkfen }
3606