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