xref: /illumos-gate/usr/src/uts/common/inet/ip/spdsock.c (revision fd82d5a2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58810c16bSdanmcd  * Common Development and Distribution License (the "License").
68810c16bSdanmcd  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22134a1f4eSCasper H.S. Dik  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2369e71331SBayard Bell  * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
2478a53e20SJohn Levon  * Copyright 2019, Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/stream.h>
307c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
317c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
327c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
33f4b3ec61Sdh #include <sys/zone.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
377c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
38f8cbe0e7SDan McDonald #include <sys/timod.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
427c478bd9Sstevel@tonic-gate #include <sys/debug.h>
437c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/suntpi.h>
467c478bd9Sstevel@tonic-gate #include <sys/policy.h>
472b24ab6bSSebastien Roy #include <sys/dls.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <sys/socket.h>
507c478bd9Sstevel@tonic-gate #include <netinet/in.h>
517c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h>
527c478bd9Sstevel@tonic-gate #include <net/pfpolicy.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <inet/common.h>
557c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
567c478bd9Sstevel@tonic-gate #include <inet/ip.h>
577c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
587c478bd9Sstevel@tonic-gate #include <inet/mi.h>
590f1702c5SYu Xiangning #include <inet/proto_set.h>
607c478bd9Sstevel@tonic-gate #include <inet/nd.h>
618810c16bSdanmcd #include <inet/ip_if.h>
627c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
637c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
647c478bd9Sstevel@tonic-gate #include <inet/spdsock.h>
657c478bd9Sstevel@tonic-gate #include <inet/sadb.h>
662b24ab6bSSebastien Roy #include <inet/iptun.h>
672b24ab6bSSebastien Roy #include <inet/iptun/iptun_impl.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
707c478bd9Sstevel@tonic-gate 
71799bd290Spwernau #include <c2/audit.h>
72799bd290Spwernau 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * This is a transport provider for the PF_POLICY IPsec policy
757c478bd9Sstevel@tonic-gate  * management socket, which provides a management interface into the
767c478bd9Sstevel@tonic-gate  * SPD, allowing policy rules to be added, deleted, and queried.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * This effectively replaces the old private SIOC*IPSECONFIG ioctls
797c478bd9Sstevel@tonic-gate  * with an extensible interface which will hopefully be public some
807c478bd9Sstevel@tonic-gate  * day.
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  * See <net/pfpolicy.h> for more details on the protocol.
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * We link against drv/ip and call directly into it to manipulate the
857c478bd9Sstevel@tonic-gate  * SPD; see ipsec_impl.h for the policy data structures and spd.c for
867c478bd9Sstevel@tonic-gate  * the code which maintains them.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * The MT model of this is QPAIR with the addition of some explicit
897c478bd9Sstevel@tonic-gate  * locking to protect system-wide policy data structures.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static vmem_t *spdsock_vmem;		/* for minor numbers. */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t))
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
977c478bd9Sstevel@tonic-gate static struct T_info_ack spdsock_g_t_info_ack = {
987c478bd9Sstevel@tonic-gate 	T_INFO_ACK,
997c478bd9Sstevel@tonic-gate 	T_INFINITE,	/* TSDU_size. Maximum size messages. */
1007c478bd9Sstevel@tonic-gate 	T_INVALID,	/* ETSDU_size. No expedited data. */
1017c478bd9Sstevel@tonic-gate 	T_INVALID,	/* CDATA_size. No connect data. */
1027c478bd9Sstevel@tonic-gate 	T_INVALID,	/* DDATA_size. No disconnect data. */
1037c478bd9Sstevel@tonic-gate 	0,		/* ADDR_size. */
1047c478bd9Sstevel@tonic-gate 	0,		/* OPT_size. No user-settable options */
1057c478bd9Sstevel@tonic-gate 	64 * 1024,	/* TIDU_size. spdsock allows maximum size messages. */
1067c478bd9Sstevel@tonic-gate 	T_COTS,		/* SERV_type. spdsock supports connection oriented. */
1077c478bd9Sstevel@tonic-gate 	TS_UNBND,	/* CURRENT_state. This is set from spdsock_state. */
1087c478bd9Sstevel@tonic-gate 	(XPG4_1)	/* Provider flags */
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
112f4b3ec61Sdh typedef struct spdsockparam_s {
1137c478bd9Sstevel@tonic-gate 	uint_t	spdsock_param_min;
1147c478bd9Sstevel@tonic-gate 	uint_t	spdsock_param_max;
1157c478bd9Sstevel@tonic-gate 	uint_t	spdsock_param_value;
1167c478bd9Sstevel@tonic-gate 	char *spdsock_param_name;
1177c478bd9Sstevel@tonic-gate } spdsockparam_t;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Table of NDD variables supported by spdsock. These are loaded into
1217c478bd9Sstevel@tonic-gate  * spdsock_g_nd in spdsock_init_nd.
1227c478bd9Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
1237c478bd9Sstevel@tonic-gate  */
124f4b3ec61Sdh static	spdsockparam_t	lcl_param_arr[] = {
1257c478bd9Sstevel@tonic-gate 	/* min	max	value	name */
1267c478bd9Sstevel@tonic-gate 	{ 4096, 65536,	8192,	"spdsock_xmit_hiwat"},
1277c478bd9Sstevel@tonic-gate 	{ 0,	65536,	1024,	"spdsock_xmit_lowat"},
1287c478bd9Sstevel@tonic-gate 	{ 4096, 65536,	8192,	"spdsock_recv_hiwat"},
1297c478bd9Sstevel@tonic-gate 	{ 65536, 1024*1024*1024, 256*1024,	"spdsock_max_buf"},
1307c478bd9Sstevel@tonic-gate 	{ 0,	3,	0,	"spdsock_debug"},
1317c478bd9Sstevel@tonic-gate };
132f4b3ec61Sdh #define	spds_xmit_hiwat	spds_params[0].spdsock_param_value
133f4b3ec61Sdh #define	spds_xmit_lowat	spds_params[1].spdsock_param_value
134f4b3ec61Sdh #define	spds_recv_hiwat	spds_params[2].spdsock_param_value
135f4b3ec61Sdh #define	spds_max_buf	spds_params[3].spdsock_param_value
136f4b3ec61Sdh #define	spds_debug		spds_params[4].spdsock_param_value
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	ss0dbg(a)	printf a
1397c478bd9Sstevel@tonic-gate /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
140f4b3ec61Sdh #define	ss1dbg(spds, a)	if (spds->spds_debug != 0) printf a
141f4b3ec61Sdh #define	ss2dbg(spds, a)	if (spds->spds_debug > 1) printf a
142f4b3ec61Sdh #define	ss3dbg(spds, a)	if (spds->spds_debug > 2) printf a
1437c478bd9Sstevel@tonic-gate 
144db061fdfSdanmcd #define	RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \
145db061fdfSdanmcd 	ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \
146db061fdfSdanmcd 	(ss)->spdsock_dump_head = (iph); \
147db061fdfSdanmcd 	(ss)->spdsock_dump_gen = (iph)->iph_gen; \
148db061fdfSdanmcd 	(ss)->spdsock_dump_cur_type = 0; \
149db061fdfSdanmcd 	(ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \
150db061fdfSdanmcd 	(ss)->spdsock_dump_cur_rule = NULL; \
151db061fdfSdanmcd 	(ss)->spdsock_dump_count = 0; \
152db061fdfSdanmcd 	(ss)->spdsock_dump_cur_chain = 0; \
153db061fdfSdanmcd }
154db061fdfSdanmcd 
1555e1743f0SToomas Soome static int spdsock_close(queue_t *, int, cred_t *);
1567c478bd9Sstevel@tonic-gate static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *);
1571752e73cSToomas Soome static int spdsock_wput(queue_t *, mblk_t *);
1581752e73cSToomas Soome static int spdsock_wsrv(queue_t *);
1591752e73cSToomas Soome static int spdsock_rsrv(queue_t *);
160f4b3ec61Sdh static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns);
161134a1f4eSCasper H.S. Dik static void spdsock_stack_shutdown(netstackid_t stackid, void *arg);
162f4b3ec61Sdh static void spdsock_stack_fini(netstackid_t stackid, void *arg);
1637c478bd9Sstevel@tonic-gate static void spdsock_loadcheck(void *);
164f4b3ec61Sdh static void spdsock_merge_algs(spd_stack_t *);
165f4b3ec61Sdh static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *);
1668810c16bSdanmcd static mblk_t *spdsock_dump_next_record(spdsock_t *);
16701ac885fSDan McDonald static void update_iptun_policy(ipsec_tun_pol_t *);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static struct module_info info = {
1707c478bd9Sstevel@tonic-gate 	5138, "spdsock", 1, INFPSZ, 512, 128
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static struct qinit rinit = {
1741752e73cSToomas Soome 	NULL, spdsock_rsrv, spdsock_open, spdsock_close,
1757c478bd9Sstevel@tonic-gate 	NULL, &info
1767c478bd9Sstevel@tonic-gate };
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static struct qinit winit = {
1791752e73cSToomas Soome 	spdsock_wput, spdsock_wsrv, NULL, NULL, NULL, &info
1807c478bd9Sstevel@tonic-gate };
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate struct streamtab spdsockinfo = {
1837c478bd9Sstevel@tonic-gate 	&rinit, &winit
1847c478bd9Sstevel@tonic-gate };
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /* mapping from alg type to protocol number, as per RFC 2407 */
1877c478bd9Sstevel@tonic-gate static const uint_t algproto[] = {
1887c478bd9Sstevel@tonic-gate 	PROTO_IPSEC_AH,
1897c478bd9Sstevel@tonic-gate 	PROTO_IPSEC_ESP,
1907c478bd9Sstevel@tonic-gate };
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #define	NALGPROTOS	(sizeof (algproto) / sizeof (algproto[0]))
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /* mapping from kernel exec mode to spdsock exec mode */
1957c478bd9Sstevel@tonic-gate static const uint_t execmodes[] = {
1967c478bd9Sstevel@tonic-gate 	SPD_ALG_EXEC_MODE_SYNC,
1977c478bd9Sstevel@tonic-gate 	SPD_ALG_EXEC_MODE_ASYNC
1987c478bd9Sstevel@tonic-gate };
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #define	NEXECMODES	(sizeof (execmodes) / sizeof (execmodes[0]))
2017c478bd9Sstevel@tonic-gate 
2028810c16bSdanmcd #define	ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1)
2038810c16bSdanmcd #define	ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2)
2048810c16bSdanmcd 
205799bd290Spwernau #define	ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL)
206799bd290Spwernau 
2077c478bd9Sstevel@tonic-gate /* ARGSUSED */
2087c478bd9Sstevel@tonic-gate static int
spdsock_param_get(queue_t * q,mblk_t * mp,caddr_t cp,cred_t * cr)20969e71331SBayard Bell spdsock_param_get(
21069e71331SBayard Bell     queue_t	*q,
21169e71331SBayard Bell     mblk_t	*mp,
21269e71331SBayard Bell     caddr_t	cp,
21369e71331SBayard Bell     cred_t *cr)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	spdsockparam_t	*spdsockpa = (spdsockparam_t *)cp;
2167c478bd9Sstevel@tonic-gate 	uint_t value;
217f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
218f4b3ec61Sdh 	spd_stack_t	*spds = ss->spdsock_spds;
2197c478bd9Sstevel@tonic-gate 
220f4b3ec61Sdh 	mutex_enter(&spds->spds_param_lock);
2217c478bd9Sstevel@tonic-gate 	value = spdsockpa->spdsock_param_value;
222f4b3ec61Sdh 	mutex_exit(&spds->spds_param_lock);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
2257c478bd9Sstevel@tonic-gate 	return (0);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /* This routine sets an NDD variable in a spdsockparam_t structure. */
2297c478bd9Sstevel@tonic-gate /* ARGSUSED */
2307c478bd9Sstevel@tonic-gate static int
spdsock_param_set(queue_t * q,mblk_t * mp,char * value,caddr_t cp,cred_t * cr)23169e71331SBayard Bell spdsock_param_set(
23269e71331SBayard Bell     queue_t	*q,
23369e71331SBayard Bell     mblk_t	*mp,
23469e71331SBayard Bell     char *value,
23569e71331SBayard Bell     caddr_t	cp,
23669e71331SBayard Bell     cred_t *cr)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	ulong_t	new_value;
2397c478bd9Sstevel@tonic-gate 	spdsockparam_t	*spdsockpa = (spdsockparam_t *)cp;
240f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
241f4b3ec61Sdh 	spd_stack_t	*spds = ss->spdsock_spds;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/* Convert the value from a string into a long integer. */
2447c478bd9Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
2457c478bd9Sstevel@tonic-gate 		return (EINVAL);
2467c478bd9Sstevel@tonic-gate 
247f4b3ec61Sdh 	mutex_enter(&spds->spds_param_lock);
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
2507c478bd9Sstevel@tonic-gate 	 * required bounds.
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	if (new_value < spdsockpa->spdsock_param_min ||
2537c478bd9Sstevel@tonic-gate 	    new_value > spdsockpa->spdsock_param_max) {
254f4b3ec61Sdh 		mutex_exit(&spds->spds_param_lock);
2557c478bd9Sstevel@tonic-gate 		return (EINVAL);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* Set the new value */
2597c478bd9Sstevel@tonic-gate 	spdsockpa->spdsock_param_value = new_value;
260f4b3ec61Sdh 	mutex_exit(&spds->spds_param_lock);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	return (0);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
265f4b3ec61Sdh /*
266f4b3ec61Sdh  * Initialize at module load time
267f4b3ec61Sdh  */
2687c478bd9Sstevel@tonic-gate boolean_t
spdsock_ddi_init(void)2697c478bd9Sstevel@tonic-gate spdsock_ddi_init(void)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	spdsock_max_optsize = optcom_max_optsize(
2727c478bd9Sstevel@tonic-gate 	    spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1,
2757c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
2767c478bd9Sstevel@tonic-gate 
277f4b3ec61Sdh 	/*
278f4b3ec61Sdh 	 * We want to be informed each time a stack is created or
279f4b3ec61Sdh 	 * destroyed in the kernel, so we can maintain the
280f4b3ec61Sdh 	 * set of spd_stack_t's.
281f4b3ec61Sdh 	 */
282134a1f4eSCasper H.S. Dik 	netstack_register(NS_SPDSOCK, spdsock_stack_init,
283134a1f4eSCasper H.S. Dik 	    spdsock_stack_shutdown, spdsock_stack_fini);
284f4b3ec61Sdh 
285f4b3ec61Sdh 	return (B_TRUE);
286f4b3ec61Sdh }
2877c478bd9Sstevel@tonic-gate 
288f4b3ec61Sdh /*
289f4b3ec61Sdh  * Walk through the param array specified registering each element with the
290f4b3ec61Sdh  * named dispatch handler.
291f4b3ec61Sdh  */
292f4b3ec61Sdh static boolean_t
spdsock_param_register(IDP * ndp,spdsockparam_t * ssp,int cnt)293f4b3ec61Sdh spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt)
294f4b3ec61Sdh {
295f4b3ec61Sdh 	for (; cnt-- > 0; ssp++) {
296f4b3ec61Sdh 		if (ssp->spdsock_param_name != NULL &&
297f4b3ec61Sdh 		    ssp->spdsock_param_name[0]) {
298f4b3ec61Sdh 			if (!nd_load(ndp,
299f4b3ec61Sdh 			    ssp->spdsock_param_name,
300f4b3ec61Sdh 			    spdsock_param_get, spdsock_param_set,
301f4b3ec61Sdh 			    (caddr_t)ssp)) {
302f4b3ec61Sdh 				nd_free(ndp);
303f4b3ec61Sdh 				return (B_FALSE);
304f4b3ec61Sdh 			}
305f4b3ec61Sdh 		}
306f4b3ec61Sdh 	}
3077c478bd9Sstevel@tonic-gate 	return (B_TRUE);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
310f4b3ec61Sdh /*
311f4b3ec61Sdh  * Initialize for each stack instance
312f4b3ec61Sdh  */
313f4b3ec61Sdh /* ARGSUSED */
314f4b3ec61Sdh static void *
spdsock_stack_init(netstackid_t stackid,netstack_t * ns)315f4b3ec61Sdh spdsock_stack_init(netstackid_t stackid, netstack_t *ns)
316f4b3ec61Sdh {
317f4b3ec61Sdh 	spd_stack_t	*spds;
318f4b3ec61Sdh 	spdsockparam_t	*ssp;
319f4b3ec61Sdh 
320f4b3ec61Sdh 	spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP);
321f4b3ec61Sdh 	spds->spds_netstack = ns;
322f4b3ec61Sdh 
323f4b3ec61Sdh 	ASSERT(spds->spds_g_nd == NULL);
324f4b3ec61Sdh 
325f4b3ec61Sdh 	ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
326f4b3ec61Sdh 	spds->spds_params = ssp;
327f4b3ec61Sdh 	bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr));
328f4b3ec61Sdh 
329f4b3ec61Sdh 	(void) spdsock_param_register(&spds->spds_g_nd, ssp,
330f4b3ec61Sdh 	    A_CNT(lcl_param_arr));
331f4b3ec61Sdh 
332f4b3ec61Sdh 	mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL);
333f4b3ec61Sdh 	mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL);
334f4b3ec61Sdh 
335f4b3ec61Sdh 	return (spds);
336f4b3ec61Sdh }
337f4b3ec61Sdh 
3387c478bd9Sstevel@tonic-gate void
spdsock_ddi_destroy(void)3397c478bd9Sstevel@tonic-gate spdsock_ddi_destroy(void)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	vmem_destroy(spdsock_vmem);
342f4b3ec61Sdh 
343f4b3ec61Sdh 	netstack_unregister(NS_SPDSOCK);
344f4b3ec61Sdh }
345f4b3ec61Sdh 
346134a1f4eSCasper H.S. Dik /*
347134a1f4eSCasper H.S. Dik  * Do pre-removal cleanup.
348134a1f4eSCasper H.S. Dik  */
349134a1f4eSCasper H.S. Dik /* ARGSUSED */
350134a1f4eSCasper H.S. Dik static void
spdsock_stack_shutdown(netstackid_t stackid,void * arg)351134a1f4eSCasper H.S. Dik spdsock_stack_shutdown(netstackid_t stackid, void *arg)
352134a1f4eSCasper H.S. Dik {
353134a1f4eSCasper H.S. Dik 	spd_stack_t *spds = (spd_stack_t *)arg;
354134a1f4eSCasper H.S. Dik 
355134a1f4eSCasper H.S. Dik 	if (spds->spds_mp_algs != NULL) {
356134a1f4eSCasper H.S. Dik 		freemsg(spds->spds_mp_algs);
357134a1f4eSCasper H.S. Dik 		spds->spds_mp_algs = NULL;
358134a1f4eSCasper H.S. Dik 	}
359134a1f4eSCasper H.S. Dik }
360134a1f4eSCasper H.S. Dik 
361f4b3ec61Sdh /* ARGSUSED */
362f4b3ec61Sdh static void
spdsock_stack_fini(netstackid_t stackid,void * arg)363f4b3ec61Sdh spdsock_stack_fini(netstackid_t stackid, void *arg)
364f4b3ec61Sdh {
365f4b3ec61Sdh 	spd_stack_t *spds = (spd_stack_t *)arg;
366f4b3ec61Sdh 
367134a1f4eSCasper H.S. Dik 	ASSERT(spds->spds_mp_algs == NULL);
368f4b3ec61Sdh 	mutex_destroy(&spds->spds_param_lock);
369f4b3ec61Sdh 	mutex_destroy(&spds->spds_alg_lock);
370f4b3ec61Sdh 	nd_free(&spds->spds_g_nd);
371f4b3ec61Sdh 	kmem_free(spds->spds_params, sizeof (lcl_param_arr));
372f4b3ec61Sdh 	spds->spds_params = NULL;
373f4b3ec61Sdh 
374f4b3ec61Sdh 	kmem_free(spds, sizeof (*spds));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * NOTE: large quantities of this should be shared with keysock.
3797c478bd9Sstevel@tonic-gate  * Would be nice to combine some of this into a common module, but
3807c478bd9Sstevel@tonic-gate  * not possible given time pressures.
3817c478bd9Sstevel@tonic-gate  */
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * High-level reality checking of extensions.
3857c478bd9Sstevel@tonic-gate  */
3867c478bd9Sstevel@tonic-gate /* ARGSUSED */ /* XXX */
3877c478bd9Sstevel@tonic-gate static boolean_t
ext_check(spd_ext_t * ext)3887c478bd9Sstevel@tonic-gate ext_check(spd_ext_t *ext)
3897c478bd9Sstevel@tonic-gate {
3908810c16bSdanmcd 	spd_if_t *tunname = (spd_if_t *)ext;
3918810c16bSdanmcd 	int i;
3928810c16bSdanmcd 	char *idstr;
3938810c16bSdanmcd 
3948810c16bSdanmcd 	if (ext->spd_ext_type == SPD_EXT_TUN_NAME) {
3958810c16bSdanmcd 		/* (NOTE:  Modified from SADB_EXT_IDENTITY..) */
3967c478bd9Sstevel@tonic-gate 
3978810c16bSdanmcd 		/*
3988810c16bSdanmcd 		 * Make sure the strings in these identities are
3998810c16bSdanmcd 		 * null-terminated.  Let's "proactively" null-terminate the
4008810c16bSdanmcd 		 * string at the last byte if it's not terminated sooner.
4018810c16bSdanmcd 		 */
4028810c16bSdanmcd 		i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t);
4038810c16bSdanmcd 		idstr = (char *)(tunname + 1);
4048810c16bSdanmcd 		while (*idstr != '\0' && i > 0) {
4058810c16bSdanmcd 			i--;
4068810c16bSdanmcd 			idstr++;
4078810c16bSdanmcd 		}
4088810c16bSdanmcd 		if (i == 0) {
4098810c16bSdanmcd 			/*
4108810c16bSdanmcd 			 * I.e., if the bozo user didn't NULL-terminate the
4118810c16bSdanmcd 			 * string...
4128810c16bSdanmcd 			 */
4138810c16bSdanmcd 			idstr--;
4148810c16bSdanmcd 			*idstr = '\0';
4158810c16bSdanmcd 		}
4168810c16bSdanmcd 	}
4177c478bd9Sstevel@tonic-gate 	return (B_TRUE);	/* For now... */
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */
4237c478bd9Sstevel@tonic-gate #define	KGE_OK	0
4247c478bd9Sstevel@tonic-gate #define	KGE_DUP	1
4257c478bd9Sstevel@tonic-gate #define	KGE_UNK	2
4267c478bd9Sstevel@tonic-gate #define	KGE_LEN	3
4277c478bd9Sstevel@tonic-gate #define	KGE_CHK	4
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
4317c478bd9Sstevel@tonic-gate  * Return values include:
4327c478bd9Sstevel@tonic-gate  *
4337c478bd9Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
4347c478bd9Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
4357c478bd9Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
4367c478bd9Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
4377c478bd9Sstevel@tonic-gate  *		extv[0].
4387c478bd9Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
4397c478bd9Sstevel@tonic-gate  *		unknown header.
4407c478bd9Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
4417c478bd9Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
4427c478bd9Sstevel@tonic-gate  *
4437c478bd9Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
4447c478bd9Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate static int
spdsock_get_ext(spd_ext_t * extv[],spd_msg_t * basehdr,uint_t msgsize)4477c478bd9Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
4567c478bd9Sstevel@tonic-gate 		/* Check for unknown headers. */
4577c478bd9Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
4587c478bd9Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX)
4597c478bd9Sstevel@tonic-gate 			return (KGE_UNK);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		/*
4627c478bd9Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
4637c478bd9Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
4647c478bd9Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
4657c478bd9Sstevel@tonic-gate 		 */
4667c478bd9Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
4677c478bd9Sstevel@tonic-gate 		    (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
4687c478bd9Sstevel@tonic-gate 		    (void *)((uint8_t *)basehdr + msgsize))
4697c478bd9Sstevel@tonic-gate 			return (KGE_LEN);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		/* Check for redundant headers. */
4727c478bd9Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
4737c478bd9Sstevel@tonic-gate 			return (KGE_DUP);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		/*
4767c478bd9Sstevel@tonic-gate 		 * Reality check the extension if possible at the spdsock
4777c478bd9Sstevel@tonic-gate 		 * level.
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		if (!ext_check(extv[0]))
4807c478bd9Sstevel@tonic-gate 			return (KGE_CHK);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
4837c478bd9Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
4867c478bd9Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
4877c478bd9Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	/* Everything's cool. */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
4947c478bd9Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
4977c478bd9Sstevel@tonic-gate 		extv[0] = NULL;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	return (KGE_OK);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static const int bad_ext_diag[] = {
5037c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_LCLPORT,
5047c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_REMPORT,
5057c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_PROTO,
5067c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_LCLADDR,
5077c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_REMADDR,
5087c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_ACTION,
5097c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_RULE,
5107c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_RULESET,
5117c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE
5127c478bd9Sstevel@tonic-gate };
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate static const int dup_ext_diag[] = {
5157c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_LCLPORT,
5167c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_REMPORT,
5177c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_PROTO,
5187c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_LCLADDR,
5197c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_REMADDR,
5207c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_ACTION,
5217c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_RULE,
5227c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_RULESET,
5237c478bd9Sstevel@tonic-gate 	SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE
5247c478bd9Sstevel@tonic-gate };
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * Transmit a PF_POLICY error message to the instance either pointed to
5287c478bd9Sstevel@tonic-gate  * by ks, the instance with serial number serial, or more, depending.
5297c478bd9Sstevel@tonic-gate  *
5307c478bd9Sstevel@tonic-gate  * The faulty message (or a reasonable facsimile thereof) is in mp.
5317c478bd9Sstevel@tonic-gate  * This function will free mp or recycle it for delivery, thereby causing
5327c478bd9Sstevel@tonic-gate  * the stream head to free it.
5337c478bd9Sstevel@tonic-gate  */
5347c478bd9Sstevel@tonic-gate static void
spdsock_error(queue_t * q,mblk_t * mp,int error,int diagnostic)5357c478bd9Sstevel@tonic-gate spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_DATA);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (spmsg->spd_msg_type < SPD_MIN ||
5427c478bd9Sstevel@tonic-gate 	    spmsg->spd_msg_type > SPD_MAX)
5437c478bd9Sstevel@tonic-gate 		spmsg->spd_msg_type = SPD_RESERVED;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * Strip out extension headers.
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim);
5497c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (*spmsg);
5507c478bd9Sstevel@tonic-gate 	spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t));
5517c478bd9Sstevel@tonic-gate 	spmsg->spd_msg_errno = (uint8_t)error;
5527c478bd9Sstevel@tonic-gate 	spmsg->spd_msg_diagnostic = (uint16_t)diagnostic;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	qreply(q, mp);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate static void
spdsock_diag(queue_t * q,mblk_t * mp,int diagnostic)5587c478bd9Sstevel@tonic-gate spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	spdsock_error(q, mp, EINVAL, diagnostic);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate static void
spd_echo(queue_t * q,mblk_t * mp)5647c478bd9Sstevel@tonic-gate spd_echo(queue_t *q, mblk_t *mp)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	qreply(q, mp);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5698810c16bSdanmcd /*
5708810c16bSdanmcd  * Do NOT consume a reference to itp.
5718810c16bSdanmcd  */
572f4b3ec61Sdh /*ARGSUSED*/
5737c478bd9Sstevel@tonic-gate static void
spdsock_flush_node(ipsec_tun_pol_t * itp,void * cookie,netstack_t * ns)574f4b3ec61Sdh spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns)
5758810c16bSdanmcd {
5768810c16bSdanmcd 	boolean_t active = (boolean_t)cookie;
5778810c16bSdanmcd 	ipsec_policy_head_t *iph;
5788810c16bSdanmcd 
5798810c16bSdanmcd 	iph = active ? itp->itp_policy : itp->itp_inactive;
5808810c16bSdanmcd 	IPPH_REFHOLD(iph);
5818810c16bSdanmcd 	mutex_enter(&itp->itp_lock);
58201ac885fSDan McDonald 	spdsock_flush_one(iph, ns);  /* Releases iph refhold. */
5838810c16bSdanmcd 	if (active)
5848810c16bSdanmcd 		itp->itp_flags &= ~ITPF_PFLAGS;
5858810c16bSdanmcd 	else
5868810c16bSdanmcd 		itp->itp_flags &= ~ITPF_IFLAGS;
5878810c16bSdanmcd 	mutex_exit(&itp->itp_lock);
58801ac885fSDan McDonald 	/* SPD_FLUSH is worth a tunnel MTU check. */
58901ac885fSDan McDonald 	update_iptun_policy(itp);
5908810c16bSdanmcd }
5918810c16bSdanmcd 
5928810c16bSdanmcd /*
5938810c16bSdanmcd  * Clear out one polhead.
5948810c16bSdanmcd  */
5958810c16bSdanmcd static void
spdsock_flush_one(ipsec_policy_head_t * iph,netstack_t * ns)596f4b3ec61Sdh spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_WRITER);
599f4b3ec61Sdh 	ipsec_polhead_flush(iph, ns);
6007c478bd9Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
601f4b3ec61Sdh 	IPPH_REFRELE(iph, ns);
6028810c16bSdanmcd }
6038810c16bSdanmcd 
6048810c16bSdanmcd static void
spdsock_flush(queue_t * q,ipsec_policy_head_t * iph,ipsec_tun_pol_t * itp,mblk_t * mp)605799bd290Spwernau spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp,
606799bd290Spwernau     mblk_t *mp)
6078810c16bSdanmcd {
6088810c16bSdanmcd 	boolean_t active;
609f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
610db061fdfSdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
611005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
6128810c16bSdanmcd 
6138810c16bSdanmcd 	if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) {
614db061fdfSdanmcd 		spdsock_flush_one(iph, ns);
615005d3febSMarek Pospisil 		if (auditing) {
616799bd290Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
617de8c4a14SErik Nordmark 			cred_t *cr;
618de8c4a14SErik Nordmark 			pid_t cpid;
619799bd290Spwernau 
620de8c4a14SErik Nordmark 			cr = msg_getcred(mp, &cpid);
621799bd290Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
622de8c4a14SErik Nordmark 			audit_pf_policy(SPD_FLUSH, cr, ns,
623de8c4a14SErik Nordmark 			    ITP_NAME(itp), active, 0, cpid);
624799bd290Spwernau 		}
6258810c16bSdanmcd 	} else {
6268810c16bSdanmcd 		active = (iph == ALL_ACTIVE_POLHEADS);
6278810c16bSdanmcd 
6288810c16bSdanmcd 		/* First flush the global policy. */
629f4b3ec61Sdh 		spdsock_flush_one(active ? ipsec_system_policy(ns) :
630f4b3ec61Sdh 		    ipsec_inactive_policy(ns), ns);
631005d3febSMarek Pospisil 		if (auditing) {
632de8c4a14SErik Nordmark 			cred_t *cr;
633de8c4a14SErik Nordmark 			pid_t cpid;
634de8c4a14SErik Nordmark 
635de8c4a14SErik Nordmark 			cr = msg_getcred(mp, &cpid);
636de8c4a14SErik Nordmark 			audit_pf_policy(SPD_FLUSH, cr, ns, NULL,
637de8c4a14SErik Nordmark 			    active, 0, cpid);
638799bd290Spwernau 		}
6398810c16bSdanmcd 		/* Then flush every tunnel's appropriate one. */
640db061fdfSdanmcd 		itp_walk(spdsock_flush_node, (void *)active, ns);
641005d3febSMarek Pospisil 		if (auditing) {
642de8c4a14SErik Nordmark 			cred_t *cr;
643de8c4a14SErik Nordmark 			pid_t cpid;
644de8c4a14SErik Nordmark 
645de8c4a14SErik Nordmark 			cr = msg_getcred(mp, &cpid);
646de8c4a14SErik Nordmark 			audit_pf_policy(SPD_FLUSH, cr, ns,
647de8c4a14SErik Nordmark 			    "all tunnels", active, 0, cpid);
648de8c4a14SErik Nordmark 		}
6498810c16bSdanmcd 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	spd_echo(q, mp);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate static boolean_t
spdsock_ext_to_sel(spd_ext_t ** extv,ipsec_selkey_t * sel,int * diag)6557c478bd9Sstevel@tonic-gate spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	bzero(sel, sizeof (*sel));
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (extv[SPD_EXT_PROTO] != NULL) {
6607c478bd9Sstevel@tonic-gate 		struct spd_proto *pr =
6617c478bd9Sstevel@tonic-gate 		    (struct spd_proto *)extv[SPD_EXT_PROTO];
6627c478bd9Sstevel@tonic-gate 		sel->ipsl_proto = pr->spd_proto_number;
6637c478bd9Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_PROTOCOL;
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 	if (extv[SPD_EXT_LCLPORT] != NULL) {
6667c478bd9Sstevel@tonic-gate 		struct spd_portrange *pr =
6677c478bd9Sstevel@tonic-gate 		    (struct spd_portrange *)extv[SPD_EXT_LCLPORT];
6687c478bd9Sstevel@tonic-gate 		sel->ipsl_lport = pr->spd_ports_minport;
6697c478bd9Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_LOCAL_PORT;
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 	if (extv[SPD_EXT_REMPORT] != NULL) {
6727c478bd9Sstevel@tonic-gate 		struct spd_portrange *pr =
6737c478bd9Sstevel@tonic-gate 		    (struct spd_portrange *)extv[SPD_EXT_REMPORT];
6747c478bd9Sstevel@tonic-gate 		sel->ipsl_rport = pr->spd_ports_minport;
6757c478bd9Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_REMOTE_PORT;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) {
6797c478bd9Sstevel@tonic-gate 		struct spd_typecode *tc=
6807c478bd9Sstevel@tonic-gate 		    (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE];
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		sel->ipsl_valid |= IPSL_ICMP_TYPE;
6837c478bd9Sstevel@tonic-gate 		sel->ipsl_icmp_type = tc->spd_typecode_type;
6847c478bd9Sstevel@tonic-gate 		if (tc->spd_typecode_type_end < tc->spd_typecode_type)
6857c478bd9Sstevel@tonic-gate 			sel->ipsl_icmp_type_end = tc->spd_typecode_type;
6867c478bd9Sstevel@tonic-gate 		else
6877c478bd9Sstevel@tonic-gate 			sel->ipsl_icmp_type_end = tc->spd_typecode_type_end;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		if (tc->spd_typecode_code != 255) {
6907c478bd9Sstevel@tonic-gate 			sel->ipsl_valid |= IPSL_ICMP_CODE;
6917c478bd9Sstevel@tonic-gate 			sel->ipsl_icmp_code = tc->spd_typecode_code;
6927c478bd9Sstevel@tonic-gate 			if (tc->spd_typecode_code_end < tc->spd_typecode_code)
6937c478bd9Sstevel@tonic-gate 				sel->ipsl_icmp_code_end = tc->spd_typecode_code;
6947c478bd9Sstevel@tonic-gate 			else
6957c478bd9Sstevel@tonic-gate 				sel->ipsl_icmp_code_end =
6967c478bd9Sstevel@tonic-gate 				    tc->spd_typecode_code_end;
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 	}
6991752e73cSToomas Soome #define	ADDR2SEL(sel, extv, field, pfield, extn, bit)			\
7001752e73cSToomas Soome 	if ((extv)[(extn)] != NULL) {					\
7011752e73cSToomas Soome 		uint_t addrlen;						\
7021752e73cSToomas Soome 		struct spd_address *ap =				\
7031752e73cSToomas Soome 			(struct spd_address *)((extv)[(extn)]);		\
7041752e73cSToomas Soome 		addrlen = (ap->spd_address_af == AF_INET6) ?		\
7051752e73cSToomas Soome 			IPV6_ADDR_LEN : IP_ADDR_LEN;			\
7061752e73cSToomas Soome 		if (SPD_64TO8(ap->spd_address_len) <			\
7071752e73cSToomas Soome 			(addrlen + sizeof (*ap))) {			\
7081752e73cSToomas Soome 			*diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN;		\
7091752e73cSToomas Soome 			return (B_FALSE);				\
7101752e73cSToomas Soome 		}							\
7111752e73cSToomas Soome 		bcopy((ap+1), &((sel)->field), addrlen);		\
7121752e73cSToomas Soome 		(sel)->pfield = ap->spd_address_prefixlen;		\
7131752e73cSToomas Soome 		(sel)->ipsl_valid |= (bit);				\
7141752e73cSToomas Soome 		(sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ?	\
7151752e73cSToomas Soome 			IPSL_IPV6 : IPSL_IPV4;				\
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen,
7197c478bd9Sstevel@tonic-gate 	    SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR);
7207c478bd9Sstevel@tonic-gate 	ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen,
7217c478bd9Sstevel@tonic-gate 	    SPD_EXT_REMADDR, IPSL_REMOTE_ADDR);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) ==
7247c478bd9Sstevel@tonic-gate 	    (IPSL_IPV6|IPSL_IPV4)) {
7257c478bd9Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_MIXED_AF;
7267c478bd9Sstevel@tonic-gate 		return (B_FALSE);
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate #undef ADDR2SEL
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	return (B_TRUE);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate static boolean_t
spd_convert_type(uint32_t type,ipsec_act_t * act)7357c478bd9Sstevel@tonic-gate spd_convert_type(uint32_t type, ipsec_act_t *act)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	switch (type) {
7387c478bd9Sstevel@tonic-gate 	case SPD_ACTTYPE_DROP:
7397c478bd9Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_DISCARD;
7407c478bd9Sstevel@tonic-gate 		return (B_TRUE);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	case SPD_ACTTYPE_PASS:
7437c478bd9Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_CLEAR;
7447c478bd9Sstevel@tonic-gate 		return (B_TRUE);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	case SPD_ACTTYPE_IPSEC:
7477c478bd9Sstevel@tonic-gate 		act->ipa_type = IPSEC_ACT_APPLY;
7487c478bd9Sstevel@tonic-gate 		return (B_TRUE);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	return (B_FALSE);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate static boolean_t
spd_convert_flags(uint32_t flags,ipsec_act_t * act)7547c478bd9Sstevel@tonic-gate spd_convert_flags(uint32_t flags, ipsec_act_t *act)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * Note use of !! for boolean canonicalization.
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate 	act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH);
7607c478bd9Sstevel@tonic-gate 	act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP);
7617c478bd9Sstevel@tonic-gate 	act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA);
7627c478bd9Sstevel@tonic-gate 	act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE);
7637c478bd9Sstevel@tonic-gate 	act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE);
7647c478bd9Sstevel@tonic-gate 	return (B_TRUE);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate static void
spdsock_reset_act(ipsec_act_t * act)7687c478bd9Sstevel@tonic-gate spdsock_reset_act(ipsec_act_t *act)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	bzero(act, sizeof (*act));
7717c478bd9Sstevel@tonic-gate 	act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS;
7727c478bd9Sstevel@tonic-gate 	act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS;
7737c478bd9Sstevel@tonic-gate 	act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS;
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate /*
7777c478bd9Sstevel@tonic-gate  * Sanity check action against reality, and shrink-wrap key sizes..
7787c478bd9Sstevel@tonic-gate  */
7797c478bd9Sstevel@tonic-gate static boolean_t
spdsock_check_action(ipsec_act_t * act,boolean_t tunnel_polhead,int * diag,spd_stack_t * spds)780f4b3ec61Sdh spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag,
781f4b3ec61Sdh     spd_stack_t *spds)
7827c478bd9Sstevel@tonic-gate {
7838810c16bSdanmcd 	if (tunnel_polhead && act->ipa_apply.ipp_use_unique) {
7848810c16bSdanmcd 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7858810c16bSdanmcd 		return (B_FALSE);
7868810c16bSdanmcd 	}
7877c478bd9Sstevel@tonic-gate 	if ((act->ipa_type != IPSEC_ACT_APPLY) &&
7887c478bd9Sstevel@tonic-gate 	    (act->ipa_apply.ipp_use_ah ||
789fc80c0dfSnordmark 	    act->ipa_apply.ipp_use_esp ||
790fc80c0dfSnordmark 	    act->ipa_apply.ipp_use_espa ||
791fc80c0dfSnordmark 	    act->ipa_apply.ipp_use_se ||
792fc80c0dfSnordmark 	    act->ipa_apply.ipp_use_unique)) {
7937c478bd9Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7947c478bd9Sstevel@tonic-gate 		return (B_FALSE);
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 	if ((act->ipa_type == IPSEC_ACT_APPLY) &&
7977c478bd9Sstevel@tonic-gate 	    !act->ipa_apply.ipp_use_ah &&
7987c478bd9Sstevel@tonic-gate 	    !act->ipa_apply.ipp_use_esp) {
7997c478bd9Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
8007c478bd9Sstevel@tonic-gate 		return (B_FALSE);
8017c478bd9Sstevel@tonic-gate 	}
802f4b3ec61Sdh 	return (ipsec_check_action(act, diag, spds->spds_netstack));
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * We may be short a few error checks here..
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@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)8097c478bd9Sstevel@tonic-gate spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp,
810f4b3ec61Sdh     int *diag, spd_stack_t *spds)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	struct spd_ext_actions *sactp =
8137c478bd9Sstevel@tonic-gate 	    (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
8147c478bd9Sstevel@tonic-gate 	ipsec_act_t act, *actp, *endactp;
8157c478bd9Sstevel@tonic-gate 	struct spd_attribute *attrp, *endattrp;
8167c478bd9Sstevel@tonic-gate 	uint64_t *endp;
8177c478bd9Sstevel@tonic-gate 	int nact;
8188810c16bSdanmcd 	boolean_t tunnel_polhead;
8198810c16bSdanmcd 
8208810c16bSdanmcd 	tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL &&
8218810c16bSdanmcd 	    (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags &
822fc80c0dfSnordmark 	    SPD_RULE_FLAG_TUNNEL));
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	*actpp = NULL;
8257c478bd9Sstevel@tonic-gate 	*nactp = 0;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (sactp == NULL) {
8287c478bd9Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_NO_ACTION_EXT;
8297c478bd9Sstevel@tonic-gate 		return (B_FALSE);
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/*
8337c478bd9Sstevel@tonic-gate 	 * Parse the "action" extension and convert into an action chain.
8347c478bd9Sstevel@tonic-gate 	 */
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	nact = sactp->spd_actions_count;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	endp = (uint64_t *)sactp;
8397c478bd9Sstevel@tonic-gate 	endp += sactp->spd_actions_len;
8407c478bd9Sstevel@tonic-gate 	endattrp = (struct spd_attribute *)endp;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP);
8437c478bd9Sstevel@tonic-gate 	if (actp == NULL) {
8447c478bd9Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_NO_MEM;
8457c478bd9Sstevel@tonic-gate 		return (B_FALSE);
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	*actpp = actp;
8487c478bd9Sstevel@tonic-gate 	*nactp = nact;
8497c478bd9Sstevel@tonic-gate 	endactp = actp + nact;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	spdsock_reset_act(&act);
8527c478bd9Sstevel@tonic-gate 	attrp = (struct spd_attribute *)(&sactp[1]);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	for (; attrp < endattrp; attrp++) {
8557c478bd9Sstevel@tonic-gate 		switch (attrp->spd_attr_tag) {
8567c478bd9Sstevel@tonic-gate 		case SPD_ATTR_NOP:
8577c478bd9Sstevel@tonic-gate 			break;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		case SPD_ATTR_EMPTY:
8607c478bd9Sstevel@tonic-gate 			spdsock_reset_act(&act);
8617c478bd9Sstevel@tonic-gate 			break;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		case SPD_ATTR_END:
8647c478bd9Sstevel@tonic-gate 			attrp = endattrp;
8657c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
8667c478bd9Sstevel@tonic-gate 		case SPD_ATTR_NEXT:
8677c478bd9Sstevel@tonic-gate 			if (actp >= endactp) {
8687c478bd9Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
8697c478bd9Sstevel@tonic-gate 				goto fail;
8707c478bd9Sstevel@tonic-gate 			}
871f4b3ec61Sdh 			if (!spdsock_check_action(&act, tunnel_polhead,
872f4b3ec61Sdh 			    diag, spds))
8737c478bd9Sstevel@tonic-gate 				goto fail;
8747c478bd9Sstevel@tonic-gate 			*actp++ = act;
8758810c16bSdanmcd 			spdsock_reset_act(&act);
8767c478bd9Sstevel@tonic-gate 			break;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		case SPD_ATTR_TYPE:
8797c478bd9Sstevel@tonic-gate 			if (!spd_convert_type(attrp->spd_attr_value, &act)) {
8807c478bd9Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE;
8817c478bd9Sstevel@tonic-gate 				goto fail;
8827c478bd9Sstevel@tonic-gate 			}
8837c478bd9Sstevel@tonic-gate 			break;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		case SPD_ATTR_FLAGS:
8868810c16bSdanmcd 			if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) {
8878810c16bSdanmcd 				/*
8888810c16bSdanmcd 				 * Set "sa unique" for transport-mode
8898810c16bSdanmcd 				 * tunnels whether we want to or not.
8908810c16bSdanmcd 				 */
8918810c16bSdanmcd 				attrp->spd_attr_value |= SPD_APPLY_UNIQUE;
8928810c16bSdanmcd 			}
8937c478bd9Sstevel@tonic-gate 			if (!spd_convert_flags(attrp->spd_attr_value, &act)) {
8947c478bd9Sstevel@tonic-gate 				*diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS;
8957c478bd9Sstevel@tonic-gate 				goto fail;
8967c478bd9Sstevel@tonic-gate 			}
8977c478bd9Sstevel@tonic-gate 			break;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		case SPD_ATTR_AH_AUTH:
9008810c16bSdanmcd 			if (attrp->spd_attr_value == 0) {
9018810c16bSdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG;
9028810c16bSdanmcd 				goto fail;
9038810c16bSdanmcd 			}
9047c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value;
9057c478bd9Sstevel@tonic-gate 			break;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ESP_ENCR:
9088810c16bSdanmcd 			if (attrp->spd_attr_value == 0) {
9098810c16bSdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG;
9108810c16bSdanmcd 				goto fail;
9118810c16bSdanmcd 			}
9127c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value;
9137c478bd9Sstevel@tonic-gate 			break;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ESP_AUTH:
9168810c16bSdanmcd 			if (attrp->spd_attr_value == 0) {
9178810c16bSdanmcd 				*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG;
9188810c16bSdanmcd 				goto fail;
9198810c16bSdanmcd 			}
9207c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value;
9217c478bd9Sstevel@tonic-gate 			break;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ENCR_MINBITS:
9247c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value;
9257c478bd9Sstevel@tonic-gate 			break;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ENCR_MAXBITS:
9287c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value;
9297c478bd9Sstevel@tonic-gate 			break;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		case SPD_ATTR_AH_MINBITS:
9327c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value;
9337c478bd9Sstevel@tonic-gate 			break;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 		case SPD_ATTR_AH_MAXBITS:
9367c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value;
9377c478bd9Sstevel@tonic-gate 			break;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ESPA_MINBITS:
9407c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value;
9417c478bd9Sstevel@tonic-gate 			break;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ESPA_MAXBITS:
9447c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value;
9457c478bd9Sstevel@tonic-gate 			break;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		case SPD_ATTR_LIFE_SOFT_TIME:
9487c478bd9Sstevel@tonic-gate 		case SPD_ATTR_LIFE_HARD_TIME:
9497c478bd9Sstevel@tonic-gate 		case SPD_ATTR_LIFE_SOFT_BYTES:
9507c478bd9Sstevel@tonic-gate 		case SPD_ATTR_LIFE_HARD_BYTES:
9517c478bd9Sstevel@tonic-gate 			break;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		case SPD_ATTR_KM_PROTO:
9547c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_km_proto = attrp->spd_attr_value;
9557c478bd9Sstevel@tonic-gate 			break;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		case SPD_ATTR_KM_COOKIE:
9587c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value;
9597c478bd9Sstevel@tonic-gate 			break;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		case SPD_ATTR_REPLAY_DEPTH:
9627c478bd9Sstevel@tonic-gate 			act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value;
9637c478bd9Sstevel@tonic-gate 			break;
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 	if (actp != endactp) {
9677c478bd9Sstevel@tonic-gate 		*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
9687c478bd9Sstevel@tonic-gate 		goto fail;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	return (B_TRUE);
9727c478bd9Sstevel@tonic-gate fail:
9737c478bd9Sstevel@tonic-gate 	ipsec_actvec_free(*actpp, nact);
9747c478bd9Sstevel@tonic-gate 	*actpp = NULL;
9757c478bd9Sstevel@tonic-gate 	return (B_FALSE);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate typedef struct
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate 	ipsec_policy_t *pol;
9817c478bd9Sstevel@tonic-gate 	int dir;
9827c478bd9Sstevel@tonic-gate } tmprule_t;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@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)9857c478bd9Sstevel@tonic-gate mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule,
9867c478bd9Sstevel@tonic-gate     ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af,
987f4b3ec61Sdh     tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	ipsec_policy_t *pol;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4);
9927c478bd9Sstevel@tonic-gate 	sel->ipsl_valid |= af;
9937c478bd9Sstevel@tonic-gate 
9948810c16bSdanmcd 	pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority,
995f4b3ec61Sdh 	    index, spds->spds_netstack);
9967c478bd9Sstevel@tonic-gate 	if (pol == NULL)
9977c478bd9Sstevel@tonic-gate 		return (ENOMEM);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	(*rp)->pol = pol;
10007c478bd9Sstevel@tonic-gate 	(*rp)->dir = dir;
10017c478bd9Sstevel@tonic-gate 	(*rp)++;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	if (!ipsec_check_policy(iph, pol, dir))
10047c478bd9Sstevel@tonic-gate 		return (EEXIST);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	rule->spd_rule_index = pol->ipsp_index;
10077c478bd9Sstevel@tonic-gate 	return (0);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@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)10117c478bd9Sstevel@tonic-gate mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule,
10127c478bd9Sstevel@tonic-gate     ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs,
1013f4b3ec61Sdh     tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	int error;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if (afs & IPSL_IPV4) {
10188810c16bSdanmcd 		error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp,
1019f4b3ec61Sdh 		    index, spds);
10207c478bd9Sstevel@tonic-gate 		if (error != 0)
10217c478bd9Sstevel@tonic-gate 			return (error);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	if (afs & IPSL_IPV6) {
10248810c16bSdanmcd 		error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp,
1025f4b3ec61Sdh 		    index, spds);
10267c478bd9Sstevel@tonic-gate 		if (error != 0)
10277c478bd9Sstevel@tonic-gate 			return (error);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 	return (0);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@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)10348810c16bSdanmcd spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
10358810c16bSdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
10367c478bd9Sstevel@tonic-gate {
10377c478bd9Sstevel@tonic-gate 	ipsec_selkey_t sel;
10387c478bd9Sstevel@tonic-gate 	ipsec_act_t *actp;
10397c478bd9Sstevel@tonic-gate 	uint_t nact;
10408810c16bSdanmcd 	int diag = 0, error, afs;
10417c478bd9Sstevel@tonic-gate 	struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
10427c478bd9Sstevel@tonic-gate 	tmprule_t rules[4], *rulep = &rules[0];
10438810c16bSdanmcd 	boolean_t tunnel_mode, empty_itp, active;
10448810c16bSdanmcd 	uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index;
1045f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
1046005d3febSMarek Pospisil 	spd_stack_t *spds = ss->spdsock_spds;
1047005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	if (rule == NULL) {
10507c478bd9Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
1051005d3febSMarek Pospisil 		if (auditing) {
1052799bd290Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1053de8c4a14SErik Nordmark 			cred_t *cr;
1054de8c4a14SErik Nordmark 			pid_t cpid;
1055799bd290Spwernau 
1056de8c4a14SErik Nordmark 			cr = msg_getcred(mp, &cpid);
1057799bd290Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1058de8c4a14SErik Nordmark 			audit_pf_policy(SPD_ADDRULE, cr,
1059799bd290Spwernau 			    spds->spds_netstack, ITP_NAME(itp), active,
1060de8c4a14SErik Nordmark 			    SPD_DIAGNOSTIC_NO_RULE_EXT, cpid);
1061799bd290Spwernau 		}
10627c478bd9Sstevel@tonic-gate 		return;
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 
10658810c16bSdanmcd 	tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL);
10668810c16bSdanmcd 
10678810c16bSdanmcd 	if (itp != NULL) {
10688810c16bSdanmcd 		mutex_enter(&itp->itp_lock);
10698810c16bSdanmcd 		ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph);
10708810c16bSdanmcd 		active = (itp->itp_policy == iph);
10718810c16bSdanmcd 		if (ITP_P_ISACTIVE(itp, iph)) {
10728810c16bSdanmcd 			/* Check for mix-and-match of tunnel/transport. */
10738810c16bSdanmcd 			if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) ||
10748810c16bSdanmcd 			    (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) {
10758810c16bSdanmcd 				mutex_exit(&itp->itp_lock);
10768810c16bSdanmcd 				spdsock_error(q, mp, EBUSY, 0);
10778810c16bSdanmcd 				return;
10788810c16bSdanmcd 			}
10798810c16bSdanmcd 			empty_itp = B_FALSE;
10808810c16bSdanmcd 		} else {
10818810c16bSdanmcd 			empty_itp = B_TRUE;
10828810c16bSdanmcd 			itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE;
10838810c16bSdanmcd 			if (tunnel_mode)
10848810c16bSdanmcd 				itp->itp_flags |= active ? ITPF_P_TUNNEL :
10858810c16bSdanmcd 				    ITPF_I_TUNNEL;
10868810c16bSdanmcd 		}
10878810c16bSdanmcd 	} else {
10888810c16bSdanmcd 		empty_itp = B_FALSE;
10898810c16bSdanmcd 	}
10908810c16bSdanmcd 
10917c478bd9Sstevel@tonic-gate 	if (rule->spd_rule_index != 0) {
10928810c16bSdanmcd 		diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX;
10938810c16bSdanmcd 		error = EINVAL;
10948810c16bSdanmcd 		goto fail2;
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
10988810c16bSdanmcd 		error = EINVAL;
10998810c16bSdanmcd 		goto fail2;
11008810c16bSdanmcd 	}
11018810c16bSdanmcd 
11028810c16bSdanmcd 	if (itp != NULL) {
11038810c16bSdanmcd 		if (tunnel_mode) {
11048810c16bSdanmcd 			if (sel.ipsl_valid &
11058810c16bSdanmcd 			    (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) {
11068810c16bSdanmcd 				itp->itp_flags |= active ?
11078810c16bSdanmcd 				    ITPF_P_PER_PORT_SECURITY :
11088810c16bSdanmcd 				    ITPF_I_PER_PORT_SECURITY;
11098810c16bSdanmcd 			}
11108810c16bSdanmcd 		} else {
11118810c16bSdanmcd 			/*
11128810c16bSdanmcd 			 * For now, we don't allow transport-mode on a tunnel
11138810c16bSdanmcd 			 * with ANY specific selectors.  Bail if we have such
11148810c16bSdanmcd 			 * a request.
11158810c16bSdanmcd 			 */
11168810c16bSdanmcd 			if (sel.ipsl_valid & IPSL_WILDCARD) {
11178810c16bSdanmcd 				diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS;
11188810c16bSdanmcd 				error = EINVAL;
11198810c16bSdanmcd 				goto fail2;
11208810c16bSdanmcd 			}
11218810c16bSdanmcd 		}
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 
1124f4b3ec61Sdh 	if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) {
11258810c16bSdanmcd 		error = EINVAL;
11268810c16bSdanmcd 		goto fail2;
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 	/*
11297c478bd9Sstevel@tonic-gate 	 * If no addresses were specified, add both.
11307c478bd9Sstevel@tonic-gate 	 */
11317c478bd9Sstevel@tonic-gate 	afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4);
11327c478bd9Sstevel@tonic-gate 	if (afs == 0)
11337c478bd9Sstevel@tonic-gate 		afs = (IPSL_IPV6|IPSL_IPV4);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_WRITER);
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) {
11387c478bd9Sstevel@tonic-gate 		error = mkrulepair(iph, rule, &sel, actp, nact,
1139f4b3ec61Sdh 		    IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds);
11407c478bd9Sstevel@tonic-gate 		if (error != 0)
11417c478bd9Sstevel@tonic-gate 			goto fail;
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) {
11457c478bd9Sstevel@tonic-gate 		error = mkrulepair(iph, rule, &sel, actp, nact,
1146f4b3ec61Sdh 		    IPSEC_TYPE_INBOUND, afs, &rulep, index, spds);
11477c478bd9Sstevel@tonic-gate 		if (error != 0)
11487c478bd9Sstevel@tonic-gate 			goto fail;
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 
1151f4b3ec61Sdh 	while ((--rulep) >= &rules[0]) {
1152f4b3ec61Sdh 		ipsec_enter_policy(iph, rulep->pol, rulep->dir,
1153f4b3ec61Sdh 		    spds->spds_netstack);
1154f4b3ec61Sdh 	}
11557c478bd9Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
11568810c16bSdanmcd 	if (itp != NULL)
11578810c16bSdanmcd 		mutex_exit(&itp->itp_lock);
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	ipsec_actvec_free(actp, nact);
11607c478bd9Sstevel@tonic-gate 	spd_echo(q, mp);
1161005d3febSMarek Pospisil 	if (auditing) {
1162799bd290Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1163de8c4a14SErik Nordmark 		cred_t *cr;
1164de8c4a14SErik Nordmark 		pid_t cpid;
1165799bd290Spwernau 
1166de8c4a14SErik Nordmark 		cr = msg_getcred(mp, &cpid);
1167799bd290Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1168de8c4a14SErik Nordmark 		audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1169de8c4a14SErik Nordmark 		    ITP_NAME(itp), active, 0, cpid);
1170799bd290Spwernau 	}
11717c478bd9Sstevel@tonic-gate 	return;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate fail:
11747c478bd9Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
1175bd670b35SErik Nordmark 	while ((--rulep) >= &rules[0])
1176bd670b35SErik Nordmark 		IPPOL_REFRELE(rulep->pol);
11777c478bd9Sstevel@tonic-gate 	ipsec_actvec_free(actp, nact);
11788810c16bSdanmcd fail2:
11798810c16bSdanmcd 	if (itp != NULL) {
11808810c16bSdanmcd 		if (empty_itp)
11818810c16bSdanmcd 			itp->itp_flags = 0;
11828810c16bSdanmcd 		mutex_exit(&itp->itp_lock);
11838810c16bSdanmcd 	}
11848810c16bSdanmcd 	spdsock_error(q, mp, error, diag);
1185005d3febSMarek Pospisil 	if (auditing) {
1186799bd290Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1187de8c4a14SErik Nordmark 		cred_t *cr;
1188de8c4a14SErik Nordmark 		pid_t cpid;
1189799bd290Spwernau 
1190de8c4a14SErik Nordmark 		cr = msg_getcred(mp, &cpid);
1191799bd290Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1192de8c4a14SErik Nordmark 		audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1193de8c4a14SErik Nordmark 		    ITP_NAME(itp), active, error, cpid);
1194799bd290Spwernau 	}
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@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)11988810c16bSdanmcd spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
11998810c16bSdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate 	ipsec_selkey_t sel;
12027c478bd9Sstevel@tonic-gate 	struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
12038810c16bSdanmcd 	int err, diag = 0;
1204f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
1205db061fdfSdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
1206005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	if (rule == NULL) {
12097c478bd9Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
1210005d3febSMarek Pospisil 		if (auditing) {
1211799bd290Spwernau 			boolean_t active;
1212799bd290Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1213de8c4a14SErik Nordmark 			cred_t *cr;
1214de8c4a14SErik Nordmark 			pid_t cpid;
1215799bd290Spwernau 
1216de8c4a14SErik Nordmark 			cr = msg_getcred(mp, &cpid);
1217799bd290Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1218de8c4a14SErik Nordmark 			audit_pf_policy(SPD_DELETERULE, cr, ns,
1219db061fdfSdanmcd 			    ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT,
1220de8c4a14SErik Nordmark 			    cpid);
1221799bd290Spwernau 		}
12227c478bd9Sstevel@tonic-gate 		return;
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 
12258810c16bSdanmcd 	/*
12268810c16bSdanmcd 	 * Must enter itp_lock first to avoid deadlock.  See tun.c's
12278810c16bSdanmcd 	 * set_sec_simple() for the other case of itp_lock and iph_lock.
12288810c16bSdanmcd 	 */
12298810c16bSdanmcd 	if (itp != NULL)
12308810c16bSdanmcd 		mutex_enter(&itp->itp_lock);
12318810c16bSdanmcd 
12327c478bd9Sstevel@tonic-gate 	if (rule->spd_rule_index != 0) {
1233db061fdfSdanmcd 		if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) !=
1234db061fdfSdanmcd 		    0) {
12358810c16bSdanmcd 			err = ESRCH;
12368810c16bSdanmcd 			goto fail;
12377c478bd9Sstevel@tonic-gate 		}
12387c478bd9Sstevel@tonic-gate 	} else {
12397c478bd9Sstevel@tonic-gate 		if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
12408810c16bSdanmcd 			err = EINVAL;	/* diag already set... */
12418810c16bSdanmcd 			goto fail;
12427c478bd9Sstevel@tonic-gate 		}
12437c478bd9Sstevel@tonic-gate 
12448810c16bSdanmcd 		if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) &&
1245db061fdfSdanmcd 		    !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) {
12468810c16bSdanmcd 			err = ESRCH;
12478810c16bSdanmcd 			goto fail;
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 
12508810c16bSdanmcd 		if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) &&
1251db061fdfSdanmcd 		    !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) {
12528810c16bSdanmcd 			err = ESRCH;
12538810c16bSdanmcd 			goto fail;
12547c478bd9Sstevel@tonic-gate 		}
12557c478bd9Sstevel@tonic-gate 	}
12568810c16bSdanmcd 
12578810c16bSdanmcd 	if (itp != NULL) {
12588810c16bSdanmcd 		ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive);
12598810c16bSdanmcd 		rw_enter(&iph->iph_lock, RW_READER);
12608810c16bSdanmcd 		if (avl_numnodes(&iph->iph_rulebyid) == 0) {
12618810c16bSdanmcd 			if (iph == itp->itp_policy)
12628810c16bSdanmcd 				itp->itp_flags &= ~ITPF_PFLAGS;
12638810c16bSdanmcd 			else
12648810c16bSdanmcd 				itp->itp_flags &= ~ITPF_IFLAGS;
12658810c16bSdanmcd 		}
12668810c16bSdanmcd 		/* Can exit locks in any order. */
12678810c16bSdanmcd 		rw_exit(&iph->iph_lock);
12688810c16bSdanmcd 		mutex_exit(&itp->itp_lock);
12698810c16bSdanmcd 	}
12707c478bd9Sstevel@tonic-gate 	spd_echo(q, mp);
1271005d3febSMarek Pospisil 	if (auditing) {
1272799bd290Spwernau 		boolean_t active;
1273799bd290Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1274de8c4a14SErik Nordmark 		cred_t *cr;
1275de8c4a14SErik Nordmark 		pid_t cpid;
1276799bd290Spwernau 
1277de8c4a14SErik Nordmark 		cr = msg_getcred(mp, &cpid);
1278799bd290Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1279de8c4a14SErik Nordmark 		audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1280de8c4a14SErik Nordmark 		    active, 0, cpid);
1281799bd290Spwernau 	}
12827c478bd9Sstevel@tonic-gate 	return;
12837c478bd9Sstevel@tonic-gate fail:
12848810c16bSdanmcd 	if (itp != NULL)
12858810c16bSdanmcd 		mutex_exit(&itp->itp_lock);
12868810c16bSdanmcd 	spdsock_error(q, mp, err, diag);
1287005d3febSMarek Pospisil 	if (auditing) {
1288799bd290Spwernau 		boolean_t active;
1289799bd290Spwernau 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1290de8c4a14SErik Nordmark 		cred_t *cr;
1291de8c4a14SErik Nordmark 		pid_t cpid;
1292799bd290Spwernau 
1293de8c4a14SErik Nordmark 		cr = msg_getcred(mp, &cpid);
1294799bd290Spwernau 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1295de8c4a14SErik Nordmark 		audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1296de8c4a14SErik Nordmark 		    active, err, cpid);
1297799bd290Spwernau 	}
12988810c16bSdanmcd }
12998810c16bSdanmcd 
13008810c16bSdanmcd /* Do NOT consume a reference to itp. */
13018810c16bSdanmcd /* ARGSUSED */
13028810c16bSdanmcd static void
spdsock_flip_node(ipsec_tun_pol_t * itp,void * ignoreme,netstack_t * ns)1303f4b3ec61Sdh spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns)
13048810c16bSdanmcd {
13058810c16bSdanmcd 	mutex_enter(&itp->itp_lock);
13068810c16bSdanmcd 	ITPF_SWAP(itp->itp_flags);
1307f4b3ec61Sdh 	ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns);
13088810c16bSdanmcd 	mutex_exit(&itp->itp_lock);
130901ac885fSDan McDonald 	/* SPD_FLIP is worth a tunnel MTU check. */
131001ac885fSDan McDonald 	update_iptun_policy(itp);
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate void
spdsock_flip(queue_t * q,mblk_t * mp,spd_if_t * tunname)13148810c16bSdanmcd spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname)
13157c478bd9Sstevel@tonic-gate {
13168810c16bSdanmcd 	char *tname;
13178810c16bSdanmcd 	ipsec_tun_pol_t *itp;
1318f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
1319db061fdfSdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
1320005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
13218810c16bSdanmcd 
13228810c16bSdanmcd 	if (tunname != NULL) {
13238810c16bSdanmcd 		tname = (char *)tunname->spd_if_name;
13248810c16bSdanmcd 		if (*tname == '\0') {
1325f4b3ec61Sdh 			/* can't fail */
1326db061fdfSdanmcd 			ipsec_swap_global_policy(ns);
1327005d3febSMarek Pospisil 			if (auditing) {
1328799bd290Spwernau 				boolean_t active;
1329799bd290Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1330de8c4a14SErik Nordmark 				cred_t *cr;
1331de8c4a14SErik Nordmark 				pid_t cpid;
1332799bd290Spwernau 
1333de8c4a14SErik Nordmark 				cr = msg_getcred(mp, &cpid);
1334799bd290Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1335de8c4a14SErik Nordmark 				audit_pf_policy(SPD_FLIP, cr, ns,
1336de8c4a14SErik Nordmark 				    NULL, active, 0, cpid);
1337799bd290Spwernau 			}
1338db061fdfSdanmcd 			itp_walk(spdsock_flip_node, NULL, ns);
1339005d3febSMarek Pospisil 			if (auditing) {
1340799bd290Spwernau 				boolean_t active;
1341799bd290Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1342de8c4a14SErik Nordmark 				cred_t *cr;
1343de8c4a14SErik Nordmark 				pid_t cpid;
1344799bd290Spwernau 
1345de8c4a14SErik Nordmark 				cr = msg_getcred(mp, &cpid);
1346799bd290Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1347de8c4a14SErik Nordmark 				audit_pf_policy(SPD_FLIP, cr, ns,
1348de8c4a14SErik Nordmark 				    "all tunnels", active, 0, cpid);
1349799bd290Spwernau 			}
13508810c16bSdanmcd 		} else {
1351db061fdfSdanmcd 			itp = get_tunnel_policy(tname, ns);
13528810c16bSdanmcd 			if (itp == NULL) {
13538810c16bSdanmcd 				/* Better idea for "tunnel not found"? */
13548810c16bSdanmcd 				spdsock_error(q, mp, ESRCH, 0);
1355005d3febSMarek Pospisil 				if (auditing) {
1356799bd290Spwernau 					boolean_t active;
1357799bd290Spwernau 					spd_msg_t *spmsg =
1358799bd290Spwernau 					    (spd_msg_t *)mp->b_rptr;
1359de8c4a14SErik Nordmark 					cred_t *cr;
1360de8c4a14SErik Nordmark 					pid_t cpid;
1361799bd290Spwernau 
1362de8c4a14SErik Nordmark 					cr = msg_getcred(mp, &cpid);
1363799bd290Spwernau 					active = (spmsg->spd_msg_spdid ==
1364799bd290Spwernau 					    SPD_ACTIVE);
1365de8c4a14SErik Nordmark 					audit_pf_policy(SPD_FLIP, cr, ns,
1366de8c4a14SErik Nordmark 					    ITP_NAME(itp), active,
1367de8c4a14SErik Nordmark 					    ESRCH, cpid);
1368799bd290Spwernau 				}
13698810c16bSdanmcd 				return;
13708810c16bSdanmcd 			}
137101ac885fSDan McDonald 			spdsock_flip_node(itp, NULL, ns);
1372005d3febSMarek Pospisil 			if (auditing) {
1373799bd290Spwernau 				boolean_t active;
1374799bd290Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1375de8c4a14SErik Nordmark 				cred_t *cr;
1376de8c4a14SErik Nordmark 				pid_t cpid;
1377799bd290Spwernau 
1378de8c4a14SErik Nordmark 				cr = msg_getcred(mp, &cpid);
1379799bd290Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1380de8c4a14SErik Nordmark 				audit_pf_policy(SPD_FLIP, cr, ns,
1381de8c4a14SErik Nordmark 				    ITP_NAME(itp), active, 0, cpid);
1382799bd290Spwernau 			}
1383db061fdfSdanmcd 			ITP_REFRELE(itp, ns);
13848810c16bSdanmcd 		}
13858810c16bSdanmcd 	} else {
1386db061fdfSdanmcd 		ipsec_swap_global_policy(ns);	/* can't fail */
1387005d3febSMarek Pospisil 		if (auditing) {
1388799bd290Spwernau 			boolean_t active;
1389799bd290Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1390de8c4a14SErik Nordmark 			cred_t *cr;
1391de8c4a14SErik Nordmark 			pid_t cpid;
1392799bd290Spwernau 
1393de8c4a14SErik Nordmark 			cr = msg_getcred(mp, &cpid);
1394799bd290Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1395de8c4a14SErik Nordmark 			audit_pf_policy(SPD_FLIP, cr,
1396de8c4a14SErik Nordmark 			    ns, NULL, active, 0, cpid);
1397799bd290Spwernau 		}
13988810c16bSdanmcd 	}
13997c478bd9Sstevel@tonic-gate 	spd_echo(q, mp);
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate /*
14037c478bd9Sstevel@tonic-gate  * Unimplemented feature
14047c478bd9Sstevel@tonic-gate  */
14057c478bd9Sstevel@tonic-gate /* ARGSUSED */
14067c478bd9Sstevel@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)14078810c16bSdanmcd spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
14088810c16bSdanmcd     spd_ext_t **extv, ipsec_tun_pol_t *itp)
14097c478bd9Sstevel@tonic-gate {
14107c478bd9Sstevel@tonic-gate 	spdsock_error(q, mp, EINVAL, 0);
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate static mblk_t *
spdsock_dump_ruleset(mblk_t * req,ipsec_policy_head_t * iph,uint32_t count,uint16_t error)14157c478bd9Sstevel@tonic-gate spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph,
14167c478bd9Sstevel@tonic-gate     uint32_t count, uint16_t error)
14177c478bd9Sstevel@tonic-gate {
14187c478bd9Sstevel@tonic-gate 	size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t);
14197c478bd9Sstevel@tonic-gate 	spd_msg_t *msg;
14207c478bd9Sstevel@tonic-gate 	spd_ruleset_ext_t *ruleset;
14217c478bd9Sstevel@tonic-gate 	mblk_t *m = allocb(len, BPRI_HI);
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&iph->iph_lock));
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	if (m == NULL) {
14267c478bd9Sstevel@tonic-gate 		return (NULL);
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 	msg = (spd_msg_t *)m->b_rptr;
14297c478bd9Sstevel@tonic-gate 	ruleset = (spd_ruleset_ext_t *)(&msg[1]);
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	m->b_wptr = (uint8_t *)&ruleset[1];
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	*msg = *(spd_msg_t *)(req->b_rptr);
14347c478bd9Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(len);
14357c478bd9Sstevel@tonic-gate 	msg->spd_msg_errno = error;
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset));
14387c478bd9Sstevel@tonic-gate 	ruleset->spd_ruleset_type = SPD_EXT_RULESET;
14397c478bd9Sstevel@tonic-gate 	ruleset->spd_ruleset_count = count;
14407c478bd9Sstevel@tonic-gate 	ruleset->spd_ruleset_version = iph->iph_gen;
14417c478bd9Sstevel@tonic-gate 	return (m);
14427c478bd9Sstevel@tonic-gate }
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate static mblk_t *
spdsock_dump_finish(spdsock_t * ss,int error)14457c478bd9Sstevel@tonic-gate spdsock_dump_finish(spdsock_t *ss, int error)
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	mblk_t *m;
14487c478bd9Sstevel@tonic-gate 	ipsec_policy_head_t *iph = ss->spdsock_dump_head;
14497c478bd9Sstevel@tonic-gate 	mblk_t *req = ss->spdsock_dump_req;
14506be82dacSDan McDonald 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
14537c478bd9Sstevel@tonic-gate 	m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error);
14547c478bd9Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
14556be82dacSDan McDonald 	IPPH_REFRELE(iph, ns);
14566be82dacSDan McDonald 	if (ss->spdsock_itp != NULL) {
14576be82dacSDan McDonald 		ITP_REFRELE(ss->spdsock_itp, ns);
14586be82dacSDan McDonald 		ss->spdsock_itp = NULL;
14596be82dacSDan McDonald 	}
14607c478bd9Sstevel@tonic-gate 	ss->spdsock_dump_req = NULL;
14617c478bd9Sstevel@tonic-gate 	freemsg(req);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	return (m);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate /*
14677c478bd9Sstevel@tonic-gate  * Rule encoding functions.
14687c478bd9Sstevel@tonic-gate  * We do a two-pass encode.
14697c478bd9Sstevel@tonic-gate  * If base != NULL, fill in encoded rule part starting at base+offset.
14707c478bd9Sstevel@tonic-gate  * Always return "offset" plus length of to-be-encoded data.
14717c478bd9Sstevel@tonic-gate  */
14727c478bd9Sstevel@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)14737c478bd9Sstevel@tonic-gate spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type,
14747c478bd9Sstevel@tonic-gate     uint8_t type_end, uint8_t code, uint8_t code_end)
14757c478bd9Sstevel@tonic-gate {
14767c478bd9Sstevel@tonic-gate 	struct spd_typecode *tcp;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if (base != NULL) {
14817c478bd9Sstevel@tonic-gate 		tcp = (struct spd_typecode *)(base + offset);
14827c478bd9Sstevel@tonic-gate 		tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp));
14837c478bd9Sstevel@tonic-gate 		tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE;
14847c478bd9Sstevel@tonic-gate 		tcp->spd_typecode_code = code;
14857c478bd9Sstevel@tonic-gate 		tcp->spd_typecode_type = type;
14867c478bd9Sstevel@tonic-gate 		tcp->spd_typecode_type_end = type_end;
14877c478bd9Sstevel@tonic-gate 		tcp->spd_typecode_code_end = code_end;
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 	offset += sizeof (*tcp);
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	return (offset);
14947c478bd9Sstevel@tonic-gate }
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_proto(uint8_t * base,uint_t offset,uint8_t proto)14977c478bd9Sstevel@tonic-gate spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto)
14987c478bd9Sstevel@tonic-gate {
14997c478bd9Sstevel@tonic-gate 	struct spd_proto *spp;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	if (base != NULL) {
15047c478bd9Sstevel@tonic-gate 		spp = (struct spd_proto *)(base + offset);
15057c478bd9Sstevel@tonic-gate 		spp->spd_proto_len = SPD_8TO64(sizeof (*spp));
15067c478bd9Sstevel@tonic-gate 		spp->spd_proto_exttype = SPD_EXT_PROTO;
15077c478bd9Sstevel@tonic-gate 		spp->spd_proto_number = proto;
15087c478bd9Sstevel@tonic-gate 		spp->spd_proto_reserved1 = 0;
15097c478bd9Sstevel@tonic-gate 		spp->spd_proto_reserved2 = 0;
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 	offset += sizeof (*spp);
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	return (offset);
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_port(uint8_t * base,uint_t offset,uint16_t ext,uint16_t port)15197c478bd9Sstevel@tonic-gate spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	struct spd_portrange *spp;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	if (base != NULL) {
15267c478bd9Sstevel@tonic-gate 		spp = (struct spd_portrange *)(base + offset);
15277c478bd9Sstevel@tonic-gate 		spp->spd_ports_len = SPD_8TO64(sizeof (*spp));
15287c478bd9Sstevel@tonic-gate 		spp->spd_ports_exttype = ext;
15297c478bd9Sstevel@tonic-gate 		spp->spd_ports_minport = port;
15307c478bd9Sstevel@tonic-gate 		spp->spd_ports_maxport = port;
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 	offset += sizeof (*spp);
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	return (offset);
15377c478bd9Sstevel@tonic-gate }
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@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)15407c478bd9Sstevel@tonic-gate spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext,
15417c478bd9Sstevel@tonic-gate     const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen)
15427c478bd9Sstevel@tonic-gate {
15437c478bd9Sstevel@tonic-gate 	struct spd_address *sae;
15447c478bd9Sstevel@tonic-gate 	ipsec_addr_t *spdaddr;
15457c478bd9Sstevel@tonic-gate 	uint_t start = offset;
15467c478bd9Sstevel@tonic-gate 	uint_t addrlen;
15477c478bd9Sstevel@tonic-gate 	uint_t af;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	if (sel->ipsl_valid & IPSL_IPV4) {
15507c478bd9Sstevel@tonic-gate 		af = AF_INET;
15517c478bd9Sstevel@tonic-gate 		addrlen = IP_ADDR_LEN;
15527c478bd9Sstevel@tonic-gate 	} else {
15537c478bd9Sstevel@tonic-gate 		af = AF_INET6;
15547c478bd9Sstevel@tonic-gate 		addrlen = IPV6_ADDR_LEN;
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (base != NULL) {
15607c478bd9Sstevel@tonic-gate 		sae = (struct spd_address *)(base + offset);
15617c478bd9Sstevel@tonic-gate 		sae->spd_address_exttype = ext;
15627c478bd9Sstevel@tonic-gate 		sae->spd_address_af = af;
15637c478bd9Sstevel@tonic-gate 		sae->spd_address_prefixlen = pfxlen;
15647c478bd9Sstevel@tonic-gate 		sae->spd_address_reserved2 = 0;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		spdaddr = (ipsec_addr_t *)(&sae[1]);
15677c478bd9Sstevel@tonic-gate 		bcopy(addr, spdaddr, addrlen);
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 	offset += sizeof (*sae);
15707c478bd9Sstevel@tonic-gate 	addrlen = roundup(addrlen, sizeof (uint64_t));
15717c478bd9Sstevel@tonic-gate 	offset += addrlen;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	if (base != NULL)
15767c478bd9Sstevel@tonic-gate 		sae->spd_address_len = SPD_8TO64(offset - start);
15777c478bd9Sstevel@tonic-gate 	return (offset);
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_sel(uint8_t * base,uint_t offset,const ipsec_sel_t * sel)15817c478bd9Sstevel@tonic-gate spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	const ipsec_selkey_t *selkey = &sel->ipsl_key;
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_PROTOCOL)
15867c478bd9Sstevel@tonic-gate 		offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto);
15877c478bd9Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_LOCAL_PORT)
15887c478bd9Sstevel@tonic-gate 		offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT,
15897c478bd9Sstevel@tonic-gate 		    selkey->ipsl_lport);
15907c478bd9Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_REMOTE_PORT)
15917c478bd9Sstevel@tonic-gate 		offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT,
15927c478bd9Sstevel@tonic-gate 		    selkey->ipsl_rport);
15937c478bd9Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_REMOTE_ADDR)
15947c478bd9Sstevel@tonic-gate 		offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR,
15957c478bd9Sstevel@tonic-gate 		    selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen);
15967c478bd9Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_LOCAL_ADDR)
15977c478bd9Sstevel@tonic-gate 		offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR,
15987c478bd9Sstevel@tonic-gate 		    selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen);
15997c478bd9Sstevel@tonic-gate 	if (selkey->ipsl_valid & IPSL_ICMP_TYPE) {
16007c478bd9Sstevel@tonic-gate 		offset = spdsock_encode_typecode(base, offset,
16017c478bd9Sstevel@tonic-gate 		    selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end,
16027c478bd9Sstevel@tonic-gate 		    (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
1603fc80c0dfSnordmark 		    selkey->ipsl_icmp_code : 255,
16047c478bd9Sstevel@tonic-gate 		    (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
1605fc80c0dfSnordmark 		    selkey->ipsl_icmp_code_end : 255);
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 	return (offset);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_actattr(uint8_t * base,uint_t offset,uint32_t tag,uint32_t value)16117c478bd9Sstevel@tonic-gate spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag,
16127c478bd9Sstevel@tonic-gate     uint32_t value)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	struct spd_attribute *attr;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	if (base != NULL) {
16197c478bd9Sstevel@tonic-gate 		attr = (struct spd_attribute *)(base + offset);
16207c478bd9Sstevel@tonic-gate 		attr->spd_attr_tag = tag;
16217c478bd9Sstevel@tonic-gate 		attr->spd_attr_value = value;
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 	offset += sizeof (struct spd_attribute);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	return (offset);
16287c478bd9Sstevel@tonic-gate }
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate #define	EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v))
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_action(uint8_t * base,uint_t offset,const ipsec_action_t * ap)16347c478bd9Sstevel@tonic-gate spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap)
16357c478bd9Sstevel@tonic-gate {
16367c478bd9Sstevel@tonic-gate 	const struct ipsec_act *act = &(ap->ipa_act);
16377c478bd9Sstevel@tonic-gate 	uint_t flags;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	EMIT(SPD_ATTR_EMPTY, 0);
16407c478bd9Sstevel@tonic-gate 	switch (act->ipa_type) {
16417c478bd9Sstevel@tonic-gate 	case IPSEC_ACT_DISCARD:
16427c478bd9Sstevel@tonic-gate 	case IPSEC_ACT_REJECT:
16437c478bd9Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP);
16447c478bd9Sstevel@tonic-gate 		break;
16457c478bd9Sstevel@tonic-gate 	case IPSEC_ACT_BYPASS:
16467c478bd9Sstevel@tonic-gate 	case IPSEC_ACT_CLEAR:
16477c478bd9Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS);
16487c478bd9Sstevel@tonic-gate 		break;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	case IPSEC_ACT_APPLY:
16517c478bd9Sstevel@tonic-gate 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC);
16527c478bd9Sstevel@tonic-gate 		flags = 0;
16537c478bd9Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_ah)
16547c478bd9Sstevel@tonic-gate 			flags |= SPD_APPLY_AH;
16557c478bd9Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_esp)
16567c478bd9Sstevel@tonic-gate 			flags |= SPD_APPLY_ESP;
16577c478bd9Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_espa)
16587c478bd9Sstevel@tonic-gate 			flags |= SPD_APPLY_ESPA;
16597c478bd9Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_se)
16607c478bd9Sstevel@tonic-gate 			flags |= SPD_APPLY_SE;
16617c478bd9Sstevel@tonic-gate 		if (act->ipa_apply.ipp_use_unique)
16627c478bd9Sstevel@tonic-gate 			flags |= SPD_APPLY_UNIQUE;
16637c478bd9Sstevel@tonic-gate 		EMIT(SPD_ATTR_FLAGS, flags);
16647c478bd9Sstevel@tonic-gate 		if (flags & SPD_APPLY_AH) {
16657c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg);
16667c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_MINBITS,
16677c478bd9Sstevel@tonic-gate 			    act->ipa_apply.ipp_ah_minbits);
16687c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_AH_MAXBITS,
16697c478bd9Sstevel@tonic-gate 			    act->ipa_apply.ipp_ah_maxbits);
16707c478bd9Sstevel@tonic-gate 		}
16717c478bd9Sstevel@tonic-gate 		if (flags & SPD_APPLY_ESP) {
16727c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg);
16737c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ENCR_MINBITS,
16747c478bd9Sstevel@tonic-gate 			    act->ipa_apply.ipp_espe_minbits);
16757c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ENCR_MAXBITS,
16767c478bd9Sstevel@tonic-gate 			    act->ipa_apply.ipp_espe_maxbits);
16777c478bd9Sstevel@tonic-gate 			if (flags & SPD_APPLY_ESPA) {
16787c478bd9Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESP_AUTH,
16797c478bd9Sstevel@tonic-gate 				    act->ipa_apply.ipp_esp_auth_alg);
16807c478bd9Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESPA_MINBITS,
16817c478bd9Sstevel@tonic-gate 				    act->ipa_apply.ipp_espa_minbits);
16827c478bd9Sstevel@tonic-gate 				EMIT(SPD_ATTR_ESPA_MAXBITS,
16837c478bd9Sstevel@tonic-gate 				    act->ipa_apply.ipp_espa_maxbits);
16847c478bd9Sstevel@tonic-gate 			}
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 		if (act->ipa_apply.ipp_km_proto != 0)
16877c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto);
16887c478bd9Sstevel@tonic-gate 		if (act->ipa_apply.ipp_km_cookie != 0)
16897c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie);
16907c478bd9Sstevel@tonic-gate 		if (act->ipa_apply.ipp_replay_depth != 0)
16917c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_REPLAY_DEPTH,
16927c478bd9Sstevel@tonic-gate 			    act->ipa_apply.ipp_replay_depth);
16937c478bd9Sstevel@tonic-gate 		/* Add more here */
16947c478bd9Sstevel@tonic-gate 		break;
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	return (offset);
16987c478bd9Sstevel@tonic-gate }
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_action_list(uint8_t * base,uint_t offset,const ipsec_action_t * ap)17017c478bd9Sstevel@tonic-gate spdsock_encode_action_list(uint8_t *base, uint_t offset,
17027c478bd9Sstevel@tonic-gate     const ipsec_action_t *ap)
17037c478bd9Sstevel@tonic-gate {
17047c478bd9Sstevel@tonic-gate 	struct spd_ext_actions *act;
17057c478bd9Sstevel@tonic-gate 	uint_t nact = 0;
17067c478bd9Sstevel@tonic-gate 	uint_t start = offset;
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	if (base != NULL) {
17117c478bd9Sstevel@tonic-gate 		act = (struct spd_ext_actions *)(base + offset);
17127c478bd9Sstevel@tonic-gate 		act->spd_actions_len = 0;
17137c478bd9Sstevel@tonic-gate 		act->spd_actions_exttype = SPD_EXT_ACTION;
17147c478bd9Sstevel@tonic-gate 		act->spd_actions_count = 0;
17157c478bd9Sstevel@tonic-gate 		act->spd_actions_reserved = 0;
17167c478bd9Sstevel@tonic-gate 	}
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	offset += sizeof (*act);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	while (ap != NULL) {
17237c478bd9Sstevel@tonic-gate 		offset = spdsock_encode_action(base, offset, ap);
17247c478bd9Sstevel@tonic-gate 		ap = ap->ipa_next;
17257c478bd9Sstevel@tonic-gate 		nact++;
17267c478bd9Sstevel@tonic-gate 		if (ap != NULL) {
17277c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_NEXT, 0);
17287c478bd9Sstevel@tonic-gate 		}
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 	EMIT(SPD_ATTR_END, 0);
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	if (base != NULL) {
17357c478bd9Sstevel@tonic-gate 		act->spd_actions_count = nact;
17367c478bd9Sstevel@tonic-gate 		act->spd_actions_len = SPD_8TO64(offset - start);
17377c478bd9Sstevel@tonic-gate 	}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	return (offset);
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate #undef EMIT
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate /* ARGSUSED */
17457c478bd9Sstevel@tonic-gate static uint_t
spdsock_rule_flags(uint_t dir,uint_t af)17467c478bd9Sstevel@tonic-gate spdsock_rule_flags(uint_t dir, uint_t af)
17477c478bd9Sstevel@tonic-gate {
17487c478bd9Sstevel@tonic-gate 	uint_t flags = 0;
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	if (dir == IPSEC_TYPE_INBOUND)
17517c478bd9Sstevel@tonic-gate 		flags |= SPD_RULE_FLAG_INBOUND;
17527c478bd9Sstevel@tonic-gate 	if (dir == IPSEC_TYPE_OUTBOUND)
17537c478bd9Sstevel@tonic-gate 		flags |= SPD_RULE_FLAG_OUTBOUND;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	return (flags);
17567c478bd9Sstevel@tonic-gate }
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@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)17608810c16bSdanmcd spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req,
17618810c16bSdanmcd     const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name,
17628810c16bSdanmcd     boolean_t tunnel)
17637c478bd9Sstevel@tonic-gate {
17647c478bd9Sstevel@tonic-gate 	struct spd_msg *spmsg;
17657c478bd9Sstevel@tonic-gate 	struct spd_rule *spr;
17668810c16bSdanmcd 	spd_if_t *sid;
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	uint_t start = offset;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	if (base != NULL) {
17737c478bd9Sstevel@tonic-gate 		spmsg = (struct spd_msg *)(base + offset);
17747c478bd9Sstevel@tonic-gate 		bzero(spmsg, sizeof (*spmsg));
17757c478bd9Sstevel@tonic-gate 		spmsg->spd_msg_version = PF_POLICY_V1;
17767c478bd9Sstevel@tonic-gate 		spmsg->spd_msg_type = SPD_DUMP;
17777c478bd9Sstevel@tonic-gate 		spmsg->spd_msg_seq = req->spd_msg_seq;
17787c478bd9Sstevel@tonic-gate 		spmsg->spd_msg_pid = req->spd_msg_pid;
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 	offset += sizeof (struct spd_msg);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	if (base != NULL) {
17857c478bd9Sstevel@tonic-gate 		spr = (struct spd_rule *)(base + offset);
17867c478bd9Sstevel@tonic-gate 		spr->spd_rule_type = SPD_EXT_RULE;
17877c478bd9Sstevel@tonic-gate 		spr->spd_rule_priority = rule->ipsp_prio;
17887c478bd9Sstevel@tonic-gate 		spr->spd_rule_flags = spdsock_rule_flags(dir, af);
17898810c16bSdanmcd 		if (tunnel)
17908810c16bSdanmcd 			spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL;
17917c478bd9Sstevel@tonic-gate 		spr->spd_rule_unused = 0;
17927c478bd9Sstevel@tonic-gate 		spr->spd_rule_len = SPD_8TO64(sizeof (*spr));
17937c478bd9Sstevel@tonic-gate 		spr->spd_rule_index = rule->ipsp_index;
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate 	offset += sizeof (struct spd_rule);
17968810c16bSdanmcd 
17978810c16bSdanmcd 	/*
17988810c16bSdanmcd 	 * If we have an interface name (i.e. if this policy head came from
17998810c16bSdanmcd 	 * a tunnel), add the SPD_EXT_TUN_NAME extension.
18008810c16bSdanmcd 	 */
1801cceae6f8Sdanmcd 	if (name != NULL) {
18028810c16bSdanmcd 
18038810c16bSdanmcd 		ASSERT(ALIGNED64(offset));
18048810c16bSdanmcd 
18058810c16bSdanmcd 		if (base != NULL) {
18068810c16bSdanmcd 			sid = (spd_if_t *)(base + offset);
18078810c16bSdanmcd 			sid->spd_if_exttype = SPD_EXT_TUN_NAME;
18088810c16bSdanmcd 			sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) +
18098810c16bSdanmcd 			    roundup((strlen(name) - 4), 8));
18108810c16bSdanmcd 			(void) strlcpy((char *)sid->spd_if_name, name,
18118810c16bSdanmcd 			    LIFNAMSIZ);
18128810c16bSdanmcd 		}
18138810c16bSdanmcd 
18148810c16bSdanmcd 		offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8);
18158810c16bSdanmcd 	}
18168810c16bSdanmcd 
18177c478bd9Sstevel@tonic-gate 	offset = spdsock_encode_sel(base, offset, rule->ipsp_sel);
18187c478bd9Sstevel@tonic-gate 	offset = spdsock_encode_action_list(base, offset, rule->ipsp_act);
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(offset));
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	if (base != NULL) {
18237c478bd9Sstevel@tonic-gate 		spmsg->spd_msg_len = SPD_8TO64(offset - start);
18247c478bd9Sstevel@tonic-gate 	}
18257c478bd9Sstevel@tonic-gate 	return (offset);
18267c478bd9Sstevel@tonic-gate }
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate /* ARGSUSED */
18297c478bd9Sstevel@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)18307c478bd9Sstevel@tonic-gate spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule,
18318810c16bSdanmcd     uint_t dir, uint_t af, char *name, boolean_t tunnel)
18327c478bd9Sstevel@tonic-gate {
18337c478bd9Sstevel@tonic-gate 	mblk_t *m;
18347c478bd9Sstevel@tonic-gate 	uint_t len;
18357c478bd9Sstevel@tonic-gate 	spd_msg_t *mreq = (spd_msg_t *)req->b_rptr;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	/*
18387c478bd9Sstevel@tonic-gate 	 * Figure out how much space we'll need.
18397c478bd9Sstevel@tonic-gate 	 */
18408810c16bSdanmcd 	len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name,
18418810c16bSdanmcd 	    tunnel);
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	/*
18447c478bd9Sstevel@tonic-gate 	 * Allocate mblk.
18457c478bd9Sstevel@tonic-gate 	 */
18467c478bd9Sstevel@tonic-gate 	m = allocb(len, BPRI_HI);
18477c478bd9Sstevel@tonic-gate 	if (m == NULL)
18487c478bd9Sstevel@tonic-gate 		return (NULL);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/*
18517c478bd9Sstevel@tonic-gate 	 * Fill it in..
18527c478bd9Sstevel@tonic-gate 	 */
18537c478bd9Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + len;
18547c478bd9Sstevel@tonic-gate 	bzero(m->b_rptr, len);
18558810c16bSdanmcd 	(void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af,
18568810c16bSdanmcd 	    name, tunnel);
18577c478bd9Sstevel@tonic-gate 	return (m);
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate 
18606a182920Ssommerfe static ipsec_policy_t *
spdsock_dump_next_in_chain(spdsock_t * ss,ipsec_policy_head_t * iph,ipsec_policy_t * cur)18616a182920Ssommerfe spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph,
18626a182920Ssommerfe     ipsec_policy_t *cur)
18636a182920Ssommerfe {
18646a182920Ssommerfe 	ASSERT(RW_READ_HELD(&iph->iph_lock));
18656a182920Ssommerfe 
18666a182920Ssommerfe 	ss->spdsock_dump_count++;
18676a182920Ssommerfe 	ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next;
18686a182920Ssommerfe 	return (cur);
18696a182920Ssommerfe }
18706a182920Ssommerfe 
18717c478bd9Sstevel@tonic-gate static ipsec_policy_t *
spdsock_dump_next_rule(spdsock_t * ss,ipsec_policy_head_t * iph)18727c478bd9Sstevel@tonic-gate spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph)
18737c478bd9Sstevel@tonic-gate {
18747c478bd9Sstevel@tonic-gate 	ipsec_policy_t *cur;
18756a182920Ssommerfe 	ipsec_policy_root_t *ipr;
18766a182920Ssommerfe 	int chain, nchains, type, af;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&iph->iph_lock));
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	cur = ss->spdsock_dump_cur_rule;
18817c478bd9Sstevel@tonic-gate 
18826a182920Ssommerfe 	if (cur != NULL)
18836a182920Ssommerfe 		return (spdsock_dump_next_in_chain(ss, iph, cur));
18846a182920Ssommerfe 
18856a182920Ssommerfe 	type = ss->spdsock_dump_cur_type;
18866a182920Ssommerfe 
18876a182920Ssommerfe next:
18886a182920Ssommerfe 	chain = ss->spdsock_dump_cur_chain;
18896a182920Ssommerfe 	ipr = &iph->iph_root[type];
18906a182920Ssommerfe 	nchains = ipr->ipr_nchains;
18916a182920Ssommerfe 
18926a182920Ssommerfe 	while (chain < nchains) {
18936a182920Ssommerfe 		cur = ipr->ipr_hash[chain].hash_head;
18946a182920Ssommerfe 		chain++;
18956a182920Ssommerfe 		if (cur != NULL) {
18966a182920Ssommerfe 			ss->spdsock_dump_cur_chain = chain;
18976a182920Ssommerfe 			return (spdsock_dump_next_in_chain(ss, iph, cur));
18986a182920Ssommerfe 		}
18997c478bd9Sstevel@tonic-gate 	}
19006a182920Ssommerfe 	ss->spdsock_dump_cur_chain = nchains;
19016a182920Ssommerfe 
19026a182920Ssommerfe 	af = ss->spdsock_dump_cur_af;
19036a182920Ssommerfe 	while (af < IPSEC_NAF) {
19046a182920Ssommerfe 		cur = ipr->ipr_nonhash[af];
19056a182920Ssommerfe 		af++;
19066a182920Ssommerfe 		if (cur != NULL) {
19076a182920Ssommerfe 			ss->spdsock_dump_cur_af = af;
19086a182920Ssommerfe 			return (spdsock_dump_next_in_chain(ss, iph, cur));
19096a182920Ssommerfe 		}
19106a182920Ssommerfe 	}
19116a182920Ssommerfe 
19126a182920Ssommerfe 	type++;
19136a182920Ssommerfe 	if (type >= IPSEC_NTYPES)
19146a182920Ssommerfe 		return (NULL);
19156a182920Ssommerfe 
19166a182920Ssommerfe 	ss->spdsock_dump_cur_chain = 0;
19176a182920Ssommerfe 	ss->spdsock_dump_cur_type = type;
19186a182920Ssommerfe 	ss->spdsock_dump_cur_af = IPSEC_AF_V4;
19196a182920Ssommerfe 	goto next;
19206a182920Ssommerfe 
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate 
1923db061fdfSdanmcd /*
1924db061fdfSdanmcd  * If we're done with one policy head, but have more to go, we iterate through
1925db061fdfSdanmcd  * another IPsec tunnel policy head (itp).  Return NULL if it is an error
1926db061fdfSdanmcd  * worthy of returning EAGAIN via PF_POLICY.
1927db061fdfSdanmcd  */
1928db061fdfSdanmcd static ipsec_tun_pol_t *
spdsock_dump_iterate_next_tunnel(spdsock_t * ss,ipsec_stack_t * ipss)1929db061fdfSdanmcd spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss)
1930db061fdfSdanmcd {
1931db061fdfSdanmcd 	ipsec_tun_pol_t *itp;
1932db061fdfSdanmcd 
1933db061fdfSdanmcd 	ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock));
1934db061fdfSdanmcd 	if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) {
1935db061fdfSdanmcd 		/* Oops, state of the tunnel polheads changed. */
1936db061fdfSdanmcd 		itp = NULL;
1937db061fdfSdanmcd 	} else if (ss->spdsock_itp == NULL) {
1938db061fdfSdanmcd 		/* Just finished global, find first node. */
1939db061fdfSdanmcd 		itp = avl_first(&ipss->ipsec_tunnel_policies);
1940db061fdfSdanmcd 	} else {
1941db061fdfSdanmcd 		/* We just finished current polhead, find the next one. */
1942db061fdfSdanmcd 		itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp);
1943db061fdfSdanmcd 	}
1944db061fdfSdanmcd 	if (itp != NULL) {
1945db061fdfSdanmcd 		ITP_REFHOLD(itp);
1946db061fdfSdanmcd 	}
1947db061fdfSdanmcd 	if (ss->spdsock_itp != NULL) {
1948db061fdfSdanmcd 		ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack);
1949db061fdfSdanmcd 	}
1950db061fdfSdanmcd 	ss->spdsock_itp = itp;
1951db061fdfSdanmcd 	return (itp);
1952db061fdfSdanmcd }
1953db061fdfSdanmcd 
19547c478bd9Sstevel@tonic-gate static mblk_t *
spdsock_dump_next_record(spdsock_t * ss)19557c478bd9Sstevel@tonic-gate spdsock_dump_next_record(spdsock_t *ss)
19567c478bd9Sstevel@tonic-gate {
19577c478bd9Sstevel@tonic-gate 	ipsec_policy_head_t *iph;
19587c478bd9Sstevel@tonic-gate 	ipsec_policy_t *rule;
19597c478bd9Sstevel@tonic-gate 	mblk_t *m;
1960db061fdfSdanmcd 	ipsec_tun_pol_t *itp;
1961db061fdfSdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
1962db061fdfSdanmcd 	ipsec_stack_t *ipss = ns->netstack_ipsec;
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	iph = ss->spdsock_dump_head;
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	ASSERT(iph != NULL);
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	if (iph->iph_gen != ss->spdsock_dump_gen) {
19717c478bd9Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
19727c478bd9Sstevel@tonic-gate 		return (spdsock_dump_finish(ss, EAGAIN));
19737c478bd9Sstevel@tonic-gate 	}
19747c478bd9Sstevel@tonic-gate 
1975db061fdfSdanmcd 	while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) {
19767c478bd9Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
1977db061fdfSdanmcd 		if (--(ss->spdsock_dump_remaining_polheads) == 0)
1978db061fdfSdanmcd 			return (spdsock_dump_finish(ss, 0));
1979db061fdfSdanmcd 
1980db061fdfSdanmcd 
1981db061fdfSdanmcd 		/*
1982db061fdfSdanmcd 		 * If we reach here, we have more policy heads (tunnel
1983db061fdfSdanmcd 		 * entries) to dump.  Let's reset to a new policy head
1984db061fdfSdanmcd 		 * and get some more rules.
1985db061fdfSdanmcd 		 *
1986db061fdfSdanmcd 		 * An empty policy head will have spdsock_dump_next_rule()
1987db061fdfSdanmcd 		 * return NULL, and we loop (while dropping the number of
1988db061fdfSdanmcd 		 * remaining polheads).  If we loop to 0, we finish.  We
1989db061fdfSdanmcd 		 * keep looping until we hit 0 or until we have a rule to
1990db061fdfSdanmcd 		 * encode.
1991db061fdfSdanmcd 		 *
1992db061fdfSdanmcd 		 * NOTE:  No need for ITP_REF*() macros here as we're only
1993db061fdfSdanmcd 		 * going after and refholding the policy head itself.
1994db061fdfSdanmcd 		 */
1995db061fdfSdanmcd 		rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
1996db061fdfSdanmcd 		itp = spdsock_dump_iterate_next_tunnel(ss, ipss);
1997db061fdfSdanmcd 		if (itp == NULL) {
1998db061fdfSdanmcd 			rw_exit(&ipss->ipsec_tunnel_policy_lock);
1999db061fdfSdanmcd 			return (spdsock_dump_finish(ss, EAGAIN));
2000db061fdfSdanmcd 		}
2001db061fdfSdanmcd 
2002db061fdfSdanmcd 		/* Reset other spdsock_dump thingies. */
2003db061fdfSdanmcd 		IPPH_REFRELE(ss->spdsock_dump_head, ns);
2004db061fdfSdanmcd 		if (ss->spdsock_dump_active) {
2005db061fdfSdanmcd 			ss->spdsock_dump_tunnel =
2006db061fdfSdanmcd 			    itp->itp_flags & ITPF_P_TUNNEL;
2007db061fdfSdanmcd 			iph = itp->itp_policy;
2008db061fdfSdanmcd 		} else {
2009db061fdfSdanmcd 			ss->spdsock_dump_tunnel =
2010db061fdfSdanmcd 			    itp->itp_flags & ITPF_I_TUNNEL;
2011db061fdfSdanmcd 			iph = itp->itp_inactive;
2012db061fdfSdanmcd 		}
2013db061fdfSdanmcd 		IPPH_REFHOLD(iph);
2014db061fdfSdanmcd 		rw_exit(&ipss->ipsec_tunnel_policy_lock);
2015db061fdfSdanmcd 
2016db061fdfSdanmcd 		rw_enter(&iph->iph_lock, RW_READER);
2017db061fdfSdanmcd 		RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
20187c478bd9Sstevel@tonic-gate 	}
20197c478bd9Sstevel@tonic-gate 
2020db061fdfSdanmcd 	m = spdsock_encode_rule(ss->spdsock_dump_req, rule,
2021db061fdfSdanmcd 	    ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af,
2022cceae6f8Sdanmcd 	    (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name,
2023cceae6f8Sdanmcd 	    ss->spdsock_dump_tunnel);
20247c478bd9Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	if (m == NULL)
20277c478bd9Sstevel@tonic-gate 		return (spdsock_dump_finish(ss, ENOMEM));
20287c478bd9Sstevel@tonic-gate 	return (m);
20297c478bd9Sstevel@tonic-gate }
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate /*
20327c478bd9Sstevel@tonic-gate  * Dump records until we run into flow-control back-pressure.
20337c478bd9Sstevel@tonic-gate  */
20347c478bd9Sstevel@tonic-gate static void
spdsock_dump_some(queue_t * q,spdsock_t * ss)20357c478bd9Sstevel@tonic-gate spdsock_dump_some(queue_t *q, spdsock_t *ss)
20367c478bd9Sstevel@tonic-gate {
20377c478bd9Sstevel@tonic-gate 	mblk_t *m, *dataind;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	while ((ss->spdsock_dump_req != NULL) && canputnext(q)) {
20407c478bd9Sstevel@tonic-gate 		m = spdsock_dump_next_record(ss);
20417c478bd9Sstevel@tonic-gate 		if (m == NULL)
20427c478bd9Sstevel@tonic-gate 			return;
20437c478bd9Sstevel@tonic-gate 		dataind = allocb(sizeof (struct T_data_req), BPRI_HI);
20447c478bd9Sstevel@tonic-gate 		if (dataind == NULL) {
20457c478bd9Sstevel@tonic-gate 			freemsg(m);
20467c478bd9Sstevel@tonic-gate 			return;
20477c478bd9Sstevel@tonic-gate 		}
20487c478bd9Sstevel@tonic-gate 		dataind->b_cont = m;
20497c478bd9Sstevel@tonic-gate 		dataind->b_wptr += sizeof (struct T_data_req);
20507c478bd9Sstevel@tonic-gate 		((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND;
20517c478bd9Sstevel@tonic-gate 		((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0;
20527c478bd9Sstevel@tonic-gate 		dataind->b_datap->db_type = M_PROTO;
20537c478bd9Sstevel@tonic-gate 		putnext(q, dataind);
20547c478bd9Sstevel@tonic-gate 	}
20557c478bd9Sstevel@tonic-gate }
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate /*
20587c478bd9Sstevel@tonic-gate  * Start dumping.
20597c478bd9Sstevel@tonic-gate  * Format a start-of-dump record, and set up the stream and kick the rsrv
20607c478bd9Sstevel@tonic-gate  * procedure to continue the job..
20617c478bd9Sstevel@tonic-gate  */
20627c478bd9Sstevel@tonic-gate /* ARGSUSED */
20637c478bd9Sstevel@tonic-gate static void
spdsock_dump(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp)20648810c16bSdanmcd spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp)
20657c478bd9Sstevel@tonic-gate {
20667c478bd9Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2067db061fdfSdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
2068f4b3ec61Sdh 	ipsec_stack_t *ipss = ns->netstack_ipsec;
20697c478bd9Sstevel@tonic-gate 	mblk_t *mr;
20707c478bd9Sstevel@tonic-gate 
2071db061fdfSdanmcd 	/* spdsock_open() already set spdsock_itp to NULL. */
20728810c16bSdanmcd 	if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
2073f4b3ec61Sdh 		rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
20748810c16bSdanmcd 		ss->spdsock_dump_remaining_polheads = 1 +
2075f4b3ec61Sdh 		    avl_numnodes(&ipss->ipsec_tunnel_policies);
2076f4b3ec61Sdh 		ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen;
2077f4b3ec61Sdh 		rw_exit(&ipss->ipsec_tunnel_policy_lock);
20788810c16bSdanmcd 		if (iph == ALL_ACTIVE_POLHEADS) {
2079f4b3ec61Sdh 			iph = ipsec_system_policy(ns);
20808810c16bSdanmcd 			ss->spdsock_dump_active = B_TRUE;
20818810c16bSdanmcd 		} else {
2082db061fdfSdanmcd 			iph = ipsec_inactive_policy(ns);
20838810c16bSdanmcd 			ss->spdsock_dump_active = B_FALSE;
20848810c16bSdanmcd 		}
2085db061fdfSdanmcd 		ASSERT(ss->spdsock_itp == NULL);
20868810c16bSdanmcd 	} else {
20878810c16bSdanmcd 		ss->spdsock_dump_remaining_polheads = 1;
20888810c16bSdanmcd 	}
20898810c16bSdanmcd 
20907c478bd9Sstevel@tonic-gate 	rw_enter(&iph->iph_lock, RW_READER);
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	mr = spdsock_dump_ruleset(mp, iph, 0, 0);
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	if (!mr) {
20957c478bd9Sstevel@tonic-gate 		rw_exit(&iph->iph_lock);
20967c478bd9Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
20977c478bd9Sstevel@tonic-gate 		return;
20987c478bd9Sstevel@tonic-gate 	}
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	ss->spdsock_dump_req = mp;
2101db061fdfSdanmcd 	RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
2102db061fdfSdanmcd 
21037c478bd9Sstevel@tonic-gate 	rw_exit(&iph->iph_lock);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	qreply(q, mr);
21067c478bd9Sstevel@tonic-gate 	qenable(OTHERQ(q));
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
21098810c16bSdanmcd /* Do NOT consume a reference to ITP. */
21108810c16bSdanmcd void
spdsock_clone_node(ipsec_tun_pol_t * itp,void * ep,netstack_t * ns)2111f4b3ec61Sdh spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns)
21128810c16bSdanmcd {
21138810c16bSdanmcd 	int *errptr = (int *)ep;
21148810c16bSdanmcd 
21158810c16bSdanmcd 	if (*errptr != 0)
21168810c16bSdanmcd 		return;	/* We've failed already for some reason. */
21178810c16bSdanmcd 	mutex_enter(&itp->itp_lock);
21188810c16bSdanmcd 	ITPF_CLONE(itp->itp_flags);
2119f4b3ec61Sdh 	*errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns);
21208810c16bSdanmcd 	mutex_exit(&itp->itp_lock);
21218810c16bSdanmcd }
21228810c16bSdanmcd 
21237c478bd9Sstevel@tonic-gate void
spdsock_clone(queue_t * q,mblk_t * mp,spd_if_t * tunname)21248810c16bSdanmcd spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname)
21257c478bd9Sstevel@tonic-gate {
21268810c16bSdanmcd 	int error;
21278810c16bSdanmcd 	char *tname;
21288810c16bSdanmcd 	ipsec_tun_pol_t *itp;
2129f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2130db061fdfSdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
2131005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
21328810c16bSdanmcd 
21338810c16bSdanmcd 	if (tunname != NULL) {
21348810c16bSdanmcd 		tname = (char *)tunname->spd_if_name;
21358810c16bSdanmcd 		if (*tname == '\0') {
2136db061fdfSdanmcd 			error = ipsec_clone_system_policy(ns);
2137005d3febSMarek Pospisil 			if (auditing) {
2138799bd290Spwernau 				boolean_t active;
2139799bd290Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2140de8c4a14SErik Nordmark 				cred_t *cr;
2141de8c4a14SErik Nordmark 				pid_t cpid;
2142799bd290Spwernau 
2143de8c4a14SErik Nordmark 				cr = msg_getcred(mp, &cpid);
2144799bd290Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2145de8c4a14SErik Nordmark 				audit_pf_policy(SPD_CLONE, cr, ns,
2146de8c4a14SErik Nordmark 				    NULL, active, error, cpid);
2147799bd290Spwernau 			}
2148799bd290Spwernau 			if (error == 0) {
2149db061fdfSdanmcd 				itp_walk(spdsock_clone_node, &error, ns);
2150005d3febSMarek Pospisil 				if (auditing) {
2151799bd290Spwernau 					boolean_t active;
2152799bd290Spwernau 					spd_msg_t *spmsg =
2153799bd290Spwernau 					    (spd_msg_t *)mp->b_rptr;
2154de8c4a14SErik Nordmark 					cred_t *cr;
2155de8c4a14SErik Nordmark 					pid_t cpid;
2156799bd290Spwernau 
2157de8c4a14SErik Nordmark 					cr = msg_getcred(mp, &cpid);
2158799bd290Spwernau 					active = (spmsg->spd_msg_spdid ==
2159799bd290Spwernau 					    SPD_ACTIVE);
2160de8c4a14SErik Nordmark 					audit_pf_policy(SPD_CLONE, cr,
2161db061fdfSdanmcd 					    ns, "all tunnels", active, 0,
2162de8c4a14SErik Nordmark 					    cpid);
2163799bd290Spwernau 				}
2164799bd290Spwernau 			}
21658810c16bSdanmcd 		} else {
2166db061fdfSdanmcd 			itp = get_tunnel_policy(tname, ns);
21678810c16bSdanmcd 			if (itp == NULL) {
21688810c16bSdanmcd 				spdsock_error(q, mp, ENOENT, 0);
2169005d3febSMarek Pospisil 				if (auditing) {
2170799bd290Spwernau 					boolean_t active;
2171799bd290Spwernau 					spd_msg_t *spmsg =
2172799bd290Spwernau 					    (spd_msg_t *)mp->b_rptr;
2173de8c4a14SErik Nordmark 					cred_t *cr;
2174de8c4a14SErik Nordmark 					pid_t cpid;
2175799bd290Spwernau 
2176de8c4a14SErik Nordmark 					cr = msg_getcred(mp, &cpid);
2177799bd290Spwernau 					active = (spmsg->spd_msg_spdid ==
2178799bd290Spwernau 					    SPD_ACTIVE);
2179de8c4a14SErik Nordmark 					audit_pf_policy(SPD_CLONE, cr,
2180051c7f8aSDan McDonald 					    ns, NULL, active, ENOENT, cpid);
2181799bd290Spwernau 				}
21828810c16bSdanmcd 				return;
21838810c16bSdanmcd 			}
2184f4b3ec61Sdh 			spdsock_clone_node(itp, &error, NULL);
2185005d3febSMarek Pospisil 			if (auditing) {
2186799bd290Spwernau 				boolean_t active;
2187799bd290Spwernau 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2188de8c4a14SErik Nordmark 				cred_t *cr;
2189de8c4a14SErik Nordmark 				pid_t cpid;
2190799bd290Spwernau 
2191de8c4a14SErik Nordmark 				cr = msg_getcred(mp, &cpid);
2192799bd290Spwernau 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2193de8c4a14SErik Nordmark 				audit_pf_policy(SPD_CLONE, cr, ns,
2194de8c4a14SErik Nordmark 				    ITP_NAME(itp), active, error, cpid);
2195799bd290Spwernau 			}
2196051c7f8aSDan McDonald 			ITP_REFRELE(itp, ns);
21978810c16bSdanmcd 		}
21988810c16bSdanmcd 	} else {
2199db061fdfSdanmcd 		error = ipsec_clone_system_policy(ns);
2200005d3febSMarek Pospisil 		if (auditing) {
2201799bd290Spwernau 			boolean_t active;
2202799bd290Spwernau 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2203de8c4a14SErik Nordmark 			cred_t *cr;
2204de8c4a14SErik Nordmark 			pid_t cpid;
2205799bd290Spwernau 
2206de8c4a14SErik Nordmark 			cr = msg_getcred(mp, &cpid);
2207799bd290Spwernau 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2208de8c4a14SErik Nordmark 			audit_pf_policy(SPD_CLONE, cr, ns, NULL,
2209de8c4a14SErik Nordmark 			    active, error, cpid);
2210799bd290Spwernau 		}
22118810c16bSdanmcd 	}
22128810c16bSdanmcd 
22137c478bd9Sstevel@tonic-gate 	if (error != 0)
22147c478bd9Sstevel@tonic-gate 		spdsock_error(q, mp, error, 0);
22157c478bd9Sstevel@tonic-gate 	else
22167c478bd9Sstevel@tonic-gate 		spd_echo(q, mp);
22177c478bd9Sstevel@tonic-gate }
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate /*
22207c478bd9Sstevel@tonic-gate  * Process a SPD_ALGLIST request. The caller expects separate alg entries
22217c478bd9Sstevel@tonic-gate  * for AH authentication, ESP authentication, and ESP encryption.
22227c478bd9Sstevel@tonic-gate  * The same distinction is then used when setting the min and max key
22237c478bd9Sstevel@tonic-gate  * sizes when defining policies.
22247c478bd9Sstevel@tonic-gate  */
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate #define	SPDSOCK_AH_AUTH		0
22277c478bd9Sstevel@tonic-gate #define	SPDSOCK_ESP_AUTH	1
22287c478bd9Sstevel@tonic-gate #define	SPDSOCK_ESP_ENCR	2
22297c478bd9Sstevel@tonic-gate #define	SPDSOCK_NTYPES		3
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate static const uint_t algattr[SPDSOCK_NTYPES] = {
22327c478bd9Sstevel@tonic-gate 	SPD_ATTR_AH_AUTH,
22337c478bd9Sstevel@tonic-gate 	SPD_ATTR_ESP_AUTH,
22347c478bd9Sstevel@tonic-gate 	SPD_ATTR_ESP_ENCR
22357c478bd9Sstevel@tonic-gate };
22367c478bd9Sstevel@tonic-gate static const uint_t minbitsattr[SPDSOCK_NTYPES] = {
22377c478bd9Sstevel@tonic-gate 	SPD_ATTR_AH_MINBITS,
22387c478bd9Sstevel@tonic-gate 	SPD_ATTR_ESPA_MINBITS,
22397c478bd9Sstevel@tonic-gate 	SPD_ATTR_ENCR_MINBITS
22407c478bd9Sstevel@tonic-gate };
22417c478bd9Sstevel@tonic-gate static const uint_t maxbitsattr[SPDSOCK_NTYPES] = {
22427c478bd9Sstevel@tonic-gate 	SPD_ATTR_AH_MAXBITS,
22437c478bd9Sstevel@tonic-gate 	SPD_ATTR_ESPA_MAXBITS,
22447c478bd9Sstevel@tonic-gate 	SPD_ATTR_ENCR_MAXBITS
22457c478bd9Sstevel@tonic-gate };
22467c478bd9Sstevel@tonic-gate static const uint_t defbitsattr[SPDSOCK_NTYPES] = {
22477c478bd9Sstevel@tonic-gate 	SPD_ATTR_AH_DEFBITS,
22487c478bd9Sstevel@tonic-gate 	SPD_ATTR_ESPA_DEFBITS,
22497c478bd9Sstevel@tonic-gate 	SPD_ATTR_ENCR_DEFBITS
22507c478bd9Sstevel@tonic-gate };
22517c478bd9Sstevel@tonic-gate static const uint_t incrbitsattr[SPDSOCK_NTYPES] = {
22527c478bd9Sstevel@tonic-gate 	SPD_ATTR_AH_INCRBITS,
22537c478bd9Sstevel@tonic-gate 	SPD_ATTR_ESPA_INCRBITS,
22547c478bd9Sstevel@tonic-gate 	SPD_ATTR_ENCR_INCRBITS
22557c478bd9Sstevel@tonic-gate };
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate #define	ATTRPERALG	6	/* fixed attributes per algs */
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate void
spdsock_alglist(queue_t * q,mblk_t * mp)22607c478bd9Sstevel@tonic-gate spdsock_alglist(queue_t *q, mblk_t *mp)
22617c478bd9Sstevel@tonic-gate {
22627c478bd9Sstevel@tonic-gate 	uint_t algtype;
22637c478bd9Sstevel@tonic-gate 	uint_t algidx;
22647c478bd9Sstevel@tonic-gate 	uint_t algcount;
22657c478bd9Sstevel@tonic-gate 	uint_t size;
22667c478bd9Sstevel@tonic-gate 	mblk_t *m;
22677c478bd9Sstevel@tonic-gate 	uint8_t *cur;
22687c478bd9Sstevel@tonic-gate 	spd_msg_t *msg;
22697c478bd9Sstevel@tonic-gate 	struct spd_ext_actions *act;
22707c478bd9Sstevel@tonic-gate 	struct spd_attribute *attr;
2271f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2272db061fdfSdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
22737c478bd9Sstevel@tonic-gate 
227469e71331SBayard Bell 	rw_enter(&ipss->ipsec_alg_lock, RW_READER);
22757c478bd9Sstevel@tonic-gate 	/*
22767c478bd9Sstevel@tonic-gate 	 * The SPD client expects to receive separate entries for
22777c478bd9Sstevel@tonic-gate 	 * AH authentication and ESP authentication supported algorithms.
22787c478bd9Sstevel@tonic-gate 	 *
22797c478bd9Sstevel@tonic-gate 	 * Don't return the "any" algorithms, if defined, as no
22807c478bd9Sstevel@tonic-gate 	 * kernel policies can be set for these algorithms.
22817c478bd9Sstevel@tonic-gate 	 */
2282f4b3ec61Sdh 	algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
2283f4b3ec61Sdh 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
22847c478bd9Sstevel@tonic-gate 
2285f4b3ec61Sdh 	if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL)
22867c478bd9Sstevel@tonic-gate 		algcount--;
2287f4b3ec61Sdh 	if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL)
22887c478bd9Sstevel@tonic-gate 		algcount--;
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 	/*
22917c478bd9Sstevel@tonic-gate 	 * For each algorithm, we encode:
22927c478bd9Sstevel@tonic-gate 	 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
22937c478bd9Sstevel@tonic-gate 	 */
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) +
22967c478bd9Sstevel@tonic-gate 	    ATTRPERALG * sizeof (struct spd_attribute) * algcount;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(size));
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	m = allocb(size, BPRI_HI);
23017c478bd9Sstevel@tonic-gate 	if (m == NULL) {
230269e71331SBayard Bell 		rw_exit(&ipss->ipsec_alg_lock);
23037c478bd9Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
23047c478bd9Sstevel@tonic-gate 		return;
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + size;
23087c478bd9Sstevel@tonic-gate 	cur = m->b_rptr;
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	msg = (spd_msg_t *)cur;
23117c478bd9Sstevel@tonic-gate 	bcopy(mp->b_rptr, cur, sizeof (*msg));
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(size);
23147c478bd9Sstevel@tonic-gate 	msg->spd_msg_errno = 0;
23157c478bd9Sstevel@tonic-gate 	msg->spd_msg_diagnostic = 0;
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	cur += sizeof (*msg);
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	act = (struct spd_ext_actions *)cur;
23207c478bd9Sstevel@tonic-gate 	cur += sizeof (*act);
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
23237c478bd9Sstevel@tonic-gate 	act->spd_actions_exttype = SPD_EXT_ACTION;
23247c478bd9Sstevel@tonic-gate 	act->spd_actions_count = algcount;
23257c478bd9Sstevel@tonic-gate 	act->spd_actions_reserved = 0;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	attr = (struct spd_attribute *)cur;
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate #define	EMIT(tag, value) {					\
23301752e73cSToomas Soome 		attr->spd_attr_tag = (tag);			\
23311752e73cSToomas Soome 		attr->spd_attr_value = (value);			\
23321752e73cSToomas Soome 		attr++;						\
23337c478bd9Sstevel@tonic-gate 	}
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	/*
23367c478bd9Sstevel@tonic-gate 	 * If you change the number of EMIT's here, change
23377c478bd9Sstevel@tonic-gate 	 * ATTRPERALG above to match
23387c478bd9Sstevel@tonic-gate 	 */
23397c478bd9Sstevel@tonic-gate #define	EMITALGATTRS(_type) {					\
23401752e73cSToomas Soome 		EMIT(algattr[_type], algid);		/* 1 */	\
23417c478bd9Sstevel@tonic-gate 		EMIT(minbitsattr[_type], minbits);	/* 2 */	\
23427c478bd9Sstevel@tonic-gate 		EMIT(maxbitsattr[_type], maxbits);	/* 3 */	\
23437c478bd9Sstevel@tonic-gate 		EMIT(defbitsattr[_type], defbits);	/* 4 */	\
23447c478bd9Sstevel@tonic-gate 		EMIT(incrbitsattr[_type], incr);	/* 5 */	\
23457c478bd9Sstevel@tonic-gate 		EMIT(SPD_ATTR_NEXT, 0);			/* 6 */	\
23467c478bd9Sstevel@tonic-gate 	}
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2349f4b3ec61Sdh 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2350f4b3ec61Sdh 		    algidx++) {
2351f4b3ec61Sdh 			int algid = ipss->ipsec_sortlist[algtype][algidx];
2352f4b3ec61Sdh 			ipsec_alginfo_t *alg =
2353f4b3ec61Sdh 			    ipss->ipsec_alglists[algtype][algid];
23547c478bd9Sstevel@tonic-gate 			uint_t minbits = alg->alg_minbits;
23557c478bd9Sstevel@tonic-gate 			uint_t maxbits = alg->alg_maxbits;
23567c478bd9Sstevel@tonic-gate 			uint_t defbits = alg->alg_default_bits;
23577c478bd9Sstevel@tonic-gate 			uint_t incr = alg->alg_increment;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 			if (algtype == IPSEC_ALG_AUTH) {
23607c478bd9Sstevel@tonic-gate 				if (algid == SADB_AALG_NONE)
23617c478bd9Sstevel@tonic-gate 					continue;
23627c478bd9Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_AH_AUTH);
23637c478bd9Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_ESP_AUTH);
23647c478bd9Sstevel@tonic-gate 			} else {
23657c478bd9Sstevel@tonic-gate 				if (algid == SADB_EALG_NONE)
23667c478bd9Sstevel@tonic-gate 					continue;
23677c478bd9Sstevel@tonic-gate 				ASSERT(algtype == IPSEC_ALG_ENCR);
23687c478bd9Sstevel@tonic-gate 				EMITALGATTRS(SPDSOCK_ESP_ENCR);
23697c478bd9Sstevel@tonic-gate 			}
23707c478bd9Sstevel@tonic-gate 		}
23717c478bd9Sstevel@tonic-gate 	}
23727c478bd9Sstevel@tonic-gate 
237369e71331SBayard Bell 	rw_exit(&ipss->ipsec_alg_lock);
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate #undef EMITALGATTRS
23767c478bd9Sstevel@tonic-gate #undef EMIT
23777c478bd9Sstevel@tonic-gate #undef ATTRPERALG
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	attr--;
23807c478bd9Sstevel@tonic-gate 	attr->spd_attr_tag = SPD_ATTR_END;
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	freemsg(mp);
23837c478bd9Sstevel@tonic-gate 	qreply(q, m);
23847c478bd9Sstevel@tonic-gate }
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate /*
23877c478bd9Sstevel@tonic-gate  * Process a SPD_DUMPALGS request.
23887c478bd9Sstevel@tonic-gate  */
23897c478bd9Sstevel@tonic-gate 
2390628b0c67SMark Fenwick #define	ATTRPERALG	9	/* fixed attributes per algs */
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate void
spdsock_dumpalgs(queue_t * q,mblk_t * mp)23937c478bd9Sstevel@tonic-gate spdsock_dumpalgs(queue_t *q, mblk_t *mp)
23947c478bd9Sstevel@tonic-gate {
23957c478bd9Sstevel@tonic-gate 	uint_t algtype;
23967c478bd9Sstevel@tonic-gate 	uint_t algidx;
23977c478bd9Sstevel@tonic-gate 	uint_t size;
23987c478bd9Sstevel@tonic-gate 	mblk_t *m;
23997c478bd9Sstevel@tonic-gate 	uint8_t *cur;
24007c478bd9Sstevel@tonic-gate 	spd_msg_t *msg;
24017c478bd9Sstevel@tonic-gate 	struct spd_ext_actions *act;
24027c478bd9Sstevel@tonic-gate 	struct spd_attribute *attr;
24037c478bd9Sstevel@tonic-gate 	ipsec_alginfo_t *alg;
24047c478bd9Sstevel@tonic-gate 	uint_t algid;
24057c478bd9Sstevel@tonic-gate 	uint_t i;
24067c478bd9Sstevel@tonic-gate 	uint_t alg_size;
2407f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2408db061fdfSdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
24097c478bd9Sstevel@tonic-gate 
241069e71331SBayard Bell 	rw_enter(&ipss->ipsec_alg_lock, RW_READER);
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	/*
24137c478bd9Sstevel@tonic-gate 	 * For each algorithm, we encode:
24147c478bd9Sstevel@tonic-gate 	 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
24157c478bd9Sstevel@tonic-gate 	 *
24167c478bd9Sstevel@tonic-gate 	 * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE*
2417628b0c67SMark Fenwick 	 * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_NPARAMS / ALG_PARAMS* /
2418628b0c67SMark Fenwick 	 * ALG_MECHNAME / ALG_FLAGS / {END, NEXT}
24197c478bd9Sstevel@tonic-gate 	 */
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	/*
24227c478bd9Sstevel@tonic-gate 	 * Compute the size of the SPD message.
24237c478bd9Sstevel@tonic-gate 	 */
24247c478bd9Sstevel@tonic-gate 	size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions);
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2427f4b3ec61Sdh 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2428f4b3ec61Sdh 		    algidx++) {
2429f4b3ec61Sdh 			algid = ipss->ipsec_sortlist[algtype][algidx];
2430f4b3ec61Sdh 			alg = ipss->ipsec_alglists[algtype][algid];
24317c478bd9Sstevel@tonic-gate 			alg_size = sizeof (struct spd_attribute) *
24327c478bd9Sstevel@tonic-gate 			    (ATTRPERALG + alg->alg_nkey_sizes +
2433628b0c67SMark Fenwick 			    alg->alg_nblock_sizes + alg->alg_nparams) +
2434628b0c67SMark Fenwick 			    CRYPTO_MAX_MECH_NAME;
24357c478bd9Sstevel@tonic-gate 			size += alg_size;
24367c478bd9Sstevel@tonic-gate 		}
24377c478bd9Sstevel@tonic-gate 	}
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 	ASSERT(ALIGNED64(size));
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	m = allocb(size, BPRI_HI);
24427c478bd9Sstevel@tonic-gate 	if (m == NULL) {
244369e71331SBayard Bell 		rw_exit(&ipss->ipsec_alg_lock);
24447c478bd9Sstevel@tonic-gate 		spdsock_error(q, mp, ENOMEM, 0);
24457c478bd9Sstevel@tonic-gate 		return;
24467c478bd9Sstevel@tonic-gate 	}
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate 	m->b_wptr = m->b_rptr + size;
24497c478bd9Sstevel@tonic-gate 	cur = m->b_rptr;
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	msg = (spd_msg_t *)cur;
24527c478bd9Sstevel@tonic-gate 	bcopy(mp->b_rptr, cur, sizeof (*msg));
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	msg->spd_msg_len = SPD_8TO64(size);
24557c478bd9Sstevel@tonic-gate 	msg->spd_msg_errno = 0;
2456a1ba8781SMark Fenwick 	msg->spd_msg_type = SPD_ALGLIST;
2457a1ba8781SMark Fenwick 
24587c478bd9Sstevel@tonic-gate 	msg->spd_msg_diagnostic = 0;
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	cur += sizeof (*msg);
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	act = (struct spd_ext_actions *)cur;
24637c478bd9Sstevel@tonic-gate 	cur += sizeof (*act);
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
24667c478bd9Sstevel@tonic-gate 	act->spd_actions_exttype = SPD_EXT_ACTION;
2467f4b3ec61Sdh 	act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
2468f4b3ec61Sdh 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
24697c478bd9Sstevel@tonic-gate 	act->spd_actions_reserved = 0;
24707c478bd9Sstevel@tonic-gate 
2471a1ba8781SMark Fenwick 	/*
2472a1ba8781SMark Fenwick 	 * If there aren't any algorithms registered, return an empty message.
2473a1ba8781SMark Fenwick 	 * spdsock_get_ext() knows how to deal with this.
2474a1ba8781SMark Fenwick 	 */
2475a1ba8781SMark Fenwick 	if (act->spd_actions_count == 0) {
2476a1ba8781SMark Fenwick 		act->spd_actions_len = 0;
247769e71331SBayard Bell 		rw_exit(&ipss->ipsec_alg_lock);
2478a1ba8781SMark Fenwick 		goto error;
2479a1ba8781SMark Fenwick 	}
2480a1ba8781SMark Fenwick 
24817c478bd9Sstevel@tonic-gate 	attr = (struct spd_attribute *)cur;
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate #define	EMIT(tag, value) {					\
24841752e73cSToomas Soome 		attr->spd_attr_tag = (tag);			\
24851752e73cSToomas Soome 		attr->spd_attr_value = (value);			\
24861752e73cSToomas Soome 		attr++;						\
24877c478bd9Sstevel@tonic-gate 	}
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2490f4b3ec61Sdh 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2491f4b3ec61Sdh 		    algidx++) {
24927c478bd9Sstevel@tonic-gate 
2493f4b3ec61Sdh 			algid = ipss->ipsec_sortlist[algtype][algidx];
2494f4b3ec61Sdh 			alg = ipss->ipsec_alglists[algtype][algid];
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 			/*
24977c478bd9Sstevel@tonic-gate 			 * If you change the number of EMIT's here, change
24987c478bd9Sstevel@tonic-gate 			 * ATTRPERALG above to match
24997c478bd9Sstevel@tonic-gate 			 */
25007c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_ID, algid);
25017c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]);
25027c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment);
25037c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes);
25047c478bd9Sstevel@tonic-gate 			for (i = 0; i < alg->alg_nkey_sizes; i++)
25057c478bd9Sstevel@tonic-gate 				EMIT(SPD_ATTR_ALG_KEYSIZE,
25067c478bd9Sstevel@tonic-gate 				    alg->alg_key_sizes[i]);
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes);
25097c478bd9Sstevel@tonic-gate 			for (i = 0; i < alg->alg_nblock_sizes; i++)
25107c478bd9Sstevel@tonic-gate 				EMIT(SPD_ATTR_ALG_BLOCKSIZE,
25117c478bd9Sstevel@tonic-gate 				    alg->alg_block_sizes[i]);
25127c478bd9Sstevel@tonic-gate 
2513628b0c67SMark Fenwick 			EMIT(SPD_ATTR_ALG_NPARAMS, alg->alg_nparams);
2514628b0c67SMark Fenwick 			for (i = 0; i < alg->alg_nparams; i++)
2515628b0c67SMark Fenwick 				EMIT(SPD_ATTR_ALG_PARAMS,
2516628b0c67SMark Fenwick 				    alg->alg_params[i]);
2517628b0c67SMark Fenwick 
2518628b0c67SMark Fenwick 			EMIT(SPD_ATTR_ALG_FLAGS, alg->alg_flags);
2519628b0c67SMark Fenwick 
25207c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME);
25217c478bd9Sstevel@tonic-gate 			bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME);
25227c478bd9Sstevel@tonic-gate 			attr = (struct spd_attribute *)((char *)attr +
25237c478bd9Sstevel@tonic-gate 			    CRYPTO_MAX_MECH_NAME);
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 			EMIT(SPD_ATTR_NEXT, 0);
25267c478bd9Sstevel@tonic-gate 		}
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 
252969e71331SBayard Bell 	rw_exit(&ipss->ipsec_alg_lock);
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate #undef EMITALGATTRS
25327c478bd9Sstevel@tonic-gate #undef EMIT
25337c478bd9Sstevel@tonic-gate #undef ATTRPERALG
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	attr--;
25367c478bd9Sstevel@tonic-gate 	attr->spd_attr_tag = SPD_ATTR_END;
25377c478bd9Sstevel@tonic-gate 
2538a1ba8781SMark Fenwick error:
25397c478bd9Sstevel@tonic-gate 	freemsg(mp);
25407c478bd9Sstevel@tonic-gate 	qreply(q, m);
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate /*
25447c478bd9Sstevel@tonic-gate  * Do the actual work of processing an SPD_UPDATEALGS request. Can
25457c478bd9Sstevel@tonic-gate  * be invoked either once IPsec is loaded on a cached request, or
25467c478bd9Sstevel@tonic-gate  * when a request is received while IPsec is loaded.
25477c478bd9Sstevel@tonic-gate  */
2548bd670b35SErik Nordmark static int
spdsock_do_updatealg(spd_ext_t * extv[],spd_stack_t * spds)2549bd670b35SErik Nordmark spdsock_do_updatealg(spd_ext_t *extv[], spd_stack_t *spds)
25507c478bd9Sstevel@tonic-gate {
25517c478bd9Sstevel@tonic-gate 	struct spd_ext_actions *actp;
25527c478bd9Sstevel@tonic-gate 	struct spd_attribute *attr, *endattr;
25537c478bd9Sstevel@tonic-gate 	uint64_t *start, *end;
25547c478bd9Sstevel@tonic-gate 	ipsec_alginfo_t *alg = NULL;
25557c478bd9Sstevel@tonic-gate 	ipsec_algtype_t alg_type = 0;
25567c478bd9Sstevel@tonic-gate 	boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE;
25577c478bd9Sstevel@tonic-gate 	uint_t i, cur_key, cur_block, algid;
2558bd670b35SErik Nordmark 	int diag = -1;
25597c478bd9Sstevel@tonic-gate 
2560f4b3ec61Sdh 	ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	/* parse the message, building the list of algorithms */
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 	actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
2565bd670b35SErik Nordmark 	if (actp == NULL)
2566bd670b35SErik Nordmark 		return (SPD_DIAGNOSTIC_NO_ACTION_EXT);
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	start = (uint64_t *)actp;
25697c478bd9Sstevel@tonic-gate 	end = (start + actp->spd_actions_len);
25707c478bd9Sstevel@tonic-gate 	endattr = (struct spd_attribute *)end;
25717c478bd9Sstevel@tonic-gate 	attr = (struct spd_attribute *)&actp[1];
25727c478bd9Sstevel@tonic-gate 
2573f4b3ec61Sdh 	bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS *
25747c478bd9Sstevel@tonic-gate 	    sizeof (ipsec_alginfo_t *));
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 	alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate #define	ALG_KEY_SIZES(a)   (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t))
25797c478bd9Sstevel@tonic-gate #define	ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t))
2580d0115d88SMark Fenwick #define	ALG_PARAM_SIZES(a) (((a)->alg_nparams + 1) * sizeof (uint16_t))
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	while (attr < endattr) {
25837c478bd9Sstevel@tonic-gate 		switch (attr->spd_attr_tag) {
25847c478bd9Sstevel@tonic-gate 		case SPD_ATTR_NOP:
25857c478bd9Sstevel@tonic-gate 		case SPD_ATTR_EMPTY:
25867c478bd9Sstevel@tonic-gate 			break;
25877c478bd9Sstevel@tonic-gate 		case SPD_ATTR_END:
25887c478bd9Sstevel@tonic-gate 			attr = endattr;
25897c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
25907c478bd9Sstevel@tonic-gate 		case SPD_ATTR_NEXT:
25917c478bd9Sstevel@tonic-gate 			if (doing_proto) {
25927c478bd9Sstevel@tonic-gate 				doing_proto = B_FALSE;
25937c478bd9Sstevel@tonic-gate 				break;
25947c478bd9Sstevel@tonic-gate 			}
25957c478bd9Sstevel@tonic-gate 			if (skip_alg) {
25967c478bd9Sstevel@tonic-gate 				ipsec_alg_free(alg);
25977c478bd9Sstevel@tonic-gate 			} else {
25987c478bd9Sstevel@tonic-gate 				ipsec_alg_free(
2599f4b3ec61Sdh 				    spds->spds_algs[alg_type][alg->alg_id]);
2600f4b3ec61Sdh 				spds->spds_algs[alg_type][alg->alg_id] =
2601f4b3ec61Sdh 				    alg;
26027c478bd9Sstevel@tonic-gate 			}
26037c478bd9Sstevel@tonic-gate 			alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
26047c478bd9Sstevel@tonic-gate 			break;
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ALG_ID:
26077c478bd9Sstevel@tonic-gate 			if (attr->spd_attr_value >= IPSEC_MAX_ALGS) {
2608f4b3ec61Sdh 				ss1dbg(spds, ("spdsock_do_updatealg: "
26097c478bd9Sstevel@tonic-gate 				    "invalid alg id %d\n",
26107c478bd9Sstevel@tonic-gate 				    attr->spd_attr_value));
2611bd670b35SErik Nordmark 				diag = SPD_DIAGNOSTIC_ALG_ID_RANGE;
26127c478bd9Sstevel@tonic-gate 				goto bail;
26137c478bd9Sstevel@tonic-gate 			}
26147c478bd9Sstevel@tonic-gate 			alg->alg_id = attr->spd_attr_value;
26157c478bd9Sstevel@tonic-gate 			break;
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ALG_PROTO:
26187c478bd9Sstevel@tonic-gate 			/* find the alg type */
26197c478bd9Sstevel@tonic-gate 			for (i = 0; i < NALGPROTOS; i++)
26207c478bd9Sstevel@tonic-gate 				if (algproto[i] == attr->spd_attr_value)
26217c478bd9Sstevel@tonic-gate 					break;
26227c478bd9Sstevel@tonic-gate 			skip_alg = (i == NALGPROTOS);
26237c478bd9Sstevel@tonic-gate 			if (!skip_alg)
26247c478bd9Sstevel@tonic-gate 				alg_type = i;
26257c478bd9Sstevel@tonic-gate 			break;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ALG_INCRBITS:
26287c478bd9Sstevel@tonic-gate 			alg->alg_increment = attr->spd_attr_value;
26297c478bd9Sstevel@tonic-gate 			break;
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ALG_NKEYSIZES:
26327c478bd9Sstevel@tonic-gate 			if (alg->alg_key_sizes != NULL) {
26337c478bd9Sstevel@tonic-gate 				kmem_free(alg->alg_key_sizes,
26347c478bd9Sstevel@tonic-gate 				    ALG_KEY_SIZES(alg));
26357c478bd9Sstevel@tonic-gate 			}
26367c478bd9Sstevel@tonic-gate 			alg->alg_nkey_sizes = attr->spd_attr_value;
26377c478bd9Sstevel@tonic-gate 			/*
26387c478bd9Sstevel@tonic-gate 			 * Allocate room for the trailing zero key size
26397c478bd9Sstevel@tonic-gate 			 * value as well.
26407c478bd9Sstevel@tonic-gate 			 */
26417c478bd9Sstevel@tonic-gate 			alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg),
26427c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
26437c478bd9Sstevel@tonic-gate 			cur_key = 0;
26447c478bd9Sstevel@tonic-gate 			break;
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ALG_KEYSIZE:
26477c478bd9Sstevel@tonic-gate 			if (alg->alg_key_sizes == NULL ||
26487c478bd9Sstevel@tonic-gate 			    cur_key >= alg->alg_nkey_sizes) {
2649f4b3ec61Sdh 				ss1dbg(spds, ("spdsock_do_updatealg: "
2650fc80c0dfSnordmark 				    "too many key sizes\n"));
2651bd670b35SErik Nordmark 				diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES;
26527c478bd9Sstevel@tonic-gate 				goto bail;
26537c478bd9Sstevel@tonic-gate 			}
26547c478bd9Sstevel@tonic-gate 			alg->alg_key_sizes[cur_key++] = attr->spd_attr_value;
26557c478bd9Sstevel@tonic-gate 			break;
26567c478bd9Sstevel@tonic-gate 
2657628b0c67SMark Fenwick 		case SPD_ATTR_ALG_FLAGS:
2658628b0c67SMark Fenwick 			/*
2659628b0c67SMark Fenwick 			 * Flags (bit mask). The alg_flags element of
2660628b0c67SMark Fenwick 			 * ipsecalg_flags_t is only 8 bits wide. The
2661628b0c67SMark Fenwick 			 * user can set the VALID bit, but we will ignore it
2662628b0c67SMark Fenwick 			 * and make the decision is the algorithm is valid.
2663628b0c67SMark Fenwick 			 */
2664628b0c67SMark Fenwick 			alg->alg_flags |= (uint8_t)attr->spd_attr_value;
2665628b0c67SMark Fenwick 			break;
2666628b0c67SMark Fenwick 
26677c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ALG_NBLOCKSIZES:
26687c478bd9Sstevel@tonic-gate 			if (alg->alg_block_sizes != NULL) {
26697c478bd9Sstevel@tonic-gate 				kmem_free(alg->alg_block_sizes,
26707c478bd9Sstevel@tonic-gate 				    ALG_BLOCK_SIZES(alg));
26717c478bd9Sstevel@tonic-gate 			}
26727c478bd9Sstevel@tonic-gate 			alg->alg_nblock_sizes = attr->spd_attr_value;
26737c478bd9Sstevel@tonic-gate 			/*
26747c478bd9Sstevel@tonic-gate 			 * Allocate room for the trailing zero block size
26757c478bd9Sstevel@tonic-gate 			 * value as well.
26767c478bd9Sstevel@tonic-gate 			 */
26777c478bd9Sstevel@tonic-gate 			alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg),
26787c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
26797c478bd9Sstevel@tonic-gate 			cur_block = 0;
26807c478bd9Sstevel@tonic-gate 			break;
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ALG_BLOCKSIZE:
26837c478bd9Sstevel@tonic-gate 			if (alg->alg_block_sizes == NULL ||
26847c478bd9Sstevel@tonic-gate 			    cur_block >= alg->alg_nblock_sizes) {
2685f4b3ec61Sdh 				ss1dbg(spds, ("spdsock_do_updatealg: "
2686fc80c0dfSnordmark 				    "too many block sizes\n"));
2687bd670b35SErik Nordmark 				diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
26887c478bd9Sstevel@tonic-gate 				goto bail;
26897c478bd9Sstevel@tonic-gate 			}
26907c478bd9Sstevel@tonic-gate 			alg->alg_block_sizes[cur_block++] =
26917c478bd9Sstevel@tonic-gate 			    attr->spd_attr_value;
26927c478bd9Sstevel@tonic-gate 			break;
26937c478bd9Sstevel@tonic-gate 
2694628b0c67SMark Fenwick 		case SPD_ATTR_ALG_NPARAMS:
2695628b0c67SMark Fenwick 			if (alg->alg_params != NULL) {
2696628b0c67SMark Fenwick 				kmem_free(alg->alg_params,
2697d0115d88SMark Fenwick 				    ALG_PARAM_SIZES(alg));
2698628b0c67SMark Fenwick 			}
2699628b0c67SMark Fenwick 			alg->alg_nparams = attr->spd_attr_value;
2700628b0c67SMark Fenwick 			/*
2701628b0c67SMark Fenwick 			 * Allocate room for the trailing zero block size
2702628b0c67SMark Fenwick 			 * value as well.
2703628b0c67SMark Fenwick 			 */
2704d0115d88SMark Fenwick 			alg->alg_params = kmem_zalloc(ALG_PARAM_SIZES(alg),
2705628b0c67SMark Fenwick 			    KM_SLEEP);
2706628b0c67SMark Fenwick 			cur_block = 0;
2707628b0c67SMark Fenwick 			break;
2708628b0c67SMark Fenwick 
2709628b0c67SMark Fenwick 		case SPD_ATTR_ALG_PARAMS:
2710628b0c67SMark Fenwick 			if (alg->alg_params == NULL ||
2711628b0c67SMark Fenwick 			    cur_block >= alg->alg_nparams) {
2712628b0c67SMark Fenwick 				ss1dbg(spds, ("spdsock_do_updatealg: "
2713628b0c67SMark Fenwick 				    "too many params\n"));
2714bd670b35SErik Nordmark 				diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
2715628b0c67SMark Fenwick 				goto bail;
2716628b0c67SMark Fenwick 			}
2717628b0c67SMark Fenwick 			/*
2718628b0c67SMark Fenwick 			 * Array contains: iv_len, icv_len, salt_len
2719628b0c67SMark Fenwick 			 * Any additional parameters are currently ignored.
2720628b0c67SMark Fenwick 			 */
2721628b0c67SMark Fenwick 			alg->alg_params[cur_block++] =
2722628b0c67SMark Fenwick 			    attr->spd_attr_value;
2723628b0c67SMark Fenwick 			break;
2724628b0c67SMark Fenwick 
27257c478bd9Sstevel@tonic-gate 		case SPD_ATTR_ALG_MECHNAME: {
27267c478bd9Sstevel@tonic-gate 			char *mech_name;
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 			if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) {
2729f4b3ec61Sdh 				ss1dbg(spds, ("spdsock_do_updatealg: "
2730fc80c0dfSnordmark 				    "mech name too long\n"));
2731bd670b35SErik Nordmark 				diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN;
27327c478bd9Sstevel@tonic-gate 				goto bail;
27337c478bd9Sstevel@tonic-gate 			}
27347c478bd9Sstevel@tonic-gate 			mech_name = (char *)(attr + 1);
27357c478bd9Sstevel@tonic-gate 			bcopy(mech_name, alg->alg_mech_name,
27367c478bd9Sstevel@tonic-gate 			    attr->spd_attr_value);
27377c478bd9Sstevel@tonic-gate 			alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0';
27387c478bd9Sstevel@tonic-gate 			attr = (struct spd_attribute *)((char *)attr +
27397c478bd9Sstevel@tonic-gate 			    attr->spd_attr_value);
27407c478bd9Sstevel@tonic-gate 			break;
27417c478bd9Sstevel@tonic-gate 		}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 		case SPD_ATTR_PROTO_ID:
27447c478bd9Sstevel@tonic-gate 			doing_proto = B_TRUE;
27457c478bd9Sstevel@tonic-gate 			for (i = 0; i < NALGPROTOS; i++) {
27467c478bd9Sstevel@tonic-gate 				if (algproto[i] == attr->spd_attr_value) {
27477c478bd9Sstevel@tonic-gate 					alg_type = i;
27487c478bd9Sstevel@tonic-gate 					break;
27497c478bd9Sstevel@tonic-gate 				}
27507c478bd9Sstevel@tonic-gate 			}
27517c478bd9Sstevel@tonic-gate 			break;
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 		case SPD_ATTR_PROTO_EXEC_MODE:
27547c478bd9Sstevel@tonic-gate 			if (!doing_proto)
27557c478bd9Sstevel@tonic-gate 				break;
27567c478bd9Sstevel@tonic-gate 			for (i = 0; i < NEXECMODES; i++) {
27577c478bd9Sstevel@tonic-gate 				if (execmodes[i] == attr->spd_attr_value) {
2758f4b3ec61Sdh 					spds->spds_algs_exec_mode[alg_type] = i;
27597c478bd9Sstevel@tonic-gate 					break;
27607c478bd9Sstevel@tonic-gate 				}
27617c478bd9Sstevel@tonic-gate 			}
27627c478bd9Sstevel@tonic-gate 			break;
27637c478bd9Sstevel@tonic-gate 		}
27647c478bd9Sstevel@tonic-gate 		attr++;
27657c478bd9Sstevel@tonic-gate 	}
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate #undef	ALG_KEY_SIZES
27687c478bd9Sstevel@tonic-gate #undef	ALG_BLOCK_SIZES
2769d0115d88SMark Fenwick #undef	ALG_PARAM_SIZES
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	/* update the algorithm tables */
2772f4b3ec61Sdh 	spdsock_merge_algs(spds);
27737c478bd9Sstevel@tonic-gate bail:
27747c478bd9Sstevel@tonic-gate 	/* cleanup */
27757c478bd9Sstevel@tonic-gate 	ipsec_alg_free(alg);
2776*fd82d5a2SToomas Soome 	for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++) {
2777*fd82d5a2SToomas Soome 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
2778*fd82d5a2SToomas Soome 			if (spds->spds_algs[alg_type][algid] != NULL) {
2779*fd82d5a2SToomas Soome 				ipsec_alg_free(
2780*fd82d5a2SToomas Soome 				    spds->spds_algs[alg_type][algid]);
2781*fd82d5a2SToomas Soome 			}
2782*fd82d5a2SToomas Soome 		}
2783*fd82d5a2SToomas Soome 	}
2784bd670b35SErik Nordmark 	return (diag);
27857c478bd9Sstevel@tonic-gate }
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate /*
27887c478bd9Sstevel@tonic-gate  * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue
27897c478bd9Sstevel@tonic-gate  * the request until IPsec loads. If IPsec is loaded, act on it
27907c478bd9Sstevel@tonic-gate  * immediately.
27917c478bd9Sstevel@tonic-gate  */
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate static void
spdsock_updatealg(queue_t * q,mblk_t * mp,spd_ext_t * extv[])27947c478bd9Sstevel@tonic-gate spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[])
27957c478bd9Sstevel@tonic-gate {
2796f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2797f4b3ec61Sdh 	spd_stack_t	*spds = ss->spdsock_spds;
2798f4b3ec61Sdh 	ipsec_stack_t	*ipss = spds->spds_netstack->netstack_ipsec;
2799005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
2800f4b3ec61Sdh 
2801f4b3ec61Sdh 	if (!ipsec_loaded(ipss)) {
28027c478bd9Sstevel@tonic-gate 		/*
28037c478bd9Sstevel@tonic-gate 		 * IPsec is not loaded, save request and return nicely,
28047c478bd9Sstevel@tonic-gate 		 * the message will be processed once IPsec loads.
28057c478bd9Sstevel@tonic-gate 		 */
28067c478bd9Sstevel@tonic-gate 		mblk_t *new_mp;
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 		/* last update message wins */
28097c478bd9Sstevel@tonic-gate 		if ((new_mp = copymsg(mp)) == NULL) {
28107c478bd9Sstevel@tonic-gate 			spdsock_error(q, mp, ENOMEM, 0);
28117c478bd9Sstevel@tonic-gate 			return;
28127c478bd9Sstevel@tonic-gate 		}
2813f4b3ec61Sdh 		mutex_enter(&spds->spds_alg_lock);
2814f4b3ec61Sdh 		bcopy(extv, spds->spds_extv_algs,
28157c478bd9Sstevel@tonic-gate 		    sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
2816f4b3ec61Sdh 		if (spds->spds_mp_algs != NULL)
2817f4b3ec61Sdh 			freemsg(spds->spds_mp_algs);
2818f4b3ec61Sdh 		spds->spds_mp_algs = mp;
2819f4b3ec61Sdh 		mutex_exit(&spds->spds_alg_lock);
2820005d3febSMarek Pospisil 		if (auditing) {
2821de8c4a14SErik Nordmark 			cred_t *cr;
2822de8c4a14SErik Nordmark 			pid_t cpid;
2823de8c4a14SErik Nordmark 
2824de8c4a14SErik Nordmark 			cr = msg_getcred(mp, &cpid);
2825de8c4a14SErik Nordmark 			audit_pf_policy(SPD_UPDATEALGS, cr,
2826799bd290Spwernau 			    spds->spds_netstack, NULL, B_TRUE, EAGAIN,
2827de8c4a14SErik Nordmark 			    cpid);
2828de8c4a14SErik Nordmark 		}
28297c478bd9Sstevel@tonic-gate 		spd_echo(q, new_mp);
28307c478bd9Sstevel@tonic-gate 	} else {
28317c478bd9Sstevel@tonic-gate 		/*
28327c478bd9Sstevel@tonic-gate 		 * IPsec is loaded, act on the message immediately.
28337c478bd9Sstevel@tonic-gate 		 */
28347c478bd9Sstevel@tonic-gate 		int diag;
28357c478bd9Sstevel@tonic-gate 
2836f4b3ec61Sdh 		mutex_enter(&spds->spds_alg_lock);
2837bd670b35SErik Nordmark 		diag = spdsock_do_updatealg(extv, spds);
2838799bd290Spwernau 		if (diag == -1) {
2839bd670b35SErik Nordmark 			/* Keep the lock held while we walk the SA tables. */
2840bd670b35SErik Nordmark 			sadb_alg_update(IPSEC_ALG_ALL, 0, 0,
2841bd670b35SErik Nordmark 			    spds->spds_netstack);
2842bd670b35SErik Nordmark 			mutex_exit(&spds->spds_alg_lock);
28437c478bd9Sstevel@tonic-gate 			spd_echo(q, mp);
2844005d3febSMarek Pospisil 			if (auditing) {
2845de8c4a14SErik Nordmark 				cred_t *cr;
2846de8c4a14SErik Nordmark 				pid_t cpid;
2847de8c4a14SErik Nordmark 
2848de8c4a14SErik Nordmark 				cr = msg_getcred(mp, &cpid);
2849de8c4a14SErik Nordmark 				audit_pf_policy(SPD_UPDATEALGS, cr,
2850de8c4a14SErik Nordmark 				    spds->spds_netstack, NULL, B_TRUE, 0,
2851de8c4a14SErik Nordmark 				    cpid);
2852de8c4a14SErik Nordmark 			}
2853799bd290Spwernau 		} else {
2854bd670b35SErik Nordmark 			mutex_exit(&spds->spds_alg_lock);
28557c478bd9Sstevel@tonic-gate 			spdsock_diag(q, mp, diag);
2856005d3febSMarek Pospisil 			if (auditing) {
2857de8c4a14SErik Nordmark 				cred_t *cr;
2858de8c4a14SErik Nordmark 				pid_t cpid;
2859de8c4a14SErik Nordmark 
2860de8c4a14SErik Nordmark 				cr = msg_getcred(mp, &cpid);
2861de8c4a14SErik Nordmark 				audit_pf_policy(SPD_UPDATEALGS, cr,
2862de8c4a14SErik Nordmark 				    spds->spds_netstack, NULL, B_TRUE, diag,
2863de8c4a14SErik Nordmark 				    cpid);
2864de8c4a14SErik Nordmark 			}
2865799bd290Spwernau 		}
28667c478bd9Sstevel@tonic-gate 	}
28677c478bd9Sstevel@tonic-gate }
28687c478bd9Sstevel@tonic-gate 
286901ac885fSDan McDonald /*
287001ac885fSDan McDonald  * Find a tunnel instance (using the name to link ID mapping), and
287101ac885fSDan McDonald  * update it after an IPsec change.  We need to do this always in case
287201ac885fSDan McDonald  * we add policy AFTER plumbing a tunnel.  We also need to do this
287301ac885fSDan McDonald  * because, as a side-effect, the tunnel's MTU is updated to reflect
287401ac885fSDan McDonald  * any IPsec overhead in the itp's policy.
287501ac885fSDan McDonald  */
287601ac885fSDan McDonald static void
update_iptun_policy(ipsec_tun_pol_t * itp)287701ac885fSDan McDonald update_iptun_policy(ipsec_tun_pol_t *itp)
287801ac885fSDan McDonald {
287901ac885fSDan McDonald 	datalink_id_t linkid;
288001ac885fSDan McDonald 
288101ac885fSDan McDonald 	if (dls_mgmt_get_linkid(itp->itp_name, &linkid) == 0)
288201ac885fSDan McDonald 		iptun_set_policy(linkid, itp);
288301ac885fSDan McDonald }
288401ac885fSDan McDonald 
28858810c16bSdanmcd /*
28868810c16bSdanmcd  * Sort through the mess of polhead options to retrieve an appropriate one.
28878810c16bSdanmcd  * Returns NULL if we send an spdsock error.  Returns a valid pointer if we
28888810c16bSdanmcd  * found a valid polhead.  Returns ALL_ACTIVE_POLHEADS (aka. -1) or
28898810c16bSdanmcd  * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to
28908810c16bSdanmcd  * act on ALL policy heads.
28918810c16bSdanmcd  */
28928810c16bSdanmcd 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)28938810c16bSdanmcd get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid,
28948810c16bSdanmcd     int msgtype, ipsec_tun_pol_t **itpp)
28958810c16bSdanmcd {
28968810c16bSdanmcd 	ipsec_tun_pol_t *itp;
28978810c16bSdanmcd 	ipsec_policy_head_t *iph;
28988810c16bSdanmcd 	int errno;
28998810c16bSdanmcd 	char *tname;
29008810c16bSdanmcd 	boolean_t active;
29018810c16bSdanmcd 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2902db061fdfSdanmcd 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
29038810c16bSdanmcd 	uint64_t gen;	/* Placeholder */
29048810c16bSdanmcd 
29058810c16bSdanmcd 	active = (spdid == SPD_ACTIVE);
29068810c16bSdanmcd 	*itpp = NULL;
29078810c16bSdanmcd 	if (!active && spdid != SPD_STANDBY) {
29088810c16bSdanmcd 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID);
29098810c16bSdanmcd 		return (NULL);
29108810c16bSdanmcd 	}
29118810c16bSdanmcd 
29128810c16bSdanmcd 	if (tunname != NULL) {
29138810c16bSdanmcd 		/* Acting on a tunnel's SPD. */
29148810c16bSdanmcd 		tname = (char *)tunname->spd_if_name;
29158810c16bSdanmcd 		if (*tname == '\0') {
29168810c16bSdanmcd 			/* Handle all-polhead cases here. */
29178810c16bSdanmcd 			if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) {
29188810c16bSdanmcd 				spdsock_diag(q, mp,
29198810c16bSdanmcd 				    SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
29208810c16bSdanmcd 				return (NULL);
29218810c16bSdanmcd 			}
29228810c16bSdanmcd 			return (active ? ALL_ACTIVE_POLHEADS :
29238810c16bSdanmcd 			    ALL_INACTIVE_POLHEADS);
29248810c16bSdanmcd 		}
29258810c16bSdanmcd 
2926db061fdfSdanmcd 		itp = get_tunnel_policy(tname, ns);
29278810c16bSdanmcd 		if (itp == NULL) {
29288810c16bSdanmcd 			if (msgtype != SPD_ADDRULE) {
29298810c16bSdanmcd 				/* "Tunnel not found" */
29308810c16bSdanmcd 				spdsock_error(q, mp, ENOENT, 0);
29318810c16bSdanmcd 				return (NULL);
29328810c16bSdanmcd 			}
29338810c16bSdanmcd 
29348810c16bSdanmcd 			errno = 0;
2935db061fdfSdanmcd 			itp = create_tunnel_policy(tname, &errno, &gen, ns);
29368810c16bSdanmcd 			if (itp == NULL) {
29378810c16bSdanmcd 				/*
29388810c16bSdanmcd 				 * Something very bad happened, most likely
29398810c16bSdanmcd 				 * ENOMEM.  Return an indicator.
29408810c16bSdanmcd 				 */
29418810c16bSdanmcd 				spdsock_error(q, mp, errno, 0);
29428810c16bSdanmcd 				return (NULL);
29438810c16bSdanmcd 			}
29448810c16bSdanmcd 		}
294501ac885fSDan McDonald 
294601ac885fSDan McDonald 		/* Match up the itp to an iptun instance. */
294701ac885fSDan McDonald 		update_iptun_policy(itp);
29482b24ab6bSSebastien Roy 
29498810c16bSdanmcd 		*itpp = itp;
29508810c16bSdanmcd 		/* For spdsock dump state, set the polhead's name. */
29518810c16bSdanmcd 		if (msgtype == SPD_DUMP) {
2952db061fdfSdanmcd 			ITP_REFHOLD(itp);
2953db061fdfSdanmcd 			ss->spdsock_itp = itp;
29548810c16bSdanmcd 			ss->spdsock_dump_tunnel = itp->itp_flags &
29558810c16bSdanmcd 			    (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL);
29568810c16bSdanmcd 		}
29578810c16bSdanmcd 	} else {
29588810c16bSdanmcd 		itp = NULL;
29598810c16bSdanmcd 		/* For spdsock dump state, indicate it's global policy. */
29608810c16bSdanmcd 		if (msgtype == SPD_DUMP)
2961db061fdfSdanmcd 			ss->spdsock_itp = NULL;
29628810c16bSdanmcd 	}
29638810c16bSdanmcd 
29648810c16bSdanmcd 	if (active)
2965f4b3ec61Sdh 		iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy;
29668810c16bSdanmcd 	else
2967f4b3ec61Sdh 		iph = (itp == NULL) ? ipsec_inactive_policy(ns) :
29688810c16bSdanmcd 		    itp->itp_inactive;
29698810c16bSdanmcd 
29708810c16bSdanmcd 	ASSERT(iph != NULL);
29718810c16bSdanmcd 	if (itp != NULL) {
29728810c16bSdanmcd 		IPPH_REFHOLD(iph);
29738810c16bSdanmcd 	}
29748810c16bSdanmcd 
29758810c16bSdanmcd 	return (iph);
29768810c16bSdanmcd }
29778810c16bSdanmcd 
29787c478bd9Sstevel@tonic-gate static void
spdsock_parse(queue_t * q,mblk_t * mp)29797c478bd9Sstevel@tonic-gate spdsock_parse(queue_t *q, mblk_t *mp)
29807c478bd9Sstevel@tonic-gate {
29817c478bd9Sstevel@tonic-gate 	spd_msg_t *spmsg;
29827c478bd9Sstevel@tonic-gate 	spd_ext_t *extv[SPD_EXT_MAX + 1];
29837c478bd9Sstevel@tonic-gate 	uint_t msgsize;
29847c478bd9Sstevel@tonic-gate 	ipsec_policy_head_t *iph;
29858810c16bSdanmcd 	ipsec_tun_pol_t *itp;
29868810c16bSdanmcd 	spd_if_t *tunname;
2987f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2988f4b3ec61Sdh 	spd_stack_t *spds = ss->spdsock_spds;
2989f4b3ec61Sdh 	netstack_t *ns = spds->spds_netstack;
2990f4b3ec61Sdh 	ipsec_stack_t *ipss = ns->netstack_ipsec;
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 	/* Make sure nothing's below me. */
29937c478bd9Sstevel@tonic-gate 	ASSERT(WR(q)->q_next == NULL);
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 	spmsg = (spd_msg_t *)mp->b_rptr;
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	msgsize = SPD_64TO8(spmsg->spd_msg_len);
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 	if (msgdsize(mp) != msgsize) {
30007c478bd9Sstevel@tonic-gate 		/*
30017c478bd9Sstevel@tonic-gate 		 * Message len incorrect w.r.t. actual size.  Send an error
30027c478bd9Sstevel@tonic-gate 		 * (EMSGSIZE).	It may be necessary to massage things a
30037c478bd9Sstevel@tonic-gate 		 * bit.	 For example, if the spd_msg_type is hosed,
30047c478bd9Sstevel@tonic-gate 		 * I need to set it to SPD_RESERVED to get delivery to
30057c478bd9Sstevel@tonic-gate 		 * do the right thing.	Then again, maybe just letting
30067c478bd9Sstevel@tonic-gate 		 * the error delivery do the right thing.
30077c478bd9Sstevel@tonic-gate 		 */
3008f4b3ec61Sdh 		ss2dbg(spds,
3009f4b3ec61Sdh 		    ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
30107c478bd9Sstevel@tonic-gate 		    msgdsize(mp), msgsize));
30117c478bd9Sstevel@tonic-gate 		spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE);
30127c478bd9Sstevel@tonic-gate 		return;
30137c478bd9Sstevel@tonic-gate 	}
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 	if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
30167c478bd9Sstevel@tonic-gate 		/* Get all message into one mblk. */
30177c478bd9Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
30187c478bd9Sstevel@tonic-gate 			/*
30197c478bd9Sstevel@tonic-gate 			 * Something screwy happened.
30207c478bd9Sstevel@tonic-gate 			 */
3021f4b3ec61Sdh 			ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n"));
30227c478bd9Sstevel@tonic-gate 			return;
30237c478bd9Sstevel@tonic-gate 		} else {
30247c478bd9Sstevel@tonic-gate 			spmsg = (spd_msg_t *)mp->b_rptr;
30257c478bd9Sstevel@tonic-gate 		}
30267c478bd9Sstevel@tonic-gate 	}
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 	switch (spdsock_get_ext(extv, spmsg, msgsize)) {
30297c478bd9Sstevel@tonic-gate 	case KGE_DUP:
30307c478bd9Sstevel@tonic-gate 		/* Handle duplicate extension. */
3031f4b3ec61Sdh 		ss1dbg(spds, ("Got duplicate extension of type %d.\n",
30327c478bd9Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
30337c478bd9Sstevel@tonic-gate 		spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]);
30347c478bd9Sstevel@tonic-gate 		return;
30357c478bd9Sstevel@tonic-gate 	case KGE_UNK:
30367c478bd9Sstevel@tonic-gate 		/* Handle unknown extension. */
3037f4b3ec61Sdh 		ss1dbg(spds, ("Got unknown extension of type %d.\n",
30387c478bd9Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
30397c478bd9Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT);
30407c478bd9Sstevel@tonic-gate 		return;
30417c478bd9Sstevel@tonic-gate 	case KGE_LEN:
30427c478bd9Sstevel@tonic-gate 		/* Length error. */
3043f4b3ec61Sdh 		ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n",
30447c478bd9Sstevel@tonic-gate 		    extv[0]->spd_ext_len, extv[0]->spd_ext_type));
30457c478bd9Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN);
30467c478bd9Sstevel@tonic-gate 		return;
30477c478bd9Sstevel@tonic-gate 	case KGE_CHK:
30487c478bd9Sstevel@tonic-gate 		/* Reality check failed. */
3049f4b3ec61Sdh 		ss1dbg(spds, ("Reality check failed on extension type %d.\n",
30507c478bd9Sstevel@tonic-gate 		    extv[0]->spd_ext_type));
30517c478bd9Sstevel@tonic-gate 		spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]);
30527c478bd9Sstevel@tonic-gate 		return;
30537c478bd9Sstevel@tonic-gate 	default:
30547c478bd9Sstevel@tonic-gate 		/* Default case is no errors. */
30557c478bd9Sstevel@tonic-gate 		break;
30567c478bd9Sstevel@tonic-gate 	}
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	/*
30597c478bd9Sstevel@tonic-gate 	 * Special-case SPD_UPDATEALGS so as not to load IPsec.
30607c478bd9Sstevel@tonic-gate 	 */
3061f4b3ec61Sdh 	if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) {
30627c478bd9Sstevel@tonic-gate 		spdsock_t *ss = (spdsock_t *)q->q_ptr;
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 		ASSERT(ss != NULL);
3065f4b3ec61Sdh 		ipsec_loader_loadnow(ipss);
30667c478bd9Sstevel@tonic-gate 		ss->spdsock_timeout_arg = mp;
30677c478bd9Sstevel@tonic-gate 		ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck,
30687c478bd9Sstevel@tonic-gate 		    q, LOADCHECK_INTERVAL);
30697c478bd9Sstevel@tonic-gate 		return;
30707c478bd9Sstevel@tonic-gate 	}
30717c478bd9Sstevel@tonic-gate 
30728810c16bSdanmcd 	/* First check for messages that need no polheads at all. */
30737c478bd9Sstevel@tonic-gate 	switch (spmsg->spd_msg_type) {
30747c478bd9Sstevel@tonic-gate 	case SPD_UPDATEALGS:
30757c478bd9Sstevel@tonic-gate 		spdsock_updatealg(q, mp, extv);
30767c478bd9Sstevel@tonic-gate 		return;
30778810c16bSdanmcd 	case SPD_ALGLIST:
30788810c16bSdanmcd 		spdsock_alglist(q, mp);
30797c478bd9Sstevel@tonic-gate 		return;
30808810c16bSdanmcd 	case SPD_DUMPALGS:
30818810c16bSdanmcd 		spdsock_dumpalgs(q, mp);
30827c478bd9Sstevel@tonic-gate 		return;
30838810c16bSdanmcd 	}
30847c478bd9Sstevel@tonic-gate 
30858810c16bSdanmcd 	/*
30868810c16bSdanmcd 	 * Then check for ones that need both primary/secondary polheads,
30878810c16bSdanmcd 	 * finding the appropriate tunnel policy if need be.
30888810c16bSdanmcd 	 */
30898810c16bSdanmcd 	tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME];
30908810c16bSdanmcd 	switch (spmsg->spd_msg_type) {
30917c478bd9Sstevel@tonic-gate 	case SPD_FLIP:
30928810c16bSdanmcd 		spdsock_flip(q, mp, tunname);
30937c478bd9Sstevel@tonic-gate 		return;
30948810c16bSdanmcd 	case SPD_CLONE:
30958810c16bSdanmcd 		spdsock_clone(q, mp, tunname);
30967c478bd9Sstevel@tonic-gate 		return;
30978810c16bSdanmcd 	}
30987c478bd9Sstevel@tonic-gate 
30998810c16bSdanmcd 	/*
31008810c16bSdanmcd 	 * Finally, find ones that operate on exactly one polhead, or
31018810c16bSdanmcd 	 * "all polheads" of a given type (active/inactive).
31028810c16bSdanmcd 	 */
31038810c16bSdanmcd 	iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid,
31048810c16bSdanmcd 	    spmsg->spd_msg_type, &itp);
31058810c16bSdanmcd 	if (iph == NULL)
31067c478bd9Sstevel@tonic-gate 		return;
31077c478bd9Sstevel@tonic-gate 
31088810c16bSdanmcd 	/* All-polheads-ready operations. */
31098810c16bSdanmcd 	switch (spmsg->spd_msg_type) {
31108810c16bSdanmcd 	case SPD_FLUSH:
31118810c16bSdanmcd 		if (itp != NULL) {
31128810c16bSdanmcd 			mutex_enter(&itp->itp_lock);
31138810c16bSdanmcd 			if (spmsg->spd_msg_spdid == SPD_ACTIVE)
31148810c16bSdanmcd 				itp->itp_flags &= ~ITPF_PFLAGS;
31158810c16bSdanmcd 			else
31168810c16bSdanmcd 				itp->itp_flags &= ~ITPF_IFLAGS;
31178810c16bSdanmcd 			mutex_exit(&itp->itp_lock);
31188810c16bSdanmcd 		}
311901ac885fSDan McDonald 
3120799bd290Spwernau 		spdsock_flush(q, iph, itp, mp);
312101ac885fSDan McDonald 
312201ac885fSDan McDonald 		if (itp != NULL) {
312301ac885fSDan McDonald 			/* SPD_FLUSH is worth a tunnel MTU check. */
312401ac885fSDan McDonald 			update_iptun_policy(itp);
312501ac885fSDan McDonald 			ITP_REFRELE(itp, ns);
312601ac885fSDan McDonald 		}
31277c478bd9Sstevel@tonic-gate 		return;
31288810c16bSdanmcd 	case SPD_DUMP:
31298810c16bSdanmcd 		if (itp != NULL)
3130f4b3ec61Sdh 			ITP_REFRELE(itp, ns);
31318810c16bSdanmcd 		spdsock_dump(q, iph, mp);
31327c478bd9Sstevel@tonic-gate 		return;
31338810c16bSdanmcd 	}
31347c478bd9Sstevel@tonic-gate 
31358810c16bSdanmcd 	if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
31368810c16bSdanmcd 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
31377c478bd9Sstevel@tonic-gate 		return;
31388810c16bSdanmcd 	}
31397c478bd9Sstevel@tonic-gate 
31408810c16bSdanmcd 	/* Single-polhead-only operations. */
31418810c16bSdanmcd 	switch (spmsg->spd_msg_type) {
31428810c16bSdanmcd 	case SPD_ADDRULE:
31438810c16bSdanmcd 		spdsock_addrule(q, iph, mp, extv, itp);
31448810c16bSdanmcd 		break;
31458810c16bSdanmcd 	case SPD_DELETERULE:
31468810c16bSdanmcd 		spdsock_deleterule(q, iph, mp, extv, itp);
31478810c16bSdanmcd 		break;
31488810c16bSdanmcd 	case SPD_LOOKUP:
31498810c16bSdanmcd 		spdsock_lookup(q, iph, mp, extv, itp);
31508810c16bSdanmcd 		break;
31517c478bd9Sstevel@tonic-gate 	default:
31527c478bd9Sstevel@tonic-gate 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE);
31538810c16bSdanmcd 		break;
31547c478bd9Sstevel@tonic-gate 	}
31558810c16bSdanmcd 
3156db061fdfSdanmcd 	IPPH_REFRELE(iph, ns);
315701ac885fSDan McDonald 	if (itp != NULL) {
315801ac885fSDan McDonald 		/* SPD_{ADD,DELETE}RULE are worth a tunnel MTU check. */
315901ac885fSDan McDonald 		if (spmsg->spd_msg_type == SPD_ADDRULE ||
316001ac885fSDan McDonald 		    spmsg->spd_msg_type == SPD_DELETERULE)
316101ac885fSDan McDonald 			update_iptun_policy(itp);
3162f4b3ec61Sdh 		ITP_REFRELE(itp, ns);
316301ac885fSDan McDonald 	}
31647c478bd9Sstevel@tonic-gate }
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate /*
31677c478bd9Sstevel@tonic-gate  * If an algorithm mapping was received before IPsec was loaded, process it.
31687c478bd9Sstevel@tonic-gate  * Called from the IPsec loader.
31697c478bd9Sstevel@tonic-gate  */
31707c478bd9Sstevel@tonic-gate void
spdsock_update_pending_algs(netstack_t * ns)3171f4b3ec61Sdh spdsock_update_pending_algs(netstack_t *ns)
31727c478bd9Sstevel@tonic-gate {
3173f4b3ec61Sdh 	spd_stack_t *spds = ns->netstack_spdsock;
3174f4b3ec61Sdh 
3175f4b3ec61Sdh 	mutex_enter(&spds->spds_alg_lock);
3176134a1f4eSCasper H.S. Dik 	if (spds->spds_mp_algs != NULL) {
3177bd670b35SErik Nordmark 		(void) spdsock_do_updatealg(spds->spds_extv_algs, spds);
3178134a1f4eSCasper H.S. Dik 		freemsg(spds->spds_mp_algs);
3179134a1f4eSCasper H.S. Dik 		spds->spds_mp_algs = NULL;
31807c478bd9Sstevel@tonic-gate 	}
3181f4b3ec61Sdh 	mutex_exit(&spds->spds_alg_lock);
31827c478bd9Sstevel@tonic-gate }
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate static void
spdsock_loadcheck(void * arg)31857c478bd9Sstevel@tonic-gate spdsock_loadcheck(void *arg)
31867c478bd9Sstevel@tonic-gate {
31877c478bd9Sstevel@tonic-gate 	queue_t *q = (queue_t *)arg;
31887c478bd9Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
31897c478bd9Sstevel@tonic-gate 	mblk_t *mp;
3190db061fdfSdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	ASSERT(ss != NULL);
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	ss->spdsock_timeout = 0;
31957c478bd9Sstevel@tonic-gate 	mp = ss->spdsock_timeout_arg;
31967c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
31977c478bd9Sstevel@tonic-gate 	ss->spdsock_timeout_arg = NULL;
3198f4b3ec61Sdh 	if (ipsec_failed(ipss))
31997c478bd9Sstevel@tonic-gate 		spdsock_error(q, mp, EPROTONOSUPPORT, 0);
32007c478bd9Sstevel@tonic-gate 	else
32017c478bd9Sstevel@tonic-gate 		spdsock_parse(q, mp);
32027c478bd9Sstevel@tonic-gate }
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate /*
32057c478bd9Sstevel@tonic-gate  * Copy relevant state bits.
32067c478bd9Sstevel@tonic-gate  */
32077c478bd9Sstevel@tonic-gate static void
spdsock_copy_info(struct T_info_ack * tap,spdsock_t * ss)32087c478bd9Sstevel@tonic-gate spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss)
32097c478bd9Sstevel@tonic-gate {
32107c478bd9Sstevel@tonic-gate 	*tap = spdsock_g_t_info_ack;
32117c478bd9Sstevel@tonic-gate 	tap->CURRENT_state = ss->spdsock_state;
32127c478bd9Sstevel@tonic-gate 	tap->OPT_size = spdsock_max_optsize;
32137c478bd9Sstevel@tonic-gate }
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate /*
32167c478bd9Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
32177c478bd9Sstevel@tonic-gate  * spdsock_wput.  Much of the T_CAPABILITY_ACK information is copied from
32187c478bd9Sstevel@tonic-gate  * spdsock_g_t_info_ack.  The current state of the stream is copied from
32197c478bd9Sstevel@tonic-gate  * spdsock_state.
32207c478bd9Sstevel@tonic-gate  */
32217c478bd9Sstevel@tonic-gate static void
spdsock_capability_req(queue_t * q,mblk_t * mp)32227c478bd9Sstevel@tonic-gate spdsock_capability_req(queue_t *q, mblk_t *mp)
32237c478bd9Sstevel@tonic-gate {
32247c478bd9Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
32257c478bd9Sstevel@tonic-gate 	t_uscalar_t cap_bits1;
32267c478bd9Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
3231fc80c0dfSnordmark 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
32327c478bd9Sstevel@tonic-gate 	if (mp == NULL)
32337c478bd9Sstevel@tonic-gate 		return;
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
32367c478bd9Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
32397c478bd9Sstevel@tonic-gate 		spdsock_copy_info(&tcap->INFO_ack, ss);
32407c478bd9Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
32417c478bd9Sstevel@tonic-gate 	}
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	qreply(q, mp);
32447c478bd9Sstevel@tonic-gate }
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate /*
32477c478bd9Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages. It is called by
32487c478bd9Sstevel@tonic-gate  * spdsock_wput_other.
32497c478bd9Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack.
32507c478bd9Sstevel@tonic-gate  * The current state of the stream is copied from spdsock_state.
32517c478bd9Sstevel@tonic-gate  */
32527c478bd9Sstevel@tonic-gate static void
spdsock_info_req(queue_t * q,mblk_t * mp)325369e71331SBayard Bell spdsock_info_req(
325469e71331SBayard Bell     queue_t	*q,
325569e71331SBayard Bell     mblk_t	*mp)
32567c478bd9Sstevel@tonic-gate {
32577c478bd9Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
32587c478bd9Sstevel@tonic-gate 	    T_INFO_ACK);
32597c478bd9Sstevel@tonic-gate 	if (mp == NULL)
32607c478bd9Sstevel@tonic-gate 		return;
32617c478bd9Sstevel@tonic-gate 	spdsock_copy_info((struct T_info_ack *)mp->b_rptr,
32627c478bd9Sstevel@tonic-gate 	    (spdsock_t *)q->q_ptr);
32637c478bd9Sstevel@tonic-gate 	qreply(q, mp);
32647c478bd9Sstevel@tonic-gate }
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate /*
32677c478bd9Sstevel@tonic-gate  * spdsock_err_ack. This routine creates a
32687c478bd9Sstevel@tonic-gate  * T_ERROR_ACK message and passes it
32697c478bd9Sstevel@tonic-gate  * upstream.
32707c478bd9Sstevel@tonic-gate  */
32717c478bd9Sstevel@tonic-gate static void
spdsock_err_ack(queue_t * q,mblk_t * mp,int t_error,int sys_error)327269e71331SBayard Bell spdsock_err_ack(
327369e71331SBayard Bell     queue_t	*q,
327469e71331SBayard Bell     mblk_t	*mp,
327569e71331SBayard Bell     int	t_error,
327669e71331SBayard Bell     int	sys_error)
32777c478bd9Sstevel@tonic-gate {
32787c478bd9Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
32797c478bd9Sstevel@tonic-gate 		qreply(q, mp);
32807c478bd9Sstevel@tonic-gate }
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate /*
32837c478bd9Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
32847c478bd9Sstevel@tonic-gate  * It returns the size of the option retrieved.
32857c478bd9Sstevel@tonic-gate  */
32867c478bd9Sstevel@tonic-gate /* ARGSUSED */
32877c478bd9Sstevel@tonic-gate int
spdsock_opt_get(queue_t * q,int level,int name,uchar_t * ptr)32887c478bd9Sstevel@tonic-gate spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
32897c478bd9Sstevel@tonic-gate {
32907c478bd9Sstevel@tonic-gate 	int *i1 = (int *)ptr;
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 	switch (level) {
32937c478bd9Sstevel@tonic-gate 	case SOL_SOCKET:
32947c478bd9Sstevel@tonic-gate 		switch (name) {
32957c478bd9Sstevel@tonic-gate 		case SO_TYPE:
32967c478bd9Sstevel@tonic-gate 			*i1 = SOCK_RAW;
32977c478bd9Sstevel@tonic-gate 			break;
32987c478bd9Sstevel@tonic-gate 		/*
32997c478bd9Sstevel@tonic-gate 		 * The following two items can be manipulated,
33007c478bd9Sstevel@tonic-gate 		 * but changing them should do nothing.
33017c478bd9Sstevel@tonic-gate 		 */
33027c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
33037c478bd9Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
33047c478bd9Sstevel@tonic-gate 			break;
33057c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
33067c478bd9Sstevel@tonic-gate 			*i1 = (int)(RD(q)->q_hiwat);
33077c478bd9Sstevel@tonic-gate 			break;
33087c478bd9Sstevel@tonic-gate 		}
33097c478bd9Sstevel@tonic-gate 		break;
33107c478bd9Sstevel@tonic-gate 	default:
33117c478bd9Sstevel@tonic-gate 		return (0);
33127c478bd9Sstevel@tonic-gate 	}
33137c478bd9Sstevel@tonic-gate 	return (sizeof (int));
33147c478bd9Sstevel@tonic-gate }
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate /*
33177c478bd9Sstevel@tonic-gate  * This routine sets socket options.
33187c478bd9Sstevel@tonic-gate  */
33197c478bd9Sstevel@tonic-gate /* ARGSUSED */
33207c478bd9Sstevel@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)33217c478bd9Sstevel@tonic-gate spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name,
33227c478bd9Sstevel@tonic-gate     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
3323bd670b35SErik Nordmark     void *thisdg_attrs, cred_t *cr)
33247c478bd9Sstevel@tonic-gate {
33257c478bd9Sstevel@tonic-gate 	int *i1 = (int *)invalp;
3326f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
3327f4b3ec61Sdh 	spd_stack_t	*spds = ss->spdsock_spds;
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate 	switch (level) {
33307c478bd9Sstevel@tonic-gate 	case SOL_SOCKET:
33317c478bd9Sstevel@tonic-gate 		switch (name) {
33327c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
3333f4b3ec61Sdh 			if (*i1 > spds->spds_max_buf)
33347c478bd9Sstevel@tonic-gate 				return (ENOBUFS);
33357c478bd9Sstevel@tonic-gate 			q->q_hiwat = *i1;
33367c478bd9Sstevel@tonic-gate 			break;
33377c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
3338f4b3ec61Sdh 			if (*i1 > spds->spds_max_buf)
33397c478bd9Sstevel@tonic-gate 				return (ENOBUFS);
33407c478bd9Sstevel@tonic-gate 			RD(q)->q_hiwat = *i1;
33410f1702c5SYu Xiangning 			(void) proto_set_rx_hiwat(RD(q), NULL, *i1);
33427c478bd9Sstevel@tonic-gate 			break;
33437c478bd9Sstevel@tonic-gate 		}
33447c478bd9Sstevel@tonic-gate 		break;
33457c478bd9Sstevel@tonic-gate 	}
33467c478bd9Sstevel@tonic-gate 	return (0);
33477c478bd9Sstevel@tonic-gate }
33487c478bd9Sstevel@tonic-gate 
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate /*
33517c478bd9Sstevel@tonic-gate  * Handle STREAMS messages.
33527c478bd9Sstevel@tonic-gate  */
33537c478bd9Sstevel@tonic-gate static void
spdsock_wput_other(queue_t * q,mblk_t * mp)33547c478bd9Sstevel@tonic-gate spdsock_wput_other(queue_t *q, mblk_t *mp)
33557c478bd9Sstevel@tonic-gate {
33567c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
33577c478bd9Sstevel@tonic-gate 	int error;
3358f4b3ec61Sdh 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
3359f4b3ec61Sdh 	spd_stack_t	*spds = ss->spdsock_spds;
3360f4b3ec61Sdh 	cred_t		*cr;
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
33637c478bd9Sstevel@tonic-gate 	case M_PROTO:
33647c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
33657c478bd9Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
3366f4b3ec61Sdh 			ss3dbg(spds, (
33677c478bd9Sstevel@tonic-gate 			    "spdsock_wput_other: Not big enough M_PROTO\n"));
33687c478bd9Sstevel@tonic-gate 			freemsg(mp);
33697c478bd9Sstevel@tonic-gate 			return;
33707c478bd9Sstevel@tonic-gate 		}
33717c478bd9Sstevel@tonic-gate 		switch (((union T_primitives *)mp->b_rptr)->type) {
33727c478bd9Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
33737c478bd9Sstevel@tonic-gate 			spdsock_capability_req(q, mp);
3374f4b3ec61Sdh 			break;
33757c478bd9Sstevel@tonic-gate 		case T_INFO_REQ:
33767c478bd9Sstevel@tonic-gate 			spdsock_info_req(q, mp);
3377f4b3ec61Sdh 			break;
33787c478bd9Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
33797c478bd9Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
3380de8c4a14SErik Nordmark 			/*
3381de8c4a14SErik Nordmark 			 * All Solaris components should pass a db_credp
3382de8c4a14SErik Nordmark 			 * for this TPI message, hence we ASSERT.
3383de8c4a14SErik Nordmark 			 * But in case there is some other M_PROTO that looks
3384de8c4a14SErik Nordmark 			 * like a TPI message sent by some other kernel
3385de8c4a14SErik Nordmark 			 * component, we check and return an error.
3386de8c4a14SErik Nordmark 			 */
3387de8c4a14SErik Nordmark 			cr = msg_getcred(mp, NULL);
3388de8c4a14SErik Nordmark 			ASSERT(cr != NULL);
3389de8c4a14SErik Nordmark 			if (cr == NULL) {
3390de8c4a14SErik Nordmark 				spdsock_err_ack(q, mp, TSYSERR, EINVAL);
3391de8c4a14SErik Nordmark 				return;
3392de8c4a14SErik Nordmark 			}
3393de8c4a14SErik Nordmark 			if (((union T_primitives *)mp->b_rptr)->type ==
3394de8c4a14SErik Nordmark 			    T_SVR4_OPTMGMT_REQ) {
3395bd670b35SErik Nordmark 				svr4_optcom_req(q, mp, cr, &spdsock_opt_obj);
3396de8c4a14SErik Nordmark 			} else {
3397bd670b35SErik Nordmark 				tpi_optcom_req(q, mp, cr, &spdsock_opt_obj);
3398de8c4a14SErik Nordmark 			}
3399f4b3ec61Sdh 			break;
34007c478bd9Sstevel@tonic-gate 		case T_DATA_REQ:
34017c478bd9Sstevel@tonic-gate 		case T_EXDATA_REQ:
34027c478bd9Sstevel@tonic-gate 		case T_ORDREL_REQ:
34037c478bd9Sstevel@tonic-gate 			/* Illegal for spdsock. */
34047c478bd9Sstevel@tonic-gate 			freemsg(mp);
34057c478bd9Sstevel@tonic-gate 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
3406f4b3ec61Sdh 			break;
34077c478bd9Sstevel@tonic-gate 		default:
34087c478bd9Sstevel@tonic-gate 			/* Not supported by spdsock. */
34097c478bd9Sstevel@tonic-gate 			spdsock_err_ack(q, mp, TNOTSUPPORT, 0);
3410f4b3ec61Sdh 			break;
34117c478bd9Sstevel@tonic-gate 		}
3412f4b3ec61Sdh 		return;
3413f8cbe0e7SDan McDonald 	case M_IOCDATA:
3414f8cbe0e7SDan McDonald 		keysock_spdsock_wput_iocdata(q, mp, PF_POLICY);
3415f8cbe0e7SDan McDonald 		return;
34167c478bd9Sstevel@tonic-gate 	case M_IOCTL:
34177c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
34187c478bd9Sstevel@tonic-gate 		error = EINVAL;
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
3421f8cbe0e7SDan McDonald 		case TI_GETMYNAME:
3422f8cbe0e7SDan McDonald 		case TI_GETPEERNAME:
3423f8cbe0e7SDan McDonald 			/*
3424f8cbe0e7SDan McDonald 			 * For pfiles(1) observability with getsockname().
3425f8cbe0e7SDan McDonald 			 * See keysock_spdsock_wput_iocdata() for the rest of
3426f8cbe0e7SDan McDonald 			 * this.
3427f8cbe0e7SDan McDonald 			 */
3428f8cbe0e7SDan McDonald 			mi_copyin(q, mp, NULL,
3429f8cbe0e7SDan McDonald 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
3430f8cbe0e7SDan McDonald 			return;
34317c478bd9Sstevel@tonic-gate 		case ND_SET:
34327c478bd9Sstevel@tonic-gate 		case ND_GET:
3433f4b3ec61Sdh 			if (nd_getset(q, spds->spds_g_nd, mp)) {
34347c478bd9Sstevel@tonic-gate 				qreply(q, mp);
34357c478bd9Sstevel@tonic-gate 				return;
34367c478bd9Sstevel@tonic-gate 			} else
34377c478bd9Sstevel@tonic-gate 				error = ENOENT;
34387c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
34397c478bd9Sstevel@tonic-gate 		default:
34407c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
34417c478bd9Sstevel@tonic-gate 			return;
34427c478bd9Sstevel@tonic-gate 		}
34437c478bd9Sstevel@tonic-gate 	case M_FLUSH:
34447c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
34457c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHALL);
34467c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
34477c478bd9Sstevel@tonic-gate 		}
34487c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
34497c478bd9Sstevel@tonic-gate 			qreply(q, mp);
34507c478bd9Sstevel@tonic-gate 			return;
34517c478bd9Sstevel@tonic-gate 		}
34527c478bd9Sstevel@tonic-gate 		/* Else FALLTHRU */
34537c478bd9Sstevel@tonic-gate 	}
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 	/* If fell through, just black-hole the message. */
34567c478bd9Sstevel@tonic-gate 	freemsg(mp);
34577c478bd9Sstevel@tonic-gate }
34587c478bd9Sstevel@tonic-gate 
34591752e73cSToomas Soome static int
spdsock_wput(queue_t * q,mblk_t * mp)34607c478bd9Sstevel@tonic-gate spdsock_wput(queue_t *q, mblk_t *mp)
34617c478bd9Sstevel@tonic-gate {
34627c478bd9Sstevel@tonic-gate 	uint8_t *rptr = mp->b_rptr;
34637c478bd9Sstevel@tonic-gate 	mblk_t *mp1;
34647c478bd9Sstevel@tonic-gate 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
3465f4b3ec61Sdh 	spd_stack_t	*spds = ss->spdsock_spds;
34667c478bd9Sstevel@tonic-gate 
34677c478bd9Sstevel@tonic-gate 	/*
34687c478bd9Sstevel@tonic-gate 	 * If we're dumping, defer processing other messages until the
34697c478bd9Sstevel@tonic-gate 	 * dump completes.
34707c478bd9Sstevel@tonic-gate 	 */
34717c478bd9Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL) {
34727c478bd9Sstevel@tonic-gate 		if (!putq(q, mp))
34737c478bd9Sstevel@tonic-gate 			freemsg(mp);
34741752e73cSToomas Soome 		return (0);
34757c478bd9Sstevel@tonic-gate 	}
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
34787c478bd9Sstevel@tonic-gate 	case M_DATA:
34797c478bd9Sstevel@tonic-gate 		/*
34807c478bd9Sstevel@tonic-gate 		 * Silently discard.
34817c478bd9Sstevel@tonic-gate 		 */
3482f4b3ec61Sdh 		ss2dbg(spds, ("raw M_DATA in spdsock.\n"));
34837c478bd9Sstevel@tonic-gate 		freemsg(mp);
34841752e73cSToomas Soome 		return (0);
34857c478bd9Sstevel@tonic-gate 	case M_PROTO:
34867c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
34877c478bd9Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
34887c478bd9Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
34897c478bd9Sstevel@tonic-gate 				if ((mp1 = mp->b_cont) == NULL) {
34907c478bd9Sstevel@tonic-gate 					/* No data after T_DATA_REQ. */
3491f4b3ec61Sdh 					ss2dbg(spds,
3492f4b3ec61Sdh 					    ("No data after DATA_REQ.\n"));
34937c478bd9Sstevel@tonic-gate 					freemsg(mp);
34941752e73cSToomas Soome 					return (0);
34957c478bd9Sstevel@tonic-gate 				}
34967c478bd9Sstevel@tonic-gate 				freeb(mp);
34977c478bd9Sstevel@tonic-gate 				mp = mp1;
3498f4b3ec61Sdh 				ss2dbg(spds, ("T_DATA_REQ\n"));
34997c478bd9Sstevel@tonic-gate 				break;	/* Out of switch. */
35007c478bd9Sstevel@tonic-gate 			}
35017c478bd9Sstevel@tonic-gate 		}
35027c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
35037c478bd9Sstevel@tonic-gate 	default:
3504f4b3ec61Sdh 		ss3dbg(spds, ("In default wput case (%d %d).\n",
35057c478bd9Sstevel@tonic-gate 		    mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
35067c478bd9Sstevel@tonic-gate 		spdsock_wput_other(q, mp);
35071752e73cSToomas Soome 		return (0);
35087c478bd9Sstevel@tonic-gate 	}
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	/* I now have a PF_POLICY message in an M_DATA block. */
35117c478bd9Sstevel@tonic-gate 	spdsock_parse(q, mp);
35121752e73cSToomas Soome 	return (0);
35137c478bd9Sstevel@tonic-gate }
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate /*
35167c478bd9Sstevel@tonic-gate  * Device open procedure, called when new queue pair created.
35177c478bd9Sstevel@tonic-gate  * We are passed the read-side queue.
35187c478bd9Sstevel@tonic-gate  */
35197c478bd9Sstevel@tonic-gate /* ARGSUSED */
35207c478bd9Sstevel@tonic-gate static int
spdsock_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)35217c478bd9Sstevel@tonic-gate spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
35227c478bd9Sstevel@tonic-gate {
35237c478bd9Sstevel@tonic-gate 	spdsock_t *ss;
35247c478bd9Sstevel@tonic-gate 	queue_t *oq = OTHERQ(q);
35257c478bd9Sstevel@tonic-gate 	minor_t ssminor;
3526f4b3ec61Sdh 	netstack_t *ns;
3527f4b3ec61Sdh 	spd_stack_t *spds;
35287c478bd9Sstevel@tonic-gate 
3529f4b3ec61Sdh 	if (secpolicy_ip_config(credp, B_FALSE) != 0)
35307c478bd9Sstevel@tonic-gate 		return (EPERM);
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	if (q->q_ptr != NULL)
35337c478bd9Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	if (sflag & MODOPEN)
35367c478bd9Sstevel@tonic-gate 		return (EINVAL);
35377c478bd9Sstevel@tonic-gate 
3538f4b3ec61Sdh 	ns = netstack_find_by_cred(credp);
3539f4b3ec61Sdh 	ASSERT(ns != NULL);
3540f4b3ec61Sdh 	spds = ns->netstack_spdsock;
3541f4b3ec61Sdh 	ASSERT(spds != NULL);
3542f4b3ec61Sdh 
3543f4b3ec61Sdh 	ss2dbg(spds, ("Made it into PF_POLICY socket open.\n"));
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate 	ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP);
3546f4b3ec61Sdh 	if (ssminor == 0) {
3547f4b3ec61Sdh 		netstack_rele(spds->spds_netstack);
35487c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3549f4b3ec61Sdh 	}
35507c478bd9Sstevel@tonic-gate 	ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP);
35517c478bd9Sstevel@tonic-gate 	if (ss == NULL) {
35527c478bd9Sstevel@tonic-gate 		vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1);
3553f4b3ec61Sdh 		netstack_rele(spds->spds_netstack);
35547c478bd9Sstevel@tonic-gate 		return (ENOMEM);
35557c478bd9Sstevel@tonic-gate 	}
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	ss->spdsock_minor = ssminor;
35587c478bd9Sstevel@tonic-gate 	ss->spdsock_state = TS_UNBND;
35597c478bd9Sstevel@tonic-gate 	ss->spdsock_dump_req = NULL;
35607c478bd9Sstevel@tonic-gate 
3561f4b3ec61Sdh 	ss->spdsock_spds = spds;
3562f4b3ec61Sdh 
35637c478bd9Sstevel@tonic-gate 	q->q_ptr = ss;
35647c478bd9Sstevel@tonic-gate 	oq->q_ptr = ss;
35657c478bd9Sstevel@tonic-gate 
3566f4b3ec61Sdh 	q->q_hiwat = spds->spds_recv_hiwat;
35677c478bd9Sstevel@tonic-gate 
3568f4b3ec61Sdh 	oq->q_hiwat = spds->spds_xmit_hiwat;
3569f4b3ec61Sdh 	oq->q_lowat = spds->spds_xmit_lowat;
35707c478bd9Sstevel@tonic-gate 
35717c478bd9Sstevel@tonic-gate 	qprocson(q);
35720f1702c5SYu Xiangning 	(void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat);
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), ss->spdsock_minor);
35757c478bd9Sstevel@tonic-gate 	return (0);
35767c478bd9Sstevel@tonic-gate }
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate /*
35797c478bd9Sstevel@tonic-gate  * Read-side service procedure, invoked when we get back-enabled
35807c478bd9Sstevel@tonic-gate  * when buffer space becomes available.
35817c478bd9Sstevel@tonic-gate  *
35827c478bd9Sstevel@tonic-gate  * Dump another chunk if we were dumping before; when we finish, kick
35837c478bd9Sstevel@tonic-gate  * the write-side queue in case it's waiting for read queue space.
35847c478bd9Sstevel@tonic-gate  */
35851752e73cSToomas Soome int
spdsock_rsrv(queue_t * q)35867c478bd9Sstevel@tonic-gate spdsock_rsrv(queue_t *q)
35877c478bd9Sstevel@tonic-gate {
35887c478bd9Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL)
35917c478bd9Sstevel@tonic-gate 		spdsock_dump_some(q, ss);
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	if (ss->spdsock_dump_req == NULL)
35947c478bd9Sstevel@tonic-gate 		qenable(OTHERQ(q));
35951752e73cSToomas Soome 	return (0);
35967c478bd9Sstevel@tonic-gate }
35977c478bd9Sstevel@tonic-gate 
35987c478bd9Sstevel@tonic-gate /*
35997c478bd9Sstevel@tonic-gate  * Write-side service procedure, invoked when we defer processing
36007c478bd9Sstevel@tonic-gate  * if another message is received while a dump is in progress.
36017c478bd9Sstevel@tonic-gate  */
36021752e73cSToomas Soome int
spdsock_wsrv(queue_t * q)36037c478bd9Sstevel@tonic-gate spdsock_wsrv(queue_t *q)
36047c478bd9Sstevel@tonic-gate {
36057c478bd9Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
36067c478bd9Sstevel@tonic-gate 	mblk_t *mp;
3607db061fdfSdanmcd 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	if (ss->spdsock_dump_req != NULL) {
36107c478bd9Sstevel@tonic-gate 		qenable(OTHERQ(q));
36111752e73cSToomas Soome 		return (0);
36127c478bd9Sstevel@tonic-gate 	}
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
3615f4b3ec61Sdh 		if (ipsec_loaded(ipss)) {
361678a53e20SJohn Levon 			(void) spdsock_wput(q, mp);
36177c478bd9Sstevel@tonic-gate 			if (ss->spdsock_dump_req != NULL)
36181752e73cSToomas Soome 				return (0);
3619f4b3ec61Sdh 		} else if (!ipsec_failed(ipss)) {
36207c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
36217c478bd9Sstevel@tonic-gate 		} else {
36227c478bd9Sstevel@tonic-gate 			spdsock_error(q, mp, EPFNOSUPPORT, 0);
36237c478bd9Sstevel@tonic-gate 		}
36247c478bd9Sstevel@tonic-gate 	}
36251752e73cSToomas Soome 	return (0);
36267c478bd9Sstevel@tonic-gate }
36277c478bd9Sstevel@tonic-gate 
36285e1743f0SToomas Soome /* ARGSUSED */
36297c478bd9Sstevel@tonic-gate static int
spdsock_close(queue_t * q,int flags __unused,cred_t * credp __unused)36305e1743f0SToomas Soome spdsock_close(queue_t *q, int flags __unused, cred_t *credp __unused)
36317c478bd9Sstevel@tonic-gate {
36327c478bd9Sstevel@tonic-gate 	spdsock_t *ss = q->q_ptr;
3633f4b3ec61Sdh 	spd_stack_t	*spds = ss->spdsock_spds;
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	qprocsoff(q);
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	/* Safe assumption. */
36387c478bd9Sstevel@tonic-gate 	ASSERT(ss != NULL);
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate 	if (ss->spdsock_timeout != 0)
36417c478bd9Sstevel@tonic-gate 		(void) quntimeout(q, ss->spdsock_timeout);
36427c478bd9Sstevel@tonic-gate 
3643f4b3ec61Sdh 	ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n"));
36447c478bd9Sstevel@tonic-gate 
36457c478bd9Sstevel@tonic-gate 	vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1);
3646f4b3ec61Sdh 	netstack_rele(ss->spdsock_spds->spds_netstack);
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate 	kmem_free(ss, sizeof (spdsock_t));
36497c478bd9Sstevel@tonic-gate 	return (0);
36507c478bd9Sstevel@tonic-gate }
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate /*
36537c478bd9Sstevel@tonic-gate  * Merge the IPsec algorithms tables with the received algorithm information.
36547c478bd9Sstevel@tonic-gate  */
36557c478bd9Sstevel@tonic-gate void
spdsock_merge_algs(spd_stack_t * spds)3656f4b3ec61Sdh spdsock_merge_algs(spd_stack_t *spds)
36577c478bd9Sstevel@tonic-gate {
36587c478bd9Sstevel@tonic-gate 	ipsec_alginfo_t *alg, *oalg;
36597c478bd9Sstevel@tonic-gate 	ipsec_algtype_t algtype;
36607c478bd9Sstevel@tonic-gate 	uint_t algidx, algid, nalgs;
36617c478bd9Sstevel@tonic-gate 	crypto_mech_name_t *mechs;
36627c478bd9Sstevel@tonic-gate 	uint_t mech_count, mech_idx;
3663f4b3ec61Sdh 	netstack_t	*ns = spds->spds_netstack;
3664f4b3ec61Sdh 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
36657c478bd9Sstevel@tonic-gate 
3666f4b3ec61Sdh 	ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate 	/*
36697c478bd9Sstevel@tonic-gate 	 * Get the list of supported mechanisms from the crypto framework.
36707c478bd9Sstevel@tonic-gate 	 * If a mechanism is supported by KCF, resolve its mechanism
36717c478bd9Sstevel@tonic-gate 	 * id and mark it as being valid. This operation must be done
36727c478bd9Sstevel@tonic-gate 	 * without holding alg_lock, since it can cause a provider
36737c478bd9Sstevel@tonic-gate 	 * module to be loaded and the provider notification callback to
36747c478bd9Sstevel@tonic-gate 	 * be invoked.
36757c478bd9Sstevel@tonic-gate 	 */
36767c478bd9Sstevel@tonic-gate 	mechs = crypto_get_mech_list(&mech_count, KM_SLEEP);
36777c478bd9Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
36787c478bd9Sstevel@tonic-gate 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
36797c478bd9Sstevel@tonic-gate 			int algflags = 0;
36807c478bd9Sstevel@tonic-gate 			crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID;
36817c478bd9Sstevel@tonic-gate 
3682f4b3ec61Sdh 			alg = spds->spds_algs[algtype][algid];
3683f4b3ec61Sdh 			if (alg == NULL)
36847c478bd9Sstevel@tonic-gate 				continue;
36857c478bd9Sstevel@tonic-gate 
36867c478bd9Sstevel@tonic-gate 			/*
36877c478bd9Sstevel@tonic-gate 			 * The NULL encryption algorithm is a special
36887c478bd9Sstevel@tonic-gate 			 * case because there are no mechanisms, yet
36897c478bd9Sstevel@tonic-gate 			 * the algorithm is still valid.
36907c478bd9Sstevel@tonic-gate 			 */
36917c478bd9Sstevel@tonic-gate 			if (alg->alg_id == SADB_EALG_NULL) {
36927c478bd9Sstevel@tonic-gate 				alg->alg_mech_type = CRYPTO_MECHANISM_INVALID;
3693628b0c67SMark Fenwick 				alg->alg_flags |= ALG_FLAG_VALID;
36947c478bd9Sstevel@tonic-gate 				continue;
36957c478bd9Sstevel@tonic-gate 			}
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 			for (mech_idx = 0; mech_idx < mech_count; mech_idx++) {
36987c478bd9Sstevel@tonic-gate 				if (strncmp(alg->alg_mech_name, mechs[mech_idx],
36997c478bd9Sstevel@tonic-gate 				    CRYPTO_MAX_MECH_NAME) == 0) {
37007c478bd9Sstevel@tonic-gate 					mt = crypto_mech2id(alg->alg_mech_name);
37017c478bd9Sstevel@tonic-gate 					ASSERT(mt != CRYPTO_MECHANISM_INVALID);
37027c478bd9Sstevel@tonic-gate 					algflags = ALG_FLAG_VALID;
37037c478bd9Sstevel@tonic-gate 					break;
37047c478bd9Sstevel@tonic-gate 				}
37057c478bd9Sstevel@tonic-gate 			}
37067c478bd9Sstevel@tonic-gate 			alg->alg_mech_type = mt;
3707628b0c67SMark Fenwick 			alg->alg_flags |= algflags;
37087c478bd9Sstevel@tonic-gate 		}
37097c478bd9Sstevel@tonic-gate 	}
37107c478bd9Sstevel@tonic-gate 
371169e71331SBayard Bell 	rw_enter(&ipss->ipsec_alg_lock, RW_WRITER);
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	/*
37147c478bd9Sstevel@tonic-gate 	 * For each algorithm currently defined, check if it is
37157c478bd9Sstevel@tonic-gate 	 * present in the new tables created from the SPD_UPDATEALGS
37167c478bd9Sstevel@tonic-gate 	 * message received from user-space.
37177c478bd9Sstevel@tonic-gate 	 * Delete the algorithm entries that are currently defined
37187c478bd9Sstevel@tonic-gate 	 * but not part of the new tables.
37197c478bd9Sstevel@tonic-gate 	 */
37207c478bd9Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3721f4b3ec61Sdh 		nalgs = ipss->ipsec_nalgs[algtype];
37227c478bd9Sstevel@tonic-gate 		for (algidx = 0; algidx < nalgs; algidx++) {
3723f4b3ec61Sdh 			algid = ipss->ipsec_sortlist[algtype][algidx];
3724f4b3ec61Sdh 			if (spds->spds_algs[algtype][algid] == NULL)
3725f4b3ec61Sdh 				ipsec_alg_unreg(algtype, algid, ns);
37267c478bd9Sstevel@tonic-gate 		}
37277c478bd9Sstevel@tonic-gate 	}
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate 	/*
37307c478bd9Sstevel@tonic-gate 	 * For each algorithm we just received, check if it is
37317c478bd9Sstevel@tonic-gate 	 * present in the currently defined tables. If it is, swap
37327c478bd9Sstevel@tonic-gate 	 * the entry with the one we just allocated.
37337c478bd9Sstevel@tonic-gate 	 * If the new algorithm is not in the current tables,
37347c478bd9Sstevel@tonic-gate 	 * add it.
37357c478bd9Sstevel@tonic-gate 	 */
37367c478bd9Sstevel@tonic-gate 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
37377c478bd9Sstevel@tonic-gate 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
3738f4b3ec61Sdh 			alg = spds->spds_algs[algtype][algid];
3739f4b3ec61Sdh 			if (alg == NULL)
37407c478bd9Sstevel@tonic-gate 				continue;
37417c478bd9Sstevel@tonic-gate 
3742f4b3ec61Sdh 			if ((oalg = ipss->ipsec_alglists[algtype][algid]) ==
3743f4b3ec61Sdh 			    NULL) {
37447c478bd9Sstevel@tonic-gate 				/*
37457c478bd9Sstevel@tonic-gate 				 * New algorithm, add it to the algorithm
37467c478bd9Sstevel@tonic-gate 				 * table.
37477c478bd9Sstevel@tonic-gate 				 */
3748f4b3ec61Sdh 				ipsec_alg_reg(algtype, alg, ns);
37497c478bd9Sstevel@tonic-gate 			} else {
37507c478bd9Sstevel@tonic-gate 				/*
37517c478bd9Sstevel@tonic-gate 				 * Algorithm is already in the table. Swap
37527c478bd9Sstevel@tonic-gate 				 * the existing entry with the new one.
37537c478bd9Sstevel@tonic-gate 				 */
3754f4b3ec61Sdh 				ipsec_alg_fix_min_max(alg, algtype, ns);
3755f4b3ec61Sdh 				ipss->ipsec_alglists[algtype][algid] = alg;
37567c478bd9Sstevel@tonic-gate 				ipsec_alg_free(oalg);
37577c478bd9Sstevel@tonic-gate 			}
3758f4b3ec61Sdh 			spds->spds_algs[algtype][algid] = NULL;
37597c478bd9Sstevel@tonic-gate 		}
37607c478bd9Sstevel@tonic-gate 	}
37617c478bd9Sstevel@tonic-gate 
3762f4b3ec61Sdh 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3763f4b3ec61Sdh 		ipss->ipsec_algs_exec_mode[algtype] =
3764f4b3ec61Sdh 		    spds->spds_algs_exec_mode[algtype];
3765f4b3ec61Sdh 	}
37667c478bd9Sstevel@tonic-gate 
376769e71331SBayard Bell 	rw_exit(&ipss->ipsec_alg_lock);
37687c478bd9Sstevel@tonic-gate 
37697c478bd9Sstevel@tonic-gate 	crypto_free_mech_list(mechs, mech_count);
37707c478bd9Sstevel@tonic-gate 
3771f4b3ec61Sdh 	ipsecah_algs_changed(ns);
3772f4b3ec61Sdh 	ipsecesp_algs_changed(ns);
37737c478bd9Sstevel@tonic-gate }
3774