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