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