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