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