xref: /titanic_51/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ipseckey.c (revision 947c7bc0a3af11f56521c217e779aa335b7ac922)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * NOTE:I'm trying to use "struct sadb_foo" instead of "sadb_foo_t"
28  *	as a maximal PF_KEY portability test.
29  *
30  *	Also, this is a deliberately single-threaded app, also for portability
31  *	to systems without POSIX threads.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/socket.h>
40 #include <sys/sysmacros.h>
41 #include <sys/fcntl.h>
42 #include <net/pfkeyv2.h>
43 #include <arpa/inet.h>
44 #include <netinet/in.h>
45 #include <sys/uio.h>
46 
47 #include <syslog.h>
48 #include <signal.h>
49 #include <unistd.h>
50 #include <limits.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <stdarg.h>
54 #include <netdb.h>
55 #include <pwd.h>
56 #include <errno.h>
57 #include <libintl.h>
58 #include <locale.h>
59 #include <fcntl.h>
60 #include <strings.h>
61 #include <ctype.h>
62 
63 #include <ipsec_util.h>
64 
65 static int keysock;
66 static uint32_t seq;
67 static pid_t mypid;
68 static boolean_t vflag = B_FALSE;	/* Verbose? */
69 static boolean_t cflag = B_FALSE;	/* Check Only */
70 
71 char *my_fmri = NULL;
72 FILE *debugfile = stdout;
73 
74 #define	MAX_GET_SIZE	1024
75 /*
76  * WARN() and ERROR() do the same thing really, with ERROR() the function
77  * that prints the error buffer needs to be called at the end of a code block
78  * This will print out all accumulated errors before bailing. The WARN()
79  * macro calls handle_errors() in such a way that it prints the message
80  * then continues.
81  * If the FATAL() macro used call handle_errors() immediately.
82  */
83 #define	ERROR(x, y, z)  x = record_error(x, y, z)
84 #define	ERROR1(w, x, y, z)  w = record_error(w, x, y, z)
85 #define	ERROR2(v, w, x, y, z)  v = record_error(v, w, x, y, z)
86 #define	WARN(x, y, z) ERROR(x, y, z);\
87 	handle_errors(x, NULL, B_FALSE, B_FALSE); x = NULL
88 #define	WARN1(w, x, y, z) ERROR1(w, x, y, z);\
89 	handle_errors(w, NULL, B_FALSE, B_FALSE); w = NULL
90 #define	WARN2(v, w, x, y, z) ERROR2(v, w, x, y, z);\
91 	handle_errors(v, NULL, B_FALSE, B_FALSE); v = NULL
92 #define	FATAL(x, y, z) ERROR(x, y, z);\
93 	handle_errors(x, y, B_TRUE, B_TRUE)
94 #define	FATAL1(w, x, y, z) ERROR1(w, x, y, z);\
95 	handle_errors(w, x, B_TRUE, B_TRUE)
96 
97 /* Defined as a uint64_t array for alignment purposes. */
98 static uint64_t get_buffer[MAX_GET_SIZE];
99 
100 /*
101  * Create/Grow a buffer large enough to hold error messages. If *ebuf
102  * is not NULL then it will contain a copy of the command line that
103  * triggered the error/warning, copy this into a new buffer or
104  * append new messages to the existing buffer.
105  */
106 /*PRINTFLIKE1*/
107 char *
108 record_error(char *ep, char *ebuf, char *fmt, ...)
109 {
110 	char *err_ptr;
111 	char tmp_buff[1024];
112 	va_list ap;
113 	int length = 0;
114 	err_ptr = ep;
115 
116 	va_start(ap, fmt);
117 	(void) vsnprintf(tmp_buff, sizeof (tmp_buff), fmt, ap);
118 	va_end(ap);
119 
120 	if (ep == NULL) {
121 		/*
122 		 * This is the first error to record, get a
123 		 * new buffer, copy in the command line that
124 		 * triggered this error/warning.
125 		 */
126 		if (ebuf != NULL) {
127 			length = strlen(ebuf);
128 			err_ptr = calloc(length, sizeof (char));
129 			if (err_ptr == NULL)
130 				Bail("calloc() failed");
131 			(void) strlcpy(err_ptr, ebuf, length);
132 		}
133 	} else {
134 		length = strlen(ep);
135 	}
136 	length += strlen(tmp_buff);
137 	/* There is a new line character */
138 	length++;
139 	err_ptr = realloc(err_ptr, length);
140 	if (err_ptr == NULL)
141 		Bail("realloc() failure");
142 	(void) strlcat(err_ptr, tmp_buff, length);
143 	return (err_ptr);
144 }
145 
146 /*
147  * Print usage message.
148  */
149 static void
150 usage(void)
151 {
152 	if (!interactive) {
153 		(void) fprintf(stderr, gettext("Usage:\t"
154 		    "ipseckey [ -nvp ] | cmd [sa_type] [extfield value]*\n"));
155 		(void) fprintf(stderr,
156 		    gettext("\tipseckey [ -nvp ] -f infile\n"));
157 		(void) fprintf(stderr,
158 		    gettext("\tipseckey [ -nvp ] -s outfile\n"));
159 	}
160 	EXIT_FATAL(NULL);
161 }
162 
163 
164 /*
165  * Print out any errors, tidy up as required.
166  * error pointer ep will be free()'d
167  */
168 void
169 handle_errors(char *ep, char *ebuf, boolean_t fatal, boolean_t done)
170 {
171 	if (ep != NULL) {
172 		if (my_fmri == NULL) {
173 			/*
174 			 * For now suppress the errors when run from smf(5)
175 			 * because potentially sensitive information could
176 			 * end up in a publicly readable logfile.
177 			 */
178 			(void) fprintf(stdout, "%s\n", ep);
179 			(void) fflush(stdout);
180 		}
181 		free(ep);
182 		if (fatal) {
183 			if (ebuf != NULL) {
184 				free(ebuf);
185 			}
186 			/* reset command buffer */
187 			if (interactive)
188 				longjmp(env, 1);
189 		} else {
190 			return;
191 		}
192 	} else {
193 		/*
194 		 * No errors, if this is the last time that this function
195 		 * is called, free(ebuf) and reset command buffer.
196 		 */
197 		if (done) {
198 			if (ebuf != NULL) {
199 				free(ebuf);
200 			}
201 			/* reset command buffer */
202 			if (interactive)
203 				longjmp(env, 1);
204 		}
205 		return;
206 	}
207 }
208 
209 /*
210  * Initialize a PF_KEY base message.
211  */
212 static void
213 msg_init(struct sadb_msg *msg, uint8_t type, uint8_t satype)
214 {
215 	msg->sadb_msg_version = PF_KEY_V2;
216 	msg->sadb_msg_type = type;
217 	msg->sadb_msg_errno = 0;
218 	msg->sadb_msg_satype = satype;
219 	/* For starters... */
220 	msg->sadb_msg_len = SADB_8TO64(sizeof (*msg));
221 	msg->sadb_msg_reserved = 0;
222 	msg->sadb_msg_seq = ++seq;
223 	msg->sadb_msg_pid = mypid;
224 }
225 
226 /*
227  * parseXXX and rparseXXX commands parse input and convert them to PF_KEY
228  * field values, or do the reverse for the purposes of saving the SA tables.
229  * (See the save_XXX functions.)
230  */
231 
232 #define	CMD_NONE	0
233 #define	CMD_UPDATE	2
234 #define	CMD_ADD		3
235 #define	CMD_DELETE	4
236 #define	CMD_GET		5
237 #define	CMD_FLUSH	9
238 #define	CMD_DUMP	10
239 #define	CMD_MONITOR	11
240 #define	CMD_PMONITOR	12
241 #define	CMD_QUIT	13
242 #define	CMD_SAVE	14
243 #define	CMD_HELP	15
244 
245 /*
246  * Parse the command.
247  */
248 static int
249 parsecmd(char *cmdstr)
250 {
251 	static struct cmdtable {
252 		char *cmd;
253 		int token;
254 	} table[] = {
255 		/*
256 		 * Q: Do we want to do GETSPI?
257 		 * A: No, it's for automated key mgmt. only.  Either that,
258 		 *    or it isn't relevant until we support non IPsec SA types.
259 		 */
260 		{"update",		CMD_UPDATE},
261 		{"add",			CMD_ADD},
262 		{"delete", 		CMD_DELETE},
263 		{"get", 		CMD_GET},
264 		/*
265 		 * Q: And ACQUIRE and REGISTER and EXPIRE?
266 		 * A: not until we support non IPsec SA types.
267 		 */
268 		{"flush",		CMD_FLUSH},
269 		{"dump",		CMD_DUMP},
270 		{"monitor",		CMD_MONITOR},
271 		{"passive_monitor",	CMD_PMONITOR},
272 		{"pmonitor",		CMD_PMONITOR},
273 		{"quit",		CMD_QUIT},
274 		{"exit",		CMD_QUIT},
275 		{"save",		CMD_SAVE},
276 		{"help",		CMD_HELP},
277 		{"?",			CMD_HELP},
278 		{NULL,			CMD_NONE}
279 	};
280 	struct cmdtable *ct = table;
281 
282 	while (ct->cmd != NULL && strcmp(ct->cmd, cmdstr) != 0)
283 		ct++;
284 	return (ct->token);
285 }
286 
287 /*
288  * Convert a number from a command line.  I picked "u_longlong_t" for the
289  * number because we need the largest number available.  Also, the strto<num>
290  * calls don't deal in units of uintNN_t.
291  */
292 static u_longlong_t
293 parsenum(char *num, boolean_t bail, char *ebuf)
294 {
295 	u_longlong_t rc = 0;
296 	char *end = NULL;
297 	char *ep = NULL;
298 
299 	if (num == NULL) {
300 		FATAL(ep, ebuf, gettext("Unexpected end of command line,"
301 		    " was expecting a number.\n"));
302 		/* NOTREACHED */
303 	}
304 
305 	errno = 0;
306 	rc = strtoull(num, &end, 0);
307 	if (errno != 0 || end == num || *end != '\0') {
308 		if (bail) {
309 			ERROR1(ep, ebuf, gettext(
310 			"Expecting a number, not \"%s\"!\n"), num);
311 		} else {
312 			/*
313 			 * -1, while not optimal, is sufficiently out of range
314 			 * for most of this function's applications when
315 			 * we don't just bail.
316 			 */
317 			return ((u_longlong_t)-1);
318 		}
319 	}
320 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
321 	return (rc);
322 }
323 
324 /*
325  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
326  */
327 static struct typetable {
328 	char *type;
329 	int token;
330 } type_table[] = {
331 	{"all",	SADB_SATYPE_UNSPEC},
332 	{"ah",	SADB_SATYPE_AH},
333 	{"esp",	SADB_SATYPE_ESP},
334 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
335 	{NULL,	0}	/* Token value is irrelevant for this entry. */
336 };
337 
338 
339 static int
340 parsesatype(char *type, char *ebuf)
341 {
342 	struct typetable *tt = type_table;
343 	char *ep = NULL;
344 
345 	if (type == NULL)
346 		return (SADB_SATYPE_UNSPEC);
347 
348 	while (tt->type != NULL && strcasecmp(tt->type, type) != 0)
349 		tt++;
350 
351 	/*
352 	 * New SA types (including ones keysock maintains for user-land
353 	 * protocols) may be added, so parse a numeric value if possible.
354 	 */
355 	if (tt->type == NULL) {
356 		tt->token = (int)parsenum(type, B_FALSE, ebuf);
357 		if (tt->token == -1) {
358 			ERROR1(ep, ebuf, gettext(
359 			    "Unknown SA type (%s).\n"), type);
360 			tt->token = SADB_SATYPE_UNSPEC;
361 		}
362 	}
363 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
364 	return (tt->token);
365 }
366 
367 #define	NEXTEOF		0
368 #define	NEXTNONE	1
369 #define	NEXTNUM		2
370 #define	NEXTSTR		3
371 #define	NEXTNUMSTR	4
372 #define	NEXTADDR	5
373 #define	NEXTHEX		6
374 #define	NEXTIDENT	7
375 #define	NEXTADDR4	8
376 #define	NEXTADDR6	9
377 
378 #define	TOK_EOF			0
379 #define	TOK_UNKNOWN		1
380 #define	TOK_SPI			2
381 #define	TOK_REPLAY		3
382 #define	TOK_STATE		4
383 #define	TOK_AUTHALG		5
384 #define	TOK_ENCRALG		6
385 #define	TOK_FLAGS		7
386 #define	TOK_SOFT_ALLOC		8
387 #define	TOK_SOFT_BYTES		9
388 #define	TOK_SOFT_ADDTIME	10
389 #define	TOK_SOFT_USETIME	11
390 #define	TOK_HARD_ALLOC		12
391 #define	TOK_HARD_BYTES		13
392 #define	TOK_HARD_ADDTIME	14
393 #define	TOK_HARD_USETIME	15
394 #define	TOK_CURRENT_ALLOC	16
395 #define	TOK_CURRENT_BYTES	17
396 #define	TOK_CURRENT_ADDTIME	18
397 #define	TOK_CURRENT_USETIME	19
398 #define	TOK_SRCADDR		20
399 #define	TOK_DSTADDR		21
400 #define	TOK_PROXYADDR		22
401 #define	TOK_AUTHKEY		23
402 #define	TOK_ENCRKEY		24
403 #define	TOK_SRCIDTYPE		25
404 #define	TOK_DSTIDTYPE		26
405 #define	TOK_DPD			27
406 #define	TOK_SENS_LEVEL		28
407 #define	TOK_SENS_MAP		29
408 #define	TOK_INTEG_LEVEL		30
409 #define	TOK_INTEG_MAP		31
410 #define	TOK_SRCADDR6		32
411 #define	TOK_DSTADDR6		33
412 #define	TOK_PROXYADDR6		34
413 #define	TOK_SRCPORT		35
414 #define	TOK_DSTPORT		36
415 #define	TOK_PROTO		37
416 #define	TOK_ENCAP		38
417 #define	TOK_NATLOC		39
418 #define	TOK_NATREM		40
419 #define	TOK_NATLPORT		41
420 #define	TOK_NATRPORT		42
421 #define	TOK_IPROTO		43
422 #define	TOK_IDSTADDR		44
423 #define	TOK_IDSTADDR6		45
424 #define	TOK_ISRCPORT		46
425 #define	TOK_IDSTPORT		47
426 
427 static struct toktable {
428 	char *string;
429 	int token;
430 	int next;
431 } tokens[] = {
432 	/* "String",		token value,		next arg is */
433 	{"spi",			TOK_SPI,		NEXTNUM},
434 	{"replay",		TOK_REPLAY,		NEXTNUM},
435 	{"state",		TOK_STATE,		NEXTNUMSTR},
436 	{"auth_alg",		TOK_AUTHALG,		NEXTNUMSTR},
437 	{"authalg",		TOK_AUTHALG,		NEXTNUMSTR},
438 	{"encr_alg",		TOK_ENCRALG,		NEXTNUMSTR},
439 	{"encralg",		TOK_ENCRALG,		NEXTNUMSTR},
440 	{"flags",		TOK_FLAGS,		NEXTNUM},
441 	{"soft_alloc",		TOK_SOFT_ALLOC,		NEXTNUM},
442 	{"soft_bytes",		TOK_SOFT_BYTES,		NEXTNUM},
443 	{"soft_addtime",	TOK_SOFT_ADDTIME,	NEXTNUM},
444 	{"soft_usetime",	TOK_SOFT_USETIME,	NEXTNUM},
445 	{"hard_alloc",		TOK_HARD_ALLOC,		NEXTNUM},
446 	{"hard_bytes",		TOK_HARD_BYTES,		NEXTNUM},
447 	{"hard_addtime",	TOK_HARD_ADDTIME,	NEXTNUM},
448 	{"hard_usetime",	TOK_HARD_USETIME,	NEXTNUM},
449 	{"current_alloc",	TOK_CURRENT_ALLOC,	NEXTNUM},
450 	{"current_bytes",	TOK_CURRENT_BYTES,	NEXTNUM},
451 	{"current_addtime",	TOK_CURRENT_ADDTIME,	NEXTNUM},
452 	{"current_usetime",	TOK_CURRENT_USETIME,	NEXTNUM},
453 
454 	{"saddr",		TOK_SRCADDR,		NEXTADDR},
455 	{"srcaddr",		TOK_SRCADDR,		NEXTADDR},
456 	{"src",			TOK_SRCADDR,		NEXTADDR},
457 	{"daddr",		TOK_DSTADDR,		NEXTADDR},
458 	{"dstaddr",		TOK_DSTADDR,		NEXTADDR},
459 	{"dst",			TOK_DSTADDR,		NEXTADDR},
460 	{"proxyaddr",		TOK_PROXYADDR,		NEXTADDR},
461 	{"proxy",		TOK_PROXYADDR,		NEXTADDR},
462 	{"innersrc",		TOK_PROXYADDR,		NEXTADDR},
463 	{"isrc",		TOK_PROXYADDR,		NEXTADDR},
464 	{"innerdst",		TOK_IDSTADDR,		NEXTADDR},
465 	{"idst",		TOK_IDSTADDR,		NEXTADDR},
466 
467 	{"sport",		TOK_SRCPORT,		NEXTNUM},
468 	{"dport",		TOK_DSTPORT,		NEXTNUM},
469 	{"innersport",		TOK_ISRCPORT,		NEXTNUM},
470 	{"isport",		TOK_ISRCPORT,		NEXTNUM},
471 	{"innerdport",		TOK_IDSTPORT,		NEXTNUM},
472 	{"idport",		TOK_IDSTPORT,		NEXTNUM},
473 	{"proto",		TOK_PROTO,		NEXTNUM},
474 	{"ulp",			TOK_PROTO,		NEXTNUM},
475 	{"iproto",		TOK_IPROTO,		NEXTNUM},
476 	{"iulp",		TOK_IPROTO,		NEXTNUM},
477 
478 	{"saddr6",		TOK_SRCADDR6,		NEXTADDR},
479 	{"srcaddr6",		TOK_SRCADDR6,		NEXTADDR},
480 	{"src6",		TOK_SRCADDR6,		NEXTADDR},
481 	{"daddr6",		TOK_DSTADDR6,		NEXTADDR},
482 	{"dstaddr6",		TOK_DSTADDR6,		NEXTADDR},
483 	{"dst6",		TOK_DSTADDR6,		NEXTADDR},
484 	{"proxyaddr6",		TOK_PROXYADDR6,		NEXTADDR},
485 	{"proxy6",		TOK_PROXYADDR6,		NEXTADDR},
486 	{"innersrc6",		TOK_PROXYADDR6,		NEXTADDR},
487 	{"isrc6",		TOK_PROXYADDR6,		NEXTADDR},
488 	{"innerdst6",		TOK_IDSTADDR6,		NEXTADDR},
489 	{"idst6",		TOK_IDSTADDR6,		NEXTADDR},
490 
491 	{"authkey",		TOK_AUTHKEY,		NEXTHEX},
492 	{"encrkey",		TOK_ENCRKEY,		NEXTHEX},
493 	{"srcidtype",		TOK_SRCIDTYPE,		NEXTIDENT},
494 	{"dstidtype",		TOK_DSTIDTYPE,		NEXTIDENT},
495 	{"dpd",			TOK_DPD,		NEXTNUM},
496 	{"sens_level",		TOK_SENS_LEVEL,		NEXTNUM},
497 	{"sens_map",		TOK_SENS_MAP,		NEXTHEX},
498 	{"integ_level",		TOK_INTEG_LEVEL,	NEXTNUM},
499 	{"integ_map",		TOK_INTEG_MAP,		NEXTHEX},
500 	{"nat_loc",		TOK_NATLOC,		NEXTADDR},
501 	{"nat_rem",		TOK_NATREM,		NEXTADDR},
502 	{"nat_lport",		TOK_NATLPORT,		NEXTNUM},
503 	{"nat_rport",		TOK_NATRPORT,		NEXTNUM},
504 	{"encap",		TOK_ENCAP,		NEXTNUMSTR},
505 	{NULL,			TOK_UNKNOWN,		NEXTEOF}
506 };
507 
508 /*
509  * Q:	Do I need stuff for proposals, combinations, supported algorithms,
510  *	or SPI ranges?
511  *
512  * A:	Probably not, but you never know.
513  *
514  * Parse out extension header type values.
515  */
516 static int
517 parseextval(char *value, int *next)
518 {
519 	struct toktable *tp;
520 
521 	if (value == NULL)
522 		return (TOK_EOF);
523 
524 	for (tp = tokens; tp->string != NULL; tp++)
525 		if (strcmp(value, tp->string) == 0)
526 			break;
527 
528 	/*
529 	 * Since the OS controls what extensions are available, we don't have
530 	 * to parse numeric values here.
531 	 */
532 
533 	*next = tp->next;
534 	return (tp->token);
535 }
536 
537 /*
538  * Parse possible state values.
539  */
540 static uint8_t
541 parsestate(char *state, char *ebuf)
542 {
543 	struct states {
544 		char *state;
545 		uint8_t retval;
546 	} states[] = {
547 		{"larval",	SADB_SASTATE_LARVAL},
548 		{"mature",	SADB_SASTATE_MATURE},
549 		{"dying",	SADB_SASTATE_DYING},
550 		{"dead",	SADB_SASTATE_DEAD},
551 		{NULL,		0}
552 	};
553 	struct states *sp;
554 	char *ep = NULL;
555 
556 	if (state == NULL) {
557 		FATAL(ep, ebuf, "Unexpected end of command line "
558 		    "was expecting a state.\n");
559 	}
560 
561 	for (sp = states; sp->state != NULL; sp++) {
562 		if (strcmp(sp->state, state) == 0)
563 			return (sp->retval);
564 	}
565 	ERROR1(ep, ebuf, gettext("Unknown state type \"%s\"\n"), state);
566 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
567 	return (0);
568 }
569 
570 /*
571  * Return the numerical algorithm identifier corresponding to the specified
572  * algorithm name.
573  */
574 static uint8_t
575 parsealg(char *alg, int proto_num, char *ebuf)
576 {
577 	u_longlong_t invalue;
578 	struct ipsecalgent *algent;
579 	char *ep = NULL;
580 
581 	if (alg == NULL) {
582 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
583 		    "was expecting an algorithm name.\n"));
584 	}
585 
586 	algent = getipsecalgbyname(alg, proto_num, NULL);
587 	if (algent != NULL) {
588 		uint8_t alg_num;
589 
590 		alg_num = algent->a_alg_num;
591 		freeipsecalgent(algent);
592 
593 		return (alg_num);
594 	}
595 
596 	/*
597 	 * Since algorithms can be loaded during kernel run-time, check for
598 	 * numeric algorithm values too.  PF_KEY can catch bad ones with EINVAL.
599 	 */
600 	invalue = parsenum(alg, B_FALSE, ebuf);
601 	if (invalue != (u_longlong_t)-1 &&
602 	    (u_longlong_t)(invalue & (u_longlong_t)0xff) == invalue)
603 		return ((uint8_t)invalue);
604 
605 	if (proto_num == IPSEC_PROTO_ESP) {
606 		ERROR1(ep, ebuf, gettext(
607 		    "Unknown encryption algorithm type \"%s\"\n"), alg);
608 	} else {
609 		ERROR1(ep, ebuf, gettext(
610 		    "Unknown authentication algorithm type \"%s\"\n"), alg);
611 	}
612 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
613 	return (0);
614 }
615 
616 /*
617  * Parse and reverse parse out a source/destination ID type.
618  */
619 static struct idtypes {
620 	char *idtype;
621 	uint8_t retval;
622 } idtypes[] = {
623 	{"prefix",	SADB_IDENTTYPE_PREFIX},
624 	{"fqdn",	SADB_IDENTTYPE_FQDN},
625 	{"domain",	SADB_IDENTTYPE_FQDN},
626 	{"domainname",	SADB_IDENTTYPE_FQDN},
627 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
628 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
629 	{"der_dn",	SADB_X_IDENTTYPE_DN},
630 	{"der_gn",	SADB_X_IDENTTYPE_GN},
631 	{NULL,		0}
632 };
633 
634 static uint16_t
635 parseidtype(char *type, char *ebuf)
636 {
637 	struct idtypes *idp;
638 	u_longlong_t invalue;
639 	char *ep = NULL;
640 
641 	if (type == NULL) {
642 		/* Shouldn't reach here, see callers for why. */
643 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
644 		    "was expecting a type.\n"));
645 	}
646 
647 	for (idp = idtypes; idp->idtype != NULL; idp++) {
648 		if (strcasecmp(idp->idtype, type) == 0)
649 			return (idp->retval);
650 	}
651 	/*
652 	 * Since identity types are almost arbitrary, check for numeric
653 	 * algorithm values too.  PF_KEY can catch bad ones with EINVAL.
654 	 */
655 	invalue = parsenum(type, B_FALSE, ebuf);
656 	if (invalue != (u_longlong_t)-1 &&
657 	    (u_longlong_t)(invalue & (u_longlong_t)0xffff) == invalue)
658 		return ((uint16_t)invalue);
659 
660 
661 	ERROR1(ep, ebuf, gettext("Unknown identity type \"%s\"\n"), type);
662 
663 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
664 	return (0);
665 }
666 
667 /*
668  * Parse an address off the command line.  Return length of sockaddr,
669  * and either return a hostent pointer (caller frees).  The new
670  * getipnodebyname() call does the Right Thing (TM), even with
671  * raw addresses (colon-separated IPv6 or dotted decimal IPv4).
672  */
673 
674 static struct {
675 	struct hostent he;
676 	char *addtl[2];
677 	} dummy;
678 static union {
679 	struct in6_addr ipv6;
680 	struct in_addr ipv4;
681 	uint64_t aligner;
682 } addr1;
683 
684 static int
685 parseaddr(char *addr, struct hostent **hpp, boolean_t v6only, char *ebuf)
686 {
687 	int hp_errno;
688 	struct hostent *hp = NULL;
689 	char *ep = NULL;
690 
691 	if (addr == NULL) {
692 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
693 		    "was expecting an address.\n"));
694 	}
695 
696 	if (!nflag) {
697 		/*
698 		 * Try name->address first.  Assume AF_INET6, and
699 		 * get IPv4's, plus IPv6's if and only if IPv6 is configured.
700 		 * This means to add IPv6 SAs, you must have IPv6
701 		 * up-and-running.  (AI_DEFAULT works here.)
702 		 */
703 		hp = getipnodebyname(addr, AF_INET6,
704 		    (v6only ? AI_ADDRCONFIG : (AI_DEFAULT | AI_ALL)),
705 		    &hp_errno);
706 	} else {
707 		/*
708 		 * Try a normal address conversion only.  Use "dummy"
709 		 * to construct a fake hostent.  Caller will know not
710 		 * to free this one.
711 		 */
712 		if (inet_pton(AF_INET6, addr, &addr1) == 1) {
713 			dummy.he.h_addr_list = dummy.addtl;
714 			dummy.addtl[0] = (char *)&addr1;
715 			dummy.addtl[1] = NULL;
716 			hp = &dummy.he;
717 			dummy.he.h_addrtype = AF_INET6;
718 			dummy.he.h_length = sizeof (struct in6_addr);
719 		} else if (inet_pton(AF_INET, addr, &addr1) == 1) {
720 			/*
721 			 * Remap to AF_INET6 anyway.
722 			 */
723 			dummy.he.h_addr_list = dummy.addtl;
724 			dummy.addtl[0] = (char *)&addr1;
725 			dummy.addtl[1] = NULL;
726 			hp = &dummy.he;
727 			dummy.he.h_addrtype = AF_INET6;
728 			dummy.he.h_length = sizeof (struct in6_addr);
729 			/*
730 			 * NOTE:  If macro changes to disallow in-place
731 			 * conversion, rewhack this.
732 			 */
733 			IN6_INADDR_TO_V4MAPPED(&addr1.ipv4, &addr1.ipv6);
734 		} else {
735 			hp = NULL;
736 		}
737 	}
738 
739 	if (hp == NULL)
740 		WARN1(ep, ebuf, gettext("Unknown address %s."), addr);
741 
742 	*hpp = hp;
743 	/* Always return sockaddr_in6 for now. */
744 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
745 	return (sizeof (struct sockaddr_in6));
746 }
747 
748 /*
749  * Parse a hex character for a key.  A string will take the form:
750  *	xxxxxxxxx/nn
751  * where
752  *	xxxxxxxxx == a string of hex characters ([0-9][a-f][A-F])
753  *	nn == an optional decimal "mask".  If it is not present, it
754  *	is assumed that the hex string will be rounded to the nearest
755  *	byte, where odd nibbles, like 123 will become 0x0123.
756  *
757  * NOTE:Unlike the expression of IP addresses, I will not allow an
758  *	excessive "mask".  For example 2112/50 is very illegal.
759  * NOTE2:	This key should be in canonical order.  Consult your man
760  *		pages per algorithm about said order.
761  */
762 
763 #define	hd2num(hd) (((hd) >= '0' && (hd) <= '9') ? ((hd) - '0') : \
764 	(((hd) >= 'a' && (hd) <= 'f') ? ((hd) - 'a' + 10) : ((hd) - 'A' + 10)))
765 
766 static struct sadb_key *
767 parsekey(char *input, char *ebuf)
768 {
769 	struct sadb_key *retval;
770 	uint_t i, hexlen = 0, bits, alloclen;
771 	uint8_t *key;
772 	char *ep = NULL;
773 
774 	if (input == NULL) {
775 		FATAL(ep, ebuf, gettext("Unexpected end of command line, "
776 		    "was expecting a key.\n"));
777 	}
778 	/* Allow hex values prepended with 0x convention */
779 	if ((strnlen(input, sizeof (hexlen)) > 2) &&
780 	    (strncasecmp(input, "0x", 2) == 0))
781 		input += 2;
782 
783 	for (i = 0; input[i] != '\0' && input[i] != '/'; i++)
784 		hexlen++;
785 
786 	if (input[i] == '\0') {
787 		bits = 0;
788 	} else {
789 		/* Have /nn. */
790 		input[i] = '\0';
791 		if (sscanf((input + i + 1), "%u", &bits) != 1) {
792 			FATAL1(ep, ebuf, gettext(
793 			    "\"%s\" is not a bit specifier.\n"),
794 			    (input + i + 1));
795 		}
796 		/* hexlen in nibbles */
797 		if (((bits + 3) >> 2) > hexlen) {
798 			ERROR2(ep, ebuf, gettext(
799 			    "bit length %d is too big for %s.\n"), bits, input);
800 		}
801 		/*
802 		 * Adjust hexlen down if user gave us too small of a bit
803 		 * count.
804 		 */
805 		if ((hexlen << 2) > bits + 3) {
806 			WARN2(ep, ebuf, gettext(
807 			    "WARNING: Lower bits will be truncated "
808 			    "for:\n\t%s/%d.\n"), input, bits);
809 			hexlen = (bits + 3) >> 2;
810 			input[hexlen] = '\0';
811 		}
812 	}
813 
814 	/*
815 	 * Allocate.  Remember, hexlen is in nibbles.
816 	 */
817 
818 	alloclen = sizeof (*retval) + roundup((hexlen/2 + (hexlen & 0x1)), 8);
819 	retval = malloc(alloclen);
820 
821 	if (retval == NULL)
822 		Bail("malloc(parsekey)");
823 	retval->sadb_key_len = SADB_8TO64(alloclen);
824 	retval->sadb_key_reserved = 0;
825 	if (bits == 0)
826 		retval->sadb_key_bits = (hexlen + (hexlen & 0x1)) << 2;
827 	else
828 		retval->sadb_key_bits = bits;
829 
830 	/*
831 	 * Read in nibbles.  Read in odd-numbered as shifted high.
832 	 * (e.g. 123 becomes 0x1230).
833 	 */
834 
835 	key = (uint8_t *)(retval + 1);
836 	for (i = 0; input[i] != '\0'; i += 2) {
837 		boolean_t second = (input[i + 1] != '\0');
838 
839 		if (!isxdigit(input[i]) ||
840 		    (!isxdigit(input[i + 1]) && second)) {
841 			ERROR1(ep, ebuf, gettext(
842 			    "string '%s' not a hex value.\n"), input);
843 			free(retval);
844 			retval = NULL;
845 			break;
846 		}
847 		*key = (hd2num(input[i]) << 4);
848 		if (second)
849 			*key |= hd2num(input[i + 1]);
850 		else
851 			break;	/* out of for loop. */
852 		key++;
853 	}
854 
855 	/* bzero the remaining bits if we're a non-octet amount. */
856 	if (bits & 0x7)
857 		*((input[i] == '\0') ? key - 1 : key) &=
858 		    0xff << (8 - (bits & 0x7));
859 
860 	handle_errors(ep, NULL, B_FALSE, B_FALSE);
861 	return (retval);
862 }
863 
864 /*
865  * Write a message to the PF_KEY socket.  If verbose, print the message
866  * heading into the kernel.
867  */
868 static int
869 key_write(int fd, void *msg, size_t len)
870 {
871 	if (vflag) {
872 		(void) printf(
873 		    gettext("VERBOSE ON:  Message to kernel looks like:\n"));
874 		(void) printf("==========================================\n");
875 		print_samsg(stdout, msg, B_FALSE, vflag, nflag);
876 		(void) printf("==========================================\n");
877 	}
878 
879 	return (write(fd, msg, len));
880 }
881 
882 /*
883  * SIGALRM handler for time_critical_enter.
884  */
885 static void
886 time_critical_catch(int signal)
887 {
888 	if (signal == SIGALRM) {
889 		errx(1, gettext("Reply message from PF_KEY timed out."));
890 	} else {
891 		errx(1, gettext("Caught signal %d while trying to receive"
892 		    "PF_KEY reply message"), signal);
893 	}
894 	/* errx() calls exit. */
895 }
896 
897 #define	TIME_CRITICAL_TIME 10	/* In seconds */
898 
899 /*
900  * Enter a "time critical" section where key is waiting for a return message.
901  */
902 static void
903 time_critical_enter(void)
904 {
905 	(void) signal(SIGALRM, time_critical_catch);
906 	(void) alarm(TIME_CRITICAL_TIME);
907 }
908 
909 /*
910  * Exit the "time critical" section after getting an appropriate return
911  * message.
912  */
913 static void
914 time_critical_exit(void)
915 {
916 	(void) alarm(0);
917 	(void) signal(SIGALRM, SIG_DFL);
918 }
919 
920 /*
921  * Construct a PF_KEY FLUSH message for the SA type specified.
922  */
923 static void
924 doflush(int satype)
925 {
926 	struct sadb_msg msg;
927 	int rc;
928 
929 	msg_init(&msg, SADB_FLUSH, (uint8_t)satype);
930 	rc = key_write(keysock, &msg, sizeof (msg));
931 	if (rc == -1)
932 		Bail("write() to PF_KEY socket failed (in doflush)");
933 
934 	time_critical_enter();
935 	do {
936 		rc = read(keysock, &msg, sizeof (msg));
937 		if (rc == -1)
938 			Bail("read (in doflush)");
939 	} while (msg.sadb_msg_seq != seq || msg.sadb_msg_pid != mypid);
940 	time_critical_exit();
941 
942 	/*
943 	 * I should _never_ hit the following unless:
944 	 *
945 	 * 1. There is a kernel bug.
946 	 * 2. There is another process filling in its pid with mine, and
947 	 *    issuing a different message that would cause a different result.
948 	 */
949 	if (msg.sadb_msg_type != SADB_FLUSH ||
950 	    msg.sadb_msg_satype != (uint8_t)satype) {
951 		syslog((LOG_NOTICE|LOG_AUTH),
952 		    gettext("doflush: Return message not of type SADB_FLUSH!"));
953 		Bail("doflush: Return message not of type SADB_FLUSH!");
954 	}
955 
956 	if (msg.sadb_msg_errno != 0) {
957 		errno = msg.sadb_msg_errno;
958 		if (errno == EINVAL) {
959 			print_diagnostic(stderr, msg.sadb_x_msg_diagnostic);
960 			warnx(gettext("Cannot flush SA type %d."), satype);
961 		}
962 		Bail("return message (in doflush)");
963 	}
964 }
965 
966 /*
967  * save_XXX functions are used when "saving" the SA tables to either a
968  * file or standard output.  They use the dump_XXX functions where needed,
969  * but mostly they use the rparseXXX functions.
970  */
971 
972 /*
973  * Because "save" and "dump" both use the SADB_DUMP message, fold both
974  * into the same function.
975  */
976 static void
977 dodump(int satype, FILE *ofile)
978 {
979 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
980 	int rc;
981 
982 	if (ofile != NULL) {
983 		(void) fprintf(ofile,
984 		    gettext("# This key file was generated by the"));
985 		(void) fprintf(ofile,
986 		    gettext(" ipseckey(1m) command's 'save' feature.\n\n"));
987 	}
988 	msg_init(msg, SADB_DUMP, (uint8_t)satype);
989 	rc = key_write(keysock, msg, sizeof (*msg));
990 	if (rc == -1)
991 		Bail("write to PF_KEY socket failed (in dodump)");
992 
993 	do {
994 		/*
995 		 * For DUMP, do only the read as a time critical section.
996 		 */
997 		time_critical_enter();
998 		rc = read(keysock, get_buffer, sizeof (get_buffer));
999 		time_critical_exit();
1000 		if (rc == -1)
1001 			Bail("read (in dodump)");
1002 		if (msg->sadb_msg_pid == mypid &&
1003 		    msg->sadb_msg_type == SADB_DUMP &&
1004 		    msg->sadb_msg_seq != 0 &&
1005 		    msg->sadb_msg_errno == 0) {
1006 			if (ofile == NULL) {
1007 				print_samsg(stdout, get_buffer, B_FALSE, vflag,
1008 				    nflag);
1009 				(void) putchar('\n');
1010 			} else {
1011 				save_assoc(get_buffer, ofile);
1012 			}
1013 		}
1014 	} while (msg->sadb_msg_pid != mypid ||
1015 	    (msg->sadb_msg_errno == 0 && msg->sadb_msg_seq != 0));
1016 
1017 	if (ofile != NULL && ofile != stdout)
1018 		(void) fclose(ofile);
1019 
1020 	if (msg->sadb_msg_errno == 0) {
1021 		if (ofile == NULL)
1022 			(void) printf(
1023 			    gettext("Dump succeeded for SA type %d.\n"),
1024 			    satype);
1025 	} else {
1026 		print_diagnostic(stderr, msg->sadb_x_msg_diagnostic);
1027 		errno = msg->sadb_msg_errno;
1028 		Bail("Dump failed");
1029 	}
1030 }
1031 
1032 #define	SCOPE_UNSPEC 0
1033 #define	SCOPE_LINKLOCAL 1
1034 #define	SCOPE_SITELOCAL 2
1035 #define	SCOPE_GLOBAL 3
1036 #define	SCOPE_V4COMPAT 4
1037 #define	SCOPE_LOOPBACK 5	/* Pedantic, yes, but necessary. */
1038 
1039 static int
1040 ipv6_addr_scope(struct in6_addr *addr)
1041 {
1042 	/* Don't return anything regarding multicast for now... */
1043 
1044 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
1045 		return (SCOPE_UNSPEC);
1046 
1047 	if (IN6_IS_ADDR_LINKLOCAL(addr))
1048 		return (SCOPE_LINKLOCAL);
1049 
1050 	if (IN6_IS_ADDR_SITELOCAL(addr))
1051 		return (SCOPE_SITELOCAL);
1052 
1053 	if (IN6_IS_ADDR_V4COMPAT(addr))
1054 		return (SCOPE_V4COMPAT);
1055 
1056 	if (IN6_IS_ADDR_LOOPBACK(addr))
1057 		return (SCOPE_LOOPBACK);
1058 
1059 	/* For now, return global by default. */
1060 	return (SCOPE_GLOBAL);
1061 }
1062 
1063 /*
1064  * doaddresses():
1065  *
1066  * Used by doaddup() and dodelget() to create new SA's based on the
1067  * provided source and destination addresses hostent.
1068  *
1069  * sadb_msg_type: expected PF_KEY reply message type
1070  * sadb_msg_satype: expected PF_KEY reply satype
1071  * cmd: user command
1072  * srchp: hostent for the source address(es)
1073  * dsthp: hostent for the destination address(es)
1074  * src: points to the SADB source address extension
1075  * dst: points to the SADB destination address extension
1076  * unspec_src: indicates an unspecified source address.
1077  * buffer: pointer to the SADB buffer to use with PF_KEY
1078  * buffer_size: size of buffer
1079  * spi: spi for this message (set by caller)
1080  * srcport: source port if specified
1081  * dstport: destination port if specified
1082  * proto: IP protocol number if specified
1083  * iproto: Inner (tunnel mode) IP protocol number if specified
1084  * NATT note: we are going to assume a semi-sane world where NAT
1085  * boxen don't explode to multiple addresses.
1086  */
1087 static void
1088 doaddresses(uint8_t sadb_msg_type, uint8_t sadb_msg_satype, int cmd,
1089     struct hostent *srchp, struct hostent *dsthp,
1090     struct sadb_address *src, struct sadb_address *dst,
1091     boolean_t unspec_src, uint64_t *buffer, int buffer_size, uint32_t spi,
1092     char *ebuf)
1093 {
1094 	boolean_t single_dst;
1095 	struct sockaddr_in6 *sin6;
1096 	struct sadb_msg *msgp;
1097 	int i, rc;
1098 	char **walker;	/* For the SRC and PROXY walking functions. */
1099 	char *first_match;
1100 	uint64_t savebuf[MAX_GET_SIZE];
1101 	uint16_t srcport = 0, dstport = 0;
1102 	char *ep = NULL;
1103 
1104 	/*
1105 	 * Okay, now we have "src", "dst", and maybe "proxy" reassigned
1106 	 * to point into the buffer to be written to PF_KEY, we can do
1107 	 * potentially several writes based on destination address.
1108 	 *
1109 	 * First, obtain port numbers from passed-in extensions.
1110 	 */
1111 
1112 	if (src != NULL) {
1113 		sin6 = (struct sockaddr_in6 *)(src + 1);
1114 		srcport = ntohs(sin6->sin6_port);
1115 	}
1116 	if (dst != NULL) {
1117 		sin6 = (struct sockaddr_in6 *)(dst + 1);
1118 		dstport = ntohs(sin6->sin6_port);
1119 	}
1120 
1121 	/*
1122 	 * The rules for ADD, GET, and UPDATE: (NOTE:  This assumes IPsec.
1123 	 * If other consumers of PF_KEY happen, this will have to be
1124 	 * rewhacked.):
1125 	 *
1126 	 *	Do a message for every possible DST address.
1127 	 *
1128 	 *	If a source or proxy address explodes, keep unspecified
1129 	 *	(and mention unspecified).
1130 	 *
1131 	 * If dsthp is == dummy.he, then go through the loop once.
1132 	 * If any other hp is == dummy.he, then you don't have to apply any
1133 	 * silly rules.
1134 	 *
1135 	 * DELETE is different, because you can leave either "src" or "dst"
1136 	 * blank!  You need to explode if one of them is full, and not assume
1137 	 * that the other is set.
1138 	 */
1139 
1140 	if (dsthp == NULL) {
1141 		/*
1142 		 * No destination address specified.
1143 		 * With extended diagnostics, we don't have to bail the
1144 		 * non-DELETE cases here.  The EINVAL diagnostics will be
1145 		 * enough to inform the user(s) what happened.
1146 		 */
1147 		i = 0;
1148 		do {
1149 			if (srchp == &dummy.he) {
1150 				/* Just to be sure... */
1151 				srchp->h_addr_list[1] = NULL;
1152 			} else if (srchp != NULL) {
1153 				/* Degenerate case, h_addr_list[0] == NULL. */
1154 				if (srchp->h_addr_list[i] == NULL)
1155 					Bail("Empty source address list");
1156 
1157 				/*
1158 				 * Fill in the src sockaddr.
1159 				 */
1160 				sin6 = (struct sockaddr_in6 *)(src + 1);
1161 				bzero(sin6, sizeof (*sin6));
1162 				bcopy(srchp->h_addr_list[i], &sin6->sin6_addr,
1163 				    sizeof (struct in6_addr));
1164 				sin6->sin6_family = AF_INET6;
1165 				sin6->sin6_port = htons(srcport);
1166 			}
1167 
1168 			/* Save off a copy for later writing... */
1169 			msgp = (struct sadb_msg *)buffer;
1170 			bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
1171 
1172 			rc = key_write(keysock, buffer,
1173 			    SADB_64TO8(msgp->sadb_msg_len));
1174 			if (rc == -1)
1175 				Bail("write() to PF_KEY socket "
1176 				    "(in doaddresses)");
1177 
1178 			time_critical_enter();
1179 			do {
1180 				rc = read(keysock, buffer, buffer_size);
1181 				if (rc == -1)
1182 					Bail("read (in doaddresses)");
1183 			} while (msgp->sadb_msg_seq != seq ||
1184 			    msgp->sadb_msg_pid != mypid);
1185 			time_critical_exit();
1186 
1187 			if (msgp->sadb_msg_type != sadb_msg_type ||
1188 			    msgp->sadb_msg_satype != sadb_msg_satype) {
1189 				syslog((LOG_NOTICE|LOG_AUTH), gettext(
1190 				    "doaddresses: Unexpected returned message "
1191 				    "(%d exp %d)\n"), msgp->sadb_msg_type,
1192 				    sadb_msg_type);
1193 				Bail("doaddresses: Unexpected returned "
1194 				    "message");
1195 			}
1196 
1197 			errno = msgp->sadb_msg_errno;
1198 			if (errno != 0) {
1199 				if (errno == EINVAL) {
1200 					WARN(ep, ebuf, gettext(
1201 					    "One of the entered "
1202 					    "values is incorrect."));
1203 					print_diagnostic(stderr,
1204 					    msgp->sadb_x_msg_diagnostic);
1205 				} else {
1206 					Bail("return message (in doaddresses)");
1207 				}
1208 			}
1209 
1210 			/* ...and then restore the saved buffer. */
1211 			msgp = (struct sadb_msg *)savebuf;
1212 			bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
1213 		} while (srchp != NULL && srchp->h_addr_list[++i] != NULL);
1214 		return;
1215 	}
1216 
1217 	single_dst = (dsthp == &dummy.he || dsthp->h_addr_list[1] == NULL);
1218 
1219 	for (i = 0; dsthp->h_addr_list[i] != NULL; i++) {
1220 		if (dsthp == &dummy.he) {
1221 			/* Just to be sure... */
1222 			dsthp->h_addr_list[1] = NULL;
1223 		} else {
1224 			/*
1225 			 * Fill in the dst sockaddr.
1226 			 */
1227 			sin6 = (struct sockaddr_in6 *)(dst + 1);
1228 			bzero(sin6, sizeof (*sin6));
1229 			bcopy(dsthp->h_addr_list[i], &sin6->sin6_addr,
1230 			    sizeof (struct in6_addr));
1231 			sin6->sin6_family = AF_INET6;
1232 			sin6->sin6_port = htons(dstport);
1233 		}
1234 
1235 		/*
1236 		 * Try and assign src, if there's any ambiguity.
1237 		 */
1238 		if (!unspec_src && srchp != &dummy.he) {
1239 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1240 				/*
1241 				 * IPv4 address.  Find an IPv4 address, then
1242 				 * keep looking for a second one.  If a second
1243 				 * exists, print a message, and fill in the
1244 				 * unspecified address.
1245 				 */
1246 				first_match = NULL;
1247 
1248 				for (walker = srchp->h_addr_list;
1249 				    *walker != NULL; walker++) {
1250 					/* LINTED E_BAD_PTR_CAST_ALIGN */
1251 					if (IN6_IS_ADDR_V4MAPPED(
1252 					    (struct in6_addr *)*walker)) {
1253 						if (first_match != NULL)
1254 							break;
1255 						else
1256 							first_match = *walker;
1257 					}
1258 				}
1259 				sin6 = (struct sockaddr_in6 *)(src + 1);
1260 				bzero(sin6, sizeof (*sin6));
1261 
1262 				if (first_match == NULL) {
1263 					/*
1264 					 * No IPv4 hits.  Is this a single
1265 					 * dest?
1266 					 */
1267 					WARN1(ep, ebuf, gettext(
1268 					    "No IPv4 source address "
1269 					    "for name %s.\n"), srchp->h_name);
1270 					if (single_dst) {
1271 						ERROR(ep, ebuf, gettext(
1272 						    "Only single destination "
1273 						    "IP address.\n"));
1274 					} else {
1275 						/* Continue, but do I print? */
1276 						continue;  /* for loop */
1277 					}
1278 
1279 					/* I should never reach here. */
1280 				}
1281 
1282 				sin6->sin6_family = AF_INET6;
1283 				sin6->sin6_port = htons(srcport);
1284 				if (*walker != NULL) {
1285 					/*
1286 					 * Early loop exit.  It must've been
1287 					 * multiple hits...
1288 					 *
1289 					 * Issue a null-source warning?
1290 					 */
1291 					WARN1(ep, ebuf, gettext(
1292 					    "Multiple IPv4 source addresses "
1293 					    "for %s, using unspecified source "
1294 					    "instead."), srchp->h_name);
1295 				} else {
1296 					/*
1297 					 * If I reach here w/o hitting the
1298 					 * previous if statements, I have a
1299 					 * single source address for this
1300 					 * destination.
1301 					 */
1302 					bcopy(first_match, &sin6->sin6_addr,
1303 					    sizeof (struct in6_addr));
1304 				}
1305 			} else {
1306 				/*
1307 				 * IPv6 address.  Find an IPv6 address.
1308 				 * Unlike IPv4 addresses, things can get a
1309 				 * little more sticky with scopes, etc.
1310 				 */
1311 				int dst_scope, src_scope;
1312 
1313 				dst_scope = ipv6_addr_scope(&sin6->sin6_addr);
1314 
1315 				first_match = NULL;
1316 				for (walker = srchp->h_addr_list;
1317 				    *walker != NULL; walker++) {
1318 					/* LINTED E_BAD_PTR_CAST_ALIGN */
1319 					if (!IN6_IS_ADDR_V4MAPPED(
1320 					    (struct in6_addr *)*walker)) {
1321 						/*
1322 						 * Set first-match, etc.
1323 						 * Take into account scopes,
1324 						 * and other IPv6 thingies.
1325 						 */
1326 						src_scope = ipv6_addr_scope(
1327 						    /* LINTED E_BAD_PTR_CAST */
1328 						    (struct in6_addr *)*walker);
1329 						if (src_scope == SCOPE_UNSPEC ||
1330 						    src_scope == dst_scope) {
1331 							if (first_match !=
1332 							    NULL)
1333 								break;
1334 							else
1335 								first_match =
1336 								    *walker;
1337 						}
1338 					}
1339 				}
1340 
1341 				sin6 = (struct sockaddr_in6 *)(src + 1);
1342 				bzero(sin6, sizeof (*sin6));
1343 				sin6->sin6_port = htons(srcport);
1344 				if (first_match == NULL) {
1345 					/*
1346 					 * No IPv6 hits.  Is this a single
1347 					 * dest?
1348 					 */
1349 					WARN1(ep, ebuf, gettext(
1350 					    "No IPv6 source address of "
1351 					    "matching scope for name %s.\n"),
1352 					    srchp->h_name);
1353 					if (single_dst) {
1354 						ERROR(ep, ebuf, gettext(
1355 						    "Only a single IPV6 "
1356 						    "destination "
1357 						    "address.\n"));
1358 					} else {
1359 						/* Continue, but do I print? */
1360 						continue;  /* for loop */
1361 					}
1362 
1363 					/* I should never reach here. */
1364 				}
1365 				sin6->sin6_family = AF_INET6;
1366 				if (*walker != NULL) {
1367 					/*
1368 					 * Early loop exit.  Issue a
1369 					 * null-source warning?
1370 					 */
1371 					WARN1(ep, ebuf, gettext(
1372 					    "Multiple IPv6 source addresses "
1373 					    "for %s of the same scope, using "
1374 					    "unspecified source instead.\n"),
1375 					    srchp->h_name);
1376 				} else {
1377 					/*
1378 					 * If I reach here w/o hitting the
1379 					 * previous if statements, I have a
1380 					 * single source address for this
1381 					 * destination.
1382 					 */
1383 					bcopy(first_match, &sin6->sin6_addr,
1384 					    sizeof (struct in6_addr));
1385 				}
1386 			}
1387 		}
1388 
1389 		/*
1390 		 * If there are errors at this point there is no
1391 		 * point sending anything to PF_KEY.
1392 		 */
1393 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
1394 
1395 		/* Save off a copy for later writing... */
1396 		msgp = (struct sadb_msg *)buffer;
1397 		bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
1398 
1399 		rc = key_write(keysock, buffer, SADB_64TO8(msgp->sadb_msg_len));
1400 		if (rc == -1)
1401 			Bail("write() to PF_KEY socket (in doaddresses)");
1402 
1403 		/* Blank the key for paranoia's sake. */
1404 		bzero(buffer, buffer_size);
1405 		time_critical_enter();
1406 		do {
1407 			rc = read(keysock, buffer, buffer_size);
1408 			if (rc == -1)
1409 				Bail("read (in doaddresses)");
1410 		} while (msgp->sadb_msg_seq != seq ||
1411 		    msgp->sadb_msg_pid != mypid);
1412 		time_critical_exit();
1413 
1414 		/*
1415 		 * I should _never_ hit the following unless:
1416 		 *
1417 		 * 1. There is a kernel bug.
1418 		 * 2. Another process is mistakenly using my pid in a PF_KEY
1419 		 *    message.
1420 		 */
1421 		if (msgp->sadb_msg_type != sadb_msg_type ||
1422 		    msgp->sadb_msg_satype != sadb_msg_satype) {
1423 			syslog((LOG_NOTICE|LOG_AUTH), gettext(
1424 			    "doaddresses: Unexpected returned message "
1425 			    "(%d exp %d)\n"), msgp->sadb_msg_type,
1426 			    sadb_msg_type);
1427 			Bail("doaddresses: Unexpected returned message");
1428 		}
1429 
1430 		if (msgp->sadb_msg_errno != 0) {
1431 			char addrprint[INET6_ADDRSTRLEN];
1432 			int on_errno = 0;
1433 			char *on_errno_msg;
1434 
1435 			/*
1436 			 * Print different error messages depending
1437 			 * on the SADB message type being processed.
1438 			 * If we get a ESRCH error for a GET/DELETE
1439 			 * messages, we report that the SA does not
1440 			 * exist. If we get a EEXIST error for a
1441 			 * ADD/UPDATE message, we report that the
1442 			 * SA already exists.
1443 			 */
1444 			if (sadb_msg_type == SADB_GET ||
1445 			    sadb_msg_type == SADB_DELETE) {
1446 				on_errno = ESRCH;
1447 				on_errno_msg = "does not exist";
1448 			} else if (sadb_msg_type == SADB_ADD ||
1449 			    sadb_msg_type == SADB_UPDATE) {
1450 				on_errno = EEXIST;
1451 				on_errno_msg = "already exists";
1452 			}
1453 
1454 			errno = msgp->sadb_msg_errno;
1455 			if (errno == on_errno) {
1456 				ERROR2(ep, ebuf, gettext(
1457 				    "Association (type = %s) "
1458 				    "with spi 0x%x and addr\n"),
1459 				    rparsesatype(msgp->sadb_msg_satype),
1460 				    ntohl(spi));
1461 				ERROR2(ep, ebuf, "%s %s.\n",
1462 				    do_inet_ntop(dsthp->h_addr_list[i],
1463 				    addrprint, sizeof (addrprint)),
1464 				    on_errno_msg);
1465 				msgp = (struct sadb_msg *)savebuf;
1466 				bcopy(savebuf, buffer,
1467 				    SADB_64TO8(msgp->sadb_msg_len));
1468 				continue;
1469 			} else {
1470 				if (errno == EINVAL) {
1471 					ERROR2(ep, ebuf, gettext(
1472 					    "PF_KEY Diagnostic code %u: %s.\n"),
1473 					    msgp->sadb_x_msg_diagnostic,
1474 					    keysock_diag(
1475 					    msgp->sadb_x_msg_diagnostic));
1476 				} else {
1477 					Bail("return message (in doaddresses)");
1478 				}
1479 			}
1480 		}
1481 
1482 		if (cmd == CMD_GET) {
1483 			if (msgp->sadb_msg_len > MAX_GET_SIZE) {
1484 				WARN1(ep, ebuf, gettext("WARNING:  "
1485 				    "SA information bigger than %d bytes.\n"),
1486 				    SADB_64TO8(MAX_GET_SIZE));
1487 			}
1488 			print_samsg(stdout, buffer, B_FALSE, vflag, nflag);
1489 		}
1490 
1491 		handle_errors(ep, ebuf, B_TRUE, B_FALSE);
1492 
1493 		/* ...and then restore the saved buffer. */
1494 		msgp = (struct sadb_msg *)savebuf;
1495 		bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
1496 		lines_added++;
1497 	}
1498 
1499 	/* Degenerate case, h_addr_list[0] == NULL. */
1500 	if (i == 0)
1501 		Bail("Empty destination address list");
1502 
1503 	/*
1504 	 * free(ebuf) even if there are no errors.
1505 	 * handle_errors() won't return here.
1506 	 */
1507 	handle_errors(ep, ebuf, B_TRUE, B_TRUE);
1508 }
1509 
1510 /*
1511  * Perform an add or an update.  ADD and UPDATE are similar in the extensions
1512  * they need.
1513  */
1514 static void
1515 doaddup(int cmd, int satype, char *argv[], char *ebuf)
1516 {
1517 	uint64_t *buffer, *nexthdr;
1518 	struct sadb_msg msg;
1519 	struct sadb_sa *assoc = NULL;
1520 	struct sadb_address *src = NULL, *dst = NULL;
1521 	struct sadb_address *isrc = NULL, *idst = NULL;
1522 	struct sadb_address *natt_local = NULL, *natt_remote = NULL;
1523 	struct sadb_key *encrypt = NULL, *auth = NULL;
1524 	struct sadb_ident *srcid = NULL, *dstid = NULL;
1525 	struct sadb_lifetime *hard = NULL, *soft = NULL;  /* Current? */
1526 	struct sockaddr_in6 *sin6;
1527 	/* MLS TODO:  Need sensitivity eventually. */
1528 	int next, token, sa_len, alloclen, totallen = sizeof (msg), prefix;
1529 	uint32_t spi;
1530 	char *thiscmd, *pstr;
1531 	boolean_t readstate = B_FALSE, unspec_src = B_FALSE;
1532 	boolean_t alloc_inner = B_FALSE, use_natt = B_FALSE;
1533 	struct hostent *srchp = NULL, *dsthp = NULL, *isrchp = NULL,
1534 	    *idsthp = NULL;
1535 	struct hostent *natt_lhp = NULL, *natt_rhp = NULL;
1536 	uint16_t srcport = 0, dstport = 0, natt_lport = 0, natt_rport = 0,
1537 	    isrcport = 0, idstport = 0;
1538 	uint8_t proto = 0, iproto = 0;
1539 	char *ep = NULL;
1540 
1541 	thiscmd = (cmd == CMD_ADD) ? "add" : "update";
1542 
1543 	msg_init(&msg, ((cmd == CMD_ADD) ? SADB_ADD : SADB_UPDATE),
1544 	    (uint8_t)satype);
1545 
1546 	/* Assume last element in argv is set to NULL. */
1547 	do {
1548 		token = parseextval(*argv, &next);
1549 		argv++;
1550 		switch (token) {
1551 		case TOK_EOF:
1552 			/* Do nothing, I'm done. */
1553 			break;
1554 		case TOK_UNKNOWN:
1555 			ERROR1(ep, ebuf, gettext(
1556 			    "Unknown extension field \"%s\" \n"), *(argv - 1));
1557 			break;
1558 		case TOK_SPI:
1559 		case TOK_REPLAY:
1560 		case TOK_STATE:
1561 		case TOK_AUTHALG:
1562 		case TOK_ENCRALG:
1563 		case TOK_ENCAP:
1564 			/*
1565 			 * May want to place this chunk of code in a function.
1566 			 *
1567 			 * This code checks for duplicate entries on a command
1568 			 * line.
1569 			 */
1570 
1571 			/* Allocate the SADB_EXT_SA extension. */
1572 			if (assoc == NULL) {
1573 				assoc = malloc(sizeof (*assoc));
1574 				if (assoc == NULL)
1575 					Bail("malloc(assoc)");
1576 				bzero(assoc, sizeof (*assoc));
1577 				assoc->sadb_sa_exttype = SADB_EXT_SA;
1578 				assoc->sadb_sa_len =
1579 				    SADB_8TO64(sizeof (*assoc));
1580 				totallen += sizeof (*assoc);
1581 			}
1582 			switch (token) {
1583 			case TOK_SPI:
1584 				/*
1585 				 * If some cretin types in "spi 0" then he/she
1586 				 * can type in another SPI.
1587 				 */
1588 				if (assoc->sadb_sa_spi != 0) {
1589 					ERROR(ep, ebuf, gettext(
1590 					    "Can only specify "
1591 					    "single SPI value.\n"));
1592 					break;
1593 				}
1594 				/* Must convert SPI to network order! */
1595 				assoc->sadb_sa_spi =
1596 				    htonl((uint32_t)parsenum(*argv, B_TRUE,
1597 				    ebuf));
1598 				if (assoc->sadb_sa_spi == 0) {
1599 					ERROR(ep, ebuf, gettext(
1600 					    "Invalid SPI value \"0\" .\n"));
1601 				}
1602 				break;
1603 			case TOK_REPLAY:
1604 				/*
1605 				 * That same cretin can do the same with
1606 				 * replay.
1607 				 */
1608 				if (assoc->sadb_sa_replay != 0) {
1609 					ERROR(ep, ebuf, gettext(
1610 					    "Can only specify "
1611 					    "single replay window size.\n"));
1612 					break;
1613 				}
1614 				assoc->sadb_sa_replay =
1615 				    (uint8_t)parsenum(*argv, B_TRUE, ebuf);
1616 				if (assoc->sadb_sa_replay != 0) {
1617 					WARN(ep, ebuf, gettext(
1618 					    "WARNING:  Replay with manual"
1619 					    " keying considered harmful.\n"));
1620 				}
1621 				break;
1622 			case TOK_STATE:
1623 				/*
1624 				 * 0 is an actual state value, LARVAL.  This
1625 				 * means that one can type in the larval state
1626 				 * and then type in another state on the same
1627 				 * command line.
1628 				 */
1629 				if (assoc->sadb_sa_state != 0) {
1630 					ERROR(ep, ebuf, gettext(
1631 					    "Can only specify "
1632 					    "single SA state.\n"));
1633 					break;
1634 				}
1635 				assoc->sadb_sa_state = parsestate(*argv,
1636 				    ebuf);
1637 				readstate = B_TRUE;
1638 				break;
1639 			case TOK_AUTHALG:
1640 				if (assoc->sadb_sa_auth != 0) {
1641 					ERROR(ep, ebuf, gettext(
1642 					    "Can only specify "
1643 					    "single auth algorithm.\n"));
1644 					break;
1645 				}
1646 				assoc->sadb_sa_auth = parsealg(*argv,
1647 				    IPSEC_PROTO_AH, ebuf);
1648 				break;
1649 			case TOK_ENCRALG:
1650 				if (satype == SADB_SATYPE_AH) {
1651 					ERROR(ep, ebuf, gettext("Cannot specify"
1652 					    " encryption with SA type ah.\n"));
1653 					break;
1654 				}
1655 				if (assoc->sadb_sa_encrypt != 0) {
1656 					ERROR(ep, ebuf, gettext(
1657 					    "Can only specify "
1658 					    "single encryption algorithm.\n"));
1659 					break;
1660 				}
1661 				assoc->sadb_sa_encrypt = parsealg(*argv,
1662 				    IPSEC_PROTO_ESP, ebuf);
1663 				break;
1664 			case TOK_ENCAP:
1665 				if (use_natt) {
1666 					ERROR(ep, ebuf, gettext(
1667 					    "Can only specify single"
1668 					    " encapsulation.\n"));
1669 					break;
1670 				}
1671 				if (strncmp(*argv, "udp", 3)) {
1672 					ERROR(ep, ebuf, gettext(
1673 					    "Can only specify udp"
1674 					    " encapsulation.\n"));
1675 					break;
1676 				}
1677 				use_natt = B_TRUE;
1678 				/* set assoc flags later */
1679 				break;
1680 			}
1681 			argv++;
1682 			break;
1683 		case TOK_SRCPORT:
1684 			if (srcport != 0) {
1685 				ERROR(ep, ebuf,  gettext("Can only specify "
1686 				    "single source port.\n"));
1687 				break;
1688 			}
1689 			srcport = parsenum(*argv, B_TRUE, ebuf);
1690 			argv++;
1691 			break;
1692 		case TOK_DSTPORT:
1693 			if (dstport != 0) {
1694 				ERROR(ep, ebuf, gettext("Can only specify "
1695 				    "single destination port.\n"));
1696 				break;
1697 			}
1698 			dstport = parsenum(*argv, B_TRUE, ebuf);
1699 			argv++;
1700 			break;
1701 		case TOK_ISRCPORT:
1702 			alloc_inner = B_TRUE;
1703 			if (isrcport != 0) {
1704 				ERROR(ep, ebuf, gettext(
1705 				    "Can only specify "
1706 				    "single inner-source port.\n"));
1707 				break;
1708 			}
1709 			isrcport = parsenum(*argv, B_TRUE, ebuf);
1710 			argv++;
1711 			break;
1712 		case TOK_IDSTPORT:
1713 			alloc_inner = B_TRUE;
1714 			if (idstport != 0) {
1715 				ERROR(ep, ebuf, gettext(
1716 				    "Can only specify "
1717 				    "single inner-destination port.\n"));
1718 				break;
1719 			}
1720 			idstport = parsenum(*argv, B_TRUE, ebuf);
1721 			argv++;
1722 			break;
1723 		case TOK_NATLPORT:
1724 			if (natt_lport != 0) {
1725 				ERROR(ep, ebuf, gettext(
1726 				    "Can only specify "
1727 				    "single NAT-T local port.\n"));
1728 				break;
1729 			}
1730 
1731 			if (natt_rport != 0) {
1732 				ERROR(ep, ebuf, gettext(
1733 				    "Can only specify "
1734 				    "one of NAT-T remote and local port.\n"));
1735 				break;
1736 			}
1737 			natt_lport = parsenum(*argv, B_TRUE, ebuf);
1738 			argv++;
1739 			break;
1740 		case TOK_NATRPORT:
1741 			if (natt_rport != 0) {
1742 				ERROR(ep, ebuf, gettext(
1743 				    "Can only specify "
1744 				    "single NAT-T remote port.\n"));
1745 				break;
1746 			}
1747 
1748 			if (natt_lport != 0) {
1749 				ERROR(ep, ebuf, gettext(
1750 				    "Can only specify "
1751 				    "one of NAT-T remote and local port.\n"));
1752 				break;
1753 			}
1754 			natt_rport = parsenum(*argv, B_TRUE, ebuf);
1755 			argv++;
1756 			break;
1757 
1758 		case TOK_PROTO:
1759 			if (proto != 0) {
1760 				ERROR(ep, ebuf, gettext(
1761 				    "Can only specify "
1762 				    "single protocol.\n"));
1763 				break;
1764 			}
1765 			proto = parsenum(*argv, B_TRUE, ebuf);
1766 			argv++;
1767 			break;
1768 		case TOK_IPROTO:
1769 			alloc_inner = B_TRUE;
1770 			if (iproto != 0) {
1771 				ERROR(ep, ebuf, gettext(
1772 				    "Can only specify "
1773 				    "single inner protocol.\n"));
1774 				break;
1775 			}
1776 			iproto = parsenum(*argv, B_TRUE, ebuf);
1777 			argv++;
1778 			break;
1779 		case TOK_SRCADDR:
1780 		case TOK_SRCADDR6:
1781 			if (src != NULL) {
1782 				ERROR(ep, ebuf, gettext(
1783 				    "Can only specify "
1784 				    "single source address.\n"));
1785 				break;
1786 			}
1787 			sa_len = parseaddr(*argv, &srchp,
1788 			    (token == TOK_SRCADDR6), ebuf);
1789 			if (srchp == NULL) {
1790 				ERROR1(ep, ebuf, gettext(
1791 				    "Unknown src address \"%s\"\n"), *argv);
1792 				break;
1793 			}
1794 			argv++;
1795 			/*
1796 			 * Round of the sockaddr length to an 8 byte
1797 			 * boundary to make PF_KEY happy.
1798 			 */
1799 			alloclen = sizeof (*src) + roundup(sa_len, 8);
1800 			src = malloc(alloclen);
1801 			if (src == NULL)
1802 				Bail("malloc(src)");
1803 			totallen += alloclen;
1804 			src->sadb_address_len = SADB_8TO64(alloclen);
1805 			src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1806 			src->sadb_address_reserved = 0;
1807 			src->sadb_address_prefixlen = 0;
1808 			src->sadb_address_proto = 0;
1809 			if (srchp == &dummy.he) {
1810 				/*
1811 				 * Single address with -n flag.
1812 				 */
1813 				sin6 = (struct sockaddr_in6 *)(src + 1);
1814 				bzero(sin6, sizeof (*sin6));
1815 				sin6->sin6_family = AF_INET6;
1816 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
1817 				    sizeof (struct in6_addr));
1818 			}
1819 			break;
1820 		case TOK_DSTADDR:
1821 		case TOK_DSTADDR6:
1822 			if (dst != NULL) {
1823 				ERROR(ep, ebuf, gettext(
1824 				    "Can only specify single "
1825 				    "destination address.\n"));
1826 				break;
1827 			}
1828 			sa_len = parseaddr(*argv, &dsthp,
1829 			    (token == TOK_DSTADDR6), ebuf);
1830 			if (dsthp == NULL) {
1831 				ERROR1(ep, ebuf, gettext(
1832 				    "Unknown dst address \"%s\"\n"), *argv);
1833 				break;
1834 			}
1835 			argv++;
1836 			alloclen = sizeof (*dst) + roundup(sa_len, 8);
1837 			dst = malloc(alloclen);
1838 			if (dst == NULL)
1839 				Bail("malloc(dst)");
1840 			totallen += alloclen;
1841 			dst->sadb_address_len = SADB_8TO64(alloclen);
1842 			dst->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1843 			dst->sadb_address_reserved = 0;
1844 			dst->sadb_address_prefixlen = 0;
1845 			dst->sadb_address_proto = 0;
1846 			if (dsthp == &dummy.he) {
1847 				/*
1848 				 * Single address with -n flag.
1849 				 */
1850 				sin6 = (struct sockaddr_in6 *)(dst + 1);
1851 				bzero(sin6, sizeof (*sin6));
1852 				sin6->sin6_family = AF_INET6;
1853 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
1854 				    sizeof (struct in6_addr));
1855 			}
1856 			break;
1857 		case TOK_PROXYADDR:
1858 		case TOK_PROXYADDR6:
1859 			if (isrc != NULL) {
1860 				ERROR(ep, ebuf, gettext(
1861 				    "Can only specify single "
1862 				    "proxy/inner-source address.\n"));
1863 				break;
1864 			}
1865 			if ((pstr = strchr(*argv, '/')) != NULL) {
1866 				/* Parse out the prefix. */
1867 				errno = 0;
1868 				prefix = strtol(pstr + 1, NULL, 10);
1869 				if (errno != 0) {
1870 					ERROR1(ep, ebuf, gettext(
1871 					    "Invalid prefix %s."), pstr);
1872 					break;
1873 				}
1874 				/* Recycle pstr */
1875 				alloclen = (int)(pstr - *argv);
1876 				pstr = malloc(alloclen + 1);
1877 				if (pstr == NULL) {
1878 					Bail("malloc(pstr)");
1879 				}
1880 				(void) strlcpy(pstr, *argv, alloclen + 1);
1881 			} else {
1882 				pstr = *argv;
1883 				/*
1884 				 * Assume mapping to AF_INET6, and we're a host.
1885 				 * XXX some miscreants may still make classful
1886 				 * assumptions.  If this is a problem, fix it
1887 				 * here.
1888 				 */
1889 				prefix = 128;
1890 			}
1891 			sa_len = parseaddr(pstr, &isrchp,
1892 			    (token == TOK_PROXYADDR6), ebuf);
1893 			if (isrchp == NULL) {
1894 				ERROR1(ep, ebuf, gettext(
1895 				    "Unknown proxy/inner-source address "
1896 				    "\"%s\"\n"), *argv);
1897 				break;
1898 			}
1899 			if (pstr != *argv)
1900 				free(pstr);
1901 			argv++;
1902 			alloclen = sizeof (*isrc) + roundup(sa_len, 8);
1903 			isrc = malloc(alloclen);
1904 			if (isrc == NULL)
1905 				Bail("malloc(isrc)");
1906 			totallen += alloclen;
1907 			isrc->sadb_address_len = SADB_8TO64(alloclen);
1908 			isrc->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
1909 			isrc->sadb_address_reserved = 0;
1910 			isrc->sadb_address_prefixlen = prefix;
1911 			isrc->sadb_address_proto = 0;
1912 			if (isrchp == &dummy.he ||
1913 			    isrchp->h_addr_list[1] == NULL) {
1914 				/*
1915 				 * Single address with -n flag or single name.
1916 				 */
1917 				sin6 = (struct sockaddr_in6 *)(isrc + 1);
1918 				bzero(sin6, sizeof (*sin6));
1919 				sin6->sin6_family = AF_INET6;
1920 				bcopy(isrchp->h_addr_list[0], &sin6->sin6_addr,
1921 				    sizeof (struct in6_addr));
1922 				/*
1923 				 * normalize prefixlen for IPv4-mapped
1924 				 * addresses.
1925 				 */
1926 				if (prefix <= 32 &&
1927 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
1928 					isrc->sadb_address_prefixlen += 96;
1929 				alloc_inner = B_TRUE;
1930 			} else {
1931 				/*
1932 				 * If the proxy/isrc address is vague, don't
1933 				 * bother.
1934 				 */
1935 				totallen -= alloclen;
1936 				free(isrc);
1937 				isrc = NULL;
1938 				WARN1(ep, ebuf, gettext(
1939 				    "Proxy/inner-source address %s "
1940 				    "is vague, not using.\n"), isrchp->h_name);
1941 				freehostent(isrchp);
1942 				isrchp = NULL;
1943 				break;
1944 			}
1945 			break;
1946 		case TOK_IDSTADDR:
1947 		case TOK_IDSTADDR6:
1948 			if (idst != NULL) {
1949 				ERROR(ep, ebuf, gettext(
1950 				    "Can only specify single "
1951 				    "inner-destination address.\n"));
1952 				break;
1953 			}
1954 			if ((pstr = strchr(*argv, '/')) != NULL) {
1955 				/* Parse out the prefix. */
1956 				errno = 0;
1957 				prefix = strtol(pstr + 1, NULL, 10);
1958 				if (errno != 0) {
1959 					ERROR1(ep, ebuf, gettext(
1960 					    "Invalid prefix %s.\n"), pstr);
1961 					break;
1962 				}
1963 				/* Recycle pstr */
1964 				alloclen = (int)(pstr - *argv);
1965 				pstr = malloc(alloclen + 1);
1966 				if (pstr == NULL) {
1967 					Bail("malloc(pstr)");
1968 				}
1969 				(void) strlcpy(pstr, *argv, alloclen + 1);
1970 			} else {
1971 				pstr = *argv;
1972 				/*
1973 				 * Assume mapping to AF_INET6, and we're a host.
1974 				 * XXX some miscreants may still make classful
1975 				 * assumptions.  If this is a problem, fix it
1976 				 * here.
1977 				 */
1978 				prefix = 128;
1979 			}
1980 			sa_len = parseaddr(pstr, &idsthp,
1981 			    (token == TOK_IDSTADDR6), ebuf);
1982 			if (idsthp == NULL) {
1983 				ERROR1(ep, ebuf, gettext(
1984 				    "Unknown Inner Src address "
1985 				    " \"%s\"\n"), *argv);
1986 				break;
1987 			}
1988 			if (pstr != *argv)
1989 				free(pstr);
1990 			argv++;
1991 			alloclen = sizeof (*idst) + roundup(sa_len, 8);
1992 			idst = malloc(alloclen);
1993 			if (idst == NULL)
1994 				Bail("malloc(idst)");
1995 			totallen += alloclen;
1996 			idst->sadb_address_len = SADB_8TO64(alloclen);
1997 			idst->sadb_address_exttype =
1998 			    SADB_X_EXT_ADDRESS_INNER_DST;
1999 			idst->sadb_address_reserved = 0;
2000 			idst->sadb_address_prefixlen = prefix;
2001 			idst->sadb_address_proto = 0;
2002 			if (idsthp == &dummy.he ||
2003 			    idsthp->h_addr_list[1] == NULL) {
2004 				/*
2005 				 * Single address with -n flag or single name.
2006 				 */
2007 				sin6 = (struct sockaddr_in6 *)(idst + 1);
2008 				bzero(sin6, sizeof (*sin6));
2009 				sin6->sin6_family = AF_INET6;
2010 				bcopy(idsthp->h_addr_list[0], &sin6->sin6_addr,
2011 				    sizeof (struct in6_addr));
2012 				/*
2013 				 * normalize prefixlen for IPv4-mapped
2014 				 * addresses.
2015 				 */
2016 				if (prefix <= 32 &&
2017 				    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
2018 					idst->sadb_address_prefixlen += 96;
2019 				alloc_inner = B_TRUE;
2020 			} else {
2021 				/*
2022 				 * If the idst address is vague, don't bother.
2023 				 */
2024 				totallen -= alloclen;
2025 				free(idst);
2026 				idst = NULL;
2027 				WARN1(ep, ebuf, gettext(
2028 				    "Inner destination address %s "
2029 				    "is vague, not using.\n"), idsthp->h_name);
2030 				freehostent(idsthp);
2031 				idsthp = NULL;
2032 				break;
2033 			}
2034 			break;
2035 		case TOK_NATLOC:
2036 			if (natt_local != NULL) {
2037 				ERROR(ep, ebuf, gettext(
2038 				    "Can only specify "
2039 				    "single NAT-T local address.\n"));
2040 				break;
2041 			}
2042 			sa_len = parseaddr(*argv, &natt_lhp, 0, ebuf);
2043 			if (natt_lhp == NULL) {
2044 				ERROR1(ep, ebuf, gettext(
2045 				    "Unknown NAT-T local address \"%s\"\n"),
2046 				    *argv);
2047 				break;
2048 			}
2049 			argv++;
2050 			/*
2051 			 * Round of the sockaddr length to an 8 byte
2052 			 * boundary to make PF_KEY happy.
2053 			 */
2054 			alloclen = sizeof (*natt_local) + roundup(sa_len, 8);
2055 			natt_local = malloc(alloclen);
2056 			if (natt_local == NULL)
2057 				Bail("malloc(natt_local)");
2058 			totallen += alloclen;
2059 			natt_local->sadb_address_len = SADB_8TO64(alloclen);
2060 			natt_local->sadb_address_exttype =
2061 			    SADB_X_EXT_ADDRESS_NATT_LOC;
2062 			natt_local->sadb_address_reserved = 0;
2063 			natt_local->sadb_address_prefixlen = 0;
2064 			natt_local->sadb_address_proto = 0;
2065 			if (natt_lhp == &dummy.he ||
2066 			    natt_lhp->h_addr_list[1] == NULL) {
2067 				/*
2068 				 * Single address with -n flag or single name.
2069 				 */
2070 				sin6 = (struct sockaddr_in6 *)(natt_local + 1);
2071 				bzero(sin6, sizeof (*sin6));
2072 				sin6->sin6_family = AF_INET6;
2073 				bcopy(natt_lhp->h_addr_list[0],
2074 				    &sin6->sin6_addr, sizeof (struct in6_addr));
2075 			} else {
2076 				/*
2077 				 * If the nat-local address is vague, don't
2078 				 * bother.
2079 				 */
2080 				totallen -= alloclen;
2081 				free(natt_local);
2082 				natt_local = NULL;
2083 				WARN1(ep, ebuf, gettext(
2084 				    "NAT-T local address %s "
2085 				    "is vague, not using.\n"),
2086 				    natt_lhp->h_name);
2087 				freehostent(natt_lhp);
2088 				natt_lhp = NULL;
2089 				break;
2090 			}
2091 			break;
2092 		case TOK_NATREM:
2093 			if (natt_remote != NULL) {
2094 				ERROR(ep, ebuf, gettext(
2095 				    "Can only specify "
2096 				    "single NAT-T remote address.\n"));
2097 				break;
2098 			}
2099 			sa_len = parseaddr(*argv, &natt_rhp, 0, ebuf);
2100 			if (natt_rhp == NULL) {
2101 				ERROR1(ep, ebuf, gettext(
2102 				    "Unknown NAT-T remote address \"%s\"\n"),
2103 				    *argv);
2104 				break;
2105 			}
2106 			argv++;
2107 			/*
2108 			 * Round of the sockaddr length to an 8 byte
2109 			 * boundary to make PF_KEY happy.
2110 			 */
2111 			alloclen = sizeof (*natt_remote) + roundup(sa_len, 8);
2112 			natt_remote = malloc(alloclen);
2113 			if (natt_remote == NULL)
2114 				Bail("malloc(natt_remote)");
2115 			totallen += alloclen;
2116 			natt_remote->sadb_address_len = SADB_8TO64(alloclen);
2117 			natt_remote->sadb_address_exttype =
2118 			    SADB_X_EXT_ADDRESS_NATT_REM;
2119 			natt_remote->sadb_address_reserved = 0;
2120 			natt_remote->sadb_address_prefixlen = 0;
2121 			natt_remote->sadb_address_proto = 0;
2122 			if (natt_rhp == &dummy.he ||
2123 			    natt_rhp->h_addr_list[1] == NULL) {
2124 				/*
2125 				 * Single address with -n flag or single name.
2126 				 */
2127 				sin6 = (struct sockaddr_in6 *)(natt_remote + 1);
2128 				bzero(sin6, sizeof (*sin6));
2129 				sin6->sin6_family = AF_INET6;
2130 				bcopy(natt_rhp->h_addr_list[0],
2131 				    &sin6->sin6_addr, sizeof (struct in6_addr));
2132 			} else {
2133 				/*
2134 				 * If the nat-renote address is vague, don't
2135 				 * bother.
2136 				 */
2137 				totallen -= alloclen;
2138 				free(natt_remote);
2139 				natt_remote = NULL;
2140 				WARN1(ep, ebuf, gettext(
2141 				    "NAT-T remote address %s "
2142 				    "is vague, not using.\n"),
2143 				    natt_rhp->h_name);
2144 				freehostent(natt_rhp);
2145 				natt_rhp = NULL;
2146 				break;
2147 			}
2148 			break;
2149 		case TOK_ENCRKEY:
2150 			if (encrypt != NULL) {
2151 				ERROR(ep, ebuf, gettext(
2152 				    "Can only specify "
2153 				    "single encryption key.\n"));
2154 				break;
2155 			}
2156 			if (assoc->sadb_sa_encrypt == SADB_EALG_NULL) {
2157 				FATAL(ep, ebuf, gettext(
2158 				    "Cannot specify a key with NULL "
2159 				    "encryption algorithm.\n"));
2160 				break;
2161 			}
2162 			encrypt = parsekey(*argv, ebuf);
2163 			argv++;
2164 			if (encrypt == NULL) {
2165 				ERROR(ep, ebuf, gettext(
2166 				    "Invalid encryption key.\n"));
2167 				break;
2168 			}
2169 			totallen += SADB_64TO8(encrypt->sadb_key_len);
2170 			encrypt->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
2171 			break;
2172 		case TOK_AUTHKEY:
2173 			if (auth != NULL) {
2174 				ERROR(ep, ebuf, gettext(
2175 				    "Can only specify single"
2176 				    " authentication key.\n"));
2177 				break;
2178 			}
2179 			auth = parsekey(*argv, ebuf);
2180 			argv++;
2181 			if (auth == NULL) {
2182 				ERROR(ep, ebuf, gettext(
2183 				    "Invalid authentication key.\n"));
2184 				break;
2185 			}
2186 			totallen += SADB_64TO8(auth->sadb_key_len);
2187 			auth->sadb_key_exttype = SADB_EXT_KEY_AUTH;
2188 			break;
2189 		case TOK_SRCIDTYPE:
2190 			if (*argv == NULL || *(argv + 1) == NULL) {
2191 				FATAL(ep, ebuf, gettext(
2192 				    "Unexpected end of command "
2193 				    "line - Expecting Src Type.\n"));
2194 				/* NOTREACHED */
2195 				break;
2196 			}
2197 			if (srcid != NULL) {
2198 				ERROR(ep, ebuf, gettext(
2199 				    "Can only specify single"
2200 				    " source certificate identity.\n"));
2201 				break;
2202 			}
2203 			alloclen = sizeof (*srcid) +
2204 			    roundup(strlen(*(argv + 1)) + 1, 8);
2205 			srcid = malloc(alloclen);
2206 			if (srcid == NULL)
2207 				Bail("malloc(srcid)");
2208 			totallen += alloclen;
2209 			srcid->sadb_ident_type = parseidtype(*argv, ebuf);
2210 			argv++;
2211 			srcid->sadb_ident_len = SADB_8TO64(alloclen);
2212 			srcid->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC;
2213 			srcid->sadb_ident_reserved = 0;
2214 			srcid->sadb_ident_id = 0;  /* Not useful here. */
2215 			(void) strlcpy((char *)(srcid + 1), *argv, alloclen);
2216 			argv++;
2217 			break;
2218 		case TOK_DSTIDTYPE:
2219 			if (*argv == NULL || *(argv + 1) == NULL) {
2220 				ERROR(ep, ebuf, gettext(
2221 				    "Unexpected end of command"
2222 				    " line - expecting dst type.\n"));
2223 				break;
2224 			}
2225 			if (dstid != NULL) {
2226 				ERROR(ep, ebuf, gettext(
2227 				    "Can only specify single destination "
2228 				    "certificate identity.\n"));
2229 				break;
2230 			}
2231 			alloclen = sizeof (*dstid) +
2232 			    roundup(strlen(*(argv + 1)) + 1, 8);
2233 			dstid = malloc(alloclen);
2234 			if (dstid == NULL)
2235 				Bail("malloc(dstid)");
2236 			totallen += alloclen;
2237 			dstid->sadb_ident_type = parseidtype(*argv, ebuf);
2238 			argv++;
2239 			dstid->sadb_ident_len = SADB_8TO64(alloclen);
2240 			dstid->sadb_ident_exttype = SADB_EXT_IDENTITY_DST;
2241 			dstid->sadb_ident_reserved = 0;
2242 			dstid->sadb_ident_id = 0;  /* Not useful here. */
2243 			(void) strlcpy((char *)(dstid + 1), *argv, alloclen);
2244 			argv++;
2245 			break;
2246 		case TOK_HARD_ALLOC:
2247 		case TOK_HARD_BYTES:
2248 		case TOK_HARD_ADDTIME:
2249 		case TOK_HARD_USETIME:
2250 			if (hard == NULL) {
2251 				hard = malloc(sizeof (*hard));
2252 				if (hard == NULL)
2253 					Bail("malloc(hard_lifetime)");
2254 				bzero(hard, sizeof (*hard));
2255 				hard->sadb_lifetime_exttype =
2256 				    SADB_EXT_LIFETIME_HARD;
2257 				hard->sadb_lifetime_len =
2258 				    SADB_8TO64(sizeof (*hard));
2259 				totallen += sizeof (*hard);
2260 			}
2261 			switch (token) {
2262 			case TOK_HARD_ALLOC:
2263 				if (hard->sadb_lifetime_allocations != 0) {
2264 					ERROR(ep, ebuf, gettext(
2265 					    "Can only specify single"
2266 					    " hard allocation limit.\n"));
2267 					break;
2268 				}
2269 				hard->sadb_lifetime_allocations =
2270 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
2271 				break;
2272 			case TOK_HARD_BYTES:
2273 				if (hard->sadb_lifetime_bytes != 0) {
2274 					ERROR(ep, ebuf, gettext(
2275 					    "Can only specify "
2276 					    "single hard byte limit.\n"));
2277 					break;
2278 				}
2279 				hard->sadb_lifetime_bytes = parsenum(*argv,
2280 				    B_TRUE, ebuf);
2281 				break;
2282 			case TOK_HARD_ADDTIME:
2283 				if (hard->sadb_lifetime_addtime != 0) {
2284 					ERROR(ep, ebuf, gettext(
2285 					    "Can only specify "
2286 					    "single past-add lifetime.\n"));
2287 					break;
2288 				}
2289 				hard->sadb_lifetime_addtime = parsenum(*argv,
2290 				    B_TRUE, ebuf);
2291 				break;
2292 			case TOK_HARD_USETIME:
2293 				if (hard->sadb_lifetime_usetime != 0) {
2294 					ERROR(ep, ebuf, gettext(
2295 					    "Can only specify "
2296 					    "single past-use lifetime.\n"));
2297 					break;
2298 				}
2299 				hard->sadb_lifetime_usetime = parsenum(*argv,
2300 				    B_TRUE, ebuf);
2301 				break;
2302 			}
2303 			argv++;
2304 			break;
2305 		case TOK_SOFT_ALLOC:
2306 		case TOK_SOFT_BYTES:
2307 		case TOK_SOFT_ADDTIME:
2308 		case TOK_SOFT_USETIME:
2309 			if (soft == NULL) {
2310 				soft = malloc(sizeof (*soft));
2311 				if (soft == NULL)
2312 					Bail("malloc(soft_lifetime)");
2313 				bzero(soft, sizeof (*soft));
2314 				soft->sadb_lifetime_exttype =
2315 				    SADB_EXT_LIFETIME_SOFT;
2316 				soft->sadb_lifetime_len =
2317 				    SADB_8TO64(sizeof (*soft));
2318 				totallen += sizeof (*soft);
2319 			}
2320 			switch (token) {
2321 			case TOK_SOFT_ALLOC:
2322 				if (soft->sadb_lifetime_allocations != 0) {
2323 					ERROR(ep, ebuf, gettext(
2324 					    "Can only specify single"
2325 					    " soft allocation limit.\n"));
2326 					break;
2327 				}
2328 				soft->sadb_lifetime_allocations =
2329 				    (uint32_t)parsenum(*argv, B_TRUE, ebuf);
2330 				break;
2331 			case TOK_SOFT_BYTES:
2332 				if (soft->sadb_lifetime_bytes != 0) {
2333 					ERROR(ep, ebuf, gettext(
2334 					    "Can only specify single"
2335 					    " soft byte limit.\n"));
2336 					break;
2337 				}
2338 				soft->sadb_lifetime_bytes = parsenum(*argv,
2339 				    B_TRUE, ebuf);
2340 				break;
2341 			case TOK_SOFT_ADDTIME:
2342 				if (soft->sadb_lifetime_addtime != 0) {
2343 					ERROR(ep, ebuf, gettext(
2344 					    "Can only specify single"
2345 					    " past-add lifetime.\n"));
2346 					break;
2347 				}
2348 				soft->sadb_lifetime_addtime = parsenum(*argv,
2349 				    B_TRUE, ebuf);
2350 				break;
2351 			case TOK_SOFT_USETIME:
2352 				if (soft->sadb_lifetime_usetime != 0) {
2353 					ERROR(ep, ebuf, gettext(
2354 					    "Can only specify single"
2355 					    " past-use lifetime.\n"));
2356 					break;
2357 				}
2358 				soft->sadb_lifetime_usetime = parsenum(*argv,
2359 				    B_TRUE, ebuf);
2360 				break;
2361 			}
2362 			argv++;
2363 			break;
2364 		default:
2365 			ERROR1(ep, ebuf, gettext(
2366 			    "Don't use extension %s for add/update.\n"),
2367 			    *(argv - 1));
2368 			break;
2369 		}
2370 	} while (token != TOK_EOF);
2371 
2372 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
2373 
2374 	/*
2375 	 * If we specify inner ports w/o addresses, we still need to
2376 	 * allocate.  Also, if we have one inner address, we need the
2377 	 * other, even if we don't specify anything.
2378 	 */
2379 	if (alloc_inner && idst == NULL) {
2380 		/* Allocate zeroed-out. */
2381 		alloclen = sizeof (*idst) + sizeof (struct sockaddr_in6);
2382 		idst = calloc(1, alloclen);
2383 		if (idst == NULL) {
2384 			Bail("malloc(implicit idst)");
2385 		}
2386 		totallen += alloclen;
2387 		idst->sadb_address_len = SADB_8TO64(alloclen);
2388 		idst->sadb_address_exttype = SADB_X_EXT_ADDRESS_INNER_DST;
2389 		sin6 = (struct sockaddr_in6 *)(idst + 1);
2390 		sin6->sin6_family = AF_INET6;
2391 	}
2392 
2393 	if (alloc_inner && isrc == NULL) {
2394 		/* Allocate zeroed-out. */
2395 		alloclen = sizeof (*isrc) + sizeof (struct sockaddr_in6);
2396 		isrc = calloc(1, alloclen);
2397 		if (isrc == NULL) {
2398 			Bail("malloc(implicit isrc)");
2399 		}
2400 		totallen += alloclen;
2401 		isrc->sadb_address_len = SADB_8TO64(alloclen);
2402 		isrc->sadb_address_exttype = SADB_X_EXT_ADDRESS_INNER_SRC;
2403 		sin6 = (struct sockaddr_in6 *)(isrc + 1);
2404 		sin6->sin6_family = AF_INET6;
2405 	}
2406 
2407 	/*
2408 	 * Okay, so now I have all of the potential extensions!
2409 	 * Allocate a single contiguous buffer.  Keep in mind that it'll
2410 	 * be enough because the key itself will be yanked.
2411 	 */
2412 
2413 	if (src == NULL && dst != NULL) {
2414 		/*
2415 		 * Set explicit unspecified source address.
2416 		 */
2417 		size_t lenbytes = SADB_64TO8(dst->sadb_address_len);
2418 
2419 		unspec_src = B_TRUE;
2420 		totallen += lenbytes;
2421 		src = malloc(lenbytes);
2422 		if (src == NULL)
2423 			Bail("malloc(implicit src)");
2424 		/* Confusing, but we're copying from DST to SRC.  :) */
2425 		bcopy(dst, src, lenbytes);
2426 		src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2427 		sin6 = (struct sockaddr_in6 *)(src + 1);
2428 		bzero(sin6, sizeof (*sin6));
2429 		sin6->sin6_family = AF_INET6;
2430 	}
2431 
2432 	msg.sadb_msg_len = SADB_8TO64(totallen);
2433 
2434 	buffer = malloc(totallen);
2435 	nexthdr = buffer;
2436 	bcopy(&msg, nexthdr, sizeof (msg));
2437 	nexthdr += SADB_8TO64(sizeof (msg));
2438 	if (assoc != NULL) {
2439 		if (assoc->sadb_sa_spi == 0) {
2440 			ERROR1(ep, ebuf, gettext(
2441 			    "The SPI value is missing for "
2442 			    "the association you wish to %s.\n"), thiscmd);
2443 		}
2444 		if (assoc->sadb_sa_auth == 0 && assoc->sadb_sa_encrypt == 0 &&
2445 		    cmd == CMD_ADD) {
2446 			free(assoc);
2447 			FATAL(ep, ebuf, gettext(
2448 			    "Select at least one algorithm "
2449 			    "for this add.\n"));
2450 		}
2451 
2452 		/* Hack to let user specify NULL ESP implicitly. */
2453 		if (msg.sadb_msg_satype == SADB_SATYPE_ESP &&
2454 		    assoc->sadb_sa_encrypt == 0)
2455 			assoc->sadb_sa_encrypt = SADB_EALG_NULL;
2456 
2457 		/* 0 is an actual value.  Print a warning if it was entered. */
2458 		if (assoc->sadb_sa_state == 0) {
2459 			if (readstate) {
2460 				ERROR(ep, ebuf, gettext(
2461 				    "WARNING: Cannot set LARVAL SA state.\n"));
2462 			}
2463 			assoc->sadb_sa_state = SADB_SASTATE_MATURE;
2464 		}
2465 
2466 		if (use_natt) {
2467 			if (natt_remote != NULL)
2468 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_REM;
2469 			if (natt_local != NULL)
2470 				assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_LOC;
2471 		}
2472 
2473 		if (alloc_inner) {
2474 			/*
2475 			 * For now, assume RFC 3884's dream of transport-mode
2476 			 * SAs with inner IP address selectors will not
2477 			 * happen.
2478 			 */
2479 			assoc->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL;
2480 			if (proto != 0 && proto != IPPROTO_ENCAP &&
2481 			    proto != IPPROTO_IPV6) {
2482 				ERROR1(ep, ebuf, gettext(
2483 				    "WARNING: Protocol type %d not "
2484 				    "for use with Tunnel-Mode SA.\n"), proto);
2485 				/* Continue and let PF_KEY scream... */
2486 			}
2487 		}
2488 
2489 		bcopy(assoc, nexthdr, SADB_64TO8(assoc->sadb_sa_len));
2490 		nexthdr += assoc->sadb_sa_len;
2491 		/* Save the SPI for the case of an error. */
2492 		spi = assoc->sadb_sa_spi;
2493 		free(assoc);
2494 	} else {
2495 		ERROR1(ep, ebuf, gettext(
2496 		    "Need SA parameters for %s.\n"), thiscmd);
2497 	}
2498 
2499 	if (hard != NULL) {
2500 		bcopy(hard, nexthdr, SADB_64TO8(hard->sadb_lifetime_len));
2501 		nexthdr += hard->sadb_lifetime_len;
2502 		free(hard);
2503 	}
2504 
2505 	if (soft != NULL) {
2506 		bcopy(soft, nexthdr, SADB_64TO8(soft->sadb_lifetime_len));
2507 		nexthdr += soft->sadb_lifetime_len;
2508 		free(soft);
2509 	}
2510 
2511 	if (encrypt == NULL && auth == NULL && cmd == CMD_ADD) {
2512 		ERROR(ep, ebuf, gettext(
2513 		    "Must have at least one key for an add.\n"));
2514 	}
2515 
2516 	if (encrypt != NULL) {
2517 		bcopy(encrypt, nexthdr, SADB_64TO8(encrypt->sadb_key_len));
2518 		nexthdr += encrypt->sadb_key_len;
2519 		bzero(encrypt, SADB_64TO8(encrypt->sadb_key_len));
2520 		free(encrypt);
2521 	}
2522 
2523 	if (auth != NULL) {
2524 		bcopy(auth, nexthdr, SADB_64TO8(auth->sadb_key_len));
2525 		nexthdr += auth->sadb_key_len;
2526 		bzero(auth, SADB_64TO8(auth->sadb_key_len));
2527 		free(auth);
2528 	}
2529 
2530 	if (srcid != NULL) {
2531 		bcopy(srcid, nexthdr, SADB_64TO8(srcid->sadb_ident_len));
2532 		nexthdr += srcid->sadb_ident_len;
2533 		free(srcid);
2534 	}
2535 
2536 	if (dstid != NULL) {
2537 		bcopy(dstid, nexthdr, SADB_64TO8(dstid->sadb_ident_len));
2538 		nexthdr += dstid->sadb_ident_len;
2539 		free(dstid);
2540 	}
2541 
2542 	if (dst != NULL) {
2543 		bcopy(dst, nexthdr, SADB_64TO8(dst->sadb_address_len));
2544 		free(dst);
2545 		dst = (struct sadb_address *)nexthdr;
2546 		dst->sadb_address_proto = proto;
2547 		((struct sockaddr_in6 *)(dst + 1))->sin6_port = htons(dstport);
2548 		nexthdr += dst->sadb_address_len;
2549 	} else {
2550 		FATAL1(ep, ebuf, gettext(
2551 		    "Need destination address for %s.\n"), thiscmd);
2552 	}
2553 
2554 	if (use_natt) {
2555 		if (natt_remote == NULL && natt_local == NULL) {
2556 			ERROR(ep, ebuf, gettext(
2557 			    "Must specify NAT-T remote or local address "
2558 			    "for UDP encapsulation.\n"));
2559 		}
2560 
2561 		if (natt_lport != 0 && natt_local == NULL) {
2562 			ERROR(ep, ebuf, gettext(
2563 			    "If NAT-T local port is specified, NAT-T "
2564 			    "local address must also be specified.\n"));
2565 		}
2566 
2567 		if (natt_rport != 0 && natt_remote == NULL) {
2568 			ERROR(ep, ebuf, gettext(
2569 			    "If NAT-T remote port is specified, NAT-T "
2570 			    "remote address must also be specified.\n"));
2571 		}
2572 
2573 		if (natt_remote != NULL) {
2574 			bcopy(natt_remote, nexthdr,
2575 			    SADB_64TO8(natt_remote->sadb_address_len));
2576 			free(natt_remote);
2577 			natt_remote = (struct sadb_address *)nexthdr;
2578 			nexthdr += natt_remote->sadb_address_len;
2579 			((struct sockaddr_in6 *)(natt_remote + 1))->sin6_port =
2580 			    htons(natt_rport);
2581 		}
2582 
2583 		if (natt_local != NULL) {
2584 			bcopy(natt_local, nexthdr,
2585 			    SADB_64TO8(natt_local->sadb_address_len));
2586 			free(natt_local);
2587 			natt_local = (struct sadb_address *)nexthdr;
2588 			nexthdr += natt_local->sadb_address_len;
2589 			((struct sockaddr_in6 *)(natt_local + 1))->sin6_port =
2590 			    htons(natt_lport);
2591 		}
2592 	}
2593 
2594 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
2595 
2596 	/*
2597 	 * PF_KEY requires a source address extension, even if the source
2598 	 * address itself is unspecified. (See "Set explicit unspecified..."
2599 	 * code fragment above. Destination reality check was above.)
2600 	 */
2601 	bcopy(src, nexthdr, SADB_64TO8(src->sadb_address_len));
2602 	free(src);
2603 	src = (struct sadb_address *)nexthdr;
2604 	src->sadb_address_proto = proto;
2605 	((struct sockaddr_in6 *)(src + 1))->sin6_port = htons(srcport);
2606 	nexthdr += src->sadb_address_len;
2607 
2608 	if (isrc != NULL) {
2609 		bcopy(isrc, nexthdr, SADB_64TO8(isrc->sadb_address_len));
2610 		free(isrc);
2611 		isrc = (struct sadb_address *)nexthdr;
2612 		isrc->sadb_address_proto = iproto;
2613 		((struct sockaddr_in6 *)(isrc + 1))->sin6_port =
2614 		    htons(isrcport);
2615 		nexthdr += isrc->sadb_address_len;
2616 	}
2617 
2618 	if (idst != NULL) {
2619 		bcopy(idst, nexthdr, SADB_64TO8(idst->sadb_address_len));
2620 		free(idst);
2621 		idst = (struct sadb_address *)nexthdr;
2622 		idst->sadb_address_proto = iproto;
2623 		((struct sockaddr_in6 *)(idst + 1))->sin6_port =
2624 		    htons(idstport);
2625 		nexthdr += idst->sadb_address_len;
2626 	}
2627 
2628 	if (cflag) {
2629 		/*
2630 		 * Assume the checked cmd would have worked if it was actually
2631 		 * used. doaddresses() will increment lines_added if it
2632 		 * succeeds.
2633 		 */
2634 		lines_added++;
2635 	} else {
2636 		doaddresses((cmd == CMD_ADD) ? SADB_ADD : SADB_UPDATE, satype,
2637 		    cmd, srchp, dsthp, src, dst, unspec_src, buffer, totallen,
2638 		    spi, ebuf);
2639 	}
2640 
2641 	if (isrchp != NULL && isrchp != &dummy.he)
2642 		freehostent(isrchp);
2643 	if (idsthp != NULL && idsthp != &dummy.he)
2644 		freehostent(idsthp);
2645 	if (srchp != NULL && srchp != &dummy.he)
2646 		freehostent(srchp);
2647 	if (dsthp != NULL && dsthp != &dummy.he)
2648 		freehostent(dsthp);
2649 	if (natt_lhp != NULL && natt_lhp != &dummy.he)
2650 		freehostent(natt_lhp);
2651 	if (natt_rhp != NULL && natt_rhp != &dummy.he)
2652 		freehostent(natt_rhp);
2653 
2654 	free(ebuf);
2655 	free(buffer);
2656 }
2657 
2658 /*
2659  * DELETE and GET are similar, in that they only need the extensions
2660  * required to _find_ an SA, and then either delete it or obtain its
2661  * information.
2662  */
2663 static void
2664 dodelget(int cmd, int satype, char *argv[], char *ebuf)
2665 {
2666 	struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
2667 	uint64_t *nextext;
2668 	struct sadb_sa *assoc = NULL;
2669 	struct sadb_address *src = NULL, *dst = NULL;
2670 	int next, token, sa_len;
2671 	char *thiscmd;
2672 	uint32_t spi;
2673 	struct hostent *srchp = NULL, *dsthp = NULL;
2674 	struct sockaddr_in6 *sin6;
2675 	boolean_t unspec_src = B_TRUE;
2676 	uint16_t srcport = 0, dstport = 0;
2677 	uint8_t proto = 0;
2678 	char *ep = NULL;
2679 
2680 	msg_init(msg, ((cmd == CMD_GET) ? SADB_GET : SADB_DELETE),
2681 	    (uint8_t)satype);
2682 	/* Set the first extension header to right past the base message. */
2683 	nextext = (uint64_t *)(msg + 1);
2684 	bzero(nextext, sizeof (get_buffer) - sizeof (*msg));
2685 
2686 	thiscmd = (cmd == CMD_GET) ? "get" : "delete";
2687 
2688 #define	ALLOC_ADDR_EXT(ext, exttype)			\
2689 	(ext) = (struct sadb_address *)nextext;		\
2690 	nextext = (uint64_t *)((ext) + 1);		\
2691 	nextext += SADB_8TO64(roundup(sa_len, 8));	\
2692 	(ext)->sadb_address_exttype = exttype;		\
2693 	(ext)->sadb_address_len = nextext - ((uint64_t *)ext);
2694 
2695 	/* Assume last element in argv is set to NULL. */
2696 	do {
2697 		token = parseextval(*argv, &next);
2698 		argv++;
2699 		switch (token) {
2700 		case TOK_EOF:
2701 			/* Do nothing, I'm done. */
2702 			break;
2703 		case TOK_UNKNOWN:
2704 			ERROR1(ep, ebuf, gettext(
2705 			    "Unknown extension field \"%s\"\n"), *(argv - 1));
2706 			break;
2707 		case TOK_SPI:
2708 			if (assoc != NULL) {
2709 				ERROR(ep, ebuf, gettext(
2710 				    "Can only specify single SPI value.\n"));
2711 				break;
2712 			}
2713 			assoc = (struct sadb_sa *)nextext;
2714 			nextext = (uint64_t *)(assoc + 1);
2715 			assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc));
2716 			assoc->sadb_sa_exttype = SADB_EXT_SA;
2717 			assoc->sadb_sa_spi = htonl((uint32_t)parsenum(*argv,
2718 			    B_TRUE, ebuf));
2719 			spi = assoc->sadb_sa_spi;
2720 			argv++;
2721 			break;
2722 		case TOK_SRCPORT:
2723 			if (srcport != 0) {
2724 				ERROR(ep, ebuf, gettext(
2725 				    "Can only specify single source port.\n"));
2726 				break;
2727 			}
2728 			srcport = parsenum(*argv, B_TRUE, ebuf);
2729 			argv++;
2730 			break;
2731 		case TOK_DSTPORT:
2732 			if (dstport != 0) {
2733 				ERROR(ep, ebuf, gettext(
2734 				    "Can only "
2735 				    "specify single destination port.\n"));
2736 				break;
2737 			}
2738 			dstport = parsenum(*argv, B_TRUE, ebuf);
2739 			argv++;
2740 			break;
2741 		case TOK_PROTO:
2742 			if (proto != 0) {
2743 				ERROR(ep, ebuf, gettext(
2744 				    "Can only specify single protocol.\n"));
2745 				break;
2746 			}
2747 			proto = parsenum(*argv, B_TRUE, ebuf);
2748 			argv++;
2749 			break;
2750 		case TOK_SRCADDR:
2751 		case TOK_SRCADDR6:
2752 			if (src != NULL) {
2753 				ERROR(ep, ebuf, gettext(
2754 				    "Can only specify single source addr.\n"));
2755 				break;
2756 			}
2757 			sa_len = parseaddr(*argv, &srchp,
2758 			    (token == TOK_SRCADDR6), ebuf);
2759 			if (srchp == NULL) {
2760 				ERROR1(ep, ebuf, gettext(
2761 				    "Unknown source address \"%s\"\n"), *argv);
2762 				break;
2763 			}
2764 			argv++;
2765 
2766 			unspec_src = B_FALSE;
2767 
2768 			ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
2769 
2770 			if (srchp == &dummy.he) {
2771 				/*
2772 				 * Single address with -n flag.
2773 				 */
2774 				sin6 = (struct sockaddr_in6 *)(src + 1);
2775 				bzero(sin6, sizeof (*sin6));
2776 				sin6->sin6_family = AF_INET6;
2777 				bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
2778 				    sizeof (struct in6_addr));
2779 			}
2780 			/* The rest is pre-bzeroed for us. */
2781 			break;
2782 		case TOK_DSTADDR:
2783 		case TOK_DSTADDR6:
2784 			if (dst != NULL) {
2785 				ERROR(ep, ebuf, gettext(
2786 				    "Can only specify single destination "
2787 				    "address.\n"));
2788 				break;
2789 			}
2790 			sa_len = parseaddr(*argv, &dsthp,
2791 			    (token == TOK_SRCADDR6), ebuf);
2792 			if (dsthp == NULL) {
2793 				ERROR1(ep, ebuf, gettext(
2794 				    "Unknown destination address \"%s\"\n"),
2795 				    *argv);
2796 				break;
2797 			}
2798 			argv++;
2799 
2800 			ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
2801 
2802 			if (dsthp == &dummy.he) {
2803 				/*
2804 				 * Single address with -n flag.
2805 				 */
2806 				sin6 = (struct sockaddr_in6 *)(dst + 1);
2807 				bzero(sin6, sizeof (*sin6));
2808 				sin6->sin6_family = AF_INET6;
2809 				bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
2810 				    sizeof (struct in6_addr));
2811 			}
2812 			/* The rest is pre-bzeroed for us. */
2813 			break;
2814 		default:
2815 			ERROR2(ep, ebuf, gettext(
2816 			    "Don't use extension %s for '%s' command.\n"),
2817 			    *(argv - 1), thiscmd);
2818 			break;
2819 		}
2820 	} while (token != TOK_EOF);
2821 
2822 	handle_errors(ep, ebuf, B_TRUE, B_FALSE);
2823 
2824 	if ((srcport != 0) && (src == NULL)) {
2825 		ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
2826 		sin6 = (struct sockaddr_in6 *)(src + 1);
2827 		src->sadb_address_proto = proto;
2828 		bzero(sin6, sizeof (*sin6));
2829 		sin6->sin6_family = AF_INET6;
2830 		sin6->sin6_port = htons(srcport);
2831 	}
2832 
2833 	if ((dstport != 0) && (dst == NULL)) {
2834 		ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
2835 		sin6 = (struct sockaddr_in6 *)(dst + 1);
2836 		src->sadb_address_proto = proto;
2837 		bzero(sin6, sizeof (*sin6));
2838 		sin6->sin6_family = AF_INET6;
2839 		sin6->sin6_port = htons(dstport);
2840 	}
2841 
2842 	/* So I have enough of the message to send it down! */
2843 	msg->sadb_msg_len = nextext - get_buffer;
2844 
2845 	if (assoc == NULL) {
2846 		FATAL1(ep, ebuf, gettext(
2847 		    "Need SA parameters for %s.\n"), thiscmd);
2848 	}
2849 
2850 	if (cflag) {
2851 		/*
2852 		 * Assume the checked cmd would have worked if it was actually
2853 		 * used. doaddresses() will increment lines_added if it
2854 		 * succeeds.
2855 		 */
2856 		lines_added++;
2857 	} else {
2858 		doaddresses((cmd == CMD_GET) ? SADB_GET : SADB_DELETE, satype,
2859 		    cmd, srchp, dsthp, src, dst, unspec_src, get_buffer,
2860 		    sizeof (get_buffer), spi, NULL);
2861 	}
2862 
2863 	if (srchp != NULL && srchp != &dummy.he)
2864 		freehostent(srchp);
2865 	if (dsthp != NULL && dsthp != &dummy.he)
2866 		freehostent(dsthp);
2867 }
2868 
2869 /*
2870  * "ipseckey monitor" should exit very gracefully if ^C is tapped.
2871  */
2872 static void
2873 monitor_catch(int signal)
2874 {
2875 	errx(signal, gettext("Bailing on signal %d."), signal);
2876 }
2877 
2878 /*
2879  * Loop forever, listening on PF_KEY messages.
2880  */
2881 static void
2882 domonitor(boolean_t passive)
2883 {
2884 	struct sadb_msg *samsg;
2885 	int rc;
2886 
2887 	/* Catch ^C. */
2888 	(void) signal(SIGINT, monitor_catch);
2889 
2890 	samsg = (struct sadb_msg *)get_buffer;
2891 	if (!passive) {
2892 		(void) printf(gettext("Actively"));
2893 		msg_init(samsg, SADB_X_PROMISC, 1);	/* Turn ON promisc. */
2894 		rc = key_write(keysock, samsg, sizeof (*samsg));
2895 		if (rc == -1)
2896 			Bail("write (SADB_X_PROMISC)");
2897 	} else {
2898 		(void) printf(gettext("Passively"));
2899 	}
2900 	(void) printf(gettext(" monitoring the PF_KEY socket.\n"));
2901 
2902 	for (; ; ) {
2903 		/*
2904 		 * I assume that read() is non-blocking, and will never
2905 		 * return 0.
2906 		 */
2907 		rc = read(keysock, samsg, sizeof (get_buffer));
2908 		if (rc == -1)
2909 			Bail("read (in domonitor)");
2910 		(void) printf(gettext("Read %d bytes.\n"), rc);
2911 		/*
2912 		 * Q:  Should I use the same method of printing as GET does?
2913 		 * A:  For now, yes.
2914 		 */
2915 		print_samsg(stdout, get_buffer, B_TRUE, vflag, nflag);
2916 		(void) putchar('\n');
2917 	}
2918 }
2919 
2920 /*
2921  * Either mask or unmask all relevant signals.
2922  */
2923 static void
2924 mask_signals(boolean_t unmask)
2925 {
2926 	sigset_t set;
2927 	static sigset_t oset;
2928 
2929 	if (unmask) {
2930 		(void) sigprocmask(SIG_SETMASK, &oset, NULL);
2931 	} else {
2932 		(void) sigfillset(&set);
2933 		(void) sigprocmask(SIG_SETMASK, &set, &oset);
2934 	}
2935 }
2936 
2937 /*
2938  * Assorted functions to print help text.
2939  */
2940 #define	puts_tr(s) (void) puts(gettext(s))
2941 
2942 static void
2943 doattrhelp()
2944 {
2945 	int i;
2946 
2947 	puts_tr("\nSA attributes:");
2948 
2949 	for (i = 0; tokens[i].string != NULL; i++) {
2950 		if (i%3 == 0)
2951 			(void) printf("\n");
2952 		(void) printf("    %-15.15s", tokens[i].string);
2953 	}
2954 	(void) printf("\n");
2955 }
2956 
2957 static void
2958 dohelpcmd(char *cmds)
2959 {
2960 	int cmd;
2961 
2962 	if (strcmp(cmds, "attr") == 0) {
2963 		doattrhelp();
2964 		return;
2965 	}
2966 
2967 	cmd = parsecmd(cmds);
2968 	switch (cmd) {
2969 	case CMD_UPDATE:
2970 		puts_tr("update	 - Update an existing SA");
2971 		break;
2972 	case CMD_ADD:
2973 		puts_tr("add	 - Add a new security association (SA)");
2974 		break;
2975 	case CMD_DELETE:
2976 		puts_tr("delete - Delete an SA");
2977 		break;
2978 	case CMD_GET:
2979 		puts_tr("get - Display an SA");
2980 		break;
2981 	case CMD_FLUSH:
2982 		puts_tr("flush - Delete all SAs");
2983 		break;
2984 	case CMD_DUMP:
2985 		puts_tr("dump - Display all SAs");
2986 		break;
2987 	case CMD_MONITOR:
2988 		puts_tr("monitor - Monitor all PF_KEY reply messages.");
2989 		break;
2990 	case CMD_PMONITOR:
2991 		puts_tr(
2992 "pmonitor, passive_monitor - Monitor PF_KEY messages that");
2993 		puts_tr(
2994 "                            reply to all PF_KEY sockets.");
2995 		break;
2996 
2997 	case CMD_QUIT:
2998 		puts_tr("quit, exit - Exit the program");
2999 		break;
3000 	case CMD_SAVE:
3001 		puts_tr("save	    - Saves all SAs to a file");
3002 		break;
3003 	case CMD_HELP:
3004 		puts_tr("help	    - Display list of commands");
3005 		puts_tr("help <cmd> - Display help for command");
3006 		puts_tr("help attr  - Display possible SA attributes");
3007 		break;
3008 	default:
3009 		(void) printf(gettext("%s: Unknown command\n"), cmds);
3010 		break;
3011 	}
3012 }
3013 
3014 
3015 static void
3016 dohelp(char *cmds)
3017 {
3018 	if (cmds != NULL) {
3019 		dohelpcmd(cmds);
3020 		return;
3021 	}
3022 	puts_tr("Commands");
3023 	puts_tr("--------");
3024 	puts_tr("?, help  - Display this list");
3025 	puts_tr("help <cmd> - Display help for command");
3026 	puts_tr("help attr  - Display possible SA attributes");
3027 	puts_tr("quit, exit - Exit the program");
3028 	puts_tr("monitor - Monitor all PF_KEY reply messages.");
3029 	puts_tr("pmonitor, passive_monitor - Monitor PF_KEY messages that");
3030 	puts_tr("                            reply to all PF_KEY sockets.");
3031 	puts_tr("");
3032 	puts_tr("The following commands are of the form:");
3033 	puts_tr("    <command> {SA type} {attribute value}*");
3034 	puts_tr("");
3035 	puts_tr("add (interactive only) - Add a new security association (SA)");
3036 	puts_tr("update (interactive only) - Update an existing SA");
3037 	puts_tr("delete - Delete an SA");
3038 	puts_tr("get - Display an SA");
3039 	puts_tr("flush - Delete all SAs");
3040 	puts_tr("dump - Display all SAs");
3041 	puts_tr("save - Saves all SAs to a file");
3042 }
3043 
3044 /*
3045  * "Parse" a command line from argv.
3046  */
3047 static void
3048 parseit(int argc, char *argv[], char *ebuf, boolean_t read_cmdfile)
3049 {
3050 	int cmd, satype;
3051 	char *ep = NULL;
3052 
3053 	if (argc == 0)
3054 		return;
3055 	cmd = parsecmd(*argv++);
3056 
3057 	/*
3058 	 * Some commands loop forever and should only be run from the command
3059 	 * line, they should never be run from a command file as this may
3060 	 * be used at boot time.
3061 	 */
3062 	switch (cmd) {
3063 	case CMD_HELP:
3064 		if (read_cmdfile)
3065 			ERROR(ep, ebuf, gettext("Help not appropriate in "
3066 			    "config file."));
3067 		else
3068 			dohelp(*argv);
3069 		return;
3070 	case CMD_MONITOR:
3071 		if (read_cmdfile)
3072 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
3073 			    "config file."));
3074 		else
3075 			domonitor(B_FALSE);
3076 		break;
3077 	case CMD_PMONITOR:
3078 		if (read_cmdfile)
3079 			ERROR(ep, ebuf, gettext("Monitor not appropriate in "
3080 			    "config file."));
3081 		else
3082 			domonitor(B_TRUE);
3083 		break;
3084 	case CMD_QUIT:
3085 		EXIT_OK(NULL);
3086 	}
3087 
3088 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
3089 
3090 	satype = parsesatype(*argv, ebuf);
3091 
3092 	if (satype != SADB_SATYPE_UNSPEC) {
3093 		argv++;
3094 	} else {
3095 		/*
3096 		 * You must specify either "all" or a specific SA type
3097 		 * for the "save" command.
3098 		 */
3099 		if (cmd == CMD_SAVE)
3100 			if (*argv == NULL) {
3101 				FATAL(ep, ebuf, gettext(
3102 				    "Must specify a specific "
3103 				    "SA type for save.\n"));
3104 			} else {
3105 				argv++;
3106 			}
3107 	}
3108 
3109 	switch (cmd) {
3110 	case CMD_FLUSH:
3111 		if (!cflag)
3112 			doflush(satype);
3113 		/*
3114 		 * If this was called because of an entry in a cmd file
3115 		 * then this action needs to be counted to prevent
3116 		 * do_interactive() treating this as an error.
3117 		 */
3118 		lines_added++;
3119 		break;
3120 	case CMD_ADD:
3121 	case CMD_UPDATE:
3122 		/*
3123 		 * NOTE: Shouldn't allow ADDs or UPDATEs with keying material
3124 		 * from the command line.
3125 		 */
3126 		if (!interactive) {
3127 			errx(1, gettext(
3128 			    "can't do ADD or UPDATE from the command line.\n"));
3129 		}
3130 		if (satype == SADB_SATYPE_UNSPEC) {
3131 			FATAL(ep, ebuf, gettext(
3132 			    "Must specify a specific SA type."));
3133 			/* NOTREACHED */
3134 		}
3135 		/* Parse for extensions, including keying material. */
3136 		doaddup(cmd, satype, argv, ebuf);
3137 		break;
3138 	case CMD_DELETE:
3139 	case CMD_GET:
3140 		if (satype == SADB_SATYPE_UNSPEC) {
3141 			FATAL(ep, ebuf, gettext(
3142 			    "Must specify a single SA type."));
3143 			/* NOTREACHED */
3144 		}
3145 		/* Parse for bare minimum to locate an SA. */
3146 		dodelget(cmd, satype, argv, ebuf);
3147 		break;
3148 	case CMD_DUMP:
3149 		if (read_cmdfile)
3150 			ERROR(ep, ebuf, gettext("Dump not appropriate in "
3151 			    "config file."));
3152 		else
3153 			dodump(satype, NULL);
3154 		break;
3155 	case CMD_SAVE:
3156 		if (read_cmdfile) {
3157 			ERROR(ep, ebuf, gettext("Save not appropriate in "
3158 			    "config file."));
3159 		} else {
3160 			mask_signals(B_FALSE);	/* Mask signals */
3161 			dodump(satype, opensavefile(argv[0]));
3162 			mask_signals(B_TRUE);	/* Unmask signals */
3163 		}
3164 		break;
3165 	default:
3166 		warnx(gettext("Unknown command (%s).\n"),
3167 		    *(argv - ((satype == SADB_SATYPE_UNSPEC) ? 1 : 2)));
3168 		usage();
3169 	}
3170 	handle_errors(ep, ebuf, B_FALSE, B_FALSE);
3171 }
3172 
3173 int
3174 main(int argc, char *argv[])
3175 {
3176 	int ch;
3177 	FILE *infile = stdin, *savefile;
3178 	boolean_t dosave = B_FALSE, readfile = B_FALSE;
3179 	char *configfile = NULL;
3180 
3181 	(void) setlocale(LC_ALL, "");
3182 #if !defined(TEXT_DOMAIN)
3183 #define	TEXT_DOMAIN "SYS_TEST"
3184 #endif
3185 	(void) textdomain(TEXT_DOMAIN);
3186 
3187 	/*
3188 	 * Check to see if the command is being run from smf(5).
3189 	 */
3190 	my_fmri = getenv("SMF_FMRI");
3191 
3192 	openlog("ipseckey", LOG_CONS, LOG_AUTH);
3193 	if (getuid() != 0) {
3194 		errx(1, "Insufficient privileges to run ipseckey.");
3195 	}
3196 
3197 	/* umask me to paranoid, I only want to create files read-only */
3198 	(void) umask((mode_t)00377);
3199 
3200 	while ((ch = getopt(argc, argv, "pnvf:s:c:")) != EOF)
3201 		switch (ch) {
3202 		case 'p':
3203 			pflag = B_TRUE;
3204 			break;
3205 		case 'n':
3206 			nflag = B_TRUE;
3207 			break;
3208 		case 'v':
3209 			vflag = B_TRUE;
3210 			break;
3211 		case 'c':
3212 			cflag = B_TRUE;
3213 			/* FALLTHRU */
3214 		case 'f':
3215 			if (dosave)
3216 				usage();
3217 			infile = fopen(optarg, "r");
3218 			if (infile == NULL) {
3219 				EXIT_BADCONFIG2("Unable to open configuration "
3220 				    "file: %s\n", optarg);
3221 			}
3222 			configfile = strdup(optarg);
3223 			readfile = B_TRUE;
3224 			break;
3225 		case 's':
3226 			if (readfile)
3227 				usage();
3228 			dosave = B_TRUE;
3229 			savefile = opensavefile(optarg);
3230 			break;
3231 		default:
3232 			usage();
3233 		}
3234 
3235 	argc -= optind;
3236 	argv += optind;
3237 
3238 	mypid = getpid();
3239 
3240 	keysock = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
3241 
3242 	if (keysock == -1) {
3243 		if (errno == EPERM) {
3244 			EXIT_BADPERM("Insufficient privileges to open "
3245 			    "PF_KEY socket.\n");
3246 		} else {
3247 			/* some other reason */
3248 			EXIT_FATAL("Opening PF_KEY socket");
3249 		}
3250 	}
3251 
3252 	if (dosave) {
3253 		mask_signals(B_FALSE);	/* Mask signals */
3254 		dodump(SADB_SATYPE_UNSPEC, savefile);
3255 		mask_signals(B_TRUE);	/* Unmask signals */
3256 		EXIT_OK(NULL);
3257 	}
3258 
3259 	/*
3260 	 * When run from smf(5) flush any existing SA's first
3261 	 * otherwise you will end up in maintenance mode.
3262 	 */
3263 	if ((my_fmri != NULL) && readfile) {
3264 		(void) fprintf(stdout, gettext(
3265 		    "Flushing existing SA's before adding new SA's\n"));
3266 		(void) fflush(stdout);
3267 		doflush(SADB_SATYPE_UNSPEC);
3268 	}
3269 	if (infile != stdin || *argv == NULL) {
3270 		/* Go into interactive mode here. */
3271 		do_interactive(infile, configfile, "ipseckey> ", my_fmri,
3272 		    parseit);
3273 	}
3274 	parseit(argc, argv, NULL, B_FALSE);
3275 
3276 	return (0);
3277 }
3278