xref: /onnv-gate/usr/src/uts/common/inet/ip/ipsecesp.c (revision 3192:e4e2ffd4e00f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52751Sdanmcd  * Common Development and Distribution License (the "License").
62751Sdanmcd  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
222751Sdanmcd  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/stream.h>
300Sstevel@tonic-gate #include <sys/stropts.h>
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/strlog.h>
330Sstevel@tonic-gate #include <sys/tihdr.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/vtrace.h>
410Sstevel@tonic-gate #include <sys/debug.h>
420Sstevel@tonic-gate #include <sys/atomic.h>
430Sstevel@tonic-gate #include <sys/strsun.h>
440Sstevel@tonic-gate #include <sys/random.h>
450Sstevel@tonic-gate #include <netinet/in.h>
460Sstevel@tonic-gate #include <net/if.h>
470Sstevel@tonic-gate #include <netinet/ip6.h>
480Sstevel@tonic-gate #include <net/pfkeyv2.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <inet/common.h>
510Sstevel@tonic-gate #include <inet/mi.h>
520Sstevel@tonic-gate #include <inet/nd.h>
530Sstevel@tonic-gate #include <inet/ip.h>
540Sstevel@tonic-gate #include <inet/ip6.h>
550Sstevel@tonic-gate #include <inet/sadb.h>
560Sstevel@tonic-gate #include <inet/ipsec_info.h>
570Sstevel@tonic-gate #include <inet/ipsec_impl.h>
580Sstevel@tonic-gate #include <inet/ipsecesp.h>
590Sstevel@tonic-gate #include <inet/ipdrop.h>
600Sstevel@tonic-gate #include <inet/tcp.h>
610Sstevel@tonic-gate #include <sys/kstat.h>
620Sstevel@tonic-gate #include <sys/policy.h>
630Sstevel@tonic-gate #include <sys/strsun.h>
640Sstevel@tonic-gate #include <inet/udp_impl.h>
650Sstevel@tonic-gate #include <sys/taskq.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <sys/iphada.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* Packet dropper for ESP drops. */
700Sstevel@tonic-gate static ipdropper_t esp_dropper;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static kmutex_t ipsecesp_param_lock; /* Protects ipsecesp_param_arr[] below. */
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Table of ND variables supported by ipsecesp. These are loaded into
750Sstevel@tonic-gate  * ipsecesp_g_nd in ipsecesp_init_nd.
760Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate static	ipsecespparam_t	ipsecesp_param_arr[] = {
790Sstevel@tonic-gate 	/* min	max			value	name */
800Sstevel@tonic-gate 	{ 0,	3,			0,	"ipsecesp_debug"},
810Sstevel@tonic-gate 	{ 125,	32000, SADB_AGE_INTERVAL_DEFAULT, "ipsecesp_age_interval"},
820Sstevel@tonic-gate 	{ 1,	10,			1,	"ipsecesp_reap_delay"},
830Sstevel@tonic-gate 	{ 1,	SADB_MAX_REPLAY,	64,	"ipsecesp_replay_size"},
840Sstevel@tonic-gate 	{ 1,	300,			15,	"ipsecesp_acquire_timeout"},
850Sstevel@tonic-gate 	{ 1,	1800,			90,	"ipsecesp_larval_timeout"},
860Sstevel@tonic-gate 	/* Default lifetime values for ACQUIRE messages. */
870Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_soft_bytes"},
880Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_hard_bytes"},
890Sstevel@tonic-gate 	{ 0,	0xffffffffU,	24000,	"ipsecesp_default_soft_addtime"},
900Sstevel@tonic-gate 	{ 0,	0xffffffffU,	28800,	"ipsecesp_default_hard_addtime"},
910Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_soft_usetime"},
920Sstevel@tonic-gate 	{ 0,	0xffffffffU,	0,	"ipsecesp_default_hard_usetime"},
930Sstevel@tonic-gate 	{ 0,	1,		0,	"ipsecesp_log_unknown_spi"},
940Sstevel@tonic-gate 	{ 0,	2,		1,	"ipsecesp_padding_check"},
950Sstevel@tonic-gate };
960Sstevel@tonic-gate #define	ipsecesp_debug		ipsecesp_param_arr[0].ipsecesp_param_value
970Sstevel@tonic-gate #define	ipsecesp_age_interval	ipsecesp_param_arr[1].ipsecesp_param_value
980Sstevel@tonic-gate #define	ipsecesp_age_int_max	ipsecesp_param_arr[1].ipsecesp_param_max
990Sstevel@tonic-gate #define	ipsecesp_reap_delay	ipsecesp_param_arr[2].ipsecesp_param_value
1000Sstevel@tonic-gate #define	ipsecesp_replay_size	ipsecesp_param_arr[3].ipsecesp_param_value
1010Sstevel@tonic-gate #define	ipsecesp_acquire_timeout ipsecesp_param_arr[4].ipsecesp_param_value
1020Sstevel@tonic-gate #define	ipsecesp_larval_timeout ipsecesp_param_arr[5].ipsecesp_param_value
1030Sstevel@tonic-gate #define	ipsecesp_default_soft_bytes \
1040Sstevel@tonic-gate 	ipsecesp_param_arr[6].ipsecesp_param_value
1050Sstevel@tonic-gate #define	ipsecesp_default_hard_bytes \
1060Sstevel@tonic-gate 	ipsecesp_param_arr[7].ipsecesp_param_value
1070Sstevel@tonic-gate #define	ipsecesp_default_soft_addtime \
1080Sstevel@tonic-gate 	ipsecesp_param_arr[8].ipsecesp_param_value
1090Sstevel@tonic-gate #define	ipsecesp_default_hard_addtime \
1100Sstevel@tonic-gate 	ipsecesp_param_arr[9].ipsecesp_param_value
1110Sstevel@tonic-gate #define	ipsecesp_default_soft_usetime \
1120Sstevel@tonic-gate 	ipsecesp_param_arr[10].ipsecesp_param_value
1130Sstevel@tonic-gate #define	ipsecesp_default_hard_usetime \
1140Sstevel@tonic-gate 	ipsecesp_param_arr[11].ipsecesp_param_value
1150Sstevel@tonic-gate #define	ipsecesp_log_unknown_spi \
1160Sstevel@tonic-gate 	ipsecesp_param_arr[12].ipsecesp_param_value
1170Sstevel@tonic-gate #define	ipsecesp_padding_check \
1180Sstevel@tonic-gate 	ipsecesp_param_arr[13].ipsecesp_param_value
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #define	esp0dbg(a)	printf a
1210Sstevel@tonic-gate /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
1220Sstevel@tonic-gate #define	esp1dbg(a)	if (ipsecesp_debug != 0) printf a
1230Sstevel@tonic-gate #define	esp2dbg(a)	if (ipsecesp_debug > 1) printf a
1240Sstevel@tonic-gate #define	esp3dbg(a)	if (ipsecesp_debug > 2) printf a
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate static IDP ipsecesp_g_nd;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static int ipsecesp_open(queue_t *, dev_t *, int, int, cred_t *);
1290Sstevel@tonic-gate static int ipsecesp_close(queue_t *);
1300Sstevel@tonic-gate static void ipsecesp_rput(queue_t *, mblk_t *);
1310Sstevel@tonic-gate static void ipsecesp_wput(queue_t *, mblk_t *);
1320Sstevel@tonic-gate static void esp_send_acquire(ipsacq_t *, mblk_t *);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static ipsec_status_t esp_outbound_accelerated(mblk_t *, uint_t);
1350Sstevel@tonic-gate static ipsec_status_t esp_inbound_accelerated(mblk_t *, mblk_t *,
1360Sstevel@tonic-gate     boolean_t, ipsa_t *);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static boolean_t esp_register_out(uint32_t, uint32_t, uint_t);
1390Sstevel@tonic-gate static boolean_t esp_strip_header(mblk_t *, boolean_t, uint32_t,
1400Sstevel@tonic-gate     kstat_named_t **);
1410Sstevel@tonic-gate static ipsec_status_t esp_submit_req_inbound(mblk_t *, ipsa_t *, uint_t);
1420Sstevel@tonic-gate static ipsec_status_t esp_submit_req_outbound(mblk_t *, ipsa_t *, uchar_t *,
1430Sstevel@tonic-gate     uint_t);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static struct module_info info = {
1460Sstevel@tonic-gate 	5137, "ipsecesp", 0, INFPSZ, 65536, 1024
1470Sstevel@tonic-gate };
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static struct qinit rinit = {
1500Sstevel@tonic-gate 	(pfi_t)ipsecesp_rput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info,
1510Sstevel@tonic-gate 	NULL
1520Sstevel@tonic-gate };
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static struct qinit winit = {
1550Sstevel@tonic-gate 	(pfi_t)ipsecesp_wput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info,
1560Sstevel@tonic-gate 	NULL
1570Sstevel@tonic-gate };
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate struct streamtab ipsecespinfo = {
1600Sstevel@tonic-gate 	&rinit, &winit, NULL, NULL
1610Sstevel@tonic-gate };
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * Keysock instance of ESP.  "There can be only one." :)
1650Sstevel@tonic-gate  * Use casptr() on this because I don't set it until KEYSOCK_HELLO comes down.
1660Sstevel@tonic-gate  * Paired up with the esp_pfkey_q is the esp_event, which will age SAs.
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate static queue_t *esp_pfkey_q;
1690Sstevel@tonic-gate static timeout_id_t esp_event;
1700Sstevel@tonic-gate static taskq_t *esp_taskq;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * OTOH, this one is set at open/close, and I'm D_MTQPAIR for now.
1740Sstevel@tonic-gate  *
1750Sstevel@tonic-gate  * Question:	Do I need this, given that all instance's esps->esps_wq point
1760Sstevel@tonic-gate  *		to IP?
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  * Answer:	Yes, because I need to know which queue is BOUND to
1790Sstevel@tonic-gate  *		IPPROTO_ESP
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate static mblk_t *esp_ip_unbind;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Stats.  This may eventually become a full-blown SNMP MIB once that spec
1850Sstevel@tonic-gate  * stabilizes.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate typedef struct {
1890Sstevel@tonic-gate 	kstat_named_t esp_stat_num_aalgs;
1900Sstevel@tonic-gate 	kstat_named_t esp_stat_good_auth;
1910Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_auth;
1920Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_padding;
1930Sstevel@tonic-gate 	kstat_named_t esp_stat_replay_failures;
1940Sstevel@tonic-gate 	kstat_named_t esp_stat_replay_early_failures;
1950Sstevel@tonic-gate 	kstat_named_t esp_stat_keysock_in;
1960Sstevel@tonic-gate 	kstat_named_t esp_stat_out_requests;
1970Sstevel@tonic-gate 	kstat_named_t esp_stat_acquire_requests;
1980Sstevel@tonic-gate 	kstat_named_t esp_stat_bytes_expired;
1990Sstevel@tonic-gate 	kstat_named_t esp_stat_out_discards;
2000Sstevel@tonic-gate 	kstat_named_t esp_stat_in_accelerated;
2010Sstevel@tonic-gate 	kstat_named_t esp_stat_out_accelerated;
2020Sstevel@tonic-gate 	kstat_named_t esp_stat_noaccel;
2030Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_sync;
2040Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_async;
2050Sstevel@tonic-gate 	kstat_named_t esp_stat_crypto_failures;
2060Sstevel@tonic-gate 	kstat_named_t esp_stat_num_ealgs;
2070Sstevel@tonic-gate 	kstat_named_t esp_stat_bad_decrypt;
2080Sstevel@tonic-gate } esp_kstats_t;
2090Sstevel@tonic-gate 
210564Ssommerfe uint32_t esp_hash_size = IPSEC_DEFAULT_HASH_SIZE;
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;
457564Ssommerfe 	sadbp_init("ESP", &esp_sadb, SADB_SATYPE_ESP, esp_hash_size);
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) {
663564Ssommerfe 			outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
6640Sstevel@tonic-gate 			    &inassoc->ipsa_dstaddr));
6650Sstevel@tonic-gate 		} else {
666564Ssommerfe 			outhash = OUTBOUND_HASH_V4(sp, *((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;
684564Ssommerfe 		bucket = INBOUND_BUCKET(sp, 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 /*
766*3192Sdanmcd  * Strip ESP header, check padding, 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;
779*3192Sdanmcd 	uint8_t *lastbyte;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * Strip ESP data and fix IP header.
7830Sstevel@tonic-gate 	 *
7840Sstevel@tonic-gate 	 * XXX In case the beginning of esp_inbound() changes to not do a
7850Sstevel@tonic-gate 	 * pullup, this part of the code can remain unchanged.
7860Sstevel@tonic-gate 	 */
7870Sstevel@tonic-gate 	if (isv4) {
7880Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ipha_t));
7890Sstevel@tonic-gate 		ipha = (ipha_t *)data_mp->b_rptr;
7900Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (esph_t) +
7910Sstevel@tonic-gate 		    IPH_HDR_LENGTH(ipha));
7920Sstevel@tonic-gate 		divpoint = IPH_HDR_LENGTH(ipha);
7930Sstevel@tonic-gate 	} else {
7940Sstevel@tonic-gate 		ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ip6_t));
7950Sstevel@tonic-gate 		ip6h = (ip6_t *)data_mp->b_rptr;
7960Sstevel@tonic-gate 		divpoint = ip_hdr_length_v6(data_mp, ip6h);
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	scratch = data_mp;
8000Sstevel@tonic-gate 	while (scratch->b_cont != NULL)
8010Sstevel@tonic-gate 		scratch = scratch->b_cont;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	ASSERT((scratch->b_wptr - scratch->b_rptr) >= 3);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	/*
8060Sstevel@tonic-gate 	 * "Next header" and padding length are the last two bytes in the
8070Sstevel@tonic-gate 	 * ESP-protected datagram, thus the explicit - 1 and - 2.
8080Sstevel@tonic-gate 	 * lastpad is the last byte of the padding, which can be used for
8090Sstevel@tonic-gate 	 * a quick check to see if the padding is correct.
8100Sstevel@tonic-gate 	 */
811*3192Sdanmcd 	lastbyte = scratch->b_wptr - 1;
812*3192Sdanmcd 	nexthdr = *lastbyte--;
813*3192Sdanmcd 	padlen = *lastbyte--;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if (isv4) {
8160Sstevel@tonic-gate 		/* Fix part of the IP header. */
8170Sstevel@tonic-gate 		ipha->ipha_protocol = nexthdr;
8180Sstevel@tonic-gate 		/*
8190Sstevel@tonic-gate 		 * Reality check the padlen.  The explicit - 2 is for the
8200Sstevel@tonic-gate 		 * padding length and the next-header bytes.
8210Sstevel@tonic-gate 		 */
8220Sstevel@tonic-gate 		if (padlen >= ntohs(ipha->ipha_length) - sizeof (ipha_t) - 2 -
8230Sstevel@tonic-gate 		    sizeof (esph_t) - ivlen) {
8240Sstevel@tonic-gate 			ESP_BUMP_STAT(bad_decrypt);
8250Sstevel@tonic-gate 			ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
826*3192Sdanmcd 			    "Corrupt ESP packet (padlen too big).\n");
8270Sstevel@tonic-gate 			esp1dbg(("padlen (%d) is greater than:\n", padlen));
8280Sstevel@tonic-gate 			esp1dbg(("pkt len(%d) - ip hdr - esp hdr - ivlen(%d) "
8290Sstevel@tonic-gate 			    "= %d.\n", ntohs(ipha->ipha_length), ivlen,
8300Sstevel@tonic-gate 			    (int)(ntohs(ipha->ipha_length) - sizeof (ipha_t) -
8310Sstevel@tonic-gate 				2 - sizeof (esph_t) - ivlen)));
8320Sstevel@tonic-gate 			*counter = &ipdrops_esp_bad_padlen;
8330Sstevel@tonic-gate 			return (B_FALSE);
8340Sstevel@tonic-gate 		}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		/*
8370Sstevel@tonic-gate 		 * Fix the rest of the header.  The explicit - 2 is for the
8380Sstevel@tonic-gate 		 * padding length and the next-header bytes.
8390Sstevel@tonic-gate 		 */
8400Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) - padlen -
8410Sstevel@tonic-gate 		    2 - sizeof (esph_t) - ivlen);
8420Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = 0;
8430Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
8440Sstevel@tonic-gate 	} else {
8450Sstevel@tonic-gate 		if (ip6h->ip6_nxt == IPPROTO_ESP) {
8460Sstevel@tonic-gate 			ip6h->ip6_nxt = nexthdr;
8470Sstevel@tonic-gate 		} else {
8480Sstevel@tonic-gate 			ip6_pkt_t ipp;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 			bzero(&ipp, sizeof (ipp));
8510Sstevel@tonic-gate 			(void) ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL);
8520Sstevel@tonic-gate 			if (ipp.ipp_dstopts != NULL) {
8530Sstevel@tonic-gate 				ipp.ipp_dstopts->ip6d_nxt = nexthdr;
8540Sstevel@tonic-gate 			} else if (ipp.ipp_rthdr != NULL) {
8550Sstevel@tonic-gate 				ipp.ipp_rthdr->ip6r_nxt = nexthdr;
8560Sstevel@tonic-gate 			} else if (ipp.ipp_hopopts != NULL) {
8570Sstevel@tonic-gate 				ipp.ipp_hopopts->ip6h_nxt = nexthdr;
8580Sstevel@tonic-gate 			} else {
8590Sstevel@tonic-gate 				/* Panic a DEBUG kernel. */
8600Sstevel@tonic-gate 				ASSERT(ipp.ipp_hopopts != NULL);
8610Sstevel@tonic-gate 				/* Otherwise, pretend it's IP + ESP. */
8620Sstevel@tonic-gate 				cmn_err(CE_WARN, "ESP IPv6 headers wrong.\n");
8630Sstevel@tonic-gate 				ip6h->ip6_nxt = nexthdr;
8640Sstevel@tonic-gate 			}
8650Sstevel@tonic-gate 		}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 		if (padlen >= ntohs(ip6h->ip6_plen) - 2 - sizeof (esph_t) -
8680Sstevel@tonic-gate 		    ivlen) {
8690Sstevel@tonic-gate 			ESP_BUMP_STAT(bad_decrypt);
8700Sstevel@tonic-gate 			ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
871*3192Sdanmcd 			    "Corrupt ESP packet (v6 padlen too big).\n");
8720Sstevel@tonic-gate 			esp1dbg(("padlen (%d) is greater than:\n", padlen));
8730Sstevel@tonic-gate 			esp1dbg(("pkt len(%u) - ip hdr - esp hdr - ivlen(%d)"
8740Sstevel@tonic-gate 			    " = %u.\n", (unsigned)(ntohs(ip6h->ip6_plen)
8750Sstevel@tonic-gate 				+ sizeof (ip6_t)), ivlen,
8760Sstevel@tonic-gate 			    (unsigned)(ntohs(ip6h->ip6_plen) - 2 -
8770Sstevel@tonic-gate 				sizeof (esph_t) - ivlen)));
8780Sstevel@tonic-gate 			*counter = &ipdrops_esp_bad_padlen;
8790Sstevel@tonic-gate 			return (B_FALSE);
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		/*
8840Sstevel@tonic-gate 		 * Fix the rest of the header.  The explicit - 2 is for the
8850Sstevel@tonic-gate 		 * padding length and the next-header bytes.  IPv6 is nice,
8860Sstevel@tonic-gate 		 * because there's no hdr checksum!
8870Sstevel@tonic-gate 		 */
8880Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - padlen -
8890Sstevel@tonic-gate 		    2 - sizeof (esph_t) - ivlen);
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
892*3192Sdanmcd 	if (ipsecesp_padding_check > 0 && padlen > 0) {
893*3192Sdanmcd 		/*
894*3192Sdanmcd 		 * Weak padding check: compare last-byte to length, they
895*3192Sdanmcd 		 * should be equal.
896*3192Sdanmcd 		 */
897*3192Sdanmcd 		lastpad = *lastbyte--;
898*3192Sdanmcd 
899*3192Sdanmcd 		if (padlen != lastpad) {
900*3192Sdanmcd 			ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
901*3192Sdanmcd 			    "Corrupt ESP packet (lastpad != padlen).\n");
902*3192Sdanmcd 			esp1dbg(("lastpad (%d) not equal to padlen (%d):\n",
903*3192Sdanmcd 				    lastpad, padlen));
904*3192Sdanmcd 			ESP_BUMP_STAT(bad_padding);
905*3192Sdanmcd 			*counter = &ipdrops_esp_bad_padding;
906*3192Sdanmcd 			return (B_FALSE);
907*3192Sdanmcd 		}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 		/*
910*3192Sdanmcd 		 * Strong padding check: Check all pad bytes to see that
911*3192Sdanmcd 		 * they're ascending.  Go backwards using a descending counter
912*3192Sdanmcd 		 * to verify.  padlen == 1 is checked by previous block, so
913*3192Sdanmcd 		 * only bother if we've more than 1 byte of padding.
914*3192Sdanmcd 		 * Consequently, start the check one byte before the location
915*3192Sdanmcd 		 * of "lastpad".
9160Sstevel@tonic-gate 		 */
917*3192Sdanmcd 		if (ipsecesp_padding_check > 1) {
918*3192Sdanmcd 			/*
919*3192Sdanmcd 			 * This assert may have to become an if and a pullup
920*3192Sdanmcd 			 * if we start accepting multi-dblk mblks. For now,
921*3192Sdanmcd 			 * though, any packet here will have been pulled up in
922*3192Sdanmcd 			 * esp_inbound.
923*3192Sdanmcd 			 */
924*3192Sdanmcd 			ASSERT(MBLKL(scratch) >= lastpad + 3);
925*3192Sdanmcd 
926*3192Sdanmcd 			/*
927*3192Sdanmcd 			 * Use "--lastpad" because we already checked the very
928*3192Sdanmcd 			 * last pad byte previously.
929*3192Sdanmcd 			 */
930*3192Sdanmcd 			while (--lastpad != 0) {
931*3192Sdanmcd 				if (lastpad != *lastbyte) {
932*3192Sdanmcd 					ipsec_rl_strlog(info.mi_idnum, 0, 0,
933*3192Sdanmcd 					    SL_ERROR | SL_WARN, "Corrupt ESP "
934*3192Sdanmcd 					    "packet (bad padding).\n");
935*3192Sdanmcd 					esp1dbg(("padding not in correct"
936*3192Sdanmcd 						    " format:\n"));
937*3192Sdanmcd 					ESP_BUMP_STAT(bad_padding);
938*3192Sdanmcd 					*counter = &ipdrops_esp_bad_padding;
939*3192Sdanmcd 					return (B_FALSE);
940*3192Sdanmcd 				}
941*3192Sdanmcd 				lastbyte--;
9420Sstevel@tonic-gate 			}
9430Sstevel@tonic-gate 		}
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/* Trim off the padding. */
9470Sstevel@tonic-gate 	ASSERT(data_mp->b_cont == NULL);
9480Sstevel@tonic-gate 	data_mp->b_wptr -= (padlen + 2);
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	/*
9510Sstevel@tonic-gate 	 * Remove the ESP header.
9520Sstevel@tonic-gate 	 *
9530Sstevel@tonic-gate 	 * The above assertions about data_mp's size will make this work.
9540Sstevel@tonic-gate 	 *
9550Sstevel@tonic-gate 	 * XXX  Question:  If I send up and get back a contiguous mblk,
9560Sstevel@tonic-gate 	 * would it be quicker to bcopy over, or keep doing the dupb stuff?
9570Sstevel@tonic-gate 	 * I go with copying for now.
9580Sstevel@tonic-gate 	 */
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	if (IS_P2ALIGNED(data_mp->b_rptr, sizeof (uint32_t)) &&
9610Sstevel@tonic-gate 	    IS_P2ALIGNED(ivlen, sizeof (uint32_t))) {
9620Sstevel@tonic-gate 		uint8_t *start = data_mp->b_rptr;
9630Sstevel@tonic-gate 		uint32_t *src, *dst;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		src = (uint32_t *)(start + divpoint);
9660Sstevel@tonic-gate 		dst = (uint32_t *)(start + divpoint + sizeof (esph_t) + ivlen);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(dst, sizeof (uint32_t)) &&
9690Sstevel@tonic-gate 		    IS_P2ALIGNED(src, sizeof (uint32_t)));
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 		do {
9720Sstevel@tonic-gate 			src--;
9730Sstevel@tonic-gate 			dst--;
9740Sstevel@tonic-gate 			*dst = *src;
9750Sstevel@tonic-gate 		} while (src != (uint32_t *)start);
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 		data_mp->b_rptr = (uchar_t *)dst;
9780Sstevel@tonic-gate 	} else {
9790Sstevel@tonic-gate 		uint8_t *start = data_mp->b_rptr;
9800Sstevel@tonic-gate 		uint8_t *src, *dst;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 		src = start + divpoint;
9830Sstevel@tonic-gate 		dst = src + sizeof (esph_t) + ivlen;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		do {
9860Sstevel@tonic-gate 			src--;
9870Sstevel@tonic-gate 			dst--;
9880Sstevel@tonic-gate 			*dst = *src;
9890Sstevel@tonic-gate 		} while (src != start);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		data_mp->b_rptr = dst;
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	esp2dbg(("data_mp after inbound ESP adjustment:\n"));
9950Sstevel@tonic-gate 	esp2dbg((dump_msg(data_mp)));
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	return (B_TRUE);
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate  * Updating use times can be tricky business if the ipsa_haspeer flag is
10020Sstevel@tonic-gate  * set.  This function is called once in an SA's lifetime.
10030Sstevel@tonic-gate  *
10040Sstevel@tonic-gate  * Caller has to REFRELE "assoc" which is passed in.  This function has
10050Sstevel@tonic-gate  * to REFRELE any peer SA that is obtained.
10060Sstevel@tonic-gate  */
10070Sstevel@tonic-gate static void
10080Sstevel@tonic-gate esp_set_usetime(ipsa_t *assoc, boolean_t inbound)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
10110Sstevel@tonic-gate 	isaf_t *bucket;
10120Sstevel@tonic-gate 	sadb_t *sp;
10130Sstevel@tonic-gate 	int outhash;
10140Sstevel@tonic-gate 	boolean_t isv6;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	/* No peer?  No problem! */
10170Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
10180Sstevel@tonic-gate 		sadb_set_usetime(assoc);
10190Sstevel@tonic-gate 		return;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/*
10230Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
10240Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, the times
10250Sstevel@tonic-gate 	 * will be out-of-synch by at most a second, and since our time
10260Sstevel@tonic-gate 	 * granularity is a second, this won't be a problem.
10270Sstevel@tonic-gate 	 *
10280Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
10290Sstevel@tonic-gate 	 * reconsider.
10300Sstevel@tonic-gate 	 */
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/* Use address length to select IPv6/IPv4 */
10330Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
10340Sstevel@tonic-gate 	sp = isv6 ? &esp_sadb.s_v6 : &esp_sadb.s_v4;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	if (inbound) {
10370Sstevel@tonic-gate 		inassoc = assoc;
10380Sstevel@tonic-gate 		if (isv6) {
1039564Ssommerfe 			outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
10400Sstevel@tonic-gate 			    &inassoc->ipsa_dstaddr));
10410Sstevel@tonic-gate 		} else {
1042564Ssommerfe 			outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
10430Sstevel@tonic-gate 				&inassoc->ipsa_dstaddr));
10440Sstevel@tonic-gate 		}
10450Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
10460Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
10470Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
10480Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
10490Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
10500Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
10510Sstevel@tonic-gate 		if (outassoc == NULL) {
10520Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
10530Sstevel@tonic-gate 			esp0dbg(("esp_set_usetime: "
10540Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
10550Sstevel@tonic-gate 			sadb_set_usetime(inassoc);
10560Sstevel@tonic-gate 			return;
10570Sstevel@tonic-gate 		}
10580Sstevel@tonic-gate 	} else {
10590Sstevel@tonic-gate 		outassoc = assoc;
1060564Ssommerfe 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
10610Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
10620Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
10630Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
10640Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
10650Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
10660Sstevel@tonic-gate 		if (inassoc == NULL) {
10670Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
10680Sstevel@tonic-gate 			esp0dbg(("esp_set_usetime: "
10690Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
10700Sstevel@tonic-gate 			sadb_set_usetime(outassoc);
10710Sstevel@tonic-gate 			return;
10720Sstevel@tonic-gate 		}
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	/* Update usetime on both. */
10760Sstevel@tonic-gate 	sadb_set_usetime(inassoc);
10770Sstevel@tonic-gate 	sadb_set_usetime(outassoc);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	/*
10800Sstevel@tonic-gate 	 * REFRELE any peer SA.
10810Sstevel@tonic-gate 	 *
10820Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
10830Sstevel@tonic-gate 	 * them in { }.
10840Sstevel@tonic-gate 	 */
10850Sstevel@tonic-gate 	if (inbound) {
10860Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
10870Sstevel@tonic-gate 	} else {
10880Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate  * Handle ESP inbound data for IPv4 and IPv6.
10940Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
10950Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
10960Sstevel@tonic-gate  */
10970Sstevel@tonic-gate ipsec_status_t
10980Sstevel@tonic-gate esp_inbound(mblk_t *ipsec_in_mp, void *arg)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 	mblk_t *data_mp = ipsec_in_mp->b_cont;
11010Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
11020Sstevel@tonic-gate 	esph_t *esph = (esph_t *)arg;
11030Sstevel@tonic-gate 	ipsa_t *ipsa = ii->ipsec_in_esp_sa;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	if (ipsa->ipsa_usetime == 0)
11060Sstevel@tonic-gate 		esp_set_usetime(ipsa, B_TRUE);
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * We may wish to check replay in-range-only here as an optimization.
11100Sstevel@tonic-gate 	 * Include the reality check of ipsa->ipsa_replay >
11110Sstevel@tonic-gate 	 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
11120Sstevel@tonic-gate 	 * where N == ipsa->ipsa_replay_wsize.
11130Sstevel@tonic-gate 	 *
11140Sstevel@tonic-gate 	 * Another check that may come here later is the "collision" check.
11150Sstevel@tonic-gate 	 * If legitimate packets flow quickly enough, this won't be a problem,
11160Sstevel@tonic-gate 	 * but collisions may cause authentication algorithm crunching to
11170Sstevel@tonic-gate 	 * take place when it doesn't need to.
11180Sstevel@tonic-gate 	 */
11190Sstevel@tonic-gate 	if (!sadb_replay_peek(ipsa, esph->esph_replay)) {
11200Sstevel@tonic-gate 		ESP_BUMP_STAT(replay_early_failures);
11210Sstevel@tonic-gate 		IP_ESP_BUMP_STAT(in_discards);
11220Sstevel@tonic-gate 		/*
11230Sstevel@tonic-gate 		 * TODO: Extract inbound interface from the IPSEC_IN
11240Sstevel@tonic-gate 		 * message's ii->ipsec_in_rill_index.
11250Sstevel@tonic-gate 		 */
11260Sstevel@tonic-gate 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
11270Sstevel@tonic-gate 		    &ipdrops_esp_early_replay, &esp_dropper);
11280Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	/*
11320Sstevel@tonic-gate 	 * Has this packet already been processed by a hardware
11330Sstevel@tonic-gate 	 * IPsec accelerator?
11340Sstevel@tonic-gate 	 */
11350Sstevel@tonic-gate 	if (ii->ipsec_in_accelerated) {
11360Sstevel@tonic-gate 		ipsec_status_t rv;
11370Sstevel@tonic-gate 		esp3dbg(("esp_inbound: pkt processed by ill=%d isv6=%d\n",
11380Sstevel@tonic-gate 		    ii->ipsec_in_ill_index, !ii->ipsec_in_v4));
11390Sstevel@tonic-gate 		rv = esp_inbound_accelerated(ipsec_in_mp,
11400Sstevel@tonic-gate 		    data_mp, ii->ipsec_in_v4, ipsa);
11410Sstevel@tonic-gate 		return (rv);
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	ESP_BUMP_STAT(noaccel);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	/*
11460Sstevel@tonic-gate 	 * Adjust the IP header's payload length to reflect the removal
11470Sstevel@tonic-gate 	 * of the ICV.
11480Sstevel@tonic-gate 	 */
11490Sstevel@tonic-gate 	if (!ii->ipsec_in_v4) {
11500Sstevel@tonic-gate 		ip6_t *ip6h = (ip6_t *)data_mp->b_rptr;
11510Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) -
11520Sstevel@tonic-gate 		    ipsa->ipsa_mac_len);
11530Sstevel@tonic-gate 	} else {
11540Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
11550Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) -
11560Sstevel@tonic-gate 		    ipsa->ipsa_mac_len);
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	/* submit the request to the crypto framework */
11600Sstevel@tonic-gate 	return (esp_submit_req_inbound(ipsec_in_mp, ipsa,
11610Sstevel@tonic-gate 	    (uint8_t *)esph - data_mp->b_rptr));
11620Sstevel@tonic-gate }
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate /*
11650Sstevel@tonic-gate  * Perform the really difficult work of inserting the proposed situation.
11660Sstevel@tonic-gate  * Called while holding the algorithm lock.
11670Sstevel@tonic-gate  */
11680Sstevel@tonic-gate static void
11690Sstevel@tonic-gate esp_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs)
11700Sstevel@tonic-gate {
11710Sstevel@tonic-gate 	sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
11720Sstevel@tonic-gate 	ipsec_out_t *io;
11730Sstevel@tonic-gate 	ipsec_action_t *ap;
11740Sstevel@tonic-gate 	ipsec_prot_t *prot;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&alg_lock));
11770Sstevel@tonic-gate 	io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr;
11780Sstevel@tonic-gate 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
11810Sstevel@tonic-gate 	prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
11820Sstevel@tonic-gate 	*(uint32_t *)(&prop->sadb_prop_replay) = 0;	/* Quick zero-out! */
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	prop->sadb_prop_replay = ipsecesp_replay_size;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	/*
11870Sstevel@tonic-gate 	 * Based upon algorithm properties, and what-not, prioritize
11880Sstevel@tonic-gate 	 * a proposal.  If the IPSEC_OUT message has an algorithm specified,
11890Sstevel@tonic-gate 	 * use it first and foremost.
11900Sstevel@tonic-gate 	 *
11910Sstevel@tonic-gate 	 * For each action in policy list
11920Sstevel@tonic-gate 	 *   Add combination.  If I've hit limit, return.
11930Sstevel@tonic-gate 	 */
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	for (ap = acqrec->ipsacq_act; ap != NULL;
11960Sstevel@tonic-gate 	    ap = ap->ipa_next) {
11970Sstevel@tonic-gate 		ipsec_alginfo_t *ealg = NULL;
11980Sstevel@tonic-gate 		ipsec_alginfo_t *aalg = NULL;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		if (ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY)
12010Sstevel@tonic-gate 			continue;
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 		prot = &ap->ipa_act.ipa_apply;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 		if (!(prot->ipp_use_esp))
12060Sstevel@tonic-gate 			continue;
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 		if (prot->ipp_esp_auth_alg != 0) {
12090Sstevel@tonic-gate 			aalg = ipsec_alglists[IPSEC_ALG_AUTH]
12100Sstevel@tonic-gate 			    [prot->ipp_esp_auth_alg];
12110Sstevel@tonic-gate 			if (aalg == NULL || !ALG_VALID(aalg))
12120Sstevel@tonic-gate 				continue;
12130Sstevel@tonic-gate 		}
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 		ASSERT(prot->ipp_encr_alg > 0);
12160Sstevel@tonic-gate 		ealg = ipsec_alglists[IPSEC_ALG_ENCR][prot->ipp_encr_alg];
12170Sstevel@tonic-gate 		if (ealg == NULL || !ALG_VALID(ealg))
12180Sstevel@tonic-gate 			continue;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 		comb->sadb_comb_flags = 0;
12210Sstevel@tonic-gate 		comb->sadb_comb_reserved = 0;
12220Sstevel@tonic-gate 		comb->sadb_comb_encrypt = ealg->alg_id;
12232751Sdanmcd 		comb->sadb_comb_encrypt_minbits =
12242751Sdanmcd 		    MAX(prot->ipp_espe_minbits, ealg->alg_ef_minbits);
12252751Sdanmcd 		comb->sadb_comb_encrypt_maxbits =
12262751Sdanmcd 		    MIN(prot->ipp_espe_maxbits, ealg->alg_ef_maxbits);
12270Sstevel@tonic-gate 		if (aalg == NULL) {
12280Sstevel@tonic-gate 			comb->sadb_comb_auth = 0;
12290Sstevel@tonic-gate 			comb->sadb_comb_auth_minbits = 0;
12300Sstevel@tonic-gate 			comb->sadb_comb_auth_maxbits = 0;
12310Sstevel@tonic-gate 		} else {
12320Sstevel@tonic-gate 			comb->sadb_comb_auth = aalg->alg_id;
12332751Sdanmcd 			comb->sadb_comb_auth_minbits =
12342751Sdanmcd 			    MAX(prot->ipp_espa_minbits, aalg->alg_ef_minbits);
12352751Sdanmcd 			comb->sadb_comb_auth_maxbits =
12362751Sdanmcd 			    MIN(prot->ipp_espa_maxbits, aalg->alg_ef_maxbits);
12370Sstevel@tonic-gate 		}
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 		/*
12400Sstevel@tonic-gate 		 * The following may be based on algorithm
12410Sstevel@tonic-gate 		 * properties, but in the meantime, we just pick
12420Sstevel@tonic-gate 		 * some good, sensible numbers.  Key mgmt. can
12430Sstevel@tonic-gate 		 * (and perhaps should) be the place to finalize
12440Sstevel@tonic-gate 		 * such decisions.
12450Sstevel@tonic-gate 		 */
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 		/*
12480Sstevel@tonic-gate 		 * No limits on allocations, since we really don't
12490Sstevel@tonic-gate 		 * support that concept currently.
12500Sstevel@tonic-gate 		 */
12510Sstevel@tonic-gate 		comb->sadb_comb_soft_allocations = 0;
12520Sstevel@tonic-gate 		comb->sadb_comb_hard_allocations = 0;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 		/*
12550Sstevel@tonic-gate 		 * These may want to come from policy rule..
12560Sstevel@tonic-gate 		 */
12570Sstevel@tonic-gate 		comb->sadb_comb_soft_bytes = ipsecesp_default_soft_bytes;
12580Sstevel@tonic-gate 		comb->sadb_comb_hard_bytes = ipsecesp_default_hard_bytes;
12590Sstevel@tonic-gate 		comb->sadb_comb_soft_addtime = ipsecesp_default_soft_addtime;
12600Sstevel@tonic-gate 		comb->sadb_comb_hard_addtime = ipsecesp_default_hard_addtime;
12610Sstevel@tonic-gate 		comb->sadb_comb_soft_usetime = ipsecesp_default_soft_usetime;
12620Sstevel@tonic-gate 		comb->sadb_comb_hard_usetime = ipsecesp_default_hard_usetime;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
12650Sstevel@tonic-gate 		if (--combs == 0)
12660Sstevel@tonic-gate 			break;	/* out of space.. */
12670Sstevel@tonic-gate 		comb++;
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate /*
12720Sstevel@tonic-gate  * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
12730Sstevel@tonic-gate  */
12740Sstevel@tonic-gate static void
12750Sstevel@tonic-gate esp_send_acquire(ipsacq_t *acqrec, mblk_t *extended)
12760Sstevel@tonic-gate {
12773055Sdanmcd 	uint_t combs;
12780Sstevel@tonic-gate 	sadb_msg_t *samsg;
12790Sstevel@tonic-gate 	sadb_prop_t *prop;
12803055Sdanmcd 	mblk_t *pfkeymp, *msgmp;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	ESP_BUMP_STAT(acquire_requests);
12830Sstevel@tonic-gate 
12843055Sdanmcd 	if (esp_pfkey_q == NULL)
12853055Sdanmcd 		return;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	/* Set up ACQUIRE. */
12883055Sdanmcd 	pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_ESP);
12893055Sdanmcd 	if (pfkeymp == NULL) {
12900Sstevel@tonic-gate 		esp0dbg(("sadb_setup_acquire failed.\n"));
12913055Sdanmcd 		return;
12920Sstevel@tonic-gate 	}
12933055Sdanmcd 	ASSERT(MUTEX_HELD(&alg_lock));
12943055Sdanmcd 	combs = ipsec_nalgs[IPSEC_ALG_AUTH] * ipsec_nalgs[IPSEC_ALG_ENCR];
12953055Sdanmcd 	msgmp = pfkeymp->b_cont;
12963055Sdanmcd 	samsg = (sadb_msg_t *)(msgmp->b_rptr);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	/* Insert proposal here. */
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
13010Sstevel@tonic-gate 	esp_insert_prop(prop, acqrec, combs);
13020Sstevel@tonic-gate 	samsg->sadb_msg_len += prop->sadb_prop_len;
13030Sstevel@tonic-gate 	msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	mutex_exit(&alg_lock);
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	/*
13080Sstevel@tonic-gate 	 * Must mutex_exit() before sending PF_KEY message up, in
13090Sstevel@tonic-gate 	 * order to avoid recursive mutex_enter() if there are no registered
13100Sstevel@tonic-gate 	 * listeners.
13110Sstevel@tonic-gate 	 *
13120Sstevel@tonic-gate 	 * Once I've sent the message, I'm cool anyway.
13130Sstevel@tonic-gate 	 */
13140Sstevel@tonic-gate 	mutex_exit(&acqrec->ipsacq_lock);
13153055Sdanmcd 	if (extended != NULL) {
13163055Sdanmcd 		putnext(esp_pfkey_q, extended);
13170Sstevel@tonic-gate 	}
13183055Sdanmcd 	putnext(esp_pfkey_q, pfkeymp);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate /*
13220Sstevel@tonic-gate  * Handle the SADB_GETSPI message.  Create a larval SA.
13230Sstevel@tonic-gate  */
13240Sstevel@tonic-gate static void
13250Sstevel@tonic-gate esp_getspi(mblk_t *mp, keysock_in_t *ksi)
13260Sstevel@tonic-gate {
13270Sstevel@tonic-gate 	ipsa_t *newbie, *target;
13280Sstevel@tonic-gate 	isaf_t *outbound, *inbound;
13290Sstevel@tonic-gate 	int rc, diagnostic;
13300Sstevel@tonic-gate 	sadb_sa_t *assoc;
13310Sstevel@tonic-gate 	keysock_out_t *kso;
13320Sstevel@tonic-gate 	uint32_t newspi;
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	/*
13350Sstevel@tonic-gate 	 * Randomly generate a proposed SPI value
13360Sstevel@tonic-gate 	 */
13370Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t));
13380Sstevel@tonic-gate 	newbie = sadb_getspi(ksi, newspi, &diagnostic);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	if (newbie == NULL) {
13410Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, ENOMEM, diagnostic,
13420Sstevel@tonic-gate 		    ksi->ks_in_serial);
13430Sstevel@tonic-gate 		return;
13440Sstevel@tonic-gate 	} else if (newbie == (ipsa_t *)-1) {
13450Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, EINVAL, diagnostic,
13460Sstevel@tonic-gate 		    ksi->ks_in_serial);
13470Sstevel@tonic-gate 		return;
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	/*
13510Sstevel@tonic-gate 	 * XXX - We may randomly collide.  We really should recover from this.
13520Sstevel@tonic-gate 	 *	 Unfortunately, that could require spending way-too-much-time
13530Sstevel@tonic-gate 	 *	 in here.  For now, let the user retry.
13540Sstevel@tonic-gate 	 */
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	if (newbie->ipsa_addrfam == AF_INET6) {
1357564Ssommerfe 		outbound = OUTBOUND_BUCKET_V6(&esp_sadb.s_v6,
1358564Ssommerfe 		    *(uint32_t *)(newbie->ipsa_dstaddr));
1359564Ssommerfe 		inbound = INBOUND_BUCKET(&esp_sadb.s_v6, newbie->ipsa_spi);
13600Sstevel@tonic-gate 	} else {
13610Sstevel@tonic-gate 		ASSERT(newbie->ipsa_addrfam == AF_INET);
1362564Ssommerfe 		outbound = OUTBOUND_BUCKET_V4(&esp_sadb.s_v4,
1363564Ssommerfe 		    *(uint32_t *)(newbie->ipsa_dstaddr));
1364564Ssommerfe 		inbound = INBOUND_BUCKET(&esp_sadb.s_v4, newbie->ipsa_spi);
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	mutex_enter(&outbound->isaf_lock);
13680Sstevel@tonic-gate 	mutex_enter(&inbound->isaf_lock);
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	/*
13710Sstevel@tonic-gate 	 * Check for collisions (i.e. did sadb_getspi() return with something
13720Sstevel@tonic-gate 	 * that already exists?).
13730Sstevel@tonic-gate 	 *
13740Sstevel@tonic-gate 	 * Try outbound first.  Even though SADB_GETSPI is traditionally
13750Sstevel@tonic-gate 	 * for inbound SAs, you never know what a user might do.
13760Sstevel@tonic-gate 	 */
13770Sstevel@tonic-gate 	target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
13780Sstevel@tonic-gate 	    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
13790Sstevel@tonic-gate 	if (target == NULL) {
13800Sstevel@tonic-gate 		target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
13810Sstevel@tonic-gate 		    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
13820Sstevel@tonic-gate 		    newbie->ipsa_addrfam);
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	/*
13860Sstevel@tonic-gate 	 * I don't have collisions elsewhere!
13870Sstevel@tonic-gate 	 * (Nor will I because I'm still holding inbound/outbound locks.)
13880Sstevel@tonic-gate 	 */
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	if (target != NULL) {
13910Sstevel@tonic-gate 		rc = EEXIST;
13920Sstevel@tonic-gate 		IPSA_REFRELE(target);
13930Sstevel@tonic-gate 	} else {
13940Sstevel@tonic-gate 		/*
13950Sstevel@tonic-gate 		 * sadb_insertassoc() also checks for collisions, so
13960Sstevel@tonic-gate 		 * if there's a colliding entry, rc will be set
13970Sstevel@tonic-gate 		 * to EEXIST.
13980Sstevel@tonic-gate 		 */
13990Sstevel@tonic-gate 		rc = sadb_insertassoc(newbie, inbound);
14000Sstevel@tonic-gate 		(void) drv_getparm(TIME, &newbie->ipsa_hardexpiretime);
14010Sstevel@tonic-gate 		newbie->ipsa_hardexpiretime += ipsecesp_larval_timeout;
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	/*
14050Sstevel@tonic-gate 	 * Can exit outbound mutex.  Hold inbound until we're done
14060Sstevel@tonic-gate 	 * with newbie.
14070Sstevel@tonic-gate 	 */
14080Sstevel@tonic-gate 	mutex_exit(&outbound->isaf_lock);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	if (rc != 0) {
14110Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
14120Sstevel@tonic-gate 		IPSA_REFRELE(newbie);
14130Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, rc, SADB_X_DIAGNOSTIC_NONE,
14140Sstevel@tonic-gate 		    ksi->ks_in_serial);
14150Sstevel@tonic-gate 		return;
14160Sstevel@tonic-gate 	}
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	/* Can write here because I'm still holding the bucket lock. */
14200Sstevel@tonic-gate 	newbie->ipsa_type = SADB_SATYPE_ESP;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	/*
14230Sstevel@tonic-gate 	 * Construct successful return message.  We have one thing going
14240Sstevel@tonic-gate 	 * for us in PF_KEY v2.  That's the fact that
14250Sstevel@tonic-gate 	 *	sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
14260Sstevel@tonic-gate 	 */
14270Sstevel@tonic-gate 	assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
14280Sstevel@tonic-gate 	assoc->sadb_sa_exttype = SADB_EXT_SA;
14290Sstevel@tonic-gate 	assoc->sadb_sa_spi = newbie->ipsa_spi;
14300Sstevel@tonic-gate 	*((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
14310Sstevel@tonic-gate 	mutex_exit(&inbound->isaf_lock);
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	/* Convert KEYSOCK_IN to KEYSOCK_OUT. */
14340Sstevel@tonic-gate 	kso = (keysock_out_t *)ksi;
14350Sstevel@tonic-gate 	kso->ks_out_len = sizeof (*kso);
14360Sstevel@tonic-gate 	kso->ks_out_serial = ksi->ks_in_serial;
14370Sstevel@tonic-gate 	kso->ks_out_type = KEYSOCK_OUT;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	/*
14400Sstevel@tonic-gate 	 * Can safely putnext() to esp_pfkey_q, because this is a turnaround
14410Sstevel@tonic-gate 	 * from the esp_pfkey_q.
14420Sstevel@tonic-gate 	 */
14430Sstevel@tonic-gate 	putnext(esp_pfkey_q, mp);
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate /*
14470Sstevel@tonic-gate  * Insert the ESP header into a packet.  Duplicate an mblk, and insert a newly
14480Sstevel@tonic-gate  * allocated mblk with the ESP header in between the two.
14490Sstevel@tonic-gate  */
14500Sstevel@tonic-gate static boolean_t
14510Sstevel@tonic-gate esp_insert_esp(mblk_t *mp, mblk_t *esp_mp, uint_t divpoint)
14520Sstevel@tonic-gate {
14530Sstevel@tonic-gate 	mblk_t *split_mp = mp;
14540Sstevel@tonic-gate 	uint_t wheretodiv = divpoint;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	while ((split_mp->b_wptr - split_mp->b_rptr) < wheretodiv) {
14570Sstevel@tonic-gate 		wheretodiv -= (split_mp->b_wptr - split_mp->b_rptr);
14580Sstevel@tonic-gate 		split_mp = split_mp->b_cont;
14590Sstevel@tonic-gate 		ASSERT(split_mp != NULL);
14600Sstevel@tonic-gate 	}
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	if (split_mp->b_wptr - split_mp->b_rptr != wheretodiv) {
14630Sstevel@tonic-gate 		mblk_t *scratch;
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 		/* "scratch" is the 2nd half, split_mp is the first. */
14660Sstevel@tonic-gate 		scratch = dupb(split_mp);
14670Sstevel@tonic-gate 		if (scratch == NULL) {
14680Sstevel@tonic-gate 			esp1dbg(("esp_insert_esp: can't allocate scratch.\n"));
14690Sstevel@tonic-gate 			return (B_FALSE);
14700Sstevel@tonic-gate 		}
14710Sstevel@tonic-gate 		/* NOTE:  dupb() doesn't set b_cont appropriately. */
14720Sstevel@tonic-gate 		scratch->b_cont = split_mp->b_cont;
14730Sstevel@tonic-gate 		scratch->b_rptr += wheretodiv;
14740Sstevel@tonic-gate 		split_mp->b_wptr = split_mp->b_rptr + wheretodiv;
14750Sstevel@tonic-gate 		split_mp->b_cont = scratch;
14760Sstevel@tonic-gate 	}
14770Sstevel@tonic-gate 	/*
14780Sstevel@tonic-gate 	 * At this point, split_mp is exactly "wheretodiv" bytes long, and
14790Sstevel@tonic-gate 	 * holds the end of the pre-ESP part of the datagram.
14800Sstevel@tonic-gate 	 */
14810Sstevel@tonic-gate 	esp_mp->b_cont = split_mp->b_cont;
14820Sstevel@tonic-gate 	split_mp->b_cont = esp_mp;
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	return (B_TRUE);
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate /*
14880Sstevel@tonic-gate  * Finish processing of an inbound ESP packet after processing by the
14890Sstevel@tonic-gate  * crypto framework.
14900Sstevel@tonic-gate  * - Remove the ESP header.
14910Sstevel@tonic-gate  * - Send packet back to IP.
14920Sstevel@tonic-gate  * If authentication was performed on the packet, this function is called
14930Sstevel@tonic-gate  * only if the authentication succeeded.
14940Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
14950Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
14960Sstevel@tonic-gate  */
14970Sstevel@tonic-gate static ipsec_status_t
14980Sstevel@tonic-gate esp_in_done(mblk_t *ipsec_in_mp)
14990Sstevel@tonic-gate {
15000Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
15010Sstevel@tonic-gate 	mblk_t *data_mp;
15020Sstevel@tonic-gate 	ipsa_t *assoc;
15030Sstevel@tonic-gate 	uint_t espstart;
15040Sstevel@tonic-gate 	uint32_t ivlen = 0;
15050Sstevel@tonic-gate 	uint_t processed_len;
15060Sstevel@tonic-gate 	esph_t *esph;
15070Sstevel@tonic-gate 	kstat_named_t *counter;
15080Sstevel@tonic-gate 	boolean_t is_natt;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	assoc = ii->ipsec_in_esp_sa;
15110Sstevel@tonic-gate 	ASSERT(assoc != NULL);
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	/* get the pointer to the ESP header */
15160Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg == SADB_EALG_NULL) {
15170Sstevel@tonic-gate 		/* authentication-only ESP */
15180Sstevel@tonic-gate 		espstart = ii->ipsec_in_crypto_data.cd_offset;
15190Sstevel@tonic-gate 		processed_len = ii->ipsec_in_crypto_data.cd_length;
15200Sstevel@tonic-gate 	} else {
15210Sstevel@tonic-gate 		/* encryption present */
15220Sstevel@tonic-gate 		ivlen = assoc->ipsa_iv_len;
15230Sstevel@tonic-gate 		if (assoc->ipsa_auth_alg == SADB_AALG_NONE) {
15240Sstevel@tonic-gate 			/* encryption-only ESP */
15250Sstevel@tonic-gate 			espstart = ii->ipsec_in_crypto_data.cd_offset -
15260Sstevel@tonic-gate 				sizeof (esph_t) - assoc->ipsa_iv_len;
15270Sstevel@tonic-gate 			processed_len = ii->ipsec_in_crypto_data.cd_length +
15280Sstevel@tonic-gate 				ivlen;
15290Sstevel@tonic-gate 		} else {
15300Sstevel@tonic-gate 			/* encryption with authentication */
15310Sstevel@tonic-gate 			espstart = ii->ipsec_in_crypto_dual_data.dd_offset1;
15320Sstevel@tonic-gate 			processed_len = ii->ipsec_in_crypto_dual_data.dd_len2 +
15330Sstevel@tonic-gate 			    ivlen;
15340Sstevel@tonic-gate 		}
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	data_mp = ipsec_in_mp->b_cont;
15380Sstevel@tonic-gate 	esph = (esph_t *)(data_mp->b_rptr + espstart);
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) {
15410Sstevel@tonic-gate 		/* authentication passed if we reach this point */
15420Sstevel@tonic-gate 		ESP_BUMP_STAT(good_auth);
15430Sstevel@tonic-gate 		data_mp->b_wptr -= assoc->ipsa_mac_len;
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 		/*
15460Sstevel@tonic-gate 		 * Check replay window here!
15470Sstevel@tonic-gate 		 * For right now, assume keysock will set the replay window
15480Sstevel@tonic-gate 		 * size to zero for SAs that have an unspecified sender.
15490Sstevel@tonic-gate 		 * This may change...
15500Sstevel@tonic-gate 		 */
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 		if (!sadb_replay_check(assoc, esph->esph_replay)) {
15530Sstevel@tonic-gate 			/*
15540Sstevel@tonic-gate 			 * Log the event. As of now we print out an event.
15550Sstevel@tonic-gate 			 * Do not print the replay failure number, or else
15560Sstevel@tonic-gate 			 * syslog cannot collate the error messages.  Printing
15570Sstevel@tonic-gate 			 * the replay number that failed opens a denial-of-
15580Sstevel@tonic-gate 			 * service attack.
15590Sstevel@tonic-gate 			 */
15600Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
15610Sstevel@tonic-gate 			    SL_ERROR | SL_WARN,
15620Sstevel@tonic-gate 			    "Replay failed for ESP spi 0x%x, dst %s.\n",
15630Sstevel@tonic-gate 			    assoc->ipsa_spi, assoc->ipsa_dstaddr,
15640Sstevel@tonic-gate 			    assoc->ipsa_addrfam);
15650Sstevel@tonic-gate 			ESP_BUMP_STAT(replay_failures);
15660Sstevel@tonic-gate 			counter = &ipdrops_esp_replay;
15670Sstevel@tonic-gate 			goto drop_and_bail;
15680Sstevel@tonic-gate 		}
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	if (!esp_age_bytes(assoc, processed_len, B_TRUE)) {
15720Sstevel@tonic-gate 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
15730Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
15740Sstevel@tonic-gate 		    SL_ERROR | SL_WARN,
15750Sstevel@tonic-gate 		    "ESP association 0x%x, dst %s had bytes expire.\n",
15760Sstevel@tonic-gate 		    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam);
15770Sstevel@tonic-gate 		ESP_BUMP_STAT(bytes_expired);
15780Sstevel@tonic-gate 		counter = &ipdrops_esp_bytes_expire;
15790Sstevel@tonic-gate 		goto drop_and_bail;
15800Sstevel@tonic-gate 	}
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	/*
15830Sstevel@tonic-gate 	 * Remove ESP header and padding from packet.  I hope the compiler
15840Sstevel@tonic-gate 	 * spews "branch, predict taken" code for this.
15850Sstevel@tonic-gate 	 */
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	if (esp_strip_header(data_mp, ii->ipsec_in_v4, ivlen, &counter)) {
15880Sstevel@tonic-gate 		if (is_natt)
15890Sstevel@tonic-gate 			return (esp_fix_natt_checksums(data_mp, assoc));
15900Sstevel@tonic-gate 		return (IPSEC_STATUS_SUCCESS);
15910Sstevel@tonic-gate 	}
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	esp1dbg(("esp_in_done: esp_strip_header() failed\n"));
15940Sstevel@tonic-gate drop_and_bail:
15950Sstevel@tonic-gate 	IP_ESP_BUMP_STAT(in_discards);
15960Sstevel@tonic-gate 	/*
15970Sstevel@tonic-gate 	 * TODO: Extract inbound interface from the IPSEC_IN message's
15980Sstevel@tonic-gate 	 * ii->ipsec_in_rill_index.
15990Sstevel@tonic-gate 	 */
16000Sstevel@tonic-gate 	ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, counter, &esp_dropper);
16010Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate /*
16050Sstevel@tonic-gate  * Called upon failing the inbound ICV check. The message passed as
16060Sstevel@tonic-gate  * argument is freed.
16070Sstevel@tonic-gate  */
16080Sstevel@tonic-gate static void
16090Sstevel@tonic-gate esp_log_bad_auth(mblk_t *ipsec_in)
16100Sstevel@tonic-gate {
16110Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
16120Sstevel@tonic-gate 	ipsa_t *assoc = ii->ipsec_in_esp_sa;
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	/*
16150Sstevel@tonic-gate 	 * Log the event. Don't print to the console, block
16160Sstevel@tonic-gate 	 * potential denial-of-service attack.
16170Sstevel@tonic-gate 	 */
16180Sstevel@tonic-gate 	ESP_BUMP_STAT(bad_auth);
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
16210Sstevel@tonic-gate 	    "ESP Authentication failed for spi 0x%x, dst %s.\n",
16220Sstevel@tonic-gate 	    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam);
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	IP_ESP_BUMP_STAT(in_discards);
16250Sstevel@tonic-gate 	/*
16260Sstevel@tonic-gate 	 * TODO: Extract inbound interface from the IPSEC_IN
16270Sstevel@tonic-gate 	 * message's ii->ipsec_in_rill_index.
16280Sstevel@tonic-gate 	 */
16290Sstevel@tonic-gate 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_esp_bad_auth,
16300Sstevel@tonic-gate 	    &esp_dropper);
16310Sstevel@tonic-gate }
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate /*
16350Sstevel@tonic-gate  * Invoked for outbound packets after ESP processing. If the packet
16360Sstevel@tonic-gate  * also requires AH, performs the AH SA selection and AH processing.
16370Sstevel@tonic-gate  * Returns B_TRUE if the AH processing was not needed or if it was
16380Sstevel@tonic-gate  * performed successfully. Returns B_FALSE and consumes the passed mblk
16390Sstevel@tonic-gate  * if AH processing was required but could not be performed.
16400Sstevel@tonic-gate  */
16410Sstevel@tonic-gate static boolean_t
16420Sstevel@tonic-gate esp_do_outbound_ah(mblk_t *ipsec_mp)
16430Sstevel@tonic-gate {
16440Sstevel@tonic-gate 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
16450Sstevel@tonic-gate 	ipsec_status_t ipsec_rc;
16460Sstevel@tonic-gate 	ipsec_action_t *ap;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	ap = io->ipsec_out_act;
16490Sstevel@tonic-gate 	if (ap == NULL) {
16500Sstevel@tonic-gate 		ipsec_policy_t *pp = io->ipsec_out_policy;
16510Sstevel@tonic-gate 		ap = pp->ipsp_act;
16520Sstevel@tonic-gate 	}
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	if (!ap->ipa_want_ah)
16550Sstevel@tonic-gate 		return (B_TRUE);
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	ASSERT(io->ipsec_out_ah_done == B_FALSE);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	if (io->ipsec_out_ah_sa == NULL) {
16600Sstevel@tonic-gate 		if (!ipsec_outbound_sa(ipsec_mp, IPPROTO_AH)) {
16610Sstevel@tonic-gate 			sadb_acquire(ipsec_mp, io, B_TRUE, B_FALSE);
16620Sstevel@tonic-gate 			return (B_FALSE);
16630Sstevel@tonic-gate 		}
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate 	ASSERT(io->ipsec_out_ah_sa != NULL);
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	io->ipsec_out_ah_done = B_TRUE;
16680Sstevel@tonic-gate 	ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp);
16690Sstevel@tonic-gate 	return (ipsec_rc == IPSEC_STATUS_SUCCESS);
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate /*
16740Sstevel@tonic-gate  * Kernel crypto framework callback invoked after completion of async
16750Sstevel@tonic-gate  * crypto requests.
16760Sstevel@tonic-gate  */
16770Sstevel@tonic-gate static void
16780Sstevel@tonic-gate esp_kcf_callback(void *arg, int status)
16790Sstevel@tonic-gate {
16800Sstevel@tonic-gate 	mblk_t *ipsec_mp = (mblk_t *)arg;
16810Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
16820Sstevel@tonic-gate 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	ASSERT(ipsec_mp->b_cont != NULL);
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	if (status == CRYPTO_SUCCESS) {
16870Sstevel@tonic-gate 		if (is_inbound) {
16880Sstevel@tonic-gate 			if (esp_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS)
16890Sstevel@tonic-gate 				return;
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 			/* finish IPsec processing */
16920Sstevel@tonic-gate 			ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL);
16930Sstevel@tonic-gate 		} else {
16940Sstevel@tonic-gate 			/*
16950Sstevel@tonic-gate 			 * If a ICV was computed, it was stored by the
16960Sstevel@tonic-gate 			 * crypto framework at the end of the packet.
16970Sstevel@tonic-gate 			 */
16980Sstevel@tonic-gate 			ipha_t *ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 			/* do AH processing if needed */
17010Sstevel@tonic-gate 			if (!esp_do_outbound_ah(ipsec_mp))
17020Sstevel@tonic-gate 				return;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 			/* finish IPsec processing */
17050Sstevel@tonic-gate 			if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
17060Sstevel@tonic-gate 				ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL,
17070Sstevel@tonic-gate 				    NULL);
17080Sstevel@tonic-gate 			} else {
17090Sstevel@tonic-gate 				ip6_t *ip6h = (ip6_t *)ipha;
17100Sstevel@tonic-gate 				ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h,
17110Sstevel@tonic-gate 				    NULL, NULL);
17120Sstevel@tonic-gate 			}
17130Sstevel@tonic-gate 		}
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	} else if (status == CRYPTO_INVALID_MAC) {
17160Sstevel@tonic-gate 		esp_log_bad_auth(ipsec_mp);
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	} else {
17190Sstevel@tonic-gate 		esp1dbg(("esp_kcf_callback: crypto failed with 0x%x\n",
17200Sstevel@tonic-gate 		    status));
17210Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_failures);
17220Sstevel@tonic-gate 		if (is_inbound)
17230Sstevel@tonic-gate 			IP_ESP_BUMP_STAT(in_discards);
17240Sstevel@tonic-gate 		else
17250Sstevel@tonic-gate 			ESP_BUMP_STAT(out_discards);
17260Sstevel@tonic-gate 		ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL,
17270Sstevel@tonic-gate 		    &ipdrops_esp_crypto_failed, &esp_dropper);
17280Sstevel@tonic-gate 	}
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate /*
17320Sstevel@tonic-gate  * Invoked on crypto framework failure during inbound and outbound processing.
17330Sstevel@tonic-gate  */
17340Sstevel@tonic-gate static void
17350Sstevel@tonic-gate esp_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc)
17360Sstevel@tonic-gate {
17370Sstevel@tonic-gate 	esp1dbg(("crypto failed for %s ESP with 0x%x\n",
17380Sstevel@tonic-gate 	    is_inbound ? "inbound" : "outbound", kef_rc));
17390Sstevel@tonic-gate 	ip_drop_packet(mp, is_inbound, NULL, NULL, &ipdrops_esp_crypto_failed,
17400Sstevel@tonic-gate 	    &esp_dropper);
17410Sstevel@tonic-gate 	ESP_BUMP_STAT(crypto_failures);
17420Sstevel@tonic-gate 	if (is_inbound)
17430Sstevel@tonic-gate 		IP_ESP_BUMP_STAT(in_discards);
17440Sstevel@tonic-gate 	else
17450Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate #define	ESP_INIT_CALLREQ(_cr) {						\
17490Sstevel@tonic-gate 	(_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED;		\
17500Sstevel@tonic-gate 	(_cr)->cr_callback_arg = ipsec_mp;				\
17510Sstevel@tonic-gate 	(_cr)->cr_callback_func = esp_kcf_callback;			\
17520Sstevel@tonic-gate }
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) {			\
17550Sstevel@tonic-gate 	(mac)->cd_format = CRYPTO_DATA_RAW;				\
17560Sstevel@tonic-gate 	(mac)->cd_offset = 0;						\
17570Sstevel@tonic-gate 	(mac)->cd_length = icvlen;					\
17580Sstevel@tonic-gate 	(mac)->cd_raw.iov_base = (char *)icvbuf;			\
17590Sstevel@tonic-gate 	(mac)->cd_raw.iov_len = icvlen;					\
17600Sstevel@tonic-gate }
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_DATA(data, mp, off, len) {			\
17630Sstevel@tonic-gate 	if (MBLKL(mp) >= (len) + (off)) {				\
17640Sstevel@tonic-gate 		(data)->cd_format = CRYPTO_DATA_RAW;			\
17650Sstevel@tonic-gate 		(data)->cd_raw.iov_base = (char *)(mp)->b_rptr;		\
17660Sstevel@tonic-gate 		(data)->cd_raw.iov_len = MBLKL(mp);			\
17670Sstevel@tonic-gate 		(data)->cd_offset = off;				\
17680Sstevel@tonic-gate 	} else {							\
17690Sstevel@tonic-gate 		(data)->cd_format = CRYPTO_DATA_MBLK;			\
17700Sstevel@tonic-gate 		(data)->cd_mp = mp;			       		\
17710Sstevel@tonic-gate 		(data)->cd_offset = off;				\
17720Sstevel@tonic-gate 	}								\
17730Sstevel@tonic-gate 	(data)->cd_length = len;					\
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate #define	ESP_INIT_CRYPTO_DUAL_DATA(data, mp, off1, len1, off2, len2) {	\
17770Sstevel@tonic-gate 	(data)->dd_format = CRYPTO_DATA_MBLK;				\
17780Sstevel@tonic-gate 	(data)->dd_mp = mp;						\
17790Sstevel@tonic-gate 	(data)->dd_len1 = len1;						\
17800Sstevel@tonic-gate 	(data)->dd_offset1 = off1;					\
17810Sstevel@tonic-gate 	(data)->dd_len2 = len2;						\
17820Sstevel@tonic-gate 	(data)->dd_offset2 = off2;					\
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate static ipsec_status_t
17860Sstevel@tonic-gate esp_submit_req_inbound(mblk_t *ipsec_mp, ipsa_t *assoc, uint_t esph_offset)
17870Sstevel@tonic-gate {
17880Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
17890Sstevel@tonic-gate 	boolean_t do_auth;
17900Sstevel@tonic-gate 	uint_t auth_offset, msg_len, auth_len;
17910Sstevel@tonic-gate 	crypto_call_req_t call_req;
17920Sstevel@tonic-gate 	mblk_t *esp_mp;
17930Sstevel@tonic-gate 	int kef_rc = CRYPTO_FAILED;
17940Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
17950Sstevel@tonic-gate 	crypto_ctx_template_t auth_ctx_tmpl;
17960Sstevel@tonic-gate 	boolean_t do_encr;
17970Sstevel@tonic-gate 	uint_t encr_offset, encr_len;
17980Sstevel@tonic-gate 	uint_t iv_len = assoc->ipsa_iv_len;
17990Sstevel@tonic-gate 	crypto_ctx_template_t encr_ctx_tmpl;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
18040Sstevel@tonic-gate 	do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	/*
18070Sstevel@tonic-gate 	 * An inbound packet is of the form:
18080Sstevel@tonic-gate 	 * IPSEC_IN -> [IP,options,ESP,IV,data,ICV,pad]
18090Sstevel@tonic-gate 	 */
18100Sstevel@tonic-gate 	esp_mp = ipsec_mp->b_cont;
18110Sstevel@tonic-gate 	msg_len = MBLKL(esp_mp);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	ESP_INIT_CALLREQ(&call_req);
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	if (do_auth) {
18160Sstevel@tonic-gate 		/* force asynchronous processing? */
18170Sstevel@tonic-gate 		if (ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
18180Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
18190Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 		/* authentication context template */
18220Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
18230Sstevel@tonic-gate 		    auth_ctx_tmpl);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 		/* ICV to be verified */
18260Sstevel@tonic-gate 		ESP_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac,
18270Sstevel@tonic-gate 		    icv_len, esp_mp->b_wptr - icv_len);
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 		/* authentication starts at the ESP header */
18300Sstevel@tonic-gate 		auth_offset = esph_offset;
18310Sstevel@tonic-gate 		auth_len = msg_len - auth_offset - icv_len;
18320Sstevel@tonic-gate 		if (!do_encr) {
18330Sstevel@tonic-gate 			/* authentication only */
18340Sstevel@tonic-gate 			/* initialize input data argument */
18350Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data,
18360Sstevel@tonic-gate 			    esp_mp, auth_offset, auth_len);
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 			/* call the crypto framework */
18390Sstevel@tonic-gate 			kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
18400Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_data,
18410Sstevel@tonic-gate 			    &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
18420Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_mac, &call_req);
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	if (do_encr) {
18470Sstevel@tonic-gate 		/* force asynchronous processing? */
18480Sstevel@tonic-gate 		if (ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
18490Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
18500Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 		/* encryption template */
18530Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
18540Sstevel@tonic-gate 		    encr_ctx_tmpl);
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 		/* skip IV, since it is passed separately */
18570Sstevel@tonic-gate 		encr_offset = esph_offset + sizeof (esph_t) + iv_len;
18580Sstevel@tonic-gate 		encr_len = msg_len - encr_offset;
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 		if (!do_auth) {
18610Sstevel@tonic-gate 			/* decryption only */
18620Sstevel@tonic-gate 			/* initialize input data argument */
18630Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data,
18640Sstevel@tonic-gate 			    esp_mp, encr_offset, encr_len);
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 			/* specify IV */
18670Sstevel@tonic-gate 			ii->ipsec_in_crypto_data.cd_miscdata =
18680Sstevel@tonic-gate 			    (char *)esp_mp->b_rptr + sizeof (esph_t) +
18690Sstevel@tonic-gate 			    esph_offset;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 			/* call the crypto framework */
18720Sstevel@tonic-gate 			kef_rc = crypto_decrypt(&assoc->ipsa_emech,
18730Sstevel@tonic-gate 			    &ii->ipsec_in_crypto_data,
18740Sstevel@tonic-gate 			    &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
18750Sstevel@tonic-gate 			    NULL, &call_req);
18760Sstevel@tonic-gate 		}
18770Sstevel@tonic-gate 	}
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	if (do_auth && do_encr) {
18800Sstevel@tonic-gate 		/* dual operation */
18810Sstevel@tonic-gate 		/* initialize input data argument */
18820Sstevel@tonic-gate 		ESP_INIT_CRYPTO_DUAL_DATA(&ii->ipsec_in_crypto_dual_data,
18830Sstevel@tonic-gate 		    esp_mp, auth_offset, auth_len,
18840Sstevel@tonic-gate 		    encr_offset, encr_len - icv_len);
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 		/* specify IV */
18870Sstevel@tonic-gate 		ii->ipsec_in_crypto_dual_data.dd_miscdata =
18880Sstevel@tonic-gate 		    (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset;
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 		/* call the framework */
18910Sstevel@tonic-gate 		kef_rc = crypto_mac_verify_decrypt(&assoc->ipsa_amech,
18920Sstevel@tonic-gate 		    &assoc->ipsa_emech, &ii->ipsec_in_crypto_dual_data,
18930Sstevel@tonic-gate 		    &assoc->ipsa_kcfauthkey, &assoc->ipsa_kcfencrkey,
18940Sstevel@tonic-gate 		    auth_ctx_tmpl, encr_ctx_tmpl, &ii->ipsec_in_crypto_mac,
18950Sstevel@tonic-gate 		    NULL, &call_req);
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	switch (kef_rc) {
18990Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
19000Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_sync);
19010Sstevel@tonic-gate 		return (esp_in_done(ipsec_mp));
19020Sstevel@tonic-gate 	case CRYPTO_QUEUED:
19030Sstevel@tonic-gate 		/* esp_kcf_callback() will be invoked on completion */
19040Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_async);
19050Sstevel@tonic-gate 		return (IPSEC_STATUS_PENDING);
19060Sstevel@tonic-gate 	case CRYPTO_INVALID_MAC:
19070Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_sync);
19080Sstevel@tonic-gate 		esp_log_bad_auth(ipsec_mp);
19090Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
19100Sstevel@tonic-gate 	}
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc);
19130Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate static ipsec_status_t
19170Sstevel@tonic-gate esp_submit_req_outbound(mblk_t *ipsec_mp, ipsa_t *assoc, uchar_t *icv_buf,
19180Sstevel@tonic-gate     uint_t payload_len)
19190Sstevel@tonic-gate {
19200Sstevel@tonic-gate 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
19210Sstevel@tonic-gate 	uint_t auth_len;
19220Sstevel@tonic-gate 	crypto_call_req_t call_req;
19230Sstevel@tonic-gate 	mblk_t *esp_mp;
19240Sstevel@tonic-gate 	int kef_rc = CRYPTO_FAILED;
19250Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
19260Sstevel@tonic-gate 	crypto_ctx_template_t auth_ctx_tmpl;
19270Sstevel@tonic-gate 	boolean_t do_auth;
19280Sstevel@tonic-gate 	boolean_t do_encr;
19290Sstevel@tonic-gate 	uint_t iv_len = assoc->ipsa_iv_len;
19300Sstevel@tonic-gate 	crypto_ctx_template_t encr_ctx_tmpl;
19310Sstevel@tonic-gate 	boolean_t is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
19320Sstevel@tonic-gate 	size_t esph_offset = (is_natt ? UDPH_SIZE : 0);
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	esp3dbg(("esp_submit_req_outbound:%s", is_natt ? "natt" : "not natt"));
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
19390Sstevel@tonic-gate 	do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	/*
19420Sstevel@tonic-gate 	 * Outbound IPsec packets are of the form:
19430Sstevel@tonic-gate 	 * IPSEC_OUT -> [IP,options] -> [ESP,IV] -> [data] -> [pad,ICV]
19440Sstevel@tonic-gate 	 * unless it's NATT, then it's
19450Sstevel@tonic-gate 	 * IPSEC_OUT -> [IP,options] -> [udp][ESP,IV] -> [data] -> [pad,ICV]
19460Sstevel@tonic-gate 	 * Get a pointer to the mblk containing the ESP header.
19470Sstevel@tonic-gate 	 */
19480Sstevel@tonic-gate 	ASSERT(ipsec_mp->b_cont != NULL && ipsec_mp->b_cont->b_cont != NULL);
19490Sstevel@tonic-gate 	esp_mp = ipsec_mp->b_cont->b_cont;
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 	ESP_INIT_CALLREQ(&call_req);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	if (do_auth) {
19540Sstevel@tonic-gate 		/* force asynchronous processing? */
19550Sstevel@tonic-gate 		if (ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
19560Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
19570Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 		/* authentication context template */
19600Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
19610Sstevel@tonic-gate 		    auth_ctx_tmpl);
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 		/* where to store the computed mac */
19640Sstevel@tonic-gate 		ESP_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac,
19650Sstevel@tonic-gate 		    icv_len, icv_buf);
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 		/* authentication starts at the ESP header */
1968134Sdanmcd 		auth_len = payload_len + iv_len + sizeof (esph_t);
19690Sstevel@tonic-gate 		if (!do_encr) {
19700Sstevel@tonic-gate 			/* authentication only */
19710Sstevel@tonic-gate 			/* initialize input data argument */
19720Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data,
19730Sstevel@tonic-gate 			    esp_mp, esph_offset, auth_len);
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 			/* call the crypto framework */
19760Sstevel@tonic-gate 			kef_rc = crypto_mac(&assoc->ipsa_amech,
19770Sstevel@tonic-gate 			    &io->ipsec_out_crypto_data,
19780Sstevel@tonic-gate 			    &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
19790Sstevel@tonic-gate 			    &io->ipsec_out_crypto_mac, &call_req);
19800Sstevel@tonic-gate 		}
19810Sstevel@tonic-gate 	}
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	if (do_encr) {
19840Sstevel@tonic-gate 		/* force asynchronous processing? */
19850Sstevel@tonic-gate 		if (ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
19860Sstevel@tonic-gate 		    IPSEC_ALGS_EXEC_ASYNC)
19870Sstevel@tonic-gate 			call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE;
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 		/* encryption context template */
19900Sstevel@tonic-gate 		IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
19910Sstevel@tonic-gate 		    encr_ctx_tmpl);
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 		if (!do_auth) {
19940Sstevel@tonic-gate 			/* encryption only, skip mblk that contains ESP hdr */
19950Sstevel@tonic-gate 			/* initialize input data argument */
19960Sstevel@tonic-gate 			ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data,
19970Sstevel@tonic-gate 			    esp_mp->b_cont, 0, payload_len);
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 			/* specify IV */
20000Sstevel@tonic-gate 			io->ipsec_out_crypto_data.cd_miscdata =
20010Sstevel@tonic-gate 			    (char *)esp_mp->b_rptr + sizeof (esph_t) +
20020Sstevel@tonic-gate 			    esph_offset;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 			/* call the crypto framework */
20050Sstevel@tonic-gate 			kef_rc = crypto_encrypt(&assoc->ipsa_emech,
20060Sstevel@tonic-gate 			    &io->ipsec_out_crypto_data,
20070Sstevel@tonic-gate 			    &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
20080Sstevel@tonic-gate 			    NULL, &call_req);
20090Sstevel@tonic-gate 		}
20100Sstevel@tonic-gate 	}
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 	if (do_auth && do_encr) {
20130Sstevel@tonic-gate 		/*
20140Sstevel@tonic-gate 		 * Encryption and authentication:
20150Sstevel@tonic-gate 		 * Pass the pointer to the mblk chain starting at the ESP
20160Sstevel@tonic-gate 		 * header to the framework. Skip the ESP header mblk
20170Sstevel@tonic-gate 		 * for encryption, which is reflected by an encryption
20180Sstevel@tonic-gate 		 * offset equal to the length of that mblk. Start
20190Sstevel@tonic-gate 		 * the authentication at the ESP header, i.e. use an
20200Sstevel@tonic-gate 		 * authentication offset of zero.
20210Sstevel@tonic-gate 		 */
20220Sstevel@tonic-gate 		ESP_INIT_CRYPTO_DUAL_DATA(&io->ipsec_out_crypto_dual_data,
20230Sstevel@tonic-gate 		    esp_mp, MBLKL(esp_mp), payload_len, esph_offset, auth_len);
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 		/* specify IV */
20260Sstevel@tonic-gate 		io->ipsec_out_crypto_dual_data.dd_miscdata =
20270Sstevel@tonic-gate 		    (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset;
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 		/* call the framework */
20300Sstevel@tonic-gate 		kef_rc = crypto_encrypt_mac(&assoc->ipsa_emech,
20310Sstevel@tonic-gate 		    &assoc->ipsa_amech, NULL,
20320Sstevel@tonic-gate 		    &assoc->ipsa_kcfencrkey, &assoc->ipsa_kcfauthkey,
20330Sstevel@tonic-gate 		    encr_ctx_tmpl, auth_ctx_tmpl,
20340Sstevel@tonic-gate 		    &io->ipsec_out_crypto_dual_data,
20350Sstevel@tonic-gate 		    &io->ipsec_out_crypto_mac, &call_req);
20360Sstevel@tonic-gate 	}
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	switch (kef_rc) {
20390Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
20400Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_sync);
20410Sstevel@tonic-gate 		return (IPSEC_STATUS_SUCCESS);
20420Sstevel@tonic-gate 	case CRYPTO_QUEUED:
20430Sstevel@tonic-gate 		/* esp_kcf_callback() will be invoked on completion */
20440Sstevel@tonic-gate 		ESP_BUMP_STAT(crypto_async);
20450Sstevel@tonic-gate 		return (IPSEC_STATUS_PENDING);
20460Sstevel@tonic-gate 	}
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc);
20490Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
20500Sstevel@tonic-gate }
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate /*
20530Sstevel@tonic-gate  * Handle outbound IPsec processing for IPv4 and IPv6
20540Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
20550Sstevel@tonic-gate  * mblk chain ipsec_in_mp.
20560Sstevel@tonic-gate  */
20570Sstevel@tonic-gate static ipsec_status_t
20580Sstevel@tonic-gate esp_outbound(mblk_t *mp)
20590Sstevel@tonic-gate {
20600Sstevel@tonic-gate 	mblk_t *ipsec_out_mp, *data_mp, *espmp, *tailmp;
20610Sstevel@tonic-gate 	ipsec_out_t *io;
20620Sstevel@tonic-gate 	ipha_t *ipha;
20630Sstevel@tonic-gate 	ip6_t *ip6h;
20640Sstevel@tonic-gate 	esph_t *esph;
20650Sstevel@tonic-gate 	uint_t af;
20660Sstevel@tonic-gate 	uint8_t *nhp;
20670Sstevel@tonic-gate 	uintptr_t divpoint, datalen, adj, padlen, i, alloclen;
20680Sstevel@tonic-gate 	uintptr_t esplen = sizeof (esph_t);
20690Sstevel@tonic-gate 	uint8_t protocol;
20700Sstevel@tonic-gate 	ipsa_t *assoc;
2071*3192Sdanmcd 	uint_t iv_len, mac_len = 0;
20720Sstevel@tonic-gate 	uchar_t *icv_buf;
20730Sstevel@tonic-gate 	udpha_t *udpha;
20740Sstevel@tonic-gate 	boolean_t is_natt = B_FALSE;
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate 	ESP_BUMP_STAT(out_requests);
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	ipsec_out_mp = mp;
20790Sstevel@tonic-gate 	data_mp = ipsec_out_mp->b_cont;
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	/*
20820Sstevel@tonic-gate 	 * <sigh> We have to copy the message here, because TCP (for example)
20830Sstevel@tonic-gate 	 * keeps a dupb() of the message lying around for retransmission.
20840Sstevel@tonic-gate 	 * Since ESP changes the whole of the datagram, we have to create our
20850Sstevel@tonic-gate 	 * own copy lest we clobber TCP's data.  Since we have to copy anyway,
20860Sstevel@tonic-gate 	 * we might as well make use of msgpullup() and get the mblk into one
20870Sstevel@tonic-gate 	 * contiguous piece!
20880Sstevel@tonic-gate 	 */
20890Sstevel@tonic-gate 	ipsec_out_mp->b_cont = msgpullup(data_mp, -1);
20900Sstevel@tonic-gate 	if (ipsec_out_mp->b_cont == NULL) {
20910Sstevel@tonic-gate 		esp0dbg(("esp_outbound: msgpullup() failed, "
20920Sstevel@tonic-gate 		    "dropping packet.\n"));
20930Sstevel@tonic-gate 		ipsec_out_mp->b_cont = data_mp;
20940Sstevel@tonic-gate 		/*
20950Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
20960Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
20970Sstevel@tonic-gate 		 */
20980Sstevel@tonic-gate 		ip_drop_packet(ipsec_out_mp, B_FALSE, NULL, NULL,
20990Sstevel@tonic-gate 		    &ipdrops_esp_nomem, &esp_dropper);
21000Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
21010Sstevel@tonic-gate 	} else {
21020Sstevel@tonic-gate 		freemsg(data_mp);
21030Sstevel@tonic-gate 		data_mp = ipsec_out_mp->b_cont;
21040Sstevel@tonic-gate 	}
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	io = (ipsec_out_t *)ipsec_out_mp->b_rptr;
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	/*
21090Sstevel@tonic-gate 	 * Reality check....
21100Sstevel@tonic-gate 	 */
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	ipha = (ipha_t *)data_mp->b_rptr;  /* So we can call esp_acquire(). */
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 	if (io->ipsec_out_v4) {
21150Sstevel@tonic-gate 		af = AF_INET;
21160Sstevel@tonic-gate 		divpoint = IPH_HDR_LENGTH(ipha);
21170Sstevel@tonic-gate 		datalen = ntohs(ipha->ipha_length) - divpoint;
21180Sstevel@tonic-gate 		nhp = (uint8_t *)&ipha->ipha_protocol;
21190Sstevel@tonic-gate 	} else {
21200Sstevel@tonic-gate 		ip6_pkt_t ipp;
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 		af = AF_INET6;
21230Sstevel@tonic-gate 		ip6h = (ip6_t *)ipha;
21240Sstevel@tonic-gate 		bzero(&ipp, sizeof (ipp));
21250Sstevel@tonic-gate 		divpoint = ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL);
21260Sstevel@tonic-gate 		if (ipp.ipp_dstopts != NULL &&
21270Sstevel@tonic-gate 		    ipp.ipp_dstopts->ip6d_nxt != IPPROTO_ROUTING) {
21280Sstevel@tonic-gate 			/*
21290Sstevel@tonic-gate 			 * Destination options are tricky.  If we get in here,
21300Sstevel@tonic-gate 			 * then we have a terminal header following the
21310Sstevel@tonic-gate 			 * destination options.  We need to adjust backwards
21320Sstevel@tonic-gate 			 * so we insert ESP BEFORE the destination options
21330Sstevel@tonic-gate 			 * bag.  (So that the dstopts get encrypted!)
21340Sstevel@tonic-gate 			 *
21350Sstevel@tonic-gate 			 * Since this is for outbound packets only, we know
21360Sstevel@tonic-gate 			 * that non-terminal destination options only precede
21370Sstevel@tonic-gate 			 * routing headers.
21380Sstevel@tonic-gate 			 */
21390Sstevel@tonic-gate 			divpoint -= ipp.ipp_dstoptslen;
21400Sstevel@tonic-gate 		}
21410Sstevel@tonic-gate 		datalen = ntohs(ip6h->ip6_plen) + sizeof (ip6_t) - divpoint;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 		if (ipp.ipp_rthdr != NULL) {
21440Sstevel@tonic-gate 			nhp = &ipp.ipp_rthdr->ip6r_nxt;
21450Sstevel@tonic-gate 		} else if (ipp.ipp_hopopts != NULL) {
21460Sstevel@tonic-gate 			nhp = &ipp.ipp_hopopts->ip6h_nxt;
21470Sstevel@tonic-gate 		} else {
21480Sstevel@tonic-gate 			ASSERT(divpoint == sizeof (ip6_t));
21490Sstevel@tonic-gate 			/* It's probably IP + ESP. */
21500Sstevel@tonic-gate 			nhp = &ip6h->ip6_nxt;
21510Sstevel@tonic-gate 		}
21520Sstevel@tonic-gate 	}
21530Sstevel@tonic-gate 	assoc = io->ipsec_out_esp_sa;
21540Sstevel@tonic-gate 	ASSERT(assoc != NULL);
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	if (assoc->ipsa_usetime == 0)
21570Sstevel@tonic-gate 		esp_set_usetime(assoc, B_FALSE);
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != SADB_AALG_NONE)
21600Sstevel@tonic-gate 		mac_len = assoc->ipsa_mac_len;
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 	if (assoc->ipsa_flags & IPSA_F_NATT) {
21630Sstevel@tonic-gate 		/* wedge in fake UDP */
21640Sstevel@tonic-gate 		is_natt = B_TRUE;
21650Sstevel@tonic-gate 		esplen += UDPH_SIZE;
21660Sstevel@tonic-gate 	}
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 	/*
21690Sstevel@tonic-gate 	 * Set up ESP header and encryption padding for ENCR PI request.
21700Sstevel@tonic-gate 	 */
21710Sstevel@tonic-gate 
2172*3192Sdanmcd 	/* Determine the padding length.  Pad to 4-bytes for no-encryption. */
21730Sstevel@tonic-gate 	if (assoc->ipsa_encr_alg != SADB_EALG_NULL) {
2174*3192Sdanmcd 		iv_len = assoc->ipsa_iv_len;
2175*3192Sdanmcd 
2176*3192Sdanmcd 		/*
2177*3192Sdanmcd 		 * Include the two additional bytes (hence the - 2) for the
2178*3192Sdanmcd 		 * padding length and the next header.  Take this into account
2179*3192Sdanmcd 		 * when calculating the actual length of the padding.
2180*3192Sdanmcd 		 */
2181*3192Sdanmcd 		ASSERT(ISP2(iv_len));
2182*3192Sdanmcd 		padlen = ((unsigned)(iv_len - datalen - 2)) & (iv_len - 1);
21830Sstevel@tonic-gate 	} else {
2184*3192Sdanmcd 		iv_len = 0;
2185*3192Sdanmcd 		padlen = ((unsigned)(sizeof (uint32_t) - datalen - 2)) &
2186*3192Sdanmcd 		    (sizeof (uint32_t) - 1);
21870Sstevel@tonic-gate 	}
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 	/* Allocate ESP header and IV. */
21900Sstevel@tonic-gate 	esplen += iv_len;
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	/*
21930Sstevel@tonic-gate 	 * Update association byte-count lifetimes.  Don't forget to take
21940Sstevel@tonic-gate 	 * into account the padding length and next-header (hence the + 2).
2195134Sdanmcd 	 *
21960Sstevel@tonic-gate 	 * Use the amount of data fed into the "encryption algorithm".  This
21970Sstevel@tonic-gate 	 * is the IV, the data length, the padding length, and the final two
21980Sstevel@tonic-gate 	 * bytes (padlen, and next-header).
21990Sstevel@tonic-gate 	 *
22000Sstevel@tonic-gate 	 */
22010Sstevel@tonic-gate 
2202134Sdanmcd 	if (!esp_age_bytes(assoc, datalen + padlen + iv_len + 2, B_FALSE)) {
22030Sstevel@tonic-gate 		/*
22040Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
22050Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
22060Sstevel@tonic-gate 		 */
22070Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
22080Sstevel@tonic-gate 		    &ipdrops_esp_bytes_expire, &esp_dropper);
22090Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
22100Sstevel@tonic-gate 	}
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	espmp = allocb(esplen, BPRI_HI);
22130Sstevel@tonic-gate 	if (espmp == NULL) {
22140Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
22150Sstevel@tonic-gate 		esp1dbg(("esp_outbound: can't allocate espmp.\n"));
22160Sstevel@tonic-gate 		/*
22170Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
22180Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
22190Sstevel@tonic-gate 		 */
22200Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_nomem,
22210Sstevel@tonic-gate 		    &esp_dropper);
22220Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
22230Sstevel@tonic-gate 	}
22240Sstevel@tonic-gate 	espmp->b_wptr += esplen;
22250Sstevel@tonic-gate 	esph = (esph_t *)espmp->b_rptr;
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	if (is_natt) {
22280Sstevel@tonic-gate 		esp3dbg(("esp_outbound: NATT"));
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 		udpha = (udpha_t *)espmp->b_rptr;
22310Sstevel@tonic-gate 		udpha->uha_src_port = htons(IPPORT_IKE_NATT);
22320Sstevel@tonic-gate 		if (assoc->ipsa_remote_port != 0)
22330Sstevel@tonic-gate 			udpha->uha_dst_port = assoc->ipsa_remote_port;
22340Sstevel@tonic-gate 		else
22350Sstevel@tonic-gate 			udpha->uha_dst_port = htons(IPPORT_IKE_NATT);
22360Sstevel@tonic-gate 		/*
22370Sstevel@tonic-gate 		 * Set the checksum to 0, so that the ip_wput_ipsec_out()
22380Sstevel@tonic-gate 		 * can do the right thing.
22390Sstevel@tonic-gate 		 */
22400Sstevel@tonic-gate 		udpha->uha_checksum = 0;
22410Sstevel@tonic-gate 		esph = (esph_t *)(udpha + 1);
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	esph->esph_spi = assoc->ipsa_spi;
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 	esph->esph_replay = htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
22470Sstevel@tonic-gate 	if (esph->esph_replay == 0 && assoc->ipsa_replay_wsize != 0) {
22480Sstevel@tonic-gate 		/*
22490Sstevel@tonic-gate 		 * XXX We have replay counter wrapping.
22500Sstevel@tonic-gate 		 * We probably want to nuke this SA (and its peer).
22510Sstevel@tonic-gate 		 */
22520Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
22530Sstevel@tonic-gate 		    SL_ERROR | SL_CONSOLE | SL_WARN,
22540Sstevel@tonic-gate 		    "Outbound ESP SA (0x%x, %s) has wrapped sequence.\n",
22550Sstevel@tonic-gate 		    esph->esph_spi, assoc->ipsa_dstaddr, af);
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
22580Sstevel@tonic-gate 		sadb_replay_delete(assoc);
22590Sstevel@tonic-gate 		/*
22600Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
22610Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
22620Sstevel@tonic-gate 		 */
22630Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_replay,
22640Sstevel@tonic-gate 		    &esp_dropper);
22650Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
22660Sstevel@tonic-gate 	}
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	/*
22690Sstevel@tonic-gate 	 * Set the IV to a random quantity.  We do not require the
22700Sstevel@tonic-gate 	 * highest quality random bits, but for best security with CBC
22710Sstevel@tonic-gate 	 * mode ciphers, the value must be unlikely to repeat and also
22720Sstevel@tonic-gate 	 * must not be known in advance to an adversary capable of
22730Sstevel@tonic-gate 	 * influencing the plaintext.
22740Sstevel@tonic-gate 	 */
22750Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)(esph + 1), iv_len);
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	/* Fix the IP header. */
22780Sstevel@tonic-gate 	alloclen = padlen + 2 + mac_len;
22790Sstevel@tonic-gate 	adj = alloclen + (espmp->b_wptr - espmp->b_rptr);
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	protocol = *nhp;
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	if (io->ipsec_out_v4) {
22840Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) + adj);
22850Sstevel@tonic-gate 		if (is_natt) {
22860Sstevel@tonic-gate 			*nhp = IPPROTO_UDP;
22870Sstevel@tonic-gate 			udpha->uha_length = htons(ntohs(ipha->ipha_length) -
22880Sstevel@tonic-gate 			    IPH_HDR_LENGTH(ipha));
22890Sstevel@tonic-gate 		} else {
22900Sstevel@tonic-gate 			*nhp = IPPROTO_ESP;
22910Sstevel@tonic-gate 		}
22920Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = 0;
22930Sstevel@tonic-gate 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
22940Sstevel@tonic-gate 	} else {
22950Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) + adj);
22960Sstevel@tonic-gate 		*nhp = IPPROTO_ESP;
22970Sstevel@tonic-gate 	}
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 	/* I've got the two ESP mblks, now insert them. */
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	esp2dbg(("data_mp before outbound ESP adjustment:\n"));
23020Sstevel@tonic-gate 	esp2dbg((dump_msg(data_mp)));
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	if (!esp_insert_esp(data_mp, espmp, divpoint)) {
23050Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
23060Sstevel@tonic-gate 		/* NOTE:  esp_insert_esp() only fails if there's no memory. */
23070Sstevel@tonic-gate 		/*
23080Sstevel@tonic-gate 		 * TODO:  Find the outbound IRE for this packet and
23090Sstevel@tonic-gate 		 * pass it to ip_drop_packet().
23100Sstevel@tonic-gate 		 */
23110Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_nomem,
23120Sstevel@tonic-gate 		    &esp_dropper);
23130Sstevel@tonic-gate 		freeb(espmp);
23140Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
23150Sstevel@tonic-gate 	}
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 	/* Append padding (and leave room for ICV). */
23180Sstevel@tonic-gate 	for (tailmp = data_mp; tailmp->b_cont != NULL; tailmp = tailmp->b_cont)
23190Sstevel@tonic-gate 		;
23200Sstevel@tonic-gate 	if (tailmp->b_wptr + alloclen > tailmp->b_datap->db_lim) {
23210Sstevel@tonic-gate 		tailmp->b_cont = allocb(alloclen, BPRI_HI);
23220Sstevel@tonic-gate 		if (tailmp->b_cont == NULL) {
23230Sstevel@tonic-gate 			ESP_BUMP_STAT(out_discards);
23240Sstevel@tonic-gate 			esp0dbg(("esp_outbound:  Can't allocate tailmp.\n"));
23250Sstevel@tonic-gate 			/*
23260Sstevel@tonic-gate 			 * TODO:  Find the outbound IRE for this packet and
23270Sstevel@tonic-gate 			 * pass it to ip_drop_packet().
23280Sstevel@tonic-gate 			 */
23290Sstevel@tonic-gate 			ip_drop_packet(mp, B_FALSE, NULL, NULL,
23300Sstevel@tonic-gate 			    &ipdrops_esp_nomem, &esp_dropper);
23310Sstevel@tonic-gate 			return (IPSEC_STATUS_FAILED);
23320Sstevel@tonic-gate 		}
23330Sstevel@tonic-gate 		tailmp = tailmp->b_cont;
23340Sstevel@tonic-gate 	}
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	/*
23370Sstevel@tonic-gate 	 * If there's padding, N bytes of padding must be of the form 0x1,
23380Sstevel@tonic-gate 	 * 0x2, 0x3... 0xN.
23390Sstevel@tonic-gate 	 */
23400Sstevel@tonic-gate 	for (i = 0; i < padlen; ) {
23410Sstevel@tonic-gate 		i++;
23420Sstevel@tonic-gate 		*tailmp->b_wptr++ = i;
23430Sstevel@tonic-gate 	}
23440Sstevel@tonic-gate 	*tailmp->b_wptr++ = i;
23450Sstevel@tonic-gate 	*tailmp->b_wptr++ = protocol;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	esp2dbg(("data_Mp before encryption:\n"));
23480Sstevel@tonic-gate 	esp2dbg((dump_msg(data_mp)));
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	/*
23510Sstevel@tonic-gate 	 * The packet is eligible for hardware acceleration if the
23520Sstevel@tonic-gate 	 * following conditions are satisfied:
23530Sstevel@tonic-gate 	 *
23540Sstevel@tonic-gate 	 * 1. the packet will not be fragmented
23550Sstevel@tonic-gate 	 * 2. the provider supports the algorithms specified by SA
23560Sstevel@tonic-gate 	 * 3. there is no pending control message being exchanged
23570Sstevel@tonic-gate 	 * 4. snoop is not attached
23580Sstevel@tonic-gate 	 * 5. the destination address is not a multicast address
23590Sstevel@tonic-gate 	 *
23600Sstevel@tonic-gate 	 * All five of these conditions are checked by IP prior to
23610Sstevel@tonic-gate 	 * sending the packet to ESP.
23620Sstevel@tonic-gate 	 *
23630Sstevel@tonic-gate 	 * But We, and We Alone, can, nay MUST check if the packet
23640Sstevel@tonic-gate 	 * is over NATT, and then disqualify it from hardware
23650Sstevel@tonic-gate 	 * acceleration.
23660Sstevel@tonic-gate 	 */
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 	if (io->ipsec_out_is_capab_ill && !(assoc->ipsa_flags & IPSA_F_NATT)) {
23690Sstevel@tonic-gate 		return (esp_outbound_accelerated(ipsec_out_mp, mac_len));
23700Sstevel@tonic-gate 	}
23710Sstevel@tonic-gate 	ESP_BUMP_STAT(noaccel);
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	/*
23740Sstevel@tonic-gate 	 * Okay.  I've set up the pre-encryption ESP.  Let's do it!
23750Sstevel@tonic-gate 	 */
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	if (mac_len > 0) {
23780Sstevel@tonic-gate 		ASSERT(tailmp->b_wptr + mac_len <= tailmp->b_datap->db_lim);
23790Sstevel@tonic-gate 		icv_buf = tailmp->b_wptr;
23800Sstevel@tonic-gate 		tailmp->b_wptr += mac_len;
23810Sstevel@tonic-gate 	} else {
23820Sstevel@tonic-gate 		icv_buf = NULL;
23830Sstevel@tonic-gate 	}
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	return (esp_submit_req_outbound(ipsec_out_mp, assoc, icv_buf,
23860Sstevel@tonic-gate 	    datalen + padlen + 2));
23870Sstevel@tonic-gate }
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate /*
23900Sstevel@tonic-gate  * IP calls this to validate the ICMP errors that
23910Sstevel@tonic-gate  * we got from the network.
23920Sstevel@tonic-gate  */
23930Sstevel@tonic-gate ipsec_status_t
23940Sstevel@tonic-gate ipsecesp_icmp_error(mblk_t *ipsec_mp)
23950Sstevel@tonic-gate {
23960Sstevel@tonic-gate 	/*
23970Sstevel@tonic-gate 	 * Unless we get an entire packet back, this function is useless.
23980Sstevel@tonic-gate 	 * Why?
23990Sstevel@tonic-gate 	 *
24000Sstevel@tonic-gate 	 * 1.)	Partial packets are useless, because the "next header"
24010Sstevel@tonic-gate 	 *	is at the end of the decrypted ESP packet.  Without the
24020Sstevel@tonic-gate 	 *	whole packet, this is useless.
24030Sstevel@tonic-gate 	 *
24040Sstevel@tonic-gate 	 * 2.)	If we every use a stateful cipher, such as a stream or a
24050Sstevel@tonic-gate 	 *	one-time pad, we can't do anything.
24060Sstevel@tonic-gate 	 *
24070Sstevel@tonic-gate 	 * Since the chances of us getting an entire packet back are very
24080Sstevel@tonic-gate 	 * very small, we discard here.
24090Sstevel@tonic-gate 	 */
24100Sstevel@tonic-gate 	IP_ESP_BUMP_STAT(in_discards);
24110Sstevel@tonic-gate 	ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_esp_icmp,
24120Sstevel@tonic-gate 	    &esp_dropper);
24130Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
24140Sstevel@tonic-gate }
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate /*
24170Sstevel@tonic-gate  * ESP module read put routine.
24180Sstevel@tonic-gate  */
24190Sstevel@tonic-gate /* ARGSUSED */
24200Sstevel@tonic-gate static void
24210Sstevel@tonic-gate ipsecesp_rput(queue_t *q, mblk_t *mp)
24220Sstevel@tonic-gate {
24233055Sdanmcd 	ASSERT(mp->b_datap->db_type != M_CTL);	/* No more IRE_DB_REQ. */
24240Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
24250Sstevel@tonic-gate 	case M_PROTO:
24260Sstevel@tonic-gate 	case M_PCPROTO:
24270Sstevel@tonic-gate 		/* TPI message of some sort. */
24280Sstevel@tonic-gate 		switch (*((t_scalar_t *)mp->b_rptr)) {
24290Sstevel@tonic-gate 		case T_BIND_ACK:
24300Sstevel@tonic-gate 			esp3dbg(("Thank you IP from ESP for T_BIND_ACK\n"));
24310Sstevel@tonic-gate 			break;
24320Sstevel@tonic-gate 		case T_ERROR_ACK:
24330Sstevel@tonic-gate 			cmn_err(CE_WARN,
24340Sstevel@tonic-gate 			    "ipsecesp:  ESP received T_ERROR_ACK from IP.");
24350Sstevel@tonic-gate 			/*
24360Sstevel@tonic-gate 			 * Make esp_sadb.s_ip_q NULL, and in the
24370Sstevel@tonic-gate 			 * future, perhaps try again.
24380Sstevel@tonic-gate 			 */
24390Sstevel@tonic-gate 			esp_sadb.s_ip_q = NULL;
24400Sstevel@tonic-gate 			break;
24410Sstevel@tonic-gate 		case T_OK_ACK:
24420Sstevel@tonic-gate 			/* Probably from a (rarely sent) T_UNBIND_REQ. */
24430Sstevel@tonic-gate 			break;
24440Sstevel@tonic-gate 		default:
24450Sstevel@tonic-gate 			esp0dbg(("Unknown M_{,PC}PROTO message.\n"));
24460Sstevel@tonic-gate 		}
24470Sstevel@tonic-gate 		freemsg(mp);
24480Sstevel@tonic-gate 		break;
24490Sstevel@tonic-gate 	default:
24500Sstevel@tonic-gate 		/* For now, passthru message. */
24510Sstevel@tonic-gate 		esp2dbg(("ESP got unknown mblk type %d.\n",
24520Sstevel@tonic-gate 		    mp->b_datap->db_type));
24530Sstevel@tonic-gate 		putnext(q, mp);
24540Sstevel@tonic-gate 	}
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate /*
24580Sstevel@tonic-gate  * Construct an SADB_REGISTER message with the current algorithms.
24590Sstevel@tonic-gate  */
24600Sstevel@tonic-gate static boolean_t
24610Sstevel@tonic-gate esp_register_out(uint32_t sequence, uint32_t pid, uint_t serial)
24620Sstevel@tonic-gate {
24630Sstevel@tonic-gate 	mblk_t *pfkey_msg_mp, *keysock_out_mp;
24640Sstevel@tonic-gate 	sadb_msg_t *samsg;
24650Sstevel@tonic-gate 	sadb_supported_t *sasupp_auth = NULL;
24660Sstevel@tonic-gate 	sadb_supported_t *sasupp_encr = NULL;
24670Sstevel@tonic-gate 	sadb_alg_t *saalg;
24680Sstevel@tonic-gate 	uint_t allocsize = sizeof (*samsg);
24690Sstevel@tonic-gate 	uint_t i, numalgs_snap;
24700Sstevel@tonic-gate 	int current_aalgs;
24710Sstevel@tonic-gate 	ipsec_alginfo_t **authalgs;
24720Sstevel@tonic-gate 	uint_t num_aalgs;
24730Sstevel@tonic-gate 	int current_ealgs;
24740Sstevel@tonic-gate 	ipsec_alginfo_t **encralgs;
24750Sstevel@tonic-gate 	uint_t num_ealgs;
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 	/* Allocate the KEYSOCK_OUT. */
24780Sstevel@tonic-gate 	keysock_out_mp = sadb_keysock_out(serial);
24790Sstevel@tonic-gate 	if (keysock_out_mp == NULL) {
24800Sstevel@tonic-gate 		esp0dbg(("esp_register_out: couldn't allocate mblk.\n"));
24810Sstevel@tonic-gate 		return (B_FALSE);
24820Sstevel@tonic-gate 	}
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 	/*
24850Sstevel@tonic-gate 	 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
24860Sstevel@tonic-gate 	 */
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 	mutex_enter(&alg_lock);
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	/*
24910Sstevel@tonic-gate 	 * Fill SADB_REGISTER message's algorithm descriptors.  Hold
24920Sstevel@tonic-gate 	 * down the lock while filling it.
24930Sstevel@tonic-gate 	 *
24940Sstevel@tonic-gate 	 * Return only valid algorithms, so the number of algorithms
24950Sstevel@tonic-gate 	 * to send up may be less than the number of algorithm entries
24960Sstevel@tonic-gate 	 * in the table.
24970Sstevel@tonic-gate 	 */
24980Sstevel@tonic-gate 	authalgs = ipsec_alglists[IPSEC_ALG_AUTH];
24990Sstevel@tonic-gate 	for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
25000Sstevel@tonic-gate 		if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
25010Sstevel@tonic-gate 			num_aalgs++;
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	if (num_aalgs != 0) {
25040Sstevel@tonic-gate 		allocsize += (num_aalgs * sizeof (*saalg));
25050Sstevel@tonic-gate 		allocsize += sizeof (*sasupp_auth);
25060Sstevel@tonic-gate 	}
25070Sstevel@tonic-gate 	encralgs = ipsec_alglists[IPSEC_ALG_ENCR];
25080Sstevel@tonic-gate 	for (num_ealgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
25090Sstevel@tonic-gate 		if (encralgs[i] != NULL && ALG_VALID(encralgs[i]))
25100Sstevel@tonic-gate 			num_ealgs++;
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	if (num_ealgs != 0) {
25130Sstevel@tonic-gate 		allocsize += (num_ealgs * sizeof (*saalg));
25140Sstevel@tonic-gate 		allocsize += sizeof (*sasupp_encr);
25150Sstevel@tonic-gate 	}
25160Sstevel@tonic-gate 	keysock_out_mp->b_cont = allocb(allocsize, BPRI_HI);
25170Sstevel@tonic-gate 	if (keysock_out_mp->b_cont == NULL) {
25180Sstevel@tonic-gate 		mutex_exit(&alg_lock);
25190Sstevel@tonic-gate 		freemsg(keysock_out_mp);
25200Sstevel@tonic-gate 		return (B_FALSE);
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	pfkey_msg_mp = keysock_out_mp->b_cont;
25240Sstevel@tonic-gate 	pfkey_msg_mp->b_wptr += allocsize;
25250Sstevel@tonic-gate 	if (num_aalgs != 0) {
25260Sstevel@tonic-gate 		sasupp_auth = (sadb_supported_t *)
25270Sstevel@tonic-gate 		    (pfkey_msg_mp->b_rptr + sizeof (*samsg));
25280Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(sasupp_auth + 1);
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 		ASSERT(((ulong_t)saalg & 0x7) == 0);
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 		numalgs_snap = 0;
25330Sstevel@tonic-gate 		for (i = 0;
25340Sstevel@tonic-gate 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs)); i++) {
25350Sstevel@tonic-gate 			if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
25360Sstevel@tonic-gate 				continue;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 			saalg->sadb_alg_id = authalgs[i]->alg_id;
25390Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = 0;
25400Sstevel@tonic-gate 			saalg->sadb_alg_minbits	= authalgs[i]->alg_ef_minbits;
25410Sstevel@tonic-gate 			saalg->sadb_alg_maxbits	= authalgs[i]->alg_ef_maxbits;
25420Sstevel@tonic-gate 			saalg->sadb_x_alg_defincr = authalgs[i]->alg_ef_default;
25430Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
25440Sstevel@tonic-gate 			    authalgs[i]->alg_increment;
25450Sstevel@tonic-gate 			numalgs_snap++;
25460Sstevel@tonic-gate 			saalg++;
25470Sstevel@tonic-gate 		}
25480Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_aalgs);
25490Sstevel@tonic-gate #ifdef DEBUG
25500Sstevel@tonic-gate 		/*
25510Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
25520Sstevel@tonic-gate 		 * algorithms.
25530Sstevel@tonic-gate 		 */
25540Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++) {
25550Sstevel@tonic-gate 			if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) {
25560Sstevel@tonic-gate 				cmn_err(CE_PANIC, "esp_register_out()! "
25570Sstevel@tonic-gate 				    "Missed aalg #%d.\n", i);
25580Sstevel@tonic-gate 			}
25590Sstevel@tonic-gate 		}
25600Sstevel@tonic-gate #endif /* DEBUG */
25610Sstevel@tonic-gate 	} else {
25620Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(pfkey_msg_mp->b_rptr + sizeof (*samsg));
25630Sstevel@tonic-gate 	}
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	if (num_ealgs != 0) {
25660Sstevel@tonic-gate 		sasupp_encr = (sadb_supported_t *)saalg;
25670Sstevel@tonic-gate 		saalg = (sadb_alg_t *)(sasupp_encr + 1);
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 		numalgs_snap = 0;
25700Sstevel@tonic-gate 		for (i = 0;
25710Sstevel@tonic-gate 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_ealgs)); i++) {
25720Sstevel@tonic-gate 			if (encralgs[i] == NULL || !ALG_VALID(encralgs[i]))
25730Sstevel@tonic-gate 				continue;
25740Sstevel@tonic-gate 			saalg->sadb_alg_id = encralgs[i]->alg_id;
25750Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = encralgs[i]->alg_datalen;
25760Sstevel@tonic-gate 			saalg->sadb_alg_minbits	= encralgs[i]->alg_ef_minbits;
25770Sstevel@tonic-gate 			saalg->sadb_alg_maxbits	= encralgs[i]->alg_ef_maxbits;
25780Sstevel@tonic-gate 			saalg->sadb_x_alg_defincr = encralgs[i]->alg_ef_default;
25790Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
25800Sstevel@tonic-gate 			    encralgs[i]->alg_increment;
25810Sstevel@tonic-gate 			numalgs_snap++;
25820Sstevel@tonic-gate 			saalg++;
25830Sstevel@tonic-gate 		}
25840Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_ealgs);
25850Sstevel@tonic-gate #ifdef DEBUG
25860Sstevel@tonic-gate 		/*
25870Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
25880Sstevel@tonic-gate 		 * algorithms.
25890Sstevel@tonic-gate 		 */
25900Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++) {
25910Sstevel@tonic-gate 			if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) {
25920Sstevel@tonic-gate 				cmn_err(CE_PANIC, "esp_register_out()! "
25930Sstevel@tonic-gate 				    "Missed ealg #%d.\n", i);
25940Sstevel@tonic-gate 			}
25950Sstevel@tonic-gate 		}
25960Sstevel@tonic-gate #endif /* DEBUG */
25970Sstevel@tonic-gate 	}
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	current_aalgs = num_aalgs;
26000Sstevel@tonic-gate 	current_ealgs = num_ealgs;
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	mutex_exit(&alg_lock);
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	/* Now fill the rest of the SADB_REGISTER message. */
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	samsg = (sadb_msg_t *)pfkey_msg_mp->b_rptr;
26070Sstevel@tonic-gate 	samsg->sadb_msg_version = PF_KEY_V2;
26080Sstevel@tonic-gate 	samsg->sadb_msg_type = SADB_REGISTER;
26090Sstevel@tonic-gate 	samsg->sadb_msg_errno = 0;
26100Sstevel@tonic-gate 	samsg->sadb_msg_satype = SADB_SATYPE_ESP;
26110Sstevel@tonic-gate 	samsg->sadb_msg_len = SADB_8TO64(allocsize);
26120Sstevel@tonic-gate 	samsg->sadb_msg_reserved = 0;
26130Sstevel@tonic-gate 	/*
26140Sstevel@tonic-gate 	 * Assume caller has sufficient sequence/pid number info.  If it's one
26150Sstevel@tonic-gate 	 * from me over a new alg., I could give two hoots about sequence.
26160Sstevel@tonic-gate 	 */
26170Sstevel@tonic-gate 	samsg->sadb_msg_seq = sequence;
26180Sstevel@tonic-gate 	samsg->sadb_msg_pid = pid;
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 	if (sasupp_auth != NULL) {
26210Sstevel@tonic-gate 		sasupp_auth->sadb_supported_len =
26220Sstevel@tonic-gate 		    SADB_8TO64(sizeof (*sasupp_auth) +
26230Sstevel@tonic-gate 			sizeof (*saalg) * current_aalgs);
26240Sstevel@tonic-gate 		sasupp_auth->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
26250Sstevel@tonic-gate 		sasupp_auth->sadb_supported_reserved = 0;
26260Sstevel@tonic-gate 	}
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 	if (sasupp_encr != NULL) {
26290Sstevel@tonic-gate 		sasupp_encr->sadb_supported_len =
26300Sstevel@tonic-gate 		    SADB_8TO64(sizeof (*sasupp_encr) +
26310Sstevel@tonic-gate 			sizeof (*saalg) * current_ealgs);
26320Sstevel@tonic-gate 		sasupp_encr->sadb_supported_exttype =
26330Sstevel@tonic-gate 		    SADB_EXT_SUPPORTED_ENCRYPT;
26340Sstevel@tonic-gate 		sasupp_encr->sadb_supported_reserved = 0;
26350Sstevel@tonic-gate 	}
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	if (esp_pfkey_q != NULL)
26380Sstevel@tonic-gate 		putnext(esp_pfkey_q, keysock_out_mp);
26390Sstevel@tonic-gate 	else {
26400Sstevel@tonic-gate 		freemsg(keysock_out_mp);
26410Sstevel@tonic-gate 		return (B_FALSE);
26420Sstevel@tonic-gate 	}
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	return (B_TRUE);
26450Sstevel@tonic-gate }
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate /*
26480Sstevel@tonic-gate  * Invoked when the algorithm table changes. Causes SADB_REGISTER
26490Sstevel@tonic-gate  * messages continaining the current list of algorithms to be
26500Sstevel@tonic-gate  * sent up to the ESP listeners.
26510Sstevel@tonic-gate  */
26520Sstevel@tonic-gate void
26530Sstevel@tonic-gate ipsecesp_algs_changed(void)
26540Sstevel@tonic-gate {
26550Sstevel@tonic-gate 	/*
26560Sstevel@tonic-gate 	 * Time to send a PF_KEY SADB_REGISTER message to ESP listeners
26570Sstevel@tonic-gate 	 * everywhere.  (The function itself checks for NULL esp_pfkey_q.)
26580Sstevel@tonic-gate 	 */
26590Sstevel@tonic-gate 	(void) esp_register_out(0, 0, 0);
26600Sstevel@tonic-gate }
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate /*
26630Sstevel@tonic-gate  * taskq_dispatch handler.
26640Sstevel@tonic-gate  */
26650Sstevel@tonic-gate static void
26660Sstevel@tonic-gate inbound_task(void *arg)
26670Sstevel@tonic-gate {
26680Sstevel@tonic-gate 	esph_t *esph;
26690Sstevel@tonic-gate 	mblk_t *mp = (mblk_t *)arg;
26700Sstevel@tonic-gate 	ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr;
26710Sstevel@tonic-gate 	int ipsec_rc;
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	esp2dbg(("in ESP inbound_task"));
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	esph = ipsec_inbound_esp_sa(mp);
26760Sstevel@tonic-gate 	if (esph == NULL)
26770Sstevel@tonic-gate 		return;
26780Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_esp_sa != NULL);
26790Sstevel@tonic-gate 	ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func(mp, esph);
26800Sstevel@tonic-gate 	if (ipsec_rc != IPSEC_STATUS_SUCCESS)
26810Sstevel@tonic-gate 		return;
26820Sstevel@tonic-gate 	ip_fanout_proto_again(mp, NULL, NULL, NULL);
26830Sstevel@tonic-gate }
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate /*
26860Sstevel@tonic-gate  * Now that weak-key passed, actually ADD the security association, and
26870Sstevel@tonic-gate  * send back a reply ADD message.
26880Sstevel@tonic-gate  */
26890Sstevel@tonic-gate static int
26903055Sdanmcd esp_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi,
26913055Sdanmcd     int *diagnostic)
26920Sstevel@tonic-gate {
2693691Ssommerfe 	isaf_t *primary, *secondary, *inbound, *outbound;
26940Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
26950Sstevel@tonic-gate 	sadb_address_t *dstext =
26960Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
26970Sstevel@tonic-gate 	struct sockaddr_in *dst;
26980Sstevel@tonic-gate 	struct sockaddr_in6 *dst6;
26990Sstevel@tonic-gate 	boolean_t is_ipv4, clone = B_FALSE, is_inbound = B_FALSE;
27000Sstevel@tonic-gate 	uint32_t *dstaddr;
27010Sstevel@tonic-gate 	ipsa_t *larval = NULL;
27020Sstevel@tonic-gate 	ipsacq_t *acqrec;
27030Sstevel@tonic-gate 	iacqf_t *acq_bucket;
27040Sstevel@tonic-gate 	mblk_t *acq_msgs = NULL;
27050Sstevel@tonic-gate 	int rc;
27060Sstevel@tonic-gate 	sadb_t *sp;
27070Sstevel@tonic-gate 	int outhash;
27080Sstevel@tonic-gate 	mblk_t *lpkt;
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 	/*
27110Sstevel@tonic-gate 	 * Locate the appropriate table(s).
27120Sstevel@tonic-gate 	 */
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
27150Sstevel@tonic-gate 	dst6 = (struct sockaddr_in6 *)dst;
27160Sstevel@tonic-gate 	is_ipv4 = (dst->sin_family == AF_INET);
27170Sstevel@tonic-gate 	if (is_ipv4) {
27180Sstevel@tonic-gate 		sp = &esp_sadb.s_v4;
27190Sstevel@tonic-gate 		dstaddr = (uint32_t *)(&dst->sin_addr);
2720564Ssommerfe 		outhash = OUTBOUND_HASH_V4(sp, *(ipaddr_t *)dstaddr);
27210Sstevel@tonic-gate 	} else {
27220Sstevel@tonic-gate 		sp = &esp_sadb.s_v6;
27230Sstevel@tonic-gate 		dstaddr = (uint32_t *)(&dst6->sin6_addr);
2724564Ssommerfe 		outhash = OUTBOUND_HASH_V6(sp, *(in6_addr_t *)dstaddr);
27250Sstevel@tonic-gate 	}
2726691Ssommerfe 
2727564Ssommerfe 	inbound = INBOUND_BUCKET(sp, assoc->sadb_sa_spi);
2728691Ssommerfe 	outbound = &sp->sdb_of[outhash];
2729691Ssommerfe 
27300Sstevel@tonic-gate 	switch (ksi->ks_in_dsttype) {
27310Sstevel@tonic-gate 	case KS_IN_ADDR_MBCAST:
27320Sstevel@tonic-gate 		clone = B_TRUE;	/* All mcast SAs can be bidirectional */
27330Sstevel@tonic-gate 		/* FALLTHRU */
27340Sstevel@tonic-gate 	case KS_IN_ADDR_ME:
27350Sstevel@tonic-gate 		primary = inbound;
2736691Ssommerfe 		secondary = outbound;
27370Sstevel@tonic-gate 		/*
27380Sstevel@tonic-gate 		 * If the source address is either one of mine, or unspecified
27390Sstevel@tonic-gate 		 * (which is best summed up by saying "not 'not mine'"),
27400Sstevel@tonic-gate 		 * then the association is potentially bi-directional,
27410Sstevel@tonic-gate 		 * in that it can be used for inbound traffic and outbound
27420Sstevel@tonic-gate 		 * traffic.  The best example of such an SA is a multicast
27430Sstevel@tonic-gate 		 * SA (which allows me to receive the outbound traffic).
27440Sstevel@tonic-gate 		 */
27450Sstevel@tonic-gate 		if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
27460Sstevel@tonic-gate 			clone = B_TRUE;
27470Sstevel@tonic-gate 		is_inbound = B_TRUE;
27480Sstevel@tonic-gate 		break;
27490Sstevel@tonic-gate 	case KS_IN_ADDR_NOTME:
2750691Ssommerfe 		primary = outbound;
27510Sstevel@tonic-gate 		secondary = inbound;
27520Sstevel@tonic-gate 		/*
27530Sstevel@tonic-gate 		 * If the source address literally not mine (either
27540Sstevel@tonic-gate 		 * unspecified or not mine), then this SA may have an
27550Sstevel@tonic-gate 		 * address that WILL be mine after some configuration.
27560Sstevel@tonic-gate 		 * We pay the price for this by making it a bi-directional
27570Sstevel@tonic-gate 		 * SA.
27580Sstevel@tonic-gate 		 */
27590Sstevel@tonic-gate 		if (ksi->ks_in_srctype != KS_IN_ADDR_ME)
27600Sstevel@tonic-gate 			clone = B_TRUE;
27610Sstevel@tonic-gate 		break;
27620Sstevel@tonic-gate 	default:
27633055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
27640Sstevel@tonic-gate 		return (EINVAL);
27650Sstevel@tonic-gate 	}
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 	/*
27680Sstevel@tonic-gate 	 * Find a ACQUIRE list entry if possible.  If we've added an SA that
27690Sstevel@tonic-gate 	 * suits the needs of an ACQUIRE list entry, we can eliminate the
27700Sstevel@tonic-gate 	 * ACQUIRE list entry and transmit the enqueued packets.  Use the
27710Sstevel@tonic-gate 	 * high-bit of the sequence number to queue it.  Key off destination
27720Sstevel@tonic-gate 	 * addr, and change acqrec's state.
27730Sstevel@tonic-gate 	 */
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate 	if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
27760Sstevel@tonic-gate 		acq_bucket = &sp->sdb_acq[outhash];
27770Sstevel@tonic-gate 		mutex_enter(&acq_bucket->iacqf_lock);
27780Sstevel@tonic-gate 		for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
27790Sstevel@tonic-gate 		    acqrec = acqrec->ipsacq_next) {
27800Sstevel@tonic-gate 			mutex_enter(&acqrec->ipsacq_lock);
27810Sstevel@tonic-gate 			/*
27820Sstevel@tonic-gate 			 * Q:  I only check sequence.  Should I check dst?
27830Sstevel@tonic-gate 			 * A: Yes, check dest because those are the packets
27840Sstevel@tonic-gate 			 *    that are queued up.
27850Sstevel@tonic-gate 			 */
27860Sstevel@tonic-gate 			if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
27870Sstevel@tonic-gate 			    IPSA_ARE_ADDR_EQUAL(dstaddr,
27880Sstevel@tonic-gate 				acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
27890Sstevel@tonic-gate 				break;
27900Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
27910Sstevel@tonic-gate 		}
27920Sstevel@tonic-gate 		if (acqrec != NULL) {
27930Sstevel@tonic-gate 			/*
27940Sstevel@tonic-gate 			 * AHA!  I found an ACQUIRE record for this SA.
27950Sstevel@tonic-gate 			 * Grab the msg list, and free the acquire record.
27960Sstevel@tonic-gate 			 * I already am holding the lock for this record,
27970Sstevel@tonic-gate 			 * so all I have to do is free it.
27980Sstevel@tonic-gate 			 */
27990Sstevel@tonic-gate 			acq_msgs = acqrec->ipsacq_mp;
28000Sstevel@tonic-gate 			acqrec->ipsacq_mp = NULL;
28010Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
28020Sstevel@tonic-gate 			sadb_destroy_acquire(acqrec);
28030Sstevel@tonic-gate 		}
28040Sstevel@tonic-gate 		mutex_exit(&acq_bucket->iacqf_lock);
28050Sstevel@tonic-gate 	}
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 	/*
28080Sstevel@tonic-gate 	 * Find PF_KEY message, and see if I'm an update.  If so, find entry
28090Sstevel@tonic-gate 	 * in larval list (if there).
28100Sstevel@tonic-gate 	 */
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_UPDATE) {
28130Sstevel@tonic-gate 		mutex_enter(&inbound->isaf_lock);
28140Sstevel@tonic-gate 		larval = ipsec_getassocbyspi(inbound, assoc->sadb_sa_spi,
28150Sstevel@tonic-gate 		    ALL_ZEROES_PTR, dstaddr, dst->sin_family);
28160Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 		if (larval == NULL) {
28190Sstevel@tonic-gate 			esp0dbg(("Larval update, but larval disappeared.\n"));
28200Sstevel@tonic-gate 			return (ESRCH);
28210Sstevel@tonic-gate 		} /* Else sadb_common_add unlinks it for me! */
28220Sstevel@tonic-gate 	}
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	lpkt = NULL;
28250Sstevel@tonic-gate 	if (larval != NULL)
28260Sstevel@tonic-gate 		lpkt = sadb_clear_lpkt(larval);
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	rc = sadb_common_add(esp_sadb.s_ip_q, esp_pfkey_q, mp, samsg, ksi,
28293055Sdanmcd 	    primary, secondary, larval, clone, is_inbound, diagnostic);
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 	if (rc == 0 && lpkt != NULL) {
28320Sstevel@tonic-gate 		rc = !taskq_dispatch(esp_taskq, inbound_task,
28330Sstevel@tonic-gate 			    (void *) lpkt, TQ_NOSLEEP);
28340Sstevel@tonic-gate 	}
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 	if (rc != 0) {
28370Sstevel@tonic-gate 		ip_drop_packet(lpkt, B_TRUE, NULL, NULL,
28380Sstevel@tonic-gate 		    &ipdrops_sadb_inlarval_timeout, &esp_dropper);
28390Sstevel@tonic-gate 	}
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	/*
28420Sstevel@tonic-gate 	 * How much more stack will I create with all of these
28430Sstevel@tonic-gate 	 * esp_outbound() calls?
28440Sstevel@tonic-gate 	 */
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate 	while (acq_msgs != NULL) {
28470Sstevel@tonic-gate 		mblk_t *mp = acq_msgs;
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 		acq_msgs = acq_msgs->b_next;
28500Sstevel@tonic-gate 		mp->b_next = NULL;
28510Sstevel@tonic-gate 		if (rc == 0) {
28520Sstevel@tonic-gate 			if (ipsec_outbound_sa(mp, IPPROTO_ESP)) {
28530Sstevel@tonic-gate 				((ipsec_out_t *)(mp->b_rptr))->
28540Sstevel@tonic-gate 				    ipsec_out_esp_done = B_TRUE;
28550Sstevel@tonic-gate 				if (esp_outbound(mp) == IPSEC_STATUS_SUCCESS) {
28563055Sdanmcd 					ipha_t *ipha;
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 					/* do AH processing if needed */
28590Sstevel@tonic-gate 					if (!esp_do_outbound_ah(mp))
28600Sstevel@tonic-gate 						continue;
28610Sstevel@tonic-gate 
28623055Sdanmcd 					ipha = (ipha_t *)mp->b_cont->b_rptr;
28633055Sdanmcd 
28640Sstevel@tonic-gate 					/* finish IPsec processing */
28650Sstevel@tonic-gate 					if (is_ipv4) {
28660Sstevel@tonic-gate 						ip_wput_ipsec_out(NULL, mp,
28670Sstevel@tonic-gate 						    ipha, NULL, NULL);
28680Sstevel@tonic-gate 					} else {
28690Sstevel@tonic-gate 						ip6_t *ip6h = (ip6_t *)ipha;
28700Sstevel@tonic-gate 						ip_wput_ipsec_out_v6(NULL,
28710Sstevel@tonic-gate 						    mp, ip6h, NULL, NULL);
28720Sstevel@tonic-gate 					}
28730Sstevel@tonic-gate 				}
28740Sstevel@tonic-gate 				continue;
28750Sstevel@tonic-gate 			}
28760Sstevel@tonic-gate 		}
28770Sstevel@tonic-gate 		ESP_BUMP_STAT(out_discards);
28780Sstevel@tonic-gate 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
28790Sstevel@tonic-gate 		    &ipdrops_sadb_acquire_timeout, &esp_dropper);
28800Sstevel@tonic-gate 	}
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	return (rc);
28830Sstevel@tonic-gate }
28840Sstevel@tonic-gate 
28850Sstevel@tonic-gate /*
28860Sstevel@tonic-gate  * Add new ESP security association.  This may become a generic AH/ESP
28870Sstevel@tonic-gate  * routine eventually.
28880Sstevel@tonic-gate  */
28890Sstevel@tonic-gate static int
28900Sstevel@tonic-gate esp_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic)
28910Sstevel@tonic-gate {
28920Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
28930Sstevel@tonic-gate 	sadb_address_t *srcext =
28940Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
28950Sstevel@tonic-gate 	sadb_address_t *dstext =
28960Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
28973055Sdanmcd 	sadb_address_t *isrcext =
28983055Sdanmcd 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
28993055Sdanmcd 	sadb_address_t *idstext =
29003055Sdanmcd 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
29010Sstevel@tonic-gate 	sadb_address_t *nttext_loc =
29020Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC];
29030Sstevel@tonic-gate 	sadb_address_t *nttext_rem =
29040Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM];
29050Sstevel@tonic-gate 	sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
29060Sstevel@tonic-gate 	sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT];
29070Sstevel@tonic-gate 	struct sockaddr_in *src, *dst;
29080Sstevel@tonic-gate 	struct sockaddr_in *natt_loc, *natt_rem;
29090Sstevel@tonic-gate 	struct sockaddr_in6 *natt_loc6, *natt_rem6;
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 	sadb_lifetime_t *soft =
29120Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
29130Sstevel@tonic-gate 	sadb_lifetime_t *hard =
29140Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	/* I need certain extensions present for an ADD message. */
29170Sstevel@tonic-gate 	if (srcext == NULL) {
29180Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
29190Sstevel@tonic-gate 		return (EINVAL);
29200Sstevel@tonic-gate 	}
29210Sstevel@tonic-gate 	if (dstext == NULL) {
29220Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
29230Sstevel@tonic-gate 		return (EINVAL);
29240Sstevel@tonic-gate 	}
29253055Sdanmcd 	if (isrcext == NULL && idstext != NULL) {
29263055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
29273055Sdanmcd 		return (EINVAL);
29283055Sdanmcd 	}
29293055Sdanmcd 	if (isrcext != NULL && idstext == NULL) {
29303055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
29313055Sdanmcd 		return (EINVAL);
29323055Sdanmcd 	}
29330Sstevel@tonic-gate 	if (assoc == NULL) {
29340Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
29350Sstevel@tonic-gate 		return (EINVAL);
29360Sstevel@tonic-gate 	}
29370Sstevel@tonic-gate 	if (ekey == NULL && assoc->sadb_sa_encrypt != SADB_EALG_NULL) {
29380Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_EKEY;
29390Sstevel@tonic-gate 		return (EINVAL);
29400Sstevel@tonic-gate 	}
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	src = (struct sockaddr_in *)(srcext + 1);
29430Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
29440Sstevel@tonic-gate 	natt_loc = (struct sockaddr_in *)(nttext_loc + 1);
29450Sstevel@tonic-gate 	natt_loc6 = (struct sockaddr_in6 *)(nttext_loc + 1);
29460Sstevel@tonic-gate 	natt_rem = (struct sockaddr_in *)(nttext_rem + 1);
29470Sstevel@tonic-gate 	natt_rem6 = (struct sockaddr_in6 *)(nttext_rem + 1);
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 	/* Sundry ADD-specific reality checks. */
29500Sstevel@tonic-gate 	/* XXX STATS :  Logging/stats here? */
29510Sstevel@tonic-gate 	if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
29520Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
29530Sstevel@tonic-gate 		return (EINVAL);
29540Sstevel@tonic-gate 	}
29550Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt == SADB_EALG_NONE) {
29560Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
29570Sstevel@tonic-gate 		return (EINVAL);
29580Sstevel@tonic-gate 	}
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt == SADB_EALG_NULL &&
29610Sstevel@tonic-gate 	    assoc->sadb_sa_auth == SADB_AALG_NONE) {
29620Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
29630Sstevel@tonic-gate 		return (EINVAL);
29640Sstevel@tonic-gate 	}
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & ~(SADB_SAFLAGS_NOREPLAY |
29673055Sdanmcd 	    SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM |
29683055Sdanmcd 		SADB_X_SAFLAGS_TUNNEL)) {
29690Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
29700Sstevel@tonic-gate 		return (EINVAL);
29710Sstevel@tonic-gate 	}
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate 	if ((*diagnostic = sadb_hardsoftchk(hard, soft)) != 0) {
29740Sstevel@tonic-gate 		return (EINVAL);
29750Sstevel@tonic-gate 	}
29763055Sdanmcd 	ASSERT(src->sin_family == dst->sin_family);
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) {
29790Sstevel@tonic-gate 		if (nttext_loc == NULL) {
29800Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_LOC;
29810Sstevel@tonic-gate 			return (EINVAL);
29820Sstevel@tonic-gate 		}
29830Sstevel@tonic-gate 
29840Sstevel@tonic-gate 		if (natt_loc->sin_family == AF_INET6 &&
29850Sstevel@tonic-gate 		    !IN6_IS_ADDR_V4MAPPED(&natt_loc6->sin6_addr)) {
29860Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC;
29870Sstevel@tonic-gate 			return (EINVAL);
29880Sstevel@tonic-gate 		}
29890Sstevel@tonic-gate 	}
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) {
29920Sstevel@tonic-gate 		if (nttext_rem == NULL) {
29930Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_REM;
29940Sstevel@tonic-gate 			return (EINVAL);
29950Sstevel@tonic-gate 		}
29960Sstevel@tonic-gate 		if (natt_rem->sin_family == AF_INET6 &&
29970Sstevel@tonic-gate 		    !IN6_IS_ADDR_V4MAPPED(&natt_rem6->sin6_addr)) {
29980Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM;
29990Sstevel@tonic-gate 			return (EINVAL);
30000Sstevel@tonic-gate 		}
30010Sstevel@tonic-gate 	}
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 
30040Sstevel@tonic-gate 	/* Stuff I don't support, for now.  XXX Diagnostic? */
30050Sstevel@tonic-gate 	if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL ||
30060Sstevel@tonic-gate 	    ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL)
30070Sstevel@tonic-gate 		return (EOPNOTSUPP);
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 	/*
30100Sstevel@tonic-gate 	 * XXX Policy :  I'm not checking identities or sensitivity
30110Sstevel@tonic-gate 	 * labels at this time, but if I did, I'd do them here, before I sent
30120Sstevel@tonic-gate 	 * the weak key check up to the algorithm.
30130Sstevel@tonic-gate 	 */
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	mutex_enter(&alg_lock);
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 	/*
30180Sstevel@tonic-gate 	 * First locate the authentication algorithm.
30190Sstevel@tonic-gate 	 */
30200Sstevel@tonic-gate 	if (akey != NULL) {
30210Sstevel@tonic-gate 		ipsec_alginfo_t *aalg;
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate 		aalg = ipsec_alglists[IPSEC_ALG_AUTH][assoc->sadb_sa_auth];
30240Sstevel@tonic-gate 		if (aalg == NULL || !ALG_VALID(aalg)) {
30250Sstevel@tonic-gate 			mutex_exit(&alg_lock);
30260Sstevel@tonic-gate 			esp1dbg(("Couldn't find auth alg #%d.\n",
30270Sstevel@tonic-gate 			    assoc->sadb_sa_auth));
30280Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
30290Sstevel@tonic-gate 			return (EINVAL);
30300Sstevel@tonic-gate 		}
30312810Smarkfen 
30322810Smarkfen 		/*
30332810Smarkfen 		 * Sanity check key sizes.
30342810Smarkfen 		 * Note: It's not possible to use SADB_AALG_NONE because
30352810Smarkfen 		 * this auth_alg is not defined with ALG_FLAG_VALID. If this
30362810Smarkfen 		 * ever changes, the same check for SADB_AALG_NONE and
30372810Smarkfen 		 * a auth_key != NULL should be made here ( see below).
30382810Smarkfen 		 */
30390Sstevel@tonic-gate 		if (!ipsec_valid_key_size(akey->sadb_key_bits, aalg)) {
30400Sstevel@tonic-gate 			mutex_exit(&alg_lock);
30410Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
30420Sstevel@tonic-gate 			return (EINVAL);
30430Sstevel@tonic-gate 		}
30442810Smarkfen 		ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
30450Sstevel@tonic-gate 
30460Sstevel@tonic-gate 		/* check key and fix parity if needed */
30470Sstevel@tonic-gate 		if (ipsec_check_key(aalg->alg_mech_type, akey, B_TRUE,
30480Sstevel@tonic-gate 		    diagnostic) != 0) {
30490Sstevel@tonic-gate 			mutex_exit(&alg_lock);
30500Sstevel@tonic-gate 			return (EINVAL);
30510Sstevel@tonic-gate 		}
30520Sstevel@tonic-gate 	}
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	/*
30550Sstevel@tonic-gate 	 * Then locate the encryption algorithm.
30560Sstevel@tonic-gate 	 */
30570Sstevel@tonic-gate 	if (ekey != NULL) {
30580Sstevel@tonic-gate 		ipsec_alginfo_t *ealg;
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 		ealg = ipsec_alglists[IPSEC_ALG_ENCR][assoc->sadb_sa_encrypt];
30610Sstevel@tonic-gate 		if (ealg == NULL || !ALG_VALID(ealg)) {
30620Sstevel@tonic-gate 			mutex_exit(&alg_lock);
30630Sstevel@tonic-gate 			esp1dbg(("Couldn't find encr alg #%d.\n",
30640Sstevel@tonic-gate 			    assoc->sadb_sa_encrypt));
30650Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
30660Sstevel@tonic-gate 			return (EINVAL);
30670Sstevel@tonic-gate 		}
30682810Smarkfen 
30692810Smarkfen 		/*
30702810Smarkfen 		 * Sanity check key sizes. If the encryption algorithm is
30712810Smarkfen 		 * SADB_EALG_NULL but the encryption key is NOT
30722810Smarkfen 		 * NULL then complain.
30732810Smarkfen 		 */
30742810Smarkfen 		if ((assoc->sadb_sa_encrypt == SADB_EALG_NULL) ||
30752810Smarkfen 		    (!ipsec_valid_key_size(ekey->sadb_key_bits, ealg))) {
30760Sstevel@tonic-gate 			mutex_exit(&alg_lock);
30770Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_EKEYBITS;
30780Sstevel@tonic-gate 			return (EINVAL);
30790Sstevel@tonic-gate 		}
30802810Smarkfen 		ASSERT(ealg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate 		/* check key */
30830Sstevel@tonic-gate 		if (ipsec_check_key(ealg->alg_mech_type, ekey, B_FALSE,
30840Sstevel@tonic-gate 		    diagnostic) != 0) {
30850Sstevel@tonic-gate 			mutex_exit(&alg_lock);
30860Sstevel@tonic-gate 			return (EINVAL);
30870Sstevel@tonic-gate 		}
30880Sstevel@tonic-gate 	}
30890Sstevel@tonic-gate 	mutex_exit(&alg_lock);
30900Sstevel@tonic-gate 
30913055Sdanmcd 	return (esp_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
30923055Sdanmcd 		    diagnostic));
30930Sstevel@tonic-gate }
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate /*
30960Sstevel@tonic-gate  * Update a security association.  Updates come in two varieties.  The first
30970Sstevel@tonic-gate  * is an update of lifetimes on a non-larval SA.  The second is an update of
30980Sstevel@tonic-gate  * a larval SA, which ends up looking a lot more like an add.
30990Sstevel@tonic-gate  */
31000Sstevel@tonic-gate static int
31010Sstevel@tonic-gate esp_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic)
31020Sstevel@tonic-gate {
31030Sstevel@tonic-gate 	sadb_address_t *dstext =
31040Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
31050Sstevel@tonic-gate 	struct sockaddr_in *sin;
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate 	if (dstext == NULL) {
31080Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
31090Sstevel@tonic-gate 		return (EINVAL);
31100Sstevel@tonic-gate 	}
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 	sin = (struct sockaddr_in *)(dstext + 1);
31130Sstevel@tonic-gate 	return (sadb_update_sa(mp, ksi,
31140Sstevel@tonic-gate 	    (sin->sin_family == AF_INET6) ? &esp_sadb.s_v6 : &esp_sadb.s_v4,
31150Sstevel@tonic-gate 	    diagnostic, esp_pfkey_q, esp_add_sa));
31160Sstevel@tonic-gate }
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate /*
31190Sstevel@tonic-gate  * Delete a security association.  This is REALLY likely to be code common to
31200Sstevel@tonic-gate  * both AH and ESP.  Find the association, then unlink it.
31210Sstevel@tonic-gate  */
31220Sstevel@tonic-gate static int
31230Sstevel@tonic-gate esp_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic)
31240Sstevel@tonic-gate {
31250Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
31260Sstevel@tonic-gate 	sadb_address_t *dstext =
31270Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
31280Sstevel@tonic-gate 	sadb_address_t *srcext =
31290Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
31300Sstevel@tonic-gate 	struct sockaddr_in *sin;
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate 	if (assoc == NULL) {
31330Sstevel@tonic-gate 		if (dstext != NULL) {
31340Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(dstext + 1);
31350Sstevel@tonic-gate 		} else if (srcext != NULL) {
31360Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(srcext + 1);
31370Sstevel@tonic-gate 		} else {
31380Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
31390Sstevel@tonic-gate 			return (EINVAL);
31400Sstevel@tonic-gate 		}
31413055Sdanmcd 		return (sadb_purge_sa(mp, ksi,
31423055Sdanmcd 			    (sin->sin_family == AF_INET6) ? &esp_sadb.s_v6 :
31433055Sdanmcd 			    &esp_sadb.s_v4, esp_pfkey_q, esp_sadb.s_ip_q));
31440Sstevel@tonic-gate 	}
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 	return (sadb_del_sa(mp, ksi, &esp_sadb, diagnostic, esp_pfkey_q));
31470Sstevel@tonic-gate }
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate /*
31500Sstevel@tonic-gate  * Convert the entire contents of all of ESP's SA tables into PF_KEY SADB_DUMP
31510Sstevel@tonic-gate  * messages.
31520Sstevel@tonic-gate  */
31530Sstevel@tonic-gate static void
31540Sstevel@tonic-gate esp_dump(mblk_t *mp, keysock_in_t *ksi)
31550Sstevel@tonic-gate {
31560Sstevel@tonic-gate 	int error;
31570Sstevel@tonic-gate 	sadb_msg_t *samsg;
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate 	/*
31600Sstevel@tonic-gate 	 * Dump each fanout, bailing if error is non-zero.
31610Sstevel@tonic-gate 	 */
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	error = sadb_dump(esp_pfkey_q, mp, ksi->ks_in_serial, &esp_sadb.s_v4);
31640Sstevel@tonic-gate 	if (error != 0)
31650Sstevel@tonic-gate 		goto bail;
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate 	error = sadb_dump(esp_pfkey_q, mp, ksi->ks_in_serial, &esp_sadb.s_v6);
31680Sstevel@tonic-gate bail:
31690Sstevel@tonic-gate 	ASSERT(mp->b_cont != NULL);
31700Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
31710Sstevel@tonic-gate 	samsg->sadb_msg_errno = (uint8_t)error;
31720Sstevel@tonic-gate 	sadb_pfkey_echo(esp_pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
31730Sstevel@tonic-gate 	    NULL);
31740Sstevel@tonic-gate }
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate /*
31773055Sdanmcd  * First-cut reality check for an inbound PF_KEY message.
31783055Sdanmcd  */
31793055Sdanmcd static boolean_t
31803055Sdanmcd esp_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi)
31813055Sdanmcd {
31823055Sdanmcd 	int diagnostic;
31833055Sdanmcd 
31843055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
31853055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
31863055Sdanmcd 		goto badmsg;
31873055Sdanmcd 	}
31883055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
31893055Sdanmcd 	    ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
31903055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
31913055Sdanmcd 		goto badmsg;
31923055Sdanmcd 	}
31933055Sdanmcd 	return (B_FALSE);	/* False ==> no failures */
31943055Sdanmcd 
31953055Sdanmcd badmsg:
31963055Sdanmcd 	sadb_pfkey_error(esp_pfkey_q, mp, EINVAL, diagnostic,
31973055Sdanmcd 	    ksi->ks_in_serial);
31983055Sdanmcd 	return (B_TRUE);	/* True ==> failures */
31993055Sdanmcd }
32003055Sdanmcd 
32013055Sdanmcd /*
32020Sstevel@tonic-gate  * ESP parsing of PF_KEY messages.  Keysock did most of the really silly
32030Sstevel@tonic-gate  * error cases.  What I receive is a fully-formed, syntactically legal
32040Sstevel@tonic-gate  * PF_KEY message.  I then need to check semantics...
32050Sstevel@tonic-gate  *
32060Sstevel@tonic-gate  * This code may become common to AH and ESP.  Stay tuned.
32070Sstevel@tonic-gate  *
32080Sstevel@tonic-gate  * I also make the assumption that db_ref's are cool.  If this assumption
32090Sstevel@tonic-gate  * is wrong, this means that someone other than keysock or me has been
32100Sstevel@tonic-gate  * mucking with PF_KEY messages.
32110Sstevel@tonic-gate  */
32120Sstevel@tonic-gate static void
32130Sstevel@tonic-gate esp_parse_pfkey(mblk_t *mp)
32140Sstevel@tonic-gate {
32150Sstevel@tonic-gate 	mblk_t *msg = mp->b_cont;
32160Sstevel@tonic-gate 	sadb_msg_t *samsg;
32170Sstevel@tonic-gate 	keysock_in_t *ksi;
32180Sstevel@tonic-gate 	int error;
32190Sstevel@tonic-gate 	int diagnostic = SADB_X_DIAGNOSTIC_NONE;
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	ASSERT(msg != NULL);
32220Sstevel@tonic-gate 	samsg = (sadb_msg_t *)msg->b_rptr;
32230Sstevel@tonic-gate 	ksi = (keysock_in_t *)mp->b_rptr;
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 	/*
32260Sstevel@tonic-gate 	 * If applicable, convert unspecified AF_INET6 to unspecified
32273055Sdanmcd 	 * AF_INET.  And do other address reality checks.
32280Sstevel@tonic-gate 	 */
32293055Sdanmcd 	if (!sadb_addrfix(ksi, esp_pfkey_q, mp) ||
32303055Sdanmcd 	    esp_pfkey_reality_failures(mp, ksi)) {
32313055Sdanmcd 		return;
32323055Sdanmcd 	}
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
32350Sstevel@tonic-gate 	case SADB_ADD:
32360Sstevel@tonic-gate 		error = esp_add_sa(mp, ksi, &diagnostic);
32370Sstevel@tonic-gate 		if (error != 0) {
32380Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic,
32390Sstevel@tonic-gate 			    ksi->ks_in_serial);
32400Sstevel@tonic-gate 		}
32410Sstevel@tonic-gate 		/* else esp_add_sa() took care of things. */
32420Sstevel@tonic-gate 		break;
32430Sstevel@tonic-gate 	case SADB_DELETE:
32440Sstevel@tonic-gate 		error = esp_del_sa(mp, ksi, &diagnostic);
32450Sstevel@tonic-gate 		if (error != 0) {
32460Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic,
32470Sstevel@tonic-gate 			    ksi->ks_in_serial);
32480Sstevel@tonic-gate 		}
32490Sstevel@tonic-gate 		/* Else esp_del_sa() took care of things. */
32500Sstevel@tonic-gate 		break;
32510Sstevel@tonic-gate 	case SADB_GET:
32520Sstevel@tonic-gate 		error = sadb_get_sa(mp, ksi, &esp_sadb, &diagnostic,
32530Sstevel@tonic-gate 		    esp_pfkey_q);
32540Sstevel@tonic-gate 		if (error != 0) {
32550Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic,
32560Sstevel@tonic-gate 			    ksi->ks_in_serial);
32570Sstevel@tonic-gate 		}
32580Sstevel@tonic-gate 		/* Else sadb_get_sa() took care of things. */
32590Sstevel@tonic-gate 		break;
32600Sstevel@tonic-gate 	case SADB_FLUSH:
32610Sstevel@tonic-gate 		sadbp_flush(&esp_sadb);
32620Sstevel@tonic-gate 		sadb_pfkey_echo(esp_pfkey_q, mp, samsg, ksi, NULL);
32630Sstevel@tonic-gate 		break;
32640Sstevel@tonic-gate 	case SADB_REGISTER:
32650Sstevel@tonic-gate 		/*
32660Sstevel@tonic-gate 		 * Hmmm, let's do it!  Check for extensions (there should
32670Sstevel@tonic-gate 		 * be none), extract the fields, call esp_register_out(),
32680Sstevel@tonic-gate 		 * then either free or report an error.
32690Sstevel@tonic-gate 		 *
32700Sstevel@tonic-gate 		 * Keysock takes care of the PF_KEY bookkeeping for this.
32710Sstevel@tonic-gate 		 */
32720Sstevel@tonic-gate 		if (esp_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
32730Sstevel@tonic-gate 		    ksi->ks_in_serial)) {
32740Sstevel@tonic-gate 			freemsg(mp);
32750Sstevel@tonic-gate 		} else {
32760Sstevel@tonic-gate 			/*
32770Sstevel@tonic-gate 			 * Only way this path hits is if there is a memory
32780Sstevel@tonic-gate 			 * failure.  It will not return B_FALSE because of
32790Sstevel@tonic-gate 			 * lack of esp_pfkey_q if I am in wput().
32800Sstevel@tonic-gate 			 */
32810Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, ENOMEM, diagnostic,
32820Sstevel@tonic-gate 			    ksi->ks_in_serial);
32830Sstevel@tonic-gate 		}
32840Sstevel@tonic-gate 		break;
32850Sstevel@tonic-gate 	case SADB_UPDATE:
32860Sstevel@tonic-gate 		/*
32870Sstevel@tonic-gate 		 * Find a larval, if not there, find a full one and get
32880Sstevel@tonic-gate 		 * strict.
32890Sstevel@tonic-gate 		 */
32900Sstevel@tonic-gate 		error = esp_update_sa(mp, ksi, &diagnostic);
32910Sstevel@tonic-gate 		if (error != 0) {
32920Sstevel@tonic-gate 			sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic,
32930Sstevel@tonic-gate 			    ksi->ks_in_serial);
32940Sstevel@tonic-gate 		}
32950Sstevel@tonic-gate 		/* else esp_update_sa() took care of things. */
32960Sstevel@tonic-gate 		break;
32970Sstevel@tonic-gate 	case SADB_GETSPI:
32980Sstevel@tonic-gate 		/*
32990Sstevel@tonic-gate 		 * Reserve a new larval entry.
33000Sstevel@tonic-gate 		 */
33010Sstevel@tonic-gate 		esp_getspi(mp, ksi);
33020Sstevel@tonic-gate 		break;
33030Sstevel@tonic-gate 	case SADB_ACQUIRE:
33040Sstevel@tonic-gate 		/*
33050Sstevel@tonic-gate 		 * Find larval and/or ACQUIRE record and kill it (them), I'm
33060Sstevel@tonic-gate 		 * most likely an error.  Inbound ACQUIRE messages should only
33070Sstevel@tonic-gate 		 * have the base header.
33080Sstevel@tonic-gate 		 */
33090Sstevel@tonic-gate 		sadb_in_acquire(samsg, &esp_sadb, esp_pfkey_q);
33100Sstevel@tonic-gate 		freemsg(mp);
33110Sstevel@tonic-gate 		break;
33120Sstevel@tonic-gate 	case SADB_DUMP:
33130Sstevel@tonic-gate 		/*
33140Sstevel@tonic-gate 		 * Dump all entries.
33150Sstevel@tonic-gate 		 */
33160Sstevel@tonic-gate 		esp_dump(mp, ksi);
33170Sstevel@tonic-gate 		/* esp_dump will take care of the return message, etc. */
33180Sstevel@tonic-gate 		break;
33190Sstevel@tonic-gate 	case SADB_EXPIRE:
33200Sstevel@tonic-gate 		/* Should never reach me. */
33210Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, EOPNOTSUPP, diagnostic,
33220Sstevel@tonic-gate 		    ksi->ks_in_serial);
33230Sstevel@tonic-gate 		break;
33240Sstevel@tonic-gate 	default:
33250Sstevel@tonic-gate 		sadb_pfkey_error(esp_pfkey_q, mp, EINVAL,
33260Sstevel@tonic-gate 		    SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
33270Sstevel@tonic-gate 		break;
33280Sstevel@tonic-gate 	}
33290Sstevel@tonic-gate }
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate /*
33320Sstevel@tonic-gate  * Handle case where PF_KEY says it can't find a keysock for one of my
33330Sstevel@tonic-gate  * ACQUIRE messages.
33340Sstevel@tonic-gate  */
33350Sstevel@tonic-gate static void
33360Sstevel@tonic-gate esp_keysock_no_socket(mblk_t *mp)
33370Sstevel@tonic-gate {
33380Sstevel@tonic-gate 	sadb_msg_t *samsg;
33390Sstevel@tonic-gate 	keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate 	if (mp->b_cont == NULL) {
33420Sstevel@tonic-gate 		freemsg(mp);
33430Sstevel@tonic-gate 		return;
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
33460Sstevel@tonic-gate 
33470Sstevel@tonic-gate 	/*
33480Sstevel@tonic-gate 	 * If keysock can't find any registered, delete the acquire record
33490Sstevel@tonic-gate 	 * immediately, and handle errors.
33500Sstevel@tonic-gate 	 */
33510Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_ACQUIRE) {
33520Sstevel@tonic-gate 		samsg->sadb_msg_errno = kse->ks_err_errno;
33530Sstevel@tonic-gate 		samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
33540Sstevel@tonic-gate 		/*
33550Sstevel@tonic-gate 		 * Use the write-side of the esp_pfkey_q, in case there is
33560Sstevel@tonic-gate 		 * no esp_sadb.s_ip_q.
33570Sstevel@tonic-gate 		 */
33580Sstevel@tonic-gate 		sadb_in_acquire(samsg, &esp_sadb, WR(esp_pfkey_q));
33590Sstevel@tonic-gate 	}
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 	freemsg(mp);
33620Sstevel@tonic-gate }
33630Sstevel@tonic-gate 
33640Sstevel@tonic-gate /*
33650Sstevel@tonic-gate  * ESP module write put routine.
33660Sstevel@tonic-gate  */
33670Sstevel@tonic-gate static void
33680Sstevel@tonic-gate ipsecesp_wput(queue_t *q, mblk_t *mp)
33690Sstevel@tonic-gate {
33700Sstevel@tonic-gate 	ipsec_info_t *ii;
33710Sstevel@tonic-gate 	struct iocblk *iocp;
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate 	esp3dbg(("In esp_wput().\n"));
33740Sstevel@tonic-gate 
33750Sstevel@tonic-gate 	/* NOTE: Each case must take care of freeing or passing mp. */
33760Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
33770Sstevel@tonic-gate 	case M_CTL:
33780Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
33790Sstevel@tonic-gate 			/* Not big enough message. */
33800Sstevel@tonic-gate 			freemsg(mp);
33810Sstevel@tonic-gate 			break;
33820Sstevel@tonic-gate 		}
33830Sstevel@tonic-gate 		ii = (ipsec_info_t *)mp->b_rptr;
33840Sstevel@tonic-gate 
33850Sstevel@tonic-gate 		switch (ii->ipsec_info_type) {
33860Sstevel@tonic-gate 		case KEYSOCK_OUT_ERR:
33870Sstevel@tonic-gate 			esp1dbg(("Got KEYSOCK_OUT_ERR message.\n"));
33880Sstevel@tonic-gate 			esp_keysock_no_socket(mp);
33890Sstevel@tonic-gate 			break;
33900Sstevel@tonic-gate 		case KEYSOCK_IN:
33910Sstevel@tonic-gate 			ESP_BUMP_STAT(keysock_in);
33920Sstevel@tonic-gate 			esp3dbg(("Got KEYSOCK_IN message.\n"));
33933055Sdanmcd 
33943055Sdanmcd 			/* Parse the message. */
33950Sstevel@tonic-gate 			esp_parse_pfkey(mp);
33960Sstevel@tonic-gate 			break;
33970Sstevel@tonic-gate 		case KEYSOCK_HELLO:
33980Sstevel@tonic-gate 			sadb_keysock_hello(&esp_pfkey_q, q, mp,
33990Sstevel@tonic-gate 			    esp_ager, &esp_event, SADB_SATYPE_ESP);
34000Sstevel@tonic-gate 			break;
34010Sstevel@tonic-gate 		default:
34020Sstevel@tonic-gate 			esp2dbg(("Got M_CTL from above of 0x%x.\n",
34030Sstevel@tonic-gate 			    ii->ipsec_info_type));
34040Sstevel@tonic-gate 			freemsg(mp);
34050Sstevel@tonic-gate 			break;
34060Sstevel@tonic-gate 		}
34070Sstevel@tonic-gate 		break;
34080Sstevel@tonic-gate 	case M_IOCTL:
34090Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
34100Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
34110Sstevel@tonic-gate 		case ND_SET:
34120Sstevel@tonic-gate 		case ND_GET:
34130Sstevel@tonic-gate 			if (nd_getset(q, ipsecesp_g_nd, mp)) {
34140Sstevel@tonic-gate 				qreply(q, mp);
34150Sstevel@tonic-gate 				return;
34160Sstevel@tonic-gate 			} else {
34170Sstevel@tonic-gate 				iocp->ioc_error = ENOENT;
34180Sstevel@tonic-gate 			}
34190Sstevel@tonic-gate 			/* FALLTHRU */
34200Sstevel@tonic-gate 		default:
34210Sstevel@tonic-gate 			/* We really don't support any other ioctls, do we? */
34220Sstevel@tonic-gate 
34230Sstevel@tonic-gate 			/* Return EINVAL */
34240Sstevel@tonic-gate 			if (iocp->ioc_error != ENOENT)
34250Sstevel@tonic-gate 				iocp->ioc_error = EINVAL;
34260Sstevel@tonic-gate 			iocp->ioc_count = 0;
34270Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34280Sstevel@tonic-gate 			qreply(q, mp);
34290Sstevel@tonic-gate 			return;
34300Sstevel@tonic-gate 		}
34310Sstevel@tonic-gate 	default:
34320Sstevel@tonic-gate 		esp3dbg(("Got default message, type %d, passing to IP.\n",
34330Sstevel@tonic-gate 		    mp->b_datap->db_type));
34340Sstevel@tonic-gate 		putnext(q, mp);
34350Sstevel@tonic-gate 	}
34360Sstevel@tonic-gate }
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate /*
34390Sstevel@tonic-gate  * Process an outbound ESP packet that can be accelerated by a IPsec
34400Sstevel@tonic-gate  * hardware acceleration capable Provider.
34410Sstevel@tonic-gate  * The caller already inserted and initialized the ESP header.
34420Sstevel@tonic-gate  * This function allocates a tagging M_CTL, and adds room at the end
34430Sstevel@tonic-gate  * of the packet to hold the ICV if authentication is needed.
34440Sstevel@tonic-gate  *
34450Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
34460Sstevel@tonic-gate  * mblk chain ipsec_out.
34470Sstevel@tonic-gate  */
34480Sstevel@tonic-gate static ipsec_status_t
34490Sstevel@tonic-gate esp_outbound_accelerated(mblk_t *ipsec_out, uint_t icv_len)
34500Sstevel@tonic-gate {
34510Sstevel@tonic-gate 	ipsec_out_t *io;
34520Sstevel@tonic-gate 	mblk_t *lastmp;
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 	ESP_BUMP_STAT(out_accelerated);
34550Sstevel@tonic-gate 
34560Sstevel@tonic-gate 	io = (ipsec_out_t *)ipsec_out->b_rptr;
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	/* mark packet as being accelerated in IPSEC_OUT */
34590Sstevel@tonic-gate 	ASSERT(io->ipsec_out_accelerated == B_FALSE);
34600Sstevel@tonic-gate 	io->ipsec_out_accelerated = B_TRUE;
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	/*
34630Sstevel@tonic-gate 	 * add room at the end of the packet for the ICV if needed
34640Sstevel@tonic-gate 	 */
34650Sstevel@tonic-gate 	if (icv_len > 0) {
34660Sstevel@tonic-gate 		/* go to last mblk */
34670Sstevel@tonic-gate 		lastmp = ipsec_out;	/* For following while loop. */
34680Sstevel@tonic-gate 		do {
34690Sstevel@tonic-gate 			lastmp = lastmp->b_cont;
34700Sstevel@tonic-gate 		} while (lastmp->b_cont != NULL);
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 		/* if not enough available room, allocate new mblk */
34730Sstevel@tonic-gate 		if ((lastmp->b_wptr + icv_len) > lastmp->b_datap->db_lim) {
34740Sstevel@tonic-gate 			lastmp->b_cont = allocb(icv_len, BPRI_HI);
34750Sstevel@tonic-gate 			if (lastmp->b_cont == NULL) {
34760Sstevel@tonic-gate 				ESP_BUMP_STAT(out_discards);
34770Sstevel@tonic-gate 				ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
34780Sstevel@tonic-gate 				    &ipdrops_esp_nomem, &esp_dropper);
34790Sstevel@tonic-gate 				return (IPSEC_STATUS_FAILED);
34800Sstevel@tonic-gate 			}
34810Sstevel@tonic-gate 			lastmp = lastmp->b_cont;
34820Sstevel@tonic-gate 		}
34830Sstevel@tonic-gate 		lastmp->b_wptr += icv_len;
34840Sstevel@tonic-gate 	}
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
34870Sstevel@tonic-gate }
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate /*
34900Sstevel@tonic-gate  * Process an inbound accelerated ESP packet.
34910Sstevel@tonic-gate  * On success returns B_TRUE, on failure returns B_FALSE and frees the
34920Sstevel@tonic-gate  * mblk chain ipsec_in.
34930Sstevel@tonic-gate  */
34940Sstevel@tonic-gate static ipsec_status_t
34950Sstevel@tonic-gate esp_inbound_accelerated(mblk_t *ipsec_in, mblk_t *data_mp, boolean_t isv4,
34960Sstevel@tonic-gate     ipsa_t *assoc)
34970Sstevel@tonic-gate {
34980Sstevel@tonic-gate 	ipsec_in_t *ii;
34990Sstevel@tonic-gate 	mblk_t *hada_mp;
35000Sstevel@tonic-gate 	uint32_t icv_len = 0;
35010Sstevel@tonic-gate 	da_ipsec_t *hada;
35020Sstevel@tonic-gate 	ipha_t *ipha;
35030Sstevel@tonic-gate 	ip6_t *ip6h;
35040Sstevel@tonic-gate 	kstat_named_t *counter;
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 	ESP_BUMP_STAT(in_accelerated);
35070Sstevel@tonic-gate 
35080Sstevel@tonic-gate 	ii = (ipsec_in_t *)ipsec_in->b_rptr;
35090Sstevel@tonic-gate 	hada_mp = ii->ipsec_in_da;
35100Sstevel@tonic-gate 	ASSERT(hada_mp != NULL);
35110Sstevel@tonic-gate 	hada = (da_ipsec_t *)hada_mp->b_rptr;
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 	/*
35140Sstevel@tonic-gate 	 * We only support one level of decapsulation in hardware, so
35150Sstevel@tonic-gate 	 * nuke the pointer.
35160Sstevel@tonic-gate 	 */
35170Sstevel@tonic-gate 	ii->ipsec_in_da = NULL;
35180Sstevel@tonic-gate 	ii->ipsec_in_accelerated = B_FALSE;
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate 	if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) {
35210Sstevel@tonic-gate 		/*
35220Sstevel@tonic-gate 		 * ESP with authentication. We expect the Provider to have
35230Sstevel@tonic-gate 		 * computed the ICV and placed it in the hardware acceleration
35240Sstevel@tonic-gate 		 * data attributes.
35250Sstevel@tonic-gate 		 *
35260Sstevel@tonic-gate 		 * Extract ICV length from attributes M_CTL and sanity check
35270Sstevel@tonic-gate 		 * its value. We allow the mblk to be smaller than da_ipsec_t
35280Sstevel@tonic-gate 		 * for a small ICV, as long as the entire ICV fits within the
35290Sstevel@tonic-gate 		 * mblk.
35300Sstevel@tonic-gate 		 *
35310Sstevel@tonic-gate 		 * Also ensures that the ICV length computed by Provider
35320Sstevel@tonic-gate 		 * corresponds to the ICV length of the agorithm specified by
35330Sstevel@tonic-gate 		 * the SA.
35340Sstevel@tonic-gate 		 */
35350Sstevel@tonic-gate 		icv_len = hada->da_icv_len;
35360Sstevel@tonic-gate 		if ((icv_len != assoc->ipsa_mac_len) ||
35370Sstevel@tonic-gate 		    (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) <
35380Sstevel@tonic-gate 			(sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) {
35390Sstevel@tonic-gate 			esp0dbg(("esp_inbound_accelerated: "
35400Sstevel@tonic-gate 			    "ICV len (%u) incorrect or mblk too small (%u)\n",
35410Sstevel@tonic-gate 			    icv_len, (uint32_t)(MBLKL(hada_mp))));
35420Sstevel@tonic-gate 			counter = &ipdrops_esp_bad_auth;
35430Sstevel@tonic-gate 			goto esp_in_discard;
35440Sstevel@tonic-gate 		}
35450Sstevel@tonic-gate 	}
35460Sstevel@tonic-gate 
35470Sstevel@tonic-gate 	/* get pointers to IP header */
35480Sstevel@tonic-gate 	if (isv4) {
35490Sstevel@tonic-gate 		ipha = (ipha_t *)data_mp->b_rptr;
35500Sstevel@tonic-gate 	} else {
35510Sstevel@tonic-gate 		ip6h = (ip6_t *)data_mp->b_rptr;
35520Sstevel@tonic-gate 	}
35530Sstevel@tonic-gate 
35540Sstevel@tonic-gate 	/*
35550Sstevel@tonic-gate 	 * Compare ICV in ESP packet vs ICV computed by adapter.
35560Sstevel@tonic-gate 	 * We also remove the ICV from the end of the packet since
35570Sstevel@tonic-gate 	 * it will no longer be needed.
35580Sstevel@tonic-gate 	 *
35590Sstevel@tonic-gate 	 * Assume that esp_inbound() already ensured that the pkt
35600Sstevel@tonic-gate 	 * was in one mblk.
35610Sstevel@tonic-gate 	 */
35620Sstevel@tonic-gate 	ASSERT(data_mp->b_cont == NULL);
35630Sstevel@tonic-gate 	data_mp->b_wptr -= icv_len;
35640Sstevel@tonic-gate 	/* adjust IP header */
35650Sstevel@tonic-gate 	if (isv4)
35660Sstevel@tonic-gate 		ipha->ipha_length = htons(ntohs(ipha->ipha_length) - icv_len);
35670Sstevel@tonic-gate 	else
35680Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - icv_len);
35690Sstevel@tonic-gate 	if (icv_len && bcmp(hada->da_icv, data_mp->b_wptr, icv_len)) {
35700Sstevel@tonic-gate 		int af;
35710Sstevel@tonic-gate 		void *addr;
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate 		if (isv4) {
35740Sstevel@tonic-gate 			addr = &ipha->ipha_dst;
35750Sstevel@tonic-gate 			af = AF_INET;
35760Sstevel@tonic-gate 		} else {
35770Sstevel@tonic-gate 			addr = &ip6h->ip6_dst;
35780Sstevel@tonic-gate 			af = AF_INET6;
35790Sstevel@tonic-gate 		}
35800Sstevel@tonic-gate 
35810Sstevel@tonic-gate 		/*
35820Sstevel@tonic-gate 		 * Log the event. Don't print to the console, block
35830Sstevel@tonic-gate 		 * potential denial-of-service attack.
35840Sstevel@tonic-gate 		 */
35850Sstevel@tonic-gate 		ESP_BUMP_STAT(bad_auth);
35860Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
35870Sstevel@tonic-gate 		    "ESP Authentication failed spi %x, dst_addr %s",
35880Sstevel@tonic-gate 		    assoc->ipsa_spi, addr, af);
35890Sstevel@tonic-gate 		counter = &ipdrops_esp_bad_auth;
35900Sstevel@tonic-gate 		goto esp_in_discard;
35910Sstevel@tonic-gate 	}
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	esp3dbg(("esp_inbound_accelerated: ESP authentication succeeded, "
35940Sstevel@tonic-gate 	    "checking replay\n"));
35950Sstevel@tonic-gate 
35960Sstevel@tonic-gate 	ipsec_in->b_cont = data_mp;
35970Sstevel@tonic-gate 
35980Sstevel@tonic-gate 	/*
35990Sstevel@tonic-gate 	 * Remove ESP header and padding from packet.
36000Sstevel@tonic-gate 	 */
36010Sstevel@tonic-gate 	if (!esp_strip_header(data_mp, ii->ipsec_in_v4, assoc->ipsa_iv_len,
36020Sstevel@tonic-gate 		&counter)) {
36030Sstevel@tonic-gate 		esp1dbg(("esp_inbound_accelerated: "
36040Sstevel@tonic-gate 		    "esp_strip_header() failed\n"));
36050Sstevel@tonic-gate 		goto esp_in_discard;
36060Sstevel@tonic-gate 	}
36070Sstevel@tonic-gate 
36080Sstevel@tonic-gate 	freeb(hada_mp);
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 	/*
36110Sstevel@tonic-gate 	 * Account for usage..
36120Sstevel@tonic-gate 	 */
36130Sstevel@tonic-gate 	if (!esp_age_bytes(assoc, msgdsize(data_mp), B_TRUE)) {
36140Sstevel@tonic-gate 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
36150Sstevel@tonic-gate 		ESP_BUMP_STAT(bytes_expired);
36160Sstevel@tonic-gate 		IP_ESP_BUMP_STAT(in_discards);
36170Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
36180Sstevel@tonic-gate 		    "ESP association 0x%x, dst %s had bytes expire.\n",
36190Sstevel@tonic-gate 		    assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam);
36200Sstevel@tonic-gate 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
36210Sstevel@tonic-gate 		    &ipdrops_esp_bytes_expire, &esp_dropper);
36220Sstevel@tonic-gate 		return (IPSEC_STATUS_FAILED);
36230Sstevel@tonic-gate 	}
36240Sstevel@tonic-gate 
36250Sstevel@tonic-gate 	/* done processing the packet */
36260Sstevel@tonic-gate 	return (IPSEC_STATUS_SUCCESS);
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate esp_in_discard:
36290Sstevel@tonic-gate 	IP_ESP_BUMP_STAT(in_discards);
36300Sstevel@tonic-gate 	freeb(hada_mp);
36310Sstevel@tonic-gate 
36320Sstevel@tonic-gate 	ipsec_in->b_cont = data_mp;	/* For ip_drop_packet()'s sake... */
36330Sstevel@tonic-gate 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter, &esp_dropper);
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 	return (IPSEC_STATUS_FAILED);
36360Sstevel@tonic-gate }
36370Sstevel@tonic-gate 
36380Sstevel@tonic-gate /*
36390Sstevel@tonic-gate  * Wrapper to allow IP to trigger an ESP association failure message
36400Sstevel@tonic-gate  * during inbound SA selection.
36410Sstevel@tonic-gate  */
36420Sstevel@tonic-gate void
36430Sstevel@tonic-gate ipsecesp_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
36440Sstevel@tonic-gate     uint32_t spi, void *addr, int af)
36450Sstevel@tonic-gate {
36460Sstevel@tonic-gate 	if (ipsecesp_log_unknown_spi) {
36470Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
36480Sstevel@tonic-gate 		    addr, af);
36490Sstevel@tonic-gate 	}
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 	ip_drop_packet(mp, B_TRUE, NULL, NULL, &ipdrops_esp_no_sa,
36520Sstevel@tonic-gate 	    &esp_dropper);
36530Sstevel@tonic-gate }
36540Sstevel@tonic-gate 
36550Sstevel@tonic-gate /*
36560Sstevel@tonic-gate  * Initialize the ESP input and output processing functions.
36570Sstevel@tonic-gate  */
36580Sstevel@tonic-gate void
36590Sstevel@tonic-gate ipsecesp_init_funcs(ipsa_t *sa)
36600Sstevel@tonic-gate {
36610Sstevel@tonic-gate 	if (sa->ipsa_output_func == NULL)
36620Sstevel@tonic-gate 		sa->ipsa_output_func = esp_outbound;
36630Sstevel@tonic-gate 	if (sa->ipsa_input_func == NULL)
36640Sstevel@tonic-gate 		sa->ipsa_input_func = esp_inbound;
36650Sstevel@tonic-gate }
3666