1e3320f40Smarkfen /*
2e3320f40Smarkfen * CDDL HEADER START
3e3320f40Smarkfen *
4e3320f40Smarkfen * The contents of this file are subject to the terms of the
5e3320f40Smarkfen * Common Development and Distribution License (the "License").
6e3320f40Smarkfen * You may not use this file except in compliance with the License.
7e3320f40Smarkfen *
8e3320f40Smarkfen * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e3320f40Smarkfen * or http://www.opensolaris.org/os/licensing.
10e3320f40Smarkfen * See the License for the specific language governing permissions
11e3320f40Smarkfen * and limitations under the License.
12e3320f40Smarkfen *
13e3320f40Smarkfen * When distributing Covered Code, include this CDDL HEADER in each
14e3320f40Smarkfen * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15e3320f40Smarkfen * If applicable, add the following below this CDDL HEADER, with the
16e3320f40Smarkfen * fields enclosed by brackets "[]" replaced with your own identifying
17e3320f40Smarkfen * information: Portions Copyright [yyyy] [name of copyright owner]
18e3320f40Smarkfen *
19e3320f40Smarkfen * CDDL HEADER END
20e3320f40Smarkfen */
21e3320f40Smarkfen /*
22*f02131e0SVladimir Kotal * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23e3320f40Smarkfen */
24e3320f40Smarkfen
25e3320f40Smarkfen /*
26e3320f40Smarkfen * NOTE:I'm trying to use "struct sadb_foo" instead of "sadb_foo_t"
27e3320f40Smarkfen * as a maximal PF_KEY portability test.
28e3320f40Smarkfen *
29e3320f40Smarkfen * Also, this is a deliberately single-threaded app, also for portability
30e3320f40Smarkfen * to systems without POSIX threads.
31e3320f40Smarkfen */
32e3320f40Smarkfen
33e3320f40Smarkfen #include <sys/types.h>
34e3320f40Smarkfen #include <sys/stat.h>
35e3320f40Smarkfen #include <sys/socket.h>
36e3320f40Smarkfen #include <sys/sysmacros.h>
37e3320f40Smarkfen #include <sys/fcntl.h>
38e3320f40Smarkfen #include <net/pfkeyv2.h>
39e3320f40Smarkfen #include <arpa/inet.h>
40e3320f40Smarkfen #include <netinet/in.h>
41e3320f40Smarkfen #include <sys/uio.h>
42e3320f40Smarkfen
43e3320f40Smarkfen #include <syslog.h>
44e3320f40Smarkfen #include <signal.h>
45e3320f40Smarkfen #include <unistd.h>
46e3320f40Smarkfen #include <limits.h>
47e3320f40Smarkfen #include <stdlib.h>
48e3320f40Smarkfen #include <stdio.h>
49e3320f40Smarkfen #include <stdarg.h>
50e3320f40Smarkfen #include <netdb.h>
51e3320f40Smarkfen #include <pwd.h>
52e3320f40Smarkfen #include <errno.h>
53e3320f40Smarkfen #include <libintl.h>
54e3320f40Smarkfen #include <locale.h>
55e3320f40Smarkfen #include <fcntl.h>
56e3320f40Smarkfen #include <strings.h>
57e3320f40Smarkfen #include <ctype.h>
589c2c14abSThejaswini Singarajipura #include <sys/cladm.h>
59e3320f40Smarkfen
60e3320f40Smarkfen #include <ipsec_util.h>
61e3320f40Smarkfen
62e3320f40Smarkfen static int keysock;
639c2c14abSThejaswini Singarajipura static int cluster_socket;
64e3320f40Smarkfen static uint32_t seq;
65e3320f40Smarkfen static pid_t mypid;
66e3320f40Smarkfen static boolean_t vflag = B_FALSE; /* Verbose? */
67e3320f40Smarkfen static boolean_t cflag = B_FALSE; /* Check Only */
68e3320f40Smarkfen
69e3320f40Smarkfen char *my_fmri = NULL;
70e3320f40Smarkfen FILE *debugfile = stdout;
719c2c14abSThejaswini Singarajipura static struct sockaddr_in cli_addr;
729c2c14abSThejaswini Singarajipura static boolean_t in_cluster_mode = B_FALSE;
73e3320f40Smarkfen
74e3320f40Smarkfen #define MAX_GET_SIZE 1024
75e3320f40Smarkfen /*
76ec485834Spwernau * WARN() and ERROR() do the same thing really, with ERROR() the function
77e3320f40Smarkfen * that prints the error buffer needs to be called at the end of a code block
78e3320f40Smarkfen * This will print out all accumulated errors before bailing. The WARN()
79e3320f40Smarkfen * macro calls handle_errors() in such a way that it prints the message
80e3320f40Smarkfen * then continues.
81e3320f40Smarkfen * If the FATAL() macro used call handle_errors() immediately.
82e3320f40Smarkfen */
83e3320f40Smarkfen #define ERROR(x, y, z) x = record_error(x, y, z)
84e3320f40Smarkfen #define ERROR1(w, x, y, z) w = record_error(w, x, y, z)
85e3320f40Smarkfen #define ERROR2(v, w, x, y, z) v = record_error(v, w, x, y, z)
86e3320f40Smarkfen #define WARN(x, y, z) ERROR(x, y, z);\
87e3320f40Smarkfen handle_errors(x, NULL, B_FALSE, B_FALSE); x = NULL
88e3320f40Smarkfen #define WARN1(w, x, y, z) ERROR1(w, x, y, z);\
89e3320f40Smarkfen handle_errors(w, NULL, B_FALSE, B_FALSE); w = NULL
90e3320f40Smarkfen #define WARN2(v, w, x, y, z) ERROR2(v, w, x, y, z);\
91e3320f40Smarkfen handle_errors(v, NULL, B_FALSE, B_FALSE); v = NULL
92e3320f40Smarkfen #define FATAL(x, y, z) ERROR(x, y, z);\
93e3320f40Smarkfen handle_errors(x, y, B_TRUE, B_TRUE)
94e3320f40Smarkfen #define FATAL1(w, x, y, z) ERROR1(w, x, y, z);\
95e3320f40Smarkfen handle_errors(w, x, B_TRUE, B_TRUE)
96e3320f40Smarkfen
97e3320f40Smarkfen /* Defined as a uint64_t array for alignment purposes. */
98e3320f40Smarkfen static uint64_t get_buffer[MAX_GET_SIZE];
99e3320f40Smarkfen
100e3320f40Smarkfen /*
101bfe6f8f5SVladimir Kotal * Disable default TAB completion for now (until some brave soul tackles it).
102bfe6f8f5SVladimir Kotal */
103bfe6f8f5SVladimir Kotal /* ARGSUSED */
104bfe6f8f5SVladimir Kotal static
CPL_MATCH_FN(no_match)105bfe6f8f5SVladimir Kotal CPL_MATCH_FN(no_match)
106bfe6f8f5SVladimir Kotal {
107bfe6f8f5SVladimir Kotal return (0);
108bfe6f8f5SVladimir Kotal }
109bfe6f8f5SVladimir Kotal
110bfe6f8f5SVladimir Kotal /*
111e3320f40Smarkfen * Create/Grow a buffer large enough to hold error messages. If *ebuf
112e3320f40Smarkfen * is not NULL then it will contain a copy of the command line that
113e3320f40Smarkfen * triggered the error/warning, copy this into a new buffer or
114e3320f40Smarkfen * append new messages to the existing buffer.
115e3320f40Smarkfen */
116e3320f40Smarkfen /*PRINTFLIKE1*/
117e3320f40Smarkfen char *
record_error(char * ep,char * ebuf,char * fmt,...)118e3320f40Smarkfen record_error(char *ep, char *ebuf, char *fmt, ...)
119e3320f40Smarkfen {
120e3320f40Smarkfen char *err_ptr;
121e3320f40Smarkfen char tmp_buff[1024];
122e3320f40Smarkfen va_list ap;
123e3320f40Smarkfen int length = 0;
124e3320f40Smarkfen err_ptr = ep;
125e3320f40Smarkfen
126e3320f40Smarkfen va_start(ap, fmt);
12772c8fd38Smarkfen length = vsnprintf(tmp_buff, sizeof (tmp_buff), fmt, ap);
128e3320f40Smarkfen va_end(ap);
129e3320f40Smarkfen
130e3320f40Smarkfen /* There is a new line character */
131e3320f40Smarkfen length++;
13272c8fd38Smarkfen
13372c8fd38Smarkfen if (ep == NULL) {
13472c8fd38Smarkfen if (ebuf != NULL)
13572c8fd38Smarkfen length += strlen(ebuf);
13672c8fd38Smarkfen } else {
13772c8fd38Smarkfen length += strlen(ep);
13872c8fd38Smarkfen }
13972c8fd38Smarkfen
14072c8fd38Smarkfen if (err_ptr == NULL)
14172c8fd38Smarkfen err_ptr = calloc(length, sizeof (char));
14272c8fd38Smarkfen else
143e3320f40Smarkfen err_ptr = realloc(err_ptr, length);
14472c8fd38Smarkfen
145e3320f40Smarkfen if (err_ptr == NULL)
146e3320f40Smarkfen Bail("realloc() failure");
14772c8fd38Smarkfen
14872c8fd38Smarkfen /*
14972c8fd38Smarkfen * If (ep == NULL) then this is the first error to record,
15072c8fd38Smarkfen * copy in the command line that triggered this error/warning.
15172c8fd38Smarkfen */
15272c8fd38Smarkfen if (ep == NULL && ebuf != NULL)
15372c8fd38Smarkfen (void) strlcpy(err_ptr, ebuf, length);
15472c8fd38Smarkfen
15572c8fd38Smarkfen /*
15672c8fd38Smarkfen * Now the actual error.
15772c8fd38Smarkfen */
158e3320f40Smarkfen (void) strlcat(err_ptr, tmp_buff, length);
159e3320f40Smarkfen return (err_ptr);
160e3320f40Smarkfen }
161e3320f40Smarkfen
162e3320f40Smarkfen /*
163bfe6f8f5SVladimir Kotal * If not in interactive mode print usage message and exit.
164e3320f40Smarkfen */
165e3320f40Smarkfen static void
usage(void)166e3320f40Smarkfen usage(void)
167e3320f40Smarkfen {
168e3320f40Smarkfen if (!interactive) {
169e3320f40Smarkfen (void) fprintf(stderr, gettext("Usage:\t"
170e3320f40Smarkfen "ipseckey [ -nvp ] | cmd [sa_type] [extfield value]*\n"));
171e3320f40Smarkfen (void) fprintf(stderr,
172e3320f40Smarkfen gettext("\tipseckey [ -nvp ] -f infile\n"));
173e3320f40Smarkfen (void) fprintf(stderr,
174e3320f40Smarkfen gettext("\tipseckey [ -nvp ] -s outfile\n"));
175e3320f40Smarkfen EXIT_FATAL(NULL);
176bfe6f8f5SVladimir Kotal } else {
177bfe6f8f5SVladimir Kotal (void) fprintf(stderr,
178bfe6f8f5SVladimir Kotal gettext("Type help or ? for usage info\n"));
179bfe6f8f5SVladimir Kotal }
180e3320f40Smarkfen }
181e3320f40Smarkfen
182e3320f40Smarkfen
183e3320f40Smarkfen /*
184e3320f40Smarkfen * Print out any errors, tidy up as required.
185e3320f40Smarkfen * error pointer ep will be free()'d
186e3320f40Smarkfen */
187e3320f40Smarkfen void
handle_errors(char * ep,char * ebuf,boolean_t fatal,boolean_t done)188e3320f40Smarkfen handle_errors(char *ep, char *ebuf, boolean_t fatal, boolean_t done)
189e3320f40Smarkfen {
190e3320f40Smarkfen if (ep != NULL) {
191e3320f40Smarkfen if (my_fmri == NULL) {
192e3320f40Smarkfen /*
193e3320f40Smarkfen * For now suppress the errors when run from smf(5)
194e3320f40Smarkfen * because potentially sensitive information could
195e3320f40Smarkfen * end up in a publicly readable logfile.
196e3320f40Smarkfen */
197e3320f40Smarkfen (void) fprintf(stdout, "%s\n", ep);
198e3320f40Smarkfen (void) fflush(stdout);
199e3320f40Smarkfen }
200e3320f40Smarkfen free(ep);
201e3320f40Smarkfen if (fatal) {
202e3320f40Smarkfen if (ebuf != NULL) {
203e3320f40Smarkfen free(ebuf);
204e3320f40Smarkfen }
205e3320f40Smarkfen /* reset command buffer */
206e3320f40Smarkfen if (interactive)
207e3320f40Smarkfen longjmp(env, 1);
208e3320f40Smarkfen } else {
209e3320f40Smarkfen return;
210e3320f40Smarkfen }
211e3320f40Smarkfen } else {
212e3320f40Smarkfen /*
213e3320f40Smarkfen * No errors, if this is the last time that this function
214e3320f40Smarkfen * is called, free(ebuf) and reset command buffer.
215e3320f40Smarkfen */
216e3320f40Smarkfen if (done) {
217e3320f40Smarkfen if (ebuf != NULL) {
218e3320f40Smarkfen free(ebuf);
219e3320f40Smarkfen }
220e3320f40Smarkfen /* reset command buffer */
221e3320f40Smarkfen if (interactive)
222e3320f40Smarkfen longjmp(env, 1);
223e3320f40Smarkfen }
224e3320f40Smarkfen return;
225e3320f40Smarkfen }
22672c8fd38Smarkfen EXIT_FATAL(NULL);
227e3320f40Smarkfen }
228e3320f40Smarkfen
229e3320f40Smarkfen /*
230e3320f40Smarkfen * Initialize a PF_KEY base message.
231e3320f40Smarkfen */
232e3320f40Smarkfen static void
msg_init(struct sadb_msg * msg,uint8_t type,uint8_t satype)233e3320f40Smarkfen msg_init(struct sadb_msg *msg, uint8_t type, uint8_t satype)
234e3320f40Smarkfen {
235e3320f40Smarkfen msg->sadb_msg_version = PF_KEY_V2;
236e3320f40Smarkfen msg->sadb_msg_type = type;
237e3320f40Smarkfen msg->sadb_msg_errno = 0;
238e3320f40Smarkfen msg->sadb_msg_satype = satype;
239e3320f40Smarkfen /* For starters... */
240e3320f40Smarkfen msg->sadb_msg_len = SADB_8TO64(sizeof (*msg));
241e3320f40Smarkfen msg->sadb_msg_reserved = 0;
242e3320f40Smarkfen msg->sadb_msg_seq = ++seq;
243e3320f40Smarkfen msg->sadb_msg_pid = mypid;
244e3320f40Smarkfen }
245e3320f40Smarkfen
246e3320f40Smarkfen /*
247e3320f40Smarkfen * parseXXX and rparseXXX commands parse input and convert them to PF_KEY
248e3320f40Smarkfen * field values, or do the reverse for the purposes of saving the SA tables.
249e3320f40Smarkfen * (See the save_XXX functions.)
250e3320f40Smarkfen */
251e3320f40Smarkfen
252e3320f40Smarkfen #define CMD_NONE 0
253e3320f40Smarkfen #define CMD_UPDATE 2
25438d95a78Smarkfen #define CMD_UPDATE_PAIR 3
25538d95a78Smarkfen #define CMD_ADD 4
25638d95a78Smarkfen #define CMD_DELETE 5
25738d95a78Smarkfen #define CMD_DELETE_PAIR 6
25838d95a78Smarkfen #define CMD_GET 7
259e3320f40Smarkfen #define CMD_FLUSH 9
260e3320f40Smarkfen #define CMD_DUMP 10
261e3320f40Smarkfen #define CMD_MONITOR 11
262e3320f40Smarkfen #define CMD_PMONITOR 12
263e3320f40Smarkfen #define CMD_QUIT 13
264e3320f40Smarkfen #define CMD_SAVE 14
265e3320f40Smarkfen #define CMD_HELP 15
266e3320f40Smarkfen
267e3320f40Smarkfen /*
268e3320f40Smarkfen * Parse the command.
269e3320f40Smarkfen */
270e3320f40Smarkfen static int
parsecmd(char * cmdstr)271e3320f40Smarkfen parsecmd(char *cmdstr)
272e3320f40Smarkfen {
273e3320f40Smarkfen static struct cmdtable {
274e3320f40Smarkfen char *cmd;
275e3320f40Smarkfen int token;
276e3320f40Smarkfen } table[] = {
277e3320f40Smarkfen /*
278e3320f40Smarkfen * Q: Do we want to do GETSPI?
279e3320f40Smarkfen * A: No, it's for automated key mgmt. only. Either that,
280e3320f40Smarkfen * or it isn't relevant until we support non IPsec SA types.
281e3320f40Smarkfen */
282e3320f40Smarkfen {"update", CMD_UPDATE},
28338d95a78Smarkfen {"update-pair", CMD_UPDATE_PAIR},
284e3320f40Smarkfen {"add", CMD_ADD},
285e3320f40Smarkfen {"delete", CMD_DELETE},
28638d95a78Smarkfen {"delete-pair", CMD_DELETE_PAIR},
287e3320f40Smarkfen {"get", CMD_GET},
288e3320f40Smarkfen /*
289e3320f40Smarkfen * Q: And ACQUIRE and REGISTER and EXPIRE?
290e3320f40Smarkfen * A: not until we support non IPsec SA types.
291e3320f40Smarkfen */
292e3320f40Smarkfen {"flush", CMD_FLUSH},
293e3320f40Smarkfen {"dump", CMD_DUMP},
294e3320f40Smarkfen {"monitor", CMD_MONITOR},
295e3320f40Smarkfen {"passive_monitor", CMD_PMONITOR},
296e3320f40Smarkfen {"pmonitor", CMD_PMONITOR},
297e3320f40Smarkfen {"quit", CMD_QUIT},
298e3320f40Smarkfen {"exit", CMD_QUIT},
299e3320f40Smarkfen {"save", CMD_SAVE},
300e3320f40Smarkfen {"help", CMD_HELP},
301e3320f40Smarkfen {"?", CMD_HELP},
302e3320f40Smarkfen {NULL, CMD_NONE}
303e3320f40Smarkfen };
304e3320f40Smarkfen struct cmdtable *ct = table;
305e3320f40Smarkfen
306e3320f40Smarkfen while (ct->cmd != NULL && strcmp(ct->cmd, cmdstr) != 0)
307e3320f40Smarkfen ct++;
308e3320f40Smarkfen return (ct->token);
309e3320f40Smarkfen }
310e3320f40Smarkfen
311e3320f40Smarkfen /*
312e3320f40Smarkfen * Convert a number from a command line. I picked "u_longlong_t" for the
313e3320f40Smarkfen * number because we need the largest number available. Also, the strto<num>
314e3320f40Smarkfen * calls don't deal in units of uintNN_t.
315e3320f40Smarkfen */
316e3320f40Smarkfen static u_longlong_t
parsenum(char * num,boolean_t bail,char * ebuf)317e3320f40Smarkfen parsenum(char *num, boolean_t bail, char *ebuf)
318e3320f40Smarkfen {
319e3320f40Smarkfen u_longlong_t rc = 0;
320e3320f40Smarkfen char *end = NULL;
321e3320f40Smarkfen char *ep = NULL;
322e3320f40Smarkfen
323e3320f40Smarkfen if (num == NULL) {
324e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line,"
325e3320f40Smarkfen " was expecting a number.\n"));
326e3320f40Smarkfen /* NOTREACHED */
327e3320f40Smarkfen }
328e3320f40Smarkfen
329e3320f40Smarkfen errno = 0;
330e3320f40Smarkfen rc = strtoull(num, &end, 0);
331e3320f40Smarkfen if (errno != 0 || end == num || *end != '\0') {
332e3320f40Smarkfen if (bail) {
33338d95a78Smarkfen FATAL1(ep, ebuf, gettext(
334e3320f40Smarkfen "Expecting a number, not \"%s\"!\n"), num);
335e3320f40Smarkfen } else {
336e3320f40Smarkfen /*
337e3320f40Smarkfen * -1, while not optimal, is sufficiently out of range
338e3320f40Smarkfen * for most of this function's applications when
339e3320f40Smarkfen * we don't just bail.
340e3320f40Smarkfen */
341e3320f40Smarkfen return ((u_longlong_t)-1);
342e3320f40Smarkfen }
343e3320f40Smarkfen }
344e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE);
345e3320f40Smarkfen return (rc);
346e3320f40Smarkfen }
347e3320f40Smarkfen
348e3320f40Smarkfen /*
349e3320f40Smarkfen * Parse and reverse parse a specific SA type (AH, ESP, etc.).
350e3320f40Smarkfen */
351e3320f40Smarkfen static struct typetable {
352e3320f40Smarkfen char *type;
353e3320f40Smarkfen int token;
354e3320f40Smarkfen } type_table[] = {
355e3320f40Smarkfen {"all", SADB_SATYPE_UNSPEC},
356e3320f40Smarkfen {"ah", SADB_SATYPE_AH},
357e3320f40Smarkfen {"esp", SADB_SATYPE_ESP},
358e3320f40Smarkfen /* PF_KEY NOTE: More to come if net/pfkeyv2.h gets updated. */
359e3320f40Smarkfen {NULL, 0} /* Token value is irrelevant for this entry. */
360e3320f40Smarkfen };
361e3320f40Smarkfen
362e3320f40Smarkfen
363e3320f40Smarkfen static int
parsesatype(char * type,char * ebuf)364e3320f40Smarkfen parsesatype(char *type, char *ebuf)
365e3320f40Smarkfen {
366e3320f40Smarkfen struct typetable *tt = type_table;
367e3320f40Smarkfen char *ep = NULL;
368e3320f40Smarkfen
369e3320f40Smarkfen if (type == NULL)
370e3320f40Smarkfen return (SADB_SATYPE_UNSPEC);
371e3320f40Smarkfen
372e3320f40Smarkfen while (tt->type != NULL && strcasecmp(tt->type, type) != 0)
373e3320f40Smarkfen tt++;
374e3320f40Smarkfen
375e3320f40Smarkfen /*
376e3320f40Smarkfen * New SA types (including ones keysock maintains for user-land
377e3320f40Smarkfen * protocols) may be added, so parse a numeric value if possible.
378e3320f40Smarkfen */
379e3320f40Smarkfen if (tt->type == NULL) {
380e3320f40Smarkfen tt->token = (int)parsenum(type, B_FALSE, ebuf);
381e3320f40Smarkfen if (tt->token == -1) {
382e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
383e3320f40Smarkfen "Unknown SA type (%s).\n"), type);
384e3320f40Smarkfen tt->token = SADB_SATYPE_UNSPEC;
385e3320f40Smarkfen }
386e3320f40Smarkfen }
387bfe6f8f5SVladimir Kotal handle_errors(ep, NULL, interactive ? B_TRUE : B_FALSE, B_FALSE);
388e3320f40Smarkfen return (tt->token);
389e3320f40Smarkfen }
390e3320f40Smarkfen
391e3320f40Smarkfen #define NEXTEOF 0
392e3320f40Smarkfen #define NEXTNONE 1
393e3320f40Smarkfen #define NEXTNUM 2
394e3320f40Smarkfen #define NEXTSTR 3
395e3320f40Smarkfen #define NEXTNUMSTR 4
396e3320f40Smarkfen #define NEXTADDR 5
397e3320f40Smarkfen #define NEXTHEX 6
398e3320f40Smarkfen #define NEXTIDENT 7
399e3320f40Smarkfen #define NEXTADDR4 8
400e3320f40Smarkfen #define NEXTADDR6 9
4015d3b8cb7SBill Sommerfeld #define NEXTLABEL 10
402e3320f40Smarkfen
403e3320f40Smarkfen #define TOK_EOF 0
404e3320f40Smarkfen #define TOK_UNKNOWN 1
405e3320f40Smarkfen #define TOK_SPI 2
406e3320f40Smarkfen #define TOK_REPLAY 3
407e3320f40Smarkfen #define TOK_STATE 4
408e3320f40Smarkfen #define TOK_AUTHALG 5
409e3320f40Smarkfen #define TOK_ENCRALG 6
410e3320f40Smarkfen #define TOK_FLAGS 7
411e3320f40Smarkfen #define TOK_SOFT_ALLOC 8
412e3320f40Smarkfen #define TOK_SOFT_BYTES 9
413e3320f40Smarkfen #define TOK_SOFT_ADDTIME 10
414e3320f40Smarkfen #define TOK_SOFT_USETIME 11
415e3320f40Smarkfen #define TOK_HARD_ALLOC 12
416e3320f40Smarkfen #define TOK_HARD_BYTES 13
417e3320f40Smarkfen #define TOK_HARD_ADDTIME 14
418e3320f40Smarkfen #define TOK_HARD_USETIME 15
419e3320f40Smarkfen #define TOK_CURRENT_ALLOC 16
420e3320f40Smarkfen #define TOK_CURRENT_BYTES 17
421e3320f40Smarkfen #define TOK_CURRENT_ADDTIME 18
422e3320f40Smarkfen #define TOK_CURRENT_USETIME 19
423e3320f40Smarkfen #define TOK_SRCADDR 20
424e3320f40Smarkfen #define TOK_DSTADDR 21
425e3320f40Smarkfen #define TOK_PROXYADDR 22
426e3320f40Smarkfen #define TOK_AUTHKEY 23
427e3320f40Smarkfen #define TOK_ENCRKEY 24
428e3320f40Smarkfen #define TOK_SRCIDTYPE 25
429e3320f40Smarkfen #define TOK_DSTIDTYPE 26
430e3320f40Smarkfen #define TOK_DPD 27
431e3320f40Smarkfen #define TOK_SENS_LEVEL 28
432e3320f40Smarkfen #define TOK_SENS_MAP 29
433e3320f40Smarkfen #define TOK_INTEG_LEVEL 30
434e3320f40Smarkfen #define TOK_INTEG_MAP 31
435e3320f40Smarkfen #define TOK_SRCADDR6 32
436e3320f40Smarkfen #define TOK_DSTADDR6 33
437e3320f40Smarkfen #define TOK_PROXYADDR6 34
438e3320f40Smarkfen #define TOK_SRCPORT 35
439e3320f40Smarkfen #define TOK_DSTPORT 36
440e3320f40Smarkfen #define TOK_PROTO 37
441e3320f40Smarkfen #define TOK_ENCAP 38
442e3320f40Smarkfen #define TOK_NATLOC 39
443e3320f40Smarkfen #define TOK_NATREM 40
444e3320f40Smarkfen #define TOK_NATLPORT 41
445e3320f40Smarkfen #define TOK_NATRPORT 42
446e3320f40Smarkfen #define TOK_IPROTO 43
447e3320f40Smarkfen #define TOK_IDSTADDR 44
448e3320f40Smarkfen #define TOK_IDSTADDR6 45
449e3320f40Smarkfen #define TOK_ISRCPORT 46
450e3320f40Smarkfen #define TOK_IDSTPORT 47
45138d95a78Smarkfen #define TOK_PAIR_SPI 48
45238d95a78Smarkfen #define TOK_FLAG_INBOUND 49
45338d95a78Smarkfen #define TOK_FLAG_OUTBOUND 50
4549c2c14abSThejaswini Singarajipura #define TOK_REPLAY_VALUE 51
4559c2c14abSThejaswini Singarajipura #define TOK_IDLE_ADDTIME 52
4569c2c14abSThejaswini Singarajipura #define TOK_IDLE_USETIME 53
457628b0c67SMark Fenwick #define TOK_RESERVED 54
4585d3b8cb7SBill Sommerfeld #define TOK_LABEL 55
4595d3b8cb7SBill Sommerfeld #define TOK_OLABEL 56
4605d3b8cb7SBill Sommerfeld #define TOK_IMPLABEL 57
4615d3b8cb7SBill Sommerfeld
462e3320f40Smarkfen
463e3320f40Smarkfen static struct toktable {
464e3320f40Smarkfen char *string;
465e3320f40Smarkfen int token;
466e3320f40Smarkfen int next;
467e3320f40Smarkfen } tokens[] = {
468e3320f40Smarkfen /* "String", token value, next arg is */
469e3320f40Smarkfen {"spi", TOK_SPI, NEXTNUM},
47038d95a78Smarkfen {"pair-spi", TOK_PAIR_SPI, NEXTNUM},
471e3320f40Smarkfen {"replay", TOK_REPLAY, NEXTNUM},
472e3320f40Smarkfen {"state", TOK_STATE, NEXTNUMSTR},
473e3320f40Smarkfen {"auth_alg", TOK_AUTHALG, NEXTNUMSTR},
474e3320f40Smarkfen {"authalg", TOK_AUTHALG, NEXTNUMSTR},
475e3320f40Smarkfen {"encr_alg", TOK_ENCRALG, NEXTNUMSTR},
476e3320f40Smarkfen {"encralg", TOK_ENCRALG, NEXTNUMSTR},
477e3320f40Smarkfen {"flags", TOK_FLAGS, NEXTNUM},
478e3320f40Smarkfen {"soft_alloc", TOK_SOFT_ALLOC, NEXTNUM},
479e3320f40Smarkfen {"soft_bytes", TOK_SOFT_BYTES, NEXTNUM},
480e3320f40Smarkfen {"soft_addtime", TOK_SOFT_ADDTIME, NEXTNUM},
481e3320f40Smarkfen {"soft_usetime", TOK_SOFT_USETIME, NEXTNUM},
482e3320f40Smarkfen {"hard_alloc", TOK_HARD_ALLOC, NEXTNUM},
483e3320f40Smarkfen {"hard_bytes", TOK_HARD_BYTES, NEXTNUM},
484e3320f40Smarkfen {"hard_addtime", TOK_HARD_ADDTIME, NEXTNUM},
485e3320f40Smarkfen {"hard_usetime", TOK_HARD_USETIME, NEXTNUM},
486e3320f40Smarkfen {"current_alloc", TOK_CURRENT_ALLOC, NEXTNUM},
487e3320f40Smarkfen {"current_bytes", TOK_CURRENT_BYTES, NEXTNUM},
488e3320f40Smarkfen {"current_addtime", TOK_CURRENT_ADDTIME, NEXTNUM},
489e3320f40Smarkfen {"current_usetime", TOK_CURRENT_USETIME, NEXTNUM},
490e3320f40Smarkfen
491e3320f40Smarkfen {"saddr", TOK_SRCADDR, NEXTADDR},
492e3320f40Smarkfen {"srcaddr", TOK_SRCADDR, NEXTADDR},
493e3320f40Smarkfen {"src", TOK_SRCADDR, NEXTADDR},
494e3320f40Smarkfen {"daddr", TOK_DSTADDR, NEXTADDR},
495e3320f40Smarkfen {"dstaddr", TOK_DSTADDR, NEXTADDR},
496e3320f40Smarkfen {"dst", TOK_DSTADDR, NEXTADDR},
497e3320f40Smarkfen {"proxyaddr", TOK_PROXYADDR, NEXTADDR},
498e3320f40Smarkfen {"proxy", TOK_PROXYADDR, NEXTADDR},
499e3320f40Smarkfen {"innersrc", TOK_PROXYADDR, NEXTADDR},
500e3320f40Smarkfen {"isrc", TOK_PROXYADDR, NEXTADDR},
501e3320f40Smarkfen {"innerdst", TOK_IDSTADDR, NEXTADDR},
502e3320f40Smarkfen {"idst", TOK_IDSTADDR, NEXTADDR},
503e3320f40Smarkfen
504e3320f40Smarkfen {"sport", TOK_SRCPORT, NEXTNUM},
505e3320f40Smarkfen {"dport", TOK_DSTPORT, NEXTNUM},
506e3320f40Smarkfen {"innersport", TOK_ISRCPORT, NEXTNUM},
507e3320f40Smarkfen {"isport", TOK_ISRCPORT, NEXTNUM},
508e3320f40Smarkfen {"innerdport", TOK_IDSTPORT, NEXTNUM},
509e3320f40Smarkfen {"idport", TOK_IDSTPORT, NEXTNUM},
510e3320f40Smarkfen {"proto", TOK_PROTO, NEXTNUM},
511e3320f40Smarkfen {"ulp", TOK_PROTO, NEXTNUM},
512e3320f40Smarkfen {"iproto", TOK_IPROTO, NEXTNUM},
513e3320f40Smarkfen {"iulp", TOK_IPROTO, NEXTNUM},
514e3320f40Smarkfen
515e3320f40Smarkfen {"saddr6", TOK_SRCADDR6, NEXTADDR},
516e3320f40Smarkfen {"srcaddr6", TOK_SRCADDR6, NEXTADDR},
517e3320f40Smarkfen {"src6", TOK_SRCADDR6, NEXTADDR},
518e3320f40Smarkfen {"daddr6", TOK_DSTADDR6, NEXTADDR},
519e3320f40Smarkfen {"dstaddr6", TOK_DSTADDR6, NEXTADDR},
520e3320f40Smarkfen {"dst6", TOK_DSTADDR6, NEXTADDR},
521e3320f40Smarkfen {"proxyaddr6", TOK_PROXYADDR6, NEXTADDR},
522e3320f40Smarkfen {"proxy6", TOK_PROXYADDR6, NEXTADDR},
523e3320f40Smarkfen {"innersrc6", TOK_PROXYADDR6, NEXTADDR},
524e3320f40Smarkfen {"isrc6", TOK_PROXYADDR6, NEXTADDR},
525e3320f40Smarkfen {"innerdst6", TOK_IDSTADDR6, NEXTADDR},
526e3320f40Smarkfen {"idst6", TOK_IDSTADDR6, NEXTADDR},
527e3320f40Smarkfen
528e3320f40Smarkfen {"authkey", TOK_AUTHKEY, NEXTHEX},
529e3320f40Smarkfen {"encrkey", TOK_ENCRKEY, NEXTHEX},
530e3320f40Smarkfen {"srcidtype", TOK_SRCIDTYPE, NEXTIDENT},
531e3320f40Smarkfen {"dstidtype", TOK_DSTIDTYPE, NEXTIDENT},
532e3320f40Smarkfen {"dpd", TOK_DPD, NEXTNUM},
533e3320f40Smarkfen {"sens_level", TOK_SENS_LEVEL, NEXTNUM},
534e3320f40Smarkfen {"sens_map", TOK_SENS_MAP, NEXTHEX},
535e3320f40Smarkfen {"integ_level", TOK_INTEG_LEVEL, NEXTNUM},
536e3320f40Smarkfen {"integ_map", TOK_INTEG_MAP, NEXTHEX},
537e3320f40Smarkfen {"nat_loc", TOK_NATLOC, NEXTADDR},
538e3320f40Smarkfen {"nat_rem", TOK_NATREM, NEXTADDR},
539e3320f40Smarkfen {"nat_lport", TOK_NATLPORT, NEXTNUM},
540e3320f40Smarkfen {"nat_rport", TOK_NATRPORT, NEXTNUM},
541e3320f40Smarkfen {"encap", TOK_ENCAP, NEXTNUMSTR},
54238d95a78Smarkfen
54338d95a78Smarkfen {"outbound", TOK_FLAG_OUTBOUND, NULL},
54438d95a78Smarkfen {"inbound", TOK_FLAG_INBOUND, NULL},
5459c2c14abSThejaswini Singarajipura
546628b0c67SMark Fenwick {"reserved_bits", TOK_RESERVED, NEXTNUM},
5479c2c14abSThejaswini Singarajipura {"replay_value", TOK_REPLAY_VALUE, NEXTNUM},
5489c2c14abSThejaswini Singarajipura {"idle_addtime", TOK_IDLE_ADDTIME, NEXTNUM},
5499c2c14abSThejaswini Singarajipura {"idle_usetime", TOK_IDLE_USETIME, NEXTNUM},
5505d3b8cb7SBill Sommerfeld
5515d3b8cb7SBill Sommerfeld {"label", TOK_LABEL, NEXTLABEL},
5525d3b8cb7SBill Sommerfeld {"outer-label", TOK_OLABEL, NEXTLABEL},
5535d3b8cb7SBill Sommerfeld {"implicit-label", TOK_IMPLABEL, NEXTLABEL},
5545d3b8cb7SBill Sommerfeld
555e3320f40Smarkfen {NULL, TOK_UNKNOWN, NEXTEOF}
556e3320f40Smarkfen };
557e3320f40Smarkfen
558e3320f40Smarkfen /*
559e3320f40Smarkfen * Q: Do I need stuff for proposals, combinations, supported algorithms,
560e3320f40Smarkfen * or SPI ranges?
561e3320f40Smarkfen *
562e3320f40Smarkfen * A: Probably not, but you never know.
563e3320f40Smarkfen *
564e3320f40Smarkfen * Parse out extension header type values.
565e3320f40Smarkfen */
566e3320f40Smarkfen static int
parseextval(char * value,int * next)567e3320f40Smarkfen parseextval(char *value, int *next)
568e3320f40Smarkfen {
569e3320f40Smarkfen struct toktable *tp;
570e3320f40Smarkfen
571e3320f40Smarkfen if (value == NULL)
572e3320f40Smarkfen return (TOK_EOF);
573e3320f40Smarkfen
574e3320f40Smarkfen for (tp = tokens; tp->string != NULL; tp++)
575e3320f40Smarkfen if (strcmp(value, tp->string) == 0)
576e3320f40Smarkfen break;
577e3320f40Smarkfen
578e3320f40Smarkfen /*
579e3320f40Smarkfen * Since the OS controls what extensions are available, we don't have
580e3320f40Smarkfen * to parse numeric values here.
581e3320f40Smarkfen */
582e3320f40Smarkfen
583e3320f40Smarkfen *next = tp->next;
584e3320f40Smarkfen return (tp->token);
585e3320f40Smarkfen }
586e3320f40Smarkfen
587e3320f40Smarkfen /*
588e3320f40Smarkfen * Parse possible state values.
589e3320f40Smarkfen */
590e3320f40Smarkfen static uint8_t
parsestate(char * state,char * ebuf)591e3320f40Smarkfen parsestate(char *state, char *ebuf)
592e3320f40Smarkfen {
593e3320f40Smarkfen struct states {
594e3320f40Smarkfen char *state;
595e3320f40Smarkfen uint8_t retval;
596e3320f40Smarkfen } states[] = {
597e3320f40Smarkfen {"larval", SADB_SASTATE_LARVAL},
598e3320f40Smarkfen {"mature", SADB_SASTATE_MATURE},
599e3320f40Smarkfen {"dying", SADB_SASTATE_DYING},
600e3320f40Smarkfen {"dead", SADB_SASTATE_DEAD},
601e3320f40Smarkfen {NULL, 0}
602e3320f40Smarkfen };
603e3320f40Smarkfen struct states *sp;
604e3320f40Smarkfen char *ep = NULL;
605e3320f40Smarkfen
606e3320f40Smarkfen if (state == NULL) {
607e3320f40Smarkfen FATAL(ep, ebuf, "Unexpected end of command line "
608e3320f40Smarkfen "was expecting a state.\n");
609e3320f40Smarkfen }
610e3320f40Smarkfen
611e3320f40Smarkfen for (sp = states; sp->state != NULL; sp++) {
612e3320f40Smarkfen if (strcmp(sp->state, state) == 0)
613e3320f40Smarkfen return (sp->retval);
614e3320f40Smarkfen }
615e3320f40Smarkfen ERROR1(ep, ebuf, gettext("Unknown state type \"%s\"\n"), state);
616e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE);
617e3320f40Smarkfen return (0);
618e3320f40Smarkfen }
619e3320f40Smarkfen
620e3320f40Smarkfen /*
621e3320f40Smarkfen * Return the numerical algorithm identifier corresponding to the specified
622e3320f40Smarkfen * algorithm name.
623e3320f40Smarkfen */
624e3320f40Smarkfen static uint8_t
parsealg(char * alg,int proto_num,char * ebuf)625e3320f40Smarkfen parsealg(char *alg, int proto_num, char *ebuf)
626e3320f40Smarkfen {
627e3320f40Smarkfen u_longlong_t invalue;
628e3320f40Smarkfen struct ipsecalgent *algent;
629e3320f40Smarkfen char *ep = NULL;
630e3320f40Smarkfen
631e3320f40Smarkfen if (alg == NULL) {
632e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line, "
633e3320f40Smarkfen "was expecting an algorithm name.\n"));
634e3320f40Smarkfen }
635e3320f40Smarkfen
636e3320f40Smarkfen algent = getipsecalgbyname(alg, proto_num, NULL);
637e3320f40Smarkfen if (algent != NULL) {
638e3320f40Smarkfen uint8_t alg_num;
639e3320f40Smarkfen
640e3320f40Smarkfen alg_num = algent->a_alg_num;
641628b0c67SMark Fenwick if (ALG_FLAG_COUNTERMODE & algent->a_alg_flags)
642628b0c67SMark Fenwick WARN1(ep, ebuf, gettext(
643628b0c67SMark Fenwick "Using manual keying with a Counter mode algorithm "
644628b0c67SMark Fenwick "such as \"%s\" may be insecure!\n"),
645628b0c67SMark Fenwick algent->a_names[0]);
646e3320f40Smarkfen freeipsecalgent(algent);
647e3320f40Smarkfen
648e3320f40Smarkfen return (alg_num);
649e3320f40Smarkfen }
650e3320f40Smarkfen
651e3320f40Smarkfen /*
652e3320f40Smarkfen * Since algorithms can be loaded during kernel run-time, check for
653e3320f40Smarkfen * numeric algorithm values too. PF_KEY can catch bad ones with EINVAL.
654e3320f40Smarkfen */
655e3320f40Smarkfen invalue = parsenum(alg, B_FALSE, ebuf);
656e3320f40Smarkfen if (invalue != (u_longlong_t)-1 &&
657e3320f40Smarkfen (u_longlong_t)(invalue & (u_longlong_t)0xff) == invalue)
658e3320f40Smarkfen return ((uint8_t)invalue);
659e3320f40Smarkfen
660e3320f40Smarkfen if (proto_num == IPSEC_PROTO_ESP) {
661e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
662e3320f40Smarkfen "Unknown encryption algorithm type \"%s\"\n"), alg);
663e3320f40Smarkfen } else {
664e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
665e3320f40Smarkfen "Unknown authentication algorithm type \"%s\"\n"), alg);
666e3320f40Smarkfen }
667e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE);
668e3320f40Smarkfen return (0);
669e3320f40Smarkfen }
670e3320f40Smarkfen
671e3320f40Smarkfen /*
672e3320f40Smarkfen * Parse and reverse parse out a source/destination ID type.
673e3320f40Smarkfen */
674e3320f40Smarkfen static struct idtypes {
675e3320f40Smarkfen char *idtype;
676e3320f40Smarkfen uint8_t retval;
677e3320f40Smarkfen } idtypes[] = {
678e3320f40Smarkfen {"prefix", SADB_IDENTTYPE_PREFIX},
679e3320f40Smarkfen {"fqdn", SADB_IDENTTYPE_FQDN},
680e3320f40Smarkfen {"domain", SADB_IDENTTYPE_FQDN},
681e3320f40Smarkfen {"domainname", SADB_IDENTTYPE_FQDN},
682e3320f40Smarkfen {"user_fqdn", SADB_IDENTTYPE_USER_FQDN},
683e3320f40Smarkfen {"mailbox", SADB_IDENTTYPE_USER_FQDN},
684e3320f40Smarkfen {"der_dn", SADB_X_IDENTTYPE_DN},
685e3320f40Smarkfen {"der_gn", SADB_X_IDENTTYPE_GN},
686e3320f40Smarkfen {NULL, 0}
687e3320f40Smarkfen };
688e3320f40Smarkfen
689e3320f40Smarkfen static uint16_t
parseidtype(char * type,char * ebuf)690e3320f40Smarkfen parseidtype(char *type, char *ebuf)
691e3320f40Smarkfen {
692e3320f40Smarkfen struct idtypes *idp;
693e3320f40Smarkfen u_longlong_t invalue;
694e3320f40Smarkfen char *ep = NULL;
695e3320f40Smarkfen
696e3320f40Smarkfen if (type == NULL) {
697e3320f40Smarkfen /* Shouldn't reach here, see callers for why. */
698e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line, "
699e3320f40Smarkfen "was expecting a type.\n"));
700e3320f40Smarkfen }
701e3320f40Smarkfen
702e3320f40Smarkfen for (idp = idtypes; idp->idtype != NULL; idp++) {
703e3320f40Smarkfen if (strcasecmp(idp->idtype, type) == 0)
704e3320f40Smarkfen return (idp->retval);
705e3320f40Smarkfen }
706e3320f40Smarkfen /*
707e3320f40Smarkfen * Since identity types are almost arbitrary, check for numeric
708e3320f40Smarkfen * algorithm values too. PF_KEY can catch bad ones with EINVAL.
709e3320f40Smarkfen */
710e3320f40Smarkfen invalue = parsenum(type, B_FALSE, ebuf);
711e3320f40Smarkfen if (invalue != (u_longlong_t)-1 &&
712e3320f40Smarkfen (u_longlong_t)(invalue & (u_longlong_t)0xffff) == invalue)
713e3320f40Smarkfen return ((uint16_t)invalue);
714e3320f40Smarkfen
715e3320f40Smarkfen
716e3320f40Smarkfen ERROR1(ep, ebuf, gettext("Unknown identity type \"%s\"\n"), type);
717e3320f40Smarkfen
718e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE);
719e3320f40Smarkfen return (0);
720e3320f40Smarkfen }
721e3320f40Smarkfen
722e3320f40Smarkfen /*
723e3320f40Smarkfen * Parse an address off the command line. Return length of sockaddr,
724e3320f40Smarkfen * and either return a hostent pointer (caller frees). The new
725e3320f40Smarkfen * getipnodebyname() call does the Right Thing (TM), even with
726e3320f40Smarkfen * raw addresses (colon-separated IPv6 or dotted decimal IPv4).
727e3320f40Smarkfen */
728e3320f40Smarkfen
729e3320f40Smarkfen static struct {
730e3320f40Smarkfen struct hostent he;
731e3320f40Smarkfen char *addtl[2];
732e3320f40Smarkfen } dummy;
733e3320f40Smarkfen static union {
734e3320f40Smarkfen struct in6_addr ipv6;
735e3320f40Smarkfen struct in_addr ipv4;
736e3320f40Smarkfen uint64_t aligner;
737e3320f40Smarkfen } addr1;
738e3320f40Smarkfen
739e3320f40Smarkfen static int
parseaddr(char * addr,struct hostent ** hpp,boolean_t v6only,char * ebuf)740e3320f40Smarkfen parseaddr(char *addr, struct hostent **hpp, boolean_t v6only, char *ebuf)
741e3320f40Smarkfen {
742e3320f40Smarkfen int hp_errno;
743e3320f40Smarkfen struct hostent *hp = NULL;
744e3320f40Smarkfen char *ep = NULL;
745e3320f40Smarkfen
746e3320f40Smarkfen if (addr == NULL) {
747e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line, "
748e3320f40Smarkfen "was expecting an address.\n"));
749e3320f40Smarkfen }
750e3320f40Smarkfen
751e3320f40Smarkfen if (!nflag) {
752e3320f40Smarkfen /*
753e3320f40Smarkfen * Try name->address first. Assume AF_INET6, and
754e3320f40Smarkfen * get IPv4's, plus IPv6's if and only if IPv6 is configured.
755e3320f40Smarkfen * This means to add IPv6 SAs, you must have IPv6
756e3320f40Smarkfen * up-and-running. (AI_DEFAULT works here.)
757e3320f40Smarkfen */
758e3320f40Smarkfen hp = getipnodebyname(addr, AF_INET6,
759e3320f40Smarkfen (v6only ? AI_ADDRCONFIG : (AI_DEFAULT | AI_ALL)),
760e3320f40Smarkfen &hp_errno);
761e3320f40Smarkfen } else {
762e3320f40Smarkfen /*
763e3320f40Smarkfen * Try a normal address conversion only. Use "dummy"
764e3320f40Smarkfen * to construct a fake hostent. Caller will know not
765e3320f40Smarkfen * to free this one.
766e3320f40Smarkfen */
767e3320f40Smarkfen if (inet_pton(AF_INET6, addr, &addr1) == 1) {
768e3320f40Smarkfen dummy.he.h_addr_list = dummy.addtl;
769e3320f40Smarkfen dummy.addtl[0] = (char *)&addr1;
770e3320f40Smarkfen dummy.addtl[1] = NULL;
771e3320f40Smarkfen hp = &dummy.he;
772e3320f40Smarkfen dummy.he.h_addrtype = AF_INET6;
773e3320f40Smarkfen dummy.he.h_length = sizeof (struct in6_addr);
774e3320f40Smarkfen } else if (inet_pton(AF_INET, addr, &addr1) == 1) {
775e3320f40Smarkfen /*
776e3320f40Smarkfen * Remap to AF_INET6 anyway.
777e3320f40Smarkfen */
778e3320f40Smarkfen dummy.he.h_addr_list = dummy.addtl;
779e3320f40Smarkfen dummy.addtl[0] = (char *)&addr1;
780e3320f40Smarkfen dummy.addtl[1] = NULL;
781e3320f40Smarkfen hp = &dummy.he;
782e3320f40Smarkfen dummy.he.h_addrtype = AF_INET6;
783e3320f40Smarkfen dummy.he.h_length = sizeof (struct in6_addr);
784e3320f40Smarkfen /*
785e3320f40Smarkfen * NOTE: If macro changes to disallow in-place
786e3320f40Smarkfen * conversion, rewhack this.
787e3320f40Smarkfen */
788e3320f40Smarkfen IN6_INADDR_TO_V4MAPPED(&addr1.ipv4, &addr1.ipv6);
789e3320f40Smarkfen } else {
790e3320f40Smarkfen hp = NULL;
791e3320f40Smarkfen }
792e3320f40Smarkfen }
793e3320f40Smarkfen
794e3320f40Smarkfen if (hp == NULL)
795e3320f40Smarkfen WARN1(ep, ebuf, gettext("Unknown address %s."), addr);
796e3320f40Smarkfen
797e3320f40Smarkfen *hpp = hp;
798e3320f40Smarkfen /* Always return sockaddr_in6 for now. */
799e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE);
800e3320f40Smarkfen return (sizeof (struct sockaddr_in6));
801e3320f40Smarkfen }
802e3320f40Smarkfen
803e3320f40Smarkfen /*
804e3320f40Smarkfen * Parse a hex character for a key. A string will take the form:
805e3320f40Smarkfen * xxxxxxxxx/nn
806e3320f40Smarkfen * where
807e3320f40Smarkfen * xxxxxxxxx == a string of hex characters ([0-9][a-f][A-F])
808e3320f40Smarkfen * nn == an optional decimal "mask". If it is not present, it
809e3320f40Smarkfen * is assumed that the hex string will be rounded to the nearest
810e3320f40Smarkfen * byte, where odd nibbles, like 123 will become 0x0123.
811e3320f40Smarkfen *
812e3320f40Smarkfen * NOTE:Unlike the expression of IP addresses, I will not allow an
813e3320f40Smarkfen * excessive "mask". For example 2112/50 is very illegal.
814e3320f40Smarkfen * NOTE2: This key should be in canonical order. Consult your man
815e3320f40Smarkfen * pages per algorithm about said order.
816e3320f40Smarkfen */
817e3320f40Smarkfen
818e3320f40Smarkfen #define hd2num(hd) (((hd) >= '0' && (hd) <= '9') ? ((hd) - '0') : \
819e3320f40Smarkfen (((hd) >= 'a' && (hd) <= 'f') ? ((hd) - 'a' + 10) : ((hd) - 'A' + 10)))
820e3320f40Smarkfen
821e3320f40Smarkfen static struct sadb_key *
parsekey(char * input,char * ebuf,uint_t reserved_bits)822628b0c67SMark Fenwick parsekey(char *input, char *ebuf, uint_t reserved_bits)
823e3320f40Smarkfen {
824e3320f40Smarkfen struct sadb_key *retval;
825e3320f40Smarkfen uint_t i, hexlen = 0, bits, alloclen;
826e3320f40Smarkfen uint8_t *key;
827e3320f40Smarkfen char *ep = NULL;
828e3320f40Smarkfen
829e3320f40Smarkfen if (input == NULL) {
830e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line, "
831e3320f40Smarkfen "was expecting a key.\n"));
832e3320f40Smarkfen }
833ec485834Spwernau /* Allow hex values prepended with 0x convention */
834ec485834Spwernau if ((strnlen(input, sizeof (hexlen)) > 2) &&
835ec485834Spwernau (strncasecmp(input, "0x", 2) == 0))
836ec485834Spwernau input += 2;
837e3320f40Smarkfen
838e3320f40Smarkfen for (i = 0; input[i] != '\0' && input[i] != '/'; i++)
839e3320f40Smarkfen hexlen++;
840e3320f40Smarkfen
841e3320f40Smarkfen if (input[i] == '\0') {
842e3320f40Smarkfen bits = 0;
843e3320f40Smarkfen } else {
844e3320f40Smarkfen /* Have /nn. */
845e3320f40Smarkfen input[i] = '\0';
846e3320f40Smarkfen if (sscanf((input + i + 1), "%u", &bits) != 1) {
847e3320f40Smarkfen FATAL1(ep, ebuf, gettext(
848e3320f40Smarkfen "\"%s\" is not a bit specifier.\n"),
849e3320f40Smarkfen (input + i + 1));
850e3320f40Smarkfen }
851e3320f40Smarkfen /* hexlen in nibbles */
852e3320f40Smarkfen if (((bits + 3) >> 2) > hexlen) {
853e3320f40Smarkfen ERROR2(ep, ebuf, gettext(
854e3320f40Smarkfen "bit length %d is too big for %s.\n"), bits, input);
855e3320f40Smarkfen }
856e3320f40Smarkfen /*
857e3320f40Smarkfen * Adjust hexlen down if user gave us too small of a bit
858e3320f40Smarkfen * count.
859e3320f40Smarkfen */
860e3320f40Smarkfen if ((hexlen << 2) > bits + 3) {
861e3320f40Smarkfen WARN2(ep, ebuf, gettext(
862e3320f40Smarkfen "WARNING: Lower bits will be truncated "
863e3320f40Smarkfen "for:\n\t%s/%d.\n"), input, bits);
864e3320f40Smarkfen hexlen = (bits + 3) >> 2;
865e3320f40Smarkfen input[hexlen] = '\0';
866e3320f40Smarkfen }
867e3320f40Smarkfen }
868e3320f40Smarkfen
869e3320f40Smarkfen /*
870e3320f40Smarkfen * Allocate. Remember, hexlen is in nibbles.
871e3320f40Smarkfen */
872e3320f40Smarkfen
873e3320f40Smarkfen alloclen = sizeof (*retval) + roundup((hexlen/2 + (hexlen & 0x1)), 8);
874e3320f40Smarkfen retval = malloc(alloclen);
875e3320f40Smarkfen
876e3320f40Smarkfen if (retval == NULL)
877e3320f40Smarkfen Bail("malloc(parsekey)");
878e3320f40Smarkfen retval->sadb_key_len = SADB_8TO64(alloclen);
879628b0c67SMark Fenwick
880628b0c67SMark Fenwick retval->sadb_key_reserved = reserved_bits;
881628b0c67SMark Fenwick
882e3320f40Smarkfen if (bits == 0)
883e3320f40Smarkfen retval->sadb_key_bits = (hexlen + (hexlen & 0x1)) << 2;
884e3320f40Smarkfen else
885e3320f40Smarkfen retval->sadb_key_bits = bits;
886e3320f40Smarkfen
887e3320f40Smarkfen /*
888e3320f40Smarkfen * Read in nibbles. Read in odd-numbered as shifted high.
889e3320f40Smarkfen * (e.g. 123 becomes 0x1230).
890e3320f40Smarkfen */
891e3320f40Smarkfen
892e3320f40Smarkfen key = (uint8_t *)(retval + 1);
893e3320f40Smarkfen for (i = 0; input[i] != '\0'; i += 2) {
894e3320f40Smarkfen boolean_t second = (input[i + 1] != '\0');
895e3320f40Smarkfen
896e3320f40Smarkfen if (!isxdigit(input[i]) ||
897e3320f40Smarkfen (!isxdigit(input[i + 1]) && second)) {
898e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
899e3320f40Smarkfen "string '%s' not a hex value.\n"), input);
900e3320f40Smarkfen free(retval);
901e3320f40Smarkfen retval = NULL;
902e3320f40Smarkfen break;
903e3320f40Smarkfen }
904e3320f40Smarkfen *key = (hd2num(input[i]) << 4);
905e3320f40Smarkfen if (second)
906e3320f40Smarkfen *key |= hd2num(input[i + 1]);
907e3320f40Smarkfen else
908e3320f40Smarkfen break; /* out of for loop. */
909e3320f40Smarkfen key++;
910e3320f40Smarkfen }
911e3320f40Smarkfen
912e3320f40Smarkfen /* bzero the remaining bits if we're a non-octet amount. */
913e3320f40Smarkfen if (bits & 0x7)
914e3320f40Smarkfen *((input[i] == '\0') ? key - 1 : key) &=
915e3320f40Smarkfen 0xff << (8 - (bits & 0x7));
916e3320f40Smarkfen
917e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE);
918e3320f40Smarkfen return (retval);
919e3320f40Smarkfen }
920e3320f40Smarkfen
9215d3b8cb7SBill Sommerfeld #include <tsol/label.h>
9225d3b8cb7SBill Sommerfeld
9235d3b8cb7SBill Sommerfeld #define PARSELABEL_BAD_TOKEN ((struct sadb_sens *)-1)
9245d3b8cb7SBill Sommerfeld
9255d3b8cb7SBill Sommerfeld static struct sadb_sens *
parselabel(int token,char * label)9265d3b8cb7SBill Sommerfeld parselabel(int token, char *label)
9275d3b8cb7SBill Sommerfeld {
9285d3b8cb7SBill Sommerfeld bslabel_t *sl = NULL;
9295d3b8cb7SBill Sommerfeld int err, len;
9305d3b8cb7SBill Sommerfeld sadb_sens_t *sens;
9315d3b8cb7SBill Sommerfeld int doi = 1; /* XXX XXX DEFAULT_DOI XXX XXX */
9325d3b8cb7SBill Sommerfeld
9335d3b8cb7SBill Sommerfeld err = str_to_label(label, &sl, MAC_LABEL, L_DEFAULT, NULL);
9345d3b8cb7SBill Sommerfeld if (err < 0)
9355d3b8cb7SBill Sommerfeld return (NULL);
9365d3b8cb7SBill Sommerfeld
9375d3b8cb7SBill Sommerfeld len = ipsec_convert_sl_to_sens(doi, sl, NULL);
9385d3b8cb7SBill Sommerfeld
9395d3b8cb7SBill Sommerfeld sens = malloc(len);
9405d3b8cb7SBill Sommerfeld if (sens == NULL) {
9415d3b8cb7SBill Sommerfeld Bail("malloc parsed label");
9425d3b8cb7SBill Sommerfeld /* Should exit before reaching here... */
9435d3b8cb7SBill Sommerfeld return (NULL);
9445d3b8cb7SBill Sommerfeld }
9455d3b8cb7SBill Sommerfeld
9465d3b8cb7SBill Sommerfeld (void) ipsec_convert_sl_to_sens(doi, sl, sens);
9475d3b8cb7SBill Sommerfeld
9485d3b8cb7SBill Sommerfeld switch (token) {
9495d3b8cb7SBill Sommerfeld case TOK_LABEL:
9505d3b8cb7SBill Sommerfeld break;
9515d3b8cb7SBill Sommerfeld
9525d3b8cb7SBill Sommerfeld case TOK_OLABEL:
9535d3b8cb7SBill Sommerfeld sens->sadb_sens_exttype = SADB_X_EXT_OUTER_SENS;
9545d3b8cb7SBill Sommerfeld break;
9555d3b8cb7SBill Sommerfeld
9565d3b8cb7SBill Sommerfeld case TOK_IMPLABEL:
9575d3b8cb7SBill Sommerfeld sens->sadb_sens_exttype = SADB_X_EXT_OUTER_SENS;
9585d3b8cb7SBill Sommerfeld sens->sadb_x_sens_flags = SADB_X_SENS_IMPLICIT;
9595d3b8cb7SBill Sommerfeld break;
9605d3b8cb7SBill Sommerfeld
9615d3b8cb7SBill Sommerfeld default:
9625d3b8cb7SBill Sommerfeld free(sens);
9635d3b8cb7SBill Sommerfeld /*
9645d3b8cb7SBill Sommerfeld * Return a different return code for a bad label, but really,
9655d3b8cb7SBill Sommerfeld * this would be a caller error.
9665d3b8cb7SBill Sommerfeld */
9675d3b8cb7SBill Sommerfeld return (PARSELABEL_BAD_TOKEN);
9685d3b8cb7SBill Sommerfeld }
9695d3b8cb7SBill Sommerfeld
9705d3b8cb7SBill Sommerfeld return (sens);
9715d3b8cb7SBill Sommerfeld }
9725d3b8cb7SBill Sommerfeld
973e3320f40Smarkfen /*
974e3320f40Smarkfen * Write a message to the PF_KEY socket. If verbose, print the message
975e3320f40Smarkfen * heading into the kernel.
976e3320f40Smarkfen */
977e3320f40Smarkfen static int
key_write(int fd,void * msg,size_t len)978e3320f40Smarkfen key_write(int fd, void *msg, size_t len)
979e3320f40Smarkfen {
980e3320f40Smarkfen if (vflag) {
981e3320f40Smarkfen (void) printf(
982e3320f40Smarkfen gettext("VERBOSE ON: Message to kernel looks like:\n"));
983e3320f40Smarkfen (void) printf("==========================================\n");
984bb3ed8dfSpwernau print_samsg(stdout, msg, B_FALSE, vflag, nflag);
985e3320f40Smarkfen (void) printf("==========================================\n");
986e3320f40Smarkfen }
987e3320f40Smarkfen
988e3320f40Smarkfen return (write(fd, msg, len));
989e3320f40Smarkfen }
990e3320f40Smarkfen
991e3320f40Smarkfen /*
992e3320f40Smarkfen * SIGALRM handler for time_critical_enter.
993e3320f40Smarkfen */
994e3320f40Smarkfen static void
time_critical_catch(int signal)995e3320f40Smarkfen time_critical_catch(int signal)
996e3320f40Smarkfen {
997e3320f40Smarkfen if (signal == SIGALRM) {
998e3320f40Smarkfen errx(1, gettext("Reply message from PF_KEY timed out."));
999e3320f40Smarkfen } else {
1000e3320f40Smarkfen errx(1, gettext("Caught signal %d while trying to receive"
1001e3320f40Smarkfen "PF_KEY reply message"), signal);
1002e3320f40Smarkfen }
1003e3320f40Smarkfen /* errx() calls exit. */
1004e3320f40Smarkfen }
1005e3320f40Smarkfen
1006e3320f40Smarkfen #define TIME_CRITICAL_TIME 10 /* In seconds */
1007e3320f40Smarkfen
1008e3320f40Smarkfen /*
1009e3320f40Smarkfen * Enter a "time critical" section where key is waiting for a return message.
1010e3320f40Smarkfen */
1011e3320f40Smarkfen static void
time_critical_enter(void)1012e3320f40Smarkfen time_critical_enter(void)
1013e3320f40Smarkfen {
1014e3320f40Smarkfen (void) signal(SIGALRM, time_critical_catch);
1015e3320f40Smarkfen (void) alarm(TIME_CRITICAL_TIME);
1016e3320f40Smarkfen }
1017e3320f40Smarkfen
1018e3320f40Smarkfen /*
1019e3320f40Smarkfen * Exit the "time critical" section after getting an appropriate return
1020e3320f40Smarkfen * message.
1021e3320f40Smarkfen */
1022e3320f40Smarkfen static void
time_critical_exit(void)1023e3320f40Smarkfen time_critical_exit(void)
1024e3320f40Smarkfen {
1025e3320f40Smarkfen (void) alarm(0);
1026e3320f40Smarkfen (void) signal(SIGALRM, SIG_DFL);
1027e3320f40Smarkfen }
1028e3320f40Smarkfen
1029e3320f40Smarkfen /*
1030e3320f40Smarkfen * Construct a PF_KEY FLUSH message for the SA type specified.
1031e3320f40Smarkfen */
1032e3320f40Smarkfen static void
doflush(int satype)1033e3320f40Smarkfen doflush(int satype)
1034e3320f40Smarkfen {
1035e3320f40Smarkfen struct sadb_msg msg;
1036e3320f40Smarkfen int rc;
1037e3320f40Smarkfen
1038e3320f40Smarkfen msg_init(&msg, SADB_FLUSH, (uint8_t)satype);
1039e3320f40Smarkfen rc = key_write(keysock, &msg, sizeof (msg));
1040e3320f40Smarkfen if (rc == -1)
1041e3320f40Smarkfen Bail("write() to PF_KEY socket failed (in doflush)");
1042e3320f40Smarkfen
1043e3320f40Smarkfen time_critical_enter();
1044e3320f40Smarkfen do {
1045e3320f40Smarkfen rc = read(keysock, &msg, sizeof (msg));
1046e3320f40Smarkfen if (rc == -1)
1047e3320f40Smarkfen Bail("read (in doflush)");
1048e3320f40Smarkfen } while (msg.sadb_msg_seq != seq || msg.sadb_msg_pid != mypid);
1049e3320f40Smarkfen time_critical_exit();
1050e3320f40Smarkfen
1051e3320f40Smarkfen /*
1052e3320f40Smarkfen * I should _never_ hit the following unless:
1053e3320f40Smarkfen *
1054e3320f40Smarkfen * 1. There is a kernel bug.
1055e3320f40Smarkfen * 2. There is another process filling in its pid with mine, and
1056e3320f40Smarkfen * issuing a different message that would cause a different result.
1057e3320f40Smarkfen */
1058e3320f40Smarkfen if (msg.sadb_msg_type != SADB_FLUSH ||
1059e3320f40Smarkfen msg.sadb_msg_satype != (uint8_t)satype) {
1060e3320f40Smarkfen syslog((LOG_NOTICE|LOG_AUTH),
1061e3320f40Smarkfen gettext("doflush: Return message not of type SADB_FLUSH!"));
1062e3320f40Smarkfen Bail("doflush: Return message not of type SADB_FLUSH!");
1063e3320f40Smarkfen }
1064e3320f40Smarkfen
1065e3320f40Smarkfen if (msg.sadb_msg_errno != 0) {
1066e3320f40Smarkfen errno = msg.sadb_msg_errno;
1067e3320f40Smarkfen if (errno == EINVAL) {
1068e3320f40Smarkfen print_diagnostic(stderr, msg.sadb_x_msg_diagnostic);
1069e3320f40Smarkfen warnx(gettext("Cannot flush SA type %d."), satype);
1070e3320f40Smarkfen }
1071e3320f40Smarkfen Bail("return message (in doflush)");
1072e3320f40Smarkfen }
1073e3320f40Smarkfen }
1074e3320f40Smarkfen
1075e3320f40Smarkfen /*
1076e3320f40Smarkfen * save_XXX functions are used when "saving" the SA tables to either a
1077e3320f40Smarkfen * file or standard output. They use the dump_XXX functions where needed,
1078e3320f40Smarkfen * but mostly they use the rparseXXX functions.
1079e3320f40Smarkfen */
1080e3320f40Smarkfen
1081e3320f40Smarkfen /*
1082e3320f40Smarkfen * Because "save" and "dump" both use the SADB_DUMP message, fold both
1083e3320f40Smarkfen * into the same function.
1084e3320f40Smarkfen */
1085e3320f40Smarkfen static void
dodump(int satype,FILE * ofile)1086e3320f40Smarkfen dodump(int satype, FILE *ofile)
1087e3320f40Smarkfen {
1088e3320f40Smarkfen struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
1089e3320f40Smarkfen int rc;
1090e3320f40Smarkfen
1091e3320f40Smarkfen if (ofile != NULL) {
1092e3320f40Smarkfen (void) fprintf(ofile,
1093e3320f40Smarkfen gettext("# This key file was generated by the"));
1094e3320f40Smarkfen (void) fprintf(ofile,
1095e3320f40Smarkfen gettext(" ipseckey(1m) command's 'save' feature.\n\n"));
1096e3320f40Smarkfen }
1097e3320f40Smarkfen msg_init(msg, SADB_DUMP, (uint8_t)satype);
1098e3320f40Smarkfen rc = key_write(keysock, msg, sizeof (*msg));
1099e3320f40Smarkfen if (rc == -1)
1100e3320f40Smarkfen Bail("write to PF_KEY socket failed (in dodump)");
1101e3320f40Smarkfen
1102e3320f40Smarkfen do {
1103e3320f40Smarkfen /*
1104e3320f40Smarkfen * For DUMP, do only the read as a time critical section.
1105e3320f40Smarkfen */
1106e3320f40Smarkfen time_critical_enter();
1107e3320f40Smarkfen rc = read(keysock, get_buffer, sizeof (get_buffer));
1108e3320f40Smarkfen time_critical_exit();
1109e3320f40Smarkfen if (rc == -1)
1110e3320f40Smarkfen Bail("read (in dodump)");
1111e3320f40Smarkfen if (msg->sadb_msg_pid == mypid &&
1112e3320f40Smarkfen msg->sadb_msg_type == SADB_DUMP &&
1113e3320f40Smarkfen msg->sadb_msg_seq != 0 &&
1114e3320f40Smarkfen msg->sadb_msg_errno == 0) {
1115e3320f40Smarkfen if (ofile == NULL) {
1116bb3ed8dfSpwernau print_samsg(stdout, get_buffer, B_FALSE, vflag,
1117bb3ed8dfSpwernau nflag);
1118e3320f40Smarkfen (void) putchar('\n');
1119e3320f40Smarkfen } else {
1120e3320f40Smarkfen save_assoc(get_buffer, ofile);
1121e3320f40Smarkfen }
1122e3320f40Smarkfen }
1123e3320f40Smarkfen } while (msg->sadb_msg_pid != mypid ||
1124e3320f40Smarkfen (msg->sadb_msg_errno == 0 && msg->sadb_msg_seq != 0));
1125e3320f40Smarkfen
1126e3320f40Smarkfen if (ofile != NULL && ofile != stdout)
1127e3320f40Smarkfen (void) fclose(ofile);
1128e3320f40Smarkfen
1129e3320f40Smarkfen if (msg->sadb_msg_errno == 0) {
1130e3320f40Smarkfen if (ofile == NULL)
1131e3320f40Smarkfen (void) printf(
1132e3320f40Smarkfen gettext("Dump succeeded for SA type %d.\n"),
1133e3320f40Smarkfen satype);
1134e3320f40Smarkfen } else {
1135e3320f40Smarkfen print_diagnostic(stderr, msg->sadb_x_msg_diagnostic);
1136e3320f40Smarkfen errno = msg->sadb_msg_errno;
1137e3320f40Smarkfen Bail("Dump failed");
1138e3320f40Smarkfen }
1139e3320f40Smarkfen }
1140e3320f40Smarkfen
1141e3320f40Smarkfen #define SCOPE_UNSPEC 0
1142e3320f40Smarkfen #define SCOPE_LINKLOCAL 1
1143e3320f40Smarkfen #define SCOPE_SITELOCAL 2
1144e3320f40Smarkfen #define SCOPE_GLOBAL 3
1145e3320f40Smarkfen #define SCOPE_V4COMPAT 4
1146e3320f40Smarkfen #define SCOPE_LOOPBACK 5 /* Pedantic, yes, but necessary. */
1147e3320f40Smarkfen
1148e3320f40Smarkfen static int
ipv6_addr_scope(struct in6_addr * addr)1149e3320f40Smarkfen ipv6_addr_scope(struct in6_addr *addr)
1150e3320f40Smarkfen {
1151e3320f40Smarkfen /* Don't return anything regarding multicast for now... */
1152e3320f40Smarkfen
1153e3320f40Smarkfen if (IN6_IS_ADDR_UNSPECIFIED(addr))
1154e3320f40Smarkfen return (SCOPE_UNSPEC);
1155e3320f40Smarkfen
1156e3320f40Smarkfen if (IN6_IS_ADDR_LINKLOCAL(addr))
1157e3320f40Smarkfen return (SCOPE_LINKLOCAL);
1158e3320f40Smarkfen
1159e3320f40Smarkfen if (IN6_IS_ADDR_SITELOCAL(addr))
1160e3320f40Smarkfen return (SCOPE_SITELOCAL);
1161e3320f40Smarkfen
1162e3320f40Smarkfen if (IN6_IS_ADDR_V4COMPAT(addr))
1163e3320f40Smarkfen return (SCOPE_V4COMPAT);
1164e3320f40Smarkfen
1165e3320f40Smarkfen if (IN6_IS_ADDR_LOOPBACK(addr))
1166e3320f40Smarkfen return (SCOPE_LOOPBACK);
1167e3320f40Smarkfen
1168e3320f40Smarkfen /* For now, return global by default. */
1169e3320f40Smarkfen return (SCOPE_GLOBAL);
1170e3320f40Smarkfen }
1171e3320f40Smarkfen
1172e3320f40Smarkfen /*
1173e3320f40Smarkfen * doaddresses():
1174e3320f40Smarkfen *
1175e3320f40Smarkfen * Used by doaddup() and dodelget() to create new SA's based on the
1176e3320f40Smarkfen * provided source and destination addresses hostent.
1177e3320f40Smarkfen *
1178e3320f40Smarkfen * sadb_msg_type: expected PF_KEY reply message type
1179e3320f40Smarkfen * sadb_msg_satype: expected PF_KEY reply satype
1180e3320f40Smarkfen * cmd: user command
1181e3320f40Smarkfen * srchp: hostent for the source address(es)
1182e3320f40Smarkfen * dsthp: hostent for the destination address(es)
1183e3320f40Smarkfen * src: points to the SADB source address extension
1184e3320f40Smarkfen * dst: points to the SADB destination address extension
1185e3320f40Smarkfen * unspec_src: indicates an unspecified source address.
1186e3320f40Smarkfen * buffer: pointer to the SADB buffer to use with PF_KEY
1187e3320f40Smarkfen * buffer_size: size of buffer
1188e3320f40Smarkfen * spi: spi for this message (set by caller)
1189e3320f40Smarkfen * srcport: source port if specified
1190e3320f40Smarkfen * dstport: destination port if specified
1191e3320f40Smarkfen * proto: IP protocol number if specified
1192e3320f40Smarkfen * iproto: Inner (tunnel mode) IP protocol number if specified
1193e3320f40Smarkfen * NATT note: we are going to assume a semi-sane world where NAT
1194e3320f40Smarkfen * boxen don't explode to multiple addresses.
1195e3320f40Smarkfen */
1196e3320f40Smarkfen static void
doaddresses(uint8_t sadb_msg_type,uint8_t sadb_msg_satype,int cmd,struct hostent * srchp,struct hostent * dsthp,struct sadb_address * src,struct sadb_address * dst,boolean_t unspec_src,uint64_t * buffer,int buffer_size,uint32_t spi,char * ebuf)1197e3320f40Smarkfen doaddresses(uint8_t sadb_msg_type, uint8_t sadb_msg_satype, int cmd,
1198e3320f40Smarkfen struct hostent *srchp, struct hostent *dsthp,
1199e3320f40Smarkfen struct sadb_address *src, struct sadb_address *dst,
1200e3320f40Smarkfen boolean_t unspec_src, uint64_t *buffer, int buffer_size, uint32_t spi,
1201e3320f40Smarkfen char *ebuf)
1202e3320f40Smarkfen {
1203*f02131e0SVladimir Kotal boolean_t last_dst;
1204e3320f40Smarkfen struct sockaddr_in6 *sin6;
1205e3320f40Smarkfen struct sadb_msg *msgp;
1206e3320f40Smarkfen int i, rc;
1207e3320f40Smarkfen char **walker; /* For the SRC and PROXY walking functions. */
1208e3320f40Smarkfen char *first_match;
1209e3320f40Smarkfen uint64_t savebuf[MAX_GET_SIZE];
1210e3320f40Smarkfen uint16_t srcport = 0, dstport = 0;
1211e3320f40Smarkfen char *ep = NULL;
1212e3320f40Smarkfen
1213e3320f40Smarkfen /*
1214e3320f40Smarkfen * Okay, now we have "src", "dst", and maybe "proxy" reassigned
1215e3320f40Smarkfen * to point into the buffer to be written to PF_KEY, we can do
1216e3320f40Smarkfen * potentially several writes based on destination address.
1217e3320f40Smarkfen *
1218e3320f40Smarkfen * First, obtain port numbers from passed-in extensions.
1219e3320f40Smarkfen */
1220e3320f40Smarkfen
1221e3320f40Smarkfen if (src != NULL) {
1222e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(src + 1);
1223e3320f40Smarkfen srcport = ntohs(sin6->sin6_port);
1224e3320f40Smarkfen }
1225e3320f40Smarkfen if (dst != NULL) {
1226e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(dst + 1);
1227e3320f40Smarkfen dstport = ntohs(sin6->sin6_port);
1228e3320f40Smarkfen }
1229e3320f40Smarkfen
1230e3320f40Smarkfen /*
1231e3320f40Smarkfen * The rules for ADD, GET, and UPDATE: (NOTE: This assumes IPsec.
1232e3320f40Smarkfen * If other consumers of PF_KEY happen, this will have to be
1233e3320f40Smarkfen * rewhacked.):
1234e3320f40Smarkfen *
1235e3320f40Smarkfen * Do a message for every possible DST address.
1236e3320f40Smarkfen *
1237e3320f40Smarkfen * If a source or proxy address explodes, keep unspecified
1238e3320f40Smarkfen * (and mention unspecified).
1239e3320f40Smarkfen *
1240e3320f40Smarkfen * DELETE is different, because you can leave either "src" or "dst"
1241e3320f40Smarkfen * blank! You need to explode if one of them is full, and not assume
1242e3320f40Smarkfen * that the other is set.
1243e3320f40Smarkfen */
1244e3320f40Smarkfen
1245e3320f40Smarkfen if (dsthp == NULL) {
1246e3320f40Smarkfen /*
1247e3320f40Smarkfen * No destination address specified.
1248e3320f40Smarkfen * With extended diagnostics, we don't have to bail the
1249e3320f40Smarkfen * non-DELETE cases here. The EINVAL diagnostics will be
1250e3320f40Smarkfen * enough to inform the user(s) what happened.
1251e3320f40Smarkfen */
1252e3320f40Smarkfen i = 0;
1253e3320f40Smarkfen do {
1254e3320f40Smarkfen if (srchp == &dummy.he) {
1255e3320f40Smarkfen /* Just to be sure... */
1256e3320f40Smarkfen srchp->h_addr_list[1] = NULL;
1257e3320f40Smarkfen } else if (srchp != NULL) {
1258e3320f40Smarkfen /* Degenerate case, h_addr_list[0] == NULL. */
1259e3320f40Smarkfen if (srchp->h_addr_list[i] == NULL)
1260e3320f40Smarkfen Bail("Empty source address list");
1261e3320f40Smarkfen
1262e3320f40Smarkfen /*
1263e3320f40Smarkfen * Fill in the src sockaddr.
1264e3320f40Smarkfen */
1265e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(src + 1);
1266e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
1267e3320f40Smarkfen bcopy(srchp->h_addr_list[i], &sin6->sin6_addr,
1268e3320f40Smarkfen sizeof (struct in6_addr));
1269e3320f40Smarkfen sin6->sin6_family = AF_INET6;
1270e3320f40Smarkfen sin6->sin6_port = htons(srcport);
1271e3320f40Smarkfen }
1272e3320f40Smarkfen
1273e3320f40Smarkfen /* Save off a copy for later writing... */
1274e3320f40Smarkfen msgp = (struct sadb_msg *)buffer;
1275e3320f40Smarkfen bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
1276e3320f40Smarkfen
1277e3320f40Smarkfen rc = key_write(keysock, buffer,
1278e3320f40Smarkfen SADB_64TO8(msgp->sadb_msg_len));
1279e3320f40Smarkfen if (rc == -1)
1280e3320f40Smarkfen Bail("write() to PF_KEY socket "
1281e3320f40Smarkfen "(in doaddresses)");
12829c2c14abSThejaswini Singarajipura /*
12839c2c14abSThejaswini Singarajipura * Sends the message to the Solaris Cluster daemon
12849c2c14abSThejaswini Singarajipura */
12859c2c14abSThejaswini Singarajipura
12869c2c14abSThejaswini Singarajipura if (in_cluster_mode) {
12879c2c14abSThejaswini Singarajipura (void) sendto(cluster_socket, buffer,
12889c2c14abSThejaswini Singarajipura SADB_64TO8(msgp->sadb_msg_len), 0,
12899c2c14abSThejaswini Singarajipura (struct sockaddr *)&cli_addr,
12909c2c14abSThejaswini Singarajipura sizeof (cli_addr));
12919c2c14abSThejaswini Singarajipura }
1292e3320f40Smarkfen
1293e3320f40Smarkfen time_critical_enter();
1294e3320f40Smarkfen do {
1295e3320f40Smarkfen rc = read(keysock, buffer, buffer_size);
1296e3320f40Smarkfen if (rc == -1)
1297e3320f40Smarkfen Bail("read (in doaddresses)");
1298e3320f40Smarkfen } while (msgp->sadb_msg_seq != seq ||
1299e3320f40Smarkfen msgp->sadb_msg_pid != mypid);
1300e3320f40Smarkfen time_critical_exit();
1301e3320f40Smarkfen
1302e3320f40Smarkfen if (msgp->sadb_msg_type != sadb_msg_type ||
1303e3320f40Smarkfen msgp->sadb_msg_satype != sadb_msg_satype) {
1304e3320f40Smarkfen syslog((LOG_NOTICE|LOG_AUTH), gettext(
1305e3320f40Smarkfen "doaddresses: Unexpected returned message "
1306e3320f40Smarkfen "(%d exp %d)\n"), msgp->sadb_msg_type,
1307e3320f40Smarkfen sadb_msg_type);
1308e3320f40Smarkfen Bail("doaddresses: Unexpected returned "
1309e3320f40Smarkfen "message");
1310e3320f40Smarkfen }
1311e3320f40Smarkfen
1312e3320f40Smarkfen errno = msgp->sadb_msg_errno;
1313e3320f40Smarkfen if (errno != 0) {
1314e3320f40Smarkfen if (errno == EINVAL) {
1315e3320f40Smarkfen WARN(ep, ebuf, gettext(
1316e3320f40Smarkfen "One of the entered "
1317e3320f40Smarkfen "values is incorrect."));
1318e3320f40Smarkfen print_diagnostic(stderr,
1319e3320f40Smarkfen msgp->sadb_x_msg_diagnostic);
1320e3320f40Smarkfen } else {
1321e3320f40Smarkfen Bail("return message (in doaddresses)");
1322e3320f40Smarkfen }
1323e3320f40Smarkfen }
1324e3320f40Smarkfen
1325e3320f40Smarkfen /* ...and then restore the saved buffer. */
1326e3320f40Smarkfen msgp = (struct sadb_msg *)savebuf;
1327e3320f40Smarkfen bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
1328e3320f40Smarkfen } while (srchp != NULL && srchp->h_addr_list[++i] != NULL);
1329e3320f40Smarkfen return;
1330e3320f40Smarkfen }
1331e3320f40Smarkfen
1332*f02131e0SVladimir Kotal /*
1333*f02131e0SVladimir Kotal * Go through the list of all dst addresses, trying to find matching
1334*f02131e0SVladimir Kotal * src address for each. If the first address is == dummy.he we will go
1335*f02131e0SVladimir Kotal * through the loop just once. If any other hp is == dummy.he, then we
1336*f02131e0SVladimir Kotal * don't have to apply any silly rules.
1337*f02131e0SVladimir Kotal */
1338e3320f40Smarkfen for (i = 0; dsthp->h_addr_list[i] != NULL; i++) {
1339e3320f40Smarkfen if (dsthp == &dummy.he) {
1340e3320f40Smarkfen /* Just to be sure... */
1341e3320f40Smarkfen dsthp->h_addr_list[1] = NULL;
1342e3320f40Smarkfen } else {
1343e3320f40Smarkfen /*
1344e3320f40Smarkfen * Fill in the dst sockaddr.
1345e3320f40Smarkfen */
1346e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(dst + 1);
1347e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
1348e3320f40Smarkfen bcopy(dsthp->h_addr_list[i], &sin6->sin6_addr,
1349e3320f40Smarkfen sizeof (struct in6_addr));
1350e3320f40Smarkfen sin6->sin6_family = AF_INET6;
1351e3320f40Smarkfen sin6->sin6_port = htons(dstport);
1352e3320f40Smarkfen }
1353e3320f40Smarkfen
1354*f02131e0SVladimir Kotal last_dst = (dsthp->h_addr_list[i + 1] == NULL);
1355*f02131e0SVladimir Kotal
1356e3320f40Smarkfen /*
1357e3320f40Smarkfen * Try and assign src, if there's any ambiguity.
1358e3320f40Smarkfen */
1359e3320f40Smarkfen if (!unspec_src && srchp != &dummy.he) {
1360e3320f40Smarkfen if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1361e3320f40Smarkfen /*
1362e3320f40Smarkfen * IPv4 address. Find an IPv4 address, then
1363e3320f40Smarkfen * keep looking for a second one. If a second
1364e3320f40Smarkfen * exists, print a message, and fill in the
1365e3320f40Smarkfen * unspecified address.
1366e3320f40Smarkfen */
1367e3320f40Smarkfen first_match = NULL;
1368e3320f40Smarkfen
1369e3320f40Smarkfen for (walker = srchp->h_addr_list;
1370e3320f40Smarkfen *walker != NULL; walker++) {
1371e3320f40Smarkfen /* LINTED E_BAD_PTR_CAST_ALIGN */
1372e3320f40Smarkfen if (IN6_IS_ADDR_V4MAPPED(
1373e3320f40Smarkfen (struct in6_addr *)*walker)) {
1374e3320f40Smarkfen if (first_match != NULL)
1375e3320f40Smarkfen break;
1376e3320f40Smarkfen else
1377e3320f40Smarkfen first_match = *walker;
1378e3320f40Smarkfen }
1379e3320f40Smarkfen }
1380e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(src + 1);
1381e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
1382e3320f40Smarkfen
1383e3320f40Smarkfen if (first_match == NULL) {
1384e3320f40Smarkfen /*
1385*f02131e0SVladimir Kotal * No IPv4 hits. Is this the last
1386*f02131e0SVladimir Kotal * destination address in the list ?
1387e3320f40Smarkfen */
1388*f02131e0SVladimir Kotal ERROR1(ep, ebuf, gettext(
1389e3320f40Smarkfen "No IPv4 source address "
1390e3320f40Smarkfen "for name %s.\n"), srchp->h_name);
1391*f02131e0SVladimir Kotal if (last_dst) {
1392*f02131e0SVladimir Kotal FATAL(ep, ebuf, gettext(
1393*f02131e0SVladimir Kotal "No match for destination "
1394e3320f40Smarkfen "IP address.\n"));
1395e3320f40Smarkfen } else {
1396e3320f40Smarkfen /* Continue, but do I print? */
1397e3320f40Smarkfen continue; /* for loop */
1398e3320f40Smarkfen }
1399e3320f40Smarkfen
1400e3320f40Smarkfen /* I should never reach here. */
1401e3320f40Smarkfen }
1402e3320f40Smarkfen
1403e3320f40Smarkfen sin6->sin6_family = AF_INET6;
1404e3320f40Smarkfen sin6->sin6_port = htons(srcport);
1405e3320f40Smarkfen if (*walker != NULL) {
1406e3320f40Smarkfen /*
1407e3320f40Smarkfen * Early loop exit. It must've been
1408e3320f40Smarkfen * multiple hits...
1409e3320f40Smarkfen *
1410e3320f40Smarkfen * Issue a null-source warning?
1411e3320f40Smarkfen */
1412e3320f40Smarkfen WARN1(ep, ebuf, gettext(
1413e3320f40Smarkfen "Multiple IPv4 source addresses "
1414e3320f40Smarkfen "for %s, using unspecified source "
1415e3320f40Smarkfen "instead."), srchp->h_name);
1416e3320f40Smarkfen } else {
1417e3320f40Smarkfen /*
1418e3320f40Smarkfen * If I reach here w/o hitting the
1419e3320f40Smarkfen * previous if statements, I have a
1420e3320f40Smarkfen * single source address for this
1421e3320f40Smarkfen * destination.
1422e3320f40Smarkfen */
1423e3320f40Smarkfen bcopy(first_match, &sin6->sin6_addr,
1424e3320f40Smarkfen sizeof (struct in6_addr));
1425e3320f40Smarkfen }
1426e3320f40Smarkfen } else {
1427e3320f40Smarkfen /*
1428e3320f40Smarkfen * IPv6 address. Find an IPv6 address.
1429e3320f40Smarkfen * Unlike IPv4 addresses, things can get a
1430e3320f40Smarkfen * little more sticky with scopes, etc.
1431e3320f40Smarkfen */
1432e3320f40Smarkfen int dst_scope, src_scope;
1433e3320f40Smarkfen
1434e3320f40Smarkfen dst_scope = ipv6_addr_scope(&sin6->sin6_addr);
1435e3320f40Smarkfen
1436e3320f40Smarkfen first_match = NULL;
1437e3320f40Smarkfen for (walker = srchp->h_addr_list;
1438e3320f40Smarkfen *walker != NULL; walker++) {
1439e3320f40Smarkfen /* LINTED E_BAD_PTR_CAST_ALIGN */
1440e3320f40Smarkfen if (!IN6_IS_ADDR_V4MAPPED(
1441e3320f40Smarkfen (struct in6_addr *)*walker)) {
1442e3320f40Smarkfen /*
1443e3320f40Smarkfen * Set first-match, etc.
1444e3320f40Smarkfen * Take into account scopes,
1445e3320f40Smarkfen * and other IPv6 thingies.
1446e3320f40Smarkfen */
1447e3320f40Smarkfen src_scope = ipv6_addr_scope(
1448e3320f40Smarkfen /* LINTED E_BAD_PTR_CAST */
1449e3320f40Smarkfen (struct in6_addr *)*walker);
1450e3320f40Smarkfen if (src_scope == SCOPE_UNSPEC ||
1451e3320f40Smarkfen src_scope == dst_scope) {
1452e3320f40Smarkfen if (first_match !=
1453e3320f40Smarkfen NULL)
1454e3320f40Smarkfen break;
1455e3320f40Smarkfen else
1456e3320f40Smarkfen first_match =
1457e3320f40Smarkfen *walker;
1458e3320f40Smarkfen }
1459e3320f40Smarkfen }
1460e3320f40Smarkfen }
1461e3320f40Smarkfen
1462e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(src + 1);
1463e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
1464e3320f40Smarkfen sin6->sin6_port = htons(srcport);
1465e3320f40Smarkfen if (first_match == NULL) {
1466e3320f40Smarkfen /*
1467*f02131e0SVladimir Kotal * No IPv6 hits. Is this the last
1468*f02131e0SVladimir Kotal * destination address in the list ?
1469e3320f40Smarkfen */
1470*f02131e0SVladimir Kotal ERROR1(ep, ebuf, gettext(
1471e3320f40Smarkfen "No IPv6 source address of "
1472e3320f40Smarkfen "matching scope for name %s.\n"),
1473e3320f40Smarkfen srchp->h_name);
1474*f02131e0SVladimir Kotal if (last_dst) {
1475*f02131e0SVladimir Kotal FATAL(ep, ebuf, gettext(
1476*f02131e0SVladimir Kotal "No match for IPV6 "
1477e3320f40Smarkfen "destination "
1478e3320f40Smarkfen "address.\n"));
1479e3320f40Smarkfen } else {
1480e3320f40Smarkfen /* Continue, but do I print? */
1481e3320f40Smarkfen continue; /* for loop */
1482e3320f40Smarkfen }
1483e3320f40Smarkfen
1484e3320f40Smarkfen /* I should never reach here. */
1485e3320f40Smarkfen }
1486e3320f40Smarkfen sin6->sin6_family = AF_INET6;
1487e3320f40Smarkfen if (*walker != NULL) {
1488e3320f40Smarkfen /*
1489e3320f40Smarkfen * Early loop exit. Issue a
1490e3320f40Smarkfen * null-source warning?
1491e3320f40Smarkfen */
1492e3320f40Smarkfen WARN1(ep, ebuf, gettext(
1493e3320f40Smarkfen "Multiple IPv6 source addresses "
1494e3320f40Smarkfen "for %s of the same scope, using "
1495e3320f40Smarkfen "unspecified source instead.\n"),
1496e3320f40Smarkfen srchp->h_name);
1497e3320f40Smarkfen } else {
1498e3320f40Smarkfen /*
1499e3320f40Smarkfen * If I reach here w/o hitting the
1500e3320f40Smarkfen * previous if statements, I have a
1501e3320f40Smarkfen * single source address for this
1502e3320f40Smarkfen * destination.
1503e3320f40Smarkfen */
1504e3320f40Smarkfen bcopy(first_match, &sin6->sin6_addr,
1505e3320f40Smarkfen sizeof (struct in6_addr));
1506e3320f40Smarkfen }
1507e3320f40Smarkfen }
1508e3320f40Smarkfen }
1509e3320f40Smarkfen
1510e3320f40Smarkfen /*
1511e3320f40Smarkfen * If there are errors at this point there is no
1512e3320f40Smarkfen * point sending anything to PF_KEY.
1513e3320f40Smarkfen */
1514e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE);
1515e3320f40Smarkfen
1516e3320f40Smarkfen /* Save off a copy for later writing... */
1517e3320f40Smarkfen msgp = (struct sadb_msg *)buffer;
1518e3320f40Smarkfen bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len));
1519e3320f40Smarkfen
1520e3320f40Smarkfen rc = key_write(keysock, buffer, SADB_64TO8(msgp->sadb_msg_len));
1521e3320f40Smarkfen if (rc == -1)
1522e3320f40Smarkfen Bail("write() to PF_KEY socket (in doaddresses)");
1523e3320f40Smarkfen
15249c2c14abSThejaswini Singarajipura if (in_cluster_mode) {
15259c2c14abSThejaswini Singarajipura (void) sendto(cluster_socket, buffer,
15269c2c14abSThejaswini Singarajipura SADB_64TO8(msgp->sadb_msg_len), 0,
15279c2c14abSThejaswini Singarajipura (struct sockaddr *)&cli_addr,
15289c2c14abSThejaswini Singarajipura sizeof (cli_addr));
15299c2c14abSThejaswini Singarajipura }
1530e3320f40Smarkfen /* Blank the key for paranoia's sake. */
1531e3320f40Smarkfen bzero(buffer, buffer_size);
1532e3320f40Smarkfen time_critical_enter();
1533e3320f40Smarkfen do {
1534e3320f40Smarkfen rc = read(keysock, buffer, buffer_size);
1535e3320f40Smarkfen if (rc == -1)
1536e3320f40Smarkfen Bail("read (in doaddresses)");
1537e3320f40Smarkfen } while (msgp->sadb_msg_seq != seq ||
1538e3320f40Smarkfen msgp->sadb_msg_pid != mypid);
1539e3320f40Smarkfen time_critical_exit();
1540e3320f40Smarkfen
1541e3320f40Smarkfen /*
1542e3320f40Smarkfen * I should _never_ hit the following unless:
1543e3320f40Smarkfen *
1544e3320f40Smarkfen * 1. There is a kernel bug.
1545e3320f40Smarkfen * 2. Another process is mistakenly using my pid in a PF_KEY
1546e3320f40Smarkfen * message.
1547e3320f40Smarkfen */
1548e3320f40Smarkfen if (msgp->sadb_msg_type != sadb_msg_type ||
1549e3320f40Smarkfen msgp->sadb_msg_satype != sadb_msg_satype) {
1550e3320f40Smarkfen syslog((LOG_NOTICE|LOG_AUTH), gettext(
1551e3320f40Smarkfen "doaddresses: Unexpected returned message "
1552e3320f40Smarkfen "(%d exp %d)\n"), msgp->sadb_msg_type,
1553e3320f40Smarkfen sadb_msg_type);
1554e3320f40Smarkfen Bail("doaddresses: Unexpected returned message");
1555e3320f40Smarkfen }
1556e3320f40Smarkfen
1557e3320f40Smarkfen if (msgp->sadb_msg_errno != 0) {
1558e3320f40Smarkfen char addrprint[INET6_ADDRSTRLEN];
1559e3320f40Smarkfen int on_errno = 0;
1560e3320f40Smarkfen char *on_errno_msg;
1561e3320f40Smarkfen
1562e3320f40Smarkfen /*
1563e3320f40Smarkfen * Print different error messages depending
1564e3320f40Smarkfen * on the SADB message type being processed.
1565e3320f40Smarkfen * If we get a ESRCH error for a GET/DELETE
1566e3320f40Smarkfen * messages, we report that the SA does not
1567e3320f40Smarkfen * exist. If we get a EEXIST error for a
1568e3320f40Smarkfen * ADD/UPDATE message, we report that the
1569e3320f40Smarkfen * SA already exists.
1570e3320f40Smarkfen */
1571e3320f40Smarkfen if (sadb_msg_type == SADB_GET ||
1572e3320f40Smarkfen sadb_msg_type == SADB_DELETE) {
1573e3320f40Smarkfen on_errno = ESRCH;
1574e3320f40Smarkfen on_errno_msg = "does not exist";
1575e3320f40Smarkfen } else if (sadb_msg_type == SADB_ADD ||
1576e3320f40Smarkfen sadb_msg_type == SADB_UPDATE) {
1577e3320f40Smarkfen on_errno = EEXIST;
1578e3320f40Smarkfen on_errno_msg = "already exists";
1579e3320f40Smarkfen }
1580e3320f40Smarkfen
1581e3320f40Smarkfen errno = msgp->sadb_msg_errno;
1582e3320f40Smarkfen if (errno == on_errno) {
1583e3320f40Smarkfen ERROR2(ep, ebuf, gettext(
1584e3320f40Smarkfen "Association (type = %s) "
1585e3320f40Smarkfen "with spi 0x%x and addr\n"),
1586e3320f40Smarkfen rparsesatype(msgp->sadb_msg_satype),
1587e3320f40Smarkfen ntohl(spi));
1588e3320f40Smarkfen ERROR2(ep, ebuf, "%s %s.\n",
1589e3320f40Smarkfen do_inet_ntop(dsthp->h_addr_list[i],
1590e3320f40Smarkfen addrprint, sizeof (addrprint)),
1591e3320f40Smarkfen on_errno_msg);
1592e3320f40Smarkfen msgp = (struct sadb_msg *)savebuf;
1593e3320f40Smarkfen bcopy(savebuf, buffer,
1594e3320f40Smarkfen SADB_64TO8(msgp->sadb_msg_len));
1595e3320f40Smarkfen continue;
1596e3320f40Smarkfen } else {
159738d95a78Smarkfen if (errno == EINVAL || errno == ESRCH) {
1598e3320f40Smarkfen ERROR2(ep, ebuf, gettext(
1599e3320f40Smarkfen "PF_KEY Diagnostic code %u: %s.\n"),
1600e3320f40Smarkfen msgp->sadb_x_msg_diagnostic,
1601e3320f40Smarkfen keysock_diag(
1602e3320f40Smarkfen msgp->sadb_x_msg_diagnostic));
1603e3320f40Smarkfen } else {
1604e3320f40Smarkfen Bail("return message (in doaddresses)");
1605e3320f40Smarkfen }
1606e3320f40Smarkfen }
1607e3320f40Smarkfen }
1608e3320f40Smarkfen
1609e3320f40Smarkfen if (cmd == CMD_GET) {
1610e3320f40Smarkfen if (msgp->sadb_msg_len > MAX_GET_SIZE) {
1611e3320f40Smarkfen WARN1(ep, ebuf, gettext("WARNING: "
1612e3320f40Smarkfen "SA information bigger than %d bytes.\n"),
1613e3320f40Smarkfen SADB_64TO8(MAX_GET_SIZE));
1614e3320f40Smarkfen }
1615bb3ed8dfSpwernau print_samsg(stdout, buffer, B_FALSE, vflag, nflag);
1616e3320f40Smarkfen }
1617e3320f40Smarkfen
1618e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE);
1619e3320f40Smarkfen
1620e3320f40Smarkfen /* ...and then restore the saved buffer. */
1621e3320f40Smarkfen msgp = (struct sadb_msg *)savebuf;
1622e3320f40Smarkfen bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len));
1623e3320f40Smarkfen lines_added++;
1624e3320f40Smarkfen }
1625e3320f40Smarkfen
1626e3320f40Smarkfen /* Degenerate case, h_addr_list[0] == NULL. */
1627e3320f40Smarkfen if (i == 0)
1628e3320f40Smarkfen Bail("Empty destination address list");
1629e3320f40Smarkfen
1630e3320f40Smarkfen /*
1631e3320f40Smarkfen * free(ebuf) even if there are no errors.
1632e3320f40Smarkfen * handle_errors() won't return here.
1633e3320f40Smarkfen */
1634e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_TRUE);
1635e3320f40Smarkfen }
1636e3320f40Smarkfen
1637e3320f40Smarkfen /*
1638e3320f40Smarkfen * Perform an add or an update. ADD and UPDATE are similar in the extensions
1639e3320f40Smarkfen * they need.
1640e3320f40Smarkfen */
1641e3320f40Smarkfen static void
doaddup(int cmd,int satype,char * argv[],char * ebuf)1642e3320f40Smarkfen doaddup(int cmd, int satype, char *argv[], char *ebuf)
1643e3320f40Smarkfen {
1644e3320f40Smarkfen uint64_t *buffer, *nexthdr;
1645e3320f40Smarkfen struct sadb_msg msg;
1646e3320f40Smarkfen struct sadb_sa *assoc = NULL;
164738d95a78Smarkfen struct sadb_x_pair *sadb_pair = NULL;
1648e3320f40Smarkfen struct sadb_address *src = NULL, *dst = NULL;
1649e3320f40Smarkfen struct sadb_address *isrc = NULL, *idst = NULL;
1650e3320f40Smarkfen struct sadb_address *natt_local = NULL, *natt_remote = NULL;
1651e3320f40Smarkfen struct sadb_key *encrypt = NULL, *auth = NULL;
1652e3320f40Smarkfen struct sadb_ident *srcid = NULL, *dstid = NULL;
1653e3320f40Smarkfen struct sadb_lifetime *hard = NULL, *soft = NULL; /* Current? */
16549c2c14abSThejaswini Singarajipura struct sadb_lifetime *idle = NULL;
16559c2c14abSThejaswini Singarajipura struct sadb_x_replay_ctr *replay_ctr = NULL;
16565d3b8cb7SBill Sommerfeld struct sadb_sens *label = NULL, *olabel = NULL;
1657e3320f40Smarkfen struct sockaddr_in6 *sin6;
1658e3320f40Smarkfen /* MLS TODO: Need sensitivity eventually. */
1659e3320f40Smarkfen int next, token, sa_len, alloclen, totallen = sizeof (msg), prefix;
1660a050d7e9Spwernau uint32_t spi = 0;
1661628b0c67SMark Fenwick uint_t reserved_bits = 0;
166238d95a78Smarkfen uint8_t sadb_msg_type;
1663e3320f40Smarkfen char *thiscmd, *pstr;
1664e3320f40Smarkfen boolean_t readstate = B_FALSE, unspec_src = B_FALSE;
1665e3320f40Smarkfen boolean_t alloc_inner = B_FALSE, use_natt = B_FALSE;
1666e3320f40Smarkfen struct hostent *srchp = NULL, *dsthp = NULL, *isrchp = NULL,
1667e3320f40Smarkfen *idsthp = NULL;
1668e3320f40Smarkfen struct hostent *natt_lhp = NULL, *natt_rhp = NULL;
1669e3320f40Smarkfen uint16_t srcport = 0, dstport = 0, natt_lport = 0, natt_rport = 0,
1670e3320f40Smarkfen isrcport = 0, idstport = 0;
1671e3320f40Smarkfen uint8_t proto = 0, iproto = 0;
1672e3320f40Smarkfen char *ep = NULL;
1673e3320f40Smarkfen
167438d95a78Smarkfen switch (cmd) {
167538d95a78Smarkfen case CMD_ADD:
167638d95a78Smarkfen thiscmd = "add";
167738d95a78Smarkfen sadb_msg_type = SADB_ADD;
167838d95a78Smarkfen break;
167938d95a78Smarkfen case CMD_UPDATE:
168038d95a78Smarkfen thiscmd = "update";
168138d95a78Smarkfen sadb_msg_type = SADB_UPDATE;
168238d95a78Smarkfen break;
168338d95a78Smarkfen case CMD_UPDATE_PAIR:
168438d95a78Smarkfen thiscmd = "update-pair";
168538d95a78Smarkfen sadb_msg_type = SADB_X_UPDATEPAIR;
168638d95a78Smarkfen break;
168738d95a78Smarkfen }
1688e3320f40Smarkfen
168938d95a78Smarkfen msg_init(&msg, sadb_msg_type, (uint8_t)satype);
1690e3320f40Smarkfen /* Assume last element in argv is set to NULL. */
1691e3320f40Smarkfen do {
1692e3320f40Smarkfen token = parseextval(*argv, &next);
1693e3320f40Smarkfen argv++;
1694e3320f40Smarkfen switch (token) {
1695e3320f40Smarkfen case TOK_EOF:
1696e3320f40Smarkfen /* Do nothing, I'm done. */
1697e3320f40Smarkfen break;
1698e3320f40Smarkfen case TOK_UNKNOWN:
1699e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
1700e3320f40Smarkfen "Unknown extension field \"%s\" \n"), *(argv - 1));
1701e3320f40Smarkfen break;
1702e3320f40Smarkfen case TOK_SPI:
170338d95a78Smarkfen case TOK_PAIR_SPI:
1704e3320f40Smarkfen case TOK_REPLAY:
1705e3320f40Smarkfen case TOK_STATE:
1706e3320f40Smarkfen case TOK_AUTHALG:
1707e3320f40Smarkfen case TOK_ENCRALG:
1708e3320f40Smarkfen case TOK_ENCAP:
1709e3320f40Smarkfen /*
1710e3320f40Smarkfen * May want to place this chunk of code in a function.
1711e3320f40Smarkfen *
1712e3320f40Smarkfen * This code checks for duplicate entries on a command
1713e3320f40Smarkfen * line.
1714e3320f40Smarkfen */
1715e3320f40Smarkfen
1716e3320f40Smarkfen /* Allocate the SADB_EXT_SA extension. */
1717e3320f40Smarkfen if (assoc == NULL) {
1718e3320f40Smarkfen assoc = malloc(sizeof (*assoc));
1719e3320f40Smarkfen if (assoc == NULL)
1720e3320f40Smarkfen Bail("malloc(assoc)");
1721e3320f40Smarkfen bzero(assoc, sizeof (*assoc));
1722e3320f40Smarkfen assoc->sadb_sa_exttype = SADB_EXT_SA;
1723e3320f40Smarkfen assoc->sadb_sa_len =
1724e3320f40Smarkfen SADB_8TO64(sizeof (*assoc));
1725e3320f40Smarkfen totallen += sizeof (*assoc);
1726e3320f40Smarkfen }
1727e3320f40Smarkfen switch (token) {
1728e3320f40Smarkfen case TOK_SPI:
1729e3320f40Smarkfen /*
1730e3320f40Smarkfen * If some cretin types in "spi 0" then he/she
1731e3320f40Smarkfen * can type in another SPI.
1732e3320f40Smarkfen */
1733e3320f40Smarkfen if (assoc->sadb_sa_spi != 0) {
1734e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1735e3320f40Smarkfen "Can only specify "
1736e3320f40Smarkfen "single SPI value.\n"));
1737e3320f40Smarkfen break;
1738e3320f40Smarkfen }
1739e3320f40Smarkfen /* Must convert SPI to network order! */
1740e3320f40Smarkfen assoc->sadb_sa_spi =
1741e3320f40Smarkfen htonl((uint32_t)parsenum(*argv, B_TRUE,
1742e3320f40Smarkfen ebuf));
1743e3320f40Smarkfen if (assoc->sadb_sa_spi == 0) {
1744e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1745e3320f40Smarkfen "Invalid SPI value \"0\" .\n"));
1746e3320f40Smarkfen }
1747e3320f40Smarkfen break;
174838d95a78Smarkfen case TOK_PAIR_SPI:
174938d95a78Smarkfen if (cmd == CMD_UPDATE_PAIR) {
175038d95a78Smarkfen ERROR(ep, ebuf, gettext(
175138d95a78Smarkfen "pair-spi can not be used with the "
175238d95a78Smarkfen "\"update-pair\" command.\n"));
175338d95a78Smarkfen }
175438d95a78Smarkfen if (sadb_pair == NULL) {
175538d95a78Smarkfen sadb_pair = malloc(sizeof (*sadb_pair));
175638d95a78Smarkfen if (assoc == NULL)
175738d95a78Smarkfen Bail("malloc(assoc)");
175838d95a78Smarkfen bzero(sadb_pair, sizeof (*sadb_pair));
175938d95a78Smarkfen totallen += sizeof (*sadb_pair);
176038d95a78Smarkfen }
176138d95a78Smarkfen if (sadb_pair->sadb_x_pair_spi != 0) {
176238d95a78Smarkfen ERROR(ep, ebuf, gettext(
176338d95a78Smarkfen "Can only specify "
176438d95a78Smarkfen "single pair SPI value.\n"));
176538d95a78Smarkfen break;
176638d95a78Smarkfen }
176738d95a78Smarkfen /* Must convert SPI to network order! */
176838d95a78Smarkfen sadb_pair->sadb_x_pair_len =
176938d95a78Smarkfen SADB_8TO64(sizeof (*sadb_pair));
177038d95a78Smarkfen sadb_pair->sadb_x_pair_exttype =
177138d95a78Smarkfen SADB_X_EXT_PAIR;
177238d95a78Smarkfen sadb_pair->sadb_x_pair_spi =
177338d95a78Smarkfen htonl((uint32_t)parsenum(*argv, B_TRUE,
177438d95a78Smarkfen ebuf));
177538d95a78Smarkfen if (sadb_pair->sadb_x_pair_spi == 0) {
177638d95a78Smarkfen ERROR(ep, ebuf, gettext(
177738d95a78Smarkfen "Invalid SPI value \"0\" .\n"));
177838d95a78Smarkfen }
177938d95a78Smarkfen assoc->sadb_sa_flags |=
178038d95a78Smarkfen SADB_X_SAFLAGS_PAIRED;
178138d95a78Smarkfen break;
1782e3320f40Smarkfen case TOK_REPLAY:
1783e3320f40Smarkfen /*
1784e3320f40Smarkfen * That same cretin can do the same with
1785e3320f40Smarkfen * replay.
1786e3320f40Smarkfen */
1787e3320f40Smarkfen if (assoc->sadb_sa_replay != 0) {
1788e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1789e3320f40Smarkfen "Can only specify "
1790e3320f40Smarkfen "single replay window size.\n"));
1791e3320f40Smarkfen break;
1792e3320f40Smarkfen }
1793e3320f40Smarkfen assoc->sadb_sa_replay =
1794e3320f40Smarkfen (uint8_t)parsenum(*argv, B_TRUE, ebuf);
1795e3320f40Smarkfen if (assoc->sadb_sa_replay != 0) {
1796e3320f40Smarkfen WARN(ep, ebuf, gettext(
1797e3320f40Smarkfen "WARNING: Replay with manual"
1798e3320f40Smarkfen " keying considered harmful.\n"));
1799e3320f40Smarkfen }
1800e3320f40Smarkfen break;
1801e3320f40Smarkfen case TOK_STATE:
1802e3320f40Smarkfen /*
1803e3320f40Smarkfen * 0 is an actual state value, LARVAL. This
1804e3320f40Smarkfen * means that one can type in the larval state
1805e3320f40Smarkfen * and then type in another state on the same
1806e3320f40Smarkfen * command line.
1807e3320f40Smarkfen */
1808e3320f40Smarkfen if (assoc->sadb_sa_state != 0) {
1809e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1810e3320f40Smarkfen "Can only specify "
1811e3320f40Smarkfen "single SA state.\n"));
1812e3320f40Smarkfen break;
1813e3320f40Smarkfen }
1814e3320f40Smarkfen assoc->sadb_sa_state = parsestate(*argv,
1815e3320f40Smarkfen ebuf);
1816e3320f40Smarkfen readstate = B_TRUE;
1817e3320f40Smarkfen break;
1818e3320f40Smarkfen case TOK_AUTHALG:
1819e3320f40Smarkfen if (assoc->sadb_sa_auth != 0) {
1820e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1821e3320f40Smarkfen "Can only specify "
1822e3320f40Smarkfen "single auth algorithm.\n"));
1823e3320f40Smarkfen break;
1824e3320f40Smarkfen }
1825e3320f40Smarkfen assoc->sadb_sa_auth = parsealg(*argv,
1826e3320f40Smarkfen IPSEC_PROTO_AH, ebuf);
1827e3320f40Smarkfen break;
1828e3320f40Smarkfen case TOK_ENCRALG:
1829e3320f40Smarkfen if (satype == SADB_SATYPE_AH) {
1830e3320f40Smarkfen ERROR(ep, ebuf, gettext("Cannot specify"
1831e3320f40Smarkfen " encryption with SA type ah.\n"));
1832e3320f40Smarkfen break;
1833e3320f40Smarkfen }
1834e3320f40Smarkfen if (assoc->sadb_sa_encrypt != 0) {
1835e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1836e3320f40Smarkfen "Can only specify "
1837e3320f40Smarkfen "single encryption algorithm.\n"));
1838e3320f40Smarkfen break;
1839e3320f40Smarkfen }
1840e3320f40Smarkfen assoc->sadb_sa_encrypt = parsealg(*argv,
1841e3320f40Smarkfen IPSEC_PROTO_ESP, ebuf);
1842e3320f40Smarkfen break;
1843e3320f40Smarkfen case TOK_ENCAP:
1844e3320f40Smarkfen if (use_natt) {
1845e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1846e3320f40Smarkfen "Can only specify single"
1847e3320f40Smarkfen " encapsulation.\n"));
1848e3320f40Smarkfen break;
1849e3320f40Smarkfen }
1850e3320f40Smarkfen if (strncmp(*argv, "udp", 3)) {
1851e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1852e3320f40Smarkfen "Can only specify udp"
1853e3320f40Smarkfen " encapsulation.\n"));
1854e3320f40Smarkfen break;
1855e3320f40Smarkfen }
1856e3320f40Smarkfen use_natt = B_TRUE;
1857e3320f40Smarkfen /* set assoc flags later */
1858e3320f40Smarkfen break;
1859e3320f40Smarkfen }
1860e3320f40Smarkfen argv++;
1861e3320f40Smarkfen break;
1862e3320f40Smarkfen case TOK_SRCPORT:
1863e3320f40Smarkfen if (srcport != 0) {
1864e3320f40Smarkfen ERROR(ep, ebuf, gettext("Can only specify "
1865e3320f40Smarkfen "single source port.\n"));
1866e3320f40Smarkfen break;
1867e3320f40Smarkfen }
1868e3320f40Smarkfen srcport = parsenum(*argv, B_TRUE, ebuf);
1869e3320f40Smarkfen argv++;
1870e3320f40Smarkfen break;
1871e3320f40Smarkfen case TOK_DSTPORT:
1872e3320f40Smarkfen if (dstport != 0) {
1873e3320f40Smarkfen ERROR(ep, ebuf, gettext("Can only specify "
1874e3320f40Smarkfen "single destination port.\n"));
1875e3320f40Smarkfen break;
1876e3320f40Smarkfen }
1877e3320f40Smarkfen dstport = parsenum(*argv, B_TRUE, ebuf);
1878e3320f40Smarkfen argv++;
1879e3320f40Smarkfen break;
1880e3320f40Smarkfen case TOK_ISRCPORT:
1881e3320f40Smarkfen alloc_inner = B_TRUE;
1882e3320f40Smarkfen if (isrcport != 0) {
1883e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1884e3320f40Smarkfen "Can only specify "
1885e3320f40Smarkfen "single inner-source port.\n"));
1886e3320f40Smarkfen break;
1887e3320f40Smarkfen }
1888e3320f40Smarkfen isrcport = parsenum(*argv, B_TRUE, ebuf);
1889e3320f40Smarkfen argv++;
1890e3320f40Smarkfen break;
1891e3320f40Smarkfen case TOK_IDSTPORT:
1892e3320f40Smarkfen alloc_inner = B_TRUE;
1893e3320f40Smarkfen if (idstport != 0) {
1894e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1895e3320f40Smarkfen "Can only specify "
1896e3320f40Smarkfen "single inner-destination port.\n"));
1897e3320f40Smarkfen break;
1898e3320f40Smarkfen }
1899e3320f40Smarkfen idstport = parsenum(*argv, B_TRUE, ebuf);
1900e3320f40Smarkfen argv++;
1901e3320f40Smarkfen break;
1902e3320f40Smarkfen case TOK_NATLPORT:
1903e3320f40Smarkfen if (natt_lport != 0) {
1904e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1905e3320f40Smarkfen "Can only specify "
1906e3320f40Smarkfen "single NAT-T local port.\n"));
1907e3320f40Smarkfen break;
1908e3320f40Smarkfen }
1909e3320f40Smarkfen natt_lport = parsenum(*argv, B_TRUE, ebuf);
1910e3320f40Smarkfen argv++;
1911e3320f40Smarkfen break;
1912e3320f40Smarkfen case TOK_NATRPORT:
1913e3320f40Smarkfen if (natt_rport != 0) {
1914e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1915e3320f40Smarkfen "Can only specify "
1916e3320f40Smarkfen "single NAT-T remote port.\n"));
1917e3320f40Smarkfen break;
1918e3320f40Smarkfen }
1919e3320f40Smarkfen natt_rport = parsenum(*argv, B_TRUE, ebuf);
1920e3320f40Smarkfen argv++;
1921e3320f40Smarkfen break;
1922e3320f40Smarkfen
1923e3320f40Smarkfen case TOK_PROTO:
1924e3320f40Smarkfen if (proto != 0) {
1925e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1926e3320f40Smarkfen "Can only specify "
1927e3320f40Smarkfen "single protocol.\n"));
1928e3320f40Smarkfen break;
1929e3320f40Smarkfen }
1930e3320f40Smarkfen proto = parsenum(*argv, B_TRUE, ebuf);
1931e3320f40Smarkfen argv++;
1932e3320f40Smarkfen break;
1933e3320f40Smarkfen case TOK_IPROTO:
1934e3320f40Smarkfen alloc_inner = B_TRUE;
1935e3320f40Smarkfen if (iproto != 0) {
1936e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1937e3320f40Smarkfen "Can only specify "
1938e3320f40Smarkfen "single inner protocol.\n"));
1939e3320f40Smarkfen break;
1940e3320f40Smarkfen }
1941e3320f40Smarkfen iproto = parsenum(*argv, B_TRUE, ebuf);
1942e3320f40Smarkfen argv++;
1943e3320f40Smarkfen break;
1944e3320f40Smarkfen case TOK_SRCADDR:
1945e3320f40Smarkfen case TOK_SRCADDR6:
1946e3320f40Smarkfen if (src != NULL) {
1947e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1948e3320f40Smarkfen "Can only specify "
1949e3320f40Smarkfen "single source address.\n"));
1950e3320f40Smarkfen break;
1951e3320f40Smarkfen }
1952e3320f40Smarkfen sa_len = parseaddr(*argv, &srchp,
1953e3320f40Smarkfen (token == TOK_SRCADDR6), ebuf);
1954e3320f40Smarkfen if (srchp == NULL) {
1955e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
1956e3320f40Smarkfen "Unknown src address \"%s\"\n"), *argv);
1957e3320f40Smarkfen break;
1958e3320f40Smarkfen }
1959e3320f40Smarkfen argv++;
1960e3320f40Smarkfen /*
1961e3320f40Smarkfen * Round of the sockaddr length to an 8 byte
1962e3320f40Smarkfen * boundary to make PF_KEY happy.
1963e3320f40Smarkfen */
1964e3320f40Smarkfen alloclen = sizeof (*src) + roundup(sa_len, 8);
1965e3320f40Smarkfen src = malloc(alloclen);
1966e3320f40Smarkfen if (src == NULL)
1967e3320f40Smarkfen Bail("malloc(src)");
1968e3320f40Smarkfen totallen += alloclen;
1969e3320f40Smarkfen src->sadb_address_len = SADB_8TO64(alloclen);
1970e3320f40Smarkfen src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1971e3320f40Smarkfen src->sadb_address_reserved = 0;
1972e3320f40Smarkfen src->sadb_address_prefixlen = 0;
1973e3320f40Smarkfen src->sadb_address_proto = 0;
1974e3320f40Smarkfen if (srchp == &dummy.he) {
1975e3320f40Smarkfen /*
1976e3320f40Smarkfen * Single address with -n flag.
1977e3320f40Smarkfen */
1978e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(src + 1);
1979e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
1980e3320f40Smarkfen sin6->sin6_family = AF_INET6;
1981e3320f40Smarkfen bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
1982e3320f40Smarkfen sizeof (struct in6_addr));
1983e3320f40Smarkfen }
1984e3320f40Smarkfen break;
1985e3320f40Smarkfen case TOK_DSTADDR:
1986e3320f40Smarkfen case TOK_DSTADDR6:
1987e3320f40Smarkfen if (dst != NULL) {
1988e3320f40Smarkfen ERROR(ep, ebuf, gettext(
1989e3320f40Smarkfen "Can only specify single "
1990e3320f40Smarkfen "destination address.\n"));
1991e3320f40Smarkfen break;
1992e3320f40Smarkfen }
1993e3320f40Smarkfen sa_len = parseaddr(*argv, &dsthp,
1994e3320f40Smarkfen (token == TOK_DSTADDR6), ebuf);
1995e3320f40Smarkfen if (dsthp == NULL) {
1996e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
1997e3320f40Smarkfen "Unknown dst address \"%s\"\n"), *argv);
1998e3320f40Smarkfen break;
1999e3320f40Smarkfen }
2000e3320f40Smarkfen argv++;
2001e3320f40Smarkfen alloclen = sizeof (*dst) + roundup(sa_len, 8);
2002e3320f40Smarkfen dst = malloc(alloclen);
2003e3320f40Smarkfen if (dst == NULL)
2004e3320f40Smarkfen Bail("malloc(dst)");
2005e3320f40Smarkfen totallen += alloclen;
2006e3320f40Smarkfen dst->sadb_address_len = SADB_8TO64(alloclen);
2007e3320f40Smarkfen dst->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2008e3320f40Smarkfen dst->sadb_address_reserved = 0;
2009e3320f40Smarkfen dst->sadb_address_prefixlen = 0;
2010e3320f40Smarkfen dst->sadb_address_proto = 0;
2011e3320f40Smarkfen if (dsthp == &dummy.he) {
2012e3320f40Smarkfen /*
2013e3320f40Smarkfen * Single address with -n flag.
2014e3320f40Smarkfen */
2015e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(dst + 1);
2016e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
2017e3320f40Smarkfen sin6->sin6_family = AF_INET6;
2018e3320f40Smarkfen bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
2019e3320f40Smarkfen sizeof (struct in6_addr));
2020e3320f40Smarkfen }
2021e3320f40Smarkfen break;
2022e3320f40Smarkfen case TOK_PROXYADDR:
2023e3320f40Smarkfen case TOK_PROXYADDR6:
2024e3320f40Smarkfen if (isrc != NULL) {
2025e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2026e3320f40Smarkfen "Can only specify single "
2027e3320f40Smarkfen "proxy/inner-source address.\n"));
2028e3320f40Smarkfen break;
2029e3320f40Smarkfen }
2030e3320f40Smarkfen if ((pstr = strchr(*argv, '/')) != NULL) {
2031e3320f40Smarkfen /* Parse out the prefix. */
2032e3320f40Smarkfen errno = 0;
2033e3320f40Smarkfen prefix = strtol(pstr + 1, NULL, 10);
2034e3320f40Smarkfen if (errno != 0) {
2035e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2036e3320f40Smarkfen "Invalid prefix %s."), pstr);
2037e3320f40Smarkfen break;
2038e3320f40Smarkfen }
2039e3320f40Smarkfen /* Recycle pstr */
2040e3320f40Smarkfen alloclen = (int)(pstr - *argv);
2041e3320f40Smarkfen pstr = malloc(alloclen + 1);
2042e3320f40Smarkfen if (pstr == NULL) {
2043e3320f40Smarkfen Bail("malloc(pstr)");
2044e3320f40Smarkfen }
2045e3320f40Smarkfen (void) strlcpy(pstr, *argv, alloclen + 1);
2046e3320f40Smarkfen } else {
2047e3320f40Smarkfen pstr = *argv;
2048e3320f40Smarkfen /*
2049e3320f40Smarkfen * Assume mapping to AF_INET6, and we're a host.
2050e3320f40Smarkfen * XXX some miscreants may still make classful
2051e3320f40Smarkfen * assumptions. If this is a problem, fix it
2052e3320f40Smarkfen * here.
2053e3320f40Smarkfen */
2054e3320f40Smarkfen prefix = 128;
2055e3320f40Smarkfen }
2056e3320f40Smarkfen sa_len = parseaddr(pstr, &isrchp,
2057e3320f40Smarkfen (token == TOK_PROXYADDR6), ebuf);
2058e3320f40Smarkfen if (isrchp == NULL) {
2059e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2060e3320f40Smarkfen "Unknown proxy/inner-source address "
2061e3320f40Smarkfen "\"%s\"\n"), *argv);
2062e3320f40Smarkfen break;
2063e3320f40Smarkfen }
2064e3320f40Smarkfen if (pstr != *argv)
2065e3320f40Smarkfen free(pstr);
2066e3320f40Smarkfen argv++;
2067e3320f40Smarkfen alloclen = sizeof (*isrc) + roundup(sa_len, 8);
2068e3320f40Smarkfen isrc = malloc(alloclen);
2069e3320f40Smarkfen if (isrc == NULL)
2070e3320f40Smarkfen Bail("malloc(isrc)");
2071e3320f40Smarkfen totallen += alloclen;
2072e3320f40Smarkfen isrc->sadb_address_len = SADB_8TO64(alloclen);
2073e3320f40Smarkfen isrc->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
2074e3320f40Smarkfen isrc->sadb_address_reserved = 0;
2075e3320f40Smarkfen isrc->sadb_address_prefixlen = prefix;
2076e3320f40Smarkfen isrc->sadb_address_proto = 0;
2077e3320f40Smarkfen if (isrchp == &dummy.he ||
2078e3320f40Smarkfen isrchp->h_addr_list[1] == NULL) {
2079e3320f40Smarkfen /*
2080e3320f40Smarkfen * Single address with -n flag or single name.
2081e3320f40Smarkfen */
2082e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(isrc + 1);
2083e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
2084e3320f40Smarkfen sin6->sin6_family = AF_INET6;
2085e3320f40Smarkfen bcopy(isrchp->h_addr_list[0], &sin6->sin6_addr,
2086e3320f40Smarkfen sizeof (struct in6_addr));
2087e3320f40Smarkfen /*
2088e3320f40Smarkfen * normalize prefixlen for IPv4-mapped
2089e3320f40Smarkfen * addresses.
2090e3320f40Smarkfen */
2091e3320f40Smarkfen if (prefix <= 32 &&
2092e3320f40Smarkfen IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
2093e3320f40Smarkfen isrc->sadb_address_prefixlen += 96;
2094e3320f40Smarkfen alloc_inner = B_TRUE;
2095e3320f40Smarkfen } else {
2096e3320f40Smarkfen /*
2097e3320f40Smarkfen * If the proxy/isrc address is vague, don't
2098e3320f40Smarkfen * bother.
2099e3320f40Smarkfen */
2100e3320f40Smarkfen totallen -= alloclen;
2101e3320f40Smarkfen free(isrc);
2102e3320f40Smarkfen isrc = NULL;
2103e3320f40Smarkfen WARN1(ep, ebuf, gettext(
2104e3320f40Smarkfen "Proxy/inner-source address %s "
2105e3320f40Smarkfen "is vague, not using.\n"), isrchp->h_name);
2106e3320f40Smarkfen freehostent(isrchp);
2107e3320f40Smarkfen isrchp = NULL;
2108e3320f40Smarkfen break;
2109e3320f40Smarkfen }
2110e3320f40Smarkfen break;
2111e3320f40Smarkfen case TOK_IDSTADDR:
2112e3320f40Smarkfen case TOK_IDSTADDR6:
2113e3320f40Smarkfen if (idst != NULL) {
2114e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2115e3320f40Smarkfen "Can only specify single "
2116e3320f40Smarkfen "inner-destination address.\n"));
2117e3320f40Smarkfen break;
2118e3320f40Smarkfen }
2119e3320f40Smarkfen if ((pstr = strchr(*argv, '/')) != NULL) {
2120e3320f40Smarkfen /* Parse out the prefix. */
2121e3320f40Smarkfen errno = 0;
2122e3320f40Smarkfen prefix = strtol(pstr + 1, NULL, 10);
2123e3320f40Smarkfen if (errno != 0) {
2124e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2125e3320f40Smarkfen "Invalid prefix %s.\n"), pstr);
2126e3320f40Smarkfen break;
2127e3320f40Smarkfen }
2128e3320f40Smarkfen /* Recycle pstr */
2129e3320f40Smarkfen alloclen = (int)(pstr - *argv);
2130e3320f40Smarkfen pstr = malloc(alloclen + 1);
2131e3320f40Smarkfen if (pstr == NULL) {
2132e3320f40Smarkfen Bail("malloc(pstr)");
2133e3320f40Smarkfen }
2134e3320f40Smarkfen (void) strlcpy(pstr, *argv, alloclen + 1);
2135e3320f40Smarkfen } else {
2136e3320f40Smarkfen pstr = *argv;
2137e3320f40Smarkfen /*
2138e3320f40Smarkfen * Assume mapping to AF_INET6, and we're a host.
2139e3320f40Smarkfen * XXX some miscreants may still make classful
2140e3320f40Smarkfen * assumptions. If this is a problem, fix it
2141e3320f40Smarkfen * here.
2142e3320f40Smarkfen */
2143e3320f40Smarkfen prefix = 128;
2144e3320f40Smarkfen }
2145e3320f40Smarkfen sa_len = parseaddr(pstr, &idsthp,
2146e3320f40Smarkfen (token == TOK_IDSTADDR6), ebuf);
2147e3320f40Smarkfen if (idsthp == NULL) {
2148e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2149e3320f40Smarkfen "Unknown Inner Src address "
2150e3320f40Smarkfen " \"%s\"\n"), *argv);
2151e3320f40Smarkfen break;
2152e3320f40Smarkfen }
2153e3320f40Smarkfen if (pstr != *argv)
2154e3320f40Smarkfen free(pstr);
2155e3320f40Smarkfen argv++;
2156e3320f40Smarkfen alloclen = sizeof (*idst) + roundup(sa_len, 8);
2157e3320f40Smarkfen idst = malloc(alloclen);
2158e3320f40Smarkfen if (idst == NULL)
2159e3320f40Smarkfen Bail("malloc(idst)");
2160e3320f40Smarkfen totallen += alloclen;
2161e3320f40Smarkfen idst->sadb_address_len = SADB_8TO64(alloclen);
2162e3320f40Smarkfen idst->sadb_address_exttype =
2163e3320f40Smarkfen SADB_X_EXT_ADDRESS_INNER_DST;
2164e3320f40Smarkfen idst->sadb_address_reserved = 0;
2165e3320f40Smarkfen idst->sadb_address_prefixlen = prefix;
2166e3320f40Smarkfen idst->sadb_address_proto = 0;
2167e3320f40Smarkfen if (idsthp == &dummy.he ||
2168e3320f40Smarkfen idsthp->h_addr_list[1] == NULL) {
2169e3320f40Smarkfen /*
2170e3320f40Smarkfen * Single address with -n flag or single name.
2171e3320f40Smarkfen */
2172e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(idst + 1);
2173e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
2174e3320f40Smarkfen sin6->sin6_family = AF_INET6;
2175e3320f40Smarkfen bcopy(idsthp->h_addr_list[0], &sin6->sin6_addr,
2176e3320f40Smarkfen sizeof (struct in6_addr));
2177e3320f40Smarkfen /*
2178e3320f40Smarkfen * normalize prefixlen for IPv4-mapped
2179e3320f40Smarkfen * addresses.
2180e3320f40Smarkfen */
2181e3320f40Smarkfen if (prefix <= 32 &&
2182e3320f40Smarkfen IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
2183e3320f40Smarkfen idst->sadb_address_prefixlen += 96;
2184e3320f40Smarkfen alloc_inner = B_TRUE;
2185e3320f40Smarkfen } else {
2186e3320f40Smarkfen /*
2187e3320f40Smarkfen * If the idst address is vague, don't bother.
2188e3320f40Smarkfen */
2189e3320f40Smarkfen totallen -= alloclen;
2190e3320f40Smarkfen free(idst);
2191e3320f40Smarkfen idst = NULL;
2192e3320f40Smarkfen WARN1(ep, ebuf, gettext(
2193e3320f40Smarkfen "Inner destination address %s "
2194e3320f40Smarkfen "is vague, not using.\n"), idsthp->h_name);
2195e3320f40Smarkfen freehostent(idsthp);
2196e3320f40Smarkfen idsthp = NULL;
2197e3320f40Smarkfen break;
2198e3320f40Smarkfen }
2199e3320f40Smarkfen break;
2200e3320f40Smarkfen case TOK_NATLOC:
2201e3320f40Smarkfen if (natt_local != NULL) {
2202e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2203e3320f40Smarkfen "Can only specify "
2204e3320f40Smarkfen "single NAT-T local address.\n"));
2205e3320f40Smarkfen break;
2206e3320f40Smarkfen }
2207e3320f40Smarkfen sa_len = parseaddr(*argv, &natt_lhp, 0, ebuf);
2208e3320f40Smarkfen if (natt_lhp == NULL) {
2209e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2210e3320f40Smarkfen "Unknown NAT-T local address \"%s\"\n"),
2211e3320f40Smarkfen *argv);
2212e3320f40Smarkfen break;
2213e3320f40Smarkfen }
2214e3320f40Smarkfen argv++;
2215e3320f40Smarkfen /*
2216e3320f40Smarkfen * Round of the sockaddr length to an 8 byte
2217e3320f40Smarkfen * boundary to make PF_KEY happy.
2218e3320f40Smarkfen */
2219e3320f40Smarkfen alloclen = sizeof (*natt_local) + roundup(sa_len, 8);
2220e3320f40Smarkfen natt_local = malloc(alloclen);
2221e3320f40Smarkfen if (natt_local == NULL)
2222e3320f40Smarkfen Bail("malloc(natt_local)");
2223e3320f40Smarkfen totallen += alloclen;
2224e3320f40Smarkfen natt_local->sadb_address_len = SADB_8TO64(alloclen);
2225e3320f40Smarkfen natt_local->sadb_address_exttype =
2226e3320f40Smarkfen SADB_X_EXT_ADDRESS_NATT_LOC;
2227e3320f40Smarkfen natt_local->sadb_address_reserved = 0;
2228e3320f40Smarkfen natt_local->sadb_address_prefixlen = 0;
2229e3320f40Smarkfen natt_local->sadb_address_proto = 0;
2230e3320f40Smarkfen if (natt_lhp == &dummy.he ||
2231e3320f40Smarkfen natt_lhp->h_addr_list[1] == NULL) {
2232e3320f40Smarkfen /*
2233e3320f40Smarkfen * Single address with -n flag or single name.
2234e3320f40Smarkfen */
2235e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(natt_local + 1);
2236e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
2237e3320f40Smarkfen sin6->sin6_family = AF_INET6;
2238e3320f40Smarkfen bcopy(natt_lhp->h_addr_list[0],
2239e3320f40Smarkfen &sin6->sin6_addr, sizeof (struct in6_addr));
2240e3320f40Smarkfen } else {
2241e3320f40Smarkfen /*
2242e3320f40Smarkfen * If the nat-local address is vague, don't
2243e3320f40Smarkfen * bother.
2244e3320f40Smarkfen */
2245e3320f40Smarkfen totallen -= alloclen;
2246e3320f40Smarkfen free(natt_local);
2247e3320f40Smarkfen natt_local = NULL;
2248e3320f40Smarkfen WARN1(ep, ebuf, gettext(
2249e3320f40Smarkfen "NAT-T local address %s "
2250e3320f40Smarkfen "is vague, not using.\n"),
2251e3320f40Smarkfen natt_lhp->h_name);
2252e3320f40Smarkfen freehostent(natt_lhp);
2253e3320f40Smarkfen natt_lhp = NULL;
2254e3320f40Smarkfen break;
2255e3320f40Smarkfen }
2256e3320f40Smarkfen break;
2257e3320f40Smarkfen case TOK_NATREM:
2258e3320f40Smarkfen if (natt_remote != NULL) {
2259e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2260e3320f40Smarkfen "Can only specify "
2261e3320f40Smarkfen "single NAT-T remote address.\n"));
2262e3320f40Smarkfen break;
2263e3320f40Smarkfen }
2264e3320f40Smarkfen sa_len = parseaddr(*argv, &natt_rhp, 0, ebuf);
2265e3320f40Smarkfen if (natt_rhp == NULL) {
2266e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2267e3320f40Smarkfen "Unknown NAT-T remote address \"%s\"\n"),
2268e3320f40Smarkfen *argv);
2269e3320f40Smarkfen break;
2270e3320f40Smarkfen }
2271e3320f40Smarkfen argv++;
2272e3320f40Smarkfen /*
2273e3320f40Smarkfen * Round of the sockaddr length to an 8 byte
2274e3320f40Smarkfen * boundary to make PF_KEY happy.
2275e3320f40Smarkfen */
2276e3320f40Smarkfen alloclen = sizeof (*natt_remote) + roundup(sa_len, 8);
2277e3320f40Smarkfen natt_remote = malloc(alloclen);
2278e3320f40Smarkfen if (natt_remote == NULL)
2279e3320f40Smarkfen Bail("malloc(natt_remote)");
2280e3320f40Smarkfen totallen += alloclen;
2281e3320f40Smarkfen natt_remote->sadb_address_len = SADB_8TO64(alloclen);
2282e3320f40Smarkfen natt_remote->sadb_address_exttype =
2283e3320f40Smarkfen SADB_X_EXT_ADDRESS_NATT_REM;
2284e3320f40Smarkfen natt_remote->sadb_address_reserved = 0;
2285e3320f40Smarkfen natt_remote->sadb_address_prefixlen = 0;
2286e3320f40Smarkfen natt_remote->sadb_address_proto = 0;
2287e3320f40Smarkfen if (natt_rhp == &dummy.he ||
2288e3320f40Smarkfen natt_rhp->h_addr_list[1] == NULL) {
2289e3320f40Smarkfen /*
2290e3320f40Smarkfen * Single address with -n flag or single name.
2291e3320f40Smarkfen */
2292e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(natt_remote + 1);
2293e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
2294e3320f40Smarkfen sin6->sin6_family = AF_INET6;
2295e3320f40Smarkfen bcopy(natt_rhp->h_addr_list[0],
2296e3320f40Smarkfen &sin6->sin6_addr, sizeof (struct in6_addr));
2297e3320f40Smarkfen } else {
2298e3320f40Smarkfen /*
2299e3320f40Smarkfen * If the nat-renote address is vague, don't
2300e3320f40Smarkfen * bother.
2301e3320f40Smarkfen */
2302e3320f40Smarkfen totallen -= alloclen;
2303e3320f40Smarkfen free(natt_remote);
2304e3320f40Smarkfen natt_remote = NULL;
2305e3320f40Smarkfen WARN1(ep, ebuf, gettext(
2306e3320f40Smarkfen "NAT-T remote address %s "
2307e3320f40Smarkfen "is vague, not using.\n"),
2308e3320f40Smarkfen natt_rhp->h_name);
2309e3320f40Smarkfen freehostent(natt_rhp);
2310e3320f40Smarkfen natt_rhp = NULL;
2311e3320f40Smarkfen break;
2312e3320f40Smarkfen }
2313e3320f40Smarkfen break;
2314e3320f40Smarkfen case TOK_ENCRKEY:
2315e3320f40Smarkfen if (encrypt != NULL) {
2316e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2317e3320f40Smarkfen "Can only specify "
2318e3320f40Smarkfen "single encryption key.\n"));
2319e3320f40Smarkfen break;
2320e3320f40Smarkfen }
2321a050d7e9Spwernau if (assoc != NULL &&
2322a050d7e9Spwernau assoc->sadb_sa_encrypt == SADB_EALG_NULL) {
2323ec485834Spwernau FATAL(ep, ebuf, gettext(
2324ec485834Spwernau "Cannot specify a key with NULL "
2325ec485834Spwernau "encryption algorithm.\n"));
2326ec485834Spwernau break;
2327ec485834Spwernau }
2328628b0c67SMark Fenwick encrypt = parsekey(*argv, ebuf, reserved_bits);
2329e3320f40Smarkfen argv++;
2330e3320f40Smarkfen if (encrypt == NULL) {
2331e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2332e3320f40Smarkfen "Invalid encryption key.\n"));
2333e3320f40Smarkfen break;
2334e3320f40Smarkfen }
2335e3320f40Smarkfen totallen += SADB_64TO8(encrypt->sadb_key_len);
2336e3320f40Smarkfen encrypt->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
2337e3320f40Smarkfen break;
2338e3320f40Smarkfen case TOK_AUTHKEY:
2339e3320f40Smarkfen if (auth != NULL) {
2340e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2341e3320f40Smarkfen "Can only specify single"
2342e3320f40Smarkfen " authentication key.\n"));
2343e3320f40Smarkfen break;
2344e3320f40Smarkfen }
2345628b0c67SMark Fenwick auth = parsekey(*argv, ebuf, 0);
2346e3320f40Smarkfen argv++;
2347e3320f40Smarkfen if (auth == NULL) {
2348e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2349e3320f40Smarkfen "Invalid authentication key.\n"));
2350e3320f40Smarkfen break;
2351e3320f40Smarkfen }
2352e3320f40Smarkfen totallen += SADB_64TO8(auth->sadb_key_len);
2353e3320f40Smarkfen auth->sadb_key_exttype = SADB_EXT_KEY_AUTH;
2354e3320f40Smarkfen break;
2355e3320f40Smarkfen case TOK_SRCIDTYPE:
2356e3320f40Smarkfen if (*argv == NULL || *(argv + 1) == NULL) {
2357e3320f40Smarkfen FATAL(ep, ebuf, gettext(
2358e3320f40Smarkfen "Unexpected end of command "
2359e3320f40Smarkfen "line - Expecting Src Type.\n"));
2360e3320f40Smarkfen /* NOTREACHED */
2361e3320f40Smarkfen break;
2362e3320f40Smarkfen }
2363e3320f40Smarkfen if (srcid != NULL) {
2364e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2365e3320f40Smarkfen "Can only specify single"
2366e3320f40Smarkfen " source certificate identity.\n"));
2367e3320f40Smarkfen break;
2368e3320f40Smarkfen }
2369e3320f40Smarkfen alloclen = sizeof (*srcid) +
2370e3320f40Smarkfen roundup(strlen(*(argv + 1)) + 1, 8);
2371e3320f40Smarkfen srcid = malloc(alloclen);
2372e3320f40Smarkfen if (srcid == NULL)
2373e3320f40Smarkfen Bail("malloc(srcid)");
2374e3320f40Smarkfen totallen += alloclen;
2375e3320f40Smarkfen srcid->sadb_ident_type = parseidtype(*argv, ebuf);
2376e3320f40Smarkfen argv++;
2377e3320f40Smarkfen srcid->sadb_ident_len = SADB_8TO64(alloclen);
2378e3320f40Smarkfen srcid->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC;
2379e3320f40Smarkfen srcid->sadb_ident_reserved = 0;
2380e3320f40Smarkfen srcid->sadb_ident_id = 0; /* Not useful here. */
2381e3320f40Smarkfen (void) strlcpy((char *)(srcid + 1), *argv, alloclen);
2382e3320f40Smarkfen argv++;
2383e3320f40Smarkfen break;
2384e3320f40Smarkfen case TOK_DSTIDTYPE:
2385e3320f40Smarkfen if (*argv == NULL || *(argv + 1) == NULL) {
2386e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2387e3320f40Smarkfen "Unexpected end of command"
2388e3320f40Smarkfen " line - expecting dst type.\n"));
2389e3320f40Smarkfen break;
2390e3320f40Smarkfen }
2391e3320f40Smarkfen if (dstid != NULL) {
2392e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2393e3320f40Smarkfen "Can only specify single destination "
2394e3320f40Smarkfen "certificate identity.\n"));
2395e3320f40Smarkfen break;
2396e3320f40Smarkfen }
2397e3320f40Smarkfen alloclen = sizeof (*dstid) +
2398e3320f40Smarkfen roundup(strlen(*(argv + 1)) + 1, 8);
2399e3320f40Smarkfen dstid = malloc(alloclen);
2400e3320f40Smarkfen if (dstid == NULL)
2401e3320f40Smarkfen Bail("malloc(dstid)");
2402e3320f40Smarkfen totallen += alloclen;
2403e3320f40Smarkfen dstid->sadb_ident_type = parseidtype(*argv, ebuf);
2404e3320f40Smarkfen argv++;
2405e3320f40Smarkfen dstid->sadb_ident_len = SADB_8TO64(alloclen);
2406e3320f40Smarkfen dstid->sadb_ident_exttype = SADB_EXT_IDENTITY_DST;
2407e3320f40Smarkfen dstid->sadb_ident_reserved = 0;
2408e3320f40Smarkfen dstid->sadb_ident_id = 0; /* Not useful here. */
2409e3320f40Smarkfen (void) strlcpy((char *)(dstid + 1), *argv, alloclen);
2410e3320f40Smarkfen argv++;
2411e3320f40Smarkfen break;
2412e3320f40Smarkfen case TOK_HARD_ALLOC:
2413e3320f40Smarkfen case TOK_HARD_BYTES:
2414e3320f40Smarkfen case TOK_HARD_ADDTIME:
2415e3320f40Smarkfen case TOK_HARD_USETIME:
2416e3320f40Smarkfen if (hard == NULL) {
2417e3320f40Smarkfen hard = malloc(sizeof (*hard));
2418e3320f40Smarkfen if (hard == NULL)
2419e3320f40Smarkfen Bail("malloc(hard_lifetime)");
2420e3320f40Smarkfen bzero(hard, sizeof (*hard));
2421e3320f40Smarkfen hard->sadb_lifetime_exttype =
2422e3320f40Smarkfen SADB_EXT_LIFETIME_HARD;
2423e3320f40Smarkfen hard->sadb_lifetime_len =
2424e3320f40Smarkfen SADB_8TO64(sizeof (*hard));
2425e3320f40Smarkfen totallen += sizeof (*hard);
2426e3320f40Smarkfen }
2427e3320f40Smarkfen switch (token) {
2428e3320f40Smarkfen case TOK_HARD_ALLOC:
2429e3320f40Smarkfen if (hard->sadb_lifetime_allocations != 0) {
2430e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2431e3320f40Smarkfen "Can only specify single"
2432e3320f40Smarkfen " hard allocation limit.\n"));
2433e3320f40Smarkfen break;
2434e3320f40Smarkfen }
2435e3320f40Smarkfen hard->sadb_lifetime_allocations =
2436e3320f40Smarkfen (uint32_t)parsenum(*argv, B_TRUE, ebuf);
2437e3320f40Smarkfen break;
2438e3320f40Smarkfen case TOK_HARD_BYTES:
2439e3320f40Smarkfen if (hard->sadb_lifetime_bytes != 0) {
2440e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2441e3320f40Smarkfen "Can only specify "
2442e3320f40Smarkfen "single hard byte limit.\n"));
2443e3320f40Smarkfen break;
2444e3320f40Smarkfen }
2445e3320f40Smarkfen hard->sadb_lifetime_bytes = parsenum(*argv,
2446e3320f40Smarkfen B_TRUE, ebuf);
2447e3320f40Smarkfen break;
2448e3320f40Smarkfen case TOK_HARD_ADDTIME:
2449e3320f40Smarkfen if (hard->sadb_lifetime_addtime != 0) {
2450e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2451e3320f40Smarkfen "Can only specify "
2452e3320f40Smarkfen "single past-add lifetime.\n"));
2453e3320f40Smarkfen break;
2454e3320f40Smarkfen }
2455e3320f40Smarkfen hard->sadb_lifetime_addtime = parsenum(*argv,
2456e3320f40Smarkfen B_TRUE, ebuf);
2457e3320f40Smarkfen break;
2458e3320f40Smarkfen case TOK_HARD_USETIME:
2459e3320f40Smarkfen if (hard->sadb_lifetime_usetime != 0) {
2460e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2461e3320f40Smarkfen "Can only specify "
2462e3320f40Smarkfen "single past-use lifetime.\n"));
2463e3320f40Smarkfen break;
2464e3320f40Smarkfen }
2465e3320f40Smarkfen hard->sadb_lifetime_usetime = parsenum(*argv,
2466e3320f40Smarkfen B_TRUE, ebuf);
2467e3320f40Smarkfen break;
2468e3320f40Smarkfen }
2469e3320f40Smarkfen argv++;
2470e3320f40Smarkfen break;
2471e3320f40Smarkfen case TOK_SOFT_ALLOC:
2472e3320f40Smarkfen case TOK_SOFT_BYTES:
2473e3320f40Smarkfen case TOK_SOFT_ADDTIME:
2474e3320f40Smarkfen case TOK_SOFT_USETIME:
2475e3320f40Smarkfen if (soft == NULL) {
2476e3320f40Smarkfen soft = malloc(sizeof (*soft));
2477e3320f40Smarkfen if (soft == NULL)
2478e3320f40Smarkfen Bail("malloc(soft_lifetime)");
2479e3320f40Smarkfen bzero(soft, sizeof (*soft));
2480e3320f40Smarkfen soft->sadb_lifetime_exttype =
2481e3320f40Smarkfen SADB_EXT_LIFETIME_SOFT;
2482e3320f40Smarkfen soft->sadb_lifetime_len =
2483e3320f40Smarkfen SADB_8TO64(sizeof (*soft));
2484e3320f40Smarkfen totallen += sizeof (*soft);
2485e3320f40Smarkfen }
2486e3320f40Smarkfen switch (token) {
2487e3320f40Smarkfen case TOK_SOFT_ALLOC:
2488e3320f40Smarkfen if (soft->sadb_lifetime_allocations != 0) {
2489e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2490e3320f40Smarkfen "Can only specify single"
2491e3320f40Smarkfen " soft allocation limit.\n"));
2492e3320f40Smarkfen break;
2493e3320f40Smarkfen }
2494e3320f40Smarkfen soft->sadb_lifetime_allocations =
2495e3320f40Smarkfen (uint32_t)parsenum(*argv, B_TRUE, ebuf);
2496e3320f40Smarkfen break;
2497e3320f40Smarkfen case TOK_SOFT_BYTES:
2498e3320f40Smarkfen if (soft->sadb_lifetime_bytes != 0) {
2499e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2500e3320f40Smarkfen "Can only specify single"
2501e3320f40Smarkfen " soft byte limit.\n"));
2502e3320f40Smarkfen break;
2503e3320f40Smarkfen }
2504e3320f40Smarkfen soft->sadb_lifetime_bytes = parsenum(*argv,
2505e3320f40Smarkfen B_TRUE, ebuf);
2506e3320f40Smarkfen break;
2507e3320f40Smarkfen case TOK_SOFT_ADDTIME:
2508e3320f40Smarkfen if (soft->sadb_lifetime_addtime != 0) {
2509e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2510e3320f40Smarkfen "Can only specify single"
2511e3320f40Smarkfen " past-add lifetime.\n"));
2512e3320f40Smarkfen break;
2513e3320f40Smarkfen }
2514e3320f40Smarkfen soft->sadb_lifetime_addtime = parsenum(*argv,
2515e3320f40Smarkfen B_TRUE, ebuf);
2516e3320f40Smarkfen break;
2517e3320f40Smarkfen case TOK_SOFT_USETIME:
2518e3320f40Smarkfen if (soft->sadb_lifetime_usetime != 0) {
2519e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2520e3320f40Smarkfen "Can only specify single"
2521e3320f40Smarkfen " past-use lifetime.\n"));
2522e3320f40Smarkfen break;
2523e3320f40Smarkfen }
2524e3320f40Smarkfen soft->sadb_lifetime_usetime = parsenum(*argv,
2525e3320f40Smarkfen B_TRUE, ebuf);
2526e3320f40Smarkfen break;
2527e3320f40Smarkfen }
2528e3320f40Smarkfen argv++;
2529e3320f40Smarkfen break;
253038d95a78Smarkfen case TOK_FLAG_INBOUND:
253138d95a78Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_INBOUND;
253238d95a78Smarkfen break;
253338d95a78Smarkfen case TOK_FLAG_OUTBOUND:
253438d95a78Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_OUTBOUND;
253538d95a78Smarkfen break;
25369c2c14abSThejaswini Singarajipura case TOK_REPLAY_VALUE:
25379c2c14abSThejaswini Singarajipura if (replay_ctr != NULL) {
25389c2c14abSThejaswini Singarajipura ERROR(ep, ebuf, gettext(
25399c2c14abSThejaswini Singarajipura "Can only specify single "
25409c2c14abSThejaswini Singarajipura "replay value."));
25419c2c14abSThejaswini Singarajipura break;
25429c2c14abSThejaswini Singarajipura }
25439c2c14abSThejaswini Singarajipura replay_ctr = calloc(1, sizeof (*replay_ctr));
25449c2c14abSThejaswini Singarajipura if (replay_ctr == NULL) {
25459c2c14abSThejaswini Singarajipura Bail("malloc(replay value)");
25469c2c14abSThejaswini Singarajipura }
25479c2c14abSThejaswini Singarajipura /*
25489c2c14abSThejaswini Singarajipura * We currently do not support a 64-bit
25499c2c14abSThejaswini Singarajipura * replay value. RFC 4301 will require one,
25509c2c14abSThejaswini Singarajipura * however, and we have a field in place when
25519c2c14abSThejaswini Singarajipura * 4301 is built.
25529c2c14abSThejaswini Singarajipura */
25539c2c14abSThejaswini Singarajipura replay_ctr->sadb_x_rc_exttype = SADB_X_EXT_REPLAY_VALUE;
25549c2c14abSThejaswini Singarajipura replay_ctr->sadb_x_rc_len =
25559c2c14abSThejaswini Singarajipura SADB_8TO64(sizeof (*replay_ctr));
25569c2c14abSThejaswini Singarajipura totallen += sizeof (*replay_ctr);
25579c2c14abSThejaswini Singarajipura replay_ctr->sadb_x_rc_replay32 = (uint32_t)parsenum(
25589c2c14abSThejaswini Singarajipura *argv, B_TRUE, ebuf);
25599c2c14abSThejaswini Singarajipura argv++;
25609c2c14abSThejaswini Singarajipura break;
25619c2c14abSThejaswini Singarajipura case TOK_IDLE_ADDTIME:
25629c2c14abSThejaswini Singarajipura case TOK_IDLE_USETIME:
25639c2c14abSThejaswini Singarajipura if (idle == NULL) {
25649c2c14abSThejaswini Singarajipura idle = calloc(1, sizeof (*idle));
25659c2c14abSThejaswini Singarajipura if (idle == NULL) {
25669c2c14abSThejaswini Singarajipura Bail("malloc idle lifetime");
25679c2c14abSThejaswini Singarajipura }
25689c2c14abSThejaswini Singarajipura idle->sadb_lifetime_exttype =
25699c2c14abSThejaswini Singarajipura SADB_X_EXT_LIFETIME_IDLE;
25709c2c14abSThejaswini Singarajipura idle->sadb_lifetime_len =
25719c2c14abSThejaswini Singarajipura SADB_8TO64(sizeof (*idle));
25729c2c14abSThejaswini Singarajipura totallen += sizeof (*idle);
25739c2c14abSThejaswini Singarajipura }
25749c2c14abSThejaswini Singarajipura switch (token) {
25759c2c14abSThejaswini Singarajipura case TOK_IDLE_ADDTIME:
25769c2c14abSThejaswini Singarajipura idle->sadb_lifetime_addtime =
25779c2c14abSThejaswini Singarajipura (uint32_t)parsenum(*argv,
25789c2c14abSThejaswini Singarajipura B_TRUE, ebuf);
25799c2c14abSThejaswini Singarajipura break;
25809c2c14abSThejaswini Singarajipura case TOK_IDLE_USETIME:
25819c2c14abSThejaswini Singarajipura idle->sadb_lifetime_usetime =
25829c2c14abSThejaswini Singarajipura (uint32_t)parsenum(*argv,
25839c2c14abSThejaswini Singarajipura B_TRUE, ebuf);
25849c2c14abSThejaswini Singarajipura break;
25859c2c14abSThejaswini Singarajipura }
25869c2c14abSThejaswini Singarajipura argv++;
25879c2c14abSThejaswini Singarajipura break;
2588628b0c67SMark Fenwick case TOK_RESERVED:
2589628b0c67SMark Fenwick if (encrypt != NULL)
2590628b0c67SMark Fenwick ERROR(ep, ebuf, gettext(
2591628b0c67SMark Fenwick "Reserved bits need to be "
2592628b0c67SMark Fenwick "specified before key.\n"));
2593628b0c67SMark Fenwick reserved_bits = (uint_t)parsenum(*argv,
2594628b0c67SMark Fenwick B_TRUE, ebuf);
2595628b0c67SMark Fenwick argv++;
2596628b0c67SMark Fenwick break;
25975d3b8cb7SBill Sommerfeld case TOK_LABEL:
25985d3b8cb7SBill Sommerfeld label = parselabel(token, *argv);
25995d3b8cb7SBill Sommerfeld argv++;
26005d3b8cb7SBill Sommerfeld if (label == NULL) {
26015d3b8cb7SBill Sommerfeld ERROR(ep, ebuf,
26025d3b8cb7SBill Sommerfeld gettext("Malformed security label\n"));
26035d3b8cb7SBill Sommerfeld break;
26045d3b8cb7SBill Sommerfeld } else if (label == PARSELABEL_BAD_TOKEN) {
26055d3b8cb7SBill Sommerfeld Bail("Internal token value error");
26065d3b8cb7SBill Sommerfeld }
26075d3b8cb7SBill Sommerfeld totallen += SADB_64TO8(label->sadb_sens_len);
26085d3b8cb7SBill Sommerfeld break;
26095d3b8cb7SBill Sommerfeld
26105d3b8cb7SBill Sommerfeld case TOK_OLABEL:
26115d3b8cb7SBill Sommerfeld case TOK_IMPLABEL:
26125d3b8cb7SBill Sommerfeld olabel = parselabel(token, *argv);
26135d3b8cb7SBill Sommerfeld argv++;
26145d3b8cb7SBill Sommerfeld if (label == NULL) {
26155d3b8cb7SBill Sommerfeld ERROR(ep, ebuf,
26165d3b8cb7SBill Sommerfeld gettext("Malformed security label\n"));
26175d3b8cb7SBill Sommerfeld break;
26185d3b8cb7SBill Sommerfeld } else if (label == PARSELABEL_BAD_TOKEN) {
26195d3b8cb7SBill Sommerfeld Bail("Internal token value error");
26205d3b8cb7SBill Sommerfeld }
26215d3b8cb7SBill Sommerfeld totallen += SADB_64TO8(olabel->sadb_sens_len);
26225d3b8cb7SBill Sommerfeld break;
2623e3320f40Smarkfen default:
2624e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2625e3320f40Smarkfen "Don't use extension %s for add/update.\n"),
2626e3320f40Smarkfen *(argv - 1));
2627e3320f40Smarkfen break;
2628e3320f40Smarkfen }
2629e3320f40Smarkfen } while (token != TOK_EOF);
2630e3320f40Smarkfen
2631e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE);
2632e3320f40Smarkfen
2633437220cdSdanmcd #define PORT_ONLY_ALLOCATE(af, socktype, exttype, extvar, port) { \
2634437220cdSdanmcd alloclen = sizeof (sadb_address_t) + roundup(sizeof (socktype), 8); \
2635437220cdSdanmcd (extvar) = calloc(1, alloclen); \
2636437220cdSdanmcd if ((extvar) == NULL) { \
2637437220cdSdanmcd Bail("malloc(implicit port)"); \
2638437220cdSdanmcd } \
2639437220cdSdanmcd totallen += alloclen; \
2640437220cdSdanmcd (extvar)->sadb_address_len = SADB_8TO64(alloclen); \
2641437220cdSdanmcd (extvar)->sadb_address_exttype = (exttype); \
2642437220cdSdanmcd /* sin/sin6 has equivalent offsets for ports! */ \
2643437220cdSdanmcd sin6 = (struct sockaddr_in6 *)((extvar) + 1); \
2644437220cdSdanmcd sin6->sin6_family = (af); \
2645437220cdSdanmcd sin6->sin6_port = (port); \
2646437220cdSdanmcd }
2647437220cdSdanmcd
2648e3320f40Smarkfen /*
2649437220cdSdanmcd * If we specify inner ports or NAT ports w/o addresses, we still need
2650437220cdSdanmcd * to allocate. Also, if we have one inner address, we need the
2651e3320f40Smarkfen * other, even if we don't specify anything.
2652e3320f40Smarkfen */
2653437220cdSdanmcd if (use_natt) {
2654437220cdSdanmcd if (natt_lport != 0 && natt_local == NULL) {
2655437220cdSdanmcd PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in,
2656437220cdSdanmcd SADB_X_EXT_ADDRESS_NATT_LOC, natt_local,
2657437220cdSdanmcd natt_lport);
2658e3320f40Smarkfen }
2659437220cdSdanmcd
2660437220cdSdanmcd if (natt_rport != 0 && natt_remote == NULL) {
2661437220cdSdanmcd PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in,
2662437220cdSdanmcd SADB_X_EXT_ADDRESS_NATT_REM, natt_remote,
2663437220cdSdanmcd natt_rport);
2664437220cdSdanmcd }
2665437220cdSdanmcd } else {
2666437220cdSdanmcd if (natt_lport != 0 || natt_rport != 0) {
2667437220cdSdanmcd ERROR(ep, ebuf, gettext("Must specify 'encap udp' "
2668437220cdSdanmcd "with any NAT-T port.\n"));
2669437220cdSdanmcd } else if (natt_local != NULL || natt_remote != NULL) {
2670437220cdSdanmcd ERROR(ep, ebuf, gettext("Must specify 'encap udp' "
2671437220cdSdanmcd "with any NAT-T address.\n"));
2672437220cdSdanmcd }
2673437220cdSdanmcd }
2674437220cdSdanmcd
2675437220cdSdanmcd if (alloc_inner && idst == NULL) {
2676437220cdSdanmcd PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6,
2677437220cdSdanmcd SADB_X_EXT_ADDRESS_INNER_DST, idst, 0);
2678e3320f40Smarkfen }
2679e3320f40Smarkfen
2680e3320f40Smarkfen if (alloc_inner && isrc == NULL) {
2681437220cdSdanmcd PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6,
2682437220cdSdanmcd SADB_X_EXT_ADDRESS_INNER_SRC, isrc, 0);
2683e3320f40Smarkfen }
2684437220cdSdanmcd #undef PORT_ONLY_ALLOCATE
2685e3320f40Smarkfen
2686e3320f40Smarkfen /*
2687e3320f40Smarkfen * Okay, so now I have all of the potential extensions!
2688e3320f40Smarkfen * Allocate a single contiguous buffer. Keep in mind that it'll
2689e3320f40Smarkfen * be enough because the key itself will be yanked.
2690e3320f40Smarkfen */
2691e3320f40Smarkfen
2692e3320f40Smarkfen if (src == NULL && dst != NULL) {
2693e3320f40Smarkfen /*
2694e3320f40Smarkfen * Set explicit unspecified source address.
2695e3320f40Smarkfen */
2696e3320f40Smarkfen size_t lenbytes = SADB_64TO8(dst->sadb_address_len);
2697e3320f40Smarkfen
2698e3320f40Smarkfen unspec_src = B_TRUE;
2699e3320f40Smarkfen totallen += lenbytes;
2700e3320f40Smarkfen src = malloc(lenbytes);
2701e3320f40Smarkfen if (src == NULL)
2702e3320f40Smarkfen Bail("malloc(implicit src)");
2703e3320f40Smarkfen /* Confusing, but we're copying from DST to SRC. :) */
2704e3320f40Smarkfen bcopy(dst, src, lenbytes);
2705e3320f40Smarkfen src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2706e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(src + 1);
2707e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
2708e3320f40Smarkfen sin6->sin6_family = AF_INET6;
2709e3320f40Smarkfen }
2710e3320f40Smarkfen
2711e3320f40Smarkfen msg.sadb_msg_len = SADB_8TO64(totallen);
2712e3320f40Smarkfen
2713e3320f40Smarkfen buffer = malloc(totallen);
2714e3320f40Smarkfen nexthdr = buffer;
2715e3320f40Smarkfen bcopy(&msg, nexthdr, sizeof (msg));
2716e3320f40Smarkfen nexthdr += SADB_8TO64(sizeof (msg));
2717e3320f40Smarkfen if (assoc != NULL) {
2718e3320f40Smarkfen if (assoc->sadb_sa_spi == 0) {
2719e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2720e3320f40Smarkfen "The SPI value is missing for "
2721e3320f40Smarkfen "the association you wish to %s.\n"), thiscmd);
2722e3320f40Smarkfen }
2723e3320f40Smarkfen if (assoc->sadb_sa_auth == 0 && assoc->sadb_sa_encrypt == 0 &&
2724e3320f40Smarkfen cmd == CMD_ADD) {
2725e3320f40Smarkfen free(assoc);
2726e3320f40Smarkfen FATAL(ep, ebuf, gettext(
2727e3320f40Smarkfen "Select at least one algorithm "
2728e3320f40Smarkfen "for this add.\n"));
2729e3320f40Smarkfen }
2730e3320f40Smarkfen
2731e3320f40Smarkfen /* Hack to let user specify NULL ESP implicitly. */
2732e3320f40Smarkfen if (msg.sadb_msg_satype == SADB_SATYPE_ESP &&
2733e3320f40Smarkfen assoc->sadb_sa_encrypt == 0)
2734e3320f40Smarkfen assoc->sadb_sa_encrypt = SADB_EALG_NULL;
2735e3320f40Smarkfen
2736e3320f40Smarkfen /* 0 is an actual value. Print a warning if it was entered. */
2737e3320f40Smarkfen if (assoc->sadb_sa_state == 0) {
2738e3320f40Smarkfen if (readstate) {
2739e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2740e3320f40Smarkfen "WARNING: Cannot set LARVAL SA state.\n"));
2741e3320f40Smarkfen }
2742e3320f40Smarkfen assoc->sadb_sa_state = SADB_SASTATE_MATURE;
2743e3320f40Smarkfen }
2744e3320f40Smarkfen
2745e3320f40Smarkfen if (use_natt) {
2746e3320f40Smarkfen if (natt_remote != NULL)
2747e3320f40Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_REM;
2748e3320f40Smarkfen if (natt_local != NULL)
2749e3320f40Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_LOC;
2750e3320f40Smarkfen }
2751e3320f40Smarkfen
2752e3320f40Smarkfen if (alloc_inner) {
2753e3320f40Smarkfen /*
2754e3320f40Smarkfen * For now, assume RFC 3884's dream of transport-mode
2755e3320f40Smarkfen * SAs with inner IP address selectors will not
2756e3320f40Smarkfen * happen.
2757e3320f40Smarkfen */
2758e3320f40Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL;
2759e3320f40Smarkfen if (proto != 0 && proto != IPPROTO_ENCAP &&
2760e3320f40Smarkfen proto != IPPROTO_IPV6) {
2761e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2762e3320f40Smarkfen "WARNING: Protocol type %d not "
2763e3320f40Smarkfen "for use with Tunnel-Mode SA.\n"), proto);
2764e3320f40Smarkfen /* Continue and let PF_KEY scream... */
2765e3320f40Smarkfen }
2766e3320f40Smarkfen }
2767e3320f40Smarkfen
2768e3320f40Smarkfen bcopy(assoc, nexthdr, SADB_64TO8(assoc->sadb_sa_len));
2769e3320f40Smarkfen nexthdr += assoc->sadb_sa_len;
2770e3320f40Smarkfen /* Save the SPI for the case of an error. */
2771e3320f40Smarkfen spi = assoc->sadb_sa_spi;
2772e3320f40Smarkfen free(assoc);
2773e3320f40Smarkfen } else {
2774a050d7e9Spwernau if (spi == 0)
2775a050d7e9Spwernau ERROR1(ep, ebuf, gettext(
2776a050d7e9Spwernau "Need to define SPI for %s.\n"), thiscmd);
2777e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
2778e3320f40Smarkfen "Need SA parameters for %s.\n"), thiscmd);
2779e3320f40Smarkfen }
2780e3320f40Smarkfen
278138d95a78Smarkfen if (sadb_pair != NULL) {
278238d95a78Smarkfen if (sadb_pair->sadb_x_pair_spi == 0) {
278338d95a78Smarkfen ERROR1(ep, ebuf, gettext(
278438d95a78Smarkfen "The SPI value is missing for the "
278538d95a78Smarkfen "association you wish to %s.\n"), thiscmd);
278638d95a78Smarkfen }
278738d95a78Smarkfen bcopy(sadb_pair, nexthdr,
278838d95a78Smarkfen SADB_64TO8(sadb_pair->sadb_x_pair_len));
278938d95a78Smarkfen nexthdr += sadb_pair->sadb_x_pair_len;
279038d95a78Smarkfen free(sadb_pair);
279138d95a78Smarkfen }
279238d95a78Smarkfen
2793e3320f40Smarkfen if (hard != NULL) {
2794e3320f40Smarkfen bcopy(hard, nexthdr, SADB_64TO8(hard->sadb_lifetime_len));
2795e3320f40Smarkfen nexthdr += hard->sadb_lifetime_len;
2796e3320f40Smarkfen free(hard);
2797e3320f40Smarkfen }
2798e3320f40Smarkfen
2799e3320f40Smarkfen if (soft != NULL) {
2800e3320f40Smarkfen bcopy(soft, nexthdr, SADB_64TO8(soft->sadb_lifetime_len));
2801e3320f40Smarkfen nexthdr += soft->sadb_lifetime_len;
2802e3320f40Smarkfen free(soft);
2803e3320f40Smarkfen }
2804e3320f40Smarkfen
28059c2c14abSThejaswini Singarajipura if (idle != NULL) {
28069c2c14abSThejaswini Singarajipura bcopy(idle, nexthdr, SADB_64TO8(idle->sadb_lifetime_len));
28079c2c14abSThejaswini Singarajipura nexthdr += idle->sadb_lifetime_len;
28089c2c14abSThejaswini Singarajipura free(idle);
28099c2c14abSThejaswini Singarajipura }
28109c2c14abSThejaswini Singarajipura
2811e3320f40Smarkfen if (encrypt == NULL && auth == NULL && cmd == CMD_ADD) {
2812e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2813e3320f40Smarkfen "Must have at least one key for an add.\n"));
2814e3320f40Smarkfen }
2815e3320f40Smarkfen
2816e3320f40Smarkfen if (encrypt != NULL) {
2817e3320f40Smarkfen bcopy(encrypt, nexthdr, SADB_64TO8(encrypt->sadb_key_len));
2818e3320f40Smarkfen nexthdr += encrypt->sadb_key_len;
2819e3320f40Smarkfen bzero(encrypt, SADB_64TO8(encrypt->sadb_key_len));
2820e3320f40Smarkfen free(encrypt);
2821e3320f40Smarkfen }
2822e3320f40Smarkfen
2823e3320f40Smarkfen if (auth != NULL) {
2824e3320f40Smarkfen bcopy(auth, nexthdr, SADB_64TO8(auth->sadb_key_len));
2825e3320f40Smarkfen nexthdr += auth->sadb_key_len;
2826e3320f40Smarkfen bzero(auth, SADB_64TO8(auth->sadb_key_len));
2827e3320f40Smarkfen free(auth);
2828e3320f40Smarkfen }
2829e3320f40Smarkfen
2830e3320f40Smarkfen if (srcid != NULL) {
2831e3320f40Smarkfen bcopy(srcid, nexthdr, SADB_64TO8(srcid->sadb_ident_len));
2832e3320f40Smarkfen nexthdr += srcid->sadb_ident_len;
2833e3320f40Smarkfen free(srcid);
2834e3320f40Smarkfen }
2835e3320f40Smarkfen
2836e3320f40Smarkfen if (dstid != NULL) {
2837e3320f40Smarkfen bcopy(dstid, nexthdr, SADB_64TO8(dstid->sadb_ident_len));
2838e3320f40Smarkfen nexthdr += dstid->sadb_ident_len;
2839e3320f40Smarkfen free(dstid);
2840e3320f40Smarkfen }
2841e3320f40Smarkfen
2842e3320f40Smarkfen if (dst != NULL) {
2843e3320f40Smarkfen bcopy(dst, nexthdr, SADB_64TO8(dst->sadb_address_len));
2844e3320f40Smarkfen free(dst);
2845e3320f40Smarkfen dst = (struct sadb_address *)nexthdr;
2846e3320f40Smarkfen dst->sadb_address_proto = proto;
2847e3320f40Smarkfen ((struct sockaddr_in6 *)(dst + 1))->sin6_port = htons(dstport);
2848e3320f40Smarkfen nexthdr += dst->sadb_address_len;
2849e3320f40Smarkfen } else {
2850e3320f40Smarkfen FATAL1(ep, ebuf, gettext(
2851e3320f40Smarkfen "Need destination address for %s.\n"), thiscmd);
2852e3320f40Smarkfen }
2853e3320f40Smarkfen
2854e3320f40Smarkfen if (use_natt) {
2855e3320f40Smarkfen if (natt_remote == NULL && natt_local == NULL) {
2856e3320f40Smarkfen ERROR(ep, ebuf, gettext(
2857e3320f40Smarkfen "Must specify NAT-T remote or local address "
2858e3320f40Smarkfen "for UDP encapsulation.\n"));
2859e3320f40Smarkfen }
2860e3320f40Smarkfen
2861e3320f40Smarkfen if (natt_remote != NULL) {
2862e3320f40Smarkfen bcopy(natt_remote, nexthdr,
2863e3320f40Smarkfen SADB_64TO8(natt_remote->sadb_address_len));
2864e3320f40Smarkfen free(natt_remote);
2865e3320f40Smarkfen natt_remote = (struct sadb_address *)nexthdr;
2866e3320f40Smarkfen nexthdr += natt_remote->sadb_address_len;
2867e3320f40Smarkfen ((struct sockaddr_in6 *)(natt_remote + 1))->sin6_port =
2868e3320f40Smarkfen htons(natt_rport);
2869e3320f40Smarkfen }
2870e3320f40Smarkfen
2871e3320f40Smarkfen if (natt_local != NULL) {
2872e3320f40Smarkfen bcopy(natt_local, nexthdr,
2873e3320f40Smarkfen SADB_64TO8(natt_local->sadb_address_len));
2874e3320f40Smarkfen free(natt_local);
2875e3320f40Smarkfen natt_local = (struct sadb_address *)nexthdr;
2876e3320f40Smarkfen nexthdr += natt_local->sadb_address_len;
2877e3320f40Smarkfen ((struct sockaddr_in6 *)(natt_local + 1))->sin6_port =
2878e3320f40Smarkfen htons(natt_lport);
2879e3320f40Smarkfen }
2880e3320f40Smarkfen }
2881e3320f40Smarkfen
2882e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE);
2883e3320f40Smarkfen
2884e3320f40Smarkfen /*
2885e3320f40Smarkfen * PF_KEY requires a source address extension, even if the source
2886e3320f40Smarkfen * address itself is unspecified. (See "Set explicit unspecified..."
2887e3320f40Smarkfen * code fragment above. Destination reality check was above.)
2888e3320f40Smarkfen */
2889e3320f40Smarkfen bcopy(src, nexthdr, SADB_64TO8(src->sadb_address_len));
2890e3320f40Smarkfen free(src);
2891e3320f40Smarkfen src = (struct sadb_address *)nexthdr;
2892e3320f40Smarkfen src->sadb_address_proto = proto;
2893e3320f40Smarkfen ((struct sockaddr_in6 *)(src + 1))->sin6_port = htons(srcport);
2894e3320f40Smarkfen nexthdr += src->sadb_address_len;
2895e3320f40Smarkfen
2896e3320f40Smarkfen if (isrc != NULL) {
2897e3320f40Smarkfen bcopy(isrc, nexthdr, SADB_64TO8(isrc->sadb_address_len));
2898e3320f40Smarkfen free(isrc);
2899e3320f40Smarkfen isrc = (struct sadb_address *)nexthdr;
2900e3320f40Smarkfen isrc->sadb_address_proto = iproto;
2901e3320f40Smarkfen ((struct sockaddr_in6 *)(isrc + 1))->sin6_port =
2902e3320f40Smarkfen htons(isrcport);
2903e3320f40Smarkfen nexthdr += isrc->sadb_address_len;
2904e3320f40Smarkfen }
2905e3320f40Smarkfen
2906e3320f40Smarkfen if (idst != NULL) {
2907e3320f40Smarkfen bcopy(idst, nexthdr, SADB_64TO8(idst->sadb_address_len));
2908e3320f40Smarkfen free(idst);
2909e3320f40Smarkfen idst = (struct sadb_address *)nexthdr;
2910e3320f40Smarkfen idst->sadb_address_proto = iproto;
2911e3320f40Smarkfen ((struct sockaddr_in6 *)(idst + 1))->sin6_port =
2912e3320f40Smarkfen htons(idstport);
2913e3320f40Smarkfen nexthdr += idst->sadb_address_len;
2914e3320f40Smarkfen }
2915e3320f40Smarkfen
29169c2c14abSThejaswini Singarajipura if (replay_ctr != NULL) {
29179c2c14abSThejaswini Singarajipura bcopy(replay_ctr, nexthdr,
29189c2c14abSThejaswini Singarajipura SADB_64TO8(replay_ctr->sadb_x_rc_len));
29199c2c14abSThejaswini Singarajipura nexthdr += replay_ctr->sadb_x_rc_len;
29209c2c14abSThejaswini Singarajipura free(replay_ctr);
29219c2c14abSThejaswini Singarajipura }
29229c2c14abSThejaswini Singarajipura
29235d3b8cb7SBill Sommerfeld if (label != NULL) {
29245d3b8cb7SBill Sommerfeld bcopy(label, nexthdr, SADB_64TO8(label->sadb_sens_len));
29255d3b8cb7SBill Sommerfeld nexthdr += label->sadb_sens_len;
29265d3b8cb7SBill Sommerfeld free(label);
29275d3b8cb7SBill Sommerfeld label = NULL;
29285d3b8cb7SBill Sommerfeld }
29295d3b8cb7SBill Sommerfeld
29305d3b8cb7SBill Sommerfeld if (olabel != NULL) {
29315d3b8cb7SBill Sommerfeld bcopy(olabel, nexthdr, SADB_64TO8(olabel->sadb_sens_len));
29325d3b8cb7SBill Sommerfeld nexthdr += olabel->sadb_sens_len;
29335d3b8cb7SBill Sommerfeld free(olabel);
29345d3b8cb7SBill Sommerfeld olabel = NULL;
29355d3b8cb7SBill Sommerfeld }
29365d3b8cb7SBill Sommerfeld
293725e435e0Spwernau if (cflag) {
293825e435e0Spwernau /*
293925e435e0Spwernau * Assume the checked cmd would have worked if it was actually
294025e435e0Spwernau * used. doaddresses() will increment lines_added if it
294125e435e0Spwernau * succeeds.
294225e435e0Spwernau */
294325e435e0Spwernau lines_added++;
294425e435e0Spwernau } else {
294538d95a78Smarkfen doaddresses(sadb_msg_type, satype,
2946e3320f40Smarkfen cmd, srchp, dsthp, src, dst, unspec_src, buffer, totallen,
2947e3320f40Smarkfen spi, ebuf);
2948e3320f40Smarkfen }
2949e3320f40Smarkfen
2950e3320f40Smarkfen if (isrchp != NULL && isrchp != &dummy.he)
2951e3320f40Smarkfen freehostent(isrchp);
2952e3320f40Smarkfen if (idsthp != NULL && idsthp != &dummy.he)
2953e3320f40Smarkfen freehostent(idsthp);
2954e3320f40Smarkfen if (srchp != NULL && srchp != &dummy.he)
2955e3320f40Smarkfen freehostent(srchp);
2956e3320f40Smarkfen if (dsthp != NULL && dsthp != &dummy.he)
2957e3320f40Smarkfen freehostent(dsthp);
2958e3320f40Smarkfen if (natt_lhp != NULL && natt_lhp != &dummy.he)
2959e3320f40Smarkfen freehostent(natt_lhp);
2960e3320f40Smarkfen if (natt_rhp != NULL && natt_rhp != &dummy.he)
2961e3320f40Smarkfen freehostent(natt_rhp);
2962e3320f40Smarkfen free(ebuf);
2963e3320f40Smarkfen free(buffer);
2964e3320f40Smarkfen }
2965e3320f40Smarkfen
2966e3320f40Smarkfen /*
2967e3320f40Smarkfen * DELETE and GET are similar, in that they only need the extensions
2968e3320f40Smarkfen * required to _find_ an SA, and then either delete it or obtain its
2969e3320f40Smarkfen * information.
2970e3320f40Smarkfen */
2971e3320f40Smarkfen static void
dodelget(int cmd,int satype,char * argv[],char * ebuf)2972e3320f40Smarkfen dodelget(int cmd, int satype, char *argv[], char *ebuf)
2973e3320f40Smarkfen {
2974e3320f40Smarkfen struct sadb_msg *msg = (struct sadb_msg *)get_buffer;
2975e3320f40Smarkfen uint64_t *nextext;
2976e3320f40Smarkfen struct sadb_sa *assoc = NULL;
2977e3320f40Smarkfen struct sadb_address *src = NULL, *dst = NULL;
2978e3320f40Smarkfen int next, token, sa_len;
2979e3320f40Smarkfen char *thiscmd;
2980e3320f40Smarkfen uint32_t spi;
298138d95a78Smarkfen uint8_t sadb_msg_type;
2982e3320f40Smarkfen struct hostent *srchp = NULL, *dsthp = NULL;
2983e3320f40Smarkfen struct sockaddr_in6 *sin6;
2984e3320f40Smarkfen boolean_t unspec_src = B_TRUE;
2985e3320f40Smarkfen uint16_t srcport = 0, dstport = 0;
2986e3320f40Smarkfen uint8_t proto = 0;
2987e3320f40Smarkfen char *ep = NULL;
2988df8eb1c6SVladimir Kotal uint32_t sa_flags = 0;
2989e3320f40Smarkfen
2990e3320f40Smarkfen /* Set the first extension header to right past the base message. */
2991e3320f40Smarkfen nextext = (uint64_t *)(msg + 1);
2992e3320f40Smarkfen bzero(nextext, sizeof (get_buffer) - sizeof (*msg));
2993e3320f40Smarkfen
299438d95a78Smarkfen switch (cmd) {
299538d95a78Smarkfen case CMD_GET:
299638d95a78Smarkfen thiscmd = "get";
299738d95a78Smarkfen sadb_msg_type = SADB_GET;
299838d95a78Smarkfen break;
299938d95a78Smarkfen case CMD_DELETE:
300038d95a78Smarkfen thiscmd = "delete";
300138d95a78Smarkfen sadb_msg_type = SADB_DELETE;
300238d95a78Smarkfen break;
300338d95a78Smarkfen case CMD_DELETE_PAIR:
300438d95a78Smarkfen thiscmd = "delete-pair";
300538d95a78Smarkfen sadb_msg_type = SADB_X_DELPAIR;
300638d95a78Smarkfen break;
300738d95a78Smarkfen }
300838d95a78Smarkfen
300938d95a78Smarkfen msg_init(msg, sadb_msg_type, (uint8_t)satype);
3010e3320f40Smarkfen
3011e3320f40Smarkfen #define ALLOC_ADDR_EXT(ext, exttype) \
3012e3320f40Smarkfen (ext) = (struct sadb_address *)nextext; \
3013e3320f40Smarkfen nextext = (uint64_t *)((ext) + 1); \
3014e3320f40Smarkfen nextext += SADB_8TO64(roundup(sa_len, 8)); \
3015e3320f40Smarkfen (ext)->sadb_address_exttype = exttype; \
3016e3320f40Smarkfen (ext)->sadb_address_len = nextext - ((uint64_t *)ext);
3017e3320f40Smarkfen
3018e3320f40Smarkfen /* Assume last element in argv is set to NULL. */
3019e3320f40Smarkfen do {
3020e3320f40Smarkfen token = parseextval(*argv, &next);
3021e3320f40Smarkfen argv++;
3022e3320f40Smarkfen switch (token) {
3023e3320f40Smarkfen case TOK_EOF:
3024e3320f40Smarkfen /* Do nothing, I'm done. */
3025e3320f40Smarkfen break;
3026e3320f40Smarkfen case TOK_UNKNOWN:
3027e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
3028e3320f40Smarkfen "Unknown extension field \"%s\"\n"), *(argv - 1));
3029e3320f40Smarkfen break;
3030e3320f40Smarkfen case TOK_SPI:
3031e3320f40Smarkfen if (assoc != NULL) {
3032e3320f40Smarkfen ERROR(ep, ebuf, gettext(
3033e3320f40Smarkfen "Can only specify single SPI value.\n"));
3034e3320f40Smarkfen break;
3035e3320f40Smarkfen }
3036e3320f40Smarkfen assoc = (struct sadb_sa *)nextext;
3037e3320f40Smarkfen nextext = (uint64_t *)(assoc + 1);
3038e3320f40Smarkfen assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc));
3039e3320f40Smarkfen assoc->sadb_sa_exttype = SADB_EXT_SA;
3040e3320f40Smarkfen assoc->sadb_sa_spi = htonl((uint32_t)parsenum(*argv,
3041e3320f40Smarkfen B_TRUE, ebuf));
3042e3320f40Smarkfen spi = assoc->sadb_sa_spi;
3043e3320f40Smarkfen argv++;
3044e3320f40Smarkfen break;
3045e3320f40Smarkfen case TOK_SRCPORT:
3046e3320f40Smarkfen if (srcport != 0) {
3047e3320f40Smarkfen ERROR(ep, ebuf, gettext(
3048e3320f40Smarkfen "Can only specify single source port.\n"));
3049e3320f40Smarkfen break;
3050e3320f40Smarkfen }
3051e3320f40Smarkfen srcport = parsenum(*argv, B_TRUE, ebuf);
3052e3320f40Smarkfen argv++;
3053e3320f40Smarkfen break;
3054e3320f40Smarkfen case TOK_DSTPORT:
3055e3320f40Smarkfen if (dstport != 0) {
3056e3320f40Smarkfen ERROR(ep, ebuf, gettext(
3057e3320f40Smarkfen "Can only "
3058e3320f40Smarkfen "specify single destination port.\n"));
3059e3320f40Smarkfen break;
3060e3320f40Smarkfen }
3061e3320f40Smarkfen dstport = parsenum(*argv, B_TRUE, ebuf);
3062e3320f40Smarkfen argv++;
3063e3320f40Smarkfen break;
3064e3320f40Smarkfen case TOK_PROTO:
3065e3320f40Smarkfen if (proto != 0) {
3066e3320f40Smarkfen ERROR(ep, ebuf, gettext(
3067e3320f40Smarkfen "Can only specify single protocol.\n"));
3068e3320f40Smarkfen break;
3069e3320f40Smarkfen }
3070e3320f40Smarkfen proto = parsenum(*argv, B_TRUE, ebuf);
3071e3320f40Smarkfen argv++;
3072e3320f40Smarkfen break;
3073e3320f40Smarkfen case TOK_SRCADDR:
3074e3320f40Smarkfen case TOK_SRCADDR6:
3075e3320f40Smarkfen if (src != NULL) {
3076e3320f40Smarkfen ERROR(ep, ebuf, gettext(
3077e3320f40Smarkfen "Can only specify single source addr.\n"));
3078e3320f40Smarkfen break;
3079e3320f40Smarkfen }
3080e3320f40Smarkfen sa_len = parseaddr(*argv, &srchp,
3081e3320f40Smarkfen (token == TOK_SRCADDR6), ebuf);
3082e3320f40Smarkfen if (srchp == NULL) {
3083e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
3084e3320f40Smarkfen "Unknown source address \"%s\"\n"), *argv);
3085e3320f40Smarkfen break;
3086e3320f40Smarkfen }
3087e3320f40Smarkfen argv++;
3088e3320f40Smarkfen
3089e3320f40Smarkfen unspec_src = B_FALSE;
3090e3320f40Smarkfen
3091e3320f40Smarkfen ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
3092e3320f40Smarkfen
3093e3320f40Smarkfen if (srchp == &dummy.he) {
3094e3320f40Smarkfen /*
3095e3320f40Smarkfen * Single address with -n flag.
3096e3320f40Smarkfen */
3097e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(src + 1);
3098e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
3099e3320f40Smarkfen sin6->sin6_family = AF_INET6;
3100e3320f40Smarkfen bcopy(srchp->h_addr_list[0], &sin6->sin6_addr,
3101e3320f40Smarkfen sizeof (struct in6_addr));
3102e3320f40Smarkfen }
3103e3320f40Smarkfen /* The rest is pre-bzeroed for us. */
3104e3320f40Smarkfen break;
3105e3320f40Smarkfen case TOK_DSTADDR:
3106e3320f40Smarkfen case TOK_DSTADDR6:
3107e3320f40Smarkfen if (dst != NULL) {
3108e3320f40Smarkfen ERROR(ep, ebuf, gettext(
3109e3320f40Smarkfen "Can only specify single destination "
3110e3320f40Smarkfen "address.\n"));
3111e3320f40Smarkfen break;
3112e3320f40Smarkfen }
3113e3320f40Smarkfen sa_len = parseaddr(*argv, &dsthp,
3114e3320f40Smarkfen (token == TOK_SRCADDR6), ebuf);
3115e3320f40Smarkfen if (dsthp == NULL) {
3116e3320f40Smarkfen ERROR1(ep, ebuf, gettext(
3117e3320f40Smarkfen "Unknown destination address \"%s\"\n"),
3118e3320f40Smarkfen *argv);
3119e3320f40Smarkfen break;
3120e3320f40Smarkfen }
3121e3320f40Smarkfen argv++;
3122e3320f40Smarkfen
3123e3320f40Smarkfen ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
3124e3320f40Smarkfen
3125e3320f40Smarkfen if (dsthp == &dummy.he) {
3126e3320f40Smarkfen /*
3127e3320f40Smarkfen * Single address with -n flag.
3128e3320f40Smarkfen */
3129e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(dst + 1);
3130e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
3131e3320f40Smarkfen sin6->sin6_family = AF_INET6;
3132e3320f40Smarkfen bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr,
3133e3320f40Smarkfen sizeof (struct in6_addr));
3134e3320f40Smarkfen }
3135e3320f40Smarkfen /* The rest is pre-bzeroed for us. */
3136e3320f40Smarkfen break;
313738d95a78Smarkfen case TOK_FLAG_INBOUND:
3138df8eb1c6SVladimir Kotal sa_flags |= SADB_X_SAFLAGS_INBOUND;
313938d95a78Smarkfen break;
314038d95a78Smarkfen case TOK_FLAG_OUTBOUND:
3141df8eb1c6SVladimir Kotal sa_flags |= SADB_X_SAFLAGS_OUTBOUND;
314238d95a78Smarkfen break;
3143e3320f40Smarkfen default:
3144e3320f40Smarkfen ERROR2(ep, ebuf, gettext(
3145e3320f40Smarkfen "Don't use extension %s for '%s' command.\n"),
3146e3320f40Smarkfen *(argv - 1), thiscmd);
3147e3320f40Smarkfen break;
3148e3320f40Smarkfen }
3149e3320f40Smarkfen } while (token != TOK_EOF);
3150e3320f40Smarkfen
3151e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE);
3152e3320f40Smarkfen
3153df8eb1c6SVladimir Kotal if (assoc == NULL) {
3154df8eb1c6SVladimir Kotal FATAL1(ep, ebuf, gettext(
3155df8eb1c6SVladimir Kotal "Need SA parameters for %s.\n"), thiscmd);
3156df8eb1c6SVladimir Kotal }
3157df8eb1c6SVladimir Kotal
3158df8eb1c6SVladimir Kotal /* We can set the flags now with valid assoc in hand. */
3159df8eb1c6SVladimir Kotal assoc->sadb_sa_flags |= sa_flags;
3160df8eb1c6SVladimir Kotal
3161e3320f40Smarkfen if ((srcport != 0) && (src == NULL)) {
3162e3320f40Smarkfen ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC);
3163e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(src + 1);
3164e3320f40Smarkfen src->sadb_address_proto = proto;
3165e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
3166e3320f40Smarkfen sin6->sin6_family = AF_INET6;
3167e3320f40Smarkfen sin6->sin6_port = htons(srcport);
3168e3320f40Smarkfen }
3169e3320f40Smarkfen
3170e3320f40Smarkfen if ((dstport != 0) && (dst == NULL)) {
3171e3320f40Smarkfen ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST);
3172e3320f40Smarkfen sin6 = (struct sockaddr_in6 *)(dst + 1);
3173e3320f40Smarkfen src->sadb_address_proto = proto;
3174e3320f40Smarkfen bzero(sin6, sizeof (*sin6));
3175e3320f40Smarkfen sin6->sin6_family = AF_INET6;
3176e3320f40Smarkfen sin6->sin6_port = htons(dstport);
3177e3320f40Smarkfen }
3178e3320f40Smarkfen
3179e3320f40Smarkfen /* So I have enough of the message to send it down! */
3180e3320f40Smarkfen msg->sadb_msg_len = nextext - get_buffer;
3181e3320f40Smarkfen
318225e435e0Spwernau if (cflag) {
318325e435e0Spwernau /*
318425e435e0Spwernau * Assume the checked cmd would have worked if it was actually
318525e435e0Spwernau * used. doaddresses() will increment lines_added if it
318625e435e0Spwernau * succeeds.
318725e435e0Spwernau */
318825e435e0Spwernau lines_added++;
318925e435e0Spwernau } else {
319038d95a78Smarkfen doaddresses(sadb_msg_type, satype,
3191e3320f40Smarkfen cmd, srchp, dsthp, src, dst, unspec_src, get_buffer,
3192e3320f40Smarkfen sizeof (get_buffer), spi, NULL);
3193e3320f40Smarkfen }
3194e3320f40Smarkfen
3195e3320f40Smarkfen if (srchp != NULL && srchp != &dummy.he)
3196e3320f40Smarkfen freehostent(srchp);
3197e3320f40Smarkfen if (dsthp != NULL && dsthp != &dummy.he)
3198e3320f40Smarkfen freehostent(dsthp);
3199e3320f40Smarkfen }
3200e3320f40Smarkfen
3201e3320f40Smarkfen /*
3202bfe6f8f5SVladimir Kotal * "ipseckey monitor" should exit very gracefully if ^C is tapped provided
3203bfe6f8f5SVladimir Kotal * it is not running in interactive mode.
3204e3320f40Smarkfen */
3205e3320f40Smarkfen static void
monitor_catch(int signal)3206e3320f40Smarkfen monitor_catch(int signal)
3207e3320f40Smarkfen {
3208bfe6f8f5SVladimir Kotal if (!interactive)
3209e3320f40Smarkfen errx(signal, gettext("Bailing on signal %d."), signal);
3210e3320f40Smarkfen }
3211e3320f40Smarkfen
3212e3320f40Smarkfen /*
3213e3320f40Smarkfen * Loop forever, listening on PF_KEY messages.
3214e3320f40Smarkfen */
3215e3320f40Smarkfen static void
domonitor(boolean_t passive)3216e3320f40Smarkfen domonitor(boolean_t passive)
3217e3320f40Smarkfen {
3218e3320f40Smarkfen struct sadb_msg *samsg;
3219bfe6f8f5SVladimir Kotal struct sigaction newsig, oldsig;
3220e3320f40Smarkfen int rc;
3221e3320f40Smarkfen
3222e3320f40Smarkfen /* Catch ^C. */
3223bfe6f8f5SVladimir Kotal newsig.sa_handler = monitor_catch;
3224bfe6f8f5SVladimir Kotal newsig.sa_flags = 0;
3225bfe6f8f5SVladimir Kotal (void) sigemptyset(&newsig.sa_mask);
3226bfe6f8f5SVladimir Kotal (void) sigaddset(&newsig.sa_mask, SIGINT);
3227bfe6f8f5SVladimir Kotal (void) sigaction(SIGINT, &newsig, &oldsig);
3228e3320f40Smarkfen
3229e3320f40Smarkfen samsg = (struct sadb_msg *)get_buffer;
3230e3320f40Smarkfen if (!passive) {
3231e3320f40Smarkfen (void) printf(gettext("Actively"));
3232e3320f40Smarkfen msg_init(samsg, SADB_X_PROMISC, 1); /* Turn ON promisc. */
3233e3320f40Smarkfen rc = key_write(keysock, samsg, sizeof (*samsg));
3234e3320f40Smarkfen if (rc == -1)
3235e3320f40Smarkfen Bail("write (SADB_X_PROMISC)");
3236e3320f40Smarkfen } else {
3237e3320f40Smarkfen (void) printf(gettext("Passively"));
3238e3320f40Smarkfen }
3239e3320f40Smarkfen (void) printf(gettext(" monitoring the PF_KEY socket.\n"));
3240e3320f40Smarkfen
3241e3320f40Smarkfen for (; ; ) {
3242e3320f40Smarkfen /*
3243e3320f40Smarkfen * I assume that read() is non-blocking, and will never
3244e3320f40Smarkfen * return 0.
3245e3320f40Smarkfen */
3246e3320f40Smarkfen rc = read(keysock, samsg, sizeof (get_buffer));
3247bfe6f8f5SVladimir Kotal if (rc == -1) {
3248bfe6f8f5SVladimir Kotal if (errno == EINTR && interactive)
3249bfe6f8f5SVladimir Kotal goto out;
3250bfe6f8f5SVladimir Kotal else
3251e3320f40Smarkfen Bail("read (in domonitor)");
3252bfe6f8f5SVladimir Kotal }
3253e3320f40Smarkfen (void) printf(gettext("Read %d bytes.\n"), rc);
3254e3320f40Smarkfen /*
3255e3320f40Smarkfen * Q: Should I use the same method of printing as GET does?
3256e3320f40Smarkfen * A: For now, yes.
3257e3320f40Smarkfen */
3258bb3ed8dfSpwernau print_samsg(stdout, get_buffer, B_TRUE, vflag, nflag);
3259e3320f40Smarkfen (void) putchar('\n');
3260e3320f40Smarkfen }
3261bfe6f8f5SVladimir Kotal
3262bfe6f8f5SVladimir Kotal out:
3263bfe6f8f5SVladimir Kotal if (interactive)
3264bfe6f8f5SVladimir Kotal /* restore SIGINT behavior */
3265bfe6f8f5SVladimir Kotal (void) sigaction(SIGINT, &oldsig, NULL);
3266e3320f40Smarkfen }
3267e3320f40Smarkfen
3268e3320f40Smarkfen /*
3269e3320f40Smarkfen * Either mask or unmask all relevant signals.
3270e3320f40Smarkfen */
3271e3320f40Smarkfen static void
mask_signals(boolean_t unmask)3272e3320f40Smarkfen mask_signals(boolean_t unmask)
3273e3320f40Smarkfen {
3274e3320f40Smarkfen sigset_t set;
3275e3320f40Smarkfen static sigset_t oset;
3276e3320f40Smarkfen
3277e3320f40Smarkfen if (unmask) {
3278e3320f40Smarkfen (void) sigprocmask(SIG_SETMASK, &oset, NULL);
3279e3320f40Smarkfen } else {
3280e3320f40Smarkfen (void) sigfillset(&set);
3281e3320f40Smarkfen (void) sigprocmask(SIG_SETMASK, &set, &oset);
3282e3320f40Smarkfen }
3283e3320f40Smarkfen }
3284e3320f40Smarkfen
3285e3320f40Smarkfen /*
3286e3320f40Smarkfen * Assorted functions to print help text.
3287e3320f40Smarkfen */
3288e3320f40Smarkfen #define puts_tr(s) (void) puts(gettext(s))
3289e3320f40Smarkfen
3290e3320f40Smarkfen static void
doattrhelp()3291e3320f40Smarkfen doattrhelp()
3292e3320f40Smarkfen {
3293e3320f40Smarkfen int i;
3294e3320f40Smarkfen
3295e3320f40Smarkfen puts_tr("\nSA attributes:");
3296e3320f40Smarkfen
3297e3320f40Smarkfen for (i = 0; tokens[i].string != NULL; i++) {
3298e3320f40Smarkfen if (i%3 == 0)
3299e3320f40Smarkfen (void) printf("\n");
3300e3320f40Smarkfen (void) printf(" %-15.15s", tokens[i].string);
3301e3320f40Smarkfen }
3302e3320f40Smarkfen (void) printf("\n");
3303e3320f40Smarkfen }
3304e3320f40Smarkfen
3305e3320f40Smarkfen static void
dohelpcmd(char * cmds)3306e3320f40Smarkfen dohelpcmd(char *cmds)
3307e3320f40Smarkfen {
3308e3320f40Smarkfen int cmd;
3309e3320f40Smarkfen
3310e3320f40Smarkfen if (strcmp(cmds, "attr") == 0) {
3311e3320f40Smarkfen doattrhelp();
3312e3320f40Smarkfen return;
3313e3320f40Smarkfen }
3314e3320f40Smarkfen
3315e3320f40Smarkfen cmd = parsecmd(cmds);
3316e3320f40Smarkfen switch (cmd) {
3317e3320f40Smarkfen case CMD_UPDATE:
3318e3320f40Smarkfen puts_tr("update - Update an existing SA");
3319e3320f40Smarkfen break;
332038d95a78Smarkfen case CMD_UPDATE_PAIR:
332138d95a78Smarkfen puts_tr("update-pair - Update an existing pair of SA's");
332238d95a78Smarkfen break;
3323e3320f40Smarkfen case CMD_ADD:
3324e3320f40Smarkfen puts_tr("add - Add a new security association (SA)");
3325e3320f40Smarkfen break;
3326e3320f40Smarkfen case CMD_DELETE:
3327e3320f40Smarkfen puts_tr("delete - Delete an SA");
3328e3320f40Smarkfen break;
332938d95a78Smarkfen case CMD_DELETE_PAIR:
333038d95a78Smarkfen puts_tr("delete-pair - Delete a pair of SA's");
333138d95a78Smarkfen break;
3332e3320f40Smarkfen case CMD_GET:
3333e3320f40Smarkfen puts_tr("get - Display an SA");
3334e3320f40Smarkfen break;
3335e3320f40Smarkfen case CMD_FLUSH:
3336e3320f40Smarkfen puts_tr("flush - Delete all SAs");
3337bfe6f8f5SVladimir Kotal puts_tr("");
3338bfe6f8f5SVladimir Kotal puts_tr("Optional arguments:");
3339bfe6f8f5SVladimir Kotal puts_tr("all delete all SAs");
3340bfe6f8f5SVladimir Kotal puts_tr("esp delete just ESP SAs");
3341bfe6f8f5SVladimir Kotal puts_tr("ah delete just AH SAs");
3342bfe6f8f5SVladimir Kotal puts_tr("<number> delete just SAs with type given by number");
3343bfe6f8f5SVladimir Kotal puts_tr("");
3344e3320f40Smarkfen break;
3345e3320f40Smarkfen case CMD_DUMP:
3346e3320f40Smarkfen puts_tr("dump - Display all SAs");
3347bfe6f8f5SVladimir Kotal puts_tr("");
3348bfe6f8f5SVladimir Kotal puts_tr("Optional arguments:");
3349bfe6f8f5SVladimir Kotal puts_tr("all display all SAs");
3350bfe6f8f5SVladimir Kotal puts_tr("esp display just ESP SAs");
3351bfe6f8f5SVladimir Kotal puts_tr("ah display just AH SAs");
3352bfe6f8f5SVladimir Kotal puts_tr("<number> display just SAs with type "
3353bfe6f8f5SVladimir Kotal "given by number");
3354bfe6f8f5SVladimir Kotal puts_tr("");
3355e3320f40Smarkfen break;
3356e3320f40Smarkfen case CMD_MONITOR:
3357e3320f40Smarkfen puts_tr("monitor - Monitor all PF_KEY reply messages.");
3358e3320f40Smarkfen break;
3359e3320f40Smarkfen case CMD_PMONITOR:
3360e3320f40Smarkfen puts_tr(
3361e3320f40Smarkfen "pmonitor, passive_monitor - Monitor PF_KEY messages that");
3362e3320f40Smarkfen puts_tr(
3363e3320f40Smarkfen " reply to all PF_KEY sockets.");
3364e3320f40Smarkfen break;
3365e3320f40Smarkfen
3366e3320f40Smarkfen case CMD_QUIT:
3367e3320f40Smarkfen puts_tr("quit, exit - Exit the program");
3368e3320f40Smarkfen break;
3369e3320f40Smarkfen case CMD_SAVE:
3370e3320f40Smarkfen puts_tr("save - Saves all SAs to a file");
3371e3320f40Smarkfen break;
3372e3320f40Smarkfen case CMD_HELP:
3373e3320f40Smarkfen puts_tr("help - Display list of commands");
3374e3320f40Smarkfen puts_tr("help <cmd> - Display help for command");
3375e3320f40Smarkfen puts_tr("help attr - Display possible SA attributes");
3376e3320f40Smarkfen break;
3377e3320f40Smarkfen default:
3378e3320f40Smarkfen (void) printf(gettext("%s: Unknown command\n"), cmds);
3379e3320f40Smarkfen break;
3380e3320f40Smarkfen }
3381e3320f40Smarkfen }
3382e3320f40Smarkfen
3383e3320f40Smarkfen
3384e3320f40Smarkfen static void
dohelp(char * cmds)3385e3320f40Smarkfen dohelp(char *cmds)
3386e3320f40Smarkfen {
3387e3320f40Smarkfen if (cmds != NULL) {
3388e3320f40Smarkfen dohelpcmd(cmds);
3389e3320f40Smarkfen return;
3390e3320f40Smarkfen }
3391e3320f40Smarkfen puts_tr("Commands");
3392e3320f40Smarkfen puts_tr("--------");
3393e3320f40Smarkfen puts_tr("?, help - Display this list");
3394e3320f40Smarkfen puts_tr("help <cmd> - Display help for command");
3395e3320f40Smarkfen puts_tr("help attr - Display possible SA attributes");
3396e3320f40Smarkfen puts_tr("quit, exit - Exit the program");
3397e3320f40Smarkfen puts_tr("monitor - Monitor all PF_KEY reply messages.");
3398e3320f40Smarkfen puts_tr("pmonitor, passive_monitor - Monitor PF_KEY messages that");
3399e3320f40Smarkfen puts_tr(" reply to all PF_KEY sockets.");
3400e3320f40Smarkfen puts_tr("");
3401e3320f40Smarkfen puts_tr("The following commands are of the form:");
3402e3320f40Smarkfen puts_tr(" <command> {SA type} {attribute value}*");
3403e3320f40Smarkfen puts_tr("");
3404e3320f40Smarkfen puts_tr("add (interactive only) - Add a new security association (SA)");
3405e3320f40Smarkfen puts_tr("update (interactive only) - Update an existing SA");
340638d95a78Smarkfen puts_tr("update-pair (interactive only) - Update an existing SA pair");
3407e3320f40Smarkfen puts_tr("delete - Delete an SA");
340838d95a78Smarkfen puts_tr("delete-pair - Delete an SA pair");
3409e3320f40Smarkfen puts_tr("get - Display an SA");
3410e3320f40Smarkfen puts_tr("flush - Delete all SAs");
3411e3320f40Smarkfen puts_tr("dump - Display all SAs");
3412e3320f40Smarkfen puts_tr("save - Saves all SAs to a file");
3413e3320f40Smarkfen }
3414e3320f40Smarkfen
3415e3320f40Smarkfen /*
3416e3320f40Smarkfen * "Parse" a command line from argv.
3417e3320f40Smarkfen */
3418e3320f40Smarkfen static void
parseit(int argc,char * argv[],char * ebuf,boolean_t read_cmdfile)341925e435e0Spwernau parseit(int argc, char *argv[], char *ebuf, boolean_t read_cmdfile)
3420e3320f40Smarkfen {
3421e3320f40Smarkfen int cmd, satype;
3422e3320f40Smarkfen char *ep = NULL;
3423e3320f40Smarkfen
3424e3320f40Smarkfen if (argc == 0)
3425e3320f40Smarkfen return;
3426e3320f40Smarkfen cmd = parsecmd(*argv++);
3427e3320f40Smarkfen
342825e435e0Spwernau /*
342925e435e0Spwernau * Some commands loop forever and should only be run from the command
343025e435e0Spwernau * line, they should never be run from a command file as this may
343125e435e0Spwernau * be used at boot time.
343225e435e0Spwernau */
3433e3320f40Smarkfen switch (cmd) {
3434e3320f40Smarkfen case CMD_HELP:
343525e435e0Spwernau if (read_cmdfile)
343625e435e0Spwernau ERROR(ep, ebuf, gettext("Help not appropriate in "
343725e435e0Spwernau "config file."));
343825e435e0Spwernau else
3439e3320f40Smarkfen dohelp(*argv);
3440e3320f40Smarkfen return;
3441e3320f40Smarkfen case CMD_MONITOR:
344225e435e0Spwernau if (read_cmdfile)
344325e435e0Spwernau ERROR(ep, ebuf, gettext("Monitor not appropriate in "
344425e435e0Spwernau "config file."));
3445bfe6f8f5SVladimir Kotal else {
3446e3320f40Smarkfen domonitor(B_FALSE);
3447bfe6f8f5SVladimir Kotal /*
3448bfe6f8f5SVladimir Kotal * Return from the function in interactive mode to
3449bfe6f8f5SVladimir Kotal * avoid error message in the next switch statement.
3450bfe6f8f5SVladimir Kotal * Also print newline to prevent prompt clobbering.
3451bfe6f8f5SVladimir Kotal * The same is done for CMD_PMONITOR.
3452bfe6f8f5SVladimir Kotal */
3453bfe6f8f5SVladimir Kotal if (interactive) {
3454bfe6f8f5SVladimir Kotal (void) printf("\n");
3455bfe6f8f5SVladimir Kotal return;
3456bfe6f8f5SVladimir Kotal }
3457bfe6f8f5SVladimir Kotal }
3458e3320f40Smarkfen break;
3459e3320f40Smarkfen case CMD_PMONITOR:
346025e435e0Spwernau if (read_cmdfile)
346125e435e0Spwernau ERROR(ep, ebuf, gettext("Monitor not appropriate in "
346225e435e0Spwernau "config file."));
3463bfe6f8f5SVladimir Kotal else {
3464e3320f40Smarkfen domonitor(B_TRUE);
3465bfe6f8f5SVladimir Kotal if (interactive) {
3466bfe6f8f5SVladimir Kotal (void) printf("\n");
3467bfe6f8f5SVladimir Kotal return;
3468bfe6f8f5SVladimir Kotal }
3469bfe6f8f5SVladimir Kotal }
3470e3320f40Smarkfen break;
3471e3320f40Smarkfen case CMD_QUIT:
3472e3320f40Smarkfen EXIT_OK(NULL);
3473e3320f40Smarkfen }
3474e3320f40Smarkfen
347525e435e0Spwernau handle_errors(ep, ebuf, B_FALSE, B_FALSE);
347625e435e0Spwernau
3477e3320f40Smarkfen satype = parsesatype(*argv, ebuf);
3478e3320f40Smarkfen
3479e3320f40Smarkfen if (satype != SADB_SATYPE_UNSPEC) {
3480e3320f40Smarkfen argv++;
3481e3320f40Smarkfen } else {
3482e3320f40Smarkfen /*
3483e3320f40Smarkfen * You must specify either "all" or a specific SA type
3484e3320f40Smarkfen * for the "save" command.
3485e3320f40Smarkfen */
3486e3320f40Smarkfen if (cmd == CMD_SAVE)
3487e3320f40Smarkfen if (*argv == NULL) {
3488e3320f40Smarkfen FATAL(ep, ebuf, gettext(
3489e3320f40Smarkfen "Must specify a specific "
3490e3320f40Smarkfen "SA type for save.\n"));
3491e3320f40Smarkfen } else {
3492e3320f40Smarkfen argv++;
3493e3320f40Smarkfen }
3494e3320f40Smarkfen }
3495e3320f40Smarkfen
3496e3320f40Smarkfen switch (cmd) {
3497e3320f40Smarkfen case CMD_FLUSH:
3498bfe6f8f5SVladimir Kotal if (argc > 2) {
3499bfe6f8f5SVladimir Kotal ERROR(ep, ebuf, gettext("Too many arguments for "
3500bfe6f8f5SVladimir Kotal "flush command"));
3501bfe6f8f5SVladimir Kotal handle_errors(ep, ebuf,
3502bfe6f8f5SVladimir Kotal interactive ? B_TRUE : B_FALSE, B_FALSE);
3503bfe6f8f5SVladimir Kotal }
350425e435e0Spwernau if (!cflag)
3505e3320f40Smarkfen doflush(satype);
350625e435e0Spwernau /*
350725e435e0Spwernau * If this was called because of an entry in a cmd file
350825e435e0Spwernau * then this action needs to be counted to prevent
350925e435e0Spwernau * do_interactive() treating this as an error.
351025e435e0Spwernau */
351125e435e0Spwernau lines_added++;
3512e3320f40Smarkfen break;
3513e3320f40Smarkfen case CMD_ADD:
3514e3320f40Smarkfen case CMD_UPDATE:
351538d95a78Smarkfen case CMD_UPDATE_PAIR:
3516e3320f40Smarkfen /*
3517e3320f40Smarkfen * NOTE: Shouldn't allow ADDs or UPDATEs with keying material
3518e3320f40Smarkfen * from the command line.
3519e3320f40Smarkfen */
3520e3320f40Smarkfen if (!interactive) {
3521e3320f40Smarkfen errx(1, gettext(
3522e3320f40Smarkfen "can't do ADD or UPDATE from the command line.\n"));
3523e3320f40Smarkfen }
3524e3320f40Smarkfen if (satype == SADB_SATYPE_UNSPEC) {
3525e3320f40Smarkfen FATAL(ep, ebuf, gettext(
3526e3320f40Smarkfen "Must specify a specific SA type."));
3527e3320f40Smarkfen /* NOTREACHED */
3528e3320f40Smarkfen }
3529e3320f40Smarkfen /* Parse for extensions, including keying material. */
3530e3320f40Smarkfen doaddup(cmd, satype, argv, ebuf);
3531e3320f40Smarkfen break;
3532e3320f40Smarkfen case CMD_DELETE:
353338d95a78Smarkfen case CMD_DELETE_PAIR:
3534e3320f40Smarkfen case CMD_GET:
3535e3320f40Smarkfen if (satype == SADB_SATYPE_UNSPEC) {
3536e3320f40Smarkfen FATAL(ep, ebuf, gettext(
3537e3320f40Smarkfen "Must specify a single SA type."));
3538e3320f40Smarkfen /* NOTREACHED */
3539e3320f40Smarkfen }
3540e3320f40Smarkfen /* Parse for bare minimum to locate an SA. */
3541e3320f40Smarkfen dodelget(cmd, satype, argv, ebuf);
3542e3320f40Smarkfen break;
3543e3320f40Smarkfen case CMD_DUMP:
354425e435e0Spwernau if (read_cmdfile)
354525e435e0Spwernau ERROR(ep, ebuf, gettext("Dump not appropriate in "
354625e435e0Spwernau "config file."));
3547bfe6f8f5SVladimir Kotal else {
3548bfe6f8f5SVladimir Kotal if (argc > 2) {
3549bfe6f8f5SVladimir Kotal ERROR(ep, ebuf, gettext("Too many arguments "
3550bfe6f8f5SVladimir Kotal "for dump command"));
3551bfe6f8f5SVladimir Kotal handle_errors(ep, ebuf,
3552bfe6f8f5SVladimir Kotal interactive ? B_TRUE : B_FALSE, B_FALSE);
3553bfe6f8f5SVladimir Kotal }
3554e3320f40Smarkfen dodump(satype, NULL);
3555bfe6f8f5SVladimir Kotal }
3556e3320f40Smarkfen break;
3557e3320f40Smarkfen case CMD_SAVE:
355825e435e0Spwernau if (read_cmdfile) {
355925e435e0Spwernau ERROR(ep, ebuf, gettext("Save not appropriate in "
356025e435e0Spwernau "config file."));
356125e435e0Spwernau } else {
3562e3320f40Smarkfen mask_signals(B_FALSE); /* Mask signals */
3563e3320f40Smarkfen dodump(satype, opensavefile(argv[0]));
3564e3320f40Smarkfen mask_signals(B_TRUE); /* Unmask signals */
356525e435e0Spwernau }
3566e3320f40Smarkfen break;
3567e3320f40Smarkfen default:
3568e3320f40Smarkfen warnx(gettext("Unknown command (%s).\n"),
3569e3320f40Smarkfen *(argv - ((satype == SADB_SATYPE_UNSPEC) ? 1 : 2)));
3570e3320f40Smarkfen usage();
3571e3320f40Smarkfen }
357225e435e0Spwernau handle_errors(ep, ebuf, B_FALSE, B_FALSE);
3573e3320f40Smarkfen }
3574e3320f40Smarkfen
3575e3320f40Smarkfen int
main(int argc,char * argv[])3576e3320f40Smarkfen main(int argc, char *argv[])
3577e3320f40Smarkfen {
3578e3320f40Smarkfen int ch;
3579e3320f40Smarkfen FILE *infile = stdin, *savefile;
3580e3320f40Smarkfen boolean_t dosave = B_FALSE, readfile = B_FALSE;
3581e3320f40Smarkfen char *configfile = NULL;
358223c73eccSpwernau struct stat sbuf;
35839c2c14abSThejaswini Singarajipura int bootflags;
3584e3320f40Smarkfen
3585e3320f40Smarkfen (void) setlocale(LC_ALL, "");
3586e3320f40Smarkfen #if !defined(TEXT_DOMAIN)
3587e3320f40Smarkfen #define TEXT_DOMAIN "SYS_TEST"
3588e3320f40Smarkfen #endif
3589e3320f40Smarkfen (void) textdomain(TEXT_DOMAIN);
3590e3320f40Smarkfen
3591e3320f40Smarkfen /*
3592e3320f40Smarkfen * Check to see if the command is being run from smf(5).
3593e3320f40Smarkfen */
3594e3320f40Smarkfen my_fmri = getenv("SMF_FMRI");
3595e3320f40Smarkfen
3596e3320f40Smarkfen openlog("ipseckey", LOG_CONS, LOG_AUTH);
3597e3320f40Smarkfen if (getuid() != 0) {
3598e3320f40Smarkfen errx(1, "Insufficient privileges to run ipseckey.");
3599e3320f40Smarkfen }
3600e3320f40Smarkfen
3601e3320f40Smarkfen /* umask me to paranoid, I only want to create files read-only */
3602e3320f40Smarkfen (void) umask((mode_t)00377);
3603e3320f40Smarkfen
3604e3320f40Smarkfen while ((ch = getopt(argc, argv, "pnvf:s:c:")) != EOF)
3605e3320f40Smarkfen switch (ch) {
3606e3320f40Smarkfen case 'p':
3607e3320f40Smarkfen pflag = B_TRUE;
3608e3320f40Smarkfen break;
3609e3320f40Smarkfen case 'n':
3610e3320f40Smarkfen nflag = B_TRUE;
3611e3320f40Smarkfen break;
3612e3320f40Smarkfen case 'v':
3613e3320f40Smarkfen vflag = B_TRUE;
3614e3320f40Smarkfen break;
3615e3320f40Smarkfen case 'c':
3616e3320f40Smarkfen cflag = B_TRUE;
3617e3320f40Smarkfen /* FALLTHRU */
3618e3320f40Smarkfen case 'f':
3619e3320f40Smarkfen if (dosave)
3620e3320f40Smarkfen usage();
3621d0115d88SMark Fenwick
3622d0115d88SMark Fenwick /*
3623d0115d88SMark Fenwick * Use stat() to check and see if the user inadvertently
3624d0115d88SMark Fenwick * passed in a bad pathname, or the name of a directory.
3625d0115d88SMark Fenwick * We should also check to see if the filename is a
3626d0115d88SMark Fenwick * pipe. We use stat() here because fopen() will block
3627d0115d88SMark Fenwick * unless the other end of the pipe is open. This would
3628d0115d88SMark Fenwick * be undesirable, especially if this is called at boot
3629d0115d88SMark Fenwick * time. If we ever need to support reading from a pipe
3630d0115d88SMark Fenwick * or special file, this should be revisited.
3631d0115d88SMark Fenwick */
3632d0115d88SMark Fenwick if (stat(optarg, &sbuf) == -1) {
3633d0115d88SMark Fenwick EXIT_BADCONFIG2("Invalid pathname: %s\n",
3634d0115d88SMark Fenwick optarg);
3635d0115d88SMark Fenwick }
3636d0115d88SMark Fenwick if (!(sbuf.st_mode & S_IFREG)) {
3637d0115d88SMark Fenwick EXIT_BADCONFIG2("%s - Not a regular file\n",
3638d0115d88SMark Fenwick optarg);
3639d0115d88SMark Fenwick }
3640e3320f40Smarkfen infile = fopen(optarg, "r");
3641e3320f40Smarkfen if (infile == NULL) {
3642e3320f40Smarkfen EXIT_BADCONFIG2("Unable to open configuration "
3643e3320f40Smarkfen "file: %s\n", optarg);
3644e3320f40Smarkfen }
364523c73eccSpwernau /*
3646d0115d88SMark Fenwick * The input file contains keying information, because
3647d0115d88SMark Fenwick * this is sensative, we should only accept data from
3648d0115d88SMark Fenwick * this file if the file is root owned and only readable
3649d0115d88SMark Fenwick * by privileged users. If the command is being run by
3650d0115d88SMark Fenwick * the administrator, issue a warning, if this is run by
3651d0115d88SMark Fenwick * smf(5) (IE: boot time) and the permissions are too
3652d0115d88SMark Fenwick * open, we will fail, the SMF service will end up in
3653d0115d88SMark Fenwick * maintenace mode. The check is made with fstat() to
3654d0115d88SMark Fenwick * eliminate any possible TOT to TOU window.
365523c73eccSpwernau */
365623c73eccSpwernau if (fstat(fileno(infile), &sbuf) == -1) {
365723c73eccSpwernau (void) fclose(infile);
365823c73eccSpwernau EXIT_BADCONFIG2("Unable to stat configuration "
365923c73eccSpwernau "file: %s\n", optarg);
366023c73eccSpwernau }
366123c73eccSpwernau if (INSECURE_PERMS(sbuf)) {
366223c73eccSpwernau if (my_fmri != NULL) {
366323c73eccSpwernau (void) fclose(infile);
366423c73eccSpwernau EXIT_BADCONFIG2("Config file "
366523c73eccSpwernau "%s has insecure permissions.",
366623c73eccSpwernau optarg);
366723c73eccSpwernau } else {
3668d0115d88SMark Fenwick (void) fprintf(stderr, gettext(
3669d0115d88SMark Fenwick "Config file %s has insecure "
3670d0115d88SMark Fenwick "permissions, will be rejected in "
3671d0115d88SMark Fenwick "permanent config.\n"), optarg);
367223c73eccSpwernau }
367323c73eccSpwernau }
3674e3320f40Smarkfen configfile = strdup(optarg);
3675e3320f40Smarkfen readfile = B_TRUE;
3676e3320f40Smarkfen break;
3677e3320f40Smarkfen case 's':
3678e3320f40Smarkfen if (readfile)
3679e3320f40Smarkfen usage();
3680e3320f40Smarkfen dosave = B_TRUE;
3681e3320f40Smarkfen savefile = opensavefile(optarg);
3682e3320f40Smarkfen break;
3683e3320f40Smarkfen default:
3684e3320f40Smarkfen usage();
3685e3320f40Smarkfen }
3686e3320f40Smarkfen
3687e3320f40Smarkfen argc -= optind;
3688e3320f40Smarkfen argv += optind;
3689e3320f40Smarkfen
3690e3320f40Smarkfen mypid = getpid();
3691e3320f40Smarkfen
3692e3320f40Smarkfen keysock = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
3693e3320f40Smarkfen
3694e3320f40Smarkfen if (keysock == -1) {
3695e3320f40Smarkfen if (errno == EPERM) {
3696e3320f40Smarkfen EXIT_BADPERM("Insufficient privileges to open "
3697e3320f40Smarkfen "PF_KEY socket.\n");
3698e3320f40Smarkfen } else {
3699e3320f40Smarkfen /* some other reason */
3700e3320f40Smarkfen EXIT_FATAL("Opening PF_KEY socket");
3701e3320f40Smarkfen }
3702e3320f40Smarkfen }
3703e3320f40Smarkfen
37049c2c14abSThejaswini Singarajipura if ((_cladm(CL_INITIALIZE, CL_GET_BOOTFLAG, &bootflags) != 0) ||
37059c2c14abSThejaswini Singarajipura (bootflags & CLUSTER_BOOTED)) {
37069c2c14abSThejaswini Singarajipura in_cluster_mode = B_TRUE;
37079c2c14abSThejaswini Singarajipura cluster_socket = socket(AF_INET, SOCK_DGRAM, 0);
37089c2c14abSThejaswini Singarajipura cli_addr.sin_family = AF_INET;
3709510c3f91SVladimir Kotal cli_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
37109c2c14abSThejaswini Singarajipura cli_addr.sin_port = htons(CLUSTER_UDP_PORT);
37119c2c14abSThejaswini Singarajipura }
37129c2c14abSThejaswini Singarajipura
3713e3320f40Smarkfen if (dosave) {
3714e3320f40Smarkfen mask_signals(B_FALSE); /* Mask signals */
3715e3320f40Smarkfen dodump(SADB_SATYPE_UNSPEC, savefile);
3716e3320f40Smarkfen mask_signals(B_TRUE); /* Unmask signals */
3717e3320f40Smarkfen EXIT_OK(NULL);
3718e3320f40Smarkfen }
3719e3320f40Smarkfen
3720e3320f40Smarkfen /*
3721e3320f40Smarkfen * When run from smf(5) flush any existing SA's first
3722e3320f40Smarkfen * otherwise you will end up in maintenance mode.
3723e3320f40Smarkfen */
3724e3320f40Smarkfen if ((my_fmri != NULL) && readfile) {
3725e3320f40Smarkfen (void) fprintf(stdout, gettext(
3726e3320f40Smarkfen "Flushing existing SA's before adding new SA's\n"));
3727e3320f40Smarkfen (void) fflush(stdout);
3728e3320f40Smarkfen doflush(SADB_SATYPE_UNSPEC);
3729e3320f40Smarkfen }
3730bfe6f8f5SVladimir Kotal if (infile != stdin || argc == 0) {
3731e3320f40Smarkfen /* Go into interactive mode here. */
3732e3320f40Smarkfen do_interactive(infile, configfile, "ipseckey> ", my_fmri,
3733bfe6f8f5SVladimir Kotal parseit, no_match);
3734e3320f40Smarkfen }
373525e435e0Spwernau parseit(argc, argv, NULL, B_FALSE);
3736e3320f40Smarkfen
3737e3320f40Smarkfen return (0);
3738e3320f40Smarkfen }
3739