xref: /onnv-gate/usr/src/uts/common/inet/ip/ipsecesp.c (revision 4987:9a60eca0a35c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52751Sdanmcd  * Common Development and Distribution License (the "License").
62751Sdanmcd  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/stream.h>
300Sstevel@tonic-gate #include <sys/stropts.h>
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/strlog.h>
330Sstevel@tonic-gate #include <sys/tihdr.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
383448Sdh155122 #include <sys/zone.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/cmn_err.h>
410Sstevel@tonic-gate #include <sys/vtrace.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/atomic.h>
440Sstevel@tonic-gate #include <sys/strsun.h>
450Sstevel@tonic-gate #include <sys/random.h>
460Sstevel@tonic-gate #include <netinet/in.h>
470Sstevel@tonic-gate #include <net/if.h>
480Sstevel@tonic-gate #include <netinet/ip6.h>
490Sstevel@tonic-gate #include <net/pfkeyv2.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <inet/common.h>
520Sstevel@tonic-gate #include <inet/mi.h>
530Sstevel@tonic-gate #include <inet/nd.h>
540Sstevel@tonic-gate #include <inet/ip.h>
55*4987Sdanmcd #include <inet/ip_impl.h>
560Sstevel@tonic-gate #include <inet/ip6.h>
570Sstevel@tonic-gate #include <inet/sadb.h>
580Sstevel@tonic-gate #include <inet/ipsec_info.h>
590Sstevel@tonic-gate #include <inet/ipsec_impl.h>
600Sstevel@tonic-gate #include <inet/ipsecesp.h>
610Sstevel@tonic-gate #include <inet/ipdrop.h>
620Sstevel@tonic-gate #include <inet/tcp.h>
630Sstevel@tonic-gate #include <sys/kstat.h>
640Sstevel@tonic-gate #include <sys/policy.h>
650Sstevel@tonic-gate #include <sys/strsun.h>
660Sstevel@tonic-gate #include <inet/udp_impl.h>
670Sstevel@tonic-gate #include <sys/taskq.h>
683448Sdh155122 #include <sys/note.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <sys/iphada.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Table of ND variables supported by ipsecesp. These are loaded into
740Sstevel@tonic-gate  * ipsecesp_g_nd in ipsecesp_init_nd.
750Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
760Sstevel@tonic-gate  */
773448Sdh155122 static	ipsecespparam_t	lcl_param_arr[] = {
780Sstevel@tonic-gate 	/* min	max			value	name */
790Sstevel@tonic-gate 	{ 0,	3,			0,	"ipsecesp_debug"},
800Sstevel@tonic-gate 	{ 125,	32000, SADB_AGE_INTERVAL_DEFAULT, "ipsecesp_age_interval"},
810Sstevel@tonic-gate 	{ 1,	10,			1,	"ipsecesp_reap_delay"},
820Sstevel@tonic-gate 	{ 1,	SADB_MAX_REPLAY,	64,	"ipsecesp_replay_size"},
830Sstevel@tonic-gate 	{ 1,	300,			15,	"ipsecesp_acquire_timeout"},
840Sstevel@tonic-gate 	{ 1,	1800,			90,	"ipsecesp_larval_timeout"},
850Sstevel@tonic-gate 	/* Default lifetime values for ACQUIRE messages. */
860Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_soft_bytes"},
870Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_hard_bytes"},
880Sstevel@tonic-gate 	{ 0,	0xffffffffU,	24000,	"ipsecesp_default_soft_addtime"},
890Sstevel@tonic-gate 	{ 0,	0xffffffffU,	28800,	"ipsecesp_default_hard_addtime"},
900Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_soft_usetime"},
910Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_hard_usetime"},
920Sstevel@tonic-gate 	{ 0,	1,		0,	"ipsecesp_log_unknown_spi"},
930Sstevel@tonic-gate 	{ 0,	2,		1,	"ipsecesp_padding_check"},
94*4987Sdanmcd 	{ 0,	600,		20,	"ipsecesp_nat_keepalive_interval"},
950Sstevel@tonic-gate };
963448Sdh155122 #define	ipsecesp_debug	ipsecesp_params[0].ipsecesp_param_value
973448Sdh155122 #define	ipsecesp_age_interval ipsecesp_params[1].ipsecesp_param_value
983448Sdh155122 #define	ipsecesp_age_int_max	ipsecesp_params[1].ipsecesp_param_max
993448Sdh155122 #define	ipsecesp_reap_delay	ipsecesp_params[2].ipsecesp_param_value
1003448Sdh155122 #define	ipsecesp_replay_size	ipsecesp_params[3].ipsecesp_param_value
1013448Sdh155122 #define	ipsecesp_acquire_timeout	\
1023448Sdh155122 	ipsecesp_params[4].ipsecesp_param_value
1033448Sdh155122 #define	ipsecesp_larval_timeout	\
1043448Sdh155122 	ipsecesp_params[5].ipsecesp_param_value
1053448Sdh155122 #define	ipsecesp_default_soft_bytes	\
1063448Sdh155122 	ipsecesp_params[6].ipsecesp_param_value
1073448Sdh155122 #define	ipsecesp_default_hard_bytes	\
1083448Sdh155122 	ipsecesp_params[7].ipsecesp_param_value
1093448Sdh155122 #define	ipsecesp_default_soft_addtime	\
1103448Sdh155122 	ipsecesp_params[8].ipsecesp_param_value
1113448Sdh155122 #define	ipsecesp_default_hard_addtime	\
1123448Sdh155122 	ipsecesp_params[9].ipsecesp_param_value
1133448Sdh155122 #define	ipsecesp_default_soft_usetime	\
1143448Sdh155122 	ipsecesp_params[10].ipsecesp_param_value
1153448Sdh155122 #define	ipsecesp_default_hard_usetime	\
1163448Sdh155122 	ipsecesp_params[11].ipsecesp_param_value
1173448Sdh155122 #define	ipsecesp_log_unknown_spi	\
1183448Sdh155122 	ipsecesp_params[12].ipsecesp_param_value
1193448Sdh155122 #define	ipsecesp_padding_check	\
1203448Sdh155122 	ipsecesp_params[13].ipsecesp_param_value
121*4987Sdanmcd /* For ipsecesp_nat_keepalive_interval, see ipsecesp.h. */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #define	esp0dbg(a)	printf a
1240Sstevel@tonic-gate /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
1253448Sdh155122 #define	esp1dbg(espstack, a)	if (espstack->ipsecesp_debug != 0) printf a
1263448Sdh155122 #define	esp2dbg(espstack, a)	if (espstack->ipsecesp_debug > 1) printf a
1273448Sdh155122 #define	esp3dbg(espstack, a)	if (espstack->ipsecesp_debug > 2) printf a
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static int ipsecesp_open(queue_t *, dev_t *, int, int, cred_t *);
1300Sstevel@tonic-gate static int ipsecesp_close(queue_t *);
1310Sstevel@tonic-gate static void ipsecesp_rput(queue_t *, mblk_t *);
1320Sstevel@tonic-gate static void ipsecesp_wput(queue_t *, mblk_t *);
1333448Sdh155122 static void	*ipsecesp_stack_init(netstackid_t stackid, netstack_t *ns);
1343448Sdh155122 static void	ipsecesp_stack_fini(netstackid_t stackid, void *arg);
1353448Sdh155122 static void esp_send_acquire(ipsacq_t *, mblk_t *, netstack_t *);
1360Sstevel@tonic-gate 
137*4987Sdanmcd static void esp_prepare_udp(netstack_t *, mblk_t *, ipha_t *);
1380Sstevel@tonic-gate static ipsec_status_t esp_outbound_accelerated(mblk_t *, uint_t);
1390Sstevel@tonic-gate static ipsec_status_t esp_inbound_accelerated(mblk_t *, mblk_t *,
1400Sstevel@tonic-gate     boolean_t, ipsa_t *);
1410Sstevel@tonic-gate 
1423448Sdh155122 static boolean_t esp_register_out(uint32_t, uint32_t, uint_t,
1433448Sdh155122     ipsecesp_stack_t *);
1440Sstevel@tonic-gate static boolean_t esp_strip_header(mblk_t *, boolean_t, uint32_t,
1453448Sdh155122     kstat_named_t **, ipsecesp_stack_t *);
1460Sstevel@tonic-gate static ipsec_status_t esp_submit_req_inbound(mblk_t *, ipsa_t *, uint_t);
1470Sstevel@tonic-gate static ipsec_status_t esp_submit_req_outbound(mblk_t *, ipsa_t *, uchar_t *,
1480Sstevel@tonic-gate     uint_t);
1493448Sdh155122 /* Setable in /etc/system */
1503448Sdh155122 uint32_t esp_hash_size = IPSEC_DEFAULT_HASH_SIZE;
1513448Sdh155122 
1520Sstevel@tonic-gate static struct module_info info = {
1530Sstevel@tonic-gate 	5137, "ipsecesp", 0, INFPSZ, 65536, 1024
1540Sstevel@tonic-gate };
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct qinit rinit = {
1570Sstevel@tonic-gate 	(pfi_t)ipsecesp_rput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info,
1580Sstevel@tonic-gate 	NULL
1590Sstevel@tonic-gate };
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static struct qinit winit = {
1620Sstevel@tonic-gate 	(pfi_t)ipsecesp_wput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info,
1630Sstevel@tonic-gate 	NULL
1640Sstevel@tonic-gate };
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate struct streamtab ipsecespinfo = {
1670Sstevel@tonic-gate 	&rinit, &winit, NULL, NULL
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static taskq_t *esp_taskq;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * OTOH, this one is set at open/close, and I'm D_MTQPAIR for now.
1740Sstevel@tonic-gate  *
1750Sstevel@tonic-gate  * Question:	Do I need this, given that all instance's esps->esps_wq point
1760Sstevel@tonic-gate  *		to IP?
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  * Answer:	Yes, because I need to know which queue is BOUND to
1790Sstevel@tonic-gate  *		IPPROTO_ESP
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * Stats.  This may eventually become a full-blown SNMP MIB once that spec
1840Sstevel@tonic-gate  * stabilizes.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate 
1873448Sdh155122 typedef struct esp_kstats_s {
1880Sstevel@tonic-gate 	kstat_named_t esp_stat_num_aalgs;
1890Sstevel@tonic-gate 	kstat_named_t esp_stat_good_auth;
1900Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_auth;
1910Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_padding;
1920Sstevel@tonic-gate 	kstat_named_t esp_stat_replay_failures;
1930Sstevel@tonic-gate 	kstat_named_t esp_stat_replay_early_failures;
1940Sstevel@tonic-gate 	kstat_named_t esp_stat_keysock_in;
1950Sstevel@tonic-gate 	kstat_named_t esp_stat_out_requests;
1960Sstevel@tonic-gate 	kstat_named_t esp_stat_acquire_requests;
1970Sstevel@tonic-gate 	kstat_named_t esp_stat_bytes_expired;
1980Sstevel@tonic-gate 	kstat_named_t esp_stat_out_discards;
1990Sstevel@tonic-gate 	kstat_named_t esp_stat_in_accelerated;
2000Sstevel@tonic-gate 	kstat_named_t esp_stat_out_accelerated;
2010Sstevel@tonic-gate 	kstat_named_t esp_stat_noaccel;
2020Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_sync;
2030Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_async;
2040Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_failures;
2050Sstevel@tonic-gate 	kstat_named_t esp_stat_num_ealgs;
2060Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_decrypt;
2070Sstevel@tonic-gate } esp_kstats_t;
2080Sstevel@tonic-gate 
2093448Sdh155122 /*
2103448Sdh155122  * espstack->esp_kstats is equal to espstack->esp_ksp->ks_data if
2113448Sdh155122  * kstat_create_netstack for espstack->esp_ksp succeeds, but when it
2123448Sdh155122  * fails, it will be NULL. Note this is done for all stack instances,
2133448Sdh155122  * so it *could* fail. hence a non-NULL checking is done for
2143448Sdh155122  * ESP_BUMP_STAT and ESP_DEBUMP_STAT
2153448Sdh155122  */
2163448Sdh155122 #define	ESP_BUMP_STAT(espstack, x)					\
2173448Sdh155122 do {									\
2183448Sdh155122 	if (espstack->esp_kstats != NULL)				\
2193448Sdh155122 		(espstack->esp_kstats->esp_stat_ ## x).value.ui64++;	\
2203448Sdh155122 _NOTE(CONSTCOND)							\
2213448Sdh155122 } while (0)
2223448Sdh155122 
2233448Sdh155122 #define	ESP_DEBUMP_STAT(espstack, x)					\
2243448Sdh155122 do {									\
2253448Sdh155122 	if (espstack->esp_kstats != NULL)				\
2263448Sdh155122 		(espstack->esp_kstats->esp_stat_ ## x).value.ui64--;	\
2273448Sdh155122 _NOTE(CONSTCOND)							\
2283448Sdh155122 } while (0)
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate static int	esp_kstat_update(kstat_t *, int);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate static boolean_t
2333448Sdh155122 esp_kstat_init(ipsecesp_stack_t *espstack, netstackid_t stackid)
2340Sstevel@tonic-gate {
2353448Sdh155122 	espstack->esp_ksp = kstat_create_netstack("ipsecesp", 0, "esp_stat",
2363448Sdh155122 	    "net", KSTAT_TYPE_NAMED,
2373448Sdh155122 	    sizeof (esp_kstats_t) / sizeof (kstat_named_t),
2383448Sdh155122 	    KSTAT_FLAG_PERSISTENT, stackid);
2393448Sdh155122 
2403448Sdh155122 	if (espstack->esp_ksp == NULL || espstack->esp_ksp->ks_data == NULL)
2410Sstevel@tonic-gate 		return (B_FALSE);
2420Sstevel@tonic-gate 
2433448Sdh155122 	espstack->esp_kstats = espstack->esp_ksp->ks_data;
2443448Sdh155122 
2453448Sdh155122 	espstack->esp_ksp->ks_update = esp_kstat_update;
2463448Sdh155122 	espstack->esp_ksp->ks_private = (void *)(uintptr_t)stackid;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate #define	K64 KSTAT_DATA_UINT64
2493448Sdh155122 #define	KI(x) kstat_named_init(&(espstack->esp_kstats->esp_stat_##x), #x, K64)
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	KI(num_aalgs);
2520Sstevel@tonic-gate 	KI(num_ealgs);
2530Sstevel@tonic-gate 	KI(good_auth);
2540Sstevel@tonic-gate 	KI(bad_auth);
2550Sstevel@tonic-gate 	KI(bad_padding);
2560Sstevel@tonic-gate 	KI(replay_failures);
2570Sstevel@tonic-gate 	KI(replay_early_failures);
2580Sstevel@tonic-gate 	KI(keysock_in);
2590Sstevel@tonic-gate 	KI(out_requests);
2600Sstevel@tonic-gate 	KI(acquire_requests);
2610Sstevel@tonic-gate 	KI(bytes_expired);
2620Sstevel@tonic-gate 	KI(out_discards);
2630Sstevel@tonic-gate 	KI(in_accelerated);
2640Sstevel@tonic-gate 	KI(out_accelerated);
2650Sstevel@tonic-gate 	KI(noaccel);
2660Sstevel@tonic-gate 	KI(crypto_sync);
2670Sstevel@tonic-gate 	KI(crypto_async);
2680Sstevel@tonic-gate 	KI(crypto_failures);
2690Sstevel@tonic-gate 	KI(bad_decrypt);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate #undef KI
2720Sstevel@tonic-gate #undef K64
2730Sstevel@tonic-gate 
2743448Sdh155122 	kstat_install(espstack->esp_ksp);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	return (B_TRUE);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate static int
2800Sstevel@tonic-gate esp_kstat_update(kstat_t *kp, int rw)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	esp_kstats_t *ekp;
2833448Sdh155122 	netstackid_t	stackid = (zoneid_t)(uintptr_t)kp->ks_private;
2843448Sdh155122 	netstack_t	*ns;
2853448Sdh155122 	ipsec_stack_t	*ipss;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if ((kp == NULL) || (kp->ks_data == NULL))
2880Sstevel@tonic-gate 		return (EIO);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
2910Sstevel@tonic-gate 		return (EACCES);
2920Sstevel@tonic-gate 
2933448Sdh155122 	ns = netstack_find_by_stackid(stackid);
2943448Sdh155122 	if (ns == NULL)
2953448Sdh155122 		return (-1);
2963448Sdh155122 	ipss = ns->netstack_ipsec;
2973448Sdh155122 	if (ipss == NULL) {
2983448Sdh155122 		netstack_rele(ns);
2993448Sdh155122 		return (-1);
3003448Sdh155122 	}
3010Sstevel@tonic-gate 	ekp = (esp_kstats_t *)kp->ks_data;
3023448Sdh155122 
3033448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
3043448Sdh155122 	ekp->esp_stat_num_aalgs.value.ui64 =
3053448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
3063448Sdh155122 	ekp->esp_stat_num_ealgs.value.ui64 =
3073448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
3083448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
3093448Sdh155122 
3103448Sdh155122 	netstack_rele(ns);
3110Sstevel@tonic-gate 	return (0);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate #ifdef DEBUG
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * Debug routine, useful to see pre-encryption data.
3170Sstevel@tonic-gate  */
3180Sstevel@tonic-gate static char *
3190Sstevel@tonic-gate dump_msg(mblk_t *mp)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	char tmp_str[3], tmp_line[256];
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	while (mp != NULL) {
3240Sstevel@tonic-gate 		unsigned char *ptr;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 		printf("mblk address 0x%p, length %ld, db_ref %d "
3270Sstevel@tonic-gate 		    "type %d, base 0x%p, lim 0x%p\n",
3280Sstevel@tonic-gate 		    (void *) mp, (long)(mp->b_wptr - mp->b_rptr),
3290Sstevel@tonic-gate 		    mp->b_datap->db_ref, mp->b_datap->db_type,
3300Sstevel@tonic-gate 		    (void *)mp->b_datap->db_base, (void *)mp->b_datap->db_lim);
3310Sstevel@tonic-gate 		ptr = mp->b_rptr;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		tmp_line[0] = '\0';
3340Sstevel@tonic-gate 		while (ptr < mp->b_wptr) {
3350Sstevel@tonic-gate 			uint_t diff;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 			diff = (ptr - mp->b_rptr);
3380Sstevel@tonic-gate 			if (!(diff & 0x1f)) {
3390Sstevel@tonic-gate 				if (strlen(tmp_line) > 0) {
3400Sstevel@tonic-gate 					printf("bytes: %s\n", tmp_line);
3410Sstevel@tonic-gate 					tmp_line[0] = '\0';
3420Sstevel@tonic-gate 				}
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 			if (!(diff & 0x3))
3450Sstevel@tonic-gate 				(void) strcat(tmp_line, " ");
3460Sstevel@tonic-gate 			(void) sprintf(tmp_str, "%02x", *ptr);
3470Sstevel@tonic-gate 			(void) strcat(tmp_line, tmp_str);
3480Sstevel@tonic-gate 			ptr++;
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 		if (strlen(tmp_line) > 0)
3510Sstevel@tonic-gate 			printf("bytes: %s\n", tmp_line);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		mp = mp->b_cont;
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	return ("\n");
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate #else /* DEBUG */
3600Sstevel@tonic-gate static char *
3610Sstevel@tonic-gate dump_msg(mblk_t *mp)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	printf("Find value of mp %p.\n", mp);
3640Sstevel@tonic-gate 	return ("\n");
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate #endif /* DEBUG */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate  * Don't have to lock age_interval, as only one thread will access it at
3700Sstevel@tonic-gate  * a time, because I control the one function that does with timeout().
3710Sstevel@tonic-gate  */
3720Sstevel@tonic-gate static void
3733448Sdh155122 esp_ager(void *arg)
3740Sstevel@tonic-gate {
3753448Sdh155122 	ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)arg;
3763448Sdh155122 	netstack_t	*ns = espstack->ipsecesp_netstack;
3770Sstevel@tonic-gate 	hrtime_t begin = gethrtime();
3780Sstevel@tonic-gate 
3793448Sdh155122 	sadb_ager(&espstack->esp_sadb.s_v4, espstack->esp_pfkey_q,
3803448Sdh155122 	    espstack->esp_sadb.s_ip_q, espstack->ipsecesp_reap_delay, ns);
3813448Sdh155122 	sadb_ager(&espstack->esp_sadb.s_v6, espstack->esp_pfkey_q,
3823448Sdh155122 	    espstack->esp_sadb.s_ip_q, espstack->ipsecesp_reap_delay, ns);
3833448Sdh155122 
3843448Sdh155122 	espstack->esp_event = sadb_retimeout(begin, espstack->esp_pfkey_q,
3853448Sdh155122 	    esp_ager, espstack,
3863448Sdh155122 	    &espstack->ipsecesp_age_interval, espstack->ipsecesp_age_int_max,
3873448Sdh155122 	    info.mi_idnum);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate  * Get an ESP NDD parameter.
3920Sstevel@tonic-gate  */
3930Sstevel@tonic-gate /* ARGSUSED */
3940Sstevel@tonic-gate static int
3950Sstevel@tonic-gate ipsecesp_param_get(q, mp, cp, cr)
3960Sstevel@tonic-gate 	queue_t	*q;
3970Sstevel@tonic-gate 	mblk_t	*mp;
3980Sstevel@tonic-gate 	caddr_t	cp;
3990Sstevel@tonic-gate 	cred_t *cr;
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate 	ipsecespparam_t	*ipsecesppa = (ipsecespparam_t *)cp;
4020Sstevel@tonic-gate 	uint_t value;
4033448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
4043448Sdh155122 
4053448Sdh155122 	mutex_enter(&espstack->ipsecesp_param_lock);
4060Sstevel@tonic-gate 	value = ipsecesppa->ipsecesp_param_value;
4073448Sdh155122 	mutex_exit(&espstack->ipsecesp_param_lock);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
4100Sstevel@tonic-gate 	return (0);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * This routine sets an NDD variable in a ipsecespparam_t structure.
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate /* ARGSUSED */
4170Sstevel@tonic-gate static int
4180Sstevel@tonic-gate ipsecesp_param_set(q, mp, value, cp, cr)
4190Sstevel@tonic-gate 	queue_t	*q;
4200Sstevel@tonic-gate 	mblk_t	*mp;
4210Sstevel@tonic-gate 	char	*value;
4220Sstevel@tonic-gate 	caddr_t	cp;
4230Sstevel@tonic-gate 	cred_t *cr;
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	ulong_t	new_value;
4260Sstevel@tonic-gate 	ipsecespparam_t	*ipsecesppa = (ipsecespparam_t *)cp;
4273448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/*
4300Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
4310Sstevel@tonic-gate 	 * required bounds.
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
4340Sstevel@tonic-gate 	    new_value < ipsecesppa->ipsecesp_param_min ||
4350Sstevel@tonic-gate 	    new_value > ipsecesppa->ipsecesp_param_max) {
4360Sstevel@tonic-gate 		return (EINVAL);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/* Set the new value */
4403448Sdh155122 	mutex_enter(&espstack->ipsecesp_param_lock);
4410Sstevel@tonic-gate 	ipsecesppa->ipsecesp_param_value = new_value;
4423448Sdh155122 	mutex_exit(&espstack->ipsecesp_param_lock);
4430Sstevel@tonic-gate 	return (0);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  * Using lifetime NDD variables, fill in an extended combination's
4480Sstevel@tonic-gate  * lifetime information.
4490Sstevel@tonic-gate  */
4500Sstevel@tonic-gate void
4513448Sdh155122 ipsecesp_fill_defs(sadb_x_ecomb_t *ecomb, netstack_t *ns)
4520Sstevel@tonic-gate {
4533448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
4543448Sdh155122 
4553448Sdh155122 	ecomb->sadb_x_ecomb_soft_bytes = espstack->ipsecesp_default_soft_bytes;
4563448Sdh155122 	ecomb->sadb_x_ecomb_hard_bytes = espstack->ipsecesp_default_hard_bytes;
4573448Sdh155122 	ecomb->sadb_x_ecomb_soft_addtime =
4583448Sdh155122 	    espstack->ipsecesp_default_soft_addtime;
4593448Sdh155122 	ecomb->sadb_x_ecomb_hard_addtime =
4603448Sdh155122 	    espstack->ipsecesp_default_hard_addtime;
4613448Sdh155122 	ecomb->sadb_x_ecomb_soft_usetime =
4623448Sdh155122 	    espstack->ipsecesp_default_soft_usetime;
4633448Sdh155122 	ecomb->sadb_x_ecomb_hard_usetime =
4643448Sdh155122 	    espstack->ipsecesp_default_hard_usetime;
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate  * Initialize things for ESP at module load time.
4690Sstevel@tonic-gate  */
4700Sstevel@tonic-gate boolean_t
4710Sstevel@tonic-gate ipsecesp_ddi_init(void)
4720Sstevel@tonic-gate {
4733448Sdh155122 	esp_taskq = taskq_create("esp_taskq", 1, minclsyspri,
4743448Sdh155122 	    IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0);
4753448Sdh155122 
4763448Sdh155122 	/*
4773448Sdh155122 	 * We want to be informed each time a stack is created or
4783448Sdh155122 	 * destroyed in the kernel, so we can maintain the
4793448Sdh155122 	 * set of ipsecesp_stack_t's.
4803448Sdh155122 	 */
4813448Sdh155122 	netstack_register(NS_IPSECESP, ipsecesp_stack_init, NULL,
4823448Sdh155122 	    ipsecesp_stack_fini);
4833448Sdh155122 
4843448Sdh155122 	return (B_TRUE);
4853448Sdh155122 }
4863448Sdh155122 
4873448Sdh155122 /*
4883448Sdh155122  * Walk through the param array specified registering each element with the
4893448Sdh155122  * named dispatch handler.
4903448Sdh155122  */
4913448Sdh155122 static boolean_t
4923448Sdh155122 ipsecesp_param_register(IDP *ndp, ipsecespparam_t *espp, int cnt)
4933448Sdh155122 {
4943448Sdh155122 	for (; cnt-- > 0; espp++) {
4950Sstevel@tonic-gate 		if (espp->ipsecesp_param_name != NULL &&
4960Sstevel@tonic-gate 		    espp->ipsecesp_param_name[0]) {
4973448Sdh155122 			if (!nd_load(ndp,
4983448Sdh155122 			    espp->ipsecesp_param_name,
4990Sstevel@tonic-gate 			    ipsecesp_param_get, ipsecesp_param_set,
5000Sstevel@tonic-gate 			    (caddr_t)espp)) {
5013448Sdh155122 				nd_free(ndp);
5020Sstevel@tonic-gate 				return (B_FALSE);
5030Sstevel@tonic-gate 			}
5040Sstevel@tonic-gate 		}
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 	return (B_TRUE);
5070Sstevel@tonic-gate }
5083448Sdh155122 /*
5093448Sdh155122  * Initialize things for ESP for each stack instance
5103448Sdh155122  */
5113448Sdh155122 static void *
5123448Sdh155122 ipsecesp_stack_init(netstackid_t stackid, netstack_t *ns)
5133448Sdh155122 {
5143448Sdh155122 	ipsecesp_stack_t	*espstack;
5153448Sdh155122 	ipsecespparam_t		*espp;
5163448Sdh155122 
5173448Sdh155122 	espstack = (ipsecesp_stack_t *)kmem_zalloc(sizeof (*espstack),
5183448Sdh155122 	    KM_SLEEP);
5193448Sdh155122 	espstack->ipsecesp_netstack = ns;
5203448Sdh155122 
5213448Sdh155122 	espp = (ipsecespparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
5223448Sdh155122 	espstack->ipsecesp_params = espp;
5233448Sdh155122 	bcopy(lcl_param_arr, espp, sizeof (lcl_param_arr));
5243448Sdh155122 
5253448Sdh155122 	(void) ipsecesp_param_register(&espstack->ipsecesp_g_nd, espp,
5263448Sdh155122 	    A_CNT(lcl_param_arr));
5273448Sdh155122 
5283448Sdh155122 	(void) esp_kstat_init(espstack, stackid);
5293448Sdh155122 
5303448Sdh155122 	espstack->esp_sadb.s_acquire_timeout =
5313448Sdh155122 	    &espstack->ipsecesp_acquire_timeout;
5323448Sdh155122 	espstack->esp_sadb.s_acqfn = esp_send_acquire;
5333448Sdh155122 	sadbp_init("ESP", &espstack->esp_sadb, SADB_SATYPE_ESP, esp_hash_size,
5343448Sdh155122 	    espstack->ipsecesp_netstack);
5353448Sdh155122 
5363448Sdh155122 	mutex_init(&espstack->ipsecesp_param_lock, NULL, MUTEX_DEFAULT, 0);
5373448Sdh155122 
5383448Sdh155122 	ip_drop_register(&espstack->esp_dropper, "IPsec ESP");
5393448Sdh155122 	return (espstack);
5403448Sdh155122 }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate  * Destroy things for ESP at module unload time.
5440Sstevel@tonic-gate  */
5450Sstevel@tonic-gate void
5460Sstevel@tonic-gate ipsecesp_ddi_destroy(void)
5470Sstevel@tonic-gate {
5483448Sdh155122 	netstack_unregister(NS_IPSECESP);
5490Sstevel@tonic-gate 	taskq_destroy(esp_taskq);
5503448Sdh155122 }
5513448Sdh155122 
5523448Sdh155122 /*
5533448Sdh155122  * Destroy things for ESP for one stack instance
5543448Sdh155122  */
5553448Sdh155122 static void
5563448Sdh155122 ipsecesp_stack_fini(netstackid_t stackid, void *arg)
5573448Sdh155122 {
5583448Sdh155122 	ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)arg;
5593448Sdh155122 
5603448Sdh155122 	if (espstack->esp_pfkey_q != NULL) {
5613448Sdh155122 		(void) quntimeout(espstack->esp_pfkey_q, espstack->esp_event);
5623448Sdh155122 	}
5633448Sdh155122 	espstack->esp_sadb.s_acqfn = NULL;
5643448Sdh155122 	espstack->esp_sadb.s_acquire_timeout = NULL;
5653448Sdh155122 	sadbp_destroy(&espstack->esp_sadb, espstack->ipsecesp_netstack);
5663448Sdh155122 	ip_drop_unregister(&espstack->esp_dropper);
5673448Sdh155122 	mutex_destroy(&espstack->ipsecesp_param_lock);
5683448Sdh155122 	nd_free(&espstack->ipsecesp_g_nd);
5693448Sdh155122 
5703448Sdh155122 	kmem_free(espstack->ipsecesp_params, sizeof (lcl_param_arr));
5713448Sdh155122 	espstack->ipsecesp_params = NULL;
5723448Sdh155122 	kstat_delete_netstack(espstack->esp_ksp, stackid);
5733448Sdh155122 	espstack->esp_ksp = NULL;
5743448Sdh155122 	espstack->esp_kstats = NULL;
5753448Sdh155122 	kmem_free(espstack, sizeof (*espstack));
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate  * ESP module open routine.
5800Sstevel@tonic-gate  */
5810Sstevel@tonic-gate /* ARGSUSED */
5820Sstevel@tonic-gate static int
5830Sstevel@tonic-gate ipsecesp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5840Sstevel@tonic-gate {
5853448Sdh155122 	netstack_t		*ns;
5863448Sdh155122 	ipsecesp_stack_t	*espstack;
5873448Sdh155122 
5883448Sdh155122 	if (secpolicy_ip_config(credp, B_FALSE) != 0) {
5893448Sdh155122 		esp0dbg(("Non-privileged user trying to open ipsecesp.\n"));
5900Sstevel@tonic-gate 		return (EPERM);
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5940Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	if (sflag != MODOPEN)
5970Sstevel@tonic-gate 		return (EINVAL);
5980Sstevel@tonic-gate 
5993448Sdh155122 	ns = netstack_find_by_cred(credp);
6003448Sdh155122 	ASSERT(ns != NULL);
6013448Sdh155122 	espstack = ns->netstack_ipsecesp;
6023448Sdh155122 	ASSERT(espstack != NULL);
6033448Sdh155122 
6040Sstevel@tonic-gate 	/*
6050Sstevel@tonic-gate 	 * ASSUMPTIONS (because I'm MT_OCEXCL):
6060Sstevel@tonic-gate 	 *
6070Sstevel@tonic-gate 	 *	* I'm being pushed on top of IP for all my opens (incl. #1).
6080Sstevel@tonic-gate 	 *	* Only ipsecesp_open() can write into esp_sadb.s_ip_q.
6090Sstevel@tonic-gate 	 *	* Because of this, I can check lazily for esp_sadb.s_ip_q.
6100Sstevel@tonic-gate 	 *
6110Sstevel@tonic-gate 	 *  If these assumptions are wrong, I'm in BIG trouble...
6120Sstevel@tonic-gate 	 */
6130Sstevel@tonic-gate 
6143448Sdh155122 	q->q_ptr = espstack;
6153448Sdh155122 	WR(q)->q_ptr = q->q_ptr;
6163448Sdh155122 
6173448Sdh155122 	if (espstack->esp_sadb.s_ip_q == NULL) {
6180Sstevel@tonic-gate 		struct T_unbind_req *tur;
6190Sstevel@tonic-gate 
6203448Sdh155122 		espstack->esp_sadb.s_ip_q = WR(q);
6210Sstevel@tonic-gate 		/* Allocate an unbind... */
6223448Sdh155122 		espstack->esp_ip_unbind = allocb(sizeof (struct T_unbind_req),
6233448Sdh155122 		    BPRI_HI);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * Send down T_BIND_REQ to bind IPPROTO_ESP.
6270Sstevel@tonic-gate 		 * Handle the ACK here in ESP.
6280Sstevel@tonic-gate 		 */
6290Sstevel@tonic-gate 		qprocson(q);
6303448Sdh155122 		if (espstack->esp_ip_unbind == NULL ||
6313448Sdh155122 		    !sadb_t_bind_req(espstack->esp_sadb.s_ip_q, IPPROTO_ESP)) {
6323448Sdh155122 			if (espstack->esp_ip_unbind != NULL) {
6333448Sdh155122 				freeb(espstack->esp_ip_unbind);
6343448Sdh155122 				espstack->esp_ip_unbind = NULL;
6350Sstevel@tonic-gate 			}
6360Sstevel@tonic-gate 			q->q_ptr = NULL;
6373448Sdh155122 			netstack_rele(espstack->ipsecesp_netstack);
6380Sstevel@tonic-gate 			return (ENOMEM);
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 
6413448Sdh155122 		espstack->esp_ip_unbind->b_datap->db_type = M_PROTO;
6423448Sdh155122 		tur = (struct T_unbind_req *)espstack->esp_ip_unbind->b_rptr;
6430Sstevel@tonic-gate 		tur->PRIM_type = T_UNBIND_REQ;
6440Sstevel@tonic-gate 	} else {
6450Sstevel@tonic-gate 		qprocson(q);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	/*
6490Sstevel@tonic-gate 	 * For now, there's not much I can do.  I'll be getting a message
6500Sstevel@tonic-gate 	 * passed down to me from keysock (in my wput), and a T_BIND_ACK
6510Sstevel@tonic-gate 	 * up from IP (in my rput).
6520Sstevel@tonic-gate 	 */
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	return (0);
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate  * ESP module close routine.
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate static int
6610Sstevel@tonic-gate ipsecesp_close(queue_t *q)
6620Sstevel@tonic-gate {
6633448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
6643448Sdh155122 
6650Sstevel@tonic-gate 	/*
6660Sstevel@tonic-gate 	 * If esp_sadb.s_ip_q is attached to this instance, send a
6670Sstevel@tonic-gate 	 * T_UNBIND_REQ to IP for the instance before doing
6680Sstevel@tonic-gate 	 * a qprocsoff().
6690Sstevel@tonic-gate 	 */
6703448Sdh155122 	if (WR(q) == espstack->esp_sadb.s_ip_q &&
6713448Sdh155122 	    espstack->esp_ip_unbind != NULL) {
6723448Sdh155122 		putnext(WR(q), espstack->esp_ip_unbind);
6733448Sdh155122 		espstack->esp_ip_unbind = NULL;
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/*
6770Sstevel@tonic-gate 	 * Clean up q_ptr, if needed.
6780Sstevel@tonic-gate 	 */
6790Sstevel@tonic-gate 	qprocsoff(q);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	/* Keysock queue check is safe, because of OCEXCL perimeter. */
6820Sstevel@tonic-gate 
6833448Sdh155122 	if (q == espstack->esp_pfkey_q) {
6843448Sdh155122 		esp1dbg(espstack,
6853448Sdh155122 		    ("ipsecesp_close:  Ummm... keysock is closing ESP.\n"));
6863448Sdh155122 		espstack->esp_pfkey_q = NULL;
6870Sstevel@tonic-gate 		/* Detach qtimeouts. */
6883448Sdh155122 		(void) quntimeout(q, espstack->esp_event);
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6913448Sdh155122 	if (WR(q) == espstack->esp_sadb.s_ip_q) {
6920Sstevel@tonic-gate 		/*
6930Sstevel@tonic-gate 		 * If the esp_sadb.s_ip_q is attached to this instance, find
6940Sstevel@tonic-gate 		 * another.  The OCEXCL outer perimeter helps us here.
6950Sstevel@tonic-gate 		 */
6963448Sdh155122 		espstack->esp_sadb.s_ip_q = NULL;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		/*
6990Sstevel@tonic-gate 		 * Find a replacement queue for esp_sadb.s_ip_q.
7000Sstevel@tonic-gate 		 */
7013448Sdh155122 		if (espstack->esp_pfkey_q != NULL &&
7023448Sdh155122 		    espstack->esp_pfkey_q != RD(q)) {
7030Sstevel@tonic-gate 			/*
7040Sstevel@tonic-gate 			 * See if we can use the pfkey_q.
7050Sstevel@tonic-gate 			 */
7063448Sdh155122 			espstack->esp_sadb.s_ip_q = WR(espstack->esp_pfkey_q);
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 
7093448Sdh155122 		if (espstack->esp_sadb.s_ip_q == NULL ||
7103448Sdh155122 		    !sadb_t_bind_req(espstack->esp_sadb.s_ip_q, IPPROTO_ESP)) {
7113448Sdh155122 			esp1dbg(espstack, ("ipsecesp: Can't reassign ip_q.\n"));
7123448Sdh155122 			espstack->esp_sadb.s_ip_q = NULL;
7130Sstevel@tonic-gate 		} else {
7143448Sdh155122 			espstack->esp_ip_unbind =
7153448Sdh155122 			    allocb(sizeof (struct T_unbind_req), BPRI_HI);
7163448Sdh155122 
7173448Sdh155122 			if (espstack->esp_ip_unbind != NULL) {
7180Sstevel@tonic-gate 				struct T_unbind_req *tur;
7190Sstevel@tonic-gate 
7203448Sdh155122 				espstack->esp_ip_unbind->b_datap->db_type =
7213448Sdh155122 				    M_PROTO;
7220Sstevel@tonic-gate 				tur = (struct T_unbind_req *)
7233448Sdh155122 				    espstack->esp_ip_unbind->b_rptr;
7240Sstevel@tonic-gate 				tur->PRIM_type = T_UNBIND_REQ;
7250Sstevel@tonic-gate 			}
7260Sstevel@tonic-gate 			/* If it's NULL, I can't do much here. */
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 
7303448Sdh155122 	netstack_rele(espstack->ipsecesp_netstack);
7310Sstevel@tonic-gate 	return (0);
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate  * Add a number of bytes to what the SA has protected so far.  Return
7360Sstevel@tonic-gate  * B_TRUE if the SA can still protect that many bytes.
7370Sstevel@tonic-gate  *
7380Sstevel@tonic-gate  * Caller must REFRELE the passed-in assoc.  This function must REFRELE
7390Sstevel@tonic-gate  * any obtained peer SA.
7400Sstevel@tonic-gate  */
7410Sstevel@tonic-gate static boolean_t
7420Sstevel@tonic-gate esp_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
7450Sstevel@tonic-gate 	isaf_t *bucket;
7460Sstevel@tonic-gate 	boolean_t inrc, outrc, isv6;
7470Sstevel@tonic-gate 	sadb_t *sp;
7480Sstevel@tonic-gate 	int outhash;
7493448Sdh155122 	netstack_t		*ns = assoc->ipsa_netstack;
7503448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	/* No peer?  No problem! */
7530Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
7543448Sdh155122 		return (sadb_age_bytes(espstack->esp_pfkey_q, assoc, bytes,
7550Sstevel@tonic-gate 		    B_TRUE));
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/*
7590Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
7600Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, two
7610Sstevel@tonic-gate 	 * expire messages may occur.  We limit this by only sending the
7620Sstevel@tonic-gate 	 * expire message on one of the peers, we'll pick the inbound
7630Sstevel@tonic-gate 	 * arbitrarily.
7640Sstevel@tonic-gate 	 *
7650Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
7660Sstevel@tonic-gate 	 * reconsider.
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	/* Use address length to select IPv6/IPv4 */
7700Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
7713448Sdh155122 	sp = isv6 ? &espstack->esp_sadb.s_v6 : &espstack->esp_sadb.s_v4;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (inbound) {
7740Sstevel@tonic-gate 		inassoc = assoc;
7750Sstevel@tonic-gate 		if (isv6) {
776564Ssommerfe 			outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
7770Sstevel@tonic-gate 			    &inassoc->ipsa_dstaddr));
7780Sstevel@tonic-gate 		} else {
779564Ssommerfe 			outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
780*4987Sdanmcd 			    &inassoc->ipsa_dstaddr));
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
7830Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
7840Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
7850Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
7860Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
7870Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
7880Sstevel@tonic-gate 		if (outassoc == NULL) {
7890Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
7900Sstevel@tonic-gate 			esp0dbg(("esp_age_bytes: "
7910Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
7923448Sdh155122 			return (sadb_age_bytes(espstack->esp_pfkey_q, inassoc,
7930Sstevel@tonic-gate 			    bytes, B_TRUE));
7940Sstevel@tonic-gate 		}
7950Sstevel@tonic-gate 	} else {
7960Sstevel@tonic-gate 		outassoc = assoc;
797564Ssommerfe 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
7980Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
7990Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
8000Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
8010Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
8020Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
8030Sstevel@tonic-gate 		if (inassoc == NULL) {
8040Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
8050Sstevel@tonic-gate 			esp0dbg(("esp_age_bytes: "
8060Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
8073448Sdh155122 			return (sadb_age_bytes(espstack->esp_pfkey_q, outassoc,
8080Sstevel@tonic-gate 			    bytes, B_TRUE));
8090Sstevel@tonic-gate 		}
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 
8123448Sdh155122 	inrc = sadb_age_bytes(espstack->esp_pfkey_q, inassoc, bytes, B_TRUE);
8133448Sdh155122 	outrc = sadb_age_bytes(espstack->esp_pfkey_q, outassoc, bytes, B_FALSE);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/*
8160Sstevel@tonic-gate 	 * REFRELE any peer SA.
8170Sstevel@tonic-gate 	 *
8180Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
8190Sstevel@tonic-gate 	 * them in { }.
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	if (inbound) {
8220Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
8230Sstevel@tonic-gate 	} else {
8240Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	return (inrc && outrc);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /*
8310Sstevel@tonic-gate  * Do incoming NAT-T manipulations for packet.
8320Sstevel@tonic-gate  */
8330Sstevel@tonic-gate static ipsec_status_t
8340Sstevel@tonic-gate esp_fix_natt_checksums(mblk_t *data_mp, ipsa_t *assoc)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
8370Sstevel@tonic-gate 	tcpha_t *tcph;
8380Sstevel@tonic-gate 	udpha_t *udpha;
8390Sstevel@tonic-gate 	/* Initialize to our inbound cksum adjustment... */
8400Sstevel@tonic-gate 	uint32_t sum = assoc->ipsa_inbound_cksum;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	switch (ipha->ipha_protocol) {
8430Sstevel@tonic-gate 	case IPPROTO_TCP:
8440Sstevel@tonic-gate 		tcph = (tcpha_t *)(data_mp->b_rptr +
8450Sstevel@tonic-gate 		    IPH_HDR_LENGTH(ipha));
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate #define	DOWN_SUM(x) (x) = ((x) & 0xFFFF) +	 ((x) >> 16)
8480Sstevel@tonic-gate 		sum += ~ntohs(tcph->tha_sum) & 0xFFFF;
8490Sstevel@tonic-gate 		DOWN_SUM(sum);
8500Sstevel@tonic-gate 		DOWN_SUM(sum);
8510Sstevel@tonic-gate 		tcph->tha_sum = ~htons(sum);
8520Sstevel@tonic-gate 		break;
8530Sstevel@tonic-gate 	case IPPROTO_UDP:
8540Sstevel@tonic-gate 		udpha = (udpha_t *)(data_mp->b_rptr + IPH_HDR_LENGTH(ipha));
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		if (udpha->uha_checksum != 0) {
8570Sstevel@tonic-gate 			/* Adujst if the inbound one was not zero. */
8580Sstevel@tonic-gate 			sum += ~ntohs(udpha->uha_checksum) & 0xFFFF;
8590Sstevel@tonic-gate 			DOWN_SUM(sum);
8600Sstevel@tonic-gate 			DOWN_SUM(sum);
8610Sstevel@tonic-gate 			udpha->uha_checksum = ~htons(sum);
8620Sstevel@tonic-gate 			if (udpha->uha_checksum == 0)
8630Sstevel@tonic-gate 				udpha->uha_checksum = 0xFFFF;
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate #undef DOWN_SUM
8660Sstevel@tonic-gate 		break;
8670Sstevel@tonic-gate 	case IPPROTO_IP:
8680Sstevel@tonic-gate 		/*
8690Sstevel@tonic-gate 		 * This case is only an issue for self-encapsulated
8700Sstevel@tonic-gate 		 * packets.  So for now, fall through.
8710Sstevel@tonic-gate 		 */
8720Sstevel@tonic-gate 		break;
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate /*
8793192Sdanmcd  * Strip ESP header, check padding, and fix IP header.
8800Sstevel@tonic-gate  * Returns B_TRUE on success, B_FALSE if an error occured.
8810Sstevel@tonic-gate  */
8820Sstevel@tonic-gate static boolean_t
8830Sstevel@tonic-gate esp_strip_header(mblk_t *data_mp, boolean_t isv4, uint32_t ivlen,
8843448Sdh155122     kstat_named_t **counter, ipsecesp_stack_t *espstack)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	ipha_t *ipha;
8870Sstevel@tonic-gate 	ip6_t *ip6h;
8880Sstevel@tonic-gate 	uint_t divpoint;
8890Sstevel@tonic-gate 	mblk_t *scratch;
8900Sstevel@tonic-gate 	uint8_t nexthdr, padlen;
8910Sstevel@tonic-gate 	uint8_t lastpad;
8923448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
8933192Sdanmcd 	uint8_t *lastbyte;
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * Strip ESP data and fix IP header.
8970Sstevel@tonic-gate 	 *
8980Sstevel@tonic-gate 	 * XXX In case the beginning of esp_inbound() changes to not do a
8990Sstevel@tonic-gate 	 * pullup, this part of the code can remain unchanged.
9000Sstevel@tonic-gate 	 */
9010Sstevel@tonic-gate 	if (isv4) {
9020Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ipha_t));
9030Sstevel@tonic-gate 		ipha = (ipha_t *)data_mp->b_rptr;
9040Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (esph_t) +
9050Sstevel@tonic-gate 		    IPH_HDR_LENGTH(ipha));
9060Sstevel@tonic-gate 		divpoint = IPH_HDR_LENGTH(ipha);
9070Sstevel@tonic-gate 	} else {
9080Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ip6_t));
9090Sstevel@tonic-gate 		ip6h = (ip6_t *)data_mp->b_rptr;
9100Sstevel@tonic-gate 		divpoint = ip_hdr_length_v6(data_mp, ip6h);
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	scratch = data_mp;
9140Sstevel@tonic-gate 	while (scratch->b_cont != NULL)
9150Sstevel@tonic-gate 		scratch = scratch->b_cont;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	ASSERT((scratch->b_wptr - scratch->b_rptr) >= 3);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/*
9200Sstevel@tonic-gate 	 * "Next header" and padding length are the last two bytes in the
9210Sstevel@tonic-gate 	 * ESP-protected datagram, thus the explicit - 1 and - 2.
9220Sstevel@tonic-gate 	 * lastpad is the last byte of the padding, which can be used for
9230Sstevel@tonic-gate 	 * a quick check to see if the padding is correct.
9240Sstevel@tonic-gate 	 */
9253192Sdanmcd 	lastbyte = scratch->b_wptr - 1;
9263192Sdanmcd 	nexthdr = *lastbyte--;
9273192Sdanmcd 	padlen = *lastbyte--;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if (isv4) {
9300Sstevel@tonic-gate 		/* Fix part of the IP header. */
9310Sstevel@tonic-gate 		ipha->ipha_protocol = nexthdr;
9320Sstevel@tonic-gate 		/*
9330Sstevel@tonic-gate 		 * Reality check the padlen.  The explicit - 2 is for the
9340Sstevel@tonic-gate 		 * padding length and the next-header bytes.
9350Sstevel@tonic-gate 		 */
9360Sstevel@tonic-gate 		if (padlen >= ntohs(ipha->ipha_length) - sizeof (ipha_t) - 2 -
9370Sstevel@tonic-gate 		    sizeof (esph_t) - ivlen) {
9383448Sdh155122 			ESP_BUMP_STAT(espstack, bad_decrypt);
9393448Sdh155122 			ipsec_rl_strlog(espstack->ipsecesp_netstack,
9403448Sdh155122 			    info.mi_idnum, 0, 0,
9413448Sdh155122 			    SL_ERROR | SL_WARN,
9423192Sdanmcd 			    "Corrupt ESP packet (padlen too big).\n");
9433448Sdh155122 			esp1dbg(espstack, ("padlen (%d) is greater than:\n",
944*4987Sdanmcd 			    padlen));
9453448Sdh155122 			esp1dbg(espstack, ("pkt len(%d) - ip hdr - esp "
9463448Sdh155122 			    "hdr - ivlen(%d) = %d.\n",
9473448Sdh155122 			    ntohs(ipha->ipha_length), ivlen,
9480Sstevel@tonic-gate 			    (int)(ntohs(ipha->ipha_length) - sizeof (ipha_t) -
9493448Sdh155122 			    2 - sizeof (esph_t) - ivlen)));
9503448Sdh155122 			*counter = DROPPER(ipss, ipds_esp_bad_padlen);
9510Sstevel@tonic-gate 			return (B_FALSE);
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		/*
9550Sstevel@tonic-gate 		 * Fix the rest of the header.  The explicit - 2 is for the
9560Sstevel@tonic-gate 		 * padding length and the next-header bytes.
9570Sstevel@tonic-gate 		 */
9580Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) - padlen -
9590Sstevel@tonic-gate 		    2 - sizeof (esph_t) - ivlen);
9600Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = 0;
9610Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
9620Sstevel@tonic-gate 	} else {
9630Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_ESP) {
9640Sstevel@tonic-gate 			ip6h->ip6_nxt = nexthdr;
9650Sstevel@tonic-gate 		} else {
9660Sstevel@tonic-gate 			ip6_pkt_t ipp;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 			bzero(&ipp, sizeof (ipp));
9690Sstevel@tonic-gate 			(void) ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL);
9700Sstevel@tonic-gate 			if (ipp.ipp_dstopts != NULL) {
9710Sstevel@tonic-gate 				ipp.ipp_dstopts->ip6d_nxt = nexthdr;
9720Sstevel@tonic-gate 			} else if (ipp.ipp_rthdr != NULL) {
9730Sstevel@tonic-gate 				ipp.ipp_rthdr->ip6r_nxt = nexthdr;
9740Sstevel@tonic-gate 			} else if (ipp.ipp_hopopts != NULL) {
9750Sstevel@tonic-gate 				ipp.ipp_hopopts->ip6h_nxt = nexthdr;
9760Sstevel@tonic-gate 			} else {
9770Sstevel@tonic-gate 				/* Panic a DEBUG kernel. */
9780Sstevel@tonic-gate 				ASSERT(ipp.ipp_hopopts != NULL);
9790Sstevel@tonic-gate 				/* Otherwise, pretend it's IP + ESP. */
9800Sstevel@tonic-gate 				cmn_err(CE_WARN, "ESP IPv6 headers wrong.\n");
9810Sstevel@tonic-gate 				ip6h->ip6_nxt = nexthdr;
9820Sstevel@tonic-gate 			}
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		if (padlen >= ntohs(ip6h->ip6_plen) - 2 - sizeof (esph_t) -
9860Sstevel@tonic-gate 		    ivlen) {
9873448Sdh155122 			ESP_BUMP_STAT(espstack, bad_decrypt);
9883448Sdh155122 			ipsec_rl_strlog(espstack->ipsecesp_netstack,
9893448Sdh155122 			    info.mi_idnum, 0, 0,
9903448Sdh155122 			    SL_ERROR | SL_WARN,
9913192Sdanmcd 			    "Corrupt ESP packet (v6 padlen too big).\n");
9923448Sdh155122 			esp1dbg(espstack, ("padlen (%d) is greater than:\n",
993*4987Sdanmcd 			    padlen));
994*4987Sdanmcd 			esp1dbg(espstack,
995*4987Sdanmcd 			    ("pkt len(%u) - ip hdr - esp hdr - ivlen(%d) = "
996*4987Sdanmcd 			    "%u.\n", (unsigned)(ntohs(ip6h->ip6_plen)
997*4987Sdanmcd 			    + sizeof (ip6_t)), ivlen,
998*4987Sdanmcd 			    (unsigned)(ntohs(ip6h->ip6_plen) - 2 -
999*4987Sdanmcd 			    sizeof (esph_t) - ivlen)));
10003448Sdh155122 			*counter = DROPPER(ipss, ipds_esp_bad_padlen);
10010Sstevel@tonic-gate 			return (B_FALSE);
10020Sstevel@tonic-gate 		}
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		/*
10060Sstevel@tonic-gate 		 * Fix the rest of the header.  The explicit - 2 is for the
10070Sstevel@tonic-gate 		 * padding length and the next-header bytes.  IPv6 is nice,
10080Sstevel@tonic-gate 		 * because there's no hdr checksum!
10090Sstevel@tonic-gate 		 */
10100Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - padlen -
10110Sstevel@tonic-gate 		    2 - sizeof (esph_t) - ivlen);
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 
10143448Sdh155122 	if (espstack->ipsecesp_padding_check > 0 && padlen > 0) {
10153192Sdanmcd 		/*
10163192Sdanmcd 		 * Weak padding check: compare last-byte to length, they
10173192Sdanmcd 		 * should be equal.
10183192Sdanmcd 		 */
10193192Sdanmcd 		lastpad = *lastbyte--;
10203192Sdanmcd 
10213192Sdanmcd 		if (padlen != lastpad) {
10223448Sdh155122 			ipsec_rl_strlog(espstack->ipsecesp_netstack,
10233448Sdh155122 			    info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
10243192Sdanmcd 			    "Corrupt ESP packet (lastpad != padlen).\n");
10253448Sdh155122 			esp1dbg(espstack,
10263448Sdh155122 			    ("lastpad (%d) not equal to padlen (%d):\n",
10273448Sdh155122 			    lastpad, padlen));
10283448Sdh155122 			ESP_BUMP_STAT(espstack, bad_padding);
10293448Sdh155122 			*counter = DROPPER(ipss, ipds_esp_bad_padding);
10303192Sdanmcd 			return (B_FALSE);
10313192Sdanmcd 		}
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 		/*
10343192Sdanmcd 		 * Strong padding check: Check all pad bytes to see that
10353192Sdanmcd 		 * they're ascending.  Go backwards using a descending counter
10363192Sdanmcd 		 * to verify.  padlen == 1 is checked by previous block, so
10373192Sdanmcd 		 * only bother if we've more than 1 byte of padding.
10383192Sdanmcd 		 * Consequently, start the check one byte before the location
10393192Sdanmcd 		 * of "lastpad".
10400Sstevel@tonic-gate 		 */
10413448Sdh155122 		if (espstack->ipsecesp_padding_check > 1) {
10423192Sdanmcd 			/*
10433192Sdanmcd 			 * This assert may have to become an if and a pullup
10443192Sdanmcd 			 * if we start accepting multi-dblk mblks. For now,
10453192Sdanmcd 			 * though, any packet here will have been pulled up in
10463192Sdanmcd 			 * esp_inbound.
10473192Sdanmcd 			 */
10483192Sdanmcd 			ASSERT(MBLKL(scratch) >= lastpad + 3);
10493192Sdanmcd 
10503192Sdanmcd 			/*
10513192Sdanmcd 			 * Use "--lastpad" because we already checked the very
10523192Sdanmcd 			 * last pad byte previously.
10533192Sdanmcd 			 */
10543192Sdanmcd 			while (--lastpad != 0) {
10553192Sdanmcd 				if (lastpad != *lastbyte) {
10563448Sdh155122 					ipsec_rl_strlog(
10573448Sdh155122 					    espstack->ipsecesp_netstack,
10583448Sdh155122 					    info.mi_idnum, 0, 0,
10593192Sdanmcd 					    SL_ERROR | SL_WARN, "Corrupt ESP "
10603192Sdanmcd 					    "packet (bad padding).\n");
10613448Sdh155122 					esp1dbg(espstack,
10623448Sdh155122 					    ("padding not in correct"
10633448Sdh155122 					    " format:\n"));
10643448Sdh155122 					ESP_BUMP_STAT(espstack, bad_padding);
10653448Sdh155122 					*counter = DROPPER(ipss,
10663448Sdh155122 					    ipds_esp_bad_padding);
10673192Sdanmcd 					return (B_FALSE);
10683192Sdanmcd 				}
10693192Sdanmcd 				lastbyte--;
10700Sstevel@tonic-gate 			}
10710Sstevel@tonic-gate 		}
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	/* Trim off the padding. */
10750Sstevel@tonic-gate 	ASSERT(data_mp->b_cont == NULL);
10760Sstevel@tonic-gate 	data_mp->b_wptr -= (padlen + 2);
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	/*
10790Sstevel@tonic-gate 	 * Remove the ESP header.
10800Sstevel@tonic-gate 	 *
10810Sstevel@tonic-gate 	 * The above assertions about data_mp's size will make this work.
10820Sstevel@tonic-gate 	 *
10830Sstevel@tonic-gate 	 * XXX  Question:  If I send up and get back a contiguous mblk,
10840Sstevel@tonic-gate 	 * would it be quicker to bcopy over, or keep doing the dupb stuff?
10850Sstevel@tonic-gate 	 * I go with copying for now.
10860Sstevel@tonic-gate 	 */
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	if (IS_P2ALIGNED(data_mp->b_rptr, sizeof (uint32_t)) &&
10890Sstevel@tonic-gate 	    IS_P2ALIGNED(ivlen, sizeof (uint32_t))) {
10900Sstevel@tonic-gate 		uint8_t *start = data_mp->b_rptr;
10910Sstevel@tonic-gate 		uint32_t *src, *dst;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		src = (uint32_t *)(start + divpoint);
10940Sstevel@tonic-gate 		dst = (uint32_t *)(start + divpoint + sizeof (esph_t) + ivlen);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(dst, sizeof (uint32_t)) &&
10970Sstevel@tonic-gate 		    IS_P2ALIGNED(src, sizeof (uint32_t)));
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 		do {
11000Sstevel@tonic-gate 			src--;
11010Sstevel@tonic-gate 			dst--;
11020Sstevel@tonic-gate 			*dst = *src;
11030Sstevel@tonic-gate 		} while (src != (uint32_t *)start);
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		data_mp->b_rptr = (uchar_t *)dst;
11060Sstevel@tonic-gate 	} else {
11070Sstevel@tonic-gate 		uint8_t *start = data_mp->b_rptr;
11080Sstevel@tonic-gate 		uint8_t *src, *dst;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 		src = start + divpoint;
11110Sstevel@tonic-gate 		dst = src + sizeof (esph_t) + ivlen;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 		do {
11140Sstevel@tonic-gate 			src--;
11150Sstevel@tonic-gate 			dst--;
11160Sstevel@tonic-gate 			*dst = *src;
11170Sstevel@tonic-gate 		} while (src != start);
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 		data_mp->b_rptr = dst;
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 
11223448Sdh155122 	esp2dbg(espstack, ("data_mp after inbound ESP adjustment:\n"));
11233448Sdh155122 	esp2dbg(espstack, (dump_msg(data_mp)));
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	return (B_TRUE);
11260Sstevel@tonic-gate }
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate /*
11290Sstevel@tonic-gate  * Updating use times can be tricky business if the ipsa_haspeer flag is
11300Sstevel@tonic-gate  * set.  This function is called once in an SA's lifetime.
11310Sstevel@tonic-gate  *
11320Sstevel@tonic-gate  * Caller has to REFRELE "assoc" which is passed in.  This function has
11330Sstevel@tonic-gate  * to REFRELE any peer SA that is obtained.
11340Sstevel@tonic-gate  */
11350Sstevel@tonic-gate static void
11360Sstevel@tonic-gate esp_set_usetime(ipsa_t *assoc, boolean_t inbound)
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
11390Sstevel@tonic-gate 	isaf_t *bucket;
11400Sstevel@tonic-gate 	sadb_t *sp;
11410Sstevel@tonic-gate 	int outhash;
11420Sstevel@tonic-gate 	boolean_t isv6;
11433448Sdh155122 	netstack_t		*ns = assoc->ipsa_netstack;
11443448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/* No peer?  No problem! */
11470Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
11480Sstevel@tonic-gate 		sadb_set_usetime(assoc);
11490Sstevel@tonic-gate 		return;
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/*
11530Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
11540Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, the times
11550Sstevel@tonic-gate 	 * will be out-of-synch by at most a second, and since our time
11560Sstevel@tonic-gate 	 * granularity is a second, this won't be a problem.
11570Sstevel@tonic-gate 	 *
11580Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
11590Sstevel@tonic-gate 	 * reconsider.
11600Sstevel@tonic-gate 	 */
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	/* Use address length to select IPv6/IPv4 */
11630Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
11643448Sdh155122 	sp = isv6 ? &espstack->esp_sadb.s_v6 : &espstack->esp_sadb.s_v4;
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (inbound) {
11670Sstevel@tonic-gate 		inassoc = assoc;
11680Sstevel@tonic-gate 		if (isv6) {
1169564Ssommerfe 			outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
11700Sstevel@tonic-gate 			    &inassoc->ipsa_dstaddr));
11710Sstevel@tonic-gate 		} else {
1172564Ssommerfe 			outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
1173*4987Sdanmcd 			    &inassoc->ipsa_dstaddr));
11740Sstevel@tonic-gate 		}
11750Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
11760Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
11770Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
11780Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
11790Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
11800Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
11810Sstevel@tonic-gate 		if (outassoc == NULL) {
11820Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
11830Sstevel@tonic-gate 			esp0dbg(("esp_set_usetime: "
11840Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
11850Sstevel@tonic-gate 			sadb_set_usetime(inassoc);
11860Sstevel@tonic-gate 			return;
11870Sstevel@tonic-gate 		}
11880Sstevel@tonic-gate 	} else {
11890Sstevel@tonic-gate 		outassoc = assoc;
1190564Ssommerfe 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
11910Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
11920Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
11930Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
11940Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
11950Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
11960Sstevel@tonic-gate 		if (inassoc == NULL) {
11970Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
11980Sstevel@tonic-gate 			esp0dbg(("esp_set_usetime: "
11990Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
12000Sstevel@tonic-gate 			sadb_set_usetime(outassoc);
12010Sstevel@tonic-gate 			return;
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	/* Update usetime on both. */
12060Sstevel@tonic-gate 	sadb_set_usetime(inassoc);
12070Sstevel@tonic-gate 	sadb_set_usetime(outassoc);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	/*
12100Sstevel@tonic-gate 	 * REFRELE any peer SA.
12110Sstevel@tonic-gate 	 *
12120Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
12130Sstevel@tonic-gate 	 * them in { }.
12140Sstevel@tonic-gate 	 */
12150Sstevel@tonic-gate 	if (inbound) {
12160Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
12170Sstevel@tonic-gate 	} else {
12180Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate /*
12230Sstevel@tonic-gate  * Handle ESP inbound data for IPv4 and IPv6.
12240Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
12250Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
12260Sstevel@tonic-gate  */
12270Sstevel@tonic-gate ipsec_status_t
12280Sstevel@tonic-gate esp_inbound(mblk_t *ipsec_in_mp, void *arg)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	mblk_t *data_mp = ipsec_in_mp->b_cont;
12310Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
12320Sstevel@tonic-gate 	esph_t *esph = (esph_t *)arg;
12330Sstevel@tonic-gate 	ipsa_t *ipsa = ii->ipsec_in_esp_sa;
12343448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
12353448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
12363448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	/*
12390Sstevel@tonic-gate 	 * We may wish to check replay in-range-only here as an optimization.
12400Sstevel@tonic-gate 	 * Include the reality check of ipsa->ipsa_replay >
12410Sstevel@tonic-gate 	 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
12420Sstevel@tonic-gate 	 * where N == ipsa->ipsa_replay_wsize.
12430Sstevel@tonic-gate 	 *
12440Sstevel@tonic-gate 	 * Another check that may come here later is the "collision" check.
12450Sstevel@tonic-gate 	 * If legitimate packets flow quickly enough, this won't be a problem,
12460Sstevel@tonic-gate 	 * but collisions may cause authentication algorithm crunching to
12470Sstevel@tonic-gate 	 * take place when it doesn't need to.
12480Sstevel@tonic-gate 	 */
12490Sstevel@tonic-gate 	if (!sadb_replay_peek(ipsa, esph->esph_replay)) {
12503448Sdh155122 		ESP_BUMP_STAT(espstack, replay_early_failures);
12513448Sdh155122 		IP_ESP_BUMP_STAT(ipss, in_discards);
12520Sstevel@tonic-gate 		/*
12530Sstevel@tonic-gate 		 * TODO: Extract inbound interface from the IPSEC_IN
12540Sstevel@tonic-gate 		 * message's ii->ipsec_in_rill_index.
12550Sstevel@tonic-gate 		 */
12560Sstevel@tonic-gate 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
12573448Sdh155122 		    DROPPER(ipss, ipds_esp_early_replay),
12583448Sdh155122 		    &espstack->esp_dropper);
12590Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
12600Sstevel@tonic-gate 	}
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	/*
12630Sstevel@tonic-gate 	 * Has this packet already been processed by a hardware
12640Sstevel@tonic-gate 	 * IPsec accelerator?
12650Sstevel@tonic-gate 	 */
12660Sstevel@tonic-gate 	if (ii->ipsec_in_accelerated) {
12670Sstevel@tonic-gate 		ipsec_status_t rv;
12683448Sdh155122 		esp3dbg(espstack,
12693448Sdh155122 		    ("esp_inbound: pkt processed by ill=%d isv6=%d\n",
12700Sstevel@tonic-gate 		    ii->ipsec_in_ill_index, !ii->ipsec_in_v4));
12710Sstevel@tonic-gate 		rv = esp_inbound_accelerated(ipsec_in_mp,
12720Sstevel@tonic-gate 		    data_mp, ii->ipsec_in_v4, ipsa);
12730Sstevel@tonic-gate 		return (rv);
12740Sstevel@tonic-gate 	}
12753448Sdh155122 	ESP_BUMP_STAT(espstack, noaccel);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	/*
12780Sstevel@tonic-gate 	 * Adjust the IP header's payload length to reflect the removal
12790Sstevel@tonic-gate 	 * of the ICV.
12800Sstevel@tonic-gate 	 */
12810Sstevel@tonic-gate 	if (!ii->ipsec_in_v4) {
12820Sstevel@tonic-gate 		ip6_t *ip6h = (ip6_t *)data_mp->b_rptr;
12830Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) -
12840Sstevel@tonic-gate 		    ipsa->ipsa_mac_len);
12850Sstevel@tonic-gate 	} else {
12860Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
12870Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) -
12880Sstevel@tonic-gate 		    ipsa->ipsa_mac_len);
12890Sstevel@tonic-gate 	}
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	/* submit the request to the crypto framework */
12920Sstevel@tonic-gate 	return (esp_submit_req_inbound(ipsec_in_mp, ipsa,
12930Sstevel@tonic-gate 	    (uint8_t *)esph - data_mp->b_rptr));
12940Sstevel@tonic-gate }
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate /*
12970Sstevel@tonic-gate  * Perform the really difficult work of inserting the proposed situation.
12980Sstevel@tonic-gate  * Called while holding the algorithm lock.
12990Sstevel@tonic-gate  */
13000Sstevel@tonic-gate static void
13010Sstevel@tonic-gate esp_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs)
13020Sstevel@tonic-gate {
13030Sstevel@tonic-gate 	sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
13040Sstevel@tonic-gate 	ipsec_out_t *io;
13050Sstevel@tonic-gate 	ipsec_action_t *ap;
13060Sstevel@tonic-gate 	ipsec_prot_t *prot;
13073448Sdh155122 	netstack_t *ns;
13083448Sdh155122 	ipsecesp_stack_t *espstack;
13093448Sdh155122 	ipsec_stack_t *ipss;
13103448Sdh155122 
13110Sstevel@tonic-gate 	io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr;
13120Sstevel@tonic-gate 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
13133448Sdh155122 	ns = io->ipsec_out_ns;
13143448Sdh155122 	espstack = ns->netstack_ipsecesp;
13153448Sdh155122 	ipss = ns->netstack_ipsec;
13163448Sdh155122 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
13190Sstevel@tonic-gate 	prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
13200Sstevel@tonic-gate 	*(uint32_t *)(&prop->sadb_prop_replay) = 0;	/* Quick zero-out! */
13210Sstevel@tonic-gate 
13223448Sdh155122 	prop->sadb_prop_replay = espstack->ipsecesp_replay_size;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/*
13250Sstevel@tonic-gate 	 * Based upon algorithm properties, and what-not, prioritize
13260Sstevel@tonic-gate 	 * a proposal.  If the IPSEC_OUT message has an algorithm specified,
13270Sstevel@tonic-gate 	 * use it first and foremost.
13280Sstevel@tonic-gate 	 *
13290Sstevel@tonic-gate 	 * For each action in policy list
13300Sstevel@tonic-gate 	 *   Add combination.  If I've hit limit, return.
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	for (ap = acqrec->ipsacq_act; ap != NULL;
13340Sstevel@tonic-gate 	    ap = ap->ipa_next) {
13350Sstevel@tonic-gate 		ipsec_alginfo_t *ealg = NULL;
13360Sstevel@tonic-gate 		ipsec_alginfo_t *aalg = NULL;
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 		if (ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY)
13390Sstevel@tonic-gate 			continue;
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		prot = &ap->ipa_act.ipa_apply;
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 		if (!(prot->ipp_use_esp))
13440Sstevel@tonic-gate 			continue;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 		if (prot->ipp_esp_auth_alg != 0) {
13473448Sdh155122 			aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
13480Sstevel@tonic-gate 			    [prot->ipp_esp_auth_alg];
13490Sstevel@tonic-gate 			if (aalg == NULL || !ALG_VALID(aalg))
13500Sstevel@tonic-gate 				continue;
13510Sstevel@tonic-gate 		}
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 		ASSERT(prot->ipp_encr_alg > 0);
13543448Sdh155122 		ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR]
13553448Sdh155122 		    [prot->ipp_encr_alg];
13560Sstevel@tonic-gate 		if (ealg == NULL || !ALG_VALID(ealg))
13570Sstevel@tonic-gate 			continue;
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 		comb->sadb_comb_flags = 0;
13600Sstevel@tonic-gate 		comb->sadb_comb_reserved = 0;
13610Sstevel@tonic-gate 		comb->sadb_comb_encrypt = ealg->alg_id;
13622751Sdanmcd 		comb->sadb_comb_encrypt_minbits =
13632751Sdanmcd 		    MAX(prot->ipp_espe_minbits, ealg->alg_ef_minbits);
13642751Sdanmcd 		comb->sadb_comb_encrypt_maxbits =
13652751Sdanmcd 		    MIN(prot->ipp_espe_maxbits, ealg->alg_ef_maxbits);
13660Sstevel@tonic-gate 		if (aalg == NULL) {
13670Sstevel@tonic-gate 			comb->sadb_comb_auth = 0;
13680Sstevel@tonic-gate 			comb->sadb_comb_auth_minbits = 0;
13690Sstevel@tonic-gate 			comb->sadb_comb_auth_maxbits = 0;
13700Sstevel@tonic-gate 		} else {
13710Sstevel@tonic-gate 			comb->sadb_comb_auth = aalg->alg_id;
13722751Sdanmcd 			comb->sadb_comb_auth_minbits =
13732751Sdanmcd 			    MAX(prot->ipp_espa_minbits, aalg->alg_ef_minbits);
13742751Sdanmcd 			comb->sadb_comb_auth_maxbits =
13752751Sdanmcd 			    MIN(prot->ipp_espa_maxbits, aalg->alg_ef_maxbits);
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 		/*
13790Sstevel@tonic-gate 		 * The following may be based on algorithm
13800Sstevel@tonic-gate 		 * properties, but in the meantime, we just pick
13810Sstevel@tonic-gate 		 * some good, sensible numbers.  Key mgmt. can
13820Sstevel@tonic-gate 		 * (and perhaps should) be the place to finalize
13830Sstevel@tonic-gate 		 * such decisions.
13840Sstevel@tonic-gate 		 */
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		/*
13870Sstevel@tonic-gate 		 * No limits on allocations, since we really don't
13880Sstevel@tonic-gate 		 * support that concept currently.
13890Sstevel@tonic-gate 		 */
13900Sstevel@tonic-gate 		comb->sadb_comb_soft_allocations = 0;
13910Sstevel@tonic-gate 		comb->sadb_comb_hard_allocations = 0;
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 		/*
13940Sstevel@tonic-gate 		 * These may want to come from policy rule..
13950Sstevel@tonic-gate 		 */
13963448Sdh155122 		comb->sadb_comb_soft_bytes =
13973448Sdh155122 		    espstack->ipsecesp_default_soft_bytes;
13983448Sdh155122 		comb->sadb_comb_hard_bytes =
13993448Sdh155122 		    espstack->ipsecesp_default_hard_bytes;
14003448Sdh155122 		comb->sadb_comb_soft_addtime =
14013448Sdh155122 		    espstack->ipsecesp_default_soft_addtime;
14023448Sdh155122 		comb->sadb_comb_hard_addtime =
14033448Sdh155122 		    espstack->ipsecesp_default_hard_addtime;
14043448Sdh155122 		comb->sadb_comb_soft_usetime =
14053448Sdh155122 		    espstack->ipsecesp_default_soft_usetime;
14063448Sdh155122 		comb->sadb_comb_hard_usetime =
14073448Sdh155122 		    espstack->ipsecesp_default_hard_usetime;
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
14100Sstevel@tonic-gate 		if (--combs == 0)
14110Sstevel@tonic-gate 			break;	/* out of space.. */
14120Sstevel@tonic-gate 		comb++;
14130Sstevel@tonic-gate 	}
14140Sstevel@tonic-gate }
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate /*
14170Sstevel@tonic-gate  * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
14180Sstevel@tonic-gate  */
14190Sstevel@tonic-gate static void
14203448Sdh155122 esp_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns)
14210Sstevel@tonic-gate {
14223055Sdanmcd 	uint_t combs;
14230Sstevel@tonic-gate 	sadb_msg_t *samsg;
14240Sstevel@tonic-gate 	sadb_prop_t *prop;
14253055Sdanmcd 	mblk_t *pfkeymp, *msgmp;
14263448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
14273448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
14283448Sdh155122 
14293448Sdh155122 	ESP_BUMP_STAT(espstack, acquire_requests);
14303448Sdh155122 
14313448Sdh155122 	if (espstack->esp_pfkey_q == NULL)
14323055Sdanmcd 		return;
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	/* Set up ACQUIRE. */
14353448Sdh155122 	pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_ESP,
14363448Sdh155122 	    ns->netstack_ipsec);
14373055Sdanmcd 	if (pfkeymp == NULL) {
14380Sstevel@tonic-gate 		esp0dbg(("sadb_setup_acquire failed.\n"));
14393055Sdanmcd 		return;
14400Sstevel@tonic-gate 	}
14413448Sdh155122 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
14423448Sdh155122 	combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] *
14433448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
14443055Sdanmcd 	msgmp = pfkeymp->b_cont;
14453055Sdanmcd 	samsg = (sadb_msg_t *)(msgmp->b_rptr);
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	/* Insert proposal here. */
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
14500Sstevel@tonic-gate 	esp_insert_prop(prop, acqrec, combs);
14510Sstevel@tonic-gate 	samsg->sadb_msg_len += prop->sadb_prop_len;
14520Sstevel@tonic-gate 	msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
14530Sstevel@tonic-gate 
14543448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	/*
14570Sstevel@tonic-gate 	 * Must mutex_exit() before sending PF_KEY message up, in
14580Sstevel@tonic-gate 	 * order to avoid recursive mutex_enter() if there are no registered
14590Sstevel@tonic-gate 	 * listeners.
14600Sstevel@tonic-gate 	 *
14610Sstevel@tonic-gate 	 * Once I've sent the message, I'm cool anyway.
14620Sstevel@tonic-gate 	 */
14630Sstevel@tonic-gate 	mutex_exit(&acqrec->ipsacq_lock);
14643055Sdanmcd 	if (extended != NULL) {
14653448Sdh155122 		putnext(espstack->esp_pfkey_q, extended);
14660Sstevel@tonic-gate 	}
14673448Sdh155122 	putnext(espstack->esp_pfkey_q, pfkeymp);
14680Sstevel@tonic-gate }
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate /*
14710Sstevel@tonic-gate  * Handle the SADB_GETSPI message.  Create a larval SA.
14720Sstevel@tonic-gate  */
14730Sstevel@tonic-gate static void
14743448Sdh155122 esp_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecesp_stack_t *espstack)
14750Sstevel@tonic-gate {
14760Sstevel@tonic-gate 	ipsa_t *newbie, *target;
14770Sstevel@tonic-gate 	isaf_t *outbound, *inbound;
14780Sstevel@tonic-gate 	int rc, diagnostic;
14790Sstevel@tonic-gate 	sadb_sa_t *assoc;
14800Sstevel@tonic-gate 	keysock_out_t *kso;
14810Sstevel@tonic-gate 	uint32_t newspi;
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	/*
14840Sstevel@tonic-gate 	 * Randomly generate a proposed SPI value
14850Sstevel@tonic-gate 	 */
14860Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t));
14873448Sdh155122 	newbie = sadb_getspi(ksi, newspi, &diagnostic,
14883448Sdh155122 	    espstack->ipsecesp_netstack);
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	if (newbie == NULL) {
14913448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, ENOMEM, diagnostic,
14920Sstevel@tonic-gate 		    ksi->ks_in_serial);
14930Sstevel@tonic-gate 		return;
14940Sstevel@tonic-gate 	} else if (newbie == (ipsa_t *)-1) {
14953448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL, diagnostic,
14960Sstevel@tonic-gate 		    ksi->ks_in_serial);
14970Sstevel@tonic-gate 		return;
14980Sstevel@tonic-gate 	}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	/*
15010Sstevel@tonic-gate 	 * XXX - We may randomly collide.  We really should recover from this.
15020Sstevel@tonic-gate 	 *	 Unfortunately, that could require spending way-too-much-time
15030Sstevel@tonic-gate 	 *	 in here.  For now, let the user retry.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	if (newbie->ipsa_addrfam == AF_INET6) {
15073448Sdh155122 		outbound = OUTBOUND_BUCKET_V6(&espstack->esp_sadb.s_v6,
1508564Ssommerfe 		    *(uint32_t *)(newbie->ipsa_dstaddr));
15093448Sdh155122 		inbound = INBOUND_BUCKET(&espstack->esp_sadb.s_v6,
15103448Sdh155122 		    newbie->ipsa_spi);
15110Sstevel@tonic-gate 	} else {
15120Sstevel@tonic-gate 		ASSERT(newbie->ipsa_addrfam == AF_INET);
15133448Sdh155122 		outbound = OUTBOUND_BUCKET_V4(&espstack->esp_sadb.s_v4,
1514564Ssommerfe 		    *(uint32_t *)(newbie->ipsa_dstaddr));
15153448Sdh155122 		inbound = INBOUND_BUCKET(&espstack->esp_sadb.s_v4,
15163448Sdh155122 		    newbie->ipsa_spi);
15170Sstevel@tonic-gate 	}
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	mutex_enter(&outbound->isaf_lock);
15200Sstevel@tonic-gate 	mutex_enter(&inbound->isaf_lock);
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15230Sstevel@tonic-gate 	 * Check for collisions (i.e. did sadb_getspi() return with something
15240Sstevel@tonic-gate 	 * that already exists?).
15250Sstevel@tonic-gate 	 *
15260Sstevel@tonic-gate 	 * Try outbound first.  Even though SADB_GETSPI is traditionally
15270Sstevel@tonic-gate 	 * for inbound SAs, you never know what a user might do.
15280Sstevel@tonic-gate 	 */
15290Sstevel@tonic-gate 	target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
15300Sstevel@tonic-gate 	    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
15310Sstevel@tonic-gate 	if (target == NULL) {
15320Sstevel@tonic-gate 		target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
15330Sstevel@tonic-gate 		    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
15340Sstevel@tonic-gate 		    newbie->ipsa_addrfam);
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	/*
15380Sstevel@tonic-gate 	 * I don't have collisions elsewhere!
15390Sstevel@tonic-gate 	 * (Nor will I because I'm still holding inbound/outbound locks.)
15400Sstevel@tonic-gate 	 */
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	if (target != NULL) {
15430Sstevel@tonic-gate 		rc = EEXIST;
15440Sstevel@tonic-gate 		IPSA_REFRELE(target);
15450Sstevel@tonic-gate 	} else {
15460Sstevel@tonic-gate 		/*
15470Sstevel@tonic-gate 		 * sadb_insertassoc() also checks for collisions, so
15480Sstevel@tonic-gate 		 * if there's a colliding entry, rc will be set
15490Sstevel@tonic-gate 		 * to EEXIST.
15500Sstevel@tonic-gate 		 */
15510Sstevel@tonic-gate 		rc = sadb_insertassoc(newbie, inbound);
1552*4987Sdanmcd 		newbie->ipsa_hardexpiretime = gethrestime_sec();
15533448Sdh155122 		newbie->ipsa_hardexpiretime +=
15543448Sdh155122 		    espstack->ipsecesp_larval_timeout;
15550Sstevel@tonic-gate 	}
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	/*
15580Sstevel@tonic-gate 	 * Can exit outbound mutex.  Hold inbound until we're done
15590Sstevel@tonic-gate 	 * with newbie.
15600Sstevel@tonic-gate 	 */
15610Sstevel@tonic-gate 	mutex_exit(&outbound->isaf_lock);
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	if (rc != 0) {
15640Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
15650Sstevel@tonic-gate 		IPSA_REFRELE(newbie);
15663448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, rc,
15673448Sdh155122 		    SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
15680Sstevel@tonic-gate 		return;
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/* Can write here because I'm still holding the bucket lock. */
15730Sstevel@tonic-gate 	newbie->ipsa_type = SADB_SATYPE_ESP;
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	/*
15760Sstevel@tonic-gate 	 * Construct successful return message.  We have one thing going
15770Sstevel@tonic-gate 	 * for us in PF_KEY v2.  That's the fact that
15780Sstevel@tonic-gate 	 *	sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
15790Sstevel@tonic-gate 	 */
15800Sstevel@tonic-gate 	assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
15810Sstevel@tonic-gate 	assoc->sadb_sa_exttype = SADB_EXT_SA;
15820Sstevel@tonic-gate 	assoc->sadb_sa_spi = newbie->ipsa_spi;
15830Sstevel@tonic-gate 	*((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
15840Sstevel@tonic-gate 	mutex_exit(&inbound->isaf_lock);
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	/* Convert KEYSOCK_IN to KEYSOCK_OUT. */
15870Sstevel@tonic-gate 	kso = (keysock_out_t *)ksi;
15880Sstevel@tonic-gate 	kso->ks_out_len = sizeof (*kso);
15890Sstevel@tonic-gate 	kso->ks_out_serial = ksi->ks_in_serial;
15900Sstevel@tonic-gate 	kso->ks_out_type = KEYSOCK_OUT;
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	/*
15930Sstevel@tonic-gate 	 * Can safely putnext() to esp_pfkey_q, because this is a turnaround
15940Sstevel@tonic-gate 	 * from the esp_pfkey_q.
15950Sstevel@tonic-gate 	 */
15963448Sdh155122 	putnext(espstack->esp_pfkey_q, mp);
15970Sstevel@tonic-gate }
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate /*
16000Sstevel@tonic-gate  * Insert the ESP header into a packet.  Duplicate an mblk, and insert a newly
16010Sstevel@tonic-gate  * allocated mblk with the ESP header in between the two.
16020Sstevel@tonic-gate  */
16030Sstevel@tonic-gate static boolean_t
16043448Sdh155122 esp_insert_esp(mblk_t *mp, mblk_t *esp_mp, uint_t divpoint,
16053448Sdh155122     ipsecesp_stack_t *espstack)
16060Sstevel@tonic-gate {
16070Sstevel@tonic-gate 	mblk_t *split_mp = mp;
16080Sstevel@tonic-gate 	uint_t wheretodiv = divpoint;
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	while ((split_mp->b_wptr - split_mp->b_rptr) < wheretodiv) {
16110Sstevel@tonic-gate 		wheretodiv -= (split_mp->b_wptr - split_mp->b_rptr);
16120Sstevel@tonic-gate 		split_mp = split_mp->b_cont;
16130Sstevel@tonic-gate 		ASSERT(split_mp != NULL);
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	if (split_mp->b_wptr - split_mp->b_rptr != wheretodiv) {
16170Sstevel@tonic-gate 		mblk_t *scratch;
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 		/* "scratch" is the 2nd half, split_mp is the first. */
16200Sstevel@tonic-gate 		scratch = dupb(split_mp);
16210Sstevel@tonic-gate 		if (scratch == NULL) {
16223448Sdh155122 			esp1dbg(espstack,
16233448Sdh155122 			    ("esp_insert_esp: can't allocate scratch.\n"));
16240Sstevel@tonic-gate 			return (B_FALSE);
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 		/* NOTE:  dupb() doesn't set b_cont appropriately. */
16270Sstevel@tonic-gate 		scratch->b_cont = split_mp->b_cont;
16280Sstevel@tonic-gate 		scratch->b_rptr += wheretodiv;
16290Sstevel@tonic-gate 		split_mp->b_wptr = split_mp->b_rptr + wheretodiv;
16300Sstevel@tonic-gate 		split_mp->b_cont = scratch;
16310Sstevel@tonic-gate 	}
16320Sstevel@tonic-gate 	/*
16330Sstevel@tonic-gate 	 * At this point, split_mp is exactly "wheretodiv" bytes long, and
16340Sstevel@tonic-gate 	 * holds the end of the pre-ESP part of the datagram.
16350Sstevel@tonic-gate 	 */
16360Sstevel@tonic-gate 	esp_mp->b_cont = split_mp->b_cont;
16370Sstevel@tonic-gate 	split_mp->b_cont = esp_mp;
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	return (B_TRUE);
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate /*
16430Sstevel@tonic-gate  * Finish processing of an inbound ESP packet after processing by the
16440Sstevel@tonic-gate  * crypto framework.
16450Sstevel@tonic-gate  * - Remove the ESP header.
16460Sstevel@tonic-gate  * - Send packet back to IP.
16470Sstevel@tonic-gate  * If authentication was performed on the packet, this function is called
16480Sstevel@tonic-gate  * only if the authentication succeeded.
16490Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
16500Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
16510Sstevel@tonic-gate  */
16520Sstevel@tonic-gate static ipsec_status_t
16530Sstevel@tonic-gate esp_in_done(mblk_t *ipsec_in_mp)
16540Sstevel@tonic-gate {
16550Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
16560Sstevel@tonic-gate 	mblk_t *data_mp;
16570Sstevel@tonic-gate 	ipsa_t *assoc;
16580Sstevel@tonic-gate 	uint_t espstart;
16590Sstevel@tonic-gate 	uint32_t ivlen = 0;
16600Sstevel@tonic-gate 	uint_t processed_len;
16610Sstevel@tonic-gate 	esph_t *esph;
16620Sstevel@tonic-gate 	kstat_named_t *counter;
16630Sstevel@tonic-gate 	boolean_t is_natt;
16643448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
16653448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
16663448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	assoc = ii->ipsec_in_esp_sa;
16690Sstevel@tonic-gate 	ASSERT(assoc != NULL);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	/* get the pointer to the ESP header */
16740Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg == SADB_EALG_NULL) {
16750Sstevel@tonic-gate 		/* authentication-only ESP */
16760Sstevel@tonic-gate 		espstart = ii->ipsec_in_crypto_data.cd_offset;
16770Sstevel@tonic-gate 		processed_len = ii->ipsec_in_crypto_data.cd_length;
16780Sstevel@tonic-gate 	} else {
16790Sstevel@tonic-gate 		/* encryption present */
16800Sstevel@tonic-gate 		ivlen = assoc->ipsa_iv_len;
16810Sstevel@tonic-gate 		if (assoc->ipsa_auth_alg == SADB_AALG_NONE) {
16820Sstevel@tonic-gate 			/* encryption-only ESP */
16830Sstevel@tonic-gate 			espstart = ii->ipsec_in_crypto_data.cd_offset -
1684*4987Sdanmcd 			    sizeof (esph_t) - assoc->ipsa_iv_len;
16850Sstevel@tonic-gate 			processed_len = ii->ipsec_in_crypto_data.cd_length +
1686*4987Sdanmcd 			    ivlen;
16870Sstevel@tonic-gate 		} else {
16880Sstevel@tonic-gate 			/* encryption with authentication */
16890Sstevel@tonic-gate 			espstart = ii->ipsec_in_crypto_dual_data.dd_offset1;
16900Sstevel@tonic-gate 			processed_len = ii->ipsec_in_crypto_dual_data.dd_len2 +
16910Sstevel@tonic-gate 			    ivlen;
16920Sstevel@tonic-gate 		}
16930Sstevel@tonic-gate 	}
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 	data_mp = ipsec_in_mp->b_cont;
16960Sstevel@tonic-gate 	esph = (esph_t *)(data_mp->b_rptr + espstart);
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) {
16990Sstevel@tonic-gate 		/* authentication passed if we reach this point */
17003448Sdh155122 		ESP_BUMP_STAT(espstack, good_auth);
17010Sstevel@tonic-gate 		data_mp->b_wptr -= assoc->ipsa_mac_len;
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 		/*
17040Sstevel@tonic-gate 		 * Check replay window here!
17050Sstevel@tonic-gate 		 * For right now, assume keysock will set the replay window
17060Sstevel@tonic-gate 		 * size to zero for SAs that have an unspecified sender.
17070Sstevel@tonic-gate 		 * This may change...
17080Sstevel@tonic-gate 		 */
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 		if (!sadb_replay_check(assoc, esph->esph_replay)) {
17110Sstevel@tonic-gate 			/*
17120Sstevel@tonic-gate 			 * Log the event. As of now we print out an event.
17130Sstevel@tonic-gate 			 * Do not print the replay failure number, or else
17140Sstevel@tonic-gate 			 * syslog cannot collate the error messages.  Printing
17150Sstevel@tonic-gate 			 * the replay number that failed opens a denial-of-
17160Sstevel@tonic-gate 			 * service attack.
17170Sstevel@tonic-gate 			 */
17180Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
17190Sstevel@tonic-gate 			    SL_ERROR | SL_WARN,
17200Sstevel@tonic-gate 			    "Replay failed for ESP spi 0x%x, dst %s.\n",
17210Sstevel@tonic-gate 			    assoc->ipsa_spi, assoc->ipsa_dstaddr,
17223448Sdh155122 			    assoc->ipsa_addrfam, espstack->ipsecesp_netstack);
17233448Sdh155122 			ESP_BUMP_STAT(espstack, replay_failures);
17243448Sdh155122 			counter = DROPPER(ipss, ipds_esp_replay);
17250Sstevel@tonic-gate 			goto drop_and_bail;
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 
1729*4987Sdanmcd 	esp_set_usetime(assoc, B_TRUE);
1730*4987Sdanmcd 
17310Sstevel@tonic-gate 	if (!esp_age_bytes(assoc, processed_len, B_TRUE)) {
17320Sstevel@tonic-gate 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
17330Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
17340Sstevel@tonic-gate 		    SL_ERROR | SL_WARN,
17350Sstevel@tonic-gate 		    "ESP association 0x%x, dst %s had bytes expire.\n",
17363448Sdh155122 		    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
17373448Sdh155122 		    espstack->ipsecesp_netstack);
17383448Sdh155122 		ESP_BUMP_STAT(espstack, bytes_expired);
17393448Sdh155122 		counter = DROPPER(ipss, ipds_esp_bytes_expire);
17400Sstevel@tonic-gate 		goto drop_and_bail;
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	/*
17440Sstevel@tonic-gate 	 * Remove ESP header and padding from packet.  I hope the compiler
17450Sstevel@tonic-gate 	 * spews "branch, predict taken" code for this.
17460Sstevel@tonic-gate 	 */
17470Sstevel@tonic-gate 
17483448Sdh155122 	if (esp_strip_header(data_mp, ii->ipsec_in_v4, ivlen, &counter,
1749*4987Sdanmcd 	    espstack)) {
17500Sstevel@tonic-gate 		if (is_natt)
17510Sstevel@tonic-gate 			return (esp_fix_natt_checksums(data_mp, assoc));
17520Sstevel@tonic-gate 		return (IPSEC_STATUS_SUCCESS);
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 
17553448Sdh155122 	esp1dbg(espstack, ("esp_in_done: esp_strip_header() failed\n"));
17560Sstevel@tonic-gate drop_and_bail:
17573448Sdh155122 	IP_ESP_BUMP_STAT(ipss, in_discards);
17580Sstevel@tonic-gate 	/*
17590Sstevel@tonic-gate 	 * TODO: Extract inbound interface from the IPSEC_IN message's
17600Sstevel@tonic-gate 	 * ii->ipsec_in_rill_index.
17610Sstevel@tonic-gate 	 */
17623448Sdh155122 	ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, counter,
17633448Sdh155122 	    &espstack->esp_dropper);
17640Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
17650Sstevel@tonic-gate }
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate /*
17680Sstevel@tonic-gate  * Called upon failing the inbound ICV check. The message passed as
17690Sstevel@tonic-gate  * argument is freed.
17700Sstevel@tonic-gate  */
17710Sstevel@tonic-gate static void
17720Sstevel@tonic-gate esp_log_bad_auth(mblk_t *ipsec_in)
17730Sstevel@tonic-gate {
17740Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
17750Sstevel@tonic-gate 	ipsa_t *assoc = ii->ipsec_in_esp_sa;
17763448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
17773448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
17783448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	/*
17810Sstevel@tonic-gate 	 * Log the event. Don't print to the console, block
17820Sstevel@tonic-gate 	 * potential denial-of-service attack.
17830Sstevel@tonic-gate 	 */
17843448Sdh155122 	ESP_BUMP_STAT(espstack, bad_auth);
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
17870Sstevel@tonic-gate 	    "ESP Authentication failed for spi 0x%x, dst %s.\n",
17883448Sdh155122 	    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
17893448Sdh155122 	    espstack->ipsecesp_netstack);
17903448Sdh155122 
17913448Sdh155122 	IP_ESP_BUMP_STAT(ipss, in_discards);
17920Sstevel@tonic-gate 	/*
17930Sstevel@tonic-gate 	 * TODO: Extract inbound interface from the IPSEC_IN
17940Sstevel@tonic-gate 	 * message's ii->ipsec_in_rill_index.
17950Sstevel@tonic-gate 	 */
17963448Sdh155122 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
17973448Sdh155122 	    DROPPER(ipss, ipds_esp_bad_auth),
17983448Sdh155122 	    &espstack->esp_dropper);
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate /*
18030Sstevel@tonic-gate  * Invoked for outbound packets after ESP processing. If the packet
18040Sstevel@tonic-gate  * also requires AH, performs the AH SA selection and AH processing.
18050Sstevel@tonic-gate  * Returns B_TRUE if the AH processing was not needed or if it was
18060Sstevel@tonic-gate  * performed successfully. Returns B_FALSE and consumes the passed mblk
18070Sstevel@tonic-gate  * if AH processing was required but could not be performed.
18080Sstevel@tonic-gate  */
18090Sstevel@tonic-gate static boolean_t
18100Sstevel@tonic-gate esp_do_outbound_ah(mblk_t *ipsec_mp)
18110Sstevel@tonic-gate {
18120Sstevel@tonic-gate 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
18130Sstevel@tonic-gate 	ipsec_status_t ipsec_rc;
18140Sstevel@tonic-gate 	ipsec_action_t *ap;
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	ap = io->ipsec_out_act;
18170Sstevel@tonic-gate 	if (ap == NULL) {
18180Sstevel@tonic-gate 		ipsec_policy_t *pp = io->ipsec_out_policy;
18190Sstevel@tonic-gate 		ap = pp->ipsp_act;
18200Sstevel@tonic-gate 	}
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	if (!ap->ipa_want_ah)
18230Sstevel@tonic-gate 		return (B_TRUE);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	ASSERT(io->ipsec_out_ah_done == B_FALSE);
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	if (io->ipsec_out_ah_sa == NULL) {
18280Sstevel@tonic-gate 		if (!ipsec_outbound_sa(ipsec_mp, IPPROTO_AH)) {
18290Sstevel@tonic-gate 			sadb_acquire(ipsec_mp, io, B_TRUE, B_FALSE);
18300Sstevel@tonic-gate 			return (B_FALSE);
18310Sstevel@tonic-gate 		}
18320Sstevel@tonic-gate 	}
18330Sstevel@tonic-gate 	ASSERT(io->ipsec_out_ah_sa != NULL);
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	io->ipsec_out_ah_done = B_TRUE;
18360Sstevel@tonic-gate 	ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp);
18370Sstevel@tonic-gate 	return (ipsec_rc == IPSEC_STATUS_SUCCESS);
18380Sstevel@tonic-gate }
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate /*
18420Sstevel@tonic-gate  * Kernel crypto framework callback invoked after completion of async
18430Sstevel@tonic-gate  * crypto requests.
18440Sstevel@tonic-gate  */
18450Sstevel@tonic-gate static void
18460Sstevel@tonic-gate esp_kcf_callback(void *arg, int status)
18470Sstevel@tonic-gate {
18480Sstevel@tonic-gate 	mblk_t *ipsec_mp = (mblk_t *)arg;
18490Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
1850*4987Sdanmcd 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
18510Sstevel@tonic-gate 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
18523448Sdh155122 	netstackid_t	stackid;
18533448Sdh155122 	netstack_t	*ns, *ns_arg;
18543448Sdh155122 	ipsecesp_stack_t *espstack;
18553448Sdh155122 	ipsec_stack_t	*ipss;
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	ASSERT(ipsec_mp->b_cont != NULL);
18580Sstevel@tonic-gate 
18593448Sdh155122 	if (is_inbound) {
18603448Sdh155122 		stackid = ii->ipsec_in_stackid;
18613448Sdh155122 		ns_arg = ii->ipsec_in_ns;
18623448Sdh155122 	} else {
18633448Sdh155122 		stackid = io->ipsec_out_stackid;
18643448Sdh155122 		ns_arg = io->ipsec_out_ns;
18653448Sdh155122 	}
18663448Sdh155122 
18673448Sdh155122 	/*
18683448Sdh155122 	 * Verify that the netstack is still around; could have vanished
18693448Sdh155122 	 * while kEf was doing its work.
18703448Sdh155122 	 */
18713448Sdh155122 	ns = netstack_find_by_stackid(stackid);
18723448Sdh155122 	if (ns == NULL || ns != ns_arg) {
18733448Sdh155122 		/* Disappeared on us */
18743448Sdh155122 		if (ns != NULL)
18753448Sdh155122 			netstack_rele(ns);
18763448Sdh155122 		freemsg(ipsec_mp);
18773448Sdh155122 		return;
18783448Sdh155122 	}
18793448Sdh155122 
18803448Sdh155122 	espstack = ns->netstack_ipsecesp;
18813448Sdh155122 	ipss = ns->netstack_ipsec;
18823448Sdh155122 
18830Sstevel@tonic-gate 	if (status == CRYPTO_SUCCESS) {
18840Sstevel@tonic-gate 		if (is_inbound) {
18853448Sdh155122 			if (esp_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS) {
18863448Sdh155122 				netstack_rele(ns);
18870Sstevel@tonic-gate 				return;
18883448Sdh155122 			}
18890Sstevel@tonic-gate 			/* finish IPsec processing */
18900Sstevel@tonic-gate 			ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL);
18910Sstevel@tonic-gate 		} else {
18920Sstevel@tonic-gate 			/*
18930Sstevel@tonic-gate 			 * If a ICV was computed, it was stored by the
18940Sstevel@tonic-gate 			 * crypto framework at the end of the packet.
18950Sstevel@tonic-gate 			 */
18960Sstevel@tonic-gate 			ipha_t *ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
18970Sstevel@tonic-gate 
1898*4987Sdanmcd 			esp_set_usetime(io->ipsec_out_esp_sa, B_FALSE);
1899*4987Sdanmcd 			/* NAT-T packet. */
1900*4987Sdanmcd 			if (ipha->ipha_protocol == IPPROTO_UDP)
1901*4987Sdanmcd 				esp_prepare_udp(ns, ipsec_mp->b_cont, ipha);
1902*4987Sdanmcd 
19030Sstevel@tonic-gate 			/* do AH processing if needed */
19043448Sdh155122 			if (!esp_do_outbound_ah(ipsec_mp)) {
19053448Sdh155122 				netstack_rele(ns);
19060Sstevel@tonic-gate 				return;
19073448Sdh155122 			}
19080Sstevel@tonic-gate 			/* finish IPsec processing */
19090Sstevel@tonic-gate 			if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
19100Sstevel@tonic-gate 				ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL,
19110Sstevel@tonic-gate 				    NULL);
19120Sstevel@tonic-gate 			} else {
19130Sstevel@tonic-gate 				ip6_t *ip6h = (ip6_t *)ipha;
19140Sstevel@tonic-gate 				ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h,
19150Sstevel@tonic-gate 				    NULL, NULL);
19160Sstevel@tonic-gate 			}
19170Sstevel@tonic-gate 		}
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	} else if (status == CRYPTO_INVALID_MAC) {
19200Sstevel@tonic-gate 		esp_log_bad_auth(ipsec_mp);
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	} else {
19233448Sdh155122 		esp1dbg(espstack,
19243448Sdh155122 		    ("esp_kcf_callback: crypto failed with 0x%x\n",
19250Sstevel@tonic-gate 		    status));
19263448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_failures);
19270Sstevel@tonic-gate 		if (is_inbound)
19283448Sdh155122 			IP_ESP_BUMP_STAT(ipss, in_discards);
19290Sstevel@tonic-gate 		else
19303448Sdh155122 			ESP_BUMP_STAT(espstack, out_discards);
19310Sstevel@tonic-gate 		ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL,
19323448Sdh155122 		    DROPPER(ipss, ipds_esp_crypto_failed),
19333448Sdh155122 		    &espstack->esp_dropper);
19340Sstevel@tonic-gate 	}
19353448Sdh155122 	netstack_rele(ns);
19360Sstevel@tonic-gate }
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate /*
19390Sstevel@tonic-gate  * Invoked on crypto framework failure during inbound and outbound processing.
19400Sstevel@tonic-gate  */
19410Sstevel@tonic-gate static void
19423448Sdh155122 esp_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc,
19433448Sdh155122     ipsecesp_stack_t *espstack)
19440Sstevel@tonic-gate {
19453448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
19463448Sdh155122 
19473448Sdh155122 	esp1dbg(espstack, ("crypto failed for %s ESP with 0x%x\n",
19480Sstevel@tonic-gate 	    is_inbound ? "inbound" : "outbound", kef_rc));
19493448Sdh155122 	ip_drop_packet(mp, is_inbound, NULL, NULL,
19503448Sdh155122 	    DROPPER(ipss, ipds_esp_crypto_failed),
19513448Sdh155122 	    &espstack->esp_dropper);
19523448Sdh155122 	ESP_BUMP_STAT(espstack, crypto_failures);
19530Sstevel@tonic-gate 	if (is_inbound)
19543448Sdh155122 		IP_ESP_BUMP_STAT(ipss, in_discards);
19550Sstevel@tonic-gate 	else
19563448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
19570Sstevel@tonic-gate }
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate #define	ESP_INIT_CALLREQ(_cr) {						\
19600Sstevel@tonic-gate 	(_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED;		\
19610Sstevel@tonic-gate 	(_cr)->cr_callback_arg = ipsec_mp;				\
19620Sstevel@tonic-gate 	(_cr)->cr_callback_func = esp_kcf_callback;			\
19630Sstevel@tonic-gate }
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) {			\
19660Sstevel@tonic-gate 	(mac)->cd_format = CRYPTO_DATA_RAW;				\
19670Sstevel@tonic-gate 	(mac)->cd_offset = 0;						\
19680Sstevel@tonic-gate 	(mac)->cd_length = icvlen;					\
19690Sstevel@tonic-gate 	(mac)->cd_raw.iov_base = (char *)icvbuf;			\
19700Sstevel@tonic-gate 	(mac)->cd_raw.iov_len = icvlen;					\
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_DATA(data, mp, off, len) {			\
19740Sstevel@tonic-gate 	if (MBLKL(mp) >= (len) + (off)) {				\
19750Sstevel@tonic-gate 		(data)->cd_format = CRYPTO_DATA_RAW;			\
19760Sstevel@tonic-gate 		(data)->cd_raw.iov_base = (char *)(mp)->b_rptr;		\
19770Sstevel@tonic-gate 		(data)->cd_raw.iov_len = MBLKL(mp);			\
19780Sstevel@tonic-gate 		(data)->cd_offset = off;				\
19790Sstevel@tonic-gate 	} else {							\
19800Sstevel@tonic-gate 		(data)->cd_format = CRYPTO_DATA_MBLK;			\
19810Sstevel@tonic-gate 		(data)->cd_mp = mp;			       		\
19820Sstevel@tonic-gate 		(data)->cd_offset = off;				\
19830Sstevel@tonic-gate 	}								\
19840Sstevel@tonic-gate 	(data)->cd_length = len;					\
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_DUAL_DATA(data, mp, off1, len1, off2, len2) {	\
19880Sstevel@tonic-gate 	(data)->dd_format = CRYPTO_DATA_MBLK;				\
19890Sstevel@tonic-gate 	(data)->dd_mp = mp;						\
19900Sstevel@tonic-gate 	(data)->dd_len1 = len1;						\
19910Sstevel@tonic-gate 	(data)->dd_offset1 = off1;					\
19920Sstevel@tonic-gate 	(data)->dd_len2 = len2;						\
19930Sstevel@tonic-gate 	(data)->dd_offset2 = off2;					\
19940Sstevel@tonic-gate }
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate static ipsec_status_t
19970Sstevel@tonic-gate esp_submit_req_inbound(mblk_t *ipsec_mp, ipsa_t *assoc, uint_t esph_offset)
19980Sstevel@tonic-gate {
19990Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
20000Sstevel@tonic-gate 	boolean_t do_auth;
20010Sstevel@tonic-gate 	uint_t auth_offset, msg_len, auth_len;
20020Sstevel@tonic-gate 	crypto_call_req_t call_req;
20030Sstevel@tonic-gate 	mblk_t *esp_mp;
20040Sstevel@tonic-gate 	int kef_rc = CRYPTO_FAILED;
20050Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
20060Sstevel@tonic-gate 	crypto_ctx_template_t auth_ctx_tmpl;
20070Sstevel@tonic-gate 	boolean_t do_encr;
20080Sstevel@tonic-gate 	uint_t encr_offset, encr_len;
20090Sstevel@tonic-gate 	uint_t iv_len = assoc->ipsa_iv_len;
20100Sstevel@tonic-gate 	crypto_ctx_template_t encr_ctx_tmpl;
20113448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
20123448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
20133448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
20160Sstevel@tonic-gate 
20173448Sdh155122 	/*
20183448Sdh155122 	 * In case kEF queues and calls back, keep netstackid_t for
20193448Sdh155122 	 * verification that the IP instance is still around in
20203448Sdh155122 	 * esp_kcf_callback().
20213448Sdh155122 	 */
20223448Sdh155122 	ii->ipsec_in_stackid = ns->netstack_stackid;
20233448Sdh155122 
20240Sstevel@tonic-gate 	do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
20250Sstevel@tonic-gate 	do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	/*
20280Sstevel@tonic-gate 	 * An inbound packet is of the form:
20290Sstevel@tonic-gate 	 * IPSEC_IN -> [IP,options,ESP,IV,data,ICV,pad]
20300Sstevel@tonic-gate 	 */
20310Sstevel@tonic-gate 	esp_mp = ipsec_mp->b_cont;
20320Sstevel@tonic-gate 	msg_len = MBLKL(esp_mp);
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	ESP_INIT_CALLREQ(&call_req);
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	if (do_auth) {
20370Sstevel@tonic-gate 		/* force asynchronous processing? */
20383448Sdh155122 		if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
20390Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
20400Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 		/* authentication context template */
20430Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
20440Sstevel@tonic-gate 		    auth_ctx_tmpl);
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 		/* ICV to be verified */
20470Sstevel@tonic-gate 		ESP_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac,
20480Sstevel@tonic-gate 		    icv_len, esp_mp->b_wptr - icv_len);
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 		/* authentication starts at the ESP header */
20510Sstevel@tonic-gate 		auth_offset = esph_offset;
20520Sstevel@tonic-gate 		auth_len = msg_len - auth_offset - icv_len;
20530Sstevel@tonic-gate 		if (!do_encr) {
20540Sstevel@tonic-gate 			/* authentication only */
20550Sstevel@tonic-gate 			/* initialize input data argument */
20560Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data,
20570Sstevel@tonic-gate 			    esp_mp, auth_offset, auth_len);
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 			/* call the crypto framework */
20600Sstevel@tonic-gate 			kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
20610Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_data,
20620Sstevel@tonic-gate 			    &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
20630Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_mac, &call_req);
20640Sstevel@tonic-gate 		}
20650Sstevel@tonic-gate 	}
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	if (do_encr) {
20680Sstevel@tonic-gate 		/* force asynchronous processing? */
20693448Sdh155122 		if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
20700Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
20710Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 		/* encryption template */
20740Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
20750Sstevel@tonic-gate 		    encr_ctx_tmpl);
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 		/* skip IV, since it is passed separately */
20780Sstevel@tonic-gate 		encr_offset = esph_offset + sizeof (esph_t) + iv_len;
20790Sstevel@tonic-gate 		encr_len = msg_len - encr_offset;
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 		if (!do_auth) {
20820Sstevel@tonic-gate 			/* decryption only */
20830Sstevel@tonic-gate 			/* initialize input data argument */
20840Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data,
20850Sstevel@tonic-gate 			    esp_mp, encr_offset, encr_len);
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 			/* specify IV */
20880Sstevel@tonic-gate 			ii->ipsec_in_crypto_data.cd_miscdata =
20890Sstevel@tonic-gate 			    (char *)esp_mp->b_rptr + sizeof (esph_t) +
20900Sstevel@tonic-gate 			    esph_offset;
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 			/* call the crypto framework */
20930Sstevel@tonic-gate 			kef_rc = crypto_decrypt(&assoc->ipsa_emech,
20940Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_data,
20950Sstevel@tonic-gate 			    &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
20960Sstevel@tonic-gate 			    NULL, &call_req);
20970Sstevel@tonic-gate 		}
20980Sstevel@tonic-gate 	}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	if (do_auth && do_encr) {
21010Sstevel@tonic-gate 		/* dual operation */
21020Sstevel@tonic-gate 		/* initialize input data argument */
21030Sstevel@tonic-gate 		ESP_INIT_CRYPTO_DUAL_DATA(&ii->ipsec_in_crypto_dual_data,
21040Sstevel@tonic-gate 		    esp_mp, auth_offset, auth_len,
21050Sstevel@tonic-gate 		    encr_offset, encr_len - icv_len);
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 		/* specify IV */
21080Sstevel@tonic-gate 		ii->ipsec_in_crypto_dual_data.dd_miscdata =
21090Sstevel@tonic-gate 		    (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset;
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 		/* call the framework */
21120Sstevel@tonic-gate 		kef_rc = crypto_mac_verify_decrypt(&assoc->ipsa_amech,
21130Sstevel@tonic-gate 		    &assoc->ipsa_emech, &ii->ipsec_in_crypto_dual_data,
21140Sstevel@tonic-gate 		    &assoc->ipsa_kcfauthkey, &assoc->ipsa_kcfencrkey,
21150Sstevel@tonic-gate 		    auth_ctx_tmpl, encr_ctx_tmpl, &ii->ipsec_in_crypto_mac,
21160Sstevel@tonic-gate 		    NULL, &call_req);
21170Sstevel@tonic-gate 	}
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 	switch (kef_rc) {
21200Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
21213448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_sync);
21220Sstevel@tonic-gate 		return (esp_in_done(ipsec_mp));
21230Sstevel@tonic-gate 	case CRYPTO_QUEUED:
21240Sstevel@tonic-gate 		/* esp_kcf_callback() will be invoked on completion */
21253448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_async);
21260Sstevel@tonic-gate 		return (IPSEC_STATUS_PENDING);
21270Sstevel@tonic-gate 	case CRYPTO_INVALID_MAC:
21283448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_sync);
21290Sstevel@tonic-gate 		esp_log_bad_auth(ipsec_mp);
21300Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
21310Sstevel@tonic-gate 	}
21320Sstevel@tonic-gate 
21333448Sdh155122 	esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc, espstack);
21340Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
21350Sstevel@tonic-gate }
21360Sstevel@tonic-gate 
2137*4987Sdanmcd /*
2138*4987Sdanmcd  * Compute the IP and UDP checksums -- common code for both keepalives and
2139*4987Sdanmcd  * actual ESP-in-UDP packets.  Be flexible with multiple mblks because ESP
2140*4987Sdanmcd  * uses mblk-insertion to insert the UDP header.
2141*4987Sdanmcd  * TODO - If there is an easy way to prep a packet for HW checksums, make
2142*4987Sdanmcd  * it happen here.
2143*4987Sdanmcd  */
2144*4987Sdanmcd static void
2145*4987Sdanmcd esp_prepare_udp(netstack_t *ns, mblk_t *mp, ipha_t *ipha)
2146*4987Sdanmcd {
2147*4987Sdanmcd 	int offset;
2148*4987Sdanmcd 	uint32_t cksum;
2149*4987Sdanmcd 	uint16_t *arr;
2150*4987Sdanmcd 	mblk_t *udpmp = mp;
2151*4987Sdanmcd 
2152*4987Sdanmcd 	ASSERT(MBLKL(mp) >= sizeof (ipha_t));
2153*4987Sdanmcd 
2154*4987Sdanmcd 	ipha->ipha_hdr_checksum = 0;
2155*4987Sdanmcd 	ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
2156*4987Sdanmcd 
2157*4987Sdanmcd 	if (ns->netstack_udp->us_do_checksum) {
2158*4987Sdanmcd 		ASSERT(MBLKL(udpmp) >= sizeof (udpha_t));
2159*4987Sdanmcd 		/* arr points to the IP header. */
2160*4987Sdanmcd 		arr = (uint16_t *)ipha;
2161*4987Sdanmcd 		IP_STAT(ns->netstack_ip, ip_out_sw_cksum);
2162*4987Sdanmcd 		IP_STAT_UPDATE(ns->netstack_ip, ip_udp_out_sw_cksum_bytes,
2163*4987Sdanmcd 		    ntohs(htons(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH));
2164*4987Sdanmcd 		/* arr[6-9] are the IP addresses. */
2165*4987Sdanmcd 		cksum = IP_UDP_CSUM_COMP + arr[6] + arr[7] + arr[8] + arr[9] +
2166*4987Sdanmcd 		    ntohs(htons(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH);
2167*4987Sdanmcd 		cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH, cksum);
2168*4987Sdanmcd 		offset = IP_SIMPLE_HDR_LENGTH + UDP_CHECKSUM_OFFSET;
2169*4987Sdanmcd 		while (offset >= MBLKL(udpmp)) {
2170*4987Sdanmcd 			offset -= MBLKL(udpmp);
2171*4987Sdanmcd 			udpmp = udpmp->b_cont;
2172*4987Sdanmcd 		}
2173*4987Sdanmcd 		/* arr points to the UDP header's checksum field. */
2174*4987Sdanmcd 		arr = (uint16_t *)(udpmp->b_rptr + offset);
2175*4987Sdanmcd 		*arr = cksum;
2176*4987Sdanmcd 	}
2177*4987Sdanmcd }
2178*4987Sdanmcd 
2179*4987Sdanmcd /*
2180*4987Sdanmcd  * Send a one-byte UDP NAT-T keepalive.  Construct an IPSEC_OUT too that'll
2181*4987Sdanmcd  * get fed into esp_send_udp/ip_wput_ipsec_out.
2182*4987Sdanmcd  */
2183*4987Sdanmcd void
2184*4987Sdanmcd ipsecesp_send_keepalive(ipsa_t *assoc)
2185*4987Sdanmcd {
2186*4987Sdanmcd 	mblk_t *mp = NULL, *ipsec_mp = NULL;
2187*4987Sdanmcd 	ipha_t *ipha;
2188*4987Sdanmcd 	udpha_t *udpha;
2189*4987Sdanmcd 	ipsec_out_t *io;
2190*4987Sdanmcd 
2191*4987Sdanmcd 	ASSERT(!MUTEX_HELD(&assoc->ipsa_lock));
2192*4987Sdanmcd 
2193*4987Sdanmcd 	mp = allocb(sizeof (ipha_t) + sizeof (udpha_t) + 1, BPRI_HI);
2194*4987Sdanmcd 	if (mp == NULL)
2195*4987Sdanmcd 		return;
2196*4987Sdanmcd 	ipha = (ipha_t *)mp->b_rptr;
2197*4987Sdanmcd 	ipha->ipha_version_and_hdr_length = IP_SIMPLE_HDR_VERSION;
2198*4987Sdanmcd 	ipha->ipha_type_of_service = 0;
2199*4987Sdanmcd 	ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (udpha_t) + 1);
2200*4987Sdanmcd 	/* Use the low-16 of the SPI so we have some clue where it came from. */
2201*4987Sdanmcd 	ipha->ipha_ident = *(((uint16_t *)(&assoc->ipsa_spi)) + 1);
2202*4987Sdanmcd 	ipha->ipha_fragment_offset_and_flags = 0;  /* Too small to fragment! */
2203*4987Sdanmcd 	ipha->ipha_ttl = 0xFF;
2204*4987Sdanmcd 	ipha->ipha_protocol = IPPROTO_UDP;
2205*4987Sdanmcd 	ipha->ipha_hdr_checksum = 0;
2206*4987Sdanmcd 	ipha->ipha_src = assoc->ipsa_srcaddr[0];
2207*4987Sdanmcd 	ipha->ipha_dst = assoc->ipsa_dstaddr[0];
2208*4987Sdanmcd 	udpha = (udpha_t *)(ipha + 1);
2209*4987Sdanmcd 	udpha->uha_src_port = (assoc->ipsa_local_nat_port != 0) ?
2210*4987Sdanmcd 	    assoc->ipsa_local_nat_port : htons(IPPORT_IKE_NATT);
2211*4987Sdanmcd 	udpha->uha_dst_port = (assoc->ipsa_remote_nat_port != 0) ?
2212*4987Sdanmcd 	    assoc->ipsa_remote_nat_port : htons(IPPORT_IKE_NATT);
2213*4987Sdanmcd 	udpha->uha_length = htons(sizeof (udpha_t) + 1);
2214*4987Sdanmcd 	udpha->uha_checksum = 0;
2215*4987Sdanmcd 	mp->b_wptr = (uint8_t *)(udpha + 1);
2216*4987Sdanmcd 	*(mp->b_wptr++) = 0xFF;
2217*4987Sdanmcd 
2218*4987Sdanmcd 	ipsec_mp = ipsec_alloc_ipsec_out(assoc->ipsa_netstack);
2219*4987Sdanmcd 	if (ipsec_mp == NULL) {
2220*4987Sdanmcd 		freeb(mp);
2221*4987Sdanmcd 		return;
2222*4987Sdanmcd 	}
2223*4987Sdanmcd 	ipsec_mp->b_cont = mp;
2224*4987Sdanmcd 	io = (ipsec_out_t *)ipsec_mp->b_rptr;
2225*4987Sdanmcd 	io->ipsec_out_zoneid =
2226*4987Sdanmcd 	    netstackid_to_zoneid(assoc->ipsa_netstack->netstack_stackid);
2227*4987Sdanmcd 
2228*4987Sdanmcd 	esp_prepare_udp(assoc->ipsa_netstack, mp, ipha);
2229*4987Sdanmcd 	ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL, NULL);
2230*4987Sdanmcd }
2231*4987Sdanmcd 
22320Sstevel@tonic-gate static ipsec_status_t
22330Sstevel@tonic-gate esp_submit_req_outbound(mblk_t *ipsec_mp, ipsa_t *assoc, uchar_t *icv_buf,
22340Sstevel@tonic-gate     uint_t payload_len)
22350Sstevel@tonic-gate {
22360Sstevel@tonic-gate 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
22370Sstevel@tonic-gate 	uint_t auth_len;
22380Sstevel@tonic-gate 	crypto_call_req_t call_req;
22390Sstevel@tonic-gate 	mblk_t *esp_mp;
22400Sstevel@tonic-gate 	int kef_rc = CRYPTO_FAILED;
22410Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
22420Sstevel@tonic-gate 	crypto_ctx_template_t auth_ctx_tmpl;
22430Sstevel@tonic-gate 	boolean_t do_auth;
22440Sstevel@tonic-gate 	boolean_t do_encr;
22450Sstevel@tonic-gate 	uint_t iv_len = assoc->ipsa_iv_len;
22460Sstevel@tonic-gate 	crypto_ctx_template_t encr_ctx_tmpl;
22470Sstevel@tonic-gate 	boolean_t is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
22480Sstevel@tonic-gate 	size_t esph_offset = (is_natt ? UDPH_SIZE : 0);
22493448Sdh155122 	netstack_t	*ns = io->ipsec_out_ns;
22503448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
22513448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
22523448Sdh155122 
22533448Sdh155122 	esp3dbg(espstack, ("esp_submit_req_outbound:%s",
2254*4987Sdanmcd 	    is_natt ? "natt" : "not natt"));
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
22570Sstevel@tonic-gate 
22583448Sdh155122 	/*
22593448Sdh155122 	 * In case kEF queues and calls back, keep netstackid_t for
22603448Sdh155122 	 * verification that the IP instance is still around in
22613448Sdh155122 	 * esp_kcf_callback().
22623448Sdh155122 	 */
22633448Sdh155122 	io->ipsec_out_stackid = ns->netstack_stackid;
22643448Sdh155122 
22650Sstevel@tonic-gate 	do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
22660Sstevel@tonic-gate 	do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	/*
22690Sstevel@tonic-gate 	 * Outbound IPsec packets are of the form:
22700Sstevel@tonic-gate 	 * IPSEC_OUT -> [IP,options] -> [ESP,IV] -> [data] -> [pad,ICV]
22710Sstevel@tonic-gate 	 * unless it's NATT, then it's
22720Sstevel@tonic-gate 	 * IPSEC_OUT -> [IP,options] -> [udp][ESP,IV] -> [data] -> [pad,ICV]
22730Sstevel@tonic-gate 	 * Get a pointer to the mblk containing the ESP header.
22740Sstevel@tonic-gate 	 */
22750Sstevel@tonic-gate 	ASSERT(ipsec_mp->b_cont != NULL && ipsec_mp->b_cont->b_cont != NULL);
22760Sstevel@tonic-gate 	esp_mp = ipsec_mp->b_cont->b_cont;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	ESP_INIT_CALLREQ(&call_req);
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 	if (do_auth) {
22810Sstevel@tonic-gate 		/* force asynchronous processing? */
22823448Sdh155122 		if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
22830Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
22840Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 		/* authentication context template */
22870Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
22880Sstevel@tonic-gate 		    auth_ctx_tmpl);
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 		/* where to store the computed mac */
22910Sstevel@tonic-gate 		ESP_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac,
22920Sstevel@tonic-gate 		    icv_len, icv_buf);
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 		/* authentication starts at the ESP header */
2295134Sdanmcd 		auth_len = payload_len + iv_len + sizeof (esph_t);
22960Sstevel@tonic-gate 		if (!do_encr) {
22970Sstevel@tonic-gate 			/* authentication only */
22980Sstevel@tonic-gate 			/* initialize input data argument */
22990Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data,
23000Sstevel@tonic-gate 			    esp_mp, esph_offset, auth_len);
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 			/* call the crypto framework */
23030Sstevel@tonic-gate 			kef_rc = crypto_mac(&assoc->ipsa_amech,
23040Sstevel@tonic-gate 			    &io->ipsec_out_crypto_data,
23050Sstevel@tonic-gate 			    &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
23060Sstevel@tonic-gate 			    &io->ipsec_out_crypto_mac, &call_req);
23070Sstevel@tonic-gate 		}
23080Sstevel@tonic-gate 	}
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 	if (do_encr) {
23110Sstevel@tonic-gate 		/* force asynchronous processing? */
23123448Sdh155122 		if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
23130Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
23140Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 		/* encryption context template */
23170Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
23180Sstevel@tonic-gate 		    encr_ctx_tmpl);
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 		if (!do_auth) {
23210Sstevel@tonic-gate 			/* encryption only, skip mblk that contains ESP hdr */
23220Sstevel@tonic-gate 			/* initialize input data argument */
23230Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data,
23240Sstevel@tonic-gate 			    esp_mp->b_cont, 0, payload_len);
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 			/* specify IV */
23270Sstevel@tonic-gate 			io->ipsec_out_crypto_data.cd_miscdata =
23280Sstevel@tonic-gate 			    (char *)esp_mp->b_rptr + sizeof (esph_t) +
23290Sstevel@tonic-gate 			    esph_offset;
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 			/* call the crypto framework */
23320Sstevel@tonic-gate 			kef_rc = crypto_encrypt(&assoc->ipsa_emech,
23330Sstevel@tonic-gate 			    &io->ipsec_out_crypto_data,
23340Sstevel@tonic-gate 			    &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
23350Sstevel@tonic-gate 			    NULL, &call_req);
23360Sstevel@tonic-gate 		}
23370Sstevel@tonic-gate 	}
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	if (do_auth && do_encr) {
23400Sstevel@tonic-gate 		/*
23410Sstevel@tonic-gate 		 * Encryption and authentication:
23420Sstevel@tonic-gate 		 * Pass the pointer to the mblk chain starting at the ESP
23430Sstevel@tonic-gate 		 * header to the framework. Skip the ESP header mblk
23440Sstevel@tonic-gate 		 * for encryption, which is reflected by an encryption
23450Sstevel@tonic-gate 		 * offset equal to the length of that mblk. Start
23460Sstevel@tonic-gate 		 * the authentication at the ESP header, i.e. use an
23470Sstevel@tonic-gate 		 * authentication offset of zero.
23480Sstevel@tonic-gate 		 */
23490Sstevel@tonic-gate 		ESP_INIT_CRYPTO_DUAL_DATA(&io->ipsec_out_crypto_dual_data,
23500Sstevel@tonic-gate 		    esp_mp, MBLKL(esp_mp), payload_len, esph_offset, auth_len);
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 		/* specify IV */
23530Sstevel@tonic-gate 		io->ipsec_out_crypto_dual_data.dd_miscdata =
23540Sstevel@tonic-gate 		    (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset;
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 		/* call the framework */
23570Sstevel@tonic-gate 		kef_rc = crypto_encrypt_mac(&assoc->ipsa_emech,
23580Sstevel@tonic-gate 		    &assoc->ipsa_amech, NULL,
23590Sstevel@tonic-gate 		    &assoc->ipsa_kcfencrkey, &assoc->ipsa_kcfauthkey,
23600Sstevel@tonic-gate 		    encr_ctx_tmpl, auth_ctx_tmpl,
23610Sstevel@tonic-gate 		    &io->ipsec_out_crypto_dual_data,
23620Sstevel@tonic-gate 		    &io->ipsec_out_crypto_mac, &call_req);
23630Sstevel@tonic-gate 	}
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 	switch (kef_rc) {
23660Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
23673448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_sync);
2368*4987Sdanmcd 		esp_set_usetime(assoc, B_FALSE);
2369*4987Sdanmcd 		if (is_natt)
2370*4987Sdanmcd 			esp_prepare_udp(ns, ipsec_mp->b_cont,
2371*4987Sdanmcd 			    (ipha_t *)ipsec_mp->b_cont->b_rptr);
23720Sstevel@tonic-gate 		return (IPSEC_STATUS_SUCCESS);
23730Sstevel@tonic-gate 	case CRYPTO_QUEUED:
23740Sstevel@tonic-gate 		/* esp_kcf_callback() will be invoked on completion */
23753448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_async);
23760Sstevel@tonic-gate 		return (IPSEC_STATUS_PENDING);
23770Sstevel@tonic-gate 	}
23780Sstevel@tonic-gate 
23793448Sdh155122 	esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc, espstack);
23800Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
23810Sstevel@tonic-gate }
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate /*
23840Sstevel@tonic-gate  * Handle outbound IPsec processing for IPv4 and IPv6
23850Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
23860Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
23870Sstevel@tonic-gate  */
23880Sstevel@tonic-gate static ipsec_status_t
23890Sstevel@tonic-gate esp_outbound(mblk_t *mp)
23900Sstevel@tonic-gate {
23910Sstevel@tonic-gate 	mblk_t *ipsec_out_mp, *data_mp, *espmp, *tailmp;
23920Sstevel@tonic-gate 	ipsec_out_t *io;
23930Sstevel@tonic-gate 	ipha_t *ipha;
23940Sstevel@tonic-gate 	ip6_t *ip6h;
23950Sstevel@tonic-gate 	esph_t *esph;
23960Sstevel@tonic-gate 	uint_t af;
23970Sstevel@tonic-gate 	uint8_t *nhp;
23980Sstevel@tonic-gate 	uintptr_t divpoint, datalen, adj, padlen, i, alloclen;
23990Sstevel@tonic-gate 	uintptr_t esplen = sizeof (esph_t);
24000Sstevel@tonic-gate 	uint8_t protocol;
24010Sstevel@tonic-gate 	ipsa_t *assoc;
24023192Sdanmcd 	uint_t iv_len, mac_len = 0;
24030Sstevel@tonic-gate 	uchar_t *icv_buf;
24040Sstevel@tonic-gate 	udpha_t *udpha;
24050Sstevel@tonic-gate 	boolean_t is_natt = B_FALSE;
24063448Sdh155122 	netstack_t	*ns;
24073448Sdh155122 	ipsecesp_stack_t *espstack;
24083448Sdh155122 	ipsec_stack_t	*ipss;
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 	ipsec_out_mp = mp;
24110Sstevel@tonic-gate 	data_mp = ipsec_out_mp->b_cont;
24120Sstevel@tonic-gate 
24133448Sdh155122 	io = (ipsec_out_t *)ipsec_out_mp->b_rptr;
24143448Sdh155122 	ns = io->ipsec_out_ns;
24153448Sdh155122 	espstack = ns->netstack_ipsecesp;
24163448Sdh155122 	ipss = ns->netstack_ipsec;
24173448Sdh155122 
24183448Sdh155122 	ESP_BUMP_STAT(espstack, out_requests);
24193448Sdh155122 
24200Sstevel@tonic-gate 	/*
24210Sstevel@tonic-gate 	 * <sigh> We have to copy the message here, because TCP (for example)
24220Sstevel@tonic-gate 	 * keeps a dupb() of the message lying around for retransmission.
24230Sstevel@tonic-gate 	 * Since ESP changes the whole of the datagram, we have to create our
24240Sstevel@tonic-gate 	 * own copy lest we clobber TCP's data.  Since we have to copy anyway,
24250Sstevel@tonic-gate 	 * we might as well make use of msgpullup() and get the mblk into one
24260Sstevel@tonic-gate 	 * contiguous piece!
24270Sstevel@tonic-gate 	 */
24280Sstevel@tonic-gate 	ipsec_out_mp->b_cont = msgpullup(data_mp, -1);
24290Sstevel@tonic-gate 	if (ipsec_out_mp->b_cont == NULL) {
24300Sstevel@tonic-gate 		esp0dbg(("esp_outbound: msgpullup() failed, "
24310Sstevel@tonic-gate 		    "dropping packet.\n"));
24320Sstevel@tonic-gate 		ipsec_out_mp->b_cont = data_mp;
24330Sstevel@tonic-gate 		/*
24340Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
24350Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
24360Sstevel@tonic-gate 		 */
24370Sstevel@tonic-gate 		ip_drop_packet(ipsec_out_mp, B_FALSE, NULL, NULL,
24383448Sdh155122 		    DROPPER(ipss, ipds_esp_nomem),
24393448Sdh155122 		    &espstack->esp_dropper);
24400Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
24410Sstevel@tonic-gate 	} else {
24420Sstevel@tonic-gate 		freemsg(data_mp);
24430Sstevel@tonic-gate 		data_mp = ipsec_out_mp->b_cont;
24440Sstevel@tonic-gate 	}
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 	/*
24470Sstevel@tonic-gate 	 * Reality check....
24480Sstevel@tonic-gate 	 */
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 	ipha = (ipha_t *)data_mp->b_rptr;  /* So we can call esp_acquire(). */
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 	if (io->ipsec_out_v4) {
24530Sstevel@tonic-gate 		af = AF_INET;
24540Sstevel@tonic-gate 		divpoint = IPH_HDR_LENGTH(ipha);
24550Sstevel@tonic-gate 		datalen = ntohs(ipha->ipha_length) - divpoint;
24560Sstevel@tonic-gate 		nhp = (uint8_t *)&ipha->ipha_protocol;
24570Sstevel@tonic-gate 	} else {
24580Sstevel@tonic-gate 		ip6_pkt_t ipp;
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 		af = AF_INET6;
24610Sstevel@tonic-gate 		ip6h = (ip6_t *)ipha;
24620Sstevel@tonic-gate 		bzero(&ipp, sizeof (ipp));
24630Sstevel@tonic-gate 		divpoint = ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL);
24640Sstevel@tonic-gate 		if (ipp.ipp_dstopts != NULL &&
24650Sstevel@tonic-gate 		    ipp.ipp_dstopts->ip6d_nxt != IPPROTO_ROUTING) {
24660Sstevel@tonic-gate 			/*
24670Sstevel@tonic-gate 			 * Destination options are tricky.  If we get in here,
24680Sstevel@tonic-gate 			 * then we have a terminal header following the
24690Sstevel@tonic-gate 			 * destination options.  We need to adjust backwards
24700Sstevel@tonic-gate 			 * so we insert ESP BEFORE the destination options
24710Sstevel@tonic-gate 			 * bag.  (So that the dstopts get encrypted!)
24720Sstevel@tonic-gate 			 *
24730Sstevel@tonic-gate 			 * Since this is for outbound packets only, we know
24740Sstevel@tonic-gate 			 * that non-terminal destination options only precede
24750Sstevel@tonic-gate 			 * routing headers.
24760Sstevel@tonic-gate 			 */
24770Sstevel@tonic-gate 			divpoint -= ipp.ipp_dstoptslen;
24780Sstevel@tonic-gate 		}
24790Sstevel@tonic-gate 		datalen = ntohs(ip6h->ip6_plen) + sizeof (ip6_t) - divpoint;
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 		if (ipp.ipp_rthdr != NULL) {
24820Sstevel@tonic-gate 			nhp = &ipp.ipp_rthdr->ip6r_nxt;
24830Sstevel@tonic-gate 		} else if (ipp.ipp_hopopts != NULL) {
24840Sstevel@tonic-gate 			nhp = &ipp.ipp_hopopts->ip6h_nxt;
24850Sstevel@tonic-gate 		} else {
24860Sstevel@tonic-gate 			ASSERT(divpoint == sizeof (ip6_t));
24870Sstevel@tonic-gate 			/* It's probably IP + ESP. */
24880Sstevel@tonic-gate 			nhp = &ip6h->ip6_nxt;
24890Sstevel@tonic-gate 		}
24900Sstevel@tonic-gate 	}
24910Sstevel@tonic-gate 	assoc = io->ipsec_out_esp_sa;
24920Sstevel@tonic-gate 	ASSERT(assoc != NULL);
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != SADB_AALG_NONE)
24950Sstevel@tonic-gate 		mac_len = assoc->ipsa_mac_len;
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 	if (assoc->ipsa_flags & IPSA_F_NATT) {
24980Sstevel@tonic-gate 		/* wedge in fake UDP */
24990Sstevel@tonic-gate 		is_natt = B_TRUE;
25000Sstevel@tonic-gate 		esplen += UDPH_SIZE;
25010Sstevel@tonic-gate 	}
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	/*
25040Sstevel@tonic-gate 	 * Set up ESP header and encryption padding for ENCR PI request.
25050Sstevel@tonic-gate 	 */
25060Sstevel@tonic-gate 
25073192Sdanmcd 	/* Determine the padding length.  Pad to 4-bytes for no-encryption. */
25080Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg != SADB_EALG_NULL) {
25093192Sdanmcd 		iv_len = assoc->ipsa_iv_len;
25103192Sdanmcd 
25113192Sdanmcd 		/*
25123192Sdanmcd 		 * Include the two additional bytes (hence the - 2) for the
25133192Sdanmcd 		 * padding length and the next header.  Take this into account
25143192Sdanmcd 		 * when calculating the actual length of the padding.
25153192Sdanmcd 		 */
25163192Sdanmcd 		ASSERT(ISP2(iv_len));
25173192Sdanmcd 		padlen = ((unsigned)(iv_len - datalen - 2)) & (iv_len - 1);
25180Sstevel@tonic-gate 	} else {
25193192Sdanmcd 		iv_len = 0;
25203192Sdanmcd 		padlen = ((unsigned)(sizeof (uint32_t) - datalen - 2)) &
25213192Sdanmcd 		    (sizeof (uint32_t) - 1);
25220Sstevel@tonic-gate 	}
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 	/* Allocate ESP header and IV. */
25250Sstevel@tonic-gate 	esplen += iv_len;
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	/*
25280Sstevel@tonic-gate 	 * Update association byte-count lifetimes.  Don't forget to take
25290Sstevel@tonic-gate 	 * into account the padding length and next-header (hence the + 2).
2530134Sdanmcd 	 *
25310Sstevel@tonic-gate 	 * Use the amount of data fed into the "encryption algorithm".  This
25320Sstevel@tonic-gate 	 * is the IV, the data length, the padding length, and the final two
25330Sstevel@tonic-gate 	 * bytes (padlen, and next-header).
25340Sstevel@tonic-gate 	 *
25350Sstevel@tonic-gate 	 */
25360Sstevel@tonic-gate 
2537134Sdanmcd 	if (!esp_age_bytes(assoc, datalen + padlen + iv_len + 2, B_FALSE)) {
25380Sstevel@tonic-gate 		/*
25390Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
25400Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
25410Sstevel@tonic-gate 		 */
25420Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
25433448Sdh155122 		    DROPPER(ipss, ipds_esp_bytes_expire),
25443448Sdh155122 		    &espstack->esp_dropper);
25450Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
25460Sstevel@tonic-gate 	}
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 	espmp = allocb(esplen, BPRI_HI);
25490Sstevel@tonic-gate 	if (espmp == NULL) {
25503448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
25513448Sdh155122 		esp1dbg(espstack, ("esp_outbound: can't allocate espmp.\n"));
25520Sstevel@tonic-gate 		/*
25530Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
25540Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
25550Sstevel@tonic-gate 		 */
25563448Sdh155122 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
25573448Sdh155122 		    DROPPER(ipss, ipds_esp_nomem),
25583448Sdh155122 		    &espstack->esp_dropper);
25590Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
25600Sstevel@tonic-gate 	}
25610Sstevel@tonic-gate 	espmp->b_wptr += esplen;
25620Sstevel@tonic-gate 	esph = (esph_t *)espmp->b_rptr;
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	if (is_natt) {
25653448Sdh155122 		esp3dbg(espstack, ("esp_outbound: NATT"));
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 		udpha = (udpha_t *)espmp->b_rptr;
2568*4987Sdanmcd 		udpha->uha_src_port = (assoc->ipsa_local_nat_port != 0) ?
2569*4987Sdanmcd 		    assoc->ipsa_local_nat_port : htons(IPPORT_IKE_NATT);
2570*4987Sdanmcd 		udpha->uha_dst_port = (assoc->ipsa_remote_nat_port != 0) ?
2571*4987Sdanmcd 		    assoc->ipsa_remote_nat_port : htons(IPPORT_IKE_NATT);
25720Sstevel@tonic-gate 		/*
2573*4987Sdanmcd 		 * Set the checksum to 0, so that the esp_prepare_udp() call
25740Sstevel@tonic-gate 		 * can do the right thing.
25750Sstevel@tonic-gate 		 */
25760Sstevel@tonic-gate 		udpha->uha_checksum = 0;
25770Sstevel@tonic-gate 		esph = (esph_t *)(udpha + 1);
25780Sstevel@tonic-gate 	}
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 	esph->esph_spi = assoc->ipsa_spi;
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	esph->esph_replay = htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
25830Sstevel@tonic-gate 	if (esph->esph_replay == 0 && assoc->ipsa_replay_wsize != 0) {
25840Sstevel@tonic-gate 		/*
25850Sstevel@tonic-gate 		 * XXX We have replay counter wrapping.
25860Sstevel@tonic-gate 		 * We probably want to nuke this SA (and its peer).
25870Sstevel@tonic-gate 		 */
25880Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
25890Sstevel@tonic-gate 		    SL_ERROR | SL_CONSOLE | SL_WARN,
25900Sstevel@tonic-gate 		    "Outbound ESP SA (0x%x, %s) has wrapped sequence.\n",
25913448Sdh155122 		    esph->esph_spi, assoc->ipsa_dstaddr, af,
25923448Sdh155122 		    espstack->ipsecesp_netstack);
25933448Sdh155122 
25943448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
25950Sstevel@tonic-gate 		sadb_replay_delete(assoc);
25960Sstevel@tonic-gate 		/*
25970Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
25980Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
25990Sstevel@tonic-gate 		 */
26003448Sdh155122 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
26013448Sdh155122 		    DROPPER(ipss, ipds_esp_replay),
26023448Sdh155122 		    &espstack->esp_dropper);
26030Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
26040Sstevel@tonic-gate 	}
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	/*
26070Sstevel@tonic-gate 	 * Set the IV to a random quantity.  We do not require the
26080Sstevel@tonic-gate 	 * highest quality random bits, but for best security with CBC
26090Sstevel@tonic-gate 	 * mode ciphers, the value must be unlikely to repeat and also
26100Sstevel@tonic-gate 	 * must not be known in advance to an adversary capable of
26110Sstevel@tonic-gate 	 * influencing the plaintext.
26120Sstevel@tonic-gate 	 */
26130Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)(esph + 1), iv_len);
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate 	/* Fix the IP header. */
26160Sstevel@tonic-gate 	alloclen = padlen + 2 + mac_len;
26170Sstevel@tonic-gate 	adj = alloclen + (espmp->b_wptr - espmp->b_rptr);
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	protocol = *nhp;
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	if (io->ipsec_out_v4) {
26220Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) + adj);
26230Sstevel@tonic-gate 		if (is_natt) {
26240Sstevel@tonic-gate 			*nhp = IPPROTO_UDP;
26250Sstevel@tonic-gate 			udpha->uha_length = htons(ntohs(ipha->ipha_length) -
26260Sstevel@tonic-gate 			    IPH_HDR_LENGTH(ipha));
26270Sstevel@tonic-gate 		} else {
26280Sstevel@tonic-gate 			*nhp = IPPROTO_ESP;
26290Sstevel@tonic-gate 		}
26300Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = 0;
26310Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
26320Sstevel@tonic-gate 	} else {
26330Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) + adj);
26340Sstevel@tonic-gate 		*nhp = IPPROTO_ESP;
26350Sstevel@tonic-gate 	}
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	/* I've got the two ESP mblks, now insert them. */
26380Sstevel@tonic-gate 
26393448Sdh155122 	esp2dbg(espstack, ("data_mp before outbound ESP adjustment:\n"));
26403448Sdh155122 	esp2dbg(espstack, (dump_msg(data_mp)));
26413448Sdh155122 
26423448Sdh155122 	if (!esp_insert_esp(data_mp, espmp, divpoint, espstack)) {
26433448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
26440Sstevel@tonic-gate 		/* NOTE:  esp_insert_esp() only fails if there's no memory. */
26450Sstevel@tonic-gate 		/*
26460Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
26470Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
26480Sstevel@tonic-gate 		 */
26493448Sdh155122 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
26503448Sdh155122 		    DROPPER(ipss, ipds_esp_nomem),
26513448Sdh155122 		    &espstack->esp_dropper);
26520Sstevel@tonic-gate 		freeb(espmp);
26530Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	/* Append padding (and leave room for ICV). */
26570Sstevel@tonic-gate 	for (tailmp = data_mp; tailmp->b_cont != NULL; tailmp = tailmp->b_cont)
26580Sstevel@tonic-gate 		;
26590Sstevel@tonic-gate 	if (tailmp->b_wptr + alloclen > tailmp->b_datap->db_lim) {
26600Sstevel@tonic-gate 		tailmp->b_cont = allocb(alloclen, BPRI_HI);
26610Sstevel@tonic-gate 		if (tailmp->b_cont == NULL) {
26623448Sdh155122 			ESP_BUMP_STAT(espstack, out_discards);
26630Sstevel@tonic-gate 			esp0dbg(("esp_outbound:  Can't allocate tailmp.\n"));
26640Sstevel@tonic-gate 			/*
26650Sstevel@tonic-gate 			 * TODO:  Find the outbound IRE for this packet and
26660Sstevel@tonic-gate 			 * pass it to ip_drop_packet().
26670Sstevel@tonic-gate 			 */
26680Sstevel@tonic-gate 			ip_drop_packet(mp, B_FALSE, NULL, NULL,
26693448Sdh155122 			    DROPPER(ipss, ipds_esp_nomem),
26703448Sdh155122 			    &espstack->esp_dropper);
26710Sstevel@tonic-gate 			return (IPSEC_STATUS_FAILED);
26720Sstevel@tonic-gate 		}
26730Sstevel@tonic-gate 		tailmp = tailmp->b_cont;
26740Sstevel@tonic-gate 	}
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	/*
26770Sstevel@tonic-gate 	 * If there's padding, N bytes of padding must be of the form 0x1,
26780Sstevel@tonic-gate 	 * 0x2, 0x3... 0xN.
26790Sstevel@tonic-gate 	 */
26800Sstevel@tonic-gate 	for (i = 0; i < padlen; ) {
26810Sstevel@tonic-gate 		i++;
26820Sstevel@tonic-gate 		*tailmp->b_wptr++ = i;
26830Sstevel@tonic-gate 	}
26840Sstevel@tonic-gate 	*tailmp->b_wptr++ = i;
26850Sstevel@tonic-gate 	*tailmp->b_wptr++ = protocol;
26860Sstevel@tonic-gate 
26873448Sdh155122 	esp2dbg(espstack, ("data_Mp before encryption:\n"));
26883448Sdh155122 	esp2dbg(espstack, (dump_msg(data_mp)));
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 	/*
26910Sstevel@tonic-gate 	 * The packet is eligible for hardware acceleration if the
26920Sstevel@tonic-gate 	 * following conditions are satisfied:
26930Sstevel@tonic-gate 	 *
26940Sstevel@tonic-gate 	 * 1. the packet will not be fragmented
26950Sstevel@tonic-gate 	 * 2. the provider supports the algorithms specified by SA
26960Sstevel@tonic-gate 	 * 3. there is no pending control message being exchanged
26970Sstevel@tonic-gate 	 * 4. snoop is not attached
26980Sstevel@tonic-gate 	 * 5. the destination address is not a multicast address
26990Sstevel@tonic-gate 	 *
27000Sstevel@tonic-gate 	 * All five of these conditions are checked by IP prior to
27010Sstevel@tonic-gate 	 * sending the packet to ESP.
27020Sstevel@tonic-gate 	 *
27030Sstevel@tonic-gate 	 * But We, and We Alone, can, nay MUST check if the packet
27040Sstevel@tonic-gate 	 * is over NATT, and then disqualify it from hardware
27050Sstevel@tonic-gate 	 * acceleration.
27060Sstevel@tonic-gate 	 */
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 	if (io->ipsec_out_is_capab_ill && !(assoc->ipsa_flags & IPSA_F_NATT)) {
27090Sstevel@tonic-gate 		return (esp_outbound_accelerated(ipsec_out_mp, mac_len));
27100Sstevel@tonic-gate 	}
27113448Sdh155122 	ESP_BUMP_STAT(espstack, noaccel);
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate 	/*
27140Sstevel@tonic-gate 	 * Okay.  I've set up the pre-encryption ESP.  Let's do it!
27150Sstevel@tonic-gate 	 */
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 	if (mac_len > 0) {
27180Sstevel@tonic-gate 		ASSERT(tailmp->b_wptr + mac_len <= tailmp->b_datap->db_lim);
27190Sstevel@tonic-gate 		icv_buf = tailmp->b_wptr;
27200Sstevel@tonic-gate 		tailmp->b_wptr += mac_len;
27210Sstevel@tonic-gate 	} else {
27220Sstevel@tonic-gate 		icv_buf = NULL;
27230Sstevel@tonic-gate 	}
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	return (esp_submit_req_outbound(ipsec_out_mp, assoc, icv_buf,
27260Sstevel@tonic-gate 	    datalen + padlen + 2));
27270Sstevel@tonic-gate }
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate /*
27300Sstevel@tonic-gate  * IP calls this to validate the ICMP errors that
27310Sstevel@tonic-gate  * we got from the network.
27320Sstevel@tonic-gate  */
27330Sstevel@tonic-gate ipsec_status_t
27340Sstevel@tonic-gate ipsecesp_icmp_error(mblk_t *ipsec_mp)
27350Sstevel@tonic-gate {
27363448Sdh155122 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
27373448Sdh155122 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
27383448Sdh155122 	netstack_t	*ns;
27393448Sdh155122 	ipsecesp_stack_t *espstack;
27403448Sdh155122 	ipsec_stack_t	*ipss;
27413448Sdh155122 
27423448Sdh155122 	if (is_inbound) {
27433448Sdh155122 		ns = ii->ipsec_in_ns;
27443448Sdh155122 	} else {
27453448Sdh155122 		ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
27463448Sdh155122 
27473448Sdh155122 		ns = io->ipsec_out_ns;
27483448Sdh155122 	}
27493448Sdh155122 	espstack = ns->netstack_ipsecesp;
27503448Sdh155122 	ipss = ns->netstack_ipsec;
27513448Sdh155122 
27520Sstevel@tonic-gate 	/*
27530Sstevel@tonic-gate 	 * Unless we get an entire packet back, this function is useless.
27540Sstevel@tonic-gate 	 * Why?
27550Sstevel@tonic-gate 	 *
27560Sstevel@tonic-gate 	 * 1.)	Partial packets are useless, because the "next header"
27570Sstevel@tonic-gate 	 *	is at the end of the decrypted ESP packet.  Without the
27580Sstevel@tonic-gate 	 *	whole packet, this is useless.
27590Sstevel@tonic-gate 	 *
27600Sstevel@tonic-gate 	 * 2.)	If we every use a stateful cipher, such as a stream or a
27610Sstevel@tonic-gate 	 *	one-time pad, we can't do anything.
27620Sstevel@tonic-gate 	 *
27630Sstevel@tonic-gate 	 * Since the chances of us getting an entire packet back are very
27640Sstevel@tonic-gate 	 * very small, we discard here.
27650Sstevel@tonic-gate 	 */
27663448Sdh155122 	IP_ESP_BUMP_STAT(ipss, in_discards);
27673448Sdh155122 	ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
27683448Sdh155122 	    DROPPER(ipss, ipds_esp_icmp),
27693448Sdh155122 	    &espstack->esp_dropper);
27700Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
27710Sstevel@tonic-gate }
27720Sstevel@tonic-gate 
27730Sstevel@tonic-gate /*
27740Sstevel@tonic-gate  * ESP module read put routine.
27750Sstevel@tonic-gate  */
27760Sstevel@tonic-gate /* ARGSUSED */
27770Sstevel@tonic-gate static void
27780Sstevel@tonic-gate ipsecesp_rput(queue_t *q, mblk_t *mp)
27790Sstevel@tonic-gate {
27803448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
27813448Sdh155122 
27823055Sdanmcd 	ASSERT(mp->b_datap->db_type != M_CTL);	/* No more IRE_DB_REQ. */
27833448Sdh155122 
27840Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
27850Sstevel@tonic-gate 	case M_PROTO:
27860Sstevel@tonic-gate 	case M_PCPROTO:
27870Sstevel@tonic-gate 		/* TPI message of some sort. */
27880Sstevel@tonic-gate 		switch (*((t_scalar_t *)mp->b_rptr)) {
27890Sstevel@tonic-gate 		case T_BIND_ACK:
27903448Sdh155122 			esp3dbg(espstack,
27913448Sdh155122 			    ("Thank you IP from ESP for T_BIND_ACK\n"));
27920Sstevel@tonic-gate 			break;
27930Sstevel@tonic-gate 		case T_ERROR_ACK:
27940Sstevel@tonic-gate 			cmn_err(CE_WARN,
27950Sstevel@tonic-gate 			    "ipsecesp:  ESP received T_ERROR_ACK from IP.");
27960Sstevel@tonic-gate 			/*
27970Sstevel@tonic-gate 			 * Make esp_sadb.s_ip_q NULL, and in the
27980Sstevel@tonic-gate 			 * future, perhaps try again.
27990Sstevel@tonic-gate 			 */
28003448Sdh155122 			espstack->esp_sadb.s_ip_q = NULL;
28010Sstevel@tonic-gate 			break;
28020Sstevel@tonic-gate 		case T_OK_ACK:
28030Sstevel@tonic-gate 			/* Probably from a (rarely sent) T_UNBIND_REQ. */
28040Sstevel@tonic-gate 			break;
28050Sstevel@tonic-gate 		default:
28060Sstevel@tonic-gate 			esp0dbg(("Unknown M_{,PC}PROTO message.\n"));
28070Sstevel@tonic-gate 		}
28080Sstevel@tonic-gate 		freemsg(mp);
28090Sstevel@tonic-gate 		break;
28100Sstevel@tonic-gate 	default:
28110Sstevel@tonic-gate 		/* For now, passthru message. */
28123448Sdh155122 		esp2dbg(espstack, ("ESP got unknown mblk type %d.\n",
28130Sstevel@tonic-gate 		    mp->b_datap->db_type));
28140Sstevel@tonic-gate 		putnext(q, mp);
28150Sstevel@tonic-gate 	}
28160Sstevel@tonic-gate }
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate /*
28190Sstevel@tonic-gate  * Construct an SADB_REGISTER message with the current algorithms.
28200Sstevel@tonic-gate  */
28210Sstevel@tonic-gate static boolean_t
28223448Sdh155122 esp_register_out(uint32_t sequence, uint32_t pid, uint_t serial,
28233448Sdh155122     ipsecesp_stack_t *espstack)
28240Sstevel@tonic-gate {
28250Sstevel@tonic-gate 	mblk_t *pfkey_msg_mp, *keysock_out_mp;
28260Sstevel@tonic-gate 	sadb_msg_t *samsg;
28270Sstevel@tonic-gate 	sadb_supported_t *sasupp_auth = NULL;
28280Sstevel@tonic-gate 	sadb_supported_t *sasupp_encr = NULL;
28290Sstevel@tonic-gate 	sadb_alg_t *saalg;
28300Sstevel@tonic-gate 	uint_t allocsize = sizeof (*samsg);
28310Sstevel@tonic-gate 	uint_t i, numalgs_snap;
28320Sstevel@tonic-gate 	int current_aalgs;
28330Sstevel@tonic-gate 	ipsec_alginfo_t **authalgs;
28340Sstevel@tonic-gate 	uint_t num_aalgs;
28350Sstevel@tonic-gate 	int current_ealgs;
28360Sstevel@tonic-gate 	ipsec_alginfo_t **encralgs;
28370Sstevel@tonic-gate 	uint_t num_ealgs;
28383448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 	/* Allocate the KEYSOCK_OUT. */
28410Sstevel@tonic-gate 	keysock_out_mp = sadb_keysock_out(serial);
28420Sstevel@tonic-gate 	if (keysock_out_mp == NULL) {
28430Sstevel@tonic-gate 		esp0dbg(("esp_register_out: couldn't allocate mblk.\n"));
28440Sstevel@tonic-gate 		return (B_FALSE);
28450Sstevel@tonic-gate 	}
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	/*
28480Sstevel@tonic-gate 	 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
28490Sstevel@tonic-gate 	 */
28500Sstevel@tonic-gate 
28513448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
28520Sstevel@tonic-gate 
28530Sstevel@tonic-gate 	/*
28540Sstevel@tonic-gate 	 * Fill SADB_REGISTER message's algorithm descriptors.  Hold
28550Sstevel@tonic-gate 	 * down the lock while filling it.
28560Sstevel@tonic-gate 	 *
28570Sstevel@tonic-gate 	 * Return only valid algorithms, so the number of algorithms
28580Sstevel@tonic-gate 	 * to send up may be less than the number of algorithm entries
28590Sstevel@tonic-gate 	 * in the table.
28600Sstevel@tonic-gate 	 */
28613448Sdh155122 	authalgs = ipss->ipsec_alglists[IPSEC_ALG_AUTH];
28620Sstevel@tonic-gate 	for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
28630Sstevel@tonic-gate 		if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
28640Sstevel@tonic-gate 			num_aalgs++;
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	if (num_aalgs != 0) {
28670Sstevel@tonic-gate 		allocsize += (num_aalgs * sizeof (*saalg));
28680Sstevel@tonic-gate 		allocsize += sizeof (*sasupp_auth);
28690Sstevel@tonic-gate 	}
28703448Sdh155122 	encralgs = ipss->ipsec_alglists[IPSEC_ALG_ENCR];
28710Sstevel@tonic-gate 	for (num_ealgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
28720Sstevel@tonic-gate 		if (encralgs[i] != NULL && ALG_VALID(encralgs[i]))
28730Sstevel@tonic-gate 			num_ealgs++;
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 	if (num_ealgs != 0) {
28760Sstevel@tonic-gate 		allocsize += (num_ealgs * sizeof (*saalg));
28770Sstevel@tonic-gate 		allocsize += sizeof (*sasupp_encr);
28780Sstevel@tonic-gate 	}
28790Sstevel@tonic-gate 	keysock_out_mp->b_cont = allocb(allocsize, BPRI_HI);
28800Sstevel@tonic-gate 	if (keysock_out_mp->b_cont == NULL) {
28813448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
28820Sstevel@tonic-gate 		freemsg(keysock_out_mp);
28830Sstevel@tonic-gate 		return (B_FALSE);
28840Sstevel@tonic-gate 	}
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	pfkey_msg_mp = keysock_out_mp->b_cont;
28870Sstevel@tonic-gate 	pfkey_msg_mp->b_wptr += allocsize;
28880Sstevel@tonic-gate 	if (num_aalgs != 0) {
28890Sstevel@tonic-gate 		sasupp_auth = (sadb_supported_t *)
28900Sstevel@tonic-gate 		    (pfkey_msg_mp->b_rptr + sizeof (*samsg));
28910Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(sasupp_auth + 1);
28920Sstevel@tonic-gate 
28930Sstevel@tonic-gate 		ASSERT(((ulong_t)saalg & 0x7) == 0);
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate 		numalgs_snap = 0;
28960Sstevel@tonic-gate 		for (i = 0;
28973448Sdh155122 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs));
28983448Sdh155122 		    i++) {
28990Sstevel@tonic-gate 			if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
29000Sstevel@tonic-gate 				continue;
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 			saalg->sadb_alg_id = authalgs[i]->alg_id;
29030Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = 0;
29040Sstevel@tonic-gate 			saalg->sadb_alg_minbits	= authalgs[i]->alg_ef_minbits;
29050Sstevel@tonic-gate 			saalg->sadb_alg_maxbits	= authalgs[i]->alg_ef_maxbits;
29060Sstevel@tonic-gate 			saalg->sadb_x_alg_defincr = authalgs[i]->alg_ef_default;
29070Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
29080Sstevel@tonic-gate 			    authalgs[i]->alg_increment;
29090Sstevel@tonic-gate 			numalgs_snap++;
29100Sstevel@tonic-gate 			saalg++;
29110Sstevel@tonic-gate 		}
29120Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_aalgs);
29130Sstevel@tonic-gate #ifdef DEBUG
29140Sstevel@tonic-gate 		/*
29150Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
29160Sstevel@tonic-gate 		 * algorithms.
29170Sstevel@tonic-gate 		 */
29180Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++) {
29190Sstevel@tonic-gate 			if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) {
29200Sstevel@tonic-gate 				cmn_err(CE_PANIC, "esp_register_out()! "
29210Sstevel@tonic-gate 				    "Missed aalg #%d.\n", i);
29220Sstevel@tonic-gate 			}
29230Sstevel@tonic-gate 		}
29240Sstevel@tonic-gate #endif /* DEBUG */
29250Sstevel@tonic-gate 	} else {
29260Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(pfkey_msg_mp->b_rptr + sizeof (*samsg));
29270Sstevel@tonic-gate 	}
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	if (num_ealgs != 0) {
29300Sstevel@tonic-gate 		sasupp_encr = (sadb_supported_t *)saalg;
29310Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(sasupp_encr + 1);
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 		numalgs_snap = 0;
29340Sstevel@tonic-gate 		for (i = 0;
29350Sstevel@tonic-gate 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_ealgs)); i++) {
29360Sstevel@tonic-gate 			if (encralgs[i] == NULL || !ALG_VALID(encralgs[i]))
29370Sstevel@tonic-gate 				continue;
29380Sstevel@tonic-gate 			saalg->sadb_alg_id = encralgs[i]->alg_id;
29390Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = encralgs[i]->alg_datalen;
29400Sstevel@tonic-gate 			saalg->sadb_alg_minbits	= encralgs[i]->alg_ef_minbits;
29410Sstevel@tonic-gate 			saalg->sadb_alg_maxbits	= encralgs[i]->alg_ef_maxbits;
29420Sstevel@tonic-gate 			saalg->sadb_x_alg_defincr = encralgs[i]->alg_ef_default;
29430Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
29440Sstevel@tonic-gate 			    encralgs[i]->alg_increment;
29450Sstevel@tonic-gate 			numalgs_snap++;
29460Sstevel@tonic-gate 			saalg++;
29470Sstevel@tonic-gate 		}
29480Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_ealgs);
29490Sstevel@tonic-gate #ifdef DEBUG
29500Sstevel@tonic-gate 		/*
29510Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
29520Sstevel@tonic-gate 		 * algorithms.
29530Sstevel@tonic-gate 		 */
29540Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++) {
29550Sstevel@tonic-gate 			if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) {
29560Sstevel@tonic-gate 				cmn_err(CE_PANIC, "esp_register_out()! "
29570Sstevel@tonic-gate 				    "Missed ealg #%d.\n", i);
29580Sstevel@tonic-gate 			}
29590Sstevel@tonic-gate 		}
29600Sstevel@tonic-gate #endif /* DEBUG */
29610Sstevel@tonic-gate 	}
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 	current_aalgs = num_aalgs;
29640Sstevel@tonic-gate 	current_ealgs = num_ealgs;
29650Sstevel@tonic-gate 
29663448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	/* Now fill the rest of the SADB_REGISTER message. */
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate 	samsg = (sadb_msg_t *)pfkey_msg_mp->b_rptr;
29710Sstevel@tonic-gate 	samsg->sadb_msg_version = PF_KEY_V2;
29720Sstevel@tonic-gate 	samsg->sadb_msg_type = SADB_REGISTER;
29730Sstevel@tonic-gate 	samsg->sadb_msg_errno = 0;
29740Sstevel@tonic-gate 	samsg->sadb_msg_satype = SADB_SATYPE_ESP;
29750Sstevel@tonic-gate 	samsg->sadb_msg_len = SADB_8TO64(allocsize);
29760Sstevel@tonic-gate 	samsg->sadb_msg_reserved = 0;
29770Sstevel@tonic-gate 	/*
29780Sstevel@tonic-gate 	 * Assume caller has sufficient sequence/pid number info.  If it's one
29790Sstevel@tonic-gate 	 * from me over a new alg., I could give two hoots about sequence.
29800Sstevel@tonic-gate 	 */
29810Sstevel@tonic-gate 	samsg->sadb_msg_seq = sequence;
29820Sstevel@tonic-gate 	samsg->sadb_msg_pid = pid;
29830Sstevel@tonic-gate 
29840Sstevel@tonic-gate 	if (sasupp_auth != NULL) {
2985*4987Sdanmcd 		sasupp_auth->sadb_supported_len = SADB_8TO64(
2986*4987Sdanmcd 		    sizeof (*sasupp_auth) + sizeof (*saalg) * current_aalgs);
29870Sstevel@tonic-gate 		sasupp_auth->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
29880Sstevel@tonic-gate 		sasupp_auth->sadb_supported_reserved = 0;
29890Sstevel@tonic-gate 	}
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 	if (sasupp_encr != NULL) {
2992*4987Sdanmcd 		sasupp_encr->sadb_supported_len = SADB_8TO64(
2993*4987Sdanmcd 		    sizeof (*sasupp_encr) + sizeof (*saalg) * current_ealgs);
29940Sstevel@tonic-gate 		sasupp_encr->sadb_supported_exttype =
29950Sstevel@tonic-gate 		    SADB_EXT_SUPPORTED_ENCRYPT;
29960Sstevel@tonic-gate 		sasupp_encr->sadb_supported_reserved = 0;
29970Sstevel@tonic-gate 	}
29980Sstevel@tonic-gate 
29993448Sdh155122 	if (espstack->esp_pfkey_q != NULL)
30003448Sdh155122 		putnext(espstack->esp_pfkey_q, keysock_out_mp);
30010Sstevel@tonic-gate 	else {
30020Sstevel@tonic-gate 		freemsg(keysock_out_mp);
30030Sstevel@tonic-gate 		return (B_FALSE);
30040Sstevel@tonic-gate 	}
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	return (B_TRUE);
30070Sstevel@tonic-gate }
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate /*
30100Sstevel@tonic-gate  * Invoked when the algorithm table changes. Causes SADB_REGISTER
30110Sstevel@tonic-gate  * messages continaining the current list of algorithms to be
30120Sstevel@tonic-gate  * sent up to the ESP listeners.
30130Sstevel@tonic-gate  */
30140Sstevel@tonic-gate void
30153448Sdh155122 ipsecesp_algs_changed(netstack_t *ns)
30160Sstevel@tonic-gate {
30173448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
30183448Sdh155122 
30190Sstevel@tonic-gate 	/*
30200Sstevel@tonic-gate 	 * Time to send a PF_KEY SADB_REGISTER message to ESP listeners
30210Sstevel@tonic-gate 	 * everywhere.  (The function itself checks for NULL esp_pfkey_q.)
30220Sstevel@tonic-gate 	 */
30233448Sdh155122 	(void) esp_register_out(0, 0, 0, espstack);
30240Sstevel@tonic-gate }
30250Sstevel@tonic-gate 
30260Sstevel@tonic-gate /*
30270Sstevel@tonic-gate  * taskq_dispatch handler.
30280Sstevel@tonic-gate  */
30290Sstevel@tonic-gate static void
30300Sstevel@tonic-gate inbound_task(void *arg)
30310Sstevel@tonic-gate {
30320Sstevel@tonic-gate 	esph_t *esph;
30330Sstevel@tonic-gate 	mblk_t *mp = (mblk_t *)arg;
30340Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr;
30353448Sdh155122 	netstack_t		*ns = ii->ipsec_in_ns;
30363448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
30370Sstevel@tonic-gate 	int ipsec_rc;
30380Sstevel@tonic-gate 
30393448Sdh155122 	esp2dbg(espstack, ("in ESP inbound_task"));
30403448Sdh155122 	ASSERT(espstack != NULL);
30413448Sdh155122 
30423448Sdh155122 	esph = ipsec_inbound_esp_sa(mp, ns);
30430Sstevel@tonic-gate 	if (esph == NULL)
30440Sstevel@tonic-gate 		return;
30450Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_esp_sa != NULL);
30460Sstevel@tonic-gate 	ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func(mp, esph);
30470Sstevel@tonic-gate 	if (ipsec_rc != IPSEC_STATUS_SUCCESS)
30480Sstevel@tonic-gate 		return;
30490Sstevel@tonic-gate 	ip_fanout_proto_again(mp, NULL, NULL, NULL);
30500Sstevel@tonic-gate }
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate /*
30530Sstevel@tonic-gate  * Now that weak-key passed, actually ADD the security association, and
30540Sstevel@tonic-gate  * send back a reply ADD message.
30550Sstevel@tonic-gate  */
30560Sstevel@tonic-gate static int
30573055Sdanmcd esp_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi,
30583448Sdh155122     int *diagnostic, ipsecesp_stack_t *espstack)
30590Sstevel@tonic-gate {
3060691Ssommerfe 	isaf_t *primary, *secondary, *inbound, *outbound;
30610Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
30620Sstevel@tonic-gate 	sadb_address_t *dstext =
30630Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
30640Sstevel@tonic-gate 	struct sockaddr_in *dst;
30650Sstevel@tonic-gate 	struct sockaddr_in6 *dst6;
30660Sstevel@tonic-gate 	boolean_t is_ipv4, clone = B_FALSE, is_inbound = B_FALSE;
30670Sstevel@tonic-gate 	uint32_t *dstaddr;
30680Sstevel@tonic-gate 	ipsa_t *larval = NULL;
30690Sstevel@tonic-gate 	ipsacq_t *acqrec;
30700Sstevel@tonic-gate 	iacqf_t *acq_bucket;
30710Sstevel@tonic-gate 	mblk_t *acq_msgs = NULL;
30720Sstevel@tonic-gate 	int rc;
30730Sstevel@tonic-gate 	sadb_t *sp;
30740Sstevel@tonic-gate 	int outhash;
30750Sstevel@tonic-gate 	mblk_t *lpkt;
30763448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate 	/*
30790Sstevel@tonic-gate 	 * Locate the appropriate table(s).
30800Sstevel@tonic-gate 	 */
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
30830Sstevel@tonic-gate 	dst6 = (struct sockaddr_in6 *)dst;
30840Sstevel@tonic-gate 	is_ipv4 = (dst->sin_family == AF_INET);
30850Sstevel@tonic-gate 	if (is_ipv4) {
30863448Sdh155122 		sp = &espstack->esp_sadb.s_v4;
30870Sstevel@tonic-gate 		dstaddr = (uint32_t *)(&dst->sin_addr);
3088564Ssommerfe 		outhash = OUTBOUND_HASH_V4(sp, *(ipaddr_t *)dstaddr);
30890Sstevel@tonic-gate 	} else {
30903448Sdh155122 		sp = &espstack->esp_sadb.s_v6;
30910Sstevel@tonic-gate 		dstaddr = (uint32_t *)(&dst6->sin6_addr);
3092564Ssommerfe 		outhash = OUTBOUND_HASH_V6(sp, *(in6_addr_t *)dstaddr);
30930Sstevel@tonic-gate 	}
3094691Ssommerfe 
3095564Ssommerfe 	inbound = INBOUND_BUCKET(sp, assoc->sadb_sa_spi);
3096691Ssommerfe 	outbound = &sp->sdb_of[outhash];
3097691Ssommerfe 
30980Sstevel@tonic-gate 	switch (ksi->ks_in_dsttype) {
30990Sstevel@tonic-gate 	case KS_IN_ADDR_MBCAST:
31000Sstevel@tonic-gate 		clone = B_TRUE;	/* All mcast SAs can be bidirectional */
31010Sstevel@tonic-gate 		/* FALLTHRU */
31020Sstevel@tonic-gate 	case KS_IN_ADDR_ME:
31030Sstevel@tonic-gate 		primary = inbound;
3104691Ssommerfe 		secondary = outbound;
31050Sstevel@tonic-gate 		/*
31060Sstevel@tonic-gate 		 * If the source address is either one of mine, or unspecified
31070Sstevel@tonic-gate 		 * (which is best summed up by saying "not 'not mine'"),
31080Sstevel@tonic-gate 		 * then the association is potentially bi-directional,
31090Sstevel@tonic-gate 		 * in that it can be used for inbound traffic and outbound
31100Sstevel@tonic-gate 		 * traffic.  The best example of such an SA is a multicast
31110Sstevel@tonic-gate 		 * SA (which allows me to receive the outbound traffic).
31120Sstevel@tonic-gate 		 */
31130Sstevel@tonic-gate 		if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
31140Sstevel@tonic-gate 			clone = B_TRUE;
31150Sstevel@tonic-gate 		is_inbound = B_TRUE;
31160Sstevel@tonic-gate 		break;
31170Sstevel@tonic-gate 	case KS_IN_ADDR_NOTME:
3118691Ssommerfe 		primary = outbound;
31190Sstevel@tonic-gate 		secondary = inbound;
31200Sstevel@tonic-gate 		/*
31210Sstevel@tonic-gate 		 * If the source address literally not mine (either
31220Sstevel@tonic-gate 		 * unspecified or not mine), then this SA may have an
31230Sstevel@tonic-gate 		 * address that WILL be mine after some configuration.
31240Sstevel@tonic-gate 		 * We pay the price for this by making it a bi-directional
31250Sstevel@tonic-gate 		 * SA.
31260Sstevel@tonic-gate 		 */
31270Sstevel@tonic-gate 		if (ksi->ks_in_srctype != KS_IN_ADDR_ME)
31280Sstevel@tonic-gate 			clone = B_TRUE;
31290Sstevel@tonic-gate 		break;
31300Sstevel@tonic-gate 	default:
31313055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
31320Sstevel@tonic-gate 		return (EINVAL);
31330Sstevel@tonic-gate 	}
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	/*
31360Sstevel@tonic-gate 	 * Find a ACQUIRE list entry if possible.  If we've added an SA that
31370Sstevel@tonic-gate 	 * suits the needs of an ACQUIRE list entry, we can eliminate the
31380Sstevel@tonic-gate 	 * ACQUIRE list entry and transmit the enqueued packets.  Use the
31390Sstevel@tonic-gate 	 * high-bit of the sequence number to queue it.  Key off destination
31400Sstevel@tonic-gate 	 * addr, and change acqrec's state.
31410Sstevel@tonic-gate 	 */
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 	if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
31440Sstevel@tonic-gate 		acq_bucket = &sp->sdb_acq[outhash];
31450Sstevel@tonic-gate 		mutex_enter(&acq_bucket->iacqf_lock);
31460Sstevel@tonic-gate 		for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
31470Sstevel@tonic-gate 		    acqrec = acqrec->ipsacq_next) {
31480Sstevel@tonic-gate 			mutex_enter(&acqrec->ipsacq_lock);
31490Sstevel@tonic-gate 			/*
31500Sstevel@tonic-gate 			 * Q:  I only check sequence.  Should I check dst?
31510Sstevel@tonic-gate 			 * A: Yes, check dest because those are the packets
31520Sstevel@tonic-gate 			 *    that are queued up.
31530Sstevel@tonic-gate 			 */
31540Sstevel@tonic-gate 			if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
31550Sstevel@tonic-gate 			    IPSA_ARE_ADDR_EQUAL(dstaddr,
3156*4987Sdanmcd 			    acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
31570Sstevel@tonic-gate 				break;
31580Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
31590Sstevel@tonic-gate 		}
31600Sstevel@tonic-gate 		if (acqrec != NULL) {
31610Sstevel@tonic-gate 			/*
31620Sstevel@tonic-gate 			 * AHA!  I found an ACQUIRE record for this SA.
31630Sstevel@tonic-gate 			 * Grab the msg list, and free the acquire record.
31640Sstevel@tonic-gate 			 * I already am holding the lock for this record,
31650Sstevel@tonic-gate 			 * so all I have to do is free it.
31660Sstevel@tonic-gate 			 */
31670Sstevel@tonic-gate 			acq_msgs = acqrec->ipsacq_mp;
31680Sstevel@tonic-gate 			acqrec->ipsacq_mp = NULL;
31690Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
31703448Sdh155122 			sadb_destroy_acquire(acqrec,
31713448Sdh155122 			    espstack->ipsecesp_netstack);
31720Sstevel@tonic-gate 		}
31730Sstevel@tonic-gate 		mutex_exit(&acq_bucket->iacqf_lock);
31740Sstevel@tonic-gate 	}
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 	/*
31770Sstevel@tonic-gate 	 * Find PF_KEY message, and see if I'm an update.  If so, find entry
31780Sstevel@tonic-gate 	 * in larval list (if there).
31790Sstevel@tonic-gate 	 */
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_UPDATE) {
31820Sstevel@tonic-gate 		mutex_enter(&inbound->isaf_lock);
31830Sstevel@tonic-gate 		larval = ipsec_getassocbyspi(inbound, assoc->sadb_sa_spi,
31840Sstevel@tonic-gate 		    ALL_ZEROES_PTR, dstaddr, dst->sin_family);
31850Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 		if (larval == NULL) {
31880Sstevel@tonic-gate 			esp0dbg(("Larval update, but larval disappeared.\n"));
31890Sstevel@tonic-gate 			return (ESRCH);
31900Sstevel@tonic-gate 		} /* Else sadb_common_add unlinks it for me! */
31910Sstevel@tonic-gate 	}
31920Sstevel@tonic-gate 
31930Sstevel@tonic-gate 	lpkt = NULL;
31940Sstevel@tonic-gate 	if (larval != NULL)
31950Sstevel@tonic-gate 		lpkt = sadb_clear_lpkt(larval);
31960Sstevel@tonic-gate 
31973448Sdh155122 	rc = sadb_common_add(espstack->esp_sadb.s_ip_q, espstack->esp_pfkey_q,
31983448Sdh155122 	    mp, samsg, ksi, primary, secondary, larval, clone, is_inbound,
31993448Sdh155122 	    diagnostic, espstack->ipsecesp_netstack);
32000Sstevel@tonic-gate 
32010Sstevel@tonic-gate 	if (rc == 0 && lpkt != NULL) {
32020Sstevel@tonic-gate 		rc = !taskq_dispatch(esp_taskq, inbound_task,
3203*4987Sdanmcd 		    (void *) lpkt, TQ_NOSLEEP);
32040Sstevel@tonic-gate 	}
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 	if (rc != 0) {
32070Sstevel@tonic-gate 		ip_drop_packet(lpkt, B_TRUE, NULL, NULL,
32083448Sdh155122 		    DROPPER(ipss, ipds_sadb_inlarval_timeout),
32093448Sdh155122 		    &espstack->esp_dropper);
32100Sstevel@tonic-gate 	}
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate 	/*
32130Sstevel@tonic-gate 	 * How much more stack will I create with all of these
32140Sstevel@tonic-gate 	 * esp_outbound() calls?
32150Sstevel@tonic-gate 	 */
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 	while (acq_msgs != NULL) {
32180Sstevel@tonic-gate 		mblk_t *mp = acq_msgs;
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 		acq_msgs = acq_msgs->b_next;
32210Sstevel@tonic-gate 		mp->b_next = NULL;
32220Sstevel@tonic-gate 		if (rc == 0) {
32230Sstevel@tonic-gate 			if (ipsec_outbound_sa(mp, IPPROTO_ESP)) {
32240Sstevel@tonic-gate 				((ipsec_out_t *)(mp->b_rptr))->
32250Sstevel@tonic-gate 				    ipsec_out_esp_done = B_TRUE;
32260Sstevel@tonic-gate 				if (esp_outbound(mp) == IPSEC_STATUS_SUCCESS) {
32273055Sdanmcd 					ipha_t *ipha;
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate 					/* do AH processing if needed */
32300Sstevel@tonic-gate 					if (!esp_do_outbound_ah(mp))
32310Sstevel@tonic-gate 						continue;
32320Sstevel@tonic-gate 
32333055Sdanmcd 					ipha = (ipha_t *)mp->b_cont->b_rptr;
32343055Sdanmcd 
32350Sstevel@tonic-gate 					/* finish IPsec processing */
32360Sstevel@tonic-gate 					if (is_ipv4) {
32370Sstevel@tonic-gate 						ip_wput_ipsec_out(NULL, mp,
32380Sstevel@tonic-gate 						    ipha, NULL, NULL);
32390Sstevel@tonic-gate 					} else {
32400Sstevel@tonic-gate 						ip6_t *ip6h = (ip6_t *)ipha;
32410Sstevel@tonic-gate 						ip_wput_ipsec_out_v6(NULL,
32420Sstevel@tonic-gate 						    mp, ip6h, NULL, NULL);
32430Sstevel@tonic-gate 					}
32440Sstevel@tonic-gate 				}
32450Sstevel@tonic-gate 				continue;
32460Sstevel@tonic-gate 			}
32470Sstevel@tonic-gate 		}
32483448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
32490Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
32503448Sdh155122 		    DROPPER(ipss, ipds_sadb_acquire_timeout),
32513448Sdh155122 		    &espstack->esp_dropper);
32520Sstevel@tonic-gate 	}
32530Sstevel@tonic-gate 
32540Sstevel@tonic-gate 	return (rc);
32550Sstevel@tonic-gate }
32560Sstevel@tonic-gate 
32570Sstevel@tonic-gate /*
32580Sstevel@tonic-gate  * Add new ESP security association.  This may become a generic AH/ESP
32590Sstevel@tonic-gate  * routine eventually.
32600Sstevel@tonic-gate  */
32610Sstevel@tonic-gate static int
32623448Sdh155122 esp_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, netstack_t *ns)
32630Sstevel@tonic-gate {
32640Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
32650Sstevel@tonic-gate 	sadb_address_t *srcext =
32660Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
32670Sstevel@tonic-gate 	sadb_address_t *dstext =
32680Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
32693055Sdanmcd 	sadb_address_t *isrcext =
32703055Sdanmcd 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
32713055Sdanmcd 	sadb_address_t *idstext =
32723055Sdanmcd 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
32730Sstevel@tonic-gate 	sadb_address_t *nttext_loc =
32740Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC];
32750Sstevel@tonic-gate 	sadb_address_t *nttext_rem =
32760Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM];
32770Sstevel@tonic-gate 	sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
32780Sstevel@tonic-gate 	sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT];
32790Sstevel@tonic-gate 	struct sockaddr_in *src, *dst;
32800Sstevel@tonic-gate 	struct sockaddr_in *natt_loc, *natt_rem;
32810Sstevel@tonic-gate 	struct sockaddr_in6 *natt_loc6, *natt_rem6;
32820Sstevel@tonic-gate 	sadb_lifetime_t *soft =
32830Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
32840Sstevel@tonic-gate 	sadb_lifetime_t *hard =
32850Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
32863448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
32873448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
32880Sstevel@tonic-gate 
32890Sstevel@tonic-gate 	/* I need certain extensions present for an ADD message. */
32900Sstevel@tonic-gate 	if (srcext == NULL) {
32910Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
32920Sstevel@tonic-gate 		return (EINVAL);
32930Sstevel@tonic-gate 	}
32940Sstevel@tonic-gate 	if (dstext == NULL) {
32950Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
32960Sstevel@tonic-gate 		return (EINVAL);
32970Sstevel@tonic-gate 	}
32983055Sdanmcd 	if (isrcext == NULL && idstext != NULL) {
32993055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
33003055Sdanmcd 		return (EINVAL);
33013055Sdanmcd 	}
33023055Sdanmcd 	if (isrcext != NULL && idstext == NULL) {
33033055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
33043055Sdanmcd 		return (EINVAL);
33053055Sdanmcd 	}
33060Sstevel@tonic-gate 	if (assoc == NULL) {
33070Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
33080Sstevel@tonic-gate 		return (EINVAL);
33090Sstevel@tonic-gate 	}
33100Sstevel@tonic-gate 	if (ekey == NULL && assoc->sadb_sa_encrypt != SADB_EALG_NULL) {
33110Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_EKEY;
33120Sstevel@tonic-gate 		return (EINVAL);
33130Sstevel@tonic-gate 	}
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 	src = (struct sockaddr_in *)(srcext + 1);
33160Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
33170Sstevel@tonic-gate 	natt_loc = (struct sockaddr_in *)(nttext_loc + 1);
33180Sstevel@tonic-gate 	natt_loc6 = (struct sockaddr_in6 *)(nttext_loc + 1);
33190Sstevel@tonic-gate 	natt_rem = (struct sockaddr_in *)(nttext_rem + 1);
33200Sstevel@tonic-gate 	natt_rem6 = (struct sockaddr_in6 *)(nttext_rem + 1);
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate 	/* Sundry ADD-specific reality checks. */
33230Sstevel@tonic-gate 	/* XXX STATS :  Logging/stats here? */
33240Sstevel@tonic-gate 	if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
33250Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
33260Sstevel@tonic-gate 		return (EINVAL);
33270Sstevel@tonic-gate 	}
33280Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt == SADB_EALG_NONE) {
33290Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
33300Sstevel@tonic-gate 		return (EINVAL);
33310Sstevel@tonic-gate 	}
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt == SADB_EALG_NULL &&
33340Sstevel@tonic-gate 	    assoc->sadb_sa_auth == SADB_AALG_NONE) {
33350Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
33360Sstevel@tonic-gate 		return (EINVAL);
33370Sstevel@tonic-gate 	}
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & ~(SADB_SAFLAGS_NOREPLAY |
33403055Sdanmcd 	    SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM |
3341*4987Sdanmcd 	    SADB_X_SAFLAGS_TUNNEL)) {
33420Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
33430Sstevel@tonic-gate 		return (EINVAL);
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 	if ((*diagnostic = sadb_hardsoftchk(hard, soft)) != 0) {
33470Sstevel@tonic-gate 		return (EINVAL);
33480Sstevel@tonic-gate 	}
33493055Sdanmcd 	ASSERT(src->sin_family == dst->sin_family);
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) {
33520Sstevel@tonic-gate 		if (nttext_loc == NULL) {
33530Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_LOC;
33540Sstevel@tonic-gate 			return (EINVAL);
33550Sstevel@tonic-gate 		}
33560Sstevel@tonic-gate 
33570Sstevel@tonic-gate 		if (natt_loc->sin_family == AF_INET6 &&
33580Sstevel@tonic-gate 		    !IN6_IS_ADDR_V4MAPPED(&natt_loc6->sin6_addr)) {
33590Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC;
33600Sstevel@tonic-gate 			return (EINVAL);
33610Sstevel@tonic-gate 		}
33620Sstevel@tonic-gate 	}
33630Sstevel@tonic-gate 
33640Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) {
33650Sstevel@tonic-gate 		if (nttext_rem == NULL) {
33660Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_REM;
33670Sstevel@tonic-gate 			return (EINVAL);
33680Sstevel@tonic-gate 		}
33690Sstevel@tonic-gate 		if (natt_rem->sin_family == AF_INET6 &&
33700Sstevel@tonic-gate 		    !IN6_IS_ADDR_V4MAPPED(&natt_rem6->sin6_addr)) {
33710Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM;
33720Sstevel@tonic-gate 			return (EINVAL);
33730Sstevel@tonic-gate 		}
33740Sstevel@tonic-gate 	}
33750Sstevel@tonic-gate 
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate 	/* Stuff I don't support, for now.  XXX Diagnostic? */
33780Sstevel@tonic-gate 	if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL ||
33790Sstevel@tonic-gate 	    ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL)
33800Sstevel@tonic-gate 		return (EOPNOTSUPP);
33810Sstevel@tonic-gate 
33820Sstevel@tonic-gate 	/*
33830Sstevel@tonic-gate 	 * XXX Policy :  I'm not checking identities or sensitivity
33840Sstevel@tonic-gate 	 * labels at this time, but if I did, I'd do them here, before I sent
33850Sstevel@tonic-gate 	 * the weak key check up to the algorithm.
33860Sstevel@tonic-gate 	 */
33870Sstevel@tonic-gate 
33883448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate 	/*
33910Sstevel@tonic-gate 	 * First locate the authentication algorithm.
33920Sstevel@tonic-gate 	 */
33930Sstevel@tonic-gate 	if (akey != NULL) {
33940Sstevel@tonic-gate 		ipsec_alginfo_t *aalg;
33950Sstevel@tonic-gate 
33963448Sdh155122 		aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
33973448Sdh155122 		    [assoc->sadb_sa_auth];
33980Sstevel@tonic-gate 		if (aalg == NULL || !ALG_VALID(aalg)) {
33993448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
34003448Sdh155122 			esp1dbg(espstack, ("Couldn't find auth alg #%d.\n",
34010Sstevel@tonic-gate 			    assoc->sadb_sa_auth));
34020Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
34030Sstevel@tonic-gate 			return (EINVAL);
34040Sstevel@tonic-gate 		}
34052810Smarkfen 
34062810Smarkfen 		/*
34072810Smarkfen 		 * Sanity check key sizes.
34082810Smarkfen 		 * Note: It's not possible to use SADB_AALG_NONE because
34092810Smarkfen 		 * this auth_alg is not defined with ALG_FLAG_VALID. If this
34102810Smarkfen 		 * ever changes, the same check for SADB_AALG_NONE and
34112810Smarkfen 		 * a auth_key != NULL should be made here ( see below).
34122810Smarkfen 		 */
34130Sstevel@tonic-gate 		if (!ipsec_valid_key_size(akey->sadb_key_bits, aalg)) {
34143448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
34150Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
34160Sstevel@tonic-gate 			return (EINVAL);
34170Sstevel@tonic-gate 		}
34182810Smarkfen 		ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
34190Sstevel@tonic-gate 
34200Sstevel@tonic-gate 		/* check key and fix parity if needed */
34210Sstevel@tonic-gate 		if (ipsec_check_key(aalg->alg_mech_type, akey, B_TRUE,
34220Sstevel@tonic-gate 		    diagnostic) != 0) {
34233448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
34240Sstevel@tonic-gate 			return (EINVAL);
34250Sstevel@tonic-gate 		}
34260Sstevel@tonic-gate 	}
34270Sstevel@tonic-gate 
34280Sstevel@tonic-gate 	/*
34290Sstevel@tonic-gate 	 * Then locate the encryption algorithm.
34300Sstevel@tonic-gate 	 */
34310Sstevel@tonic-gate 	if (ekey != NULL) {
34320Sstevel@tonic-gate 		ipsec_alginfo_t *ealg;
34330Sstevel@tonic-gate 
34343448Sdh155122 		ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR]
34353448Sdh155122 		    [assoc->sadb_sa_encrypt];
34360Sstevel@tonic-gate 		if (ealg == NULL || !ALG_VALID(ealg)) {
34373448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
34383448Sdh155122 			esp1dbg(espstack, ("Couldn't find encr alg #%d.\n",
34390Sstevel@tonic-gate 			    assoc->sadb_sa_encrypt));
34400Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
34410Sstevel@tonic-gate 			return (EINVAL);
34420Sstevel@tonic-gate 		}
34432810Smarkfen 
34442810Smarkfen 		/*
34452810Smarkfen 		 * Sanity check key sizes. If the encryption algorithm is
34462810Smarkfen 		 * SADB_EALG_NULL but the encryption key is NOT
34472810Smarkfen 		 * NULL then complain.
34482810Smarkfen 		 */
34492810Smarkfen 		if ((assoc->sadb_sa_encrypt == SADB_EALG_NULL) ||
34502810Smarkfen 		    (!ipsec_valid_key_size(ekey->sadb_key_bits, ealg))) {
34513448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
34520Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_EKEYBITS;
34530Sstevel@tonic-gate 			return (EINVAL);
34540Sstevel@tonic-gate 		}
34552810Smarkfen 		ASSERT(ealg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 		/* check key */
34580Sstevel@tonic-gate 		if (ipsec_check_key(ealg->alg_mech_type, ekey, B_FALSE,
34590Sstevel@tonic-gate 		    diagnostic) != 0) {
34603448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
34610Sstevel@tonic-gate 			return (EINVAL);
34620Sstevel@tonic-gate 		}
34630Sstevel@tonic-gate 	}
34643448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
34650Sstevel@tonic-gate 
34663055Sdanmcd 	return (esp_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
3467*4987Sdanmcd 	    diagnostic, espstack));
34680Sstevel@tonic-gate }
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate /*
34710Sstevel@tonic-gate  * Update a security association.  Updates come in two varieties.  The first
34720Sstevel@tonic-gate  * is an update of lifetimes on a non-larval SA.  The second is an update of
34730Sstevel@tonic-gate  * a larval SA, which ends up looking a lot more like an add.
34740Sstevel@tonic-gate  */
34750Sstevel@tonic-gate static int
34763448Sdh155122 esp_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
34773448Sdh155122     ipsecesp_stack_t *espstack)
34780Sstevel@tonic-gate {
34790Sstevel@tonic-gate 	sadb_address_t *dstext =
34800Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
34810Sstevel@tonic-gate 	struct sockaddr_in *sin;
34820Sstevel@tonic-gate 
34830Sstevel@tonic-gate 	if (dstext == NULL) {
34840Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
34850Sstevel@tonic-gate 		return (EINVAL);
34860Sstevel@tonic-gate 	}
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 	sin = (struct sockaddr_in *)(dstext + 1);
34890Sstevel@tonic-gate 	return (sadb_update_sa(mp, ksi,
3490*4987Sdanmcd 	    (sin->sin_family == AF_INET6) ? &espstack->esp_sadb.s_v6 :
3491*4987Sdanmcd 	    &espstack->esp_sadb.s_v4, diagnostic, espstack->esp_pfkey_q,
3492*4987Sdanmcd 	    esp_add_sa, espstack->ipsecesp_netstack));
34930Sstevel@tonic-gate }
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate /*
34960Sstevel@tonic-gate  * Delete a security association.  This is REALLY likely to be code common to
34970Sstevel@tonic-gate  * both AH and ESP.  Find the association, then unlink it.
34980Sstevel@tonic-gate  */
34990Sstevel@tonic-gate static int
35003448Sdh155122 esp_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
35013448Sdh155122     ipsecesp_stack_t *espstack)
35020Sstevel@tonic-gate {
35030Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
35040Sstevel@tonic-gate 	sadb_address_t *dstext =
35050Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
35060Sstevel@tonic-gate 	sadb_address_t *srcext =
35070Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
35080Sstevel@tonic-gate 	struct sockaddr_in *sin;
35090Sstevel@tonic-gate 
35100Sstevel@tonic-gate 	if (assoc == NULL) {
35110Sstevel@tonic-gate 		if (dstext != NULL) {
35120Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(dstext + 1);
35130Sstevel@tonic-gate 		} else if (srcext != NULL) {
35140Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(srcext + 1);
35150Sstevel@tonic-gate 		} else {
35160Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
35170Sstevel@tonic-gate 			return (EINVAL);
35180Sstevel@tonic-gate 		}
35193055Sdanmcd 		return (sadb_purge_sa(mp, ksi,
35203448Sdh155122 		    (sin->sin_family == AF_INET6) ? &espstack->esp_sadb.s_v6 :
35213448Sdh155122 		    &espstack->esp_sadb.s_v4, espstack->esp_pfkey_q,
35223448Sdh155122 		    espstack->esp_sadb.s_ip_q));
35230Sstevel@tonic-gate 	}
35240Sstevel@tonic-gate 
35253448Sdh155122 	return (sadb_del_sa(mp, ksi, &espstack->esp_sadb, diagnostic,
3526*4987Sdanmcd 	    espstack->esp_pfkey_q));
35270Sstevel@tonic-gate }
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate /*
35300Sstevel@tonic-gate  * Convert the entire contents of all of ESP's SA tables into PF_KEY SADB_DUMP
35310Sstevel@tonic-gate  * messages.
35320Sstevel@tonic-gate  */
35330Sstevel@tonic-gate static void
35343448Sdh155122 esp_dump(mblk_t *mp, keysock_in_t *ksi, ipsecesp_stack_t *espstack)
35350Sstevel@tonic-gate {
35360Sstevel@tonic-gate 	int error;
35370Sstevel@tonic-gate 	sadb_msg_t *samsg;
35380Sstevel@tonic-gate 
35390Sstevel@tonic-gate 	/*
35400Sstevel@tonic-gate 	 * Dump each fanout, bailing if error is non-zero.
35410Sstevel@tonic-gate 	 */
35420Sstevel@tonic-gate 
35433448Sdh155122 	error = sadb_dump(espstack->esp_pfkey_q, mp, ksi->ks_in_serial,
35443448Sdh155122 	    &espstack->esp_sadb.s_v4);
35450Sstevel@tonic-gate 	if (error != 0)
35460Sstevel@tonic-gate 		goto bail;
35470Sstevel@tonic-gate 
35483448Sdh155122 	error = sadb_dump(espstack->esp_pfkey_q, mp, ksi->ks_in_serial,
35493448Sdh155122 	    &espstack->esp_sadb.s_v6);
35500Sstevel@tonic-gate bail:
35510Sstevel@tonic-gate 	ASSERT(mp->b_cont != NULL);
35520Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
35530Sstevel@tonic-gate 	samsg->sadb_msg_errno = (uint8_t)error;
35543448Sdh155122 	sadb_pfkey_echo(espstack->esp_pfkey_q, mp,
35553448Sdh155122 	    (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL);
35560Sstevel@tonic-gate }
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate /*
35593055Sdanmcd  * First-cut reality check for an inbound PF_KEY message.
35603055Sdanmcd  */
35613055Sdanmcd static boolean_t
35623448Sdh155122 esp_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
35633448Sdh155122     ipsecesp_stack_t *espstack)
35643055Sdanmcd {
35653055Sdanmcd 	int diagnostic;
35663055Sdanmcd 
35673055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
35683055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
35693055Sdanmcd 		goto badmsg;
35703055Sdanmcd 	}
35713055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
35723055Sdanmcd 	    ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
35733055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
35743055Sdanmcd 		goto badmsg;
35753055Sdanmcd 	}
35763055Sdanmcd 	return (B_FALSE);	/* False ==> no failures */
35773055Sdanmcd 
35783055Sdanmcd badmsg:
35793448Sdh155122 	sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL, diagnostic,
35803055Sdanmcd 	    ksi->ks_in_serial);
35813055Sdanmcd 	return (B_TRUE);	/* True ==> failures */
35823055Sdanmcd }
35833055Sdanmcd 
35843055Sdanmcd /*
35850Sstevel@tonic-gate  * ESP parsing of PF_KEY messages.  Keysock did most of the really silly
35860Sstevel@tonic-gate  * error cases.  What I receive is a fully-formed, syntactically legal
35870Sstevel@tonic-gate  * PF_KEY message.  I then need to check semantics...
35880Sstevel@tonic-gate  *
35890Sstevel@tonic-gate  * This code may become common to AH and ESP.  Stay tuned.
35900Sstevel@tonic-gate  *
35910Sstevel@tonic-gate  * I also make the assumption that db_ref's are cool.  If this assumption
35920Sstevel@tonic-gate  * is wrong, this means that someone other than keysock or me has been
35930Sstevel@tonic-gate  * mucking with PF_KEY messages.
35940Sstevel@tonic-gate  */
35950Sstevel@tonic-gate static void
35963448Sdh155122 esp_parse_pfkey(mblk_t *mp, ipsecesp_stack_t *espstack)
35970Sstevel@tonic-gate {
35980Sstevel@tonic-gate 	mblk_t *msg = mp->b_cont;
35990Sstevel@tonic-gate 	sadb_msg_t *samsg;
36000Sstevel@tonic-gate 	keysock_in_t *ksi;
36010Sstevel@tonic-gate 	int error;
36020Sstevel@tonic-gate 	int diagnostic = SADB_X_DIAGNOSTIC_NONE;
36030Sstevel@tonic-gate 
36040Sstevel@tonic-gate 	ASSERT(msg != NULL);
36053448Sdh155122 
36060Sstevel@tonic-gate 	samsg = (sadb_msg_t *)msg->b_rptr;
36070Sstevel@tonic-gate 	ksi = (keysock_in_t *)mp->b_rptr;
36080Sstevel@tonic-gate 
36090Sstevel@tonic-gate 	/*
36100Sstevel@tonic-gate 	 * If applicable, convert unspecified AF_INET6 to unspecified
36113055Sdanmcd 	 * AF_INET.  And do other address reality checks.
36120Sstevel@tonic-gate 	 */
36133448Sdh155122 	if (!sadb_addrfix(ksi, espstack->esp_pfkey_q, mp,
36143448Sdh155122 	    espstack->ipsecesp_netstack) ||
36153448Sdh155122 	    esp_pfkey_reality_failures(mp, ksi, espstack)) {
36163055Sdanmcd 		return;
36173055Sdanmcd 	}
36180Sstevel@tonic-gate 
36190Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
36200Sstevel@tonic-gate 	case SADB_ADD:
36213448Sdh155122 		error = esp_add_sa(mp, ksi, &diagnostic,
36223448Sdh155122 		    espstack->ipsecesp_netstack);
36230Sstevel@tonic-gate 		if (error != 0) {
36243448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
36253448Sdh155122 			    diagnostic, ksi->ks_in_serial);
36260Sstevel@tonic-gate 		}
36270Sstevel@tonic-gate 		/* else esp_add_sa() took care of things. */
36280Sstevel@tonic-gate 		break;
36290Sstevel@tonic-gate 	case SADB_DELETE:
36303448Sdh155122 		error = esp_del_sa(mp, ksi, &diagnostic, espstack);
36310Sstevel@tonic-gate 		if (error != 0) {
36323448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
36333448Sdh155122 			    diagnostic, ksi->ks_in_serial);
36340Sstevel@tonic-gate 		}
36350Sstevel@tonic-gate 		/* Else esp_del_sa() took care of things. */
36360Sstevel@tonic-gate 		break;
36370Sstevel@tonic-gate 	case SADB_GET:
36383448Sdh155122 		error = sadb_get_sa(mp, ksi, &espstack->esp_sadb, &diagnostic,
36393448Sdh155122 		    espstack->esp_pfkey_q);
36400Sstevel@tonic-gate 		if (error != 0) {
36413448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
36423448Sdh155122 			    diagnostic, ksi->ks_in_serial);
36430Sstevel@tonic-gate 		}
36440Sstevel@tonic-gate 		/* Else sadb_get_sa() took care of things. */
36450Sstevel@tonic-gate 		break;
36460Sstevel@tonic-gate 	case SADB_FLUSH:
36473448Sdh155122 		sadbp_flush(&espstack->esp_sadb, espstack->ipsecesp_netstack);
36483448Sdh155122 		sadb_pfkey_echo(espstack->esp_pfkey_q, mp, samsg, ksi, NULL);
36490Sstevel@tonic-gate 		break;
36500Sstevel@tonic-gate 	case SADB_REGISTER:
36510Sstevel@tonic-gate 		/*
36520Sstevel@tonic-gate 		 * Hmmm, let's do it!  Check for extensions (there should
36530Sstevel@tonic-gate 		 * be none), extract the fields, call esp_register_out(),
36540Sstevel@tonic-gate 		 * then either free or report an error.
36550Sstevel@tonic-gate 		 *
36560Sstevel@tonic-gate 		 * Keysock takes care of the PF_KEY bookkeeping for this.
36570Sstevel@tonic-gate 		 */
36580Sstevel@tonic-gate 		if (esp_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
36593448Sdh155122 		    ksi->ks_in_serial, espstack)) {
36600Sstevel@tonic-gate 			freemsg(mp);
36610Sstevel@tonic-gate 		} else {
36620Sstevel@tonic-gate 			/*
36630Sstevel@tonic-gate 			 * Only way this path hits is if there is a memory
36640Sstevel@tonic-gate 			 * failure.  It will not return B_FALSE because of
36650Sstevel@tonic-gate 			 * lack of esp_pfkey_q if I am in wput().
36660Sstevel@tonic-gate 			 */
36673448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, ENOMEM,
36683448Sdh155122 			    diagnostic, ksi->ks_in_serial);
36690Sstevel@tonic-gate 		}
36700Sstevel@tonic-gate 		break;
36710Sstevel@tonic-gate 	case SADB_UPDATE:
36720Sstevel@tonic-gate 		/*
36730Sstevel@tonic-gate 		 * Find a larval, if not there, find a full one and get
36740Sstevel@tonic-gate 		 * strict.
36750Sstevel@tonic-gate 		 */
36763448Sdh155122 		error = esp_update_sa(mp, ksi, &diagnostic, espstack);
36770Sstevel@tonic-gate 		if (error != 0) {
36783448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
36793448Sdh155122 			    diagnostic, ksi->ks_in_serial);
36800Sstevel@tonic-gate 		}
36810Sstevel@tonic-gate 		/* else esp_update_sa() took care of things. */
36820Sstevel@tonic-gate 		break;
36830Sstevel@tonic-gate 	case SADB_GETSPI:
36840Sstevel@tonic-gate 		/*
36850Sstevel@tonic-gate 		 * Reserve a new larval entry.
36860Sstevel@tonic-gate 		 */
36873448Sdh155122 		esp_getspi(mp, ksi, espstack);
36880Sstevel@tonic-gate 		break;
36890Sstevel@tonic-gate 	case SADB_ACQUIRE:
36900Sstevel@tonic-gate 		/*
36910Sstevel@tonic-gate 		 * Find larval and/or ACQUIRE record and kill it (them), I'm
36920Sstevel@tonic-gate 		 * most likely an error.  Inbound ACQUIRE messages should only
36930Sstevel@tonic-gate 		 * have the base header.
36940Sstevel@tonic-gate 		 */
36953448Sdh155122 		sadb_in_acquire(samsg, &espstack->esp_sadb,
36963448Sdh155122 		    espstack->esp_pfkey_q, espstack->ipsecesp_netstack);
36970Sstevel@tonic-gate 		freemsg(mp);
36980Sstevel@tonic-gate 		break;
36990Sstevel@tonic-gate 	case SADB_DUMP:
37000Sstevel@tonic-gate 		/*
37010Sstevel@tonic-gate 		 * Dump all entries.
37020Sstevel@tonic-gate 		 */
37033448Sdh155122 		esp_dump(mp, ksi, espstack);
37040Sstevel@tonic-gate 		/* esp_dump will take care of the return message, etc. */
37050Sstevel@tonic-gate 		break;
37060Sstevel@tonic-gate 	case SADB_EXPIRE:
37070Sstevel@tonic-gate 		/* Should never reach me. */
37083448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, EOPNOTSUPP,
37093448Sdh155122 		    diagnostic, ksi->ks_in_serial);
37100Sstevel@tonic-gate 		break;
37110Sstevel@tonic-gate 	default:
37123448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL,
37130Sstevel@tonic-gate 		    SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
37140Sstevel@tonic-gate 		break;
37150Sstevel@tonic-gate 	}
37160Sstevel@tonic-gate }
37170Sstevel@tonic-gate 
37180Sstevel@tonic-gate /*
37190Sstevel@tonic-gate  * Handle case where PF_KEY says it can't find a keysock for one of my
37200Sstevel@tonic-gate  * ACQUIRE messages.
37210Sstevel@tonic-gate  */
37220Sstevel@tonic-gate static void
37233448Sdh155122 esp_keysock_no_socket(mblk_t *mp, ipsecesp_stack_t *espstack)
37240Sstevel@tonic-gate {
37250Sstevel@tonic-gate 	sadb_msg_t *samsg;
37260Sstevel@tonic-gate 	keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
37270Sstevel@tonic-gate 
37280Sstevel@tonic-gate 	if (mp->b_cont == NULL) {
37290Sstevel@tonic-gate 		freemsg(mp);
37300Sstevel@tonic-gate 		return;
37310Sstevel@tonic-gate 	}
37320Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
37330Sstevel@tonic-gate 
37340Sstevel@tonic-gate 	/*
37350Sstevel@tonic-gate 	 * If keysock can't find any registered, delete the acquire record
37360Sstevel@tonic-gate 	 * immediately, and handle errors.
37370Sstevel@tonic-gate 	 */
37380Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_ACQUIRE) {
37390Sstevel@tonic-gate 		samsg->sadb_msg_errno = kse->ks_err_errno;
37400Sstevel@tonic-gate 		samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
37410Sstevel@tonic-gate 		/*
37420Sstevel@tonic-gate 		 * Use the write-side of the esp_pfkey_q, in case there is
37430Sstevel@tonic-gate 		 * no esp_sadb.s_ip_q.
37440Sstevel@tonic-gate 		 */
37453448Sdh155122 		sadb_in_acquire(samsg, &espstack->esp_sadb,
37463448Sdh155122 		    WR(espstack->esp_pfkey_q), espstack->ipsecesp_netstack);
37470Sstevel@tonic-gate 	}
37480Sstevel@tonic-gate 
37490Sstevel@tonic-gate 	freemsg(mp);
37500Sstevel@tonic-gate }
37510Sstevel@tonic-gate 
37520Sstevel@tonic-gate /*
37530Sstevel@tonic-gate  * ESP module write put routine.
37540Sstevel@tonic-gate  */
37550Sstevel@tonic-gate static void
37560Sstevel@tonic-gate ipsecesp_wput(queue_t *q, mblk_t *mp)
37570Sstevel@tonic-gate {
37580Sstevel@tonic-gate 	ipsec_info_t *ii;
37590Sstevel@tonic-gate 	struct iocblk *iocp;
37603448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
37613448Sdh155122 
37623448Sdh155122 	esp3dbg(espstack, ("In esp_wput().\n"));
37630Sstevel@tonic-gate 
37640Sstevel@tonic-gate 	/* NOTE: Each case must take care of freeing or passing mp. */
37650Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
37660Sstevel@tonic-gate 	case M_CTL:
37670Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
37680Sstevel@tonic-gate 			/* Not big enough message. */
37690Sstevel@tonic-gate 			freemsg(mp);
37700Sstevel@tonic-gate 			break;
37710Sstevel@tonic-gate 		}
37720Sstevel@tonic-gate 		ii = (ipsec_info_t *)mp->b_rptr;
37730Sstevel@tonic-gate 
37740Sstevel@tonic-gate 		switch (ii->ipsec_info_type) {
37750Sstevel@tonic-gate 		case KEYSOCK_OUT_ERR:
37763448Sdh155122 			esp1dbg(espstack, ("Got KEYSOCK_OUT_ERR message.\n"));
37773448Sdh155122 			esp_keysock_no_socket(mp, espstack);
37780Sstevel@tonic-gate 			break;
37790Sstevel@tonic-gate 		case KEYSOCK_IN:
37803448Sdh155122 			ESP_BUMP_STAT(espstack, keysock_in);
37813448Sdh155122 			esp3dbg(espstack, ("Got KEYSOCK_IN message.\n"));
37823055Sdanmcd 
37833055Sdanmcd 			/* Parse the message. */
37843448Sdh155122 			esp_parse_pfkey(mp, espstack);
37850Sstevel@tonic-gate 			break;
37860Sstevel@tonic-gate 		case KEYSOCK_HELLO:
37873448Sdh155122 			sadb_keysock_hello(&espstack->esp_pfkey_q, q, mp,
37883448Sdh155122 			    esp_ager, (void *)espstack, &espstack->esp_event,
37893448Sdh155122 			    SADB_SATYPE_ESP);
37900Sstevel@tonic-gate 			break;
37910Sstevel@tonic-gate 		default:
37923448Sdh155122 			esp2dbg(espstack, ("Got M_CTL from above of 0x%x.\n",
37930Sstevel@tonic-gate 			    ii->ipsec_info_type));
37940Sstevel@tonic-gate 			freemsg(mp);
37950Sstevel@tonic-gate 			break;
37960Sstevel@tonic-gate 		}
37970Sstevel@tonic-gate 		break;
37980Sstevel@tonic-gate 	case M_IOCTL:
37990Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
38000Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
38010Sstevel@tonic-gate 		case ND_SET:
38020Sstevel@tonic-gate 		case ND_GET:
38033448Sdh155122 			if (nd_getset(q, espstack->ipsecesp_g_nd, mp)) {
38040Sstevel@tonic-gate 				qreply(q, mp);
38050Sstevel@tonic-gate 				return;
38060Sstevel@tonic-gate 			} else {
38070Sstevel@tonic-gate 				iocp->ioc_error = ENOENT;
38080Sstevel@tonic-gate 			}
38090Sstevel@tonic-gate 			/* FALLTHRU */
38100Sstevel@tonic-gate 		default:
38110Sstevel@tonic-gate 			/* We really don't support any other ioctls, do we? */
38120Sstevel@tonic-gate 
38130Sstevel@tonic-gate 			/* Return EINVAL */
38140Sstevel@tonic-gate 			if (iocp->ioc_error != ENOENT)
38150Sstevel@tonic-gate 				iocp->ioc_error = EINVAL;
38160Sstevel@tonic-gate 			iocp->ioc_count = 0;
38170Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
38180Sstevel@tonic-gate 			qreply(q, mp);
38190Sstevel@tonic-gate 			return;
38200Sstevel@tonic-gate 		}
38210Sstevel@tonic-gate 	default:
38223448Sdh155122 		esp3dbg(espstack,
38233448Sdh155122 		    ("Got default message, type %d, passing to IP.\n",
38240Sstevel@tonic-gate 		    mp->b_datap->db_type));
38250Sstevel@tonic-gate 		putnext(q, mp);
38260Sstevel@tonic-gate 	}
38270Sstevel@tonic-gate }
38280Sstevel@tonic-gate 
38290Sstevel@tonic-gate /*
38300Sstevel@tonic-gate  * Process an outbound ESP packet that can be accelerated by a IPsec
38310Sstevel@tonic-gate  * hardware acceleration capable Provider.
38320Sstevel@tonic-gate  * The caller already inserted and initialized the ESP header.
38330Sstevel@tonic-gate  * This function allocates a tagging M_CTL, and adds room at the end
38340Sstevel@tonic-gate  * of the packet to hold the ICV if authentication is needed.
38350Sstevel@tonic-gate  *
38360Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
38370Sstevel@tonic-gate  * mblk chain ipsec_out.
38380Sstevel@tonic-gate  */
38390Sstevel@tonic-gate static ipsec_status_t
38400Sstevel@tonic-gate esp_outbound_accelerated(mblk_t *ipsec_out, uint_t icv_len)
38410Sstevel@tonic-gate {
38420Sstevel@tonic-gate 	ipsec_out_t *io;
38430Sstevel@tonic-gate 	mblk_t *lastmp;
38443448Sdh155122 	netstack_t	*ns;
38453448Sdh155122 	ipsecesp_stack_t *espstack;
38463448Sdh155122 	ipsec_stack_t	*ipss;
38470Sstevel@tonic-gate 
38480Sstevel@tonic-gate 	io = (ipsec_out_t *)ipsec_out->b_rptr;
38493448Sdh155122 	ns = io->ipsec_out_ns;
38503448Sdh155122 	espstack = ns->netstack_ipsecesp;
38513448Sdh155122 	ipss = ns->netstack_ipsec;
38523448Sdh155122 
38533448Sdh155122 	ESP_BUMP_STAT(espstack, out_accelerated);
38540Sstevel@tonic-gate 
38550Sstevel@tonic-gate 	/* mark packet as being accelerated in IPSEC_OUT */
38560Sstevel@tonic-gate 	ASSERT(io->ipsec_out_accelerated == B_FALSE);
38570Sstevel@tonic-gate 	io->ipsec_out_accelerated = B_TRUE;
38580Sstevel@tonic-gate 
38590Sstevel@tonic-gate 	/*
38600Sstevel@tonic-gate 	 * add room at the end of the packet for the ICV if needed
38610Sstevel@tonic-gate 	 */
38620Sstevel@tonic-gate 	if (icv_len > 0) {
38630Sstevel@tonic-gate 		/* go to last mblk */
38640Sstevel@tonic-gate 		lastmp = ipsec_out;	/* For following while loop. */
38650Sstevel@tonic-gate 		do {
38660Sstevel@tonic-gate 			lastmp = lastmp->b_cont;
38670Sstevel@tonic-gate 		} while (lastmp->b_cont != NULL);
38680Sstevel@tonic-gate 
38690Sstevel@tonic-gate 		/* if not enough available room, allocate new mblk */
38700Sstevel@tonic-gate 		if ((lastmp->b_wptr + icv_len) > lastmp->b_datap->db_lim) {
38710Sstevel@tonic-gate 			lastmp->b_cont = allocb(icv_len, BPRI_HI);
38720Sstevel@tonic-gate 			if (lastmp->b_cont == NULL) {
38733448Sdh155122 				ESP_BUMP_STAT(espstack, out_discards);
38740Sstevel@tonic-gate 				ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
38753448Sdh155122 				    DROPPER(ipss, ipds_esp_nomem),
38763448Sdh155122 				    &espstack->esp_dropper);
38770Sstevel@tonic-gate 				return (IPSEC_STATUS_FAILED);
38780Sstevel@tonic-gate 			}
38790Sstevel@tonic-gate 			lastmp = lastmp->b_cont;
38800Sstevel@tonic-gate 		}
38810Sstevel@tonic-gate 		lastmp->b_wptr += icv_len;
38820Sstevel@tonic-gate 	}
38830Sstevel@tonic-gate 
38840Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
38850Sstevel@tonic-gate }
38860Sstevel@tonic-gate 
38870Sstevel@tonic-gate /*
38880Sstevel@tonic-gate  * Process an inbound accelerated ESP packet.
38890Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
38900Sstevel@tonic-gate  * mblk chain ipsec_in.
38910Sstevel@tonic-gate  */
38920Sstevel@tonic-gate static ipsec_status_t
38930Sstevel@tonic-gate esp_inbound_accelerated(mblk_t *ipsec_in, mblk_t *data_mp, boolean_t isv4,
38940Sstevel@tonic-gate     ipsa_t *assoc)
38950Sstevel@tonic-gate {
38963448Sdh155122 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
38970Sstevel@tonic-gate 	mblk_t *hada_mp;
38980Sstevel@tonic-gate 	uint32_t icv_len = 0;
38990Sstevel@tonic-gate 	da_ipsec_t *hada;
39000Sstevel@tonic-gate 	ipha_t *ipha;
39010Sstevel@tonic-gate 	ip6_t *ip6h;
39020Sstevel@tonic-gate 	kstat_named_t *counter;
39033448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
39043448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
39053448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
39063448Sdh155122 
39073448Sdh155122 	ESP_BUMP_STAT(espstack, in_accelerated);
39083448Sdh155122 
39090Sstevel@tonic-gate 	hada_mp = ii->ipsec_in_da;
39100Sstevel@tonic-gate 	ASSERT(hada_mp != NULL);
39110Sstevel@tonic-gate 	hada = (da_ipsec_t *)hada_mp->b_rptr;
39120Sstevel@tonic-gate 
39130Sstevel@tonic-gate 	/*
39140Sstevel@tonic-gate 	 * We only support one level of decapsulation in hardware, so
39150Sstevel@tonic-gate 	 * nuke the pointer.
39160Sstevel@tonic-gate 	 */
39170Sstevel@tonic-gate 	ii->ipsec_in_da = NULL;
39180Sstevel@tonic-gate 	ii->ipsec_in_accelerated = B_FALSE;
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) {
39210Sstevel@tonic-gate 		/*
39220Sstevel@tonic-gate 		 * ESP with authentication. We expect the Provider to have
39230Sstevel@tonic-gate 		 * computed the ICV and placed it in the hardware acceleration
39240Sstevel@tonic-gate 		 * data attributes.
39250Sstevel@tonic-gate 		 *
39260Sstevel@tonic-gate 		 * Extract ICV length from attributes M_CTL and sanity check
39270Sstevel@tonic-gate 		 * its value. We allow the mblk to be smaller than da_ipsec_t
39280Sstevel@tonic-gate 		 * for a small ICV, as long as the entire ICV fits within the
39290Sstevel@tonic-gate 		 * mblk.
39300Sstevel@tonic-gate 		 *
39310Sstevel@tonic-gate 		 * Also ensures that the ICV length computed by Provider
39320Sstevel@tonic-gate 		 * corresponds to the ICV length of the agorithm specified by
39330Sstevel@tonic-gate 		 * the SA.
39340Sstevel@tonic-gate 		 */
39350Sstevel@tonic-gate 		icv_len = hada->da_icv_len;
39360Sstevel@tonic-gate 		if ((icv_len != assoc->ipsa_mac_len) ||
39370Sstevel@tonic-gate 		    (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) <
3938*4987Sdanmcd 		    (sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) {
39390Sstevel@tonic-gate 			esp0dbg(("esp_inbound_accelerated: "
39400Sstevel@tonic-gate 			    "ICV len (%u) incorrect or mblk too small (%u)\n",
39410Sstevel@tonic-gate 			    icv_len, (uint32_t)(MBLKL(hada_mp))));
39423448Sdh155122 			counter = DROPPER(ipss, ipds_esp_bad_auth);
39430Sstevel@tonic-gate 			goto esp_in_discard;
39440Sstevel@tonic-gate 		}
39450Sstevel@tonic-gate 	}
39460Sstevel@tonic-gate 
39470Sstevel@tonic-gate 	/* get pointers to IP header */
39480Sstevel@tonic-gate 	if (isv4) {
39490Sstevel@tonic-gate 		ipha = (ipha_t *)data_mp->b_rptr;
39500Sstevel@tonic-gate 	} else {
39510Sstevel@tonic-gate 		ip6h = (ip6_t *)data_mp->b_rptr;
39520Sstevel@tonic-gate 	}
39530Sstevel@tonic-gate 
39540Sstevel@tonic-gate 	/*
39550Sstevel@tonic-gate 	 * Compare ICV in ESP packet vs ICV computed by adapter.
39560Sstevel@tonic-gate 	 * We also remove the ICV from the end of the packet since
39570Sstevel@tonic-gate 	 * it will no longer be needed.
39580Sstevel@tonic-gate 	 *
39590Sstevel@tonic-gate 	 * Assume that esp_inbound() already ensured that the pkt
39600Sstevel@tonic-gate 	 * was in one mblk.
39610Sstevel@tonic-gate 	 */
39620Sstevel@tonic-gate 	ASSERT(data_mp->b_cont == NULL);
39630Sstevel@tonic-gate 	data_mp->b_wptr -= icv_len;
39640Sstevel@tonic-gate 	/* adjust IP header */
39650Sstevel@tonic-gate 	if (isv4)
39660Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) - icv_len);
39670Sstevel@tonic-gate 	else
39680Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - icv_len);
39690Sstevel@tonic-gate 	if (icv_len && bcmp(hada->da_icv, data_mp->b_wptr, icv_len)) {
39700Sstevel@tonic-gate 		int af;
39710Sstevel@tonic-gate 		void *addr;
39720Sstevel@tonic-gate 
39730Sstevel@tonic-gate 		if (isv4) {
39740Sstevel@tonic-gate 			addr = &ipha->ipha_dst;
39750Sstevel@tonic-gate 			af = AF_INET;
39760Sstevel@tonic-gate 		} else {
39770Sstevel@tonic-gate 			addr = &ip6h->ip6_dst;
39780Sstevel@tonic-gate 			af = AF_INET6;
39790Sstevel@tonic-gate 		}
39800Sstevel@tonic-gate 
39810Sstevel@tonic-gate 		/*
39820Sstevel@tonic-gate 		 * Log the event. Don't print to the console, block
39830Sstevel@tonic-gate 		 * potential denial-of-service attack.
39840Sstevel@tonic-gate 		 */
39853448Sdh155122 		ESP_BUMP_STAT(espstack, bad_auth);
39860Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
39870Sstevel@tonic-gate 		    "ESP Authentication failed spi %x, dst_addr %s",
39883448Sdh155122 		    assoc->ipsa_spi, addr, af, espstack->ipsecesp_netstack);
39893448Sdh155122 		counter = DROPPER(ipss, ipds_esp_bad_auth);
39900Sstevel@tonic-gate 		goto esp_in_discard;
39910Sstevel@tonic-gate 	}
39920Sstevel@tonic-gate 
39933448Sdh155122 	esp3dbg(espstack, ("esp_inbound_accelerated: ESP authentication "
3994*4987Sdanmcd 	    "succeeded, checking replay\n"));
39950Sstevel@tonic-gate 
39960Sstevel@tonic-gate 	ipsec_in->b_cont = data_mp;
39970Sstevel@tonic-gate 
39980Sstevel@tonic-gate 	/*
39990Sstevel@tonic-gate 	 * Remove ESP header and padding from packet.
40000Sstevel@tonic-gate 	 */
40010Sstevel@tonic-gate 	if (!esp_strip_header(data_mp, ii->ipsec_in_v4, assoc->ipsa_iv_len,
4002*4987Sdanmcd 	    &counter, espstack)) {
40033448Sdh155122 		esp1dbg(espstack, ("esp_inbound_accelerated: "
40040Sstevel@tonic-gate 		    "esp_strip_header() failed\n"));
40050Sstevel@tonic-gate 		goto esp_in_discard;
40060Sstevel@tonic-gate 	}
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate 	freeb(hada_mp);
40090Sstevel@tonic-gate 
40100Sstevel@tonic-gate 	/*
40110Sstevel@tonic-gate 	 * Account for usage..
40120Sstevel@tonic-gate 	 */
40130Sstevel@tonic-gate 	if (!esp_age_bytes(assoc, msgdsize(data_mp), B_TRUE)) {
40140Sstevel@tonic-gate 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
40153448Sdh155122 		ESP_BUMP_STAT(espstack, bytes_expired);
40163448Sdh155122 		IP_ESP_BUMP_STAT(ipss, in_discards);
40170Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
40180Sstevel@tonic-gate 		    "ESP association 0x%x, dst %s had bytes expire.\n",
40193448Sdh155122 		    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
40203448Sdh155122 		    espstack->ipsecesp_netstack);
40210Sstevel@tonic-gate 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
40223448Sdh155122 		    DROPPER(ipss, ipds_esp_bytes_expire),
40233448Sdh155122 		    &espstack->esp_dropper);
40240Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
40250Sstevel@tonic-gate 	}
40260Sstevel@tonic-gate 
40270Sstevel@tonic-gate 	/* done processing the packet */
40280Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate esp_in_discard:
40313448Sdh155122 	IP_ESP_BUMP_STAT(ipss, in_discards);
40320Sstevel@tonic-gate 	freeb(hada_mp);
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate 	ipsec_in->b_cont = data_mp;	/* For ip_drop_packet()'s sake... */
40353448Sdh155122 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter,
40363448Sdh155122 	    &espstack->esp_dropper);
40370Sstevel@tonic-gate 
40380Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
40390Sstevel@tonic-gate }
40400Sstevel@tonic-gate 
40410Sstevel@tonic-gate /*
40420Sstevel@tonic-gate  * Wrapper to allow IP to trigger an ESP association failure message
40430Sstevel@tonic-gate  * during inbound SA selection.
40440Sstevel@tonic-gate  */
40450Sstevel@tonic-gate void
40460Sstevel@tonic-gate ipsecesp_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
40473448Sdh155122     uint32_t spi, void *addr, int af, ipsecesp_stack_t *espstack)
40480Sstevel@tonic-gate {
40493448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
40503448Sdh155122 
40513448Sdh155122 	if (espstack->ipsecesp_log_unknown_spi) {
40520Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
40533448Sdh155122 		    addr, af, espstack->ipsecesp_netstack);
40540Sstevel@tonic-gate 	}
40550Sstevel@tonic-gate 
40563448Sdh155122 	ip_drop_packet(mp, B_TRUE, NULL, NULL,
40573448Sdh155122 	    DROPPER(ipss, ipds_esp_no_sa),
40583448Sdh155122 	    &espstack->esp_dropper);
40590Sstevel@tonic-gate }
40600Sstevel@tonic-gate 
40610Sstevel@tonic-gate /*
40620Sstevel@tonic-gate  * Initialize the ESP input and output processing functions.
40630Sstevel@tonic-gate  */
40640Sstevel@tonic-gate void
40650Sstevel@tonic-gate ipsecesp_init_funcs(ipsa_t *sa)
40660Sstevel@tonic-gate {
40670Sstevel@tonic-gate 	if (sa->ipsa_output_func == NULL)
40680Sstevel@tonic-gate 		sa->ipsa_output_func = esp_outbound;
40690Sstevel@tonic-gate 	if (sa->ipsa_input_func == NULL)
40700Sstevel@tonic-gate 		sa->ipsa_input_func = esp_inbound;
40710Sstevel@tonic-gate }
4072