xref: /titanic_44/usr/src/uts/common/inet/ip/ipsecah.c (revision 942214a9d3873106f26cc86dd4aef6ac6176b830)
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 =
1792 		    MAX(prot->ipp_ah_minbits, aalg->alg_ef_minbits);
1793 		comb->sadb_comb_auth_maxbits =
1794 		    MIN(prot->ipp_ah_maxbits, aalg->alg_ef_maxbits);
1795 
1796 		/*
1797 		 * The following may be based on algorithm
1798 		 * properties, but in the meantime, we just pick
1799 		 * some good, sensible numbers.  Key mgmt. can
1800 		 * (and perhaps should) be the place to finalize
1801 		 * such decisions.
1802 		 */
1803 
1804 		/*
1805 		 * No limits on allocations, since we really don't
1806 		 * support that concept currently.
1807 		 */
1808 		comb->sadb_comb_soft_allocations = 0;
1809 		comb->sadb_comb_hard_allocations = 0;
1810 
1811 		/*
1812 		 * These may want to come from policy rule..
1813 		 */
1814 		comb->sadb_comb_soft_bytes = ipsecah_default_soft_bytes;
1815 		comb->sadb_comb_hard_bytes = ipsecah_default_hard_bytes;
1816 		comb->sadb_comb_soft_addtime = ipsecah_default_soft_addtime;
1817 		comb->sadb_comb_hard_addtime = ipsecah_default_hard_addtime;
1818 		comb->sadb_comb_soft_usetime = ipsecah_default_soft_usetime;
1819 		comb->sadb_comb_hard_usetime = ipsecah_default_hard_usetime;
1820 
1821 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
1822 		if (--combs == 0)
1823 			return;	/* out of space.. */
1824 		comb++;
1825 	}
1826 }
1827 
1828 /*
1829  * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
1830  */
1831 static void
1832 ah_send_acquire(ipsacq_t *acqrec, mblk_t *extended)
1833 {
1834 	mblk_t *pfkeymp, *msgmp;
1835 	uint_t allocsize, combs;
1836 	sadb_msg_t *samsg;
1837 	sadb_prop_t *prop;
1838 	uint8_t *cur, *end;
1839 
1840 	AH_BUMP_STAT(acquire_requests);
1841 
1842 	ASSERT(MUTEX_HELD(&acqrec->ipsacq_lock));
1843 
1844 	pfkeymp = sadb_keysock_out(0);
1845 	if (pfkeymp == NULL) {
1846 		ah1dbg(("ah_send_acquire: 1st allocb() failed.\n"));
1847 		/* Just bail. */
1848 		goto done;
1849 	}
1850 
1851 	/*
1852 	 * First, allocate a basic ACQUIRE message.  Beyond that,
1853 	 * you need to extract certificate info from
1854 	 */
1855 	allocsize = sizeof (sadb_msg_t) + sizeof (sadb_address_t) +
1856 	    sizeof (sadb_address_t) + sizeof (sadb_prop_t);
1857 
1858 	switch (acqrec->ipsacq_addrfam) {
1859 	case AF_INET:
1860 		allocsize += 2 * sizeof (struct sockaddr_in);
1861 		break;
1862 	case AF_INET6:
1863 		allocsize += 2 * sizeof (struct sockaddr_in6);
1864 		break;
1865 	}
1866 
1867 	mutex_enter(&alg_lock);
1868 
1869 	combs = ipsec_nalgs[IPSEC_ALG_AUTH];
1870 
1871 	allocsize += combs * sizeof (sadb_comb_t);
1872 
1873 	/*
1874 	 * XXX If there are:
1875 	 *	certificate IDs
1876 	 *	proxy address
1877 	 *	<Others>
1878 	 * add additional allocation size.
1879 	 */
1880 
1881 	msgmp = allocb(allocsize, BPRI_HI);
1882 	if (msgmp == NULL) {
1883 		ah0dbg(("ah_send_acquire: 2nd allocb() failed.\n"));
1884 		/* Just bail. */
1885 		freemsg(pfkeymp);
1886 		pfkeymp = NULL;
1887 		goto done;
1888 	}
1889 
1890 	cur = msgmp->b_rptr;
1891 	end = cur + allocsize;
1892 	samsg = (sadb_msg_t *)cur;
1893 	pfkeymp->b_cont = msgmp;
1894 
1895 	/* Set up ACQUIRE. */
1896 	cur = sadb_setup_acquire(cur, end, acqrec);
1897 	if (cur == NULL) {
1898 		ah0dbg(("sadb_setup_acquire failed.\n"));
1899 		/* Just bail. */
1900 		freemsg(pfkeymp);
1901 		pfkeymp = NULL;
1902 		goto done;
1903 	}
1904 	samsg->sadb_msg_satype = SADB_SATYPE_AH;
1905 
1906 	/* XXX Insert proxy address information here. */
1907 
1908 	/* XXX Insert identity information here. */
1909 
1910 	/* XXXMLS Insert sensitivity information here. */
1911 
1912 	/* Insert proposal here. */
1913 
1914 	prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
1915 	ah_insert_prop(prop, acqrec, combs);
1916 	samsg->sadb_msg_len += prop->sadb_prop_len;
1917 	msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
1918 
1919 done:
1920 	mutex_exit(&alg_lock);
1921 
1922 	/*
1923 	 * Must mutex_exit() before sending PF_KEY message up, in
1924 	 * order to avoid recursive mutex_enter() if there are no registered
1925 	 * listeners.
1926 	 *
1927 	 * Once I've sent the message, I'm cool anyway.
1928 	 */
1929 	mutex_exit(&acqrec->ipsacq_lock);
1930 	if (ah_pfkey_q != NULL && pfkeymp != NULL) {
1931 		if (extended != NULL) {
1932 			putnext(ah_pfkey_q, extended);
1933 		}
1934 		putnext(ah_pfkey_q, pfkeymp);
1935 		return;
1936 	}
1937 	/* NOTE: freemsg() works for extended == NULL. */
1938 	freemsg(extended);
1939 	freemsg(pfkeymp);
1940 }
1941 
1942 /*
1943  * Handle the SADB_GETSPI message.  Create a larval SA.
1944  */
1945 static void
1946 ah_getspi(mblk_t *mp, keysock_in_t *ksi)
1947 {
1948 	ipsa_t *newbie, *target;
1949 	isaf_t *outbound, *inbound;
1950 	int rc, diagnostic;
1951 	sadb_sa_t *assoc;
1952 	keysock_out_t *kso;
1953 	uint32_t newspi;
1954 
1955 	/*
1956 	 * Randomly generate a proposed SPI value.
1957 	 */
1958 	(void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t));
1959 	newbie = sadb_getspi(ksi, newspi, &diagnostic);
1960 
1961 	if (newbie == NULL) {
1962 		sadb_pfkey_error(ah_pfkey_q, mp, ENOMEM, diagnostic,
1963 		    ksi->ks_in_serial);
1964 		return;
1965 	} else if (newbie == (ipsa_t *)-1) {
1966 		sadb_pfkey_error(ah_pfkey_q, mp, EINVAL, diagnostic,
1967 		    ksi->ks_in_serial);
1968 		return;
1969 	}
1970 
1971 	/*
1972 	 * XXX - We may randomly collide.  We really should recover from this.
1973 	 *	 Unfortunately, that could require spending way-too-much-time
1974 	 *	 in here.  For now, let the user retry.
1975 	 */
1976 
1977 	if (newbie->ipsa_addrfam == AF_INET6) {
1978 		outbound = OUTBOUND_BUCKET_V6(&ah_sadb.s_v6,
1979 		    *(uint32_t *)(newbie->ipsa_dstaddr));
1980 		inbound = INBOUND_BUCKET(&ah_sadb.s_v6, newbie->ipsa_spi);
1981 	} else {
1982 		outbound = OUTBOUND_BUCKET_V4(&ah_sadb.s_v4,
1983 		    *(uint32_t *)(newbie->ipsa_dstaddr));
1984 		inbound = INBOUND_BUCKET(&ah_sadb.s_v4, newbie->ipsa_spi);
1985 	}
1986 
1987 	mutex_enter(&outbound->isaf_lock);
1988 	mutex_enter(&inbound->isaf_lock);
1989 
1990 	/*
1991 	 * Check for collisions (i.e. did sadb_getspi() return with something
1992 	 * that already exists?).
1993 	 *
1994 	 * Try outbound first.  Even though SADB_GETSPI is traditionally
1995 	 * for inbound SAs, you never know what a user might do.
1996 	 */
1997 	target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
1998 	    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
1999 	if (target == NULL) {
2000 		target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
2001 		    newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
2002 		    newbie->ipsa_addrfam);
2003 	}
2004 
2005 	/*
2006 	 * I don't have collisions elsewhere!
2007 	 * (Nor will I because I'm still holding inbound/outbound locks.)
2008 	 */
2009 
2010 	if (target != NULL) {
2011 		rc = EEXIST;
2012 		IPSA_REFRELE(target);
2013 	} else {
2014 		/*
2015 		 * sadb_insertassoc() also checks for collisions, so
2016 		 * if there's a colliding larval entry, rc will be set
2017 		 * to EEXIST.
2018 		 */
2019 		rc = sadb_insertassoc(newbie, inbound);
2020 		(void) drv_getparm(TIME, &newbie->ipsa_hardexpiretime);
2021 		newbie->ipsa_hardexpiretime += ipsecah_larval_timeout;
2022 	}
2023 
2024 	/*
2025 	 * Can exit outbound mutex.  Hold inbound until we're done with
2026 	 * newbie.
2027 	 */
2028 	mutex_exit(&outbound->isaf_lock);
2029 
2030 	if (rc != 0) {
2031 		mutex_exit(&inbound->isaf_lock);
2032 		IPSA_REFRELE(newbie);
2033 		sadb_pfkey_error(ah_pfkey_q, mp, rc, SADB_X_DIAGNOSTIC_NONE,
2034 		    ksi->ks_in_serial);
2035 		return;
2036 	}
2037 
2038 	/* Can write here because I'm still holding the bucket lock. */
2039 	newbie->ipsa_type = SADB_SATYPE_AH;
2040 
2041 	/*
2042 	 * Construct successful return message.  We have one thing going
2043 	 * for us in PF_KEY v2.  That's the fact that
2044 	 *	sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
2045 	 */
2046 	assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
2047 	assoc->sadb_sa_exttype = SADB_EXT_SA;
2048 	assoc->sadb_sa_spi = newbie->ipsa_spi;
2049 	*((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
2050 	mutex_exit(&inbound->isaf_lock);
2051 
2052 	/* Convert KEYSOCK_IN to KEYSOCK_OUT. */
2053 	kso = (keysock_out_t *)ksi;
2054 	kso->ks_out_len = sizeof (*kso);
2055 	kso->ks_out_serial = ksi->ks_in_serial;
2056 	kso->ks_out_type = KEYSOCK_OUT;
2057 
2058 	/*
2059 	 * Can safely putnext() to ah_pfkey_q, because this is a turnaround
2060 	 * from the ah_pfkey_q.
2061 	 */
2062 	putnext(ah_pfkey_q, mp);
2063 }
2064 
2065 /*
2066  * IPv6 sends up the ICMP errors for validation and the removal of the AH
2067  * header.
2068  */
2069 static ipsec_status_t
2070 ah_icmp_error_v6(mblk_t *ipsec_mp)
2071 {
2072 	mblk_t *mp;
2073 	ip6_t *ip6h, *oip6h;
2074 	uint16_t hdr_length, ah_length;
2075 	uint8_t *nexthdrp;
2076 	ah_t *ah;
2077 	icmp6_t *icmp6;
2078 	isaf_t *isaf;
2079 	ipsa_t *assoc;
2080 	uint8_t *post_ah_ptr;
2081 
2082 	mp = ipsec_mp->b_cont;
2083 	ASSERT(mp->b_datap->db_type == M_CTL);
2084 
2085 	/*
2086 	 * Change the type to M_DATA till we finish pullups.
2087 	 */
2088 	mp->b_datap->db_type = M_DATA;
2089 
2090 	/*
2091 	 * Eat the cost of a pullupmsg() for now.  It makes the rest of this
2092 	 * code far less convoluted.
2093 	 */
2094 	if (!pullupmsg(mp, -1) ||
2095 	    !ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, &hdr_length,
2096 		&nexthdrp) ||
2097 	    mp->b_rptr + hdr_length + sizeof (icmp6_t) + sizeof (ip6_t) +
2098 	    sizeof (ah_t) > mp->b_wptr) {
2099 		IP_AH_BUMP_STAT(in_discards);
2100 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_ah_nomem,
2101 		    &ah_dropper);
2102 		return (IPSEC_STATUS_FAILED);
2103 	}
2104 
2105 	oip6h = (ip6_t *)mp->b_rptr;
2106 	icmp6 = (icmp6_t *)((uint8_t *)oip6h + hdr_length);
2107 	ip6h = (ip6_t *)(icmp6 + 1);
2108 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) {
2109 		IP_AH_BUMP_STAT(in_discards);
2110 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2111 		    &ipdrops_ah_bad_v6_hdrs, &ah_dropper);
2112 		return (IPSEC_STATUS_FAILED);
2113 	}
2114 	ah = (ah_t *)((uint8_t *)ip6h + hdr_length);
2115 
2116 	isaf = OUTBOUND_BUCKET_V6(&ah_sadb.s_v6, ip6h->ip6_dst);
2117 	mutex_enter(&isaf->isaf_lock);
2118 	assoc = ipsec_getassocbyspi(isaf, ah->ah_spi,
2119 	    (uint32_t *)&ip6h->ip6_src, (uint32_t *)&ip6h->ip6_dst, AF_INET6);
2120 	mutex_exit(&isaf->isaf_lock);
2121 
2122 	if (assoc == NULL) {
2123 		IP_AH_BUMP_STAT(lookup_failure);
2124 		IP_AH_BUMP_STAT(in_discards);
2125 		if (ipsecah_log_unknown_spi) {
2126 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2127 			    SL_CONSOLE | SL_WARN | SL_ERROR,
2128 			    "Bad ICMP message - No association for the "
2129 			    "attached AH header whose spi is 0x%x, "
2130 			    "sender is 0x%x\n",
2131 			    ah->ah_spi, &oip6h->ip6_src, AF_INET6);
2132 		}
2133 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_ah_no_sa,
2134 		    &ah_dropper);
2135 		return (IPSEC_STATUS_FAILED);
2136 	}
2137 
2138 	IPSA_REFRELE(assoc);
2139 
2140 	/*
2141 	 * There seems to be a valid association. If there is enough of AH
2142 	 * header remove it, otherwise bail.  One could check whether it has
2143 	 * complete AH header plus 8 bytes but it does not make sense if an
2144 	 * icmp error is returned for ICMP messages e.g ICMP time exceeded,
2145 	 * that are being sent up. Let the caller figure out.
2146 	 *
2147 	 * NOTE: ah_length is the number of 32 bit words minus 2.
2148 	 */
2149 	ah_length = (ah->ah_length << 2) + 8;
2150 	post_ah_ptr = (uint8_t *)ah + ah_length;
2151 
2152 	if (post_ah_ptr > mp->b_wptr) {
2153 		IP_AH_BUMP_STAT(in_discards);
2154 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2155 		    &ipdrops_ah_bad_length, &ah_dropper);
2156 		return (IPSEC_STATUS_FAILED);
2157 	}
2158 
2159 	ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - ah_length);
2160 	*nexthdrp = ah->ah_nexthdr;
2161 	ovbcopy(post_ah_ptr, ah,
2162 	    (size_t)((uintptr_t)mp->b_wptr - (uintptr_t)post_ah_ptr));
2163 	mp->b_wptr -= ah_length;
2164 	/* Rewhack to be an ICMP error. */
2165 	mp->b_datap->db_type = M_CTL;
2166 
2167 	return (IPSEC_STATUS_SUCCESS);
2168 }
2169 
2170 /*
2171  * IP sends up the ICMP errors for validation and the removal of
2172  * the AH header.
2173  */
2174 static ipsec_status_t
2175 ah_icmp_error_v4(mblk_t *ipsec_mp)
2176 {
2177 	mblk_t *mp;
2178 	mblk_t *mp1;
2179 	icmph_t *icmph;
2180 	int iph_hdr_length;
2181 	int hdr_length;
2182 	isaf_t *hptr;
2183 	ipsa_t *assoc;
2184 	int ah_length;
2185 	ipha_t *ipha;
2186 	ipha_t *oipha;
2187 	ah_t *ah;
2188 	uint32_t length;
2189 	int alloc_size;
2190 	uint8_t nexthdr;
2191 
2192 	mp = ipsec_mp->b_cont;
2193 	ASSERT(mp->b_datap->db_type == M_CTL);
2194 
2195 	/*
2196 	 * Change the type to M_DATA till we finish pullups.
2197 	 */
2198 	mp->b_datap->db_type = M_DATA;
2199 
2200 	oipha = ipha = (ipha_t *)mp->b_rptr;
2201 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
2202 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2203 
2204 	ipha = (ipha_t *)&icmph[1];
2205 	hdr_length = IPH_HDR_LENGTH(ipha);
2206 
2207 	/*
2208 	 * See if we have enough to locate the SPI
2209 	 */
2210 	if ((uchar_t *)ipha + hdr_length + 8 > mp->b_wptr) {
2211 		if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 8 -
2212 			    mp->b_rptr)) {
2213 			ipsec_rl_strlog(info.mi_idnum, 0, 0,
2214 			    SL_WARN | SL_ERROR,
2215 			    "ICMP error: Small AH header\n");
2216 			IP_AH_BUMP_STAT(in_discards);
2217 			ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2218 			    &ipdrops_ah_bad_length, &ah_dropper);
2219 			return (IPSEC_STATUS_FAILED);
2220 		}
2221 		icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2222 		ipha = (ipha_t *)&icmph[1];
2223 	}
2224 
2225 	ah = (ah_t *)((uint8_t *)ipha + hdr_length);
2226 	nexthdr = ah->ah_nexthdr;
2227 
2228 	hptr = OUTBOUND_BUCKET_V4(&ah_sadb.s_v4, ipha->ipha_dst);
2229 	mutex_enter(&hptr->isaf_lock);
2230 	assoc = ipsec_getassocbyspi(hptr, ah->ah_spi,
2231 	    (uint32_t *)&ipha->ipha_src, (uint32_t *)&ipha->ipha_dst, AF_INET);
2232 	mutex_exit(&hptr->isaf_lock);
2233 
2234 	if (assoc == NULL) {
2235 		IP_AH_BUMP_STAT(lookup_failure);
2236 		IP_AH_BUMP_STAT(in_discards);
2237 		if (ipsecah_log_unknown_spi) {
2238 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2239 			    SL_CONSOLE | SL_WARN | SL_ERROR,
2240 			    "Bad ICMP message - No association for the "
2241 			    "attached AH header whose spi is 0x%x, "
2242 			    "sender is 0x%x\n",
2243 			    ah->ah_spi, &oipha->ipha_src, AF_INET);
2244 		}
2245 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_ah_no_sa,
2246 		    &ah_dropper);
2247 		return (IPSEC_STATUS_FAILED);
2248 	}
2249 
2250 	IPSA_REFRELE(assoc);
2251 	/*
2252 	 * There seems to be a valid association. If there
2253 	 * is enough of AH header remove it, otherwise remove
2254 	 * as much as possible and send it back. One could check
2255 	 * whether it has complete AH header plus 8 bytes but it
2256 	 * does not make sense if an icmp error is returned for
2257 	 * ICMP messages e.g ICMP time exceeded, that are being
2258 	 * sent up. Let the caller figure out.
2259 	 *
2260 	 * NOTE: ah_length is the number of 32 bit words minus 2.
2261 	 */
2262 	ah_length = (ah->ah_length << 2) + 8;
2263 
2264 	if ((uchar_t *)ipha + hdr_length + ah_length > mp->b_wptr) {
2265 		if (mp->b_cont == NULL) {
2266 			/*
2267 			 * There is nothing to pullup. Just remove as
2268 			 * much as possible. This is a common case for
2269 			 * IPV4.
2270 			 */
2271 			ah_length = (mp->b_wptr - ((uchar_t *)ipha +
2272 			    hdr_length));
2273 			goto done;
2274 		}
2275 		/* Pullup the full ah header */
2276 		if (!pullupmsg(mp, (uchar_t *)ah + ah_length - mp->b_rptr)) {
2277 			/*
2278 			 * pullupmsg could have failed if there was not
2279 			 * enough to pullup or memory allocation failed.
2280 			 * We tried hard, give up now.
2281 			 */
2282 			IP_AH_BUMP_STAT(in_discards);
2283 			ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL,
2284 			    &ipdrops_ah_nomem, &ah_dropper);
2285 			return (IPSEC_STATUS_FAILED);
2286 		}
2287 		icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2288 		ipha = (ipha_t *)&icmph[1];
2289 	}
2290 done:
2291 	/*
2292 	 * Remove the AH header and change the protocol.
2293 	 * Don't update the spi fields in the ipsec_in
2294 	 * message as we are called just to validate the
2295 	 * message attached to the ICMP message.
2296 	 *
2297 	 * If we never pulled up since all of the message
2298 	 * is in one single mblk, we can't remove the AH header
2299 	 * by just setting the b_wptr to the beginning of the
2300 	 * AH header. We need to allocate a mblk that can hold
2301 	 * up until the inner IP header and copy them.
2302 	 */
2303 	alloc_size = iph_hdr_length + sizeof (icmph_t) + hdr_length;
2304 
2305 	if ((mp1 = allocb(alloc_size, BPRI_LO)) == NULL) {
2306 		IP_AH_BUMP_STAT(in_discards);
2307 		ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_ah_nomem,
2308 		    &ah_dropper);
2309 		return (IPSEC_STATUS_FAILED);
2310 	}
2311 	/* ICMP errors are M_CTL messages */
2312 	mp1->b_datap->db_type = M_CTL;
2313 	ipsec_mp->b_cont = mp1;
2314 	bcopy(mp->b_rptr, mp1->b_rptr, alloc_size);
2315 	mp1->b_wptr += alloc_size;
2316 
2317 	/*
2318 	 * Skip whatever we have copied and as much of AH header
2319 	 * possible. If we still have something left in the original
2320 	 * message, tag on.
2321 	 */
2322 	mp->b_rptr = (uchar_t *)ipha + hdr_length + ah_length;
2323 
2324 	if (mp->b_rptr != mp->b_wptr) {
2325 		mp1->b_cont = mp;
2326 	} else {
2327 		if (mp->b_cont != NULL)
2328 			mp1->b_cont = mp->b_cont;
2329 		freeb(mp);
2330 	}
2331 
2332 	ipha = (ipha_t *)(mp1->b_rptr + iph_hdr_length + sizeof (icmph_t));
2333 	ipha->ipha_protocol = nexthdr;
2334 	length = ntohs(ipha->ipha_length);
2335 	length -= ah_length;
2336 	ipha->ipha_length = htons((uint16_t)length);
2337 	ipha->ipha_hdr_checksum = 0;
2338 	ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
2339 
2340 	return (IPSEC_STATUS_SUCCESS);
2341 }
2342 
2343 /*
2344  * IP calls this to validate the ICMP errors that
2345  * we got from the network.
2346  */
2347 ipsec_status_t
2348 ipsecah_icmp_error(mblk_t *mp)
2349 {
2350 	ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr;
2351 
2352 	if (ii->ipsec_in_v4)
2353 		return (ah_icmp_error_v4(mp));
2354 	else
2355 		return (ah_icmp_error_v6(mp));
2356 }
2357 
2358 static int
2359 ah_fix_tlv_options_v6(uint8_t *oi_opt, uint8_t *pi_opt, uint_t ehdrlen,
2360     uint8_t hdr_type, boolean_t copy_always)
2361 {
2362 	uint8_t opt_type;
2363 	uint_t optlen;
2364 
2365 	ASSERT(hdr_type == IPPROTO_DSTOPTS || hdr_type == IPPROTO_HOPOPTS);
2366 
2367 	/*
2368 	 * Copy the next header and hdr ext. len of the HOP-by-HOP
2369 	 * and Destination option.
2370 	 */
2371 	*pi_opt++ = *oi_opt++;
2372 	*pi_opt++ = *oi_opt++;
2373 	ehdrlen -= 2;
2374 
2375 	/*
2376 	 * Now handle all the TLV encoded options.
2377 	 */
2378 	while (ehdrlen != 0) {
2379 		opt_type = *oi_opt;
2380 
2381 		if (opt_type == IP6OPT_PAD1) {
2382 			optlen = 1;
2383 		} else {
2384 			if (ehdrlen < 2)
2385 				goto bad_opt;
2386 			optlen = 2 + oi_opt[1];
2387 			if (optlen > ehdrlen)
2388 				goto bad_opt;
2389 		}
2390 		if (copy_always || !(opt_type & IP6OPT_MUTABLE)) {
2391 			bcopy(oi_opt, pi_opt, optlen);
2392 		} else {
2393 			if (optlen == 1) {
2394 				*pi_opt = 0;
2395 			} else {
2396 				/*
2397 				 * Copy the type and data length fields.
2398 				 * Zero the option data by skipping
2399 				 * option type and option data len
2400 				 * fields.
2401 				 */
2402 				*pi_opt = *oi_opt;
2403 				*(pi_opt + 1) = *(oi_opt + 1);
2404 				bzero(pi_opt + 2, optlen - 2);
2405 			}
2406 		}
2407 		ehdrlen -= optlen;
2408 		oi_opt += optlen;
2409 		pi_opt += optlen;
2410 	}
2411 	return (0);
2412 bad_opt:
2413 	return (-1);
2414 }
2415 
2416 /*
2417  * Construct a pseudo header for AH, processing all the options.
2418  *
2419  * oip6h is the IPv6 header of the incoming or outgoing packet.
2420  * ip6h is the pointer to the pseudo headers IPV6 header. All
2421  * the space needed for the options have been allocated including
2422  * the AH header.
2423  *
2424  * If copy_always is set, all the options that appear before AH are copied
2425  * blindly without checking for IP6OPT_MUTABLE. This is used by
2426  * ah_auth_out_done().  Please refer to that function for details.
2427  *
2428  * NOTE :
2429  *
2430  * *  AH header is never copied in this function even if copy_always
2431  *    is set. It just returns the ah_offset - offset of the AH header
2432  *    and the caller needs to do the copying. This is done so that we
2433  *    don't have pass extra arguments e.g. SA etc. and also,
2434  *    it is not needed when ah_auth_out_done is calling this function.
2435  */
2436 static uint_t
2437 ah_fix_phdr_v6(ip6_t *ip6h, ip6_t *oip6h, boolean_t outbound,
2438     boolean_t copy_always)
2439 {
2440 	uint8_t	*oi_opt;
2441 	uint8_t	*pi_opt;
2442 	uint8_t nexthdr;
2443 	uint8_t *prev_nexthdr;
2444 	ip6_hbh_t *hbhhdr;
2445 	ip6_dest_t *dsthdr = NULL;
2446 	ip6_rthdr0_t *rthdr;
2447 	int ehdrlen;
2448 	ah_t *ah;
2449 	int ret;
2450 
2451 	/*
2452 	 * In the outbound case for source route, ULP has already moved
2453 	 * the first hop, which is now in ip6_dst. We need to re-arrange
2454 	 * the header to make it look like how it would appear in the
2455 	 * receiver i.e
2456 	 *
2457 	 * Because of ip_massage_options_v6 the header looks like
2458 	 * this :
2459 	 *
2460 	 * ip6_src = S, ip6_dst = I1. followed by I2,I3,D.
2461 	 *
2462 	 * When it reaches the receiver, it would look like
2463 	 *
2464 	 * ip6_src = S, ip6_dst = D. followed by I1,I2,I3.
2465 	 *
2466 	 * NOTE : We assume that there are no problems with the options
2467 	 * as IP should have already checked this.
2468 	 */
2469 
2470 	oi_opt = (uchar_t *)&oip6h[1];
2471 	pi_opt = (uchar_t *)&ip6h[1];
2472 
2473 	/*
2474 	 * We set the prev_nexthdr properly in the pseudo header.
2475 	 * After we finish authentication and come back from the
2476 	 * algorithm module, pseudo header will become the real
2477 	 * IP header.
2478 	 */
2479 	prev_nexthdr = (uint8_t *)&ip6h->ip6_nxt;
2480 	nexthdr = oip6h->ip6_nxt;
2481 	/* Assume IP has already stripped it */
2482 	ASSERT(nexthdr != IPPROTO_FRAGMENT && nexthdr != IPPROTO_RAW);
2483 	ah = NULL;
2484 	dsthdr = NULL;
2485 	for (;;) {
2486 		switch (nexthdr) {
2487 		case IPPROTO_HOPOPTS:
2488 			hbhhdr = (ip6_hbh_t *)oi_opt;
2489 			nexthdr = hbhhdr->ip6h_nxt;
2490 			ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
2491 			ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2492 			    IPPROTO_HOPOPTS, copy_always);
2493 			/*
2494 			 * Return a zero offset indicating error if there
2495 			 * was error.
2496 			 */
2497 			if (ret == -1)
2498 				return (0);
2499 			hbhhdr = (ip6_hbh_t *)pi_opt;
2500 			prev_nexthdr = (uint8_t *)&hbhhdr->ip6h_nxt;
2501 			break;
2502 		case IPPROTO_ROUTING:
2503 			rthdr = (ip6_rthdr0_t *)oi_opt;
2504 			nexthdr = rthdr->ip6r0_nxt;
2505 			ehdrlen = 8 * (rthdr->ip6r0_len + 1);
2506 			if (!copy_always && outbound) {
2507 				int i, left;
2508 				ip6_rthdr0_t *prthdr;
2509 				in6_addr_t *ap, *pap;
2510 
2511 				left = rthdr->ip6r0_segleft;
2512 				prthdr = (ip6_rthdr0_t *)pi_opt;
2513 				pap = (in6_addr_t *)(prthdr + 1);
2514 				ap = (in6_addr_t *)(rthdr + 1);
2515 				/*
2516 				 * First eight bytes except seg_left
2517 				 * does not change en route.
2518 				 */
2519 				bcopy(oi_opt, pi_opt, 8);
2520 				prthdr->ip6r0_segleft = 0;
2521 				/*
2522 				 * First address has been moved to
2523 				 * the destination address of the
2524 				 * ip header by ip_massage_options_v6.
2525 				 * And the real destination address is
2526 				 * in the last address part of the
2527 				 * option.
2528 				 */
2529 				*pap = oip6h->ip6_dst;
2530 				for (i = 1; i < left - 1; i++)
2531 					pap[i] = ap[i - 1];
2532 				ip6h->ip6_dst = *(ap + left - 1);
2533 			} else {
2534 				bcopy(oi_opt, pi_opt, ehdrlen);
2535 			}
2536 			rthdr = (ip6_rthdr0_t *)pi_opt;
2537 			prev_nexthdr = (uint8_t *)&rthdr->ip6r0_nxt;
2538 			break;
2539 		case IPPROTO_DSTOPTS:
2540 			/*
2541 			 * Destination options are tricky.  If there is
2542 			 * a terminal (e.g. non-IPv6-extension) header
2543 			 * following the destination options, don't
2544 			 * reset prev_nexthdr or advance the AH insertion
2545 			 * point and just treat this as a terminal header.
2546 			 *
2547 			 * If this is an inbound packet, just deal with
2548 			 * it as is.
2549 			 */
2550 			dsthdr = (ip6_dest_t *)oi_opt;
2551 			/*
2552 			 * XXX I hope common-subexpression elimination
2553 			 * saves us the double-evaluate.
2554 			 */
2555 			if (outbound && dsthdr->ip6d_nxt != IPPROTO_ROUTING &&
2556 			    dsthdr->ip6d_nxt != IPPROTO_HOPOPTS)
2557 				goto terminal_hdr;
2558 			nexthdr = dsthdr->ip6d_nxt;
2559 			ehdrlen = 8 * (dsthdr->ip6d_len + 1);
2560 			ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2561 			    IPPROTO_DSTOPTS, copy_always);
2562 			/*
2563 			 * Return a zero offset indicating error if there
2564 			 * was error.
2565 			 */
2566 			if (ret == -1)
2567 				return (0);
2568 			break;
2569 		case IPPROTO_AH:
2570 			/*
2571 			 * Be conservative in what you send.  We shouldn't
2572 			 * see two same-scoped AH's in one packet.
2573 			 * (Inner-IP-scoped AH will be hit by terminal
2574 			 * header of IP or IPv6.)
2575 			 */
2576 			ASSERT(!outbound);
2577 			return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2578 		default:
2579 			ASSERT(outbound);
2580 terminal_hdr:
2581 			*prev_nexthdr = IPPROTO_AH;
2582 			ah = (ah_t *)pi_opt;
2583 			ah->ah_nexthdr = nexthdr;
2584 			return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2585 		}
2586 		pi_opt += ehdrlen;
2587 		oi_opt += ehdrlen;
2588 	}
2589 	/* NOTREACHED */
2590 }
2591 
2592 static boolean_t
2593 ah_finish_up(ah_t *phdr_ah, ah_t *inbound_ah, ipsa_t *assoc,
2594     int ah_data_sz, int ah_align_sz)
2595 {
2596 	int i;
2597 
2598 	/*
2599 	 * Padding :
2600 	 *
2601 	 * 1) Authentication data may have to be padded
2602 	 * before ICV calculation if ICV is not a multiple
2603 	 * of 64 bits. This padding is arbitrary and transmitted
2604 	 * with the packet at the end of the authentication data.
2605 	 * Payload length should include the padding bytes.
2606 	 *
2607 	 * 2) Explicit padding of the whole datagram may be
2608 	 * required by the algorithm which need not be
2609 	 * transmitted. It is assumed that this will be taken
2610 	 * care by the algorithm module.
2611 	 */
2612 	bzero(phdr_ah + 1, ah_data_sz);	/* Zero out ICV for pseudo-hdr. */
2613 
2614 	if (inbound_ah == NULL) {
2615 		/* Outbound AH datagram. */
2616 
2617 		phdr_ah->ah_length = (ah_align_sz >> 2) + 1;
2618 		phdr_ah->ah_reserved = 0;
2619 		phdr_ah->ah_spi = assoc->ipsa_spi;
2620 
2621 		phdr_ah->ah_replay =
2622 		    htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
2623 		if (phdr_ah->ah_replay == 0 && assoc->ipsa_replay_wsize != 0) {
2624 			/*
2625 			 * XXX We have replay counter wrapping.  We probably
2626 			 * want to nuke this SA (and its peer).
2627 			 */
2628 			ipsec_assocfailure(info.mi_idnum, 0, 0,
2629 			    SL_ERROR | SL_CONSOLE | SL_WARN,
2630 			    "Outbound AH SA (0x%x), dst %s has wrapped "
2631 			    "sequence.\n", phdr_ah->ah_spi,
2632 			    assoc->ipsa_dstaddr, assoc->ipsa_addrfam);
2633 
2634 			sadb_replay_delete(assoc);
2635 			/* Caller will free phdr_mp and return NULL. */
2636 			return (B_FALSE);
2637 		}
2638 
2639 		if (ah_data_sz != ah_align_sz) {
2640 			uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2641 			    ah_data_sz);
2642 
2643 			for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2644 				pad[i] = (uchar_t)i;	/* Fill the padding */
2645 			}
2646 		}
2647 	} else {
2648 		/* Inbound AH datagram. */
2649 		phdr_ah->ah_nexthdr = inbound_ah->ah_nexthdr;
2650 		phdr_ah->ah_length = inbound_ah->ah_length;
2651 		phdr_ah->ah_reserved = 0;
2652 		ASSERT(inbound_ah->ah_spi == assoc->ipsa_spi);
2653 		phdr_ah->ah_spi = inbound_ah->ah_spi;
2654 		phdr_ah->ah_replay = inbound_ah->ah_replay;
2655 
2656 		if (ah_data_sz != ah_align_sz) {
2657 			uchar_t *opad = ((uchar_t *)inbound_ah + sizeof (ah_t) +
2658 			    ah_data_sz);
2659 			uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2660 			    ah_data_sz);
2661 
2662 			for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2663 				pad[i] = opad[i];	/* Copy the padding */
2664 			}
2665 		}
2666 	}
2667 
2668 	return (B_TRUE);
2669 }
2670 
2671 /*
2672  * Called upon failing the inbound ICV check. The message passed as
2673  * argument is freed.
2674  */
2675 static void
2676 ah_log_bad_auth(mblk_t *ipsec_in)
2677 {
2678 	mblk_t *mp = ipsec_in->b_cont->b_cont;
2679 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr;
2680 	boolean_t isv4 = ii->ipsec_in_v4;
2681 	ipsa_t *assoc = ii->ipsec_in_ah_sa;
2682 	int af;
2683 	void *addr;
2684 
2685 	mp->b_rptr -= ii->ipsec_in_skip_len;
2686 
2687 	if (isv4) {
2688 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
2689 		addr = &ipha->ipha_dst;
2690 		af = AF_INET;
2691 	} else {
2692 		ip6_t *ip6h = (ip6_t *)mp->b_rptr;
2693 		addr = &ip6h->ip6_dst;
2694 		af = AF_INET6;
2695 	}
2696 
2697 	/*
2698 	 * Log the event. Don't print to the console, block
2699 	 * potential denial-of-service attack.
2700 	 */
2701 	AH_BUMP_STAT(bad_auth);
2702 
2703 	ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
2704 	    "AH Authentication failed spi %x, dst_addr %s",
2705 	    assoc->ipsa_spi, addr, af);
2706 
2707 	IP_AH_BUMP_STAT(in_discards);
2708 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_ah_bad_auth,
2709 	    &ah_dropper);
2710 }
2711 
2712 /*
2713  * Kernel crypto framework callback invoked after completion of async
2714  * crypto requests.
2715  */
2716 static void
2717 ah_kcf_callback(void *arg, int status)
2718 {
2719 	mblk_t *ipsec_mp = (mblk_t *)arg;
2720 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
2721 	boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN);
2722 
2723 	ASSERT(ipsec_mp->b_cont != NULL);
2724 
2725 	if (status == CRYPTO_SUCCESS) {
2726 		if (is_inbound) {
2727 			if (ah_auth_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS)
2728 				return;
2729 			/* finish IPsec processing */
2730 			ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL);
2731 		} else {
2732 			ipha_t *ipha;
2733 
2734 			if (ah_auth_out_done(ipsec_mp) != IPSEC_STATUS_SUCCESS)
2735 				return;
2736 
2737 			/* finish IPsec processing */
2738 			ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
2739 			if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
2740 				ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL,
2741 				    NULL);
2742 			} else {
2743 				ip6_t *ip6h = (ip6_t *)ipha;
2744 				ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h,
2745 				    NULL, NULL);
2746 			}
2747 		}
2748 
2749 	} else if (status == CRYPTO_INVALID_MAC) {
2750 		ah_log_bad_auth(ipsec_mp);
2751 
2752 	} else {
2753 		ah1dbg(("ah_kcf_callback: crypto failed with 0x%x\n", status));
2754 		AH_BUMP_STAT(crypto_failures);
2755 		if (is_inbound)
2756 			IP_AH_BUMP_STAT(in_discards);
2757 		else
2758 			AH_BUMP_STAT(out_discards);
2759 		ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL,
2760 		    &ipdrops_ah_crypto_failed, &ah_dropper);
2761 	}
2762 }
2763 
2764 /*
2765  * Invoked on kernel crypto failure during inbound and outbound processing.
2766  */
2767 static void
2768 ah_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc)
2769 {
2770 	ah1dbg(("crypto failed for %s AH with 0x%x\n",
2771 	    is_inbound ? "inbound" : "outbound", kef_rc));
2772 	ip_drop_packet(mp, is_inbound, NULL, NULL, &ipdrops_ah_crypto_failed,
2773 	    &ah_dropper);
2774 	AH_BUMP_STAT(crypto_failures);
2775 	if (is_inbound)
2776 		IP_AH_BUMP_STAT(in_discards);
2777 	else
2778 		AH_BUMP_STAT(out_discards);
2779 }
2780 
2781 /*
2782  * Helper macros for the ah_submit_req_{inbound,outbound}() functions.
2783  */
2784 
2785 #define	AH_INIT_CALLREQ(_cr) {						\
2786 	(_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED;		\
2787 	if (ipsec_algs_exec_mode[IPSEC_ALG_AUTH] == IPSEC_ALGS_EXEC_ASYNC) \
2788 		(_cr)->cr_flag |= CRYPTO_ALWAYS_QUEUE;			\
2789 	(_cr)->cr_callback_arg = ipsec_mp;				\
2790 	(_cr)->cr_callback_func = ah_kcf_callback;			\
2791 }
2792 
2793 #define	AH_INIT_CRYPTO_DATA(data, msglen, mblk) {			\
2794 	(data)->cd_format = CRYPTO_DATA_MBLK;				\
2795 	(data)->cd_mp = mblk;						\
2796 	(data)->cd_offset = 0;						\
2797 	(data)->cd_length = msglen;					\
2798 }
2799 
2800 #define	AH_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) {			\
2801 	(mac)->cd_format = CRYPTO_DATA_RAW;				\
2802 	(mac)->cd_offset = 0;						\
2803 	(mac)->cd_length = icvlen;					\
2804 	(mac)->cd_raw.iov_base = icvbuf;				\
2805 	(mac)->cd_raw.iov_len = icvlen;					\
2806 }
2807 
2808 /*
2809  * Submit an inbound packet for processing by the crypto framework.
2810  */
2811 static ipsec_status_t
2812 ah_submit_req_inbound(mblk_t *ipsec_mp, size_t skip_len, uint32_t ah_offset,
2813     ipsa_t *assoc)
2814 {
2815 	int kef_rc;
2816 	mblk_t *phdr_mp;
2817 	crypto_call_req_t call_req;
2818 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr;
2819 	uint_t icv_len = assoc->ipsa_mac_len;
2820 	crypto_ctx_template_t ctx_tmpl;
2821 
2822 	phdr_mp = ipsec_mp->b_cont;
2823 	ASSERT(phdr_mp != NULL);
2824 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
2825 
2826 	/* init arguments for the crypto framework */
2827 	AH_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data, AH_MSGSIZE(phdr_mp),
2828 	    phdr_mp);
2829 
2830 	AH_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac, icv_len,
2831 	    (char *)phdr_mp->b_cont->b_rptr - skip_len + ah_offset +
2832 	    sizeof (ah_t));
2833 
2834 	AH_INIT_CALLREQ(&call_req);
2835 
2836 	ii->ipsec_in_skip_len = skip_len;
2837 
2838 	IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, ctx_tmpl);
2839 
2840 	/* call KEF to do the MAC operation */
2841 	kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
2842 	    &ii->ipsec_in_crypto_data, &assoc->ipsa_kcfauthkey, ctx_tmpl,
2843 	    &ii->ipsec_in_crypto_mac, &call_req);
2844 
2845 	switch (kef_rc) {
2846 	case CRYPTO_SUCCESS:
2847 		AH_BUMP_STAT(crypto_sync);
2848 		return (ah_auth_in_done(ipsec_mp));
2849 	case CRYPTO_QUEUED:
2850 		/* ah_callback() will be invoked on completion */
2851 		AH_BUMP_STAT(crypto_async);
2852 		return (IPSEC_STATUS_PENDING);
2853 	case CRYPTO_INVALID_MAC:
2854 		AH_BUMP_STAT(crypto_sync);
2855 		ah_log_bad_auth(ipsec_mp);
2856 		return (IPSEC_STATUS_FAILED);
2857 	}
2858 
2859 	ah_crypto_failed(ipsec_mp, B_TRUE, kef_rc);
2860 	return (IPSEC_STATUS_FAILED);
2861 }
2862 
2863 /*
2864  * Submit an outbound packet for processing by the crypto framework.
2865  */
2866 static ipsec_status_t
2867 ah_submit_req_outbound(mblk_t *ipsec_mp, size_t skip_len, ipsa_t *assoc)
2868 {
2869 	int kef_rc;
2870 	mblk_t *phdr_mp;
2871 	crypto_call_req_t call_req;
2872 	ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr;
2873 	uint_t icv_len = assoc->ipsa_mac_len;
2874 
2875 	phdr_mp = ipsec_mp->b_cont;
2876 	ASSERT(phdr_mp != NULL);
2877 	ASSERT(io->ipsec_out_type == IPSEC_OUT);
2878 
2879 	/* init arguments for the crypto framework */
2880 	AH_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data, AH_MSGSIZE(phdr_mp),
2881 	    phdr_mp);
2882 
2883 	AH_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac, icv_len,
2884 	    (char *)phdr_mp->b_wptr);
2885 
2886 	AH_INIT_CALLREQ(&call_req);
2887 
2888 	io->ipsec_out_skip_len = skip_len;
2889 
2890 	ASSERT(io->ipsec_out_ah_sa != NULL);
2891 
2892 	/* call KEF to do the MAC operation */
2893 	kef_rc = crypto_mac(&assoc->ipsa_amech, &io->ipsec_out_crypto_data,
2894 	    &assoc->ipsa_kcfauthkey, assoc->ipsa_authtmpl,
2895 	    &io->ipsec_out_crypto_mac, &call_req);
2896 
2897 	switch (kef_rc) {
2898 	case CRYPTO_SUCCESS:
2899 		AH_BUMP_STAT(crypto_sync);
2900 		return (ah_auth_out_done(ipsec_mp));
2901 	case CRYPTO_QUEUED:
2902 		/* ah_callback() will be invoked on completion */
2903 		AH_BUMP_STAT(crypto_async);
2904 		return (IPSEC_STATUS_PENDING);
2905 	}
2906 
2907 	ah_crypto_failed(ipsec_mp, B_FALSE, kef_rc);
2908 	return (IPSEC_STATUS_FAILED);
2909 }
2910 
2911 /*
2912  * This function constructs a pseudo header by looking at the IP header
2913  * and options if any. This is called for both outbound and inbound,
2914  * before computing the ICV.
2915  */
2916 static mblk_t *
2917 ah_process_ip_options_v6(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
2918     uint_t ah_data_sz, boolean_t outbound)
2919 {
2920 	ip6_t	*ip6h;
2921 	ip6_t	*oip6h;
2922 	mblk_t 	*phdr_mp;
2923 	int option_length;
2924 	uint_t	ah_align_sz;
2925 	uint_t ah_offset;
2926 	int hdr_size;
2927 
2928 	/*
2929 	 * Allocate space for the authentication data also. It is
2930 	 * useful both during the ICV calculation where we need to
2931 	 * feed in zeroes and while sending the datagram back to IP
2932 	 * where we will be using the same space.
2933 	 *
2934 	 * We need to allocate space for padding bytes if it is not
2935 	 * a multiple of IPV6_PADDING_ALIGN.
2936 	 *
2937 	 * In addition, we allocate space for the ICV computed by
2938 	 * the kernel crypto framework, saving us a separate kmem
2939 	 * allocation down the road.
2940 	 */
2941 
2942 	ah_align_sz = P2ALIGN(ah_data_sz + IPV6_PADDING_ALIGN - 1,
2943 	    IPV6_PADDING_ALIGN);
2944 
2945 	ASSERT(ah_align_sz >= ah_data_sz);
2946 
2947 	hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
2948 	option_length = hdr_size - IPV6_HDR_LEN;
2949 
2950 	/* This was not included in ipsec_ah_get_hdr_size_v6() */
2951 	hdr_size += (sizeof (ah_t) + ah_align_sz);
2952 
2953 	if (!outbound && (MBLKL(mp) < hdr_size)) {
2954 		/*
2955 		 * We have post-AH header options in a separate mblk,
2956 		 * a pullup is required.
2957 		 */
2958 		if (!pullupmsg(mp, hdr_size))
2959 			return (NULL);
2960 	}
2961 
2962 	if ((phdr_mp = allocb(hdr_size + ah_data_sz, BPRI_HI)) == NULL) {
2963 		return (NULL);
2964 	}
2965 
2966 	oip6h = (ip6_t *)mp->b_rptr;
2967 
2968 	/*
2969 	 * Form the basic IP header first. Zero out the header
2970 	 * so that the mutable fields are zeroed out.
2971 	 */
2972 	ip6h = (ip6_t *)phdr_mp->b_rptr;
2973 	bzero(ip6h, sizeof (ip6_t));
2974 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
2975 
2976 	if (outbound) {
2977 		/*
2978 		 * Include the size of AH and authentication data.
2979 		 * This is how our recipient would compute the
2980 		 * authentication data. Look at what we do in the
2981 		 * inbound case below.
2982 		 */
2983 		ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) +
2984 		    sizeof (ah_t) + ah_align_sz);
2985 	} else {
2986 		ip6h->ip6_plen = oip6h->ip6_plen;
2987 	}
2988 
2989 	ip6h->ip6_src = oip6h->ip6_src;
2990 	ip6h->ip6_dst = oip6h->ip6_dst;
2991 
2992 	*length_to_skip = IPV6_HDR_LEN;
2993 	if (option_length == 0) {
2994 		/* Form the AH header */
2995 		ip6h->ip6_nxt = IPPROTO_AH;
2996 		((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
2997 		ah_offset = *length_to_skip;
2998 	} else {
2999 		ip6h->ip6_nxt = oip6h->ip6_nxt;
3000 		/* option_length does not include the AH header's size */
3001 		*length_to_skip += option_length;
3002 
3003 		ah_offset = ah_fix_phdr_v6(ip6h, oip6h, outbound, B_FALSE);
3004 		if (ah_offset == 0) {
3005 			ip_drop_packet(phdr_mp, !outbound, NULL, NULL,
3006 			    &ipdrops_ah_bad_v6_hdrs, &ah_dropper);
3007 			return (NULL);
3008 		}
3009 	}
3010 
3011 	if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)),
3012 	    (outbound ? NULL : ((ah_t *)((uint8_t *)oip6h + ah_offset))),
3013 	    assoc, ah_data_sz, ah_align_sz)) {
3014 		freeb(phdr_mp);
3015 		/*
3016 		 * Returning NULL will tell the caller to
3017 		 * IPSA_REFELE(), free the memory, etc.
3018 		 */
3019 		return (NULL);
3020 	}
3021 
3022 	phdr_mp->b_wptr = ((uint8_t *)ip6h + ah_offset + sizeof (ah_t) +
3023 	    ah_align_sz);
3024 	if (!outbound)
3025 		*length_to_skip += sizeof (ah_t) + ah_align_sz;
3026 	return (phdr_mp);
3027 }
3028 
3029 /*
3030  * This function constructs a pseudo header by looking at the IP header
3031  * and options if any. This is called for both outbound and inbound,
3032  * before computing the ICV.
3033  */
3034 static mblk_t *
3035 ah_process_ip_options_v4(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
3036     uint_t ah_data_sz, boolean_t outbound)
3037 {
3038 	ipoptp_t opts;
3039 	uint32_t option_length;
3040 	ipha_t	*ipha;
3041 	ipha_t	*oipha;
3042 	mblk_t 	*phdr_mp;
3043 	int	 size;
3044 	uchar_t	*optptr;
3045 	uint8_t optval;
3046 	uint8_t optlen;
3047 	ipaddr_t dst;
3048 	uint32_t v_hlen_tos_len;
3049 	int ip_hdr_length;
3050 	uint_t	ah_align_sz;
3051 	uint32_t off;
3052 
3053 #ifdef	_BIG_ENDIAN
3054 #define	V_HLEN	(v_hlen_tos_len >> 24)
3055 #else
3056 #define	V_HLEN	(v_hlen_tos_len & 0xFF)
3057 #endif
3058 
3059 	oipha = (ipha_t *)mp->b_rptr;
3060 	v_hlen_tos_len = ((uint32_t *)oipha)[0];
3061 
3062 	/*
3063 	 * Allocate space for the authentication data also. It is
3064 	 * useful both during the ICV calculation where we need to
3065 	 * feed in zeroes and while sending the datagram back to IP
3066 	 * where we will be using the same space.
3067 	 *
3068 	 * We need to allocate space for padding bytes if it is not
3069 	 * a multiple of IPV4_PADDING_ALIGN.
3070 	 *
3071 	 * In addition, we allocate space for the ICV computed by
3072 	 * the kernel crypto framework, saving us a separate kmem
3073 	 * allocation down the road.
3074 	 */
3075 
3076 	ah_align_sz = P2ALIGN(ah_data_sz + IPV4_PADDING_ALIGN - 1,
3077 	    IPV4_PADDING_ALIGN);
3078 
3079 	ASSERT(ah_align_sz >= ah_data_sz);
3080 
3081 	size = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz +
3082 	    ah_data_sz;
3083 
3084 	if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
3085 		option_length = oipha->ipha_version_and_hdr_length -
3086 		    (uint8_t)((IP_VERSION << 4) +
3087 		    IP_SIMPLE_HDR_LENGTH_IN_WORDS);
3088 		option_length <<= 2;
3089 		size += option_length;
3090 	}
3091 
3092 	if ((phdr_mp = allocb(size, BPRI_HI)) == NULL) {
3093 		return (NULL);
3094 	}
3095 
3096 	/*
3097 	 * Form the basic IP header first.
3098 	 */
3099 	ipha = (ipha_t *)phdr_mp->b_rptr;
3100 	ipha->ipha_version_and_hdr_length = oipha->ipha_version_and_hdr_length;
3101 	ipha->ipha_type_of_service = 0;
3102 
3103 	if (outbound) {
3104 		/*
3105 		 * Include the size of AH and authentication data.
3106 		 * This is how our recipient would compute the
3107 		 * authentication data. Look at what we do in the
3108 		 * inbound case below.
3109 		 */
3110 		ipha->ipha_length = ntohs(htons(oipha->ipha_length) +
3111 		    sizeof (ah_t) + ah_align_sz);
3112 	} else {
3113 		ipha->ipha_length = oipha->ipha_length;
3114 	}
3115 
3116 	ipha->ipha_ident = oipha->ipha_ident;
3117 	ipha->ipha_fragment_offset_and_flags = 0;
3118 	ipha->ipha_ttl = 0;
3119 	ipha->ipha_protocol = IPPROTO_AH;
3120 	ipha->ipha_hdr_checksum = 0;
3121 	ipha->ipha_src = oipha->ipha_src;
3122 	ipha->ipha_dst = dst = oipha->ipha_dst;
3123 
3124 	/*
3125 	 * If there is no option to process return now.
3126 	 */
3127 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
3128 
3129 	if (V_HLEN == IP_SIMPLE_HDR_VERSION) {
3130 		/* Form the AH header */
3131 		goto ah_hdr;
3132 	}
3133 
3134 	ip_hdr_length += option_length;
3135 
3136 	/*
3137 	 * We have options. In the outbound case for source route,
3138 	 * ULP has already moved the first hop, which is now in
3139 	 * ipha_dst. We need the final destination for the calculation
3140 	 * of authentication data. And also make sure that mutable
3141 	 * and experimental fields are zeroed out in the IP options.
3142 	 */
3143 
3144 	bcopy(&oipha[1], &ipha[1], option_length);
3145 
3146 	for (optval = ipoptp_first(&opts, ipha);
3147 	    optval != IPOPT_EOL;
3148 	    optval = ipoptp_next(&opts)) {
3149 		optptr = opts.ipoptp_cur;
3150 		optlen = opts.ipoptp_len;
3151 		switch (optval) {
3152 		case IPOPT_EXTSEC:
3153 		case IPOPT_COMSEC:
3154 		case IPOPT_RA:
3155 		case IPOPT_SDMDD:
3156 		case IPOPT_SECURITY:
3157 			/*
3158 			 * These options are Immutable, leave them as-is.
3159 			 * Note that IPOPT_NOP is also Immutable, but it
3160 			 * was skipped by ipoptp_next() and thus remains
3161 			 * intact in the header.
3162 			 */
3163 			break;
3164 		case IPOPT_SSRR:
3165 		case IPOPT_LSRR:
3166 			if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0)
3167 				goto bad_ipv4opt;
3168 			/*
3169 			 * These two are mutable and will be zeroed, but
3170 			 * first get the final destination.
3171 			 */
3172 			off = optptr[IPOPT_OFFSET];
3173 			/*
3174 			 * If one of the conditions is true, it means
3175 			 * end of options and dst already has the right
3176 			 * value. So, just fall through.
3177 			 */
3178 			if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
3179 				off = optlen - IP_ADDR_LEN;
3180 				bcopy(&optptr[off], &dst, IP_ADDR_LEN);
3181 			}
3182 			/* FALLTHRU */
3183 		case IPOPT_RR:
3184 		case IPOPT_TS:
3185 		case IPOPT_SATID:
3186 		default:
3187 			/*
3188 			 * optlen should include from the beginning of an
3189 			 * option.
3190 			 * NOTE : Stream Identifier Option (SID): RFC 791
3191 			 * shows the bit pattern of optlen as 2 and documents
3192 			 * the length as 4. We assume it to be 2 here.
3193 			 */
3194 			bzero(optptr, optlen);
3195 			break;
3196 		}
3197 	}
3198 
3199 	if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
3200 bad_ipv4opt:
3201 		ah1dbg(("AH : bad IPv4 option"));
3202 		freeb(phdr_mp);
3203 		return (NULL);
3204 	}
3205 
3206 	/*
3207 	 * Don't change ipha_dst for an inbound datagram as it points
3208 	 * to the right value. Only for the outbound with LSRR/SSRR,
3209 	 * because of ip_massage_options called by the ULP, ipha_dst
3210 	 * points to the first hop and we need to use the final
3211 	 * destination for computing the ICV.
3212 	 */
3213 
3214 	if (outbound)
3215 		ipha->ipha_dst = dst;
3216 ah_hdr:
3217 	((ah_t *)((uint8_t *)ipha + ip_hdr_length))->ah_nexthdr =
3218 	    oipha->ipha_protocol;
3219 	if (!ah_finish_up(((ah_t *)((uint8_t *)ipha + ip_hdr_length)),
3220 	    (outbound ? NULL : ((ah_t *)((uint8_t *)oipha + ip_hdr_length))),
3221 	    assoc, ah_data_sz, ah_align_sz)) {
3222 		freeb(phdr_mp);
3223 		/*
3224 		 * Returning NULL will tell the caller to IPSA_REFELE(), free
3225 		 * the memory, etc.
3226 		 */
3227 		return (NULL);
3228 	}
3229 
3230 	phdr_mp->b_wptr = ((uchar_t *)ipha + ip_hdr_length +
3231 	    sizeof (ah_t) + ah_align_sz);
3232 
3233 	ASSERT(phdr_mp->b_wptr <= phdr_mp->b_datap->db_lim);
3234 	if (outbound)
3235 		*length_to_skip = ip_hdr_length;
3236 	else
3237 		*length_to_skip = ip_hdr_length + sizeof (ah_t) + ah_align_sz;
3238 	return (phdr_mp);
3239 }
3240 
3241 /*
3242  * Authenticate an outbound datagram. This function is called
3243  * whenever IP sends an outbound datagram that needs authentication.
3244  */
3245 static ipsec_status_t
3246 ah_outbound(mblk_t *ipsec_out)
3247 {
3248 	mblk_t *mp;
3249 	mblk_t *phdr_mp;
3250 	ipsec_out_t *oi;
3251 	ipsa_t *assoc;
3252 	int length_to_skip;
3253 	uint_t ah_align_sz;
3254 	uint_t age_bytes;
3255 
3256 	/*
3257 	 * Construct the chain of mblks
3258 	 *
3259 	 * IPSEC_OUT->PSEUDO_HDR->DATA
3260 	 *
3261 	 * one by one.
3262 	 */
3263 
3264 	AH_BUMP_STAT(out_requests);
3265 
3266 	ASSERT(ipsec_out->b_datap->db_type == M_CTL);
3267 
3268 	ASSERT(MBLKL(ipsec_out) >= sizeof (ipsec_info_t));
3269 
3270 	mp = ipsec_out->b_cont;
3271 	oi = (ipsec_out_t *)ipsec_out->b_rptr;
3272 
3273 	ASSERT(mp->b_datap->db_type == M_DATA);
3274 
3275 	assoc = oi->ipsec_out_ah_sa;
3276 	ASSERT(assoc != NULL);
3277 	if (assoc->ipsa_usetime == 0)
3278 		ah_set_usetime(assoc, B_FALSE);
3279 
3280 	/*
3281 	 * Age SA according to number of bytes that will be sent after
3282 	 * adding the AH header, ICV, and padding to the packet.
3283 	 */
3284 
3285 	if (oi->ipsec_out_v4) {
3286 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
3287 		ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3288 		    IPV4_PADDING_ALIGN - 1, IPV4_PADDING_ALIGN);
3289 		age_bytes = ntohs(ipha->ipha_length) + sizeof (ah_t) +
3290 		    ah_align_sz;
3291 	} else {
3292 		ip6_t *ip6h = (ip6_t *)mp->b_rptr;
3293 		ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3294 		    IPV6_PADDING_ALIGN - 1, IPV6_PADDING_ALIGN);
3295 		age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
3296 			sizeof (ah_t) + ah_align_sz;
3297 	}
3298 
3299 	if (!ah_age_bytes(assoc, age_bytes, B_FALSE)) {
3300 		/* rig things as if ipsec_getassocbyconn() failed */
3301 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
3302 		    "AH association 0x%x, dst %s had bytes expire.\n",
3303 		    ntohl(assoc->ipsa_spi), assoc->ipsa_dstaddr, AF_INET);
3304 		freemsg(ipsec_out);
3305 		return (IPSEC_STATUS_FAILED);
3306 	}
3307 
3308 	if (oi->ipsec_out_is_capab_ill) {
3309 		ah3dbg(("ah_outbound: pkt can be accelerated\n"));
3310 		if (oi->ipsec_out_v4)
3311 			return (ah_outbound_accelerated_v4(ipsec_out, assoc));
3312 		else
3313 			return (ah_outbound_accelerated_v6(ipsec_out, assoc));
3314 	}
3315 	AH_BUMP_STAT(noaccel);
3316 
3317 	/*
3318 	 * Insert pseudo header:
3319 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3320 	 */
3321 
3322 	if (oi->ipsec_out_v4) {
3323 		phdr_mp = ah_process_ip_options_v4(mp, assoc, &length_to_skip,
3324 		    assoc->ipsa_mac_len, B_TRUE);
3325 	} else {
3326 		phdr_mp = ah_process_ip_options_v6(mp, assoc, &length_to_skip,
3327 		    assoc->ipsa_mac_len, B_TRUE);
3328 	}
3329 
3330 	if (phdr_mp == NULL) {
3331 		AH_BUMP_STAT(out_discards);
3332 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
3333 		    &ipdrops_ah_bad_v4_opts, &ah_dropper);
3334 		return (IPSEC_STATUS_FAILED);
3335 	}
3336 
3337 	ipsec_out->b_cont = phdr_mp;
3338 	phdr_mp->b_cont = mp;
3339 	mp->b_rptr += length_to_skip;
3340 
3341 	/*
3342 	 * At this point ipsec_out points to the IPSEC_OUT, new_mp
3343 	 * points to an mblk containing the pseudo header (IP header,
3344 	 * AH header, and ICV with mutable fields zero'ed out).
3345 	 * mp points to the mblk containing the ULP data. The original
3346 	 * IP header is kept before the ULP data in mp.
3347 	 */
3348 
3349 	/* submit MAC request to KCF */
3350 	return (ah_submit_req_outbound(ipsec_out, length_to_skip, assoc));
3351 }
3352 
3353 static ipsec_status_t
3354 ah_inbound(mblk_t *ipsec_in_mp, void *arg)
3355 {
3356 	mblk_t *data_mp = ipsec_in_mp->b_cont;
3357 	ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr;
3358 	ah_t *ah = (ah_t *)arg;
3359 	ipsa_t *assoc = ii->ipsec_in_ah_sa;
3360 	int length_to_skip;
3361 	int ah_length;
3362 	mblk_t *phdr_mp;
3363 	uint32_t ah_offset;
3364 
3365 	ASSERT(assoc != NULL);
3366 	if (assoc->ipsa_usetime == 0)
3367 		ah_set_usetime(assoc, B_TRUE);
3368 
3369 	/*
3370 	 * We may wish to check replay in-range-only here as an optimization.
3371 	 * Include the reality check of ipsa->ipsa_replay >
3372 	 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
3373 	 * where N == ipsa->ipsa_replay_wsize.
3374 	 *
3375 	 * Another check that may come here later is the "collision" check.
3376 	 * If legitimate packets flow quickly enough, this won't be a problem,
3377 	 * but collisions may cause authentication algorithm crunching to
3378 	 * take place when it doesn't need to.
3379 	 */
3380 	if (!sadb_replay_peek(assoc, ah->ah_replay)) {
3381 		AH_BUMP_STAT(replay_early_failures);
3382 		IP_AH_BUMP_STAT(in_discards);
3383 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3384 		    &ipdrops_ah_early_replay, &ah_dropper);
3385 		return (IPSEC_STATUS_FAILED);
3386 	}
3387 
3388 	/*
3389 	 * The offset of the AH header can be computed from its pointer
3390 	 * within the data mblk, which was pulled up until the AH header
3391 	 * by ipsec_inbound_ah_sa() during SA selection.
3392 	 */
3393 	ah_offset = (uchar_t *)ah - data_mp->b_rptr;
3394 
3395 	/*
3396 	 * Has this packet already been processed by a hardware
3397 	 * IPsec accelerator?
3398 	 */
3399 	if (ii->ipsec_in_accelerated) {
3400 		ah3dbg(("ah_inbound_v6: pkt processed by ill=%d isv6=%d\n",
3401 		    ii->ipsec_in_ill_index, !ii->ipsec_in_v4));
3402 		return (ah_inbound_accelerated(ipsec_in_mp, ii->ipsec_in_v4,
3403 		    assoc, ah_offset));
3404 	}
3405 	AH_BUMP_STAT(noaccel);
3406 
3407 	/*
3408 	 * We need to pullup until the ICV before we call
3409 	 * ah_process_ip_options_v6.
3410 	 */
3411 	ah_length = (ah->ah_length << 2) + 8;
3412 
3413 	/*
3414 	 * NOTE : If we want to use any field of IP/AH header, you need
3415 	 * to re-assign following the pullup.
3416 	 */
3417 	if (((uchar_t *)ah + ah_length) > data_mp->b_wptr) {
3418 		if (!pullupmsg(data_mp, (uchar_t *)ah + ah_length -
3419 		    data_mp->b_rptr)) {
3420 			(void) ipsec_rl_strlog(info.mi_idnum, 0, 0,
3421 			    SL_WARN | SL_ERROR,
3422 			    "ah_inbound: Small AH header\n");
3423 			IP_AH_BUMP_STAT(in_discards);
3424 			ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3425 			    &ipdrops_ah_nomem, &ah_dropper);
3426 			return (IPSEC_STATUS_FAILED);
3427 		}
3428 	}
3429 
3430 	/*
3431 	 * Insert pseudo header:
3432 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3433 	 */
3434 	if (ii->ipsec_in_v4) {
3435 		phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
3436 		    &length_to_skip, assoc->ipsa_mac_len, B_FALSE);
3437 	} else {
3438 		phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
3439 		    &length_to_skip, assoc->ipsa_mac_len, B_FALSE);
3440 	}
3441 
3442 	if (phdr_mp == NULL) {
3443 		IP_AH_BUMP_STAT(in_discards);
3444 		ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL,
3445 		    ii->ipsec_in_v4 ? &ipdrops_ah_bad_v4_opts :
3446 		    &ipdrops_ah_bad_v6_hdrs, &ah_dropper);
3447 		return (IPSEC_STATUS_FAILED);
3448 	}
3449 
3450 	ipsec_in_mp->b_cont = phdr_mp;
3451 	phdr_mp->b_cont = data_mp;
3452 	data_mp->b_rptr += length_to_skip;
3453 
3454 	/* submit request to KCF */
3455 	return (ah_submit_req_inbound(ipsec_in_mp, length_to_skip, ah_offset,
3456 	    assoc));
3457 }
3458 
3459 /*
3460  * ah_inbound_accelerated:
3461  * Called from ah_inbound() to process IPsec packets that have been
3462  * accelerated by hardware.
3463  *
3464  * Basically does what ah_auth_in_done() with some changes since
3465  * no pseudo-headers are involved, i.e. the passed message is a
3466  * IPSEC_INFO->DATA.
3467  *
3468  * It is assumed that only packets that have been successfully
3469  * processed by the adapter come here.
3470  *
3471  * 1. get algorithm structure corresponding to association
3472  * 2. calculate pointers to authentication header and ICV
3473  * 3. compare ICV in AH header with ICV in data attributes
3474  *    3.1 if different:
3475  *	  3.1.1 generate error
3476  *        3.1.2 discard message
3477  *    3.2 if ICV matches:
3478  *	  3.2.1 check replay
3479  *        3.2.2 remove AH header
3480  *        3.2.3 age SA byte
3481  *        3.2.4 send to IP
3482  */
3483 ipsec_status_t
3484 ah_inbound_accelerated(mblk_t *ipsec_in, boolean_t isv4, ipsa_t *assoc,
3485     uint32_t ah_offset)
3486 {
3487 	mblk_t *mp;
3488 	ipha_t *ipha;
3489 	ah_t *ah;
3490 	ipsec_in_t *ii;
3491 	uint32_t icv_len;
3492 	uint32_t align_len;
3493 	uint32_t age_bytes;
3494 	ip6_t *ip6h;
3495 	uint8_t *in_icv;
3496 	mblk_t *hada_mp;
3497 	uint32_t next_hdr;
3498 	da_ipsec_t *hada;
3499 	kstat_named_t *counter;
3500 
3501 	AH_BUMP_STAT(in_accelerated);
3502 
3503 	ii = (ipsec_in_t *)ipsec_in->b_rptr;
3504 	mp = ipsec_in->b_cont;
3505 	hada_mp = ii->ipsec_in_da;
3506 	ASSERT(hada_mp != NULL);
3507 	hada = (da_ipsec_t *)hada_mp->b_rptr;
3508 
3509 	/*
3510 	 * We only support one level of decapsulation in hardware, so
3511 	 * nuke the pointer.
3512 	 */
3513 	ii->ipsec_in_da = NULL;
3514 	ii->ipsec_in_accelerated = B_FALSE;
3515 
3516 	/*
3517 	 * Extract ICV length from attributes M_CTL and sanity check
3518 	 * its value. We allow the mblk to be smaller than da_ipsec_t
3519 	 * for a small ICV, as long as the entire ICV fits within the mblk.
3520 	 * Also ensures that the ICV length computed by Provider
3521 	 * corresponds to the ICV length of the algorithm specified by the SA.
3522 	 */
3523 	icv_len = hada->da_icv_len;
3524 	if ((icv_len != assoc->ipsa_mac_len) ||
3525 	    (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) <
3526 		(sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) {
3527 		ah0dbg(("ah_inbound_accelerated: "
3528 		    "ICV len (%u) incorrect or mblk too small (%u)\n",
3529 		    icv_len, (uint32_t)(MBLKL(hada_mp))));
3530 		counter = &ipdrops_ah_bad_length;
3531 		goto ah_in_discard;
3532 	}
3533 	ASSERT(icv_len != 0);
3534 
3535 	/* compute the padded AH ICV len */
3536 	if (isv4) {
3537 		ipha = (ipha_t *)mp->b_rptr;
3538 		align_len = (icv_len + IPV4_PADDING_ALIGN - 1) &
3539 		    -IPV4_PADDING_ALIGN;
3540 	} else {
3541 		ip6h = (ip6_t *)mp->b_rptr;
3542 		align_len = (icv_len + IPV6_PADDING_ALIGN - 1) &
3543 		    -IPV6_PADDING_ALIGN;
3544 	}
3545 
3546 	ah = (ah_t *)(mp->b_rptr + ah_offset);
3547 	in_icv = (uint8_t *)ah + sizeof (ah_t);
3548 
3549 	/* compare ICV in AH header vs ICV computed by adapter */
3550 	if (bcmp(hada->da_icv, in_icv, icv_len)) {
3551 		int af;
3552 		void *addr;
3553 
3554 		if (isv4) {
3555 			addr = &ipha->ipha_dst;
3556 			af = AF_INET;
3557 		} else {
3558 			addr = &ip6h->ip6_dst;
3559 			af = AF_INET6;
3560 		}
3561 
3562 		/*
3563 		 * Log the event. Don't print to the console, block
3564 		 * potential denial-of-service attack.
3565 		 */
3566 		AH_BUMP_STAT(bad_auth);
3567 		ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
3568 		    "AH Authentication failed spi %x, dst_addr %s",
3569 		    assoc->ipsa_spi, addr, af);
3570 		counter = &ipdrops_ah_bad_auth;
3571 		goto ah_in_discard;
3572 	}
3573 
3574 	ah3dbg(("AH succeeded, checking replay\n"));
3575 	AH_BUMP_STAT(good_auth);
3576 
3577 	if (!sadb_replay_check(assoc, ah->ah_replay)) {
3578 		int af;
3579 		void *addr;
3580 
3581 		if (isv4) {
3582 			addr = &ipha->ipha_dst;
3583 			af = AF_INET;
3584 		} else {
3585 			addr = &ip6h->ip6_dst;
3586 			af = AF_INET6;
3587 		}
3588 
3589 		/*
3590 		 * Log the event. As of now we print out an event.
3591 		 * Do not print the replay failure number, or else
3592 		 * syslog cannot collate the error messages.  Printing
3593 		 * the replay number that failed (or printing to the
3594 		 * console) opens a denial-of-service attack.
3595 		 */
3596 		AH_BUMP_STAT(replay_failures);
3597 		ipsec_assocfailure(info.mi_idnum, 0, 0,
3598 		    SL_ERROR | SL_WARN,
3599 		    "Replay failed for AH spi %x, dst_addr %s",
3600 		    assoc->ipsa_spi, addr, af);
3601 		counter = &ipdrops_ah_replay;
3602 		goto ah_in_discard;
3603 	}
3604 
3605 	/*
3606 	 * Remove AH header. We do this by copying everything before
3607 	 * the AH header onto the AH header+ICV.
3608 	 */
3609 	/* overwrite AH with what was preceeding it (IP header) */
3610 	next_hdr = ah->ah_nexthdr;
3611 	ovbcopy(mp->b_rptr, mp->b_rptr + sizeof (ah_t) + align_len,
3612 	    ah_offset);
3613 	mp->b_rptr += sizeof (ah_t) + align_len;
3614 	if (isv4) {
3615 		/* adjust IP header next protocol */
3616 		ipha = (ipha_t *)mp->b_rptr;
3617 		ipha->ipha_protocol = next_hdr;
3618 
3619 		age_bytes = ipha->ipha_length;
3620 
3621 		/* adjust length in IP header */
3622 		ipha->ipha_length -= (sizeof (ah_t) + align_len);
3623 
3624 		/* recalculate checksum */
3625 		ipha->ipha_hdr_checksum = 0;
3626 		ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
3627 	} else {
3628 		/* adjust IP header next protocol */
3629 		ip6h = (ip6_t *)mp->b_rptr;
3630 		ip6h->ip6_nxt = next_hdr;
3631 
3632 		age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
3633 		    sizeof (ah_t);
3634 
3635 		/* adjust length in IP header */
3636 		ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) -
3637 		    (sizeof (ah_t) + align_len));
3638 	}
3639 
3640 	/* age SA */
3641 	if (!ah_age_bytes(assoc, age_bytes, B_TRUE)) {
3642 		/* The ipsa has hit hard expiration, LOG and AUDIT. */
3643 		ipsec_assocfailure(info.mi_idnum, 0, 0,
3644 		    SL_ERROR | SL_WARN,
3645 		    "AH Association 0x%x, dst %s had bytes expire.\n",
3646 		    assoc->ipsa_spi, assoc->ipsa_dstaddr,
3647 		    AF_INET);
3648 		AH_BUMP_STAT(bytes_expired);
3649 		counter = &ipdrops_ah_bytes_expire;
3650 		goto ah_in_discard;
3651 	}
3652 
3653 	freeb(hada_mp);
3654 	return (IPSEC_STATUS_SUCCESS);
3655 
3656 ah_in_discard:
3657 	IP_AH_BUMP_STAT(in_discards);
3658 	freeb(hada_mp);
3659 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter, &ah_dropper);
3660 	return (IPSEC_STATUS_FAILED);
3661 }
3662 
3663 /*
3664  * ah_outbound_accelerated_v4:
3665  * Called from ah_outbound_v4() and once it is determined that the
3666  * packet is elligible for hardware acceleration.
3667  *
3668  * We proceed as follows:
3669  * 1. allocate and initialize attributes mblk
3670  * 2. mark IPSEC_OUT to indicate that pkt is accelerated
3671  * 3. insert AH header
3672  */
3673 static ipsec_status_t
3674 ah_outbound_accelerated_v4(mblk_t *ipsec_mp, ipsa_t *assoc)
3675 {
3676 	mblk_t *mp, *new_mp;
3677 	ipsec_out_t *oi;
3678 	uint_t ah_data_sz;	/* ICV length, algorithm dependent */
3679 	uint_t ah_align_sz;	/* ICV length + padding */
3680 	uint32_t v_hlen_tos_len; /* from original IP header */
3681 	ipha_t	*oipha;		/* original IP header */
3682 	ipha_t	*nipha;		/* new IP header */
3683 	uint_t option_length = 0;
3684 	uint_t new_hdr_len;	/* new header length */
3685 	uint_t iphdr_length;
3686 	ah_t *ah_hdr;		/* ptr to AH header */
3687 
3688 	AH_BUMP_STAT(out_accelerated);
3689 
3690 	oi = (ipsec_out_t *)ipsec_mp->b_rptr;
3691 	mp = ipsec_mp->b_cont;
3692 
3693 	oipha = (ipha_t *)mp->b_rptr;
3694 	v_hlen_tos_len = ((uint32_t *)oipha)[0];
3695 
3696 	/* mark packet as being accelerated in IPSEC_OUT */
3697 	ASSERT(oi->ipsec_out_accelerated == B_FALSE);
3698 	oi->ipsec_out_accelerated = B_TRUE;
3699 
3700 	/* calculate authentication data length, i.e. ICV + padding */
3701 	ah_data_sz = assoc->ipsa_mac_len;
3702 	ah_align_sz = (ah_data_sz + IPV4_PADDING_ALIGN - 1) &
3703 	    -IPV4_PADDING_ALIGN;
3704 
3705 	/*
3706 	 * Insert pseudo header:
3707 	 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP
3708 	 */
3709 
3710 	/* IP + AH + authentication + padding data length */
3711 	new_hdr_len = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz;
3712 	if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
3713 		option_length = oipha->ipha_version_and_hdr_length -
3714 		    (uint8_t)((IP_VERSION << 4) +
3715 		    IP_SIMPLE_HDR_LENGTH_IN_WORDS);
3716 		option_length <<= 2;
3717 		new_hdr_len += option_length;
3718 	}
3719 
3720 	/* allocate pseudo-header mblk */
3721 	if ((new_mp = allocb(new_hdr_len, BPRI_HI)) == NULL) {
3722 		/* IPsec kstats: bump bean counter here */
3723 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3724 		    &ipdrops_ah_nomem, &ah_dropper);
3725 		return (IPSEC_STATUS_FAILED);
3726 	}
3727 
3728 	new_mp->b_cont = mp;
3729 	ipsec_mp->b_cont = new_mp;
3730 	new_mp->b_wptr += new_hdr_len;
3731 
3732 	/* copy original IP header to new header */
3733 	bcopy(mp->b_rptr, new_mp->b_rptr, IP_SIMPLE_HDR_LENGTH +
3734 	    option_length);
3735 
3736 	/* update IP header */
3737 	nipha = (ipha_t *)new_mp->b_rptr;
3738 	nipha->ipha_protocol = IPPROTO_AH;
3739 	iphdr_length = ntohs(nipha->ipha_length);
3740 	iphdr_length += sizeof (ah_t) + ah_align_sz;
3741 	nipha->ipha_length = htons(iphdr_length);
3742 	nipha->ipha_hdr_checksum = 0;
3743 	nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
3744 
3745 	/* skip original IP header in mp */
3746 	mp->b_rptr += IP_SIMPLE_HDR_LENGTH + option_length;
3747 
3748 	/* initialize AH header */
3749 	ah_hdr = (ah_t *)(new_mp->b_rptr + IP_SIMPLE_HDR_LENGTH +
3750 	    option_length);
3751 	ah_hdr->ah_nexthdr = oipha->ipha_protocol;
3752 	if (!ah_finish_up(ah_hdr, NULL, assoc, ah_data_sz, ah_align_sz)) {
3753 		/* Only way this fails is if outbound replay counter wraps. */
3754 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3755 		    &ipdrops_ah_replay, &ah_dropper);
3756 		return (IPSEC_STATUS_FAILED);
3757 	}
3758 
3759 	return (IPSEC_STATUS_SUCCESS);
3760 }
3761 
3762 /*
3763  * ah_outbound_accelerated_v6:
3764  *
3765  * Called from ah_outbound_v6() once it is determined that the packet
3766  * is eligible for hardware acceleration.
3767  *
3768  * We proceed as follows:
3769  * 1. allocate and initialize attributes mblk
3770  * 2. mark IPSEC_OUT to indicate that pkt is accelerated
3771  * 3. insert AH header
3772  */
3773 static ipsec_status_t
3774 ah_outbound_accelerated_v6(mblk_t *ipsec_mp, ipsa_t *assoc)
3775 {
3776 	mblk_t *mp, *phdr_mp;
3777 	ipsec_out_t *oi;
3778 	uint_t ah_data_sz;	/* ICV length, algorithm dependent */
3779 	uint_t ah_align_sz;	/* ICV length + padding */
3780 	ip6_t	*oip6h;		/* original IP header */
3781 	ip6_t	*ip6h;		/* new IP header */
3782 	uint_t option_length = 0;
3783 	uint_t hdr_size;
3784 	uint_t ah_offset;
3785 	ah_t *ah_hdr;		/* ptr to AH header */
3786 
3787 	AH_BUMP_STAT(out_accelerated);
3788 
3789 	oi = (ipsec_out_t *)ipsec_mp->b_rptr;
3790 	mp = ipsec_mp->b_cont;
3791 
3792 	oip6h = (ip6_t *)mp->b_rptr;
3793 
3794 	/* mark packet as being accelerated in IPSEC_OUT */
3795 	ASSERT(oi->ipsec_out_accelerated == B_FALSE);
3796 	oi->ipsec_out_accelerated = B_TRUE;
3797 
3798 	/* calculate authentication data length, i.e. ICV + padding */
3799 	ah_data_sz = assoc->ipsa_mac_len;
3800 	ah_align_sz = (ah_data_sz + IPV4_PADDING_ALIGN - 1) &
3801 	    -IPV4_PADDING_ALIGN;
3802 
3803 	ASSERT(ah_align_sz >= ah_data_sz);
3804 
3805 	hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
3806 	option_length = hdr_size - IPV6_HDR_LEN;
3807 
3808 	/* This was not included in ipsec_ah_get_hdr_size_v6() */
3809 	hdr_size += (sizeof (ah_t) + ah_align_sz);
3810 
3811 	if ((phdr_mp = allocb(hdr_size, BPRI_HI)) == NULL) {
3812 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL, &ipdrops_ah_nomem,
3813 		    &ah_dropper);
3814 		return (IPSEC_STATUS_FAILED);
3815 	}
3816 	phdr_mp->b_wptr += hdr_size;
3817 
3818 	/*
3819 	 * Form the basic IP header first.  We always assign every bit
3820 	 * of the v6 basic header, so a separate bzero is unneeded.
3821 	 */
3822 	ip6h = (ip6_t *)phdr_mp->b_rptr;
3823 	ip6h->ip6_vcf = oip6h->ip6_vcf;
3824 	ip6h->ip6_hlim = oip6h->ip6_hlim;
3825 	ip6h->ip6_src = oip6h->ip6_src;
3826 	ip6h->ip6_dst = oip6h->ip6_dst;
3827 	/*
3828 	 * Include the size of AH and authentication data.
3829 	 * This is how our recipient would compute the
3830 	 * authentication data. Look at what we do in the
3831 	 * inbound case below.
3832 	 */
3833 	ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) + sizeof (ah_t) +
3834 	    ah_align_sz);
3835 
3836 	/*
3837 	 * Insert pseudo header:
3838 	 * IPSEC_INFO -> [IP6, LLH, ULP] =>
3839 	 *	IPSEC_INFO -> [IP, LLH, AH, ICV] -> ULP
3840 	 */
3841 
3842 	if (option_length == 0) {
3843 		/* Form the AH header */
3844 		ip6h->ip6_nxt = IPPROTO_AH;
3845 		((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
3846 		ah_offset = IPV6_HDR_LEN;
3847 	} else {
3848 		ip6h->ip6_nxt = oip6h->ip6_nxt;
3849 		/* option_length does not include the AH header's size */
3850 		ah_offset = ah_fix_phdr_v6(ip6h, oip6h, B_TRUE, B_FALSE);
3851 		if (ah_offset == 0) {
3852 			freemsg(phdr_mp);
3853 			ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3854 			    &ipdrops_ah_bad_v6_hdrs, &ah_dropper);
3855 			return (IPSEC_STATUS_FAILED);
3856 		}
3857 	}
3858 
3859 	phdr_mp->b_cont = mp;
3860 	ipsec_mp->b_cont = phdr_mp;
3861 
3862 	/* skip original IP header in mp */
3863 	mp->b_rptr += IPV6_HDR_LEN + option_length;
3864 
3865 	/* initialize AH header */
3866 	ah_hdr = (ah_t *)(phdr_mp->b_rptr + IPV6_HDR_LEN + option_length);
3867 	ah_hdr->ah_nexthdr = oip6h->ip6_nxt;
3868 
3869 	if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)), NULL,
3870 	    assoc, ah_data_sz, ah_align_sz)) {
3871 		/* Only way this fails is if outbound replay counter wraps. */
3872 		ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL,
3873 		    &ipdrops_ah_replay, &ah_dropper);
3874 		return (IPSEC_STATUS_FAILED);
3875 	}
3876 
3877 	return (IPSEC_STATUS_SUCCESS);
3878 }
3879 
3880 /*
3881  * Invoked after processing of an inbound packet by the
3882  * kernel crypto framework. Called by ah_submit_req() for a sync request,
3883  * or by the kcf callback for an async request.
3884  * Returns IPSEC_STATUS_SUCCESS on success, IPSEC_STATUS_FAILED on failure.
3885  * On failure, the mblk chain ipsec_in is freed by this function.
3886  */
3887 static ipsec_status_t
3888 ah_auth_in_done(mblk_t *ipsec_in)
3889 {
3890 	mblk_t *phdr_mp;
3891 	ipha_t *ipha;
3892 	uint_t ah_offset = 0;
3893 	mblk_t *mp;
3894 	int align_len;
3895 	ah_t *ah;
3896 	ipha_t *nipha;
3897 	uint32_t length;
3898 	ipsec_in_t *ii;
3899 	boolean_t isv4;
3900 	ip6_t *ip6h;
3901 	ip6_t *nip6h;
3902 	uint_t icv_len;
3903 	ipsa_t *assoc;
3904 	kstat_named_t *counter;
3905 
3906 	ii = (ipsec_in_t *)ipsec_in->b_rptr;
3907 	isv4 = ii->ipsec_in_v4;
3908 	assoc = ii->ipsec_in_ah_sa;
3909 	icv_len = (uint_t)ii->ipsec_in_crypto_mac.cd_raw.iov_len;
3910 
3911 	phdr_mp = ipsec_in->b_cont;
3912 	if (phdr_mp == NULL) {
3913 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_ah_nomem,
3914 		    &ah_dropper);
3915 		return (IPSEC_STATUS_FAILED);
3916 	}
3917 
3918 	mp = phdr_mp->b_cont;
3919 	if (mp == NULL) {
3920 		ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_ah_nomem,
3921 		    &ah_dropper);
3922 		return (IPSEC_STATUS_FAILED);
3923 	}
3924 	mp->b_rptr -= ii->ipsec_in_skip_len;
3925 
3926 	if (isv4) {
3927 		ipha = (ipha_t *)mp->b_rptr;
3928 		ah_offset = ipha->ipha_version_and_hdr_length -
3929 		    (uint8_t)((IP_VERSION << 4));
3930 		ah_offset <<= 2;
3931 		align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
3932 		    IPV4_PADDING_ALIGN);
3933 	} else {
3934 		ip6h = (ip6_t *)mp->b_rptr;
3935 		ah_offset = ipsec_ah_get_hdr_size_v6(mp, B_TRUE);
3936 		ASSERT((mp->b_wptr - mp->b_rptr) >= ah_offset);
3937 		align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
3938 		    IPV6_PADDING_ALIGN);
3939 	}
3940 
3941 	ah = (ah_t *)(mp->b_rptr + ah_offset);
3942 
3943 	/*
3944 	 * We get here only when authentication passed.
3945 	 */
3946 
3947 	ah3dbg(("AH succeeded, checking replay\n"));
3948 	AH_BUMP_STAT(good_auth);
3949 
3950 	if (!sadb_replay_check(assoc, ah->ah_replay)) {
3951 		int af;
3952 		void *addr;
3953 
3954 		if (isv4) {
3955 			addr = &ipha->ipha_dst;
3956 			af = AF_INET;
3957 		} else {
3958 			addr = &ip6h->ip6_dst;
3959 			af = AF_INET6;
3960 		}
3961 
3962 		/*
3963 		 * Log the event. As of now we print out an event.
3964 		 * Do not print the replay failure number, or else
3965 		 * syslog cannot collate the error messages.  Printing
3966 		 * the replay number that failed (or printing to the
3967 		 * console) opens a denial-of-service attack.
3968 		 */
3969 		AH_BUMP_STAT(replay_failures);
3970 		ipsec_assocfailure(info.mi_idnum, 0, 0,
3971 		    SL_ERROR | SL_WARN,
3972 		    "Replay failed for AH spi %x, dst_addr %s",
3973 		    assoc->ipsa_spi, addr, af);
3974 		counter = &ipdrops_ah_replay;
3975 		goto ah_in_discard;
3976 	}
3977 
3978 	/*
3979 	 * We need to remove the AH header from the original
3980 	 * datagram. Easy way to do this is to use phdr_mp
3981 	 * to hold the IP header and the orginal mp to hold
3982 	 * the rest of it. So, we copy the IP header on to
3983 	 * phdr_mp, and set the b_rptr in mp past AH header.
3984 	 */
3985 	if (isv4) {
3986 		bcopy(mp->b_rptr, phdr_mp->b_rptr, ah_offset);
3987 		phdr_mp->b_wptr = phdr_mp->b_rptr + ah_offset;
3988 		nipha = (ipha_t *)phdr_mp->b_rptr;
3989 		/*
3990 		 * Assign the right protocol, adjust the length as we
3991 		 * are removing the AH header and adjust the checksum to
3992 		 * account for the protocol and length.
3993 		 */
3994 		nipha->ipha_protocol = ah->ah_nexthdr;
3995 		length = ntohs(nipha->ipha_length);
3996 		if (!ah_age_bytes(assoc, length, B_TRUE)) {
3997 			/* The ipsa has hit hard expiration, LOG and AUDIT. */
3998 			ipsec_assocfailure(info.mi_idnum, 0, 0,
3999 			    SL_ERROR | SL_WARN,
4000 			    "AH Association 0x%x, dst %s had bytes expire.\n",
4001 			    assoc->ipsa_spi, assoc->ipsa_dstaddr,
4002 			    AF_INET);
4003 			AH_BUMP_STAT(bytes_expired);
4004 			counter = &ipdrops_ah_bytes_expire;
4005 			goto ah_in_discard;
4006 		}
4007 		length -= (sizeof (ah_t) + align_len);
4008 
4009 		nipha->ipha_length = htons((uint16_t)length);
4010 		nipha->ipha_hdr_checksum = 0;
4011 		nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
4012 		/*
4013 		 * Skip IP,AH and the authentication data in the
4014 		 * original datagram.
4015 		 */
4016 		mp->b_rptr += (ah_offset + sizeof (ah_t) + align_len);
4017 	} else {
4018 		uchar_t *whereptr;
4019 		int hdrlen;
4020 		uint8_t *nexthdr;
4021 		ip6_hbh_t *hbhhdr;
4022 		ip6_dest_t *dsthdr;
4023 		ip6_rthdr0_t *rthdr;
4024 
4025 		nip6h = (ip6_t *)phdr_mp->b_rptr;
4026 
4027 		/*
4028 		 * Make phdr_mp hold until the AH header and make
4029 		 * mp hold everything past AH header.
4030 		 */
4031 		length = ntohs(nip6h->ip6_plen);
4032 		if (!ah_age_bytes(assoc, length + sizeof (ip6_t), B_TRUE)) {
4033 			/* The ipsa has hit hard expiration, LOG and AUDIT. */
4034 			ipsec_assocfailure(info.mi_idnum, 0, 0,
4035 			    SL_ERROR | SL_WARN,
4036 			    "AH Association 0x%x, dst %s had bytes "
4037 			    "expire.\n", assoc->ipsa_spi, &ip6h->ip6_dst,
4038 			    AF_INET6);
4039 			AH_BUMP_STAT(bytes_expired);
4040 			counter = &ipdrops_ah_bytes_expire;
4041 			goto ah_in_discard;
4042 		}
4043 		bcopy(ip6h, nip6h, ah_offset);
4044 		phdr_mp->b_wptr = phdr_mp->b_rptr + ah_offset;
4045 		mp->b_rptr += (ah_offset + sizeof (ah_t) + align_len);
4046 
4047 		/*
4048 		 * Update the next header field of the header preceding
4049 		 * AH with the next header field of AH. Start with the
4050 		 * IPv6 header and proceed with the extension headers
4051 		 * until we find what we're looking for.
4052 		 */
4053 		nexthdr = &nip6h->ip6_nxt;
4054 		whereptr =  (uchar_t *)nip6h;
4055 		hdrlen = sizeof (ip6_t);
4056 
4057 		while (*nexthdr != IPPROTO_AH) {
4058 			whereptr += hdrlen;
4059 			/* Assume IP has already stripped it */
4060 			ASSERT(*nexthdr != IPPROTO_FRAGMENT &&
4061 			    *nexthdr != IPPROTO_RAW);
4062 			switch (*nexthdr) {
4063 			case IPPROTO_HOPOPTS:
4064 				hbhhdr = (ip6_hbh_t *)whereptr;
4065 				nexthdr = &hbhhdr->ip6h_nxt;
4066 				hdrlen = 8 * (hbhhdr->ip6h_len + 1);
4067 				break;
4068 			case IPPROTO_DSTOPTS:
4069 				dsthdr = (ip6_dest_t *)whereptr;
4070 				nexthdr = &dsthdr->ip6d_nxt;
4071 				hdrlen = 8 * (dsthdr->ip6d_len + 1);
4072 				break;
4073 			case IPPROTO_ROUTING:
4074 				rthdr = (ip6_rthdr0_t *)whereptr;
4075 				nexthdr = &rthdr->ip6r0_nxt;
4076 				hdrlen = 8 * (rthdr->ip6r0_len + 1);
4077 				break;
4078 			}
4079 		}
4080 		*nexthdr = ah->ah_nexthdr;
4081 
4082 		length -= (sizeof (ah_t) + align_len);
4083 		nip6h->ip6_plen = htons((uint16_t)length);
4084 	}
4085 
4086 	if (is_system_labeled()) {
4087 		/*
4088 		 * inherit the label by setting it in the new ip header
4089 		 */
4090 		mblk_setcred(phdr_mp, DB_CRED(mp));
4091 	}
4092 	return (IPSEC_STATUS_SUCCESS);
4093 
4094 ah_in_discard:
4095 	IP_AH_BUMP_STAT(in_discards);
4096 	ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter, &ah_dropper);
4097 	return (IPSEC_STATUS_FAILED);
4098 }
4099 
4100 /*
4101  * Invoked after processing of an outbound packet by the
4102  * kernel crypto framework, either by ah_submit_req() for a request
4103  * executed syncrhonously, or by the KEF callback for a request
4104  * executed asynchronously.
4105  */
4106 static ipsec_status_t
4107 ah_auth_out_done(mblk_t *ipsec_out)
4108 {
4109 	mblk_t *phdr_mp;
4110 	mblk_t *mp;
4111 	int align_len;
4112 	uint32_t hdrs_length;
4113 	uchar_t *ptr;
4114 	uint32_t length;
4115 	boolean_t isv4;
4116 	ipsec_out_t *io;
4117 	size_t icv_len;
4118 
4119 	io = (ipsec_out_t *)ipsec_out->b_rptr;
4120 	isv4 = io->ipsec_out_v4;
4121 	icv_len = io->ipsec_out_crypto_mac.cd_raw.iov_len;
4122 
4123 	phdr_mp = ipsec_out->b_cont;
4124 	if (phdr_mp == NULL) {
4125 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
4126 		    &ipdrops_ah_nomem, &ah_dropper);
4127 		return (IPSEC_STATUS_FAILED);
4128 	}
4129 
4130 	mp = phdr_mp->b_cont;
4131 	if (mp == NULL) {
4132 		ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL,
4133 		    &ipdrops_ah_nomem, &ah_dropper);
4134 		return (IPSEC_STATUS_FAILED);
4135 	}
4136 	mp->b_rptr -= io->ipsec_out_skip_len;
4137 
4138 	if (isv4) {
4139 		ipha_t *ipha;
4140 		ipha_t *nipha;
4141 
4142 		ipha = (ipha_t *)mp->b_rptr;
4143 		hdrs_length = ipha->ipha_version_and_hdr_length -
4144 		    (uint8_t)((IP_VERSION << 4));
4145 		hdrs_length <<= 2;
4146 		align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
4147 		    IPV4_PADDING_ALIGN);
4148 		/*
4149 		 * phdr_mp must have the right amount of space for the
4150 		 * combined IP and AH header. Copy the IP header and
4151 		 * the ack_data onto AH. Note that the AH header was
4152 		 * already formed before the ICV calculation and hence
4153 		 * you don't have to copy it here.
4154 		 */
4155 		bcopy(mp->b_rptr, phdr_mp->b_rptr, hdrs_length);
4156 
4157 		ptr = phdr_mp->b_rptr + hdrs_length + sizeof (ah_t);
4158 		bcopy(phdr_mp->b_wptr, ptr, icv_len);
4159 
4160 		/*
4161 		 * Compute the new header checksum as we are assigning
4162 		 * IPPROTO_AH and adjusting the length here.
4163 		 */
4164 		nipha = (ipha_t *)phdr_mp->b_rptr;
4165 
4166 		nipha->ipha_protocol = IPPROTO_AH;
4167 		length = ntohs(nipha->ipha_length);
4168 		length += (sizeof (ah_t) + align_len);
4169 		nipha->ipha_length = htons((uint16_t)length);
4170 		nipha->ipha_hdr_checksum = 0;
4171 		nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
4172 	} else {
4173 		ip6_t *ip6h;
4174 		ip6_t *nip6h;
4175 		uint_t ah_offset;
4176 
4177 		ip6h = (ip6_t *)mp->b_rptr;
4178 		nip6h = (ip6_t *)phdr_mp->b_rptr;
4179 		align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
4180 		    IPV6_PADDING_ALIGN);
4181 		/*
4182 		 * phdr_mp must have the right amount of space for the
4183 		 * combined IP and AH header. Copy the IP header with
4184 		 * options into the pseudo header. When we constructed
4185 		 * a pseudo header, we did not copy some of the mutable
4186 		 * fields. We do it now by calling ah_fix_phdr_v6()
4187 		 * with the last argument B_TRUE. It returns the
4188 		 * ah_offset into the pseudo header.
4189 		 */
4190 
4191 		bcopy(ip6h, nip6h, IPV6_HDR_LEN);
4192 		ah_offset = ah_fix_phdr_v6(nip6h, ip6h, B_TRUE, B_TRUE);
4193 		ASSERT(ah_offset != 0);
4194 		/*
4195 		 * phdr_mp can hold exactly the whole IP header with options
4196 		 * plus the AH header also. Thus subtracting the AH header's
4197 		 * size should give exactly how much of the original header
4198 		 * should be skipped.
4199 		 */
4200 		hdrs_length = (phdr_mp->b_wptr - phdr_mp->b_rptr) -
4201 		    sizeof (ah_t) - icv_len;
4202 		bcopy(phdr_mp->b_wptr, ((uint8_t *)nip6h + ah_offset +
4203 		    sizeof (ah_t)), icv_len);
4204 		length = ntohs(nip6h->ip6_plen);
4205 		length += (sizeof (ah_t) + align_len);
4206 		nip6h->ip6_plen = htons((uint16_t)length);
4207 	}
4208 
4209 	if (is_system_labeled()) {
4210 		/*
4211 		 * inherit the label by setting it in the new ip header
4212 		 */
4213 		mblk_setcred(phdr_mp, DB_CRED(mp));
4214 	}
4215 
4216 	/* Skip the original IP header */
4217 	mp->b_rptr += hdrs_length;
4218 	if (mp->b_rptr == mp->b_wptr) {
4219 		phdr_mp->b_cont = mp->b_cont;
4220 		freeb(mp);
4221 	}
4222 
4223 	return (IPSEC_STATUS_SUCCESS);
4224 }
4225 
4226 /*
4227  * Wrapper to allow IP to trigger an AH association failure message
4228  * during SA inbound selection.
4229  */
4230 void
4231 ipsecah_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
4232     uint32_t spi, void *addr, int af)
4233 {
4234 	if (ipsecah_log_unknown_spi) {
4235 		ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
4236 		    addr, af);
4237 	}
4238 
4239 	ip_drop_packet(mp, B_TRUE, NULL, NULL, &ipdrops_ah_no_sa,
4240 	    &ah_dropper);
4241 }
4242 
4243 /*
4244  * Initialize the AH input and output processing functions.
4245  */
4246 void
4247 ipsecah_init_funcs(ipsa_t *sa)
4248 {
4249 	if (sa->ipsa_output_func == NULL)
4250 		sa->ipsa_output_func = ah_outbound;
4251 	if (sa->ipsa_input_func == NULL)
4252 		sa->ipsa_input_func = ah_inbound;
4253 }
4254