xref: /onnv-gate/usr/src/uts/common/inet/ip/ipsecesp.c (revision 134:5a95d4458a76)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*134Sdanmcd  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/stropts.h>
320Sstevel@tonic-gate #include <sys/errno.h>
330Sstevel@tonic-gate #include <sys/strlog.h>
340Sstevel@tonic-gate #include <sys/tihdr.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <sys/ddi.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/kmem.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>
550Sstevel@tonic-gate #include <inet/ip6.h>
560Sstevel@tonic-gate #include <inet/sadb.h>
570Sstevel@tonic-gate #include <inet/ipsec_info.h>
580Sstevel@tonic-gate #include <inet/ipsec_impl.h>
590Sstevel@tonic-gate #include <inet/ipsecesp.h>
600Sstevel@tonic-gate #include <inet/ipdrop.h>
610Sstevel@tonic-gate #include <inet/tcp.h>
620Sstevel@tonic-gate #include <sys/kstat.h>
630Sstevel@tonic-gate #include <sys/policy.h>
640Sstevel@tonic-gate #include <sys/strsun.h>
650Sstevel@tonic-gate #include <inet/udp_impl.h>
660Sstevel@tonic-gate #include <sys/taskq.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <sys/iphada.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /* Packet dropper for ESP drops. */
710Sstevel@tonic-gate static ipdropper_t esp_dropper;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static kmutex_t ipsecesp_param_lock; /* Protects ipsecesp_param_arr[] below. */
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Table of ND variables supported by ipsecesp. These are loaded into
760Sstevel@tonic-gate  * ipsecesp_g_nd in ipsecesp_init_nd.
770Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate static	ipsecespparam_t	ipsecesp_param_arr[] = {
800Sstevel@tonic-gate 	/* min	max			value	name */
810Sstevel@tonic-gate 	{ 0,	3,			0,	"ipsecesp_debug"},
820Sstevel@tonic-gate 	{ 125,	32000, SADB_AGE_INTERVAL_DEFAULT, "ipsecesp_age_interval"},
830Sstevel@tonic-gate 	{ 1,	10,			1,	"ipsecesp_reap_delay"},
840Sstevel@tonic-gate 	{ 1,	SADB_MAX_REPLAY,	64,	"ipsecesp_replay_size"},
850Sstevel@tonic-gate 	{ 1,	300,			15,	"ipsecesp_acquire_timeout"},
860Sstevel@tonic-gate 	{ 1,	1800,			90,	"ipsecesp_larval_timeout"},
870Sstevel@tonic-gate 	/* Default lifetime values for ACQUIRE messages. */
880Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_soft_bytes"},
890Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_hard_bytes"},
900Sstevel@tonic-gate 	{ 0,	0xffffffffU,	24000,	"ipsecesp_default_soft_addtime"},
910Sstevel@tonic-gate 	{ 0,	0xffffffffU,	28800,	"ipsecesp_default_hard_addtime"},
920Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_soft_usetime"},
930Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_hard_usetime"},
940Sstevel@tonic-gate 	{ 0,	1,		0,	"ipsecesp_log_unknown_spi"},
950Sstevel@tonic-gate 	{ 0,	2,		1,	"ipsecesp_padding_check"},
960Sstevel@tonic-gate };
970Sstevel@tonic-gate #define	ipsecesp_debug		ipsecesp_param_arr[0].ipsecesp_param_value
980Sstevel@tonic-gate #define	ipsecesp_age_interval	ipsecesp_param_arr[1].ipsecesp_param_value
990Sstevel@tonic-gate #define	ipsecesp_age_int_max	ipsecesp_param_arr[1].ipsecesp_param_max
1000Sstevel@tonic-gate #define	ipsecesp_reap_delay	ipsecesp_param_arr[2].ipsecesp_param_value
1010Sstevel@tonic-gate #define	ipsecesp_replay_size	ipsecesp_param_arr[3].ipsecesp_param_value
1020Sstevel@tonic-gate #define	ipsecesp_acquire_timeout ipsecesp_param_arr[4].ipsecesp_param_value
1030Sstevel@tonic-gate #define	ipsecesp_larval_timeout ipsecesp_param_arr[5].ipsecesp_param_value
1040Sstevel@tonic-gate #define	ipsecesp_default_soft_bytes \
1050Sstevel@tonic-gate 	ipsecesp_param_arr[6].ipsecesp_param_value
1060Sstevel@tonic-gate #define	ipsecesp_default_hard_bytes \
1070Sstevel@tonic-gate 	ipsecesp_param_arr[7].ipsecesp_param_value
1080Sstevel@tonic-gate #define	ipsecesp_default_soft_addtime \
1090Sstevel@tonic-gate 	ipsecesp_param_arr[8].ipsecesp_param_value
1100Sstevel@tonic-gate #define	ipsecesp_default_hard_addtime \
1110Sstevel@tonic-gate 	ipsecesp_param_arr[9].ipsecesp_param_value
1120Sstevel@tonic-gate #define	ipsecesp_default_soft_usetime \
1130Sstevel@tonic-gate 	ipsecesp_param_arr[10].ipsecesp_param_value
1140Sstevel@tonic-gate #define	ipsecesp_default_hard_usetime \
1150Sstevel@tonic-gate 	ipsecesp_param_arr[11].ipsecesp_param_value
1160Sstevel@tonic-gate #define	ipsecesp_log_unknown_spi \
1170Sstevel@tonic-gate 	ipsecesp_param_arr[12].ipsecesp_param_value
1180Sstevel@tonic-gate #define	ipsecesp_padding_check \
1190Sstevel@tonic-gate 	ipsecesp_param_arr[13].ipsecesp_param_value
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate #define	esp0dbg(a)	printf a
1220Sstevel@tonic-gate /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
1230Sstevel@tonic-gate #define	esp1dbg(a)	if (ipsecesp_debug != 0) printf a
1240Sstevel@tonic-gate #define	esp2dbg(a)	if (ipsecesp_debug > 1) printf a
1250Sstevel@tonic-gate #define	esp3dbg(a)	if (ipsecesp_debug > 2) printf a
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static IDP ipsecesp_g_nd;
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 *);
1330Sstevel@tonic-gate static void esp_send_acquire(ipsacq_t *, mblk_t *);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static ipsec_status_t esp_outbound_accelerated(mblk_t *, uint_t);
1360Sstevel@tonic-gate static ipsec_status_t esp_inbound_accelerated(mblk_t *, mblk_t *,
1370Sstevel@tonic-gate     boolean_t, ipsa_t *);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static boolean_t esp_register_out(uint32_t, uint32_t, uint_t);
1400Sstevel@tonic-gate static boolean_t esp_strip_header(mblk_t *, boolean_t, uint32_t,
1410Sstevel@tonic-gate     kstat_named_t **);
1420Sstevel@tonic-gate static ipsec_status_t esp_submit_req_inbound(mblk_t *, ipsa_t *, uint_t);
1430Sstevel@tonic-gate static ipsec_status_t esp_submit_req_outbound(mblk_t *, ipsa_t *, uchar_t *,
1440Sstevel@tonic-gate     uint_t);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate static struct module_info info = {
1470Sstevel@tonic-gate 	5137, "ipsecesp", 0, INFPSZ, 65536, 1024
1480Sstevel@tonic-gate };
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static struct qinit rinit = {
1510Sstevel@tonic-gate 	(pfi_t)ipsecesp_rput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info,
1520Sstevel@tonic-gate 	NULL
1530Sstevel@tonic-gate };
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static struct qinit winit = {
1560Sstevel@tonic-gate 	(pfi_t)ipsecesp_wput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info,
1570Sstevel@tonic-gate 	NULL
1580Sstevel@tonic-gate };
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate struct streamtab ipsecespinfo = {
1610Sstevel@tonic-gate 	&rinit, &winit, NULL, NULL
1620Sstevel@tonic-gate };
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * Keysock instance of ESP.  "There can be only one." :)
1660Sstevel@tonic-gate  * Use casptr() on this because I don't set it until KEYSOCK_HELLO comes down.
1670Sstevel@tonic-gate  * Paired up with the esp_pfkey_q is the esp_event, which will age SAs.
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate static queue_t *esp_pfkey_q;
1700Sstevel@tonic-gate static timeout_id_t esp_event;
1710Sstevel@tonic-gate static taskq_t *esp_taskq;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * OTOH, this one is set at open/close, and I'm D_MTQPAIR for now.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * Question:	Do I need this, given that all instance's esps->esps_wq point
1770Sstevel@tonic-gate  *		to IP?
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  * Answer:	Yes, because I need to know which queue is BOUND to
1800Sstevel@tonic-gate  *		IPPROTO_ESP
1810Sstevel@tonic-gate  */
1820Sstevel@tonic-gate static mblk_t *esp_ip_unbind;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * Stats.  This may eventually become a full-blown SNMP MIB once that spec
1860Sstevel@tonic-gate  * stabilizes.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate typedef struct {
1900Sstevel@tonic-gate 	kstat_named_t esp_stat_num_aalgs;
1910Sstevel@tonic-gate 	kstat_named_t esp_stat_good_auth;
1920Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_auth;
1930Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_padding;
1940Sstevel@tonic-gate 	kstat_named_t esp_stat_replay_failures;
1950Sstevel@tonic-gate 	kstat_named_t esp_stat_replay_early_failures;
1960Sstevel@tonic-gate 	kstat_named_t esp_stat_keysock_in;
1970Sstevel@tonic-gate 	kstat_named_t esp_stat_out_requests;
1980Sstevel@tonic-gate 	kstat_named_t esp_stat_acquire_requests;
1990Sstevel@tonic-gate 	kstat_named_t esp_stat_bytes_expired;
2000Sstevel@tonic-gate 	kstat_named_t esp_stat_out_discards;
2010Sstevel@tonic-gate 	kstat_named_t esp_stat_in_accelerated;
2020Sstevel@tonic-gate 	kstat_named_t esp_stat_out_accelerated;
2030Sstevel@tonic-gate 	kstat_named_t esp_stat_noaccel;
2040Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_sync;
2050Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_async;
2060Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_failures;
2070Sstevel@tonic-gate 	kstat_named_t esp_stat_num_ealgs;
2080Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_decrypt;
2090Sstevel@tonic-gate } esp_kstats_t;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate #define	ESP_BUMP_STAT(x) (esp_kstats->esp_stat_ ## x).value.ui64++
2120Sstevel@tonic-gate #define	ESP_DEBUMP_STAT(x) (esp_kstats->esp_stat_ ## x).value.ui64--
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate static kstat_t *esp_ksp;
2150Sstevel@tonic-gate static esp_kstats_t *esp_kstats;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static int	esp_kstat_update(kstat_t *, int);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static boolean_t
2200Sstevel@tonic-gate esp_kstat_init(void)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	esp_ksp = kstat_create("ipsecesp", 0, "esp_stat", "net",
2230Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, sizeof (*esp_kstats) / sizeof (kstat_named_t),
2240Sstevel@tonic-gate 	    KSTAT_FLAG_PERSISTENT);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (esp_ksp == NULL)
2270Sstevel@tonic-gate 		return (B_FALSE);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	esp_kstats = esp_ksp->ks_data;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	esp_ksp->ks_update = esp_kstat_update;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate #define	K64 KSTAT_DATA_UINT64
2340Sstevel@tonic-gate #define	KI(x) kstat_named_init(&(esp_kstats->esp_stat_##x), #x, K64)
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	KI(num_aalgs);
2370Sstevel@tonic-gate 	KI(num_ealgs);
2380Sstevel@tonic-gate 	KI(good_auth);
2390Sstevel@tonic-gate 	KI(bad_auth);
2400Sstevel@tonic-gate 	KI(bad_padding);
2410Sstevel@tonic-gate 	KI(replay_failures);
2420Sstevel@tonic-gate 	KI(replay_early_failures);
2430Sstevel@tonic-gate 	KI(keysock_in);
2440Sstevel@tonic-gate 	KI(out_requests);
2450Sstevel@tonic-gate 	KI(acquire_requests);
2460Sstevel@tonic-gate 	KI(bytes_expired);
2470Sstevel@tonic-gate 	KI(out_discards);
2480Sstevel@tonic-gate 	KI(in_accelerated);
2490Sstevel@tonic-gate 	KI(out_accelerated);
2500Sstevel@tonic-gate 	KI(noaccel);
2510Sstevel@tonic-gate 	KI(crypto_sync);
2520Sstevel@tonic-gate 	KI(crypto_async);
2530Sstevel@tonic-gate 	KI(crypto_failures);
2540Sstevel@tonic-gate 	KI(bad_decrypt);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate #undef KI
2570Sstevel@tonic-gate #undef K64
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	kstat_install(esp_ksp);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	return (B_TRUE);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate static int
2650Sstevel@tonic-gate esp_kstat_update(kstat_t *kp, int rw)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	esp_kstats_t *ekp;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if ((kp == NULL) || (kp->ks_data == NULL))
2700Sstevel@tonic-gate 		return (EIO);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
2730Sstevel@tonic-gate 		return (EACCES);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	ASSERT(kp == esp_ksp);
2760Sstevel@tonic-gate 	ekp = (esp_kstats_t *)kp->ks_data;
2770Sstevel@tonic-gate 	ASSERT(ekp == esp_kstats);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	mutex_enter(&alg_lock);
2800Sstevel@tonic-gate 	ekp->esp_stat_num_aalgs.value.ui64 = ipsec_nalgs[IPSEC_ALG_AUTH];
2810Sstevel@tonic-gate 	ekp->esp_stat_num_ealgs.value.ui64 = ipsec_nalgs[IPSEC_ALG_ENCR];
2820Sstevel@tonic-gate 	mutex_exit(&alg_lock);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	return (0);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate #ifdef DEBUG
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Debug routine, useful to see pre-encryption data.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate static char *
2920Sstevel@tonic-gate dump_msg(mblk_t *mp)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	char tmp_str[3], tmp_line[256];
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	while (mp != NULL) {
2970Sstevel@tonic-gate 		unsigned char *ptr;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		printf("mblk address 0x%p, length %ld, db_ref %d "
3000Sstevel@tonic-gate 		    "type %d, base 0x%p, lim 0x%p\n",
3010Sstevel@tonic-gate 		    (void *) mp, (long)(mp->b_wptr - mp->b_rptr),
3020Sstevel@tonic-gate 		    mp->b_datap->db_ref, mp->b_datap->db_type,
3030Sstevel@tonic-gate 		    (void *)mp->b_datap->db_base, (void *)mp->b_datap->db_lim);
3040Sstevel@tonic-gate 		ptr = mp->b_rptr;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 		tmp_line[0] = '\0';
3070Sstevel@tonic-gate 		while (ptr < mp->b_wptr) {
3080Sstevel@tonic-gate 			uint_t diff;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 			diff = (ptr - mp->b_rptr);
3110Sstevel@tonic-gate 			if (!(diff & 0x1f)) {
3120Sstevel@tonic-gate 				if (strlen(tmp_line) > 0) {
3130Sstevel@tonic-gate 					printf("bytes: %s\n", tmp_line);
3140Sstevel@tonic-gate 					tmp_line[0] = '\0';
3150Sstevel@tonic-gate 				}
3160Sstevel@tonic-gate 			}
3170Sstevel@tonic-gate 			if (!(diff & 0x3))
3180Sstevel@tonic-gate 				(void) strcat(tmp_line, " ");
3190Sstevel@tonic-gate 			(void) sprintf(tmp_str, "%02x", *ptr);
3200Sstevel@tonic-gate 			(void) strcat(tmp_line, tmp_str);
3210Sstevel@tonic-gate 			ptr++;
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 		if (strlen(tmp_line) > 0)
3240Sstevel@tonic-gate 			printf("bytes: %s\n", tmp_line);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 		mp = mp->b_cont;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	return ("\n");
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate #else /* DEBUG */
3330Sstevel@tonic-gate static char *
3340Sstevel@tonic-gate dump_msg(mblk_t *mp)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	printf("Find value of mp %p.\n", mp);
3370Sstevel@tonic-gate 	return ("\n");
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate #endif /* DEBUG */
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * Don't have to lock age_interval, as only one thread will access it at
3430Sstevel@tonic-gate  * a time, because I control the one function that does with timeout().
3440Sstevel@tonic-gate  */
3450Sstevel@tonic-gate /* ARGSUSED */
3460Sstevel@tonic-gate static void
3470Sstevel@tonic-gate esp_ager(void *ignoreme)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	hrtime_t begin = gethrtime();
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	sadb_ager(&esp_sadb.s_v4, esp_pfkey_q, esp_sadb.s_ip_q,
3520Sstevel@tonic-gate 	    ipsecesp_reap_delay);
3530Sstevel@tonic-gate 	sadb_ager(&esp_sadb.s_v6, esp_pfkey_q, esp_sadb.s_ip_q,
3540Sstevel@tonic-gate 	    ipsecesp_reap_delay);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	esp_event = sadb_retimeout(begin, esp_pfkey_q, esp_ager,
3570Sstevel@tonic-gate 	    &(ipsecesp_age_interval), ipsecesp_age_int_max, info.mi_idnum);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
3610Sstevel@tonic-gate  * Get an ESP NDD parameter.
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate /* ARGSUSED */
3640Sstevel@tonic-gate static int
3650Sstevel@tonic-gate ipsecesp_param_get(q, mp, cp, cr)
3660Sstevel@tonic-gate 	queue_t	*q;
3670Sstevel@tonic-gate 	mblk_t	*mp;
3680Sstevel@tonic-gate 	caddr_t	cp;
3690Sstevel@tonic-gate 	cred_t *cr;
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	ipsecespparam_t	*ipsecesppa = (ipsecespparam_t *)cp;
3720Sstevel@tonic-gate 	uint_t value;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	mutex_enter(&ipsecesp_param_lock);
3750Sstevel@tonic-gate 	value = ipsecesppa->ipsecesp_param_value;
3760Sstevel@tonic-gate 	mutex_exit(&ipsecesp_param_lock);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
3790Sstevel@tonic-gate 	return (0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  * This routine sets an NDD variable in a ipsecespparam_t structure.
3840Sstevel@tonic-gate  */
3850Sstevel@tonic-gate /* ARGSUSED */
3860Sstevel@tonic-gate static int
3870Sstevel@tonic-gate ipsecesp_param_set(q, mp, value, cp, cr)
3880Sstevel@tonic-gate 	queue_t	*q;
3890Sstevel@tonic-gate 	mblk_t	*mp;
3900Sstevel@tonic-gate 	char	*value;
3910Sstevel@tonic-gate 	caddr_t	cp;
3920Sstevel@tonic-gate 	cred_t *cr;
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	ulong_t	new_value;
3950Sstevel@tonic-gate 	ipsecespparam_t	*ipsecesppa = (ipsecespparam_t *)cp;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
3990Sstevel@tonic-gate 	 * required bounds.
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
4020Sstevel@tonic-gate 	    new_value < ipsecesppa->ipsecesp_param_min ||
4030Sstevel@tonic-gate 	    new_value > ipsecesppa->ipsecesp_param_max) {
4040Sstevel@tonic-gate 		return (EINVAL);
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/* Set the new value */
4080Sstevel@tonic-gate 	mutex_enter(&ipsecesp_param_lock);
4090Sstevel@tonic-gate 	ipsecesppa->ipsecesp_param_value = new_value;
4100Sstevel@tonic-gate 	mutex_exit(&ipsecesp_param_lock);
4110Sstevel@tonic-gate 	return (0);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * Using lifetime NDD variables, fill in an extended combination's
4160Sstevel@tonic-gate  * lifetime information.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate void
4190Sstevel@tonic-gate ipsecesp_fill_defs(sadb_x_ecomb_t *ecomb)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	ecomb->sadb_x_ecomb_soft_bytes = ipsecesp_default_soft_bytes;
4220Sstevel@tonic-gate 	ecomb->sadb_x_ecomb_hard_bytes = ipsecesp_default_hard_bytes;
4230Sstevel@tonic-gate 	ecomb->sadb_x_ecomb_soft_addtime = ipsecesp_default_soft_addtime;
4240Sstevel@tonic-gate 	ecomb->sadb_x_ecomb_hard_addtime = ipsecesp_default_hard_addtime;
4250Sstevel@tonic-gate 	ecomb->sadb_x_ecomb_soft_usetime = ipsecesp_default_soft_usetime;
4260Sstevel@tonic-gate 	ecomb->sadb_x_ecomb_hard_usetime = ipsecesp_default_hard_usetime;
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  * Initialize things for ESP at module load time.
4310Sstevel@tonic-gate  */
4320Sstevel@tonic-gate boolean_t
4330Sstevel@tonic-gate ipsecesp_ddi_init(void)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	int count;
4360Sstevel@tonic-gate 	ipsecespparam_t *espp = ipsecesp_param_arr;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	for (count = A_CNT(ipsecesp_param_arr); count-- > 0; espp++) {
4390Sstevel@tonic-gate 		if (espp->ipsecesp_param_name != NULL &&
4400Sstevel@tonic-gate 		    espp->ipsecesp_param_name[0]) {
4410Sstevel@tonic-gate 			if (!nd_load(&ipsecesp_g_nd, espp->ipsecesp_param_name,
4420Sstevel@tonic-gate 			    ipsecesp_param_get, ipsecesp_param_set,
4430Sstevel@tonic-gate 			    (caddr_t)espp)) {
4440Sstevel@tonic-gate 				nd_free(&ipsecesp_g_nd);
4450Sstevel@tonic-gate 				return (B_FALSE);
4460Sstevel@tonic-gate 			}
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	if (!esp_kstat_init()) {
4510Sstevel@tonic-gate 		nd_free(&ipsecesp_g_nd);
4520Sstevel@tonic-gate 		return (B_FALSE);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	esp_sadb.s_acquire_timeout = &ipsecesp_acquire_timeout;
4560Sstevel@tonic-gate 	esp_sadb.s_acqfn = esp_send_acquire;
4570Sstevel@tonic-gate 	sadbp_init(&esp_sadb, SADB_SATYPE_ESP);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	esp_taskq = taskq_create("esp_taskq", 1, minclsyspri,
4600Sstevel@tonic-gate 	    IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	mutex_init(&ipsecesp_param_lock, NULL, MUTEX_DEFAULT, 0);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	ip_drop_register(&esp_dropper, "IPsec ESP");
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	return (B_TRUE);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate /*
4700Sstevel@tonic-gate  * Destroy things for ESP at module unload time.
4710Sstevel@tonic-gate  */
4720Sstevel@tonic-gate void
4730Sstevel@tonic-gate ipsecesp_ddi_destroy(void)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	esp1dbg(("In ipsecesp_ddi_destroy.\n"));
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	sadbp_destroy(&esp_sadb);
4780Sstevel@tonic-gate 	ip_drop_unregister(&esp_dropper);
4790Sstevel@tonic-gate 	taskq_destroy(esp_taskq);
4800Sstevel@tonic-gate 	mutex_destroy(&ipsecesp_param_lock);
4810Sstevel@tonic-gate 	nd_free(&ipsecesp_g_nd);
4820Sstevel@tonic-gate 	kstat_delete(esp_ksp);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate  * ESP module open routine.
4870Sstevel@tonic-gate  */
4880Sstevel@tonic-gate /* ARGSUSED */
4890Sstevel@tonic-gate static int
4900Sstevel@tonic-gate ipsecesp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	if (secpolicy_net_config(credp, B_FALSE) != 0) {
4930Sstevel@tonic-gate 		esp1dbg(("Non-privileged user trying to open ipsecesp.\n"));
4940Sstevel@tonic-gate 		return (EPERM);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (q->q_ptr != NULL)
4980Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	if (sflag != MODOPEN)
5010Sstevel@tonic-gate 		return (EINVAL);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/*
5040Sstevel@tonic-gate 	 * ASSUMPTIONS (because I'm MT_OCEXCL):
5050Sstevel@tonic-gate 	 *
5060Sstevel@tonic-gate 	 *	* I'm being pushed on top of IP for all my opens (incl. #1).
5070Sstevel@tonic-gate 	 *	* Only ipsecesp_open() can write into esp_sadb.s_ip_q.
5080Sstevel@tonic-gate 	 *	* Because of this, I can check lazily for esp_sadb.s_ip_q.
5090Sstevel@tonic-gate 	 *
5100Sstevel@tonic-gate 	 *  If these assumptions are wrong, I'm in BIG trouble...
5110Sstevel@tonic-gate 	 */
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	q->q_ptr = q; /* just so I know I'm open */
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (esp_sadb.s_ip_q == NULL) {
5160Sstevel@tonic-gate 		struct T_unbind_req *tur;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		esp_sadb.s_ip_q = WR(q);
5190Sstevel@tonic-gate 		/* Allocate an unbind... */
5200Sstevel@tonic-gate 		esp_ip_unbind = allocb(sizeof (struct T_unbind_req), BPRI_HI);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 		/*
5230Sstevel@tonic-gate 		 * Send down T_BIND_REQ to bind IPPROTO_ESP.
5240Sstevel@tonic-gate 		 * Handle the ACK here in ESP.
5250Sstevel@tonic-gate 		 */
5260Sstevel@tonic-gate 		qprocson(q);
5270Sstevel@tonic-gate 		if (esp_ip_unbind == NULL ||
5280Sstevel@tonic-gate 		    !sadb_t_bind_req(esp_sadb.s_ip_q, IPPROTO_ESP)) {
5290Sstevel@tonic-gate 			if (esp_ip_unbind != NULL) {
5300Sstevel@tonic-gate 				freeb(esp_ip_unbind);
5310Sstevel@tonic-gate 				esp_ip_unbind = NULL;
5320Sstevel@tonic-gate 			}
5330Sstevel@tonic-gate 			q->q_ptr = NULL;
5340Sstevel@tonic-gate 			return (ENOMEM);
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		esp_ip_unbind->b_datap->db_type = M_PROTO;
5380Sstevel@tonic-gate 		tur = (struct T_unbind_req *)esp_ip_unbind->b_rptr;
5390Sstevel@tonic-gate 		tur->PRIM_type = T_UNBIND_REQ;
5400Sstevel@tonic-gate 	} else {
5410Sstevel@tonic-gate 		qprocson(q);
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/*
5450Sstevel@tonic-gate 	 * For now, there's not much I can do.  I'll be getting a message
5460Sstevel@tonic-gate 	 * passed down to me from keysock (in my wput), and a T_BIND_ACK
5470Sstevel@tonic-gate 	 * up from IP (in my rput).
5480Sstevel@tonic-gate 	 */
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	return (0);
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate  * ESP module close routine.
5550Sstevel@tonic-gate  */
5560Sstevel@tonic-gate static int
5570Sstevel@tonic-gate ipsecesp_close(queue_t *q)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	/*
5600Sstevel@tonic-gate 	 * If esp_sadb.s_ip_q is attached to this instance, send a
5610Sstevel@tonic-gate 	 * T_UNBIND_REQ to IP for the instance before doing
5620Sstevel@tonic-gate 	 * a qprocsoff().
5630Sstevel@tonic-gate 	 */
5640Sstevel@tonic-gate 	if (WR(q) == esp_sadb.s_ip_q && esp_ip_unbind != NULL) {
5650Sstevel@tonic-gate 		putnext(WR(q), esp_ip_unbind);
5660Sstevel@tonic-gate 		esp_ip_unbind = NULL;
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * Clean up q_ptr, if needed.
5710Sstevel@tonic-gate 	 */
5720Sstevel@tonic-gate 	qprocsoff(q);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	/* Keysock queue check is safe, because of OCEXCL perimeter. */
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (q == esp_pfkey_q) {
5770Sstevel@tonic-gate 		esp0dbg(("ipsecesp_close:  Ummm... keysock is closing ESP.\n"));
5780Sstevel@tonic-gate 		esp_pfkey_q = NULL;
5790Sstevel@tonic-gate 		/* Detach qtimeouts. */
5800Sstevel@tonic-gate 		(void) quntimeout(q, esp_event);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if (WR(q) == esp_sadb.s_ip_q) {
5840Sstevel@tonic-gate 		/*
5850Sstevel@tonic-gate 		 * If the esp_sadb.s_ip_q is attached to this instance, find
5860Sstevel@tonic-gate 		 * another.  The OCEXCL outer perimeter helps us here.
5870Sstevel@tonic-gate 		 */
5880Sstevel@tonic-gate 		esp_sadb.s_ip_q = NULL;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		/*
5910Sstevel@tonic-gate 		 * Find a replacement queue for esp_sadb.s_ip_q.
5920Sstevel@tonic-gate 		 */
5930Sstevel@tonic-gate 		if (esp_pfkey_q != NULL && esp_pfkey_q != RD(q)) {
5940Sstevel@tonic-gate 			/*
5950Sstevel@tonic-gate 			 * See if we can use the pfkey_q.
5960Sstevel@tonic-gate 			 */
5970Sstevel@tonic-gate 			esp_sadb.s_ip_q = WR(esp_pfkey_q);
5980Sstevel@tonic-gate 		}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 		if (esp_sadb.s_ip_q == NULL ||
6010Sstevel@tonic-gate 		    !sadb_t_bind_req(esp_sadb.s_ip_q, IPPROTO_ESP)) {
6020Sstevel@tonic-gate 			esp1dbg(("ipsecesp: Can't reassign ip_q.\n"));
6030Sstevel@tonic-gate 			esp_sadb.s_ip_q = NULL;
6040Sstevel@tonic-gate 		} else {
6050Sstevel@tonic-gate 			esp_ip_unbind = allocb(sizeof (struct T_unbind_req),
6060Sstevel@tonic-gate 			    BPRI_HI);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 			if (esp_ip_unbind != NULL) {
6090Sstevel@tonic-gate 				struct T_unbind_req *tur;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 				esp_ip_unbind->b_datap->db_type = M_PROTO;
6120Sstevel@tonic-gate 				tur = (struct T_unbind_req *)
6130Sstevel@tonic-gate 				    esp_ip_unbind->b_rptr;
6140Sstevel@tonic-gate 				tur->PRIM_type = T_UNBIND_REQ;
6150Sstevel@tonic-gate 			}
6160Sstevel@tonic-gate 			/* If it's NULL, I can't do much here. */
6170Sstevel@tonic-gate 		}
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	return (0);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * Add a number of bytes to what the SA has protected so far.  Return
6250Sstevel@tonic-gate  * B_TRUE if the SA can still protect that many bytes.
6260Sstevel@tonic-gate  *
6270Sstevel@tonic-gate  * Caller must REFRELE the passed-in assoc.  This function must REFRELE
6280Sstevel@tonic-gate  * any obtained peer SA.
6290Sstevel@tonic-gate  */
6300Sstevel@tonic-gate static boolean_t
6310Sstevel@tonic-gate esp_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
6320Sstevel@tonic-gate {
6330Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
6340Sstevel@tonic-gate 	isaf_t *bucket;
6350Sstevel@tonic-gate 	boolean_t inrc, outrc, isv6;
6360Sstevel@tonic-gate 	sadb_t *sp;
6370Sstevel@tonic-gate 	int outhash;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	/* No peer?  No problem! */
6400Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
6410Sstevel@tonic-gate 		return (sadb_age_bytes(esp_pfkey_q, assoc, bytes,
6420Sstevel@tonic-gate 		    B_TRUE));
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	/*
6460Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
6470Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, two
6480Sstevel@tonic-gate 	 * expire messages may occur.  We limit this by only sending the
6490Sstevel@tonic-gate 	 * expire message on one of the peers, we'll pick the inbound
6500Sstevel@tonic-gate 	 * arbitrarily.
6510Sstevel@tonic-gate 	 *
6520Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
6530Sstevel@tonic-gate 	 * reconsider.
6540Sstevel@tonic-gate 	 */
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	/* Use address length to select IPv6/IPv4 */
6570Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
6580Sstevel@tonic-gate 	sp = isv6 ? &esp_sadb.s_v6 : &esp_sadb.s_v4;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	if (inbound) {
6610Sstevel@tonic-gate 		inassoc = assoc;
6620Sstevel@tonic-gate 		if (isv6) {
6630Sstevel@tonic-gate 			outhash = OUTBOUND_HASH_V6(*((in6_addr_t *)
6640Sstevel@tonic-gate 			    &inassoc->ipsa_dstaddr));
6650Sstevel@tonic-gate 		} else {
6660Sstevel@tonic-gate 			outhash = OUTBOUND_HASH_V4(*((ipaddr_t *)
6670Sstevel@tonic-gate 				&inassoc->ipsa_dstaddr));
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
6700Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
6710Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
6720Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
6730Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
6740Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
6750Sstevel@tonic-gate 		if (outassoc == NULL) {
6760Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
6770Sstevel@tonic-gate 			esp0dbg(("esp_age_bytes: "
6780Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
6790Sstevel@tonic-gate 			return (sadb_age_bytes(esp_pfkey_q, inassoc,
6800Sstevel@tonic-gate 			    bytes, B_TRUE));
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 	} else {
6830Sstevel@tonic-gate 		outassoc = assoc;
6840Sstevel@tonic-gate 		bucket = &sp->sdb_if[INBOUND_HASH(outassoc->ipsa_spi)];
6850Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
6860Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
6870Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
6880Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
6890Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
6900Sstevel@tonic-gate 		if (inassoc == NULL) {
6910Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
6920Sstevel@tonic-gate 			esp0dbg(("esp_age_bytes: "
6930Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
6940Sstevel@tonic-gate 			return (sadb_age_bytes(esp_pfkey_q, outassoc,
6950Sstevel@tonic-gate 			    bytes, B_TRUE));
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	inrc = sadb_age_bytes(esp_pfkey_q, inassoc, bytes, B_TRUE);
7000Sstevel@tonic-gate 	outrc = sadb_age_bytes(esp_pfkey_q, outassoc, bytes, B_FALSE);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * REFRELE any peer SA.
7040Sstevel@tonic-gate 	 *
7050Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
7060Sstevel@tonic-gate 	 * them in { }.
7070Sstevel@tonic-gate 	 */
7080Sstevel@tonic-gate 	if (inbound) {
7090Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
7100Sstevel@tonic-gate 	} else {
7110Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	return (inrc && outrc);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate /*
7180Sstevel@tonic-gate  * Do incoming NAT-T manipulations for packet.
7190Sstevel@tonic-gate  */
7200Sstevel@tonic-gate static ipsec_status_t
7210Sstevel@tonic-gate esp_fix_natt_checksums(mblk_t *data_mp, ipsa_t *assoc)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
7240Sstevel@tonic-gate 	tcpha_t *tcph;
7250Sstevel@tonic-gate 	udpha_t *udpha;
7260Sstevel@tonic-gate 	/* Initialize to our inbound cksum adjustment... */
7270Sstevel@tonic-gate 	uint32_t sum = assoc->ipsa_inbound_cksum;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	switch (ipha->ipha_protocol) {
7300Sstevel@tonic-gate 	case IPPROTO_TCP:
7310Sstevel@tonic-gate 		tcph = (tcpha_t *)(data_mp->b_rptr +
7320Sstevel@tonic-gate 		    IPH_HDR_LENGTH(ipha));
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate #define	DOWN_SUM(x) (x) = ((x) & 0xFFFF) +	 ((x) >> 16)
7350Sstevel@tonic-gate 		sum += ~ntohs(tcph->tha_sum) & 0xFFFF;
7360Sstevel@tonic-gate 		DOWN_SUM(sum);
7370Sstevel@tonic-gate 		DOWN_SUM(sum);
7380Sstevel@tonic-gate 		tcph->tha_sum = ~htons(sum);
7390Sstevel@tonic-gate 		break;
7400Sstevel@tonic-gate 	case IPPROTO_UDP:
7410Sstevel@tonic-gate 		udpha = (udpha_t *)(data_mp->b_rptr + IPH_HDR_LENGTH(ipha));
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		if (udpha->uha_checksum != 0) {
7440Sstevel@tonic-gate 			/* Adujst if the inbound one was not zero. */
7450Sstevel@tonic-gate 			sum += ~ntohs(udpha->uha_checksum) & 0xFFFF;
7460Sstevel@tonic-gate 			DOWN_SUM(sum);
7470Sstevel@tonic-gate 			DOWN_SUM(sum);
7480Sstevel@tonic-gate 			udpha->uha_checksum = ~htons(sum);
7490Sstevel@tonic-gate 			if (udpha->uha_checksum == 0)
7500Sstevel@tonic-gate 				udpha->uha_checksum = 0xFFFF;
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate #undef DOWN_SUM
7530Sstevel@tonic-gate 		break;
7540Sstevel@tonic-gate 	case IPPROTO_IP:
7550Sstevel@tonic-gate 		/*
7560Sstevel@tonic-gate 		 * This case is only an issue for self-encapsulated
7570Sstevel@tonic-gate 		 * packets.  So for now, fall through.
7580Sstevel@tonic-gate 		 */
7590Sstevel@tonic-gate 		break;
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * Strip ESP header and fix IP header
7670Sstevel@tonic-gate  * Returns B_TRUE on success, B_FALSE if an error occured.
7680Sstevel@tonic-gate  */
7690Sstevel@tonic-gate static boolean_t
7700Sstevel@tonic-gate esp_strip_header(mblk_t *data_mp, boolean_t isv4, uint32_t ivlen,
7710Sstevel@tonic-gate     kstat_named_t **counter)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	ipha_t *ipha;
7740Sstevel@tonic-gate 	ip6_t *ip6h;
7750Sstevel@tonic-gate 	uint_t divpoint;
7760Sstevel@tonic-gate 	mblk_t *scratch;
7770Sstevel@tonic-gate 	uint8_t nexthdr, padlen;
7780Sstevel@tonic-gate 	uint8_t lastpad;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/*
7810Sstevel@tonic-gate 	 * Strip ESP data and fix IP header.
7820Sstevel@tonic-gate 	 *
7830Sstevel@tonic-gate 	 * XXX In case the beginning of esp_inbound() changes to not do a
7840Sstevel@tonic-gate 	 * pullup, this part of the code can remain unchanged.
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	if (isv4) {
7870Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ipha_t));
7880Sstevel@tonic-gate 		ipha = (ipha_t *)data_mp->b_rptr;
7890Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (esph_t) +
7900Sstevel@tonic-gate 		    IPH_HDR_LENGTH(ipha));
7910Sstevel@tonic-gate 		divpoint = IPH_HDR_LENGTH(ipha);
7920Sstevel@tonic-gate 	} else {
7930Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ip6_t));
7940Sstevel@tonic-gate 		ip6h = (ip6_t *)data_mp->b_rptr;
7950Sstevel@tonic-gate 		divpoint = ip_hdr_length_v6(data_mp, ip6h);
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	scratch = data_mp;
7990Sstevel@tonic-gate 	while (scratch->b_cont != NULL)
8000Sstevel@tonic-gate 		scratch = scratch->b_cont;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	ASSERT((scratch->b_wptr - scratch->b_rptr) >= 3);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * "Next header" and padding length are the last two bytes in the
8060Sstevel@tonic-gate 	 * ESP-protected datagram, thus the explicit - 1 and - 2.
8070Sstevel@tonic-gate 	 * lastpad is the last byte of the padding, which can be used for
8080Sstevel@tonic-gate 	 * a quick check to see if the padding is correct.
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	nexthdr = *(scratch->b_wptr - 1);
8110Sstevel@tonic-gate 	padlen = *(scratch->b_wptr - 2);
8120Sstevel@tonic-gate 	lastpad = *(scratch->b_wptr - 3);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	if (isv4) {
8150Sstevel@tonic-gate 		/* Fix part of the IP header. */
8160Sstevel@tonic-gate 		ipha->ipha_protocol = nexthdr;
8170Sstevel@tonic-gate 		/*
8180Sstevel@tonic-gate 		 * Reality check the padlen.  The explicit - 2 is for the
8190Sstevel@tonic-gate 		 * padding length and the next-header bytes.
8200Sstevel@tonic-gate 		 */
8210Sstevel@tonic-gate 		if (padlen >= ntohs(ipha->ipha_length) - sizeof (ipha_t) - 2 -
8220Sstevel@tonic-gate 		    sizeof (esph_t) - ivlen) {
8230Sstevel@tonic-gate 			ESP_BUMP_STAT(bad_decrypt);
8240Sstevel@tonic-gate 			ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
8250Sstevel@tonic-gate 			    "Possibly corrupt ESP packet.");
8260Sstevel@tonic-gate 			esp1dbg(("padlen (%d) is greater than:\n", padlen));
8270Sstevel@tonic-gate 			esp1dbg(("pkt len(%d) - ip hdr - esp hdr - ivlen(%d) "
8280Sstevel@tonic-gate 			    "= %d.\n", ntohs(ipha->ipha_length), ivlen,
8290Sstevel@tonic-gate 			    (int)(ntohs(ipha->ipha_length) - sizeof (ipha_t) -
8300Sstevel@tonic-gate 				2 - sizeof (esph_t) - ivlen)));
8310Sstevel@tonic-gate 			*counter = &ipdrops_esp_bad_padlen;
8320Sstevel@tonic-gate 			return (B_FALSE);
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		/*
8360Sstevel@tonic-gate 		 * Fix the rest of the header.  The explicit - 2 is for the
8370Sstevel@tonic-gate 		 * padding length and the next-header bytes.
8380Sstevel@tonic-gate 		 */
8390Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) - padlen -
8400Sstevel@tonic-gate 		    2 - sizeof (esph_t) - ivlen);
8410Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = 0;
8420Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
8430Sstevel@tonic-gate 	} else {
8440Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_ESP) {
8450Sstevel@tonic-gate 			ip6h->ip6_nxt = nexthdr;
8460Sstevel@tonic-gate 		} else {
8470Sstevel@tonic-gate 			ip6_pkt_t ipp;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 			bzero(&ipp, sizeof (ipp));
8500Sstevel@tonic-gate 			(void) ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL);
8510Sstevel@tonic-gate 			if (ipp.ipp_dstopts != NULL) {
8520Sstevel@tonic-gate 				ipp.ipp_dstopts->ip6d_nxt = nexthdr;
8530Sstevel@tonic-gate 			} else if (ipp.ipp_rthdr != NULL) {
8540Sstevel@tonic-gate 				ipp.ipp_rthdr->ip6r_nxt = nexthdr;
8550Sstevel@tonic-gate 			} else if (ipp.ipp_hopopts != NULL) {
8560Sstevel@tonic-gate 				ipp.ipp_hopopts->ip6h_nxt = nexthdr;
8570Sstevel@tonic-gate 			} else {
8580Sstevel@tonic-gate 				/* Panic a DEBUG kernel. */
8590Sstevel@tonic-gate 				ASSERT(ipp.ipp_hopopts != NULL);
8600Sstevel@tonic-gate 				/* Otherwise, pretend it's IP + ESP. */
8610Sstevel@tonic-gate 				cmn_err(CE_WARN, "ESP IPv6 headers wrong.\n");
8620Sstevel@tonic-gate 				ip6h->ip6_nxt = nexthdr;
8630Sstevel@tonic-gate 			}
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		if (padlen >= ntohs(ip6h->ip6_plen) - 2 - sizeof (esph_t) -
8670Sstevel@tonic-gate 		    ivlen) {
8680Sstevel@tonic-gate 			ESP_BUMP_STAT(bad_decrypt);
8690Sstevel@tonic-gate 			ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
8700Sstevel@tonic-gate 			    "Possibly corrupt ESP packet.");
8710Sstevel@tonic-gate 			esp1dbg(("padlen (%d) is greater than:\n", padlen));
8720Sstevel@tonic-gate 			esp1dbg(("pkt len(%u) - ip hdr - esp hdr - ivlen(%d)"
8730Sstevel@tonic-gate 			    " = %u.\n", (unsigned)(ntohs(ip6h->ip6_plen)
8740Sstevel@tonic-gate 				+ sizeof (ip6_t)), ivlen,
8750Sstevel@tonic-gate 			    (unsigned)(ntohs(ip6h->ip6_plen) - 2 -
8760Sstevel@tonic-gate 				sizeof (esph_t) - ivlen)));
8770Sstevel@tonic-gate 			*counter = &ipdrops_esp_bad_padlen;
8780Sstevel@tonic-gate 			return (B_FALSE);
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 		/*
8830Sstevel@tonic-gate 		 * Fix the rest of the header.  The explicit - 2 is for the
8840Sstevel@tonic-gate 		 * padding length and the next-header bytes.  IPv6 is nice,
8850Sstevel@tonic-gate 		 * because there's no hdr checksum!
8860Sstevel@tonic-gate 		 */
8870Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - padlen -
8880Sstevel@tonic-gate 		    2 - sizeof (esph_t) - ivlen);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if (ipsecesp_padding_check > 0 &&
8920Sstevel@tonic-gate 		padlen != lastpad && padlen != 0) {
8930Sstevel@tonic-gate 		ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
8940Sstevel@tonic-gate 		    "Possibly corrupt ESP packet.");
8950Sstevel@tonic-gate 		esp1dbg(("lastpad (%d) not equal to padlen (%d):\n",
8960Sstevel@tonic-gate 		    lastpad, padlen));
8970Sstevel@tonic-gate 		ESP_BUMP_STAT(bad_padding);
8980Sstevel@tonic-gate 		*counter = &ipdrops_esp_bad_padding;
8990Sstevel@tonic-gate 		return (B_FALSE);
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if (ipsecesp_padding_check > 1) {
9030Sstevel@tonic-gate 		uint8_t *last = (uint8_t *)(scratch->b_wptr - 3);
9040Sstevel@tonic-gate 		uint8_t lastval = *last;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		/*
9070Sstevel@tonic-gate 		 * this assert may have to become an if
9080Sstevel@tonic-gate 		 * and a pullup if we start accepting
9090Sstevel@tonic-gate 		 * multi-dblk mblks. Any packet here will
9100Sstevel@tonic-gate 		 * have been pulled up in esp_inbound.
9110Sstevel@tonic-gate 		 */
9120Sstevel@tonic-gate 		ASSERT(MBLKL(scratch) >= lastval + 3);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 		while (lastval != 0) {
9150Sstevel@tonic-gate 			if (lastval != *last) {
9160Sstevel@tonic-gate 				ipsec_rl_strlog(info.mi_idnum, 0, 0,
9170Sstevel@tonic-gate 				    SL_ERROR | SL_WARN,
9180Sstevel@tonic-gate 				    "Possibly corrupt ESP packet.");
9190Sstevel@tonic-gate 				esp1dbg(("padding not in correct"
9200Sstevel@tonic-gate 				    " format:\n"));
9210Sstevel@tonic-gate 				ESP_BUMP_STAT(bad_padding);
9220Sstevel@tonic-gate 				*counter = &ipdrops_esp_bad_padding;
9230Sstevel@tonic-gate 				return (B_FALSE);
9240Sstevel@tonic-gate 			}
9250Sstevel@tonic-gate 			lastval--; last--;
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	/* Trim off the padding. */
9300Sstevel@tonic-gate 	ASSERT(data_mp->b_cont == NULL);
9310Sstevel@tonic-gate 	data_mp->b_wptr -= (padlen + 2);
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	/*
9340Sstevel@tonic-gate 	 * Remove the ESP header.
9350Sstevel@tonic-gate 	 *
9360Sstevel@tonic-gate 	 * The above assertions about data_mp's size will make this work.
9370Sstevel@tonic-gate 	 *
9380Sstevel@tonic-gate 	 * XXX  Question:  If I send up and get back a contiguous mblk,
9390Sstevel@tonic-gate 	 * would it be quicker to bcopy over, or keep doing the dupb stuff?
9400Sstevel@tonic-gate 	 * I go with copying for now.
9410Sstevel@tonic-gate 	 */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	if (IS_P2ALIGNED(data_mp->b_rptr, sizeof (uint32_t)) &&
9440Sstevel@tonic-gate 	    IS_P2ALIGNED(ivlen, sizeof (uint32_t))) {
9450Sstevel@tonic-gate 		uint8_t *start = data_mp->b_rptr;
9460Sstevel@tonic-gate 		uint32_t *src, *dst;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		src = (uint32_t *)(start + divpoint);
9490Sstevel@tonic-gate 		dst = (uint32_t *)(start + divpoint + sizeof (esph_t) + ivlen);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(dst, sizeof (uint32_t)) &&
9520Sstevel@tonic-gate 		    IS_P2ALIGNED(src, sizeof (uint32_t)));
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		do {
9550Sstevel@tonic-gate 			src--;
9560Sstevel@tonic-gate 			dst--;
9570Sstevel@tonic-gate 			*dst = *src;
9580Sstevel@tonic-gate 		} while (src != (uint32_t *)start);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 		data_mp->b_rptr = (uchar_t *)dst;
9610Sstevel@tonic-gate 	} else {
9620Sstevel@tonic-gate 		uint8_t *start = data_mp->b_rptr;
9630Sstevel@tonic-gate 		uint8_t *src, *dst;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		src = start + divpoint;
9660Sstevel@tonic-gate 		dst = src + sizeof (esph_t) + ivlen;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		do {
9690Sstevel@tonic-gate 			src--;
9700Sstevel@tonic-gate 			dst--;
9710Sstevel@tonic-gate 			*dst = *src;
9720Sstevel@tonic-gate 		} while (src != start);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		data_mp->b_rptr = dst;
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	esp2dbg(("data_mp after inbound ESP adjustment:\n"));
9780Sstevel@tonic-gate 	esp2dbg((dump_msg(data_mp)));
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	return (B_TRUE);
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate /*
9840Sstevel@tonic-gate  * Updating use times can be tricky business if the ipsa_haspeer flag is
9850Sstevel@tonic-gate  * set.  This function is called once in an SA's lifetime.
9860Sstevel@tonic-gate  *
9870Sstevel@tonic-gate  * Caller has to REFRELE "assoc" which is passed in.  This function has
9880Sstevel@tonic-gate  * to REFRELE any peer SA that is obtained.
9890Sstevel@tonic-gate  */
9900Sstevel@tonic-gate static void
9910Sstevel@tonic-gate esp_set_usetime(ipsa_t *assoc, boolean_t inbound)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
9940Sstevel@tonic-gate 	isaf_t *bucket;
9950Sstevel@tonic-gate 	sadb_t *sp;
9960Sstevel@tonic-gate 	int outhash;
9970Sstevel@tonic-gate 	boolean_t isv6;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	/* No peer?  No problem! */
10000Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
10010Sstevel@tonic-gate 		sadb_set_usetime(assoc);
10020Sstevel@tonic-gate 		return;
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	/*
10060Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
10070Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, the times
10080Sstevel@tonic-gate 	 * will be out-of-synch by at most a second, and since our time
10090Sstevel@tonic-gate 	 * granularity is a second, this won't be a problem.
10100Sstevel@tonic-gate 	 *
10110Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
10120Sstevel@tonic-gate 	 * reconsider.
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	/* Use address length to select IPv6/IPv4 */
10160Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
10170Sstevel@tonic-gate 	sp = isv6 ? &esp_sadb.s_v6 : &esp_sadb.s_v4;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	if (inbound) {
10200Sstevel@tonic-gate 		inassoc = assoc;
10210Sstevel@tonic-gate 		if (isv6) {
10220Sstevel@tonic-gate 			outhash = OUTBOUND_HASH_V6(*((in6_addr_t *)
10230Sstevel@tonic-gate 			    &inassoc->ipsa_dstaddr));
10240Sstevel@tonic-gate 		} else {
10250Sstevel@tonic-gate 			outhash = OUTBOUND_HASH_V4(*((ipaddr_t *)
10260Sstevel@tonic-gate 				&inassoc->ipsa_dstaddr));
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
10290Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
10300Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
10310Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
10320Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
10330Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
10340Sstevel@tonic-gate 		if (outassoc == NULL) {
10350Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
10360Sstevel@tonic-gate 			esp0dbg(("esp_set_usetime: "
10370Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
10380Sstevel@tonic-gate 			sadb_set_usetime(inassoc);
10390Sstevel@tonic-gate 			return;
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate 	} else {
10420Sstevel@tonic-gate 		outassoc = assoc;
10430Sstevel@tonic-gate 		bucket = &sp->sdb_if[INBOUND_HASH(outassoc->ipsa_spi)];
10440Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
10450Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
10460Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
10470Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
10480Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
10490Sstevel@tonic-gate 		if (inassoc == NULL) {
10500Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
10510Sstevel@tonic-gate 			esp0dbg(("esp_set_usetime: "
10520Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
10530Sstevel@tonic-gate 			sadb_set_usetime(outassoc);
10540Sstevel@tonic-gate 			return;
10550Sstevel@tonic-gate 		}
10560Sstevel@tonic-gate 	}
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	/* Update usetime on both. */
10590Sstevel@tonic-gate 	sadb_set_usetime(inassoc);
10600Sstevel@tonic-gate 	sadb_set_usetime(outassoc);
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	/*
10630Sstevel@tonic-gate 	 * REFRELE any peer SA.
10640Sstevel@tonic-gate 	 *
10650Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
10660Sstevel@tonic-gate 	 * them in { }.
10670Sstevel@tonic-gate 	 */
10680Sstevel@tonic-gate 	if (inbound) {
10690Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
10700Sstevel@tonic-gate 	} else {
10710Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate /*
10760Sstevel@tonic-gate  * Handle ESP inbound data for IPv4 and IPv6.
10770Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
10780Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
10790Sstevel@tonic-gate  */
10800Sstevel@tonic-gate ipsec_status_t
10810Sstevel@tonic-gate esp_inbound(mblk_t *ipsec_in_mp, void *arg)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	mblk_t *data_mp = ipsec_in_mp->b_cont;
10840Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
10850Sstevel@tonic-gate 	esph_t *esph = (esph_t *)arg;
10860Sstevel@tonic-gate 	ipsa_t *ipsa = ii->ipsec_in_esp_sa;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	if (ipsa->ipsa_usetime == 0)
10890Sstevel@tonic-gate 		esp_set_usetime(ipsa, B_TRUE);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	/*
10920Sstevel@tonic-gate 	 * We may wish to check replay in-range-only here as an optimization.
10930Sstevel@tonic-gate 	 * Include the reality check of ipsa->ipsa_replay >
10940Sstevel@tonic-gate 	 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
10950Sstevel@tonic-gate 	 * where N == ipsa->ipsa_replay_wsize.
10960Sstevel@tonic-gate 	 *
10970Sstevel@tonic-gate 	 * Another check that may come here later is the "collision" check.
10980Sstevel@tonic-gate 	 * If legitimate packets flow quickly enough, this won't be a problem,
10990Sstevel@tonic-gate 	 * but collisions may cause authentication algorithm crunching to
11000Sstevel@tonic-gate 	 * take place when it doesn't need to.
11010Sstevel@tonic-gate 	 */
11020Sstevel@tonic-gate 	if (!sadb_replay_peek(ipsa, esph->esph_replay)) {
11030Sstevel@tonic-gate 		ESP_BUMP_STAT(replay_early_failures);
11040Sstevel@tonic-gate 		IP_ESP_BUMP_STAT(in_discards);
11050Sstevel@tonic-gate 		/*
11060Sstevel@tonic-gate 		 * TODO: Extract inbound interface from the IPSEC_IN
11070Sstevel@tonic-gate 		 * message's ii->ipsec_in_rill_index.
11080Sstevel@tonic-gate 		 */
11090Sstevel@tonic-gate 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
11100Sstevel@tonic-gate 		    &ipdrops_esp_early_replay, &esp_dropper);
11110Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	/*
11150Sstevel@tonic-gate 	 * Has this packet already been processed by a hardware
11160Sstevel@tonic-gate 	 * IPsec accelerator?
11170Sstevel@tonic-gate 	 */
11180Sstevel@tonic-gate 	if (ii->ipsec_in_accelerated) {
11190Sstevel@tonic-gate 		ipsec_status_t rv;
11200Sstevel@tonic-gate 		esp3dbg(("esp_inbound: pkt processed by ill=%d isv6=%d\n",
11210Sstevel@tonic-gate 		    ii->ipsec_in_ill_index, !ii->ipsec_in_v4));
11220Sstevel@tonic-gate 		rv = esp_inbound_accelerated(ipsec_in_mp,
11230Sstevel@tonic-gate 		    data_mp, ii->ipsec_in_v4, ipsa);
11240Sstevel@tonic-gate 		return (rv);
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 	ESP_BUMP_STAT(noaccel);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * Adjust the IP header's payload length to reflect the removal
11300Sstevel@tonic-gate 	 * of the ICV.
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 	if (!ii->ipsec_in_v4) {
11330Sstevel@tonic-gate 		ip6_t *ip6h = (ip6_t *)data_mp->b_rptr;
11340Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) -
11350Sstevel@tonic-gate 		    ipsa->ipsa_mac_len);
11360Sstevel@tonic-gate 	} else {
11370Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
11380Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) -
11390Sstevel@tonic-gate 		    ipsa->ipsa_mac_len);
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	/* submit the request to the crypto framework */
11430Sstevel@tonic-gate 	return (esp_submit_req_inbound(ipsec_in_mp, ipsa,
11440Sstevel@tonic-gate 	    (uint8_t *)esph - data_mp->b_rptr));
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * Perform the really difficult work of inserting the proposed situation.
11490Sstevel@tonic-gate  * Called while holding the algorithm lock.
11500Sstevel@tonic-gate  */
11510Sstevel@tonic-gate static void
11520Sstevel@tonic-gate esp_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs)
11530Sstevel@tonic-gate {
11540Sstevel@tonic-gate 	sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
11550Sstevel@tonic-gate 	ipsec_out_t *io;
11560Sstevel@tonic-gate 	ipsec_action_t *ap;
11570Sstevel@tonic-gate 	ipsec_prot_t *prot;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&alg_lock));
11600Sstevel@tonic-gate 	io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr;
11610Sstevel@tonic-gate 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
11640Sstevel@tonic-gate 	prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
11650Sstevel@tonic-gate 	*(uint32_t *)(&prop->sadb_prop_replay) = 0;	/* Quick zero-out! */
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	prop->sadb_prop_replay = ipsecesp_replay_size;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	/*
11700Sstevel@tonic-gate 	 * Based upon algorithm properties, and what-not, prioritize
11710Sstevel@tonic-gate 	 * a proposal.  If the IPSEC_OUT message has an algorithm specified,
11720Sstevel@tonic-gate 	 * use it first and foremost.
11730Sstevel@tonic-gate 	 *
11740Sstevel@tonic-gate 	 * For each action in policy list
11750Sstevel@tonic-gate 	 *   Add combination.  If I've hit limit, return.
11760Sstevel@tonic-gate 	 */
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	for (ap = acqrec->ipsacq_act; ap != NULL;
11790Sstevel@tonic-gate 	    ap = ap->ipa_next) {
11800Sstevel@tonic-gate 		ipsec_alginfo_t *ealg = NULL;
11810Sstevel@tonic-gate 		ipsec_alginfo_t *aalg = NULL;
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		if (ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY)
11840Sstevel@tonic-gate 			continue;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 		prot = &ap->ipa_act.ipa_apply;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 		if (!(prot->ipp_use_esp))
11890Sstevel@tonic-gate 			continue;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 		if (prot->ipp_esp_auth_alg != 0) {
11920Sstevel@tonic-gate 			aalg = ipsec_alglists[IPSEC_ALG_AUTH]
11930Sstevel@tonic-gate 			    [prot->ipp_esp_auth_alg];
11940Sstevel@tonic-gate 			if (aalg == NULL || !ALG_VALID(aalg))
11950Sstevel@tonic-gate 				continue;
11960Sstevel@tonic-gate 		}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 		ASSERT(prot->ipp_encr_alg > 0);
11990Sstevel@tonic-gate 		ealg = ipsec_alglists[IPSEC_ALG_ENCR][prot->ipp_encr_alg];
12000Sstevel@tonic-gate 		if (ealg == NULL || !ALG_VALID(ealg))
12010Sstevel@tonic-gate 			continue;
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 		comb->sadb_comb_flags = 0;
12040Sstevel@tonic-gate 		comb->sadb_comb_reserved = 0;
12050Sstevel@tonic-gate 		comb->sadb_comb_encrypt = ealg->alg_id;
12060Sstevel@tonic-gate 		comb->sadb_comb_encrypt_minbits = prot->ipp_espe_minbits;
12070Sstevel@tonic-gate 		comb->sadb_comb_encrypt_maxbits = prot->ipp_espe_maxbits;
12080Sstevel@tonic-gate 		if (aalg == NULL) {
12090Sstevel@tonic-gate 			comb->sadb_comb_auth = 0;
12100Sstevel@tonic-gate 			comb->sadb_comb_auth_minbits = 0;
12110Sstevel@tonic-gate 			comb->sadb_comb_auth_maxbits = 0;
12120Sstevel@tonic-gate 		} else {
12130Sstevel@tonic-gate 			comb->sadb_comb_auth = aalg->alg_id;
12140Sstevel@tonic-gate 			comb->sadb_comb_auth_minbits = prot->ipp_espa_minbits;
12150Sstevel@tonic-gate 			comb->sadb_comb_auth_maxbits = prot->ipp_espa_maxbits;
12160Sstevel@tonic-gate 		}
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 		/*
12190Sstevel@tonic-gate 		 * The following may be based on algorithm
12200Sstevel@tonic-gate 		 * properties, but in the meantime, we just pick
12210Sstevel@tonic-gate 		 * some good, sensible numbers.  Key mgmt. can
12220Sstevel@tonic-gate 		 * (and perhaps should) be the place to finalize
12230Sstevel@tonic-gate 		 * such decisions.
12240Sstevel@tonic-gate 		 */
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 		/*
12270Sstevel@tonic-gate 		 * No limits on allocations, since we really don't
12280Sstevel@tonic-gate 		 * support that concept currently.
12290Sstevel@tonic-gate 		 */
12300Sstevel@tonic-gate 		comb->sadb_comb_soft_allocations = 0;
12310Sstevel@tonic-gate 		comb->sadb_comb_hard_allocations = 0;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 		/*
12340Sstevel@tonic-gate 		 * These may want to come from policy rule..
12350Sstevel@tonic-gate 		 */
12360Sstevel@tonic-gate 		comb->sadb_comb_soft_bytes = ipsecesp_default_soft_bytes;
12370Sstevel@tonic-gate 		comb->sadb_comb_hard_bytes = ipsecesp_default_hard_bytes;
12380Sstevel@tonic-gate 		comb->sadb_comb_soft_addtime = ipsecesp_default_soft_addtime;
12390Sstevel@tonic-gate 		comb->sadb_comb_hard_addtime = ipsecesp_default_hard_addtime;
12400Sstevel@tonic-gate 		comb->sadb_comb_soft_usetime = ipsecesp_default_soft_usetime;
12410Sstevel@tonic-gate 		comb->sadb_comb_hard_usetime = ipsecesp_default_hard_usetime;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
12440Sstevel@tonic-gate 		if (--combs == 0)
12450Sstevel@tonic-gate 			break;	/* out of space.. */
12460Sstevel@tonic-gate 		comb++;
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate /*
12510Sstevel@tonic-gate  * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
12520Sstevel@tonic-gate  */
12530Sstevel@tonic-gate static void
12540Sstevel@tonic-gate esp_send_acquire(ipsacq_t *acqrec, mblk_t *extended)
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate 	mblk_t *pfkeymp, *msgmp;
12570Sstevel@tonic-gate 	uint_t allocsize, combs;
12580Sstevel@tonic-gate 	sadb_msg_t *samsg;
12590Sstevel@tonic-gate 	sadb_prop_t *prop;
12600Sstevel@tonic-gate 	uint8_t *cur, *end;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	ESP_BUMP_STAT(acquire_requests);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&acqrec->ipsacq_lock));
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	pfkeymp = sadb_keysock_out(0);
12670Sstevel@tonic-gate 	if (pfkeymp == NULL) {
12680Sstevel@tonic-gate 		esp0dbg(("esp_send_acquire: 1st allocb() failed.\n"));
12690Sstevel@tonic-gate 		/* Just bail. */
12700Sstevel@tonic-gate 		goto done;
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	/*
12740Sstevel@tonic-gate 	 * First, allocate a basic ACQUIRE message.  Beyond that,
12750Sstevel@tonic-gate 	 * you need to extract certificate info from
12760Sstevel@tonic-gate 	 */
12770Sstevel@tonic-gate 	allocsize = sizeof (sadb_msg_t) + sizeof (sadb_address_t) +
12780Sstevel@tonic-gate 	    sizeof (sadb_address_t) + sizeof (sadb_prop_t);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	switch (acqrec->ipsacq_addrfam) {
12810Sstevel@tonic-gate 	case AF_INET:
12820Sstevel@tonic-gate 		allocsize += 2 * sizeof (struct sockaddr_in);
12830Sstevel@tonic-gate 		break;
12840Sstevel@tonic-gate 	case AF_INET6:
12850Sstevel@tonic-gate 		allocsize += 2 * sizeof (struct sockaddr_in6);
12860Sstevel@tonic-gate 		break;
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	mutex_enter(&alg_lock);
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	combs = ipsec_nalgs[IPSEC_ALG_AUTH] * ipsec_nalgs[IPSEC_ALG_ENCR];
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	allocsize += combs * sizeof (sadb_comb_t);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * XXX If there are:
12970Sstevel@tonic-gate 	 *	certificate IDs
12980Sstevel@tonic-gate 	 *	proxy address
12990Sstevel@tonic-gate 	 *	<Others>
13000Sstevel@tonic-gate 	 * add additional allocation size.
13010Sstevel@tonic-gate 	 */
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	msgmp = allocb(allocsize, BPRI_HI);
13040Sstevel@tonic-gate 	if (msgmp == NULL) {
13050Sstevel@tonic-gate 		esp0dbg(("esp_send_acquire: 2nd allocb() failed.\n"));
13060Sstevel@tonic-gate 		/* Just bail. */
13070Sstevel@tonic-gate 		freemsg(pfkeymp);
13080Sstevel@tonic-gate 		pfkeymp = NULL;
13090Sstevel@tonic-gate 		goto done;
13100Sstevel@tonic-gate 	}
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	cur = msgmp->b_rptr;
13130Sstevel@tonic-gate 	end = cur + allocsize;
13140Sstevel@tonic-gate 	samsg = (sadb_msg_t *)cur;
13150Sstevel@tonic-gate 	pfkeymp->b_cont = msgmp;
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	/* Set up ACQUIRE. */
13180Sstevel@tonic-gate 	cur = sadb_setup_acquire(cur, end, acqrec);
13190Sstevel@tonic-gate 	if (cur == NULL) {
13200Sstevel@tonic-gate 		esp0dbg(("sadb_setup_acquire failed.\n"));
13210Sstevel@tonic-gate 		/* Just bail. */
13220Sstevel@tonic-gate 		freemsg(pfkeymp);
13230Sstevel@tonic-gate 		pfkeymp = NULL;
13240Sstevel@tonic-gate 		goto done;
13250Sstevel@tonic-gate 	}
13260Sstevel@tonic-gate 	samsg->sadb_msg_satype = SADB_SATYPE_ESP;
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	/* XXX Insert proxy address information here. */
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	/* XXX Insert identity information here. */
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	/* XXXMLS Insert sensitivity information here. */
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	/* Insert proposal here. */
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
13370Sstevel@tonic-gate 	esp_insert_prop(prop, acqrec, combs);
13380Sstevel@tonic-gate 	samsg->sadb_msg_len += prop->sadb_prop_len;
13390Sstevel@tonic-gate 	msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate done:
13420Sstevel@tonic-gate 	mutex_exit(&alg_lock);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	/*
13450Sstevel@tonic-gate 	 * Must mutex_exit() before sending PF_KEY message up, in
13460Sstevel@tonic-gate 	 * order to avoid recursive mutex_enter() if there are no registered
13470Sstevel@tonic-gate 	 * listeners.
13480Sstevel@tonic-gate 	 *
13490Sstevel@tonic-gate 	 * Once I've sent the message, I'm cool anyway.
13500Sstevel@tonic-gate 	 */
13510Sstevel@tonic-gate 	mutex_exit(&acqrec->ipsacq_lock);
13520Sstevel@tonic-gate 	if (esp_pfkey_q != NULL && pfkeymp != NULL) {
13530Sstevel@tonic-gate 		if (extended != NULL) {
13540Sstevel@tonic-gate 			putnext(esp_pfkey_q, extended);
13550Sstevel@tonic-gate 		}
13560Sstevel@tonic-gate 		putnext(esp_pfkey_q, pfkeymp);
13570Sstevel@tonic-gate 		return;
13580Sstevel@tonic-gate 	}
13590Sstevel@tonic-gate 	/* XXX freemsg() works for extended == NULL. */
13600Sstevel@tonic-gate 	freemsg(extended);
13610Sstevel@tonic-gate 	freemsg(pfkeymp);
13620Sstevel@tonic-gate }
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate /*
13650Sstevel@tonic-gate  * Handle the SADB_GETSPI message.  Create a larval SA.
13660Sstevel@tonic-gate  */
13670Sstevel@tonic-gate static void
13680Sstevel@tonic-gate esp_getspi(mblk_t *mp, keysock_in_t *ksi)
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate 	ipsa_t *newbie, *target;
13710Sstevel@tonic-gate 	isaf_t *outbound, *inbound;
13720Sstevel@tonic-gate 	int rc, diagnostic;
13730Sstevel@tonic-gate 	sadb_sa_t *assoc;
13740Sstevel@tonic-gate 	keysock_out_t *kso;
13750Sstevel@tonic-gate 	uint32_t newspi;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/*
13780Sstevel@tonic-gate 	 * Randomly generate a proposed SPI value
13790Sstevel@tonic-gate 	 */
13800Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t));
13810Sstevel@tonic-gate 	newbie = sadb_getspi(ksi, newspi, &diagnostic);
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	if (newbie == NULL) {
13840Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, ENOMEM, diagnostic,
13850Sstevel@tonic-gate 		    ksi->ks_in_serial);
13860Sstevel@tonic-gate 		return;
13870Sstevel@tonic-gate 	} else if (newbie == (ipsa_t *)-1) {
13880Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, EINVAL, diagnostic,
13890Sstevel@tonic-gate 		    ksi->ks_in_serial);
13900Sstevel@tonic-gate 		return;
13910Sstevel@tonic-gate 	}
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	/*
13940Sstevel@tonic-gate 	 * XXX - We may randomly collide.  We really should recover from this.
13950Sstevel@tonic-gate 	 *	 Unfortunately, that could require spending way-too-much-time
13960Sstevel@tonic-gate 	 *	 in here.  For now, let the user retry.
13970Sstevel@tonic-gate 	 */
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	if (newbie->ipsa_addrfam == AF_INET6) {
14000Sstevel@tonic-gate 		outbound = &esp_sadb.s_v6.sdb_of[
14010Sstevel@tonic-gate 		    OUTBOUND_HASH_V6(*(uint32_t *)(newbie->ipsa_dstaddr))];
14020Sstevel@tonic-gate 		inbound = &esp_sadb.s_v6.sdb_if[INBOUND_HASH(newbie->ipsa_spi)];
14030Sstevel@tonic-gate 	} else {
14040Sstevel@tonic-gate 		ASSERT(newbie->ipsa_addrfam == AF_INET);
14050Sstevel@tonic-gate 		outbound = &esp_sadb.s_v4.sdb_of[
14060Sstevel@tonic-gate 		    OUTBOUND_HASH_V4(*(uint32_t *)(newbie->ipsa_dstaddr))];
14070Sstevel@tonic-gate 		inbound = &esp_sadb.s_v4.sdb_if[INBOUND_HASH(newbie->ipsa_spi)];
14080Sstevel@tonic-gate 	}
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	mutex_enter(&outbound->isaf_lock);
14110Sstevel@tonic-gate 	mutex_enter(&inbound->isaf_lock);
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	/*
14140Sstevel@tonic-gate 	 * Check for collisions (i.e. did sadb_getspi() return with something
14150Sstevel@tonic-gate 	 * that already exists?).
14160Sstevel@tonic-gate 	 *
14170Sstevel@tonic-gate 	 * Try outbound first.  Even though SADB_GETSPI is traditionally
14180Sstevel@tonic-gate 	 * for inbound SAs, you never know what a user might do.
14190Sstevel@tonic-gate 	 */
14200Sstevel@tonic-gate 	target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
14210Sstevel@tonic-gate 	    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
14220Sstevel@tonic-gate 	if (target == NULL) {
14230Sstevel@tonic-gate 		target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
14240Sstevel@tonic-gate 		    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
14250Sstevel@tonic-gate 		    newbie->ipsa_addrfam);
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	/*
14290Sstevel@tonic-gate 	 * I don't have collisions elsewhere!
14300Sstevel@tonic-gate 	 * (Nor will I because I'm still holding inbound/outbound locks.)
14310Sstevel@tonic-gate 	 */
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	if (target != NULL) {
14340Sstevel@tonic-gate 		rc = EEXIST;
14350Sstevel@tonic-gate 		IPSA_REFRELE(target);
14360Sstevel@tonic-gate 	} else {
14370Sstevel@tonic-gate 		/*
14380Sstevel@tonic-gate 		 * sadb_insertassoc() also checks for collisions, so
14390Sstevel@tonic-gate 		 * if there's a colliding entry, rc will be set
14400Sstevel@tonic-gate 		 * to EEXIST.
14410Sstevel@tonic-gate 		 */
14420Sstevel@tonic-gate 		rc = sadb_insertassoc(newbie, inbound);
14430Sstevel@tonic-gate 		(void) drv_getparm(TIME, &newbie->ipsa_hardexpiretime);
14440Sstevel@tonic-gate 		newbie->ipsa_hardexpiretime += ipsecesp_larval_timeout;
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	/*
14480Sstevel@tonic-gate 	 * Can exit outbound mutex.  Hold inbound until we're done
14490Sstevel@tonic-gate 	 * with newbie.
14500Sstevel@tonic-gate 	 */
14510Sstevel@tonic-gate 	mutex_exit(&outbound->isaf_lock);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	if (rc != 0) {
14540Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
14550Sstevel@tonic-gate 		IPSA_REFRELE(newbie);
14560Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, rc, SADB_X_DIAGNOSTIC_NONE,
14570Sstevel@tonic-gate 		    ksi->ks_in_serial);
14580Sstevel@tonic-gate 		return;
14590Sstevel@tonic-gate 	}
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	/* Can write here because I'm still holding the bucket lock. */
14630Sstevel@tonic-gate 	newbie->ipsa_type = SADB_SATYPE_ESP;
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/*
14660Sstevel@tonic-gate 	 * Construct successful return message.  We have one thing going
14670Sstevel@tonic-gate 	 * for us in PF_KEY v2.  That's the fact that
14680Sstevel@tonic-gate 	 *	sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
14690Sstevel@tonic-gate 	 */
14700Sstevel@tonic-gate 	assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
14710Sstevel@tonic-gate 	assoc->sadb_sa_exttype = SADB_EXT_SA;
14720Sstevel@tonic-gate 	assoc->sadb_sa_spi = newbie->ipsa_spi;
14730Sstevel@tonic-gate 	*((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
14740Sstevel@tonic-gate 	mutex_exit(&inbound->isaf_lock);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	/* Convert KEYSOCK_IN to KEYSOCK_OUT. */
14770Sstevel@tonic-gate 	kso = (keysock_out_t *)ksi;
14780Sstevel@tonic-gate 	kso->ks_out_len = sizeof (*kso);
14790Sstevel@tonic-gate 	kso->ks_out_serial = ksi->ks_in_serial;
14800Sstevel@tonic-gate 	kso->ks_out_type = KEYSOCK_OUT;
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	/*
14830Sstevel@tonic-gate 	 * Can safely putnext() to esp_pfkey_q, because this is a turnaround
14840Sstevel@tonic-gate 	 * from the esp_pfkey_q.
14850Sstevel@tonic-gate 	 */
14860Sstevel@tonic-gate 	putnext(esp_pfkey_q, mp);
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate /*
14900Sstevel@tonic-gate  * Insert the ESP header into a packet.  Duplicate an mblk, and insert a newly
14910Sstevel@tonic-gate  * allocated mblk with the ESP header in between the two.
14920Sstevel@tonic-gate  */
14930Sstevel@tonic-gate static boolean_t
14940Sstevel@tonic-gate esp_insert_esp(mblk_t *mp, mblk_t *esp_mp, uint_t divpoint)
14950Sstevel@tonic-gate {
14960Sstevel@tonic-gate 	mblk_t *split_mp = mp;
14970Sstevel@tonic-gate 	uint_t wheretodiv = divpoint;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	while ((split_mp->b_wptr - split_mp->b_rptr) < wheretodiv) {
15000Sstevel@tonic-gate 		wheretodiv -= (split_mp->b_wptr - split_mp->b_rptr);
15010Sstevel@tonic-gate 		split_mp = split_mp->b_cont;
15020Sstevel@tonic-gate 		ASSERT(split_mp != NULL);
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	if (split_mp->b_wptr - split_mp->b_rptr != wheretodiv) {
15060Sstevel@tonic-gate 		mblk_t *scratch;
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 		/* "scratch" is the 2nd half, split_mp is the first. */
15090Sstevel@tonic-gate 		scratch = dupb(split_mp);
15100Sstevel@tonic-gate 		if (scratch == NULL) {
15110Sstevel@tonic-gate 			esp1dbg(("esp_insert_esp: can't allocate scratch.\n"));
15120Sstevel@tonic-gate 			return (B_FALSE);
15130Sstevel@tonic-gate 		}
15140Sstevel@tonic-gate 		/* NOTE:  dupb() doesn't set b_cont appropriately. */
15150Sstevel@tonic-gate 		scratch->b_cont = split_mp->b_cont;
15160Sstevel@tonic-gate 		scratch->b_rptr += wheretodiv;
15170Sstevel@tonic-gate 		split_mp->b_wptr = split_mp->b_rptr + wheretodiv;
15180Sstevel@tonic-gate 		split_mp->b_cont = scratch;
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 	/*
15210Sstevel@tonic-gate 	 * At this point, split_mp is exactly "wheretodiv" bytes long, and
15220Sstevel@tonic-gate 	 * holds the end of the pre-ESP part of the datagram.
15230Sstevel@tonic-gate 	 */
15240Sstevel@tonic-gate 	esp_mp->b_cont = split_mp->b_cont;
15250Sstevel@tonic-gate 	split_mp->b_cont = esp_mp;
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	return (B_TRUE);
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate /*
15310Sstevel@tonic-gate  * Finish processing of an inbound ESP packet after processing by the
15320Sstevel@tonic-gate  * crypto framework.
15330Sstevel@tonic-gate  * - Remove the ESP header.
15340Sstevel@tonic-gate  * - Send packet back to IP.
15350Sstevel@tonic-gate  * If authentication was performed on the packet, this function is called
15360Sstevel@tonic-gate  * only if the authentication succeeded.
15370Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
15380Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
15390Sstevel@tonic-gate  */
15400Sstevel@tonic-gate static ipsec_status_t
15410Sstevel@tonic-gate esp_in_done(mblk_t *ipsec_in_mp)
15420Sstevel@tonic-gate {
15430Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
15440Sstevel@tonic-gate 	mblk_t *data_mp;
15450Sstevel@tonic-gate 	ipsa_t *assoc;
15460Sstevel@tonic-gate 	uint_t espstart;
15470Sstevel@tonic-gate 	uint32_t ivlen = 0;
15480Sstevel@tonic-gate 	uint_t processed_len;
15490Sstevel@tonic-gate 	esph_t *esph;
15500Sstevel@tonic-gate 	kstat_named_t *counter;
15510Sstevel@tonic-gate 	boolean_t is_natt;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	assoc = ii->ipsec_in_esp_sa;
15540Sstevel@tonic-gate 	ASSERT(assoc != NULL);
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	/* get the pointer to the ESP header */
15590Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg == SADB_EALG_NULL) {
15600Sstevel@tonic-gate 		/* authentication-only ESP */
15610Sstevel@tonic-gate 		espstart = ii->ipsec_in_crypto_data.cd_offset;
15620Sstevel@tonic-gate 		processed_len = ii->ipsec_in_crypto_data.cd_length;
15630Sstevel@tonic-gate 	} else {
15640Sstevel@tonic-gate 		/* encryption present */
15650Sstevel@tonic-gate 		ivlen = assoc->ipsa_iv_len;
15660Sstevel@tonic-gate 		if (assoc->ipsa_auth_alg == SADB_AALG_NONE) {
15670Sstevel@tonic-gate 			/* encryption-only ESP */
15680Sstevel@tonic-gate 			espstart = ii->ipsec_in_crypto_data.cd_offset -
15690Sstevel@tonic-gate 				sizeof (esph_t) - assoc->ipsa_iv_len;
15700Sstevel@tonic-gate 			processed_len = ii->ipsec_in_crypto_data.cd_length +
15710Sstevel@tonic-gate 				ivlen;
15720Sstevel@tonic-gate 		} else {
15730Sstevel@tonic-gate 			/* encryption with authentication */
15740Sstevel@tonic-gate 			espstart = ii->ipsec_in_crypto_dual_data.dd_offset1;
15750Sstevel@tonic-gate 			processed_len = ii->ipsec_in_crypto_dual_data.dd_len2 +
15760Sstevel@tonic-gate 			    ivlen;
15770Sstevel@tonic-gate 		}
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	data_mp = ipsec_in_mp->b_cont;
15810Sstevel@tonic-gate 	esph = (esph_t *)(data_mp->b_rptr + espstart);
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) {
15840Sstevel@tonic-gate 		/* authentication passed if we reach this point */
15850Sstevel@tonic-gate 		ESP_BUMP_STAT(good_auth);
15860Sstevel@tonic-gate 		data_mp->b_wptr -= assoc->ipsa_mac_len;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 		/*
15890Sstevel@tonic-gate 		 * Check replay window here!
15900Sstevel@tonic-gate 		 * For right now, assume keysock will set the replay window
15910Sstevel@tonic-gate 		 * size to zero for SAs that have an unspecified sender.
15920Sstevel@tonic-gate 		 * This may change...
15930Sstevel@tonic-gate 		 */
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 		if (!sadb_replay_check(assoc, esph->esph_replay)) {
15960Sstevel@tonic-gate 			/*
15970Sstevel@tonic-gate 			 * Log the event. As of now we print out an event.
15980Sstevel@tonic-gate 			 * Do not print the replay failure number, or else
15990Sstevel@tonic-gate 			 * syslog cannot collate the error messages.  Printing
16000Sstevel@tonic-gate 			 * the replay number that failed opens a denial-of-
16010Sstevel@tonic-gate 			 * service attack.
16020Sstevel@tonic-gate 			 */
16030Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
16040Sstevel@tonic-gate 			    SL_ERROR | SL_WARN,
16050Sstevel@tonic-gate 			    "Replay failed for ESP spi 0x%x, dst %s.\n",
16060Sstevel@tonic-gate 			    assoc->ipsa_spi, assoc->ipsa_dstaddr,
16070Sstevel@tonic-gate 			    assoc->ipsa_addrfam);
16080Sstevel@tonic-gate 			ESP_BUMP_STAT(replay_failures);
16090Sstevel@tonic-gate 			counter = &ipdrops_esp_replay;
16100Sstevel@tonic-gate 			goto drop_and_bail;
16110Sstevel@tonic-gate 		}
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	if (!esp_age_bytes(assoc, processed_len, B_TRUE)) {
16150Sstevel@tonic-gate 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
16160Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
16170Sstevel@tonic-gate 		    SL_ERROR | SL_WARN,
16180Sstevel@tonic-gate 		    "ESP association 0x%x, dst %s had bytes expire.\n",
16190Sstevel@tonic-gate 		    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam);
16200Sstevel@tonic-gate 		ESP_BUMP_STAT(bytes_expired);
16210Sstevel@tonic-gate 		counter = &ipdrops_esp_bytes_expire;
16220Sstevel@tonic-gate 		goto drop_and_bail;
16230Sstevel@tonic-gate 	}
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	/*
16260Sstevel@tonic-gate 	 * Remove ESP header and padding from packet.  I hope the compiler
16270Sstevel@tonic-gate 	 * spews "branch, predict taken" code for this.
16280Sstevel@tonic-gate 	 */
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	if (esp_strip_header(data_mp, ii->ipsec_in_v4, ivlen, &counter)) {
16310Sstevel@tonic-gate 		if (is_natt)
16320Sstevel@tonic-gate 			return (esp_fix_natt_checksums(data_mp, assoc));
16330Sstevel@tonic-gate 		return (IPSEC_STATUS_SUCCESS);
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	esp1dbg(("esp_in_done: esp_strip_header() failed\n"));
16370Sstevel@tonic-gate drop_and_bail:
16380Sstevel@tonic-gate 	IP_ESP_BUMP_STAT(in_discards);
16390Sstevel@tonic-gate 	/*
16400Sstevel@tonic-gate 	 * TODO: Extract inbound interface from the IPSEC_IN message's
16410Sstevel@tonic-gate 	 * ii->ipsec_in_rill_index.
16420Sstevel@tonic-gate 	 */
16430Sstevel@tonic-gate 	ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, counter, &esp_dropper);
16440Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate /*
16480Sstevel@tonic-gate  * Called upon failing the inbound ICV check. The message passed as
16490Sstevel@tonic-gate  * argument is freed.
16500Sstevel@tonic-gate  */
16510Sstevel@tonic-gate static void
16520Sstevel@tonic-gate esp_log_bad_auth(mblk_t *ipsec_in)
16530Sstevel@tonic-gate {
16540Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
16550Sstevel@tonic-gate 	ipsa_t *assoc = ii->ipsec_in_esp_sa;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	/*
16580Sstevel@tonic-gate 	 * Log the event. Don't print to the console, block
16590Sstevel@tonic-gate 	 * potential denial-of-service attack.
16600Sstevel@tonic-gate 	 */
16610Sstevel@tonic-gate 	ESP_BUMP_STAT(bad_auth);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
16640Sstevel@tonic-gate 	    "ESP Authentication failed for spi 0x%x, dst %s.\n",
16650Sstevel@tonic-gate 	    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam);
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	IP_ESP_BUMP_STAT(in_discards);
16680Sstevel@tonic-gate 	/*
16690Sstevel@tonic-gate 	 * TODO: Extract inbound interface from the IPSEC_IN
16700Sstevel@tonic-gate 	 * message's ii->ipsec_in_rill_index.
16710Sstevel@tonic-gate 	 */
16720Sstevel@tonic-gate 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_esp_bad_auth,
16730Sstevel@tonic-gate 	    &esp_dropper);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate /*
16780Sstevel@tonic-gate  * Invoked for outbound packets after ESP processing. If the packet
16790Sstevel@tonic-gate  * also requires AH, performs the AH SA selection and AH processing.
16800Sstevel@tonic-gate  * Returns B_TRUE if the AH processing was not needed or if it was
16810Sstevel@tonic-gate  * performed successfully. Returns B_FALSE and consumes the passed mblk
16820Sstevel@tonic-gate  * if AH processing was required but could not be performed.
16830Sstevel@tonic-gate  */
16840Sstevel@tonic-gate static boolean_t
16850Sstevel@tonic-gate esp_do_outbound_ah(mblk_t *ipsec_mp)
16860Sstevel@tonic-gate {
16870Sstevel@tonic-gate 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
16880Sstevel@tonic-gate 	ipsec_status_t ipsec_rc;
16890Sstevel@tonic-gate 	ipsec_action_t *ap;
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	ap = io->ipsec_out_act;
16920Sstevel@tonic-gate 	if (ap == NULL) {
16930Sstevel@tonic-gate 		ipsec_policy_t *pp = io->ipsec_out_policy;
16940Sstevel@tonic-gate 		ap = pp->ipsp_act;
16950Sstevel@tonic-gate 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	if (!ap->ipa_want_ah)
16980Sstevel@tonic-gate 		return (B_TRUE);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	ASSERT(io->ipsec_out_ah_done == B_FALSE);
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	if (io->ipsec_out_ah_sa == NULL) {
17030Sstevel@tonic-gate 		if (!ipsec_outbound_sa(ipsec_mp, IPPROTO_AH)) {
17040Sstevel@tonic-gate 			sadb_acquire(ipsec_mp, io, B_TRUE, B_FALSE);
17050Sstevel@tonic-gate 			return (B_FALSE);
17060Sstevel@tonic-gate 		}
17070Sstevel@tonic-gate 	}
17080Sstevel@tonic-gate 	ASSERT(io->ipsec_out_ah_sa != NULL);
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	io->ipsec_out_ah_done = B_TRUE;
17110Sstevel@tonic-gate 	ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp);
17120Sstevel@tonic-gate 	return (ipsec_rc == IPSEC_STATUS_SUCCESS);
17130Sstevel@tonic-gate }
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate /*
17170Sstevel@tonic-gate  * Kernel crypto framework callback invoked after completion of async
17180Sstevel@tonic-gate  * crypto requests.
17190Sstevel@tonic-gate  */
17200Sstevel@tonic-gate static void
17210Sstevel@tonic-gate esp_kcf_callback(void *arg, int status)
17220Sstevel@tonic-gate {
17230Sstevel@tonic-gate 	mblk_t *ipsec_mp = (mblk_t *)arg;
17240Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
17250Sstevel@tonic-gate 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	ASSERT(ipsec_mp->b_cont != NULL);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	if (status == CRYPTO_SUCCESS) {
17300Sstevel@tonic-gate 		if (is_inbound) {
17310Sstevel@tonic-gate 			if (esp_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS)
17320Sstevel@tonic-gate 				return;
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 			/* finish IPsec processing */
17350Sstevel@tonic-gate 			ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL);
17360Sstevel@tonic-gate 		} else {
17370Sstevel@tonic-gate 			/*
17380Sstevel@tonic-gate 			 * If a ICV was computed, it was stored by the
17390Sstevel@tonic-gate 			 * crypto framework at the end of the packet.
17400Sstevel@tonic-gate 			 */
17410Sstevel@tonic-gate 			ipha_t *ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 			/* do AH processing if needed */
17440Sstevel@tonic-gate 			if (!esp_do_outbound_ah(ipsec_mp))
17450Sstevel@tonic-gate 				return;
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 			/* finish IPsec processing */
17480Sstevel@tonic-gate 			if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
17490Sstevel@tonic-gate 				ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL,
17500Sstevel@tonic-gate 				    NULL);
17510Sstevel@tonic-gate 			} else {
17520Sstevel@tonic-gate 				ip6_t *ip6h = (ip6_t *)ipha;
17530Sstevel@tonic-gate 				ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h,
17540Sstevel@tonic-gate 				    NULL, NULL);
17550Sstevel@tonic-gate 			}
17560Sstevel@tonic-gate 		}
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	} else if (status == CRYPTO_INVALID_MAC) {
17590Sstevel@tonic-gate 		esp_log_bad_auth(ipsec_mp);
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	} else {
17620Sstevel@tonic-gate 		esp1dbg(("esp_kcf_callback: crypto failed with 0x%x\n",
17630Sstevel@tonic-gate 		    status));
17640Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_failures);
17650Sstevel@tonic-gate 		if (is_inbound)
17660Sstevel@tonic-gate 			IP_ESP_BUMP_STAT(in_discards);
17670Sstevel@tonic-gate 		else
17680Sstevel@tonic-gate 			ESP_BUMP_STAT(out_discards);
17690Sstevel@tonic-gate 		ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL,
17700Sstevel@tonic-gate 		    &ipdrops_esp_crypto_failed, &esp_dropper);
17710Sstevel@tonic-gate 	}
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate /*
17750Sstevel@tonic-gate  * Invoked on crypto framework failure during inbound and outbound processing.
17760Sstevel@tonic-gate  */
17770Sstevel@tonic-gate static void
17780Sstevel@tonic-gate esp_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc)
17790Sstevel@tonic-gate {
17800Sstevel@tonic-gate 	esp1dbg(("crypto failed for %s ESP with 0x%x\n",
17810Sstevel@tonic-gate 	    is_inbound ? "inbound" : "outbound", kef_rc));
17820Sstevel@tonic-gate 	ip_drop_packet(mp, is_inbound, NULL, NULL, &ipdrops_esp_crypto_failed,
17830Sstevel@tonic-gate 	    &esp_dropper);
17840Sstevel@tonic-gate 	ESP_BUMP_STAT(crypto_failures);
17850Sstevel@tonic-gate 	if (is_inbound)
17860Sstevel@tonic-gate 		IP_ESP_BUMP_STAT(in_discards);
17870Sstevel@tonic-gate 	else
17880Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
17890Sstevel@tonic-gate }
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate #define	ESP_INIT_CALLREQ(_cr) {						\
17920Sstevel@tonic-gate 	(_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED;		\
17930Sstevel@tonic-gate 	(_cr)->cr_callback_arg = ipsec_mp;				\
17940Sstevel@tonic-gate 	(_cr)->cr_callback_func = esp_kcf_callback;			\
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) {			\
17980Sstevel@tonic-gate 	(mac)->cd_format = CRYPTO_DATA_RAW;				\
17990Sstevel@tonic-gate 	(mac)->cd_offset = 0;						\
18000Sstevel@tonic-gate 	(mac)->cd_length = icvlen;					\
18010Sstevel@tonic-gate 	(mac)->cd_raw.iov_base = (char *)icvbuf;			\
18020Sstevel@tonic-gate 	(mac)->cd_raw.iov_len = icvlen;					\
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_DATA(data, mp, off, len) {			\
18060Sstevel@tonic-gate 	if (MBLKL(mp) >= (len) + (off)) {				\
18070Sstevel@tonic-gate 		(data)->cd_format = CRYPTO_DATA_RAW;			\
18080Sstevel@tonic-gate 		(data)->cd_raw.iov_base = (char *)(mp)->b_rptr;		\
18090Sstevel@tonic-gate 		(data)->cd_raw.iov_len = MBLKL(mp);			\
18100Sstevel@tonic-gate 		(data)->cd_offset = off;				\
18110Sstevel@tonic-gate 	} else {							\
18120Sstevel@tonic-gate 		(data)->cd_format = CRYPTO_DATA_MBLK;			\
18130Sstevel@tonic-gate 		(data)->cd_mp = mp;			       		\
18140Sstevel@tonic-gate 		(data)->cd_offset = off;				\
18150Sstevel@tonic-gate 	}								\
18160Sstevel@tonic-gate 	(data)->cd_length = len;					\
18170Sstevel@tonic-gate }
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_DUAL_DATA(data, mp, off1, len1, off2, len2) {	\
18200Sstevel@tonic-gate 	(data)->dd_format = CRYPTO_DATA_MBLK;				\
18210Sstevel@tonic-gate 	(data)->dd_mp = mp;						\
18220Sstevel@tonic-gate 	(data)->dd_len1 = len1;						\
18230Sstevel@tonic-gate 	(data)->dd_offset1 = off1;					\
18240Sstevel@tonic-gate 	(data)->dd_len2 = len2;						\
18250Sstevel@tonic-gate 	(data)->dd_offset2 = off2;					\
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate static ipsec_status_t
18290Sstevel@tonic-gate esp_submit_req_inbound(mblk_t *ipsec_mp, ipsa_t *assoc, uint_t esph_offset)
18300Sstevel@tonic-gate {
18310Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
18320Sstevel@tonic-gate 	boolean_t do_auth;
18330Sstevel@tonic-gate 	uint_t auth_offset, msg_len, auth_len;
18340Sstevel@tonic-gate 	crypto_call_req_t call_req;
18350Sstevel@tonic-gate 	mblk_t *esp_mp;
18360Sstevel@tonic-gate 	int kef_rc = CRYPTO_FAILED;
18370Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
18380Sstevel@tonic-gate 	crypto_ctx_template_t auth_ctx_tmpl;
18390Sstevel@tonic-gate 	boolean_t do_encr;
18400Sstevel@tonic-gate 	uint_t encr_offset, encr_len;
18410Sstevel@tonic-gate 	uint_t iv_len = assoc->ipsa_iv_len;
18420Sstevel@tonic-gate 	crypto_ctx_template_t encr_ctx_tmpl;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
18470Sstevel@tonic-gate 	do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	/*
18500Sstevel@tonic-gate 	 * An inbound packet is of the form:
18510Sstevel@tonic-gate 	 * IPSEC_IN -> [IP,options,ESP,IV,data,ICV,pad]
18520Sstevel@tonic-gate 	 */
18530Sstevel@tonic-gate 	esp_mp = ipsec_mp->b_cont;
18540Sstevel@tonic-gate 	msg_len = MBLKL(esp_mp);
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	ESP_INIT_CALLREQ(&call_req);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	if (do_auth) {
18590Sstevel@tonic-gate 		/* force asynchronous processing? */
18600Sstevel@tonic-gate 		if (ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
18610Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
18620Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 		/* authentication context template */
18650Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
18660Sstevel@tonic-gate 		    auth_ctx_tmpl);
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 		/* ICV to be verified */
18690Sstevel@tonic-gate 		ESP_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac,
18700Sstevel@tonic-gate 		    icv_len, esp_mp->b_wptr - icv_len);
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 		/* authentication starts at the ESP header */
18730Sstevel@tonic-gate 		auth_offset = esph_offset;
18740Sstevel@tonic-gate 		auth_len = msg_len - auth_offset - icv_len;
18750Sstevel@tonic-gate 		if (!do_encr) {
18760Sstevel@tonic-gate 			/* authentication only */
18770Sstevel@tonic-gate 			/* initialize input data argument */
18780Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data,
18790Sstevel@tonic-gate 			    esp_mp, auth_offset, auth_len);
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 			/* call the crypto framework */
18820Sstevel@tonic-gate 			kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
18830Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_data,
18840Sstevel@tonic-gate 			    &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
18850Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_mac, &call_req);
18860Sstevel@tonic-gate 		}
18870Sstevel@tonic-gate 	}
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	if (do_encr) {
18900Sstevel@tonic-gate 		/* force asynchronous processing? */
18910Sstevel@tonic-gate 		if (ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
18920Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
18930Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 		/* encryption template */
18960Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
18970Sstevel@tonic-gate 		    encr_ctx_tmpl);
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		/* skip IV, since it is passed separately */
19000Sstevel@tonic-gate 		encr_offset = esph_offset + sizeof (esph_t) + iv_len;
19010Sstevel@tonic-gate 		encr_len = msg_len - encr_offset;
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 		if (!do_auth) {
19040Sstevel@tonic-gate 			/* decryption only */
19050Sstevel@tonic-gate 			/* initialize input data argument */
19060Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data,
19070Sstevel@tonic-gate 			    esp_mp, encr_offset, encr_len);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 			/* specify IV */
19100Sstevel@tonic-gate 			ii->ipsec_in_crypto_data.cd_miscdata =
19110Sstevel@tonic-gate 			    (char *)esp_mp->b_rptr + sizeof (esph_t) +
19120Sstevel@tonic-gate 			    esph_offset;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 			/* call the crypto framework */
19150Sstevel@tonic-gate 			kef_rc = crypto_decrypt(&assoc->ipsa_emech,
19160Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_data,
19170Sstevel@tonic-gate 			    &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
19180Sstevel@tonic-gate 			    NULL, &call_req);
19190Sstevel@tonic-gate 		}
19200Sstevel@tonic-gate 	}
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	if (do_auth && do_encr) {
19230Sstevel@tonic-gate 		/* dual operation */
19240Sstevel@tonic-gate 		/* initialize input data argument */
19250Sstevel@tonic-gate 		ESP_INIT_CRYPTO_DUAL_DATA(&ii->ipsec_in_crypto_dual_data,
19260Sstevel@tonic-gate 		    esp_mp, auth_offset, auth_len,
19270Sstevel@tonic-gate 		    encr_offset, encr_len - icv_len);
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 		/* specify IV */
19300Sstevel@tonic-gate 		ii->ipsec_in_crypto_dual_data.dd_miscdata =
19310Sstevel@tonic-gate 		    (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 		/* call the framework */
19340Sstevel@tonic-gate 		kef_rc = crypto_mac_verify_decrypt(&assoc->ipsa_amech,
19350Sstevel@tonic-gate 		    &assoc->ipsa_emech, &ii->ipsec_in_crypto_dual_data,
19360Sstevel@tonic-gate 		    &assoc->ipsa_kcfauthkey, &assoc->ipsa_kcfencrkey,
19370Sstevel@tonic-gate 		    auth_ctx_tmpl, encr_ctx_tmpl, &ii->ipsec_in_crypto_mac,
19380Sstevel@tonic-gate 		    NULL, &call_req);
19390Sstevel@tonic-gate 	}
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	switch (kef_rc) {
19420Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
19430Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_sync);
19440Sstevel@tonic-gate 		return (esp_in_done(ipsec_mp));
19450Sstevel@tonic-gate 	case CRYPTO_QUEUED:
19460Sstevel@tonic-gate 		/* esp_kcf_callback() will be invoked on completion */
19470Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_async);
19480Sstevel@tonic-gate 		return (IPSEC_STATUS_PENDING);
19490Sstevel@tonic-gate 	case CRYPTO_INVALID_MAC:
19500Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_sync);
19510Sstevel@tonic-gate 		esp_log_bad_auth(ipsec_mp);
19520Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
19530Sstevel@tonic-gate 	}
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc);
19560Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
19570Sstevel@tonic-gate }
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate static ipsec_status_t
19600Sstevel@tonic-gate esp_submit_req_outbound(mblk_t *ipsec_mp, ipsa_t *assoc, uchar_t *icv_buf,
19610Sstevel@tonic-gate     uint_t payload_len)
19620Sstevel@tonic-gate {
19630Sstevel@tonic-gate 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
19640Sstevel@tonic-gate 	uint_t auth_len;
19650Sstevel@tonic-gate 	crypto_call_req_t call_req;
19660Sstevel@tonic-gate 	mblk_t *esp_mp;
19670Sstevel@tonic-gate 	int kef_rc = CRYPTO_FAILED;
19680Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
19690Sstevel@tonic-gate 	crypto_ctx_template_t auth_ctx_tmpl;
19700Sstevel@tonic-gate 	boolean_t do_auth;
19710Sstevel@tonic-gate 	boolean_t do_encr;
19720Sstevel@tonic-gate 	uint_t iv_len = assoc->ipsa_iv_len;
19730Sstevel@tonic-gate 	crypto_ctx_template_t encr_ctx_tmpl;
19740Sstevel@tonic-gate 	boolean_t is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
19750Sstevel@tonic-gate 	size_t esph_offset = (is_natt ? UDPH_SIZE : 0);
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	esp3dbg(("esp_submit_req_outbound:%s", is_natt ? "natt" : "not natt"));
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
19820Sstevel@tonic-gate 	do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	/*
19850Sstevel@tonic-gate 	 * Outbound IPsec packets are of the form:
19860Sstevel@tonic-gate 	 * IPSEC_OUT -> [IP,options] -> [ESP,IV] -> [data] -> [pad,ICV]
19870Sstevel@tonic-gate 	 * unless it's NATT, then it's
19880Sstevel@tonic-gate 	 * IPSEC_OUT -> [IP,options] -> [udp][ESP,IV] -> [data] -> [pad,ICV]
19890Sstevel@tonic-gate 	 * Get a pointer to the mblk containing the ESP header.
19900Sstevel@tonic-gate 	 */
19910Sstevel@tonic-gate 	ASSERT(ipsec_mp->b_cont != NULL && ipsec_mp->b_cont->b_cont != NULL);
19920Sstevel@tonic-gate 	esp_mp = ipsec_mp->b_cont->b_cont;
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	ESP_INIT_CALLREQ(&call_req);
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 	if (do_auth) {
19970Sstevel@tonic-gate 		/* force asynchronous processing? */
19980Sstevel@tonic-gate 		if (ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
19990Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
20000Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 		/* authentication context template */
20030Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
20040Sstevel@tonic-gate 		    auth_ctx_tmpl);
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 		/* where to store the computed mac */
20070Sstevel@tonic-gate 		ESP_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac,
20080Sstevel@tonic-gate 		    icv_len, icv_buf);
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 		/* authentication starts at the ESP header */
2011*134Sdanmcd 		auth_len = payload_len + iv_len + sizeof (esph_t);
20120Sstevel@tonic-gate 		if (!do_encr) {
20130Sstevel@tonic-gate 			/* authentication only */
20140Sstevel@tonic-gate 			/* initialize input data argument */
20150Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data,
20160Sstevel@tonic-gate 			    esp_mp, esph_offset, auth_len);
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 			/* call the crypto framework */
20190Sstevel@tonic-gate 			kef_rc = crypto_mac(&assoc->ipsa_amech,
20200Sstevel@tonic-gate 			    &io->ipsec_out_crypto_data,
20210Sstevel@tonic-gate 			    &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
20220Sstevel@tonic-gate 			    &io->ipsec_out_crypto_mac, &call_req);
20230Sstevel@tonic-gate 		}
20240Sstevel@tonic-gate 	}
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	if (do_encr) {
20270Sstevel@tonic-gate 		/* force asynchronous processing? */
20280Sstevel@tonic-gate 		if (ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
20290Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
20300Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 		/* encryption context template */
20330Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
20340Sstevel@tonic-gate 		    encr_ctx_tmpl);
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 		if (!do_auth) {
20370Sstevel@tonic-gate 			/* encryption only, skip mblk that contains ESP hdr */
20380Sstevel@tonic-gate 			/* initialize input data argument */
20390Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data,
20400Sstevel@tonic-gate 			    esp_mp->b_cont, 0, payload_len);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 			/* specify IV */
20430Sstevel@tonic-gate 			io->ipsec_out_crypto_data.cd_miscdata =
20440Sstevel@tonic-gate 			    (char *)esp_mp->b_rptr + sizeof (esph_t) +
20450Sstevel@tonic-gate 			    esph_offset;
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 			/* call the crypto framework */
20480Sstevel@tonic-gate 			kef_rc = crypto_encrypt(&assoc->ipsa_emech,
20490Sstevel@tonic-gate 			    &io->ipsec_out_crypto_data,
20500Sstevel@tonic-gate 			    &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
20510Sstevel@tonic-gate 			    NULL, &call_req);
20520Sstevel@tonic-gate 		}
20530Sstevel@tonic-gate 	}
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	if (do_auth && do_encr) {
20560Sstevel@tonic-gate 		/*
20570Sstevel@tonic-gate 		 * Encryption and authentication:
20580Sstevel@tonic-gate 		 * Pass the pointer to the mblk chain starting at the ESP
20590Sstevel@tonic-gate 		 * header to the framework. Skip the ESP header mblk
20600Sstevel@tonic-gate 		 * for encryption, which is reflected by an encryption
20610Sstevel@tonic-gate 		 * offset equal to the length of that mblk. Start
20620Sstevel@tonic-gate 		 * the authentication at the ESP header, i.e. use an
20630Sstevel@tonic-gate 		 * authentication offset of zero.
20640Sstevel@tonic-gate 		 */
20650Sstevel@tonic-gate 		ESP_INIT_CRYPTO_DUAL_DATA(&io->ipsec_out_crypto_dual_data,
20660Sstevel@tonic-gate 		    esp_mp, MBLKL(esp_mp), payload_len, esph_offset, auth_len);
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 		/* specify IV */
20690Sstevel@tonic-gate 		io->ipsec_out_crypto_dual_data.dd_miscdata =
20700Sstevel@tonic-gate 		    (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 		/* call the framework */
20730Sstevel@tonic-gate 		kef_rc = crypto_encrypt_mac(&assoc->ipsa_emech,
20740Sstevel@tonic-gate 		    &assoc->ipsa_amech, NULL,
20750Sstevel@tonic-gate 		    &assoc->ipsa_kcfencrkey, &assoc->ipsa_kcfauthkey,
20760Sstevel@tonic-gate 		    encr_ctx_tmpl, auth_ctx_tmpl,
20770Sstevel@tonic-gate 		    &io->ipsec_out_crypto_dual_data,
20780Sstevel@tonic-gate 		    &io->ipsec_out_crypto_mac, &call_req);
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	switch (kef_rc) {
20820Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
20830Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_sync);
20840Sstevel@tonic-gate 		return (IPSEC_STATUS_SUCCESS);
20850Sstevel@tonic-gate 	case CRYPTO_QUEUED:
20860Sstevel@tonic-gate 		/* esp_kcf_callback() will be invoked on completion */
20870Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_async);
20880Sstevel@tonic-gate 		return (IPSEC_STATUS_PENDING);
20890Sstevel@tonic-gate 	}
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 	esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc);
20920Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
20930Sstevel@tonic-gate }
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate /*
20960Sstevel@tonic-gate  * Handle outbound IPsec processing for IPv4 and IPv6
20970Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
20980Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
20990Sstevel@tonic-gate  */
21000Sstevel@tonic-gate static ipsec_status_t
21010Sstevel@tonic-gate esp_outbound(mblk_t *mp)
21020Sstevel@tonic-gate {
21030Sstevel@tonic-gate 	mblk_t *ipsec_out_mp, *data_mp, *espmp, *tailmp;
21040Sstevel@tonic-gate 	ipsec_out_t *io;
21050Sstevel@tonic-gate 	ipha_t *ipha;
21060Sstevel@tonic-gate 	ip6_t *ip6h;
21070Sstevel@tonic-gate 	esph_t *esph;
21080Sstevel@tonic-gate 	uint_t af;
21090Sstevel@tonic-gate 	uint8_t *nhp;
21100Sstevel@tonic-gate 	uintptr_t divpoint, datalen, adj, padlen, i, alloclen;
21110Sstevel@tonic-gate 	uintptr_t esplen = sizeof (esph_t);
21120Sstevel@tonic-gate 	uint8_t protocol;
21130Sstevel@tonic-gate 	ipsa_t *assoc;
21140Sstevel@tonic-gate 	uint_t iv_len = 0, mac_len = 0;
21150Sstevel@tonic-gate 	uchar_t *icv_buf;
21160Sstevel@tonic-gate 	udpha_t *udpha;
21170Sstevel@tonic-gate 	boolean_t is_natt = B_FALSE;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 	ESP_BUMP_STAT(out_requests);
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	ipsec_out_mp = mp;
21220Sstevel@tonic-gate 	data_mp = ipsec_out_mp->b_cont;
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	/*
21250Sstevel@tonic-gate 	 * <sigh> We have to copy the message here, because TCP (for example)
21260Sstevel@tonic-gate 	 * keeps a dupb() of the message lying around for retransmission.
21270Sstevel@tonic-gate 	 * Since ESP changes the whole of the datagram, we have to create our
21280Sstevel@tonic-gate 	 * own copy lest we clobber TCP's data.  Since we have to copy anyway,
21290Sstevel@tonic-gate 	 * we might as well make use of msgpullup() and get the mblk into one
21300Sstevel@tonic-gate 	 * contiguous piece!
21310Sstevel@tonic-gate 	 */
21320Sstevel@tonic-gate 	ipsec_out_mp->b_cont = msgpullup(data_mp, -1);
21330Sstevel@tonic-gate 	if (ipsec_out_mp->b_cont == NULL) {
21340Sstevel@tonic-gate 		esp0dbg(("esp_outbound: msgpullup() failed, "
21350Sstevel@tonic-gate 		    "dropping packet.\n"));
21360Sstevel@tonic-gate 		ipsec_out_mp->b_cont = data_mp;
21370Sstevel@tonic-gate 		/*
21380Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
21390Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
21400Sstevel@tonic-gate 		 */
21410Sstevel@tonic-gate 		ip_drop_packet(ipsec_out_mp, B_FALSE, NULL, NULL,
21420Sstevel@tonic-gate 		    &ipdrops_esp_nomem, &esp_dropper);
21430Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
21440Sstevel@tonic-gate 	} else {
21450Sstevel@tonic-gate 		freemsg(data_mp);
21460Sstevel@tonic-gate 		data_mp = ipsec_out_mp->b_cont;
21470Sstevel@tonic-gate 	}
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 	io = (ipsec_out_t *)ipsec_out_mp->b_rptr;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	/*
21520Sstevel@tonic-gate 	 * Reality check....
21530Sstevel@tonic-gate 	 */
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	ipha = (ipha_t *)data_mp->b_rptr;  /* So we can call esp_acquire(). */
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	if (io->ipsec_out_v4) {
21580Sstevel@tonic-gate 		af = AF_INET;
21590Sstevel@tonic-gate 		divpoint = IPH_HDR_LENGTH(ipha);
21600Sstevel@tonic-gate 		datalen = ntohs(ipha->ipha_length) - divpoint;
21610Sstevel@tonic-gate 		nhp = (uint8_t *)&ipha->ipha_protocol;
21620Sstevel@tonic-gate 	} else {
21630Sstevel@tonic-gate 		ip6_pkt_t ipp;
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 		af = AF_INET6;
21660Sstevel@tonic-gate 		ip6h = (ip6_t *)ipha;
21670Sstevel@tonic-gate 		bzero(&ipp, sizeof (ipp));
21680Sstevel@tonic-gate 		divpoint = ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL);
21690Sstevel@tonic-gate 		if (ipp.ipp_dstopts != NULL &&
21700Sstevel@tonic-gate 		    ipp.ipp_dstopts->ip6d_nxt != IPPROTO_ROUTING) {
21710Sstevel@tonic-gate 			/*
21720Sstevel@tonic-gate 			 * Destination options are tricky.  If we get in here,
21730Sstevel@tonic-gate 			 * then we have a terminal header following the
21740Sstevel@tonic-gate 			 * destination options.  We need to adjust backwards
21750Sstevel@tonic-gate 			 * so we insert ESP BEFORE the destination options
21760Sstevel@tonic-gate 			 * bag.  (So that the dstopts get encrypted!)
21770Sstevel@tonic-gate 			 *
21780Sstevel@tonic-gate 			 * Since this is for outbound packets only, we know
21790Sstevel@tonic-gate 			 * that non-terminal destination options only precede
21800Sstevel@tonic-gate 			 * routing headers.
21810Sstevel@tonic-gate 			 */
21820Sstevel@tonic-gate 			divpoint -= ipp.ipp_dstoptslen;
21830Sstevel@tonic-gate 		}
21840Sstevel@tonic-gate 		datalen = ntohs(ip6h->ip6_plen) + sizeof (ip6_t) - divpoint;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 		if (ipp.ipp_rthdr != NULL) {
21870Sstevel@tonic-gate 			nhp = &ipp.ipp_rthdr->ip6r_nxt;
21880Sstevel@tonic-gate 		} else if (ipp.ipp_hopopts != NULL) {
21890Sstevel@tonic-gate 			nhp = &ipp.ipp_hopopts->ip6h_nxt;
21900Sstevel@tonic-gate 		} else {
21910Sstevel@tonic-gate 			ASSERT(divpoint == sizeof (ip6_t));
21920Sstevel@tonic-gate 			/* It's probably IP + ESP. */
21930Sstevel@tonic-gate 			nhp = &ip6h->ip6_nxt;
21940Sstevel@tonic-gate 		}
21950Sstevel@tonic-gate 	}
21960Sstevel@tonic-gate 	assoc = io->ipsec_out_esp_sa;
21970Sstevel@tonic-gate 	ASSERT(assoc != NULL);
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 	if (assoc->ipsa_usetime == 0)
22000Sstevel@tonic-gate 		esp_set_usetime(assoc, B_FALSE);
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != SADB_AALG_NONE)
22030Sstevel@tonic-gate 		mac_len = assoc->ipsa_mac_len;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	if (assoc->ipsa_flags & IPSA_F_NATT) {
22060Sstevel@tonic-gate 		/* wedge in fake UDP */
22070Sstevel@tonic-gate 		is_natt = B_TRUE;
22080Sstevel@tonic-gate 		esplen += UDPH_SIZE;
22090Sstevel@tonic-gate 	}
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg != SADB_EALG_NULL)
22120Sstevel@tonic-gate 		iv_len = assoc->ipsa_iv_len;
2213*134Sdanmcd 
22140Sstevel@tonic-gate 	/*
22150Sstevel@tonic-gate 	 * Set up ESP header and encryption padding for ENCR PI request.
22160Sstevel@tonic-gate 	 */
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	/*
22190Sstevel@tonic-gate 	 * Determine the padding length.   Pad to 4-bytes.
22200Sstevel@tonic-gate 	 *
22210Sstevel@tonic-gate 	 * Include the two additional bytes (hence the - 2) for the padding
22220Sstevel@tonic-gate 	 * length and the next header.  Take this into account when
22230Sstevel@tonic-gate 	 * calculating the actual length of the padding.
22240Sstevel@tonic-gate 	 */
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg != SADB_EALG_NULL) {
22270Sstevel@tonic-gate 		padlen = ((unsigned)(iv_len - datalen - 2)) % iv_len;
22280Sstevel@tonic-gate 	} else {
22290Sstevel@tonic-gate 		padlen = ((unsigned)(sizeof (uint32_t) - datalen - 2)) %
22300Sstevel@tonic-gate 		    sizeof (uint32_t);
22310Sstevel@tonic-gate 	}
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 	/* Allocate ESP header and IV. */
22340Sstevel@tonic-gate 	esplen += iv_len;
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	/*
22370Sstevel@tonic-gate 	 * Update association byte-count lifetimes.  Don't forget to take
22380Sstevel@tonic-gate 	 * into account the padding length and next-header (hence the + 2).
2239*134Sdanmcd 	 *
22400Sstevel@tonic-gate 	 * Use the amount of data fed into the "encryption algorithm".  This
22410Sstevel@tonic-gate 	 * is the IV, the data length, the padding length, and the final two
22420Sstevel@tonic-gate 	 * bytes (padlen, and next-header).
22430Sstevel@tonic-gate 	 *
22440Sstevel@tonic-gate 	 */
22450Sstevel@tonic-gate 
2246*134Sdanmcd 	if (!esp_age_bytes(assoc, datalen + padlen + iv_len + 2, B_FALSE)) {
22470Sstevel@tonic-gate 		/*
22480Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
22490Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
22500Sstevel@tonic-gate 		 */
22510Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
22520Sstevel@tonic-gate 		    &ipdrops_esp_bytes_expire, &esp_dropper);
22530Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
22540Sstevel@tonic-gate 	}
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 	espmp = allocb(esplen, BPRI_HI);
22570Sstevel@tonic-gate 	if (espmp == NULL) {
22580Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
22590Sstevel@tonic-gate 		esp1dbg(("esp_outbound: can't allocate espmp.\n"));
22600Sstevel@tonic-gate 		/*
22610Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
22620Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
22630Sstevel@tonic-gate 		 */
22640Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_nomem,
22650Sstevel@tonic-gate 		    &esp_dropper);
22660Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate 	espmp->b_wptr += esplen;
22690Sstevel@tonic-gate 	esph = (esph_t *)espmp->b_rptr;
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	if (is_natt) {
22720Sstevel@tonic-gate 		esp3dbg(("esp_outbound: NATT"));
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 		udpha = (udpha_t *)espmp->b_rptr;
22750Sstevel@tonic-gate 		udpha->uha_src_port = htons(IPPORT_IKE_NATT);
22760Sstevel@tonic-gate 		if (assoc->ipsa_remote_port != 0)
22770Sstevel@tonic-gate 			udpha->uha_dst_port = assoc->ipsa_remote_port;
22780Sstevel@tonic-gate 		else
22790Sstevel@tonic-gate 			udpha->uha_dst_port = htons(IPPORT_IKE_NATT);
22800Sstevel@tonic-gate 		/*
22810Sstevel@tonic-gate 		 * Set the checksum to 0, so that the ip_wput_ipsec_out()
22820Sstevel@tonic-gate 		 * can do the right thing.
22830Sstevel@tonic-gate 		 */
22840Sstevel@tonic-gate 		udpha->uha_checksum = 0;
22850Sstevel@tonic-gate 		esph = (esph_t *)(udpha + 1);
22860Sstevel@tonic-gate 	}
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	esph->esph_spi = assoc->ipsa_spi;
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 	esph->esph_replay = htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
22910Sstevel@tonic-gate 	if (esph->esph_replay == 0 && assoc->ipsa_replay_wsize != 0) {
22920Sstevel@tonic-gate 		/*
22930Sstevel@tonic-gate 		 * XXX We have replay counter wrapping.
22940Sstevel@tonic-gate 		 * We probably want to nuke this SA (and its peer).
22950Sstevel@tonic-gate 		 */
22960Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
22970Sstevel@tonic-gate 		    SL_ERROR | SL_CONSOLE | SL_WARN,
22980Sstevel@tonic-gate 		    "Outbound ESP SA (0x%x, %s) has wrapped sequence.\n",
22990Sstevel@tonic-gate 		    esph->esph_spi, assoc->ipsa_dstaddr, af);
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
23020Sstevel@tonic-gate 		sadb_replay_delete(assoc);
23030Sstevel@tonic-gate 		/*
23040Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
23050Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
23060Sstevel@tonic-gate 		 */
23070Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_replay,
23080Sstevel@tonic-gate 		    &esp_dropper);
23090Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
23100Sstevel@tonic-gate 	}
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	/*
23130Sstevel@tonic-gate 	 * Set the IV to a random quantity.  We do not require the
23140Sstevel@tonic-gate 	 * highest quality random bits, but for best security with CBC
23150Sstevel@tonic-gate 	 * mode ciphers, the value must be unlikely to repeat and also
23160Sstevel@tonic-gate 	 * must not be known in advance to an adversary capable of
23170Sstevel@tonic-gate 	 * influencing the plaintext.
23180Sstevel@tonic-gate 	 */
23190Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)(esph + 1), iv_len);
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	/* Fix the IP header. */
23220Sstevel@tonic-gate 	alloclen = padlen + 2 + mac_len;
23230Sstevel@tonic-gate 	adj = alloclen + (espmp->b_wptr - espmp->b_rptr);
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 	protocol = *nhp;
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	if (io->ipsec_out_v4) {
23280Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) + adj);
23290Sstevel@tonic-gate 		if (is_natt) {
23300Sstevel@tonic-gate 			*nhp = IPPROTO_UDP;
23310Sstevel@tonic-gate 			udpha->uha_length = htons(ntohs(ipha->ipha_length) -
23320Sstevel@tonic-gate 			    IPH_HDR_LENGTH(ipha));
23330Sstevel@tonic-gate 		} else {
23340Sstevel@tonic-gate 			*nhp = IPPROTO_ESP;
23350Sstevel@tonic-gate 		}
23360Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = 0;
23370Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
23380Sstevel@tonic-gate 	} else {
23390Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) + adj);
23400Sstevel@tonic-gate 		*nhp = IPPROTO_ESP;
23410Sstevel@tonic-gate 	}
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	/* I've got the two ESP mblks, now insert them. */
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 	esp2dbg(("data_mp before outbound ESP adjustment:\n"));
23460Sstevel@tonic-gate 	esp2dbg((dump_msg(data_mp)));
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	if (!esp_insert_esp(data_mp, espmp, divpoint)) {
23490Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
23500Sstevel@tonic-gate 		/* NOTE:  esp_insert_esp() only fails if there's no memory. */
23510Sstevel@tonic-gate 		/*
23520Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
23530Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
23540Sstevel@tonic-gate 		 */
23550Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_nomem,
23560Sstevel@tonic-gate 		    &esp_dropper);
23570Sstevel@tonic-gate 		freeb(espmp);
23580Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
23590Sstevel@tonic-gate 	}
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	/* Append padding (and leave room for ICV). */
23620Sstevel@tonic-gate 	for (tailmp = data_mp; tailmp->b_cont != NULL; tailmp = tailmp->b_cont)
23630Sstevel@tonic-gate 		;
23640Sstevel@tonic-gate 	if (tailmp->b_wptr + alloclen > tailmp->b_datap->db_lim) {
23650Sstevel@tonic-gate 		tailmp->b_cont = allocb(alloclen, BPRI_HI);
23660Sstevel@tonic-gate 		if (tailmp->b_cont == NULL) {
23670Sstevel@tonic-gate 			ESP_BUMP_STAT(out_discards);
23680Sstevel@tonic-gate 			esp0dbg(("esp_outbound:  Can't allocate tailmp.\n"));
23690Sstevel@tonic-gate 			/*
23700Sstevel@tonic-gate 			 * TODO:  Find the outbound IRE for this packet and
23710Sstevel@tonic-gate 			 * pass it to ip_drop_packet().
23720Sstevel@tonic-gate 			 */
23730Sstevel@tonic-gate 			ip_drop_packet(mp, B_FALSE, NULL, NULL,
23740Sstevel@tonic-gate 			    &ipdrops_esp_nomem, &esp_dropper);
23750Sstevel@tonic-gate 			return (IPSEC_STATUS_FAILED);
23760Sstevel@tonic-gate 		}
23770Sstevel@tonic-gate 		tailmp = tailmp->b_cont;
23780Sstevel@tonic-gate 	}
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	/*
23810Sstevel@tonic-gate 	 * If there's padding, N bytes of padding must be of the form 0x1,
23820Sstevel@tonic-gate 	 * 0x2, 0x3... 0xN.
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	for (i = 0; i < padlen; ) {
23850Sstevel@tonic-gate 		i++;
23860Sstevel@tonic-gate 		*tailmp->b_wptr++ = i;
23870Sstevel@tonic-gate 	}
23880Sstevel@tonic-gate 	*tailmp->b_wptr++ = i;
23890Sstevel@tonic-gate 	*tailmp->b_wptr++ = protocol;
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	esp2dbg(("data_Mp before encryption:\n"));
23920Sstevel@tonic-gate 	esp2dbg((dump_msg(data_mp)));
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	/*
23950Sstevel@tonic-gate 	 * The packet is eligible for hardware acceleration if the
23960Sstevel@tonic-gate 	 * following conditions are satisfied:
23970Sstevel@tonic-gate 	 *
23980Sstevel@tonic-gate 	 * 1. the packet will not be fragmented
23990Sstevel@tonic-gate 	 * 2. the provider supports the algorithms specified by SA
24000Sstevel@tonic-gate 	 * 3. there is no pending control message being exchanged
24010Sstevel@tonic-gate 	 * 4. snoop is not attached
24020Sstevel@tonic-gate 	 * 5. the destination address is not a multicast address
24030Sstevel@tonic-gate 	 *
24040Sstevel@tonic-gate 	 * All five of these conditions are checked by IP prior to
24050Sstevel@tonic-gate 	 * sending the packet to ESP.
24060Sstevel@tonic-gate 	 *
24070Sstevel@tonic-gate 	 * But We, and We Alone, can, nay MUST check if the packet
24080Sstevel@tonic-gate 	 * is over NATT, and then disqualify it from hardware
24090Sstevel@tonic-gate 	 * acceleration.
24100Sstevel@tonic-gate 	 */
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	if (io->ipsec_out_is_capab_ill && !(assoc->ipsa_flags & IPSA_F_NATT)) {
24130Sstevel@tonic-gate 		return (esp_outbound_accelerated(ipsec_out_mp, mac_len));
24140Sstevel@tonic-gate 	}
24150Sstevel@tonic-gate 	ESP_BUMP_STAT(noaccel);
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 	/*
24180Sstevel@tonic-gate 	 * Okay.  I've set up the pre-encryption ESP.  Let's do it!
24190Sstevel@tonic-gate 	 */
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	if (mac_len > 0) {
24220Sstevel@tonic-gate 		ASSERT(tailmp->b_wptr + mac_len <= tailmp->b_datap->db_lim);
24230Sstevel@tonic-gate 		icv_buf = tailmp->b_wptr;
24240Sstevel@tonic-gate 		tailmp->b_wptr += mac_len;
24250Sstevel@tonic-gate 	} else {
24260Sstevel@tonic-gate 		icv_buf = NULL;
24270Sstevel@tonic-gate 	}
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 	return (esp_submit_req_outbound(ipsec_out_mp, assoc, icv_buf,
24300Sstevel@tonic-gate 	    datalen + padlen + 2));
24310Sstevel@tonic-gate }
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate /*
24340Sstevel@tonic-gate  * IP calls this to validate the ICMP errors that
24350Sstevel@tonic-gate  * we got from the network.
24360Sstevel@tonic-gate  */
24370Sstevel@tonic-gate ipsec_status_t
24380Sstevel@tonic-gate ipsecesp_icmp_error(mblk_t *ipsec_mp)
24390Sstevel@tonic-gate {
24400Sstevel@tonic-gate 	/*
24410Sstevel@tonic-gate 	 * Unless we get an entire packet back, this function is useless.
24420Sstevel@tonic-gate 	 * Why?
24430Sstevel@tonic-gate 	 *
24440Sstevel@tonic-gate 	 * 1.)	Partial packets are useless, because the "next header"
24450Sstevel@tonic-gate 	 *	is at the end of the decrypted ESP packet.  Without the
24460Sstevel@tonic-gate 	 *	whole packet, this is useless.
24470Sstevel@tonic-gate 	 *
24480Sstevel@tonic-gate 	 * 2.)	If we every use a stateful cipher, such as a stream or a
24490Sstevel@tonic-gate 	 *	one-time pad, we can't do anything.
24500Sstevel@tonic-gate 	 *
24510Sstevel@tonic-gate 	 * Since the chances of us getting an entire packet back are very
24520Sstevel@tonic-gate 	 * very small, we discard here.
24530Sstevel@tonic-gate 	 */
24540Sstevel@tonic-gate 	IP_ESP_BUMP_STAT(in_discards);
24550Sstevel@tonic-gate 	ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_esp_icmp,
24560Sstevel@tonic-gate 	    &esp_dropper);
24570Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
24580Sstevel@tonic-gate }
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate /*
24610Sstevel@tonic-gate  * ESP module read put routine.
24620Sstevel@tonic-gate  */
24630Sstevel@tonic-gate /* ARGSUSED */
24640Sstevel@tonic-gate static void
24650Sstevel@tonic-gate ipsecesp_rput(queue_t *q, mblk_t *mp)
24660Sstevel@tonic-gate {
24670Sstevel@tonic-gate 	keysock_in_t *ksi;
24680Sstevel@tonic-gate 	int *addrtype;
24690Sstevel@tonic-gate 	ire_t *ire;
24700Sstevel@tonic-gate 	mblk_t *ire_mp, *last_mp;
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
24730Sstevel@tonic-gate 	case M_CTL:
24740Sstevel@tonic-gate 		/*
24750Sstevel@tonic-gate 		 * IPsec request of some variety from IP.  IPSEC_{IN,OUT}
24760Sstevel@tonic-gate 		 * are the common cases, but even ICMP error messages from IP
24770Sstevel@tonic-gate 		 * may rise up here.
24780Sstevel@tonic-gate 		 *
24790Sstevel@tonic-gate 		 * Ummmm, actually, this can also be the reflected KEYSOCK_IN
24800Sstevel@tonic-gate 		 * message, with an IRE_DB_TYPE hung off at the end.
24810Sstevel@tonic-gate 		 */
24820Sstevel@tonic-gate 		switch (((ipsec_info_t *)(mp->b_rptr))->ipsec_info_type) {
24830Sstevel@tonic-gate 		case KEYSOCK_IN:
24840Sstevel@tonic-gate 			last_mp = mp;
24850Sstevel@tonic-gate 			while (last_mp->b_cont != NULL &&
24860Sstevel@tonic-gate 			    last_mp->b_cont->b_datap->db_type != IRE_DB_TYPE)
24870Sstevel@tonic-gate 				last_mp = last_mp->b_cont;
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 			if (last_mp->b_cont == NULL) {
24900Sstevel@tonic-gate 				freemsg(mp);
24910Sstevel@tonic-gate 				break;	/* Out of switch. */
24920Sstevel@tonic-gate 			}
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 			ire_mp = last_mp->b_cont;
24950Sstevel@tonic-gate 			last_mp->b_cont = NULL;
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 			ksi = (keysock_in_t *)mp->b_rptr;
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 			if (ksi->ks_in_srctype == KS_IN_ADDR_UNKNOWN)
25000Sstevel@tonic-gate 				addrtype = &ksi->ks_in_srctype;
25010Sstevel@tonic-gate 			else if (ksi->ks_in_dsttype == KS_IN_ADDR_UNKNOWN)
25020Sstevel@tonic-gate 				addrtype = &ksi->ks_in_dsttype;
25030Sstevel@tonic-gate 			else if (ksi->ks_in_proxytype == KS_IN_ADDR_UNKNOWN)
25040Sstevel@tonic-gate 				addrtype = &ksi->ks_in_proxytype;
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 			ire = (ire_t *)ire_mp->b_rptr;
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 			*addrtype = sadb_addrset(ire);
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 			freemsg(ire_mp);
25110Sstevel@tonic-gate 			if (esp_pfkey_q != NULL) {
25120Sstevel@tonic-gate 				/*
25130Sstevel@tonic-gate 				 * Decrement counter to make up for
25140Sstevel@tonic-gate 				 * auto-increment in ipsecesp_wput().
25150Sstevel@tonic-gate 				 * I'm running all MT-hot through here, so
25160Sstevel@tonic-gate 				 * don't worry about perimeters and lateral
25170Sstevel@tonic-gate 				 * puts.
25180Sstevel@tonic-gate 				 */
25190Sstevel@tonic-gate 				ESP_DEBUMP_STAT(keysock_in);
25200Sstevel@tonic-gate 				ipsecesp_wput(WR(esp_pfkey_q), mp);
25210Sstevel@tonic-gate 			} else {
25220Sstevel@tonic-gate 				freemsg(mp);
25230Sstevel@tonic-gate 			}
25240Sstevel@tonic-gate 			break;
25250Sstevel@tonic-gate 		default:
25260Sstevel@tonic-gate 			freemsg(mp);
25270Sstevel@tonic-gate 			break;
25280Sstevel@tonic-gate 		}
25290Sstevel@tonic-gate 		break;
25300Sstevel@tonic-gate 	case M_PROTO:
25310Sstevel@tonic-gate 	case M_PCPROTO:
25320Sstevel@tonic-gate 		/* TPI message of some sort. */
25330Sstevel@tonic-gate 		switch (*((t_scalar_t *)mp->b_rptr)) {
25340Sstevel@tonic-gate 		case T_BIND_ACK:
25350Sstevel@tonic-gate 			esp3dbg(("Thank you IP from ESP for T_BIND_ACK\n"));
25360Sstevel@tonic-gate 			break;
25370Sstevel@tonic-gate 		case T_ERROR_ACK:
25380Sstevel@tonic-gate 			cmn_err(CE_WARN,
25390Sstevel@tonic-gate 			    "ipsecesp:  ESP received T_ERROR_ACK from IP.");
25400Sstevel@tonic-gate 			/*
25410Sstevel@tonic-gate 			 * Make esp_sadb.s_ip_q NULL, and in the
25420Sstevel@tonic-gate 			 * future, perhaps try again.
25430Sstevel@tonic-gate 			 */
25440Sstevel@tonic-gate 			esp_sadb.s_ip_q = NULL;
25450Sstevel@tonic-gate 			break;
25460Sstevel@tonic-gate 		case T_OK_ACK:
25470Sstevel@tonic-gate 			/* Probably from a (rarely sent) T_UNBIND_REQ. */
25480Sstevel@tonic-gate 			break;
25490Sstevel@tonic-gate 		default:
25500Sstevel@tonic-gate 			esp0dbg(("Unknown M_{,PC}PROTO message.\n"));
25510Sstevel@tonic-gate 		}
25520Sstevel@tonic-gate 		freemsg(mp);
25530Sstevel@tonic-gate 		break;
25540Sstevel@tonic-gate 	default:
25550Sstevel@tonic-gate 		/* For now, passthru message. */
25560Sstevel@tonic-gate 		esp2dbg(("ESP got unknown mblk type %d.\n",
25570Sstevel@tonic-gate 		    mp->b_datap->db_type));
25580Sstevel@tonic-gate 		putnext(q, mp);
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate }
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate /*
25630Sstevel@tonic-gate  * Construct an SADB_REGISTER message with the current algorithms.
25640Sstevel@tonic-gate  */
25650Sstevel@tonic-gate static boolean_t
25660Sstevel@tonic-gate esp_register_out(uint32_t sequence, uint32_t pid, uint_t serial)
25670Sstevel@tonic-gate {
25680Sstevel@tonic-gate 	mblk_t *pfkey_msg_mp, *keysock_out_mp;
25690Sstevel@tonic-gate 	sadb_msg_t *samsg;
25700Sstevel@tonic-gate 	sadb_supported_t *sasupp_auth = NULL;
25710Sstevel@tonic-gate 	sadb_supported_t *sasupp_encr = NULL;
25720Sstevel@tonic-gate 	sadb_alg_t *saalg;
25730Sstevel@tonic-gate 	uint_t allocsize = sizeof (*samsg);
25740Sstevel@tonic-gate 	uint_t i, numalgs_snap;
25750Sstevel@tonic-gate 	int current_aalgs;
25760Sstevel@tonic-gate 	ipsec_alginfo_t **authalgs;
25770Sstevel@tonic-gate 	uint_t num_aalgs;
25780Sstevel@tonic-gate 	int current_ealgs;
25790Sstevel@tonic-gate 	ipsec_alginfo_t **encralgs;
25800Sstevel@tonic-gate 	uint_t num_ealgs;
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	/* Allocate the KEYSOCK_OUT. */
25830Sstevel@tonic-gate 	keysock_out_mp = sadb_keysock_out(serial);
25840Sstevel@tonic-gate 	if (keysock_out_mp == NULL) {
25850Sstevel@tonic-gate 		esp0dbg(("esp_register_out: couldn't allocate mblk.\n"));
25860Sstevel@tonic-gate 		return (B_FALSE);
25870Sstevel@tonic-gate 	}
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	/*
25900Sstevel@tonic-gate 	 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
25910Sstevel@tonic-gate 	 */
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	mutex_enter(&alg_lock);
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 	/*
25960Sstevel@tonic-gate 	 * Fill SADB_REGISTER message's algorithm descriptors.  Hold
25970Sstevel@tonic-gate 	 * down the lock while filling it.
25980Sstevel@tonic-gate 	 *
25990Sstevel@tonic-gate 	 * Return only valid algorithms, so the number of algorithms
26000Sstevel@tonic-gate 	 * to send up may be less than the number of algorithm entries
26010Sstevel@tonic-gate 	 * in the table.
26020Sstevel@tonic-gate 	 */
26030Sstevel@tonic-gate 	authalgs = ipsec_alglists[IPSEC_ALG_AUTH];
26040Sstevel@tonic-gate 	for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
26050Sstevel@tonic-gate 		if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
26060Sstevel@tonic-gate 			num_aalgs++;
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 	if (num_aalgs != 0) {
26090Sstevel@tonic-gate 		allocsize += (num_aalgs * sizeof (*saalg));
26100Sstevel@tonic-gate 		allocsize += sizeof (*sasupp_auth);
26110Sstevel@tonic-gate 	}
26120Sstevel@tonic-gate 	encralgs = ipsec_alglists[IPSEC_ALG_ENCR];
26130Sstevel@tonic-gate 	for (num_ealgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
26140Sstevel@tonic-gate 		if (encralgs[i] != NULL && ALG_VALID(encralgs[i]))
26150Sstevel@tonic-gate 			num_ealgs++;
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 	if (num_ealgs != 0) {
26180Sstevel@tonic-gate 		allocsize += (num_ealgs * sizeof (*saalg));
26190Sstevel@tonic-gate 		allocsize += sizeof (*sasupp_encr);
26200Sstevel@tonic-gate 	}
26210Sstevel@tonic-gate 	keysock_out_mp->b_cont = allocb(allocsize, BPRI_HI);
26220Sstevel@tonic-gate 	if (keysock_out_mp->b_cont == NULL) {
26230Sstevel@tonic-gate 		mutex_exit(&alg_lock);
26240Sstevel@tonic-gate 		freemsg(keysock_out_mp);
26250Sstevel@tonic-gate 		return (B_FALSE);
26260Sstevel@tonic-gate 	}
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 	pfkey_msg_mp = keysock_out_mp->b_cont;
26290Sstevel@tonic-gate 	pfkey_msg_mp->b_wptr += allocsize;
26300Sstevel@tonic-gate 	if (num_aalgs != 0) {
26310Sstevel@tonic-gate 		sasupp_auth = (sadb_supported_t *)
26320Sstevel@tonic-gate 		    (pfkey_msg_mp->b_rptr + sizeof (*samsg));
26330Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(sasupp_auth + 1);
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 		ASSERT(((ulong_t)saalg & 0x7) == 0);
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 		numalgs_snap = 0;
26380Sstevel@tonic-gate 		for (i = 0;
26390Sstevel@tonic-gate 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs)); i++) {
26400Sstevel@tonic-gate 			if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
26410Sstevel@tonic-gate 				continue;
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 			saalg->sadb_alg_id = authalgs[i]->alg_id;
26440Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = 0;
26450Sstevel@tonic-gate 			saalg->sadb_alg_minbits	= authalgs[i]->alg_ef_minbits;
26460Sstevel@tonic-gate 			saalg->sadb_alg_maxbits	= authalgs[i]->alg_ef_maxbits;
26470Sstevel@tonic-gate 			saalg->sadb_x_alg_defincr = authalgs[i]->alg_ef_default;
26480Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
26490Sstevel@tonic-gate 			    authalgs[i]->alg_increment;
26500Sstevel@tonic-gate 			numalgs_snap++;
26510Sstevel@tonic-gate 			saalg++;
26520Sstevel@tonic-gate 		}
26530Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_aalgs);
26540Sstevel@tonic-gate #ifdef DEBUG
26550Sstevel@tonic-gate 		/*
26560Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
26570Sstevel@tonic-gate 		 * algorithms.
26580Sstevel@tonic-gate 		 */
26590Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++) {
26600Sstevel@tonic-gate 			if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) {
26610Sstevel@tonic-gate 				cmn_err(CE_PANIC, "esp_register_out()! "
26620Sstevel@tonic-gate 				    "Missed aalg #%d.\n", i);
26630Sstevel@tonic-gate 			}
26640Sstevel@tonic-gate 		}
26650Sstevel@tonic-gate #endif /* DEBUG */
26660Sstevel@tonic-gate 	} else {
26670Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(pfkey_msg_mp->b_rptr + sizeof (*samsg));
26680Sstevel@tonic-gate 	}
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	if (num_ealgs != 0) {
26710Sstevel@tonic-gate 		sasupp_encr = (sadb_supported_t *)saalg;
26720Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(sasupp_encr + 1);
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 		numalgs_snap = 0;
26750Sstevel@tonic-gate 		for (i = 0;
26760Sstevel@tonic-gate 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_ealgs)); i++) {
26770Sstevel@tonic-gate 			if (encralgs[i] == NULL || !ALG_VALID(encralgs[i]))
26780Sstevel@tonic-gate 				continue;
26790Sstevel@tonic-gate 			saalg->sadb_alg_id = encralgs[i]->alg_id;
26800Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = encralgs[i]->alg_datalen;
26810Sstevel@tonic-gate 			saalg->sadb_alg_minbits	= encralgs[i]->alg_ef_minbits;
26820Sstevel@tonic-gate 			saalg->sadb_alg_maxbits	= encralgs[i]->alg_ef_maxbits;
26830Sstevel@tonic-gate 			saalg->sadb_x_alg_defincr = encralgs[i]->alg_ef_default;
26840Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
26850Sstevel@tonic-gate 			    encralgs[i]->alg_increment;
26860Sstevel@tonic-gate 			numalgs_snap++;
26870Sstevel@tonic-gate 			saalg++;
26880Sstevel@tonic-gate 		}
26890Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_ealgs);
26900Sstevel@tonic-gate #ifdef DEBUG
26910Sstevel@tonic-gate 		/*
26920Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
26930Sstevel@tonic-gate 		 * algorithms.
26940Sstevel@tonic-gate 		 */
26950Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++) {
26960Sstevel@tonic-gate 			if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) {
26970Sstevel@tonic-gate 				cmn_err(CE_PANIC, "esp_register_out()! "
26980Sstevel@tonic-gate 				    "Missed ealg #%d.\n", i);
26990Sstevel@tonic-gate 			}
27000Sstevel@tonic-gate 		}
27010Sstevel@tonic-gate #endif /* DEBUG */
27020Sstevel@tonic-gate 	}
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 	current_aalgs = num_aalgs;
27050Sstevel@tonic-gate 	current_ealgs = num_ealgs;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	mutex_exit(&alg_lock);
27080Sstevel@tonic-gate 
27090Sstevel@tonic-gate 	/* Now fill the rest of the SADB_REGISTER message. */
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	samsg = (sadb_msg_t *)pfkey_msg_mp->b_rptr;
27120Sstevel@tonic-gate 	samsg->sadb_msg_version = PF_KEY_V2;
27130Sstevel@tonic-gate 	samsg->sadb_msg_type = SADB_REGISTER;
27140Sstevel@tonic-gate 	samsg->sadb_msg_errno = 0;
27150Sstevel@tonic-gate 	samsg->sadb_msg_satype = SADB_SATYPE_ESP;
27160Sstevel@tonic-gate 	samsg->sadb_msg_len = SADB_8TO64(allocsize);
27170Sstevel@tonic-gate 	samsg->sadb_msg_reserved = 0;
27180Sstevel@tonic-gate 	/*
27190Sstevel@tonic-gate 	 * Assume caller has sufficient sequence/pid number info.  If it's one
27200Sstevel@tonic-gate 	 * from me over a new alg., I could give two hoots about sequence.
27210Sstevel@tonic-gate 	 */
27220Sstevel@tonic-gate 	samsg->sadb_msg_seq = sequence;
27230Sstevel@tonic-gate 	samsg->sadb_msg_pid = pid;
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	if (sasupp_auth != NULL) {
27260Sstevel@tonic-gate 		sasupp_auth->sadb_supported_len =
27270Sstevel@tonic-gate 		    SADB_8TO64(sizeof (*sasupp_auth) +
27280Sstevel@tonic-gate 			sizeof (*saalg) * current_aalgs);
27290Sstevel@tonic-gate 		sasupp_auth->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
27300Sstevel@tonic-gate 		sasupp_auth->sadb_supported_reserved = 0;
27310Sstevel@tonic-gate 	}
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 	if (sasupp_encr != NULL) {
27340Sstevel@tonic-gate 		sasupp_encr->sadb_supported_len =
27350Sstevel@tonic-gate 		    SADB_8TO64(sizeof (*sasupp_encr) +
27360Sstevel@tonic-gate 			sizeof (*saalg) * current_ealgs);
27370Sstevel@tonic-gate 		sasupp_encr->sadb_supported_exttype =
27380Sstevel@tonic-gate 		    SADB_EXT_SUPPORTED_ENCRYPT;
27390Sstevel@tonic-gate 		sasupp_encr->sadb_supported_reserved = 0;
27400Sstevel@tonic-gate 	}
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	if (esp_pfkey_q != NULL)
27430Sstevel@tonic-gate 		putnext(esp_pfkey_q, keysock_out_mp);
27440Sstevel@tonic-gate 	else {
27450Sstevel@tonic-gate 		freemsg(keysock_out_mp);
27460Sstevel@tonic-gate 		return (B_FALSE);
27470Sstevel@tonic-gate 	}
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	return (B_TRUE);
27500Sstevel@tonic-gate }
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate /*
27530Sstevel@tonic-gate  * Invoked when the algorithm table changes. Causes SADB_REGISTER
27540Sstevel@tonic-gate  * messages continaining the current list of algorithms to be
27550Sstevel@tonic-gate  * sent up to the ESP listeners.
27560Sstevel@tonic-gate  */
27570Sstevel@tonic-gate void
27580Sstevel@tonic-gate ipsecesp_algs_changed(void)
27590Sstevel@tonic-gate {
27600Sstevel@tonic-gate 	/*
27610Sstevel@tonic-gate 	 * Time to send a PF_KEY SADB_REGISTER message to ESP listeners
27620Sstevel@tonic-gate 	 * everywhere.  (The function itself checks for NULL esp_pfkey_q.)
27630Sstevel@tonic-gate 	 */
27640Sstevel@tonic-gate 	(void) esp_register_out(0, 0, 0);
27650Sstevel@tonic-gate }
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate /*
27680Sstevel@tonic-gate  * taskq_dispatch handler.
27690Sstevel@tonic-gate  */
27700Sstevel@tonic-gate static void
27710Sstevel@tonic-gate inbound_task(void *arg)
27720Sstevel@tonic-gate {
27730Sstevel@tonic-gate 	esph_t *esph;
27740Sstevel@tonic-gate 	mblk_t *mp = (mblk_t *)arg;
27750Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr;
27760Sstevel@tonic-gate 	int ipsec_rc;
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	esp2dbg(("in ESP inbound_task"));
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 	esph = ipsec_inbound_esp_sa(mp);
27810Sstevel@tonic-gate 	if (esph == NULL)
27820Sstevel@tonic-gate 		return;
27830Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_esp_sa != NULL);
27840Sstevel@tonic-gate 	ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func(mp, esph);
27850Sstevel@tonic-gate 	if (ipsec_rc != IPSEC_STATUS_SUCCESS)
27860Sstevel@tonic-gate 		return;
27870Sstevel@tonic-gate 	ip_fanout_proto_again(mp, NULL, NULL, NULL);
27880Sstevel@tonic-gate }
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate /*
27910Sstevel@tonic-gate  * Now that weak-key passed, actually ADD the security association, and
27920Sstevel@tonic-gate  * send back a reply ADD message.
27930Sstevel@tonic-gate  */
27940Sstevel@tonic-gate static int
27950Sstevel@tonic-gate esp_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi)
27960Sstevel@tonic-gate {
27970Sstevel@tonic-gate 	isaf_t *primary, *secondary, *inbound;
27980Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
27990Sstevel@tonic-gate 	sadb_address_t *dstext =
28000Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
28010Sstevel@tonic-gate 	struct sockaddr_in *dst;
28020Sstevel@tonic-gate 	struct sockaddr_in6 *dst6;
28030Sstevel@tonic-gate 	boolean_t is_ipv4, clone = B_FALSE, is_inbound = B_FALSE;
28040Sstevel@tonic-gate 	uint32_t *dstaddr;
28050Sstevel@tonic-gate 	ipsa_t *larval = NULL;
28060Sstevel@tonic-gate 	ipsacq_t *acqrec;
28070Sstevel@tonic-gate 	iacqf_t *acq_bucket;
28080Sstevel@tonic-gate 	mblk_t *acq_msgs = NULL;
28090Sstevel@tonic-gate 	int rc;
28100Sstevel@tonic-gate 	sadb_t *sp;
28110Sstevel@tonic-gate 	int outhash;
28120Sstevel@tonic-gate 	mblk_t *lpkt;
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 	/*
28150Sstevel@tonic-gate 	 * Locate the appropriate table(s).
28160Sstevel@tonic-gate 	 */
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
28190Sstevel@tonic-gate 	dst6 = (struct sockaddr_in6 *)dst;
28200Sstevel@tonic-gate 	is_ipv4 = (dst->sin_family == AF_INET);
28210Sstevel@tonic-gate 	if (is_ipv4) {
28220Sstevel@tonic-gate 		sp = &esp_sadb.s_v4;
28230Sstevel@tonic-gate 		dstaddr = (uint32_t *)(&dst->sin_addr);
28240Sstevel@tonic-gate 		outhash = OUTBOUND_HASH_V4(*(ipaddr_t *)dstaddr);
28250Sstevel@tonic-gate 	} else {
28260Sstevel@tonic-gate 		sp = &esp_sadb.s_v6;
28270Sstevel@tonic-gate 		dstaddr = (uint32_t *)(&dst6->sin6_addr);
28280Sstevel@tonic-gate 		outhash = OUTBOUND_HASH_V6(*(in6_addr_t *)dstaddr);
28290Sstevel@tonic-gate 	}
28300Sstevel@tonic-gate 	inbound = &sp->sdb_if[INBOUND_HASH(assoc->sadb_sa_spi)];
28310Sstevel@tonic-gate 	switch (ksi->ks_in_dsttype) {
28320Sstevel@tonic-gate 	case KS_IN_ADDR_MBCAST:
28330Sstevel@tonic-gate 		clone = B_TRUE;	/* All mcast SAs can be bidirectional */
28340Sstevel@tonic-gate 		/* FALLTHRU */
28350Sstevel@tonic-gate 	case KS_IN_ADDR_ME:
28360Sstevel@tonic-gate 		primary = inbound;
28370Sstevel@tonic-gate 		secondary = &sp->sdb_of[outhash];
28380Sstevel@tonic-gate 		/*
28390Sstevel@tonic-gate 		 * If the source address is either one of mine, or unspecified
28400Sstevel@tonic-gate 		 * (which is best summed up by saying "not 'not mine'"),
28410Sstevel@tonic-gate 		 * then the association is potentially bi-directional,
28420Sstevel@tonic-gate 		 * in that it can be used for inbound traffic and outbound
28430Sstevel@tonic-gate 		 * traffic.  The best example of such an SA is a multicast
28440Sstevel@tonic-gate 		 * SA (which allows me to receive the outbound traffic).
28450Sstevel@tonic-gate 		 */
28460Sstevel@tonic-gate 		if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
28470Sstevel@tonic-gate 			clone = B_TRUE;
28480Sstevel@tonic-gate 		is_inbound = B_TRUE;
28490Sstevel@tonic-gate 		break;
28500Sstevel@tonic-gate 	case KS_IN_ADDR_NOTME:
28510Sstevel@tonic-gate 		primary = &sp->sdb_of[outhash];
28520Sstevel@tonic-gate 		secondary = inbound;
28530Sstevel@tonic-gate 		/*
28540Sstevel@tonic-gate 		 * If the source address literally not mine (either
28550Sstevel@tonic-gate 		 * unspecified or not mine), then this SA may have an
28560Sstevel@tonic-gate 		 * address that WILL be mine after some configuration.
28570Sstevel@tonic-gate 		 * We pay the price for this by making it a bi-directional
28580Sstevel@tonic-gate 		 * SA.
28590Sstevel@tonic-gate 		 */
28600Sstevel@tonic-gate 		if (ksi->ks_in_srctype != KS_IN_ADDR_ME)
28610Sstevel@tonic-gate 			clone = B_TRUE;
28620Sstevel@tonic-gate 		break;
28630Sstevel@tonic-gate 	default:
28640Sstevel@tonic-gate 		samsg->sadb_x_msg_diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
28650Sstevel@tonic-gate 		return (EINVAL);
28660Sstevel@tonic-gate 	}
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 	/*
28690Sstevel@tonic-gate 	 * Find a ACQUIRE list entry if possible.  If we've added an SA that
28700Sstevel@tonic-gate 	 * suits the needs of an ACQUIRE list entry, we can eliminate the
28710Sstevel@tonic-gate 	 * ACQUIRE list entry and transmit the enqueued packets.  Use the
28720Sstevel@tonic-gate 	 * high-bit of the sequence number to queue it.  Key off destination
28730Sstevel@tonic-gate 	 * addr, and change acqrec's state.
28740Sstevel@tonic-gate 	 */
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 	if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
28770Sstevel@tonic-gate 		acq_bucket = &sp->sdb_acq[outhash];
28780Sstevel@tonic-gate 		mutex_enter(&acq_bucket->iacqf_lock);
28790Sstevel@tonic-gate 		for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
28800Sstevel@tonic-gate 		    acqrec = acqrec->ipsacq_next) {
28810Sstevel@tonic-gate 			mutex_enter(&acqrec->ipsacq_lock);
28820Sstevel@tonic-gate 			/*
28830Sstevel@tonic-gate 			 * Q:  I only check sequence.  Should I check dst?
28840Sstevel@tonic-gate 			 * A: Yes, check dest because those are the packets
28850Sstevel@tonic-gate 			 *    that are queued up.
28860Sstevel@tonic-gate 			 */
28870Sstevel@tonic-gate 			if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
28880Sstevel@tonic-gate 			    IPSA_ARE_ADDR_EQUAL(dstaddr,
28890Sstevel@tonic-gate 				acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
28900Sstevel@tonic-gate 				break;
28910Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
28920Sstevel@tonic-gate 		}
28930Sstevel@tonic-gate 		if (acqrec != NULL) {
28940Sstevel@tonic-gate 			/*
28950Sstevel@tonic-gate 			 * AHA!  I found an ACQUIRE record for this SA.
28960Sstevel@tonic-gate 			 * Grab the msg list, and free the acquire record.
28970Sstevel@tonic-gate 			 * I already am holding the lock for this record,
28980Sstevel@tonic-gate 			 * so all I have to do is free it.
28990Sstevel@tonic-gate 			 */
29000Sstevel@tonic-gate 			acq_msgs = acqrec->ipsacq_mp;
29010Sstevel@tonic-gate 			acqrec->ipsacq_mp = NULL;
29020Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
29030Sstevel@tonic-gate 			sadb_destroy_acquire(acqrec);
29040Sstevel@tonic-gate 		}
29050Sstevel@tonic-gate 		mutex_exit(&acq_bucket->iacqf_lock);
29060Sstevel@tonic-gate 	}
29070Sstevel@tonic-gate 
29080Sstevel@tonic-gate 	/*
29090Sstevel@tonic-gate 	 * Find PF_KEY message, and see if I'm an update.  If so, find entry
29100Sstevel@tonic-gate 	 * in larval list (if there).
29110Sstevel@tonic-gate 	 */
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_UPDATE) {
29140Sstevel@tonic-gate 		mutex_enter(&inbound->isaf_lock);
29150Sstevel@tonic-gate 		larval = ipsec_getassocbyspi(inbound, assoc->sadb_sa_spi,
29160Sstevel@tonic-gate 		    ALL_ZEROES_PTR, dstaddr, dst->sin_family);
29170Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 		if (larval == NULL) {
29200Sstevel@tonic-gate 			esp0dbg(("Larval update, but larval disappeared.\n"));
29210Sstevel@tonic-gate 			return (ESRCH);
29220Sstevel@tonic-gate 		} /* Else sadb_common_add unlinks it for me! */
29230Sstevel@tonic-gate 	}
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate 	lpkt = NULL;
29260Sstevel@tonic-gate 	if (larval != NULL)
29270Sstevel@tonic-gate 		lpkt = sadb_clear_lpkt(larval);
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	rc = sadb_common_add(esp_sadb.s_ip_q, esp_pfkey_q, mp, samsg, ksi,
29300Sstevel@tonic-gate 	    primary, secondary, larval, clone, is_inbound);
29310Sstevel@tonic-gate 
29320Sstevel@tonic-gate 	if (rc == 0 && lpkt != NULL) {
29330Sstevel@tonic-gate 		rc = !taskq_dispatch(esp_taskq, inbound_task,
29340Sstevel@tonic-gate 			    (void *) lpkt, TQ_NOSLEEP);
29350Sstevel@tonic-gate 	}
29360Sstevel@tonic-gate 
29370Sstevel@tonic-gate 	if (rc != 0) {
29380Sstevel@tonic-gate 		ip_drop_packet(lpkt, B_TRUE, NULL, NULL,
29390Sstevel@tonic-gate 		    &ipdrops_sadb_inlarval_timeout, &esp_dropper);
29400Sstevel@tonic-gate 	}
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	/*
29430Sstevel@tonic-gate 	 * How much more stack will I create with all of these
29440Sstevel@tonic-gate 	 * esp_outbound() calls?
29450Sstevel@tonic-gate 	 */
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	while (acq_msgs != NULL) {
29480Sstevel@tonic-gate 		mblk_t *mp = acq_msgs;
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 		acq_msgs = acq_msgs->b_next;
29510Sstevel@tonic-gate 		mp->b_next = NULL;
29520Sstevel@tonic-gate 		if (rc == 0) {
29530Sstevel@tonic-gate 			if (ipsec_outbound_sa(mp, IPPROTO_ESP)) {
29540Sstevel@tonic-gate 				((ipsec_out_t *)(mp->b_rptr))->
29550Sstevel@tonic-gate 				    ipsec_out_esp_done = B_TRUE;
29560Sstevel@tonic-gate 				if (esp_outbound(mp) == IPSEC_STATUS_SUCCESS) {
29570Sstevel@tonic-gate 					ipha_t *ipha = (ipha_t *)
29580Sstevel@tonic-gate 					    mp->b_cont->b_rptr;
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate 					/* do AH processing if needed */
29610Sstevel@tonic-gate 					if (!esp_do_outbound_ah(mp))
29620Sstevel@tonic-gate 						continue;
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 					/* finish IPsec processing */
29650Sstevel@tonic-gate 					if (is_ipv4) {
29660Sstevel@tonic-gate 						ip_wput_ipsec_out(NULL, mp,
29670Sstevel@tonic-gate 						    ipha, NULL, NULL);
29680Sstevel@tonic-gate 					} else {
29690Sstevel@tonic-gate 						ip6_t *ip6h = (ip6_t *)ipha;
29700Sstevel@tonic-gate 						ip_wput_ipsec_out_v6(NULL,
29710Sstevel@tonic-gate 						    mp, ip6h, NULL, NULL);
29720Sstevel@tonic-gate 					}
29730Sstevel@tonic-gate 				}
29740Sstevel@tonic-gate 				continue;
29750Sstevel@tonic-gate 			}
29760Sstevel@tonic-gate 		}
29770Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
29780Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
29790Sstevel@tonic-gate 		    &ipdrops_sadb_acquire_timeout, &esp_dropper);
29800Sstevel@tonic-gate 	}
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	return (rc);
29830Sstevel@tonic-gate }
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate /*
29860Sstevel@tonic-gate  * Add new ESP security association.  This may become a generic AH/ESP
29870Sstevel@tonic-gate  * routine eventually.
29880Sstevel@tonic-gate  */
29890Sstevel@tonic-gate static int
29900Sstevel@tonic-gate esp_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic)
29910Sstevel@tonic-gate {
29920Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
29930Sstevel@tonic-gate 	sadb_address_t *srcext =
29940Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
29950Sstevel@tonic-gate 	sadb_address_t *dstext =
29960Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
29970Sstevel@tonic-gate 	sadb_address_t *nttext_loc =
29980Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC];
29990Sstevel@tonic-gate 	sadb_address_t *nttext_rem =
30000Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM];
30010Sstevel@tonic-gate 	sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
30020Sstevel@tonic-gate 	sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT];
30030Sstevel@tonic-gate 	struct sockaddr_in *src, *dst;
30040Sstevel@tonic-gate 	struct sockaddr_in *natt_loc, *natt_rem;
30050Sstevel@tonic-gate 	struct sockaddr_in6 *natt_loc6, *natt_rem6;
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate 	sadb_lifetime_t *soft =
30080Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
30090Sstevel@tonic-gate 	sadb_lifetime_t *hard =
30100Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
30110Sstevel@tonic-gate 
30120Sstevel@tonic-gate 	/* I need certain extensions present for an ADD message. */
30130Sstevel@tonic-gate 	if (srcext == NULL) {
30140Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
30150Sstevel@tonic-gate 		return (EINVAL);
30160Sstevel@tonic-gate 	}
30170Sstevel@tonic-gate 	if (dstext == NULL) {
30180Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
30190Sstevel@tonic-gate 		return (EINVAL);
30200Sstevel@tonic-gate 	}
30210Sstevel@tonic-gate 	if (assoc == NULL) {
30220Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
30230Sstevel@tonic-gate 		return (EINVAL);
30240Sstevel@tonic-gate 	}
30250Sstevel@tonic-gate 	if (ekey == NULL && assoc->sadb_sa_encrypt != SADB_EALG_NULL) {
30260Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_EKEY;
30270Sstevel@tonic-gate 		return (EINVAL);
30280Sstevel@tonic-gate 	}
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	src = (struct sockaddr_in *)(srcext + 1);
30310Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
30320Sstevel@tonic-gate 	natt_loc = (struct sockaddr_in *)(nttext_loc + 1);
30330Sstevel@tonic-gate 	natt_loc6 = (struct sockaddr_in6 *)(nttext_loc + 1);
30340Sstevel@tonic-gate 	natt_rem = (struct sockaddr_in *)(nttext_rem + 1);
30350Sstevel@tonic-gate 	natt_rem6 = (struct sockaddr_in6 *)(nttext_rem + 1);
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 	/* Sundry ADD-specific reality checks. */
30380Sstevel@tonic-gate 	/* XXX STATS :  Logging/stats here? */
30390Sstevel@tonic-gate 	if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
30400Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
30410Sstevel@tonic-gate 		return (EINVAL);
30420Sstevel@tonic-gate 	}
30430Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt == SADB_EALG_NONE) {
30440Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
30450Sstevel@tonic-gate 		return (EINVAL);
30460Sstevel@tonic-gate 	}
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt == SADB_EALG_NULL &&
30490Sstevel@tonic-gate 	    assoc->sadb_sa_auth == SADB_AALG_NONE) {
30500Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
30510Sstevel@tonic-gate 		return (EINVAL);
30520Sstevel@tonic-gate 	}
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & ~(SADB_SAFLAGS_NOREPLAY |
30550Sstevel@tonic-gate 	    SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM)) {
30560Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
30570Sstevel@tonic-gate 		return (EINVAL);
30580Sstevel@tonic-gate 	}
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 	if ((*diagnostic = sadb_hardsoftchk(hard, soft)) != 0) {
30610Sstevel@tonic-gate 		return (EINVAL);
30620Sstevel@tonic-gate 	}
30630Sstevel@tonic-gate 	if (src->sin_family != dst->sin_family) {
30640Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH;
30650Sstevel@tonic-gate 		return (EINVAL);
30660Sstevel@tonic-gate 	}
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) {
30700Sstevel@tonic-gate 		if (nttext_loc == NULL) {
30710Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_LOC;
30720Sstevel@tonic-gate 			return (EINVAL);
30730Sstevel@tonic-gate 		}
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate 		if (natt_loc->sin_family == AF_INET6 &&
30760Sstevel@tonic-gate 		    !IN6_IS_ADDR_V4MAPPED(&natt_loc6->sin6_addr)) {
30770Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC;
30780Sstevel@tonic-gate 			return (EINVAL);
30790Sstevel@tonic-gate 		}
30800Sstevel@tonic-gate 	}
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) {
30830Sstevel@tonic-gate 		if (nttext_rem == NULL) {
30840Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_REM;
30850Sstevel@tonic-gate 			return (EINVAL);
30860Sstevel@tonic-gate 		}
30870Sstevel@tonic-gate 		if (natt_rem->sin_family == AF_INET6 &&
30880Sstevel@tonic-gate 		    !IN6_IS_ADDR_V4MAPPED(&natt_rem6->sin6_addr)) {
30890Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM;
30900Sstevel@tonic-gate 			return (EINVAL);
30910Sstevel@tonic-gate 		}
30920Sstevel@tonic-gate 	}
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate 	/* Stuff I don't support, for now.  XXX Diagnostic? */
30960Sstevel@tonic-gate 	if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL ||
30970Sstevel@tonic-gate 	    ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL)
30980Sstevel@tonic-gate 		return (EOPNOTSUPP);
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 	/*
31010Sstevel@tonic-gate 	 * XXX Policy :  I'm not checking identities or sensitivity
31020Sstevel@tonic-gate 	 * labels at this time, but if I did, I'd do them here, before I sent
31030Sstevel@tonic-gate 	 * the weak key check up to the algorithm.
31040Sstevel@tonic-gate 	 */
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate 	mutex_enter(&alg_lock);
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 	/*
31090Sstevel@tonic-gate 	 * First locate the authentication algorithm.
31100Sstevel@tonic-gate 	 */
31110Sstevel@tonic-gate 	if (akey != NULL) {
31120Sstevel@tonic-gate 		ipsec_alginfo_t *aalg;
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 		aalg = ipsec_alglists[IPSEC_ALG_AUTH][assoc->sadb_sa_auth];
31150Sstevel@tonic-gate 		if (aalg == NULL || !ALG_VALID(aalg)) {
31160Sstevel@tonic-gate 			mutex_exit(&alg_lock);
31170Sstevel@tonic-gate 			esp1dbg(("Couldn't find auth alg #%d.\n",
31180Sstevel@tonic-gate 			    assoc->sadb_sa_auth));
31190Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
31200Sstevel@tonic-gate 			return (EINVAL);
31210Sstevel@tonic-gate 		}
31220Sstevel@tonic-gate 		ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 		/* sanity check key sizes */
31250Sstevel@tonic-gate 		if (!ipsec_valid_key_size(akey->sadb_key_bits, aalg)) {
31260Sstevel@tonic-gate 			mutex_exit(&alg_lock);
31270Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
31280Sstevel@tonic-gate 			return (EINVAL);
31290Sstevel@tonic-gate 		}
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 		/* check key and fix parity if needed */
31320Sstevel@tonic-gate 		if (ipsec_check_key(aalg->alg_mech_type, akey, B_TRUE,
31330Sstevel@tonic-gate 		    diagnostic) != 0) {
31340Sstevel@tonic-gate 			mutex_exit(&alg_lock);
31350Sstevel@tonic-gate 			return (EINVAL);
31360Sstevel@tonic-gate 		}
31370Sstevel@tonic-gate 	}
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 	/*
31400Sstevel@tonic-gate 	 * Then locate the encryption algorithm.
31410Sstevel@tonic-gate 	 */
31420Sstevel@tonic-gate 	if (ekey != NULL) {
31430Sstevel@tonic-gate 		ipsec_alginfo_t *ealg;
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 		ealg = ipsec_alglists[IPSEC_ALG_ENCR][assoc->sadb_sa_encrypt];
31460Sstevel@tonic-gate 		if (ealg == NULL || !ALG_VALID(ealg)) {
31470Sstevel@tonic-gate 			mutex_exit(&alg_lock);
31480Sstevel@tonic-gate 			esp1dbg(("Couldn't find encr alg #%d.\n",
31490Sstevel@tonic-gate 			    assoc->sadb_sa_encrypt));
31500Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
31510Sstevel@tonic-gate 			return (EINVAL);
31520Sstevel@tonic-gate 		}
31530Sstevel@tonic-gate 		ASSERT(ealg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate 		/* sanity check key sizes */
31560Sstevel@tonic-gate 		if (!ipsec_valid_key_size(ekey->sadb_key_bits, ealg)) {
31570Sstevel@tonic-gate 			mutex_exit(&alg_lock);
31580Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_EKEYBITS;
31590Sstevel@tonic-gate 			return (EINVAL);
31600Sstevel@tonic-gate 		}
31610Sstevel@tonic-gate 
31620Sstevel@tonic-gate 		/* check key */
31630Sstevel@tonic-gate 		if (ipsec_check_key(ealg->alg_mech_type, ekey, B_FALSE,
31640Sstevel@tonic-gate 		    diagnostic) != 0) {
31650Sstevel@tonic-gate 			mutex_exit(&alg_lock);
31660Sstevel@tonic-gate 			return (EINVAL);
31670Sstevel@tonic-gate 		}
31680Sstevel@tonic-gate 	}
31690Sstevel@tonic-gate 	mutex_exit(&alg_lock);
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate 	return (esp_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi));
31720Sstevel@tonic-gate }
31730Sstevel@tonic-gate 
31740Sstevel@tonic-gate /*
31750Sstevel@tonic-gate  * Update a security association.  Updates come in two varieties.  The first
31760Sstevel@tonic-gate  * is an update of lifetimes on a non-larval SA.  The second is an update of
31770Sstevel@tonic-gate  * a larval SA, which ends up looking a lot more like an add.
31780Sstevel@tonic-gate  */
31790Sstevel@tonic-gate static int
31800Sstevel@tonic-gate esp_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic)
31810Sstevel@tonic-gate {
31820Sstevel@tonic-gate 	sadb_address_t *dstext =
31830Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
31840Sstevel@tonic-gate 	struct sockaddr_in *sin;
31850Sstevel@tonic-gate 
31860Sstevel@tonic-gate 	if (dstext == NULL) {
31870Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
31880Sstevel@tonic-gate 		return (EINVAL);
31890Sstevel@tonic-gate 	}
31900Sstevel@tonic-gate 
31910Sstevel@tonic-gate 	sin = (struct sockaddr_in *)(dstext + 1);
31920Sstevel@tonic-gate 	return (sadb_update_sa(mp, ksi,
31930Sstevel@tonic-gate 	    (sin->sin_family == AF_INET6) ? &esp_sadb.s_v6 : &esp_sadb.s_v4,
31940Sstevel@tonic-gate 	    diagnostic, esp_pfkey_q, esp_add_sa));
31950Sstevel@tonic-gate }
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate /*
31980Sstevel@tonic-gate  * Delete a security association.  This is REALLY likely to be code common to
31990Sstevel@tonic-gate  * both AH and ESP.  Find the association, then unlink it.
32000Sstevel@tonic-gate  */
32010Sstevel@tonic-gate static int
32020Sstevel@tonic-gate esp_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic)
32030Sstevel@tonic-gate {
32040Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
32050Sstevel@tonic-gate 	sadb_address_t *dstext =
32060Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
32070Sstevel@tonic-gate 	sadb_address_t *srcext =
32080Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
32090Sstevel@tonic-gate 	struct sockaddr_in *sin;
32100Sstevel@tonic-gate 
32110Sstevel@tonic-gate 	if (assoc == NULL) {
32120Sstevel@tonic-gate 		if (dstext != NULL) {
32130Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(dstext + 1);
32140Sstevel@tonic-gate 		} else if (srcext != NULL) {
32150Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(srcext + 1);
32160Sstevel@tonic-gate 		} else {
32170Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
32180Sstevel@tonic-gate 			return (EINVAL);
32190Sstevel@tonic-gate 		}
32200Sstevel@tonic-gate 		return sadb_purge_sa(mp, ksi,
32210Sstevel@tonic-gate 		    (sin->sin_family == AF_INET6) ? &esp_sadb.s_v6 :
32220Sstevel@tonic-gate 		    &esp_sadb.s_v4,
32230Sstevel@tonic-gate 		    diagnostic, esp_pfkey_q, esp_sadb.s_ip_q);
32240Sstevel@tonic-gate 	}
32250Sstevel@tonic-gate 
32260Sstevel@tonic-gate 	return (sadb_del_sa(mp, ksi, &esp_sadb, diagnostic, esp_pfkey_q));
32270Sstevel@tonic-gate }
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate /*
32300Sstevel@tonic-gate  * Convert the entire contents of all of ESP's SA tables into PF_KEY SADB_DUMP
32310Sstevel@tonic-gate  * messages.
32320Sstevel@tonic-gate  */
32330Sstevel@tonic-gate static void
32340Sstevel@tonic-gate esp_dump(mblk_t *mp, keysock_in_t *ksi)
32350Sstevel@tonic-gate {
32360Sstevel@tonic-gate 	int error;
32370Sstevel@tonic-gate 	sadb_msg_t *samsg;
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate 	/*
32400Sstevel@tonic-gate 	 * Dump each fanout, bailing if error is non-zero.
32410Sstevel@tonic-gate 	 */
32420Sstevel@tonic-gate 
32430Sstevel@tonic-gate 	error = sadb_dump(esp_pfkey_q, mp, ksi->ks_in_serial, &esp_sadb.s_v4);
32440Sstevel@tonic-gate 	if (error != 0)
32450Sstevel@tonic-gate 		goto bail;
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 	error = sadb_dump(esp_pfkey_q, mp, ksi->ks_in_serial, &esp_sadb.s_v6);
32480Sstevel@tonic-gate bail:
32490Sstevel@tonic-gate 	ASSERT(mp->b_cont != NULL);
32500Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
32510Sstevel@tonic-gate 	samsg->sadb_msg_errno = (uint8_t)error;
32520Sstevel@tonic-gate 	sadb_pfkey_echo(esp_pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
32530Sstevel@tonic-gate 	    NULL);
32540Sstevel@tonic-gate }
32550Sstevel@tonic-gate 
32560Sstevel@tonic-gate /*
32570Sstevel@tonic-gate  * ESP parsing of PF_KEY messages.  Keysock did most of the really silly
32580Sstevel@tonic-gate  * error cases.  What I receive is a fully-formed, syntactically legal
32590Sstevel@tonic-gate  * PF_KEY message.  I then need to check semantics...
32600Sstevel@tonic-gate  *
32610Sstevel@tonic-gate  * This code may become common to AH and ESP.  Stay tuned.
32620Sstevel@tonic-gate  *
32630Sstevel@tonic-gate  * I also make the assumption that db_ref's are cool.  If this assumption
32640Sstevel@tonic-gate  * is wrong, this means that someone other than keysock or me has been
32650Sstevel@tonic-gate  * mucking with PF_KEY messages.
32660Sstevel@tonic-gate  */
32670Sstevel@tonic-gate static void
32680Sstevel@tonic-gate esp_parse_pfkey(mblk_t *mp)
32690Sstevel@tonic-gate {
32700Sstevel@tonic-gate 	mblk_t *msg = mp->b_cont;
32710Sstevel@tonic-gate 	sadb_msg_t *samsg;
32720Sstevel@tonic-gate 	keysock_in_t *ksi;
32730Sstevel@tonic-gate 	int error;
32740Sstevel@tonic-gate 	int diagnostic = SADB_X_DIAGNOSTIC_NONE;
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	ASSERT(msg != NULL);
32770Sstevel@tonic-gate 	samsg = (sadb_msg_t *)msg->b_rptr;
32780Sstevel@tonic-gate 	ksi = (keysock_in_t *)mp->b_rptr;
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 	/*
32810Sstevel@tonic-gate 	 * If applicable, convert unspecified AF_INET6 to unspecified
32820Sstevel@tonic-gate 	 * AF_INET.
32830Sstevel@tonic-gate 	 */
32840Sstevel@tonic-gate 	sadb_srcaddrfix(ksi);
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
32870Sstevel@tonic-gate 	case SADB_ADD:
32880Sstevel@tonic-gate 		error = esp_add_sa(mp, ksi, &diagnostic);
32890Sstevel@tonic-gate 		if (error != 0) {
32900Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic,
32910Sstevel@tonic-gate 			    ksi->ks_in_serial);
32920Sstevel@tonic-gate 		}
32930Sstevel@tonic-gate 		/* else esp_add_sa() took care of things. */
32940Sstevel@tonic-gate 		break;
32950Sstevel@tonic-gate 	case SADB_DELETE:
32960Sstevel@tonic-gate 		error = esp_del_sa(mp, ksi, &diagnostic);
32970Sstevel@tonic-gate 		if (error != 0) {
32980Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic,
32990Sstevel@tonic-gate 			    ksi->ks_in_serial);
33000Sstevel@tonic-gate 		}
33010Sstevel@tonic-gate 		/* Else esp_del_sa() took care of things. */
33020Sstevel@tonic-gate 		break;
33030Sstevel@tonic-gate 	case SADB_GET:
33040Sstevel@tonic-gate 		error = sadb_get_sa(mp, ksi, &esp_sadb, &diagnostic,
33050Sstevel@tonic-gate 		    esp_pfkey_q);
33060Sstevel@tonic-gate 		if (error != 0) {
33070Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic,
33080Sstevel@tonic-gate 			    ksi->ks_in_serial);
33090Sstevel@tonic-gate 		}
33100Sstevel@tonic-gate 		/* Else sadb_get_sa() took care of things. */
33110Sstevel@tonic-gate 		break;
33120Sstevel@tonic-gate 	case SADB_FLUSH:
33130Sstevel@tonic-gate 		sadbp_flush(&esp_sadb);
33140Sstevel@tonic-gate 		sadb_pfkey_echo(esp_pfkey_q, mp, samsg, ksi, NULL);
33150Sstevel@tonic-gate 		break;
33160Sstevel@tonic-gate 	case SADB_REGISTER:
33170Sstevel@tonic-gate 		/*
33180Sstevel@tonic-gate 		 * Hmmm, let's do it!  Check for extensions (there should
33190Sstevel@tonic-gate 		 * be none), extract the fields, call esp_register_out(),
33200Sstevel@tonic-gate 		 * then either free or report an error.
33210Sstevel@tonic-gate 		 *
33220Sstevel@tonic-gate 		 * Keysock takes care of the PF_KEY bookkeeping for this.
33230Sstevel@tonic-gate 		 */
33240Sstevel@tonic-gate 		if (esp_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
33250Sstevel@tonic-gate 		    ksi->ks_in_serial)) {
33260Sstevel@tonic-gate 			freemsg(mp);
33270Sstevel@tonic-gate 		} else {
33280Sstevel@tonic-gate 			/*
33290Sstevel@tonic-gate 			 * Only way this path hits is if there is a memory
33300Sstevel@tonic-gate 			 * failure.  It will not return B_FALSE because of
33310Sstevel@tonic-gate 			 * lack of esp_pfkey_q if I am in wput().
33320Sstevel@tonic-gate 			 */
33330Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, ENOMEM, diagnostic,
33340Sstevel@tonic-gate 			    ksi->ks_in_serial);
33350Sstevel@tonic-gate 		}
33360Sstevel@tonic-gate 		break;
33370Sstevel@tonic-gate 	case SADB_UPDATE:
33380Sstevel@tonic-gate 		/*
33390Sstevel@tonic-gate 		 * Find a larval, if not there, find a full one and get
33400Sstevel@tonic-gate 		 * strict.
33410Sstevel@tonic-gate 		 */
33420Sstevel@tonic-gate 		error = esp_update_sa(mp, ksi, &diagnostic);
33430Sstevel@tonic-gate 		if (error != 0) {
33440Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic,
33450Sstevel@tonic-gate 			    ksi->ks_in_serial);
33460Sstevel@tonic-gate 		}
33470Sstevel@tonic-gate 		/* else esp_update_sa() took care of things. */
33480Sstevel@tonic-gate 		break;
33490Sstevel@tonic-gate 	case SADB_GETSPI:
33500Sstevel@tonic-gate 		/*
33510Sstevel@tonic-gate 		 * Reserve a new larval entry.
33520Sstevel@tonic-gate 		 */
33530Sstevel@tonic-gate 		esp_getspi(mp, ksi);
33540Sstevel@tonic-gate 		break;
33550Sstevel@tonic-gate 	case SADB_ACQUIRE:
33560Sstevel@tonic-gate 		/*
33570Sstevel@tonic-gate 		 * Find larval and/or ACQUIRE record and kill it (them), I'm
33580Sstevel@tonic-gate 		 * most likely an error.  Inbound ACQUIRE messages should only
33590Sstevel@tonic-gate 		 * have the base header.
33600Sstevel@tonic-gate 		 */
33610Sstevel@tonic-gate 		sadb_in_acquire(samsg, &esp_sadb, esp_pfkey_q);
33620Sstevel@tonic-gate 		freemsg(mp);
33630Sstevel@tonic-gate 		break;
33640Sstevel@tonic-gate 	case SADB_DUMP:
33650Sstevel@tonic-gate 		/*
33660Sstevel@tonic-gate 		 * Dump all entries.
33670Sstevel@tonic-gate 		 */
33680Sstevel@tonic-gate 		esp_dump(mp, ksi);
33690Sstevel@tonic-gate 		/* esp_dump will take care of the return message, etc. */
33700Sstevel@tonic-gate 		break;
33710Sstevel@tonic-gate 	case SADB_EXPIRE:
33720Sstevel@tonic-gate 		/* Should never reach me. */
33730Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, EOPNOTSUPP, diagnostic,
33740Sstevel@tonic-gate 		    ksi->ks_in_serial);
33750Sstevel@tonic-gate 		break;
33760Sstevel@tonic-gate 	default:
33770Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, EINVAL,
33780Sstevel@tonic-gate 		    SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
33790Sstevel@tonic-gate 		break;
33800Sstevel@tonic-gate 	}
33810Sstevel@tonic-gate }
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate /*
33840Sstevel@tonic-gate  * Handle case where PF_KEY says it can't find a keysock for one of my
33850Sstevel@tonic-gate  * ACQUIRE messages.
33860Sstevel@tonic-gate  */
33870Sstevel@tonic-gate static void
33880Sstevel@tonic-gate esp_keysock_no_socket(mblk_t *mp)
33890Sstevel@tonic-gate {
33900Sstevel@tonic-gate 	sadb_msg_t *samsg;
33910Sstevel@tonic-gate 	keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate 	if (mp->b_cont == NULL) {
33940Sstevel@tonic-gate 		freemsg(mp);
33950Sstevel@tonic-gate 		return;
33960Sstevel@tonic-gate 	}
33970Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 	/*
34000Sstevel@tonic-gate 	 * If keysock can't find any registered, delete the acquire record
34010Sstevel@tonic-gate 	 * immediately, and handle errors.
34020Sstevel@tonic-gate 	 */
34030Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_ACQUIRE) {
34040Sstevel@tonic-gate 		samsg->sadb_msg_errno = kse->ks_err_errno;
34050Sstevel@tonic-gate 		samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
34060Sstevel@tonic-gate 		/*
34070Sstevel@tonic-gate 		 * Use the write-side of the esp_pfkey_q, in case there is
34080Sstevel@tonic-gate 		 * no esp_sadb.s_ip_q.
34090Sstevel@tonic-gate 		 */
34100Sstevel@tonic-gate 		sadb_in_acquire(samsg, &esp_sadb, WR(esp_pfkey_q));
34110Sstevel@tonic-gate 	}
34120Sstevel@tonic-gate 
34130Sstevel@tonic-gate 	freemsg(mp);
34140Sstevel@tonic-gate }
34150Sstevel@tonic-gate 
34160Sstevel@tonic-gate /*
34170Sstevel@tonic-gate  * First-cut reality check for an inbound PF_KEY message.
34180Sstevel@tonic-gate  */
34190Sstevel@tonic-gate static boolean_t
34200Sstevel@tonic-gate esp_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi)
34210Sstevel@tonic-gate {
34220Sstevel@tonic-gate 	int diagnostic;
34230Sstevel@tonic-gate 
34240Sstevel@tonic-gate 	if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
34250Sstevel@tonic-gate 		diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
34260Sstevel@tonic-gate 		goto badmsg;
34270Sstevel@tonic-gate 	}
34280Sstevel@tonic-gate 	if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
34290Sstevel@tonic-gate 	    ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
34300Sstevel@tonic-gate 		diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
34310Sstevel@tonic-gate 		goto badmsg;
34320Sstevel@tonic-gate 	}
34330Sstevel@tonic-gate 	if (ksi->ks_in_srctype == KS_IN_ADDR_MBCAST) {
34340Sstevel@tonic-gate 		diagnostic = SADB_X_DIAGNOSTIC_BAD_SRC;
34350Sstevel@tonic-gate 		goto badmsg;
34360Sstevel@tonic-gate 	}
34370Sstevel@tonic-gate 	if (ksi->ks_in_dsttype == KS_IN_ADDR_UNSPEC) {
34380Sstevel@tonic-gate 		diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
34390Sstevel@tonic-gate 		goto badmsg;
34400Sstevel@tonic-gate 	}
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate 	return (B_FALSE);	/* False ==> no failures */
34430Sstevel@tonic-gate 
34440Sstevel@tonic-gate badmsg:
34450Sstevel@tonic-gate 	sadb_pfkey_error(esp_pfkey_q, mp, EINVAL, diagnostic,
34460Sstevel@tonic-gate 	    ksi->ks_in_serial);
34470Sstevel@tonic-gate 	return (B_TRUE);	/* True ==> failures */
34480Sstevel@tonic-gate }
34490Sstevel@tonic-gate 
34500Sstevel@tonic-gate /*
34510Sstevel@tonic-gate  * ESP module write put routine.
34520Sstevel@tonic-gate  */
34530Sstevel@tonic-gate static void
34540Sstevel@tonic-gate ipsecesp_wput(queue_t *q, mblk_t *mp)
34550Sstevel@tonic-gate {
34560Sstevel@tonic-gate 	ipsec_info_t *ii;
34570Sstevel@tonic-gate 	keysock_in_t *ksi;
34580Sstevel@tonic-gate 	int rc;
34590Sstevel@tonic-gate 	struct iocblk *iocp;
34600Sstevel@tonic-gate 
34610Sstevel@tonic-gate 	esp3dbg(("In esp_wput().\n"));
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 	/* NOTE: Each case must take care of freeing or passing mp. */
34640Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
34650Sstevel@tonic-gate 	case M_CTL:
34660Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
34670Sstevel@tonic-gate 			/* Not big enough message. */
34680Sstevel@tonic-gate 			freemsg(mp);
34690Sstevel@tonic-gate 			break;
34700Sstevel@tonic-gate 		}
34710Sstevel@tonic-gate 		ii = (ipsec_info_t *)mp->b_rptr;
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 		switch (ii->ipsec_info_type) {
34740Sstevel@tonic-gate 		case KEYSOCK_OUT_ERR:
34750Sstevel@tonic-gate 			esp1dbg(("Got KEYSOCK_OUT_ERR message.\n"));
34760Sstevel@tonic-gate 			esp_keysock_no_socket(mp);
34770Sstevel@tonic-gate 			break;
34780Sstevel@tonic-gate 		case KEYSOCK_IN:
34790Sstevel@tonic-gate 			ESP_BUMP_STAT(keysock_in);
34800Sstevel@tonic-gate 			esp3dbg(("Got KEYSOCK_IN message.\n"));
34810Sstevel@tonic-gate 			ksi = (keysock_in_t *)ii;
34820Sstevel@tonic-gate 			/*
34830Sstevel@tonic-gate 			 * Some common reality checks.
34840Sstevel@tonic-gate 			 */
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 			if (esp_pfkey_reality_failures(mp, ksi))
34870Sstevel@tonic-gate 				return;
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate 			/*
34900Sstevel@tonic-gate 			 * Use 'q' instead of esp_sadb.s_ip_q, since
34910Sstevel@tonic-gate 			 * it's the write side already, and it'll go
34920Sstevel@tonic-gate 			 * down to IP.  Use esp_pfkey_q because we
34930Sstevel@tonic-gate 			 * wouldn't get here if that weren't set, and
34940Sstevel@tonic-gate 			 * the RD(q) has been done already.
34950Sstevel@tonic-gate 			 */
34960Sstevel@tonic-gate 			if (ksi->ks_in_srctype == KS_IN_ADDR_UNKNOWN) {
34970Sstevel@tonic-gate 				rc = sadb_addrcheck(q, esp_pfkey_q, mp,
34980Sstevel@tonic-gate 				    ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC],
34990Sstevel@tonic-gate 				    ksi->ks_in_serial);
35000Sstevel@tonic-gate 				if (rc == KS_IN_ADDR_UNKNOWN)
35010Sstevel@tonic-gate 					return;
35020Sstevel@tonic-gate 				else
35030Sstevel@tonic-gate 					ksi->ks_in_srctype = rc;
35040Sstevel@tonic-gate 			}
35050Sstevel@tonic-gate 			if (ksi->ks_in_dsttype == KS_IN_ADDR_UNKNOWN) {
35060Sstevel@tonic-gate 				rc = sadb_addrcheck(q, esp_pfkey_q, mp,
35070Sstevel@tonic-gate 				    ksi->ks_in_extv[SADB_EXT_ADDRESS_DST],
35080Sstevel@tonic-gate 				    ksi->ks_in_serial);
35090Sstevel@tonic-gate 				if (rc == KS_IN_ADDR_UNKNOWN)
35100Sstevel@tonic-gate 					return;
35110Sstevel@tonic-gate 				else
35120Sstevel@tonic-gate 					ksi->ks_in_dsttype = rc;
35130Sstevel@tonic-gate 			}
35140Sstevel@tonic-gate 			/*
35150Sstevel@tonic-gate 			 * XXX Proxy may be a different address family.
35160Sstevel@tonic-gate 			 */
35170Sstevel@tonic-gate 			if (ksi->ks_in_proxytype == KS_IN_ADDR_UNKNOWN) {
35180Sstevel@tonic-gate 				rc = sadb_addrcheck(q, esp_pfkey_q, mp,
35190Sstevel@tonic-gate 				    ksi->ks_in_extv[SADB_EXT_ADDRESS_PROXY],
35200Sstevel@tonic-gate 				    ksi->ks_in_serial);
35210Sstevel@tonic-gate 				if (rc == KS_IN_ADDR_UNKNOWN)
35220Sstevel@tonic-gate 					return;
35230Sstevel@tonic-gate 				else
35240Sstevel@tonic-gate 					ksi->ks_in_proxytype = rc;
35250Sstevel@tonic-gate 			}
35260Sstevel@tonic-gate 			esp_parse_pfkey(mp);
35270Sstevel@tonic-gate 			break;
35280Sstevel@tonic-gate 		case KEYSOCK_HELLO:
35290Sstevel@tonic-gate 			sadb_keysock_hello(&esp_pfkey_q, q, mp,
35300Sstevel@tonic-gate 			    esp_ager, &esp_event, SADB_SATYPE_ESP);
35310Sstevel@tonic-gate 			break;
35320Sstevel@tonic-gate 		default:
35330Sstevel@tonic-gate 			esp2dbg(("Got M_CTL from above of 0x%x.\n",
35340Sstevel@tonic-gate 			    ii->ipsec_info_type));
35350Sstevel@tonic-gate 			freemsg(mp);
35360Sstevel@tonic-gate 			break;
35370Sstevel@tonic-gate 		}
35380Sstevel@tonic-gate 		break;
35390Sstevel@tonic-gate 	case M_IOCTL:
35400Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
35410Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
35420Sstevel@tonic-gate 		case ND_SET:
35430Sstevel@tonic-gate 		case ND_GET:
35440Sstevel@tonic-gate 			if (nd_getset(q, ipsecesp_g_nd, mp)) {
35450Sstevel@tonic-gate 				qreply(q, mp);
35460Sstevel@tonic-gate 				return;
35470Sstevel@tonic-gate 			} else {
35480Sstevel@tonic-gate 				iocp->ioc_error = ENOENT;
35490Sstevel@tonic-gate 			}
35500Sstevel@tonic-gate 			/* FALLTHRU */
35510Sstevel@tonic-gate 		default:
35520Sstevel@tonic-gate 			/* We really don't support any other ioctls, do we? */
35530Sstevel@tonic-gate 
35540Sstevel@tonic-gate 			/* Return EINVAL */
35550Sstevel@tonic-gate 			if (iocp->ioc_error != ENOENT)
35560Sstevel@tonic-gate 				iocp->ioc_error = EINVAL;
35570Sstevel@tonic-gate 			iocp->ioc_count = 0;
35580Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
35590Sstevel@tonic-gate 			qreply(q, mp);
35600Sstevel@tonic-gate 			return;
35610Sstevel@tonic-gate 		}
35620Sstevel@tonic-gate 	default:
35630Sstevel@tonic-gate 		esp3dbg(("Got default message, type %d, passing to IP.\n",
35640Sstevel@tonic-gate 		    mp->b_datap->db_type));
35650Sstevel@tonic-gate 		putnext(q, mp);
35660Sstevel@tonic-gate 	}
35670Sstevel@tonic-gate }
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate /*
35700Sstevel@tonic-gate  * Process an outbound ESP packet that can be accelerated by a IPsec
35710Sstevel@tonic-gate  * hardware acceleration capable Provider.
35720Sstevel@tonic-gate  * The caller already inserted and initialized the ESP header.
35730Sstevel@tonic-gate  * This function allocates a tagging M_CTL, and adds room at the end
35740Sstevel@tonic-gate  * of the packet to hold the ICV if authentication is needed.
35750Sstevel@tonic-gate  *
35760Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
35770Sstevel@tonic-gate  * mblk chain ipsec_out.
35780Sstevel@tonic-gate  */
35790Sstevel@tonic-gate static ipsec_status_t
35800Sstevel@tonic-gate esp_outbound_accelerated(mblk_t *ipsec_out, uint_t icv_len)
35810Sstevel@tonic-gate {
35820Sstevel@tonic-gate 	ipsec_out_t *io;
35830Sstevel@tonic-gate 	mblk_t *lastmp;
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	ESP_BUMP_STAT(out_accelerated);
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	io = (ipsec_out_t *)ipsec_out->b_rptr;
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate 	/* mark packet as being accelerated in IPSEC_OUT */
35900Sstevel@tonic-gate 	ASSERT(io->ipsec_out_accelerated == B_FALSE);
35910Sstevel@tonic-gate 	io->ipsec_out_accelerated = B_TRUE;
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	/*
35940Sstevel@tonic-gate 	 * add room at the end of the packet for the ICV if needed
35950Sstevel@tonic-gate 	 */
35960Sstevel@tonic-gate 	if (icv_len > 0) {
35970Sstevel@tonic-gate 		/* go to last mblk */
35980Sstevel@tonic-gate 		lastmp = ipsec_out;	/* For following while loop. */
35990Sstevel@tonic-gate 		do {
36000Sstevel@tonic-gate 			lastmp = lastmp->b_cont;
36010Sstevel@tonic-gate 		} while (lastmp->b_cont != NULL);
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 		/* if not enough available room, allocate new mblk */
36040Sstevel@tonic-gate 		if ((lastmp->b_wptr + icv_len) > lastmp->b_datap->db_lim) {
36050Sstevel@tonic-gate 			lastmp->b_cont = allocb(icv_len, BPRI_HI);
36060Sstevel@tonic-gate 			if (lastmp->b_cont == NULL) {
36070Sstevel@tonic-gate 				ESP_BUMP_STAT(out_discards);
36080Sstevel@tonic-gate 				ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
36090Sstevel@tonic-gate 				    &ipdrops_esp_nomem, &esp_dropper);
36100Sstevel@tonic-gate 				return (IPSEC_STATUS_FAILED);
36110Sstevel@tonic-gate 			}
36120Sstevel@tonic-gate 			lastmp = lastmp->b_cont;
36130Sstevel@tonic-gate 		}
36140Sstevel@tonic-gate 		lastmp->b_wptr += icv_len;
36150Sstevel@tonic-gate 	}
36160Sstevel@tonic-gate 
36170Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
36180Sstevel@tonic-gate }
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate /*
36210Sstevel@tonic-gate  * Process an inbound accelerated ESP packet.
36220Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
36230Sstevel@tonic-gate  * mblk chain ipsec_in.
36240Sstevel@tonic-gate  */
36250Sstevel@tonic-gate static ipsec_status_t
36260Sstevel@tonic-gate esp_inbound_accelerated(mblk_t *ipsec_in, mblk_t *data_mp, boolean_t isv4,
36270Sstevel@tonic-gate     ipsa_t *assoc)
36280Sstevel@tonic-gate {
36290Sstevel@tonic-gate 	ipsec_in_t *ii;
36300Sstevel@tonic-gate 	mblk_t *hada_mp;
36310Sstevel@tonic-gate 	uint32_t icv_len = 0;
36320Sstevel@tonic-gate 	da_ipsec_t *hada;
36330Sstevel@tonic-gate 	ipha_t *ipha;
36340Sstevel@tonic-gate 	ip6_t *ip6h;
36350Sstevel@tonic-gate 	kstat_named_t *counter;
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 	ESP_BUMP_STAT(in_accelerated);
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate 	ii = (ipsec_in_t *)ipsec_in->b_rptr;
36400Sstevel@tonic-gate 	hada_mp = ii->ipsec_in_da;
36410Sstevel@tonic-gate 	ASSERT(hada_mp != NULL);
36420Sstevel@tonic-gate 	hada = (da_ipsec_t *)hada_mp->b_rptr;
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 	/*
36450Sstevel@tonic-gate 	 * We only support one level of decapsulation in hardware, so
36460Sstevel@tonic-gate 	 * nuke the pointer.
36470Sstevel@tonic-gate 	 */
36480Sstevel@tonic-gate 	ii->ipsec_in_da = NULL;
36490Sstevel@tonic-gate 	ii->ipsec_in_accelerated = B_FALSE;
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) {
36520Sstevel@tonic-gate 		/*
36530Sstevel@tonic-gate 		 * ESP with authentication. We expect the Provider to have
36540Sstevel@tonic-gate 		 * computed the ICV and placed it in the hardware acceleration
36550Sstevel@tonic-gate 		 * data attributes.
36560Sstevel@tonic-gate 		 *
36570Sstevel@tonic-gate 		 * Extract ICV length from attributes M_CTL and sanity check
36580Sstevel@tonic-gate 		 * its value. We allow the mblk to be smaller than da_ipsec_t
36590Sstevel@tonic-gate 		 * for a small ICV, as long as the entire ICV fits within the
36600Sstevel@tonic-gate 		 * mblk.
36610Sstevel@tonic-gate 		 *
36620Sstevel@tonic-gate 		 * Also ensures that the ICV length computed by Provider
36630Sstevel@tonic-gate 		 * corresponds to the ICV length of the agorithm specified by
36640Sstevel@tonic-gate 		 * the SA.
36650Sstevel@tonic-gate 		 */
36660Sstevel@tonic-gate 		icv_len = hada->da_icv_len;
36670Sstevel@tonic-gate 		if ((icv_len != assoc->ipsa_mac_len) ||
36680Sstevel@tonic-gate 		    (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) <
36690Sstevel@tonic-gate 			(sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) {
36700Sstevel@tonic-gate 			esp0dbg(("esp_inbound_accelerated: "
36710Sstevel@tonic-gate 			    "ICV len (%u) incorrect or mblk too small (%u)\n",
36720Sstevel@tonic-gate 			    icv_len, (uint32_t)(MBLKL(hada_mp))));
36730Sstevel@tonic-gate 			counter = &ipdrops_esp_bad_auth;
36740Sstevel@tonic-gate 			goto esp_in_discard;
36750Sstevel@tonic-gate 		}
36760Sstevel@tonic-gate 	}
36770Sstevel@tonic-gate 
36780Sstevel@tonic-gate 	/* get pointers to IP header */
36790Sstevel@tonic-gate 	if (isv4) {
36800Sstevel@tonic-gate 		ipha = (ipha_t *)data_mp->b_rptr;
36810Sstevel@tonic-gate 	} else {
36820Sstevel@tonic-gate 		ip6h = (ip6_t *)data_mp->b_rptr;
36830Sstevel@tonic-gate 	}
36840Sstevel@tonic-gate 
36850Sstevel@tonic-gate 	/*
36860Sstevel@tonic-gate 	 * Compare ICV in ESP packet vs ICV computed by adapter.
36870Sstevel@tonic-gate 	 * We also remove the ICV from the end of the packet since
36880Sstevel@tonic-gate 	 * it will no longer be needed.
36890Sstevel@tonic-gate 	 *
36900Sstevel@tonic-gate 	 * Assume that esp_inbound() already ensured that the pkt
36910Sstevel@tonic-gate 	 * was in one mblk.
36920Sstevel@tonic-gate 	 */
36930Sstevel@tonic-gate 	ASSERT(data_mp->b_cont == NULL);
36940Sstevel@tonic-gate 	data_mp->b_wptr -= icv_len;
36950Sstevel@tonic-gate 	/* adjust IP header */
36960Sstevel@tonic-gate 	if (isv4)
36970Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) - icv_len);
36980Sstevel@tonic-gate 	else
36990Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - icv_len);
37000Sstevel@tonic-gate 	if (icv_len && bcmp(hada->da_icv, data_mp->b_wptr, icv_len)) {
37010Sstevel@tonic-gate 		int af;
37020Sstevel@tonic-gate 		void *addr;
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 		if (isv4) {
37050Sstevel@tonic-gate 			addr = &ipha->ipha_dst;
37060Sstevel@tonic-gate 			af = AF_INET;
37070Sstevel@tonic-gate 		} else {
37080Sstevel@tonic-gate 			addr = &ip6h->ip6_dst;
37090Sstevel@tonic-gate 			af = AF_INET6;
37100Sstevel@tonic-gate 		}
37110Sstevel@tonic-gate 
37120Sstevel@tonic-gate 		/*
37130Sstevel@tonic-gate 		 * Log the event. Don't print to the console, block
37140Sstevel@tonic-gate 		 * potential denial-of-service attack.
37150Sstevel@tonic-gate 		 */
37160Sstevel@tonic-gate 		ESP_BUMP_STAT(bad_auth);
37170Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
37180Sstevel@tonic-gate 		    "ESP Authentication failed spi %x, dst_addr %s",
37190Sstevel@tonic-gate 		    assoc->ipsa_spi, addr, af);
37200Sstevel@tonic-gate 		counter = &ipdrops_esp_bad_auth;
37210Sstevel@tonic-gate 		goto esp_in_discard;
37220Sstevel@tonic-gate 	}
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate 	esp3dbg(("esp_inbound_accelerated: ESP authentication succeeded, "
37250Sstevel@tonic-gate 	    "checking replay\n"));
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 	ipsec_in->b_cont = data_mp;
37280Sstevel@tonic-gate 
37290Sstevel@tonic-gate 	/*
37300Sstevel@tonic-gate 	 * Remove ESP header and padding from packet.
37310Sstevel@tonic-gate 	 */
37320Sstevel@tonic-gate 	if (!esp_strip_header(data_mp, ii->ipsec_in_v4, assoc->ipsa_iv_len,
37330Sstevel@tonic-gate 		&counter)) {
37340Sstevel@tonic-gate 		esp1dbg(("esp_inbound_accelerated: "
37350Sstevel@tonic-gate 		    "esp_strip_header() failed\n"));
37360Sstevel@tonic-gate 		goto esp_in_discard;
37370Sstevel@tonic-gate 	}
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 	freeb(hada_mp);
37400Sstevel@tonic-gate 
37410Sstevel@tonic-gate 	/*
37420Sstevel@tonic-gate 	 * Account for usage..
37430Sstevel@tonic-gate 	 */
37440Sstevel@tonic-gate 	if (!esp_age_bytes(assoc, msgdsize(data_mp), B_TRUE)) {
37450Sstevel@tonic-gate 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
37460Sstevel@tonic-gate 		ESP_BUMP_STAT(bytes_expired);
37470Sstevel@tonic-gate 		IP_ESP_BUMP_STAT(in_discards);
37480Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
37490Sstevel@tonic-gate 		    "ESP association 0x%x, dst %s had bytes expire.\n",
37500Sstevel@tonic-gate 		    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam);
37510Sstevel@tonic-gate 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
37520Sstevel@tonic-gate 		    &ipdrops_esp_bytes_expire, &esp_dropper);
37530Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
37540Sstevel@tonic-gate 	}
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 	/* done processing the packet */
37570Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
37580Sstevel@tonic-gate 
37590Sstevel@tonic-gate esp_in_discard:
37600Sstevel@tonic-gate 	IP_ESP_BUMP_STAT(in_discards);
37610Sstevel@tonic-gate 	freeb(hada_mp);
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 	ipsec_in->b_cont = data_mp;	/* For ip_drop_packet()'s sake... */
37640Sstevel@tonic-gate 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter, &esp_dropper);
37650Sstevel@tonic-gate 
37660Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
37670Sstevel@tonic-gate }
37680Sstevel@tonic-gate 
37690Sstevel@tonic-gate /*
37700Sstevel@tonic-gate  * Wrapper to allow IP to trigger an ESP association failure message
37710Sstevel@tonic-gate  * during inbound SA selection.
37720Sstevel@tonic-gate  */
37730Sstevel@tonic-gate void
37740Sstevel@tonic-gate ipsecesp_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
37750Sstevel@tonic-gate     uint32_t spi, void *addr, int af)
37760Sstevel@tonic-gate {
37770Sstevel@tonic-gate 	if (ipsecesp_log_unknown_spi) {
37780Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
37790Sstevel@tonic-gate 		    addr, af);
37800Sstevel@tonic-gate 	}
37810Sstevel@tonic-gate 
37820Sstevel@tonic-gate 	ip_drop_packet(mp, B_TRUE, NULL, NULL, &ipdrops_esp_no_sa,
37830Sstevel@tonic-gate 	    &esp_dropper);
37840Sstevel@tonic-gate }
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate /*
37870Sstevel@tonic-gate  * Initialize the ESP input and output processing functions.
37880Sstevel@tonic-gate  */
37890Sstevel@tonic-gate void
37900Sstevel@tonic-gate ipsecesp_init_funcs(ipsa_t *sa)
37910Sstevel@tonic-gate {
37920Sstevel@tonic-gate 	if (sa->ipsa_output_func == NULL)
37930Sstevel@tonic-gate 		sa->ipsa_output_func = esp_outbound;
37940Sstevel@tonic-gate 	if (sa->ipsa_input_func == NULL)
37950Sstevel@tonic-gate 		sa->ipsa_input_func = esp_inbound;
37960Sstevel@tonic-gate }
3797