xref: /onnv-gate/usr/src/uts/common/inet/ip/ipsecesp.c (revision 7073:2738ccb6c0fd)
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 /*
226352Swy83408  * Copyright 2008 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>
554987Sdanmcd #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"},
944987Sdanmcd 	{ 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
1214987Sdanmcd /* 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 
1374987Sdanmcd 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;
2077066Sdanmcd 	kstat_named_t esp_stat_sa_port_renumbers;
2080Sstevel@tonic-gate } esp_kstats_t;
2090Sstevel@tonic-gate 
2103448Sdh155122 /*
2113448Sdh155122  * espstack->esp_kstats is equal to espstack->esp_ksp->ks_data if
2123448Sdh155122  * kstat_create_netstack for espstack->esp_ksp succeeds, but when it
2133448Sdh155122  * fails, it will be NULL. Note this is done for all stack instances,
2143448Sdh155122  * so it *could* fail. hence a non-NULL checking is done for
2153448Sdh155122  * ESP_BUMP_STAT and ESP_DEBUMP_STAT
2163448Sdh155122  */
2173448Sdh155122 #define	ESP_BUMP_STAT(espstack, x)					\
2183448Sdh155122 do {									\
2193448Sdh155122 	if (espstack->esp_kstats != NULL)				\
2203448Sdh155122 		(espstack->esp_kstats->esp_stat_ ## x).value.ui64++;	\
2213448Sdh155122 _NOTE(CONSTCOND)							\
2223448Sdh155122 } while (0)
2233448Sdh155122 
2243448Sdh155122 #define	ESP_DEBUMP_STAT(espstack, x)					\
2253448Sdh155122 do {									\
2263448Sdh155122 	if (espstack->esp_kstats != NULL)				\
2273448Sdh155122 		(espstack->esp_kstats->esp_stat_ ## x).value.ui64--;	\
2283448Sdh155122 _NOTE(CONSTCOND)							\
2293448Sdh155122 } while (0)
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static int	esp_kstat_update(kstat_t *, int);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate static boolean_t
2343448Sdh155122 esp_kstat_init(ipsecesp_stack_t *espstack, netstackid_t stackid)
2350Sstevel@tonic-gate {
2363448Sdh155122 	espstack->esp_ksp = kstat_create_netstack("ipsecesp", 0, "esp_stat",
2373448Sdh155122 	    "net", KSTAT_TYPE_NAMED,
2383448Sdh155122 	    sizeof (esp_kstats_t) / sizeof (kstat_named_t),
2393448Sdh155122 	    KSTAT_FLAG_PERSISTENT, stackid);
2403448Sdh155122 
2413448Sdh155122 	if (espstack->esp_ksp == NULL || espstack->esp_ksp->ks_data == NULL)
2420Sstevel@tonic-gate 		return (B_FALSE);
2430Sstevel@tonic-gate 
2443448Sdh155122 	espstack->esp_kstats = espstack->esp_ksp->ks_data;
2453448Sdh155122 
2463448Sdh155122 	espstack->esp_ksp->ks_update = esp_kstat_update;
2473448Sdh155122 	espstack->esp_ksp->ks_private = (void *)(uintptr_t)stackid;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate #define	K64 KSTAT_DATA_UINT64
2503448Sdh155122 #define	KI(x) kstat_named_init(&(espstack->esp_kstats->esp_stat_##x), #x, K64)
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	KI(num_aalgs);
2530Sstevel@tonic-gate 	KI(num_ealgs);
2540Sstevel@tonic-gate 	KI(good_auth);
2550Sstevel@tonic-gate 	KI(bad_auth);
2560Sstevel@tonic-gate 	KI(bad_padding);
2570Sstevel@tonic-gate 	KI(replay_failures);
2580Sstevel@tonic-gate 	KI(replay_early_failures);
2590Sstevel@tonic-gate 	KI(keysock_in);
2600Sstevel@tonic-gate 	KI(out_requests);
2610Sstevel@tonic-gate 	KI(acquire_requests);
2620Sstevel@tonic-gate 	KI(bytes_expired);
2630Sstevel@tonic-gate 	KI(out_discards);
2640Sstevel@tonic-gate 	KI(in_accelerated);
2650Sstevel@tonic-gate 	KI(out_accelerated);
2660Sstevel@tonic-gate 	KI(noaccel);
2670Sstevel@tonic-gate 	KI(crypto_sync);
2680Sstevel@tonic-gate 	KI(crypto_async);
2690Sstevel@tonic-gate 	KI(crypto_failures);
2700Sstevel@tonic-gate 	KI(bad_decrypt);
2717066Sdanmcd 	KI(sa_port_renumbers);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate #undef KI
2740Sstevel@tonic-gate #undef K64
2750Sstevel@tonic-gate 
2763448Sdh155122 	kstat_install(espstack->esp_ksp);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	return (B_TRUE);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate static int
2820Sstevel@tonic-gate esp_kstat_update(kstat_t *kp, int rw)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 	esp_kstats_t *ekp;
2853448Sdh155122 	netstackid_t	stackid = (zoneid_t)(uintptr_t)kp->ks_private;
2863448Sdh155122 	netstack_t	*ns;
2873448Sdh155122 	ipsec_stack_t	*ipss;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if ((kp == NULL) || (kp->ks_data == NULL))
2900Sstevel@tonic-gate 		return (EIO);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
2930Sstevel@tonic-gate 		return (EACCES);
2940Sstevel@tonic-gate 
2953448Sdh155122 	ns = netstack_find_by_stackid(stackid);
2963448Sdh155122 	if (ns == NULL)
2973448Sdh155122 		return (-1);
2983448Sdh155122 	ipss = ns->netstack_ipsec;
2993448Sdh155122 	if (ipss == NULL) {
3003448Sdh155122 		netstack_rele(ns);
3013448Sdh155122 		return (-1);
3023448Sdh155122 	}
3030Sstevel@tonic-gate 	ekp = (esp_kstats_t *)kp->ks_data;
3043448Sdh155122 
3053448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
3063448Sdh155122 	ekp->esp_stat_num_aalgs.value.ui64 =
3073448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
3083448Sdh155122 	ekp->esp_stat_num_ealgs.value.ui64 =
3093448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
3103448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
3113448Sdh155122 
3123448Sdh155122 	netstack_rele(ns);
3130Sstevel@tonic-gate 	return (0);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate #ifdef DEBUG
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate  * Debug routine, useful to see pre-encryption data.
3190Sstevel@tonic-gate  */
3200Sstevel@tonic-gate static char *
3210Sstevel@tonic-gate dump_msg(mblk_t *mp)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	char tmp_str[3], tmp_line[256];
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	while (mp != NULL) {
3260Sstevel@tonic-gate 		unsigned char *ptr;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		printf("mblk address 0x%p, length %ld, db_ref %d "
3290Sstevel@tonic-gate 		    "type %d, base 0x%p, lim 0x%p\n",
3300Sstevel@tonic-gate 		    (void *) mp, (long)(mp->b_wptr - mp->b_rptr),
3310Sstevel@tonic-gate 		    mp->b_datap->db_ref, mp->b_datap->db_type,
3320Sstevel@tonic-gate 		    (void *)mp->b_datap->db_base, (void *)mp->b_datap->db_lim);
3330Sstevel@tonic-gate 		ptr = mp->b_rptr;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 		tmp_line[0] = '\0';
3360Sstevel@tonic-gate 		while (ptr < mp->b_wptr) {
3370Sstevel@tonic-gate 			uint_t diff;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 			diff = (ptr - mp->b_rptr);
3400Sstevel@tonic-gate 			if (!(diff & 0x1f)) {
3410Sstevel@tonic-gate 				if (strlen(tmp_line) > 0) {
3420Sstevel@tonic-gate 					printf("bytes: %s\n", tmp_line);
3430Sstevel@tonic-gate 					tmp_line[0] = '\0';
3440Sstevel@tonic-gate 				}
3450Sstevel@tonic-gate 			}
3460Sstevel@tonic-gate 			if (!(diff & 0x3))
3470Sstevel@tonic-gate 				(void) strcat(tmp_line, " ");
3480Sstevel@tonic-gate 			(void) sprintf(tmp_str, "%02x", *ptr);
3490Sstevel@tonic-gate 			(void) strcat(tmp_line, tmp_str);
3500Sstevel@tonic-gate 			ptr++;
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 		if (strlen(tmp_line) > 0)
3530Sstevel@tonic-gate 			printf("bytes: %s\n", tmp_line);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 		mp = mp->b_cont;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	return ("\n");
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate #else /* DEBUG */
3620Sstevel@tonic-gate static char *
3630Sstevel@tonic-gate dump_msg(mblk_t *mp)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	printf("Find value of mp %p.\n", mp);
3660Sstevel@tonic-gate 	return ("\n");
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate #endif /* DEBUG */
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * Don't have to lock age_interval, as only one thread will access it at
3720Sstevel@tonic-gate  * a time, because I control the one function that does with timeout().
3730Sstevel@tonic-gate  */
3740Sstevel@tonic-gate static void
3753448Sdh155122 esp_ager(void *arg)
3760Sstevel@tonic-gate {
3773448Sdh155122 	ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)arg;
3783448Sdh155122 	netstack_t	*ns = espstack->ipsecesp_netstack;
3790Sstevel@tonic-gate 	hrtime_t begin = gethrtime();
3800Sstevel@tonic-gate 
3813448Sdh155122 	sadb_ager(&espstack->esp_sadb.s_v4, espstack->esp_pfkey_q,
3823448Sdh155122 	    espstack->esp_sadb.s_ip_q, espstack->ipsecesp_reap_delay, ns);
3833448Sdh155122 	sadb_ager(&espstack->esp_sadb.s_v6, espstack->esp_pfkey_q,
3843448Sdh155122 	    espstack->esp_sadb.s_ip_q, espstack->ipsecesp_reap_delay, ns);
3853448Sdh155122 
3863448Sdh155122 	espstack->esp_event = sadb_retimeout(begin, espstack->esp_pfkey_q,
3873448Sdh155122 	    esp_ager, espstack,
3883448Sdh155122 	    &espstack->ipsecesp_age_interval, espstack->ipsecesp_age_int_max,
3893448Sdh155122 	    info.mi_idnum);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * Get an ESP NDD parameter.
3940Sstevel@tonic-gate  */
3950Sstevel@tonic-gate /* ARGSUSED */
3960Sstevel@tonic-gate static int
3970Sstevel@tonic-gate ipsecesp_param_get(q, mp, cp, cr)
3980Sstevel@tonic-gate 	queue_t	*q;
3990Sstevel@tonic-gate 	mblk_t	*mp;
4000Sstevel@tonic-gate 	caddr_t	cp;
4010Sstevel@tonic-gate 	cred_t *cr;
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	ipsecespparam_t	*ipsecesppa = (ipsecespparam_t *)cp;
4040Sstevel@tonic-gate 	uint_t value;
4053448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
4063448Sdh155122 
4073448Sdh155122 	mutex_enter(&espstack->ipsecesp_param_lock);
4080Sstevel@tonic-gate 	value = ipsecesppa->ipsecesp_param_value;
4093448Sdh155122 	mutex_exit(&espstack->ipsecesp_param_lock);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
4120Sstevel@tonic-gate 	return (0);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate  * This routine sets an NDD variable in a ipsecespparam_t structure.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate /* ARGSUSED */
4190Sstevel@tonic-gate static int
4200Sstevel@tonic-gate ipsecesp_param_set(q, mp, value, cp, cr)
4210Sstevel@tonic-gate 	queue_t	*q;
4220Sstevel@tonic-gate 	mblk_t	*mp;
4230Sstevel@tonic-gate 	char	*value;
4240Sstevel@tonic-gate 	caddr_t	cp;
4250Sstevel@tonic-gate 	cred_t *cr;
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate 	ulong_t	new_value;
4280Sstevel@tonic-gate 	ipsecespparam_t	*ipsecesppa = (ipsecespparam_t *)cp;
4293448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
4330Sstevel@tonic-gate 	 * required bounds.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
4360Sstevel@tonic-gate 	    new_value < ipsecesppa->ipsecesp_param_min ||
4370Sstevel@tonic-gate 	    new_value > ipsecesppa->ipsecesp_param_max) {
4380Sstevel@tonic-gate 		return (EINVAL);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	/* Set the new value */
4423448Sdh155122 	mutex_enter(&espstack->ipsecesp_param_lock);
4430Sstevel@tonic-gate 	ipsecesppa->ipsecesp_param_value = new_value;
4443448Sdh155122 	mutex_exit(&espstack->ipsecesp_param_lock);
4450Sstevel@tonic-gate 	return (0);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate  * Using lifetime NDD variables, fill in an extended combination's
4500Sstevel@tonic-gate  * lifetime information.
4510Sstevel@tonic-gate  */
4520Sstevel@tonic-gate void
4533448Sdh155122 ipsecesp_fill_defs(sadb_x_ecomb_t *ecomb, netstack_t *ns)
4540Sstevel@tonic-gate {
4553448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
4563448Sdh155122 
4573448Sdh155122 	ecomb->sadb_x_ecomb_soft_bytes = espstack->ipsecesp_default_soft_bytes;
4583448Sdh155122 	ecomb->sadb_x_ecomb_hard_bytes = espstack->ipsecesp_default_hard_bytes;
4593448Sdh155122 	ecomb->sadb_x_ecomb_soft_addtime =
4603448Sdh155122 	    espstack->ipsecesp_default_soft_addtime;
4613448Sdh155122 	ecomb->sadb_x_ecomb_hard_addtime =
4623448Sdh155122 	    espstack->ipsecesp_default_hard_addtime;
4633448Sdh155122 	ecomb->sadb_x_ecomb_soft_usetime =
4643448Sdh155122 	    espstack->ipsecesp_default_soft_usetime;
4653448Sdh155122 	ecomb->sadb_x_ecomb_hard_usetime =
4663448Sdh155122 	    espstack->ipsecesp_default_hard_usetime;
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate /*
4700Sstevel@tonic-gate  * Initialize things for ESP at module load time.
4710Sstevel@tonic-gate  */
4720Sstevel@tonic-gate boolean_t
4730Sstevel@tonic-gate ipsecesp_ddi_init(void)
4740Sstevel@tonic-gate {
4753448Sdh155122 	esp_taskq = taskq_create("esp_taskq", 1, minclsyspri,
4763448Sdh155122 	    IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0);
4773448Sdh155122 
4783448Sdh155122 	/*
4793448Sdh155122 	 * We want to be informed each time a stack is created or
4803448Sdh155122 	 * destroyed in the kernel, so we can maintain the
4813448Sdh155122 	 * set of ipsecesp_stack_t's.
4823448Sdh155122 	 */
4833448Sdh155122 	netstack_register(NS_IPSECESP, ipsecesp_stack_init, NULL,
4843448Sdh155122 	    ipsecesp_stack_fini);
4853448Sdh155122 
4863448Sdh155122 	return (B_TRUE);
4873448Sdh155122 }
4883448Sdh155122 
4893448Sdh155122 /*
4903448Sdh155122  * Walk through the param array specified registering each element with the
4913448Sdh155122  * named dispatch handler.
4923448Sdh155122  */
4933448Sdh155122 static boolean_t
4943448Sdh155122 ipsecesp_param_register(IDP *ndp, ipsecespparam_t *espp, int cnt)
4953448Sdh155122 {
4963448Sdh155122 	for (; cnt-- > 0; espp++) {
4970Sstevel@tonic-gate 		if (espp->ipsecesp_param_name != NULL &&
4980Sstevel@tonic-gate 		    espp->ipsecesp_param_name[0]) {
4993448Sdh155122 			if (!nd_load(ndp,
5003448Sdh155122 			    espp->ipsecesp_param_name,
5010Sstevel@tonic-gate 			    ipsecesp_param_get, ipsecesp_param_set,
5020Sstevel@tonic-gate 			    (caddr_t)espp)) {
5033448Sdh155122 				nd_free(ndp);
5040Sstevel@tonic-gate 				return (B_FALSE);
5050Sstevel@tonic-gate 			}
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 	return (B_TRUE);
5090Sstevel@tonic-gate }
5103448Sdh155122 /*
5113448Sdh155122  * Initialize things for ESP for each stack instance
5123448Sdh155122  */
5133448Sdh155122 static void *
5143448Sdh155122 ipsecesp_stack_init(netstackid_t stackid, netstack_t *ns)
5153448Sdh155122 {
5163448Sdh155122 	ipsecesp_stack_t	*espstack;
5173448Sdh155122 	ipsecespparam_t		*espp;
5183448Sdh155122 
5193448Sdh155122 	espstack = (ipsecesp_stack_t *)kmem_zalloc(sizeof (*espstack),
5203448Sdh155122 	    KM_SLEEP);
5213448Sdh155122 	espstack->ipsecesp_netstack = ns;
5223448Sdh155122 
5233448Sdh155122 	espp = (ipsecespparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
5243448Sdh155122 	espstack->ipsecesp_params = espp;
5253448Sdh155122 	bcopy(lcl_param_arr, espp, sizeof (lcl_param_arr));
5263448Sdh155122 
5273448Sdh155122 	(void) ipsecesp_param_register(&espstack->ipsecesp_g_nd, espp,
5283448Sdh155122 	    A_CNT(lcl_param_arr));
5293448Sdh155122 
5303448Sdh155122 	(void) esp_kstat_init(espstack, stackid);
5313448Sdh155122 
5323448Sdh155122 	espstack->esp_sadb.s_acquire_timeout =
5333448Sdh155122 	    &espstack->ipsecesp_acquire_timeout;
5343448Sdh155122 	espstack->esp_sadb.s_acqfn = esp_send_acquire;
5353448Sdh155122 	sadbp_init("ESP", &espstack->esp_sadb, SADB_SATYPE_ESP, esp_hash_size,
5363448Sdh155122 	    espstack->ipsecesp_netstack);
5373448Sdh155122 
5383448Sdh155122 	mutex_init(&espstack->ipsecesp_param_lock, NULL, MUTEX_DEFAULT, 0);
5393448Sdh155122 
5403448Sdh155122 	ip_drop_register(&espstack->esp_dropper, "IPsec ESP");
5413448Sdh155122 	return (espstack);
5423448Sdh155122 }
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate /*
5450Sstevel@tonic-gate  * Destroy things for ESP at module unload time.
5460Sstevel@tonic-gate  */
5470Sstevel@tonic-gate void
5480Sstevel@tonic-gate ipsecesp_ddi_destroy(void)
5490Sstevel@tonic-gate {
5503448Sdh155122 	netstack_unregister(NS_IPSECESP);
5510Sstevel@tonic-gate 	taskq_destroy(esp_taskq);
5523448Sdh155122 }
5533448Sdh155122 
5543448Sdh155122 /*
5553448Sdh155122  * Destroy things for ESP for one stack instance
5563448Sdh155122  */
5573448Sdh155122 static void
5583448Sdh155122 ipsecesp_stack_fini(netstackid_t stackid, void *arg)
5593448Sdh155122 {
5603448Sdh155122 	ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)arg;
5613448Sdh155122 
5623448Sdh155122 	if (espstack->esp_pfkey_q != NULL) {
5633448Sdh155122 		(void) quntimeout(espstack->esp_pfkey_q, espstack->esp_event);
5643448Sdh155122 	}
5653448Sdh155122 	espstack->esp_sadb.s_acqfn = NULL;
5663448Sdh155122 	espstack->esp_sadb.s_acquire_timeout = NULL;
5673448Sdh155122 	sadbp_destroy(&espstack->esp_sadb, espstack->ipsecesp_netstack);
5683448Sdh155122 	ip_drop_unregister(&espstack->esp_dropper);
5693448Sdh155122 	mutex_destroy(&espstack->ipsecesp_param_lock);
5703448Sdh155122 	nd_free(&espstack->ipsecesp_g_nd);
5713448Sdh155122 
5723448Sdh155122 	kmem_free(espstack->ipsecesp_params, sizeof (lcl_param_arr));
5733448Sdh155122 	espstack->ipsecesp_params = NULL;
5743448Sdh155122 	kstat_delete_netstack(espstack->esp_ksp, stackid);
5753448Sdh155122 	espstack->esp_ksp = NULL;
5763448Sdh155122 	espstack->esp_kstats = NULL;
5773448Sdh155122 	kmem_free(espstack, sizeof (*espstack));
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * ESP module open routine.
5820Sstevel@tonic-gate  */
5830Sstevel@tonic-gate /* ARGSUSED */
5840Sstevel@tonic-gate static int
5850Sstevel@tonic-gate ipsecesp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5860Sstevel@tonic-gate {
5873448Sdh155122 	netstack_t		*ns;
5883448Sdh155122 	ipsecesp_stack_t	*espstack;
5893448Sdh155122 
5903448Sdh155122 	if (secpolicy_ip_config(credp, B_FALSE) != 0) {
5913448Sdh155122 		esp0dbg(("Non-privileged user trying to open ipsecesp.\n"));
5920Sstevel@tonic-gate 		return (EPERM);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5960Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (sflag != MODOPEN)
5990Sstevel@tonic-gate 		return (EINVAL);
6000Sstevel@tonic-gate 
6013448Sdh155122 	ns = netstack_find_by_cred(credp);
6023448Sdh155122 	ASSERT(ns != NULL);
6033448Sdh155122 	espstack = ns->netstack_ipsecesp;
6043448Sdh155122 	ASSERT(espstack != NULL);
6053448Sdh155122 
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * ASSUMPTIONS (because I'm MT_OCEXCL):
6080Sstevel@tonic-gate 	 *
6090Sstevel@tonic-gate 	 *	* I'm being pushed on top of IP for all my opens (incl. #1).
6100Sstevel@tonic-gate 	 *	* Only ipsecesp_open() can write into esp_sadb.s_ip_q.
6110Sstevel@tonic-gate 	 *	* Because of this, I can check lazily for esp_sadb.s_ip_q.
6120Sstevel@tonic-gate 	 *
6130Sstevel@tonic-gate 	 *  If these assumptions are wrong, I'm in BIG trouble...
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 
6163448Sdh155122 	q->q_ptr = espstack;
6173448Sdh155122 	WR(q)->q_ptr = q->q_ptr;
6183448Sdh155122 
6193448Sdh155122 	if (espstack->esp_sadb.s_ip_q == NULL) {
6200Sstevel@tonic-gate 		struct T_unbind_req *tur;
6210Sstevel@tonic-gate 
6223448Sdh155122 		espstack->esp_sadb.s_ip_q = WR(q);
6230Sstevel@tonic-gate 		/* Allocate an unbind... */
6243448Sdh155122 		espstack->esp_ip_unbind = allocb(sizeof (struct T_unbind_req),
6253448Sdh155122 		    BPRI_HI);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		/*
6280Sstevel@tonic-gate 		 * Send down T_BIND_REQ to bind IPPROTO_ESP.
6290Sstevel@tonic-gate 		 * Handle the ACK here in ESP.
6300Sstevel@tonic-gate 		 */
6310Sstevel@tonic-gate 		qprocson(q);
6323448Sdh155122 		if (espstack->esp_ip_unbind == NULL ||
6333448Sdh155122 		    !sadb_t_bind_req(espstack->esp_sadb.s_ip_q, IPPROTO_ESP)) {
6343448Sdh155122 			if (espstack->esp_ip_unbind != NULL) {
6353448Sdh155122 				freeb(espstack->esp_ip_unbind);
6363448Sdh155122 				espstack->esp_ip_unbind = NULL;
6370Sstevel@tonic-gate 			}
6380Sstevel@tonic-gate 			q->q_ptr = NULL;
6393448Sdh155122 			netstack_rele(espstack->ipsecesp_netstack);
6400Sstevel@tonic-gate 			return (ENOMEM);
6410Sstevel@tonic-gate 		}
6420Sstevel@tonic-gate 
6433448Sdh155122 		espstack->esp_ip_unbind->b_datap->db_type = M_PROTO;
6443448Sdh155122 		tur = (struct T_unbind_req *)espstack->esp_ip_unbind->b_rptr;
6450Sstevel@tonic-gate 		tur->PRIM_type = T_UNBIND_REQ;
6460Sstevel@tonic-gate 	} else {
6470Sstevel@tonic-gate 		qprocson(q);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	/*
6510Sstevel@tonic-gate 	 * For now, there's not much I can do.  I'll be getting a message
6520Sstevel@tonic-gate 	 * passed down to me from keysock (in my wput), and a T_BIND_ACK
6530Sstevel@tonic-gate 	 * up from IP (in my rput).
6540Sstevel@tonic-gate 	 */
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	return (0);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * ESP module close routine.
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate static int
6630Sstevel@tonic-gate ipsecesp_close(queue_t *q)
6640Sstevel@tonic-gate {
6653448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
6663448Sdh155122 
6670Sstevel@tonic-gate 	/*
6680Sstevel@tonic-gate 	 * If esp_sadb.s_ip_q is attached to this instance, send a
6690Sstevel@tonic-gate 	 * T_UNBIND_REQ to IP for the instance before doing
6700Sstevel@tonic-gate 	 * a qprocsoff().
6710Sstevel@tonic-gate 	 */
6723448Sdh155122 	if (WR(q) == espstack->esp_sadb.s_ip_q &&
6733448Sdh155122 	    espstack->esp_ip_unbind != NULL) {
6743448Sdh155122 		putnext(WR(q), espstack->esp_ip_unbind);
6753448Sdh155122 		espstack->esp_ip_unbind = NULL;
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * Clean up q_ptr, if needed.
6800Sstevel@tonic-gate 	 */
6810Sstevel@tonic-gate 	qprocsoff(q);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/* Keysock queue check is safe, because of OCEXCL perimeter. */
6840Sstevel@tonic-gate 
6853448Sdh155122 	if (q == espstack->esp_pfkey_q) {
6863448Sdh155122 		esp1dbg(espstack,
6873448Sdh155122 		    ("ipsecesp_close:  Ummm... keysock is closing ESP.\n"));
6883448Sdh155122 		espstack->esp_pfkey_q = NULL;
6890Sstevel@tonic-gate 		/* Detach qtimeouts. */
6903448Sdh155122 		(void) quntimeout(q, espstack->esp_event);
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6933448Sdh155122 	if (WR(q) == espstack->esp_sadb.s_ip_q) {
6940Sstevel@tonic-gate 		/*
6950Sstevel@tonic-gate 		 * If the esp_sadb.s_ip_q is attached to this instance, find
6960Sstevel@tonic-gate 		 * another.  The OCEXCL outer perimeter helps us here.
6970Sstevel@tonic-gate 		 */
6983448Sdh155122 		espstack->esp_sadb.s_ip_q = NULL;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		/*
7010Sstevel@tonic-gate 		 * Find a replacement queue for esp_sadb.s_ip_q.
7020Sstevel@tonic-gate 		 */
7033448Sdh155122 		if (espstack->esp_pfkey_q != NULL &&
7043448Sdh155122 		    espstack->esp_pfkey_q != RD(q)) {
7050Sstevel@tonic-gate 			/*
7060Sstevel@tonic-gate 			 * See if we can use the pfkey_q.
7070Sstevel@tonic-gate 			 */
7083448Sdh155122 			espstack->esp_sadb.s_ip_q = WR(espstack->esp_pfkey_q);
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 
7113448Sdh155122 		if (espstack->esp_sadb.s_ip_q == NULL ||
7123448Sdh155122 		    !sadb_t_bind_req(espstack->esp_sadb.s_ip_q, IPPROTO_ESP)) {
7133448Sdh155122 			esp1dbg(espstack, ("ipsecesp: Can't reassign ip_q.\n"));
7143448Sdh155122 			espstack->esp_sadb.s_ip_q = NULL;
7150Sstevel@tonic-gate 		} else {
7163448Sdh155122 			espstack->esp_ip_unbind =
7173448Sdh155122 			    allocb(sizeof (struct T_unbind_req), BPRI_HI);
7183448Sdh155122 
7193448Sdh155122 			if (espstack->esp_ip_unbind != NULL) {
7200Sstevel@tonic-gate 				struct T_unbind_req *tur;
7210Sstevel@tonic-gate 
7223448Sdh155122 				espstack->esp_ip_unbind->b_datap->db_type =
7233448Sdh155122 				    M_PROTO;
7240Sstevel@tonic-gate 				tur = (struct T_unbind_req *)
7253448Sdh155122 				    espstack->esp_ip_unbind->b_rptr;
7260Sstevel@tonic-gate 				tur->PRIM_type = T_UNBIND_REQ;
7270Sstevel@tonic-gate 			}
7280Sstevel@tonic-gate 			/* If it's NULL, I can't do much here. */
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7323448Sdh155122 	netstack_rele(espstack->ipsecesp_netstack);
7330Sstevel@tonic-gate 	return (0);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate /*
7370Sstevel@tonic-gate  * Add a number of bytes to what the SA has protected so far.  Return
7380Sstevel@tonic-gate  * B_TRUE if the SA can still protect that many bytes.
7390Sstevel@tonic-gate  *
7400Sstevel@tonic-gate  * Caller must REFRELE the passed-in assoc.  This function must REFRELE
7410Sstevel@tonic-gate  * any obtained peer SA.
7420Sstevel@tonic-gate  */
7430Sstevel@tonic-gate static boolean_t
7440Sstevel@tonic-gate esp_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
7470Sstevel@tonic-gate 	isaf_t *bucket;
7480Sstevel@tonic-gate 	boolean_t inrc, outrc, isv6;
7490Sstevel@tonic-gate 	sadb_t *sp;
7500Sstevel@tonic-gate 	int outhash;
7513448Sdh155122 	netstack_t		*ns = assoc->ipsa_netstack;
7523448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	/* No peer?  No problem! */
7550Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
7563448Sdh155122 		return (sadb_age_bytes(espstack->esp_pfkey_q, assoc, bytes,
7570Sstevel@tonic-gate 		    B_TRUE));
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	/*
7610Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
7620Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, two
7630Sstevel@tonic-gate 	 * expire messages may occur.  We limit this by only sending the
7640Sstevel@tonic-gate 	 * expire message on one of the peers, we'll pick the inbound
7650Sstevel@tonic-gate 	 * arbitrarily.
7660Sstevel@tonic-gate 	 *
7670Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
7680Sstevel@tonic-gate 	 * reconsider.
7690Sstevel@tonic-gate 	 */
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/* Use address length to select IPv6/IPv4 */
7720Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
7733448Sdh155122 	sp = isv6 ? &espstack->esp_sadb.s_v6 : &espstack->esp_sadb.s_v4;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if (inbound) {
7760Sstevel@tonic-gate 		inassoc = assoc;
7770Sstevel@tonic-gate 		if (isv6) {
778564Ssommerfe 			outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
7790Sstevel@tonic-gate 			    &inassoc->ipsa_dstaddr));
7800Sstevel@tonic-gate 		} else {
781564Ssommerfe 			outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
7824987Sdanmcd 			    &inassoc->ipsa_dstaddr));
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
7850Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
7860Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
7870Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
7880Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
7890Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
7900Sstevel@tonic-gate 		if (outassoc == NULL) {
7910Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
7920Sstevel@tonic-gate 			esp0dbg(("esp_age_bytes: "
7930Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
7943448Sdh155122 			return (sadb_age_bytes(espstack->esp_pfkey_q, inassoc,
7950Sstevel@tonic-gate 			    bytes, B_TRUE));
7960Sstevel@tonic-gate 		}
7970Sstevel@tonic-gate 	} else {
7980Sstevel@tonic-gate 		outassoc = assoc;
799564Ssommerfe 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
8000Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
8010Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
8020Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
8030Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
8040Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
8050Sstevel@tonic-gate 		if (inassoc == NULL) {
8060Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
8070Sstevel@tonic-gate 			esp0dbg(("esp_age_bytes: "
8080Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
8093448Sdh155122 			return (sadb_age_bytes(espstack->esp_pfkey_q, outassoc,
8100Sstevel@tonic-gate 			    bytes, B_TRUE));
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8143448Sdh155122 	inrc = sadb_age_bytes(espstack->esp_pfkey_q, inassoc, bytes, B_TRUE);
8153448Sdh155122 	outrc = sadb_age_bytes(espstack->esp_pfkey_q, outassoc, bytes, B_FALSE);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * REFRELE any peer SA.
8190Sstevel@tonic-gate 	 *
8200Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
8210Sstevel@tonic-gate 	 * them in { }.
8220Sstevel@tonic-gate 	 */
8230Sstevel@tonic-gate 	if (inbound) {
8240Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
8250Sstevel@tonic-gate 	} else {
8260Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
8270Sstevel@tonic-gate 	}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	return (inrc && outrc);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate /*
8330Sstevel@tonic-gate  * Do incoming NAT-T manipulations for packet.
8340Sstevel@tonic-gate  */
8350Sstevel@tonic-gate static ipsec_status_t
8360Sstevel@tonic-gate esp_fix_natt_checksums(mblk_t *data_mp, ipsa_t *assoc)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate 	ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
8390Sstevel@tonic-gate 	tcpha_t *tcph;
8400Sstevel@tonic-gate 	udpha_t *udpha;
8410Sstevel@tonic-gate 	/* Initialize to our inbound cksum adjustment... */
8420Sstevel@tonic-gate 	uint32_t sum = assoc->ipsa_inbound_cksum;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	switch (ipha->ipha_protocol) {
8450Sstevel@tonic-gate 	case IPPROTO_TCP:
8460Sstevel@tonic-gate 		tcph = (tcpha_t *)(data_mp->b_rptr +
8470Sstevel@tonic-gate 		    IPH_HDR_LENGTH(ipha));
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate #define	DOWN_SUM(x) (x) = ((x) & 0xFFFF) +	 ((x) >> 16)
8500Sstevel@tonic-gate 		sum += ~ntohs(tcph->tha_sum) & 0xFFFF;
8510Sstevel@tonic-gate 		DOWN_SUM(sum);
8520Sstevel@tonic-gate 		DOWN_SUM(sum);
8530Sstevel@tonic-gate 		tcph->tha_sum = ~htons(sum);
8540Sstevel@tonic-gate 		break;
8550Sstevel@tonic-gate 	case IPPROTO_UDP:
8560Sstevel@tonic-gate 		udpha = (udpha_t *)(data_mp->b_rptr + IPH_HDR_LENGTH(ipha));
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		if (udpha->uha_checksum != 0) {
8590Sstevel@tonic-gate 			/* Adujst if the inbound one was not zero. */
8600Sstevel@tonic-gate 			sum += ~ntohs(udpha->uha_checksum) & 0xFFFF;
8610Sstevel@tonic-gate 			DOWN_SUM(sum);
8620Sstevel@tonic-gate 			DOWN_SUM(sum);
8630Sstevel@tonic-gate 			udpha->uha_checksum = ~htons(sum);
8640Sstevel@tonic-gate 			if (udpha->uha_checksum == 0)
8650Sstevel@tonic-gate 				udpha->uha_checksum = 0xFFFF;
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate #undef DOWN_SUM
8680Sstevel@tonic-gate 		break;
8690Sstevel@tonic-gate 	case IPPROTO_IP:
8700Sstevel@tonic-gate 		/*
8710Sstevel@tonic-gate 		 * This case is only an issue for self-encapsulated
8720Sstevel@tonic-gate 		 * packets.  So for now, fall through.
8730Sstevel@tonic-gate 		 */
8740Sstevel@tonic-gate 		break;
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /*
8813192Sdanmcd  * Strip ESP header, check padding, and fix IP header.
8820Sstevel@tonic-gate  * Returns B_TRUE on success, B_FALSE if an error occured.
8830Sstevel@tonic-gate  */
8840Sstevel@tonic-gate static boolean_t
8850Sstevel@tonic-gate esp_strip_header(mblk_t *data_mp, boolean_t isv4, uint32_t ivlen,
8863448Sdh155122     kstat_named_t **counter, ipsecesp_stack_t *espstack)
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate 	ipha_t *ipha;
8890Sstevel@tonic-gate 	ip6_t *ip6h;
8900Sstevel@tonic-gate 	uint_t divpoint;
8910Sstevel@tonic-gate 	mblk_t *scratch;
8920Sstevel@tonic-gate 	uint8_t nexthdr, padlen;
8930Sstevel@tonic-gate 	uint8_t lastpad;
8943448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
8953192Sdanmcd 	uint8_t *lastbyte;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/*
8980Sstevel@tonic-gate 	 * Strip ESP data and fix IP header.
8990Sstevel@tonic-gate 	 *
9000Sstevel@tonic-gate 	 * XXX In case the beginning of esp_inbound() changes to not do a
9010Sstevel@tonic-gate 	 * pullup, this part of the code can remain unchanged.
9020Sstevel@tonic-gate 	 */
9030Sstevel@tonic-gate 	if (isv4) {
9040Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ipha_t));
9050Sstevel@tonic-gate 		ipha = (ipha_t *)data_mp->b_rptr;
9060Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (esph_t) +
9070Sstevel@tonic-gate 		    IPH_HDR_LENGTH(ipha));
9080Sstevel@tonic-gate 		divpoint = IPH_HDR_LENGTH(ipha);
9090Sstevel@tonic-gate 	} else {
9100Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ip6_t));
9110Sstevel@tonic-gate 		ip6h = (ip6_t *)data_mp->b_rptr;
9120Sstevel@tonic-gate 		divpoint = ip_hdr_length_v6(data_mp, ip6h);
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	scratch = data_mp;
9160Sstevel@tonic-gate 	while (scratch->b_cont != NULL)
9170Sstevel@tonic-gate 		scratch = scratch->b_cont;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	ASSERT((scratch->b_wptr - scratch->b_rptr) >= 3);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	/*
9220Sstevel@tonic-gate 	 * "Next header" and padding length are the last two bytes in the
9230Sstevel@tonic-gate 	 * ESP-protected datagram, thus the explicit - 1 and - 2.
9240Sstevel@tonic-gate 	 * lastpad is the last byte of the padding, which can be used for
9250Sstevel@tonic-gate 	 * a quick check to see if the padding is correct.
9260Sstevel@tonic-gate 	 */
9273192Sdanmcd 	lastbyte = scratch->b_wptr - 1;
9283192Sdanmcd 	nexthdr = *lastbyte--;
9293192Sdanmcd 	padlen = *lastbyte--;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	if (isv4) {
9320Sstevel@tonic-gate 		/* Fix part of the IP header. */
9330Sstevel@tonic-gate 		ipha->ipha_protocol = nexthdr;
9340Sstevel@tonic-gate 		/*
9350Sstevel@tonic-gate 		 * Reality check the padlen.  The explicit - 2 is for the
9360Sstevel@tonic-gate 		 * padding length and the next-header bytes.
9370Sstevel@tonic-gate 		 */
9380Sstevel@tonic-gate 		if (padlen >= ntohs(ipha->ipha_length) - sizeof (ipha_t) - 2 -
9390Sstevel@tonic-gate 		    sizeof (esph_t) - ivlen) {
9403448Sdh155122 			ESP_BUMP_STAT(espstack, bad_decrypt);
9413448Sdh155122 			ipsec_rl_strlog(espstack->ipsecesp_netstack,
9423448Sdh155122 			    info.mi_idnum, 0, 0,
9433448Sdh155122 			    SL_ERROR | SL_WARN,
9443192Sdanmcd 			    "Corrupt ESP packet (padlen too big).\n");
9453448Sdh155122 			esp1dbg(espstack, ("padlen (%d) is greater than:\n",
9464987Sdanmcd 			    padlen));
9473448Sdh155122 			esp1dbg(espstack, ("pkt len(%d) - ip hdr - esp "
9483448Sdh155122 			    "hdr - ivlen(%d) = %d.\n",
9493448Sdh155122 			    ntohs(ipha->ipha_length), ivlen,
9500Sstevel@tonic-gate 			    (int)(ntohs(ipha->ipha_length) - sizeof (ipha_t) -
9513448Sdh155122 			    2 - sizeof (esph_t) - ivlen)));
9523448Sdh155122 			*counter = DROPPER(ipss, ipds_esp_bad_padlen);
9530Sstevel@tonic-gate 			return (B_FALSE);
9540Sstevel@tonic-gate 		}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		/*
9570Sstevel@tonic-gate 		 * Fix the rest of the header.  The explicit - 2 is for the
9580Sstevel@tonic-gate 		 * padding length and the next-header bytes.
9590Sstevel@tonic-gate 		 */
9600Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) - padlen -
9610Sstevel@tonic-gate 		    2 - sizeof (esph_t) - ivlen);
9620Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = 0;
9630Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
9640Sstevel@tonic-gate 	} else {
9650Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_ESP) {
9660Sstevel@tonic-gate 			ip6h->ip6_nxt = nexthdr;
9670Sstevel@tonic-gate 		} else {
9680Sstevel@tonic-gate 			ip6_pkt_t ipp;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 			bzero(&ipp, sizeof (ipp));
9710Sstevel@tonic-gate 			(void) ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL);
9720Sstevel@tonic-gate 			if (ipp.ipp_dstopts != NULL) {
9730Sstevel@tonic-gate 				ipp.ipp_dstopts->ip6d_nxt = nexthdr;
9740Sstevel@tonic-gate 			} else if (ipp.ipp_rthdr != NULL) {
9750Sstevel@tonic-gate 				ipp.ipp_rthdr->ip6r_nxt = nexthdr;
9760Sstevel@tonic-gate 			} else if (ipp.ipp_hopopts != NULL) {
9770Sstevel@tonic-gate 				ipp.ipp_hopopts->ip6h_nxt = nexthdr;
9780Sstevel@tonic-gate 			} else {
9790Sstevel@tonic-gate 				/* Panic a DEBUG kernel. */
9800Sstevel@tonic-gate 				ASSERT(ipp.ipp_hopopts != NULL);
9810Sstevel@tonic-gate 				/* Otherwise, pretend it's IP + ESP. */
9820Sstevel@tonic-gate 				cmn_err(CE_WARN, "ESP IPv6 headers wrong.\n");
9830Sstevel@tonic-gate 				ip6h->ip6_nxt = nexthdr;
9840Sstevel@tonic-gate 			}
9850Sstevel@tonic-gate 		}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		if (padlen >= ntohs(ip6h->ip6_plen) - 2 - sizeof (esph_t) -
9880Sstevel@tonic-gate 		    ivlen) {
9893448Sdh155122 			ESP_BUMP_STAT(espstack, bad_decrypt);
9903448Sdh155122 			ipsec_rl_strlog(espstack->ipsecesp_netstack,
9913448Sdh155122 			    info.mi_idnum, 0, 0,
9923448Sdh155122 			    SL_ERROR | SL_WARN,
9933192Sdanmcd 			    "Corrupt ESP packet (v6 padlen too big).\n");
9943448Sdh155122 			esp1dbg(espstack, ("padlen (%d) is greater than:\n",
9954987Sdanmcd 			    padlen));
9964987Sdanmcd 			esp1dbg(espstack,
9974987Sdanmcd 			    ("pkt len(%u) - ip hdr - esp hdr - ivlen(%d) = "
9984987Sdanmcd 			    "%u.\n", (unsigned)(ntohs(ip6h->ip6_plen)
9994987Sdanmcd 			    + sizeof (ip6_t)), ivlen,
10004987Sdanmcd 			    (unsigned)(ntohs(ip6h->ip6_plen) - 2 -
10014987Sdanmcd 			    sizeof (esph_t) - ivlen)));
10023448Sdh155122 			*counter = DROPPER(ipss, ipds_esp_bad_padlen);
10030Sstevel@tonic-gate 			return (B_FALSE);
10040Sstevel@tonic-gate 		}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		/*
10080Sstevel@tonic-gate 		 * Fix the rest of the header.  The explicit - 2 is for the
10090Sstevel@tonic-gate 		 * padding length and the next-header bytes.  IPv6 is nice,
10100Sstevel@tonic-gate 		 * because there's no hdr checksum!
10110Sstevel@tonic-gate 		 */
10120Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - padlen -
10130Sstevel@tonic-gate 		    2 - sizeof (esph_t) - ivlen);
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 
10163448Sdh155122 	if (espstack->ipsecesp_padding_check > 0 && padlen > 0) {
10173192Sdanmcd 		/*
10183192Sdanmcd 		 * Weak padding check: compare last-byte to length, they
10193192Sdanmcd 		 * should be equal.
10203192Sdanmcd 		 */
10213192Sdanmcd 		lastpad = *lastbyte--;
10223192Sdanmcd 
10233192Sdanmcd 		if (padlen != lastpad) {
10243448Sdh155122 			ipsec_rl_strlog(espstack->ipsecesp_netstack,
10253448Sdh155122 			    info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
10263192Sdanmcd 			    "Corrupt ESP packet (lastpad != padlen).\n");
10273448Sdh155122 			esp1dbg(espstack,
10283448Sdh155122 			    ("lastpad (%d) not equal to padlen (%d):\n",
10293448Sdh155122 			    lastpad, padlen));
10303448Sdh155122 			ESP_BUMP_STAT(espstack, bad_padding);
10313448Sdh155122 			*counter = DROPPER(ipss, ipds_esp_bad_padding);
10323192Sdanmcd 			return (B_FALSE);
10333192Sdanmcd 		}
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 		/*
10363192Sdanmcd 		 * Strong padding check: Check all pad bytes to see that
10373192Sdanmcd 		 * they're ascending.  Go backwards using a descending counter
10383192Sdanmcd 		 * to verify.  padlen == 1 is checked by previous block, so
10393192Sdanmcd 		 * only bother if we've more than 1 byte of padding.
10403192Sdanmcd 		 * Consequently, start the check one byte before the location
10413192Sdanmcd 		 * of "lastpad".
10420Sstevel@tonic-gate 		 */
10433448Sdh155122 		if (espstack->ipsecesp_padding_check > 1) {
10443192Sdanmcd 			/*
10453192Sdanmcd 			 * This assert may have to become an if and a pullup
10463192Sdanmcd 			 * if we start accepting multi-dblk mblks. For now,
10473192Sdanmcd 			 * though, any packet here will have been pulled up in
10483192Sdanmcd 			 * esp_inbound.
10493192Sdanmcd 			 */
10503192Sdanmcd 			ASSERT(MBLKL(scratch) >= lastpad + 3);
10513192Sdanmcd 
10523192Sdanmcd 			/*
10533192Sdanmcd 			 * Use "--lastpad" because we already checked the very
10543192Sdanmcd 			 * last pad byte previously.
10553192Sdanmcd 			 */
10563192Sdanmcd 			while (--lastpad != 0) {
10573192Sdanmcd 				if (lastpad != *lastbyte) {
10583448Sdh155122 					ipsec_rl_strlog(
10593448Sdh155122 					    espstack->ipsecesp_netstack,
10603448Sdh155122 					    info.mi_idnum, 0, 0,
10613192Sdanmcd 					    SL_ERROR | SL_WARN, "Corrupt ESP "
10623192Sdanmcd 					    "packet (bad padding).\n");
10633448Sdh155122 					esp1dbg(espstack,
10643448Sdh155122 					    ("padding not in correct"
10653448Sdh155122 					    " format:\n"));
10663448Sdh155122 					ESP_BUMP_STAT(espstack, bad_padding);
10673448Sdh155122 					*counter = DROPPER(ipss,
10683448Sdh155122 					    ipds_esp_bad_padding);
10693192Sdanmcd 					return (B_FALSE);
10703192Sdanmcd 				}
10713192Sdanmcd 				lastbyte--;
10720Sstevel@tonic-gate 			}
10730Sstevel@tonic-gate 		}
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	/* Trim off the padding. */
10770Sstevel@tonic-gate 	ASSERT(data_mp->b_cont == NULL);
10780Sstevel@tonic-gate 	data_mp->b_wptr -= (padlen + 2);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	/*
10810Sstevel@tonic-gate 	 * Remove the ESP header.
10820Sstevel@tonic-gate 	 *
10830Sstevel@tonic-gate 	 * The above assertions about data_mp's size will make this work.
10840Sstevel@tonic-gate 	 *
10850Sstevel@tonic-gate 	 * XXX  Question:  If I send up and get back a contiguous mblk,
10860Sstevel@tonic-gate 	 * would it be quicker to bcopy over, or keep doing the dupb stuff?
10870Sstevel@tonic-gate 	 * I go with copying for now.
10880Sstevel@tonic-gate 	 */
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	if (IS_P2ALIGNED(data_mp->b_rptr, sizeof (uint32_t)) &&
10910Sstevel@tonic-gate 	    IS_P2ALIGNED(ivlen, sizeof (uint32_t))) {
10920Sstevel@tonic-gate 		uint8_t *start = data_mp->b_rptr;
10930Sstevel@tonic-gate 		uint32_t *src, *dst;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 		src = (uint32_t *)(start + divpoint);
10960Sstevel@tonic-gate 		dst = (uint32_t *)(start + divpoint + sizeof (esph_t) + ivlen);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(dst, sizeof (uint32_t)) &&
10990Sstevel@tonic-gate 		    IS_P2ALIGNED(src, sizeof (uint32_t)));
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 		do {
11020Sstevel@tonic-gate 			src--;
11030Sstevel@tonic-gate 			dst--;
11040Sstevel@tonic-gate 			*dst = *src;
11050Sstevel@tonic-gate 		} while (src != (uint32_t *)start);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		data_mp->b_rptr = (uchar_t *)dst;
11080Sstevel@tonic-gate 	} else {
11090Sstevel@tonic-gate 		uint8_t *start = data_mp->b_rptr;
11100Sstevel@tonic-gate 		uint8_t *src, *dst;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		src = start + divpoint;
11130Sstevel@tonic-gate 		dst = src + sizeof (esph_t) + ivlen;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		do {
11160Sstevel@tonic-gate 			src--;
11170Sstevel@tonic-gate 			dst--;
11180Sstevel@tonic-gate 			*dst = *src;
11190Sstevel@tonic-gate 		} while (src != start);
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		data_mp->b_rptr = dst;
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 
11243448Sdh155122 	esp2dbg(espstack, ("data_mp after inbound ESP adjustment:\n"));
11253448Sdh155122 	esp2dbg(espstack, (dump_msg(data_mp)));
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	return (B_TRUE);
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate  * Updating use times can be tricky business if the ipsa_haspeer flag is
11320Sstevel@tonic-gate  * set.  This function is called once in an SA's lifetime.
11330Sstevel@tonic-gate  *
11340Sstevel@tonic-gate  * Caller has to REFRELE "assoc" which is passed in.  This function has
11350Sstevel@tonic-gate  * to REFRELE any peer SA that is obtained.
11360Sstevel@tonic-gate  */
11370Sstevel@tonic-gate static void
11380Sstevel@tonic-gate esp_set_usetime(ipsa_t *assoc, boolean_t inbound)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
11410Sstevel@tonic-gate 	isaf_t *bucket;
11420Sstevel@tonic-gate 	sadb_t *sp;
11430Sstevel@tonic-gate 	int outhash;
11440Sstevel@tonic-gate 	boolean_t isv6;
11453448Sdh155122 	netstack_t		*ns = assoc->ipsa_netstack;
11463448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	/* No peer?  No problem! */
11490Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
11500Sstevel@tonic-gate 		sadb_set_usetime(assoc);
11510Sstevel@tonic-gate 		return;
11520Sstevel@tonic-gate 	}
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	/*
11550Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
11560Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, the times
11570Sstevel@tonic-gate 	 * will be out-of-synch by at most a second, and since our time
11580Sstevel@tonic-gate 	 * granularity is a second, this won't be a problem.
11590Sstevel@tonic-gate 	 *
11600Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
11610Sstevel@tonic-gate 	 * reconsider.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	/* Use address length to select IPv6/IPv4 */
11650Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
11663448Sdh155122 	sp = isv6 ? &espstack->esp_sadb.s_v6 : &espstack->esp_sadb.s_v4;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	if (inbound) {
11690Sstevel@tonic-gate 		inassoc = assoc;
11700Sstevel@tonic-gate 		if (isv6) {
1171564Ssommerfe 			outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
11720Sstevel@tonic-gate 			    &inassoc->ipsa_dstaddr));
11730Sstevel@tonic-gate 		} else {
1174564Ssommerfe 			outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
11754987Sdanmcd 			    &inassoc->ipsa_dstaddr));
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
11780Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
11790Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
11800Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
11810Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
11820Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
11830Sstevel@tonic-gate 		if (outassoc == NULL) {
11840Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
11850Sstevel@tonic-gate 			esp0dbg(("esp_set_usetime: "
11860Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
11870Sstevel@tonic-gate 			sadb_set_usetime(inassoc);
11880Sstevel@tonic-gate 			return;
11890Sstevel@tonic-gate 		}
11900Sstevel@tonic-gate 	} else {
11910Sstevel@tonic-gate 		outassoc = assoc;
1192564Ssommerfe 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
11930Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
11940Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
11950Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
11960Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
11970Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
11980Sstevel@tonic-gate 		if (inassoc == NULL) {
11990Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
12000Sstevel@tonic-gate 			esp0dbg(("esp_set_usetime: "
12010Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
12020Sstevel@tonic-gate 			sadb_set_usetime(outassoc);
12030Sstevel@tonic-gate 			return;
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	/* Update usetime on both. */
12080Sstevel@tonic-gate 	sadb_set_usetime(inassoc);
12090Sstevel@tonic-gate 	sadb_set_usetime(outassoc);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	/*
12120Sstevel@tonic-gate 	 * REFRELE any peer SA.
12130Sstevel@tonic-gate 	 *
12140Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
12150Sstevel@tonic-gate 	 * them in { }.
12160Sstevel@tonic-gate 	 */
12170Sstevel@tonic-gate 	if (inbound) {
12180Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
12190Sstevel@tonic-gate 	} else {
12200Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate /*
12250Sstevel@tonic-gate  * Handle ESP inbound data for IPv4 and IPv6.
12260Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
12270Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
12280Sstevel@tonic-gate  */
12290Sstevel@tonic-gate ipsec_status_t
12300Sstevel@tonic-gate esp_inbound(mblk_t *ipsec_in_mp, void *arg)
12310Sstevel@tonic-gate {
12320Sstevel@tonic-gate 	mblk_t *data_mp = ipsec_in_mp->b_cont;
12330Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
12340Sstevel@tonic-gate 	esph_t *esph = (esph_t *)arg;
12350Sstevel@tonic-gate 	ipsa_t *ipsa = ii->ipsec_in_esp_sa;
12363448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
12373448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
12383448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	/*
12410Sstevel@tonic-gate 	 * We may wish to check replay in-range-only here as an optimization.
12420Sstevel@tonic-gate 	 * Include the reality check of ipsa->ipsa_replay >
12430Sstevel@tonic-gate 	 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
12440Sstevel@tonic-gate 	 * where N == ipsa->ipsa_replay_wsize.
12450Sstevel@tonic-gate 	 *
12460Sstevel@tonic-gate 	 * Another check that may come here later is the "collision" check.
12470Sstevel@tonic-gate 	 * If legitimate packets flow quickly enough, this won't be a problem,
12480Sstevel@tonic-gate 	 * but collisions may cause authentication algorithm crunching to
12490Sstevel@tonic-gate 	 * take place when it doesn't need to.
12500Sstevel@tonic-gate 	 */
12510Sstevel@tonic-gate 	if (!sadb_replay_peek(ipsa, esph->esph_replay)) {
12523448Sdh155122 		ESP_BUMP_STAT(espstack, replay_early_failures);
12533448Sdh155122 		IP_ESP_BUMP_STAT(ipss, in_discards);
12540Sstevel@tonic-gate 		/*
12550Sstevel@tonic-gate 		 * TODO: Extract inbound interface from the IPSEC_IN
12560Sstevel@tonic-gate 		 * message's ii->ipsec_in_rill_index.
12570Sstevel@tonic-gate 		 */
12580Sstevel@tonic-gate 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
12593448Sdh155122 		    DROPPER(ipss, ipds_esp_early_replay),
12603448Sdh155122 		    &espstack->esp_dropper);
12610Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
12620Sstevel@tonic-gate 	}
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	/*
12650Sstevel@tonic-gate 	 * Has this packet already been processed by a hardware
12660Sstevel@tonic-gate 	 * IPsec accelerator?
12670Sstevel@tonic-gate 	 */
12680Sstevel@tonic-gate 	if (ii->ipsec_in_accelerated) {
12690Sstevel@tonic-gate 		ipsec_status_t rv;
12703448Sdh155122 		esp3dbg(espstack,
12713448Sdh155122 		    ("esp_inbound: pkt processed by ill=%d isv6=%d\n",
12720Sstevel@tonic-gate 		    ii->ipsec_in_ill_index, !ii->ipsec_in_v4));
12730Sstevel@tonic-gate 		rv = esp_inbound_accelerated(ipsec_in_mp,
12740Sstevel@tonic-gate 		    data_mp, ii->ipsec_in_v4, ipsa);
12750Sstevel@tonic-gate 		return (rv);
12760Sstevel@tonic-gate 	}
12773448Sdh155122 	ESP_BUMP_STAT(espstack, noaccel);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	/*
12800Sstevel@tonic-gate 	 * Adjust the IP header's payload length to reflect the removal
12810Sstevel@tonic-gate 	 * of the ICV.
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	if (!ii->ipsec_in_v4) {
12840Sstevel@tonic-gate 		ip6_t *ip6h = (ip6_t *)data_mp->b_rptr;
12850Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) -
12860Sstevel@tonic-gate 		    ipsa->ipsa_mac_len);
12870Sstevel@tonic-gate 	} else {
12880Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
12890Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) -
12900Sstevel@tonic-gate 		    ipsa->ipsa_mac_len);
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	/* submit the request to the crypto framework */
12940Sstevel@tonic-gate 	return (esp_submit_req_inbound(ipsec_in_mp, ipsa,
12950Sstevel@tonic-gate 	    (uint8_t *)esph - data_mp->b_rptr));
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /*
12990Sstevel@tonic-gate  * Perform the really difficult work of inserting the proposed situation.
13000Sstevel@tonic-gate  * Called while holding the algorithm lock.
13010Sstevel@tonic-gate  */
13020Sstevel@tonic-gate static void
13030Sstevel@tonic-gate esp_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs)
13040Sstevel@tonic-gate {
13050Sstevel@tonic-gate 	sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
13060Sstevel@tonic-gate 	ipsec_out_t *io;
13070Sstevel@tonic-gate 	ipsec_action_t *ap;
13080Sstevel@tonic-gate 	ipsec_prot_t *prot;
13093448Sdh155122 	netstack_t *ns;
13103448Sdh155122 	ipsecesp_stack_t *espstack;
13113448Sdh155122 	ipsec_stack_t *ipss;
13123448Sdh155122 
13130Sstevel@tonic-gate 	io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr;
13140Sstevel@tonic-gate 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
13153448Sdh155122 	ns = io->ipsec_out_ns;
13163448Sdh155122 	espstack = ns->netstack_ipsecesp;
13173448Sdh155122 	ipss = ns->netstack_ipsec;
13183448Sdh155122 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
13210Sstevel@tonic-gate 	prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
13220Sstevel@tonic-gate 	*(uint32_t *)(&prop->sadb_prop_replay) = 0;	/* Quick zero-out! */
13230Sstevel@tonic-gate 
13243448Sdh155122 	prop->sadb_prop_replay = espstack->ipsecesp_replay_size;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	/*
13270Sstevel@tonic-gate 	 * Based upon algorithm properties, and what-not, prioritize
13280Sstevel@tonic-gate 	 * a proposal.  If the IPSEC_OUT message has an algorithm specified,
13290Sstevel@tonic-gate 	 * use it first and foremost.
13300Sstevel@tonic-gate 	 *
13310Sstevel@tonic-gate 	 * For each action in policy list
13320Sstevel@tonic-gate 	 *   Add combination.  If I've hit limit, return.
13330Sstevel@tonic-gate 	 */
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	for (ap = acqrec->ipsacq_act; ap != NULL;
13360Sstevel@tonic-gate 	    ap = ap->ipa_next) {
13370Sstevel@tonic-gate 		ipsec_alginfo_t *ealg = NULL;
13380Sstevel@tonic-gate 		ipsec_alginfo_t *aalg = NULL;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 		if (ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY)
13410Sstevel@tonic-gate 			continue;
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 		prot = &ap->ipa_act.ipa_apply;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 		if (!(prot->ipp_use_esp))
13460Sstevel@tonic-gate 			continue;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 		if (prot->ipp_esp_auth_alg != 0) {
13493448Sdh155122 			aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
13500Sstevel@tonic-gate 			    [prot->ipp_esp_auth_alg];
13510Sstevel@tonic-gate 			if (aalg == NULL || !ALG_VALID(aalg))
13520Sstevel@tonic-gate 				continue;
13530Sstevel@tonic-gate 		}
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		ASSERT(prot->ipp_encr_alg > 0);
13563448Sdh155122 		ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR]
13573448Sdh155122 		    [prot->ipp_encr_alg];
13580Sstevel@tonic-gate 		if (ealg == NULL || !ALG_VALID(ealg))
13590Sstevel@tonic-gate 			continue;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		comb->sadb_comb_flags = 0;
13620Sstevel@tonic-gate 		comb->sadb_comb_reserved = 0;
13630Sstevel@tonic-gate 		comb->sadb_comb_encrypt = ealg->alg_id;
13642751Sdanmcd 		comb->sadb_comb_encrypt_minbits =
13652751Sdanmcd 		    MAX(prot->ipp_espe_minbits, ealg->alg_ef_minbits);
13662751Sdanmcd 		comb->sadb_comb_encrypt_maxbits =
13672751Sdanmcd 		    MIN(prot->ipp_espe_maxbits, ealg->alg_ef_maxbits);
13680Sstevel@tonic-gate 		if (aalg == NULL) {
13690Sstevel@tonic-gate 			comb->sadb_comb_auth = 0;
13700Sstevel@tonic-gate 			comb->sadb_comb_auth_minbits = 0;
13710Sstevel@tonic-gate 			comb->sadb_comb_auth_maxbits = 0;
13720Sstevel@tonic-gate 		} else {
13730Sstevel@tonic-gate 			comb->sadb_comb_auth = aalg->alg_id;
13742751Sdanmcd 			comb->sadb_comb_auth_minbits =
13752751Sdanmcd 			    MAX(prot->ipp_espa_minbits, aalg->alg_ef_minbits);
13762751Sdanmcd 			comb->sadb_comb_auth_maxbits =
13772751Sdanmcd 			    MIN(prot->ipp_espa_maxbits, aalg->alg_ef_maxbits);
13780Sstevel@tonic-gate 		}
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 		/*
13810Sstevel@tonic-gate 		 * The following may be based on algorithm
13820Sstevel@tonic-gate 		 * properties, but in the meantime, we just pick
13830Sstevel@tonic-gate 		 * some good, sensible numbers.  Key mgmt. can
13840Sstevel@tonic-gate 		 * (and perhaps should) be the place to finalize
13850Sstevel@tonic-gate 		 * such decisions.
13860Sstevel@tonic-gate 		 */
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		/*
13890Sstevel@tonic-gate 		 * No limits on allocations, since we really don't
13900Sstevel@tonic-gate 		 * support that concept currently.
13910Sstevel@tonic-gate 		 */
13920Sstevel@tonic-gate 		comb->sadb_comb_soft_allocations = 0;
13930Sstevel@tonic-gate 		comb->sadb_comb_hard_allocations = 0;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		/*
13960Sstevel@tonic-gate 		 * These may want to come from policy rule..
13970Sstevel@tonic-gate 		 */
13983448Sdh155122 		comb->sadb_comb_soft_bytes =
13993448Sdh155122 		    espstack->ipsecesp_default_soft_bytes;
14003448Sdh155122 		comb->sadb_comb_hard_bytes =
14013448Sdh155122 		    espstack->ipsecesp_default_hard_bytes;
14023448Sdh155122 		comb->sadb_comb_soft_addtime =
14033448Sdh155122 		    espstack->ipsecesp_default_soft_addtime;
14043448Sdh155122 		comb->sadb_comb_hard_addtime =
14053448Sdh155122 		    espstack->ipsecesp_default_hard_addtime;
14063448Sdh155122 		comb->sadb_comb_soft_usetime =
14073448Sdh155122 		    espstack->ipsecesp_default_soft_usetime;
14083448Sdh155122 		comb->sadb_comb_hard_usetime =
14093448Sdh155122 		    espstack->ipsecesp_default_hard_usetime;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
14120Sstevel@tonic-gate 		if (--combs == 0)
14130Sstevel@tonic-gate 			break;	/* out of space.. */
14140Sstevel@tonic-gate 		comb++;
14150Sstevel@tonic-gate 	}
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate /*
14190Sstevel@tonic-gate  * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
14200Sstevel@tonic-gate  */
14210Sstevel@tonic-gate static void
14223448Sdh155122 esp_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns)
14230Sstevel@tonic-gate {
14243055Sdanmcd 	uint_t combs;
14250Sstevel@tonic-gate 	sadb_msg_t *samsg;
14260Sstevel@tonic-gate 	sadb_prop_t *prop;
14273055Sdanmcd 	mblk_t *pfkeymp, *msgmp;
14283448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
14293448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
14303448Sdh155122 
14313448Sdh155122 	ESP_BUMP_STAT(espstack, acquire_requests);
14323448Sdh155122 
1433*7073Spwernau 	if (espstack->esp_pfkey_q == NULL) {
1434*7073Spwernau 		mutex_exit(&acqrec->ipsacq_lock);
14353055Sdanmcd 		return;
1436*7073Spwernau 	}
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	/* Set up ACQUIRE. */
14393448Sdh155122 	pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_ESP,
14403448Sdh155122 	    ns->netstack_ipsec);
14413055Sdanmcd 	if (pfkeymp == NULL) {
14420Sstevel@tonic-gate 		esp0dbg(("sadb_setup_acquire failed.\n"));
1443*7073Spwernau 		mutex_exit(&acqrec->ipsacq_lock);
14443055Sdanmcd 		return;
14450Sstevel@tonic-gate 	}
14463448Sdh155122 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
14473448Sdh155122 	combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] *
14483448Sdh155122 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
14493055Sdanmcd 	msgmp = pfkeymp->b_cont;
14503055Sdanmcd 	samsg = (sadb_msg_t *)(msgmp->b_rptr);
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	/* Insert proposal here. */
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
14550Sstevel@tonic-gate 	esp_insert_prop(prop, acqrec, combs);
14560Sstevel@tonic-gate 	samsg->sadb_msg_len += prop->sadb_prop_len;
14570Sstevel@tonic-gate 	msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
14580Sstevel@tonic-gate 
14593448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	/*
14620Sstevel@tonic-gate 	 * Must mutex_exit() before sending PF_KEY message up, in
14630Sstevel@tonic-gate 	 * order to avoid recursive mutex_enter() if there are no registered
14640Sstevel@tonic-gate 	 * listeners.
14650Sstevel@tonic-gate 	 *
14660Sstevel@tonic-gate 	 * Once I've sent the message, I'm cool anyway.
14670Sstevel@tonic-gate 	 */
14680Sstevel@tonic-gate 	mutex_exit(&acqrec->ipsacq_lock);
14693055Sdanmcd 	if (extended != NULL) {
14703448Sdh155122 		putnext(espstack->esp_pfkey_q, extended);
14710Sstevel@tonic-gate 	}
14723448Sdh155122 	putnext(espstack->esp_pfkey_q, pfkeymp);
14730Sstevel@tonic-gate }
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate /*
14760Sstevel@tonic-gate  * Handle the SADB_GETSPI message.  Create a larval SA.
14770Sstevel@tonic-gate  */
14780Sstevel@tonic-gate static void
14793448Sdh155122 esp_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecesp_stack_t *espstack)
14800Sstevel@tonic-gate {
14810Sstevel@tonic-gate 	ipsa_t *newbie, *target;
14820Sstevel@tonic-gate 	isaf_t *outbound, *inbound;
14830Sstevel@tonic-gate 	int rc, diagnostic;
14840Sstevel@tonic-gate 	sadb_sa_t *assoc;
14850Sstevel@tonic-gate 	keysock_out_t *kso;
14860Sstevel@tonic-gate 	uint32_t newspi;
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	/*
14890Sstevel@tonic-gate 	 * Randomly generate a proposed SPI value
14900Sstevel@tonic-gate 	 */
14910Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t));
14923448Sdh155122 	newbie = sadb_getspi(ksi, newspi, &diagnostic,
14933448Sdh155122 	    espstack->ipsecesp_netstack);
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if (newbie == NULL) {
14963448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, ENOMEM, diagnostic,
14970Sstevel@tonic-gate 		    ksi->ks_in_serial);
14980Sstevel@tonic-gate 		return;
14990Sstevel@tonic-gate 	} else if (newbie == (ipsa_t *)-1) {
15003448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL, diagnostic,
15010Sstevel@tonic-gate 		    ksi->ks_in_serial);
15020Sstevel@tonic-gate 		return;
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	/*
15060Sstevel@tonic-gate 	 * XXX - We may randomly collide.  We really should recover from this.
15070Sstevel@tonic-gate 	 *	 Unfortunately, that could require spending way-too-much-time
15080Sstevel@tonic-gate 	 *	 in here.  For now, let the user retry.
15090Sstevel@tonic-gate 	 */
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	if (newbie->ipsa_addrfam == AF_INET6) {
15123448Sdh155122 		outbound = OUTBOUND_BUCKET_V6(&espstack->esp_sadb.s_v6,
1513564Ssommerfe 		    *(uint32_t *)(newbie->ipsa_dstaddr));
15143448Sdh155122 		inbound = INBOUND_BUCKET(&espstack->esp_sadb.s_v6,
15153448Sdh155122 		    newbie->ipsa_spi);
15160Sstevel@tonic-gate 	} else {
15170Sstevel@tonic-gate 		ASSERT(newbie->ipsa_addrfam == AF_INET);
15183448Sdh155122 		outbound = OUTBOUND_BUCKET_V4(&espstack->esp_sadb.s_v4,
1519564Ssommerfe 		    *(uint32_t *)(newbie->ipsa_dstaddr));
15203448Sdh155122 		inbound = INBOUND_BUCKET(&espstack->esp_sadb.s_v4,
15213448Sdh155122 		    newbie->ipsa_spi);
15220Sstevel@tonic-gate 	}
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	mutex_enter(&outbound->isaf_lock);
15250Sstevel@tonic-gate 	mutex_enter(&inbound->isaf_lock);
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	/*
15280Sstevel@tonic-gate 	 * Check for collisions (i.e. did sadb_getspi() return with something
15290Sstevel@tonic-gate 	 * that already exists?).
15300Sstevel@tonic-gate 	 *
15310Sstevel@tonic-gate 	 * Try outbound first.  Even though SADB_GETSPI is traditionally
15320Sstevel@tonic-gate 	 * for inbound SAs, you never know what a user might do.
15330Sstevel@tonic-gate 	 */
15340Sstevel@tonic-gate 	target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
15350Sstevel@tonic-gate 	    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
15360Sstevel@tonic-gate 	if (target == NULL) {
15370Sstevel@tonic-gate 		target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
15380Sstevel@tonic-gate 		    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
15390Sstevel@tonic-gate 		    newbie->ipsa_addrfam);
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	/*
15430Sstevel@tonic-gate 	 * I don't have collisions elsewhere!
15440Sstevel@tonic-gate 	 * (Nor will I because I'm still holding inbound/outbound locks.)
15450Sstevel@tonic-gate 	 */
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	if (target != NULL) {
15480Sstevel@tonic-gate 		rc = EEXIST;
15490Sstevel@tonic-gate 		IPSA_REFRELE(target);
15500Sstevel@tonic-gate 	} else {
15510Sstevel@tonic-gate 		/*
15520Sstevel@tonic-gate 		 * sadb_insertassoc() also checks for collisions, so
15530Sstevel@tonic-gate 		 * if there's a colliding entry, rc will be set
15540Sstevel@tonic-gate 		 * to EEXIST.
15550Sstevel@tonic-gate 		 */
15560Sstevel@tonic-gate 		rc = sadb_insertassoc(newbie, inbound);
15574987Sdanmcd 		newbie->ipsa_hardexpiretime = gethrestime_sec();
15583448Sdh155122 		newbie->ipsa_hardexpiretime +=
15593448Sdh155122 		    espstack->ipsecesp_larval_timeout;
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	/*
15630Sstevel@tonic-gate 	 * Can exit outbound mutex.  Hold inbound until we're done
15640Sstevel@tonic-gate 	 * with newbie.
15650Sstevel@tonic-gate 	 */
15660Sstevel@tonic-gate 	mutex_exit(&outbound->isaf_lock);
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	if (rc != 0) {
15690Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
15700Sstevel@tonic-gate 		IPSA_REFRELE(newbie);
15713448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, rc,
15723448Sdh155122 		    SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
15730Sstevel@tonic-gate 		return;
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	/* Can write here because I'm still holding the bucket lock. */
15780Sstevel@tonic-gate 	newbie->ipsa_type = SADB_SATYPE_ESP;
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	/*
15816668Smarkfen 	 * Construct successful return message. We have one thing going
15820Sstevel@tonic-gate 	 * for us in PF_KEY v2.  That's the fact that
15830Sstevel@tonic-gate 	 *	sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
15840Sstevel@tonic-gate 	 */
15850Sstevel@tonic-gate 	assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
15860Sstevel@tonic-gate 	assoc->sadb_sa_exttype = SADB_EXT_SA;
15870Sstevel@tonic-gate 	assoc->sadb_sa_spi = newbie->ipsa_spi;
15880Sstevel@tonic-gate 	*((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
15890Sstevel@tonic-gate 	mutex_exit(&inbound->isaf_lock);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	/* Convert KEYSOCK_IN to KEYSOCK_OUT. */
15920Sstevel@tonic-gate 	kso = (keysock_out_t *)ksi;
15930Sstevel@tonic-gate 	kso->ks_out_len = sizeof (*kso);
15940Sstevel@tonic-gate 	kso->ks_out_serial = ksi->ks_in_serial;
15950Sstevel@tonic-gate 	kso->ks_out_type = KEYSOCK_OUT;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	/*
15980Sstevel@tonic-gate 	 * Can safely putnext() to esp_pfkey_q, because this is a turnaround
15990Sstevel@tonic-gate 	 * from the esp_pfkey_q.
16000Sstevel@tonic-gate 	 */
16013448Sdh155122 	putnext(espstack->esp_pfkey_q, mp);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate /*
16050Sstevel@tonic-gate  * Insert the ESP header into a packet.  Duplicate an mblk, and insert a newly
16060Sstevel@tonic-gate  * allocated mblk with the ESP header in between the two.
16070Sstevel@tonic-gate  */
16080Sstevel@tonic-gate static boolean_t
16093448Sdh155122 esp_insert_esp(mblk_t *mp, mblk_t *esp_mp, uint_t divpoint,
16103448Sdh155122     ipsecesp_stack_t *espstack)
16110Sstevel@tonic-gate {
16120Sstevel@tonic-gate 	mblk_t *split_mp = mp;
16130Sstevel@tonic-gate 	uint_t wheretodiv = divpoint;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	while ((split_mp->b_wptr - split_mp->b_rptr) < wheretodiv) {
16160Sstevel@tonic-gate 		wheretodiv -= (split_mp->b_wptr - split_mp->b_rptr);
16170Sstevel@tonic-gate 		split_mp = split_mp->b_cont;
16180Sstevel@tonic-gate 		ASSERT(split_mp != NULL);
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	if (split_mp->b_wptr - split_mp->b_rptr != wheretodiv) {
16220Sstevel@tonic-gate 		mblk_t *scratch;
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 		/* "scratch" is the 2nd half, split_mp is the first. */
16250Sstevel@tonic-gate 		scratch = dupb(split_mp);
16260Sstevel@tonic-gate 		if (scratch == NULL) {
16273448Sdh155122 			esp1dbg(espstack,
16283448Sdh155122 			    ("esp_insert_esp: can't allocate scratch.\n"));
16290Sstevel@tonic-gate 			return (B_FALSE);
16300Sstevel@tonic-gate 		}
16310Sstevel@tonic-gate 		/* NOTE:  dupb() doesn't set b_cont appropriately. */
16320Sstevel@tonic-gate 		scratch->b_cont = split_mp->b_cont;
16330Sstevel@tonic-gate 		scratch->b_rptr += wheretodiv;
16340Sstevel@tonic-gate 		split_mp->b_wptr = split_mp->b_rptr + wheretodiv;
16350Sstevel@tonic-gate 		split_mp->b_cont = scratch;
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate 	/*
16380Sstevel@tonic-gate 	 * At this point, split_mp is exactly "wheretodiv" bytes long, and
16390Sstevel@tonic-gate 	 * holds the end of the pre-ESP part of the datagram.
16400Sstevel@tonic-gate 	 */
16410Sstevel@tonic-gate 	esp_mp->b_cont = split_mp->b_cont;
16420Sstevel@tonic-gate 	split_mp->b_cont = esp_mp;
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	return (B_TRUE);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate /*
16487066Sdanmcd  * Section 7 of RFC 3947 says:
16497066Sdanmcd  *
16507066Sdanmcd  * 7.  Recovering from the Expiring NAT Mappings
16517066Sdanmcd  *
16527066Sdanmcd  *    There are cases where NAT box decides to remove mappings that are still
16537066Sdanmcd  *    alive (for example, when the keepalive interval is too long, or when the
16547066Sdanmcd  *    NAT box is rebooted).  To recover from this, ends that are NOT behind
16557066Sdanmcd  *    NAT SHOULD use the last valid UDP encapsulated IKE or IPsec packet from
16567066Sdanmcd  *    the other end to determine which IP and port addresses should be used.
16577066Sdanmcd  *    The host behind dynamic NAT MUST NOT do this, as otherwise it opens a
16587066Sdanmcd  *    DoS attack possibility because the IP address or port of the other host
16597066Sdanmcd  *    will not change (it is not behind NAT).
16607066Sdanmcd  *
16617066Sdanmcd  *    Keepalives cannot be used for these purposes, as they are not
16627066Sdanmcd  *    authenticated, but any IKE authenticated IKE packet or ESP packet can be
16637066Sdanmcd  *    used to detect whether the IP address or the port has changed.
16647066Sdanmcd  *
16657066Sdanmcd  * The following function will check an SA and its explicitly-set pair to see
16667066Sdanmcd  * if the NAT-T remote port matches the received packet (which must have
16677066Sdanmcd  * passed ESP authentication, see esp_in_done() for the caller context).  If
16687066Sdanmcd  * there is a mismatch, the SAs are updated.  It is not important if we race
16697066Sdanmcd  * with a transmitting thread, as if there is a transmitting thread, it will
16707066Sdanmcd  * merely emit a packet that will most-likely be dropped.
16717066Sdanmcd  *
16727066Sdanmcd  * "ports" are ordered src,dst, and assoc is an inbound SA, where src should
16737066Sdanmcd  * match ipsa_remote_nat_port and dst should match ipsa_local_nat_port.
16747066Sdanmcd  */
16757066Sdanmcd #ifdef _LITTLE_ENDIAN
16767066Sdanmcd #define	FIRST_16(x) ((x) & 0xFFFF)
16777066Sdanmcd #define	NEXT_16(x) (((x) >> 16) & 0xFFFF)
16787066Sdanmcd #else
16797066Sdanmcd #define	FIRST_16(x) (((x) >> 16) & 0xFFFF)
16807066Sdanmcd #define	NEXT_16(x) ((x) & 0xFFFF)
16817066Sdanmcd #endif
16827066Sdanmcd static void
16837066Sdanmcd esp_port_freshness(uint32_t ports, ipsa_t *assoc)
16847066Sdanmcd {
16857066Sdanmcd 	uint16_t remote = FIRST_16(ports);
16867066Sdanmcd 	uint16_t local = NEXT_16(ports);
16877066Sdanmcd 	ipsa_t *outbound_peer;
16887066Sdanmcd 	isaf_t *bucket;
16897066Sdanmcd 	ipsecesp_stack_t *espstack = assoc->ipsa_netstack->netstack_ipsecesp;
16907066Sdanmcd 
16917066Sdanmcd 	/* We found a conn_t, therefore local != 0. */
16927066Sdanmcd 	ASSERT(local != 0);
16937066Sdanmcd 	/* Assume an IPv4 SA. */
16947066Sdanmcd 	ASSERT(assoc->ipsa_addrfam == AF_INET);
16957066Sdanmcd 
16967066Sdanmcd 	/*
16977066Sdanmcd 	 * On-the-wire rport == 0 means something's very wrong.
16987066Sdanmcd 	 * An unpaired SA is also useless to us.
16997066Sdanmcd 	 * If we are behind the NAT, don't bother.
17007066Sdanmcd 	 * A zero local NAT port defaults to 4500, so check that too.
17017066Sdanmcd 	 * And, of course, if the ports already match, we don't need to
17027066Sdanmcd 	 * bother.
17037066Sdanmcd 	 */
17047066Sdanmcd 	if (remote == 0 || assoc->ipsa_otherspi == 0 ||
17057066Sdanmcd 	    (assoc->ipsa_flags & IPSA_F_BEHIND_NAT) ||
17067066Sdanmcd 	    (assoc->ipsa_remote_nat_port == 0 &&
17077066Sdanmcd 	    remote == htons(IPPORT_IKE_NATT)) ||
17087066Sdanmcd 	    remote == assoc->ipsa_remote_nat_port)
17097066Sdanmcd 		return;
17107066Sdanmcd 
17117066Sdanmcd 	/* Try and snag the peer.   NOTE:  Assume IPv4 for now. */
17127066Sdanmcd 	bucket = OUTBOUND_BUCKET_V4(&(espstack->esp_sadb.s_v4),
17137066Sdanmcd 	    assoc->ipsa_srcaddr[0]);
17147066Sdanmcd 	mutex_enter(&bucket->isaf_lock);
17157066Sdanmcd 	outbound_peer = ipsec_getassocbyspi(bucket, assoc->ipsa_otherspi,
17167066Sdanmcd 	    assoc->ipsa_dstaddr, assoc->ipsa_srcaddr, AF_INET);
17177066Sdanmcd 	mutex_exit(&bucket->isaf_lock);
17187066Sdanmcd 
17197066Sdanmcd 	/* We probably lost a race to a deleting or expiring thread. */
17207066Sdanmcd 	if (outbound_peer == NULL)
17217066Sdanmcd 		return;
17227066Sdanmcd 
17237066Sdanmcd 	/*
17247066Sdanmcd 	 * Hold the mutexes for both SAs so we don't race another inbound
17257066Sdanmcd 	 * thread.  A lock-entry order shouldn't matter, since all other
17267066Sdanmcd 	 * per-ipsa locks are individually held-then-released.
17277066Sdanmcd 	 *
17287066Sdanmcd 	 * Luckily, this has nothing to do with the remote-NAT address,
17297066Sdanmcd 	 * so we don't have to re-scribble the cached-checksum differential.
17307066Sdanmcd 	 */
17317066Sdanmcd 	mutex_enter(&outbound_peer->ipsa_lock);
17327066Sdanmcd 	mutex_enter(&assoc->ipsa_lock);
17337066Sdanmcd 	outbound_peer->ipsa_remote_nat_port = assoc->ipsa_remote_nat_port =
17347066Sdanmcd 	    remote;
17357066Sdanmcd 	mutex_exit(&assoc->ipsa_lock);
17367066Sdanmcd 	mutex_exit(&outbound_peer->ipsa_lock);
17377066Sdanmcd 	IPSA_REFRELE(outbound_peer);
17387066Sdanmcd 	ESP_BUMP_STAT(espstack, sa_port_renumbers);
17397066Sdanmcd }
17407066Sdanmcd 
17417066Sdanmcd /*
17420Sstevel@tonic-gate  * Finish processing of an inbound ESP packet after processing by the
17430Sstevel@tonic-gate  * crypto framework.
17440Sstevel@tonic-gate  * - Remove the ESP header.
17450Sstevel@tonic-gate  * - Send packet back to IP.
17460Sstevel@tonic-gate  * If authentication was performed on the packet, this function is called
17470Sstevel@tonic-gate  * only if the authentication succeeded.
17480Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
17490Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
17500Sstevel@tonic-gate  */
17510Sstevel@tonic-gate static ipsec_status_t
17520Sstevel@tonic-gate esp_in_done(mblk_t *ipsec_in_mp)
17530Sstevel@tonic-gate {
17540Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
17550Sstevel@tonic-gate 	mblk_t *data_mp;
17560Sstevel@tonic-gate 	ipsa_t *assoc;
17570Sstevel@tonic-gate 	uint_t espstart;
17580Sstevel@tonic-gate 	uint32_t ivlen = 0;
17590Sstevel@tonic-gate 	uint_t processed_len;
17600Sstevel@tonic-gate 	esph_t *esph;
17610Sstevel@tonic-gate 	kstat_named_t *counter;
17620Sstevel@tonic-gate 	boolean_t is_natt;
17633448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
17643448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
17653448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	assoc = ii->ipsec_in_esp_sa;
17680Sstevel@tonic-gate 	ASSERT(assoc != NULL);
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	/* get the pointer to the ESP header */
17730Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg == SADB_EALG_NULL) {
17740Sstevel@tonic-gate 		/* authentication-only ESP */
17750Sstevel@tonic-gate 		espstart = ii->ipsec_in_crypto_data.cd_offset;
17760Sstevel@tonic-gate 		processed_len = ii->ipsec_in_crypto_data.cd_length;
17770Sstevel@tonic-gate 	} else {
17780Sstevel@tonic-gate 		/* encryption present */
17790Sstevel@tonic-gate 		ivlen = assoc->ipsa_iv_len;
17800Sstevel@tonic-gate 		if (assoc->ipsa_auth_alg == SADB_AALG_NONE) {
17810Sstevel@tonic-gate 			/* encryption-only ESP */
17820Sstevel@tonic-gate 			espstart = ii->ipsec_in_crypto_data.cd_offset -
17834987Sdanmcd 			    sizeof (esph_t) - assoc->ipsa_iv_len;
17840Sstevel@tonic-gate 			processed_len = ii->ipsec_in_crypto_data.cd_length +
17854987Sdanmcd 			    ivlen;
17860Sstevel@tonic-gate 		} else {
17870Sstevel@tonic-gate 			/* encryption with authentication */
17880Sstevel@tonic-gate 			espstart = ii->ipsec_in_crypto_dual_data.dd_offset1;
17890Sstevel@tonic-gate 			processed_len = ii->ipsec_in_crypto_dual_data.dd_len2 +
17900Sstevel@tonic-gate 			    ivlen;
17910Sstevel@tonic-gate 		}
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	data_mp = ipsec_in_mp->b_cont;
17950Sstevel@tonic-gate 	esph = (esph_t *)(data_mp->b_rptr + espstart);
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) {
17980Sstevel@tonic-gate 		/* authentication passed if we reach this point */
17993448Sdh155122 		ESP_BUMP_STAT(espstack, good_auth);
18000Sstevel@tonic-gate 		data_mp->b_wptr -= assoc->ipsa_mac_len;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 		/*
18030Sstevel@tonic-gate 		 * Check replay window here!
18040Sstevel@tonic-gate 		 * For right now, assume keysock will set the replay window
18050Sstevel@tonic-gate 		 * size to zero for SAs that have an unspecified sender.
18060Sstevel@tonic-gate 		 * This may change...
18070Sstevel@tonic-gate 		 */
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 		if (!sadb_replay_check(assoc, esph->esph_replay)) {
18100Sstevel@tonic-gate 			/*
18110Sstevel@tonic-gate 			 * Log the event. As of now we print out an event.
18120Sstevel@tonic-gate 			 * Do not print the replay failure number, or else
18130Sstevel@tonic-gate 			 * syslog cannot collate the error messages.  Printing
18140Sstevel@tonic-gate 			 * the replay number that failed opens a denial-of-
18150Sstevel@tonic-gate 			 * service attack.
18160Sstevel@tonic-gate 			 */
18170Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
18180Sstevel@tonic-gate 			    SL_ERROR | SL_WARN,
18190Sstevel@tonic-gate 			    "Replay failed for ESP spi 0x%x, dst %s.\n",
18200Sstevel@tonic-gate 			    assoc->ipsa_spi, assoc->ipsa_dstaddr,
18213448Sdh155122 			    assoc->ipsa_addrfam, espstack->ipsecesp_netstack);
18223448Sdh155122 			ESP_BUMP_STAT(espstack, replay_failures);
18233448Sdh155122 			counter = DROPPER(ipss, ipds_esp_replay);
18240Sstevel@tonic-gate 			goto drop_and_bail;
18250Sstevel@tonic-gate 		}
18267066Sdanmcd 
18277066Sdanmcd 		if (is_natt)
18287066Sdanmcd 			esp_port_freshness(ii->ipsec_in_esp_udp_ports, assoc);
18290Sstevel@tonic-gate 	}
18300Sstevel@tonic-gate 
18314987Sdanmcd 	esp_set_usetime(assoc, B_TRUE);
18324987Sdanmcd 
18330Sstevel@tonic-gate 	if (!esp_age_bytes(assoc, processed_len, B_TRUE)) {
18340Sstevel@tonic-gate 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
18350Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
18360Sstevel@tonic-gate 		    SL_ERROR | SL_WARN,
18370Sstevel@tonic-gate 		    "ESP association 0x%x, dst %s had bytes expire.\n",
18383448Sdh155122 		    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
18393448Sdh155122 		    espstack->ipsecesp_netstack);
18403448Sdh155122 		ESP_BUMP_STAT(espstack, bytes_expired);
18413448Sdh155122 		counter = DROPPER(ipss, ipds_esp_bytes_expire);
18420Sstevel@tonic-gate 		goto drop_and_bail;
18430Sstevel@tonic-gate 	}
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	/*
18460Sstevel@tonic-gate 	 * Remove ESP header and padding from packet.  I hope the compiler
18470Sstevel@tonic-gate 	 * spews "branch, predict taken" code for this.
18480Sstevel@tonic-gate 	 */
18490Sstevel@tonic-gate 
18503448Sdh155122 	if (esp_strip_header(data_mp, ii->ipsec_in_v4, ivlen, &counter,
18514987Sdanmcd 	    espstack)) {
18520Sstevel@tonic-gate 		if (is_natt)
18530Sstevel@tonic-gate 			return (esp_fix_natt_checksums(data_mp, assoc));
18540Sstevel@tonic-gate 		return (IPSEC_STATUS_SUCCESS);
18550Sstevel@tonic-gate 	}
18560Sstevel@tonic-gate 
18573448Sdh155122 	esp1dbg(espstack, ("esp_in_done: esp_strip_header() failed\n"));
18580Sstevel@tonic-gate drop_and_bail:
18593448Sdh155122 	IP_ESP_BUMP_STAT(ipss, in_discards);
18600Sstevel@tonic-gate 	/*
18610Sstevel@tonic-gate 	 * TODO: Extract inbound interface from the IPSEC_IN message's
18620Sstevel@tonic-gate 	 * ii->ipsec_in_rill_index.
18630Sstevel@tonic-gate 	 */
18643448Sdh155122 	ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, counter,
18653448Sdh155122 	    &espstack->esp_dropper);
18660Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
18670Sstevel@tonic-gate }
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate /*
18700Sstevel@tonic-gate  * Called upon failing the inbound ICV check. The message passed as
18710Sstevel@tonic-gate  * argument is freed.
18720Sstevel@tonic-gate  */
18730Sstevel@tonic-gate static void
18740Sstevel@tonic-gate esp_log_bad_auth(mblk_t *ipsec_in)
18750Sstevel@tonic-gate {
18760Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
18770Sstevel@tonic-gate 	ipsa_t *assoc = ii->ipsec_in_esp_sa;
18783448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
18793448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
18803448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	/*
18830Sstevel@tonic-gate 	 * Log the event. Don't print to the console, block
18840Sstevel@tonic-gate 	 * potential denial-of-service attack.
18850Sstevel@tonic-gate 	 */
18863448Sdh155122 	ESP_BUMP_STAT(espstack, bad_auth);
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
18890Sstevel@tonic-gate 	    "ESP Authentication failed for spi 0x%x, dst %s.\n",
18903448Sdh155122 	    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
18913448Sdh155122 	    espstack->ipsecesp_netstack);
18923448Sdh155122 
18933448Sdh155122 	IP_ESP_BUMP_STAT(ipss, in_discards);
18940Sstevel@tonic-gate 	/*
18950Sstevel@tonic-gate 	 * TODO: Extract inbound interface from the IPSEC_IN
18960Sstevel@tonic-gate 	 * message's ii->ipsec_in_rill_index.
18970Sstevel@tonic-gate 	 */
18983448Sdh155122 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
18993448Sdh155122 	    DROPPER(ipss, ipds_esp_bad_auth),
19003448Sdh155122 	    &espstack->esp_dropper);
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate /*
19050Sstevel@tonic-gate  * Invoked for outbound packets after ESP processing. If the packet
19060Sstevel@tonic-gate  * also requires AH, performs the AH SA selection and AH processing.
19070Sstevel@tonic-gate  * Returns B_TRUE if the AH processing was not needed or if it was
19080Sstevel@tonic-gate  * performed successfully. Returns B_FALSE and consumes the passed mblk
19090Sstevel@tonic-gate  * if AH processing was required but could not be performed.
19100Sstevel@tonic-gate  */
19110Sstevel@tonic-gate static boolean_t
19120Sstevel@tonic-gate esp_do_outbound_ah(mblk_t *ipsec_mp)
19130Sstevel@tonic-gate {
19140Sstevel@tonic-gate 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
19150Sstevel@tonic-gate 	ipsec_status_t ipsec_rc;
19160Sstevel@tonic-gate 	ipsec_action_t *ap;
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	ap = io->ipsec_out_act;
19190Sstevel@tonic-gate 	if (ap == NULL) {
19200Sstevel@tonic-gate 		ipsec_policy_t *pp = io->ipsec_out_policy;
19210Sstevel@tonic-gate 		ap = pp->ipsp_act;
19220Sstevel@tonic-gate 	}
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	if (!ap->ipa_want_ah)
19250Sstevel@tonic-gate 		return (B_TRUE);
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	ASSERT(io->ipsec_out_ah_done == B_FALSE);
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	if (io->ipsec_out_ah_sa == NULL) {
19300Sstevel@tonic-gate 		if (!ipsec_outbound_sa(ipsec_mp, IPPROTO_AH)) {
19310Sstevel@tonic-gate 			sadb_acquire(ipsec_mp, io, B_TRUE, B_FALSE);
19320Sstevel@tonic-gate 			return (B_FALSE);
19330Sstevel@tonic-gate 		}
19340Sstevel@tonic-gate 	}
19350Sstevel@tonic-gate 	ASSERT(io->ipsec_out_ah_sa != NULL);
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	io->ipsec_out_ah_done = B_TRUE;
19380Sstevel@tonic-gate 	ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp);
19390Sstevel@tonic-gate 	return (ipsec_rc == IPSEC_STATUS_SUCCESS);
19400Sstevel@tonic-gate }
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate /*
19440Sstevel@tonic-gate  * Kernel crypto framework callback invoked after completion of async
19450Sstevel@tonic-gate  * crypto requests.
19460Sstevel@tonic-gate  */
19470Sstevel@tonic-gate static void
19480Sstevel@tonic-gate esp_kcf_callback(void *arg, int status)
19490Sstevel@tonic-gate {
19500Sstevel@tonic-gate 	mblk_t *ipsec_mp = (mblk_t *)arg;
19510Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
19524987Sdanmcd 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
19530Sstevel@tonic-gate 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
19543448Sdh155122 	netstackid_t	stackid;
19553448Sdh155122 	netstack_t	*ns, *ns_arg;
19563448Sdh155122 	ipsecesp_stack_t *espstack;
19573448Sdh155122 	ipsec_stack_t	*ipss;
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 	ASSERT(ipsec_mp->b_cont != NULL);
19600Sstevel@tonic-gate 
19613448Sdh155122 	if (is_inbound) {
19623448Sdh155122 		stackid = ii->ipsec_in_stackid;
19633448Sdh155122 		ns_arg = ii->ipsec_in_ns;
19643448Sdh155122 	} else {
19653448Sdh155122 		stackid = io->ipsec_out_stackid;
19663448Sdh155122 		ns_arg = io->ipsec_out_ns;
19673448Sdh155122 	}
19683448Sdh155122 
19693448Sdh155122 	/*
19703448Sdh155122 	 * Verify that the netstack is still around; could have vanished
19713448Sdh155122 	 * while kEf was doing its work.
19723448Sdh155122 	 */
19733448Sdh155122 	ns = netstack_find_by_stackid(stackid);
19743448Sdh155122 	if (ns == NULL || ns != ns_arg) {
19753448Sdh155122 		/* Disappeared on us */
19763448Sdh155122 		if (ns != NULL)
19773448Sdh155122 			netstack_rele(ns);
19783448Sdh155122 		freemsg(ipsec_mp);
19793448Sdh155122 		return;
19803448Sdh155122 	}
19813448Sdh155122 
19823448Sdh155122 	espstack = ns->netstack_ipsecesp;
19833448Sdh155122 	ipss = ns->netstack_ipsec;
19843448Sdh155122 
19850Sstevel@tonic-gate 	if (status == CRYPTO_SUCCESS) {
19860Sstevel@tonic-gate 		if (is_inbound) {
19873448Sdh155122 			if (esp_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS) {
19883448Sdh155122 				netstack_rele(ns);
19890Sstevel@tonic-gate 				return;
19903448Sdh155122 			}
19910Sstevel@tonic-gate 			/* finish IPsec processing */
19920Sstevel@tonic-gate 			ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL);
19930Sstevel@tonic-gate 		} else {
19940Sstevel@tonic-gate 			/*
19950Sstevel@tonic-gate 			 * If a ICV was computed, it was stored by the
19960Sstevel@tonic-gate 			 * crypto framework at the end of the packet.
19970Sstevel@tonic-gate 			 */
19980Sstevel@tonic-gate 			ipha_t *ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
19990Sstevel@tonic-gate 
20004987Sdanmcd 			esp_set_usetime(io->ipsec_out_esp_sa, B_FALSE);
20014987Sdanmcd 			/* NAT-T packet. */
20024987Sdanmcd 			if (ipha->ipha_protocol == IPPROTO_UDP)
20034987Sdanmcd 				esp_prepare_udp(ns, ipsec_mp->b_cont, ipha);
20044987Sdanmcd 
20050Sstevel@tonic-gate 			/* do AH processing if needed */
20063448Sdh155122 			if (!esp_do_outbound_ah(ipsec_mp)) {
20073448Sdh155122 				netstack_rele(ns);
20080Sstevel@tonic-gate 				return;
20093448Sdh155122 			}
20100Sstevel@tonic-gate 			/* finish IPsec processing */
20110Sstevel@tonic-gate 			if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
20120Sstevel@tonic-gate 				ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL,
20130Sstevel@tonic-gate 				    NULL);
20140Sstevel@tonic-gate 			} else {
20150Sstevel@tonic-gate 				ip6_t *ip6h = (ip6_t *)ipha;
20160Sstevel@tonic-gate 				ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h,
20170Sstevel@tonic-gate 				    NULL, NULL);
20180Sstevel@tonic-gate 			}
20190Sstevel@tonic-gate 		}
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	} else if (status == CRYPTO_INVALID_MAC) {
20220Sstevel@tonic-gate 		esp_log_bad_auth(ipsec_mp);
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	} else {
20253448Sdh155122 		esp1dbg(espstack,
20263448Sdh155122 		    ("esp_kcf_callback: crypto failed with 0x%x\n",
20270Sstevel@tonic-gate 		    status));
20283448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_failures);
20290Sstevel@tonic-gate 		if (is_inbound)
20303448Sdh155122 			IP_ESP_BUMP_STAT(ipss, in_discards);
20310Sstevel@tonic-gate 		else
20323448Sdh155122 			ESP_BUMP_STAT(espstack, out_discards);
20330Sstevel@tonic-gate 		ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL,
20343448Sdh155122 		    DROPPER(ipss, ipds_esp_crypto_failed),
20353448Sdh155122 		    &espstack->esp_dropper);
20360Sstevel@tonic-gate 	}
20373448Sdh155122 	netstack_rele(ns);
20380Sstevel@tonic-gate }
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate /*
20410Sstevel@tonic-gate  * Invoked on crypto framework failure during inbound and outbound processing.
20420Sstevel@tonic-gate  */
20430Sstevel@tonic-gate static void
20443448Sdh155122 esp_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc,
20453448Sdh155122     ipsecesp_stack_t *espstack)
20460Sstevel@tonic-gate {
20473448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
20483448Sdh155122 
20493448Sdh155122 	esp1dbg(espstack, ("crypto failed for %s ESP with 0x%x\n",
20500Sstevel@tonic-gate 	    is_inbound ? "inbound" : "outbound", kef_rc));
20513448Sdh155122 	ip_drop_packet(mp, is_inbound, NULL, NULL,
20523448Sdh155122 	    DROPPER(ipss, ipds_esp_crypto_failed),
20533448Sdh155122 	    &espstack->esp_dropper);
20543448Sdh155122 	ESP_BUMP_STAT(espstack, crypto_failures);
20550Sstevel@tonic-gate 	if (is_inbound)
20563448Sdh155122 		IP_ESP_BUMP_STAT(ipss, in_discards);
20570Sstevel@tonic-gate 	else
20583448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
20590Sstevel@tonic-gate }
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate #define	ESP_INIT_CALLREQ(_cr) {						\
20620Sstevel@tonic-gate 	(_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED;		\
20630Sstevel@tonic-gate 	(_cr)->cr_callback_arg = ipsec_mp;				\
20640Sstevel@tonic-gate 	(_cr)->cr_callback_func = esp_kcf_callback;			\
20650Sstevel@tonic-gate }
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) {			\
20680Sstevel@tonic-gate 	(mac)->cd_format = CRYPTO_DATA_RAW;				\
20690Sstevel@tonic-gate 	(mac)->cd_offset = 0;						\
20700Sstevel@tonic-gate 	(mac)->cd_length = icvlen;					\
20710Sstevel@tonic-gate 	(mac)->cd_raw.iov_base = (char *)icvbuf;			\
20720Sstevel@tonic-gate 	(mac)->cd_raw.iov_len = icvlen;					\
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_DATA(data, mp, off, len) {			\
20760Sstevel@tonic-gate 	if (MBLKL(mp) >= (len) + (off)) {				\
20770Sstevel@tonic-gate 		(data)->cd_format = CRYPTO_DATA_RAW;			\
20780Sstevel@tonic-gate 		(data)->cd_raw.iov_base = (char *)(mp)->b_rptr;		\
20790Sstevel@tonic-gate 		(data)->cd_raw.iov_len = MBLKL(mp);			\
20800Sstevel@tonic-gate 		(data)->cd_offset = off;				\
20810Sstevel@tonic-gate 	} else {							\
20820Sstevel@tonic-gate 		(data)->cd_format = CRYPTO_DATA_MBLK;			\
20830Sstevel@tonic-gate 		(data)->cd_mp = mp;			       		\
20840Sstevel@tonic-gate 		(data)->cd_offset = off;				\
20850Sstevel@tonic-gate 	}								\
20860Sstevel@tonic-gate 	(data)->cd_length = len;					\
20870Sstevel@tonic-gate }
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_DUAL_DATA(data, mp, off1, len1, off2, len2) {	\
20900Sstevel@tonic-gate 	(data)->dd_format = CRYPTO_DATA_MBLK;				\
20910Sstevel@tonic-gate 	(data)->dd_mp = mp;						\
20920Sstevel@tonic-gate 	(data)->dd_len1 = len1;						\
20930Sstevel@tonic-gate 	(data)->dd_offset1 = off1;					\
20940Sstevel@tonic-gate 	(data)->dd_len2 = len2;						\
20950Sstevel@tonic-gate 	(data)->dd_offset2 = off2;					\
20960Sstevel@tonic-gate }
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate static ipsec_status_t
20990Sstevel@tonic-gate esp_submit_req_inbound(mblk_t *ipsec_mp, ipsa_t *assoc, uint_t esph_offset)
21000Sstevel@tonic-gate {
21010Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
21020Sstevel@tonic-gate 	boolean_t do_auth;
21030Sstevel@tonic-gate 	uint_t auth_offset, msg_len, auth_len;
21040Sstevel@tonic-gate 	crypto_call_req_t call_req;
21050Sstevel@tonic-gate 	mblk_t *esp_mp;
21060Sstevel@tonic-gate 	int kef_rc = CRYPTO_FAILED;
21070Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
21080Sstevel@tonic-gate 	crypto_ctx_template_t auth_ctx_tmpl;
21090Sstevel@tonic-gate 	boolean_t do_encr;
21100Sstevel@tonic-gate 	uint_t encr_offset, encr_len;
21110Sstevel@tonic-gate 	uint_t iv_len = assoc->ipsa_iv_len;
21120Sstevel@tonic-gate 	crypto_ctx_template_t encr_ctx_tmpl;
21133448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
21143448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
21153448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
21180Sstevel@tonic-gate 
21193448Sdh155122 	/*
21203448Sdh155122 	 * In case kEF queues and calls back, keep netstackid_t for
21213448Sdh155122 	 * verification that the IP instance is still around in
21223448Sdh155122 	 * esp_kcf_callback().
21233448Sdh155122 	 */
21243448Sdh155122 	ii->ipsec_in_stackid = ns->netstack_stackid;
21253448Sdh155122 
21260Sstevel@tonic-gate 	do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
21270Sstevel@tonic-gate 	do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 	/*
21300Sstevel@tonic-gate 	 * An inbound packet is of the form:
21310Sstevel@tonic-gate 	 * IPSEC_IN -> [IP,options,ESP,IV,data,ICV,pad]
21320Sstevel@tonic-gate 	 */
21330Sstevel@tonic-gate 	esp_mp = ipsec_mp->b_cont;
21340Sstevel@tonic-gate 	msg_len = MBLKL(esp_mp);
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	ESP_INIT_CALLREQ(&call_req);
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	if (do_auth) {
21390Sstevel@tonic-gate 		/* force asynchronous processing? */
21403448Sdh155122 		if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
21410Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
21420Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 		/* authentication context template */
21450Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
21460Sstevel@tonic-gate 		    auth_ctx_tmpl);
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 		/* ICV to be verified */
21490Sstevel@tonic-gate 		ESP_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac,
21500Sstevel@tonic-gate 		    icv_len, esp_mp->b_wptr - icv_len);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 		/* authentication starts at the ESP header */
21530Sstevel@tonic-gate 		auth_offset = esph_offset;
21540Sstevel@tonic-gate 		auth_len = msg_len - auth_offset - icv_len;
21550Sstevel@tonic-gate 		if (!do_encr) {
21560Sstevel@tonic-gate 			/* authentication only */
21570Sstevel@tonic-gate 			/* initialize input data argument */
21580Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data,
21590Sstevel@tonic-gate 			    esp_mp, auth_offset, auth_len);
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 			/* call the crypto framework */
21620Sstevel@tonic-gate 			kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
21630Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_data,
21640Sstevel@tonic-gate 			    &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
21650Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_mac, &call_req);
21660Sstevel@tonic-gate 		}
21670Sstevel@tonic-gate 	}
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 	if (do_encr) {
21700Sstevel@tonic-gate 		/* force asynchronous processing? */
21713448Sdh155122 		if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
21720Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
21730Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 		/* encryption template */
21760Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
21770Sstevel@tonic-gate 		    encr_ctx_tmpl);
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 		/* skip IV, since it is passed separately */
21800Sstevel@tonic-gate 		encr_offset = esph_offset + sizeof (esph_t) + iv_len;
21810Sstevel@tonic-gate 		encr_len = msg_len - encr_offset;
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 		if (!do_auth) {
21840Sstevel@tonic-gate 			/* decryption only */
21850Sstevel@tonic-gate 			/* initialize input data argument */
21860Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data,
21870Sstevel@tonic-gate 			    esp_mp, encr_offset, encr_len);
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 			/* specify IV */
21900Sstevel@tonic-gate 			ii->ipsec_in_crypto_data.cd_miscdata =
21910Sstevel@tonic-gate 			    (char *)esp_mp->b_rptr + sizeof (esph_t) +
21920Sstevel@tonic-gate 			    esph_offset;
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 			/* call the crypto framework */
21950Sstevel@tonic-gate 			kef_rc = crypto_decrypt(&assoc->ipsa_emech,
21960Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_data,
21970Sstevel@tonic-gate 			    &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
21980Sstevel@tonic-gate 			    NULL, &call_req);
21990Sstevel@tonic-gate 		}
22000Sstevel@tonic-gate 	}
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	if (do_auth && do_encr) {
22030Sstevel@tonic-gate 		/* dual operation */
22040Sstevel@tonic-gate 		/* initialize input data argument */
22050Sstevel@tonic-gate 		ESP_INIT_CRYPTO_DUAL_DATA(&ii->ipsec_in_crypto_dual_data,
22060Sstevel@tonic-gate 		    esp_mp, auth_offset, auth_len,
22070Sstevel@tonic-gate 		    encr_offset, encr_len - icv_len);
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 		/* specify IV */
22100Sstevel@tonic-gate 		ii->ipsec_in_crypto_dual_data.dd_miscdata =
22110Sstevel@tonic-gate 		    (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset;
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 		/* call the framework */
22140Sstevel@tonic-gate 		kef_rc = crypto_mac_verify_decrypt(&assoc->ipsa_amech,
22150Sstevel@tonic-gate 		    &assoc->ipsa_emech, &ii->ipsec_in_crypto_dual_data,
22160Sstevel@tonic-gate 		    &assoc->ipsa_kcfauthkey, &assoc->ipsa_kcfencrkey,
22170Sstevel@tonic-gate 		    auth_ctx_tmpl, encr_ctx_tmpl, &ii->ipsec_in_crypto_mac,
22180Sstevel@tonic-gate 		    NULL, &call_req);
22190Sstevel@tonic-gate 	}
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 	switch (kef_rc) {
22220Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
22233448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_sync);
22240Sstevel@tonic-gate 		return (esp_in_done(ipsec_mp));
22250Sstevel@tonic-gate 	case CRYPTO_QUEUED:
22260Sstevel@tonic-gate 		/* esp_kcf_callback() will be invoked on completion */
22273448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_async);
22280Sstevel@tonic-gate 		return (IPSEC_STATUS_PENDING);
22290Sstevel@tonic-gate 	case CRYPTO_INVALID_MAC:
22303448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_sync);
22310Sstevel@tonic-gate 		esp_log_bad_auth(ipsec_mp);
22320Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
22330Sstevel@tonic-gate 	}
22340Sstevel@tonic-gate 
22353448Sdh155122 	esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc, espstack);
22360Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
22370Sstevel@tonic-gate }
22380Sstevel@tonic-gate 
22394987Sdanmcd /*
22404987Sdanmcd  * Compute the IP and UDP checksums -- common code for both keepalives and
22414987Sdanmcd  * actual ESP-in-UDP packets.  Be flexible with multiple mblks because ESP
22424987Sdanmcd  * uses mblk-insertion to insert the UDP header.
22434987Sdanmcd  * TODO - If there is an easy way to prep a packet for HW checksums, make
22444987Sdanmcd  * it happen here.
22454987Sdanmcd  */
22464987Sdanmcd static void
22474987Sdanmcd esp_prepare_udp(netstack_t *ns, mblk_t *mp, ipha_t *ipha)
22484987Sdanmcd {
22494987Sdanmcd 	int offset;
22504987Sdanmcd 	uint32_t cksum;
22514987Sdanmcd 	uint16_t *arr;
22524987Sdanmcd 	mblk_t *udpmp = mp;
22536352Swy83408 	uint_t hlen = IPH_HDR_LENGTH(ipha);
22544987Sdanmcd 
22554987Sdanmcd 	ASSERT(MBLKL(mp) >= sizeof (ipha_t));
22564987Sdanmcd 
22574987Sdanmcd 	ipha->ipha_hdr_checksum = 0;
22584987Sdanmcd 	ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
22594987Sdanmcd 
22604987Sdanmcd 	if (ns->netstack_udp->us_do_checksum) {
22614987Sdanmcd 		ASSERT(MBLKL(udpmp) >= sizeof (udpha_t));
22624987Sdanmcd 		/* arr points to the IP header. */
22634987Sdanmcd 		arr = (uint16_t *)ipha;
22644987Sdanmcd 		IP_STAT(ns->netstack_ip, ip_out_sw_cksum);
22654987Sdanmcd 		IP_STAT_UPDATE(ns->netstack_ip, ip_udp_out_sw_cksum_bytes,
22666352Swy83408 		    ntohs(htons(ipha->ipha_length) - hlen));
22674987Sdanmcd 		/* arr[6-9] are the IP addresses. */
22684987Sdanmcd 		cksum = IP_UDP_CSUM_COMP + arr[6] + arr[7] + arr[8] + arr[9] +
22696352Swy83408 		    ntohs(htons(ipha->ipha_length) - hlen);
22706352Swy83408 		cksum = IP_CSUM(mp, hlen, cksum);
22716352Swy83408 		offset = hlen + UDP_CHECKSUM_OFFSET;
22724987Sdanmcd 		while (offset >= MBLKL(udpmp)) {
22734987Sdanmcd 			offset -= MBLKL(udpmp);
22744987Sdanmcd 			udpmp = udpmp->b_cont;
22754987Sdanmcd 		}
22764987Sdanmcd 		/* arr points to the UDP header's checksum field. */
22774987Sdanmcd 		arr = (uint16_t *)(udpmp->b_rptr + offset);
22784987Sdanmcd 		*arr = cksum;
22794987Sdanmcd 	}
22804987Sdanmcd }
22814987Sdanmcd 
22824987Sdanmcd /*
22834987Sdanmcd  * Send a one-byte UDP NAT-T keepalive.  Construct an IPSEC_OUT too that'll
22844987Sdanmcd  * get fed into esp_send_udp/ip_wput_ipsec_out.
22854987Sdanmcd  */
22864987Sdanmcd void
22874987Sdanmcd ipsecesp_send_keepalive(ipsa_t *assoc)
22884987Sdanmcd {
22894987Sdanmcd 	mblk_t *mp = NULL, *ipsec_mp = NULL;
22904987Sdanmcd 	ipha_t *ipha;
22914987Sdanmcd 	udpha_t *udpha;
22924987Sdanmcd 	ipsec_out_t *io;
22934987Sdanmcd 
22944987Sdanmcd 	ASSERT(!MUTEX_HELD(&assoc->ipsa_lock));
22954987Sdanmcd 
22964987Sdanmcd 	mp = allocb(sizeof (ipha_t) + sizeof (udpha_t) + 1, BPRI_HI);
22974987Sdanmcd 	if (mp == NULL)
22984987Sdanmcd 		return;
22994987Sdanmcd 	ipha = (ipha_t *)mp->b_rptr;
23004987Sdanmcd 	ipha->ipha_version_and_hdr_length = IP_SIMPLE_HDR_VERSION;
23014987Sdanmcd 	ipha->ipha_type_of_service = 0;
23024987Sdanmcd 	ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (udpha_t) + 1);
23034987Sdanmcd 	/* Use the low-16 of the SPI so we have some clue where it came from. */
23044987Sdanmcd 	ipha->ipha_ident = *(((uint16_t *)(&assoc->ipsa_spi)) + 1);
23054987Sdanmcd 	ipha->ipha_fragment_offset_and_flags = 0;  /* Too small to fragment! */
23064987Sdanmcd 	ipha->ipha_ttl = 0xFF;
23074987Sdanmcd 	ipha->ipha_protocol = IPPROTO_UDP;
23084987Sdanmcd 	ipha->ipha_hdr_checksum = 0;
23094987Sdanmcd 	ipha->ipha_src = assoc->ipsa_srcaddr[0];
23104987Sdanmcd 	ipha->ipha_dst = assoc->ipsa_dstaddr[0];
23114987Sdanmcd 	udpha = (udpha_t *)(ipha + 1);
23124987Sdanmcd 	udpha->uha_src_port = (assoc->ipsa_local_nat_port != 0) ?
23134987Sdanmcd 	    assoc->ipsa_local_nat_port : htons(IPPORT_IKE_NATT);
23144987Sdanmcd 	udpha->uha_dst_port = (assoc->ipsa_remote_nat_port != 0) ?
23154987Sdanmcd 	    assoc->ipsa_remote_nat_port : htons(IPPORT_IKE_NATT);
23164987Sdanmcd 	udpha->uha_length = htons(sizeof (udpha_t) + 1);
23174987Sdanmcd 	udpha->uha_checksum = 0;
23184987Sdanmcd 	mp->b_wptr = (uint8_t *)(udpha + 1);
23194987Sdanmcd 	*(mp->b_wptr++) = 0xFF;
23204987Sdanmcd 
23214987Sdanmcd 	ipsec_mp = ipsec_alloc_ipsec_out(assoc->ipsa_netstack);
23224987Sdanmcd 	if (ipsec_mp == NULL) {
23234987Sdanmcd 		freeb(mp);
23244987Sdanmcd 		return;
23254987Sdanmcd 	}
23264987Sdanmcd 	ipsec_mp->b_cont = mp;
23274987Sdanmcd 	io = (ipsec_out_t *)ipsec_mp->b_rptr;
23284987Sdanmcd 	io->ipsec_out_zoneid =
23294987Sdanmcd 	    netstackid_to_zoneid(assoc->ipsa_netstack->netstack_stackid);
23304987Sdanmcd 
23314987Sdanmcd 	esp_prepare_udp(assoc->ipsa_netstack, mp, ipha);
23324987Sdanmcd 	ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL, NULL);
23334987Sdanmcd }
23344987Sdanmcd 
23350Sstevel@tonic-gate static ipsec_status_t
23360Sstevel@tonic-gate esp_submit_req_outbound(mblk_t *ipsec_mp, ipsa_t *assoc, uchar_t *icv_buf,
23370Sstevel@tonic-gate     uint_t payload_len)
23380Sstevel@tonic-gate {
23390Sstevel@tonic-gate 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
23400Sstevel@tonic-gate 	uint_t auth_len;
23410Sstevel@tonic-gate 	crypto_call_req_t call_req;
23420Sstevel@tonic-gate 	mblk_t *esp_mp;
23430Sstevel@tonic-gate 	int kef_rc = CRYPTO_FAILED;
23440Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
23450Sstevel@tonic-gate 	crypto_ctx_template_t auth_ctx_tmpl;
23460Sstevel@tonic-gate 	boolean_t do_auth;
23470Sstevel@tonic-gate 	boolean_t do_encr;
23480Sstevel@tonic-gate 	uint_t iv_len = assoc->ipsa_iv_len;
23490Sstevel@tonic-gate 	crypto_ctx_template_t encr_ctx_tmpl;
23500Sstevel@tonic-gate 	boolean_t is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
23510Sstevel@tonic-gate 	size_t esph_offset = (is_natt ? UDPH_SIZE : 0);
23523448Sdh155122 	netstack_t	*ns = io->ipsec_out_ns;
23533448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
23543448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
23553448Sdh155122 
23563448Sdh155122 	esp3dbg(espstack, ("esp_submit_req_outbound:%s",
23574987Sdanmcd 	    is_natt ? "natt" : "not natt"));
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
23600Sstevel@tonic-gate 
23613448Sdh155122 	/*
23623448Sdh155122 	 * In case kEF queues and calls back, keep netstackid_t for
23633448Sdh155122 	 * verification that the IP instance is still around in
23643448Sdh155122 	 * esp_kcf_callback().
23653448Sdh155122 	 */
23663448Sdh155122 	io->ipsec_out_stackid = ns->netstack_stackid;
23673448Sdh155122 
23680Sstevel@tonic-gate 	do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
23690Sstevel@tonic-gate 	do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	/*
23720Sstevel@tonic-gate 	 * Outbound IPsec packets are of the form:
23730Sstevel@tonic-gate 	 * IPSEC_OUT -> [IP,options] -> [ESP,IV] -> [data] -> [pad,ICV]
23740Sstevel@tonic-gate 	 * unless it's NATT, then it's
23750Sstevel@tonic-gate 	 * IPSEC_OUT -> [IP,options] -> [udp][ESP,IV] -> [data] -> [pad,ICV]
23760Sstevel@tonic-gate 	 * Get a pointer to the mblk containing the ESP header.
23770Sstevel@tonic-gate 	 */
23780Sstevel@tonic-gate 	ASSERT(ipsec_mp->b_cont != NULL && ipsec_mp->b_cont->b_cont != NULL);
23790Sstevel@tonic-gate 	esp_mp = ipsec_mp->b_cont->b_cont;
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	ESP_INIT_CALLREQ(&call_req);
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	if (do_auth) {
23840Sstevel@tonic-gate 		/* force asynchronous processing? */
23853448Sdh155122 		if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
23860Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
23870Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 		/* authentication context template */
23900Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
23910Sstevel@tonic-gate 		    auth_ctx_tmpl);
23920Sstevel@tonic-gate 
23930Sstevel@tonic-gate 		/* where to store the computed mac */
23940Sstevel@tonic-gate 		ESP_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac,
23950Sstevel@tonic-gate 		    icv_len, icv_buf);
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 		/* authentication starts at the ESP header */
2398134Sdanmcd 		auth_len = payload_len + iv_len + sizeof (esph_t);
23990Sstevel@tonic-gate 		if (!do_encr) {
24000Sstevel@tonic-gate 			/* authentication only */
24010Sstevel@tonic-gate 			/* initialize input data argument */
24020Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data,
24030Sstevel@tonic-gate 			    esp_mp, esph_offset, auth_len);
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 			/* call the crypto framework */
24060Sstevel@tonic-gate 			kef_rc = crypto_mac(&assoc->ipsa_amech,
24070Sstevel@tonic-gate 			    &io->ipsec_out_crypto_data,
24080Sstevel@tonic-gate 			    &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
24090Sstevel@tonic-gate 			    &io->ipsec_out_crypto_mac, &call_req);
24100Sstevel@tonic-gate 		}
24110Sstevel@tonic-gate 	}
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 	if (do_encr) {
24140Sstevel@tonic-gate 		/* force asynchronous processing? */
24153448Sdh155122 		if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
24160Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
24170Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 		/* encryption context template */
24200Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
24210Sstevel@tonic-gate 		    encr_ctx_tmpl);
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 		if (!do_auth) {
24240Sstevel@tonic-gate 			/* encryption only, skip mblk that contains ESP hdr */
24250Sstevel@tonic-gate 			/* initialize input data argument */
24260Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data,
24270Sstevel@tonic-gate 			    esp_mp->b_cont, 0, payload_len);
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 			/* specify IV */
24300Sstevel@tonic-gate 			io->ipsec_out_crypto_data.cd_miscdata =
24310Sstevel@tonic-gate 			    (char *)esp_mp->b_rptr + sizeof (esph_t) +
24320Sstevel@tonic-gate 			    esph_offset;
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 			/* call the crypto framework */
24350Sstevel@tonic-gate 			kef_rc = crypto_encrypt(&assoc->ipsa_emech,
24360Sstevel@tonic-gate 			    &io->ipsec_out_crypto_data,
24370Sstevel@tonic-gate 			    &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
24380Sstevel@tonic-gate 			    NULL, &call_req);
24390Sstevel@tonic-gate 		}
24400Sstevel@tonic-gate 	}
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 	if (do_auth && do_encr) {
24430Sstevel@tonic-gate 		/*
24440Sstevel@tonic-gate 		 * Encryption and authentication:
24450Sstevel@tonic-gate 		 * Pass the pointer to the mblk chain starting at the ESP
24460Sstevel@tonic-gate 		 * header to the framework. Skip the ESP header mblk
24470Sstevel@tonic-gate 		 * for encryption, which is reflected by an encryption
24480Sstevel@tonic-gate 		 * offset equal to the length of that mblk. Start
24490Sstevel@tonic-gate 		 * the authentication at the ESP header, i.e. use an
24500Sstevel@tonic-gate 		 * authentication offset of zero.
24510Sstevel@tonic-gate 		 */
24520Sstevel@tonic-gate 		ESP_INIT_CRYPTO_DUAL_DATA(&io->ipsec_out_crypto_dual_data,
24530Sstevel@tonic-gate 		    esp_mp, MBLKL(esp_mp), payload_len, esph_offset, auth_len);
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 		/* specify IV */
24560Sstevel@tonic-gate 		io->ipsec_out_crypto_dual_data.dd_miscdata =
24570Sstevel@tonic-gate 		    (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset;
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 		/* call the framework */
24600Sstevel@tonic-gate 		kef_rc = crypto_encrypt_mac(&assoc->ipsa_emech,
24610Sstevel@tonic-gate 		    &assoc->ipsa_amech, NULL,
24620Sstevel@tonic-gate 		    &assoc->ipsa_kcfencrkey, &assoc->ipsa_kcfauthkey,
24630Sstevel@tonic-gate 		    encr_ctx_tmpl, auth_ctx_tmpl,
24640Sstevel@tonic-gate 		    &io->ipsec_out_crypto_dual_data,
24650Sstevel@tonic-gate 		    &io->ipsec_out_crypto_mac, &call_req);
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	switch (kef_rc) {
24690Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
24703448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_sync);
24714987Sdanmcd 		esp_set_usetime(assoc, B_FALSE);
24724987Sdanmcd 		if (is_natt)
24734987Sdanmcd 			esp_prepare_udp(ns, ipsec_mp->b_cont,
24744987Sdanmcd 			    (ipha_t *)ipsec_mp->b_cont->b_rptr);
24750Sstevel@tonic-gate 		return (IPSEC_STATUS_SUCCESS);
24760Sstevel@tonic-gate 	case CRYPTO_QUEUED:
24770Sstevel@tonic-gate 		/* esp_kcf_callback() will be invoked on completion */
24783448Sdh155122 		ESP_BUMP_STAT(espstack, crypto_async);
24790Sstevel@tonic-gate 		return (IPSEC_STATUS_PENDING);
24800Sstevel@tonic-gate 	}
24810Sstevel@tonic-gate 
24823448Sdh155122 	esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc, espstack);
24830Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
24840Sstevel@tonic-gate }
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate /*
24870Sstevel@tonic-gate  * Handle outbound IPsec processing for IPv4 and IPv6
24880Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
24890Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
24900Sstevel@tonic-gate  */
24910Sstevel@tonic-gate static ipsec_status_t
24920Sstevel@tonic-gate esp_outbound(mblk_t *mp)
24930Sstevel@tonic-gate {
24940Sstevel@tonic-gate 	mblk_t *ipsec_out_mp, *data_mp, *espmp, *tailmp;
24950Sstevel@tonic-gate 	ipsec_out_t *io;
24960Sstevel@tonic-gate 	ipha_t *ipha;
24970Sstevel@tonic-gate 	ip6_t *ip6h;
24980Sstevel@tonic-gate 	esph_t *esph;
24990Sstevel@tonic-gate 	uint_t af;
25000Sstevel@tonic-gate 	uint8_t *nhp;
25010Sstevel@tonic-gate 	uintptr_t divpoint, datalen, adj, padlen, i, alloclen;
25020Sstevel@tonic-gate 	uintptr_t esplen = sizeof (esph_t);
25030Sstevel@tonic-gate 	uint8_t protocol;
25040Sstevel@tonic-gate 	ipsa_t *assoc;
25053192Sdanmcd 	uint_t iv_len, mac_len = 0;
25060Sstevel@tonic-gate 	uchar_t *icv_buf;
25070Sstevel@tonic-gate 	udpha_t *udpha;
25080Sstevel@tonic-gate 	boolean_t is_natt = B_FALSE;
25093448Sdh155122 	netstack_t	*ns;
25103448Sdh155122 	ipsecesp_stack_t *espstack;
25113448Sdh155122 	ipsec_stack_t	*ipss;
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 	ipsec_out_mp = mp;
25140Sstevel@tonic-gate 	data_mp = ipsec_out_mp->b_cont;
25150Sstevel@tonic-gate 
25163448Sdh155122 	io = (ipsec_out_t *)ipsec_out_mp->b_rptr;
25173448Sdh155122 	ns = io->ipsec_out_ns;
25183448Sdh155122 	espstack = ns->netstack_ipsecesp;
25193448Sdh155122 	ipss = ns->netstack_ipsec;
25203448Sdh155122 
25213448Sdh155122 	ESP_BUMP_STAT(espstack, out_requests);
25223448Sdh155122 
25230Sstevel@tonic-gate 	/*
25240Sstevel@tonic-gate 	 * <sigh> We have to copy the message here, because TCP (for example)
25250Sstevel@tonic-gate 	 * keeps a dupb() of the message lying around for retransmission.
25260Sstevel@tonic-gate 	 * Since ESP changes the whole of the datagram, we have to create our
25270Sstevel@tonic-gate 	 * own copy lest we clobber TCP's data.  Since we have to copy anyway,
25280Sstevel@tonic-gate 	 * we might as well make use of msgpullup() and get the mblk into one
25290Sstevel@tonic-gate 	 * contiguous piece!
25300Sstevel@tonic-gate 	 */
25310Sstevel@tonic-gate 	ipsec_out_mp->b_cont = msgpullup(data_mp, -1);
25320Sstevel@tonic-gate 	if (ipsec_out_mp->b_cont == NULL) {
25330Sstevel@tonic-gate 		esp0dbg(("esp_outbound: msgpullup() failed, "
25340Sstevel@tonic-gate 		    "dropping packet.\n"));
25350Sstevel@tonic-gate 		ipsec_out_mp->b_cont = data_mp;
25360Sstevel@tonic-gate 		/*
25370Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
25380Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
25390Sstevel@tonic-gate 		 */
25400Sstevel@tonic-gate 		ip_drop_packet(ipsec_out_mp, B_FALSE, NULL, NULL,
25413448Sdh155122 		    DROPPER(ipss, ipds_esp_nomem),
25423448Sdh155122 		    &espstack->esp_dropper);
25430Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
25440Sstevel@tonic-gate 	} else {
25450Sstevel@tonic-gate 		freemsg(data_mp);
25460Sstevel@tonic-gate 		data_mp = ipsec_out_mp->b_cont;
25470Sstevel@tonic-gate 	}
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	/*
25500Sstevel@tonic-gate 	 * Reality check....
25510Sstevel@tonic-gate 	 */
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	ipha = (ipha_t *)data_mp->b_rptr;  /* So we can call esp_acquire(). */
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 	if (io->ipsec_out_v4) {
25560Sstevel@tonic-gate 		af = AF_INET;
25570Sstevel@tonic-gate 		divpoint = IPH_HDR_LENGTH(ipha);
25580Sstevel@tonic-gate 		datalen = ntohs(ipha->ipha_length) - divpoint;
25590Sstevel@tonic-gate 		nhp = (uint8_t *)&ipha->ipha_protocol;
25600Sstevel@tonic-gate 	} else {
25610Sstevel@tonic-gate 		ip6_pkt_t ipp;
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate 		af = AF_INET6;
25640Sstevel@tonic-gate 		ip6h = (ip6_t *)ipha;
25650Sstevel@tonic-gate 		bzero(&ipp, sizeof (ipp));
25660Sstevel@tonic-gate 		divpoint = ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL);
25670Sstevel@tonic-gate 		if (ipp.ipp_dstopts != NULL &&
25680Sstevel@tonic-gate 		    ipp.ipp_dstopts->ip6d_nxt != IPPROTO_ROUTING) {
25690Sstevel@tonic-gate 			/*
25700Sstevel@tonic-gate 			 * Destination options are tricky.  If we get in here,
25710Sstevel@tonic-gate 			 * then we have a terminal header following the
25720Sstevel@tonic-gate 			 * destination options.  We need to adjust backwards
25730Sstevel@tonic-gate 			 * so we insert ESP BEFORE the destination options
25740Sstevel@tonic-gate 			 * bag.  (So that the dstopts get encrypted!)
25750Sstevel@tonic-gate 			 *
25760Sstevel@tonic-gate 			 * Since this is for outbound packets only, we know
25770Sstevel@tonic-gate 			 * that non-terminal destination options only precede
25780Sstevel@tonic-gate 			 * routing headers.
25790Sstevel@tonic-gate 			 */
25800Sstevel@tonic-gate 			divpoint -= ipp.ipp_dstoptslen;
25810Sstevel@tonic-gate 		}
25820Sstevel@tonic-gate 		datalen = ntohs(ip6h->ip6_plen) + sizeof (ip6_t) - divpoint;
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 		if (ipp.ipp_rthdr != NULL) {
25850Sstevel@tonic-gate 			nhp = &ipp.ipp_rthdr->ip6r_nxt;
25860Sstevel@tonic-gate 		} else if (ipp.ipp_hopopts != NULL) {
25870Sstevel@tonic-gate 			nhp = &ipp.ipp_hopopts->ip6h_nxt;
25880Sstevel@tonic-gate 		} else {
25890Sstevel@tonic-gate 			ASSERT(divpoint == sizeof (ip6_t));
25900Sstevel@tonic-gate 			/* It's probably IP + ESP. */
25910Sstevel@tonic-gate 			nhp = &ip6h->ip6_nxt;
25920Sstevel@tonic-gate 		}
25930Sstevel@tonic-gate 	}
25940Sstevel@tonic-gate 	assoc = io->ipsec_out_esp_sa;
25950Sstevel@tonic-gate 	ASSERT(assoc != NULL);
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != SADB_AALG_NONE)
25980Sstevel@tonic-gate 		mac_len = assoc->ipsa_mac_len;
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 	if (assoc->ipsa_flags & IPSA_F_NATT) {
26010Sstevel@tonic-gate 		/* wedge in fake UDP */
26020Sstevel@tonic-gate 		is_natt = B_TRUE;
26030Sstevel@tonic-gate 		esplen += UDPH_SIZE;
26040Sstevel@tonic-gate 	}
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	/*
26070Sstevel@tonic-gate 	 * Set up ESP header and encryption padding for ENCR PI request.
26080Sstevel@tonic-gate 	 */
26090Sstevel@tonic-gate 
26103192Sdanmcd 	/* Determine the padding length.  Pad to 4-bytes for no-encryption. */
26110Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg != SADB_EALG_NULL) {
26123192Sdanmcd 		iv_len = assoc->ipsa_iv_len;
26133192Sdanmcd 
26143192Sdanmcd 		/*
26153192Sdanmcd 		 * Include the two additional bytes (hence the - 2) for the
26163192Sdanmcd 		 * padding length and the next header.  Take this into account
26173192Sdanmcd 		 * when calculating the actual length of the padding.
26183192Sdanmcd 		 */
26193192Sdanmcd 		ASSERT(ISP2(iv_len));
26203192Sdanmcd 		padlen = ((unsigned)(iv_len - datalen - 2)) & (iv_len - 1);
26210Sstevel@tonic-gate 	} else {
26223192Sdanmcd 		iv_len = 0;
26233192Sdanmcd 		padlen = ((unsigned)(sizeof (uint32_t) - datalen - 2)) &
26243192Sdanmcd 		    (sizeof (uint32_t) - 1);
26250Sstevel@tonic-gate 	}
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	/* Allocate ESP header and IV. */
26280Sstevel@tonic-gate 	esplen += iv_len;
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate 	/*
26310Sstevel@tonic-gate 	 * Update association byte-count lifetimes.  Don't forget to take
26320Sstevel@tonic-gate 	 * into account the padding length and next-header (hence the + 2).
2633134Sdanmcd 	 *
26340Sstevel@tonic-gate 	 * Use the amount of data fed into the "encryption algorithm".  This
26350Sstevel@tonic-gate 	 * is the IV, the data length, the padding length, and the final two
26360Sstevel@tonic-gate 	 * bytes (padlen, and next-header).
26370Sstevel@tonic-gate 	 *
26380Sstevel@tonic-gate 	 */
26390Sstevel@tonic-gate 
2640134Sdanmcd 	if (!esp_age_bytes(assoc, datalen + padlen + iv_len + 2, B_FALSE)) {
26410Sstevel@tonic-gate 		/*
26420Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
26430Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
26440Sstevel@tonic-gate 		 */
26450Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
26463448Sdh155122 		    DROPPER(ipss, ipds_esp_bytes_expire),
26473448Sdh155122 		    &espstack->esp_dropper);
26480Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
26490Sstevel@tonic-gate 	}
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	espmp = allocb(esplen, BPRI_HI);
26520Sstevel@tonic-gate 	if (espmp == NULL) {
26533448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
26543448Sdh155122 		esp1dbg(espstack, ("esp_outbound: can't allocate espmp.\n"));
26550Sstevel@tonic-gate 		/*
26560Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
26570Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
26580Sstevel@tonic-gate 		 */
26593448Sdh155122 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
26603448Sdh155122 		    DROPPER(ipss, ipds_esp_nomem),
26613448Sdh155122 		    &espstack->esp_dropper);
26620Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
26630Sstevel@tonic-gate 	}
26640Sstevel@tonic-gate 	espmp->b_wptr += esplen;
26650Sstevel@tonic-gate 	esph = (esph_t *)espmp->b_rptr;
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	if (is_natt) {
26683448Sdh155122 		esp3dbg(espstack, ("esp_outbound: NATT"));
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 		udpha = (udpha_t *)espmp->b_rptr;
26714987Sdanmcd 		udpha->uha_src_port = (assoc->ipsa_local_nat_port != 0) ?
26724987Sdanmcd 		    assoc->ipsa_local_nat_port : htons(IPPORT_IKE_NATT);
26734987Sdanmcd 		udpha->uha_dst_port = (assoc->ipsa_remote_nat_port != 0) ?
26744987Sdanmcd 		    assoc->ipsa_remote_nat_port : htons(IPPORT_IKE_NATT);
26750Sstevel@tonic-gate 		/*
26764987Sdanmcd 		 * Set the checksum to 0, so that the esp_prepare_udp() call
26770Sstevel@tonic-gate 		 * can do the right thing.
26780Sstevel@tonic-gate 		 */
26790Sstevel@tonic-gate 		udpha->uha_checksum = 0;
26800Sstevel@tonic-gate 		esph = (esph_t *)(udpha + 1);
26810Sstevel@tonic-gate 	}
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate 	esph->esph_spi = assoc->ipsa_spi;
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	esph->esph_replay = htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
26860Sstevel@tonic-gate 	if (esph->esph_replay == 0 && assoc->ipsa_replay_wsize != 0) {
26870Sstevel@tonic-gate 		/*
26880Sstevel@tonic-gate 		 * XXX We have replay counter wrapping.
26890Sstevel@tonic-gate 		 * We probably want to nuke this SA (and its peer).
26900Sstevel@tonic-gate 		 */
26910Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
26920Sstevel@tonic-gate 		    SL_ERROR | SL_CONSOLE | SL_WARN,
26930Sstevel@tonic-gate 		    "Outbound ESP SA (0x%x, %s) has wrapped sequence.\n",
26943448Sdh155122 		    esph->esph_spi, assoc->ipsa_dstaddr, af,
26953448Sdh155122 		    espstack->ipsecesp_netstack);
26963448Sdh155122 
26973448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
26980Sstevel@tonic-gate 		sadb_replay_delete(assoc);
26990Sstevel@tonic-gate 		/*
27000Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
27010Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
27020Sstevel@tonic-gate 		 */
27033448Sdh155122 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
27043448Sdh155122 		    DROPPER(ipss, ipds_esp_replay),
27053448Sdh155122 		    &espstack->esp_dropper);
27060Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
27070Sstevel@tonic-gate 	}
27080Sstevel@tonic-gate 
27090Sstevel@tonic-gate 	/*
27100Sstevel@tonic-gate 	 * Set the IV to a random quantity.  We do not require the
27110Sstevel@tonic-gate 	 * highest quality random bits, but for best security with CBC
27120Sstevel@tonic-gate 	 * mode ciphers, the value must be unlikely to repeat and also
27130Sstevel@tonic-gate 	 * must not be known in advance to an adversary capable of
27140Sstevel@tonic-gate 	 * influencing the plaintext.
27150Sstevel@tonic-gate 	 */
27160Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)(esph + 1), iv_len);
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	/* Fix the IP header. */
27190Sstevel@tonic-gate 	alloclen = padlen + 2 + mac_len;
27200Sstevel@tonic-gate 	adj = alloclen + (espmp->b_wptr - espmp->b_rptr);
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate 	protocol = *nhp;
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	if (io->ipsec_out_v4) {
27250Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) + adj);
27260Sstevel@tonic-gate 		if (is_natt) {
27270Sstevel@tonic-gate 			*nhp = IPPROTO_UDP;
27280Sstevel@tonic-gate 			udpha->uha_length = htons(ntohs(ipha->ipha_length) -
27290Sstevel@tonic-gate 			    IPH_HDR_LENGTH(ipha));
27300Sstevel@tonic-gate 		} else {
27310Sstevel@tonic-gate 			*nhp = IPPROTO_ESP;
27320Sstevel@tonic-gate 		}
27330Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = 0;
27340Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
27350Sstevel@tonic-gate 	} else {
27360Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) + adj);
27370Sstevel@tonic-gate 		*nhp = IPPROTO_ESP;
27380Sstevel@tonic-gate 	}
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	/* I've got the two ESP mblks, now insert them. */
27410Sstevel@tonic-gate 
27423448Sdh155122 	esp2dbg(espstack, ("data_mp before outbound ESP adjustment:\n"));
27433448Sdh155122 	esp2dbg(espstack, (dump_msg(data_mp)));
27443448Sdh155122 
27453448Sdh155122 	if (!esp_insert_esp(data_mp, espmp, divpoint, espstack)) {
27463448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
27470Sstevel@tonic-gate 		/* NOTE:  esp_insert_esp() only fails if there's no memory. */
27480Sstevel@tonic-gate 		/*
27490Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
27500Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
27510Sstevel@tonic-gate 		 */
27523448Sdh155122 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
27533448Sdh155122 		    DROPPER(ipss, ipds_esp_nomem),
27543448Sdh155122 		    &espstack->esp_dropper);
27550Sstevel@tonic-gate 		freeb(espmp);
27560Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
27570Sstevel@tonic-gate 	}
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 	/* Append padding (and leave room for ICV). */
27600Sstevel@tonic-gate 	for (tailmp = data_mp; tailmp->b_cont != NULL; tailmp = tailmp->b_cont)
27610Sstevel@tonic-gate 		;
27620Sstevel@tonic-gate 	if (tailmp->b_wptr + alloclen > tailmp->b_datap->db_lim) {
27630Sstevel@tonic-gate 		tailmp->b_cont = allocb(alloclen, BPRI_HI);
27640Sstevel@tonic-gate 		if (tailmp->b_cont == NULL) {
27653448Sdh155122 			ESP_BUMP_STAT(espstack, out_discards);
27660Sstevel@tonic-gate 			esp0dbg(("esp_outbound:  Can't allocate tailmp.\n"));
27670Sstevel@tonic-gate 			/*
27680Sstevel@tonic-gate 			 * TODO:  Find the outbound IRE for this packet and
27690Sstevel@tonic-gate 			 * pass it to ip_drop_packet().
27700Sstevel@tonic-gate 			 */
27710Sstevel@tonic-gate 			ip_drop_packet(mp, B_FALSE, NULL, NULL,
27723448Sdh155122 			    DROPPER(ipss, ipds_esp_nomem),
27733448Sdh155122 			    &espstack->esp_dropper);
27740Sstevel@tonic-gate 			return (IPSEC_STATUS_FAILED);
27750Sstevel@tonic-gate 		}
27760Sstevel@tonic-gate 		tailmp = tailmp->b_cont;
27770Sstevel@tonic-gate 	}
27780Sstevel@tonic-gate 
27790Sstevel@tonic-gate 	/*
27800Sstevel@tonic-gate 	 * If there's padding, N bytes of padding must be of the form 0x1,
27810Sstevel@tonic-gate 	 * 0x2, 0x3... 0xN.
27820Sstevel@tonic-gate 	 */
27830Sstevel@tonic-gate 	for (i = 0; i < padlen; ) {
27840Sstevel@tonic-gate 		i++;
27850Sstevel@tonic-gate 		*tailmp->b_wptr++ = i;
27860Sstevel@tonic-gate 	}
27870Sstevel@tonic-gate 	*tailmp->b_wptr++ = i;
27880Sstevel@tonic-gate 	*tailmp->b_wptr++ = protocol;
27890Sstevel@tonic-gate 
27903448Sdh155122 	esp2dbg(espstack, ("data_Mp before encryption:\n"));
27913448Sdh155122 	esp2dbg(espstack, (dump_msg(data_mp)));
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 	/*
27940Sstevel@tonic-gate 	 * The packet is eligible for hardware acceleration if the
27950Sstevel@tonic-gate 	 * following conditions are satisfied:
27960Sstevel@tonic-gate 	 *
27970Sstevel@tonic-gate 	 * 1. the packet will not be fragmented
27980Sstevel@tonic-gate 	 * 2. the provider supports the algorithms specified by SA
27990Sstevel@tonic-gate 	 * 3. there is no pending control message being exchanged
28000Sstevel@tonic-gate 	 * 4. snoop is not attached
28010Sstevel@tonic-gate 	 * 5. the destination address is not a multicast address
28020Sstevel@tonic-gate 	 *
28030Sstevel@tonic-gate 	 * All five of these conditions are checked by IP prior to
28040Sstevel@tonic-gate 	 * sending the packet to ESP.
28050Sstevel@tonic-gate 	 *
28060Sstevel@tonic-gate 	 * But We, and We Alone, can, nay MUST check if the packet
28070Sstevel@tonic-gate 	 * is over NATT, and then disqualify it from hardware
28080Sstevel@tonic-gate 	 * acceleration.
28090Sstevel@tonic-gate 	 */
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 	if (io->ipsec_out_is_capab_ill && !(assoc->ipsa_flags & IPSA_F_NATT)) {
28120Sstevel@tonic-gate 		return (esp_outbound_accelerated(ipsec_out_mp, mac_len));
28130Sstevel@tonic-gate 	}
28143448Sdh155122 	ESP_BUMP_STAT(espstack, noaccel);
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	/*
28170Sstevel@tonic-gate 	 * Okay.  I've set up the pre-encryption ESP.  Let's do it!
28180Sstevel@tonic-gate 	 */
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 	if (mac_len > 0) {
28210Sstevel@tonic-gate 		ASSERT(tailmp->b_wptr + mac_len <= tailmp->b_datap->db_lim);
28220Sstevel@tonic-gate 		icv_buf = tailmp->b_wptr;
28230Sstevel@tonic-gate 		tailmp->b_wptr += mac_len;
28240Sstevel@tonic-gate 	} else {
28250Sstevel@tonic-gate 		icv_buf = NULL;
28260Sstevel@tonic-gate 	}
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	return (esp_submit_req_outbound(ipsec_out_mp, assoc, icv_buf,
28290Sstevel@tonic-gate 	    datalen + padlen + 2));
28300Sstevel@tonic-gate }
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate /*
28330Sstevel@tonic-gate  * IP calls this to validate the ICMP errors that
28340Sstevel@tonic-gate  * we got from the network.
28350Sstevel@tonic-gate  */
28360Sstevel@tonic-gate ipsec_status_t
28370Sstevel@tonic-gate ipsecesp_icmp_error(mblk_t *ipsec_mp)
28380Sstevel@tonic-gate {
28393448Sdh155122 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
28403448Sdh155122 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
28413448Sdh155122 	netstack_t	*ns;
28423448Sdh155122 	ipsecesp_stack_t *espstack;
28433448Sdh155122 	ipsec_stack_t	*ipss;
28443448Sdh155122 
28453448Sdh155122 	if (is_inbound) {
28463448Sdh155122 		ns = ii->ipsec_in_ns;
28473448Sdh155122 	} else {
28483448Sdh155122 		ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
28493448Sdh155122 
28503448Sdh155122 		ns = io->ipsec_out_ns;
28513448Sdh155122 	}
28523448Sdh155122 	espstack = ns->netstack_ipsecesp;
28533448Sdh155122 	ipss = ns->netstack_ipsec;
28543448Sdh155122 
28550Sstevel@tonic-gate 	/*
28560Sstevel@tonic-gate 	 * Unless we get an entire packet back, this function is useless.
28570Sstevel@tonic-gate 	 * Why?
28580Sstevel@tonic-gate 	 *
28590Sstevel@tonic-gate 	 * 1.)	Partial packets are useless, because the "next header"
28600Sstevel@tonic-gate 	 *	is at the end of the decrypted ESP packet.  Without the
28610Sstevel@tonic-gate 	 *	whole packet, this is useless.
28620Sstevel@tonic-gate 	 *
28630Sstevel@tonic-gate 	 * 2.)	If we every use a stateful cipher, such as a stream or a
28640Sstevel@tonic-gate 	 *	one-time pad, we can't do anything.
28650Sstevel@tonic-gate 	 *
28660Sstevel@tonic-gate 	 * Since the chances of us getting an entire packet back are very
28670Sstevel@tonic-gate 	 * very small, we discard here.
28680Sstevel@tonic-gate 	 */
28693448Sdh155122 	IP_ESP_BUMP_STAT(ipss, in_discards);
28703448Sdh155122 	ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
28713448Sdh155122 	    DROPPER(ipss, ipds_esp_icmp),
28723448Sdh155122 	    &espstack->esp_dropper);
28730Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
28740Sstevel@tonic-gate }
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate /*
28770Sstevel@tonic-gate  * ESP module read put routine.
28780Sstevel@tonic-gate  */
28790Sstevel@tonic-gate /* ARGSUSED */
28800Sstevel@tonic-gate static void
28810Sstevel@tonic-gate ipsecesp_rput(queue_t *q, mblk_t *mp)
28820Sstevel@tonic-gate {
28833448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
28843448Sdh155122 
28853055Sdanmcd 	ASSERT(mp->b_datap->db_type != M_CTL);	/* No more IRE_DB_REQ. */
28863448Sdh155122 
28870Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
28880Sstevel@tonic-gate 	case M_PROTO:
28890Sstevel@tonic-gate 	case M_PCPROTO:
28900Sstevel@tonic-gate 		/* TPI message of some sort. */
28910Sstevel@tonic-gate 		switch (*((t_scalar_t *)mp->b_rptr)) {
28920Sstevel@tonic-gate 		case T_BIND_ACK:
28933448Sdh155122 			esp3dbg(espstack,
28943448Sdh155122 			    ("Thank you IP from ESP for T_BIND_ACK\n"));
28950Sstevel@tonic-gate 			break;
28960Sstevel@tonic-gate 		case T_ERROR_ACK:
28970Sstevel@tonic-gate 			cmn_err(CE_WARN,
28980Sstevel@tonic-gate 			    "ipsecesp:  ESP received T_ERROR_ACK from IP.");
28990Sstevel@tonic-gate 			/*
29000Sstevel@tonic-gate 			 * Make esp_sadb.s_ip_q NULL, and in the
29010Sstevel@tonic-gate 			 * future, perhaps try again.
29020Sstevel@tonic-gate 			 */
29033448Sdh155122 			espstack->esp_sadb.s_ip_q = NULL;
29040Sstevel@tonic-gate 			break;
29050Sstevel@tonic-gate 		case T_OK_ACK:
29060Sstevel@tonic-gate 			/* Probably from a (rarely sent) T_UNBIND_REQ. */
29070Sstevel@tonic-gate 			break;
29080Sstevel@tonic-gate 		default:
29090Sstevel@tonic-gate 			esp0dbg(("Unknown M_{,PC}PROTO message.\n"));
29100Sstevel@tonic-gate 		}
29110Sstevel@tonic-gate 		freemsg(mp);
29120Sstevel@tonic-gate 		break;
29130Sstevel@tonic-gate 	default:
29140Sstevel@tonic-gate 		/* For now, passthru message. */
29153448Sdh155122 		esp2dbg(espstack, ("ESP got unknown mblk type %d.\n",
29160Sstevel@tonic-gate 		    mp->b_datap->db_type));
29170Sstevel@tonic-gate 		putnext(q, mp);
29180Sstevel@tonic-gate 	}
29190Sstevel@tonic-gate }
29200Sstevel@tonic-gate 
29210Sstevel@tonic-gate /*
29220Sstevel@tonic-gate  * Construct an SADB_REGISTER message with the current algorithms.
29230Sstevel@tonic-gate  */
29240Sstevel@tonic-gate static boolean_t
29253448Sdh155122 esp_register_out(uint32_t sequence, uint32_t pid, uint_t serial,
29263448Sdh155122     ipsecesp_stack_t *espstack)
29270Sstevel@tonic-gate {
29280Sstevel@tonic-gate 	mblk_t *pfkey_msg_mp, *keysock_out_mp;
29290Sstevel@tonic-gate 	sadb_msg_t *samsg;
29300Sstevel@tonic-gate 	sadb_supported_t *sasupp_auth = NULL;
29310Sstevel@tonic-gate 	sadb_supported_t *sasupp_encr = NULL;
29320Sstevel@tonic-gate 	sadb_alg_t *saalg;
29330Sstevel@tonic-gate 	uint_t allocsize = sizeof (*samsg);
29340Sstevel@tonic-gate 	uint_t i, numalgs_snap;
29350Sstevel@tonic-gate 	int current_aalgs;
29360Sstevel@tonic-gate 	ipsec_alginfo_t **authalgs;
29370Sstevel@tonic-gate 	uint_t num_aalgs;
29380Sstevel@tonic-gate 	int current_ealgs;
29390Sstevel@tonic-gate 	ipsec_alginfo_t **encralgs;
29400Sstevel@tonic-gate 	uint_t num_ealgs;
29413448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	/* Allocate the KEYSOCK_OUT. */
29440Sstevel@tonic-gate 	keysock_out_mp = sadb_keysock_out(serial);
29450Sstevel@tonic-gate 	if (keysock_out_mp == NULL) {
29460Sstevel@tonic-gate 		esp0dbg(("esp_register_out: couldn't allocate mblk.\n"));
29470Sstevel@tonic-gate 		return (B_FALSE);
29480Sstevel@tonic-gate 	}
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 	/*
29510Sstevel@tonic-gate 	 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
29520Sstevel@tonic-gate 	 */
29530Sstevel@tonic-gate 
29543448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate 	/*
29570Sstevel@tonic-gate 	 * Fill SADB_REGISTER message's algorithm descriptors.  Hold
29580Sstevel@tonic-gate 	 * down the lock while filling it.
29590Sstevel@tonic-gate 	 *
29600Sstevel@tonic-gate 	 * Return only valid algorithms, so the number of algorithms
29610Sstevel@tonic-gate 	 * to send up may be less than the number of algorithm entries
29620Sstevel@tonic-gate 	 * in the table.
29630Sstevel@tonic-gate 	 */
29643448Sdh155122 	authalgs = ipss->ipsec_alglists[IPSEC_ALG_AUTH];
29650Sstevel@tonic-gate 	for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
29660Sstevel@tonic-gate 		if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
29670Sstevel@tonic-gate 			num_aalgs++;
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 	if (num_aalgs != 0) {
29700Sstevel@tonic-gate 		allocsize += (num_aalgs * sizeof (*saalg));
29710Sstevel@tonic-gate 		allocsize += sizeof (*sasupp_auth);
29720Sstevel@tonic-gate 	}
29733448Sdh155122 	encralgs = ipss->ipsec_alglists[IPSEC_ALG_ENCR];
29740Sstevel@tonic-gate 	for (num_ealgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
29750Sstevel@tonic-gate 		if (encralgs[i] != NULL && ALG_VALID(encralgs[i]))
29760Sstevel@tonic-gate 			num_ealgs++;
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	if (num_ealgs != 0) {
29790Sstevel@tonic-gate 		allocsize += (num_ealgs * sizeof (*saalg));
29800Sstevel@tonic-gate 		allocsize += sizeof (*sasupp_encr);
29810Sstevel@tonic-gate 	}
29820Sstevel@tonic-gate 	keysock_out_mp->b_cont = allocb(allocsize, BPRI_HI);
29830Sstevel@tonic-gate 	if (keysock_out_mp->b_cont == NULL) {
29843448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
29850Sstevel@tonic-gate 		freemsg(keysock_out_mp);
29860Sstevel@tonic-gate 		return (B_FALSE);
29870Sstevel@tonic-gate 	}
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate 	pfkey_msg_mp = keysock_out_mp->b_cont;
29900Sstevel@tonic-gate 	pfkey_msg_mp->b_wptr += allocsize;
29910Sstevel@tonic-gate 	if (num_aalgs != 0) {
29920Sstevel@tonic-gate 		sasupp_auth = (sadb_supported_t *)
29930Sstevel@tonic-gate 		    (pfkey_msg_mp->b_rptr + sizeof (*samsg));
29940Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(sasupp_auth + 1);
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate 		ASSERT(((ulong_t)saalg & 0x7) == 0);
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 		numalgs_snap = 0;
29990Sstevel@tonic-gate 		for (i = 0;
30003448Sdh155122 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs));
30013448Sdh155122 		    i++) {
30020Sstevel@tonic-gate 			if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
30030Sstevel@tonic-gate 				continue;
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 			saalg->sadb_alg_id = authalgs[i]->alg_id;
30060Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = 0;
30070Sstevel@tonic-gate 			saalg->sadb_alg_minbits	= authalgs[i]->alg_ef_minbits;
30080Sstevel@tonic-gate 			saalg->sadb_alg_maxbits	= authalgs[i]->alg_ef_maxbits;
30090Sstevel@tonic-gate 			saalg->sadb_x_alg_defincr = authalgs[i]->alg_ef_default;
30100Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
30110Sstevel@tonic-gate 			    authalgs[i]->alg_increment;
30120Sstevel@tonic-gate 			numalgs_snap++;
30130Sstevel@tonic-gate 			saalg++;
30140Sstevel@tonic-gate 		}
30150Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_aalgs);
30160Sstevel@tonic-gate #ifdef DEBUG
30170Sstevel@tonic-gate 		/*
30180Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
30190Sstevel@tonic-gate 		 * algorithms.
30200Sstevel@tonic-gate 		 */
30210Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++) {
30220Sstevel@tonic-gate 			if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) {
30230Sstevel@tonic-gate 				cmn_err(CE_PANIC, "esp_register_out()! "
30240Sstevel@tonic-gate 				    "Missed aalg #%d.\n", i);
30250Sstevel@tonic-gate 			}
30260Sstevel@tonic-gate 		}
30270Sstevel@tonic-gate #endif /* DEBUG */
30280Sstevel@tonic-gate 	} else {
30290Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(pfkey_msg_mp->b_rptr + sizeof (*samsg));
30300Sstevel@tonic-gate 	}
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate 	if (num_ealgs != 0) {
30330Sstevel@tonic-gate 		sasupp_encr = (sadb_supported_t *)saalg;
30340Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(sasupp_encr + 1);
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 		numalgs_snap = 0;
30370Sstevel@tonic-gate 		for (i = 0;
30380Sstevel@tonic-gate 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_ealgs)); i++) {
30390Sstevel@tonic-gate 			if (encralgs[i] == NULL || !ALG_VALID(encralgs[i]))
30400Sstevel@tonic-gate 				continue;
30410Sstevel@tonic-gate 			saalg->sadb_alg_id = encralgs[i]->alg_id;
30420Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = encralgs[i]->alg_datalen;
30430Sstevel@tonic-gate 			saalg->sadb_alg_minbits	= encralgs[i]->alg_ef_minbits;
30440Sstevel@tonic-gate 			saalg->sadb_alg_maxbits	= encralgs[i]->alg_ef_maxbits;
30450Sstevel@tonic-gate 			saalg->sadb_x_alg_defincr = encralgs[i]->alg_ef_default;
30460Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
30470Sstevel@tonic-gate 			    encralgs[i]->alg_increment;
30480Sstevel@tonic-gate 			numalgs_snap++;
30490Sstevel@tonic-gate 			saalg++;
30500Sstevel@tonic-gate 		}
30510Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_ealgs);
30520Sstevel@tonic-gate #ifdef DEBUG
30530Sstevel@tonic-gate 		/*
30540Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
30550Sstevel@tonic-gate 		 * algorithms.
30560Sstevel@tonic-gate 		 */
30570Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++) {
30580Sstevel@tonic-gate 			if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) {
30590Sstevel@tonic-gate 				cmn_err(CE_PANIC, "esp_register_out()! "
30600Sstevel@tonic-gate 				    "Missed ealg #%d.\n", i);
30610Sstevel@tonic-gate 			}
30620Sstevel@tonic-gate 		}
30630Sstevel@tonic-gate #endif /* DEBUG */
30640Sstevel@tonic-gate 	}
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	current_aalgs = num_aalgs;
30670Sstevel@tonic-gate 	current_ealgs = num_ealgs;
30680Sstevel@tonic-gate 
30693448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 	/* Now fill the rest of the SADB_REGISTER message. */
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate 	samsg = (sadb_msg_t *)pfkey_msg_mp->b_rptr;
30740Sstevel@tonic-gate 	samsg->sadb_msg_version = PF_KEY_V2;
30750Sstevel@tonic-gate 	samsg->sadb_msg_type = SADB_REGISTER;
30760Sstevel@tonic-gate 	samsg->sadb_msg_errno = 0;
30770Sstevel@tonic-gate 	samsg->sadb_msg_satype = SADB_SATYPE_ESP;
30780Sstevel@tonic-gate 	samsg->sadb_msg_len = SADB_8TO64(allocsize);
30790Sstevel@tonic-gate 	samsg->sadb_msg_reserved = 0;
30800Sstevel@tonic-gate 	/*
30810Sstevel@tonic-gate 	 * Assume caller has sufficient sequence/pid number info.  If it's one
30820Sstevel@tonic-gate 	 * from me over a new alg., I could give two hoots about sequence.
30830Sstevel@tonic-gate 	 */
30840Sstevel@tonic-gate 	samsg->sadb_msg_seq = sequence;
30850Sstevel@tonic-gate 	samsg->sadb_msg_pid = pid;
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	if (sasupp_auth != NULL) {
30884987Sdanmcd 		sasupp_auth->sadb_supported_len = SADB_8TO64(
30894987Sdanmcd 		    sizeof (*sasupp_auth) + sizeof (*saalg) * current_aalgs);
30900Sstevel@tonic-gate 		sasupp_auth->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
30910Sstevel@tonic-gate 		sasupp_auth->sadb_supported_reserved = 0;
30920Sstevel@tonic-gate 	}
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	if (sasupp_encr != NULL) {
30954987Sdanmcd 		sasupp_encr->sadb_supported_len = SADB_8TO64(
30964987Sdanmcd 		    sizeof (*sasupp_encr) + sizeof (*saalg) * current_ealgs);
30970Sstevel@tonic-gate 		sasupp_encr->sadb_supported_exttype =
30980Sstevel@tonic-gate 		    SADB_EXT_SUPPORTED_ENCRYPT;
30990Sstevel@tonic-gate 		sasupp_encr->sadb_supported_reserved = 0;
31000Sstevel@tonic-gate 	}
31010Sstevel@tonic-gate 
31023448Sdh155122 	if (espstack->esp_pfkey_q != NULL)
31033448Sdh155122 		putnext(espstack->esp_pfkey_q, keysock_out_mp);
31040Sstevel@tonic-gate 	else {
31050Sstevel@tonic-gate 		freemsg(keysock_out_mp);
31060Sstevel@tonic-gate 		return (B_FALSE);
31070Sstevel@tonic-gate 	}
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 	return (B_TRUE);
31100Sstevel@tonic-gate }
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate /*
31130Sstevel@tonic-gate  * Invoked when the algorithm table changes. Causes SADB_REGISTER
31140Sstevel@tonic-gate  * messages continaining the current list of algorithms to be
31150Sstevel@tonic-gate  * sent up to the ESP listeners.
31160Sstevel@tonic-gate  */
31170Sstevel@tonic-gate void
31183448Sdh155122 ipsecesp_algs_changed(netstack_t *ns)
31190Sstevel@tonic-gate {
31203448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
31213448Sdh155122 
31220Sstevel@tonic-gate 	/*
31230Sstevel@tonic-gate 	 * Time to send a PF_KEY SADB_REGISTER message to ESP listeners
31240Sstevel@tonic-gate 	 * everywhere.  (The function itself checks for NULL esp_pfkey_q.)
31250Sstevel@tonic-gate 	 */
31263448Sdh155122 	(void) esp_register_out(0, 0, 0, espstack);
31270Sstevel@tonic-gate }
31280Sstevel@tonic-gate 
31290Sstevel@tonic-gate /*
31300Sstevel@tonic-gate  * taskq_dispatch handler.
31310Sstevel@tonic-gate  */
31320Sstevel@tonic-gate static void
31330Sstevel@tonic-gate inbound_task(void *arg)
31340Sstevel@tonic-gate {
31350Sstevel@tonic-gate 	esph_t *esph;
31360Sstevel@tonic-gate 	mblk_t *mp = (mblk_t *)arg;
31370Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr;
31383448Sdh155122 	netstack_t		*ns = ii->ipsec_in_ns;
31393448Sdh155122 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
31400Sstevel@tonic-gate 	int ipsec_rc;
31410Sstevel@tonic-gate 
31423448Sdh155122 	esp2dbg(espstack, ("in ESP inbound_task"));
31433448Sdh155122 	ASSERT(espstack != NULL);
31443448Sdh155122 
31453448Sdh155122 	esph = ipsec_inbound_esp_sa(mp, ns);
31460Sstevel@tonic-gate 	if (esph == NULL)
31470Sstevel@tonic-gate 		return;
31480Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_esp_sa != NULL);
31490Sstevel@tonic-gate 	ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func(mp, esph);
31500Sstevel@tonic-gate 	if (ipsec_rc != IPSEC_STATUS_SUCCESS)
31510Sstevel@tonic-gate 		return;
31520Sstevel@tonic-gate 	ip_fanout_proto_again(mp, NULL, NULL, NULL);
31530Sstevel@tonic-gate }
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate /*
31560Sstevel@tonic-gate  * Now that weak-key passed, actually ADD the security association, and
31570Sstevel@tonic-gate  * send back a reply ADD message.
31580Sstevel@tonic-gate  */
31590Sstevel@tonic-gate static int
31603055Sdanmcd esp_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi,
31613448Sdh155122     int *diagnostic, ipsecesp_stack_t *espstack)
31620Sstevel@tonic-gate {
31636668Smarkfen 	isaf_t *primary = NULL, *secondary, *inbound, *outbound;
31640Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
31650Sstevel@tonic-gate 	sadb_address_t *dstext =
31660Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
31670Sstevel@tonic-gate 	struct sockaddr_in *dst;
31680Sstevel@tonic-gate 	struct sockaddr_in6 *dst6;
31690Sstevel@tonic-gate 	boolean_t is_ipv4, clone = B_FALSE, is_inbound = B_FALSE;
31700Sstevel@tonic-gate 	uint32_t *dstaddr;
31710Sstevel@tonic-gate 	ipsa_t *larval = NULL;
31720Sstevel@tonic-gate 	ipsacq_t *acqrec;
31730Sstevel@tonic-gate 	iacqf_t *acq_bucket;
31740Sstevel@tonic-gate 	mblk_t *acq_msgs = NULL;
31750Sstevel@tonic-gate 	int rc;
31760Sstevel@tonic-gate 	sadb_t *sp;
31770Sstevel@tonic-gate 	int outhash;
31780Sstevel@tonic-gate 	mblk_t *lpkt;
31793448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 	/*
31820Sstevel@tonic-gate 	 * Locate the appropriate table(s).
31830Sstevel@tonic-gate 	 */
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
31860Sstevel@tonic-gate 	dst6 = (struct sockaddr_in6 *)dst;
31870Sstevel@tonic-gate 	is_ipv4 = (dst->sin_family == AF_INET);
31880Sstevel@tonic-gate 	if (is_ipv4) {
31893448Sdh155122 		sp = &espstack->esp_sadb.s_v4;
31900Sstevel@tonic-gate 		dstaddr = (uint32_t *)(&dst->sin_addr);
3191564Ssommerfe 		outhash = OUTBOUND_HASH_V4(sp, *(ipaddr_t *)dstaddr);
31920Sstevel@tonic-gate 	} else {
31933448Sdh155122 		sp = &espstack->esp_sadb.s_v6;
31940Sstevel@tonic-gate 		dstaddr = (uint32_t *)(&dst6->sin6_addr);
3195564Ssommerfe 		outhash = OUTBOUND_HASH_V6(sp, *(in6_addr_t *)dstaddr);
31960Sstevel@tonic-gate 	}
3197691Ssommerfe 
3198564Ssommerfe 	inbound = INBOUND_BUCKET(sp, assoc->sadb_sa_spi);
3199691Ssommerfe 	outbound = &sp->sdb_of[outhash];
3200691Ssommerfe 
32016668Smarkfen 	/*
32026668Smarkfen 	 * Use the direction flags provided by the KMD to determine
32036668Smarkfen 	 * if the inbound or outbound table should be the primary
32046668Smarkfen 	 * for this SA. If these flags were absent then make this
32056668Smarkfen 	 * decision based on the addresses.
32066668Smarkfen 	 */
32076668Smarkfen 	if (assoc->sadb_sa_flags & IPSA_F_INBOUND) {
32080Sstevel@tonic-gate 		primary = inbound;
3209691Ssommerfe 		secondary = outbound;
32106668Smarkfen 		is_inbound = B_TRUE;
32116668Smarkfen 		if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND)
32126668Smarkfen 			clone = B_TRUE;
32136668Smarkfen 	} else {
32146668Smarkfen 		if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND) {
32156668Smarkfen 			primary = outbound;
32166668Smarkfen 			secondary = inbound;
32176668Smarkfen 		}
32186668Smarkfen 	}
32196668Smarkfen 
32206668Smarkfen 	if (primary == NULL) {
32216668Smarkfen 		/*
32226668Smarkfen 		 * The KMD did not set a direction flag, determine which
32236668Smarkfen 		 * table to insert the SA into based on addresses.
32246668Smarkfen 		 */
32256668Smarkfen 		switch (ksi->ks_in_dsttype) {
32266668Smarkfen 		case KS_IN_ADDR_MBCAST:
32276668Smarkfen 			clone = B_TRUE;	/* All mcast SAs can be bidirectional */
32286668Smarkfen 			assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
32296668Smarkfen 			/* FALLTHRU */
32300Sstevel@tonic-gate 		/*
32310Sstevel@tonic-gate 		 * If the source address is either one of mine, or unspecified
32320Sstevel@tonic-gate 		 * (which is best summed up by saying "not 'not mine'"),
32330Sstevel@tonic-gate 		 * then the association is potentially bi-directional,
32340Sstevel@tonic-gate 		 * in that it can be used for inbound traffic and outbound
32350Sstevel@tonic-gate 		 * traffic.  The best example of such an SA is a multicast
32360Sstevel@tonic-gate 		 * SA (which allows me to receive the outbound traffic).
32370Sstevel@tonic-gate 		 */
32386668Smarkfen 		case KS_IN_ADDR_ME:
32396668Smarkfen 			assoc->sadb_sa_flags |= IPSA_F_INBOUND;
32406668Smarkfen 			primary = inbound;
32416668Smarkfen 			secondary = outbound;
32426668Smarkfen 			if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
32436668Smarkfen 				clone = B_TRUE;
32446668Smarkfen 			is_inbound = B_TRUE;
32456668Smarkfen 			break;
32460Sstevel@tonic-gate 		/*
32470Sstevel@tonic-gate 		 * If the source address literally not mine (either
32480Sstevel@tonic-gate 		 * unspecified or not mine), then this SA may have an
32490Sstevel@tonic-gate 		 * address that WILL be mine after some configuration.
32500Sstevel@tonic-gate 		 * We pay the price for this by making it a bi-directional
32510Sstevel@tonic-gate 		 * SA.
32520Sstevel@tonic-gate 		 */
32536668Smarkfen 		case KS_IN_ADDR_NOTME:
32546668Smarkfen 			assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
32556668Smarkfen 			primary = outbound;
32566668Smarkfen 			secondary = inbound;
32576668Smarkfen 			if (ksi->ks_in_srctype != KS_IN_ADDR_ME) {
32586668Smarkfen 				assoc->sadb_sa_flags |= IPSA_F_INBOUND;
32596668Smarkfen 				clone = B_TRUE;
32606668Smarkfen 			}
32616668Smarkfen 			break;
32626668Smarkfen 		default:
32636668Smarkfen 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
32646668Smarkfen 			return (EINVAL);
32656668Smarkfen 		}
32660Sstevel@tonic-gate 	}
32670Sstevel@tonic-gate 
32680Sstevel@tonic-gate 	/*
32690Sstevel@tonic-gate 	 * Find a ACQUIRE list entry if possible.  If we've added an SA that
32700Sstevel@tonic-gate 	 * suits the needs of an ACQUIRE list entry, we can eliminate the
32710Sstevel@tonic-gate 	 * ACQUIRE list entry and transmit the enqueued packets.  Use the
32720Sstevel@tonic-gate 	 * high-bit of the sequence number to queue it.  Key off destination
32730Sstevel@tonic-gate 	 * addr, and change acqrec's state.
32740Sstevel@tonic-gate 	 */
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
32770Sstevel@tonic-gate 		acq_bucket = &sp->sdb_acq[outhash];
32780Sstevel@tonic-gate 		mutex_enter(&acq_bucket->iacqf_lock);
32790Sstevel@tonic-gate 		for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
32800Sstevel@tonic-gate 		    acqrec = acqrec->ipsacq_next) {
32810Sstevel@tonic-gate 			mutex_enter(&acqrec->ipsacq_lock);
32820Sstevel@tonic-gate 			/*
32830Sstevel@tonic-gate 			 * Q:  I only check sequence.  Should I check dst?
32840Sstevel@tonic-gate 			 * A: Yes, check dest because those are the packets
32850Sstevel@tonic-gate 			 *    that are queued up.
32860Sstevel@tonic-gate 			 */
32870Sstevel@tonic-gate 			if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
32880Sstevel@tonic-gate 			    IPSA_ARE_ADDR_EQUAL(dstaddr,
32894987Sdanmcd 			    acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
32900Sstevel@tonic-gate 				break;
32910Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
32920Sstevel@tonic-gate 		}
32930Sstevel@tonic-gate 		if (acqrec != NULL) {
32940Sstevel@tonic-gate 			/*
32950Sstevel@tonic-gate 			 * AHA!  I found an ACQUIRE record for this SA.
32960Sstevel@tonic-gate 			 * Grab the msg list, and free the acquire record.
32970Sstevel@tonic-gate 			 * I already am holding the lock for this record,
32980Sstevel@tonic-gate 			 * so all I have to do is free it.
32990Sstevel@tonic-gate 			 */
33000Sstevel@tonic-gate 			acq_msgs = acqrec->ipsacq_mp;
33010Sstevel@tonic-gate 			acqrec->ipsacq_mp = NULL;
33020Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
33033448Sdh155122 			sadb_destroy_acquire(acqrec,
33043448Sdh155122 			    espstack->ipsecesp_netstack);
33050Sstevel@tonic-gate 		}
33060Sstevel@tonic-gate 		mutex_exit(&acq_bucket->iacqf_lock);
33070Sstevel@tonic-gate 	}
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 	/*
33100Sstevel@tonic-gate 	 * Find PF_KEY message, and see if I'm an update.  If so, find entry
33110Sstevel@tonic-gate 	 * in larval list (if there).
33120Sstevel@tonic-gate 	 */
33130Sstevel@tonic-gate 
33140Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_UPDATE) {
33150Sstevel@tonic-gate 		mutex_enter(&inbound->isaf_lock);
33160Sstevel@tonic-gate 		larval = ipsec_getassocbyspi(inbound, assoc->sadb_sa_spi,
33170Sstevel@tonic-gate 		    ALL_ZEROES_PTR, dstaddr, dst->sin_family);
33180Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
33190Sstevel@tonic-gate 
33200Sstevel@tonic-gate 		if (larval == NULL) {
33216668Smarkfen 			*diagnostic = SADB_X_DIAGNOSTIC_SA_NOTFOUND;
33220Sstevel@tonic-gate 			esp0dbg(("Larval update, but larval disappeared.\n"));
33230Sstevel@tonic-gate 			return (ESRCH);
33240Sstevel@tonic-gate 		} /* Else sadb_common_add unlinks it for me! */
33250Sstevel@tonic-gate 	}
33260Sstevel@tonic-gate 
33270Sstevel@tonic-gate 	lpkt = NULL;
33280Sstevel@tonic-gate 	if (larval != NULL)
33290Sstevel@tonic-gate 		lpkt = sadb_clear_lpkt(larval);
33300Sstevel@tonic-gate 
33313448Sdh155122 	rc = sadb_common_add(espstack->esp_sadb.s_ip_q, espstack->esp_pfkey_q,
33323448Sdh155122 	    mp, samsg, ksi, primary, secondary, larval, clone, is_inbound,
33336668Smarkfen 	    diagnostic, espstack->ipsecesp_netstack, &espstack->esp_sadb);
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 	if (rc == 0 && lpkt != NULL) {
33360Sstevel@tonic-gate 		rc = !taskq_dispatch(esp_taskq, inbound_task,
33374987Sdanmcd 		    (void *) lpkt, TQ_NOSLEEP);
33380Sstevel@tonic-gate 	}
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate 	if (rc != 0) {
33410Sstevel@tonic-gate 		ip_drop_packet(lpkt, B_TRUE, NULL, NULL,
33423448Sdh155122 		    DROPPER(ipss, ipds_sadb_inlarval_timeout),
33433448Sdh155122 		    &espstack->esp_dropper);
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 	/*
33470Sstevel@tonic-gate 	 * How much more stack will I create with all of these
33480Sstevel@tonic-gate 	 * esp_outbound() calls?
33490Sstevel@tonic-gate 	 */
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate 	while (acq_msgs != NULL) {
33520Sstevel@tonic-gate 		mblk_t *mp = acq_msgs;
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 		acq_msgs = acq_msgs->b_next;
33550Sstevel@tonic-gate 		mp->b_next = NULL;
33560Sstevel@tonic-gate 		if (rc == 0) {
33570Sstevel@tonic-gate 			if (ipsec_outbound_sa(mp, IPPROTO_ESP)) {
33580Sstevel@tonic-gate 				((ipsec_out_t *)(mp->b_rptr))->
33590Sstevel@tonic-gate 				    ipsec_out_esp_done = B_TRUE;
33600Sstevel@tonic-gate 				if (esp_outbound(mp) == IPSEC_STATUS_SUCCESS) {
33613055Sdanmcd 					ipha_t *ipha;
33620Sstevel@tonic-gate 
33630Sstevel@tonic-gate 					/* do AH processing if needed */
33640Sstevel@tonic-gate 					if (!esp_do_outbound_ah(mp))
33650Sstevel@tonic-gate 						continue;
33660Sstevel@tonic-gate 
33673055Sdanmcd 					ipha = (ipha_t *)mp->b_cont->b_rptr;
33683055Sdanmcd 
33690Sstevel@tonic-gate 					/* finish IPsec processing */
33700Sstevel@tonic-gate 					if (is_ipv4) {
33710Sstevel@tonic-gate 						ip_wput_ipsec_out(NULL, mp,
33720Sstevel@tonic-gate 						    ipha, NULL, NULL);
33730Sstevel@tonic-gate 					} else {
33740Sstevel@tonic-gate 						ip6_t *ip6h = (ip6_t *)ipha;
33750Sstevel@tonic-gate 						ip_wput_ipsec_out_v6(NULL,
33760Sstevel@tonic-gate 						    mp, ip6h, NULL, NULL);
33770Sstevel@tonic-gate 					}
33780Sstevel@tonic-gate 				}
33790Sstevel@tonic-gate 				continue;
33800Sstevel@tonic-gate 			}
33810Sstevel@tonic-gate 		}
33823448Sdh155122 		ESP_BUMP_STAT(espstack, out_discards);
33830Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
33843448Sdh155122 		    DROPPER(ipss, ipds_sadb_acquire_timeout),
33853448Sdh155122 		    &espstack->esp_dropper);
33860Sstevel@tonic-gate 	}
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate 	return (rc);
33890Sstevel@tonic-gate }
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate /*
33920Sstevel@tonic-gate  * Add new ESP security association.  This may become a generic AH/ESP
33930Sstevel@tonic-gate  * routine eventually.
33940Sstevel@tonic-gate  */
33950Sstevel@tonic-gate static int
33963448Sdh155122 esp_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, netstack_t *ns)
33970Sstevel@tonic-gate {
33980Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
33990Sstevel@tonic-gate 	sadb_address_t *srcext =
34000Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
34010Sstevel@tonic-gate 	sadb_address_t *dstext =
34020Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
34033055Sdanmcd 	sadb_address_t *isrcext =
34043055Sdanmcd 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
34053055Sdanmcd 	sadb_address_t *idstext =
34063055Sdanmcd 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
34070Sstevel@tonic-gate 	sadb_address_t *nttext_loc =
34080Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC];
34090Sstevel@tonic-gate 	sadb_address_t *nttext_rem =
34100Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM];
34110Sstevel@tonic-gate 	sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
34120Sstevel@tonic-gate 	sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT];
34130Sstevel@tonic-gate 	struct sockaddr_in *src, *dst;
34140Sstevel@tonic-gate 	struct sockaddr_in *natt_loc, *natt_rem;
34150Sstevel@tonic-gate 	struct sockaddr_in6 *natt_loc6, *natt_rem6;
34160Sstevel@tonic-gate 	sadb_lifetime_t *soft =
34170Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
34180Sstevel@tonic-gate 	sadb_lifetime_t *hard =
34190Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
34203448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
34213448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
34220Sstevel@tonic-gate 
34230Sstevel@tonic-gate 	/* I need certain extensions present for an ADD message. */
34240Sstevel@tonic-gate 	if (srcext == NULL) {
34250Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
34260Sstevel@tonic-gate 		return (EINVAL);
34270Sstevel@tonic-gate 	}
34280Sstevel@tonic-gate 	if (dstext == NULL) {
34290Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
34300Sstevel@tonic-gate 		return (EINVAL);
34310Sstevel@tonic-gate 	}
34323055Sdanmcd 	if (isrcext == NULL && idstext != NULL) {
34333055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
34343055Sdanmcd 		return (EINVAL);
34353055Sdanmcd 	}
34363055Sdanmcd 	if (isrcext != NULL && idstext == NULL) {
34373055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
34383055Sdanmcd 		return (EINVAL);
34393055Sdanmcd 	}
34400Sstevel@tonic-gate 	if (assoc == NULL) {
34410Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
34420Sstevel@tonic-gate 		return (EINVAL);
34430Sstevel@tonic-gate 	}
34440Sstevel@tonic-gate 	if (ekey == NULL && assoc->sadb_sa_encrypt != SADB_EALG_NULL) {
34450Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_EKEY;
34460Sstevel@tonic-gate 		return (EINVAL);
34470Sstevel@tonic-gate 	}
34480Sstevel@tonic-gate 
34490Sstevel@tonic-gate 	src = (struct sockaddr_in *)(srcext + 1);
34500Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
34510Sstevel@tonic-gate 	natt_loc = (struct sockaddr_in *)(nttext_loc + 1);
34520Sstevel@tonic-gate 	natt_loc6 = (struct sockaddr_in6 *)(nttext_loc + 1);
34530Sstevel@tonic-gate 	natt_rem = (struct sockaddr_in *)(nttext_rem + 1);
34540Sstevel@tonic-gate 	natt_rem6 = (struct sockaddr_in6 *)(nttext_rem + 1);
34550Sstevel@tonic-gate 
34560Sstevel@tonic-gate 	/* Sundry ADD-specific reality checks. */
34570Sstevel@tonic-gate 	/* XXX STATS :  Logging/stats here? */
34580Sstevel@tonic-gate 	if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
34590Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
34600Sstevel@tonic-gate 		return (EINVAL);
34610Sstevel@tonic-gate 	}
34620Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt == SADB_EALG_NONE) {
34630Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
34640Sstevel@tonic-gate 		return (EINVAL);
34650Sstevel@tonic-gate 	}
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt == SADB_EALG_NULL &&
34680Sstevel@tonic-gate 	    assoc->sadb_sa_auth == SADB_AALG_NONE) {
34690Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
34700Sstevel@tonic-gate 		return (EINVAL);
34710Sstevel@tonic-gate 	}
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & ~(SADB_SAFLAGS_NOREPLAY |
34743055Sdanmcd 	    SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM |
34756668Smarkfen 	    SADB_X_SAFLAGS_TUNNEL | SADB_X_SAFLAGS_OUTBOUND |
34766668Smarkfen 	    SADB_X_SAFLAGS_INBOUND | SADB_X_SAFLAGS_PAIRED)) {
34770Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
34780Sstevel@tonic-gate 		return (EINVAL);
34790Sstevel@tonic-gate 	}
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 	if ((*diagnostic = sadb_hardsoftchk(hard, soft)) != 0) {
34820Sstevel@tonic-gate 		return (EINVAL);
34830Sstevel@tonic-gate 	}
34843055Sdanmcd 	ASSERT(src->sin_family == dst->sin_family);
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) {
34870Sstevel@tonic-gate 		if (nttext_loc == NULL) {
34880Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_LOC;
34890Sstevel@tonic-gate 			return (EINVAL);
34900Sstevel@tonic-gate 		}
34910Sstevel@tonic-gate 
34920Sstevel@tonic-gate 		if (natt_loc->sin_family == AF_INET6 &&
34930Sstevel@tonic-gate 		    !IN6_IS_ADDR_V4MAPPED(&natt_loc6->sin6_addr)) {
34940Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC;
34950Sstevel@tonic-gate 			return (EINVAL);
34960Sstevel@tonic-gate 		}
34970Sstevel@tonic-gate 	}
34980Sstevel@tonic-gate 
34990Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) {
35000Sstevel@tonic-gate 		if (nttext_rem == NULL) {
35010Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_REM;
35020Sstevel@tonic-gate 			return (EINVAL);
35030Sstevel@tonic-gate 		}
35040Sstevel@tonic-gate 		if (natt_rem->sin_family == AF_INET6 &&
35050Sstevel@tonic-gate 		    !IN6_IS_ADDR_V4MAPPED(&natt_rem6->sin6_addr)) {
35060Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM;
35070Sstevel@tonic-gate 			return (EINVAL);
35080Sstevel@tonic-gate 		}
35090Sstevel@tonic-gate 	}
35100Sstevel@tonic-gate 
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 	/* Stuff I don't support, for now.  XXX Diagnostic? */
35130Sstevel@tonic-gate 	if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL ||
35140Sstevel@tonic-gate 	    ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL)
35150Sstevel@tonic-gate 		return (EOPNOTSUPP);
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 	/*
35180Sstevel@tonic-gate 	 * XXX Policy :  I'm not checking identities or sensitivity
35190Sstevel@tonic-gate 	 * labels at this time, but if I did, I'd do them here, before I sent
35200Sstevel@tonic-gate 	 * the weak key check up to the algorithm.
35210Sstevel@tonic-gate 	 */
35220Sstevel@tonic-gate 
35233448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate 	/*
35260Sstevel@tonic-gate 	 * First locate the authentication algorithm.
35270Sstevel@tonic-gate 	 */
35280Sstevel@tonic-gate 	if (akey != NULL) {
35290Sstevel@tonic-gate 		ipsec_alginfo_t *aalg;
35300Sstevel@tonic-gate 
35313448Sdh155122 		aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
35323448Sdh155122 		    [assoc->sadb_sa_auth];
35330Sstevel@tonic-gate 		if (aalg == NULL || !ALG_VALID(aalg)) {
35343448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
35353448Sdh155122 			esp1dbg(espstack, ("Couldn't find auth alg #%d.\n",
35360Sstevel@tonic-gate 			    assoc->sadb_sa_auth));
35370Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
35380Sstevel@tonic-gate 			return (EINVAL);
35390Sstevel@tonic-gate 		}
35402810Smarkfen 
35412810Smarkfen 		/*
35422810Smarkfen 		 * Sanity check key sizes.
35432810Smarkfen 		 * Note: It's not possible to use SADB_AALG_NONE because
35442810Smarkfen 		 * this auth_alg is not defined with ALG_FLAG_VALID. If this
35452810Smarkfen 		 * ever changes, the same check for SADB_AALG_NONE and
35462810Smarkfen 		 * a auth_key != NULL should be made here ( see below).
35472810Smarkfen 		 */
35480Sstevel@tonic-gate 		if (!ipsec_valid_key_size(akey->sadb_key_bits, aalg)) {
35493448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
35500Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
35510Sstevel@tonic-gate 			return (EINVAL);
35520Sstevel@tonic-gate 		}
35532810Smarkfen 		ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 		/* check key and fix parity if needed */
35560Sstevel@tonic-gate 		if (ipsec_check_key(aalg->alg_mech_type, akey, B_TRUE,
35570Sstevel@tonic-gate 		    diagnostic) != 0) {
35583448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
35590Sstevel@tonic-gate 			return (EINVAL);
35600Sstevel@tonic-gate 		}
35610Sstevel@tonic-gate 	}
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	/*
35640Sstevel@tonic-gate 	 * Then locate the encryption algorithm.
35650Sstevel@tonic-gate 	 */
35660Sstevel@tonic-gate 	if (ekey != NULL) {
35670Sstevel@tonic-gate 		ipsec_alginfo_t *ealg;
35680Sstevel@tonic-gate 
35693448Sdh155122 		ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR]
35703448Sdh155122 		    [assoc->sadb_sa_encrypt];
35710Sstevel@tonic-gate 		if (ealg == NULL || !ALG_VALID(ealg)) {
35723448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
35733448Sdh155122 			esp1dbg(espstack, ("Couldn't find encr alg #%d.\n",
35740Sstevel@tonic-gate 			    assoc->sadb_sa_encrypt));
35750Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
35760Sstevel@tonic-gate 			return (EINVAL);
35770Sstevel@tonic-gate 		}
35782810Smarkfen 
35792810Smarkfen 		/*
35802810Smarkfen 		 * Sanity check key sizes. If the encryption algorithm is
35812810Smarkfen 		 * SADB_EALG_NULL but the encryption key is NOT
35822810Smarkfen 		 * NULL then complain.
35832810Smarkfen 		 */
35842810Smarkfen 		if ((assoc->sadb_sa_encrypt == SADB_EALG_NULL) ||
35852810Smarkfen 		    (!ipsec_valid_key_size(ekey->sadb_key_bits, ealg))) {
35863448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
35870Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_EKEYBITS;
35880Sstevel@tonic-gate 			return (EINVAL);
35890Sstevel@tonic-gate 		}
35902810Smarkfen 		ASSERT(ealg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate 		/* check key */
35930Sstevel@tonic-gate 		if (ipsec_check_key(ealg->alg_mech_type, ekey, B_FALSE,
35940Sstevel@tonic-gate 		    diagnostic) != 0) {
35953448Sdh155122 			mutex_exit(&ipss->ipsec_alg_lock);
35960Sstevel@tonic-gate 			return (EINVAL);
35970Sstevel@tonic-gate 		}
35980Sstevel@tonic-gate 	}
35993448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
36000Sstevel@tonic-gate 
36013055Sdanmcd 	return (esp_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
36024987Sdanmcd 	    diagnostic, espstack));
36030Sstevel@tonic-gate }
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate /*
36060Sstevel@tonic-gate  * Update a security association.  Updates come in two varieties.  The first
36070Sstevel@tonic-gate  * is an update of lifetimes on a non-larval SA.  The second is an update of
36080Sstevel@tonic-gate  * a larval SA, which ends up looking a lot more like an add.
36090Sstevel@tonic-gate  */
36100Sstevel@tonic-gate static int
36113448Sdh155122 esp_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
36126668Smarkfen     ipsecesp_stack_t *espstack, uint8_t sadb_msg_type)
36130Sstevel@tonic-gate {
36140Sstevel@tonic-gate 	sadb_address_t *dstext =
36150Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
36160Sstevel@tonic-gate 
36170Sstevel@tonic-gate 	if (dstext == NULL) {
36180Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
36190Sstevel@tonic-gate 		return (EINVAL);
36200Sstevel@tonic-gate 	}
36210Sstevel@tonic-gate 
36226668Smarkfen 	return (sadb_update_sa(mp, ksi, &espstack->esp_sadb,
36236668Smarkfen 	    diagnostic, espstack->esp_pfkey_q,
36246668Smarkfen 	    esp_add_sa, espstack->ipsecesp_netstack, sadb_msg_type));
36250Sstevel@tonic-gate }
36260Sstevel@tonic-gate 
36270Sstevel@tonic-gate /*
36280Sstevel@tonic-gate  * Delete a security association.  This is REALLY likely to be code common to
36290Sstevel@tonic-gate  * both AH and ESP.  Find the association, then unlink it.
36300Sstevel@tonic-gate  */
36310Sstevel@tonic-gate static int
36323448Sdh155122 esp_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
36336668Smarkfen     ipsecesp_stack_t *espstack, uint8_t sadb_msg_type)
36340Sstevel@tonic-gate {
36350Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
36360Sstevel@tonic-gate 	sadb_address_t *dstext =
36370Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
36380Sstevel@tonic-gate 	sadb_address_t *srcext =
36390Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
36400Sstevel@tonic-gate 	struct sockaddr_in *sin;
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate 	if (assoc == NULL) {
36430Sstevel@tonic-gate 		if (dstext != NULL) {
36440Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(dstext + 1);
36450Sstevel@tonic-gate 		} else if (srcext != NULL) {
36460Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(srcext + 1);
36470Sstevel@tonic-gate 		} else {
36480Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
36490Sstevel@tonic-gate 			return (EINVAL);
36500Sstevel@tonic-gate 		}
36513055Sdanmcd 		return (sadb_purge_sa(mp, ksi,
36523448Sdh155122 		    (sin->sin_family == AF_INET6) ? &espstack->esp_sadb.s_v6 :
36533448Sdh155122 		    &espstack->esp_sadb.s_v4, espstack->esp_pfkey_q,
36543448Sdh155122 		    espstack->esp_sadb.s_ip_q));
36550Sstevel@tonic-gate 	}
36560Sstevel@tonic-gate 
36576668Smarkfen 	return (sadb_delget_sa(mp, ksi, &espstack->esp_sadb, diagnostic,
36586668Smarkfen 	    espstack->esp_pfkey_q, sadb_msg_type));
36590Sstevel@tonic-gate }
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate /*
36620Sstevel@tonic-gate  * Convert the entire contents of all of ESP's SA tables into PF_KEY SADB_DUMP
36630Sstevel@tonic-gate  * messages.
36640Sstevel@tonic-gate  */
36650Sstevel@tonic-gate static void
36663448Sdh155122 esp_dump(mblk_t *mp, keysock_in_t *ksi, ipsecesp_stack_t *espstack)
36670Sstevel@tonic-gate {
36680Sstevel@tonic-gate 	int error;
36690Sstevel@tonic-gate 	sadb_msg_t *samsg;
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate 	/*
36720Sstevel@tonic-gate 	 * Dump each fanout, bailing if error is non-zero.
36730Sstevel@tonic-gate 	 */
36740Sstevel@tonic-gate 
36753448Sdh155122 	error = sadb_dump(espstack->esp_pfkey_q, mp, ksi->ks_in_serial,
36763448Sdh155122 	    &espstack->esp_sadb.s_v4);
36770Sstevel@tonic-gate 	if (error != 0)
36780Sstevel@tonic-gate 		goto bail;
36790Sstevel@tonic-gate 
36803448Sdh155122 	error = sadb_dump(espstack->esp_pfkey_q, mp, ksi->ks_in_serial,
36813448Sdh155122 	    &espstack->esp_sadb.s_v6);
36820Sstevel@tonic-gate bail:
36830Sstevel@tonic-gate 	ASSERT(mp->b_cont != NULL);
36840Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
36850Sstevel@tonic-gate 	samsg->sadb_msg_errno = (uint8_t)error;
36863448Sdh155122 	sadb_pfkey_echo(espstack->esp_pfkey_q, mp,
36873448Sdh155122 	    (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL);
36880Sstevel@tonic-gate }
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate /*
36913055Sdanmcd  * First-cut reality check for an inbound PF_KEY message.
36923055Sdanmcd  */
36933055Sdanmcd static boolean_t
36943448Sdh155122 esp_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
36953448Sdh155122     ipsecesp_stack_t *espstack)
36963055Sdanmcd {
36973055Sdanmcd 	int diagnostic;
36983055Sdanmcd 
36993055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
37003055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
37013055Sdanmcd 		goto badmsg;
37023055Sdanmcd 	}
37033055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
37043055Sdanmcd 	    ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
37053055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
37063055Sdanmcd 		goto badmsg;
37073055Sdanmcd 	}
37083055Sdanmcd 	return (B_FALSE);	/* False ==> no failures */
37093055Sdanmcd 
37103055Sdanmcd badmsg:
37113448Sdh155122 	sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL, diagnostic,
37123055Sdanmcd 	    ksi->ks_in_serial);
37133055Sdanmcd 	return (B_TRUE);	/* True ==> failures */
37143055Sdanmcd }
37153055Sdanmcd 
37163055Sdanmcd /*
37170Sstevel@tonic-gate  * ESP parsing of PF_KEY messages.  Keysock did most of the really silly
37180Sstevel@tonic-gate  * error cases.  What I receive is a fully-formed, syntactically legal
37190Sstevel@tonic-gate  * PF_KEY message.  I then need to check semantics...
37200Sstevel@tonic-gate  *
37210Sstevel@tonic-gate  * This code may become common to AH and ESP.  Stay tuned.
37220Sstevel@tonic-gate  *
37230Sstevel@tonic-gate  * I also make the assumption that db_ref's are cool.  If this assumption
37240Sstevel@tonic-gate  * is wrong, this means that someone other than keysock or me has been
37250Sstevel@tonic-gate  * mucking with PF_KEY messages.
37260Sstevel@tonic-gate  */
37270Sstevel@tonic-gate static void
37283448Sdh155122 esp_parse_pfkey(mblk_t *mp, ipsecesp_stack_t *espstack)
37290Sstevel@tonic-gate {
37300Sstevel@tonic-gate 	mblk_t *msg = mp->b_cont;
37310Sstevel@tonic-gate 	sadb_msg_t *samsg;
37320Sstevel@tonic-gate 	keysock_in_t *ksi;
37330Sstevel@tonic-gate 	int error;
37340Sstevel@tonic-gate 	int diagnostic = SADB_X_DIAGNOSTIC_NONE;
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate 	ASSERT(msg != NULL);
37373448Sdh155122 
37380Sstevel@tonic-gate 	samsg = (sadb_msg_t *)msg->b_rptr;
37390Sstevel@tonic-gate 	ksi = (keysock_in_t *)mp->b_rptr;
37400Sstevel@tonic-gate 
37410Sstevel@tonic-gate 	/*
37420Sstevel@tonic-gate 	 * If applicable, convert unspecified AF_INET6 to unspecified
37433055Sdanmcd 	 * AF_INET.  And do other address reality checks.
37440Sstevel@tonic-gate 	 */
37453448Sdh155122 	if (!sadb_addrfix(ksi, espstack->esp_pfkey_q, mp,
37463448Sdh155122 	    espstack->ipsecesp_netstack) ||
37473448Sdh155122 	    esp_pfkey_reality_failures(mp, ksi, espstack)) {
37483055Sdanmcd 		return;
37493055Sdanmcd 	}
37500Sstevel@tonic-gate 
37510Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
37520Sstevel@tonic-gate 	case SADB_ADD:
37533448Sdh155122 		error = esp_add_sa(mp, ksi, &diagnostic,
37543448Sdh155122 		    espstack->ipsecesp_netstack);
37550Sstevel@tonic-gate 		if (error != 0) {
37563448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
37573448Sdh155122 			    diagnostic, ksi->ks_in_serial);
37580Sstevel@tonic-gate 		}
37590Sstevel@tonic-gate 		/* else esp_add_sa() took care of things. */
37600Sstevel@tonic-gate 		break;
37610Sstevel@tonic-gate 	case SADB_DELETE:
37626668Smarkfen 	case SADB_X_DELPAIR:
37636668Smarkfen 		error = esp_del_sa(mp, ksi, &diagnostic, espstack,
37646668Smarkfen 		    samsg->sadb_msg_type);
37650Sstevel@tonic-gate 		if (error != 0) {
37663448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
37673448Sdh155122 			    diagnostic, ksi->ks_in_serial);
37680Sstevel@tonic-gate 		}
37690Sstevel@tonic-gate 		/* Else esp_del_sa() took care of things. */
37700Sstevel@tonic-gate 		break;
37710Sstevel@tonic-gate 	case SADB_GET:
37726668Smarkfen 		error = sadb_delget_sa(mp, ksi, &espstack->esp_sadb,
37736668Smarkfen 		    &diagnostic, espstack->esp_pfkey_q, samsg->sadb_msg_type);
37740Sstevel@tonic-gate 		if (error != 0) {
37753448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
37763448Sdh155122 			    diagnostic, ksi->ks_in_serial);
37770Sstevel@tonic-gate 		}
37780Sstevel@tonic-gate 		/* Else sadb_get_sa() took care of things. */
37790Sstevel@tonic-gate 		break;
37800Sstevel@tonic-gate 	case SADB_FLUSH:
37813448Sdh155122 		sadbp_flush(&espstack->esp_sadb, espstack->ipsecesp_netstack);
37823448Sdh155122 		sadb_pfkey_echo(espstack->esp_pfkey_q, mp, samsg, ksi, NULL);
37830Sstevel@tonic-gate 		break;
37840Sstevel@tonic-gate 	case SADB_REGISTER:
37850Sstevel@tonic-gate 		/*
37860Sstevel@tonic-gate 		 * Hmmm, let's do it!  Check for extensions (there should
37870Sstevel@tonic-gate 		 * be none), extract the fields, call esp_register_out(),
37880Sstevel@tonic-gate 		 * then either free or report an error.
37890Sstevel@tonic-gate 		 *
37900Sstevel@tonic-gate 		 * Keysock takes care of the PF_KEY bookkeeping for this.
37910Sstevel@tonic-gate 		 */
37920Sstevel@tonic-gate 		if (esp_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
37933448Sdh155122 		    ksi->ks_in_serial, espstack)) {
37940Sstevel@tonic-gate 			freemsg(mp);
37950Sstevel@tonic-gate 		} else {
37960Sstevel@tonic-gate 			/*
37970Sstevel@tonic-gate 			 * Only way this path hits is if there is a memory
37980Sstevel@tonic-gate 			 * failure.  It will not return B_FALSE because of
37990Sstevel@tonic-gate 			 * lack of esp_pfkey_q if I am in wput().
38000Sstevel@tonic-gate 			 */
38013448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, ENOMEM,
38023448Sdh155122 			    diagnostic, ksi->ks_in_serial);
38030Sstevel@tonic-gate 		}
38040Sstevel@tonic-gate 		break;
38050Sstevel@tonic-gate 	case SADB_UPDATE:
38066668Smarkfen 	case SADB_X_UPDATEPAIR:
38070Sstevel@tonic-gate 		/*
38080Sstevel@tonic-gate 		 * Find a larval, if not there, find a full one and get
38090Sstevel@tonic-gate 		 * strict.
38100Sstevel@tonic-gate 		 */
38116668Smarkfen 		error = esp_update_sa(mp, ksi, &diagnostic, espstack,
38126668Smarkfen 		    samsg->sadb_msg_type);
38130Sstevel@tonic-gate 		if (error != 0) {
38143448Sdh155122 			sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
38153448Sdh155122 			    diagnostic, ksi->ks_in_serial);
38160Sstevel@tonic-gate 		}
38170Sstevel@tonic-gate 		/* else esp_update_sa() took care of things. */
38180Sstevel@tonic-gate 		break;
38190Sstevel@tonic-gate 	case SADB_GETSPI:
38200Sstevel@tonic-gate 		/*
38210Sstevel@tonic-gate 		 * Reserve a new larval entry.
38220Sstevel@tonic-gate 		 */
38233448Sdh155122 		esp_getspi(mp, ksi, espstack);
38240Sstevel@tonic-gate 		break;
38250Sstevel@tonic-gate 	case SADB_ACQUIRE:
38260Sstevel@tonic-gate 		/*
38270Sstevel@tonic-gate 		 * Find larval and/or ACQUIRE record and kill it (them), I'm
38280Sstevel@tonic-gate 		 * most likely an error.  Inbound ACQUIRE messages should only
38290Sstevel@tonic-gate 		 * have the base header.
38300Sstevel@tonic-gate 		 */
38313448Sdh155122 		sadb_in_acquire(samsg, &espstack->esp_sadb,
38323448Sdh155122 		    espstack->esp_pfkey_q, espstack->ipsecesp_netstack);
38330Sstevel@tonic-gate 		freemsg(mp);
38340Sstevel@tonic-gate 		break;
38350Sstevel@tonic-gate 	case SADB_DUMP:
38360Sstevel@tonic-gate 		/*
38370Sstevel@tonic-gate 		 * Dump all entries.
38380Sstevel@tonic-gate 		 */
38393448Sdh155122 		esp_dump(mp, ksi, espstack);
38400Sstevel@tonic-gate 		/* esp_dump will take care of the return message, etc. */
38410Sstevel@tonic-gate 		break;
38420Sstevel@tonic-gate 	case SADB_EXPIRE:
38430Sstevel@tonic-gate 		/* Should never reach me. */
38443448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, EOPNOTSUPP,
38453448Sdh155122 		    diagnostic, ksi->ks_in_serial);
38460Sstevel@tonic-gate 		break;
38470Sstevel@tonic-gate 	default:
38483448Sdh155122 		sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL,
38490Sstevel@tonic-gate 		    SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
38500Sstevel@tonic-gate 		break;
38510Sstevel@tonic-gate 	}
38520Sstevel@tonic-gate }
38530Sstevel@tonic-gate 
38540Sstevel@tonic-gate /*
38550Sstevel@tonic-gate  * Handle case where PF_KEY says it can't find a keysock for one of my
38560Sstevel@tonic-gate  * ACQUIRE messages.
38570Sstevel@tonic-gate  */
38580Sstevel@tonic-gate static void
38593448Sdh155122 esp_keysock_no_socket(mblk_t *mp, ipsecesp_stack_t *espstack)
38600Sstevel@tonic-gate {
38610Sstevel@tonic-gate 	sadb_msg_t *samsg;
38620Sstevel@tonic-gate 	keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
38630Sstevel@tonic-gate 
38640Sstevel@tonic-gate 	if (mp->b_cont == NULL) {
38650Sstevel@tonic-gate 		freemsg(mp);
38660Sstevel@tonic-gate 		return;
38670Sstevel@tonic-gate 	}
38680Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
38690Sstevel@tonic-gate 
38700Sstevel@tonic-gate 	/*
38710Sstevel@tonic-gate 	 * If keysock can't find any registered, delete the acquire record
38720Sstevel@tonic-gate 	 * immediately, and handle errors.
38730Sstevel@tonic-gate 	 */
38740Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_ACQUIRE) {
38750Sstevel@tonic-gate 		samsg->sadb_msg_errno = kse->ks_err_errno;
38760Sstevel@tonic-gate 		samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
38770Sstevel@tonic-gate 		/*
38780Sstevel@tonic-gate 		 * Use the write-side of the esp_pfkey_q, in case there is
38790Sstevel@tonic-gate 		 * no esp_sadb.s_ip_q.
38800Sstevel@tonic-gate 		 */
38813448Sdh155122 		sadb_in_acquire(samsg, &espstack->esp_sadb,
38823448Sdh155122 		    WR(espstack->esp_pfkey_q), espstack->ipsecesp_netstack);
38830Sstevel@tonic-gate 	}
38840Sstevel@tonic-gate 
38850Sstevel@tonic-gate 	freemsg(mp);
38860Sstevel@tonic-gate }
38870Sstevel@tonic-gate 
38880Sstevel@tonic-gate /*
38890Sstevel@tonic-gate  * ESP module write put routine.
38900Sstevel@tonic-gate  */
38910Sstevel@tonic-gate static void
38920Sstevel@tonic-gate ipsecesp_wput(queue_t *q, mblk_t *mp)
38930Sstevel@tonic-gate {
38940Sstevel@tonic-gate 	ipsec_info_t *ii;
38950Sstevel@tonic-gate 	struct iocblk *iocp;
38963448Sdh155122 	ipsecesp_stack_t	*espstack = (ipsecesp_stack_t *)q->q_ptr;
38973448Sdh155122 
38983448Sdh155122 	esp3dbg(espstack, ("In esp_wput().\n"));
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate 	/* NOTE: Each case must take care of freeing or passing mp. */
39010Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
39020Sstevel@tonic-gate 	case M_CTL:
39030Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
39040Sstevel@tonic-gate 			/* Not big enough message. */
39050Sstevel@tonic-gate 			freemsg(mp);
39060Sstevel@tonic-gate 			break;
39070Sstevel@tonic-gate 		}
39080Sstevel@tonic-gate 		ii = (ipsec_info_t *)mp->b_rptr;
39090Sstevel@tonic-gate 
39100Sstevel@tonic-gate 		switch (ii->ipsec_info_type) {
39110Sstevel@tonic-gate 		case KEYSOCK_OUT_ERR:
39123448Sdh155122 			esp1dbg(espstack, ("Got KEYSOCK_OUT_ERR message.\n"));
39133448Sdh155122 			esp_keysock_no_socket(mp, espstack);
39140Sstevel@tonic-gate 			break;
39150Sstevel@tonic-gate 		case KEYSOCK_IN:
39163448Sdh155122 			ESP_BUMP_STAT(espstack, keysock_in);
39173448Sdh155122 			esp3dbg(espstack, ("Got KEYSOCK_IN message.\n"));
39183055Sdanmcd 
39193055Sdanmcd 			/* Parse the message. */
39203448Sdh155122 			esp_parse_pfkey(mp, espstack);
39210Sstevel@tonic-gate 			break;
39220Sstevel@tonic-gate 		case KEYSOCK_HELLO:
39233448Sdh155122 			sadb_keysock_hello(&espstack->esp_pfkey_q, q, mp,
39243448Sdh155122 			    esp_ager, (void *)espstack, &espstack->esp_event,
39253448Sdh155122 			    SADB_SATYPE_ESP);
39260Sstevel@tonic-gate 			break;
39270Sstevel@tonic-gate 		default:
39283448Sdh155122 			esp2dbg(espstack, ("Got M_CTL from above of 0x%x.\n",
39290Sstevel@tonic-gate 			    ii->ipsec_info_type));
39300Sstevel@tonic-gate 			freemsg(mp);
39310Sstevel@tonic-gate 			break;
39320Sstevel@tonic-gate 		}
39330Sstevel@tonic-gate 		break;
39340Sstevel@tonic-gate 	case M_IOCTL:
39350Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
39360Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
39370Sstevel@tonic-gate 		case ND_SET:
39380Sstevel@tonic-gate 		case ND_GET:
39393448Sdh155122 			if (nd_getset(q, espstack->ipsecesp_g_nd, mp)) {
39400Sstevel@tonic-gate 				qreply(q, mp);
39410Sstevel@tonic-gate 				return;
39420Sstevel@tonic-gate 			} else {
39430Sstevel@tonic-gate 				iocp->ioc_error = ENOENT;
39440Sstevel@tonic-gate 			}
39450Sstevel@tonic-gate 			/* FALLTHRU */
39460Sstevel@tonic-gate 		default:
39470Sstevel@tonic-gate 			/* We really don't support any other ioctls, do we? */
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 			/* Return EINVAL */
39500Sstevel@tonic-gate 			if (iocp->ioc_error != ENOENT)
39510Sstevel@tonic-gate 				iocp->ioc_error = EINVAL;
39520Sstevel@tonic-gate 			iocp->ioc_count = 0;
39530Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
39540Sstevel@tonic-gate 			qreply(q, mp);
39550Sstevel@tonic-gate 			return;
39560Sstevel@tonic-gate 		}
39570Sstevel@tonic-gate 	default:
39583448Sdh155122 		esp3dbg(espstack,
39593448Sdh155122 		    ("Got default message, type %d, passing to IP.\n",
39600Sstevel@tonic-gate 		    mp->b_datap->db_type));
39610Sstevel@tonic-gate 		putnext(q, mp);
39620Sstevel@tonic-gate 	}
39630Sstevel@tonic-gate }
39640Sstevel@tonic-gate 
39650Sstevel@tonic-gate /*
39660Sstevel@tonic-gate  * Process an outbound ESP packet that can be accelerated by a IPsec
39670Sstevel@tonic-gate  * hardware acceleration capable Provider.
39680Sstevel@tonic-gate  * The caller already inserted and initialized the ESP header.
39690Sstevel@tonic-gate  * This function allocates a tagging M_CTL, and adds room at the end
39700Sstevel@tonic-gate  * of the packet to hold the ICV if authentication is needed.
39710Sstevel@tonic-gate  *
39720Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
39730Sstevel@tonic-gate  * mblk chain ipsec_out.
39740Sstevel@tonic-gate  */
39750Sstevel@tonic-gate static ipsec_status_t
39760Sstevel@tonic-gate esp_outbound_accelerated(mblk_t *ipsec_out, uint_t icv_len)
39770Sstevel@tonic-gate {
39780Sstevel@tonic-gate 	ipsec_out_t *io;
39790Sstevel@tonic-gate 	mblk_t *lastmp;
39803448Sdh155122 	netstack_t	*ns;
39813448Sdh155122 	ipsecesp_stack_t *espstack;
39823448Sdh155122 	ipsec_stack_t	*ipss;
39830Sstevel@tonic-gate 
39840Sstevel@tonic-gate 	io = (ipsec_out_t *)ipsec_out->b_rptr;
39853448Sdh155122 	ns = io->ipsec_out_ns;
39863448Sdh155122 	espstack = ns->netstack_ipsecesp;
39873448Sdh155122 	ipss = ns->netstack_ipsec;
39883448Sdh155122 
39893448Sdh155122 	ESP_BUMP_STAT(espstack, out_accelerated);
39900Sstevel@tonic-gate 
39910Sstevel@tonic-gate 	/* mark packet as being accelerated in IPSEC_OUT */
39920Sstevel@tonic-gate 	ASSERT(io->ipsec_out_accelerated == B_FALSE);
39930Sstevel@tonic-gate 	io->ipsec_out_accelerated = B_TRUE;
39940Sstevel@tonic-gate 
39950Sstevel@tonic-gate 	/*
39960Sstevel@tonic-gate 	 * add room at the end of the packet for the ICV if needed
39970Sstevel@tonic-gate 	 */
39980Sstevel@tonic-gate 	if (icv_len > 0) {
39990Sstevel@tonic-gate 		/* go to last mblk */
40000Sstevel@tonic-gate 		lastmp = ipsec_out;	/* For following while loop. */
40010Sstevel@tonic-gate 		do {
40020Sstevel@tonic-gate 			lastmp = lastmp->b_cont;
40030Sstevel@tonic-gate 		} while (lastmp->b_cont != NULL);
40040Sstevel@tonic-gate 
40050Sstevel@tonic-gate 		/* if not enough available room, allocate new mblk */
40060Sstevel@tonic-gate 		if ((lastmp->b_wptr + icv_len) > lastmp->b_datap->db_lim) {
40070Sstevel@tonic-gate 			lastmp->b_cont = allocb(icv_len, BPRI_HI);
40080Sstevel@tonic-gate 			if (lastmp->b_cont == NULL) {
40093448Sdh155122 				ESP_BUMP_STAT(espstack, out_discards);
40100Sstevel@tonic-gate 				ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
40113448Sdh155122 				    DROPPER(ipss, ipds_esp_nomem),
40123448Sdh155122 				    &espstack->esp_dropper);
40130Sstevel@tonic-gate 				return (IPSEC_STATUS_FAILED);
40140Sstevel@tonic-gate 			}
40150Sstevel@tonic-gate 			lastmp = lastmp->b_cont;
40160Sstevel@tonic-gate 		}
40170Sstevel@tonic-gate 		lastmp->b_wptr += icv_len;
40180Sstevel@tonic-gate 	}
40190Sstevel@tonic-gate 
40200Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
40210Sstevel@tonic-gate }
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate /*
40240Sstevel@tonic-gate  * Process an inbound accelerated ESP packet.
40250Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
40260Sstevel@tonic-gate  * mblk chain ipsec_in.
40270Sstevel@tonic-gate  */
40280Sstevel@tonic-gate static ipsec_status_t
40290Sstevel@tonic-gate esp_inbound_accelerated(mblk_t *ipsec_in, mblk_t *data_mp, boolean_t isv4,
40300Sstevel@tonic-gate     ipsa_t *assoc)
40310Sstevel@tonic-gate {
40323448Sdh155122 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
40330Sstevel@tonic-gate 	mblk_t *hada_mp;
40340Sstevel@tonic-gate 	uint32_t icv_len = 0;
40350Sstevel@tonic-gate 	da_ipsec_t *hada;
40360Sstevel@tonic-gate 	ipha_t *ipha;
40370Sstevel@tonic-gate 	ip6_t *ip6h;
40380Sstevel@tonic-gate 	kstat_named_t *counter;
40393448Sdh155122 	netstack_t	*ns = ii->ipsec_in_ns;
40403448Sdh155122 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
40413448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
40423448Sdh155122 
40433448Sdh155122 	ESP_BUMP_STAT(espstack, in_accelerated);
40443448Sdh155122 
40450Sstevel@tonic-gate 	hada_mp = ii->ipsec_in_da;
40460Sstevel@tonic-gate 	ASSERT(hada_mp != NULL);
40470Sstevel@tonic-gate 	hada = (da_ipsec_t *)hada_mp->b_rptr;
40480Sstevel@tonic-gate 
40490Sstevel@tonic-gate 	/*
40500Sstevel@tonic-gate 	 * We only support one level of decapsulation in hardware, so
40510Sstevel@tonic-gate 	 * nuke the pointer.
40520Sstevel@tonic-gate 	 */
40530Sstevel@tonic-gate 	ii->ipsec_in_da = NULL;
40540Sstevel@tonic-gate 	ii->ipsec_in_accelerated = B_FALSE;
40550Sstevel@tonic-gate 
40560Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) {
40570Sstevel@tonic-gate 		/*
40580Sstevel@tonic-gate 		 * ESP with authentication. We expect the Provider to have
40590Sstevel@tonic-gate 		 * computed the ICV and placed it in the hardware acceleration
40600Sstevel@tonic-gate 		 * data attributes.
40610Sstevel@tonic-gate 		 *
40620Sstevel@tonic-gate 		 * Extract ICV length from attributes M_CTL and sanity check
40630Sstevel@tonic-gate 		 * its value. We allow the mblk to be smaller than da_ipsec_t
40640Sstevel@tonic-gate 		 * for a small ICV, as long as the entire ICV fits within the
40650Sstevel@tonic-gate 		 * mblk.
40660Sstevel@tonic-gate 		 *
40670Sstevel@tonic-gate 		 * Also ensures that the ICV length computed by Provider
40680Sstevel@tonic-gate 		 * corresponds to the ICV length of the agorithm specified by
40690Sstevel@tonic-gate 		 * the SA.
40700Sstevel@tonic-gate 		 */
40710Sstevel@tonic-gate 		icv_len = hada->da_icv_len;
40720Sstevel@tonic-gate 		if ((icv_len != assoc->ipsa_mac_len) ||
40730Sstevel@tonic-gate 		    (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) <
40744987Sdanmcd 		    (sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) {
40750Sstevel@tonic-gate 			esp0dbg(("esp_inbound_accelerated: "
40760Sstevel@tonic-gate 			    "ICV len (%u) incorrect or mblk too small (%u)\n",
40770Sstevel@tonic-gate 			    icv_len, (uint32_t)(MBLKL(hada_mp))));
40783448Sdh155122 			counter = DROPPER(ipss, ipds_esp_bad_auth);
40790Sstevel@tonic-gate 			goto esp_in_discard;
40800Sstevel@tonic-gate 		}
40810Sstevel@tonic-gate 	}
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate 	/* get pointers to IP header */
40840Sstevel@tonic-gate 	if (isv4) {
40850Sstevel@tonic-gate 		ipha = (ipha_t *)data_mp->b_rptr;
40860Sstevel@tonic-gate 	} else {
40870Sstevel@tonic-gate 		ip6h = (ip6_t *)data_mp->b_rptr;
40880Sstevel@tonic-gate 	}
40890Sstevel@tonic-gate 
40900Sstevel@tonic-gate 	/*
40910Sstevel@tonic-gate 	 * Compare ICV in ESP packet vs ICV computed by adapter.
40920Sstevel@tonic-gate 	 * We also remove the ICV from the end of the packet since
40930Sstevel@tonic-gate 	 * it will no longer be needed.
40940Sstevel@tonic-gate 	 *
40950Sstevel@tonic-gate 	 * Assume that esp_inbound() already ensured that the pkt
40960Sstevel@tonic-gate 	 * was in one mblk.
40970Sstevel@tonic-gate 	 */
40980Sstevel@tonic-gate 	ASSERT(data_mp->b_cont == NULL);
40990Sstevel@tonic-gate 	data_mp->b_wptr -= icv_len;
41000Sstevel@tonic-gate 	/* adjust IP header */
41010Sstevel@tonic-gate 	if (isv4)
41020Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) - icv_len);
41030Sstevel@tonic-gate 	else
41040Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - icv_len);
41050Sstevel@tonic-gate 	if (icv_len && bcmp(hada->da_icv, data_mp->b_wptr, icv_len)) {
41060Sstevel@tonic-gate 		int af;
41070Sstevel@tonic-gate 		void *addr;
41080Sstevel@tonic-gate 
41090Sstevel@tonic-gate 		if (isv4) {
41100Sstevel@tonic-gate 			addr = &ipha->ipha_dst;
41110Sstevel@tonic-gate 			af = AF_INET;
41120Sstevel@tonic-gate 		} else {
41130Sstevel@tonic-gate 			addr = &ip6h->ip6_dst;
41140Sstevel@tonic-gate 			af = AF_INET6;
41150Sstevel@tonic-gate 		}
41160Sstevel@tonic-gate 
41170Sstevel@tonic-gate 		/*
41180Sstevel@tonic-gate 		 * Log the event. Don't print to the console, block
41190Sstevel@tonic-gate 		 * potential denial-of-service attack.
41200Sstevel@tonic-gate 		 */
41213448Sdh155122 		ESP_BUMP_STAT(espstack, bad_auth);
41220Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
41230Sstevel@tonic-gate 		    "ESP Authentication failed spi %x, dst_addr %s",
41243448Sdh155122 		    assoc->ipsa_spi, addr, af, espstack->ipsecesp_netstack);
41253448Sdh155122 		counter = DROPPER(ipss, ipds_esp_bad_auth);
41260Sstevel@tonic-gate 		goto esp_in_discard;
41270Sstevel@tonic-gate 	}
41280Sstevel@tonic-gate 
41293448Sdh155122 	esp3dbg(espstack, ("esp_inbound_accelerated: ESP authentication "
41304987Sdanmcd 	    "succeeded, checking replay\n"));
41310Sstevel@tonic-gate 
41320Sstevel@tonic-gate 	ipsec_in->b_cont = data_mp;
41330Sstevel@tonic-gate 
41340Sstevel@tonic-gate 	/*
41350Sstevel@tonic-gate 	 * Remove ESP header and padding from packet.
41360Sstevel@tonic-gate 	 */
41370Sstevel@tonic-gate 	if (!esp_strip_header(data_mp, ii->ipsec_in_v4, assoc->ipsa_iv_len,
41384987Sdanmcd 	    &counter, espstack)) {
41393448Sdh155122 		esp1dbg(espstack, ("esp_inbound_accelerated: "
41400Sstevel@tonic-gate 		    "esp_strip_header() failed\n"));
41410Sstevel@tonic-gate 		goto esp_in_discard;
41420Sstevel@tonic-gate 	}
41430Sstevel@tonic-gate 
41440Sstevel@tonic-gate 	freeb(hada_mp);
41450Sstevel@tonic-gate 
41460Sstevel@tonic-gate 	/*
41470Sstevel@tonic-gate 	 * Account for usage..
41480Sstevel@tonic-gate 	 */
41490Sstevel@tonic-gate 	if (!esp_age_bytes(assoc, msgdsize(data_mp), B_TRUE)) {
41500Sstevel@tonic-gate 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
41513448Sdh155122 		ESP_BUMP_STAT(espstack, bytes_expired);
41523448Sdh155122 		IP_ESP_BUMP_STAT(ipss, in_discards);
41530Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
41540Sstevel@tonic-gate 		    "ESP association 0x%x, dst %s had bytes expire.\n",
41553448Sdh155122 		    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
41563448Sdh155122 		    espstack->ipsecesp_netstack);
41570Sstevel@tonic-gate 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
41583448Sdh155122 		    DROPPER(ipss, ipds_esp_bytes_expire),
41593448Sdh155122 		    &espstack->esp_dropper);
41600Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
41610Sstevel@tonic-gate 	}
41620Sstevel@tonic-gate 
41630Sstevel@tonic-gate 	/* done processing the packet */
41640Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
41650Sstevel@tonic-gate 
41660Sstevel@tonic-gate esp_in_discard:
41673448Sdh155122 	IP_ESP_BUMP_STAT(ipss, in_discards);
41680Sstevel@tonic-gate 	freeb(hada_mp);
41690Sstevel@tonic-gate 
41700Sstevel@tonic-gate 	ipsec_in->b_cont = data_mp;	/* For ip_drop_packet()'s sake... */
41713448Sdh155122 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter,
41723448Sdh155122 	    &espstack->esp_dropper);
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
41750Sstevel@tonic-gate }
41760Sstevel@tonic-gate 
41770Sstevel@tonic-gate /*
41780Sstevel@tonic-gate  * Wrapper to allow IP to trigger an ESP association failure message
41790Sstevel@tonic-gate  * during inbound SA selection.
41800Sstevel@tonic-gate  */
41810Sstevel@tonic-gate void
41820Sstevel@tonic-gate ipsecesp_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
41833448Sdh155122     uint32_t spi, void *addr, int af, ipsecesp_stack_t *espstack)
41840Sstevel@tonic-gate {
41853448Sdh155122 	ipsec_stack_t	*ipss = espstack->ipsecesp_netstack->netstack_ipsec;
41863448Sdh155122 
41873448Sdh155122 	if (espstack->ipsecesp_log_unknown_spi) {
41880Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
41893448Sdh155122 		    addr, af, espstack->ipsecesp_netstack);
41900Sstevel@tonic-gate 	}
41910Sstevel@tonic-gate 
41923448Sdh155122 	ip_drop_packet(mp, B_TRUE, NULL, NULL,
41933448Sdh155122 	    DROPPER(ipss, ipds_esp_no_sa),
41943448Sdh155122 	    &espstack->esp_dropper);
41950Sstevel@tonic-gate }
41960Sstevel@tonic-gate 
41970Sstevel@tonic-gate /*
41980Sstevel@tonic-gate  * Initialize the ESP input and output processing functions.
41990Sstevel@tonic-gate  */
42000Sstevel@tonic-gate void
42010Sstevel@tonic-gate ipsecesp_init_funcs(ipsa_t *sa)
42020Sstevel@tonic-gate {
42030Sstevel@tonic-gate 	if (sa->ipsa_output_func == NULL)
42040Sstevel@tonic-gate 		sa->ipsa_output_func = esp_outbound;
42050Sstevel@tonic-gate 	if (sa->ipsa_input_func == NULL)
42060Sstevel@tonic-gate 		sa->ipsa_input_func = esp_inbound;
42070Sstevel@tonic-gate }
4208