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