17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 58810c16bSdanmcd * Common Development and Distribution License (the "License"). 68810c16bSdanmcd * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22a050d7e9Spwernau * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * NOTE:I'm trying to use "struct sadb_foo" instead of "sadb_foo_t" 287c478bd9Sstevel@tonic-gate * as a maximal PF_KEY portability test. 297c478bd9Sstevel@tonic-gate * 307c478bd9Sstevel@tonic-gate * Also, this is a deliberately single-threaded app, also for portability 317c478bd9Sstevel@tonic-gate * to systems without POSIX threads. 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include <sys/types.h> 357c478bd9Sstevel@tonic-gate #include <sys/stat.h> 367c478bd9Sstevel@tonic-gate #include <sys/socket.h> 377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 387c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 397c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h> 407c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 417c478bd9Sstevel@tonic-gate #include <netinet/in.h> 427c478bd9Sstevel@tonic-gate #include <sys/uio.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <syslog.h> 457c478bd9Sstevel@tonic-gate #include <signal.h> 467c478bd9Sstevel@tonic-gate #include <unistd.h> 477c478bd9Sstevel@tonic-gate #include <limits.h> 487c478bd9Sstevel@tonic-gate #include <stdlib.h> 497c478bd9Sstevel@tonic-gate #include <stdio.h> 50e3320f40Smarkfen #include <stdarg.h> 517c478bd9Sstevel@tonic-gate #include <netdb.h> 527c478bd9Sstevel@tonic-gate #include <pwd.h> 537c478bd9Sstevel@tonic-gate #include <errno.h> 547c478bd9Sstevel@tonic-gate #include <libintl.h> 557c478bd9Sstevel@tonic-gate #include <locale.h> 567c478bd9Sstevel@tonic-gate #include <fcntl.h> 577c478bd9Sstevel@tonic-gate #include <strings.h> 587c478bd9Sstevel@tonic-gate #include <ctype.h> 59*9c2c14abSThejaswini Singarajipura #include <sys/cladm.h> 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #include <ipsec_util.h> 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate static int keysock; 64*9c2c14abSThejaswini Singarajipura static int cluster_socket; 657c478bd9Sstevel@tonic-gate static uint32_t seq; 667c478bd9Sstevel@tonic-gate static pid_t mypid; 677c478bd9Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* Verbose? */ 68e3320f40Smarkfen static boolean_t cflag = B_FALSE; /* Check Only */ 69e3320f40Smarkfen 70e3320f40Smarkfen char *my_fmri = NULL; 71e3320f40Smarkfen FILE *debugfile = stdout; 72*9c2c14abSThejaswini Singarajipura static struct sockaddr_in cli_addr; 73*9c2c14abSThejaswini Singarajipura static boolean_t in_cluster_mode = B_FALSE; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate #define MAX_GET_SIZE 1024 76e3320f40Smarkfen /* 77ec485834Spwernau * WARN() and ERROR() do the same thing really, with ERROR() the function 78e3320f40Smarkfen * that prints the error buffer needs to be called at the end of a code block 79e3320f40Smarkfen * This will print out all accumulated errors before bailing. The WARN() 80e3320f40Smarkfen * macro calls handle_errors() in such a way that it prints the message 81e3320f40Smarkfen * then continues. 82e3320f40Smarkfen * If the FATAL() macro used call handle_errors() immediately. 83e3320f40Smarkfen */ 84e3320f40Smarkfen #define ERROR(x, y, z) x = record_error(x, y, z) 85e3320f40Smarkfen #define ERROR1(w, x, y, z) w = record_error(w, x, y, z) 86e3320f40Smarkfen #define ERROR2(v, w, x, y, z) v = record_error(v, w, x, y, z) 87e3320f40Smarkfen #define WARN(x, y, z) ERROR(x, y, z);\ 88e3320f40Smarkfen handle_errors(x, NULL, B_FALSE, B_FALSE); x = NULL 89e3320f40Smarkfen #define WARN1(w, x, y, z) ERROR1(w, x, y, z);\ 90e3320f40Smarkfen handle_errors(w, NULL, B_FALSE, B_FALSE); w = NULL 91e3320f40Smarkfen #define WARN2(v, w, x, y, z) ERROR2(v, w, x, y, z);\ 92e3320f40Smarkfen handle_errors(v, NULL, B_FALSE, B_FALSE); v = NULL 93e3320f40Smarkfen #define FATAL(x, y, z) ERROR(x, y, z);\ 94e3320f40Smarkfen handle_errors(x, y, B_TRUE, B_TRUE) 95e3320f40Smarkfen #define FATAL1(w, x, y, z) ERROR1(w, x, y, z);\ 96e3320f40Smarkfen handle_errors(w, x, B_TRUE, B_TRUE) 97e3320f40Smarkfen 987c478bd9Sstevel@tonic-gate /* Defined as a uint64_t array for alignment purposes. */ 997c478bd9Sstevel@tonic-gate static uint64_t get_buffer[MAX_GET_SIZE]; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 102e3320f40Smarkfen * Create/Grow a buffer large enough to hold error messages. If *ebuf 103e3320f40Smarkfen * is not NULL then it will contain a copy of the command line that 104e3320f40Smarkfen * triggered the error/warning, copy this into a new buffer or 105e3320f40Smarkfen * append new messages to the existing buffer. 106e3320f40Smarkfen */ 107e3320f40Smarkfen /*PRINTFLIKE1*/ 108e3320f40Smarkfen char * 109e3320f40Smarkfen record_error(char *ep, char *ebuf, char *fmt, ...) 110e3320f40Smarkfen { 111e3320f40Smarkfen char *err_ptr; 112e3320f40Smarkfen char tmp_buff[1024]; 113e3320f40Smarkfen va_list ap; 114e3320f40Smarkfen int length = 0; 115e3320f40Smarkfen err_ptr = ep; 116e3320f40Smarkfen 117e3320f40Smarkfen va_start(ap, fmt); 11872c8fd38Smarkfen length = vsnprintf(tmp_buff, sizeof (tmp_buff), fmt, ap); 119e3320f40Smarkfen va_end(ap); 120e3320f40Smarkfen 121e3320f40Smarkfen /* There is a new line character */ 122e3320f40Smarkfen length++; 12372c8fd38Smarkfen 12472c8fd38Smarkfen if (ep == NULL) { 12572c8fd38Smarkfen if (ebuf != NULL) 12672c8fd38Smarkfen length += strlen(ebuf); 12772c8fd38Smarkfen } else { 12872c8fd38Smarkfen length += strlen(ep); 12972c8fd38Smarkfen } 13072c8fd38Smarkfen 13172c8fd38Smarkfen if (err_ptr == NULL) 13272c8fd38Smarkfen err_ptr = calloc(length, sizeof (char)); 13372c8fd38Smarkfen else 13472c8fd38Smarkfen err_ptr = realloc(err_ptr, length); 13572c8fd38Smarkfen 136e3320f40Smarkfen if (err_ptr == NULL) 137e3320f40Smarkfen Bail("realloc() failure"); 13872c8fd38Smarkfen 13972c8fd38Smarkfen /* 14072c8fd38Smarkfen * If (ep == NULL) then this is the first error to record, 14172c8fd38Smarkfen * copy in the command line that triggered this error/warning. 14272c8fd38Smarkfen */ 14372c8fd38Smarkfen if (ep == NULL && ebuf != NULL) 14472c8fd38Smarkfen (void) strlcpy(err_ptr, ebuf, length); 14572c8fd38Smarkfen 14672c8fd38Smarkfen /* 14772c8fd38Smarkfen * Now the actual error. 14872c8fd38Smarkfen */ 149e3320f40Smarkfen (void) strlcat(err_ptr, tmp_buff, length); 150e3320f40Smarkfen return (err_ptr); 151e3320f40Smarkfen } 152e3320f40Smarkfen 153e3320f40Smarkfen /* 154e3320f40Smarkfen * Print usage message. 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate static void 1577c478bd9Sstevel@tonic-gate usage(void) 1587c478bd9Sstevel@tonic-gate { 1597c478bd9Sstevel@tonic-gate if (!interactive) { 1607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage:\t" 1617c478bd9Sstevel@tonic-gate "ipseckey [ -nvp ] | cmd [sa_type] [extfield value]*\n")); 1627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1637c478bd9Sstevel@tonic-gate gettext("\tipseckey [ -nvp ] -f infile\n")); 1647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1657c478bd9Sstevel@tonic-gate gettext("\tipseckey [ -nvp ] -s outfile\n")); 166e3320f40Smarkfen } 167e3320f40Smarkfen EXIT_FATAL(NULL); 168e3320f40Smarkfen } 169e3320f40Smarkfen 170e3320f40Smarkfen 171e3320f40Smarkfen /* 172e3320f40Smarkfen * Print out any errors, tidy up as required. 173e3320f40Smarkfen * error pointer ep will be free()'d 174e3320f40Smarkfen */ 175e3320f40Smarkfen void 176e3320f40Smarkfen handle_errors(char *ep, char *ebuf, boolean_t fatal, boolean_t done) 177e3320f40Smarkfen { 178e3320f40Smarkfen if (ep != NULL) { 179e3320f40Smarkfen if (my_fmri == NULL) { 180e3320f40Smarkfen /* 181e3320f40Smarkfen * For now suppress the errors when run from smf(5) 182e3320f40Smarkfen * because potentially sensitive information could 183e3320f40Smarkfen * end up in a publicly readable logfile. 184e3320f40Smarkfen */ 185e3320f40Smarkfen (void) fprintf(stdout, "%s\n", ep); 186e3320f40Smarkfen (void) fflush(stdout); 187e3320f40Smarkfen } 188e3320f40Smarkfen free(ep); 189e3320f40Smarkfen if (fatal) { 190e3320f40Smarkfen if (ebuf != NULL) { 191e3320f40Smarkfen free(ebuf); 192e3320f40Smarkfen } 193e3320f40Smarkfen /* reset command buffer */ 194e3320f40Smarkfen if (interactive) 195e3320f40Smarkfen longjmp(env, 1); 196e3320f40Smarkfen } else { 197e3320f40Smarkfen return; 198e3320f40Smarkfen } 1997c478bd9Sstevel@tonic-gate } else { 200e3320f40Smarkfen /* 201e3320f40Smarkfen * No errors, if this is the last time that this function 202e3320f40Smarkfen * is called, free(ebuf) and reset command buffer. 203e3320f40Smarkfen */ 204e3320f40Smarkfen if (done) { 205e3320f40Smarkfen if (ebuf != NULL) { 206e3320f40Smarkfen free(ebuf); 207e3320f40Smarkfen } 208e3320f40Smarkfen /* reset command buffer */ 209e3320f40Smarkfen if (interactive) 210e3320f40Smarkfen longjmp(env, 1); 211e3320f40Smarkfen } 212e3320f40Smarkfen return; 2137c478bd9Sstevel@tonic-gate } 21472c8fd38Smarkfen EXIT_FATAL(NULL); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate /* 2187c478bd9Sstevel@tonic-gate * Initialize a PF_KEY base message. 2197c478bd9Sstevel@tonic-gate */ 2207c478bd9Sstevel@tonic-gate static void 2217c478bd9Sstevel@tonic-gate msg_init(struct sadb_msg *msg, uint8_t type, uint8_t satype) 2227c478bd9Sstevel@tonic-gate { 2237c478bd9Sstevel@tonic-gate msg->sadb_msg_version = PF_KEY_V2; 2247c478bd9Sstevel@tonic-gate msg->sadb_msg_type = type; 2257c478bd9Sstevel@tonic-gate msg->sadb_msg_errno = 0; 2267c478bd9Sstevel@tonic-gate msg->sadb_msg_satype = satype; 2277c478bd9Sstevel@tonic-gate /* For starters... */ 2287c478bd9Sstevel@tonic-gate msg->sadb_msg_len = SADB_8TO64(sizeof (*msg)); 2297c478bd9Sstevel@tonic-gate msg->sadb_msg_reserved = 0; 2307c478bd9Sstevel@tonic-gate msg->sadb_msg_seq = ++seq; 2317c478bd9Sstevel@tonic-gate msg->sadb_msg_pid = mypid; 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* 2357c478bd9Sstevel@tonic-gate * parseXXX and rparseXXX commands parse input and convert them to PF_KEY 2367c478bd9Sstevel@tonic-gate * field values, or do the reverse for the purposes of saving the SA tables. 2377c478bd9Sstevel@tonic-gate * (See the save_XXX functions.) 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate #define CMD_NONE 0 2417c478bd9Sstevel@tonic-gate #define CMD_UPDATE 2 24238d95a78Smarkfen #define CMD_UPDATE_PAIR 3 24338d95a78Smarkfen #define CMD_ADD 4 24438d95a78Smarkfen #define CMD_DELETE 5 24538d95a78Smarkfen #define CMD_DELETE_PAIR 6 24638d95a78Smarkfen #define CMD_GET 7 2477c478bd9Sstevel@tonic-gate #define CMD_FLUSH 9 2487c478bd9Sstevel@tonic-gate #define CMD_DUMP 10 2497c478bd9Sstevel@tonic-gate #define CMD_MONITOR 11 2507c478bd9Sstevel@tonic-gate #define CMD_PMONITOR 12 2517c478bd9Sstevel@tonic-gate #define CMD_QUIT 13 2527c478bd9Sstevel@tonic-gate #define CMD_SAVE 14 2537c478bd9Sstevel@tonic-gate #define CMD_HELP 15 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * Parse the command. 2577c478bd9Sstevel@tonic-gate */ 2587c478bd9Sstevel@tonic-gate static int 2597c478bd9Sstevel@tonic-gate parsecmd(char *cmdstr) 2607c478bd9Sstevel@tonic-gate { 2617c478bd9Sstevel@tonic-gate static struct cmdtable { 2627c478bd9Sstevel@tonic-gate char *cmd; 2637c478bd9Sstevel@tonic-gate int token; 2647c478bd9Sstevel@tonic-gate } table[] = { 2657c478bd9Sstevel@tonic-gate /* 2667c478bd9Sstevel@tonic-gate * Q: Do we want to do GETSPI? 2677c478bd9Sstevel@tonic-gate * A: No, it's for automated key mgmt. only. Either that, 2687c478bd9Sstevel@tonic-gate * or it isn't relevant until we support non IPsec SA types. 2697c478bd9Sstevel@tonic-gate */ 2707c478bd9Sstevel@tonic-gate {"update", CMD_UPDATE}, 27138d95a78Smarkfen {"update-pair", CMD_UPDATE_PAIR}, 2727c478bd9Sstevel@tonic-gate {"add", CMD_ADD}, 2737c478bd9Sstevel@tonic-gate {"delete", CMD_DELETE}, 27438d95a78Smarkfen {"delete-pair", CMD_DELETE_PAIR}, 2757c478bd9Sstevel@tonic-gate {"get", CMD_GET}, 2767c478bd9Sstevel@tonic-gate /* 2777c478bd9Sstevel@tonic-gate * Q: And ACQUIRE and REGISTER and EXPIRE? 2787c478bd9Sstevel@tonic-gate * A: not until we support non IPsec SA types. 2797c478bd9Sstevel@tonic-gate */ 2807c478bd9Sstevel@tonic-gate {"flush", CMD_FLUSH}, 2817c478bd9Sstevel@tonic-gate {"dump", CMD_DUMP}, 2827c478bd9Sstevel@tonic-gate {"monitor", CMD_MONITOR}, 2837c478bd9Sstevel@tonic-gate {"passive_monitor", CMD_PMONITOR}, 2847c478bd9Sstevel@tonic-gate {"pmonitor", CMD_PMONITOR}, 2857c478bd9Sstevel@tonic-gate {"quit", CMD_QUIT}, 2867c478bd9Sstevel@tonic-gate {"exit", CMD_QUIT}, 2877c478bd9Sstevel@tonic-gate {"save", CMD_SAVE}, 2887c478bd9Sstevel@tonic-gate {"help", CMD_HELP}, 2897c478bd9Sstevel@tonic-gate {"?", CMD_HELP}, 2907c478bd9Sstevel@tonic-gate {NULL, CMD_NONE} 2917c478bd9Sstevel@tonic-gate }; 2927c478bd9Sstevel@tonic-gate struct cmdtable *ct = table; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate while (ct->cmd != NULL && strcmp(ct->cmd, cmdstr) != 0) 2957c478bd9Sstevel@tonic-gate ct++; 2967c478bd9Sstevel@tonic-gate return (ct->token); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate /* 3007c478bd9Sstevel@tonic-gate * Convert a number from a command line. I picked "u_longlong_t" for the 3017c478bd9Sstevel@tonic-gate * number because we need the largest number available. Also, the strto<num> 3027c478bd9Sstevel@tonic-gate * calls don't deal in units of uintNN_t. 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate static u_longlong_t 305e3320f40Smarkfen parsenum(char *num, boolean_t bail, char *ebuf) 3067c478bd9Sstevel@tonic-gate { 307e3320f40Smarkfen u_longlong_t rc = 0; 3087c478bd9Sstevel@tonic-gate char *end = NULL; 309e3320f40Smarkfen char *ep = NULL; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate if (num == NULL) { 312e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line," 313e3320f40Smarkfen " was expecting a number.\n")); 314e3320f40Smarkfen /* NOTREACHED */ 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate errno = 0; 3187c478bd9Sstevel@tonic-gate rc = strtoull(num, &end, 0); 3197c478bd9Sstevel@tonic-gate if (errno != 0 || end == num || *end != '\0') { 3207c478bd9Sstevel@tonic-gate if (bail) { 32138d95a78Smarkfen FATAL1(ep, ebuf, gettext( 322e3320f40Smarkfen "Expecting a number, not \"%s\"!\n"), num); 3237c478bd9Sstevel@tonic-gate } else { 3247c478bd9Sstevel@tonic-gate /* 3257c478bd9Sstevel@tonic-gate * -1, while not optimal, is sufficiently out of range 3267c478bd9Sstevel@tonic-gate * for most of this function's applications when 3277c478bd9Sstevel@tonic-gate * we don't just bail. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate return ((u_longlong_t)-1); 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate } 332e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE); 3337c478bd9Sstevel@tonic-gate return (rc); 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* 3377c478bd9Sstevel@tonic-gate * Parse and reverse parse a specific SA type (AH, ESP, etc.). 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate static struct typetable { 3407c478bd9Sstevel@tonic-gate char *type; 3417c478bd9Sstevel@tonic-gate int token; 3427c478bd9Sstevel@tonic-gate } type_table[] = { 3437c478bd9Sstevel@tonic-gate {"all", SADB_SATYPE_UNSPEC}, 3447c478bd9Sstevel@tonic-gate {"ah", SADB_SATYPE_AH}, 3457c478bd9Sstevel@tonic-gate {"esp", SADB_SATYPE_ESP}, 3467c478bd9Sstevel@tonic-gate /* PF_KEY NOTE: More to come if net/pfkeyv2.h gets updated. */ 3477c478bd9Sstevel@tonic-gate {NULL, 0} /* Token value is irrelevant for this entry. */ 3487c478bd9Sstevel@tonic-gate }; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate static int 352e3320f40Smarkfen parsesatype(char *type, char *ebuf) 3537c478bd9Sstevel@tonic-gate { 3547c478bd9Sstevel@tonic-gate struct typetable *tt = type_table; 355e3320f40Smarkfen char *ep = NULL; 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate if (type == NULL) 3587c478bd9Sstevel@tonic-gate return (SADB_SATYPE_UNSPEC); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate while (tt->type != NULL && strcasecmp(tt->type, type) != 0) 3617c478bd9Sstevel@tonic-gate tt++; 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate /* 3647c478bd9Sstevel@tonic-gate * New SA types (including ones keysock maintains for user-land 3657c478bd9Sstevel@tonic-gate * protocols) may be added, so parse a numeric value if possible. 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate if (tt->type == NULL) { 368e3320f40Smarkfen tt->token = (int)parsenum(type, B_FALSE, ebuf); 3697c478bd9Sstevel@tonic-gate if (tt->token == -1) { 370e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 371e3320f40Smarkfen "Unknown SA type (%s).\n"), type); 372e3320f40Smarkfen tt->token = SADB_SATYPE_UNSPEC; 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate } 375e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE); 3767c478bd9Sstevel@tonic-gate return (tt->token); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate #define NEXTEOF 0 3807c478bd9Sstevel@tonic-gate #define NEXTNONE 1 3817c478bd9Sstevel@tonic-gate #define NEXTNUM 2 3827c478bd9Sstevel@tonic-gate #define NEXTSTR 3 3837c478bd9Sstevel@tonic-gate #define NEXTNUMSTR 4 3847c478bd9Sstevel@tonic-gate #define NEXTADDR 5 3857c478bd9Sstevel@tonic-gate #define NEXTHEX 6 3867c478bd9Sstevel@tonic-gate #define NEXTIDENT 7 3877c478bd9Sstevel@tonic-gate #define NEXTADDR4 8 3887c478bd9Sstevel@tonic-gate #define NEXTADDR6 9 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate #define TOK_EOF 0 3917c478bd9Sstevel@tonic-gate #define TOK_UNKNOWN 1 3927c478bd9Sstevel@tonic-gate #define TOK_SPI 2 3937c478bd9Sstevel@tonic-gate #define TOK_REPLAY 3 3947c478bd9Sstevel@tonic-gate #define TOK_STATE 4 3957c478bd9Sstevel@tonic-gate #define TOK_AUTHALG 5 3967c478bd9Sstevel@tonic-gate #define TOK_ENCRALG 6 3977c478bd9Sstevel@tonic-gate #define TOK_FLAGS 7 3987c478bd9Sstevel@tonic-gate #define TOK_SOFT_ALLOC 8 3997c478bd9Sstevel@tonic-gate #define TOK_SOFT_BYTES 9 4007c478bd9Sstevel@tonic-gate #define TOK_SOFT_ADDTIME 10 4017c478bd9Sstevel@tonic-gate #define TOK_SOFT_USETIME 11 4027c478bd9Sstevel@tonic-gate #define TOK_HARD_ALLOC 12 4037c478bd9Sstevel@tonic-gate #define TOK_HARD_BYTES 13 4047c478bd9Sstevel@tonic-gate #define TOK_HARD_ADDTIME 14 4057c478bd9Sstevel@tonic-gate #define TOK_HARD_USETIME 15 4067c478bd9Sstevel@tonic-gate #define TOK_CURRENT_ALLOC 16 4077c478bd9Sstevel@tonic-gate #define TOK_CURRENT_BYTES 17 4087c478bd9Sstevel@tonic-gate #define TOK_CURRENT_ADDTIME 18 4097c478bd9Sstevel@tonic-gate #define TOK_CURRENT_USETIME 19 4107c478bd9Sstevel@tonic-gate #define TOK_SRCADDR 20 4117c478bd9Sstevel@tonic-gate #define TOK_DSTADDR 21 4127c478bd9Sstevel@tonic-gate #define TOK_PROXYADDR 22 4137c478bd9Sstevel@tonic-gate #define TOK_AUTHKEY 23 4147c478bd9Sstevel@tonic-gate #define TOK_ENCRKEY 24 4157c478bd9Sstevel@tonic-gate #define TOK_SRCIDTYPE 25 4167c478bd9Sstevel@tonic-gate #define TOK_DSTIDTYPE 26 4177c478bd9Sstevel@tonic-gate #define TOK_DPD 27 4187c478bd9Sstevel@tonic-gate #define TOK_SENS_LEVEL 28 4197c478bd9Sstevel@tonic-gate #define TOK_SENS_MAP 29 4207c478bd9Sstevel@tonic-gate #define TOK_INTEG_LEVEL 30 4217c478bd9Sstevel@tonic-gate #define TOK_INTEG_MAP 31 4227c478bd9Sstevel@tonic-gate #define TOK_SRCADDR6 32 4237c478bd9Sstevel@tonic-gate #define TOK_DSTADDR6 33 4247c478bd9Sstevel@tonic-gate #define TOK_PROXYADDR6 34 4257c478bd9Sstevel@tonic-gate #define TOK_SRCPORT 35 4267c478bd9Sstevel@tonic-gate #define TOK_DSTPORT 36 4277c478bd9Sstevel@tonic-gate #define TOK_PROTO 37 4287c478bd9Sstevel@tonic-gate #define TOK_ENCAP 38 4297c478bd9Sstevel@tonic-gate #define TOK_NATLOC 39 4307c478bd9Sstevel@tonic-gate #define TOK_NATREM 40 4317c478bd9Sstevel@tonic-gate #define TOK_NATLPORT 41 4327c478bd9Sstevel@tonic-gate #define TOK_NATRPORT 42 4338810c16bSdanmcd #define TOK_IPROTO 43 4348810c16bSdanmcd #define TOK_IDSTADDR 44 4358810c16bSdanmcd #define TOK_IDSTADDR6 45 4368810c16bSdanmcd #define TOK_ISRCPORT 46 4378810c16bSdanmcd #define TOK_IDSTPORT 47 43838d95a78Smarkfen #define TOK_PAIR_SPI 48 43938d95a78Smarkfen #define TOK_FLAG_INBOUND 49 44038d95a78Smarkfen #define TOK_FLAG_OUTBOUND 50 441*9c2c14abSThejaswini Singarajipura #define TOK_REPLAY_VALUE 51 442*9c2c14abSThejaswini Singarajipura #define TOK_IDLE_ADDTIME 52 443*9c2c14abSThejaswini Singarajipura #define TOK_IDLE_USETIME 53 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate static struct toktable { 4467c478bd9Sstevel@tonic-gate char *string; 4477c478bd9Sstevel@tonic-gate int token; 4487c478bd9Sstevel@tonic-gate int next; 4497c478bd9Sstevel@tonic-gate } tokens[] = { 4507c478bd9Sstevel@tonic-gate /* "String", token value, next arg is */ 4517c478bd9Sstevel@tonic-gate {"spi", TOK_SPI, NEXTNUM}, 45238d95a78Smarkfen {"pair-spi", TOK_PAIR_SPI, NEXTNUM}, 4537c478bd9Sstevel@tonic-gate {"replay", TOK_REPLAY, NEXTNUM}, 4547c478bd9Sstevel@tonic-gate {"state", TOK_STATE, NEXTNUMSTR}, 4557c478bd9Sstevel@tonic-gate {"auth_alg", TOK_AUTHALG, NEXTNUMSTR}, 4567c478bd9Sstevel@tonic-gate {"authalg", TOK_AUTHALG, NEXTNUMSTR}, 4577c478bd9Sstevel@tonic-gate {"encr_alg", TOK_ENCRALG, NEXTNUMSTR}, 4587c478bd9Sstevel@tonic-gate {"encralg", TOK_ENCRALG, NEXTNUMSTR}, 4597c478bd9Sstevel@tonic-gate {"flags", TOK_FLAGS, NEXTNUM}, 4607c478bd9Sstevel@tonic-gate {"soft_alloc", TOK_SOFT_ALLOC, NEXTNUM}, 4617c478bd9Sstevel@tonic-gate {"soft_bytes", TOK_SOFT_BYTES, NEXTNUM}, 4627c478bd9Sstevel@tonic-gate {"soft_addtime", TOK_SOFT_ADDTIME, NEXTNUM}, 4637c478bd9Sstevel@tonic-gate {"soft_usetime", TOK_SOFT_USETIME, NEXTNUM}, 4647c478bd9Sstevel@tonic-gate {"hard_alloc", TOK_HARD_ALLOC, NEXTNUM}, 4657c478bd9Sstevel@tonic-gate {"hard_bytes", TOK_HARD_BYTES, NEXTNUM}, 4667c478bd9Sstevel@tonic-gate {"hard_addtime", TOK_HARD_ADDTIME, NEXTNUM}, 4677c478bd9Sstevel@tonic-gate {"hard_usetime", TOK_HARD_USETIME, NEXTNUM}, 4687c478bd9Sstevel@tonic-gate {"current_alloc", TOK_CURRENT_ALLOC, NEXTNUM}, 4697c478bd9Sstevel@tonic-gate {"current_bytes", TOK_CURRENT_BYTES, NEXTNUM}, 4707c478bd9Sstevel@tonic-gate {"current_addtime", TOK_CURRENT_ADDTIME, NEXTNUM}, 4717c478bd9Sstevel@tonic-gate {"current_usetime", TOK_CURRENT_USETIME, NEXTNUM}, 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate {"saddr", TOK_SRCADDR, NEXTADDR}, 4747c478bd9Sstevel@tonic-gate {"srcaddr", TOK_SRCADDR, NEXTADDR}, 4757c478bd9Sstevel@tonic-gate {"src", TOK_SRCADDR, NEXTADDR}, 4767c478bd9Sstevel@tonic-gate {"daddr", TOK_DSTADDR, NEXTADDR}, 4777c478bd9Sstevel@tonic-gate {"dstaddr", TOK_DSTADDR, NEXTADDR}, 4787c478bd9Sstevel@tonic-gate {"dst", TOK_DSTADDR, NEXTADDR}, 4797c478bd9Sstevel@tonic-gate {"proxyaddr", TOK_PROXYADDR, NEXTADDR}, 4807c478bd9Sstevel@tonic-gate {"proxy", TOK_PROXYADDR, NEXTADDR}, 4818810c16bSdanmcd {"innersrc", TOK_PROXYADDR, NEXTADDR}, 4828810c16bSdanmcd {"isrc", TOK_PROXYADDR, NEXTADDR}, 4838810c16bSdanmcd {"innerdst", TOK_IDSTADDR, NEXTADDR}, 4848810c16bSdanmcd {"idst", TOK_IDSTADDR, NEXTADDR}, 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate {"sport", TOK_SRCPORT, NEXTNUM}, 4877c478bd9Sstevel@tonic-gate {"dport", TOK_DSTPORT, NEXTNUM}, 4888810c16bSdanmcd {"innersport", TOK_ISRCPORT, NEXTNUM}, 4898810c16bSdanmcd {"isport", TOK_ISRCPORT, NEXTNUM}, 4908810c16bSdanmcd {"innerdport", TOK_IDSTPORT, NEXTNUM}, 4918810c16bSdanmcd {"idport", TOK_IDSTPORT, NEXTNUM}, 4927c478bd9Sstevel@tonic-gate {"proto", TOK_PROTO, NEXTNUM}, 493eeda67c6Sjojemann {"ulp", TOK_PROTO, NEXTNUM}, 4948810c16bSdanmcd {"iproto", TOK_IPROTO, NEXTNUM}, 4958810c16bSdanmcd {"iulp", TOK_IPROTO, NEXTNUM}, 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate {"saddr6", TOK_SRCADDR6, NEXTADDR}, 4987c478bd9Sstevel@tonic-gate {"srcaddr6", TOK_SRCADDR6, NEXTADDR}, 4997c478bd9Sstevel@tonic-gate {"src6", TOK_SRCADDR6, NEXTADDR}, 5007c478bd9Sstevel@tonic-gate {"daddr6", TOK_DSTADDR6, NEXTADDR}, 5017c478bd9Sstevel@tonic-gate {"dstaddr6", TOK_DSTADDR6, NEXTADDR}, 5027c478bd9Sstevel@tonic-gate {"dst6", TOK_DSTADDR6, NEXTADDR}, 5037c478bd9Sstevel@tonic-gate {"proxyaddr6", TOK_PROXYADDR6, NEXTADDR}, 5047c478bd9Sstevel@tonic-gate {"proxy6", TOK_PROXYADDR6, NEXTADDR}, 5058810c16bSdanmcd {"innersrc6", TOK_PROXYADDR6, NEXTADDR}, 5068810c16bSdanmcd {"isrc6", TOK_PROXYADDR6, NEXTADDR}, 5078810c16bSdanmcd {"innerdst6", TOK_IDSTADDR6, NEXTADDR}, 5088810c16bSdanmcd {"idst6", TOK_IDSTADDR6, NEXTADDR}, 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate {"authkey", TOK_AUTHKEY, NEXTHEX}, 5117c478bd9Sstevel@tonic-gate {"encrkey", TOK_ENCRKEY, NEXTHEX}, 5127c478bd9Sstevel@tonic-gate {"srcidtype", TOK_SRCIDTYPE, NEXTIDENT}, 5137c478bd9Sstevel@tonic-gate {"dstidtype", TOK_DSTIDTYPE, NEXTIDENT}, 5147c478bd9Sstevel@tonic-gate {"dpd", TOK_DPD, NEXTNUM}, 5157c478bd9Sstevel@tonic-gate {"sens_level", TOK_SENS_LEVEL, NEXTNUM}, 5167c478bd9Sstevel@tonic-gate {"sens_map", TOK_SENS_MAP, NEXTHEX}, 5177c478bd9Sstevel@tonic-gate {"integ_level", TOK_INTEG_LEVEL, NEXTNUM}, 5187c478bd9Sstevel@tonic-gate {"integ_map", TOK_INTEG_MAP, NEXTHEX}, 5197c478bd9Sstevel@tonic-gate {"nat_loc", TOK_NATLOC, NEXTADDR}, 5207c478bd9Sstevel@tonic-gate {"nat_rem", TOK_NATREM, NEXTADDR}, 5217c478bd9Sstevel@tonic-gate {"nat_lport", TOK_NATLPORT, NEXTNUM}, 5227c478bd9Sstevel@tonic-gate {"nat_rport", TOK_NATRPORT, NEXTNUM}, 5237c478bd9Sstevel@tonic-gate {"encap", TOK_ENCAP, NEXTNUMSTR}, 52438d95a78Smarkfen 52538d95a78Smarkfen {"outbound", TOK_FLAG_OUTBOUND, NULL}, 52638d95a78Smarkfen {"inbound", TOK_FLAG_INBOUND, NULL}, 527*9c2c14abSThejaswini Singarajipura 528*9c2c14abSThejaswini Singarajipura {"replay_value", TOK_REPLAY_VALUE, NEXTNUM}, 529*9c2c14abSThejaswini Singarajipura {"idle_addtime", TOK_IDLE_ADDTIME, NEXTNUM}, 530*9c2c14abSThejaswini Singarajipura {"idle_usetime", TOK_IDLE_USETIME, NEXTNUM}, 5317c478bd9Sstevel@tonic-gate {NULL, TOK_UNKNOWN, NEXTEOF} 5327c478bd9Sstevel@tonic-gate }; 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate /* 5357c478bd9Sstevel@tonic-gate * Q: Do I need stuff for proposals, combinations, supported algorithms, 5367c478bd9Sstevel@tonic-gate * or SPI ranges? 5377c478bd9Sstevel@tonic-gate * 5387c478bd9Sstevel@tonic-gate * A: Probably not, but you never know. 5397c478bd9Sstevel@tonic-gate * 5407c478bd9Sstevel@tonic-gate * Parse out extension header type values. 5417c478bd9Sstevel@tonic-gate */ 5427c478bd9Sstevel@tonic-gate static int 5437c478bd9Sstevel@tonic-gate parseextval(char *value, int *next) 5447c478bd9Sstevel@tonic-gate { 5457c478bd9Sstevel@tonic-gate struct toktable *tp; 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate if (value == NULL) 5487c478bd9Sstevel@tonic-gate return (TOK_EOF); 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate for (tp = tokens; tp->string != NULL; tp++) 5517c478bd9Sstevel@tonic-gate if (strcmp(value, tp->string) == 0) 5527c478bd9Sstevel@tonic-gate break; 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate /* 5557c478bd9Sstevel@tonic-gate * Since the OS controls what extensions are available, we don't have 5567c478bd9Sstevel@tonic-gate * to parse numeric values here. 5577c478bd9Sstevel@tonic-gate */ 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate *next = tp->next; 5607c478bd9Sstevel@tonic-gate return (tp->token); 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate /* 5647c478bd9Sstevel@tonic-gate * Parse possible state values. 5657c478bd9Sstevel@tonic-gate */ 5667c478bd9Sstevel@tonic-gate static uint8_t 567e3320f40Smarkfen parsestate(char *state, char *ebuf) 5687c478bd9Sstevel@tonic-gate { 5697c478bd9Sstevel@tonic-gate struct states { 5707c478bd9Sstevel@tonic-gate char *state; 5717c478bd9Sstevel@tonic-gate uint8_t retval; 5727c478bd9Sstevel@tonic-gate } states[] = { 5737c478bd9Sstevel@tonic-gate {"larval", SADB_SASTATE_LARVAL}, 5747c478bd9Sstevel@tonic-gate {"mature", SADB_SASTATE_MATURE}, 5757c478bd9Sstevel@tonic-gate {"dying", SADB_SASTATE_DYING}, 5767c478bd9Sstevel@tonic-gate {"dead", SADB_SASTATE_DEAD}, 5777c478bd9Sstevel@tonic-gate {NULL, 0} 5787c478bd9Sstevel@tonic-gate }; 5797c478bd9Sstevel@tonic-gate struct states *sp; 580e3320f40Smarkfen char *ep = NULL; 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate if (state == NULL) { 583e3320f40Smarkfen FATAL(ep, ebuf, "Unexpected end of command line " 584e3320f40Smarkfen "was expecting a state.\n"); 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate for (sp = states; sp->state != NULL; sp++) { 5887c478bd9Sstevel@tonic-gate if (strcmp(sp->state, state) == 0) 5897c478bd9Sstevel@tonic-gate return (sp->retval); 5907c478bd9Sstevel@tonic-gate } 591e3320f40Smarkfen ERROR1(ep, ebuf, gettext("Unknown state type \"%s\"\n"), state); 592e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE); 593eeda67c6Sjojemann return (0); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate /* 5977c478bd9Sstevel@tonic-gate * Return the numerical algorithm identifier corresponding to the specified 5987c478bd9Sstevel@tonic-gate * algorithm name. 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate static uint8_t 601e3320f40Smarkfen parsealg(char *alg, int proto_num, char *ebuf) 6027c478bd9Sstevel@tonic-gate { 6037c478bd9Sstevel@tonic-gate u_longlong_t invalue; 6047c478bd9Sstevel@tonic-gate struct ipsecalgent *algent; 605e3320f40Smarkfen char *ep = NULL; 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate if (alg == NULL) { 608e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line, " 609e3320f40Smarkfen "was expecting an algorithm name.\n")); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate algent = getipsecalgbyname(alg, proto_num, NULL); 6137c478bd9Sstevel@tonic-gate if (algent != NULL) { 6147c478bd9Sstevel@tonic-gate uint8_t alg_num; 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate alg_num = algent->a_alg_num; 6177c478bd9Sstevel@tonic-gate freeipsecalgent(algent); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate return (alg_num); 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate /* 6237c478bd9Sstevel@tonic-gate * Since algorithms can be loaded during kernel run-time, check for 6247c478bd9Sstevel@tonic-gate * numeric algorithm values too. PF_KEY can catch bad ones with EINVAL. 6257c478bd9Sstevel@tonic-gate */ 626e3320f40Smarkfen invalue = parsenum(alg, B_FALSE, ebuf); 6277c478bd9Sstevel@tonic-gate if (invalue != (u_longlong_t)-1 && 6287c478bd9Sstevel@tonic-gate (u_longlong_t)(invalue & (u_longlong_t)0xff) == invalue) 6297c478bd9Sstevel@tonic-gate return ((uint8_t)invalue); 6307c478bd9Sstevel@tonic-gate 631e3320f40Smarkfen if (proto_num == IPSEC_PROTO_ESP) { 632e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 633e3320f40Smarkfen "Unknown encryption algorithm type \"%s\"\n"), alg); 634e3320f40Smarkfen } else { 635e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 636e3320f40Smarkfen "Unknown authentication algorithm type \"%s\"\n"), alg); 637e3320f40Smarkfen } 638e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE); 639eeda67c6Sjojemann return (0); 6407c478bd9Sstevel@tonic-gate } 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate /* 6437c478bd9Sstevel@tonic-gate * Parse and reverse parse out a source/destination ID type. 6447c478bd9Sstevel@tonic-gate */ 6457c478bd9Sstevel@tonic-gate static struct idtypes { 6467c478bd9Sstevel@tonic-gate char *idtype; 6477c478bd9Sstevel@tonic-gate uint8_t retval; 6487c478bd9Sstevel@tonic-gate } idtypes[] = { 6497c478bd9Sstevel@tonic-gate {"prefix", SADB_IDENTTYPE_PREFIX}, 6507c478bd9Sstevel@tonic-gate {"fqdn", SADB_IDENTTYPE_FQDN}, 6517c478bd9Sstevel@tonic-gate {"domain", SADB_IDENTTYPE_FQDN}, 6527c478bd9Sstevel@tonic-gate {"domainname", SADB_IDENTTYPE_FQDN}, 6537c478bd9Sstevel@tonic-gate {"user_fqdn", SADB_IDENTTYPE_USER_FQDN}, 6547c478bd9Sstevel@tonic-gate {"mailbox", SADB_IDENTTYPE_USER_FQDN}, 6557c478bd9Sstevel@tonic-gate {"der_dn", SADB_X_IDENTTYPE_DN}, 6567c478bd9Sstevel@tonic-gate {"der_gn", SADB_X_IDENTTYPE_GN}, 6577c478bd9Sstevel@tonic-gate {NULL, 0} 6587c478bd9Sstevel@tonic-gate }; 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate static uint16_t 661e3320f40Smarkfen parseidtype(char *type, char *ebuf) 6627c478bd9Sstevel@tonic-gate { 6637c478bd9Sstevel@tonic-gate struct idtypes *idp; 6647c478bd9Sstevel@tonic-gate u_longlong_t invalue; 665e3320f40Smarkfen char *ep = NULL; 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate if (type == NULL) { 6687c478bd9Sstevel@tonic-gate /* Shouldn't reach here, see callers for why. */ 669e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line, " 670e3320f40Smarkfen "was expecting a type.\n")); 6717c478bd9Sstevel@tonic-gate } 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate for (idp = idtypes; idp->idtype != NULL; idp++) { 6747c478bd9Sstevel@tonic-gate if (strcasecmp(idp->idtype, type) == 0) 6757c478bd9Sstevel@tonic-gate return (idp->retval); 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate /* 6787c478bd9Sstevel@tonic-gate * Since identity types are almost arbitrary, check for numeric 6797c478bd9Sstevel@tonic-gate * algorithm values too. PF_KEY can catch bad ones with EINVAL. 6807c478bd9Sstevel@tonic-gate */ 681e3320f40Smarkfen invalue = parsenum(type, B_FALSE, ebuf); 6827c478bd9Sstevel@tonic-gate if (invalue != (u_longlong_t)-1 && 6837c478bd9Sstevel@tonic-gate (u_longlong_t)(invalue & (u_longlong_t)0xffff) == invalue) 6847c478bd9Sstevel@tonic-gate return ((uint16_t)invalue); 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate 687e3320f40Smarkfen ERROR1(ep, ebuf, gettext("Unknown identity type \"%s\"\n"), type); 688e3320f40Smarkfen 689e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE); 690eeda67c6Sjojemann return (0); 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate /* 6947c478bd9Sstevel@tonic-gate * Parse an address off the command line. Return length of sockaddr, 6957c478bd9Sstevel@tonic-gate * and either return a hostent pointer (caller frees). The new 6967c478bd9Sstevel@tonic-gate * getipnodebyname() call does the Right Thing (TM), even with 6977c478bd9Sstevel@tonic-gate * raw addresses (colon-separated IPv6 or dotted decimal IPv4). 6987c478bd9Sstevel@tonic-gate */ 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate static struct { 7017c478bd9Sstevel@tonic-gate struct hostent he; 7027c478bd9Sstevel@tonic-gate char *addtl[2]; 7037c478bd9Sstevel@tonic-gate } dummy; 7047c478bd9Sstevel@tonic-gate static union { 7057c478bd9Sstevel@tonic-gate struct in6_addr ipv6; 7067c478bd9Sstevel@tonic-gate struct in_addr ipv4; 7077c478bd9Sstevel@tonic-gate uint64_t aligner; 7087c478bd9Sstevel@tonic-gate } addr1; 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate static int 711e3320f40Smarkfen parseaddr(char *addr, struct hostent **hpp, boolean_t v6only, char *ebuf) 7127c478bd9Sstevel@tonic-gate { 7137c478bd9Sstevel@tonic-gate int hp_errno; 7147c478bd9Sstevel@tonic-gate struct hostent *hp = NULL; 715e3320f40Smarkfen char *ep = NULL; 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate if (addr == NULL) { 718e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line, " 719e3320f40Smarkfen "was expecting an address.\n")); 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate if (!nflag) { 7237c478bd9Sstevel@tonic-gate /* 7247c478bd9Sstevel@tonic-gate * Try name->address first. Assume AF_INET6, and 7257c478bd9Sstevel@tonic-gate * get IPv4's, plus IPv6's if and only if IPv6 is configured. 7267c478bd9Sstevel@tonic-gate * This means to add IPv6 SAs, you must have IPv6 7277c478bd9Sstevel@tonic-gate * up-and-running. (AI_DEFAULT works here.) 7287c478bd9Sstevel@tonic-gate */ 7297c478bd9Sstevel@tonic-gate hp = getipnodebyname(addr, AF_INET6, 7307c478bd9Sstevel@tonic-gate (v6only ? AI_ADDRCONFIG : (AI_DEFAULT | AI_ALL)), 7317c478bd9Sstevel@tonic-gate &hp_errno); 7327c478bd9Sstevel@tonic-gate } else { 7337c478bd9Sstevel@tonic-gate /* 7347c478bd9Sstevel@tonic-gate * Try a normal address conversion only. Use "dummy" 7357c478bd9Sstevel@tonic-gate * to construct a fake hostent. Caller will know not 7367c478bd9Sstevel@tonic-gate * to free this one. 7377c478bd9Sstevel@tonic-gate */ 7387c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, addr, &addr1) == 1) { 7397c478bd9Sstevel@tonic-gate dummy.he.h_addr_list = dummy.addtl; 7407c478bd9Sstevel@tonic-gate dummy.addtl[0] = (char *)&addr1; 7417c478bd9Sstevel@tonic-gate dummy.addtl[1] = NULL; 7427c478bd9Sstevel@tonic-gate hp = &dummy.he; 7437c478bd9Sstevel@tonic-gate dummy.he.h_addrtype = AF_INET6; 7447c478bd9Sstevel@tonic-gate dummy.he.h_length = sizeof (struct in6_addr); 7457c478bd9Sstevel@tonic-gate } else if (inet_pton(AF_INET, addr, &addr1) == 1) { 7467c478bd9Sstevel@tonic-gate /* 7478810c16bSdanmcd * Remap to AF_INET6 anyway. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate dummy.he.h_addr_list = dummy.addtl; 7507c478bd9Sstevel@tonic-gate dummy.addtl[0] = (char *)&addr1; 7517c478bd9Sstevel@tonic-gate dummy.addtl[1] = NULL; 7527c478bd9Sstevel@tonic-gate hp = &dummy.he; 7537c478bd9Sstevel@tonic-gate dummy.he.h_addrtype = AF_INET6; 7547c478bd9Sstevel@tonic-gate dummy.he.h_length = sizeof (struct in6_addr); 7557c478bd9Sstevel@tonic-gate /* 7567c478bd9Sstevel@tonic-gate * NOTE: If macro changes to disallow in-place 7577c478bd9Sstevel@tonic-gate * conversion, rewhack this. 7587c478bd9Sstevel@tonic-gate */ 7597c478bd9Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(&addr1.ipv4, &addr1.ipv6); 7607c478bd9Sstevel@tonic-gate } else { 7617c478bd9Sstevel@tonic-gate hp = NULL; 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate 765e3320f40Smarkfen if (hp == NULL) 766e3320f40Smarkfen WARN1(ep, ebuf, gettext("Unknown address %s."), addr); 7677c478bd9Sstevel@tonic-gate 7687c478bd9Sstevel@tonic-gate *hpp = hp; 7698810c16bSdanmcd /* Always return sockaddr_in6 for now. */ 770e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE); 7718810c16bSdanmcd return (sizeof (struct sockaddr_in6)); 7727c478bd9Sstevel@tonic-gate } 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate /* 7757c478bd9Sstevel@tonic-gate * Parse a hex character for a key. A string will take the form: 7767c478bd9Sstevel@tonic-gate * xxxxxxxxx/nn 7777c478bd9Sstevel@tonic-gate * where 7787c478bd9Sstevel@tonic-gate * xxxxxxxxx == a string of hex characters ([0-9][a-f][A-F]) 7797c478bd9Sstevel@tonic-gate * nn == an optional decimal "mask". If it is not present, it 7807c478bd9Sstevel@tonic-gate * is assumed that the hex string will be rounded to the nearest 7817c478bd9Sstevel@tonic-gate * byte, where odd nibbles, like 123 will become 0x0123. 7827c478bd9Sstevel@tonic-gate * 7837c478bd9Sstevel@tonic-gate * NOTE:Unlike the expression of IP addresses, I will not allow an 7847c478bd9Sstevel@tonic-gate * excessive "mask". For example 2112/50 is very illegal. 7857c478bd9Sstevel@tonic-gate * NOTE2: This key should be in canonical order. Consult your man 7867c478bd9Sstevel@tonic-gate * pages per algorithm about said order. 7877c478bd9Sstevel@tonic-gate */ 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate #define hd2num(hd) (((hd) >= '0' && (hd) <= '9') ? ((hd) - '0') : \ 7907c478bd9Sstevel@tonic-gate (((hd) >= 'a' && (hd) <= 'f') ? ((hd) - 'a' + 10) : ((hd) - 'A' + 10))) 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate static struct sadb_key * 793e3320f40Smarkfen parsekey(char *input, char *ebuf) 7947c478bd9Sstevel@tonic-gate { 7957c478bd9Sstevel@tonic-gate struct sadb_key *retval; 7967c478bd9Sstevel@tonic-gate uint_t i, hexlen = 0, bits, alloclen; 7977c478bd9Sstevel@tonic-gate uint8_t *key; 798e3320f40Smarkfen char *ep = NULL; 7997c478bd9Sstevel@tonic-gate 8007c478bd9Sstevel@tonic-gate if (input == NULL) { 801e3320f40Smarkfen FATAL(ep, ebuf, gettext("Unexpected end of command line, " 802e3320f40Smarkfen "was expecting a key.\n")); 8037c478bd9Sstevel@tonic-gate } 804ec485834Spwernau /* Allow hex values prepended with 0x convention */ 805ec485834Spwernau if ((strnlen(input, sizeof (hexlen)) > 2) && 806ec485834Spwernau (strncasecmp(input, "0x", 2) == 0)) 807ec485834Spwernau input += 2; 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate for (i = 0; input[i] != '\0' && input[i] != '/'; i++) 8107c478bd9Sstevel@tonic-gate hexlen++; 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate if (input[i] == '\0') { 8137c478bd9Sstevel@tonic-gate bits = 0; 8147c478bd9Sstevel@tonic-gate } else { 8157c478bd9Sstevel@tonic-gate /* Have /nn. */ 8167c478bd9Sstevel@tonic-gate input[i] = '\0'; 8177c478bd9Sstevel@tonic-gate if (sscanf((input + i + 1), "%u", &bits) != 1) { 818e3320f40Smarkfen FATAL1(ep, ebuf, gettext( 819e3320f40Smarkfen "\"%s\" is not a bit specifier.\n"), 8207c478bd9Sstevel@tonic-gate (input + i + 1)); 8217c478bd9Sstevel@tonic-gate } 8227c478bd9Sstevel@tonic-gate /* hexlen in nibbles */ 8237c478bd9Sstevel@tonic-gate if (((bits + 3) >> 2) > hexlen) { 824e3320f40Smarkfen ERROR2(ep, ebuf, gettext( 825e3320f40Smarkfen "bit length %d is too big for %s.\n"), bits, input); 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate /* 8287c478bd9Sstevel@tonic-gate * Adjust hexlen down if user gave us too small of a bit 8297c478bd9Sstevel@tonic-gate * count. 8307c478bd9Sstevel@tonic-gate */ 8317c478bd9Sstevel@tonic-gate if ((hexlen << 2) > bits + 3) { 832e3320f40Smarkfen WARN2(ep, ebuf, gettext( 833e3320f40Smarkfen "WARNING: Lower bits will be truncated " 834e3320f40Smarkfen "for:\n\t%s/%d.\n"), input, bits); 8357c478bd9Sstevel@tonic-gate hexlen = (bits + 3) >> 2; 8367c478bd9Sstevel@tonic-gate input[hexlen] = '\0'; 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate } 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate /* 8417c478bd9Sstevel@tonic-gate * Allocate. Remember, hexlen is in nibbles. 8427c478bd9Sstevel@tonic-gate */ 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate alloclen = sizeof (*retval) + roundup((hexlen/2 + (hexlen & 0x1)), 8); 8457c478bd9Sstevel@tonic-gate retval = malloc(alloclen); 8467c478bd9Sstevel@tonic-gate 8477c478bd9Sstevel@tonic-gate if (retval == NULL) 8487c478bd9Sstevel@tonic-gate Bail("malloc(parsekey)"); 8497c478bd9Sstevel@tonic-gate retval->sadb_key_len = SADB_8TO64(alloclen); 8507c478bd9Sstevel@tonic-gate retval->sadb_key_reserved = 0; 8517c478bd9Sstevel@tonic-gate if (bits == 0) 8527c478bd9Sstevel@tonic-gate retval->sadb_key_bits = (hexlen + (hexlen & 0x1)) << 2; 8537c478bd9Sstevel@tonic-gate else 8547c478bd9Sstevel@tonic-gate retval->sadb_key_bits = bits; 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate /* 8577c478bd9Sstevel@tonic-gate * Read in nibbles. Read in odd-numbered as shifted high. 8587c478bd9Sstevel@tonic-gate * (e.g. 123 becomes 0x1230). 8597c478bd9Sstevel@tonic-gate */ 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate key = (uint8_t *)(retval + 1); 8627c478bd9Sstevel@tonic-gate for (i = 0; input[i] != '\0'; i += 2) { 8637c478bd9Sstevel@tonic-gate boolean_t second = (input[i + 1] != '\0'); 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate if (!isxdigit(input[i]) || 8667c478bd9Sstevel@tonic-gate (!isxdigit(input[i + 1]) && second)) { 867e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 868e3320f40Smarkfen "string '%s' not a hex value.\n"), input); 869e3320f40Smarkfen free(retval); 870e3320f40Smarkfen retval = NULL; 871e3320f40Smarkfen break; 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate *key = (hd2num(input[i]) << 4); 8747c478bd9Sstevel@tonic-gate if (second) 8757c478bd9Sstevel@tonic-gate *key |= hd2num(input[i + 1]); 8767c478bd9Sstevel@tonic-gate else 8777c478bd9Sstevel@tonic-gate break; /* out of for loop. */ 8787c478bd9Sstevel@tonic-gate key++; 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate 8817c478bd9Sstevel@tonic-gate /* bzero the remaining bits if we're a non-octet amount. */ 8827c478bd9Sstevel@tonic-gate if (bits & 0x7) 8837c478bd9Sstevel@tonic-gate *((input[i] == '\0') ? key - 1 : key) &= 8847c478bd9Sstevel@tonic-gate 0xff << (8 - (bits & 0x7)); 8857c478bd9Sstevel@tonic-gate 886e3320f40Smarkfen handle_errors(ep, NULL, B_FALSE, B_FALSE); 8877c478bd9Sstevel@tonic-gate return (retval); 8887c478bd9Sstevel@tonic-gate } 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate /* 8917c478bd9Sstevel@tonic-gate * Write a message to the PF_KEY socket. If verbose, print the message 8927c478bd9Sstevel@tonic-gate * heading into the kernel. 8937c478bd9Sstevel@tonic-gate */ 8947c478bd9Sstevel@tonic-gate static int 8957c478bd9Sstevel@tonic-gate key_write(int fd, void *msg, size_t len) 8967c478bd9Sstevel@tonic-gate { 8977c478bd9Sstevel@tonic-gate if (vflag) { 8987c478bd9Sstevel@tonic-gate (void) printf( 8997c478bd9Sstevel@tonic-gate gettext("VERBOSE ON: Message to kernel looks like:\n")); 9007c478bd9Sstevel@tonic-gate (void) printf("==========================================\n"); 901bb3ed8dfSpwernau print_samsg(stdout, msg, B_FALSE, vflag, nflag); 9027c478bd9Sstevel@tonic-gate (void) printf("==========================================\n"); 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate 9057c478bd9Sstevel@tonic-gate return (write(fd, msg, len)); 9067c478bd9Sstevel@tonic-gate } 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate /* 9097c478bd9Sstevel@tonic-gate * SIGALRM handler for time_critical_enter. 9107c478bd9Sstevel@tonic-gate */ 9117c478bd9Sstevel@tonic-gate static void 9127c478bd9Sstevel@tonic-gate time_critical_catch(int signal) 9137c478bd9Sstevel@tonic-gate { 9147c478bd9Sstevel@tonic-gate if (signal == SIGALRM) { 9157c478bd9Sstevel@tonic-gate errx(1, gettext("Reply message from PF_KEY timed out.")); 9167c478bd9Sstevel@tonic-gate } else { 9177c478bd9Sstevel@tonic-gate errx(1, gettext("Caught signal %d while trying to receive" 91825e435e0Spwernau "PF_KEY reply message"), signal); 9197c478bd9Sstevel@tonic-gate } 9207c478bd9Sstevel@tonic-gate /* errx() calls exit. */ 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate 9237c478bd9Sstevel@tonic-gate #define TIME_CRITICAL_TIME 10 /* In seconds */ 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* 9267c478bd9Sstevel@tonic-gate * Enter a "time critical" section where key is waiting for a return message. 9277c478bd9Sstevel@tonic-gate */ 9287c478bd9Sstevel@tonic-gate static void 9297c478bd9Sstevel@tonic-gate time_critical_enter(void) 9307c478bd9Sstevel@tonic-gate { 9317c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, time_critical_catch); 9327c478bd9Sstevel@tonic-gate (void) alarm(TIME_CRITICAL_TIME); 9337c478bd9Sstevel@tonic-gate } 9347c478bd9Sstevel@tonic-gate 9357c478bd9Sstevel@tonic-gate /* 9367c478bd9Sstevel@tonic-gate * Exit the "time critical" section after getting an appropriate return 9377c478bd9Sstevel@tonic-gate * message. 9387c478bd9Sstevel@tonic-gate */ 9397c478bd9Sstevel@tonic-gate static void 9407c478bd9Sstevel@tonic-gate time_critical_exit(void) 9417c478bd9Sstevel@tonic-gate { 9427c478bd9Sstevel@tonic-gate (void) alarm(0); 9437c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, SIG_DFL); 9447c478bd9Sstevel@tonic-gate } 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate /* 9477c478bd9Sstevel@tonic-gate * Construct a PF_KEY FLUSH message for the SA type specified. 9487c478bd9Sstevel@tonic-gate */ 9497c478bd9Sstevel@tonic-gate static void 9507c478bd9Sstevel@tonic-gate doflush(int satype) 9517c478bd9Sstevel@tonic-gate { 9527c478bd9Sstevel@tonic-gate struct sadb_msg msg; 9537c478bd9Sstevel@tonic-gate int rc; 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate msg_init(&msg, SADB_FLUSH, (uint8_t)satype); 9567c478bd9Sstevel@tonic-gate rc = key_write(keysock, &msg, sizeof (msg)); 9577c478bd9Sstevel@tonic-gate if (rc == -1) 9587c478bd9Sstevel@tonic-gate Bail("write() to PF_KEY socket failed (in doflush)"); 9597c478bd9Sstevel@tonic-gate 9607c478bd9Sstevel@tonic-gate time_critical_enter(); 9617c478bd9Sstevel@tonic-gate do { 9627c478bd9Sstevel@tonic-gate rc = read(keysock, &msg, sizeof (msg)); 9637c478bd9Sstevel@tonic-gate if (rc == -1) 9647c478bd9Sstevel@tonic-gate Bail("read (in doflush)"); 9657c478bd9Sstevel@tonic-gate } while (msg.sadb_msg_seq != seq || msg.sadb_msg_pid != mypid); 9667c478bd9Sstevel@tonic-gate time_critical_exit(); 9677c478bd9Sstevel@tonic-gate 9687c478bd9Sstevel@tonic-gate /* 9697c478bd9Sstevel@tonic-gate * I should _never_ hit the following unless: 9707c478bd9Sstevel@tonic-gate * 9717c478bd9Sstevel@tonic-gate * 1. There is a kernel bug. 9727c478bd9Sstevel@tonic-gate * 2. There is another process filling in its pid with mine, and 9737c478bd9Sstevel@tonic-gate * issuing a different message that would cause a different result. 9747c478bd9Sstevel@tonic-gate */ 9757c478bd9Sstevel@tonic-gate if (msg.sadb_msg_type != SADB_FLUSH || 9767c478bd9Sstevel@tonic-gate msg.sadb_msg_satype != (uint8_t)satype) { 9777c478bd9Sstevel@tonic-gate syslog((LOG_NOTICE|LOG_AUTH), 9787c478bd9Sstevel@tonic-gate gettext("doflush: Return message not of type SADB_FLUSH!")); 9797c478bd9Sstevel@tonic-gate Bail("doflush: Return message not of type SADB_FLUSH!"); 9807c478bd9Sstevel@tonic-gate } 9817c478bd9Sstevel@tonic-gate 9827c478bd9Sstevel@tonic-gate if (msg.sadb_msg_errno != 0) { 9837c478bd9Sstevel@tonic-gate errno = msg.sadb_msg_errno; 9847c478bd9Sstevel@tonic-gate if (errno == EINVAL) { 9857c478bd9Sstevel@tonic-gate print_diagnostic(stderr, msg.sadb_x_msg_diagnostic); 9867c478bd9Sstevel@tonic-gate warnx(gettext("Cannot flush SA type %d."), satype); 9877c478bd9Sstevel@tonic-gate } 9887c478bd9Sstevel@tonic-gate Bail("return message (in doflush)"); 9897c478bd9Sstevel@tonic-gate } 9907c478bd9Sstevel@tonic-gate } 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate /* 9937c478bd9Sstevel@tonic-gate * save_XXX functions are used when "saving" the SA tables to either a 9947c478bd9Sstevel@tonic-gate * file or standard output. They use the dump_XXX functions where needed, 9957c478bd9Sstevel@tonic-gate * but mostly they use the rparseXXX functions. 9967c478bd9Sstevel@tonic-gate */ 9977c478bd9Sstevel@tonic-gate 9987c478bd9Sstevel@tonic-gate /* 9997c478bd9Sstevel@tonic-gate * Because "save" and "dump" both use the SADB_DUMP message, fold both 10007c478bd9Sstevel@tonic-gate * into the same function. 10017c478bd9Sstevel@tonic-gate */ 10027c478bd9Sstevel@tonic-gate static void 10037c478bd9Sstevel@tonic-gate dodump(int satype, FILE *ofile) 10047c478bd9Sstevel@tonic-gate { 10057c478bd9Sstevel@tonic-gate struct sadb_msg *msg = (struct sadb_msg *)get_buffer; 10067c478bd9Sstevel@tonic-gate int rc; 10077c478bd9Sstevel@tonic-gate 10087c478bd9Sstevel@tonic-gate if (ofile != NULL) { 10097c478bd9Sstevel@tonic-gate (void) fprintf(ofile, 10107c478bd9Sstevel@tonic-gate gettext("# This key file was generated by the")); 10117c478bd9Sstevel@tonic-gate (void) fprintf(ofile, 10127c478bd9Sstevel@tonic-gate gettext(" ipseckey(1m) command's 'save' feature.\n\n")); 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate msg_init(msg, SADB_DUMP, (uint8_t)satype); 10157c478bd9Sstevel@tonic-gate rc = key_write(keysock, msg, sizeof (*msg)); 10167c478bd9Sstevel@tonic-gate if (rc == -1) 10177c478bd9Sstevel@tonic-gate Bail("write to PF_KEY socket failed (in dodump)"); 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate do { 10207c478bd9Sstevel@tonic-gate /* 10217c478bd9Sstevel@tonic-gate * For DUMP, do only the read as a time critical section. 10227c478bd9Sstevel@tonic-gate */ 10237c478bd9Sstevel@tonic-gate time_critical_enter(); 10247c478bd9Sstevel@tonic-gate rc = read(keysock, get_buffer, sizeof (get_buffer)); 10257c478bd9Sstevel@tonic-gate time_critical_exit(); 10267c478bd9Sstevel@tonic-gate if (rc == -1) 10277c478bd9Sstevel@tonic-gate Bail("read (in dodump)"); 10287c478bd9Sstevel@tonic-gate if (msg->sadb_msg_pid == mypid && 10297c478bd9Sstevel@tonic-gate msg->sadb_msg_type == SADB_DUMP && 10307c478bd9Sstevel@tonic-gate msg->sadb_msg_seq != 0 && 10317c478bd9Sstevel@tonic-gate msg->sadb_msg_errno == 0) { 10327c478bd9Sstevel@tonic-gate if (ofile == NULL) { 1033bb3ed8dfSpwernau print_samsg(stdout, get_buffer, B_FALSE, vflag, 1034bb3ed8dfSpwernau nflag); 10357c478bd9Sstevel@tonic-gate (void) putchar('\n'); 10367c478bd9Sstevel@tonic-gate } else { 10377c478bd9Sstevel@tonic-gate save_assoc(get_buffer, ofile); 10387c478bd9Sstevel@tonic-gate } 10397c478bd9Sstevel@tonic-gate } 10407c478bd9Sstevel@tonic-gate } while (msg->sadb_msg_pid != mypid || 10417c478bd9Sstevel@tonic-gate (msg->sadb_msg_errno == 0 && msg->sadb_msg_seq != 0)); 10427c478bd9Sstevel@tonic-gate 10437c478bd9Sstevel@tonic-gate if (ofile != NULL && ofile != stdout) 10447c478bd9Sstevel@tonic-gate (void) fclose(ofile); 10457c478bd9Sstevel@tonic-gate 10467c478bd9Sstevel@tonic-gate if (msg->sadb_msg_errno == 0) { 10477c478bd9Sstevel@tonic-gate if (ofile == NULL) 10487c478bd9Sstevel@tonic-gate (void) printf( 10497c478bd9Sstevel@tonic-gate gettext("Dump succeeded for SA type %d.\n"), 10507c478bd9Sstevel@tonic-gate satype); 10517c478bd9Sstevel@tonic-gate } else { 10527c478bd9Sstevel@tonic-gate print_diagnostic(stderr, msg->sadb_x_msg_diagnostic); 10537c478bd9Sstevel@tonic-gate errno = msg->sadb_msg_errno; 10547c478bd9Sstevel@tonic-gate Bail("Dump failed"); 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate } 10577c478bd9Sstevel@tonic-gate 10587c478bd9Sstevel@tonic-gate #define SCOPE_UNSPEC 0 10597c478bd9Sstevel@tonic-gate #define SCOPE_LINKLOCAL 1 10607c478bd9Sstevel@tonic-gate #define SCOPE_SITELOCAL 2 10617c478bd9Sstevel@tonic-gate #define SCOPE_GLOBAL 3 10627c478bd9Sstevel@tonic-gate #define SCOPE_V4COMPAT 4 10637c478bd9Sstevel@tonic-gate #define SCOPE_LOOPBACK 5 /* Pedantic, yes, but necessary. */ 10647c478bd9Sstevel@tonic-gate 10657c478bd9Sstevel@tonic-gate static int 10667c478bd9Sstevel@tonic-gate ipv6_addr_scope(struct in6_addr *addr) 10677c478bd9Sstevel@tonic-gate { 10687c478bd9Sstevel@tonic-gate /* Don't return anything regarding multicast for now... */ 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED(addr)) 10717c478bd9Sstevel@tonic-gate return (SCOPE_UNSPEC); 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_LINKLOCAL(addr)) 10747c478bd9Sstevel@tonic-gate return (SCOPE_LINKLOCAL); 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_SITELOCAL(addr)) 10777c478bd9Sstevel@tonic-gate return (SCOPE_SITELOCAL); 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4COMPAT(addr)) 10807c478bd9Sstevel@tonic-gate return (SCOPE_V4COMPAT); 10817c478bd9Sstevel@tonic-gate 10827c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_LOOPBACK(addr)) 10837c478bd9Sstevel@tonic-gate return (SCOPE_LOOPBACK); 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate /* For now, return global by default. */ 10867c478bd9Sstevel@tonic-gate return (SCOPE_GLOBAL); 10877c478bd9Sstevel@tonic-gate } 10887c478bd9Sstevel@tonic-gate 10897c478bd9Sstevel@tonic-gate /* 10907c478bd9Sstevel@tonic-gate * doaddresses(): 10917c478bd9Sstevel@tonic-gate * 10927c478bd9Sstevel@tonic-gate * Used by doaddup() and dodelget() to create new SA's based on the 10937c478bd9Sstevel@tonic-gate * provided source and destination addresses hostent. 10947c478bd9Sstevel@tonic-gate * 10957c478bd9Sstevel@tonic-gate * sadb_msg_type: expected PF_KEY reply message type 10967c478bd9Sstevel@tonic-gate * sadb_msg_satype: expected PF_KEY reply satype 10977c478bd9Sstevel@tonic-gate * cmd: user command 10987c478bd9Sstevel@tonic-gate * srchp: hostent for the source address(es) 10997c478bd9Sstevel@tonic-gate * dsthp: hostent for the destination address(es) 11007c478bd9Sstevel@tonic-gate * src: points to the SADB source address extension 11017c478bd9Sstevel@tonic-gate * dst: points to the SADB destination address extension 11027c478bd9Sstevel@tonic-gate * unspec_src: indicates an unspecified source address. 11037c478bd9Sstevel@tonic-gate * buffer: pointer to the SADB buffer to use with PF_KEY 11047c478bd9Sstevel@tonic-gate * buffer_size: size of buffer 11057c478bd9Sstevel@tonic-gate * spi: spi for this message (set by caller) 11067c478bd9Sstevel@tonic-gate * srcport: source port if specified 11078810c16bSdanmcd * dstport: destination port if specified 11087c478bd9Sstevel@tonic-gate * proto: IP protocol number if specified 11098810c16bSdanmcd * iproto: Inner (tunnel mode) IP protocol number if specified 11107c478bd9Sstevel@tonic-gate * NATT note: we are going to assume a semi-sane world where NAT 11117c478bd9Sstevel@tonic-gate * boxen don't explode to multiple addresses. 11127c478bd9Sstevel@tonic-gate */ 11137c478bd9Sstevel@tonic-gate static void 11147c478bd9Sstevel@tonic-gate doaddresses(uint8_t sadb_msg_type, uint8_t sadb_msg_satype, int cmd, 11157c478bd9Sstevel@tonic-gate struct hostent *srchp, struct hostent *dsthp, 11167c478bd9Sstevel@tonic-gate struct sadb_address *src, struct sadb_address *dst, 1117e3320f40Smarkfen boolean_t unspec_src, uint64_t *buffer, int buffer_size, uint32_t spi, 1118e3320f40Smarkfen char *ebuf) 11197c478bd9Sstevel@tonic-gate { 11207c478bd9Sstevel@tonic-gate boolean_t single_dst; 11217c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 11227c478bd9Sstevel@tonic-gate struct sadb_msg *msgp; 11237c478bd9Sstevel@tonic-gate int i, rc; 11247c478bd9Sstevel@tonic-gate char **walker; /* For the SRC and PROXY walking functions. */ 11257c478bd9Sstevel@tonic-gate char *first_match; 11268810c16bSdanmcd uint64_t savebuf[MAX_GET_SIZE]; 11278810c16bSdanmcd uint16_t srcport = 0, dstport = 0; 1128e3320f40Smarkfen char *ep = NULL; 11297c478bd9Sstevel@tonic-gate 11307c478bd9Sstevel@tonic-gate /* 11317c478bd9Sstevel@tonic-gate * Okay, now we have "src", "dst", and maybe "proxy" reassigned 11327c478bd9Sstevel@tonic-gate * to point into the buffer to be written to PF_KEY, we can do 11337c478bd9Sstevel@tonic-gate * potentially several writes based on destination address. 11347c478bd9Sstevel@tonic-gate * 11358810c16bSdanmcd * First, obtain port numbers from passed-in extensions. 11367c478bd9Sstevel@tonic-gate */ 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate if (src != NULL) { 11397c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(src + 1); 11408810c16bSdanmcd srcport = ntohs(sin6->sin6_port); 11417c478bd9Sstevel@tonic-gate } 11427c478bd9Sstevel@tonic-gate if (dst != NULL) { 11437c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(dst + 1); 11448810c16bSdanmcd dstport = ntohs(sin6->sin6_port); 11457c478bd9Sstevel@tonic-gate } 11467c478bd9Sstevel@tonic-gate 11477c478bd9Sstevel@tonic-gate /* 11487c478bd9Sstevel@tonic-gate * The rules for ADD, GET, and UPDATE: (NOTE: This assumes IPsec. 11497c478bd9Sstevel@tonic-gate * If other consumers of PF_KEY happen, this will have to be 11507c478bd9Sstevel@tonic-gate * rewhacked.): 11517c478bd9Sstevel@tonic-gate * 11527c478bd9Sstevel@tonic-gate * Do a message for every possible DST address. 11537c478bd9Sstevel@tonic-gate * 11547c478bd9Sstevel@tonic-gate * If a source or proxy address explodes, keep unspecified 11557c478bd9Sstevel@tonic-gate * (and mention unspecified). 11567c478bd9Sstevel@tonic-gate * 11577c478bd9Sstevel@tonic-gate * If dsthp is == dummy.he, then go through the loop once. 11587c478bd9Sstevel@tonic-gate * If any other hp is == dummy.he, then you don't have to apply any 11597c478bd9Sstevel@tonic-gate * silly rules. 11607c478bd9Sstevel@tonic-gate * 11617c478bd9Sstevel@tonic-gate * DELETE is different, because you can leave either "src" or "dst" 11627c478bd9Sstevel@tonic-gate * blank! You need to explode if one of them is full, and not assume 11637c478bd9Sstevel@tonic-gate * that the other is set. 11647c478bd9Sstevel@tonic-gate */ 11657c478bd9Sstevel@tonic-gate 11667c478bd9Sstevel@tonic-gate if (dsthp == NULL) { 11677c478bd9Sstevel@tonic-gate /* 11687c478bd9Sstevel@tonic-gate * No destination address specified. 11697c478bd9Sstevel@tonic-gate * With extended diagnostics, we don't have to bail the 11707c478bd9Sstevel@tonic-gate * non-DELETE cases here. The EINVAL diagnostics will be 11717c478bd9Sstevel@tonic-gate * enough to inform the user(s) what happened. 11727c478bd9Sstevel@tonic-gate */ 11737c478bd9Sstevel@tonic-gate i = 0; 11747c478bd9Sstevel@tonic-gate do { 11757c478bd9Sstevel@tonic-gate if (srchp == &dummy.he) { 11767c478bd9Sstevel@tonic-gate /* Just to be sure... */ 11777c478bd9Sstevel@tonic-gate srchp->h_addr_list[1] = NULL; 11787c478bd9Sstevel@tonic-gate } else if (srchp != NULL) { 11797c478bd9Sstevel@tonic-gate /* Degenerate case, h_addr_list[0] == NULL. */ 11807c478bd9Sstevel@tonic-gate if (srchp->h_addr_list[i] == NULL) 11817c478bd9Sstevel@tonic-gate Bail("Empty source address list"); 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate /* 11847c478bd9Sstevel@tonic-gate * Fill in the src sockaddr. 11857c478bd9Sstevel@tonic-gate */ 11867c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(src + 1); 11877c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 11887c478bd9Sstevel@tonic-gate bcopy(srchp->h_addr_list[i], &sin6->sin6_addr, 11897c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 11907c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 11917c478bd9Sstevel@tonic-gate sin6->sin6_port = htons(srcport); 11927c478bd9Sstevel@tonic-gate } 11937c478bd9Sstevel@tonic-gate 11947c478bd9Sstevel@tonic-gate /* Save off a copy for later writing... */ 11957c478bd9Sstevel@tonic-gate msgp = (struct sadb_msg *)buffer; 11967c478bd9Sstevel@tonic-gate bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len)); 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate rc = key_write(keysock, buffer, 11997c478bd9Sstevel@tonic-gate SADB_64TO8(msgp->sadb_msg_len)); 12007c478bd9Sstevel@tonic-gate if (rc == -1) 12017c478bd9Sstevel@tonic-gate Bail("write() to PF_KEY socket " 12027c478bd9Sstevel@tonic-gate "(in doaddresses)"); 1203*9c2c14abSThejaswini Singarajipura /* 1204*9c2c14abSThejaswini Singarajipura * Sends the message to the Solaris Cluster daemon 1205*9c2c14abSThejaswini Singarajipura */ 1206*9c2c14abSThejaswini Singarajipura 1207*9c2c14abSThejaswini Singarajipura if (in_cluster_mode) { 1208*9c2c14abSThejaswini Singarajipura (void) sendto(cluster_socket, buffer, 1209*9c2c14abSThejaswini Singarajipura SADB_64TO8(msgp->sadb_msg_len), 0, 1210*9c2c14abSThejaswini Singarajipura (struct sockaddr *)&cli_addr, 1211*9c2c14abSThejaswini Singarajipura sizeof (cli_addr)); 1212*9c2c14abSThejaswini Singarajipura } 12137c478bd9Sstevel@tonic-gate 12147c478bd9Sstevel@tonic-gate time_critical_enter(); 12157c478bd9Sstevel@tonic-gate do { 12167c478bd9Sstevel@tonic-gate rc = read(keysock, buffer, buffer_size); 12177c478bd9Sstevel@tonic-gate if (rc == -1) 12187c478bd9Sstevel@tonic-gate Bail("read (in doaddresses)"); 12197c478bd9Sstevel@tonic-gate } while (msgp->sadb_msg_seq != seq || 12207c478bd9Sstevel@tonic-gate msgp->sadb_msg_pid != mypid); 12217c478bd9Sstevel@tonic-gate time_critical_exit(); 12227c478bd9Sstevel@tonic-gate 12237c478bd9Sstevel@tonic-gate if (msgp->sadb_msg_type != sadb_msg_type || 12247c478bd9Sstevel@tonic-gate msgp->sadb_msg_satype != sadb_msg_satype) { 12257c478bd9Sstevel@tonic-gate syslog((LOG_NOTICE|LOG_AUTH), gettext( 12267c478bd9Sstevel@tonic-gate "doaddresses: Unexpected returned message " 12277c478bd9Sstevel@tonic-gate "(%d exp %d)\n"), msgp->sadb_msg_type, 12287c478bd9Sstevel@tonic-gate sadb_msg_type); 12297c478bd9Sstevel@tonic-gate Bail("doaddresses: Unexpected returned " 12307c478bd9Sstevel@tonic-gate "message"); 12317c478bd9Sstevel@tonic-gate } 12327c478bd9Sstevel@tonic-gate 12337c478bd9Sstevel@tonic-gate errno = msgp->sadb_msg_errno; 12347c478bd9Sstevel@tonic-gate if (errno != 0) { 12357c478bd9Sstevel@tonic-gate if (errno == EINVAL) { 1236e3320f40Smarkfen WARN(ep, ebuf, gettext( 1237e3320f40Smarkfen "One of the entered " 1238e3320f40Smarkfen "values is incorrect.")); 12397c478bd9Sstevel@tonic-gate print_diagnostic(stderr, 12407c478bd9Sstevel@tonic-gate msgp->sadb_x_msg_diagnostic); 1241e3320f40Smarkfen } else { 1242e3320f40Smarkfen Bail("return message (in doaddresses)"); 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate } 12457c478bd9Sstevel@tonic-gate 12467c478bd9Sstevel@tonic-gate /* ...and then restore the saved buffer. */ 12477c478bd9Sstevel@tonic-gate msgp = (struct sadb_msg *)savebuf; 12487c478bd9Sstevel@tonic-gate bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len)); 12497c478bd9Sstevel@tonic-gate } while (srchp != NULL && srchp->h_addr_list[++i] != NULL); 12507c478bd9Sstevel@tonic-gate return; 12517c478bd9Sstevel@tonic-gate } 12527c478bd9Sstevel@tonic-gate 12537c478bd9Sstevel@tonic-gate single_dst = (dsthp == &dummy.he || dsthp->h_addr_list[1] == NULL); 12547c478bd9Sstevel@tonic-gate 12557c478bd9Sstevel@tonic-gate for (i = 0; dsthp->h_addr_list[i] != NULL; i++) { 12567c478bd9Sstevel@tonic-gate if (dsthp == &dummy.he) { 12577c478bd9Sstevel@tonic-gate /* Just to be sure... */ 12587c478bd9Sstevel@tonic-gate dsthp->h_addr_list[1] = NULL; 12597c478bd9Sstevel@tonic-gate } else { 12607c478bd9Sstevel@tonic-gate /* 12617c478bd9Sstevel@tonic-gate * Fill in the dst sockaddr. 12627c478bd9Sstevel@tonic-gate */ 12637c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(dst + 1); 12647c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 12657c478bd9Sstevel@tonic-gate bcopy(dsthp->h_addr_list[i], &sin6->sin6_addr, 12667c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 12677c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 12687c478bd9Sstevel@tonic-gate sin6->sin6_port = htons(dstport); 12697c478bd9Sstevel@tonic-gate } 12707c478bd9Sstevel@tonic-gate 12717c478bd9Sstevel@tonic-gate /* 12727c478bd9Sstevel@tonic-gate * Try and assign src, if there's any ambiguity. 12737c478bd9Sstevel@tonic-gate */ 12747c478bd9Sstevel@tonic-gate if (!unspec_src && srchp != &dummy.he) { 12757c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 12767c478bd9Sstevel@tonic-gate /* 12777c478bd9Sstevel@tonic-gate * IPv4 address. Find an IPv4 address, then 12787c478bd9Sstevel@tonic-gate * keep looking for a second one. If a second 12797c478bd9Sstevel@tonic-gate * exists, print a message, and fill in the 12807c478bd9Sstevel@tonic-gate * unspecified address. 12817c478bd9Sstevel@tonic-gate */ 12827c478bd9Sstevel@tonic-gate first_match = NULL; 12837c478bd9Sstevel@tonic-gate 12847c478bd9Sstevel@tonic-gate for (walker = srchp->h_addr_list; 12857c478bd9Sstevel@tonic-gate *walker != NULL; walker++) { 12867c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 12877c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED( 12887c478bd9Sstevel@tonic-gate (struct in6_addr *)*walker)) { 12897c478bd9Sstevel@tonic-gate if (first_match != NULL) 12907c478bd9Sstevel@tonic-gate break; 12917c478bd9Sstevel@tonic-gate else 12927c478bd9Sstevel@tonic-gate first_match = *walker; 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(src + 1); 12967c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 12977c478bd9Sstevel@tonic-gate 12987c478bd9Sstevel@tonic-gate if (first_match == NULL) { 12997c478bd9Sstevel@tonic-gate /* 13007c478bd9Sstevel@tonic-gate * No IPv4 hits. Is this a single 13017c478bd9Sstevel@tonic-gate * dest? 13027c478bd9Sstevel@tonic-gate */ 1303e3320f40Smarkfen WARN1(ep, ebuf, gettext( 13047c478bd9Sstevel@tonic-gate "No IPv4 source address " 1305e3320f40Smarkfen "for name %s.\n"), srchp->h_name); 13067c478bd9Sstevel@tonic-gate if (single_dst) { 1307e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1308e3320f40Smarkfen "Only single destination " 1309e3320f40Smarkfen "IP address.\n")); 13107c478bd9Sstevel@tonic-gate } else { 13117c478bd9Sstevel@tonic-gate /* Continue, but do I print? */ 13127c478bd9Sstevel@tonic-gate continue; /* for loop */ 13137c478bd9Sstevel@tonic-gate } 13147c478bd9Sstevel@tonic-gate 13157c478bd9Sstevel@tonic-gate /* I should never reach here. */ 13167c478bd9Sstevel@tonic-gate } 13177c478bd9Sstevel@tonic-gate 13187c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 13197c478bd9Sstevel@tonic-gate sin6->sin6_port = htons(srcport); 13207c478bd9Sstevel@tonic-gate if (*walker != NULL) { 13217c478bd9Sstevel@tonic-gate /* 13227c478bd9Sstevel@tonic-gate * Early loop exit. It must've been 13237c478bd9Sstevel@tonic-gate * multiple hits... 13247c478bd9Sstevel@tonic-gate * 13257c478bd9Sstevel@tonic-gate * Issue a null-source warning? 13267c478bd9Sstevel@tonic-gate */ 1327e3320f40Smarkfen WARN1(ep, ebuf, gettext( 13287c478bd9Sstevel@tonic-gate "Multiple IPv4 source addresses " 13297c478bd9Sstevel@tonic-gate "for %s, using unspecified source " 13307c478bd9Sstevel@tonic-gate "instead."), srchp->h_name); 13317c478bd9Sstevel@tonic-gate } else { 13327c478bd9Sstevel@tonic-gate /* 13337c478bd9Sstevel@tonic-gate * If I reach here w/o hitting the 13347c478bd9Sstevel@tonic-gate * previous if statements, I have a 13357c478bd9Sstevel@tonic-gate * single source address for this 13367c478bd9Sstevel@tonic-gate * destination. 13377c478bd9Sstevel@tonic-gate */ 13387c478bd9Sstevel@tonic-gate bcopy(first_match, &sin6->sin6_addr, 13397c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 13407c478bd9Sstevel@tonic-gate } 13417c478bd9Sstevel@tonic-gate } else { 13427c478bd9Sstevel@tonic-gate /* 13437c478bd9Sstevel@tonic-gate * IPv6 address. Find an IPv6 address. 13447c478bd9Sstevel@tonic-gate * Unlike IPv4 addresses, things can get a 13457c478bd9Sstevel@tonic-gate * little more sticky with scopes, etc. 13467c478bd9Sstevel@tonic-gate */ 13477c478bd9Sstevel@tonic-gate int dst_scope, src_scope; 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate dst_scope = ipv6_addr_scope(&sin6->sin6_addr); 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate first_match = NULL; 13527c478bd9Sstevel@tonic-gate for (walker = srchp->h_addr_list; 13537c478bd9Sstevel@tonic-gate *walker != NULL; walker++) { 13547c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 13557c478bd9Sstevel@tonic-gate if (!IN6_IS_ADDR_V4MAPPED( 13567c478bd9Sstevel@tonic-gate (struct in6_addr *)*walker)) { 13577c478bd9Sstevel@tonic-gate /* 13587c478bd9Sstevel@tonic-gate * Set first-match, etc. 13597c478bd9Sstevel@tonic-gate * Take into account scopes, 13607c478bd9Sstevel@tonic-gate * and other IPv6 thingies. 13617c478bd9Sstevel@tonic-gate */ 13627c478bd9Sstevel@tonic-gate src_scope = ipv6_addr_scope( 13637c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST */ 13647c478bd9Sstevel@tonic-gate (struct in6_addr *)*walker); 13657c478bd9Sstevel@tonic-gate if (src_scope == SCOPE_UNSPEC || 13667c478bd9Sstevel@tonic-gate src_scope == dst_scope) { 13677c478bd9Sstevel@tonic-gate if (first_match != 13687c478bd9Sstevel@tonic-gate NULL) 13697c478bd9Sstevel@tonic-gate break; 13707c478bd9Sstevel@tonic-gate else 13717c478bd9Sstevel@tonic-gate first_match = 13727c478bd9Sstevel@tonic-gate *walker; 13737c478bd9Sstevel@tonic-gate } 13747c478bd9Sstevel@tonic-gate } 13757c478bd9Sstevel@tonic-gate } 13767c478bd9Sstevel@tonic-gate 13777c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(src + 1); 13787c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 13797c478bd9Sstevel@tonic-gate sin6->sin6_port = htons(srcport); 13807c478bd9Sstevel@tonic-gate if (first_match == NULL) { 13817c478bd9Sstevel@tonic-gate /* 13827c478bd9Sstevel@tonic-gate * No IPv6 hits. Is this a single 13837c478bd9Sstevel@tonic-gate * dest? 13847c478bd9Sstevel@tonic-gate */ 1385e3320f40Smarkfen WARN1(ep, ebuf, gettext( 13867c478bd9Sstevel@tonic-gate "No IPv6 source address of " 1387e3320f40Smarkfen "matching scope for name %s.\n"), 13887c478bd9Sstevel@tonic-gate srchp->h_name); 13897c478bd9Sstevel@tonic-gate if (single_dst) { 1390e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1391e3320f40Smarkfen "Only a single IPV6 " 1392e3320f40Smarkfen "destination " 1393e3320f40Smarkfen "address.\n")); 13947c478bd9Sstevel@tonic-gate } else { 13957c478bd9Sstevel@tonic-gate /* Continue, but do I print? */ 13967c478bd9Sstevel@tonic-gate continue; /* for loop */ 13977c478bd9Sstevel@tonic-gate } 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate /* I should never reach here. */ 14007c478bd9Sstevel@tonic-gate } 14017c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 14027c478bd9Sstevel@tonic-gate if (*walker != NULL) { 14037c478bd9Sstevel@tonic-gate /* 14047c478bd9Sstevel@tonic-gate * Early loop exit. Issue a 14057c478bd9Sstevel@tonic-gate * null-source warning? 14067c478bd9Sstevel@tonic-gate */ 1407e3320f40Smarkfen WARN1(ep, ebuf, gettext( 14087c478bd9Sstevel@tonic-gate "Multiple IPv6 source addresses " 14097c478bd9Sstevel@tonic-gate "for %s of the same scope, using " 1410e3320f40Smarkfen "unspecified source instead.\n"), 14117c478bd9Sstevel@tonic-gate srchp->h_name); 14127c478bd9Sstevel@tonic-gate } else { 14137c478bd9Sstevel@tonic-gate /* 14147c478bd9Sstevel@tonic-gate * If I reach here w/o hitting the 14157c478bd9Sstevel@tonic-gate * previous if statements, I have a 14167c478bd9Sstevel@tonic-gate * single source address for this 14177c478bd9Sstevel@tonic-gate * destination. 14187c478bd9Sstevel@tonic-gate */ 14197c478bd9Sstevel@tonic-gate bcopy(first_match, &sin6->sin6_addr, 14207c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 14217c478bd9Sstevel@tonic-gate } 14227c478bd9Sstevel@tonic-gate } 14237c478bd9Sstevel@tonic-gate } 14247c478bd9Sstevel@tonic-gate 1425e3320f40Smarkfen /* 1426e3320f40Smarkfen * If there are errors at this point there is no 1427e3320f40Smarkfen * point sending anything to PF_KEY. 1428e3320f40Smarkfen */ 1429e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE); 1430e3320f40Smarkfen 14317c478bd9Sstevel@tonic-gate /* Save off a copy for later writing... */ 14327c478bd9Sstevel@tonic-gate msgp = (struct sadb_msg *)buffer; 14337c478bd9Sstevel@tonic-gate bcopy(buffer, savebuf, SADB_64TO8(msgp->sadb_msg_len)); 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate rc = key_write(keysock, buffer, SADB_64TO8(msgp->sadb_msg_len)); 14367c478bd9Sstevel@tonic-gate if (rc == -1) 14377c478bd9Sstevel@tonic-gate Bail("write() to PF_KEY socket (in doaddresses)"); 14387c478bd9Sstevel@tonic-gate 1439*9c2c14abSThejaswini Singarajipura if (in_cluster_mode) { 1440*9c2c14abSThejaswini Singarajipura (void) sendto(cluster_socket, buffer, 1441*9c2c14abSThejaswini Singarajipura SADB_64TO8(msgp->sadb_msg_len), 0, 1442*9c2c14abSThejaswini Singarajipura (struct sockaddr *)&cli_addr, 1443*9c2c14abSThejaswini Singarajipura sizeof (cli_addr)); 1444*9c2c14abSThejaswini Singarajipura } 14457c478bd9Sstevel@tonic-gate /* Blank the key for paranoia's sake. */ 14467c478bd9Sstevel@tonic-gate bzero(buffer, buffer_size); 14477c478bd9Sstevel@tonic-gate time_critical_enter(); 14487c478bd9Sstevel@tonic-gate do { 14497c478bd9Sstevel@tonic-gate rc = read(keysock, buffer, buffer_size); 14507c478bd9Sstevel@tonic-gate if (rc == -1) 14517c478bd9Sstevel@tonic-gate Bail("read (in doaddresses)"); 14527c478bd9Sstevel@tonic-gate } while (msgp->sadb_msg_seq != seq || 14537c478bd9Sstevel@tonic-gate msgp->sadb_msg_pid != mypid); 14547c478bd9Sstevel@tonic-gate time_critical_exit(); 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate /* 14577c478bd9Sstevel@tonic-gate * I should _never_ hit the following unless: 14587c478bd9Sstevel@tonic-gate * 14597c478bd9Sstevel@tonic-gate * 1. There is a kernel bug. 14607c478bd9Sstevel@tonic-gate * 2. Another process is mistakenly using my pid in a PF_KEY 14617c478bd9Sstevel@tonic-gate * message. 14627c478bd9Sstevel@tonic-gate */ 14637c478bd9Sstevel@tonic-gate if (msgp->sadb_msg_type != sadb_msg_type || 14647c478bd9Sstevel@tonic-gate msgp->sadb_msg_satype != sadb_msg_satype) { 14657c478bd9Sstevel@tonic-gate syslog((LOG_NOTICE|LOG_AUTH), gettext( 14667c478bd9Sstevel@tonic-gate "doaddresses: Unexpected returned message " 14677c478bd9Sstevel@tonic-gate "(%d exp %d)\n"), msgp->sadb_msg_type, 14687c478bd9Sstevel@tonic-gate sadb_msg_type); 14697c478bd9Sstevel@tonic-gate Bail("doaddresses: Unexpected returned message"); 14707c478bd9Sstevel@tonic-gate } 14717c478bd9Sstevel@tonic-gate 14727c478bd9Sstevel@tonic-gate if (msgp->sadb_msg_errno != 0) { 14737c478bd9Sstevel@tonic-gate char addrprint[INET6_ADDRSTRLEN]; 14747c478bd9Sstevel@tonic-gate int on_errno = 0; 14757c478bd9Sstevel@tonic-gate char *on_errno_msg; 14767c478bd9Sstevel@tonic-gate 14777c478bd9Sstevel@tonic-gate /* 14787c478bd9Sstevel@tonic-gate * Print different error messages depending 14797c478bd9Sstevel@tonic-gate * on the SADB message type being processed. 14807c478bd9Sstevel@tonic-gate * If we get a ESRCH error for a GET/DELETE 14817c478bd9Sstevel@tonic-gate * messages, we report that the SA does not 14827c478bd9Sstevel@tonic-gate * exist. If we get a EEXIST error for a 14837c478bd9Sstevel@tonic-gate * ADD/UPDATE message, we report that the 14847c478bd9Sstevel@tonic-gate * SA already exists. 14857c478bd9Sstevel@tonic-gate */ 14867c478bd9Sstevel@tonic-gate if (sadb_msg_type == SADB_GET || 14877c478bd9Sstevel@tonic-gate sadb_msg_type == SADB_DELETE) { 14887c478bd9Sstevel@tonic-gate on_errno = ESRCH; 14897c478bd9Sstevel@tonic-gate on_errno_msg = "does not exist"; 14907c478bd9Sstevel@tonic-gate } else if (sadb_msg_type == SADB_ADD || 14917c478bd9Sstevel@tonic-gate sadb_msg_type == SADB_UPDATE) { 14927c478bd9Sstevel@tonic-gate on_errno = EEXIST; 14937c478bd9Sstevel@tonic-gate on_errno_msg = "already exists"; 14947c478bd9Sstevel@tonic-gate } 14957c478bd9Sstevel@tonic-gate 14967c478bd9Sstevel@tonic-gate errno = msgp->sadb_msg_errno; 14977c478bd9Sstevel@tonic-gate if (errno == on_errno) { 1498e3320f40Smarkfen ERROR2(ep, ebuf, gettext( 1499e3320f40Smarkfen "Association (type = %s) " 1500e3320f40Smarkfen "with spi 0x%x and addr\n"), 15017c478bd9Sstevel@tonic-gate rparsesatype(msgp->sadb_msg_satype), 1502e3320f40Smarkfen ntohl(spi)); 1503e3320f40Smarkfen ERROR2(ep, ebuf, "%s %s.\n", 15047c478bd9Sstevel@tonic-gate do_inet_ntop(dsthp->h_addr_list[i], 150525e435e0Spwernau addrprint, sizeof (addrprint)), 15067c478bd9Sstevel@tonic-gate on_errno_msg); 15077c478bd9Sstevel@tonic-gate msgp = (struct sadb_msg *)savebuf; 15087c478bd9Sstevel@tonic-gate bcopy(savebuf, buffer, 15097c478bd9Sstevel@tonic-gate SADB_64TO8(msgp->sadb_msg_len)); 15107c478bd9Sstevel@tonic-gate continue; 15117c478bd9Sstevel@tonic-gate } else { 151238d95a78Smarkfen if (errno == EINVAL || errno == ESRCH) { 1513e3320f40Smarkfen ERROR2(ep, ebuf, gettext( 1514e3320f40Smarkfen "PF_KEY Diagnostic code %u: %s.\n"), 1515e3320f40Smarkfen msgp->sadb_x_msg_diagnostic, 1516e3320f40Smarkfen keysock_diag( 1517e3320f40Smarkfen msgp->sadb_x_msg_diagnostic)); 1518e3320f40Smarkfen } else { 1519e3320f40Smarkfen Bail("return message (in doaddresses)"); 15207c478bd9Sstevel@tonic-gate } 15217c478bd9Sstevel@tonic-gate } 15227c478bd9Sstevel@tonic-gate } 1523e3320f40Smarkfen 15247c478bd9Sstevel@tonic-gate if (cmd == CMD_GET) { 15258810c16bSdanmcd if (msgp->sadb_msg_len > MAX_GET_SIZE) { 1526e3320f40Smarkfen WARN1(ep, ebuf, gettext("WARNING: " 1527e3320f40Smarkfen "SA information bigger than %d bytes.\n"), 15288810c16bSdanmcd SADB_64TO8(MAX_GET_SIZE)); 15297c478bd9Sstevel@tonic-gate } 1530bb3ed8dfSpwernau print_samsg(stdout, buffer, B_FALSE, vflag, nflag); 15317c478bd9Sstevel@tonic-gate } 15327c478bd9Sstevel@tonic-gate 1533e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE); 1534e3320f40Smarkfen 15357c478bd9Sstevel@tonic-gate /* ...and then restore the saved buffer. */ 15367c478bd9Sstevel@tonic-gate msgp = (struct sadb_msg *)savebuf; 15377c478bd9Sstevel@tonic-gate bcopy(savebuf, buffer, SADB_64TO8(msgp->sadb_msg_len)); 1538e3320f40Smarkfen lines_added++; 15397c478bd9Sstevel@tonic-gate } 15407c478bd9Sstevel@tonic-gate 15417c478bd9Sstevel@tonic-gate /* Degenerate case, h_addr_list[0] == NULL. */ 15427c478bd9Sstevel@tonic-gate if (i == 0) 15437c478bd9Sstevel@tonic-gate Bail("Empty destination address list"); 1544e3320f40Smarkfen 1545e3320f40Smarkfen /* 1546e3320f40Smarkfen * free(ebuf) even if there are no errors. 1547e3320f40Smarkfen * handle_errors() won't return here. 1548e3320f40Smarkfen */ 1549e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_TRUE); 15507c478bd9Sstevel@tonic-gate } 15517c478bd9Sstevel@tonic-gate 15527c478bd9Sstevel@tonic-gate /* 15537c478bd9Sstevel@tonic-gate * Perform an add or an update. ADD and UPDATE are similar in the extensions 15547c478bd9Sstevel@tonic-gate * they need. 15557c478bd9Sstevel@tonic-gate */ 15567c478bd9Sstevel@tonic-gate static void 1557e3320f40Smarkfen doaddup(int cmd, int satype, char *argv[], char *ebuf) 15587c478bd9Sstevel@tonic-gate { 15597c478bd9Sstevel@tonic-gate uint64_t *buffer, *nexthdr; 15607c478bd9Sstevel@tonic-gate struct sadb_msg msg; 15617c478bd9Sstevel@tonic-gate struct sadb_sa *assoc = NULL; 156238d95a78Smarkfen struct sadb_x_pair *sadb_pair = NULL; 15638810c16bSdanmcd struct sadb_address *src = NULL, *dst = NULL; 15648810c16bSdanmcd struct sadb_address *isrc = NULL, *idst = NULL; 15657c478bd9Sstevel@tonic-gate struct sadb_address *natt_local = NULL, *natt_remote = NULL; 15667c478bd9Sstevel@tonic-gate struct sadb_key *encrypt = NULL, *auth = NULL; 15677c478bd9Sstevel@tonic-gate struct sadb_ident *srcid = NULL, *dstid = NULL; 15687c478bd9Sstevel@tonic-gate struct sadb_lifetime *hard = NULL, *soft = NULL; /* Current? */ 1569*9c2c14abSThejaswini Singarajipura struct sadb_lifetime *idle = NULL; 1570*9c2c14abSThejaswini Singarajipura struct sadb_x_replay_ctr *replay_ctr = NULL; 15717c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 15727c478bd9Sstevel@tonic-gate /* MLS TODO: Need sensitivity eventually. */ 15738810c16bSdanmcd int next, token, sa_len, alloclen, totallen = sizeof (msg), prefix; 1574a050d7e9Spwernau uint32_t spi = 0; 157538d95a78Smarkfen uint8_t sadb_msg_type; 15768810c16bSdanmcd char *thiscmd, *pstr; 15778810c16bSdanmcd boolean_t readstate = B_FALSE, unspec_src = B_FALSE; 15788810c16bSdanmcd boolean_t alloc_inner = B_FALSE, use_natt = B_FALSE; 15798810c16bSdanmcd struct hostent *srchp = NULL, *dsthp = NULL, *isrchp = NULL, 15808810c16bSdanmcd *idsthp = NULL; 15817c478bd9Sstevel@tonic-gate struct hostent *natt_lhp = NULL, *natt_rhp = NULL; 15828810c16bSdanmcd uint16_t srcport = 0, dstport = 0, natt_lport = 0, natt_rport = 0, 15838810c16bSdanmcd isrcport = 0, idstport = 0; 15848810c16bSdanmcd uint8_t proto = 0, iproto = 0; 1585e3320f40Smarkfen char *ep = NULL; 15867c478bd9Sstevel@tonic-gate 158738d95a78Smarkfen switch (cmd) { 158838d95a78Smarkfen case CMD_ADD: 158938d95a78Smarkfen thiscmd = "add"; 159038d95a78Smarkfen sadb_msg_type = SADB_ADD; 159138d95a78Smarkfen break; 159238d95a78Smarkfen case CMD_UPDATE: 159338d95a78Smarkfen thiscmd = "update"; 159438d95a78Smarkfen sadb_msg_type = SADB_UPDATE; 159538d95a78Smarkfen break; 159638d95a78Smarkfen case CMD_UPDATE_PAIR: 159738d95a78Smarkfen thiscmd = "update-pair"; 159838d95a78Smarkfen sadb_msg_type = SADB_X_UPDATEPAIR; 159938d95a78Smarkfen break; 160038d95a78Smarkfen } 16017c478bd9Sstevel@tonic-gate 160238d95a78Smarkfen msg_init(&msg, sadb_msg_type, (uint8_t)satype); 16037c478bd9Sstevel@tonic-gate /* Assume last element in argv is set to NULL. */ 16047c478bd9Sstevel@tonic-gate do { 16057c478bd9Sstevel@tonic-gate token = parseextval(*argv, &next); 16067c478bd9Sstevel@tonic-gate argv++; 16077c478bd9Sstevel@tonic-gate switch (token) { 16087c478bd9Sstevel@tonic-gate case TOK_EOF: 16097c478bd9Sstevel@tonic-gate /* Do nothing, I'm done. */ 16107c478bd9Sstevel@tonic-gate break; 16117c478bd9Sstevel@tonic-gate case TOK_UNKNOWN: 1612e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 1613e3320f40Smarkfen "Unknown extension field \"%s\" \n"), *(argv - 1)); 16147c478bd9Sstevel@tonic-gate break; 16157c478bd9Sstevel@tonic-gate case TOK_SPI: 161638d95a78Smarkfen case TOK_PAIR_SPI: 16177c478bd9Sstevel@tonic-gate case TOK_REPLAY: 16187c478bd9Sstevel@tonic-gate case TOK_STATE: 16197c478bd9Sstevel@tonic-gate case TOK_AUTHALG: 16207c478bd9Sstevel@tonic-gate case TOK_ENCRALG: 16217c478bd9Sstevel@tonic-gate case TOK_ENCAP: 16227c478bd9Sstevel@tonic-gate /* 16237c478bd9Sstevel@tonic-gate * May want to place this chunk of code in a function. 16247c478bd9Sstevel@tonic-gate * 16257c478bd9Sstevel@tonic-gate * This code checks for duplicate entries on a command 16267c478bd9Sstevel@tonic-gate * line. 16277c478bd9Sstevel@tonic-gate */ 16287c478bd9Sstevel@tonic-gate 16297c478bd9Sstevel@tonic-gate /* Allocate the SADB_EXT_SA extension. */ 16307c478bd9Sstevel@tonic-gate if (assoc == NULL) { 16317c478bd9Sstevel@tonic-gate assoc = malloc(sizeof (*assoc)); 16327c478bd9Sstevel@tonic-gate if (assoc == NULL) 16337c478bd9Sstevel@tonic-gate Bail("malloc(assoc)"); 16347c478bd9Sstevel@tonic-gate bzero(assoc, sizeof (*assoc)); 16357c478bd9Sstevel@tonic-gate assoc->sadb_sa_exttype = SADB_EXT_SA; 16367c478bd9Sstevel@tonic-gate assoc->sadb_sa_len = 16377c478bd9Sstevel@tonic-gate SADB_8TO64(sizeof (*assoc)); 16387c478bd9Sstevel@tonic-gate totallen += sizeof (*assoc); 16397c478bd9Sstevel@tonic-gate } 16407c478bd9Sstevel@tonic-gate switch (token) { 16417c478bd9Sstevel@tonic-gate case TOK_SPI: 16427c478bd9Sstevel@tonic-gate /* 16437c478bd9Sstevel@tonic-gate * If some cretin types in "spi 0" then he/she 16447c478bd9Sstevel@tonic-gate * can type in another SPI. 16457c478bd9Sstevel@tonic-gate */ 16467c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_spi != 0) { 1647e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1648e3320f40Smarkfen "Can only specify " 1649e3320f40Smarkfen "single SPI value.\n")); 1650e3320f40Smarkfen break; 16517c478bd9Sstevel@tonic-gate } 16527c478bd9Sstevel@tonic-gate /* Must convert SPI to network order! */ 16537c478bd9Sstevel@tonic-gate assoc->sadb_sa_spi = 1654e3320f40Smarkfen htonl((uint32_t)parsenum(*argv, B_TRUE, 1655e3320f40Smarkfen ebuf)); 1656e3320f40Smarkfen if (assoc->sadb_sa_spi == 0) { 1657e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1658e3320f40Smarkfen "Invalid SPI value \"0\" .\n")); 1659e3320f40Smarkfen } 16607c478bd9Sstevel@tonic-gate break; 166138d95a78Smarkfen case TOK_PAIR_SPI: 166238d95a78Smarkfen if (cmd == CMD_UPDATE_PAIR) { 166338d95a78Smarkfen ERROR(ep, ebuf, gettext( 166438d95a78Smarkfen "pair-spi can not be used with the " 166538d95a78Smarkfen "\"update-pair\" command.\n")); 166638d95a78Smarkfen } 166738d95a78Smarkfen if (sadb_pair == NULL) { 166838d95a78Smarkfen sadb_pair = malloc(sizeof (*sadb_pair)); 166938d95a78Smarkfen if (assoc == NULL) 167038d95a78Smarkfen Bail("malloc(assoc)"); 167138d95a78Smarkfen bzero(sadb_pair, sizeof (*sadb_pair)); 167238d95a78Smarkfen totallen += sizeof (*sadb_pair); 167338d95a78Smarkfen } 167438d95a78Smarkfen if (sadb_pair->sadb_x_pair_spi != 0) { 167538d95a78Smarkfen ERROR(ep, ebuf, gettext( 167638d95a78Smarkfen "Can only specify " 167738d95a78Smarkfen "single pair SPI value.\n")); 167838d95a78Smarkfen break; 167938d95a78Smarkfen } 168038d95a78Smarkfen /* Must convert SPI to network order! */ 168138d95a78Smarkfen sadb_pair->sadb_x_pair_len = 168238d95a78Smarkfen SADB_8TO64(sizeof (*sadb_pair)); 168338d95a78Smarkfen sadb_pair->sadb_x_pair_exttype = 168438d95a78Smarkfen SADB_X_EXT_PAIR; 168538d95a78Smarkfen sadb_pair->sadb_x_pair_spi = 168638d95a78Smarkfen htonl((uint32_t)parsenum(*argv, B_TRUE, 168738d95a78Smarkfen ebuf)); 168838d95a78Smarkfen if (sadb_pair->sadb_x_pair_spi == 0) { 168938d95a78Smarkfen ERROR(ep, ebuf, gettext( 169038d95a78Smarkfen "Invalid SPI value \"0\" .\n")); 169138d95a78Smarkfen } 169238d95a78Smarkfen assoc->sadb_sa_flags |= 169338d95a78Smarkfen SADB_X_SAFLAGS_PAIRED; 169438d95a78Smarkfen break; 16957c478bd9Sstevel@tonic-gate case TOK_REPLAY: 16967c478bd9Sstevel@tonic-gate /* 16977c478bd9Sstevel@tonic-gate * That same cretin can do the same with 16987c478bd9Sstevel@tonic-gate * replay. 16997c478bd9Sstevel@tonic-gate */ 17007c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_replay != 0) { 1701e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1702e3320f40Smarkfen "Can only specify " 1703e3320f40Smarkfen "single replay window size.\n")); 1704e3320f40Smarkfen break; 17057c478bd9Sstevel@tonic-gate } 17067c478bd9Sstevel@tonic-gate assoc->sadb_sa_replay = 1707e3320f40Smarkfen (uint8_t)parsenum(*argv, B_TRUE, ebuf); 17087c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_replay != 0) { 1709e3320f40Smarkfen WARN(ep, ebuf, gettext( 17107c478bd9Sstevel@tonic-gate "WARNING: Replay with manual" 1711e3320f40Smarkfen " keying considered harmful.\n")); 17127c478bd9Sstevel@tonic-gate } 17137c478bd9Sstevel@tonic-gate break; 17147c478bd9Sstevel@tonic-gate case TOK_STATE: 17157c478bd9Sstevel@tonic-gate /* 17167c478bd9Sstevel@tonic-gate * 0 is an actual state value, LARVAL. This 17177c478bd9Sstevel@tonic-gate * means that one can type in the larval state 17187c478bd9Sstevel@tonic-gate * and then type in another state on the same 17197c478bd9Sstevel@tonic-gate * command line. 17207c478bd9Sstevel@tonic-gate */ 17217c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_state != 0) { 1722e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1723e3320f40Smarkfen "Can only specify " 1724e3320f40Smarkfen "single SA state.\n")); 1725e3320f40Smarkfen break; 17267c478bd9Sstevel@tonic-gate } 1727e3320f40Smarkfen assoc->sadb_sa_state = parsestate(*argv, 1728e3320f40Smarkfen ebuf); 17297c478bd9Sstevel@tonic-gate readstate = B_TRUE; 17307c478bd9Sstevel@tonic-gate break; 17317c478bd9Sstevel@tonic-gate case TOK_AUTHALG: 17327c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_auth != 0) { 1733e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1734e3320f40Smarkfen "Can only specify " 1735e3320f40Smarkfen "single auth algorithm.\n")); 1736e3320f40Smarkfen break; 17377c478bd9Sstevel@tonic-gate } 17387c478bd9Sstevel@tonic-gate assoc->sadb_sa_auth = parsealg(*argv, 1739e3320f40Smarkfen IPSEC_PROTO_AH, ebuf); 17407c478bd9Sstevel@tonic-gate break; 17417c478bd9Sstevel@tonic-gate case TOK_ENCRALG: 17428810c16bSdanmcd if (satype == SADB_SATYPE_AH) { 1743e3320f40Smarkfen ERROR(ep, ebuf, gettext("Cannot specify" 1744e3320f40Smarkfen " encryption with SA type ah.\n")); 1745e3320f40Smarkfen break; 17468810c16bSdanmcd } 17477c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_encrypt != 0) { 1748e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1749e3320f40Smarkfen "Can only specify " 1750e3320f40Smarkfen "single encryption algorithm.\n")); 1751e3320f40Smarkfen break; 17527c478bd9Sstevel@tonic-gate } 17537c478bd9Sstevel@tonic-gate assoc->sadb_sa_encrypt = parsealg(*argv, 1754e3320f40Smarkfen IPSEC_PROTO_ESP, ebuf); 17557c478bd9Sstevel@tonic-gate break; 17567c478bd9Sstevel@tonic-gate case TOK_ENCAP: 17577c478bd9Sstevel@tonic-gate if (use_natt) { 1758e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1759e3320f40Smarkfen "Can only specify single" 1760e3320f40Smarkfen " encapsulation.\n")); 1761e3320f40Smarkfen break; 17627c478bd9Sstevel@tonic-gate } 17637c478bd9Sstevel@tonic-gate if (strncmp(*argv, "udp", 3)) { 1764e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1765e3320f40Smarkfen "Can only specify udp" 1766e3320f40Smarkfen " encapsulation.\n")); 1767e3320f40Smarkfen break; 17687c478bd9Sstevel@tonic-gate } 17697c478bd9Sstevel@tonic-gate use_natt = B_TRUE; 17707c478bd9Sstevel@tonic-gate /* set assoc flags later */ 17717c478bd9Sstevel@tonic-gate break; 17727c478bd9Sstevel@tonic-gate } 17737c478bd9Sstevel@tonic-gate argv++; 17747c478bd9Sstevel@tonic-gate break; 17757c478bd9Sstevel@tonic-gate case TOK_SRCPORT: 17767c478bd9Sstevel@tonic-gate if (srcport != 0) { 1777e3320f40Smarkfen ERROR(ep, ebuf, gettext("Can only specify " 1778e3320f40Smarkfen "single source port.\n")); 1779e3320f40Smarkfen break; 17807c478bd9Sstevel@tonic-gate } 1781e3320f40Smarkfen srcport = parsenum(*argv, B_TRUE, ebuf); 17827c478bd9Sstevel@tonic-gate argv++; 17837c478bd9Sstevel@tonic-gate break; 17847c478bd9Sstevel@tonic-gate case TOK_DSTPORT: 17857c478bd9Sstevel@tonic-gate if (dstport != 0) { 1786e3320f40Smarkfen ERROR(ep, ebuf, gettext("Can only specify " 1787e3320f40Smarkfen "single destination port.\n")); 1788e3320f40Smarkfen break; 17897c478bd9Sstevel@tonic-gate } 1790e3320f40Smarkfen dstport = parsenum(*argv, B_TRUE, ebuf); 17917c478bd9Sstevel@tonic-gate argv++; 17927c478bd9Sstevel@tonic-gate break; 17938810c16bSdanmcd case TOK_ISRCPORT: 17948810c16bSdanmcd alloc_inner = B_TRUE; 17958810c16bSdanmcd if (isrcport != 0) { 1796e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1797e3320f40Smarkfen "Can only specify " 1798e3320f40Smarkfen "single inner-source port.\n")); 1799e3320f40Smarkfen break; 18008810c16bSdanmcd } 1801e3320f40Smarkfen isrcport = parsenum(*argv, B_TRUE, ebuf); 18028810c16bSdanmcd argv++; 18038810c16bSdanmcd break; 18048810c16bSdanmcd case TOK_IDSTPORT: 18058810c16bSdanmcd alloc_inner = B_TRUE; 18068810c16bSdanmcd if (idstport != 0) { 1807e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1808e3320f40Smarkfen "Can only specify " 1809e3320f40Smarkfen "single inner-destination port.\n")); 1810e3320f40Smarkfen break; 18118810c16bSdanmcd } 1812e3320f40Smarkfen idstport = parsenum(*argv, B_TRUE, ebuf); 18138810c16bSdanmcd argv++; 18148810c16bSdanmcd break; 18157c478bd9Sstevel@tonic-gate case TOK_NATLPORT: 18167c478bd9Sstevel@tonic-gate if (natt_lport != 0) { 1817e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1818e3320f40Smarkfen "Can only specify " 1819e3320f40Smarkfen "single NAT-T local port.\n")); 1820e3320f40Smarkfen break; 18217c478bd9Sstevel@tonic-gate } 1822e3320f40Smarkfen natt_lport = parsenum(*argv, B_TRUE, ebuf); 18237c478bd9Sstevel@tonic-gate argv++; 18247c478bd9Sstevel@tonic-gate break; 18257c478bd9Sstevel@tonic-gate case TOK_NATRPORT: 18267c478bd9Sstevel@tonic-gate if (natt_rport != 0) { 1827e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1828e3320f40Smarkfen "Can only specify " 1829e3320f40Smarkfen "single NAT-T remote port.\n")); 1830e3320f40Smarkfen break; 18317c478bd9Sstevel@tonic-gate } 1832e3320f40Smarkfen natt_rport = parsenum(*argv, B_TRUE, ebuf); 18337c478bd9Sstevel@tonic-gate argv++; 18347c478bd9Sstevel@tonic-gate break; 18357c478bd9Sstevel@tonic-gate 18367c478bd9Sstevel@tonic-gate case TOK_PROTO: 18377c478bd9Sstevel@tonic-gate if (proto != 0) { 1838e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1839e3320f40Smarkfen "Can only specify " 1840e3320f40Smarkfen "single protocol.\n")); 1841e3320f40Smarkfen break; 18427c478bd9Sstevel@tonic-gate } 1843e3320f40Smarkfen proto = parsenum(*argv, B_TRUE, ebuf); 18447c478bd9Sstevel@tonic-gate argv++; 18457c478bd9Sstevel@tonic-gate break; 18468810c16bSdanmcd case TOK_IPROTO: 18478810c16bSdanmcd alloc_inner = B_TRUE; 18488810c16bSdanmcd if (iproto != 0) { 1849e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1850e3320f40Smarkfen "Can only specify " 1851e3320f40Smarkfen "single inner protocol.\n")); 1852e3320f40Smarkfen break; 18538810c16bSdanmcd } 1854e3320f40Smarkfen iproto = parsenum(*argv, B_TRUE, ebuf); 18558810c16bSdanmcd argv++; 18568810c16bSdanmcd break; 18577c478bd9Sstevel@tonic-gate case TOK_SRCADDR: 18587c478bd9Sstevel@tonic-gate case TOK_SRCADDR6: 18597c478bd9Sstevel@tonic-gate if (src != NULL) { 1860e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1861e3320f40Smarkfen "Can only specify " 1862e3320f40Smarkfen "single source address.\n")); 1863e3320f40Smarkfen break; 18647c478bd9Sstevel@tonic-gate } 18657c478bd9Sstevel@tonic-gate sa_len = parseaddr(*argv, &srchp, 1866e3320f40Smarkfen (token == TOK_SRCADDR6), ebuf); 1867e3320f40Smarkfen if (srchp == NULL) { 1868e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 1869e3320f40Smarkfen "Unknown src address \"%s\"\n"), *argv); 1870e3320f40Smarkfen break; 1871e3320f40Smarkfen } 18727c478bd9Sstevel@tonic-gate argv++; 18737c478bd9Sstevel@tonic-gate /* 18747c478bd9Sstevel@tonic-gate * Round of the sockaddr length to an 8 byte 18757c478bd9Sstevel@tonic-gate * boundary to make PF_KEY happy. 18767c478bd9Sstevel@tonic-gate */ 18777c478bd9Sstevel@tonic-gate alloclen = sizeof (*src) + roundup(sa_len, 8); 18787c478bd9Sstevel@tonic-gate src = malloc(alloclen); 18797c478bd9Sstevel@tonic-gate if (src == NULL) 18807c478bd9Sstevel@tonic-gate Bail("malloc(src)"); 18817c478bd9Sstevel@tonic-gate totallen += alloclen; 18827c478bd9Sstevel@tonic-gate src->sadb_address_len = SADB_8TO64(alloclen); 18837c478bd9Sstevel@tonic-gate src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; 18847c478bd9Sstevel@tonic-gate src->sadb_address_reserved = 0; 18857c478bd9Sstevel@tonic-gate src->sadb_address_prefixlen = 0; 18867c478bd9Sstevel@tonic-gate src->sadb_address_proto = 0; 18877c478bd9Sstevel@tonic-gate if (srchp == &dummy.he) { 18887c478bd9Sstevel@tonic-gate /* 18897c478bd9Sstevel@tonic-gate * Single address with -n flag. 18907c478bd9Sstevel@tonic-gate */ 18917c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(src + 1); 18927c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 18937c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 18947c478bd9Sstevel@tonic-gate bcopy(srchp->h_addr_list[0], &sin6->sin6_addr, 18957c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 18967c478bd9Sstevel@tonic-gate } 18977c478bd9Sstevel@tonic-gate break; 18987c478bd9Sstevel@tonic-gate case TOK_DSTADDR: 18997c478bd9Sstevel@tonic-gate case TOK_DSTADDR6: 19007c478bd9Sstevel@tonic-gate if (dst != NULL) { 1901e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1902e3320f40Smarkfen "Can only specify single " 1903e3320f40Smarkfen "destination address.\n")); 1904e3320f40Smarkfen break; 19057c478bd9Sstevel@tonic-gate } 19067c478bd9Sstevel@tonic-gate sa_len = parseaddr(*argv, &dsthp, 1907e3320f40Smarkfen (token == TOK_DSTADDR6), ebuf); 1908e3320f40Smarkfen if (dsthp == NULL) { 1909e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 1910e3320f40Smarkfen "Unknown dst address \"%s\"\n"), *argv); 1911e3320f40Smarkfen break; 1912e3320f40Smarkfen } 19137c478bd9Sstevel@tonic-gate argv++; 19147c478bd9Sstevel@tonic-gate alloclen = sizeof (*dst) + roundup(sa_len, 8); 19157c478bd9Sstevel@tonic-gate dst = malloc(alloclen); 19167c478bd9Sstevel@tonic-gate if (dst == NULL) 19177c478bd9Sstevel@tonic-gate Bail("malloc(dst)"); 19187c478bd9Sstevel@tonic-gate totallen += alloclen; 19197c478bd9Sstevel@tonic-gate dst->sadb_address_len = SADB_8TO64(alloclen); 19207c478bd9Sstevel@tonic-gate dst->sadb_address_exttype = SADB_EXT_ADDRESS_DST; 19217c478bd9Sstevel@tonic-gate dst->sadb_address_reserved = 0; 19227c478bd9Sstevel@tonic-gate dst->sadb_address_prefixlen = 0; 19237c478bd9Sstevel@tonic-gate dst->sadb_address_proto = 0; 19247c478bd9Sstevel@tonic-gate if (dsthp == &dummy.he) { 19257c478bd9Sstevel@tonic-gate /* 19267c478bd9Sstevel@tonic-gate * Single address with -n flag. 19277c478bd9Sstevel@tonic-gate */ 19287c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(dst + 1); 19297c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 19307c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 19317c478bd9Sstevel@tonic-gate bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr, 19327c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 19337c478bd9Sstevel@tonic-gate } 19347c478bd9Sstevel@tonic-gate break; 19357c478bd9Sstevel@tonic-gate case TOK_PROXYADDR: 19367c478bd9Sstevel@tonic-gate case TOK_PROXYADDR6: 19378810c16bSdanmcd if (isrc != NULL) { 1938e3320f40Smarkfen ERROR(ep, ebuf, gettext( 1939e3320f40Smarkfen "Can only specify single " 1940e3320f40Smarkfen "proxy/inner-source address.\n")); 1941e3320f40Smarkfen break; 19427c478bd9Sstevel@tonic-gate } 19438810c16bSdanmcd if ((pstr = strchr(*argv, '/')) != NULL) { 19448810c16bSdanmcd /* Parse out the prefix. */ 19458810c16bSdanmcd errno = 0; 19468810c16bSdanmcd prefix = strtol(pstr + 1, NULL, 10); 19478810c16bSdanmcd if (errno != 0) { 1948e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 1949e3320f40Smarkfen "Invalid prefix %s."), pstr); 1950e3320f40Smarkfen break; 19518810c16bSdanmcd } 19528810c16bSdanmcd /* Recycle pstr */ 19538810c16bSdanmcd alloclen = (int)(pstr - *argv); 19548810c16bSdanmcd pstr = malloc(alloclen + 1); 19558810c16bSdanmcd if (pstr == NULL) { 19568810c16bSdanmcd Bail("malloc(pstr)"); 19578810c16bSdanmcd } 19588810c16bSdanmcd (void) strlcpy(pstr, *argv, alloclen + 1); 19598810c16bSdanmcd } else { 19608810c16bSdanmcd pstr = *argv; 19618810c16bSdanmcd /* 19628810c16bSdanmcd * Assume mapping to AF_INET6, and we're a host. 19638810c16bSdanmcd * XXX some miscreants may still make classful 19648810c16bSdanmcd * assumptions. If this is a problem, fix it 19658810c16bSdanmcd * here. 19668810c16bSdanmcd */ 19678810c16bSdanmcd prefix = 128; 19688810c16bSdanmcd } 19698810c16bSdanmcd sa_len = parseaddr(pstr, &isrchp, 1970e3320f40Smarkfen (token == TOK_PROXYADDR6), ebuf); 1971e3320f40Smarkfen if (isrchp == NULL) { 1972e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 1973e3320f40Smarkfen "Unknown proxy/inner-source address " 1974e3320f40Smarkfen "\"%s\"\n"), *argv); 1975e3320f40Smarkfen break; 1976e3320f40Smarkfen } 19778810c16bSdanmcd if (pstr != *argv) 19788810c16bSdanmcd free(pstr); 19797c478bd9Sstevel@tonic-gate argv++; 19808810c16bSdanmcd alloclen = sizeof (*isrc) + roundup(sa_len, 8); 19818810c16bSdanmcd isrc = malloc(alloclen); 19828810c16bSdanmcd if (isrc == NULL) 19838810c16bSdanmcd Bail("malloc(isrc)"); 19847c478bd9Sstevel@tonic-gate totallen += alloclen; 19858810c16bSdanmcd isrc->sadb_address_len = SADB_8TO64(alloclen); 19868810c16bSdanmcd isrc->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY; 19878810c16bSdanmcd isrc->sadb_address_reserved = 0; 19888810c16bSdanmcd isrc->sadb_address_prefixlen = prefix; 19898810c16bSdanmcd isrc->sadb_address_proto = 0; 19908810c16bSdanmcd if (isrchp == &dummy.he || 19918810c16bSdanmcd isrchp->h_addr_list[1] == NULL) { 19927c478bd9Sstevel@tonic-gate /* 19937c478bd9Sstevel@tonic-gate * Single address with -n flag or single name. 19947c478bd9Sstevel@tonic-gate */ 19958810c16bSdanmcd sin6 = (struct sockaddr_in6 *)(isrc + 1); 19967c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 19977c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 19988810c16bSdanmcd bcopy(isrchp->h_addr_list[0], &sin6->sin6_addr, 19997c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 20008810c16bSdanmcd /* 20018810c16bSdanmcd * normalize prefixlen for IPv4-mapped 20028810c16bSdanmcd * addresses. 20038810c16bSdanmcd */ 20048810c16bSdanmcd if (prefix <= 32 && 20058810c16bSdanmcd IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 20068810c16bSdanmcd isrc->sadb_address_prefixlen += 96; 20078810c16bSdanmcd alloc_inner = B_TRUE; 20087c478bd9Sstevel@tonic-gate } else { 20097c478bd9Sstevel@tonic-gate /* 20108810c16bSdanmcd * If the proxy/isrc address is vague, don't 20118810c16bSdanmcd * bother. 20127c478bd9Sstevel@tonic-gate */ 20137c478bd9Sstevel@tonic-gate totallen -= alloclen; 20148810c16bSdanmcd free(isrc); 20158810c16bSdanmcd isrc = NULL; 2016e3320f40Smarkfen WARN1(ep, ebuf, gettext( 2017e3320f40Smarkfen "Proxy/inner-source address %s " 2018e3320f40Smarkfen "is vague, not using.\n"), isrchp->h_name); 20198810c16bSdanmcd freehostent(isrchp); 20208810c16bSdanmcd isrchp = NULL; 2021e3320f40Smarkfen break; 20228810c16bSdanmcd } 20238810c16bSdanmcd break; 20248810c16bSdanmcd case TOK_IDSTADDR: 20258810c16bSdanmcd case TOK_IDSTADDR6: 20268810c16bSdanmcd if (idst != NULL) { 2027e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2028e3320f40Smarkfen "Can only specify single " 2029e3320f40Smarkfen "inner-destination address.\n")); 2030e3320f40Smarkfen break; 20318810c16bSdanmcd } 20328810c16bSdanmcd if ((pstr = strchr(*argv, '/')) != NULL) { 20338810c16bSdanmcd /* Parse out the prefix. */ 20348810c16bSdanmcd errno = 0; 20358810c16bSdanmcd prefix = strtol(pstr + 1, NULL, 10); 20368810c16bSdanmcd if (errno != 0) { 2037e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2038e3320f40Smarkfen "Invalid prefix %s.\n"), pstr); 2039e3320f40Smarkfen break; 20408810c16bSdanmcd } 20418810c16bSdanmcd /* Recycle pstr */ 20428810c16bSdanmcd alloclen = (int)(pstr - *argv); 20438810c16bSdanmcd pstr = malloc(alloclen + 1); 20448810c16bSdanmcd if (pstr == NULL) { 20458810c16bSdanmcd Bail("malloc(pstr)"); 20468810c16bSdanmcd } 20478810c16bSdanmcd (void) strlcpy(pstr, *argv, alloclen + 1); 20488810c16bSdanmcd } else { 20498810c16bSdanmcd pstr = *argv; 20508810c16bSdanmcd /* 20518810c16bSdanmcd * Assume mapping to AF_INET6, and we're a host. 20528810c16bSdanmcd * XXX some miscreants may still make classful 20538810c16bSdanmcd * assumptions. If this is a problem, fix it 20548810c16bSdanmcd * here. 20558810c16bSdanmcd */ 20568810c16bSdanmcd prefix = 128; 20578810c16bSdanmcd } 20588810c16bSdanmcd sa_len = parseaddr(pstr, &idsthp, 2059e3320f40Smarkfen (token == TOK_IDSTADDR6), ebuf); 2060e3320f40Smarkfen if (idsthp == NULL) { 2061e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2062e3320f40Smarkfen "Unknown Inner Src address " 2063e3320f40Smarkfen " \"%s\"\n"), *argv); 2064e3320f40Smarkfen break; 2065e3320f40Smarkfen } 20668810c16bSdanmcd if (pstr != *argv) 20678810c16bSdanmcd free(pstr); 20688810c16bSdanmcd argv++; 20698810c16bSdanmcd alloclen = sizeof (*idst) + roundup(sa_len, 8); 20708810c16bSdanmcd idst = malloc(alloclen); 20718810c16bSdanmcd if (idst == NULL) 20728810c16bSdanmcd Bail("malloc(idst)"); 20738810c16bSdanmcd totallen += alloclen; 20748810c16bSdanmcd idst->sadb_address_len = SADB_8TO64(alloclen); 20758810c16bSdanmcd idst->sadb_address_exttype = 20768810c16bSdanmcd SADB_X_EXT_ADDRESS_INNER_DST; 20778810c16bSdanmcd idst->sadb_address_reserved = 0; 20788810c16bSdanmcd idst->sadb_address_prefixlen = prefix; 20798810c16bSdanmcd idst->sadb_address_proto = 0; 20808810c16bSdanmcd if (idsthp == &dummy.he || 20818810c16bSdanmcd idsthp->h_addr_list[1] == NULL) { 20828810c16bSdanmcd /* 20838810c16bSdanmcd * Single address with -n flag or single name. 20848810c16bSdanmcd */ 20858810c16bSdanmcd sin6 = (struct sockaddr_in6 *)(idst + 1); 20868810c16bSdanmcd bzero(sin6, sizeof (*sin6)); 20878810c16bSdanmcd sin6->sin6_family = AF_INET6; 20888810c16bSdanmcd bcopy(idsthp->h_addr_list[0], &sin6->sin6_addr, 20898810c16bSdanmcd sizeof (struct in6_addr)); 20908810c16bSdanmcd /* 20918810c16bSdanmcd * normalize prefixlen for IPv4-mapped 20928810c16bSdanmcd * addresses. 20938810c16bSdanmcd */ 20948810c16bSdanmcd if (prefix <= 32 && 20958810c16bSdanmcd IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 20968810c16bSdanmcd idst->sadb_address_prefixlen += 96; 20978810c16bSdanmcd alloc_inner = B_TRUE; 20988810c16bSdanmcd } else { 20998810c16bSdanmcd /* 21008810c16bSdanmcd * If the idst address is vague, don't bother. 21018810c16bSdanmcd */ 21028810c16bSdanmcd totallen -= alloclen; 21038810c16bSdanmcd free(idst); 21048810c16bSdanmcd idst = NULL; 2105e3320f40Smarkfen WARN1(ep, ebuf, gettext( 2106e3320f40Smarkfen "Inner destination address %s " 2107e3320f40Smarkfen "is vague, not using.\n"), idsthp->h_name); 21088810c16bSdanmcd freehostent(idsthp); 21098810c16bSdanmcd idsthp = NULL; 2110e3320f40Smarkfen break; 21117c478bd9Sstevel@tonic-gate } 21127c478bd9Sstevel@tonic-gate break; 21137c478bd9Sstevel@tonic-gate case TOK_NATLOC: 21147c478bd9Sstevel@tonic-gate if (natt_local != NULL) { 2115e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2116e3320f40Smarkfen "Can only specify " 2117e3320f40Smarkfen "single NAT-T local address.\n")); 2118e3320f40Smarkfen break; 2119e3320f40Smarkfen } 2120e3320f40Smarkfen sa_len = parseaddr(*argv, &natt_lhp, 0, ebuf); 2121e3320f40Smarkfen if (natt_lhp == NULL) { 2122e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2123e3320f40Smarkfen "Unknown NAT-T local address \"%s\"\n"), 2124e3320f40Smarkfen *argv); 2125e3320f40Smarkfen break; 21267c478bd9Sstevel@tonic-gate } 21277c478bd9Sstevel@tonic-gate argv++; 21287c478bd9Sstevel@tonic-gate /* 21297c478bd9Sstevel@tonic-gate * Round of the sockaddr length to an 8 byte 21307c478bd9Sstevel@tonic-gate * boundary to make PF_KEY happy. 21317c478bd9Sstevel@tonic-gate */ 21327c478bd9Sstevel@tonic-gate alloclen = sizeof (*natt_local) + roundup(sa_len, 8); 21337c478bd9Sstevel@tonic-gate natt_local = malloc(alloclen); 21347c478bd9Sstevel@tonic-gate if (natt_local == NULL) 21357c478bd9Sstevel@tonic-gate Bail("malloc(natt_local)"); 21367c478bd9Sstevel@tonic-gate totallen += alloclen; 21377c478bd9Sstevel@tonic-gate natt_local->sadb_address_len = SADB_8TO64(alloclen); 21387c478bd9Sstevel@tonic-gate natt_local->sadb_address_exttype = 21397c478bd9Sstevel@tonic-gate SADB_X_EXT_ADDRESS_NATT_LOC; 21407c478bd9Sstevel@tonic-gate natt_local->sadb_address_reserved = 0; 21417c478bd9Sstevel@tonic-gate natt_local->sadb_address_prefixlen = 0; 21427c478bd9Sstevel@tonic-gate natt_local->sadb_address_proto = 0; 21438810c16bSdanmcd if (natt_lhp == &dummy.he || 21448810c16bSdanmcd natt_lhp->h_addr_list[1] == NULL) { 21457c478bd9Sstevel@tonic-gate /* 21468810c16bSdanmcd * Single address with -n flag or single name. 21477c478bd9Sstevel@tonic-gate */ 21487c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(natt_local + 1); 21497c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 21507c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 21517c478bd9Sstevel@tonic-gate bcopy(natt_lhp->h_addr_list[0], 21527c478bd9Sstevel@tonic-gate &sin6->sin6_addr, sizeof (struct in6_addr)); 21538810c16bSdanmcd } else { 21548810c16bSdanmcd /* 21558810c16bSdanmcd * If the nat-local address is vague, don't 21568810c16bSdanmcd * bother. 21578810c16bSdanmcd */ 21588810c16bSdanmcd totallen -= alloclen; 21598810c16bSdanmcd free(natt_local); 21608810c16bSdanmcd natt_local = NULL; 2161e3320f40Smarkfen WARN1(ep, ebuf, gettext( 2162e3320f40Smarkfen "NAT-T local address %s " 2163e3320f40Smarkfen "is vague, not using.\n"), 2164e3320f40Smarkfen natt_lhp->h_name); 21658810c16bSdanmcd freehostent(natt_lhp); 21668810c16bSdanmcd natt_lhp = NULL; 2167e3320f40Smarkfen break; 21687c478bd9Sstevel@tonic-gate } 21697c478bd9Sstevel@tonic-gate break; 21707c478bd9Sstevel@tonic-gate case TOK_NATREM: 21717c478bd9Sstevel@tonic-gate if (natt_remote != NULL) { 2172e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2173e3320f40Smarkfen "Can only specify " 2174e3320f40Smarkfen "single NAT-T remote address.\n")); 2175e3320f40Smarkfen break; 2176e3320f40Smarkfen } 2177e3320f40Smarkfen sa_len = parseaddr(*argv, &natt_rhp, 0, ebuf); 2178e3320f40Smarkfen if (natt_rhp == NULL) { 2179e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2180e3320f40Smarkfen "Unknown NAT-T remote address \"%s\"\n"), 2181e3320f40Smarkfen *argv); 2182e3320f40Smarkfen break; 21837c478bd9Sstevel@tonic-gate } 21847c478bd9Sstevel@tonic-gate argv++; 21857c478bd9Sstevel@tonic-gate /* 21867c478bd9Sstevel@tonic-gate * Round of the sockaddr length to an 8 byte 21877c478bd9Sstevel@tonic-gate * boundary to make PF_KEY happy. 21887c478bd9Sstevel@tonic-gate */ 21897c478bd9Sstevel@tonic-gate alloclen = sizeof (*natt_remote) + roundup(sa_len, 8); 21907c478bd9Sstevel@tonic-gate natt_remote = malloc(alloclen); 21917c478bd9Sstevel@tonic-gate if (natt_remote == NULL) 21927c478bd9Sstevel@tonic-gate Bail("malloc(natt_remote)"); 21937c478bd9Sstevel@tonic-gate totallen += alloclen; 21947c478bd9Sstevel@tonic-gate natt_remote->sadb_address_len = SADB_8TO64(alloclen); 21957c478bd9Sstevel@tonic-gate natt_remote->sadb_address_exttype = 21967c478bd9Sstevel@tonic-gate SADB_X_EXT_ADDRESS_NATT_REM; 21977c478bd9Sstevel@tonic-gate natt_remote->sadb_address_reserved = 0; 21987c478bd9Sstevel@tonic-gate natt_remote->sadb_address_prefixlen = 0; 21997c478bd9Sstevel@tonic-gate natt_remote->sadb_address_proto = 0; 22008810c16bSdanmcd if (natt_rhp == &dummy.he || 22018810c16bSdanmcd natt_rhp->h_addr_list[1] == NULL) { 22027c478bd9Sstevel@tonic-gate /* 22038810c16bSdanmcd * Single address with -n flag or single name. 22047c478bd9Sstevel@tonic-gate */ 22057c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(natt_remote + 1); 22067c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 22077c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 22087c478bd9Sstevel@tonic-gate bcopy(natt_rhp->h_addr_list[0], 22097c478bd9Sstevel@tonic-gate &sin6->sin6_addr, sizeof (struct in6_addr)); 22108810c16bSdanmcd } else { 22118810c16bSdanmcd /* 2212e3320f40Smarkfen * If the nat-renote address is vague, don't 22138810c16bSdanmcd * bother. 22148810c16bSdanmcd */ 22158810c16bSdanmcd totallen -= alloclen; 22168810c16bSdanmcd free(natt_remote); 22178810c16bSdanmcd natt_remote = NULL; 2218e3320f40Smarkfen WARN1(ep, ebuf, gettext( 2219e3320f40Smarkfen "NAT-T remote address %s " 2220e3320f40Smarkfen "is vague, not using.\n"), 2221e3320f40Smarkfen natt_rhp->h_name); 22228810c16bSdanmcd freehostent(natt_rhp); 22238810c16bSdanmcd natt_rhp = NULL; 2224e3320f40Smarkfen break; 22257c478bd9Sstevel@tonic-gate } 22267c478bd9Sstevel@tonic-gate break; 22277c478bd9Sstevel@tonic-gate case TOK_ENCRKEY: 22287c478bd9Sstevel@tonic-gate if (encrypt != NULL) { 2229e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2230e3320f40Smarkfen "Can only specify " 2231e3320f40Smarkfen "single encryption key.\n")); 2232e3320f40Smarkfen break; 22337c478bd9Sstevel@tonic-gate } 2234a050d7e9Spwernau if (assoc != NULL && 2235a050d7e9Spwernau assoc->sadb_sa_encrypt == SADB_EALG_NULL) { 2236ec485834Spwernau FATAL(ep, ebuf, gettext( 2237ec485834Spwernau "Cannot specify a key with NULL " 2238ec485834Spwernau "encryption algorithm.\n")); 2239ec485834Spwernau break; 2240ec485834Spwernau } 2241e3320f40Smarkfen encrypt = parsekey(*argv, ebuf); 22427c478bd9Sstevel@tonic-gate argv++; 2243e3320f40Smarkfen if (encrypt == NULL) { 2244e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2245e3320f40Smarkfen "Invalid encryption key.\n")); 2246e3320f40Smarkfen break; 2247e3320f40Smarkfen } 2248e3320f40Smarkfen totallen += SADB_64TO8(encrypt->sadb_key_len); 22497c478bd9Sstevel@tonic-gate encrypt->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT; 22507c478bd9Sstevel@tonic-gate break; 22517c478bd9Sstevel@tonic-gate case TOK_AUTHKEY: 22527c478bd9Sstevel@tonic-gate if (auth != NULL) { 2253e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2254e3320f40Smarkfen "Can only specify single" 2255e3320f40Smarkfen " authentication key.\n")); 2256e3320f40Smarkfen break; 22577c478bd9Sstevel@tonic-gate } 2258e3320f40Smarkfen auth = parsekey(*argv, ebuf); 22597c478bd9Sstevel@tonic-gate argv++; 2260e3320f40Smarkfen if (auth == NULL) { 2261e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2262e3320f40Smarkfen "Invalid authentication key.\n")); 2263e3320f40Smarkfen break; 2264e3320f40Smarkfen } 22657c478bd9Sstevel@tonic-gate totallen += SADB_64TO8(auth->sadb_key_len); 22667c478bd9Sstevel@tonic-gate auth->sadb_key_exttype = SADB_EXT_KEY_AUTH; 22677c478bd9Sstevel@tonic-gate break; 22687c478bd9Sstevel@tonic-gate case TOK_SRCIDTYPE: 22697c478bd9Sstevel@tonic-gate if (*argv == NULL || *(argv + 1) == NULL) { 2270e3320f40Smarkfen FATAL(ep, ebuf, gettext( 2271e3320f40Smarkfen "Unexpected end of command " 2272e3320f40Smarkfen "line - Expecting Src Type.\n")); 2273e3320f40Smarkfen /* NOTREACHED */ 2274e3320f40Smarkfen break; 22757c478bd9Sstevel@tonic-gate } 22767c478bd9Sstevel@tonic-gate if (srcid != NULL) { 2277e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2278e3320f40Smarkfen "Can only specify single" 2279e3320f40Smarkfen " source certificate identity.\n")); 2280e3320f40Smarkfen break; 22817c478bd9Sstevel@tonic-gate } 22827c478bd9Sstevel@tonic-gate alloclen = sizeof (*srcid) + 22837c478bd9Sstevel@tonic-gate roundup(strlen(*(argv + 1)) + 1, 8); 22847c478bd9Sstevel@tonic-gate srcid = malloc(alloclen); 22857c478bd9Sstevel@tonic-gate if (srcid == NULL) 22867c478bd9Sstevel@tonic-gate Bail("malloc(srcid)"); 22877c478bd9Sstevel@tonic-gate totallen += alloclen; 2288e3320f40Smarkfen srcid->sadb_ident_type = parseidtype(*argv, ebuf); 22897c478bd9Sstevel@tonic-gate argv++; 22907c478bd9Sstevel@tonic-gate srcid->sadb_ident_len = SADB_8TO64(alloclen); 22917c478bd9Sstevel@tonic-gate srcid->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC; 22927c478bd9Sstevel@tonic-gate srcid->sadb_ident_reserved = 0; 22937c478bd9Sstevel@tonic-gate srcid->sadb_ident_id = 0; /* Not useful here. */ 2294e3320f40Smarkfen (void) strlcpy((char *)(srcid + 1), *argv, alloclen); 22957c478bd9Sstevel@tonic-gate argv++; 22967c478bd9Sstevel@tonic-gate break; 22977c478bd9Sstevel@tonic-gate case TOK_DSTIDTYPE: 22987c478bd9Sstevel@tonic-gate if (*argv == NULL || *(argv + 1) == NULL) { 2299e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2300e3320f40Smarkfen "Unexpected end of command" 2301e3320f40Smarkfen " line - expecting dst type.\n")); 2302e3320f40Smarkfen break; 23037c478bd9Sstevel@tonic-gate } 23047c478bd9Sstevel@tonic-gate if (dstid != NULL) { 2305e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2306e3320f40Smarkfen "Can only specify single destination " 230725e435e0Spwernau "certificate identity.\n")); 2308e3320f40Smarkfen break; 23097c478bd9Sstevel@tonic-gate } 23107c478bd9Sstevel@tonic-gate alloclen = sizeof (*dstid) + 23117c478bd9Sstevel@tonic-gate roundup(strlen(*(argv + 1)) + 1, 8); 23127c478bd9Sstevel@tonic-gate dstid = malloc(alloclen); 23137c478bd9Sstevel@tonic-gate if (dstid == NULL) 23147c478bd9Sstevel@tonic-gate Bail("malloc(dstid)"); 23157c478bd9Sstevel@tonic-gate totallen += alloclen; 2316e3320f40Smarkfen dstid->sadb_ident_type = parseidtype(*argv, ebuf); 23177c478bd9Sstevel@tonic-gate argv++; 23187c478bd9Sstevel@tonic-gate dstid->sadb_ident_len = SADB_8TO64(alloclen); 23197c478bd9Sstevel@tonic-gate dstid->sadb_ident_exttype = SADB_EXT_IDENTITY_DST; 23207c478bd9Sstevel@tonic-gate dstid->sadb_ident_reserved = 0; 23217c478bd9Sstevel@tonic-gate dstid->sadb_ident_id = 0; /* Not useful here. */ 2322e3320f40Smarkfen (void) strlcpy((char *)(dstid + 1), *argv, alloclen); 23237c478bd9Sstevel@tonic-gate argv++; 23247c478bd9Sstevel@tonic-gate break; 23257c478bd9Sstevel@tonic-gate case TOK_HARD_ALLOC: 23267c478bd9Sstevel@tonic-gate case TOK_HARD_BYTES: 23277c478bd9Sstevel@tonic-gate case TOK_HARD_ADDTIME: 23287c478bd9Sstevel@tonic-gate case TOK_HARD_USETIME: 23297c478bd9Sstevel@tonic-gate if (hard == NULL) { 23307c478bd9Sstevel@tonic-gate hard = malloc(sizeof (*hard)); 23317c478bd9Sstevel@tonic-gate if (hard == NULL) 23327c478bd9Sstevel@tonic-gate Bail("malloc(hard_lifetime)"); 23337c478bd9Sstevel@tonic-gate bzero(hard, sizeof (*hard)); 23347c478bd9Sstevel@tonic-gate hard->sadb_lifetime_exttype = 23357c478bd9Sstevel@tonic-gate SADB_EXT_LIFETIME_HARD; 23367c478bd9Sstevel@tonic-gate hard->sadb_lifetime_len = 23377c478bd9Sstevel@tonic-gate SADB_8TO64(sizeof (*hard)); 23387c478bd9Sstevel@tonic-gate totallen += sizeof (*hard); 23397c478bd9Sstevel@tonic-gate } 23407c478bd9Sstevel@tonic-gate switch (token) { 23417c478bd9Sstevel@tonic-gate case TOK_HARD_ALLOC: 23427c478bd9Sstevel@tonic-gate if (hard->sadb_lifetime_allocations != 0) { 2343e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2344e3320f40Smarkfen "Can only specify single" 2345e3320f40Smarkfen " hard allocation limit.\n")); 2346e3320f40Smarkfen break; 23477c478bd9Sstevel@tonic-gate } 23487c478bd9Sstevel@tonic-gate hard->sadb_lifetime_allocations = 2349e3320f40Smarkfen (uint32_t)parsenum(*argv, B_TRUE, ebuf); 23507c478bd9Sstevel@tonic-gate break; 23517c478bd9Sstevel@tonic-gate case TOK_HARD_BYTES: 23527c478bd9Sstevel@tonic-gate if (hard->sadb_lifetime_bytes != 0) { 2353e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2354e3320f40Smarkfen "Can only specify " 2355e3320f40Smarkfen "single hard byte limit.\n")); 2356e3320f40Smarkfen break; 23577c478bd9Sstevel@tonic-gate } 23587c478bd9Sstevel@tonic-gate hard->sadb_lifetime_bytes = parsenum(*argv, 2359e3320f40Smarkfen B_TRUE, ebuf); 23607c478bd9Sstevel@tonic-gate break; 23617c478bd9Sstevel@tonic-gate case TOK_HARD_ADDTIME: 23627c478bd9Sstevel@tonic-gate if (hard->sadb_lifetime_addtime != 0) { 2363e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2364e3320f40Smarkfen "Can only specify " 2365e3320f40Smarkfen "single past-add lifetime.\n")); 2366e3320f40Smarkfen break; 23677c478bd9Sstevel@tonic-gate } 23687c478bd9Sstevel@tonic-gate hard->sadb_lifetime_addtime = parsenum(*argv, 2369e3320f40Smarkfen B_TRUE, ebuf); 23707c478bd9Sstevel@tonic-gate break; 23717c478bd9Sstevel@tonic-gate case TOK_HARD_USETIME: 23727c478bd9Sstevel@tonic-gate if (hard->sadb_lifetime_usetime != 0) { 2373e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2374e3320f40Smarkfen "Can only specify " 2375e3320f40Smarkfen "single past-use lifetime.\n")); 2376e3320f40Smarkfen break; 23777c478bd9Sstevel@tonic-gate } 23787c478bd9Sstevel@tonic-gate hard->sadb_lifetime_usetime = parsenum(*argv, 2379e3320f40Smarkfen B_TRUE, ebuf); 23807c478bd9Sstevel@tonic-gate break; 23817c478bd9Sstevel@tonic-gate } 23827c478bd9Sstevel@tonic-gate argv++; 23837c478bd9Sstevel@tonic-gate break; 23847c478bd9Sstevel@tonic-gate case TOK_SOFT_ALLOC: 23857c478bd9Sstevel@tonic-gate case TOK_SOFT_BYTES: 23867c478bd9Sstevel@tonic-gate case TOK_SOFT_ADDTIME: 23877c478bd9Sstevel@tonic-gate case TOK_SOFT_USETIME: 23887c478bd9Sstevel@tonic-gate if (soft == NULL) { 23897c478bd9Sstevel@tonic-gate soft = malloc(sizeof (*soft)); 23907c478bd9Sstevel@tonic-gate if (soft == NULL) 23917c478bd9Sstevel@tonic-gate Bail("malloc(soft_lifetime)"); 23927c478bd9Sstevel@tonic-gate bzero(soft, sizeof (*soft)); 23937c478bd9Sstevel@tonic-gate soft->sadb_lifetime_exttype = 23947c478bd9Sstevel@tonic-gate SADB_EXT_LIFETIME_SOFT; 23957c478bd9Sstevel@tonic-gate soft->sadb_lifetime_len = 23967c478bd9Sstevel@tonic-gate SADB_8TO64(sizeof (*soft)); 23977c478bd9Sstevel@tonic-gate totallen += sizeof (*soft); 23987c478bd9Sstevel@tonic-gate } 23997c478bd9Sstevel@tonic-gate switch (token) { 24007c478bd9Sstevel@tonic-gate case TOK_SOFT_ALLOC: 24017c478bd9Sstevel@tonic-gate if (soft->sadb_lifetime_allocations != 0) { 2402e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2403e3320f40Smarkfen "Can only specify single" 2404e3320f40Smarkfen " soft allocation limit.\n")); 2405e3320f40Smarkfen break; 24067c478bd9Sstevel@tonic-gate } 24077c478bd9Sstevel@tonic-gate soft->sadb_lifetime_allocations = 2408e3320f40Smarkfen (uint32_t)parsenum(*argv, B_TRUE, ebuf); 24097c478bd9Sstevel@tonic-gate break; 24107c478bd9Sstevel@tonic-gate case TOK_SOFT_BYTES: 24117c478bd9Sstevel@tonic-gate if (soft->sadb_lifetime_bytes != 0) { 2412e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2413e3320f40Smarkfen "Can only specify single" 2414e3320f40Smarkfen " soft byte limit.\n")); 2415e3320f40Smarkfen break; 24167c478bd9Sstevel@tonic-gate } 24177c478bd9Sstevel@tonic-gate soft->sadb_lifetime_bytes = parsenum(*argv, 2418e3320f40Smarkfen B_TRUE, ebuf); 24197c478bd9Sstevel@tonic-gate break; 24207c478bd9Sstevel@tonic-gate case TOK_SOFT_ADDTIME: 24217c478bd9Sstevel@tonic-gate if (soft->sadb_lifetime_addtime != 0) { 2422e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2423e3320f40Smarkfen "Can only specify single" 2424e3320f40Smarkfen " past-add lifetime.\n")); 2425e3320f40Smarkfen break; 24267c478bd9Sstevel@tonic-gate } 24277c478bd9Sstevel@tonic-gate soft->sadb_lifetime_addtime = parsenum(*argv, 2428e3320f40Smarkfen B_TRUE, ebuf); 24297c478bd9Sstevel@tonic-gate break; 24307c478bd9Sstevel@tonic-gate case TOK_SOFT_USETIME: 24317c478bd9Sstevel@tonic-gate if (soft->sadb_lifetime_usetime != 0) { 2432e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2433e3320f40Smarkfen "Can only specify single" 2434e3320f40Smarkfen " past-use lifetime.\n")); 2435e3320f40Smarkfen break; 24367c478bd9Sstevel@tonic-gate } 24377c478bd9Sstevel@tonic-gate soft->sadb_lifetime_usetime = parsenum(*argv, 2438e3320f40Smarkfen B_TRUE, ebuf); 24397c478bd9Sstevel@tonic-gate break; 24407c478bd9Sstevel@tonic-gate } 24417c478bd9Sstevel@tonic-gate argv++; 24427c478bd9Sstevel@tonic-gate break; 244338d95a78Smarkfen case TOK_FLAG_INBOUND: 244438d95a78Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_INBOUND; 244538d95a78Smarkfen break; 244638d95a78Smarkfen case TOK_FLAG_OUTBOUND: 244738d95a78Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_OUTBOUND; 244838d95a78Smarkfen break; 2449*9c2c14abSThejaswini Singarajipura case TOK_REPLAY_VALUE: 2450*9c2c14abSThejaswini Singarajipura if (replay_ctr != NULL) { 2451*9c2c14abSThejaswini Singarajipura ERROR(ep, ebuf, gettext( 2452*9c2c14abSThejaswini Singarajipura "Can only specify single " 2453*9c2c14abSThejaswini Singarajipura "replay value.")); 2454*9c2c14abSThejaswini Singarajipura break; 2455*9c2c14abSThejaswini Singarajipura } 2456*9c2c14abSThejaswini Singarajipura replay_ctr = calloc(1, sizeof (*replay_ctr)); 2457*9c2c14abSThejaswini Singarajipura if (replay_ctr == NULL) { 2458*9c2c14abSThejaswini Singarajipura Bail("malloc(replay value)"); 2459*9c2c14abSThejaswini Singarajipura } 2460*9c2c14abSThejaswini Singarajipura /* 2461*9c2c14abSThejaswini Singarajipura * We currently do not support a 64-bit 2462*9c2c14abSThejaswini Singarajipura * replay value. RFC 4301 will require one, 2463*9c2c14abSThejaswini Singarajipura * however, and we have a field in place when 2464*9c2c14abSThejaswini Singarajipura * 4301 is built. 2465*9c2c14abSThejaswini Singarajipura */ 2466*9c2c14abSThejaswini Singarajipura replay_ctr->sadb_x_rc_exttype = SADB_X_EXT_REPLAY_VALUE; 2467*9c2c14abSThejaswini Singarajipura replay_ctr->sadb_x_rc_len = 2468*9c2c14abSThejaswini Singarajipura SADB_8TO64(sizeof (*replay_ctr)); 2469*9c2c14abSThejaswini Singarajipura totallen += sizeof (*replay_ctr); 2470*9c2c14abSThejaswini Singarajipura replay_ctr->sadb_x_rc_replay32 = (uint32_t)parsenum( 2471*9c2c14abSThejaswini Singarajipura *argv, B_TRUE, ebuf); 2472*9c2c14abSThejaswini Singarajipura argv++; 2473*9c2c14abSThejaswini Singarajipura break; 2474*9c2c14abSThejaswini Singarajipura case TOK_IDLE_ADDTIME: 2475*9c2c14abSThejaswini Singarajipura case TOK_IDLE_USETIME: 2476*9c2c14abSThejaswini Singarajipura if (idle == NULL) { 2477*9c2c14abSThejaswini Singarajipura idle = calloc(1, sizeof (*idle)); 2478*9c2c14abSThejaswini Singarajipura if (idle == NULL) { 2479*9c2c14abSThejaswini Singarajipura Bail("malloc idle lifetime"); 2480*9c2c14abSThejaswini Singarajipura } 2481*9c2c14abSThejaswini Singarajipura idle->sadb_lifetime_exttype = 2482*9c2c14abSThejaswini Singarajipura SADB_X_EXT_LIFETIME_IDLE; 2483*9c2c14abSThejaswini Singarajipura idle->sadb_lifetime_len = 2484*9c2c14abSThejaswini Singarajipura SADB_8TO64(sizeof (*idle)); 2485*9c2c14abSThejaswini Singarajipura totallen += sizeof (*idle); 2486*9c2c14abSThejaswini Singarajipura } 2487*9c2c14abSThejaswini Singarajipura switch (token) { 2488*9c2c14abSThejaswini Singarajipura case TOK_IDLE_ADDTIME: 2489*9c2c14abSThejaswini Singarajipura idle->sadb_lifetime_addtime = 2490*9c2c14abSThejaswini Singarajipura (uint32_t)parsenum(*argv, 2491*9c2c14abSThejaswini Singarajipura B_TRUE, ebuf); 2492*9c2c14abSThejaswini Singarajipura break; 2493*9c2c14abSThejaswini Singarajipura case TOK_IDLE_USETIME: 2494*9c2c14abSThejaswini Singarajipura idle->sadb_lifetime_usetime = 2495*9c2c14abSThejaswini Singarajipura (uint32_t)parsenum(*argv, 2496*9c2c14abSThejaswini Singarajipura B_TRUE, ebuf); 2497*9c2c14abSThejaswini Singarajipura break; 2498*9c2c14abSThejaswini Singarajipura } 2499*9c2c14abSThejaswini Singarajipura argv++; 2500*9c2c14abSThejaswini Singarajipura break; 25017c478bd9Sstevel@tonic-gate default: 2502e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2503e3320f40Smarkfen "Don't use extension %s for add/update.\n"), 25047c478bd9Sstevel@tonic-gate *(argv - 1)); 25057c478bd9Sstevel@tonic-gate break; 25067c478bd9Sstevel@tonic-gate } 25077c478bd9Sstevel@tonic-gate } while (token != TOK_EOF); 25087c478bd9Sstevel@tonic-gate 2509e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE); 2510e3320f40Smarkfen 2511437220cdSdanmcd #define PORT_ONLY_ALLOCATE(af, socktype, exttype, extvar, port) { \ 2512437220cdSdanmcd alloclen = sizeof (sadb_address_t) + roundup(sizeof (socktype), 8); \ 2513437220cdSdanmcd (extvar) = calloc(1, alloclen); \ 2514437220cdSdanmcd if ((extvar) == NULL) { \ 2515437220cdSdanmcd Bail("malloc(implicit port)"); \ 2516437220cdSdanmcd } \ 2517437220cdSdanmcd totallen += alloclen; \ 2518437220cdSdanmcd (extvar)->sadb_address_len = SADB_8TO64(alloclen); \ 2519437220cdSdanmcd (extvar)->sadb_address_exttype = (exttype); \ 2520437220cdSdanmcd /* sin/sin6 has equivalent offsets for ports! */ \ 2521437220cdSdanmcd sin6 = (struct sockaddr_in6 *)((extvar) + 1); \ 2522437220cdSdanmcd sin6->sin6_family = (af); \ 2523437220cdSdanmcd sin6->sin6_port = (port); \ 2524437220cdSdanmcd } 2525437220cdSdanmcd 25268810c16bSdanmcd /* 2527437220cdSdanmcd * If we specify inner ports or NAT ports w/o addresses, we still need 2528437220cdSdanmcd * to allocate. Also, if we have one inner address, we need the 25298810c16bSdanmcd * other, even if we don't specify anything. 25308810c16bSdanmcd */ 2531437220cdSdanmcd if (use_natt) { 2532437220cdSdanmcd if (natt_lport != 0 && natt_local == NULL) { 2533437220cdSdanmcd PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in, 2534437220cdSdanmcd SADB_X_EXT_ADDRESS_NATT_LOC, natt_local, 2535437220cdSdanmcd natt_lport); 2536437220cdSdanmcd } 2537437220cdSdanmcd 2538437220cdSdanmcd if (natt_rport != 0 && natt_remote == NULL) { 2539437220cdSdanmcd PORT_ONLY_ALLOCATE(AF_INET, struct sockaddr_in, 2540437220cdSdanmcd SADB_X_EXT_ADDRESS_NATT_REM, natt_remote, 2541437220cdSdanmcd natt_rport); 2542437220cdSdanmcd } 2543437220cdSdanmcd } else { 2544437220cdSdanmcd if (natt_lport != 0 || natt_rport != 0) { 2545437220cdSdanmcd ERROR(ep, ebuf, gettext("Must specify 'encap udp' " 2546437220cdSdanmcd "with any NAT-T port.\n")); 2547437220cdSdanmcd } else if (natt_local != NULL || natt_remote != NULL) { 2548437220cdSdanmcd ERROR(ep, ebuf, gettext("Must specify 'encap udp' " 2549437220cdSdanmcd "with any NAT-T address.\n")); 25508810c16bSdanmcd } 2551437220cdSdanmcd } 2552437220cdSdanmcd 2553437220cdSdanmcd if (alloc_inner && idst == NULL) { 2554437220cdSdanmcd PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6, 2555437220cdSdanmcd SADB_X_EXT_ADDRESS_INNER_DST, idst, 0); 25568810c16bSdanmcd } 25578810c16bSdanmcd 25588810c16bSdanmcd if (alloc_inner && isrc == NULL) { 2559437220cdSdanmcd PORT_ONLY_ALLOCATE(AF_INET6, struct sockaddr_in6, 2560437220cdSdanmcd SADB_X_EXT_ADDRESS_INNER_SRC, isrc, 0); 25618810c16bSdanmcd } 2562437220cdSdanmcd #undef PORT_ONLY_ALLOCATE 25638810c16bSdanmcd 25647c478bd9Sstevel@tonic-gate /* 25657c478bd9Sstevel@tonic-gate * Okay, so now I have all of the potential extensions! 25667c478bd9Sstevel@tonic-gate * Allocate a single contiguous buffer. Keep in mind that it'll 25677c478bd9Sstevel@tonic-gate * be enough because the key itself will be yanked. 25687c478bd9Sstevel@tonic-gate */ 25697c478bd9Sstevel@tonic-gate 25707c478bd9Sstevel@tonic-gate if (src == NULL && dst != NULL) { 25717c478bd9Sstevel@tonic-gate /* 25727c478bd9Sstevel@tonic-gate * Set explicit unspecified source address. 25737c478bd9Sstevel@tonic-gate */ 25747c478bd9Sstevel@tonic-gate size_t lenbytes = SADB_64TO8(dst->sadb_address_len); 25757c478bd9Sstevel@tonic-gate 25767c478bd9Sstevel@tonic-gate unspec_src = B_TRUE; 25777c478bd9Sstevel@tonic-gate totallen += lenbytes; 25787c478bd9Sstevel@tonic-gate src = malloc(lenbytes); 25797c478bd9Sstevel@tonic-gate if (src == NULL) 25807c478bd9Sstevel@tonic-gate Bail("malloc(implicit src)"); 25817c478bd9Sstevel@tonic-gate /* Confusing, but we're copying from DST to SRC. :) */ 25827c478bd9Sstevel@tonic-gate bcopy(dst, src, lenbytes); 25837c478bd9Sstevel@tonic-gate src->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; 25847c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(src + 1); 25857c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 25867c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 25877c478bd9Sstevel@tonic-gate } 25888810c16bSdanmcd 25897c478bd9Sstevel@tonic-gate msg.sadb_msg_len = SADB_8TO64(totallen); 25907c478bd9Sstevel@tonic-gate 25917c478bd9Sstevel@tonic-gate buffer = malloc(totallen); 25927c478bd9Sstevel@tonic-gate nexthdr = buffer; 25937c478bd9Sstevel@tonic-gate bcopy(&msg, nexthdr, sizeof (msg)); 25947c478bd9Sstevel@tonic-gate nexthdr += SADB_8TO64(sizeof (msg)); 25957c478bd9Sstevel@tonic-gate if (assoc != NULL) { 25967c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_spi == 0) { 2597e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2598e3320f40Smarkfen "The SPI value is missing for " 2599e3320f40Smarkfen "the association you wish to %s.\n"), thiscmd); 26007c478bd9Sstevel@tonic-gate } 26017c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_auth == 0 && assoc->sadb_sa_encrypt == 0 && 260225e435e0Spwernau cmd == CMD_ADD) { 2603e3320f40Smarkfen free(assoc); 2604e3320f40Smarkfen FATAL(ep, ebuf, gettext( 2605e3320f40Smarkfen "Select at least one algorithm " 2606e3320f40Smarkfen "for this add.\n")); 26077c478bd9Sstevel@tonic-gate } 26087c478bd9Sstevel@tonic-gate 26097c478bd9Sstevel@tonic-gate /* Hack to let user specify NULL ESP implicitly. */ 26107c478bd9Sstevel@tonic-gate if (msg.sadb_msg_satype == SADB_SATYPE_ESP && 26117c478bd9Sstevel@tonic-gate assoc->sadb_sa_encrypt == 0) 26127c478bd9Sstevel@tonic-gate assoc->sadb_sa_encrypt = SADB_EALG_NULL; 26137c478bd9Sstevel@tonic-gate 26147c478bd9Sstevel@tonic-gate /* 0 is an actual value. Print a warning if it was entered. */ 26157c478bd9Sstevel@tonic-gate if (assoc->sadb_sa_state == 0) { 2616e3320f40Smarkfen if (readstate) { 2617e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2618e3320f40Smarkfen "WARNING: Cannot set LARVAL SA state.\n")); 2619e3320f40Smarkfen } 26207c478bd9Sstevel@tonic-gate assoc->sadb_sa_state = SADB_SASTATE_MATURE; 26217c478bd9Sstevel@tonic-gate } 26227c478bd9Sstevel@tonic-gate 26237c478bd9Sstevel@tonic-gate if (use_natt) { 26247c478bd9Sstevel@tonic-gate if (natt_remote != NULL) 26257c478bd9Sstevel@tonic-gate assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_REM; 26267c478bd9Sstevel@tonic-gate if (natt_local != NULL) 26277c478bd9Sstevel@tonic-gate assoc->sadb_sa_flags |= SADB_X_SAFLAGS_NATT_LOC; 26287c478bd9Sstevel@tonic-gate } 26297c478bd9Sstevel@tonic-gate 26308810c16bSdanmcd if (alloc_inner) { 26318810c16bSdanmcd /* 26328810c16bSdanmcd * For now, assume RFC 3884's dream of transport-mode 26338810c16bSdanmcd * SAs with inner IP address selectors will not 26348810c16bSdanmcd * happen. 26358810c16bSdanmcd */ 26368810c16bSdanmcd assoc->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL; 26378810c16bSdanmcd if (proto != 0 && proto != IPPROTO_ENCAP && 26388810c16bSdanmcd proto != IPPROTO_IPV6) { 2639e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2640e3320f40Smarkfen "WARNING: Protocol type %d not " 2641e3320f40Smarkfen "for use with Tunnel-Mode SA.\n"), proto); 26428810c16bSdanmcd /* Continue and let PF_KEY scream... */ 26438810c16bSdanmcd } 26448810c16bSdanmcd } 26458810c16bSdanmcd 26467c478bd9Sstevel@tonic-gate bcopy(assoc, nexthdr, SADB_64TO8(assoc->sadb_sa_len)); 26477c478bd9Sstevel@tonic-gate nexthdr += assoc->sadb_sa_len; 26487c478bd9Sstevel@tonic-gate /* Save the SPI for the case of an error. */ 26497c478bd9Sstevel@tonic-gate spi = assoc->sadb_sa_spi; 26507c478bd9Sstevel@tonic-gate free(assoc); 26517c478bd9Sstevel@tonic-gate } else { 2652a050d7e9Spwernau if (spi == 0) 2653a050d7e9Spwernau ERROR1(ep, ebuf, gettext( 2654a050d7e9Spwernau "Need to define SPI for %s.\n"), thiscmd); 2655e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2656e3320f40Smarkfen "Need SA parameters for %s.\n"), thiscmd); 26577c478bd9Sstevel@tonic-gate } 26587c478bd9Sstevel@tonic-gate 265938d95a78Smarkfen if (sadb_pair != NULL) { 266038d95a78Smarkfen if (sadb_pair->sadb_x_pair_spi == 0) { 266138d95a78Smarkfen ERROR1(ep, ebuf, gettext( 266238d95a78Smarkfen "The SPI value is missing for the " 266338d95a78Smarkfen "association you wish to %s.\n"), thiscmd); 266438d95a78Smarkfen } 266538d95a78Smarkfen bcopy(sadb_pair, nexthdr, 266638d95a78Smarkfen SADB_64TO8(sadb_pair->sadb_x_pair_len)); 266738d95a78Smarkfen nexthdr += sadb_pair->sadb_x_pair_len; 266838d95a78Smarkfen free(sadb_pair); 266938d95a78Smarkfen } 267038d95a78Smarkfen 26717c478bd9Sstevel@tonic-gate if (hard != NULL) { 26727c478bd9Sstevel@tonic-gate bcopy(hard, nexthdr, SADB_64TO8(hard->sadb_lifetime_len)); 26737c478bd9Sstevel@tonic-gate nexthdr += hard->sadb_lifetime_len; 26747c478bd9Sstevel@tonic-gate free(hard); 26757c478bd9Sstevel@tonic-gate } 26767c478bd9Sstevel@tonic-gate 26777c478bd9Sstevel@tonic-gate if (soft != NULL) { 26787c478bd9Sstevel@tonic-gate bcopy(soft, nexthdr, SADB_64TO8(soft->sadb_lifetime_len)); 26797c478bd9Sstevel@tonic-gate nexthdr += soft->sadb_lifetime_len; 26807c478bd9Sstevel@tonic-gate free(soft); 26817c478bd9Sstevel@tonic-gate } 26827c478bd9Sstevel@tonic-gate 2683*9c2c14abSThejaswini Singarajipura if (idle != NULL) { 2684*9c2c14abSThejaswini Singarajipura bcopy(idle, nexthdr, SADB_64TO8(idle->sadb_lifetime_len)); 2685*9c2c14abSThejaswini Singarajipura nexthdr += idle->sadb_lifetime_len; 2686*9c2c14abSThejaswini Singarajipura free(idle); 2687*9c2c14abSThejaswini Singarajipura } 2688*9c2c14abSThejaswini Singarajipura 26897c478bd9Sstevel@tonic-gate if (encrypt == NULL && auth == NULL && cmd == CMD_ADD) { 2690e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2691e3320f40Smarkfen "Must have at least one key for an add.\n")); 26927c478bd9Sstevel@tonic-gate } 26937c478bd9Sstevel@tonic-gate 26947c478bd9Sstevel@tonic-gate if (encrypt != NULL) { 26957c478bd9Sstevel@tonic-gate bcopy(encrypt, nexthdr, SADB_64TO8(encrypt->sadb_key_len)); 26967c478bd9Sstevel@tonic-gate nexthdr += encrypt->sadb_key_len; 26977c478bd9Sstevel@tonic-gate bzero(encrypt, SADB_64TO8(encrypt->sadb_key_len)); 26987c478bd9Sstevel@tonic-gate free(encrypt); 26997c478bd9Sstevel@tonic-gate } 27007c478bd9Sstevel@tonic-gate 27017c478bd9Sstevel@tonic-gate if (auth != NULL) { 27027c478bd9Sstevel@tonic-gate bcopy(auth, nexthdr, SADB_64TO8(auth->sadb_key_len)); 27037c478bd9Sstevel@tonic-gate nexthdr += auth->sadb_key_len; 27047c478bd9Sstevel@tonic-gate bzero(auth, SADB_64TO8(auth->sadb_key_len)); 27057c478bd9Sstevel@tonic-gate free(auth); 27067c478bd9Sstevel@tonic-gate } 27077c478bd9Sstevel@tonic-gate 27087c478bd9Sstevel@tonic-gate if (srcid != NULL) { 27097c478bd9Sstevel@tonic-gate bcopy(srcid, nexthdr, SADB_64TO8(srcid->sadb_ident_len)); 27107c478bd9Sstevel@tonic-gate nexthdr += srcid->sadb_ident_len; 27117c478bd9Sstevel@tonic-gate free(srcid); 27127c478bd9Sstevel@tonic-gate } 27137c478bd9Sstevel@tonic-gate 27147c478bd9Sstevel@tonic-gate if (dstid != NULL) { 27157c478bd9Sstevel@tonic-gate bcopy(dstid, nexthdr, SADB_64TO8(dstid->sadb_ident_len)); 27167c478bd9Sstevel@tonic-gate nexthdr += dstid->sadb_ident_len; 27177c478bd9Sstevel@tonic-gate free(dstid); 27187c478bd9Sstevel@tonic-gate } 27197c478bd9Sstevel@tonic-gate 27207c478bd9Sstevel@tonic-gate if (dst != NULL) { 27217c478bd9Sstevel@tonic-gate bcopy(dst, nexthdr, SADB_64TO8(dst->sadb_address_len)); 27227c478bd9Sstevel@tonic-gate free(dst); 27237c478bd9Sstevel@tonic-gate dst = (struct sadb_address *)nexthdr; 27248810c16bSdanmcd dst->sadb_address_proto = proto; 27258810c16bSdanmcd ((struct sockaddr_in6 *)(dst + 1))->sin6_port = htons(dstport); 27267c478bd9Sstevel@tonic-gate nexthdr += dst->sadb_address_len; 27277c478bd9Sstevel@tonic-gate } else { 2728e3320f40Smarkfen FATAL1(ep, ebuf, gettext( 2729e3320f40Smarkfen "Need destination address for %s.\n"), thiscmd); 27307c478bd9Sstevel@tonic-gate } 27317c478bd9Sstevel@tonic-gate 27327c478bd9Sstevel@tonic-gate if (use_natt) { 27337c478bd9Sstevel@tonic-gate if (natt_remote == NULL && natt_local == NULL) { 2734e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2735e3320f40Smarkfen "Must specify NAT-T remote or local address " 2736e3320f40Smarkfen "for UDP encapsulation.\n")); 27377c478bd9Sstevel@tonic-gate } 27387c478bd9Sstevel@tonic-gate 27397c478bd9Sstevel@tonic-gate if (natt_remote != NULL) { 27408810c16bSdanmcd bcopy(natt_remote, nexthdr, 27418810c16bSdanmcd SADB_64TO8(natt_remote->sadb_address_len)); 27427c478bd9Sstevel@tonic-gate free(natt_remote); 27437c478bd9Sstevel@tonic-gate natt_remote = (struct sadb_address *)nexthdr; 27447c478bd9Sstevel@tonic-gate nexthdr += natt_remote->sadb_address_len; 27458810c16bSdanmcd ((struct sockaddr_in6 *)(natt_remote + 1))->sin6_port = 27468810c16bSdanmcd htons(natt_rport); 27477c478bd9Sstevel@tonic-gate } 27488810c16bSdanmcd 27497c478bd9Sstevel@tonic-gate if (natt_local != NULL) { 27507c478bd9Sstevel@tonic-gate bcopy(natt_local, nexthdr, 27517c478bd9Sstevel@tonic-gate SADB_64TO8(natt_local->sadb_address_len)); 27527c478bd9Sstevel@tonic-gate free(natt_local); 27537c478bd9Sstevel@tonic-gate natt_local = (struct sadb_address *)nexthdr; 27547c478bd9Sstevel@tonic-gate nexthdr += natt_local->sadb_address_len; 27558810c16bSdanmcd ((struct sockaddr_in6 *)(natt_local + 1))->sin6_port = 27568810c16bSdanmcd htons(natt_lport); 27577c478bd9Sstevel@tonic-gate } 27587c478bd9Sstevel@tonic-gate } 2759e3320f40Smarkfen 2760e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE); 2761e3320f40Smarkfen 27627c478bd9Sstevel@tonic-gate /* 27637c478bd9Sstevel@tonic-gate * PF_KEY requires a source address extension, even if the source 27647c478bd9Sstevel@tonic-gate * address itself is unspecified. (See "Set explicit unspecified..." 27657c478bd9Sstevel@tonic-gate * code fragment above. Destination reality check was above.) 27667c478bd9Sstevel@tonic-gate */ 27677c478bd9Sstevel@tonic-gate bcopy(src, nexthdr, SADB_64TO8(src->sadb_address_len)); 27687c478bd9Sstevel@tonic-gate free(src); 27697c478bd9Sstevel@tonic-gate src = (struct sadb_address *)nexthdr; 27708810c16bSdanmcd src->sadb_address_proto = proto; 27718810c16bSdanmcd ((struct sockaddr_in6 *)(src + 1))->sin6_port = htons(srcport); 27727c478bd9Sstevel@tonic-gate nexthdr += src->sadb_address_len; 27737c478bd9Sstevel@tonic-gate 27748810c16bSdanmcd if (isrc != NULL) { 27758810c16bSdanmcd bcopy(isrc, nexthdr, SADB_64TO8(isrc->sadb_address_len)); 27768810c16bSdanmcd free(isrc); 27778810c16bSdanmcd isrc = (struct sadb_address *)nexthdr; 27788810c16bSdanmcd isrc->sadb_address_proto = iproto; 27798810c16bSdanmcd ((struct sockaddr_in6 *)(isrc + 1))->sin6_port = 27808810c16bSdanmcd htons(isrcport); 27818810c16bSdanmcd nexthdr += isrc->sadb_address_len; 27827c478bd9Sstevel@tonic-gate } 27837c478bd9Sstevel@tonic-gate 27848810c16bSdanmcd if (idst != NULL) { 27858810c16bSdanmcd bcopy(idst, nexthdr, SADB_64TO8(idst->sadb_address_len)); 27868810c16bSdanmcd free(idst); 27878810c16bSdanmcd idst = (struct sadb_address *)nexthdr; 27888810c16bSdanmcd idst->sadb_address_proto = iproto; 27898810c16bSdanmcd ((struct sockaddr_in6 *)(idst + 1))->sin6_port = 27908810c16bSdanmcd htons(idstport); 27918810c16bSdanmcd nexthdr += idst->sadb_address_len; 27928810c16bSdanmcd } 27937c478bd9Sstevel@tonic-gate 2794*9c2c14abSThejaswini Singarajipura if (replay_ctr != NULL) { 2795*9c2c14abSThejaswini Singarajipura bcopy(replay_ctr, nexthdr, 2796*9c2c14abSThejaswini Singarajipura SADB_64TO8(replay_ctr->sadb_x_rc_len)); 2797*9c2c14abSThejaswini Singarajipura nexthdr += replay_ctr->sadb_x_rc_len; 2798*9c2c14abSThejaswini Singarajipura free(replay_ctr); 2799*9c2c14abSThejaswini Singarajipura } 2800*9c2c14abSThejaswini Singarajipura 280125e435e0Spwernau if (cflag) { 280225e435e0Spwernau /* 280325e435e0Spwernau * Assume the checked cmd would have worked if it was actually 280425e435e0Spwernau * used. doaddresses() will increment lines_added if it 280525e435e0Spwernau * succeeds. 280625e435e0Spwernau */ 280725e435e0Spwernau lines_added++; 280825e435e0Spwernau } else { 280938d95a78Smarkfen doaddresses(sadb_msg_type, satype, 2810e3320f40Smarkfen cmd, srchp, dsthp, src, dst, unspec_src, buffer, totallen, 2811e3320f40Smarkfen spi, ebuf); 2812e3320f40Smarkfen } 28137c478bd9Sstevel@tonic-gate 28148810c16bSdanmcd if (isrchp != NULL && isrchp != &dummy.he) 281525e435e0Spwernau freehostent(isrchp); 28168810c16bSdanmcd if (idsthp != NULL && idsthp != &dummy.he) 281725e435e0Spwernau freehostent(idsthp); 28187c478bd9Sstevel@tonic-gate if (srchp != NULL && srchp != &dummy.he) 281925e435e0Spwernau freehostent(srchp); 28207c478bd9Sstevel@tonic-gate if (dsthp != NULL && dsthp != &dummy.he) 282125e435e0Spwernau freehostent(dsthp); 28227c478bd9Sstevel@tonic-gate if (natt_lhp != NULL && natt_lhp != &dummy.he) 282325e435e0Spwernau freehostent(natt_lhp); 28247c478bd9Sstevel@tonic-gate if (natt_rhp != NULL && natt_rhp != &dummy.he) 282525e435e0Spwernau freehostent(natt_rhp); 2826e3320f40Smarkfen 2827e3320f40Smarkfen free(ebuf); 2828e3320f40Smarkfen free(buffer); 28297c478bd9Sstevel@tonic-gate } 28307c478bd9Sstevel@tonic-gate 28317c478bd9Sstevel@tonic-gate /* 28327c478bd9Sstevel@tonic-gate * DELETE and GET are similar, in that they only need the extensions 28337c478bd9Sstevel@tonic-gate * required to _find_ an SA, and then either delete it or obtain its 28347c478bd9Sstevel@tonic-gate * information. 28357c478bd9Sstevel@tonic-gate */ 28367c478bd9Sstevel@tonic-gate static void 2837e3320f40Smarkfen dodelget(int cmd, int satype, char *argv[], char *ebuf) 28387c478bd9Sstevel@tonic-gate { 28397c478bd9Sstevel@tonic-gate struct sadb_msg *msg = (struct sadb_msg *)get_buffer; 28407c478bd9Sstevel@tonic-gate uint64_t *nextext; 28417c478bd9Sstevel@tonic-gate struct sadb_sa *assoc = NULL; 28427c478bd9Sstevel@tonic-gate struct sadb_address *src = NULL, *dst = NULL; 28437c478bd9Sstevel@tonic-gate int next, token, sa_len; 28447c478bd9Sstevel@tonic-gate char *thiscmd; 28457c478bd9Sstevel@tonic-gate uint32_t spi; 284638d95a78Smarkfen uint8_t sadb_msg_type; 28477c478bd9Sstevel@tonic-gate struct hostent *srchp = NULL, *dsthp = NULL; 28487c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 28497c478bd9Sstevel@tonic-gate boolean_t unspec_src = B_TRUE; 28507c478bd9Sstevel@tonic-gate uint16_t srcport = 0, dstport = 0; 28517c478bd9Sstevel@tonic-gate uint8_t proto = 0; 2852e3320f40Smarkfen char *ep = NULL; 28537c478bd9Sstevel@tonic-gate 28547c478bd9Sstevel@tonic-gate /* Set the first extension header to right past the base message. */ 28557c478bd9Sstevel@tonic-gate nextext = (uint64_t *)(msg + 1); 28567c478bd9Sstevel@tonic-gate bzero(nextext, sizeof (get_buffer) - sizeof (*msg)); 28577c478bd9Sstevel@tonic-gate 285838d95a78Smarkfen switch (cmd) { 285938d95a78Smarkfen case CMD_GET: 286038d95a78Smarkfen thiscmd = "get"; 286138d95a78Smarkfen sadb_msg_type = SADB_GET; 286238d95a78Smarkfen break; 286338d95a78Smarkfen case CMD_DELETE: 286438d95a78Smarkfen thiscmd = "delete"; 286538d95a78Smarkfen sadb_msg_type = SADB_DELETE; 286638d95a78Smarkfen break; 286738d95a78Smarkfen case CMD_DELETE_PAIR: 286838d95a78Smarkfen thiscmd = "delete-pair"; 286938d95a78Smarkfen sadb_msg_type = SADB_X_DELPAIR; 287038d95a78Smarkfen break; 287138d95a78Smarkfen } 287238d95a78Smarkfen 287338d95a78Smarkfen msg_init(msg, sadb_msg_type, (uint8_t)satype); 28747c478bd9Sstevel@tonic-gate 287507b56925Ssommerfe #define ALLOC_ADDR_EXT(ext, exttype) \ 287607b56925Ssommerfe (ext) = (struct sadb_address *)nextext; \ 287707b56925Ssommerfe nextext = (uint64_t *)((ext) + 1); \ 287807b56925Ssommerfe nextext += SADB_8TO64(roundup(sa_len, 8)); \ 287907b56925Ssommerfe (ext)->sadb_address_exttype = exttype; \ 288007b56925Ssommerfe (ext)->sadb_address_len = nextext - ((uint64_t *)ext); 288107b56925Ssommerfe 28827c478bd9Sstevel@tonic-gate /* Assume last element in argv is set to NULL. */ 28837c478bd9Sstevel@tonic-gate do { 28847c478bd9Sstevel@tonic-gate token = parseextval(*argv, &next); 28857c478bd9Sstevel@tonic-gate argv++; 28867c478bd9Sstevel@tonic-gate switch (token) { 28877c478bd9Sstevel@tonic-gate case TOK_EOF: 28887c478bd9Sstevel@tonic-gate /* Do nothing, I'm done. */ 28897c478bd9Sstevel@tonic-gate break; 28907c478bd9Sstevel@tonic-gate case TOK_UNKNOWN: 2891e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2892e3320f40Smarkfen "Unknown extension field \"%s\"\n"), *(argv - 1)); 28937c478bd9Sstevel@tonic-gate break; 28947c478bd9Sstevel@tonic-gate case TOK_SPI: 28957c478bd9Sstevel@tonic-gate if (assoc != NULL) { 2896e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2897e3320f40Smarkfen "Can only specify single SPI value.\n")); 2898e3320f40Smarkfen break; 28997c478bd9Sstevel@tonic-gate } 29007c478bd9Sstevel@tonic-gate assoc = (struct sadb_sa *)nextext; 29017c478bd9Sstevel@tonic-gate nextext = (uint64_t *)(assoc + 1); 29027c478bd9Sstevel@tonic-gate assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc)); 29037c478bd9Sstevel@tonic-gate assoc->sadb_sa_exttype = SADB_EXT_SA; 29047c478bd9Sstevel@tonic-gate assoc->sadb_sa_spi = htonl((uint32_t)parsenum(*argv, 2905e3320f40Smarkfen B_TRUE, ebuf)); 29067c478bd9Sstevel@tonic-gate spi = assoc->sadb_sa_spi; 29077c478bd9Sstevel@tonic-gate argv++; 29087c478bd9Sstevel@tonic-gate break; 29097c478bd9Sstevel@tonic-gate case TOK_SRCPORT: 29107c478bd9Sstevel@tonic-gate if (srcport != 0) { 2911e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2912e3320f40Smarkfen "Can only specify single source port.\n")); 2913e3320f40Smarkfen break; 29147c478bd9Sstevel@tonic-gate } 2915e3320f40Smarkfen srcport = parsenum(*argv, B_TRUE, ebuf); 29167c478bd9Sstevel@tonic-gate argv++; 29177c478bd9Sstevel@tonic-gate break; 29187c478bd9Sstevel@tonic-gate case TOK_DSTPORT: 29197c478bd9Sstevel@tonic-gate if (dstport != 0) { 2920e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2921e3320f40Smarkfen "Can only " 2922e3320f40Smarkfen "specify single destination port.\n")); 2923e3320f40Smarkfen break; 29247c478bd9Sstevel@tonic-gate } 2925e3320f40Smarkfen dstport = parsenum(*argv, B_TRUE, ebuf); 29267c478bd9Sstevel@tonic-gate argv++; 29277c478bd9Sstevel@tonic-gate break; 29287c478bd9Sstevel@tonic-gate case TOK_PROTO: 29297c478bd9Sstevel@tonic-gate if (proto != 0) { 2930e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2931e3320f40Smarkfen "Can only specify single protocol.\n")); 2932e3320f40Smarkfen break; 29337c478bd9Sstevel@tonic-gate } 2934e3320f40Smarkfen proto = parsenum(*argv, B_TRUE, ebuf); 29357c478bd9Sstevel@tonic-gate argv++; 29367c478bd9Sstevel@tonic-gate break; 29377c478bd9Sstevel@tonic-gate case TOK_SRCADDR: 29387c478bd9Sstevel@tonic-gate case TOK_SRCADDR6: 29397c478bd9Sstevel@tonic-gate if (src != NULL) { 2940e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2941e3320f40Smarkfen "Can only specify single source addr.\n")); 2942e3320f40Smarkfen break; 29437c478bd9Sstevel@tonic-gate } 29447c478bd9Sstevel@tonic-gate sa_len = parseaddr(*argv, &srchp, 2945e3320f40Smarkfen (token == TOK_SRCADDR6), ebuf); 2946e3320f40Smarkfen if (srchp == NULL) { 2947e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2948e3320f40Smarkfen "Unknown source address \"%s\"\n"), *argv); 2949e3320f40Smarkfen break; 2950e3320f40Smarkfen } 29517c478bd9Sstevel@tonic-gate argv++; 29527c478bd9Sstevel@tonic-gate 29537c478bd9Sstevel@tonic-gate unspec_src = B_FALSE; 295407b56925Ssommerfe 295507b56925Ssommerfe ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC); 295607b56925Ssommerfe 29577c478bd9Sstevel@tonic-gate if (srchp == &dummy.he) { 29587c478bd9Sstevel@tonic-gate /* 29597c478bd9Sstevel@tonic-gate * Single address with -n flag. 29607c478bd9Sstevel@tonic-gate */ 29617c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(src + 1); 29627c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 29637c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 29647c478bd9Sstevel@tonic-gate bcopy(srchp->h_addr_list[0], &sin6->sin6_addr, 29657c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 29667c478bd9Sstevel@tonic-gate } 29677c478bd9Sstevel@tonic-gate /* The rest is pre-bzeroed for us. */ 29687c478bd9Sstevel@tonic-gate break; 29697c478bd9Sstevel@tonic-gate case TOK_DSTADDR: 29707c478bd9Sstevel@tonic-gate case TOK_DSTADDR6: 29717c478bd9Sstevel@tonic-gate if (dst != NULL) { 2972e3320f40Smarkfen ERROR(ep, ebuf, gettext( 2973e3320f40Smarkfen "Can only specify single destination " 2974e3320f40Smarkfen "address.\n")); 2975e3320f40Smarkfen break; 29767c478bd9Sstevel@tonic-gate } 29777c478bd9Sstevel@tonic-gate sa_len = parseaddr(*argv, &dsthp, 2978e3320f40Smarkfen (token == TOK_SRCADDR6), ebuf); 2979e3320f40Smarkfen if (dsthp == NULL) { 2980e3320f40Smarkfen ERROR1(ep, ebuf, gettext( 2981e3320f40Smarkfen "Unknown destination address \"%s\"\n"), 2982e3320f40Smarkfen *argv); 2983e3320f40Smarkfen break; 2984e3320f40Smarkfen } 29857c478bd9Sstevel@tonic-gate argv++; 29867c478bd9Sstevel@tonic-gate 298707b56925Ssommerfe ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST); 298807b56925Ssommerfe 29897c478bd9Sstevel@tonic-gate if (dsthp == &dummy.he) { 29907c478bd9Sstevel@tonic-gate /* 29917c478bd9Sstevel@tonic-gate * Single address with -n flag. 29927c478bd9Sstevel@tonic-gate */ 29937c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)(dst + 1); 29947c478bd9Sstevel@tonic-gate bzero(sin6, sizeof (*sin6)); 29957c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 29967c478bd9Sstevel@tonic-gate bcopy(dsthp->h_addr_list[0], &sin6->sin6_addr, 29977c478bd9Sstevel@tonic-gate sizeof (struct in6_addr)); 29987c478bd9Sstevel@tonic-gate } 29997c478bd9Sstevel@tonic-gate /* The rest is pre-bzeroed for us. */ 30007c478bd9Sstevel@tonic-gate break; 300138d95a78Smarkfen case TOK_FLAG_INBOUND: 300238d95a78Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_INBOUND; 300338d95a78Smarkfen break; 300438d95a78Smarkfen case TOK_FLAG_OUTBOUND: 300538d95a78Smarkfen assoc->sadb_sa_flags |= SADB_X_SAFLAGS_OUTBOUND; 300638d95a78Smarkfen break; 30077c478bd9Sstevel@tonic-gate default: 3008e3320f40Smarkfen ERROR2(ep, ebuf, gettext( 3009e3320f40Smarkfen "Don't use extension %s for '%s' command.\n"), 3010e3320f40Smarkfen *(argv - 1), thiscmd); 30117c478bd9Sstevel@tonic-gate break; 30127c478bd9Sstevel@tonic-gate } 30137c478bd9Sstevel@tonic-gate } while (token != TOK_EOF); 30147c478bd9Sstevel@tonic-gate 3015e3320f40Smarkfen handle_errors(ep, ebuf, B_TRUE, B_FALSE); 3016e3320f40Smarkfen 301707b56925Ssommerfe if ((srcport != 0) && (src == NULL)) { 301807b56925Ssommerfe ALLOC_ADDR_EXT(src, SADB_EXT_ADDRESS_SRC); 301907b56925Ssommerfe sin6 = (struct sockaddr_in6 *)(src + 1); 30208810c16bSdanmcd src->sadb_address_proto = proto; 302107b56925Ssommerfe bzero(sin6, sizeof (*sin6)); 302207b56925Ssommerfe sin6->sin6_family = AF_INET6; 30238810c16bSdanmcd sin6->sin6_port = htons(srcport); 302407b56925Ssommerfe } 302507b56925Ssommerfe 302607b56925Ssommerfe if ((dstport != 0) && (dst == NULL)) { 302707b56925Ssommerfe ALLOC_ADDR_EXT(dst, SADB_EXT_ADDRESS_DST); 302807b56925Ssommerfe sin6 = (struct sockaddr_in6 *)(dst + 1); 30298810c16bSdanmcd src->sadb_address_proto = proto; 303007b56925Ssommerfe bzero(sin6, sizeof (*sin6)); 303107b56925Ssommerfe sin6->sin6_family = AF_INET6; 30328810c16bSdanmcd sin6->sin6_port = htons(dstport); 303307b56925Ssommerfe } 303407b56925Ssommerfe 30357c478bd9Sstevel@tonic-gate /* So I have enough of the message to send it down! */ 30367c478bd9Sstevel@tonic-gate msg->sadb_msg_len = nextext - get_buffer; 30377c478bd9Sstevel@tonic-gate 3038e3320f40Smarkfen if (assoc == NULL) { 3039e3320f40Smarkfen FATAL1(ep, ebuf, gettext( 3040e3320f40Smarkfen "Need SA parameters for %s.\n"), thiscmd); 3041e3320f40Smarkfen } 3042e3320f40Smarkfen 304325e435e0Spwernau if (cflag) { 304425e435e0Spwernau /* 304525e435e0Spwernau * Assume the checked cmd would have worked if it was actually 304625e435e0Spwernau * used. doaddresses() will increment lines_added if it 304725e435e0Spwernau * succeeds. 304825e435e0Spwernau */ 304925e435e0Spwernau lines_added++; 305025e435e0Spwernau } else { 305138d95a78Smarkfen doaddresses(sadb_msg_type, satype, 3052e3320f40Smarkfen cmd, srchp, dsthp, src, dst, unspec_src, get_buffer, 3053e3320f40Smarkfen sizeof (get_buffer), spi, NULL); 3054e3320f40Smarkfen } 30557c478bd9Sstevel@tonic-gate 30567c478bd9Sstevel@tonic-gate if (srchp != NULL && srchp != &dummy.he) 30577c478bd9Sstevel@tonic-gate freehostent(srchp); 30587c478bd9Sstevel@tonic-gate if (dsthp != NULL && dsthp != &dummy.he) 30597c478bd9Sstevel@tonic-gate freehostent(dsthp); 30607c478bd9Sstevel@tonic-gate } 30617c478bd9Sstevel@tonic-gate 30627c478bd9Sstevel@tonic-gate /* 30637c478bd9Sstevel@tonic-gate * "ipseckey monitor" should exit very gracefully if ^C is tapped. 30647c478bd9Sstevel@tonic-gate */ 30657c478bd9Sstevel@tonic-gate static void 30667c478bd9Sstevel@tonic-gate monitor_catch(int signal) 30677c478bd9Sstevel@tonic-gate { 30687c478bd9Sstevel@tonic-gate errx(signal, gettext("Bailing on signal %d."), signal); 30697c478bd9Sstevel@tonic-gate } 30707c478bd9Sstevel@tonic-gate 30717c478bd9Sstevel@tonic-gate /* 30727c478bd9Sstevel@tonic-gate * Loop forever, listening on PF_KEY messages. 30737c478bd9Sstevel@tonic-gate */ 30747c478bd9Sstevel@tonic-gate static void 30757c478bd9Sstevel@tonic-gate domonitor(boolean_t passive) 30767c478bd9Sstevel@tonic-gate { 30777c478bd9Sstevel@tonic-gate struct sadb_msg *samsg; 30787c478bd9Sstevel@tonic-gate int rc; 30797c478bd9Sstevel@tonic-gate 30807c478bd9Sstevel@tonic-gate /* Catch ^C. */ 30817c478bd9Sstevel@tonic-gate (void) signal(SIGINT, monitor_catch); 30827c478bd9Sstevel@tonic-gate 30837c478bd9Sstevel@tonic-gate samsg = (struct sadb_msg *)get_buffer; 30847c478bd9Sstevel@tonic-gate if (!passive) { 30857c478bd9Sstevel@tonic-gate (void) printf(gettext("Actively")); 30867c478bd9Sstevel@tonic-gate msg_init(samsg, SADB_X_PROMISC, 1); /* Turn ON promisc. */ 30877c478bd9Sstevel@tonic-gate rc = key_write(keysock, samsg, sizeof (*samsg)); 30887c478bd9Sstevel@tonic-gate if (rc == -1) 30897c478bd9Sstevel@tonic-gate Bail("write (SADB_X_PROMISC)"); 30907c478bd9Sstevel@tonic-gate } else { 30917c478bd9Sstevel@tonic-gate (void) printf(gettext("Passively")); 30927c478bd9Sstevel@tonic-gate } 30937c478bd9Sstevel@tonic-gate (void) printf(gettext(" monitoring the PF_KEY socket.\n")); 30947c478bd9Sstevel@tonic-gate 30957c478bd9Sstevel@tonic-gate for (; ; ) { 30967c478bd9Sstevel@tonic-gate /* 30977c478bd9Sstevel@tonic-gate * I assume that read() is non-blocking, and will never 30987c478bd9Sstevel@tonic-gate * return 0. 30997c478bd9Sstevel@tonic-gate */ 31007c478bd9Sstevel@tonic-gate rc = read(keysock, samsg, sizeof (get_buffer)); 31017c478bd9Sstevel@tonic-gate if (rc == -1) 31027c478bd9Sstevel@tonic-gate Bail("read (in domonitor)"); 31037c478bd9Sstevel@tonic-gate (void) printf(gettext("Read %d bytes.\n"), rc); 31047c478bd9Sstevel@tonic-gate /* 31057c478bd9Sstevel@tonic-gate * Q: Should I use the same method of printing as GET does? 31067c478bd9Sstevel@tonic-gate * A: For now, yes. 31077c478bd9Sstevel@tonic-gate */ 3108bb3ed8dfSpwernau print_samsg(stdout, get_buffer, B_TRUE, vflag, nflag); 31097c478bd9Sstevel@tonic-gate (void) putchar('\n'); 31107c478bd9Sstevel@tonic-gate } 31117c478bd9Sstevel@tonic-gate } 31127c478bd9Sstevel@tonic-gate 31137c478bd9Sstevel@tonic-gate /* 31147c478bd9Sstevel@tonic-gate * Either mask or unmask all relevant signals. 31157c478bd9Sstevel@tonic-gate */ 31167c478bd9Sstevel@tonic-gate static void 31177c478bd9Sstevel@tonic-gate mask_signals(boolean_t unmask) 31187c478bd9Sstevel@tonic-gate { 31197c478bd9Sstevel@tonic-gate sigset_t set; 31207c478bd9Sstevel@tonic-gate static sigset_t oset; 31217c478bd9Sstevel@tonic-gate 31227c478bd9Sstevel@tonic-gate if (unmask) { 31237c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oset, NULL); 31247c478bd9Sstevel@tonic-gate } else { 31257c478bd9Sstevel@tonic-gate (void) sigfillset(&set); 31267c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &set, &oset); 31277c478bd9Sstevel@tonic-gate } 31287c478bd9Sstevel@tonic-gate } 31297c478bd9Sstevel@tonic-gate 31307c478bd9Sstevel@tonic-gate /* 31317c478bd9Sstevel@tonic-gate * Assorted functions to print help text. 31327c478bd9Sstevel@tonic-gate */ 31337c478bd9Sstevel@tonic-gate #define puts_tr(s) (void) puts(gettext(s)) 31347c478bd9Sstevel@tonic-gate 31357c478bd9Sstevel@tonic-gate static void 31367c478bd9Sstevel@tonic-gate doattrhelp() 31377c478bd9Sstevel@tonic-gate { 31387c478bd9Sstevel@tonic-gate int i; 31397c478bd9Sstevel@tonic-gate 31407c478bd9Sstevel@tonic-gate puts_tr("\nSA attributes:"); 31417c478bd9Sstevel@tonic-gate 31427c478bd9Sstevel@tonic-gate for (i = 0; tokens[i].string != NULL; i++) { 31437c478bd9Sstevel@tonic-gate if (i%3 == 0) 31447c478bd9Sstevel@tonic-gate (void) printf("\n"); 31457c478bd9Sstevel@tonic-gate (void) printf(" %-15.15s", tokens[i].string); 31467c478bd9Sstevel@tonic-gate } 31477c478bd9Sstevel@tonic-gate (void) printf("\n"); 31487c478bd9Sstevel@tonic-gate } 31497c478bd9Sstevel@tonic-gate 31507c478bd9Sstevel@tonic-gate static void 31517c478bd9Sstevel@tonic-gate dohelpcmd(char *cmds) 31527c478bd9Sstevel@tonic-gate { 31537c478bd9Sstevel@tonic-gate int cmd; 31547c478bd9Sstevel@tonic-gate 31557c478bd9Sstevel@tonic-gate if (strcmp(cmds, "attr") == 0) { 31567c478bd9Sstevel@tonic-gate doattrhelp(); 31577c478bd9Sstevel@tonic-gate return; 31587c478bd9Sstevel@tonic-gate } 31597c478bd9Sstevel@tonic-gate 31607c478bd9Sstevel@tonic-gate cmd = parsecmd(cmds); 31617c478bd9Sstevel@tonic-gate switch (cmd) { 31627c478bd9Sstevel@tonic-gate case CMD_UPDATE: 31637c478bd9Sstevel@tonic-gate puts_tr("update - Update an existing SA"); 31647c478bd9Sstevel@tonic-gate break; 316538d95a78Smarkfen case CMD_UPDATE_PAIR: 316638d95a78Smarkfen puts_tr("update-pair - Update an existing pair of SA's"); 316738d95a78Smarkfen break; 31687c478bd9Sstevel@tonic-gate case CMD_ADD: 31697c478bd9Sstevel@tonic-gate puts_tr("add - Add a new security association (SA)"); 31707c478bd9Sstevel@tonic-gate break; 31717c478bd9Sstevel@tonic-gate case CMD_DELETE: 31727c478bd9Sstevel@tonic-gate puts_tr("delete - Delete an SA"); 31737c478bd9Sstevel@tonic-gate break; 317438d95a78Smarkfen case CMD_DELETE_PAIR: 317538d95a78Smarkfen puts_tr("delete-pair - Delete a pair of SA's"); 317638d95a78Smarkfen break; 31777c478bd9Sstevel@tonic-gate case CMD_GET: 31787c478bd9Sstevel@tonic-gate puts_tr("get - Display an SA"); 31797c478bd9Sstevel@tonic-gate break; 31807c478bd9Sstevel@tonic-gate case CMD_FLUSH: 31817c478bd9Sstevel@tonic-gate puts_tr("flush - Delete all SAs"); 31827c478bd9Sstevel@tonic-gate break; 31837c478bd9Sstevel@tonic-gate case CMD_DUMP: 31847c478bd9Sstevel@tonic-gate puts_tr("dump - Display all SAs"); 31857c478bd9Sstevel@tonic-gate break; 31867c478bd9Sstevel@tonic-gate case CMD_MONITOR: 31877c478bd9Sstevel@tonic-gate puts_tr("monitor - Monitor all PF_KEY reply messages."); 31887c478bd9Sstevel@tonic-gate break; 31897c478bd9Sstevel@tonic-gate case CMD_PMONITOR: 31907c478bd9Sstevel@tonic-gate puts_tr( 31917c478bd9Sstevel@tonic-gate "pmonitor, passive_monitor - Monitor PF_KEY messages that"); 31927c478bd9Sstevel@tonic-gate puts_tr( 31937c478bd9Sstevel@tonic-gate " reply to all PF_KEY sockets."); 31947c478bd9Sstevel@tonic-gate break; 31957c478bd9Sstevel@tonic-gate 31967c478bd9Sstevel@tonic-gate case CMD_QUIT: 31977c478bd9Sstevel@tonic-gate puts_tr("quit, exit - Exit the program"); 31987c478bd9Sstevel@tonic-gate break; 31997c478bd9Sstevel@tonic-gate case CMD_SAVE: 32007c478bd9Sstevel@tonic-gate puts_tr("save - Saves all SAs to a file"); 32017c478bd9Sstevel@tonic-gate break; 32027c478bd9Sstevel@tonic-gate case CMD_HELP: 32037c478bd9Sstevel@tonic-gate puts_tr("help - Display list of commands"); 32047c478bd9Sstevel@tonic-gate puts_tr("help <cmd> - Display help for command"); 32057c478bd9Sstevel@tonic-gate puts_tr("help attr - Display possible SA attributes"); 32067c478bd9Sstevel@tonic-gate break; 32077c478bd9Sstevel@tonic-gate default: 32087c478bd9Sstevel@tonic-gate (void) printf(gettext("%s: Unknown command\n"), cmds); 32097c478bd9Sstevel@tonic-gate break; 32107c478bd9Sstevel@tonic-gate } 32117c478bd9Sstevel@tonic-gate } 32127c478bd9Sstevel@tonic-gate 32137c478bd9Sstevel@tonic-gate 32147c478bd9Sstevel@tonic-gate static void 32157c478bd9Sstevel@tonic-gate dohelp(char *cmds) 32167c478bd9Sstevel@tonic-gate { 32177c478bd9Sstevel@tonic-gate if (cmds != NULL) { 32187c478bd9Sstevel@tonic-gate dohelpcmd(cmds); 32197c478bd9Sstevel@tonic-gate return; 32207c478bd9Sstevel@tonic-gate } 32217c478bd9Sstevel@tonic-gate puts_tr("Commands"); 32227c478bd9Sstevel@tonic-gate puts_tr("--------"); 32237c478bd9Sstevel@tonic-gate puts_tr("?, help - Display this list"); 32247c478bd9Sstevel@tonic-gate puts_tr("help <cmd> - Display help for command"); 32257c478bd9Sstevel@tonic-gate puts_tr("help attr - Display possible SA attributes"); 32267c478bd9Sstevel@tonic-gate puts_tr("quit, exit - Exit the program"); 32277c478bd9Sstevel@tonic-gate puts_tr("monitor - Monitor all PF_KEY reply messages."); 32287c478bd9Sstevel@tonic-gate puts_tr("pmonitor, passive_monitor - Monitor PF_KEY messages that"); 32297c478bd9Sstevel@tonic-gate puts_tr(" reply to all PF_KEY sockets."); 32307c478bd9Sstevel@tonic-gate puts_tr(""); 32317c478bd9Sstevel@tonic-gate puts_tr("The following commands are of the form:"); 32327c478bd9Sstevel@tonic-gate puts_tr(" <command> {SA type} {attribute value}*"); 32337c478bd9Sstevel@tonic-gate puts_tr(""); 32347c478bd9Sstevel@tonic-gate puts_tr("add (interactive only) - Add a new security association (SA)"); 32357c478bd9Sstevel@tonic-gate puts_tr("update (interactive only) - Update an existing SA"); 323638d95a78Smarkfen puts_tr("update-pair (interactive only) - Update an existing SA pair"); 32377c478bd9Sstevel@tonic-gate puts_tr("delete - Delete an SA"); 323838d95a78Smarkfen puts_tr("delete-pair - Delete an SA pair"); 32397c478bd9Sstevel@tonic-gate puts_tr("get - Display an SA"); 32407c478bd9Sstevel@tonic-gate puts_tr("flush - Delete all SAs"); 32417c478bd9Sstevel@tonic-gate puts_tr("dump - Display all SAs"); 32427c478bd9Sstevel@tonic-gate puts_tr("save - Saves all SAs to a file"); 32437c478bd9Sstevel@tonic-gate } 32447c478bd9Sstevel@tonic-gate 32457c478bd9Sstevel@tonic-gate /* 32467c478bd9Sstevel@tonic-gate * "Parse" a command line from argv. 32477c478bd9Sstevel@tonic-gate */ 32487c478bd9Sstevel@tonic-gate static void 324925e435e0Spwernau parseit(int argc, char *argv[], char *ebuf, boolean_t read_cmdfile) 32507c478bd9Sstevel@tonic-gate { 32517c478bd9Sstevel@tonic-gate int cmd, satype; 3252e3320f40Smarkfen char *ep = NULL; 32537c478bd9Sstevel@tonic-gate 32547c478bd9Sstevel@tonic-gate if (argc == 0) 32557c478bd9Sstevel@tonic-gate return; 32567c478bd9Sstevel@tonic-gate cmd = parsecmd(*argv++); 32577c478bd9Sstevel@tonic-gate 325825e435e0Spwernau /* 325925e435e0Spwernau * Some commands loop forever and should only be run from the command 326025e435e0Spwernau * line, they should never be run from a command file as this may 326125e435e0Spwernau * be used at boot time. 326225e435e0Spwernau */ 32637c478bd9Sstevel@tonic-gate switch (cmd) { 32647c478bd9Sstevel@tonic-gate case CMD_HELP: 326525e435e0Spwernau if (read_cmdfile) 326625e435e0Spwernau ERROR(ep, ebuf, gettext("Help not appropriate in " 326725e435e0Spwernau "config file.")); 326825e435e0Spwernau else 326925e435e0Spwernau dohelp(*argv); 32707c478bd9Sstevel@tonic-gate return; 32717c478bd9Sstevel@tonic-gate case CMD_MONITOR: 327225e435e0Spwernau if (read_cmdfile) 327325e435e0Spwernau ERROR(ep, ebuf, gettext("Monitor not appropriate in " 327425e435e0Spwernau "config file.")); 327525e435e0Spwernau else 327625e435e0Spwernau domonitor(B_FALSE); 32777c478bd9Sstevel@tonic-gate break; 32787c478bd9Sstevel@tonic-gate case CMD_PMONITOR: 327925e435e0Spwernau if (read_cmdfile) 328025e435e0Spwernau ERROR(ep, ebuf, gettext("Monitor not appropriate in " 328125e435e0Spwernau "config file.")); 328225e435e0Spwernau else 328325e435e0Spwernau domonitor(B_TRUE); 32847c478bd9Sstevel@tonic-gate break; 32857c478bd9Sstevel@tonic-gate case CMD_QUIT: 3286e3320f40Smarkfen EXIT_OK(NULL); 32877c478bd9Sstevel@tonic-gate } 32887c478bd9Sstevel@tonic-gate 328925e435e0Spwernau handle_errors(ep, ebuf, B_FALSE, B_FALSE); 329025e435e0Spwernau 3291e3320f40Smarkfen satype = parsesatype(*argv, ebuf); 32927c478bd9Sstevel@tonic-gate 32937c478bd9Sstevel@tonic-gate if (satype != SADB_SATYPE_UNSPEC) { 32947c478bd9Sstevel@tonic-gate argv++; 32957c478bd9Sstevel@tonic-gate } else { 32967c478bd9Sstevel@tonic-gate /* 32977c478bd9Sstevel@tonic-gate * You must specify either "all" or a specific SA type 32987c478bd9Sstevel@tonic-gate * for the "save" command. 32997c478bd9Sstevel@tonic-gate */ 33007c478bd9Sstevel@tonic-gate if (cmd == CMD_SAVE) 33017c478bd9Sstevel@tonic-gate if (*argv == NULL) { 3302e3320f40Smarkfen FATAL(ep, ebuf, gettext( 3303e3320f40Smarkfen "Must specify a specific " 3304e3320f40Smarkfen "SA type for save.\n")); 33057c478bd9Sstevel@tonic-gate } else { 33067c478bd9Sstevel@tonic-gate argv++; 33077c478bd9Sstevel@tonic-gate } 33087c478bd9Sstevel@tonic-gate } 33097c478bd9Sstevel@tonic-gate 33107c478bd9Sstevel@tonic-gate switch (cmd) { 33117c478bd9Sstevel@tonic-gate case CMD_FLUSH: 331225e435e0Spwernau if (!cflag) 331325e435e0Spwernau doflush(satype); 331425e435e0Spwernau /* 331525e435e0Spwernau * If this was called because of an entry in a cmd file 331625e435e0Spwernau * then this action needs to be counted to prevent 331725e435e0Spwernau * do_interactive() treating this as an error. 331825e435e0Spwernau */ 331925e435e0Spwernau lines_added++; 33207c478bd9Sstevel@tonic-gate break; 33217c478bd9Sstevel@tonic-gate case CMD_ADD: 33227c478bd9Sstevel@tonic-gate case CMD_UPDATE: 332338d95a78Smarkfen case CMD_UPDATE_PAIR: 33247c478bd9Sstevel@tonic-gate /* 33257c478bd9Sstevel@tonic-gate * NOTE: Shouldn't allow ADDs or UPDATEs with keying material 33267c478bd9Sstevel@tonic-gate * from the command line. 33277c478bd9Sstevel@tonic-gate */ 33287c478bd9Sstevel@tonic-gate if (!interactive) { 33297c478bd9Sstevel@tonic-gate errx(1, gettext( 3330e3320f40Smarkfen "can't do ADD or UPDATE from the command line.\n")); 33317c478bd9Sstevel@tonic-gate } 33327c478bd9Sstevel@tonic-gate if (satype == SADB_SATYPE_UNSPEC) { 3333e3320f40Smarkfen FATAL(ep, ebuf, gettext( 3334e3320f40Smarkfen "Must specify a specific SA type.")); 33357c478bd9Sstevel@tonic-gate /* NOTREACHED */ 33367c478bd9Sstevel@tonic-gate } 33377c478bd9Sstevel@tonic-gate /* Parse for extensions, including keying material. */ 3338e3320f40Smarkfen doaddup(cmd, satype, argv, ebuf); 33397c478bd9Sstevel@tonic-gate break; 33407c478bd9Sstevel@tonic-gate case CMD_DELETE: 334138d95a78Smarkfen case CMD_DELETE_PAIR: 33427c478bd9Sstevel@tonic-gate case CMD_GET: 33437c478bd9Sstevel@tonic-gate if (satype == SADB_SATYPE_UNSPEC) { 3344e3320f40Smarkfen FATAL(ep, ebuf, gettext( 3345e3320f40Smarkfen "Must specify a single SA type.")); 33467c478bd9Sstevel@tonic-gate /* NOTREACHED */ 33477c478bd9Sstevel@tonic-gate } 33487c478bd9Sstevel@tonic-gate /* Parse for bare minimum to locate an SA. */ 3349e3320f40Smarkfen dodelget(cmd, satype, argv, ebuf); 33507c478bd9Sstevel@tonic-gate break; 33517c478bd9Sstevel@tonic-gate case CMD_DUMP: 335225e435e0Spwernau if (read_cmdfile) 335325e435e0Spwernau ERROR(ep, ebuf, gettext("Dump not appropriate in " 335425e435e0Spwernau "config file.")); 335525e435e0Spwernau else 335625e435e0Spwernau dodump(satype, NULL); 33577c478bd9Sstevel@tonic-gate break; 33587c478bd9Sstevel@tonic-gate case CMD_SAVE: 335925e435e0Spwernau if (read_cmdfile) { 336025e435e0Spwernau ERROR(ep, ebuf, gettext("Save not appropriate in " 336125e435e0Spwernau "config file.")); 336225e435e0Spwernau } else { 336325e435e0Spwernau mask_signals(B_FALSE); /* Mask signals */ 336425e435e0Spwernau dodump(satype, opensavefile(argv[0])); 336525e435e0Spwernau mask_signals(B_TRUE); /* Unmask signals */ 336625e435e0Spwernau } 33677c478bd9Sstevel@tonic-gate break; 33687c478bd9Sstevel@tonic-gate default: 3369e3320f40Smarkfen warnx(gettext("Unknown command (%s).\n"), 33707c478bd9Sstevel@tonic-gate *(argv - ((satype == SADB_SATYPE_UNSPEC) ? 1 : 2))); 33717c478bd9Sstevel@tonic-gate usage(); 33727c478bd9Sstevel@tonic-gate } 337325e435e0Spwernau handle_errors(ep, ebuf, B_FALSE, B_FALSE); 33747c478bd9Sstevel@tonic-gate } 33757c478bd9Sstevel@tonic-gate 33767c478bd9Sstevel@tonic-gate int 33777c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 33787c478bd9Sstevel@tonic-gate { 33797c478bd9Sstevel@tonic-gate int ch; 33807c478bd9Sstevel@tonic-gate FILE *infile = stdin, *savefile; 33817c478bd9Sstevel@tonic-gate boolean_t dosave = B_FALSE, readfile = B_FALSE; 3382e3320f40Smarkfen char *configfile = NULL; 338323c73eccSpwernau struct stat sbuf; 3384*9c2c14abSThejaswini Singarajipura int bootflags; 33857c478bd9Sstevel@tonic-gate 33867c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 33877c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 33887c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 33897c478bd9Sstevel@tonic-gate #endif 33907c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 33917c478bd9Sstevel@tonic-gate 3392e3320f40Smarkfen /* 3393e3320f40Smarkfen * Check to see if the command is being run from smf(5). 3394e3320f40Smarkfen */ 3395e3320f40Smarkfen my_fmri = getenv("SMF_FMRI"); 3396e3320f40Smarkfen 33977c478bd9Sstevel@tonic-gate openlog("ipseckey", LOG_CONS, LOG_AUTH); 33987c478bd9Sstevel@tonic-gate if (getuid() != 0) { 3399e3320f40Smarkfen errx(1, "Insufficient privileges to run ipseckey."); 34007c478bd9Sstevel@tonic-gate } 34017c478bd9Sstevel@tonic-gate 34027c478bd9Sstevel@tonic-gate /* umask me to paranoid, I only want to create files read-only */ 34037c478bd9Sstevel@tonic-gate (void) umask((mode_t)00377); 34047c478bd9Sstevel@tonic-gate 3405e3320f40Smarkfen while ((ch = getopt(argc, argv, "pnvf:s:c:")) != EOF) 34067c478bd9Sstevel@tonic-gate switch (ch) { 34077c478bd9Sstevel@tonic-gate case 'p': 34087c478bd9Sstevel@tonic-gate pflag = B_TRUE; 34097c478bd9Sstevel@tonic-gate break; 34107c478bd9Sstevel@tonic-gate case 'n': 34117c478bd9Sstevel@tonic-gate nflag = B_TRUE; 34127c478bd9Sstevel@tonic-gate break; 34137c478bd9Sstevel@tonic-gate case 'v': 34147c478bd9Sstevel@tonic-gate vflag = B_TRUE; 34157c478bd9Sstevel@tonic-gate break; 3416e3320f40Smarkfen case 'c': 3417e3320f40Smarkfen cflag = B_TRUE; 3418e3320f40Smarkfen /* FALLTHRU */ 34197c478bd9Sstevel@tonic-gate case 'f': 34207c478bd9Sstevel@tonic-gate if (dosave) 34217c478bd9Sstevel@tonic-gate usage(); 34227c478bd9Sstevel@tonic-gate infile = fopen(optarg, "r"); 3423e3320f40Smarkfen if (infile == NULL) { 3424e3320f40Smarkfen EXIT_BADCONFIG2("Unable to open configuration " 3425e3320f40Smarkfen "file: %s\n", optarg); 3426e3320f40Smarkfen } 342723c73eccSpwernau /* 342823c73eccSpwernau * Check file permissions/ownership and warn or 342923c73eccSpwernau * fail depending on state of SMF control. 343023c73eccSpwernau */ 343123c73eccSpwernau if (fstat(fileno(infile), &sbuf) == -1) { 343223c73eccSpwernau (void) fclose(infile); 343323c73eccSpwernau EXIT_BADCONFIG2("Unable to stat configuration " 343423c73eccSpwernau "file: %s\n", optarg); 343523c73eccSpwernau } 343623c73eccSpwernau if (INSECURE_PERMS(sbuf)) { 343723c73eccSpwernau if (my_fmri != NULL) { 343823c73eccSpwernau (void) fclose(infile); 343923c73eccSpwernau EXIT_BADCONFIG2("Config file " 344023c73eccSpwernau "%s has insecure permissions.", 344123c73eccSpwernau optarg); 344223c73eccSpwernau } else { 344323c73eccSpwernau (void) fprintf(stderr, "%s %s\n", 344423c73eccSpwernau optarg, gettext( 344523c73eccSpwernau "has insecure permissions, will be " 344623c73eccSpwernau "rejected in permanent config.")); 344723c73eccSpwernau } 344823c73eccSpwernau } 3449e3320f40Smarkfen configfile = strdup(optarg); 34507c478bd9Sstevel@tonic-gate readfile = B_TRUE; 34517c478bd9Sstevel@tonic-gate break; 34527c478bd9Sstevel@tonic-gate case 's': 34537c478bd9Sstevel@tonic-gate if (readfile) 34547c478bd9Sstevel@tonic-gate usage(); 34557c478bd9Sstevel@tonic-gate dosave = B_TRUE; 34567c478bd9Sstevel@tonic-gate savefile = opensavefile(optarg); 34577c478bd9Sstevel@tonic-gate break; 34587c478bd9Sstevel@tonic-gate default: 34597c478bd9Sstevel@tonic-gate usage(); 34607c478bd9Sstevel@tonic-gate } 34617c478bd9Sstevel@tonic-gate 34627c478bd9Sstevel@tonic-gate argc -= optind; 34637c478bd9Sstevel@tonic-gate argv += optind; 34647c478bd9Sstevel@tonic-gate 34657c478bd9Sstevel@tonic-gate mypid = getpid(); 34667c478bd9Sstevel@tonic-gate 34677c478bd9Sstevel@tonic-gate keysock = socket(PF_KEY, SOCK_RAW, PF_KEY_V2); 34687c478bd9Sstevel@tonic-gate 3469e3320f40Smarkfen if (keysock == -1) { 3470e3320f40Smarkfen if (errno == EPERM) { 3471e3320f40Smarkfen EXIT_BADPERM("Insufficient privileges to open " 3472e3320f40Smarkfen "PF_KEY socket.\n"); 3473e3320f40Smarkfen } else { 3474e3320f40Smarkfen /* some other reason */ 3475e3320f40Smarkfen EXIT_FATAL("Opening PF_KEY socket"); 3476e3320f40Smarkfen } 3477e3320f40Smarkfen } 34787c478bd9Sstevel@tonic-gate 3479*9c2c14abSThejaswini Singarajipura if ((_cladm(CL_INITIALIZE, CL_GET_BOOTFLAG, &bootflags) != 0) || 3480*9c2c14abSThejaswini Singarajipura (bootflags & CLUSTER_BOOTED)) { 3481*9c2c14abSThejaswini Singarajipura in_cluster_mode = B_TRUE; 3482*9c2c14abSThejaswini Singarajipura cluster_socket = socket(AF_INET, SOCK_DGRAM, 0); 3483*9c2c14abSThejaswini Singarajipura cli_addr.sin_family = AF_INET; 3484*9c2c14abSThejaswini Singarajipura cli_addr.sin_addr.s_addr = INADDR_LOOPBACK; 3485*9c2c14abSThejaswini Singarajipura cli_addr.sin_port = htons(CLUSTER_UDP_PORT); 3486*9c2c14abSThejaswini Singarajipura } 3487*9c2c14abSThejaswini Singarajipura 34887c478bd9Sstevel@tonic-gate if (dosave) { 34897c478bd9Sstevel@tonic-gate mask_signals(B_FALSE); /* Mask signals */ 34907c478bd9Sstevel@tonic-gate dodump(SADB_SATYPE_UNSPEC, savefile); 34917c478bd9Sstevel@tonic-gate mask_signals(B_TRUE); /* Unmask signals */ 3492e3320f40Smarkfen EXIT_OK(NULL); 34937c478bd9Sstevel@tonic-gate } 34947c478bd9Sstevel@tonic-gate 3495e3320f40Smarkfen /* 3496e3320f40Smarkfen * When run from smf(5) flush any existing SA's first 3497e3320f40Smarkfen * otherwise you will end up in maintenance mode. 3498e3320f40Smarkfen */ 3499e3320f40Smarkfen if ((my_fmri != NULL) && readfile) { 3500e3320f40Smarkfen (void) fprintf(stdout, gettext( 3501e3320f40Smarkfen "Flushing existing SA's before adding new SA's\n")); 3502e3320f40Smarkfen (void) fflush(stdout); 3503e3320f40Smarkfen doflush(SADB_SATYPE_UNSPEC); 3504e3320f40Smarkfen } 35057c478bd9Sstevel@tonic-gate if (infile != stdin || *argv == NULL) { 35067c478bd9Sstevel@tonic-gate /* Go into interactive mode here. */ 3507e3320f40Smarkfen do_interactive(infile, configfile, "ipseckey> ", my_fmri, 3508e3320f40Smarkfen parseit); 35097c478bd9Sstevel@tonic-gate } 351025e435e0Spwernau parseit(argc, argv, NULL, B_FALSE); 35117c478bd9Sstevel@tonic-gate 35127c478bd9Sstevel@tonic-gate return (0); 35137c478bd9Sstevel@tonic-gate } 3514