xref: /illumos-gate/usr/src/lib/libipsecutil/common/ipsec_util.h (revision 1edba515a3484e0f74b638b203d462b3112ac84d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22c7777ac8SPaul Wernau  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25f4a6f97eSDan McDonald /*
26bdc560abSJason King  * Copyright 2018 Joyent, Inc.
27f4a6f97eSDan McDonald  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #ifndef	_IPSEC_UTIL_H
307c478bd9Sstevel@tonic-gate #define	_IPSEC_UTIL_H
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Headers and definitions for support functions that are shared by
347c478bd9Sstevel@tonic-gate  * the ipsec utilities ipseckey and ikeadm.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef __cplusplus
387c478bd9Sstevel@tonic-gate extern "C" {
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/socket.h>
438810c16bSdanmcd #include <net/pfkeyv2.h>
448810c16bSdanmcd #include <netinet/in.h>
458810c16bSdanmcd #include <inet/ip.h>
467c478bd9Sstevel@tonic-gate #include <setjmp.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <err.h>
49d362b749Svk199839 #include <errfp.h>
507c478bd9Sstevel@tonic-gate #include <net/pfpolicy.h>
51bfe6f8f5SVladimir Kotal #include <libtecla.h>
527c478bd9Sstevel@tonic-gate 
5345916cd2Sjpk #ifndef A_CNT
547c478bd9Sstevel@tonic-gate /* macros for array manipulation */
557c478bd9Sstevel@tonic-gate #define	A_CNT(arr)	(sizeof (arr)/sizeof (arr[0]))
567c478bd9Sstevel@tonic-gate #define	A_END(arr)	(&arr[A_CNT(arr)])
5745916cd2Sjpk #endif
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* used for file parsing */
607c478bd9Sstevel@tonic-gate #define	NBUF_SIZE	16
617c478bd9Sstevel@tonic-gate #define	COMMENT_CHAR	'#'
627c478bd9Sstevel@tonic-gate #define	CONT_CHAR	'\\'
637c478bd9Sstevel@tonic-gate #define	QUOTE_CHAR	'"'
64bfe6f8f5SVladimir Kotal /*
65bfe6f8f5SVladimir Kotal  * Input buffer size limits maximum line length for both file parsing and
66bfe6f8f5SVladimir Kotal  * interactive mode. 4K chars should be enough even for broad commands and
67bfe6f8f5SVladimir Kotal  * all possible key lenghts of today's symmetric ciphers entered via
68bbf21555SRichard Lowe  * ipseckey(8) which has the most bifurcated grammar from all IPsec commands.
69bfe6f8f5SVladimir Kotal  */
70bfe6f8f5SVladimir Kotal #define	IBUF_SIZE	4096
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* used for command-line parsing */
737c478bd9Sstevel@tonic-gate #define	START_ARG	8
747c478bd9Sstevel@tonic-gate #define	TOO_MANY_ARGS	(START_ARG << 9)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* Return codes for argv/argc vector creation */
777c478bd9Sstevel@tonic-gate #define	TOO_MANY_TOKENS		-3
787c478bd9Sstevel@tonic-gate #define	MEMORY_ALLOCATION	-2
797c478bd9Sstevel@tonic-gate #define	COMMENT_LINE		1
807c478bd9Sstevel@tonic-gate #define	SUCCESS			0
817c478bd9Sstevel@tonic-gate 
82510c3f91SVladimir Kotal /* Flags for {bytecnt,secs}2out() */
83510c3f91SVladimir Kotal #define	SPC_NOSPACES	0x00000000		/* no space prefix/suffix */
84510c3f91SVladimir Kotal #define	SPC_BEGIN	0x00000001		/* put space at the beginning */
85510c3f91SVladimir Kotal #define	SPC_END		0x00000002		/* put space at the end */
86510c3f91SVladimir Kotal #define	SPC_BOTH	SPC_BEGIN|SPC_END	/* print both spaces */
87510c3f91SVladimir Kotal 
88510c3f91SVladimir Kotal /*
89510c3f91SVladimir Kotal  * The following lengths should be sufficient for character buffers passed to
90510c3f91SVladimir Kotal  * bytecnt2str(),secs2str(). This is because the string output of these
91510c3f91SVladimir Kotal  * functions consists of limited number and units. The lengths should be also
92510c3f91SVladimir Kotal  * sufficient for bytecnt2out(),secs2out() functions.
93510c3f91SVladimir Kotal  */
94510c3f91SVladimir Kotal #define	BYTE_STR_SIZE		16
95510c3f91SVladimir Kotal #define	SECS_STR_SIZE		20
96510c3f91SVladimir Kotal 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Time printing defines...
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  * TBUF_SIZE is pretty arbitrary.  Perhaps it shouldn't be.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate #define	TBUF_SIZE	50
1037c478bd9Sstevel@tonic-gate #define	TIME_MAX	LONG_MAX
1047c478bd9Sstevel@tonic-gate 
10523c73eccSpwernau #ifndef INSECURE_PERMS
10623c73eccSpwernau #define	INSECURE_PERMS(sbuf)	(((sbuf).st_uid != 0) || \
10723c73eccSpwernau 	((sbuf).st_mode & S_IRWXG) || ((sbuf).st_mode & S_IRWXO))
10823c73eccSpwernau #endif
10923c73eccSpwernau 
110c7777ac8SPaul Wernau #ifndef PKCS11_TOKSIZE
111c7777ac8SPaul Wernau #define	PKCS11_TOKSIZE 32	/* Fixed length of PKCS#11 token string len. */
112c7777ac8SPaul Wernau #endif
113c7777ac8SPaul Wernau 
1149c2c14abSThejaswini Singarajipura /*
1159c2c14abSThejaswini Singarajipura  * Solaris UDP port used to communicate with the Solaris Cluster
1169c2c14abSThejaswini Singarajipura  * daemon. It is used only when the node is booted in cluster mode.
1179c2c14abSThejaswini Singarajipura  */
1189c2c14abSThejaswini Singarajipura #define	CLUSTER_UDP_PORT	2005
1199c2c14abSThejaswini Singarajipura 
1207c478bd9Sstevel@tonic-gate /* For keyword-lookup tables */
1217c478bd9Sstevel@tonic-gate typedef struct keywdtab {
1227c478bd9Sstevel@tonic-gate 	uint_t	kw_tag;
1237c478bd9Sstevel@tonic-gate 	char	*kw_str;
1247c478bd9Sstevel@tonic-gate } keywdtab_t;
1257c478bd9Sstevel@tonic-gate 
126a1ba8781SMark Fenwick /*
127a1ba8781SMark Fenwick  * These different exit states are designed to give consistant behaviour
128a1ba8781SMark Fenwick  * when a program needs to exit because of an error. These exit_types
129a1ba8781SMark Fenwick  * are used in macros, defined later in this file, which call ipsecutil_exit().
130a1ba8781SMark Fenwick  * What happens when ipsecutil_exit() may differ if the command was started
131bbf21555SRichard Lowe  * on the command line or via smf(7), See ipsecutil_exit() source for details.
132a1ba8781SMark Fenwick  *
133a1ba8781SMark Fenwick  * Note: The calling function should decide what "debug mode" is before calling
134a1ba8781SMark Fenwick  * ipsecutil_exit() with DEBUG_FATAL.
135a1ba8781SMark Fenwick  */
136e3320f40Smarkfen typedef enum exit_type {
137a1ba8781SMark Fenwick 	SERVICE_EXIT_OK,	/* Exit without error. */
138a1ba8781SMark Fenwick 	SERVICE_DEGRADE,	/* A hint that service should be degraded. */
139a1ba8781SMark Fenwick 	SERVICE_BADPERM,	/* A Permission error occured. */
140a1ba8781SMark Fenwick 	SERVICE_BADCONF,	/* Misconfiguration. */
141bbf21555SRichard Lowe 	SERVICE_MAINTAIN,	/* smf(7) to put service in maintenance mode. */
142bbf21555SRichard Lowe 	SERVICE_DISABLE,	/* Tell smf(7) to disable me. */
143a1ba8781SMark Fenwick 	SERVICE_FATAL,		/* Whatever happened is not fixable. */
144bbf21555SRichard Lowe 	SERVICE_RESTART,	/* Tell smf(7) to restart the service. */
145a1ba8781SMark Fenwick 	DEBUG_FATAL		/* Exit in debug mode. */
146e3320f40Smarkfen } exit_type_t;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * Function Prototypes
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Print errno and if cmdline or readfile, exit; if interactive reset state
1547c478bd9Sstevel@tonic-gate  */
155e3320f40Smarkfen extern void ipsecutil_exit(exit_type_t, char *, FILE *, const char *fmt, ...);
1567c478bd9Sstevel@tonic-gate extern void bail(char *);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
159a7485808Smarkfen  * Localization macro - Only to be used from usr/src/cmd because Macros
160a7485808Smarkfen  * are not expanded in usr/src/lib when message catalogs are built.
1617c478bd9Sstevel@tonic-gate  */
162a7485808Smarkfen #define	Bail(s)	bail(dgettext(TEXT_DOMAIN, s))
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * Print caller-supplied, variable-arg error message, then exit if cmdline
1667c478bd9Sstevel@tonic-gate  * or readfile, or reset state if interactive.
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate extern void bail_msg(char *, ...);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * dump_XXX functions produce ASCII output from the passed in data.
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  * Because certain errors need to do this stderr, dump_XXX functions
1747c478bd9Sstevel@tonic-gate  * take a FILE pointer.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate 
177bb3ed8dfSpwernau extern int dump_sockaddr(struct sockaddr *, uint8_t, boolean_t, FILE *,
178bb3ed8dfSpwernau     boolean_t);
1797c478bd9Sstevel@tonic-gate 
180628b0c67SMark Fenwick extern int dump_key(uint8_t *, uint_t, uint_t, FILE *, boolean_t);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate extern int dump_aalg(uint8_t, FILE *);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate extern int dump_ealg(uint8_t, FILE *);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /* return true if sadb string is printable (based on type), false otherwise */
1877c478bd9Sstevel@tonic-gate extern boolean_t dump_sadb_idtype(uint8_t, FILE *, int *);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * do_interactive: Enter a mode where commands are read from a file;
1917c478bd9Sstevel@tonic-gate  * treat stdin special.  infile is the file cmds are read from;
1927c478bd9Sstevel@tonic-gate  * promptstring is the string printed to stdout (if the cmds are
1937c478bd9Sstevel@tonic-gate  * being read from stdin) to prompt for a new command; parseit is
1947c478bd9Sstevel@tonic-gate  * the function to be called to process the command line once it's
1957c478bd9Sstevel@tonic-gate  * been read in and broken up into an argv/argc vector.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /* callback function passed in to do_interactive() */
19925e435e0Spwernau typedef void (*parse_cmdln_fn)(int, char **, char *, boolean_t);
2007c478bd9Sstevel@tonic-gate 
201bfe6f8f5SVladimir Kotal extern void do_interactive(FILE *, char *, char *, char *, parse_cmdln_fn,
202bfe6f8f5SVladimir Kotal     CplMatchFn *);
203e3320f40Smarkfen 
204e3320f40Smarkfen extern uint_t lines_parsed;
205e3320f40Smarkfen extern uint_t lines_added;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /* convert a string to an IKE_PRIV_* constant */
2087c478bd9Sstevel@tonic-gate extern int privstr2num(char *);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /* convert a string to a D_* debug flag */
2117c478bd9Sstevel@tonic-gate extern int dbgstr2num(char *);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /* convert a string of debug strings with +|- delimiters to a debug level */
2147c478bd9Sstevel@tonic-gate extern int parsedbgopts(char *);
2157c478bd9Sstevel@tonic-gate 
216a12f8217Spwernau /*
217f4a6f97eSDan McDonald  * functions to manipulate the IKEv1 kmcookie-label mapping file
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #define	KMCFILE		"/var/run/ipsec_kmc_map"
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Insert a mapping into the file (if it's not already there), given the
2247c478bd9Sstevel@tonic-gate  * new label.  Return the assigned cookie, or -1 on error.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate extern int kmc_insert_mapping(char *);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * Lookup the given cookie and return its corresponding label.  Return
2307c478bd9Sstevel@tonic-gate  * a pointer to the label on success, NULL on error (or if the label is
2317c478bd9Sstevel@tonic-gate  * not found).
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate extern char *kmc_lookup_by_cookie(int);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate  * These globals are declared for us in ipsec_util.c, since it needs to
2377c478bd9Sstevel@tonic-gate  * refer to them also...
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate extern boolean_t nflag;	/* Avoid nameservice? */
2407c478bd9Sstevel@tonic-gate extern boolean_t pflag;	/* Paranoid w.r.t. printing keying material? */
2417c478bd9Sstevel@tonic-gate extern boolean_t interactive;
2427c478bd9Sstevel@tonic-gate extern boolean_t readfile;
2437c478bd9Sstevel@tonic-gate extern uint_t lineno;
2448810c16bSdanmcd extern char numprint[NBUF_SIZE];
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /* For error recovery in interactive or read-file mode. */
2477c478bd9Sstevel@tonic-gate extern jmp_buf env;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * Back-end stuff for getalgby*().
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate #define	INET_IPSECALGSPATH	"/etc/inet/"
2547c478bd9Sstevel@tonic-gate #define	INET_IPSECALGSFILE	(INET_IPSECALGSPATH "ipsecalgs")
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /* To preserve packages delimiters in /etc/inet/ipsecalgs */
2577c478bd9Sstevel@tonic-gate typedef struct ipsecalgs_pkg {
2587c478bd9Sstevel@tonic-gate 	int alg_num;
2597c478bd9Sstevel@tonic-gate 	char *pkg_name;
2607c478bd9Sstevel@tonic-gate } ipsecalgs_pkg_t;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * The cached representation of /etc/inet/ipsecalgs is represented by:
2647c478bd9Sstevel@tonic-gate  * - A dynamically-grown (optionally sorted) array of IPsec protocols
2657c478bd9Sstevel@tonic-gate  * - Each protocol has an array (again, dynamically grown and sorted)
2667c478bd9Sstevel@tonic-gate  *   of algorithms, each a full-fledged struct ipsecalgent.
2677c478bd9Sstevel@tonic-gate  * - The getipsecalg*() routines will search the list, then duplicate the
2687c478bd9Sstevel@tonic-gate  *   struct ipsecalgent and return it.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate typedef enum {
2727c478bd9Sstevel@tonic-gate 	LIBIPSEC_ALGS_EXEC_SYNC,
2737c478bd9Sstevel@tonic-gate 	LIBIPSEC_ALGS_EXEC_ASYNC
2747c478bd9Sstevel@tonic-gate } ipsecalgs_exec_mode_t;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate typedef struct ipsec_proto {
2777c478bd9Sstevel@tonic-gate 	int proto_num;
2787c478bd9Sstevel@tonic-gate 	char *proto_name;
2797c478bd9Sstevel@tonic-gate 	char *proto_pkg;
2807c478bd9Sstevel@tonic-gate 	int proto_numalgs;
2817c478bd9Sstevel@tonic-gate 	struct ipsecalgent **proto_algs;
2827c478bd9Sstevel@tonic-gate 	ipsecalgs_pkg_t *proto_algs_pkgs;
2837c478bd9Sstevel@tonic-gate 	int proto_algs_npkgs;
2847c478bd9Sstevel@tonic-gate 	ipsecalgs_exec_mode_t proto_exec_mode;
2857c478bd9Sstevel@tonic-gate } ipsec_proto_t;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate extern void _build_internal_algs(ipsec_proto_t **, int *);
2887c478bd9Sstevel@tonic-gate extern int _str_to_ipsec_exec_mode(char *, ipsecalgs_exec_mode_t *);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate extern int addipsecalg(struct ipsecalgent *, uint_t);
2917c478bd9Sstevel@tonic-gate extern int delipsecalgbyname(const char *, int);
2927c478bd9Sstevel@tonic-gate extern int delipsecalgbynum(int, int);
2937c478bd9Sstevel@tonic-gate extern int addipsecproto(const char *, int, ipsecalgs_exec_mode_t, uint_t);
2947c478bd9Sstevel@tonic-gate extern int delipsecprotobyname(const char *);
2957c478bd9Sstevel@tonic-gate extern int delipsecprotobynum(int);
2967c478bd9Sstevel@tonic-gate extern int *getipsecprotos(int *);
2977c478bd9Sstevel@tonic-gate extern int *getipsecalgs(int *, int);
2987c478bd9Sstevel@tonic-gate extern int list_ints(FILE *, int *);
2997c478bd9Sstevel@tonic-gate extern const char *ipsecalgs_diag(int);
3007c478bd9Sstevel@tonic-gate extern int ipsecproto_get_exec_mode(int, ipsecalgs_exec_mode_t *);
3017c478bd9Sstevel@tonic-gate extern int ipsecproto_set_exec_mode(int, ipsecalgs_exec_mode_t);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /* Flags for add/delete routines. */
3047c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_ADD_FORCE 0x00000001
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate  * Helper definitions for indices into array of key sizes when key sizes
3087c478bd9Sstevel@tonic-gate  * are defined by range.
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_KEY_DEF_IDX	0	/* default key size */
3117c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_KEY_MIN_IDX	1	/* min key size */
3127c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_KEY_MAX_IDX	2	/* max key size */
3137c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_KEY_NUM_VAL	4	/* def, min, max, 0 */
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate /* Error codes for IPsec algorithms management */
3167c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALG_EXISTS		-1
3177c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_PROTO_EXISTS		-2
3187c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_UNKN_PROTO		-3
3197c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_UNKN_ALG		-4
3207c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_NOMEM		-5
3217c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALGSFILEOPEN		-6
3227c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALGSFILEFDOPEN	-7
3237c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALGSFILELOCK		-8
3247c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALGSFILERENAME	-9
3257c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALGSFILEWRITE	-10
3267c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALGSFILECHMOD	-11
3277c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALGSFILECHOWN	-12
3287c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_DIAG_ALGSFILECLOSE	-13
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /* /etc/inet/ipsecalgs keywords and package sections delimiters */
3317c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_LINE_PROTO		"PROTO|"
3327c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_LINE_ALG			"ALG|"
3337c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_LINE_PKGSTART		"# Start "
3347c478bd9Sstevel@tonic-gate #define	LIBIPSEC_ALGS_LINE_PKGEND		"# End "
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate /* Put these in libnsl for and process caching testing. */
3377c478bd9Sstevel@tonic-gate extern int *_real_getipsecprotos(int *);
3387c478bd9Sstevel@tonic-gate extern int *_real_getipsecalgs(int *, int);
3397c478bd9Sstevel@tonic-gate extern struct ipsecalgent *_duplicate_alg(struct ipsecalgent *);
3407c478bd9Sstevel@tonic-gate extern void _clean_trash(ipsec_proto_t *, int);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /* spdsock support functions */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */
3457c478bd9Sstevel@tonic-gate #define	KGE_OK	0
3467c478bd9Sstevel@tonic-gate #define	KGE_DUP	1
3477c478bd9Sstevel@tonic-gate #define	KGE_UNK	2
3487c478bd9Sstevel@tonic-gate #define	KGE_LEN	3
3497c478bd9Sstevel@tonic-gate #define	KGE_CHK	4
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate extern int spdsock_get_ext(spd_ext_t *[], spd_msg_t *, uint_t, char *, uint_t);
3527c478bd9Sstevel@tonic-gate extern const char *spdsock_diag(int);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /* PF_KEY (keysock) support functions */
3557c478bd9Sstevel@tonic-gate extern const char *keysock_diag(int);
3568810c16bSdanmcd extern int in_masktoprefix(uint8_t *, boolean_t);
3578810c16bSdanmcd 
3588810c16bSdanmcd /* SA support functions */
3598810c16bSdanmcd 
360510c3f91SVladimir Kotal extern char *secs2out(unsigned int, char *, int, int);
361510c3f91SVladimir Kotal extern char *secs2str(unsigned int, char *, int);
362510c3f91SVladimir Kotal extern char *bytecnt2out(uint64_t, char *, size_t, int);
363510c3f91SVladimir Kotal extern char *bytecnt2str(uint64_t, char *, size_t);
3648810c16bSdanmcd extern void print_diagnostic(FILE *, uint16_t);
365bb3ed8dfSpwernau extern void print_sadb_msg(FILE *, struct sadb_msg *, time_t, boolean_t);
366bb3ed8dfSpwernau extern void print_sa(FILE *, char *, struct sadb_sa *);
367bb3ed8dfSpwernau extern void printsatime(FILE *, int64_t, const char *, const char *,
368bb3ed8dfSpwernau     const char *, boolean_t);
369bb3ed8dfSpwernau extern void print_lifetimes(FILE *, time_t, struct sadb_lifetime *,
3709c2c14abSThejaswini Singarajipura     struct sadb_lifetime *, struct sadb_lifetime *, struct sadb_lifetime *,
3719c2c14abSThejaswini Singarajipura     boolean_t vflag);
372bb3ed8dfSpwernau extern void print_address(FILE *, char *, struct sadb_address *, boolean_t);
373a12f8217Spwernau extern void print_asn1_name(FILE *, const unsigned char *, long);
374bb3ed8dfSpwernau extern void print_key(FILE *, char *, struct sadb_key *);
375*1edba515SAndy Fiddaman extern void print_keystr(FILE *, char *, struct sadb_key *);
376bb3ed8dfSpwernau extern void print_ident(FILE *, char *, struct sadb_ident *);
3775d3b8cb7SBill Sommerfeld extern void print_sens(FILE *, char *, const struct sadb_sens *, boolean_t);
378bb3ed8dfSpwernau extern void print_prop(FILE *, char *, struct sadb_prop *);
379bb3ed8dfSpwernau extern void print_eprop(FILE *, char *, struct sadb_prop *);
380bb3ed8dfSpwernau extern void print_supp(FILE *, char *, struct sadb_supported *);
381bb3ed8dfSpwernau extern void print_spirange(FILE *, char *, struct sadb_spirange *);
382bb3ed8dfSpwernau extern void print_kmc(FILE *, char *, struct sadb_x_kmc *);
383bb3ed8dfSpwernau extern void print_samsg(FILE *, uint64_t *, boolean_t, boolean_t, boolean_t);
3848810c16bSdanmcd extern char *rparsesatype(int);
3858810c16bSdanmcd extern char *rparsealg(uint8_t, int);
386*1edba515SAndy Fiddaman extern const char *rparsetcpsigalg(uint8_t);
3878810c16bSdanmcd extern char *rparseidtype(uint16_t);
3888810c16bSdanmcd extern boolean_t save_lifetime(struct sadb_lifetime *, FILE *);
3898810c16bSdanmcd extern boolean_t save_address(struct sadb_address *, FILE *);
3908810c16bSdanmcd extern boolean_t save_key(struct sadb_key *, FILE *);
391*1edba515SAndy Fiddaman extern boolean_t save_keystr(struct sadb_key *, FILE *);
3928810c16bSdanmcd extern boolean_t save_ident(struct sadb_ident *, FILE *);
3938810c16bSdanmcd extern void save_assoc(uint64_t *, FILE *);
3948810c16bSdanmcd extern FILE *opensavefile(char *);
3958810c16bSdanmcd extern const char *do_inet_ntop(const void *, char *, size_t);
396*1edba515SAndy Fiddaman extern uint8_t gettcpsigalgbyname(const char *);
397*1edba515SAndy Fiddaman extern const char *gettcpsigalgbynum(uint8_t);
3987c478bd9Sstevel@tonic-gate 
399e3320f40Smarkfen /*
4005d3b8cb7SBill Sommerfeld  * Label conversion convenience functions.
4015d3b8cb7SBill Sommerfeld  */
4025d3b8cb7SBill Sommerfeld 
4035d3b8cb7SBill Sommerfeld #include <tsol/label.h>
4045d3b8cb7SBill Sommerfeld 
4055d3b8cb7SBill Sommerfeld extern void ipsec_convert_sens_to_bslabel(const struct sadb_sens *,
4065d3b8cb7SBill Sommerfeld     bslabel_t *);
4075d3b8cb7SBill Sommerfeld extern int ipsec_convert_sl_to_sens(int doi, bslabel_t *, struct sadb_sens *);
4085d3b8cb7SBill Sommerfeld extern void ipsec_convert_bslabel_to_string(bslabel_t *, char **);
4095d3b8cb7SBill Sommerfeld extern void ipsec_convert_bslabel_to_hex(bslabel_t *, char **);
4105d3b8cb7SBill Sommerfeld 
4115d3b8cb7SBill Sommerfeld /*
412e3320f40Smarkfen  * These exit macros give a consistent exit behaviour for all
413e3320f40Smarkfen  * programs that use libipsecutil. These wll work in usr/src/cmd
414e3320f40Smarkfen  * and usr/src/lib, but because macros in usr/src/lib don't get
415e3320f40Smarkfen  * expanded when I18N message catalogs are built, avoid using
416a1ba8781SMark Fenwick  * these with text inside libipsecutil. See source of ipsecutil_exit()
417a1ba8781SMark Fenwick  * for more details.
418e3320f40Smarkfen  */
419e3320f40Smarkfen #define	EXIT_OK(x) \
420e3320f40Smarkfen 	ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
421e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x))
422e3320f40Smarkfen #define	EXIT_OK2(x, y) \
423e3320f40Smarkfen 	ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
424e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x), y)
425e3320f40Smarkfen #define	EXIT_OK3(x, y, z) \
426e3320f40Smarkfen 	ipsecutil_exit(SERVICE_EXIT_OK, my_fmri, debugfile, \
427e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x), y, z)
428e3320f40Smarkfen #define	EXIT_BADCONFIG(x) \
429e3320f40Smarkfen 	ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
430e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x))
431e3320f40Smarkfen #define	EXIT_BADCONFIG2(x, y) \
432e3320f40Smarkfen 	ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
433e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x), y)
434e3320f40Smarkfen #define	EXIT_BADCONFIG3(x, y, z) \
435e3320f40Smarkfen 	ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile, \
436e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x), y, z)
437e3320f40Smarkfen #define	EXIT_MAINTAIN(x) \
438e3320f40Smarkfen 	ipsecutil_exit(SERVICE_MAINTAIN, my_fmri, debugfile, \
439e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x))
440e3320f40Smarkfen #define	EXIT_MAINTAIN2(x, y) \
441e3320f40Smarkfen 	ipsecutil_exit(SERVICE_MAINTAIN, my_fmri, debugfile, \
442e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x), y)
443e3320f40Smarkfen #define	EXIT_DEGRADE(x) \
444e3320f40Smarkfen 	ipsecutil_exit(SERVICE_DEGRADE, my_fmri, debugfile, \
445e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x))
446e3320f40Smarkfen #define	EXIT_BADPERM(x) \
447e3320f40Smarkfen 	ipsecutil_exit(SERVICE_BADPERM, my_fmri, debugfile, \
448e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x))
449e3320f40Smarkfen #define	EXIT_BADPERM2(x, y) \
450e3320f40Smarkfen 	ipsecutil_exit(SERVICE_BADPERM, my_fmri, debugfile, \
451e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x), y)
452e3320f40Smarkfen #define	EXIT_FATAL(x) \
453e3320f40Smarkfen 	ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
454e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x))
455e3320f40Smarkfen #define	EXIT_FATAL2(x, y) \
456e3320f40Smarkfen 	ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
457e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x), y)
458e3320f40Smarkfen #define	EXIT_FATAL3(x, y, z) \
459e3320f40Smarkfen 	ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile, \
460e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x), y, z)
461e3320f40Smarkfen #define	EXIT_RESTART(x) \
462e3320f40Smarkfen 	ipsecutil_exit(SERVICE_RESTART, my_fmri, debugfile, \
463e3320f40Smarkfen 	dgettext(TEXT_DOMAIN, x))
464e3320f40Smarkfen 
4657c478bd9Sstevel@tonic-gate #ifdef __cplusplus
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate #endif
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate #endif	/* _IPSEC_UTIL_H */
470