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