xref: /onnv-gate/usr/src/uts/common/inet/ip/spdsock.c (revision 8778:b4169d2ab299)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53055Sdanmcd  * Common Development and Distribution License (the "License").
63055Sdanmcd  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8778SErik.Nordmark@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/stream.h>
290Sstevel@tonic-gate #include <sys/strsubr.h>
300Sstevel@tonic-gate #include <sys/strsun.h>
310Sstevel@tonic-gate #include <sys/stropts.h>
323448Sdh155122 #include <sys/zone.h>
330Sstevel@tonic-gate #include <sys/vnode.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
360Sstevel@tonic-gate #include <sys/tihdr.h>
370Sstevel@tonic-gate #include <sys/ddi.h>
380Sstevel@tonic-gate #include <sys/sunddi.h>
390Sstevel@tonic-gate #include <sys/mkdev.h>
400Sstevel@tonic-gate #include <sys/debug.h>
410Sstevel@tonic-gate #include <sys/kmem.h>
420Sstevel@tonic-gate #include <sys/cmn_err.h>
430Sstevel@tonic-gate #include <sys/suntpi.h>
440Sstevel@tonic-gate #include <sys/policy.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/socket.h>
470Sstevel@tonic-gate #include <netinet/in.h>
480Sstevel@tonic-gate #include <net/pfkeyv2.h>
490Sstevel@tonic-gate #include <net/pfpolicy.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <inet/common.h>
520Sstevel@tonic-gate #include <netinet/ip6.h>
530Sstevel@tonic-gate #include <inet/ip.h>
540Sstevel@tonic-gate #include <inet/ip6.h>
550Sstevel@tonic-gate #include <inet/mi.h>
568348SEric.Yu@Sun.COM #include <inet/proto_set.h>
570Sstevel@tonic-gate #include <inet/nd.h>
583055Sdanmcd #include <inet/ip_if.h>
593055Sdanmcd #include <inet/tun.h>
600Sstevel@tonic-gate #include <inet/optcom.h>
610Sstevel@tonic-gate #include <inet/ipsec_info.h>
620Sstevel@tonic-gate #include <inet/ipsec_impl.h>
630Sstevel@tonic-gate #include <inet/spdsock.h>
640Sstevel@tonic-gate #include <inet/sadb.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/isa_defs.h>
670Sstevel@tonic-gate 
684307Spwernau #include <c2/audit.h>
694307Spwernau 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * This is a transport provider for the PF_POLICY IPsec policy
720Sstevel@tonic-gate  * management socket, which provides a management interface into the
730Sstevel@tonic-gate  * SPD, allowing policy rules to be added, deleted, and queried.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * This effectively replaces the old private SIOC*IPSECONFIG ioctls
760Sstevel@tonic-gate  * with an extensible interface which will hopefully be public some
770Sstevel@tonic-gate  * day.
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  * See <net/pfpolicy.h> for more details on the protocol.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * We link against drv/ip and call directly into it to manipulate the
820Sstevel@tonic-gate  * SPD; see ipsec_impl.h for the policy data structures and spd.c for
830Sstevel@tonic-gate  * the code which maintains them.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * The MT model of this is QPAIR with the addition of some explicit
860Sstevel@tonic-gate  * locking to protect system-wide policy data structures.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static vmem_t *spdsock_vmem;		/* for minor numbers. */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t))
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
940Sstevel@tonic-gate static struct T_info_ack spdsock_g_t_info_ack = {
950Sstevel@tonic-gate 	T_INFO_ACK,
960Sstevel@tonic-gate 	T_INFINITE,	/* TSDU_size. Maximum size messages. */
970Sstevel@tonic-gate 	T_INVALID,	/* ETSDU_size. No expedited data. */
980Sstevel@tonic-gate 	T_INVALID,	/* CDATA_size. No connect data. */
990Sstevel@tonic-gate 	T_INVALID,	/* DDATA_size. No disconnect data. */
1000Sstevel@tonic-gate 	0,		/* ADDR_size. */
1010Sstevel@tonic-gate 	0,		/* OPT_size. No user-settable options */
1020Sstevel@tonic-gate 	64 * 1024,	/* TIDU_size. spdsock allows maximum size messages. */
1030Sstevel@tonic-gate 	T_COTS,		/* SERV_type. spdsock supports connection oriented. */
1040Sstevel@tonic-gate 	TS_UNBND,	/* CURRENT_state. This is set from spdsock_state. */
1050Sstevel@tonic-gate 	(XPG4_1)	/* Provider flags */
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
1093448Sdh155122 typedef struct spdsockparam_s {
1100Sstevel@tonic-gate 	uint_t	spdsock_param_min;
1110Sstevel@tonic-gate 	uint_t	spdsock_param_max;
1120Sstevel@tonic-gate 	uint_t	spdsock_param_value;
1130Sstevel@tonic-gate 	char *spdsock_param_name;
1140Sstevel@tonic-gate } spdsockparam_t;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * Table of NDD variables supported by spdsock. These are loaded into
1180Sstevel@tonic-gate  * spdsock_g_nd in spdsock_init_nd.
1190Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
1200Sstevel@tonic-gate  */
1213448Sdh155122 static	spdsockparam_t	lcl_param_arr[] = {
1220Sstevel@tonic-gate 	/* min	max	value	name */
1230Sstevel@tonic-gate 	{ 4096, 65536,	8192,	"spdsock_xmit_hiwat"},
1240Sstevel@tonic-gate 	{ 0,	65536,	1024,	"spdsock_xmit_lowat"},
1250Sstevel@tonic-gate 	{ 4096, 65536,	8192,	"spdsock_recv_hiwat"},
1260Sstevel@tonic-gate 	{ 65536, 1024*1024*1024, 256*1024,	"spdsock_max_buf"},
1270Sstevel@tonic-gate 	{ 0,	3,	0,	"spdsock_debug"},
1280Sstevel@tonic-gate };
1293448Sdh155122 #define	spds_xmit_hiwat	spds_params[0].spdsock_param_value
1303448Sdh155122 #define	spds_xmit_lowat	spds_params[1].spdsock_param_value
1313448Sdh155122 #define	spds_recv_hiwat	spds_params[2].spdsock_param_value
1323448Sdh155122 #define	spds_max_buf	spds_params[3].spdsock_param_value
1333448Sdh155122 #define	spds_debug		spds_params[4].spdsock_param_value
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate #define	ss0dbg(a)	printf a
1360Sstevel@tonic-gate /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
1373448Sdh155122 #define	ss1dbg(spds, a)	if (spds->spds_debug != 0) printf a
1383448Sdh155122 #define	ss2dbg(spds, a)	if (spds->spds_debug > 1) printf a
1393448Sdh155122 #define	ss3dbg(spds, a)	if (spds->spds_debug > 2) printf a
1400Sstevel@tonic-gate 
1416430Sdanmcd #define	RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \
1426430Sdanmcd 	ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \
1436430Sdanmcd 	(ss)->spdsock_dump_head = (iph); \
1446430Sdanmcd 	(ss)->spdsock_dump_gen = (iph)->iph_gen; \
1456430Sdanmcd 	(ss)->spdsock_dump_cur_type = 0; \
1466430Sdanmcd 	(ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \
1476430Sdanmcd 	(ss)->spdsock_dump_cur_rule = NULL; \
1486430Sdanmcd 	(ss)->spdsock_dump_count = 0; \
1496430Sdanmcd 	(ss)->spdsock_dump_cur_chain = 0; \
1506430Sdanmcd }
1516430Sdanmcd 
1520Sstevel@tonic-gate static int spdsock_close(queue_t *);
1530Sstevel@tonic-gate static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *);
1540Sstevel@tonic-gate static void spdsock_wput(queue_t *, mblk_t *);
1550Sstevel@tonic-gate static void spdsock_wsrv(queue_t *);
1560Sstevel@tonic-gate static void spdsock_rsrv(queue_t *);
1573448Sdh155122 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns);
1583448Sdh155122 static void spdsock_stack_fini(netstackid_t stackid, void *arg);
1590Sstevel@tonic-gate static void spdsock_loadcheck(void *);
1603448Sdh155122 static void spdsock_merge_algs(spd_stack_t *);
1613448Sdh155122 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *);
1623055Sdanmcd static mblk_t *spdsock_dump_next_record(spdsock_t *);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static struct module_info info = {
1650Sstevel@tonic-gate 	5138, "spdsock", 1, INFPSZ, 512, 128
1660Sstevel@tonic-gate };
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static struct qinit rinit = {
1690Sstevel@tonic-gate 	NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close,
1700Sstevel@tonic-gate 	NULL, &info
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static struct qinit winit = {
1740Sstevel@tonic-gate 	(pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info
1750Sstevel@tonic-gate };
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate struct streamtab spdsockinfo = {
1780Sstevel@tonic-gate 	&rinit, &winit
1790Sstevel@tonic-gate };
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /* mapping from alg type to protocol number, as per RFC 2407 */
1820Sstevel@tonic-gate static const uint_t algproto[] = {
1830Sstevel@tonic-gate 	PROTO_IPSEC_AH,
1840Sstevel@tonic-gate 	PROTO_IPSEC_ESP,
1850Sstevel@tonic-gate };
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate #define	NALGPROTOS	(sizeof (algproto) / sizeof (algproto[0]))
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /* mapping from kernel exec mode to spdsock exec mode */
1900Sstevel@tonic-gate static const uint_t execmodes[] = {
1910Sstevel@tonic-gate 	SPD_ALG_EXEC_MODE_SYNC,
1920Sstevel@tonic-gate 	SPD_ALG_EXEC_MODE_ASYNC
1930Sstevel@tonic-gate };
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate #define	NEXECMODES	(sizeof (execmodes) / sizeof (execmodes[0]))
1960Sstevel@tonic-gate 
1973055Sdanmcd #define	ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1)
1983055Sdanmcd #define	ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2)
1993055Sdanmcd 
2004307Spwernau #define	ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL)
2014307Spwernau 
2020Sstevel@tonic-gate /* ARGSUSED */
2030Sstevel@tonic-gate static int
2040Sstevel@tonic-gate spdsock_param_get(q, mp, cp, cr)
2050Sstevel@tonic-gate 	queue_t	*q;
2060Sstevel@tonic-gate 	mblk_t	*mp;
2070Sstevel@tonic-gate 	caddr_t	cp;
2080Sstevel@tonic-gate 	cred_t *cr;
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	spdsockparam_t	*spdsockpa = (spdsockparam_t *)cp;
2110Sstevel@tonic-gate 	uint_t value;
2123448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2133448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
2143448Sdh155122 
2153448Sdh155122 	mutex_enter(&spds->spds_param_lock);
2160Sstevel@tonic-gate 	value = spdsockpa->spdsock_param_value;
2173448Sdh155122 	mutex_exit(&spds->spds_param_lock);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
2200Sstevel@tonic-gate 	return (0);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /* This routine sets an NDD variable in a spdsockparam_t structure. */
2240Sstevel@tonic-gate /* ARGSUSED */
2250Sstevel@tonic-gate static int
2260Sstevel@tonic-gate spdsock_param_set(q, mp, value, cp, cr)
2270Sstevel@tonic-gate 	queue_t	*q;
2280Sstevel@tonic-gate 	mblk_t	*mp;
2290Sstevel@tonic-gate 	char *value;
2300Sstevel@tonic-gate 	caddr_t	cp;
2310Sstevel@tonic-gate 	cred_t *cr;
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	ulong_t	new_value;
2340Sstevel@tonic-gate 	spdsockparam_t	*spdsockpa = (spdsockparam_t *)cp;
2353448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2363448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/* Convert the value from a string into a long integer. */
2390Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
2400Sstevel@tonic-gate 		return (EINVAL);
2410Sstevel@tonic-gate 
2423448Sdh155122 	mutex_enter(&spds->spds_param_lock);
2430Sstevel@tonic-gate 	/*
2440Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
2450Sstevel@tonic-gate 	 * required bounds.
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	if (new_value < spdsockpa->spdsock_param_min ||
2480Sstevel@tonic-gate 	    new_value > spdsockpa->spdsock_param_max) {
2493448Sdh155122 		mutex_exit(&spds->spds_param_lock);
2500Sstevel@tonic-gate 		return (EINVAL);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/* Set the new value */
2540Sstevel@tonic-gate 	spdsockpa->spdsock_param_value = new_value;
2553448Sdh155122 	mutex_exit(&spds->spds_param_lock);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	return (0);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2603448Sdh155122 /*
2613448Sdh155122  * Initialize at module load time
2623448Sdh155122  */
2630Sstevel@tonic-gate boolean_t
2640Sstevel@tonic-gate spdsock_ddi_init(void)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	spdsock_max_optsize = optcom_max_optsize(
2670Sstevel@tonic-gate 	    spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1,
2700Sstevel@tonic-gate 	    NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
2710Sstevel@tonic-gate 
2723448Sdh155122 	/*
2733448Sdh155122 	 * We want to be informed each time a stack is created or
2743448Sdh155122 	 * destroyed in the kernel, so we can maintain the
2753448Sdh155122 	 * set of spd_stack_t's.
2763448Sdh155122 	 */
2773448Sdh155122 	netstack_register(NS_SPDSOCK, spdsock_stack_init, NULL,
2783448Sdh155122 	    spdsock_stack_fini);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	return (B_TRUE);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2833448Sdh155122 /*
2843448Sdh155122  * Walk through the param array specified registering each element with the
2853448Sdh155122  * named dispatch handler.
2863448Sdh155122  */
2873448Sdh155122 static boolean_t
2883448Sdh155122 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt)
2893448Sdh155122 {
2903448Sdh155122 	for (; cnt-- > 0; ssp++) {
2913448Sdh155122 		if (ssp->spdsock_param_name != NULL &&
2923448Sdh155122 		    ssp->spdsock_param_name[0]) {
2933448Sdh155122 			if (!nd_load(ndp,
2943448Sdh155122 			    ssp->spdsock_param_name,
2953448Sdh155122 			    spdsock_param_get, spdsock_param_set,
2963448Sdh155122 			    (caddr_t)ssp)) {
2973448Sdh155122 				nd_free(ndp);
2983448Sdh155122 				return (B_FALSE);
2993448Sdh155122 			}
3003448Sdh155122 		}
3013448Sdh155122 	}
3023448Sdh155122 	return (B_TRUE);
3033448Sdh155122 }
3043448Sdh155122 
3053448Sdh155122 /*
3063448Sdh155122  * Initialize for each stack instance
3073448Sdh155122  */
3083448Sdh155122 /* ARGSUSED */
3093448Sdh155122 static void *
3103448Sdh155122 spdsock_stack_init(netstackid_t stackid, netstack_t *ns)
3113448Sdh155122 {
3123448Sdh155122 	spd_stack_t	*spds;
3133448Sdh155122 	spdsockparam_t	*ssp;
3143448Sdh155122 
3153448Sdh155122 	spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP);
3163448Sdh155122 	spds->spds_netstack = ns;
3173448Sdh155122 
3183448Sdh155122 	ASSERT(spds->spds_g_nd == NULL);
3193448Sdh155122 
3203448Sdh155122 	ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
3213448Sdh155122 	spds->spds_params = ssp;
3223448Sdh155122 	bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr));
3233448Sdh155122 
3243448Sdh155122 	(void) spdsock_param_register(&spds->spds_g_nd, ssp,
3253448Sdh155122 	    A_CNT(lcl_param_arr));
3263448Sdh155122 
3273448Sdh155122 	mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL);
3283448Sdh155122 	mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL);
3293448Sdh155122 
3303448Sdh155122 	return (spds);
3313448Sdh155122 }
3323448Sdh155122 
3330Sstevel@tonic-gate void
3340Sstevel@tonic-gate spdsock_ddi_destroy(void)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	vmem_destroy(spdsock_vmem);
3373448Sdh155122 
3383448Sdh155122 	netstack_unregister(NS_SPDSOCK);
3393448Sdh155122 }
3403448Sdh155122 
3413448Sdh155122 /* ARGSUSED */
3423448Sdh155122 static void
3433448Sdh155122 spdsock_stack_fini(netstackid_t stackid, void *arg)
3443448Sdh155122 {
3453448Sdh155122 	spd_stack_t *spds = (spd_stack_t *)arg;
3463448Sdh155122 
3476430Sdanmcd 	freemsg(spds->spds_mp_algs);
3483448Sdh155122 	mutex_destroy(&spds->spds_param_lock);
3493448Sdh155122 	mutex_destroy(&spds->spds_alg_lock);
3503448Sdh155122 	nd_free(&spds->spds_g_nd);
3513448Sdh155122 	kmem_free(spds->spds_params, sizeof (lcl_param_arr));
3523448Sdh155122 	spds->spds_params = NULL;
3533448Sdh155122 
3543448Sdh155122 	kmem_free(spds, sizeof (*spds));
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * NOTE: large quantities of this should be shared with keysock.
3590Sstevel@tonic-gate  * Would be nice to combine some of this into a common module, but
3600Sstevel@tonic-gate  * not possible given time pressures.
3610Sstevel@tonic-gate  */
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  * High-level reality checking of extensions.
3650Sstevel@tonic-gate  */
3660Sstevel@tonic-gate /* ARGSUSED */ /* XXX */
3670Sstevel@tonic-gate static boolean_t
3680Sstevel@tonic-gate ext_check(spd_ext_t *ext)
3690Sstevel@tonic-gate {
3703055Sdanmcd 	spd_if_t *tunname = (spd_if_t *)ext;
3713055Sdanmcd 	int i;
3723055Sdanmcd 	char *idstr;
3733055Sdanmcd 
3743055Sdanmcd 	if (ext->spd_ext_type == SPD_EXT_TUN_NAME) {
3753055Sdanmcd 		/* (NOTE:  Modified from SADB_EXT_IDENTITY..) */
3763055Sdanmcd 
3773055Sdanmcd 		/*
3783055Sdanmcd 		 * Make sure the strings in these identities are
3793055Sdanmcd 		 * null-terminated.  Let's "proactively" null-terminate the
3803055Sdanmcd 		 * string at the last byte if it's not terminated sooner.
3813055Sdanmcd 		 */
3823055Sdanmcd 		i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t);
3833055Sdanmcd 		idstr = (char *)(tunname + 1);
3843055Sdanmcd 		while (*idstr != '\0' && i > 0) {
3853055Sdanmcd 			i--;
3863055Sdanmcd 			idstr++;
3873055Sdanmcd 		}
3883055Sdanmcd 		if (i == 0) {
3893055Sdanmcd 			/*
3903055Sdanmcd 			 * I.e., if the bozo user didn't NULL-terminate the
3913055Sdanmcd 			 * string...
3923055Sdanmcd 			 */
3933055Sdanmcd 			idstr--;
3943055Sdanmcd 			*idstr = '\0';
3953055Sdanmcd 		}
3963055Sdanmcd 	}
3970Sstevel@tonic-gate 	return (B_TRUE);	/* For now... */
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */
4030Sstevel@tonic-gate #define	KGE_OK	0
4040Sstevel@tonic-gate #define	KGE_DUP	1
4050Sstevel@tonic-gate #define	KGE_UNK	2
4060Sstevel@tonic-gate #define	KGE_LEN	3
4070Sstevel@tonic-gate #define	KGE_CHK	4
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
4110Sstevel@tonic-gate  * Return values include:
4120Sstevel@tonic-gate  *
4130Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
4140Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
4150Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
4160Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
4170Sstevel@tonic-gate  *		extv[0].
4180Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
4190Sstevel@tonic-gate  *		unknown header.
4200Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
4210Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
4240Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
4250Sstevel@tonic-gate  */
4260Sstevel@tonic-gate static int
4270Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
4360Sstevel@tonic-gate 		/* Check for unknown headers. */
4370Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
4380Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX)
4390Sstevel@tonic-gate 			return (KGE_UNK);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		/*
4420Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
4430Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
4440Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
4450Sstevel@tonic-gate 		 */
4460Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
4470Sstevel@tonic-gate 		    (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
4480Sstevel@tonic-gate 		    (void *)((uint8_t *)basehdr + msgsize))
4490Sstevel@tonic-gate 			return (KGE_LEN);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 		/* Check for redundant headers. */
4520Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
4530Sstevel@tonic-gate 			return (KGE_DUP);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		/*
4560Sstevel@tonic-gate 		 * Reality check the extension if possible at the spdsock
4570Sstevel@tonic-gate 		 * level.
4580Sstevel@tonic-gate 		 */
4590Sstevel@tonic-gate 		if (!ext_check(extv[0]))
4600Sstevel@tonic-gate 			return (KGE_CHK);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
4630Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
4660Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
4670Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/* Everything's cool. */
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	/*
4730Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
4740Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
4750Sstevel@tonic-gate 	 */
4760Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
4770Sstevel@tonic-gate 		extv[0] = NULL;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	return (KGE_OK);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate static const int bad_ext_diag[] = {
4830Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_LCLPORT,
4840Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_REMPORT,
4850Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_PROTO,
4860Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_LCLADDR,
4870Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_REMADDR,
4880Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_ACTION,
4890Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_RULE,
4900Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_RULESET,
4910Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE
4920Sstevel@tonic-gate };
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate static const int dup_ext_diag[] = {
4950Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_LCLPORT,
4960Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_REMPORT,
4970Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_PROTO,
4980Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_LCLADDR,
4990Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_REMADDR,
5000Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_ACTION,
5010Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_RULE,
5020Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_RULESET,
5030Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE
5040Sstevel@tonic-gate };
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate  * Transmit a PF_POLICY error message to the instance either pointed to
5080Sstevel@tonic-gate  * by ks, the instance with serial number serial, or more, depending.
5090Sstevel@tonic-gate  *
5100Sstevel@tonic-gate  * The faulty message (or a reasonable facsimile thereof) is in mp.
5110Sstevel@tonic-gate  * This function will free mp or recycle it for delivery, thereby causing
5120Sstevel@tonic-gate  * the stream head to free it.
5130Sstevel@tonic-gate  */
5140Sstevel@tonic-gate static void
5150Sstevel@tonic-gate spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate 	spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_DATA);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (spmsg->spd_msg_type < SPD_MIN ||
5220Sstevel@tonic-gate 	    spmsg->spd_msg_type > SPD_MAX)
5230Sstevel@tonic-gate 		spmsg->spd_msg_type = SPD_RESERVED;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/*
5260Sstevel@tonic-gate 	 * Strip out extension headers.
5270Sstevel@tonic-gate 	 */
5280Sstevel@tonic-gate 	ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim);
5290Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (*spmsg);
5300Sstevel@tonic-gate 	spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t));
5310Sstevel@tonic-gate 	spmsg->spd_msg_errno = (uint8_t)error;
5320Sstevel@tonic-gate 	spmsg->spd_msg_diagnostic = (uint16_t)diagnostic;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	qreply(q, mp);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate static void
5380Sstevel@tonic-gate spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	spdsock_error(q, mp, EINVAL, diagnostic);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate static void
5440Sstevel@tonic-gate spd_echo(queue_t *q, mblk_t *mp)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	qreply(q, mp);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5493055Sdanmcd /*
5503055Sdanmcd  * Do NOT consume a reference to itp.
5513055Sdanmcd  */
5523448Sdh155122 /*ARGSUSED*/
5530Sstevel@tonic-gate static void
5543448Sdh155122 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns)
5553055Sdanmcd {
5563055Sdanmcd 	boolean_t active = (boolean_t)cookie;
5573055Sdanmcd 	ipsec_policy_head_t *iph;
5583055Sdanmcd 
5593055Sdanmcd 	iph = active ? itp->itp_policy : itp->itp_inactive;
5603055Sdanmcd 	IPPH_REFHOLD(iph);
5613055Sdanmcd 	mutex_enter(&itp->itp_lock);
5623448Sdh155122 	spdsock_flush_one(iph, ns);
5633055Sdanmcd 	if (active)
5643055Sdanmcd 		itp->itp_flags &= ~ITPF_PFLAGS;
5653055Sdanmcd 	else
5663055Sdanmcd 		itp->itp_flags &= ~ITPF_IFLAGS;
5673055Sdanmcd 	mutex_exit(&itp->itp_lock);
5683055Sdanmcd }
5693055Sdanmcd 
5703055Sdanmcd /*
5713055Sdanmcd  * Clear out one polhead.
5723055Sdanmcd  */
5733055Sdanmcd static void
5743448Sdh155122 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_WRITER);
5773448Sdh155122 	ipsec_polhead_flush(iph, ns);
5780Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
5793448Sdh155122 	IPPH_REFRELE(iph, ns);
5803055Sdanmcd }
5813055Sdanmcd 
5823055Sdanmcd static void
5834307Spwernau spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp,
5844307Spwernau     mblk_t *mp)
5853055Sdanmcd {
5863055Sdanmcd 	boolean_t active;
5873448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
5886430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
5893055Sdanmcd 
5903055Sdanmcd 	if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) {
5916430Sdanmcd 		spdsock_flush_one(iph, ns);
5924307Spwernau 		if (audit_active) {
5934307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
594*8778SErik.Nordmark@Sun.COM 			cred_t *cr;
595*8778SErik.Nordmark@Sun.COM 			pid_t cpid;
596*8778SErik.Nordmark@Sun.COM 
597*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
5984307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
599*8778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_FLUSH, cr, ns,
600*8778SErik.Nordmark@Sun.COM 			    ITP_NAME(itp), active, 0, cpid);
6014307Spwernau 		}
6023055Sdanmcd 	} else {
6033055Sdanmcd 		active = (iph == ALL_ACTIVE_POLHEADS);
6043055Sdanmcd 
6053055Sdanmcd 		/* First flush the global policy. */
6063448Sdh155122 		spdsock_flush_one(active ? ipsec_system_policy(ns) :
6073448Sdh155122 		    ipsec_inactive_policy(ns), ns);
6084307Spwernau 		if (audit_active) {
609*8778SErik.Nordmark@Sun.COM 			cred_t *cr;
610*8778SErik.Nordmark@Sun.COM 			pid_t cpid;
611*8778SErik.Nordmark@Sun.COM 
612*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
613*8778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_FLUSH, cr, ns, NULL,
614*8778SErik.Nordmark@Sun.COM 			    active, 0, cpid);
6154307Spwernau 		}
6163055Sdanmcd 		/* Then flush every tunnel's appropriate one. */
6176430Sdanmcd 		itp_walk(spdsock_flush_node, (void *)active, ns);
618*8778SErik.Nordmark@Sun.COM 		if (audit_active) {
619*8778SErik.Nordmark@Sun.COM 			cred_t *cr;
620*8778SErik.Nordmark@Sun.COM 			pid_t cpid;
621*8778SErik.Nordmark@Sun.COM 
622*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
623*8778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_FLUSH, cr, ns,
624*8778SErik.Nordmark@Sun.COM 			    "all tunnels", active, 0, cpid);
625*8778SErik.Nordmark@Sun.COM 		}
6263055Sdanmcd 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	spd_echo(q, mp);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate static boolean_t
6320Sstevel@tonic-gate spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	bzero(sel, sizeof (*sel));
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if (extv[SPD_EXT_PROTO] != NULL) {
6370Sstevel@tonic-gate 		struct spd_proto *pr =
6380Sstevel@tonic-gate 		    (struct spd_proto *)extv[SPD_EXT_PROTO];
6390Sstevel@tonic-gate 		sel->ipsl_proto = pr->spd_proto_number;
6400Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_PROTOCOL;
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 	if (extv[SPD_EXT_LCLPORT] != NULL) {
6430Sstevel@tonic-gate 		struct spd_portrange *pr =
6440Sstevel@tonic-gate 		    (struct spd_portrange *)extv[SPD_EXT_LCLPORT];
6450Sstevel@tonic-gate 		sel->ipsl_lport = pr->spd_ports_minport;
6460Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_LOCAL_PORT;
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 	if (extv[SPD_EXT_REMPORT] != NULL) {
6490Sstevel@tonic-gate 		struct spd_portrange *pr =
6500Sstevel@tonic-gate 		    (struct spd_portrange *)extv[SPD_EXT_REMPORT];
6510Sstevel@tonic-gate 		sel->ipsl_rport = pr->spd_ports_minport;
6520Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_REMOTE_PORT;
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) {
6560Sstevel@tonic-gate 		struct spd_typecode *tc=
6570Sstevel@tonic-gate 		    (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE];
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_ICMP_TYPE;
6600Sstevel@tonic-gate 		sel->ipsl_icmp_type = tc->spd_typecode_type;
6610Sstevel@tonic-gate 		if (tc->spd_typecode_type_end < tc->spd_typecode_type)
6620Sstevel@tonic-gate 			sel->ipsl_icmp_type_end = tc->spd_typecode_type;
6630Sstevel@tonic-gate 		else
6640Sstevel@tonic-gate 			sel->ipsl_icmp_type_end = tc->spd_typecode_type_end;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 		if (tc->spd_typecode_code != 255) {
6670Sstevel@tonic-gate 			sel->ipsl_valid |= IPSL_ICMP_CODE;
6680Sstevel@tonic-gate 			sel->ipsl_icmp_code = tc->spd_typecode_code;
6690Sstevel@tonic-gate 			if (tc->spd_typecode_code_end < tc->spd_typecode_code)
6700Sstevel@tonic-gate 				sel->ipsl_icmp_code_end = tc->spd_typecode_code;
6710Sstevel@tonic-gate 			else
6720Sstevel@tonic-gate 				sel->ipsl_icmp_code_end =
6730Sstevel@tonic-gate 				    tc->spd_typecode_code_end;
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate #define	ADDR2SEL(sel, extv, field, pfield, extn, bit)			      \
6770Sstevel@tonic-gate 	if ((extv)[(extn)] != NULL) {					      \
6780Sstevel@tonic-gate 		uint_t addrlen;						      \
6790Sstevel@tonic-gate 		struct spd_address *ap = 				      \
6800Sstevel@tonic-gate 			(struct spd_address *)((extv)[(extn)]); 	      \
6810Sstevel@tonic-gate 		addrlen = (ap->spd_address_af == AF_INET6) ? 		      \
6820Sstevel@tonic-gate 			IPV6_ADDR_LEN : IP_ADDR_LEN;			      \
6830Sstevel@tonic-gate 		if (SPD_64TO8(ap->spd_address_len) < 			      \
6840Sstevel@tonic-gate 			(addrlen + sizeof (*ap))) {			      \
6850Sstevel@tonic-gate 			*diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN;		      \
6860Sstevel@tonic-gate 			return (B_FALSE);				      \
6870Sstevel@tonic-gate 		}							      \
6880Sstevel@tonic-gate 		bcopy((ap+1), &((sel)->field), addrlen);		      \
6890Sstevel@tonic-gate 		(sel)->pfield = ap->spd_address_prefixlen;		      \
6900Sstevel@tonic-gate 		(sel)->ipsl_valid |= (bit);				      \
6910Sstevel@tonic-gate 		(sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ?	      \
6920Sstevel@tonic-gate 			IPSL_IPV6 : IPSL_IPV4;				      \
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen,
6960Sstevel@tonic-gate 	    SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR);
6970Sstevel@tonic-gate 	ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen,
6980Sstevel@tonic-gate 	    SPD_EXT_REMADDR, IPSL_REMOTE_ADDR);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) ==
7010Sstevel@tonic-gate 	    (IPSL_IPV6|IPSL_IPV4)) {
7020Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_MIXED_AF;
7030Sstevel@tonic-gate 		return (B_FALSE);
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate #undef ADDR2SEL
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	return (B_TRUE);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate static boolean_t
7120Sstevel@tonic-gate spd_convert_type(uint32_t type, ipsec_act_t *act)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	switch (type) {
7150Sstevel@tonic-gate 	case SPD_ACTTYPE_DROP:
7160Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_DISCARD;
7170Sstevel@tonic-gate 		return (B_TRUE);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	case SPD_ACTTYPE_PASS:
7200Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_CLEAR;
7210Sstevel@tonic-gate 		return (B_TRUE);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	case SPD_ACTTYPE_IPSEC:
7240Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_APPLY;
7250Sstevel@tonic-gate 		return (B_TRUE);
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 	return (B_FALSE);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate static boolean_t
7310Sstevel@tonic-gate spd_convert_flags(uint32_t flags, ipsec_act_t *act)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	/*
7340Sstevel@tonic-gate 	 * Note use of !! for boolean canonicalization.
7350Sstevel@tonic-gate 	 */
7360Sstevel@tonic-gate 	act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH);
7370Sstevel@tonic-gate 	act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP);
7380Sstevel@tonic-gate 	act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA);
7390Sstevel@tonic-gate 	act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE);
7400Sstevel@tonic-gate 	act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE);
7410Sstevel@tonic-gate 	return (B_TRUE);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate static void
7450Sstevel@tonic-gate spdsock_reset_act(ipsec_act_t *act)
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate 	bzero(act, sizeof (*act));
7480Sstevel@tonic-gate 	act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS;
7490Sstevel@tonic-gate 	act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS;
7500Sstevel@tonic-gate 	act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS;
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate  * Sanity check action against reality, and shrink-wrap key sizes..
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate static boolean_t
7573448Sdh155122 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag,
7583448Sdh155122     spd_stack_t *spds)
7590Sstevel@tonic-gate {
7603055Sdanmcd 	if (tunnel_polhead && act->ipa_apply.ipp_use_unique) {
7613055Sdanmcd 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7623055Sdanmcd 		return (B_FALSE);
7633055Sdanmcd 	}
7640Sstevel@tonic-gate 	if ((act->ipa_type != IPSEC_ACT_APPLY) &&
7650Sstevel@tonic-gate 	    (act->ipa_apply.ipp_use_ah ||
7665240Snordmark 	    act->ipa_apply.ipp_use_esp ||
7675240Snordmark 	    act->ipa_apply.ipp_use_espa ||
7685240Snordmark 	    act->ipa_apply.ipp_use_se ||
7695240Snordmark 	    act->ipa_apply.ipp_use_unique)) {
7700Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7710Sstevel@tonic-gate 		return (B_FALSE);
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 	if ((act->ipa_type == IPSEC_ACT_APPLY) &&
7740Sstevel@tonic-gate 	    !act->ipa_apply.ipp_use_ah &&
7750Sstevel@tonic-gate 	    !act->ipa_apply.ipp_use_esp) {
7760Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7770Sstevel@tonic-gate 		return (B_FALSE);
7780Sstevel@tonic-gate 	}
7793448Sdh155122 	return (ipsec_check_action(act, diag, spds->spds_netstack));
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate  * We may be short a few error checks here..
7840Sstevel@tonic-gate  */
7850Sstevel@tonic-gate static boolean_t
7860Sstevel@tonic-gate spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp,
7873448Sdh155122     int *diag, spd_stack_t *spds)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	struct spd_ext_actions *sactp =
7900Sstevel@tonic-gate 	    (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
7910Sstevel@tonic-gate 	ipsec_act_t act, *actp, *endactp;
7920Sstevel@tonic-gate 	struct spd_attribute *attrp, *endattrp;
7930Sstevel@tonic-gate 	uint64_t *endp;
7940Sstevel@tonic-gate 	int nact;
7953055Sdanmcd 	boolean_t tunnel_polhead;
7963055Sdanmcd 
7973055Sdanmcd 	tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL &&
7983055Sdanmcd 	    (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags &
7995240Snordmark 	    SPD_RULE_FLAG_TUNNEL));
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	*actpp = NULL;
8020Sstevel@tonic-gate 	*nactp = 0;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	if (sactp == NULL) {
8050Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_NO_ACTION_EXT;
8060Sstevel@tonic-gate 		return (B_FALSE);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * Parse the "action" extension and convert into an action chain.
8110Sstevel@tonic-gate 	 */
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	nact = sactp->spd_actions_count;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	endp = (uint64_t *)sactp;
8160Sstevel@tonic-gate 	endp += sactp->spd_actions_len;
8170Sstevel@tonic-gate 	endattrp = (struct spd_attribute *)endp;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP);
8200Sstevel@tonic-gate 	if (actp == NULL) {
8210Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_NO_MEM;
8220Sstevel@tonic-gate 		return (B_FALSE);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 	*actpp = actp;
8250Sstevel@tonic-gate 	*nactp = nact;
8260Sstevel@tonic-gate 	endactp = actp + nact;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	spdsock_reset_act(&act);
8290Sstevel@tonic-gate 	attrp = (struct spd_attribute *)(&sactp[1]);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	for (; attrp < endattrp; attrp++) {
8320Sstevel@tonic-gate 		switch (attrp->spd_attr_tag) {
8330Sstevel@tonic-gate 		case SPD_ATTR_NOP:
8340Sstevel@tonic-gate 			break;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		case SPD_ATTR_EMPTY:
8370Sstevel@tonic-gate 			spdsock_reset_act(&act);
8380Sstevel@tonic-gate 			break;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 		case SPD_ATTR_END:
8410Sstevel@tonic-gate 			attrp = endattrp;
8420Sstevel@tonic-gate 			/* FALLTHRU */
8430Sstevel@tonic-gate 		case SPD_ATTR_NEXT:
8440Sstevel@tonic-gate 			if (actp >= endactp) {
8450Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
8460Sstevel@tonic-gate 				goto fail;
8470Sstevel@tonic-gate 			}
8483448Sdh155122 			if (!spdsock_check_action(&act, tunnel_polhead,
8493448Sdh155122 			    diag, spds))
8500Sstevel@tonic-gate 				goto fail;
8510Sstevel@tonic-gate 			*actp++ = act;
8523055Sdanmcd 			spdsock_reset_act(&act);
8530Sstevel@tonic-gate 			break;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		case SPD_ATTR_TYPE:
8560Sstevel@tonic-gate 			if (!spd_convert_type(attrp->spd_attr_value, &act)) {
8570Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE;
8580Sstevel@tonic-gate 				goto fail;
8590Sstevel@tonic-gate 			}
8600Sstevel@tonic-gate 			break;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		case SPD_ATTR_FLAGS:
8633055Sdanmcd 			if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) {
8643055Sdanmcd 				/*
8653055Sdanmcd 				 * Set "sa unique" for transport-mode
8663055Sdanmcd 				 * tunnels whether we want to or not.
8673055Sdanmcd 				 */
8683055Sdanmcd 				attrp->spd_attr_value |= SPD_APPLY_UNIQUE;
8693055Sdanmcd 			}
8700Sstevel@tonic-gate 			if (!spd_convert_flags(attrp->spd_attr_value, &act)) {
8710Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS;
8720Sstevel@tonic-gate 				goto fail;
8730Sstevel@tonic-gate 			}
8740Sstevel@tonic-gate 			break;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 		case SPD_ATTR_AH_AUTH:
8773055Sdanmcd 			if (attrp->spd_attr_value == 0) {
8783055Sdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG;
8793055Sdanmcd 				goto fail;
8803055Sdanmcd 			}
8810Sstevel@tonic-gate 			act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value;
8820Sstevel@tonic-gate 			break;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 		case SPD_ATTR_ESP_ENCR:
8853055Sdanmcd 			if (attrp->spd_attr_value == 0) {
8863055Sdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG;
8873055Sdanmcd 				goto fail;
8883055Sdanmcd 			}
8890Sstevel@tonic-gate 			act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value;
8900Sstevel@tonic-gate 			break;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 		case SPD_ATTR_ESP_AUTH:
8933055Sdanmcd 			if (attrp->spd_attr_value == 0) {
8943055Sdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG;
8953055Sdanmcd 				goto fail;
8963055Sdanmcd 			}
8970Sstevel@tonic-gate 			act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value;
8980Sstevel@tonic-gate 			break;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 		case SPD_ATTR_ENCR_MINBITS:
9010Sstevel@tonic-gate 			act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value;
9020Sstevel@tonic-gate 			break;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 		case SPD_ATTR_ENCR_MAXBITS:
9050Sstevel@tonic-gate 			act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value;
9060Sstevel@tonic-gate 			break;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		case SPD_ATTR_AH_MINBITS:
9090Sstevel@tonic-gate 			act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value;
9100Sstevel@tonic-gate 			break;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 		case SPD_ATTR_AH_MAXBITS:
9130Sstevel@tonic-gate 			act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value;
9140Sstevel@tonic-gate 			break;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		case SPD_ATTR_ESPA_MINBITS:
9170Sstevel@tonic-gate 			act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value;
9180Sstevel@tonic-gate 			break;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		case SPD_ATTR_ESPA_MAXBITS:
9210Sstevel@tonic-gate 			act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value;
9220Sstevel@tonic-gate 			break;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		case SPD_ATTR_LIFE_SOFT_TIME:
9250Sstevel@tonic-gate 		case SPD_ATTR_LIFE_HARD_TIME:
9260Sstevel@tonic-gate 		case SPD_ATTR_LIFE_SOFT_BYTES:
9270Sstevel@tonic-gate 		case SPD_ATTR_LIFE_HARD_BYTES:
9280Sstevel@tonic-gate 			break;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 		case SPD_ATTR_KM_PROTO:
9310Sstevel@tonic-gate 			act.ipa_apply.ipp_km_proto = attrp->spd_attr_value;
9320Sstevel@tonic-gate 			break;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		case SPD_ATTR_KM_COOKIE:
9350Sstevel@tonic-gate 			act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value;
9360Sstevel@tonic-gate 			break;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 		case SPD_ATTR_REPLAY_DEPTH:
9390Sstevel@tonic-gate 			act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value;
9400Sstevel@tonic-gate 			break;
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 	if (actp != endactp) {
9440Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
9450Sstevel@tonic-gate 		goto fail;
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	return (B_TRUE);
9490Sstevel@tonic-gate fail:
9500Sstevel@tonic-gate 	ipsec_actvec_free(*actpp, nact);
9510Sstevel@tonic-gate 	*actpp = NULL;
9520Sstevel@tonic-gate 	return (B_FALSE);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate typedef struct
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	ipsec_policy_t *pol;
9580Sstevel@tonic-gate 	int dir;
9590Sstevel@tonic-gate } tmprule_t;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate static int
9620Sstevel@tonic-gate mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule,
9630Sstevel@tonic-gate     ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af,
9643448Sdh155122     tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate 	ipsec_policy_t *pol;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4);
9690Sstevel@tonic-gate 	sel->ipsl_valid |= af;
9700Sstevel@tonic-gate 
9713055Sdanmcd 	pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority,
9723448Sdh155122 	    index, spds->spds_netstack);
9730Sstevel@tonic-gate 	if (pol == NULL)
9740Sstevel@tonic-gate 		return (ENOMEM);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	(*rp)->pol = pol;
9770Sstevel@tonic-gate 	(*rp)->dir = dir;
9780Sstevel@tonic-gate 	(*rp)++;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if (!ipsec_check_policy(iph, pol, dir))
9810Sstevel@tonic-gate 		return (EEXIST);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	rule->spd_rule_index = pol->ipsp_index;
9840Sstevel@tonic-gate 	return (0);
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate static int
9880Sstevel@tonic-gate mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule,
9890Sstevel@tonic-gate     ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs,
9903448Sdh155122     tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate 	int error;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	if (afs & IPSL_IPV4) {
9953055Sdanmcd 		error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp,
9963448Sdh155122 		    index, spds);
9970Sstevel@tonic-gate 		if (error != 0)
9980Sstevel@tonic-gate 			return (error);
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate 	if (afs & IPSL_IPV6) {
10013055Sdanmcd 		error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp,
10023448Sdh155122 		    index, spds);
10030Sstevel@tonic-gate 		if (error != 0)
10040Sstevel@tonic-gate 			return (error);
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 	return (0);
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate static void
10113055Sdanmcd spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
10123055Sdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate 	ipsec_selkey_t sel;
10150Sstevel@tonic-gate 	ipsec_act_t *actp;
10160Sstevel@tonic-gate 	uint_t nact;
10173055Sdanmcd 	int diag = 0, error, afs;
10180Sstevel@tonic-gate 	struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
10190Sstevel@tonic-gate 	tmprule_t rules[4], *rulep = &rules[0];
10203055Sdanmcd 	boolean_t tunnel_mode, empty_itp, active;
10213055Sdanmcd 	uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index;
10223448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
10233448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (rule == NULL) {
10260Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
10274307Spwernau 		if (audit_active) {
10284307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1029*8778SErik.Nordmark@Sun.COM 			cred_t *cr;
1030*8778SErik.Nordmark@Sun.COM 			pid_t cpid;
1031*8778SErik.Nordmark@Sun.COM 
1032*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
10334307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1034*8778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_ADDRULE, cr,
10354307Spwernau 			    spds->spds_netstack, ITP_NAME(itp), active,
1036*8778SErik.Nordmark@Sun.COM 			    SPD_DIAGNOSTIC_NO_RULE_EXT, cpid);
10374307Spwernau 		}
10380Sstevel@tonic-gate 		return;
10390Sstevel@tonic-gate 	}
10400Sstevel@tonic-gate 
10413055Sdanmcd 	tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL);
10423055Sdanmcd 
10433055Sdanmcd 	if (itp != NULL) {
10443055Sdanmcd 		mutex_enter(&itp->itp_lock);
10453055Sdanmcd 		ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph);
10463055Sdanmcd 		active = (itp->itp_policy == iph);
10473055Sdanmcd 		if (ITP_P_ISACTIVE(itp, iph)) {
10483055Sdanmcd 			/* Check for mix-and-match of tunnel/transport. */
10493055Sdanmcd 			if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) ||
10503055Sdanmcd 			    (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) {
10513055Sdanmcd 				mutex_exit(&itp->itp_lock);
10523055Sdanmcd 				spdsock_error(q, mp, EBUSY, 0);
10533055Sdanmcd 				return;
10543055Sdanmcd 			}
10553055Sdanmcd 			empty_itp = B_FALSE;
10563055Sdanmcd 		} else {
10573055Sdanmcd 			empty_itp = B_TRUE;
10583055Sdanmcd 			itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE;
10593055Sdanmcd 			if (tunnel_mode)
10603055Sdanmcd 				itp->itp_flags |= active ? ITPF_P_TUNNEL :
10613055Sdanmcd 				    ITPF_I_TUNNEL;
10623055Sdanmcd 		}
10633055Sdanmcd 	} else {
10643055Sdanmcd 		empty_itp = B_FALSE;
10653055Sdanmcd 	}
10663055Sdanmcd 
10670Sstevel@tonic-gate 	if (rule->spd_rule_index != 0) {
10683055Sdanmcd 		diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX;
10693055Sdanmcd 		error = EINVAL;
10703055Sdanmcd 		goto fail2;
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
10743055Sdanmcd 		error = EINVAL;
10753055Sdanmcd 		goto fail2;
10763055Sdanmcd 	}
10773055Sdanmcd 
10783055Sdanmcd 	if (itp != NULL) {
10793055Sdanmcd 		if (tunnel_mode) {
10803055Sdanmcd 			if (sel.ipsl_valid &
10813055Sdanmcd 			    (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) {
10823055Sdanmcd 				itp->itp_flags |= active ?
10833055Sdanmcd 				    ITPF_P_PER_PORT_SECURITY :
10843055Sdanmcd 				    ITPF_I_PER_PORT_SECURITY;
10853055Sdanmcd 			}
10863055Sdanmcd 		} else {
10873055Sdanmcd 			/*
10883055Sdanmcd 			 * For now, we don't allow transport-mode on a tunnel
10893055Sdanmcd 			 * with ANY specific selectors.  Bail if we have such
10903055Sdanmcd 			 * a request.
10913055Sdanmcd 			 */
10923055Sdanmcd 			if (sel.ipsl_valid & IPSL_WILDCARD) {
10933055Sdanmcd 				diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS;
10943055Sdanmcd 				error = EINVAL;
10953055Sdanmcd 				goto fail2;
10963055Sdanmcd 			}
10973055Sdanmcd 		}
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11003448Sdh155122 	if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) {
11013055Sdanmcd 		error = EINVAL;
11023055Sdanmcd 		goto fail2;
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 	/*
11050Sstevel@tonic-gate 	 * If no addresses were specified, add both.
11060Sstevel@tonic-gate 	 */
11070Sstevel@tonic-gate 	afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4);
11080Sstevel@tonic-gate 	if (afs == 0)
11090Sstevel@tonic-gate 		afs = (IPSL_IPV6|IPSL_IPV4);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_WRITER);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) {
11140Sstevel@tonic-gate 		error = mkrulepair(iph, rule, &sel, actp, nact,
11153448Sdh155122 		    IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds);
11160Sstevel@tonic-gate 		if (error != 0)
11170Sstevel@tonic-gate 			goto fail;
11180Sstevel@tonic-gate 	}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) {
11210Sstevel@tonic-gate 		error = mkrulepair(iph, rule, &sel, actp, nact,
11223448Sdh155122 		    IPSEC_TYPE_INBOUND, afs, &rulep, index, spds);
11230Sstevel@tonic-gate 		if (error != 0)
11240Sstevel@tonic-gate 			goto fail;
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 
11273448Sdh155122 	while ((--rulep) >= &rules[0]) {
11283448Sdh155122 		ipsec_enter_policy(iph, rulep->pol, rulep->dir,
11293448Sdh155122 		    spds->spds_netstack);
11303448Sdh155122 	}
11310Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
11323055Sdanmcd 	if (itp != NULL)
11333055Sdanmcd 		mutex_exit(&itp->itp_lock);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	ipsec_actvec_free(actp, nact);
11360Sstevel@tonic-gate 	spd_echo(q, mp);
11374307Spwernau 	if (audit_active) {
11384307Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1139*8778SErik.Nordmark@Sun.COM 		cred_t *cr;
1140*8778SErik.Nordmark@Sun.COM 		pid_t cpid;
1141*8778SErik.Nordmark@Sun.COM 
1142*8778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
11434307Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1144*8778SErik.Nordmark@Sun.COM 		audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1145*8778SErik.Nordmark@Sun.COM 		    ITP_NAME(itp), active, 0, cpid);
11464307Spwernau 	}
11470Sstevel@tonic-gate 	return;
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate fail:
11500Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
11510Sstevel@tonic-gate 	while ((--rulep) >= &rules[0]) {
11523448Sdh155122 		IPPOL_REFRELE(rulep->pol, spds->spds_netstack);
11530Sstevel@tonic-gate 	}
11540Sstevel@tonic-gate 	ipsec_actvec_free(actp, nact);
11553055Sdanmcd fail2:
11563055Sdanmcd 	if (itp != NULL) {
11573055Sdanmcd 		if (empty_itp)
11583055Sdanmcd 			itp->itp_flags = 0;
11593055Sdanmcd 		mutex_exit(&itp->itp_lock);
11603055Sdanmcd 	}
11613055Sdanmcd 	spdsock_error(q, mp, error, diag);
11624307Spwernau 	if (audit_active) {
11634307Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1164*8778SErik.Nordmark@Sun.COM 		cred_t *cr;
1165*8778SErik.Nordmark@Sun.COM 		pid_t cpid;
1166*8778SErik.Nordmark@Sun.COM 
1167*8778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
11684307Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1169*8778SErik.Nordmark@Sun.COM 		audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1170*8778SErik.Nordmark@Sun.COM 		    ITP_NAME(itp), active, error, cpid);
11714307Spwernau 	}
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate void
11753055Sdanmcd spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
11763055Sdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
11770Sstevel@tonic-gate {
11780Sstevel@tonic-gate 	ipsec_selkey_t sel;
11790Sstevel@tonic-gate 	struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
11803055Sdanmcd 	int err, diag = 0;
11813448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
11826430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if (rule == NULL) {
11850Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
11864307Spwernau 		if (audit_active) {
11874307Spwernau 			boolean_t active;
11884307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1189*8778SErik.Nordmark@Sun.COM 			cred_t *cr;
1190*8778SErik.Nordmark@Sun.COM 			pid_t cpid;
1191*8778SErik.Nordmark@Sun.COM 
1192*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
11934307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1194*8778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_DELETERULE, cr, ns,
11956430Sdanmcd 			    ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT,
1196*8778SErik.Nordmark@Sun.COM 			    cpid);
11974307Spwernau 		}
11980Sstevel@tonic-gate 		return;
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 
12013055Sdanmcd 	/*
12023055Sdanmcd 	 * Must enter itp_lock first to avoid deadlock.  See tun.c's
12033055Sdanmcd 	 * set_sec_simple() for the other case of itp_lock and iph_lock.
12043055Sdanmcd 	 */
12053055Sdanmcd 	if (itp != NULL)
12063055Sdanmcd 		mutex_enter(&itp->itp_lock);
12073055Sdanmcd 
12080Sstevel@tonic-gate 	if (rule->spd_rule_index != 0) {
12096430Sdanmcd 		if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) !=
12106430Sdanmcd 		    0) {
12113055Sdanmcd 			err = ESRCH;
12123055Sdanmcd 			goto fail;
12130Sstevel@tonic-gate 		}
12140Sstevel@tonic-gate 	} else {
12150Sstevel@tonic-gate 		if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
12163055Sdanmcd 			err = EINVAL;	/* diag already set... */
12173055Sdanmcd 			goto fail;
12183055Sdanmcd 		}
12193055Sdanmcd 
12203055Sdanmcd 		if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) &&
12216430Sdanmcd 		    !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) {
12223055Sdanmcd 			err = ESRCH;
12233055Sdanmcd 			goto fail;
12243055Sdanmcd 		}
12253055Sdanmcd 
12263055Sdanmcd 		if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) &&
12276430Sdanmcd 		    !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) {
12283055Sdanmcd 			err = ESRCH;
12293055Sdanmcd 			goto fail;
12300Sstevel@tonic-gate 		}
12313055Sdanmcd 	}
12323055Sdanmcd 
12333055Sdanmcd 	if (itp != NULL) {
12343055Sdanmcd 		ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive);
12353055Sdanmcd 		rw_enter(&iph->iph_lock, RW_READER);
12363055Sdanmcd 		if (avl_numnodes(&iph->iph_rulebyid) == 0) {
12373055Sdanmcd 			if (iph == itp->itp_policy)
12383055Sdanmcd 				itp->itp_flags &= ~ITPF_PFLAGS;
12393055Sdanmcd 			else
12403055Sdanmcd 				itp->itp_flags &= ~ITPF_IFLAGS;
12410Sstevel@tonic-gate 		}
12423055Sdanmcd 		/* Can exit locks in any order. */
12433055Sdanmcd 		rw_exit(&iph->iph_lock);
12443055Sdanmcd 		mutex_exit(&itp->itp_lock);
12450Sstevel@tonic-gate 	}
12460Sstevel@tonic-gate 	spd_echo(q, mp);
12474307Spwernau 	if (audit_active) {
12484307Spwernau 		boolean_t active;
12494307Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1250*8778SErik.Nordmark@Sun.COM 		cred_t *cr;
1251*8778SErik.Nordmark@Sun.COM 		pid_t cpid;
1252*8778SErik.Nordmark@Sun.COM 
1253*8778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
12544307Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1255*8778SErik.Nordmark@Sun.COM 		audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1256*8778SErik.Nordmark@Sun.COM 		    active, 0, cpid);
12574307Spwernau 	}
12580Sstevel@tonic-gate 	return;
12590Sstevel@tonic-gate fail:
12603055Sdanmcd 	if (itp != NULL)
12613055Sdanmcd 		mutex_exit(&itp->itp_lock);
12623055Sdanmcd 	spdsock_error(q, mp, err, diag);
12634307Spwernau 	if (audit_active) {
12644307Spwernau 		boolean_t active;
12654307Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1266*8778SErik.Nordmark@Sun.COM 		cred_t *cr;
1267*8778SErik.Nordmark@Sun.COM 		pid_t cpid;
1268*8778SErik.Nordmark@Sun.COM 
1269*8778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
12704307Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1271*8778SErik.Nordmark@Sun.COM 		audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1272*8778SErik.Nordmark@Sun.COM 		    active, err, cpid);
12734307Spwernau 	}
12743055Sdanmcd }
12753055Sdanmcd 
12763055Sdanmcd /* Do NOT consume a reference to itp. */
12773055Sdanmcd /* ARGSUSED */
12783055Sdanmcd static void
12793448Sdh155122 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns)
12803055Sdanmcd {
12813055Sdanmcd 	mutex_enter(&itp->itp_lock);
12823055Sdanmcd 	ITPF_SWAP(itp->itp_flags);
12833448Sdh155122 	ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns);
12843055Sdanmcd 	mutex_exit(&itp->itp_lock);
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate void
12883055Sdanmcd spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname)
12890Sstevel@tonic-gate {
12903055Sdanmcd 	char *tname;
12913055Sdanmcd 	ipsec_tun_pol_t *itp;
12923448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
12936430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
12943055Sdanmcd 
12953055Sdanmcd 	if (tunname != NULL) {
12963055Sdanmcd 		tname = (char *)tunname->spd_if_name;
12973055Sdanmcd 		if (*tname == '\0') {
12983448Sdh155122 			/* can't fail */
12996430Sdanmcd 			ipsec_swap_global_policy(ns);
13004307Spwernau 			if (audit_active) {
13014307Spwernau 				boolean_t active;
13024307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1303*8778SErik.Nordmark@Sun.COM 				cred_t *cr;
1304*8778SErik.Nordmark@Sun.COM 				pid_t cpid;
1305*8778SErik.Nordmark@Sun.COM 
1306*8778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
13074307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1308*8778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_FLIP, cr, ns,
1309*8778SErik.Nordmark@Sun.COM 				    NULL, active, 0, cpid);
13104307Spwernau 			}
13116430Sdanmcd 			itp_walk(spdsock_flip_node, NULL, ns);
13124307Spwernau 			if (audit_active) {
13134307Spwernau 				boolean_t active;
13144307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1315*8778SErik.Nordmark@Sun.COM 				cred_t *cr;
1316*8778SErik.Nordmark@Sun.COM 				pid_t cpid;
1317*8778SErik.Nordmark@Sun.COM 
1318*8778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
13194307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1320*8778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_FLIP, cr, ns,
1321*8778SErik.Nordmark@Sun.COM 				    "all tunnels", active, 0, cpid);
13224307Spwernau 			}
13233055Sdanmcd 		} else {
13246430Sdanmcd 			itp = get_tunnel_policy(tname, ns);
13253055Sdanmcd 			if (itp == NULL) {
13263055Sdanmcd 				/* Better idea for "tunnel not found"? */
13273055Sdanmcd 				spdsock_error(q, mp, ESRCH, 0);
13284307Spwernau 				if (audit_active) {
13294307Spwernau 					boolean_t active;
13304307Spwernau 					spd_msg_t *spmsg =
13314307Spwernau 					    (spd_msg_t *)mp->b_rptr;
1332*8778SErik.Nordmark@Sun.COM 					cred_t *cr;
1333*8778SErik.Nordmark@Sun.COM 					pid_t cpid;
1334*8778SErik.Nordmark@Sun.COM 
1335*8778SErik.Nordmark@Sun.COM 					cr = msg_getcred(mp, &cpid);
13364307Spwernau 					active = (spmsg->spd_msg_spdid ==
13374307Spwernau 					    SPD_ACTIVE);
1338*8778SErik.Nordmark@Sun.COM 					audit_pf_policy(SPD_FLIP, cr, ns,
1339*8778SErik.Nordmark@Sun.COM 					    ITP_NAME(itp), active,
1340*8778SErik.Nordmark@Sun.COM 					    ESRCH, cpid);
13414307Spwernau 				}
13423055Sdanmcd 				return;
13433055Sdanmcd 			}
13443448Sdh155122 			spdsock_flip_node(itp, NULL, NULL);
13454307Spwernau 			if (audit_active) {
13464307Spwernau 				boolean_t active;
13474307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1348*8778SErik.Nordmark@Sun.COM 				cred_t *cr;
1349*8778SErik.Nordmark@Sun.COM 				pid_t cpid;
1350*8778SErik.Nordmark@Sun.COM 
1351*8778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
13524307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1353*8778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_FLIP, cr, ns,
1354*8778SErik.Nordmark@Sun.COM 				    ITP_NAME(itp), active, 0, cpid);
13554307Spwernau 			}
13566430Sdanmcd 			ITP_REFRELE(itp, ns);
13573055Sdanmcd 		}
13583055Sdanmcd 	} else {
13596430Sdanmcd 		ipsec_swap_global_policy(ns);	/* can't fail */
13604307Spwernau 		if (audit_active) {
13614307Spwernau 			boolean_t active;
13624307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1363*8778SErik.Nordmark@Sun.COM 			cred_t *cr;
1364*8778SErik.Nordmark@Sun.COM 			pid_t cpid;
1365*8778SErik.Nordmark@Sun.COM 
1366*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
13674307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1368*8778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_FLIP, cr,
1369*8778SErik.Nordmark@Sun.COM 			    ns, NULL, active, 0, cpid);
13704307Spwernau 		}
13713055Sdanmcd 	}
13720Sstevel@tonic-gate 	spd_echo(q, mp);
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate /*
13760Sstevel@tonic-gate  * Unimplemented feature
13770Sstevel@tonic-gate  */
13780Sstevel@tonic-gate /* ARGSUSED */
13790Sstevel@tonic-gate static void
13803055Sdanmcd spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
13813055Sdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
13820Sstevel@tonic-gate {
13830Sstevel@tonic-gate 	spdsock_error(q, mp, EINVAL, 0);
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate static mblk_t *
13880Sstevel@tonic-gate spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph,
13890Sstevel@tonic-gate     uint32_t count, uint16_t error)
13900Sstevel@tonic-gate {
13910Sstevel@tonic-gate 	size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t);
13920Sstevel@tonic-gate 	spd_msg_t *msg;
13930Sstevel@tonic-gate 	spd_ruleset_ext_t *ruleset;
13940Sstevel@tonic-gate 	mblk_t *m = allocb(len, BPRI_HI);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&iph->iph_lock));
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	if (m == NULL) {
13990Sstevel@tonic-gate 		return (NULL);
14000Sstevel@tonic-gate 	}
14010Sstevel@tonic-gate 	msg = (spd_msg_t *)m->b_rptr;
14020Sstevel@tonic-gate 	ruleset = (spd_ruleset_ext_t *)(&msg[1]);
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	m->b_wptr = (uint8_t *)&ruleset[1];
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	*msg = *(spd_msg_t *)(req->b_rptr);
14070Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(len);
14080Sstevel@tonic-gate 	msg->spd_msg_errno = error;
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset));
14110Sstevel@tonic-gate 	ruleset->spd_ruleset_type = SPD_EXT_RULESET;
14120Sstevel@tonic-gate 	ruleset->spd_ruleset_count = count;
14130Sstevel@tonic-gate 	ruleset->spd_ruleset_version = iph->iph_gen;
14140Sstevel@tonic-gate 	return (m);
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate static mblk_t *
14180Sstevel@tonic-gate spdsock_dump_finish(spdsock_t *ss, int error)
14190Sstevel@tonic-gate {
14200Sstevel@tonic-gate 	mblk_t *m;
14210Sstevel@tonic-gate 	ipsec_policy_head_t *iph = ss->spdsock_dump_head;
14220Sstevel@tonic-gate 	mblk_t *req = ss->spdsock_dump_req;
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
14250Sstevel@tonic-gate 	m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error);
14260Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
14276430Sdanmcd 	IPPH_REFRELE(iph, ss->spdsock_spds->spds_netstack);
14280Sstevel@tonic-gate 	ss->spdsock_dump_req = NULL;
14290Sstevel@tonic-gate 	freemsg(req);
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	return (m);
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate /*
14350Sstevel@tonic-gate  * Rule encoding functions.
14360Sstevel@tonic-gate  * We do a two-pass encode.
14370Sstevel@tonic-gate  * If base != NULL, fill in encoded rule part starting at base+offset.
14380Sstevel@tonic-gate  * Always return "offset" plus length of to-be-encoded data.
14390Sstevel@tonic-gate  */
14400Sstevel@tonic-gate static uint_t
14410Sstevel@tonic-gate spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type,
14420Sstevel@tonic-gate     uint8_t type_end, uint8_t code, uint8_t code_end)
14430Sstevel@tonic-gate {
14440Sstevel@tonic-gate 	struct spd_typecode *tcp;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	if (base != NULL) {
14490Sstevel@tonic-gate 		tcp = (struct spd_typecode *)(base + offset);
14500Sstevel@tonic-gate 		tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp));
14510Sstevel@tonic-gate 		tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE;
14520Sstevel@tonic-gate 		tcp->spd_typecode_code = code;
14530Sstevel@tonic-gate 		tcp->spd_typecode_type = type;
14540Sstevel@tonic-gate 		tcp->spd_typecode_type_end = type_end;
14550Sstevel@tonic-gate 		tcp->spd_typecode_code_end = code_end;
14560Sstevel@tonic-gate 	}
14570Sstevel@tonic-gate 	offset += sizeof (*tcp);
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	return (offset);
14620Sstevel@tonic-gate }
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate static uint_t
14650Sstevel@tonic-gate spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto)
14660Sstevel@tonic-gate {
14670Sstevel@tonic-gate 	struct spd_proto *spp;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	if (base != NULL) {
14720Sstevel@tonic-gate 		spp = (struct spd_proto *)(base + offset);
14730Sstevel@tonic-gate 		spp->spd_proto_len = SPD_8TO64(sizeof (*spp));
14740Sstevel@tonic-gate 		spp->spd_proto_exttype = SPD_EXT_PROTO;
14750Sstevel@tonic-gate 		spp->spd_proto_number = proto;
14760Sstevel@tonic-gate 		spp->spd_proto_reserved1 = 0;
14770Sstevel@tonic-gate 		spp->spd_proto_reserved2 = 0;
14780Sstevel@tonic-gate 	}
14790Sstevel@tonic-gate 	offset += sizeof (*spp);
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	return (offset);
14840Sstevel@tonic-gate }
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate static uint_t
14870Sstevel@tonic-gate spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port)
14880Sstevel@tonic-gate {
14890Sstevel@tonic-gate 	struct spd_portrange *spp;
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	if (base != NULL) {
14940Sstevel@tonic-gate 		spp = (struct spd_portrange *)(base + offset);
14950Sstevel@tonic-gate 		spp->spd_ports_len = SPD_8TO64(sizeof (*spp));
14960Sstevel@tonic-gate 		spp->spd_ports_exttype = ext;
14970Sstevel@tonic-gate 		spp->spd_ports_minport = port;
14980Sstevel@tonic-gate 		spp->spd_ports_maxport = port;
14990Sstevel@tonic-gate 	}
15000Sstevel@tonic-gate 	offset += sizeof (*spp);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	return (offset);
15050Sstevel@tonic-gate }
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate static uint_t
15080Sstevel@tonic-gate spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext,
15090Sstevel@tonic-gate     const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen)
15100Sstevel@tonic-gate {
15110Sstevel@tonic-gate 	struct spd_address *sae;
15120Sstevel@tonic-gate 	ipsec_addr_t *spdaddr;
15130Sstevel@tonic-gate 	uint_t start = offset;
15140Sstevel@tonic-gate 	uint_t addrlen;
15150Sstevel@tonic-gate 	uint_t af;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	if (sel->ipsl_valid & IPSL_IPV4) {
15180Sstevel@tonic-gate 		af = AF_INET;
15190Sstevel@tonic-gate 		addrlen = IP_ADDR_LEN;
15200Sstevel@tonic-gate 	} else {
15210Sstevel@tonic-gate 		af = AF_INET6;
15220Sstevel@tonic-gate 		addrlen = IPV6_ADDR_LEN;
15230Sstevel@tonic-gate 	}
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	if (base != NULL) {
15280Sstevel@tonic-gate 		sae = (struct spd_address *)(base + offset);
15290Sstevel@tonic-gate 		sae->spd_address_exttype = ext;
15300Sstevel@tonic-gate 		sae->spd_address_af = af;
15310Sstevel@tonic-gate 		sae->spd_address_prefixlen = pfxlen;
15320Sstevel@tonic-gate 		sae->spd_address_reserved2 = 0;
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 		spdaddr = (ipsec_addr_t *)(&sae[1]);
15350Sstevel@tonic-gate 		bcopy(addr, spdaddr, addrlen);
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 	offset += sizeof (*sae);
15380Sstevel@tonic-gate 	addrlen = roundup(addrlen, sizeof (uint64_t));
15390Sstevel@tonic-gate 	offset += addrlen;
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	if (base != NULL)
15440Sstevel@tonic-gate 		sae->spd_address_len = SPD_8TO64(offset - start);
15450Sstevel@tonic-gate 	return (offset);
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate static uint_t
15490Sstevel@tonic-gate spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate 	const ipsec_selkey_t *selkey = &sel->ipsl_key;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_PROTOCOL)
15540Sstevel@tonic-gate 		offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto);
15550Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_LOCAL_PORT)
15560Sstevel@tonic-gate 		offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT,
15570Sstevel@tonic-gate 		    selkey->ipsl_lport);
15580Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_REMOTE_PORT)
15590Sstevel@tonic-gate 		offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT,
15600Sstevel@tonic-gate 		    selkey->ipsl_rport);
15610Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_REMOTE_ADDR)
15620Sstevel@tonic-gate 		offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR,
15630Sstevel@tonic-gate 		    selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen);
15640Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_LOCAL_ADDR)
15650Sstevel@tonic-gate 		offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR,
15660Sstevel@tonic-gate 		    selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen);
15670Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_ICMP_TYPE) {
15680Sstevel@tonic-gate 		offset = spdsock_encode_typecode(base, offset,
15690Sstevel@tonic-gate 		    selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end,
15700Sstevel@tonic-gate 		    (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
15715240Snordmark 		    selkey->ipsl_icmp_code : 255,
15720Sstevel@tonic-gate 		    (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
15735240Snordmark 		    selkey->ipsl_icmp_code_end : 255);
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 	return (offset);
15760Sstevel@tonic-gate }
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate static uint_t
15790Sstevel@tonic-gate spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag,
15800Sstevel@tonic-gate     uint32_t value)
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate 	struct spd_attribute *attr;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	if (base != NULL) {
15870Sstevel@tonic-gate 		attr = (struct spd_attribute *)(base + offset);
15880Sstevel@tonic-gate 		attr->spd_attr_tag = tag;
15890Sstevel@tonic-gate 		attr->spd_attr_value = value;
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 	offset += sizeof (struct spd_attribute);
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	return (offset);
15960Sstevel@tonic-gate }
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate #define	EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v))
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate static uint_t
16020Sstevel@tonic-gate spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap)
16030Sstevel@tonic-gate {
16040Sstevel@tonic-gate 	const struct ipsec_act *act = &(ap->ipa_act);
16050Sstevel@tonic-gate 	uint_t flags;
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	EMIT(SPD_ATTR_EMPTY, 0);
16080Sstevel@tonic-gate 	switch (act->ipa_type) {
16090Sstevel@tonic-gate 	case IPSEC_ACT_DISCARD:
16100Sstevel@tonic-gate 	case IPSEC_ACT_REJECT:
16110Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP);
16120Sstevel@tonic-gate 		break;
16130Sstevel@tonic-gate 	case IPSEC_ACT_BYPASS:
16140Sstevel@tonic-gate 	case IPSEC_ACT_CLEAR:
16150Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS);
16160Sstevel@tonic-gate 		break;
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	case IPSEC_ACT_APPLY:
16190Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC);
16200Sstevel@tonic-gate 		flags = 0;
16210Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_ah)
16220Sstevel@tonic-gate 			flags |= SPD_APPLY_AH;
16230Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_esp)
16240Sstevel@tonic-gate 			flags |= SPD_APPLY_ESP;
16250Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_espa)
16260Sstevel@tonic-gate 			flags |= SPD_APPLY_ESPA;
16270Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_se)
16280Sstevel@tonic-gate 			flags |= SPD_APPLY_SE;
16290Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_unique)
16300Sstevel@tonic-gate 			flags |= SPD_APPLY_UNIQUE;
16310Sstevel@tonic-gate 		EMIT(SPD_ATTR_FLAGS, flags);
16320Sstevel@tonic-gate 		if (flags & SPD_APPLY_AH) {
16330Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg);
16340Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_MINBITS,
16350Sstevel@tonic-gate 			    act->ipa_apply.ipp_ah_minbits);
16360Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_MAXBITS,
16370Sstevel@tonic-gate 			    act->ipa_apply.ipp_ah_maxbits);
16380Sstevel@tonic-gate 		}
16390Sstevel@tonic-gate 		if (flags & SPD_APPLY_ESP) {
16400Sstevel@tonic-gate 			EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg);
16410Sstevel@tonic-gate 			EMIT(SPD_ATTR_ENCR_MINBITS,
16420Sstevel@tonic-gate 			    act->ipa_apply.ipp_espe_minbits);
16430Sstevel@tonic-gate 			EMIT(SPD_ATTR_ENCR_MAXBITS,
16440Sstevel@tonic-gate 			    act->ipa_apply.ipp_espe_maxbits);
16450Sstevel@tonic-gate 			if (flags & SPD_APPLY_ESPA) {
16460Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESP_AUTH,
16470Sstevel@tonic-gate 				    act->ipa_apply.ipp_esp_auth_alg);
16480Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESPA_MINBITS,
16490Sstevel@tonic-gate 				    act->ipa_apply.ipp_espa_minbits);
16500Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESPA_MAXBITS,
16510Sstevel@tonic-gate 				    act->ipa_apply.ipp_espa_maxbits);
16520Sstevel@tonic-gate 			}
16530Sstevel@tonic-gate 		}
16540Sstevel@tonic-gate 		if (act->ipa_apply.ipp_km_proto != 0)
16550Sstevel@tonic-gate 			EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto);
16560Sstevel@tonic-gate 		if (act->ipa_apply.ipp_km_cookie != 0)
16570Sstevel@tonic-gate 			EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie);
16580Sstevel@tonic-gate 		if (act->ipa_apply.ipp_replay_depth != 0)
16590Sstevel@tonic-gate 			EMIT(SPD_ATTR_REPLAY_DEPTH,
16600Sstevel@tonic-gate 			    act->ipa_apply.ipp_replay_depth);
16610Sstevel@tonic-gate 		/* Add more here */
16620Sstevel@tonic-gate 		break;
16630Sstevel@tonic-gate 	}
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	return (offset);
16660Sstevel@tonic-gate }
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate static uint_t
16690Sstevel@tonic-gate spdsock_encode_action_list(uint8_t *base, uint_t offset,
16700Sstevel@tonic-gate     const ipsec_action_t *ap)
16710Sstevel@tonic-gate {
16720Sstevel@tonic-gate 	struct spd_ext_actions *act;
16730Sstevel@tonic-gate 	uint_t nact = 0;
16740Sstevel@tonic-gate 	uint_t start = offset;
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	if (base != NULL) {
16790Sstevel@tonic-gate 		act = (struct spd_ext_actions *)(base + offset);
16800Sstevel@tonic-gate 		act->spd_actions_len = 0;
16810Sstevel@tonic-gate 		act->spd_actions_exttype = SPD_EXT_ACTION;
16820Sstevel@tonic-gate 		act->spd_actions_count = 0;
16830Sstevel@tonic-gate 		act->spd_actions_reserved = 0;
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	offset += sizeof (*act);
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	while (ap != NULL) {
16910Sstevel@tonic-gate 		offset = spdsock_encode_action(base, offset, ap);
16920Sstevel@tonic-gate 		ap = ap->ipa_next;
16930Sstevel@tonic-gate 		nact++;
16940Sstevel@tonic-gate 		if (ap != NULL) {
16950Sstevel@tonic-gate 			EMIT(SPD_ATTR_NEXT, 0);
16960Sstevel@tonic-gate 		}
16970Sstevel@tonic-gate 	}
16980Sstevel@tonic-gate 	EMIT(SPD_ATTR_END, 0);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	if (base != NULL) {
17030Sstevel@tonic-gate 		act->spd_actions_count = nact;
17040Sstevel@tonic-gate 		act->spd_actions_len = SPD_8TO64(offset - start);
17050Sstevel@tonic-gate 	}
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	return (offset);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate #undef EMIT
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate /* ARGSUSED */
17130Sstevel@tonic-gate static uint_t
17140Sstevel@tonic-gate spdsock_rule_flags(uint_t dir, uint_t af)
17150Sstevel@tonic-gate {
17160Sstevel@tonic-gate 	uint_t flags = 0;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	if (dir == IPSEC_TYPE_INBOUND)
17190Sstevel@tonic-gate 		flags |= SPD_RULE_FLAG_INBOUND;
17200Sstevel@tonic-gate 	if (dir == IPSEC_TYPE_OUTBOUND)
17210Sstevel@tonic-gate 		flags |= SPD_RULE_FLAG_OUTBOUND;
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	return (flags);
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate static uint_t
17283055Sdanmcd spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req,
17293055Sdanmcd     const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name,
17303055Sdanmcd     boolean_t tunnel)
17310Sstevel@tonic-gate {
17320Sstevel@tonic-gate 	struct spd_msg *spmsg;
17330Sstevel@tonic-gate 	struct spd_rule *spr;
17343055Sdanmcd 	spd_if_t *sid;
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	uint_t start = offset;
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	if (base != NULL) {
17410Sstevel@tonic-gate 		spmsg = (struct spd_msg *)(base + offset);
17420Sstevel@tonic-gate 		bzero(spmsg, sizeof (*spmsg));
17430Sstevel@tonic-gate 		spmsg->spd_msg_version = PF_POLICY_V1;
17440Sstevel@tonic-gate 		spmsg->spd_msg_type = SPD_DUMP;
17450Sstevel@tonic-gate 		spmsg->spd_msg_seq = req->spd_msg_seq;
17460Sstevel@tonic-gate 		spmsg->spd_msg_pid = req->spd_msg_pid;
17470Sstevel@tonic-gate 	}
17480Sstevel@tonic-gate 	offset += sizeof (struct spd_msg);
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	if (base != NULL) {
17530Sstevel@tonic-gate 		spr = (struct spd_rule *)(base + offset);
17540Sstevel@tonic-gate 		spr->spd_rule_type = SPD_EXT_RULE;
17550Sstevel@tonic-gate 		spr->spd_rule_priority = rule->ipsp_prio;
17560Sstevel@tonic-gate 		spr->spd_rule_flags = spdsock_rule_flags(dir, af);
17573055Sdanmcd 		if (tunnel)
17583055Sdanmcd 			spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL;
17590Sstevel@tonic-gate 		spr->spd_rule_unused = 0;
17600Sstevel@tonic-gate 		spr->spd_rule_len = SPD_8TO64(sizeof (*spr));
17610Sstevel@tonic-gate 		spr->spd_rule_index = rule->ipsp_index;
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate 	offset += sizeof (struct spd_rule);
17643055Sdanmcd 
17653055Sdanmcd 	/*
17663055Sdanmcd 	 * If we have an interface name (i.e. if this policy head came from
17673055Sdanmcd 	 * a tunnel), add the SPD_EXT_TUN_NAME extension.
17683055Sdanmcd 	 */
17696466Sdanmcd 	if (name != NULL) {
17703055Sdanmcd 
17713055Sdanmcd 		ASSERT(ALIGNED64(offset));
17723055Sdanmcd 
17733055Sdanmcd 		if (base != NULL) {
17743055Sdanmcd 			sid = (spd_if_t *)(base + offset);
17753055Sdanmcd 			sid->spd_if_exttype = SPD_EXT_TUN_NAME;
17763055Sdanmcd 			sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) +
17773055Sdanmcd 			    roundup((strlen(name) - 4), 8));
17783055Sdanmcd 			(void) strlcpy((char *)sid->spd_if_name, name,
17793055Sdanmcd 			    LIFNAMSIZ);
17803055Sdanmcd 		}
17813055Sdanmcd 
17823055Sdanmcd 		offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8);
17833055Sdanmcd 	}
17843055Sdanmcd 
17850Sstevel@tonic-gate 	offset = spdsock_encode_sel(base, offset, rule->ipsp_sel);
17860Sstevel@tonic-gate 	offset = spdsock_encode_action_list(base, offset, rule->ipsp_act);
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	if (base != NULL) {
17910Sstevel@tonic-gate 		spmsg->spd_msg_len = SPD_8TO64(offset - start);
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 	return (offset);
17940Sstevel@tonic-gate }
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate /* ARGSUSED */
17970Sstevel@tonic-gate static mblk_t *
17980Sstevel@tonic-gate spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule,
17993055Sdanmcd     uint_t dir, uint_t af, char *name, boolean_t tunnel)
18000Sstevel@tonic-gate {
18010Sstevel@tonic-gate 	mblk_t *m;
18020Sstevel@tonic-gate 	uint_t len;
18030Sstevel@tonic-gate 	spd_msg_t *mreq = (spd_msg_t *)req->b_rptr;
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	/*
18060Sstevel@tonic-gate 	 * Figure out how much space we'll need.
18070Sstevel@tonic-gate 	 */
18083055Sdanmcd 	len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name,
18093055Sdanmcd 	    tunnel);
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	/*
18120Sstevel@tonic-gate 	 * Allocate mblk.
18130Sstevel@tonic-gate 	 */
18140Sstevel@tonic-gate 	m = allocb(len, BPRI_HI);
18150Sstevel@tonic-gate 	if (m == NULL)
18160Sstevel@tonic-gate 		return (NULL);
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	/*
18190Sstevel@tonic-gate 	 * Fill it in..
18200Sstevel@tonic-gate 	 */
18210Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + len;
18220Sstevel@tonic-gate 	bzero(m->b_rptr, len);
18233055Sdanmcd 	(void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af,
18243055Sdanmcd 	    name, tunnel);
18250Sstevel@tonic-gate 	return (m);
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate static ipsec_policy_t *
182981Ssommerfe spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph,
183081Ssommerfe     ipsec_policy_t *cur)
183181Ssommerfe {
183281Ssommerfe 	ASSERT(RW_READ_HELD(&iph->iph_lock));
183381Ssommerfe 
183481Ssommerfe 	ss->spdsock_dump_count++;
183581Ssommerfe 	ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next;
183681Ssommerfe 	return (cur);
183781Ssommerfe }
183881Ssommerfe 
183981Ssommerfe static ipsec_policy_t *
18400Sstevel@tonic-gate spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph)
18410Sstevel@tonic-gate {
18420Sstevel@tonic-gate 	ipsec_policy_t *cur;
184381Ssommerfe 	ipsec_policy_root_t *ipr;
184481Ssommerfe 	int chain, nchains, type, af;
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&iph->iph_lock));
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	cur = ss->spdsock_dump_cur_rule;
18490Sstevel@tonic-gate 
185081Ssommerfe 	if (cur != NULL)
185181Ssommerfe 		return (spdsock_dump_next_in_chain(ss, iph, cur));
185281Ssommerfe 
185381Ssommerfe 	type = ss->spdsock_dump_cur_type;
185481Ssommerfe 
185581Ssommerfe next:
185681Ssommerfe 	chain = ss->spdsock_dump_cur_chain;
185781Ssommerfe 	ipr = &iph->iph_root[type];
185881Ssommerfe 	nchains = ipr->ipr_nchains;
185981Ssommerfe 
186081Ssommerfe 	while (chain < nchains) {
186181Ssommerfe 		cur = ipr->ipr_hash[chain].hash_head;
186281Ssommerfe 		chain++;
186381Ssommerfe 		if (cur != NULL) {
186481Ssommerfe 			ss->spdsock_dump_cur_chain = chain;
186581Ssommerfe 			return (spdsock_dump_next_in_chain(ss, iph, cur));
186681Ssommerfe 		}
18670Sstevel@tonic-gate 	}
186881Ssommerfe 	ss->spdsock_dump_cur_chain = nchains;
186981Ssommerfe 
187081Ssommerfe 	af = ss->spdsock_dump_cur_af;
187181Ssommerfe 	while (af < IPSEC_NAF) {
187281Ssommerfe 		cur = ipr->ipr_nonhash[af];
187381Ssommerfe 		af++;
187481Ssommerfe 		if (cur != NULL) {
187581Ssommerfe 			ss->spdsock_dump_cur_af = af;
187681Ssommerfe 			return (spdsock_dump_next_in_chain(ss, iph, cur));
187781Ssommerfe 		}
187881Ssommerfe 	}
187981Ssommerfe 
188081Ssommerfe 	type++;
188181Ssommerfe 	if (type >= IPSEC_NTYPES)
188281Ssommerfe 		return (NULL);
188381Ssommerfe 
188481Ssommerfe 	ss->spdsock_dump_cur_chain = 0;
188581Ssommerfe 	ss->spdsock_dump_cur_type = type;
188681Ssommerfe 	ss->spdsock_dump_cur_af = IPSEC_AF_V4;
188781Ssommerfe 	goto next;
188881Ssommerfe 
18890Sstevel@tonic-gate }
18900Sstevel@tonic-gate 
18916430Sdanmcd /*
18926430Sdanmcd  * If we're done with one policy head, but have more to go, we iterate through
18936430Sdanmcd  * another IPsec tunnel policy head (itp).  Return NULL if it is an error
18946430Sdanmcd  * worthy of returning EAGAIN via PF_POLICY.
18956430Sdanmcd  */
18966430Sdanmcd static ipsec_tun_pol_t *
18976430Sdanmcd spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss)
18986430Sdanmcd {
18996430Sdanmcd 	ipsec_tun_pol_t *itp;
19006430Sdanmcd 
19016430Sdanmcd 	ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock));
19026430Sdanmcd 	if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) {
19036430Sdanmcd 		/* Oops, state of the tunnel polheads changed. */
19046430Sdanmcd 		itp = NULL;
19056430Sdanmcd 	} else if (ss->spdsock_itp == NULL) {
19066430Sdanmcd 		/* Just finished global, find first node. */
19076430Sdanmcd 		itp = avl_first(&ipss->ipsec_tunnel_policies);
19086430Sdanmcd 	} else {
19096430Sdanmcd 		/* We just finished current polhead, find the next one. */
19106430Sdanmcd 		itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp);
19116430Sdanmcd 	}
19126430Sdanmcd 	if (itp != NULL) {
19136430Sdanmcd 		ITP_REFHOLD(itp);
19146430Sdanmcd 	}
19156430Sdanmcd 	if (ss->spdsock_itp != NULL) {
19166430Sdanmcd 		ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack);
19176430Sdanmcd 	}
19186430Sdanmcd 	ss->spdsock_itp = itp;
19196430Sdanmcd 	return (itp);
19206430Sdanmcd }
19216430Sdanmcd 
19220Sstevel@tonic-gate static mblk_t *
19230Sstevel@tonic-gate spdsock_dump_next_record(spdsock_t *ss)
19240Sstevel@tonic-gate {
19250Sstevel@tonic-gate 	ipsec_policy_head_t *iph;
19260Sstevel@tonic-gate 	ipsec_policy_t *rule;
19270Sstevel@tonic-gate 	mblk_t *m;
19286430Sdanmcd 	ipsec_tun_pol_t *itp;
19296430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
19306430Sdanmcd 	ipsec_stack_t *ipss = ns->netstack_ipsec;
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	iph = ss->spdsock_dump_head;
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	ASSERT(iph != NULL);
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	if (iph->iph_gen != ss->spdsock_dump_gen) {
19390Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
19400Sstevel@tonic-gate 		return (spdsock_dump_finish(ss, EAGAIN));
19410Sstevel@tonic-gate 	}
19420Sstevel@tonic-gate 
19436430Sdanmcd 	while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) {
19440Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
19456430Sdanmcd 		if (--(ss->spdsock_dump_remaining_polheads) == 0)
19466430Sdanmcd 			return (spdsock_dump_finish(ss, 0));
19476430Sdanmcd 
19486430Sdanmcd 
19496430Sdanmcd 		/*
19506430Sdanmcd 		 * If we reach here, we have more policy heads (tunnel
19516430Sdanmcd 		 * entries) to dump.  Let's reset to a new policy head
19526430Sdanmcd 		 * and get some more rules.
19536430Sdanmcd 		 *
19546430Sdanmcd 		 * An empty policy head will have spdsock_dump_next_rule()
19556430Sdanmcd 		 * return NULL, and we loop (while dropping the number of
19566430Sdanmcd 		 * remaining polheads).  If we loop to 0, we finish.  We
19576430Sdanmcd 		 * keep looping until we hit 0 or until we have a rule to
19586430Sdanmcd 		 * encode.
19596430Sdanmcd 		 *
19606430Sdanmcd 		 * NOTE:  No need for ITP_REF*() macros here as we're only
19616430Sdanmcd 		 * going after and refholding the policy head itself.
19626430Sdanmcd 		 */
19636430Sdanmcd 		rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
19646430Sdanmcd 		itp = spdsock_dump_iterate_next_tunnel(ss, ipss);
19656430Sdanmcd 		if (itp == NULL) {
19666430Sdanmcd 			rw_exit(&ipss->ipsec_tunnel_policy_lock);
19676430Sdanmcd 			return (spdsock_dump_finish(ss, EAGAIN));
19686430Sdanmcd 		}
19696430Sdanmcd 
19706430Sdanmcd 		/* Reset other spdsock_dump thingies. */
19716430Sdanmcd 		IPPH_REFRELE(ss->spdsock_dump_head, ns);
19726430Sdanmcd 		if (ss->spdsock_dump_active) {
19736430Sdanmcd 			ss->spdsock_dump_tunnel =
19746430Sdanmcd 			    itp->itp_flags & ITPF_P_TUNNEL;
19756430Sdanmcd 			iph = itp->itp_policy;
19766430Sdanmcd 		} else {
19776430Sdanmcd 			ss->spdsock_dump_tunnel =
19786430Sdanmcd 			    itp->itp_flags & ITPF_I_TUNNEL;
19796430Sdanmcd 			iph = itp->itp_inactive;
19806430Sdanmcd 		}
19816430Sdanmcd 		IPPH_REFHOLD(iph);
19826430Sdanmcd 		rw_exit(&ipss->ipsec_tunnel_policy_lock);
19836430Sdanmcd 
19846430Sdanmcd 		rw_enter(&iph->iph_lock, RW_READER);
19856430Sdanmcd 		RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
19860Sstevel@tonic-gate 	}
19870Sstevel@tonic-gate 
19886430Sdanmcd 	m = spdsock_encode_rule(ss->spdsock_dump_req, rule,
19896430Sdanmcd 	    ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af,
19906466Sdanmcd 	    (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name,
19916466Sdanmcd 	    ss->spdsock_dump_tunnel);
19920Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	if (m == NULL)
19950Sstevel@tonic-gate 		return (spdsock_dump_finish(ss, ENOMEM));
19960Sstevel@tonic-gate 	return (m);
19970Sstevel@tonic-gate }
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate /*
20000Sstevel@tonic-gate  * Dump records until we run into flow-control back-pressure.
20010Sstevel@tonic-gate  */
20020Sstevel@tonic-gate static void
20030Sstevel@tonic-gate spdsock_dump_some(queue_t *q, spdsock_t *ss)
20040Sstevel@tonic-gate {
20050Sstevel@tonic-gate 	mblk_t *m, *dataind;
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	while ((ss->spdsock_dump_req != NULL) && canputnext(q)) {
20080Sstevel@tonic-gate 		m = spdsock_dump_next_record(ss);
20090Sstevel@tonic-gate 		if (m == NULL)
20100Sstevel@tonic-gate 			return;
20110Sstevel@tonic-gate 		dataind = allocb(sizeof (struct T_data_req), BPRI_HI);
20120Sstevel@tonic-gate 		if (dataind == NULL) {
20130Sstevel@tonic-gate 			freemsg(m);
20140Sstevel@tonic-gate 			return;
20150Sstevel@tonic-gate 		}
20160Sstevel@tonic-gate 		dataind->b_cont = m;
20170Sstevel@tonic-gate 		dataind->b_wptr += sizeof (struct T_data_req);
20180Sstevel@tonic-gate 		((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND;
20190Sstevel@tonic-gate 		((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0;
20200Sstevel@tonic-gate 		dataind->b_datap->db_type = M_PROTO;
20210Sstevel@tonic-gate 		putnext(q, dataind);
20220Sstevel@tonic-gate 	}
20230Sstevel@tonic-gate }
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate /*
20260Sstevel@tonic-gate  * Start dumping.
20270Sstevel@tonic-gate  * Format a start-of-dump record, and set up the stream and kick the rsrv
20280Sstevel@tonic-gate  * procedure to continue the job..
20290Sstevel@tonic-gate  */
20300Sstevel@tonic-gate /* ARGSUSED */
20310Sstevel@tonic-gate static void
20323055Sdanmcd spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp)
20330Sstevel@tonic-gate {
20340Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
20356430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
20363448Sdh155122 	ipsec_stack_t *ipss = ns->netstack_ipsec;
20370Sstevel@tonic-gate 	mblk_t *mr;
20380Sstevel@tonic-gate 
20396430Sdanmcd 	/* spdsock_open() already set spdsock_itp to NULL. */
20403055Sdanmcd 	if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
20413448Sdh155122 		rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
20423055Sdanmcd 		ss->spdsock_dump_remaining_polheads = 1 +
20433448Sdh155122 		    avl_numnodes(&ipss->ipsec_tunnel_policies);
20443448Sdh155122 		ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen;
20453448Sdh155122 		rw_exit(&ipss->ipsec_tunnel_policy_lock);
20463055Sdanmcd 		if (iph == ALL_ACTIVE_POLHEADS) {
20473448Sdh155122 			iph = ipsec_system_policy(ns);
20483055Sdanmcd 			ss->spdsock_dump_active = B_TRUE;
20493055Sdanmcd 		} else {
20506430Sdanmcd 			iph = ipsec_inactive_policy(ns);
20513055Sdanmcd 			ss->spdsock_dump_active = B_FALSE;
20523055Sdanmcd 		}
20536430Sdanmcd 		ASSERT(ss->spdsock_itp == NULL);
20543055Sdanmcd 	} else {
20553055Sdanmcd 		ss->spdsock_dump_remaining_polheads = 1;
20563055Sdanmcd 	}
20573055Sdanmcd 
20580Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 	mr = spdsock_dump_ruleset(mp, iph, 0, 0);
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 	if (!mr) {
20630Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
20640Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
20650Sstevel@tonic-gate 		return;
20660Sstevel@tonic-gate 	}
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	ss->spdsock_dump_req = mp;
20696430Sdanmcd 	RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
20706430Sdanmcd 
20710Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	qreply(q, mr);
20740Sstevel@tonic-gate 	qenable(OTHERQ(q));
20750Sstevel@tonic-gate }
20760Sstevel@tonic-gate 
20773055Sdanmcd /* Do NOT consume a reference to ITP. */
20780Sstevel@tonic-gate void
20793448Sdh155122 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns)
20803055Sdanmcd {
20813055Sdanmcd 	int *errptr = (int *)ep;
20823055Sdanmcd 
20833055Sdanmcd 	if (*errptr != 0)
20843055Sdanmcd 		return;	/* We've failed already for some reason. */
20853055Sdanmcd 	mutex_enter(&itp->itp_lock);
20863055Sdanmcd 	ITPF_CLONE(itp->itp_flags);
20873448Sdh155122 	*errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns);
20883055Sdanmcd 	mutex_exit(&itp->itp_lock);
20893055Sdanmcd }
20903055Sdanmcd 
20913055Sdanmcd void
20923055Sdanmcd spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname)
20930Sstevel@tonic-gate {
20943055Sdanmcd 	int error;
20953055Sdanmcd 	char *tname;
20963055Sdanmcd 	ipsec_tun_pol_t *itp;
20973448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
20986430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
20993055Sdanmcd 
21003055Sdanmcd 	if (tunname != NULL) {
21013055Sdanmcd 		tname = (char *)tunname->spd_if_name;
21023055Sdanmcd 		if (*tname == '\0') {
21036430Sdanmcd 			error = ipsec_clone_system_policy(ns);
21044307Spwernau 			if (audit_active) {
21054307Spwernau 				boolean_t active;
21064307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2107*8778SErik.Nordmark@Sun.COM 				cred_t *cr;
2108*8778SErik.Nordmark@Sun.COM 				pid_t cpid;
2109*8778SErik.Nordmark@Sun.COM 
2110*8778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
21114307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2112*8778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_CLONE, cr, ns,
2113*8778SErik.Nordmark@Sun.COM 				    NULL, active, error, cpid);
21144307Spwernau 			}
21154307Spwernau 			if (error == 0) {
21166430Sdanmcd 				itp_walk(spdsock_clone_node, &error, ns);
21174307Spwernau 				if (audit_active) {
21184307Spwernau 					boolean_t active;
21194307Spwernau 					spd_msg_t *spmsg =
21204307Spwernau 					    (spd_msg_t *)mp->b_rptr;
2121*8778SErik.Nordmark@Sun.COM 					cred_t *cr;
2122*8778SErik.Nordmark@Sun.COM 					pid_t cpid;
2123*8778SErik.Nordmark@Sun.COM 
2124*8778SErik.Nordmark@Sun.COM 					cr = msg_getcred(mp, &cpid);
21254307Spwernau 					active = (spmsg->spd_msg_spdid ==
21264307Spwernau 					    SPD_ACTIVE);
2127*8778SErik.Nordmark@Sun.COM 					audit_pf_policy(SPD_CLONE, cr,
21286430Sdanmcd 					    ns, "all tunnels", active, 0,
2129*8778SErik.Nordmark@Sun.COM 					    cpid);
21304307Spwernau 				}
21314307Spwernau 			}
21323055Sdanmcd 		} else {
21336430Sdanmcd 			itp = get_tunnel_policy(tname, ns);
21343055Sdanmcd 			if (itp == NULL) {
21353055Sdanmcd 				spdsock_error(q, mp, ENOENT, 0);
21364307Spwernau 				if (audit_active) {
21374307Spwernau 					boolean_t active;
21384307Spwernau 					spd_msg_t *spmsg =
21394307Spwernau 					    (spd_msg_t *)mp->b_rptr;
2140*8778SErik.Nordmark@Sun.COM 					cred_t *cr;
2141*8778SErik.Nordmark@Sun.COM 					pid_t cpid;
2142*8778SErik.Nordmark@Sun.COM 
2143*8778SErik.Nordmark@Sun.COM 					cr = msg_getcred(mp, &cpid);
21444307Spwernau 					active = (spmsg->spd_msg_spdid ==
21454307Spwernau 					    SPD_ACTIVE);
2146*8778SErik.Nordmark@Sun.COM 					audit_pf_policy(SPD_CLONE, cr,
21476430Sdanmcd 					    ns, ITP_NAME(itp), active, ENOENT,
2148*8778SErik.Nordmark@Sun.COM 					    cpid);
21494307Spwernau 				}
21503055Sdanmcd 				return;
21513055Sdanmcd 			}
21523448Sdh155122 			spdsock_clone_node(itp, &error, NULL);
21536430Sdanmcd 			ITP_REFRELE(itp, ns);
21544307Spwernau 			if (audit_active) {
21554307Spwernau 				boolean_t active;
21564307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2157*8778SErik.Nordmark@Sun.COM 				cred_t *cr;
2158*8778SErik.Nordmark@Sun.COM 				pid_t cpid;
2159*8778SErik.Nordmark@Sun.COM 
2160*8778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
21614307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2162*8778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_CLONE, cr, ns,
2163*8778SErik.Nordmark@Sun.COM 				    ITP_NAME(itp), active, error, cpid);
21644307Spwernau 			}
21653055Sdanmcd 		}
21663055Sdanmcd 	} else {
21676430Sdanmcd 		error = ipsec_clone_system_policy(ns);
21684307Spwernau 		if (audit_active) {
21694307Spwernau 			boolean_t active;
21704307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2171*8778SErik.Nordmark@Sun.COM 			cred_t *cr;
2172*8778SErik.Nordmark@Sun.COM 			pid_t cpid;
2173*8778SErik.Nordmark@Sun.COM 
2174*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
21754307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2176*8778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_CLONE, cr, ns, NULL,
2177*8778SErik.Nordmark@Sun.COM 			    active, error, cpid);
21784307Spwernau 		}
21793055Sdanmcd 	}
21803055Sdanmcd 
21810Sstevel@tonic-gate 	if (error != 0)
21820Sstevel@tonic-gate 		spdsock_error(q, mp, error, 0);
21830Sstevel@tonic-gate 	else
21840Sstevel@tonic-gate 		spd_echo(q, mp);
21850Sstevel@tonic-gate }
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate /*
21880Sstevel@tonic-gate  * Process a SPD_ALGLIST request. The caller expects separate alg entries
21890Sstevel@tonic-gate  * for AH authentication, ESP authentication, and ESP encryption.
21900Sstevel@tonic-gate  * The same distinction is then used when setting the min and max key
21910Sstevel@tonic-gate  * sizes when defining policies.
21920Sstevel@tonic-gate  */
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate #define	SPDSOCK_AH_AUTH		0
21950Sstevel@tonic-gate #define	SPDSOCK_ESP_AUTH	1
21960Sstevel@tonic-gate #define	SPDSOCK_ESP_ENCR	2
21970Sstevel@tonic-gate #define	SPDSOCK_NTYPES		3
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate static const uint_t algattr[SPDSOCK_NTYPES] = {
22000Sstevel@tonic-gate 	SPD_ATTR_AH_AUTH,
22010Sstevel@tonic-gate 	SPD_ATTR_ESP_AUTH,
22020Sstevel@tonic-gate 	SPD_ATTR_ESP_ENCR
22030Sstevel@tonic-gate };
22040Sstevel@tonic-gate static const uint_t minbitsattr[SPDSOCK_NTYPES] = {
22050Sstevel@tonic-gate 	SPD_ATTR_AH_MINBITS,
22060Sstevel@tonic-gate 	SPD_ATTR_ESPA_MINBITS,
22070Sstevel@tonic-gate 	SPD_ATTR_ENCR_MINBITS
22080Sstevel@tonic-gate };
22090Sstevel@tonic-gate static const uint_t maxbitsattr[SPDSOCK_NTYPES] = {
22100Sstevel@tonic-gate 	SPD_ATTR_AH_MAXBITS,
22110Sstevel@tonic-gate 	SPD_ATTR_ESPA_MAXBITS,
22120Sstevel@tonic-gate 	SPD_ATTR_ENCR_MAXBITS
22130Sstevel@tonic-gate };
22140Sstevel@tonic-gate static const uint_t defbitsattr[SPDSOCK_NTYPES] = {
22150Sstevel@tonic-gate 	SPD_ATTR_AH_DEFBITS,
22160Sstevel@tonic-gate 	SPD_ATTR_ESPA_DEFBITS,
22170Sstevel@tonic-gate 	SPD_ATTR_ENCR_DEFBITS
22180Sstevel@tonic-gate };
22190Sstevel@tonic-gate static const uint_t incrbitsattr[SPDSOCK_NTYPES] = {
22200Sstevel@tonic-gate 	SPD_ATTR_AH_INCRBITS,
22210Sstevel@tonic-gate 	SPD_ATTR_ESPA_INCRBITS,
22220Sstevel@tonic-gate 	SPD_ATTR_ENCR_INCRBITS
22230Sstevel@tonic-gate };
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate #define	ATTRPERALG	6	/* fixed attributes per algs */
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate void
22280Sstevel@tonic-gate spdsock_alglist(queue_t *q, mblk_t *mp)
22290Sstevel@tonic-gate {
22300Sstevel@tonic-gate 	uint_t algtype;
22310Sstevel@tonic-gate 	uint_t algidx;
22320Sstevel@tonic-gate 	uint_t algcount;
22330Sstevel@tonic-gate 	uint_t size;
22340Sstevel@tonic-gate 	mblk_t *m;
22350Sstevel@tonic-gate 	uint8_t *cur;
22360Sstevel@tonic-gate 	spd_msg_t *msg;
22370Sstevel@tonic-gate 	struct spd_ext_actions *act;
22380Sstevel@tonic-gate 	struct spd_attribute *attr;
22393448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
22406430Sdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
22413448Sdh155122 
22423448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
22430Sstevel@tonic-gate 	/*
22440Sstevel@tonic-gate 	 * The SPD client expects to receive separate entries for
22450Sstevel@tonic-gate 	 * AH authentication and ESP authentication supported algorithms.
22460Sstevel@tonic-gate 	 *
22470Sstevel@tonic-gate 	 * Don't return the "any" algorithms, if defined, as no
22480Sstevel@tonic-gate 	 * kernel policies can be set for these algorithms.
22490Sstevel@tonic-gate 	 */
22503448Sdh155122 	algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
22513448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
22523448Sdh155122 
22533448Sdh155122 	if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL)
22540Sstevel@tonic-gate 		algcount--;
22553448Sdh155122 	if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL)
22560Sstevel@tonic-gate 		algcount--;
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 	/*
22590Sstevel@tonic-gate 	 * For each algorithm, we encode:
22600Sstevel@tonic-gate 	 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
22610Sstevel@tonic-gate 	 */
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) +
22640Sstevel@tonic-gate 	    ATTRPERALG * sizeof (struct spd_attribute) * algcount;
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	ASSERT(ALIGNED64(size));
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	m = allocb(size, BPRI_HI);
22690Sstevel@tonic-gate 	if (m == NULL) {
22703448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
22710Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
22720Sstevel@tonic-gate 		return;
22730Sstevel@tonic-gate 	}
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + size;
22760Sstevel@tonic-gate 	cur = m->b_rptr;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	msg = (spd_msg_t *)cur;
22790Sstevel@tonic-gate 	bcopy(mp->b_rptr, cur, sizeof (*msg));
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(size);
22820Sstevel@tonic-gate 	msg->spd_msg_errno = 0;
22830Sstevel@tonic-gate 	msg->spd_msg_diagnostic = 0;
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	cur += sizeof (*msg);
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	act = (struct spd_ext_actions *)cur;
22880Sstevel@tonic-gate 	cur += sizeof (*act);
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 	act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
22910Sstevel@tonic-gate 	act->spd_actions_exttype = SPD_EXT_ACTION;
22920Sstevel@tonic-gate 	act->spd_actions_count = algcount;
22930Sstevel@tonic-gate 	act->spd_actions_reserved = 0;
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 	attr = (struct spd_attribute *)cur;
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate #define	EMIT(tag, value) {					\
22980Sstevel@tonic-gate 		attr->spd_attr_tag = (tag); 			\
22990Sstevel@tonic-gate 		attr->spd_attr_value = (value); 		\
23000Sstevel@tonic-gate 		attr++;			  			\
23010Sstevel@tonic-gate 	}
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 	/*
23040Sstevel@tonic-gate 	 * If you change the number of EMIT's here, change
23050Sstevel@tonic-gate 	 * ATTRPERALG above to match
23060Sstevel@tonic-gate 	 */
23070Sstevel@tonic-gate #define	EMITALGATTRS(_type) {					\
23080Sstevel@tonic-gate 		EMIT(algattr[_type], algid); 		/* 1 */	\
23090Sstevel@tonic-gate 		EMIT(minbitsattr[_type], minbits);	/* 2 */	\
23100Sstevel@tonic-gate 		EMIT(maxbitsattr[_type], maxbits);	/* 3 */	\
23110Sstevel@tonic-gate 		EMIT(defbitsattr[_type], defbits);	/* 4 */	\
23120Sstevel@tonic-gate 		EMIT(incrbitsattr[_type], incr);	/* 5 */	\
23130Sstevel@tonic-gate 		EMIT(SPD_ATTR_NEXT, 0);			/* 6 */	\
23140Sstevel@tonic-gate 	}
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
23173448Sdh155122 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
23183448Sdh155122 		    algidx++) {
23193448Sdh155122 			int algid = ipss->ipsec_sortlist[algtype][algidx];
23203448Sdh155122 			ipsec_alginfo_t *alg =
23213448Sdh155122 			    ipss->ipsec_alglists[algtype][algid];
23220Sstevel@tonic-gate 			uint_t minbits = alg->alg_minbits;
23230Sstevel@tonic-gate 			uint_t maxbits = alg->alg_maxbits;
23240Sstevel@tonic-gate 			uint_t defbits = alg->alg_default_bits;
23250Sstevel@tonic-gate 			uint_t incr = alg->alg_increment;
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 			if (algtype == IPSEC_ALG_AUTH) {
23280Sstevel@tonic-gate 				if (algid == SADB_AALG_NONE)
23290Sstevel@tonic-gate 					continue;
23300Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_AH_AUTH);
23310Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_ESP_AUTH);
23320Sstevel@tonic-gate 			} else {
23330Sstevel@tonic-gate 				if (algid == SADB_EALG_NONE)
23340Sstevel@tonic-gate 					continue;
23350Sstevel@tonic-gate 				ASSERT(algtype == IPSEC_ALG_ENCR);
23360Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_ESP_ENCR);
23370Sstevel@tonic-gate 			}
23380Sstevel@tonic-gate 		}
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 
23413448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate #undef EMITALGATTRS
23440Sstevel@tonic-gate #undef EMIT
23450Sstevel@tonic-gate #undef ATTRPERALG
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	attr--;
23480Sstevel@tonic-gate 	attr->spd_attr_tag = SPD_ATTR_END;
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	freemsg(mp);
23510Sstevel@tonic-gate 	qreply(q, m);
23520Sstevel@tonic-gate }
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate /*
23550Sstevel@tonic-gate  * Process a SPD_DUMPALGS request.
23560Sstevel@tonic-gate  */
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate #define	ATTRPERALG	7	/* fixed attributes per algs */
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate void
23610Sstevel@tonic-gate spdsock_dumpalgs(queue_t *q, mblk_t *mp)
23620Sstevel@tonic-gate {
23630Sstevel@tonic-gate 	uint_t algtype;
23640Sstevel@tonic-gate 	uint_t algidx;
23650Sstevel@tonic-gate 	uint_t size;
23660Sstevel@tonic-gate 	mblk_t *m;
23670Sstevel@tonic-gate 	uint8_t *cur;
23680Sstevel@tonic-gate 	spd_msg_t *msg;
23690Sstevel@tonic-gate 	struct spd_ext_actions *act;
23700Sstevel@tonic-gate 	struct spd_attribute *attr;
23710Sstevel@tonic-gate 	ipsec_alginfo_t *alg;
23720Sstevel@tonic-gate 	uint_t algid;
23730Sstevel@tonic-gate 	uint_t i;
23740Sstevel@tonic-gate 	uint_t alg_size;
23753448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
23766430Sdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
23773448Sdh155122 
23783448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	/*
23810Sstevel@tonic-gate 	 * For each algorithm, we encode:
23820Sstevel@tonic-gate 	 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
23830Sstevel@tonic-gate 	 *
23840Sstevel@tonic-gate 	 * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE*
23850Sstevel@tonic-gate 	 * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_MECHNAME / {END, NEXT}
23860Sstevel@tonic-gate 	 */
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	/*
23890Sstevel@tonic-gate 	 * Compute the size of the SPD message.
23900Sstevel@tonic-gate 	 */
23910Sstevel@tonic-gate 	size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions);
23920Sstevel@tonic-gate 
23930Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
23943448Sdh155122 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
23953448Sdh155122 		    algidx++) {
23963448Sdh155122 			algid = ipss->ipsec_sortlist[algtype][algidx];
23973448Sdh155122 			alg = ipss->ipsec_alglists[algtype][algid];
23980Sstevel@tonic-gate 			alg_size = sizeof (struct spd_attribute) *
23990Sstevel@tonic-gate 			    (ATTRPERALG + alg->alg_nkey_sizes +
24000Sstevel@tonic-gate 			    alg->alg_nblock_sizes) + CRYPTO_MAX_MECH_NAME;
24010Sstevel@tonic-gate 			size += alg_size;
24020Sstevel@tonic-gate 		}
24030Sstevel@tonic-gate 	}
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	ASSERT(ALIGNED64(size));
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	m = allocb(size, BPRI_HI);
24080Sstevel@tonic-gate 	if (m == NULL) {
24093448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
24100Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
24110Sstevel@tonic-gate 		return;
24120Sstevel@tonic-gate 	}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + size;
24150Sstevel@tonic-gate 	cur = m->b_rptr;
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 	msg = (spd_msg_t *)cur;
24180Sstevel@tonic-gate 	bcopy(mp->b_rptr, cur, sizeof (*msg));
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(size);
24210Sstevel@tonic-gate 	msg->spd_msg_errno = 0;
24220Sstevel@tonic-gate 	msg->spd_msg_diagnostic = 0;
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	cur += sizeof (*msg);
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 	act = (struct spd_ext_actions *)cur;
24270Sstevel@tonic-gate 	cur += sizeof (*act);
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 	act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
24300Sstevel@tonic-gate 	act->spd_actions_exttype = SPD_EXT_ACTION;
24313448Sdh155122 	act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
24323448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
24330Sstevel@tonic-gate 	act->spd_actions_reserved = 0;
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 	attr = (struct spd_attribute *)cur;
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate #define	EMIT(tag, value) {					\
24380Sstevel@tonic-gate 		attr->spd_attr_tag = (tag); 			\
24390Sstevel@tonic-gate 		attr->spd_attr_value = (value); 		\
24400Sstevel@tonic-gate 		attr++;			  			\
24410Sstevel@tonic-gate 	}
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
24443448Sdh155122 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
24453448Sdh155122 		    algidx++) {
24463448Sdh155122 
24473448Sdh155122 			algid = ipss->ipsec_sortlist[algtype][algidx];
24483448Sdh155122 			alg = ipss->ipsec_alglists[algtype][algid];
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 			/*
24510Sstevel@tonic-gate 			 * If you change the number of EMIT's here, change
24520Sstevel@tonic-gate 			 * ATTRPERALG above to match
24530Sstevel@tonic-gate 			 */
24540Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_ID, algid);
24550Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]);
24560Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment);
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes);
24590Sstevel@tonic-gate 			for (i = 0; i < alg->alg_nkey_sizes; i++)
24600Sstevel@tonic-gate 				EMIT(SPD_ATTR_ALG_KEYSIZE,
24610Sstevel@tonic-gate 				    alg->alg_key_sizes[i]);
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes);
24640Sstevel@tonic-gate 			for (i = 0; i < alg->alg_nblock_sizes; i++)
24650Sstevel@tonic-gate 				EMIT(SPD_ATTR_ALG_BLOCKSIZE,
24660Sstevel@tonic-gate 				    alg->alg_block_sizes[i]);
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME);
24690Sstevel@tonic-gate 			bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME);
24700Sstevel@tonic-gate 			attr = (struct spd_attribute *)((char *)attr +
24710Sstevel@tonic-gate 			    CRYPTO_MAX_MECH_NAME);
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate 			EMIT(SPD_ATTR_NEXT, 0);
24740Sstevel@tonic-gate 		}
24750Sstevel@tonic-gate 	}
24760Sstevel@tonic-gate 
24773448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate #undef EMITALGATTRS
24800Sstevel@tonic-gate #undef EMIT
24810Sstevel@tonic-gate #undef ATTRPERALG
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	attr--;
24840Sstevel@tonic-gate 	attr->spd_attr_tag = SPD_ATTR_END;
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	freemsg(mp);
24870Sstevel@tonic-gate 	qreply(q, m);
24880Sstevel@tonic-gate }
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate /*
24910Sstevel@tonic-gate  * Do the actual work of processing an SPD_UPDATEALGS request. Can
24920Sstevel@tonic-gate  * be invoked either once IPsec is loaded on a cached request, or
24930Sstevel@tonic-gate  * when a request is received while IPsec is loaded.
24940Sstevel@tonic-gate  */
24950Sstevel@tonic-gate static void
24963448Sdh155122 spdsock_do_updatealg(spd_ext_t *extv[], int *diag, spd_stack_t *spds)
24970Sstevel@tonic-gate {
24980Sstevel@tonic-gate 	struct spd_ext_actions *actp;
24990Sstevel@tonic-gate 	struct spd_attribute *attr, *endattr;
25000Sstevel@tonic-gate 	uint64_t *start, *end;
25010Sstevel@tonic-gate 	ipsec_alginfo_t *alg = NULL;
25020Sstevel@tonic-gate 	ipsec_algtype_t alg_type = 0;
25030Sstevel@tonic-gate 	boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE;
25040Sstevel@tonic-gate 	uint_t i, cur_key, cur_block, algid;
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	*diag = -1;
25073448Sdh155122 	ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	/* parse the message, building the list of algorithms */
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 	actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
25120Sstevel@tonic-gate 	if (actp == NULL) {
25130Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_NO_ACTION_EXT;
25140Sstevel@tonic-gate 		return;
25150Sstevel@tonic-gate 	}
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	start = (uint64_t *)actp;
25180Sstevel@tonic-gate 	end = (start + actp->spd_actions_len);
25190Sstevel@tonic-gate 	endattr = (struct spd_attribute *)end;
25200Sstevel@tonic-gate 	attr = (struct spd_attribute *)&actp[1];
25210Sstevel@tonic-gate 
25223448Sdh155122 	bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS *
25230Sstevel@tonic-gate 	    sizeof (ipsec_alginfo_t *));
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate #define	ALG_KEY_SIZES(a)   (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t))
25280Sstevel@tonic-gate #define	ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t))
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	while (attr < endattr) {
25310Sstevel@tonic-gate 		switch (attr->spd_attr_tag) {
25320Sstevel@tonic-gate 		case SPD_ATTR_NOP:
25330Sstevel@tonic-gate 		case SPD_ATTR_EMPTY:
25340Sstevel@tonic-gate 			break;
25350Sstevel@tonic-gate 		case SPD_ATTR_END:
25360Sstevel@tonic-gate 			attr = endattr;
25370Sstevel@tonic-gate 			/* FALLTHRU */
25380Sstevel@tonic-gate 		case SPD_ATTR_NEXT:
25390Sstevel@tonic-gate 			if (doing_proto) {
25400Sstevel@tonic-gate 				doing_proto = B_FALSE;
25410Sstevel@tonic-gate 				break;
25420Sstevel@tonic-gate 			}
25430Sstevel@tonic-gate 			if (skip_alg) {
25440Sstevel@tonic-gate 				ipsec_alg_free(alg);
25450Sstevel@tonic-gate 			} else {
25460Sstevel@tonic-gate 				ipsec_alg_free(
25473448Sdh155122 				    spds->spds_algs[alg_type][alg->alg_id]);
25483448Sdh155122 				spds->spds_algs[alg_type][alg->alg_id] =
25493448Sdh155122 				    alg;
25500Sstevel@tonic-gate 			}
25510Sstevel@tonic-gate 			alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
25520Sstevel@tonic-gate 			break;
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 		case SPD_ATTR_ALG_ID:
25550Sstevel@tonic-gate 			if (attr->spd_attr_value >= IPSEC_MAX_ALGS) {
25563448Sdh155122 				ss1dbg(spds, ("spdsock_do_updatealg: "
25570Sstevel@tonic-gate 				    "invalid alg id %d\n",
25580Sstevel@tonic-gate 				    attr->spd_attr_value));
25590Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ALG_ID_RANGE;
25600Sstevel@tonic-gate 				goto bail;
25610Sstevel@tonic-gate 			}
25620Sstevel@tonic-gate 			alg->alg_id = attr->spd_attr_value;
25630Sstevel@tonic-gate 			break;
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 		case SPD_ATTR_ALG_PROTO:
25660Sstevel@tonic-gate 			/* find the alg type */
25670Sstevel@tonic-gate 			for (i = 0; i < NALGPROTOS; i++)
25680Sstevel@tonic-gate 				if (algproto[i] == attr->spd_attr_value)
25690Sstevel@tonic-gate 					break;
25700Sstevel@tonic-gate 			skip_alg = (i == NALGPROTOS);
25710Sstevel@tonic-gate 			if (!skip_alg)
25720Sstevel@tonic-gate 				alg_type = i;
25730Sstevel@tonic-gate 			break;
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 		case SPD_ATTR_ALG_INCRBITS:
25760Sstevel@tonic-gate 			alg->alg_increment = attr->spd_attr_value;
25770Sstevel@tonic-gate 			break;
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 		case SPD_ATTR_ALG_NKEYSIZES:
25800Sstevel@tonic-gate 			if (alg->alg_key_sizes != NULL) {
25810Sstevel@tonic-gate 				kmem_free(alg->alg_key_sizes,
25820Sstevel@tonic-gate 				    ALG_KEY_SIZES(alg));
25830Sstevel@tonic-gate 			}
25840Sstevel@tonic-gate 			alg->alg_nkey_sizes = attr->spd_attr_value;
25850Sstevel@tonic-gate 			/*
25860Sstevel@tonic-gate 			 * Allocate room for the trailing zero key size
25870Sstevel@tonic-gate 			 * value as well.
25880Sstevel@tonic-gate 			 */
25890Sstevel@tonic-gate 			alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg),
25900Sstevel@tonic-gate 			    KM_SLEEP);
25910Sstevel@tonic-gate 			cur_key = 0;
25920Sstevel@tonic-gate 			break;
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 		case SPD_ATTR_ALG_KEYSIZE:
25950Sstevel@tonic-gate 			if (alg->alg_key_sizes == NULL ||
25960Sstevel@tonic-gate 			    cur_key >= alg->alg_nkey_sizes) {
25973448Sdh155122 				ss1dbg(spds, ("spdsock_do_updatealg: "
25985240Snordmark 				    "too many key sizes\n"));
25990Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES;
26000Sstevel@tonic-gate 				goto bail;
26010Sstevel@tonic-gate 			}
26020Sstevel@tonic-gate 			alg->alg_key_sizes[cur_key++] = attr->spd_attr_value;
26030Sstevel@tonic-gate 			break;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 		case SPD_ATTR_ALG_NBLOCKSIZES:
26060Sstevel@tonic-gate 			if (alg->alg_block_sizes != NULL) {
26070Sstevel@tonic-gate 				kmem_free(alg->alg_block_sizes,
26080Sstevel@tonic-gate 				    ALG_BLOCK_SIZES(alg));
26090Sstevel@tonic-gate 			}
26100Sstevel@tonic-gate 			alg->alg_nblock_sizes = attr->spd_attr_value;
26110Sstevel@tonic-gate 			/*
26120Sstevel@tonic-gate 			 * Allocate room for the trailing zero block size
26130Sstevel@tonic-gate 			 * value as well.
26140Sstevel@tonic-gate 			 */
26150Sstevel@tonic-gate 			alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg),
26160Sstevel@tonic-gate 			    KM_SLEEP);
26170Sstevel@tonic-gate 			cur_block = 0;
26180Sstevel@tonic-gate 			break;
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 		case SPD_ATTR_ALG_BLOCKSIZE:
26210Sstevel@tonic-gate 			if (alg->alg_block_sizes == NULL ||
26220Sstevel@tonic-gate 			    cur_block >= alg->alg_nblock_sizes) {
26233448Sdh155122 				ss1dbg(spds, ("spdsock_do_updatealg: "
26245240Snordmark 				    "too many block sizes\n"));
26250Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
26260Sstevel@tonic-gate 				goto bail;
26270Sstevel@tonic-gate 			}
26280Sstevel@tonic-gate 			alg->alg_block_sizes[cur_block++] =
26290Sstevel@tonic-gate 			    attr->spd_attr_value;
26300Sstevel@tonic-gate 			break;
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 		case SPD_ATTR_ALG_MECHNAME: {
26330Sstevel@tonic-gate 			char *mech_name;
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 			if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) {
26363448Sdh155122 				ss1dbg(spds, ("spdsock_do_updatealg: "
26375240Snordmark 				    "mech name too long\n"));
26380Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN;
26390Sstevel@tonic-gate 				goto bail;
26400Sstevel@tonic-gate 			}
26410Sstevel@tonic-gate 			mech_name = (char *)(attr + 1);
26420Sstevel@tonic-gate 			bcopy(mech_name, alg->alg_mech_name,
26430Sstevel@tonic-gate 			    attr->spd_attr_value);
26440Sstevel@tonic-gate 			alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0';
26450Sstevel@tonic-gate 			attr = (struct spd_attribute *)((char *)attr +
26460Sstevel@tonic-gate 			    attr->spd_attr_value);
26470Sstevel@tonic-gate 			break;
26480Sstevel@tonic-gate 		}
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 		case SPD_ATTR_PROTO_ID:
26510Sstevel@tonic-gate 			doing_proto = B_TRUE;
26520Sstevel@tonic-gate 			for (i = 0; i < NALGPROTOS; i++) {
26530Sstevel@tonic-gate 				if (algproto[i] == attr->spd_attr_value) {
26540Sstevel@tonic-gate 					alg_type = i;
26550Sstevel@tonic-gate 					break;
26560Sstevel@tonic-gate 				}
26570Sstevel@tonic-gate 			}
26580Sstevel@tonic-gate 			break;
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 		case SPD_ATTR_PROTO_EXEC_MODE:
26610Sstevel@tonic-gate 			if (!doing_proto)
26620Sstevel@tonic-gate 				break;
26630Sstevel@tonic-gate 			for (i = 0; i < NEXECMODES; i++) {
26640Sstevel@tonic-gate 				if (execmodes[i] == attr->spd_attr_value) {
26653448Sdh155122 					spds->spds_algs_exec_mode[alg_type] = i;
26660Sstevel@tonic-gate 					break;
26670Sstevel@tonic-gate 				}
26680Sstevel@tonic-gate 			}
26690Sstevel@tonic-gate 			break;
26700Sstevel@tonic-gate 		}
26710Sstevel@tonic-gate 		attr++;
26720Sstevel@tonic-gate 	}
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate #undef	ALG_KEY_SIZES
26750Sstevel@tonic-gate #undef	ALG_BLOCK_SIZES
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate 	/* update the algorithm tables */
26783448Sdh155122 	spdsock_merge_algs(spds);
26790Sstevel@tonic-gate bail:
26800Sstevel@tonic-gate 	/* cleanup */
26810Sstevel@tonic-gate 	ipsec_alg_free(alg);
26820Sstevel@tonic-gate 	for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++)
26835240Snordmark 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++)
26843448Sdh155122 		if (spds->spds_algs[alg_type][algid] != NULL)
26855240Snordmark 			ipsec_alg_free(spds->spds_algs[alg_type][algid]);
26860Sstevel@tonic-gate }
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate /*
26890Sstevel@tonic-gate  * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue
26900Sstevel@tonic-gate  * the request until IPsec loads. If IPsec is loaded, act on it
26910Sstevel@tonic-gate  * immediately.
26920Sstevel@tonic-gate  */
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate static void
26950Sstevel@tonic-gate spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[])
26960Sstevel@tonic-gate {
26973448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
26983448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
26993448Sdh155122 	ipsec_stack_t	*ipss = spds->spds_netstack->netstack_ipsec;
27003448Sdh155122 
27013448Sdh155122 	if (!ipsec_loaded(ipss)) {
27020Sstevel@tonic-gate 		/*
27030Sstevel@tonic-gate 		 * IPsec is not loaded, save request and return nicely,
27040Sstevel@tonic-gate 		 * the message will be processed once IPsec loads.
27050Sstevel@tonic-gate 		 */
27060Sstevel@tonic-gate 		mblk_t *new_mp;
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 		/* last update message wins */
27090Sstevel@tonic-gate 		if ((new_mp = copymsg(mp)) == NULL) {
27100Sstevel@tonic-gate 			spdsock_error(q, mp, ENOMEM, 0);
27110Sstevel@tonic-gate 			return;
27120Sstevel@tonic-gate 		}
27133448Sdh155122 		mutex_enter(&spds->spds_alg_lock);
27143448Sdh155122 		bcopy(extv, spds->spds_extv_algs,
27150Sstevel@tonic-gate 		    sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
27163448Sdh155122 		if (spds->spds_mp_algs != NULL)
27173448Sdh155122 			freemsg(spds->spds_mp_algs);
27183448Sdh155122 		spds->spds_mp_algs = mp;
27193448Sdh155122 		spds->spds_algs_pending = B_TRUE;
27203448Sdh155122 		mutex_exit(&spds->spds_alg_lock);
2721*8778SErik.Nordmark@Sun.COM 		if (audit_active) {
2722*8778SErik.Nordmark@Sun.COM 			cred_t *cr;
2723*8778SErik.Nordmark@Sun.COM 			pid_t cpid;
2724*8778SErik.Nordmark@Sun.COM 
2725*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
2726*8778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_UPDATEALGS, cr,
27274307Spwernau 			    spds->spds_netstack, NULL, B_TRUE, EAGAIN,
2728*8778SErik.Nordmark@Sun.COM 			    cpid);
2729*8778SErik.Nordmark@Sun.COM 		}
27300Sstevel@tonic-gate 		spd_echo(q, new_mp);
27310Sstevel@tonic-gate 	} else {
27320Sstevel@tonic-gate 		/*
27330Sstevel@tonic-gate 		 * IPsec is loaded, act on the message immediately.
27340Sstevel@tonic-gate 		 */
27350Sstevel@tonic-gate 		int diag;
27360Sstevel@tonic-gate 
27373448Sdh155122 		mutex_enter(&spds->spds_alg_lock);
27383448Sdh155122 		spdsock_do_updatealg(extv, &diag, spds);
27393448Sdh155122 		mutex_exit(&spds->spds_alg_lock);
27404307Spwernau 		if (diag == -1) {
27410Sstevel@tonic-gate 			spd_echo(q, mp);
2742*8778SErik.Nordmark@Sun.COM 			if (audit_active) {
2743*8778SErik.Nordmark@Sun.COM 				cred_t *cr;
2744*8778SErik.Nordmark@Sun.COM 				pid_t cpid;
2745*8778SErik.Nordmark@Sun.COM 
2746*8778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
2747*8778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_UPDATEALGS, cr,
2748*8778SErik.Nordmark@Sun.COM 				    spds->spds_netstack, NULL, B_TRUE, 0,
2749*8778SErik.Nordmark@Sun.COM 				    cpid);
2750*8778SErik.Nordmark@Sun.COM 			}
27514307Spwernau 		} else {
27520Sstevel@tonic-gate 			spdsock_diag(q, mp, diag);
2753*8778SErik.Nordmark@Sun.COM 			if (audit_active) {
2754*8778SErik.Nordmark@Sun.COM 				cred_t *cr;
2755*8778SErik.Nordmark@Sun.COM 				pid_t cpid;
2756*8778SErik.Nordmark@Sun.COM 
2757*8778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
2758*8778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_UPDATEALGS, cr,
2759*8778SErik.Nordmark@Sun.COM 				    spds->spds_netstack, NULL, B_TRUE, diag,
2760*8778SErik.Nordmark@Sun.COM 				    cpid);
2761*8778SErik.Nordmark@Sun.COM 			}
27624307Spwernau 		}
27630Sstevel@tonic-gate 	}
27640Sstevel@tonic-gate }
27650Sstevel@tonic-gate 
27663055Sdanmcd /*
27673055Sdanmcd  * With a reference-held ill, dig down and find an instance of "tun", and
27683055Sdanmcd  * assign its tunnel policy pointer, while reference-holding it.  Also,
27693055Sdanmcd  * release ill's refrence when finished.
27703055Sdanmcd  *
27713055Sdanmcd  * We'll be messing with q_next, so be VERY careful.
27723055Sdanmcd  */
27733055Sdanmcd static void
27743055Sdanmcd find_tun_and_set_itp(ill_t *ill, ipsec_tun_pol_t *itp)
27753055Sdanmcd {
27763055Sdanmcd 	queue_t *q;
27773055Sdanmcd 	tun_t *tun;
27783055Sdanmcd 
27793055Sdanmcd 	/* Don't bother if this ill is going away. */
27803055Sdanmcd 	if (ill->ill_flags & ILL_CONDEMNED) {
27813055Sdanmcd 		ill_refrele(ill);
27823055Sdanmcd 		return;
27833055Sdanmcd 	}
27843055Sdanmcd 
27853055Sdanmcd 
27863055Sdanmcd 	q = ill->ill_wq;
27873055Sdanmcd 	claimstr(q);	/* Lighter-weight than freezestr(). */
27883055Sdanmcd 
27893055Sdanmcd 	do {
27903055Sdanmcd 		/* Use strcmp() because "tun" is bounded. */
27913055Sdanmcd 		if (strcmp(q->q_qinfo->qi_minfo->mi_idname, "tun") == 0) {
27923055Sdanmcd 			/* Aha!  Got it. */
27933055Sdanmcd 			tun = (tun_t *)q->q_ptr;
27943055Sdanmcd 			if (tun != NULL) {
27953055Sdanmcd 				mutex_enter(&tun->tun_lock);
27963055Sdanmcd 				if (tun->tun_itp != itp) {
27973055Sdanmcd 					ASSERT(tun->tun_itp == NULL);
27983055Sdanmcd 					ITP_REFHOLD(itp);
27993055Sdanmcd 					tun->tun_itp = itp;
28003055Sdanmcd 				}
28013055Sdanmcd 				mutex_exit(&tun->tun_lock);
28023055Sdanmcd 				goto release_and_return;
28033055Sdanmcd 			}
28043055Sdanmcd 			/*
28053055Sdanmcd 			 * Else assume this is some other module named "tun"
28063055Sdanmcd 			 * and move on, hoping we find one that actually has
28073055Sdanmcd 			 * something in q_ptr.
28083055Sdanmcd 			 */
28093055Sdanmcd 		}
28103055Sdanmcd 		q = q->q_next;
28113055Sdanmcd 	} while (q != NULL);
28123055Sdanmcd 
28133055Sdanmcd release_and_return:
28143055Sdanmcd 	releasestr(ill->ill_wq);
28153055Sdanmcd 	ill_refrele(ill);
28163055Sdanmcd }
28173055Sdanmcd 
28183055Sdanmcd /*
28193055Sdanmcd  * Sort through the mess of polhead options to retrieve an appropriate one.
28203055Sdanmcd  * Returns NULL if we send an spdsock error.  Returns a valid pointer if we
28213055Sdanmcd  * found a valid polhead.  Returns ALL_ACTIVE_POLHEADS (aka. -1) or
28223055Sdanmcd  * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to
28233055Sdanmcd  * act on ALL policy heads.
28243055Sdanmcd  */
28253055Sdanmcd static ipsec_policy_head_t *
28263055Sdanmcd get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid,
28273055Sdanmcd     int msgtype, ipsec_tun_pol_t **itpp)
28283055Sdanmcd {
28293055Sdanmcd 	ipsec_tun_pol_t *itp;
28303055Sdanmcd 	ipsec_policy_head_t *iph;
28313055Sdanmcd 	int errno;
28323055Sdanmcd 	char *tname;
28333055Sdanmcd 	boolean_t active;
28343055Sdanmcd 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
28356430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
28363055Sdanmcd 	uint64_t gen;	/* Placeholder */
28373055Sdanmcd 	ill_t *v4, *v6;
28383055Sdanmcd 
28393055Sdanmcd 	active = (spdid == SPD_ACTIVE);
28403055Sdanmcd 	*itpp = NULL;
28413055Sdanmcd 	if (!active && spdid != SPD_STANDBY) {
28423055Sdanmcd 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID);
28433055Sdanmcd 		return (NULL);
28443055Sdanmcd 	}
28453055Sdanmcd 
28463055Sdanmcd 	if (tunname != NULL) {
28473055Sdanmcd 		/* Acting on a tunnel's SPD. */
28483055Sdanmcd 		tname = (char *)tunname->spd_if_name;
28493055Sdanmcd 		if (*tname == '\0') {
28503055Sdanmcd 			/* Handle all-polhead cases here. */
28513055Sdanmcd 			if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) {
28523055Sdanmcd 				spdsock_diag(q, mp,
28533055Sdanmcd 				    SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
28543055Sdanmcd 				return (NULL);
28553055Sdanmcd 			}
28563055Sdanmcd 			return (active ? ALL_ACTIVE_POLHEADS :
28573055Sdanmcd 			    ALL_INACTIVE_POLHEADS);
28583055Sdanmcd 		}
28593055Sdanmcd 
28606430Sdanmcd 		itp = get_tunnel_policy(tname, ns);
28613055Sdanmcd 		if (itp == NULL) {
28623055Sdanmcd 			if (msgtype != SPD_ADDRULE) {
28633055Sdanmcd 				/* "Tunnel not found" */
28643055Sdanmcd 				spdsock_error(q, mp, ENOENT, 0);
28653055Sdanmcd 				return (NULL);
28663055Sdanmcd 			}
28673055Sdanmcd 
28683055Sdanmcd 			errno = 0;
28696430Sdanmcd 			itp = create_tunnel_policy(tname, &errno, &gen, ns);
28703055Sdanmcd 			if (itp == NULL) {
28713055Sdanmcd 				/*
28723055Sdanmcd 				 * Something very bad happened, most likely
28733055Sdanmcd 				 * ENOMEM.  Return an indicator.
28743055Sdanmcd 				 */
28753055Sdanmcd 				spdsock_error(q, mp, errno, 0);
28763055Sdanmcd 				return (NULL);
28773055Sdanmcd 			}
28783055Sdanmcd 		}
28793055Sdanmcd 		/*
28803055Sdanmcd 		 * Troll the plumbed tunnels and see if we have a
28813055Sdanmcd 		 * match.  We need to do this always in case we add
28823055Sdanmcd 		 * policy AFTER plumbing a tunnel.
28833055Sdanmcd 		 */
28843055Sdanmcd 		v4 = ill_lookup_on_name(tname, B_FALSE, B_FALSE, NULL,
28853448Sdh155122 		    NULL, NULL, &errno, NULL, ns->netstack_ip);
28863055Sdanmcd 		if (v4 != NULL)
28873055Sdanmcd 			find_tun_and_set_itp(v4, itp);
28883055Sdanmcd 		v6 = ill_lookup_on_name(tname, B_FALSE, B_TRUE, NULL,
28893448Sdh155122 		    NULL, NULL, &errno, NULL, ns->netstack_ip);
28903055Sdanmcd 		if (v6 != NULL)
28913055Sdanmcd 			find_tun_and_set_itp(v6, itp);
28923055Sdanmcd 		ASSERT(itp != NULL);
28933055Sdanmcd 		*itpp = itp;
28943055Sdanmcd 		/* For spdsock dump state, set the polhead's name. */
28953055Sdanmcd 		if (msgtype == SPD_DUMP) {
28966430Sdanmcd 			ITP_REFHOLD(itp);
28976430Sdanmcd 			ss->spdsock_itp = itp;
28983055Sdanmcd 			ss->spdsock_dump_tunnel = itp->itp_flags &
28993055Sdanmcd 			    (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL);
29003055Sdanmcd 		}
29013055Sdanmcd 	} else {
29023055Sdanmcd 		itp = NULL;
29033055Sdanmcd 		/* For spdsock dump state, indicate it's global policy. */
29043055Sdanmcd 		if (msgtype == SPD_DUMP)
29056430Sdanmcd 			ss->spdsock_itp = NULL;
29063055Sdanmcd 	}
29073055Sdanmcd 
29083055Sdanmcd 	if (active)
29093448Sdh155122 		iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy;
29103055Sdanmcd 	else
29113448Sdh155122 		iph = (itp == NULL) ? ipsec_inactive_policy(ns) :
29123055Sdanmcd 		    itp->itp_inactive;
29133055Sdanmcd 
29143055Sdanmcd 	ASSERT(iph != NULL);
29153055Sdanmcd 	if (itp != NULL) {
29163055Sdanmcd 		IPPH_REFHOLD(iph);
29173055Sdanmcd 	}
29183055Sdanmcd 
29193055Sdanmcd 	return (iph);
29203055Sdanmcd }
29213055Sdanmcd 
29220Sstevel@tonic-gate static void
29230Sstevel@tonic-gate spdsock_parse(queue_t *q, mblk_t *mp)
29240Sstevel@tonic-gate {
29250Sstevel@tonic-gate 	spd_msg_t *spmsg;
29260Sstevel@tonic-gate 	spd_ext_t *extv[SPD_EXT_MAX + 1];
29270Sstevel@tonic-gate 	uint_t msgsize;
29280Sstevel@tonic-gate 	ipsec_policy_head_t *iph;
29293055Sdanmcd 	ipsec_tun_pol_t *itp;
29303055Sdanmcd 	spd_if_t *tunname;
29313448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
29323448Sdh155122 	spd_stack_t *spds = ss->spdsock_spds;
29333448Sdh155122 	netstack_t *ns = spds->spds_netstack;
29343448Sdh155122 	ipsec_stack_t *ipss = ns->netstack_ipsec;
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 	/* Make sure nothing's below me. */
29370Sstevel@tonic-gate 	ASSERT(WR(q)->q_next == NULL);
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 	spmsg = (spd_msg_t *)mp->b_rptr;
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 	msgsize = SPD_64TO8(spmsg->spd_msg_len);
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	if (msgdsize(mp) != msgsize) {
29440Sstevel@tonic-gate 		/*
29450Sstevel@tonic-gate 		 * Message len incorrect w.r.t. actual size.  Send an error
29460Sstevel@tonic-gate 		 * (EMSGSIZE).	It may be necessary to massage things a
29470Sstevel@tonic-gate 		 * bit.	 For example, if the spd_msg_type is hosed,
29480Sstevel@tonic-gate 		 * I need to set it to SPD_RESERVED to get delivery to
29490Sstevel@tonic-gate 		 * do the right thing.	Then again, maybe just letting
29500Sstevel@tonic-gate 		 * the error delivery do the right thing.
29510Sstevel@tonic-gate 		 */
29523448Sdh155122 		ss2dbg(spds,
29533448Sdh155122 		    ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
29540Sstevel@tonic-gate 		    msgdsize(mp), msgsize));
29550Sstevel@tonic-gate 		spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE);
29560Sstevel@tonic-gate 		return;
29570Sstevel@tonic-gate 	}
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 	if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
29600Sstevel@tonic-gate 		/* Get all message into one mblk. */
29610Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
29620Sstevel@tonic-gate 			/*
29630Sstevel@tonic-gate 			 * Something screwy happened.
29640Sstevel@tonic-gate 			 */
29653448Sdh155122 			ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n"));
29660Sstevel@tonic-gate 			return;
29670Sstevel@tonic-gate 		} else {
29680Sstevel@tonic-gate 			spmsg = (spd_msg_t *)mp->b_rptr;
29690Sstevel@tonic-gate 		}
29700Sstevel@tonic-gate 	}
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 	switch (spdsock_get_ext(extv, spmsg, msgsize)) {
29730Sstevel@tonic-gate 	case KGE_DUP:
29740Sstevel@tonic-gate 		/* Handle duplicate extension. */
29753448Sdh155122 		ss1dbg(spds, ("Got duplicate extension of type %d.\n",
29760Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
29770Sstevel@tonic-gate 		spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]);
29780Sstevel@tonic-gate 		return;
29790Sstevel@tonic-gate 	case KGE_UNK:
29800Sstevel@tonic-gate 		/* Handle unknown extension. */
29813448Sdh155122 		ss1dbg(spds, ("Got unknown extension of type %d.\n",
29820Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
29830Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT);
29840Sstevel@tonic-gate 		return;
29850Sstevel@tonic-gate 	case KGE_LEN:
29860Sstevel@tonic-gate 		/* Length error. */
29873448Sdh155122 		ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n",
29880Sstevel@tonic-gate 		    extv[0]->spd_ext_len, extv[0]->spd_ext_type));
29890Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN);
29900Sstevel@tonic-gate 		return;
29910Sstevel@tonic-gate 	case KGE_CHK:
29920Sstevel@tonic-gate 		/* Reality check failed. */
29933448Sdh155122 		ss1dbg(spds, ("Reality check failed on extension type %d.\n",
29940Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
29950Sstevel@tonic-gate 		spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]);
29960Sstevel@tonic-gate 		return;
29970Sstevel@tonic-gate 	default:
29980Sstevel@tonic-gate 		/* Default case is no errors. */
29990Sstevel@tonic-gate 		break;
30000Sstevel@tonic-gate 	}
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate 	/*
30030Sstevel@tonic-gate 	 * Special-case SPD_UPDATEALGS so as not to load IPsec.
30040Sstevel@tonic-gate 	 */
30053448Sdh155122 	if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) {
30060Sstevel@tonic-gate 		spdsock_t *ss = (spdsock_t *)q->q_ptr;
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 		ASSERT(ss != NULL);
30093448Sdh155122 		ipsec_loader_loadnow(ipss);
30100Sstevel@tonic-gate 		ss->spdsock_timeout_arg = mp;
30110Sstevel@tonic-gate 		ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck,
30120Sstevel@tonic-gate 		    q, LOADCHECK_INTERVAL);
30130Sstevel@tonic-gate 		return;
30140Sstevel@tonic-gate 	}
30150Sstevel@tonic-gate 
30163055Sdanmcd 	/* First check for messages that need no polheads at all. */
30170Sstevel@tonic-gate 	switch (spmsg->spd_msg_type) {
30180Sstevel@tonic-gate 	case SPD_UPDATEALGS:
30190Sstevel@tonic-gate 		spdsock_updatealg(q, mp, extv);
30200Sstevel@tonic-gate 		return;
30210Sstevel@tonic-gate 	case SPD_ALGLIST:
30220Sstevel@tonic-gate 		spdsock_alglist(q, mp);
30230Sstevel@tonic-gate 		return;
30240Sstevel@tonic-gate 	case SPD_DUMPALGS:
30250Sstevel@tonic-gate 		spdsock_dumpalgs(q, mp);
30260Sstevel@tonic-gate 		return;
30273055Sdanmcd 	}
30283055Sdanmcd 
30293055Sdanmcd 	/*
30303055Sdanmcd 	 * Then check for ones that need both primary/secondary polheads,
30313055Sdanmcd 	 * finding the appropriate tunnel policy if need be.
30323055Sdanmcd 	 */
30333055Sdanmcd 	tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME];
30343055Sdanmcd 	switch (spmsg->spd_msg_type) {
30353055Sdanmcd 	case SPD_FLIP:
30363055Sdanmcd 		spdsock_flip(q, mp, tunname);
30373055Sdanmcd 		return;
30383055Sdanmcd 	case SPD_CLONE:
30393055Sdanmcd 		spdsock_clone(q, mp, tunname);
30403055Sdanmcd 		return;
30413055Sdanmcd 	}
30423055Sdanmcd 
30433055Sdanmcd 	/*
30443055Sdanmcd 	 * Finally, find ones that operate on exactly one polhead, or
30453055Sdanmcd 	 * "all polheads" of a given type (active/inactive).
30463055Sdanmcd 	 */
30473055Sdanmcd 	iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid,
30483055Sdanmcd 	    spmsg->spd_msg_type, &itp);
30493055Sdanmcd 	if (iph == NULL)
30503055Sdanmcd 		return;
30513055Sdanmcd 
30523055Sdanmcd 	/* All-polheads-ready operations. */
30533055Sdanmcd 	switch (spmsg->spd_msg_type) {
30543055Sdanmcd 	case SPD_FLUSH:
30553055Sdanmcd 		if (itp != NULL) {
30563055Sdanmcd 			mutex_enter(&itp->itp_lock);
30573055Sdanmcd 			if (spmsg->spd_msg_spdid == SPD_ACTIVE)
30583055Sdanmcd 				itp->itp_flags &= ~ITPF_PFLAGS;
30593055Sdanmcd 			else
30603055Sdanmcd 				itp->itp_flags &= ~ITPF_IFLAGS;
30613055Sdanmcd 			mutex_exit(&itp->itp_lock);
30623448Sdh155122 			ITP_REFRELE(itp, ns);
30633055Sdanmcd 		}
30644307Spwernau 		spdsock_flush(q, iph, itp, mp);
30653055Sdanmcd 		return;
30663055Sdanmcd 	case SPD_DUMP:
30673055Sdanmcd 		if (itp != NULL)
30683448Sdh155122 			ITP_REFRELE(itp, ns);
30693055Sdanmcd 		spdsock_dump(q, iph, mp);
30703055Sdanmcd 		return;
30713055Sdanmcd 	}
30723055Sdanmcd 
30733055Sdanmcd 	if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
30743055Sdanmcd 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
30753055Sdanmcd 		return;
30763055Sdanmcd 	}
30773055Sdanmcd 
30783055Sdanmcd 	/* Single-polhead-only operations. */
30793055Sdanmcd 	switch (spmsg->spd_msg_type) {
30803055Sdanmcd 	case SPD_ADDRULE:
30813055Sdanmcd 		spdsock_addrule(q, iph, mp, extv, itp);
30823055Sdanmcd 		break;
30833055Sdanmcd 	case SPD_DELETERULE:
30843055Sdanmcd 		spdsock_deleterule(q, iph, mp, extv, itp);
30853055Sdanmcd 		break;
30863055Sdanmcd 	case SPD_LOOKUP:
30873055Sdanmcd 		spdsock_lookup(q, iph, mp, extv, itp);
30883055Sdanmcd 		break;
30890Sstevel@tonic-gate 	default:
30900Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE);
30913055Sdanmcd 		break;
30920Sstevel@tonic-gate 	}
30933055Sdanmcd 
30946430Sdanmcd 	IPPH_REFRELE(iph, ns);
30953055Sdanmcd 	if (itp != NULL)
30963448Sdh155122 		ITP_REFRELE(itp, ns);
30970Sstevel@tonic-gate }
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate /*
31000Sstevel@tonic-gate  * If an algorithm mapping was received before IPsec was loaded, process it.
31010Sstevel@tonic-gate  * Called from the IPsec loader.
31020Sstevel@tonic-gate  */
31030Sstevel@tonic-gate void
31043448Sdh155122 spdsock_update_pending_algs(netstack_t *ns)
31050Sstevel@tonic-gate {
31063448Sdh155122 	spd_stack_t *spds = ns->netstack_spdsock;
31073448Sdh155122 
31083448Sdh155122 	mutex_enter(&spds->spds_alg_lock);
31093448Sdh155122 	if (spds->spds_algs_pending) {
31100Sstevel@tonic-gate 		int diag;
31113448Sdh155122 
31123448Sdh155122 		spdsock_do_updatealg(spds->spds_extv_algs, &diag,
31133448Sdh155122 		    spds);
31143448Sdh155122 		spds->spds_algs_pending = B_FALSE;
31150Sstevel@tonic-gate 	}
31163448Sdh155122 	mutex_exit(&spds->spds_alg_lock);
31170Sstevel@tonic-gate }
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate static void
31200Sstevel@tonic-gate spdsock_loadcheck(void *arg)
31210Sstevel@tonic-gate {
31220Sstevel@tonic-gate 	queue_t *q = (queue_t *)arg;
31230Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
31240Sstevel@tonic-gate 	mblk_t *mp;
31256430Sdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 	ASSERT(ss != NULL);
31280Sstevel@tonic-gate 
31290Sstevel@tonic-gate 	ss->spdsock_timeout = 0;
31300Sstevel@tonic-gate 	mp = ss->spdsock_timeout_arg;
31310Sstevel@tonic-gate 	ASSERT(mp != NULL);
31320Sstevel@tonic-gate 	ss->spdsock_timeout_arg = NULL;
31333448Sdh155122 	if (ipsec_failed(ipss))
31340Sstevel@tonic-gate 		spdsock_error(q, mp, EPROTONOSUPPORT, 0);
31350Sstevel@tonic-gate 	else
31360Sstevel@tonic-gate 		spdsock_parse(q, mp);
31370Sstevel@tonic-gate }
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate /*
31400Sstevel@tonic-gate  * Copy relevant state bits.
31410Sstevel@tonic-gate  */
31420Sstevel@tonic-gate static void
31430Sstevel@tonic-gate spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss)
31440Sstevel@tonic-gate {
31450Sstevel@tonic-gate 	*tap = spdsock_g_t_info_ack;
31460Sstevel@tonic-gate 	tap->CURRENT_state = ss->spdsock_state;
31470Sstevel@tonic-gate 	tap->OPT_size = spdsock_max_optsize;
31480Sstevel@tonic-gate }
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate /*
31510Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
31520Sstevel@tonic-gate  * spdsock_wput.  Much of the T_CAPABILITY_ACK information is copied from
31530Sstevel@tonic-gate  * spdsock_g_t_info_ack.  The current state of the stream is copied from
31540Sstevel@tonic-gate  * spdsock_state.
31550Sstevel@tonic-gate  */
31560Sstevel@tonic-gate static void
31570Sstevel@tonic-gate spdsock_capability_req(queue_t *q, mblk_t *mp)
31580Sstevel@tonic-gate {
31590Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
31600Sstevel@tonic-gate 	t_uscalar_t cap_bits1;
31610Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
31640Sstevel@tonic-gate 
31650Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
31665240Snordmark 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
31670Sstevel@tonic-gate 	if (mp == NULL)
31680Sstevel@tonic-gate 		return;
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
31710Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
31740Sstevel@tonic-gate 		spdsock_copy_info(&tcap->INFO_ack, ss);
31750Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
31760Sstevel@tonic-gate 	}
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 	qreply(q, mp);
31790Sstevel@tonic-gate }
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate /*
31820Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages. It is called by
31830Sstevel@tonic-gate  * spdsock_wput_other.
31840Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack.
31850Sstevel@tonic-gate  * The current state of the stream is copied from spdsock_state.
31860Sstevel@tonic-gate  */
31870Sstevel@tonic-gate static void
31880Sstevel@tonic-gate spdsock_info_req(q, mp)
31890Sstevel@tonic-gate 	queue_t	*q;
31900Sstevel@tonic-gate 	mblk_t	*mp;
31910Sstevel@tonic-gate {
31920Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
31930Sstevel@tonic-gate 	    T_INFO_ACK);
31940Sstevel@tonic-gate 	if (mp == NULL)
31950Sstevel@tonic-gate 		return;
31960Sstevel@tonic-gate 	spdsock_copy_info((struct T_info_ack *)mp->b_rptr,
31970Sstevel@tonic-gate 	    (spdsock_t *)q->q_ptr);
31980Sstevel@tonic-gate 	qreply(q, mp);
31990Sstevel@tonic-gate }
32000Sstevel@tonic-gate 
32010Sstevel@tonic-gate /*
32020Sstevel@tonic-gate  * spdsock_err_ack. This routine creates a
32030Sstevel@tonic-gate  * T_ERROR_ACK message and passes it
32040Sstevel@tonic-gate  * upstream.
32050Sstevel@tonic-gate  */
32060Sstevel@tonic-gate static void
32070Sstevel@tonic-gate spdsock_err_ack(q, mp, t_error, sys_error)
32080Sstevel@tonic-gate 	queue_t	*q;
32090Sstevel@tonic-gate 	mblk_t	*mp;
32100Sstevel@tonic-gate 	int	t_error;
32110Sstevel@tonic-gate 	int	sys_error;
32120Sstevel@tonic-gate {
32130Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
32140Sstevel@tonic-gate 		qreply(q, mp);
32150Sstevel@tonic-gate }
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate /*
32180Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
32190Sstevel@tonic-gate  * It returns the size of the option retrieved.
32200Sstevel@tonic-gate  */
32210Sstevel@tonic-gate /* ARGSUSED */
32220Sstevel@tonic-gate int
32230Sstevel@tonic-gate spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
32240Sstevel@tonic-gate {
32250Sstevel@tonic-gate 	int *i1 = (int *)ptr;
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate 	switch (level) {
32280Sstevel@tonic-gate 	case SOL_SOCKET:
32290Sstevel@tonic-gate 		switch (name) {
32300Sstevel@tonic-gate 		case SO_TYPE:
32310Sstevel@tonic-gate 			*i1 = SOCK_RAW;
32320Sstevel@tonic-gate 			break;
32330Sstevel@tonic-gate 		/*
32340Sstevel@tonic-gate 		 * The following two items can be manipulated,
32350Sstevel@tonic-gate 		 * but changing them should do nothing.
32360Sstevel@tonic-gate 		 */
32370Sstevel@tonic-gate 		case SO_SNDBUF:
32380Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
32390Sstevel@tonic-gate 			break;
32400Sstevel@tonic-gate 		case SO_RCVBUF:
32410Sstevel@tonic-gate 			*i1 = (int)(RD(q)->q_hiwat);
32420Sstevel@tonic-gate 			break;
32430Sstevel@tonic-gate 		}
32440Sstevel@tonic-gate 		break;
32450Sstevel@tonic-gate 	default:
32460Sstevel@tonic-gate 		return (0);
32470Sstevel@tonic-gate 	}
32480Sstevel@tonic-gate 	return (sizeof (int));
32490Sstevel@tonic-gate }
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate /*
32520Sstevel@tonic-gate  * This routine sets socket options.
32530Sstevel@tonic-gate  */
32540Sstevel@tonic-gate /* ARGSUSED */
32550Sstevel@tonic-gate int
32560Sstevel@tonic-gate spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name,
32570Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
32580Sstevel@tonic-gate     void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
32590Sstevel@tonic-gate {
32600Sstevel@tonic-gate 	int *i1 = (int *)invalp;
32613448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
32623448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 	switch (level) {
32650Sstevel@tonic-gate 	case SOL_SOCKET:
32660Sstevel@tonic-gate 		switch (name) {
32670Sstevel@tonic-gate 		case SO_SNDBUF:
32683448Sdh155122 			if (*i1 > spds->spds_max_buf)
32690Sstevel@tonic-gate 				return (ENOBUFS);
32700Sstevel@tonic-gate 			q->q_hiwat = *i1;
32710Sstevel@tonic-gate 			break;
32720Sstevel@tonic-gate 		case SO_RCVBUF:
32733448Sdh155122 			if (*i1 > spds->spds_max_buf)
32740Sstevel@tonic-gate 				return (ENOBUFS);
32750Sstevel@tonic-gate 			RD(q)->q_hiwat = *i1;
32768348SEric.Yu@Sun.COM 			(void) proto_set_rx_hiwat(RD(q), NULL, *i1);
32770Sstevel@tonic-gate 			break;
32780Sstevel@tonic-gate 		}
32790Sstevel@tonic-gate 		break;
32800Sstevel@tonic-gate 	}
32810Sstevel@tonic-gate 	return (0);
32820Sstevel@tonic-gate }
32830Sstevel@tonic-gate 
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate /*
32860Sstevel@tonic-gate  * Handle STREAMS messages.
32870Sstevel@tonic-gate  */
32880Sstevel@tonic-gate static void
32890Sstevel@tonic-gate spdsock_wput_other(queue_t *q, mblk_t *mp)
32900Sstevel@tonic-gate {
32910Sstevel@tonic-gate 	struct iocblk *iocp;
32920Sstevel@tonic-gate 	int error;
32933448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
32943448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
32953448Sdh155122 	cred_t		*cr;
32960Sstevel@tonic-gate 
32970Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
32980Sstevel@tonic-gate 	case M_PROTO:
32990Sstevel@tonic-gate 	case M_PCPROTO:
33000Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
33013448Sdh155122 			ss3dbg(spds, (
33020Sstevel@tonic-gate 			    "spdsock_wput_other: Not big enough M_PROTO\n"));
33030Sstevel@tonic-gate 			freemsg(mp);
33040Sstevel@tonic-gate 			return;
33050Sstevel@tonic-gate 		}
33060Sstevel@tonic-gate 		switch (((union T_primitives *)mp->b_rptr)->type) {
33070Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
33080Sstevel@tonic-gate 			spdsock_capability_req(q, mp);
33093448Sdh155122 			break;
33100Sstevel@tonic-gate 		case T_INFO_REQ:
33110Sstevel@tonic-gate 			spdsock_info_req(q, mp);
33123448Sdh155122 			break;
33130Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
33140Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
3315*8778SErik.Nordmark@Sun.COM 			/*
3316*8778SErik.Nordmark@Sun.COM 			 * All Solaris components should pass a db_credp
3317*8778SErik.Nordmark@Sun.COM 			 * for this TPI message, hence we ASSERT.
3318*8778SErik.Nordmark@Sun.COM 			 * But in case there is some other M_PROTO that looks
3319*8778SErik.Nordmark@Sun.COM 			 * like a TPI message sent by some other kernel
3320*8778SErik.Nordmark@Sun.COM 			 * component, we check and return an error.
3321*8778SErik.Nordmark@Sun.COM 			 */
3322*8778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, NULL);
3323*8778SErik.Nordmark@Sun.COM 			ASSERT(cr != NULL);
3324*8778SErik.Nordmark@Sun.COM 			if (cr == NULL) {
3325*8778SErik.Nordmark@Sun.COM 				spdsock_err_ack(q, mp, TSYSERR, EINVAL);
3326*8778SErik.Nordmark@Sun.COM 				return;
3327*8778SErik.Nordmark@Sun.COM 			}
3328*8778SErik.Nordmark@Sun.COM 			if (((union T_primitives *)mp->b_rptr)->type ==
3329*8778SErik.Nordmark@Sun.COM 			    T_SVR4_OPTMGMT_REQ) {
3330*8778SErik.Nordmark@Sun.COM 				(void) svr4_optcom_req(q, mp, cr,
3331*8778SErik.Nordmark@Sun.COM 				    &spdsock_opt_obj, B_FALSE);
3332*8778SErik.Nordmark@Sun.COM 			} else {
3333*8778SErik.Nordmark@Sun.COM 				(void) tpi_optcom_req(q, mp, cr,
3334*8778SErik.Nordmark@Sun.COM 				    &spdsock_opt_obj, B_FALSE);
3335*8778SErik.Nordmark@Sun.COM 			}
33363448Sdh155122 			break;
33370Sstevel@tonic-gate 		case T_DATA_REQ:
33380Sstevel@tonic-gate 		case T_EXDATA_REQ:
33390Sstevel@tonic-gate 		case T_ORDREL_REQ:
33400Sstevel@tonic-gate 			/* Illegal for spdsock. */
33410Sstevel@tonic-gate 			freemsg(mp);
33420Sstevel@tonic-gate 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
33433448Sdh155122 			break;
33440Sstevel@tonic-gate 		default:
33450Sstevel@tonic-gate 			/* Not supported by spdsock. */
33460Sstevel@tonic-gate 			spdsock_err_ack(q, mp, TNOTSUPPORT, 0);
33473448Sdh155122 			break;
33480Sstevel@tonic-gate 		}
33493448Sdh155122 		return;
33500Sstevel@tonic-gate 	case M_IOCTL:
33510Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
33520Sstevel@tonic-gate 		error = EINVAL;
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
33550Sstevel@tonic-gate 		case ND_SET:
33560Sstevel@tonic-gate 		case ND_GET:
33573448Sdh155122 			if (nd_getset(q, spds->spds_g_nd, mp)) {
33580Sstevel@tonic-gate 				qreply(q, mp);
33590Sstevel@tonic-gate 				return;
33600Sstevel@tonic-gate 			} else
33610Sstevel@tonic-gate 				error = ENOENT;
33620Sstevel@tonic-gate 			/* FALLTHRU */
33630Sstevel@tonic-gate 		default:
33640Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
33650Sstevel@tonic-gate 			return;
33660Sstevel@tonic-gate 		}
33670Sstevel@tonic-gate 	case M_FLUSH:
33680Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
33690Sstevel@tonic-gate 			flushq(q, FLUSHALL);
33700Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
33710Sstevel@tonic-gate 		}
33720Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
33730Sstevel@tonic-gate 			qreply(q, mp);
33740Sstevel@tonic-gate 			return;
33750Sstevel@tonic-gate 		}
33760Sstevel@tonic-gate 		/* Else FALLTHRU */
33770Sstevel@tonic-gate 	}
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate 	/* If fell through, just black-hole the message. */
33800Sstevel@tonic-gate 	freemsg(mp);
33810Sstevel@tonic-gate }
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate static void
33840Sstevel@tonic-gate spdsock_wput(queue_t *q, mblk_t *mp)
33850Sstevel@tonic-gate {
33860Sstevel@tonic-gate 	uint8_t *rptr = mp->b_rptr;
33870Sstevel@tonic-gate 	mblk_t *mp1;
33880Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
33893448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate 	/*
33920Sstevel@tonic-gate 	 * If we're dumping, defer processing other messages until the
33930Sstevel@tonic-gate 	 * dump completes.
33940Sstevel@tonic-gate 	 */
33950Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL) {
33960Sstevel@tonic-gate 		if (!putq(q, mp))
33970Sstevel@tonic-gate 			freemsg(mp);
33980Sstevel@tonic-gate 		return;
33990Sstevel@tonic-gate 	}
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
34020Sstevel@tonic-gate 	case M_DATA:
34030Sstevel@tonic-gate 		/*
34040Sstevel@tonic-gate 		 * Silently discard.
34050Sstevel@tonic-gate 		 */
34063448Sdh155122 		ss2dbg(spds, ("raw M_DATA in spdsock.\n"));
34070Sstevel@tonic-gate 		freemsg(mp);
34080Sstevel@tonic-gate 		return;
34090Sstevel@tonic-gate 	case M_PROTO:
34100Sstevel@tonic-gate 	case M_PCPROTO:
34110Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
34120Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
34130Sstevel@tonic-gate 				if ((mp1 = mp->b_cont) == NULL) {
34140Sstevel@tonic-gate 					/* No data after T_DATA_REQ. */
34153448Sdh155122 					ss2dbg(spds,
34163448Sdh155122 					    ("No data after DATA_REQ.\n"));
34170Sstevel@tonic-gate 					freemsg(mp);
34180Sstevel@tonic-gate 					return;
34190Sstevel@tonic-gate 				}
34200Sstevel@tonic-gate 				freeb(mp);
34210Sstevel@tonic-gate 				mp = mp1;
34223448Sdh155122 				ss2dbg(spds, ("T_DATA_REQ\n"));
34230Sstevel@tonic-gate 				break;	/* Out of switch. */
34240Sstevel@tonic-gate 			}
34250Sstevel@tonic-gate 		}
34260Sstevel@tonic-gate 		/* FALLTHRU */
34270Sstevel@tonic-gate 	default:
34283448Sdh155122 		ss3dbg(spds, ("In default wput case (%d %d).\n",
34290Sstevel@tonic-gate 		    mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
34300Sstevel@tonic-gate 		spdsock_wput_other(q, mp);
34310Sstevel@tonic-gate 		return;
34320Sstevel@tonic-gate 	}
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	/* I now have a PF_POLICY message in an M_DATA block. */
34350Sstevel@tonic-gate 	spdsock_parse(q, mp);
34360Sstevel@tonic-gate }
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate /*
34390Sstevel@tonic-gate  * Device open procedure, called when new queue pair created.
34400Sstevel@tonic-gate  * We are passed the read-side queue.
34410Sstevel@tonic-gate  */
34420Sstevel@tonic-gate /* ARGSUSED */
34430Sstevel@tonic-gate static int
34440Sstevel@tonic-gate spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
34450Sstevel@tonic-gate {
34460Sstevel@tonic-gate 	spdsock_t *ss;
34470Sstevel@tonic-gate 	queue_t *oq = OTHERQ(q);
34480Sstevel@tonic-gate 	minor_t ssminor;
34493448Sdh155122 	netstack_t *ns;
34503448Sdh155122 	spd_stack_t *spds;
34513448Sdh155122 
34523448Sdh155122 	if (secpolicy_ip_config(credp, B_FALSE) != 0)
34530Sstevel@tonic-gate 		return (EPERM);
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 	if (q->q_ptr != NULL)
34560Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	if (sflag & MODOPEN)
34590Sstevel@tonic-gate 		return (EINVAL);
34600Sstevel@tonic-gate 
34613448Sdh155122 	ns = netstack_find_by_cred(credp);
34623448Sdh155122 	ASSERT(ns != NULL);
34633448Sdh155122 	spds = ns->netstack_spdsock;
34643448Sdh155122 	ASSERT(spds != NULL);
34653448Sdh155122 
34663448Sdh155122 	ss2dbg(spds, ("Made it into PF_POLICY socket open.\n"));
34670Sstevel@tonic-gate 
34680Sstevel@tonic-gate 	ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP);
34693448Sdh155122 	if (ssminor == 0) {
34703448Sdh155122 		netstack_rele(spds->spds_netstack);
34710Sstevel@tonic-gate 		return (ENOMEM);
34723448Sdh155122 	}
34730Sstevel@tonic-gate 	ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP);
34740Sstevel@tonic-gate 	if (ss == NULL) {
34750Sstevel@tonic-gate 		vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1);
34763448Sdh155122 		netstack_rele(spds->spds_netstack);
34770Sstevel@tonic-gate 		return (ENOMEM);
34780Sstevel@tonic-gate 	}
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate 	ss->spdsock_minor = ssminor;
34810Sstevel@tonic-gate 	ss->spdsock_state = TS_UNBND;
34820Sstevel@tonic-gate 	ss->spdsock_dump_req = NULL;
34830Sstevel@tonic-gate 
34843448Sdh155122 	ss->spdsock_spds = spds;
34853448Sdh155122 
34860Sstevel@tonic-gate 	q->q_ptr = ss;
34870Sstevel@tonic-gate 	oq->q_ptr = ss;
34880Sstevel@tonic-gate 
34893448Sdh155122 	q->q_hiwat = spds->spds_recv_hiwat;
34903448Sdh155122 
34913448Sdh155122 	oq->q_hiwat = spds->spds_xmit_hiwat;
34923448Sdh155122 	oq->q_lowat = spds->spds_xmit_lowat;
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 	qprocson(q);
34958348SEric.Yu@Sun.COM 	(void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat);
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), ss->spdsock_minor);
34980Sstevel@tonic-gate 	return (0);
34990Sstevel@tonic-gate }
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate /*
35020Sstevel@tonic-gate  * Read-side service procedure, invoked when we get back-enabled
35030Sstevel@tonic-gate  * when buffer space becomes available.
35040Sstevel@tonic-gate  *
35050Sstevel@tonic-gate  * Dump another chunk if we were dumping before; when we finish, kick
35060Sstevel@tonic-gate  * the write-side queue in case it's waiting for read queue space.
35070Sstevel@tonic-gate  */
35080Sstevel@tonic-gate void
35090Sstevel@tonic-gate spdsock_rsrv(queue_t *q)
35100Sstevel@tonic-gate {
35110Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL)
35140Sstevel@tonic-gate 		spdsock_dump_some(q, ss);
35150Sstevel@tonic-gate 
35160Sstevel@tonic-gate 	if (ss->spdsock_dump_req == NULL)
35170Sstevel@tonic-gate 		qenable(OTHERQ(q));
35180Sstevel@tonic-gate }
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate /*
35210Sstevel@tonic-gate  * Write-side service procedure, invoked when we defer processing
35220Sstevel@tonic-gate  * if another message is received while a dump is in progress.
35230Sstevel@tonic-gate  */
35240Sstevel@tonic-gate void
35250Sstevel@tonic-gate spdsock_wsrv(queue_t *q)
35260Sstevel@tonic-gate {
35270Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
35280Sstevel@tonic-gate 	mblk_t *mp;
35296430Sdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL) {
35320Sstevel@tonic-gate 		qenable(OTHERQ(q));
35330Sstevel@tonic-gate 		return;
35340Sstevel@tonic-gate 	}
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
35373448Sdh155122 		if (ipsec_loaded(ipss)) {
35380Sstevel@tonic-gate 			spdsock_wput(q, mp);
35390Sstevel@tonic-gate 			if (ss->spdsock_dump_req != NULL)
35400Sstevel@tonic-gate 				return;
35413448Sdh155122 		} else if (!ipsec_failed(ipss)) {
35420Sstevel@tonic-gate 			(void) putq(q, mp);
35430Sstevel@tonic-gate 		} else {
35440Sstevel@tonic-gate 			spdsock_error(q, mp, EPFNOSUPPORT, 0);
35450Sstevel@tonic-gate 		}
35460Sstevel@tonic-gate 	}
35470Sstevel@tonic-gate }
35480Sstevel@tonic-gate 
35490Sstevel@tonic-gate static int
35500Sstevel@tonic-gate spdsock_close(queue_t *q)
35510Sstevel@tonic-gate {
35520Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
35533448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	qprocsoff(q);
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate 	/* Safe assumption. */
35580Sstevel@tonic-gate 	ASSERT(ss != NULL);
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate 	if (ss->spdsock_timeout != 0)
35610Sstevel@tonic-gate 		(void) quntimeout(q, ss->spdsock_timeout);
35620Sstevel@tonic-gate 
35633448Sdh155122 	ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n"));
35640Sstevel@tonic-gate 
35650Sstevel@tonic-gate 	vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1);
35663448Sdh155122 	netstack_rele(ss->spdsock_spds->spds_netstack);
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	kmem_free(ss, sizeof (spdsock_t));
35690Sstevel@tonic-gate 	return (0);
35700Sstevel@tonic-gate }
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate /*
35730Sstevel@tonic-gate  * Merge the IPsec algorithms tables with the received algorithm information.
35740Sstevel@tonic-gate  */
35750Sstevel@tonic-gate void
35763448Sdh155122 spdsock_merge_algs(spd_stack_t *spds)
35770Sstevel@tonic-gate {
35780Sstevel@tonic-gate 	ipsec_alginfo_t *alg, *oalg;
35790Sstevel@tonic-gate 	ipsec_algtype_t algtype;
35800Sstevel@tonic-gate 	uint_t algidx, algid, nalgs;
35810Sstevel@tonic-gate 	crypto_mech_name_t *mechs;
35820Sstevel@tonic-gate 	uint_t mech_count, mech_idx;
35833448Sdh155122 	netstack_t	*ns = spds->spds_netstack;
35843448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
35853448Sdh155122 
35863448Sdh155122 	ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate 	/*
35890Sstevel@tonic-gate 	 * Get the list of supported mechanisms from the crypto framework.
35900Sstevel@tonic-gate 	 * If a mechanism is supported by KCF, resolve its mechanism
35910Sstevel@tonic-gate 	 * id and mark it as being valid. This operation must be done
35920Sstevel@tonic-gate 	 * without holding alg_lock, since it can cause a provider
35930Sstevel@tonic-gate 	 * module to be loaded and the provider notification callback to
35940Sstevel@tonic-gate 	 * be invoked.
35950Sstevel@tonic-gate 	 */
35960Sstevel@tonic-gate 	mechs = crypto_get_mech_list(&mech_count, KM_SLEEP);
35970Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
35980Sstevel@tonic-gate 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
35990Sstevel@tonic-gate 			int algflags = 0;
36000Sstevel@tonic-gate 			crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID;
36010Sstevel@tonic-gate 
36023448Sdh155122 			alg = spds->spds_algs[algtype][algid];
36033448Sdh155122 			if (alg == NULL)
36040Sstevel@tonic-gate 				continue;
36050Sstevel@tonic-gate 
36060Sstevel@tonic-gate 			/*
36070Sstevel@tonic-gate 			 * The NULL encryption algorithm is a special
36080Sstevel@tonic-gate 			 * case because there are no mechanisms, yet
36090Sstevel@tonic-gate 			 * the algorithm is still valid.
36100Sstevel@tonic-gate 			 */
36110Sstevel@tonic-gate 			if (alg->alg_id == SADB_EALG_NULL) {
36120Sstevel@tonic-gate 				alg->alg_mech_type = CRYPTO_MECHANISM_INVALID;
36130Sstevel@tonic-gate 				alg->alg_flags = ALG_FLAG_VALID;
36140Sstevel@tonic-gate 				continue;
36150Sstevel@tonic-gate 			}
36160Sstevel@tonic-gate 
36170Sstevel@tonic-gate 			for (mech_idx = 0; mech_idx < mech_count; mech_idx++) {
36180Sstevel@tonic-gate 				if (strncmp(alg->alg_mech_name, mechs[mech_idx],
36190Sstevel@tonic-gate 				    CRYPTO_MAX_MECH_NAME) == 0) {
36200Sstevel@tonic-gate 					mt = crypto_mech2id(alg->alg_mech_name);
36210Sstevel@tonic-gate 					ASSERT(mt != CRYPTO_MECHANISM_INVALID);
36220Sstevel@tonic-gate 					algflags = ALG_FLAG_VALID;
36230Sstevel@tonic-gate 					break;
36240Sstevel@tonic-gate 				}
36250Sstevel@tonic-gate 			}
36260Sstevel@tonic-gate 			alg->alg_mech_type = mt;
36270Sstevel@tonic-gate 			alg->alg_flags = algflags;
36280Sstevel@tonic-gate 		}
36290Sstevel@tonic-gate 	}
36300Sstevel@tonic-gate 
36313448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 	/*
36340Sstevel@tonic-gate 	 * For each algorithm currently defined, check if it is
36350Sstevel@tonic-gate 	 * present in the new tables created from the SPD_UPDATEALGS
36360Sstevel@tonic-gate 	 * message received from user-space.
36370Sstevel@tonic-gate 	 * Delete the algorithm entries that are currently defined
36380Sstevel@tonic-gate 	 * but not part of the new tables.
36390Sstevel@tonic-gate 	 */
36400Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
36413448Sdh155122 		nalgs = ipss->ipsec_nalgs[algtype];
36420Sstevel@tonic-gate 		for (algidx = 0; algidx < nalgs; algidx++) {
36433448Sdh155122 			algid = ipss->ipsec_sortlist[algtype][algidx];
36443448Sdh155122 			if (spds->spds_algs[algtype][algid] == NULL)
36453448Sdh155122 				ipsec_alg_unreg(algtype, algid, ns);
36460Sstevel@tonic-gate 		}
36470Sstevel@tonic-gate 	}
36480Sstevel@tonic-gate 
36490Sstevel@tonic-gate 	/*
36500Sstevel@tonic-gate 	 * For each algorithm we just received, check if it is
36510Sstevel@tonic-gate 	 * present in the currently defined tables. If it is, swap
36520Sstevel@tonic-gate 	 * the entry with the one we just allocated.
36530Sstevel@tonic-gate 	 * If the new algorithm is not in the current tables,
36540Sstevel@tonic-gate 	 * add it.
36550Sstevel@tonic-gate 	 */
36560Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
36570Sstevel@tonic-gate 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
36583448Sdh155122 			alg = spds->spds_algs[algtype][algid];
36593448Sdh155122 			if (alg == NULL)
36600Sstevel@tonic-gate 				continue;
36610Sstevel@tonic-gate 
36623448Sdh155122 			if ((oalg = ipss->ipsec_alglists[algtype][algid]) ==
36633448Sdh155122 			    NULL) {
36640Sstevel@tonic-gate 				/*
36650Sstevel@tonic-gate 				 * New algorithm, add it to the algorithm
36660Sstevel@tonic-gate 				 * table.
36670Sstevel@tonic-gate 				 */
36683448Sdh155122 				ipsec_alg_reg(algtype, alg, ns);
36690Sstevel@tonic-gate 			} else {
36700Sstevel@tonic-gate 				/*
36710Sstevel@tonic-gate 				 * Algorithm is already in the table. Swap
36720Sstevel@tonic-gate 				 * the existing entry with the new one.
36730Sstevel@tonic-gate 				 */
36743448Sdh155122 				ipsec_alg_fix_min_max(alg, algtype, ns);
36753448Sdh155122 				ipss->ipsec_alglists[algtype][algid] = alg;
36760Sstevel@tonic-gate 				ipsec_alg_free(oalg);
36770Sstevel@tonic-gate 			}
36783448Sdh155122 			spds->spds_algs[algtype][algid] = NULL;
36790Sstevel@tonic-gate 		}
36800Sstevel@tonic-gate 	}
36810Sstevel@tonic-gate 
36823448Sdh155122 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
36833448Sdh155122 		ipss->ipsec_algs_exec_mode[algtype] =
36843448Sdh155122 		    spds->spds_algs_exec_mode[algtype];
36853448Sdh155122 	}
36863448Sdh155122 
36873448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 	crypto_free_mech_list(mechs, mech_count);
36900Sstevel@tonic-gate 
36913448Sdh155122 	ipsecah_algs_changed(ns);
36923448Sdh155122 	ipsecesp_algs_changed(ns);
36930Sstevel@tonic-gate }
3694