xref: /titanic_44/usr/src/uts/common/inet/ip/ipsecah.c (revision 458cf4d67cff5ff99a68a866b7657f1b1d6fe61c)
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 &
1119 	    ~(SADB_SAFLAGS_NOREPLAY | 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, diagnostic, ahstack->ah_pfkey_q, ah_add_sa,
1193 	    ahstack->ipsecah_netstack));
1194 }
1195 
1196 /*
1197  * Delete a security association.  This is REALLY likely to be code common to
1198  * both AH and ESP.  Find the association, then unlink it.
1199  */
1200 static int
1201 ah_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
1202     ipsecah_stack_t *ahstack)
1203 {
1204 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1205 	sadb_address_t *dstext =
1206 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1207 	sadb_address_t *srcext =
1208 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
1209 	struct sockaddr_in *sin;
1210 
1211 	if (assoc == NULL) {
1212 		if (dstext != NULL)
1213 			sin = (struct sockaddr_in *)(dstext + 1);
1214 		else if (srcext != NULL)
1215 			sin = (struct sockaddr_in *)(srcext + 1);
1216 		else {
1217 			*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1218 			return (EINVAL);
1219 		}
1220 		return (sadb_purge_sa(mp, ksi,
1221 		    (sin->sin_family == AF_INET6) ? &ahstack->ah_sadb.s_v6 :
1222 		    &ahstack->ah_sadb.s_v4,
1223 		    ahstack->ah_pfkey_q, ahstack->ah_sadb.s_ip_q));
1224 	}
1225 
1226 	return (sadb_del_sa(mp, ksi, &ahstack->ah_sadb, diagnostic,
1227 	    ahstack->ah_pfkey_q));
1228 }
1229 
1230 /*
1231  * Convert the entire contents of all of AH's SA tables into PF_KEY SADB_DUMP
1232  * messages.
1233  */
1234 static void
1235 ah_dump(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1236 {
1237 	int error;
1238 	sadb_msg_t *samsg;
1239 
1240 	/*
1241 	 * Dump each fanout, bailing if error is non-zero.
1242 	 */
1243 
1244 	error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi->ks_in_serial,
1245 	    &ahstack->ah_sadb.s_v4);
1246 	if (error != 0)
1247 		goto bail;
1248 
1249 	error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi->ks_in_serial,
1250 	    &ahstack->ah_sadb.s_v6);
1251 bail:
1252 	ASSERT(mp->b_cont != NULL);
1253 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
1254 	samsg->sadb_msg_errno = (uint8_t)error;
1255 	sadb_pfkey_echo(ahstack->ah_pfkey_q, mp,
1256 	    (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL);
1257 }
1258 
1259 /*
1260  * First-cut reality check for an inbound PF_KEY message.
1261  */
1262 static boolean_t
1263 ah_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
1264     ipsecah_stack_t *ahstack)
1265 {
1266 	int diagnostic;
1267 
1268 	if (mp->b_cont == NULL) {
1269 		freemsg(mp);
1270 		return (B_TRUE);
1271 	}
1272 
1273 	if (ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL) {
1274 		diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT;
1275 		goto badmsg;
1276 	}
1277 	if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
1278 		diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
1279 		goto badmsg;
1280 	}
1281 	if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
1282 	    ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
1283 		diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
1284 		goto badmsg;
1285 	}
1286 	return (B_FALSE);	/* False ==> no failures */
1287 
1288 badmsg:
1289 	sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1290 	    diagnostic, ksi->ks_in_serial);
1291 	return (B_TRUE);	/* True ==> failures */
1292 }
1293 
1294 /*
1295  * AH parsing of PF_KEY messages.  Keysock did most of the really silly
1296  * error cases.  What I receive is a fully-formed, syntactically legal
1297  * PF_KEY message.  I then need to check semantics...
1298  *
1299  * This code may become common to AH and ESP.  Stay tuned.
1300  *
1301  * I also make the assumption that db_ref's are cool.  If this assumption
1302  * is wrong, this means that someone other than keysock or me has been
1303  * mucking with PF_KEY messages.
1304  */
1305 static void
1306 ah_parse_pfkey(mblk_t *mp, ipsecah_stack_t *ahstack)
1307 {
1308 	mblk_t *msg = mp->b_cont;
1309 	sadb_msg_t *samsg;
1310 	keysock_in_t *ksi;
1311 	int error;
1312 	int diagnostic = SADB_X_DIAGNOSTIC_NONE;
1313 
1314 	ASSERT(msg != NULL);
1315 
1316 	samsg = (sadb_msg_t *)msg->b_rptr;
1317 	ksi = (keysock_in_t *)mp->b_rptr;
1318 
1319 	/*
1320 	 * If applicable, convert unspecified AF_INET6 to unspecified
1321 	 * AF_INET.
1322 	 */
1323 	if (!sadb_addrfix(ksi, ahstack->ah_pfkey_q, mp,
1324 	    ahstack->ipsecah_netstack) ||
1325 	    ah_pfkey_reality_failures(mp, ksi, ahstack)) {
1326 		return;
1327 	}
1328 
1329 	switch (samsg->sadb_msg_type) {
1330 	case SADB_ADD:
1331 		error = ah_add_sa(mp, ksi, &diagnostic,
1332 		    ahstack->ipsecah_netstack);
1333 		if (error != 0) {
1334 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1335 			    diagnostic, ksi->ks_in_serial);
1336 		}
1337 		/* else ah_add_sa() took care of things. */
1338 		break;
1339 	case SADB_DELETE:
1340 		error = ah_del_sa(mp, ksi, &diagnostic, ahstack);
1341 		if (error != 0) {
1342 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1343 			    diagnostic, ksi->ks_in_serial);
1344 		}
1345 		/* Else ah_del_sa() took care of things. */
1346 		break;
1347 	case SADB_GET:
1348 		error = sadb_get_sa(mp, ksi, &ahstack->ah_sadb, &diagnostic,
1349 		    ahstack->ah_pfkey_q);
1350 		if (error != 0) {
1351 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1352 			    diagnostic, ksi->ks_in_serial);
1353 		}
1354 		/* Else sadb_get_sa() took care of things. */
1355 		break;
1356 	case SADB_FLUSH:
1357 		sadbp_flush(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
1358 		sadb_pfkey_echo(ahstack->ah_pfkey_q, mp, samsg, ksi, NULL);
1359 		break;
1360 	case SADB_REGISTER:
1361 		/*
1362 		 * Hmmm, let's do it!  Check for extensions (there should
1363 		 * be none), extract the fields, call ah_register_out(),
1364 		 * then either free or report an error.
1365 		 *
1366 		 * Keysock takes care of the PF_KEY bookkeeping for this.
1367 		 */
1368 		if (ah_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
1369 		    ksi->ks_in_serial, ahstack)) {
1370 			freemsg(mp);
1371 		} else {
1372 			/*
1373 			 * Only way this path hits is if there is a memory
1374 			 * failure.  It will not return B_FALSE because of
1375 			 * lack of ah_pfkey_q if I am in wput().
1376 			 */
1377 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM,
1378 			    diagnostic, ksi->ks_in_serial);
1379 		}
1380 		break;
1381 	case SADB_UPDATE:
1382 		/*
1383 		 * Find a larval, if not there, find a full one and get
1384 		 * strict.
1385 		 */
1386 		error = ah_update_sa(mp, ksi, &diagnostic, ahstack);
1387 		if (error != 0) {
1388 			sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1389 			    diagnostic, ksi->ks_in_serial);
1390 		}
1391 		/* else ah_update_sa() took care of things. */
1392 		break;
1393 	case SADB_GETSPI:
1394 		/*
1395 		 * Reserve a new larval entry.
1396 		 */
1397 		ah_getspi(mp, ksi, ahstack);
1398 		break;
1399 	case SADB_ACQUIRE:
1400 		/*
1401 		 * Find larval and/or ACQUIRE record and kill it (them), I'm
1402 		 * most likely an error.  Inbound ACQUIRE messages should only
1403 		 * have the base header.
1404 		 */
1405 		sadb_in_acquire(samsg, &ahstack->ah_sadb, ahstack->ah_pfkey_q,
1406 		    ahstack->ipsecah_netstack);
1407 		freemsg(mp);
1408 		break;
1409 	case SADB_DUMP:
1410 		/*
1411 		 * Dump all entries.
1412 		 */
1413 		ah_dump(mp, ksi, ahstack);
1414 		/* ah_dump will take care of the return message, etc. */
1415 		break;
1416 	case SADB_EXPIRE:
1417 		/* Should never reach me. */
1418 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EOPNOTSUPP,
1419 		    diagnostic, ksi->ks_in_serial);
1420 		break;
1421 	default:
1422 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1423 		    SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
1424 		break;
1425 	}
1426 }
1427 
1428 /*
1429  * Handle case where PF_KEY says it can't find a keysock for one of my
1430  * ACQUIRE messages.
1431  */
1432 static void
1433 ah_keysock_no_socket(mblk_t *mp, ipsecah_stack_t *ahstack)
1434 {
1435 	sadb_msg_t *samsg;
1436 	keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
1437 
1438 	if (mp->b_cont == NULL) {
1439 		freemsg(mp);
1440 		return;
1441 	}
1442 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
1443 
1444 	/*
1445 	 * If keysock can't find any registered, delete the acquire record
1446 	 * immediately, and handle errors.
1447 	 */
1448 	if (samsg->sadb_msg_type == SADB_ACQUIRE) {
1449 		samsg->sadb_msg_errno = kse->ks_err_errno;
1450 		samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
1451 		/*
1452 		 * Use the write-side of the ah_pfkey_q, in case there is
1453 		 * no ahstack->ah_sadb.s_ip_q.
1454 		 */
1455 		sadb_in_acquire(samsg, &ahstack->ah_sadb,
1456 		    WR(ahstack->ah_pfkey_q), ahstack->ipsecah_netstack);
1457 	}
1458 
1459 	freemsg(mp);
1460 }
1461 
1462 /*
1463  * AH module write put routine.
1464  */
1465 static void
1466 ipsecah_wput(queue_t *q, mblk_t *mp)
1467 {
1468 	ipsec_info_t *ii;
1469 	struct iocblk *iocp;
1470 	ipsecah_stack_t	*ahstack = (ipsecah_stack_t *)q->q_ptr;
1471 
1472 	ah3dbg(ahstack, ("In ah_wput().\n"));
1473 
1474 	/* NOTE:  Each case must take care of freeing or passing mp. */
1475 	switch (mp->b_datap->db_type) {
1476 	case M_CTL:
1477 		if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
1478 			/* Not big enough message. */
1479 			freemsg(mp);
1480 			break;
1481 		}
1482 		ii = (ipsec_info_t *)mp->b_rptr;
1483 
1484 		switch (ii->ipsec_info_type) {
1485 		case KEYSOCK_OUT_ERR:
1486 			ah1dbg(ahstack, ("Got KEYSOCK_OUT_ERR message.\n"));
1487 			ah_keysock_no_socket(mp, ahstack);
1488 			break;
1489 		case KEYSOCK_IN:
1490 			AH_BUMP_STAT(ahstack, keysock_in);
1491 			ah3dbg(ahstack, ("Got KEYSOCK_IN message.\n"));
1492 
1493 			/* Parse the message. */
1494 			ah_parse_pfkey(mp, ahstack);
1495 			break;
1496 		case KEYSOCK_HELLO:
1497 			sadb_keysock_hello(&ahstack->ah_pfkey_q, q, mp,
1498 			    ah_ager, (void *)ahstack, &ahstack->ah_event,
1499 			    SADB_SATYPE_AH);
1500 			break;
1501 		default:
1502 			ah1dbg(ahstack, ("Got M_CTL from above of 0x%x.\n",
1503 			    ii->ipsec_info_type));
1504 			freemsg(mp);
1505 			break;
1506 		}
1507 		break;
1508 	case M_IOCTL:
1509 		iocp = (struct iocblk *)mp->b_rptr;
1510 		switch (iocp->ioc_cmd) {
1511 		case ND_SET:
1512 		case ND_GET:
1513 			if (nd_getset(q, ahstack->ipsecah_g_nd, mp)) {
1514 				qreply(q, mp);
1515 				return;
1516 			} else {
1517 				iocp->ioc_error = ENOENT;
1518 			}
1519 			/* FALLTHRU */
1520 		default:
1521 			/* We really don't support any other ioctls, do we? */
1522 
1523 			/* Return EINVAL */
1524 			if (iocp->ioc_error != ENOENT)
1525 				iocp->ioc_error = EINVAL;
1526 			iocp->ioc_count = 0;
1527 			mp->b_datap->db_type = M_IOCACK;
1528 			qreply(q, mp);
1529 			return;
1530 		}
1531 	default:
1532 		ah3dbg(ahstack,
1533 		    ("Got default message, type %d, passing to IP.\n",
1534 		    mp->b_datap->db_type));
1535 		putnext(q, mp);
1536 	}
1537 }
1538 
1539 /*
1540  * Updating use times can be tricky business if the ipsa_haspeer flag is
1541  * set.  This function is called once in an SA's lifetime.
1542  *
1543  * Caller has to REFRELE "assoc" which is passed in.  This function has
1544  * to REFRELE any peer SA that is obtained.
1545  */
1546 static void
1547 ah_set_usetime(ipsa_t *assoc, boolean_t inbound)
1548 {
1549 	ipsa_t *inassoc, *outassoc;
1550 	isaf_t *bucket;
1551 	sadb_t *sp;
1552 	int outhash;
1553 	boolean_t isv6;
1554 	netstack_t	*ns = assoc->ipsa_netstack;
1555 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
1556 
1557 	/* No peer?  No problem! */
1558 	if (!assoc->ipsa_haspeer) {
1559 		sadb_set_usetime(assoc);
1560 		return;
1561 	}
1562 
1563 	/*
1564 	 * Otherwise, we want to grab both the original assoc and its peer.
1565 	 * There might be a race for this, but if it's a real race, the times
1566 	 * will be out-of-synch by at most a second, and since our time
1567 	 * granularity is a second, this won't be a problem.
1568 	 *
1569 	 * If we need tight synchronization on the peer SA, then we need to
1570 	 * reconsider.
1571 	 */
1572 
1573 	/* Use address family to select IPv6/IPv4 */
1574 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
1575 	if (isv6) {
1576 		sp = &ahstack->ah_sadb.s_v6;
1577 	} else {
1578 		sp = &ahstack->ah_sadb.s_v4;
1579 		ASSERT(assoc->ipsa_addrfam == AF_INET);
1580 	}
1581 	if (inbound) {
1582 		inassoc = assoc;
1583 		if (isv6)
1584 			outhash = OUTBOUND_HASH_V6(sp,
1585 			    *((in6_addr_t *)&inassoc->ipsa_dstaddr));
1586 		else
1587 			outhash = OUTBOUND_HASH_V4(sp,
1588 			    *((ipaddr_t *)&inassoc->ipsa_dstaddr));
1589 		bucket = &sp->sdb_of[outhash];
1590 
1591 		mutex_enter(&bucket->isaf_lock);
1592 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
1593 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
1594 		    inassoc->ipsa_addrfam);
1595 		mutex_exit(&bucket->isaf_lock);
1596 		if (outassoc == NULL) {
1597 			/* Q: Do we wish to set haspeer == B_FALSE? */
1598 			ah0dbg(("ah_set_usetime: "
1599 			    "can't find peer for inbound.\n"));
1600 			sadb_set_usetime(inassoc);
1601 			return;
1602 		}
1603 	} else {
1604 		outassoc = assoc;
1605 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
1606 		mutex_enter(&bucket->isaf_lock);
1607 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
1608 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
1609 		    outassoc->ipsa_addrfam);
1610 		mutex_exit(&bucket->isaf_lock);
1611 		if (inassoc == NULL) {
1612 			/* Q: Do we wish to set haspeer == B_FALSE? */
1613 			ah0dbg(("ah_set_usetime: "
1614 			    "can't find peer for outbound.\n"));
1615 			sadb_set_usetime(outassoc);
1616 			return;
1617 		}
1618 	}
1619 
1620 	/* Update usetime on both. */
1621 	sadb_set_usetime(inassoc);
1622 	sadb_set_usetime(outassoc);
1623 
1624 	/*
1625 	 * REFRELE any peer SA.
1626 	 *
1627 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1628 	 * them in { }.
1629 	 */
1630 	if (inbound) {
1631 		IPSA_REFRELE(outassoc);
1632 	} else {
1633 		IPSA_REFRELE(inassoc);
1634 	}
1635 }
1636 
1637 /*
1638  * Add a number of bytes to what the SA has protected so far.  Return
1639  * B_TRUE if the SA can still protect that many bytes.
1640  *
1641  * Caller must REFRELE the passed-in assoc.  This function must REFRELE
1642  * any obtained peer SA.
1643  */
1644 static boolean_t
1645 ah_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
1646 {
1647 	ipsa_t *inassoc, *outassoc;
1648 	isaf_t *bucket;
1649 	boolean_t inrc, outrc, isv6;
1650 	sadb_t *sp;
1651 	int outhash;
1652 	netstack_t	*ns = assoc->ipsa_netstack;
1653 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
1654 
1655 	/* No peer?  No problem! */
1656 	if (!assoc->ipsa_haspeer) {
1657 		return (sadb_age_bytes(ahstack->ah_pfkey_q, assoc, bytes,
1658 		    B_TRUE));
1659 	}
1660 
1661 	/*
1662 	 * Otherwise, we want to grab both the original assoc and its peer.
1663 	 * There might be a race for this, but if it's a real race, two
1664 	 * expire messages may occur.  We limit this by only sending the
1665 	 * expire message on one of the peers, we'll pick the inbound
1666 	 * arbitrarily.
1667 	 *
1668 	 * If we need tight synchronization on the peer SA, then we need to
1669 	 * reconsider.
1670 	 */
1671 
1672 	/* Pick v4/v6 bucket based on addrfam. */
1673 	isv6 = (assoc->ipsa_addrfam == AF_INET6);
1674 	if (isv6) {
1675 		sp = &ahstack->ah_sadb.s_v6;
1676 	} else {
1677 		sp = &ahstack->ah_sadb.s_v4;
1678 		ASSERT(assoc->ipsa_addrfam == AF_INET);
1679 	}
1680 	if (inbound) {
1681 		inassoc = assoc;
1682 		if (isv6)
1683 			outhash = OUTBOUND_HASH_V6(sp,
1684 			    *((in6_addr_t *)&inassoc->ipsa_dstaddr));
1685 		else
1686 			outhash = OUTBOUND_HASH_V4(sp,
1687 			    *((ipaddr_t *)&inassoc->ipsa_dstaddr));
1688 		bucket = &sp->sdb_of[outhash];
1689 		mutex_enter(&bucket->isaf_lock);
1690 		outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
1691 		    inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
1692 		    inassoc->ipsa_addrfam);
1693 		mutex_exit(&bucket->isaf_lock);
1694 		if (outassoc == NULL) {
1695 			/* Q: Do we wish to set haspeer == B_FALSE? */
1696 			ah0dbg(("ah_age_bytes: "
1697 			    "can't find peer for inbound.\n"));
1698 			return (sadb_age_bytes(ahstack->ah_pfkey_q, inassoc,
1699 			    bytes, B_TRUE));
1700 		}
1701 	} else {
1702 		outassoc = assoc;
1703 		bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
1704 		mutex_enter(&bucket->isaf_lock);
1705 		inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
1706 		    outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
1707 		    outassoc->ipsa_addrfam);
1708 		mutex_exit(&bucket->isaf_lock);
1709 		if (inassoc == NULL) {
1710 			/* Q: Do we wish to set haspeer == B_FALSE? */
1711 			ah0dbg(("ah_age_bytes: "
1712 			    "can't find peer for outbound.\n"));
1713 			return (sadb_age_bytes(ahstack->ah_pfkey_q, outassoc,
1714 			    bytes, B_TRUE));
1715 		}
1716 	}
1717 
1718 	inrc = sadb_age_bytes(ahstack->ah_pfkey_q, inassoc, bytes, B_TRUE);
1719 	outrc = sadb_age_bytes(ahstack->ah_pfkey_q, outassoc, bytes, B_FALSE);
1720 
1721 	/*
1722 	 * REFRELE any peer SA.
1723 	 *
1724 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1725 	 * them in { }.
1726 	 */
1727 	if (inbound) {
1728 		IPSA_REFRELE(outassoc);
1729 	} else {
1730 		IPSA_REFRELE(inassoc);
1731 	}
1732 
1733 	return (inrc && outrc);
1734 }
1735 
1736 /*
1737  * Perform the really difficult work of inserting the proposed situation.
1738  * Called while holding the algorithm lock.
1739  */
1740 static void
1741 ah_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs)
1742 {
1743 	sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
1744 	ipsec_out_t *io;
1745 	ipsec_action_t *ap;
1746 	ipsec_prot_t *prot;
1747 	ipsecah_stack_t	*ahstack;
1748 	netstack_t	*ns;
1749 	ipsec_stack_t	*ipss;
1750 
1751 	io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr;
1752 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
1753 
1754 	ns = io->ipsec_out_ns;
1755 	ipss = ns->netstack_ipsec;
1756 	ahstack = ns->netstack_ipsecah;
1757 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
1758 
1759 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
1760 	prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
1761 	*(uint32_t *)(&prop->sadb_prop_replay) = 0;	/* Quick zero-out! */
1762 
1763 	prop->sadb_prop_replay = ahstack->ipsecah_replay_size;
1764 
1765 	/*
1766 	 * Based upon algorithm properties, and what-not, prioritize a
1767 	 * proposal, based on the ordering of the ah algorithms in the
1768 	 * alternatives presented in the policy rule passed down
1769 	 * through the ipsec_out_t and attached to the acquire record.
1770 	 */
1771 
1772 	for (ap = acqrec->ipsacq_act; ap != NULL;
1773 	    ap = ap->ipa_next) {
1774 		ipsec_alginfo_t *aalg;
1775 
1776 		if ((ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY) ||
1777 		    (!ap->ipa_act.ipa_apply.ipp_use_ah))
1778 			continue;
1779 
1780 		prot = &ap->ipa_act.ipa_apply;
1781 
1782 		ASSERT(prot->ipp_auth_alg > 0);
1783 
1784 		aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
1785 		    [prot->ipp_auth_alg];
1786 		if (aalg == NULL || !ALG_VALID(aalg))
1787 			continue;
1788 
1789 		/* XXX check aalg for duplicates??.. */
1790 
1791 		comb->sadb_comb_flags = 0;
1792 		comb->sadb_comb_reserved = 0;
1793 		comb->sadb_comb_encrypt = 0;
1794 		comb->sadb_comb_encrypt_minbits = 0;
1795 		comb->sadb_comb_encrypt_maxbits = 0;
1796 
1797 		comb->sadb_comb_auth = aalg->alg_id;
1798 		comb->sadb_comb_auth_minbits =
1799 		    MAX(prot->ipp_ah_minbits, aalg->alg_ef_minbits);
1800 		comb->sadb_comb_auth_maxbits =
1801 		    MIN(prot->ipp_ah_maxbits, aalg->alg_ef_maxbits);
1802 
1803 		/*
1804 		 * The following may be based on algorithm
1805 		 * properties, but in the meantime, we just pick
1806 		 * some good, sensible numbers.  Key mgmt. can
1807 		 * (and perhaps should) be the place to finalize
1808 		 * such decisions.
1809 		 */
1810 
1811 		/*
1812 		 * No limits on allocations, since we really don't
1813 		 * support that concept currently.
1814 		 */
1815 		comb->sadb_comb_soft_allocations = 0;
1816 		comb->sadb_comb_hard_allocations = 0;
1817 
1818 		/*
1819 		 * These may want to come from policy rule..
1820 		 */
1821 		comb->sadb_comb_soft_bytes =
1822 		    ahstack->ipsecah_default_soft_bytes;
1823 		comb->sadb_comb_hard_bytes =
1824 		    ahstack->ipsecah_default_hard_bytes;
1825 		comb->sadb_comb_soft_addtime =
1826 		    ahstack->ipsecah_default_soft_addtime;
1827 		comb->sadb_comb_hard_addtime =
1828 		    ahstack->ipsecah_default_hard_addtime;
1829 		comb->sadb_comb_soft_usetime =
1830 		    ahstack->ipsecah_default_soft_usetime;
1831 		comb->sadb_comb_hard_usetime =
1832 		    ahstack->ipsecah_default_hard_usetime;
1833 
1834 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
1835 		if (--combs == 0)
1836 			return;	/* out of space.. */
1837 		comb++;
1838 	}
1839 }
1840 
1841 /*
1842  * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
1843  */
1844 static void
1845 ah_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns)
1846 {
1847 	uint_t combs;
1848 	sadb_msg_t *samsg;
1849 	sadb_prop_t *prop;
1850 	mblk_t *pfkeymp, *msgmp;
1851 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
1852 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
1853 
1854 	AH_BUMP_STAT(ahstack, acquire_requests);
1855 
1856 	if (ahstack->ah_pfkey_q == NULL)
1857 		return;
1858 
1859 	/* Set up ACQUIRE. */
1860 	pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_AH,
1861 	    ns->netstack_ipsec);
1862 	if (pfkeymp == NULL) {
1863 		ah0dbg(("sadb_setup_acquire failed.\n"));
1864 		return;
1865 	}
1866 	ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
1867 	combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
1868 	msgmp = pfkeymp->b_cont;
1869 	samsg = (sadb_msg_t *)(msgmp->b_rptr);
1870 
1871 	/* Insert proposal here. */
1872 
1873 	prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
1874 	ah_insert_prop(prop, acqrec, combs);
1875 	samsg->sadb_msg_len += prop->sadb_prop_len;
1876 	msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
1877 
1878 	mutex_exit(&ipss->ipsec_alg_lock);
1879 
1880 	/*
1881 	 * Must mutex_exit() before sending PF_KEY message up, in
1882 	 * order to avoid recursive mutex_enter() if there are no registered
1883 	 * listeners.
1884 	 *
1885 	 * Once I've sent the message, I'm cool anyway.
1886 	 */
1887 	mutex_exit(&acqrec->ipsacq_lock);
1888 	if (extended != NULL) {
1889 		putnext(ahstack->ah_pfkey_q, extended);
1890 	}
1891 	putnext(ahstack->ah_pfkey_q, pfkeymp);
1892 }
1893 
1894 /*
1895  * Handle the SADB_GETSPI message.  Create a larval SA.
1896  */
1897 static void
1898 ah_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1899 {
1900 	ipsa_t *newbie, *target;
1901 	isaf_t *outbound, *inbound;
1902 	int rc, diagnostic;
1903 	sadb_sa_t *assoc;
1904 	keysock_out_t *kso;
1905 	uint32_t newspi;
1906 
1907 	/*
1908 	 * Randomly generate a proposed SPI value.
1909 	 */
1910 	(void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t));
1911 	newbie = sadb_getspi(ksi, newspi, &diagnostic,
1912 	    ahstack->ipsecah_netstack);
1913 
1914 	if (newbie == NULL) {
1915 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM, diagnostic,
1916 		    ksi->ks_in_serial);
1917 		return;
1918 	} else if (newbie == (ipsa_t *)-1) {
1919 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL, diagnostic,
1920 		    ksi->ks_in_serial);
1921 		return;
1922 	}
1923 
1924 	/*
1925 	 * XXX - We may randomly collide.  We really should recover from this.
1926 	 *	 Unfortunately, that could require spending way-too-much-time
1927 	 *	 in here.  For now, let the user retry.
1928 	 */
1929 
1930 	if (newbie->ipsa_addrfam == AF_INET6) {
1931 		outbound = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6,
1932 		    *(uint32_t *)(newbie->ipsa_dstaddr));
1933 		inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v6,
1934 		    newbie->ipsa_spi);
1935 	} else {
1936 		outbound = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4,
1937 		    *(uint32_t *)(newbie->ipsa_dstaddr));
1938 		inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v4,
1939 		    newbie->ipsa_spi);
1940 	}
1941 
1942 	mutex_enter(&outbound->isaf_lock);
1943 	mutex_enter(&inbound->isaf_lock);
1944 
1945 	/*
1946 	 * Check for collisions (i.e. did sadb_getspi() return with something
1947 	 * that already exists?).
1948 	 *
1949 	 * Try outbound first.  Even though SADB_GETSPI is traditionally
1950 	 * for inbound SAs, you never know what a user might do.
1951 	 */
1952 	target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
1953 	    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
1954 	if (target == NULL) {
1955 		target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
1956 		    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
1957 		    newbie->ipsa_addrfam);
1958 	}
1959 
1960 	/*
1961 	 * I don't have collisions elsewhere!
1962 	 * (Nor will I because I'm still holding inbound/outbound locks.)
1963 	 */
1964 
1965 	if (target != NULL) {
1966 		rc = EEXIST;
1967 		IPSA_REFRELE(target);
1968 	} else {
1969 		/*
1970 		 * sadb_insertassoc() also checks for collisions, so
1971 		 * if there's a colliding larval entry, rc will be set
1972 		 * to EEXIST.
1973 		 */
1974 		rc = sadb_insertassoc(newbie, inbound);
1975 		newbie->ipsa_hardexpiretime = gethrestime_sec();
1976 		newbie->ipsa_hardexpiretime += ahstack->ipsecah_larval_timeout;
1977 	}
1978 
1979 	/*
1980 	 * Can exit outbound mutex.  Hold inbound until we're done with
1981 	 * newbie.
1982 	 */
1983 	mutex_exit(&outbound->isaf_lock);
1984 
1985 	if (rc != 0) {
1986 		mutex_exit(&inbound->isaf_lock);
1987 		IPSA_REFRELE(newbie);
1988 		sadb_pfkey_error(ahstack->ah_pfkey_q, mp, rc,
1989 		    SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
1990 		return;
1991 	}
1992 
1993 	/* Can write here because I'm still holding the bucket lock. */
1994 	newbie->ipsa_type = SADB_SATYPE_AH;
1995 
1996 	/*
1997 	 * Construct successful return message.  We have one thing going
1998 	 * for us in PF_KEY v2.  That's the fact that
1999 	 *	sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
2000 	 */
2001 	assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
2002 	assoc->sadb_sa_exttype = SADB_EXT_SA;
2003 	assoc->sadb_sa_spi = newbie->ipsa_spi;
2004 	*((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
2005 	mutex_exit(&inbound->isaf_lock);
2006 
2007 	/* Convert KEYSOCK_IN to KEYSOCK_OUT. */
2008 	kso = (keysock_out_t *)ksi;
2009 	kso->ks_out_len = sizeof (*kso);
2010 	kso->ks_out_serial = ksi->ks_in_serial;
2011 	kso->ks_out_type = KEYSOCK_OUT;
2012 
2013 	/*
2014 	 * Can safely putnext() to ah_pfkey_q, because this is a turnaround
2015 	 * from the ah_pfkey_q.
2016 	 */
2017 	putnext(ahstack->ah_pfkey_q, mp);
2018 }
2019 
2020 /*
2021  * IPv6 sends up the ICMP errors for validation and the removal of the AH
2022  * header.
2023  */
2024 static ipsec_status_t
2025 ah_icmp_error_v6(mblk_t *ipsec_mp, ipsecah_stack_t *ahstack)
2026 {
2027 	mblk_t *mp;
2028 	ip6_t *ip6h, *oip6h;
2029 	uint16_t hdr_length, ah_length;
2030 	uint8_t *nexthdrp;
2031 	ah_t *ah;
2032 	icmp6_t *icmp6;
2033 	isaf_t *isaf;
2034 	ipsa_t *assoc;
2035 	uint8_t *post_ah_ptr;
2036 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2037 
2038 	mp = ipsec_mp->b_cont;
2039 	ASSERT(mp->b_datap->db_type == M_CTL);
2040 
2041 	/*
2042 	 * Change the type to M_DATA till we finish pullups.
2043 	 */
2044 	mp->b_datap->db_type = M_DATA;
2045 
2046 	/*
2047 	 * Eat the cost of a pullupmsg() for now.  It makes the rest of this
2048 	 * code far less convoluted.
2049 	 */
2050 	if (!pullupmsg(mp, -1) ||
2051 	    !ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, &hdr_length,
2052 	    &nexthdrp) ||
2053 	    mp->b_rptr + hdr_length + sizeof (icmp6_t) + sizeof (ip6_t) +
2054 	    sizeof (ah_t) > mp->b_wptr) {
2055 		IP_AH_BUMP_STAT(ipss, in_discards);
2056 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2057 		    DROPPER(ipss, ipds_ah_nomem),
2058 		    &ahstack->ah_dropper);
2059 		return (IPSEC_STATUS_FAILED);
2060 	}
2061 
2062 	oip6h = (ip6_t *)mp->b_rptr;
2063 	icmp6 = (icmp6_t *)((uint8_t *)oip6h + hdr_length);
2064 	ip6h = (ip6_t *)(icmp6 + 1);
2065 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) {
2066 		IP_AH_BUMP_STAT(ipss, in_discards);
2067 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2068 		    DROPPER(ipss, ipds_ah_bad_v6_hdrs),
2069 		    &ahstack->ah_dropper);
2070 		return (IPSEC_STATUS_FAILED);
2071 	}
2072 	ah = (ah_t *)((uint8_t *)ip6h + hdr_length);
2073 
2074 	isaf = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6, ip6h->ip6_dst);
2075 	mutex_enter(&isaf->isaf_lock);
2076 	assoc = ipsec_getassocbyspi(isaf, ah->ah_spi,
2077 	    (uint32_t *)&ip6h->ip6_src, (uint32_t *)&ip6h->ip6_dst, AF_INET6);
2078 	mutex_exit(&isaf->isaf_lock);
2079 
2080 	if (assoc == NULL) {
2081 		IP_AH_BUMP_STAT(ipss, lookup_failure);
2082 		IP_AH_BUMP_STAT(ipss, in_discards);
2083 		if (ahstack->ipsecah_log_unknown_spi) {
2084 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2085 			    SL_CONSOLE | SL_WARN | SL_ERROR,
2086 			    "Bad ICMP message - No association for the "
2087 			    "attached AH header whose spi is 0x%x, "
2088 			    "sender is 0x%x\n",
2089 			    ah->ah_spi, &oip6h->ip6_src, AF_INET6,
2090 			    ahstack->ipsecah_netstack);
2091 		}
2092 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2093 		    DROPPER(ipss, ipds_ah_no_sa),
2094 		    &ahstack->ah_dropper);
2095 		return (IPSEC_STATUS_FAILED);
2096 	}
2097 
2098 	IPSA_REFRELE(assoc);
2099 
2100 	/*
2101 	 * There seems to be a valid association. If there is enough of AH
2102 	 * header remove it, otherwise bail.  One could check whether it has
2103 	 * complete AH header plus 8 bytes but it does not make sense if an
2104 	 * icmp error is returned for ICMP messages e.g ICMP time exceeded,
2105 	 * that are being sent up. Let the caller figure out.
2106 	 *
2107 	 * NOTE: ah_length is the number of 32 bit words minus 2.
2108 	 */
2109 	ah_length = (ah->ah_length << 2) + 8;
2110 	post_ah_ptr = (uint8_t *)ah + ah_length;
2111 
2112 	if (post_ah_ptr > mp->b_wptr) {
2113 		IP_AH_BUMP_STAT(ipss, in_discards);
2114 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2115 		    DROPPER(ipss, ipds_ah_bad_length),
2116 		    &ahstack->ah_dropper);
2117 		return (IPSEC_STATUS_FAILED);
2118 	}
2119 
2120 	ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - ah_length);
2121 	*nexthdrp = ah->ah_nexthdr;
2122 	ovbcopy(post_ah_ptr, ah,
2123 	    (size_t)((uintptr_t)mp->b_wptr - (uintptr_t)post_ah_ptr));
2124 	mp->b_wptr -= ah_length;
2125 	/* Rewhack to be an ICMP error. */
2126 	mp->b_datap->db_type = M_CTL;
2127 
2128 	return (IPSEC_STATUS_SUCCESS);
2129 }
2130 
2131 /*
2132  * IP sends up the ICMP errors for validation and the removal of
2133  * the AH header.
2134  */
2135 static ipsec_status_t
2136 ah_icmp_error_v4(mblk_t *ipsec_mp, ipsecah_stack_t *ahstack)
2137 {
2138 	mblk_t *mp;
2139 	mblk_t *mp1;
2140 	icmph_t *icmph;
2141 	int iph_hdr_length;
2142 	int hdr_length;
2143 	isaf_t *hptr;
2144 	ipsa_t *assoc;
2145 	int ah_length;
2146 	ipha_t *ipha;
2147 	ipha_t *oipha;
2148 	ah_t *ah;
2149 	uint32_t length;
2150 	int alloc_size;
2151 	uint8_t nexthdr;
2152 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2153 
2154 	mp = ipsec_mp->b_cont;
2155 	ASSERT(mp->b_datap->db_type == M_CTL);
2156 
2157 	/*
2158 	 * Change the type to M_DATA till we finish pullups.
2159 	 */
2160 	mp->b_datap->db_type = M_DATA;
2161 
2162 	oipha = ipha = (ipha_t *)mp->b_rptr;
2163 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
2164 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2165 
2166 	ipha = (ipha_t *)&icmph[1];
2167 	hdr_length = IPH_HDR_LENGTH(ipha);
2168 
2169 	/*
2170 	 * See if we have enough to locate the SPI
2171 	 */
2172 	if ((uchar_t *)ipha + hdr_length + 8 > mp->b_wptr) {
2173 		if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 8 -
2174 		    mp->b_rptr)) {
2175 			ipsec_rl_strlog(ahstack->ipsecah_netstack,
2176 			    info.mi_idnum, 0, 0,
2177 			    SL_WARN | SL_ERROR,
2178 			    "ICMP error: Small AH header\n");
2179 			IP_AH_BUMP_STAT(ipss, in_discards);
2180 			ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2181 			    DROPPER(ipss, ipds_ah_bad_length),
2182 			    &ahstack->ah_dropper);
2183 			return (IPSEC_STATUS_FAILED);
2184 		}
2185 		icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2186 		ipha = (ipha_t *)&icmph[1];
2187 	}
2188 
2189 	ah = (ah_t *)((uint8_t *)ipha + hdr_length);
2190 	nexthdr = ah->ah_nexthdr;
2191 
2192 	hptr = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4, ipha->ipha_dst);
2193 	mutex_enter(&hptr->isaf_lock);
2194 	assoc = ipsec_getassocbyspi(hptr, ah->ah_spi,
2195 	    (uint32_t *)&ipha->ipha_src, (uint32_t *)&ipha->ipha_dst, AF_INET);
2196 	mutex_exit(&hptr->isaf_lock);
2197 
2198 	if (assoc == NULL) {
2199 		IP_AH_BUMP_STAT(ipss, lookup_failure);
2200 		IP_AH_BUMP_STAT(ipss, in_discards);
2201 		if (ahstack->ipsecah_log_unknown_spi) {
2202 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2203 			    SL_CONSOLE | SL_WARN | SL_ERROR,
2204 			    "Bad ICMP message - No association for the "
2205 			    "attached AH header whose spi is 0x%x, "
2206 			    "sender is 0x%x\n",
2207 			    ah->ah_spi, &oipha->ipha_src, AF_INET,
2208 			    ahstack->ipsecah_netstack);
2209 		}
2210 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2211 		    DROPPER(ipss, ipds_ah_no_sa),
2212 		    &ahstack->ah_dropper);
2213 		return (IPSEC_STATUS_FAILED);
2214 	}
2215 
2216 	IPSA_REFRELE(assoc);
2217 	/*
2218 	 * There seems to be a valid association. If there
2219 	 * is enough of AH header remove it, otherwise remove
2220 	 * as much as possible and send it back. One could check
2221 	 * whether it has complete AH header plus 8 bytes but it
2222 	 * does not make sense if an icmp error is returned for
2223 	 * ICMP messages e.g ICMP time exceeded, that are being
2224 	 * sent up. Let the caller figure out.
2225 	 *
2226 	 * NOTE: ah_length is the number of 32 bit words minus 2.
2227 	 */
2228 	ah_length = (ah->ah_length << 2) + 8;
2229 
2230 	if ((uchar_t *)ipha + hdr_length + ah_length > mp->b_wptr) {
2231 		if (mp->b_cont == NULL) {
2232 			/*
2233 			 * There is nothing to pullup. Just remove as
2234 			 * much as possible. This is a common case for
2235 			 * IPV4.
2236 			 */
2237 			ah_length = (mp->b_wptr - ((uchar_t *)ipha +
2238 			    hdr_length));
2239 			goto done;
2240 		}
2241 		/* Pullup the full ah header */
2242 		if (!pullupmsg(mp, (uchar_t *)ah + ah_length - mp->b_rptr)) {
2243 			/*
2244 			 * pullupmsg could have failed if there was not
2245 			 * enough to pullup or memory allocation failed.
2246 			 * We tried hard, give up now.
2247 			 */
2248 			IP_AH_BUMP_STAT(ipss, in_discards);
2249 			ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2250 			    DROPPER(ipss, ipds_ah_nomem),
2251 			    &ahstack->ah_dropper);
2252 			return (IPSEC_STATUS_FAILED);
2253 		}
2254 		icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2255 		ipha = (ipha_t *)&icmph[1];
2256 	}
2257 done:
2258 	/*
2259 	 * Remove the AH header and change the protocol.
2260 	 * Don't update the spi fields in the ipsec_in
2261 	 * message as we are called just to validate the
2262 	 * message attached to the ICMP message.
2263 	 *
2264 	 * If we never pulled up since all of the message
2265 	 * is in one single mblk, we can't remove the AH header
2266 	 * by just setting the b_wptr to the beginning of the
2267 	 * AH header. We need to allocate a mblk that can hold
2268 	 * up until the inner IP header and copy them.
2269 	 */
2270 	alloc_size = iph_hdr_length + sizeof (icmph_t) + hdr_length;
2271 
2272 	if ((mp1 = allocb(alloc_size, BPRI_LO)) == NULL) {
2273 		IP_AH_BUMP_STAT(ipss, in_discards);
2274 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2275 		    DROPPER(ipss, ipds_ah_nomem),
2276 		    &ahstack->ah_dropper);
2277 		return (IPSEC_STATUS_FAILED);
2278 	}
2279 	/* ICMP errors are M_CTL messages */
2280 	mp1->b_datap->db_type = M_CTL;
2281 	ipsec_mp->b_cont = mp1;
2282 	bcopy(mp->b_rptr, mp1->b_rptr, alloc_size);
2283 	mp1->b_wptr += alloc_size;
2284 
2285 	/*
2286 	 * Skip whatever we have copied and as much of AH header
2287 	 * possible. If we still have something left in the original
2288 	 * message, tag on.
2289 	 */
2290 	mp->b_rptr = (uchar_t *)ipha + hdr_length + ah_length;
2291 
2292 	if (mp->b_rptr != mp->b_wptr) {
2293 		mp1->b_cont = mp;
2294 	} else {
2295 		if (mp->b_cont != NULL)
2296 			mp1->b_cont = mp->b_cont;
2297 		freeb(mp);
2298 	}
2299 
2300 	ipha = (ipha_t *)(mp1->b_rptr + iph_hdr_length + sizeof (icmph_t));
2301 	ipha->ipha_protocol = nexthdr;
2302 	length = ntohs(ipha->ipha_length);
2303 	length -= ah_length;
2304 	ipha->ipha_length = htons((uint16_t)length);
2305 	ipha->ipha_hdr_checksum = 0;
2306 	ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
2307 
2308 	return (IPSEC_STATUS_SUCCESS);
2309 }
2310 
2311 /*
2312  * IP calls this to validate the ICMP errors that
2313  * we got from the network.
2314  */
2315 ipsec_status_t
2316 ipsecah_icmp_error(mblk_t *mp)
2317 {
2318 	ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr;
2319 	netstack_t	*ns = ii->ipsec_in_ns;
2320 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
2321 
2322 	if (ii->ipsec_in_v4)
2323 		return (ah_icmp_error_v4(mp, ahstack));
2324 	else
2325 		return (ah_icmp_error_v6(mp, ahstack));
2326 }
2327 
2328 static int
2329 ah_fix_tlv_options_v6(uint8_t *oi_opt, uint8_t *pi_opt, uint_t ehdrlen,
2330     uint8_t hdr_type, boolean_t copy_always)
2331 {
2332 	uint8_t opt_type;
2333 	uint_t optlen;
2334 
2335 	ASSERT(hdr_type == IPPROTO_DSTOPTS || hdr_type == IPPROTO_HOPOPTS);
2336 
2337 	/*
2338 	 * Copy the next header and hdr ext. len of the HOP-by-HOP
2339 	 * and Destination option.
2340 	 */
2341 	*pi_opt++ = *oi_opt++;
2342 	*pi_opt++ = *oi_opt++;
2343 	ehdrlen -= 2;
2344 
2345 	/*
2346 	 * Now handle all the TLV encoded options.
2347 	 */
2348 	while (ehdrlen != 0) {
2349 		opt_type = *oi_opt;
2350 
2351 		if (opt_type == IP6OPT_PAD1) {
2352 			optlen = 1;
2353 		} else {
2354 			if (ehdrlen < 2)
2355 				goto bad_opt;
2356 			optlen = 2 + oi_opt[1];
2357 			if (optlen > ehdrlen)
2358 				goto bad_opt;
2359 		}
2360 		if (copy_always || !(opt_type & IP6OPT_MUTABLE)) {
2361 			bcopy(oi_opt, pi_opt, optlen);
2362 		} else {
2363 			if (optlen == 1) {
2364 				*pi_opt = 0;
2365 			} else {
2366 				/*
2367 				 * Copy the type and data length fields.
2368 				 * Zero the option data by skipping
2369 				 * option type and option data len
2370 				 * fields.
2371 				 */
2372 				*pi_opt = *oi_opt;
2373 				*(pi_opt + 1) = *(oi_opt + 1);
2374 				bzero(pi_opt + 2, optlen - 2);
2375 			}
2376 		}
2377 		ehdrlen -= optlen;
2378 		oi_opt += optlen;
2379 		pi_opt += optlen;
2380 	}
2381 	return (0);
2382 bad_opt:
2383 	return (-1);
2384 }
2385 
2386 /*
2387  * Construct a pseudo header for AH, processing all the options.
2388  *
2389  * oip6h is the IPv6 header of the incoming or outgoing packet.
2390  * ip6h is the pointer to the pseudo headers IPV6 header. All
2391  * the space needed for the options have been allocated including
2392  * the AH header.
2393  *
2394  * If copy_always is set, all the options that appear before AH are copied
2395  * blindly without checking for IP6OPT_MUTABLE. This is used by
2396  * ah_auth_out_done().  Please refer to that function for details.
2397  *
2398  * NOTE :
2399  *
2400  * *  AH header is never copied in this function even if copy_always
2401  *    is set. It just returns the ah_offset - offset of the AH header
2402  *    and the caller needs to do the copying. This is done so that we
2403  *    don't have pass extra arguments e.g. SA etc. and also,
2404  *    it is not needed when ah_auth_out_done is calling this function.
2405  */
2406 static uint_t
2407 ah_fix_phdr_v6(ip6_t *ip6h, ip6_t *oip6h, boolean_t outbound,
2408     boolean_t copy_always)
2409 {
2410 	uint8_t	*oi_opt;
2411 	uint8_t	*pi_opt;
2412 	uint8_t nexthdr;
2413 	uint8_t *prev_nexthdr;
2414 	ip6_hbh_t *hbhhdr;
2415 	ip6_dest_t *dsthdr = NULL;
2416 	ip6_rthdr0_t *rthdr;
2417 	int ehdrlen;
2418 	ah_t *ah;
2419 	int ret;
2420 
2421 	/*
2422 	 * In the outbound case for source route, ULP has already moved
2423 	 * the first hop, which is now in ip6_dst. We need to re-arrange
2424 	 * the header to make it look like how it would appear in the
2425 	 * receiver i.e
2426 	 *
2427 	 * Because of ip_massage_options_v6 the header looks like
2428 	 * this :
2429 	 *
2430 	 * ip6_src = S, ip6_dst = I1. followed by I2,I3,D.
2431 	 *
2432 	 * When it reaches the receiver, it would look like
2433 	 *
2434 	 * ip6_src = S, ip6_dst = D. followed by I1,I2,I3.
2435 	 *
2436 	 * NOTE : We assume that there are no problems with the options
2437 	 * as IP should have already checked this.
2438 	 */
2439 
2440 	oi_opt = (uchar_t *)&oip6h[1];
2441 	pi_opt = (uchar_t *)&ip6h[1];
2442 
2443 	/*
2444 	 * We set the prev_nexthdr properly in the pseudo header.
2445 	 * After we finish authentication and come back from the
2446 	 * algorithm module, pseudo header will become the real
2447 	 * IP header.
2448 	 */
2449 	prev_nexthdr = (uint8_t *)&ip6h->ip6_nxt;
2450 	nexthdr = oip6h->ip6_nxt;
2451 	/* Assume IP has already stripped it */
2452 	ASSERT(nexthdr != IPPROTO_FRAGMENT && nexthdr != IPPROTO_RAW);
2453 	ah = NULL;
2454 	dsthdr = NULL;
2455 	for (;;) {
2456 		switch (nexthdr) {
2457 		case IPPROTO_HOPOPTS:
2458 			hbhhdr = (ip6_hbh_t *)oi_opt;
2459 			nexthdr = hbhhdr->ip6h_nxt;
2460 			ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
2461 			ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2462 			    IPPROTO_HOPOPTS, copy_always);
2463 			/*
2464 			 * Return a zero offset indicating error if there
2465 			 * was error.
2466 			 */
2467 			if (ret == -1)
2468 				return (0);
2469 			hbhhdr = (ip6_hbh_t *)pi_opt;
2470 			prev_nexthdr = (uint8_t *)&hbhhdr->ip6h_nxt;
2471 			break;
2472 		case IPPROTO_ROUTING:
2473 			rthdr = (ip6_rthdr0_t *)oi_opt;
2474 			nexthdr = rthdr->ip6r0_nxt;
2475 			ehdrlen = 8 * (rthdr->ip6r0_len + 1);
2476 			if (!copy_always && outbound) {
2477 				int i, left;
2478 				ip6_rthdr0_t *prthdr;
2479 				in6_addr_t *ap, *pap;
2480 
2481 				left = rthdr->ip6r0_segleft;
2482 				prthdr = (ip6_rthdr0_t *)pi_opt;
2483 				pap = (in6_addr_t *)(prthdr + 1);
2484 				ap = (in6_addr_t *)(rthdr + 1);
2485 				/*
2486 				 * First eight bytes except seg_left
2487 				 * does not change en route.
2488 				 */
2489 				bcopy(oi_opt, pi_opt, 8);
2490 				prthdr->ip6r0_segleft = 0;
2491 				/*
2492 				 * First address has been moved to
2493 				 * the destination address of the
2494 				 * ip header by ip_massage_options_v6.
2495 				 * And the real destination address is
2496 				 * in the last address part of the
2497 				 * option.
2498 				 */
2499 				*pap = oip6h->ip6_dst;
2500 				for (i = 1; i < left - 1; i++)
2501 					pap[i] = ap[i - 1];
2502 				ip6h->ip6_dst = *(ap + left - 1);
2503 			} else {
2504 				bcopy(oi_opt, pi_opt, ehdrlen);
2505 			}
2506 			rthdr = (ip6_rthdr0_t *)pi_opt;
2507 			prev_nexthdr = (uint8_t *)&rthdr->ip6r0_nxt;
2508 			break;
2509 		case IPPROTO_DSTOPTS:
2510 			/*
2511 			 * Destination options are tricky.  If there is
2512 			 * a terminal (e.g. non-IPv6-extension) header
2513 			 * following the destination options, don't
2514 			 * reset prev_nexthdr or advance the AH insertion
2515 			 * point and just treat this as a terminal header.
2516 			 *
2517 			 * If this is an inbound packet, just deal with
2518 			 * it as is.
2519 			 */
2520 			dsthdr = (ip6_dest_t *)oi_opt;
2521 			/*
2522 			 * XXX I hope common-subexpression elimination
2523 			 * saves us the double-evaluate.
2524 			 */
2525 			if (outbound && dsthdr->ip6d_nxt != IPPROTO_ROUTING &&
2526 			    dsthdr->ip6d_nxt != IPPROTO_HOPOPTS)
2527 				goto terminal_hdr;
2528 			nexthdr = dsthdr->ip6d_nxt;
2529 			ehdrlen = 8 * (dsthdr->ip6d_len + 1);
2530 			ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2531 			    IPPROTO_DSTOPTS, copy_always);
2532 			/*
2533 			 * Return a zero offset indicating error if there
2534 			 * was error.
2535 			 */
2536 			if (ret == -1)
2537 				return (0);
2538 			break;
2539 		case IPPROTO_AH:
2540 			/*
2541 			 * Be conservative in what you send.  We shouldn't
2542 			 * see two same-scoped AH's in one packet.
2543 			 * (Inner-IP-scoped AH will be hit by terminal
2544 			 * header of IP or IPv6.)
2545 			 */
2546 			ASSERT(!outbound);
2547 			return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2548 		default:
2549 			ASSERT(outbound);
2550 terminal_hdr:
2551 			*prev_nexthdr = IPPROTO_AH;
2552 			ah = (ah_t *)pi_opt;
2553 			ah->ah_nexthdr = nexthdr;
2554 			return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2555 		}
2556 		pi_opt += ehdrlen;
2557 		oi_opt += ehdrlen;
2558 	}
2559 	/* NOTREACHED */
2560 }
2561 
2562 static boolean_t
2563 ah_finish_up(ah_t *phdr_ah, ah_t *inbound_ah, ipsa_t *assoc,
2564     int ah_data_sz, int ah_align_sz, ipsecah_stack_t *ahstack)
2565 {
2566 	int i;
2567 
2568 	/*
2569 	 * Padding :
2570 	 *
2571 	 * 1) Authentication data may have to be padded
2572 	 * before ICV calculation if ICV is not a multiple
2573 	 * of 64 bits. This padding is arbitrary and transmitted
2574 	 * with the packet at the end of the authentication data.
2575 	 * Payload length should include the padding bytes.
2576 	 *
2577 	 * 2) Explicit padding of the whole datagram may be
2578 	 * required by the algorithm which need not be
2579 	 * transmitted. It is assumed that this will be taken
2580 	 * care by the algorithm module.
2581 	 */
2582 	bzero(phdr_ah + 1, ah_data_sz);	/* Zero out ICV for pseudo-hdr. */
2583 
2584 	if (inbound_ah == NULL) {
2585 		/* Outbound AH datagram. */
2586 
2587 		phdr_ah->ah_length = (ah_align_sz >> 2) + 1;
2588 		phdr_ah->ah_reserved = 0;
2589 		phdr_ah->ah_spi = assoc->ipsa_spi;
2590 
2591 		phdr_ah->ah_replay =
2592 		    htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
2593 		if (phdr_ah->ah_replay == 0 && assoc->ipsa_replay_wsize != 0) {
2594 			/*
2595 			 * XXX We have replay counter wrapping.  We probably
2596 			 * want to nuke this SA (and its peer).
2597 			 */
2598 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2599 			    SL_ERROR | SL_CONSOLE | SL_WARN,
2600 			    "Outbound AH SA (0x%x), dst %s has wrapped "
2601 			    "sequence.\n", phdr_ah->ah_spi,
2602 			    assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
2603 			    ahstack->ipsecah_netstack);
2604 
2605 			sadb_replay_delete(assoc);
2606 			/* Caller will free phdr_mp and return NULL. */
2607 			return (B_FALSE);
2608 		}
2609 
2610 		if (ah_data_sz != ah_align_sz) {
2611 			uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2612 			    ah_data_sz);
2613 
2614 			for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2615 				pad[i] = (uchar_t)i;	/* Fill the padding */
2616 			}
2617 		}
2618 	} else {
2619 		/* Inbound AH datagram. */
2620 		phdr_ah->ah_nexthdr = inbound_ah->ah_nexthdr;
2621 		phdr_ah->ah_length = inbound_ah->ah_length;
2622 		phdr_ah->ah_reserved = 0;
2623 		ASSERT(inbound_ah->ah_spi == assoc->ipsa_spi);
2624 		phdr_ah->ah_spi = inbound_ah->ah_spi;
2625 		phdr_ah->ah_replay = inbound_ah->ah_replay;
2626 
2627 		if (ah_data_sz != ah_align_sz) {
2628 			uchar_t *opad = ((uchar_t *)inbound_ah +
2629 			    sizeof (ah_t) + ah_data_sz);
2630 			uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2631 			    ah_data_sz);
2632 
2633 			for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2634 				pad[i] = opad[i];	/* Copy the padding */
2635 			}
2636 		}
2637 	}
2638 
2639 	return (B_TRUE);
2640 }
2641 
2642 /*
2643  * Called upon failing the inbound ICV check. The message passed as
2644  * argument is freed.
2645  */
2646 static void
2647 ah_log_bad_auth(mblk_t *ipsec_in)
2648 {
2649 	mblk_t *mp = ipsec_in->b_cont->b_cont;
2650 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
2651 	boolean_t isv4 = ii->ipsec_in_v4;
2652 	ipsa_t *assoc = ii->ipsec_in_ah_sa;
2653 	int af;
2654 	void *addr;
2655 	netstack_t	*ns = ii->ipsec_in_ns;
2656 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
2657 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2658 
2659 	mp->b_rptr -= ii->ipsec_in_skip_len;
2660 
2661 	if (isv4) {
2662 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
2663 		addr = &ipha->ipha_dst;
2664 		af = AF_INET;
2665 	} else {
2666 		ip6_t *ip6h = (ip6_t *)mp->b_rptr;
2667 		addr = &ip6h->ip6_dst;
2668 		af = AF_INET6;
2669 	}
2670 
2671 	/*
2672 	 * Log the event. Don't print to the console, block
2673 	 * potential denial-of-service attack.
2674 	 */
2675 	AH_BUMP_STAT(ahstack, bad_auth);
2676 
2677 	ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
2678 	    "AH Authentication failed spi %x, dst_addr %s",
2679 	    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
2680 
2681 	IP_AH_BUMP_STAT(ipss, in_discards);
2682 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
2683 	    DROPPER(ipss, ipds_ah_bad_auth),
2684 	    &ahstack->ah_dropper);
2685 }
2686 
2687 /*
2688  * Kernel crypto framework callback invoked after completion of async
2689  * crypto requests.
2690  */
2691 static void
2692 ah_kcf_callback(void *arg, int status)
2693 {
2694 	mblk_t *ipsec_mp = (mblk_t *)arg;
2695 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
2696 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
2697 	netstackid_t	stackid;
2698 	netstack_t	*ns, *ns_arg;
2699 	ipsec_stack_t	*ipss;
2700 	ipsecah_stack_t	*ahstack;
2701 	ipsec_out_t	*io = (ipsec_out_t *)ii;
2702 
2703 	ASSERT(ipsec_mp->b_cont != NULL);
2704 
2705 	if (is_inbound) {
2706 		stackid = ii->ipsec_in_stackid;
2707 		ns_arg = ii->ipsec_in_ns;
2708 	} else {
2709 		stackid = io->ipsec_out_stackid;
2710 		ns_arg = io->ipsec_out_ns;
2711 	}
2712 	/*
2713 	 * Verify that the netstack is still around; could have vanished
2714 	 * while kEf was doing its work.
2715 	 */
2716 	ns = netstack_find_by_stackid(stackid);
2717 	if (ns == NULL || ns != ns_arg) {
2718 		/* Disappeared on us */
2719 		if (ns != NULL)
2720 			netstack_rele(ns);
2721 		freemsg(ipsec_mp);
2722 		return;
2723 	}
2724 
2725 	ahstack = ns->netstack_ipsecah;
2726 	ipss = ns->netstack_ipsec;
2727 
2728 	if (status == CRYPTO_SUCCESS) {
2729 		if (is_inbound) {
2730 			if (ah_auth_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS) {
2731 				netstack_rele(ns);
2732 				return;
2733 			}
2734 			/* finish IPsec processing */
2735 			ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL);
2736 		} else {
2737 			ipha_t *ipha;
2738 
2739 			if (ah_auth_out_done(ipsec_mp) !=
2740 			    IPSEC_STATUS_SUCCESS) {
2741 				netstack_rele(ns);
2742 				return;
2743 			}
2744 
2745 			/* finish IPsec processing */
2746 			ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
2747 			if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
2748 				ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL,
2749 				    NULL);
2750 			} else {
2751 				ip6_t *ip6h = (ip6_t *)ipha;
2752 				ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h,
2753 				    NULL, NULL);
2754 			}
2755 		}
2756 
2757 	} else if (status == CRYPTO_INVALID_MAC) {
2758 		ah_log_bad_auth(ipsec_mp);
2759 	} else {
2760 		ah1dbg(ahstack, ("ah_kcf_callback: crypto failed with 0x%x\n",
2761 		    status));
2762 		AH_BUMP_STAT(ahstack, crypto_failures);
2763 		if (is_inbound)
2764 			IP_AH_BUMP_STAT(ipss, in_discards);
2765 		else
2766 			AH_BUMP_STAT(ahstack, out_discards);
2767 		ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL,
2768 		    DROPPER(ipss, ipds_ah_crypto_failed),
2769 		    &ahstack->ah_dropper);
2770 	}
2771 	netstack_rele(ns);
2772 }
2773 
2774 /*
2775  * Invoked on kernel crypto failure during inbound and outbound processing.
2776  */
2777 static void
2778 ah_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc,
2779     ipsecah_stack_t *ahstack)
2780 {
2781 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2782 
2783 	ah1dbg(ahstack, ("crypto failed for %s AH with 0x%x\n",
2784 	    is_inbound ? "inbound" : "outbound", kef_rc));
2785 	ip_drop_packet(mp, is_inbound, NULL, NULL,
2786 	    DROPPER(ipss, ipds_ah_crypto_failed),
2787 	    &ahstack->ah_dropper);
2788 	AH_BUMP_STAT(ahstack, crypto_failures);
2789 	if (is_inbound)
2790 		IP_AH_BUMP_STAT(ipss, in_discards);
2791 	else
2792 		AH_BUMP_STAT(ahstack, out_discards);
2793 }
2794 
2795 /*
2796  * Helper macros for the ah_submit_req_{inbound,outbound}() functions.
2797  */
2798 
2799 #define	AH_INIT_CALLREQ(_cr, _ipss) {					\
2800 	(_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED;		\
2801 	if ((_ipss)->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] == 		\
2802 	    IPSEC_ALGS_EXEC_ASYNC)					\
2803 		(_cr)->cr_flag |= CRYPTO_ALWAYS_QUEUE;			\
2804 	(_cr)->cr_callback_arg = ipsec_mp;				\
2805 	(_cr)->cr_callback_func = ah_kcf_callback;			\
2806 }
2807 
2808 #define	AH_INIT_CRYPTO_DATA(data, msglen, mblk) {			\
2809 	(data)->cd_format = CRYPTO_DATA_MBLK;				\
2810 	(data)->cd_mp = mblk;						\
2811 	(data)->cd_offset = 0;						\
2812 	(data)->cd_length = msglen;					\
2813 }
2814 
2815 #define	AH_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) {			\
2816 	(mac)->cd_format = CRYPTO_DATA_RAW;				\
2817 	(mac)->cd_offset = 0;						\
2818 	(mac)->cd_length = icvlen;					\
2819 	(mac)->cd_raw.iov_base = icvbuf;				\
2820 	(mac)->cd_raw.iov_len = icvlen;					\
2821 }
2822 
2823 /*
2824  * Submit an inbound packet for processing by the crypto framework.
2825  */
2826 static ipsec_status_t
2827 ah_submit_req_inbound(mblk_t *ipsec_mp, size_t skip_len, uint32_t ah_offset,
2828     ipsa_t *assoc)
2829 {
2830 	int kef_rc;
2831 	mblk_t *phdr_mp;
2832 	crypto_call_req_t call_req;
2833 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
2834 	uint_t icv_len = assoc->ipsa_mac_len;
2835 	crypto_ctx_template_t ctx_tmpl;
2836 	netstack_t	*ns = ii->ipsec_in_ns;
2837 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
2838 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2839 
2840 	phdr_mp = ipsec_mp->b_cont;
2841 	ASSERT(phdr_mp != NULL);
2842 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
2843 
2844 	/*
2845 	 * In case kEF queues and calls back, keep netstackid_t for
2846 	 * verification that the IP instance is still around in
2847 	 * ah_kcf_callback().
2848 	 */
2849 	ii->ipsec_in_stackid = ns->netstack_stackid;
2850 
2851 	/* init arguments for the crypto framework */
2852 	AH_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data, AH_MSGSIZE(phdr_mp),
2853 	    phdr_mp);
2854 
2855 	AH_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac, icv_len,
2856 	    (char *)phdr_mp->b_cont->b_rptr - skip_len + ah_offset +
2857 	    sizeof (ah_t));
2858 
2859 	AH_INIT_CALLREQ(&call_req, ipss);
2860 
2861 	ii->ipsec_in_skip_len = skip_len;
2862 
2863 	IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, ctx_tmpl);
2864 
2865 	/* call KEF to do the MAC operation */
2866 	kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
2867 	    &ii->ipsec_in_crypto_data, &assoc->ipsa_kcfauthkey, ctx_tmpl,
2868 	    &ii->ipsec_in_crypto_mac, &call_req);
2869 
2870 	switch (kef_rc) {
2871 	case CRYPTO_SUCCESS:
2872 		AH_BUMP_STAT(ahstack, crypto_sync);
2873 		return (ah_auth_in_done(ipsec_mp));
2874 	case CRYPTO_QUEUED:
2875 		/* ah_kcf_callback() will be invoked on completion */
2876 		AH_BUMP_STAT(ahstack, crypto_async);
2877 		return (IPSEC_STATUS_PENDING);
2878 	case CRYPTO_INVALID_MAC:
2879 		AH_BUMP_STAT(ahstack, crypto_sync);
2880 		ah_log_bad_auth(ipsec_mp);
2881 		return (IPSEC_STATUS_FAILED);
2882 	}
2883 
2884 	ah_crypto_failed(ipsec_mp, B_TRUE, kef_rc, ahstack);
2885 	return (IPSEC_STATUS_FAILED);
2886 }
2887 
2888 /*
2889  * Submit an outbound packet for processing by the crypto framework.
2890  */
2891 static ipsec_status_t
2892 ah_submit_req_outbound(mblk_t *ipsec_mp, size_t skip_len, ipsa_t *assoc)
2893 {
2894 	int kef_rc;
2895 	mblk_t *phdr_mp;
2896 	crypto_call_req_t call_req;
2897 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
2898 	uint_t icv_len = assoc->ipsa_mac_len;
2899 	netstack_t	*ns = io->ipsec_out_ns;
2900 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
2901 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2902 
2903 	phdr_mp = ipsec_mp->b_cont;
2904 	ASSERT(phdr_mp != NULL);
2905 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
2906 
2907 	/*
2908 	 * In case kEF queues and calls back, keep netstackid_t for
2909 	 * verification that the IP instance is still around in
2910 	 * ah_kcf_callback().
2911 	 */
2912 	io->ipsec_out_stackid = ns->netstack_stackid;
2913 
2914 	/* init arguments for the crypto framework */
2915 	AH_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data, AH_MSGSIZE(phdr_mp),
2916 	    phdr_mp);
2917 
2918 	AH_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac, icv_len,
2919 	    (char *)phdr_mp->b_wptr);
2920 
2921 	AH_INIT_CALLREQ(&call_req, ipss);
2922 
2923 	io->ipsec_out_skip_len = skip_len;
2924 
2925 	ASSERT(io->ipsec_out_ah_sa != NULL);
2926 
2927 	/* call KEF to do the MAC operation */
2928 	kef_rc = crypto_mac(&assoc->ipsa_amech, &io->ipsec_out_crypto_data,
2929 	    &assoc->ipsa_kcfauthkey, assoc->ipsa_authtmpl,
2930 	    &io->ipsec_out_crypto_mac, &call_req);
2931 
2932 	switch (kef_rc) {
2933 	case CRYPTO_SUCCESS:
2934 		AH_BUMP_STAT(ahstack, crypto_sync);
2935 		return (ah_auth_out_done(ipsec_mp));
2936 	case CRYPTO_QUEUED:
2937 		/* ah_kcf_callback() will be invoked on completion */
2938 		AH_BUMP_STAT(ahstack, crypto_async);
2939 		return (IPSEC_STATUS_PENDING);
2940 	}
2941 
2942 	ah_crypto_failed(ipsec_mp, B_FALSE, kef_rc, ahstack);
2943 	return (IPSEC_STATUS_FAILED);
2944 }
2945 
2946 /*
2947  * This function constructs a pseudo header by looking at the IP header
2948  * and options if any. This is called for both outbound and inbound,
2949  * before computing the ICV.
2950  */
2951 static mblk_t *
2952 ah_process_ip_options_v6(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
2953     uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
2954 {
2955 	ip6_t	*ip6h;
2956 	ip6_t	*oip6h;
2957 	mblk_t 	*phdr_mp;
2958 	int option_length;
2959 	uint_t	ah_align_sz;
2960 	uint_t ah_offset;
2961 	int hdr_size;
2962 	ipsec_stack_t	*ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2963 
2964 	/*
2965 	 * Allocate space for the authentication data also. It is
2966 	 * useful both during the ICV calculation where we need to
2967 	 * feed in zeroes and while sending the datagram back to IP
2968 	 * where we will be using the same space.
2969 	 *
2970 	 * We need to allocate space for padding bytes if it is not
2971 	 * a multiple of IPV6_PADDING_ALIGN.
2972 	 *
2973 	 * In addition, we allocate space for the ICV computed by
2974 	 * the kernel crypto framework, saving us a separate kmem
2975 	 * allocation down the road.
2976 	 */
2977 
2978 	ah_align_sz = P2ALIGN(ah_data_sz + IPV6_PADDING_ALIGN - 1,
2979 	    IPV6_PADDING_ALIGN);
2980 
2981 	ASSERT(ah_align_sz >= ah_data_sz);
2982 
2983 	hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
2984 	option_length = hdr_size - IPV6_HDR_LEN;
2985 
2986 	/* This was not included in ipsec_ah_get_hdr_size_v6() */
2987 	hdr_size += (sizeof (ah_t) + ah_align_sz);
2988 
2989 	if (!outbound && (MBLKL(mp) < hdr_size)) {
2990 		/*
2991 		 * We have post-AH header options in a separate mblk,
2992 		 * a pullup is required.
2993 		 */
2994 		if (!pullupmsg(mp, hdr_size))
2995 			return (NULL);
2996 	}
2997 
2998 	if ((phdr_mp = allocb(hdr_size + ah_data_sz, BPRI_HI)) == NULL) {
2999 		return (NULL);
3000 	}
3001 
3002 	oip6h = (ip6_t *)mp->b_rptr;
3003 
3004 	/*
3005 	 * Form the basic IP header first. Zero out the header
3006 	 * so that the mutable fields are zeroed out.
3007 	 */
3008 	ip6h = (ip6_t *)phdr_mp->b_rptr;
3009 	bzero(ip6h, sizeof (ip6_t));
3010 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
3011 
3012 	if (outbound) {
3013 		/*
3014 		 * Include the size of AH and authentication data.
3015 		 * This is how our recipient would compute the
3016 		 * authentication data. Look at what we do in the
3017 		 * inbound case below.
3018 		 */
3019 		ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) +
3020 		    sizeof (ah_t) + ah_align_sz);
3021 	} else {
3022 		ip6h->ip6_plen = oip6h->ip6_plen;
3023 	}
3024 
3025 	ip6h->ip6_src = oip6h->ip6_src;
3026 	ip6h->ip6_dst = oip6h->ip6_dst;
3027 
3028 	*length_to_skip = IPV6_HDR_LEN;
3029 	if (option_length == 0) {
3030 		/* Form the AH header */
3031 		ip6h->ip6_nxt = IPPROTO_AH;
3032 		((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
3033 		ah_offset = *length_to_skip;
3034 	} else {
3035 		ip6h->ip6_nxt = oip6h->ip6_nxt;
3036 		/* option_length does not include the AH header's size */
3037 		*length_to_skip += option_length;
3038 
3039 		ah_offset = ah_fix_phdr_v6(ip6h, oip6h, outbound, B_FALSE);
3040 		if (ah_offset == 0) {
3041 			ip_drop_packet(phdr_mp, !outbound, NULL, NULL,
3042 			    DROPPER(ipss, ipds_ah_bad_v6_hdrs),
3043 			    &ahstack->ah_dropper);
3044 			return (NULL);
3045 		}
3046 	}
3047 
3048 	if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)),
3049 	    (outbound ? NULL : ((ah_t *)((uint8_t *)oip6h + ah_offset))),
3050 	    assoc, ah_data_sz, ah_align_sz, ahstack)) {
3051 		freeb(phdr_mp);
3052 		/*
3053 		 * Returning NULL will tell the caller to
3054 		 * IPSA_REFELE(), free the memory, etc.
3055 		 */
3056 		return (NULL);
3057 	}
3058 
3059 	phdr_mp->b_wptr = ((uint8_t *)ip6h + ah_offset + sizeof (ah_t) +
3060 	    ah_align_sz);
3061 	if (!outbound)
3062 		*length_to_skip += sizeof (ah_t) + ah_align_sz;
3063 	return (phdr_mp);
3064 }
3065 
3066 /*
3067  * This function constructs a pseudo header by looking at the IP header
3068  * and options if any. This is called for both outbound and inbound,
3069  * before computing the ICV.
3070  */
3071 static mblk_t *
3072 ah_process_ip_options_v4(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
3073     uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
3074 {
3075 	ipoptp_t opts;
3076 	uint32_t option_length;
3077 	ipha_t	*ipha;
3078 	ipha_t	*oipha;
3079 	mblk_t 	*phdr_mp;
3080 	int	 size;
3081 	uchar_t	*optptr;
3082 	uint8_t optval;
3083 	uint8_t optlen;
3084 	ipaddr_t dst;
3085 	uint32_t v_hlen_tos_len;
3086 	int ip_hdr_length;
3087 	uint_t	ah_align_sz;
3088 	uint32_t off;
3089 
3090 #ifdef	_BIG_ENDIAN
3091 #define	V_HLEN	(v_hlen_tos_len >> 24)
3092 #else
3093 #define	V_HLEN	(v_hlen_tos_len & 0xFF)
3094 #endif
3095 
3096 	oipha = (ipha_t *)mp->b_rptr;
3097 	v_hlen_tos_len = ((uint32_t *)oipha)[0];
3098 
3099 	/*
3100 	 * Allocate space for the authentication data also. It is
3101 	 * useful both during the ICV calculation where we need to
3102 	 * feed in zeroes and while sending the datagram back to IP
3103 	 * where we will be using the same space.
3104 	 *
3105 	 * We need to allocate space for padding bytes if it is not
3106 	 * a multiple of IPV4_PADDING_ALIGN.
3107 	 *
3108 	 * In addition, we allocate space for the ICV computed by
3109 	 * the kernel crypto framework, saving us a separate kmem
3110 	 * allocation down the road.
3111 	 */
3112 
3113 	ah_align_sz = P2ALIGN(ah_data_sz + IPV4_PADDING_ALIGN - 1,
3114 	    IPV4_PADDING_ALIGN);
3115 
3116 	ASSERT(ah_align_sz >= ah_data_sz);
3117 
3118 	size = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz +
3119 	    ah_data_sz;
3120 
3121 	if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
3122 		option_length = oipha->ipha_version_and_hdr_length -
3123 		    (uint8_t)((IP_VERSION << 4) +
3124 		    IP_SIMPLE_HDR_LENGTH_IN_WORDS);
3125 		option_length <<= 2;
3126 		size += option_length;
3127 	}
3128 
3129 	if ((phdr_mp = allocb(size, BPRI_HI)) == NULL) {
3130 		return (NULL);
3131 	}
3132 
3133 	/*
3134 	 * Form the basic IP header first.
3135 	 */
3136 	ipha = (ipha_t *)phdr_mp->b_rptr;
3137 	ipha->ipha_version_and_hdr_length = oipha->ipha_version_and_hdr_length;
3138 	ipha->ipha_type_of_service = 0;
3139 
3140 	if (outbound) {
3141 		/*
3142 		 * Include the size of AH and authentication data.
3143 		 * This is how our recipient would compute the
3144 		 * authentication data. Look at what we do in the
3145 		 * inbound case below.
3146 		 */
3147 		ipha->ipha_length = ntohs(htons(oipha->ipha_length) +
3148 		    sizeof (ah_t) + ah_align_sz);
3149 	} else {
3150 		ipha->ipha_length = oipha->ipha_length;
3151 	}
3152 
3153 	ipha->ipha_ident = oipha->ipha_ident;
3154 	ipha->ipha_fragment_offset_and_flags = 0;
3155 	ipha->ipha_ttl = 0;
3156 	ipha->ipha_protocol = IPPROTO_AH;
3157 	ipha->ipha_hdr_checksum = 0;
3158 	ipha->ipha_src = oipha->ipha_src;
3159 	ipha->ipha_dst = dst = oipha->ipha_dst;
3160 
3161 	/*
3162 	 * If there is no option to process return now.
3163 	 */
3164 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
3165 
3166 	if (V_HLEN == IP_SIMPLE_HDR_VERSION) {
3167 		/* Form the AH header */
3168 		goto ah_hdr;
3169 	}
3170 
3171 	ip_hdr_length += option_length;
3172 
3173 	/*
3174 	 * We have options. In the outbound case for source route,
3175 	 * ULP has already moved the first hop, which is now in
3176 	 * ipha_dst. We need the final destination for the calculation
3177 	 * of authentication data. And also make sure that mutable
3178 	 * and experimental fields are zeroed out in the IP options.
3179 	 */
3180 
3181 	bcopy(&oipha[1], &ipha[1], option_length);
3182 
3183 	for (optval = ipoptp_first(&opts, ipha);
3184 	    optval != IPOPT_EOL;
3185 	    optval = ipoptp_next(&opts)) {
3186 		optptr = opts.ipoptp_cur;
3187 		optlen = opts.ipoptp_len;
3188 		switch (optval) {
3189 		case IPOPT_EXTSEC:
3190 		case IPOPT_COMSEC:
3191 		case IPOPT_RA:
3192 		case IPOPT_SDMDD:
3193 		case IPOPT_SECURITY:
3194 			/*
3195 			 * These options are Immutable, leave them as-is.
3196 			 * Note that IPOPT_NOP is also Immutable, but it
3197 			 * was skipped by ipoptp_next() and thus remains
3198 			 * intact in the header.
3199 			 */
3200 			break;
3201 		case IPOPT_SSRR:
3202 		case IPOPT_LSRR:
3203 			if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0)
3204 				goto bad_ipv4opt;
3205 			/*
3206 			 * These two are mutable and will be zeroed, but
3207 			 * first get the final destination.
3208 			 */
3209 			off = optptr[IPOPT_OFFSET];
3210 			/*
3211 			 * If one of the conditions is true, it means
3212 			 * end of options and dst already has the right
3213 			 * value. So, just fall through.
3214 			 */
3215 			if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
3216 				off = optlen - IP_ADDR_LEN;
3217 				bcopy(&optptr[off], &dst, IP_ADDR_LEN);
3218 			}
3219 			/* FALLTHRU */
3220 		case IPOPT_RR:
3221 		case IPOPT_TS:
3222 		case IPOPT_SATID:
3223 		default:
3224 			/*
3225 			 * optlen should include from the beginning of an
3226 			 * option.
3227 			 * NOTE : Stream Identifier Option (SID): RFC 791
3228 			 * shows the bit pattern of optlen as 2 and documents
3229 			 * the length as 4. We assume it to be 2 here.
3230 			 */
3231 			bzero(optptr, optlen);
3232 			break;
3233 		}
3234 	}
3235 
3236 	if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
3237 bad_ipv4opt:
3238 		ah1dbg(ahstack, ("AH : bad IPv4 option"));
3239 		freeb(phdr_mp);
3240 		return (NULL);
3241 	}
3242 
3243 	/*
3244 	 * Don't change ipha_dst for an inbound datagram as it points
3245 	 * to the right value. Only for the outbound with LSRR/SSRR,
3246 	 * because of ip_massage_options called by the ULP, ipha_dst
3247 	 * points to the first hop and we need to use the final
3248 	 * destination for computing the ICV.
3249 	 */
3250 
3251 	if (outbound)
3252 		ipha->ipha_dst = dst;
3253 ah_hdr:
3254 	((ah_t *)((uint8_t *)ipha + ip_hdr_length))->ah_nexthdr =
3255 	    oipha->ipha_protocol;
3256 	if (!ah_finish_up(((ah_t *)((uint8_t *)ipha + ip_hdr_length)),
3257 	    (outbound ? NULL : ((ah_t *)((uint8_t *)oipha + ip_hdr_length))),
3258 	    assoc, ah_data_sz, ah_align_sz, ahstack)) {
3259 		freeb(phdr_mp);
3260 		/*
3261 		 * Returning NULL will tell the caller to IPSA_REFELE(), free
3262 		 * the memory, etc.
3263 		 */
3264 		return (NULL);
3265 	}
3266 
3267 	phdr_mp->b_wptr = ((uchar_t *)ipha + ip_hdr_length +
3268 	    sizeof (ah_t) + ah_align_sz);
3269 
3270 	ASSERT(phdr_mp->b_wptr <= phdr_mp->b_datap->db_lim);
3271 	if (outbound)
3272 		*length_to_skip = ip_hdr_length;
3273 	else
3274 		*length_to_skip = ip_hdr_length + sizeof (ah_t) + ah_align_sz;
3275 	return (phdr_mp);
3276 }
3277 
3278 /*
3279  * Authenticate an outbound datagram. This function is called
3280  * whenever IP sends an outbound datagram that needs authentication.
3281  */
3282 static ipsec_status_t
3283 ah_outbound(mblk_t *ipsec_out)
3284 {
3285 	mblk_t *mp;
3286 	mblk_t *phdr_mp;
3287 	ipsec_out_t *oi;
3288 	ipsa_t *assoc;
3289 	int length_to_skip;
3290 	uint_t ah_align_sz;
3291 	uint_t age_bytes;
3292 	netstack_t	*ns;
3293 	ipsec_stack_t	*ipss;
3294 	ipsecah_stack_t	*ahstack;
3295 
3296 	/*
3297 	 * Construct the chain of mblks
3298 	 *
3299 	 * IPSEC_OUT->PSEUDO_HDR->DATA
3300 	 *
3301 	 * one by one.
3302 	 */
3303 
3304 	ASSERT(ipsec_out->b_datap->db_type == M_CTL);
3305 
3306 	ASSERT(MBLKL(ipsec_out) >= sizeof (ipsec_info_t));
3307 
3308 	mp = ipsec_out->b_cont;
3309 	oi = (ipsec_out_t *)ipsec_out->b_rptr;
3310 	ns = oi->ipsec_out_ns;
3311 	ipss = ns->netstack_ipsec;
3312 	ahstack = ns->netstack_ipsecah;
3313 
3314 	AH_BUMP_STAT(ahstack, out_requests);
3315 
3316 	ASSERT(mp->b_datap->db_type == M_DATA);
3317 
3318 	assoc = oi->ipsec_out_ah_sa;
3319 	ASSERT(assoc != NULL);
3320 
3321 	/*
3322 	 * Age SA according to number of bytes that will be sent after
3323 	 * adding the AH header, ICV, and padding to the packet.
3324 	 */
3325 
3326 	if (oi->ipsec_out_v4) {
3327 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
3328 		ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3329 		    IPV4_PADDING_ALIGN - 1, IPV4_PADDING_ALIGN);
3330 		age_bytes = ntohs(ipha->ipha_length) + sizeof (ah_t) +
3331 		    ah_align_sz;
3332 	} else {
3333 		ip6_t *ip6h = (ip6_t *)mp->b_rptr;
3334 		ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3335 		    IPV6_PADDING_ALIGN - 1, IPV6_PADDING_ALIGN);
3336 		age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
3337 		    sizeof (ah_t) + ah_align_sz;
3338 	}
3339 
3340 	if (!ah_age_bytes(assoc, age_bytes, B_FALSE)) {
3341 		/* rig things as if ipsec_getassocbyconn() failed */
3342 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
3343 		    "AH association 0x%x, dst %s had bytes expire.\n",
3344 		    ntohl(assoc->ipsa_spi), assoc->ipsa_dstaddr, AF_INET,
3345 		    ahstack->ipsecah_netstack);
3346 		freemsg(ipsec_out);
3347 		return (IPSEC_STATUS_FAILED);
3348 	}
3349 
3350 	if (oi->ipsec_out_is_capab_ill) {
3351 		ah3dbg(ahstack, ("ah_outbound: pkt can be accelerated\n"));
3352 		if (oi->ipsec_out_v4)
3353 			return (ah_outbound_accelerated_v4(ipsec_out, assoc));
3354 		else
3355 			return (ah_outbound_accelerated_v6(ipsec_out, assoc));
3356 	}
3357 	AH_BUMP_STAT(ahstack, noaccel);
3358 
3359 	/*
3360 	 * Insert pseudo header:
3361 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3362 	 */
3363 
3364 	if (oi->ipsec_out_v4) {
3365 		phdr_mp = ah_process_ip_options_v4(mp, assoc, &length_to_skip,
3366 		    assoc->ipsa_mac_len, B_TRUE, ahstack);
3367 	} else {
3368 		phdr_mp = ah_process_ip_options_v6(mp, assoc, &length_to_skip,
3369 		    assoc->ipsa_mac_len, B_TRUE, ahstack);
3370 	}
3371 
3372 	if (phdr_mp == NULL) {
3373 		AH_BUMP_STAT(ahstack, out_discards);
3374 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
3375 		    DROPPER(ipss, ipds_ah_bad_v4_opts),
3376 		    &ahstack->ah_dropper);
3377 		return (IPSEC_STATUS_FAILED);
3378 	}
3379 
3380 	ipsec_out->b_cont = phdr_mp;
3381 	phdr_mp->b_cont = mp;
3382 	mp->b_rptr += length_to_skip;
3383 
3384 	/*
3385 	 * At this point ipsec_out points to the IPSEC_OUT, new_mp
3386 	 * points to an mblk containing the pseudo header (IP header,
3387 	 * AH header, and ICV with mutable fields zero'ed out).
3388 	 * mp points to the mblk containing the ULP data. The original
3389 	 * IP header is kept before the ULP data in mp.
3390 	 */
3391 
3392 	/* submit MAC request to KCF */
3393 	return (ah_submit_req_outbound(ipsec_out, length_to_skip, assoc));
3394 }
3395 
3396 static ipsec_status_t
3397 ah_inbound(mblk_t *ipsec_in_mp, void *arg)
3398 {
3399 	mblk_t *data_mp = ipsec_in_mp->b_cont;
3400 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
3401 	ah_t *ah = (ah_t *)arg;
3402 	ipsa_t *assoc = ii->ipsec_in_ah_sa;
3403 	int length_to_skip;
3404 	int ah_length;
3405 	mblk_t *phdr_mp;
3406 	uint32_t ah_offset;
3407 	netstack_t	*ns = ii->ipsec_in_ns;
3408 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
3409 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
3410 
3411 	ASSERT(assoc != NULL);
3412 
3413 	/*
3414 	 * We may wish to check replay in-range-only here as an optimization.
3415 	 * Include the reality check of ipsa->ipsa_replay >
3416 	 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
3417 	 * where N == ipsa->ipsa_replay_wsize.
3418 	 *
3419 	 * Another check that may come here later is the "collision" check.
3420 	 * If legitimate packets flow quickly enough, this won't be a problem,
3421 	 * but collisions may cause authentication algorithm crunching to
3422 	 * take place when it doesn't need to.
3423 	 */
3424 	if (!sadb_replay_peek(assoc, ah->ah_replay)) {
3425 		AH_BUMP_STAT(ahstack, replay_early_failures);
3426 		IP_AH_BUMP_STAT(ipss, in_discards);
3427 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3428 		    DROPPER(ipss, ipds_ah_early_replay),
3429 		    &ahstack->ah_dropper);
3430 		return (IPSEC_STATUS_FAILED);
3431 	}
3432 
3433 	/*
3434 	 * The offset of the AH header can be computed from its pointer
3435 	 * within the data mblk, which was pulled up until the AH header
3436 	 * by ipsec_inbound_ah_sa() during SA selection.
3437 	 */
3438 	ah_offset = (uchar_t *)ah - data_mp->b_rptr;
3439 
3440 	/*
3441 	 * Has this packet already been processed by a hardware
3442 	 * IPsec accelerator?
3443 	 */
3444 	if (ii->ipsec_in_accelerated) {
3445 		ah3dbg(ahstack,
3446 		    ("ah_inbound_v6: pkt processed by ill=%d isv6=%d\n",
3447 		    ii->ipsec_in_ill_index, !ii->ipsec_in_v4));
3448 		return (ah_inbound_accelerated(ipsec_in_mp, ii->ipsec_in_v4,
3449 		    assoc, ah_offset));
3450 	}
3451 	AH_BUMP_STAT(ahstack, noaccel);
3452 
3453 	/*
3454 	 * We need to pullup until the ICV before we call
3455 	 * ah_process_ip_options_v6.
3456 	 */
3457 	ah_length = (ah->ah_length << 2) + 8;
3458 
3459 	/*
3460 	 * NOTE : If we want to use any field of IP/AH header, you need
3461 	 * to re-assign following the pullup.
3462 	 */
3463 	if (((uchar_t *)ah + ah_length) > data_mp->b_wptr) {
3464 		if (!pullupmsg(data_mp, (uchar_t *)ah + ah_length -
3465 		    data_mp->b_rptr)) {
3466 			(void) ipsec_rl_strlog(ns, info.mi_idnum, 0, 0,
3467 			    SL_WARN | SL_ERROR,
3468 			    "ah_inbound: Small AH header\n");
3469 			IP_AH_BUMP_STAT(ipss, in_discards);
3470 			ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3471 			    DROPPER(ipss, ipds_ah_nomem),
3472 			    &ahstack->ah_dropper);
3473 			return (IPSEC_STATUS_FAILED);
3474 		}
3475 	}
3476 
3477 	/*
3478 	 * Insert pseudo header:
3479 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3480 	 */
3481 	if (ii->ipsec_in_v4) {
3482 		phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
3483 		    &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
3484 	} else {
3485 		phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
3486 		    &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
3487 	}
3488 
3489 	if (phdr_mp == NULL) {
3490 		IP_AH_BUMP_STAT(ipss, in_discards);
3491 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3492 		    (ii->ipsec_in_v4 ?
3493 		    DROPPER(ipss, ipds_ah_bad_v4_opts) :
3494 		    DROPPER(ipss, ipds_ah_bad_v6_hdrs)),
3495 		    &ahstack->ah_dropper);
3496 		return (IPSEC_STATUS_FAILED);
3497 	}
3498 
3499 	ipsec_in_mp->b_cont = phdr_mp;
3500 	phdr_mp->b_cont = data_mp;
3501 	data_mp->b_rptr += length_to_skip;
3502 
3503 	/* submit request to KCF */
3504 	return (ah_submit_req_inbound(ipsec_in_mp, length_to_skip, ah_offset,
3505 	    assoc));
3506 }
3507 
3508 /*
3509  * ah_inbound_accelerated:
3510  * Called from ah_inbound() to process IPsec packets that have been
3511  * accelerated by hardware.
3512  *
3513  * Basically does what ah_auth_in_done() with some changes since
3514  * no pseudo-headers are involved, i.e. the passed message is a
3515  * IPSEC_INFO->DATA.
3516  *
3517  * It is assumed that only packets that have been successfully
3518  * processed by the adapter come here.
3519  *
3520  * 1. get algorithm structure corresponding to association
3521  * 2. calculate pointers to authentication header and ICV
3522  * 3. compare ICV in AH header with ICV in data attributes
3523  *    3.1 if different:
3524  *	  3.1.1 generate error
3525  *        3.1.2 discard message
3526  *    3.2 if ICV matches:
3527  *	  3.2.1 check replay
3528  *        3.2.2 remove AH header
3529  *        3.2.3 age SA byte
3530  *        3.2.4 send to IP
3531  */
3532 ipsec_status_t
3533 ah_inbound_accelerated(mblk_t *ipsec_in, boolean_t isv4, ipsa_t *assoc,
3534     uint32_t ah_offset)
3535 {
3536 	mblk_t *mp;
3537 	ipha_t *ipha;
3538 	ah_t *ah;
3539 	ipsec_in_t *ii;
3540 	uint32_t icv_len;
3541 	uint32_t align_len;
3542 	uint32_t age_bytes;
3543 	ip6_t *ip6h;
3544 	uint8_t *in_icv;
3545 	mblk_t *hada_mp;
3546 	uint32_t next_hdr;
3547 	da_ipsec_t *hada;
3548 	kstat_named_t *counter;
3549 	ipsecah_stack_t	*ahstack;
3550 	netstack_t	*ns;
3551 	ipsec_stack_t	*ipss;
3552 
3553 	ii = (ipsec_in_t *)ipsec_in->b_rptr;
3554 	ns = ii->ipsec_in_ns;
3555 	ahstack = ns->netstack_ipsecah;
3556 	ipss = ns->netstack_ipsec;
3557 
3558 	mp = ipsec_in->b_cont;
3559 	hada_mp = ii->ipsec_in_da;
3560 	ASSERT(hada_mp != NULL);
3561 	hada = (da_ipsec_t *)hada_mp->b_rptr;
3562 
3563 	AH_BUMP_STAT(ahstack, in_accelerated);
3564 
3565 	/*
3566 	 * We only support one level of decapsulation in hardware, so
3567 	 * nuke the pointer.
3568 	 */
3569 	ii->ipsec_in_da = NULL;
3570 	ii->ipsec_in_accelerated = B_FALSE;
3571 
3572 	/*
3573 	 * Extract ICV length from attributes M_CTL and sanity check
3574 	 * its value. We allow the mblk to be smaller than da_ipsec_t
3575 	 * for a small ICV, as long as the entire ICV fits within the mblk.
3576 	 * Also ensures that the ICV length computed by Provider
3577 	 * corresponds to the ICV length of the algorithm specified by the SA.
3578 	 */
3579 	icv_len = hada->da_icv_len;
3580 	if ((icv_len != assoc->ipsa_mac_len) ||
3581 	    (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) <
3582 	    (sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) {
3583 		ah0dbg(("ah_inbound_accelerated: "
3584 		    "ICV len (%u) incorrect or mblk too small (%u)\n",
3585 		    icv_len, (uint32_t)(MBLKL(hada_mp))));
3586 		counter = DROPPER(ipss, ipds_ah_bad_length);
3587 		goto ah_in_discard;
3588 	}
3589 	ASSERT(icv_len != 0);
3590 
3591 	/* compute the padded AH ICV len */
3592 	if (isv4) {
3593 		ipha = (ipha_t *)mp->b_rptr;
3594 		align_len = (icv_len + IPV4_PADDING_ALIGN - 1) &
3595 		    -IPV4_PADDING_ALIGN;
3596 	} else {
3597 		ip6h = (ip6_t *)mp->b_rptr;
3598 		align_len = (icv_len + IPV6_PADDING_ALIGN - 1) &
3599 		    -IPV6_PADDING_ALIGN;
3600 	}
3601 
3602 	ah = (ah_t *)(mp->b_rptr + ah_offset);
3603 	in_icv = (uint8_t *)ah + sizeof (ah_t);
3604 
3605 	/* compare ICV in AH header vs ICV computed by adapter */
3606 	if (bcmp(hada->da_icv, in_icv, icv_len)) {
3607 		int af;
3608 		void *addr;
3609 
3610 		if (isv4) {
3611 			addr = &ipha->ipha_dst;
3612 			af = AF_INET;
3613 		} else {
3614 			addr = &ip6h->ip6_dst;
3615 			af = AF_INET6;
3616 		}
3617 
3618 		/*
3619 		 * Log the event. Don't print to the console, block
3620 		 * potential denial-of-service attack.
3621 		 */
3622 		AH_BUMP_STAT(ahstack, bad_auth);
3623 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
3624 		    "AH Authentication failed spi %x, dst_addr %s",
3625 		    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
3626 		counter = DROPPER(ipss, ipds_ah_bad_auth);
3627 		goto ah_in_discard;
3628 	}
3629 
3630 	ah3dbg(ahstack, ("AH succeeded, checking replay\n"));
3631 	AH_BUMP_STAT(ahstack, good_auth);
3632 
3633 	if (!sadb_replay_check(assoc, ah->ah_replay)) {
3634 		int af;
3635 		void *addr;
3636 
3637 		if (isv4) {
3638 			addr = &ipha->ipha_dst;
3639 			af = AF_INET;
3640 		} else {
3641 			addr = &ip6h->ip6_dst;
3642 			af = AF_INET6;
3643 		}
3644 
3645 		/*
3646 		 * Log the event. As of now we print out an event.
3647 		 * Do not print the replay failure number, or else
3648 		 * syslog cannot collate the error messages.  Printing
3649 		 * the replay number that failed (or printing to the
3650 		 * console) opens a denial-of-service attack.
3651 		 */
3652 		AH_BUMP_STAT(ahstack, replay_failures);
3653 		ipsec_assocfailure(info.mi_idnum, 0, 0,
3654 		    SL_ERROR | SL_WARN,
3655 		    "Replay failed for AH spi %x, dst_addr %s",
3656 		    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
3657 		counter = DROPPER(ipss, ipds_ah_replay);
3658 		goto ah_in_discard;
3659 	}
3660 
3661 	/*
3662 	 * Remove AH header. We do this by copying everything before
3663 	 * the AH header onto the AH header+ICV.
3664 	 */
3665 	/* overwrite AH with what was preceeding it (IP header) */
3666 	next_hdr = ah->ah_nexthdr;
3667 	ovbcopy(mp->b_rptr, mp->b_rptr + sizeof (ah_t) + align_len,
3668 	    ah_offset);
3669 	mp->b_rptr += sizeof (ah_t) + align_len;
3670 	if (isv4) {
3671 		/* adjust IP header next protocol */
3672 		ipha = (ipha_t *)mp->b_rptr;
3673 		ipha->ipha_protocol = next_hdr;
3674 
3675 		age_bytes = ipha->ipha_length;
3676 
3677 		/* adjust length in IP header */
3678 		ipha->ipha_length -= (sizeof (ah_t) + align_len);
3679 
3680 		/* recalculate checksum */
3681 		ipha->ipha_hdr_checksum = 0;
3682 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
3683 	} else {
3684 		/* adjust IP header next protocol */
3685 		ip6h = (ip6_t *)mp->b_rptr;
3686 		ip6h->ip6_nxt = next_hdr;
3687 
3688 		age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
3689 		    sizeof (ah_t);
3690 
3691 		/* adjust length in IP header */
3692 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) -
3693 		    (sizeof (ah_t) + align_len));
3694 	}
3695 
3696 	/* age SA */
3697 	if (!ah_age_bytes(assoc, age_bytes, B_TRUE)) {
3698 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
3699 		ipsec_assocfailure(info.mi_idnum, 0, 0,
3700 		    SL_ERROR | SL_WARN,
3701 		    "AH Association 0x%x, dst %s had bytes expire.\n",
3702 		    assoc->ipsa_spi, assoc->ipsa_dstaddr,
3703 		    AF_INET, ahstack->ipsecah_netstack);
3704 		AH_BUMP_STAT(ahstack, bytes_expired);
3705 		counter = DROPPER(ipss, ipds_ah_bytes_expire);
3706 		goto ah_in_discard;
3707 	}
3708 
3709 	freeb(hada_mp);
3710 	return (IPSEC_STATUS_SUCCESS);
3711 
3712 ah_in_discard:
3713 	IP_AH_BUMP_STAT(ipss, in_discards);
3714 	freeb(hada_mp);
3715 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter,
3716 	    &ahstack->ah_dropper);
3717 	return (IPSEC_STATUS_FAILED);
3718 }
3719 
3720 /*
3721  * ah_outbound_accelerated_v4:
3722  * Called from ah_outbound_v4() and once it is determined that the
3723  * packet is elligible for hardware acceleration.
3724  *
3725  * We proceed as follows:
3726  * 1. allocate and initialize attributes mblk
3727  * 2. mark IPSEC_OUT to indicate that pkt is accelerated
3728  * 3. insert AH header
3729  */
3730 static ipsec_status_t
3731 ah_outbound_accelerated_v4(mblk_t *ipsec_mp, ipsa_t *assoc)
3732 {
3733 	mblk_t *mp, *new_mp;
3734 	ipsec_out_t *oi;
3735 	uint_t ah_data_sz;	/* ICV length, algorithm dependent */
3736 	uint_t ah_align_sz;	/* ICV length + padding */
3737 	uint32_t v_hlen_tos_len; /* from original IP header */
3738 	ipha_t	*oipha;		/* original IP header */
3739 	ipha_t	*nipha;		/* new IP header */
3740 	uint_t option_length = 0;
3741 	uint_t new_hdr_len;	/* new header length */
3742 	uint_t iphdr_length;
3743 	ah_t *ah_hdr;		/* ptr to AH header */
3744 	netstack_t	*ns;
3745 	ipsec_stack_t	*ipss;
3746 	ipsecah_stack_t	*ahstack;
3747 
3748 	oi = (ipsec_out_t *)ipsec_mp->b_rptr;
3749 	ns = oi->ipsec_out_ns;
3750 	ipss = ns->netstack_ipsec;
3751 	ahstack = ns->netstack_ipsecah;
3752 
3753 	mp = ipsec_mp->b_cont;
3754 
3755 	AH_BUMP_STAT(ahstack, out_accelerated);
3756 
3757 	oipha = (ipha_t *)mp->b_rptr;
3758 	v_hlen_tos_len = ((uint32_t *)oipha)[0];
3759 
3760 	/* mark packet as being accelerated in IPSEC_OUT */
3761 	ASSERT(oi->ipsec_out_accelerated == B_FALSE);
3762 	oi->ipsec_out_accelerated = B_TRUE;
3763 
3764 	/* calculate authentication data length, i.e. ICV + padding */
3765 	ah_data_sz = assoc->ipsa_mac_len;
3766 	ah_align_sz = (ah_data_sz + IPV4_PADDING_ALIGN - 1) &
3767 	    -IPV4_PADDING_ALIGN;
3768 
3769 	/*
3770 	 * Insert pseudo header:
3771 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3772 	 */
3773 
3774 	/* IP + AH + authentication + padding data length */
3775 	new_hdr_len = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz;
3776 	if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
3777 		option_length = oipha->ipha_version_and_hdr_length -
3778 		    (uint8_t)((IP_VERSION << 4) +
3779 		    IP_SIMPLE_HDR_LENGTH_IN_WORDS);
3780 		option_length <<= 2;
3781 		new_hdr_len += option_length;
3782 	}
3783 
3784 	/* allocate pseudo-header mblk */
3785 	if ((new_mp = allocb(new_hdr_len, BPRI_HI)) == NULL) {
3786 		/* IPsec kstats: bump bean counter here */
3787 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3788 		    DROPPER(ipss, ipds_ah_nomem),
3789 		    &ahstack->ah_dropper);
3790 		return (IPSEC_STATUS_FAILED);
3791 	}
3792 
3793 	new_mp->b_cont = mp;
3794 	ipsec_mp->b_cont = new_mp;
3795 	new_mp->b_wptr += new_hdr_len;
3796 
3797 	/* copy original IP header to new header */
3798 	bcopy(mp->b_rptr, new_mp->b_rptr, IP_SIMPLE_HDR_LENGTH +
3799 	    option_length);
3800 
3801 	/* update IP header */
3802 	nipha = (ipha_t *)new_mp->b_rptr;
3803 	nipha->ipha_protocol = IPPROTO_AH;
3804 	iphdr_length = ntohs(nipha->ipha_length);
3805 	iphdr_length += sizeof (ah_t) + ah_align_sz;
3806 	nipha->ipha_length = htons(iphdr_length);
3807 	nipha->ipha_hdr_checksum = 0;
3808 	nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
3809 
3810 	/* skip original IP header in mp */
3811 	mp->b_rptr += IP_SIMPLE_HDR_LENGTH + option_length;
3812 
3813 	/* initialize AH header */
3814 	ah_hdr = (ah_t *)(new_mp->b_rptr + IP_SIMPLE_HDR_LENGTH +
3815 	    option_length);
3816 	ah_hdr->ah_nexthdr = oipha->ipha_protocol;
3817 	if (!ah_finish_up(ah_hdr, NULL, assoc, ah_data_sz, ah_align_sz,
3818 	    ahstack)) {
3819 		/* Only way this fails is if outbound replay counter wraps. */
3820 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3821 		    DROPPER(ipss, ipds_ah_replay),
3822 		    &ahstack->ah_dropper);
3823 		return (IPSEC_STATUS_FAILED);
3824 	}
3825 
3826 	return (IPSEC_STATUS_SUCCESS);
3827 }
3828 
3829 /*
3830  * ah_outbound_accelerated_v6:
3831  *
3832  * Called from ah_outbound_v6() once it is determined that the packet
3833  * is eligible for hardware acceleration.
3834  *
3835  * We proceed as follows:
3836  * 1. allocate and initialize attributes mblk
3837  * 2. mark IPSEC_OUT to indicate that pkt is accelerated
3838  * 3. insert AH header
3839  */
3840 static ipsec_status_t
3841 ah_outbound_accelerated_v6(mblk_t *ipsec_mp, ipsa_t *assoc)
3842 {
3843 	mblk_t *mp, *phdr_mp;
3844 	ipsec_out_t *oi;
3845 	uint_t ah_data_sz;	/* ICV length, algorithm dependent */
3846 	uint_t ah_align_sz;	/* ICV length + padding */
3847 	ip6_t	*oip6h;		/* original IP header */
3848 	ip6_t	*ip6h;		/* new IP header */
3849 	uint_t option_length = 0;
3850 	uint_t hdr_size;
3851 	uint_t ah_offset;
3852 	ah_t *ah_hdr;		/* ptr to AH header */
3853 	netstack_t	*ns;
3854 	ipsec_stack_t	*ipss;
3855 	ipsecah_stack_t	*ahstack;
3856 
3857 	oi = (ipsec_out_t *)ipsec_mp->b_rptr;
3858 	ns = oi->ipsec_out_ns;
3859 	ipss = ns->netstack_ipsec;
3860 	ahstack = ns->netstack_ipsecah;
3861 
3862 	mp = ipsec_mp->b_cont;
3863 
3864 	AH_BUMP_STAT(ahstack, out_accelerated);
3865 
3866 	oip6h = (ip6_t *)mp->b_rptr;
3867 
3868 	/* mark packet as being accelerated in IPSEC_OUT */
3869 	ASSERT(oi->ipsec_out_accelerated == B_FALSE);
3870 	oi->ipsec_out_accelerated = B_TRUE;
3871 
3872 	/* calculate authentication data length, i.e. ICV + padding */
3873 	ah_data_sz = assoc->ipsa_mac_len;
3874 	ah_align_sz = (ah_data_sz + IPV4_PADDING_ALIGN - 1) &
3875 	    -IPV4_PADDING_ALIGN;
3876 
3877 	ASSERT(ah_align_sz >= ah_data_sz);
3878 
3879 	hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
3880 	option_length = hdr_size - IPV6_HDR_LEN;
3881 
3882 	/* This was not included in ipsec_ah_get_hdr_size_v6() */
3883 	hdr_size += (sizeof (ah_t) + ah_align_sz);
3884 
3885 	if ((phdr_mp = allocb(hdr_size, BPRI_HI)) == NULL) {
3886 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3887 		    DROPPER(ipss, ipds_ah_nomem),
3888 		    &ahstack->ah_dropper);
3889 		return (IPSEC_STATUS_FAILED);
3890 	}
3891 	phdr_mp->b_wptr += hdr_size;
3892 
3893 	/*
3894 	 * Form the basic IP header first.  We always assign every bit
3895 	 * of the v6 basic header, so a separate bzero is unneeded.
3896 	 */
3897 	ip6h = (ip6_t *)phdr_mp->b_rptr;
3898 	ip6h->ip6_vcf = oip6h->ip6_vcf;
3899 	ip6h->ip6_hlim = oip6h->ip6_hlim;
3900 	ip6h->ip6_src = oip6h->ip6_src;
3901 	ip6h->ip6_dst = oip6h->ip6_dst;
3902 	/*
3903 	 * Include the size of AH and authentication data.
3904 	 * This is how our recipient would compute the
3905 	 * authentication data. Look at what we do in the
3906 	 * inbound case below.
3907 	 */
3908 	ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) + sizeof (ah_t) +
3909 	    ah_align_sz);
3910 
3911 	/*
3912 	 * Insert pseudo header:
3913 	 * IPSEC_INFO -> [IP6, LLH, ULP] =>
3914 	 *	IPSEC_INFO -> [IP, LLH, AH, ICV] -> ULP
3915 	 */
3916 
3917 	if (option_length == 0) {
3918 		/* Form the AH header */
3919 		ip6h->ip6_nxt = IPPROTO_AH;
3920 		((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
3921 		ah_offset = IPV6_HDR_LEN;
3922 	} else {
3923 		ip6h->ip6_nxt = oip6h->ip6_nxt;
3924 		/* option_length does not include the AH header's size */
3925 		ah_offset = ah_fix_phdr_v6(ip6h, oip6h, B_TRUE, B_FALSE);
3926 		if (ah_offset == 0) {
3927 			freemsg(phdr_mp);
3928 			ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3929 			    DROPPER(ipss, ipds_ah_bad_v6_hdrs),
3930 			    &ahstack->ah_dropper);
3931 			return (IPSEC_STATUS_FAILED);
3932 		}
3933 	}
3934 
3935 	phdr_mp->b_cont = mp;
3936 	ipsec_mp->b_cont = phdr_mp;
3937 
3938 	/* skip original IP header in mp */
3939 	mp->b_rptr += IPV6_HDR_LEN + option_length;
3940 
3941 	/* initialize AH header */
3942 	ah_hdr = (ah_t *)(phdr_mp->b_rptr + IPV6_HDR_LEN + option_length);
3943 	ah_hdr->ah_nexthdr = oip6h->ip6_nxt;
3944 
3945 	if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)), NULL,
3946 	    assoc, ah_data_sz, ah_align_sz, ahstack)) {
3947 		/* Only way this fails is if outbound replay counter wraps. */
3948 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3949 		    DROPPER(ipss, ipds_ah_replay),
3950 		    &ahstack->ah_dropper);
3951 		return (IPSEC_STATUS_FAILED);
3952 	}
3953 
3954 	return (IPSEC_STATUS_SUCCESS);
3955 }
3956 
3957 /*
3958  * Invoked after processing of an inbound packet by the
3959  * kernel crypto framework. Called by ah_submit_req() for a sync request,
3960  * or by the kcf callback for an async request.
3961  * Returns IPSEC_STATUS_SUCCESS on success, IPSEC_STATUS_FAILED on failure.
3962  * On failure, the mblk chain ipsec_in is freed by this function.
3963  */
3964 static ipsec_status_t
3965 ah_auth_in_done(mblk_t *ipsec_in)
3966 {
3967 	mblk_t *phdr_mp;
3968 	ipha_t *ipha;
3969 	uint_t ah_offset = 0;
3970 	mblk_t *mp;
3971 	int align_len, newpos;
3972 	ah_t *ah;
3973 	uint32_t length;
3974 	uint32_t *dest32;
3975 	uint8_t *dest;
3976 	ipsec_in_t *ii;
3977 	boolean_t isv4;
3978 	ip6_t *ip6h;
3979 	uint_t icv_len;
3980 	ipsa_t *assoc;
3981 	kstat_named_t *counter;
3982 	netstack_t	*ns;
3983 	ipsecah_stack_t	*ahstack;
3984 	ipsec_stack_t	*ipss;
3985 
3986 	ii = (ipsec_in_t *)ipsec_in->b_rptr;
3987 	ns = ii->ipsec_in_ns;
3988 	ahstack = ns->netstack_ipsecah;
3989 	ipss = ns->netstack_ipsec;
3990 
3991 	isv4 = ii->ipsec_in_v4;
3992 	assoc = ii->ipsec_in_ah_sa;
3993 	icv_len = (uint_t)ii->ipsec_in_crypto_mac.cd_raw.iov_len;
3994 
3995 	phdr_mp = ipsec_in->b_cont;
3996 	if (phdr_mp == NULL) {
3997 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
3998 		    DROPPER(ipss, ipds_ah_nomem),
3999 		    &ahstack->ah_dropper);
4000 		return (IPSEC_STATUS_FAILED);
4001 	}
4002 
4003 	mp = phdr_mp->b_cont;
4004 	if (mp == NULL) {
4005 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL,
4006 		    DROPPER(ipss, ipds_ah_nomem),
4007 		    &ahstack->ah_dropper);
4008 		return (IPSEC_STATUS_FAILED);
4009 	}
4010 	mp->b_rptr -= ii->ipsec_in_skip_len;
4011 
4012 	ah_set_usetime(assoc, B_TRUE);
4013 
4014 	if (isv4) {
4015 		ipha = (ipha_t *)mp->b_rptr;
4016 		ah_offset = ipha->ipha_version_and_hdr_length -
4017 		    (uint8_t)((IP_VERSION << 4));
4018 		ah_offset <<= 2;
4019 		align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
4020 		    IPV4_PADDING_ALIGN);
4021 	} else {
4022 		ip6h = (ip6_t *)mp->b_rptr;
4023 		ah_offset = ipsec_ah_get_hdr_size_v6(mp, B_TRUE);
4024 		ASSERT((mp->b_wptr - mp->b_rptr) >= ah_offset);
4025 		align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
4026 		    IPV6_PADDING_ALIGN);
4027 	}
4028 
4029 	ah = (ah_t *)(mp->b_rptr + ah_offset);
4030 	newpos = sizeof (ah_t) + align_len;
4031 
4032 	/*
4033 	 * We get here only when authentication passed.
4034 	 */
4035 
4036 	ah3dbg(ahstack, ("AH succeeded, checking replay\n"));
4037 	AH_BUMP_STAT(ahstack, good_auth);
4038 
4039 	if (!sadb_replay_check(assoc, ah->ah_replay)) {
4040 		int af;
4041 		void *addr;
4042 
4043 		if (isv4) {
4044 			addr = &ipha->ipha_dst;
4045 			af = AF_INET;
4046 		} else {
4047 			addr = &ip6h->ip6_dst;
4048 			af = AF_INET6;
4049 		}
4050 
4051 		/*
4052 		 * Log the event. As of now we print out an event.
4053 		 * Do not print the replay failure number, or else
4054 		 * syslog cannot collate the error messages.  Printing
4055 		 * the replay number that failed (or printing to the
4056 		 * console) opens a denial-of-service attack.
4057 		 */
4058 		AH_BUMP_STAT(ahstack, replay_failures);
4059 		ipsec_assocfailure(info.mi_idnum, 0, 0,
4060 		    SL_ERROR | SL_WARN,
4061 		    "Replay failed for AH spi %x, dst_addr %s",
4062 		    assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
4063 		counter = DROPPER(ipss, ipds_ah_replay);
4064 		goto ah_in_discard;
4065 	}
4066 
4067 	/*
4068 	 * We need to remove the AH header from the original
4069 	 * datagram. Best way to do this is to move the pre-AH headers
4070 	 * forward in the (relatively simple) IPv4 case.  In IPv6, it's
4071 	 * a bit more complicated because of IPv6's next-header chaining,
4072 	 * but it's doable.
4073 	 */
4074 	if (isv4) {
4075 		/*
4076 		 * Assign the right protocol, adjust the length as we
4077 		 * are removing the AH header and adjust the checksum to
4078 		 * account for the protocol and length.
4079 		 */
4080 		length = ntohs(ipha->ipha_length);
4081 		if (!ah_age_bytes(assoc, length, B_TRUE)) {
4082 			/* The ipsa has hit hard expiration, LOG and AUDIT. */
4083 			ipsec_assocfailure(info.mi_idnum, 0, 0,
4084 			    SL_ERROR | SL_WARN,
4085 			    "AH Association 0x%x, dst %s had bytes expire.\n",
4086 			    assoc->ipsa_spi, assoc->ipsa_dstaddr,
4087 			    AF_INET, ahstack->ipsecah_netstack);
4088 			AH_BUMP_STAT(ahstack, bytes_expired);
4089 			counter = DROPPER(ipss, ipds_ah_bytes_expire);
4090 			goto ah_in_discard;
4091 		}
4092 		ipha->ipha_protocol = ah->ah_nexthdr;
4093 		length -= newpos;
4094 
4095 		ipha->ipha_length = htons((uint16_t)length);
4096 		ipha->ipha_hdr_checksum = 0;
4097 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
4098 	} else {
4099 		uchar_t *whereptr;
4100 		int hdrlen;
4101 		uint8_t *nexthdr;
4102 		ip6_hbh_t *hbhhdr;
4103 		ip6_dest_t *dsthdr;
4104 		ip6_rthdr0_t *rthdr;
4105 
4106 		/*
4107 		 * Make phdr_mp hold until the AH header and make
4108 		 * mp hold everything past AH header.
4109 		 */
4110 		length = ntohs(ip6h->ip6_plen);
4111 		if (!ah_age_bytes(assoc, length + sizeof (ip6_t), B_TRUE)) {
4112 			/* The ipsa has hit hard expiration, LOG and AUDIT. */
4113 			ipsec_assocfailure(info.mi_idnum, 0, 0,
4114 			    SL_ERROR | SL_WARN,
4115 			    "AH Association 0x%x, dst %s had bytes "
4116 			    "expire.\n", assoc->ipsa_spi, &ip6h->ip6_dst,
4117 			    AF_INET6, ahstack->ipsecah_netstack);
4118 			AH_BUMP_STAT(ahstack, bytes_expired);
4119 			counter = DROPPER(ipss, ipds_ah_bytes_expire);
4120 			goto ah_in_discard;
4121 		}
4122 
4123 		/*
4124 		 * Update the next header field of the header preceding
4125 		 * AH with the next header field of AH. Start with the
4126 		 * IPv6 header and proceed with the extension headers
4127 		 * until we find what we're looking for.
4128 		 */
4129 		nexthdr = &ip6h->ip6_nxt;
4130 		whereptr =  (uchar_t *)ip6h;
4131 		hdrlen = sizeof (ip6_t);
4132 
4133 		while (*nexthdr != IPPROTO_AH) {
4134 			whereptr += hdrlen;
4135 			/* Assume IP has already stripped it */
4136 			ASSERT(*nexthdr != IPPROTO_FRAGMENT &&
4137 			    *nexthdr != IPPROTO_RAW);
4138 			switch (*nexthdr) {
4139 			case IPPROTO_HOPOPTS:
4140 				hbhhdr = (ip6_hbh_t *)whereptr;
4141 				nexthdr = &hbhhdr->ip6h_nxt;
4142 				hdrlen = 8 * (hbhhdr->ip6h_len + 1);
4143 				break;
4144 			case IPPROTO_DSTOPTS:
4145 				dsthdr = (ip6_dest_t *)whereptr;
4146 				nexthdr = &dsthdr->ip6d_nxt;
4147 				hdrlen = 8 * (dsthdr->ip6d_len + 1);
4148 				break;
4149 			case IPPROTO_ROUTING:
4150 				rthdr = (ip6_rthdr0_t *)whereptr;
4151 				nexthdr = &rthdr->ip6r0_nxt;
4152 				hdrlen = 8 * (rthdr->ip6r0_len + 1);
4153 				break;
4154 			}
4155 		}
4156 		*nexthdr = ah->ah_nexthdr;
4157 		length -= newpos;
4158 		ip6h->ip6_plen = htons((uint16_t)length);
4159 	}
4160 
4161 	/* Now that we've fixed the IP header, move it forward. */
4162 	mp->b_rptr += newpos;
4163 	if (IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) {
4164 		dest32 = (uint32_t *)(mp->b_rptr + ah_offset);
4165 		while (--dest32 >= (uint32_t *)mp->b_rptr)
4166 			*dest32 = *(dest32 - (newpos >> 2));
4167 	} else {
4168 		dest = mp->b_rptr + ah_offset;
4169 		while (--dest >= mp->b_rptr)
4170 			*dest = *(dest - newpos);
4171 	}
4172 	freeb(phdr_mp);
4173 	ipsec_in->b_cont = mp;
4174 
4175 	if (is_system_labeled()) {
4176 		/*
4177 		 * inherit the label by setting it in the new ip header
4178 		 */
4179 		mblk_setcred(mp, DB_CRED(mp));
4180 	}
4181 	return (IPSEC_STATUS_SUCCESS);
4182 
4183 ah_in_discard:
4184 	IP_AH_BUMP_STAT(ipss, in_discards);
4185 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter,
4186 	    &ahstack->ah_dropper);
4187 	return (IPSEC_STATUS_FAILED);
4188 }
4189 
4190 /*
4191  * Invoked after processing of an outbound packet by the
4192  * kernel crypto framework, either by ah_submit_req() for a request
4193  * executed syncrhonously, or by the KEF callback for a request
4194  * executed asynchronously.
4195  */
4196 static ipsec_status_t
4197 ah_auth_out_done(mblk_t *ipsec_out)
4198 {
4199 	mblk_t *phdr_mp;
4200 	mblk_t *mp;
4201 	int align_len;
4202 	uint32_t hdrs_length;
4203 	uchar_t *ptr;
4204 	uint32_t length;
4205 	boolean_t isv4;
4206 	ipsec_out_t *io;
4207 	size_t icv_len;
4208 	netstack_t	*ns;
4209 	ipsec_stack_t	*ipss;
4210 	ipsecah_stack_t	*ahstack;
4211 
4212 	io = (ipsec_out_t *)ipsec_out->b_rptr;
4213 	ns = io->ipsec_out_ns;
4214 	ipss = ns->netstack_ipsec;
4215 	ahstack = ns->netstack_ipsecah;
4216 
4217 	isv4 = io->ipsec_out_v4;
4218 	icv_len = io->ipsec_out_crypto_mac.cd_raw.iov_len;
4219 
4220 	phdr_mp = ipsec_out->b_cont;
4221 	if (phdr_mp == NULL) {
4222 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
4223 		    DROPPER(ipss, ipds_ah_nomem),
4224 		    &ahstack->ah_dropper);
4225 		return (IPSEC_STATUS_FAILED);
4226 	}
4227 
4228 	mp = phdr_mp->b_cont;
4229 	if (mp == NULL) {
4230 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
4231 		    DROPPER(ipss, ipds_ah_nomem),
4232 		    &ahstack->ah_dropper);
4233 		return (IPSEC_STATUS_FAILED);
4234 	}
4235 	mp->b_rptr -= io->ipsec_out_skip_len;
4236 
4237 	ASSERT(io->ipsec_out_ah_sa != NULL);
4238 	ah_set_usetime(io->ipsec_out_ah_sa, B_FALSE);
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