xref: /onnv-gate/usr/src/uts/common/inet/ip/spdsock.c (revision 12273:63678502e95e)
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*12273SCasper.Dik@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/param.h>
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/stream.h>
280Sstevel@tonic-gate #include <sys/strsubr.h>
290Sstevel@tonic-gate #include <sys/strsun.h>
300Sstevel@tonic-gate #include <sys/stropts.h>
313448Sdh155122 #include <sys/zone.h>
320Sstevel@tonic-gate #include <sys/vnode.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
350Sstevel@tonic-gate #include <sys/tihdr.h>
360Sstevel@tonic-gate #include <sys/ddi.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/mkdev.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/suntpi.h>
430Sstevel@tonic-gate #include <sys/policy.h>
4410616SSebastien.Roy@Sun.COM #include <sys/dls.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>
590Sstevel@tonic-gate #include <inet/optcom.h>
600Sstevel@tonic-gate #include <inet/ipsec_impl.h>
610Sstevel@tonic-gate #include <inet/spdsock.h>
620Sstevel@tonic-gate #include <inet/sadb.h>
6310616SSebastien.Roy@Sun.COM #include <inet/iptun.h>
6410616SSebastien.Roy@Sun.COM #include <inet/iptun/iptun_impl.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);
158*12273SCasper.Dik@Sun.COM static void spdsock_stack_shutdown(netstackid_t stackid, void *arg);
1593448Sdh155122 static void spdsock_stack_fini(netstackid_t stackid, void *arg);
1600Sstevel@tonic-gate static void spdsock_loadcheck(void *);
1613448Sdh155122 static void spdsock_merge_algs(spd_stack_t *);
1623448Sdh155122 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *);
1633055Sdanmcd static mblk_t *spdsock_dump_next_record(spdsock_t *);
16411456Sdanmcd@sun.com static void update_iptun_policy(ipsec_tun_pol_t *);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static struct module_info info = {
1670Sstevel@tonic-gate 	5138, "spdsock", 1, INFPSZ, 512, 128
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static struct qinit rinit = {
1710Sstevel@tonic-gate 	NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close,
1720Sstevel@tonic-gate 	NULL, &info
1730Sstevel@tonic-gate };
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate static struct qinit winit = {
1760Sstevel@tonic-gate 	(pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info
1770Sstevel@tonic-gate };
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate struct streamtab spdsockinfo = {
1800Sstevel@tonic-gate 	&rinit, &winit
1810Sstevel@tonic-gate };
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /* mapping from alg type to protocol number, as per RFC 2407 */
1840Sstevel@tonic-gate static const uint_t algproto[] = {
1850Sstevel@tonic-gate 	PROTO_IPSEC_AH,
1860Sstevel@tonic-gate 	PROTO_IPSEC_ESP,
1870Sstevel@tonic-gate };
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #define	NALGPROTOS	(sizeof (algproto) / sizeof (algproto[0]))
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /* mapping from kernel exec mode to spdsock exec mode */
1920Sstevel@tonic-gate static const uint_t execmodes[] = {
1930Sstevel@tonic-gate 	SPD_ALG_EXEC_MODE_SYNC,
1940Sstevel@tonic-gate 	SPD_ALG_EXEC_MODE_ASYNC
1950Sstevel@tonic-gate };
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate #define	NEXECMODES	(sizeof (execmodes) / sizeof (execmodes[0]))
1980Sstevel@tonic-gate 
1993055Sdanmcd #define	ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1)
2003055Sdanmcd #define	ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2)
2013055Sdanmcd 
2024307Spwernau #define	ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL)
2034307Spwernau 
2040Sstevel@tonic-gate /* ARGSUSED */
2050Sstevel@tonic-gate static int
spdsock_param_get(q,mp,cp,cr)2060Sstevel@tonic-gate spdsock_param_get(q, mp, cp, cr)
2070Sstevel@tonic-gate 	queue_t	*q;
2080Sstevel@tonic-gate 	mblk_t	*mp;
2090Sstevel@tonic-gate 	caddr_t	cp;
2100Sstevel@tonic-gate 	cred_t *cr;
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	spdsockparam_t	*spdsockpa = (spdsockparam_t *)cp;
2130Sstevel@tonic-gate 	uint_t value;
2143448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2153448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
2163448Sdh155122 
2173448Sdh155122 	mutex_enter(&spds->spds_param_lock);
2180Sstevel@tonic-gate 	value = spdsockpa->spdsock_param_value;
2193448Sdh155122 	mutex_exit(&spds->spds_param_lock);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
2220Sstevel@tonic-gate 	return (0);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /* This routine sets an NDD variable in a spdsockparam_t structure. */
2260Sstevel@tonic-gate /* ARGSUSED */
2270Sstevel@tonic-gate static int
spdsock_param_set(q,mp,value,cp,cr)2280Sstevel@tonic-gate spdsock_param_set(q, mp, value, cp, cr)
2290Sstevel@tonic-gate 	queue_t	*q;
2300Sstevel@tonic-gate 	mblk_t	*mp;
2310Sstevel@tonic-gate 	char *value;
2320Sstevel@tonic-gate 	caddr_t	cp;
2330Sstevel@tonic-gate 	cred_t *cr;
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	ulong_t	new_value;
2360Sstevel@tonic-gate 	spdsockparam_t	*spdsockpa = (spdsockparam_t *)cp;
2373448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2383448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* Convert the value from a string into a long integer. */
2410Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
2420Sstevel@tonic-gate 		return (EINVAL);
2430Sstevel@tonic-gate 
2443448Sdh155122 	mutex_enter(&spds->spds_param_lock);
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
2470Sstevel@tonic-gate 	 * required bounds.
2480Sstevel@tonic-gate 	 */
2490Sstevel@tonic-gate 	if (new_value < spdsockpa->spdsock_param_min ||
2500Sstevel@tonic-gate 	    new_value > spdsockpa->spdsock_param_max) {
2513448Sdh155122 		mutex_exit(&spds->spds_param_lock);
2520Sstevel@tonic-gate 		return (EINVAL);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/* Set the new value */
2560Sstevel@tonic-gate 	spdsockpa->spdsock_param_value = new_value;
2573448Sdh155122 	mutex_exit(&spds->spds_param_lock);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	return (0);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2623448Sdh155122 /*
2633448Sdh155122  * Initialize at module load time
2643448Sdh155122  */
2650Sstevel@tonic-gate boolean_t
spdsock_ddi_init(void)2660Sstevel@tonic-gate spdsock_ddi_init(void)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	spdsock_max_optsize = optcom_max_optsize(
2690Sstevel@tonic-gate 	    spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1,
2720Sstevel@tonic-gate 	    NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
2730Sstevel@tonic-gate 
2743448Sdh155122 	/*
2753448Sdh155122 	 * We want to be informed each time a stack is created or
2763448Sdh155122 	 * destroyed in the kernel, so we can maintain the
2773448Sdh155122 	 * set of spd_stack_t's.
2783448Sdh155122 	 */
279*12273SCasper.Dik@Sun.COM 	netstack_register(NS_SPDSOCK, spdsock_stack_init,
280*12273SCasper.Dik@Sun.COM 	    spdsock_stack_shutdown, spdsock_stack_fini);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	return (B_TRUE);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2853448Sdh155122 /*
2863448Sdh155122  * Walk through the param array specified registering each element with the
2873448Sdh155122  * named dispatch handler.
2883448Sdh155122  */
2893448Sdh155122 static boolean_t
spdsock_param_register(IDP * ndp,spdsockparam_t * ssp,int cnt)2903448Sdh155122 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt)
2913448Sdh155122 {
2923448Sdh155122 	for (; cnt-- > 0; ssp++) {
2933448Sdh155122 		if (ssp->spdsock_param_name != NULL &&
2943448Sdh155122 		    ssp->spdsock_param_name[0]) {
2953448Sdh155122 			if (!nd_load(ndp,
2963448Sdh155122 			    ssp->spdsock_param_name,
2973448Sdh155122 			    spdsock_param_get, spdsock_param_set,
2983448Sdh155122 			    (caddr_t)ssp)) {
2993448Sdh155122 				nd_free(ndp);
3003448Sdh155122 				return (B_FALSE);
3013448Sdh155122 			}
3023448Sdh155122 		}
3033448Sdh155122 	}
3043448Sdh155122 	return (B_TRUE);
3053448Sdh155122 }
3063448Sdh155122 
3073448Sdh155122 /*
3083448Sdh155122  * Initialize for each stack instance
3093448Sdh155122  */
3103448Sdh155122 /* ARGSUSED */
3113448Sdh155122 static void *
spdsock_stack_init(netstackid_t stackid,netstack_t * ns)3123448Sdh155122 spdsock_stack_init(netstackid_t stackid, netstack_t *ns)
3133448Sdh155122 {
3143448Sdh155122 	spd_stack_t	*spds;
3153448Sdh155122 	spdsockparam_t	*ssp;
3163448Sdh155122 
3173448Sdh155122 	spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP);
3183448Sdh155122 	spds->spds_netstack = ns;
3193448Sdh155122 
3203448Sdh155122 	ASSERT(spds->spds_g_nd == NULL);
3213448Sdh155122 
3223448Sdh155122 	ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
3233448Sdh155122 	spds->spds_params = ssp;
3243448Sdh155122 	bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr));
3253448Sdh155122 
3263448Sdh155122 	(void) spdsock_param_register(&spds->spds_g_nd, ssp,
3273448Sdh155122 	    A_CNT(lcl_param_arr));
3283448Sdh155122 
3293448Sdh155122 	mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL);
3303448Sdh155122 	mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL);
3313448Sdh155122 
3323448Sdh155122 	return (spds);
3333448Sdh155122 }
3343448Sdh155122 
3350Sstevel@tonic-gate void
spdsock_ddi_destroy(void)3360Sstevel@tonic-gate spdsock_ddi_destroy(void)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	vmem_destroy(spdsock_vmem);
3393448Sdh155122 
3403448Sdh155122 	netstack_unregister(NS_SPDSOCK);
3413448Sdh155122 }
3423448Sdh155122 
343*12273SCasper.Dik@Sun.COM /*
344*12273SCasper.Dik@Sun.COM  * Do pre-removal cleanup.
345*12273SCasper.Dik@Sun.COM  */
346*12273SCasper.Dik@Sun.COM /* ARGSUSED */
347*12273SCasper.Dik@Sun.COM static void
spdsock_stack_shutdown(netstackid_t stackid,void * arg)348*12273SCasper.Dik@Sun.COM spdsock_stack_shutdown(netstackid_t stackid, void *arg)
349*12273SCasper.Dik@Sun.COM {
350*12273SCasper.Dik@Sun.COM 	spd_stack_t *spds = (spd_stack_t *)arg;
351*12273SCasper.Dik@Sun.COM 
352*12273SCasper.Dik@Sun.COM 	if (spds->spds_mp_algs != NULL) {
353*12273SCasper.Dik@Sun.COM 		freemsg(spds->spds_mp_algs);
354*12273SCasper.Dik@Sun.COM 		spds->spds_mp_algs = NULL;
355*12273SCasper.Dik@Sun.COM 	}
356*12273SCasper.Dik@Sun.COM }
357*12273SCasper.Dik@Sun.COM 
3583448Sdh155122 /* ARGSUSED */
3593448Sdh155122 static void
spdsock_stack_fini(netstackid_t stackid,void * arg)3603448Sdh155122 spdsock_stack_fini(netstackid_t stackid, void *arg)
3613448Sdh155122 {
3623448Sdh155122 	spd_stack_t *spds = (spd_stack_t *)arg;
3633448Sdh155122 
364*12273SCasper.Dik@Sun.COM 	ASSERT(spds->spds_mp_algs == NULL);
3653448Sdh155122 	mutex_destroy(&spds->spds_param_lock);
3663448Sdh155122 	mutex_destroy(&spds->spds_alg_lock);
3673448Sdh155122 	nd_free(&spds->spds_g_nd);
3683448Sdh155122 	kmem_free(spds->spds_params, sizeof (lcl_param_arr));
3693448Sdh155122 	spds->spds_params = NULL;
3703448Sdh155122 
3713448Sdh155122 	kmem_free(spds, sizeof (*spds));
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate  * NOTE: large quantities of this should be shared with keysock.
3760Sstevel@tonic-gate  * Would be nice to combine some of this into a common module, but
3770Sstevel@tonic-gate  * not possible given time pressures.
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate  * High-level reality checking of extensions.
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate /* ARGSUSED */ /* XXX */
3840Sstevel@tonic-gate static boolean_t
ext_check(spd_ext_t * ext)3850Sstevel@tonic-gate ext_check(spd_ext_t *ext)
3860Sstevel@tonic-gate {
3873055Sdanmcd 	spd_if_t *tunname = (spd_if_t *)ext;
3883055Sdanmcd 	int i;
3893055Sdanmcd 	char *idstr;
3903055Sdanmcd 
3913055Sdanmcd 	if (ext->spd_ext_type == SPD_EXT_TUN_NAME) {
3923055Sdanmcd 		/* (NOTE:  Modified from SADB_EXT_IDENTITY..) */
3933055Sdanmcd 
3943055Sdanmcd 		/*
3953055Sdanmcd 		 * Make sure the strings in these identities are
3963055Sdanmcd 		 * null-terminated.  Let's "proactively" null-terminate the
3973055Sdanmcd 		 * string at the last byte if it's not terminated sooner.
3983055Sdanmcd 		 */
3993055Sdanmcd 		i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t);
4003055Sdanmcd 		idstr = (char *)(tunname + 1);
4013055Sdanmcd 		while (*idstr != '\0' && i > 0) {
4023055Sdanmcd 			i--;
4033055Sdanmcd 			idstr++;
4043055Sdanmcd 		}
4053055Sdanmcd 		if (i == 0) {
4063055Sdanmcd 			/*
4073055Sdanmcd 			 * I.e., if the bozo user didn't NULL-terminate the
4083055Sdanmcd 			 * string...
4093055Sdanmcd 			 */
4103055Sdanmcd 			idstr--;
4113055Sdanmcd 			*idstr = '\0';
4123055Sdanmcd 		}
4133055Sdanmcd 	}
4140Sstevel@tonic-gate 	return (B_TRUE);	/* For now... */
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */
4200Sstevel@tonic-gate #define	KGE_OK	0
4210Sstevel@tonic-gate #define	KGE_DUP	1
4220Sstevel@tonic-gate #define	KGE_UNK	2
4230Sstevel@tonic-gate #define	KGE_LEN	3
4240Sstevel@tonic-gate #define	KGE_CHK	4
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
4280Sstevel@tonic-gate  * Return values include:
4290Sstevel@tonic-gate  *
4300Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
4310Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
4320Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
4330Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
4340Sstevel@tonic-gate  *		extv[0].
4350Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
4360Sstevel@tonic-gate  *		unknown header.
4370Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
4380Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
4390Sstevel@tonic-gate  *
4400Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
4410Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
4420Sstevel@tonic-gate  */
4430Sstevel@tonic-gate static int
spdsock_get_ext(spd_ext_t * extv[],spd_msg_t * basehdr,uint_t msgsize)4440Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
4530Sstevel@tonic-gate 		/* Check for unknown headers. */
4540Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
4550Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX)
4560Sstevel@tonic-gate 			return (KGE_UNK);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		/*
4590Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
4600Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
4610Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
4620Sstevel@tonic-gate 		 */
4630Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
4640Sstevel@tonic-gate 		    (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
4650Sstevel@tonic-gate 		    (void *)((uint8_t *)basehdr + msgsize))
4660Sstevel@tonic-gate 			return (KGE_LEN);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		/* Check for redundant headers. */
4690Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
4700Sstevel@tonic-gate 			return (KGE_DUP);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 		/*
4730Sstevel@tonic-gate 		 * Reality check the extension if possible at the spdsock
4740Sstevel@tonic-gate 		 * level.
4750Sstevel@tonic-gate 		 */
4760Sstevel@tonic-gate 		if (!ext_check(extv[0]))
4770Sstevel@tonic-gate 			return (KGE_CHK);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
4800Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
4830Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
4840Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/* Everything's cool. */
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
4910Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
4920Sstevel@tonic-gate 	 */
4930Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
4940Sstevel@tonic-gate 		extv[0] = NULL;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	return (KGE_OK);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate static const int bad_ext_diag[] = {
5000Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_LCLPORT,
5010Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_REMPORT,
5020Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_PROTO,
5030Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_LCLADDR,
5040Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_REMADDR,
5050Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_ACTION,
5060Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_RULE,
5070Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_RULESET,
5080Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE
5090Sstevel@tonic-gate };
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate static const int dup_ext_diag[] = {
5120Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_LCLPORT,
5130Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_REMPORT,
5140Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_PROTO,
5150Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_LCLADDR,
5160Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_REMADDR,
5170Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_ACTION,
5180Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_RULE,
5190Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_RULESET,
5200Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE
5210Sstevel@tonic-gate };
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate /*
5240Sstevel@tonic-gate  * Transmit a PF_POLICY error message to the instance either pointed to
5250Sstevel@tonic-gate  * by ks, the instance with serial number serial, or more, depending.
5260Sstevel@tonic-gate  *
5270Sstevel@tonic-gate  * The faulty message (or a reasonable facsimile thereof) is in mp.
5280Sstevel@tonic-gate  * This function will free mp or recycle it for delivery, thereby causing
5290Sstevel@tonic-gate  * the stream head to free it.
5300Sstevel@tonic-gate  */
5310Sstevel@tonic-gate static void
spdsock_error(queue_t * q,mblk_t * mp,int error,int diagnostic)5320Sstevel@tonic-gate spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_DATA);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (spmsg->spd_msg_type < SPD_MIN ||
5390Sstevel@tonic-gate 	    spmsg->spd_msg_type > SPD_MAX)
5400Sstevel@tonic-gate 		spmsg->spd_msg_type = SPD_RESERVED;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * Strip out extension headers.
5440Sstevel@tonic-gate 	 */
5450Sstevel@tonic-gate 	ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim);
5460Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (*spmsg);
5470Sstevel@tonic-gate 	spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t));
5480Sstevel@tonic-gate 	spmsg->spd_msg_errno = (uint8_t)error;
5490Sstevel@tonic-gate 	spmsg->spd_msg_diagnostic = (uint16_t)diagnostic;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	qreply(q, mp);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate static void
spdsock_diag(queue_t * q,mblk_t * mp,int diagnostic)5550Sstevel@tonic-gate spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	spdsock_error(q, mp, EINVAL, diagnostic);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate static void
spd_echo(queue_t * q,mblk_t * mp)5610Sstevel@tonic-gate spd_echo(queue_t *q, mblk_t *mp)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate 	qreply(q, mp);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5663055Sdanmcd /*
5673055Sdanmcd  * Do NOT consume a reference to itp.
5683055Sdanmcd  */
5693448Sdh155122 /*ARGSUSED*/
5700Sstevel@tonic-gate static void
spdsock_flush_node(ipsec_tun_pol_t * itp,void * cookie,netstack_t * ns)5713448Sdh155122 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns)
5723055Sdanmcd {
5733055Sdanmcd 	boolean_t active = (boolean_t)cookie;
5743055Sdanmcd 	ipsec_policy_head_t *iph;
5753055Sdanmcd 
5763055Sdanmcd 	iph = active ? itp->itp_policy : itp->itp_inactive;
5773055Sdanmcd 	IPPH_REFHOLD(iph);
5783055Sdanmcd 	mutex_enter(&itp->itp_lock);
57911456Sdanmcd@sun.com 	spdsock_flush_one(iph, ns);  /* Releases iph refhold. */
5803055Sdanmcd 	if (active)
5813055Sdanmcd 		itp->itp_flags &= ~ITPF_PFLAGS;
5823055Sdanmcd 	else
5833055Sdanmcd 		itp->itp_flags &= ~ITPF_IFLAGS;
5843055Sdanmcd 	mutex_exit(&itp->itp_lock);
58511456Sdanmcd@sun.com 	/* SPD_FLUSH is worth a tunnel MTU check. */
58611456Sdanmcd@sun.com 	update_iptun_policy(itp);
5873055Sdanmcd }
5883055Sdanmcd 
5893055Sdanmcd /*
5903055Sdanmcd  * Clear out one polhead.
5913055Sdanmcd  */
5923055Sdanmcd static void
spdsock_flush_one(ipsec_policy_head_t * iph,netstack_t * ns)5933448Sdh155122 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_WRITER);
5963448Sdh155122 	ipsec_polhead_flush(iph, ns);
5970Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
5983448Sdh155122 	IPPH_REFRELE(iph, ns);
5993055Sdanmcd }
6003055Sdanmcd 
6013055Sdanmcd static void
spdsock_flush(queue_t * q,ipsec_policy_head_t * iph,ipsec_tun_pol_t * itp,mblk_t * mp)6024307Spwernau spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp,
6034307Spwernau     mblk_t *mp)
6043055Sdanmcd {
6053055Sdanmcd 	boolean_t active;
6063448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
6076430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
60811861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
6093055Sdanmcd 
6103055Sdanmcd 	if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) {
6116430Sdanmcd 		spdsock_flush_one(iph, ns);
61211861SMarek.Pospisil@Sun.COM 		if (auditing) {
6134307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
6148778SErik.Nordmark@Sun.COM 			cred_t *cr;
6158778SErik.Nordmark@Sun.COM 			pid_t cpid;
6168778SErik.Nordmark@Sun.COM 
6178778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
6184307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
6198778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_FLUSH, cr, ns,
6208778SErik.Nordmark@Sun.COM 			    ITP_NAME(itp), active, 0, cpid);
6214307Spwernau 		}
6223055Sdanmcd 	} else {
6233055Sdanmcd 		active = (iph == ALL_ACTIVE_POLHEADS);
6243055Sdanmcd 
6253055Sdanmcd 		/* First flush the global policy. */
6263448Sdh155122 		spdsock_flush_one(active ? ipsec_system_policy(ns) :
6273448Sdh155122 		    ipsec_inactive_policy(ns), ns);
62811861SMarek.Pospisil@Sun.COM 		if (auditing) {
6298778SErik.Nordmark@Sun.COM 			cred_t *cr;
6308778SErik.Nordmark@Sun.COM 			pid_t cpid;
6318778SErik.Nordmark@Sun.COM 
6328778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
6338778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_FLUSH, cr, ns, NULL,
6348778SErik.Nordmark@Sun.COM 			    active, 0, cpid);
6354307Spwernau 		}
6363055Sdanmcd 		/* Then flush every tunnel's appropriate one. */
6376430Sdanmcd 		itp_walk(spdsock_flush_node, (void *)active, ns);
63811861SMarek.Pospisil@Sun.COM 		if (auditing) {
6398778SErik.Nordmark@Sun.COM 			cred_t *cr;
6408778SErik.Nordmark@Sun.COM 			pid_t cpid;
6418778SErik.Nordmark@Sun.COM 
6428778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
6438778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_FLUSH, cr, ns,
6448778SErik.Nordmark@Sun.COM 			    "all tunnels", active, 0, cpid);
6458778SErik.Nordmark@Sun.COM 		}
6463055Sdanmcd 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	spd_echo(q, mp);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate static boolean_t
spdsock_ext_to_sel(spd_ext_t ** extv,ipsec_selkey_t * sel,int * diag)6520Sstevel@tonic-gate spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	bzero(sel, sizeof (*sel));
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	if (extv[SPD_EXT_PROTO] != NULL) {
6570Sstevel@tonic-gate 		struct spd_proto *pr =
6580Sstevel@tonic-gate 		    (struct spd_proto *)extv[SPD_EXT_PROTO];
6590Sstevel@tonic-gate 		sel->ipsl_proto = pr->spd_proto_number;
6600Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_PROTOCOL;
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 	if (extv[SPD_EXT_LCLPORT] != NULL) {
6630Sstevel@tonic-gate 		struct spd_portrange *pr =
6640Sstevel@tonic-gate 		    (struct spd_portrange *)extv[SPD_EXT_LCLPORT];
6650Sstevel@tonic-gate 		sel->ipsl_lport = pr->spd_ports_minport;
6660Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_LOCAL_PORT;
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 	if (extv[SPD_EXT_REMPORT] != NULL) {
6690Sstevel@tonic-gate 		struct spd_portrange *pr =
6700Sstevel@tonic-gate 		    (struct spd_portrange *)extv[SPD_EXT_REMPORT];
6710Sstevel@tonic-gate 		sel->ipsl_rport = pr->spd_ports_minport;
6720Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_REMOTE_PORT;
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) {
6760Sstevel@tonic-gate 		struct spd_typecode *tc=
6770Sstevel@tonic-gate 		    (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE];
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_ICMP_TYPE;
6800Sstevel@tonic-gate 		sel->ipsl_icmp_type = tc->spd_typecode_type;
6810Sstevel@tonic-gate 		if (tc->spd_typecode_type_end < tc->spd_typecode_type)
6820Sstevel@tonic-gate 			sel->ipsl_icmp_type_end = tc->spd_typecode_type;
6830Sstevel@tonic-gate 		else
6840Sstevel@tonic-gate 			sel->ipsl_icmp_type_end = tc->spd_typecode_type_end;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		if (tc->spd_typecode_code != 255) {
6870Sstevel@tonic-gate 			sel->ipsl_valid |= IPSL_ICMP_CODE;
6880Sstevel@tonic-gate 			sel->ipsl_icmp_code = tc->spd_typecode_code;
6890Sstevel@tonic-gate 			if (tc->spd_typecode_code_end < tc->spd_typecode_code)
6900Sstevel@tonic-gate 				sel->ipsl_icmp_code_end = tc->spd_typecode_code;
6910Sstevel@tonic-gate 			else
6920Sstevel@tonic-gate 				sel->ipsl_icmp_code_end =
6930Sstevel@tonic-gate 				    tc->spd_typecode_code_end;
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate #define	ADDR2SEL(sel, extv, field, pfield, extn, bit)			      \
6970Sstevel@tonic-gate 	if ((extv)[(extn)] != NULL) {					      \
6980Sstevel@tonic-gate 		uint_t addrlen;						      \
6990Sstevel@tonic-gate 		struct spd_address *ap = 				      \
7000Sstevel@tonic-gate 			(struct spd_address *)((extv)[(extn)]); 	      \
7010Sstevel@tonic-gate 		addrlen = (ap->spd_address_af == AF_INET6) ? 		      \
7020Sstevel@tonic-gate 			IPV6_ADDR_LEN : IP_ADDR_LEN;			      \
7030Sstevel@tonic-gate 		if (SPD_64TO8(ap->spd_address_len) < 			      \
7040Sstevel@tonic-gate 			(addrlen + sizeof (*ap))) {			      \
7050Sstevel@tonic-gate 			*diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN;		      \
7060Sstevel@tonic-gate 			return (B_FALSE);				      \
7070Sstevel@tonic-gate 		}							      \
7080Sstevel@tonic-gate 		bcopy((ap+1), &((sel)->field), addrlen);		      \
7090Sstevel@tonic-gate 		(sel)->pfield = ap->spd_address_prefixlen;		      \
7100Sstevel@tonic-gate 		(sel)->ipsl_valid |= (bit);				      \
7110Sstevel@tonic-gate 		(sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ?	      \
7120Sstevel@tonic-gate 			IPSL_IPV6 : IPSL_IPV4;				      \
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen,
7160Sstevel@tonic-gate 	    SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR);
7170Sstevel@tonic-gate 	ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen,
7180Sstevel@tonic-gate 	    SPD_EXT_REMADDR, IPSL_REMOTE_ADDR);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) ==
7210Sstevel@tonic-gate 	    (IPSL_IPV6|IPSL_IPV4)) {
7220Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_MIXED_AF;
7230Sstevel@tonic-gate 		return (B_FALSE);
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate #undef ADDR2SEL
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	return (B_TRUE);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate static boolean_t
spd_convert_type(uint32_t type,ipsec_act_t * act)7320Sstevel@tonic-gate spd_convert_type(uint32_t type, ipsec_act_t *act)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate 	switch (type) {
7350Sstevel@tonic-gate 	case SPD_ACTTYPE_DROP:
7360Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_DISCARD;
7370Sstevel@tonic-gate 		return (B_TRUE);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	case SPD_ACTTYPE_PASS:
7400Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_CLEAR;
7410Sstevel@tonic-gate 		return (B_TRUE);
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	case SPD_ACTTYPE_IPSEC:
7440Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_APPLY;
7450Sstevel@tonic-gate 		return (B_TRUE);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 	return (B_FALSE);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate static boolean_t
spd_convert_flags(uint32_t flags,ipsec_act_t * act)7510Sstevel@tonic-gate spd_convert_flags(uint32_t flags, ipsec_act_t *act)
7520Sstevel@tonic-gate {
7530Sstevel@tonic-gate 	/*
7540Sstevel@tonic-gate 	 * Note use of !! for boolean canonicalization.
7550Sstevel@tonic-gate 	 */
7560Sstevel@tonic-gate 	act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH);
7570Sstevel@tonic-gate 	act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP);
7580Sstevel@tonic-gate 	act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA);
7590Sstevel@tonic-gate 	act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE);
7600Sstevel@tonic-gate 	act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE);
7610Sstevel@tonic-gate 	return (B_TRUE);
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate static void
spdsock_reset_act(ipsec_act_t * act)7650Sstevel@tonic-gate spdsock_reset_act(ipsec_act_t *act)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	bzero(act, sizeof (*act));
7680Sstevel@tonic-gate 	act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS;
7690Sstevel@tonic-gate 	act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS;
7700Sstevel@tonic-gate 	act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS;
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * Sanity check action against reality, and shrink-wrap key sizes..
7750Sstevel@tonic-gate  */
7760Sstevel@tonic-gate static boolean_t
spdsock_check_action(ipsec_act_t * act,boolean_t tunnel_polhead,int * diag,spd_stack_t * spds)7773448Sdh155122 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag,
7783448Sdh155122     spd_stack_t *spds)
7790Sstevel@tonic-gate {
7803055Sdanmcd 	if (tunnel_polhead && act->ipa_apply.ipp_use_unique) {
7813055Sdanmcd 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7823055Sdanmcd 		return (B_FALSE);
7833055Sdanmcd 	}
7840Sstevel@tonic-gate 	if ((act->ipa_type != IPSEC_ACT_APPLY) &&
7850Sstevel@tonic-gate 	    (act->ipa_apply.ipp_use_ah ||
7865240Snordmark 	    act->ipa_apply.ipp_use_esp ||
7875240Snordmark 	    act->ipa_apply.ipp_use_espa ||
7885240Snordmark 	    act->ipa_apply.ipp_use_se ||
7895240Snordmark 	    act->ipa_apply.ipp_use_unique)) {
7900Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7910Sstevel@tonic-gate 		return (B_FALSE);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 	if ((act->ipa_type == IPSEC_ACT_APPLY) &&
7940Sstevel@tonic-gate 	    !act->ipa_apply.ipp_use_ah &&
7950Sstevel@tonic-gate 	    !act->ipa_apply.ipp_use_esp) {
7960Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7970Sstevel@tonic-gate 		return (B_FALSE);
7980Sstevel@tonic-gate 	}
7993448Sdh155122 	return (ipsec_check_action(act, diag, spds->spds_netstack));
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate  * We may be short a few error checks here..
8040Sstevel@tonic-gate  */
8050Sstevel@tonic-gate static boolean_t
spdsock_ext_to_actvec(spd_ext_t ** extv,ipsec_act_t ** actpp,uint_t * nactp,int * diag,spd_stack_t * spds)8060Sstevel@tonic-gate spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp,
8073448Sdh155122     int *diag, spd_stack_t *spds)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	struct spd_ext_actions *sactp =
8100Sstevel@tonic-gate 	    (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
8110Sstevel@tonic-gate 	ipsec_act_t act, *actp, *endactp;
8120Sstevel@tonic-gate 	struct spd_attribute *attrp, *endattrp;
8130Sstevel@tonic-gate 	uint64_t *endp;
8140Sstevel@tonic-gate 	int nact;
8153055Sdanmcd 	boolean_t tunnel_polhead;
8163055Sdanmcd 
8173055Sdanmcd 	tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL &&
8183055Sdanmcd 	    (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags &
8195240Snordmark 	    SPD_RULE_FLAG_TUNNEL));
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	*actpp = NULL;
8220Sstevel@tonic-gate 	*nactp = 0;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if (sactp == NULL) {
8250Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_NO_ACTION_EXT;
8260Sstevel@tonic-gate 		return (B_FALSE);
8270Sstevel@tonic-gate 	}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/*
8300Sstevel@tonic-gate 	 * Parse the "action" extension and convert into an action chain.
8310Sstevel@tonic-gate 	 */
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	nact = sactp->spd_actions_count;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	endp = (uint64_t *)sactp;
8360Sstevel@tonic-gate 	endp += sactp->spd_actions_len;
8370Sstevel@tonic-gate 	endattrp = (struct spd_attribute *)endp;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP);
8400Sstevel@tonic-gate 	if (actp == NULL) {
8410Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_NO_MEM;
8420Sstevel@tonic-gate 		return (B_FALSE);
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 	*actpp = actp;
8450Sstevel@tonic-gate 	*nactp = nact;
8460Sstevel@tonic-gate 	endactp = actp + nact;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	spdsock_reset_act(&act);
8490Sstevel@tonic-gate 	attrp = (struct spd_attribute *)(&sactp[1]);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	for (; attrp < endattrp; attrp++) {
8520Sstevel@tonic-gate 		switch (attrp->spd_attr_tag) {
8530Sstevel@tonic-gate 		case SPD_ATTR_NOP:
8540Sstevel@tonic-gate 			break;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		case SPD_ATTR_EMPTY:
8570Sstevel@tonic-gate 			spdsock_reset_act(&act);
8580Sstevel@tonic-gate 			break;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		case SPD_ATTR_END:
8610Sstevel@tonic-gate 			attrp = endattrp;
8620Sstevel@tonic-gate 			/* FALLTHRU */
8630Sstevel@tonic-gate 		case SPD_ATTR_NEXT:
8640Sstevel@tonic-gate 			if (actp >= endactp) {
8650Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
8660Sstevel@tonic-gate 				goto fail;
8670Sstevel@tonic-gate 			}
8683448Sdh155122 			if (!spdsock_check_action(&act, tunnel_polhead,
8693448Sdh155122 			    diag, spds))
8700Sstevel@tonic-gate 				goto fail;
8710Sstevel@tonic-gate 			*actp++ = act;
8723055Sdanmcd 			spdsock_reset_act(&act);
8730Sstevel@tonic-gate 			break;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		case SPD_ATTR_TYPE:
8760Sstevel@tonic-gate 			if (!spd_convert_type(attrp->spd_attr_value, &act)) {
8770Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE;
8780Sstevel@tonic-gate 				goto fail;
8790Sstevel@tonic-gate 			}
8800Sstevel@tonic-gate 			break;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 		case SPD_ATTR_FLAGS:
8833055Sdanmcd 			if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) {
8843055Sdanmcd 				/*
8853055Sdanmcd 				 * Set "sa unique" for transport-mode
8863055Sdanmcd 				 * tunnels whether we want to or not.
8873055Sdanmcd 				 */
8883055Sdanmcd 				attrp->spd_attr_value |= SPD_APPLY_UNIQUE;
8893055Sdanmcd 			}
8900Sstevel@tonic-gate 			if (!spd_convert_flags(attrp->spd_attr_value, &act)) {
8910Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS;
8920Sstevel@tonic-gate 				goto fail;
8930Sstevel@tonic-gate 			}
8940Sstevel@tonic-gate 			break;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 		case SPD_ATTR_AH_AUTH:
8973055Sdanmcd 			if (attrp->spd_attr_value == 0) {
8983055Sdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG;
8993055Sdanmcd 				goto fail;
9003055Sdanmcd 			}
9010Sstevel@tonic-gate 			act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value;
9020Sstevel@tonic-gate 			break;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 		case SPD_ATTR_ESP_ENCR:
9053055Sdanmcd 			if (attrp->spd_attr_value == 0) {
9063055Sdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG;
9073055Sdanmcd 				goto fail;
9083055Sdanmcd 			}
9090Sstevel@tonic-gate 			act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value;
9100Sstevel@tonic-gate 			break;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 		case SPD_ATTR_ESP_AUTH:
9133055Sdanmcd 			if (attrp->spd_attr_value == 0) {
9143055Sdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG;
9153055Sdanmcd 				goto fail;
9163055Sdanmcd 			}
9170Sstevel@tonic-gate 			act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value;
9180Sstevel@tonic-gate 			break;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		case SPD_ATTR_ENCR_MINBITS:
9210Sstevel@tonic-gate 			act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value;
9220Sstevel@tonic-gate 			break;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		case SPD_ATTR_ENCR_MAXBITS:
9250Sstevel@tonic-gate 			act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value;
9260Sstevel@tonic-gate 			break;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 		case SPD_ATTR_AH_MINBITS:
9290Sstevel@tonic-gate 			act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value;
9300Sstevel@tonic-gate 			break;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		case SPD_ATTR_AH_MAXBITS:
9330Sstevel@tonic-gate 			act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value;
9340Sstevel@tonic-gate 			break;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 		case SPD_ATTR_ESPA_MINBITS:
9370Sstevel@tonic-gate 			act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value;
9380Sstevel@tonic-gate 			break;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 		case SPD_ATTR_ESPA_MAXBITS:
9410Sstevel@tonic-gate 			act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value;
9420Sstevel@tonic-gate 			break;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		case SPD_ATTR_LIFE_SOFT_TIME:
9450Sstevel@tonic-gate 		case SPD_ATTR_LIFE_HARD_TIME:
9460Sstevel@tonic-gate 		case SPD_ATTR_LIFE_SOFT_BYTES:
9470Sstevel@tonic-gate 		case SPD_ATTR_LIFE_HARD_BYTES:
9480Sstevel@tonic-gate 			break;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		case SPD_ATTR_KM_PROTO:
9510Sstevel@tonic-gate 			act.ipa_apply.ipp_km_proto = attrp->spd_attr_value;
9520Sstevel@tonic-gate 			break;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		case SPD_ATTR_KM_COOKIE:
9550Sstevel@tonic-gate 			act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value;
9560Sstevel@tonic-gate 			break;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 		case SPD_ATTR_REPLAY_DEPTH:
9590Sstevel@tonic-gate 			act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value;
9600Sstevel@tonic-gate 			break;
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 	if (actp != endactp) {
9640Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
9650Sstevel@tonic-gate 		goto fail;
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	return (B_TRUE);
9690Sstevel@tonic-gate fail:
9700Sstevel@tonic-gate 	ipsec_actvec_free(*actpp, nact);
9710Sstevel@tonic-gate 	*actpp = NULL;
9720Sstevel@tonic-gate 	return (B_FALSE);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate typedef struct
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	ipsec_policy_t *pol;
9780Sstevel@tonic-gate 	int dir;
9790Sstevel@tonic-gate } tmprule_t;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate static int
mkrule(ipsec_policy_head_t * iph,struct spd_rule * rule,ipsec_selkey_t * sel,ipsec_act_t * actp,int nact,uint_t dir,uint_t af,tmprule_t ** rp,uint64_t * index,spd_stack_t * spds)9820Sstevel@tonic-gate mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule,
9830Sstevel@tonic-gate     ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af,
9843448Sdh155122     tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
9850Sstevel@tonic-gate {
9860Sstevel@tonic-gate 	ipsec_policy_t *pol;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4);
9890Sstevel@tonic-gate 	sel->ipsl_valid |= af;
9900Sstevel@tonic-gate 
9913055Sdanmcd 	pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority,
9923448Sdh155122 	    index, spds->spds_netstack);
9930Sstevel@tonic-gate 	if (pol == NULL)
9940Sstevel@tonic-gate 		return (ENOMEM);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	(*rp)->pol = pol;
9970Sstevel@tonic-gate 	(*rp)->dir = dir;
9980Sstevel@tonic-gate 	(*rp)++;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	if (!ipsec_check_policy(iph, pol, dir))
10010Sstevel@tonic-gate 		return (EEXIST);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	rule->spd_rule_index = pol->ipsp_index;
10040Sstevel@tonic-gate 	return (0);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate static int
mkrulepair(ipsec_policy_head_t * iph,struct spd_rule * rule,ipsec_selkey_t * sel,ipsec_act_t * actp,int nact,uint_t dir,uint_t afs,tmprule_t ** rp,uint64_t * index,spd_stack_t * spds)10080Sstevel@tonic-gate mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule,
10090Sstevel@tonic-gate     ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs,
10103448Sdh155122     tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate 	int error;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	if (afs & IPSL_IPV4) {
10153055Sdanmcd 		error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp,
10163448Sdh155122 		    index, spds);
10170Sstevel@tonic-gate 		if (error != 0)
10180Sstevel@tonic-gate 			return (error);
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 	if (afs & IPSL_IPV6) {
10213055Sdanmcd 		error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp,
10223448Sdh155122 		    index, spds);
10230Sstevel@tonic-gate 		if (error != 0)
10240Sstevel@tonic-gate 			return (error);
10250Sstevel@tonic-gate 	}
10260Sstevel@tonic-gate 	return (0);
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate static void
spdsock_addrule(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp,spd_ext_t ** extv,ipsec_tun_pol_t * itp)10313055Sdanmcd spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
10323055Sdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	ipsec_selkey_t sel;
10350Sstevel@tonic-gate 	ipsec_act_t *actp;
10360Sstevel@tonic-gate 	uint_t nact;
10373055Sdanmcd 	int diag = 0, error, afs;
10380Sstevel@tonic-gate 	struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
10390Sstevel@tonic-gate 	tmprule_t rules[4], *rulep = &rules[0];
10403055Sdanmcd 	boolean_t tunnel_mode, empty_itp, active;
10413055Sdanmcd 	uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index;
10423448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
104311861SMarek.Pospisil@Sun.COM 	spd_stack_t *spds = ss->spdsock_spds;
104411861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	if (rule == NULL) {
10470Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
104811861SMarek.Pospisil@Sun.COM 		if (auditing) {
10494307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
10508778SErik.Nordmark@Sun.COM 			cred_t *cr;
10518778SErik.Nordmark@Sun.COM 			pid_t cpid;
10528778SErik.Nordmark@Sun.COM 
10538778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
10544307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
10558778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_ADDRULE, cr,
10564307Spwernau 			    spds->spds_netstack, ITP_NAME(itp), active,
10578778SErik.Nordmark@Sun.COM 			    SPD_DIAGNOSTIC_NO_RULE_EXT, cpid);
10584307Spwernau 		}
10590Sstevel@tonic-gate 		return;
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 
10623055Sdanmcd 	tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL);
10633055Sdanmcd 
10643055Sdanmcd 	if (itp != NULL) {
10653055Sdanmcd 		mutex_enter(&itp->itp_lock);
10663055Sdanmcd 		ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph);
10673055Sdanmcd 		active = (itp->itp_policy == iph);
10683055Sdanmcd 		if (ITP_P_ISACTIVE(itp, iph)) {
10693055Sdanmcd 			/* Check for mix-and-match of tunnel/transport. */
10703055Sdanmcd 			if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) ||
10713055Sdanmcd 			    (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) {
10723055Sdanmcd 				mutex_exit(&itp->itp_lock);
10733055Sdanmcd 				spdsock_error(q, mp, EBUSY, 0);
10743055Sdanmcd 				return;
10753055Sdanmcd 			}
10763055Sdanmcd 			empty_itp = B_FALSE;
10773055Sdanmcd 		} else {
10783055Sdanmcd 			empty_itp = B_TRUE;
10793055Sdanmcd 			itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE;
10803055Sdanmcd 			if (tunnel_mode)
10813055Sdanmcd 				itp->itp_flags |= active ? ITPF_P_TUNNEL :
10823055Sdanmcd 				    ITPF_I_TUNNEL;
10833055Sdanmcd 		}
10843055Sdanmcd 	} else {
10853055Sdanmcd 		empty_itp = B_FALSE;
10863055Sdanmcd 	}
10873055Sdanmcd 
10880Sstevel@tonic-gate 	if (rule->spd_rule_index != 0) {
10893055Sdanmcd 		diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX;
10903055Sdanmcd 		error = EINVAL;
10913055Sdanmcd 		goto fail2;
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
10953055Sdanmcd 		error = EINVAL;
10963055Sdanmcd 		goto fail2;
10973055Sdanmcd 	}
10983055Sdanmcd 
10993055Sdanmcd 	if (itp != NULL) {
11003055Sdanmcd 		if (tunnel_mode) {
11013055Sdanmcd 			if (sel.ipsl_valid &
11023055Sdanmcd 			    (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) {
11033055Sdanmcd 				itp->itp_flags |= active ?
11043055Sdanmcd 				    ITPF_P_PER_PORT_SECURITY :
11053055Sdanmcd 				    ITPF_I_PER_PORT_SECURITY;
11063055Sdanmcd 			}
11073055Sdanmcd 		} else {
11083055Sdanmcd 			/*
11093055Sdanmcd 			 * For now, we don't allow transport-mode on a tunnel
11103055Sdanmcd 			 * with ANY specific selectors.  Bail if we have such
11113055Sdanmcd 			 * a request.
11123055Sdanmcd 			 */
11133055Sdanmcd 			if (sel.ipsl_valid & IPSL_WILDCARD) {
11143055Sdanmcd 				diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS;
11153055Sdanmcd 				error = EINVAL;
11163055Sdanmcd 				goto fail2;
11173055Sdanmcd 			}
11183055Sdanmcd 		}
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11213448Sdh155122 	if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) {
11223055Sdanmcd 		error = EINVAL;
11233055Sdanmcd 		goto fail2;
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 	/*
11260Sstevel@tonic-gate 	 * If no addresses were specified, add both.
11270Sstevel@tonic-gate 	 */
11280Sstevel@tonic-gate 	afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4);
11290Sstevel@tonic-gate 	if (afs == 0)
11300Sstevel@tonic-gate 		afs = (IPSL_IPV6|IPSL_IPV4);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_WRITER);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) {
11350Sstevel@tonic-gate 		error = mkrulepair(iph, rule, &sel, actp, nact,
11363448Sdh155122 		    IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds);
11370Sstevel@tonic-gate 		if (error != 0)
11380Sstevel@tonic-gate 			goto fail;
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) {
11420Sstevel@tonic-gate 		error = mkrulepair(iph, rule, &sel, actp, nact,
11433448Sdh155122 		    IPSEC_TYPE_INBOUND, afs, &rulep, index, spds);
11440Sstevel@tonic-gate 		if (error != 0)
11450Sstevel@tonic-gate 			goto fail;
11460Sstevel@tonic-gate 	}
11470Sstevel@tonic-gate 
11483448Sdh155122 	while ((--rulep) >= &rules[0]) {
11493448Sdh155122 		ipsec_enter_policy(iph, rulep->pol, rulep->dir,
11503448Sdh155122 		    spds->spds_netstack);
11513448Sdh155122 	}
11520Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
11533055Sdanmcd 	if (itp != NULL)
11543055Sdanmcd 		mutex_exit(&itp->itp_lock);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	ipsec_actvec_free(actp, nact);
11570Sstevel@tonic-gate 	spd_echo(q, mp);
115811861SMarek.Pospisil@Sun.COM 	if (auditing) {
11594307Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
11608778SErik.Nordmark@Sun.COM 		cred_t *cr;
11618778SErik.Nordmark@Sun.COM 		pid_t cpid;
11628778SErik.Nordmark@Sun.COM 
11638778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
11644307Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
11658778SErik.Nordmark@Sun.COM 		audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
11668778SErik.Nordmark@Sun.COM 		    ITP_NAME(itp), active, 0, cpid);
11674307Spwernau 	}
11680Sstevel@tonic-gate 	return;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate fail:
11710Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
117211042SErik.Nordmark@Sun.COM 	while ((--rulep) >= &rules[0])
117311042SErik.Nordmark@Sun.COM 		IPPOL_REFRELE(rulep->pol);
11740Sstevel@tonic-gate 	ipsec_actvec_free(actp, nact);
11753055Sdanmcd fail2:
11763055Sdanmcd 	if (itp != NULL) {
11773055Sdanmcd 		if (empty_itp)
11783055Sdanmcd 			itp->itp_flags = 0;
11793055Sdanmcd 		mutex_exit(&itp->itp_lock);
11803055Sdanmcd 	}
11813055Sdanmcd 	spdsock_error(q, mp, error, diag);
118211861SMarek.Pospisil@Sun.COM 	if (auditing) {
11834307Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
11848778SErik.Nordmark@Sun.COM 		cred_t *cr;
11858778SErik.Nordmark@Sun.COM 		pid_t cpid;
11868778SErik.Nordmark@Sun.COM 
11878778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
11884307Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
11898778SErik.Nordmark@Sun.COM 		audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
11908778SErik.Nordmark@Sun.COM 		    ITP_NAME(itp), active, error, cpid);
11914307Spwernau 	}
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate void
spdsock_deleterule(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp,spd_ext_t ** extv,ipsec_tun_pol_t * itp)11953055Sdanmcd spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
11963055Sdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate 	ipsec_selkey_t sel;
11990Sstevel@tonic-gate 	struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
12003055Sdanmcd 	int err, diag = 0;
12013448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
12026430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
120311861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	if (rule == NULL) {
12060Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
120711861SMarek.Pospisil@Sun.COM 		if (auditing) {
12084307Spwernau 			boolean_t active;
12094307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
12108778SErik.Nordmark@Sun.COM 			cred_t *cr;
12118778SErik.Nordmark@Sun.COM 			pid_t cpid;
12128778SErik.Nordmark@Sun.COM 
12138778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
12144307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
12158778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_DELETERULE, cr, ns,
12166430Sdanmcd 			    ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT,
12178778SErik.Nordmark@Sun.COM 			    cpid);
12184307Spwernau 		}
12190Sstevel@tonic-gate 		return;
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 
12223055Sdanmcd 	/*
12233055Sdanmcd 	 * Must enter itp_lock first to avoid deadlock.  See tun.c's
12243055Sdanmcd 	 * set_sec_simple() for the other case of itp_lock and iph_lock.
12253055Sdanmcd 	 */
12263055Sdanmcd 	if (itp != NULL)
12273055Sdanmcd 		mutex_enter(&itp->itp_lock);
12283055Sdanmcd 
12290Sstevel@tonic-gate 	if (rule->spd_rule_index != 0) {
12306430Sdanmcd 		if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) !=
12316430Sdanmcd 		    0) {
12323055Sdanmcd 			err = ESRCH;
12333055Sdanmcd 			goto fail;
12340Sstevel@tonic-gate 		}
12350Sstevel@tonic-gate 	} else {
12360Sstevel@tonic-gate 		if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
12373055Sdanmcd 			err = EINVAL;	/* diag already set... */
12383055Sdanmcd 			goto fail;
12393055Sdanmcd 		}
12403055Sdanmcd 
12413055Sdanmcd 		if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) &&
12426430Sdanmcd 		    !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) {
12433055Sdanmcd 			err = ESRCH;
12443055Sdanmcd 			goto fail;
12453055Sdanmcd 		}
12463055Sdanmcd 
12473055Sdanmcd 		if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) &&
12486430Sdanmcd 		    !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) {
12493055Sdanmcd 			err = ESRCH;
12503055Sdanmcd 			goto fail;
12510Sstevel@tonic-gate 		}
12523055Sdanmcd 	}
12533055Sdanmcd 
12543055Sdanmcd 	if (itp != NULL) {
12553055Sdanmcd 		ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive);
12563055Sdanmcd 		rw_enter(&iph->iph_lock, RW_READER);
12573055Sdanmcd 		if (avl_numnodes(&iph->iph_rulebyid) == 0) {
12583055Sdanmcd 			if (iph == itp->itp_policy)
12593055Sdanmcd 				itp->itp_flags &= ~ITPF_PFLAGS;
12603055Sdanmcd 			else
12613055Sdanmcd 				itp->itp_flags &= ~ITPF_IFLAGS;
12620Sstevel@tonic-gate 		}
12633055Sdanmcd 		/* Can exit locks in any order. */
12643055Sdanmcd 		rw_exit(&iph->iph_lock);
12653055Sdanmcd 		mutex_exit(&itp->itp_lock);
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 	spd_echo(q, mp);
126811861SMarek.Pospisil@Sun.COM 	if (auditing) {
12694307Spwernau 		boolean_t active;
12704307Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
12718778SErik.Nordmark@Sun.COM 		cred_t *cr;
12728778SErik.Nordmark@Sun.COM 		pid_t cpid;
12738778SErik.Nordmark@Sun.COM 
12748778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
12754307Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
12768778SErik.Nordmark@Sun.COM 		audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
12778778SErik.Nordmark@Sun.COM 		    active, 0, cpid);
12784307Spwernau 	}
12790Sstevel@tonic-gate 	return;
12800Sstevel@tonic-gate fail:
12813055Sdanmcd 	if (itp != NULL)
12823055Sdanmcd 		mutex_exit(&itp->itp_lock);
12833055Sdanmcd 	spdsock_error(q, mp, err, diag);
128411861SMarek.Pospisil@Sun.COM 	if (auditing) {
12854307Spwernau 		boolean_t active;
12864307Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
12878778SErik.Nordmark@Sun.COM 		cred_t *cr;
12888778SErik.Nordmark@Sun.COM 		pid_t cpid;
12898778SErik.Nordmark@Sun.COM 
12908778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
12914307Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
12928778SErik.Nordmark@Sun.COM 		audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
12938778SErik.Nordmark@Sun.COM 		    active, err, cpid);
12944307Spwernau 	}
12953055Sdanmcd }
12963055Sdanmcd 
12973055Sdanmcd /* Do NOT consume a reference to itp. */
12983055Sdanmcd /* ARGSUSED */
12993055Sdanmcd static void
spdsock_flip_node(ipsec_tun_pol_t * itp,void * ignoreme,netstack_t * ns)13003448Sdh155122 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns)
13013055Sdanmcd {
13023055Sdanmcd 	mutex_enter(&itp->itp_lock);
13033055Sdanmcd 	ITPF_SWAP(itp->itp_flags);
13043448Sdh155122 	ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns);
13053055Sdanmcd 	mutex_exit(&itp->itp_lock);
130611456Sdanmcd@sun.com 	/* SPD_FLIP is worth a tunnel MTU check. */
130711456Sdanmcd@sun.com 	update_iptun_policy(itp);
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate void
spdsock_flip(queue_t * q,mblk_t * mp,spd_if_t * tunname)13113055Sdanmcd spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname)
13120Sstevel@tonic-gate {
13133055Sdanmcd 	char *tname;
13143055Sdanmcd 	ipsec_tun_pol_t *itp;
13153448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
13166430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
131711861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
13183055Sdanmcd 
13193055Sdanmcd 	if (tunname != NULL) {
13203055Sdanmcd 		tname = (char *)tunname->spd_if_name;
13213055Sdanmcd 		if (*tname == '\0') {
13223448Sdh155122 			/* can't fail */
13236430Sdanmcd 			ipsec_swap_global_policy(ns);
132411861SMarek.Pospisil@Sun.COM 			if (auditing) {
13254307Spwernau 				boolean_t active;
13264307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
13278778SErik.Nordmark@Sun.COM 				cred_t *cr;
13288778SErik.Nordmark@Sun.COM 				pid_t cpid;
13298778SErik.Nordmark@Sun.COM 
13308778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
13314307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
13328778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_FLIP, cr, ns,
13338778SErik.Nordmark@Sun.COM 				    NULL, active, 0, cpid);
13344307Spwernau 			}
13356430Sdanmcd 			itp_walk(spdsock_flip_node, NULL, ns);
133611861SMarek.Pospisil@Sun.COM 			if (auditing) {
13374307Spwernau 				boolean_t active;
13384307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
13398778SErik.Nordmark@Sun.COM 				cred_t *cr;
13408778SErik.Nordmark@Sun.COM 				pid_t cpid;
13418778SErik.Nordmark@Sun.COM 
13428778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
13434307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
13448778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_FLIP, cr, ns,
13458778SErik.Nordmark@Sun.COM 				    "all tunnels", active, 0, cpid);
13464307Spwernau 			}
13473055Sdanmcd 		} else {
13486430Sdanmcd 			itp = get_tunnel_policy(tname, ns);
13493055Sdanmcd 			if (itp == NULL) {
13503055Sdanmcd 				/* Better idea for "tunnel not found"? */
13513055Sdanmcd 				spdsock_error(q, mp, ESRCH, 0);
135211861SMarek.Pospisil@Sun.COM 				if (auditing) {
13534307Spwernau 					boolean_t active;
13544307Spwernau 					spd_msg_t *spmsg =
13554307Spwernau 					    (spd_msg_t *)mp->b_rptr;
13568778SErik.Nordmark@Sun.COM 					cred_t *cr;
13578778SErik.Nordmark@Sun.COM 					pid_t cpid;
13588778SErik.Nordmark@Sun.COM 
13598778SErik.Nordmark@Sun.COM 					cr = msg_getcred(mp, &cpid);
13604307Spwernau 					active = (spmsg->spd_msg_spdid ==
13614307Spwernau 					    SPD_ACTIVE);
13628778SErik.Nordmark@Sun.COM 					audit_pf_policy(SPD_FLIP, cr, ns,
13638778SErik.Nordmark@Sun.COM 					    ITP_NAME(itp), active,
13648778SErik.Nordmark@Sun.COM 					    ESRCH, cpid);
13654307Spwernau 				}
13663055Sdanmcd 				return;
13673055Sdanmcd 			}
136811456Sdanmcd@sun.com 			spdsock_flip_node(itp, NULL, ns);
136911861SMarek.Pospisil@Sun.COM 			if (auditing) {
13704307Spwernau 				boolean_t active;
13714307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
13728778SErik.Nordmark@Sun.COM 				cred_t *cr;
13738778SErik.Nordmark@Sun.COM 				pid_t cpid;
13748778SErik.Nordmark@Sun.COM 
13758778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
13764307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
13778778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_FLIP, cr, ns,
13788778SErik.Nordmark@Sun.COM 				    ITP_NAME(itp), active, 0, cpid);
13794307Spwernau 			}
13806430Sdanmcd 			ITP_REFRELE(itp, ns);
13813055Sdanmcd 		}
13823055Sdanmcd 	} else {
13836430Sdanmcd 		ipsec_swap_global_policy(ns);	/* can't fail */
138411861SMarek.Pospisil@Sun.COM 		if (auditing) {
13854307Spwernau 			boolean_t active;
13864307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
13878778SErik.Nordmark@Sun.COM 			cred_t *cr;
13888778SErik.Nordmark@Sun.COM 			pid_t cpid;
13898778SErik.Nordmark@Sun.COM 
13908778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
13914307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
13928778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_FLIP, cr,
13938778SErik.Nordmark@Sun.COM 			    ns, NULL, active, 0, cpid);
13944307Spwernau 		}
13953055Sdanmcd 	}
13960Sstevel@tonic-gate 	spd_echo(q, mp);
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate /*
14000Sstevel@tonic-gate  * Unimplemented feature
14010Sstevel@tonic-gate  */
14020Sstevel@tonic-gate /* ARGSUSED */
14030Sstevel@tonic-gate static void
spdsock_lookup(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp,spd_ext_t ** extv,ipsec_tun_pol_t * itp)14043055Sdanmcd spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
14053055Sdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
14060Sstevel@tonic-gate {
14070Sstevel@tonic-gate 	spdsock_error(q, mp, EINVAL, 0);
14080Sstevel@tonic-gate }
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate static mblk_t *
spdsock_dump_ruleset(mblk_t * req,ipsec_policy_head_t * iph,uint32_t count,uint16_t error)14120Sstevel@tonic-gate spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph,
14130Sstevel@tonic-gate     uint32_t count, uint16_t error)
14140Sstevel@tonic-gate {
14150Sstevel@tonic-gate 	size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t);
14160Sstevel@tonic-gate 	spd_msg_t *msg;
14170Sstevel@tonic-gate 	spd_ruleset_ext_t *ruleset;
14180Sstevel@tonic-gate 	mblk_t *m = allocb(len, BPRI_HI);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&iph->iph_lock));
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	if (m == NULL) {
14230Sstevel@tonic-gate 		return (NULL);
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 	msg = (spd_msg_t *)m->b_rptr;
14260Sstevel@tonic-gate 	ruleset = (spd_ruleset_ext_t *)(&msg[1]);
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	m->b_wptr = (uint8_t *)&ruleset[1];
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	*msg = *(spd_msg_t *)(req->b_rptr);
14310Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(len);
14320Sstevel@tonic-gate 	msg->spd_msg_errno = error;
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset));
14350Sstevel@tonic-gate 	ruleset->spd_ruleset_type = SPD_EXT_RULESET;
14360Sstevel@tonic-gate 	ruleset->spd_ruleset_count = count;
14370Sstevel@tonic-gate 	ruleset->spd_ruleset_version = iph->iph_gen;
14380Sstevel@tonic-gate 	return (m);
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate static mblk_t *
spdsock_dump_finish(spdsock_t * ss,int error)14420Sstevel@tonic-gate spdsock_dump_finish(spdsock_t *ss, int error)
14430Sstevel@tonic-gate {
14440Sstevel@tonic-gate 	mblk_t *m;
14450Sstevel@tonic-gate 	ipsec_policy_head_t *iph = ss->spdsock_dump_head;
14460Sstevel@tonic-gate 	mblk_t *req = ss->spdsock_dump_req;
144710592Sdanmcd@sun.com 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
14500Sstevel@tonic-gate 	m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error);
14510Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
145210592Sdanmcd@sun.com 	IPPH_REFRELE(iph, ns);
145310592Sdanmcd@sun.com 	if (ss->spdsock_itp != NULL) {
145410592Sdanmcd@sun.com 		ITP_REFRELE(ss->spdsock_itp, ns);
145510592Sdanmcd@sun.com 		ss->spdsock_itp = NULL;
145610592Sdanmcd@sun.com 	}
14570Sstevel@tonic-gate 	ss->spdsock_dump_req = NULL;
14580Sstevel@tonic-gate 	freemsg(req);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	return (m);
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate /*
14640Sstevel@tonic-gate  * Rule encoding functions.
14650Sstevel@tonic-gate  * We do a two-pass encode.
14660Sstevel@tonic-gate  * If base != NULL, fill in encoded rule part starting at base+offset.
14670Sstevel@tonic-gate  * Always return "offset" plus length of to-be-encoded data.
14680Sstevel@tonic-gate  */
14690Sstevel@tonic-gate static uint_t
spdsock_encode_typecode(uint8_t * base,uint_t offset,uint8_t type,uint8_t type_end,uint8_t code,uint8_t code_end)14700Sstevel@tonic-gate spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type,
14710Sstevel@tonic-gate     uint8_t type_end, uint8_t code, uint8_t code_end)
14720Sstevel@tonic-gate {
14730Sstevel@tonic-gate 	struct spd_typecode *tcp;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	if (base != NULL) {
14780Sstevel@tonic-gate 		tcp = (struct spd_typecode *)(base + offset);
14790Sstevel@tonic-gate 		tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp));
14800Sstevel@tonic-gate 		tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE;
14810Sstevel@tonic-gate 		tcp->spd_typecode_code = code;
14820Sstevel@tonic-gate 		tcp->spd_typecode_type = type;
14830Sstevel@tonic-gate 		tcp->spd_typecode_type_end = type_end;
14840Sstevel@tonic-gate 		tcp->spd_typecode_code_end = code_end;
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate 	offset += sizeof (*tcp);
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	return (offset);
14910Sstevel@tonic-gate }
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate static uint_t
spdsock_encode_proto(uint8_t * base,uint_t offset,uint8_t proto)14940Sstevel@tonic-gate spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto)
14950Sstevel@tonic-gate {
14960Sstevel@tonic-gate 	struct spd_proto *spp;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	if (base != NULL) {
15010Sstevel@tonic-gate 		spp = (struct spd_proto *)(base + offset);
15020Sstevel@tonic-gate 		spp->spd_proto_len = SPD_8TO64(sizeof (*spp));
15030Sstevel@tonic-gate 		spp->spd_proto_exttype = SPD_EXT_PROTO;
15040Sstevel@tonic-gate 		spp->spd_proto_number = proto;
15050Sstevel@tonic-gate 		spp->spd_proto_reserved1 = 0;
15060Sstevel@tonic-gate 		spp->spd_proto_reserved2 = 0;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 	offset += sizeof (*spp);
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	return (offset);
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate static uint_t
spdsock_encode_port(uint8_t * base,uint_t offset,uint16_t ext,uint16_t port)15160Sstevel@tonic-gate spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port)
15170Sstevel@tonic-gate {
15180Sstevel@tonic-gate 	struct spd_portrange *spp;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	if (base != NULL) {
15230Sstevel@tonic-gate 		spp = (struct spd_portrange *)(base + offset);
15240Sstevel@tonic-gate 		spp->spd_ports_len = SPD_8TO64(sizeof (*spp));
15250Sstevel@tonic-gate 		spp->spd_ports_exttype = ext;
15260Sstevel@tonic-gate 		spp->spd_ports_minport = port;
15270Sstevel@tonic-gate 		spp->spd_ports_maxport = port;
15280Sstevel@tonic-gate 	}
15290Sstevel@tonic-gate 	offset += sizeof (*spp);
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	return (offset);
15340Sstevel@tonic-gate }
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate static uint_t
spdsock_encode_addr(uint8_t * base,uint_t offset,uint16_t ext,const ipsec_selkey_t * sel,const ipsec_addr_t * addr,uint_t pfxlen)15370Sstevel@tonic-gate spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext,
15380Sstevel@tonic-gate     const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen)
15390Sstevel@tonic-gate {
15400Sstevel@tonic-gate 	struct spd_address *sae;
15410Sstevel@tonic-gate 	ipsec_addr_t *spdaddr;
15420Sstevel@tonic-gate 	uint_t start = offset;
15430Sstevel@tonic-gate 	uint_t addrlen;
15440Sstevel@tonic-gate 	uint_t af;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	if (sel->ipsl_valid & IPSL_IPV4) {
15470Sstevel@tonic-gate 		af = AF_INET;
15480Sstevel@tonic-gate 		addrlen = IP_ADDR_LEN;
15490Sstevel@tonic-gate 	} else {
15500Sstevel@tonic-gate 		af = AF_INET6;
15510Sstevel@tonic-gate 		addrlen = IPV6_ADDR_LEN;
15520Sstevel@tonic-gate 	}
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	if (base != NULL) {
15570Sstevel@tonic-gate 		sae = (struct spd_address *)(base + offset);
15580Sstevel@tonic-gate 		sae->spd_address_exttype = ext;
15590Sstevel@tonic-gate 		sae->spd_address_af = af;
15600Sstevel@tonic-gate 		sae->spd_address_prefixlen = pfxlen;
15610Sstevel@tonic-gate 		sae->spd_address_reserved2 = 0;
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 		spdaddr = (ipsec_addr_t *)(&sae[1]);
15640Sstevel@tonic-gate 		bcopy(addr, spdaddr, addrlen);
15650Sstevel@tonic-gate 	}
15660Sstevel@tonic-gate 	offset += sizeof (*sae);
15670Sstevel@tonic-gate 	addrlen = roundup(addrlen, sizeof (uint64_t));
15680Sstevel@tonic-gate 	offset += addrlen;
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	if (base != NULL)
15730Sstevel@tonic-gate 		sae->spd_address_len = SPD_8TO64(offset - start);
15740Sstevel@tonic-gate 	return (offset);
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate static uint_t
spdsock_encode_sel(uint8_t * base,uint_t offset,const ipsec_sel_t * sel)15780Sstevel@tonic-gate spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel)
15790Sstevel@tonic-gate {
15800Sstevel@tonic-gate 	const ipsec_selkey_t *selkey = &sel->ipsl_key;
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_PROTOCOL)
15830Sstevel@tonic-gate 		offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto);
15840Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_LOCAL_PORT)
15850Sstevel@tonic-gate 		offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT,
15860Sstevel@tonic-gate 		    selkey->ipsl_lport);
15870Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_REMOTE_PORT)
15880Sstevel@tonic-gate 		offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT,
15890Sstevel@tonic-gate 		    selkey->ipsl_rport);
15900Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_REMOTE_ADDR)
15910Sstevel@tonic-gate 		offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR,
15920Sstevel@tonic-gate 		    selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen);
15930Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_LOCAL_ADDR)
15940Sstevel@tonic-gate 		offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR,
15950Sstevel@tonic-gate 		    selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen);
15960Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_ICMP_TYPE) {
15970Sstevel@tonic-gate 		offset = spdsock_encode_typecode(base, offset,
15980Sstevel@tonic-gate 		    selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end,
15990Sstevel@tonic-gate 		    (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
16005240Snordmark 		    selkey->ipsl_icmp_code : 255,
16010Sstevel@tonic-gate 		    (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
16025240Snordmark 		    selkey->ipsl_icmp_code_end : 255);
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 	return (offset);
16050Sstevel@tonic-gate }
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate static uint_t
spdsock_encode_actattr(uint8_t * base,uint_t offset,uint32_t tag,uint32_t value)16080Sstevel@tonic-gate spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag,
16090Sstevel@tonic-gate     uint32_t value)
16100Sstevel@tonic-gate {
16110Sstevel@tonic-gate 	struct spd_attribute *attr;
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	if (base != NULL) {
16160Sstevel@tonic-gate 		attr = (struct spd_attribute *)(base + offset);
16170Sstevel@tonic-gate 		attr->spd_attr_tag = tag;
16180Sstevel@tonic-gate 		attr->spd_attr_value = value;
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 	offset += sizeof (struct spd_attribute);
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	return (offset);
16250Sstevel@tonic-gate }
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate #define	EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v))
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate static uint_t
spdsock_encode_action(uint8_t * base,uint_t offset,const ipsec_action_t * ap)16310Sstevel@tonic-gate spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap)
16320Sstevel@tonic-gate {
16330Sstevel@tonic-gate 	const struct ipsec_act *act = &(ap->ipa_act);
16340Sstevel@tonic-gate 	uint_t flags;
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	EMIT(SPD_ATTR_EMPTY, 0);
16370Sstevel@tonic-gate 	switch (act->ipa_type) {
16380Sstevel@tonic-gate 	case IPSEC_ACT_DISCARD:
16390Sstevel@tonic-gate 	case IPSEC_ACT_REJECT:
16400Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP);
16410Sstevel@tonic-gate 		break;
16420Sstevel@tonic-gate 	case IPSEC_ACT_BYPASS:
16430Sstevel@tonic-gate 	case IPSEC_ACT_CLEAR:
16440Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS);
16450Sstevel@tonic-gate 		break;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	case IPSEC_ACT_APPLY:
16480Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC);
16490Sstevel@tonic-gate 		flags = 0;
16500Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_ah)
16510Sstevel@tonic-gate 			flags |= SPD_APPLY_AH;
16520Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_esp)
16530Sstevel@tonic-gate 			flags |= SPD_APPLY_ESP;
16540Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_espa)
16550Sstevel@tonic-gate 			flags |= SPD_APPLY_ESPA;
16560Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_se)
16570Sstevel@tonic-gate 			flags |= SPD_APPLY_SE;
16580Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_unique)
16590Sstevel@tonic-gate 			flags |= SPD_APPLY_UNIQUE;
16600Sstevel@tonic-gate 		EMIT(SPD_ATTR_FLAGS, flags);
16610Sstevel@tonic-gate 		if (flags & SPD_APPLY_AH) {
16620Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg);
16630Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_MINBITS,
16640Sstevel@tonic-gate 			    act->ipa_apply.ipp_ah_minbits);
16650Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_MAXBITS,
16660Sstevel@tonic-gate 			    act->ipa_apply.ipp_ah_maxbits);
16670Sstevel@tonic-gate 		}
16680Sstevel@tonic-gate 		if (flags & SPD_APPLY_ESP) {
16690Sstevel@tonic-gate 			EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg);
16700Sstevel@tonic-gate 			EMIT(SPD_ATTR_ENCR_MINBITS,
16710Sstevel@tonic-gate 			    act->ipa_apply.ipp_espe_minbits);
16720Sstevel@tonic-gate 			EMIT(SPD_ATTR_ENCR_MAXBITS,
16730Sstevel@tonic-gate 			    act->ipa_apply.ipp_espe_maxbits);
16740Sstevel@tonic-gate 			if (flags & SPD_APPLY_ESPA) {
16750Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESP_AUTH,
16760Sstevel@tonic-gate 				    act->ipa_apply.ipp_esp_auth_alg);
16770Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESPA_MINBITS,
16780Sstevel@tonic-gate 				    act->ipa_apply.ipp_espa_minbits);
16790Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESPA_MAXBITS,
16800Sstevel@tonic-gate 				    act->ipa_apply.ipp_espa_maxbits);
16810Sstevel@tonic-gate 			}
16820Sstevel@tonic-gate 		}
16830Sstevel@tonic-gate 		if (act->ipa_apply.ipp_km_proto != 0)
16840Sstevel@tonic-gate 			EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto);
16850Sstevel@tonic-gate 		if (act->ipa_apply.ipp_km_cookie != 0)
16860Sstevel@tonic-gate 			EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie);
16870Sstevel@tonic-gate 		if (act->ipa_apply.ipp_replay_depth != 0)
16880Sstevel@tonic-gate 			EMIT(SPD_ATTR_REPLAY_DEPTH,
16890Sstevel@tonic-gate 			    act->ipa_apply.ipp_replay_depth);
16900Sstevel@tonic-gate 		/* Add more here */
16910Sstevel@tonic-gate 		break;
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	return (offset);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate static uint_t
spdsock_encode_action_list(uint8_t * base,uint_t offset,const ipsec_action_t * ap)16980Sstevel@tonic-gate spdsock_encode_action_list(uint8_t *base, uint_t offset,
16990Sstevel@tonic-gate     const ipsec_action_t *ap)
17000Sstevel@tonic-gate {
17010Sstevel@tonic-gate 	struct spd_ext_actions *act;
17020Sstevel@tonic-gate 	uint_t nact = 0;
17030Sstevel@tonic-gate 	uint_t start = offset;
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	if (base != NULL) {
17080Sstevel@tonic-gate 		act = (struct spd_ext_actions *)(base + offset);
17090Sstevel@tonic-gate 		act->spd_actions_len = 0;
17100Sstevel@tonic-gate 		act->spd_actions_exttype = SPD_EXT_ACTION;
17110Sstevel@tonic-gate 		act->spd_actions_count = 0;
17120Sstevel@tonic-gate 		act->spd_actions_reserved = 0;
17130Sstevel@tonic-gate 	}
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	offset += sizeof (*act);
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 	while (ap != NULL) {
17200Sstevel@tonic-gate 		offset = spdsock_encode_action(base, offset, ap);
17210Sstevel@tonic-gate 		ap = ap->ipa_next;
17220Sstevel@tonic-gate 		nact++;
17230Sstevel@tonic-gate 		if (ap != NULL) {
17240Sstevel@tonic-gate 			EMIT(SPD_ATTR_NEXT, 0);
17250Sstevel@tonic-gate 		}
17260Sstevel@tonic-gate 	}
17270Sstevel@tonic-gate 	EMIT(SPD_ATTR_END, 0);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	if (base != NULL) {
17320Sstevel@tonic-gate 		act->spd_actions_count = nact;
17330Sstevel@tonic-gate 		act->spd_actions_len = SPD_8TO64(offset - start);
17340Sstevel@tonic-gate 	}
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	return (offset);
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate #undef EMIT
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate /* ARGSUSED */
17420Sstevel@tonic-gate static uint_t
spdsock_rule_flags(uint_t dir,uint_t af)17430Sstevel@tonic-gate spdsock_rule_flags(uint_t dir, uint_t af)
17440Sstevel@tonic-gate {
17450Sstevel@tonic-gate 	uint_t flags = 0;
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	if (dir == IPSEC_TYPE_INBOUND)
17480Sstevel@tonic-gate 		flags |= SPD_RULE_FLAG_INBOUND;
17490Sstevel@tonic-gate 	if (dir == IPSEC_TYPE_OUTBOUND)
17500Sstevel@tonic-gate 		flags |= SPD_RULE_FLAG_OUTBOUND;
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	return (flags);
17530Sstevel@tonic-gate }
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate static uint_t
spdsock_encode_rule_head(uint8_t * base,uint_t offset,spd_msg_t * req,const ipsec_policy_t * rule,uint_t dir,uint_t af,char * name,boolean_t tunnel)17573055Sdanmcd spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req,
17583055Sdanmcd     const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name,
17593055Sdanmcd     boolean_t tunnel)
17600Sstevel@tonic-gate {
17610Sstevel@tonic-gate 	struct spd_msg *spmsg;
17620Sstevel@tonic-gate 	struct spd_rule *spr;
17633055Sdanmcd 	spd_if_t *sid;
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	uint_t start = offset;
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	if (base != NULL) {
17700Sstevel@tonic-gate 		spmsg = (struct spd_msg *)(base + offset);
17710Sstevel@tonic-gate 		bzero(spmsg, sizeof (*spmsg));
17720Sstevel@tonic-gate 		spmsg->spd_msg_version = PF_POLICY_V1;
17730Sstevel@tonic-gate 		spmsg->spd_msg_type = SPD_DUMP;
17740Sstevel@tonic-gate 		spmsg->spd_msg_seq = req->spd_msg_seq;
17750Sstevel@tonic-gate 		spmsg->spd_msg_pid = req->spd_msg_pid;
17760Sstevel@tonic-gate 	}
17770Sstevel@tonic-gate 	offset += sizeof (struct spd_msg);
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	if (base != NULL) {
17820Sstevel@tonic-gate 		spr = (struct spd_rule *)(base + offset);
17830Sstevel@tonic-gate 		spr->spd_rule_type = SPD_EXT_RULE;
17840Sstevel@tonic-gate 		spr->spd_rule_priority = rule->ipsp_prio;
17850Sstevel@tonic-gate 		spr->spd_rule_flags = spdsock_rule_flags(dir, af);
17863055Sdanmcd 		if (tunnel)
17873055Sdanmcd 			spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL;
17880Sstevel@tonic-gate 		spr->spd_rule_unused = 0;
17890Sstevel@tonic-gate 		spr->spd_rule_len = SPD_8TO64(sizeof (*spr));
17900Sstevel@tonic-gate 		spr->spd_rule_index = rule->ipsp_index;
17910Sstevel@tonic-gate 	}
17920Sstevel@tonic-gate 	offset += sizeof (struct spd_rule);
17933055Sdanmcd 
17943055Sdanmcd 	/*
17953055Sdanmcd 	 * If we have an interface name (i.e. if this policy head came from
17963055Sdanmcd 	 * a tunnel), add the SPD_EXT_TUN_NAME extension.
17973055Sdanmcd 	 */
17986466Sdanmcd 	if (name != NULL) {
17993055Sdanmcd 
18003055Sdanmcd 		ASSERT(ALIGNED64(offset));
18013055Sdanmcd 
18023055Sdanmcd 		if (base != NULL) {
18033055Sdanmcd 			sid = (spd_if_t *)(base + offset);
18043055Sdanmcd 			sid->spd_if_exttype = SPD_EXT_TUN_NAME;
18053055Sdanmcd 			sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) +
18063055Sdanmcd 			    roundup((strlen(name) - 4), 8));
18073055Sdanmcd 			(void) strlcpy((char *)sid->spd_if_name, name,
18083055Sdanmcd 			    LIFNAMSIZ);
18093055Sdanmcd 		}
18103055Sdanmcd 
18113055Sdanmcd 		offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8);
18123055Sdanmcd 	}
18133055Sdanmcd 
18140Sstevel@tonic-gate 	offset = spdsock_encode_sel(base, offset, rule->ipsp_sel);
18150Sstevel@tonic-gate 	offset = spdsock_encode_action_list(base, offset, rule->ipsp_act);
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	if (base != NULL) {
18200Sstevel@tonic-gate 		spmsg->spd_msg_len = SPD_8TO64(offset - start);
18210Sstevel@tonic-gate 	}
18220Sstevel@tonic-gate 	return (offset);
18230Sstevel@tonic-gate }
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate /* ARGSUSED */
18260Sstevel@tonic-gate static mblk_t *
spdsock_encode_rule(mblk_t * req,const ipsec_policy_t * rule,uint_t dir,uint_t af,char * name,boolean_t tunnel)18270Sstevel@tonic-gate spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule,
18283055Sdanmcd     uint_t dir, uint_t af, char *name, boolean_t tunnel)
18290Sstevel@tonic-gate {
18300Sstevel@tonic-gate 	mblk_t *m;
18310Sstevel@tonic-gate 	uint_t len;
18320Sstevel@tonic-gate 	spd_msg_t *mreq = (spd_msg_t *)req->b_rptr;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 	/*
18350Sstevel@tonic-gate 	 * Figure out how much space we'll need.
18360Sstevel@tonic-gate 	 */
18373055Sdanmcd 	len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name,
18383055Sdanmcd 	    tunnel);
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 	/*
18410Sstevel@tonic-gate 	 * Allocate mblk.
18420Sstevel@tonic-gate 	 */
18430Sstevel@tonic-gate 	m = allocb(len, BPRI_HI);
18440Sstevel@tonic-gate 	if (m == NULL)
18450Sstevel@tonic-gate 		return (NULL);
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	/*
18480Sstevel@tonic-gate 	 * Fill it in..
18490Sstevel@tonic-gate 	 */
18500Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + len;
18510Sstevel@tonic-gate 	bzero(m->b_rptr, len);
18523055Sdanmcd 	(void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af,
18533055Sdanmcd 	    name, tunnel);
18540Sstevel@tonic-gate 	return (m);
18550Sstevel@tonic-gate }
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate static ipsec_policy_t *
spdsock_dump_next_in_chain(spdsock_t * ss,ipsec_policy_head_t * iph,ipsec_policy_t * cur)185881Ssommerfe spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph,
185981Ssommerfe     ipsec_policy_t *cur)
186081Ssommerfe {
186181Ssommerfe 	ASSERT(RW_READ_HELD(&iph->iph_lock));
186281Ssommerfe 
186381Ssommerfe 	ss->spdsock_dump_count++;
186481Ssommerfe 	ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next;
186581Ssommerfe 	return (cur);
186681Ssommerfe }
186781Ssommerfe 
186881Ssommerfe static ipsec_policy_t *
spdsock_dump_next_rule(spdsock_t * ss,ipsec_policy_head_t * iph)18690Sstevel@tonic-gate spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph)
18700Sstevel@tonic-gate {
18710Sstevel@tonic-gate 	ipsec_policy_t *cur;
187281Ssommerfe 	ipsec_policy_root_t *ipr;
187381Ssommerfe 	int chain, nchains, type, af;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&iph->iph_lock));
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	cur = ss->spdsock_dump_cur_rule;
18780Sstevel@tonic-gate 
187981Ssommerfe 	if (cur != NULL)
188081Ssommerfe 		return (spdsock_dump_next_in_chain(ss, iph, cur));
188181Ssommerfe 
188281Ssommerfe 	type = ss->spdsock_dump_cur_type;
188381Ssommerfe 
188481Ssommerfe next:
188581Ssommerfe 	chain = ss->spdsock_dump_cur_chain;
188681Ssommerfe 	ipr = &iph->iph_root[type];
188781Ssommerfe 	nchains = ipr->ipr_nchains;
188881Ssommerfe 
188981Ssommerfe 	while (chain < nchains) {
189081Ssommerfe 		cur = ipr->ipr_hash[chain].hash_head;
189181Ssommerfe 		chain++;
189281Ssommerfe 		if (cur != NULL) {
189381Ssommerfe 			ss->spdsock_dump_cur_chain = chain;
189481Ssommerfe 			return (spdsock_dump_next_in_chain(ss, iph, cur));
189581Ssommerfe 		}
18960Sstevel@tonic-gate 	}
189781Ssommerfe 	ss->spdsock_dump_cur_chain = nchains;
189881Ssommerfe 
189981Ssommerfe 	af = ss->spdsock_dump_cur_af;
190081Ssommerfe 	while (af < IPSEC_NAF) {
190181Ssommerfe 		cur = ipr->ipr_nonhash[af];
190281Ssommerfe 		af++;
190381Ssommerfe 		if (cur != NULL) {
190481Ssommerfe 			ss->spdsock_dump_cur_af = af;
190581Ssommerfe 			return (spdsock_dump_next_in_chain(ss, iph, cur));
190681Ssommerfe 		}
190781Ssommerfe 	}
190881Ssommerfe 
190981Ssommerfe 	type++;
191081Ssommerfe 	if (type >= IPSEC_NTYPES)
191181Ssommerfe 		return (NULL);
191281Ssommerfe 
191381Ssommerfe 	ss->spdsock_dump_cur_chain = 0;
191481Ssommerfe 	ss->spdsock_dump_cur_type = type;
191581Ssommerfe 	ss->spdsock_dump_cur_af = IPSEC_AF_V4;
191681Ssommerfe 	goto next;
191781Ssommerfe 
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate 
19206430Sdanmcd /*
19216430Sdanmcd  * If we're done with one policy head, but have more to go, we iterate through
19226430Sdanmcd  * another IPsec tunnel policy head (itp).  Return NULL if it is an error
19236430Sdanmcd  * worthy of returning EAGAIN via PF_POLICY.
19246430Sdanmcd  */
19256430Sdanmcd static ipsec_tun_pol_t *
spdsock_dump_iterate_next_tunnel(spdsock_t * ss,ipsec_stack_t * ipss)19266430Sdanmcd spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss)
19276430Sdanmcd {
19286430Sdanmcd 	ipsec_tun_pol_t *itp;
19296430Sdanmcd 
19306430Sdanmcd 	ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock));
19316430Sdanmcd 	if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) {
19326430Sdanmcd 		/* Oops, state of the tunnel polheads changed. */
19336430Sdanmcd 		itp = NULL;
19346430Sdanmcd 	} else if (ss->spdsock_itp == NULL) {
19356430Sdanmcd 		/* Just finished global, find first node. */
19366430Sdanmcd 		itp = avl_first(&ipss->ipsec_tunnel_policies);
19376430Sdanmcd 	} else {
19386430Sdanmcd 		/* We just finished current polhead, find the next one. */
19396430Sdanmcd 		itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp);
19406430Sdanmcd 	}
19416430Sdanmcd 	if (itp != NULL) {
19426430Sdanmcd 		ITP_REFHOLD(itp);
19436430Sdanmcd 	}
19446430Sdanmcd 	if (ss->spdsock_itp != NULL) {
19456430Sdanmcd 		ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack);
19466430Sdanmcd 	}
19476430Sdanmcd 	ss->spdsock_itp = itp;
19486430Sdanmcd 	return (itp);
19496430Sdanmcd }
19506430Sdanmcd 
19510Sstevel@tonic-gate static mblk_t *
spdsock_dump_next_record(spdsock_t * ss)19520Sstevel@tonic-gate spdsock_dump_next_record(spdsock_t *ss)
19530Sstevel@tonic-gate {
19540Sstevel@tonic-gate 	ipsec_policy_head_t *iph;
19550Sstevel@tonic-gate 	ipsec_policy_t *rule;
19560Sstevel@tonic-gate 	mblk_t *m;
19576430Sdanmcd 	ipsec_tun_pol_t *itp;
19586430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
19596430Sdanmcd 	ipsec_stack_t *ipss = ns->netstack_ipsec;
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	iph = ss->spdsock_dump_head;
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	ASSERT(iph != NULL);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	if (iph->iph_gen != ss->spdsock_dump_gen) {
19680Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
19690Sstevel@tonic-gate 		return (spdsock_dump_finish(ss, EAGAIN));
19700Sstevel@tonic-gate 	}
19710Sstevel@tonic-gate 
19726430Sdanmcd 	while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) {
19730Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
19746430Sdanmcd 		if (--(ss->spdsock_dump_remaining_polheads) == 0)
19756430Sdanmcd 			return (spdsock_dump_finish(ss, 0));
19766430Sdanmcd 
19776430Sdanmcd 
19786430Sdanmcd 		/*
19796430Sdanmcd 		 * If we reach here, we have more policy heads (tunnel
19806430Sdanmcd 		 * entries) to dump.  Let's reset to a new policy head
19816430Sdanmcd 		 * and get some more rules.
19826430Sdanmcd 		 *
19836430Sdanmcd 		 * An empty policy head will have spdsock_dump_next_rule()
19846430Sdanmcd 		 * return NULL, and we loop (while dropping the number of
19856430Sdanmcd 		 * remaining polheads).  If we loop to 0, we finish.  We
19866430Sdanmcd 		 * keep looping until we hit 0 or until we have a rule to
19876430Sdanmcd 		 * encode.
19886430Sdanmcd 		 *
19896430Sdanmcd 		 * NOTE:  No need for ITP_REF*() macros here as we're only
19906430Sdanmcd 		 * going after and refholding the policy head itself.
19916430Sdanmcd 		 */
19926430Sdanmcd 		rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
19936430Sdanmcd 		itp = spdsock_dump_iterate_next_tunnel(ss, ipss);
19946430Sdanmcd 		if (itp == NULL) {
19956430Sdanmcd 			rw_exit(&ipss->ipsec_tunnel_policy_lock);
19966430Sdanmcd 			return (spdsock_dump_finish(ss, EAGAIN));
19976430Sdanmcd 		}
19986430Sdanmcd 
19996430Sdanmcd 		/* Reset other spdsock_dump thingies. */
20006430Sdanmcd 		IPPH_REFRELE(ss->spdsock_dump_head, ns);
20016430Sdanmcd 		if (ss->spdsock_dump_active) {
20026430Sdanmcd 			ss->spdsock_dump_tunnel =
20036430Sdanmcd 			    itp->itp_flags & ITPF_P_TUNNEL;
20046430Sdanmcd 			iph = itp->itp_policy;
20056430Sdanmcd 		} else {
20066430Sdanmcd 			ss->spdsock_dump_tunnel =
20076430Sdanmcd 			    itp->itp_flags & ITPF_I_TUNNEL;
20086430Sdanmcd 			iph = itp->itp_inactive;
20096430Sdanmcd 		}
20106430Sdanmcd 		IPPH_REFHOLD(iph);
20116430Sdanmcd 		rw_exit(&ipss->ipsec_tunnel_policy_lock);
20126430Sdanmcd 
20136430Sdanmcd 		rw_enter(&iph->iph_lock, RW_READER);
20146430Sdanmcd 		RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20176430Sdanmcd 	m = spdsock_encode_rule(ss->spdsock_dump_req, rule,
20186430Sdanmcd 	    ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af,
20196466Sdanmcd 	    (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name,
20206466Sdanmcd 	    ss->spdsock_dump_tunnel);
20210Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 	if (m == NULL)
20240Sstevel@tonic-gate 		return (spdsock_dump_finish(ss, ENOMEM));
20250Sstevel@tonic-gate 	return (m);
20260Sstevel@tonic-gate }
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate /*
20290Sstevel@tonic-gate  * Dump records until we run into flow-control back-pressure.
20300Sstevel@tonic-gate  */
20310Sstevel@tonic-gate static void
spdsock_dump_some(queue_t * q,spdsock_t * ss)20320Sstevel@tonic-gate spdsock_dump_some(queue_t *q, spdsock_t *ss)
20330Sstevel@tonic-gate {
20340Sstevel@tonic-gate 	mblk_t *m, *dataind;
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	while ((ss->spdsock_dump_req != NULL) && canputnext(q)) {
20370Sstevel@tonic-gate 		m = spdsock_dump_next_record(ss);
20380Sstevel@tonic-gate 		if (m == NULL)
20390Sstevel@tonic-gate 			return;
20400Sstevel@tonic-gate 		dataind = allocb(sizeof (struct T_data_req), BPRI_HI);
20410Sstevel@tonic-gate 		if (dataind == NULL) {
20420Sstevel@tonic-gate 			freemsg(m);
20430Sstevel@tonic-gate 			return;
20440Sstevel@tonic-gate 		}
20450Sstevel@tonic-gate 		dataind->b_cont = m;
20460Sstevel@tonic-gate 		dataind->b_wptr += sizeof (struct T_data_req);
20470Sstevel@tonic-gate 		((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND;
20480Sstevel@tonic-gate 		((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0;
20490Sstevel@tonic-gate 		dataind->b_datap->db_type = M_PROTO;
20500Sstevel@tonic-gate 		putnext(q, dataind);
20510Sstevel@tonic-gate 	}
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate /*
20550Sstevel@tonic-gate  * Start dumping.
20560Sstevel@tonic-gate  * Format a start-of-dump record, and set up the stream and kick the rsrv
20570Sstevel@tonic-gate  * procedure to continue the job..
20580Sstevel@tonic-gate  */
20590Sstevel@tonic-gate /* ARGSUSED */
20600Sstevel@tonic-gate static void
spdsock_dump(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp)20613055Sdanmcd spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp)
20620Sstevel@tonic-gate {
20630Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
20646430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
20653448Sdh155122 	ipsec_stack_t *ipss = ns->netstack_ipsec;
20660Sstevel@tonic-gate 	mblk_t *mr;
20670Sstevel@tonic-gate 
20686430Sdanmcd 	/* spdsock_open() already set spdsock_itp to NULL. */
20693055Sdanmcd 	if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
20703448Sdh155122 		rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
20713055Sdanmcd 		ss->spdsock_dump_remaining_polheads = 1 +
20723448Sdh155122 		    avl_numnodes(&ipss->ipsec_tunnel_policies);
20733448Sdh155122 		ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen;
20743448Sdh155122 		rw_exit(&ipss->ipsec_tunnel_policy_lock);
20753055Sdanmcd 		if (iph == ALL_ACTIVE_POLHEADS) {
20763448Sdh155122 			iph = ipsec_system_policy(ns);
20773055Sdanmcd 			ss->spdsock_dump_active = B_TRUE;
20783055Sdanmcd 		} else {
20796430Sdanmcd 			iph = ipsec_inactive_policy(ns);
20803055Sdanmcd 			ss->spdsock_dump_active = B_FALSE;
20813055Sdanmcd 		}
20826430Sdanmcd 		ASSERT(ss->spdsock_itp == NULL);
20833055Sdanmcd 	} else {
20843055Sdanmcd 		ss->spdsock_dump_remaining_polheads = 1;
20853055Sdanmcd 	}
20863055Sdanmcd 
20870Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	mr = spdsock_dump_ruleset(mp, iph, 0, 0);
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 	if (!mr) {
20920Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
20930Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
20940Sstevel@tonic-gate 		return;
20950Sstevel@tonic-gate 	}
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	ss->spdsock_dump_req = mp;
20986430Sdanmcd 	RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
20996430Sdanmcd 
21000Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	qreply(q, mr);
21030Sstevel@tonic-gate 	qenable(OTHERQ(q));
21040Sstevel@tonic-gate }
21050Sstevel@tonic-gate 
21063055Sdanmcd /* Do NOT consume a reference to ITP. */
21070Sstevel@tonic-gate void
spdsock_clone_node(ipsec_tun_pol_t * itp,void * ep,netstack_t * ns)21083448Sdh155122 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns)
21093055Sdanmcd {
21103055Sdanmcd 	int *errptr = (int *)ep;
21113055Sdanmcd 
21123055Sdanmcd 	if (*errptr != 0)
21133055Sdanmcd 		return;	/* We've failed already for some reason. */
21143055Sdanmcd 	mutex_enter(&itp->itp_lock);
21153055Sdanmcd 	ITPF_CLONE(itp->itp_flags);
21163448Sdh155122 	*errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns);
21173055Sdanmcd 	mutex_exit(&itp->itp_lock);
21183055Sdanmcd }
21193055Sdanmcd 
21203055Sdanmcd void
spdsock_clone(queue_t * q,mblk_t * mp,spd_if_t * tunname)21213055Sdanmcd spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname)
21220Sstevel@tonic-gate {
21233055Sdanmcd 	int error;
21243055Sdanmcd 	char *tname;
21253055Sdanmcd 	ipsec_tun_pol_t *itp;
21263448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
21276430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
212811861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
21293055Sdanmcd 
21303055Sdanmcd 	if (tunname != NULL) {
21313055Sdanmcd 		tname = (char *)tunname->spd_if_name;
21323055Sdanmcd 		if (*tname == '\0') {
21336430Sdanmcd 			error = ipsec_clone_system_policy(ns);
213411861SMarek.Pospisil@Sun.COM 			if (auditing) {
21354307Spwernau 				boolean_t active;
21364307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
21378778SErik.Nordmark@Sun.COM 				cred_t *cr;
21388778SErik.Nordmark@Sun.COM 				pid_t cpid;
21398778SErik.Nordmark@Sun.COM 
21408778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
21414307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
21428778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_CLONE, cr, ns,
21438778SErik.Nordmark@Sun.COM 				    NULL, active, error, cpid);
21444307Spwernau 			}
21454307Spwernau 			if (error == 0) {
21466430Sdanmcd 				itp_walk(spdsock_clone_node, &error, ns);
214711861SMarek.Pospisil@Sun.COM 				if (auditing) {
21484307Spwernau 					boolean_t active;
21494307Spwernau 					spd_msg_t *spmsg =
21504307Spwernau 					    (spd_msg_t *)mp->b_rptr;
21518778SErik.Nordmark@Sun.COM 					cred_t *cr;
21528778SErik.Nordmark@Sun.COM 					pid_t cpid;
21538778SErik.Nordmark@Sun.COM 
21548778SErik.Nordmark@Sun.COM 					cr = msg_getcred(mp, &cpid);
21554307Spwernau 					active = (spmsg->spd_msg_spdid ==
21564307Spwernau 					    SPD_ACTIVE);
21578778SErik.Nordmark@Sun.COM 					audit_pf_policy(SPD_CLONE, cr,
21586430Sdanmcd 					    ns, "all tunnels", active, 0,
21598778SErik.Nordmark@Sun.COM 					    cpid);
21604307Spwernau 				}
21614307Spwernau 			}
21623055Sdanmcd 		} else {
21636430Sdanmcd 			itp = get_tunnel_policy(tname, ns);
21643055Sdanmcd 			if (itp == NULL) {
21653055Sdanmcd 				spdsock_error(q, mp, ENOENT, 0);
216611861SMarek.Pospisil@Sun.COM 				if (auditing) {
21674307Spwernau 					boolean_t active;
21684307Spwernau 					spd_msg_t *spmsg =
21694307Spwernau 					    (spd_msg_t *)mp->b_rptr;
21708778SErik.Nordmark@Sun.COM 					cred_t *cr;
21718778SErik.Nordmark@Sun.COM 					pid_t cpid;
21728778SErik.Nordmark@Sun.COM 
21738778SErik.Nordmark@Sun.COM 					cr = msg_getcred(mp, &cpid);
21744307Spwernau 					active = (spmsg->spd_msg_spdid ==
21754307Spwernau 					    SPD_ACTIVE);
21768778SErik.Nordmark@Sun.COM 					audit_pf_policy(SPD_CLONE, cr,
217710764Sdanmcd@sun.com 					    ns, NULL, active, ENOENT, cpid);
21784307Spwernau 				}
21793055Sdanmcd 				return;
21803055Sdanmcd 			}
21813448Sdh155122 			spdsock_clone_node(itp, &error, NULL);
218211861SMarek.Pospisil@Sun.COM 			if (auditing) {
21834307Spwernau 				boolean_t active;
21844307Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
21858778SErik.Nordmark@Sun.COM 				cred_t *cr;
21868778SErik.Nordmark@Sun.COM 				pid_t cpid;
21878778SErik.Nordmark@Sun.COM 
21888778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
21894307Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
21908778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_CLONE, cr, ns,
21918778SErik.Nordmark@Sun.COM 				    ITP_NAME(itp), active, error, cpid);
21924307Spwernau 			}
219310764Sdanmcd@sun.com 			ITP_REFRELE(itp, ns);
21943055Sdanmcd 		}
21953055Sdanmcd 	} else {
21966430Sdanmcd 		error = ipsec_clone_system_policy(ns);
219711861SMarek.Pospisil@Sun.COM 		if (auditing) {
21984307Spwernau 			boolean_t active;
21994307Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
22008778SErik.Nordmark@Sun.COM 			cred_t *cr;
22018778SErik.Nordmark@Sun.COM 			pid_t cpid;
22028778SErik.Nordmark@Sun.COM 
22038778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
22044307Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
22058778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_CLONE, cr, ns, NULL,
22068778SErik.Nordmark@Sun.COM 			    active, error, cpid);
22074307Spwernau 		}
22083055Sdanmcd 	}
22093055Sdanmcd 
22100Sstevel@tonic-gate 	if (error != 0)
22110Sstevel@tonic-gate 		spdsock_error(q, mp, error, 0);
22120Sstevel@tonic-gate 	else
22130Sstevel@tonic-gate 		spd_echo(q, mp);
22140Sstevel@tonic-gate }
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate /*
22170Sstevel@tonic-gate  * Process a SPD_ALGLIST request. The caller expects separate alg entries
22180Sstevel@tonic-gate  * for AH authentication, ESP authentication, and ESP encryption.
22190Sstevel@tonic-gate  * The same distinction is then used when setting the min and max key
22200Sstevel@tonic-gate  * sizes when defining policies.
22210Sstevel@tonic-gate  */
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate #define	SPDSOCK_AH_AUTH		0
22240Sstevel@tonic-gate #define	SPDSOCK_ESP_AUTH	1
22250Sstevel@tonic-gate #define	SPDSOCK_ESP_ENCR	2
22260Sstevel@tonic-gate #define	SPDSOCK_NTYPES		3
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate static const uint_t algattr[SPDSOCK_NTYPES] = {
22290Sstevel@tonic-gate 	SPD_ATTR_AH_AUTH,
22300Sstevel@tonic-gate 	SPD_ATTR_ESP_AUTH,
22310Sstevel@tonic-gate 	SPD_ATTR_ESP_ENCR
22320Sstevel@tonic-gate };
22330Sstevel@tonic-gate static const uint_t minbitsattr[SPDSOCK_NTYPES] = {
22340Sstevel@tonic-gate 	SPD_ATTR_AH_MINBITS,
22350Sstevel@tonic-gate 	SPD_ATTR_ESPA_MINBITS,
22360Sstevel@tonic-gate 	SPD_ATTR_ENCR_MINBITS
22370Sstevel@tonic-gate };
22380Sstevel@tonic-gate static const uint_t maxbitsattr[SPDSOCK_NTYPES] = {
22390Sstevel@tonic-gate 	SPD_ATTR_AH_MAXBITS,
22400Sstevel@tonic-gate 	SPD_ATTR_ESPA_MAXBITS,
22410Sstevel@tonic-gate 	SPD_ATTR_ENCR_MAXBITS
22420Sstevel@tonic-gate };
22430Sstevel@tonic-gate static const uint_t defbitsattr[SPDSOCK_NTYPES] = {
22440Sstevel@tonic-gate 	SPD_ATTR_AH_DEFBITS,
22450Sstevel@tonic-gate 	SPD_ATTR_ESPA_DEFBITS,
22460Sstevel@tonic-gate 	SPD_ATTR_ENCR_DEFBITS
22470Sstevel@tonic-gate };
22480Sstevel@tonic-gate static const uint_t incrbitsattr[SPDSOCK_NTYPES] = {
22490Sstevel@tonic-gate 	SPD_ATTR_AH_INCRBITS,
22500Sstevel@tonic-gate 	SPD_ATTR_ESPA_INCRBITS,
22510Sstevel@tonic-gate 	SPD_ATTR_ENCR_INCRBITS
22520Sstevel@tonic-gate };
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate #define	ATTRPERALG	6	/* fixed attributes per algs */
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate void
spdsock_alglist(queue_t * q,mblk_t * mp)22570Sstevel@tonic-gate spdsock_alglist(queue_t *q, mblk_t *mp)
22580Sstevel@tonic-gate {
22590Sstevel@tonic-gate 	uint_t algtype;
22600Sstevel@tonic-gate 	uint_t algidx;
22610Sstevel@tonic-gate 	uint_t algcount;
22620Sstevel@tonic-gate 	uint_t size;
22630Sstevel@tonic-gate 	mblk_t *m;
22640Sstevel@tonic-gate 	uint8_t *cur;
22650Sstevel@tonic-gate 	spd_msg_t *msg;
22660Sstevel@tonic-gate 	struct spd_ext_actions *act;
22670Sstevel@tonic-gate 	struct spd_attribute *attr;
22683448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
22696430Sdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
22703448Sdh155122 
22713448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
22720Sstevel@tonic-gate 	/*
22730Sstevel@tonic-gate 	 * The SPD client expects to receive separate entries for
22740Sstevel@tonic-gate 	 * AH authentication and ESP authentication supported algorithms.
22750Sstevel@tonic-gate 	 *
22760Sstevel@tonic-gate 	 * Don't return the "any" algorithms, if defined, as no
22770Sstevel@tonic-gate 	 * kernel policies can be set for these algorithms.
22780Sstevel@tonic-gate 	 */
22793448Sdh155122 	algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
22803448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
22813448Sdh155122 
22823448Sdh155122 	if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL)
22830Sstevel@tonic-gate 		algcount--;
22843448Sdh155122 	if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL)
22850Sstevel@tonic-gate 		algcount--;
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	/*
22880Sstevel@tonic-gate 	 * For each algorithm, we encode:
22890Sstevel@tonic-gate 	 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
22900Sstevel@tonic-gate 	 */
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 	size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) +
22930Sstevel@tonic-gate 	    ATTRPERALG * sizeof (struct spd_attribute) * algcount;
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 	ASSERT(ALIGNED64(size));
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	m = allocb(size, BPRI_HI);
22980Sstevel@tonic-gate 	if (m == NULL) {
22993448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
23000Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
23010Sstevel@tonic-gate 		return;
23020Sstevel@tonic-gate 	}
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + size;
23050Sstevel@tonic-gate 	cur = m->b_rptr;
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	msg = (spd_msg_t *)cur;
23080Sstevel@tonic-gate 	bcopy(mp->b_rptr, cur, sizeof (*msg));
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(size);
23110Sstevel@tonic-gate 	msg->spd_msg_errno = 0;
23120Sstevel@tonic-gate 	msg->spd_msg_diagnostic = 0;
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	cur += sizeof (*msg);
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	act = (struct spd_ext_actions *)cur;
23170Sstevel@tonic-gate 	cur += sizeof (*act);
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
23200Sstevel@tonic-gate 	act->spd_actions_exttype = SPD_EXT_ACTION;
23210Sstevel@tonic-gate 	act->spd_actions_count = algcount;
23220Sstevel@tonic-gate 	act->spd_actions_reserved = 0;
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	attr = (struct spd_attribute *)cur;
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate #define	EMIT(tag, value) {					\
23270Sstevel@tonic-gate 		attr->spd_attr_tag = (tag); 			\
23280Sstevel@tonic-gate 		attr->spd_attr_value = (value); 		\
23290Sstevel@tonic-gate 		attr++;			  			\
23300Sstevel@tonic-gate 	}
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 	/*
23330Sstevel@tonic-gate 	 * If you change the number of EMIT's here, change
23340Sstevel@tonic-gate 	 * ATTRPERALG above to match
23350Sstevel@tonic-gate 	 */
23360Sstevel@tonic-gate #define	EMITALGATTRS(_type) {					\
23370Sstevel@tonic-gate 		EMIT(algattr[_type], algid); 		/* 1 */	\
23380Sstevel@tonic-gate 		EMIT(minbitsattr[_type], minbits);	/* 2 */	\
23390Sstevel@tonic-gate 		EMIT(maxbitsattr[_type], maxbits);	/* 3 */	\
23400Sstevel@tonic-gate 		EMIT(defbitsattr[_type], defbits);	/* 4 */	\
23410Sstevel@tonic-gate 		EMIT(incrbitsattr[_type], incr);	/* 5 */	\
23420Sstevel@tonic-gate 		EMIT(SPD_ATTR_NEXT, 0);			/* 6 */	\
23430Sstevel@tonic-gate 	}
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
23463448Sdh155122 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
23473448Sdh155122 		    algidx++) {
23483448Sdh155122 			int algid = ipss->ipsec_sortlist[algtype][algidx];
23493448Sdh155122 			ipsec_alginfo_t *alg =
23503448Sdh155122 			    ipss->ipsec_alglists[algtype][algid];
23510Sstevel@tonic-gate 			uint_t minbits = alg->alg_minbits;
23520Sstevel@tonic-gate 			uint_t maxbits = alg->alg_maxbits;
23530Sstevel@tonic-gate 			uint_t defbits = alg->alg_default_bits;
23540Sstevel@tonic-gate 			uint_t incr = alg->alg_increment;
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 			if (algtype == IPSEC_ALG_AUTH) {
23570Sstevel@tonic-gate 				if (algid == SADB_AALG_NONE)
23580Sstevel@tonic-gate 					continue;
23590Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_AH_AUTH);
23600Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_ESP_AUTH);
23610Sstevel@tonic-gate 			} else {
23620Sstevel@tonic-gate 				if (algid == SADB_EALG_NONE)
23630Sstevel@tonic-gate 					continue;
23640Sstevel@tonic-gate 				ASSERT(algtype == IPSEC_ALG_ENCR);
23650Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_ESP_ENCR);
23660Sstevel@tonic-gate 			}
23670Sstevel@tonic-gate 		}
23680Sstevel@tonic-gate 	}
23690Sstevel@tonic-gate 
23703448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate #undef EMITALGATTRS
23730Sstevel@tonic-gate #undef EMIT
23740Sstevel@tonic-gate #undef ATTRPERALG
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate 	attr--;
23770Sstevel@tonic-gate 	attr->spd_attr_tag = SPD_ATTR_END;
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 	freemsg(mp);
23800Sstevel@tonic-gate 	qreply(q, m);
23810Sstevel@tonic-gate }
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate /*
23840Sstevel@tonic-gate  * Process a SPD_DUMPALGS request.
23850Sstevel@tonic-gate  */
23860Sstevel@tonic-gate 
238710824SMark.Fenwick@Sun.COM #define	ATTRPERALG	9	/* fixed attributes per algs */
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate void
spdsock_dumpalgs(queue_t * q,mblk_t * mp)23900Sstevel@tonic-gate spdsock_dumpalgs(queue_t *q, mblk_t *mp)
23910Sstevel@tonic-gate {
23920Sstevel@tonic-gate 	uint_t algtype;
23930Sstevel@tonic-gate 	uint_t algidx;
23940Sstevel@tonic-gate 	uint_t size;
23950Sstevel@tonic-gate 	mblk_t *m;
23960Sstevel@tonic-gate 	uint8_t *cur;
23970Sstevel@tonic-gate 	spd_msg_t *msg;
23980Sstevel@tonic-gate 	struct spd_ext_actions *act;
23990Sstevel@tonic-gate 	struct spd_attribute *attr;
24000Sstevel@tonic-gate 	ipsec_alginfo_t *alg;
24010Sstevel@tonic-gate 	uint_t algid;
24020Sstevel@tonic-gate 	uint_t i;
24030Sstevel@tonic-gate 	uint_t alg_size;
24043448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
24056430Sdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
24063448Sdh155122 
24073448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	/*
24100Sstevel@tonic-gate 	 * For each algorithm, we encode:
24110Sstevel@tonic-gate 	 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
24120Sstevel@tonic-gate 	 *
24130Sstevel@tonic-gate 	 * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE*
241410824SMark.Fenwick@Sun.COM 	 * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_NPARAMS / ALG_PARAMS* /
241510824SMark.Fenwick@Sun.COM 	 * ALG_MECHNAME / ALG_FLAGS / {END, NEXT}
24160Sstevel@tonic-gate 	 */
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	/*
24190Sstevel@tonic-gate 	 * Compute the size of the SPD message.
24200Sstevel@tonic-gate 	 */
24210Sstevel@tonic-gate 	size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions);
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
24243448Sdh155122 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
24253448Sdh155122 		    algidx++) {
24263448Sdh155122 			algid = ipss->ipsec_sortlist[algtype][algidx];
24273448Sdh155122 			alg = ipss->ipsec_alglists[algtype][algid];
24280Sstevel@tonic-gate 			alg_size = sizeof (struct spd_attribute) *
24290Sstevel@tonic-gate 			    (ATTRPERALG + alg->alg_nkey_sizes +
243010824SMark.Fenwick@Sun.COM 			    alg->alg_nblock_sizes + alg->alg_nparams) +
243110824SMark.Fenwick@Sun.COM 			    CRYPTO_MAX_MECH_NAME;
24320Sstevel@tonic-gate 			size += alg_size;
24330Sstevel@tonic-gate 		}
24340Sstevel@tonic-gate 	}
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	ASSERT(ALIGNED64(size));
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	m = allocb(size, BPRI_HI);
24390Sstevel@tonic-gate 	if (m == NULL) {
24403448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
24410Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
24420Sstevel@tonic-gate 		return;
24430Sstevel@tonic-gate 	}
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + size;
24460Sstevel@tonic-gate 	cur = m->b_rptr;
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	msg = (spd_msg_t *)cur;
24490Sstevel@tonic-gate 	bcopy(mp->b_rptr, cur, sizeof (*msg));
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(size);
24520Sstevel@tonic-gate 	msg->spd_msg_errno = 0;
245310019SMark.Fenwick@Sun.COM 	msg->spd_msg_type = SPD_ALGLIST;
245410019SMark.Fenwick@Sun.COM 
24550Sstevel@tonic-gate 	msg->spd_msg_diagnostic = 0;
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 	cur += sizeof (*msg);
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 	act = (struct spd_ext_actions *)cur;
24600Sstevel@tonic-gate 	cur += sizeof (*act);
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
24630Sstevel@tonic-gate 	act->spd_actions_exttype = SPD_EXT_ACTION;
24643448Sdh155122 	act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
24653448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
24660Sstevel@tonic-gate 	act->spd_actions_reserved = 0;
24670Sstevel@tonic-gate 
246810019SMark.Fenwick@Sun.COM 	/*
246910019SMark.Fenwick@Sun.COM 	 * If there aren't any algorithms registered, return an empty message.
247010019SMark.Fenwick@Sun.COM 	 * spdsock_get_ext() knows how to deal with this.
247110019SMark.Fenwick@Sun.COM 	 */
247210019SMark.Fenwick@Sun.COM 	if (act->spd_actions_count == 0) {
247310019SMark.Fenwick@Sun.COM 		act->spd_actions_len = 0;
247410019SMark.Fenwick@Sun.COM 		mutex_exit(&ipss->ipsec_alg_lock);
247510019SMark.Fenwick@Sun.COM 		goto error;
247610019SMark.Fenwick@Sun.COM 	}
247710019SMark.Fenwick@Sun.COM 
24780Sstevel@tonic-gate 	attr = (struct spd_attribute *)cur;
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate #define	EMIT(tag, value) {					\
24810Sstevel@tonic-gate 		attr->spd_attr_tag = (tag); 			\
24820Sstevel@tonic-gate 		attr->spd_attr_value = (value); 		\
24830Sstevel@tonic-gate 		attr++;			  			\
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
24873448Sdh155122 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
24883448Sdh155122 		    algidx++) {
24893448Sdh155122 
24903448Sdh155122 			algid = ipss->ipsec_sortlist[algtype][algidx];
24913448Sdh155122 			alg = ipss->ipsec_alglists[algtype][algid];
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 			/*
24940Sstevel@tonic-gate 			 * If you change the number of EMIT's here, change
24950Sstevel@tonic-gate 			 * ATTRPERALG above to match
24960Sstevel@tonic-gate 			 */
24970Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_ID, algid);
24980Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]);
24990Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment);
25000Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes);
25010Sstevel@tonic-gate 			for (i = 0; i < alg->alg_nkey_sizes; i++)
25020Sstevel@tonic-gate 				EMIT(SPD_ATTR_ALG_KEYSIZE,
25030Sstevel@tonic-gate 				    alg->alg_key_sizes[i]);
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes);
25060Sstevel@tonic-gate 			for (i = 0; i < alg->alg_nblock_sizes; i++)
25070Sstevel@tonic-gate 				EMIT(SPD_ATTR_ALG_BLOCKSIZE,
25080Sstevel@tonic-gate 				    alg->alg_block_sizes[i]);
25090Sstevel@tonic-gate 
251010824SMark.Fenwick@Sun.COM 			EMIT(SPD_ATTR_ALG_NPARAMS, alg->alg_nparams);
251110824SMark.Fenwick@Sun.COM 			for (i = 0; i < alg->alg_nparams; i++)
251210824SMark.Fenwick@Sun.COM 				EMIT(SPD_ATTR_ALG_PARAMS,
251310824SMark.Fenwick@Sun.COM 				    alg->alg_params[i]);
251410824SMark.Fenwick@Sun.COM 
251510824SMark.Fenwick@Sun.COM 			EMIT(SPD_ATTR_ALG_FLAGS, alg->alg_flags);
251610824SMark.Fenwick@Sun.COM 
25170Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME);
25180Sstevel@tonic-gate 			bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME);
25190Sstevel@tonic-gate 			attr = (struct spd_attribute *)((char *)attr +
25200Sstevel@tonic-gate 			    CRYPTO_MAX_MECH_NAME);
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 			EMIT(SPD_ATTR_NEXT, 0);
25230Sstevel@tonic-gate 		}
25240Sstevel@tonic-gate 	}
25250Sstevel@tonic-gate 
25263448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate #undef EMITALGATTRS
25290Sstevel@tonic-gate #undef EMIT
25300Sstevel@tonic-gate #undef ATTRPERALG
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	attr--;
25330Sstevel@tonic-gate 	attr->spd_attr_tag = SPD_ATTR_END;
25340Sstevel@tonic-gate 
253510019SMark.Fenwick@Sun.COM error:
25360Sstevel@tonic-gate 	freemsg(mp);
25370Sstevel@tonic-gate 	qreply(q, m);
25380Sstevel@tonic-gate }
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate /*
25410Sstevel@tonic-gate  * Do the actual work of processing an SPD_UPDATEALGS request. Can
25420Sstevel@tonic-gate  * be invoked either once IPsec is loaded on a cached request, or
25430Sstevel@tonic-gate  * when a request is received while IPsec is loaded.
25440Sstevel@tonic-gate  */
254511042SErik.Nordmark@Sun.COM static int
spdsock_do_updatealg(spd_ext_t * extv[],spd_stack_t * spds)254611042SErik.Nordmark@Sun.COM spdsock_do_updatealg(spd_ext_t *extv[], spd_stack_t *spds)
25470Sstevel@tonic-gate {
25480Sstevel@tonic-gate 	struct spd_ext_actions *actp;
25490Sstevel@tonic-gate 	struct spd_attribute *attr, *endattr;
25500Sstevel@tonic-gate 	uint64_t *start, *end;
25510Sstevel@tonic-gate 	ipsec_alginfo_t *alg = NULL;
25520Sstevel@tonic-gate 	ipsec_algtype_t alg_type = 0;
25530Sstevel@tonic-gate 	boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE;
25540Sstevel@tonic-gate 	uint_t i, cur_key, cur_block, algid;
255511042SErik.Nordmark@Sun.COM 	int diag = -1;
255611042SErik.Nordmark@Sun.COM 
25573448Sdh155122 	ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	/* parse the message, building the list of algorithms */
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
256211042SErik.Nordmark@Sun.COM 	if (actp == NULL)
256311042SErik.Nordmark@Sun.COM 		return (SPD_DIAGNOSTIC_NO_ACTION_EXT);
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	start = (uint64_t *)actp;
25660Sstevel@tonic-gate 	end = (start + actp->spd_actions_len);
25670Sstevel@tonic-gate 	endattr = (struct spd_attribute *)end;
25680Sstevel@tonic-gate 	attr = (struct spd_attribute *)&actp[1];
25690Sstevel@tonic-gate 
25703448Sdh155122 	bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS *
25710Sstevel@tonic-gate 	    sizeof (ipsec_alginfo_t *));
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate #define	ALG_KEY_SIZES(a)   (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t))
25760Sstevel@tonic-gate #define	ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t))
257711136SMark.Fenwick@Sun.COM #define	ALG_PARAM_SIZES(a) (((a)->alg_nparams + 1) * sizeof (uint16_t))
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 	while (attr < endattr) {
25800Sstevel@tonic-gate 		switch (attr->spd_attr_tag) {
25810Sstevel@tonic-gate 		case SPD_ATTR_NOP:
25820Sstevel@tonic-gate 		case SPD_ATTR_EMPTY:
25830Sstevel@tonic-gate 			break;
25840Sstevel@tonic-gate 		case SPD_ATTR_END:
25850Sstevel@tonic-gate 			attr = endattr;
25860Sstevel@tonic-gate 			/* FALLTHRU */
25870Sstevel@tonic-gate 		case SPD_ATTR_NEXT:
25880Sstevel@tonic-gate 			if (doing_proto) {
25890Sstevel@tonic-gate 				doing_proto = B_FALSE;
25900Sstevel@tonic-gate 				break;
25910Sstevel@tonic-gate 			}
25920Sstevel@tonic-gate 			if (skip_alg) {
25930Sstevel@tonic-gate 				ipsec_alg_free(alg);
25940Sstevel@tonic-gate 			} else {
25950Sstevel@tonic-gate 				ipsec_alg_free(
25963448Sdh155122 				    spds->spds_algs[alg_type][alg->alg_id]);
25973448Sdh155122 				spds->spds_algs[alg_type][alg->alg_id] =
25983448Sdh155122 				    alg;
25990Sstevel@tonic-gate 			}
26000Sstevel@tonic-gate 			alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
26010Sstevel@tonic-gate 			break;
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 		case SPD_ATTR_ALG_ID:
26040Sstevel@tonic-gate 			if (attr->spd_attr_value >= IPSEC_MAX_ALGS) {
26053448Sdh155122 				ss1dbg(spds, ("spdsock_do_updatealg: "
26060Sstevel@tonic-gate 				    "invalid alg id %d\n",
26070Sstevel@tonic-gate 				    attr->spd_attr_value));
260811042SErik.Nordmark@Sun.COM 				diag = SPD_DIAGNOSTIC_ALG_ID_RANGE;
26090Sstevel@tonic-gate 				goto bail;
26100Sstevel@tonic-gate 			}
26110Sstevel@tonic-gate 			alg->alg_id = attr->spd_attr_value;
26120Sstevel@tonic-gate 			break;
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 		case SPD_ATTR_ALG_PROTO:
26150Sstevel@tonic-gate 			/* find the alg type */
26160Sstevel@tonic-gate 			for (i = 0; i < NALGPROTOS; i++)
26170Sstevel@tonic-gate 				if (algproto[i] == attr->spd_attr_value)
26180Sstevel@tonic-gate 					break;
26190Sstevel@tonic-gate 			skip_alg = (i == NALGPROTOS);
26200Sstevel@tonic-gate 			if (!skip_alg)
26210Sstevel@tonic-gate 				alg_type = i;
26220Sstevel@tonic-gate 			break;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 		case SPD_ATTR_ALG_INCRBITS:
26250Sstevel@tonic-gate 			alg->alg_increment = attr->spd_attr_value;
26260Sstevel@tonic-gate 			break;
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 		case SPD_ATTR_ALG_NKEYSIZES:
26290Sstevel@tonic-gate 			if (alg->alg_key_sizes != NULL) {
26300Sstevel@tonic-gate 				kmem_free(alg->alg_key_sizes,
26310Sstevel@tonic-gate 				    ALG_KEY_SIZES(alg));
26320Sstevel@tonic-gate 			}
26330Sstevel@tonic-gate 			alg->alg_nkey_sizes = attr->spd_attr_value;
26340Sstevel@tonic-gate 			/*
26350Sstevel@tonic-gate 			 * Allocate room for the trailing zero key size
26360Sstevel@tonic-gate 			 * value as well.
26370Sstevel@tonic-gate 			 */
26380Sstevel@tonic-gate 			alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg),
26390Sstevel@tonic-gate 			    KM_SLEEP);
26400Sstevel@tonic-gate 			cur_key = 0;
26410Sstevel@tonic-gate 			break;
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 		case SPD_ATTR_ALG_KEYSIZE:
26440Sstevel@tonic-gate 			if (alg->alg_key_sizes == NULL ||
26450Sstevel@tonic-gate 			    cur_key >= alg->alg_nkey_sizes) {
26463448Sdh155122 				ss1dbg(spds, ("spdsock_do_updatealg: "
26475240Snordmark 				    "too many key sizes\n"));
264811042SErik.Nordmark@Sun.COM 				diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES;
26490Sstevel@tonic-gate 				goto bail;
26500Sstevel@tonic-gate 			}
26510Sstevel@tonic-gate 			alg->alg_key_sizes[cur_key++] = attr->spd_attr_value;
26520Sstevel@tonic-gate 			break;
26530Sstevel@tonic-gate 
265410824SMark.Fenwick@Sun.COM 		case SPD_ATTR_ALG_FLAGS:
265510824SMark.Fenwick@Sun.COM 			/*
265610824SMark.Fenwick@Sun.COM 			 * Flags (bit mask). The alg_flags element of
265710824SMark.Fenwick@Sun.COM 			 * ipsecalg_flags_t is only 8 bits wide. The
265810824SMark.Fenwick@Sun.COM 			 * user can set the VALID bit, but we will ignore it
265910824SMark.Fenwick@Sun.COM 			 * and make the decision is the algorithm is valid.
266010824SMark.Fenwick@Sun.COM 			 */
266110824SMark.Fenwick@Sun.COM 			alg->alg_flags |= (uint8_t)attr->spd_attr_value;
266210824SMark.Fenwick@Sun.COM 			break;
266310824SMark.Fenwick@Sun.COM 
26640Sstevel@tonic-gate 		case SPD_ATTR_ALG_NBLOCKSIZES:
26650Sstevel@tonic-gate 			if (alg->alg_block_sizes != NULL) {
26660Sstevel@tonic-gate 				kmem_free(alg->alg_block_sizes,
26670Sstevel@tonic-gate 				    ALG_BLOCK_SIZES(alg));
26680Sstevel@tonic-gate 			}
26690Sstevel@tonic-gate 			alg->alg_nblock_sizes = attr->spd_attr_value;
26700Sstevel@tonic-gate 			/*
26710Sstevel@tonic-gate 			 * Allocate room for the trailing zero block size
26720Sstevel@tonic-gate 			 * value as well.
26730Sstevel@tonic-gate 			 */
26740Sstevel@tonic-gate 			alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg),
26750Sstevel@tonic-gate 			    KM_SLEEP);
26760Sstevel@tonic-gate 			cur_block = 0;
26770Sstevel@tonic-gate 			break;
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate 		case SPD_ATTR_ALG_BLOCKSIZE:
26800Sstevel@tonic-gate 			if (alg->alg_block_sizes == NULL ||
26810Sstevel@tonic-gate 			    cur_block >= alg->alg_nblock_sizes) {
26823448Sdh155122 				ss1dbg(spds, ("spdsock_do_updatealg: "
26835240Snordmark 				    "too many block sizes\n"));
268411042SErik.Nordmark@Sun.COM 				diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
26850Sstevel@tonic-gate 				goto bail;
26860Sstevel@tonic-gate 			}
26870Sstevel@tonic-gate 			alg->alg_block_sizes[cur_block++] =
26880Sstevel@tonic-gate 			    attr->spd_attr_value;
26890Sstevel@tonic-gate 			break;
26900Sstevel@tonic-gate 
269110824SMark.Fenwick@Sun.COM 		case SPD_ATTR_ALG_NPARAMS:
269210824SMark.Fenwick@Sun.COM 			if (alg->alg_params != NULL) {
269310824SMark.Fenwick@Sun.COM 				kmem_free(alg->alg_params,
269411136SMark.Fenwick@Sun.COM 				    ALG_PARAM_SIZES(alg));
269510824SMark.Fenwick@Sun.COM 			}
269610824SMark.Fenwick@Sun.COM 			alg->alg_nparams = attr->spd_attr_value;
269710824SMark.Fenwick@Sun.COM 			/*
269810824SMark.Fenwick@Sun.COM 			 * Allocate room for the trailing zero block size
269910824SMark.Fenwick@Sun.COM 			 * value as well.
270010824SMark.Fenwick@Sun.COM 			 */
270111136SMark.Fenwick@Sun.COM 			alg->alg_params = kmem_zalloc(ALG_PARAM_SIZES(alg),
270210824SMark.Fenwick@Sun.COM 			    KM_SLEEP);
270310824SMark.Fenwick@Sun.COM 			cur_block = 0;
270410824SMark.Fenwick@Sun.COM 			break;
270510824SMark.Fenwick@Sun.COM 
270610824SMark.Fenwick@Sun.COM 		case SPD_ATTR_ALG_PARAMS:
270710824SMark.Fenwick@Sun.COM 			if (alg->alg_params == NULL ||
270810824SMark.Fenwick@Sun.COM 			    cur_block >= alg->alg_nparams) {
270910824SMark.Fenwick@Sun.COM 				ss1dbg(spds, ("spdsock_do_updatealg: "
271010824SMark.Fenwick@Sun.COM 				    "too many params\n"));
271111042SErik.Nordmark@Sun.COM 				diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
271210824SMark.Fenwick@Sun.COM 				goto bail;
271310824SMark.Fenwick@Sun.COM 			}
271410824SMark.Fenwick@Sun.COM 			/*
271510824SMark.Fenwick@Sun.COM 			 * Array contains: iv_len, icv_len, salt_len
271610824SMark.Fenwick@Sun.COM 			 * Any additional parameters are currently ignored.
271710824SMark.Fenwick@Sun.COM 			 */
271810824SMark.Fenwick@Sun.COM 			alg->alg_params[cur_block++] =
271910824SMark.Fenwick@Sun.COM 			    attr->spd_attr_value;
272010824SMark.Fenwick@Sun.COM 			break;
272110824SMark.Fenwick@Sun.COM 
27220Sstevel@tonic-gate 		case SPD_ATTR_ALG_MECHNAME: {
27230Sstevel@tonic-gate 			char *mech_name;
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 			if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) {
27263448Sdh155122 				ss1dbg(spds, ("spdsock_do_updatealg: "
27275240Snordmark 				    "mech name too long\n"));
272811042SErik.Nordmark@Sun.COM 				diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN;
27290Sstevel@tonic-gate 				goto bail;
27300Sstevel@tonic-gate 			}
27310Sstevel@tonic-gate 			mech_name = (char *)(attr + 1);
27320Sstevel@tonic-gate 			bcopy(mech_name, alg->alg_mech_name,
27330Sstevel@tonic-gate 			    attr->spd_attr_value);
27340Sstevel@tonic-gate 			alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0';
27350Sstevel@tonic-gate 			attr = (struct spd_attribute *)((char *)attr +
27360Sstevel@tonic-gate 			    attr->spd_attr_value);
27370Sstevel@tonic-gate 			break;
27380Sstevel@tonic-gate 		}
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 		case SPD_ATTR_PROTO_ID:
27410Sstevel@tonic-gate 			doing_proto = B_TRUE;
27420Sstevel@tonic-gate 			for (i = 0; i < NALGPROTOS; i++) {
27430Sstevel@tonic-gate 				if (algproto[i] == attr->spd_attr_value) {
27440Sstevel@tonic-gate 					alg_type = i;
27450Sstevel@tonic-gate 					break;
27460Sstevel@tonic-gate 				}
27470Sstevel@tonic-gate 			}
27480Sstevel@tonic-gate 			break;
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate 		case SPD_ATTR_PROTO_EXEC_MODE:
27510Sstevel@tonic-gate 			if (!doing_proto)
27520Sstevel@tonic-gate 				break;
27530Sstevel@tonic-gate 			for (i = 0; i < NEXECMODES; i++) {
27540Sstevel@tonic-gate 				if (execmodes[i] == attr->spd_attr_value) {
27553448Sdh155122 					spds->spds_algs_exec_mode[alg_type] = i;
27560Sstevel@tonic-gate 					break;
27570Sstevel@tonic-gate 				}
27580Sstevel@tonic-gate 			}
27590Sstevel@tonic-gate 			break;
27600Sstevel@tonic-gate 		}
27610Sstevel@tonic-gate 		attr++;
27620Sstevel@tonic-gate 	}
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate #undef	ALG_KEY_SIZES
27650Sstevel@tonic-gate #undef	ALG_BLOCK_SIZES
276611136SMark.Fenwick@Sun.COM #undef	ALG_PARAM_SIZES
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 	/* update the algorithm tables */
27693448Sdh155122 	spdsock_merge_algs(spds);
27700Sstevel@tonic-gate bail:
27710Sstevel@tonic-gate 	/* cleanup */
27720Sstevel@tonic-gate 	ipsec_alg_free(alg);
27730Sstevel@tonic-gate 	for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++)
27745240Snordmark 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++)
27753448Sdh155122 		if (spds->spds_algs[alg_type][algid] != NULL)
27765240Snordmark 			ipsec_alg_free(spds->spds_algs[alg_type][algid]);
277711042SErik.Nordmark@Sun.COM 	return (diag);
27780Sstevel@tonic-gate }
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate /*
27810Sstevel@tonic-gate  * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue
27820Sstevel@tonic-gate  * the request until IPsec loads. If IPsec is loaded, act on it
27830Sstevel@tonic-gate  * immediately.
27840Sstevel@tonic-gate  */
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate static void
spdsock_updatealg(queue_t * q,mblk_t * mp,spd_ext_t * extv[])27870Sstevel@tonic-gate spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[])
27880Sstevel@tonic-gate {
27893448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
27903448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
27913448Sdh155122 	ipsec_stack_t	*ipss = spds->spds_netstack->netstack_ipsec;
279211861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
27933448Sdh155122 
27943448Sdh155122 	if (!ipsec_loaded(ipss)) {
27950Sstevel@tonic-gate 		/*
27960Sstevel@tonic-gate 		 * IPsec is not loaded, save request and return nicely,
27970Sstevel@tonic-gate 		 * the message will be processed once IPsec loads.
27980Sstevel@tonic-gate 		 */
27990Sstevel@tonic-gate 		mblk_t *new_mp;
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate 		/* last update message wins */
28020Sstevel@tonic-gate 		if ((new_mp = copymsg(mp)) == NULL) {
28030Sstevel@tonic-gate 			spdsock_error(q, mp, ENOMEM, 0);
28040Sstevel@tonic-gate 			return;
28050Sstevel@tonic-gate 		}
28063448Sdh155122 		mutex_enter(&spds->spds_alg_lock);
28073448Sdh155122 		bcopy(extv, spds->spds_extv_algs,
28080Sstevel@tonic-gate 		    sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
28093448Sdh155122 		if (spds->spds_mp_algs != NULL)
28103448Sdh155122 			freemsg(spds->spds_mp_algs);
28113448Sdh155122 		spds->spds_mp_algs = mp;
28123448Sdh155122 		mutex_exit(&spds->spds_alg_lock);
281311861SMarek.Pospisil@Sun.COM 		if (auditing) {
28148778SErik.Nordmark@Sun.COM 			cred_t *cr;
28158778SErik.Nordmark@Sun.COM 			pid_t cpid;
28168778SErik.Nordmark@Sun.COM 
28178778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, &cpid);
28188778SErik.Nordmark@Sun.COM 			audit_pf_policy(SPD_UPDATEALGS, cr,
28194307Spwernau 			    spds->spds_netstack, NULL, B_TRUE, EAGAIN,
28208778SErik.Nordmark@Sun.COM 			    cpid);
28218778SErik.Nordmark@Sun.COM 		}
28220Sstevel@tonic-gate 		spd_echo(q, new_mp);
28230Sstevel@tonic-gate 	} else {
28240Sstevel@tonic-gate 		/*
28250Sstevel@tonic-gate 		 * IPsec is loaded, act on the message immediately.
28260Sstevel@tonic-gate 		 */
28270Sstevel@tonic-gate 		int diag;
28280Sstevel@tonic-gate 
28293448Sdh155122 		mutex_enter(&spds->spds_alg_lock);
283011042SErik.Nordmark@Sun.COM 		diag = spdsock_do_updatealg(extv, spds);
28314307Spwernau 		if (diag == -1) {
283211042SErik.Nordmark@Sun.COM 			/* Keep the lock held while we walk the SA tables. */
283311042SErik.Nordmark@Sun.COM 			sadb_alg_update(IPSEC_ALG_ALL, 0, 0,
283411042SErik.Nordmark@Sun.COM 			    spds->spds_netstack);
283511042SErik.Nordmark@Sun.COM 			mutex_exit(&spds->spds_alg_lock);
28360Sstevel@tonic-gate 			spd_echo(q, mp);
283711861SMarek.Pospisil@Sun.COM 			if (auditing) {
28388778SErik.Nordmark@Sun.COM 				cred_t *cr;
28398778SErik.Nordmark@Sun.COM 				pid_t cpid;
28408778SErik.Nordmark@Sun.COM 
28418778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
28428778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_UPDATEALGS, cr,
28438778SErik.Nordmark@Sun.COM 				    spds->spds_netstack, NULL, B_TRUE, 0,
28448778SErik.Nordmark@Sun.COM 				    cpid);
28458778SErik.Nordmark@Sun.COM 			}
28464307Spwernau 		} else {
284711042SErik.Nordmark@Sun.COM 			mutex_exit(&spds->spds_alg_lock);
28480Sstevel@tonic-gate 			spdsock_diag(q, mp, diag);
284911861SMarek.Pospisil@Sun.COM 			if (auditing) {
28508778SErik.Nordmark@Sun.COM 				cred_t *cr;
28518778SErik.Nordmark@Sun.COM 				pid_t cpid;
28528778SErik.Nordmark@Sun.COM 
28538778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
28548778SErik.Nordmark@Sun.COM 				audit_pf_policy(SPD_UPDATEALGS, cr,
28558778SErik.Nordmark@Sun.COM 				    spds->spds_netstack, NULL, B_TRUE, diag,
28568778SErik.Nordmark@Sun.COM 				    cpid);
28578778SErik.Nordmark@Sun.COM 			}
28584307Spwernau 		}
28590Sstevel@tonic-gate 	}
28600Sstevel@tonic-gate }
28610Sstevel@tonic-gate 
28623055Sdanmcd /*
286311456Sdanmcd@sun.com  * Find a tunnel instance (using the name to link ID mapping), and
286411456Sdanmcd@sun.com  * update it after an IPsec change.  We need to do this always in case
286511456Sdanmcd@sun.com  * we add policy AFTER plumbing a tunnel.  We also need to do this
286611456Sdanmcd@sun.com  * because, as a side-effect, the tunnel's MTU is updated to reflect
286711456Sdanmcd@sun.com  * any IPsec overhead in the itp's policy.
286811456Sdanmcd@sun.com  */
286911456Sdanmcd@sun.com static void
update_iptun_policy(ipsec_tun_pol_t * itp)287011456Sdanmcd@sun.com update_iptun_policy(ipsec_tun_pol_t *itp)
287111456Sdanmcd@sun.com {
287211456Sdanmcd@sun.com 	datalink_id_t linkid;
287311456Sdanmcd@sun.com 
287411456Sdanmcd@sun.com 	if (dls_mgmt_get_linkid(itp->itp_name, &linkid) == 0)
287511456Sdanmcd@sun.com 		iptun_set_policy(linkid, itp);
287611456Sdanmcd@sun.com }
287711456Sdanmcd@sun.com 
287811456Sdanmcd@sun.com /*
28793055Sdanmcd  * Sort through the mess of polhead options to retrieve an appropriate one.
28803055Sdanmcd  * Returns NULL if we send an spdsock error.  Returns a valid pointer if we
28813055Sdanmcd  * found a valid polhead.  Returns ALL_ACTIVE_POLHEADS (aka. -1) or
28823055Sdanmcd  * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to
28833055Sdanmcd  * act on ALL policy heads.
28843055Sdanmcd  */
28853055Sdanmcd static ipsec_policy_head_t *
get_appropriate_polhead(queue_t * q,mblk_t * mp,spd_if_t * tunname,int spdid,int msgtype,ipsec_tun_pol_t ** itpp)28863055Sdanmcd get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid,
28873055Sdanmcd     int msgtype, ipsec_tun_pol_t **itpp)
28883055Sdanmcd {
28893055Sdanmcd 	ipsec_tun_pol_t *itp;
28903055Sdanmcd 	ipsec_policy_head_t *iph;
28913055Sdanmcd 	int errno;
28923055Sdanmcd 	char *tname;
28933055Sdanmcd 	boolean_t active;
28943055Sdanmcd 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
28956430Sdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
28963055Sdanmcd 	uint64_t gen;	/* Placeholder */
28973055Sdanmcd 
28983055Sdanmcd 	active = (spdid == SPD_ACTIVE);
28993055Sdanmcd 	*itpp = NULL;
29003055Sdanmcd 	if (!active && spdid != SPD_STANDBY) {
29013055Sdanmcd 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID);
29023055Sdanmcd 		return (NULL);
29033055Sdanmcd 	}
29043055Sdanmcd 
29053055Sdanmcd 	if (tunname != NULL) {
29063055Sdanmcd 		/* Acting on a tunnel's SPD. */
29073055Sdanmcd 		tname = (char *)tunname->spd_if_name;
29083055Sdanmcd 		if (*tname == '\0') {
29093055Sdanmcd 			/* Handle all-polhead cases here. */
29103055Sdanmcd 			if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) {
29113055Sdanmcd 				spdsock_diag(q, mp,
29123055Sdanmcd 				    SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
29133055Sdanmcd 				return (NULL);
29143055Sdanmcd 			}
29153055Sdanmcd 			return (active ? ALL_ACTIVE_POLHEADS :
29163055Sdanmcd 			    ALL_INACTIVE_POLHEADS);
29173055Sdanmcd 		}
29183055Sdanmcd 
29196430Sdanmcd 		itp = get_tunnel_policy(tname, ns);
29203055Sdanmcd 		if (itp == NULL) {
29213055Sdanmcd 			if (msgtype != SPD_ADDRULE) {
29223055Sdanmcd 				/* "Tunnel not found" */
29233055Sdanmcd 				spdsock_error(q, mp, ENOENT, 0);
29243055Sdanmcd 				return (NULL);
29253055Sdanmcd 			}
29263055Sdanmcd 
29273055Sdanmcd 			errno = 0;
29286430Sdanmcd 			itp = create_tunnel_policy(tname, &errno, &gen, ns);
29293055Sdanmcd 			if (itp == NULL) {
29303055Sdanmcd 				/*
29313055Sdanmcd 				 * Something very bad happened, most likely
29323055Sdanmcd 				 * ENOMEM.  Return an indicator.
29333055Sdanmcd 				 */
29343055Sdanmcd 				spdsock_error(q, mp, errno, 0);
29353055Sdanmcd 				return (NULL);
29363055Sdanmcd 			}
29373055Sdanmcd 		}
293811456Sdanmcd@sun.com 
293911456Sdanmcd@sun.com 		/* Match up the itp to an iptun instance. */
294011456Sdanmcd@sun.com 		update_iptun_policy(itp);
294110616SSebastien.Roy@Sun.COM 
29423055Sdanmcd 		*itpp = itp;
29433055Sdanmcd 		/* For spdsock dump state, set the polhead's name. */
29443055Sdanmcd 		if (msgtype == SPD_DUMP) {
29456430Sdanmcd 			ITP_REFHOLD(itp);
29466430Sdanmcd 			ss->spdsock_itp = itp;
29473055Sdanmcd 			ss->spdsock_dump_tunnel = itp->itp_flags &
29483055Sdanmcd 			    (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL);
29493055Sdanmcd 		}
29503055Sdanmcd 	} else {
29513055Sdanmcd 		itp = NULL;
29523055Sdanmcd 		/* For spdsock dump state, indicate it's global policy. */
29533055Sdanmcd 		if (msgtype == SPD_DUMP)
29546430Sdanmcd 			ss->spdsock_itp = NULL;
29553055Sdanmcd 	}
29563055Sdanmcd 
29573055Sdanmcd 	if (active)
29583448Sdh155122 		iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy;
29593055Sdanmcd 	else
29603448Sdh155122 		iph = (itp == NULL) ? ipsec_inactive_policy(ns) :
29613055Sdanmcd 		    itp->itp_inactive;
29623055Sdanmcd 
29633055Sdanmcd 	ASSERT(iph != NULL);
29643055Sdanmcd 	if (itp != NULL) {
29653055Sdanmcd 		IPPH_REFHOLD(iph);
29663055Sdanmcd 	}
29673055Sdanmcd 
29683055Sdanmcd 	return (iph);
29693055Sdanmcd }
29703055Sdanmcd 
29710Sstevel@tonic-gate static void
spdsock_parse(queue_t * q,mblk_t * mp)29720Sstevel@tonic-gate spdsock_parse(queue_t *q, mblk_t *mp)
29730Sstevel@tonic-gate {
29740Sstevel@tonic-gate 	spd_msg_t *spmsg;
29750Sstevel@tonic-gate 	spd_ext_t *extv[SPD_EXT_MAX + 1];
29760Sstevel@tonic-gate 	uint_t msgsize;
29770Sstevel@tonic-gate 	ipsec_policy_head_t *iph;
29783055Sdanmcd 	ipsec_tun_pol_t *itp;
29793055Sdanmcd 	spd_if_t *tunname;
29803448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
29813448Sdh155122 	spd_stack_t *spds = ss->spdsock_spds;
29823448Sdh155122 	netstack_t *ns = spds->spds_netstack;
29833448Sdh155122 	ipsec_stack_t *ipss = ns->netstack_ipsec;
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	/* Make sure nothing's below me. */
29860Sstevel@tonic-gate 	ASSERT(WR(q)->q_next == NULL);
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate 	spmsg = (spd_msg_t *)mp->b_rptr;
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	msgsize = SPD_64TO8(spmsg->spd_msg_len);
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate 	if (msgdsize(mp) != msgsize) {
29930Sstevel@tonic-gate 		/*
29940Sstevel@tonic-gate 		 * Message len incorrect w.r.t. actual size.  Send an error
29950Sstevel@tonic-gate 		 * (EMSGSIZE).	It may be necessary to massage things a
29960Sstevel@tonic-gate 		 * bit.	 For example, if the spd_msg_type is hosed,
29970Sstevel@tonic-gate 		 * I need to set it to SPD_RESERVED to get delivery to
29980Sstevel@tonic-gate 		 * do the right thing.	Then again, maybe just letting
29990Sstevel@tonic-gate 		 * the error delivery do the right thing.
30000Sstevel@tonic-gate 		 */
30013448Sdh155122 		ss2dbg(spds,
30023448Sdh155122 		    ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
30030Sstevel@tonic-gate 		    msgdsize(mp), msgsize));
30040Sstevel@tonic-gate 		spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE);
30050Sstevel@tonic-gate 		return;
30060Sstevel@tonic-gate 	}
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 	if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
30090Sstevel@tonic-gate 		/* Get all message into one mblk. */
30100Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
30110Sstevel@tonic-gate 			/*
30120Sstevel@tonic-gate 			 * Something screwy happened.
30130Sstevel@tonic-gate 			 */
30143448Sdh155122 			ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n"));
30150Sstevel@tonic-gate 			return;
30160Sstevel@tonic-gate 		} else {
30170Sstevel@tonic-gate 			spmsg = (spd_msg_t *)mp->b_rptr;
30180Sstevel@tonic-gate 		}
30190Sstevel@tonic-gate 	}
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 	switch (spdsock_get_ext(extv, spmsg, msgsize)) {
30220Sstevel@tonic-gate 	case KGE_DUP:
30230Sstevel@tonic-gate 		/* Handle duplicate extension. */
30243448Sdh155122 		ss1dbg(spds, ("Got duplicate extension of type %d.\n",
30250Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
30260Sstevel@tonic-gate 		spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]);
30270Sstevel@tonic-gate 		return;
30280Sstevel@tonic-gate 	case KGE_UNK:
30290Sstevel@tonic-gate 		/* Handle unknown extension. */
30303448Sdh155122 		ss1dbg(spds, ("Got unknown extension of type %d.\n",
30310Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
30320Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT);
30330Sstevel@tonic-gate 		return;
30340Sstevel@tonic-gate 	case KGE_LEN:
30350Sstevel@tonic-gate 		/* Length error. */
30363448Sdh155122 		ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n",
30370Sstevel@tonic-gate 		    extv[0]->spd_ext_len, extv[0]->spd_ext_type));
30380Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN);
30390Sstevel@tonic-gate 		return;
30400Sstevel@tonic-gate 	case KGE_CHK:
30410Sstevel@tonic-gate 		/* Reality check failed. */
30423448Sdh155122 		ss1dbg(spds, ("Reality check failed on extension type %d.\n",
30430Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
30440Sstevel@tonic-gate 		spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]);
30450Sstevel@tonic-gate 		return;
30460Sstevel@tonic-gate 	default:
30470Sstevel@tonic-gate 		/* Default case is no errors. */
30480Sstevel@tonic-gate 		break;
30490Sstevel@tonic-gate 	}
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 	/*
30520Sstevel@tonic-gate 	 * Special-case SPD_UPDATEALGS so as not to load IPsec.
30530Sstevel@tonic-gate 	 */
30543448Sdh155122 	if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) {
30550Sstevel@tonic-gate 		spdsock_t *ss = (spdsock_t *)q->q_ptr;
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate 		ASSERT(ss != NULL);
30583448Sdh155122 		ipsec_loader_loadnow(ipss);
30590Sstevel@tonic-gate 		ss->spdsock_timeout_arg = mp;
30600Sstevel@tonic-gate 		ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck,
30610Sstevel@tonic-gate 		    q, LOADCHECK_INTERVAL);
30620Sstevel@tonic-gate 		return;
30630Sstevel@tonic-gate 	}
30640Sstevel@tonic-gate 
30653055Sdanmcd 	/* First check for messages that need no polheads at all. */
30660Sstevel@tonic-gate 	switch (spmsg->spd_msg_type) {
30670Sstevel@tonic-gate 	case SPD_UPDATEALGS:
30680Sstevel@tonic-gate 		spdsock_updatealg(q, mp, extv);
30690Sstevel@tonic-gate 		return;
30700Sstevel@tonic-gate 	case SPD_ALGLIST:
30710Sstevel@tonic-gate 		spdsock_alglist(q, mp);
30720Sstevel@tonic-gate 		return;
30730Sstevel@tonic-gate 	case SPD_DUMPALGS:
30740Sstevel@tonic-gate 		spdsock_dumpalgs(q, mp);
30750Sstevel@tonic-gate 		return;
30763055Sdanmcd 	}
30773055Sdanmcd 
30783055Sdanmcd 	/*
30793055Sdanmcd 	 * Then check for ones that need both primary/secondary polheads,
30803055Sdanmcd 	 * finding the appropriate tunnel policy if need be.
30813055Sdanmcd 	 */
30823055Sdanmcd 	tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME];
30833055Sdanmcd 	switch (spmsg->spd_msg_type) {
30843055Sdanmcd 	case SPD_FLIP:
30853055Sdanmcd 		spdsock_flip(q, mp, tunname);
30863055Sdanmcd 		return;
30873055Sdanmcd 	case SPD_CLONE:
30883055Sdanmcd 		spdsock_clone(q, mp, tunname);
30893055Sdanmcd 		return;
30903055Sdanmcd 	}
30913055Sdanmcd 
30923055Sdanmcd 	/*
30933055Sdanmcd 	 * Finally, find ones that operate on exactly one polhead, or
30943055Sdanmcd 	 * "all polheads" of a given type (active/inactive).
30953055Sdanmcd 	 */
30963055Sdanmcd 	iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid,
30973055Sdanmcd 	    spmsg->spd_msg_type, &itp);
30983055Sdanmcd 	if (iph == NULL)
30993055Sdanmcd 		return;
31003055Sdanmcd 
31013055Sdanmcd 	/* All-polheads-ready operations. */
31023055Sdanmcd 	switch (spmsg->spd_msg_type) {
31033055Sdanmcd 	case SPD_FLUSH:
31043055Sdanmcd 		if (itp != NULL) {
31053055Sdanmcd 			mutex_enter(&itp->itp_lock);
31063055Sdanmcd 			if (spmsg->spd_msg_spdid == SPD_ACTIVE)
31073055Sdanmcd 				itp->itp_flags &= ~ITPF_PFLAGS;
31083055Sdanmcd 			else
31093055Sdanmcd 				itp->itp_flags &= ~ITPF_IFLAGS;
31103055Sdanmcd 			mutex_exit(&itp->itp_lock);
311111456Sdanmcd@sun.com 		}
311211456Sdanmcd@sun.com 
311311456Sdanmcd@sun.com 		spdsock_flush(q, iph, itp, mp);
311411456Sdanmcd@sun.com 
311511456Sdanmcd@sun.com 		if (itp != NULL) {
311611456Sdanmcd@sun.com 			/* SPD_FLUSH is worth a tunnel MTU check. */
311711456Sdanmcd@sun.com 			update_iptun_policy(itp);
31183448Sdh155122 			ITP_REFRELE(itp, ns);
31193055Sdanmcd 		}
31203055Sdanmcd 		return;
31213055Sdanmcd 	case SPD_DUMP:
31223055Sdanmcd 		if (itp != NULL)
31233448Sdh155122 			ITP_REFRELE(itp, ns);
31243055Sdanmcd 		spdsock_dump(q, iph, mp);
31253055Sdanmcd 		return;
31263055Sdanmcd 	}
31273055Sdanmcd 
31283055Sdanmcd 	if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
31293055Sdanmcd 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
31303055Sdanmcd 		return;
31313055Sdanmcd 	}
31323055Sdanmcd 
31333055Sdanmcd 	/* Single-polhead-only operations. */
31343055Sdanmcd 	switch (spmsg->spd_msg_type) {
31353055Sdanmcd 	case SPD_ADDRULE:
31363055Sdanmcd 		spdsock_addrule(q, iph, mp, extv, itp);
31373055Sdanmcd 		break;
31383055Sdanmcd 	case SPD_DELETERULE:
31393055Sdanmcd 		spdsock_deleterule(q, iph, mp, extv, itp);
31403055Sdanmcd 		break;
31413055Sdanmcd 	case SPD_LOOKUP:
31423055Sdanmcd 		spdsock_lookup(q, iph, mp, extv, itp);
31433055Sdanmcd 		break;
31440Sstevel@tonic-gate 	default:
31450Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE);
31463055Sdanmcd 		break;
31470Sstevel@tonic-gate 	}
31483055Sdanmcd 
31496430Sdanmcd 	IPPH_REFRELE(iph, ns);
315011456Sdanmcd@sun.com 	if (itp != NULL) {
315111456Sdanmcd@sun.com 		/* SPD_{ADD,DELETE}RULE are worth a tunnel MTU check. */
315211456Sdanmcd@sun.com 		if (spmsg->spd_msg_type == SPD_ADDRULE ||
315311456Sdanmcd@sun.com 		    spmsg->spd_msg_type == SPD_DELETERULE)
315411456Sdanmcd@sun.com 			update_iptun_policy(itp);
31553448Sdh155122 		ITP_REFRELE(itp, ns);
315611456Sdanmcd@sun.com 	}
31570Sstevel@tonic-gate }
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate /*
31600Sstevel@tonic-gate  * If an algorithm mapping was received before IPsec was loaded, process it.
31610Sstevel@tonic-gate  * Called from the IPsec loader.
31620Sstevel@tonic-gate  */
31630Sstevel@tonic-gate void
spdsock_update_pending_algs(netstack_t * ns)31643448Sdh155122 spdsock_update_pending_algs(netstack_t *ns)
31650Sstevel@tonic-gate {
31663448Sdh155122 	spd_stack_t *spds = ns->netstack_spdsock;
31673448Sdh155122 
31683448Sdh155122 	mutex_enter(&spds->spds_alg_lock);
3169*12273SCasper.Dik@Sun.COM 	if (spds->spds_mp_algs != NULL) {
317011042SErik.Nordmark@Sun.COM 		(void) spdsock_do_updatealg(spds->spds_extv_algs, spds);
3171*12273SCasper.Dik@Sun.COM 		freemsg(spds->spds_mp_algs);
3172*12273SCasper.Dik@Sun.COM 		spds->spds_mp_algs = NULL;
31730Sstevel@tonic-gate 	}
31743448Sdh155122 	mutex_exit(&spds->spds_alg_lock);
31750Sstevel@tonic-gate }
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate static void
spdsock_loadcheck(void * arg)31780Sstevel@tonic-gate spdsock_loadcheck(void *arg)
31790Sstevel@tonic-gate {
31800Sstevel@tonic-gate 	queue_t *q = (queue_t *)arg;
31810Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
31820Sstevel@tonic-gate 	mblk_t *mp;
31836430Sdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 	ASSERT(ss != NULL);
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 	ss->spdsock_timeout = 0;
31880Sstevel@tonic-gate 	mp = ss->spdsock_timeout_arg;
31890Sstevel@tonic-gate 	ASSERT(mp != NULL);
31900Sstevel@tonic-gate 	ss->spdsock_timeout_arg = NULL;
31913448Sdh155122 	if (ipsec_failed(ipss))
31920Sstevel@tonic-gate 		spdsock_error(q, mp, EPROTONOSUPPORT, 0);
31930Sstevel@tonic-gate 	else
31940Sstevel@tonic-gate 		spdsock_parse(q, mp);
31950Sstevel@tonic-gate }
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate /*
31980Sstevel@tonic-gate  * Copy relevant state bits.
31990Sstevel@tonic-gate  */
32000Sstevel@tonic-gate static void
spdsock_copy_info(struct T_info_ack * tap,spdsock_t * ss)32010Sstevel@tonic-gate spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss)
32020Sstevel@tonic-gate {
32030Sstevel@tonic-gate 	*tap = spdsock_g_t_info_ack;
32040Sstevel@tonic-gate 	tap->CURRENT_state = ss->spdsock_state;
32050Sstevel@tonic-gate 	tap->OPT_size = spdsock_max_optsize;
32060Sstevel@tonic-gate }
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate /*
32090Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
32100Sstevel@tonic-gate  * spdsock_wput.  Much of the T_CAPABILITY_ACK information is copied from
32110Sstevel@tonic-gate  * spdsock_g_t_info_ack.  The current state of the stream is copied from
32120Sstevel@tonic-gate  * spdsock_state.
32130Sstevel@tonic-gate  */
32140Sstevel@tonic-gate static void
spdsock_capability_req(queue_t * q,mblk_t * mp)32150Sstevel@tonic-gate spdsock_capability_req(queue_t *q, mblk_t *mp)
32160Sstevel@tonic-gate {
32170Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
32180Sstevel@tonic-gate 	t_uscalar_t cap_bits1;
32190Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
32245240Snordmark 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
32250Sstevel@tonic-gate 	if (mp == NULL)
32260Sstevel@tonic-gate 		return;
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
32290Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
32320Sstevel@tonic-gate 		spdsock_copy_info(&tcap->INFO_ack, ss);
32330Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
32340Sstevel@tonic-gate 	}
32350Sstevel@tonic-gate 
32360Sstevel@tonic-gate 	qreply(q, mp);
32370Sstevel@tonic-gate }
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate /*
32400Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages. It is called by
32410Sstevel@tonic-gate  * spdsock_wput_other.
32420Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack.
32430Sstevel@tonic-gate  * The current state of the stream is copied from spdsock_state.
32440Sstevel@tonic-gate  */
32450Sstevel@tonic-gate static void
spdsock_info_req(q,mp)32460Sstevel@tonic-gate spdsock_info_req(q, mp)
32470Sstevel@tonic-gate 	queue_t	*q;
32480Sstevel@tonic-gate 	mblk_t	*mp;
32490Sstevel@tonic-gate {
32500Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
32510Sstevel@tonic-gate 	    T_INFO_ACK);
32520Sstevel@tonic-gate 	if (mp == NULL)
32530Sstevel@tonic-gate 		return;
32540Sstevel@tonic-gate 	spdsock_copy_info((struct T_info_ack *)mp->b_rptr,
32550Sstevel@tonic-gate 	    (spdsock_t *)q->q_ptr);
32560Sstevel@tonic-gate 	qreply(q, mp);
32570Sstevel@tonic-gate }
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate /*
32600Sstevel@tonic-gate  * spdsock_err_ack. This routine creates a
32610Sstevel@tonic-gate  * T_ERROR_ACK message and passes it
32620Sstevel@tonic-gate  * upstream.
32630Sstevel@tonic-gate  */
32640Sstevel@tonic-gate static void
spdsock_err_ack(q,mp,t_error,sys_error)32650Sstevel@tonic-gate spdsock_err_ack(q, mp, t_error, sys_error)
32660Sstevel@tonic-gate 	queue_t	*q;
32670Sstevel@tonic-gate 	mblk_t	*mp;
32680Sstevel@tonic-gate 	int	t_error;
32690Sstevel@tonic-gate 	int	sys_error;
32700Sstevel@tonic-gate {
32710Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
32720Sstevel@tonic-gate 		qreply(q, mp);
32730Sstevel@tonic-gate }
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate /*
32760Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
32770Sstevel@tonic-gate  * It returns the size of the option retrieved.
32780Sstevel@tonic-gate  */
32790Sstevel@tonic-gate /* ARGSUSED */
32800Sstevel@tonic-gate int
spdsock_opt_get(queue_t * q,int level,int name,uchar_t * ptr)32810Sstevel@tonic-gate spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
32820Sstevel@tonic-gate {
32830Sstevel@tonic-gate 	int *i1 = (int *)ptr;
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate 	switch (level) {
32860Sstevel@tonic-gate 	case SOL_SOCKET:
32870Sstevel@tonic-gate 		switch (name) {
32880Sstevel@tonic-gate 		case SO_TYPE:
32890Sstevel@tonic-gate 			*i1 = SOCK_RAW;
32900Sstevel@tonic-gate 			break;
32910Sstevel@tonic-gate 		/*
32920Sstevel@tonic-gate 		 * The following two items can be manipulated,
32930Sstevel@tonic-gate 		 * but changing them should do nothing.
32940Sstevel@tonic-gate 		 */
32950Sstevel@tonic-gate 		case SO_SNDBUF:
32960Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
32970Sstevel@tonic-gate 			break;
32980Sstevel@tonic-gate 		case SO_RCVBUF:
32990Sstevel@tonic-gate 			*i1 = (int)(RD(q)->q_hiwat);
33000Sstevel@tonic-gate 			break;
33010Sstevel@tonic-gate 		}
33020Sstevel@tonic-gate 		break;
33030Sstevel@tonic-gate 	default:
33040Sstevel@tonic-gate 		return (0);
33050Sstevel@tonic-gate 	}
33060Sstevel@tonic-gate 	return (sizeof (int));
33070Sstevel@tonic-gate }
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate /*
33100Sstevel@tonic-gate  * This routine sets socket options.
33110Sstevel@tonic-gate  */
33120Sstevel@tonic-gate /* ARGSUSED */
33130Sstevel@tonic-gate int
spdsock_opt_set(queue_t * q,uint_t mgmt_flags,int level,int name,uint_t inlen,uchar_t * invalp,uint_t * outlenp,uchar_t * outvalp,void * thisdg_attrs,cred_t * cr)33140Sstevel@tonic-gate spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name,
33150Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
331611042SErik.Nordmark@Sun.COM     void *thisdg_attrs, cred_t *cr)
33170Sstevel@tonic-gate {
33180Sstevel@tonic-gate 	int *i1 = (int *)invalp;
33193448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
33203448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate 	switch (level) {
33230Sstevel@tonic-gate 	case SOL_SOCKET:
33240Sstevel@tonic-gate 		switch (name) {
33250Sstevel@tonic-gate 		case SO_SNDBUF:
33263448Sdh155122 			if (*i1 > spds->spds_max_buf)
33270Sstevel@tonic-gate 				return (ENOBUFS);
33280Sstevel@tonic-gate 			q->q_hiwat = *i1;
33290Sstevel@tonic-gate 			break;
33300Sstevel@tonic-gate 		case SO_RCVBUF:
33313448Sdh155122 			if (*i1 > spds->spds_max_buf)
33320Sstevel@tonic-gate 				return (ENOBUFS);
33330Sstevel@tonic-gate 			RD(q)->q_hiwat = *i1;
33348348SEric.Yu@Sun.COM 			(void) proto_set_rx_hiwat(RD(q), NULL, *i1);
33350Sstevel@tonic-gate 			break;
33360Sstevel@tonic-gate 		}
33370Sstevel@tonic-gate 		break;
33380Sstevel@tonic-gate 	}
33390Sstevel@tonic-gate 	return (0);
33400Sstevel@tonic-gate }
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate 
33430Sstevel@tonic-gate /*
33440Sstevel@tonic-gate  * Handle STREAMS messages.
33450Sstevel@tonic-gate  */
33460Sstevel@tonic-gate static void
spdsock_wput_other(queue_t * q,mblk_t * mp)33470Sstevel@tonic-gate spdsock_wput_other(queue_t *q, mblk_t *mp)
33480Sstevel@tonic-gate {
33490Sstevel@tonic-gate 	struct iocblk *iocp;
33500Sstevel@tonic-gate 	int error;
33513448Sdh155122 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
33523448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
33533448Sdh155122 	cred_t		*cr;
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
33560Sstevel@tonic-gate 	case M_PROTO:
33570Sstevel@tonic-gate 	case M_PCPROTO:
33580Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
33593448Sdh155122 			ss3dbg(spds, (
33600Sstevel@tonic-gate 			    "spdsock_wput_other: Not big enough M_PROTO\n"));
33610Sstevel@tonic-gate 			freemsg(mp);
33620Sstevel@tonic-gate 			return;
33630Sstevel@tonic-gate 		}
33640Sstevel@tonic-gate 		switch (((union T_primitives *)mp->b_rptr)->type) {
33650Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
33660Sstevel@tonic-gate 			spdsock_capability_req(q, mp);
33673448Sdh155122 			break;
33680Sstevel@tonic-gate 		case T_INFO_REQ:
33690Sstevel@tonic-gate 			spdsock_info_req(q, mp);
33703448Sdh155122 			break;
33710Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
33720Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
33738778SErik.Nordmark@Sun.COM 			/*
33748778SErik.Nordmark@Sun.COM 			 * All Solaris components should pass a db_credp
33758778SErik.Nordmark@Sun.COM 			 * for this TPI message, hence we ASSERT.
33768778SErik.Nordmark@Sun.COM 			 * But in case there is some other M_PROTO that looks
33778778SErik.Nordmark@Sun.COM 			 * like a TPI message sent by some other kernel
33788778SErik.Nordmark@Sun.COM 			 * component, we check and return an error.
33798778SErik.Nordmark@Sun.COM 			 */
33808778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, NULL);
33818778SErik.Nordmark@Sun.COM 			ASSERT(cr != NULL);
33828778SErik.Nordmark@Sun.COM 			if (cr == NULL) {
33838778SErik.Nordmark@Sun.COM 				spdsock_err_ack(q, mp, TSYSERR, EINVAL);
33848778SErik.Nordmark@Sun.COM 				return;
33858778SErik.Nordmark@Sun.COM 			}
33868778SErik.Nordmark@Sun.COM 			if (((union T_primitives *)mp->b_rptr)->type ==
33878778SErik.Nordmark@Sun.COM 			    T_SVR4_OPTMGMT_REQ) {
338811042SErik.Nordmark@Sun.COM 				svr4_optcom_req(q, mp, cr, &spdsock_opt_obj);
33898778SErik.Nordmark@Sun.COM 			} else {
339011042SErik.Nordmark@Sun.COM 				tpi_optcom_req(q, mp, cr, &spdsock_opt_obj);
33918778SErik.Nordmark@Sun.COM 			}
33923448Sdh155122 			break;
33930Sstevel@tonic-gate 		case T_DATA_REQ:
33940Sstevel@tonic-gate 		case T_EXDATA_REQ:
33950Sstevel@tonic-gate 		case T_ORDREL_REQ:
33960Sstevel@tonic-gate 			/* Illegal for spdsock. */
33970Sstevel@tonic-gate 			freemsg(mp);
33980Sstevel@tonic-gate 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
33993448Sdh155122 			break;
34000Sstevel@tonic-gate 		default:
34010Sstevel@tonic-gate 			/* Not supported by spdsock. */
34020Sstevel@tonic-gate 			spdsock_err_ack(q, mp, TNOTSUPPORT, 0);
34033448Sdh155122 			break;
34040Sstevel@tonic-gate 		}
34053448Sdh155122 		return;
34060Sstevel@tonic-gate 	case M_IOCTL:
34070Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
34080Sstevel@tonic-gate 		error = EINVAL;
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
34110Sstevel@tonic-gate 		case ND_SET:
34120Sstevel@tonic-gate 		case ND_GET:
34133448Sdh155122 			if (nd_getset(q, spds->spds_g_nd, mp)) {
34140Sstevel@tonic-gate 				qreply(q, mp);
34150Sstevel@tonic-gate 				return;
34160Sstevel@tonic-gate 			} else
34170Sstevel@tonic-gate 				error = ENOENT;
34180Sstevel@tonic-gate 			/* FALLTHRU */
34190Sstevel@tonic-gate 		default:
34200Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
34210Sstevel@tonic-gate 			return;
34220Sstevel@tonic-gate 		}
34230Sstevel@tonic-gate 	case M_FLUSH:
34240Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
34250Sstevel@tonic-gate 			flushq(q, FLUSHALL);
34260Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
34270Sstevel@tonic-gate 		}
34280Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
34290Sstevel@tonic-gate 			qreply(q, mp);
34300Sstevel@tonic-gate 			return;
34310Sstevel@tonic-gate 		}
34320Sstevel@tonic-gate 		/* Else FALLTHRU */
34330Sstevel@tonic-gate 	}
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 	/* If fell through, just black-hole the message. */
34360Sstevel@tonic-gate 	freemsg(mp);
34370Sstevel@tonic-gate }
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate static void
spdsock_wput(queue_t * q,mblk_t * mp)34400Sstevel@tonic-gate spdsock_wput(queue_t *q, mblk_t *mp)
34410Sstevel@tonic-gate {
34420Sstevel@tonic-gate 	uint8_t *rptr = mp->b_rptr;
34430Sstevel@tonic-gate 	mblk_t *mp1;
34440Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
34453448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate 	/*
34480Sstevel@tonic-gate 	 * If we're dumping, defer processing other messages until the
34490Sstevel@tonic-gate 	 * dump completes.
34500Sstevel@tonic-gate 	 */
34510Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL) {
34520Sstevel@tonic-gate 		if (!putq(q, mp))
34530Sstevel@tonic-gate 			freemsg(mp);
34540Sstevel@tonic-gate 		return;
34550Sstevel@tonic-gate 	}
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
34580Sstevel@tonic-gate 	case M_DATA:
34590Sstevel@tonic-gate 		/*
34600Sstevel@tonic-gate 		 * Silently discard.
34610Sstevel@tonic-gate 		 */
34623448Sdh155122 		ss2dbg(spds, ("raw M_DATA in spdsock.\n"));
34630Sstevel@tonic-gate 		freemsg(mp);
34640Sstevel@tonic-gate 		return;
34650Sstevel@tonic-gate 	case M_PROTO:
34660Sstevel@tonic-gate 	case M_PCPROTO:
34670Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
34680Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
34690Sstevel@tonic-gate 				if ((mp1 = mp->b_cont) == NULL) {
34700Sstevel@tonic-gate 					/* No data after T_DATA_REQ. */
34713448Sdh155122 					ss2dbg(spds,
34723448Sdh155122 					    ("No data after DATA_REQ.\n"));
34730Sstevel@tonic-gate 					freemsg(mp);
34740Sstevel@tonic-gate 					return;
34750Sstevel@tonic-gate 				}
34760Sstevel@tonic-gate 				freeb(mp);
34770Sstevel@tonic-gate 				mp = mp1;
34783448Sdh155122 				ss2dbg(spds, ("T_DATA_REQ\n"));
34790Sstevel@tonic-gate 				break;	/* Out of switch. */
34800Sstevel@tonic-gate 			}
34810Sstevel@tonic-gate 		}
34820Sstevel@tonic-gate 		/* FALLTHRU */
34830Sstevel@tonic-gate 	default:
34843448Sdh155122 		ss3dbg(spds, ("In default wput case (%d %d).\n",
34850Sstevel@tonic-gate 		    mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
34860Sstevel@tonic-gate 		spdsock_wput_other(q, mp);
34870Sstevel@tonic-gate 		return;
34880Sstevel@tonic-gate 	}
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate 	/* I now have a PF_POLICY message in an M_DATA block. */
34910Sstevel@tonic-gate 	spdsock_parse(q, mp);
34920Sstevel@tonic-gate }
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate /*
34950Sstevel@tonic-gate  * Device open procedure, called when new queue pair created.
34960Sstevel@tonic-gate  * We are passed the read-side queue.
34970Sstevel@tonic-gate  */
34980Sstevel@tonic-gate /* ARGSUSED */
34990Sstevel@tonic-gate static int
spdsock_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)35000Sstevel@tonic-gate spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
35010Sstevel@tonic-gate {
35020Sstevel@tonic-gate 	spdsock_t *ss;
35030Sstevel@tonic-gate 	queue_t *oq = OTHERQ(q);
35040Sstevel@tonic-gate 	minor_t ssminor;
35053448Sdh155122 	netstack_t *ns;
35063448Sdh155122 	spd_stack_t *spds;
35073448Sdh155122 
35083448Sdh155122 	if (secpolicy_ip_config(credp, B_FALSE) != 0)
35090Sstevel@tonic-gate 		return (EPERM);
35100Sstevel@tonic-gate 
35110Sstevel@tonic-gate 	if (q->q_ptr != NULL)
35120Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 	if (sflag & MODOPEN)
35150Sstevel@tonic-gate 		return (EINVAL);
35160Sstevel@tonic-gate 
35173448Sdh155122 	ns = netstack_find_by_cred(credp);
35183448Sdh155122 	ASSERT(ns != NULL);
35193448Sdh155122 	spds = ns->netstack_spdsock;
35203448Sdh155122 	ASSERT(spds != NULL);
35213448Sdh155122 
35223448Sdh155122 	ss2dbg(spds, ("Made it into PF_POLICY socket open.\n"));
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 	ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP);
35253448Sdh155122 	if (ssminor == 0) {
35263448Sdh155122 		netstack_rele(spds->spds_netstack);
35270Sstevel@tonic-gate 		return (ENOMEM);
35283448Sdh155122 	}
35290Sstevel@tonic-gate 	ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP);
35300Sstevel@tonic-gate 	if (ss == NULL) {
35310Sstevel@tonic-gate 		vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1);
35323448Sdh155122 		netstack_rele(spds->spds_netstack);
35330Sstevel@tonic-gate 		return (ENOMEM);
35340Sstevel@tonic-gate 	}
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	ss->spdsock_minor = ssminor;
35370Sstevel@tonic-gate 	ss->spdsock_state = TS_UNBND;
35380Sstevel@tonic-gate 	ss->spdsock_dump_req = NULL;
35390Sstevel@tonic-gate 
35403448Sdh155122 	ss->spdsock_spds = spds;
35413448Sdh155122 
35420Sstevel@tonic-gate 	q->q_ptr = ss;
35430Sstevel@tonic-gate 	oq->q_ptr = ss;
35440Sstevel@tonic-gate 
35453448Sdh155122 	q->q_hiwat = spds->spds_recv_hiwat;
35463448Sdh155122 
35473448Sdh155122 	oq->q_hiwat = spds->spds_xmit_hiwat;
35483448Sdh155122 	oq->q_lowat = spds->spds_xmit_lowat;
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate 	qprocson(q);
35518348SEric.Yu@Sun.COM 	(void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat);
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), ss->spdsock_minor);
35540Sstevel@tonic-gate 	return (0);
35550Sstevel@tonic-gate }
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate /*
35580Sstevel@tonic-gate  * Read-side service procedure, invoked when we get back-enabled
35590Sstevel@tonic-gate  * when buffer space becomes available.
35600Sstevel@tonic-gate  *
35610Sstevel@tonic-gate  * Dump another chunk if we were dumping before; when we finish, kick
35620Sstevel@tonic-gate  * the write-side queue in case it's waiting for read queue space.
35630Sstevel@tonic-gate  */
35640Sstevel@tonic-gate void
spdsock_rsrv(queue_t * q)35650Sstevel@tonic-gate spdsock_rsrv(queue_t *q)
35660Sstevel@tonic-gate {
35670Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL)
35700Sstevel@tonic-gate 		spdsock_dump_some(q, ss);
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 	if (ss->spdsock_dump_req == NULL)
35730Sstevel@tonic-gate 		qenable(OTHERQ(q));
35740Sstevel@tonic-gate }
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate /*
35770Sstevel@tonic-gate  * Write-side service procedure, invoked when we defer processing
35780Sstevel@tonic-gate  * if another message is received while a dump is in progress.
35790Sstevel@tonic-gate  */
35800Sstevel@tonic-gate void
spdsock_wsrv(queue_t * q)35810Sstevel@tonic-gate spdsock_wsrv(queue_t *q)
35820Sstevel@tonic-gate {
35830Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
35840Sstevel@tonic-gate 	mblk_t *mp;
35856430Sdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL) {
35880Sstevel@tonic-gate 		qenable(OTHERQ(q));
35890Sstevel@tonic-gate 		return;
35900Sstevel@tonic-gate 	}
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
35933448Sdh155122 		if (ipsec_loaded(ipss)) {
35940Sstevel@tonic-gate 			spdsock_wput(q, mp);
35950Sstevel@tonic-gate 			if (ss->spdsock_dump_req != NULL)
35960Sstevel@tonic-gate 				return;
35973448Sdh155122 		} else if (!ipsec_failed(ipss)) {
35980Sstevel@tonic-gate 			(void) putq(q, mp);
35990Sstevel@tonic-gate 		} else {
36000Sstevel@tonic-gate 			spdsock_error(q, mp, EPFNOSUPPORT, 0);
36010Sstevel@tonic-gate 		}
36020Sstevel@tonic-gate 	}
36030Sstevel@tonic-gate }
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate static int
spdsock_close(queue_t * q)36060Sstevel@tonic-gate spdsock_close(queue_t *q)
36070Sstevel@tonic-gate {
36080Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
36093448Sdh155122 	spd_stack_t	*spds = ss->spdsock_spds;
36100Sstevel@tonic-gate 
36110Sstevel@tonic-gate 	qprocsoff(q);
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 	/* Safe assumption. */
36140Sstevel@tonic-gate 	ASSERT(ss != NULL);
36150Sstevel@tonic-gate 
36160Sstevel@tonic-gate 	if (ss->spdsock_timeout != 0)
36170Sstevel@tonic-gate 		(void) quntimeout(q, ss->spdsock_timeout);
36180Sstevel@tonic-gate 
36193448Sdh155122 	ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n"));
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 	vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1);
36223448Sdh155122 	netstack_rele(ss->spdsock_spds->spds_netstack);
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	kmem_free(ss, sizeof (spdsock_t));
36250Sstevel@tonic-gate 	return (0);
36260Sstevel@tonic-gate }
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate /*
36290Sstevel@tonic-gate  * Merge the IPsec algorithms tables with the received algorithm information.
36300Sstevel@tonic-gate  */
36310Sstevel@tonic-gate void
spdsock_merge_algs(spd_stack_t * spds)36323448Sdh155122 spdsock_merge_algs(spd_stack_t *spds)
36330Sstevel@tonic-gate {
36340Sstevel@tonic-gate 	ipsec_alginfo_t *alg, *oalg;
36350Sstevel@tonic-gate 	ipsec_algtype_t algtype;
36360Sstevel@tonic-gate 	uint_t algidx, algid, nalgs;
36370Sstevel@tonic-gate 	crypto_mech_name_t *mechs;
36380Sstevel@tonic-gate 	uint_t mech_count, mech_idx;
36393448Sdh155122 	netstack_t	*ns = spds->spds_netstack;
36403448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
36413448Sdh155122 
36423448Sdh155122 	ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 	/*
36450Sstevel@tonic-gate 	 * Get the list of supported mechanisms from the crypto framework.
36460Sstevel@tonic-gate 	 * If a mechanism is supported by KCF, resolve its mechanism
36470Sstevel@tonic-gate 	 * id and mark it as being valid. This operation must be done
36480Sstevel@tonic-gate 	 * without holding alg_lock, since it can cause a provider
36490Sstevel@tonic-gate 	 * module to be loaded and the provider notification callback to
36500Sstevel@tonic-gate 	 * be invoked.
36510Sstevel@tonic-gate 	 */
36520Sstevel@tonic-gate 	mechs = crypto_get_mech_list(&mech_count, KM_SLEEP);
36530Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
36540Sstevel@tonic-gate 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
36550Sstevel@tonic-gate 			int algflags = 0;
36560Sstevel@tonic-gate 			crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID;
36570Sstevel@tonic-gate 
36583448Sdh155122 			alg = spds->spds_algs[algtype][algid];
36593448Sdh155122 			if (alg == NULL)
36600Sstevel@tonic-gate 				continue;
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate 			/*
36630Sstevel@tonic-gate 			 * The NULL encryption algorithm is a special
36640Sstevel@tonic-gate 			 * case because there are no mechanisms, yet
36650Sstevel@tonic-gate 			 * the algorithm is still valid.
36660Sstevel@tonic-gate 			 */
36670Sstevel@tonic-gate 			if (alg->alg_id == SADB_EALG_NULL) {
36680Sstevel@tonic-gate 				alg->alg_mech_type = CRYPTO_MECHANISM_INVALID;
366910824SMark.Fenwick@Sun.COM 				alg->alg_flags |= ALG_FLAG_VALID;
36700Sstevel@tonic-gate 				continue;
36710Sstevel@tonic-gate 			}
36720Sstevel@tonic-gate 
36730Sstevel@tonic-gate 			for (mech_idx = 0; mech_idx < mech_count; mech_idx++) {
36740Sstevel@tonic-gate 				if (strncmp(alg->alg_mech_name, mechs[mech_idx],
36750Sstevel@tonic-gate 				    CRYPTO_MAX_MECH_NAME) == 0) {
36760Sstevel@tonic-gate 					mt = crypto_mech2id(alg->alg_mech_name);
36770Sstevel@tonic-gate 					ASSERT(mt != CRYPTO_MECHANISM_INVALID);
36780Sstevel@tonic-gate 					algflags = ALG_FLAG_VALID;
36790Sstevel@tonic-gate 					break;
36800Sstevel@tonic-gate 				}
36810Sstevel@tonic-gate 			}
36820Sstevel@tonic-gate 			alg->alg_mech_type = mt;
368310824SMark.Fenwick@Sun.COM 			alg->alg_flags |= algflags;
36840Sstevel@tonic-gate 		}
36850Sstevel@tonic-gate 	}
36860Sstevel@tonic-gate 
36873448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 	/*
36900Sstevel@tonic-gate 	 * For each algorithm currently defined, check if it is
36910Sstevel@tonic-gate 	 * present in the new tables created from the SPD_UPDATEALGS
36920Sstevel@tonic-gate 	 * message received from user-space.
36930Sstevel@tonic-gate 	 * Delete the algorithm entries that are currently defined
36940Sstevel@tonic-gate 	 * but not part of the new tables.
36950Sstevel@tonic-gate 	 */
36960Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
36973448Sdh155122 		nalgs = ipss->ipsec_nalgs[algtype];
36980Sstevel@tonic-gate 		for (algidx = 0; algidx < nalgs; algidx++) {
36993448Sdh155122 			algid = ipss->ipsec_sortlist[algtype][algidx];
37003448Sdh155122 			if (spds->spds_algs[algtype][algid] == NULL)
37013448Sdh155122 				ipsec_alg_unreg(algtype, algid, ns);
37020Sstevel@tonic-gate 		}
37030Sstevel@tonic-gate 	}
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate 	/*
37060Sstevel@tonic-gate 	 * For each algorithm we just received, check if it is
37070Sstevel@tonic-gate 	 * present in the currently defined tables. If it is, swap
37080Sstevel@tonic-gate 	 * the entry with the one we just allocated.
37090Sstevel@tonic-gate 	 * If the new algorithm is not in the current tables,
37100Sstevel@tonic-gate 	 * add it.
37110Sstevel@tonic-gate 	 */
37120Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
37130Sstevel@tonic-gate 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
37143448Sdh155122 			alg = spds->spds_algs[algtype][algid];
37153448Sdh155122 			if (alg == NULL)
37160Sstevel@tonic-gate 				continue;
37170Sstevel@tonic-gate 
37183448Sdh155122 			if ((oalg = ipss->ipsec_alglists[algtype][algid]) ==
37193448Sdh155122 			    NULL) {
37200Sstevel@tonic-gate 				/*
37210Sstevel@tonic-gate 				 * New algorithm, add it to the algorithm
37220Sstevel@tonic-gate 				 * table.
37230Sstevel@tonic-gate 				 */
37243448Sdh155122 				ipsec_alg_reg(algtype, alg, ns);
37250Sstevel@tonic-gate 			} else {
37260Sstevel@tonic-gate 				/*
37270Sstevel@tonic-gate 				 * Algorithm is already in the table. Swap
37280Sstevel@tonic-gate 				 * the existing entry with the new one.
37290Sstevel@tonic-gate 				 */
37303448Sdh155122 				ipsec_alg_fix_min_max(alg, algtype, ns);
37313448Sdh155122 				ipss->ipsec_alglists[algtype][algid] = alg;
37320Sstevel@tonic-gate 				ipsec_alg_free(oalg);
37330Sstevel@tonic-gate 			}
37343448Sdh155122 			spds->spds_algs[algtype][algid] = NULL;
37350Sstevel@tonic-gate 		}
37360Sstevel@tonic-gate 	}
37370Sstevel@tonic-gate 
37383448Sdh155122 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
37393448Sdh155122 		ipss->ipsec_algs_exec_mode[algtype] =
37403448Sdh155122 		    spds->spds_algs_exec_mode[algtype];
37413448Sdh155122 	}
37423448Sdh155122 
37433448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
37440Sstevel@tonic-gate 
37450Sstevel@tonic-gate 	crypto_free_mech_list(mechs, mech_count);
37460Sstevel@tonic-gate 
37473448Sdh155122 	ipsecah_algs_changed(ns);
37483448Sdh155122 	ipsecesp_algs_changed(ns);
37490Sstevel@tonic-gate }
3750