xref: /onnv-gate/usr/src/uts/common/inet/ip/ipsecah.c (revision 11446:e1a2490f37f2)
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
51659Smarkfen  * Common Development and Distribution License (the "License").
61659Smarkfen  * 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 /*
22*11446Sdanmcd@sun.com  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/stream.h>
280Sstevel@tonic-gate #include <sys/stropts.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/strlog.h>
310Sstevel@tonic-gate #include <sys/tihdr.h>
320Sstevel@tonic-gate #include <sys/socket.h>
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
357749SThejaswini.Singarajipura@Sun.COM #include <sys/mkdev.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
373448Sdh155122 #include <sys/zone.h>
383192Sdanmcd #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 <netinet/icmp6.h>
490Sstevel@tonic-gate #include <net/pfkeyv2.h>
5010824SMark.Fenwick@Sun.COM #include <net/pfpolicy.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <inet/common.h>
530Sstevel@tonic-gate #include <inet/mi.h>
540Sstevel@tonic-gate #include <inet/ip.h>
550Sstevel@tonic-gate #include <inet/ip6.h>
560Sstevel@tonic-gate #include <inet/nd.h>
5711042SErik.Nordmark@Sun.COM #include <inet/ip_if.h>
5811042SErik.Nordmark@Sun.COM #include <inet/ip_ndp.h>
590Sstevel@tonic-gate #include <inet/ipsec_info.h>
600Sstevel@tonic-gate #include <inet/ipsec_impl.h>
610Sstevel@tonic-gate #include <inet/sadb.h>
620Sstevel@tonic-gate #include <inet/ipsecah.h>
630Sstevel@tonic-gate #include <inet/ipsec_impl.h>
640Sstevel@tonic-gate #include <inet/ipdrop.h>
650Sstevel@tonic-gate #include <sys/taskq.h>
660Sstevel@tonic-gate #include <sys/policy.h>
670Sstevel@tonic-gate #include <sys/strsun.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <sys/crypto/common.h>
700Sstevel@tonic-gate #include <sys/crypto/api.h>
710Sstevel@tonic-gate #include <sys/kstat.h>
721676Sjpk #include <sys/strsubr.h>
730Sstevel@tonic-gate 
7410934Ssommerfeld@sun.com #include <sys/tsol/tnet.h>
7510934Ssommerfeld@sun.com 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Table of ND variables supported by ipsecah. These are loaded into
780Sstevel@tonic-gate  * ipsecah_g_nd in ipsecah_init_nd.
790Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
800Sstevel@tonic-gate  */
813448Sdh155122 static	ipsecahparam_t	lcl_param_arr[] = {
820Sstevel@tonic-gate 	/* min	max			value	name */
830Sstevel@tonic-gate 	{ 0,	3,			0,	"ipsecah_debug"},
840Sstevel@tonic-gate 	{ 125,	32000, SADB_AGE_INTERVAL_DEFAULT,	"ipsecah_age_interval"},
850Sstevel@tonic-gate 	{ 1,	10,			1,	"ipsecah_reap_delay"},
860Sstevel@tonic-gate 	{ 1,	SADB_MAX_REPLAY,	64,	"ipsecah_replay_size"},
870Sstevel@tonic-gate 	{ 1,	300,			15,	"ipsecah_acquire_timeout"},
880Sstevel@tonic-gate 	{ 1,	1800,			90,	"ipsecah_larval_timeout"},
890Sstevel@tonic-gate 	/* Default lifetime values for ACQUIRE messages. */
900Sstevel@tonic-gate 	{ 0,	0xffffffffU,		0,	"ipsecah_default_soft_bytes"},
910Sstevel@tonic-gate 	{ 0,	0xffffffffU,		0,	"ipsecah_default_hard_bytes"},
920Sstevel@tonic-gate 	{ 0,	0xffffffffU,		24000,	"ipsecah_default_soft_addtime"},
930Sstevel@tonic-gate 	{ 0,	0xffffffffU,		28800,	"ipsecah_default_hard_addtime"},
940Sstevel@tonic-gate 	{ 0,	0xffffffffU,		0,	"ipsecah_default_soft_usetime"},
950Sstevel@tonic-gate 	{ 0,	0xffffffffU,		0,	"ipsecah_default_hard_usetime"},
960Sstevel@tonic-gate 	{ 0,	1,			0,	"ipsecah_log_unknown_spi"},
970Sstevel@tonic-gate };
983448Sdh155122 #define	ipsecah_debug			ipsecah_params[0].ipsecah_param_value
993448Sdh155122 #define	ipsecah_age_interval		ipsecah_params[1].ipsecah_param_value
1003448Sdh155122 #define	ipsecah_age_int_max		ipsecah_params[1].ipsecah_param_max
1013448Sdh155122 #define	ipsecah_reap_delay		ipsecah_params[2].ipsecah_param_value
1023448Sdh155122 #define	ipsecah_replay_size		ipsecah_params[3].ipsecah_param_value
1033448Sdh155122 #define	ipsecah_acquire_timeout		ipsecah_params[4].ipsecah_param_value
1043448Sdh155122 #define	ipsecah_larval_timeout		ipsecah_params[5].ipsecah_param_value
1053448Sdh155122 #define	ipsecah_default_soft_bytes	ipsecah_params[6].ipsecah_param_value
1063448Sdh155122 #define	ipsecah_default_hard_bytes	ipsecah_params[7].ipsecah_param_value
1073448Sdh155122 #define	ipsecah_default_soft_addtime	ipsecah_params[8].ipsecah_param_value
1083448Sdh155122 #define	ipsecah_default_hard_addtime	ipsecah_params[9].ipsecah_param_value
1093448Sdh155122 #define	ipsecah_default_soft_usetime	ipsecah_params[10].ipsecah_param_value
1103448Sdh155122 #define	ipsecah_default_hard_usetime	ipsecah_params[11].ipsecah_param_value
1113448Sdh155122 #define	ipsecah_log_unknown_spi		ipsecah_params[12].ipsecah_param_value
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #define	ah0dbg(a)	printf a
1140Sstevel@tonic-gate /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
1153448Sdh155122 #define	ah1dbg(ahstack, a)	if (ahstack->ipsecah_debug != 0) printf a
1163448Sdh155122 #define	ah2dbg(ahstack, a)	if (ahstack->ipsecah_debug > 1) printf a
1173448Sdh155122 #define	ah3dbg(ahstack, a)	if (ahstack->ipsecah_debug > 2) printf a
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * XXX This is broken. Padding should be determined dynamically
1210Sstevel@tonic-gate  * depending on the ICV size and IP version number so that the
1220Sstevel@tonic-gate  * total AH header size is a multiple of 32 bits or 64 bits
1230Sstevel@tonic-gate  * for V4 and V6 respectively. For 96bit ICVs we have no problems.
1240Sstevel@tonic-gate  * Anything different from that, we need to fix our code.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate #define	IPV4_PADDING_ALIGN	0x04	/* Multiple of 32 bits */
1270Sstevel@tonic-gate #define	IPV6_PADDING_ALIGN	0x04	/* Multiple of 32 bits */
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * Helper macro. Avoids a call to msgdsize if there is only one
1310Sstevel@tonic-gate  * mblk in the chain.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate #define	AH_MSGSIZE(mp) ((mp)->b_cont != NULL ? msgdsize(mp) : MBLKL(mp))
1340Sstevel@tonic-gate 
1353448Sdh155122 
13611042SErik.Nordmark@Sun.COM static mblk_t *ah_auth_out_done(mblk_t *, ip_xmit_attr_t *, ipsec_crypto_t *);
13711042SErik.Nordmark@Sun.COM static mblk_t *ah_auth_in_done(mblk_t *, ip_recv_attr_t *, ipsec_crypto_t *);
1380Sstevel@tonic-gate static mblk_t *ah_process_ip_options_v4(mblk_t *, ipsa_t *, int *, uint_t,
1393448Sdh155122     boolean_t, ipsecah_stack_t *);
1400Sstevel@tonic-gate static mblk_t *ah_process_ip_options_v6(mblk_t *, ipsa_t *, int *, uint_t,
1413448Sdh155122     boolean_t, ipsecah_stack_t *);
1423448Sdh155122 static void ah_getspi(mblk_t *, keysock_in_t *, ipsecah_stack_t *);
14311042SErik.Nordmark@Sun.COM static void ah_inbound_restart(mblk_t *, ip_recv_attr_t *);
14411042SErik.Nordmark@Sun.COM 
14511042SErik.Nordmark@Sun.COM static mblk_t *ah_outbound(mblk_t *, ip_xmit_attr_t *);
14611042SErik.Nordmark@Sun.COM static void ah_outbound_finish(mblk_t *, ip_xmit_attr_t *);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate static int ipsecah_open(queue_t *, dev_t *, int, int, cred_t *);
1490Sstevel@tonic-gate static int ipsecah_close(queue_t *);
1500Sstevel@tonic-gate static void ipsecah_wput(queue_t *, mblk_t *);
1513448Sdh155122 static void ah_send_acquire(ipsacq_t *, mblk_t *, netstack_t *);
15210934Ssommerfeld@sun.com static boolean_t ah_register_out(uint32_t, uint32_t, uint_t, ipsecah_stack_t *,
15311042SErik.Nordmark@Sun.COM     cred_t *);
1543448Sdh155122 static void	*ipsecah_stack_init(netstackid_t stackid, netstack_t *ns);
1553448Sdh155122 static void	ipsecah_stack_fini(netstackid_t stackid, void *arg);
1563448Sdh155122 
1573448Sdh155122 /* Setable in /etc/system */
1583448Sdh155122 uint32_t ah_hash_size = IPSEC_DEFAULT_HASH_SIZE;
1593448Sdh155122 
1603448Sdh155122 static taskq_t *ah_taskq;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate static struct module_info info = {
1630Sstevel@tonic-gate 	5136, "ipsecah", 0, INFPSZ, 65536, 1024
1640Sstevel@tonic-gate };
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static struct qinit rinit = {
16711042SErik.Nordmark@Sun.COM 	(pfi_t)putnext, NULL, ipsecah_open, ipsecah_close, NULL, &info,
1680Sstevel@tonic-gate 	NULL
1690Sstevel@tonic-gate };
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate static struct qinit winit = {
1720Sstevel@tonic-gate 	(pfi_t)ipsecah_wput, NULL, ipsecah_open, ipsecah_close, NULL, &info,
1730Sstevel@tonic-gate 	NULL
1740Sstevel@tonic-gate };
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate struct streamtab ipsecahinfo = {
1770Sstevel@tonic-gate 	&rinit, &winit, NULL, NULL
1780Sstevel@tonic-gate };
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static int ah_kstat_update(kstat_t *, int);
1810Sstevel@tonic-gate 
1821659Smarkfen uint64_t ipsacq_maxpackets = IPSACQ_MAXPACKETS;
1831659Smarkfen 
1840Sstevel@tonic-gate static boolean_t
ah_kstat_init(ipsecah_stack_t * ahstack,netstackid_t stackid)1853448Sdh155122 ah_kstat_init(ipsecah_stack_t *ahstack, netstackid_t stackid)
1860Sstevel@tonic-gate {
1873448Sdh155122 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
1883448Sdh155122 
1893448Sdh155122 	ahstack->ah_ksp = kstat_create_netstack("ipsecah", 0, "ah_stat", "net",
1903448Sdh155122 	    KSTAT_TYPE_NAMED, sizeof (ah_kstats_t) / sizeof (kstat_named_t),
1913448Sdh155122 	    KSTAT_FLAG_PERSISTENT, stackid);
1923448Sdh155122 
1933448Sdh155122 	if (ahstack->ah_ksp == NULL || ahstack->ah_ksp->ks_data == NULL)
1940Sstevel@tonic-gate 		return (B_FALSE);
1950Sstevel@tonic-gate 
1963448Sdh155122 	ahstack->ah_kstats = ahstack->ah_ksp->ks_data;
1973448Sdh155122 
1983448Sdh155122 	ahstack->ah_ksp->ks_update = ah_kstat_update;
1993448Sdh155122 	ahstack->ah_ksp->ks_private = (void *)(uintptr_t)stackid;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate #define	K64 KSTAT_DATA_UINT64
2023448Sdh155122 #define	KI(x) kstat_named_init(&(ahstack->ah_kstats->ah_stat_##x), #x, K64)
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	KI(num_aalgs);
2050Sstevel@tonic-gate 	KI(good_auth);
2060Sstevel@tonic-gate 	KI(bad_auth);
2070Sstevel@tonic-gate 	KI(replay_failures);
2080Sstevel@tonic-gate 	KI(replay_early_failures);
2090Sstevel@tonic-gate 	KI(keysock_in);
2100Sstevel@tonic-gate 	KI(out_requests);
2110Sstevel@tonic-gate 	KI(acquire_requests);
2120Sstevel@tonic-gate 	KI(bytes_expired);
2130Sstevel@tonic-gate 	KI(out_discards);
2140Sstevel@tonic-gate 	KI(crypto_sync);
2150Sstevel@tonic-gate 	KI(crypto_async);
2160Sstevel@tonic-gate 	KI(crypto_failures);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate #undef KI
2190Sstevel@tonic-gate #undef K64
2200Sstevel@tonic-gate 
2213448Sdh155122 	kstat_install(ahstack->ah_ksp);
2223448Sdh155122 	IP_ACQUIRE_STAT(ipss, maxpackets, ipsacq_maxpackets);
2230Sstevel@tonic-gate 	return (B_TRUE);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate static int
ah_kstat_update(kstat_t * kp,int rw)2270Sstevel@tonic-gate ah_kstat_update(kstat_t *kp, int rw)
2280Sstevel@tonic-gate {
2293448Sdh155122 	ah_kstats_t	*ekp;
2303448Sdh155122 	netstackid_t	stackid = (netstackid_t)(uintptr_t)kp->ks_private;
2313448Sdh155122 	netstack_t	*ns;
2323448Sdh155122 	ipsec_stack_t	*ipss;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if ((kp == NULL) || (kp->ks_data == NULL))
2350Sstevel@tonic-gate 		return (EIO);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
2380Sstevel@tonic-gate 		return (EACCES);
2390Sstevel@tonic-gate 
2403448Sdh155122 	ns = netstack_find_by_stackid(stackid);
2413448Sdh155122 	if (ns == NULL)
2423448Sdh155122 		return (-1);
2433448Sdh155122 	ipss = ns->netstack_ipsec;
2443448Sdh155122 	if (ipss == NULL) {
2453448Sdh155122 		netstack_rele(ns);
2463448Sdh155122 		return (-1);
2473448Sdh155122 	}
2480Sstevel@tonic-gate 	ekp = (ah_kstats_t *)kp->ks_data;
2493448Sdh155122 
2503448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
2513448Sdh155122 	ekp->ah_stat_num_aalgs.value.ui64 = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
2523448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
2533448Sdh155122 
2543448Sdh155122 	netstack_rele(ns);
2550Sstevel@tonic-gate 	return (0);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate  * Don't have to lock ipsec_age_interval, as only one thread will access it at
2600Sstevel@tonic-gate  * a time, because I control the one function that does a qtimeout() on
2610Sstevel@tonic-gate  * ah_pfkey_q.
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate static void
ah_ager(void * arg)2643448Sdh155122 ah_ager(void *arg)
2650Sstevel@tonic-gate {
2663448Sdh155122 	ipsecah_stack_t *ahstack = (ipsecah_stack_t *)arg;
2673448Sdh155122 	netstack_t	*ns = ahstack->ipsecah_netstack;
2680Sstevel@tonic-gate 	hrtime_t begin = gethrtime();
2690Sstevel@tonic-gate 
2703448Sdh155122 	sadb_ager(&ahstack->ah_sadb.s_v4, ahstack->ah_pfkey_q,
27111042SErik.Nordmark@Sun.COM 	    ahstack->ipsecah_reap_delay, ns);
2723448Sdh155122 	sadb_ager(&ahstack->ah_sadb.s_v6, ahstack->ah_pfkey_q,
27311042SErik.Nordmark@Sun.COM 	    ahstack->ipsecah_reap_delay, ns);
2743448Sdh155122 
2753448Sdh155122 	ahstack->ah_event = sadb_retimeout(begin, ahstack->ah_pfkey_q,
2763448Sdh155122 	    ah_ager, ahstack,
2773448Sdh155122 	    &ahstack->ipsecah_age_interval, ahstack->ipsecah_age_int_max,
2783448Sdh155122 	    info.mi_idnum);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate  * Get an AH NDD parameter.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate /* ARGSUSED */
2850Sstevel@tonic-gate static int
ipsecah_param_get(q,mp,cp,cr)2860Sstevel@tonic-gate ipsecah_param_get(q, mp, cp, cr)
2870Sstevel@tonic-gate 	queue_t	*q;
2880Sstevel@tonic-gate 	mblk_t	*mp;
2890Sstevel@tonic-gate 	caddr_t	cp;
2900Sstevel@tonic-gate 	cred_t *cr;
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	ipsecahparam_t	*ipsecahpa = (ipsecahparam_t *)cp;
2930Sstevel@tonic-gate 	uint_t value;
2943448Sdh155122 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
2953448Sdh155122 
2963448Sdh155122 	mutex_enter(&ahstack->ipsecah_param_lock);
2970Sstevel@tonic-gate 	value = ipsecahpa->ipsecah_param_value;
2983448Sdh155122 	mutex_exit(&ahstack->ipsecah_param_lock);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
3010Sstevel@tonic-gate 	return (0);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate  * This routine sets an NDD variable in a ipsecahparam_t structure.
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate /* ARGSUSED */
3080Sstevel@tonic-gate static int
ipsecah_param_set(q,mp,value,cp,cr)3090Sstevel@tonic-gate ipsecah_param_set(q, mp, value, cp, cr)
3100Sstevel@tonic-gate 	queue_t	*q;
3110Sstevel@tonic-gate 	mblk_t	*mp;
3120Sstevel@tonic-gate 	char	*value;
3130Sstevel@tonic-gate 	caddr_t	cp;
3140Sstevel@tonic-gate 	cred_t *cr;
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	ulong_t	new_value;
3170Sstevel@tonic-gate 	ipsecahparam_t	*ipsecahpa = (ipsecahparam_t *)cp;
3183448Sdh155122 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
3220Sstevel@tonic-gate 	 * required bounds.
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
3250Sstevel@tonic-gate 	    new_value < ipsecahpa->ipsecah_param_min ||
3260Sstevel@tonic-gate 	    new_value > ipsecahpa->ipsecah_param_max) {
3270Sstevel@tonic-gate 		    return (EINVAL);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/* Set the new value */
3313448Sdh155122 	mutex_enter(&ahstack->ipsecah_param_lock);
3320Sstevel@tonic-gate 	ipsecahpa->ipsecah_param_value = new_value;
3333448Sdh155122 	mutex_exit(&ahstack->ipsecah_param_lock);
3340Sstevel@tonic-gate 	return (0);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * Using lifetime NDD variables, fill in an extended combination's
3390Sstevel@tonic-gate  * lifetime information.
3400Sstevel@tonic-gate  */
3410Sstevel@tonic-gate void
ipsecah_fill_defs(sadb_x_ecomb_t * ecomb,netstack_t * ns)3423448Sdh155122 ipsecah_fill_defs(sadb_x_ecomb_t *ecomb, netstack_t *ns)
3430Sstevel@tonic-gate {
3443448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
3453448Sdh155122 
3463448Sdh155122 	ecomb->sadb_x_ecomb_soft_bytes = ahstack->ipsecah_default_soft_bytes;
3473448Sdh155122 	ecomb->sadb_x_ecomb_hard_bytes = ahstack->ipsecah_default_hard_bytes;
3483448Sdh155122 	ecomb->sadb_x_ecomb_soft_addtime =
3493448Sdh155122 	    ahstack->ipsecah_default_soft_addtime;
3503448Sdh155122 	ecomb->sadb_x_ecomb_hard_addtime =
3513448Sdh155122 	    ahstack->ipsecah_default_hard_addtime;
3523448Sdh155122 	ecomb->sadb_x_ecomb_soft_usetime =
3533448Sdh155122 	    ahstack->ipsecah_default_soft_usetime;
3543448Sdh155122 	ecomb->sadb_x_ecomb_hard_usetime =
3553448Sdh155122 	    ahstack->ipsecah_default_hard_usetime;
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * Initialize things for AH at module load time.
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate boolean_t
ipsecah_ddi_init(void)3620Sstevel@tonic-gate ipsecah_ddi_init(void)
3630Sstevel@tonic-gate {
3643448Sdh155122 	ah_taskq = taskq_create("ah_taskq", 1, minclsyspri,
3653448Sdh155122 	    IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0);
3663448Sdh155122 
3673448Sdh155122 	/*
3683448Sdh155122 	 * We want to be informed each time a stack is created or
3693448Sdh155122 	 * destroyed in the kernel, so we can maintain the
3703448Sdh155122 	 * set of ipsecah_stack_t's.
3713448Sdh155122 	 */
3723448Sdh155122 	netstack_register(NS_IPSECAH, ipsecah_stack_init, NULL,
3733448Sdh155122 	    ipsecah_stack_fini);
3743448Sdh155122 
3753448Sdh155122 	return (B_TRUE);
3763448Sdh155122 }
3773448Sdh155122 
3783448Sdh155122 /*
3793448Sdh155122  * Walk through the param array specified registering each element with the
3803448Sdh155122  * named dispatch handler.
3813448Sdh155122  */
3823448Sdh155122 static boolean_t
ipsecah_param_register(IDP * ndp,ipsecahparam_t * ahp,int cnt)3833448Sdh155122 ipsecah_param_register(IDP *ndp, ipsecahparam_t *ahp, int cnt)
3843448Sdh155122 {
3853448Sdh155122 	for (; cnt-- > 0; ahp++) {
3860Sstevel@tonic-gate 		if (ahp->ipsecah_param_name != NULL &&
3870Sstevel@tonic-gate 		    ahp->ipsecah_param_name[0]) {
3883448Sdh155122 			if (!nd_load(ndp,
3893448Sdh155122 			    ahp->ipsecah_param_name,
3900Sstevel@tonic-gate 			    ipsecah_param_get, ipsecah_param_set,
3910Sstevel@tonic-gate 			    (caddr_t)ahp)) {
3923448Sdh155122 				nd_free(ndp);
3930Sstevel@tonic-gate 				return (B_FALSE);
3940Sstevel@tonic-gate 			}
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	return (B_TRUE);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /*
4013448Sdh155122  * Initialize things for AH for each stack instance
4023448Sdh155122  */
4033448Sdh155122 static void *
ipsecah_stack_init(netstackid_t stackid,netstack_t * ns)4043448Sdh155122 ipsecah_stack_init(netstackid_t stackid, netstack_t *ns)
4053448Sdh155122 {
4063448Sdh155122 	ipsecah_stack_t	*ahstack;
4073448Sdh155122 	ipsecahparam_t	*ahp;
4083448Sdh155122 
4093448Sdh155122 	ahstack = (ipsecah_stack_t *)kmem_zalloc(sizeof (*ahstack), KM_SLEEP);
4103448Sdh155122 	ahstack->ipsecah_netstack = ns;
4113448Sdh155122 
4123448Sdh155122 	ahp = (ipsecahparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
4133448Sdh155122 	ahstack->ipsecah_params = ahp;
4143448Sdh155122 	bcopy(lcl_param_arr, ahp, sizeof (lcl_param_arr));
4153448Sdh155122 
4163448Sdh155122 	(void) ipsecah_param_register(&ahstack->ipsecah_g_nd, ahp,
4173448Sdh155122 	    A_CNT(lcl_param_arr));
4183448Sdh155122 
4193448Sdh155122 	(void) ah_kstat_init(ahstack, stackid);
4203448Sdh155122 
4213448Sdh155122 	ahstack->ah_sadb.s_acquire_timeout = &ahstack->ipsecah_acquire_timeout;
4223448Sdh155122 	ahstack->ah_sadb.s_acqfn = ah_send_acquire;
4233448Sdh155122 	sadbp_init("AH", &ahstack->ah_sadb, SADB_SATYPE_AH, ah_hash_size,
4243448Sdh155122 	    ahstack->ipsecah_netstack);
4253448Sdh155122 
4263448Sdh155122 	mutex_init(&ahstack->ipsecah_param_lock, NULL, MUTEX_DEFAULT, 0);
4273448Sdh155122 
4283448Sdh155122 	ip_drop_register(&ahstack->ah_dropper, "IPsec AH");
4293448Sdh155122 	return (ahstack);
4303448Sdh155122 }
4313448Sdh155122 
4323448Sdh155122 /*
4330Sstevel@tonic-gate  * Destroy things for AH at module unload time.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate void
ipsecah_ddi_destroy(void)4360Sstevel@tonic-gate ipsecah_ddi_destroy(void)
4370Sstevel@tonic-gate {
4383448Sdh155122 	netstack_unregister(NS_IPSECAH);
4390Sstevel@tonic-gate 	taskq_destroy(ah_taskq);
4403448Sdh155122 }
4413448Sdh155122 
4423448Sdh155122 /*
4433448Sdh155122  * Destroy things for AH for one stack... Never called?
4443448Sdh155122  */
4453448Sdh155122 static void
ipsecah_stack_fini(netstackid_t stackid,void * arg)4463448Sdh155122 ipsecah_stack_fini(netstackid_t stackid, void *arg)
4473448Sdh155122 {
4483448Sdh155122 	ipsecah_stack_t *ahstack = (ipsecah_stack_t *)arg;
4493448Sdh155122 
4503448Sdh155122 	if (ahstack->ah_pfkey_q != NULL) {
4513448Sdh155122 		(void) quntimeout(ahstack->ah_pfkey_q, ahstack->ah_event);
4523448Sdh155122 	}
4533448Sdh155122 	ahstack->ah_sadb.s_acqfn = NULL;
4543448Sdh155122 	ahstack->ah_sadb.s_acquire_timeout = NULL;
4553448Sdh155122 	sadbp_destroy(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
4563448Sdh155122 	ip_drop_unregister(&ahstack->ah_dropper);
4573448Sdh155122 	mutex_destroy(&ahstack->ipsecah_param_lock);
4583448Sdh155122 	nd_free(&ahstack->ipsecah_g_nd);
4593448Sdh155122 
4603448Sdh155122 	kmem_free(ahstack->ipsecah_params, sizeof (lcl_param_arr));
4613448Sdh155122 	ahstack->ipsecah_params = NULL;
4623448Sdh155122 	kstat_delete_netstack(ahstack->ah_ksp, stackid);
4633448Sdh155122 	ahstack->ah_ksp = NULL;
4643448Sdh155122 	ahstack->ah_kstats = NULL;
4653448Sdh155122 
4663448Sdh155122 	kmem_free(ahstack, sizeof (*ahstack));
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate /*
47011042SErik.Nordmark@Sun.COM  * AH module open routine, which is here for keysock plumbing.
47111042SErik.Nordmark@Sun.COM  * Keysock is pushed over {AH,ESP} which is an artifact from the Bad Old
47211042SErik.Nordmark@Sun.COM  * Days of export control, and fears that ESP would not be allowed
47311042SErik.Nordmark@Sun.COM  * to be shipped at all by default.  Eventually, keysock should
47411042SErik.Nordmark@Sun.COM  * either access AH and ESP via modstubs or krtld dependencies, or
47511042SErik.Nordmark@Sun.COM  * perhaps be folded in with AH and ESP into a single IPsec/netsec
47611042SErik.Nordmark@Sun.COM  * module ("netsec" if PF_KEY provides more than AH/ESP keying tables).
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate /* ARGSUSED */
4790Sstevel@tonic-gate static int
ipsecah_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)4800Sstevel@tonic-gate ipsecah_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
4810Sstevel@tonic-gate {
4823448Sdh155122 	netstack_t	*ns;
4833448Sdh155122 	ipsecah_stack_t	*ahstack;
4843448Sdh155122 
4857118Ssommerfe 	if (secpolicy_ip_config(credp, B_FALSE) != 0)
4860Sstevel@tonic-gate 		return (EPERM);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (q->q_ptr != NULL)
4890Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (sflag != MODOPEN)
4920Sstevel@tonic-gate 		return (EINVAL);
4930Sstevel@tonic-gate 
4943448Sdh155122 	ns = netstack_find_by_cred(credp);
4953448Sdh155122 	ASSERT(ns != NULL);
4963448Sdh155122 	ahstack = ns->netstack_ipsecah;
4973448Sdh155122 	ASSERT(ahstack != NULL);
4983448Sdh155122 
4993448Sdh155122 	q->q_ptr = ahstack;
5003448Sdh155122 	WR(q)->q_ptr = q->q_ptr;
5013448Sdh155122 
50211042SErik.Nordmark@Sun.COM 	qprocson(q);
5030Sstevel@tonic-gate 	return (0);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate  * AH module close routine.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static int
ipsecah_close(queue_t * q)5100Sstevel@tonic-gate ipsecah_close(queue_t *q)
5110Sstevel@tonic-gate {
5123448Sdh155122 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
5133448Sdh155122 
5140Sstevel@tonic-gate 	/*
5150Sstevel@tonic-gate 	 * Clean up q_ptr, if needed.
5160Sstevel@tonic-gate 	 */
5170Sstevel@tonic-gate 	qprocsoff(q);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	/* Keysock queue check is safe, because of OCEXCL perimeter. */
5200Sstevel@tonic-gate 
5213448Sdh155122 	if (q == ahstack->ah_pfkey_q) {
5223448Sdh155122 		ah1dbg(ahstack,
5233448Sdh155122 		    ("ipsecah_close:  Ummm... keysock is closing AH.\n"));
5243448Sdh155122 		ahstack->ah_pfkey_q = NULL;
5250Sstevel@tonic-gate 		/* Detach qtimeouts. */
5263448Sdh155122 		(void) quntimeout(q, ahstack->ah_event);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 
5293448Sdh155122 	netstack_rele(ahstack->ipsecah_netstack);
5300Sstevel@tonic-gate 	return (0);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * Construct an SADB_REGISTER message with the current algorithms.
5350Sstevel@tonic-gate  */
5360Sstevel@tonic-gate static boolean_t
ah_register_out(uint32_t sequence,uint32_t pid,uint_t serial,ipsecah_stack_t * ahstack,cred_t * cr)5373448Sdh155122 ah_register_out(uint32_t sequence, uint32_t pid, uint_t serial,
53811042SErik.Nordmark@Sun.COM     ipsecah_stack_t *ahstack, cred_t *cr)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	mblk_t *mp;
5410Sstevel@tonic-gate 	boolean_t rc = B_TRUE;
5420Sstevel@tonic-gate 	sadb_msg_t *samsg;
5430Sstevel@tonic-gate 	sadb_supported_t *sasupp;
5440Sstevel@tonic-gate 	sadb_alg_t *saalg;
5450Sstevel@tonic-gate 	uint_t allocsize = sizeof (*samsg);
5460Sstevel@tonic-gate 	uint_t i, numalgs_snap;
5470Sstevel@tonic-gate 	ipsec_alginfo_t **authalgs;
5480Sstevel@tonic-gate 	uint_t num_aalgs;
5493448Sdh155122 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
55010934Ssommerfeld@sun.com 	sadb_sens_t *sens;
55110934Ssommerfeld@sun.com 	size_t sens_len = 0;
55210934Ssommerfeld@sun.com 	sadb_ext_t *nextext;
55311042SErik.Nordmark@Sun.COM 	ts_label_t *sens_tsl = NULL;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/* Allocate the KEYSOCK_OUT. */
5560Sstevel@tonic-gate 	mp = sadb_keysock_out(serial);
5570Sstevel@tonic-gate 	if (mp == NULL) {
5580Sstevel@tonic-gate 		ah0dbg(("ah_register_out: couldn't allocate mblk.\n"));
5590Sstevel@tonic-gate 		return (B_FALSE);
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
56211042SErik.Nordmark@Sun.COM 	if (is_system_labeled() && (cr != NULL)) {
56311042SErik.Nordmark@Sun.COM 		sens_tsl = crgetlabel(cr);
56411042SErik.Nordmark@Sun.COM 		if (sens_tsl != NULL) {
56511042SErik.Nordmark@Sun.COM 			sens_len = sadb_sens_len_from_label(sens_tsl);
56610934Ssommerfeld@sun.com 			allocsize += sens_len;
56710934Ssommerfeld@sun.com 		}
56810934Ssommerfeld@sun.com 	}
56910934Ssommerfeld@sun.com 
5700Sstevel@tonic-gate 	/*
5710Sstevel@tonic-gate 	 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
5720Sstevel@tonic-gate 	 * The alg reader lock needs to be held while allocating
5730Sstevel@tonic-gate 	 * the variable part (i.e. the algorithms) of the message.
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 
5763448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	/*
5790Sstevel@tonic-gate 	 * Return only valid algorithms, so the number of algorithms
5800Sstevel@tonic-gate 	 * to send up may be less than the number of algorithm entries
5810Sstevel@tonic-gate 	 * in the table.
5820Sstevel@tonic-gate 	 */
5833448Sdh155122 	authalgs = ipss->ipsec_alglists[IPSEC_ALG_AUTH];
5840Sstevel@tonic-gate 	for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
5850Sstevel@tonic-gate 		if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
5860Sstevel@tonic-gate 			num_aalgs++;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/*
5890Sstevel@tonic-gate 	 * Fill SADB_REGISTER message's algorithm descriptors.  Hold
5900Sstevel@tonic-gate 	 * down the lock while filling it.
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	if (num_aalgs != 0) {
5930Sstevel@tonic-gate 		allocsize += (num_aalgs * sizeof (*saalg));
5940Sstevel@tonic-gate 		allocsize += sizeof (*sasupp);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	mp->b_cont = allocb(allocsize, BPRI_HI);
5970Sstevel@tonic-gate 	if (mp->b_cont == NULL) {
5983448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
5990Sstevel@tonic-gate 		freemsg(mp);
6000Sstevel@tonic-gate 		return (B_FALSE);
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	mp->b_cont->b_wptr += allocsize;
60410934Ssommerfeld@sun.com 	nextext = (sadb_ext_t *)(mp->b_cont->b_rptr + sizeof (*samsg));
60510934Ssommerfeld@sun.com 
6060Sstevel@tonic-gate 	if (num_aalgs != 0) {
6070Sstevel@tonic-gate 
60810934Ssommerfeld@sun.com 		saalg = (sadb_alg_t *)(((uint8_t *)nextext) + sizeof (*sasupp));
6090Sstevel@tonic-gate 		ASSERT(((ulong_t)saalg & 0x7) == 0);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		numalgs_snap = 0;
6120Sstevel@tonic-gate 		for (i = 0;
6133448Sdh155122 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs));
6143448Sdh155122 		    i++) {
6150Sstevel@tonic-gate 			if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
6160Sstevel@tonic-gate 				continue;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 			saalg->sadb_alg_id = authalgs[i]->alg_id;
6190Sstevel@tonic-gate 			saalg->sadb_alg_ivlen = 0;
6200Sstevel@tonic-gate 			saalg->sadb_alg_minbits = authalgs[i]->alg_ef_minbits;
6210Sstevel@tonic-gate 			saalg->sadb_alg_maxbits = authalgs[i]->alg_ef_maxbits;
6220Sstevel@tonic-gate 			saalg->sadb_x_alg_increment =
6230Sstevel@tonic-gate 			    authalgs[i]->alg_increment;
62410824SMark.Fenwick@Sun.COM 			/* For now, salt is meaningless in AH. */
62510824SMark.Fenwick@Sun.COM 			ASSERT(authalgs[i]->alg_saltlen == 0);
62610824SMark.Fenwick@Sun.COM 			saalg->sadb_x_alg_saltbits =
62710824SMark.Fenwick@Sun.COM 			    SADB_8TO1(authalgs[i]->alg_saltlen);
6280Sstevel@tonic-gate 			numalgs_snap++;
6290Sstevel@tonic-gate 			saalg++;
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 		ASSERT(numalgs_snap == num_aalgs);
6320Sstevel@tonic-gate #ifdef DEBUG
6330Sstevel@tonic-gate 		/*
6340Sstevel@tonic-gate 		 * Reality check to make sure I snagged all of the
6350Sstevel@tonic-gate 		 * algorithms.
6360Sstevel@tonic-gate 		 */
6370Sstevel@tonic-gate 		for (; i < IPSEC_MAX_ALGS; i++)
6380Sstevel@tonic-gate 			if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
6390Sstevel@tonic-gate 				cmn_err(CE_PANIC,
6400Sstevel@tonic-gate 				    "ah_register_out()!  Missed #%d.\n", i);
6410Sstevel@tonic-gate #endif /* DEBUG */
64210934Ssommerfeld@sun.com 		nextext = (sadb_ext_t *)saalg;
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6453448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
6460Sstevel@tonic-gate 
64711042SErik.Nordmark@Sun.COM 	if (sens_tsl != NULL) {
64810934Ssommerfeld@sun.com 		sens = (sadb_sens_t *)nextext;
64911042SErik.Nordmark@Sun.COM 		sadb_sens_from_label(sens, SADB_EXT_SENSITIVITY,
65011042SErik.Nordmark@Sun.COM 		    sens_tsl, sens_len);
65110934Ssommerfeld@sun.com 
65210934Ssommerfeld@sun.com 		nextext = (sadb_ext_t *)(((uint8_t *)sens) + sens_len);
65310934Ssommerfeld@sun.com 	}
65410934Ssommerfeld@sun.com 
6550Sstevel@tonic-gate 	/* Now fill the restof the SADB_REGISTER message. */
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
6580Sstevel@tonic-gate 	samsg->sadb_msg_version = PF_KEY_V2;
6590Sstevel@tonic-gate 	samsg->sadb_msg_type = SADB_REGISTER;
6600Sstevel@tonic-gate 	samsg->sadb_msg_errno = 0;
6610Sstevel@tonic-gate 	samsg->sadb_msg_satype = SADB_SATYPE_AH;
6620Sstevel@tonic-gate 	samsg->sadb_msg_len = SADB_8TO64(allocsize);
6630Sstevel@tonic-gate 	samsg->sadb_msg_reserved = 0;
6640Sstevel@tonic-gate 	/*
6650Sstevel@tonic-gate 	 * Assume caller has sufficient sequence/pid number info.  If it's one
6660Sstevel@tonic-gate 	 * from me over a new alg., I could give two hoots about sequence.
6670Sstevel@tonic-gate 	 */
6680Sstevel@tonic-gate 	samsg->sadb_msg_seq = sequence;
6690Sstevel@tonic-gate 	samsg->sadb_msg_pid = pid;
6700Sstevel@tonic-gate 
67110934Ssommerfeld@sun.com 	if (num_aalgs != 0) {
6720Sstevel@tonic-gate 		sasupp = (sadb_supported_t *)(samsg + 1);
67310934Ssommerfeld@sun.com 		sasupp->sadb_supported_len = SADB_8TO64(
67410934Ssommerfeld@sun.com 		    sizeof (*sasupp) + sizeof (*saalg) * num_aalgs);
6750Sstevel@tonic-gate 		sasupp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6760Sstevel@tonic-gate 		sasupp->sadb_supported_reserved = 0;
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6793448Sdh155122 	if (ahstack->ah_pfkey_q != NULL)
6803448Sdh155122 		putnext(ahstack->ah_pfkey_q, mp);
6810Sstevel@tonic-gate 	else {
6820Sstevel@tonic-gate 		rc = B_FALSE;
6830Sstevel@tonic-gate 		freemsg(mp);
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	return (rc);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate /*
6900Sstevel@tonic-gate  * Invoked when the algorithm table changes. Causes SADB_REGISTER
6910Sstevel@tonic-gate  * messages continaining the current list of algorithms to be
6920Sstevel@tonic-gate  * sent up to the AH listeners.
6930Sstevel@tonic-gate  */
6940Sstevel@tonic-gate void
ipsecah_algs_changed(netstack_t * ns)6953448Sdh155122 ipsecah_algs_changed(netstack_t *ns)
6960Sstevel@tonic-gate {
6973448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
6983448Sdh155122 
6990Sstevel@tonic-gate 	/*
7000Sstevel@tonic-gate 	 * Time to send a PF_KEY SADB_REGISTER message to AH listeners
7010Sstevel@tonic-gate 	 * everywhere.  (The function itself checks for NULL ah_pfkey_q.)
7020Sstevel@tonic-gate 	 */
70310934Ssommerfeld@sun.com 	(void) ah_register_out(0, 0, 0, ahstack, NULL);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate /*
7070Sstevel@tonic-gate  * Stub function that taskq_dispatch() invokes to take the mblk (in arg)
70811042SErik.Nordmark@Sun.COM  * and send it into AH and IP again.
7090Sstevel@tonic-gate  */
7100Sstevel@tonic-gate static void
inbound_task(void * arg)7110Sstevel@tonic-gate inbound_task(void *arg)
7120Sstevel@tonic-gate {
71311042SErik.Nordmark@Sun.COM 	mblk_t		*mp = (mblk_t *)arg;
71411042SErik.Nordmark@Sun.COM 	mblk_t		*async_mp;
71511042SErik.Nordmark@Sun.COM 	ip_recv_attr_t	iras;
71611042SErik.Nordmark@Sun.COM 
71711042SErik.Nordmark@Sun.COM 	async_mp = mp;
71811042SErik.Nordmark@Sun.COM 	mp = async_mp->b_cont;
71911042SErik.Nordmark@Sun.COM 	async_mp->b_cont = NULL;
72011042SErik.Nordmark@Sun.COM 	if (!ip_recv_attr_from_mblk(async_mp, &iras)) {
72111042SErik.Nordmark@Sun.COM 		/* The ill or ip_stack_t disappeared on us */
72211042SErik.Nordmark@Sun.COM 		ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
72311042SErik.Nordmark@Sun.COM 		freemsg(mp);
72411042SErik.Nordmark@Sun.COM 		goto done;
72511042SErik.Nordmark@Sun.COM 	}
72611042SErik.Nordmark@Sun.COM 
72711042SErik.Nordmark@Sun.COM 	ah_inbound_restart(mp, &iras);
72811042SErik.Nordmark@Sun.COM done:
72911042SErik.Nordmark@Sun.COM 	ira_cleanup(&iras, B_TRUE);
73011042SErik.Nordmark@Sun.COM }
73111042SErik.Nordmark@Sun.COM 
73211042SErik.Nordmark@Sun.COM /*
73311042SErik.Nordmark@Sun.COM  * Restart ESP after the SA has been added.
73411042SErik.Nordmark@Sun.COM  */
73511042SErik.Nordmark@Sun.COM static void
ah_inbound_restart(mblk_t * mp,ip_recv_attr_t * ira)73611042SErik.Nordmark@Sun.COM ah_inbound_restart(mblk_t *mp, ip_recv_attr_t *ira)
73711042SErik.Nordmark@Sun.COM {
73811042SErik.Nordmark@Sun.COM 	ah_t		*ah;
73911042SErik.Nordmark@Sun.COM 	netstack_t	*ns;
7408704Sdanmcd@sun.com 	ipsecah_stack_t	*ahstack;
7418704Sdanmcd@sun.com 
74211042SErik.Nordmark@Sun.COM 	ns = ira->ira_ill->ill_ipst->ips_netstack;
74311042SErik.Nordmark@Sun.COM 	ahstack = ns->netstack_ipsecah;
74411042SErik.Nordmark@Sun.COM 
74511042SErik.Nordmark@Sun.COM 	ASSERT(ahstack != NULL);
74611042SErik.Nordmark@Sun.COM 	mp = ipsec_inbound_ah_sa(mp, ira, &ah);
74711042SErik.Nordmark@Sun.COM 	if (mp == NULL)
74811042SErik.Nordmark@Sun.COM 		return;
74911042SErik.Nordmark@Sun.COM 
75011042SErik.Nordmark@Sun.COM 	ASSERT(ah != NULL);
75111042SErik.Nordmark@Sun.COM 	ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
75211042SErik.Nordmark@Sun.COM 	ASSERT(ira->ira_ipsec_ah_sa != NULL);
75311042SErik.Nordmark@Sun.COM 
75411042SErik.Nordmark@Sun.COM 	mp = ira->ira_ipsec_ah_sa->ipsa_input_func(mp, ah, ira);
75511042SErik.Nordmark@Sun.COM 	if (mp == NULL) {
75611042SErik.Nordmark@Sun.COM 		/*
75711042SErik.Nordmark@Sun.COM 		 * Either it failed or is pending. In the former case
75811042SErik.Nordmark@Sun.COM 		 * ipIfStatsInDiscards was increased.
75911042SErik.Nordmark@Sun.COM 		 */
7608704Sdanmcd@sun.com 		return;
7618704Sdanmcd@sun.com 	}
76211042SErik.Nordmark@Sun.COM 	ip_input_post_ipsec(mp, ira);
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * Now that weak-key passed, actually ADD the security association, and
7670Sstevel@tonic-gate  * send back a reply ADD message.
7680Sstevel@tonic-gate  */
7690Sstevel@tonic-gate static int
ah_add_sa_finish(mblk_t * mp,sadb_msg_t * samsg,keysock_in_t * ksi,int * diagnostic,ipsecah_stack_t * ahstack)7703055Sdanmcd ah_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi,
7713448Sdh155122     int *diagnostic, ipsecah_stack_t *ahstack)
7720Sstevel@tonic-gate {
77310934Ssommerfeld@sun.com 	isaf_t *primary = NULL, *secondary;
77410934Ssommerfeld@sun.com 	boolean_t clone = B_FALSE, is_inbound = B_FALSE;
7750Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
7760Sstevel@tonic-gate 	ipsa_t *larval;
7770Sstevel@tonic-gate 	ipsacq_t *acqrec;
7780Sstevel@tonic-gate 	iacqf_t *acq_bucket;
7790Sstevel@tonic-gate 	mblk_t *acq_msgs = NULL;
7800Sstevel@tonic-gate 	mblk_t *lpkt;
7810Sstevel@tonic-gate 	int rc;
78210934Ssommerfeld@sun.com 	ipsa_query_t sq;
78310934Ssommerfeld@sun.com 	int error;
7843448Sdh155122 	netstack_t	*ns = ahstack->ipsecah_netstack;
7853448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	 * Locate the appropriate table(s).
7890Sstevel@tonic-gate 	 */
7900Sstevel@tonic-gate 
79110934Ssommerfeld@sun.com 	sq.spp = &ahstack->ah_sadb;
79210934Ssommerfeld@sun.com 	error = sadb_form_query(ksi, IPSA_Q_SA|IPSA_Q_DST,
79310934Ssommerfeld@sun.com 	    IPSA_Q_SA|IPSA_Q_DST|IPSA_Q_INBOUND|IPSA_Q_OUTBOUND,
79410934Ssommerfeld@sun.com 	    &sq, diagnostic);
79510934Ssommerfeld@sun.com 	if (error)
79610934Ssommerfeld@sun.com 		return (error);
79710934Ssommerfeld@sun.com 
7986668Smarkfen 	/*
7996668Smarkfen 	 * Use the direction flags provided by the KMD to determine
8006668Smarkfen 	 * if the inbound or outbound table should be the primary
8016668Smarkfen 	 * for this SA. If these flags were absent then make this
8026668Smarkfen 	 * decision based on the addresses.
8036668Smarkfen 	 */
8046668Smarkfen 	if (assoc->sadb_sa_flags & IPSA_F_INBOUND) {
80510934Ssommerfeld@sun.com 		primary = sq.inbound;
80610934Ssommerfeld@sun.com 		secondary = sq.outbound;
8076668Smarkfen 		is_inbound = B_TRUE;
8086668Smarkfen 		if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND)
8096668Smarkfen 			clone = B_TRUE;
8106668Smarkfen 	} else {
8116668Smarkfen 		if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND) {
81210934Ssommerfeld@sun.com 			primary = sq.outbound;
81310934Ssommerfeld@sun.com 			secondary = sq.inbound;
8146668Smarkfen 		}
8156668Smarkfen 	}
8166668Smarkfen 	if (primary == NULL) {
8176668Smarkfen 		/*
8186668Smarkfen 		 * The KMD did not set a direction flag, determine which
8196668Smarkfen 		 * table to insert the SA into based on addresses.
8206668Smarkfen 		 */
8216668Smarkfen 		switch (ksi->ks_in_dsttype) {
8226668Smarkfen 		case KS_IN_ADDR_MBCAST:
8236668Smarkfen 			clone = B_TRUE;	/* All mcast SAs can be bidirectional */
8246668Smarkfen 			assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
8256668Smarkfen 			/* FALLTHRU */
8260Sstevel@tonic-gate 		/*
8270Sstevel@tonic-gate 		 * If the source address is either one of mine, or unspecified
8280Sstevel@tonic-gate 		 * (which is best summed up by saying "not 'not mine'"),
8290Sstevel@tonic-gate 		 * then the association is potentially bi-directional,
8300Sstevel@tonic-gate 		 * in that it can be used for inbound traffic and outbound
8310Sstevel@tonic-gate 		 * traffic.  The best example of such and SA is a multicast
8320Sstevel@tonic-gate 		 * SA (which allows me to receive the outbound traffic).
8330Sstevel@tonic-gate 		 */
8346668Smarkfen 		case KS_IN_ADDR_ME:
8356668Smarkfen 			assoc->sadb_sa_flags |= IPSA_F_INBOUND;
83610934Ssommerfeld@sun.com 			primary = sq.inbound;
83710934Ssommerfeld@sun.com 			secondary = sq.outbound;
8386668Smarkfen 			if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
8396668Smarkfen 				clone = B_TRUE;
8406668Smarkfen 			is_inbound = B_TRUE;
8416668Smarkfen 			break;
84210934Ssommerfeld@sun.com 
8430Sstevel@tonic-gate 		/*
8440Sstevel@tonic-gate 		 * If the source address literally not mine (either
8450Sstevel@tonic-gate 		 * unspecified or not mine), then this SA may have an
8460Sstevel@tonic-gate 		 * address that WILL be mine after some configuration.
8470Sstevel@tonic-gate 		 * We pay the price for this by making it a bi-directional
8480Sstevel@tonic-gate 		 * SA.
8490Sstevel@tonic-gate 		 */
8506668Smarkfen 		case KS_IN_ADDR_NOTME:
8516668Smarkfen 			assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
85210934Ssommerfeld@sun.com 			primary = sq.outbound;
85310934Ssommerfeld@sun.com 			secondary = sq.inbound;
8546668Smarkfen 			if (ksi->ks_in_srctype != KS_IN_ADDR_ME) {
8556668Smarkfen 				assoc->sadb_sa_flags |= IPSA_F_INBOUND;
8566668Smarkfen 				clone = B_TRUE;
8576668Smarkfen 			}
8586668Smarkfen 			break;
8596668Smarkfen 		default:
8606668Smarkfen 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
8616668Smarkfen 			return (EINVAL);
8626668Smarkfen 		}
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	/*
8660Sstevel@tonic-gate 	 * Find a ACQUIRE list entry if possible.  If we've added an SA that
8670Sstevel@tonic-gate 	 * suits the needs of an ACQUIRE list entry, we can eliminate the
8680Sstevel@tonic-gate 	 * ACQUIRE list entry and transmit the enqueued packets.  Use the
8690Sstevel@tonic-gate 	 * high-bit of the sequence number to queue it.  Key off destination
8700Sstevel@tonic-gate 	 * addr, and change acqrec's state.
8710Sstevel@tonic-gate 	 */
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
87410934Ssommerfeld@sun.com 		acq_bucket = &(sq.sp->sdb_acq[sq.outhash]);
8750Sstevel@tonic-gate 		mutex_enter(&acq_bucket->iacqf_lock);
8760Sstevel@tonic-gate 		for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
8770Sstevel@tonic-gate 		    acqrec = acqrec->ipsacq_next) {
8780Sstevel@tonic-gate 			mutex_enter(&acqrec->ipsacq_lock);
8790Sstevel@tonic-gate 			/*
8800Sstevel@tonic-gate 			 * Q:  I only check sequence.  Should I check dst?
8810Sstevel@tonic-gate 			 * A: Yes, check dest because those are the packets
8820Sstevel@tonic-gate 			 *    that are queued up.
8830Sstevel@tonic-gate 			 */
8840Sstevel@tonic-gate 			if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
88510934Ssommerfeld@sun.com 			    IPSA_ARE_ADDR_EQUAL(sq.dstaddr,
8864987Sdanmcd 			    acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
8870Sstevel@tonic-gate 				break;
8880Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
8890Sstevel@tonic-gate 		}
8900Sstevel@tonic-gate 		if (acqrec != NULL) {
8910Sstevel@tonic-gate 			/*
8920Sstevel@tonic-gate 			 * AHA!  I found an ACQUIRE record for this SA.
8930Sstevel@tonic-gate 			 * Grab the msg list, and free the acquire record.
8940Sstevel@tonic-gate 			 * I already am holding the lock for this record,
8950Sstevel@tonic-gate 			 * so all I have to do is free it.
8960Sstevel@tonic-gate 			 */
8970Sstevel@tonic-gate 			acq_msgs = acqrec->ipsacq_mp;
8980Sstevel@tonic-gate 			acqrec->ipsacq_mp = NULL;
8990Sstevel@tonic-gate 			mutex_exit(&acqrec->ipsacq_lock);
9003448Sdh155122 			sadb_destroy_acquire(acqrec, ns);
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 		mutex_exit(&acq_bucket->iacqf_lock);
9030Sstevel@tonic-gate 	}
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	/*
9060Sstevel@tonic-gate 	 * Find PF_KEY message, and see if I'm an update.  If so, find entry
9070Sstevel@tonic-gate 	 * in larval list (if there).
9080Sstevel@tonic-gate 	 */
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	larval = NULL;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_UPDATE) {
91310934Ssommerfeld@sun.com 		mutex_enter(&sq.inbound->isaf_lock);
91410934Ssommerfeld@sun.com 		larval = ipsec_getassocbyspi(sq.inbound, sq.assoc->sadb_sa_spi,
91510934Ssommerfeld@sun.com 		    ALL_ZEROES_PTR, sq.dstaddr, sq.dst->sin_family);
91610934Ssommerfeld@sun.com 		mutex_exit(&sq.inbound->isaf_lock);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		if ((larval == NULL) ||
9190Sstevel@tonic-gate 		    (larval->ipsa_state != IPSA_STATE_LARVAL)) {
9206668Smarkfen 			*diagnostic = SADB_X_DIAGNOSTIC_SA_NOTFOUND;
9217110Sdanmcd 			if (larval != NULL) {
9227110Sdanmcd 				IPSA_REFRELE(larval);
9237110Sdanmcd 			}
9240Sstevel@tonic-gate 			ah0dbg(("Larval update, but larval disappeared.\n"));
9250Sstevel@tonic-gate 			return (ESRCH);
9260Sstevel@tonic-gate 		} /* Else sadb_common_add unlinks it for me! */
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 
929*11446Sdanmcd@sun.com 	if (larval != NULL) {
930*11446Sdanmcd@sun.com 		/*
931*11446Sdanmcd@sun.com 		 * Hold again, because sadb_common_add() consumes a reference,
932*11446Sdanmcd@sun.com 		 * and we don't want to clear_lpkt() without a reference.
933*11446Sdanmcd@sun.com 		 */
934*11446Sdanmcd@sun.com 		IPSA_REFHOLD(larval);
935*11446Sdanmcd@sun.com 	}
9360Sstevel@tonic-gate 
93711042SErik.Nordmark@Sun.COM 	rc = sadb_common_add(ahstack->ah_pfkey_q, mp,
9383448Sdh155122 	    samsg, ksi, primary, secondary, larval, clone, is_inbound,
9396668Smarkfen 	    diagnostic, ns, &ahstack->ah_sadb);
9400Sstevel@tonic-gate 
941*11446Sdanmcd@sun.com 	if (larval != NULL) {
94211042SErik.Nordmark@Sun.COM 		if (rc == 0) {
943*11446Sdanmcd@sun.com 			lpkt = sadb_clear_lpkt(larval);
944*11446Sdanmcd@sun.com 			if (lpkt != NULL) {
945*11446Sdanmcd@sun.com 				rc = !taskq_dispatch(ah_taskq, inbound_task,
946*11446Sdanmcd@sun.com 				    lpkt, TQ_NOSLEEP);
947*11446Sdanmcd@sun.com 			}
94811042SErik.Nordmark@Sun.COM 		}
949*11446Sdanmcd@sun.com 		IPSA_REFRELE(larval);
95011042SErik.Nordmark@Sun.COM 	}
95111042SErik.Nordmark@Sun.COM 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * How much more stack will I create with all of these
95411042SErik.Nordmark@Sun.COM 	 * ah_outbound_*() calls?
9550Sstevel@tonic-gate 	 */
9560Sstevel@tonic-gate 
95711042SErik.Nordmark@Sun.COM 	/* Handle the packets queued waiting for the SA */
9580Sstevel@tonic-gate 	while (acq_msgs != NULL) {
95911042SErik.Nordmark@Sun.COM 		mblk_t		*asyncmp;
96011042SErik.Nordmark@Sun.COM 		mblk_t		*data_mp;
96111042SErik.Nordmark@Sun.COM 		ip_xmit_attr_t	ixas;
96211042SErik.Nordmark@Sun.COM 		ill_t		*ill;
96311042SErik.Nordmark@Sun.COM 
96411042SErik.Nordmark@Sun.COM 		asyncmp = acq_msgs;
9650Sstevel@tonic-gate 		acq_msgs = acq_msgs->b_next;
96611042SErik.Nordmark@Sun.COM 		asyncmp->b_next = NULL;
96711042SErik.Nordmark@Sun.COM 
96811042SErik.Nordmark@Sun.COM 		/*
96911042SErik.Nordmark@Sun.COM 		 * Extract the ip_xmit_attr_t from the first mblk.
97011042SErik.Nordmark@Sun.COM 		 * Verifies that the netstack and ill is still around; could
97111042SErik.Nordmark@Sun.COM 		 * have vanished while iked was doing its work.
97211042SErik.Nordmark@Sun.COM 		 * On succesful return we have a nce_t and the ill/ipst can't
97311042SErik.Nordmark@Sun.COM 		 * disappear until we do the nce_refrele in ixa_cleanup.
97411042SErik.Nordmark@Sun.COM 		 */
97511042SErik.Nordmark@Sun.COM 		data_mp = asyncmp->b_cont;
97611042SErik.Nordmark@Sun.COM 		asyncmp->b_cont = NULL;
97711042SErik.Nordmark@Sun.COM 		if (!ip_xmit_attr_from_mblk(asyncmp, &ixas)) {
97811042SErik.Nordmark@Sun.COM 			AH_BUMP_STAT(ahstack, out_discards);
97911042SErik.Nordmark@Sun.COM 			ip_drop_packet(data_mp, B_FALSE, NULL,
98011042SErik.Nordmark@Sun.COM 			    DROPPER(ipss, ipds_sadb_acquire_timeout),
98111042SErik.Nordmark@Sun.COM 			    &ahstack->ah_dropper);
98211042SErik.Nordmark@Sun.COM 		} else if (rc != 0) {
98311042SErik.Nordmark@Sun.COM 			ill = ixas.ixa_nce->nce_ill;
98411042SErik.Nordmark@Sun.COM 			AH_BUMP_STAT(ahstack, out_discards);
98511042SErik.Nordmark@Sun.COM 			ip_drop_packet(data_mp, B_FALSE, ill,
98611042SErik.Nordmark@Sun.COM 			    DROPPER(ipss, ipds_sadb_acquire_timeout),
98711042SErik.Nordmark@Sun.COM 			    &ahstack->ah_dropper);
98811042SErik.Nordmark@Sun.COM 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
98911042SErik.Nordmark@Sun.COM 		} else {
99011042SErik.Nordmark@Sun.COM 			ah_outbound_finish(data_mp, &ixas);
9910Sstevel@tonic-gate 		}
99211042SErik.Nordmark@Sun.COM 		ixa_cleanup(&ixas);
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	return (rc);
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate 
99811042SErik.Nordmark@Sun.COM 
99911042SErik.Nordmark@Sun.COM /*
100011042SErik.Nordmark@Sun.COM  * Process one of the queued messages (from ipsacq_mp) once the SA
100111042SErik.Nordmark@Sun.COM  * has been added.
100211042SErik.Nordmark@Sun.COM  */
100311042SErik.Nordmark@Sun.COM static void
ah_outbound_finish(mblk_t * data_mp,ip_xmit_attr_t * ixa)100411042SErik.Nordmark@Sun.COM ah_outbound_finish(mblk_t *data_mp, ip_xmit_attr_t *ixa)
100511042SErik.Nordmark@Sun.COM {
100611042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ixa->ixa_ipst->ips_netstack;
100711042SErik.Nordmark@Sun.COM 	ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
100811042SErik.Nordmark@Sun.COM 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
100911042SErik.Nordmark@Sun.COM 	ill_t		*ill = ixa->ixa_nce->nce_ill;
101011042SErik.Nordmark@Sun.COM 
101111042SErik.Nordmark@Sun.COM 	if (!ipsec_outbound_sa(data_mp, ixa, IPPROTO_AH)) {
101211042SErik.Nordmark@Sun.COM 		AH_BUMP_STAT(ahstack, out_discards);
101311042SErik.Nordmark@Sun.COM 		ip_drop_packet(data_mp, B_FALSE, ill,
101411042SErik.Nordmark@Sun.COM 		    DROPPER(ipss, ipds_sadb_acquire_timeout),
101511042SErik.Nordmark@Sun.COM 		    &ahstack->ah_dropper);
101611042SErik.Nordmark@Sun.COM 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
101711042SErik.Nordmark@Sun.COM 		return;
101811042SErik.Nordmark@Sun.COM 	}
101911042SErik.Nordmark@Sun.COM 
102011042SErik.Nordmark@Sun.COM 	data_mp = ah_outbound(data_mp, ixa);
102111042SErik.Nordmark@Sun.COM 	if (data_mp == NULL)
102211042SErik.Nordmark@Sun.COM 		return;
102311042SErik.Nordmark@Sun.COM 
102411042SErik.Nordmark@Sun.COM 	(void) ip_output_post_ipsec(data_mp, ixa);
102511042SErik.Nordmark@Sun.COM }
102611042SErik.Nordmark@Sun.COM 
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate  * Add new AH security association.  This may become a generic AH/ESP
10290Sstevel@tonic-gate  * routine eventually.
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate static int
ah_add_sa(mblk_t * mp,keysock_in_t * ksi,int * diagnostic,netstack_t * ns)10323448Sdh155122 ah_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, netstack_t *ns)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
10350Sstevel@tonic-gate 	sadb_address_t *srcext =
10360Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
10370Sstevel@tonic-gate 	sadb_address_t *dstext =
10380Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
10393055Sdanmcd 	sadb_address_t *isrcext =
10403055Sdanmcd 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
10413055Sdanmcd 	sadb_address_t *idstext =
10423055Sdanmcd 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
10430Sstevel@tonic-gate 	sadb_key_t *key = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
10440Sstevel@tonic-gate 	struct sockaddr_in *src, *dst;
10450Sstevel@tonic-gate 	/* We don't need sockaddr_in6 for now. */
10460Sstevel@tonic-gate 	sadb_lifetime_t *soft =
10470Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
10480Sstevel@tonic-gate 	sadb_lifetime_t *hard =
10490Sstevel@tonic-gate 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
10507749SThejaswini.Singarajipura@Sun.COM 	sadb_lifetime_t *idle =
10517749SThejaswini.Singarajipura@Sun.COM 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_X_EXT_LIFETIME_IDLE];
10520Sstevel@tonic-gate 	ipsec_alginfo_t *aalg;
10533448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
10543448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	/* I need certain extensions present for an ADD message. */
10570Sstevel@tonic-gate 	if (srcext == NULL) {
10580Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
10590Sstevel@tonic-gate 		return (EINVAL);
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 	if (dstext == NULL) {
10620Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
10630Sstevel@tonic-gate 		return (EINVAL);
10640Sstevel@tonic-gate 	}
10653055Sdanmcd 	if (isrcext == NULL && idstext != NULL) {
10663055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
10673055Sdanmcd 		return (EINVAL);
10683055Sdanmcd 	}
10693055Sdanmcd 	if (isrcext != NULL && idstext == NULL) {
10703055Sdanmcd 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
10713055Sdanmcd 		return (EINVAL);
10723055Sdanmcd 	}
10730Sstevel@tonic-gate 	if (assoc == NULL) {
10740Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
10750Sstevel@tonic-gate 		return (EINVAL);
10760Sstevel@tonic-gate 	}
10770Sstevel@tonic-gate 	if (key == NULL) {
10780Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_AKEY;
10790Sstevel@tonic-gate 		return (EINVAL);
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	src = (struct sockaddr_in *)(srcext + 1);
10830Sstevel@tonic-gate 	dst = (struct sockaddr_in *)(dstext + 1);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/* Sundry ADD-specific reality checks. */
10860Sstevel@tonic-gate 	/* XXX STATS : Logging/stats here? */
10870Sstevel@tonic-gate 
10887749SThejaswini.Singarajipura@Sun.COM 	if ((assoc->sadb_sa_state != SADB_SASTATE_MATURE) &&
10897749SThejaswini.Singarajipura@Sun.COM 	    (assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE_ELSEWHERE)) {
10900Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
10910Sstevel@tonic-gate 		return (EINVAL);
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 	if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
10940Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_ENCR_NOTSUPP;
10950Sstevel@tonic-gate 		return (EINVAL);
10960Sstevel@tonic-gate 	}
10977110Sdanmcd 	if (assoc->sadb_sa_flags & ~ahstack->ah_sadb.s_addflags) {
10980Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
10990Sstevel@tonic-gate 		return (EINVAL);
11000Sstevel@tonic-gate 	}
11017749SThejaswini.Singarajipura@Sun.COM 	if ((*diagnostic = sadb_hardsoftchk(hard, soft, idle)) != 0)
11020Sstevel@tonic-gate 		return (EINVAL);
11030Sstevel@tonic-gate 
11043055Sdanmcd 	ASSERT(src->sin_family == dst->sin_family);
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	/* Stuff I don't support, for now.  XXX Diagnostic? */
110710934Ssommerfeld@sun.com 	if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL)
11080Sstevel@tonic-gate 		return (EOPNOTSUPP);
11090Sstevel@tonic-gate 
111010934Ssommerfeld@sun.com 	if (ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL) {
111110934Ssommerfeld@sun.com 		if (!is_system_labeled())
111210934Ssommerfeld@sun.com 			return (EOPNOTSUPP);
111310934Ssommerfeld@sun.com 	}
111410934Ssommerfeld@sun.com 
111510934Ssommerfeld@sun.com 	if (ksi->ks_in_extv[SADB_X_EXT_OUTER_SENS] != NULL) {
111610934Ssommerfeld@sun.com 		if (!is_system_labeled())
111710934Ssommerfeld@sun.com 			return (EOPNOTSUPP);
111810934Ssommerfeld@sun.com 	}
11190Sstevel@tonic-gate 	/*
112010934Ssommerfeld@sun.com 	 * XXX Policy : I'm not checking identities at this time, but
112110934Ssommerfeld@sun.com 	 * if I did, I'd do them here, before I sent the weak key
112210934Ssommerfeld@sun.com 	 * check up to the algorithm.
11230Sstevel@tonic-gate 	 */
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	/* verify that there is a mapping for the specified algorithm */
11263448Sdh155122 	mutex_enter(&ipss->ipsec_alg_lock);
11273448Sdh155122 	aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH][assoc->sadb_sa_auth];
11280Sstevel@tonic-gate 	if (aalg == NULL || !ALG_VALID(aalg)) {
11293448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
11303448Sdh155122 		ah1dbg(ahstack, ("Couldn't find auth alg #%d.\n",
11314987Sdanmcd 		    assoc->sadb_sa_auth));
11320Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
11330Sstevel@tonic-gate 		return (EINVAL);
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 	ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	/* sanity check key sizes */
11380Sstevel@tonic-gate 	if (!ipsec_valid_key_size(key->sadb_key_bits, aalg)) {
11393448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
11400Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
11410Sstevel@tonic-gate 		return (EINVAL);
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	/* check key and fix parity if needed */
11450Sstevel@tonic-gate 	if (ipsec_check_key(aalg->alg_mech_type, key, B_TRUE,
11460Sstevel@tonic-gate 	    diagnostic) != 0) {
11473448Sdh155122 		mutex_exit(&ipss->ipsec_alg_lock);
11480Sstevel@tonic-gate 		return (EINVAL);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11513448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
11520Sstevel@tonic-gate 
11533055Sdanmcd 	return (ah_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
11544987Sdanmcd 	    diagnostic, ahstack));
11550Sstevel@tonic-gate }
11560Sstevel@tonic-gate 
115710934Ssommerfeld@sun.com /* Refactor me */
11580Sstevel@tonic-gate /*
11590Sstevel@tonic-gate  * Update a security association.  Updates come in two varieties.  The first
11600Sstevel@tonic-gate  * is an update of lifetimes on a non-larval SA.  The second is an update of
11610Sstevel@tonic-gate  * a larval SA, which ends up looking a lot more like an add.
11620Sstevel@tonic-gate  */
11630Sstevel@tonic-gate static int
ah_update_sa(mblk_t * mp,keysock_in_t * ksi,int * diagnostic,ipsecah_stack_t * ahstack,uint8_t sadb_msg_type)11643448Sdh155122 ah_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
11656668Smarkfen     ipsecah_stack_t *ahstack, uint8_t sadb_msg_type)
11660Sstevel@tonic-gate {
11677749SThejaswini.Singarajipura@Sun.COM 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
11680Sstevel@tonic-gate 	sadb_address_t *dstext =
11690Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
11707749SThejaswini.Singarajipura@Sun.COM 	mblk_t	*buf_pkt;
11717749SThejaswini.Singarajipura@Sun.COM 	int rcode;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if (dstext == NULL) {
11740Sstevel@tonic-gate 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
11750Sstevel@tonic-gate 		return (EINVAL);
11760Sstevel@tonic-gate 	}
11777749SThejaswini.Singarajipura@Sun.COM 
11787749SThejaswini.Singarajipura@Sun.COM 	rcode = sadb_update_sa(mp, ksi, &buf_pkt, &ahstack->ah_sadb,
11797749SThejaswini.Singarajipura@Sun.COM 	    diagnostic, ahstack->ah_pfkey_q, ah_add_sa,
11807749SThejaswini.Singarajipura@Sun.COM 	    ahstack->ipsecah_netstack, sadb_msg_type);
11817749SThejaswini.Singarajipura@Sun.COM 
11827749SThejaswini.Singarajipura@Sun.COM 	if ((assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE) ||
11837749SThejaswini.Singarajipura@Sun.COM 	    (rcode != 0)) {
11847749SThejaswini.Singarajipura@Sun.COM 		return (rcode);
11857749SThejaswini.Singarajipura@Sun.COM 	}
11867749SThejaswini.Singarajipura@Sun.COM 
11878704Sdanmcd@sun.com 	HANDLE_BUF_PKT(ah_taskq, ahstack->ipsecah_netstack->netstack_ipsec,
11888704Sdanmcd@sun.com 	    ahstack->ah_dropper, buf_pkt);
11897749SThejaswini.Singarajipura@Sun.COM 
11907749SThejaswini.Singarajipura@Sun.COM 	return (rcode);
11910Sstevel@tonic-gate }
11920Sstevel@tonic-gate 
119310934Ssommerfeld@sun.com /* Refactor me */
11940Sstevel@tonic-gate /*
11950Sstevel@tonic-gate  * Delete a security association.  This is REALLY likely to be code common to
11960Sstevel@tonic-gate  * both AH and ESP.  Find the association, then unlink it.
11970Sstevel@tonic-gate  */
11980Sstevel@tonic-gate static int
ah_del_sa(mblk_t * mp,keysock_in_t * ksi,int * diagnostic,ipsecah_stack_t * ahstack,uint8_t sadb_msg_type)11993448Sdh155122 ah_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
12006668Smarkfen     ipsecah_stack_t *ahstack, uint8_t sadb_msg_type)
12010Sstevel@tonic-gate {
12020Sstevel@tonic-gate 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
12030Sstevel@tonic-gate 	sadb_address_t *dstext =
12040Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
12050Sstevel@tonic-gate 	sadb_address_t *srcext =
12060Sstevel@tonic-gate 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
12070Sstevel@tonic-gate 	struct sockaddr_in *sin;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	if (assoc == NULL) {
12100Sstevel@tonic-gate 		if (dstext != NULL)
12110Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(dstext + 1);
12120Sstevel@tonic-gate 		else if (srcext != NULL)
12130Sstevel@tonic-gate 			sin = (struct sockaddr_in *)(srcext + 1);
12140Sstevel@tonic-gate 		else {
12150Sstevel@tonic-gate 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
12160Sstevel@tonic-gate 			return (EINVAL);
12170Sstevel@tonic-gate 		}
12183055Sdanmcd 		return (sadb_purge_sa(mp, ksi,
12193448Sdh155122 		    (sin->sin_family == AF_INET6) ? &ahstack->ah_sadb.s_v6 :
122011042SErik.Nordmark@Sun.COM 		    &ahstack->ah_sadb.s_v4, diagnostic, ahstack->ah_pfkey_q));
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate 
12236668Smarkfen 	return (sadb_delget_sa(mp, ksi, &ahstack->ah_sadb, diagnostic,
12246668Smarkfen 	    ahstack->ah_pfkey_q, sadb_msg_type));
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate 
122710934Ssommerfeld@sun.com /* Refactor me */
12280Sstevel@tonic-gate /*
12290Sstevel@tonic-gate  * Convert the entire contents of all of AH's SA tables into PF_KEY SADB_DUMP
12300Sstevel@tonic-gate  * messages.
12310Sstevel@tonic-gate  */
12320Sstevel@tonic-gate static void
ah_dump(mblk_t * mp,keysock_in_t * ksi,ipsecah_stack_t * ahstack)12333448Sdh155122 ah_dump(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
12340Sstevel@tonic-gate {
12350Sstevel@tonic-gate 	int error;
12360Sstevel@tonic-gate 	sadb_msg_t *samsg;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	/*
12390Sstevel@tonic-gate 	 * Dump each fanout, bailing if error is non-zero.
12400Sstevel@tonic-gate 	 */
12410Sstevel@tonic-gate 
12427749SThejaswini.Singarajipura@Sun.COM 	error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi, &ahstack->ah_sadb.s_v4);
12430Sstevel@tonic-gate 	if (error != 0)
12440Sstevel@tonic-gate 		goto bail;
12450Sstevel@tonic-gate 
12467749SThejaswini.Singarajipura@Sun.COM 	error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi, &ahstack->ah_sadb.s_v6);
12470Sstevel@tonic-gate bail:
12480Sstevel@tonic-gate 	ASSERT(mp->b_cont != NULL);
12490Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
12500Sstevel@tonic-gate 	samsg->sadb_msg_errno = (uint8_t)error;
12513448Sdh155122 	sadb_pfkey_echo(ahstack->ah_pfkey_q, mp,
12523448Sdh155122 	    (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL);
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate /*
12563055Sdanmcd  * First-cut reality check for an inbound PF_KEY message.
12573055Sdanmcd  */
12583055Sdanmcd static boolean_t
ah_pfkey_reality_failures(mblk_t * mp,keysock_in_t * ksi,ipsecah_stack_t * ahstack)12593448Sdh155122 ah_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
12603448Sdh155122     ipsecah_stack_t *ahstack)
12613055Sdanmcd {
12623055Sdanmcd 	int diagnostic;
12633055Sdanmcd 
12643055Sdanmcd 	if (mp->b_cont == NULL) {
12653055Sdanmcd 		freemsg(mp);
12663055Sdanmcd 		return (B_TRUE);
12673055Sdanmcd 	}
12683055Sdanmcd 
12693055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL) {
12703055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT;
12713055Sdanmcd 		goto badmsg;
12723055Sdanmcd 	}
12733055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
12743055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
12753055Sdanmcd 		goto badmsg;
12763055Sdanmcd 	}
12773055Sdanmcd 	if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
12783055Sdanmcd 	    ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
12793055Sdanmcd 		diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
12803055Sdanmcd 		goto badmsg;
12813055Sdanmcd 	}
12823055Sdanmcd 	return (B_FALSE);	/* False ==> no failures */
12833055Sdanmcd 
12843055Sdanmcd badmsg:
12853448Sdh155122 	sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
12863448Sdh155122 	    diagnostic, ksi->ks_in_serial);
12873055Sdanmcd 	return (B_TRUE);	/* True ==> failures */
12883055Sdanmcd }
12893055Sdanmcd 
12903055Sdanmcd /*
12910Sstevel@tonic-gate  * AH parsing of PF_KEY messages.  Keysock did most of the really silly
12920Sstevel@tonic-gate  * error cases.  What I receive is a fully-formed, syntactically legal
12930Sstevel@tonic-gate  * PF_KEY message.  I then need to check semantics...
12940Sstevel@tonic-gate  *
12950Sstevel@tonic-gate  * This code may become common to AH and ESP.  Stay tuned.
12960Sstevel@tonic-gate  *
12970Sstevel@tonic-gate  * I also make the assumption that db_ref's are cool.  If this assumption
12980Sstevel@tonic-gate  * is wrong, this means that someone other than keysock or me has been
12990Sstevel@tonic-gate  * mucking with PF_KEY messages.
13000Sstevel@tonic-gate  */
13010Sstevel@tonic-gate static void
ah_parse_pfkey(mblk_t * mp,ipsecah_stack_t * ahstack)13023448Sdh155122 ah_parse_pfkey(mblk_t *mp, ipsecah_stack_t *ahstack)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	mblk_t *msg = mp->b_cont;
13050Sstevel@tonic-gate 	sadb_msg_t *samsg;
13060Sstevel@tonic-gate 	keysock_in_t *ksi;
13070Sstevel@tonic-gate 	int error;
13080Sstevel@tonic-gate 	int diagnostic = SADB_X_DIAGNOSTIC_NONE;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	ASSERT(msg != NULL);
13113448Sdh155122 
13120Sstevel@tonic-gate 	samsg = (sadb_msg_t *)msg->b_rptr;
13130Sstevel@tonic-gate 	ksi = (keysock_in_t *)mp->b_rptr;
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * If applicable, convert unspecified AF_INET6 to unspecified
13170Sstevel@tonic-gate 	 * AF_INET.
13180Sstevel@tonic-gate 	 */
13193448Sdh155122 	if (!sadb_addrfix(ksi, ahstack->ah_pfkey_q, mp,
13203448Sdh155122 	    ahstack->ipsecah_netstack) ||
13213448Sdh155122 	    ah_pfkey_reality_failures(mp, ksi, ahstack)) {
13223055Sdanmcd 		return;
13233055Sdanmcd 	}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
13260Sstevel@tonic-gate 	case SADB_ADD:
13273448Sdh155122 		error = ah_add_sa(mp, ksi, &diagnostic,
13283448Sdh155122 		    ahstack->ipsecah_netstack);
13290Sstevel@tonic-gate 		if (error != 0) {
13303448Sdh155122 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
13313448Sdh155122 			    diagnostic, ksi->ks_in_serial);
13320Sstevel@tonic-gate 		}
13330Sstevel@tonic-gate 		/* else ah_add_sa() took care of things. */
13340Sstevel@tonic-gate 		break;
13350Sstevel@tonic-gate 	case SADB_DELETE:
13366668Smarkfen 	case SADB_X_DELPAIR:
13377749SThejaswini.Singarajipura@Sun.COM 	case SADB_X_DELPAIR_STATE:
13386668Smarkfen 		error = ah_del_sa(mp, ksi, &diagnostic, ahstack,
13396668Smarkfen 		    samsg->sadb_msg_type);
13400Sstevel@tonic-gate 		if (error != 0) {
13413448Sdh155122 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
13423448Sdh155122 			    diagnostic, ksi->ks_in_serial);
13430Sstevel@tonic-gate 		}
13440Sstevel@tonic-gate 		/* Else ah_del_sa() took care of things. */
13450Sstevel@tonic-gate 		break;
13460Sstevel@tonic-gate 	case SADB_GET:
13476668Smarkfen 		error = sadb_delget_sa(mp, ksi, &ahstack->ah_sadb, &diagnostic,
13486668Smarkfen 		    ahstack->ah_pfkey_q, samsg->sadb_msg_type);
13490Sstevel@tonic-gate 		if (error != 0) {
13503448Sdh155122 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
13513448Sdh155122 			    diagnostic, ksi->ks_in_serial);
13520Sstevel@tonic-gate 		}
13530Sstevel@tonic-gate 		/* Else sadb_get_sa() took care of things. */
13540Sstevel@tonic-gate 		break;
13550Sstevel@tonic-gate 	case SADB_FLUSH:
13563448Sdh155122 		sadbp_flush(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
13573448Sdh155122 		sadb_pfkey_echo(ahstack->ah_pfkey_q, mp, samsg, ksi, NULL);
13580Sstevel@tonic-gate 		break;
13590Sstevel@tonic-gate 	case SADB_REGISTER:
13600Sstevel@tonic-gate 		/*
13610Sstevel@tonic-gate 		 * Hmmm, let's do it!  Check for extensions (there should
13620Sstevel@tonic-gate 		 * be none), extract the fields, call ah_register_out(),
13630Sstevel@tonic-gate 		 * then either free or report an error.
13640Sstevel@tonic-gate 		 *
13650Sstevel@tonic-gate 		 * Keysock takes care of the PF_KEY bookkeeping for this.
13660Sstevel@tonic-gate 		 */
13670Sstevel@tonic-gate 		if (ah_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
136811042SErik.Nordmark@Sun.COM 		    ksi->ks_in_serial, ahstack, msg_getcred(mp, NULL))) {
13690Sstevel@tonic-gate 			freemsg(mp);
13700Sstevel@tonic-gate 		} else {
13710Sstevel@tonic-gate 			/*
13720Sstevel@tonic-gate 			 * Only way this path hits is if there is a memory
13730Sstevel@tonic-gate 			 * failure.  It will not return B_FALSE because of
13740Sstevel@tonic-gate 			 * lack of ah_pfkey_q if I am in wput().
13750Sstevel@tonic-gate 			 */
13763448Sdh155122 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM,
13773448Sdh155122 			    diagnostic, ksi->ks_in_serial);
13780Sstevel@tonic-gate 		}
13790Sstevel@tonic-gate 		break;
13800Sstevel@tonic-gate 	case SADB_UPDATE:
13816668Smarkfen 	case SADB_X_UPDATEPAIR:
13820Sstevel@tonic-gate 		/*
13830Sstevel@tonic-gate 		 * Find a larval, if not there, find a full one and get
13840Sstevel@tonic-gate 		 * strict.
13850Sstevel@tonic-gate 		 */
13866668Smarkfen 		error = ah_update_sa(mp, ksi, &diagnostic, ahstack,
13876668Smarkfen 		    samsg->sadb_msg_type);
13880Sstevel@tonic-gate 		if (error != 0) {
13893448Sdh155122 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
13903448Sdh155122 			    diagnostic, ksi->ks_in_serial);
13910Sstevel@tonic-gate 		}
13920Sstevel@tonic-gate 		/* else ah_update_sa() took care of things. */
13930Sstevel@tonic-gate 		break;
13940Sstevel@tonic-gate 	case SADB_GETSPI:
13950Sstevel@tonic-gate 		/*
13960Sstevel@tonic-gate 		 * Reserve a new larval entry.
13970Sstevel@tonic-gate 		 */
13983448Sdh155122 		ah_getspi(mp, ksi, ahstack);
13990Sstevel@tonic-gate 		break;
14000Sstevel@tonic-gate 	case SADB_ACQUIRE:
14010Sstevel@tonic-gate 		/*
14020Sstevel@tonic-gate 		 * Find larval and/or ACQUIRE record and kill it (them), I'm
14030Sstevel@tonic-gate 		 * most likely an error.  Inbound ACQUIRE messages should only
14040Sstevel@tonic-gate 		 * have the base header.
14050Sstevel@tonic-gate 		 */
14063448Sdh155122 		sadb_in_acquire(samsg, &ahstack->ah_sadb, ahstack->ah_pfkey_q,
14073448Sdh155122 		    ahstack->ipsecah_netstack);
14080Sstevel@tonic-gate 		freemsg(mp);
14090Sstevel@tonic-gate 		break;
14100Sstevel@tonic-gate 	case SADB_DUMP:
14110Sstevel@tonic-gate 		/*
14120Sstevel@tonic-gate 		 * Dump all entries.
14130Sstevel@tonic-gate 		 */
14143448Sdh155122 		ah_dump(mp, ksi, ahstack);
14150Sstevel@tonic-gate 		/* ah_dump will take care of the return message, etc. */
14160Sstevel@tonic-gate 		break;
14170Sstevel@tonic-gate 	case SADB_EXPIRE:
14180Sstevel@tonic-gate 		/* Should never reach me. */
14193448Sdh155122 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EOPNOTSUPP,
14203448Sdh155122 		    diagnostic, ksi->ks_in_serial);
14210Sstevel@tonic-gate 		break;
14220Sstevel@tonic-gate 	default:
14233448Sdh155122 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
14240Sstevel@tonic-gate 		    SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
14250Sstevel@tonic-gate 		break;
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate }
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate /*
14300Sstevel@tonic-gate  * Handle case where PF_KEY says it can't find a keysock for one of my
14310Sstevel@tonic-gate  * ACQUIRE messages.
14320Sstevel@tonic-gate  */
14330Sstevel@tonic-gate static void
ah_keysock_no_socket(mblk_t * mp,ipsecah_stack_t * ahstack)14343448Sdh155122 ah_keysock_no_socket(mblk_t *mp, ipsecah_stack_t *ahstack)
14350Sstevel@tonic-gate {
14360Sstevel@tonic-gate 	sadb_msg_t *samsg;
14370Sstevel@tonic-gate 	keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	if (mp->b_cont == NULL) {
14400Sstevel@tonic-gate 		freemsg(mp);
14410Sstevel@tonic-gate 		return;
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	/*
14460Sstevel@tonic-gate 	 * If keysock can't find any registered, delete the acquire record
14470Sstevel@tonic-gate 	 * immediately, and handle errors.
14480Sstevel@tonic-gate 	 */
14490Sstevel@tonic-gate 	if (samsg->sadb_msg_type == SADB_ACQUIRE) {
14500Sstevel@tonic-gate 		samsg->sadb_msg_errno = kse->ks_err_errno;
14510Sstevel@tonic-gate 		samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
14520Sstevel@tonic-gate 		/*
145311042SErik.Nordmark@Sun.COM 		 * Use the write-side of the ah_pfkey_q
14540Sstevel@tonic-gate 		 */
14553448Sdh155122 		sadb_in_acquire(samsg, &ahstack->ah_sadb,
14563448Sdh155122 		    WR(ahstack->ah_pfkey_q), ahstack->ipsecah_netstack);
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	freemsg(mp);
14600Sstevel@tonic-gate }
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate /*
14630Sstevel@tonic-gate  * AH module write put routine.
14640Sstevel@tonic-gate  */
14650Sstevel@tonic-gate static void
ipsecah_wput(queue_t * q,mblk_t * mp)14660Sstevel@tonic-gate ipsecah_wput(queue_t *q, mblk_t *mp)
14670Sstevel@tonic-gate {
14680Sstevel@tonic-gate 	ipsec_info_t *ii;
14690Sstevel@tonic-gate 	struct iocblk *iocp;
14703448Sdh155122 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
14713448Sdh155122 
14723448Sdh155122 	ah3dbg(ahstack, ("In ah_wput().\n"));
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	/* NOTE:  Each case must take care of freeing or passing mp. */
14750Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
14760Sstevel@tonic-gate 	case M_CTL:
14770Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
14780Sstevel@tonic-gate 			/* Not big enough message. */
14790Sstevel@tonic-gate 			freemsg(mp);
14800Sstevel@tonic-gate 			break;
14810Sstevel@tonic-gate 		}
14820Sstevel@tonic-gate 		ii = (ipsec_info_t *)mp->b_rptr;
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		switch (ii->ipsec_info_type) {
14850Sstevel@tonic-gate 		case KEYSOCK_OUT_ERR:
14863448Sdh155122 			ah1dbg(ahstack, ("Got KEYSOCK_OUT_ERR message.\n"));
14873448Sdh155122 			ah_keysock_no_socket(mp, ahstack);
14880Sstevel@tonic-gate 			break;
14890Sstevel@tonic-gate 		case KEYSOCK_IN:
14903448Sdh155122 			AH_BUMP_STAT(ahstack, keysock_in);
14913448Sdh155122 			ah3dbg(ahstack, ("Got KEYSOCK_IN message.\n"));
14923055Sdanmcd 
14933055Sdanmcd 			/* Parse the message. */
14943448Sdh155122 			ah_parse_pfkey(mp, ahstack);
14950Sstevel@tonic-gate 			break;
14960Sstevel@tonic-gate 		case KEYSOCK_HELLO:
14973448Sdh155122 			sadb_keysock_hello(&ahstack->ah_pfkey_q, q, mp,
14983448Sdh155122 			    ah_ager, (void *)ahstack, &ahstack->ah_event,
14993448Sdh155122 			    SADB_SATYPE_AH);
15000Sstevel@tonic-gate 			break;
15010Sstevel@tonic-gate 		default:
15023448Sdh155122 			ah1dbg(ahstack, ("Got M_CTL from above of 0x%x.\n",
15030Sstevel@tonic-gate 			    ii->ipsec_info_type));
15040Sstevel@tonic-gate 			freemsg(mp);
15050Sstevel@tonic-gate 			break;
15060Sstevel@tonic-gate 		}
15070Sstevel@tonic-gate 		break;
15080Sstevel@tonic-gate 	case M_IOCTL:
15090Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
15100Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
15110Sstevel@tonic-gate 		case ND_SET:
15120Sstevel@tonic-gate 		case ND_GET:
15133448Sdh155122 			if (nd_getset(q, ahstack->ipsecah_g_nd, mp)) {
15140Sstevel@tonic-gate 				qreply(q, mp);
15150Sstevel@tonic-gate 				return;
15160Sstevel@tonic-gate 			} else {
15170Sstevel@tonic-gate 				iocp->ioc_error = ENOENT;
15180Sstevel@tonic-gate 			}
15190Sstevel@tonic-gate 			/* FALLTHRU */
15200Sstevel@tonic-gate 		default:
15210Sstevel@tonic-gate 			/* We really don't support any other ioctls, do we? */
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 			/* Return EINVAL */
15240Sstevel@tonic-gate 			if (iocp->ioc_error != ENOENT)
15250Sstevel@tonic-gate 				iocp->ioc_error = EINVAL;
15260Sstevel@tonic-gate 			iocp->ioc_count = 0;
15270Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
15280Sstevel@tonic-gate 			qreply(q, mp);
15290Sstevel@tonic-gate 			return;
15300Sstevel@tonic-gate 		}
15310Sstevel@tonic-gate 	default:
15323448Sdh155122 		ah3dbg(ahstack,
15333448Sdh155122 		    ("Got default message, type %d, passing to IP.\n",
15340Sstevel@tonic-gate 		    mp->b_datap->db_type));
15350Sstevel@tonic-gate 		putnext(q, mp);
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate }
15380Sstevel@tonic-gate 
153910934Ssommerfeld@sun.com /* Refactor me */
15400Sstevel@tonic-gate /*
15410Sstevel@tonic-gate  * Updating use times can be tricky business if the ipsa_haspeer flag is
15420Sstevel@tonic-gate  * set.  This function is called once in an SA's lifetime.
15430Sstevel@tonic-gate  *
15440Sstevel@tonic-gate  * Caller has to REFRELE "assoc" which is passed in.  This function has
15450Sstevel@tonic-gate  * to REFRELE any peer SA that is obtained.
15460Sstevel@tonic-gate  */
15470Sstevel@tonic-gate static void
ah_set_usetime(ipsa_t * assoc,boolean_t inbound)15480Sstevel@tonic-gate ah_set_usetime(ipsa_t *assoc, boolean_t inbound)
15490Sstevel@tonic-gate {
15500Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
15510Sstevel@tonic-gate 	isaf_t *bucket;
15520Sstevel@tonic-gate 	sadb_t *sp;
15530Sstevel@tonic-gate 	int outhash;
15540Sstevel@tonic-gate 	boolean_t isv6;
15553448Sdh155122 	netstack_t	*ns = assoc->ipsa_netstack;
15563448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	/* No peer?  No problem! */
15590Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
15600Sstevel@tonic-gate 		sadb_set_usetime(assoc);
15610Sstevel@tonic-gate 		return;
15620Sstevel@tonic-gate 	}
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	/*
15650Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
15660Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, the times
15670Sstevel@tonic-gate 	 * will be out-of-synch by at most a second, and since our time
15680Sstevel@tonic-gate 	 * granularity is a second, this won't be a problem.
15690Sstevel@tonic-gate 	 *
15700Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
15710Sstevel@tonic-gate 	 * reconsider.
15720Sstevel@tonic-gate 	 */
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/* Use address family to select IPv6/IPv4 */
15750Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
15760Sstevel@tonic-gate 	if (isv6) {
15773448Sdh155122 		sp = &ahstack->ah_sadb.s_v6;
15780Sstevel@tonic-gate 	} else {
15793448Sdh155122 		sp = &ahstack->ah_sadb.s_v4;
15800Sstevel@tonic-gate 		ASSERT(assoc->ipsa_addrfam == AF_INET);
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 	if (inbound) {
15830Sstevel@tonic-gate 		inassoc = assoc;
15840Sstevel@tonic-gate 		if (isv6)
15854987Sdanmcd 			outhash = OUTBOUND_HASH_V6(sp,
15864987Sdanmcd 			    *((in6_addr_t *)&inassoc->ipsa_dstaddr));
15870Sstevel@tonic-gate 		else
15884987Sdanmcd 			outhash = OUTBOUND_HASH_V4(sp,
15894987Sdanmcd 			    *((ipaddr_t *)&inassoc->ipsa_dstaddr));
15900Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
15930Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
15940Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
15950Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
15960Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
15970Sstevel@tonic-gate 		if (outassoc == NULL) {
15980Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
15990Sstevel@tonic-gate 			ah0dbg(("ah_set_usetime: "
16000Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
16010Sstevel@tonic-gate 			sadb_set_usetime(inassoc);
16020Sstevel@tonic-gate 			return;
16030Sstevel@tonic-gate 		}
16040Sstevel@tonic-gate 	} else {
16050Sstevel@tonic-gate 		outassoc = assoc;
1606564Ssommerfe 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
16070Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
16080Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
16090Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
16100Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
16110Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
16120Sstevel@tonic-gate 		if (inassoc == NULL) {
16130Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
16140Sstevel@tonic-gate 			ah0dbg(("ah_set_usetime: "
16150Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
16160Sstevel@tonic-gate 			sadb_set_usetime(outassoc);
16170Sstevel@tonic-gate 			return;
16180Sstevel@tonic-gate 		}
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	/* Update usetime on both. */
16220Sstevel@tonic-gate 	sadb_set_usetime(inassoc);
16230Sstevel@tonic-gate 	sadb_set_usetime(outassoc);
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	/*
16260Sstevel@tonic-gate 	 * REFRELE any peer SA.
16270Sstevel@tonic-gate 	 *
16280Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
16290Sstevel@tonic-gate 	 * them in { }.
16300Sstevel@tonic-gate 	 */
16310Sstevel@tonic-gate 	if (inbound) {
16320Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
16330Sstevel@tonic-gate 	} else {
16340Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
16350Sstevel@tonic-gate 	}
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate 
163810934Ssommerfeld@sun.com /* Refactor me */
16390Sstevel@tonic-gate /*
16400Sstevel@tonic-gate  * Add a number of bytes to what the SA has protected so far.  Return
16410Sstevel@tonic-gate  * B_TRUE if the SA can still protect that many bytes.
16420Sstevel@tonic-gate  *
16430Sstevel@tonic-gate  * Caller must REFRELE the passed-in assoc.  This function must REFRELE
16440Sstevel@tonic-gate  * any obtained peer SA.
16450Sstevel@tonic-gate  */
16460Sstevel@tonic-gate static boolean_t
ah_age_bytes(ipsa_t * assoc,uint64_t bytes,boolean_t inbound)16470Sstevel@tonic-gate ah_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
16480Sstevel@tonic-gate {
16490Sstevel@tonic-gate 	ipsa_t *inassoc, *outassoc;
16500Sstevel@tonic-gate 	isaf_t *bucket;
16510Sstevel@tonic-gate 	boolean_t inrc, outrc, isv6;
16520Sstevel@tonic-gate 	sadb_t *sp;
16530Sstevel@tonic-gate 	int outhash;
16543448Sdh155122 	netstack_t	*ns = assoc->ipsa_netstack;
16553448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	/* No peer?  No problem! */
16580Sstevel@tonic-gate 	if (!assoc->ipsa_haspeer) {
16593448Sdh155122 		return (sadb_age_bytes(ahstack->ah_pfkey_q, assoc, bytes,
16600Sstevel@tonic-gate 		    B_TRUE));
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	/*
16640Sstevel@tonic-gate 	 * Otherwise, we want to grab both the original assoc and its peer.
16650Sstevel@tonic-gate 	 * There might be a race for this, but if it's a real race, two
16660Sstevel@tonic-gate 	 * expire messages may occur.  We limit this by only sending the
16670Sstevel@tonic-gate 	 * expire message on one of the peers, we'll pick the inbound
16680Sstevel@tonic-gate 	 * arbitrarily.
16690Sstevel@tonic-gate 	 *
16700Sstevel@tonic-gate 	 * If we need tight synchronization on the peer SA, then we need to
16710Sstevel@tonic-gate 	 * reconsider.
16720Sstevel@tonic-gate 	 */
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	/* Pick v4/v6 bucket based on addrfam. */
16750Sstevel@tonic-gate 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
16760Sstevel@tonic-gate 	if (isv6) {
16773448Sdh155122 		sp = &ahstack->ah_sadb.s_v6;
16780Sstevel@tonic-gate 	} else {
16793448Sdh155122 		sp = &ahstack->ah_sadb.s_v4;
16800Sstevel@tonic-gate 		ASSERT(assoc->ipsa_addrfam == AF_INET);
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate 	if (inbound) {
16830Sstevel@tonic-gate 		inassoc = assoc;
16840Sstevel@tonic-gate 		if (isv6)
16854987Sdanmcd 			outhash = OUTBOUND_HASH_V6(sp,
16864987Sdanmcd 			    *((in6_addr_t *)&inassoc->ipsa_dstaddr));
16870Sstevel@tonic-gate 		else
16884987Sdanmcd 			outhash = OUTBOUND_HASH_V4(sp,
16894987Sdanmcd 			    *((ipaddr_t *)&inassoc->ipsa_dstaddr));
16900Sstevel@tonic-gate 		bucket = &sp->sdb_of[outhash];
16910Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
16920Sstevel@tonic-gate 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
16930Sstevel@tonic-gate 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
16940Sstevel@tonic-gate 		    inassoc->ipsa_addrfam);
16950Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
16960Sstevel@tonic-gate 		if (outassoc == NULL) {
16970Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
16980Sstevel@tonic-gate 			ah0dbg(("ah_age_bytes: "
16990Sstevel@tonic-gate 			    "can't find peer for inbound.\n"));
17003448Sdh155122 			return (sadb_age_bytes(ahstack->ah_pfkey_q, inassoc,
17010Sstevel@tonic-gate 			    bytes, B_TRUE));
17020Sstevel@tonic-gate 		}
17030Sstevel@tonic-gate 	} else {
17040Sstevel@tonic-gate 		outassoc = assoc;
1705564Ssommerfe 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
17060Sstevel@tonic-gate 		mutex_enter(&bucket->isaf_lock);
17070Sstevel@tonic-gate 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
17080Sstevel@tonic-gate 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
17090Sstevel@tonic-gate 		    outassoc->ipsa_addrfam);
17100Sstevel@tonic-gate 		mutex_exit(&bucket->isaf_lock);
17110Sstevel@tonic-gate 		if (inassoc == NULL) {
17120Sstevel@tonic-gate 			/* Q: Do we wish to set haspeer == B_FALSE? */
17130Sstevel@tonic-gate 			ah0dbg(("ah_age_bytes: "
17140Sstevel@tonic-gate 			    "can't find peer for outbound.\n"));
17153448Sdh155122 			return (sadb_age_bytes(ahstack->ah_pfkey_q, outassoc,
17160Sstevel@tonic-gate 			    bytes, B_TRUE));
17170Sstevel@tonic-gate 		}
17180Sstevel@tonic-gate 	}
17190Sstevel@tonic-gate 
17203448Sdh155122 	inrc = sadb_age_bytes(ahstack->ah_pfkey_q, inassoc, bytes, B_TRUE);
17213448Sdh155122 	outrc = sadb_age_bytes(ahstack->ah_pfkey_q, outassoc, bytes, B_FALSE);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	/*
17240Sstevel@tonic-gate 	 * REFRELE any peer SA.
17250Sstevel@tonic-gate 	 *
17260Sstevel@tonic-gate 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
17270Sstevel@tonic-gate 	 * them in { }.
17280Sstevel@tonic-gate 	 */
17290Sstevel@tonic-gate 	if (inbound) {
17300Sstevel@tonic-gate 		IPSA_REFRELE(outassoc);
17310Sstevel@tonic-gate 	} else {
17320Sstevel@tonic-gate 		IPSA_REFRELE(inassoc);
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	return (inrc && outrc);
17360Sstevel@tonic-gate }
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate /*
17390Sstevel@tonic-gate  * Perform the really difficult work of inserting the proposed situation.
17400Sstevel@tonic-gate  * Called while holding the algorithm lock.
17410Sstevel@tonic-gate  */
17420Sstevel@tonic-gate static void
ah_insert_prop(sadb_prop_t * prop,ipsacq_t * acqrec,uint_t combs,netstack_t * ns)174311042SErik.Nordmark@Sun.COM ah_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs,
174411042SErik.Nordmark@Sun.COM     netstack_t *ns)
17450Sstevel@tonic-gate {
17460Sstevel@tonic-gate 	sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
17470Sstevel@tonic-gate 	ipsec_action_t *ap;
17480Sstevel@tonic-gate 	ipsec_prot_t *prot;
174911042SErik.Nordmark@Sun.COM 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
175011042SErik.Nordmark@Sun.COM 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
175111042SErik.Nordmark@Sun.COM 
17523448Sdh155122 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
17533448Sdh155122 
17540Sstevel@tonic-gate 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
17550Sstevel@tonic-gate 	prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
17560Sstevel@tonic-gate 	*(uint32_t *)(&prop->sadb_prop_replay) = 0;	/* Quick zero-out! */
17570Sstevel@tonic-gate 
17583448Sdh155122 	prop->sadb_prop_replay = ahstack->ipsecah_replay_size;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	/*
17610Sstevel@tonic-gate 	 * Based upon algorithm properties, and what-not, prioritize a
176211042SErik.Nordmark@Sun.COM 	 * proposal, based on the ordering of the AH algorithms in the
176311042SErik.Nordmark@Sun.COM 	 * alternatives in the policy rule or socket that was placed
176411042SErik.Nordmark@Sun.COM 	 * in the acquire record.
17650Sstevel@tonic-gate 	 */
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	for (ap = acqrec->ipsacq_act; ap != NULL;
17680Sstevel@tonic-gate 	    ap = ap->ipa_next) {
17690Sstevel@tonic-gate 		ipsec_alginfo_t *aalg;
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 		if ((ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY) ||
17720Sstevel@tonic-gate 		    (!ap->ipa_act.ipa_apply.ipp_use_ah))
17730Sstevel@tonic-gate 			continue;
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 		prot = &ap->ipa_act.ipa_apply;
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 		ASSERT(prot->ipp_auth_alg > 0);
17780Sstevel@tonic-gate 
17793448Sdh155122 		aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
17803448Sdh155122 		    [prot->ipp_auth_alg];
17810Sstevel@tonic-gate 		if (aalg == NULL || !ALG_VALID(aalg))
17820Sstevel@tonic-gate 			continue;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 		/* XXX check aalg for duplicates??.. */
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 		comb->sadb_comb_flags = 0;
17870Sstevel@tonic-gate 		comb->sadb_comb_reserved = 0;
17880Sstevel@tonic-gate 		comb->sadb_comb_encrypt = 0;
17890Sstevel@tonic-gate 		comb->sadb_comb_encrypt_minbits = 0;
17900Sstevel@tonic-gate 		comb->sadb_comb_encrypt_maxbits = 0;
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 		comb->sadb_comb_auth = aalg->alg_id;
17932751Sdanmcd 		comb->sadb_comb_auth_minbits =
17942751Sdanmcd 		    MAX(prot->ipp_ah_minbits, aalg->alg_ef_minbits);
17952751Sdanmcd 		comb->sadb_comb_auth_maxbits =
17962751Sdanmcd 		    MIN(prot->ipp_ah_maxbits, aalg->alg_ef_maxbits);
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		/*
17990Sstevel@tonic-gate 		 * The following may be based on algorithm
18000Sstevel@tonic-gate 		 * properties, but in the meantime, we just pick
18010Sstevel@tonic-gate 		 * some good, sensible numbers.  Key mgmt. can
18020Sstevel@tonic-gate 		 * (and perhaps should) be the place to finalize
18030Sstevel@tonic-gate 		 * such decisions.
18040Sstevel@tonic-gate 		 */
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 		/*
18070Sstevel@tonic-gate 		 * No limits on allocations, since we really don't
18080Sstevel@tonic-gate 		 * support that concept currently.
18090Sstevel@tonic-gate 		 */
18100Sstevel@tonic-gate 		comb->sadb_comb_soft_allocations = 0;
18110Sstevel@tonic-gate 		comb->sadb_comb_hard_allocations = 0;
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 		/*
18140Sstevel@tonic-gate 		 * These may want to come from policy rule..
18150Sstevel@tonic-gate 		 */
18163448Sdh155122 		comb->sadb_comb_soft_bytes =
18173448Sdh155122 		    ahstack->ipsecah_default_soft_bytes;
18183448Sdh155122 		comb->sadb_comb_hard_bytes =
18193448Sdh155122 		    ahstack->ipsecah_default_hard_bytes;
18203448Sdh155122 		comb->sadb_comb_soft_addtime =
18213448Sdh155122 		    ahstack->ipsecah_default_soft_addtime;
18223448Sdh155122 		comb->sadb_comb_hard_addtime =
18233448Sdh155122 		    ahstack->ipsecah_default_hard_addtime;
18243448Sdh155122 		comb->sadb_comb_soft_usetime =
18253448Sdh155122 		    ahstack->ipsecah_default_soft_usetime;
18263448Sdh155122 		comb->sadb_comb_hard_usetime =
18273448Sdh155122 		    ahstack->ipsecah_default_hard_usetime;
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
18300Sstevel@tonic-gate 		if (--combs == 0)
18310Sstevel@tonic-gate 			return;	/* out of space.. */
18320Sstevel@tonic-gate 		comb++;
18330Sstevel@tonic-gate 	}
18340Sstevel@tonic-gate }
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate /*
18370Sstevel@tonic-gate  * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
18380Sstevel@tonic-gate  */
18390Sstevel@tonic-gate static void
ah_send_acquire(ipsacq_t * acqrec,mblk_t * extended,netstack_t * ns)18403448Sdh155122 ah_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns)
18410Sstevel@tonic-gate {
18423055Sdanmcd 	uint_t combs;
18430Sstevel@tonic-gate 	sadb_msg_t *samsg;
18440Sstevel@tonic-gate 	sadb_prop_t *prop;
18453055Sdanmcd 	mblk_t *pfkeymp, *msgmp;
18463448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
18473448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
18483448Sdh155122 
18493448Sdh155122 	AH_BUMP_STAT(ahstack, acquire_requests);
18503448Sdh155122 
18517073Spwernau 	if (ahstack->ah_pfkey_q == NULL) {
18527073Spwernau 		mutex_exit(&acqrec->ipsacq_lock);
18533055Sdanmcd 		return;
18547073Spwernau 	}
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	/* Set up ACQUIRE. */
18573448Sdh155122 	pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_AH,
18583448Sdh155122 	    ns->netstack_ipsec);
18593055Sdanmcd 	if (pfkeymp == NULL) {
18600Sstevel@tonic-gate 		ah0dbg(("sadb_setup_acquire failed.\n"));
18617073Spwernau 		mutex_exit(&acqrec->ipsacq_lock);
18623055Sdanmcd 		return;
18630Sstevel@tonic-gate 	}
18643448Sdh155122 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
18653448Sdh155122 	combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
18663055Sdanmcd 	msgmp = pfkeymp->b_cont;
18673055Sdanmcd 	samsg = (sadb_msg_t *)(msgmp->b_rptr);
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	/* Insert proposal here. */
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
187211042SErik.Nordmark@Sun.COM 	ah_insert_prop(prop, acqrec, combs, ns);
18730Sstevel@tonic-gate 	samsg->sadb_msg_len += prop->sadb_prop_len;
18740Sstevel@tonic-gate 	msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
18750Sstevel@tonic-gate 
18763448Sdh155122 	mutex_exit(&ipss->ipsec_alg_lock);
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	/*
18790Sstevel@tonic-gate 	 * Must mutex_exit() before sending PF_KEY message up, in
18800Sstevel@tonic-gate 	 * order to avoid recursive mutex_enter() if there are no registered
18810Sstevel@tonic-gate 	 * listeners.
18820Sstevel@tonic-gate 	 *
18830Sstevel@tonic-gate 	 * Once I've sent the message, I'm cool anyway.
18840Sstevel@tonic-gate 	 */
18850Sstevel@tonic-gate 	mutex_exit(&acqrec->ipsacq_lock);
18863055Sdanmcd 	if (extended != NULL) {
18873448Sdh155122 		putnext(ahstack->ah_pfkey_q, extended);
18880Sstevel@tonic-gate 	}
18893448Sdh155122 	putnext(ahstack->ah_pfkey_q, pfkeymp);
18900Sstevel@tonic-gate }
18910Sstevel@tonic-gate 
189210934Ssommerfeld@sun.com /* Refactor me */
18930Sstevel@tonic-gate /*
18940Sstevel@tonic-gate  * Handle the SADB_GETSPI message.  Create a larval SA.
18950Sstevel@tonic-gate  */
18960Sstevel@tonic-gate static void
ah_getspi(mblk_t * mp,keysock_in_t * ksi,ipsecah_stack_t * ahstack)18973448Sdh155122 ah_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
18980Sstevel@tonic-gate {
18990Sstevel@tonic-gate 	ipsa_t *newbie, *target;
19000Sstevel@tonic-gate 	isaf_t *outbound, *inbound;
19010Sstevel@tonic-gate 	int rc, diagnostic;
19020Sstevel@tonic-gate 	sadb_sa_t *assoc;
19030Sstevel@tonic-gate 	keysock_out_t *kso;
19040Sstevel@tonic-gate 	uint32_t newspi;
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	/*
19070Sstevel@tonic-gate 	 * Randomly generate a proposed SPI value.
19080Sstevel@tonic-gate 	 */
19097749SThejaswini.Singarajipura@Sun.COM 	if (cl_inet_getspi != NULL) {
19108392SHuafeng.Lv@Sun.COM 		cl_inet_getspi(ahstack->ipsecah_netstack->netstack_stackid,
19118392SHuafeng.Lv@Sun.COM 		    IPPROTO_AH, (uint8_t *)&newspi, sizeof (uint32_t), NULL);
19127749SThejaswini.Singarajipura@Sun.COM 	} else {
19137749SThejaswini.Singarajipura@Sun.COM 		(void) random_get_pseudo_bytes((uint8_t *)&newspi,
19147749SThejaswini.Singarajipura@Sun.COM 		    sizeof (uint32_t));
19157749SThejaswini.Singarajipura@Sun.COM 	}
19163448Sdh155122 	newbie = sadb_getspi(ksi, newspi, &diagnostic,
19177749SThejaswini.Singarajipura@Sun.COM 	    ahstack->ipsecah_netstack, IPPROTO_AH);
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	if (newbie == NULL) {
19203448Sdh155122 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM, diagnostic,
19210Sstevel@tonic-gate 		    ksi->ks_in_serial);
19220Sstevel@tonic-gate 		return;
19230Sstevel@tonic-gate 	} else if (newbie == (ipsa_t *)-1) {
19243448Sdh155122 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL, diagnostic,
19250Sstevel@tonic-gate 		    ksi->ks_in_serial);
19260Sstevel@tonic-gate 		return;
19270Sstevel@tonic-gate 	}
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	/*
19300Sstevel@tonic-gate 	 * XXX - We may randomly collide.  We really should recover from this.
19310Sstevel@tonic-gate 	 *	 Unfortunately, that could require spending way-too-much-time
19320Sstevel@tonic-gate 	 *	 in here.  For now, let the user retry.
19330Sstevel@tonic-gate 	 */
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	if (newbie->ipsa_addrfam == AF_INET6) {
19363448Sdh155122 		outbound = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6,
1937564Ssommerfe 		    *(uint32_t *)(newbie->ipsa_dstaddr));
19383448Sdh155122 		inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v6,
19393448Sdh155122 		    newbie->ipsa_spi);
19400Sstevel@tonic-gate 	} else {
19413448Sdh155122 		outbound = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4,
1942564Ssommerfe 		    *(uint32_t *)(newbie->ipsa_dstaddr));
19433448Sdh155122 		inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v4,
19443448Sdh155122 		    newbie->ipsa_spi);
19450Sstevel@tonic-gate 	}
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	mutex_enter(&outbound->isaf_lock);
19480Sstevel@tonic-gate 	mutex_enter(&inbound->isaf_lock);
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	/*
19510Sstevel@tonic-gate 	 * Check for collisions (i.e. did sadb_getspi() return with something
19520Sstevel@tonic-gate 	 * that already exists?).
19530Sstevel@tonic-gate 	 *
19540Sstevel@tonic-gate 	 * Try outbound first.  Even though SADB_GETSPI is traditionally
19550Sstevel@tonic-gate 	 * for inbound SAs, you never know what a user might do.
19560Sstevel@tonic-gate 	 */
19570Sstevel@tonic-gate 	target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
19580Sstevel@tonic-gate 	    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
19590Sstevel@tonic-gate 	if (target == NULL) {
19600Sstevel@tonic-gate 		target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
19610Sstevel@tonic-gate 		    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
19620Sstevel@tonic-gate 		    newbie->ipsa_addrfam);
19630Sstevel@tonic-gate 	}
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	/*
19660Sstevel@tonic-gate 	 * I don't have collisions elsewhere!
19670Sstevel@tonic-gate 	 * (Nor will I because I'm still holding inbound/outbound locks.)
19680Sstevel@tonic-gate 	 */
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	if (target != NULL) {
19710Sstevel@tonic-gate 		rc = EEXIST;
19720Sstevel@tonic-gate 		IPSA_REFRELE(target);
19730Sstevel@tonic-gate 	} else {
19740Sstevel@tonic-gate 		/*
19750Sstevel@tonic-gate 		 * sadb_insertassoc() also checks for collisions, so
19760Sstevel@tonic-gate 		 * if there's a colliding larval entry, rc will be set
19770Sstevel@tonic-gate 		 * to EEXIST.
19780Sstevel@tonic-gate 		 */
19790Sstevel@tonic-gate 		rc = sadb_insertassoc(newbie, inbound);
19804987Sdanmcd 		newbie->ipsa_hardexpiretime = gethrestime_sec();
19813448Sdh155122 		newbie->ipsa_hardexpiretime += ahstack->ipsecah_larval_timeout;
19820Sstevel@tonic-gate 	}
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	/*
19850Sstevel@tonic-gate 	 * Can exit outbound mutex.  Hold inbound until we're done with
19860Sstevel@tonic-gate 	 * newbie.
19870Sstevel@tonic-gate 	 */
19880Sstevel@tonic-gate 	mutex_exit(&outbound->isaf_lock);
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 	if (rc != 0) {
19910Sstevel@tonic-gate 		mutex_exit(&inbound->isaf_lock);
19920Sstevel@tonic-gate 		IPSA_REFRELE(newbie);
19933448Sdh155122 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, rc,
19943448Sdh155122 		    SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
19950Sstevel@tonic-gate 		return;
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 	/* Can write here because I'm still holding the bucket lock. */
19990Sstevel@tonic-gate 	newbie->ipsa_type = SADB_SATYPE_AH;
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	/*
20020Sstevel@tonic-gate 	 * Construct successful return message.  We have one thing going
20030Sstevel@tonic-gate 	 * for us in PF_KEY v2.  That's the fact that
20040Sstevel@tonic-gate 	 *	sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
20050Sstevel@tonic-gate 	 */
20060Sstevel@tonic-gate 	assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
20070Sstevel@tonic-gate 	assoc->sadb_sa_exttype = SADB_EXT_SA;
20080Sstevel@tonic-gate 	assoc->sadb_sa_spi = newbie->ipsa_spi;
20090Sstevel@tonic-gate 	*((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
20100Sstevel@tonic-gate 	mutex_exit(&inbound->isaf_lock);
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 	/* Convert KEYSOCK_IN to KEYSOCK_OUT. */
20130Sstevel@tonic-gate 	kso = (keysock_out_t *)ksi;
20140Sstevel@tonic-gate 	kso->ks_out_len = sizeof (*kso);
20150Sstevel@tonic-gate 	kso->ks_out_serial = ksi->ks_in_serial;
20160Sstevel@tonic-gate 	kso->ks_out_type = KEYSOCK_OUT;
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 	/*
20190Sstevel@tonic-gate 	 * Can safely putnext() to ah_pfkey_q, because this is a turnaround
20200Sstevel@tonic-gate 	 * from the ah_pfkey_q.
20210Sstevel@tonic-gate 	 */
20223448Sdh155122 	putnext(ahstack->ah_pfkey_q, mp);
20230Sstevel@tonic-gate }
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate /*
20260Sstevel@tonic-gate  * IPv6 sends up the ICMP errors for validation and the removal of the AH
20270Sstevel@tonic-gate  * header.
202811042SErik.Nordmark@Sun.COM  * If succesful, the mp has been modified to not include the AH header so
202911042SErik.Nordmark@Sun.COM  * that the caller can fanout to the ULP's icmp error handler.
20300Sstevel@tonic-gate  */
203111042SErik.Nordmark@Sun.COM static mblk_t *
ah_icmp_error_v6(mblk_t * mp,ip_recv_attr_t * ira,ipsecah_stack_t * ahstack)203211042SErik.Nordmark@Sun.COM ah_icmp_error_v6(mblk_t *mp, ip_recv_attr_t *ira, ipsecah_stack_t *ahstack)
20330Sstevel@tonic-gate {
20340Sstevel@tonic-gate 	ip6_t *ip6h, *oip6h;
20350Sstevel@tonic-gate 	uint16_t hdr_length, ah_length;
20360Sstevel@tonic-gate 	uint8_t *nexthdrp;
20370Sstevel@tonic-gate 	ah_t *ah;
20380Sstevel@tonic-gate 	icmp6_t *icmp6;
20390Sstevel@tonic-gate 	isaf_t *isaf;
20400Sstevel@tonic-gate 	ipsa_t *assoc;
20410Sstevel@tonic-gate 	uint8_t *post_ah_ptr;
20423448Sdh155122 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	/*
20450Sstevel@tonic-gate 	 * Eat the cost of a pullupmsg() for now.  It makes the rest of this
20460Sstevel@tonic-gate 	 * code far less convoluted.
20470Sstevel@tonic-gate 	 */
20480Sstevel@tonic-gate 	if (!pullupmsg(mp, -1) ||
20490Sstevel@tonic-gate 	    !ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, &hdr_length,
20504987Sdanmcd 	    &nexthdrp) ||
20510Sstevel@tonic-gate 	    mp->b_rptr + hdr_length + sizeof (icmp6_t) + sizeof (ip6_t) +
20520Sstevel@tonic-gate 	    sizeof (ah_t) > mp->b_wptr) {
20533448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
205411042SErik.Nordmark@Sun.COM 		ip_drop_packet(mp, B_TRUE, ira->ira_ill,
20553448Sdh155122 		    DROPPER(ipss, ipds_ah_nomem),
20563448Sdh155122 		    &ahstack->ah_dropper);
205711042SErik.Nordmark@Sun.COM 		return (NULL);
20580Sstevel@tonic-gate 	}
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 	oip6h = (ip6_t *)mp->b_rptr;
20610Sstevel@tonic-gate 	icmp6 = (icmp6_t *)((uint8_t *)oip6h + hdr_length);
20620Sstevel@tonic-gate 	ip6h = (ip6_t *)(icmp6 + 1);
20630Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) {
20643448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
206511042SErik.Nordmark@Sun.COM 		ip_drop_packet(mp, B_TRUE, ira->ira_ill,
20663448Sdh155122 		    DROPPER(ipss, ipds_ah_bad_v6_hdrs),
20673448Sdh155122 		    &ahstack->ah_dropper);
206811042SErik.Nordmark@Sun.COM 		return (NULL);
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 	ah = (ah_t *)((uint8_t *)ip6h + hdr_length);
20710Sstevel@tonic-gate 
20723448Sdh155122 	isaf = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6, ip6h->ip6_dst);
20730Sstevel@tonic-gate 	mutex_enter(&isaf->isaf_lock);
20740Sstevel@tonic-gate 	assoc = ipsec_getassocbyspi(isaf, ah->ah_spi,
20750Sstevel@tonic-gate 	    (uint32_t *)&ip6h->ip6_src, (uint32_t *)&ip6h->ip6_dst, AF_INET6);
20760Sstevel@tonic-gate 	mutex_exit(&isaf->isaf_lock);
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	if (assoc == NULL) {
20793448Sdh155122 		IP_AH_BUMP_STAT(ipss, lookup_failure);
20803448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
20813448Sdh155122 		if (ahstack->ipsecah_log_unknown_spi) {
20820Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
20830Sstevel@tonic-gate 			    SL_CONSOLE | SL_WARN | SL_ERROR,
20840Sstevel@tonic-gate 			    "Bad ICMP message - No association for the "
20850Sstevel@tonic-gate 			    "attached AH header whose spi is 0x%x, "
20860Sstevel@tonic-gate 			    "sender is 0x%x\n",
20873448Sdh155122 			    ah->ah_spi, &oip6h->ip6_src, AF_INET6,
20883448Sdh155122 			    ahstack->ipsecah_netstack);
20890Sstevel@tonic-gate 		}
209011042SErik.Nordmark@Sun.COM 		ip_drop_packet(mp, B_TRUE, ira->ira_ill,
20913448Sdh155122 		    DROPPER(ipss, ipds_ah_no_sa),
20923448Sdh155122 		    &ahstack->ah_dropper);
209311042SErik.Nordmark@Sun.COM 		return (NULL);
20940Sstevel@tonic-gate 	}
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 	IPSA_REFRELE(assoc);
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	/*
20990Sstevel@tonic-gate 	 * There seems to be a valid association. If there is enough of AH
21000Sstevel@tonic-gate 	 * header remove it, otherwise bail.  One could check whether it has
21010Sstevel@tonic-gate 	 * complete AH header plus 8 bytes but it does not make sense if an
21020Sstevel@tonic-gate 	 * icmp error is returned for ICMP messages e.g ICMP time exceeded,
21030Sstevel@tonic-gate 	 * that are being sent up. Let the caller figure out.
21040Sstevel@tonic-gate 	 *
21050Sstevel@tonic-gate 	 * NOTE: ah_length is the number of 32 bit words minus 2.
21060Sstevel@tonic-gate 	 */
21070Sstevel@tonic-gate 	ah_length = (ah->ah_length << 2) + 8;
21080Sstevel@tonic-gate 	post_ah_ptr = (uint8_t *)ah + ah_length;
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	if (post_ah_ptr > mp->b_wptr) {
21113448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
211211042SErik.Nordmark@Sun.COM 		ip_drop_packet(mp, B_TRUE, ira->ira_ill,
21133448Sdh155122 		    DROPPER(ipss, ipds_ah_bad_length),
21143448Sdh155122 		    &ahstack->ah_dropper);
211511042SErik.Nordmark@Sun.COM 		return (NULL);
21160Sstevel@tonic-gate 	}
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - ah_length);
21190Sstevel@tonic-gate 	*nexthdrp = ah->ah_nexthdr;
21200Sstevel@tonic-gate 	ovbcopy(post_ah_ptr, ah,
21210Sstevel@tonic-gate 	    (size_t)((uintptr_t)mp->b_wptr - (uintptr_t)post_ah_ptr));
21220Sstevel@tonic-gate 	mp->b_wptr -= ah_length;
212311042SErik.Nordmark@Sun.COM 
212411042SErik.Nordmark@Sun.COM 	return (mp);
21250Sstevel@tonic-gate }
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate /*
21280Sstevel@tonic-gate  * IP sends up the ICMP errors for validation and the removal of
21290Sstevel@tonic-gate  * the AH header.
213011042SErik.Nordmark@Sun.COM  * If succesful, the mp has been modified to not include the AH header so
213111042SErik.Nordmark@Sun.COM  * that the caller can fanout to the ULP's icmp error handler.
21320Sstevel@tonic-gate  */
213311042SErik.Nordmark@Sun.COM static mblk_t *
ah_icmp_error_v4(mblk_t * mp,ip_recv_attr_t * ira,ipsecah_stack_t * ahstack)213411042SErik.Nordmark@Sun.COM ah_icmp_error_v4(mblk_t *mp, ip_recv_attr_t *ira, ipsecah_stack_t *ahstack)
21350Sstevel@tonic-gate {
21360Sstevel@tonic-gate 	mblk_t *mp1;
21370Sstevel@tonic-gate 	icmph_t *icmph;
21380Sstevel@tonic-gate 	int iph_hdr_length;
21390Sstevel@tonic-gate 	int hdr_length;
21400Sstevel@tonic-gate 	isaf_t *hptr;
21410Sstevel@tonic-gate 	ipsa_t *assoc;
21420Sstevel@tonic-gate 	int ah_length;
21430Sstevel@tonic-gate 	ipha_t *ipha;
21440Sstevel@tonic-gate 	ipha_t *oipha;
21450Sstevel@tonic-gate 	ah_t *ah;
21460Sstevel@tonic-gate 	uint32_t length;
21470Sstevel@tonic-gate 	int alloc_size;
21480Sstevel@tonic-gate 	uint8_t nexthdr;
21493448Sdh155122 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	oipha = ipha = (ipha_t *)mp->b_rptr;
21520Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
21530Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
21560Sstevel@tonic-gate 	hdr_length = IPH_HDR_LENGTH(ipha);
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	/*
21590Sstevel@tonic-gate 	 * See if we have enough to locate the SPI
21600Sstevel@tonic-gate 	 */
21610Sstevel@tonic-gate 	if ((uchar_t *)ipha + hdr_length + 8 > mp->b_wptr) {
21620Sstevel@tonic-gate 		if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 8 -
21634987Sdanmcd 		    mp->b_rptr)) {
21643448Sdh155122 			ipsec_rl_strlog(ahstack->ipsecah_netstack,
21653448Sdh155122 			    info.mi_idnum, 0, 0,
21660Sstevel@tonic-gate 			    SL_WARN | SL_ERROR,
21670Sstevel@tonic-gate 			    "ICMP error: Small AH header\n");
21683448Sdh155122 			IP_AH_BUMP_STAT(ipss, in_discards);
216911042SErik.Nordmark@Sun.COM 			ip_drop_packet(mp, B_TRUE, ira->ira_ill,
21703448Sdh155122 			    DROPPER(ipss, ipds_ah_bad_length),
21713448Sdh155122 			    &ahstack->ah_dropper);
217211042SErik.Nordmark@Sun.COM 			return (NULL);
21730Sstevel@tonic-gate 		}
21740Sstevel@tonic-gate 		icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
21750Sstevel@tonic-gate 		ipha = (ipha_t *)&icmph[1];
21760Sstevel@tonic-gate 	}
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 	ah = (ah_t *)((uint8_t *)ipha + hdr_length);
21790Sstevel@tonic-gate 	nexthdr = ah->ah_nexthdr;
21800Sstevel@tonic-gate 
21813448Sdh155122 	hptr = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4, ipha->ipha_dst);
21820Sstevel@tonic-gate 	mutex_enter(&hptr->isaf_lock);
21830Sstevel@tonic-gate 	assoc = ipsec_getassocbyspi(hptr, ah->ah_spi,
21840Sstevel@tonic-gate 	    (uint32_t *)&ipha->ipha_src, (uint32_t *)&ipha->ipha_dst, AF_INET);
21850Sstevel@tonic-gate 	mutex_exit(&hptr->isaf_lock);
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	if (assoc == NULL) {
21883448Sdh155122 		IP_AH_BUMP_STAT(ipss, lookup_failure);
21893448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
21903448Sdh155122 		if (ahstack->ipsecah_log_unknown_spi) {
21910Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
21920Sstevel@tonic-gate 			    SL_CONSOLE | SL_WARN | SL_ERROR,
21930Sstevel@tonic-gate 			    "Bad ICMP message - No association for the "
21940Sstevel@tonic-gate 			    "attached AH header whose spi is 0x%x, "
21950Sstevel@tonic-gate 			    "sender is 0x%x\n",
21963448Sdh155122 			    ah->ah_spi, &oipha->ipha_src, AF_INET,
21973448Sdh155122 			    ahstack->ipsecah_netstack);
21980Sstevel@tonic-gate 		}
219911042SErik.Nordmark@Sun.COM 		ip_drop_packet(mp, B_TRUE, ira->ira_ill,
22003448Sdh155122 		    DROPPER(ipss, ipds_ah_no_sa),
22013448Sdh155122 		    &ahstack->ah_dropper);
220211042SErik.Nordmark@Sun.COM 		return (NULL);
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	IPSA_REFRELE(assoc);
22060Sstevel@tonic-gate 	/*
22070Sstevel@tonic-gate 	 * There seems to be a valid association. If there
22080Sstevel@tonic-gate 	 * is enough of AH header remove it, otherwise remove
22090Sstevel@tonic-gate 	 * as much as possible and send it back. One could check
22100Sstevel@tonic-gate 	 * whether it has complete AH header plus 8 bytes but it
22110Sstevel@tonic-gate 	 * does not make sense if an icmp error is returned for
22120Sstevel@tonic-gate 	 * ICMP messages e.g ICMP time exceeded, that are being
22130Sstevel@tonic-gate 	 * sent up. Let the caller figure out.
22140Sstevel@tonic-gate 	 *
22150Sstevel@tonic-gate 	 * NOTE: ah_length is the number of 32 bit words minus 2.
22160Sstevel@tonic-gate 	 */
22170Sstevel@tonic-gate 	ah_length = (ah->ah_length << 2) + 8;
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 	if ((uchar_t *)ipha + hdr_length + ah_length > mp->b_wptr) {
22200Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
22210Sstevel@tonic-gate 			/*
22220Sstevel@tonic-gate 			 * There is nothing to pullup. Just remove as
22230Sstevel@tonic-gate 			 * much as possible. This is a common case for
22240Sstevel@tonic-gate 			 * IPV4.
22250Sstevel@tonic-gate 			 */
22260Sstevel@tonic-gate 			ah_length = (mp->b_wptr - ((uchar_t *)ipha +
22270Sstevel@tonic-gate 			    hdr_length));
22280Sstevel@tonic-gate 			goto done;
22290Sstevel@tonic-gate 		}
22300Sstevel@tonic-gate 		/* Pullup the full ah header */
22310Sstevel@tonic-gate 		if (!pullupmsg(mp, (uchar_t *)ah + ah_length - mp->b_rptr)) {
22320Sstevel@tonic-gate 			/*
22330Sstevel@tonic-gate 			 * pullupmsg could have failed if there was not
22340Sstevel@tonic-gate 			 * enough to pullup or memory allocation failed.
22350Sstevel@tonic-gate 			 * We tried hard, give up now.
22360Sstevel@tonic-gate 			 */
22373448Sdh155122 			IP_AH_BUMP_STAT(ipss, in_discards);
223811042SErik.Nordmark@Sun.COM 			ip_drop_packet(mp, B_TRUE, ira->ira_ill,
22393448Sdh155122 			    DROPPER(ipss, ipds_ah_nomem),
22403448Sdh155122 			    &ahstack->ah_dropper);
224111042SErik.Nordmark@Sun.COM 			return (NULL);
22420Sstevel@tonic-gate 		}
22430Sstevel@tonic-gate 		icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
22440Sstevel@tonic-gate 		ipha = (ipha_t *)&icmph[1];
22450Sstevel@tonic-gate 	}
22460Sstevel@tonic-gate done:
22470Sstevel@tonic-gate 	/*
22480Sstevel@tonic-gate 	 * Remove the AH header and change the protocol.
224911042SErik.Nordmark@Sun.COM 	 * Don't update the spi fields in the ip_recv_attr_t
225011042SErik.Nordmark@Sun.COM 	 * as we are called just to validate the
22510Sstevel@tonic-gate 	 * message attached to the ICMP message.
22520Sstevel@tonic-gate 	 *
22530Sstevel@tonic-gate 	 * If we never pulled up since all of the message
22540Sstevel@tonic-gate 	 * is in one single mblk, we can't remove the AH header
22550Sstevel@tonic-gate 	 * by just setting the b_wptr to the beginning of the
22560Sstevel@tonic-gate 	 * AH header. We need to allocate a mblk that can hold
22570Sstevel@tonic-gate 	 * up until the inner IP header and copy them.
22580Sstevel@tonic-gate 	 */
22590Sstevel@tonic-gate 	alloc_size = iph_hdr_length + sizeof (icmph_t) + hdr_length;
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	if ((mp1 = allocb(alloc_size, BPRI_LO)) == NULL) {
22623448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
226311042SErik.Nordmark@Sun.COM 		ip_drop_packet(mp, B_TRUE, ira->ira_ill,
22643448Sdh155122 		    DROPPER(ipss, ipds_ah_nomem),
22653448Sdh155122 		    &ahstack->ah_dropper);
226611042SErik.Nordmark@Sun.COM 		return (NULL);
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate 	bcopy(mp->b_rptr, mp1->b_rptr, alloc_size);
22690Sstevel@tonic-gate 	mp1->b_wptr += alloc_size;
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	/*
22720Sstevel@tonic-gate 	 * Skip whatever we have copied and as much of AH header
22730Sstevel@tonic-gate 	 * possible. If we still have something left in the original
22740Sstevel@tonic-gate 	 * message, tag on.
22750Sstevel@tonic-gate 	 */
22760Sstevel@tonic-gate 	mp->b_rptr = (uchar_t *)ipha + hdr_length + ah_length;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	if (mp->b_rptr != mp->b_wptr) {
22790Sstevel@tonic-gate 		mp1->b_cont = mp;
22800Sstevel@tonic-gate 	} else {
22810Sstevel@tonic-gate 		if (mp->b_cont != NULL)
22820Sstevel@tonic-gate 			mp1->b_cont = mp->b_cont;
22830Sstevel@tonic-gate 		freeb(mp);
22840Sstevel@tonic-gate 	}
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 	ipha = (ipha_t *)(mp1->b_rptr + iph_hdr_length + sizeof (icmph_t));
22870Sstevel@tonic-gate 	ipha->ipha_protocol = nexthdr;
22880Sstevel@tonic-gate 	length = ntohs(ipha->ipha_length);
22890Sstevel@tonic-gate 	length -= ah_length;
22900Sstevel@tonic-gate 	ipha->ipha_length = htons((uint16_t)length);
22910Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = 0;
22920Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
22930Sstevel@tonic-gate 
229411042SErik.Nordmark@Sun.COM 	return (mp1);
22950Sstevel@tonic-gate }
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate /*
22980Sstevel@tonic-gate  * IP calls this to validate the ICMP errors that
22990Sstevel@tonic-gate  * we got from the network.
23000Sstevel@tonic-gate  */
230111042SErik.Nordmark@Sun.COM mblk_t *
ipsecah_icmp_error(mblk_t * data_mp,ip_recv_attr_t * ira)230211042SErik.Nordmark@Sun.COM ipsecah_icmp_error(mblk_t *data_mp, ip_recv_attr_t *ira)
23030Sstevel@tonic-gate {
230411042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ira->ira_ill->ill_ipst->ips_netstack;
23053448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
23060Sstevel@tonic-gate 
230711042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_IS_IPV4)
230811042SErik.Nordmark@Sun.COM 		return (ah_icmp_error_v4(data_mp, ira, ahstack));
23090Sstevel@tonic-gate 	else
231011042SErik.Nordmark@Sun.COM 		return (ah_icmp_error_v6(data_mp, ira, ahstack));
23110Sstevel@tonic-gate }
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate static int
ah_fix_tlv_options_v6(uint8_t * oi_opt,uint8_t * pi_opt,uint_t ehdrlen,uint8_t hdr_type,boolean_t copy_always)23140Sstevel@tonic-gate ah_fix_tlv_options_v6(uint8_t *oi_opt, uint8_t *pi_opt, uint_t ehdrlen,
23150Sstevel@tonic-gate     uint8_t hdr_type, boolean_t copy_always)
23160Sstevel@tonic-gate {
23170Sstevel@tonic-gate 	uint8_t opt_type;
23180Sstevel@tonic-gate 	uint_t optlen;
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	ASSERT(hdr_type == IPPROTO_DSTOPTS || hdr_type == IPPROTO_HOPOPTS);
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 	/*
23230Sstevel@tonic-gate 	 * Copy the next header and hdr ext. len of the HOP-by-HOP
23240Sstevel@tonic-gate 	 * and Destination option.
23250Sstevel@tonic-gate 	 */
23260Sstevel@tonic-gate 	*pi_opt++ = *oi_opt++;
23270Sstevel@tonic-gate 	*pi_opt++ = *oi_opt++;
23280Sstevel@tonic-gate 	ehdrlen -= 2;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	/*
23310Sstevel@tonic-gate 	 * Now handle all the TLV encoded options.
23320Sstevel@tonic-gate 	 */
23330Sstevel@tonic-gate 	while (ehdrlen != 0) {
23340Sstevel@tonic-gate 		opt_type = *oi_opt;
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 		if (opt_type == IP6OPT_PAD1) {
23370Sstevel@tonic-gate 			optlen = 1;
23380Sstevel@tonic-gate 		} else {
23390Sstevel@tonic-gate 			if (ehdrlen < 2)
23400Sstevel@tonic-gate 				goto bad_opt;
23410Sstevel@tonic-gate 			optlen = 2 + oi_opt[1];
23420Sstevel@tonic-gate 			if (optlen > ehdrlen)
23430Sstevel@tonic-gate 				goto bad_opt;
23440Sstevel@tonic-gate 		}
23450Sstevel@tonic-gate 		if (copy_always || !(opt_type & IP6OPT_MUTABLE)) {
23460Sstevel@tonic-gate 			bcopy(oi_opt, pi_opt, optlen);
23470Sstevel@tonic-gate 		} else {
23480Sstevel@tonic-gate 			if (optlen == 1) {
23490Sstevel@tonic-gate 				*pi_opt = 0;
23500Sstevel@tonic-gate 			} else {
23510Sstevel@tonic-gate 				/*
23520Sstevel@tonic-gate 				 * Copy the type and data length fields.
23530Sstevel@tonic-gate 				 * Zero the option data by skipping
23540Sstevel@tonic-gate 				 * option type and option data len
23550Sstevel@tonic-gate 				 * fields.
23560Sstevel@tonic-gate 				 */
23570Sstevel@tonic-gate 				*pi_opt = *oi_opt;
23580Sstevel@tonic-gate 				*(pi_opt + 1) = *(oi_opt + 1);
23590Sstevel@tonic-gate 				bzero(pi_opt + 2, optlen - 2);
23600Sstevel@tonic-gate 			}
23610Sstevel@tonic-gate 		}
23620Sstevel@tonic-gate 		ehdrlen -= optlen;
23630Sstevel@tonic-gate 		oi_opt += optlen;
23640Sstevel@tonic-gate 		pi_opt += optlen;
23650Sstevel@tonic-gate 	}
23660Sstevel@tonic-gate 	return (0);
23670Sstevel@tonic-gate bad_opt:
23680Sstevel@tonic-gate 	return (-1);
23690Sstevel@tonic-gate }
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate /*
23720Sstevel@tonic-gate  * Construct a pseudo header for AH, processing all the options.
23730Sstevel@tonic-gate  *
23740Sstevel@tonic-gate  * oip6h is the IPv6 header of the incoming or outgoing packet.
23750Sstevel@tonic-gate  * ip6h is the pointer to the pseudo headers IPV6 header. All
23760Sstevel@tonic-gate  * the space needed for the options have been allocated including
23770Sstevel@tonic-gate  * the AH header.
23780Sstevel@tonic-gate  *
23790Sstevel@tonic-gate  * If copy_always is set, all the options that appear before AH are copied
23800Sstevel@tonic-gate  * blindly without checking for IP6OPT_MUTABLE. This is used by
23810Sstevel@tonic-gate  * ah_auth_out_done().  Please refer to that function for details.
23820Sstevel@tonic-gate  *
23830Sstevel@tonic-gate  * NOTE :
23840Sstevel@tonic-gate  *
23850Sstevel@tonic-gate  * *  AH header is never copied in this function even if copy_always
23860Sstevel@tonic-gate  *    is set. It just returns the ah_offset - offset of the AH header
23870Sstevel@tonic-gate  *    and the caller needs to do the copying. This is done so that we
23880Sstevel@tonic-gate  *    don't have pass extra arguments e.g. SA etc. and also,
23890Sstevel@tonic-gate  *    it is not needed when ah_auth_out_done is calling this function.
23900Sstevel@tonic-gate  */
23910Sstevel@tonic-gate static uint_t
ah_fix_phdr_v6(ip6_t * ip6h,ip6_t * oip6h,boolean_t outbound,boolean_t copy_always)23920Sstevel@tonic-gate ah_fix_phdr_v6(ip6_t *ip6h, ip6_t *oip6h, boolean_t outbound,
23930Sstevel@tonic-gate     boolean_t copy_always)
23940Sstevel@tonic-gate {
23950Sstevel@tonic-gate 	uint8_t	*oi_opt;
23960Sstevel@tonic-gate 	uint8_t	*pi_opt;
23970Sstevel@tonic-gate 	uint8_t nexthdr;
23980Sstevel@tonic-gate 	uint8_t *prev_nexthdr;
23990Sstevel@tonic-gate 	ip6_hbh_t *hbhhdr;
24000Sstevel@tonic-gate 	ip6_dest_t *dsthdr = NULL;
24010Sstevel@tonic-gate 	ip6_rthdr0_t *rthdr;
24020Sstevel@tonic-gate 	int ehdrlen;
24030Sstevel@tonic-gate 	ah_t *ah;
24040Sstevel@tonic-gate 	int ret;
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 	/*
24070Sstevel@tonic-gate 	 * In the outbound case for source route, ULP has already moved
24080Sstevel@tonic-gate 	 * the first hop, which is now in ip6_dst. We need to re-arrange
24090Sstevel@tonic-gate 	 * the header to make it look like how it would appear in the
24100Sstevel@tonic-gate 	 * receiver i.e
24110Sstevel@tonic-gate 	 *
24120Sstevel@tonic-gate 	 * Because of ip_massage_options_v6 the header looks like
24130Sstevel@tonic-gate 	 * this :
24140Sstevel@tonic-gate 	 *
24150Sstevel@tonic-gate 	 * ip6_src = S, ip6_dst = I1. followed by I2,I3,D.
24160Sstevel@tonic-gate 	 *
24170Sstevel@tonic-gate 	 * When it reaches the receiver, it would look like
24180Sstevel@tonic-gate 	 *
24190Sstevel@tonic-gate 	 * ip6_src = S, ip6_dst = D. followed by I1,I2,I3.
24200Sstevel@tonic-gate 	 *
24210Sstevel@tonic-gate 	 * NOTE : We assume that there are no problems with the options
24220Sstevel@tonic-gate 	 * as IP should have already checked this.
24230Sstevel@tonic-gate 	 */
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	oi_opt = (uchar_t *)&oip6h[1];
24260Sstevel@tonic-gate 	pi_opt = (uchar_t *)&ip6h[1];
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	/*
24290Sstevel@tonic-gate 	 * We set the prev_nexthdr properly in the pseudo header.
24300Sstevel@tonic-gate 	 * After we finish authentication and come back from the
24310Sstevel@tonic-gate 	 * algorithm module, pseudo header will become the real
24320Sstevel@tonic-gate 	 * IP header.
24330Sstevel@tonic-gate 	 */
24340Sstevel@tonic-gate 	prev_nexthdr = (uint8_t *)&ip6h->ip6_nxt;
24350Sstevel@tonic-gate 	nexthdr = oip6h->ip6_nxt;
24360Sstevel@tonic-gate 	/* Assume IP has already stripped it */
243711042SErik.Nordmark@Sun.COM 	ASSERT(nexthdr != IPPROTO_FRAGMENT);
24380Sstevel@tonic-gate 	ah = NULL;
24390Sstevel@tonic-gate 	dsthdr = NULL;
24400Sstevel@tonic-gate 	for (;;) {
24410Sstevel@tonic-gate 		switch (nexthdr) {
24420Sstevel@tonic-gate 		case IPPROTO_HOPOPTS:
24430Sstevel@tonic-gate 			hbhhdr = (ip6_hbh_t *)oi_opt;
24440Sstevel@tonic-gate 			nexthdr = hbhhdr->ip6h_nxt;
24450Sstevel@tonic-gate 			ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
24460Sstevel@tonic-gate 			ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
24470Sstevel@tonic-gate 			    IPPROTO_HOPOPTS, copy_always);
24480Sstevel@tonic-gate 			/*
24490Sstevel@tonic-gate 			 * Return a zero offset indicating error if there
24500Sstevel@tonic-gate 			 * was error.
24510Sstevel@tonic-gate 			 */
24520Sstevel@tonic-gate 			if (ret == -1)
24530Sstevel@tonic-gate 				return (0);
24540Sstevel@tonic-gate 			hbhhdr = (ip6_hbh_t *)pi_opt;
24550Sstevel@tonic-gate 			prev_nexthdr = (uint8_t *)&hbhhdr->ip6h_nxt;
24560Sstevel@tonic-gate 			break;
24570Sstevel@tonic-gate 		case IPPROTO_ROUTING:
24580Sstevel@tonic-gate 			rthdr = (ip6_rthdr0_t *)oi_opt;
24590Sstevel@tonic-gate 			nexthdr = rthdr->ip6r0_nxt;
24600Sstevel@tonic-gate 			ehdrlen = 8 * (rthdr->ip6r0_len + 1);
24610Sstevel@tonic-gate 			if (!copy_always && outbound) {
24620Sstevel@tonic-gate 				int i, left;
24630Sstevel@tonic-gate 				ip6_rthdr0_t *prthdr;
24640Sstevel@tonic-gate 				in6_addr_t *ap, *pap;
24650Sstevel@tonic-gate 
24660Sstevel@tonic-gate 				left = rthdr->ip6r0_segleft;
24670Sstevel@tonic-gate 				prthdr = (ip6_rthdr0_t *)pi_opt;
24680Sstevel@tonic-gate 				pap = (in6_addr_t *)(prthdr + 1);
24690Sstevel@tonic-gate 				ap = (in6_addr_t *)(rthdr + 1);
24700Sstevel@tonic-gate 				/*
24710Sstevel@tonic-gate 				 * First eight bytes except seg_left
24720Sstevel@tonic-gate 				 * does not change en route.
24730Sstevel@tonic-gate 				 */
24740Sstevel@tonic-gate 				bcopy(oi_opt, pi_opt, 8);
24750Sstevel@tonic-gate 				prthdr->ip6r0_segleft = 0;
24760Sstevel@tonic-gate 				/*
24770Sstevel@tonic-gate 				 * First address has been moved to
24780Sstevel@tonic-gate 				 * the destination address of the
24790Sstevel@tonic-gate 				 * ip header by ip_massage_options_v6.
24800Sstevel@tonic-gate 				 * And the real destination address is
24810Sstevel@tonic-gate 				 * in the last address part of the
24820Sstevel@tonic-gate 				 * option.
24830Sstevel@tonic-gate 				 */
24840Sstevel@tonic-gate 				*pap = oip6h->ip6_dst;
24850Sstevel@tonic-gate 				for (i = 1; i < left - 1; i++)
24860Sstevel@tonic-gate 					pap[i] = ap[i - 1];
24870Sstevel@tonic-gate 				ip6h->ip6_dst = *(ap + left - 1);
24880Sstevel@tonic-gate 			} else {
24890Sstevel@tonic-gate 				bcopy(oi_opt, pi_opt, ehdrlen);
24900Sstevel@tonic-gate 			}
24910Sstevel@tonic-gate 			rthdr = (ip6_rthdr0_t *)pi_opt;
24920Sstevel@tonic-gate 			prev_nexthdr = (uint8_t *)&rthdr->ip6r0_nxt;
24930Sstevel@tonic-gate 			break;
24940Sstevel@tonic-gate 		case IPPROTO_DSTOPTS:
24950Sstevel@tonic-gate 			/*
24960Sstevel@tonic-gate 			 * Destination options are tricky.  If there is
24970Sstevel@tonic-gate 			 * a terminal (e.g. non-IPv6-extension) header
24980Sstevel@tonic-gate 			 * following the destination options, don't
24990Sstevel@tonic-gate 			 * reset prev_nexthdr or advance the AH insertion
25000Sstevel@tonic-gate 			 * point and just treat this as a terminal header.
25010Sstevel@tonic-gate 			 *
25020Sstevel@tonic-gate 			 * If this is an inbound packet, just deal with
25030Sstevel@tonic-gate 			 * it as is.
25040Sstevel@tonic-gate 			 */
25050Sstevel@tonic-gate 			dsthdr = (ip6_dest_t *)oi_opt;
25060Sstevel@tonic-gate 			/*
25070Sstevel@tonic-gate 			 * XXX I hope common-subexpression elimination
25080Sstevel@tonic-gate 			 * saves us the double-evaluate.
25090Sstevel@tonic-gate 			 */
25100Sstevel@tonic-gate 			if (outbound && dsthdr->ip6d_nxt != IPPROTO_ROUTING &&
25110Sstevel@tonic-gate 			    dsthdr->ip6d_nxt != IPPROTO_HOPOPTS)
25120Sstevel@tonic-gate 				goto terminal_hdr;
25130Sstevel@tonic-gate 			nexthdr = dsthdr->ip6d_nxt;
25140Sstevel@tonic-gate 			ehdrlen = 8 * (dsthdr->ip6d_len + 1);
25150Sstevel@tonic-gate 			ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
25160Sstevel@tonic-gate 			    IPPROTO_DSTOPTS, copy_always);
25170Sstevel@tonic-gate 			/*
25180Sstevel@tonic-gate 			 * Return a zero offset indicating error if there
25190Sstevel@tonic-gate 			 * was error.
25200Sstevel@tonic-gate 			 */
25210Sstevel@tonic-gate 			if (ret == -1)
25220Sstevel@tonic-gate 				return (0);
25230Sstevel@tonic-gate 			break;
25240Sstevel@tonic-gate 		case IPPROTO_AH:
25250Sstevel@tonic-gate 			/*
25260Sstevel@tonic-gate 			 * Be conservative in what you send.  We shouldn't
25270Sstevel@tonic-gate 			 * see two same-scoped AH's in one packet.
25280Sstevel@tonic-gate 			 * (Inner-IP-scoped AH will be hit by terminal
25290Sstevel@tonic-gate 			 * header of IP or IPv6.)
25300Sstevel@tonic-gate 			 */
25310Sstevel@tonic-gate 			ASSERT(!outbound);
25320Sstevel@tonic-gate 			return ((uint_t)(pi_opt - (uint8_t *)ip6h));
25330Sstevel@tonic-gate 		default:
25340Sstevel@tonic-gate 			ASSERT(outbound);
25350Sstevel@tonic-gate terminal_hdr:
25360Sstevel@tonic-gate 			*prev_nexthdr = IPPROTO_AH;
25370Sstevel@tonic-gate 			ah = (ah_t *)pi_opt;
25380Sstevel@tonic-gate 			ah->ah_nexthdr = nexthdr;
25390Sstevel@tonic-gate 			return ((uint_t)(pi_opt - (uint8_t *)ip6h));
25400Sstevel@tonic-gate 		}
25410Sstevel@tonic-gate 		pi_opt += ehdrlen;
25420Sstevel@tonic-gate 		oi_opt += ehdrlen;
25430Sstevel@tonic-gate 	}
25440Sstevel@tonic-gate 	/* NOTREACHED */
25450Sstevel@tonic-gate }
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate static boolean_t
ah_finish_up(ah_t * phdr_ah,ah_t * inbound_ah,ipsa_t * assoc,int ah_data_sz,int ah_align_sz,ipsecah_stack_t * ahstack)25480Sstevel@tonic-gate ah_finish_up(ah_t *phdr_ah, ah_t *inbound_ah, ipsa_t *assoc,
25493448Sdh155122     int ah_data_sz, int ah_align_sz, ipsecah_stack_t *ahstack)
25500Sstevel@tonic-gate {
25510Sstevel@tonic-gate 	int i;
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	/*
25540Sstevel@tonic-gate 	 * Padding :
25550Sstevel@tonic-gate 	 *
25560Sstevel@tonic-gate 	 * 1) Authentication data may have to be padded
25570Sstevel@tonic-gate 	 * before ICV calculation if ICV is not a multiple
25580Sstevel@tonic-gate 	 * of 64 bits. This padding is arbitrary and transmitted
25590Sstevel@tonic-gate 	 * with the packet at the end of the authentication data.
25600Sstevel@tonic-gate 	 * Payload length should include the padding bytes.
25610Sstevel@tonic-gate 	 *
25620Sstevel@tonic-gate 	 * 2) Explicit padding of the whole datagram may be
25630Sstevel@tonic-gate 	 * required by the algorithm which need not be
25640Sstevel@tonic-gate 	 * transmitted. It is assumed that this will be taken
25650Sstevel@tonic-gate 	 * care by the algorithm module.
25660Sstevel@tonic-gate 	 */
25670Sstevel@tonic-gate 	bzero(phdr_ah + 1, ah_data_sz);	/* Zero out ICV for pseudo-hdr. */
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	if (inbound_ah == NULL) {
25700Sstevel@tonic-gate 		/* Outbound AH datagram. */
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 		phdr_ah->ah_length = (ah_align_sz >> 2) + 1;
25730Sstevel@tonic-gate 		phdr_ah->ah_reserved = 0;
25740Sstevel@tonic-gate 		phdr_ah->ah_spi = assoc->ipsa_spi;
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate 		phdr_ah->ah_replay =
25770Sstevel@tonic-gate 		    htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
25780Sstevel@tonic-gate 		if (phdr_ah->ah_replay == 0 && assoc->ipsa_replay_wsize != 0) {
25790Sstevel@tonic-gate 			/*
25800Sstevel@tonic-gate 			 * XXX We have replay counter wrapping.  We probably
25810Sstevel@tonic-gate 			 * want to nuke this SA (and its peer).
25820Sstevel@tonic-gate 			 */
25830Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
25840Sstevel@tonic-gate 			    SL_ERROR | SL_CONSOLE | SL_WARN,
25850Sstevel@tonic-gate 			    "Outbound AH SA (0x%x), dst %s has wrapped "
25860Sstevel@tonic-gate 			    "sequence.\n", phdr_ah->ah_spi,
25873448Sdh155122 			    assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
25883448Sdh155122 			    ahstack->ipsecah_netstack);
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 			sadb_replay_delete(assoc);
25910Sstevel@tonic-gate 			/* Caller will free phdr_mp and return NULL. */
25920Sstevel@tonic-gate 			return (B_FALSE);
25930Sstevel@tonic-gate 		}
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 		if (ah_data_sz != ah_align_sz) {
25960Sstevel@tonic-gate 			uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
25970Sstevel@tonic-gate 			    ah_data_sz);
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 			for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
26000Sstevel@tonic-gate 				pad[i] = (uchar_t)i;	/* Fill the padding */
26010Sstevel@tonic-gate 			}
26020Sstevel@tonic-gate 		}
26030Sstevel@tonic-gate 	} else {
26040Sstevel@tonic-gate 		/* Inbound AH datagram. */
26050Sstevel@tonic-gate 		phdr_ah->ah_nexthdr = inbound_ah->ah_nexthdr;
26060Sstevel@tonic-gate 		phdr_ah->ah_length = inbound_ah->ah_length;
26070Sstevel@tonic-gate 		phdr_ah->ah_reserved = 0;
26080Sstevel@tonic-gate 		ASSERT(inbound_ah->ah_spi == assoc->ipsa_spi);
26090Sstevel@tonic-gate 		phdr_ah->ah_spi = inbound_ah->ah_spi;
26100Sstevel@tonic-gate 		phdr_ah->ah_replay = inbound_ah->ah_replay;
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 		if (ah_data_sz != ah_align_sz) {
26133448Sdh155122 			uchar_t *opad = ((uchar_t *)inbound_ah +
26143448Sdh155122 			    sizeof (ah_t) + ah_data_sz);
26150Sstevel@tonic-gate 			uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
26160Sstevel@tonic-gate 			    ah_data_sz);
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate 			for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
26190Sstevel@tonic-gate 				pad[i] = opad[i];	/* Copy the padding */
26200Sstevel@tonic-gate 			}
26210Sstevel@tonic-gate 		}
26220Sstevel@tonic-gate 	}
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	return (B_TRUE);
26250Sstevel@tonic-gate }
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate /*
26280Sstevel@tonic-gate  * Called upon failing the inbound ICV check. The message passed as
26290Sstevel@tonic-gate  * argument is freed.
26300Sstevel@tonic-gate  */
26310Sstevel@tonic-gate static void
ah_log_bad_auth(mblk_t * mp,ip_recv_attr_t * ira,ipsec_crypto_t * ic)263211042SErik.Nordmark@Sun.COM ah_log_bad_auth(mblk_t *mp, ip_recv_attr_t *ira, ipsec_crypto_t *ic)
26330Sstevel@tonic-gate {
263411042SErik.Nordmark@Sun.COM 	boolean_t	isv4 = (ira->ira_flags & IRAF_IS_IPV4);
263511042SErik.Nordmark@Sun.COM 	ipsa_t		*assoc = ira->ira_ipsec_ah_sa;
263611042SErik.Nordmark@Sun.COM 	int		af;
263711042SErik.Nordmark@Sun.COM 	void		*addr;
263811042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ira->ira_ill->ill_ipst->ips_netstack;
26393448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
26403448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
26410Sstevel@tonic-gate 
264211042SErik.Nordmark@Sun.COM 	ASSERT(mp->b_datap->db_type == M_DATA);
264311042SErik.Nordmark@Sun.COM 
264411042SErik.Nordmark@Sun.COM 	mp->b_rptr -= ic->ic_skip_len;
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	if (isv4) {
26470Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
26480Sstevel@tonic-gate 		addr = &ipha->ipha_dst;
26490Sstevel@tonic-gate 		af = AF_INET;
26500Sstevel@tonic-gate 	} else {
26510Sstevel@tonic-gate 		ip6_t *ip6h = (ip6_t *)mp->b_rptr;
26520Sstevel@tonic-gate 		addr = &ip6h->ip6_dst;
26530Sstevel@tonic-gate 		af = AF_INET6;
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	/*
26570Sstevel@tonic-gate 	 * Log the event. Don't print to the console, block
26580Sstevel@tonic-gate 	 * potential denial-of-service attack.
26590Sstevel@tonic-gate 	 */
26603448Sdh155122 	AH_BUMP_STAT(ahstack, bad_auth);
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
26630Sstevel@tonic-gate 	    "AH Authentication failed spi %x, dst_addr %s",
26643448Sdh155122 	    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
26653448Sdh155122 
26663448Sdh155122 	IP_AH_BUMP_STAT(ipss, in_discards);
266711042SErik.Nordmark@Sun.COM 	ip_drop_packet(mp, B_TRUE, ira->ira_ill,
26683448Sdh155122 	    DROPPER(ipss, ipds_ah_bad_auth),
26693448Sdh155122 	    &ahstack->ah_dropper);
26700Sstevel@tonic-gate }
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate /*
26730Sstevel@tonic-gate  * Kernel crypto framework callback invoked after completion of async
267411042SErik.Nordmark@Sun.COM  * crypto requests for outbound packets.
26750Sstevel@tonic-gate  */
26760Sstevel@tonic-gate static void
ah_kcf_callback_outbound(void * arg,int status)267711042SErik.Nordmark@Sun.COM ah_kcf_callback_outbound(void *arg, int status)
26780Sstevel@tonic-gate {
267911042SErik.Nordmark@Sun.COM 	mblk_t		*mp = (mblk_t *)arg;
268011042SErik.Nordmark@Sun.COM 	mblk_t		*async_mp;
268111042SErik.Nordmark@Sun.COM 	netstack_t	*ns;
26823448Sdh155122 	ipsec_stack_t	*ipss;
26833448Sdh155122 	ipsecah_stack_t	*ahstack;
268411042SErik.Nordmark@Sun.COM 	mblk_t		*data_mp;
268511042SErik.Nordmark@Sun.COM 	ip_xmit_attr_t	ixas;
268611042SErik.Nordmark@Sun.COM 	ipsec_crypto_t	*ic;
268711042SErik.Nordmark@Sun.COM 	ill_t		*ill;
268811042SErik.Nordmark@Sun.COM 
268911042SErik.Nordmark@Sun.COM 	/*
269011042SErik.Nordmark@Sun.COM 	 * First remove the ipsec_crypto_t mblk
269111042SErik.Nordmark@Sun.COM 	 * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
269211042SErik.Nordmark@Sun.COM 	 */
269311042SErik.Nordmark@Sun.COM 	async_mp = ipsec_remove_crypto_data(mp, &ic);
269411042SErik.Nordmark@Sun.COM 	ASSERT(async_mp != NULL);
269511042SErik.Nordmark@Sun.COM 
26963448Sdh155122 	/*
269711042SErik.Nordmark@Sun.COM 	 * Extract the ip_xmit_attr_t from the first mblk.
269811042SErik.Nordmark@Sun.COM 	 * Verifies that the netstack and ill is still around; could
269911042SErik.Nordmark@Sun.COM 	 * have vanished while kEf was doing its work.
270011042SErik.Nordmark@Sun.COM 	 * On succesful return we have a nce_t and the ill/ipst can't
270111042SErik.Nordmark@Sun.COM 	 * disappear until we do the nce_refrele in ixa_cleanup.
27023448Sdh155122 	 */
270311042SErik.Nordmark@Sun.COM 	data_mp = async_mp->b_cont;
270411042SErik.Nordmark@Sun.COM 	async_mp->b_cont = NULL;
270511042SErik.Nordmark@Sun.COM 	if (!ip_xmit_attr_from_mblk(async_mp, &ixas)) {
270611042SErik.Nordmark@Sun.COM 		/* Disappeared on us - no ill/ipst for MIB */
270711042SErik.Nordmark@Sun.COM 		if (ixas.ixa_nce != NULL) {
270811042SErik.Nordmark@Sun.COM 			ill = ixas.ixa_nce->nce_ill;
270911042SErik.Nordmark@Sun.COM 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
271011042SErik.Nordmark@Sun.COM 			ip_drop_output("ipIfStatsOutDiscards", data_mp, ill);
271111042SErik.Nordmark@Sun.COM 		}
271211042SErik.Nordmark@Sun.COM 		freemsg(data_mp);
271311042SErik.Nordmark@Sun.COM 		goto done;
271411042SErik.Nordmark@Sun.COM 	}
271511042SErik.Nordmark@Sun.COM 	ns = ixas.ixa_ipst->ips_netstack;
271611042SErik.Nordmark@Sun.COM 	ahstack = ns->netstack_ipsecah;
271711042SErik.Nordmark@Sun.COM 	ipss = ns->netstack_ipsec;
271811042SErik.Nordmark@Sun.COM 	ill = ixas.ixa_nce->nce_ill;
271911042SErik.Nordmark@Sun.COM 
272011042SErik.Nordmark@Sun.COM 	if (status == CRYPTO_SUCCESS) {
272111042SErik.Nordmark@Sun.COM 		data_mp = ah_auth_out_done(data_mp, &ixas, ic);
272211042SErik.Nordmark@Sun.COM 		if (data_mp == NULL)
272311042SErik.Nordmark@Sun.COM 			goto done;
272411042SErik.Nordmark@Sun.COM 
272511042SErik.Nordmark@Sun.COM 		(void) ip_output_post_ipsec(data_mp, &ixas);
272611042SErik.Nordmark@Sun.COM 	} else {
272711042SErik.Nordmark@Sun.COM 		/* Outbound shouldn't see invalid MAC */
272811042SErik.Nordmark@Sun.COM 		ASSERT(status != CRYPTO_INVALID_MAC);
272911042SErik.Nordmark@Sun.COM 
273011042SErik.Nordmark@Sun.COM 		ah1dbg(ahstack,
273111042SErik.Nordmark@Sun.COM 		    ("ah_kcf_callback_outbound: crypto failed with 0x%x\n",
273211042SErik.Nordmark@Sun.COM 		    status));
273311042SErik.Nordmark@Sun.COM 		AH_BUMP_STAT(ahstack, crypto_failures);
273411042SErik.Nordmark@Sun.COM 		AH_BUMP_STAT(ahstack, out_discards);
273511042SErik.Nordmark@Sun.COM 
273611042SErik.Nordmark@Sun.COM 		ip_drop_packet(data_mp, B_FALSE, ill,
273711042SErik.Nordmark@Sun.COM 		    DROPPER(ipss, ipds_ah_crypto_failed),
273811042SErik.Nordmark@Sun.COM 		    &ahstack->ah_dropper);
273911042SErik.Nordmark@Sun.COM 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
27403448Sdh155122 	}
274111042SErik.Nordmark@Sun.COM done:
274211042SErik.Nordmark@Sun.COM 	ixa_cleanup(&ixas);
274311042SErik.Nordmark@Sun.COM 	(void) ipsec_free_crypto_data(mp);
274411042SErik.Nordmark@Sun.COM }
274511042SErik.Nordmark@Sun.COM 
274611042SErik.Nordmark@Sun.COM /*
274711042SErik.Nordmark@Sun.COM  * Kernel crypto framework callback invoked after completion of async
274811042SErik.Nordmark@Sun.COM  * crypto requests for inbound packets.
274911042SErik.Nordmark@Sun.COM  */
275011042SErik.Nordmark@Sun.COM static void
ah_kcf_callback_inbound(void * arg,int status)275111042SErik.Nordmark@Sun.COM ah_kcf_callback_inbound(void *arg, int status)
275211042SErik.Nordmark@Sun.COM {
275311042SErik.Nordmark@Sun.COM 	mblk_t		*mp = (mblk_t *)arg;
275411042SErik.Nordmark@Sun.COM 	mblk_t		*async_mp;
275511042SErik.Nordmark@Sun.COM 	netstack_t	*ns;
275611042SErik.Nordmark@Sun.COM 	ipsec_stack_t	*ipss;
275711042SErik.Nordmark@Sun.COM 	ipsecah_stack_t	*ahstack;
275811042SErik.Nordmark@Sun.COM 	mblk_t		*data_mp;
275911042SErik.Nordmark@Sun.COM 	ip_recv_attr_t	iras;
276011042SErik.Nordmark@Sun.COM 	ipsec_crypto_t	*ic;
276111042SErik.Nordmark@Sun.COM 
276211042SErik.Nordmark@Sun.COM 	/*
276311042SErik.Nordmark@Sun.COM 	 * First remove the ipsec_crypto_t mblk
276411042SErik.Nordmark@Sun.COM 	 * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
276511042SErik.Nordmark@Sun.COM 	 */
276611042SErik.Nordmark@Sun.COM 	async_mp = ipsec_remove_crypto_data(mp, &ic);
276711042SErik.Nordmark@Sun.COM 	ASSERT(async_mp != NULL);
276811042SErik.Nordmark@Sun.COM 
276911042SErik.Nordmark@Sun.COM 	/*
277011042SErik.Nordmark@Sun.COM 	 * Extract the ip_xmit_attr_t from the first mblk.
277111042SErik.Nordmark@Sun.COM 	 * Verifies that the netstack and ill is still around; could
277211042SErik.Nordmark@Sun.COM 	 * have vanished while kEf was doing its work.
277311042SErik.Nordmark@Sun.COM 	 */
277411042SErik.Nordmark@Sun.COM 	data_mp = async_mp->b_cont;
277511042SErik.Nordmark@Sun.COM 	async_mp->b_cont = NULL;
277611042SErik.Nordmark@Sun.COM 	if (!ip_recv_attr_from_mblk(async_mp, &iras)) {
277711042SErik.Nordmark@Sun.COM 		/* The ill or ip_stack_t disappeared on us */
277811042SErik.Nordmark@Sun.COM 		ip_drop_input("ip_recv_attr_from_mblk", data_mp, NULL);
277911042SErik.Nordmark@Sun.COM 		freemsg(data_mp);
278011042SErik.Nordmark@Sun.COM 		goto done;
278111042SErik.Nordmark@Sun.COM 	}
278211042SErik.Nordmark@Sun.COM 	ns = iras.ira_ill->ill_ipst->ips_netstack;
27833448Sdh155122 	ahstack = ns->netstack_ipsecah;
27843448Sdh155122 	ipss = ns->netstack_ipsec;
27853448Sdh155122 
27860Sstevel@tonic-gate 	if (status == CRYPTO_SUCCESS) {
278711042SErik.Nordmark@Sun.COM 		data_mp = ah_auth_in_done(data_mp, &iras, ic);
278811042SErik.Nordmark@Sun.COM 		if (data_mp == NULL)
278911042SErik.Nordmark@Sun.COM 			goto done;
279011042SErik.Nordmark@Sun.COM 
279111042SErik.Nordmark@Sun.COM 		/* finish IPsec processing */
279211042SErik.Nordmark@Sun.COM 		ip_input_post_ipsec(data_mp, &iras);
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	} else if (status == CRYPTO_INVALID_MAC) {
279511042SErik.Nordmark@Sun.COM 		ah_log_bad_auth(data_mp, &iras, ic);
27960Sstevel@tonic-gate 	} else {
279711042SErik.Nordmark@Sun.COM 		ah1dbg(ahstack,
279811042SErik.Nordmark@Sun.COM 		    ("ah_kcf_callback_inbound: crypto failed with 0x%x\n",
27994987Sdanmcd 		    status));
28003448Sdh155122 		AH_BUMP_STAT(ahstack, crypto_failures);
280111042SErik.Nordmark@Sun.COM 		IP_AH_BUMP_STAT(ipss, in_discards);
280211042SErik.Nordmark@Sun.COM 		ip_drop_packet(data_mp, B_TRUE, iras.ira_ill,
28033448Sdh155122 		    DROPPER(ipss, ipds_ah_crypto_failed),
28043448Sdh155122 		    &ahstack->ah_dropper);
280511042SErik.Nordmark@Sun.COM 		BUMP_MIB(iras.ira_ill->ill_ip_mib, ipIfStatsInDiscards);
28060Sstevel@tonic-gate 	}
280711042SErik.Nordmark@Sun.COM done:
280811042SErik.Nordmark@Sun.COM 	ira_cleanup(&iras, B_TRUE);
280911042SErik.Nordmark@Sun.COM 	(void) ipsec_free_crypto_data(mp);
28100Sstevel@tonic-gate }
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate /*
28130Sstevel@tonic-gate  * Invoked on kernel crypto failure during inbound and outbound processing.
28140Sstevel@tonic-gate  */
28150Sstevel@tonic-gate static void
ah_crypto_failed(mblk_t * data_mp,boolean_t is_inbound,int kef_rc,ill_t * ill,ipsecah_stack_t * ahstack)281611042SErik.Nordmark@Sun.COM ah_crypto_failed(mblk_t *data_mp, boolean_t is_inbound, int kef_rc,
281711042SErik.Nordmark@Sun.COM     ill_t *ill, ipsecah_stack_t *ahstack)
28180Sstevel@tonic-gate {
28193448Sdh155122 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
28203448Sdh155122 
28213448Sdh155122 	ah1dbg(ahstack, ("crypto failed for %s AH with 0x%x\n",
28220Sstevel@tonic-gate 	    is_inbound ? "inbound" : "outbound", kef_rc));
282311042SErik.Nordmark@Sun.COM 	ip_drop_packet(data_mp, is_inbound, ill,
28243448Sdh155122 	    DROPPER(ipss, ipds_ah_crypto_failed),
28253448Sdh155122 	    &ahstack->ah_dropper);
28263448Sdh155122 	AH_BUMP_STAT(ahstack, crypto_failures);
28270Sstevel@tonic-gate 	if (is_inbound)
28283448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
28290Sstevel@tonic-gate 	else
28303448Sdh155122 		AH_BUMP_STAT(ahstack, out_discards);
28310Sstevel@tonic-gate }
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate /*
28340Sstevel@tonic-gate  * Helper macros for the ah_submit_req_{inbound,outbound}() functions.
28350Sstevel@tonic-gate  */
28360Sstevel@tonic-gate 
283711042SErik.Nordmark@Sun.COM /*
283811042SErik.Nordmark@Sun.COM  * A statement-equivalent macro, _cr MUST point to a modifiable
283911042SErik.Nordmark@Sun.COM  * crypto_call_req_t.
284011042SErik.Nordmark@Sun.COM  */
284111042SErik.Nordmark@Sun.COM #define	AH_INIT_CALLREQ(_cr, _mp, _callback)		\
284211042SErik.Nordmark@Sun.COM 	(_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_ALWAYS_QUEUE;	\
284311042SErik.Nordmark@Sun.COM 	(_cr)->cr_callback_arg = (_mp);				\
284411042SErik.Nordmark@Sun.COM 	(_cr)->cr_callback_func = (_callback)
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate #define	AH_INIT_CRYPTO_DATA(data, msglen, mblk) {			\
28470Sstevel@tonic-gate 	(data)->cd_format = CRYPTO_DATA_MBLK;				\
28480Sstevel@tonic-gate 	(data)->cd_mp = mblk;						\
28490Sstevel@tonic-gate 	(data)->cd_offset = 0;						\
28500Sstevel@tonic-gate 	(data)->cd_length = msglen;					\
28510Sstevel@tonic-gate }
28520Sstevel@tonic-gate 
28530Sstevel@tonic-gate #define	AH_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) {			\
28540Sstevel@tonic-gate 	(mac)->cd_format = CRYPTO_DATA_RAW;				\
28550Sstevel@tonic-gate 	(mac)->cd_offset = 0;						\
28560Sstevel@tonic-gate 	(mac)->cd_length = icvlen;					\
28570Sstevel@tonic-gate 	(mac)->cd_raw.iov_base = icvbuf;				\
28580Sstevel@tonic-gate 	(mac)->cd_raw.iov_len = icvlen;					\
28590Sstevel@tonic-gate }
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate /*
28620Sstevel@tonic-gate  * Submit an inbound packet for processing by the crypto framework.
28630Sstevel@tonic-gate  */
286411042SErik.Nordmark@Sun.COM static mblk_t *
ah_submit_req_inbound(mblk_t * phdr_mp,ip_recv_attr_t * ira,size_t skip_len,uint32_t ah_offset,ipsa_t * assoc)286511042SErik.Nordmark@Sun.COM ah_submit_req_inbound(mblk_t *phdr_mp, ip_recv_attr_t *ira,
286611042SErik.Nordmark@Sun.COM     size_t skip_len, uint32_t ah_offset, ipsa_t *assoc)
28670Sstevel@tonic-gate {
28680Sstevel@tonic-gate 	int kef_rc;
286911042SErik.Nordmark@Sun.COM 	mblk_t *mp;
287011042SErik.Nordmark@Sun.COM 	crypto_call_req_t call_req, *callrp;
28710Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
28720Sstevel@tonic-gate 	crypto_ctx_template_t ctx_tmpl;
287311042SErik.Nordmark@Sun.COM 	ipsecah_stack_t	*ahstack;
287411042SErik.Nordmark@Sun.COM 	ipsec_crypto_t	*ic, icstack;
287511042SErik.Nordmark@Sun.COM 	boolean_t force = (assoc->ipsa_flags & IPSA_F_ASYNC);
287611042SErik.Nordmark@Sun.COM 
287711042SErik.Nordmark@Sun.COM 	ahstack = ira->ira_ill->ill_ipst->ips_netstack->netstack_ipsecah;
287811042SErik.Nordmark@Sun.COM 
28790Sstevel@tonic-gate 	ASSERT(phdr_mp != NULL);
288011042SErik.Nordmark@Sun.COM 	ASSERT(phdr_mp->b_datap->db_type == M_DATA);
288111042SErik.Nordmark@Sun.COM 
288211042SErik.Nordmark@Sun.COM 	if (force) {
288311042SErik.Nordmark@Sun.COM 		/* We are doing asynch; allocate mblks to hold state */
288411042SErik.Nordmark@Sun.COM 		if ((mp = ip_recv_attr_to_mblk(ira)) == NULL ||
288511042SErik.Nordmark@Sun.COM 		    (mp = ipsec_add_crypto_data(mp, &ic)) == NULL) {
288611042SErik.Nordmark@Sun.COM 			BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
288711042SErik.Nordmark@Sun.COM 			ip_drop_input("ipIfStatsInDiscards", phdr_mp,
288811042SErik.Nordmark@Sun.COM 			    ira->ira_ill);
288911042SErik.Nordmark@Sun.COM 			freemsg(phdr_mp);
289011042SErik.Nordmark@Sun.COM 			return (NULL);
289111042SErik.Nordmark@Sun.COM 		}
289211042SErik.Nordmark@Sun.COM 
289311042SErik.Nordmark@Sun.COM 		linkb(mp, phdr_mp);
289411042SErik.Nordmark@Sun.COM 		callrp = &call_req;
289511042SErik.Nordmark@Sun.COM 		AH_INIT_CALLREQ(callrp, mp, ah_kcf_callback_inbound);
289611042SErik.Nordmark@Sun.COM 	} else {
289711042SErik.Nordmark@Sun.COM 		/*
289811042SErik.Nordmark@Sun.COM 		 * If we know we are going to do sync then ipsec_crypto_t
289911042SErik.Nordmark@Sun.COM 		 * should be on the stack.
290011042SErik.Nordmark@Sun.COM 		 */
290111042SErik.Nordmark@Sun.COM 		ic = &icstack;
290211042SErik.Nordmark@Sun.COM 		bzero(ic, sizeof (*ic));
290311042SErik.Nordmark@Sun.COM 		callrp = NULL;
290411042SErik.Nordmark@Sun.COM 	}
29053448Sdh155122 
29060Sstevel@tonic-gate 	/* init arguments for the crypto framework */
290711042SErik.Nordmark@Sun.COM 	AH_INIT_CRYPTO_DATA(&ic->ic_crypto_data, AH_MSGSIZE(phdr_mp),
29080Sstevel@tonic-gate 	    phdr_mp);
29090Sstevel@tonic-gate 
291011042SErik.Nordmark@Sun.COM 	AH_INIT_CRYPTO_MAC(&ic->ic_crypto_mac, icv_len,
29110Sstevel@tonic-gate 	    (char *)phdr_mp->b_cont->b_rptr - skip_len + ah_offset +
29120Sstevel@tonic-gate 	    sizeof (ah_t));
29130Sstevel@tonic-gate 
291411042SErik.Nordmark@Sun.COM 	ic->ic_skip_len = skip_len;
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, ctx_tmpl);
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	/* call KEF to do the MAC operation */
29190Sstevel@tonic-gate 	kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
292011042SErik.Nordmark@Sun.COM 	    &ic->ic_crypto_data, &assoc->ipsa_kcfauthkey, ctx_tmpl,
292111042SErik.Nordmark@Sun.COM 	    &ic->ic_crypto_mac, callrp);
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 	switch (kef_rc) {
29240Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
29253448Sdh155122 		AH_BUMP_STAT(ahstack, crypto_sync);
292611042SErik.Nordmark@Sun.COM 		phdr_mp = ah_auth_in_done(phdr_mp, ira, ic);
292711042SErik.Nordmark@Sun.COM 		if (force) {
292811042SErik.Nordmark@Sun.COM 			/* Free mp after we are done with ic */
292911042SErik.Nordmark@Sun.COM 			mp = ipsec_free_crypto_data(mp);
293011042SErik.Nordmark@Sun.COM 			(void) ip_recv_attr_free_mblk(mp);
293111042SErik.Nordmark@Sun.COM 		}
293211042SErik.Nordmark@Sun.COM 		return (phdr_mp);
29330Sstevel@tonic-gate 	case CRYPTO_QUEUED:
293411042SErik.Nordmark@Sun.COM 		/* ah_kcf_callback_inbound() will be invoked on completion */
29353448Sdh155122 		AH_BUMP_STAT(ahstack, crypto_async);
293611042SErik.Nordmark@Sun.COM 		return (NULL);
29370Sstevel@tonic-gate 	case CRYPTO_INVALID_MAC:
293811042SErik.Nordmark@Sun.COM 		/* Free mp after we are done with ic */
29393448Sdh155122 		AH_BUMP_STAT(ahstack, crypto_sync);
294011042SErik.Nordmark@Sun.COM 		BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
294111042SErik.Nordmark@Sun.COM 		ah_log_bad_auth(phdr_mp, ira, ic);
294211042SErik.Nordmark@Sun.COM 		/* phdr_mp was passed to ip_drop_packet */
294311042SErik.Nordmark@Sun.COM 		if (force) {
294411042SErik.Nordmark@Sun.COM 			mp = ipsec_free_crypto_data(mp);
294511042SErik.Nordmark@Sun.COM 			(void) ip_recv_attr_free_mblk(mp);
294611042SErik.Nordmark@Sun.COM 		}
294711042SErik.Nordmark@Sun.COM 		return (NULL);
29480Sstevel@tonic-gate 	}
29490Sstevel@tonic-gate 
295011042SErik.Nordmark@Sun.COM 	if (force) {
295111042SErik.Nordmark@Sun.COM 		mp = ipsec_free_crypto_data(mp);
295211042SErik.Nordmark@Sun.COM 		phdr_mp = ip_recv_attr_free_mblk(mp);
295311042SErik.Nordmark@Sun.COM 	}
295411042SErik.Nordmark@Sun.COM 	BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
295511042SErik.Nordmark@Sun.COM 	ah_crypto_failed(phdr_mp, B_TRUE, kef_rc, ira->ira_ill, ahstack);
295611042SErik.Nordmark@Sun.COM 	/* phdr_mp was passed to ip_drop_packet */
295711042SErik.Nordmark@Sun.COM 	return (NULL);
29580Sstevel@tonic-gate }
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate /*
29610Sstevel@tonic-gate  * Submit an outbound packet for processing by the crypto framework.
29620Sstevel@tonic-gate  */
296311042SErik.Nordmark@Sun.COM static mblk_t *
ah_submit_req_outbound(mblk_t * phdr_mp,ip_xmit_attr_t * ixa,size_t skip_len,ipsa_t * assoc)296411042SErik.Nordmark@Sun.COM ah_submit_req_outbound(mblk_t *phdr_mp, ip_xmit_attr_t *ixa,
296511042SErik.Nordmark@Sun.COM     size_t skip_len, ipsa_t *assoc)
29660Sstevel@tonic-gate {
29670Sstevel@tonic-gate 	int kef_rc;
296811042SErik.Nordmark@Sun.COM 	mblk_t *mp;
296911042SErik.Nordmark@Sun.COM 	crypto_call_req_t call_req, *callrp;
29700Sstevel@tonic-gate 	uint_t icv_len = assoc->ipsa_mac_len;
297111042SErik.Nordmark@Sun.COM 	ipsecah_stack_t	*ahstack;
297211042SErik.Nordmark@Sun.COM 	ipsec_crypto_t	*ic, icstack;
297311042SErik.Nordmark@Sun.COM 	ill_t		*ill = ixa->ixa_nce->nce_ill;
297411042SErik.Nordmark@Sun.COM 	boolean_t force = (assoc->ipsa_flags & IPSA_F_ASYNC);
297511042SErik.Nordmark@Sun.COM 
297611042SErik.Nordmark@Sun.COM 	ahstack = ill->ill_ipst->ips_netstack->netstack_ipsecah;
297711042SErik.Nordmark@Sun.COM 
29780Sstevel@tonic-gate 	ASSERT(phdr_mp != NULL);
297911042SErik.Nordmark@Sun.COM 	ASSERT(phdr_mp->b_datap->db_type == M_DATA);
298011042SErik.Nordmark@Sun.COM 
298111042SErik.Nordmark@Sun.COM 	if (force) {
298211042SErik.Nordmark@Sun.COM 		/* We are doing asynch; allocate mblks to hold state */
298311042SErik.Nordmark@Sun.COM 		if ((mp = ip_xmit_attr_to_mblk(ixa)) == NULL ||
298411042SErik.Nordmark@Sun.COM 		    (mp = ipsec_add_crypto_data(mp, &ic)) == NULL) {
298511042SErik.Nordmark@Sun.COM 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
298611042SErik.Nordmark@Sun.COM 			ip_drop_output("ipIfStatsOutDiscards", phdr_mp, ill);
298711042SErik.Nordmark@Sun.COM 			freemsg(phdr_mp);
298811042SErik.Nordmark@Sun.COM 			return (NULL);
298911042SErik.Nordmark@Sun.COM 		}
299011042SErik.Nordmark@Sun.COM 		linkb(mp, phdr_mp);
299111042SErik.Nordmark@Sun.COM 		callrp = &call_req;
299211042SErik.Nordmark@Sun.COM 		AH_INIT_CALLREQ(callrp, mp, ah_kcf_callback_outbound);
299311042SErik.Nordmark@Sun.COM 	} else {
299411042SErik.Nordmark@Sun.COM 		/*
299511042SErik.Nordmark@Sun.COM 		 * If we know we are going to do sync then ipsec_crypto_t
299611042SErik.Nordmark@Sun.COM 		 * should be on the stack.
299711042SErik.Nordmark@Sun.COM 		 */
299811042SErik.Nordmark@Sun.COM 		ic = &icstack;
299911042SErik.Nordmark@Sun.COM 		bzero(ic, sizeof (*ic));
300011042SErik.Nordmark@Sun.COM 		callrp = NULL;
300111042SErik.Nordmark@Sun.COM 	}
30023448Sdh155122 
30030Sstevel@tonic-gate 	/* init arguments for the crypto framework */
300411042SErik.Nordmark@Sun.COM 	AH_INIT_CRYPTO_DATA(&ic->ic_crypto_data, AH_MSGSIZE(phdr_mp),
30050Sstevel@tonic-gate 	    phdr_mp);
30060Sstevel@tonic-gate 
300711042SErik.Nordmark@Sun.COM 	AH_INIT_CRYPTO_MAC(&ic->ic_crypto_mac, icv_len,
30080Sstevel@tonic-gate 	    (char *)phdr_mp->b_wptr);
30090Sstevel@tonic-gate 
301011042SErik.Nordmark@Sun.COM 	ic->ic_skip_len = skip_len;
301111042SErik.Nordmark@Sun.COM 
301211042SErik.Nordmark@Sun.COM 	ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 	/* call KEF to do the MAC operation */
301511042SErik.Nordmark@Sun.COM 	kef_rc = crypto_mac(&assoc->ipsa_amech, &ic->ic_crypto_data,
30160Sstevel@tonic-gate 	    &assoc->ipsa_kcfauthkey, assoc->ipsa_authtmpl,
301711042SErik.Nordmark@Sun.COM 	    &ic->ic_crypto_mac, callrp);
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 	switch (kef_rc) {
30200Sstevel@tonic-gate 	case CRYPTO_SUCCESS:
30213448Sdh155122 		AH_BUMP_STAT(ahstack, crypto_sync);
302211042SErik.Nordmark@Sun.COM 		phdr_mp = ah_auth_out_done(phdr_mp, ixa, ic);
302311042SErik.Nordmark@Sun.COM 		if (force) {
302411042SErik.Nordmark@Sun.COM 			/* Free mp after we are done with ic */
302511042SErik.Nordmark@Sun.COM 			mp = ipsec_free_crypto_data(mp);
302611042SErik.Nordmark@Sun.COM 			(void) ip_xmit_attr_free_mblk(mp);
302711042SErik.Nordmark@Sun.COM 		}
302811042SErik.Nordmark@Sun.COM 		return (phdr_mp);
30290Sstevel@tonic-gate 	case CRYPTO_QUEUED:
303011042SErik.Nordmark@Sun.COM 		/* ah_kcf_callback_outbound() will be invoked on completion */
30313448Sdh155122 		AH_BUMP_STAT(ahstack, crypto_async);
303211042SErik.Nordmark@Sun.COM 		return (NULL);
30330Sstevel@tonic-gate 	}
30340Sstevel@tonic-gate 
303511042SErik.Nordmark@Sun.COM 	if (force) {
303611042SErik.Nordmark@Sun.COM 		mp = ipsec_free_crypto_data(mp);
303711042SErik.Nordmark@Sun.COM 		phdr_mp = ip_xmit_attr_free_mblk(mp);
303811042SErik.Nordmark@Sun.COM 	}
303911042SErik.Nordmark@Sun.COM 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
304011042SErik.Nordmark@Sun.COM 	ah_crypto_failed(phdr_mp, B_FALSE, kef_rc, NULL, ahstack);
304111042SErik.Nordmark@Sun.COM 	/* phdr_mp was passed to ip_drop_packet */
304211042SErik.Nordmark@Sun.COM 	return (NULL);
30430Sstevel@tonic-gate }
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate /*
30460Sstevel@tonic-gate  * This function constructs a pseudo header by looking at the IP header
30470Sstevel@tonic-gate  * and options if any. This is called for both outbound and inbound,
30480Sstevel@tonic-gate  * before computing the ICV.
30490Sstevel@tonic-gate  */
30500Sstevel@tonic-gate static mblk_t *
ah_process_ip_options_v6(mblk_t * mp,ipsa_t * assoc,int * length_to_skip,uint_t ah_data_sz,boolean_t outbound,ipsecah_stack_t * ahstack)30510Sstevel@tonic-gate ah_process_ip_options_v6(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
30523448Sdh155122     uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
30530Sstevel@tonic-gate {
30540Sstevel@tonic-gate 	ip6_t	*ip6h;
30550Sstevel@tonic-gate 	ip6_t	*oip6h;
30560Sstevel@tonic-gate 	mblk_t 	*phdr_mp;
30570Sstevel@tonic-gate 	int option_length;
30580Sstevel@tonic-gate 	uint_t	ah_align_sz;
30590Sstevel@tonic-gate 	uint_t ah_offset;
30600Sstevel@tonic-gate 	int hdr_size;
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	/*
30630Sstevel@tonic-gate 	 * Allocate space for the authentication data also. It is
30640Sstevel@tonic-gate 	 * useful both during the ICV calculation where we need to
30650Sstevel@tonic-gate 	 * feed in zeroes and while sending the datagram back to IP
30660Sstevel@tonic-gate 	 * where we will be using the same space.
30670Sstevel@tonic-gate 	 *
30680Sstevel@tonic-gate 	 * We need to allocate space for padding bytes if it is not
30690Sstevel@tonic-gate 	 * a multiple of IPV6_PADDING_ALIGN.
30700Sstevel@tonic-gate 	 *
30710Sstevel@tonic-gate 	 * In addition, we allocate space for the ICV computed by
30720Sstevel@tonic-gate 	 * the kernel crypto framework, saving us a separate kmem
30730Sstevel@tonic-gate 	 * allocation down the road.
30740Sstevel@tonic-gate 	 */
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 	ah_align_sz = P2ALIGN(ah_data_sz + IPV6_PADDING_ALIGN - 1,
30770Sstevel@tonic-gate 	    IPV6_PADDING_ALIGN);
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate 	ASSERT(ah_align_sz >= ah_data_sz);
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 	hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
30820Sstevel@tonic-gate 	option_length = hdr_size - IPV6_HDR_LEN;
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	/* This was not included in ipsec_ah_get_hdr_size_v6() */
30850Sstevel@tonic-gate 	hdr_size += (sizeof (ah_t) + ah_align_sz);
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	if (!outbound && (MBLKL(mp) < hdr_size)) {
30880Sstevel@tonic-gate 		/*
30890Sstevel@tonic-gate 		 * We have post-AH header options in a separate mblk,
30900Sstevel@tonic-gate 		 * a pullup is required.
30910Sstevel@tonic-gate 		 */
30920Sstevel@tonic-gate 		if (!pullupmsg(mp, hdr_size))
30930Sstevel@tonic-gate 			return (NULL);
30940Sstevel@tonic-gate 	}
30950Sstevel@tonic-gate 
30968778SErik.Nordmark@Sun.COM 	if ((phdr_mp = allocb_tmpl(hdr_size + ah_data_sz, mp)) == NULL) {
30970Sstevel@tonic-gate 		return (NULL);
30980Sstevel@tonic-gate 	}
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 	oip6h = (ip6_t *)mp->b_rptr;
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	/*
31030Sstevel@tonic-gate 	 * Form the basic IP header first. Zero out the header
31040Sstevel@tonic-gate 	 * so that the mutable fields are zeroed out.
31050Sstevel@tonic-gate 	 */
31060Sstevel@tonic-gate 	ip6h = (ip6_t *)phdr_mp->b_rptr;
31070Sstevel@tonic-gate 	bzero(ip6h, sizeof (ip6_t));
31080Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate 	if (outbound) {
31110Sstevel@tonic-gate 		/*
31120Sstevel@tonic-gate 		 * Include the size of AH and authentication data.
31130Sstevel@tonic-gate 		 * This is how our recipient would compute the
31140Sstevel@tonic-gate 		 * authentication data. Look at what we do in the
31150Sstevel@tonic-gate 		 * inbound case below.
31160Sstevel@tonic-gate 		 */
31170Sstevel@tonic-gate 		ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) +
31180Sstevel@tonic-gate 		    sizeof (ah_t) + ah_align_sz);
31190Sstevel@tonic-gate 	} else {
31200Sstevel@tonic-gate 		ip6h->ip6_plen = oip6h->ip6_plen;
31210Sstevel@tonic-gate 	}
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate 	ip6h->ip6_src = oip6h->ip6_src;
31240Sstevel@tonic-gate 	ip6h->ip6_dst = oip6h->ip6_dst;
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate 	*length_to_skip = IPV6_HDR_LEN;
31270Sstevel@tonic-gate 	if (option_length == 0) {
31280Sstevel@tonic-gate 		/* Form the AH header */
31290Sstevel@tonic-gate 		ip6h->ip6_nxt = IPPROTO_AH;
31300Sstevel@tonic-gate 		((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
31310Sstevel@tonic-gate 		ah_offset = *length_to_skip;
31320Sstevel@tonic-gate 	} else {
31330Sstevel@tonic-gate 		ip6h->ip6_nxt = oip6h->ip6_nxt;
31340Sstevel@tonic-gate 		/* option_length does not include the AH header's size */
31350Sstevel@tonic-gate 		*length_to_skip += option_length;
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 		ah_offset = ah_fix_phdr_v6(ip6h, oip6h, outbound, B_FALSE);
31380Sstevel@tonic-gate 		if (ah_offset == 0) {
31390Sstevel@tonic-gate 			return (NULL);
31400Sstevel@tonic-gate 		}
31410Sstevel@tonic-gate 	}
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 	if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)),
31440Sstevel@tonic-gate 	    (outbound ? NULL : ((ah_t *)((uint8_t *)oip6h + ah_offset))),
31453448Sdh155122 	    assoc, ah_data_sz, ah_align_sz, ahstack)) {
31460Sstevel@tonic-gate 		freeb(phdr_mp);
31470Sstevel@tonic-gate 		/*
31480Sstevel@tonic-gate 		 * Returning NULL will tell the caller to
31490Sstevel@tonic-gate 		 * IPSA_REFELE(), free the memory, etc.
31500Sstevel@tonic-gate 		 */
31510Sstevel@tonic-gate 		return (NULL);
31520Sstevel@tonic-gate 	}
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 	phdr_mp->b_wptr = ((uint8_t *)ip6h + ah_offset + sizeof (ah_t) +
31550Sstevel@tonic-gate 	    ah_align_sz);
31560Sstevel@tonic-gate 	if (!outbound)
31570Sstevel@tonic-gate 		*length_to_skip += sizeof (ah_t) + ah_align_sz;
31580Sstevel@tonic-gate 	return (phdr_mp);
31590Sstevel@tonic-gate }
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate /*
31620Sstevel@tonic-gate  * This function constructs a pseudo header by looking at the IP header
31630Sstevel@tonic-gate  * and options if any. This is called for both outbound and inbound,
31640Sstevel@tonic-gate  * before computing the ICV.
31650Sstevel@tonic-gate  */
31660Sstevel@tonic-gate static mblk_t *
ah_process_ip_options_v4(mblk_t * mp,ipsa_t * assoc,int * length_to_skip,uint_t ah_data_sz,boolean_t outbound,ipsecah_stack_t * ahstack)31670Sstevel@tonic-gate ah_process_ip_options_v4(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
31683448Sdh155122     uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
31690Sstevel@tonic-gate {
31700Sstevel@tonic-gate 	ipoptp_t opts;
31710Sstevel@tonic-gate 	uint32_t option_length;
31720Sstevel@tonic-gate 	ipha_t	*ipha;
31730Sstevel@tonic-gate 	ipha_t	*oipha;
31740Sstevel@tonic-gate 	mblk_t 	*phdr_mp;
31750Sstevel@tonic-gate 	int	 size;
31760Sstevel@tonic-gate 	uchar_t	*optptr;
31770Sstevel@tonic-gate 	uint8_t optval;
31780Sstevel@tonic-gate 	uint8_t optlen;
31790Sstevel@tonic-gate 	ipaddr_t dst;
31800Sstevel@tonic-gate 	uint32_t v_hlen_tos_len;
31810Sstevel@tonic-gate 	int ip_hdr_length;
31820Sstevel@tonic-gate 	uint_t	ah_align_sz;
31830Sstevel@tonic-gate 	uint32_t off;
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
31860Sstevel@tonic-gate #define	V_HLEN	(v_hlen_tos_len >> 24)
31870Sstevel@tonic-gate #else
31880Sstevel@tonic-gate #define	V_HLEN	(v_hlen_tos_len & 0xFF)
31890Sstevel@tonic-gate #endif
31900Sstevel@tonic-gate 
31910Sstevel@tonic-gate 	oipha = (ipha_t *)mp->b_rptr;
31920Sstevel@tonic-gate 	v_hlen_tos_len = ((uint32_t *)oipha)[0];
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	/*
31950Sstevel@tonic-gate 	 * Allocate space for the authentication data also. It is
31960Sstevel@tonic-gate 	 * useful both during the ICV calculation where we need to
31970Sstevel@tonic-gate 	 * feed in zeroes and while sending the datagram back to IP
31980Sstevel@tonic-gate 	 * where we will be using the same space.
31990Sstevel@tonic-gate 	 *
32000Sstevel@tonic-gate 	 * We need to allocate space for padding bytes if it is not
32010Sstevel@tonic-gate 	 * a multiple of IPV4_PADDING_ALIGN.
32020Sstevel@tonic-gate 	 *
32030Sstevel@tonic-gate 	 * In addition, we allocate space for the ICV computed by
32040Sstevel@tonic-gate 	 * the kernel crypto framework, saving us a separate kmem
32050Sstevel@tonic-gate 	 * allocation down the road.
32060Sstevel@tonic-gate 	 */
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 	ah_align_sz = P2ALIGN(ah_data_sz + IPV4_PADDING_ALIGN - 1,
32090Sstevel@tonic-gate 	    IPV4_PADDING_ALIGN);
32100Sstevel@tonic-gate 
32110Sstevel@tonic-gate 	ASSERT(ah_align_sz >= ah_data_sz);
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate 	size = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz +
32140Sstevel@tonic-gate 	    ah_data_sz;
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate 	if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
32170Sstevel@tonic-gate 		option_length = oipha->ipha_version_and_hdr_length -
32180Sstevel@tonic-gate 		    (uint8_t)((IP_VERSION << 4) +
32190Sstevel@tonic-gate 		    IP_SIMPLE_HDR_LENGTH_IN_WORDS);
32200Sstevel@tonic-gate 		option_length <<= 2;
32210Sstevel@tonic-gate 		size += option_length;
32220Sstevel@tonic-gate 	}
32230Sstevel@tonic-gate 
32248778SErik.Nordmark@Sun.COM 	if ((phdr_mp = allocb_tmpl(size, mp)) == NULL) {
32250Sstevel@tonic-gate 		return (NULL);
32260Sstevel@tonic-gate 	}
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	/*
32290Sstevel@tonic-gate 	 * Form the basic IP header first.
32300Sstevel@tonic-gate 	 */
32310Sstevel@tonic-gate 	ipha = (ipha_t *)phdr_mp->b_rptr;
32320Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length = oipha->ipha_version_and_hdr_length;
32330Sstevel@tonic-gate 	ipha->ipha_type_of_service = 0;
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate 	if (outbound) {
32360Sstevel@tonic-gate 		/*
32370Sstevel@tonic-gate 		 * Include the size of AH and authentication data.
32380Sstevel@tonic-gate 		 * This is how our recipient would compute the
32390Sstevel@tonic-gate 		 * authentication data. Look at what we do in the
32400Sstevel@tonic-gate 		 * inbound case below.
32410Sstevel@tonic-gate 		 */
32420Sstevel@tonic-gate 		ipha->ipha_length = ntohs(htons(oipha->ipha_length) +
32430Sstevel@tonic-gate 		    sizeof (ah_t) + ah_align_sz);
32440Sstevel@tonic-gate 	} else {
32450Sstevel@tonic-gate 		ipha->ipha_length = oipha->ipha_length;
32460Sstevel@tonic-gate 	}
32470Sstevel@tonic-gate 
32480Sstevel@tonic-gate 	ipha->ipha_ident = oipha->ipha_ident;
32490Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags = 0;
32500Sstevel@tonic-gate 	ipha->ipha_ttl = 0;
32510Sstevel@tonic-gate 	ipha->ipha_protocol = IPPROTO_AH;
32520Sstevel@tonic-gate 	ipha->ipha_hdr_checksum = 0;
32530Sstevel@tonic-gate 	ipha->ipha_src = oipha->ipha_src;
32540Sstevel@tonic-gate 	ipha->ipha_dst = dst = oipha->ipha_dst;
32550Sstevel@tonic-gate 
32560Sstevel@tonic-gate 	/*
32570Sstevel@tonic-gate 	 * If there is no option to process return now.
32580Sstevel@tonic-gate 	 */
32590Sstevel@tonic-gate 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 	if (V_HLEN == IP_SIMPLE_HDR_VERSION) {
32620Sstevel@tonic-gate 		/* Form the AH header */
32630Sstevel@tonic-gate 		goto ah_hdr;
32640Sstevel@tonic-gate 	}
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate 	ip_hdr_length += option_length;
32670Sstevel@tonic-gate 
32680Sstevel@tonic-gate 	/*
32690Sstevel@tonic-gate 	 * We have options. In the outbound case for source route,
32700Sstevel@tonic-gate 	 * ULP has already moved the first hop, which is now in
32710Sstevel@tonic-gate 	 * ipha_dst. We need the final destination for the calculation
32720Sstevel@tonic-gate 	 * of authentication data. And also make sure that mutable
32730Sstevel@tonic-gate 	 * and experimental fields are zeroed out in the IP options.
32740Sstevel@tonic-gate 	 */
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	bcopy(&oipha[1], &ipha[1], option_length);
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 	for (optval = ipoptp_first(&opts, ipha);
32790Sstevel@tonic-gate 	    optval != IPOPT_EOL;
32800Sstevel@tonic-gate 	    optval = ipoptp_next(&opts)) {
32810Sstevel@tonic-gate 		optptr = opts.ipoptp_cur;
32820Sstevel@tonic-gate 		optlen = opts.ipoptp_len;
32830Sstevel@tonic-gate 		switch (optval) {
32840Sstevel@tonic-gate 		case IPOPT_EXTSEC:
32850Sstevel@tonic-gate 		case IPOPT_COMSEC:
32860Sstevel@tonic-gate 		case IPOPT_RA:
32870Sstevel@tonic-gate 		case IPOPT_SDMDD:
32880Sstevel@tonic-gate 		case IPOPT_SECURITY:
32890Sstevel@tonic-gate 			/*
32900Sstevel@tonic-gate 			 * These options are Immutable, leave them as-is.
32910Sstevel@tonic-gate 			 * Note that IPOPT_NOP is also Immutable, but it
32920Sstevel@tonic-gate 			 * was skipped by ipoptp_next() and thus remains
32930Sstevel@tonic-gate 			 * intact in the header.
32940Sstevel@tonic-gate 			 */
32950Sstevel@tonic-gate 			break;
32960Sstevel@tonic-gate 		case IPOPT_SSRR:
32970Sstevel@tonic-gate 		case IPOPT_LSRR:
32980Sstevel@tonic-gate 			if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0)
32990Sstevel@tonic-gate 				goto bad_ipv4opt;
33000Sstevel@tonic-gate 			/*
33010Sstevel@tonic-gate 			 * These two are mutable and will be zeroed, but
33020Sstevel@tonic-gate 			 * first get the final destination.
33030Sstevel@tonic-gate 			 */
33040Sstevel@tonic-gate 			off = optptr[IPOPT_OFFSET];
33050Sstevel@tonic-gate 			/*
33060Sstevel@tonic-gate 			 * If one of the conditions is true, it means
33070Sstevel@tonic-gate 			 * end of options and dst already has the right
33080Sstevel@tonic-gate 			 * value. So, just fall through.
33090Sstevel@tonic-gate 			 */
33100Sstevel@tonic-gate 			if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
33110Sstevel@tonic-gate 				off = optlen - IP_ADDR_LEN;
33120Sstevel@tonic-gate 				bcopy(&optptr[off], &dst, IP_ADDR_LEN);
33130Sstevel@tonic-gate 			}
33140Sstevel@tonic-gate 			/* FALLTHRU */
33150Sstevel@tonic-gate 		case IPOPT_RR:
33160Sstevel@tonic-gate 		case IPOPT_TS:
33170Sstevel@tonic-gate 		case IPOPT_SATID:
33180Sstevel@tonic-gate 		default:
33190Sstevel@tonic-gate 			/*
33200Sstevel@tonic-gate 			 * optlen should include from the beginning of an
33210Sstevel@tonic-gate 			 * option.
33220Sstevel@tonic-gate 			 * NOTE : Stream Identifier Option (SID): RFC 791
33230Sstevel@tonic-gate 			 * shows the bit pattern of optlen as 2 and documents
33240Sstevel@tonic-gate 			 * the length as 4. We assume it to be 2 here.
33250Sstevel@tonic-gate 			 */
33260Sstevel@tonic-gate 			bzero(optptr, optlen);
33270Sstevel@tonic-gate 			break;
33280Sstevel@tonic-gate 		}
33290Sstevel@tonic-gate 	}
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate 	if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
33320Sstevel@tonic-gate bad_ipv4opt:
33333448Sdh155122 		ah1dbg(ahstack, ("AH : bad IPv4 option"));
33340Sstevel@tonic-gate 		freeb(phdr_mp);
33350Sstevel@tonic-gate 		return (NULL);
33360Sstevel@tonic-gate 	}
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 	/*
33390Sstevel@tonic-gate 	 * Don't change ipha_dst for an inbound datagram as it points
33400Sstevel@tonic-gate 	 * to the right value. Only for the outbound with LSRR/SSRR,
33410Sstevel@tonic-gate 	 * because of ip_massage_options called by the ULP, ipha_dst
33420Sstevel@tonic-gate 	 * points to the first hop and we need to use the final
33430Sstevel@tonic-gate 	 * destination for computing the ICV.
33440Sstevel@tonic-gate 	 */
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 	if (outbound)
33470Sstevel@tonic-gate 		ipha->ipha_dst = dst;
33480Sstevel@tonic-gate ah_hdr:
33490Sstevel@tonic-gate 	((ah_t *)((uint8_t *)ipha + ip_hdr_length))->ah_nexthdr =
33500Sstevel@tonic-gate 	    oipha->ipha_protocol;
33510Sstevel@tonic-gate 	if (!ah_finish_up(((ah_t *)((uint8_t *)ipha + ip_hdr_length)),
33520Sstevel@tonic-gate 	    (outbound ? NULL : ((ah_t *)((uint8_t *)oipha + ip_hdr_length))),
33533448Sdh155122 	    assoc, ah_data_sz, ah_align_sz, ahstack)) {
33540Sstevel@tonic-gate 		freeb(phdr_mp);
33550Sstevel@tonic-gate 		/*
33560Sstevel@tonic-gate 		 * Returning NULL will tell the caller to IPSA_REFELE(), free
33570Sstevel@tonic-gate 		 * the memory, etc.
33580Sstevel@tonic-gate 		 */
33590Sstevel@tonic-gate 		return (NULL);
33600Sstevel@tonic-gate 	}
33610Sstevel@tonic-gate 
33620Sstevel@tonic-gate 	phdr_mp->b_wptr = ((uchar_t *)ipha + ip_hdr_length +
33630Sstevel@tonic-gate 	    sizeof (ah_t) + ah_align_sz);
33640Sstevel@tonic-gate 
33650Sstevel@tonic-gate 	ASSERT(phdr_mp->b_wptr <= phdr_mp->b_datap->db_lim);
33660Sstevel@tonic-gate 	if (outbound)
33670Sstevel@tonic-gate 		*length_to_skip = ip_hdr_length;
33680Sstevel@tonic-gate 	else
33690Sstevel@tonic-gate 		*length_to_skip = ip_hdr_length + sizeof (ah_t) + ah_align_sz;
33700Sstevel@tonic-gate 	return (phdr_mp);
33710Sstevel@tonic-gate }
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate /*
33740Sstevel@tonic-gate  * Authenticate an outbound datagram. This function is called
33750Sstevel@tonic-gate  * whenever IP sends an outbound datagram that needs authentication.
337611042SErik.Nordmark@Sun.COM  * Returns a modified packet if done. Returns NULL if error or queued.
337711042SErik.Nordmark@Sun.COM  * If error return then ipIfStatsOutDiscards has been increased.
33780Sstevel@tonic-gate  */
337911042SErik.Nordmark@Sun.COM static mblk_t *
ah_outbound(mblk_t * data_mp,ip_xmit_attr_t * ixa)338011042SErik.Nordmark@Sun.COM ah_outbound(mblk_t *data_mp, ip_xmit_attr_t *ixa)
33810Sstevel@tonic-gate {
33820Sstevel@tonic-gate 	mblk_t *phdr_mp;
33830Sstevel@tonic-gate 	ipsa_t *assoc;
33840Sstevel@tonic-gate 	int length_to_skip;
33850Sstevel@tonic-gate 	uint_t ah_align_sz;
33860Sstevel@tonic-gate 	uint_t age_bytes;
338711042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ixa->ixa_ipst->ips_netstack;
338811042SErik.Nordmark@Sun.COM 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
338911042SErik.Nordmark@Sun.COM 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
339011042SErik.Nordmark@Sun.COM 	ill_t		*ill = ixa->ixa_nce->nce_ill;
339111042SErik.Nordmark@Sun.COM 	boolean_t	need_refrele = B_FALSE;
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate 	/*
33940Sstevel@tonic-gate 	 * Construct the chain of mblks
33950Sstevel@tonic-gate 	 *
339611042SErik.Nordmark@Sun.COM 	 * PSEUDO_HDR->DATA
33970Sstevel@tonic-gate 	 *
33980Sstevel@tonic-gate 	 * one by one.
33990Sstevel@tonic-gate 	 */
34000Sstevel@tonic-gate 
34013448Sdh155122 	AH_BUMP_STAT(ahstack, out_requests);
34020Sstevel@tonic-gate 
340311042SErik.Nordmark@Sun.COM 	ASSERT(data_mp->b_datap->db_type == M_DATA);
340411042SErik.Nordmark@Sun.COM 
340511042SErik.Nordmark@Sun.COM 	assoc = ixa->ixa_ipsec_ah_sa;
34060Sstevel@tonic-gate 	ASSERT(assoc != NULL);
34070Sstevel@tonic-gate 
340810934Ssommerfeld@sun.com 
340910934Ssommerfeld@sun.com 	/*
341010934Ssommerfeld@sun.com 	 * Get the outer IP header in shape to escape this system..
341110934Ssommerfeld@sun.com 	 */
341211042SErik.Nordmark@Sun.COM 	if (is_system_labeled() && (assoc->ipsa_otsl != NULL)) {
341311042SErik.Nordmark@Sun.COM 		/*
341411042SErik.Nordmark@Sun.COM 		 * Need to update packet with any CIPSO option and update
341511042SErik.Nordmark@Sun.COM 		 * ixa_tsl to capture the new label.
341611042SErik.Nordmark@Sun.COM 		 * We allocate a separate ixa for that purpose.
341711042SErik.Nordmark@Sun.COM 		 */
341811042SErik.Nordmark@Sun.COM 		ixa = ip_xmit_attr_duplicate(ixa);
341911042SErik.Nordmark@Sun.COM 		if (ixa == NULL) {
342011042SErik.Nordmark@Sun.COM 			ip_drop_packet(data_mp, B_FALSE, ill,
342111042SErik.Nordmark@Sun.COM 			    DROPPER(ipss, ipds_ah_nomem),
342210934Ssommerfeld@sun.com 			    &ahstack->ah_dropper);
342311042SErik.Nordmark@Sun.COM 			return (NULL);
342410934Ssommerfeld@sun.com 		}
342511042SErik.Nordmark@Sun.COM 		need_refrele = B_TRUE;
342611042SErik.Nordmark@Sun.COM 
342711042SErik.Nordmark@Sun.COM 		label_hold(assoc->ipsa_otsl);
342811042SErik.Nordmark@Sun.COM 		ip_xmit_attr_replace_tsl(ixa, assoc->ipsa_otsl);
342911042SErik.Nordmark@Sun.COM 
343011042SErik.Nordmark@Sun.COM 		data_mp = sadb_whack_label(data_mp, assoc, ixa,
343111042SErik.Nordmark@Sun.COM 		    DROPPER(ipss, ipds_ah_nomem), &ahstack->ah_dropper);
343211042SErik.Nordmark@Sun.COM 		if (data_mp == NULL) {
343311042SErik.Nordmark@Sun.COM 			/* Packet dropped by sadb_whack_label */
343411042SErik.Nordmark@Sun.COM 			ixa_refrele(ixa);
343511042SErik.Nordmark@Sun.COM 			return (NULL);
343611042SErik.Nordmark@Sun.COM 		}
343710934Ssommerfeld@sun.com 	}
343810934Ssommerfeld@sun.com 
34390Sstevel@tonic-gate 	/*
34400Sstevel@tonic-gate 	 * Age SA according to number of bytes that will be sent after
34410Sstevel@tonic-gate 	 * adding the AH header, ICV, and padding to the packet.
34420Sstevel@tonic-gate 	 */
34430Sstevel@tonic-gate 
344411042SErik.Nordmark@Sun.COM 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
344511042SErik.Nordmark@Sun.COM 		ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
34460Sstevel@tonic-gate 		ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
34470Sstevel@tonic-gate 		    IPV4_PADDING_ALIGN - 1, IPV4_PADDING_ALIGN);
34480Sstevel@tonic-gate 		age_bytes = ntohs(ipha->ipha_length) + sizeof (ah_t) +
34490Sstevel@tonic-gate 		    ah_align_sz;
34500Sstevel@tonic-gate 	} else {
345111042SErik.Nordmark@Sun.COM 		ip6_t *ip6h = (ip6_t *)data_mp->b_rptr;
34520Sstevel@tonic-gate 		ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
34530Sstevel@tonic-gate 		    IPV6_PADDING_ALIGN - 1, IPV6_PADDING_ALIGN);
34540Sstevel@tonic-gate 		age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
34554987Sdanmcd 		    sizeof (ah_t) + ah_align_sz;
34560Sstevel@tonic-gate 	}
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	if (!ah_age_bytes(assoc, age_bytes, B_FALSE)) {
34590Sstevel@tonic-gate 		/* rig things as if ipsec_getassocbyconn() failed */
34600Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
34610Sstevel@tonic-gate 		    "AH association 0x%x, dst %s had bytes expire.\n",
34623448Sdh155122 		    ntohl(assoc->ipsa_spi), assoc->ipsa_dstaddr, AF_INET,
34633448Sdh155122 		    ahstack->ipsecah_netstack);
346411042SErik.Nordmark@Sun.COM 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
346511042SErik.Nordmark@Sun.COM 		ip_drop_output("ipIfStatsOutDiscards", data_mp, ill);
346611042SErik.Nordmark@Sun.COM 		freemsg(data_mp);
346711042SErik.Nordmark@Sun.COM 		if (need_refrele)
346811042SErik.Nordmark@Sun.COM 			ixa_refrele(ixa);
346911042SErik.Nordmark@Sun.COM 		return (NULL);
34700Sstevel@tonic-gate 	}
34710Sstevel@tonic-gate 
347210934Ssommerfeld@sun.com 	/*
347310934Ssommerfeld@sun.com 	 * XXX We need to have fixed up the outer label before we get here.
347410934Ssommerfeld@sun.com 	 * (AH is computing the checksum over the outer label).
347510934Ssommerfeld@sun.com 	 */
347610934Ssommerfeld@sun.com 
34770Sstevel@tonic-gate 	/*
34780Sstevel@tonic-gate 	 * Insert pseudo header:
347911042SErik.Nordmark@Sun.COM 	 * [IP, ULP] => [IP, AH, ICV] -> ULP
34800Sstevel@tonic-gate 	 */
34810Sstevel@tonic-gate 
348211042SErik.Nordmark@Sun.COM 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
348311042SErik.Nordmark@Sun.COM 		phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
348411042SErik.Nordmark@Sun.COM 		    &length_to_skip, assoc->ipsa_mac_len, B_TRUE, ahstack);
34850Sstevel@tonic-gate 	} else {
348611042SErik.Nordmark@Sun.COM 		phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
348711042SErik.Nordmark@Sun.COM 		    &length_to_skip, assoc->ipsa_mac_len, B_TRUE, ahstack);
34880Sstevel@tonic-gate 	}
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate 	if (phdr_mp == NULL) {
34913448Sdh155122 		AH_BUMP_STAT(ahstack, out_discards);
349211042SErik.Nordmark@Sun.COM 		ip_drop_packet(data_mp, B_FALSE, ixa->ixa_nce->nce_ill,
34933448Sdh155122 		    DROPPER(ipss, ipds_ah_bad_v4_opts),
34943448Sdh155122 		    &ahstack->ah_dropper);
349511042SErik.Nordmark@Sun.COM 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
349611042SErik.Nordmark@Sun.COM 		if (need_refrele)
349711042SErik.Nordmark@Sun.COM 			ixa_refrele(ixa);
349811042SErik.Nordmark@Sun.COM 		return (NULL);
34990Sstevel@tonic-gate 	}
35000Sstevel@tonic-gate 
350111042SErik.Nordmark@Sun.COM 	phdr_mp->b_cont = data_mp;
350211042SErik.Nordmark@Sun.COM 	data_mp->b_rptr += length_to_skip;
350311042SErik.Nordmark@Sun.COM 	data_mp = phdr_mp;
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate 	/*
350611042SErik.Nordmark@Sun.COM 	 * At this point data_mp points to
350711042SErik.Nordmark@Sun.COM 	 * an mblk containing the pseudo header (IP header,
35080Sstevel@tonic-gate 	 * AH header, and ICV with mutable fields zero'ed out).
35090Sstevel@tonic-gate 	 * mp points to the mblk containing the ULP data. The original
351011042SErik.Nordmark@Sun.COM 	 * IP header is kept before the ULP data in data_mp.
35110Sstevel@tonic-gate 	 */
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 	/* submit MAC request to KCF */
351411042SErik.Nordmark@Sun.COM 	data_mp = ah_submit_req_outbound(data_mp, ixa, length_to_skip, assoc);
351511042SErik.Nordmark@Sun.COM 	if (need_refrele)
351611042SErik.Nordmark@Sun.COM 		ixa_refrele(ixa);
351711042SErik.Nordmark@Sun.COM 	return (data_mp);
35180Sstevel@tonic-gate }
35190Sstevel@tonic-gate 
352011042SErik.Nordmark@Sun.COM static mblk_t *
ah_inbound(mblk_t * data_mp,void * arg,ip_recv_attr_t * ira)352111042SErik.Nordmark@Sun.COM ah_inbound(mblk_t *data_mp, void *arg, ip_recv_attr_t *ira)
35220Sstevel@tonic-gate {
352311042SErik.Nordmark@Sun.COM 	ah_t		*ah = (ah_t *)arg;
352411042SErik.Nordmark@Sun.COM 	ipsa_t		*assoc = ira->ira_ipsec_ah_sa;
352511042SErik.Nordmark@Sun.COM 	int		length_to_skip;
352611042SErik.Nordmark@Sun.COM 	int		ah_length;
352711042SErik.Nordmark@Sun.COM 	mblk_t		*phdr_mp;
352811042SErik.Nordmark@Sun.COM 	uint32_t	ah_offset;
352911042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ira->ira_ill->ill_ipst->ips_netstack;
35303448Sdh155122 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
35313448Sdh155122 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate 	ASSERT(assoc != NULL);
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate 	/*
35360Sstevel@tonic-gate 	 * We may wish to check replay in-range-only here as an optimization.
35370Sstevel@tonic-gate 	 * Include the reality check of ipsa->ipsa_replay >
35380Sstevel@tonic-gate 	 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
35390Sstevel@tonic-gate 	 * where N == ipsa->ipsa_replay_wsize.
35400Sstevel@tonic-gate 	 *
35410Sstevel@tonic-gate 	 * Another check that may come here later is the "collision" check.
35420Sstevel@tonic-gate 	 * If legitimate packets flow quickly enough, this won't be a problem,
35430Sstevel@tonic-gate 	 * but collisions may cause authentication algorithm crunching to
35440Sstevel@tonic-gate 	 * take place when it doesn't need to.
35450Sstevel@tonic-gate 	 */
35460Sstevel@tonic-gate 	if (!sadb_replay_peek(assoc, ah->ah_replay)) {
35473448Sdh155122 		AH_BUMP_STAT(ahstack, replay_early_failures);
35483448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
354911042SErik.Nordmark@Sun.COM 		ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
35503448Sdh155122 		    DROPPER(ipss, ipds_ah_early_replay),
35513448Sdh155122 		    &ahstack->ah_dropper);
355211042SErik.Nordmark@Sun.COM 		BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
355311042SErik.Nordmark@Sun.COM 		return (NULL);
35540Sstevel@tonic-gate 	}
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate 	/*
35570Sstevel@tonic-gate 	 * The offset of the AH header can be computed from its pointer
35580Sstevel@tonic-gate 	 * within the data mblk, which was pulled up until the AH header
35590Sstevel@tonic-gate 	 * by ipsec_inbound_ah_sa() during SA selection.
35600Sstevel@tonic-gate 	 */
35610Sstevel@tonic-gate 	ah_offset = (uchar_t *)ah - data_mp->b_rptr;
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	/*
35640Sstevel@tonic-gate 	 * We need to pullup until the ICV before we call
35650Sstevel@tonic-gate 	 * ah_process_ip_options_v6.
35660Sstevel@tonic-gate 	 */
35670Sstevel@tonic-gate 	ah_length = (ah->ah_length << 2) + 8;
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 	/*
35700Sstevel@tonic-gate 	 * NOTE : If we want to use any field of IP/AH header, you need
35710Sstevel@tonic-gate 	 * to re-assign following the pullup.
35720Sstevel@tonic-gate 	 */
35730Sstevel@tonic-gate 	if (((uchar_t *)ah + ah_length) > data_mp->b_wptr) {
35740Sstevel@tonic-gate 		if (!pullupmsg(data_mp, (uchar_t *)ah + ah_length -
35750Sstevel@tonic-gate 		    data_mp->b_rptr)) {
35763448Sdh155122 			(void) ipsec_rl_strlog(ns, info.mi_idnum, 0, 0,
35770Sstevel@tonic-gate 			    SL_WARN | SL_ERROR,
35780Sstevel@tonic-gate 			    "ah_inbound: Small AH header\n");
35793448Sdh155122 			IP_AH_BUMP_STAT(ipss, in_discards);
358011042SErik.Nordmark@Sun.COM 			ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
35813448Sdh155122 			    DROPPER(ipss, ipds_ah_nomem),
35823448Sdh155122 			    &ahstack->ah_dropper);
358311042SErik.Nordmark@Sun.COM 			BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
358411042SErik.Nordmark@Sun.COM 			return (NULL);
35850Sstevel@tonic-gate 		}
35860Sstevel@tonic-gate 	}
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate 	/*
35890Sstevel@tonic-gate 	 * Insert pseudo header:
359011042SErik.Nordmark@Sun.COM 	 * [IP, ULP] => [IP, AH, ICV] -> ULP
35910Sstevel@tonic-gate 	 */
359211042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_IS_IPV4) {
35930Sstevel@tonic-gate 		phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
35943448Sdh155122 		    &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
35950Sstevel@tonic-gate 	} else {
35960Sstevel@tonic-gate 		phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
35973448Sdh155122 		    &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
35980Sstevel@tonic-gate 	}
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate 	if (phdr_mp == NULL) {
36013448Sdh155122 		IP_AH_BUMP_STAT(ipss, in_discards);
360211042SErik.Nordmark@Sun.COM 		ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
360311042SErik.Nordmark@Sun.COM 		    ((ira->ira_flags & IRAF_IS_IPV4) ?
36043448Sdh155122 		    DROPPER(ipss, ipds_ah_bad_v4_opts) :
36053448Sdh155122 		    DROPPER(ipss, ipds_ah_bad_v6_hdrs)),
36063448Sdh155122 		    &ahstack->ah_dropper);
360711042SErik.Nordmark@Sun.COM 		BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
360811042SErik.Nordmark@Sun.COM 		return (NULL);
36090Sstevel@tonic-gate 	}
36100Sstevel@tonic-gate 
36110Sstevel@tonic-gate 	phdr_mp->b_cont = data_mp;
36120Sstevel@tonic-gate 	data_mp->b_rptr += length_to_skip;
361311042SErik.Nordmark@Sun.COM 	data_mp = phdr_mp;
36140Sstevel@tonic-gate 
36150Sstevel@tonic-gate 	/* submit request to KCF */
361611042SErik.Nordmark@Sun.COM 	return (ah_submit_req_inbound(data_mp, ira, length_to_skip, ah_offset,
36170Sstevel@tonic-gate 	    assoc));
36180Sstevel@tonic-gate }
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate /*
36210Sstevel@tonic-gate  * Invoked after processing of an inbound packet by the
36220Sstevel@tonic-gate  * kernel crypto framework. Called by ah_submit_req() for a sync request,
36230Sstevel@tonic-gate  * or by the kcf callback for an async request.
362411042SErik.Nordmark@Sun.COM  * Returns NULL if the mblk chain is consumed.
36250Sstevel@tonic-gate  */
362611042SErik.Nordmark@Sun.COM static mblk_t *
ah_auth_in_done(mblk_t * phdr_mp,ip_recv_attr_t * ira,ipsec_crypto_t * ic)362711042SErik.Nordmark@Sun.COM ah_auth_in_done(mblk_t *phdr_mp, ip_recv_attr_t *ira, ipsec_crypto_t *ic)
36280Sstevel@tonic-gate {
36290Sstevel@tonic-gate 	ipha_t *ipha;
36300Sstevel@tonic-gate 	uint_t ah_offset = 0;
36310Sstevel@tonic-gate 	mblk_t *mp;
36323192Sdanmcd 	int align_len, newpos;
36330Sstevel@tonic-gate 	ah_t *ah;
36340Sstevel@tonic-gate 	uint32_t length;
36353192Sdanmcd 	uint32_t *dest32;
36363192Sdanmcd 	uint8_t *dest;
36370Sstevel@tonic-gate 	boolean_t isv4;
36380Sstevel@tonic-gate 	ip6_t *ip6h;
36390Sstevel@tonic-gate 	uint_t icv_len;
36400Sstevel@tonic-gate 	ipsa_t *assoc;
36410Sstevel@tonic-gate 	kstat_named_t *counter;
364211042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ira->ira_ill->ill_ipst->ips_netstack;
364311042SErik.Nordmark@Sun.COM 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
364411042SErik.Nordmark@Sun.COM 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
364511042SErik.Nordmark@Sun.COM 
364611042SErik.Nordmark@Sun.COM 	isv4 = (ira->ira_flags & IRAF_IS_IPV4);
364711042SErik.Nordmark@Sun.COM 	assoc = ira->ira_ipsec_ah_sa;
364811042SErik.Nordmark@Sun.COM 	icv_len = (uint_t)ic->ic_crypto_mac.cd_raw.iov_len;
364911042SErik.Nordmark@Sun.COM 
36500Sstevel@tonic-gate 	if (phdr_mp == NULL) {
365111042SErik.Nordmark@Sun.COM 		ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill,
36523448Sdh155122 		    DROPPER(ipss, ipds_ah_nomem),
36533448Sdh155122 		    &ahstack->ah_dropper);
365411042SErik.Nordmark@Sun.COM 		BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
365511042SErik.Nordmark@Sun.COM 		return (NULL);
36560Sstevel@tonic-gate 	}
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate 	mp = phdr_mp->b_cont;
36590Sstevel@tonic-gate 	if (mp == NULL) {
366011042SErik.Nordmark@Sun.COM 		ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill,
36613448Sdh155122 		    DROPPER(ipss, ipds_ah_nomem),
36623448Sdh155122 		    &ahstack->ah_dropper);
366311042SErik.Nordmark@Sun.COM 		BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
366411042SErik.Nordmark@Sun.COM 		return (NULL);
36650Sstevel@tonic-gate 	}
366611042SErik.Nordmark@Sun.COM 	mp->b_rptr -= ic->ic_skip_len;
36670Sstevel@tonic-gate 
36684987Sdanmcd 	ah_set_usetime(assoc, B_TRUE);
36694987Sdanmcd 
36700Sstevel@tonic-gate 	if (isv4) {
36710Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
36720Sstevel@tonic-gate 		ah_offset = ipha->ipha_version_and_hdr_length -
36730Sstevel@tonic-gate 		    (uint8_t)((IP_VERSION << 4));
36740Sstevel@tonic-gate 		ah_offset <<= 2;
36750Sstevel@tonic-gate 		align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
36760Sstevel@tonic-gate 		    IPV4_PADDING_ALIGN);
36770Sstevel@tonic-gate 	} else {
36780Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
36790Sstevel@tonic-gate 		ah_offset = ipsec_ah_get_hdr_size_v6(mp, B_TRUE);
36800Sstevel@tonic-gate 		ASSERT((mp->b_wptr - mp->b_rptr) >= ah_offset);
36810Sstevel@tonic-gate 		align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
36820Sstevel@tonic-gate 		    IPV6_PADDING_ALIGN);
36830Sstevel@tonic-gate 	}
36840Sstevel@tonic-gate 
36850Sstevel@tonic-gate 	ah = (ah_t *)(mp->b_rptr + ah_offset);
36863192Sdanmcd 	newpos = sizeof (ah_t) + align_len;
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	/*
36890Sstevel@tonic-gate 	 * We get here only when authentication passed.
36900Sstevel@tonic-gate 	 */
36910Sstevel@tonic-gate 
36923448Sdh155122 	ah3dbg(ahstack, ("AH succeeded, checking replay\n"));
36933448Sdh155122 	AH_BUMP_STAT(ahstack, good_auth);
36940Sstevel@tonic-gate 
36950Sstevel@tonic-gate 	if (!sadb_replay_check(assoc, ah->ah_replay)) {
36960Sstevel@tonic-gate 		int af;
36970Sstevel@tonic-gate 		void *addr;
36980Sstevel@tonic-gate 
36990Sstevel@tonic-gate 		if (isv4) {
37000Sstevel@tonic-gate 			addr = &ipha->ipha_dst;
37010Sstevel@tonic-gate 			af = AF_INET;
37020Sstevel@tonic-gate 		} else {
37030Sstevel@tonic-gate 			addr = &ip6h->ip6_dst;
37040Sstevel@tonic-gate 			af = AF_INET6;
37050Sstevel@tonic-gate 		}
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate 		/*
37080Sstevel@tonic-gate 		 * Log the event. As of now we print out an event.
37090Sstevel@tonic-gate 		 * Do not print the replay failure number, or else
37100Sstevel@tonic-gate 		 * syslog cannot collate the error messages.  Printing
37110Sstevel@tonic-gate 		 * the replay number that failed (or printing to the
37120Sstevel@tonic-gate 		 * console) opens a denial-of-service attack.
37130Sstevel@tonic-gate 		 */
37143448Sdh155122 		AH_BUMP_STAT(ahstack, replay_failures);
37150Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, 0,
37160Sstevel@tonic-gate 		    SL_ERROR | SL_WARN,
37170Sstevel@tonic-gate 		    "Replay failed for AH spi %x, dst_addr %s",
37183448Sdh155122 		    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
37193448Sdh155122 		counter = DROPPER(ipss, ipds_ah_replay);
37200Sstevel@tonic-gate 		goto ah_in_discard;
37210Sstevel@tonic-gate 	}
37220Sstevel@tonic-gate 
37230Sstevel@tonic-gate 	/*
37240Sstevel@tonic-gate 	 * We need to remove the AH header from the original
37253192Sdanmcd 	 * datagram. Best way to do this is to move the pre-AH headers
37263192Sdanmcd 	 * forward in the (relatively simple) IPv4 case.  In IPv6, it's
37273192Sdanmcd 	 * a bit more complicated because of IPv6's next-header chaining,
37283192Sdanmcd 	 * but it's doable.
37290Sstevel@tonic-gate 	 */
37300Sstevel@tonic-gate 	if (isv4) {
37310Sstevel@tonic-gate 		/*
37320Sstevel@tonic-gate 		 * Assign the right protocol, adjust the length as we
37330Sstevel@tonic-gate 		 * are removing the AH header and adjust the checksum to
37340Sstevel@tonic-gate 		 * account for the protocol and length.
37350Sstevel@tonic-gate 		 */
37363192Sdanmcd 		length = ntohs(ipha->ipha_length);
37370Sstevel@tonic-gate 		if (!ah_age_bytes(assoc, length, B_TRUE)) {
37380Sstevel@tonic-gate 			/* The ipsa has hit hard expiration, LOG and AUDIT. */
37390Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
37400Sstevel@tonic-gate 			    SL_ERROR | SL_WARN,
37410Sstevel@tonic-gate 			    "AH Association 0x%x, dst %s had bytes expire.\n",
37420Sstevel@tonic-gate 			    assoc->ipsa_spi, assoc->ipsa_dstaddr,
37433448Sdh155122 			    AF_INET, ahstack->ipsecah_netstack);
37443448Sdh155122 			AH_BUMP_STAT(ahstack, bytes_expired);
37453448Sdh155122 			counter = DROPPER(ipss, ipds_ah_bytes_expire);
37460Sstevel@tonic-gate 			goto ah_in_discard;
37470Sstevel@tonic-gate 		}
37483192Sdanmcd 		ipha->ipha_protocol = ah->ah_nexthdr;
37493192Sdanmcd 		length -= newpos;
37503192Sdanmcd 
37513192Sdanmcd 		ipha->ipha_length = htons((uint16_t)length);
37523192Sdanmcd 		ipha->ipha_hdr_checksum = 0;
37533192Sdanmcd 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
37540Sstevel@tonic-gate 	} else {
37550Sstevel@tonic-gate 		uchar_t *whereptr;
37560Sstevel@tonic-gate 		int hdrlen;
37570Sstevel@tonic-gate 		uint8_t *nexthdr;
37580Sstevel@tonic-gate 		ip6_hbh_t *hbhhdr;
37590Sstevel@tonic-gate 		ip6_dest_t *dsthdr;
37600Sstevel@tonic-gate 		ip6_rthdr0_t *rthdr;
37610Sstevel@tonic-gate 
37620Sstevel@tonic-gate 		/*
37630Sstevel@tonic-gate 		 * Make phdr_mp hold until the AH header and make
37640Sstevel@tonic-gate 		 * mp hold everything past AH header.
37650Sstevel@tonic-gate 		 */
37663192Sdanmcd 		length = ntohs(ip6h->ip6_plen);
37670Sstevel@tonic-gate 		if (!ah_age_bytes(assoc, length + sizeof (ip6_t), B_TRUE)) {
37680Sstevel@tonic-gate 			/* The ipsa has hit hard expiration, LOG and AUDIT. */
37690Sstevel@tonic-gate 			ipsec_assocfailure(info.mi_idnum, 0, 0,
37700Sstevel@tonic-gate 			    SL_ERROR | SL_WARN,
37710Sstevel@tonic-gate 			    "AH Association 0x%x, dst %s had bytes "
37720Sstevel@tonic-gate 			    "expire.\n", assoc->ipsa_spi, &ip6h->ip6_dst,
37733448Sdh155122 			    AF_INET6, ahstack->ipsecah_netstack);
37743448Sdh155122 			AH_BUMP_STAT(ahstack, bytes_expired);
37753448Sdh155122 			counter = DROPPER(ipss, ipds_ah_bytes_expire);
37760Sstevel@tonic-gate 			goto ah_in_discard;
37770Sstevel@tonic-gate 		}
37780Sstevel@tonic-gate 
37790Sstevel@tonic-gate 		/*
37800Sstevel@tonic-gate 		 * Update the next header field of the header preceding
37810Sstevel@tonic-gate 		 * AH with the next header field of AH. Start with the
37820Sstevel@tonic-gate 		 * IPv6 header and proceed with the extension headers
37830Sstevel@tonic-gate 		 * until we find what we're looking for.
37840Sstevel@tonic-gate 		 */
37853192Sdanmcd 		nexthdr = &ip6h->ip6_nxt;
37863192Sdanmcd 		whereptr =  (uchar_t *)ip6h;
37870Sstevel@tonic-gate 		hdrlen = sizeof (ip6_t);
37880Sstevel@tonic-gate 
37890Sstevel@tonic-gate 		while (*nexthdr != IPPROTO_AH) {
37900Sstevel@tonic-gate 			whereptr += hdrlen;
37910Sstevel@tonic-gate 			/* Assume IP has already stripped it */
379211042SErik.Nordmark@Sun.COM 			ASSERT(*nexthdr != IPPROTO_FRAGMENT);
37930Sstevel@tonic-gate 			switch (*nexthdr) {
37940Sstevel@tonic-gate 			case IPPROTO_HOPOPTS:
37950Sstevel@tonic-gate 				hbhhdr = (ip6_hbh_t *)whereptr;
37960Sstevel@tonic-gate 				nexthdr = &hbhhdr->ip6h_nxt;
37970Sstevel@tonic-gate 				hdrlen = 8 * (hbhhdr->ip6h_len + 1);
37980Sstevel@tonic-gate 				break;
37990Sstevel@tonic-gate 			case IPPROTO_DSTOPTS:
38000Sstevel@tonic-gate 				dsthdr = (ip6_dest_t *)whereptr;
38010Sstevel@tonic-gate 				nexthdr = &dsthdr->ip6d_nxt;
38020Sstevel@tonic-gate 				hdrlen = 8 * (dsthdr->ip6d_len + 1);
38030Sstevel@tonic-gate 				break;
38040Sstevel@tonic-gate 			case IPPROTO_ROUTING:
38050Sstevel@tonic-gate 				rthdr = (ip6_rthdr0_t *)whereptr;
38060Sstevel@tonic-gate 				nexthdr = &rthdr->ip6r0_nxt;
38070Sstevel@tonic-gate 				hdrlen = 8 * (rthdr->ip6r0_len + 1);
38080Sstevel@tonic-gate 				break;
38090Sstevel@tonic-gate 			}
38100Sstevel@tonic-gate 		}
38110Sstevel@tonic-gate 		*nexthdr = ah->ah_nexthdr;
38123192Sdanmcd 		length -= newpos;
38133192Sdanmcd 		ip6h->ip6_plen = htons((uint16_t)length);
38140Sstevel@tonic-gate 	}
38150Sstevel@tonic-gate 
38163192Sdanmcd 	/* Now that we've fixed the IP header, move it forward. */
38173192Sdanmcd 	mp->b_rptr += newpos;
38183192Sdanmcd 	if (IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) {
38193192Sdanmcd 		dest32 = (uint32_t *)(mp->b_rptr + ah_offset);
38203192Sdanmcd 		while (--dest32 >= (uint32_t *)mp->b_rptr)
38213192Sdanmcd 			*dest32 = *(dest32 - (newpos >> 2));
38223192Sdanmcd 	} else {
38233192Sdanmcd 		dest = mp->b_rptr + ah_offset;
38243192Sdanmcd 		while (--dest >= mp->b_rptr)
38253192Sdanmcd 			*dest = *(dest - newpos);
38263192Sdanmcd 	}
38273192Sdanmcd 	freeb(phdr_mp);
382810934Ssommerfeld@sun.com 
382910934Ssommerfeld@sun.com 	/*
383010934Ssommerfeld@sun.com 	 * If SA is labelled, use its label, else inherit the label
383110934Ssommerfeld@sun.com 	 */
383211042SErik.Nordmark@Sun.COM 	if (is_system_labeled() && (assoc->ipsa_tsl != NULL)) {
383311042SErik.Nordmark@Sun.COM 		if (!ip_recv_attr_replace_label(ira, assoc->ipsa_tsl)) {
383411042SErik.Nordmark@Sun.COM 			ip_drop_packet(mp, B_TRUE, ira->ira_ill,
383511042SErik.Nordmark@Sun.COM 			    DROPPER(ipss, ipds_ah_nomem), &ahstack->ah_dropper);
383611042SErik.Nordmark@Sun.COM 			BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
383711042SErik.Nordmark@Sun.COM 			return (NULL);
383811042SErik.Nordmark@Sun.COM 		}
383910934Ssommerfeld@sun.com 	}
384010934Ssommerfeld@sun.com 
38417885Sdanmcd@sun.com 	if (assoc->ipsa_state == IPSA_STATE_IDLE) {
38427885Sdanmcd@sun.com 		/*
38437885Sdanmcd@sun.com 		 * Cluster buffering case.  Tell caller that we're
38447885Sdanmcd@sun.com 		 * handling the packet.
38457885Sdanmcd@sun.com 		 */
384611042SErik.Nordmark@Sun.COM 		sadb_buf_pkt(assoc, mp, ira);
384711042SErik.Nordmark@Sun.COM 		return (NULL);
38487885Sdanmcd@sun.com 	}
384910934Ssommerfeld@sun.com 
385011042SErik.Nordmark@Sun.COM 	return (mp);
38510Sstevel@tonic-gate 
38520Sstevel@tonic-gate ah_in_discard:
38533448Sdh155122 	IP_AH_BUMP_STAT(ipss, in_discards);
385411042SErik.Nordmark@Sun.COM 	ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill, counter,
38553448Sdh155122 	    &ahstack->ah_dropper);
385611042SErik.Nordmark@Sun.COM 	BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
385711042SErik.Nordmark@Sun.COM 	return (NULL);
38580Sstevel@tonic-gate }
38590Sstevel@tonic-gate 
38600Sstevel@tonic-gate /*
38610Sstevel@tonic-gate  * Invoked after processing of an outbound packet by the
38620Sstevel@tonic-gate  * kernel crypto framework, either by ah_submit_req() for a request
38630Sstevel@tonic-gate  * executed syncrhonously, or by the KEF callback for a request
38640Sstevel@tonic-gate  * executed asynchronously.
38650Sstevel@tonic-gate  */
386611042SErik.Nordmark@Sun.COM static mblk_t *
ah_auth_out_done(mblk_t * phdr_mp,ip_xmit_attr_t * ixa,ipsec_crypto_t * ic)386711042SErik.Nordmark@Sun.COM ah_auth_out_done(mblk_t *phdr_mp, ip_xmit_attr_t *ixa, ipsec_crypto_t *ic)
38680Sstevel@tonic-gate {
38690Sstevel@tonic-gate 	mblk_t *mp;
38700Sstevel@tonic-gate 	int align_len;
38710Sstevel@tonic-gate 	uint32_t hdrs_length;
38720Sstevel@tonic-gate 	uchar_t *ptr;
38730Sstevel@tonic-gate 	uint32_t length;
38740Sstevel@tonic-gate 	boolean_t isv4;
38750Sstevel@tonic-gate 	size_t icv_len;
387611042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ixa->ixa_ipst->ips_netstack;
387711042SErik.Nordmark@Sun.COM 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
387811042SErik.Nordmark@Sun.COM 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
387911042SErik.Nordmark@Sun.COM 	ill_t		*ill = ixa->ixa_nce->nce_ill;
388011042SErik.Nordmark@Sun.COM 
388111042SErik.Nordmark@Sun.COM 	isv4 = (ixa->ixa_flags & IXAF_IS_IPV4);
388211042SErik.Nordmark@Sun.COM 	icv_len = ic->ic_crypto_mac.cd_raw.iov_len;
38830Sstevel@tonic-gate 
38840Sstevel@tonic-gate 	mp = phdr_mp->b_cont;
38850Sstevel@tonic-gate 	if (mp == NULL) {
388611042SErik.Nordmark@Sun.COM 		ip_drop_packet(phdr_mp, B_FALSE, ill,
38873448Sdh155122 		    DROPPER(ipss, ipds_ah_nomem),
38883448Sdh155122 		    &ahstack->ah_dropper);
388911042SErik.Nordmark@Sun.COM 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
389011042SErik.Nordmark@Sun.COM 		return (NULL);
38910Sstevel@tonic-gate 	}
389211042SErik.Nordmark@Sun.COM 	mp->b_rptr -= ic->ic_skip_len;
389311042SErik.Nordmark@Sun.COM 
389411042SErik.Nordmark@Sun.COM 	ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
389511042SErik.Nordmark@Sun.COM 	ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
389611042SErik.Nordmark@Sun.COM 	ah_set_usetime(ixa->ixa_ipsec_ah_sa, B_FALSE);
38974987Sdanmcd 
38980Sstevel@tonic-gate 	if (isv4) {
38990Sstevel@tonic-gate 		ipha_t *ipha;
39000Sstevel@tonic-gate 		ipha_t *nipha;
39010Sstevel@tonic-gate 
39020Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
39030Sstevel@tonic-gate 		hdrs_length = ipha->ipha_version_and_hdr_length -
39040Sstevel@tonic-gate 		    (uint8_t)((IP_VERSION << 4));
39050Sstevel@tonic-gate 		hdrs_length <<= 2;
39060Sstevel@tonic-gate 		align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
39070Sstevel@tonic-gate 		    IPV4_PADDING_ALIGN);
39080Sstevel@tonic-gate 		/*
39090Sstevel@tonic-gate 		 * phdr_mp must have the right amount of space for the
39100Sstevel@tonic-gate 		 * combined IP and AH header. Copy the IP header and
39110Sstevel@tonic-gate 		 * the ack_data onto AH. Note that the AH header was
39120Sstevel@tonic-gate 		 * already formed before the ICV calculation and hence
39130Sstevel@tonic-gate 		 * you don't have to copy it here.
39140Sstevel@tonic-gate 		 */
39150Sstevel@tonic-gate 		bcopy(mp->b_rptr, phdr_mp->b_rptr, hdrs_length);
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 		ptr = phdr_mp->b_rptr + hdrs_length + sizeof (ah_t);
39180Sstevel@tonic-gate 		bcopy(phdr_mp->b_wptr, ptr, icv_len);
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 		/*
39210Sstevel@tonic-gate 		 * Compute the new header checksum as we are assigning
39220Sstevel@tonic-gate 		 * IPPROTO_AH and adjusting the length here.
39230Sstevel@tonic-gate 		 */
39240Sstevel@tonic-gate 		nipha = (ipha_t *)phdr_mp->b_rptr;
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 		nipha->ipha_protocol = IPPROTO_AH;
39270Sstevel@tonic-gate 		length = ntohs(nipha->ipha_length);
39280Sstevel@tonic-gate 		length += (sizeof (ah_t) + align_len);
39290Sstevel@tonic-gate 		nipha->ipha_length = htons((uint16_t)length);
39300Sstevel@tonic-gate 		nipha->ipha_hdr_checksum = 0;
39310Sstevel@tonic-gate 		nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
39320Sstevel@tonic-gate 	} else {
39330Sstevel@tonic-gate 		ip6_t *ip6h;
39340Sstevel@tonic-gate 		ip6_t *nip6h;
39350Sstevel@tonic-gate 		uint_t ah_offset;
39360Sstevel@tonic-gate 
39370Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
39380Sstevel@tonic-gate 		nip6h = (ip6_t *)phdr_mp->b_rptr;
39390Sstevel@tonic-gate 		align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
39400Sstevel@tonic-gate 		    IPV6_PADDING_ALIGN);
39410Sstevel@tonic-gate 		/*
39420Sstevel@tonic-gate 		 * phdr_mp must have the right amount of space for the
39430Sstevel@tonic-gate 		 * combined IP and AH header. Copy the IP header with
39440Sstevel@tonic-gate 		 * options into the pseudo header. When we constructed
39450Sstevel@tonic-gate 		 * a pseudo header, we did not copy some of the mutable
39460Sstevel@tonic-gate 		 * fields. We do it now by calling ah_fix_phdr_v6()
39470Sstevel@tonic-gate 		 * with the last argument B_TRUE. It returns the
39480Sstevel@tonic-gate 		 * ah_offset into the pseudo header.
39490Sstevel@tonic-gate 		 */
39500Sstevel@tonic-gate 
39510Sstevel@tonic-gate 		bcopy(ip6h, nip6h, IPV6_HDR_LEN);
39520Sstevel@tonic-gate 		ah_offset = ah_fix_phdr_v6(nip6h, ip6h, B_TRUE, B_TRUE);
39530Sstevel@tonic-gate 		ASSERT(ah_offset != 0);
39540Sstevel@tonic-gate 		/*
39550Sstevel@tonic-gate 		 * phdr_mp can hold exactly the whole IP header with options
39560Sstevel@tonic-gate 		 * plus the AH header also. Thus subtracting the AH header's
39570Sstevel@tonic-gate 		 * size should give exactly how much of the original header
39580Sstevel@tonic-gate 		 * should be skipped.
39590Sstevel@tonic-gate 		 */
39600Sstevel@tonic-gate 		hdrs_length = (phdr_mp->b_wptr - phdr_mp->b_rptr) -
39610Sstevel@tonic-gate 		    sizeof (ah_t) - icv_len;
39620Sstevel@tonic-gate 		bcopy(phdr_mp->b_wptr, ((uint8_t *)nip6h + ah_offset +
39630Sstevel@tonic-gate 		    sizeof (ah_t)), icv_len);
39640Sstevel@tonic-gate 		length = ntohs(nip6h->ip6_plen);
39650Sstevel@tonic-gate 		length += (sizeof (ah_t) + align_len);
39660Sstevel@tonic-gate 		nip6h->ip6_plen = htons((uint16_t)length);
39670Sstevel@tonic-gate 	}
39680Sstevel@tonic-gate 
39690Sstevel@tonic-gate 	/* Skip the original IP header */
39700Sstevel@tonic-gate 	mp->b_rptr += hdrs_length;
39710Sstevel@tonic-gate 	if (mp->b_rptr == mp->b_wptr) {
39720Sstevel@tonic-gate 		phdr_mp->b_cont = mp->b_cont;
39730Sstevel@tonic-gate 		freeb(mp);
39740Sstevel@tonic-gate 	}
39750Sstevel@tonic-gate 
397611042SErik.Nordmark@Sun.COM 	return (phdr_mp);
39770Sstevel@tonic-gate }
39780Sstevel@tonic-gate 
397910934Ssommerfeld@sun.com /* Refactor me */
39800Sstevel@tonic-gate /*
39810Sstevel@tonic-gate  * Wrapper to allow IP to trigger an AH association failure message
39820Sstevel@tonic-gate  * during SA inbound selection.
39830Sstevel@tonic-gate  */
39840Sstevel@tonic-gate void
ipsecah_in_assocfailure(mblk_t * mp,char level,ushort_t sl,char * fmt,uint32_t spi,void * addr,int af,ip_recv_attr_t * ira)39850Sstevel@tonic-gate ipsecah_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
398611042SErik.Nordmark@Sun.COM     uint32_t spi, void *addr, int af, ip_recv_attr_t *ira)
39870Sstevel@tonic-gate {
398811042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ira->ira_ill->ill_ipst->ips_netstack;
398911042SErik.Nordmark@Sun.COM 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
399011042SErik.Nordmark@Sun.COM 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
39913448Sdh155122 
39923448Sdh155122 	if (ahstack->ipsecah_log_unknown_spi) {
39930Sstevel@tonic-gate 		ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
39943448Sdh155122 		    addr, af, ahstack->ipsecah_netstack);
39950Sstevel@tonic-gate 	}
39960Sstevel@tonic-gate 
399711042SErik.Nordmark@Sun.COM 	ip_drop_packet(mp, B_TRUE, ira->ira_ill,
39983448Sdh155122 	    DROPPER(ipss, ipds_ah_no_sa),
39993448Sdh155122 	    &ahstack->ah_dropper);
40000Sstevel@tonic-gate }
40010Sstevel@tonic-gate 
40020Sstevel@tonic-gate /*
40030Sstevel@tonic-gate  * Initialize the AH input and output processing functions.
40040Sstevel@tonic-gate  */
40050Sstevel@tonic-gate void
ipsecah_init_funcs(ipsa_t * sa)40060Sstevel@tonic-gate ipsecah_init_funcs(ipsa_t *sa)
40070Sstevel@tonic-gate {
40080Sstevel@tonic-gate 	if (sa->ipsa_output_func == NULL)
40090Sstevel@tonic-gate 		sa->ipsa_output_func = ah_outbound;
40100Sstevel@tonic-gate 	if (sa->ipsa_input_func == NULL)
40110Sstevel@tonic-gate 		sa->ipsa_input_func = ah_inbound;
40120Sstevel@tonic-gate }
4013