xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ipseckey.c (revision 12655:1759b5b76e53)
14235Smarkfen /*
24235Smarkfen  * CDDL HEADER START
34235Smarkfen  *
44235Smarkfen  * The contents of this file are subject to the terms of the
54235Smarkfen  * Common Development and Distribution License (the "License").
64235Smarkfen  * You may not use this file except in compliance with the License.
74235Smarkfen  *
84235Smarkfen  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94235Smarkfen  * or http://www.opensolaris.org/os/licensing.
104235Smarkfen  * See the License for the specific language governing permissions
114235Smarkfen  * and limitations under the License.
124235Smarkfen  *
134235Smarkfen  * When distributing Covered Code, include this CDDL HEADER in each
144235Smarkfen  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154235Smarkfen  * If applicable, add the following below this CDDL HEADER, with the
164235Smarkfen  * fields enclosed by brackets "[]" replaced with your own identifying
174235Smarkfen  * information: Portions Copyright [yyyy] [name of copyright owner]
184235Smarkfen  *
194235Smarkfen  * CDDL HEADER END
204235Smarkfen  */
214235Smarkfen /*
22*12655SVladimir.Kotal@Sun.COM  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
234235Smarkfen  */
244235Smarkfen 
254235Smarkfen /*
264235Smarkfen  * NOTE:I'm trying to use "struct sadb_foo" instead of "sadb_foo_t"
274235Smarkfen  *	as a maximal PF_KEY portability test.
284235Smarkfen  *
294235Smarkfen  *	Also, this is a deliberately single-threaded app, also for portability
304235Smarkfen  *	to systems without POSIX threads.
314235Smarkfen  */
324235Smarkfen 
334235Smarkfen #include <sys/types.h>
344235Smarkfen #include <sys/stat.h>
354235Smarkfen #include <sys/socket.h>
364235Smarkfen #include <sys/sysmacros.h>
374235Smarkfen #include <sys/fcntl.h>
384235Smarkfen #include <net/pfkeyv2.h>
394235Smarkfen #include <arpa/inet.h>
404235Smarkfen #include <netinet/in.h>
414235Smarkfen #include <sys/uio.h>
424235Smarkfen 
434235Smarkfen #include <syslog.h>
444235Smarkfen #include <signal.h>
454235Smarkfen #include <unistd.h>
464235Smarkfen #include <limits.h>
474235Smarkfen #include <stdlib.h>
484235Smarkfen #include <stdio.h>
494235Smarkfen #include <stdarg.h>
504235Smarkfen #include <netdb.h>
514235Smarkfen #include <pwd.h>
524235Smarkfen #include <errno.h>
534235Smarkfen #include <libintl.h>
544235Smarkfen #include <locale.h>
554235Smarkfen #include <fcntl.h>
564235Smarkfen #include <strings.h>
574235Smarkfen #include <ctype.h>
587749SThejaswini.Singarajipura@Sun.COM #include <sys/cladm.h>
594235Smarkfen 
604235Smarkfen #include <ipsec_util.h>
614235Smarkfen 
624235Smarkfen static int keysock;
637749SThejaswini.Singarajipura@Sun.COM static int cluster_socket;
644235Smarkfen static uint32_t seq;
654235Smarkfen static pid_t mypid;
664235Smarkfen static boolean_t vflag = B_FALSE;	/* Verbose? */
674235Smarkfen static boolean_t cflag = B_FALSE;	/* Check Only */
684235Smarkfen 
694235Smarkfen char *my_fmri = NULL;
704235Smarkfen FILE *debugfile = stdout;
717749SThejaswini.Singarajipura@Sun.COM static struct sockaddr_in cli_addr;
727749SThejaswini.Singarajipura@Sun.COM static boolean_t in_cluster_mode = B_FALSE;
734235Smarkfen 
744235Smarkfen #define	MAX_GET_SIZE	1024
754235Smarkfen /*
764573Spwernau  * WARN() and ERROR() do the same thing really, with ERROR() the function
774235Smarkfen  * that prints the error buffer needs to be called at the end of a code block
784235Smarkfen  * This will print out all accumulated errors before bailing. The WARN()
794235Smarkfen  * macro calls handle_errors() in such a way that it prints the message
804235Smarkfen  * then continues.
814235Smarkfen  * If the FATAL() macro used call handle_errors() immediately.
824235Smarkfen  */
834235Smarkfen #define	ERROR(x, y, z)  x = record_error(x, y, z)
844235Smarkfen #define	ERROR1(w, x, y, z)  w = record_error(w, x, y, z)
854235Smarkfen #define	ERROR2(v, w, x, y, z)  v = record_error(v, w, x, y, z)
864235Smarkfen #define	WARN(x, y, z) ERROR(x, y, z);\
874235Smarkfen 	handle_errors(x, NULL, B_FALSE, B_FALSE); x = NULL
884235Smarkfen #define	WARN1(w, x, y, z) ERROR1(w, x, y, z);\
894235Smarkfen 	handle_errors(w, NULL, B_FALSE, B_FALSE); w = NULL
904235Smarkfen #define	WARN2(v, w, x, y, z) ERROR2(v, w, x, y, z);\
914235Smarkfen 	handle_errors(v, NULL, B_FALSE, B_FALSE); v = NULL
924235Smarkfen #define	FATAL(x, y, z) ERROR(x, y, z);\
934235Smarkfen 	handle_errors(x, y, B_TRUE, B_TRUE)
944235Smarkfen #define	FATAL1(w, x, y, z) ERROR1(w, x, y, z);\
954235Smarkfen 	handle_errors(w, x, B_TRUE, B_TRUE)
964235Smarkfen 
974235Smarkfen /* Defined as a uint64_t array for alignment purposes. */
984235Smarkfen static uint64_t get_buffer[MAX_GET_SIZE];
994235Smarkfen 
1004235Smarkfen /*
1019086SVladimir.Kotal@Sun.COM  * Disable default TAB completion for now (until some brave soul tackles it).
1029086SVladimir.Kotal@Sun.COM  */
1039086SVladimir.Kotal@Sun.COM /* ARGSUSED */
1049086SVladimir.Kotal@Sun.COM static
CPL_MATCH_FN(no_match)1059086SVladimir.Kotal@Sun.COM CPL_MATCH_FN(no_match)
1069086SVladimir.Kotal@Sun.COM {
1079086SVladimir.Kotal@Sun.COM 	return (0);
1089086SVladimir.Kotal@Sun.COM }
1099086SVladimir.Kotal@Sun.COM 
1109086SVladimir.Kotal@Sun.COM /*
1114235Smarkfen  * Create/Grow a buffer large enough to hold error messages. If *ebuf
1124235Smarkfen  * is not NULL then it will contain a copy of the command line that
1134235Smarkfen  * triggered the error/warning, copy this into a new buffer or
1144235Smarkfen  * append new messages to the existing buffer.
1154235Smarkfen  */
1164235Smarkfen /*PRINTFLIKE1*/
1174235Smarkfen char *
record_error(char * ep,char * ebuf,char * fmt,...)1184235Smarkfen record_error(char *ep, char *ebuf, char *fmt, ...)
1194235Smarkfen {
1204235Smarkfen 	char *err_ptr;
1214235Smarkfen 	char tmp_buff[1024];
1224235Smarkfen 	va_list ap;
1234235Smarkfen 	int length = 0;
1244235Smarkfen 	err_ptr = ep;
1254235Smarkfen 
1264235Smarkfen 	va_start(ap, fmt);
1275166Smarkfen 	length = vsnprintf(tmp_buff, sizeof (tmp_buff), fmt, ap);
1284235Smarkfen 	va_end(ap);
1294235Smarkfen 
1305166Smarkfen 	/* There is a new line character */
1315166Smarkfen 	length++;
1325166Smarkfen 
1334235Smarkfen 	if (ep == NULL) {
1345166Smarkfen 		if (ebuf != NULL)
1355166Smarkfen 			length += strlen(ebuf);
1365166Smarkfen 	} else  {
1375166Smarkfen 		length += strlen(ep);
1384235Smarkfen 	}
1395166Smarkfen 
1405166Smarkfen 	if (err_ptr == NULL)
1415166Smarkfen 		err_ptr = calloc(length, sizeof (char));
1425166Smarkfen 	else
1435166Smarkfen 		err_ptr = realloc(err_ptr, length);
1445166Smarkfen 
1454235Smarkfen 	if (err_ptr == NULL)
1464235Smarkfen 		Bail("realloc() failure");
1475166Smarkfen 
1485166Smarkfen 	/*
1495166Smarkfen 	 * If (ep == NULL) then this is the first error to record,
1505166Smarkfen 	 * copy in the command line that triggered this error/warning.
1515166Smarkfen 	 */
1525166Smarkfen 	if (ep == NULL && ebuf != NULL)
1535166Smarkfen 		(void) strlcpy(err_ptr, ebuf, length);
1545166Smarkfen 
1555166Smarkfen 	/*
1565166Smarkfen 	 * Now the actual error.
1575166Smarkfen 	 */
1584235Smarkfen 	(void) strlcat(err_ptr, tmp_buff, length);
1594235Smarkfen 	return (err_ptr);
1604235Smarkfen }
1614235Smarkfen 
1624235Smarkfen /*
1639086SVladimir.Kotal@Sun.COM  * If not in interactive mode print usage message and exit.
1644235Smarkfen  */
1654235Smarkfen static void
usage(void)1664235Smarkfen usage(void)
1674235Smarkfen {
1684235Smarkfen 	if (!interactive) {
1694235Smarkfen 		(void) fprintf(stderr, gettext("Usage:\t"
1704235Smarkfen 		    "ipseckey [ -nvp ] | cmd [sa_type] [extfield value]*\n"));
1714235Smarkfen 		(void) fprintf(stderr,
1724235Smarkfen 		    gettext("\tipseckey [ -nvp ] -f infile\n"));
1734235Smarkfen 		(void) fprintf(stderr,
1744235Smarkfen 		    gettext("\tipseckey [ -nvp ] -s outfile\n"));
1759086SVladimir.Kotal@Sun.COM 		EXIT_FATAL(NULL);
1769086SVladimir.Kotal@Sun.COM 	} else {
1779086SVladimir.Kotal@Sun.COM 		(void) fprintf(stderr,
1789086SVladimir.Kotal@Sun.COM 		    gettext("Type help or ? for usage info\n"));
1794235Smarkfen 	}
1804235Smarkfen }
1814235Smarkfen 
1824235Smarkfen 
1834235Smarkfen /*
1844235Smarkfen  * Print out any errors, tidy up as required.
1854235Smarkfen  * error pointer ep will be free()'d
1864235Smarkfen  */
1874235Smarkfen void
handle_errors(char * ep,char * ebuf,boolean_t fatal,boolean_t done)1884235Smarkfen handle_errors(char *ep, char *ebuf, boolean_t fatal, boolean_t done)
1894235Smarkfen {
1904235Smarkfen 	if (ep != NULL) {
1914235Smarkfen 		if (my_fmri == NULL) {
1924235Smarkfen 			/*
1934235Smarkfen 			 * For now suppress the errors when run from smf(5)
1944235Smarkfen 			 * because potentially sensitive information could
1954235Smarkfen 			 * end up in a publicly readable logfile.
1964235Smarkfen 			 */
1974235Smarkfen 			(void) fprintf(stdout, "%s\n", ep);
1984235Smarkfen 			(void) fflush(stdout);
1994235Smarkfen 		}
2004235Smarkfen 		free(ep);
2014235Smarkfen 		if (fatal) {
2024235Smarkfen 			if (ebuf != NULL) {
2034235Smarkfen 				free(ebuf);
2044235Smarkfen 			}
2054235Smarkfen 			/* reset command buffer */
2064235Smarkfen 			if (interactive)
2074235Smarkfen 				longjmp(env, 1);
2084235Smarkfen 		} else {
2094235Smarkfen 			return;
2104235Smarkfen 		}
2114235Smarkfen 	} else {
2124235Smarkfen 		/*
2134235Smarkfen 		 * No errors, if this is the last time that this function
2144235Smarkfen 		 * is called, free(ebuf) and reset command buffer.
2154235Smarkfen 		 */
2164235Smarkfen 		if (done) {
2174235Smarkfen 			if (ebuf != NULL) {
2184235Smarkfen 				free(ebuf);
2194235Smarkfen 			}
2204235Smarkfen 			/* reset command buffer */
2214235Smarkfen 			if (interactive)
2224235Smarkfen 				longjmp(env, 1);
2234235Smarkfen 		}
2244235Smarkfen 		return;
2254235Smarkfen 	}
2265166Smarkfen 	EXIT_FATAL(NULL);
2274235Smarkfen }
2284235Smarkfen 
2294235Smarkfen /*
2304235Smarkfen  * Initialize a PF_KEY base message.
2314235Smarkfen  */
2324235Smarkfen static void
msg_init(struct sadb_msg * msg,uint8_t type,uint8_t satype)2334235Smarkfen msg_init(struct sadb_msg *msg, uint8_t type, uint8_t satype)
2344235Smarkfen {
2354235Smarkfen 	msg->sadb_msg_version = PF_KEY_V2;
2364235Smarkfen 	msg->sadb_msg_type = type;
2374235Smarkfen 	msg->sadb_msg_errno = 0;
2384235Smarkfen 	msg->sadb_msg_satype = satype;
2394235Smarkfen 	/* For starters... */
2404235Smarkfen 	msg->sadb_msg_len = SADB_8TO64(sizeof (*msg));
2414235Smarkfen 	msg->sadb_msg_reserved = 0;
2424235Smarkfen 	msg->sadb_msg_seq = ++seq;
2434235Smarkfen 	msg->sadb_msg_pid = mypid;
2444235Smarkfen }
2454235Smarkfen 
2464235Smarkfen /*
2474235Smarkfen  * parseXXX and rparseXXX commands parse input and convert them to PF_KEY
2484235Smarkfen  * field values, or do the reverse for the purposes of saving the SA tables.
2494235Smarkfen  * (See the save_XXX functions.)
2504235Smarkfen  */
2514235Smarkfen 
2524235Smarkfen #define	CMD_NONE	0
2534235Smarkfen #define	CMD_UPDATE	2
2546668Smarkfen #define	CMD_UPDATE_PAIR	3
2556668Smarkfen #define	CMD_ADD		4
2566668Smarkfen #define	CMD_DELETE	5
2576668Smarkfen #define	CMD_DELETE_PAIR	6
2586668Smarkfen #define	CMD_GET		7
2594235Smarkfen #define	CMD_FLUSH	9
2604235Smarkfen #define	CMD_DUMP	10
2614235Smarkfen #define	CMD_MONITOR	11
2624235Smarkfen #define	CMD_PMONITOR	12
2634235Smarkfen #define	CMD_QUIT	13
2644235Smarkfen #define	CMD_SAVE	14
2654235Smarkfen #define	CMD_HELP	15
2664235Smarkfen 
2674235Smarkfen /*
2684235Smarkfen  * Parse the command.
2694235Smarkfen  */
2704235Smarkfen static int
parsecmd(char * cmdstr)2714235Smarkfen parsecmd(char *cmdstr)
2724235Smarkfen {
2734235Smarkfen 	static struct cmdtable {
2744235Smarkfen 		char *cmd;
2754235Smarkfen 		int token;
2764235Smarkfen 	} table[] = {
2774235Smarkfen 		/*
2784235Smarkfen 		 * Q: Do we want to do GETSPI?
2794235Smarkfen 		 * A: No, it's for automated key mgmt. only.  Either that,
2804235Smarkfen 		 *    or it isn't relevant until we support non IPsec SA types.
2814235Smarkfen 		 */
2824235Smarkfen 		{"update",		CMD_UPDATE},
2836668Smarkfen 		{"update-pair",		CMD_UPDATE_PAIR},
2844235Smarkfen 		{"add",			CMD_ADD},
2854235Smarkfen 		{"delete", 		CMD_DELETE},
2866668Smarkfen 		{"delete-pair",		CMD_DELETE_PAIR},
2874235Smarkfen 		{"get", 		CMD_GET},
2884235Smarkfen 		/*
2894235Smarkfen 		 * Q: And ACQUIRE and REGISTER and EXPIRE?
2904235Smarkfen 		 * A: not until we support non IPsec SA types.
2914235Smarkfen 		 */
2924235Smarkfen 		{"flush",		CMD_FLUSH},
2934235Smarkfen 		{"dump",		CMD_DUMP},
2944235Smarkfen 		{"monitor",		CMD_MONITOR},
2954235Smarkfen 		{"passive_monitor",	CMD_PMONITOR},
2964235Smarkfen 		{"pmonitor",		CMD_PMONITOR},
2974235Smarkfen 		{"quit",		CMD_QUIT},
2984235Smarkfen 		{"exit",		CMD_QUIT},
2994235Smarkfen 		{"save",		CMD_SAVE},
3004235Smarkfen 		{"help",		CMD_HELP},
3014235Smarkfen 		{"?",			CMD_HELP},
3024235Smarkfen 		{NULL,			CMD_NONE}
3034235Smarkfen 	};
3044235Smarkfen 	struct cmdtable *ct = table;
3054235Smarkfen 
3064235Smarkfen 	while (ct->cmd != NULL && strcmp(ct->cmd, cmdstr) != 0)
3074235Smarkfen 		ct++;
3084235Smarkfen 	return (ct->token);
3094235Smarkfen }
3104235Smarkfen 
3114235Smarkfen /*
3124235Smarkfen  * Convert a number from a command line.  I picked "u_longlong_t" for the
3134235Smarkfen  * number because we need the largest number available.  Also, the strto<num>
3144235Smarkfen  * calls don't deal in units of uintNN_t.
3154235Smarkfen  */
3164235Smarkfen static u_longlong_t
parsenum(char * num,boolean_t bail,char * ebuf)3174235Smarkfen parsenum(char *num, boolean_t bail, char *ebuf)
3184235Smarkfen {
3194235Smarkfen 	u_longlong_t rc = 0;
3204235Smarkfen 	char *end = NULL;
3214235Smarkfen 	char *ep = NULL;
3224235Smarkfen 
3234235Smarkfen 	if (num == NULL) {
3244235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line,"
3254235Smarkfen 		    " was expecting a number.\n"));
3264235Smarkfen 		/* NOTREACHED */
3274235Smarkfen 	}
3284235Smarkfen 
3294235Smarkfen 	errno = 0;
3304235Smarkfen 	rc = strtoull(num, &end, 0);
3314235Smarkfen 	if (errno != 0 || end == num || *end != '\0') {
3324235Smarkfen 		if (bail) {
3336668Smarkfen 			FATAL1(ep, ebuf, gettext(
3344235Smarkfen 			"Expecting a number, not \"%s\"!\n"), num);
3354235Smarkfen 		} else {
3364235Smarkfen 			/*
3374235Smarkfen 			 * -1, while not optimal, is sufficiently out of range
3384235Smarkfen 			 * for most of this function's applications when
3394235Smarkfen 			 * we don't just bail.
3404235Smarkfen 			 */
3414235Smarkfen 			return ((u_longlong_t)-1);
3424235Smarkfen 		}
3434235Smarkfen 	}
3444235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
3454235Smarkfen 	return (rc);
3464235Smarkfen }
3474235Smarkfen 
3484235Smarkfen /*
3494235Smarkfen  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
3504235Smarkfen  */
3514235Smarkfen static struct typetable {
3524235Smarkfen 	char *type;
3534235Smarkfen 	int token;
3544235Smarkfen } type_table[] = {
3554235Smarkfen 	{"all",	SADB_SATYPE_UNSPEC},
3564235Smarkfen 	{"ah",	SADB_SATYPE_AH},
3574235Smarkfen 	{"esp",	SADB_SATYPE_ESP},
3584235Smarkfen 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
3594235Smarkfen 	{NULL,	0}	/* Token value is irrelevant for this entry. */
3604235Smarkfen };
3614235Smarkfen 
3624235Smarkfen 
3634235Smarkfen static int
parsesatype(char * type,char * ebuf)3644235Smarkfen parsesatype(char *type, char *ebuf)
3654235Smarkfen {
3664235Smarkfen 	struct typetable *tt = type_table;
3674235Smarkfen 	char *ep = NULL;
3684235Smarkfen 
3694235Smarkfen 	if (type == NULL)
3704235Smarkfen 		return (SADB_SATYPE_UNSPEC);
3714235Smarkfen 
3724235Smarkfen 	while (tt->type != NULL && strcasecmp(tt->type, type) != 0)
3734235Smarkfen 		tt++;
3744235Smarkfen 
3754235Smarkfen 	/*
3764235Smarkfen 	 * New SA types (including ones keysock maintains for user-land
3774235Smarkfen 	 * protocols) may be added, so parse a numeric value if possible.
3784235Smarkfen 	 */
3794235Smarkfen 	if (tt->type == NULL) {
3804235Smarkfen 		tt->token = (int)parsenum(type, B_FALSE, ebuf);
3814235Smarkfen 		if (tt->token == -1) {
3824235Smarkfen 			ERROR1(ep, ebuf, gettext(
3834235Smarkfen 			    "Unknown SA type (%s).\n"), type);
3844235Smarkfen 			tt->token = SADB_SATYPE_UNSPEC;
3854235Smarkfen 		}
3864235Smarkfen 	}
3879086SVladimir.Kotal@Sun.COM 	handle_errors(ep, NULL, interactive ? B_TRUE : B_FALSE, B_FALSE);
3884235Smarkfen 	return (tt->token);
3894235Smarkfen }
3904235Smarkfen 
3914235Smarkfen #define	NEXTEOF		0
3924235Smarkfen #define	NEXTNONE	1
3934235Smarkfen #define	NEXTNUM		2
3944235Smarkfen #define	NEXTSTR		3
3954235Smarkfen #define	NEXTNUMSTR	4
3964235Smarkfen #define	NEXTADDR	5
3974235Smarkfen #define	NEXTHEX		6
3984235Smarkfen #define	NEXTIDENT	7
3994235Smarkfen #define	NEXTADDR4	8
4004235Smarkfen #define	NEXTADDR6	9
40110934Ssommerfeld@sun.com #define	NEXTLABEL	10
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
45710824SMark.Fenwick@Sun.COM #define	TOK_RESERVED		54
45810934Ssommerfeld@sun.com #define	TOK_LABEL		55
45910934Ssommerfeld@sun.com #define	TOK_OLABEL		56
46010934Ssommerfeld@sun.com #define	TOK_IMPLABEL		57
46110934Ssommerfeld@sun.com 
4624235Smarkfen 
4634235Smarkfen static struct toktable {
4644235Smarkfen 	char *string;
4654235Smarkfen 	int token;
4664235Smarkfen 	int next;
4674235Smarkfen } tokens[] = {
4684235Smarkfen 	/* "String",		token value,		next arg is */
4694235Smarkfen 	{"spi",			TOK_SPI,		NEXTNUM},
4706668Smarkfen 	{"pair-spi",		TOK_PAIR_SPI,		NEXTNUM},
4714235Smarkfen 	{"replay",		TOK_REPLAY,		NEXTNUM},
4724235Smarkfen 	{"state",		TOK_STATE,		NEXTNUMSTR},
4734235Smarkfen 	{"auth_alg",		TOK_AUTHALG,		NEXTNUMSTR},
4744235Smarkfen 	{"authalg",		TOK_AUTHALG,		NEXTNUMSTR},
4754235Smarkfen 	{"encr_alg",		TOK_ENCRALG,		NEXTNUMSTR},
4764235Smarkfen 	{"encralg",		TOK_ENCRALG,		NEXTNUMSTR},
4774235Smarkfen 	{"flags",		TOK_FLAGS,		NEXTNUM},
4784235Smarkfen 	{"soft_alloc",		TOK_SOFT_ALLOC,		NEXTNUM},
4794235Smarkfen 	{"soft_bytes",		TOK_SOFT_BYTES,		NEXTNUM},
4804235Smarkfen 	{"soft_addtime",	TOK_SOFT_ADDTIME,	NEXTNUM},
4814235Smarkfen 	{"soft_usetime",	TOK_SOFT_USETIME,	NEXTNUM},
4824235Smarkfen 	{"hard_alloc",		TOK_HARD_ALLOC,		NEXTNUM},
4834235Smarkfen 	{"hard_bytes",		TOK_HARD_BYTES,		NEXTNUM},
4844235Smarkfen 	{"hard_addtime",	TOK_HARD_ADDTIME,	NEXTNUM},
4854235Smarkfen 	{"hard_usetime",	TOK_HARD_USETIME,	NEXTNUM},
4864235Smarkfen 	{"current_alloc",	TOK_CURRENT_ALLOC,	NEXTNUM},
4874235Smarkfen 	{"current_bytes",	TOK_CURRENT_BYTES,	NEXTNUM},
4884235Smarkfen 	{"current_addtime",	TOK_CURRENT_ADDTIME,	NEXTNUM},
4894235Smarkfen 	{"current_usetime",	TOK_CURRENT_USETIME,	NEXTNUM},
4904235Smarkfen 
4914235Smarkfen 	{"saddr",		TOK_SRCADDR,		NEXTADDR},
4924235Smarkfen 	{"srcaddr",		TOK_SRCADDR,		NEXTADDR},
4934235Smarkfen 	{"src",			TOK_SRCADDR,		NEXTADDR},
4944235Smarkfen 	{"daddr",		TOK_DSTADDR,		NEXTADDR},
4954235Smarkfen 	{"dstaddr",		TOK_DSTADDR,		NEXTADDR},
4964235Smarkfen 	{"dst",			TOK_DSTADDR,		NEXTADDR},
4974235Smarkfen 	{"proxyaddr",		TOK_PROXYADDR,		NEXTADDR},
4984235Smarkfen 	{"proxy",		TOK_PROXYADDR,		NEXTADDR},
4994235Smarkfen 	{"innersrc",		TOK_PROXYADDR,		NEXTADDR},
5004235Smarkfen 	{"isrc",		TOK_PROXYADDR,		NEXTADDR},
5014235Smarkfen 	{"innerdst",		TOK_IDSTADDR,		NEXTADDR},
5024235Smarkfen 	{"idst",		TOK_IDSTADDR,		NEXTADDR},
5034235Smarkfen 
5044235Smarkfen 	{"sport",		TOK_SRCPORT,		NEXTNUM},
5054235Smarkfen 	{"dport",		TOK_DSTPORT,		NEXTNUM},
5064235Smarkfen 	{"innersport",		TOK_ISRCPORT,		NEXTNUM},
5074235Smarkfen 	{"isport",		TOK_ISRCPORT,		NEXTNUM},
5084235Smarkfen 	{"innerdport",		TOK_IDSTPORT,		NEXTNUM},
5094235Smarkfen 	{"idport",		TOK_IDSTPORT,		NEXTNUM},
5104235Smarkfen 	{"proto",		TOK_PROTO,		NEXTNUM},
5114235Smarkfen 	{"ulp",			TOK_PROTO,		NEXTNUM},
5124235Smarkfen 	{"iproto",		TOK_IPROTO,		NEXTNUM},
5134235Smarkfen 	{"iulp",		TOK_IPROTO,		NEXTNUM},
5144235Smarkfen 
5154235Smarkfen 	{"saddr6",		TOK_SRCADDR6,		NEXTADDR},
5164235Smarkfen 	{"srcaddr6",		TOK_SRCADDR6,		NEXTADDR},
5174235Smarkfen 	{"src6",		TOK_SRCADDR6,		NEXTADDR},
5184235Smarkfen 	{"daddr6",		TOK_DSTADDR6,		NEXTADDR},
5194235Smarkfen 	{"dstaddr6",		TOK_DSTADDR6,		NEXTADDR},
5204235Smarkfen 	{"dst6",		TOK_DSTADDR6,		NEXTADDR},
5214235Smarkfen 	{"proxyaddr6",		TOK_PROXYADDR6,		NEXTADDR},
5224235Smarkfen 	{"proxy6",		TOK_PROXYADDR6,		NEXTADDR},
5234235Smarkfen 	{"innersrc6",		TOK_PROXYADDR6,		NEXTADDR},
5244235Smarkfen 	{"isrc6",		TOK_PROXYADDR6,		NEXTADDR},
5254235Smarkfen 	{"innerdst6",		TOK_IDSTADDR6,		NEXTADDR},
5264235Smarkfen 	{"idst6",		TOK_IDSTADDR6,		NEXTADDR},
5274235Smarkfen 
5284235Smarkfen 	{"authkey",		TOK_AUTHKEY,		NEXTHEX},
5294235Smarkfen 	{"encrkey",		TOK_ENCRKEY,		NEXTHEX},
5304235Smarkfen 	{"srcidtype",		TOK_SRCIDTYPE,		NEXTIDENT},
5314235Smarkfen 	{"dstidtype",		TOK_DSTIDTYPE,		NEXTIDENT},
5324235Smarkfen 	{"dpd",			TOK_DPD,		NEXTNUM},
5334235Smarkfen 	{"sens_level",		TOK_SENS_LEVEL,		NEXTNUM},
5344235Smarkfen 	{"sens_map",		TOK_SENS_MAP,		NEXTHEX},
5354235Smarkfen 	{"integ_level",		TOK_INTEG_LEVEL,	NEXTNUM},
5364235Smarkfen 	{"integ_map",		TOK_INTEG_MAP,		NEXTHEX},
5374235Smarkfen 	{"nat_loc",		TOK_NATLOC,		NEXTADDR},
5384235Smarkfen 	{"nat_rem",		TOK_NATREM,		NEXTADDR},
5394235Smarkfen 	{"nat_lport",		TOK_NATLPORT,		NEXTNUM},
5404235Smarkfen 	{"nat_rport",		TOK_NATRPORT,		NEXTNUM},
5414235Smarkfen 	{"encap",		TOK_ENCAP,		NEXTNUMSTR},
5426668Smarkfen 
5436668Smarkfen 	{"outbound",		TOK_FLAG_OUTBOUND,	NULL},
5446668Smarkfen 	{"inbound",		TOK_FLAG_INBOUND,	NULL},
5457749SThejaswini.Singarajipura@Sun.COM 
54610824SMark.Fenwick@Sun.COM 	{"reserved_bits",	TOK_RESERVED,		NEXTNUM},
5477749SThejaswini.Singarajipura@Sun.COM 	{"replay_value",	TOK_REPLAY_VALUE,	NEXTNUM},
5487749SThejaswini.Singarajipura@Sun.COM 	{"idle_addtime",	TOK_IDLE_ADDTIME,	NEXTNUM},
5497749SThejaswini.Singarajipura@Sun.COM 	{"idle_usetime",	TOK_IDLE_USETIME,	NEXTNUM},
55010934Ssommerfeld@sun.com 
55110934Ssommerfeld@sun.com 	{"label",		TOK_LABEL,		NEXTLABEL},
55210934Ssommerfeld@sun.com 	{"outer-label",		TOK_OLABEL,		NEXTLABEL},
55310934Ssommerfeld@sun.com 	{"implicit-label",	TOK_IMPLABEL,		NEXTLABEL},
55410934Ssommerfeld@sun.com 
5554235Smarkfen 	{NULL,			TOK_UNKNOWN,		NEXTEOF}
5564235Smarkfen };
5574235Smarkfen 
5584235Smarkfen /*
5594235Smarkfen  * Q:	Do I need stuff for proposals, combinations, supported algorithms,
5604235Smarkfen  *	or SPI ranges?
5614235Smarkfen  *
5624235Smarkfen  * A:	Probably not, but you never know.
5634235Smarkfen  *
5644235Smarkfen  * Parse out extension header type values.
5654235Smarkfen  */
5664235Smarkfen static int
parseextval(char * value,int * next)5674235Smarkfen parseextval(char *value, int *next)
5684235Smarkfen {
5694235Smarkfen 	struct toktable *tp;
5704235Smarkfen 
5714235Smarkfen 	if (value == NULL)
5724235Smarkfen 		return (TOK_EOF);
5734235Smarkfen 
5744235Smarkfen 	for (tp = tokens; tp->string != NULL; tp++)
5754235Smarkfen 		if (strcmp(value, tp->string) == 0)
5764235Smarkfen 			break;
5774235Smarkfen 
5784235Smarkfen 	/*
5794235Smarkfen 	 * Since the OS controls what extensions are available, we don't have
5804235Smarkfen 	 * to parse numeric values here.
5814235Smarkfen 	 */
5824235Smarkfen 
5834235Smarkfen 	*next = tp->next;
5844235Smarkfen 	return (tp->token);
5854235Smarkfen }
5864235Smarkfen 
5874235Smarkfen /*
5884235Smarkfen  * Parse possible state values.
5894235Smarkfen  */
5904235Smarkfen static uint8_t
parsestate(char * state,char * ebuf)5914235Smarkfen parsestate(char *state, char *ebuf)
5924235Smarkfen {
5934235Smarkfen 	struct states {
5944235Smarkfen 		char *state;
5954235Smarkfen 		uint8_t retval;
5964235Smarkfen 	} states[] = {
5974235Smarkfen 		{"larval",	SADB_SASTATE_LARVAL},
5984235Smarkfen 		{"mature",	SADB_SASTATE_MATURE},
5994235Smarkfen 		{"dying",	SADB_SASTATE_DYING},
6004235Smarkfen 		{"dead",	SADB_SASTATE_DEAD},
6014235Smarkfen 		{NULL,		0}
6024235Smarkfen 	};
6034235Smarkfen 	struct states *sp;
6044235Smarkfen 	char *ep = NULL;
6054235Smarkfen 
6064235Smarkfen 	if (state == NULL) {
6074235Smarkfen 		FATAL(ep, ebuf, "Unexpected end of command line "
6084235Smarkfen 		    "was expecting a state.\n");
6094235Smarkfen 	}
6104235Smarkfen 
6114235Smarkfen 	for (sp = states; sp->state != NULL; sp++) {
6124235Smarkfen 		if (strcmp(sp->state, state) == 0)
6134235Smarkfen 			return (sp->retval);
6144235Smarkfen 	}
6154235Smarkfen 	ERROR1(ep, ebuf, gettext("Unknown state type \"%s\"\n"), state);
6164235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
6174235Smarkfen 	return (0);
6184235Smarkfen }
6194235Smarkfen 
6204235Smarkfen /*
6214235Smarkfen  * Return the numerical algorithm identifier corresponding to the specified
6224235Smarkfen  * algorithm name.
6234235Smarkfen  */
6244235Smarkfen static uint8_t
parsealg(char * alg,int proto_num,char * ebuf)6254235Smarkfen parsealg(char *alg, int proto_num, char *ebuf)
6264235Smarkfen {
6274235Smarkfen 	u_longlong_t invalue;
6284235Smarkfen 	struct ipsecalgent *algent;
6294235Smarkfen 	char *ep = NULL;
6304235Smarkfen 
6314235Smarkfen 	if (alg == NULL) {
6324235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
6334235Smarkfen 		    "was expecting an algorithm name.\n"));
6344235Smarkfen 	}
6354235Smarkfen 
6364235Smarkfen 	algent = getipsecalgbyname(alg, proto_num, NULL);
6374235Smarkfen 	if (algent != NULL) {
6384235Smarkfen 		uint8_t alg_num;
6394235Smarkfen 
6404235Smarkfen 		alg_num = algent->a_alg_num;
64110824SMark.Fenwick@Sun.COM 		if (ALG_FLAG_COUNTERMODE & algent->a_alg_flags)
64210824SMark.Fenwick@Sun.COM 			WARN1(ep, ebuf, gettext(
64310824SMark.Fenwick@Sun.COM 			    "Using manual keying with a Counter mode algorithm "
64410824SMark.Fenwick@Sun.COM 			    "such as \"%s\" may be insecure!\n"),
64510824SMark.Fenwick@Sun.COM 			    algent->a_names[0]);
6464235Smarkfen 		freeipsecalgent(algent);
6474235Smarkfen 
6484235Smarkfen 		return (alg_num);
6494235Smarkfen 	}
6504235Smarkfen 
6514235Smarkfen 	/*
6524235Smarkfen 	 * Since algorithms can be loaded during kernel run-time, check for
6534235Smarkfen 	 * numeric algorithm values too.  PF_KEY can catch bad ones with EINVAL.
6544235Smarkfen 	 */
6554235Smarkfen 	invalue = parsenum(alg, B_FALSE, ebuf);
6564235Smarkfen 	if (invalue != (u_longlong_t)-1 &&
6574235Smarkfen 	    (u_longlong_t)(invalue & (u_longlong_t)0xff) == invalue)
6584235Smarkfen 		return ((uint8_t)invalue);
6594235Smarkfen 
6604235Smarkfen 	if (proto_num == IPSEC_PROTO_ESP) {
6614235Smarkfen 		ERROR1(ep, ebuf, gettext(
6624235Smarkfen 		    "Unknown encryption algorithm type \"%s\"\n"), alg);
6634235Smarkfen 	} else {
6644235Smarkfen 		ERROR1(ep, ebuf, gettext(
6654235Smarkfen 		    "Unknown authentication algorithm type \"%s\"\n"), alg);
6664235Smarkfen 	}
6674235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
6684235Smarkfen 	return (0);
6694235Smarkfen }
6704235Smarkfen 
6714235Smarkfen /*
6724235Smarkfen  * Parse and reverse parse out a source/destination ID type.
6734235Smarkfen  */
6744235Smarkfen static struct idtypes {
6754235Smarkfen 	char *idtype;
6764235Smarkfen 	uint8_t retval;
6774235Smarkfen } idtypes[] = {
6784235Smarkfen 	{"prefix",	SADB_IDENTTYPE_PREFIX},
6794235Smarkfen 	{"fqdn",	SADB_IDENTTYPE_FQDN},
6804235Smarkfen 	{"domain",	SADB_IDENTTYPE_FQDN},
6814235Smarkfen 	{"domainname",	SADB_IDENTTYPE_FQDN},
6824235Smarkfen 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
6834235Smarkfen 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
6844235Smarkfen 	{"der_dn",	SADB_X_IDENTTYPE_DN},
6854235Smarkfen 	{"der_gn",	SADB_X_IDENTTYPE_GN},
6864235Smarkfen 	{NULL,		0}
6874235Smarkfen };
6884235Smarkfen 
6894235Smarkfen static uint16_t
parseidtype(char * type,char * ebuf)6904235Smarkfen parseidtype(char *type, char *ebuf)
6914235Smarkfen {
6924235Smarkfen 	struct idtypes *idp;
6934235Smarkfen 	u_longlong_t invalue;
6944235Smarkfen 	char *ep = NULL;
6954235Smarkfen 
6964235Smarkfen 	if (type == NULL) {
6974235Smarkfen 		/* Shouldn't reach here, see callers for why. */
6984235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
6994235Smarkfen 		    "was expecting a type.\n"));
7004235Smarkfen 	}
7014235Smarkfen 
7024235Smarkfen 	for (idp = idtypes; idp->idtype != NULL; idp++) {
7034235Smarkfen 		if (strcasecmp(idp->idtype, type) == 0)
7044235Smarkfen 			return (idp->retval);
7054235Smarkfen 	}
7064235Smarkfen 	/*
7074235Smarkfen 	 * Since identity types are almost arbitrary, check for numeric
7084235Smarkfen 	 * algorithm values too.  PF_KEY can catch bad ones with EINVAL.
7094235Smarkfen 	 */
7104235Smarkfen 	invalue = parsenum(type, B_FALSE, ebuf);
7114235Smarkfen 	if (invalue != (u_longlong_t)-1 &&
7124235Smarkfen 	    (u_longlong_t)(invalue & (u_longlong_t)0xffff) == invalue)
7134235Smarkfen 		return ((uint16_t)invalue);
7144235Smarkfen 
7154235Smarkfen 
7164235Smarkfen 	ERROR1(ep, ebuf, gettext("Unknown identity type \"%s\"\n"), type);
7174235Smarkfen 
7184235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
7194235Smarkfen 	return (0);
7204235Smarkfen }
7214235Smarkfen 
7224235Smarkfen /*
7234235Smarkfen  * Parse an address off the command line.  Return length of sockaddr,
7244235Smarkfen  * and either return a hostent pointer (caller frees).  The new
7254235Smarkfen  * getipnodebyname() call does the Right Thing (TM), even with
7264235Smarkfen  * raw addresses (colon-separated IPv6 or dotted decimal IPv4).
7274235Smarkfen  */
7284235Smarkfen 
7294235Smarkfen static struct {
7304235Smarkfen 	struct hostent he;
7314235Smarkfen 	char *addtl[2];
7324235Smarkfen 	} dummy;
7334235Smarkfen static union {
7344235Smarkfen 	struct in6_addr ipv6;
7354235Smarkfen 	struct in_addr ipv4;
7364235Smarkfen 	uint64_t aligner;
7374235Smarkfen } addr1;
7384235Smarkfen 
7394235Smarkfen static int
parseaddr(char * addr,struct hostent ** hpp,boolean_t v6only,char * ebuf)7404235Smarkfen parseaddr(char *addr, struct hostent **hpp, boolean_t v6only, char *ebuf)
7414235Smarkfen {
7424235Smarkfen 	int hp_errno;
7434235Smarkfen 	struct hostent *hp = NULL;
7444235Smarkfen 	char *ep = NULL;
7454235Smarkfen 
7464235Smarkfen 	if (addr == NULL) {
7474235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
7484235Smarkfen 		    "was expecting an address.\n"));
7494235Smarkfen 	}
7504235Smarkfen 
7514235Smarkfen 	if (!nflag) {
7524235Smarkfen 		/*
7534235Smarkfen 		 * Try name->address first.  Assume AF_INET6, and
7544235Smarkfen 		 * get IPv4's, plus IPv6's if and only if IPv6 is configured.
7554235Smarkfen 		 * This means to add IPv6 SAs, you must have IPv6
7564235Smarkfen 		 * up-and-running.  (AI_DEFAULT works here.)
7574235Smarkfen 		 */
7584235Smarkfen 		hp = getipnodebyname(addr, AF_INET6,
7594235Smarkfen 		    (v6only ? AI_ADDRCONFIG : (AI_DEFAULT | AI_ALL)),
7604235Smarkfen 		    &hp_errno);
7614235Smarkfen 	} else {
7624235Smarkfen 		/*
7634235Smarkfen 		 * Try a normal address conversion only.  Use "dummy"
7644235Smarkfen 		 * to construct a fake hostent.  Caller will know not
7654235Smarkfen 		 * to free this one.
7664235Smarkfen 		 */
7674235Smarkfen 		if (inet_pton(AF_INET6, addr, &addr1) == 1) {
7684235Smarkfen 			dummy.he.h_addr_list = dummy.addtl;
7694235Smarkfen 			dummy.addtl[0] = (char *)&addr1;
7704235Smarkfen 			dummy.addtl[1] = NULL;
7714235Smarkfen 			hp = &dummy.he;
7724235Smarkfen 			dummy.he.h_addrtype = AF_INET6;
7734235Smarkfen 			dummy.he.h_length = sizeof (struct in6_addr);
7744235Smarkfen 		} else if (inet_pton(AF_INET, addr, &addr1) == 1) {
7754235Smarkfen 			/*
7764235Smarkfen 			 * Remap to AF_INET6 anyway.
7774235Smarkfen 			 */
7784235Smarkfen 			dummy.he.h_addr_list = dummy.addtl;
7794235Smarkfen 			dummy.addtl[0] = (char *)&addr1;
7804235Smarkfen 			dummy.addtl[1] = NULL;
7814235Smarkfen 			hp = &dummy.he;
7824235Smarkfen 			dummy.he.h_addrtype = AF_INET6;
7834235Smarkfen 			dummy.he.h_length = sizeof (struct in6_addr);
7844235Smarkfen 			/*
7854235Smarkfen 			 * NOTE:  If macro changes to disallow in-place
7864235Smarkfen 			 * conversion, rewhack this.
7874235Smarkfen 			 */
7884235Smarkfen 			IN6_INADDR_TO_V4MAPPED(&addr1.ipv4, &addr1.ipv6);
7894235Smarkfen 		} else {
7904235Smarkfen 			hp = NULL;
7914235Smarkfen 		}
7924235Smarkfen 	}
7934235Smarkfen 
7944235Smarkfen 	if (hp == NULL)
7954235Smarkfen 		WARN1(ep, ebuf, gettext("Unknown address %s."), addr);
7964235Smarkfen 
7974235Smarkfen 	*hpp = hp;
7984235Smarkfen 	/* Always return sockaddr_in6 for now. */
7994235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
8004235Smarkfen 	return (sizeof (struct sockaddr_in6));
8014235Smarkfen }
8024235Smarkfen 
8034235Smarkfen /*
8044235Smarkfen  * Parse a hex character for a key.  A string will take the form:
8054235Smarkfen  *	xxxxxxxxx/nn
8064235Smarkfen  * where
8074235Smarkfen  *	xxxxxxxxx == a string of hex characters ([0-9][a-f][A-F])
8084235Smarkfen  *	nn == an optional decimal "mask".  If it is not present, it
8094235Smarkfen  *	is assumed that the hex string will be rounded to the nearest
8104235Smarkfen  *	byte, where odd nibbles, like 123 will become 0x0123.
8114235Smarkfen  *
8124235Smarkfen  * NOTE:Unlike the expression of IP addresses, I will not allow an
8134235Smarkfen  *	excessive "mask".  For example 2112/50 is very illegal.
8144235Smarkfen  * NOTE2:	This key should be in canonical order.  Consult your man
8154235Smarkfen  *		pages per algorithm about said order.
8164235Smarkfen  */
8174235Smarkfen 
8184235Smarkfen #define	hd2num(hd) (((hd) >= '0' && (hd) <= '9') ? ((hd) - '0') : \
8194235Smarkfen 	(((hd) >= 'a' && (hd) <= 'f') ? ((hd) - 'a' + 10) : ((hd) - 'A' + 10)))
8204235Smarkfen 
8214235Smarkfen static struct sadb_key *
parsekey(char * input,char * ebuf,uint_t reserved_bits)82210824SMark.Fenwick@Sun.COM parsekey(char *input, char *ebuf, uint_t reserved_bits)
8234235Smarkfen {
8244235Smarkfen 	struct sadb_key *retval;
8254235Smarkfen 	uint_t i, hexlen = 0, bits, alloclen;
8264235Smarkfen 	uint8_t *key;
8274235Smarkfen 	char *ep = NULL;
8284235Smarkfen 
8294235Smarkfen 	if (input == NULL) {
8304235Smarkfen 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
8314235Smarkfen 		    "was expecting a key.\n"));
8324235Smarkfen 	}
8334573Spwernau 	/* Allow hex values prepended with 0x convention */
8344573Spwernau 	if ((strnlen(input, sizeof (hexlen)) > 2) &&
8354573Spwernau 	    (strncasecmp(input, "0x", 2) == 0))
8364573Spwernau 		input += 2;
8374235Smarkfen 
8384235Smarkfen 	for (i = 0; input[i] != '\0' && input[i] != '/'; i++)
8394235Smarkfen 		hexlen++;
8404235Smarkfen 
8414235Smarkfen 	if (input[i] == '\0') {
8424235Smarkfen 		bits = 0;
8434235Smarkfen 	} else {
8444235Smarkfen 		/* Have /nn. */
8454235Smarkfen 		input[i] = '\0';
8464235Smarkfen 		if (sscanf((input + i + 1), "%u", &bits) != 1) {
8474235Smarkfen 			FATAL1(ep, ebuf, gettext(
8484235Smarkfen 			    "\"%s\" is not a bit specifier.\n"),
8494235Smarkfen 			    (input + i + 1));
8504235Smarkfen 		}
8514235Smarkfen 		/* hexlen in nibbles */
8524235Smarkfen 		if (((bits + 3) >> 2) > hexlen) {
8534235Smarkfen 			ERROR2(ep, ebuf, gettext(
8544235Smarkfen 			    "bit length %d is too big for %s.\n"), bits, input);
8554235Smarkfen 		}
8564235Smarkfen 		/*
8574235Smarkfen 		 * Adjust hexlen down if user gave us too small of a bit
8584235Smarkfen 		 * count.
8594235Smarkfen 		 */
8604235Smarkfen 		if ((hexlen << 2) > bits + 3) {
8614235Smarkfen 			WARN2(ep, ebuf, gettext(
8624235Smarkfen 			    "WARNING: Lower bits will be truncated "
8634235Smarkfen 			    "for:\n\t%s/%d.\n"), input, bits);
8644235Smarkfen 			hexlen = (bits + 3) >> 2;
8654235Smarkfen 			input[hexlen] = '\0';
8664235Smarkfen 		}
8674235Smarkfen 	}
8684235Smarkfen 
8694235Smarkfen 	/*
8704235Smarkfen 	 * Allocate.  Remember, hexlen is in nibbles.
8714235Smarkfen 	 */
8724235Smarkfen 
8734235Smarkfen 	alloclen = sizeof (*retval) + roundup((hexlen/2 + (hexlen & 0x1)), 8);
8744235Smarkfen 	retval = malloc(alloclen);
8754235Smarkfen 
8764235Smarkfen 	if (retval == NULL)
8774235Smarkfen 		Bail("malloc(parsekey)");
8784235Smarkfen 	retval->sadb_key_len = SADB_8TO64(alloclen);
87910824SMark.Fenwick@Sun.COM 
88010824SMark.Fenwick@Sun.COM 	retval->sadb_key_reserved = reserved_bits;
88110824SMark.Fenwick@Sun.COM 
8824235Smarkfen 	if (bits == 0)
8834235Smarkfen 		retval->sadb_key_bits = (hexlen + (hexlen & 0x1)) << 2;
8844235Smarkfen 	else
8854235Smarkfen 		retval->sadb_key_bits = bits;
8864235Smarkfen 
8874235Smarkfen 	/*
8884235Smarkfen 	 * Read in nibbles.  Read in odd-numbered as shifted high.
8894235Smarkfen 	 * (e.g. 123 becomes 0x1230).
8904235Smarkfen 	 */
8914235Smarkfen 
8924235Smarkfen 	key = (uint8_t *)(retval + 1);
8934235Smarkfen 	for (i = 0; input[i] != '\0'; i += 2) {
8944235Smarkfen 		boolean_t second = (input[i + 1] != '\0');
8954235Smarkfen 
8964235Smarkfen 		if (!isxdigit(input[i]) ||
8974235Smarkfen 		    (!isxdigit(input[i + 1]) && second)) {
8984235Smarkfen 			ERROR1(ep, ebuf, gettext(
8994235Smarkfen 			    "string '%s' not a hex value.\n"), input);
9004235Smarkfen 			free(retval);
9014235Smarkfen 			retval = NULL;
9024235Smarkfen 			break;
9034235Smarkfen 		}
9044235Smarkfen 		*key = (hd2num(input[i]) << 4);
9054235Smarkfen 		if (second)
9064235Smarkfen 			*key |= hd2num(input[i + 1]);
9074235Smarkfen 		else
9084235Smarkfen 			break;	/* out of for loop. */
9094235Smarkfen 		key++;
9104235Smarkfen 	}
9114235Smarkfen 
9124235Smarkfen 	/* bzero the remaining bits if we're a non-octet amount. */
9134235Smarkfen 	if (bits & 0x7)
9144235Smarkfen 		*((input[i] == '\0') ? key - 1 : key) &=
9154235Smarkfen 		    0xff << (8 - (bits & 0x7));
9164235Smarkfen 
9174235Smarkfen 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
9184235Smarkfen 	return (retval);
9194235Smarkfen }
9204235Smarkfen 
92110934Ssommerfeld@sun.com #include <tsol/label.h>
92210934Ssommerfeld@sun.com 
92310934Ssommerfeld@sun.com #define	PARSELABEL_BAD_TOKEN ((struct sadb_sens *)-1)
92410934Ssommerfeld@sun.com 
92510934Ssommerfeld@sun.com static struct sadb_sens *
parselabel(int token,char * label)92610934Ssommerfeld@sun.com parselabel(int token, char *label)
92710934Ssommerfeld@sun.com {
92810934Ssommerfeld@sun.com 	bslabel_t *sl = NULL;
92910934Ssommerfeld@sun.com 	int err, len;
93010934Ssommerfeld@sun.com 	sadb_sens_t *sens;
93110934Ssommerfeld@sun.com 	int doi = 1;  /* XXX XXX DEFAULT_DOI XXX XXX */
93210934Ssommerfeld@sun.com 
93310934Ssommerfeld@sun.com 	err = str_to_label(label, &sl, MAC_LABEL, L_DEFAULT, NULL);
93410934Ssommerfeld@sun.com 	if (err < 0)
93510934Ssommerfeld@sun.com 		return (NULL);
93610934Ssommerfeld@sun.com 
93710934Ssommerfeld@sun.com 	len = ipsec_convert_sl_to_sens(doi, sl, NULL);
93810934Ssommerfeld@sun.com 
93910934Ssommerfeld@sun.com 	sens = malloc(len);
94010934Ssommerfeld@sun.com 	if (sens == NULL) {
94110934Ssommerfeld@sun.com 		Bail("malloc parsed label");
94210934Ssommerfeld@sun.com 		/* Should exit before reaching here... */
94310934Ssommerfeld@sun.com 		return (NULL);
94410934Ssommerfeld@sun.com 	}
94510934Ssommerfeld@sun.com 
94610934Ssommerfeld@sun.com 	(void) ipsec_convert_sl_to_sens(doi, sl, sens);
94710934Ssommerfeld@sun.com 
94810934Ssommerfeld@sun.com 	switch (token) {
94910934Ssommerfeld@sun.com 	case TOK_LABEL:
95010934Ssommerfeld@sun.com 		break;
95110934Ssommerfeld@sun.com 
95210934Ssommerfeld@sun.com 	case TOK_OLABEL:
95310934Ssommerfeld@sun.com 		sens->sadb_sens_exttype = SADB_X_EXT_OUTER_SENS;
95410934Ssommerfeld@sun.com 		break;
95510934Ssommerfeld@sun.com 
95610934Ssommerfeld@sun.com 	case TOK_IMPLABEL:
95710934Ssommerfeld@sun.com 		sens->sadb_sens_exttype = SADB_X_EXT_OUTER_SENS;
95810934Ssommerfeld@sun.com 		sens->sadb_x_sens_flags = SADB_X_SENS_IMPLICIT;
95910934Ssommerfeld@sun.com 		break;
96010934Ssommerfeld@sun.com 
96110934Ssommerfeld@sun.com 	default:
96210934Ssommerfeld@sun.com 		free(sens);
96310934Ssommerfeld@sun.com 		/*
96410934Ssommerfeld@sun.com 		 * Return a different return code for a bad label, but really,
96510934Ssommerfeld@sun.com 		 * this would be a caller error.
96610934Ssommerfeld@sun.com 		 */
96710934Ssommerfeld@sun.com 		return (PARSELABEL_BAD_TOKEN);
96810934Ssommerfeld@sun.com 	}
96910934Ssommerfeld@sun.com 
97010934Ssommerfeld@sun.com 	return (sens);
97110934Ssommerfeld@sun.com }
97210934Ssommerfeld@sun.com 
9734235Smarkfen /*
9744235Smarkfen  * Write a message to the PF_KEY socket.  If verbose, print the message
9754235Smarkfen  * heading into the kernel.
9764235Smarkfen  */
9774235Smarkfen static int
key_write(int fd,void * msg,size_t len)9784235Smarkfen key_write(int fd, void *msg, size_t len)
9794235Smarkfen {
9804235Smarkfen 	if (vflag) {
9814235Smarkfen 		(void) printf(
9824235Smarkfen 		    gettext("VERBOSE ON:  Message to kernel looks like:\n"));
9834235Smarkfen 		(void) printf("==========================================\n");
9844867Spwernau 		print_samsg(stdout, msg, B_FALSE, vflag, nflag);
9854235Smarkfen 		(void) printf("==========================================\n");
9864235Smarkfen 	}
9874235Smarkfen 
9884235Smarkfen 	return (write(fd, msg, len));
9894235Smarkfen }
9904235Smarkfen 
9914235Smarkfen /*
9924235Smarkfen  * SIGALRM handler for time_critical_enter.
9934235Smarkfen  */
9944235Smarkfen static void
time_critical_catch(int signal)9954235Smarkfen time_critical_catch(int signal)
9964235Smarkfen {
9974235Smarkfen 	if (signal == SIGALRM) {
9984235Smarkfen 		errx(1, gettext("Reply message from PF_KEY timed out."));
9994235Smarkfen 	} else {
10004235Smarkfen 		errx(1, gettext("Caught signal %d while trying to receive"
10014342Spwernau 		    "PF_KEY reply message"), signal);
10024235Smarkfen 	}
10034235Smarkfen 	/* errx() calls exit. */
10044235Smarkfen }
10054235Smarkfen 
10064235Smarkfen #define	TIME_CRITICAL_TIME 10	/* In seconds */
10074235Smarkfen 
10084235Smarkfen /*
10094235Smarkfen  * Enter a "time critical" section where key is waiting for a return message.
10104235Smarkfen  */
10114235Smarkfen static void
time_critical_enter(void)10124235Smarkfen time_critical_enter(void)
10134235Smarkfen {
10144235Smarkfen 	(void) signal(SIGALRM, time_critical_catch);
10154235Smarkfen 	(void) alarm(TIME_CRITICAL_TIME);
10164235Smarkfen }
10174235Smarkfen 
10184235Smarkfen /*
10194235Smarkfen  * Exit the "time critical" section after getting an appropriate return
10204235Smarkfen  * message.
10214235Smarkfen  */
10224235Smarkfen static void
time_critical_exit(void)10234235Smarkfen time_critical_exit(void)
10244235Smarkfen {
10254235Smarkfen 	(void) alarm(0);
10264235Smarkfen 	(void) signal(SIGALRM, SIG_DFL);
10274235Smarkfen }
10284235Smarkfen 
10294235Smarkfen /*
10304235Smarkfen  * Construct a PF_KEY FLUSH message for the SA type specified.
10314235Smarkfen  */
10324235Smarkfen static void
doflush(int satype)10334235Smarkfen doflush(int satype)
10344235Smarkfen {
10354235Smarkfen 	struct sadb_msg msg;
10364235Smarkfen 	int rc;
10374235Smarkfen 
10384235Smarkfen 	msg_init(&msg, SADB_FLUSH, (uint8_t)satype);
10394235Smarkfen 	rc = key_write(keysock, &msg, sizeof (msg));
10404235Smarkfen 	if (rc == -1)
10414235Smarkfen 		Bail("write() to PF_KEY socket failed (in doflush)");
10424235Smarkfen 
10434235Smarkfen 	time_critical_enter();
10444235Smarkfen 	do {
10454235Smarkfen 		rc = read(keysock, &msg, sizeof (msg));
10464235Smarkfen 		if (rc == -1)
10474235Smarkfen 			Bail("read (in doflush)");
10484235Smarkfen 	} while (msg.sadb_msg_seq != seq || msg.sadb_msg_pid != mypid);
10494235Smarkfen 	time_critical_exit();
10504235Smarkfen 
10514235Smarkfen 	/*
10524235Smarkfen 	 * I should _never_ hit the following unless:
10534235Smarkfen 	 *
10544235Smarkfen 	 * 1. There is a kernel bug.
10554235Smarkfen 	 * 2. There is another process filling in its pid with mine, and
10564235Smarkfen 	 *    issuing a different message that would cause a different result.
10574235Smarkfen 	 */
10584235Smarkfen 	if (msg.sadb_msg_type != SADB_FLUSH ||
10594235Smarkfen 	    msg.sadb_msg_satype != (uint8_t)satype) {
10604235Smarkfen 		syslog((LOG_NOTICE|LOG_AUTH),
10614235Smarkfen 		    gettext("doflush: Return message not of type SADB_FLUSH!"));
10624235Smarkfen 		Bail("doflush: Return message not of type SADB_FLUSH!");
10634235Smarkfen 	}
10644235Smarkfen 
10654235Smarkfen 	if (msg.sadb_msg_errno != 0) {
10664235Smarkfen 		errno = msg.sadb_msg_errno;
10674235Smarkfen 		if (errno == EINVAL) {
10684235Smarkfen 			print_diagnostic(stderr, msg.sadb_x_msg_diagnostic);
10694235Smarkfen 			warnx(gettext("Cannot flush SA type %d."), satype);
10704235Smarkfen 		}
10714235Smarkfen 		Bail("return message (in doflush)");
10724235Smarkfen 	}
10734235Smarkfen }
10744235Smarkfen 
10754235Smarkfen /*
10764235Smarkfen  * save_XXX functions are used when "saving" the SA tables to either a
10774235Smarkfen  * file or standard output.  They use the dump_XXX functions where needed,
10784235Smarkfen  * but mostly they use the rparseXXX functions.
10794235Smarkfen  */
10804235Smarkfen 
10814235Smarkfen /*
10824235Smarkfen  * Because "save" and "dump" both use the SADB_DUMP message, fold both
10834235Smarkfen  * into the same function.
10844235Smarkfen  */
10854235Smarkfen static void
dodump(int satype,FILE * ofile)10864235Smarkfen dodump(int satype, FILE *ofile)
10874235Smarkfen {
10884235Smarkfen 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
10894235Smarkfen 	int rc;
10904235Smarkfen 
10914235Smarkfen 	if (ofile != NULL) {
10924235Smarkfen 		(void) fprintf(ofile,
10934235Smarkfen 		    gettext("# This key file was generated by the"));
10944235Smarkfen 		(void) fprintf(ofile,
10954235Smarkfen 		    gettext(" ipseckey(1m) command's 'save' feature.\n\n"));
10964235Smarkfen 	}
10974235Smarkfen 	msg_init(msg, SADB_DUMP, (uint8_t)satype);
10984235Smarkfen 	rc = key_write(keysock, msg, sizeof (*msg));
10994235Smarkfen 	if (rc == -1)
11004235Smarkfen 		Bail("write to PF_KEY socket failed (in dodump)");
11014235Smarkfen 
11024235Smarkfen 	do {
11034235Smarkfen 		/*
11044235Smarkfen 		 * For DUMP, do only the read as a time critical section.
11054235Smarkfen 		 */
11064235Smarkfen 		time_critical_enter();
11074235Smarkfen 		rc = read(keysock, get_buffer, sizeof (get_buffer));
11084235Smarkfen 		time_critical_exit();
11094235Smarkfen 		if (rc == -1)
11104235Smarkfen 			Bail("read (in dodump)");
11114235Smarkfen 		if (msg->sadb_msg_pid == mypid &&
11124235Smarkfen 		    msg->sadb_msg_type == SADB_DUMP &&
11134235Smarkfen 		    msg->sadb_msg_seq != 0 &&
11144235Smarkfen 		    msg->sadb_msg_errno == 0) {
11154235Smarkfen 			if (ofile == NULL) {
11164867Spwernau 				print_samsg(stdout, get_buffer, B_FALSE, vflag,
11174867Spwernau 				    nflag);
11184235Smarkfen 				(void) putchar('\n');
11194235Smarkfen 			} else {
11204235Smarkfen 				save_assoc(get_buffer, ofile);
11214235Smarkfen 			}
11224235Smarkfen 		}
11234235Smarkfen 	} while (msg->sadb_msg_pid != mypid ||
11244235Smarkfen 	    (msg->sadb_msg_errno == 0 && msg->sadb_msg_seq != 0));
11254235Smarkfen 
11264235Smarkfen 	if (ofile != NULL && ofile != stdout)
11274235Smarkfen 		(void) fclose(ofile);
11284235Smarkfen 
11294235Smarkfen 	if (msg->sadb_msg_errno == 0) {
11304235Smarkfen 		if (ofile == NULL)
11314235Smarkfen 			(void) printf(
11324235Smarkfen 			    gettext("Dump succeeded for SA type %d.\n"),
11334235Smarkfen 			    satype);
11344235Smarkfen 	} else {
11354235Smarkfen 		print_diagnostic(stderr, msg->sadb_x_msg_diagnostic);
11364235Smarkfen 		errno = msg->sadb_msg_errno;
11374235Smarkfen 		Bail("Dump failed");
11384235Smarkfen 	}
11394235Smarkfen }
11404235Smarkfen 
11414235Smarkfen #define	SCOPE_UNSPEC 0
11424235Smarkfen #define	SCOPE_LINKLOCAL 1
11434235Smarkfen #define	SCOPE_SITELOCAL 2
11444235Smarkfen #define	SCOPE_GLOBAL 3
11454235Smarkfen #define	SCOPE_V4COMPAT 4
11464235Smarkfen #define	SCOPE_LOOPBACK 5	/* Pedantic, yes, but necessary. */
11474235Smarkfen 
11484235Smarkfen static int
ipv6_addr_scope(struct in6_addr * addr)11494235Smarkfen ipv6_addr_scope(struct in6_addr *addr)
11504235Smarkfen {
11514235Smarkfen 	/* Don't return anything regarding multicast for now... */
11524235Smarkfen 
11534235Smarkfen 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
11544235Smarkfen 		return (SCOPE_UNSPEC);
11554235Smarkfen 
11564235Smarkfen 	if (IN6_IS_ADDR_LINKLOCAL(addr))
11574235Smarkfen 		return (SCOPE_LINKLOCAL);
11584235Smarkfen 
11594235Smarkfen 	if (IN6_IS_ADDR_SITELOCAL(addr))
11604235Smarkfen 		return (SCOPE_SITELOCAL);
11614235Smarkfen 
11624235Smarkfen 	if (IN6_IS_ADDR_V4COMPAT(addr))
11634235Smarkfen 		return (SCOPE_V4COMPAT);
11644235Smarkfen 
11654235Smarkfen 	if (IN6_IS_ADDR_LOOPBACK(addr))
11664235Smarkfen 		return (SCOPE_LOOPBACK);
11674235Smarkfen 
11684235Smarkfen 	/* For now, return global by default. */
11694235Smarkfen 	return (SCOPE_GLOBAL);
11704235Smarkfen }
11714235Smarkfen 
11724235Smarkfen /*
11734235Smarkfen  * doaddresses():
11744235Smarkfen  *
11754235Smarkfen  * Used by doaddup() and dodelget() to create new SA's based on the
11764235Smarkfen  * provided source and destination addresses hostent.
11774235Smarkfen  *
11784235Smarkfen  * sadb_msg_type: expected PF_KEY reply message type
11794235Smarkfen  * sadb_msg_satype: expected PF_KEY reply satype
11804235Smarkfen  * cmd: user command
11814235Smarkfen  * srchp: hostent for the source address(es)
11824235Smarkfen  * dsthp: hostent for the destination address(es)
11834235Smarkfen  * src: points to the SADB source address extension
11844235Smarkfen  * dst: points to the SADB destination address extension
11854235Smarkfen  * unspec_src: indicates an unspecified source address.
11864235Smarkfen  * buffer: pointer to the SADB buffer to use with PF_KEY
11874235Smarkfen  * buffer_size: size of buffer
11884235Smarkfen  * spi: spi for this message (set by caller)
11894235Smarkfen  * srcport: source port if specified
11904235Smarkfen  * dstport: destination port if specified
11914235Smarkfen  * proto: IP protocol number if specified
11924235Smarkfen  * iproto: Inner (tunnel mode) IP protocol number if specified
11934235Smarkfen  * NATT note: we are going to assume a semi-sane world where NAT
11944235Smarkfen  * boxen don't explode to multiple addresses.
11954235Smarkfen  */
11964235Smarkfen static void
doaddresses(uint8_t sadb_msg_type,uint8_t sadb_msg_satype,int cmd,struct hostent * srchp,struct hostent * dsthp,struct sadb_address * src,struct sadb_address * dst,boolean_t unspec_src,uint64_t * buffer,int buffer_size,uint32_t spi,char * ebuf)11974235Smarkfen doaddresses(uint8_t sadb_msg_type, uint8_t sadb_msg_satype, int cmd,
11984235Smarkfen     struct hostent *srchp, struct hostent *dsthp,
11994235Smarkfen     struct sadb_address *src, struct sadb_address *dst,
12004235Smarkfen     boolean_t unspec_src, uint64_t *buffer, int buffer_size, uint32_t spi,
12014235Smarkfen     char *ebuf)
12024235Smarkfen {
1203*12655SVladimir.Kotal@Sun.COM 	boolean_t last_dst;
12044235Smarkfen 	struct sockaddr_in6 *sin6;
12054235Smarkfen 	struct sadb_msg *msgp;
12064235Smarkfen 	int i, rc;
12074235Smarkfen 	char **walker;	/* For the SRC and PROXY walking functions. */
12084235Smarkfen 	char *first_match;
12094235Smarkfen 	uint64_t savebuf[MAX_GET_SIZE];
12104235Smarkfen 	uint16_t srcport = 0, dstport = 0;
12114235Smarkfen 	char *ep = NULL;
12124235Smarkfen 
12134235Smarkfen 	/*
12144235Smarkfen 	 * Okay, now we have "src", "dst", and maybe "proxy" reassigned
12154235Smarkfen 	 * to point into the buffer to be written to PF_KEY, we can do
12164235Smarkfen 	 * potentially several writes based on destination address.
12174235Smarkfen 	 *
12184235Smarkfen 	 * First, obtain port numbers from passed-in extensions.
12194235Smarkfen 	 */
12204235Smarkfen 
12214235Smarkfen 	if (src != NULL) {
12224235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
12234235Smarkfen 		srcport = ntohs(sin6->sin6_port);
12244235Smarkfen 	}
12254235Smarkfen 	if (dst != NULL) {
12264235Smarkfen 		sin6 = (struct sockaddr_in6 *)(dst + 1);
12274235Smarkfen 		dstport = ntohs(sin6->sin6_port);
12284235Smarkfen 	}
12294235Smarkfen 
12304235Smarkfen 	/*
12314235Smarkfen 	 * The rules for ADD, GET, and UPDATE: (NOTE:  This assumes IPsec.
12324235Smarkfen 	 * If other consumers of PF_KEY happen, this will have to be
12334235Smarkfen 	 * rewhacked.):
12344235Smarkfen 	 *
12354235Smarkfen 	 *	Do a message for every possible DST address.
12364235Smarkfen 	 *
12374235Smarkfen 	 *	If a source or proxy address explodes, keep unspecified
12384235Smarkfen 	 *	(and mention unspecified).
12394235Smarkfen 	 *
12404235Smarkfen 	 * DELETE is different, because you can leave either "src" or "dst"
12414235Smarkfen 	 * blank!  You need to explode if one of them is full, and not assume
12424235Smarkfen 	 * that the other is set.
12434235Smarkfen 	 */
12444235Smarkfen 
12454235Smarkfen 	if (dsthp == NULL) {
12464235Smarkfen 		/*
12474235Smarkfen 		 * No destination address specified.
12484235Smarkfen 		 * With extended diagnostics, we don't have to bail the
12494235Smarkfen 		 * non-DELETE cases here.  The EINVAL diagnostics will be
12504235Smarkfen 		 * enough to inform the user(s) what happened.
12514235Smarkfen 		 */
12524235Smarkfen 		i = 0;
12534235Smarkfen 		do {
12544235Smarkfen 			if (srchp == &dummy.he) {
12554235Smarkfen 				/* Just to be sure... */
12564235Smarkfen 				srchp->h_addr_list[1] = NULL;
12574235Smarkfen 			} else if (srchp != NULL) {
12584235Smarkfen 				/* Degenerate case, h_addr_list[0] == NULL. */
12594235Smarkfen 				if (srchp->h_addr_list[i] == NULL)
12604235Smarkfen 					Bail("Empty source address list");
12614235Smarkfen 
12624235Smarkfen 				/*
12634235Smarkfen 				 * Fill in the src sockaddr.
12644235Smarkfen 				 */
12654235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
12664235Smarkfen 				bzero(sin6, sizeof (*sin6));
12674235Smarkfen 				bcopy(srchp->h_addr_list[i], &sin6->sin6_addr,
12684235Smarkfen 				    sizeof (struct in6_addr));
12694235Smarkfen 				sin6->sin6_family = AF_INET6;
12704235Smarkfen 				sin6->sin6_port = htons(srcport);
12714235Smarkfen 			}
12724235Smarkfen 
12734235Smarkfen 			/* Save off a copy for later writing... */
12744235Smarkfen 			msgp = (struct sadb_msg *)buffer;
12754235Smarkfen 			bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
12764235Smarkfen 
12774235Smarkfen 			rc = key_write(keysock, buffer,
12784235Smarkfen 			    SADB_64TO8(msgp->sadb_msg_len));
12794235Smarkfen 			if (rc == -1)
12804235Smarkfen 				Bail("write() to PF_KEY socket "
12814235Smarkfen 				    "(in doaddresses)");
12827749SThejaswini.Singarajipura@Sun.COM 			/*
12837749SThejaswini.Singarajipura@Sun.COM 			 * Sends the message to the Solaris Cluster daemon
12847749SThejaswini.Singarajipura@Sun.COM 			 */
12857749SThejaswini.Singarajipura@Sun.COM 
12867749SThejaswini.Singarajipura@Sun.COM 			if (in_cluster_mode) {
12877749SThejaswini.Singarajipura@Sun.COM 				(void) sendto(cluster_socket, buffer,
12887749SThejaswini.Singarajipura@Sun.COM 				    SADB_64TO8(msgp->sadb_msg_len), 0,
12897749SThejaswini.Singarajipura@Sun.COM 				    (struct sockaddr *)&cli_addr,
12907749SThejaswini.Singarajipura@Sun.COM 				    sizeof (cli_addr));
12917749SThejaswini.Singarajipura@Sun.COM 			}
12924235Smarkfen 
12934235Smarkfen 			time_critical_enter();
12944235Smarkfen 			do {
12954235Smarkfen 				rc = read(keysock, buffer, buffer_size);
12964235Smarkfen 				if (rc == -1)
12974235Smarkfen 					Bail("read (in doaddresses)");
12984235Smarkfen 			} while (msgp->sadb_msg_seq != seq ||
12994235Smarkfen 			    msgp->sadb_msg_pid != mypid);
13004235Smarkfen 			time_critical_exit();
13014235Smarkfen 
13024235Smarkfen 			if (msgp->sadb_msg_type != sadb_msg_type ||
13034235Smarkfen 			    msgp->sadb_msg_satype != sadb_msg_satype) {
13044235Smarkfen 				syslog((LOG_NOTICE|LOG_AUTH), gettext(
13054235Smarkfen 				    "doaddresses: Unexpected returned message "
13064235Smarkfen 				    "(%d exp %d)\n"), msgp->sadb_msg_type,
13074235Smarkfen 				    sadb_msg_type);
13084235Smarkfen 				Bail("doaddresses: Unexpected returned "
13094235Smarkfen 				    "message");
13104235Smarkfen 			}
13114235Smarkfen 
13124235Smarkfen 			errno = msgp->sadb_msg_errno;
13134235Smarkfen 			if (errno != 0) {
13144235Smarkfen 				if (errno == EINVAL) {
13154235Smarkfen 					WARN(ep, ebuf, gettext(
13164235Smarkfen 					    "One of the entered "
13174235Smarkfen 					    "values is incorrect."));
13184235Smarkfen 					print_diagnostic(stderr,
13194235Smarkfen 					    msgp->sadb_x_msg_diagnostic);
13204235Smarkfen 				} else {
13214235Smarkfen 					Bail("return message (in doaddresses)");
13224235Smarkfen 				}
13234235Smarkfen 			}
13244235Smarkfen 
13254235Smarkfen 			/* ...and then restore the saved buffer. */
13264235Smarkfen 			msgp = (struct sadb_msg *)savebuf;
13274235Smarkfen 			bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
13284235Smarkfen 		} while (srchp != NULL && srchp->h_addr_list[++i] != NULL);
13294235Smarkfen 		return;
13304235Smarkfen 	}
13314235Smarkfen 
1332*12655SVladimir.Kotal@Sun.COM 	/*
1333*12655SVladimir.Kotal@Sun.COM 	 * Go through the list of all dst addresses, trying to find matching
1334*12655SVladimir.Kotal@Sun.COM 	 * src address for each. If the first address is == dummy.he we will go
1335*12655SVladimir.Kotal@Sun.COM 	 * through the loop just once. If any other hp is == dummy.he, then we
1336*12655SVladimir.Kotal@Sun.COM 	 * don't have to apply any silly rules.
1337*12655SVladimir.Kotal@Sun.COM 	 */
13384235Smarkfen 	for (i = 0; dsthp->h_addr_list[i] != NULL; i++) {
13394235Smarkfen 		if (dsthp == &dummy.he) {
13404235Smarkfen 			/* Just to be sure... */
13414235Smarkfen 			dsthp->h_addr_list[1] = NULL;
13424235Smarkfen 		} else {
13434235Smarkfen 			/*
13444235Smarkfen 			 * Fill in the dst sockaddr.
13454235Smarkfen 			 */
13464235Smarkfen 			sin6 = (struct sockaddr_in6 *)(dst + 1);
13474235Smarkfen 			bzero(sin6, sizeof (*sin6));
13484235Smarkfen 			bcopy(dsthp->h_addr_list[i], &sin6->sin6_addr,
13494235Smarkfen 			    sizeof (struct in6_addr));
13504235Smarkfen 			sin6->sin6_family = AF_INET6;
13514235Smarkfen 			sin6->sin6_port = htons(dstport);
13524235Smarkfen 		}
13534235Smarkfen 
1354*12655SVladimir.Kotal@Sun.COM 		last_dst = (dsthp->h_addr_list[i + 1] == NULL);
1355*12655SVladimir.Kotal@Sun.COM 
13564235Smarkfen 		/*
13574235Smarkfen 		 * Try and assign src, if there's any ambiguity.
13584235Smarkfen 		 */
13594235Smarkfen 		if (!unspec_src && srchp != &dummy.he) {
13604235Smarkfen 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
13614235Smarkfen 				/*
13624235Smarkfen 				 * IPv4 address.  Find an IPv4 address, then
13634235Smarkfen 				 * keep looking for a second one.  If a second
13644235Smarkfen 				 * exists, print a message, and fill in the
13654235Smarkfen 				 * unspecified address.
13664235Smarkfen 				 */
13674235Smarkfen 				first_match = NULL;
13684235Smarkfen 
13694235Smarkfen 				for (walker = srchp->h_addr_list;
13704235Smarkfen 				    *walker != NULL; walker++) {
13714235Smarkfen 					/* LINTED E_BAD_PTR_CAST_ALIGN */
13724235Smarkfen 					if (IN6_IS_ADDR_V4MAPPED(
13734235Smarkfen 					    (struct in6_addr *)*walker)) {
13744235Smarkfen 						if (first_match != NULL)
13754235Smarkfen 							break;
13764235Smarkfen 						else
13774235Smarkfen 							first_match = *walker;
13784235Smarkfen 					}
13794235Smarkfen 				}
13804235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
13814235Smarkfen 				bzero(sin6, sizeof (*sin6));
13824235Smarkfen 
13834235Smarkfen 				if (first_match == NULL) {
13844235Smarkfen 					/*
1385*12655SVladimir.Kotal@Sun.COM 					 * No IPv4 hits. Is this the last
1386*12655SVladimir.Kotal@Sun.COM 					 * destination address in the list ?
13874235Smarkfen 					 */
1388*12655SVladimir.Kotal@Sun.COM 					ERROR1(ep, ebuf, gettext(
13894235Smarkfen 					    "No IPv4 source address "
13904235Smarkfen 					    "for name %s.\n"), srchp->h_name);
1391*12655SVladimir.Kotal@Sun.COM 					if (last_dst) {
1392*12655SVladimir.Kotal@Sun.COM 						FATAL(ep, ebuf, gettext(
1393*12655SVladimir.Kotal@Sun.COM 						    "No match for destination "
13944235Smarkfen 						    "IP address.\n"));
13954235Smarkfen 					} else {
13964235Smarkfen 						/* Continue, but do I print? */
13974235Smarkfen 						continue;  /* for loop */
13984235Smarkfen 					}
13994235Smarkfen 
14004235Smarkfen 					/* I should never reach here. */
14014235Smarkfen 				}
14024235Smarkfen 
14034235Smarkfen 				sin6->sin6_family = AF_INET6;
14044235Smarkfen 				sin6->sin6_port = htons(srcport);
14054235Smarkfen 				if (*walker != NULL) {
14064235Smarkfen 					/*
14074235Smarkfen 					 * Early loop exit.  It must've been
14084235Smarkfen 					 * multiple hits...
14094235Smarkfen 					 *
14104235Smarkfen 					 * Issue a null-source warning?
14114235Smarkfen 					 */
14124235Smarkfen 					WARN1(ep, ebuf, gettext(
14134235Smarkfen 					    "Multiple IPv4 source addresses "
14144235Smarkfen 					    "for %s, using unspecified source "
14154235Smarkfen 					    "instead."), srchp->h_name);
14164235Smarkfen 				} else {
14174235Smarkfen 					/*
14184235Smarkfen 					 * If I reach here w/o hitting the
14194235Smarkfen 					 * previous if statements, I have a
14204235Smarkfen 					 * single source address for this
14214235Smarkfen 					 * destination.
14224235Smarkfen 					 */
14234235Smarkfen 					bcopy(first_match, &sin6->sin6_addr,
14244235Smarkfen 					    sizeof (struct in6_addr));
14254235Smarkfen 				}
14264235Smarkfen 			} else {
14274235Smarkfen 				/*
14284235Smarkfen 				 * IPv6 address.  Find an IPv6 address.
14294235Smarkfen 				 * Unlike IPv4 addresses, things can get a
14304235Smarkfen 				 * little more sticky with scopes, etc.
14314235Smarkfen 				 */
14324235Smarkfen 				int dst_scope, src_scope;
14334235Smarkfen 
14344235Smarkfen 				dst_scope = ipv6_addr_scope(&sin6->sin6_addr);
14354235Smarkfen 
14364235Smarkfen 				first_match = NULL;
14374235Smarkfen 				for (walker = srchp->h_addr_list;
14384235Smarkfen 				    *walker != NULL; walker++) {
14394235Smarkfen 					/* LINTED E_BAD_PTR_CAST_ALIGN */
14404235Smarkfen 					if (!IN6_IS_ADDR_V4MAPPED(
14414235Smarkfen 					    (struct in6_addr *)*walker)) {
14424235Smarkfen 						/*
14434235Smarkfen 						 * Set first-match, etc.
14444235Smarkfen 						 * Take into account scopes,
14454235Smarkfen 						 * and other IPv6 thingies.
14464235Smarkfen 						 */
14474235Smarkfen 						src_scope = ipv6_addr_scope(
14484235Smarkfen 						    /* LINTED E_BAD_PTR_CAST */
14494235Smarkfen 						    (struct in6_addr *)*walker);
14504235Smarkfen 						if (src_scope == SCOPE_UNSPEC ||
14514235Smarkfen 						    src_scope == dst_scope) {
14524235Smarkfen 							if (first_match !=
14534235Smarkfen 							    NULL)
14544235Smarkfen 								break;
14554235Smarkfen 							else
14564235Smarkfen 								first_match =
14574235Smarkfen 								    *walker;
14584235Smarkfen 						}
14594235Smarkfen 					}
14604235Smarkfen 				}
14614235Smarkfen 
14624235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
14634235Smarkfen 				bzero(sin6, sizeof (*sin6));
14644235Smarkfen 				sin6->sin6_port = htons(srcport);
14654235Smarkfen 				if (first_match == NULL) {
14664235Smarkfen 					/*
1467*12655SVladimir.Kotal@Sun.COM 					 * No IPv6 hits. Is this the last
1468*12655SVladimir.Kotal@Sun.COM 					 * destination address in the list ?
14694235Smarkfen 					 */
1470*12655SVladimir.Kotal@Sun.COM 					ERROR1(ep, ebuf, gettext(
14714235Smarkfen 					    "No IPv6 source address of "
14724235Smarkfen 					    "matching scope for name %s.\n"),
14734235Smarkfen 					    srchp->h_name);
1474*12655SVladimir.Kotal@Sun.COM 					if (last_dst) {
1475*12655SVladimir.Kotal@Sun.COM 						FATAL(ep, ebuf, gettext(
1476*12655SVladimir.Kotal@Sun.COM 						    "No match for IPV6 "
14774235Smarkfen 						    "destination "
14784235Smarkfen 						    "address.\n"));
14794235Smarkfen 					} else {
14804235Smarkfen 						/* Continue, but do I print? */
14814235Smarkfen 						continue;  /* for loop */
14824235Smarkfen 					}
14834235Smarkfen 
14844235Smarkfen 					/* I should never reach here. */
14854235Smarkfen 				}
14864235Smarkfen 				sin6->sin6_family = AF_INET6;
14874235Smarkfen 				if (*walker != NULL) {
14884235Smarkfen 					/*
14894235Smarkfen 					 * Early loop exit.  Issue a
14904235Smarkfen 					 * null-source warning?
14914235Smarkfen 					 */
14924235Smarkfen 					WARN1(ep, ebuf, gettext(
14934235Smarkfen 					    "Multiple IPv6 source addresses "
14944235Smarkfen 					    "for %s of the same scope, using "
14954235Smarkfen 					    "unspecified source instead.\n"),
14964235Smarkfen 					    srchp->h_name);
14974235Smarkfen 				} else {
14984235Smarkfen 					/*
14994235Smarkfen 					 * If I reach here w/o hitting the
15004235Smarkfen 					 * previous if statements, I have a
15014235Smarkfen 					 * single source address for this
15024235Smarkfen 					 * destination.
15034235Smarkfen 					 */
15044235Smarkfen 					bcopy(first_match, &sin6->sin6_addr,
15054235Smarkfen 					    sizeof (struct in6_addr));
15064235Smarkfen 				}
15074235Smarkfen 			}
15084235Smarkfen 		}
15094235Smarkfen 
15104235Smarkfen 		/*
15114235Smarkfen 		 * If there are errors at this point there is no
15124235Smarkfen 		 * point sending anything to PF_KEY.
15134235Smarkfen 		 */
15144235Smarkfen 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
15154235Smarkfen 
15164235Smarkfen 		/* Save off a copy for later writing... */
15174235Smarkfen 		msgp = (struct sadb_msg *)buffer;
15184235Smarkfen 		bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
15194235Smarkfen 
15204235Smarkfen 		rc = key_write(keysock, buffer, SADB_64TO8(msgp->sadb_msg_len));
15214235Smarkfen 		if (rc == -1)
15224235Smarkfen 			Bail("write() to PF_KEY socket (in doaddresses)");
15234235Smarkfen 
15247749SThejaswini.Singarajipura@Sun.COM 		if (in_cluster_mode) {
15257749SThejaswini.Singarajipura@Sun.COM 			(void) sendto(cluster_socket, buffer,
15267749SThejaswini.Singarajipura@Sun.COM 			    SADB_64TO8(msgp->sadb_msg_len), 0,
15277749SThejaswini.Singarajipura@Sun.COM 			    (struct sockaddr *)&cli_addr,
15287749SThejaswini.Singarajipura@Sun.COM 			    sizeof (cli_addr));
15297749SThejaswini.Singarajipura@Sun.COM 		}
15304235Smarkfen 		/* Blank the key for paranoia's sake. */
15314235Smarkfen 		bzero(buffer, buffer_size);
15324235Smarkfen 		time_critical_enter();
15334235Smarkfen 		do {
15344235Smarkfen 			rc = read(keysock, buffer, buffer_size);
15354235Smarkfen 			if (rc == -1)
15364235Smarkfen 				Bail("read (in doaddresses)");
15374235Smarkfen 		} while (msgp->sadb_msg_seq != seq ||
15384235Smarkfen 		    msgp->sadb_msg_pid != mypid);
15394235Smarkfen 		time_critical_exit();
15404235Smarkfen 
15414235Smarkfen 		/*
15424235Smarkfen 		 * I should _never_ hit the following unless:
15434235Smarkfen 		 *
15444235Smarkfen 		 * 1. There is a kernel bug.
15454235Smarkfen 		 * 2. Another process is mistakenly using my pid in a PF_KEY
15464235Smarkfen 		 *    message.
15474235Smarkfen 		 */
15484235Smarkfen 		if (msgp->sadb_msg_type != sadb_msg_type ||
15494235Smarkfen 		    msgp->sadb_msg_satype != sadb_msg_satype) {
15504235Smarkfen 			syslog((LOG_NOTICE|LOG_AUTH), gettext(
15514235Smarkfen 			    "doaddresses: Unexpected returned message "
15524235Smarkfen 			    "(%d exp %d)\n"), msgp->sadb_msg_type,
15534235Smarkfen 			    sadb_msg_type);
15544235Smarkfen 			Bail("doaddresses: Unexpected returned message");
15554235Smarkfen 		}
15564235Smarkfen 
15574235Smarkfen 		if (msgp->sadb_msg_errno != 0) {
15584235Smarkfen 			char addrprint[INET6_ADDRSTRLEN];
15594235Smarkfen 			int on_errno = 0;
15604235Smarkfen 			char *on_errno_msg;
15614235Smarkfen 
15624235Smarkfen 			/*
15634235Smarkfen 			 * Print different error messages depending
15644235Smarkfen 			 * on the SADB message type being processed.
15654235Smarkfen 			 * If we get a ESRCH error for a GET/DELETE
15664235Smarkfen 			 * messages, we report that the SA does not
15674235Smarkfen 			 * exist. If we get a EEXIST error for a
15684235Smarkfen 			 * ADD/UPDATE message, we report that the
15694235Smarkfen 			 * SA already exists.
15704235Smarkfen 			 */
15714235Smarkfen 			if (sadb_msg_type == SADB_GET ||
15724235Smarkfen 			    sadb_msg_type == SADB_DELETE) {
15734235Smarkfen 				on_errno = ESRCH;
15744235Smarkfen 				on_errno_msg = "does not exist";
15754235Smarkfen 			} else if (sadb_msg_type == SADB_ADD ||
15764235Smarkfen 			    sadb_msg_type == SADB_UPDATE) {
15774235Smarkfen 				on_errno = EEXIST;
15784235Smarkfen 				on_errno_msg = "already exists";
15794235Smarkfen 			}
15804235Smarkfen 
15814235Smarkfen 			errno = msgp->sadb_msg_errno;
15824235Smarkfen 			if (errno == on_errno) {
15834235Smarkfen 				ERROR2(ep, ebuf, gettext(
15844235Smarkfen 				    "Association (type = %s) "
15854235Smarkfen 				    "with spi 0x%x and addr\n"),
15864235Smarkfen 				    rparsesatype(msgp->sadb_msg_satype),
15874235Smarkfen 				    ntohl(spi));
15884235Smarkfen 				ERROR2(ep, ebuf, "%s %s.\n",
15894235Smarkfen 				    do_inet_ntop(dsthp->h_addr_list[i],
15904342Spwernau 				    addrprint, sizeof (addrprint)),
15914235Smarkfen 				    on_errno_msg);
15924235Smarkfen 				msgp = (struct sadb_msg *)savebuf;
15934235Smarkfen 				bcopy(savebuf, buffer,
15944235Smarkfen 				    SADB_64TO8(msgp->sadb_msg_len));
15954235Smarkfen 				continue;
15964235Smarkfen 			} else {
15976668Smarkfen 				if (errno == EINVAL || errno == ESRCH) {
15984235Smarkfen 					ERROR2(ep, ebuf, gettext(
15994235Smarkfen 					    "PF_KEY Diagnostic code %u: %s.\n"),
16004235Smarkfen 					    msgp->sadb_x_msg_diagnostic,
16014235Smarkfen 					    keysock_diag(
16024235Smarkfen 					    msgp->sadb_x_msg_diagnostic));
16034235Smarkfen 				} else {
16044235Smarkfen 					Bail("return message (in doaddresses)");
16054235Smarkfen 				}
16064235Smarkfen 			}
16074235Smarkfen 		}
16084235Smarkfen 
16094235Smarkfen 		if (cmd == CMD_GET) {
16104235Smarkfen 			if (msgp->sadb_msg_len > MAX_GET_SIZE) {
16114235Smarkfen 				WARN1(ep, ebuf, gettext("WARNING:  "
16124235Smarkfen 				    "SA information bigger than %d bytes.\n"),
16134235Smarkfen 				    SADB_64TO8(MAX_GET_SIZE));
16144235Smarkfen 			}
16154867Spwernau 			print_samsg(stdout, buffer, B_FALSE, vflag, nflag);
16164235Smarkfen 		}
16174235Smarkfen 
16184235Smarkfen 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
16194235Smarkfen 
16204235Smarkfen 		/* ...and then restore the saved buffer. */
16214235Smarkfen 		msgp = (struct sadb_msg *)savebuf;
16224235Smarkfen 		bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
16234235Smarkfen 		lines_added++;
16244235Smarkfen 	}
16254235Smarkfen 
16264235Smarkfen 	/* Degenerate case, h_addr_list[0] == NULL. */
16274235Smarkfen 	if (i == 0)
16284235Smarkfen 		Bail("Empty destination address list");
16294235Smarkfen 
16304235Smarkfen 	/*
16314235Smarkfen 	 * free(ebuf) even if there are no errors.
16324235Smarkfen 	 * handle_errors() won't return here.
16334235Smarkfen 	 */
16344235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_TRUE);
16354235Smarkfen }
16364235Smarkfen 
16374235Smarkfen /*
16384235Smarkfen  * Perform an add or an update.  ADD and UPDATE are similar in the extensions
16394235Smarkfen  * they need.
16404235Smarkfen  */
16414235Smarkfen static void
doaddup(int cmd,int satype,char * argv[],char * ebuf)16424235Smarkfen doaddup(int cmd, int satype, char *argv[], char *ebuf)
16434235Smarkfen {
16444235Smarkfen 	uint64_t *buffer, *nexthdr;
16454235Smarkfen 	struct sadb_msg msg;
16464235Smarkfen 	struct sadb_sa *assoc = NULL;
16476668Smarkfen 	struct sadb_x_pair *sadb_pair = NULL;
16484235Smarkfen 	struct sadb_address *src = NULL, *dst = NULL;
16494235Smarkfen 	struct sadb_address *isrc = NULL, *idst = NULL;
16504235Smarkfen 	struct sadb_address *natt_local = NULL, *natt_remote = NULL;
16514235Smarkfen 	struct sadb_key *encrypt = NULL, *auth = NULL;
16524235Smarkfen 	struct sadb_ident *srcid = NULL, *dstid = NULL;
16534235Smarkfen 	struct sadb_lifetime *hard = NULL, *soft = NULL;  /* Current? */
16547749SThejaswini.Singarajipura@Sun.COM 	struct sadb_lifetime *idle = NULL;
16557749SThejaswini.Singarajipura@Sun.COM 	struct sadb_x_replay_ctr *replay_ctr = NULL;
165610934Ssommerfeld@sun.com 	struct sadb_sens *label = NULL, *olabel = NULL;
16574235Smarkfen 	struct sockaddr_in6 *sin6;
16584235Smarkfen 	/* MLS TODO:  Need sensitivity eventually. */
16594235Smarkfen 	int next, token, sa_len, alloclen, totallen = sizeof (msg), prefix;
16605989Spwernau 	uint32_t spi = 0;
166110824SMark.Fenwick@Sun.COM 	uint_t reserved_bits = 0;
16626668Smarkfen 	uint8_t	sadb_msg_type;
16634235Smarkfen 	char *thiscmd, *pstr;
16644235Smarkfen 	boolean_t readstate = B_FALSE, unspec_src = B_FALSE;
16654235Smarkfen 	boolean_t alloc_inner = B_FALSE, use_natt = B_FALSE;
16664235Smarkfen 	struct hostent *srchp = NULL, *dsthp = NULL, *isrchp = NULL,
16674235Smarkfen 	    *idsthp = NULL;
16684235Smarkfen 	struct hostent *natt_lhp = NULL, *natt_rhp = NULL;
16694235Smarkfen 	uint16_t srcport = 0, dstport = 0, natt_lport = 0, natt_rport = 0,
16704235Smarkfen 	    isrcport = 0, idstport = 0;
16714235Smarkfen 	uint8_t proto = 0, iproto = 0;
16724235Smarkfen 	char *ep = NULL;
16734235Smarkfen 
16746668Smarkfen 	switch (cmd) {
16756668Smarkfen 	case CMD_ADD:
16766668Smarkfen 		thiscmd = "add";
16776668Smarkfen 		sadb_msg_type = SADB_ADD;
16786668Smarkfen 		break;
16796668Smarkfen 	case CMD_UPDATE:
16806668Smarkfen 		thiscmd = "update";
16816668Smarkfen 		sadb_msg_type = SADB_UPDATE;
16826668Smarkfen 		break;
16836668Smarkfen 	case CMD_UPDATE_PAIR:
16846668Smarkfen 		thiscmd = "update-pair";
16856668Smarkfen 		sadb_msg_type = SADB_X_UPDATEPAIR;
16866668Smarkfen 		break;
16876668Smarkfen 	}
16884235Smarkfen 
16896668Smarkfen 	msg_init(&msg, sadb_msg_type, (uint8_t)satype);
16904235Smarkfen 	/* Assume last element in argv is set to NULL. */
16914235Smarkfen 	do {
16924235Smarkfen 		token = parseextval(*argv, &next);
16934235Smarkfen 		argv++;
16944235Smarkfen 		switch (token) {
16954235Smarkfen 		case TOK_EOF:
16964235Smarkfen 			/* Do nothing, I'm done. */
16974235Smarkfen 			break;
16984235Smarkfen 		case TOK_UNKNOWN:
16994235Smarkfen 			ERROR1(ep, ebuf, gettext(
17004235Smarkfen 			    "Unknown extension field \"%s\" \n"), *(argv - 1));
17014235Smarkfen 			break;
17024235Smarkfen 		case TOK_SPI:
17036668Smarkfen 		case TOK_PAIR_SPI:
17044235Smarkfen 		case TOK_REPLAY:
17054235Smarkfen 		case TOK_STATE:
17064235Smarkfen 		case TOK_AUTHALG:
17074235Smarkfen 		case TOK_ENCRALG:
17084235Smarkfen 		case TOK_ENCAP:
17094235Smarkfen 			/*
17104235Smarkfen 			 * May want to place this chunk of code in a function.
17114235Smarkfen 			 *
17124235Smarkfen 			 * This code checks for duplicate entries on a command
17134235Smarkfen 			 * line.
17144235Smarkfen 			 */
17154235Smarkfen 
17164235Smarkfen 			/* Allocate the SADB_EXT_SA extension. */
17174235Smarkfen 			if (assoc == NULL) {
17184235Smarkfen 				assoc = malloc(sizeof (*assoc));
17194235Smarkfen 				if (assoc == NULL)
17204235Smarkfen 					Bail("malloc(assoc)");
17214235Smarkfen 				bzero(assoc, sizeof (*assoc));
17224235Smarkfen 				assoc->sadb_sa_exttype = SADB_EXT_SA;
17234235Smarkfen 				assoc->sadb_sa_len =
17244235Smarkfen 				    SADB_8TO64(sizeof (*assoc));
17254235Smarkfen 				totallen += sizeof (*assoc);
17264235Smarkfen 			}
17274235Smarkfen 			switch (token) {
17284235Smarkfen 			case TOK_SPI:
17294235Smarkfen 				/*
17304235Smarkfen 				 * If some cretin types in "spi 0" then he/she
17314235Smarkfen 				 * can type in another SPI.
17324235Smarkfen 				 */
17334235Smarkfen 				if (assoc->sadb_sa_spi != 0) {
17344235Smarkfen 					ERROR(ep, ebuf, gettext(
17354235Smarkfen 					    "Can only specify "
17364235Smarkfen 					    "single SPI value.\n"));
17374235Smarkfen 					break;
17384235Smarkfen 				}
17394235Smarkfen 				/* Must convert SPI to network order! */
17404235Smarkfen 				assoc->sadb_sa_spi =
17414235Smarkfen 				    htonl((uint32_t)parsenum(*argv, B_TRUE,
17424235Smarkfen 				    ebuf));
17434235Smarkfen 				if (assoc->sadb_sa_spi == 0) {
17444235Smarkfen 					ERROR(ep, ebuf, gettext(
17454235Smarkfen 					    "Invalid SPI value \"0\" .\n"));
17464235Smarkfen 				}
17474235Smarkfen 				break;
17486668Smarkfen 			case TOK_PAIR_SPI:
17496668Smarkfen 				if (cmd == CMD_UPDATE_PAIR) {
17506668Smarkfen 					ERROR(ep, ebuf, gettext(
17516668Smarkfen 					    "pair-spi can not be used with the "
17526668Smarkfen 					    "\"update-pair\" command.\n"));
17536668Smarkfen 				}
17546668Smarkfen 				if (sadb_pair == NULL) {
17556668Smarkfen 					sadb_pair = malloc(sizeof (*sadb_pair));
17566668Smarkfen 					if (assoc == NULL)
17576668Smarkfen 						Bail("malloc(assoc)");
17586668Smarkfen 					bzero(sadb_pair, sizeof (*sadb_pair));
17596668Smarkfen 					totallen += sizeof (*sadb_pair);
17606668Smarkfen 				}
17616668Smarkfen 				if (sadb_pair->sadb_x_pair_spi != 0) {
17626668Smarkfen 					ERROR(ep, ebuf, gettext(
17636668Smarkfen 					    "Can only specify "
17646668Smarkfen 					    "single pair SPI value.\n"));
17656668Smarkfen 					break;
17666668Smarkfen 				}
17676668Smarkfen 				/* Must convert SPI to network order! */
17686668Smarkfen 				sadb_pair->sadb_x_pair_len =
17696668Smarkfen 				    SADB_8TO64(sizeof (*sadb_pair));
17706668Smarkfen 				sadb_pair->sadb_x_pair_exttype =
17716668Smarkfen 				    SADB_X_EXT_PAIR;
17726668Smarkfen 				sadb_pair->sadb_x_pair_spi =
17736668Smarkfen 				    htonl((uint32_t)parsenum(*argv, B_TRUE,
17746668Smarkfen 				    ebuf));
17756668Smarkfen 				if (sadb_pair->sadb_x_pair_spi == 0) {
17766668Smarkfen 					ERROR(ep, ebuf, gettext(
17776668Smarkfen 					    "Invalid SPI value \"0\" .\n"));
17786668Smarkfen 				}
17796668Smarkfen 				assoc->sadb_sa_flags |=
17806668Smarkfen 				    SADB_X_SAFLAGS_PAIRED;
17816668Smarkfen 				break;
17824235Smarkfen 			case TOK_REPLAY:
17834235Smarkfen 				/*
17844235Smarkfen 				 * That same cretin can do the same with
17854235Smarkfen 				 * replay.
17864235Smarkfen 				 */
17874235Smarkfen 				if (assoc->sadb_sa_replay != 0) {
17884235Smarkfen 					ERROR(ep, ebuf, gettext(
17894235Smarkfen 					    "Can only specify "
17904235Smarkfen 					    "single replay window size.\n"));
17914235Smarkfen 					break;
17924235Smarkfen 				}
17934235Smarkfen 				assoc->sadb_sa_replay =
17944235Smarkfen 				    (uint8_t)parsenum(*argv, B_TRUE, ebuf);
17954235Smarkfen 				if (assoc->sadb_sa_replay != 0) {
17964235Smarkfen 					WARN(ep, ebuf, gettext(
17974235Smarkfen 					    "WARNING:  Replay with manual"
17984235Smarkfen 					    " keying considered harmful.\n"));
17994235Smarkfen 				}
18004235Smarkfen 				break;
18014235Smarkfen 			case TOK_STATE:
18024235Smarkfen 				/*
18034235Smarkfen 				 * 0 is an actual state value, LARVAL.  This
18044235Smarkfen 				 * means that one can type in the larval state
18054235Smarkfen 				 * and then type in another state on the same
18064235Smarkfen 				 * command line.
18074235Smarkfen 				 */
18084235Smarkfen 				if (assoc->sadb_sa_state != 0) {
18094235Smarkfen 					ERROR(ep, ebuf, gettext(
18104235Smarkfen 					    "Can only specify "
18114235Smarkfen 					    "single SA state.\n"));
18124235Smarkfen 					break;
18134235Smarkfen 				}
18144235Smarkfen 				assoc->sadb_sa_state = parsestate(*argv,
18154235Smarkfen 				    ebuf);
18164235Smarkfen 				readstate = B_TRUE;
18174235Smarkfen 				break;
18184235Smarkfen 			case TOK_AUTHALG:
18194235Smarkfen 				if (assoc->sadb_sa_auth != 0) {
18204235Smarkfen 					ERROR(ep, ebuf, gettext(
18214235Smarkfen 					    "Can only specify "
18224235Smarkfen 					    "single auth algorithm.\n"));
18234235Smarkfen 					break;
18244235Smarkfen 				}
18254235Smarkfen 				assoc->sadb_sa_auth = parsealg(*argv,
18264235Smarkfen 				    IPSEC_PROTO_AH, ebuf);
18274235Smarkfen 				break;
18284235Smarkfen 			case TOK_ENCRALG:
18294235Smarkfen 				if (satype == SADB_SATYPE_AH) {
18304235Smarkfen 					ERROR(ep, ebuf, gettext("Cannot specify"
18314235Smarkfen 					    " encryption with SA type ah.\n"));
18324235Smarkfen 					break;
18334235Smarkfen 				}
18344235Smarkfen 				if (assoc->sadb_sa_encrypt != 0) {
18354235Smarkfen 					ERROR(ep, ebuf, gettext(
18364235Smarkfen 					    "Can only specify "
18374235Smarkfen 					    "single encryption algorithm.\n"));
18384235Smarkfen 					break;
18394235Smarkfen 				}
18404235Smarkfen 				assoc->sadb_sa_encrypt = parsealg(*argv,
18414235Smarkfen 				    IPSEC_PROTO_ESP, ebuf);
18424235Smarkfen 				break;
18434235Smarkfen 			case TOK_ENCAP:
18444235Smarkfen 				if (use_natt) {
18454235Smarkfen 					ERROR(ep, ebuf, gettext(
18464235Smarkfen 					    "Can only specify single"
18474235Smarkfen 					    " encapsulation.\n"));
18484235Smarkfen 					break;
18494235Smarkfen 				}
18504235Smarkfen 				if (strncmp(*argv, "udp", 3)) {
18514235Smarkfen 					ERROR(ep, ebuf, gettext(
18524235Smarkfen 					    "Can only specify udp"
18534235Smarkfen 					    " encapsulation.\n"));
18544235Smarkfen 					break;
18554235Smarkfen 				}
18564235Smarkfen 				use_natt = B_TRUE;
18574235Smarkfen 				/* set assoc flags later */
18584235Smarkfen 				break;
18594235Smarkfen 			}
18604235Smarkfen 			argv++;
18614235Smarkfen 			break;
18624235Smarkfen 		case TOK_SRCPORT:
18634235Smarkfen 			if (srcport != 0) {
18644235Smarkfen 				ERROR(ep, ebuf,  gettext("Can only specify "
18654235Smarkfen 				    "single source port.\n"));
18664235Smarkfen 				break;
18674235Smarkfen 			}
18684235Smarkfen 			srcport = parsenum(*argv, B_TRUE, ebuf);
18694235Smarkfen 			argv++;
18704235Smarkfen 			break;
18714235Smarkfen 		case TOK_DSTPORT:
18724235Smarkfen 			if (dstport != 0) {
18734235Smarkfen 				ERROR(ep, ebuf, gettext("Can only specify "
18744235Smarkfen 				    "single destination port.\n"));
18754235Smarkfen 				break;
18764235Smarkfen 			}
18774235Smarkfen 			dstport = parsenum(*argv, B_TRUE, ebuf);
18784235Smarkfen 			argv++;
18794235Smarkfen 			break;
18804235Smarkfen 		case TOK_ISRCPORT:
18814235Smarkfen 			alloc_inner = B_TRUE;
18824235Smarkfen 			if (isrcport != 0) {
18834235Smarkfen 				ERROR(ep, ebuf, gettext(
18844235Smarkfen 				    "Can only specify "
18854235Smarkfen 				    "single inner-source port.\n"));
18864235Smarkfen 				break;
18874235Smarkfen 			}
18884235Smarkfen 			isrcport = parsenum(*argv, B_TRUE, ebuf);
18894235Smarkfen 			argv++;
18904235Smarkfen 			break;
18914235Smarkfen 		case TOK_IDSTPORT:
18924235Smarkfen 			alloc_inner = B_TRUE;
18934235Smarkfen 			if (idstport != 0) {
18944235Smarkfen 				ERROR(ep, ebuf, gettext(
18954235Smarkfen 				    "Can only specify "
18964235Smarkfen 				    "single inner-destination port.\n"));
18974235Smarkfen 				break;
18984235Smarkfen 			}
18994235Smarkfen 			idstport = parsenum(*argv, B_TRUE, ebuf);
19004235Smarkfen 			argv++;
19014235Smarkfen 			break;
19024235Smarkfen 		case TOK_NATLPORT:
19034235Smarkfen 			if (natt_lport != 0) {
19044235Smarkfen 				ERROR(ep, ebuf, gettext(
19054235Smarkfen 				    "Can only specify "
19064235Smarkfen 				    "single NAT-T local port.\n"));
19074235Smarkfen 				break;
19084235Smarkfen 			}
19094235Smarkfen 			natt_lport = parsenum(*argv, B_TRUE, ebuf);
19104235Smarkfen 			argv++;
19114235Smarkfen 			break;
19124235Smarkfen 		case TOK_NATRPORT:
19134235Smarkfen 			if (natt_rport != 0) {
19144235Smarkfen 				ERROR(ep, ebuf, gettext(
19154235Smarkfen 				    "Can only specify "
19164235Smarkfen 				    "single NAT-T remote port.\n"));
19174235Smarkfen 				break;
19184235Smarkfen 			}
19194235Smarkfen 			natt_rport = parsenum(*argv, B_TRUE, ebuf);
19204235Smarkfen 			argv++;
19214235Smarkfen 			break;
19224235Smarkfen 
19234235Smarkfen 		case TOK_PROTO:
19244235Smarkfen 			if (proto != 0) {
19254235Smarkfen 				ERROR(ep, ebuf, gettext(
19264235Smarkfen 				    "Can only specify "
19274235Smarkfen 				    "single protocol.\n"));
19284235Smarkfen 				break;
19294235Smarkfen 			}
19304235Smarkfen 			proto = parsenum(*argv, B_TRUE, ebuf);
19314235Smarkfen 			argv++;
19324235Smarkfen 			break;
19334235Smarkfen 		case TOK_IPROTO:
19344235Smarkfen 			alloc_inner = B_TRUE;
19354235Smarkfen 			if (iproto != 0) {
19364235Smarkfen 				ERROR(ep, ebuf, gettext(
19374235Smarkfen 				    "Can only specify "
19384235Smarkfen 				    "single inner protocol.\n"));
19394235Smarkfen 				break;
19404235Smarkfen 			}
19414235Smarkfen 			iproto = parsenum(*argv, B_TRUE, ebuf);
19424235Smarkfen 			argv++;
19434235Smarkfen 			break;
19444235Smarkfen 		case TOK_SRCADDR:
19454235Smarkfen 		case TOK_SRCADDR6:
19464235Smarkfen 			if (src != NULL) {
19474235Smarkfen 				ERROR(ep, ebuf, gettext(
19484235Smarkfen 				    "Can only specify "
19494235Smarkfen 				    "single source address.\n"));
19504235Smarkfen 				break;
19514235Smarkfen 			}
19524235Smarkfen 			sa_len = parseaddr(*argv, &srchp,
19534235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
19544235Smarkfen 			if (srchp == NULL) {
19554235Smarkfen 				ERROR1(ep, ebuf, gettext(
19564235Smarkfen 				    "Unknown src address \"%s\"\n"), *argv);
19574235Smarkfen 				break;
19584235Smarkfen 			}
19594235Smarkfen 			argv++;
19604235Smarkfen 			/*
19614235Smarkfen 			 * Round of the sockaddr length to an 8 byte
19624235Smarkfen 			 * boundary to make PF_KEY happy.
19634235Smarkfen 			 */
19644235Smarkfen 			alloclen = sizeof (*src) + roundup(sa_len, 8);
19654235Smarkfen 			src = malloc(alloclen);
19664235Smarkfen 			if (src == NULL)
19674235Smarkfen 				Bail("malloc(src)");
19684235Smarkfen 			totallen += alloclen;
19694235Smarkfen 			src->sadb_address_len = SADB_8TO64(alloclen);
19704235Smarkfen 			src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
19714235Smarkfen 			src->sadb_address_reserved = 0;
19724235Smarkfen 			src->sadb_address_prefixlen = 0;
19734235Smarkfen 			src->sadb_address_proto = 0;
19744235Smarkfen 			if (srchp == &dummy.he) {
19754235Smarkfen 				/*
19764235Smarkfen 				 * Single address with -n flag.
19774235Smarkfen 				 */
19784235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
19794235Smarkfen 				bzero(sin6, sizeof (*sin6));
19804235Smarkfen 				sin6->sin6_family = AF_INET6;
19814235Smarkfen 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
19824235Smarkfen 				    sizeof (struct in6_addr));
19834235Smarkfen 			}
19844235Smarkfen 			break;
19854235Smarkfen 		case TOK_DSTADDR:
19864235Smarkfen 		case TOK_DSTADDR6:
19874235Smarkfen 			if (dst != NULL) {
19884235Smarkfen 				ERROR(ep, ebuf, gettext(
19894235Smarkfen 				    "Can only specify single "
19904235Smarkfen 				    "destination address.\n"));
19914235Smarkfen 				break;
19924235Smarkfen 			}
19934235Smarkfen 			sa_len = parseaddr(*argv, &dsthp,
19944235Smarkfen 			    (token == TOK_DSTADDR6), ebuf);
19954235Smarkfen 			if (dsthp == NULL) {
19964235Smarkfen 				ERROR1(ep, ebuf, gettext(
19974235Smarkfen 				    "Unknown dst address \"%s\"\n"), *argv);
19984235Smarkfen 				break;
19994235Smarkfen 			}
20004235Smarkfen 			argv++;
20014235Smarkfen 			alloclen = sizeof (*dst) + roundup(sa_len, 8);
20024235Smarkfen 			dst = malloc(alloclen);
20034235Smarkfen 			if (dst == NULL)
20044235Smarkfen 				Bail("malloc(dst)");
20054235Smarkfen 			totallen += alloclen;
20064235Smarkfen 			dst->sadb_address_len = SADB_8TO64(alloclen);
20074235Smarkfen 			dst->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
20084235Smarkfen 			dst->sadb_address_reserved = 0;
20094235Smarkfen 			dst->sadb_address_prefixlen = 0;
20104235Smarkfen 			dst->sadb_address_proto = 0;
20114235Smarkfen 			if (dsthp == &dummy.he) {
20124235Smarkfen 				/*
20134235Smarkfen 				 * Single address with -n flag.
20144235Smarkfen 				 */
20154235Smarkfen 				sin6 = (struct sockaddr_in6 *)(dst + 1);
20164235Smarkfen 				bzero(sin6, sizeof (*sin6));
20174235Smarkfen 				sin6->sin6_family = AF_INET6;
20184235Smarkfen 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
20194235Smarkfen 				    sizeof (struct in6_addr));
20204235Smarkfen 			}
20214235Smarkfen 			break;
20224235Smarkfen 		case TOK_PROXYADDR:
20234235Smarkfen 		case TOK_PROXYADDR6:
20244235Smarkfen 			if (isrc != NULL) {
20254235Smarkfen 				ERROR(ep, ebuf, gettext(
20264235Smarkfen 				    "Can only specify single "
20274235Smarkfen 				    "proxy/inner-source address.\n"));
20284235Smarkfen 				break;
20294235Smarkfen 			}
20304235Smarkfen 			if ((pstr = strchr(*argv, '/')) != NULL) {
20314235Smarkfen 				/* Parse out the prefix. */
20324235Smarkfen 				errno = 0;
20334235Smarkfen 				prefix = strtol(pstr + 1, NULL, 10);
20344235Smarkfen 				if (errno != 0) {
20354235Smarkfen 					ERROR1(ep, ebuf, gettext(
20364235Smarkfen 					    "Invalid prefix %s."), pstr);
20374235Smarkfen 					break;
20384235Smarkfen 				}
20394235Smarkfen 				/* Recycle pstr */
20404235Smarkfen 				alloclen = (int)(pstr - *argv);
20414235Smarkfen 				pstr = malloc(alloclen + 1);
20424235Smarkfen 				if (pstr == NULL) {
20434235Smarkfen 					Bail("malloc(pstr)");
20444235Smarkfen 				}
20454235Smarkfen 				(void) strlcpy(pstr, *argv, alloclen + 1);
20464235Smarkfen 			} else {
20474235Smarkfen 				pstr = *argv;
20484235Smarkfen 				/*
20494235Smarkfen 				 * Assume mapping to AF_INET6, and we're a host.
20504235Smarkfen 				 * XXX some miscreants may still make classful
20514235Smarkfen 				 * assumptions.  If this is a problem, fix it
20524235Smarkfen 				 * here.
20534235Smarkfen 				 */
20544235Smarkfen 				prefix = 128;
20554235Smarkfen 			}
20564235Smarkfen 			sa_len = parseaddr(pstr, &isrchp,
20574235Smarkfen 			    (token == TOK_PROXYADDR6), ebuf);
20584235Smarkfen 			if (isrchp == NULL) {
20594235Smarkfen 				ERROR1(ep, ebuf, gettext(
20604235Smarkfen 				    "Unknown proxy/inner-source address "
20614235Smarkfen 				    "\"%s\"\n"), *argv);
20624235Smarkfen 				break;
20634235Smarkfen 			}
20644235Smarkfen 			if (pstr != *argv)
20654235Smarkfen 				free(pstr);
20664235Smarkfen 			argv++;
20674235Smarkfen 			alloclen = sizeof (*isrc) + roundup(sa_len, 8);
20684235Smarkfen 			isrc = malloc(alloclen);
20694235Smarkfen 			if (isrc == NULL)
20704235Smarkfen 				Bail("malloc(isrc)");
20714235Smarkfen 			totallen += alloclen;
20724235Smarkfen 			isrc->sadb_address_len = SADB_8TO64(alloclen);
20734235Smarkfen 			isrc->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
20744235Smarkfen 			isrc->sadb_address_reserved = 0;
20754235Smarkfen 			isrc->sadb_address_prefixlen = prefix;
20764235Smarkfen 			isrc->sadb_address_proto = 0;
20774235Smarkfen 			if (isrchp == &dummy.he ||
20784235Smarkfen 			    isrchp->h_addr_list[1] == NULL) {
20794235Smarkfen 				/*
20804235Smarkfen 				 * Single address with -n flag or single name.
20814235Smarkfen 				 */
20824235Smarkfen 				sin6 = (struct sockaddr_in6 *)(isrc + 1);
20834235Smarkfen 				bzero(sin6, sizeof (*sin6));
20844235Smarkfen 				sin6->sin6_family = AF_INET6;
20854235Smarkfen 				bcopy(isrchp->h_addr_list[0], &sin6->sin6_addr,
20864235Smarkfen 				    sizeof (struct in6_addr));
20874235Smarkfen 				/*
20884235Smarkfen 				 * normalize prefixlen for IPv4-mapped
20894235Smarkfen 				 * addresses.
20904235Smarkfen 				 */
20914235Smarkfen 				if (prefix <= 32 &&
20924235Smarkfen 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
20934235Smarkfen 					isrc->sadb_address_prefixlen += 96;
20944235Smarkfen 				alloc_inner = B_TRUE;
20954235Smarkfen 			} else {
20964235Smarkfen 				/*
20974235Smarkfen 				 * If the proxy/isrc address is vague, don't
20984235Smarkfen 				 * bother.
20994235Smarkfen 				 */
21004235Smarkfen 				totallen -= alloclen;
21014235Smarkfen 				free(isrc);
21024235Smarkfen 				isrc = NULL;
21034235Smarkfen 				WARN1(ep, ebuf, gettext(
21044235Smarkfen 				    "Proxy/inner-source address %s "
21054235Smarkfen 				    "is vague, not using.\n"), isrchp->h_name);
21064235Smarkfen 				freehostent(isrchp);
21074235Smarkfen 				isrchp = NULL;
21084235Smarkfen 				break;
21094235Smarkfen 			}
21104235Smarkfen 			break;
21114235Smarkfen 		case TOK_IDSTADDR:
21124235Smarkfen 		case TOK_IDSTADDR6:
21134235Smarkfen 			if (idst != NULL) {
21144235Smarkfen 				ERROR(ep, ebuf, gettext(
21154235Smarkfen 				    "Can only specify single "
21164235Smarkfen 				    "inner-destination address.\n"));
21174235Smarkfen 				break;
21184235Smarkfen 			}
21194235Smarkfen 			if ((pstr = strchr(*argv, '/')) != NULL) {
21204235Smarkfen 				/* Parse out the prefix. */
21214235Smarkfen 				errno = 0;
21224235Smarkfen 				prefix = strtol(pstr + 1, NULL, 10);
21234235Smarkfen 				if (errno != 0) {
21244235Smarkfen 					ERROR1(ep, ebuf, gettext(
21254235Smarkfen 					    "Invalid prefix %s.\n"), pstr);
21264235Smarkfen 					break;
21274235Smarkfen 				}
21284235Smarkfen 				/* Recycle pstr */
21294235Smarkfen 				alloclen = (int)(pstr - *argv);
21304235Smarkfen 				pstr = malloc(alloclen + 1);
21314235Smarkfen 				if (pstr == NULL) {
21324235Smarkfen 					Bail("malloc(pstr)");
21334235Smarkfen 				}
21344235Smarkfen 				(void) strlcpy(pstr, *argv, alloclen + 1);
21354235Smarkfen 			} else {
21364235Smarkfen 				pstr = *argv;
21374235Smarkfen 				/*
21384235Smarkfen 				 * Assume mapping to AF_INET6, and we're a host.
21394235Smarkfen 				 * XXX some miscreants may still make classful
21404235Smarkfen 				 * assumptions.  If this is a problem, fix it
21414235Smarkfen 				 * here.
21424235Smarkfen 				 */
21434235Smarkfen 				prefix = 128;
21444235Smarkfen 			}
21454235Smarkfen 			sa_len = parseaddr(pstr, &idsthp,
21464235Smarkfen 			    (token == TOK_IDSTADDR6), ebuf);
21474235Smarkfen 			if (idsthp == NULL) {
21484235Smarkfen 				ERROR1(ep, ebuf, gettext(
21494235Smarkfen 				    "Unknown Inner Src address "
21504235Smarkfen 				    " \"%s\"\n"), *argv);
21514235Smarkfen 				break;
21524235Smarkfen 			}
21534235Smarkfen 			if (pstr != *argv)
21544235Smarkfen 				free(pstr);
21554235Smarkfen 			argv++;
21564235Smarkfen 			alloclen = sizeof (*idst) + roundup(sa_len, 8);
21574235Smarkfen 			idst = malloc(alloclen);
21584235Smarkfen 			if (idst == NULL)
21594235Smarkfen 				Bail("malloc(idst)");
21604235Smarkfen 			totallen += alloclen;
21614235Smarkfen 			idst->sadb_address_len = SADB_8TO64(alloclen);
21624235Smarkfen 			idst->sadb_address_exttype =
21634235Smarkfen 			    SADB_X_EXT_ADDRESS_INNER_DST;
21644235Smarkfen 			idst->sadb_address_reserved = 0;
21654235Smarkfen 			idst->sadb_address_prefixlen = prefix;
21664235Smarkfen 			idst->sadb_address_proto = 0;
21674235Smarkfen 			if (idsthp == &dummy.he ||
21684235Smarkfen 			    idsthp->h_addr_list[1] == NULL) {
21694235Smarkfen 				/*
21704235Smarkfen 				 * Single address with -n flag or single name.
21714235Smarkfen 				 */
21724235Smarkfen 				sin6 = (struct sockaddr_in6 *)(idst + 1);
21734235Smarkfen 				bzero(sin6, sizeof (*sin6));
21744235Smarkfen 				sin6->sin6_family = AF_INET6;
21754235Smarkfen 				bcopy(idsthp->h_addr_list[0], &sin6->sin6_addr,
21764235Smarkfen 				    sizeof (struct in6_addr));
21774235Smarkfen 				/*
21784235Smarkfen 				 * normalize prefixlen for IPv4-mapped
21794235Smarkfen 				 * addresses.
21804235Smarkfen 				 */
21814235Smarkfen 				if (prefix <= 32 &&
21824235Smarkfen 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
21834235Smarkfen 					idst->sadb_address_prefixlen += 96;
21844235Smarkfen 				alloc_inner = B_TRUE;
21854235Smarkfen 			} else {
21864235Smarkfen 				/*
21874235Smarkfen 				 * If the idst address is vague, don't bother.
21884235Smarkfen 				 */
21894235Smarkfen 				totallen -= alloclen;
21904235Smarkfen 				free(idst);
21914235Smarkfen 				idst = NULL;
21924235Smarkfen 				WARN1(ep, ebuf, gettext(
21934235Smarkfen 				    "Inner destination address %s "
21944235Smarkfen 				    "is vague, not using.\n"), idsthp->h_name);
21954235Smarkfen 				freehostent(idsthp);
21964235Smarkfen 				idsthp = NULL;
21974235Smarkfen 				break;
21984235Smarkfen 			}
21994235Smarkfen 			break;
22004235Smarkfen 		case TOK_NATLOC:
22014235Smarkfen 			if (natt_local != NULL) {
22024235Smarkfen 				ERROR(ep, ebuf, gettext(
22034235Smarkfen 				    "Can only specify "
22044235Smarkfen 				    "single NAT-T local address.\n"));
22054235Smarkfen 				break;
22064235Smarkfen 			}
22074235Smarkfen 			sa_len = parseaddr(*argv, &natt_lhp, 0, ebuf);
22084235Smarkfen 			if (natt_lhp == NULL) {
22094235Smarkfen 				ERROR1(ep, ebuf, gettext(
22104235Smarkfen 				    "Unknown NAT-T local address \"%s\"\n"),
22114235Smarkfen 				    *argv);
22124235Smarkfen 				break;
22134235Smarkfen 			}
22144235Smarkfen 			argv++;
22154235Smarkfen 			/*
22164235Smarkfen 			 * Round of the sockaddr length to an 8 byte
22174235Smarkfen 			 * boundary to make PF_KEY happy.
22184235Smarkfen 			 */
22194235Smarkfen 			alloclen = sizeof (*natt_local) + roundup(sa_len, 8);
22204235Smarkfen 			natt_local = malloc(alloclen);
22214235Smarkfen 			if (natt_local == NULL)
22224235Smarkfen 				Bail("malloc(natt_local)");
22234235Smarkfen 			totallen += alloclen;
22244235Smarkfen 			natt_local->sadb_address_len = SADB_8TO64(alloclen);
22254235Smarkfen 			natt_local->sadb_address_exttype =
22264235Smarkfen 			    SADB_X_EXT_ADDRESS_NATT_LOC;
22274235Smarkfen 			natt_local->sadb_address_reserved = 0;
22284235Smarkfen 			natt_local->sadb_address_prefixlen = 0;
22294235Smarkfen 			natt_local->sadb_address_proto = 0;
22304235Smarkfen 			if (natt_lhp == &dummy.he ||
22314235Smarkfen 			    natt_lhp->h_addr_list[1] == NULL) {
22324235Smarkfen 				/*
22334235Smarkfen 				 * Single address with -n flag or single name.
22344235Smarkfen 				 */
22354235Smarkfen 				sin6 = (struct sockaddr_in6 *)(natt_local + 1);
22364235Smarkfen 				bzero(sin6, sizeof (*sin6));
22374235Smarkfen 				sin6->sin6_family = AF_INET6;
22384235Smarkfen 				bcopy(natt_lhp->h_addr_list[0],
22394235Smarkfen 				    &sin6->sin6_addr, sizeof (struct in6_addr));
22404235Smarkfen 			} else {
22414235Smarkfen 				/*
22424235Smarkfen 				 * If the nat-local address is vague, don't
22434235Smarkfen 				 * bother.
22444235Smarkfen 				 */
22454235Smarkfen 				totallen -= alloclen;
22464235Smarkfen 				free(natt_local);
22474235Smarkfen 				natt_local = NULL;
22484235Smarkfen 				WARN1(ep, ebuf, gettext(
22494235Smarkfen 				    "NAT-T local address %s "
22504235Smarkfen 				    "is vague, not using.\n"),
22514235Smarkfen 				    natt_lhp->h_name);
22524235Smarkfen 				freehostent(natt_lhp);
22534235Smarkfen 				natt_lhp = NULL;
22544235Smarkfen 				break;
22554235Smarkfen 			}
22564235Smarkfen 			break;
22574235Smarkfen 		case TOK_NATREM:
22584235Smarkfen 			if (natt_remote != NULL) {
22594235Smarkfen 				ERROR(ep, ebuf, gettext(
22604235Smarkfen 				    "Can only specify "
22614235Smarkfen 				    "single NAT-T remote address.\n"));
22624235Smarkfen 				break;
22634235Smarkfen 			}
22644235Smarkfen 			sa_len = parseaddr(*argv, &natt_rhp, 0, ebuf);
22654235Smarkfen 			if (natt_rhp == NULL) {
22664235Smarkfen 				ERROR1(ep, ebuf, gettext(
22674235Smarkfen 				    "Unknown NAT-T remote address \"%s\"\n"),
22684235Smarkfen 				    *argv);
22694235Smarkfen 				break;
22704235Smarkfen 			}
22714235Smarkfen 			argv++;
22724235Smarkfen 			/*
22734235Smarkfen 			 * Round of the sockaddr length to an 8 byte
22744235Smarkfen 			 * boundary to make PF_KEY happy.
22754235Smarkfen 			 */
22764235Smarkfen 			alloclen = sizeof (*natt_remote) + roundup(sa_len, 8);
22774235Smarkfen 			natt_remote = malloc(alloclen);
22784235Smarkfen 			if (natt_remote == NULL)
22794235Smarkfen 				Bail("malloc(natt_remote)");
22804235Smarkfen 			totallen += alloclen;
22814235Smarkfen 			natt_remote->sadb_address_len = SADB_8TO64(alloclen);
22824235Smarkfen 			natt_remote->sadb_address_exttype =
22834235Smarkfen 			    SADB_X_EXT_ADDRESS_NATT_REM;
22844235Smarkfen 			natt_remote->sadb_address_reserved = 0;
22854235Smarkfen 			natt_remote->sadb_address_prefixlen = 0;
22864235Smarkfen 			natt_remote->sadb_address_proto = 0;
22874235Smarkfen 			if (natt_rhp == &dummy.he ||
22884235Smarkfen 			    natt_rhp->h_addr_list[1] == NULL) {
22894235Smarkfen 				/*
22904235Smarkfen 				 * Single address with -n flag or single name.
22914235Smarkfen 				 */
22924235Smarkfen 				sin6 = (struct sockaddr_in6 *)(natt_remote + 1);
22934235Smarkfen 				bzero(sin6, sizeof (*sin6));
22944235Smarkfen 				sin6->sin6_family = AF_INET6;
22954235Smarkfen 				bcopy(natt_rhp->h_addr_list[0],
22964235Smarkfen 				    &sin6->sin6_addr, sizeof (struct in6_addr));
22974235Smarkfen 			} else {
22984235Smarkfen 				/*
22994235Smarkfen 				 * If the nat-renote address is vague, don't
23004235Smarkfen 				 * bother.
23014235Smarkfen 				 */
23024235Smarkfen 				totallen -= alloclen;
23034235Smarkfen 				free(natt_remote);
23044235Smarkfen 				natt_remote = NULL;
23054235Smarkfen 				WARN1(ep, ebuf, gettext(
23064235Smarkfen 				    "NAT-T remote address %s "
23074235Smarkfen 				    "is vague, not using.\n"),
23084235Smarkfen 				    natt_rhp->h_name);
23094235Smarkfen 				freehostent(natt_rhp);
23104235Smarkfen 				natt_rhp = NULL;
23114235Smarkfen 				break;
23124235Smarkfen 			}
23134235Smarkfen 			break;
23144235Smarkfen 		case TOK_ENCRKEY:
23154235Smarkfen 			if (encrypt != NULL) {
23164235Smarkfen 				ERROR(ep, ebuf, gettext(
23174235Smarkfen 				    "Can only specify "
23184235Smarkfen 				    "single encryption key.\n"));
23194235Smarkfen 				break;
23204235Smarkfen 			}
23215989Spwernau 			if (assoc != NULL &&
23225989Spwernau 			    assoc->sadb_sa_encrypt == SADB_EALG_NULL) {
23234573Spwernau 				FATAL(ep, ebuf, gettext(
23244573Spwernau 				    "Cannot specify a key with NULL "
23254573Spwernau 				    "encryption algorithm.\n"));
23264573Spwernau 				break;
23274573Spwernau 			}
232810824SMark.Fenwick@Sun.COM 			encrypt = parsekey(*argv, ebuf, reserved_bits);
23294235Smarkfen 			argv++;
23304235Smarkfen 			if (encrypt == NULL) {
23314235Smarkfen 				ERROR(ep, ebuf, gettext(
23324235Smarkfen 				    "Invalid encryption key.\n"));
23334235Smarkfen 				break;
23344235Smarkfen 			}
23354235Smarkfen 			totallen += SADB_64TO8(encrypt->sadb_key_len);
23364235Smarkfen 			encrypt->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
23374235Smarkfen 			break;
23384235Smarkfen 		case TOK_AUTHKEY:
23394235Smarkfen 			if (auth != NULL) {
23404235Smarkfen 				ERROR(ep, ebuf, gettext(
23414235Smarkfen 				    "Can only specify single"
23424235Smarkfen 				    " authentication key.\n"));
23434235Smarkfen 				break;
23444235Smarkfen 			}
234510824SMark.Fenwick@Sun.COM 			auth = parsekey(*argv, ebuf, 0);
23464235Smarkfen 			argv++;
23474235Smarkfen 			if (auth == NULL) {
23484235Smarkfen 				ERROR(ep, ebuf, gettext(
23494235Smarkfen 				    "Invalid authentication key.\n"));
23504235Smarkfen 				break;
23514235Smarkfen 			}
23524235Smarkfen 			totallen += SADB_64TO8(auth->sadb_key_len);
23534235Smarkfen 			auth->sadb_key_exttype = SADB_EXT_KEY_AUTH;
23544235Smarkfen 			break;
23554235Smarkfen 		case TOK_SRCIDTYPE:
23564235Smarkfen 			if (*argv == NULL || *(argv + 1) == NULL) {
23574235Smarkfen 				FATAL(ep, ebuf, gettext(
23584235Smarkfen 				    "Unexpected end of command "
23594235Smarkfen 				    "line - Expecting Src Type.\n"));
23604235Smarkfen 				/* NOTREACHED */
23614235Smarkfen 				break;
23624235Smarkfen 			}
23634235Smarkfen 			if (srcid != NULL) {
23644235Smarkfen 				ERROR(ep, ebuf, gettext(
23654235Smarkfen 				    "Can only specify single"
23664235Smarkfen 				    " source certificate identity.\n"));
23674235Smarkfen 				break;
23684235Smarkfen 			}
23694235Smarkfen 			alloclen = sizeof (*srcid) +
23704235Smarkfen 			    roundup(strlen(*(argv + 1)) + 1, 8);
23714235Smarkfen 			srcid = malloc(alloclen);
23724235Smarkfen 			if (srcid == NULL)
23734235Smarkfen 				Bail("malloc(srcid)");
23744235Smarkfen 			totallen += alloclen;
23754235Smarkfen 			srcid->sadb_ident_type = parseidtype(*argv, ebuf);
23764235Smarkfen 			argv++;
23774235Smarkfen 			srcid->sadb_ident_len = SADB_8TO64(alloclen);
23784235Smarkfen 			srcid->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC;
23794235Smarkfen 			srcid->sadb_ident_reserved = 0;
23804235Smarkfen 			srcid->sadb_ident_id = 0;  /* Not useful here. */
23814235Smarkfen 			(void) strlcpy((char *)(srcid + 1), *argv, alloclen);
23824235Smarkfen 			argv++;
23834235Smarkfen 			break;
23844235Smarkfen 		case TOK_DSTIDTYPE:
23854235Smarkfen 			if (*argv == NULL || *(argv + 1) == NULL) {
23864235Smarkfen 				ERROR(ep, ebuf, gettext(
23874235Smarkfen 				    "Unexpected end of command"
23884235Smarkfen 				    " line - expecting dst type.\n"));
23894235Smarkfen 				break;
23904235Smarkfen 			}
23914235Smarkfen 			if (dstid != NULL) {
23924235Smarkfen 				ERROR(ep, ebuf, gettext(
23934235Smarkfen 				    "Can only specify single destination "
23944342Spwernau 				    "certificate identity.\n"));
23954235Smarkfen 				break;
23964235Smarkfen 			}
23974235Smarkfen 			alloclen = sizeof (*dstid) +
23984235Smarkfen 			    roundup(strlen(*(argv + 1)) + 1, 8);
23994235Smarkfen 			dstid = malloc(alloclen);
24004235Smarkfen 			if (dstid == NULL)
24014235Smarkfen 				Bail("malloc(dstid)");
24024235Smarkfen 			totallen += alloclen;
24034235Smarkfen 			dstid->sadb_ident_type = parseidtype(*argv, ebuf);
24044235Smarkfen 			argv++;
24054235Smarkfen 			dstid->sadb_ident_len = SADB_8TO64(alloclen);
24064235Smarkfen 			dstid->sadb_ident_exttype = SADB_EXT_IDENTITY_DST;
24074235Smarkfen 			dstid->sadb_ident_reserved = 0;
24084235Smarkfen 			dstid->sadb_ident_id = 0;  /* Not useful here. */
24094235Smarkfen 			(void) strlcpy((char *)(dstid + 1), *argv, alloclen);
24104235Smarkfen 			argv++;
24114235Smarkfen 			break;
24124235Smarkfen 		case TOK_HARD_ALLOC:
24134235Smarkfen 		case TOK_HARD_BYTES:
24144235Smarkfen 		case TOK_HARD_ADDTIME:
24154235Smarkfen 		case TOK_HARD_USETIME:
24164235Smarkfen 			if (hard == NULL) {
24174235Smarkfen 				hard = malloc(sizeof (*hard));
24184235Smarkfen 				if (hard == NULL)
24194235Smarkfen 					Bail("malloc(hard_lifetime)");
24204235Smarkfen 				bzero(hard, sizeof (*hard));
24214235Smarkfen 				hard->sadb_lifetime_exttype =
24224235Smarkfen 				    SADB_EXT_LIFETIME_HARD;
24234235Smarkfen 				hard->sadb_lifetime_len =
24244235Smarkfen 				    SADB_8TO64(sizeof (*hard));
24254235Smarkfen 				totallen += sizeof (*hard);
24264235Smarkfen 			}
24274235Smarkfen 			switch (token) {
24284235Smarkfen 			case TOK_HARD_ALLOC:
24294235Smarkfen 				if (hard->sadb_lifetime_allocations != 0) {
24304235Smarkfen 					ERROR(ep, ebuf, gettext(
24314235Smarkfen 					    "Can only specify single"
24324235Smarkfen 					    " hard allocation limit.\n"));
24334235Smarkfen 					break;
24344235Smarkfen 				}
24354235Smarkfen 				hard->sadb_lifetime_allocations =
24364235Smarkfen 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
24374235Smarkfen 				break;
24384235Smarkfen 			case TOK_HARD_BYTES:
24394235Smarkfen 				if (hard->sadb_lifetime_bytes != 0) {
24404235Smarkfen 					ERROR(ep, ebuf, gettext(
24414235Smarkfen 					    "Can only specify "
24424235Smarkfen 					    "single hard byte limit.\n"));
24434235Smarkfen 					break;
24444235Smarkfen 				}
24454235Smarkfen 				hard->sadb_lifetime_bytes = parsenum(*argv,
24464235Smarkfen 				    B_TRUE, ebuf);
24474235Smarkfen 				break;
24484235Smarkfen 			case TOK_HARD_ADDTIME:
24494235Smarkfen 				if (hard->sadb_lifetime_addtime != 0) {
24504235Smarkfen 					ERROR(ep, ebuf, gettext(
24514235Smarkfen 					    "Can only specify "
24524235Smarkfen 					    "single past-add lifetime.\n"));
24534235Smarkfen 					break;
24544235Smarkfen 				}
24554235Smarkfen 				hard->sadb_lifetime_addtime = parsenum(*argv,
24564235Smarkfen 				    B_TRUE, ebuf);
24574235Smarkfen 				break;
24584235Smarkfen 			case TOK_HARD_USETIME:
24594235Smarkfen 				if (hard->sadb_lifetime_usetime != 0) {
24604235Smarkfen 					ERROR(ep, ebuf, gettext(
24614235Smarkfen 					    "Can only specify "
24624235Smarkfen 					    "single past-use lifetime.\n"));
24634235Smarkfen 					break;
24644235Smarkfen 				}
24654235Smarkfen 				hard->sadb_lifetime_usetime = parsenum(*argv,
24664235Smarkfen 				    B_TRUE, ebuf);
24674235Smarkfen 				break;
24684235Smarkfen 			}
24694235Smarkfen 			argv++;
24704235Smarkfen 			break;
24714235Smarkfen 		case TOK_SOFT_ALLOC:
24724235Smarkfen 		case TOK_SOFT_BYTES:
24734235Smarkfen 		case TOK_SOFT_ADDTIME:
24744235Smarkfen 		case TOK_SOFT_USETIME:
24754235Smarkfen 			if (soft == NULL) {
24764235Smarkfen 				soft = malloc(sizeof (*soft));
24774235Smarkfen 				if (soft == NULL)
24784235Smarkfen 					Bail("malloc(soft_lifetime)");
24794235Smarkfen 				bzero(soft, sizeof (*soft));
24804235Smarkfen 				soft->sadb_lifetime_exttype =
24814235Smarkfen 				    SADB_EXT_LIFETIME_SOFT;
24824235Smarkfen 				soft->sadb_lifetime_len =
24834235Smarkfen 				    SADB_8TO64(sizeof (*soft));
24844235Smarkfen 				totallen += sizeof (*soft);
24854235Smarkfen 			}
24864235Smarkfen 			switch (token) {
24874235Smarkfen 			case TOK_SOFT_ALLOC:
24884235Smarkfen 				if (soft->sadb_lifetime_allocations != 0) {
24894235Smarkfen 					ERROR(ep, ebuf, gettext(
24904235Smarkfen 					    "Can only specify single"
24914235Smarkfen 					    " soft allocation limit.\n"));
24924235Smarkfen 					break;
24934235Smarkfen 				}
24944235Smarkfen 				soft->sadb_lifetime_allocations =
24954235Smarkfen 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
24964235Smarkfen 				break;
24974235Smarkfen 			case TOK_SOFT_BYTES:
24984235Smarkfen 				if (soft->sadb_lifetime_bytes != 0) {
24994235Smarkfen 					ERROR(ep, ebuf, gettext(
25004235Smarkfen 					    "Can only specify single"
25014235Smarkfen 					    " soft byte limit.\n"));
25024235Smarkfen 					break;
25034235Smarkfen 				}
25044235Smarkfen 				soft->sadb_lifetime_bytes = parsenum(*argv,
25054235Smarkfen 				    B_TRUE, ebuf);
25064235Smarkfen 				break;
25074235Smarkfen 			case TOK_SOFT_ADDTIME:
25084235Smarkfen 				if (soft->sadb_lifetime_addtime != 0) {
25094235Smarkfen 					ERROR(ep, ebuf, gettext(
25104235Smarkfen 					    "Can only specify single"
25114235Smarkfen 					    " past-add lifetime.\n"));
25124235Smarkfen 					break;
25134235Smarkfen 				}
25144235Smarkfen 				soft->sadb_lifetime_addtime = parsenum(*argv,
25154235Smarkfen 				    B_TRUE, ebuf);
25164235Smarkfen 				break;
25174235Smarkfen 			case TOK_SOFT_USETIME:
25184235Smarkfen 				if (soft->sadb_lifetime_usetime != 0) {
25194235Smarkfen 					ERROR(ep, ebuf, gettext(
25204235Smarkfen 					    "Can only specify single"
25214235Smarkfen 					    " past-use lifetime.\n"));
25224235Smarkfen 					break;
25234235Smarkfen 				}
25244235Smarkfen 				soft->sadb_lifetime_usetime = parsenum(*argv,
25254235Smarkfen 				    B_TRUE, ebuf);
25264235Smarkfen 				break;
25274235Smarkfen 			}
25284235Smarkfen 			argv++;
25294235Smarkfen 			break;
25306668Smarkfen 		case TOK_FLAG_INBOUND:
25316668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_INBOUND;
25326668Smarkfen 			break;
25336668Smarkfen 		case TOK_FLAG_OUTBOUND:
25346668Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_OUTBOUND;
25356668Smarkfen 			break;
25367749SThejaswini.Singarajipura@Sun.COM 		case TOK_REPLAY_VALUE:
25377749SThejaswini.Singarajipura@Sun.COM 			if (replay_ctr != NULL) {
25387749SThejaswini.Singarajipura@Sun.COM 				ERROR(ep, ebuf, gettext(
25397749SThejaswini.Singarajipura@Sun.COM 				    "Can only specify single "
25407749SThejaswini.Singarajipura@Sun.COM 				    "replay value."));
25417749SThejaswini.Singarajipura@Sun.COM 				break;
25427749SThejaswini.Singarajipura@Sun.COM 			}
25437749SThejaswini.Singarajipura@Sun.COM 			replay_ctr = calloc(1, sizeof (*replay_ctr));
25447749SThejaswini.Singarajipura@Sun.COM 			if (replay_ctr == NULL) {
25457749SThejaswini.Singarajipura@Sun.COM 				Bail("malloc(replay value)");
25467749SThejaswini.Singarajipura@Sun.COM 			}
25477749SThejaswini.Singarajipura@Sun.COM 			/*
25487749SThejaswini.Singarajipura@Sun.COM 			 * We currently do not support a 64-bit
25497749SThejaswini.Singarajipura@Sun.COM 			 * replay value.  RFC 4301 will require one,
25507749SThejaswini.Singarajipura@Sun.COM 			 * however, and we have a field in place when
25517749SThejaswini.Singarajipura@Sun.COM 			 * 4301 is built.
25527749SThejaswini.Singarajipura@Sun.COM 			 */
25537749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_exttype = SADB_X_EXT_REPLAY_VALUE;
25547749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_len =
25557749SThejaswini.Singarajipura@Sun.COM 			    SADB_8TO64(sizeof (*replay_ctr));
25567749SThejaswini.Singarajipura@Sun.COM 			totallen += sizeof (*replay_ctr);
25577749SThejaswini.Singarajipura@Sun.COM 			replay_ctr->sadb_x_rc_replay32 = (uint32_t)parsenum(
25587749SThejaswini.Singarajipura@Sun.COM 			    *argv, B_TRUE, ebuf);
25597749SThejaswini.Singarajipura@Sun.COM 			argv++;
25607749SThejaswini.Singarajipura@Sun.COM 			break;
25617749SThejaswini.Singarajipura@Sun.COM 		case TOK_IDLE_ADDTIME:
25627749SThejaswini.Singarajipura@Sun.COM 		case TOK_IDLE_USETIME:
25637749SThejaswini.Singarajipura@Sun.COM 			if (idle == NULL) {
25647749SThejaswini.Singarajipura@Sun.COM 				idle = calloc(1, sizeof (*idle));
25657749SThejaswini.Singarajipura@Sun.COM 				if (idle == NULL) {
25667749SThejaswini.Singarajipura@Sun.COM 					Bail("malloc idle lifetime");
25677749SThejaswini.Singarajipura@Sun.COM 				}
25687749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_exttype =
25697749SThejaswini.Singarajipura@Sun.COM 				    SADB_X_EXT_LIFETIME_IDLE;
25707749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_len =
25717749SThejaswini.Singarajipura@Sun.COM 				    SADB_8TO64(sizeof (*idle));
25727749SThejaswini.Singarajipura@Sun.COM 				totallen += sizeof (*idle);
25737749SThejaswini.Singarajipura@Sun.COM 			}
25747749SThejaswini.Singarajipura@Sun.COM 			switch (token) {
25757749SThejaswini.Singarajipura@Sun.COM 			case TOK_IDLE_ADDTIME:
25767749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_addtime =
25777749SThejaswini.Singarajipura@Sun.COM 				    (uint32_t)parsenum(*argv,
25787749SThejaswini.Singarajipura@Sun.COM 				    B_TRUE, ebuf);
25797749SThejaswini.Singarajipura@Sun.COM 				break;
25807749SThejaswini.Singarajipura@Sun.COM 			case TOK_IDLE_USETIME:
25817749SThejaswini.Singarajipura@Sun.COM 				idle->sadb_lifetime_usetime =
25827749SThejaswini.Singarajipura@Sun.COM 				    (uint32_t)parsenum(*argv,
25837749SThejaswini.Singarajipura@Sun.COM 				    B_TRUE, ebuf);
25847749SThejaswini.Singarajipura@Sun.COM 				break;
25857749SThejaswini.Singarajipura@Sun.COM 			}
25867749SThejaswini.Singarajipura@Sun.COM 			argv++;
25877749SThejaswini.Singarajipura@Sun.COM 			break;
258810824SMark.Fenwick@Sun.COM 		case TOK_RESERVED:
258910824SMark.Fenwick@Sun.COM 			if (encrypt != NULL)
259010824SMark.Fenwick@Sun.COM 				ERROR(ep, ebuf, gettext(
259110824SMark.Fenwick@Sun.COM 				    "Reserved bits need to be "
259210824SMark.Fenwick@Sun.COM 				    "specified before key.\n"));
259310824SMark.Fenwick@Sun.COM 			reserved_bits = (uint_t)parsenum(*argv,
259410824SMark.Fenwick@Sun.COM 			    B_TRUE, ebuf);
259510824SMark.Fenwick@Sun.COM 			argv++;
259610824SMark.Fenwick@Sun.COM 			break;
259710934Ssommerfeld@sun.com 		case TOK_LABEL:
259810934Ssommerfeld@sun.com 			label = parselabel(token, *argv);
259910934Ssommerfeld@sun.com 			argv++;
260010934Ssommerfeld@sun.com 			if (label == NULL) {
260110934Ssommerfeld@sun.com 				ERROR(ep, ebuf,
260210934Ssommerfeld@sun.com 				    gettext("Malformed security label\n"));
260310934Ssommerfeld@sun.com 				break;
260410934Ssommerfeld@sun.com 			} else if (label == PARSELABEL_BAD_TOKEN) {
260510934Ssommerfeld@sun.com 				Bail("Internal token value error");
260610934Ssommerfeld@sun.com 			}
260710934Ssommerfeld@sun.com 			totallen += SADB_64TO8(label->sadb_sens_len);
260810934Ssommerfeld@sun.com 			break;
260910934Ssommerfeld@sun.com 
261010934Ssommerfeld@sun.com 		case TOK_OLABEL:
261110934Ssommerfeld@sun.com 		case TOK_IMPLABEL:
261210934Ssommerfeld@sun.com 			olabel = parselabel(token, *argv);
261310934Ssommerfeld@sun.com 			argv++;
261410934Ssommerfeld@sun.com 			if (label == NULL) {
261510934Ssommerfeld@sun.com 				ERROR(ep, ebuf,
261610934Ssommerfeld@sun.com 				    gettext("Malformed security label\n"));
261710934Ssommerfeld@sun.com 				break;
261810934Ssommerfeld@sun.com 			} else if (label == PARSELABEL_BAD_TOKEN) {
261910934Ssommerfeld@sun.com 				Bail("Internal token value error");
262010934Ssommerfeld@sun.com 			}
262110934Ssommerfeld@sun.com 			totallen += SADB_64TO8(olabel->sadb_sens_len);
262210934Ssommerfeld@sun.com 			break;
26234235Smarkfen 		default:
26244235Smarkfen 			ERROR1(ep, ebuf, gettext(
26254235Smarkfen 			    "Don't use extension %s for add/update.\n"),
26264235Smarkfen 			    *(argv - 1));
26274235Smarkfen 			break;
26284235Smarkfen 		}
26294235Smarkfen 	} while (token != TOK_EOF);
26304235Smarkfen 
26314235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
26324235Smarkfen 
26334987Sdanmcd #define	PORT_ONLY_ALLOCATE(af, socktype, exttype, extvar, port) {  \
26344987Sdanmcd 	alloclen = sizeof (sadb_address_t) + roundup(sizeof (socktype), 8); \
26354987Sdanmcd 	(extvar) = calloc(1, alloclen); \
26364987Sdanmcd 	if ((extvar) == NULL) { \
26374987Sdanmcd 		Bail("malloc(implicit port)"); \
26384987Sdanmcd 	} \
26394987Sdanmcd 	totallen += alloclen; \
26404987Sdanmcd 	(extvar)->sadb_address_len = SADB_8TO64(alloclen); \
26414987Sdanmcd 	(extvar)->sadb_address_exttype = (exttype); \
26424987Sdanmcd 	/* sin/sin6 has equivalent offsets for ports! */ \
26434987Sdanmcd 	sin6 = (struct sockaddr_in6 *)((extvar) + 1); \
26444987Sdanmcd 	sin6->sin6_family = (af); \
26454987Sdanmcd 	sin6->sin6_port = (port); \
26464987Sdanmcd 	}
26474987Sdanmcd 
26484235Smarkfen 	/*
26494987Sdanmcd 	 * If we specify inner ports or NAT ports w/o addresses, we still need
26504987Sdanmcd 	 * to allocate.  Also, if we have one inner address, we need the
26514235Smarkfen 	 * other, even if we don't specify anything.
26524235Smarkfen 	 */
26534987Sdanmcd 	if (use_natt) {
26544987Sdanmcd 		if (natt_lport != 0 && natt_local == NULL) {
26554987Sdanmcd 			PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in,
26564987Sdanmcd 			    SADB_X_EXT_ADDRESS_NATT_LOC, natt_local,
26574987Sdanmcd 			    natt_lport);
26584987Sdanmcd 		}
26594987Sdanmcd 
26604987Sdanmcd 		if (natt_rport != 0 && natt_remote == NULL) {
26614987Sdanmcd 			PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in,
26624987Sdanmcd 			    SADB_X_EXT_ADDRESS_NATT_REM, natt_remote,
26634987Sdanmcd 			    natt_rport);
26644987Sdanmcd 		}
26654987Sdanmcd 	} else {
26664987Sdanmcd 		if (natt_lport != 0 || natt_rport != 0) {
26674987Sdanmcd 			ERROR(ep, ebuf, gettext("Must specify 'encap udp' "
26684987Sdanmcd 			    "with any NAT-T port.\n"));
26694987Sdanmcd 		} else if (natt_local != NULL || natt_remote != NULL) {
26704987Sdanmcd 			ERROR(ep, ebuf, gettext("Must specify 'encap udp' "
26714987Sdanmcd 			    "with any NAT-T address.\n"));
26724987Sdanmcd 		}
26734987Sdanmcd 	}
26744987Sdanmcd 
26754235Smarkfen 	if (alloc_inner && idst == NULL) {
26764987Sdanmcd 		PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6,
26774987Sdanmcd 		    SADB_X_EXT_ADDRESS_INNER_DST, idst, 0);
26784235Smarkfen 	}
26794235Smarkfen 
26804235Smarkfen 	if (alloc_inner && isrc == NULL) {
26814987Sdanmcd 		PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6,
26824987Sdanmcd 		    SADB_X_EXT_ADDRESS_INNER_SRC, isrc, 0);
26834235Smarkfen 	}
26844987Sdanmcd #undef PORT_ONLY_ALLOCATE
26854235Smarkfen 
26864235Smarkfen 	/*
26874235Smarkfen 	 * Okay, so now I have all of the potential extensions!
26884235Smarkfen 	 * Allocate a single contiguous buffer.  Keep in mind that it'll
26894235Smarkfen 	 * be enough because the key itself will be yanked.
26904235Smarkfen 	 */
26914235Smarkfen 
26924235Smarkfen 	if (src == NULL && dst != NULL) {
26934235Smarkfen 		/*
26944235Smarkfen 		 * Set explicit unspecified source address.
26954235Smarkfen 		 */
26964235Smarkfen 		size_t lenbytes = SADB_64TO8(dst->sadb_address_len);
26974235Smarkfen 
26984235Smarkfen 		unspec_src = B_TRUE;
26994235Smarkfen 		totallen += lenbytes;
27004235Smarkfen 		src = malloc(lenbytes);
27014235Smarkfen 		if (src == NULL)
27024235Smarkfen 			Bail("malloc(implicit src)");
27034235Smarkfen 		/* Confusing, but we're copying from DST to SRC.  :) */
27044235Smarkfen 		bcopy(dst, src, lenbytes);
27054235Smarkfen 		src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
27064235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
27074235Smarkfen 		bzero(sin6, sizeof (*sin6));
27084235Smarkfen 		sin6->sin6_family = AF_INET6;
27094235Smarkfen 	}
27104235Smarkfen 
27114235Smarkfen 	msg.sadb_msg_len = SADB_8TO64(totallen);
27124235Smarkfen 
27134235Smarkfen 	buffer = malloc(totallen);
27144235Smarkfen 	nexthdr = buffer;
27154235Smarkfen 	bcopy(&msg, nexthdr, sizeof (msg));
27164235Smarkfen 	nexthdr += SADB_8TO64(sizeof (msg));
27174235Smarkfen 	if (assoc != NULL) {
27184235Smarkfen 		if (assoc->sadb_sa_spi == 0) {
27194235Smarkfen 			ERROR1(ep, ebuf, gettext(
27204235Smarkfen 			    "The SPI value is missing for "
27214235Smarkfen 			    "the association you wish to %s.\n"), thiscmd);
27224235Smarkfen 		}
27234235Smarkfen 		if (assoc->sadb_sa_auth == 0 && assoc->sadb_sa_encrypt == 0 &&
27244342Spwernau 		    cmd == CMD_ADD) {
27254235Smarkfen 			free(assoc);
27264235Smarkfen 			FATAL(ep, ebuf, gettext(
27274235Smarkfen 			    "Select at least one algorithm "
27284235Smarkfen 			    "for this add.\n"));
27294235Smarkfen 		}
27304235Smarkfen 
27314235Smarkfen 		/* Hack to let user specify NULL ESP implicitly. */
27324235Smarkfen 		if (msg.sadb_msg_satype == SADB_SATYPE_ESP &&
27334235Smarkfen 		    assoc->sadb_sa_encrypt == 0)
27344235Smarkfen 			assoc->sadb_sa_encrypt = SADB_EALG_NULL;
27354235Smarkfen 
27364235Smarkfen 		/* 0 is an actual value.  Print a warning if it was entered. */
27374235Smarkfen 		if (assoc->sadb_sa_state == 0) {
27384235Smarkfen 			if (readstate) {
27394235Smarkfen 				ERROR(ep, ebuf, gettext(
27404235Smarkfen 				    "WARNING: Cannot set LARVAL SA state.\n"));
27414235Smarkfen 			}
27424235Smarkfen 			assoc->sadb_sa_state = SADB_SASTATE_MATURE;
27434235Smarkfen 		}
27444235Smarkfen 
27454235Smarkfen 		if (use_natt) {
27464235Smarkfen 			if (natt_remote != NULL)
27474235Smarkfen 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_REM;
27484235Smarkfen 			if (natt_local != NULL)
27494235Smarkfen 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_LOC;
27504235Smarkfen 		}
27514235Smarkfen 
27524235Smarkfen 		if (alloc_inner) {
27534235Smarkfen 			/*
27544235Smarkfen 			 * For now, assume RFC 3884's dream of transport-mode
27554235Smarkfen 			 * SAs with inner IP address selectors will not
27564235Smarkfen 			 * happen.
27574235Smarkfen 			 */
27584235Smarkfen 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL;
27594235Smarkfen 			if (proto != 0 && proto != IPPROTO_ENCAP &&
27604235Smarkfen 			    proto != IPPROTO_IPV6) {
27614235Smarkfen 				ERROR1(ep, ebuf, gettext(
27624235Smarkfen 				    "WARNING: Protocol type %d not "
27634235Smarkfen 				    "for use with Tunnel-Mode SA.\n"), proto);
27644235Smarkfen 				/* Continue and let PF_KEY scream... */
27654235Smarkfen 			}
27664235Smarkfen 		}
27674235Smarkfen 
27684235Smarkfen 		bcopy(assoc, nexthdr, SADB_64TO8(assoc->sadb_sa_len));
27694235Smarkfen 		nexthdr += assoc->sadb_sa_len;
27704235Smarkfen 		/* Save the SPI for the case of an error. */
27714235Smarkfen 		spi = assoc->sadb_sa_spi;
27724235Smarkfen 		free(assoc);
27734235Smarkfen 	} else {
27745989Spwernau 		if (spi == 0)
27755989Spwernau 			ERROR1(ep, ebuf, gettext(
27765989Spwernau 			    "Need to define SPI for %s.\n"), thiscmd);
27774235Smarkfen 		ERROR1(ep, ebuf, gettext(
27784235Smarkfen 		    "Need SA parameters for %s.\n"), thiscmd);
27794235Smarkfen 	}
27804235Smarkfen 
27816668Smarkfen 	if (sadb_pair != NULL) {
27826668Smarkfen 		if (sadb_pair->sadb_x_pair_spi == 0) {
27836668Smarkfen 			ERROR1(ep, ebuf, gettext(
27846668Smarkfen 			    "The SPI value is missing for the "
27856668Smarkfen 			    "association you wish to %s.\n"), thiscmd);
27866668Smarkfen 		}
27876668Smarkfen 		bcopy(sadb_pair, nexthdr,
27886668Smarkfen 		    SADB_64TO8(sadb_pair->sadb_x_pair_len));
27896668Smarkfen 		nexthdr += sadb_pair->sadb_x_pair_len;
27906668Smarkfen 		free(sadb_pair);
27916668Smarkfen 	}
27926668Smarkfen 
27934235Smarkfen 	if (hard != NULL) {
27944235Smarkfen 		bcopy(hard, nexthdr, SADB_64TO8(hard->sadb_lifetime_len));
27954235Smarkfen 		nexthdr += hard->sadb_lifetime_len;
27964235Smarkfen 		free(hard);
27974235Smarkfen 	}
27984235Smarkfen 
27994235Smarkfen 	if (soft != NULL) {
28004235Smarkfen 		bcopy(soft, nexthdr, SADB_64TO8(soft->sadb_lifetime_len));
28014235Smarkfen 		nexthdr += soft->sadb_lifetime_len;
28024235Smarkfen 		free(soft);
28034235Smarkfen 	}
28044235Smarkfen 
28057749SThejaswini.Singarajipura@Sun.COM 	if (idle != NULL) {
28067749SThejaswini.Singarajipura@Sun.COM 		bcopy(idle, nexthdr, SADB_64TO8(idle->sadb_lifetime_len));
28077749SThejaswini.Singarajipura@Sun.COM 		nexthdr += idle->sadb_lifetime_len;
28087749SThejaswini.Singarajipura@Sun.COM 		free(idle);
28097749SThejaswini.Singarajipura@Sun.COM 	}
28107749SThejaswini.Singarajipura@Sun.COM 
28114235Smarkfen 	if (encrypt == NULL && auth == NULL && cmd == CMD_ADD) {
28124235Smarkfen 		ERROR(ep, ebuf, gettext(
28134235Smarkfen 		    "Must have at least one key for an add.\n"));
28144235Smarkfen 	}
28154235Smarkfen 
28164235Smarkfen 	if (encrypt != NULL) {
28174235Smarkfen 		bcopy(encrypt, nexthdr, SADB_64TO8(encrypt->sadb_key_len));
28184235Smarkfen 		nexthdr += encrypt->sadb_key_len;
28194235Smarkfen 		bzero(encrypt, SADB_64TO8(encrypt->sadb_key_len));
28204235Smarkfen 		free(encrypt);
28214235Smarkfen 	}
28224235Smarkfen 
28234235Smarkfen 	if (auth != NULL) {
28244235Smarkfen 		bcopy(auth, nexthdr, SADB_64TO8(auth->sadb_key_len));
28254235Smarkfen 		nexthdr += auth->sadb_key_len;
28264235Smarkfen 		bzero(auth, SADB_64TO8(auth->sadb_key_len));
28274235Smarkfen 		free(auth);
28284235Smarkfen 	}
28294235Smarkfen 
28304235Smarkfen 	if (srcid != NULL) {
28314235Smarkfen 		bcopy(srcid, nexthdr, SADB_64TO8(srcid->sadb_ident_len));
28324235Smarkfen 		nexthdr += srcid->sadb_ident_len;
28334235Smarkfen 		free(srcid);
28344235Smarkfen 	}
28354235Smarkfen 
28364235Smarkfen 	if (dstid != NULL) {
28374235Smarkfen 		bcopy(dstid, nexthdr, SADB_64TO8(dstid->sadb_ident_len));
28384235Smarkfen 		nexthdr += dstid->sadb_ident_len;
28394235Smarkfen 		free(dstid);
28404235Smarkfen 	}
28414235Smarkfen 
28424235Smarkfen 	if (dst != NULL) {
28434235Smarkfen 		bcopy(dst, nexthdr, SADB_64TO8(dst->sadb_address_len));
28444235Smarkfen 		free(dst);
28454235Smarkfen 		dst = (struct sadb_address *)nexthdr;
28464235Smarkfen 		dst->sadb_address_proto = proto;
28474235Smarkfen 		((struct sockaddr_in6 *)(dst + 1))->sin6_port = htons(dstport);
28484235Smarkfen 		nexthdr += dst->sadb_address_len;
28494235Smarkfen 	} else {
28504235Smarkfen 		FATAL1(ep, ebuf, gettext(
28514235Smarkfen 		    "Need destination address for %s.\n"), thiscmd);
28524235Smarkfen 	}
28534235Smarkfen 
28544235Smarkfen 	if (use_natt) {
28554235Smarkfen 		if (natt_remote == NULL && natt_local == NULL) {
28564235Smarkfen 			ERROR(ep, ebuf, gettext(
28574235Smarkfen 			    "Must specify NAT-T remote or local address "
28584235Smarkfen 			    "for UDP encapsulation.\n"));
28594235Smarkfen 		}
28604235Smarkfen 
28614235Smarkfen 		if (natt_remote != NULL) {
28624235Smarkfen 			bcopy(natt_remote, nexthdr,
28634235Smarkfen 			    SADB_64TO8(natt_remote->sadb_address_len));
28644235Smarkfen 			free(natt_remote);
28654235Smarkfen 			natt_remote = (struct sadb_address *)nexthdr;
28664235Smarkfen 			nexthdr += natt_remote->sadb_address_len;
28674235Smarkfen 			((struct sockaddr_in6 *)(natt_remote + 1))->sin6_port =
28684235Smarkfen 			    htons(natt_rport);
28694235Smarkfen 		}
28704235Smarkfen 
28714235Smarkfen 		if (natt_local != NULL) {
28724235Smarkfen 			bcopy(natt_local, nexthdr,
28734235Smarkfen 			    SADB_64TO8(natt_local->sadb_address_len));
28744235Smarkfen 			free(natt_local);
28754235Smarkfen 			natt_local = (struct sadb_address *)nexthdr;
28764235Smarkfen 			nexthdr += natt_local->sadb_address_len;
28774235Smarkfen 			((struct sockaddr_in6 *)(natt_local + 1))->sin6_port =
28784235Smarkfen 			    htons(natt_lport);
28794235Smarkfen 		}
28804235Smarkfen 	}
28814235Smarkfen 
28824235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
28834235Smarkfen 
28844235Smarkfen 	/*
28854235Smarkfen 	 * PF_KEY requires a source address extension, even if the source
28864235Smarkfen 	 * address itself is unspecified. (See "Set explicit unspecified..."
28874235Smarkfen 	 * code fragment above. Destination reality check was above.)
28884235Smarkfen 	 */
28894235Smarkfen 	bcopy(src, nexthdr, SADB_64TO8(src->sadb_address_len));
28904235Smarkfen 	free(src);
28914235Smarkfen 	src = (struct sadb_address *)nexthdr;
28924235Smarkfen 	src->sadb_address_proto = proto;
28934235Smarkfen 	((struct sockaddr_in6 *)(src + 1))->sin6_port = htons(srcport);
28944235Smarkfen 	nexthdr += src->sadb_address_len;
28954235Smarkfen 
28964235Smarkfen 	if (isrc != NULL) {
28974235Smarkfen 		bcopy(isrc, nexthdr, SADB_64TO8(isrc->sadb_address_len));
28984235Smarkfen 		free(isrc);
28994235Smarkfen 		isrc = (struct sadb_address *)nexthdr;
29004235Smarkfen 		isrc->sadb_address_proto = iproto;
29014235Smarkfen 		((struct sockaddr_in6 *)(isrc + 1))->sin6_port =
29024235Smarkfen 		    htons(isrcport);
29034235Smarkfen 		nexthdr += isrc->sadb_address_len;
29044235Smarkfen 	}
29054235Smarkfen 
29064235Smarkfen 	if (idst != NULL) {
29074235Smarkfen 		bcopy(idst, nexthdr, SADB_64TO8(idst->sadb_address_len));
29084235Smarkfen 		free(idst);
29094235Smarkfen 		idst = (struct sadb_address *)nexthdr;
29104235Smarkfen 		idst->sadb_address_proto = iproto;
29114235Smarkfen 		((struct sockaddr_in6 *)(idst + 1))->sin6_port =
29124235Smarkfen 		    htons(idstport);
29134235Smarkfen 		nexthdr += idst->sadb_address_len;
29144235Smarkfen 	}
29154235Smarkfen 
29167749SThejaswini.Singarajipura@Sun.COM 	if (replay_ctr != NULL) {
29177749SThejaswini.Singarajipura@Sun.COM 		bcopy(replay_ctr, nexthdr,
29187749SThejaswini.Singarajipura@Sun.COM 		    SADB_64TO8(replay_ctr->sadb_x_rc_len));
29197749SThejaswini.Singarajipura@Sun.COM 		nexthdr += replay_ctr->sadb_x_rc_len;
29207749SThejaswini.Singarajipura@Sun.COM 		free(replay_ctr);
29217749SThejaswini.Singarajipura@Sun.COM 	}
29227749SThejaswini.Singarajipura@Sun.COM 
292310934Ssommerfeld@sun.com 	if (label != NULL) {
292410934Ssommerfeld@sun.com 		bcopy(label, nexthdr, SADB_64TO8(label->sadb_sens_len));
292510934Ssommerfeld@sun.com 		nexthdr += label->sadb_sens_len;
292610934Ssommerfeld@sun.com 		free(label);
292710934Ssommerfeld@sun.com 		label = NULL;
292810934Ssommerfeld@sun.com 	}
292910934Ssommerfeld@sun.com 
293010934Ssommerfeld@sun.com 	if (olabel != NULL) {
293110934Ssommerfeld@sun.com 		bcopy(olabel, nexthdr, SADB_64TO8(olabel->sadb_sens_len));
293210934Ssommerfeld@sun.com 		nexthdr += olabel->sadb_sens_len;
293310934Ssommerfeld@sun.com 		free(olabel);
293410934Ssommerfeld@sun.com 		olabel = NULL;
293510934Ssommerfeld@sun.com 	}
293610934Ssommerfeld@sun.com 
29374342Spwernau 	if (cflag) {
29384342Spwernau 		/*
29394342Spwernau 		 * Assume the checked cmd would have worked if it was actually
29404342Spwernau 		 * used. doaddresses() will increment lines_added if it
29414342Spwernau 		 * succeeds.
29424342Spwernau 		 */
29434342Spwernau 		lines_added++;
29444342Spwernau 	} else {
29456668Smarkfen 		doaddresses(sadb_msg_type, satype,
29464235Smarkfen 		    cmd, srchp, dsthp, src, dst, unspec_src, buffer, totallen,
29474235Smarkfen 		    spi, ebuf);
29484235Smarkfen 	}
29494235Smarkfen 
29504235Smarkfen 	if (isrchp != NULL && isrchp != &dummy.he)
29514342Spwernau 		freehostent(isrchp);
29524235Smarkfen 	if (idsthp != NULL && idsthp != &dummy.he)
29534342Spwernau 		freehostent(idsthp);
29544235Smarkfen 	if (srchp != NULL && srchp != &dummy.he)
29554342Spwernau 		freehostent(srchp);
29564235Smarkfen 	if (dsthp != NULL && dsthp != &dummy.he)
29574342Spwernau 		freehostent(dsthp);
29584235Smarkfen 	if (natt_lhp != NULL && natt_lhp != &dummy.he)
29594342Spwernau 		freehostent(natt_lhp);
29604235Smarkfen 	if (natt_rhp != NULL && natt_rhp != &dummy.he)
29614342Spwernau 		freehostent(natt_rhp);
29624235Smarkfen 	free(ebuf);
29634235Smarkfen 	free(buffer);
29644235Smarkfen }
29654235Smarkfen 
29664235Smarkfen /*
29674235Smarkfen  * DELETE and GET are similar, in that they only need the extensions
29684235Smarkfen  * required to _find_ an SA, and then either delete it or obtain its
29694235Smarkfen  * information.
29704235Smarkfen  */
29714235Smarkfen static void
dodelget(int cmd,int satype,char * argv[],char * ebuf)29724235Smarkfen dodelget(int cmd, int satype, char *argv[], char *ebuf)
29734235Smarkfen {
29744235Smarkfen 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
29754235Smarkfen 	uint64_t *nextext;
29764235Smarkfen 	struct sadb_sa *assoc = NULL;
29774235Smarkfen 	struct sadb_address *src = NULL, *dst = NULL;
29784235Smarkfen 	int next, token, sa_len;
29794235Smarkfen 	char *thiscmd;
29804235Smarkfen 	uint32_t spi;
29816668Smarkfen 	uint8_t	sadb_msg_type;
29824235Smarkfen 	struct hostent *srchp = NULL, *dsthp = NULL;
29834235Smarkfen 	struct sockaddr_in6 *sin6;
29844235Smarkfen 	boolean_t unspec_src = B_TRUE;
29854235Smarkfen 	uint16_t srcport = 0, dstport = 0;
29864235Smarkfen 	uint8_t proto = 0;
29874235Smarkfen 	char *ep = NULL;
298811452SVladimir.Kotal@Sun.COM 	uint32_t sa_flags = 0;
29894235Smarkfen 
29904235Smarkfen 	/* Set the first extension header to right past the base message. */
29914235Smarkfen 	nextext = (uint64_t *)(msg + 1);
29924235Smarkfen 	bzero(nextext, sizeof (get_buffer) - sizeof (*msg));
29934235Smarkfen 
29946668Smarkfen 	switch (cmd) {
29956668Smarkfen 	case CMD_GET:
29966668Smarkfen 		thiscmd = "get";
29976668Smarkfen 		sadb_msg_type = SADB_GET;
29986668Smarkfen 		break;
29996668Smarkfen 	case CMD_DELETE:
30006668Smarkfen 		thiscmd = "delete";
30016668Smarkfen 		sadb_msg_type = SADB_DELETE;
30026668Smarkfen 		break;
30036668Smarkfen 	case CMD_DELETE_PAIR:
30046668Smarkfen 		thiscmd = "delete-pair";
30056668Smarkfen 		sadb_msg_type = SADB_X_DELPAIR;
30066668Smarkfen 		break;
30076668Smarkfen 	}
30086668Smarkfen 
30096668Smarkfen 	msg_init(msg, sadb_msg_type, (uint8_t)satype);
30104235Smarkfen 
30114235Smarkfen #define	ALLOC_ADDR_EXT(ext, exttype)			\
30124235Smarkfen 	(ext) = (struct sadb_address *)nextext;		\
30134235Smarkfen 	nextext = (uint64_t *)((ext) + 1);		\
30144235Smarkfen 	nextext += SADB_8TO64(roundup(sa_len, 8));	\
30154235Smarkfen 	(ext)->sadb_address_exttype = exttype;		\
30164235Smarkfen 	(ext)->sadb_address_len = nextext - ((uint64_t *)ext);
30174235Smarkfen 
30184235Smarkfen 	/* Assume last element in argv is set to NULL. */
30194235Smarkfen 	do {
30204235Smarkfen 		token = parseextval(*argv, &next);
30214235Smarkfen 		argv++;
30224235Smarkfen 		switch (token) {
30234235Smarkfen 		case TOK_EOF:
30244235Smarkfen 			/* Do nothing, I'm done. */
30254235Smarkfen 			break;
30264235Smarkfen 		case TOK_UNKNOWN:
30274235Smarkfen 			ERROR1(ep, ebuf, gettext(
30284235Smarkfen 			    "Unknown extension field \"%s\"\n"), *(argv - 1));
30294235Smarkfen 			break;
30304235Smarkfen 		case TOK_SPI:
30314235Smarkfen 			if (assoc != NULL) {
30324235Smarkfen 				ERROR(ep, ebuf, gettext(
30334235Smarkfen 				    "Can only specify single SPI value.\n"));
30344235Smarkfen 				break;
30354235Smarkfen 			}
30364235Smarkfen 			assoc = (struct sadb_sa *)nextext;
30374235Smarkfen 			nextext = (uint64_t *)(assoc + 1);
30384235Smarkfen 			assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc));
30394235Smarkfen 			assoc->sadb_sa_exttype = SADB_EXT_SA;
30404235Smarkfen 			assoc->sadb_sa_spi = htonl((uint32_t)parsenum(*argv,
30414235Smarkfen 			    B_TRUE, ebuf));
30424235Smarkfen 			spi = assoc->sadb_sa_spi;
30434235Smarkfen 			argv++;
30444235Smarkfen 			break;
30454235Smarkfen 		case TOK_SRCPORT:
30464235Smarkfen 			if (srcport != 0) {
30474235Smarkfen 				ERROR(ep, ebuf, gettext(
30484235Smarkfen 				    "Can only specify single source port.\n"));
30494235Smarkfen 				break;
30504235Smarkfen 			}
30514235Smarkfen 			srcport = parsenum(*argv, B_TRUE, ebuf);
30524235Smarkfen 			argv++;
30534235Smarkfen 			break;
30544235Smarkfen 		case TOK_DSTPORT:
30554235Smarkfen 			if (dstport != 0) {
30564235Smarkfen 				ERROR(ep, ebuf, gettext(
30574235Smarkfen 				    "Can only "
30584235Smarkfen 				    "specify single destination port.\n"));
30594235Smarkfen 				break;
30604235Smarkfen 			}
30614235Smarkfen 			dstport = parsenum(*argv, B_TRUE, ebuf);
30624235Smarkfen 			argv++;
30634235Smarkfen 			break;
30644235Smarkfen 		case TOK_PROTO:
30654235Smarkfen 			if (proto != 0) {
30664235Smarkfen 				ERROR(ep, ebuf, gettext(
30674235Smarkfen 				    "Can only specify single protocol.\n"));
30684235Smarkfen 				break;
30694235Smarkfen 			}
30704235Smarkfen 			proto = parsenum(*argv, B_TRUE, ebuf);
30714235Smarkfen 			argv++;
30724235Smarkfen 			break;
30734235Smarkfen 		case TOK_SRCADDR:
30744235Smarkfen 		case TOK_SRCADDR6:
30754235Smarkfen 			if (src != NULL) {
30764235Smarkfen 				ERROR(ep, ebuf, gettext(
30774235Smarkfen 				    "Can only specify single source addr.\n"));
30784235Smarkfen 				break;
30794235Smarkfen 			}
30804235Smarkfen 			sa_len = parseaddr(*argv, &srchp,
30814235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
30824235Smarkfen 			if (srchp == NULL) {
30834235Smarkfen 				ERROR1(ep, ebuf, gettext(
30844235Smarkfen 				    "Unknown source address \"%s\"\n"), *argv);
30854235Smarkfen 				break;
30864235Smarkfen 			}
30874235Smarkfen 			argv++;
30884235Smarkfen 
30894235Smarkfen 			unspec_src = B_FALSE;
30904235Smarkfen 
30914235Smarkfen 			ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
30924235Smarkfen 
30934235Smarkfen 			if (srchp == &dummy.he) {
30944235Smarkfen 				/*
30954235Smarkfen 				 * Single address with -n flag.
30964235Smarkfen 				 */
30974235Smarkfen 				sin6 = (struct sockaddr_in6 *)(src + 1);
30984235Smarkfen 				bzero(sin6, sizeof (*sin6));
30994235Smarkfen 				sin6->sin6_family = AF_INET6;
31004235Smarkfen 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
31014235Smarkfen 				    sizeof (struct in6_addr));
31024235Smarkfen 			}
31034235Smarkfen 			/* The rest is pre-bzeroed for us. */
31044235Smarkfen 			break;
31054235Smarkfen 		case TOK_DSTADDR:
31064235Smarkfen 		case TOK_DSTADDR6:
31074235Smarkfen 			if (dst != NULL) {
31084235Smarkfen 				ERROR(ep, ebuf, gettext(
31094235Smarkfen 				    "Can only specify single destination "
31104235Smarkfen 				    "address.\n"));
31114235Smarkfen 				break;
31124235Smarkfen 			}
31134235Smarkfen 			sa_len = parseaddr(*argv, &dsthp,
31144235Smarkfen 			    (token == TOK_SRCADDR6), ebuf);
31154235Smarkfen 			if (dsthp == NULL) {
31164235Smarkfen 				ERROR1(ep, ebuf, gettext(
31174235Smarkfen 				    "Unknown destination address \"%s\"\n"),
31184235Smarkfen 				    *argv);
31194235Smarkfen 				break;
31204235Smarkfen 			}
31214235Smarkfen 			argv++;
31224235Smarkfen 
31234235Smarkfen 			ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
31244235Smarkfen 
31254235Smarkfen 			if (dsthp == &dummy.he) {
31264235Smarkfen 				/*
31274235Smarkfen 				 * Single address with -n flag.
31284235Smarkfen 				 */
31294235Smarkfen 				sin6 = (struct sockaddr_in6 *)(dst + 1);
31304235Smarkfen 				bzero(sin6, sizeof (*sin6));
31314235Smarkfen 				sin6->sin6_family = AF_INET6;
31324235Smarkfen 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
31334235Smarkfen 				    sizeof (struct in6_addr));
31344235Smarkfen 			}
31354235Smarkfen 			/* The rest is pre-bzeroed for us. */
31364235Smarkfen 			break;
31376668Smarkfen 		case TOK_FLAG_INBOUND:
313811452SVladimir.Kotal@Sun.COM 			sa_flags |= SADB_X_SAFLAGS_INBOUND;
31396668Smarkfen 			break;
31406668Smarkfen 		case TOK_FLAG_OUTBOUND:
314111452SVladimir.Kotal@Sun.COM 			sa_flags |= SADB_X_SAFLAGS_OUTBOUND;
31426668Smarkfen 			break;
31434235Smarkfen 		default:
31444235Smarkfen 			ERROR2(ep, ebuf, gettext(
31454235Smarkfen 			    "Don't use extension %s for '%s' command.\n"),
31464235Smarkfen 			    *(argv - 1), thiscmd);
31474235Smarkfen 			break;
31484235Smarkfen 		}
31494235Smarkfen 	} while (token != TOK_EOF);
31504235Smarkfen 
31514235Smarkfen 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
31524235Smarkfen 
315311452SVladimir.Kotal@Sun.COM 	if (assoc == NULL) {
315411452SVladimir.Kotal@Sun.COM 		FATAL1(ep, ebuf, gettext(
315511452SVladimir.Kotal@Sun.COM 		    "Need SA parameters for %s.\n"), thiscmd);
315611452SVladimir.Kotal@Sun.COM 	}
315711452SVladimir.Kotal@Sun.COM 
315811452SVladimir.Kotal@Sun.COM 	/* We can set the flags now with valid assoc in hand. */
315911452SVladimir.Kotal@Sun.COM 	assoc->sadb_sa_flags |= sa_flags;
316011452SVladimir.Kotal@Sun.COM 
31614235Smarkfen 	if ((srcport != 0) && (src == NULL)) {
31624235Smarkfen 		ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
31634235Smarkfen 		sin6 = (struct sockaddr_in6 *)(src + 1);
31644235Smarkfen 		src->sadb_address_proto = proto;
31654235Smarkfen 		bzero(sin6, sizeof (*sin6));
31664235Smarkfen 		sin6->sin6_family = AF_INET6;
31674235Smarkfen 		sin6->sin6_port = htons(srcport);
31684235Smarkfen 	}
31694235Smarkfen 
31704235Smarkfen 	if ((dstport != 0) && (dst == NULL)) {
31714235Smarkfen 		ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
31724235Smarkfen 		sin6 = (struct sockaddr_in6 *)(dst + 1);
31734235Smarkfen 		src->sadb_address_proto = proto;
31744235Smarkfen 		bzero(sin6, sizeof (*sin6));
31754235Smarkfen 		sin6->sin6_family = AF_INET6;
31764235Smarkfen 		sin6->sin6_port = htons(dstport);
31774235Smarkfen 	}
31784235Smarkfen 
31794235Smarkfen 	/* So I have enough of the message to send it down! */
31804235Smarkfen 	msg->sadb_msg_len = nextext - get_buffer;
31814235Smarkfen 
31824342Spwernau 	if (cflag) {
31834342Spwernau 		/*
31844342Spwernau 		 * Assume the checked cmd would have worked if it was actually
31854342Spwernau 		 * used. doaddresses() will increment lines_added if it
31864342Spwernau 		 * succeeds.
31874342Spwernau 		 */
31884342Spwernau 		lines_added++;
31894342Spwernau 	} else {
31906668Smarkfen 		doaddresses(sadb_msg_type, satype,
31914235Smarkfen 		    cmd, srchp, dsthp, src, dst, unspec_src, get_buffer,
31924235Smarkfen 		    sizeof (get_buffer), spi, NULL);
31934235Smarkfen 	}
31944235Smarkfen 
31954235Smarkfen 	if (srchp != NULL && srchp != &dummy.he)
31964235Smarkfen 		freehostent(srchp);
31974235Smarkfen 	if (dsthp != NULL && dsthp != &dummy.he)
31984235Smarkfen 		freehostent(dsthp);
31994235Smarkfen }
32004235Smarkfen 
32014235Smarkfen /*
32029086SVladimir.Kotal@Sun.COM  * "ipseckey monitor" should exit very gracefully if ^C is tapped provided
32039086SVladimir.Kotal@Sun.COM  * it is not running in interactive mode.
32044235Smarkfen  */
32054235Smarkfen static void
monitor_catch(int signal)32064235Smarkfen monitor_catch(int signal)
32074235Smarkfen {
32089086SVladimir.Kotal@Sun.COM 	if (!interactive)
32099086SVladimir.Kotal@Sun.COM 		errx(signal, gettext("Bailing on signal %d."), signal);
32104235Smarkfen }
32114235Smarkfen 
32124235Smarkfen /*
32134235Smarkfen  * Loop forever, listening on PF_KEY messages.
32144235Smarkfen  */
32154235Smarkfen static void
domonitor(boolean_t passive)32164235Smarkfen domonitor(boolean_t passive)
32174235Smarkfen {
32184235Smarkfen 	struct sadb_msg *samsg;
32199086SVladimir.Kotal@Sun.COM 	struct sigaction newsig, oldsig;
32204235Smarkfen 	int rc;
32214235Smarkfen 
32224235Smarkfen 	/* Catch ^C. */
32239086SVladimir.Kotal@Sun.COM 	newsig.sa_handler = monitor_catch;
32249086SVladimir.Kotal@Sun.COM 	newsig.sa_flags = 0;
32259086SVladimir.Kotal@Sun.COM 	(void) sigemptyset(&newsig.sa_mask);
32269086SVladimir.Kotal@Sun.COM 	(void) sigaddset(&newsig.sa_mask, SIGINT);
32279086SVladimir.Kotal@Sun.COM 	(void) sigaction(SIGINT, &newsig, &oldsig);
32284235Smarkfen 
32294235Smarkfen 	samsg = (struct sadb_msg *)get_buffer;
32304235Smarkfen 	if (!passive) {
32314235Smarkfen 		(void) printf(gettext("Actively"));
32324235Smarkfen 		msg_init(samsg, SADB_X_PROMISC, 1);	/* Turn ON promisc. */
32334235Smarkfen 		rc = key_write(keysock, samsg, sizeof (*samsg));
32344235Smarkfen 		if (rc == -1)
32354235Smarkfen 			Bail("write (SADB_X_PROMISC)");
32364235Smarkfen 	} else {
32374235Smarkfen 		(void) printf(gettext("Passively"));
32384235Smarkfen 	}
32394235Smarkfen 	(void) printf(gettext(" monitoring the PF_KEY socket.\n"));
32404235Smarkfen 
32414235Smarkfen 	for (; ; ) {
32424235Smarkfen 		/*
32434235Smarkfen 		 * I assume that read() is non-blocking, and will never
32444235Smarkfen 		 * return 0.
32454235Smarkfen 		 */
32464235Smarkfen 		rc = read(keysock, samsg, sizeof (get_buffer));
32479086SVladimir.Kotal@Sun.COM 		if (rc == -1) {
32489086SVladimir.Kotal@Sun.COM 			if (errno == EINTR && interactive)
32499086SVladimir.Kotal@Sun.COM 				goto out;
32509086SVladimir.Kotal@Sun.COM 			else
32519086SVladimir.Kotal@Sun.COM 				Bail("read (in domonitor)");
32529086SVladimir.Kotal@Sun.COM 		}
32534235Smarkfen 		(void) printf(gettext("Read %d bytes.\n"), rc);
32544235Smarkfen 		/*
32554235Smarkfen 		 * Q:  Should I use the same method of printing as GET does?
32564235Smarkfen 		 * A:  For now, yes.
32574235Smarkfen 		 */
32584867Spwernau 		print_samsg(stdout, get_buffer, B_TRUE, vflag, nflag);
32594235Smarkfen 		(void) putchar('\n');
32604235Smarkfen 	}
32619086SVladimir.Kotal@Sun.COM 
32629086SVladimir.Kotal@Sun.COM out:
32639086SVladimir.Kotal@Sun.COM 	if (interactive)
32649086SVladimir.Kotal@Sun.COM 		/* restore SIGINT behavior */
32659086SVladimir.Kotal@Sun.COM 		(void) sigaction(SIGINT, &oldsig, NULL);
32664235Smarkfen }
32674235Smarkfen 
32684235Smarkfen /*
32694235Smarkfen  * Either mask or unmask all relevant signals.
32704235Smarkfen  */
32714235Smarkfen static void
mask_signals(boolean_t unmask)32724235Smarkfen mask_signals(boolean_t unmask)
32734235Smarkfen {
32744235Smarkfen 	sigset_t set;
32754235Smarkfen 	static sigset_t oset;
32764235Smarkfen 
32774235Smarkfen 	if (unmask) {
32784235Smarkfen 		(void) sigprocmask(SIG_SETMASK, &oset, NULL);
32794235Smarkfen 	} else {
32804235Smarkfen 		(void) sigfillset(&set);
32814235Smarkfen 		(void) sigprocmask(SIG_SETMASK, &set, &oset);
32824235Smarkfen 	}
32834235Smarkfen }
32844235Smarkfen 
32854235Smarkfen /*
32864235Smarkfen  * Assorted functions to print help text.
32874235Smarkfen  */
32884235Smarkfen #define	puts_tr(s) (void) puts(gettext(s))
32894235Smarkfen 
32904235Smarkfen static void
doattrhelp()32914235Smarkfen doattrhelp()
32924235Smarkfen {
32934235Smarkfen 	int i;
32944235Smarkfen 
32954235Smarkfen 	puts_tr("\nSA attributes:");
32964235Smarkfen 
32974235Smarkfen 	for (i = 0; tokens[i].string != NULL; i++) {
32984235Smarkfen 		if (i%3 == 0)
32994235Smarkfen 			(void) printf("\n");
33004235Smarkfen 		(void) printf("    %-15.15s", tokens[i].string);
33014235Smarkfen 	}
33024235Smarkfen 	(void) printf("\n");
33034235Smarkfen }
33044235Smarkfen 
33054235Smarkfen static void
dohelpcmd(char * cmds)33064235Smarkfen dohelpcmd(char *cmds)
33074235Smarkfen {
33084235Smarkfen 	int cmd;
33094235Smarkfen 
33104235Smarkfen 	if (strcmp(cmds, "attr") == 0) {
33114235Smarkfen 		doattrhelp();
33124235Smarkfen 		return;
33134235Smarkfen 	}
33144235Smarkfen 
33154235Smarkfen 	cmd = parsecmd(cmds);
33164235Smarkfen 	switch (cmd) {
33174235Smarkfen 	case CMD_UPDATE:
33184235Smarkfen 		puts_tr("update	 - Update an existing SA");
33194235Smarkfen 		break;
33206668Smarkfen 	case CMD_UPDATE_PAIR:
33216668Smarkfen 		puts_tr("update-pair - Update an existing pair of SA's");
33226668Smarkfen 		break;
33234235Smarkfen 	case CMD_ADD:
33244235Smarkfen 		puts_tr("add	 - Add a new security association (SA)");
33254235Smarkfen 		break;
33264235Smarkfen 	case CMD_DELETE:
33274235Smarkfen 		puts_tr("delete - Delete an SA");
33284235Smarkfen 		break;
33296668Smarkfen 	case CMD_DELETE_PAIR:
33306668Smarkfen 		puts_tr("delete-pair - Delete a pair of SA's");
33316668Smarkfen 		break;
33324235Smarkfen 	case CMD_GET:
33334235Smarkfen 		puts_tr("get - Display an SA");
33344235Smarkfen 		break;
33354235Smarkfen 	case CMD_FLUSH:
33364235Smarkfen 		puts_tr("flush - Delete all SAs");
33379086SVladimir.Kotal@Sun.COM 		puts_tr("");
33389086SVladimir.Kotal@Sun.COM 		puts_tr("Optional arguments:");
33399086SVladimir.Kotal@Sun.COM 		puts_tr("all        delete all SAs");
33409086SVladimir.Kotal@Sun.COM 		puts_tr("esp        delete just ESP SAs");
33419086SVladimir.Kotal@Sun.COM 		puts_tr("ah         delete just AH SAs");
33429086SVladimir.Kotal@Sun.COM 		puts_tr("<number>   delete just SAs with type given by number");
33439086SVladimir.Kotal@Sun.COM 		puts_tr("");
33444235Smarkfen 		break;
33454235Smarkfen 	case CMD_DUMP:
33464235Smarkfen 		puts_tr("dump - Display all SAs");
33479086SVladimir.Kotal@Sun.COM 		puts_tr("");
33489086SVladimir.Kotal@Sun.COM 		puts_tr("Optional arguments:");
33499086SVladimir.Kotal@Sun.COM 		puts_tr("all        display all SAs");
33509086SVladimir.Kotal@Sun.COM 		puts_tr("esp        display just ESP SAs");
33519086SVladimir.Kotal@Sun.COM 		puts_tr("ah         display just AH SAs");
33529086SVladimir.Kotal@Sun.COM 		puts_tr("<number>   display just SAs with type "
33539086SVladimir.Kotal@Sun.COM 		    "given by number");
33549086SVladimir.Kotal@Sun.COM 		puts_tr("");
33554235Smarkfen 		break;
33564235Smarkfen 	case CMD_MONITOR:
33574235Smarkfen 		puts_tr("monitor - Monitor all PF_KEY reply messages.");
33584235Smarkfen 		break;
33594235Smarkfen 	case CMD_PMONITOR:
33604235Smarkfen 		puts_tr(
33614235Smarkfen "pmonitor, passive_monitor - Monitor PF_KEY messages that");
33624235Smarkfen 		puts_tr(
33634235Smarkfen "                            reply to all PF_KEY sockets.");
33644235Smarkfen 		break;
33654235Smarkfen 
33664235Smarkfen 	case CMD_QUIT:
33674235Smarkfen 		puts_tr("quit, exit - Exit the program");
33684235Smarkfen 		break;
33694235Smarkfen 	case CMD_SAVE:
33704235Smarkfen 		puts_tr("save	    - Saves all SAs to a file");
33714235Smarkfen 		break;
33724235Smarkfen 	case CMD_HELP:
33734235Smarkfen 		puts_tr("help	    - Display list of commands");
33744235Smarkfen 		puts_tr("help <cmd> - Display help for command");
33754235Smarkfen 		puts_tr("help attr  - Display possible SA attributes");
33764235Smarkfen 		break;
33774235Smarkfen 	default:
33784235Smarkfen 		(void) printf(gettext("%s: Unknown command\n"), cmds);
33794235Smarkfen 		break;
33804235Smarkfen 	}
33814235Smarkfen }
33824235Smarkfen 
33834235Smarkfen 
33844235Smarkfen static void
dohelp(char * cmds)33854235Smarkfen dohelp(char *cmds)
33864235Smarkfen {
33874235Smarkfen 	if (cmds != NULL) {
33884235Smarkfen 		dohelpcmd(cmds);
33894235Smarkfen 		return;
33904235Smarkfen 	}
33914235Smarkfen 	puts_tr("Commands");
33924235Smarkfen 	puts_tr("--------");
33934235Smarkfen 	puts_tr("?, help  - Display this list");
33944235Smarkfen 	puts_tr("help <cmd> - Display help for command");
33954235Smarkfen 	puts_tr("help attr  - Display possible SA attributes");
33964235Smarkfen 	puts_tr("quit, exit - Exit the program");
33974235Smarkfen 	puts_tr("monitor - Monitor all PF_KEY reply messages.");
33984235Smarkfen 	puts_tr("pmonitor, passive_monitor - Monitor PF_KEY messages that");
33994235Smarkfen 	puts_tr("                            reply to all PF_KEY sockets.");
34004235Smarkfen 	puts_tr("");
34014235Smarkfen 	puts_tr("The following commands are of the form:");
34024235Smarkfen 	puts_tr("    <command> {SA type} {attribute value}*");
34034235Smarkfen 	puts_tr("");
34044235Smarkfen 	puts_tr("add (interactive only) - Add a new security association (SA)");
34054235Smarkfen 	puts_tr("update (interactive only) - Update an existing SA");
34066668Smarkfen 	puts_tr("update-pair (interactive only) - Update an existing SA pair");
34074235Smarkfen 	puts_tr("delete - Delete an SA");
34086668Smarkfen 	puts_tr("delete-pair - Delete an SA pair");
34094235Smarkfen 	puts_tr("get - Display an SA");
34104235Smarkfen 	puts_tr("flush - Delete all SAs");
34114235Smarkfen 	puts_tr("dump - Display all SAs");
34124235Smarkfen 	puts_tr("save - Saves all SAs to a file");
34134235Smarkfen }
34144235Smarkfen 
34154235Smarkfen /*
34164235Smarkfen  * "Parse" a command line from argv.
34174235Smarkfen  */
34184235Smarkfen static void
parseit(int argc,char * argv[],char * ebuf,boolean_t read_cmdfile)34194342Spwernau parseit(int argc, char *argv[], char *ebuf, boolean_t read_cmdfile)
34204235Smarkfen {
34214235Smarkfen 	int cmd, satype;
34224235Smarkfen 	char *ep = NULL;
34234235Smarkfen 
34244235Smarkfen 	if (argc == 0)
34254235Smarkfen 		return;
34264235Smarkfen 	cmd = parsecmd(*argv++);
34274235Smarkfen 
34284342Spwernau 	/*
34294342Spwernau 	 * Some commands loop forever and should only be run from the command
34304342Spwernau 	 * line, they should never be run from a command file as this may
34314342Spwernau 	 * be used at boot time.
34324342Spwernau 	 */
34334235Smarkfen 	switch (cmd) {
34344235Smarkfen 	case CMD_HELP:
34354342Spwernau 		if (read_cmdfile)
34364342Spwernau 			ERROR(ep, ebuf, gettext("Help not appropriate in "
34374342Spwernau 			    "config file."));
34384342Spwernau 		else
34394342Spwernau 			dohelp(*argv);
34404235Smarkfen 		return;
34414235Smarkfen 	case CMD_MONITOR:
34424342Spwernau 		if (read_cmdfile)
34434342Spwernau 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
34444342Spwernau 			    "config file."));
34459086SVladimir.Kotal@Sun.COM 		else {
34464342Spwernau 			domonitor(B_FALSE);
34479086SVladimir.Kotal@Sun.COM 			/*
34489086SVladimir.Kotal@Sun.COM 			 * Return from the function in interactive mode to
34499086SVladimir.Kotal@Sun.COM 			 * avoid error message in the next switch statement.
34509086SVladimir.Kotal@Sun.COM 			 * Also print newline to prevent prompt clobbering.
34519086SVladimir.Kotal@Sun.COM 			 * The same is done for CMD_PMONITOR.
34529086SVladimir.Kotal@Sun.COM 			 */
34539086SVladimir.Kotal@Sun.COM 			if (interactive) {
34549086SVladimir.Kotal@Sun.COM 				(void) printf("\n");
34559086SVladimir.Kotal@Sun.COM 				return;
34569086SVladimir.Kotal@Sun.COM 			}
34579086SVladimir.Kotal@Sun.COM 		}
34584235Smarkfen 		break;
34594235Smarkfen 	case CMD_PMONITOR:
34604342Spwernau 		if (read_cmdfile)
34614342Spwernau 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
34624342Spwernau 			    "config file."));
34639086SVladimir.Kotal@Sun.COM 		else {
34644342Spwernau 			domonitor(B_TRUE);
34659086SVladimir.Kotal@Sun.COM 			if (interactive) {
34669086SVladimir.Kotal@Sun.COM 				(void) printf("\n");
34679086SVladimir.Kotal@Sun.COM 				return;
34689086SVladimir.Kotal@Sun.COM 			}
34699086SVladimir.Kotal@Sun.COM 		}
34704235Smarkfen 		break;
34714235Smarkfen 	case CMD_QUIT:
34724235Smarkfen 		EXIT_OK(NULL);
34734235Smarkfen 	}
34744235Smarkfen 
34754342Spwernau 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
34764342Spwernau 
34774235Smarkfen 	satype = parsesatype(*argv, ebuf);
34784235Smarkfen 
34794235Smarkfen 	if (satype != SADB_SATYPE_UNSPEC) {
34804235Smarkfen 		argv++;
34814235Smarkfen 	} else {
34824235Smarkfen 		/*
34834235Smarkfen 		 * You must specify either "all" or a specific SA type
34844235Smarkfen 		 * for the "save" command.
34854235Smarkfen 		 */
34864235Smarkfen 		if (cmd == CMD_SAVE)
34874235Smarkfen 			if (*argv == NULL) {
34884235Smarkfen 				FATAL(ep, ebuf, gettext(
34894235Smarkfen 				    "Must specify a specific "
34904235Smarkfen 				    "SA type for save.\n"));
34914235Smarkfen 			} else {
34924235Smarkfen 				argv++;
34934235Smarkfen 			}
34944235Smarkfen 	}
34954235Smarkfen 
34964235Smarkfen 	switch (cmd) {
34974235Smarkfen 	case CMD_FLUSH:
34989086SVladimir.Kotal@Sun.COM 		if (argc > 2) {
34999086SVladimir.Kotal@Sun.COM 			ERROR(ep, ebuf, gettext("Too many arguments for "
35009086SVladimir.Kotal@Sun.COM 			    "flush command"));
35019086SVladimir.Kotal@Sun.COM 			handle_errors(ep, ebuf,
35029086SVladimir.Kotal@Sun.COM 			    interactive ? B_TRUE : B_FALSE, B_FALSE);
35039086SVladimir.Kotal@Sun.COM 		}
35044342Spwernau 		if (!cflag)
35054342Spwernau 			doflush(satype);
35064342Spwernau 		/*
35074342Spwernau 		 * If this was called because of an entry in a cmd file
35084342Spwernau 		 * then this action needs to be counted to prevent
35094342Spwernau 		 * do_interactive() treating this as an error.
35104342Spwernau 		 */
35114342Spwernau 		lines_added++;
35124235Smarkfen 		break;
35134235Smarkfen 	case CMD_ADD:
35144235Smarkfen 	case CMD_UPDATE:
35156668Smarkfen 	case CMD_UPDATE_PAIR:
35164235Smarkfen 		/*
35174235Smarkfen 		 * NOTE: Shouldn't allow ADDs or UPDATEs with keying material
35184235Smarkfen 		 * from the command line.
35194235Smarkfen 		 */
35204235Smarkfen 		if (!interactive) {
35214235Smarkfen 			errx(1, gettext(
35224235Smarkfen 			    "can't do ADD or UPDATE from the command line.\n"));
35234235Smarkfen 		}
35244235Smarkfen 		if (satype == SADB_SATYPE_UNSPEC) {
35254235Smarkfen 			FATAL(ep, ebuf, gettext(
35264235Smarkfen 			    "Must specify a specific SA type."));
35274235Smarkfen 			/* NOTREACHED */
35284235Smarkfen 		}
35294235Smarkfen 		/* Parse for extensions, including keying material. */
35304235Smarkfen 		doaddup(cmd, satype, argv, ebuf);
35314235Smarkfen 		break;
35324235Smarkfen 	case CMD_DELETE:
35336668Smarkfen 	case CMD_DELETE_PAIR:
35344235Smarkfen 	case CMD_GET:
35354235Smarkfen 		if (satype == SADB_SATYPE_UNSPEC) {
35364235Smarkfen 			FATAL(ep, ebuf, gettext(
35374235Smarkfen 			    "Must specify a single SA type."));
35384235Smarkfen 			/* NOTREACHED */
35394235Smarkfen 		}
35404235Smarkfen 		/* Parse for bare minimum to locate an SA. */
35414235Smarkfen 		dodelget(cmd, satype, argv, ebuf);
35424235Smarkfen 		break;
35434235Smarkfen 	case CMD_DUMP:
35444342Spwernau 		if (read_cmdfile)
35454342Spwernau 			ERROR(ep, ebuf, gettext("Dump not appropriate in "
35464342Spwernau 			    "config file."));
35479086SVladimir.Kotal@Sun.COM 		else {
35489086SVladimir.Kotal@Sun.COM 			if (argc > 2) {
35499086SVladimir.Kotal@Sun.COM 				ERROR(ep, ebuf, gettext("Too many arguments "
35509086SVladimir.Kotal@Sun.COM 				    "for dump command"));
35519086SVladimir.Kotal@Sun.COM 				handle_errors(ep, ebuf,
35529086SVladimir.Kotal@Sun.COM 				    interactive ? B_TRUE : B_FALSE, B_FALSE);
35539086SVladimir.Kotal@Sun.COM 			}
35544342Spwernau 			dodump(satype, NULL);
35559086SVladimir.Kotal@Sun.COM 		}
35564235Smarkfen 		break;
35574235Smarkfen 	case CMD_SAVE:
35584342Spwernau 		if (read_cmdfile) {
35594342Spwernau 			ERROR(ep, ebuf, gettext("Save not appropriate in "
35604342Spwernau 			    "config file."));
35614342Spwernau 		} else {
35624342Spwernau 			mask_signals(B_FALSE);	/* Mask signals */
35634342Spwernau 			dodump(satype, opensavefile(argv[0]));
35644342Spwernau 			mask_signals(B_TRUE);	/* Unmask signals */
35654342Spwernau 		}
35664235Smarkfen 		break;
35674235Smarkfen 	default:
35684235Smarkfen 		warnx(gettext("Unknown command (%s).\n"),
35694235Smarkfen 		    *(argv - ((satype == SADB_SATYPE_UNSPEC) ? 1 : 2)));
35704235Smarkfen 		usage();
35714235Smarkfen 	}
35724342Spwernau 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
35734235Smarkfen }
35744235Smarkfen 
35754235Smarkfen int
main(int argc,char * argv[])35764235Smarkfen main(int argc, char *argv[])
35774235Smarkfen {
35784235Smarkfen 	int ch;
35794235Smarkfen 	FILE *infile = stdin, *savefile;
35804235Smarkfen 	boolean_t dosave = B_FALSE, readfile = B_FALSE;
35814235Smarkfen 	char *configfile = NULL;
35825321Spwernau 	struct stat sbuf;
35837749SThejaswini.Singarajipura@Sun.COM 	int bootflags;
35844235Smarkfen 
35854235Smarkfen 	(void) setlocale(LC_ALL, "");
35864235Smarkfen #if !defined(TEXT_DOMAIN)
35874235Smarkfen #define	TEXT_DOMAIN "SYS_TEST"
35884235Smarkfen #endif
35894235Smarkfen 	(void) textdomain(TEXT_DOMAIN);
35904235Smarkfen 
35914235Smarkfen 	/*
35924235Smarkfen 	 * Check to see if the command is being run from smf(5).
35934235Smarkfen 	 */
35944235Smarkfen 	my_fmri = getenv("SMF_FMRI");
35954235Smarkfen 
35964235Smarkfen 	openlog("ipseckey", LOG_CONS, LOG_AUTH);
35974235Smarkfen 	if (getuid() != 0) {
35984235Smarkfen 		errx(1, "Insufficient privileges to run ipseckey.");
35994235Smarkfen 	}
36004235Smarkfen 
36014235Smarkfen 	/* umask me to paranoid, I only want to create files read-only */
36024235Smarkfen 	(void) umask((mode_t)00377);
36034235Smarkfen 
36044235Smarkfen 	while ((ch = getopt(argc, argv, "pnvf:s:c:")) != EOF)
36054235Smarkfen 		switch (ch) {
36064235Smarkfen 		case 'p':
36074235Smarkfen 			pflag = B_TRUE;
36084235Smarkfen 			break;
36094235Smarkfen 		case 'n':
36104235Smarkfen 			nflag = B_TRUE;
36114235Smarkfen 			break;
36124235Smarkfen 		case 'v':
36134235Smarkfen 			vflag = B_TRUE;
36144235Smarkfen 			break;
36154235Smarkfen 		case 'c':
36164235Smarkfen 			cflag = B_TRUE;
36174235Smarkfen 			/* FALLTHRU */
36184235Smarkfen 		case 'f':
36194235Smarkfen 			if (dosave)
36204235Smarkfen 				usage();
362111136SMark.Fenwick@Sun.COM 
362211136SMark.Fenwick@Sun.COM 			/*
362311136SMark.Fenwick@Sun.COM 			 * Use stat() to check and see if the user inadvertently
362411136SMark.Fenwick@Sun.COM 			 * passed in a bad pathname, or the name of a directory.
362511136SMark.Fenwick@Sun.COM 			 * We should also check to see if the filename is a
362611136SMark.Fenwick@Sun.COM 			 * pipe. We use stat() here because fopen() will block
362711136SMark.Fenwick@Sun.COM 			 * unless the other end of the pipe is open. This would
362811136SMark.Fenwick@Sun.COM 			 * be undesirable, especially if this is called at boot
362911136SMark.Fenwick@Sun.COM 			 * time. If we ever need to support reading from a pipe
363011136SMark.Fenwick@Sun.COM 			 * or special file, this should be revisited.
363111136SMark.Fenwick@Sun.COM 			 */
363211136SMark.Fenwick@Sun.COM 			if (stat(optarg, &sbuf) == -1) {
363311136SMark.Fenwick@Sun.COM 				EXIT_BADCONFIG2("Invalid pathname: %s\n",
363411136SMark.Fenwick@Sun.COM 				    optarg);
363511136SMark.Fenwick@Sun.COM 			}
363611136SMark.Fenwick@Sun.COM 			if (!(sbuf.st_mode & S_IFREG)) {
363711136SMark.Fenwick@Sun.COM 				EXIT_BADCONFIG2("%s - Not a regular file\n",
363811136SMark.Fenwick@Sun.COM 				    optarg);
363911136SMark.Fenwick@Sun.COM 			}
36404235Smarkfen 			infile = fopen(optarg, "r");
36414235Smarkfen 			if (infile == NULL) {
36424235Smarkfen 				EXIT_BADCONFIG2("Unable to open configuration "
36434235Smarkfen 				    "file: %s\n", optarg);
36444235Smarkfen 			}
36455321Spwernau 			/*
364611136SMark.Fenwick@Sun.COM 			 * The input file contains keying information, because
364711136SMark.Fenwick@Sun.COM 			 * this is sensative, we should only accept data from
364811136SMark.Fenwick@Sun.COM 			 * this file if the file is root owned and only readable
364911136SMark.Fenwick@Sun.COM 			 * by privileged users. If the command is being run by
365011136SMark.Fenwick@Sun.COM 			 * the administrator, issue a warning, if this is run by
365111136SMark.Fenwick@Sun.COM 			 * smf(5) (IE: boot time) and the permissions are too
365211136SMark.Fenwick@Sun.COM 			 * open, we will fail, the SMF service will end up in
365311136SMark.Fenwick@Sun.COM 			 * maintenace mode. The check is made with fstat() to
365411136SMark.Fenwick@Sun.COM 			 * eliminate any possible TOT to TOU window.
36555321Spwernau 			 */
36565321Spwernau 			if (fstat(fileno(infile), &sbuf) == -1) {
36575321Spwernau 				(void) fclose(infile);
36585321Spwernau 				EXIT_BADCONFIG2("Unable to stat configuration "
36595321Spwernau 				    "file: %s\n", optarg);
36605321Spwernau 			}
36615321Spwernau 			if (INSECURE_PERMS(sbuf)) {
36625321Spwernau 				if (my_fmri != NULL) {
36635321Spwernau 					(void) fclose(infile);
36645321Spwernau 					EXIT_BADCONFIG2("Config file "
36655321Spwernau 					    "%s has insecure permissions.",
36665321Spwernau 					    optarg);
36675321Spwernau 				} else 	{
366811136SMark.Fenwick@Sun.COM 					(void) fprintf(stderr, gettext(
366911136SMark.Fenwick@Sun.COM 					    "Config file %s has insecure "
367011136SMark.Fenwick@Sun.COM 					    "permissions, will be rejected in "
367111136SMark.Fenwick@Sun.COM 					    "permanent config.\n"), optarg);
36725321Spwernau 				}
36735321Spwernau 			}
36744235Smarkfen 			configfile = strdup(optarg);
36754235Smarkfen 			readfile = B_TRUE;
36764235Smarkfen 			break;
36774235Smarkfen 		case 's':
36784235Smarkfen 			if (readfile)
36794235Smarkfen 				usage();
36804235Smarkfen 			dosave = B_TRUE;
36814235Smarkfen 			savefile = opensavefile(optarg);
36824235Smarkfen 			break;
36834235Smarkfen 		default:
36844235Smarkfen 			usage();
36854235Smarkfen 		}
36864235Smarkfen 
36874235Smarkfen 	argc -= optind;
36884235Smarkfen 	argv += optind;
36894235Smarkfen 
36904235Smarkfen 	mypid = getpid();
36914235Smarkfen 
36924235Smarkfen 	keysock = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
36934235Smarkfen 
36944235Smarkfen 	if (keysock == -1) {
36954235Smarkfen 		if (errno == EPERM) {
36964235Smarkfen 			EXIT_BADPERM("Insufficient privileges to open "
36974235Smarkfen 			    "PF_KEY socket.\n");
36984235Smarkfen 		} else {
36994235Smarkfen 			/* some other reason */
37004235Smarkfen 			EXIT_FATAL("Opening PF_KEY socket");
37014235Smarkfen 		}
37024235Smarkfen 	}
37034235Smarkfen 
37047749SThejaswini.Singarajipura@Sun.COM 	if ((_cladm(CL_INITIALIZE, CL_GET_BOOTFLAG, &bootflags) != 0) ||
37057749SThejaswini.Singarajipura@Sun.COM 	    (bootflags & CLUSTER_BOOTED)) {
37067749SThejaswini.Singarajipura@Sun.COM 		in_cluster_mode = B_TRUE;
37077749SThejaswini.Singarajipura@Sun.COM 		cluster_socket = socket(AF_INET, SOCK_DGRAM, 0);
37087749SThejaswini.Singarajipura@Sun.COM 		cli_addr.sin_family = AF_INET;
370911379SVladimir.Kotal@Sun.COM 		cli_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
37107749SThejaswini.Singarajipura@Sun.COM 		cli_addr.sin_port = htons(CLUSTER_UDP_PORT);
37117749SThejaswini.Singarajipura@Sun.COM 	}
37127749SThejaswini.Singarajipura@Sun.COM 
37134235Smarkfen 	if (dosave) {
37144235Smarkfen 		mask_signals(B_FALSE);	/* Mask signals */
37154235Smarkfen 		dodump(SADB_SATYPE_UNSPEC, savefile);
37164235Smarkfen 		mask_signals(B_TRUE);	/* Unmask signals */
37174235Smarkfen 		EXIT_OK(NULL);
37184235Smarkfen 	}
37194235Smarkfen 
37204235Smarkfen 	/*
37214235Smarkfen 	 * When run from smf(5) flush any existing SA's first
37224235Smarkfen 	 * otherwise you will end up in maintenance mode.
37234235Smarkfen 	 */
37244235Smarkfen 	if ((my_fmri != NULL) && readfile) {
37254235Smarkfen 		(void) fprintf(stdout, gettext(
37264235Smarkfen 		    "Flushing existing SA's before adding new SA's\n"));
37274235Smarkfen 		(void) fflush(stdout);
37284235Smarkfen 		doflush(SADB_SATYPE_UNSPEC);
37294235Smarkfen 	}
37309086SVladimir.Kotal@Sun.COM 	if (infile != stdin || argc == 0) {
37314235Smarkfen 		/* Go into interactive mode here. */
37324235Smarkfen 		do_interactive(infile, configfile, "ipseckey> ", my_fmri,
37339086SVladimir.Kotal@Sun.COM 		    parseit, no_match);
37344235Smarkfen 	}
37354342Spwernau 	parseit(argc, argv, NULL, B_FALSE);
37364235Smarkfen 
37374235Smarkfen 	return (0);
37384235Smarkfen }
3739