xref: /titanic_50/usr/src/uts/common/inet/ip/ipsecah.c (revision 1e1ddd6cc98ab5af8293f7ebd132be62900730fd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/stropts.h>
31 #include <sys/errno.h>
32 #include <sys/strlog.h>
33 #include <sys/tihdr.h>
34 #include <sys/socket.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/kmem.h>
38 #include <sys/zone.h>
39 #include <sys/sysmacros.h>
40 #include <sys/cmn_err.h>
41 #include <sys/vtrace.h>
42 #include <sys/debug.h>
43 #include <sys/atomic.h>
44 #include <sys/strsun.h>
45 #include <sys/random.h>
46 #include <netinet/in.h>
47 #include <net/if.h>
48 #include <netinet/ip6.h>
49 #include <netinet/icmp6.h>
50 #include <net/pfkeyv2.h>
51 
52 #include <inet/common.h>
53 #include <inet/mi.h>
54 #include <inet/ip.h>
55 #include <inet/ip6.h>
56 #include <inet/nd.h>
57 #include <inet/ipsec_info.h>
58 #include <inet/ipsec_impl.h>
59 #include <inet/sadb.h>
60 #include <inet/ipsecah.h>
61 #include <inet/ipsec_impl.h>
62 #include <inet/ipdrop.h>
63 #include <sys/taskq.h>
64 #include <sys/policy.h>
65 #include <sys/iphada.h>
66 #include <sys/strsun.h>
67 
68 #include <sys/crypto/common.h>
69 #include <sys/crypto/api.h>
70 #include <sys/kstat.h>
71 #include <sys/strsubr.h>
72 
73 /*
74  * Table of ND variables supported by ipsecah. These are loaded into
75  * ipsecah_g_nd in ipsecah_init_nd.
76  * All of these are alterable, within the min/max values given, at run time.
77  */
78 static	ipsecahparam_t	lcl_param_arr[] = {
79 	/* min	max			value	name */
80 	{ 0,	3,			0,	"ipsecah_debug"},
81 	{ 125,	32000, SADB_AGE_INTERVAL_DEFAULT,	"ipsecah_age_interval"},
82 	{ 1,	10,			1,	"ipsecah_reap_delay"},
83 	{ 1,	SADB_MAX_REPLAY,	64,	"ipsecah_replay_size"},
84 	{ 1,	300,			15,	"ipsecah_acquire_timeout"},
85 	{ 1,	1800,			90,	"ipsecah_larval_timeout"},
86 	/* Default lifetime values for ACQUIRE messages. */
87 	{ 0,	0xffffffffU,		0,	"ipsecah_default_soft_bytes"},
88 	{ 0,	0xffffffffU,		0,	"ipsecah_default_hard_bytes"},
89 	{ 0,	0xffffffffU,		24000,	"ipsecah_default_soft_addtime"},
90 	{ 0,	0xffffffffU,		28800,	"ipsecah_default_hard_addtime"},
91 	{ 0,	0xffffffffU,		0,	"ipsecah_default_soft_usetime"},
92 	{ 0,	0xffffffffU,		0,	"ipsecah_default_hard_usetime"},
93 	{ 0,	1,			0,	"ipsecah_log_unknown_spi"},
94 };
95 #define	ipsecah_debug			ipsecah_params[0].ipsecah_param_value
96 #define	ipsecah_age_interval		ipsecah_params[1].ipsecah_param_value
97 #define	ipsecah_age_int_max		ipsecah_params[1].ipsecah_param_max
98 #define	ipsecah_reap_delay		ipsecah_params[2].ipsecah_param_value
99 #define	ipsecah_replay_size		ipsecah_params[3].ipsecah_param_value
100 #define	ipsecah_acquire_timeout		ipsecah_params[4].ipsecah_param_value
101 #define	ipsecah_larval_timeout		ipsecah_params[5].ipsecah_param_value
102 #define	ipsecah_default_soft_bytes	ipsecah_params[6].ipsecah_param_value
103 #define	ipsecah_default_hard_bytes	ipsecah_params[7].ipsecah_param_value
104 #define	ipsecah_default_soft_addtime	ipsecah_params[8].ipsecah_param_value
105 #define	ipsecah_default_hard_addtime	ipsecah_params[9].ipsecah_param_value
106 #define	ipsecah_default_soft_usetime	ipsecah_params[10].ipsecah_param_value
107 #define	ipsecah_default_hard_usetime	ipsecah_params[11].ipsecah_param_value
108 #define	ipsecah_log_unknown_spi		ipsecah_params[12].ipsecah_param_value
109 
110 #define	ah0dbg(a)	printf a
111 /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
112 #define	ah1dbg(ahstack, a)	if (ahstack->ipsecah_debug != 0) printf a
113 #define	ah2dbg(ahstack, a)	if (ahstack->ipsecah_debug > 1) printf a
114 #define	ah3dbg(ahstack, a)	if (ahstack->ipsecah_debug > 2) printf a
115 
116 /*
117  * XXX This is broken. Padding should be determined dynamically
118  * depending on the ICV size and IP version number so that the
119  * total AH header size is a multiple of 32 bits or 64 bits
120  * for V4 and V6 respectively. For 96bit ICVs we have no problems.
121  * Anything different from that, we need to fix our code.
122  */
123 #define	IPV4_PADDING_ALIGN	0x04	/* Multiple of 32 bits */
124 #define	IPV6_PADDING_ALIGN	0x04	/* Multiple of 32 bits */
125 
126 /*
127  * Helper macro. Avoids a call to msgdsize if there is only one
128  * mblk in the chain.
129  */
130 #define	AH_MSGSIZE(mp) ((mp)->b_cont != NULL ? msgdsize(mp) : MBLKL(mp))
131 
132 
133 static ipsec_status_t ah_auth_out_done(mblk_t *);
134 static ipsec_status_t ah_auth_in_done(mblk_t *);
135 static mblk_t *ah_process_ip_options_v4(mblk_t *, ipsa_t *, int *, uint_t,
136     boolean_t, ipsecah_stack_t *);
137 static mblk_t *ah_process_ip_options_v6(mblk_t *, ipsa_t *, int *, uint_t,
138     boolean_t, ipsecah_stack_t *);
139 static void ah_getspi(mblk_t *, keysock_in_t *, ipsecah_stack_t *);
140 static ipsec_status_t ah_inbound_accelerated(mblk_t *, boolean_t, ipsa_t *,
141     uint32_t);
142 static ipsec_status_t ah_outbound_accelerated_v4(mblk_t *, ipsa_t *);
143 static ipsec_status_t ah_outbound_accelerated_v6(mblk_t *, ipsa_t *);
144 static ipsec_status_t ah_outbound(mblk_t *);
145 
146 static int ipsecah_open(queue_t *, dev_t *, int, int, cred_t *);
147 static int ipsecah_close(queue_t *);
148 static void ipsecah_rput(queue_t *, mblk_t *);
149 static void ipsecah_wput(queue_t *, mblk_t *);
150 static void ah_send_acquire(ipsacq_t *, mblk_t *, netstack_t *);
151 static boolean_t ah_register_out(uint32_t, uint32_t, uint_t, ipsecah_stack_t *);
152 static void	*ipsecah_stack_init(netstackid_t stackid, netstack_t *ns);
153 static void	ipsecah_stack_fini(netstackid_t stackid, void *arg);
154 
155 /* Setable in /etc/system */
156 uint32_t ah_hash_size = IPSEC_DEFAULT_HASH_SIZE;
157 
158 static taskq_t *ah_taskq;
159 
160 static struct module_info info = {
161 	5136, "ipsecah", 0, INFPSZ, 65536, 1024
162 };
163 
164 static struct qinit rinit = {
165 	(pfi_t)ipsecah_rput, NULL, ipsecah_open, ipsecah_close, NULL, &info,
166 	NULL
167 };
168 
169 static struct qinit winit = {
170 	(pfi_t)ipsecah_wput, NULL, ipsecah_open, ipsecah_close, NULL, &info,
171 	NULL
172 };
173 
174 struct streamtab ipsecahinfo = {
175 	&rinit, &winit, NULL, NULL
176 };
177 
178 static int ah_kstat_update(kstat_t *, int);
179 
180 uint64_t ipsacq_maxpackets = IPSACQ_MAXPACKETS;
181 
182 static boolean_t
183 ah_kstat_init(ipsecah_stack_t *ahstack, netstackid_t stackid)
184 {
185 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
186 
187 	ahstack->ah_ksp = kstat_create_netstack("ipsecah", 0, "ah_stat", "net",
188 	    KSTAT_TYPE_NAMED, sizeof (ah_kstats_t) / sizeof (kstat_named_t),
189 	    KSTAT_FLAG_PERSISTENT, stackid);
190 
191 	if (ahstack->ah_ksp == NULL || ahstack->ah_ksp->ks_data == NULL)
192 		return (B_FALSE);
193 
194 	ahstack->ah_kstats = ahstack->ah_ksp->ks_data;
195 
196 	ahstack->ah_ksp->ks_update = ah_kstat_update;
197 	ahstack->ah_ksp->ks_private = (void *)(uintptr_t)stackid;
198 
199 #define	K64 KSTAT_DATA_UINT64
200 #define	KI(x) kstat_named_init(&(ahstack->ah_kstats->ah_stat_##x), #x, K64)
201 
202 	KI(num_aalgs);
203 	KI(good_auth);
204 	KI(bad_auth);
205 	KI(replay_failures);
206 	KI(replay_early_failures);
207 	KI(keysock_in);
208 	KI(out_requests);
209 	KI(acquire_requests);
210 	KI(bytes_expired);
211 	KI(out_discards);
212 	KI(in_accelerated);
213 	KI(out_accelerated);
214 	KI(noaccel);
215 	KI(crypto_sync);
216 	KI(crypto_async);
217 	KI(crypto_failures);
218 
219 #undef KI
220 #undef K64
221 
222 	kstat_install(ahstack->ah_ksp);
223 	IP_ACQUIRE_STAT(ipss, maxpackets, ipsacq_maxpackets);
224 	return (B_TRUE);
225 }
226 
227 static int
228 ah_kstat_update(kstat_t *kp, int rw)
229 {
230 	ah_kstats_t	*ekp;
231 	netstackid_t	stackid = (netstackid_t)(uintptr_t)kp->ks_private;
232 	netstack_t	*ns;
233 	ipsec_stack_t	*ipss;
234 
235 	if ((kp == NULL) || (kp->ks_data == NULL))
236 		return (EIO);
237 
238 	if (rw == KSTAT_WRITE)
239 		return (EACCES);
240 
241 	ns = netstack_find_by_stackid(stackid);
242 	if (ns == NULL)
243 		return (-1);
244 	ipss = ns->netstack_ipsec;
245 	if (ipss == NULL) {
246 		netstack_rele(ns);
247 		return (-1);
248 	}
249 	ekp = (ah_kstats_t *)kp->ks_data;
250 
251 	mutex_enter(&ipss->ipsec_alg_lock);
252 	ekp->ah_stat_num_aalgs.value.ui64 = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
253 	mutex_exit(&ipss->ipsec_alg_lock);
254 
255 	netstack_rele(ns);
256 	return (0);
257 }
258 
259 /*
260  * Don't have to lock ipsec_age_interval, as only one thread will access it at
261  * a time, because I control the one function that does a qtimeout() on
262  * ah_pfkey_q.
263  */
264 static void
265 ah_ager(void *arg)
266 {
267 	ipsecah_stack_t *ahstack = (ipsecah_stack_t *)arg;
268 	netstack_t	*ns = ahstack->ipsecah_netstack;
269 	hrtime_t begin = gethrtime();
270 
271 	sadb_ager(&ahstack->ah_sadb.s_v4, ahstack->ah_pfkey_q,
272 	    ahstack->ah_sadb.s_ip_q, ahstack->ipsecah_reap_delay, ns);
273 	sadb_ager(&ahstack->ah_sadb.s_v6, ahstack->ah_pfkey_q,
274 	    ahstack->ah_sadb.s_ip_q, ahstack->ipsecah_reap_delay, ns);
275 
276 	ahstack->ah_event = sadb_retimeout(begin, ahstack->ah_pfkey_q,
277 	    ah_ager, ahstack,
278 	    &ahstack->ipsecah_age_interval, ahstack->ipsecah_age_int_max,
279 	    info.mi_idnum);
280 }
281 
282 /*
283  * Get an AH NDD parameter.
284  */
285 /* ARGSUSED */
286 static int
287 ipsecah_param_get(q, mp, cp, cr)
288 	queue_t	*q;
289 	mblk_t	*mp;
290 	caddr_t	cp;
291 	cred_t *cr;
292 {
293 	ipsecahparam_t	*ipsecahpa = (ipsecahparam_t *)cp;
294 	uint_t value;
295 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
296 
297 	mutex_enter(&ahstack->ipsecah_param_lock);
298 	value = ipsecahpa->ipsecah_param_value;
299 	mutex_exit(&ahstack->ipsecah_param_lock);
300 
301 	(void) mi_mpprintf(mp, "%u", value);
302 	return (0);
303 }
304 
305 /*
306  * This routine sets an NDD variable in a ipsecahparam_t structure.
307  */
308 /* ARGSUSED */
309 static int
310 ipsecah_param_set(q, mp, value, cp, cr)
311 	queue_t	*q;
312 	mblk_t	*mp;
313 	char	*value;
314 	caddr_t	cp;
315 	cred_t *cr;
316 {
317 	ulong_t	new_value;
318 	ipsecahparam_t	*ipsecahpa = (ipsecahparam_t *)cp;
319 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
320 
321 	/*
322 	 * Fail the request if the new value does not lie within the
323 	 * required bounds.
324 	 */
325 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
326 	    new_value < ipsecahpa->ipsecah_param_min ||
327 	    new_value > ipsecahpa->ipsecah_param_max) {
328 		    return (EINVAL);
329 	}
330 
331 	/* Set the new value */
332 	mutex_enter(&ahstack->ipsecah_param_lock);
333 	ipsecahpa->ipsecah_param_value = new_value;
334 	mutex_exit(&ahstack->ipsecah_param_lock);
335 	return (0);
336 }
337 
338 /*
339  * Using lifetime NDD variables, fill in an extended combination's
340  * lifetime information.
341  */
342 void
343 ipsecah_fill_defs(sadb_x_ecomb_t *ecomb, netstack_t *ns)
344 {
345 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
346 
347 	ecomb->sadb_x_ecomb_soft_bytes = ahstack->ipsecah_default_soft_bytes;
348 	ecomb->sadb_x_ecomb_hard_bytes = ahstack->ipsecah_default_hard_bytes;
349 	ecomb->sadb_x_ecomb_soft_addtime =
350 	    ahstack->ipsecah_default_soft_addtime;
351 	ecomb->sadb_x_ecomb_hard_addtime =
352 	    ahstack->ipsecah_default_hard_addtime;
353 	ecomb->sadb_x_ecomb_soft_usetime =
354 	    ahstack->ipsecah_default_soft_usetime;
355 	ecomb->sadb_x_ecomb_hard_usetime =
356 	    ahstack->ipsecah_default_hard_usetime;
357 }
358 
359 /*
360  * Initialize things for AH at module load time.
361  */
362 boolean_t
363 ipsecah_ddi_init(void)
364 {
365 	ah_taskq = taskq_create("ah_taskq", 1, minclsyspri,
366 	    IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0);
367 
368 	/*
369 	 * We want to be informed each time a stack is created or
370 	 * destroyed in the kernel, so we can maintain the
371 	 * set of ipsecah_stack_t's.
372 	 */
373 	netstack_register(NS_IPSECAH, ipsecah_stack_init, NULL,
374 	    ipsecah_stack_fini);
375 
376 	return (B_TRUE);
377 }
378 
379 /*
380  * Walk through the param array specified registering each element with the
381  * named dispatch handler.
382  */
383 static boolean_t
384 ipsecah_param_register(IDP *ndp, ipsecahparam_t *ahp, int cnt)
385 {
386 	for (; cnt-- > 0; ahp++) {
387 		if (ahp->ipsecah_param_name != NULL &&
388 		    ahp->ipsecah_param_name[0]) {
389 			if (!nd_load(ndp,
390 			    ahp->ipsecah_param_name,
391 			    ipsecah_param_get, ipsecah_param_set,
392 			    (caddr_t)ahp)) {
393 				nd_free(ndp);
394 				return (B_FALSE);
395 			}
396 		}
397 	}
398 	return (B_TRUE);
399 }
400 
401 /*
402  * Initialize things for AH for each stack instance
403  */
404 static void *
405 ipsecah_stack_init(netstackid_t stackid, netstack_t *ns)
406 {
407 	ipsecah_stack_t	*ahstack;
408 	ipsecahparam_t	*ahp;
409 
410 	ahstack = (ipsecah_stack_t *)kmem_zalloc(sizeof (*ahstack), KM_SLEEP);
411 	ahstack->ipsecah_netstack = ns;
412 
413 	ahp = (ipsecahparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
414 	ahstack->ipsecah_params = ahp;
415 	bcopy(lcl_param_arr, ahp, sizeof (lcl_param_arr));
416 
417 	(void) ipsecah_param_register(&ahstack->ipsecah_g_nd, ahp,
418 	    A_CNT(lcl_param_arr));
419 
420 	(void) ah_kstat_init(ahstack, stackid);
421 
422 	ahstack->ah_sadb.s_acquire_timeout = &ahstack->ipsecah_acquire_timeout;
423 	ahstack->ah_sadb.s_acqfn = ah_send_acquire;
424 	sadbp_init("AH", &ahstack->ah_sadb, SADB_SATYPE_AH, ah_hash_size,
425 	    ahstack->ipsecah_netstack);
426 
427 	mutex_init(&ahstack->ipsecah_param_lock, NULL, MUTEX_DEFAULT, 0);
428 
429 	ip_drop_register(&ahstack->ah_dropper, "IPsec AH");
430 	return (ahstack);
431 }
432 
433 /*
434  * Destroy things for AH at module unload time.
435  */
436 void
437 ipsecah_ddi_destroy(void)
438 {
439 	netstack_unregister(NS_IPSECAH);
440 	taskq_destroy(ah_taskq);
441 }
442 
443 /*
444  * Destroy things for AH for one stack... Never called?
445  */
446 static void
447 ipsecah_stack_fini(netstackid_t stackid, void *arg)
448 {
449 	ipsecah_stack_t *ahstack = (ipsecah_stack_t *)arg;
450 
451 	if (ahstack->ah_pfkey_q != NULL) {
452 		(void) quntimeout(ahstack->ah_pfkey_q, ahstack->ah_event);
453 	}
454 	ahstack->ah_sadb.s_acqfn = NULL;
455 	ahstack->ah_sadb.s_acquire_timeout = NULL;
456 	sadbp_destroy(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
457 	ip_drop_unregister(&ahstack->ah_dropper);
458 	mutex_destroy(&ahstack->ipsecah_param_lock);
459 	nd_free(&ahstack->ipsecah_g_nd);
460 
461 	kmem_free(ahstack->ipsecah_params, sizeof (lcl_param_arr));
462 	ahstack->ipsecah_params = NULL;
463 	kstat_delete_netstack(ahstack->ah_ksp, stackid);
464 	ahstack->ah_ksp = NULL;
465 	ahstack->ah_kstats = NULL;
466 
467 	kmem_free(ahstack, sizeof (*ahstack));
468 }
469 
470 /*
471  * AH module open routine. The module should be opened by keysock.
472  */
473 /* ARGSUSED */
474 static int
475 ipsecah_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
476 {
477 	netstack_t	*ns;
478 	ipsecah_stack_t	*ahstack;
479 
480 	if (secpolicy_ip_config(credp, B_FALSE) != 0) {
481 		ah0dbg(("Non-privileged user trying to open ipsecah.\n"));
482 		return (EPERM);
483 	}
484 
485 	if (q->q_ptr != NULL)
486 		return (0);  /* Re-open of an already open instance. */
487 
488 	if (sflag != MODOPEN)
489 		return (EINVAL);
490 
491 	ns = netstack_find_by_cred(credp);
492 	ASSERT(ns != NULL);
493 	ahstack = ns->netstack_ipsecah;
494 	ASSERT(ahstack != NULL);
495 
496 	/*
497 	 * ASSUMPTIONS (because I'm MT_OCEXCL):
498 	 *
499 	 *	* I'm being pushed on top of IP for all my opens (incl. #1).
500 	 *	* Only ipsecah_open() can write into ah_sadb.s_ip_q.
501 	 *	* Because of this, I can check lazily for ah_sadb.s_ip_q.
502 	 *
503 	 *  If these assumptions are wrong, I'm in BIG trouble...
504 	 */
505 
506 	q->q_ptr = ahstack;
507 	WR(q)->q_ptr = q->q_ptr;
508 
509 	if (ahstack->ah_sadb.s_ip_q == NULL) {
510 		struct T_unbind_req *tur;
511 
512 		ahstack->ah_sadb.s_ip_q = WR(q);
513 		/* Allocate an unbind... */
514 		ahstack->ah_ip_unbind = allocb(sizeof (struct T_unbind_req),
515 		    BPRI_HI);
516 
517 		/*
518 		 * Send down T_BIND_REQ to bind IPPROTO_AH.
519 		 * Handle the ACK here in AH.
520 		 */
521 		qprocson(q);
522 		if (ahstack->ah_ip_unbind == NULL ||
523 		    !sadb_t_bind_req(ahstack->ah_sadb.s_ip_q, IPPROTO_AH)) {
524 			if (ahstack->ah_ip_unbind != NULL) {
525 				freeb(ahstack->ah_ip_unbind);
526 				ahstack->ah_ip_unbind = NULL;
527 			}
528 			q->q_ptr = NULL;
529 			qprocsoff(q);
530 			netstack_rele(ahstack->ipsecah_netstack);
531 			return (ENOMEM);
532 		}
533 
534 		ahstack->ah_ip_unbind->b_datap->db_type = M_PROTO;
535 		tur = (struct T_unbind_req *)ahstack->ah_ip_unbind->b_rptr;
536 		tur->PRIM_type = T_UNBIND_REQ;
537 	} else {
538 		qprocson(q);
539 	}
540 
541 	/*
542 	 * For now, there's not much I can do.  I'll be getting a message
543 	 * passed down to me from keysock (in my wput), and a T_BIND_ACK
544 	 * up from IP (in my rput).
545 	 */
546 
547 	return (0);
548 }
549 
550 /*
551  * AH module close routine.
552  */
553 static int
554 ipsecah_close(queue_t *q)
555 {
556 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
557 
558 	/*
559 	 * If ah_sadb.s_ip_q is attached to this instance, send a
560 	 * T_UNBIND_REQ to IP for the instance before doing
561 	 * a qprocsoff().
562 	 */
563 	if (WR(q) == ahstack->ah_sadb.s_ip_q &&
564 	    ahstack->ah_ip_unbind != NULL) {
565 		putnext(WR(q), ahstack->ah_ip_unbind);
566 		ahstack->ah_ip_unbind = NULL;
567 	}
568 
569 	/*
570 	 * Clean up q_ptr, if needed.
571 	 */
572 	qprocsoff(q);
573 
574 	/* Keysock queue check is safe, because of OCEXCL perimeter. */
575 
576 	if (q == ahstack->ah_pfkey_q) {
577 		ah1dbg(ahstack,
578 		    ("ipsecah_close:  Ummm... keysock is closing AH.\n"));
579 		ahstack->ah_pfkey_q = NULL;
580 		/* Detach qtimeouts. */
581 		(void) quntimeout(q, ahstack->ah_event);
582 	}
583 
584 	if (WR(q) == ahstack->ah_sadb.s_ip_q) {
585 		/*
586 		 * If the ah_sadb.s_ip_q is attached to this instance, find
587 		 * another.  The OCEXCL outer perimeter helps us here.
588 		 */
589 
590 		ahstack->ah_sadb.s_ip_q = NULL;
591 
592 		/*
593 		 * Find a replacement queue for ah_sadb.s_ip_q.
594 		 */
595 		if (ahstack->ah_pfkey_q != NULL &&
596 		    ahstack->ah_pfkey_q != RD(q)) {
597 			/*
598 			 * See if we can use the pfkey_q.
599 			 */
600 			ahstack->ah_sadb.s_ip_q = WR(ahstack->ah_pfkey_q);
601 		}
602 
603 		if (ahstack->ah_sadb.s_ip_q == NULL ||
604 		    !sadb_t_bind_req(ahstack->ah_sadb.s_ip_q, IPPROTO_AH)) {
605 			ah1dbg(ahstack,
606 			    ("ipsecah: Can't reassign ah_sadb.s_ip_q.\n"));
607 			ahstack->ah_sadb.s_ip_q = NULL;
608 		} else {
609 			ahstack->ah_ip_unbind =
610 			    allocb(sizeof (struct T_unbind_req), BPRI_HI);
611 
612 			if (ahstack->ah_ip_unbind != NULL) {
613 				struct T_unbind_req *tur;
614 
615 				ahstack->ah_ip_unbind->b_datap->db_type =
616 				    M_PROTO;
617 				tur = (struct T_unbind_req *)
618 				    ahstack->ah_ip_unbind->b_rptr;
619 				tur->PRIM_type = T_UNBIND_REQ;
620 			}
621 			/* If it's NULL, I can't do much here. */
622 		}
623 	}
624 
625 	netstack_rele(ahstack->ipsecah_netstack);
626 	return (0);
627 }
628 
629 /*
630  * AH module read put routine.
631  */
632 /* ARGSUSED */
633 static void
634 ipsecah_rput(queue_t *q, mblk_t *mp)
635 {
636 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
637 
638 	ASSERT(mp->b_datap->db_type != M_CTL);	/* No more IRE_DB_REQ. */
639 
640 	switch (mp->b_datap->db_type) {
641 	case M_PROTO:
642 	case M_PCPROTO:
643 		/* TPI message of some sort. */
644 		switch (*((t_scalar_t *)mp->b_rptr)) {
645 		case T_BIND_ACK:
646 			/* We expect this. */
647 			ah3dbg(ahstack,
648 			    ("Thank you IP from AH for T_BIND_ACK\n"));
649 			break;
650 		case T_ERROR_ACK:
651 			cmn_err(CE_WARN,
652 			    "ipsecah:  AH received T_ERROR_ACK from IP.");
653 			break;
654 		case T_OK_ACK:
655 			/* Probably from a (rarely sent) T_UNBIND_REQ. */
656 			break;
657 		default:
658 			ah1dbg(ahstack, ("Unknown M_{,PC}PROTO message.\n"));
659 		}
660 		freemsg(mp);
661 		break;
662 	default:
663 		/* For now, passthru message. */
664 		ah2dbg(ahstack, ("AH got unknown mblk type %d.\n",
665 		    mp->b_datap->db_type));
666 		putnext(q, mp);
667 	}
668 }
669 
670 /*
671  * Construct an SADB_REGISTER message with the current algorithms.
672  */
673 static boolean_t
674 ah_register_out(uint32_t sequence, uint32_t pid, uint_t serial,
675     ipsecah_stack_t *ahstack)
676 {
677 	mblk_t *mp;
678 	boolean_t rc = B_TRUE;
679 	sadb_msg_t *samsg;
680 	sadb_supported_t *sasupp;
681 	sadb_alg_t *saalg;
682 	uint_t allocsize = sizeof (*samsg);
683 	uint_t i, numalgs_snap;
684 	ipsec_alginfo_t **authalgs;
685 	uint_t num_aalgs;
686 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
687 
688 	/* Allocate the KEYSOCK_OUT. */
689 	mp = sadb_keysock_out(serial);
690 	if (mp == NULL) {
691 		ah0dbg(("ah_register_out: couldn't allocate mblk.\n"));
692 		return (B_FALSE);
693 	}
694 
695 	/*
696 	 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
697 	 * The alg reader lock needs to be held while allocating
698 	 * the variable part (i.e. the algorithms) of the message.
699 	 */
700 
701 	mutex_enter(&ipss->ipsec_alg_lock);
702 
703 	/*
704 	 * Return only valid algorithms, so the number of algorithms
705 	 * to send up may be less than the number of algorithm entries
706 	 * in the table.
707 	 */
708 	authalgs = ipss->ipsec_alglists[IPSEC_ALG_AUTH];
709 	for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
710 		if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
711 			num_aalgs++;
712 
713 	/*
714 	 * Fill SADB_REGISTER message's algorithm descriptors.  Hold
715 	 * down the lock while filling it.
716 	 */
717 	if (num_aalgs != 0) {
718 		allocsize += (num_aalgs * sizeof (*saalg));
719 		allocsize += sizeof (*sasupp);
720 	}
721 	mp->b_cont = allocb(allocsize, BPRI_HI);
722 	if (mp->b_cont == NULL) {
723 		mutex_exit(&ipss->ipsec_alg_lock);
724 		freemsg(mp);
725 		return (B_FALSE);
726 	}
727 
728 	mp->b_cont->b_wptr += allocsize;
729 	if (num_aalgs != 0) {
730 
731 		saalg = (sadb_alg_t *)(mp->b_cont->b_rptr + sizeof (*samsg) +
732 		    sizeof (*sasupp));
733 		ASSERT(((ulong_t)saalg & 0x7) == 0);
734 
735 		numalgs_snap = 0;
736 		for (i = 0;
737 		    ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs));
738 		    i++) {
739 			if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
740 				continue;
741 
742 			saalg->sadb_alg_id = authalgs[i]->alg_id;
743 			saalg->sadb_alg_ivlen = 0;
744 			saalg->sadb_alg_minbits = authalgs[i]->alg_ef_minbits;
745 			saalg->sadb_alg_maxbits = authalgs[i]->alg_ef_maxbits;
746 			saalg->sadb_x_alg_increment =
747 			    authalgs[i]->alg_increment;
748 			saalg->sadb_x_alg_defincr =
749 			    authalgs[i]->alg_ef_default;
750 			numalgs_snap++;
751 			saalg++;
752 		}
753 		ASSERT(numalgs_snap == num_aalgs);
754 #ifdef DEBUG
755 		/*
756 		 * Reality check to make sure I snagged all of the
757 		 * algorithms.
758 		 */
759 		for (; i < IPSEC_MAX_ALGS; i++)
760 			if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
761 				cmn_err(CE_PANIC,
762 				    "ah_register_out()!  Missed #%d.\n", i);
763 #endif /* DEBUG */
764 	}
765 
766 	mutex_exit(&ipss->ipsec_alg_lock);
767 
768 	/* Now fill the restof the SADB_REGISTER message. */
769 
770 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
771 	samsg->sadb_msg_version = PF_KEY_V2;
772 	samsg->sadb_msg_type = SADB_REGISTER;
773 	samsg->sadb_msg_errno = 0;
774 	samsg->sadb_msg_satype = SADB_SATYPE_AH;
775 	samsg->sadb_msg_len = SADB_8TO64(allocsize);
776 	samsg->sadb_msg_reserved = 0;
777 	/*
778 	 * Assume caller has sufficient sequence/pid number info.  If it's one
779 	 * from me over a new alg., I could give two hoots about sequence.
780 	 */
781 	samsg->sadb_msg_seq = sequence;
782 	samsg->sadb_msg_pid = pid;
783 
784 	if (allocsize > sizeof (*samsg)) {
785 		sasupp = (sadb_supported_t *)(samsg + 1);
786 		sasupp->sadb_supported_len =
787 		    SADB_8TO64(allocsize - sizeof (sadb_msg_t));
788 		sasupp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
789 		sasupp->sadb_supported_reserved = 0;
790 	}
791 
792 	if (ahstack->ah_pfkey_q != NULL)
793 		putnext(ahstack->ah_pfkey_q, mp);
794 	else {
795 		rc = B_FALSE;
796 		freemsg(mp);
797 	}
798 
799 	return (rc);
800 }
801 
802 /*
803  * Invoked when the algorithm table changes. Causes SADB_REGISTER
804  * messages continaining the current list of algorithms to be
805  * sent up to the AH listeners.
806  */
807 void
808 ipsecah_algs_changed(netstack_t *ns)
809 {
810 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
811 
812 	/*
813 	 * Time to send a PF_KEY SADB_REGISTER message to AH listeners
814 	 * everywhere.  (The function itself checks for NULL ah_pfkey_q.)
815 	 */
816 	(void) ah_register_out(0, 0, 0, ahstack);
817 }
818 
819 /*
820  * Stub function that taskq_dispatch() invokes to take the mblk (in arg)
821  * and put() it into AH and STREAMS again.
822  */
823 static void
824 inbound_task(void *arg)
825 {
826 	ah_t *ah;
827 	mblk_t *mp = (mblk_t *)arg;
828 	ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr;
829 	int ipsec_rc;
830 	netstack_t	*ns = ii->ipsec_in_ns;
831 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
832 
833 	ah2dbg(ahstack, ("in AH inbound_task"));
834 
835 	ASSERT(ahstack != NULL);
836 	ah = ipsec_inbound_ah_sa(mp, ns);
837 	if (ah == NULL)
838 		return;
839 	ASSERT(ii->ipsec_in_ah_sa != NULL);
840 	ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func(mp, ah);
841 	if (ipsec_rc != IPSEC_STATUS_SUCCESS)
842 		return;
843 	ip_fanout_proto_again(mp, NULL, NULL, NULL);
844 }
845 
846 
847 /*
848  * Now that weak-key passed, actually ADD the security association, and
849  * send back a reply ADD message.
850  */
851 static int
852 ah_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi,
853     int *diagnostic, ipsecah_stack_t *ahstack)
854 {
855 	isaf_t *primary, *secondary, *inbound, *outbound;
856 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
857 	sadb_address_t *dstext =
858 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
859 	struct sockaddr_in *dst;
860 	struct sockaddr_in6 *dst6;
861 	boolean_t is_ipv4, clone = B_FALSE, is_inbound = B_FALSE;
862 	uint32_t *dstaddr;
863 	ipsa_t *larval;
864 	ipsacq_t *acqrec;
865 	iacqf_t *acq_bucket;
866 	mblk_t *acq_msgs = NULL;
867 	mblk_t *lpkt;
868 	int rc;
869 	sadb_t *sp;
870 	int outhash;
871 	netstack_t	*ns = ahstack->ipsecah_netstack;
872 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
873 
874 	/*
875 	 * Locate the appropriate table(s).
876 	 */
877 
878 	dst = (struct sockaddr_in *)(dstext + 1);
879 	dst6 = (struct sockaddr_in6 *)dst;
880 	is_ipv4 = (dst->sin_family == AF_INET);
881 	if (is_ipv4) {
882 		sp = &ahstack->ah_sadb.s_v4;
883 		dstaddr = (uint32_t *)(&dst->sin_addr);
884 		outhash = OUTBOUND_HASH_V4(sp, *(ipaddr_t *)dstaddr);
885 	} else {
886 		ASSERT(dst->sin_family == AF_INET6);
887 		sp = &ahstack->ah_sadb.s_v6;
888 		dstaddr = (uint32_t *)(&dst6->sin6_addr);
889 		outhash = OUTBOUND_HASH_V6(sp, *(in6_addr_t *)dstaddr);
890 	}
891 
892 	inbound = INBOUND_BUCKET(sp, assoc->sadb_sa_spi);
893 	outbound = &sp->sdb_of[outhash];
894 
895 	switch (ksi->ks_in_dsttype) {
896 	case KS_IN_ADDR_MBCAST:
897 		clone = B_TRUE;	/* All mcast SAs can be bidirectional */
898 		/* FALLTHRU */
899 	case KS_IN_ADDR_ME:
900 		primary = inbound;
901 		secondary = outbound;
902 		/*
903 		 * If the source address is either one of mine, or unspecified
904 		 * (which is best summed up by saying "not 'not mine'"),
905 		 * then the association is potentially bi-directional,
906 		 * in that it can be used for inbound traffic and outbound
907 		 * traffic.  The best example of such and SA is a multicast
908 		 * SA (which allows me to receive the outbound traffic).
909 		 */
910 		if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
911 			clone = B_TRUE;
912 		is_inbound = B_TRUE;
913 		break;
914 	case KS_IN_ADDR_NOTME:
915 		primary = outbound;
916 		secondary = inbound;
917 		/*
918 		 * If the source address literally not mine (either
919 		 * unspecified or not mine), then this SA may have an
920 		 * address that WILL be mine after some configuration.
921 		 * We pay the price for this by making it a bi-directional
922 		 * SA.
923 		 */
924 		if (ksi->ks_in_srctype != KS_IN_ADDR_ME)
925 			clone = B_TRUE;
926 		break;
927 	default:
928 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
929 		return (EINVAL);
930 	}
931 
932 	/*
933 	 * Find a ACQUIRE list entry if possible.  If we've added an SA that
934 	 * suits the needs of an ACQUIRE list entry, we can eliminate the
935 	 * ACQUIRE list entry and transmit the enqueued packets.  Use the
936 	 * high-bit of the sequence number to queue it.  Key off destination
937 	 * addr, and change acqrec's state.
938 	 */
939 
940 	if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
941 		acq_bucket = &sp->sdb_acq[outhash];
942 		mutex_enter(&acq_bucket->iacqf_lock);
943 		for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
944 		    acqrec = acqrec->ipsacq_next) {
945 			mutex_enter(&acqrec->ipsacq_lock);
946 			/*
947 			 * Q:  I only check sequence.  Should I check dst?
948 			 * A: Yes, check dest because those are the packets
949 			 *    that are queued up.
950 			 */
951 			if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
952 			    IPSA_ARE_ADDR_EQUAL(dstaddr,
953 				acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
954 				break;
955 			mutex_exit(&acqrec->ipsacq_lock);
956 		}
957 		if (acqrec != NULL) {
958 			/*
959 			 * AHA!  I found an ACQUIRE record for this SA.
960 			 * Grab the msg list, and free the acquire record.
961 			 * I already am holding the lock for this record,
962 			 * so all I have to do is free it.
963 			 */
964 			acq_msgs = acqrec->ipsacq_mp;
965 			acqrec->ipsacq_mp = NULL;
966 			mutex_exit(&acqrec->ipsacq_lock);
967 			sadb_destroy_acquire(acqrec, ns);
968 		}
969 		mutex_exit(&acq_bucket->iacqf_lock);
970 	}
971 
972 	/*
973 	 * Find PF_KEY message, and see if I'm an update.  If so, find entry
974 	 * in larval list (if there).
975 	 */
976 
977 	larval = NULL;
978 
979 	if (samsg->sadb_msg_type == SADB_UPDATE) {
980 		mutex_enter(&inbound->isaf_lock);
981 		larval = ipsec_getassocbyspi(inbound, assoc->sadb_sa_spi,
982 		    ALL_ZEROES_PTR, dstaddr, dst->sin_family);
983 		mutex_exit(&inbound->isaf_lock);
984 
985 		if ((larval == NULL) ||
986 		    (larval->ipsa_state != IPSA_STATE_LARVAL)) {
987 			ah0dbg(("Larval update, but larval disappeared.\n"));
988 			return (ESRCH);
989 		} /* Else sadb_common_add unlinks it for me! */
990 	}
991 
992 	lpkt = NULL;
993 	if (larval != NULL)
994 		lpkt = sadb_clear_lpkt(larval);
995 
996 	rc = sadb_common_add(ahstack->ah_sadb.s_ip_q, ahstack->ah_pfkey_q, mp,
997 	    samsg, ksi, primary, secondary, larval, clone, is_inbound,
998 	    diagnostic, ns);
999 
1000 	/*
1001 	 * How much more stack will I create with all of these
1002 	 * ah_inbound_* and ah_outbound_*() calls?
1003 	 */
1004 
1005 
1006 	if (rc == 0 && lpkt != NULL)
1007 		rc = !taskq_dispatch(ah_taskq, inbound_task,
1008 			    (void *) lpkt, TQ_NOSLEEP);
1009 
1010 	if (rc != 0) {
1011 		ip_drop_packet(lpkt, B_TRUE, NULL, NULL,
1012 		    DROPPER(ipss, ipds_sadb_inlarval_timeout),
1013 		    &ahstack->ah_dropper);
1014 	}
1015 
1016 	while (acq_msgs != NULL) {
1017 		mblk_t *mp = acq_msgs;
1018 
1019 		acq_msgs = acq_msgs->b_next;
1020 		mp->b_next = NULL;
1021 		if (rc == 0) {
1022 			ipsec_out_t *io = (ipsec_out_t *)mp->b_rptr;
1023 
1024 			ASSERT(ahstack->ah_sadb.s_ip_q != NULL);
1025 			if (ipsec_outbound_sa(mp, IPPROTO_AH)) {
1026 				io->ipsec_out_ah_done = B_TRUE;
1027 				if (ah_outbound(mp) == IPSEC_STATUS_SUCCESS) {
1028 					ipha_t *ipha = (ipha_t *)
1029 					    mp->b_cont->b_rptr;
1030 					if (is_ipv4) {
1031 						ip_wput_ipsec_out(NULL, mp,
1032 						    ipha, NULL, NULL);
1033 					} else {
1034 						ip6_t *ip6h = (ip6_t *)ipha;
1035 						ip_wput_ipsec_out_v6(NULL,
1036 						    mp, ip6h, NULL, NULL);
1037 					}
1038 				}
1039 				continue;
1040 			}
1041 		}
1042 		AH_BUMP_STAT(ahstack, out_discards);
1043 		ip_drop_packet(mp, B_FALSE, NULL, NULL,
1044 		    DROPPER(ipss, ipds_sadb_acquire_timeout),
1045 		    &ahstack->ah_dropper);
1046 	}
1047 
1048 	return (rc);
1049 }
1050 
1051 /*
1052  * Add new AH security association.  This may become a generic AH/ESP
1053  * routine eventually.
1054  */
1055 static int
1056 ah_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, netstack_t *ns)
1057 {
1058 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1059 	sadb_address_t *srcext =
1060 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
1061 	sadb_address_t *dstext =
1062 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1063 	sadb_address_t *isrcext =
1064 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
1065 	sadb_address_t *idstext =
1066 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
1067 	sadb_key_t *key = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
1068 	struct sockaddr_in *src, *dst;
1069 	/* We don't need sockaddr_in6 for now. */
1070 	sadb_lifetime_t *soft =
1071 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
1072 	sadb_lifetime_t *hard =
1073 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
1074 	ipsec_alginfo_t *aalg;
1075 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
1076 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
1077 
1078 	/* I need certain extensions present for an ADD message. */
1079 	if (srcext == NULL) {
1080 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
1081 		return (EINVAL);
1082 	}
1083 	if (dstext == NULL) {
1084 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
1085 		return (EINVAL);
1086 	}
1087 	if (isrcext == NULL && idstext != NULL) {
1088 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
1089 		return (EINVAL);
1090 	}
1091 	if (isrcext != NULL && idstext == NULL) {
1092 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
1093 		return (EINVAL);
1094 	}
1095 	if (assoc == NULL) {
1096 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1097 		return (EINVAL);
1098 	}
1099 	if (key == NULL) {
1100 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_AKEY;
1101 		return (EINVAL);
1102 	}
1103 
1104 	src = (struct sockaddr_in *)(srcext + 1);
1105 	dst = (struct sockaddr_in *)(dstext + 1);
1106 
1107 	/* Sundry ADD-specific reality checks. */
1108 	/* XXX STATS : Logging/stats here? */
1109 
1110 	if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
1111 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
1112 		return (EINVAL);
1113 	}
1114 	if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
1115 		*diagnostic = SADB_X_DIAGNOSTIC_ENCR_NOTSUPP;
1116 		return (EINVAL);
1117 	}
1118 	if (assoc->sadb_sa_flags & ~(SADB_SAFLAGS_NOREPLAY |
1119 		SADB_X_SAFLAGS_TUNNEL)) {
1120 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
1121 		return (EINVAL);
1122 	}
1123 
1124 	if ((*diagnostic = sadb_hardsoftchk(hard, soft)) != 0)
1125 		return (EINVAL);
1126 
1127 	ASSERT(src->sin_family == dst->sin_family);
1128 
1129 	/* Stuff I don't support, for now.  XXX Diagnostic? */
1130 	if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL ||
1131 	    ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL)
1132 		return (EOPNOTSUPP);
1133 
1134 	/*
1135 	 * XXX Policy : I'm not checking identities or sensitivity
1136 	 * labels at this time, but if I did, I'd do them here, before I sent
1137 	 * the weak key check up to the algorithm.
1138 	 */
1139 
1140 	/* verify that there is a mapping for the specified algorithm */
1141 	mutex_enter(&ipss->ipsec_alg_lock);
1142 	aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH][assoc->sadb_sa_auth];
1143 	if (aalg == NULL || !ALG_VALID(aalg)) {
1144 		mutex_exit(&ipss->ipsec_alg_lock);
1145 		ah1dbg(ahstack, ("Couldn't find auth alg #%d.\n",
1146 			assoc->sadb_sa_auth));
1147 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
1148 		return (EINVAL);
1149 	}
1150 	ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
1151 
1152 	/* sanity check key sizes */
1153 	if (!ipsec_valid_key_size(key->sadb_key_bits, aalg)) {
1154 		mutex_exit(&ipss->ipsec_alg_lock);
1155 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
1156 		return (EINVAL);
1157 	}
1158 
1159 	/* check key and fix parity if needed */
1160 	if (ipsec_check_key(aalg->alg_mech_type, key, B_TRUE,
1161 	    diagnostic) != 0) {
1162 		mutex_exit(&ipss->ipsec_alg_lock);
1163 		return (EINVAL);
1164 	}
1165 
1166 	mutex_exit(&ipss->ipsec_alg_lock);
1167 
1168 	return (ah_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
1169 		    diagnostic, ahstack));
1170 }
1171 
1172 /*
1173  * Update a security association.  Updates come in two varieties.  The first
1174  * is an update of lifetimes on a non-larval SA.  The second is an update of
1175  * a larval SA, which ends up looking a lot more like an add.
1176  */
1177 static int
1178 ah_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
1179     ipsecah_stack_t *ahstack)
1180 {
1181 	sadb_address_t *dstext =
1182 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1183 	struct sockaddr_in *sin;
1184 
1185 	if (dstext == NULL) {
1186 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
1187 		return (EINVAL);
1188 	}
1189 	sin = (struct sockaddr_in *)(dstext + 1);
1190 	return (sadb_update_sa(mp, ksi,
1191 		(sin->sin_family == AF_INET6) ? &ahstack->ah_sadb.s_v6 :
1192 		&ahstack->ah_sadb.s_v4,
1193 		diagnostic, ahstack->ah_pfkey_q, ah_add_sa,
1194 		ahstack->ipsecah_netstack));
1195 }
1196 
1197 /*
1198  * Delete a security association.  This is REALLY likely to be code common to
1199  * both AH and ESP.  Find the association, then unlink it.
1200  */
1201 static int
1202 ah_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
1203     ipsecah_stack_t *ahstack)
1204 {
1205 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1206 	sadb_address_t *dstext =
1207 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1208 	sadb_address_t *srcext =
1209 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
1210 	struct sockaddr_in *sin;
1211 
1212 	if (assoc == NULL) {
1213 		if (dstext != NULL)
1214 			sin = (struct sockaddr_in *)(dstext + 1);
1215 		else if (srcext != NULL)
1216 			sin = (struct sockaddr_in *)(srcext + 1);
1217 		else {
1218 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1219 			return (EINVAL);
1220 		}
1221 		return (sadb_purge_sa(mp, ksi,
1222 		    (sin->sin_family == AF_INET6) ? &ahstack->ah_sadb.s_v6 :
1223 		    &ahstack->ah_sadb.s_v4,
1224 		    ahstack->ah_pfkey_q, ahstack->ah_sadb.s_ip_q));
1225 	}
1226 
1227 	return (sadb_del_sa(mp, ksi, &ahstack->ah_sadb, diagnostic,
1228 		    ahstack->ah_pfkey_q));
1229 }
1230 
1231 /*
1232  * Convert the entire contents of all of AH's SA tables into PF_KEY SADB_DUMP
1233  * messages.
1234  */
1235 static void
1236 ah_dump(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1237 {
1238 	int error;
1239 	sadb_msg_t *samsg;
1240 
1241 	/*
1242 	 * Dump each fanout, bailing if error is non-zero.
1243 	 */
1244 
1245 	error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi->ks_in_serial,
1246 	    &ahstack->ah_sadb.s_v4);
1247 	if (error != 0)
1248 		goto bail;
1249 
1250 	error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi->ks_in_serial,
1251 	    &ahstack->ah_sadb.s_v6);
1252 bail:
1253 	ASSERT(mp->b_cont != NULL);
1254 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
1255 	samsg->sadb_msg_errno = (uint8_t)error;
1256 	sadb_pfkey_echo(ahstack->ah_pfkey_q, mp,
1257 	    (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL);
1258 }
1259 
1260 /*
1261  * First-cut reality check for an inbound PF_KEY message.
1262  */
1263 static boolean_t
1264 ah_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
1265     ipsecah_stack_t *ahstack)
1266 {
1267 	int diagnostic;
1268 
1269 	if (mp->b_cont == NULL) {
1270 		freemsg(mp);
1271 		return (B_TRUE);
1272 	}
1273 
1274 	if (ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL) {
1275 		diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT;
1276 		goto badmsg;
1277 	}
1278 	if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
1279 		diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
1280 		goto badmsg;
1281 	}
1282 	if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
1283 	    ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
1284 		diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
1285 		goto badmsg;
1286 	}
1287 	return (B_FALSE);	/* False ==> no failures */
1288 
1289 badmsg:
1290 	sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1291 	    diagnostic, ksi->ks_in_serial);
1292 	return (B_TRUE);	/* True ==> failures */
1293 }
1294 
1295 /*
1296  * AH parsing of PF_KEY messages.  Keysock did most of the really silly
1297  * error cases.  What I receive is a fully-formed, syntactically legal
1298  * PF_KEY message.  I then need to check semantics...
1299  *
1300  * This code may become common to AH and ESP.  Stay tuned.
1301  *
1302  * I also make the assumption that db_ref's are cool.  If this assumption
1303  * is wrong, this means that someone other than keysock or me has been
1304  * mucking with PF_KEY messages.
1305  */
1306 static void
1307 ah_parse_pfkey(mblk_t *mp, ipsecah_stack_t *ahstack)
1308 {
1309 	mblk_t *msg = mp->b_cont;
1310 	sadb_msg_t *samsg;
1311 	keysock_in_t *ksi;
1312 	int error;
1313 	int diagnostic = SADB_X_DIAGNOSTIC_NONE;
1314 
1315 	ASSERT(msg != NULL);
1316 
1317 	samsg = (sadb_msg_t *)msg->b_rptr;
1318 	ksi = (keysock_in_t *)mp->b_rptr;
1319 
1320 	/*
1321 	 * If applicable, convert unspecified AF_INET6 to unspecified
1322 	 * AF_INET.
1323 	 */
1324 	if (!sadb_addrfix(ksi, ahstack->ah_pfkey_q, mp,
1325 	    ahstack->ipsecah_netstack) ||
1326 	    ah_pfkey_reality_failures(mp, ksi, ahstack)) {
1327 		return;
1328 	}
1329 
1330 	switch (samsg->sadb_msg_type) {
1331 	case SADB_ADD:
1332 		error = ah_add_sa(mp, ksi, &diagnostic,
1333 		    ahstack->ipsecah_netstack);
1334 		if (error != 0) {
1335 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1336 			    diagnostic, ksi->ks_in_serial);
1337 		}
1338 		/* else ah_add_sa() took care of things. */
1339 		break;
1340 	case SADB_DELETE:
1341 		error = ah_del_sa(mp, ksi, &diagnostic, ahstack);
1342 		if (error != 0) {
1343 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1344 			    diagnostic, ksi->ks_in_serial);
1345 		}
1346 		/* Else ah_del_sa() took care of things. */
1347 		break;
1348 	case SADB_GET:
1349 		error = sadb_get_sa(mp, ksi, &ahstack->ah_sadb, &diagnostic,
1350 		    ahstack->ah_pfkey_q);
1351 		if (error != 0) {
1352 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1353 			    diagnostic, ksi->ks_in_serial);
1354 		}
1355 		/* Else sadb_get_sa() took care of things. */
1356 		break;
1357 	case SADB_FLUSH:
1358 		sadbp_flush(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
1359 		sadb_pfkey_echo(ahstack->ah_pfkey_q, mp, samsg, ksi, NULL);
1360 		break;
1361 	case SADB_REGISTER:
1362 		/*
1363 		 * Hmmm, let's do it!  Check for extensions (there should
1364 		 * be none), extract the fields, call ah_register_out(),
1365 		 * then either free or report an error.
1366 		 *
1367 		 * Keysock takes care of the PF_KEY bookkeeping for this.
1368 		 */
1369 		if (ah_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
1370 		    ksi->ks_in_serial, ahstack)) {
1371 			freemsg(mp);
1372 		} else {
1373 			/*
1374 			 * Only way this path hits is if there is a memory
1375 			 * failure.  It will not return B_FALSE because of
1376 			 * lack of ah_pfkey_q if I am in wput().
1377 			 */
1378 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM,
1379 			    diagnostic, ksi->ks_in_serial);
1380 		}
1381 		break;
1382 	case SADB_UPDATE:
1383 		/*
1384 		 * Find a larval, if not there, find a full one and get
1385 		 * strict.
1386 		 */
1387 		error = ah_update_sa(mp, ksi, &diagnostic, ahstack);
1388 		if (error != 0) {
1389 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1390 			    diagnostic, ksi->ks_in_serial);
1391 		}
1392 		/* else ah_update_sa() took care of things. */
1393 		break;
1394 	case SADB_GETSPI:
1395 		/*
1396 		 * Reserve a new larval entry.
1397 		 */
1398 		ah_getspi(mp, ksi, ahstack);
1399 		break;
1400 	case SADB_ACQUIRE:
1401 		/*
1402 		 * Find larval and/or ACQUIRE record and kill it (them), I'm
1403 		 * most likely an error.  Inbound ACQUIRE messages should only
1404 		 * have the base header.
1405 		 */
1406 		sadb_in_acquire(samsg, &ahstack->ah_sadb, ahstack->ah_pfkey_q,
1407 		    ahstack->ipsecah_netstack);
1408 		freemsg(mp);
1409 		break;
1410 	case SADB_DUMP:
1411 		/*
1412 		 * Dump all entries.
1413 		 */
1414 		ah_dump(mp, ksi, ahstack);
1415 		/* ah_dump will take care of the return message, etc. */
1416 		break;
1417 	case SADB_EXPIRE:
1418 		/* Should never reach me. */
1419 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EOPNOTSUPP,
1420 		    diagnostic, ksi->ks_in_serial);
1421 		break;
1422 	default:
1423 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1424 		    SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
1425 		break;
1426 	}
1427 }
1428 
1429 /*
1430  * Handle case where PF_KEY says it can't find a keysock for one of my
1431  * ACQUIRE messages.
1432  */
1433 static void
1434 ah_keysock_no_socket(mblk_t *mp, ipsecah_stack_t *ahstack)
1435 {
1436 	sadb_msg_t *samsg;
1437 	keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
1438 
1439 	if (mp->b_cont == NULL) {
1440 		freemsg(mp);
1441 		return;
1442 	}
1443 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
1444 
1445 	/*
1446 	 * If keysock can't find any registered, delete the acquire record
1447 	 * immediately, and handle errors.
1448 	 */
1449 	if (samsg->sadb_msg_type == SADB_ACQUIRE) {
1450 		samsg->sadb_msg_errno = kse->ks_err_errno;
1451 		samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
1452 		/*
1453 		 * Use the write-side of the ah_pfkey_q, in case there is
1454 		 * no ahstack->ah_sadb.s_ip_q.
1455 		 */
1456 		sadb_in_acquire(samsg, &ahstack->ah_sadb,
1457 		    WR(ahstack->ah_pfkey_q), ahstack->ipsecah_netstack);
1458 	}
1459 
1460 	freemsg(mp);
1461 }
1462 
1463 /*
1464  * AH module write put routine.
1465  */
1466 static void
1467 ipsecah_wput(queue_t *q, mblk_t *mp)
1468 {
1469 	ipsec_info_t *ii;
1470 	struct iocblk *iocp;
1471 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
1472 
1473 	ah3dbg(ahstack, ("In ah_wput().\n"));
1474 
1475 	/* NOTE:  Each case must take care of freeing or passing mp. */
1476 	switch (mp->b_datap->db_type) {
1477 	case M_CTL:
1478 		if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
1479 			/* Not big enough message. */
1480 			freemsg(mp);
1481 			break;
1482 		}
1483 		ii = (ipsec_info_t *)mp->b_rptr;
1484 
1485 		switch (ii->ipsec_info_type) {
1486 		case KEYSOCK_OUT_ERR:
1487 			ah1dbg(ahstack, ("Got KEYSOCK_OUT_ERR message.\n"));
1488 			ah_keysock_no_socket(mp, ahstack);
1489 			break;
1490 		case KEYSOCK_IN:
1491 			AH_BUMP_STAT(ahstack, keysock_in);
1492 			ah3dbg(ahstack, ("Got KEYSOCK_IN message.\n"));
1493 
1494 			/* Parse the message. */
1495 			ah_parse_pfkey(mp, ahstack);
1496 			break;
1497 		case KEYSOCK_HELLO:
1498 			sadb_keysock_hello(&ahstack->ah_pfkey_q, q, mp,
1499 			    ah_ager, (void *)ahstack, &ahstack->ah_event,
1500 			    SADB_SATYPE_AH);
1501 			break;
1502 		default:
1503 			ah1dbg(ahstack, ("Got M_CTL from above of 0x%x.\n",
1504 			    ii->ipsec_info_type));
1505 			freemsg(mp);
1506 			break;
1507 		}
1508 		break;
1509 	case M_IOCTL:
1510 		iocp = (struct iocblk *)mp->b_rptr;
1511 		switch (iocp->ioc_cmd) {
1512 		case ND_SET:
1513 		case ND_GET:
1514 			if (nd_getset(q, ahstack->ipsecah_g_nd, mp)) {
1515 				qreply(q, mp);
1516 				return;
1517 			} else {
1518 				iocp->ioc_error = ENOENT;
1519 			}
1520 			/* FALLTHRU */
1521 		default:
1522 			/* We really don't support any other ioctls, do we? */
1523 
1524 			/* Return EINVAL */
1525 			if (iocp->ioc_error != ENOENT)
1526 				iocp->ioc_error = EINVAL;
1527 			iocp->ioc_count = 0;
1528 			mp->b_datap->db_type = M_IOCACK;
1529 			qreply(q, mp);
1530 			return;
1531 		}
1532 	default:
1533 		ah3dbg(ahstack,
1534 		    ("Got default message, type %d, passing to IP.\n",
1535 		    mp->b_datap->db_type));
1536 		putnext(q, mp);
1537 	}
1538 }
1539 
1540 /*
1541  * Updating use times can be tricky business if the ipsa_haspeer flag is
1542  * set.  This function is called once in an SA's lifetime.
1543  *
1544  * Caller has to REFRELE "assoc" which is passed in.  This function has
1545  * to REFRELE any peer SA that is obtained.
1546  */
1547 static void
1548 ah_set_usetime(ipsa_t *assoc, boolean_t inbound)
1549 {
1550 	ipsa_t *inassoc, *outassoc;
1551 	isaf_t *bucket;
1552 	sadb_t *sp;
1553 	int outhash;
1554 	boolean_t isv6;
1555 	netstack_t	*ns = assoc->ipsa_netstack;
1556 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
1557 
1558 	/* No peer?  No problem! */
1559 	if (!assoc->ipsa_haspeer) {
1560 		sadb_set_usetime(assoc);
1561 		return;
1562 	}
1563 
1564 	/*
1565 	 * Otherwise, we want to grab both the original assoc and its peer.
1566 	 * There might be a race for this, but if it's a real race, the times
1567 	 * will be out-of-synch by at most a second, and since our time
1568 	 * granularity is a second, this won't be a problem.
1569 	 *
1570 	 * If we need tight synchronization on the peer SA, then we need to
1571 	 * reconsider.
1572 	 */
1573 
1574 	/* Use address family to select IPv6/IPv4 */
1575 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
1576 	if (isv6) {
1577 		sp = &ahstack->ah_sadb.s_v6;
1578 	} else {
1579 		sp = &ahstack->ah_sadb.s_v4;
1580 		ASSERT(assoc->ipsa_addrfam == AF_INET);
1581 	}
1582 	if (inbound) {
1583 		inassoc = assoc;
1584 		if (isv6)
1585 			outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
1586 			    &inassoc->ipsa_dstaddr));
1587 		else
1588 			outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
1589 				&inassoc->ipsa_dstaddr));
1590 		bucket = &sp->sdb_of[outhash];
1591 
1592 		mutex_enter(&bucket->isaf_lock);
1593 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
1594 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
1595 		    inassoc->ipsa_addrfam);
1596 		mutex_exit(&bucket->isaf_lock);
1597 		if (outassoc == NULL) {
1598 			/* Q: Do we wish to set haspeer == B_FALSE? */
1599 			ah0dbg(("ah_set_usetime: "
1600 			    "can't find peer for inbound.\n"));
1601 			sadb_set_usetime(inassoc);
1602 			return;
1603 		}
1604 	} else {
1605 		outassoc = assoc;
1606 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
1607 		mutex_enter(&bucket->isaf_lock);
1608 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
1609 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
1610 		    outassoc->ipsa_addrfam);
1611 		mutex_exit(&bucket->isaf_lock);
1612 		if (inassoc == NULL) {
1613 			/* Q: Do we wish to set haspeer == B_FALSE? */
1614 			ah0dbg(("ah_set_usetime: "
1615 			    "can't find peer for outbound.\n"));
1616 			sadb_set_usetime(outassoc);
1617 			return;
1618 		}
1619 	}
1620 
1621 	/* Update usetime on both. */
1622 	sadb_set_usetime(inassoc);
1623 	sadb_set_usetime(outassoc);
1624 
1625 	/*
1626 	 * REFRELE any peer SA.
1627 	 *
1628 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1629 	 * them in { }.
1630 	 */
1631 	if (inbound) {
1632 		IPSA_REFRELE(outassoc);
1633 	} else {
1634 		IPSA_REFRELE(inassoc);
1635 	}
1636 }
1637 
1638 /*
1639  * Add a number of bytes to what the SA has protected so far.  Return
1640  * B_TRUE if the SA can still protect that many bytes.
1641  *
1642  * Caller must REFRELE the passed-in assoc.  This function must REFRELE
1643  * any obtained peer SA.
1644  */
1645 static boolean_t
1646 ah_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
1647 {
1648 	ipsa_t *inassoc, *outassoc;
1649 	isaf_t *bucket;
1650 	boolean_t inrc, outrc, isv6;
1651 	sadb_t *sp;
1652 	int outhash;
1653 	netstack_t	*ns = assoc->ipsa_netstack;
1654 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
1655 
1656 	/* No peer?  No problem! */
1657 	if (!assoc->ipsa_haspeer) {
1658 		return (sadb_age_bytes(ahstack->ah_pfkey_q, assoc, bytes,
1659 		    B_TRUE));
1660 	}
1661 
1662 	/*
1663 	 * Otherwise, we want to grab both the original assoc and its peer.
1664 	 * There might be a race for this, but if it's a real race, two
1665 	 * expire messages may occur.  We limit this by only sending the
1666 	 * expire message on one of the peers, we'll pick the inbound
1667 	 * arbitrarily.
1668 	 *
1669 	 * If we need tight synchronization on the peer SA, then we need to
1670 	 * reconsider.
1671 	 */
1672 
1673 	/* Pick v4/v6 bucket based on addrfam. */
1674 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
1675 	if (isv6) {
1676 		sp = &ahstack->ah_sadb.s_v6;
1677 	} else {
1678 		sp = &ahstack->ah_sadb.s_v4;
1679 		ASSERT(assoc->ipsa_addrfam == AF_INET);
1680 	}
1681 	if (inbound) {
1682 		inassoc = assoc;
1683 		if (isv6)
1684 			outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
1685 			    &inassoc->ipsa_dstaddr));
1686 		else
1687 			outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
1688 				&inassoc->ipsa_dstaddr));
1689 		bucket = &sp->sdb_of[outhash];
1690 		mutex_enter(&bucket->isaf_lock);
1691 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
1692 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
1693 		    inassoc->ipsa_addrfam);
1694 		mutex_exit(&bucket->isaf_lock);
1695 		if (outassoc == NULL) {
1696 			/* Q: Do we wish to set haspeer == B_FALSE? */
1697 			ah0dbg(("ah_age_bytes: "
1698 			    "can't find peer for inbound.\n"));
1699 			return (sadb_age_bytes(ahstack->ah_pfkey_q, inassoc,
1700 			    bytes, B_TRUE));
1701 		}
1702 	} else {
1703 		outassoc = assoc;
1704 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
1705 		mutex_enter(&bucket->isaf_lock);
1706 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
1707 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
1708 		    outassoc->ipsa_addrfam);
1709 		mutex_exit(&bucket->isaf_lock);
1710 		if (inassoc == NULL) {
1711 			/* Q: Do we wish to set haspeer == B_FALSE? */
1712 			ah0dbg(("ah_age_bytes: "
1713 			    "can't find peer for outbound.\n"));
1714 			return (sadb_age_bytes(ahstack->ah_pfkey_q, outassoc,
1715 			    bytes, B_TRUE));
1716 		}
1717 	}
1718 
1719 	inrc = sadb_age_bytes(ahstack->ah_pfkey_q, inassoc, bytes, B_TRUE);
1720 	outrc = sadb_age_bytes(ahstack->ah_pfkey_q, outassoc, bytes, B_FALSE);
1721 
1722 	/*
1723 	 * REFRELE any peer SA.
1724 	 *
1725 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1726 	 * them in { }.
1727 	 */
1728 	if (inbound) {
1729 		IPSA_REFRELE(outassoc);
1730 	} else {
1731 		IPSA_REFRELE(inassoc);
1732 	}
1733 
1734 	return (inrc && outrc);
1735 }
1736 
1737 /*
1738  * Perform the really difficult work of inserting the proposed situation.
1739  * Called while holding the algorithm lock.
1740  */
1741 static void
1742 ah_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs)
1743 {
1744 	sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
1745 	ipsec_out_t *io;
1746 	ipsec_action_t *ap;
1747 	ipsec_prot_t *prot;
1748 	ipsecah_stack_t	*ahstack;
1749 	netstack_t	*ns;
1750 	ipsec_stack_t	*ipss;
1751 
1752 	io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr;
1753 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
1754 
1755 	ns = io->ipsec_out_ns;
1756 	ipss = ns->netstack_ipsec;
1757 	ahstack = ns->netstack_ipsecah;
1758 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
1759 
1760 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
1761 	prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
1762 	*(uint32_t *)(&prop->sadb_prop_replay) = 0;	/* Quick zero-out! */
1763 
1764 	prop->sadb_prop_replay = ahstack->ipsecah_replay_size;
1765 
1766 	/*
1767 	 * Based upon algorithm properties, and what-not, prioritize a
1768 	 * proposal, based on the ordering of the ah algorithms in the
1769 	 * alternatives presented in the policy rule passed down
1770 	 * through the ipsec_out_t and attached to the acquire record.
1771 	 */
1772 
1773 	for (ap = acqrec->ipsacq_act; ap != NULL;
1774 	    ap = ap->ipa_next) {
1775 		ipsec_alginfo_t *aalg;
1776 
1777 		if ((ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY) ||
1778 		    (!ap->ipa_act.ipa_apply.ipp_use_ah))
1779 			continue;
1780 
1781 		prot = &ap->ipa_act.ipa_apply;
1782 
1783 		ASSERT(prot->ipp_auth_alg > 0);
1784 
1785 		aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
1786 		    [prot->ipp_auth_alg];
1787 		if (aalg == NULL || !ALG_VALID(aalg))
1788 			continue;
1789 
1790 		/* XXX check aalg for duplicates??.. */
1791 
1792 		comb->sadb_comb_flags = 0;
1793 		comb->sadb_comb_reserved = 0;
1794 		comb->sadb_comb_encrypt = 0;
1795 		comb->sadb_comb_encrypt_minbits = 0;
1796 		comb->sadb_comb_encrypt_maxbits = 0;
1797 
1798 		comb->sadb_comb_auth = aalg->alg_id;
1799 		comb->sadb_comb_auth_minbits =
1800 		    MAX(prot->ipp_ah_minbits, aalg->alg_ef_minbits);
1801 		comb->sadb_comb_auth_maxbits =
1802 		    MIN(prot->ipp_ah_maxbits, aalg->alg_ef_maxbits);
1803 
1804 		/*
1805 		 * The following may be based on algorithm
1806 		 * properties, but in the meantime, we just pick
1807 		 * some good, sensible numbers.  Key mgmt. can
1808 		 * (and perhaps should) be the place to finalize
1809 		 * such decisions.
1810 		 */
1811 
1812 		/*
1813 		 * No limits on allocations, since we really don't
1814 		 * support that concept currently.
1815 		 */
1816 		comb->sadb_comb_soft_allocations = 0;
1817 		comb->sadb_comb_hard_allocations = 0;
1818 
1819 		/*
1820 		 * These may want to come from policy rule..
1821 		 */
1822 		comb->sadb_comb_soft_bytes =
1823 		    ahstack->ipsecah_default_soft_bytes;
1824 		comb->sadb_comb_hard_bytes =
1825 		    ahstack->ipsecah_default_hard_bytes;
1826 		comb->sadb_comb_soft_addtime =
1827 		    ahstack->ipsecah_default_soft_addtime;
1828 		comb->sadb_comb_hard_addtime =
1829 		    ahstack->ipsecah_default_hard_addtime;
1830 		comb->sadb_comb_soft_usetime =
1831 		    ahstack->ipsecah_default_soft_usetime;
1832 		comb->sadb_comb_hard_usetime =
1833 		    ahstack->ipsecah_default_hard_usetime;
1834 
1835 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
1836 		if (--combs == 0)
1837 			return;	/* out of space.. */
1838 		comb++;
1839 	}
1840 }
1841 
1842 /*
1843  * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
1844  */
1845 static void
1846 ah_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns)
1847 {
1848 	uint_t combs;
1849 	sadb_msg_t *samsg;
1850 	sadb_prop_t *prop;
1851 	mblk_t *pfkeymp, *msgmp;
1852 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
1853 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
1854 
1855 	AH_BUMP_STAT(ahstack, acquire_requests);
1856 
1857 	if (ahstack->ah_pfkey_q == NULL)
1858 		return;
1859 
1860 	/* Set up ACQUIRE. */
1861 	pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_AH,
1862 	    ns->netstack_ipsec);
1863 	if (pfkeymp == NULL) {
1864 		ah0dbg(("sadb_setup_acquire failed.\n"));
1865 		return;
1866 	}
1867 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
1868 	combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
1869 	msgmp = pfkeymp->b_cont;
1870 	samsg = (sadb_msg_t *)(msgmp->b_rptr);
1871 
1872 	/* Insert proposal here. */
1873 
1874 	prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
1875 	ah_insert_prop(prop, acqrec, combs);
1876 	samsg->sadb_msg_len += prop->sadb_prop_len;
1877 	msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
1878 
1879 	mutex_exit(&ipss->ipsec_alg_lock);
1880 
1881 	/*
1882 	 * Must mutex_exit() before sending PF_KEY message up, in
1883 	 * order to avoid recursive mutex_enter() if there are no registered
1884 	 * listeners.
1885 	 *
1886 	 * Once I've sent the message, I'm cool anyway.
1887 	 */
1888 	mutex_exit(&acqrec->ipsacq_lock);
1889 	if (extended != NULL) {
1890 		putnext(ahstack->ah_pfkey_q, extended);
1891 	}
1892 	putnext(ahstack->ah_pfkey_q, pfkeymp);
1893 }
1894 
1895 /*
1896  * Handle the SADB_GETSPI message.  Create a larval SA.
1897  */
1898 static void
1899 ah_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1900 {
1901 	ipsa_t *newbie, *target;
1902 	isaf_t *outbound, *inbound;
1903 	int rc, diagnostic;
1904 	sadb_sa_t *assoc;
1905 	keysock_out_t *kso;
1906 	uint32_t newspi;
1907 
1908 	/*
1909 	 * Randomly generate a proposed SPI value.
1910 	 */
1911 	(void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t));
1912 	newbie = sadb_getspi(ksi, newspi, &diagnostic,
1913 	    ahstack->ipsecah_netstack);
1914 
1915 	if (newbie == NULL) {
1916 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM, diagnostic,
1917 		    ksi->ks_in_serial);
1918 		return;
1919 	} else if (newbie == (ipsa_t *)-1) {
1920 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL, diagnostic,
1921 		    ksi->ks_in_serial);
1922 		return;
1923 	}
1924 
1925 	/*
1926 	 * XXX - We may randomly collide.  We really should recover from this.
1927 	 *	 Unfortunately, that could require spending way-too-much-time
1928 	 *	 in here.  For now, let the user retry.
1929 	 */
1930 
1931 	if (newbie->ipsa_addrfam == AF_INET6) {
1932 		outbound = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6,
1933 		    *(uint32_t *)(newbie->ipsa_dstaddr));
1934 		inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v6,
1935 		    newbie->ipsa_spi);
1936 	} else {
1937 		outbound = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4,
1938 		    *(uint32_t *)(newbie->ipsa_dstaddr));
1939 		inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v4,
1940 		    newbie->ipsa_spi);
1941 	}
1942 
1943 	mutex_enter(&outbound->isaf_lock);
1944 	mutex_enter(&inbound->isaf_lock);
1945 
1946 	/*
1947 	 * Check for collisions (i.e. did sadb_getspi() return with something
1948 	 * that already exists?).
1949 	 *
1950 	 * Try outbound first.  Even though SADB_GETSPI is traditionally
1951 	 * for inbound SAs, you never know what a user might do.
1952 	 */
1953 	target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
1954 	    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
1955 	if (target == NULL) {
1956 		target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
1957 		    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
1958 		    newbie->ipsa_addrfam);
1959 	}
1960 
1961 	/*
1962 	 * I don't have collisions elsewhere!
1963 	 * (Nor will I because I'm still holding inbound/outbound locks.)
1964 	 */
1965 
1966 	if (target != NULL) {
1967 		rc = EEXIST;
1968 		IPSA_REFRELE(target);
1969 	} else {
1970 		/*
1971 		 * sadb_insertassoc() also checks for collisions, so
1972 		 * if there's a colliding larval entry, rc will be set
1973 		 * to EEXIST.
1974 		 */
1975 		rc = sadb_insertassoc(newbie, inbound);
1976 		(void) drv_getparm(TIME, &newbie->ipsa_hardexpiretime);
1977 		newbie->ipsa_hardexpiretime += ahstack->ipsecah_larval_timeout;
1978 	}
1979 
1980 	/*
1981 	 * Can exit outbound mutex.  Hold inbound until we're done with
1982 	 * newbie.
1983 	 */
1984 	mutex_exit(&outbound->isaf_lock);
1985 
1986 	if (rc != 0) {
1987 		mutex_exit(&inbound->isaf_lock);
1988 		IPSA_REFRELE(newbie);
1989 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, rc,
1990 		    SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
1991 		return;
1992 	}
1993 
1994 	/* Can write here because I'm still holding the bucket lock. */
1995 	newbie->ipsa_type = SADB_SATYPE_AH;
1996 
1997 	/*
1998 	 * Construct successful return message.  We have one thing going
1999 	 * for us in PF_KEY v2.  That's the fact that
2000 	 *	sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
2001 	 */
2002 	assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
2003 	assoc->sadb_sa_exttype = SADB_EXT_SA;
2004 	assoc->sadb_sa_spi = newbie->ipsa_spi;
2005 	*((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
2006 	mutex_exit(&inbound->isaf_lock);
2007 
2008 	/* Convert KEYSOCK_IN to KEYSOCK_OUT. */
2009 	kso = (keysock_out_t *)ksi;
2010 	kso->ks_out_len = sizeof (*kso);
2011 	kso->ks_out_serial = ksi->ks_in_serial;
2012 	kso->ks_out_type = KEYSOCK_OUT;
2013 
2014 	/*
2015 	 * Can safely putnext() to ah_pfkey_q, because this is a turnaround
2016 	 * from the ah_pfkey_q.
2017 	 */
2018 	putnext(ahstack->ah_pfkey_q, mp);
2019 }
2020 
2021 /*
2022  * IPv6 sends up the ICMP errors for validation and the removal of the AH
2023  * header.
2024  */
2025 static ipsec_status_t
2026 ah_icmp_error_v6(mblk_t *ipsec_mp, ipsecah_stack_t *ahstack)
2027 {
2028 	mblk_t *mp;
2029 	ip6_t *ip6h, *oip6h;
2030 	uint16_t hdr_length, ah_length;
2031 	uint8_t *nexthdrp;
2032 	ah_t *ah;
2033 	icmp6_t *icmp6;
2034 	isaf_t *isaf;
2035 	ipsa_t *assoc;
2036 	uint8_t *post_ah_ptr;
2037 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2038 
2039 	mp = ipsec_mp->b_cont;
2040 	ASSERT(mp->b_datap->db_type == M_CTL);
2041 
2042 	/*
2043 	 * Change the type to M_DATA till we finish pullups.
2044 	 */
2045 	mp->b_datap->db_type = M_DATA;
2046 
2047 	/*
2048 	 * Eat the cost of a pullupmsg() for now.  It makes the rest of this
2049 	 * code far less convoluted.
2050 	 */
2051 	if (!pullupmsg(mp, -1) ||
2052 	    !ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, &hdr_length,
2053 		&nexthdrp) ||
2054 	    mp->b_rptr + hdr_length + sizeof (icmp6_t) + sizeof (ip6_t) +
2055 	    sizeof (ah_t) > mp->b_wptr) {
2056 		IP_AH_BUMP_STAT(ipss, in_discards);
2057 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2058 		    DROPPER(ipss, ipds_ah_nomem),
2059 		    &ahstack->ah_dropper);
2060 		return (IPSEC_STATUS_FAILED);
2061 	}
2062 
2063 	oip6h = (ip6_t *)mp->b_rptr;
2064 	icmp6 = (icmp6_t *)((uint8_t *)oip6h + hdr_length);
2065 	ip6h = (ip6_t *)(icmp6 + 1);
2066 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) {
2067 		IP_AH_BUMP_STAT(ipss, in_discards);
2068 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2069 		    DROPPER(ipss, ipds_ah_bad_v6_hdrs),
2070 		    &ahstack->ah_dropper);
2071 		return (IPSEC_STATUS_FAILED);
2072 	}
2073 	ah = (ah_t *)((uint8_t *)ip6h + hdr_length);
2074 
2075 	isaf = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6, ip6h->ip6_dst);
2076 	mutex_enter(&isaf->isaf_lock);
2077 	assoc = ipsec_getassocbyspi(isaf, ah->ah_spi,
2078 	    (uint32_t *)&ip6h->ip6_src, (uint32_t *)&ip6h->ip6_dst, AF_INET6);
2079 	mutex_exit(&isaf->isaf_lock);
2080 
2081 	if (assoc == NULL) {
2082 		IP_AH_BUMP_STAT(ipss, lookup_failure);
2083 		IP_AH_BUMP_STAT(ipss, in_discards);
2084 		if (ahstack->ipsecah_log_unknown_spi) {
2085 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2086 			    SL_CONSOLE | SL_WARN | SL_ERROR,
2087 			    "Bad ICMP message - No association for the "
2088 			    "attached AH header whose spi is 0x%x, "
2089 			    "sender is 0x%x\n",
2090 			    ah->ah_spi, &oip6h->ip6_src, AF_INET6,
2091 			    ahstack->ipsecah_netstack);
2092 		}
2093 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2094 		    DROPPER(ipss, ipds_ah_no_sa),
2095 		    &ahstack->ah_dropper);
2096 		return (IPSEC_STATUS_FAILED);
2097 	}
2098 
2099 	IPSA_REFRELE(assoc);
2100 
2101 	/*
2102 	 * There seems to be a valid association. If there is enough of AH
2103 	 * header remove it, otherwise bail.  One could check whether it has
2104 	 * complete AH header plus 8 bytes but it does not make sense if an
2105 	 * icmp error is returned for ICMP messages e.g ICMP time exceeded,
2106 	 * that are being sent up. Let the caller figure out.
2107 	 *
2108 	 * NOTE: ah_length is the number of 32 bit words minus 2.
2109 	 */
2110 	ah_length = (ah->ah_length << 2) + 8;
2111 	post_ah_ptr = (uint8_t *)ah + ah_length;
2112 
2113 	if (post_ah_ptr > mp->b_wptr) {
2114 		IP_AH_BUMP_STAT(ipss, in_discards);
2115 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2116 		    DROPPER(ipss, ipds_ah_bad_length),
2117 		    &ahstack->ah_dropper);
2118 		return (IPSEC_STATUS_FAILED);
2119 	}
2120 
2121 	ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - ah_length);
2122 	*nexthdrp = ah->ah_nexthdr;
2123 	ovbcopy(post_ah_ptr, ah,
2124 	    (size_t)((uintptr_t)mp->b_wptr - (uintptr_t)post_ah_ptr));
2125 	mp->b_wptr -= ah_length;
2126 	/* Rewhack to be an ICMP error. */
2127 	mp->b_datap->db_type = M_CTL;
2128 
2129 	return (IPSEC_STATUS_SUCCESS);
2130 }
2131 
2132 /*
2133  * IP sends up the ICMP errors for validation and the removal of
2134  * the AH header.
2135  */
2136 static ipsec_status_t
2137 ah_icmp_error_v4(mblk_t *ipsec_mp, ipsecah_stack_t *ahstack)
2138 {
2139 	mblk_t *mp;
2140 	mblk_t *mp1;
2141 	icmph_t *icmph;
2142 	int iph_hdr_length;
2143 	int hdr_length;
2144 	isaf_t *hptr;
2145 	ipsa_t *assoc;
2146 	int ah_length;
2147 	ipha_t *ipha;
2148 	ipha_t *oipha;
2149 	ah_t *ah;
2150 	uint32_t length;
2151 	int alloc_size;
2152 	uint8_t nexthdr;
2153 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2154 
2155 	mp = ipsec_mp->b_cont;
2156 	ASSERT(mp->b_datap->db_type == M_CTL);
2157 
2158 	/*
2159 	 * Change the type to M_DATA till we finish pullups.
2160 	 */
2161 	mp->b_datap->db_type = M_DATA;
2162 
2163 	oipha = ipha = (ipha_t *)mp->b_rptr;
2164 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
2165 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2166 
2167 	ipha = (ipha_t *)&icmph[1];
2168 	hdr_length = IPH_HDR_LENGTH(ipha);
2169 
2170 	/*
2171 	 * See if we have enough to locate the SPI
2172 	 */
2173 	if ((uchar_t *)ipha + hdr_length + 8 > mp->b_wptr) {
2174 		if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 8 -
2175 			    mp->b_rptr)) {
2176 			ipsec_rl_strlog(ahstack->ipsecah_netstack,
2177 			    info.mi_idnum, 0, 0,
2178 			    SL_WARN | SL_ERROR,
2179 			    "ICMP error: Small AH header\n");
2180 			IP_AH_BUMP_STAT(ipss, in_discards);
2181 			ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2182 			    DROPPER(ipss, ipds_ah_bad_length),
2183 			    &ahstack->ah_dropper);
2184 			return (IPSEC_STATUS_FAILED);
2185 		}
2186 		icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2187 		ipha = (ipha_t *)&icmph[1];
2188 	}
2189 
2190 	ah = (ah_t *)((uint8_t *)ipha + hdr_length);
2191 	nexthdr = ah->ah_nexthdr;
2192 
2193 	hptr = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4, ipha->ipha_dst);
2194 	mutex_enter(&hptr->isaf_lock);
2195 	assoc = ipsec_getassocbyspi(hptr, ah->ah_spi,
2196 	    (uint32_t *)&ipha->ipha_src, (uint32_t *)&ipha->ipha_dst, AF_INET);
2197 	mutex_exit(&hptr->isaf_lock);
2198 
2199 	if (assoc == NULL) {
2200 		IP_AH_BUMP_STAT(ipss, lookup_failure);
2201 		IP_AH_BUMP_STAT(ipss, in_discards);
2202 		if (ahstack->ipsecah_log_unknown_spi) {
2203 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2204 			    SL_CONSOLE | SL_WARN | SL_ERROR,
2205 			    "Bad ICMP message - No association for the "
2206 			    "attached AH header whose spi is 0x%x, "
2207 			    "sender is 0x%x\n",
2208 			    ah->ah_spi, &oipha->ipha_src, AF_INET,
2209 			    ahstack->ipsecah_netstack);
2210 		}
2211 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2212 		    DROPPER(ipss, ipds_ah_no_sa),
2213 		    &ahstack->ah_dropper);
2214 		return (IPSEC_STATUS_FAILED);
2215 	}
2216 
2217 	IPSA_REFRELE(assoc);
2218 	/*
2219 	 * There seems to be a valid association. If there
2220 	 * is enough of AH header remove it, otherwise remove
2221 	 * as much as possible and send it back. One could check
2222 	 * whether it has complete AH header plus 8 bytes but it
2223 	 * does not make sense if an icmp error is returned for
2224 	 * ICMP messages e.g ICMP time exceeded, that are being
2225 	 * sent up. Let the caller figure out.
2226 	 *
2227 	 * NOTE: ah_length is the number of 32 bit words minus 2.
2228 	 */
2229 	ah_length = (ah->ah_length << 2) + 8;
2230 
2231 	if ((uchar_t *)ipha + hdr_length + ah_length > mp->b_wptr) {
2232 		if (mp->b_cont == NULL) {
2233 			/*
2234 			 * There is nothing to pullup. Just remove as
2235 			 * much as possible. This is a common case for
2236 			 * IPV4.
2237 			 */
2238 			ah_length = (mp->b_wptr - ((uchar_t *)ipha +
2239 			    hdr_length));
2240 			goto done;
2241 		}
2242 		/* Pullup the full ah header */
2243 		if (!pullupmsg(mp, (uchar_t *)ah + ah_length - mp->b_rptr)) {
2244 			/*
2245 			 * pullupmsg could have failed if there was not
2246 			 * enough to pullup or memory allocation failed.
2247 			 * We tried hard, give up now.
2248 			 */
2249 			IP_AH_BUMP_STAT(ipss, in_discards);
2250 			ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2251 			    DROPPER(ipss, ipds_ah_nomem),
2252 			    &ahstack->ah_dropper);
2253 			return (IPSEC_STATUS_FAILED);
2254 		}
2255 		icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2256 		ipha = (ipha_t *)&icmph[1];
2257 	}
2258 done:
2259 	/*
2260 	 * Remove the AH header and change the protocol.
2261 	 * Don't update the spi fields in the ipsec_in
2262 	 * message as we are called just to validate the
2263 	 * message attached to the ICMP message.
2264 	 *
2265 	 * If we never pulled up since all of the message
2266 	 * is in one single mblk, we can't remove the AH header
2267 	 * by just setting the b_wptr to the beginning of the
2268 	 * AH header. We need to allocate a mblk that can hold
2269 	 * up until the inner IP header and copy them.
2270 	 */
2271 	alloc_size = iph_hdr_length + sizeof (icmph_t) + hdr_length;
2272 
2273 	if ((mp1 = allocb(alloc_size, BPRI_LO)) == NULL) {
2274 		IP_AH_BUMP_STAT(ipss, in_discards);
2275 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2276 		    DROPPER(ipss, ipds_ah_nomem),
2277 		    &ahstack->ah_dropper);
2278 		return (IPSEC_STATUS_FAILED);
2279 	}
2280 	/* ICMP errors are M_CTL messages */
2281 	mp1->b_datap->db_type = M_CTL;
2282 	ipsec_mp->b_cont = mp1;
2283 	bcopy(mp->b_rptr, mp1->b_rptr, alloc_size);
2284 	mp1->b_wptr += alloc_size;
2285 
2286 	/*
2287 	 * Skip whatever we have copied and as much of AH header
2288 	 * possible. If we still have something left in the original
2289 	 * message, tag on.
2290 	 */
2291 	mp->b_rptr = (uchar_t *)ipha + hdr_length + ah_length;
2292 
2293 	if (mp->b_rptr != mp->b_wptr) {
2294 		mp1->b_cont = mp;
2295 	} else {
2296 		if (mp->b_cont != NULL)
2297 			mp1->b_cont = mp->b_cont;
2298 		freeb(mp);
2299 	}
2300 
2301 	ipha = (ipha_t *)(mp1->b_rptr + iph_hdr_length + sizeof (icmph_t));
2302 	ipha->ipha_protocol = nexthdr;
2303 	length = ntohs(ipha->ipha_length);
2304 	length -= ah_length;
2305 	ipha->ipha_length = htons((uint16_t)length);
2306 	ipha->ipha_hdr_checksum = 0;
2307 	ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
2308 
2309 	return (IPSEC_STATUS_SUCCESS);
2310 }
2311 
2312 /*
2313  * IP calls this to validate the ICMP errors that
2314  * we got from the network.
2315  */
2316 ipsec_status_t
2317 ipsecah_icmp_error(mblk_t *mp)
2318 {
2319 	ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr;
2320 	netstack_t	*ns = ii->ipsec_in_ns;
2321 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
2322 
2323 	if (ii->ipsec_in_v4)
2324 		return (ah_icmp_error_v4(mp, ahstack));
2325 	else
2326 		return (ah_icmp_error_v6(mp, ahstack));
2327 }
2328 
2329 static int
2330 ah_fix_tlv_options_v6(uint8_t *oi_opt, uint8_t *pi_opt, uint_t ehdrlen,
2331     uint8_t hdr_type, boolean_t copy_always)
2332 {
2333 	uint8_t opt_type;
2334 	uint_t optlen;
2335 
2336 	ASSERT(hdr_type == IPPROTO_DSTOPTS || hdr_type == IPPROTO_HOPOPTS);
2337 
2338 	/*
2339 	 * Copy the next header and hdr ext. len of the HOP-by-HOP
2340 	 * and Destination option.
2341 	 */
2342 	*pi_opt++ = *oi_opt++;
2343 	*pi_opt++ = *oi_opt++;
2344 	ehdrlen -= 2;
2345 
2346 	/*
2347 	 * Now handle all the TLV encoded options.
2348 	 */
2349 	while (ehdrlen != 0) {
2350 		opt_type = *oi_opt;
2351 
2352 		if (opt_type == IP6OPT_PAD1) {
2353 			optlen = 1;
2354 		} else {
2355 			if (ehdrlen < 2)
2356 				goto bad_opt;
2357 			optlen = 2 + oi_opt[1];
2358 			if (optlen > ehdrlen)
2359 				goto bad_opt;
2360 		}
2361 		if (copy_always || !(opt_type & IP6OPT_MUTABLE)) {
2362 			bcopy(oi_opt, pi_opt, optlen);
2363 		} else {
2364 			if (optlen == 1) {
2365 				*pi_opt = 0;
2366 			} else {
2367 				/*
2368 				 * Copy the type and data length fields.
2369 				 * Zero the option data by skipping
2370 				 * option type and option data len
2371 				 * fields.
2372 				 */
2373 				*pi_opt = *oi_opt;
2374 				*(pi_opt + 1) = *(oi_opt + 1);
2375 				bzero(pi_opt + 2, optlen - 2);
2376 			}
2377 		}
2378 		ehdrlen -= optlen;
2379 		oi_opt += optlen;
2380 		pi_opt += optlen;
2381 	}
2382 	return (0);
2383 bad_opt:
2384 	return (-1);
2385 }
2386 
2387 /*
2388  * Construct a pseudo header for AH, processing all the options.
2389  *
2390  * oip6h is the IPv6 header of the incoming or outgoing packet.
2391  * ip6h is the pointer to the pseudo headers IPV6 header. All
2392  * the space needed for the options have been allocated including
2393  * the AH header.
2394  *
2395  * If copy_always is set, all the options that appear before AH are copied
2396  * blindly without checking for IP6OPT_MUTABLE. This is used by
2397  * ah_auth_out_done().  Please refer to that function for details.
2398  *
2399  * NOTE :
2400  *
2401  * *  AH header is never copied in this function even if copy_always
2402  *    is set. It just returns the ah_offset - offset of the AH header
2403  *    and the caller needs to do the copying. This is done so that we
2404  *    don't have pass extra arguments e.g. SA etc. and also,
2405  *    it is not needed when ah_auth_out_done is calling this function.
2406  */
2407 static uint_t
2408 ah_fix_phdr_v6(ip6_t *ip6h, ip6_t *oip6h, boolean_t outbound,
2409     boolean_t copy_always)
2410 {
2411 	uint8_t	*oi_opt;
2412 	uint8_t	*pi_opt;
2413 	uint8_t nexthdr;
2414 	uint8_t *prev_nexthdr;
2415 	ip6_hbh_t *hbhhdr;
2416 	ip6_dest_t *dsthdr = NULL;
2417 	ip6_rthdr0_t *rthdr;
2418 	int ehdrlen;
2419 	ah_t *ah;
2420 	int ret;
2421 
2422 	/*
2423 	 * In the outbound case for source route, ULP has already moved
2424 	 * the first hop, which is now in ip6_dst. We need to re-arrange
2425 	 * the header to make it look like how it would appear in the
2426 	 * receiver i.e
2427 	 *
2428 	 * Because of ip_massage_options_v6 the header looks like
2429 	 * this :
2430 	 *
2431 	 * ip6_src = S, ip6_dst = I1. followed by I2,I3,D.
2432 	 *
2433 	 * When it reaches the receiver, it would look like
2434 	 *
2435 	 * ip6_src = S, ip6_dst = D. followed by I1,I2,I3.
2436 	 *
2437 	 * NOTE : We assume that there are no problems with the options
2438 	 * as IP should have already checked this.
2439 	 */
2440 
2441 	oi_opt = (uchar_t *)&oip6h[1];
2442 	pi_opt = (uchar_t *)&ip6h[1];
2443 
2444 	/*
2445 	 * We set the prev_nexthdr properly in the pseudo header.
2446 	 * After we finish authentication and come back from the
2447 	 * algorithm module, pseudo header will become the real
2448 	 * IP header.
2449 	 */
2450 	prev_nexthdr = (uint8_t *)&ip6h->ip6_nxt;
2451 	nexthdr = oip6h->ip6_nxt;
2452 	/* Assume IP has already stripped it */
2453 	ASSERT(nexthdr != IPPROTO_FRAGMENT && nexthdr != IPPROTO_RAW);
2454 	ah = NULL;
2455 	dsthdr = NULL;
2456 	for (;;) {
2457 		switch (nexthdr) {
2458 		case IPPROTO_HOPOPTS:
2459 			hbhhdr = (ip6_hbh_t *)oi_opt;
2460 			nexthdr = hbhhdr->ip6h_nxt;
2461 			ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
2462 			ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2463 			    IPPROTO_HOPOPTS, copy_always);
2464 			/*
2465 			 * Return a zero offset indicating error if there
2466 			 * was error.
2467 			 */
2468 			if (ret == -1)
2469 				return (0);
2470 			hbhhdr = (ip6_hbh_t *)pi_opt;
2471 			prev_nexthdr = (uint8_t *)&hbhhdr->ip6h_nxt;
2472 			break;
2473 		case IPPROTO_ROUTING:
2474 			rthdr = (ip6_rthdr0_t *)oi_opt;
2475 			nexthdr = rthdr->ip6r0_nxt;
2476 			ehdrlen = 8 * (rthdr->ip6r0_len + 1);
2477 			if (!copy_always && outbound) {
2478 				int i, left;
2479 				ip6_rthdr0_t *prthdr;
2480 				in6_addr_t *ap, *pap;
2481 
2482 				left = rthdr->ip6r0_segleft;
2483 				prthdr = (ip6_rthdr0_t *)pi_opt;
2484 				pap = (in6_addr_t *)(prthdr + 1);
2485 				ap = (in6_addr_t *)(rthdr + 1);
2486 				/*
2487 				 * First eight bytes except seg_left
2488 				 * does not change en route.
2489 				 */
2490 				bcopy(oi_opt, pi_opt, 8);
2491 				prthdr->ip6r0_segleft = 0;
2492 				/*
2493 				 * First address has been moved to
2494 				 * the destination address of the
2495 				 * ip header by ip_massage_options_v6.
2496 				 * And the real destination address is
2497 				 * in the last address part of the
2498 				 * option.
2499 				 */
2500 				*pap = oip6h->ip6_dst;
2501 				for (i = 1; i < left - 1; i++)
2502 					pap[i] = ap[i - 1];
2503 				ip6h->ip6_dst = *(ap + left - 1);
2504 			} else {
2505 				bcopy(oi_opt, pi_opt, ehdrlen);
2506 			}
2507 			rthdr = (ip6_rthdr0_t *)pi_opt;
2508 			prev_nexthdr = (uint8_t *)&rthdr->ip6r0_nxt;
2509 			break;
2510 		case IPPROTO_DSTOPTS:
2511 			/*
2512 			 * Destination options are tricky.  If there is
2513 			 * a terminal (e.g. non-IPv6-extension) header
2514 			 * following the destination options, don't
2515 			 * reset prev_nexthdr or advance the AH insertion
2516 			 * point and just treat this as a terminal header.
2517 			 *
2518 			 * If this is an inbound packet, just deal with
2519 			 * it as is.
2520 			 */
2521 			dsthdr = (ip6_dest_t *)oi_opt;
2522 			/*
2523 			 * XXX I hope common-subexpression elimination
2524 			 * saves us the double-evaluate.
2525 			 */
2526 			if (outbound && dsthdr->ip6d_nxt != IPPROTO_ROUTING &&
2527 			    dsthdr->ip6d_nxt != IPPROTO_HOPOPTS)
2528 				goto terminal_hdr;
2529 			nexthdr = dsthdr->ip6d_nxt;
2530 			ehdrlen = 8 * (dsthdr->ip6d_len + 1);
2531 			ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2532 			    IPPROTO_DSTOPTS, copy_always);
2533 			/*
2534 			 * Return a zero offset indicating error if there
2535 			 * was error.
2536 			 */
2537 			if (ret == -1)
2538 				return (0);
2539 			break;
2540 		case IPPROTO_AH:
2541 			/*
2542 			 * Be conservative in what you send.  We shouldn't
2543 			 * see two same-scoped AH's in one packet.
2544 			 * (Inner-IP-scoped AH will be hit by terminal
2545 			 * header of IP or IPv6.)
2546 			 */
2547 			ASSERT(!outbound);
2548 			return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2549 		default:
2550 			ASSERT(outbound);
2551 terminal_hdr:
2552 			*prev_nexthdr = IPPROTO_AH;
2553 			ah = (ah_t *)pi_opt;
2554 			ah->ah_nexthdr = nexthdr;
2555 			return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2556 		}
2557 		pi_opt += ehdrlen;
2558 		oi_opt += ehdrlen;
2559 	}
2560 	/* NOTREACHED */
2561 }
2562 
2563 static boolean_t
2564 ah_finish_up(ah_t *phdr_ah, ah_t *inbound_ah, ipsa_t *assoc,
2565     int ah_data_sz, int ah_align_sz, ipsecah_stack_t *ahstack)
2566 {
2567 	int i;
2568 
2569 	/*
2570 	 * Padding :
2571 	 *
2572 	 * 1) Authentication data may have to be padded
2573 	 * before ICV calculation if ICV is not a multiple
2574 	 * of 64 bits. This padding is arbitrary and transmitted
2575 	 * with the packet at the end of the authentication data.
2576 	 * Payload length should include the padding bytes.
2577 	 *
2578 	 * 2) Explicit padding of the whole datagram may be
2579 	 * required by the algorithm which need not be
2580 	 * transmitted. It is assumed that this will be taken
2581 	 * care by the algorithm module.
2582 	 */
2583 	bzero(phdr_ah + 1, ah_data_sz);	/* Zero out ICV for pseudo-hdr. */
2584 
2585 	if (inbound_ah == NULL) {
2586 		/* Outbound AH datagram. */
2587 
2588 		phdr_ah->ah_length = (ah_align_sz >> 2) + 1;
2589 		phdr_ah->ah_reserved = 0;
2590 		phdr_ah->ah_spi = assoc->ipsa_spi;
2591 
2592 		phdr_ah->ah_replay =
2593 		    htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
2594 		if (phdr_ah->ah_replay == 0 && assoc->ipsa_replay_wsize != 0) {
2595 			/*
2596 			 * XXX We have replay counter wrapping.  We probably
2597 			 * want to nuke this SA (and its peer).
2598 			 */
2599 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2600 			    SL_ERROR | SL_CONSOLE | SL_WARN,
2601 			    "Outbound AH SA (0x%x), dst %s has wrapped "
2602 			    "sequence.\n", phdr_ah->ah_spi,
2603 			    assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
2604 			    ahstack->ipsecah_netstack);
2605 
2606 			sadb_replay_delete(assoc);
2607 			/* Caller will free phdr_mp and return NULL. */
2608 			return (B_FALSE);
2609 		}
2610 
2611 		if (ah_data_sz != ah_align_sz) {
2612 			uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2613 			    ah_data_sz);
2614 
2615 			for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2616 				pad[i] = (uchar_t)i;	/* Fill the padding */
2617 			}
2618 		}
2619 	} else {
2620 		/* Inbound AH datagram. */
2621 		phdr_ah->ah_nexthdr = inbound_ah->ah_nexthdr;
2622 		phdr_ah->ah_length = inbound_ah->ah_length;
2623 		phdr_ah->ah_reserved = 0;
2624 		ASSERT(inbound_ah->ah_spi == assoc->ipsa_spi);
2625 		phdr_ah->ah_spi = inbound_ah->ah_spi;
2626 		phdr_ah->ah_replay = inbound_ah->ah_replay;
2627 
2628 		if (ah_data_sz != ah_align_sz) {
2629 			uchar_t *opad = ((uchar_t *)inbound_ah +
2630 			    sizeof (ah_t) + ah_data_sz);
2631 			uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2632 			    ah_data_sz);
2633 
2634 			for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2635 				pad[i] = opad[i];	/* Copy the padding */
2636 			}
2637 		}
2638 	}
2639 
2640 	return (B_TRUE);
2641 }
2642 
2643 /*
2644  * Called upon failing the inbound ICV check. The message passed as
2645  * argument is freed.
2646  */
2647 static void
2648 ah_log_bad_auth(mblk_t *ipsec_in)
2649 {
2650 	mblk_t *mp = ipsec_in->b_cont->b_cont;
2651 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
2652 	boolean_t isv4 = ii->ipsec_in_v4;
2653 	ipsa_t *assoc = ii->ipsec_in_ah_sa;
2654 	int af;
2655 	void *addr;
2656 	netstack_t	*ns = ii->ipsec_in_ns;
2657 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
2658 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2659 
2660 	mp->b_rptr -= ii->ipsec_in_skip_len;
2661 
2662 	if (isv4) {
2663 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
2664 		addr = &ipha->ipha_dst;
2665 		af = AF_INET;
2666 	} else {
2667 		ip6_t *ip6h = (ip6_t *)mp->b_rptr;
2668 		addr = &ip6h->ip6_dst;
2669 		af = AF_INET6;
2670 	}
2671 
2672 	/*
2673 	 * Log the event. Don't print to the console, block
2674 	 * potential denial-of-service attack.
2675 	 */
2676 	AH_BUMP_STAT(ahstack, bad_auth);
2677 
2678 	ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
2679 	    "AH Authentication failed spi %x, dst_addr %s",
2680 	    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
2681 
2682 	IP_AH_BUMP_STAT(ipss, in_discards);
2683 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
2684 	    DROPPER(ipss, ipds_ah_bad_auth),
2685 	    &ahstack->ah_dropper);
2686 }
2687 
2688 /*
2689  * Kernel crypto framework callback invoked after completion of async
2690  * crypto requests.
2691  */
2692 static void
2693 ah_kcf_callback(void *arg, int status)
2694 {
2695 	mblk_t *ipsec_mp = (mblk_t *)arg;
2696 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
2697 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
2698 	netstackid_t	stackid;
2699 	netstack_t	*ns, *ns_arg;
2700 	ipsec_stack_t	*ipss;
2701 	ipsecah_stack_t	*ahstack;
2702 	ipsec_out_t	*io = (ipsec_out_t *)ii;
2703 
2704 	ASSERT(ipsec_mp->b_cont != NULL);
2705 
2706 	if (is_inbound) {
2707 		stackid = ii->ipsec_in_stackid;
2708 		ns_arg = ii->ipsec_in_ns;
2709 	} else {
2710 		stackid = io->ipsec_out_stackid;
2711 		ns_arg = io->ipsec_out_ns;
2712 	}
2713 	/*
2714 	 * Verify that the netstack is still around; could have vanished
2715 	 * while kEf was doing its work.
2716 	 */
2717 	ns = netstack_find_by_stackid(stackid);
2718 	if (ns == NULL || ns != ns_arg) {
2719 		/* Disappeared on us */
2720 		if (ns != NULL)
2721 			netstack_rele(ns);
2722 		freemsg(ipsec_mp);
2723 		return;
2724 	}
2725 
2726 	ahstack = ns->netstack_ipsecah;
2727 	ipss = ns->netstack_ipsec;
2728 
2729 	if (status == CRYPTO_SUCCESS) {
2730 		if (is_inbound) {
2731 			if (ah_auth_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS) {
2732 				netstack_rele(ns);
2733 				return;
2734 			}
2735 			/* finish IPsec processing */
2736 			ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL);
2737 		} else {
2738 			ipha_t *ipha;
2739 
2740 			if (ah_auth_out_done(ipsec_mp) !=
2741 			    IPSEC_STATUS_SUCCESS) {
2742 				netstack_rele(ns);
2743 				return;
2744 			}
2745 
2746 			/* finish IPsec processing */
2747 			ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
2748 			if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
2749 				ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL,
2750 				    NULL);
2751 			} else {
2752 				ip6_t *ip6h = (ip6_t *)ipha;
2753 				ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h,
2754 				    NULL, NULL);
2755 			}
2756 		}
2757 
2758 	} else if (status == CRYPTO_INVALID_MAC) {
2759 		ah_log_bad_auth(ipsec_mp);
2760 	} else {
2761 		ah1dbg(ahstack, ("ah_kcf_callback: crypto failed with 0x%x\n",
2762 			    status));
2763 		AH_BUMP_STAT(ahstack, crypto_failures);
2764 		if (is_inbound)
2765 			IP_AH_BUMP_STAT(ipss, in_discards);
2766 		else
2767 			AH_BUMP_STAT(ahstack, out_discards);
2768 		ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL,
2769 		    DROPPER(ipss, ipds_ah_crypto_failed),
2770 		    &ahstack->ah_dropper);
2771 	}
2772 	netstack_rele(ns);
2773 }
2774 
2775 /*
2776  * Invoked on kernel crypto failure during inbound and outbound processing.
2777  */
2778 static void
2779 ah_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc,
2780     ipsecah_stack_t *ahstack)
2781 {
2782 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2783 
2784 	ah1dbg(ahstack, ("crypto failed for %s AH with 0x%x\n",
2785 	    is_inbound ? "inbound" : "outbound", kef_rc));
2786 	ip_drop_packet(mp, is_inbound, NULL, NULL,
2787 	    DROPPER(ipss, ipds_ah_crypto_failed),
2788 	    &ahstack->ah_dropper);
2789 	AH_BUMP_STAT(ahstack, crypto_failures);
2790 	if (is_inbound)
2791 		IP_AH_BUMP_STAT(ipss, in_discards);
2792 	else
2793 		AH_BUMP_STAT(ahstack, out_discards);
2794 }
2795 
2796 /*
2797  * Helper macros for the ah_submit_req_{inbound,outbound}() functions.
2798  */
2799 
2800 #define	AH_INIT_CALLREQ(_cr, _ipss) {					\
2801 	(_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED;		\
2802 	if ((_ipss)->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] == 		\
2803 	    IPSEC_ALGS_EXEC_ASYNC)					\
2804 		(_cr)->cr_flag |= CRYPTO_ALWAYS_QUEUE;			\
2805 	(_cr)->cr_callback_arg = ipsec_mp;				\
2806 	(_cr)->cr_callback_func = ah_kcf_callback;			\
2807 }
2808 
2809 #define	AH_INIT_CRYPTO_DATA(data, msglen, mblk) {			\
2810 	(data)->cd_format = CRYPTO_DATA_MBLK;				\
2811 	(data)->cd_mp = mblk;						\
2812 	(data)->cd_offset = 0;						\
2813 	(data)->cd_length = msglen;					\
2814 }
2815 
2816 #define	AH_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) {			\
2817 	(mac)->cd_format = CRYPTO_DATA_RAW;				\
2818 	(mac)->cd_offset = 0;						\
2819 	(mac)->cd_length = icvlen;					\
2820 	(mac)->cd_raw.iov_base = icvbuf;				\
2821 	(mac)->cd_raw.iov_len = icvlen;					\
2822 }
2823 
2824 /*
2825  * Submit an inbound packet for processing by the crypto framework.
2826  */
2827 static ipsec_status_t
2828 ah_submit_req_inbound(mblk_t *ipsec_mp, size_t skip_len, uint32_t ah_offset,
2829     ipsa_t *assoc)
2830 {
2831 	int kef_rc;
2832 	mblk_t *phdr_mp;
2833 	crypto_call_req_t call_req;
2834 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
2835 	uint_t icv_len = assoc->ipsa_mac_len;
2836 	crypto_ctx_template_t ctx_tmpl;
2837 	netstack_t	*ns = ii->ipsec_in_ns;
2838 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
2839 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2840 
2841 	phdr_mp = ipsec_mp->b_cont;
2842 	ASSERT(phdr_mp != NULL);
2843 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
2844 
2845 	/*
2846 	 * In case kEF queues and calls back, keep netstackid_t for
2847 	 * verification that the IP instance is still around in
2848 	 * ah_kcf_callback().
2849 	 */
2850 	ii->ipsec_in_stackid = ns->netstack_stackid;
2851 
2852 	/* init arguments for the crypto framework */
2853 	AH_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data, AH_MSGSIZE(phdr_mp),
2854 	    phdr_mp);
2855 
2856 	AH_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac, icv_len,
2857 	    (char *)phdr_mp->b_cont->b_rptr - skip_len + ah_offset +
2858 	    sizeof (ah_t));
2859 
2860 	AH_INIT_CALLREQ(&call_req, ipss);
2861 
2862 	ii->ipsec_in_skip_len = skip_len;
2863 
2864 	IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, ctx_tmpl);
2865 
2866 	/* call KEF to do the MAC operation */
2867 	kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
2868 	    &ii->ipsec_in_crypto_data, &assoc->ipsa_kcfauthkey, ctx_tmpl,
2869 	    &ii->ipsec_in_crypto_mac, &call_req);
2870 
2871 	switch (kef_rc) {
2872 	case CRYPTO_SUCCESS:
2873 		AH_BUMP_STAT(ahstack, crypto_sync);
2874 		return (ah_auth_in_done(ipsec_mp));
2875 	case CRYPTO_QUEUED:
2876 		/* ah_kcf_callback() will be invoked on completion */
2877 		AH_BUMP_STAT(ahstack, crypto_async);
2878 		return (IPSEC_STATUS_PENDING);
2879 	case CRYPTO_INVALID_MAC:
2880 		AH_BUMP_STAT(ahstack, crypto_sync);
2881 		ah_log_bad_auth(ipsec_mp);
2882 		return (IPSEC_STATUS_FAILED);
2883 	}
2884 
2885 	ah_crypto_failed(ipsec_mp, B_TRUE, kef_rc, ahstack);
2886 	return (IPSEC_STATUS_FAILED);
2887 }
2888 
2889 /*
2890  * Submit an outbound packet for processing by the crypto framework.
2891  */
2892 static ipsec_status_t
2893 ah_submit_req_outbound(mblk_t *ipsec_mp, size_t skip_len, ipsa_t *assoc)
2894 {
2895 	int kef_rc;
2896 	mblk_t *phdr_mp;
2897 	crypto_call_req_t call_req;
2898 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
2899 	uint_t icv_len = assoc->ipsa_mac_len;
2900 	netstack_t	*ns = io->ipsec_out_ns;
2901 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
2902 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2903 
2904 	phdr_mp = ipsec_mp->b_cont;
2905 	ASSERT(phdr_mp != NULL);
2906 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
2907 
2908 	/*
2909 	 * In case kEF queues and calls back, keep netstackid_t for
2910 	 * verification that the IP instance is still around in
2911 	 * ah_kcf_callback().
2912 	 */
2913 	io->ipsec_out_stackid = ns->netstack_stackid;
2914 
2915 	/* init arguments for the crypto framework */
2916 	AH_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data, AH_MSGSIZE(phdr_mp),
2917 	    phdr_mp);
2918 
2919 	AH_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac, icv_len,
2920 	    (char *)phdr_mp->b_wptr);
2921 
2922 	AH_INIT_CALLREQ(&call_req, ipss);
2923 
2924 	io->ipsec_out_skip_len = skip_len;
2925 
2926 	ASSERT(io->ipsec_out_ah_sa != NULL);
2927 
2928 	/* call KEF to do the MAC operation */
2929 	kef_rc = crypto_mac(&assoc->ipsa_amech, &io->ipsec_out_crypto_data,
2930 	    &assoc->ipsa_kcfauthkey, assoc->ipsa_authtmpl,
2931 	    &io->ipsec_out_crypto_mac, &call_req);
2932 
2933 	switch (kef_rc) {
2934 	case CRYPTO_SUCCESS:
2935 		AH_BUMP_STAT(ahstack, crypto_sync);
2936 		return (ah_auth_out_done(ipsec_mp));
2937 	case CRYPTO_QUEUED:
2938 		/* ah_kcf_callback() will be invoked on completion */
2939 		AH_BUMP_STAT(ahstack, crypto_async);
2940 		return (IPSEC_STATUS_PENDING);
2941 	}
2942 
2943 	ah_crypto_failed(ipsec_mp, B_FALSE, kef_rc, ahstack);
2944 	return (IPSEC_STATUS_FAILED);
2945 }
2946 
2947 /*
2948  * This function constructs a pseudo header by looking at the IP header
2949  * and options if any. This is called for both outbound and inbound,
2950  * before computing the ICV.
2951  */
2952 static mblk_t *
2953 ah_process_ip_options_v6(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
2954     uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
2955 {
2956 	ip6_t	*ip6h;
2957 	ip6_t	*oip6h;
2958 	mblk_t 	*phdr_mp;
2959 	int option_length;
2960 	uint_t	ah_align_sz;
2961 	uint_t ah_offset;
2962 	int hdr_size;
2963 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2964 
2965 	/*
2966 	 * Allocate space for the authentication data also. It is
2967 	 * useful both during the ICV calculation where we need to
2968 	 * feed in zeroes and while sending the datagram back to IP
2969 	 * where we will be using the same space.
2970 	 *
2971 	 * We need to allocate space for padding bytes if it is not
2972 	 * a multiple of IPV6_PADDING_ALIGN.
2973 	 *
2974 	 * In addition, we allocate space for the ICV computed by
2975 	 * the kernel crypto framework, saving us a separate kmem
2976 	 * allocation down the road.
2977 	 */
2978 
2979 	ah_align_sz = P2ALIGN(ah_data_sz + IPV6_PADDING_ALIGN - 1,
2980 	    IPV6_PADDING_ALIGN);
2981 
2982 	ASSERT(ah_align_sz >= ah_data_sz);
2983 
2984 	hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
2985 	option_length = hdr_size - IPV6_HDR_LEN;
2986 
2987 	/* This was not included in ipsec_ah_get_hdr_size_v6() */
2988 	hdr_size += (sizeof (ah_t) + ah_align_sz);
2989 
2990 	if (!outbound && (MBLKL(mp) < hdr_size)) {
2991 		/*
2992 		 * We have post-AH header options in a separate mblk,
2993 		 * a pullup is required.
2994 		 */
2995 		if (!pullupmsg(mp, hdr_size))
2996 			return (NULL);
2997 	}
2998 
2999 	if ((phdr_mp = allocb(hdr_size + ah_data_sz, BPRI_HI)) == NULL) {
3000 		return (NULL);
3001 	}
3002 
3003 	oip6h = (ip6_t *)mp->b_rptr;
3004 
3005 	/*
3006 	 * Form the basic IP header first. Zero out the header
3007 	 * so that the mutable fields are zeroed out.
3008 	 */
3009 	ip6h = (ip6_t *)phdr_mp->b_rptr;
3010 	bzero(ip6h, sizeof (ip6_t));
3011 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
3012 
3013 	if (outbound) {
3014 		/*
3015 		 * Include the size of AH and authentication data.
3016 		 * This is how our recipient would compute the
3017 		 * authentication data. Look at what we do in the
3018 		 * inbound case below.
3019 		 */
3020 		ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) +
3021 		    sizeof (ah_t) + ah_align_sz);
3022 	} else {
3023 		ip6h->ip6_plen = oip6h->ip6_plen;
3024 	}
3025 
3026 	ip6h->ip6_src = oip6h->ip6_src;
3027 	ip6h->ip6_dst = oip6h->ip6_dst;
3028 
3029 	*length_to_skip = IPV6_HDR_LEN;
3030 	if (option_length == 0) {
3031 		/* Form the AH header */
3032 		ip6h->ip6_nxt = IPPROTO_AH;
3033 		((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
3034 		ah_offset = *length_to_skip;
3035 	} else {
3036 		ip6h->ip6_nxt = oip6h->ip6_nxt;
3037 		/* option_length does not include the AH header's size */
3038 		*length_to_skip += option_length;
3039 
3040 		ah_offset = ah_fix_phdr_v6(ip6h, oip6h, outbound, B_FALSE);
3041 		if (ah_offset == 0) {
3042 			ip_drop_packet(phdr_mp, !outbound, NULL, NULL,
3043 			    DROPPER(ipss, ipds_ah_bad_v6_hdrs),
3044 			    &ahstack->ah_dropper);
3045 			return (NULL);
3046 		}
3047 	}
3048 
3049 	if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)),
3050 	    (outbound ? NULL : ((ah_t *)((uint8_t *)oip6h + ah_offset))),
3051 	    assoc, ah_data_sz, ah_align_sz, ahstack)) {
3052 		freeb(phdr_mp);
3053 		/*
3054 		 * Returning NULL will tell the caller to
3055 		 * IPSA_REFELE(), free the memory, etc.
3056 		 */
3057 		return (NULL);
3058 	}
3059 
3060 	phdr_mp->b_wptr = ((uint8_t *)ip6h + ah_offset + sizeof (ah_t) +
3061 	    ah_align_sz);
3062 	if (!outbound)
3063 		*length_to_skip += sizeof (ah_t) + ah_align_sz;
3064 	return (phdr_mp);
3065 }
3066 
3067 /*
3068  * This function constructs a pseudo header by looking at the IP header
3069  * and options if any. This is called for both outbound and inbound,
3070  * before computing the ICV.
3071  */
3072 static mblk_t *
3073 ah_process_ip_options_v4(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
3074     uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
3075 {
3076 	ipoptp_t opts;
3077 	uint32_t option_length;
3078 	ipha_t	*ipha;
3079 	ipha_t	*oipha;
3080 	mblk_t 	*phdr_mp;
3081 	int	 size;
3082 	uchar_t	*optptr;
3083 	uint8_t optval;
3084 	uint8_t optlen;
3085 	ipaddr_t dst;
3086 	uint32_t v_hlen_tos_len;
3087 	int ip_hdr_length;
3088 	uint_t	ah_align_sz;
3089 	uint32_t off;
3090 
3091 #ifdef	_BIG_ENDIAN
3092 #define	V_HLEN	(v_hlen_tos_len >> 24)
3093 #else
3094 #define	V_HLEN	(v_hlen_tos_len & 0xFF)
3095 #endif
3096 
3097 	oipha = (ipha_t *)mp->b_rptr;
3098 	v_hlen_tos_len = ((uint32_t *)oipha)[0];
3099 
3100 	/*
3101 	 * Allocate space for the authentication data also. It is
3102 	 * useful both during the ICV calculation where we need to
3103 	 * feed in zeroes and while sending the datagram back to IP
3104 	 * where we will be using the same space.
3105 	 *
3106 	 * We need to allocate space for padding bytes if it is not
3107 	 * a multiple of IPV4_PADDING_ALIGN.
3108 	 *
3109 	 * In addition, we allocate space for the ICV computed by
3110 	 * the kernel crypto framework, saving us a separate kmem
3111 	 * allocation down the road.
3112 	 */
3113 
3114 	ah_align_sz = P2ALIGN(ah_data_sz + IPV4_PADDING_ALIGN - 1,
3115 	    IPV4_PADDING_ALIGN);
3116 
3117 	ASSERT(ah_align_sz >= ah_data_sz);
3118 
3119 	size = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz +
3120 	    ah_data_sz;
3121 
3122 	if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
3123 		option_length = oipha->ipha_version_and_hdr_length -
3124 		    (uint8_t)((IP_VERSION << 4) +
3125 		    IP_SIMPLE_HDR_LENGTH_IN_WORDS);
3126 		option_length <<= 2;
3127 		size += option_length;
3128 	}
3129 
3130 	if ((phdr_mp = allocb(size, BPRI_HI)) == NULL) {
3131 		return (NULL);
3132 	}
3133 
3134 	/*
3135 	 * Form the basic IP header first.
3136 	 */
3137 	ipha = (ipha_t *)phdr_mp->b_rptr;
3138 	ipha->ipha_version_and_hdr_length = oipha->ipha_version_and_hdr_length;
3139 	ipha->ipha_type_of_service = 0;
3140 
3141 	if (outbound) {
3142 		/*
3143 		 * Include the size of AH and authentication data.
3144 		 * This is how our recipient would compute the
3145 		 * authentication data. Look at what we do in the
3146 		 * inbound case below.
3147 		 */
3148 		ipha->ipha_length = ntohs(htons(oipha->ipha_length) +
3149 		    sizeof (ah_t) + ah_align_sz);
3150 	} else {
3151 		ipha->ipha_length = oipha->ipha_length;
3152 	}
3153 
3154 	ipha->ipha_ident = oipha->ipha_ident;
3155 	ipha->ipha_fragment_offset_and_flags = 0;
3156 	ipha->ipha_ttl = 0;
3157 	ipha->ipha_protocol = IPPROTO_AH;
3158 	ipha->ipha_hdr_checksum = 0;
3159 	ipha->ipha_src = oipha->ipha_src;
3160 	ipha->ipha_dst = dst = oipha->ipha_dst;
3161 
3162 	/*
3163 	 * If there is no option to process return now.
3164 	 */
3165 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
3166 
3167 	if (V_HLEN == IP_SIMPLE_HDR_VERSION) {
3168 		/* Form the AH header */
3169 		goto ah_hdr;
3170 	}
3171 
3172 	ip_hdr_length += option_length;
3173 
3174 	/*
3175 	 * We have options. In the outbound case for source route,
3176 	 * ULP has already moved the first hop, which is now in
3177 	 * ipha_dst. We need the final destination for the calculation
3178 	 * of authentication data. And also make sure that mutable
3179 	 * and experimental fields are zeroed out in the IP options.
3180 	 */
3181 
3182 	bcopy(&oipha[1], &ipha[1], option_length);
3183 
3184 	for (optval = ipoptp_first(&opts, ipha);
3185 	    optval != IPOPT_EOL;
3186 	    optval = ipoptp_next(&opts)) {
3187 		optptr = opts.ipoptp_cur;
3188 		optlen = opts.ipoptp_len;
3189 		switch (optval) {
3190 		case IPOPT_EXTSEC:
3191 		case IPOPT_COMSEC:
3192 		case IPOPT_RA:
3193 		case IPOPT_SDMDD:
3194 		case IPOPT_SECURITY:
3195 			/*
3196 			 * These options are Immutable, leave them as-is.
3197 			 * Note that IPOPT_NOP is also Immutable, but it
3198 			 * was skipped by ipoptp_next() and thus remains
3199 			 * intact in the header.
3200 			 */
3201 			break;
3202 		case IPOPT_SSRR:
3203 		case IPOPT_LSRR:
3204 			if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0)
3205 				goto bad_ipv4opt;
3206 			/*
3207 			 * These two are mutable and will be zeroed, but
3208 			 * first get the final destination.
3209 			 */
3210 			off = optptr[IPOPT_OFFSET];
3211 			/*
3212 			 * If one of the conditions is true, it means
3213 			 * end of options and dst already has the right
3214 			 * value. So, just fall through.
3215 			 */
3216 			if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
3217 				off = optlen - IP_ADDR_LEN;
3218 				bcopy(&optptr[off], &dst, IP_ADDR_LEN);
3219 			}
3220 			/* FALLTHRU */
3221 		case IPOPT_RR:
3222 		case IPOPT_TS:
3223 		case IPOPT_SATID:
3224 		default:
3225 			/*
3226 			 * optlen should include from the beginning of an
3227 			 * option.
3228 			 * NOTE : Stream Identifier Option (SID): RFC 791
3229 			 * shows the bit pattern of optlen as 2 and documents
3230 			 * the length as 4. We assume it to be 2 here.
3231 			 */
3232 			bzero(optptr, optlen);
3233 			break;
3234 		}
3235 	}
3236 
3237 	if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
3238 bad_ipv4opt:
3239 		ah1dbg(ahstack, ("AH : bad IPv4 option"));
3240 		freeb(phdr_mp);
3241 		return (NULL);
3242 	}
3243 
3244 	/*
3245 	 * Don't change ipha_dst for an inbound datagram as it points
3246 	 * to the right value. Only for the outbound with LSRR/SSRR,
3247 	 * because of ip_massage_options called by the ULP, ipha_dst
3248 	 * points to the first hop and we need to use the final
3249 	 * destination for computing the ICV.
3250 	 */
3251 
3252 	if (outbound)
3253 		ipha->ipha_dst = dst;
3254 ah_hdr:
3255 	((ah_t *)((uint8_t *)ipha + ip_hdr_length))->ah_nexthdr =
3256 	    oipha->ipha_protocol;
3257 	if (!ah_finish_up(((ah_t *)((uint8_t *)ipha + ip_hdr_length)),
3258 	    (outbound ? NULL : ((ah_t *)((uint8_t *)oipha + ip_hdr_length))),
3259 	    assoc, ah_data_sz, ah_align_sz, ahstack)) {
3260 		freeb(phdr_mp);
3261 		/*
3262 		 * Returning NULL will tell the caller to IPSA_REFELE(), free
3263 		 * the memory, etc.
3264 		 */
3265 		return (NULL);
3266 	}
3267 
3268 	phdr_mp->b_wptr = ((uchar_t *)ipha + ip_hdr_length +
3269 	    sizeof (ah_t) + ah_align_sz);
3270 
3271 	ASSERT(phdr_mp->b_wptr <= phdr_mp->b_datap->db_lim);
3272 	if (outbound)
3273 		*length_to_skip = ip_hdr_length;
3274 	else
3275 		*length_to_skip = ip_hdr_length + sizeof (ah_t) + ah_align_sz;
3276 	return (phdr_mp);
3277 }
3278 
3279 /*
3280  * Authenticate an outbound datagram. This function is called
3281  * whenever IP sends an outbound datagram that needs authentication.
3282  */
3283 static ipsec_status_t
3284 ah_outbound(mblk_t *ipsec_out)
3285 {
3286 	mblk_t *mp;
3287 	mblk_t *phdr_mp;
3288 	ipsec_out_t *oi;
3289 	ipsa_t *assoc;
3290 	int length_to_skip;
3291 	uint_t ah_align_sz;
3292 	uint_t age_bytes;
3293 	netstack_t	*ns;
3294 	ipsec_stack_t	*ipss;
3295 	ipsecah_stack_t	*ahstack;
3296 
3297 	/*
3298 	 * Construct the chain of mblks
3299 	 *
3300 	 * IPSEC_OUT->PSEUDO_HDR->DATA
3301 	 *
3302 	 * one by one.
3303 	 */
3304 
3305 	ASSERT(ipsec_out->b_datap->db_type == M_CTL);
3306 
3307 	ASSERT(MBLKL(ipsec_out) >= sizeof (ipsec_info_t));
3308 
3309 	mp = ipsec_out->b_cont;
3310 	oi = (ipsec_out_t *)ipsec_out->b_rptr;
3311 	ns = oi->ipsec_out_ns;
3312 	ipss = ns->netstack_ipsec;
3313 	ahstack = ns->netstack_ipsecah;
3314 
3315 	AH_BUMP_STAT(ahstack, out_requests);
3316 
3317 	ASSERT(mp->b_datap->db_type == M_DATA);
3318 
3319 	assoc = oi->ipsec_out_ah_sa;
3320 	ASSERT(assoc != NULL);
3321 	if (assoc->ipsa_usetime == 0)
3322 		ah_set_usetime(assoc, B_FALSE);
3323 
3324 	/*
3325 	 * Age SA according to number of bytes that will be sent after
3326 	 * adding the AH header, ICV, and padding to the packet.
3327 	 */
3328 
3329 	if (oi->ipsec_out_v4) {
3330 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
3331 		ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3332 		    IPV4_PADDING_ALIGN - 1, IPV4_PADDING_ALIGN);
3333 		age_bytes = ntohs(ipha->ipha_length) + sizeof (ah_t) +
3334 		    ah_align_sz;
3335 	} else {
3336 		ip6_t *ip6h = (ip6_t *)mp->b_rptr;
3337 		ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3338 		    IPV6_PADDING_ALIGN - 1, IPV6_PADDING_ALIGN);
3339 		age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
3340 			sizeof (ah_t) + ah_align_sz;
3341 	}
3342 
3343 	if (!ah_age_bytes(assoc, age_bytes, B_FALSE)) {
3344 		/* rig things as if ipsec_getassocbyconn() failed */
3345 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
3346 		    "AH association 0x%x, dst %s had bytes expire.\n",
3347 		    ntohl(assoc->ipsa_spi), assoc->ipsa_dstaddr, AF_INET,
3348 		    ahstack->ipsecah_netstack);
3349 		freemsg(ipsec_out);
3350 		return (IPSEC_STATUS_FAILED);
3351 	}
3352 
3353 	if (oi->ipsec_out_is_capab_ill) {
3354 		ah3dbg(ahstack, ("ah_outbound: pkt can be accelerated\n"));
3355 		if (oi->ipsec_out_v4)
3356 			return (ah_outbound_accelerated_v4(ipsec_out, assoc));
3357 		else
3358 			return (ah_outbound_accelerated_v6(ipsec_out, assoc));
3359 	}
3360 	AH_BUMP_STAT(ahstack, noaccel);
3361 
3362 	/*
3363 	 * Insert pseudo header:
3364 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3365 	 */
3366 
3367 	if (oi->ipsec_out_v4) {
3368 		phdr_mp = ah_process_ip_options_v4(mp, assoc, &length_to_skip,
3369 		    assoc->ipsa_mac_len, B_TRUE, ahstack);
3370 	} else {
3371 		phdr_mp = ah_process_ip_options_v6(mp, assoc, &length_to_skip,
3372 		    assoc->ipsa_mac_len, B_TRUE, ahstack);
3373 	}
3374 
3375 	if (phdr_mp == NULL) {
3376 		AH_BUMP_STAT(ahstack, out_discards);
3377 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
3378 		    DROPPER(ipss, ipds_ah_bad_v4_opts),
3379 		    &ahstack->ah_dropper);
3380 		return (IPSEC_STATUS_FAILED);
3381 	}
3382 
3383 	ipsec_out->b_cont = phdr_mp;
3384 	phdr_mp->b_cont = mp;
3385 	mp->b_rptr += length_to_skip;
3386 
3387 	/*
3388 	 * At this point ipsec_out points to the IPSEC_OUT, new_mp
3389 	 * points to an mblk containing the pseudo header (IP header,
3390 	 * AH header, and ICV with mutable fields zero'ed out).
3391 	 * mp points to the mblk containing the ULP data. The original
3392 	 * IP header is kept before the ULP data in mp.
3393 	 */
3394 
3395 	/* submit MAC request to KCF */
3396 	return (ah_submit_req_outbound(ipsec_out, length_to_skip, assoc));
3397 }
3398 
3399 static ipsec_status_t
3400 ah_inbound(mblk_t *ipsec_in_mp, void *arg)
3401 {
3402 	mblk_t *data_mp = ipsec_in_mp->b_cont;
3403 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
3404 	ah_t *ah = (ah_t *)arg;
3405 	ipsa_t *assoc = ii->ipsec_in_ah_sa;
3406 	int length_to_skip;
3407 	int ah_length;
3408 	mblk_t *phdr_mp;
3409 	uint32_t ah_offset;
3410 	netstack_t	*ns = ii->ipsec_in_ns;
3411 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
3412 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
3413 
3414 	ASSERT(assoc != NULL);
3415 	if (assoc->ipsa_usetime == 0)
3416 		ah_set_usetime(assoc, B_TRUE);
3417 
3418 	/*
3419 	 * We may wish to check replay in-range-only here as an optimization.
3420 	 * Include the reality check of ipsa->ipsa_replay >
3421 	 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
3422 	 * where N == ipsa->ipsa_replay_wsize.
3423 	 *
3424 	 * Another check that may come here later is the "collision" check.
3425 	 * If legitimate packets flow quickly enough, this won't be a problem,
3426 	 * but collisions may cause authentication algorithm crunching to
3427 	 * take place when it doesn't need to.
3428 	 */
3429 	if (!sadb_replay_peek(assoc, ah->ah_replay)) {
3430 		AH_BUMP_STAT(ahstack, replay_early_failures);
3431 		IP_AH_BUMP_STAT(ipss, in_discards);
3432 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3433 		    DROPPER(ipss, ipds_ah_early_replay),
3434 		    &ahstack->ah_dropper);
3435 		return (IPSEC_STATUS_FAILED);
3436 	}
3437 
3438 	/*
3439 	 * The offset of the AH header can be computed from its pointer
3440 	 * within the data mblk, which was pulled up until the AH header
3441 	 * by ipsec_inbound_ah_sa() during SA selection.
3442 	 */
3443 	ah_offset = (uchar_t *)ah - data_mp->b_rptr;
3444 
3445 	/*
3446 	 * Has this packet already been processed by a hardware
3447 	 * IPsec accelerator?
3448 	 */
3449 	if (ii->ipsec_in_accelerated) {
3450 		ah3dbg(ahstack,
3451 		    ("ah_inbound_v6: pkt processed by ill=%d isv6=%d\n",
3452 		    ii->ipsec_in_ill_index, !ii->ipsec_in_v4));
3453 		return (ah_inbound_accelerated(ipsec_in_mp, ii->ipsec_in_v4,
3454 		    assoc, ah_offset));
3455 	}
3456 	AH_BUMP_STAT(ahstack, noaccel);
3457 
3458 	/*
3459 	 * We need to pullup until the ICV before we call
3460 	 * ah_process_ip_options_v6.
3461 	 */
3462 	ah_length = (ah->ah_length << 2) + 8;
3463 
3464 	/*
3465 	 * NOTE : If we want to use any field of IP/AH header, you need
3466 	 * to re-assign following the pullup.
3467 	 */
3468 	if (((uchar_t *)ah + ah_length) > data_mp->b_wptr) {
3469 		if (!pullupmsg(data_mp, (uchar_t *)ah + ah_length -
3470 		    data_mp->b_rptr)) {
3471 			(void) ipsec_rl_strlog(ns, info.mi_idnum, 0, 0,
3472 			    SL_WARN | SL_ERROR,
3473 			    "ah_inbound: Small AH header\n");
3474 			IP_AH_BUMP_STAT(ipss, in_discards);
3475 			ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3476 			    DROPPER(ipss, ipds_ah_nomem),
3477 			    &ahstack->ah_dropper);
3478 			return (IPSEC_STATUS_FAILED);
3479 		}
3480 	}
3481 
3482 	/*
3483 	 * Insert pseudo header:
3484 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3485 	 */
3486 	if (ii->ipsec_in_v4) {
3487 		phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
3488 		    &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
3489 	} else {
3490 		phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
3491 		    &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
3492 	}
3493 
3494 	if (phdr_mp == NULL) {
3495 		IP_AH_BUMP_STAT(ipss, in_discards);
3496 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3497 		    (ii->ipsec_in_v4 ?
3498 		    DROPPER(ipss, ipds_ah_bad_v4_opts) :
3499 		    DROPPER(ipss, ipds_ah_bad_v6_hdrs)),
3500 		    &ahstack->ah_dropper);
3501 		return (IPSEC_STATUS_FAILED);
3502 	}
3503 
3504 	ipsec_in_mp->b_cont = phdr_mp;
3505 	phdr_mp->b_cont = data_mp;
3506 	data_mp->b_rptr += length_to_skip;
3507 
3508 	/* submit request to KCF */
3509 	return (ah_submit_req_inbound(ipsec_in_mp, length_to_skip, ah_offset,
3510 	    assoc));
3511 }
3512 
3513 /*
3514  * ah_inbound_accelerated:
3515  * Called from ah_inbound() to process IPsec packets that have been
3516  * accelerated by hardware.
3517  *
3518  * Basically does what ah_auth_in_done() with some changes since
3519  * no pseudo-headers are involved, i.e. the passed message is a
3520  * IPSEC_INFO->DATA.
3521  *
3522  * It is assumed that only packets that have been successfully
3523  * processed by the adapter come here.
3524  *
3525  * 1. get algorithm structure corresponding to association
3526  * 2. calculate pointers to authentication header and ICV
3527  * 3. compare ICV in AH header with ICV in data attributes
3528  *    3.1 if different:
3529  *	  3.1.1 generate error
3530  *        3.1.2 discard message
3531  *    3.2 if ICV matches:
3532  *	  3.2.1 check replay
3533  *        3.2.2 remove AH header
3534  *        3.2.3 age SA byte
3535  *        3.2.4 send to IP
3536  */
3537 ipsec_status_t
3538 ah_inbound_accelerated(mblk_t *ipsec_in, boolean_t isv4, ipsa_t *assoc,
3539     uint32_t ah_offset)
3540 {
3541 	mblk_t *mp;
3542 	ipha_t *ipha;
3543 	ah_t *ah;
3544 	ipsec_in_t *ii;
3545 	uint32_t icv_len;
3546 	uint32_t align_len;
3547 	uint32_t age_bytes;
3548 	ip6_t *ip6h;
3549 	uint8_t *in_icv;
3550 	mblk_t *hada_mp;
3551 	uint32_t next_hdr;
3552 	da_ipsec_t *hada;
3553 	kstat_named_t *counter;
3554 	ipsecah_stack_t	*ahstack;
3555 	netstack_t	*ns;
3556 	ipsec_stack_t	*ipss;
3557 
3558 	ii = (ipsec_in_t *)ipsec_in->b_rptr;
3559 	ns = ii->ipsec_in_ns;
3560 	ahstack = ns->netstack_ipsecah;
3561 	ipss = ns->netstack_ipsec;
3562 
3563 	mp = ipsec_in->b_cont;
3564 	hada_mp = ii->ipsec_in_da;
3565 	ASSERT(hada_mp != NULL);
3566 	hada = (da_ipsec_t *)hada_mp->b_rptr;
3567 
3568 	AH_BUMP_STAT(ahstack, in_accelerated);
3569 
3570 	/*
3571 	 * We only support one level of decapsulation in hardware, so
3572 	 * nuke the pointer.
3573 	 */
3574 	ii->ipsec_in_da = NULL;
3575 	ii->ipsec_in_accelerated = B_FALSE;
3576 
3577 	/*
3578 	 * Extract ICV length from attributes M_CTL and sanity check
3579 	 * its value. We allow the mblk to be smaller than da_ipsec_t
3580 	 * for a small ICV, as long as the entire ICV fits within the mblk.
3581 	 * Also ensures that the ICV length computed by Provider
3582 	 * corresponds to the ICV length of the algorithm specified by the SA.
3583 	 */
3584 	icv_len = hada->da_icv_len;
3585 	if ((icv_len != assoc->ipsa_mac_len) ||
3586 	    (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) <
3587 		(sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) {
3588 		ah0dbg(("ah_inbound_accelerated: "
3589 		    "ICV len (%u) incorrect or mblk too small (%u)\n",
3590 		    icv_len, (uint32_t)(MBLKL(hada_mp))));
3591 		counter = DROPPER(ipss, ipds_ah_bad_length);
3592 		goto ah_in_discard;
3593 	}
3594 	ASSERT(icv_len != 0);
3595 
3596 	/* compute the padded AH ICV len */
3597 	if (isv4) {
3598 		ipha = (ipha_t *)mp->b_rptr;
3599 		align_len = (icv_len + IPV4_PADDING_ALIGN - 1) &
3600 		    -IPV4_PADDING_ALIGN;
3601 	} else {
3602 		ip6h = (ip6_t *)mp->b_rptr;
3603 		align_len = (icv_len + IPV6_PADDING_ALIGN - 1) &
3604 		    -IPV6_PADDING_ALIGN;
3605 	}
3606 
3607 	ah = (ah_t *)(mp->b_rptr + ah_offset);
3608 	in_icv = (uint8_t *)ah + sizeof (ah_t);
3609 
3610 	/* compare ICV in AH header vs ICV computed by adapter */
3611 	if (bcmp(hada->da_icv, in_icv, icv_len)) {
3612 		int af;
3613 		void *addr;
3614 
3615 		if (isv4) {
3616 			addr = &ipha->ipha_dst;
3617 			af = AF_INET;
3618 		} else {
3619 			addr = &ip6h->ip6_dst;
3620 			af = AF_INET6;
3621 		}
3622 
3623 		/*
3624 		 * Log the event. Don't print to the console, block
3625 		 * potential denial-of-service attack.
3626 		 */
3627 		AH_BUMP_STAT(ahstack, bad_auth);
3628 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
3629 		    "AH Authentication failed spi %x, dst_addr %s",
3630 		    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
3631 		counter = DROPPER(ipss, ipds_ah_bad_auth);
3632 		goto ah_in_discard;
3633 	}
3634 
3635 	ah3dbg(ahstack, ("AH succeeded, checking replay\n"));
3636 	AH_BUMP_STAT(ahstack, good_auth);
3637 
3638 	if (!sadb_replay_check(assoc, ah->ah_replay)) {
3639 		int af;
3640 		void *addr;
3641 
3642 		if (isv4) {
3643 			addr = &ipha->ipha_dst;
3644 			af = AF_INET;
3645 		} else {
3646 			addr = &ip6h->ip6_dst;
3647 			af = AF_INET6;
3648 		}
3649 
3650 		/*
3651 		 * Log the event. As of now we print out an event.
3652 		 * Do not print the replay failure number, or else
3653 		 * syslog cannot collate the error messages.  Printing
3654 		 * the replay number that failed (or printing to the
3655 		 * console) opens a denial-of-service attack.
3656 		 */
3657 		AH_BUMP_STAT(ahstack, replay_failures);
3658 		ipsec_assocfailure(info.mi_idnum, 0, 0,
3659 		    SL_ERROR | SL_WARN,
3660 		    "Replay failed for AH spi %x, dst_addr %s",
3661 		    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
3662 		counter = DROPPER(ipss, ipds_ah_replay);
3663 		goto ah_in_discard;
3664 	}
3665 
3666 	/*
3667 	 * Remove AH header. We do this by copying everything before
3668 	 * the AH header onto the AH header+ICV.
3669 	 */
3670 	/* overwrite AH with what was preceeding it (IP header) */
3671 	next_hdr = ah->ah_nexthdr;
3672 	ovbcopy(mp->b_rptr, mp->b_rptr + sizeof (ah_t) + align_len,
3673 	    ah_offset);
3674 	mp->b_rptr += sizeof (ah_t) + align_len;
3675 	if (isv4) {
3676 		/* adjust IP header next protocol */
3677 		ipha = (ipha_t *)mp->b_rptr;
3678 		ipha->ipha_protocol = next_hdr;
3679 
3680 		age_bytes = ipha->ipha_length;
3681 
3682 		/* adjust length in IP header */
3683 		ipha->ipha_length -= (sizeof (ah_t) + align_len);
3684 
3685 		/* recalculate checksum */
3686 		ipha->ipha_hdr_checksum = 0;
3687 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
3688 	} else {
3689 		/* adjust IP header next protocol */
3690 		ip6h = (ip6_t *)mp->b_rptr;
3691 		ip6h->ip6_nxt = next_hdr;
3692 
3693 		age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
3694 		    sizeof (ah_t);
3695 
3696 		/* adjust length in IP header */
3697 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) -
3698 		    (sizeof (ah_t) + align_len));
3699 	}
3700 
3701 	/* age SA */
3702 	if (!ah_age_bytes(assoc, age_bytes, B_TRUE)) {
3703 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
3704 		ipsec_assocfailure(info.mi_idnum, 0, 0,
3705 		    SL_ERROR | SL_WARN,
3706 		    "AH Association 0x%x, dst %s had bytes expire.\n",
3707 		    assoc->ipsa_spi, assoc->ipsa_dstaddr,
3708 		    AF_INET, ahstack->ipsecah_netstack);
3709 		AH_BUMP_STAT(ahstack, bytes_expired);
3710 		counter = DROPPER(ipss, ipds_ah_bytes_expire);
3711 		goto ah_in_discard;
3712 	}
3713 
3714 	freeb(hada_mp);
3715 	return (IPSEC_STATUS_SUCCESS);
3716 
3717 ah_in_discard:
3718 	IP_AH_BUMP_STAT(ipss, in_discards);
3719 	freeb(hada_mp);
3720 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter,
3721 	    &ahstack->ah_dropper);
3722 	return (IPSEC_STATUS_FAILED);
3723 }
3724 
3725 /*
3726  * ah_outbound_accelerated_v4:
3727  * Called from ah_outbound_v4() and once it is determined that the
3728  * packet is elligible for hardware acceleration.
3729  *
3730  * We proceed as follows:
3731  * 1. allocate and initialize attributes mblk
3732  * 2. mark IPSEC_OUT to indicate that pkt is accelerated
3733  * 3. insert AH header
3734  */
3735 static ipsec_status_t
3736 ah_outbound_accelerated_v4(mblk_t *ipsec_mp, ipsa_t *assoc)
3737 {
3738 	mblk_t *mp, *new_mp;
3739 	ipsec_out_t *oi;
3740 	uint_t ah_data_sz;	/* ICV length, algorithm dependent */
3741 	uint_t ah_align_sz;	/* ICV length + padding */
3742 	uint32_t v_hlen_tos_len; /* from original IP header */
3743 	ipha_t	*oipha;		/* original IP header */
3744 	ipha_t	*nipha;		/* new IP header */
3745 	uint_t option_length = 0;
3746 	uint_t new_hdr_len;	/* new header length */
3747 	uint_t iphdr_length;
3748 	ah_t *ah_hdr;		/* ptr to AH header */
3749 	netstack_t	*ns;
3750 	ipsec_stack_t	*ipss;
3751 	ipsecah_stack_t	*ahstack;
3752 
3753 	oi = (ipsec_out_t *)ipsec_mp->b_rptr;
3754 	ns = oi->ipsec_out_ns;
3755 	ipss = ns->netstack_ipsec;
3756 	ahstack = ns->netstack_ipsecah;
3757 
3758 	mp = ipsec_mp->b_cont;
3759 
3760 	AH_BUMP_STAT(ahstack, out_accelerated);
3761 
3762 	oipha = (ipha_t *)mp->b_rptr;
3763 	v_hlen_tos_len = ((uint32_t *)oipha)[0];
3764 
3765 	/* mark packet as being accelerated in IPSEC_OUT */
3766 	ASSERT(oi->ipsec_out_accelerated == B_FALSE);
3767 	oi->ipsec_out_accelerated = B_TRUE;
3768 
3769 	/* calculate authentication data length, i.e. ICV + padding */
3770 	ah_data_sz = assoc->ipsa_mac_len;
3771 	ah_align_sz = (ah_data_sz + IPV4_PADDING_ALIGN - 1) &
3772 	    -IPV4_PADDING_ALIGN;
3773 
3774 	/*
3775 	 * Insert pseudo header:
3776 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3777 	 */
3778 
3779 	/* IP + AH + authentication + padding data length */
3780 	new_hdr_len = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz;
3781 	if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
3782 		option_length = oipha->ipha_version_and_hdr_length -
3783 		    (uint8_t)((IP_VERSION << 4) +
3784 		    IP_SIMPLE_HDR_LENGTH_IN_WORDS);
3785 		option_length <<= 2;
3786 		new_hdr_len += option_length;
3787 	}
3788 
3789 	/* allocate pseudo-header mblk */
3790 	if ((new_mp = allocb(new_hdr_len, BPRI_HI)) == NULL) {
3791 		/* IPsec kstats: bump bean counter here */
3792 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3793 		    DROPPER(ipss, ipds_ah_nomem),
3794 		    &ahstack->ah_dropper);
3795 		return (IPSEC_STATUS_FAILED);
3796 	}
3797 
3798 	new_mp->b_cont = mp;
3799 	ipsec_mp->b_cont = new_mp;
3800 	new_mp->b_wptr += new_hdr_len;
3801 
3802 	/* copy original IP header to new header */
3803 	bcopy(mp->b_rptr, new_mp->b_rptr, IP_SIMPLE_HDR_LENGTH +
3804 	    option_length);
3805 
3806 	/* update IP header */
3807 	nipha = (ipha_t *)new_mp->b_rptr;
3808 	nipha->ipha_protocol = IPPROTO_AH;
3809 	iphdr_length = ntohs(nipha->ipha_length);
3810 	iphdr_length += sizeof (ah_t) + ah_align_sz;
3811 	nipha->ipha_length = htons(iphdr_length);
3812 	nipha->ipha_hdr_checksum = 0;
3813 	nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
3814 
3815 	/* skip original IP header in mp */
3816 	mp->b_rptr += IP_SIMPLE_HDR_LENGTH + option_length;
3817 
3818 	/* initialize AH header */
3819 	ah_hdr = (ah_t *)(new_mp->b_rptr + IP_SIMPLE_HDR_LENGTH +
3820 	    option_length);
3821 	ah_hdr->ah_nexthdr = oipha->ipha_protocol;
3822 	if (!ah_finish_up(ah_hdr, NULL, assoc, ah_data_sz, ah_align_sz,
3823 	    ahstack)) {
3824 		/* Only way this fails is if outbound replay counter wraps. */
3825 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3826 		    DROPPER(ipss, ipds_ah_replay),
3827 		    &ahstack->ah_dropper);
3828 		return (IPSEC_STATUS_FAILED);
3829 	}
3830 
3831 	return (IPSEC_STATUS_SUCCESS);
3832 }
3833 
3834 /*
3835  * ah_outbound_accelerated_v6:
3836  *
3837  * Called from ah_outbound_v6() once it is determined that the packet
3838  * is eligible for hardware acceleration.
3839  *
3840  * We proceed as follows:
3841  * 1. allocate and initialize attributes mblk
3842  * 2. mark IPSEC_OUT to indicate that pkt is accelerated
3843  * 3. insert AH header
3844  */
3845 static ipsec_status_t
3846 ah_outbound_accelerated_v6(mblk_t *ipsec_mp, ipsa_t *assoc)
3847 {
3848 	mblk_t *mp, *phdr_mp;
3849 	ipsec_out_t *oi;
3850 	uint_t ah_data_sz;	/* ICV length, algorithm dependent */
3851 	uint_t ah_align_sz;	/* ICV length + padding */
3852 	ip6_t	*oip6h;		/* original IP header */
3853 	ip6_t	*ip6h;		/* new IP header */
3854 	uint_t option_length = 0;
3855 	uint_t hdr_size;
3856 	uint_t ah_offset;
3857 	ah_t *ah_hdr;		/* ptr to AH header */
3858 	netstack_t	*ns;
3859 	ipsec_stack_t	*ipss;
3860 	ipsecah_stack_t	*ahstack;
3861 
3862 	oi = (ipsec_out_t *)ipsec_mp->b_rptr;
3863 	ns = oi->ipsec_out_ns;
3864 	ipss = ns->netstack_ipsec;
3865 	ahstack = ns->netstack_ipsecah;
3866 
3867 	mp = ipsec_mp->b_cont;
3868 
3869 	AH_BUMP_STAT(ahstack, out_accelerated);
3870 
3871 	oip6h = (ip6_t *)mp->b_rptr;
3872 
3873 	/* mark packet as being accelerated in IPSEC_OUT */
3874 	ASSERT(oi->ipsec_out_accelerated == B_FALSE);
3875 	oi->ipsec_out_accelerated = B_TRUE;
3876 
3877 	/* calculate authentication data length, i.e. ICV + padding */
3878 	ah_data_sz = assoc->ipsa_mac_len;
3879 	ah_align_sz = (ah_data_sz + IPV4_PADDING_ALIGN - 1) &
3880 	    -IPV4_PADDING_ALIGN;
3881 
3882 	ASSERT(ah_align_sz >= ah_data_sz);
3883 
3884 	hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
3885 	option_length = hdr_size - IPV6_HDR_LEN;
3886 
3887 	/* This was not included in ipsec_ah_get_hdr_size_v6() */
3888 	hdr_size += (sizeof (ah_t) + ah_align_sz);
3889 
3890 	if ((phdr_mp = allocb(hdr_size, BPRI_HI)) == NULL) {
3891 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3892 		    DROPPER(ipss, ipds_ah_nomem),
3893 		    &ahstack->ah_dropper);
3894 		return (IPSEC_STATUS_FAILED);
3895 	}
3896 	phdr_mp->b_wptr += hdr_size;
3897 
3898 	/*
3899 	 * Form the basic IP header first.  We always assign every bit
3900 	 * of the v6 basic header, so a separate bzero is unneeded.
3901 	 */
3902 	ip6h = (ip6_t *)phdr_mp->b_rptr;
3903 	ip6h->ip6_vcf = oip6h->ip6_vcf;
3904 	ip6h->ip6_hlim = oip6h->ip6_hlim;
3905 	ip6h->ip6_src = oip6h->ip6_src;
3906 	ip6h->ip6_dst = oip6h->ip6_dst;
3907 	/*
3908 	 * Include the size of AH and authentication data.
3909 	 * This is how our recipient would compute the
3910 	 * authentication data. Look at what we do in the
3911 	 * inbound case below.
3912 	 */
3913 	ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) + sizeof (ah_t) +
3914 	    ah_align_sz);
3915 
3916 	/*
3917 	 * Insert pseudo header:
3918 	 * IPSEC_INFO -> [IP6, LLH, ULP] =>
3919 	 *	IPSEC_INFO -> [IP, LLH, AH, ICV] -> ULP
3920 	 */
3921 
3922 	if (option_length == 0) {
3923 		/* Form the AH header */
3924 		ip6h->ip6_nxt = IPPROTO_AH;
3925 		((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
3926 		ah_offset = IPV6_HDR_LEN;
3927 	} else {
3928 		ip6h->ip6_nxt = oip6h->ip6_nxt;
3929 		/* option_length does not include the AH header's size */
3930 		ah_offset = ah_fix_phdr_v6(ip6h, oip6h, B_TRUE, B_FALSE);
3931 		if (ah_offset == 0) {
3932 			freemsg(phdr_mp);
3933 			ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3934 			    DROPPER(ipss, ipds_ah_bad_v6_hdrs),
3935 			    &ahstack->ah_dropper);
3936 			return (IPSEC_STATUS_FAILED);
3937 		}
3938 	}
3939 
3940 	phdr_mp->b_cont = mp;
3941 	ipsec_mp->b_cont = phdr_mp;
3942 
3943 	/* skip original IP header in mp */
3944 	mp->b_rptr += IPV6_HDR_LEN + option_length;
3945 
3946 	/* initialize AH header */
3947 	ah_hdr = (ah_t *)(phdr_mp->b_rptr + IPV6_HDR_LEN + option_length);
3948 	ah_hdr->ah_nexthdr = oip6h->ip6_nxt;
3949 
3950 	if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)), NULL,
3951 	    assoc, ah_data_sz, ah_align_sz, ahstack)) {
3952 		/* Only way this fails is if outbound replay counter wraps. */
3953 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3954 		    DROPPER(ipss, ipds_ah_replay),
3955 		    &ahstack->ah_dropper);
3956 		return (IPSEC_STATUS_FAILED);
3957 	}
3958 
3959 	return (IPSEC_STATUS_SUCCESS);
3960 }
3961 
3962 /*
3963  * Invoked after processing of an inbound packet by the
3964  * kernel crypto framework. Called by ah_submit_req() for a sync request,
3965  * or by the kcf callback for an async request.
3966  * Returns IPSEC_STATUS_SUCCESS on success, IPSEC_STATUS_FAILED on failure.
3967  * On failure, the mblk chain ipsec_in is freed by this function.
3968  */
3969 static ipsec_status_t
3970 ah_auth_in_done(mblk_t *ipsec_in)
3971 {
3972 	mblk_t *phdr_mp;
3973 	ipha_t *ipha;
3974 	uint_t ah_offset = 0;
3975 	mblk_t *mp;
3976 	int align_len, newpos;
3977 	ah_t *ah;
3978 	uint32_t length;
3979 	uint32_t *dest32;
3980 	uint8_t *dest;
3981 	ipsec_in_t *ii;
3982 	boolean_t isv4;
3983 	ip6_t *ip6h;
3984 	uint_t icv_len;
3985 	ipsa_t *assoc;
3986 	kstat_named_t *counter;
3987 	netstack_t	*ns;
3988 	ipsecah_stack_t	*ahstack;
3989 	ipsec_stack_t	*ipss;
3990 
3991 	ii = (ipsec_in_t *)ipsec_in->b_rptr;
3992 	ns = ii->ipsec_in_ns;
3993 	ahstack = ns->netstack_ipsecah;
3994 	ipss = ns->netstack_ipsec;
3995 
3996 	isv4 = ii->ipsec_in_v4;
3997 	assoc = ii->ipsec_in_ah_sa;
3998 	icv_len = (uint_t)ii->ipsec_in_crypto_mac.cd_raw.iov_len;
3999 
4000 	phdr_mp = ipsec_in->b_cont;
4001 	if (phdr_mp == NULL) {
4002 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
4003 		    DROPPER(ipss, ipds_ah_nomem),
4004 		    &ahstack->ah_dropper);
4005 		return (IPSEC_STATUS_FAILED);
4006 	}
4007 
4008 	mp = phdr_mp->b_cont;
4009 	if (mp == NULL) {
4010 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
4011 		    DROPPER(ipss, ipds_ah_nomem),
4012 		    &ahstack->ah_dropper);
4013 		return (IPSEC_STATUS_FAILED);
4014 	}
4015 	mp->b_rptr -= ii->ipsec_in_skip_len;
4016 
4017 	if (isv4) {
4018 		ipha = (ipha_t *)mp->b_rptr;
4019 		ah_offset = ipha->ipha_version_and_hdr_length -
4020 		    (uint8_t)((IP_VERSION << 4));
4021 		ah_offset <<= 2;
4022 		align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
4023 		    IPV4_PADDING_ALIGN);
4024 	} else {
4025 		ip6h = (ip6_t *)mp->b_rptr;
4026 		ah_offset = ipsec_ah_get_hdr_size_v6(mp, B_TRUE);
4027 		ASSERT((mp->b_wptr - mp->b_rptr) >= ah_offset);
4028 		align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
4029 		    IPV6_PADDING_ALIGN);
4030 	}
4031 
4032 	ah = (ah_t *)(mp->b_rptr + ah_offset);
4033 	newpos = sizeof (ah_t) + align_len;
4034 
4035 	/*
4036 	 * We get here only when authentication passed.
4037 	 */
4038 
4039 	ah3dbg(ahstack, ("AH succeeded, checking replay\n"));
4040 	AH_BUMP_STAT(ahstack, good_auth);
4041 
4042 	if (!sadb_replay_check(assoc, ah->ah_replay)) {
4043 		int af;
4044 		void *addr;
4045 
4046 		if (isv4) {
4047 			addr = &ipha->ipha_dst;
4048 			af = AF_INET;
4049 		} else {
4050 			addr = &ip6h->ip6_dst;
4051 			af = AF_INET6;
4052 		}
4053 
4054 		/*
4055 		 * Log the event. As of now we print out an event.
4056 		 * Do not print the replay failure number, or else
4057 		 * syslog cannot collate the error messages.  Printing
4058 		 * the replay number that failed (or printing to the
4059 		 * console) opens a denial-of-service attack.
4060 		 */
4061 		AH_BUMP_STAT(ahstack, replay_failures);
4062 		ipsec_assocfailure(info.mi_idnum, 0, 0,
4063 		    SL_ERROR | SL_WARN,
4064 		    "Replay failed for AH spi %x, dst_addr %s",
4065 		    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
4066 		counter = DROPPER(ipss, ipds_ah_replay);
4067 		goto ah_in_discard;
4068 	}
4069 
4070 	/*
4071 	 * We need to remove the AH header from the original
4072 	 * datagram. Best way to do this is to move the pre-AH headers
4073 	 * forward in the (relatively simple) IPv4 case.  In IPv6, it's
4074 	 * a bit more complicated because of IPv6's next-header chaining,
4075 	 * but it's doable.
4076 	 */
4077 	if (isv4) {
4078 		/*
4079 		 * Assign the right protocol, adjust the length as we
4080 		 * are removing the AH header and adjust the checksum to
4081 		 * account for the protocol and length.
4082 		 */
4083 		length = ntohs(ipha->ipha_length);
4084 		if (!ah_age_bytes(assoc, length, B_TRUE)) {
4085 			/* The ipsa has hit hard expiration, LOG and AUDIT. */
4086 			ipsec_assocfailure(info.mi_idnum, 0, 0,
4087 			    SL_ERROR | SL_WARN,
4088 			    "AH Association 0x%x, dst %s had bytes expire.\n",
4089 			    assoc->ipsa_spi, assoc->ipsa_dstaddr,
4090 			    AF_INET, ahstack->ipsecah_netstack);
4091 			AH_BUMP_STAT(ahstack, bytes_expired);
4092 			counter = DROPPER(ipss, ipds_ah_bytes_expire);
4093 			goto ah_in_discard;
4094 		}
4095 		ipha->ipha_protocol = ah->ah_nexthdr;
4096 		length -= newpos;
4097 
4098 		ipha->ipha_length = htons((uint16_t)length);
4099 		ipha->ipha_hdr_checksum = 0;
4100 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
4101 	} else {
4102 		uchar_t *whereptr;
4103 		int hdrlen;
4104 		uint8_t *nexthdr;
4105 		ip6_hbh_t *hbhhdr;
4106 		ip6_dest_t *dsthdr;
4107 		ip6_rthdr0_t *rthdr;
4108 
4109 		/*
4110 		 * Make phdr_mp hold until the AH header and make
4111 		 * mp hold everything past AH header.
4112 		 */
4113 		length = ntohs(ip6h->ip6_plen);
4114 		if (!ah_age_bytes(assoc, length + sizeof (ip6_t), B_TRUE)) {
4115 			/* The ipsa has hit hard expiration, LOG and AUDIT. */
4116 			ipsec_assocfailure(info.mi_idnum, 0, 0,
4117 			    SL_ERROR | SL_WARN,
4118 			    "AH Association 0x%x, dst %s had bytes "
4119 			    "expire.\n", assoc->ipsa_spi, &ip6h->ip6_dst,
4120 			    AF_INET6, ahstack->ipsecah_netstack);
4121 			AH_BUMP_STAT(ahstack, bytes_expired);
4122 			counter = DROPPER(ipss, ipds_ah_bytes_expire);
4123 			goto ah_in_discard;
4124 		}
4125 
4126 		/*
4127 		 * Update the next header field of the header preceding
4128 		 * AH with the next header field of AH. Start with the
4129 		 * IPv6 header and proceed with the extension headers
4130 		 * until we find what we're looking for.
4131 		 */
4132 		nexthdr = &ip6h->ip6_nxt;
4133 		whereptr =  (uchar_t *)ip6h;
4134 		hdrlen = sizeof (ip6_t);
4135 
4136 		while (*nexthdr != IPPROTO_AH) {
4137 			whereptr += hdrlen;
4138 			/* Assume IP has already stripped it */
4139 			ASSERT(*nexthdr != IPPROTO_FRAGMENT &&
4140 			    *nexthdr != IPPROTO_RAW);
4141 			switch (*nexthdr) {
4142 			case IPPROTO_HOPOPTS:
4143 				hbhhdr = (ip6_hbh_t *)whereptr;
4144 				nexthdr = &hbhhdr->ip6h_nxt;
4145 				hdrlen = 8 * (hbhhdr->ip6h_len + 1);
4146 				break;
4147 			case IPPROTO_DSTOPTS:
4148 				dsthdr = (ip6_dest_t *)whereptr;
4149 				nexthdr = &dsthdr->ip6d_nxt;
4150 				hdrlen = 8 * (dsthdr->ip6d_len + 1);
4151 				break;
4152 			case IPPROTO_ROUTING:
4153 				rthdr = (ip6_rthdr0_t *)whereptr;
4154 				nexthdr = &rthdr->ip6r0_nxt;
4155 				hdrlen = 8 * (rthdr->ip6r0_len + 1);
4156 				break;
4157 			}
4158 		}
4159 		*nexthdr = ah->ah_nexthdr;
4160 		length -= newpos;
4161 		ip6h->ip6_plen = htons((uint16_t)length);
4162 	}
4163 
4164 	/* Now that we've fixed the IP header, move it forward. */
4165 	mp->b_rptr += newpos;
4166 	if (IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) {
4167 		dest32 = (uint32_t *)(mp->b_rptr + ah_offset);
4168 		while (--dest32 >= (uint32_t *)mp->b_rptr)
4169 			*dest32 = *(dest32 - (newpos >> 2));
4170 	} else {
4171 		dest = mp->b_rptr + ah_offset;
4172 		while (--dest >= mp->b_rptr)
4173 			*dest = *(dest - newpos);
4174 	}
4175 	freeb(phdr_mp);
4176 	ipsec_in->b_cont = mp;
4177 
4178 	if (is_system_labeled()) {
4179 		/*
4180 		 * inherit the label by setting it in the new ip header
4181 		 */
4182 		mblk_setcred(mp, DB_CRED(mp));
4183 	}
4184 	return (IPSEC_STATUS_SUCCESS);
4185 
4186 ah_in_discard:
4187 	IP_AH_BUMP_STAT(ipss, in_discards);
4188 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter,
4189 	    &ahstack->ah_dropper);
4190 	return (IPSEC_STATUS_FAILED);
4191 }
4192 
4193 /*
4194  * Invoked after processing of an outbound packet by the
4195  * kernel crypto framework, either by ah_submit_req() for a request
4196  * executed syncrhonously, or by the KEF callback for a request
4197  * executed asynchronously.
4198  */
4199 static ipsec_status_t
4200 ah_auth_out_done(mblk_t *ipsec_out)
4201 {
4202 	mblk_t *phdr_mp;
4203 	mblk_t *mp;
4204 	int align_len;
4205 	uint32_t hdrs_length;
4206 	uchar_t *ptr;
4207 	uint32_t length;
4208 	boolean_t isv4;
4209 	ipsec_out_t *io;
4210 	size_t icv_len;
4211 	netstack_t	*ns;
4212 	ipsec_stack_t	*ipss;
4213 	ipsecah_stack_t	*ahstack;
4214 
4215 	io = (ipsec_out_t *)ipsec_out->b_rptr;
4216 	ns = io->ipsec_out_ns;
4217 	ipss = ns->netstack_ipsec;
4218 	ahstack = ns->netstack_ipsecah;
4219 
4220 	isv4 = io->ipsec_out_v4;
4221 	icv_len = io->ipsec_out_crypto_mac.cd_raw.iov_len;
4222 
4223 	phdr_mp = ipsec_out->b_cont;
4224 	if (phdr_mp == NULL) {
4225 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
4226 		    DROPPER(ipss, ipds_ah_nomem),
4227 		    &ahstack->ah_dropper);
4228 		return (IPSEC_STATUS_FAILED);
4229 	}
4230 
4231 	mp = phdr_mp->b_cont;
4232 	if (mp == NULL) {
4233 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
4234 		    DROPPER(ipss, ipds_ah_nomem),
4235 		    &ahstack->ah_dropper);
4236 		return (IPSEC_STATUS_FAILED);
4237 	}
4238 	mp->b_rptr -= io->ipsec_out_skip_len;
4239 
4240 	if (isv4) {
4241 		ipha_t *ipha;
4242 		ipha_t *nipha;
4243 
4244 		ipha = (ipha_t *)mp->b_rptr;
4245 		hdrs_length = ipha->ipha_version_and_hdr_length -
4246 		    (uint8_t)((IP_VERSION << 4));
4247 		hdrs_length <<= 2;
4248 		align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
4249 		    IPV4_PADDING_ALIGN);
4250 		/*
4251 		 * phdr_mp must have the right amount of space for the
4252 		 * combined IP and AH header. Copy the IP header and
4253 		 * the ack_data onto AH. Note that the AH header was
4254 		 * already formed before the ICV calculation and hence
4255 		 * you don't have to copy it here.
4256 		 */
4257 		bcopy(mp->b_rptr, phdr_mp->b_rptr, hdrs_length);
4258 
4259 		ptr = phdr_mp->b_rptr + hdrs_length + sizeof (ah_t);
4260 		bcopy(phdr_mp->b_wptr, ptr, icv_len);
4261 
4262 		/*
4263 		 * Compute the new header checksum as we are assigning
4264 		 * IPPROTO_AH and adjusting the length here.
4265 		 */
4266 		nipha = (ipha_t *)phdr_mp->b_rptr;
4267 
4268 		nipha->ipha_protocol = IPPROTO_AH;
4269 		length = ntohs(nipha->ipha_length);
4270 		length += (sizeof (ah_t) + align_len);
4271 		nipha->ipha_length = htons((uint16_t)length);
4272 		nipha->ipha_hdr_checksum = 0;
4273 		nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
4274 	} else {
4275 		ip6_t *ip6h;
4276 		ip6_t *nip6h;
4277 		uint_t ah_offset;
4278 
4279 		ip6h = (ip6_t *)mp->b_rptr;
4280 		nip6h = (ip6_t *)phdr_mp->b_rptr;
4281 		align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
4282 		    IPV6_PADDING_ALIGN);
4283 		/*
4284 		 * phdr_mp must have the right amount of space for the
4285 		 * combined IP and AH header. Copy the IP header with
4286 		 * options into the pseudo header. When we constructed
4287 		 * a pseudo header, we did not copy some of the mutable
4288 		 * fields. We do it now by calling ah_fix_phdr_v6()
4289 		 * with the last argument B_TRUE. It returns the
4290 		 * ah_offset into the pseudo header.
4291 		 */
4292 
4293 		bcopy(ip6h, nip6h, IPV6_HDR_LEN);
4294 		ah_offset = ah_fix_phdr_v6(nip6h, ip6h, B_TRUE, B_TRUE);
4295 		ASSERT(ah_offset != 0);
4296 		/*
4297 		 * phdr_mp can hold exactly the whole IP header with options
4298 		 * plus the AH header also. Thus subtracting the AH header's
4299 		 * size should give exactly how much of the original header
4300 		 * should be skipped.
4301 		 */
4302 		hdrs_length = (phdr_mp->b_wptr - phdr_mp->b_rptr) -
4303 		    sizeof (ah_t) - icv_len;
4304 		bcopy(phdr_mp->b_wptr, ((uint8_t *)nip6h + ah_offset +
4305 		    sizeof (ah_t)), icv_len);
4306 		length = ntohs(nip6h->ip6_plen);
4307 		length += (sizeof (ah_t) + align_len);
4308 		nip6h->ip6_plen = htons((uint16_t)length);
4309 	}
4310 
4311 	if (is_system_labeled()) {
4312 		/*
4313 		 * inherit the label by setting it in the new ip header
4314 		 */
4315 		mblk_setcred(phdr_mp, DB_CRED(mp));
4316 	}
4317 
4318 	/* Skip the original IP header */
4319 	mp->b_rptr += hdrs_length;
4320 	if (mp->b_rptr == mp->b_wptr) {
4321 		phdr_mp->b_cont = mp->b_cont;
4322 		freeb(mp);
4323 	}
4324 
4325 	return (IPSEC_STATUS_SUCCESS);
4326 }
4327 
4328 /*
4329  * Wrapper to allow IP to trigger an AH association failure message
4330  * during SA inbound selection.
4331  */
4332 void
4333 ipsecah_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
4334     uint32_t spi, void *addr, int af, ipsecah_stack_t *ahstack)
4335 {
4336 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
4337 
4338 	if (ahstack->ipsecah_log_unknown_spi) {
4339 		ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
4340 		    addr, af, ahstack->ipsecah_netstack);
4341 	}
4342 
4343 	ip_drop_packet(mp, B_TRUE, NULL, NULL,
4344 	    DROPPER(ipss, ipds_ah_no_sa),
4345 	    &ahstack->ah_dropper);
4346 }
4347 
4348 /*
4349  * Initialize the AH input and output processing functions.
4350  */
4351 void
4352 ipsecah_init_funcs(ipsa_t *sa)
4353 {
4354 	if (sa->ipsa_output_func == NULL)
4355 		sa->ipsa_output_func = ah_outbound;
4356 	if (sa->ipsa_input_func == NULL)
4357 		sa->ipsa_input_func = ah_inbound;
4358 }
4359