xref: /illumos-gate/usr/src/uts/common/inet/ip/sadb.c (revision a23b3b1bb4e08abaac9fb78fea486e678ce6d6de)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
25  * Copyright (c) 2018 Joyent, Inc.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/stropts.h>
31 #include <sys/strsubr.h>
32 #include <sys/errno.h>
33 #include <sys/ddi.h>
34 #include <sys/debug.h>
35 #include <sys/cmn_err.h>
36 #include <sys/stream.h>
37 #include <sys/strlog.h>
38 #include <sys/kmem.h>
39 #include <sys/sunddi.h>
40 #include <sys/tihdr.h>
41 #include <sys/atomic.h>
42 #include <sys/socket.h>
43 #include <sys/sysmacros.h>
44 #include <sys/crypto/common.h>
45 #include <sys/crypto/api.h>
46 #include <sys/zone.h>
47 #include <netinet/in.h>
48 #include <net/if.h>
49 #include <net/pfkeyv2.h>
50 #include <net/pfpolicy.h>
51 #include <inet/common.h>
52 #include <netinet/ip6.h>
53 #include <inet/ip.h>
54 #include <inet/ip_ire.h>
55 #include <inet/ip6.h>
56 #include <inet/ipsec_info.h>
57 #include <inet/tcp.h>
58 #include <inet/sadb.h>
59 #include <inet/ipsec_impl.h>
60 #include <inet/ipsecah.h>
61 #include <inet/ipsecesp.h>
62 #include <sys/random.h>
63 #include <sys/dlpi.h>
64 #include <sys/strsun.h>
65 #include <sys/strsubr.h>
66 #include <inet/ip_if.h>
67 #include <inet/ipdrop.h>
68 #include <inet/ipclassifier.h>
69 #include <inet/sctp_ip.h>
70 #include <sys/tsol/tnet.h>
71 
72 /*
73  * This source file contains Security Association Database (SADB) common
74  * routines.  They are linked in with the AH module.  Since AH has no chance
75  * of falling under export control, it was safe to link it in there.
76  */
77 
78 static uint8_t *sadb_action_to_ecomb(uint8_t *, uint8_t *, ipsec_action_t *,
79     netstack_t *);
80 static ipsa_t *sadb_torch_assoc(isaf_t *, ipsa_t *);
81 static void sadb_destroy_acqlist(iacqf_t **, uint_t, boolean_t,
82 			    netstack_t *);
83 static void sadb_destroy(sadb_t *, netstack_t *);
84 static mblk_t *sadb_sa2msg(ipsa_t *, sadb_msg_t *);
85 static ts_label_t *sadb_label_from_sens(sadb_sens_t *, uint64_t *);
86 
87 static time_t sadb_add_time(time_t, uint64_t);
88 static void lifetime_fuzz(ipsa_t *);
89 static void age_pair_peer_list(templist_t *, sadb_t *, boolean_t);
90 static int get_ipsa_pair(ipsa_query_t *, ipsap_t *, int *);
91 static void init_ipsa_pair(ipsap_t *);
92 static void destroy_ipsa_pair(ipsap_t *);
93 static int update_pairing(ipsap_t *, ipsa_query_t *, keysock_in_t *, int *);
94 static void ipsa_set_replay(ipsa_t *ipsa, uint32_t offset);
95 
96 /*
97  * ipsacq_maxpackets is defined here to make it tunable
98  * from /etc/system.
99  */
100 extern uint64_t ipsacq_maxpackets;
101 
102 #define	SET_EXPIRE(sa, delta, exp) {				\
103 	if (((sa)->ipsa_ ## delta) != 0) {				\
104 		(sa)->ipsa_ ## exp = sadb_add_time((sa)->ipsa_addtime,	\
105 			(sa)->ipsa_ ## delta);				\
106 	}								\
107 }
108 
109 #define	UPDATE_EXPIRE(sa, delta, exp) {					\
110 	if (((sa)->ipsa_ ## delta) != 0) {				\
111 		time_t tmp = sadb_add_time((sa)->ipsa_usetime,		\
112 			(sa)->ipsa_ ## delta);				\
113 		if (((sa)->ipsa_ ## exp) == 0)				\
114 			(sa)->ipsa_ ## exp = tmp;			\
115 		else							\
116 			(sa)->ipsa_ ## exp =				\
117 			    MIN((sa)->ipsa_ ## exp, tmp);		\
118 	}								\
119 }
120 
121 
122 /* wrap the macro so we can pass it as a function pointer */
123 void
124 sadb_sa_refrele(void *target)
125 {
126 	IPSA_REFRELE(((ipsa_t *)target));
127 }
128 
129 /*
130  * We presume that sizeof (long) == sizeof (time_t) and that time_t is
131  * a signed type.
132  */
133 #define	TIME_MAX LONG_MAX
134 
135 /*
136  * PF_KEY gives us lifetimes in uint64_t seconds.  We presume that
137  * time_t is defined to be a signed type with the same range as
138  * "long".  On ILP32 systems, we thus run the risk of wrapping around
139  * at end of time, as well as "overwrapping" the clock back around
140  * into a seemingly valid but incorrect future date earlier than the
141  * desired expiration.
142  *
143  * In order to avoid odd behavior (either negative lifetimes or loss
144  * of high order bits) when someone asks for bizarrely long SA
145  * lifetimes, we do a saturating add for expire times.
146  *
147  * We presume that ILP32 systems will be past end of support life when
148  * the 32-bit time_t overflows (a dangerous assumption, mind you..).
149  *
150  * On LP64, 2^64 seconds are about 5.8e11 years, at which point we
151  * will hopefully have figured out clever ways to avoid the use of
152  * fixed-sized integers in computation.
153  */
154 static time_t
155 sadb_add_time(time_t base, uint64_t delta)
156 {
157 	/*
158 	 * Clip delta to the maximum possible time_t value to
159 	 * prevent "overwrapping" back into a shorter-than-desired
160 	 * future time.
161 	 */
162 	if (delta > TIME_MAX)
163 		delta = TIME_MAX;
164 
165 	if (base > 0) {
166 		if (TIME_MAX - base < delta)
167 			return (TIME_MAX);	/* Overflow */
168 	}
169 	return (base + delta);
170 }
171 
172 /*
173  * Callers of this function have already created a working security
174  * association, and have found the appropriate table & hash chain.  All this
175  * function does is check duplicates, and insert the SA.  The caller needs to
176  * hold the hash bucket lock and increment the refcnt before insertion.
177  *
178  * Return 0 if success, EEXIST if collision.
179  */
180 #define	SA_UNIQUE_MATCH(sa1, sa2) \
181 	(((sa1)->ipsa_unique_id & (sa1)->ipsa_unique_mask) == \
182 	((sa2)->ipsa_unique_id & (sa2)->ipsa_unique_mask))
183 
184 int
185 sadb_insertassoc(ipsa_t *ipsa, isaf_t *bucket)
186 {
187 	ipsa_t **ptpn = NULL;
188 	ipsa_t *walker;
189 	boolean_t unspecsrc;
190 
191 	ASSERT(MUTEX_HELD(&bucket->isaf_lock));
192 
193 	unspecsrc = IPSA_IS_ADDR_UNSPEC(ipsa->ipsa_srcaddr, ipsa->ipsa_addrfam);
194 
195 	walker = bucket->isaf_ipsa;
196 	ASSERT(walker == NULL || ipsa->ipsa_addrfam == walker->ipsa_addrfam);
197 
198 	/*
199 	 * Find insertion point (pointed to with **ptpn).  Insert at the head
200 	 * of the list unless there's an unspecified source address, then
201 	 * insert it after the last SA with a specified source address.
202 	 *
203 	 * BTW, you'll have to walk the whole chain, matching on {DST, SPI}
204 	 * checking for collisions.
205 	 */
206 
207 	while (walker != NULL) {
208 		if (IPSA_ARE_ADDR_EQUAL(walker->ipsa_dstaddr,
209 		    ipsa->ipsa_dstaddr, ipsa->ipsa_addrfam)) {
210 			if (walker->ipsa_spi == ipsa->ipsa_spi)
211 				return (EEXIST);
212 
213 			mutex_enter(&walker->ipsa_lock);
214 			if (ipsa->ipsa_state == IPSA_STATE_MATURE &&
215 			    (walker->ipsa_flags & IPSA_F_USED) &&
216 			    SA_UNIQUE_MATCH(walker, ipsa)) {
217 				walker->ipsa_flags |= IPSA_F_CINVALID;
218 			}
219 			mutex_exit(&walker->ipsa_lock);
220 		}
221 
222 		if (ptpn == NULL && unspecsrc) {
223 			if (IPSA_IS_ADDR_UNSPEC(walker->ipsa_srcaddr,
224 			    walker->ipsa_addrfam))
225 				ptpn = walker->ipsa_ptpn;
226 			else if (walker->ipsa_next == NULL)
227 				ptpn = &walker->ipsa_next;
228 		}
229 
230 		walker = walker->ipsa_next;
231 	}
232 
233 	if (ptpn == NULL)
234 		ptpn = &bucket->isaf_ipsa;
235 	ipsa->ipsa_next = *ptpn;
236 	ipsa->ipsa_ptpn = ptpn;
237 	if (ipsa->ipsa_next != NULL)
238 		ipsa->ipsa_next->ipsa_ptpn = &ipsa->ipsa_next;
239 	*ptpn = ipsa;
240 	ipsa->ipsa_linklock = &bucket->isaf_lock;
241 
242 	return (0);
243 }
244 #undef SA_UNIQUE_MATCH
245 
246 /*
247  * Free a security association.  Its reference count is 0, which means
248  * I must free it.  The SA must be unlocked and must not be linked into
249  * any fanout list.
250  */
251 static void
252 sadb_freeassoc(ipsa_t *ipsa)
253 {
254 	ipsec_stack_t	*ipss = ipsa->ipsa_netstack->netstack_ipsec;
255 	mblk_t		*asyncmp, *mp;
256 
257 	ASSERT(ipss != NULL);
258 	ASSERT(MUTEX_NOT_HELD(&ipsa->ipsa_lock));
259 	ASSERT(ipsa->ipsa_refcnt == 0);
260 	ASSERT(ipsa->ipsa_next == NULL);
261 	ASSERT(ipsa->ipsa_ptpn == NULL);
262 
263 
264 	asyncmp = sadb_clear_lpkt(ipsa);
265 	if (asyncmp != NULL) {
266 		mp = ip_recv_attr_free_mblk(asyncmp);
267 		ip_drop_packet(mp, B_TRUE, NULL,
268 		    DROPPER(ipss, ipds_sadb_inlarval_timeout),
269 		    &ipss->ipsec_sadb_dropper);
270 	}
271 	mutex_enter(&ipsa->ipsa_lock);
272 
273 	if (ipsa->ipsa_tsl != NULL) {
274 		label_rele(ipsa->ipsa_tsl);
275 		ipsa->ipsa_tsl = NULL;
276 	}
277 
278 	if (ipsa->ipsa_otsl != NULL) {
279 		label_rele(ipsa->ipsa_otsl);
280 		ipsa->ipsa_otsl = NULL;
281 	}
282 
283 	ipsec_destroy_ctx_tmpl(ipsa, IPSEC_ALG_AUTH);
284 	ipsec_destroy_ctx_tmpl(ipsa, IPSEC_ALG_ENCR);
285 	mutex_exit(&ipsa->ipsa_lock);
286 
287 	/* bzero() these fields for paranoia's sake. */
288 	if (ipsa->ipsa_authkey != NULL) {
289 		bzero(ipsa->ipsa_authkey, ipsa->ipsa_authkeylen);
290 		kmem_free(ipsa->ipsa_authkey, ipsa->ipsa_authkeylen);
291 	}
292 	if (ipsa->ipsa_encrkey != NULL) {
293 		bzero(ipsa->ipsa_encrkey, ipsa->ipsa_encrkeylen);
294 		kmem_free(ipsa->ipsa_encrkey, ipsa->ipsa_encrkeylen);
295 	}
296 	if (ipsa->ipsa_nonce_buf != NULL) {
297 		bzero(ipsa->ipsa_nonce_buf, sizeof (ipsec_nonce_t));
298 		kmem_free(ipsa->ipsa_nonce_buf, sizeof (ipsec_nonce_t));
299 	}
300 	if (ipsa->ipsa_src_cid != NULL) {
301 		IPSID_REFRELE(ipsa->ipsa_src_cid);
302 	}
303 	if (ipsa->ipsa_dst_cid != NULL) {
304 		IPSID_REFRELE(ipsa->ipsa_dst_cid);
305 	}
306 	if (ipsa->ipsa_emech.cm_param != NULL)
307 		kmem_free(ipsa->ipsa_emech.cm_param,
308 		    ipsa->ipsa_emech.cm_param_len);
309 
310 	mutex_destroy(&ipsa->ipsa_lock);
311 	kmem_free(ipsa, sizeof (*ipsa));
312 }
313 
314 /*
315  * Unlink a security association from a hash bucket.  Assume the hash bucket
316  * lock is held, but the association's lock is not.
317  *
318  * Note that we do not bump the bucket's generation number here because
319  * we might not be making a visible change to the set of visible SA's.
320  * All callers MUST bump the bucket's generation number before they unlock
321  * the bucket if they use sadb_unlinkassoc to permanetly remove an SA which
322  * was present in the bucket at the time it was locked.
323  */
324 void
325 sadb_unlinkassoc(ipsa_t *ipsa)
326 {
327 	ASSERT(ipsa->ipsa_linklock != NULL);
328 	ASSERT(MUTEX_HELD(ipsa->ipsa_linklock));
329 
330 	/* These fields are protected by the link lock. */
331 	*(ipsa->ipsa_ptpn) = ipsa->ipsa_next;
332 	if (ipsa->ipsa_next != NULL) {
333 		ipsa->ipsa_next->ipsa_ptpn = ipsa->ipsa_ptpn;
334 		ipsa->ipsa_next = NULL;
335 	}
336 
337 	ipsa->ipsa_ptpn = NULL;
338 
339 	/* This may destroy the SA. */
340 	IPSA_REFRELE(ipsa);
341 }
342 
343 void
344 sadb_delete_cluster(ipsa_t *assoc)
345 {
346 	uint8_t protocol;
347 
348 	if (cl_inet_deletespi &&
349 	    ((assoc->ipsa_state == IPSA_STATE_LARVAL) ||
350 	    (assoc->ipsa_state == IPSA_STATE_MATURE))) {
351 		protocol = (assoc->ipsa_type == SADB_SATYPE_AH) ?
352 		    IPPROTO_AH : IPPROTO_ESP;
353 		cl_inet_deletespi(assoc->ipsa_netstack->netstack_stackid,
354 		    protocol, assoc->ipsa_spi, NULL);
355 	}
356 }
357 
358 /*
359  * Create a larval security association with the specified SPI.	 All other
360  * fields are zeroed.
361  */
362 static ipsa_t *
363 sadb_makelarvalassoc(uint32_t spi, uint32_t *src, uint32_t *dst, int addrfam,
364     netstack_t *ns)
365 {
366 	ipsa_t *newbie;
367 
368 	/*
369 	 * Allocate...
370 	 */
371 
372 	newbie = (ipsa_t *)kmem_zalloc(sizeof (ipsa_t), KM_NOSLEEP);
373 	if (newbie == NULL) {
374 		/* Can't make new larval SA. */
375 		return (NULL);
376 	}
377 
378 	/* Assigned requested SPI, assume caller does SPI allocation magic. */
379 	newbie->ipsa_spi = spi;
380 	newbie->ipsa_netstack = ns;	/* No netstack_hold */
381 
382 	/*
383 	 * Copy addresses...
384 	 */
385 
386 	IPSA_COPY_ADDR(newbie->ipsa_srcaddr, src, addrfam);
387 	IPSA_COPY_ADDR(newbie->ipsa_dstaddr, dst, addrfam);
388 
389 	newbie->ipsa_addrfam = addrfam;
390 
391 	/*
392 	 * Set common initialization values, including refcnt.
393 	 */
394 	mutex_init(&newbie->ipsa_lock, NULL, MUTEX_DEFAULT, NULL);
395 	newbie->ipsa_state = IPSA_STATE_LARVAL;
396 	newbie->ipsa_refcnt = 1;
397 	newbie->ipsa_freefunc = sadb_freeassoc;
398 
399 	/*
400 	 * There aren't a lot of other common initialization values, as
401 	 * they are copied in from the PF_KEY message.
402 	 */
403 
404 	return (newbie);
405 }
406 
407 /*
408  * Call me to initialize a security association fanout.
409  */
410 static int
411 sadb_init_fanout(isaf_t **tablep, uint_t size, int kmflag)
412 {
413 	isaf_t *table;
414 	int i;
415 
416 	table = (isaf_t *)kmem_alloc(size * sizeof (*table), kmflag);
417 	*tablep = table;
418 
419 	if (table == NULL)
420 		return (ENOMEM);
421 
422 	for (i = 0; i < size; i++) {
423 		mutex_init(&(table[i].isaf_lock), NULL, MUTEX_DEFAULT, NULL);
424 		table[i].isaf_ipsa = NULL;
425 		table[i].isaf_gen = 0;
426 	}
427 
428 	return (0);
429 }
430 
431 /*
432  * Call me to initialize an acquire fanout
433  */
434 static int
435 sadb_init_acfanout(iacqf_t **tablep, uint_t size, int kmflag)
436 {
437 	iacqf_t *table;
438 	int i;
439 
440 	table = (iacqf_t *)kmem_alloc(size * sizeof (*table), kmflag);
441 	*tablep = table;
442 
443 	if (table == NULL)
444 		return (ENOMEM);
445 
446 	for (i = 0; i < size; i++) {
447 		mutex_init(&(table[i].iacqf_lock), NULL, MUTEX_DEFAULT, NULL);
448 		table[i].iacqf_ipsacq = NULL;
449 	}
450 
451 	return (0);
452 }
453 
454 /*
455  * Attempt to initialize an SADB instance.  On failure, return ENOMEM;
456  * caller must clean up partial allocations.
457  */
458 static int
459 sadb_init_trial(sadb_t *sp, uint_t size, int kmflag)
460 {
461 	ASSERT(sp->sdb_of == NULL);
462 	ASSERT(sp->sdb_if == NULL);
463 	ASSERT(sp->sdb_acq == NULL);
464 
465 	sp->sdb_hashsize = size;
466 	if (sadb_init_fanout(&sp->sdb_of, size, kmflag) != 0)
467 		return (ENOMEM);
468 	if (sadb_init_fanout(&sp->sdb_if, size, kmflag) != 0)
469 		return (ENOMEM);
470 	if (sadb_init_acfanout(&sp->sdb_acq, size, kmflag) != 0)
471 		return (ENOMEM);
472 
473 	return (0);
474 }
475 
476 /*
477  * Call me to initialize an SADB instance; fall back to default size on failure.
478  */
479 static void
480 sadb_init(const char *name, sadb_t *sp, uint_t size, uint_t ver,
481     netstack_t *ns)
482 {
483 	ASSERT(sp->sdb_of == NULL);
484 	ASSERT(sp->sdb_if == NULL);
485 	ASSERT(sp->sdb_acq == NULL);
486 
487 	if (size < IPSEC_DEFAULT_HASH_SIZE)
488 		size = IPSEC_DEFAULT_HASH_SIZE;
489 
490 	if (sadb_init_trial(sp, size, KM_NOSLEEP) != 0) {
491 
492 		cmn_err(CE_WARN,
493 		    "Unable to allocate %u entry IPv%u %s SADB hash table",
494 		    size, ver, name);
495 
496 		sadb_destroy(sp, ns);
497 		size = IPSEC_DEFAULT_HASH_SIZE;
498 		cmn_err(CE_WARN, "Falling back to %d entries", size);
499 		(void) sadb_init_trial(sp, size, KM_SLEEP);
500 	}
501 }
502 
503 
504 /*
505  * Initialize an SADB-pair.
506  */
507 void
508 sadbp_init(const char *name, sadbp_t *sp, int type, int size, netstack_t *ns)
509 {
510 	sadb_init(name, &sp->s_v4, size, 4, ns);
511 	sadb_init(name, &sp->s_v6, size, 6, ns);
512 
513 	sp->s_satype = type;
514 
515 	ASSERT((type == SADB_SATYPE_AH) || (type == SADB_SATYPE_ESP));
516 	if (type == SADB_SATYPE_AH) {
517 		ipsec_stack_t	*ipss = ns->netstack_ipsec;
518 
519 		ip_drop_register(&ipss->ipsec_sadb_dropper, "IPsec SADB");
520 		sp->s_addflags = AH_ADD_SETTABLE_FLAGS;
521 		sp->s_updateflags = AH_UPDATE_SETTABLE_FLAGS;
522 	} else {
523 		sp->s_addflags = ESP_ADD_SETTABLE_FLAGS;
524 		sp->s_updateflags = ESP_UPDATE_SETTABLE_FLAGS;
525 	}
526 }
527 
528 /*
529  * Deliver a single SADB_DUMP message representing a single SA.  This is
530  * called many times by sadb_dump().
531  *
532  * If the return value of this is ENOBUFS (not the same as ENOMEM), then
533  * the caller should take that as a hint that dupb() on the "original answer"
534  * failed, and that perhaps the caller should try again with a copyb()ed
535  * "original answer".
536  */
537 static int
538 sadb_dump_deliver(queue_t *pfkey_q, mblk_t *original_answer, ipsa_t *ipsa,
539     sadb_msg_t *samsg)
540 {
541 	mblk_t *answer;
542 
543 	answer = dupb(original_answer);
544 	if (answer == NULL)
545 		return (ENOBUFS);
546 	answer->b_cont = sadb_sa2msg(ipsa, samsg);
547 	if (answer->b_cont == NULL) {
548 		freeb(answer);
549 		return (ENOMEM);
550 	}
551 
552 	/* Just do a putnext, and let keysock deal with flow control. */
553 	putnext(pfkey_q, answer);
554 	return (0);
555 }
556 
557 /*
558  * Common function to allocate and prepare a keysock_out_t M_CTL message.
559  */
560 mblk_t *
561 sadb_keysock_out(minor_t serial)
562 {
563 	mblk_t *mp;
564 	keysock_out_t *kso;
565 
566 	mp = allocb(sizeof (ipsec_info_t), BPRI_HI);
567 	if (mp != NULL) {
568 		mp->b_datap->db_type = M_CTL;
569 		mp->b_wptr += sizeof (ipsec_info_t);
570 		kso = (keysock_out_t *)mp->b_rptr;
571 		kso->ks_out_type = KEYSOCK_OUT;
572 		kso->ks_out_len = sizeof (*kso);
573 		kso->ks_out_serial = serial;
574 	}
575 
576 	return (mp);
577 }
578 
579 /*
580  * Perform an SADB_DUMP, spewing out every SA in an array of SA fanouts
581  * to keysock.
582  */
583 static int
584 sadb_dump_fanout(queue_t *pfkey_q, mblk_t *mp, minor_t serial, isaf_t *fanout,
585     int num_entries, boolean_t do_peers, time_t active_time)
586 {
587 	int i, error = 0;
588 	mblk_t *original_answer;
589 	ipsa_t *walker;
590 	sadb_msg_t *samsg;
591 	time_t	current;
592 
593 	/*
594 	 * For each IPSA hash bucket do:
595 	 *	- Hold the mutex
596 	 *	- Walk each entry, doing an sadb_dump_deliver() on it.
597 	 */
598 	ASSERT(mp->b_cont != NULL);
599 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
600 
601 	original_answer = sadb_keysock_out(serial);
602 	if (original_answer == NULL)
603 		return (ENOMEM);
604 
605 	current = gethrestime_sec();
606 	for (i = 0; i < num_entries; i++) {
607 		mutex_enter(&fanout[i].isaf_lock);
608 		for (walker = fanout[i].isaf_ipsa; walker != NULL;
609 		    walker = walker->ipsa_next) {
610 			if (!do_peers && walker->ipsa_haspeer)
611 				continue;
612 			if ((active_time != 0) &&
613 			    ((current - walker->ipsa_lastuse) > active_time))
614 				continue;
615 			error = sadb_dump_deliver(pfkey_q, original_answer,
616 			    walker, samsg);
617 			if (error == ENOBUFS) {
618 				mblk_t *new_original_answer;
619 
620 				/* Ran out of dupb's.  Try a copyb. */
621 				new_original_answer = copyb(original_answer);
622 				if (new_original_answer == NULL) {
623 					error = ENOMEM;
624 				} else {
625 					freeb(original_answer);
626 					original_answer = new_original_answer;
627 					error = sadb_dump_deliver(pfkey_q,
628 					    original_answer, walker, samsg);
629 				}
630 			}
631 			if (error != 0)
632 				break;	/* out of for loop. */
633 		}
634 		mutex_exit(&fanout[i].isaf_lock);
635 		if (error != 0)
636 			break;	/* out of for loop. */
637 	}
638 
639 	freeb(original_answer);
640 	return (error);
641 }
642 
643 /*
644  * Dump an entire SADB; outbound first, then inbound.
645  */
646 
647 int
648 sadb_dump(queue_t *pfkey_q, mblk_t *mp, keysock_in_t *ksi, sadb_t *sp)
649 {
650 	int error;
651 	time_t	active_time = 0;
652 	sadb_x_edump_t	*edump =
653 	    (sadb_x_edump_t *)ksi->ks_in_extv[SADB_X_EXT_EDUMP];
654 
655 	if (edump != NULL) {
656 		active_time = edump->sadb_x_edump_timeout;
657 	}
658 
659 	/* Dump outbound */
660 	error = sadb_dump_fanout(pfkey_q, mp, ksi->ks_in_serial, sp->sdb_of,
661 	    sp->sdb_hashsize, B_TRUE, active_time);
662 	if (error)
663 		return (error);
664 
665 	/* Dump inbound */
666 	return sadb_dump_fanout(pfkey_q, mp, ksi->ks_in_serial, sp->sdb_if,
667 	    sp->sdb_hashsize, B_FALSE, active_time);
668 }
669 
670 /*
671  * Generic sadb table walker.
672  *
673  * Call "walkfn" for each SA in each bucket in "table"; pass the
674  * bucket, the entry and "cookie" to the callback function.
675  * Take care to ensure that walkfn can delete the SA without screwing
676  * up our traverse.
677  *
678  * The bucket is locked for the duration of the callback, both so that the
679  * callback can just call sadb_unlinkassoc() when it wants to delete something,
680  * and so that no new entries are added while we're walking the list.
681  */
682 static void
683 sadb_walker(isaf_t *table, uint_t numentries,
684     void (*walkfn)(isaf_t *head, ipsa_t *entry, void *cookie),
685     void *cookie)
686 {
687 	int i;
688 	for (i = 0; i < numentries; i++) {
689 		ipsa_t *entry, *next;
690 
691 		mutex_enter(&table[i].isaf_lock);
692 
693 		for (entry = table[i].isaf_ipsa; entry != NULL;
694 		    entry = next) {
695 			next = entry->ipsa_next;
696 			(*walkfn)(&table[i], entry, cookie);
697 		}
698 		mutex_exit(&table[i].isaf_lock);
699 	}
700 }
701 
702 /*
703  * Call me to free up a security association fanout.  Use the forever
704  * variable to indicate freeing up the SAs (forever == B_FALSE, e.g.
705  * an SADB_FLUSH message), or destroying everything (forever == B_TRUE,
706  * when a module is unloaded).
707  */
708 static void
709 sadb_destroyer(isaf_t **tablep, uint_t numentries, boolean_t forever,
710     boolean_t inbound)
711 {
712 	int i;
713 	isaf_t *table = *tablep;
714 	uint8_t protocol;
715 	ipsa_t *sa;
716 	netstackid_t sid;
717 
718 	if (table == NULL)
719 		return;
720 
721 	for (i = 0; i < numentries; i++) {
722 		mutex_enter(&table[i].isaf_lock);
723 		while ((sa = table[i].isaf_ipsa) != NULL) {
724 			if (inbound && cl_inet_deletespi &&
725 			    (sa->ipsa_state != IPSA_STATE_ACTIVE_ELSEWHERE) &&
726 			    (sa->ipsa_state != IPSA_STATE_IDLE)) {
727 				protocol = (sa->ipsa_type == SADB_SATYPE_AH) ?
728 				    IPPROTO_AH : IPPROTO_ESP;
729 				sid = sa->ipsa_netstack->netstack_stackid;
730 				cl_inet_deletespi(sid, protocol, sa->ipsa_spi,
731 				    NULL);
732 			}
733 			sadb_unlinkassoc(sa);
734 		}
735 		table[i].isaf_gen++;
736 		mutex_exit(&table[i].isaf_lock);
737 		if (forever)
738 			mutex_destroy(&(table[i].isaf_lock));
739 	}
740 
741 	if (forever) {
742 		*tablep = NULL;
743 		kmem_free(table, numentries * sizeof (*table));
744 	}
745 }
746 
747 /*
748  * Entry points to sadb_destroyer().
749  */
750 static void
751 sadb_flush(sadb_t *sp, netstack_t *ns)
752 {
753 	/*
754 	 * Flush out each bucket, one at a time.  Were it not for keysock's
755 	 * enforcement, there would be a subtlety where I could add on the
756 	 * heels of a flush.  With keysock's enforcement, however, this
757 	 * makes ESP's job easy.
758 	 */
759 	sadb_destroyer(&sp->sdb_of, sp->sdb_hashsize, B_FALSE, B_FALSE);
760 	sadb_destroyer(&sp->sdb_if, sp->sdb_hashsize, B_FALSE, B_TRUE);
761 
762 	/* For each acquire, destroy it; leave the bucket mutex alone. */
763 	sadb_destroy_acqlist(&sp->sdb_acq, sp->sdb_hashsize, B_FALSE, ns);
764 }
765 
766 static void
767 sadb_destroy(sadb_t *sp, netstack_t *ns)
768 {
769 	sadb_destroyer(&sp->sdb_of, sp->sdb_hashsize, B_TRUE, B_FALSE);
770 	sadb_destroyer(&sp->sdb_if, sp->sdb_hashsize, B_TRUE, B_TRUE);
771 
772 	/* For each acquire, destroy it, including the bucket mutex. */
773 	sadb_destroy_acqlist(&sp->sdb_acq, sp->sdb_hashsize, B_TRUE, ns);
774 
775 	ASSERT(sp->sdb_of == NULL);
776 	ASSERT(sp->sdb_if == NULL);
777 	ASSERT(sp->sdb_acq == NULL);
778 }
779 
780 void
781 sadbp_flush(sadbp_t *spp, netstack_t *ns)
782 {
783 	sadb_flush(&spp->s_v4, ns);
784 	sadb_flush(&spp->s_v6, ns);
785 }
786 
787 void
788 sadbp_destroy(sadbp_t *spp, netstack_t *ns)
789 {
790 	sadb_destroy(&spp->s_v4, ns);
791 	sadb_destroy(&spp->s_v6, ns);
792 
793 	if (spp->s_satype == SADB_SATYPE_AH) {
794 		ipsec_stack_t	*ipss = ns->netstack_ipsec;
795 
796 		ip_drop_unregister(&ipss->ipsec_sadb_dropper);
797 	}
798 }
799 
800 
801 /*
802  * Check hard vs. soft lifetimes.  If there's a reality mismatch (e.g.
803  * soft lifetimes > hard lifetimes) return an appropriate diagnostic for
804  * EINVAL.
805  */
806 int
807 sadb_hardsoftchk(sadb_lifetime_t *hard, sadb_lifetime_t *soft,
808     sadb_lifetime_t *idle)
809 {
810 	if (hard == NULL || soft == NULL)
811 		return (0);
812 
813 	if (hard->sadb_lifetime_allocations != 0 &&
814 	    soft->sadb_lifetime_allocations != 0 &&
815 	    hard->sadb_lifetime_allocations < soft->sadb_lifetime_allocations)
816 		return (SADB_X_DIAGNOSTIC_ALLOC_HSERR);
817 
818 	if (hard->sadb_lifetime_bytes != 0 &&
819 	    soft->sadb_lifetime_bytes != 0 &&
820 	    hard->sadb_lifetime_bytes < soft->sadb_lifetime_bytes)
821 		return (SADB_X_DIAGNOSTIC_BYTES_HSERR);
822 
823 	if (hard->sadb_lifetime_addtime != 0 &&
824 	    soft->sadb_lifetime_addtime != 0 &&
825 	    hard->sadb_lifetime_addtime < soft->sadb_lifetime_addtime)
826 		return (SADB_X_DIAGNOSTIC_ADDTIME_HSERR);
827 
828 	if (hard->sadb_lifetime_usetime != 0 &&
829 	    soft->sadb_lifetime_usetime != 0 &&
830 	    hard->sadb_lifetime_usetime < soft->sadb_lifetime_usetime)
831 		return (SADB_X_DIAGNOSTIC_USETIME_HSERR);
832 
833 	if (idle != NULL) {
834 		if (hard->sadb_lifetime_addtime != 0 &&
835 		    idle->sadb_lifetime_addtime != 0 &&
836 		    hard->sadb_lifetime_addtime < idle->sadb_lifetime_addtime)
837 			return (SADB_X_DIAGNOSTIC_ADDTIME_HSERR);
838 
839 		if (soft->sadb_lifetime_addtime != 0 &&
840 		    idle->sadb_lifetime_addtime != 0 &&
841 		    soft->sadb_lifetime_addtime < idle->sadb_lifetime_addtime)
842 			return (SADB_X_DIAGNOSTIC_ADDTIME_HSERR);
843 
844 		if (hard->sadb_lifetime_usetime != 0 &&
845 		    idle->sadb_lifetime_usetime != 0 &&
846 		    hard->sadb_lifetime_usetime < idle->sadb_lifetime_usetime)
847 			return (SADB_X_DIAGNOSTIC_USETIME_HSERR);
848 
849 		if (soft->sadb_lifetime_usetime != 0 &&
850 		    idle->sadb_lifetime_usetime != 0 &&
851 		    soft->sadb_lifetime_usetime < idle->sadb_lifetime_usetime)
852 			return (SADB_X_DIAGNOSTIC_USETIME_HSERR);
853 	}
854 
855 	return (0);
856 }
857 
858 /*
859  * Sanity check sensitivity labels.
860  *
861  * For now, just reject labels on unlabeled systems.
862  */
863 int
864 sadb_labelchk(keysock_in_t *ksi)
865 {
866 	if (!is_system_labeled()) {
867 		if (ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL)
868 			return (SADB_X_DIAGNOSTIC_BAD_LABEL);
869 
870 		if (ksi->ks_in_extv[SADB_X_EXT_OUTER_SENS] != NULL)
871 			return (SADB_X_DIAGNOSTIC_BAD_LABEL);
872 	}
873 
874 	return (0);
875 }
876 
877 /*
878  * Clone a security association for the purposes of inserting a single SA
879  * into inbound and outbound tables respectively. This function should only
880  * be called from sadb_common_add().
881  */
882 static ipsa_t *
883 sadb_cloneassoc(ipsa_t *ipsa)
884 {
885 	ipsa_t *newbie;
886 	boolean_t error = B_FALSE;
887 
888 	ASSERT(MUTEX_NOT_HELD(&(ipsa->ipsa_lock)));
889 
890 	newbie = kmem_alloc(sizeof (ipsa_t), KM_NOSLEEP);
891 	if (newbie == NULL)
892 		return (NULL);
893 
894 	/* Copy over what we can. */
895 	*newbie = *ipsa;
896 
897 	/* bzero and initialize locks, in case *_init() allocates... */
898 	mutex_init(&newbie->ipsa_lock, NULL, MUTEX_DEFAULT, NULL);
899 
900 	if (newbie->ipsa_tsl != NULL)
901 		label_hold(newbie->ipsa_tsl);
902 
903 	if (newbie->ipsa_otsl != NULL)
904 		label_hold(newbie->ipsa_otsl);
905 
906 	/*
907 	 * While somewhat dain-bramaged, the most graceful way to
908 	 * recover from errors is to keep plowing through the
909 	 * allocations, and getting what I can.  It's easier to call
910 	 * sadb_freeassoc() on the stillborn clone when all the
911 	 * pointers aren't pointing to the parent's data.
912 	 */
913 
914 	if (ipsa->ipsa_authkey != NULL) {
915 		newbie->ipsa_authkey = kmem_alloc(newbie->ipsa_authkeylen,
916 		    KM_NOSLEEP);
917 		if (newbie->ipsa_authkey == NULL) {
918 			error = B_TRUE;
919 		} else {
920 			bcopy(ipsa->ipsa_authkey, newbie->ipsa_authkey,
921 			    newbie->ipsa_authkeylen);
922 
923 			newbie->ipsa_kcfauthkey.ck_data =
924 			    newbie->ipsa_authkey;
925 		}
926 
927 		if (newbie->ipsa_amech.cm_param != NULL) {
928 			newbie->ipsa_amech.cm_param =
929 			    (char *)&newbie->ipsa_mac_len;
930 		}
931 	}
932 
933 	if (ipsa->ipsa_encrkey != NULL) {
934 		newbie->ipsa_encrkey = kmem_alloc(newbie->ipsa_encrkeylen,
935 		    KM_NOSLEEP);
936 		if (newbie->ipsa_encrkey == NULL) {
937 			error = B_TRUE;
938 		} else {
939 			bcopy(ipsa->ipsa_encrkey, newbie->ipsa_encrkey,
940 			    newbie->ipsa_encrkeylen);
941 
942 			newbie->ipsa_kcfencrkey.ck_data =
943 			    newbie->ipsa_encrkey;
944 		}
945 	}
946 
947 	newbie->ipsa_authtmpl = NULL;
948 	newbie->ipsa_encrtmpl = NULL;
949 	newbie->ipsa_haspeer = B_TRUE;
950 
951 	if (ipsa->ipsa_src_cid != NULL) {
952 		newbie->ipsa_src_cid = ipsa->ipsa_src_cid;
953 		IPSID_REFHOLD(ipsa->ipsa_src_cid);
954 	}
955 
956 	if (ipsa->ipsa_dst_cid != NULL) {
957 		newbie->ipsa_dst_cid = ipsa->ipsa_dst_cid;
958 		IPSID_REFHOLD(ipsa->ipsa_dst_cid);
959 	}
960 
961 	if (error) {
962 		sadb_freeassoc(newbie);
963 		return (NULL);
964 	}
965 
966 	return (newbie);
967 }
968 
969 /*
970  * Initialize a SADB address extension at the address specified by addrext.
971  * Return a pointer to the end of the new address extension.
972  */
973 static uint8_t *
974 sadb_make_addr_ext(uint8_t *start, uint8_t *end, uint16_t exttype,
975     sa_family_t af, uint32_t *addr, uint16_t port, uint8_t proto, int prefix)
976 {
977 	struct sockaddr_in *sin;
978 	struct sockaddr_in6 *sin6;
979 	uint8_t *cur = start;
980 	int addrext_len;
981 	int sin_len;
982 	sadb_address_t *addrext	= (sadb_address_t *)cur;
983 
984 	if (cur == NULL)
985 		return (NULL);
986 
987 	cur += sizeof (*addrext);
988 	if (cur > end)
989 		return (NULL);
990 
991 	addrext->sadb_address_proto = proto;
992 	addrext->sadb_address_prefixlen = prefix;
993 	addrext->sadb_address_reserved = 0;
994 	addrext->sadb_address_exttype = exttype;
995 
996 	switch (af) {
997 	case AF_INET:
998 		sin = (struct sockaddr_in *)cur;
999 		sin_len = sizeof (*sin);
1000 		cur += sin_len;
1001 		if (cur > end)
1002 			return (NULL);
1003 
1004 		sin->sin_family = af;
1005 		bzero(sin->sin_zero, sizeof (sin->sin_zero));
1006 		sin->sin_port = port;
1007 		IPSA_COPY_ADDR(&sin->sin_addr, addr, af);
1008 		break;
1009 	case AF_INET6:
1010 		sin6 = (struct sockaddr_in6 *)cur;
1011 		sin_len = sizeof (*sin6);
1012 		cur += sin_len;
1013 		if (cur > end)
1014 			return (NULL);
1015 
1016 		bzero(sin6, sizeof (*sin6));
1017 		sin6->sin6_family = af;
1018 		sin6->sin6_port = port;
1019 		IPSA_COPY_ADDR(&sin6->sin6_addr, addr, af);
1020 		break;
1021 	}
1022 
1023 	addrext_len = roundup(cur - start, sizeof (uint64_t));
1024 	addrext->sadb_address_len = SADB_8TO64(addrext_len);
1025 
1026 	cur = start + addrext_len;
1027 	if (cur > end)
1028 		cur = NULL;
1029 
1030 	return (cur);
1031 }
1032 
1033 /*
1034  * Construct a key management cookie extension.
1035  */
1036 
1037 static uint8_t *
1038 sadb_make_kmc_ext(uint8_t *cur, uint8_t *end, uint32_t kmp, uint64_t kmc)
1039 {
1040 	sadb_x_kmc_t *kmcext = (sadb_x_kmc_t *)cur;
1041 
1042 	if (cur == NULL)
1043 		return (NULL);
1044 
1045 	cur += sizeof (*kmcext);
1046 
1047 	if (cur > end)
1048 		return (NULL);
1049 
1050 	kmcext->sadb_x_kmc_len = SADB_8TO64(sizeof (*kmcext));
1051 	kmcext->sadb_x_kmc_exttype = SADB_X_EXT_KM_COOKIE;
1052 	kmcext->sadb_x_kmc_proto = kmp;
1053 	kmcext->sadb_x_kmc_cookie64 = kmc;
1054 
1055 	return (cur);
1056 }
1057 
1058 /*
1059  * Given an original message header with sufficient space following it, and an
1060  * SA, construct a full PF_KEY message with all of the relevant extensions.
1061  * This is mostly used for SADB_GET, and SADB_DUMP.
1062  */
1063 static mblk_t *
1064 sadb_sa2msg(ipsa_t *ipsa, sadb_msg_t *samsg)
1065 {
1066 	int alloclen, addrsize, paddrsize, authsize, encrsize;
1067 	int srcidsize, dstidsize, senslen, osenslen;
1068 	sa_family_t fam, pfam;	/* Address family for SADB_EXT_ADDRESS */
1069 				/* src/dst and proxy sockaddrs. */
1070 	/*
1071 	 * The following are pointers into the PF_KEY message this PF_KEY
1072 	 * message creates.
1073 	 */
1074 	sadb_msg_t *newsamsg;
1075 	sadb_sa_t *assoc;
1076 	sadb_lifetime_t *lt;
1077 	sadb_key_t *key;
1078 	sadb_ident_t *ident;
1079 	sadb_sens_t *sens;
1080 	sadb_ext_t *walker;	/* For when we need a generic ext. pointer. */
1081 	sadb_x_replay_ctr_t *repl_ctr;
1082 	sadb_x_pair_t *pair_ext;
1083 
1084 	mblk_t *mp;
1085 	uint8_t *cur, *end;
1086 	/* These indicate the presence of the above extension fields. */
1087 	boolean_t soft = B_FALSE, hard = B_FALSE;
1088 	boolean_t isrc = B_FALSE, idst = B_FALSE;
1089 	boolean_t auth = B_FALSE, encr = B_FALSE;
1090 	boolean_t sensinteg = B_FALSE, osensinteg = B_FALSE;
1091 	boolean_t srcid = B_FALSE, dstid = B_FALSE;
1092 	boolean_t idle;
1093 	boolean_t paired;
1094 	uint32_t otherspi;
1095 
1096 	/* First off, figure out the allocation length for this message. */
1097 	/*
1098 	 * Constant stuff.  This includes base, SA, address (src, dst),
1099 	 * and lifetime (current).
1100 	 */
1101 	alloclen = sizeof (sadb_msg_t) + sizeof (sadb_sa_t) +
1102 	    sizeof (sadb_lifetime_t);
1103 
1104 	fam = ipsa->ipsa_addrfam;
1105 	switch (fam) {
1106 	case AF_INET:
1107 		addrsize = roundup(sizeof (struct sockaddr_in) +
1108 		    sizeof (sadb_address_t), sizeof (uint64_t));
1109 		break;
1110 	case AF_INET6:
1111 		addrsize = roundup(sizeof (struct sockaddr_in6) +
1112 		    sizeof (sadb_address_t), sizeof (uint64_t));
1113 		break;
1114 	default:
1115 		return (NULL);
1116 	}
1117 	/*
1118 	 * Allocate TWO address extensions, for source and destination.
1119 	 * (Thus, the * 2.)
1120 	 */
1121 	alloclen += addrsize * 2;
1122 	if (ipsa->ipsa_flags & IPSA_F_NATT_REM)
1123 		alloclen += addrsize;
1124 	if (ipsa->ipsa_flags & IPSA_F_NATT_LOC)
1125 		alloclen += addrsize;
1126 
1127 	if (ipsa->ipsa_flags & IPSA_F_PAIRED) {
1128 		paired = B_TRUE;
1129 		alloclen += sizeof (sadb_x_pair_t);
1130 		otherspi = ipsa->ipsa_otherspi;
1131 	} else {
1132 		paired = B_FALSE;
1133 	}
1134 
1135 	/* How 'bout other lifetimes? */
1136 	if (ipsa->ipsa_softaddlt != 0 || ipsa->ipsa_softuselt != 0 ||
1137 	    ipsa->ipsa_softbyteslt != 0 || ipsa->ipsa_softalloc != 0) {
1138 		alloclen += sizeof (sadb_lifetime_t);
1139 		soft = B_TRUE;
1140 	}
1141 
1142 	if (ipsa->ipsa_hardaddlt != 0 || ipsa->ipsa_harduselt != 0 ||
1143 	    ipsa->ipsa_hardbyteslt != 0 || ipsa->ipsa_hardalloc != 0) {
1144 		alloclen += sizeof (sadb_lifetime_t);
1145 		hard = B_TRUE;
1146 	}
1147 
1148 	if (ipsa->ipsa_idleaddlt != 0 || ipsa->ipsa_idleuselt != 0) {
1149 		alloclen += sizeof (sadb_lifetime_t);
1150 		idle = B_TRUE;
1151 	} else {
1152 		idle = B_FALSE;
1153 	}
1154 
1155 	/* Inner addresses. */
1156 	if (ipsa->ipsa_innerfam != 0) {
1157 		pfam = ipsa->ipsa_innerfam;
1158 		switch (pfam) {
1159 		case AF_INET6:
1160 			paddrsize = roundup(sizeof (struct sockaddr_in6) +
1161 			    sizeof (sadb_address_t), sizeof (uint64_t));
1162 			break;
1163 		case AF_INET:
1164 			paddrsize = roundup(sizeof (struct sockaddr_in) +
1165 			    sizeof (sadb_address_t), sizeof (uint64_t));
1166 			break;
1167 		default:
1168 			cmn_err(CE_PANIC,
1169 			    "IPsec SADB: Proxy length failure.\n");
1170 			break;
1171 		}
1172 		isrc = B_TRUE;
1173 		idst = B_TRUE;
1174 		alloclen += 2 * paddrsize;
1175 	}
1176 
1177 	/* For the following fields, assume that length != 0 ==> stuff */
1178 	if (ipsa->ipsa_authkeylen != 0) {
1179 		authsize = roundup(sizeof (sadb_key_t) + ipsa->ipsa_authkeylen,
1180 		    sizeof (uint64_t));
1181 		alloclen += authsize;
1182 		auth = B_TRUE;
1183 	}
1184 
1185 	if (ipsa->ipsa_encrkeylen != 0) {
1186 		encrsize = roundup(sizeof (sadb_key_t) + ipsa->ipsa_encrkeylen +
1187 		    ipsa->ipsa_nonce_len, sizeof (uint64_t));
1188 		alloclen += encrsize;
1189 		encr = B_TRUE;
1190 	} else {
1191 		encr = B_FALSE;
1192 	}
1193 
1194 	if (ipsa->ipsa_tsl != NULL) {
1195 		senslen = sadb_sens_len_from_label(ipsa->ipsa_tsl);
1196 		alloclen += senslen;
1197 		sensinteg = B_TRUE;
1198 	}
1199 
1200 	if (ipsa->ipsa_otsl != NULL) {
1201 		osenslen = sadb_sens_len_from_label(ipsa->ipsa_otsl);
1202 		alloclen += osenslen;
1203 		osensinteg = B_TRUE;
1204 	}
1205 
1206 	/*
1207 	 * Must use strlen() here for lengths.	Identities use NULL
1208 	 * pointers to indicate their nonexistence.
1209 	 */
1210 	if (ipsa->ipsa_src_cid != NULL) {
1211 		srcidsize = roundup(sizeof (sadb_ident_t) +
1212 		    strlen(ipsa->ipsa_src_cid->ipsid_cid) + 1,
1213 		    sizeof (uint64_t));
1214 		alloclen += srcidsize;
1215 		srcid = B_TRUE;
1216 	}
1217 
1218 	if (ipsa->ipsa_dst_cid != NULL) {
1219 		dstidsize = roundup(sizeof (sadb_ident_t) +
1220 		    strlen(ipsa->ipsa_dst_cid->ipsid_cid) + 1,
1221 		    sizeof (uint64_t));
1222 		alloclen += dstidsize;
1223 		dstid = B_TRUE;
1224 	}
1225 
1226 	if ((ipsa->ipsa_kmp != 0) || (ipsa->ipsa_kmc != 0))
1227 		alloclen += sizeof (sadb_x_kmc_t);
1228 
1229 	if (ipsa->ipsa_replay != 0) {
1230 		alloclen += sizeof (sadb_x_replay_ctr_t);
1231 	}
1232 
1233 	/* Make sure the allocation length is a multiple of 8 bytes. */
1234 	ASSERT((alloclen & 0x7) == 0);
1235 
1236 	/* XXX Possibly make it esballoc, with a bzero-ing free_ftn. */
1237 	mp = allocb(alloclen, BPRI_HI);
1238 	if (mp == NULL)
1239 		return (NULL);
1240 	bzero(mp->b_rptr, alloclen);
1241 
1242 	mp->b_wptr += alloclen;
1243 	end = mp->b_wptr;
1244 	newsamsg = (sadb_msg_t *)mp->b_rptr;
1245 	*newsamsg = *samsg;
1246 	newsamsg->sadb_msg_len = (uint16_t)SADB_8TO64(alloclen);
1247 
1248 	mutex_enter(&ipsa->ipsa_lock);	/* Since I'm grabbing SA fields... */
1249 
1250 	newsamsg->sadb_msg_satype = ipsa->ipsa_type;
1251 
1252 	assoc = (sadb_sa_t *)(newsamsg + 1);
1253 	assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc));
1254 	assoc->sadb_sa_exttype = SADB_EXT_SA;
1255 	assoc->sadb_sa_spi = ipsa->ipsa_spi;
1256 	assoc->sadb_sa_replay = ipsa->ipsa_replay_wsize;
1257 	assoc->sadb_sa_state = ipsa->ipsa_state;
1258 	assoc->sadb_sa_auth = ipsa->ipsa_auth_alg;
1259 	assoc->sadb_sa_encrypt = ipsa->ipsa_encr_alg;
1260 	assoc->sadb_sa_flags = ipsa->ipsa_flags;
1261 
1262 	lt = (sadb_lifetime_t *)(assoc + 1);
1263 	lt->sadb_lifetime_len = SADB_8TO64(sizeof (*lt));
1264 	lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1265 	/* We do not support the concept. */
1266 	lt->sadb_lifetime_allocations = 0;
1267 	lt->sadb_lifetime_bytes = ipsa->ipsa_bytes;
1268 	lt->sadb_lifetime_addtime = ipsa->ipsa_addtime;
1269 	lt->sadb_lifetime_usetime = ipsa->ipsa_usetime;
1270 
1271 	if (hard) {
1272 		lt++;
1273 		lt->sadb_lifetime_len = SADB_8TO64(sizeof (*lt));
1274 		lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1275 		lt->sadb_lifetime_allocations = ipsa->ipsa_hardalloc;
1276 		lt->sadb_lifetime_bytes = ipsa->ipsa_hardbyteslt;
1277 		lt->sadb_lifetime_addtime = ipsa->ipsa_hardaddlt;
1278 		lt->sadb_lifetime_usetime = ipsa->ipsa_harduselt;
1279 	}
1280 
1281 	if (soft) {
1282 		lt++;
1283 		lt->sadb_lifetime_len = SADB_8TO64(sizeof (*lt));
1284 		lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1285 		lt->sadb_lifetime_allocations = ipsa->ipsa_softalloc;
1286 		lt->sadb_lifetime_bytes = ipsa->ipsa_softbyteslt;
1287 		lt->sadb_lifetime_addtime = ipsa->ipsa_softaddlt;
1288 		lt->sadb_lifetime_usetime = ipsa->ipsa_softuselt;
1289 	}
1290 
1291 	if (idle) {
1292 		lt++;
1293 		lt->sadb_lifetime_len = SADB_8TO64(sizeof (*lt));
1294 		lt->sadb_lifetime_exttype = SADB_X_EXT_LIFETIME_IDLE;
1295 		lt->sadb_lifetime_addtime = ipsa->ipsa_idleaddlt;
1296 		lt->sadb_lifetime_usetime = ipsa->ipsa_idleuselt;
1297 	}
1298 
1299 	cur = (uint8_t *)(lt + 1);
1300 
1301 	/* NOTE:  Don't fill in ports here if we are a tunnel-mode SA. */
1302 	cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_SRC, fam,
1303 	    ipsa->ipsa_srcaddr, (!isrc && !idst) ? SA_SRCPORT(ipsa) : 0,
1304 	    SA_PROTO(ipsa), 0);
1305 	if (cur == NULL) {
1306 		freemsg(mp);
1307 		mp = NULL;
1308 		goto bail;
1309 	}
1310 
1311 	cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_DST, fam,
1312 	    ipsa->ipsa_dstaddr, (!isrc && !idst) ? SA_DSTPORT(ipsa) : 0,
1313 	    SA_PROTO(ipsa), 0);
1314 	if (cur == NULL) {
1315 		freemsg(mp);
1316 		mp = NULL;
1317 		goto bail;
1318 	}
1319 
1320 	if (ipsa->ipsa_flags & IPSA_F_NATT_LOC) {
1321 		cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_NATT_LOC,
1322 		    fam, &ipsa->ipsa_natt_addr_loc, ipsa->ipsa_local_nat_port,
1323 		    IPPROTO_UDP, 0);
1324 		if (cur == NULL) {
1325 			freemsg(mp);
1326 			mp = NULL;
1327 			goto bail;
1328 		}
1329 	}
1330 
1331 	if (ipsa->ipsa_flags & IPSA_F_NATT_REM) {
1332 		cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_NATT_REM,
1333 		    fam, &ipsa->ipsa_natt_addr_rem, ipsa->ipsa_remote_nat_port,
1334 		    IPPROTO_UDP, 0);
1335 		if (cur == NULL) {
1336 			freemsg(mp);
1337 			mp = NULL;
1338 			goto bail;
1339 		}
1340 	}
1341 
1342 	/* If we are a tunnel-mode SA, fill in the inner-selectors. */
1343 	if (isrc) {
1344 		cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_SRC,
1345 		    pfam, ipsa->ipsa_innersrc, SA_SRCPORT(ipsa),
1346 		    SA_IPROTO(ipsa), ipsa->ipsa_innersrcpfx);
1347 		if (cur == NULL) {
1348 			freemsg(mp);
1349 			mp = NULL;
1350 			goto bail;
1351 		}
1352 	}
1353 
1354 	if (idst) {
1355 		cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_DST,
1356 		    pfam, ipsa->ipsa_innerdst, SA_DSTPORT(ipsa),
1357 		    SA_IPROTO(ipsa), ipsa->ipsa_innerdstpfx);
1358 		if (cur == NULL) {
1359 			freemsg(mp);
1360 			mp = NULL;
1361 			goto bail;
1362 		}
1363 	}
1364 
1365 	if ((ipsa->ipsa_kmp != 0) || (ipsa->ipsa_kmc != 0)) {
1366 		cur = sadb_make_kmc_ext(cur, end,
1367 		    ipsa->ipsa_kmp, ipsa->ipsa_kmc);
1368 		if (cur == NULL) {
1369 			freemsg(mp);
1370 			mp = NULL;
1371 			goto bail;
1372 		}
1373 	}
1374 
1375 	walker = (sadb_ext_t *)cur;
1376 	if (auth) {
1377 		key = (sadb_key_t *)walker;
1378 		key->sadb_key_len = SADB_8TO64(authsize);
1379 		key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
1380 		key->sadb_key_bits = ipsa->ipsa_authkeybits;
1381 		key->sadb_key_reserved = 0;
1382 		bcopy(ipsa->ipsa_authkey, key + 1, ipsa->ipsa_authkeylen);
1383 		walker = (sadb_ext_t *)((uint64_t *)walker +
1384 		    walker->sadb_ext_len);
1385 	}
1386 
1387 	if (encr) {
1388 		uint8_t *buf_ptr;
1389 		key = (sadb_key_t *)walker;
1390 		key->sadb_key_len = SADB_8TO64(encrsize);
1391 		key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
1392 		key->sadb_key_bits = ipsa->ipsa_encrkeybits;
1393 		key->sadb_key_reserved = ipsa->ipsa_saltbits;
1394 		buf_ptr = (uint8_t *)(key + 1);
1395 		bcopy(ipsa->ipsa_encrkey, buf_ptr, ipsa->ipsa_encrkeylen);
1396 		if (ipsa->ipsa_salt != NULL) {
1397 			buf_ptr += ipsa->ipsa_encrkeylen;
1398 			bcopy(ipsa->ipsa_salt, buf_ptr, ipsa->ipsa_saltlen);
1399 		}
1400 		walker = (sadb_ext_t *)((uint64_t *)walker +
1401 		    walker->sadb_ext_len);
1402 	}
1403 
1404 	if (srcid) {
1405 		ident = (sadb_ident_t *)walker;
1406 		ident->sadb_ident_len = SADB_8TO64(srcidsize);
1407 		ident->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC;
1408 		ident->sadb_ident_type = ipsa->ipsa_src_cid->ipsid_type;
1409 		ident->sadb_ident_id = 0;
1410 		ident->sadb_ident_reserved = 0;
1411 		(void) strcpy((char *)(ident + 1),
1412 		    ipsa->ipsa_src_cid->ipsid_cid);
1413 		walker = (sadb_ext_t *)((uint64_t *)walker +
1414 		    walker->sadb_ext_len);
1415 	}
1416 
1417 	if (dstid) {
1418 		ident = (sadb_ident_t *)walker;
1419 		ident->sadb_ident_len = SADB_8TO64(dstidsize);
1420 		ident->sadb_ident_exttype = SADB_EXT_IDENTITY_DST;
1421 		ident->sadb_ident_type = ipsa->ipsa_dst_cid->ipsid_type;
1422 		ident->sadb_ident_id = 0;
1423 		ident->sadb_ident_reserved = 0;
1424 		(void) strcpy((char *)(ident + 1),
1425 		    ipsa->ipsa_dst_cid->ipsid_cid);
1426 		walker = (sadb_ext_t *)((uint64_t *)walker +
1427 		    walker->sadb_ext_len);
1428 	}
1429 
1430 	if (sensinteg) {
1431 		sens = (sadb_sens_t *)walker;
1432 		sadb_sens_from_label(sens, SADB_EXT_SENSITIVITY,
1433 		    ipsa->ipsa_tsl, senslen);
1434 
1435 		walker = (sadb_ext_t *)((uint64_t *)walker +
1436 		    walker->sadb_ext_len);
1437 	}
1438 
1439 	if (osensinteg) {
1440 		sens = (sadb_sens_t *)walker;
1441 
1442 		sadb_sens_from_label(sens, SADB_X_EXT_OUTER_SENS,
1443 		    ipsa->ipsa_otsl, osenslen);
1444 		if (ipsa->ipsa_mac_exempt)
1445 			sens->sadb_x_sens_flags = SADB_X_SENS_IMPLICIT;
1446 
1447 		walker = (sadb_ext_t *)((uint64_t *)walker +
1448 		    walker->sadb_ext_len);
1449 	}
1450 
1451 	if (paired) {
1452 		pair_ext = (sadb_x_pair_t *)walker;
1453 
1454 		pair_ext->sadb_x_pair_len = SADB_8TO64(sizeof (sadb_x_pair_t));
1455 		pair_ext->sadb_x_pair_exttype = SADB_X_EXT_PAIR;
1456 		pair_ext->sadb_x_pair_spi = otherspi;
1457 
1458 		walker = (sadb_ext_t *)((uint64_t *)walker +
1459 		    walker->sadb_ext_len);
1460 	}
1461 
1462 	if (ipsa->ipsa_replay != 0) {
1463 		repl_ctr = (sadb_x_replay_ctr_t *)walker;
1464 		repl_ctr->sadb_x_rc_len = SADB_8TO64(sizeof (*repl_ctr));
1465 		repl_ctr->sadb_x_rc_exttype = SADB_X_EXT_REPLAY_VALUE;
1466 		repl_ctr->sadb_x_rc_replay32 = ipsa->ipsa_replay;
1467 		repl_ctr->sadb_x_rc_replay64 = 0;
1468 		walker = (sadb_ext_t *)(repl_ctr + 1);
1469 	}
1470 
1471 bail:
1472 	/* Pardon any delays... */
1473 	mutex_exit(&ipsa->ipsa_lock);
1474 
1475 	return (mp);
1476 }
1477 
1478 /*
1479  * Strip out key headers or unmarked headers (SADB_EXT_KEY_*, SADB_EXT_UNKNOWN)
1480  * and adjust base message accordingly.
1481  *
1482  * Assume message is pulled up in one piece of contiguous memory.
1483  *
1484  * Say if we start off with:
1485  *
1486  * +------+----+-------------+-----------+---------------+---------------+
1487  * | base | SA | source addr | dest addr | rsrvd. or key | soft lifetime |
1488  * +------+----+-------------+-----------+---------------+---------------+
1489  *
1490  * we will end up with
1491  *
1492  * +------+----+-------------+-----------+---------------+
1493  * | base | SA | source addr | dest addr | soft lifetime |
1494  * +------+----+-------------+-----------+---------------+
1495  */
1496 static void
1497 sadb_strip(sadb_msg_t *samsg)
1498 {
1499 	sadb_ext_t *ext;
1500 	uint8_t *target = NULL;
1501 	uint8_t *msgend;
1502 	int sofar = SADB_8TO64(sizeof (*samsg));
1503 	int copylen;
1504 
1505 	ext = (sadb_ext_t *)(samsg + 1);
1506 	msgend = (uint8_t *)samsg;
1507 	msgend += SADB_64TO8(samsg->sadb_msg_len);
1508 	while ((uint8_t *)ext < msgend) {
1509 		if (ext->sadb_ext_type == SADB_EXT_RESERVED ||
1510 		    ext->sadb_ext_type == SADB_EXT_KEY_AUTH ||
1511 		    ext->sadb_ext_type == SADB_X_EXT_EDUMP ||
1512 		    ext->sadb_ext_type == SADB_EXT_KEY_ENCRYPT) {
1513 			/*
1514 			 * Aha!	 I found a header to be erased.
1515 			 */
1516 
1517 			if (target != NULL) {
1518 				/*
1519 				 * If I had a previous header to be erased,
1520 				 * copy over it.  I can get away with just
1521 				 * copying backwards because the target will
1522 				 * always be 8 bytes behind the source.
1523 				 */
1524 				copylen = ((uint8_t *)ext) - (target +
1525 				    SADB_64TO8(
1526 				    ((sadb_ext_t *)target)->sadb_ext_len));
1527 				ovbcopy(((uint8_t *)ext - copylen), target,
1528 				    copylen);
1529 				target += copylen;
1530 				((sadb_ext_t *)target)->sadb_ext_len =
1531 				    SADB_8TO64(((uint8_t *)ext) - target +
1532 				    SADB_64TO8(ext->sadb_ext_len));
1533 			} else {
1534 				target = (uint8_t *)ext;
1535 			}
1536 		} else {
1537 			sofar += ext->sadb_ext_len;
1538 		}
1539 
1540 		ext = (sadb_ext_t *)(((uint64_t *)ext) + ext->sadb_ext_len);
1541 	}
1542 
1543 	ASSERT((uint8_t *)ext == msgend);
1544 
1545 	if (target != NULL) {
1546 		copylen = ((uint8_t *)ext) - (target +
1547 		    SADB_64TO8(((sadb_ext_t *)target)->sadb_ext_len));
1548 		if (copylen != 0)
1549 			ovbcopy(((uint8_t *)ext - copylen), target, copylen);
1550 	}
1551 
1552 	/* Adjust samsg. */
1553 	samsg->sadb_msg_len = (uint16_t)sofar;
1554 
1555 	/* Assume all of the rest is cleared by caller in sadb_pfkey_echo(). */
1556 }
1557 
1558 /*
1559  * AH needs to send an error to PF_KEY.	 Assume mp points to an M_CTL
1560  * followed by an M_DATA with a PF_KEY message in it.  The serial of
1561  * the sending keysock instance is included.
1562  */
1563 void
1564 sadb_pfkey_error(queue_t *pfkey_q, mblk_t *mp, int error, int diagnostic,
1565     uint_t serial)
1566 {
1567 	mblk_t *msg = mp->b_cont;
1568 	sadb_msg_t *samsg;
1569 	keysock_out_t *kso;
1570 
1571 	/*
1572 	 * Enough functions call this to merit a NULL queue check.
1573 	 */
1574 	if (pfkey_q == NULL) {
1575 		freemsg(mp);
1576 		return;
1577 	}
1578 
1579 	ASSERT(msg != NULL);
1580 	ASSERT((mp->b_wptr - mp->b_rptr) == sizeof (ipsec_info_t));
1581 	ASSERT((msg->b_wptr - msg->b_rptr) >= sizeof (sadb_msg_t));
1582 	samsg = (sadb_msg_t *)msg->b_rptr;
1583 	kso = (keysock_out_t *)mp->b_rptr;
1584 
1585 	kso->ks_out_type = KEYSOCK_OUT;
1586 	kso->ks_out_len = sizeof (*kso);
1587 	kso->ks_out_serial = serial;
1588 
1589 	/*
1590 	 * Only send the base message up in the event of an error.
1591 	 * Don't worry about bzero()-ing, because it was probably bogus
1592 	 * anyway.
1593 	 */
1594 	msg->b_wptr = msg->b_rptr + sizeof (*samsg);
1595 	samsg = (sadb_msg_t *)msg->b_rptr;
1596 	samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
1597 	samsg->sadb_msg_errno = (uint8_t)error;
1598 	if (diagnostic != SADB_X_DIAGNOSTIC_PRESET)
1599 		samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic;
1600 
1601 	putnext(pfkey_q, mp);
1602 }
1603 
1604 /*
1605  * Send a successful return packet back to keysock via the queue in pfkey_q.
1606  *
1607  * Often, an SA is associated with the reply message, it's passed in if needed,
1608  * and NULL if not.  BTW, that ipsa will have its refcnt appropriately held,
1609  * and the caller will release said refcnt.
1610  */
1611 void
1612 sadb_pfkey_echo(queue_t *pfkey_q, mblk_t *mp, sadb_msg_t *samsg,
1613     keysock_in_t *ksi, ipsa_t *ipsa)
1614 {
1615 	keysock_out_t *kso;
1616 	mblk_t *mp1;
1617 	sadb_msg_t *newsamsg;
1618 	uint8_t *oldend;
1619 
1620 	ASSERT((mp->b_cont != NULL) &&
1621 	    ((void *)samsg == (void *)mp->b_cont->b_rptr) &&
1622 	    ((void *)mp->b_rptr == (void *)ksi));
1623 
1624 	switch (samsg->sadb_msg_type) {
1625 	case SADB_ADD:
1626 	case SADB_UPDATE:
1627 	case SADB_X_UPDATEPAIR:
1628 	case SADB_X_DELPAIR_STATE:
1629 	case SADB_FLUSH:
1630 	case SADB_DUMP:
1631 		/*
1632 		 * I have all of the message already.  I just need to strip
1633 		 * out the keying material and echo the message back.
1634 		 *
1635 		 * NOTE: for SADB_DUMP, the function sadb_dump() did the
1636 		 * work.  When DUMP reaches here, it should only be a base
1637 		 * message.
1638 		 */
1639 	justecho:
1640 		if (ksi->ks_in_extv[SADB_EXT_KEY_AUTH] != NULL ||
1641 		    ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL ||
1642 		    ksi->ks_in_extv[SADB_X_EXT_EDUMP] != NULL) {
1643 			sadb_strip(samsg);
1644 			/* Assume PF_KEY message is contiguous. */
1645 			ASSERT(mp->b_cont->b_cont == NULL);
1646 			oldend = mp->b_cont->b_wptr;
1647 			mp->b_cont->b_wptr = mp->b_cont->b_rptr +
1648 			    SADB_64TO8(samsg->sadb_msg_len);
1649 			bzero(mp->b_cont->b_wptr, oldend - mp->b_cont->b_wptr);
1650 		}
1651 		break;
1652 	case SADB_GET:
1653 		/*
1654 		 * Do a lot of work here, because of the ipsa I just found.
1655 		 * First construct the new PF_KEY message, then abandon
1656 		 * the old one.
1657 		 */
1658 		mp1 = sadb_sa2msg(ipsa, samsg);
1659 		if (mp1 == NULL) {
1660 			sadb_pfkey_error(pfkey_q, mp, ENOMEM,
1661 			    SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
1662 			return;
1663 		}
1664 		freemsg(mp->b_cont);
1665 		mp->b_cont = mp1;
1666 		break;
1667 	case SADB_DELETE:
1668 	case SADB_X_DELPAIR:
1669 		if (ipsa == NULL)
1670 			goto justecho;
1671 		/*
1672 		 * Because listening KMds may require more info, treat
1673 		 * DELETE like a special case of GET.
1674 		 */
1675 		mp1 = sadb_sa2msg(ipsa, samsg);
1676 		if (mp1 == NULL) {
1677 			sadb_pfkey_error(pfkey_q, mp, ENOMEM,
1678 			    SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
1679 			return;
1680 		}
1681 		newsamsg = (sadb_msg_t *)mp1->b_rptr;
1682 		sadb_strip(newsamsg);
1683 		oldend = mp1->b_wptr;
1684 		mp1->b_wptr = mp1->b_rptr + SADB_64TO8(newsamsg->sadb_msg_len);
1685 		bzero(mp1->b_wptr, oldend - mp1->b_wptr);
1686 		freemsg(mp->b_cont);
1687 		mp->b_cont = mp1;
1688 		break;
1689 	default:
1690 		freemsg(mp);
1691 		return;
1692 	}
1693 
1694 	/* ksi is now null and void. */
1695 	kso = (keysock_out_t *)ksi;
1696 	kso->ks_out_type = KEYSOCK_OUT;
1697 	kso->ks_out_len = sizeof (*kso);
1698 	kso->ks_out_serial = ksi->ks_in_serial;
1699 	/* We're ready to send... */
1700 	putnext(pfkey_q, mp);
1701 }
1702 
1703 /*
1704  * Set up a global pfkey_q instance for AH, ESP, or some other consumer.
1705  */
1706 void
1707 sadb_keysock_hello(queue_t **pfkey_qp, queue_t *q, mblk_t *mp,
1708     void (*ager)(void *), void *agerarg, timeout_id_t *top, int satype)
1709 {
1710 	keysock_hello_ack_t *kha;
1711 	queue_t *oldq;
1712 
1713 	ASSERT(OTHERQ(q) != NULL);
1714 
1715 	/*
1716 	 * First, check atomically that I'm the first and only keysock
1717 	 * instance.
1718 	 *
1719 	 * Use OTHERQ(q), because qreply(q, mp) == putnext(OTHERQ(q), mp),
1720 	 * and I want this module to say putnext(*_pfkey_q, mp) for PF_KEY
1721 	 * messages.
1722 	 */
1723 
1724 	oldq = atomic_cas_ptr((void **)pfkey_qp, NULL, OTHERQ(q));
1725 	if (oldq != NULL) {
1726 		ASSERT(oldq != q);
1727 		cmn_err(CE_WARN, "Danger!  Multiple keysocks on top of %s.\n",
1728 		    (satype == SADB_SATYPE_ESP)? "ESP" : "AH or other");
1729 		freemsg(mp);
1730 		return;
1731 	}
1732 
1733 	kha = (keysock_hello_ack_t *)mp->b_rptr;
1734 	kha->ks_hello_len = sizeof (keysock_hello_ack_t);
1735 	kha->ks_hello_type = KEYSOCK_HELLO_ACK;
1736 	kha->ks_hello_satype = (uint8_t)satype;
1737 
1738 	/*
1739 	 * If we made it past the atomic_cas_ptr, then we have "exclusive"
1740 	 * access to the timeout handle.  Fire it off after the default ager
1741 	 * interval.
1742 	 */
1743 	*top = qtimeout(*pfkey_qp, ager, agerarg,
1744 	    drv_usectohz(SADB_AGE_INTERVAL_DEFAULT * 1000));
1745 
1746 	putnext(*pfkey_qp, mp);
1747 }
1748 
1749 /*
1750  * Normalize IPv4-mapped IPv6 addresses (and prefixes) as appropriate.
1751  *
1752  * Check addresses themselves for wildcard or multicast.
1753  * Check ire table for local/non-local/broadcast.
1754  */
1755 int
1756 sadb_addrcheck(queue_t *pfkey_q, mblk_t *mp, sadb_ext_t *ext, uint_t serial,
1757     netstack_t *ns)
1758 {
1759 	sadb_address_t *addr = (sadb_address_t *)ext;
1760 	struct sockaddr_in *sin;
1761 	struct sockaddr_in6 *sin6;
1762 	int diagnostic, type;
1763 	boolean_t normalized = B_FALSE;
1764 
1765 	ASSERT(ext != NULL);
1766 	ASSERT((ext->sadb_ext_type == SADB_EXT_ADDRESS_SRC) ||
1767 	    (ext->sadb_ext_type == SADB_EXT_ADDRESS_DST) ||
1768 	    (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_SRC) ||
1769 	    (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_DST) ||
1770 	    (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_NATT_LOC) ||
1771 	    (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_NATT_REM));
1772 
1773 	/* Assign both sockaddrs, the compiler will do the right thing. */
1774 	sin = (struct sockaddr_in *)(addr + 1);
1775 	sin6 = (struct sockaddr_in6 *)(addr + 1);
1776 
1777 	if (sin6->sin6_family == AF_INET6) {
1778 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1779 			/*
1780 			 * Convert to an AF_INET sockaddr.  This means the
1781 			 * return messages will have the extra space, but have
1782 			 * AF_INET sockaddrs instead of AF_INET6.
1783 			 *
1784 			 * Yes, RFC 2367 isn't clear on what to do here w.r.t.
1785 			 * mapped addresses, but since AF_INET6 ::ffff:<v4> is
1786 			 * equal to AF_INET <v4>, it shouldnt be a huge
1787 			 * problem.
1788 			 */
1789 			sin->sin_family = AF_INET;
1790 			IN6_V4MAPPED_TO_INADDR(&sin6->sin6_addr,
1791 			    &sin->sin_addr);
1792 			bzero(&sin->sin_zero, sizeof (sin->sin_zero));
1793 			normalized = B_TRUE;
1794 		}
1795 	} else if (sin->sin_family != AF_INET) {
1796 		switch (ext->sadb_ext_type) {
1797 		case SADB_EXT_ADDRESS_SRC:
1798 			diagnostic = SADB_X_DIAGNOSTIC_BAD_SRC_AF;
1799 			break;
1800 		case SADB_EXT_ADDRESS_DST:
1801 			diagnostic = SADB_X_DIAGNOSTIC_BAD_DST_AF;
1802 			break;
1803 		case SADB_X_EXT_ADDRESS_INNER_SRC:
1804 			diagnostic = SADB_X_DIAGNOSTIC_BAD_PROXY_AF;
1805 			break;
1806 		case SADB_X_EXT_ADDRESS_INNER_DST:
1807 			diagnostic = SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF;
1808 			break;
1809 		case SADB_X_EXT_ADDRESS_NATT_LOC:
1810 			diagnostic = SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF;
1811 			break;
1812 		case SADB_X_EXT_ADDRESS_NATT_REM:
1813 			diagnostic = SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF;
1814 			break;
1815 			/* There is no default, see above ASSERT. */
1816 		}
1817 bail:
1818 		if (pfkey_q != NULL) {
1819 			sadb_pfkey_error(pfkey_q, mp, EINVAL, diagnostic,
1820 			    serial);
1821 		} else {
1822 			/*
1823 			 * Scribble in sadb_msg that we got passed in.
1824 			 * Overload "mp" to be an sadb_msg pointer.
1825 			 */
1826 			sadb_msg_t *samsg = (sadb_msg_t *)mp;
1827 
1828 			samsg->sadb_msg_errno = EINVAL;
1829 			samsg->sadb_x_msg_diagnostic = diagnostic;
1830 		}
1831 		return (KS_IN_ADDR_UNKNOWN);
1832 	}
1833 
1834 	if (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_SRC ||
1835 	    ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_DST) {
1836 		/*
1837 		 * We need only check for prefix issues.
1838 		 */
1839 
1840 		/* Set diagnostic now, in case we need it later. */
1841 		diagnostic =
1842 		    (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_SRC) ?
1843 		    SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC :
1844 		    SADB_X_DIAGNOSTIC_PREFIX_INNER_DST;
1845 
1846 		if (normalized)
1847 			addr->sadb_address_prefixlen -= 96;
1848 
1849 		/*
1850 		 * Verify and mask out inner-addresses based on prefix length.
1851 		 */
1852 		if (sin->sin_family == AF_INET) {
1853 			if (addr->sadb_address_prefixlen > 32)
1854 				goto bail;
1855 			sin->sin_addr.s_addr &=
1856 			    ip_plen_to_mask(addr->sadb_address_prefixlen);
1857 		} else {
1858 			in6_addr_t mask;
1859 
1860 			ASSERT(sin->sin_family == AF_INET6);
1861 			/*
1862 			 * ip_plen_to_mask_v6() returns NULL if the value in
1863 			 * question is out of range.
1864 			 */
1865 			if (ip_plen_to_mask_v6(addr->sadb_address_prefixlen,
1866 			    &mask) == NULL)
1867 				goto bail;
1868 			sin6->sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1869 			sin6->sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1870 			sin6->sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1871 			sin6->sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1872 		}
1873 
1874 		/* We don't care in these cases. */
1875 		return (KS_IN_ADDR_DONTCARE);
1876 	}
1877 
1878 	if (sin->sin_family == AF_INET6) {
1879 		/* Check the easy ones now. */
1880 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1881 			return (KS_IN_ADDR_MBCAST);
1882 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1883 			return (KS_IN_ADDR_UNSPEC);
1884 		/*
1885 		 * At this point, we're a unicast IPv6 address.
1886 		 *
1887 		 * XXX Zones alert -> me/notme decision needs to be tempered
1888 		 * by what zone we're in when we go to zone-aware IPsec.
1889 		 */
1890 		if (ip_type_v6(&sin6->sin6_addr, ns->netstack_ip) ==
1891 		    IRE_LOCAL) {
1892 			/* Hey hey, it's local. */
1893 			return (KS_IN_ADDR_ME);
1894 		}
1895 	} else {
1896 		ASSERT(sin->sin_family == AF_INET);
1897 		if (sin->sin_addr.s_addr == INADDR_ANY)
1898 			return (KS_IN_ADDR_UNSPEC);
1899 		if (CLASSD(sin->sin_addr.s_addr))
1900 			return (KS_IN_ADDR_MBCAST);
1901 		/*
1902 		 * At this point we're a unicast or broadcast IPv4 address.
1903 		 *
1904 		 * Check if the address is IRE_BROADCAST or IRE_LOCAL.
1905 		 *
1906 		 * XXX Zones alert -> me/notme decision needs to be tempered
1907 		 * by what zone we're in when we go to zone-aware IPsec.
1908 		 */
1909 		type = ip_type_v4(sin->sin_addr.s_addr, ns->netstack_ip);
1910 		switch (type) {
1911 		case IRE_LOCAL:
1912 			return (KS_IN_ADDR_ME);
1913 		case IRE_BROADCAST:
1914 			return (KS_IN_ADDR_MBCAST);
1915 		}
1916 	}
1917 
1918 	return (KS_IN_ADDR_NOTME);
1919 }
1920 
1921 /*
1922  * Address normalizations and reality checks for inbound PF_KEY messages.
1923  *
1924  * For the case of src == unspecified AF_INET6, and dst == AF_INET, convert
1925  * the source to AF_INET.  Do the same for the inner sources.
1926  */
1927 boolean_t
1928 sadb_addrfix(keysock_in_t *ksi, queue_t *pfkey_q, mblk_t *mp, netstack_t *ns)
1929 {
1930 	struct sockaddr_in *src, *isrc;
1931 	struct sockaddr_in6 *dst, *idst;
1932 	sadb_address_t *srcext, *dstext;
1933 	uint16_t sport;
1934 	sadb_ext_t **extv = ksi->ks_in_extv;
1935 	int rc;
1936 
1937 	if (extv[SADB_EXT_ADDRESS_SRC] != NULL) {
1938 		rc = sadb_addrcheck(pfkey_q, mp, extv[SADB_EXT_ADDRESS_SRC],
1939 		    ksi->ks_in_serial, ns);
1940 		if (rc == KS_IN_ADDR_UNKNOWN)
1941 			return (B_FALSE);
1942 		if (rc == KS_IN_ADDR_MBCAST) {
1943 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
1944 			    SADB_X_DIAGNOSTIC_BAD_SRC, ksi->ks_in_serial);
1945 			return (B_FALSE);
1946 		}
1947 		ksi->ks_in_srctype = rc;
1948 	}
1949 
1950 	if (extv[SADB_EXT_ADDRESS_DST] != NULL) {
1951 		rc = sadb_addrcheck(pfkey_q, mp, extv[SADB_EXT_ADDRESS_DST],
1952 		    ksi->ks_in_serial, ns);
1953 		if (rc == KS_IN_ADDR_UNKNOWN)
1954 			return (B_FALSE);
1955 		if (rc == KS_IN_ADDR_UNSPEC) {
1956 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
1957 			    SADB_X_DIAGNOSTIC_BAD_DST, ksi->ks_in_serial);
1958 			return (B_FALSE);
1959 		}
1960 		ksi->ks_in_dsttype = rc;
1961 	}
1962 
1963 	/*
1964 	 * NAT-Traversal addrs are simple enough to not require all of
1965 	 * the checks in sadb_addrcheck().  Just normalize or reject if not
1966 	 * AF_INET.
1967 	 */
1968 	if (extv[SADB_X_EXT_ADDRESS_NATT_LOC] != NULL) {
1969 		rc = sadb_addrcheck(pfkey_q, mp,
1970 		    extv[SADB_X_EXT_ADDRESS_NATT_LOC], ksi->ks_in_serial, ns);
1971 
1972 		/*
1973 		 * Local NAT-T addresses never use an IRE_LOCAL, so it should
1974 		 * always be NOTME, or UNSPEC (to handle both tunnel mode
1975 		 * AND local-port flexibility).
1976 		 */
1977 		if (rc != KS_IN_ADDR_NOTME && rc != KS_IN_ADDR_UNSPEC) {
1978 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
1979 			    SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC,
1980 			    ksi->ks_in_serial);
1981 			return (B_FALSE);
1982 		}
1983 		src = (struct sockaddr_in *)
1984 		    (((sadb_address_t *)extv[SADB_X_EXT_ADDRESS_NATT_LOC]) + 1);
1985 		if (src->sin_family != AF_INET) {
1986 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
1987 			    SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF,
1988 			    ksi->ks_in_serial);
1989 			return (B_FALSE);
1990 		}
1991 	}
1992 
1993 	if (extv[SADB_X_EXT_ADDRESS_NATT_REM] != NULL) {
1994 		rc = sadb_addrcheck(pfkey_q, mp,
1995 		    extv[SADB_X_EXT_ADDRESS_NATT_REM], ksi->ks_in_serial, ns);
1996 
1997 		/*
1998 		 * Remote NAT-T addresses never use an IRE_LOCAL, so it should
1999 		 * always be NOTME, or UNSPEC if it's a tunnel-mode SA.
2000 		 */
2001 		if (rc != KS_IN_ADDR_NOTME &&
2002 		    !(extv[SADB_X_EXT_ADDRESS_INNER_SRC] != NULL &&
2003 		    rc == KS_IN_ADDR_UNSPEC)) {
2004 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
2005 			    SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM,
2006 			    ksi->ks_in_serial);
2007 			return (B_FALSE);
2008 		}
2009 		src = (struct sockaddr_in *)
2010 		    (((sadb_address_t *)extv[SADB_X_EXT_ADDRESS_NATT_REM]) + 1);
2011 		if (src->sin_family != AF_INET) {
2012 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
2013 			    SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF,
2014 			    ksi->ks_in_serial);
2015 			return (B_FALSE);
2016 		}
2017 	}
2018 
2019 	if (extv[SADB_X_EXT_ADDRESS_INNER_SRC] != NULL) {
2020 		if (extv[SADB_X_EXT_ADDRESS_INNER_DST] == NULL) {
2021 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
2022 			    SADB_X_DIAGNOSTIC_MISSING_INNER_DST,
2023 			    ksi->ks_in_serial);
2024 			return (B_FALSE);
2025 		}
2026 
2027 		if (sadb_addrcheck(pfkey_q, mp,
2028 		    extv[SADB_X_EXT_ADDRESS_INNER_DST], ksi->ks_in_serial, ns)
2029 		    == KS_IN_ADDR_UNKNOWN ||
2030 		    sadb_addrcheck(pfkey_q, mp,
2031 		    extv[SADB_X_EXT_ADDRESS_INNER_SRC], ksi->ks_in_serial, ns)
2032 		    == KS_IN_ADDR_UNKNOWN)
2033 			return (B_FALSE);
2034 
2035 		isrc = (struct sockaddr_in *)
2036 		    (((sadb_address_t *)extv[SADB_X_EXT_ADDRESS_INNER_SRC]) +
2037 		    1);
2038 		idst = (struct sockaddr_in6 *)
2039 		    (((sadb_address_t *)extv[SADB_X_EXT_ADDRESS_INNER_DST]) +
2040 		    1);
2041 		if (isrc->sin_family != idst->sin6_family) {
2042 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
2043 			    SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH,
2044 			    ksi->ks_in_serial);
2045 			return (B_FALSE);
2046 		}
2047 	} else if (extv[SADB_X_EXT_ADDRESS_INNER_DST] != NULL) {
2048 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
2049 			    SADB_X_DIAGNOSTIC_MISSING_INNER_SRC,
2050 			    ksi->ks_in_serial);
2051 			return (B_FALSE);
2052 	} else {
2053 		isrc = NULL;	/* For inner/outer port check below. */
2054 	}
2055 
2056 	dstext = (sadb_address_t *)extv[SADB_EXT_ADDRESS_DST];
2057 	srcext = (sadb_address_t *)extv[SADB_EXT_ADDRESS_SRC];
2058 
2059 	if (dstext == NULL || srcext == NULL)
2060 		return (B_TRUE);
2061 
2062 	dst = (struct sockaddr_in6 *)(dstext + 1);
2063 	src = (struct sockaddr_in *)(srcext + 1);
2064 
2065 	if (isrc != NULL &&
2066 	    (isrc->sin_port != 0 || idst->sin6_port != 0) &&
2067 	    (src->sin_port != 0 || dst->sin6_port != 0)) {
2068 		/* Can't set inner and outer ports in one SA. */
2069 		sadb_pfkey_error(pfkey_q, mp, EINVAL,
2070 		    SADB_X_DIAGNOSTIC_DUAL_PORT_SETS,
2071 		    ksi->ks_in_serial);
2072 		return (B_FALSE);
2073 	}
2074 
2075 	if (dst->sin6_family == src->sin_family)
2076 		return (B_TRUE);
2077 
2078 	if (srcext->sadb_address_proto != dstext->sadb_address_proto) {
2079 		if (srcext->sadb_address_proto == 0) {
2080 			srcext->sadb_address_proto = dstext->sadb_address_proto;
2081 		} else if (dstext->sadb_address_proto == 0) {
2082 			dstext->sadb_address_proto = srcext->sadb_address_proto;
2083 		} else {
2084 			/* Inequal protocols, neither were 0.  Report error. */
2085 			sadb_pfkey_error(pfkey_q, mp, EINVAL,
2086 			    SADB_X_DIAGNOSTIC_PROTO_MISMATCH,
2087 			    ksi->ks_in_serial);
2088 			return (B_FALSE);
2089 		}
2090 	}
2091 
2092 	/*
2093 	 * With the exception of an unspec IPv6 source and an IPv4
2094 	 * destination, address families MUST me matched.
2095 	 */
2096 	if (src->sin_family == AF_INET ||
2097 	    ksi->ks_in_srctype != KS_IN_ADDR_UNSPEC) {
2098 		sadb_pfkey_error(pfkey_q, mp, EINVAL,
2099 		    SADB_X_DIAGNOSTIC_AF_MISMATCH, ksi->ks_in_serial);
2100 		return (B_FALSE);
2101 	}
2102 
2103 	/*
2104 	 * Convert "src" to AF_INET INADDR_ANY.  We rely on sin_port being
2105 	 * in the same place for sockaddr_in and sockaddr_in6.
2106 	 */
2107 	sport = src->sin_port;
2108 	bzero(src, sizeof (*src));
2109 	src->sin_family = AF_INET;
2110 	src->sin_port = sport;
2111 
2112 	return (B_TRUE);
2113 }
2114 
2115 /*
2116  * Set the results in "addrtype", given an IRE as requested by
2117  * sadb_addrcheck().
2118  */
2119 int
2120 sadb_addrset(ire_t *ire)
2121 {
2122 	if ((ire->ire_type & IRE_BROADCAST) ||
2123 	    (ire->ire_ipversion == IPV4_VERSION && CLASSD(ire->ire_addr)) ||
2124 	    (ire->ire_ipversion == IPV6_VERSION &&
2125 	    IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))))
2126 		return (KS_IN_ADDR_MBCAST);
2127 	if (ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK))
2128 		return (KS_IN_ADDR_ME);
2129 	return (KS_IN_ADDR_NOTME);
2130 }
2131 
2132 /*
2133  * Match primitives..
2134  * !!! TODO: short term: inner selectors
2135  *		ipv6 scope id (ifindex)
2136  * longer term:  zone id.  sensitivity label. uid.
2137  */
2138 boolean_t
2139 sadb_match_spi(ipsa_query_t *sq, ipsa_t *sa)
2140 {
2141 	return (sq->spi == sa->ipsa_spi);
2142 }
2143 
2144 boolean_t
2145 sadb_match_dst_v6(ipsa_query_t *sq, ipsa_t *sa)
2146 {
2147 	return (IPSA_ARE_ADDR_EQUAL(sa->ipsa_dstaddr, sq->dstaddr, AF_INET6));
2148 }
2149 
2150 boolean_t
2151 sadb_match_src_v6(ipsa_query_t *sq, ipsa_t *sa)
2152 {
2153 	return (IPSA_ARE_ADDR_EQUAL(sa->ipsa_srcaddr, sq->srcaddr, AF_INET6));
2154 }
2155 
2156 boolean_t
2157 sadb_match_dst_v4(ipsa_query_t *sq, ipsa_t *sa)
2158 {
2159 	return (sq->dstaddr[0] == sa->ipsa_dstaddr[0]);
2160 }
2161 
2162 boolean_t
2163 sadb_match_src_v4(ipsa_query_t *sq, ipsa_t *sa)
2164 {
2165 	return (sq->srcaddr[0] == sa->ipsa_srcaddr[0]);
2166 }
2167 
2168 boolean_t
2169 sadb_match_dstid(ipsa_query_t *sq, ipsa_t *sa)
2170 {
2171 	return ((sa->ipsa_dst_cid != NULL) &&
2172 	    (sq->didtype == sa->ipsa_dst_cid->ipsid_type) &&
2173 	    (strcmp(sq->didstr, sa->ipsa_dst_cid->ipsid_cid) == 0));
2174 
2175 }
2176 boolean_t
2177 sadb_match_srcid(ipsa_query_t *sq, ipsa_t *sa)
2178 {
2179 	return ((sa->ipsa_src_cid != NULL) &&
2180 	    (sq->sidtype == sa->ipsa_src_cid->ipsid_type) &&
2181 	    (strcmp(sq->sidstr, sa->ipsa_src_cid->ipsid_cid) == 0));
2182 }
2183 
2184 boolean_t
2185 sadb_match_kmc(ipsa_query_t *sq, ipsa_t *sa)
2186 {
2187 #define	M(a, b) (((a) == 0) || ((b) == 0) || ((a) == (b)))
2188 
2189 	return (M(sq->kmc, sa->ipsa_kmc) && M(sq->kmp, sa->ipsa_kmp));
2190 
2191 #undef M
2192 }
2193 
2194 /*
2195  * Common function which extracts several PF_KEY extensions for ease of
2196  * SADB matching.
2197  *
2198  * XXX TODO: weed out ipsa_query_t fields not used during matching
2199  * or afterwards?
2200  */
2201 int
2202 sadb_form_query(keysock_in_t *ksi, uint32_t req, uint32_t match,
2203     ipsa_query_t *sq, int *diagnostic)
2204 {
2205 	int i;
2206 	ipsa_match_fn_t *mfpp = &(sq->matchers[0]);
2207 
2208 	for (i = 0; i < IPSA_NMATCH; i++)
2209 		sq->matchers[i] = NULL;
2210 
2211 	ASSERT((req & ~match) == 0);
2212 
2213 	sq->req = req;
2214 	sq->dstext = (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
2215 	sq->srcext = (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
2216 	sq->assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
2217 
2218 	if ((req & IPSA_Q_DST) && (sq->dstext == NULL)) {
2219 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
2220 		return (EINVAL);
2221 	}
2222 	if ((req & IPSA_Q_SRC) && (sq->srcext == NULL)) {
2223 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
2224 		return (EINVAL);
2225 	}
2226 	if ((req & IPSA_Q_SA) && (sq->assoc == NULL)) {
2227 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
2228 		return (EINVAL);
2229 	}
2230 
2231 	if (match & IPSA_Q_SA) {
2232 		*mfpp++ = sadb_match_spi;
2233 		sq->spi = sq->assoc->sadb_sa_spi;
2234 	}
2235 
2236 	if (sq->dstext != NULL)
2237 		sq->dst = (struct sockaddr_in *)(sq->dstext + 1);
2238 	else {
2239 		sq->dst = NULL;
2240 		sq->dst6 = NULL;
2241 		sq->dstaddr = NULL;
2242 	}
2243 
2244 	if (sq->srcext != NULL)
2245 		sq->src = (struct sockaddr_in *)(sq->srcext + 1);
2246 	else {
2247 		sq->src = NULL;
2248 		sq->src6 = NULL;
2249 		sq->srcaddr = NULL;
2250 	}
2251 
2252 	if (sq->dst != NULL)
2253 		sq->af = sq->dst->sin_family;
2254 	else if (sq->src != NULL)
2255 		sq->af = sq->src->sin_family;
2256 	else
2257 		sq->af = AF_INET;
2258 
2259 	if (sq->af == AF_INET6) {
2260 		if ((match & IPSA_Q_DST) && (sq->dstext != NULL)) {
2261 			*mfpp++ = sadb_match_dst_v6;
2262 			sq->dst6 = (struct sockaddr_in6 *)sq->dst;
2263 			sq->dstaddr = (uint32_t *)&(sq->dst6->sin6_addr);
2264 		} else {
2265 			match &= ~IPSA_Q_DST;
2266 			sq->dstaddr = ALL_ZEROES_PTR;
2267 		}
2268 
2269 		if ((match & IPSA_Q_SRC) && (sq->srcext != NULL)) {
2270 			sq->src6 = (struct sockaddr_in6 *)(sq->srcext + 1);
2271 			sq->srcaddr = (uint32_t *)&sq->src6->sin6_addr;
2272 			if (sq->src6->sin6_family != AF_INET6) {
2273 				*diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH;
2274 				return (EINVAL);
2275 			}
2276 			*mfpp++ = sadb_match_src_v6;
2277 		} else {
2278 			match &= ~IPSA_Q_SRC;
2279 			sq->srcaddr = ALL_ZEROES_PTR;
2280 		}
2281 	} else {
2282 		sq->src6 = sq->dst6 = NULL;
2283 		if ((match & IPSA_Q_DST) && (sq->dstext != NULL)) {
2284 			*mfpp++ = sadb_match_dst_v4;
2285 			sq->dstaddr = (uint32_t *)&sq->dst->sin_addr;
2286 		} else {
2287 			match &= ~IPSA_Q_DST;
2288 			sq->dstaddr = ALL_ZEROES_PTR;
2289 		}
2290 		if ((match & IPSA_Q_SRC) && (sq->srcext != NULL)) {
2291 			sq->srcaddr = (uint32_t *)&sq->src->sin_addr;
2292 			if (sq->src->sin_family != AF_INET) {
2293 				*diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH;
2294 				return (EINVAL);
2295 			}
2296 			*mfpp++ = sadb_match_src_v4;
2297 		} else {
2298 			match &= ~IPSA_Q_SRC;
2299 			sq->srcaddr = ALL_ZEROES_PTR;
2300 		}
2301 	}
2302 
2303 	sq->dstid = (sadb_ident_t *)ksi->ks_in_extv[SADB_EXT_IDENTITY_DST];
2304 	if ((match & IPSA_Q_DSTID) && (sq->dstid != NULL)) {
2305 		sq->didstr = (char *)(sq->dstid + 1);
2306 		sq->didtype = sq->dstid->sadb_ident_type;
2307 		*mfpp++ = sadb_match_dstid;
2308 	}
2309 
2310 	sq->srcid = (sadb_ident_t *)ksi->ks_in_extv[SADB_EXT_IDENTITY_SRC];
2311 
2312 	if ((match & IPSA_Q_SRCID) && (sq->srcid != NULL)) {
2313 		sq->sidstr = (char *)(sq->srcid + 1);
2314 		sq->sidtype = sq->srcid->sadb_ident_type;
2315 		*mfpp++ = sadb_match_srcid;
2316 	}
2317 
2318 	sq->kmcext = (sadb_x_kmc_t *)ksi->ks_in_extv[SADB_X_EXT_KM_COOKIE];
2319 	sq->kmc = 0;
2320 	sq->kmp = 0;
2321 
2322 	if ((match & IPSA_Q_KMC) && (sq->kmcext)) {
2323 		sq->kmp = sq->kmcext->sadb_x_kmc_proto;
2324 		/*
2325 		 * Be liberal in what we receive.  Special-case the IKEv1
2326 		 * cookie, which closed-source in.iked assumes is 32 bits.
2327 		 * Now that we store all 64 bits, we should pre-zero the
2328 		 * reserved field on behalf of closed-source in.iked.
2329 		 */
2330 		if (sq->kmp == SADB_X_KMP_IKE) {
2331 			/* Just in case in.iked is misbehaving... */
2332 			sq->kmcext->sadb_x_kmc_reserved = 0;
2333 		}
2334 		sq->kmc = sq->kmcext->sadb_x_kmc_cookie64;
2335 		*mfpp++ = sadb_match_kmc;
2336 	}
2337 
2338 	if (match & (IPSA_Q_INBOUND|IPSA_Q_OUTBOUND)) {
2339 		if (sq->af == AF_INET6)
2340 			sq->sp = &sq->spp->s_v6;
2341 		else
2342 			sq->sp = &sq->spp->s_v4;
2343 	} else {
2344 		sq->sp = NULL;
2345 	}
2346 
2347 	if (match & IPSA_Q_INBOUND) {
2348 		sq->inhash = INBOUND_HASH(sq->sp, sq->assoc->sadb_sa_spi);
2349 		sq->inbound = &sq->sp->sdb_if[sq->inhash];
2350 	} else {
2351 		sq->inhash = 0;
2352 		sq->inbound = NULL;
2353 	}
2354 
2355 	if (match & IPSA_Q_OUTBOUND) {
2356 		if (sq->af == AF_INET6) {
2357 			sq->outhash = OUTBOUND_HASH_V6(sq->sp, *(sq->dstaddr));
2358 		} else {
2359 			sq->outhash = OUTBOUND_HASH_V4(sq->sp, *(sq->dstaddr));
2360 		}
2361 		sq->outbound = &sq->sp->sdb_of[sq->outhash];
2362 	} else {
2363 		sq->outhash = 0;
2364 		sq->outbound = NULL;
2365 	}
2366 	sq->match = match;
2367 	return (0);
2368 }
2369 
2370 /*
2371  * Match an initialized query structure with a security association;
2372  * return B_TRUE on a match, B_FALSE on a miss.
2373  * Applies match functions set up by sadb_form_query() until one returns false.
2374  */
2375 boolean_t
2376 sadb_match_query(ipsa_query_t *sq, ipsa_t *sa)
2377 {
2378 	ipsa_match_fn_t *mfpp = &(sq->matchers[0]);
2379 	ipsa_match_fn_t mfp;
2380 
2381 	for (mfp = *mfpp++; mfp != NULL; mfp = *mfpp++) {
2382 		if (!mfp(sq, sa))
2383 			return (B_FALSE);
2384 	}
2385 	return (B_TRUE);
2386 }
2387 
2388 /*
2389  * Walker callback function to delete sa's based on src/dst address.
2390  * Assumes that we're called with *head locked, no other locks held;
2391  * Conveniently, and not coincidentally, this is both what sadb_walker
2392  * gives us and also what sadb_unlinkassoc expects.
2393  */
2394 struct sadb_purge_state
2395 {
2396 	ipsa_query_t sq;
2397 	boolean_t inbnd;
2398 	uint8_t sadb_sa_state;
2399 };
2400 
2401 static void
2402 sadb_purge_cb(isaf_t *head, ipsa_t *entry, void *cookie)
2403 {
2404 	struct sadb_purge_state *ps = (struct sadb_purge_state *)cookie;
2405 
2406 	ASSERT(MUTEX_HELD(&head->isaf_lock));
2407 
2408 	mutex_enter(&entry->ipsa_lock);
2409 
2410 	if (entry->ipsa_state == IPSA_STATE_LARVAL ||
2411 	    !sadb_match_query(&ps->sq, entry)) {
2412 		mutex_exit(&entry->ipsa_lock);
2413 		return;
2414 	}
2415 
2416 	if (ps->inbnd) {
2417 		sadb_delete_cluster(entry);
2418 	}
2419 	entry->ipsa_state = IPSA_STATE_DEAD;
2420 	(void) sadb_torch_assoc(head, entry);
2421 }
2422 
2423 /*
2424  * Common code to purge an SA with a matching src or dst address.
2425  * Don't kill larval SA's in such a purge.
2426  */
2427 int
2428 sadb_purge_sa(mblk_t *mp, keysock_in_t *ksi, sadb_t *sp,
2429     int *diagnostic, queue_t *pfkey_q)
2430 {
2431 	struct sadb_purge_state ps;
2432 	int error = sadb_form_query(ksi, 0,
2433 	    IPSA_Q_SRC|IPSA_Q_DST|IPSA_Q_SRCID|IPSA_Q_DSTID|IPSA_Q_KMC,
2434 	    &ps.sq, diagnostic);
2435 
2436 	if (error != 0)
2437 		return (error);
2438 
2439 	/*
2440 	 * This is simple, crude, and effective.
2441 	 * Unimplemented optimizations (TBD):
2442 	 * - we can limit how many places we search based on where we
2443 	 * think the SA is filed.
2444 	 * - if we get a dst address, we can hash based on dst addr to find
2445 	 * the correct bucket in the outbound table.
2446 	 */
2447 	ps.inbnd = B_TRUE;
2448 	sadb_walker(sp->sdb_if, sp->sdb_hashsize, sadb_purge_cb, &ps);
2449 	ps.inbnd = B_FALSE;
2450 	sadb_walker(sp->sdb_of, sp->sdb_hashsize, sadb_purge_cb, &ps);
2451 
2452 	ASSERT(mp->b_cont != NULL);
2453 	sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
2454 	    NULL);
2455 	return (0);
2456 }
2457 
2458 static void
2459 sadb_delpair_state_one(isaf_t *head, ipsa_t *entry, void *cookie)
2460 {
2461 	struct sadb_purge_state *ps = (struct sadb_purge_state *)cookie;
2462 	isaf_t  *inbound_bucket;
2463 	ipsa_t *peer_assoc;
2464 	ipsa_query_t *sq = &ps->sq;
2465 
2466 	ASSERT(MUTEX_HELD(&head->isaf_lock));
2467 
2468 	mutex_enter(&entry->ipsa_lock);
2469 
2470 	if ((entry->ipsa_state != ps->sadb_sa_state) ||
2471 	    ((sq->srcaddr != NULL) &&
2472 	    !IPSA_ARE_ADDR_EQUAL(entry->ipsa_srcaddr, sq->srcaddr, sq->af))) {
2473 		mutex_exit(&entry->ipsa_lock);
2474 		return;
2475 	}
2476 
2477 	/*
2478 	 * The isaf_t *, which is passed in , is always an outbound bucket,
2479 	 * and we are preserving the outbound-then-inbound hash-bucket lock
2480 	 * ordering. The sadb_walker() which triggers this function is called
2481 	 * only on the outbound fanout, and the corresponding inbound bucket
2482 	 * lock is safe to acquire here.
2483 	 */
2484 
2485 	if (entry->ipsa_haspeer) {
2486 		inbound_bucket = INBOUND_BUCKET(sq->sp, entry->ipsa_spi);
2487 		mutex_enter(&inbound_bucket->isaf_lock);
2488 		peer_assoc = ipsec_getassocbyspi(inbound_bucket,
2489 		    entry->ipsa_spi, entry->ipsa_srcaddr,
2490 		    entry->ipsa_dstaddr, entry->ipsa_addrfam);
2491 	} else {
2492 		inbound_bucket = INBOUND_BUCKET(sq->sp, entry->ipsa_otherspi);
2493 		mutex_enter(&inbound_bucket->isaf_lock);
2494 		peer_assoc = ipsec_getassocbyspi(inbound_bucket,
2495 		    entry->ipsa_otherspi, entry->ipsa_dstaddr,
2496 		    entry->ipsa_srcaddr, entry->ipsa_addrfam);
2497 	}
2498 
2499 	entry->ipsa_state = IPSA_STATE_DEAD;
2500 	(void) sadb_torch_assoc(head, entry);
2501 	if (peer_assoc != NULL) {
2502 		mutex_enter(&peer_assoc->ipsa_lock);
2503 		peer_assoc->ipsa_state = IPSA_STATE_DEAD;
2504 		(void) sadb_torch_assoc(inbound_bucket, peer_assoc);
2505 	}
2506 	mutex_exit(&inbound_bucket->isaf_lock);
2507 }
2508 
2509 static int
2510 sadb_delpair_state(mblk_t *mp, keysock_in_t *ksi, sadbp_t *spp,
2511     int *diagnostic, queue_t *pfkey_q)
2512 {
2513 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
2514 	struct sadb_purge_state ps;
2515 	int error;
2516 
2517 	ps.sq.spp = spp;		/* XXX param */
2518 
2519 	error = sadb_form_query(ksi, IPSA_Q_DST|IPSA_Q_SRC,
2520 	    IPSA_Q_SRC|IPSA_Q_DST|IPSA_Q_SRCID|IPSA_Q_DSTID|IPSA_Q_KMC,
2521 	    &ps.sq, diagnostic);
2522 	if (error != 0)
2523 		return (error);
2524 
2525 	ps.inbnd = B_FALSE;
2526 	ps.sadb_sa_state = assoc->sadb_sa_state;
2527 	sadb_walker(ps.sq.sp->sdb_of, ps.sq.sp->sdb_hashsize,
2528 	    sadb_delpair_state_one, &ps);
2529 
2530 	ASSERT(mp->b_cont != NULL);
2531 	sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr,
2532 	    ksi, NULL);
2533 	return (0);
2534 }
2535 
2536 /*
2537  * Common code to delete/get an SA.
2538  */
2539 int
2540 sadb_delget_sa(mblk_t *mp, keysock_in_t *ksi, sadbp_t *spp,
2541     int *diagnostic, queue_t *pfkey_q, uint8_t sadb_msg_type)
2542 {
2543 	ipsa_query_t sq;
2544 	ipsa_t *echo_target = NULL;
2545 	ipsap_t ipsapp;
2546 	uint_t	error = 0;
2547 
2548 	if (sadb_msg_type == SADB_X_DELPAIR_STATE)
2549 		return (sadb_delpair_state(mp, ksi, spp, diagnostic, pfkey_q));
2550 
2551 	sq.spp = spp;		/* XXX param */
2552 	error = sadb_form_query(ksi, IPSA_Q_DST|IPSA_Q_SA,
2553 	    IPSA_Q_SRC|IPSA_Q_DST|IPSA_Q_SA|IPSA_Q_INBOUND|IPSA_Q_OUTBOUND,
2554 	    &sq, diagnostic);
2555 	if (error != 0)
2556 		return (error);
2557 
2558 	error = get_ipsa_pair(&sq, &ipsapp, diagnostic);
2559 	if (error != 0) {
2560 		return (error);
2561 	}
2562 
2563 	echo_target = ipsapp.ipsap_sa_ptr;
2564 	if (echo_target == NULL)
2565 		echo_target = ipsapp.ipsap_psa_ptr;
2566 
2567 	if (sadb_msg_type == SADB_DELETE || sadb_msg_type == SADB_X_DELPAIR) {
2568 		/*
2569 		 * Bucket locks will be required if SA is actually unlinked.
2570 		 * get_ipsa_pair() returns valid hash bucket pointers even
2571 		 * if it can't find a pair SA pointer. To prevent a potential
2572 		 * deadlock, always lock the outbound bucket before the inbound.
2573 		 */
2574 		if (ipsapp.in_inbound_table) {
2575 			mutex_enter(&ipsapp.ipsap_pbucket->isaf_lock);
2576 			mutex_enter(&ipsapp.ipsap_bucket->isaf_lock);
2577 		} else {
2578 			mutex_enter(&ipsapp.ipsap_bucket->isaf_lock);
2579 			mutex_enter(&ipsapp.ipsap_pbucket->isaf_lock);
2580 		}
2581 
2582 		if (ipsapp.ipsap_sa_ptr != NULL) {
2583 			mutex_enter(&ipsapp.ipsap_sa_ptr->ipsa_lock);
2584 			if (ipsapp.ipsap_sa_ptr->ipsa_flags & IPSA_F_INBOUND) {
2585 				sadb_delete_cluster(ipsapp.ipsap_sa_ptr);
2586 			}
2587 			ipsapp.ipsap_sa_ptr->ipsa_state = IPSA_STATE_DEAD;
2588 			(void) sadb_torch_assoc(ipsapp.ipsap_bucket,
2589 			    ipsapp.ipsap_sa_ptr);
2590 			/*
2591 			 * sadb_torch_assoc() releases the ipsa_lock
2592 			 * and calls sadb_unlinkassoc() which does a
2593 			 * IPSA_REFRELE.
2594 			 */
2595 		}
2596 		if (ipsapp.ipsap_psa_ptr != NULL) {
2597 			mutex_enter(&ipsapp.ipsap_psa_ptr->ipsa_lock);
2598 			if (sadb_msg_type == SADB_X_DELPAIR ||
2599 			    ipsapp.ipsap_psa_ptr->ipsa_haspeer) {
2600 				if (ipsapp.ipsap_psa_ptr->ipsa_flags &
2601 				    IPSA_F_INBOUND) {
2602 					sadb_delete_cluster
2603 					    (ipsapp.ipsap_psa_ptr);
2604 				}
2605 				ipsapp.ipsap_psa_ptr->ipsa_state =
2606 				    IPSA_STATE_DEAD;
2607 				(void) sadb_torch_assoc(ipsapp.ipsap_pbucket,
2608 				    ipsapp.ipsap_psa_ptr);
2609 			} else {
2610 				/*
2611 				 * Only half of the "pair" has been deleted.
2612 				 * Update the remaining SA and remove references
2613 				 * to its pair SA, which is now gone.
2614 				 */
2615 				ipsapp.ipsap_psa_ptr->ipsa_otherspi = 0;
2616 				ipsapp.ipsap_psa_ptr->ipsa_flags &=
2617 				    ~IPSA_F_PAIRED;
2618 				mutex_exit(&ipsapp.ipsap_psa_ptr->ipsa_lock);
2619 			}
2620 		} else if (sadb_msg_type == SADB_X_DELPAIR) {
2621 			*diagnostic = SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND;
2622 			error = ESRCH;
2623 		}
2624 		mutex_exit(&ipsapp.ipsap_bucket->isaf_lock);
2625 		mutex_exit(&ipsapp.ipsap_pbucket->isaf_lock);
2626 	}
2627 
2628 	ASSERT(mp->b_cont != NULL);
2629 
2630 	if (error == 0)
2631 		sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)
2632 		    mp->b_cont->b_rptr, ksi, echo_target);
2633 
2634 	destroy_ipsa_pair(&ipsapp);
2635 
2636 	return (error);
2637 }
2638 
2639 /*
2640  * This function takes a sadb_sa_t and finds the ipsa_t structure
2641  * and the isaf_t (hash bucket) that its stored under. If the security
2642  * association has a peer, the ipsa_t structure and bucket for that security
2643  * association are also searched for. The "pair" of ipsa_t's and isaf_t's
2644  * are returned as a ipsap_t.
2645  *
2646  * The hash buckets are returned for convenience, if the calling function
2647  * needs to use the hash bucket locks, say to remove the SA's, it should
2648  * take care to observe the convention of locking outbound bucket then
2649  * inbound bucket. The flag in_inbound_table provides direction.
2650  *
2651  * Note that a "pair" is defined as one (but not both) of the following:
2652  *
2653  * A security association which has a soft reference to another security
2654  * association via its SPI.
2655  *
2656  * A security association that is not obviously "inbound" or "outbound" so
2657  * it appears in both hash tables, the "peer" being the same security
2658  * association in the other hash table.
2659  *
2660  * This function will return NULL if the ipsa_t can't be found in the
2661  * inbound or outbound  hash tables (not found). If only one ipsa_t is
2662  * found, the pair ipsa_t will be NULL. Both isaf_t values are valid
2663  * provided at least one ipsa_t is found.
2664  */
2665 static int
2666 get_ipsa_pair(ipsa_query_t *sq, ipsap_t *ipsapp, int *diagnostic)
2667 {
2668 	uint32_t pair_srcaddr[IPSA_MAX_ADDRLEN];
2669 	uint32_t pair_dstaddr[IPSA_MAX_ADDRLEN];
2670 	uint32_t pair_spi;
2671 
2672 	init_ipsa_pair(ipsapp);
2673 
2674 	ipsapp->in_inbound_table = B_FALSE;
2675 
2676 	/* Lock down both buckets. */
2677 	mutex_enter(&sq->outbound->isaf_lock);
2678 	mutex_enter(&sq->inbound->isaf_lock);
2679 
2680 	if (sq->assoc->sadb_sa_flags & IPSA_F_INBOUND) {
2681 		ipsapp->ipsap_sa_ptr = ipsec_getassocbyspi(sq->inbound,
2682 		    sq->assoc->sadb_sa_spi, sq->srcaddr, sq->dstaddr, sq->af);
2683 		if (ipsapp->ipsap_sa_ptr != NULL) {
2684 			ipsapp->ipsap_bucket = sq->inbound;
2685 			ipsapp->ipsap_pbucket = sq->outbound;
2686 			ipsapp->in_inbound_table = B_TRUE;
2687 		} else {
2688 			ipsapp->ipsap_sa_ptr = ipsec_getassocbyspi(sq->outbound,
2689 			    sq->assoc->sadb_sa_spi, sq->srcaddr, sq->dstaddr,
2690 			    sq->af);
2691 			ipsapp->ipsap_bucket = sq->outbound;
2692 			ipsapp->ipsap_pbucket = sq->inbound;
2693 		}
2694 	} else {
2695 		/* IPSA_F_OUTBOUND is set *or* no directions flags set. */
2696 		ipsapp->ipsap_sa_ptr =
2697 		    ipsec_getassocbyspi(sq->outbound,
2698 		    sq->assoc->sadb_sa_spi, sq->srcaddr, sq->dstaddr, sq->af);
2699 		if (ipsapp->ipsap_sa_ptr != NULL) {
2700 			ipsapp->ipsap_bucket = sq->outbound;
2701 			ipsapp->ipsap_pbucket = sq->inbound;
2702 		} else {
2703 			ipsapp->ipsap_sa_ptr = ipsec_getassocbyspi(sq->inbound,
2704 			    sq->assoc->sadb_sa_spi, sq->srcaddr, sq->dstaddr,
2705 			    sq->af);
2706 			ipsapp->ipsap_bucket = sq->inbound;
2707 			ipsapp->ipsap_pbucket = sq->outbound;
2708 			if (ipsapp->ipsap_sa_ptr != NULL)
2709 				ipsapp->in_inbound_table = B_TRUE;
2710 		}
2711 	}
2712 
2713 	if (ipsapp->ipsap_sa_ptr == NULL) {
2714 		mutex_exit(&sq->outbound->isaf_lock);
2715 		mutex_exit(&sq->inbound->isaf_lock);
2716 		*diagnostic = SADB_X_DIAGNOSTIC_SA_NOTFOUND;
2717 		return (ESRCH);
2718 	}
2719 
2720 	if ((ipsapp->ipsap_sa_ptr->ipsa_state == IPSA_STATE_LARVAL) &&
2721 	    ipsapp->in_inbound_table) {
2722 		mutex_exit(&sq->outbound->isaf_lock);
2723 		mutex_exit(&sq->inbound->isaf_lock);
2724 		return (0);
2725 	}
2726 
2727 	mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock);
2728 	if (ipsapp->ipsap_sa_ptr->ipsa_haspeer) {
2729 		/*
2730 		 * haspeer implies no sa_pairing, look for same spi
2731 		 * in other hashtable.
2732 		 */
2733 		ipsapp->ipsap_psa_ptr =
2734 		    ipsec_getassocbyspi(ipsapp->ipsap_pbucket,
2735 		    sq->assoc->sadb_sa_spi, sq->srcaddr, sq->dstaddr, sq->af);
2736 		mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock);
2737 		mutex_exit(&sq->outbound->isaf_lock);
2738 		mutex_exit(&sq->inbound->isaf_lock);
2739 		return (0);
2740 	}
2741 	pair_spi = ipsapp->ipsap_sa_ptr->ipsa_otherspi;
2742 	IPSA_COPY_ADDR(&pair_srcaddr,
2743 	    ipsapp->ipsap_sa_ptr->ipsa_srcaddr, sq->af);
2744 	IPSA_COPY_ADDR(&pair_dstaddr,
2745 	    ipsapp->ipsap_sa_ptr->ipsa_dstaddr, sq->af);
2746 	mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock);
2747 	mutex_exit(&sq->inbound->isaf_lock);
2748 	mutex_exit(&sq->outbound->isaf_lock);
2749 
2750 	if (pair_spi == 0) {
2751 		ASSERT(ipsapp->ipsap_bucket != NULL);
2752 		ASSERT(ipsapp->ipsap_pbucket != NULL);
2753 		return (0);
2754 	}
2755 
2756 	/* found sa in outbound sadb, peer should be inbound */
2757 
2758 	if (ipsapp->in_inbound_table) {
2759 		/* Found SA in inbound table, pair will be in outbound. */
2760 		if (sq->af == AF_INET6) {
2761 			ipsapp->ipsap_pbucket = OUTBOUND_BUCKET_V6(sq->sp,
2762 			    *(uint32_t *)pair_srcaddr);
2763 		} else {
2764 			ipsapp->ipsap_pbucket = OUTBOUND_BUCKET_V4(sq->sp,
2765 			    *(uint32_t *)pair_srcaddr);
2766 		}
2767 	} else {
2768 		ipsapp->ipsap_pbucket = INBOUND_BUCKET(sq->sp, pair_spi);
2769 	}
2770 	mutex_enter(&ipsapp->ipsap_pbucket->isaf_lock);
2771 	ipsapp->ipsap_psa_ptr = ipsec_getassocbyspi(ipsapp->ipsap_pbucket,
2772 	    pair_spi, pair_dstaddr, pair_srcaddr, sq->af);
2773 	mutex_exit(&ipsapp->ipsap_pbucket->isaf_lock);
2774 	ASSERT(ipsapp->ipsap_bucket != NULL);
2775 	ASSERT(ipsapp->ipsap_pbucket != NULL);
2776 	return (0);
2777 }
2778 
2779 /*
2780  * Perform NAT-traversal cached checksum offset calculations here.
2781  */
2782 static void
2783 sadb_nat_calculations(ipsa_t *newbie, sadb_address_t *natt_loc_ext,
2784     sadb_address_t *natt_rem_ext, uint32_t *src_addr_ptr,
2785     uint32_t *dst_addr_ptr)
2786 {
2787 	struct sockaddr_in *natt_loc, *natt_rem;
2788 	uint32_t *natt_loc_ptr = NULL, *natt_rem_ptr = NULL;
2789 	uint32_t running_sum = 0;
2790 
2791 #define	DOWN_SUM(x) (x) = ((x) & 0xFFFF) +	 ((x) >> 16)
2792 
2793 	if (natt_rem_ext != NULL) {
2794 		uint32_t l_src;
2795 		uint32_t l_rem;
2796 
2797 		natt_rem = (struct sockaddr_in *)(natt_rem_ext + 1);
2798 
2799 		/* Ensured by sadb_addrfix(). */
2800 		ASSERT(natt_rem->sin_family == AF_INET);
2801 
2802 		natt_rem_ptr = (uint32_t *)(&natt_rem->sin_addr);
2803 		newbie->ipsa_remote_nat_port = natt_rem->sin_port;
2804 		l_src = *src_addr_ptr;
2805 		l_rem = *natt_rem_ptr;
2806 
2807 		/* Instead of IPSA_COPY_ADDR(), just copy first 32 bits. */
2808 		newbie->ipsa_natt_addr_rem = *natt_rem_ptr;
2809 
2810 		l_src = ntohl(l_src);
2811 		DOWN_SUM(l_src);
2812 		DOWN_SUM(l_src);
2813 		l_rem = ntohl(l_rem);
2814 		DOWN_SUM(l_rem);
2815 		DOWN_SUM(l_rem);
2816 
2817 		/*
2818 		 * We're 1's complement for checksums, so check for wraparound
2819 		 * here.
2820 		 */
2821 		if (l_rem > l_src)
2822 			l_src--;
2823 
2824 		running_sum += l_src - l_rem;
2825 
2826 		DOWN_SUM(running_sum);
2827 		DOWN_SUM(running_sum);
2828 	}
2829 
2830 	if (natt_loc_ext != NULL) {
2831 		natt_loc = (struct sockaddr_in *)(natt_loc_ext + 1);
2832 
2833 		/* Ensured by sadb_addrfix(). */
2834 		ASSERT(natt_loc->sin_family == AF_INET);
2835 
2836 		natt_loc_ptr = (uint32_t *)(&natt_loc->sin_addr);
2837 		newbie->ipsa_local_nat_port = natt_loc->sin_port;
2838 
2839 		/* Instead of IPSA_COPY_ADDR(), just copy first 32 bits. */
2840 		newbie->ipsa_natt_addr_loc = *natt_loc_ptr;
2841 
2842 		/*
2843 		 * NAT-T port agility means we may have natt_loc_ext, but
2844 		 * only for a local-port change.
2845 		 */
2846 		if (natt_loc->sin_addr.s_addr != INADDR_ANY) {
2847 			uint32_t l_dst = ntohl(*dst_addr_ptr);
2848 			uint32_t l_loc = ntohl(*natt_loc_ptr);
2849 
2850 			DOWN_SUM(l_loc);
2851 			DOWN_SUM(l_loc);
2852 			DOWN_SUM(l_dst);
2853 			DOWN_SUM(l_dst);
2854 
2855 			/*
2856 			 * We're 1's complement for checksums, so check for
2857 			 * wraparound here.
2858 			 */
2859 			if (l_loc > l_dst)
2860 				l_dst--;
2861 
2862 			running_sum += l_dst - l_loc;
2863 			DOWN_SUM(running_sum);
2864 			DOWN_SUM(running_sum);
2865 		}
2866 	}
2867 
2868 	newbie->ipsa_inbound_cksum = running_sum;
2869 #undef DOWN_SUM
2870 }
2871 
2872 /*
2873  * This function is called from consumers that need to insert a fully-grown
2874  * security association into its tables.  This function takes into account that
2875  * SAs can be "inbound", "outbound", or "both".	 The "primary" and "secondary"
2876  * hash bucket parameters are set in order of what the SA will be most of the
2877  * time.  (For example, an SA with an unspecified source, and a multicast
2878  * destination will primarily be an outbound SA.  OTOH, if that destination
2879  * is unicast for this node, then the SA will primarily be inbound.)
2880  *
2881  * It takes a lot of parameters because even if clone is B_FALSE, this needs
2882  * to check both buckets for purposes of collision.
2883  *
2884  * Return 0 upon success.  Return various errnos (ENOMEM, EEXIST) for
2885  * various error conditions.  We may need to set samsg->sadb_x_msg_diagnostic
2886  * with additional diagnostic information because there is at least one EINVAL
2887  * case here.
2888  */
2889 int
2890 sadb_common_add(queue_t *pfkey_q, mblk_t *mp, sadb_msg_t *samsg,
2891     keysock_in_t *ksi, isaf_t *primary, isaf_t *secondary,
2892     ipsa_t *newbie, boolean_t clone, boolean_t is_inbound, int *diagnostic,
2893     netstack_t *ns, sadbp_t *spp)
2894 {
2895 	ipsa_t *newbie_clone = NULL, *scratch;
2896 	ipsap_t ipsapp;
2897 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
2898 	sadb_address_t *srcext =
2899 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
2900 	sadb_address_t *dstext =
2901 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
2902 	sadb_address_t *isrcext =
2903 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
2904 	sadb_address_t *idstext =
2905 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
2906 	sadb_x_kmc_t *kmcext =
2907 	    (sadb_x_kmc_t *)ksi->ks_in_extv[SADB_X_EXT_KM_COOKIE];
2908 	sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
2909 	sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT];
2910 	sadb_sens_t *sens =
2911 	    (sadb_sens_t *)ksi->ks_in_extv[SADB_EXT_SENSITIVITY];
2912 	sadb_sens_t *osens =
2913 	    (sadb_sens_t *)ksi->ks_in_extv[SADB_X_EXT_OUTER_SENS];
2914 	sadb_x_pair_t *pair_ext =
2915 	    (sadb_x_pair_t *)ksi->ks_in_extv[SADB_X_EXT_PAIR];
2916 	sadb_x_replay_ctr_t *replayext =
2917 	    (sadb_x_replay_ctr_t *)ksi->ks_in_extv[SADB_X_EXT_REPLAY_VALUE];
2918 	uint8_t protocol =
2919 	    (samsg->sadb_msg_satype == SADB_SATYPE_AH) ? IPPROTO_AH:IPPROTO_ESP;
2920 	int salt_offset;
2921 	uint8_t *buf_ptr;
2922 	struct sockaddr_in *src, *dst, *isrc, *idst;
2923 	struct sockaddr_in6 *src6, *dst6, *isrc6, *idst6;
2924 	sadb_lifetime_t *soft =
2925 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
2926 	sadb_lifetime_t *hard =
2927 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
2928 	sadb_lifetime_t	*idle =
2929 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_X_EXT_LIFETIME_IDLE];
2930 	sa_family_t af;
2931 	int error = 0;
2932 	boolean_t isupdate = (newbie != NULL);
2933 	uint32_t *src_addr_ptr, *dst_addr_ptr, *isrc_addr_ptr, *idst_addr_ptr;
2934 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2935 	ip_stack_t	*ipst = ns->netstack_ip;
2936 	ipsec_alginfo_t *alg;
2937 	int		rcode;
2938 	boolean_t	async = B_FALSE;
2939 
2940 	init_ipsa_pair(&ipsapp);
2941 
2942 	if (srcext == NULL) {
2943 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
2944 		return (EINVAL);
2945 	}
2946 	if (dstext == NULL) {
2947 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
2948 		return (EINVAL);
2949 	}
2950 	if (assoc == NULL) {
2951 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
2952 		return (EINVAL);
2953 	}
2954 
2955 	src = (struct sockaddr_in *)(srcext + 1);
2956 	src6 = (struct sockaddr_in6 *)(srcext + 1);
2957 	dst = (struct sockaddr_in *)(dstext + 1);
2958 	dst6 = (struct sockaddr_in6 *)(dstext + 1);
2959 	if (isrcext != NULL) {
2960 		isrc = (struct sockaddr_in *)(isrcext + 1);
2961 		isrc6 = (struct sockaddr_in6 *)(isrcext + 1);
2962 		ASSERT(idstext != NULL);
2963 		idst = (struct sockaddr_in *)(idstext + 1);
2964 		idst6 = (struct sockaddr_in6 *)(idstext + 1);
2965 	} else {
2966 		isrc = NULL;
2967 		isrc6 = NULL;
2968 	}
2969 
2970 	af = src->sin_family;
2971 
2972 	if (af == AF_INET) {
2973 		src_addr_ptr = (uint32_t *)&src->sin_addr;
2974 		dst_addr_ptr = (uint32_t *)&dst->sin_addr;
2975 	} else {
2976 		ASSERT(af == AF_INET6);
2977 		src_addr_ptr = (uint32_t *)&src6->sin6_addr;
2978 		dst_addr_ptr = (uint32_t *)&dst6->sin6_addr;
2979 	}
2980 
2981 	if (!isupdate && (clone == B_TRUE || is_inbound == B_TRUE) &&
2982 	    cl_inet_checkspi &&
2983 	    (assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE_ELSEWHERE)) {
2984 		rcode = cl_inet_checkspi(ns->netstack_stackid, protocol,
2985 		    assoc->sadb_sa_spi, NULL);
2986 		if (rcode == -1) {
2987 			return (EEXIST);
2988 		}
2989 	}
2990 
2991 	/*
2992 	 * Check to see if the new SA will be cloned AND paired. The
2993 	 * reason a SA will be cloned is the source or destination addresses
2994 	 * are not specific enough to determine if the SA goes in the outbound
2995 	 * or the inbound hash table, so its cloned and put in both. If
2996 	 * the SA is paired, it's soft linked to another SA for the other
2997 	 * direction. Keeping track and looking up SA's that are direction
2998 	 * unspecific and linked is too hard.
2999 	 */
3000 	if (clone && (pair_ext != NULL)) {
3001 		*diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE;
3002 		return (EINVAL);
3003 	}
3004 
3005 	if (!isupdate) {
3006 		newbie = sadb_makelarvalassoc(assoc->sadb_sa_spi,
3007 		    src_addr_ptr, dst_addr_ptr, af, ns);
3008 		if (newbie == NULL)
3009 			return (ENOMEM);
3010 	}
3011 
3012 	mutex_enter(&newbie->ipsa_lock);
3013 
3014 	if (isrc != NULL) {
3015 		if (isrc->sin_family == AF_INET) {
3016 			if (srcext->sadb_address_proto != IPPROTO_ENCAP) {
3017 				if (srcext->sadb_address_proto != 0) {
3018 					/*
3019 					 * Mismatched outer-packet protocol
3020 					 * and inner-packet address family.
3021 					 */
3022 					mutex_exit(&newbie->ipsa_lock);
3023 					error = EPROTOTYPE;
3024 					*diagnostic =
3025 					    SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH;
3026 					goto error;
3027 				} else {
3028 					/* Fill in with explicit protocol. */
3029 					srcext->sadb_address_proto =
3030 					    IPPROTO_ENCAP;
3031 					dstext->sadb_address_proto =
3032 					    IPPROTO_ENCAP;
3033 				}
3034 			}
3035 			isrc_addr_ptr = (uint32_t *)&isrc->sin_addr;
3036 			idst_addr_ptr = (uint32_t *)&idst->sin_addr;
3037 		} else {
3038 			ASSERT(isrc->sin_family == AF_INET6);
3039 			if (srcext->sadb_address_proto != IPPROTO_IPV6) {
3040 				if (srcext->sadb_address_proto != 0) {
3041 					/*
3042 					 * Mismatched outer-packet protocol
3043 					 * and inner-packet address family.
3044 					 */
3045 					mutex_exit(&newbie->ipsa_lock);
3046 					error = EPROTOTYPE;
3047 					*diagnostic =
3048 					    SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH;
3049 					goto error;
3050 				} else {
3051 					/* Fill in with explicit protocol. */
3052 					srcext->sadb_address_proto =
3053 					    IPPROTO_IPV6;
3054 					dstext->sadb_address_proto =
3055 					    IPPROTO_IPV6;
3056 				}
3057 			}
3058 			isrc_addr_ptr = (uint32_t *)&isrc6->sin6_addr;
3059 			idst_addr_ptr = (uint32_t *)&idst6->sin6_addr;
3060 		}
3061 		newbie->ipsa_innerfam = isrc->sin_family;
3062 
3063 		IPSA_COPY_ADDR(newbie->ipsa_innersrc, isrc_addr_ptr,
3064 		    newbie->ipsa_innerfam);
3065 		IPSA_COPY_ADDR(newbie->ipsa_innerdst, idst_addr_ptr,
3066 		    newbie->ipsa_innerfam);
3067 		newbie->ipsa_innersrcpfx = isrcext->sadb_address_prefixlen;
3068 		newbie->ipsa_innerdstpfx = idstext->sadb_address_prefixlen;
3069 
3070 		/* Unique value uses inner-ports for Tunnel Mode... */
3071 		newbie->ipsa_unique_id = SA_UNIQUE_ID(isrc->sin_port,
3072 		    idst->sin_port, dstext->sadb_address_proto,
3073 		    idstext->sadb_address_proto);
3074 		newbie->ipsa_unique_mask = SA_UNIQUE_MASK(isrc->sin_port,
3075 		    idst->sin_port, dstext->sadb_address_proto,
3076 		    idstext->sadb_address_proto);
3077 	} else {
3078 		/* ... and outer-ports for Transport Mode. */
3079 		newbie->ipsa_unique_id = SA_UNIQUE_ID(src->sin_port,
3080 		    dst->sin_port, dstext->sadb_address_proto, 0);
3081 		newbie->ipsa_unique_mask = SA_UNIQUE_MASK(src->sin_port,
3082 		    dst->sin_port, dstext->sadb_address_proto, 0);
3083 	}
3084 	if (newbie->ipsa_unique_mask != (uint64_t)0)
3085 		newbie->ipsa_flags |= IPSA_F_UNIQUE;
3086 
3087 	sadb_nat_calculations(newbie,
3088 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC],
3089 	    (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM],
3090 	    src_addr_ptr, dst_addr_ptr);
3091 
3092 	newbie->ipsa_type = samsg->sadb_msg_satype;
3093 
3094 	ASSERT((assoc->sadb_sa_state == SADB_SASTATE_MATURE) ||
3095 	    (assoc->sadb_sa_state == SADB_X_SASTATE_ACTIVE_ELSEWHERE));
3096 	newbie->ipsa_auth_alg = assoc->sadb_sa_auth;
3097 	newbie->ipsa_encr_alg = assoc->sadb_sa_encrypt;
3098 
3099 	newbie->ipsa_flags |= assoc->sadb_sa_flags;
3100 	if (newbie->ipsa_flags & SADB_X_SAFLAGS_NATT_LOC &&
3101 	    ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC] == NULL) {
3102 		mutex_exit(&newbie->ipsa_lock);
3103 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_LOC;
3104 		error = EINVAL;
3105 		goto error;
3106 	}
3107 	if (newbie->ipsa_flags & SADB_X_SAFLAGS_NATT_REM &&
3108 	    ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM] == NULL) {
3109 		mutex_exit(&newbie->ipsa_lock);
3110 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_REM;
3111 		error = EINVAL;
3112 		goto error;
3113 	}
3114 	if (newbie->ipsa_flags & SADB_X_SAFLAGS_TUNNEL &&
3115 	    ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC] == NULL) {
3116 		mutex_exit(&newbie->ipsa_lock);
3117 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
3118 		error = EINVAL;
3119 		goto error;
3120 	}
3121 	/*
3122 	 * If unspecified source address, force replay_wsize to 0.
3123 	 * This is because an SA that has multiple sources of secure
3124 	 * traffic cannot enforce a replay counter w/o synchronizing the
3125 	 * senders.
3126 	 */
3127 	if (ksi->ks_in_srctype != KS_IN_ADDR_UNSPEC)
3128 		newbie->ipsa_replay_wsize = assoc->sadb_sa_replay;
3129 	else
3130 		newbie->ipsa_replay_wsize = 0;
3131 
3132 	newbie->ipsa_addtime = gethrestime_sec();
3133 
3134 	if (kmcext != NULL) {
3135 		newbie->ipsa_kmp = kmcext->sadb_x_kmc_proto;
3136 		/*
3137 		 * Be liberal in what we receive.  Special-case the IKEv1
3138 		 * cookie, which closed-source in.iked assumes is 32 bits.
3139 		 * Now that we store all 64 bits, we should pre-zero the
3140 		 * reserved field on behalf of closed-source in.iked.
3141 		 */
3142 		if (newbie->ipsa_kmp == SADB_X_KMP_IKE) {
3143 			/* Just in case in.iked is misbehaving... */
3144 			kmcext->sadb_x_kmc_reserved = 0;
3145 		}
3146 		newbie->ipsa_kmc = kmcext->sadb_x_kmc_cookie64;
3147 	}
3148 
3149 	/*
3150 	 * XXX CURRENT lifetime checks MAY BE needed for an UPDATE.
3151 	 * The spec says that one can update current lifetimes, but
3152 	 * that seems impractical, especially in the larval-to-mature
3153 	 * update that this function performs.
3154 	 */
3155 	if (soft != NULL) {
3156 		newbie->ipsa_softaddlt = soft->sadb_lifetime_addtime;
3157 		newbie->ipsa_softuselt = soft->sadb_lifetime_usetime;
3158 		newbie->ipsa_softbyteslt = soft->sadb_lifetime_bytes;
3159 		newbie->ipsa_softalloc = soft->sadb_lifetime_allocations;
3160 		SET_EXPIRE(newbie, softaddlt, softexpiretime);
3161 	}
3162 	if (hard != NULL) {
3163 		newbie->ipsa_hardaddlt = hard->sadb_lifetime_addtime;
3164 		newbie->ipsa_harduselt = hard->sadb_lifetime_usetime;
3165 		newbie->ipsa_hardbyteslt = hard->sadb_lifetime_bytes;
3166 		newbie->ipsa_hardalloc = hard->sadb_lifetime_allocations;
3167 		SET_EXPIRE(newbie, hardaddlt, hardexpiretime);
3168 	}
3169 	if (idle != NULL) {
3170 		newbie->ipsa_idleaddlt = idle->sadb_lifetime_addtime;
3171 		newbie->ipsa_idleuselt = idle->sadb_lifetime_usetime;
3172 		newbie->ipsa_idleexpiretime = newbie->ipsa_addtime +
3173 		    newbie->ipsa_idleaddlt;
3174 		newbie->ipsa_idletime = newbie->ipsa_idleaddlt;
3175 	}
3176 
3177 	newbie->ipsa_authtmpl = NULL;
3178 	newbie->ipsa_encrtmpl = NULL;
3179 
3180 #ifdef IPSEC_LATENCY_TEST
3181 	if (akey != NULL && newbie->ipsa_auth_alg != SADB_AALG_NONE) {
3182 #else
3183 	if (akey != NULL) {
3184 #endif
3185 		async = (ipss->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
3186 		    IPSEC_ALGS_EXEC_ASYNC);
3187 
3188 		newbie->ipsa_authkeybits = akey->sadb_key_bits;
3189 		newbie->ipsa_authkeylen = SADB_1TO8(akey->sadb_key_bits);
3190 		/* In case we have to round up to the next byte... */
3191 		if ((akey->sadb_key_bits & 0x7) != 0)
3192 			newbie->ipsa_authkeylen++;
3193 		newbie->ipsa_authkey = kmem_alloc(newbie->ipsa_authkeylen,
3194 		    KM_NOSLEEP);
3195 		if (newbie->ipsa_authkey == NULL) {
3196 			error = ENOMEM;
3197 			mutex_exit(&newbie->ipsa_lock);
3198 			goto error;
3199 		}
3200 		bcopy(akey + 1, newbie->ipsa_authkey, newbie->ipsa_authkeylen);
3201 		bzero(akey + 1, newbie->ipsa_authkeylen);
3202 
3203 		/*
3204 		 * Pre-initialize the kernel crypto framework key
3205 		 * structure.
3206 		 */
3207 		newbie->ipsa_kcfauthkey.ck_format = CRYPTO_KEY_RAW;
3208 		newbie->ipsa_kcfauthkey.ck_length = newbie->ipsa_authkeybits;
3209 		newbie->ipsa_kcfauthkey.ck_data = newbie->ipsa_authkey;
3210 
3211 		rw_enter(&ipss->ipsec_alg_lock, RW_READER);
3212 		alg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
3213 		    [newbie->ipsa_auth_alg];
3214 		if (alg != NULL && ALG_VALID(alg)) {
3215 			newbie->ipsa_amech.cm_type = alg->alg_mech_type;
3216 			newbie->ipsa_amech.cm_param =
3217 			    (char *)&newbie->ipsa_mac_len;
3218 			newbie->ipsa_amech.cm_param_len = sizeof (size_t);
3219 			newbie->ipsa_mac_len = (size_t)alg->alg_datalen;
3220 		} else {
3221 			newbie->ipsa_amech.cm_type = CRYPTO_MECHANISM_INVALID;
3222 		}
3223 		error = ipsec_create_ctx_tmpl(newbie, IPSEC_ALG_AUTH);
3224 		rw_exit(&ipss->ipsec_alg_lock);
3225 		if (error != 0) {
3226 			mutex_exit(&newbie->ipsa_lock);
3227 			/*
3228 			 * An error here indicates that alg is the wrong type
3229 			 * (IE: not authentication) or its not in the alg tables
3230 			 * created by ipsecalgs(1m), or Kcf does not like the
3231 			 * parameters passed in with this algorithm, which is
3232 			 * probably a coding error!
3233 			 */
3234 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_CTX;
3235 
3236 			goto error;
3237 		}
3238 	}
3239 
3240 	if (ekey != NULL) {
3241 		rw_enter(&ipss->ipsec_alg_lock, RW_READER);
3242 		async = async || (ipss->ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
3243 		    IPSEC_ALGS_EXEC_ASYNC);
3244 		alg = ipss->ipsec_alglists[IPSEC_ALG_ENCR]
3245 		    [newbie->ipsa_encr_alg];
3246 
3247 		if (alg != NULL && ALG_VALID(alg)) {
3248 			newbie->ipsa_emech.cm_type = alg->alg_mech_type;
3249 			newbie->ipsa_datalen = alg->alg_datalen;
3250 			if (alg->alg_flags & ALG_FLAG_COUNTERMODE)
3251 				newbie->ipsa_flags |= IPSA_F_COUNTERMODE;
3252 
3253 			if (alg->alg_flags & ALG_FLAG_COMBINED) {
3254 				newbie->ipsa_flags |= IPSA_F_COMBINED;
3255 				newbie->ipsa_mac_len =  alg->alg_icvlen;
3256 			}
3257 
3258 			if (alg->alg_flags & ALG_FLAG_CCM)
3259 				newbie->ipsa_noncefunc = ccm_params_init;
3260 			else if (alg->alg_flags & ALG_FLAG_GCM)
3261 				newbie->ipsa_noncefunc = gcm_params_init;
3262 			else newbie->ipsa_noncefunc = cbc_params_init;
3263 
3264 			newbie->ipsa_saltlen = alg->alg_saltlen;
3265 			newbie->ipsa_saltbits = SADB_8TO1(newbie->ipsa_saltlen);
3266 			newbie->ipsa_iv_len = alg->alg_ivlen;
3267 			newbie->ipsa_nonce_len = newbie->ipsa_saltlen +
3268 			    newbie->ipsa_iv_len;
3269 			newbie->ipsa_emech.cm_param = NULL;
3270 			newbie->ipsa_emech.cm_param_len = 0;
3271 		} else {
3272 			newbie->ipsa_emech.cm_type = CRYPTO_MECHANISM_INVALID;
3273 		}
3274 		rw_exit(&ipss->ipsec_alg_lock);
3275 
3276 		/*
3277 		 * The byte stream following the sadb_key_t is made up of:
3278 		 * key bytes, [salt bytes], [IV initial value]
3279 		 * All of these have variable length. The IV is typically
3280 		 * randomly generated by this function and not passed in.
3281 		 * By supporting the injection of a known IV, the whole
3282 		 * IPsec subsystem and the underlying crypto subsystem
3283 		 * can be tested with known test vectors.
3284 		 *
3285 		 * The keying material has been checked by ext_check()
3286 		 * and ipsec_valid_key_size(), after removing salt/IV
3287 		 * bits, whats left is the encryption key. If this is too
3288 		 * short, ipsec_create_ctx_tmpl() will fail and the SA
3289 		 * won't get created.
3290 		 *
3291 		 * set ipsa_encrkeylen to length of key only.
3292 		 */
3293 		newbie->ipsa_encrkeybits = ekey->sadb_key_bits;
3294 		newbie->ipsa_encrkeybits -= ekey->sadb_key_reserved;
3295 		newbie->ipsa_encrkeybits -= newbie->ipsa_saltbits;
3296 		newbie->ipsa_encrkeylen = SADB_1TO8(newbie->ipsa_encrkeybits);
3297 
3298 		/* In case we have to round up to the next byte... */
3299 		if ((ekey->sadb_key_bits & 0x7) != 0)
3300 			newbie->ipsa_encrkeylen++;
3301 
3302 		newbie->ipsa_encrkey = kmem_alloc(newbie->ipsa_encrkeylen,
3303 		    KM_NOSLEEP);
3304 		if (newbie->ipsa_encrkey == NULL) {
3305 			error = ENOMEM;
3306 			mutex_exit(&newbie->ipsa_lock);
3307 			goto error;
3308 		}
3309 
3310 		buf_ptr = (uint8_t *)(ekey + 1);
3311 		bcopy(buf_ptr, newbie->ipsa_encrkey, newbie->ipsa_encrkeylen);
3312 
3313 		if (newbie->ipsa_flags & IPSA_F_COMBINED) {
3314 			/*
3315 			 * Combined mode algs need a nonce. Copy the salt and
3316 			 * IV into a buffer. The ipsa_nonce is a pointer into
3317 			 * this buffer, some bytes at the start of the buffer
3318 			 * may be unused, depends on the salt length. The IV
3319 			 * is 64 bit aligned so it can be incremented as a
3320 			 * uint64_t. Zero out key in samsg_t before freeing.
3321 			 */
3322 
3323 			newbie->ipsa_nonce_buf = kmem_alloc(
3324 			    sizeof (ipsec_nonce_t), KM_NOSLEEP);
3325 			if (newbie->ipsa_nonce_buf == NULL) {
3326 				error = ENOMEM;
3327 				mutex_exit(&newbie->ipsa_lock);
3328 				goto error;
3329 			}
3330 			/*
3331 			 * Initialize nonce and salt pointers to point
3332 			 * to the nonce buffer. This is just in case we get
3333 			 * bad data, the pointers will be valid, the data
3334 			 * won't be.
3335 			 *
3336 			 * See sadb.h for layout of nonce.
3337 			 */
3338 			newbie->ipsa_iv = &newbie->ipsa_nonce_buf->iv;
3339 			newbie->ipsa_salt = (uint8_t *)newbie->ipsa_nonce_buf;
3340 			newbie->ipsa_nonce = newbie->ipsa_salt;
3341 			if (newbie->ipsa_saltlen != 0) {
3342 				salt_offset = MAXSALTSIZE -
3343 				    newbie->ipsa_saltlen;
3344 				newbie->ipsa_salt = (uint8_t *)
3345 				    &newbie->ipsa_nonce_buf->salt[salt_offset];
3346 				newbie->ipsa_nonce = newbie->ipsa_salt;
3347 				buf_ptr += newbie->ipsa_encrkeylen;
3348 				bcopy(buf_ptr, newbie->ipsa_salt,
3349 				    newbie->ipsa_saltlen);
3350 			}
3351 			/*
3352 			 * The IV for CCM/GCM mode increments, it should not
3353 			 * repeat. Get a random value for the IV, make a
3354 			 * copy, the SA will expire when/if the IV ever
3355 			 * wraps back to the initial value. If an Initial IV
3356 			 * is passed in via PF_KEY, save this in the SA.
3357 			 * Initialising IV for inbound is pointless as its
3358 			 * taken from the inbound packet.
3359 			 */
3360 			if (!is_inbound) {
3361 				if (ekey->sadb_key_reserved != 0) {
3362 					buf_ptr += newbie->ipsa_saltlen;
3363 					bcopy(buf_ptr, (uint8_t *)newbie->
3364 					    ipsa_iv, SADB_1TO8(ekey->
3365 					    sadb_key_reserved));
3366 				} else {
3367 					(void) random_get_pseudo_bytes(
3368 					    (uint8_t *)newbie->ipsa_iv,
3369 					    newbie->ipsa_iv_len);
3370 				}
3371 				newbie->ipsa_iv_softexpire =
3372 				    (*newbie->ipsa_iv) << 9;
3373 				newbie->ipsa_iv_hardexpire = *newbie->ipsa_iv;
3374 			}
3375 		}
3376 		bzero((ekey + 1), SADB_1TO8(ekey->sadb_key_bits));
3377 
3378 		/*
3379 		 * Pre-initialize the kernel crypto framework key
3380 		 * structure.
3381 		 */
3382 		newbie->ipsa_kcfencrkey.ck_format = CRYPTO_KEY_RAW;
3383 		newbie->ipsa_kcfencrkey.ck_length = newbie->ipsa_encrkeybits;
3384 		newbie->ipsa_kcfencrkey.ck_data = newbie->ipsa_encrkey;
3385 
3386 		rw_enter(&ipss->ipsec_alg_lock, RW_READER);
3387 		error = ipsec_create_ctx_tmpl(newbie, IPSEC_ALG_ENCR);
3388 		rw_exit(&ipss->ipsec_alg_lock);
3389 		if (error != 0) {
3390 			mutex_exit(&newbie->ipsa_lock);
3391 			/* See above for error explanation. */
3392 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_CTX;
3393 			goto error;
3394 		}
3395 	}
3396 
3397 	if (async)
3398 		newbie->ipsa_flags |= IPSA_F_ASYNC;
3399 
3400 	/*
3401 	 * Ptrs to processing functions.
3402 	 */
3403 	if (newbie->ipsa_type == SADB_SATYPE_ESP)
3404 		ipsecesp_init_funcs(newbie);
3405 	else
3406 		ipsecah_init_funcs(newbie);
3407 	ASSERT(newbie->ipsa_output_func != NULL &&
3408 	    newbie->ipsa_input_func != NULL);
3409 
3410 	/*
3411 	 * Certificate ID stuff.
3412 	 */
3413 	if (ksi->ks_in_extv[SADB_EXT_IDENTITY_SRC] != NULL) {
3414 		sadb_ident_t *id =
3415 		    (sadb_ident_t *)ksi->ks_in_extv[SADB_EXT_IDENTITY_SRC];
3416 
3417 		/*
3418 		 * Can assume strlen() will return okay because ext_check() in
3419 		 * keysock.c prepares the string for us.
3420 		 */
3421 		newbie->ipsa_src_cid = ipsid_lookup(id->sadb_ident_type,
3422 		    (char *)(id+1), ns);
3423 		if (newbie->ipsa_src_cid == NULL) {
3424 			error = ENOMEM;
3425 			mutex_exit(&newbie->ipsa_lock);
3426 			goto error;
3427 		}
3428 	}
3429 
3430 	if (ksi->ks_in_extv[SADB_EXT_IDENTITY_DST] != NULL) {
3431 		sadb_ident_t *id =
3432 		    (sadb_ident_t *)ksi->ks_in_extv[SADB_EXT_IDENTITY_DST];
3433 
3434 		/*
3435 		 * Can assume strlen() will return okay because ext_check() in
3436 		 * keysock.c prepares the string for us.
3437 		 */
3438 		newbie->ipsa_dst_cid = ipsid_lookup(id->sadb_ident_type,
3439 		    (char *)(id+1), ns);
3440 		if (newbie->ipsa_dst_cid == NULL) {
3441 			error = ENOMEM;
3442 			mutex_exit(&newbie->ipsa_lock);
3443 			goto error;
3444 		}
3445 	}
3446 
3447 	/*
3448 	 * sensitivity label handling code:
3449 	 * Convert sens + bitmap into cred_t, and associate it
3450 	 * with the new SA.
3451 	 */
3452 	if (sens != NULL) {
3453 		uint64_t *bitmap = (uint64_t *)(sens + 1);
3454 
3455 		newbie->ipsa_tsl = sadb_label_from_sens(sens, bitmap);
3456 	}
3457 
3458 	/*
3459 	 * Likewise for outer sensitivity.
3460 	 */
3461 	if (osens != NULL) {
3462 		uint64_t *bitmap = (uint64_t *)(osens + 1);
3463 		ts_label_t *tsl, *effective_tsl;
3464 		uint32_t *peer_addr_ptr;
3465 		zoneid_t zoneid = GLOBAL_ZONEID;
3466 		zone_t *zone;
3467 
3468 		peer_addr_ptr = is_inbound ? src_addr_ptr : dst_addr_ptr;
3469 
3470 		tsl = sadb_label_from_sens(osens, bitmap);
3471 		newbie->ipsa_mac_exempt = CONN_MAC_DEFAULT;
3472 
3473 		if (osens->sadb_x_sens_flags & SADB_X_SENS_IMPLICIT) {
3474 			newbie->ipsa_mac_exempt = CONN_MAC_IMPLICIT;
3475 		}
3476 
3477 		error = tsol_check_dest(tsl, peer_addr_ptr,
3478 		    (af == AF_INET6)?IPV6_VERSION:IPV4_VERSION,
3479 		    newbie->ipsa_mac_exempt, B_TRUE, &effective_tsl);
3480 		if (error != 0) {
3481 			label_rele(tsl);
3482 			mutex_exit(&newbie->ipsa_lock);
3483 			goto error;
3484 		}
3485 
3486 		if (effective_tsl != NULL) {
3487 			label_rele(tsl);
3488 			tsl = effective_tsl;
3489 		}
3490 
3491 		newbie->ipsa_otsl = tsl;
3492 
3493 		zone = zone_find_by_label(tsl);
3494 		if (zone != NULL) {
3495 			zoneid = zone->zone_id;
3496 			zone_rele(zone);
3497 		}
3498 		/*
3499 		 * For exclusive stacks we set the zoneid to zero to operate
3500 		 * as if in the global zone for tsol_compute_label_v4/v6
3501 		 */
3502 		if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
3503 			zoneid = GLOBAL_ZONEID;
3504 
3505 		if (af == AF_INET6) {
3506 			error = tsol_compute_label_v6(tsl, zoneid,
3507 			    (in6_addr_t *)peer_addr_ptr,
3508 			    newbie->ipsa_opt_storage, ipst);
3509 		} else {
3510 			error = tsol_compute_label_v4(tsl, zoneid,
3511 			    *peer_addr_ptr, newbie->ipsa_opt_storage, ipst);
3512 		}
3513 		if (error != 0) {
3514 			mutex_exit(&newbie->ipsa_lock);
3515 			goto error;
3516 		}
3517 	}
3518 
3519 
3520 	if (replayext != NULL) {
3521 		if ((replayext->sadb_x_rc_replay32 == 0) &&
3522 		    (replayext->sadb_x_rc_replay64 != 0)) {
3523 			error = EOPNOTSUPP;
3524 			*diagnostic = SADB_X_DIAGNOSTIC_INVALID_REPLAY;
3525 			mutex_exit(&newbie->ipsa_lock);
3526 			goto error;
3527 		}
3528 		newbie->ipsa_replay = replayext->sadb_x_rc_replay32;
3529 	}
3530 
3531 	/* now that the SA has been updated, set its new state */
3532 	newbie->ipsa_state = assoc->sadb_sa_state;
3533 
3534 	if (clone) {
3535 		newbie->ipsa_haspeer = B_TRUE;
3536 	} else {
3537 		if (!is_inbound) {
3538 			lifetime_fuzz(newbie);
3539 		}
3540 	}
3541 	/*
3542 	 * The less locks I hold when doing an insertion and possible cloning,
3543 	 * the better!
3544 	 */
3545 	mutex_exit(&newbie->ipsa_lock);
3546 
3547 	if (clone) {
3548 		newbie_clone = sadb_cloneassoc(newbie);
3549 
3550 		if (newbie_clone == NULL) {
3551 			error = ENOMEM;
3552 			goto error;
3553 		}
3554 	}
3555 
3556 	/*
3557 	 * Enter the bucket locks.  The order of entry is outbound,
3558 	 * inbound.  We map "primary" and "secondary" into outbound and inbound
3559 	 * based on the destination address type.  If the destination address
3560 	 * type is for a node that isn't mine (or potentially mine), the
3561 	 * "primary" bucket is the outbound one.
3562 	 */
3563 	if (!is_inbound) {
3564 		/* primary == outbound */
3565 		mutex_enter(&primary->isaf_lock);
3566 		mutex_enter(&secondary->isaf_lock);
3567 	} else {
3568 		/* primary == inbound */
3569 		mutex_enter(&secondary->isaf_lock);
3570 		mutex_enter(&primary->isaf_lock);
3571 	}
3572 
3573 	/*
3574 	 * sadb_insertassoc() doesn't increment the reference
3575 	 * count.  We therefore have to increment the
3576 	 * reference count one more time to reflect the
3577 	 * pointers of the table that reference this SA.
3578 	 */
3579 	IPSA_REFHOLD(newbie);
3580 
3581 	if (isupdate) {
3582 		/*
3583 		 * Unlink from larval holding cell in the "inbound" fanout.
3584 		 */
3585 		ASSERT(newbie->ipsa_linklock == &primary->isaf_lock ||
3586 		    newbie->ipsa_linklock == &secondary->isaf_lock);
3587 		sadb_unlinkassoc(newbie);
3588 	}
3589 
3590 	mutex_enter(&newbie->ipsa_lock);
3591 	error = sadb_insertassoc(newbie, primary);
3592 	mutex_exit(&newbie->ipsa_lock);
3593 
3594 	if (error != 0) {
3595 		/*
3596 		 * Since sadb_insertassoc() failed, we must decrement the
3597 		 * refcount again so the cleanup code will actually free
3598 		 * the offending SA.
3599 		 */
3600 		IPSA_REFRELE(newbie);
3601 		goto error_unlock;
3602 	}
3603 
3604 	if (newbie_clone != NULL) {
3605 		mutex_enter(&newbie_clone->ipsa_lock);
3606 		error = sadb_insertassoc(newbie_clone, secondary);
3607 		mutex_exit(&newbie_clone->ipsa_lock);
3608 		if (error != 0) {
3609 			/* Collision in secondary table. */
3610 			sadb_unlinkassoc(newbie);  /* This does REFRELE. */
3611 			goto error_unlock;
3612 		}
3613 		IPSA_REFHOLD(newbie_clone);
3614 	} else {
3615 		ASSERT(primary != secondary);
3616 		scratch = ipsec_getassocbyspi(secondary, newbie->ipsa_spi,
3617 		    ALL_ZEROES_PTR, newbie->ipsa_dstaddr, af);
3618 		if (scratch != NULL) {
3619 			/* Collision in secondary table. */
3620 			sadb_unlinkassoc(newbie);  /* This does REFRELE. */
3621 			/* Set the error, since ipsec_getassocbyspi() can't. */
3622 			error = EEXIST;
3623 			goto error_unlock;
3624 		}
3625 	}
3626 
3627 	/* OKAY!  So let's do some reality check assertions. */
3628 
3629 	ASSERT(MUTEX_NOT_HELD(&newbie->ipsa_lock));
3630 	ASSERT(newbie_clone == NULL ||
3631 	    (MUTEX_NOT_HELD(&newbie_clone->ipsa_lock)));
3632 
3633 error_unlock:
3634 
3635 	/*
3636 	 * We can exit the locks in any order.	Only entrance needs to
3637 	 * follow any protocol.
3638 	 */
3639 	mutex_exit(&secondary->isaf_lock);
3640 	mutex_exit(&primary->isaf_lock);
3641 
3642 	if (pair_ext != NULL && error == 0) {
3643 		/* update pair_spi if it exists. */
3644 		ipsa_query_t sq;
3645 
3646 		sq.spp = spp;		/* XXX param */
3647 		error = sadb_form_query(ksi, IPSA_Q_DST, IPSA_Q_SRC|IPSA_Q_DST|
3648 		    IPSA_Q_SA|IPSA_Q_INBOUND|IPSA_Q_OUTBOUND, &sq, diagnostic);
3649 		if (error)
3650 			return (error);
3651 
3652 		error = get_ipsa_pair(&sq, &ipsapp, diagnostic);
3653 
3654 		if (error != 0)
3655 			goto error;
3656 
3657 		if (ipsapp.ipsap_psa_ptr != NULL) {
3658 			*diagnostic = SADB_X_DIAGNOSTIC_PAIR_ALREADY;
3659 			error = EINVAL;
3660 		} else {
3661 			/* update_pairing() sets diagnostic */
3662 			error = update_pairing(&ipsapp, &sq, ksi, diagnostic);
3663 		}
3664 	}
3665 	/* Common error point for this routine. */
3666 error:
3667 	if (newbie != NULL) {
3668 		if (error != 0) {
3669 			/* This SA is broken, let the reaper clean up. */
3670 			mutex_enter(&newbie->ipsa_lock);
3671 			newbie->ipsa_state = IPSA_STATE_DEAD;
3672 			newbie->ipsa_hardexpiretime = 1;
3673 			mutex_exit(&newbie->ipsa_lock);
3674 		}
3675 		IPSA_REFRELE(newbie);
3676 	}
3677 	if (newbie_clone != NULL) {
3678 		IPSA_REFRELE(newbie_clone);
3679 	}
3680 
3681 	if (error == 0) {
3682 		/*
3683 		 * Construct favorable PF_KEY return message and send to
3684 		 * keysock. Update the flags in the original keysock message
3685 		 * to reflect the actual flags in the new SA.
3686 		 *  (Q:  Do I need to pass "newbie"?  If I do,
3687 		 * make sure to REFHOLD, call, then REFRELE.)
3688 		 */
3689 		assoc->sadb_sa_flags = newbie->ipsa_flags;
3690 		sadb_pfkey_echo(pfkey_q, mp, samsg, ksi, NULL);
3691 	}
3692 
3693 	destroy_ipsa_pair(&ipsapp);
3694 	return (error);
3695 }
3696 
3697 /*
3698  * Set the time of first use for a security association.  Update any
3699  * expiration times as a result.
3700  */
3701 void
3702 sadb_set_usetime(ipsa_t *assoc)
3703 {
3704 	time_t snapshot = gethrestime_sec();
3705 
3706 	mutex_enter(&assoc->ipsa_lock);
3707 	assoc->ipsa_lastuse = snapshot;
3708 	assoc->ipsa_idleexpiretime = snapshot + assoc->ipsa_idletime;
3709 
3710 	/*
3711 	 * Caller does check usetime before calling me usually, and
3712 	 * double-checking is better than a mutex_enter/exit hit.
3713 	 */
3714 	if (assoc->ipsa_usetime == 0) {
3715 		/*
3716 		 * This is redundant for outbound SA's, as
3717 		 * ipsec_getassocbyconn() sets the IPSA_F_USED flag already.
3718 		 * Inbound SAs, however, have no such protection.
3719 		 */
3720 		assoc->ipsa_flags |= IPSA_F_USED;
3721 		assoc->ipsa_usetime = snapshot;
3722 
3723 		/*
3724 		 * After setting the use time, see if we have a use lifetime
3725 		 * that would cause the actual SA expiration time to shorten.
3726 		 */
3727 		UPDATE_EXPIRE(assoc, softuselt, softexpiretime);
3728 		UPDATE_EXPIRE(assoc, harduselt, hardexpiretime);
3729 	}
3730 	mutex_exit(&assoc->ipsa_lock);
3731 }
3732 
3733 /*
3734  * Send up a PF_KEY expire message for this association.
3735  */
3736 static void
3737 sadb_expire_assoc(queue_t *pfkey_q, ipsa_t *assoc)
3738 {
3739 	mblk_t *mp, *mp1;
3740 	int alloclen, af;
3741 	sadb_msg_t *samsg;
3742 	sadb_lifetime_t *current, *expire;
3743 	sadb_sa_t *saext;
3744 	uint8_t *end;
3745 	boolean_t tunnel_mode;
3746 
3747 	ASSERT(MUTEX_HELD(&assoc->ipsa_lock));
3748 
3749 	/* Don't bother sending if there's no queue. */
3750 	if (pfkey_q == NULL)
3751 		return;
3752 
3753 	mp = sadb_keysock_out(0);
3754 	if (mp == NULL) {
3755 		/* cmn_err(CE_WARN, */
3756 		/*	"sadb_expire_assoc: Can't allocate KEYSOCK_OUT.\n"); */
3757 		return;
3758 	}
3759 
3760 	alloclen = sizeof (*samsg) + sizeof (*current) + sizeof (*expire) +
3761 	    2 * sizeof (sadb_address_t) + sizeof (*saext);
3762 
3763 	af = assoc->ipsa_addrfam;
3764 	switch (af) {
3765 	case AF_INET:
3766 		alloclen += 2 * sizeof (struct sockaddr_in);
3767 		break;
3768 	case AF_INET6:
3769 		alloclen += 2 * sizeof (struct sockaddr_in6);
3770 		break;
3771 	default:
3772 		/* Won't happen unless there's a kernel bug. */
3773 		freeb(mp);
3774 		cmn_err(CE_WARN,
3775 		    "sadb_expire_assoc: Unknown address length.\n");
3776 		return;
3777 	}
3778 
3779 	tunnel_mode = (assoc->ipsa_flags & IPSA_F_TUNNEL);
3780 	if (tunnel_mode) {
3781 		alloclen += 2 * sizeof (sadb_address_t);
3782 		switch (assoc->ipsa_innerfam) {
3783 		case AF_INET:
3784 			alloclen += 2 * sizeof (struct sockaddr_in);
3785 			break;
3786 		case AF_INET6:
3787 			alloclen += 2 * sizeof (struct sockaddr_in6);
3788 			break;
3789 		default:
3790 			/* Won't happen unless there's a kernel bug. */
3791 			freeb(mp);
3792 			cmn_err(CE_WARN, "sadb_expire_assoc: "
3793 			    "Unknown inner address length.\n");
3794 			return;
3795 		}
3796 	}
3797 
3798 	mp->b_cont = allocb(alloclen, BPRI_HI);
3799 	if (mp->b_cont == NULL) {
3800 		freeb(mp);
3801 		/* cmn_err(CE_WARN, */
3802 		/*	"sadb_expire_assoc: Can't allocate message.\n"); */
3803 		return;
3804 	}
3805 
3806 	mp1 = mp;
3807 	mp = mp->b_cont;
3808 	end = mp->b_wptr + alloclen;
3809 
3810 	samsg = (sadb_msg_t *)mp->b_wptr;
3811 	mp->b_wptr += sizeof (*samsg);
3812 	samsg->sadb_msg_version = PF_KEY_V2;
3813 	samsg->sadb_msg_type = SADB_EXPIRE;
3814 	samsg->sadb_msg_errno = 0;
3815 	samsg->sadb_msg_satype = assoc->ipsa_type;
3816 	samsg->sadb_msg_len = SADB_8TO64(alloclen);
3817 	samsg->sadb_msg_reserved = 0;
3818 	samsg->sadb_msg_seq = 0;
3819 	samsg->sadb_msg_pid = 0;
3820 
3821 	saext = (sadb_sa_t *)mp->b_wptr;
3822 	mp->b_wptr += sizeof (*saext);
3823 	saext->sadb_sa_len = SADB_8TO64(sizeof (*saext));
3824 	saext->sadb_sa_exttype = SADB_EXT_SA;
3825 	saext->sadb_sa_spi = assoc->ipsa_spi;
3826 	saext->sadb_sa_replay = assoc->ipsa_replay_wsize;
3827 	saext->sadb_sa_state = assoc->ipsa_state;
3828 	saext->sadb_sa_auth = assoc->ipsa_auth_alg;
3829 	saext->sadb_sa_encrypt = assoc->ipsa_encr_alg;
3830 	saext->sadb_sa_flags = assoc->ipsa_flags;
3831 
3832 	current = (sadb_lifetime_t *)mp->b_wptr;
3833 	mp->b_wptr += sizeof (sadb_lifetime_t);
3834 	current->sadb_lifetime_len = SADB_8TO64(sizeof (*current));
3835 	current->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3836 	/* We do not support the concept. */
3837 	current->sadb_lifetime_allocations = 0;
3838 	current->sadb_lifetime_bytes = assoc->ipsa_bytes;
3839 	current->sadb_lifetime_addtime = assoc->ipsa_addtime;
3840 	current->sadb_lifetime_usetime = assoc->ipsa_usetime;
3841 
3842 	expire = (sadb_lifetime_t *)mp->b_wptr;
3843 	mp->b_wptr += sizeof (*expire);
3844 	expire->sadb_lifetime_len = SADB_8TO64(sizeof (*expire));
3845 
3846 	if (assoc->ipsa_state == IPSA_STATE_DEAD) {
3847 		expire->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
3848 		expire->sadb_lifetime_allocations = assoc->ipsa_hardalloc;
3849 		expire->sadb_lifetime_bytes = assoc->ipsa_hardbyteslt;
3850 		expire->sadb_lifetime_addtime = assoc->ipsa_hardaddlt;
3851 		expire->sadb_lifetime_usetime = assoc->ipsa_harduselt;
3852 	} else if (assoc->ipsa_state == IPSA_STATE_DYING) {
3853 		expire->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
3854 		expire->sadb_lifetime_allocations = assoc->ipsa_softalloc;
3855 		expire->sadb_lifetime_bytes = assoc->ipsa_softbyteslt;
3856 		expire->sadb_lifetime_addtime = assoc->ipsa_softaddlt;
3857 		expire->sadb_lifetime_usetime = assoc->ipsa_softuselt;
3858 	} else {
3859 		ASSERT(assoc->ipsa_state == IPSA_STATE_MATURE);
3860 		expire->sadb_lifetime_exttype = SADB_X_EXT_LIFETIME_IDLE;
3861 		expire->sadb_lifetime_allocations = 0;
3862 		expire->sadb_lifetime_bytes = 0;
3863 		expire->sadb_lifetime_addtime = assoc->ipsa_idleaddlt;
3864 		expire->sadb_lifetime_usetime = assoc->ipsa_idleuselt;
3865 	}
3866 
3867 	mp->b_wptr = sadb_make_addr_ext(mp->b_wptr, end, SADB_EXT_ADDRESS_SRC,
3868 	    af, assoc->ipsa_srcaddr, tunnel_mode ? 0 : SA_SRCPORT(assoc),
3869 	    SA_PROTO(assoc), 0);
3870 	ASSERT(mp->b_wptr != NULL);
3871 
3872 	mp->b_wptr = sadb_make_addr_ext(mp->b_wptr, end, SADB_EXT_ADDRESS_DST,
3873 	    af, assoc->ipsa_dstaddr, tunnel_mode ? 0 : SA_DSTPORT(assoc),
3874 	    SA_PROTO(assoc), 0);
3875 	ASSERT(mp->b_wptr != NULL);
3876 
3877 	if (tunnel_mode) {
3878 		mp->b_wptr = sadb_make_addr_ext(mp->b_wptr, end,
3879 		    SADB_X_EXT_ADDRESS_INNER_SRC, assoc->ipsa_innerfam,
3880 		    assoc->ipsa_innersrc, SA_SRCPORT(assoc), SA_IPROTO(assoc),
3881 		    assoc->ipsa_innersrcpfx);
3882 		ASSERT(mp->b_wptr != NULL);
3883 		mp->b_wptr = sadb_make_addr_ext(mp->b_wptr, end,
3884 		    SADB_X_EXT_ADDRESS_INNER_DST, assoc->ipsa_innerfam,
3885 		    assoc->ipsa_innerdst, SA_DSTPORT(assoc), SA_IPROTO(assoc),
3886 		    assoc->ipsa_innerdstpfx);
3887 		ASSERT(mp->b_wptr != NULL);
3888 	}
3889 
3890 	/* Can just putnext, we're ready to go! */
3891 	putnext(pfkey_q, mp1);
3892 }
3893 
3894 /*
3895  * "Age" the SA with the number of bytes that was used to protect traffic.
3896  * Send an SADB_EXPIRE message if appropriate.	Return B_TRUE if there was
3897  * enough "charge" left in the SA to protect the data.	Return B_FALSE
3898  * otherwise.  (If B_FALSE is returned, the association either was, or became
3899  * DEAD.)
3900  */
3901 boolean_t
3902 sadb_age_bytes(queue_t *pfkey_q, ipsa_t *assoc, uint64_t bytes,
3903     boolean_t sendmsg)
3904 {
3905 	boolean_t rc = B_TRUE;
3906 	uint64_t newtotal;
3907 
3908 	mutex_enter(&assoc->ipsa_lock);
3909 	newtotal = assoc->ipsa_bytes + bytes;
3910 	if (assoc->ipsa_hardbyteslt != 0 &&
3911 	    newtotal >= assoc->ipsa_hardbyteslt) {
3912 		if (assoc->ipsa_state != IPSA_STATE_DEAD) {
3913 			sadb_delete_cluster(assoc);
3914 			/*
3915 			 * Send EXPIRE message to PF_KEY.  May wish to pawn
3916 			 * this off on another non-interrupt thread.  Also
3917 			 * unlink this SA immediately.
3918 			 */
3919 			assoc->ipsa_state = IPSA_STATE_DEAD;
3920 			if (sendmsg)
3921 				sadb_expire_assoc(pfkey_q, assoc);
3922 			/*
3923 			 * Set non-zero expiration time so sadb_age_assoc()
3924 			 * will work when reaping.
3925 			 */
3926 			assoc->ipsa_hardexpiretime = (time_t)1;
3927 		} /* Else someone beat me to it! */
3928 		rc = B_FALSE;
3929 	} else if (assoc->ipsa_softbyteslt != 0 &&
3930 	    (newtotal >= assoc->ipsa_softbyteslt)) {
3931 		if (assoc->ipsa_state < IPSA_STATE_DYING) {
3932 			/*
3933 			 * Send EXPIRE message to PF_KEY.  May wish to pawn
3934 			 * this off on another non-interrupt thread.
3935 			 */
3936 			assoc->ipsa_state = IPSA_STATE_DYING;
3937 			assoc->ipsa_bytes = newtotal;
3938 			if (sendmsg)
3939 				sadb_expire_assoc(pfkey_q, assoc);
3940 		} /* Else someone beat me to it! */
3941 	}
3942 	if (rc == B_TRUE)
3943 		assoc->ipsa_bytes = newtotal;
3944 	mutex_exit(&assoc->ipsa_lock);
3945 	return (rc);
3946 }
3947 
3948 /*
3949  * "Torch" an individual SA.  Returns NULL, so it can be tail-called from
3950  *     sadb_age_assoc().
3951  */
3952 static ipsa_t *
3953 sadb_torch_assoc(isaf_t *head, ipsa_t *sa)
3954 {
3955 	ASSERT(MUTEX_HELD(&head->isaf_lock));
3956 	ASSERT(MUTEX_HELD(&sa->ipsa_lock));
3957 	ASSERT(sa->ipsa_state == IPSA_STATE_DEAD);
3958 
3959 	/*
3960 	 * Force cached SAs to be revalidated..
3961 	 */
3962 	head->isaf_gen++;
3963 
3964 	mutex_exit(&sa->ipsa_lock);
3965 	sadb_unlinkassoc(sa);
3966 
3967 	return (NULL);
3968 }
3969 
3970 /*
3971  * Do various SA-is-idle activities depending on delta (the number of idle
3972  * seconds on the SA) and/or other properties of the SA.
3973  *
3974  * Return B_TRUE if I've sent a packet, because I have to drop the
3975  * association's mutex before sending a packet out the wire.
3976  */
3977 /* ARGSUSED */
3978 static boolean_t
3979 sadb_idle_activities(ipsa_t *assoc, time_t delta, boolean_t inbound)
3980 {
3981 	ipsecesp_stack_t *espstack = assoc->ipsa_netstack->netstack_ipsecesp;
3982 	int nat_t_interval = espstack->ipsecesp_nat_keepalive_interval;
3983 
3984 	ASSERT(MUTEX_HELD(&assoc->ipsa_lock));
3985 
3986 	if (!inbound && (assoc->ipsa_flags & IPSA_F_NATT_LOC) &&
3987 	    delta >= nat_t_interval &&
3988 	    gethrestime_sec() - assoc->ipsa_last_nat_t_ka >= nat_t_interval) {
3989 		ASSERT(assoc->ipsa_type == SADB_SATYPE_ESP);
3990 		assoc->ipsa_last_nat_t_ka = gethrestime_sec();
3991 		mutex_exit(&assoc->ipsa_lock);
3992 		ipsecesp_send_keepalive(assoc);
3993 		return (B_TRUE);
3994 	}
3995 	return (B_FALSE);
3996 }
3997 
3998 /*
3999  * Return "assoc" if haspeer is true and I send an expire.  This allows
4000  * the consumers' aging functions to tidy up an expired SA's peer.
4001  */
4002 static ipsa_t *
4003 sadb_age_assoc(isaf_t *head, queue_t *pfkey_q, ipsa_t *assoc,
4004     time_t current, int reap_delay, boolean_t inbound)
4005 {
4006 	ipsa_t *retval = NULL;
4007 	boolean_t dropped_mutex = B_FALSE;
4008 
4009 	ASSERT(MUTEX_HELD(&head->isaf_lock));
4010 
4011 	mutex_enter(&assoc->ipsa_lock);
4012 
4013 	if (((assoc->ipsa_state == IPSA_STATE_LARVAL) ||
4014 	    ((assoc->ipsa_state == IPSA_STATE_IDLE) ||
4015 	    (assoc->ipsa_state == IPSA_STATE_ACTIVE_ELSEWHERE) &&
4016 	    (assoc->ipsa_hardexpiretime != 0))) &&
4017 	    (assoc->ipsa_hardexpiretime <= current)) {
4018 		assoc->ipsa_state = IPSA_STATE_DEAD;
4019 		return (sadb_torch_assoc(head, assoc));
4020 	}
4021 
4022 	/*
4023 	 * Check lifetimes.  Fortunately, SA setup is done
4024 	 * such that there are only two times to look at,
4025 	 * softexpiretime, and hardexpiretime.
4026 	 *
4027 	 * Check hard first.
4028 	 */
4029 
4030 	if (assoc->ipsa_hardexpiretime != 0 &&
4031 	    assoc->ipsa_hardexpiretime <= current) {
4032 		if (assoc->ipsa_state == IPSA_STATE_DEAD)
4033 			return (sadb_torch_assoc(head, assoc));
4034 
4035 		if (inbound) {
4036 			sadb_delete_cluster(assoc);
4037 		}
4038 
4039 		/*
4040 		 * Send SADB_EXPIRE with hard lifetime, delay for unlinking.
4041 		 */
4042 		assoc->ipsa_state = IPSA_STATE_DEAD;
4043 		if (assoc->ipsa_haspeer || assoc->ipsa_otherspi != 0) {
4044 			/*
4045 			 * If the SA is paired or peered with another, put
4046 			 * a copy on a list which can be processed later, the
4047 			 * pair/peer SA needs to be updated so the both die
4048 			 * at the same time.
4049 			 *
4050 			 * If I return assoc, I have to bump up its reference
4051 			 * count to keep with the ipsa_t reference count
4052 			 * semantics.
4053 			 */
4054 			IPSA_REFHOLD(assoc);
4055 			retval = assoc;
4056 		}
4057 		sadb_expire_assoc(pfkey_q, assoc);
4058 		assoc->ipsa_hardexpiretime = current + reap_delay;
4059 	} else if (assoc->ipsa_softexpiretime != 0 &&
4060 	    assoc->ipsa_softexpiretime <= current &&
4061 	    assoc->ipsa_state < IPSA_STATE_DYING) {
4062 		/*
4063 		 * Send EXPIRE message to PF_KEY.  May wish to pawn
4064 		 * this off on another non-interrupt thread.
4065 		 */
4066 		assoc->ipsa_state = IPSA_STATE_DYING;
4067 		if (assoc->ipsa_haspeer) {
4068 			/*
4069 			 * If the SA has a peer, update the peer's state
4070 			 * on SOFT_EXPIRE, this is mostly to prevent two
4071 			 * expire messages from effectively the same SA.
4072 			 *
4073 			 * Don't care about paired SA's, then can (and should)
4074 			 * be able to soft expire at different times.
4075 			 *
4076 			 * If I return assoc, I have to bump up its
4077 			 * reference count to keep with the ipsa_t reference
4078 			 * count semantics.
4079 			 */
4080 			IPSA_REFHOLD(assoc);
4081 			retval = assoc;
4082 		}
4083 		sadb_expire_assoc(pfkey_q, assoc);
4084 	} else if (assoc->ipsa_idletime != 0 &&
4085 	    assoc->ipsa_idleexpiretime <= current) {
4086 		if (assoc->ipsa_state == IPSA_STATE_ACTIVE_ELSEWHERE) {
4087 			assoc->ipsa_state = IPSA_STATE_IDLE;
4088 		}
4089 
4090 		/*
4091 		 * Need to handle Mature case
4092 		 */
4093 		if (assoc->ipsa_state == IPSA_STATE_MATURE) {
4094 			sadb_expire_assoc(pfkey_q, assoc);
4095 		}
4096 	} else {
4097 		/* Check idle time activities. */
4098 		dropped_mutex = sadb_idle_activities(assoc,
4099 		    current - assoc->ipsa_lastuse, inbound);
4100 	}
4101 
4102 	if (!dropped_mutex)
4103 		mutex_exit(&assoc->ipsa_lock);
4104 	return (retval);
4105 }
4106 
4107 /*
4108  * Called by a consumer protocol to do ther dirty work of reaping dead
4109  * Security Associations.
4110  *
4111  * NOTE: sadb_age_assoc() marks expired SA's as DEAD but only removed
4112  * SA's that are already marked DEAD, so expired SA's are only reaped
4113  * the second time sadb_ager() runs.
4114  */
4115 void
4116 sadb_ager(sadb_t *sp, queue_t *pfkey_q, int reap_delay, netstack_t *ns)
4117 {
4118 	int i;
4119 	isaf_t *bucket;
4120 	ipsa_t *assoc, *spare;
4121 	iacqf_t *acqlist;
4122 	ipsacq_t *acqrec, *spareacq;
4123 	templist_t *haspeerlist, *newbie;
4124 	/* Snapshot current time now. */
4125 	time_t current = gethrestime_sec();
4126 	haspeerlist = NULL;
4127 
4128 	/*
4129 	 * Do my dirty work.  This includes aging real entries, aging
4130 	 * larvals, and aging outstanding ACQUIREs.
4131 	 *
4132 	 * I hope I don't tie up resources for too long.
4133 	 */
4134 
4135 	/* Age acquires. */
4136 
4137 	for (i = 0; i < sp->sdb_hashsize; i++) {
4138 		acqlist = &sp->sdb_acq[i];
4139 		mutex_enter(&acqlist->iacqf_lock);
4140 		for (acqrec = acqlist->iacqf_ipsacq; acqrec != NULL;
4141 		    acqrec = spareacq) {
4142 			spareacq = acqrec->ipsacq_next;
4143 			if (current > acqrec->ipsacq_expire)
4144 				sadb_destroy_acquire(acqrec, ns);
4145 		}
4146 		mutex_exit(&acqlist->iacqf_lock);
4147 	}
4148 
4149 	/* Age inbound associations. */
4150 	for (i = 0; i < sp->sdb_hashsize; i++) {
4151 		bucket = &(sp->sdb_if[i]);
4152 		mutex_enter(&bucket->isaf_lock);
4153 		for (assoc = bucket->isaf_ipsa; assoc != NULL;
4154 		    assoc = spare) {
4155 			spare = assoc->ipsa_next;
4156 			if (sadb_age_assoc(bucket, pfkey_q, assoc, current,
4157 			    reap_delay, B_TRUE) != NULL) {
4158 				/*
4159 				 * Put SA's which have a peer or SA's which
4160 				 * are paired on a list for processing after
4161 				 * all the hash tables have been walked.
4162 				 *
4163 				 * sadb_age_assoc() increments the refcnt,
4164 				 * effectively doing an IPSA_REFHOLD().
4165 				 */
4166 				newbie = kmem_alloc(sizeof (*newbie),
4167 				    KM_NOSLEEP);
4168 				if (newbie == NULL) {
4169 					/*
4170 					 * Don't forget to REFRELE().
4171 					 */
4172 					IPSA_REFRELE(assoc);
4173 					continue;	/* for loop... */
4174 				}
4175 				newbie->next = haspeerlist;
4176 				newbie->ipsa = assoc;
4177 				haspeerlist = newbie;
4178 			}
4179 		}
4180 		mutex_exit(&bucket->isaf_lock);
4181 	}
4182 
4183 	age_pair_peer_list(haspeerlist, sp, B_FALSE);
4184 	haspeerlist = NULL;
4185 
4186 	/* Age outbound associations. */
4187 	for (i = 0; i < sp->sdb_hashsize; i++) {
4188 		bucket = &(sp->sdb_of[i]);
4189 		mutex_enter(&bucket->isaf_lock);
4190 		for (assoc = bucket->isaf_ipsa; assoc != NULL;
4191 		    assoc = spare) {
4192 			spare = assoc->ipsa_next;
4193 			if (sadb_age_assoc(bucket, pfkey_q, assoc, current,
4194 			    reap_delay, B_FALSE) != NULL) {
4195 				/*
4196 				 * sadb_age_assoc() increments the refcnt,
4197 				 * effectively doing an IPSA_REFHOLD().
4198 				 */
4199 				newbie = kmem_alloc(sizeof (*newbie),
4200 				    KM_NOSLEEP);
4201 				if (newbie == NULL) {
4202 					/*
4203 					 * Don't forget to REFRELE().
4204 					 */
4205 					IPSA_REFRELE(assoc);
4206 					continue;	/* for loop... */
4207 				}
4208 				newbie->next = haspeerlist;
4209 				newbie->ipsa = assoc;
4210 				haspeerlist = newbie;
4211 			}
4212 		}
4213 		mutex_exit(&bucket->isaf_lock);
4214 	}
4215 
4216 	age_pair_peer_list(haspeerlist, sp, B_TRUE);
4217 
4218 	/*
4219 	 * Run a GC pass to clean out dead identities.
4220 	 */
4221 	ipsid_gc(ns);
4222 }
4223 
4224 /*
4225  * Figure out when to reschedule the ager.
4226  */
4227 timeout_id_t
4228 sadb_retimeout(hrtime_t begin, queue_t *pfkey_q, void (*ager)(void *),
4229     void *agerarg, uint_t *intp, uint_t intmax, short mid)
4230 {
4231 	hrtime_t end = gethrtime();
4232 	uint_t interval = *intp;	/* "interval" is in ms. */
4233 
4234 	/*
4235 	 * See how long this took.  If it took too long, increase the
4236 	 * aging interval.
4237 	 */
4238 	if ((end - begin) > MSEC2NSEC(interval)) {
4239 		if (interval >= intmax) {
4240 			/* XXX Rate limit this?  Or recommend flush? */
4241 			(void) strlog(mid, 0, 0, SL_ERROR | SL_WARN,
4242 			    "Too many SA's to age out in %d msec.\n",
4243 			    intmax);
4244 		} else {
4245 			/* Double by shifting by one bit. */
4246 			interval <<= 1;
4247 			interval = min(interval, intmax);
4248 		}
4249 	} else if ((end - begin) <= (MSEC2NSEC(interval) / 2) &&
4250 	    interval > SADB_AGE_INTERVAL_DEFAULT) {
4251 		/*
4252 		 * If I took less than half of the interval, then I should
4253 		 * ratchet the interval back down.  Never automatically
4254 		 * shift below the default aging interval.
4255 		 *
4256 		 * NOTE:This even overrides manual setting of the age
4257 		 *	interval using NDD to lower the setting past the
4258 		 *	default.  In other words, if you set the interval
4259 		 *	lower than the default, and your SADB gets too big,
4260 		 *	the interval will only self-lower back to the default.
4261 		 */
4262 		/* Halve by shifting one bit. */
4263 		interval >>= 1;
4264 		interval = max(interval, SADB_AGE_INTERVAL_DEFAULT);
4265 	}
4266 	*intp = interval;
4267 	return (qtimeout(pfkey_q, ager, agerarg,
4268 	    drv_usectohz(interval * (MICROSEC / MILLISEC))));
4269 }
4270 
4271 
4272 /*
4273  * Update the lifetime values of an SA.	 This is the path an SADB_UPDATE
4274  * message takes when updating a MATURE or DYING SA.
4275  */
4276 static void
4277 sadb_update_lifetimes(ipsa_t *assoc, sadb_lifetime_t *hard,
4278     sadb_lifetime_t *soft, sadb_lifetime_t *idle, boolean_t outbound)
4279 {
4280 	mutex_enter(&assoc->ipsa_lock);
4281 
4282 	/*
4283 	 * XXX RFC 2367 mentions how an SADB_EXT_LIFETIME_CURRENT can be
4284 	 * passed in during an update message.	We currently don't handle
4285 	 * these.
4286 	 */
4287 
4288 	if (hard != NULL) {
4289 		if (hard->sadb_lifetime_bytes != 0)
4290 			assoc->ipsa_hardbyteslt = hard->sadb_lifetime_bytes;
4291 		if (hard->sadb_lifetime_usetime != 0)
4292 			assoc->ipsa_harduselt = hard->sadb_lifetime_usetime;
4293 		if (hard->sadb_lifetime_addtime != 0)
4294 			assoc->ipsa_hardaddlt = hard->sadb_lifetime_addtime;
4295 		if (assoc->ipsa_hardaddlt != 0) {
4296 			assoc->ipsa_hardexpiretime =
4297 			    assoc->ipsa_addtime + assoc->ipsa_hardaddlt;
4298 		}
4299 		if (assoc->ipsa_harduselt != 0 &&
4300 		    assoc->ipsa_flags & IPSA_F_USED) {
4301 			UPDATE_EXPIRE(assoc, harduselt, hardexpiretime);
4302 		}
4303 		if (hard->sadb_lifetime_allocations != 0)
4304 			assoc->ipsa_hardalloc = hard->sadb_lifetime_allocations;
4305 	}
4306 
4307 	if (soft != NULL) {
4308 		if (soft->sadb_lifetime_bytes != 0) {
4309 			if (soft->sadb_lifetime_bytes >
4310 			    assoc->ipsa_hardbyteslt) {
4311 				assoc->ipsa_softbyteslt =
4312 				    assoc->ipsa_hardbyteslt;
4313 			} else {
4314 				assoc->ipsa_softbyteslt =
4315 				    soft->sadb_lifetime_bytes;
4316 			}
4317 		}
4318 		if (soft->sadb_lifetime_usetime != 0) {
4319 			if (soft->sadb_lifetime_usetime >
4320 			    assoc->ipsa_harduselt) {
4321 				assoc->ipsa_softuselt =
4322 				    assoc->ipsa_harduselt;
4323 			} else {
4324 				assoc->ipsa_softuselt =
4325 				    soft->sadb_lifetime_usetime;
4326 			}
4327 		}
4328 		if (soft->sadb_lifetime_addtime != 0) {
4329 			if (soft->sadb_lifetime_addtime >
4330 			    assoc->ipsa_hardexpiretime) {
4331 				assoc->ipsa_softexpiretime =
4332 				    assoc->ipsa_hardexpiretime;
4333 			} else {
4334 				assoc->ipsa_softaddlt =
4335 				    soft->sadb_lifetime_addtime;
4336 			}
4337 		}
4338 		if (assoc->ipsa_softaddlt != 0) {
4339 			assoc->ipsa_softexpiretime =
4340 			    assoc->ipsa_addtime + assoc->ipsa_softaddlt;
4341 		}
4342 		if (assoc->ipsa_softuselt != 0 &&
4343 		    assoc->ipsa_flags & IPSA_F_USED) {
4344 			UPDATE_EXPIRE(assoc, softuselt, softexpiretime);
4345 		}
4346 		if (outbound && assoc->ipsa_softexpiretime != 0) {
4347 			if (assoc->ipsa_state == IPSA_STATE_MATURE)
4348 				lifetime_fuzz(assoc);
4349 		}
4350 
4351 		if (soft->sadb_lifetime_allocations != 0)
4352 			assoc->ipsa_softalloc = soft->sadb_lifetime_allocations;
4353 	}
4354 
4355 	if (idle != NULL) {
4356 		time_t current = gethrestime_sec();
4357 		if ((assoc->ipsa_idleexpiretime <= current) &&
4358 		    (assoc->ipsa_idleaddlt == idle->sadb_lifetime_addtime)) {
4359 			assoc->ipsa_idleexpiretime =
4360 			    current + assoc->ipsa_idleaddlt;
4361 		}
4362 		if (idle->sadb_lifetime_addtime != 0)
4363 			assoc->ipsa_idleaddlt = idle->sadb_lifetime_addtime;
4364 		if (idle->sadb_lifetime_usetime != 0)
4365 			assoc->ipsa_idleuselt = idle->sadb_lifetime_usetime;
4366 		if (assoc->ipsa_idleaddlt != 0) {
4367 			assoc->ipsa_idleexpiretime =
4368 			    current + idle->sadb_lifetime_addtime;
4369 			assoc->ipsa_idletime = idle->sadb_lifetime_addtime;
4370 		}
4371 		if (assoc->ipsa_idleuselt != 0) {
4372 			if (assoc->ipsa_idletime != 0) {
4373 				assoc->ipsa_idletime = min(assoc->ipsa_idletime,
4374 				    assoc->ipsa_idleuselt);
4375 				assoc->ipsa_idleexpiretime =
4376 				    current + assoc->ipsa_idletime;
4377 			} else {
4378 				assoc->ipsa_idleexpiretime =
4379 				    current + assoc->ipsa_idleuselt;
4380 				assoc->ipsa_idletime = assoc->ipsa_idleuselt;
4381 			}
4382 		}
4383 	}
4384 	mutex_exit(&assoc->ipsa_lock);
4385 }
4386 
4387 static int
4388 sadb_update_state(ipsa_t *assoc, uint_t new_state, mblk_t **ipkt_lst)
4389 {
4390 	int rcode = 0;
4391 	time_t current = gethrestime_sec();
4392 
4393 	mutex_enter(&assoc->ipsa_lock);
4394 
4395 	switch (new_state) {
4396 	case SADB_X_SASTATE_ACTIVE_ELSEWHERE:
4397 		if (assoc->ipsa_state == SADB_X_SASTATE_IDLE) {
4398 			assoc->ipsa_state = IPSA_STATE_ACTIVE_ELSEWHERE;
4399 			assoc->ipsa_idleexpiretime =
4400 			    current + assoc->ipsa_idletime;
4401 		}
4402 		break;
4403 	case SADB_X_SASTATE_IDLE:
4404 		if (assoc->ipsa_state == SADB_X_SASTATE_ACTIVE_ELSEWHERE) {
4405 			assoc->ipsa_state = IPSA_STATE_IDLE;
4406 			assoc->ipsa_idleexpiretime =
4407 			    current + assoc->ipsa_idletime;
4408 		} else {
4409 			rcode = EINVAL;
4410 		}
4411 		break;
4412 
4413 	case SADB_X_SASTATE_ACTIVE:
4414 		if (assoc->ipsa_state != SADB_X_SASTATE_IDLE) {
4415 			rcode = EINVAL;
4416 			break;
4417 		}
4418 		assoc->ipsa_state = IPSA_STATE_MATURE;
4419 		assoc->ipsa_idleexpiretime = current + assoc->ipsa_idletime;
4420 
4421 		if (ipkt_lst == NULL) {
4422 			break;
4423 		}
4424 
4425 		if (assoc->ipsa_bpkt_head != NULL) {
4426 			*ipkt_lst = assoc->ipsa_bpkt_head;
4427 			assoc->ipsa_bpkt_head = assoc->ipsa_bpkt_tail = NULL;
4428 			assoc->ipsa_mblkcnt = 0;
4429 		} else {
4430 			*ipkt_lst = NULL;
4431 		}
4432 		break;
4433 	default:
4434 		rcode = EINVAL;
4435 		break;
4436 	}
4437 
4438 	mutex_exit(&assoc->ipsa_lock);
4439 	return (rcode);
4440 }
4441 
4442 /*
4443  * Check a proposed KMC update for sanity.
4444  */
4445 static int
4446 sadb_check_kmc(ipsa_query_t *sq, ipsa_t *sa, int *diagnostic)
4447 {
4448 	uint32_t kmp = sq->kmp;
4449 	uint64_t kmc = sq->kmc;
4450 
4451 	if (sa == NULL)
4452 		return (0);
4453 
4454 	if (sa->ipsa_state == IPSA_STATE_DEAD)
4455 		return (ESRCH);	/* DEAD == Not there, in this case. */
4456 
4457 	if ((kmp != 0) && (sa->ipsa_kmp != 0) && (sa->ipsa_kmp != kmp)) {
4458 		*diagnostic = SADB_X_DIAGNOSTIC_DUPLICATE_KMP;
4459 		return (EINVAL);
4460 	}
4461 
4462 	/* Allow IKEv2 KMCs to update the kmc value for rekeying */
4463 	if ((kmp != SADB_X_KMP_IKEV2) && (kmc != 0) && (sa->ipsa_kmc != 0) &&
4464 	    (sa->ipsa_kmc != kmc)) {
4465 		*diagnostic = SADB_X_DIAGNOSTIC_DUPLICATE_KMC;
4466 		return (EINVAL);
4467 	}
4468 
4469 	return (0);
4470 }
4471 
4472 /*
4473  * Actually update the KMC info.
4474  */
4475 static void
4476 sadb_update_kmc(ipsa_query_t *sq, ipsa_t *sa)
4477 {
4478 	uint32_t kmp = sq->kmp;
4479 	uint64_t kmc = sq->kmc;
4480 
4481 	if (kmp != 0)
4482 		sa->ipsa_kmp = kmp;
4483 	if (kmc != 0)
4484 		sa->ipsa_kmc = kmc;
4485 }
4486 
4487 /*
4488  * Common code to update an SA.
4489  */
4490 
4491 int
4492 sadb_update_sa(mblk_t *mp, keysock_in_t *ksi, mblk_t **ipkt_lst,
4493     sadbp_t *spp, int *diagnostic, queue_t *pfkey_q,
4494     int (*add_sa_func)(mblk_t *, keysock_in_t *, int *, netstack_t *),
4495     netstack_t *ns, uint8_t sadb_msg_type)
4496 {
4497 	sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
4498 	sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT];
4499 	sadb_x_replay_ctr_t *replext =
4500 	    (sadb_x_replay_ctr_t *)ksi->ks_in_extv[SADB_X_EXT_REPLAY_VALUE];
4501 	sadb_lifetime_t *soft =
4502 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
4503 	sadb_lifetime_t *hard =
4504 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
4505 	sadb_lifetime_t *idle =
4506 	    (sadb_lifetime_t *)ksi->ks_in_extv[SADB_X_EXT_LIFETIME_IDLE];
4507 	sadb_x_pair_t *pair_ext =
4508 	    (sadb_x_pair_t *)ksi->ks_in_extv[SADB_X_EXT_PAIR];
4509 	ipsa_t *echo_target = NULL;
4510 	ipsap_t ipsapp;
4511 	ipsa_query_t sq;
4512 	time_t current = gethrestime_sec();
4513 
4514 	sq.spp = spp;		/* XXX param */
4515 	int error = sadb_form_query(ksi, IPSA_Q_SRC|IPSA_Q_DST|IPSA_Q_SA,
4516 	    IPSA_Q_SRC|IPSA_Q_DST|IPSA_Q_SA|IPSA_Q_INBOUND|IPSA_Q_OUTBOUND|
4517 	    IPSA_Q_KMC,
4518 	    &sq, diagnostic);
4519 
4520 	if (error != 0)
4521 		return (error);
4522 
4523 	error = get_ipsa_pair(&sq, &ipsapp, diagnostic);
4524 	if (error != 0)
4525 		return (error);
4526 
4527 	if (ipsapp.ipsap_psa_ptr == NULL && ipsapp.ipsap_sa_ptr != NULL) {
4528 		if (ipsapp.ipsap_sa_ptr->ipsa_state == IPSA_STATE_LARVAL) {
4529 			/*
4530 			 * REFRELE the target and let the add_sa_func()
4531 			 * deal with updating a larval SA.
4532 			 */
4533 			destroy_ipsa_pair(&ipsapp);
4534 			return (add_sa_func(mp, ksi, diagnostic, ns));
4535 		}
4536 	}
4537 
4538 	/*
4539 	 * At this point we have an UPDATE to a MATURE SA. There should
4540 	 * not be any keying material present.
4541 	 */
4542 	if (akey != NULL) {
4543 		*diagnostic = SADB_X_DIAGNOSTIC_AKEY_PRESENT;
4544 		error = EINVAL;
4545 		goto bail;
4546 	}
4547 	if (ekey != NULL) {
4548 		*diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT;
4549 		error = EINVAL;
4550 		goto bail;
4551 	}
4552 
4553 	if (sq.assoc->sadb_sa_state == SADB_X_SASTATE_ACTIVE_ELSEWHERE) {
4554 		if (ipsapp.ipsap_sa_ptr != NULL &&
4555 		    ipsapp.ipsap_sa_ptr->ipsa_state == IPSA_STATE_IDLE) {
4556 			if ((error = sadb_update_state(ipsapp.ipsap_sa_ptr,
4557 			    sq.assoc->sadb_sa_state, NULL)) != 0) {
4558 				*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
4559 				goto bail;
4560 			}
4561 		}
4562 		if (ipsapp.ipsap_psa_ptr != NULL &&
4563 		    ipsapp.ipsap_psa_ptr->ipsa_state == IPSA_STATE_IDLE) {
4564 			if ((error = sadb_update_state(ipsapp.ipsap_psa_ptr,
4565 			    sq.assoc->sadb_sa_state, NULL)) != 0) {
4566 				*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
4567 				goto bail;
4568 			}
4569 		}
4570 	}
4571 	if (sq.assoc->sadb_sa_state == SADB_X_SASTATE_ACTIVE) {
4572 		if (ipsapp.ipsap_sa_ptr != NULL) {
4573 			error = sadb_update_state(ipsapp.ipsap_sa_ptr,
4574 			    sq.assoc->sadb_sa_state,
4575 			    (ipsapp.ipsap_sa_ptr->ipsa_flags &
4576 			    IPSA_F_INBOUND) ? ipkt_lst : NULL);
4577 			if (error) {
4578 				*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
4579 				goto bail;
4580 			}
4581 		}
4582 		if (ipsapp.ipsap_psa_ptr != NULL) {
4583 			error = sadb_update_state(ipsapp.ipsap_psa_ptr,
4584 			    sq.assoc->sadb_sa_state,
4585 			    (ipsapp.ipsap_psa_ptr->ipsa_flags &
4586 			    IPSA_F_INBOUND) ? ipkt_lst : NULL);
4587 			if (error) {
4588 				*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
4589 				goto bail;
4590 			}
4591 		}
4592 		sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr,
4593 		    ksi, echo_target);
4594 		goto bail;
4595 	}
4596 
4597 	/*
4598 	 * Reality checks for updates of active associations.
4599 	 * Sundry first-pass UPDATE-specific reality checks.
4600 	 * Have to do the checks here, because it's after the add_sa code.
4601 	 * XXX STATS : logging/stats here?
4602 	 */
4603 
4604 	if (!((sq.assoc->sadb_sa_state == SADB_SASTATE_MATURE) ||
4605 	    (sq.assoc->sadb_sa_state == SADB_X_SASTATE_ACTIVE_ELSEWHERE))) {
4606 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
4607 		error = EINVAL;
4608 		goto bail;
4609 	}
4610 	if (sq.assoc->sadb_sa_flags & ~spp->s_updateflags) {
4611 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
4612 		error = EINVAL;
4613 		goto bail;
4614 	}
4615 	if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL) {
4616 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_LIFETIME;
4617 		error = EOPNOTSUPP;
4618 		goto bail;
4619 	}
4620 
4621 	if ((*diagnostic = sadb_hardsoftchk(hard, soft, idle)) != 0) {
4622 		error = EINVAL;
4623 		goto bail;
4624 	}
4625 
4626 	if ((*diagnostic = sadb_labelchk(ksi)) != 0)
4627 		return (EINVAL);
4628 
4629 	error = sadb_check_kmc(&sq, ipsapp.ipsap_sa_ptr, diagnostic);
4630 	if (error != 0)
4631 		goto bail;
4632 
4633 	error = sadb_check_kmc(&sq, ipsapp.ipsap_psa_ptr, diagnostic);
4634 	if (error != 0)
4635 		goto bail;
4636 
4637 
4638 	if (ipsapp.ipsap_sa_ptr != NULL) {
4639 		/*
4640 		 * Do not allow replay value change for MATURE or LARVAL SA.
4641 		 */
4642 
4643 		if ((replext != NULL) &&
4644 		    ((ipsapp.ipsap_sa_ptr->ipsa_state == IPSA_STATE_LARVAL) ||
4645 		    (ipsapp.ipsap_sa_ptr->ipsa_state == IPSA_STATE_MATURE))) {
4646 			*diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
4647 			error = EINVAL;
4648 			goto bail;
4649 		}
4650 	}
4651 
4652 
4653 	if (ipsapp.ipsap_sa_ptr != NULL) {
4654 		sadb_update_lifetimes(ipsapp.ipsap_sa_ptr, hard, soft,
4655 		    idle, B_TRUE);
4656 		sadb_update_kmc(&sq, ipsapp.ipsap_sa_ptr);
4657 		if ((replext != NULL) &&
4658 		    (ipsapp.ipsap_sa_ptr->ipsa_replay_wsize != 0)) {
4659 			/*
4660 			 * If an inbound SA, update the replay counter
4661 			 * and check off all the other sequence number
4662 			 */
4663 			if (ksi->ks_in_dsttype == KS_IN_ADDR_ME) {
4664 				if (!sadb_replay_check(ipsapp.ipsap_sa_ptr,
4665 				    replext->sadb_x_rc_replay32)) {
4666 					*diagnostic =
4667 					    SADB_X_DIAGNOSTIC_INVALID_REPLAY;
4668 					error = EINVAL;
4669 					goto bail;
4670 				}
4671 				mutex_enter(&ipsapp.ipsap_sa_ptr->ipsa_lock);
4672 				ipsapp.ipsap_sa_ptr->ipsa_idleexpiretime =
4673 				    current +
4674 				    ipsapp.ipsap_sa_ptr->ipsa_idletime;
4675 				mutex_exit(&ipsapp.ipsap_sa_ptr->ipsa_lock);
4676 			} else {
4677 				mutex_enter(&ipsapp.ipsap_sa_ptr->ipsa_lock);
4678 				ipsapp.ipsap_sa_ptr->ipsa_replay =
4679 				    replext->sadb_x_rc_replay32;
4680 				ipsapp.ipsap_sa_ptr->ipsa_idleexpiretime =
4681 				    current +
4682 				    ipsapp.ipsap_sa_ptr->ipsa_idletime;
4683 				mutex_exit(&ipsapp.ipsap_sa_ptr->ipsa_lock);
4684 			}
4685 		}
4686 	}
4687 
4688 	if (sadb_msg_type == SADB_X_UPDATEPAIR) {
4689 		if (ipsapp.ipsap_psa_ptr != NULL) {
4690 			sadb_update_lifetimes(ipsapp.ipsap_psa_ptr, hard, soft,
4691 			    idle, B_FALSE);
4692 			sadb_update_kmc(&sq, ipsapp.ipsap_psa_ptr);
4693 		} else {
4694 			*diagnostic = SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND;
4695 			error = ESRCH;
4696 			goto bail;
4697 		}
4698 	}
4699 
4700 	if (pair_ext != NULL)
4701 		error = update_pairing(&ipsapp, &sq, ksi, diagnostic);
4702 
4703 	if (error == 0)
4704 		sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr,
4705 		    ksi, echo_target);
4706 bail:
4707 
4708 	destroy_ipsa_pair(&ipsapp);
4709 
4710 	return (error);
4711 }
4712 
4713 
4714 static int
4715 update_pairing(ipsap_t *ipsapp, ipsa_query_t *sq, keysock_in_t *ksi,
4716     int *diagnostic)
4717 {
4718 	sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
4719 	sadb_x_pair_t *pair_ext =
4720 	    (sadb_x_pair_t *)ksi->ks_in_extv[SADB_X_EXT_PAIR];
4721 	int error = 0;
4722 	ipsap_t oipsapp;
4723 	boolean_t undo_pair = B_FALSE;
4724 	uint32_t ipsa_flags;
4725 
4726 	if (pair_ext->sadb_x_pair_spi == 0 || pair_ext->sadb_x_pair_spi ==
4727 	    assoc->sadb_sa_spi) {
4728 		*diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE;
4729 		return (EINVAL);
4730 	}
4731 
4732 	/*
4733 	 * Assume for now that the spi value provided in the SADB_UPDATE
4734 	 * message was valid, update the SA with its pair spi value.
4735 	 * If the spi turns out to be bogus or the SA no longer exists
4736 	 * then this will be detected when the reverse update is made
4737 	 * below.
4738 	 */
4739 	mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock);
4740 	ipsapp->ipsap_sa_ptr->ipsa_flags |= IPSA_F_PAIRED;
4741 	ipsapp->ipsap_sa_ptr->ipsa_otherspi = pair_ext->sadb_x_pair_spi;
4742 	mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock);
4743 
4744 	/*
4745 	 * After updating the ipsa_otherspi element of the SA, get_ipsa_pair()
4746 	 * should now return pointers to the SA *AND* its pair, if this is not
4747 	 * the case, the "otherspi" either did not exist or was deleted. Also
4748 	 * check that "otherspi" is not already paired. If everything looks
4749 	 * good, complete the update. IPSA_REFRELE the first pair_pointer
4750 	 * after this update to ensure its not deleted until we are done.
4751 	 */
4752 	error = get_ipsa_pair(sq, &oipsapp, diagnostic);
4753 	if (error != 0) {
4754 		/*
4755 		 * This should never happen, calling function still has
4756 		 * IPSA_REFHELD on the SA we just updated.
4757 		 */
4758 		return (error);	/* XXX EINVAL instead of ESRCH? */
4759 	}
4760 
4761 	if (oipsapp.ipsap_psa_ptr == NULL) {
4762 		*diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE;
4763 		error = EINVAL;
4764 		undo_pair = B_TRUE;
4765 	} else {
4766 		ipsa_flags = oipsapp.ipsap_psa_ptr->ipsa_flags;
4767 		if ((oipsapp.ipsap_psa_ptr->ipsa_state == IPSA_STATE_DEAD) ||
4768 		    (oipsapp.ipsap_psa_ptr->ipsa_state == IPSA_STATE_DYING)) {
4769 			/* Its dead Jim! */
4770 			*diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE;
4771 			undo_pair = B_TRUE;
4772 		} else if ((ipsa_flags & (IPSA_F_OUTBOUND | IPSA_F_INBOUND)) ==
4773 		    (IPSA_F_OUTBOUND | IPSA_F_INBOUND)) {
4774 			/* This SA is in both hashtables. */
4775 			*diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE;
4776 			undo_pair = B_TRUE;
4777 		} else if (ipsa_flags & IPSA_F_PAIRED) {
4778 			/* This SA is already paired with another. */
4779 			*diagnostic = SADB_X_DIAGNOSTIC_PAIR_ALREADY;
4780 			undo_pair = B_TRUE;
4781 		}
4782 	}
4783 
4784 	if (undo_pair) {
4785 		/* The pair SA does not exist. */
4786 		mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock);
4787 		ipsapp->ipsap_sa_ptr->ipsa_flags &= ~IPSA_F_PAIRED;
4788 		ipsapp->ipsap_sa_ptr->ipsa_otherspi = 0;
4789 		mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock);
4790 	} else {
4791 		mutex_enter(&oipsapp.ipsap_psa_ptr->ipsa_lock);
4792 		oipsapp.ipsap_psa_ptr->ipsa_otherspi = assoc->sadb_sa_spi;
4793 		oipsapp.ipsap_psa_ptr->ipsa_flags |= IPSA_F_PAIRED;
4794 		mutex_exit(&oipsapp.ipsap_psa_ptr->ipsa_lock);
4795 	}
4796 
4797 	destroy_ipsa_pair(&oipsapp);
4798 	return (error);
4799 }
4800 
4801 /*
4802  * The following functions deal with ACQUIRE LISTS.  An ACQUIRE list is
4803  * a list of outstanding SADB_ACQUIRE messages.	 If ipsec_getassocbyconn() fails
4804  * for an outbound datagram, that datagram is queued up on an ACQUIRE record,
4805  * and an SADB_ACQUIRE message is sent up.  Presumably, a user-space key
4806  * management daemon will process the ACQUIRE, use a SADB_GETSPI to reserve
4807  * an SPI value and a larval SA, then SADB_UPDATE the larval SA, and ADD the
4808  * other direction's SA.
4809  */
4810 
4811 /*
4812  * Check the ACQUIRE lists.  If there's an existing ACQUIRE record,
4813  * grab it, lock it, and return it.  Otherwise return NULL.
4814  *
4815  * XXX MLS number of arguments getting unwieldy here
4816  */
4817 static ipsacq_t *
4818 sadb_checkacquire(iacqf_t *bucket, ipsec_action_t *ap, ipsec_policy_t *pp,
4819     uint32_t *src, uint32_t *dst, uint32_t *isrc, uint32_t *idst,
4820     uint64_t unique_id, ts_label_t *tsl)
4821 {
4822 	ipsacq_t *walker;
4823 	sa_family_t fam;
4824 	uint32_t blank_address[4] = {0, 0, 0, 0};
4825 
4826 	if (isrc == NULL) {
4827 		ASSERT(idst == NULL);
4828 		isrc = idst = blank_address;
4829 	}
4830 
4831 	/*
4832 	 * Scan list for duplicates.  Check for UNIQUE, src/dest, policy.
4833 	 *
4834 	 * XXX May need search for duplicates based on other things too!
4835 	 */
4836 	for (walker = bucket->iacqf_ipsacq; walker != NULL;
4837 	    walker = walker->ipsacq_next) {
4838 		mutex_enter(&walker->ipsacq_lock);
4839 		fam = walker->ipsacq_addrfam;
4840 		if (IPSA_ARE_ADDR_EQUAL(dst, walker->ipsacq_dstaddr, fam) &&
4841 		    IPSA_ARE_ADDR_EQUAL(src, walker->ipsacq_srcaddr, fam) &&
4842 		    ip_addr_match((uint8_t *)isrc, walker->ipsacq_innersrcpfx,
4843 		    (in6_addr_t *)walker->ipsacq_innersrc) &&
4844 		    ip_addr_match((uint8_t *)idst, walker->ipsacq_innerdstpfx,
4845 		    (in6_addr_t *)walker->ipsacq_innerdst) &&
4846 		    (ap == walker->ipsacq_act) &&
4847 		    (pp == walker->ipsacq_policy) &&
4848 		    /* XXX do deep compares of ap/pp? */
4849 		    (unique_id == walker->ipsacq_unique_id) &&
4850 		    (ipsec_label_match(tsl, walker->ipsacq_tsl)))
4851 			break;			/* everything matched */
4852 		mutex_exit(&walker->ipsacq_lock);
4853 	}
4854 
4855 	return (walker);
4856 }
4857 
4858 /*
4859  * Generate an SADB_ACQUIRE base message mblk, including KEYSOCK_OUT metadata.
4860  * In other words, this will return, upon success, a two-mblk chain.
4861  */
4862 static inline mblk_t *
4863 sadb_acquire_msg_base(minor_t serial, uint8_t satype, uint32_t seq, pid_t pid)
4864 {
4865 	mblk_t *mp;
4866 	sadb_msg_t *samsg;
4867 
4868 	mp = sadb_keysock_out(serial);
4869 	if (mp == NULL)
4870 		return (NULL);
4871 	mp->b_cont = allocb(sizeof (sadb_msg_t), BPRI_HI);
4872 	if (mp->b_cont == NULL) {
4873 		freeb(mp);
4874 		return (NULL);
4875 	}
4876 
4877 	samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
4878 	mp->b_cont->b_wptr += sizeof (*samsg);
4879 	samsg->sadb_msg_version = PF_KEY_V2;
4880 	samsg->sadb_msg_type = SADB_ACQUIRE;
4881 	samsg->sadb_msg_errno = 0;
4882 	samsg->sadb_msg_reserved = 0;
4883 	samsg->sadb_msg_satype = satype;
4884 	samsg->sadb_msg_seq = seq;
4885 	samsg->sadb_msg_pid = pid;
4886 
4887 	return (mp);
4888 }
4889 
4890 /*
4891  * Generate address and TX/MLS sensitivity label PF_KEY extensions that are
4892  * common to both regular and extended ACQUIREs.
4893  */
4894 static mblk_t *
4895 sadb_acquire_msg_common(ipsec_selector_t *sel, ipsec_policy_t *pp,
4896     ipsec_action_t *ap, boolean_t tunnel_mode, ts_label_t *tsl,
4897     sadb_sens_t *sens)
4898 {
4899 	size_t len;
4900 	mblk_t *mp;
4901 	uint8_t *start, *cur, *end;
4902 	uint32_t *saddrptr, *daddrptr;
4903 	sa_family_t af;
4904 	ipsec_action_t *oldap;
4905 	ipsec_selkey_t *ipsl;
4906 	uint8_t proto, pfxlen;
4907 	uint16_t lport, rport;
4908 	int senslen = 0;
4909 
4910 	/*
4911 	 * Get action pointer set if it isn't already.
4912 	 */
4913 	oldap = ap;
4914 	if (pp != NULL) {
4915 		ap = pp->ipsp_act;
4916 		if (ap == NULL)
4917 			ap = oldap;
4918 	}
4919 
4920 	/*
4921 	 * Biggest-case scenario:
4922 	 * 4x (sadb_address_t + struct sockaddr_in6)
4923 	 *	(src, dst, isrc, idst)
4924 	 *	(COMING SOON, 6x, because of triggering-packet contents.)
4925 	 * sadb_x_kmc_t
4926 	 * sadb_sens_t
4927 	 * And wiggle room for label bitvectors.  Luckily there are
4928 	 * programmatic ways to find it.
4929 	 */
4930 	len = 4 * (sizeof (sadb_address_t) + sizeof (struct sockaddr_in6));
4931 
4932 	/* Figure out full and proper length of sensitivity labels. */
4933 	if (sens != NULL) {
4934 		ASSERT(tsl == NULL);
4935 		senslen = SADB_64TO8(sens->sadb_sens_len);
4936 	} else if (tsl != NULL) {
4937 		senslen = sadb_sens_len_from_label(tsl);
4938 	}
4939 #ifdef DEBUG
4940 	else {
4941 		ASSERT(senslen == 0);
4942 	}
4943 #endif /* DEBUG */
4944 	len += senslen;
4945 
4946 	mp = allocb(len, BPRI_HI);
4947 	if (mp == NULL)
4948 		return (NULL);
4949 
4950 	start = mp->b_rptr;
4951 	end = start + len;
4952 	cur = start;
4953 
4954 	/*
4955 	 * Address extensions first, from most-recently-defined to least.
4956 	 * (This should immediately trigger surprise or verify robustness on
4957 	 * older apps, like in.iked.)
4958 	 */
4959 	if (tunnel_mode) {
4960 		/*
4961 		 * Form inner address extensions based NOT on the inner
4962 		 * selectors (i.e. the packet data), but on the policy's
4963 		 * selector key (i.e. the policy's selector information).
4964 		 *
4965 		 * NOTE:  The position of IPv4 and IPv6 addresses is the
4966 		 * same in ipsec_selkey_t (unless the compiler does very
4967 		 * strange things with unions, consult your local C language
4968 		 * lawyer for details).
4969 		 */
4970 		ASSERT(pp != NULL);
4971 
4972 		ipsl = &(pp->ipsp_sel->ipsl_key);
4973 		if (ipsl->ipsl_valid & IPSL_IPV4) {
4974 			af = AF_INET;
4975 			ASSERT(sel->ips_protocol == IPPROTO_ENCAP);
4976 			ASSERT(!(ipsl->ipsl_valid & IPSL_IPV6));
4977 		} else {
4978 			af = AF_INET6;
4979 			ASSERT(sel->ips_protocol == IPPROTO_IPV6);
4980 			ASSERT(ipsl->ipsl_valid & IPSL_IPV6);
4981 		}
4982 
4983 		if (ipsl->ipsl_valid & IPSL_LOCAL_ADDR) {
4984 			saddrptr = (uint32_t *)(&ipsl->ipsl_local);
4985 			pfxlen = ipsl->ipsl_local_pfxlen;
4986 		} else {
4987 			saddrptr = (uint32_t *)(&ipv6_all_zeros);
4988 			pfxlen = 0;
4989 		}
4990 		/* XXX What about ICMP type/code? */
4991 		lport = (ipsl->ipsl_valid & IPSL_LOCAL_PORT) ?
4992 		    ipsl->ipsl_lport : 0;
4993 		proto = (ipsl->ipsl_valid & IPSL_PROTOCOL) ?
4994 		    ipsl->ipsl_proto : 0;
4995 
4996 		cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_SRC,
4997 		    af, saddrptr, lport, proto, pfxlen);
4998 		if (cur == NULL) {
4999 			freeb(mp);
5000 			return (NULL);
5001 		}
5002 
5003 		if (ipsl->ipsl_valid & IPSL_REMOTE_ADDR) {
5004 			daddrptr = (uint32_t *)(&ipsl->ipsl_remote);
5005 			pfxlen = ipsl->ipsl_remote_pfxlen;
5006 		} else {
5007 			daddrptr = (uint32_t *)(&ipv6_all_zeros);
5008 			pfxlen = 0;
5009 		}
5010 		/* XXX What about ICMP type/code? */
5011 		rport = (ipsl->ipsl_valid & IPSL_REMOTE_PORT) ?
5012 		    ipsl->ipsl_rport : 0;
5013 
5014 		cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_DST,
5015 		    af, daddrptr, rport, proto, pfxlen);
5016 		if (cur == NULL) {
5017 			freeb(mp);
5018 			return (NULL);
5019 		}
5020 		/*
5021 		 * TODO  - if we go to 3884's dream of transport mode IP-in-IP
5022 		 * _with_ inner-packet address selectors, we'll need to further
5023 		 * distinguish tunnel mode here.  For now, having inner
5024 		 * addresses and/or ports is sufficient.
5025 		 *
5026 		 * Meanwhile, whack proto/ports to reflect IP-in-IP for the
5027 		 * outer addresses.
5028 		 */
5029 		proto = sel->ips_protocol;	/* Either _ENCAP or _IPV6 */
5030 		lport = rport = 0;
5031 	} else if ((ap != NULL) && (!ap->ipa_want_unique)) {
5032 		/*
5033 		 * For cases when the policy calls out specific ports (or not).
5034 		 */
5035 		proto = 0;
5036 		lport = 0;
5037 		rport = 0;
5038 		if (pp != NULL) {
5039 			ipsl = &(pp->ipsp_sel->ipsl_key);
5040 			if (ipsl->ipsl_valid & IPSL_PROTOCOL)
5041 				proto = ipsl->ipsl_proto;
5042 			if (ipsl->ipsl_valid & IPSL_REMOTE_PORT)
5043 				rport = ipsl->ipsl_rport;
5044 			if (ipsl->ipsl_valid & IPSL_LOCAL_PORT)
5045 				lport = ipsl->ipsl_lport;
5046 		}
5047 	} else {
5048 		/*
5049 		 * For require-unique-SA policies.
5050 		 */
5051 		proto = sel->ips_protocol;
5052 		lport = sel->ips_local_port;
5053 		rport = sel->ips_remote_port;
5054 	}
5055 
5056 	/*
5057 	 * Regular addresses.  These are outer-packet ones for tunnel mode.
5058 	 * Or for transport mode, the regulard address & port information.
5059 	 */
5060 	af = sel->ips_isv4 ? AF_INET : AF_INET6;
5061 
5062 	/*
5063 	 * NOTE:  The position of IPv4 and IPv6 addresses is the same in
5064 	 * ipsec_selector_t.
5065 	 */
5066 	cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_SRC, af,
5067 	    (uint32_t *)(&sel->ips_local_addr_v6), lport, proto, 0);
5068 	if (cur == NULL) {
5069 		freeb(mp);
5070 		return (NULL);
5071 	}
5072 
5073 	cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_DST, af,
5074 	    (uint32_t *)(&sel->ips_remote_addr_v6), rport, proto, 0);
5075 	if (cur == NULL) {
5076 		freeb(mp);
5077 		return (NULL);
5078 	}
5079 
5080 	/*
5081 	 * If present, generate a sensitivity label.
5082 	 */
5083 	if (cur + senslen > end) {
5084 		freeb(mp);
5085 		return (NULL);
5086 	}
5087 	if (sens != NULL) {
5088 		/* Explicit sadb_sens_t, usually from inverse-ACQUIRE. */
5089 		bcopy(sens, cur, senslen);
5090 	} else if (tsl != NULL) {
5091 		/* Generate sadb_sens_t from ACQUIRE source. */
5092 		sadb_sens_from_label((sadb_sens_t *)cur, SADB_EXT_SENSITIVITY,
5093 		    tsl, senslen);
5094 	}
5095 #ifdef DEBUG
5096 	else {
5097 		ASSERT(senslen == 0);
5098 	}
5099 #endif /* DEBUG */
5100 	cur += senslen;
5101 	mp->b_wptr = cur;
5102 
5103 	return (mp);
5104 }
5105 
5106 /*
5107  * Generate a regular ACQUIRE's proposal extension and KMC information..
5108  */
5109 static mblk_t *
5110 sadb_acquire_prop(ipsec_action_t *ap, netstack_t *ns, boolean_t do_esp)
5111 {
5112 	ipsec_stack_t *ipss = ns->netstack_ipsec;
5113 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
5114 	ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
5115 	mblk_t *mp = NULL;
5116 	sadb_prop_t *prop;
5117 	sadb_comb_t *comb;
5118 	ipsec_action_t *walker;
5119 	int ncombs, allocsize, ealgid, aalgid, aminbits, amaxbits, eminbits,
5120 	    emaxbits, esaltlen, replay;
5121 	uint64_t softbytes, hardbytes, softaddtime, hardaddtime, softusetime,
5122 	    hardusetime;
5123 	uint64_t kmc = 0;
5124 	uint32_t kmp = 0;
5125 
5126 	/*
5127 	 * Since it's an rwlock read, AND writing to the IPsec algorithms is
5128 	 * rare, just acquire it once up top, and drop it upon return.
5129 	 */
5130 	rw_enter(&ipss->ipsec_alg_lock, RW_READER);
5131 	if (do_esp) {
5132 		uint64_t num_aalgs, num_ealgs;
5133 
5134 		if (espstack->esp_kstats == NULL)
5135 			goto bail;
5136 
5137 		num_aalgs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
5138 		num_ealgs = ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
5139 		if (num_ealgs == 0)
5140 			goto bail;	/* IPsec not loaded yet, apparently. */
5141 		num_aalgs++;	/* No-auth or self-auth-crypto ESP. */
5142 
5143 		/* Use netstack's maximum loaded algorithms... */
5144 		ncombs = num_ealgs * num_aalgs;
5145 		replay =  espstack->ipsecesp_replay_size;
5146 	} else {
5147 		if (ahstack->ah_kstats == NULL)
5148 			goto bail;
5149 
5150 		ncombs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
5151 
5152 		if (ncombs == 0)
5153 			goto bail;	/* IPsec not loaded yet, apparently. */
5154 		replay =  ahstack->ipsecah_replay_size;
5155 	}
5156 
5157 	allocsize = sizeof (*prop) + ncombs * sizeof (*comb) +
5158 	    sizeof (sadb_x_kmc_t);
5159 	mp = allocb(allocsize, BPRI_HI);
5160 	if (mp == NULL)
5161 		goto bail;
5162 	prop = (sadb_prop_t *)mp->b_rptr;
5163 	mp->b_wptr += sizeof (*prop);
5164 	comb = (sadb_comb_t *)mp->b_wptr;
5165 	/* Decrement allocsize, if it goes to or below 0, stop. */
5166 	allocsize -= sizeof (*prop);
5167 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5168 	prop->sadb_prop_len = SADB_8TO64(sizeof (*prop));
5169 	*(uint32_t *)(&prop->sadb_prop_replay) = 0;	/* Quick zero-out! */
5170 	prop->sadb_prop_replay = replay;
5171 
5172 	/*
5173 	 * Based upon algorithm properties, and what-not, prioritize a
5174 	 * proposal, based on the ordering of the ESP algorithms in the
5175 	 * alternatives in the policy rule or socket that was placed
5176 	 * in the acquire record.
5177 	 *
5178 	 * For each action in policy list
5179 	 *   Add combination.
5180 	 *   I should not hit it, but if I've hit limit, return.
5181 	 */
5182 
5183 	for (walker = ap; walker != NULL; walker = walker->ipa_next) {
5184 		ipsec_alginfo_t *ealg, *aalg;
5185 		ipsec_prot_t *prot;
5186 
5187 		if (walker->ipa_act.ipa_type != IPSEC_POLICY_APPLY)
5188 			continue;
5189 
5190 		prot = &walker->ipa_act.ipa_apply;
5191 		if (walker->ipa_act.ipa_apply.ipp_km_proto != 0)
5192 			kmp = walker->ipa_act.ipa_apply.ipp_km_proto;
5193 		if (walker->ipa_act.ipa_apply.ipp_km_cookie != 0)
5194 			kmc = walker->ipa_act.ipa_apply.ipp_km_cookie;
5195 		if (walker->ipa_act.ipa_apply.ipp_replay_depth) {
5196 			prop->sadb_prop_replay =
5197 			    walker->ipa_act.ipa_apply.ipp_replay_depth;
5198 		}
5199 
5200 		if (do_esp) {
5201 			if (!prot->ipp_use_esp)
5202 				continue;
5203 
5204 			if (prot->ipp_esp_auth_alg != 0) {
5205 				aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
5206 				    [prot->ipp_esp_auth_alg];
5207 				if (aalg == NULL || !ALG_VALID(aalg))
5208 					continue;
5209 			} else
5210 				aalg = NULL;
5211 
5212 			ASSERT(prot->ipp_encr_alg > 0);
5213 			ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR]
5214 			    [prot->ipp_encr_alg];
5215 			if (ealg == NULL || !ALG_VALID(ealg))
5216 				continue;
5217 
5218 			/*
5219 			 * These may want to come from policy rule..
5220 			 */
5221 			softbytes = espstack->ipsecesp_default_soft_bytes;
5222 			hardbytes = espstack->ipsecesp_default_hard_bytes;
5223 			softaddtime = espstack->ipsecesp_default_soft_addtime;
5224 			hardaddtime = espstack->ipsecesp_default_hard_addtime;
5225 			softusetime = espstack->ipsecesp_default_soft_usetime;
5226 			hardusetime = espstack->ipsecesp_default_hard_usetime;
5227 		} else {
5228 			if (!prot->ipp_use_ah)
5229 				continue;
5230 			ealg = NULL;
5231 			aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
5232 			    [prot->ipp_auth_alg];
5233 			if (aalg == NULL || !ALG_VALID(aalg))
5234 				continue;
5235 
5236 			/*
5237 			 * These may want to come from policy rule..
5238 			 */
5239 			softbytes = ahstack->ipsecah_default_soft_bytes;
5240 			hardbytes = ahstack->ipsecah_default_hard_bytes;
5241 			softaddtime = ahstack->ipsecah_default_soft_addtime;
5242 			hardaddtime = ahstack->ipsecah_default_hard_addtime;
5243 			softusetime = ahstack->ipsecah_default_soft_usetime;
5244 			hardusetime = ahstack->ipsecah_default_hard_usetime;
5245 		}
5246 
5247 		if (ealg == NULL) {
5248 			ealgid = eminbits = emaxbits = esaltlen = 0;
5249 		} else {
5250 			ealgid = ealg->alg_id;
5251 			eminbits =
5252 			    MAX(prot->ipp_espe_minbits, ealg->alg_ef_minbits);
5253 			emaxbits =
5254 			    MIN(prot->ipp_espe_maxbits, ealg->alg_ef_maxbits);
5255 			esaltlen = ealg->alg_saltlen;
5256 		}
5257 
5258 		if (aalg == NULL) {
5259 			aalgid = aminbits = amaxbits = 0;
5260 		} else {
5261 			aalgid = aalg->alg_id;
5262 			aminbits = MAX(prot->ipp_espa_minbits,
5263 			    aalg->alg_ef_minbits);
5264 			amaxbits = MIN(prot->ipp_espa_maxbits,
5265 			    aalg->alg_ef_maxbits);
5266 		}
5267 
5268 		comb->sadb_comb_flags = 0;
5269 		comb->sadb_comb_reserved = 0;
5270 		comb->sadb_comb_encrypt = ealgid;
5271 		comb->sadb_comb_encrypt_minbits = eminbits;
5272 		comb->sadb_comb_encrypt_maxbits = emaxbits;
5273 		comb->sadb_x_comb_encrypt_saltbits = SADB_8TO1(esaltlen);
5274 		comb->sadb_comb_auth = aalgid;
5275 		comb->sadb_comb_auth_minbits = aminbits;
5276 		comb->sadb_comb_auth_maxbits = amaxbits;
5277 		comb->sadb_comb_soft_allocations = 0;
5278 		comb->sadb_comb_hard_allocations = 0;
5279 		comb->sadb_comb_soft_bytes = softbytes;
5280 		comb->sadb_comb_hard_bytes = hardbytes;
5281 		comb->sadb_comb_soft_addtime = softaddtime;
5282 		comb->sadb_comb_hard_addtime = hardaddtime;
5283 		comb->sadb_comb_soft_usetime = softusetime;
5284 		comb->sadb_comb_hard_usetime = hardusetime;
5285 
5286 		prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
5287 		mp->b_wptr += sizeof (*comb);
5288 		allocsize -= sizeof (*comb);
5289 		/* Should never dip BELOW sizeof (KM cookie extension). */
5290 		ASSERT3S(allocsize, >=, sizeof (sadb_x_kmc_t));
5291 		if (allocsize <= sizeof (sadb_x_kmc_t))
5292 			break;	/* out of space.. */
5293 		comb++;
5294 	}
5295 
5296 	/* Don't include KMC extension if there's no room. */
5297 	if (((kmp != 0) || (kmc != 0)) && allocsize >= sizeof (sadb_x_kmc_t)) {
5298 		if (sadb_make_kmc_ext(mp->b_wptr,
5299 		    mp->b_wptr + sizeof (sadb_x_kmc_t), kmp, kmc) == NULL) {
5300 			freeb(mp);
5301 			mp = NULL;
5302 			goto bail;
5303 		}
5304 		mp->b_wptr += sizeof (sadb_x_kmc_t);
5305 		prop->sadb_prop_len += SADB_8TO64(sizeof (sadb_x_kmc_t));
5306 	}
5307 
5308 bail:
5309 	rw_exit(&ipss->ipsec_alg_lock);
5310 	return (mp);
5311 }
5312 
5313 /*
5314  * Generate an extended ACQUIRE's extended-proposal extension.
5315  */
5316 static mblk_t *
5317 sadb_acquire_extended_prop(ipsec_action_t *ap, netstack_t *ns)
5318 {
5319 	sadb_prop_t *eprop;
5320 	uint8_t *cur, *end;
5321 	mblk_t *mp;
5322 	int allocsize, numecombs = 0, numalgdescs = 0;
5323 	uint32_t kmp = 0, replay = 0;
5324 	uint64_t kmc = 0;
5325 	ipsec_action_t *walker;
5326 
5327 	allocsize = sizeof (*eprop);
5328 
5329 	/*
5330 	 * Going to walk through the action list twice.  Once for allocation
5331 	 * measurement, and once for actual construction.
5332 	 */
5333 	for (walker = ap; walker != NULL; walker = walker->ipa_next) {
5334 		ipsec_prot_t *ipp;
5335 
5336 		/*
5337 		 * Skip non-IPsec policies
5338 		 */
5339 		if (walker->ipa_act.ipa_type != IPSEC_ACT_APPLY)
5340 			continue;
5341 
5342 		ipp = &walker->ipa_act.ipa_apply;
5343 
5344 		if (walker->ipa_act.ipa_apply.ipp_km_proto)
5345 			kmp = ipp->ipp_km_proto;
5346 		if (walker->ipa_act.ipa_apply.ipp_km_cookie)
5347 			kmc = ipp->ipp_km_cookie;
5348 		if (walker->ipa_act.ipa_apply.ipp_replay_depth)
5349 			replay = ipp->ipp_replay_depth;
5350 
5351 		if (ipp->ipp_use_ah)
5352 			numalgdescs++;
5353 		if (ipp->ipp_use_esp) {
5354 			numalgdescs++;
5355 			if (ipp->ipp_use_espa)
5356 				numalgdescs++;
5357 		}
5358 
5359 		numecombs++;
5360 	}
5361 	ASSERT(numecombs > 0);
5362 
5363 	allocsize += numecombs * sizeof (sadb_x_ecomb_t) +
5364 	    numalgdescs * sizeof (sadb_x_algdesc_t) + sizeof (sadb_x_kmc_t);
5365 	mp = allocb(allocsize, BPRI_HI);
5366 	if (mp == NULL)
5367 		return (NULL);
5368 	eprop = (sadb_prop_t *)mp->b_rptr;
5369 	end = mp->b_rptr + allocsize;
5370 	cur = mp->b_rptr + sizeof (*eprop);
5371 
5372 	eprop->sadb_prop_exttype = SADB_X_EXT_EPROP;
5373 	eprop->sadb_x_prop_ereserved = 0;
5374 	eprop->sadb_x_prop_numecombs = 0;
5375 	*(uint32_t *)(&eprop->sadb_prop_replay) = 0;	/* Quick zero-out! */
5376 	/* Pick ESP's replay default if need be. */
5377 	eprop->sadb_prop_replay = (replay == 0) ?
5378 	    ns->netstack_ipsecesp->ipsecesp_replay_size : replay;
5379 
5380 	/* This time, walk through and actually allocate. */
5381 	for (walker = ap; walker != NULL; walker = walker->ipa_next) {
5382 		/*
5383 		 * Skip non-IPsec policies
5384 		 */
5385 		if (walker->ipa_act.ipa_type != IPSEC_ACT_APPLY)
5386 			continue;
5387 		cur = sadb_action_to_ecomb(cur, end, walker, ns);
5388 		if (cur == NULL) {
5389 			/* NOTE: inverse-ACQUIRE should note this as ENOMEM. */
5390 			freeb(mp);
5391 			return (NULL);
5392 		}
5393 		eprop->sadb_x_prop_numecombs++;
5394 	}
5395 
5396 	ASSERT(end - cur >= sizeof (sadb_x_kmc_t));
5397 	if ((kmp != 0) || (kmc != 0)) {
5398 		cur = sadb_make_kmc_ext(cur, end, kmp, kmc);
5399 		if (cur == NULL) {
5400 			freeb(mp);
5401 			return (NULL);
5402 		}
5403 	}
5404 	mp->b_wptr = cur;
5405 	eprop->sadb_prop_len = SADB_8TO64(cur - mp->b_rptr);
5406 
5407 	return (mp);
5408 }
5409 
5410 /*
5411  * For this mblk, insert a new acquire record.  Assume bucket contains addrs
5412  * of all of the same length.  Give up (and drop) if memory
5413  * cannot be allocated for a new one; otherwise, invoke callback to
5414  * send the acquire up..
5415  *
5416  * In cases where we need both AH and ESP, add the SA to the ESP ACQUIRE
5417  * list.  The ah_add_sa_finish() routines can look at the packet's attached
5418  * attributes and handle this case specially.
5419  */
5420 void
5421 sadb_acquire(mblk_t *datamp, ip_xmit_attr_t *ixa, boolean_t need_ah,
5422     boolean_t need_esp)
5423 {
5424 	mblk_t	*asyncmp, *regular, *extended, *common, *prop, *eprop;
5425 	sadbp_t *spp;
5426 	sadb_t *sp;
5427 	ipsacq_t *newbie;
5428 	iacqf_t *bucket;
5429 	ipha_t *ipha = (ipha_t *)datamp->b_rptr;
5430 	ip6_t *ip6h = (ip6_t *)datamp->b_rptr;
5431 	uint32_t *src, *dst, *isrc, *idst;
5432 	ipsec_policy_t *pp = ixa->ixa_ipsec_policy;
5433 	ipsec_action_t *ap = ixa->ixa_ipsec_action;
5434 	sa_family_t af;
5435 	int hashoffset;
5436 	uint32_t seq;
5437 	uint64_t unique_id = 0;
5438 	boolean_t tunnel_mode = (ixa->ixa_flags & IXAF_IPSEC_TUNNEL) != 0;
5439 	ts_label_t	*tsl;
5440 	netstack_t	*ns = ixa->ixa_ipst->ips_netstack;
5441 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
5442 	ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
5443 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
5444 	ipsec_selector_t sel;
5445 	queue_t *q;
5446 
5447 	ASSERT((pp != NULL) || (ap != NULL));
5448 
5449 	ASSERT(need_ah || need_esp);
5450 
5451 	/* Assign sadb pointers */
5452 	if (need_esp) {
5453 		/*
5454 		 * ESP happens first if we need both AH and ESP.
5455 		 */
5456 		spp = &espstack->esp_sadb;
5457 	} else {
5458 		spp = &ahstack->ah_sadb;
5459 	}
5460 	sp = (ixa->ixa_flags & IXAF_IS_IPV4) ? &spp->s_v4 : &spp->s_v6;
5461 
5462 	if (is_system_labeled())
5463 		tsl = ixa->ixa_tsl;
5464 	else
5465 		tsl = NULL;
5466 
5467 	if (ap == NULL)
5468 		ap = pp->ipsp_act;
5469 	ASSERT(ap != NULL);
5470 
5471 	if (ap->ipa_act.ipa_apply.ipp_use_unique || tunnel_mode)
5472 		unique_id = SA_FORM_UNIQUE_ID(ixa);
5473 
5474 	/*
5475 	 * Set up an ACQUIRE record.
5476 	 *
5477 	 * Immediately, make sure the ACQUIRE sequence number doesn't slip
5478 	 * below the lowest point allowed in the kernel.  (In other words,
5479 	 * make sure the high bit on the sequence number is set.)
5480 	 */
5481 
5482 	seq = keysock_next_seq(ns) | IACQF_LOWEST_SEQ;
5483 
5484 	if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
5485 		src = (uint32_t *)&ipha->ipha_src;
5486 		dst = (uint32_t *)&ipha->ipha_dst;
5487 		af = AF_INET;
5488 		hashoffset = OUTBOUND_HASH_V4(sp, ipha->ipha_dst);
5489 		ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
5490 	} else {
5491 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
5492 		src = (uint32_t *)&ip6h->ip6_src;
5493 		dst = (uint32_t *)&ip6h->ip6_dst;
5494 		af = AF_INET6;
5495 		hashoffset = OUTBOUND_HASH_V6(sp, ip6h->ip6_dst);
5496 		ASSERT(!(ixa->ixa_flags & IXAF_IS_IPV4));
5497 	}
5498 
5499 	if (tunnel_mode) {
5500 		if (pp == NULL) {
5501 			/*
5502 			 * Tunnel mode with no policy pointer means this is a
5503 			 * reflected ICMP (like a ECHO REQUEST) that came in
5504 			 * with self-encapsulated protection.  Until we better
5505 			 * support this, drop the packet.
5506 			 */
5507 			ip_drop_packet(datamp, B_FALSE, NULL,
5508 			    DROPPER(ipss, ipds_spd_got_selfencap),
5509 			    &ipss->ipsec_spd_dropper);
5510 			return;
5511 		}
5512 		/* Snag inner addresses. */
5513 		isrc = ixa->ixa_ipsec_insrc;
5514 		idst = ixa->ixa_ipsec_indst;
5515 	} else {
5516 		isrc = idst = NULL;
5517 	}
5518 
5519 	/*
5520 	 * Check buckets to see if there is an existing entry.  If so,
5521 	 * grab it.  sadb_checkacquire locks newbie if found.
5522 	 */
5523 	bucket = &(sp->sdb_acq[hashoffset]);
5524 	mutex_enter(&bucket->iacqf_lock);
5525 	newbie = sadb_checkacquire(bucket, ap, pp, src, dst, isrc, idst,
5526 	    unique_id, tsl);
5527 
5528 	if (newbie == NULL) {
5529 		/*
5530 		 * Otherwise, allocate a new one.
5531 		 */
5532 		newbie = kmem_zalloc(sizeof (*newbie), KM_NOSLEEP);
5533 		if (newbie == NULL) {
5534 			mutex_exit(&bucket->iacqf_lock);
5535 			ip_drop_packet(datamp, B_FALSE, NULL,
5536 			    DROPPER(ipss, ipds_sadb_acquire_nomem),
5537 			    &ipss->ipsec_sadb_dropper);
5538 			return;
5539 		}
5540 		newbie->ipsacq_policy = pp;
5541 		if (pp != NULL) {
5542 			IPPOL_REFHOLD(pp);
5543 		}
5544 		IPACT_REFHOLD(ap);
5545 		newbie->ipsacq_act = ap;
5546 		newbie->ipsacq_linklock = &bucket->iacqf_lock;
5547 		newbie->ipsacq_next = bucket->iacqf_ipsacq;
5548 		newbie->ipsacq_ptpn = &bucket->iacqf_ipsacq;
5549 		if (newbie->ipsacq_next != NULL)
5550 			newbie->ipsacq_next->ipsacq_ptpn = &newbie->ipsacq_next;
5551 
5552 		bucket->iacqf_ipsacq = newbie;
5553 		mutex_init(&newbie->ipsacq_lock, NULL, MUTEX_DEFAULT, NULL);
5554 		mutex_enter(&newbie->ipsacq_lock);
5555 	}
5556 
5557 	/*
5558 	 * XXX MLS does it actually help us to drop the bucket lock here?
5559 	 * we have inserted a half-built, locked acquire record into the
5560 	 * bucket.  any competing thread will now be able to lock the bucket
5561 	 * to scan it, but will immediately pile up on the new acquire
5562 	 * record's lock; I don't think we gain anything here other than to
5563 	 * disperse blame for lock contention.
5564 	 *
5565 	 * we might be able to dispense with acquire record locks entirely..
5566 	 * just use the bucket locks..
5567 	 */
5568 
5569 	mutex_exit(&bucket->iacqf_lock);
5570 
5571 	/*
5572 	 * This assert looks silly for now, but we may need to enter newbie's
5573 	 * mutex during a search.
5574 	 */
5575 	ASSERT(MUTEX_HELD(&newbie->ipsacq_lock));
5576 
5577 	/*
5578 	 * Make the ip_xmit_attr_t into something we can queue.
5579 	 * If no memory it frees datamp.
5580 	 */
5581 	asyncmp = ip_xmit_attr_to_mblk(ixa);
5582 	if (asyncmp != NULL)
5583 		linkb(asyncmp, datamp);
5584 
5585 	/* Queue up packet.  Use b_next. */
5586 
5587 	if (asyncmp == NULL) {
5588 		/* Statistics for allocation failure */
5589 		if (ixa->ixa_flags & IXAF_IS_IPV4) {
5590 			BUMP_MIB(&ixa->ixa_ipst->ips_ip_mib,
5591 			    ipIfStatsOutDiscards);
5592 		} else {
5593 			BUMP_MIB(&ixa->ixa_ipst->ips_ip6_mib,
5594 			    ipIfStatsOutDiscards);
5595 		}
5596 		ip_drop_output("No memory for asyncmp", datamp, NULL);
5597 		freemsg(datamp);
5598 		/*
5599 		 * The acquire record will be freed quickly if it's new
5600 		 * (ipsacq_expire == 0), and will proceed as if no packet
5601 		 * showed up if not.
5602 		 */
5603 		mutex_exit(&newbie->ipsacq_lock);
5604 		return;
5605 	} else if (newbie->ipsacq_numpackets == 0) {
5606 		/* First one. */
5607 		newbie->ipsacq_mp = asyncmp;
5608 		newbie->ipsacq_numpackets = 1;
5609 		newbie->ipsacq_expire = gethrestime_sec();
5610 		/*
5611 		 * Extended ACQUIRE with both AH+ESP will use ESP's timeout
5612 		 * value.
5613 		 */
5614 		newbie->ipsacq_expire += *spp->s_acquire_timeout;
5615 		newbie->ipsacq_seq = seq;
5616 		newbie->ipsacq_addrfam = af;
5617 
5618 		newbie->ipsacq_srcport = ixa->ixa_ipsec_src_port;
5619 		newbie->ipsacq_dstport = ixa->ixa_ipsec_dst_port;
5620 		newbie->ipsacq_icmp_type = ixa->ixa_ipsec_icmp_type;
5621 		newbie->ipsacq_icmp_code = ixa->ixa_ipsec_icmp_code;
5622 		if (tunnel_mode) {
5623 			newbie->ipsacq_inneraddrfam = ixa->ixa_ipsec_inaf;
5624 			newbie->ipsacq_proto = ixa->ixa_ipsec_inaf == AF_INET6 ?
5625 			    IPPROTO_IPV6 : IPPROTO_ENCAP;
5626 			newbie->ipsacq_innersrcpfx = ixa->ixa_ipsec_insrcpfx;
5627 			newbie->ipsacq_innerdstpfx = ixa->ixa_ipsec_indstpfx;
5628 			IPSA_COPY_ADDR(newbie->ipsacq_innersrc,
5629 			    ixa->ixa_ipsec_insrc, ixa->ixa_ipsec_inaf);
5630 			IPSA_COPY_ADDR(newbie->ipsacq_innerdst,
5631 			    ixa->ixa_ipsec_indst, ixa->ixa_ipsec_inaf);
5632 		} else {
5633 			newbie->ipsacq_proto = ixa->ixa_ipsec_proto;
5634 		}
5635 		newbie->ipsacq_unique_id = unique_id;
5636 
5637 		if (tsl != NULL) {
5638 			label_hold(tsl);
5639 			newbie->ipsacq_tsl = tsl;
5640 		}
5641 	} else {
5642 		/* Scan to the end of the list & insert. */
5643 		mblk_t *lastone = newbie->ipsacq_mp;
5644 
5645 		while (lastone->b_next != NULL)
5646 			lastone = lastone->b_next;
5647 		lastone->b_next = asyncmp;
5648 		if (newbie->ipsacq_numpackets++ == ipsacq_maxpackets) {
5649 			newbie->ipsacq_numpackets = ipsacq_maxpackets;
5650 			lastone = newbie->ipsacq_mp;
5651 			newbie->ipsacq_mp = lastone->b_next;
5652 			lastone->b_next = NULL;
5653 
5654 			/* Freeing the async message */
5655 			lastone = ip_xmit_attr_free_mblk(lastone);
5656 			ip_drop_packet(lastone, B_FALSE, NULL,
5657 			    DROPPER(ipss, ipds_sadb_acquire_toofull),
5658 			    &ipss->ipsec_sadb_dropper);
5659 		} else {
5660 			IP_ACQUIRE_STAT(ipss, qhiwater,
5661 			    newbie->ipsacq_numpackets);
5662 		}
5663 	}
5664 
5665 	/*
5666 	 * Reset addresses.  Set them to the most recently added mblk chain,
5667 	 * so that the address pointers in the acquire record will point
5668 	 * at an mblk still attached to the acquire list.
5669 	 */
5670 
5671 	newbie->ipsacq_srcaddr = src;
5672 	newbie->ipsacq_dstaddr = dst;
5673 
5674 	/*
5675 	 * If the acquire record has more than one queued packet, we've
5676 	 * already sent an ACQUIRE, and don't need to repeat ourself.
5677 	 */
5678 	if (newbie->ipsacq_seq != seq || newbie->ipsacq_numpackets > 1) {
5679 		/* I have an acquire outstanding already! */
5680 		mutex_exit(&newbie->ipsacq_lock);
5681 		return;
5682 	}
5683 
5684 	if (need_esp) {
5685 		ESP_BUMP_STAT(espstack, acquire_requests);
5686 		q = espstack->esp_pfkey_q;
5687 	} else {
5688 		/*
5689 		 * Two cases get us here:
5690 		 * 1.) AH-only policy.
5691 		 *
5692 		 * 2.) A continuation of an AH+ESP policy, and this is the
5693 		 * post-ESP, AH-needs-to-send-a-regular-ACQUIRE case.
5694 		 * (i.e. called from esp_do_outbound_ah().)
5695 		 */
5696 		AH_BUMP_STAT(ahstack, acquire_requests);
5697 		q = ahstack->ah_pfkey_q;
5698 	}
5699 
5700 	/*
5701 	 * Get selectors and other policy-expression bits needed for an
5702 	 * ACQUIRE.
5703 	 */
5704 	bzero(&sel, sizeof (sel));
5705 	sel.ips_isv4 = (ixa->ixa_flags & IXAF_IS_IPV4) != 0;
5706 	if (tunnel_mode) {
5707 		sel.ips_protocol = (ixa->ixa_ipsec_inaf == AF_INET) ?
5708 		    IPPROTO_ENCAP : IPPROTO_IPV6;
5709 	} else {
5710 		sel.ips_protocol = ixa->ixa_ipsec_proto;
5711 		sel.ips_local_port = ixa->ixa_ipsec_src_port;
5712 		sel.ips_remote_port = ixa->ixa_ipsec_dst_port;
5713 	}
5714 	sel.ips_icmp_type = ixa->ixa_ipsec_icmp_type;
5715 	sel.ips_icmp_code = ixa->ixa_ipsec_icmp_code;
5716 	sel.ips_is_icmp_inv_acq = 0;
5717 	if (af == AF_INET) {
5718 		sel.ips_local_addr_v4 = ipha->ipha_src;
5719 		sel.ips_remote_addr_v4 = ipha->ipha_dst;
5720 	} else {
5721 		sel.ips_local_addr_v6 = ip6h->ip6_src;
5722 		sel.ips_remote_addr_v6 = ip6h->ip6_dst;
5723 	}
5724 
5725 
5726 	/*
5727 	 * 1. Generate addresses, kmc, and sensitivity.  These are "common"
5728 	 * and should be an mblk pointed to by common. TBD -- eventually it
5729 	 * will include triggering packet contents as more address extensions.
5730 	 *
5731 	 * 2. Generate ACQUIRE & KEYSOCK_OUT and single-protocol proposal.
5732 	 * These are "regular" and "prop".  String regular->b_cont->b_cont =
5733 	 * common, common->b_cont = prop.
5734 	 *
5735 	 * 3. If extended register got turned on, generate EXT_ACQUIRE &
5736 	 * KEYSOCK_OUT and multi-protocol eprop. These are "extended" and
5737 	 * "eprop".  String extended->b_cont->b_cont = dupb(common) and
5738 	 * extended->b_cont->b_cont->b_cont = prop.
5739 	 *
5740 	 * 4. Deliver:  putnext(q, regular) and if there, putnext(q, extended).
5741 	 */
5742 
5743 	regular = extended = prop = eprop = NULL;
5744 
5745 	common = sadb_acquire_msg_common(&sel, pp, ap, tunnel_mode, tsl, NULL);
5746 	if (common == NULL)
5747 		goto bail;
5748 
5749 	regular = sadb_acquire_msg_base(0, (need_esp ?
5750 	    SADB_SATYPE_ESP : SADB_SATYPE_AH), newbie->ipsacq_seq, 0);
5751 	if (regular == NULL)
5752 		goto bail;
5753 
5754 	/*
5755 	 * Pardon the boolean cleverness. At least one of need_* must be true.
5756 	 * If they are equal, it's an AH & ESP policy and ESP needs to go
5757 	 * first.  If they aren't, just check the contents of need_esp.
5758 	 */
5759 	prop = sadb_acquire_prop(ap, ns, need_esp);
5760 	if (prop == NULL)
5761 		goto bail;
5762 
5763 	/* Link the parts together. */
5764 	regular->b_cont->b_cont = common;
5765 	common->b_cont = prop;
5766 	/*
5767 	 * Prop is now linked, so don't freemsg() it if the extended
5768 	 * construction goes off the rails.
5769 	 */
5770 	prop = NULL;
5771 
5772 	((sadb_msg_t *)(regular->b_cont->b_rptr))->sadb_msg_len =
5773 	    SADB_8TO64(msgsize(regular->b_cont));
5774 
5775 	/*
5776 	 * If we need an extended ACQUIRE, build it here.
5777 	 */
5778 	if (keysock_extended_reg(ns)) {
5779 		/* NOTE: "common" still points to what we need. */
5780 		extended = sadb_acquire_msg_base(0, 0, newbie->ipsacq_seq, 0);
5781 		if (extended == NULL) {
5782 			common = NULL;
5783 			goto bail;
5784 		}
5785 
5786 		extended->b_cont->b_cont = dupb(common);
5787 		common = NULL;
5788 		if (extended->b_cont->b_cont == NULL)
5789 			goto bail;
5790 
5791 		eprop = sadb_acquire_extended_prop(ap, ns);
5792 		if (eprop == NULL)
5793 			goto bail;
5794 		extended->b_cont->b_cont->b_cont = eprop;
5795 
5796 		((sadb_msg_t *)(extended->b_cont->b_rptr))->sadb_msg_len =
5797 		    SADB_8TO64(msgsize(extended->b_cont));
5798 	}
5799 
5800 	/* So we don't hold a lock across putnext()... */
5801 	mutex_exit(&newbie->ipsacq_lock);
5802 
5803 	if (extended != NULL)
5804 		putnext(q, extended);
5805 	ASSERT(regular != NULL);
5806 	putnext(q, regular);
5807 	return;
5808 
5809 bail:
5810 	/* Make this acquire record go away quickly... */
5811 	newbie->ipsacq_expire = 0;
5812 	/* Exploit freemsg(NULL) being legal for fun & profit. */
5813 	freemsg(common);
5814 	freemsg(prop);
5815 	freemsg(extended);
5816 	freemsg(regular);
5817 	mutex_exit(&newbie->ipsacq_lock);
5818 }
5819 
5820 /*
5821  * Unlink and free an acquire record.
5822  */
5823 void
5824 sadb_destroy_acquire(ipsacq_t *acqrec, netstack_t *ns)
5825 {
5826 	mblk_t		*mp;
5827 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
5828 
5829 	ASSERT(MUTEX_HELD(acqrec->ipsacq_linklock));
5830 
5831 	if (acqrec->ipsacq_policy != NULL) {
5832 		IPPOL_REFRELE(acqrec->ipsacq_policy);
5833 	}
5834 	if (acqrec->ipsacq_act != NULL) {
5835 		IPACT_REFRELE(acqrec->ipsacq_act);
5836 	}
5837 
5838 	/* Unlink */
5839 	*(acqrec->ipsacq_ptpn) = acqrec->ipsacq_next;
5840 	if (acqrec->ipsacq_next != NULL)
5841 		acqrec->ipsacq_next->ipsacq_ptpn = acqrec->ipsacq_ptpn;
5842 
5843 	if (acqrec->ipsacq_tsl != NULL) {
5844 		label_rele(acqrec->ipsacq_tsl);
5845 		acqrec->ipsacq_tsl = NULL;
5846 	}
5847 
5848 	/*
5849 	 * Free hanging mp's.
5850 	 *
5851 	 * XXX Instead of freemsg(), perhaps use IPSEC_REQ_FAILED.
5852 	 */
5853 
5854 	mutex_enter(&acqrec->ipsacq_lock);
5855 	while (acqrec->ipsacq_mp != NULL) {
5856 		mp = acqrec->ipsacq_mp;
5857 		acqrec->ipsacq_mp = mp->b_next;
5858 		mp->b_next = NULL;
5859 		/* Freeing the async message */
5860 		mp = ip_xmit_attr_free_mblk(mp);
5861 		ip_drop_packet(mp, B_FALSE, NULL,
5862 		    DROPPER(ipss, ipds_sadb_acquire_timeout),
5863 		    &ipss->ipsec_sadb_dropper);
5864 	}
5865 	mutex_exit(&acqrec->ipsacq_lock);
5866 
5867 	/* Free */
5868 	mutex_destroy(&acqrec->ipsacq_lock);
5869 	kmem_free(acqrec, sizeof (*acqrec));
5870 }
5871 
5872 /*
5873  * Destroy an acquire list fanout.
5874  */
5875 static void
5876 sadb_destroy_acqlist(iacqf_t **listp, uint_t numentries, boolean_t forever,
5877     netstack_t *ns)
5878 {
5879 	int i;
5880 	iacqf_t *list = *listp;
5881 
5882 	if (list == NULL)
5883 		return;
5884 
5885 	for (i = 0; i < numentries; i++) {
5886 		mutex_enter(&(list[i].iacqf_lock));
5887 		while (list[i].iacqf_ipsacq != NULL)
5888 			sadb_destroy_acquire(list[i].iacqf_ipsacq, ns);
5889 		mutex_exit(&(list[i].iacqf_lock));
5890 		if (forever)
5891 			mutex_destroy(&(list[i].iacqf_lock));
5892 	}
5893 
5894 	if (forever) {
5895 		*listp = NULL;
5896 		kmem_free(list, numentries * sizeof (*list));
5897 	}
5898 }
5899 
5900 /*
5901  * Create an algorithm descriptor for an extended ACQUIRE.  Filter crypto
5902  * framework's view of reality vs. IPsec's.  EF's wins, BTW.
5903  */
5904 static uint8_t *
5905 sadb_new_algdesc(uint8_t *start, uint8_t *limit,
5906     sadb_x_ecomb_t *ecomb, uint8_t satype, uint8_t algtype,
5907     uint8_t alg, uint16_t minbits, uint16_t maxbits, ipsec_stack_t *ipss)
5908 {
5909 	uint8_t *cur = start;
5910 	ipsec_alginfo_t *algp;
5911 	sadb_x_algdesc_t *algdesc = (sadb_x_algdesc_t *)cur;
5912 
5913 	cur += sizeof (*algdesc);
5914 	if (cur >= limit)
5915 		return (NULL);
5916 
5917 	ecomb->sadb_x_ecomb_numalgs++;
5918 
5919 	/*
5920 	 * Normalize vs. crypto framework's limits.  This way, you can specify
5921 	 * a stronger policy, and when the framework loads a stronger version,
5922 	 * you can just keep plowing w/o rewhacking your SPD.
5923 	 */
5924 	rw_enter(&ipss->ipsec_alg_lock, RW_READER);
5925 	algp = ipss->ipsec_alglists[(algtype == SADB_X_ALGTYPE_AUTH) ?
5926 	    IPSEC_ALG_AUTH : IPSEC_ALG_ENCR][alg];
5927 	if (algp == NULL) {
5928 		rw_exit(&ipss->ipsec_alg_lock);
5929 		return (NULL);	/* Algorithm doesn't exist.  Fail gracefully. */
5930 	}
5931 	if (minbits < algp->alg_ef_minbits)
5932 		minbits = algp->alg_ef_minbits;
5933 	if (maxbits > algp->alg_ef_maxbits)
5934 		maxbits = algp->alg_ef_maxbits;
5935 	rw_exit(&ipss->ipsec_alg_lock);
5936 
5937 	algdesc->sadb_x_algdesc_saltbits = SADB_8TO1(algp->alg_saltlen);
5938 	algdesc->sadb_x_algdesc_satype = satype;
5939 	algdesc->sadb_x_algdesc_algtype = algtype;
5940 	algdesc->sadb_x_algdesc_alg = alg;
5941 	algdesc->sadb_x_algdesc_minbits = minbits;
5942 	algdesc->sadb_x_algdesc_maxbits = maxbits;
5943 
5944 	return (cur);
5945 }
5946 
5947 /*
5948  * Convert the given ipsec_action_t into an ecomb starting at *ecomb
5949  * which must fit before *limit
5950  *
5951  * return NULL if we ran out of room or a pointer to the end of the ecomb.
5952  */
5953 static uint8_t *
5954 sadb_action_to_ecomb(uint8_t *start, uint8_t *limit, ipsec_action_t *act,
5955     netstack_t *ns)
5956 {
5957 	uint8_t *cur = start;
5958 	sadb_x_ecomb_t *ecomb = (sadb_x_ecomb_t *)cur;
5959 	ipsec_prot_t *ipp;
5960 	ipsec_stack_t *ipss = ns->netstack_ipsec;
5961 
5962 	cur += sizeof (*ecomb);
5963 	if (cur >= limit)
5964 		return (NULL);
5965 
5966 	ASSERT(act->ipa_act.ipa_type == IPSEC_ACT_APPLY);
5967 
5968 	ipp = &act->ipa_act.ipa_apply;
5969 
5970 	ecomb->sadb_x_ecomb_numalgs = 0;
5971 	ecomb->sadb_x_ecomb_reserved = 0;
5972 	ecomb->sadb_x_ecomb_reserved2 = 0;
5973 	/*
5974 	 * No limits on allocations, since we really don't support that
5975 	 * concept currently.
5976 	 */
5977 	ecomb->sadb_x_ecomb_soft_allocations = 0;
5978 	ecomb->sadb_x_ecomb_hard_allocations = 0;
5979 
5980 	/*
5981 	 * XXX TBD: Policy or global parameters will eventually be
5982 	 * able to fill in some of these.
5983 	 */
5984 	ecomb->sadb_x_ecomb_flags = 0;
5985 	ecomb->sadb_x_ecomb_soft_bytes = 0;
5986 	ecomb->sadb_x_ecomb_hard_bytes = 0;
5987 	ecomb->sadb_x_ecomb_soft_addtime = 0;
5988 	ecomb->sadb_x_ecomb_hard_addtime = 0;
5989 	ecomb->sadb_x_ecomb_soft_usetime = 0;
5990 	ecomb->sadb_x_ecomb_hard_usetime = 0;
5991 
5992 	if (ipp->ipp_use_ah) {
5993 		cur = sadb_new_algdesc(cur, limit, ecomb,
5994 		    SADB_SATYPE_AH, SADB_X_ALGTYPE_AUTH, ipp->ipp_auth_alg,
5995 		    ipp->ipp_ah_minbits, ipp->ipp_ah_maxbits, ipss);
5996 		if (cur == NULL)
5997 			return (NULL);
5998 		ipsecah_fill_defs(ecomb, ns);
5999 	}
6000 
6001 	if (ipp->ipp_use_esp) {
6002 		if (ipp->ipp_use_espa) {
6003 			cur = sadb_new_algdesc(cur, limit, ecomb,
6004 			    SADB_SATYPE_ESP, SADB_X_ALGTYPE_AUTH,
6005 			    ipp->ipp_esp_auth_alg,
6006 			    ipp->ipp_espa_minbits,
6007 			    ipp->ipp_espa_maxbits, ipss);
6008 			if (cur == NULL)
6009 				return (NULL);
6010 		}
6011 
6012 		cur = sadb_new_algdesc(cur, limit, ecomb,
6013 		    SADB_SATYPE_ESP, SADB_X_ALGTYPE_CRYPT,
6014 		    ipp->ipp_encr_alg,
6015 		    ipp->ipp_espe_minbits,
6016 		    ipp->ipp_espe_maxbits, ipss);
6017 		if (cur == NULL)
6018 			return (NULL);
6019 		/* Fill in lifetimes if and only if AH didn't already... */
6020 		if (!ipp->ipp_use_ah)
6021 			ipsecesp_fill_defs(ecomb, ns);
6022 	}
6023 
6024 	return (cur);
6025 }
6026 
6027 #include <sys/tsol/label_macro.h> /* XXX should not need this */
6028 
6029 /*
6030  * From a cred_t, construct a sensitivity label extension
6031  *
6032  * We send up a fixed-size sensitivity label bitmap, and are perhaps
6033  * overly chummy with the underlying data structures here.
6034  */
6035 
6036 /* ARGSUSED */
6037 int
6038 sadb_sens_len_from_label(ts_label_t *tsl)
6039 {
6040 	int baselen = sizeof (sadb_sens_t) + _C_LEN * 4;
6041 	return (roundup(baselen, sizeof (uint64_t)));
6042 }
6043 
6044 void
6045 sadb_sens_from_label(sadb_sens_t *sens, int exttype, ts_label_t *tsl,
6046     int senslen)
6047 {
6048 	uint8_t *bitmap;
6049 	bslabel_t *sl;
6050 
6051 	/* LINTED */
6052 	ASSERT((_C_LEN & 1) == 0);
6053 	ASSERT((senslen & 7) == 0);
6054 
6055 	sl = label2bslabel(tsl);
6056 
6057 	sens->sadb_sens_exttype = exttype;
6058 	sens->sadb_sens_len = SADB_8TO64(senslen);
6059 
6060 	sens->sadb_sens_dpd = tsl->tsl_doi;
6061 	sens->sadb_sens_sens_level = LCLASS(sl);
6062 	sens->sadb_sens_integ_level = 0; /* TBD */
6063 	sens->sadb_sens_sens_len = _C_LEN >> 1;
6064 	sens->sadb_sens_integ_len = 0; /* TBD */
6065 	sens->sadb_x_sens_flags = 0;
6066 
6067 	bitmap = (uint8_t *)(sens + 1);
6068 	bcopy(&(((_bslabel_impl_t *)sl)->compartments), bitmap, _C_LEN * 4);
6069 }
6070 
6071 /*
6072  * Okay, how do we report errors/invalid labels from this?
6073  * With a special designated "not a label" cred_t ?
6074  */
6075 /* ARGSUSED */
6076 ts_label_t *
6077 sadb_label_from_sens(sadb_sens_t *sens, uint64_t *bitmap)
6078 {
6079 	int bitmap_len = SADB_64TO8(sens->sadb_sens_sens_len);
6080 	bslabel_t sl;
6081 	ts_label_t *tsl;
6082 
6083 	if (sens->sadb_sens_integ_level != 0)
6084 		return (NULL);
6085 	if (sens->sadb_sens_integ_len != 0)
6086 		return (NULL);
6087 	if (bitmap_len > _C_LEN * 4)
6088 		return (NULL);
6089 
6090 	bsllow(&sl);
6091 	LCLASS_SET((_bslabel_impl_t *)&sl,
6092 	    (uint16_t)sens->sadb_sens_sens_level);
6093 	bcopy(bitmap, &((_bslabel_impl_t *)&sl)->compartments,
6094 	    bitmap_len);
6095 
6096 	tsl = labelalloc(&sl, sens->sadb_sens_dpd, KM_NOSLEEP);
6097 	if (tsl == NULL)
6098 		return (NULL);
6099 
6100 	if (sens->sadb_x_sens_flags & SADB_X_SENS_UNLABELED)
6101 		tsl->tsl_flags |= TSLF_UNLABELED;
6102 	return (tsl);
6103 }
6104 
6105 /* End XXX label-library-leakage */
6106 
6107 /*
6108  * Given an SADB_GETSPI message, find an appropriately ranged SA and
6109  * allocate an SA.  If there are message improprieties, return (ipsa_t *)-1.
6110  * If there was a memory allocation error, return NULL.	 (Assume NULL !=
6111  * (ipsa_t *)-1).
6112  *
6113  * master_spi is passed in host order.
6114  */
6115 ipsa_t *
6116 sadb_getspi(keysock_in_t *ksi, uint32_t master_spi, int *diagnostic,
6117     netstack_t *ns, uint_t sa_type)
6118 {
6119 	sadb_address_t *src =
6120 	    (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC],
6121 	    *dst = (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
6122 	sadb_spirange_t *range =
6123 	    (sadb_spirange_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
6124 	struct sockaddr_in *ssa, *dsa;
6125 	struct sockaddr_in6 *ssa6, *dsa6;
6126 	uint32_t *srcaddr, *dstaddr;
6127 	sa_family_t af;
6128 	uint32_t add, min, max;
6129 	uint8_t protocol =
6130 	    (sa_type == SADB_SATYPE_AH) ? IPPROTO_AH : IPPROTO_ESP;
6131 
6132 	if (src == NULL) {
6133 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
6134 		return ((ipsa_t *)-1);
6135 	}
6136 	if (dst == NULL) {
6137 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
6138 		return ((ipsa_t *)-1);
6139 	}
6140 	if (range == NULL) {
6141 		*diagnostic = SADB_X_DIAGNOSTIC_MISSING_RANGE;
6142 		return ((ipsa_t *)-1);
6143 	}
6144 
6145 	min = ntohl(range->sadb_spirange_min);
6146 	max = ntohl(range->sadb_spirange_max);
6147 	dsa = (struct sockaddr_in *)(dst + 1);
6148 	dsa6 = (struct sockaddr_in6 *)dsa;
6149 
6150 	ssa = (struct sockaddr_in *)(src + 1);
6151 	ssa6 = (struct sockaddr_in6 *)ssa;
6152 	ASSERT(dsa->sin_family == ssa->sin_family);
6153 
6154 	srcaddr = ALL_ZEROES_PTR;
6155 	af = dsa->sin_family;
6156 	switch (af) {
6157 	case AF_INET:
6158 		if (src != NULL)
6159 			srcaddr = (uint32_t *)(&ssa->sin_addr);
6160 		dstaddr = (uint32_t *)(&dsa->sin_addr);
6161 		break;
6162 	case AF_INET6:
6163 		if (src != NULL)
6164 			srcaddr = (uint32_t *)(&ssa6->sin6_addr);
6165 		dstaddr = (uint32_t *)(&dsa6->sin6_addr);
6166 		break;
6167 	default:
6168 		*diagnostic = SADB_X_DIAGNOSTIC_BAD_DST_AF;
6169 		return ((ipsa_t *)-1);
6170 	}
6171 
6172 	if (master_spi < min || master_spi > max) {
6173 		/* Return a random value in the range. */
6174 		if (cl_inet_getspi) {
6175 			cl_inet_getspi(ns->netstack_stackid, protocol,
6176 			    (uint8_t *)&add, sizeof (add), NULL);
6177 		} else {
6178 			(void) random_get_pseudo_bytes((uint8_t *)&add,
6179 			    sizeof (add));
6180 		}
6181 		master_spi = min + (add % (max - min + 1));
6182 	}
6183 
6184 	/*
6185 	 * Since master_spi is passed in host order, we need to htonl() it
6186 	 * for the purposes of creating a new SA.
6187 	 */
6188 	return (sadb_makelarvalassoc(htonl(master_spi), srcaddr, dstaddr, af,
6189 	    ns));
6190 }
6191 
6192 /*
6193  *
6194  * Locate an ACQUIRE and nuke it.  If I have an samsg that's larger than the
6195  * base header, just ignore it.	 Otherwise, lock down the whole ACQUIRE list
6196  * and scan for the sequence number in question.  I may wish to accept an
6197  * address pair with it, for easier searching.
6198  *
6199  * Caller frees the message, so we don't have to here.
6200  *
6201  * NOTE:	The pfkey_q parameter may be used in the future for ACQUIRE
6202  *		failures.
6203  */
6204 /* ARGSUSED */
6205 void
6206 sadb_in_acquire(sadb_msg_t *samsg, sadbp_t *sp, queue_t *pfkey_q,
6207     netstack_t *ns)
6208 {
6209 	int i;
6210 	ipsacq_t *acqrec;
6211 	iacqf_t *bucket;
6212 
6213 	/*
6214 	 * I only accept the base header for this!
6215 	 * Though to be honest, requiring the dst address would help
6216 	 * immensely.
6217 	 *
6218 	 * XXX	There are already cases where I can get the dst address.
6219 	 */
6220 	if (samsg->sadb_msg_len > SADB_8TO64(sizeof (*samsg)))
6221 		return;
6222 
6223 	/*
6224 	 * Using the samsg->sadb_msg_seq, find the ACQUIRE record, delete it,
6225 	 * (and in the future send a message to IP with the appropriate error
6226 	 * number).
6227 	 *
6228 	 * Q: Do I want to reject if pid != 0?
6229 	 */
6230 
6231 	for (i = 0; i < sp->s_v4.sdb_hashsize; i++) {
6232 		bucket = &sp->s_v4.sdb_acq[i];
6233 		mutex_enter(&bucket->iacqf_lock);
6234 		for (acqrec = bucket->iacqf_ipsacq; acqrec != NULL;
6235 		    acqrec = acqrec->ipsacq_next) {
6236 			if (samsg->sadb_msg_seq == acqrec->ipsacq_seq)
6237 				break;	/* for acqrec... loop. */
6238 		}
6239 		if (acqrec != NULL)
6240 			break;	/* for i = 0... loop. */
6241 
6242 		mutex_exit(&bucket->iacqf_lock);
6243 	}
6244 
6245 	if (acqrec == NULL) {
6246 		for (i = 0; i < sp->s_v6.sdb_hashsize; i++) {
6247 			bucket = &sp->s_v6.sdb_acq[i];
6248 			mutex_enter(&bucket->iacqf_lock);
6249 			for (acqrec = bucket->iacqf_ipsacq; acqrec != NULL;
6250 			    acqrec = acqrec->ipsacq_next) {
6251 				if (samsg->sadb_msg_seq == acqrec->ipsacq_seq)
6252 					break;	/* for acqrec... loop. */
6253 			}
6254 			if (acqrec != NULL)
6255 				break;	/* for i = 0... loop. */
6256 
6257 			mutex_exit(&bucket->iacqf_lock);
6258 		}
6259 	}
6260 
6261 
6262 	if (acqrec == NULL)
6263 		return;
6264 
6265 	/*
6266 	 * What do I do with the errno and IP?	I may need mp's services a
6267 	 * little more.	 See sadb_destroy_acquire() for future directions
6268 	 * beyond free the mblk chain on the acquire record.
6269 	 */
6270 
6271 	ASSERT(&bucket->iacqf_lock == acqrec->ipsacq_linklock);
6272 	sadb_destroy_acquire(acqrec, ns);
6273 	/* Have to exit mutex here, because of breaking out of for loop. */
6274 	mutex_exit(&bucket->iacqf_lock);
6275 }
6276 
6277 /*
6278  * The following functions work with the replay windows of an SA.  They assume
6279  * the ipsa->ipsa_replay_arr is an array of uint64_t, and that the bit vector
6280  * represents the highest sequence number packet received, and back
6281  * (ipsa->ipsa_replay_wsize) packets.
6282  */
6283 
6284 /*
6285  * Is the replay bit set?
6286  */
6287 static boolean_t
6288 ipsa_is_replay_set(ipsa_t *ipsa, uint32_t offset)
6289 {
6290 	uint64_t bit = (uint64_t)1 << (uint64_t)(offset & 63);
6291 
6292 	return ((bit & ipsa->ipsa_replay_arr[offset >> 6]) ? B_TRUE : B_FALSE);
6293 }
6294 
6295 /*
6296  * Shift the bits of the replay window over.
6297  */
6298 static void
6299 ipsa_shift_replay(ipsa_t *ipsa, uint32_t shift)
6300 {
6301 	int i;
6302 	int jump = ((shift - 1) >> 6) + 1;
6303 
6304 	if (shift == 0)
6305 		return;
6306 
6307 	for (i = (ipsa->ipsa_replay_wsize - 1) >> 6; i >= 0; i--) {
6308 		if (i + jump <= (ipsa->ipsa_replay_wsize - 1) >> 6) {
6309 			ipsa->ipsa_replay_arr[i + jump] |=
6310 			    ipsa->ipsa_replay_arr[i] >> (64 - (shift & 63));
6311 		}
6312 		ipsa->ipsa_replay_arr[i] <<= shift;
6313 	}
6314 }
6315 
6316 /*
6317  * Set a bit in the bit vector.
6318  */
6319 static void
6320 ipsa_set_replay(ipsa_t *ipsa, uint32_t offset)
6321 {
6322 	uint64_t bit = (uint64_t)1 << (uint64_t)(offset & 63);
6323 
6324 	ipsa->ipsa_replay_arr[offset >> 6] |= bit;
6325 }
6326 
6327 #define	SADB_MAX_REPLAY_VALUE 0xffffffff
6328 
6329 /*
6330  * Assume caller has NOT done ntohl() already on seq.  Check to see
6331  * if replay sequence number "seq" has been seen already.
6332  */
6333 boolean_t
6334 sadb_replay_check(ipsa_t *ipsa, uint32_t seq)
6335 {
6336 	boolean_t rc;
6337 	uint32_t diff;
6338 
6339 	if (ipsa->ipsa_replay_wsize == 0)
6340 		return (B_TRUE);
6341 
6342 	/*
6343 	 * NOTE:  I've already checked for 0 on the wire in sadb_replay_peek().
6344 	 */
6345 
6346 	/* Convert sequence number into host order before holding the mutex. */
6347 	seq = ntohl(seq);
6348 
6349 	mutex_enter(&ipsa->ipsa_lock);
6350 
6351 	/* Initialize inbound SA's ipsa_replay field to last one received. */
6352 	if (ipsa->ipsa_replay == 0)
6353 		ipsa->ipsa_replay = 1;
6354 
6355 	if (seq > ipsa->ipsa_replay) {
6356 		/*
6357 		 * I have received a new "highest value received".  Shift
6358 		 * the replay window over.
6359 		 */
6360 		diff = seq - ipsa->ipsa_replay;
6361 		if (diff < ipsa->ipsa_replay_wsize) {
6362 			/* In replay window, shift bits over. */
6363 			ipsa_shift_replay(ipsa, diff);
6364 		} else {
6365 			/* WAY FAR AHEAD, clear bits and start again. */
6366 			bzero(ipsa->ipsa_replay_arr,
6367 			    sizeof (ipsa->ipsa_replay_arr));
6368 		}
6369 		ipsa_set_replay(ipsa, 0);
6370 		ipsa->ipsa_replay = seq;
6371 		rc = B_TRUE;
6372 		goto done;
6373 	}
6374 	diff = ipsa->ipsa_replay - seq;
6375 	if (diff >= ipsa->ipsa_replay_wsize || ipsa_is_replay_set(ipsa, diff)) {
6376 		rc = B_FALSE;
6377 		goto done;
6378 	}
6379 	/* Set this packet as seen. */
6380 	ipsa_set_replay(ipsa, diff);
6381 
6382 	rc = B_TRUE;
6383 done:
6384 	mutex_exit(&ipsa->ipsa_lock);
6385 	return (rc);
6386 }
6387 
6388 /*
6389  * "Peek" and see if we should even bother going through the effort of
6390  * running an authentication check on the sequence number passed in.
6391  * this takes into account packets that are below the replay window,
6392  * and collisions with already replayed packets.  Return B_TRUE if it
6393  * is okay to proceed, B_FALSE if this packet should be dropped immediately.
6394  * Assume same byte-ordering as sadb_replay_check.
6395  */
6396 boolean_t
6397 sadb_replay_peek(ipsa_t *ipsa, uint32_t seq)
6398 {
6399 	boolean_t rc = B_FALSE;
6400 	uint32_t diff;
6401 
6402 	if (ipsa->ipsa_replay_wsize == 0)
6403 		return (B_TRUE);
6404 
6405 	/*
6406 	 * 0 is 0, regardless of byte order... :)
6407 	 *
6408 	 * If I get 0 on the wire (and there is a replay window) then the
6409 	 * sender most likely wrapped.	This ipsa may need to be marked or
6410 	 * something.
6411 	 */
6412 	if (seq == 0)
6413 		return (B_FALSE);
6414 
6415 	seq = ntohl(seq);
6416 	mutex_enter(&ipsa->ipsa_lock);
6417 	if (seq < ipsa->ipsa_replay - ipsa->ipsa_replay_wsize &&
6418 	    ipsa->ipsa_replay >= ipsa->ipsa_replay_wsize)
6419 		goto done;
6420 
6421 	/*
6422 	 * If I've hit 0xffffffff, then quite honestly, I don't need to
6423 	 * bother with formalities.  I'm not accepting any more packets
6424 	 * on this SA.
6425 	 */
6426 	if (ipsa->ipsa_replay == SADB_MAX_REPLAY_VALUE) {
6427 		/*
6428 		 * Since we're already holding the lock, update the
6429 		 * expire time ala. sadb_replay_delete() and return.
6430 		 */
6431 		ipsa->ipsa_hardexpiretime = (time_t)1;
6432 		goto done;
6433 	}
6434 
6435 	if (seq <= ipsa->ipsa_replay) {
6436 		/*
6437 		 * This seq is in the replay window.  I'm not below it,
6438 		 * because I already checked for that above!
6439 		 */
6440 		diff = ipsa->ipsa_replay - seq;
6441 		if (ipsa_is_replay_set(ipsa, diff))
6442 			goto done;
6443 	}
6444 	/* Else return B_TRUE, I'm going to advance the window. */
6445 
6446 	rc = B_TRUE;
6447 done:
6448 	mutex_exit(&ipsa->ipsa_lock);
6449 	return (rc);
6450 }
6451 
6452 /*
6453  * Delete a single SA.
6454  *
6455  * For now, use the quick-and-dirty trick of making the association's
6456  * hard-expire lifetime (time_t)1, ensuring deletion by the *_ager().
6457  */
6458 void
6459 sadb_replay_delete(ipsa_t *assoc)
6460 {
6461 	mutex_enter(&assoc->ipsa_lock);
6462 	assoc->ipsa_hardexpiretime = (time_t)1;
6463 	mutex_exit(&assoc->ipsa_lock);
6464 }
6465 
6466 /*
6467  * Special front-end to ipsec_rl_strlog() dealing with SA failure.
6468  * this is designed to take only a format string with "* %x * %s *", so
6469  * that "spi" is printed first, then "addr" is converted using inet_pton().
6470  *
6471  * This is abstracted out to save the stack space for only when inet_pton()
6472  * is called.  Make sure "spi" is in network order; it usually is when this
6473  * would get called.
6474  */
6475 void
6476 ipsec_assocfailure(short mid, short sid, char level, ushort_t sl, char *fmt,
6477     uint32_t spi, void *addr, int af, netstack_t *ns)
6478 {
6479 	char buf[INET6_ADDRSTRLEN];
6480 
6481 	ASSERT(af == AF_INET6 || af == AF_INET);
6482 
6483 	ipsec_rl_strlog(ns, mid, sid, level, sl, fmt, ntohl(spi),
6484 	    inet_ntop(af, addr, buf, sizeof (buf)));
6485 }
6486 
6487 /*
6488  * Fills in a reference to the policy, if any, from the conn, in *ppp
6489  */
6490 static void
6491 ipsec_conn_pol(ipsec_selector_t *sel, conn_t *connp, ipsec_policy_t **ppp)
6492 {
6493 	ipsec_policy_t	*pp;
6494 	ipsec_latch_t	*ipl = connp->conn_latch;
6495 
6496 	if ((ipl != NULL) && (connp->conn_ixa->ixa_ipsec_policy != NULL)) {
6497 		pp = connp->conn_ixa->ixa_ipsec_policy;
6498 		IPPOL_REFHOLD(pp);
6499 	} else {
6500 		pp = ipsec_find_policy(IPSEC_TYPE_OUTBOUND, connp, sel,
6501 		    connp->conn_netstack);
6502 	}
6503 	*ppp = pp;
6504 }
6505 
6506 /*
6507  * The following functions scan through active conn_t structures
6508  * and return a reference to the best-matching policy it can find.
6509  * Caller must release the reference.
6510  */
6511 static void
6512 ipsec_udp_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp, ip_stack_t *ipst)
6513 {
6514 	connf_t *connfp;
6515 	conn_t *connp = NULL;
6516 	ipsec_selector_t portonly;
6517 
6518 	bzero((void *)&portonly, sizeof (portonly));
6519 
6520 	if (sel->ips_local_port == 0)
6521 		return;
6522 
6523 	connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(sel->ips_local_port,
6524 	    ipst)];
6525 	mutex_enter(&connfp->connf_lock);
6526 
6527 	if (sel->ips_isv4) {
6528 		connp = connfp->connf_head;
6529 		while (connp != NULL) {
6530 			if (IPCL_UDP_MATCH(connp, sel->ips_local_port,
6531 			    sel->ips_local_addr_v4, sel->ips_remote_port,
6532 			    sel->ips_remote_addr_v4))
6533 				break;
6534 			connp = connp->conn_next;
6535 		}
6536 
6537 		if (connp == NULL) {
6538 			/* Try port-only match in IPv6. */
6539 			portonly.ips_local_port = sel->ips_local_port;
6540 			sel = &portonly;
6541 		}
6542 	}
6543 
6544 	if (connp == NULL) {
6545 		connp = connfp->connf_head;
6546 		while (connp != NULL) {
6547 			if (IPCL_UDP_MATCH_V6(connp, sel->ips_local_port,
6548 			    sel->ips_local_addr_v6, sel->ips_remote_port,
6549 			    sel->ips_remote_addr_v6))
6550 				break;
6551 			connp = connp->conn_next;
6552 		}
6553 
6554 		if (connp == NULL) {
6555 			mutex_exit(&connfp->connf_lock);
6556 			return;
6557 		}
6558 	}
6559 
6560 	CONN_INC_REF(connp);
6561 	mutex_exit(&connfp->connf_lock);
6562 
6563 	ipsec_conn_pol(sel, connp, ppp);
6564 	CONN_DEC_REF(connp);
6565 }
6566 
6567 static conn_t *
6568 ipsec_find_listen_conn(uint16_t *pptr, ipsec_selector_t *sel, ip_stack_t *ipst)
6569 {
6570 	connf_t *connfp;
6571 	conn_t *connp = NULL;
6572 	const in6_addr_t *v6addrmatch = &sel->ips_local_addr_v6;
6573 
6574 	if (sel->ips_local_port == 0)
6575 		return (NULL);
6576 
6577 	connfp = &ipst->ips_ipcl_bind_fanout[
6578 	    IPCL_BIND_HASH(sel->ips_local_port, ipst)];
6579 	mutex_enter(&connfp->connf_lock);
6580 
6581 	if (sel->ips_isv4) {
6582 		connp = connfp->connf_head;
6583 		while (connp != NULL) {
6584 			if (IPCL_BIND_MATCH(connp, IPPROTO_TCP,
6585 			    sel->ips_local_addr_v4, pptr[1]))
6586 				break;
6587 			connp = connp->conn_next;
6588 		}
6589 
6590 		if (connp == NULL) {
6591 			/* Match to all-zeroes. */
6592 			v6addrmatch = &ipv6_all_zeros;
6593 		}
6594 	}
6595 
6596 	if (connp == NULL) {
6597 		connp = connfp->connf_head;
6598 		while (connp != NULL) {
6599 			if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP,
6600 			    *v6addrmatch, pptr[1]))
6601 				break;
6602 			connp = connp->conn_next;
6603 		}
6604 
6605 		if (connp == NULL) {
6606 			mutex_exit(&connfp->connf_lock);
6607 			return (NULL);
6608 		}
6609 	}
6610 
6611 	CONN_INC_REF(connp);
6612 	mutex_exit(&connfp->connf_lock);
6613 	return (connp);
6614 }
6615 
6616 static void
6617 ipsec_tcp_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp, ip_stack_t *ipst)
6618 {
6619 	connf_t		*connfp;
6620 	conn_t		*connp;
6621 	uint32_t	ports;
6622 	uint16_t	*pptr = (uint16_t *)&ports;
6623 
6624 	/*
6625 	 * Find TCP state in the following order:
6626 	 * 1.) Connected conns.
6627 	 * 2.) Listeners.
6628 	 *
6629 	 * Even though #2 will be the common case for inbound traffic, only
6630 	 * following this order insures correctness.
6631 	 */
6632 
6633 	if (sel->ips_local_port == 0)
6634 		return;
6635 
6636 	/*
6637 	 * 0 should be fport, 1 should be lport.  SRC is the local one here.
6638 	 * See ipsec_construct_inverse_acquire() for details.
6639 	 */
6640 	pptr[0] = sel->ips_remote_port;
6641 	pptr[1] = sel->ips_local_port;
6642 
6643 	connfp = &ipst->ips_ipcl_conn_fanout[
6644 	    IPCL_CONN_HASH(sel->ips_remote_addr_v4, ports, ipst)];
6645 	mutex_enter(&connfp->connf_lock);
6646 	connp = connfp->connf_head;
6647 
6648 	if (sel->ips_isv4) {
6649 		while (connp != NULL) {
6650 			if (IPCL_CONN_MATCH(connp, IPPROTO_TCP,
6651 			    sel->ips_remote_addr_v4, sel->ips_local_addr_v4,
6652 			    ports))
6653 				break;
6654 			connp = connp->conn_next;
6655 		}
6656 	} else {
6657 		while (connp != NULL) {
6658 			if (IPCL_CONN_MATCH_V6(connp, IPPROTO_TCP,
6659 			    sel->ips_remote_addr_v6, sel->ips_local_addr_v6,
6660 			    ports))
6661 				break;
6662 			connp = connp->conn_next;
6663 		}
6664 	}
6665 
6666 	if (connp != NULL) {
6667 		CONN_INC_REF(connp);
6668 		mutex_exit(&connfp->connf_lock);
6669 	} else {
6670 		mutex_exit(&connfp->connf_lock);
6671 
6672 		/* Try the listen hash. */
6673 		if ((connp = ipsec_find_listen_conn(pptr, sel, ipst)) == NULL)
6674 			return;
6675 	}
6676 
6677 	ipsec_conn_pol(sel, connp, ppp);
6678 	CONN_DEC_REF(connp);
6679 }
6680 
6681 static void
6682 ipsec_sctp_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp,
6683     ip_stack_t *ipst)
6684 {
6685 	conn_t		*connp;
6686 	uint32_t	ports;
6687 	uint16_t	*pptr = (uint16_t *)&ports;
6688 
6689 	/*
6690 	 * Find SCP state in the following order:
6691 	 * 1.) Connected conns.
6692 	 * 2.) Listeners.
6693 	 *
6694 	 * Even though #2 will be the common case for inbound traffic, only
6695 	 * following this order insures correctness.
6696 	 */
6697 
6698 	if (sel->ips_local_port == 0)
6699 		return;
6700 
6701 	/*
6702 	 * 0 should be fport, 1 should be lport.  SRC is the local one here.
6703 	 * See ipsec_construct_inverse_acquire() for details.
6704 	 */
6705 	pptr[0] = sel->ips_remote_port;
6706 	pptr[1] = sel->ips_local_port;
6707 
6708 	/*
6709 	 * For labeled systems, there's no need to check the
6710 	 * label here.  It's known to be good as we checked
6711 	 * before allowing the connection to become bound.
6712 	 */
6713 	if (sel->ips_isv4) {
6714 		in6_addr_t	src, dst;
6715 
6716 		IN6_IPADDR_TO_V4MAPPED(sel->ips_remote_addr_v4, &dst);
6717 		IN6_IPADDR_TO_V4MAPPED(sel->ips_local_addr_v4, &src);
6718 		connp = sctp_find_conn(&dst, &src, ports, ALL_ZONES,
6719 		    0, ipst->ips_netstack->netstack_sctp);
6720 	} else {
6721 		connp = sctp_find_conn(&sel->ips_remote_addr_v6,
6722 		    &sel->ips_local_addr_v6, ports, ALL_ZONES,
6723 		    0, ipst->ips_netstack->netstack_sctp);
6724 	}
6725 	if (connp == NULL)
6726 		return;
6727 	ipsec_conn_pol(sel, connp, ppp);
6728 	CONN_DEC_REF(connp);
6729 }
6730 
6731 /*
6732  * Fill in a query for the SPD (in "sel") using two PF_KEY address extensions.
6733  * Returns 0 or errno, and always sets *diagnostic to something appropriate
6734  * to PF_KEY.
6735  *
6736  * NOTE:  For right now, this function (and ipsec_selector_t for that matter),
6737  * ignore prefix lengths in the address extension.  Since we match on first-
6738  * entered policies, this shouldn't matter.  Also, since we normalize prefix-
6739  * set addresses to mask out the lower bits, we should get a suitable search
6740  * key for the SPD anyway.  This is the function to change if the assumption
6741  * about suitable search keys is wrong.
6742  */
6743 static int
6744 ipsec_get_inverse_acquire_sel(ipsec_selector_t *sel, sadb_address_t *srcext,
6745     sadb_address_t *dstext, int *diagnostic)
6746 {
6747 	struct sockaddr_in *src, *dst;
6748 	struct sockaddr_in6 *src6, *dst6;
6749 
6750 	*diagnostic = 0;
6751 
6752 	bzero(sel, sizeof (*sel));
6753 	sel->ips_protocol = srcext->sadb_address_proto;
6754 	dst = (struct sockaddr_in *)(dstext + 1);
6755 	if (dst->sin_family == AF_INET6) {
6756 		dst6 = (struct sockaddr_in6 *)dst;
6757 		src6 = (struct sockaddr_in6 *)(srcext + 1);
6758 		if (src6->sin6_family != AF_INET6) {
6759 			*diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH;
6760 			return (EINVAL);
6761 		}
6762 		sel->ips_remote_addr_v6 = dst6->sin6_addr;
6763 		sel->ips_local_addr_v6 = src6->sin6_addr;
6764 		if (sel->ips_protocol == IPPROTO_ICMPV6) {
6765 			sel->ips_is_icmp_inv_acq = 1;
6766 		} else {
6767 			sel->ips_remote_port = dst6->sin6_port;
6768 			sel->ips_local_port = src6->sin6_port;
6769 		}
6770 		sel->ips_isv4 = B_FALSE;
6771 	} else {
6772 		src = (struct sockaddr_in *)(srcext + 1);
6773 		if (src->sin_family != AF_INET) {
6774 			*diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH;
6775 			return (EINVAL);
6776 		}
6777 		sel->ips_remote_addr_v4 = dst->sin_addr.s_addr;
6778 		sel->ips_local_addr_v4 = src->sin_addr.s_addr;
6779 		if (sel->ips_protocol == IPPROTO_ICMP) {
6780 			sel->ips_is_icmp_inv_acq = 1;
6781 		} else {
6782 			sel->ips_remote_port = dst->sin_port;
6783 			sel->ips_local_port = src->sin_port;
6784 		}
6785 		sel->ips_isv4 = B_TRUE;
6786 	}
6787 	return (0);
6788 }
6789 
6790 /*
6791  * We have encapsulation.
6792  * - Lookup tun_t by address and look for an associated
6793  *   tunnel policy
6794  * - If there are inner selectors
6795  *   - check ITPF_P_TUNNEL and ITPF_P_ACTIVE
6796  *   - Look up tunnel policy based on selectors
6797  * - Else
6798  *   - Sanity check the negotation
6799  *   - If appropriate, fall through to global policy
6800  */
6801 static int
6802 ipsec_tun_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp,
6803     sadb_address_t *innsrcext, sadb_address_t *inndstext, ipsec_tun_pol_t *itp,
6804     int *diagnostic)
6805 {
6806 	int err;
6807 	ipsec_policy_head_t *polhead;
6808 
6809 	*diagnostic = 0;
6810 
6811 	/* Check for inner selectors and act appropriately */
6812 
6813 	if (innsrcext != NULL) {
6814 		/* Inner selectors present */
6815 		ASSERT(inndstext != NULL);
6816 		if ((itp == NULL) ||
6817 		    (itp->itp_flags & (ITPF_P_ACTIVE | ITPF_P_TUNNEL)) !=
6818 		    (ITPF_P_ACTIVE | ITPF_P_TUNNEL)) {
6819 			/*
6820 			 * If inner packet selectors, we must have negotiate
6821 			 * tunnel and active policy.  If the tunnel has
6822 			 * transport-mode policy set on it, or has no policy,
6823 			 * fail.
6824 			 */
6825 			return (ENOENT);
6826 		} else {
6827 			/*
6828 			 * Reset "sel" to indicate inner selectors.  Pass
6829 			 * inner PF_KEY address extensions for this to happen.
6830 			 */
6831 			if ((err = ipsec_get_inverse_acquire_sel(sel,
6832 			    innsrcext, inndstext, diagnostic)) != 0)
6833 				return (err);
6834 			/*
6835 			 * Now look for a tunnel policy based on those inner
6836 			 * selectors.  (Common code is below.)
6837 			 */
6838 		}
6839 	} else {
6840 		/* No inner selectors present */
6841 		if ((itp == NULL) || !(itp->itp_flags & ITPF_P_ACTIVE)) {
6842 			/*
6843 			 * Transport mode negotiation with no tunnel policy
6844 			 * configured - return to indicate a global policy
6845 			 * check is needed.
6846 			 */
6847 			return (0);
6848 		} else if (itp->itp_flags & ITPF_P_TUNNEL) {
6849 			/* Tunnel mode set with no inner selectors. */
6850 			return (ENOENT);
6851 		}
6852 		/*
6853 		 * Else, this is a tunnel policy configured with ifconfig(1m)
6854 		 * or "negotiate transport" with ipsecconf(1m).  We have an
6855 		 * itp with policy set based on any match, so don't bother
6856 		 * changing fields in "sel".
6857 		 */
6858 	}
6859 
6860 	ASSERT(itp != NULL);
6861 	polhead = itp->itp_policy;
6862 	ASSERT(polhead != NULL);
6863 	rw_enter(&polhead->iph_lock, RW_READER);
6864 	*ppp = ipsec_find_policy_head(NULL, polhead, IPSEC_TYPE_INBOUND, sel);
6865 	rw_exit(&polhead->iph_lock);
6866 
6867 	/*
6868 	 * Don't default to global if we didn't find a matching policy entry.
6869 	 * Instead, send ENOENT, just like if we hit a transport-mode tunnel.
6870 	 */
6871 	if (*ppp == NULL)
6872 		return (ENOENT);
6873 
6874 	return (0);
6875 }
6876 
6877 /*
6878  * For sctp conn_faddr is the primary address, hence this is of limited
6879  * use for sctp.
6880  */
6881 static void
6882 ipsec_oth_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp,
6883     ip_stack_t *ipst)
6884 {
6885 	boolean_t	isv4 = sel->ips_isv4;
6886 	connf_t		*connfp;
6887 	conn_t		*connp;
6888 
6889 	if (isv4) {
6890 		connfp = &ipst->ips_ipcl_proto_fanout_v4[sel->ips_protocol];
6891 	} else {
6892 		connfp = &ipst->ips_ipcl_proto_fanout_v6[sel->ips_protocol];
6893 	}
6894 
6895 	mutex_enter(&connfp->connf_lock);
6896 	for (connp = connfp->connf_head; connp != NULL;
6897 	    connp = connp->conn_next) {
6898 		if (isv4) {
6899 			if ((connp->conn_laddr_v4 == INADDR_ANY ||
6900 			    connp->conn_laddr_v4 == sel->ips_local_addr_v4) &&
6901 			    (connp->conn_faddr_v4 == INADDR_ANY ||
6902 			    connp->conn_faddr_v4 == sel->ips_remote_addr_v4))
6903 				break;
6904 		} else {
6905 			if ((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6) ||
6906 			    IN6_ARE_ADDR_EQUAL(&connp->conn_laddr_v6,
6907 			    &sel->ips_local_addr_v6)) &&
6908 			    (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6) ||
6909 			    IN6_ARE_ADDR_EQUAL(&connp->conn_faddr_v6,
6910 			    &sel->ips_remote_addr_v6)))
6911 				break;
6912 		}
6913 	}
6914 	if (connp == NULL) {
6915 		mutex_exit(&connfp->connf_lock);
6916 		return;
6917 	}
6918 
6919 	CONN_INC_REF(connp);
6920 	mutex_exit(&connfp->connf_lock);
6921 
6922 	ipsec_conn_pol(sel, connp, ppp);
6923 	CONN_DEC_REF(connp);
6924 }
6925 
6926 /*
6927  * Construct an inverse ACQUIRE reply based on:
6928  *
6929  * 1.) Current global policy.
6930  * 2.) An conn_t match depending on what all was passed in the extv[].
6931  * 3.) A tunnel's policy head.
6932  * ...
6933  * N.) Other stuff TBD (e.g. identities)
6934  *
6935  * If there is an error, set sadb_msg_errno and sadb_x_msg_diagnostic
6936  * in this function so the caller can extract them where appropriately.
6937  *
6938  * The SRC address is the local one - just like an outbound ACQUIRE message.
6939  *
6940  * XXX MLS: key management supplies a label which we just reflect back up
6941  * again.  clearly we need to involve the label in the rest of the checks.
6942  */
6943 mblk_t *
6944 ipsec_construct_inverse_acquire(sadb_msg_t *samsg, sadb_ext_t *extv[],
6945     netstack_t *ns)
6946 {
6947 	int err;
6948 	int diagnostic;
6949 	sadb_address_t *srcext = (sadb_address_t *)extv[SADB_EXT_ADDRESS_SRC],
6950 	    *dstext = (sadb_address_t *)extv[SADB_EXT_ADDRESS_DST],
6951 	    *innsrcext = (sadb_address_t *)extv[SADB_X_EXT_ADDRESS_INNER_SRC],
6952 	    *inndstext = (sadb_address_t *)extv[SADB_X_EXT_ADDRESS_INNER_DST];
6953 	sadb_sens_t *sens = (sadb_sens_t *)extv[SADB_EXT_SENSITIVITY];
6954 	struct sockaddr_in6 *src, *dst;
6955 	struct sockaddr_in6 *isrc, *idst;
6956 	ipsec_tun_pol_t *itp = NULL;
6957 	ipsec_policy_t *pp = NULL;
6958 	ipsec_selector_t sel, isel;
6959 	mblk_t *retmp = NULL;
6960 	ip_stack_t	*ipst = ns->netstack_ip;
6961 
6962 
6963 	/* Normalize addresses */
6964 	if (sadb_addrcheck(NULL, (mblk_t *)samsg, (sadb_ext_t *)srcext, 0, ns)
6965 	    == KS_IN_ADDR_UNKNOWN) {
6966 		err = EINVAL;
6967 		diagnostic = SADB_X_DIAGNOSTIC_BAD_SRC;
6968 		goto bail;
6969 	}
6970 	src = (struct sockaddr_in6 *)(srcext + 1);
6971 	if (sadb_addrcheck(NULL, (mblk_t *)samsg, (sadb_ext_t *)dstext, 0, ns)
6972 	    == KS_IN_ADDR_UNKNOWN) {
6973 		err = EINVAL;
6974 		diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
6975 		goto bail;
6976 	}
6977 	dst = (struct sockaddr_in6 *)(dstext + 1);
6978 	if (src->sin6_family != dst->sin6_family) {
6979 		err = EINVAL;
6980 		diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH;
6981 		goto bail;
6982 	}
6983 
6984 	/* Check for tunnel mode and act appropriately */
6985 	if (innsrcext != NULL) {
6986 		if (inndstext == NULL) {
6987 			err = EINVAL;
6988 			diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
6989 			goto bail;
6990 		}
6991 		if (sadb_addrcheck(NULL, (mblk_t *)samsg,
6992 		    (sadb_ext_t *)innsrcext, 0, ns) == KS_IN_ADDR_UNKNOWN) {
6993 			err = EINVAL;
6994 			diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC;
6995 			goto bail;
6996 		}
6997 		isrc = (struct sockaddr_in6 *)(innsrcext + 1);
6998 		if (sadb_addrcheck(NULL, (mblk_t *)samsg,
6999 		    (sadb_ext_t *)inndstext, 0, ns) == KS_IN_ADDR_UNKNOWN) {
7000 			err = EINVAL;
7001 			diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST;
7002 			goto bail;
7003 		}
7004 		idst = (struct sockaddr_in6 *)(inndstext + 1);
7005 		if (isrc->sin6_family != idst->sin6_family) {
7006 			err = EINVAL;
7007 			diagnostic = SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH;
7008 			goto bail;
7009 		}
7010 		if (isrc->sin6_family != AF_INET &&
7011 		    isrc->sin6_family != AF_INET6) {
7012 			err = EINVAL;
7013 			diagnostic = SADB_X_DIAGNOSTIC_BAD_INNER_SRC_AF;
7014 			goto bail;
7015 		}
7016 	} else if (inndstext != NULL) {
7017 		err = EINVAL;
7018 		diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
7019 		goto bail;
7020 	}
7021 
7022 	/* Get selectors first, based on outer addresses */
7023 	err = ipsec_get_inverse_acquire_sel(&sel, srcext, dstext, &diagnostic);
7024 	if (err != 0)
7025 		goto bail;
7026 
7027 	/* Check for tunnel mode mismatches. */
7028 	if (innsrcext != NULL &&
7029 	    ((isrc->sin6_family == AF_INET &&
7030 	    sel.ips_protocol != IPPROTO_ENCAP && sel.ips_protocol != 0) ||
7031 	    (isrc->sin6_family == AF_INET6 &&
7032 	    sel.ips_protocol != IPPROTO_IPV6 && sel.ips_protocol != 0))) {
7033 		err = EPROTOTYPE;
7034 		goto bail;
7035 	}
7036 
7037 	/*
7038 	 * Okay, we have the addresses and other selector information.
7039 	 * Let's first find a conn...
7040 	 */
7041 	pp = NULL;
7042 	switch (sel.ips_protocol) {
7043 	case IPPROTO_TCP:
7044 		ipsec_tcp_pol(&sel, &pp, ipst);
7045 		break;
7046 	case IPPROTO_UDP:
7047 		ipsec_udp_pol(&sel, &pp, ipst);
7048 		break;
7049 	case IPPROTO_SCTP:
7050 		ipsec_sctp_pol(&sel, &pp, ipst);
7051 		break;
7052 	case IPPROTO_ENCAP:
7053 	case IPPROTO_IPV6:
7054 		/*
7055 		 * Assume sel.ips_remote_addr_* has the right address at
7056 		 * that exact position.
7057 		 */
7058 		itp = itp_get_byaddr((uint32_t *)(&sel.ips_local_addr_v6),
7059 		    (uint32_t *)(&sel.ips_remote_addr_v6), src->sin6_family,
7060 		    ipst);
7061 
7062 		if (innsrcext == NULL) {
7063 			/*
7064 			 * Transport-mode tunnel, make sure we fake out isel
7065 			 * to contain something based on the outer protocol.
7066 			 */
7067 			bzero(&isel, sizeof (isel));
7068 			isel.ips_isv4 = (sel.ips_protocol == IPPROTO_ENCAP);
7069 		} /* Else isel is initialized by ipsec_tun_pol(). */
7070 		err = ipsec_tun_pol(&isel, &pp, innsrcext, inndstext, itp,
7071 		    &diagnostic);
7072 		/*
7073 		 * NOTE:  isel isn't used for now, but in RFC 430x IPsec, it
7074 		 * may be.
7075 		 */
7076 		if (err != 0)
7077 			goto bail;
7078 		break;
7079 	default:
7080 		ipsec_oth_pol(&sel, &pp, ipst);
7081 		break;
7082 	}
7083 
7084 	/*
7085 	 * If we didn't find a matching conn_t or other policy head, take a
7086 	 * look in the global policy.
7087 	 */
7088 	if (pp == NULL) {
7089 		pp = ipsec_find_policy(IPSEC_TYPE_OUTBOUND, NULL, &sel, ns);
7090 		if (pp == NULL) {
7091 			/* There's no global policy. */
7092 			err = ENOENT;
7093 			diagnostic = 0;
7094 			goto bail;
7095 		}
7096 	}
7097 
7098 	ASSERT(pp != NULL);
7099 	retmp = sadb_acquire_msg_base(0, 0, samsg->sadb_msg_seq,
7100 	    samsg->sadb_msg_pid);
7101 	if (retmp != NULL) {
7102 		/* Remove KEYSOCK_OUT, because caller constructs it instead. */
7103 		mblk_t *kso = retmp;
7104 
7105 		retmp = retmp->b_cont;
7106 		freeb(kso);
7107 		/* Append addresses... */
7108 		retmp->b_cont = sadb_acquire_msg_common(&sel, pp, NULL,
7109 		    (itp != NULL && (itp->itp_flags & ITPF_P_TUNNEL)), NULL,
7110 		    sens);
7111 		if (retmp->b_cont == NULL) {
7112 			freemsg(retmp);
7113 			retmp = NULL;
7114 		}
7115 		/* And the policy result. */
7116 		retmp->b_cont->b_cont =
7117 		    sadb_acquire_extended_prop(pp->ipsp_act, ns);
7118 		if (retmp->b_cont->b_cont == NULL) {
7119 			freemsg(retmp);
7120 			retmp = NULL;
7121 		}
7122 		((sadb_msg_t *)retmp->b_rptr)->sadb_msg_len =
7123 		    SADB_8TO64(msgsize(retmp));
7124 	}
7125 
7126 	if (pp != NULL) {
7127 		IPPOL_REFRELE(pp);
7128 	}
7129 	ASSERT(err == 0 && diagnostic == 0);
7130 	if (retmp == NULL)
7131 		err = ENOMEM;
7132 bail:
7133 	if (itp != NULL) {
7134 		ITP_REFRELE(itp, ns);
7135 	}
7136 	samsg->sadb_msg_errno = (uint8_t)err;
7137 	samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic;
7138 	return (retmp);
7139 }
7140 
7141 /*
7142  * ipsa_lpkt is a one-element queue, only manipulated by the next two
7143  * functions.  They have to hold the ipsa_lock because of potential races
7144  * between key management using SADB_UPDATE, and inbound packets that may
7145  * queue up on the larval SA (hence the 'l' in "lpkt").
7146  */
7147 
7148 /*
7149  * sadb_set_lpkt:
7150  *
7151  * Returns the passed-in packet if the SA is no longer larval.
7152  *
7153  * Returns NULL if the SA is larval, and needs to be swapped into the SA for
7154  * processing after an SADB_UPDATE.
7155  */
7156 mblk_t *
7157 sadb_set_lpkt(ipsa_t *ipsa, mblk_t *npkt, ip_recv_attr_t *ira)
7158 {
7159 	mblk_t		*opkt;
7160 
7161 	mutex_enter(&ipsa->ipsa_lock);
7162 	opkt = ipsa->ipsa_lpkt;
7163 	if (ipsa->ipsa_state == IPSA_STATE_LARVAL) {
7164 		/*
7165 		 * Consume npkt and place it in the LARVAL SA's inbound
7166 		 * packet slot.
7167 		 */
7168 		mblk_t	*attrmp;
7169 
7170 		attrmp = ip_recv_attr_to_mblk(ira);
7171 		if (attrmp == NULL) {
7172 			ill_t *ill = ira->ira_ill;
7173 
7174 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7175 			ip_drop_input("ipIfStatsInDiscards", npkt, ill);
7176 			freemsg(npkt);
7177 			opkt = NULL;
7178 		} else {
7179 			ASSERT(attrmp->b_cont == NULL);
7180 			attrmp->b_cont = npkt;
7181 			ipsa->ipsa_lpkt = attrmp;
7182 		}
7183 		npkt = NULL;
7184 	} else {
7185 		/*
7186 		 * If not larval, we lost the race.  NOTE: ipsa_lpkt may still
7187 		 * have been non-NULL in the non-larval case, because of
7188 		 * inbound packets arriving prior to sadb_common_add()
7189 		 * transferring the SA completely out of larval state, but
7190 		 * after lpkt was grabbed by the AH/ESP-specific add routines.
7191 		 * We should clear the old ipsa_lpkt in this case to make sure
7192 		 * that it doesn't linger on the now-MATURE IPsec SA, or get
7193 		 * picked up as an out-of-order packet.
7194 		 */
7195 		ipsa->ipsa_lpkt = NULL;
7196 	}
7197 	mutex_exit(&ipsa->ipsa_lock);
7198 
7199 	if (opkt != NULL) {
7200 		ipsec_stack_t	*ipss;
7201 
7202 		ipss = ira->ira_ill->ill_ipst->ips_netstack->netstack_ipsec;
7203 		opkt = ip_recv_attr_free_mblk(opkt);
7204 		ip_drop_packet(opkt, B_TRUE, ira->ira_ill,
7205 		    DROPPER(ipss, ipds_sadb_inlarval_replace),
7206 		    &ipss->ipsec_sadb_dropper);
7207 	}
7208 	return (npkt);
7209 }
7210 
7211 /*
7212  * sadb_clear_lpkt: Atomically clear ipsa->ipsa_lpkt and return the
7213  * previous value.
7214  */
7215 mblk_t *
7216 sadb_clear_lpkt(ipsa_t *ipsa)
7217 {
7218 	mblk_t *opkt;
7219 
7220 	mutex_enter(&ipsa->ipsa_lock);
7221 	opkt = ipsa->ipsa_lpkt;
7222 	ipsa->ipsa_lpkt = NULL;
7223 	mutex_exit(&ipsa->ipsa_lock);
7224 	return (opkt);
7225 }
7226 
7227 /*
7228  * Buffer a packet that's in IDLE state as set by Solaris Clustering.
7229  */
7230 void
7231 sadb_buf_pkt(ipsa_t *ipsa, mblk_t *bpkt, ip_recv_attr_t *ira)
7232 {
7233 	netstack_t	*ns = ira->ira_ill->ill_ipst->ips_netstack;
7234 	ipsec_stack_t   *ipss = ns->netstack_ipsec;
7235 	in6_addr_t *srcaddr = (in6_addr_t *)(&ipsa->ipsa_srcaddr);
7236 	in6_addr_t *dstaddr = (in6_addr_t *)(&ipsa->ipsa_dstaddr);
7237 	mblk_t		*mp;
7238 
7239 	ASSERT(ipsa->ipsa_state == IPSA_STATE_IDLE);
7240 
7241 	if (cl_inet_idlesa == NULL) {
7242 		ip_drop_packet(bpkt, B_TRUE, ira->ira_ill,
7243 		    DROPPER(ipss, ipds_sadb_inidle_overflow),
7244 		    &ipss->ipsec_sadb_dropper);
7245 		return;
7246 	}
7247 
7248 	cl_inet_idlesa(ns->netstack_stackid,
7249 	    (ipsa->ipsa_type == SADB_SATYPE_AH) ? IPPROTO_AH : IPPROTO_ESP,
7250 	    ipsa->ipsa_spi, ipsa->ipsa_addrfam, *srcaddr, *dstaddr, NULL);
7251 
7252 	mp = ip_recv_attr_to_mblk(ira);
7253 	if (mp == NULL) {
7254 		ip_drop_packet(bpkt, B_TRUE, ira->ira_ill,
7255 		    DROPPER(ipss, ipds_sadb_inidle_overflow),
7256 		    &ipss->ipsec_sadb_dropper);
7257 		return;
7258 	}
7259 	linkb(mp, bpkt);
7260 
7261 	mutex_enter(&ipsa->ipsa_lock);
7262 	ipsa->ipsa_mblkcnt++;
7263 	if (ipsa->ipsa_bpkt_head == NULL) {
7264 		ipsa->ipsa_bpkt_head = ipsa->ipsa_bpkt_tail = bpkt;
7265 	} else {
7266 		ipsa->ipsa_bpkt_tail->b_next = bpkt;
7267 		ipsa->ipsa_bpkt_tail = bpkt;
7268 		if (ipsa->ipsa_mblkcnt > SADB_MAX_IDLEPKTS) {
7269 			mblk_t *tmp;
7270 
7271 			tmp = ipsa->ipsa_bpkt_head;
7272 			ipsa->ipsa_bpkt_head = ipsa->ipsa_bpkt_head->b_next;
7273 			tmp = ip_recv_attr_free_mblk(tmp);
7274 			ip_drop_packet(tmp, B_TRUE, NULL,
7275 			    DROPPER(ipss, ipds_sadb_inidle_overflow),
7276 			    &ipss->ipsec_sadb_dropper);
7277 			ipsa->ipsa_mblkcnt --;
7278 		}
7279 	}
7280 	mutex_exit(&ipsa->ipsa_lock);
7281 }
7282 
7283 /*
7284  * Stub function that taskq_dispatch() invokes to take the mblk (in arg)
7285  * and put into STREAMS again.
7286  */
7287 void
7288 sadb_clear_buf_pkt(void *ipkt)
7289 {
7290 	mblk_t	*tmp, *buf_pkt;
7291 	ip_recv_attr_t	iras;
7292 
7293 	buf_pkt = (mblk_t *)ipkt;
7294 
7295 	while (buf_pkt != NULL) {
7296 		mblk_t *data_mp;
7297 
7298 		tmp = buf_pkt->b_next;
7299 		buf_pkt->b_next = NULL;
7300 
7301 		data_mp = buf_pkt->b_cont;
7302 		buf_pkt->b_cont = NULL;
7303 		if (!ip_recv_attr_from_mblk(buf_pkt, &iras)) {
7304 			/* The ill or ip_stack_t disappeared on us. */
7305 			ip_drop_input("ip_recv_attr_from_mblk", data_mp, NULL);
7306 			freemsg(data_mp);
7307 		} else {
7308 			ip_input_post_ipsec(data_mp, &iras);
7309 		}
7310 		ira_cleanup(&iras, B_TRUE);
7311 		buf_pkt = tmp;
7312 	}
7313 }
7314 /*
7315  * Walker callback used by sadb_alg_update() to free/create crypto
7316  * context template when a crypto software provider is removed or
7317  * added.
7318  */
7319 
7320 struct sadb_update_alg_state {
7321 	ipsec_algtype_t alg_type;
7322 	uint8_t alg_id;
7323 	boolean_t is_added;
7324 	boolean_t async_auth;
7325 	boolean_t async_encr;
7326 };
7327 
7328 static void
7329 sadb_alg_update_cb(isaf_t *head, ipsa_t *entry, void *cookie)
7330 {
7331 	struct sadb_update_alg_state *update_state =
7332 	    (struct sadb_update_alg_state *)cookie;
7333 	crypto_ctx_template_t *ctx_tmpl = NULL;
7334 
7335 	ASSERT(MUTEX_HELD(&head->isaf_lock));
7336 
7337 	if (entry->ipsa_state == IPSA_STATE_LARVAL)
7338 		return;
7339 
7340 	mutex_enter(&entry->ipsa_lock);
7341 
7342 	if ((entry->ipsa_encr_alg != SADB_EALG_NONE && entry->ipsa_encr_alg !=
7343 	    SADB_EALG_NULL && update_state->async_encr) ||
7344 	    (entry->ipsa_auth_alg != SADB_AALG_NONE &&
7345 	    update_state->async_auth)) {
7346 		entry->ipsa_flags |= IPSA_F_ASYNC;
7347 	} else {
7348 		entry->ipsa_flags &= ~IPSA_F_ASYNC;
7349 	}
7350 
7351 	switch (update_state->alg_type) {
7352 	case IPSEC_ALG_AUTH:
7353 		if (entry->ipsa_auth_alg == update_state->alg_id)
7354 			ctx_tmpl = &entry->ipsa_authtmpl;
7355 		break;
7356 	case IPSEC_ALG_ENCR:
7357 		if (entry->ipsa_encr_alg == update_state->alg_id)
7358 			ctx_tmpl = &entry->ipsa_encrtmpl;
7359 		break;
7360 	default:
7361 		ctx_tmpl = NULL;
7362 	}
7363 
7364 	if (ctx_tmpl == NULL) {
7365 		mutex_exit(&entry->ipsa_lock);
7366 		return;
7367 	}
7368 
7369 	/*
7370 	 * The context template of the SA may be affected by the change
7371 	 * of crypto provider.
7372 	 */
7373 	if (update_state->is_added) {
7374 		/* create the context template if not already done */
7375 		if (*ctx_tmpl == NULL) {
7376 			(void) ipsec_create_ctx_tmpl(entry,
7377 			    update_state->alg_type);
7378 		}
7379 	} else {
7380 		/*
7381 		 * The crypto provider was removed. If the context template
7382 		 * exists but it is no longer valid, free it.
7383 		 */
7384 		if (*ctx_tmpl != NULL)
7385 			ipsec_destroy_ctx_tmpl(entry, update_state->alg_type);
7386 	}
7387 
7388 	mutex_exit(&entry->ipsa_lock);
7389 }
7390 
7391 /*
7392  * Invoked by IP when an software crypto provider has been updated, or if
7393  * the crypto synchrony changes.  The type and id of the corresponding
7394  * algorithm is passed as argument.  The type is set to ALL in the case of
7395  * a synchrony change.
7396  *
7397  * is_added is B_TRUE if the provider was added, B_FALSE if it was
7398  * removed. The function updates the SADB and free/creates the
7399  * context templates associated with SAs if needed.
7400  */
7401 
7402 #define	SADB_ALG_UPDATE_WALK(sadb, table) \
7403     sadb_walker((sadb).table, (sadb).sdb_hashsize, sadb_alg_update_cb, \
7404 	&update_state)
7405 
7406 void
7407 sadb_alg_update(ipsec_algtype_t alg_type, uint8_t alg_id, boolean_t is_added,
7408     netstack_t *ns)
7409 {
7410 	struct sadb_update_alg_state update_state;
7411 	ipsecah_stack_t	*ahstack = ns->netstack_ipsecah;
7412 	ipsecesp_stack_t	*espstack = ns->netstack_ipsecesp;
7413 	ipsec_stack_t *ipss = ns->netstack_ipsec;
7414 
7415 	update_state.alg_type = alg_type;
7416 	update_state.alg_id = alg_id;
7417 	update_state.is_added = is_added;
7418 	update_state.async_auth = ipss->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] ==
7419 	    IPSEC_ALGS_EXEC_ASYNC;
7420 	update_state.async_encr = ipss->ipsec_algs_exec_mode[IPSEC_ALG_ENCR] ==
7421 	    IPSEC_ALGS_EXEC_ASYNC;
7422 
7423 	if (alg_type == IPSEC_ALG_AUTH || alg_type == IPSEC_ALG_ALL) {
7424 		/* walk the AH tables only for auth. algorithm changes */
7425 		SADB_ALG_UPDATE_WALK(ahstack->ah_sadb.s_v4, sdb_of);
7426 		SADB_ALG_UPDATE_WALK(ahstack->ah_sadb.s_v4, sdb_if);
7427 		SADB_ALG_UPDATE_WALK(ahstack->ah_sadb.s_v6, sdb_of);
7428 		SADB_ALG_UPDATE_WALK(ahstack->ah_sadb.s_v6, sdb_if);
7429 	}
7430 
7431 	/* walk the ESP tables */
7432 	SADB_ALG_UPDATE_WALK(espstack->esp_sadb.s_v4, sdb_of);
7433 	SADB_ALG_UPDATE_WALK(espstack->esp_sadb.s_v4, sdb_if);
7434 	SADB_ALG_UPDATE_WALK(espstack->esp_sadb.s_v6, sdb_of);
7435 	SADB_ALG_UPDATE_WALK(espstack->esp_sadb.s_v6, sdb_if);
7436 }
7437 
7438 /*
7439  * Creates a context template for the specified SA. This function
7440  * is called when an SA is created and when a context template needs
7441  * to be created due to a change of software provider.
7442  */
7443 int
7444 ipsec_create_ctx_tmpl(ipsa_t *sa, ipsec_algtype_t alg_type)
7445 {
7446 	ipsec_alginfo_t *alg;
7447 	crypto_mechanism_t mech;
7448 	crypto_key_t *key;
7449 	crypto_ctx_template_t *sa_tmpl;
7450 	int rv;
7451 	ipsec_stack_t	*ipss = sa->ipsa_netstack->netstack_ipsec;
7452 
7453 	ASSERT(RW_READ_HELD(&ipss->ipsec_alg_lock));
7454 	ASSERT(MUTEX_HELD(&sa->ipsa_lock));
7455 
7456 	/* get pointers to the algorithm info, context template, and key */
7457 	switch (alg_type) {
7458 	case IPSEC_ALG_AUTH:
7459 		key = &sa->ipsa_kcfauthkey;
7460 		sa_tmpl = &sa->ipsa_authtmpl;
7461 		alg = ipss->ipsec_alglists[alg_type][sa->ipsa_auth_alg];
7462 		break;
7463 	case IPSEC_ALG_ENCR:
7464 		key = &sa->ipsa_kcfencrkey;
7465 		sa_tmpl = &sa->ipsa_encrtmpl;
7466 		alg = ipss->ipsec_alglists[alg_type][sa->ipsa_encr_alg];
7467 		break;
7468 	default:
7469 		alg = NULL;
7470 	}
7471 
7472 	if (alg == NULL || !ALG_VALID(alg))
7473 		return (EINVAL);
7474 
7475 	/* initialize the mech info structure for the framework */
7476 	ASSERT(alg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
7477 	mech.cm_type = alg->alg_mech_type;
7478 	mech.cm_param = NULL;
7479 	mech.cm_param_len = 0;
7480 
7481 	/* create a new context template */
7482 	rv = crypto_create_ctx_template(&mech, key, sa_tmpl, KM_NOSLEEP);
7483 
7484 	/*
7485 	 * CRYPTO_MECH_NOT_SUPPORTED can be returned if only hardware
7486 	 * providers are available for that mechanism. In that case
7487 	 * we don't fail, and will generate the context template from
7488 	 * the framework callback when a software provider for that
7489 	 * mechanism registers.
7490 	 *
7491 	 * The context template is assigned the special value
7492 	 * IPSEC_CTX_TMPL_ALLOC if the allocation failed due to a
7493 	 * lack of memory. No attempt will be made to use
7494 	 * the context template if it is set to this value.
7495 	 */
7496 	if (rv == CRYPTO_HOST_MEMORY) {
7497 		*sa_tmpl = IPSEC_CTX_TMPL_ALLOC;
7498 	} else if (rv != CRYPTO_SUCCESS) {
7499 		*sa_tmpl = NULL;
7500 		if (rv != CRYPTO_MECH_NOT_SUPPORTED)
7501 			return (EINVAL);
7502 	}
7503 
7504 	return (0);
7505 }
7506 
7507 /*
7508  * Destroy the context template of the specified algorithm type
7509  * of the specified SA. Must be called while holding the SA lock.
7510  */
7511 void
7512 ipsec_destroy_ctx_tmpl(ipsa_t *sa, ipsec_algtype_t alg_type)
7513 {
7514 	ASSERT(MUTEX_HELD(&sa->ipsa_lock));
7515 
7516 	if (alg_type == IPSEC_ALG_AUTH) {
7517 		if (sa->ipsa_authtmpl == IPSEC_CTX_TMPL_ALLOC)
7518 			sa->ipsa_authtmpl = NULL;
7519 		else if (sa->ipsa_authtmpl != NULL) {
7520 			crypto_destroy_ctx_template(sa->ipsa_authtmpl);
7521 			sa->ipsa_authtmpl = NULL;
7522 		}
7523 	} else {
7524 		ASSERT(alg_type == IPSEC_ALG_ENCR);
7525 		if (sa->ipsa_encrtmpl == IPSEC_CTX_TMPL_ALLOC)
7526 			sa->ipsa_encrtmpl = NULL;
7527 		else if (sa->ipsa_encrtmpl != NULL) {
7528 			crypto_destroy_ctx_template(sa->ipsa_encrtmpl);
7529 			sa->ipsa_encrtmpl = NULL;
7530 		}
7531 	}
7532 }
7533 
7534 /*
7535  * Use the kernel crypto framework to check the validity of a key received
7536  * via keysock. Returns 0 if the key is OK, -1 otherwise.
7537  */
7538 int
7539 ipsec_check_key(crypto_mech_type_t mech_type, sadb_key_t *sadb_key,
7540     boolean_t is_auth, int *diag)
7541 {
7542 	crypto_mechanism_t mech;
7543 	crypto_key_t crypto_key;
7544 	int crypto_rc;
7545 
7546 	mech.cm_type = mech_type;
7547 	mech.cm_param = NULL;
7548 	mech.cm_param_len = 0;
7549 
7550 	crypto_key.ck_format = CRYPTO_KEY_RAW;
7551 	crypto_key.ck_data = sadb_key + 1;
7552 	crypto_key.ck_length = sadb_key->sadb_key_bits;
7553 
7554 	crypto_rc = crypto_key_check(&mech, &crypto_key);
7555 
7556 	switch (crypto_rc) {
7557 	case CRYPTO_SUCCESS:
7558 		return (0);
7559 	case CRYPTO_MECHANISM_INVALID:
7560 	case CRYPTO_MECH_NOT_SUPPORTED:
7561 		*diag = is_auth ? SADB_X_DIAGNOSTIC_BAD_AALG :
7562 		    SADB_X_DIAGNOSTIC_BAD_EALG;
7563 		break;
7564 	case CRYPTO_KEY_SIZE_RANGE:
7565 		*diag = is_auth ? SADB_X_DIAGNOSTIC_BAD_AKEYBITS :
7566 		    SADB_X_DIAGNOSTIC_BAD_EKEYBITS;
7567 		break;
7568 	case CRYPTO_WEAK_KEY:
7569 		*diag = is_auth ? SADB_X_DIAGNOSTIC_WEAK_AKEY :
7570 		    SADB_X_DIAGNOSTIC_WEAK_EKEY;
7571 		break;
7572 	}
7573 
7574 	return (-1);
7575 }
7576 
7577 /*
7578  * Whack options in the outer IP header when ipsec changes the outer label
7579  *
7580  * This is inelegant and really could use refactoring.
7581  */
7582 mblk_t *
7583 sadb_whack_label_v4(mblk_t *mp, ipsa_t *assoc, kstat_named_t *counter,
7584     ipdropper_t *dropper)
7585 {
7586 	int delta;
7587 	int plen;
7588 	dblk_t *db;
7589 	int hlen;
7590 	uint8_t *opt_storage = assoc->ipsa_opt_storage;
7591 	ipha_t *ipha = (ipha_t *)mp->b_rptr;
7592 
7593 	plen = ntohs(ipha->ipha_length);
7594 
7595 	delta = tsol_remove_secopt(ipha, MBLKL(mp));
7596 	mp->b_wptr += delta;
7597 	plen += delta;
7598 
7599 	/* XXX XXX code copied from tsol_check_label */
7600 
7601 	/* Make sure we have room for the worst-case addition */
7602 	hlen = IPH_HDR_LENGTH(ipha) + opt_storage[IPOPT_OLEN];
7603 	hlen = (hlen + 3) & ~3;
7604 	if (hlen > IP_MAX_HDR_LENGTH)
7605 		hlen = IP_MAX_HDR_LENGTH;
7606 	hlen -= IPH_HDR_LENGTH(ipha);
7607 
7608 	db = mp->b_datap;
7609 	if ((db->db_ref != 1) || (mp->b_wptr + hlen > db->db_lim)) {
7610 		int copylen;
7611 		mblk_t *new_mp;
7612 
7613 		/* allocate enough to be meaningful, but not *too* much */
7614 		copylen = MBLKL(mp);
7615 		if (copylen > 256)
7616 			copylen = 256;
7617 		new_mp = allocb_tmpl(hlen + copylen +
7618 		    (mp->b_rptr - mp->b_datap->db_base), mp);
7619 
7620 		if (new_mp == NULL) {
7621 			ip_drop_packet(mp, B_FALSE, NULL, counter,  dropper);
7622 			return (NULL);
7623 		}
7624 
7625 		/* keep the bias */
7626 		new_mp->b_rptr += mp->b_rptr - mp->b_datap->db_base;
7627 		new_mp->b_wptr = new_mp->b_rptr + copylen;
7628 		bcopy(mp->b_rptr, new_mp->b_rptr, copylen);
7629 		new_mp->b_cont = mp;
7630 		if ((mp->b_rptr += copylen) >= mp->b_wptr) {
7631 			new_mp->b_cont = mp->b_cont;
7632 			freeb(mp);
7633 		}
7634 		mp = new_mp;
7635 		ipha = (ipha_t *)mp->b_rptr;
7636 	}
7637 
7638 	delta = tsol_prepend_option(assoc->ipsa_opt_storage, ipha, MBLKL(mp));
7639 
7640 	ASSERT(delta != -1);
7641 
7642 	plen += delta;
7643 	mp->b_wptr += delta;
7644 
7645 	/*
7646 	 * Paranoia
7647 	 */
7648 	db = mp->b_datap;
7649 
7650 	ASSERT3P(mp->b_wptr, <=, db->db_lim);
7651 	ASSERT3P(mp->b_rptr, <=, db->db_lim);
7652 
7653 	ASSERT3P(mp->b_wptr, >=, db->db_base);
7654 	ASSERT3P(mp->b_rptr, >=, db->db_base);
7655 	/* End paranoia */
7656 
7657 	ipha->ipha_length = htons(plen);
7658 
7659 	return (mp);
7660 }
7661 
7662 mblk_t *
7663 sadb_whack_label_v6(mblk_t *mp, ipsa_t *assoc, kstat_named_t *counter,
7664     ipdropper_t *dropper)
7665 {
7666 	int delta;
7667 	int plen;
7668 	dblk_t *db;
7669 	int hlen;
7670 	uint8_t *opt_storage = assoc->ipsa_opt_storage;
7671 	uint_t sec_opt_len; /* label option length not including type, len */
7672 	ip6_t *ip6h = (ip6_t *)mp->b_rptr;
7673 
7674 	plen = ntohs(ip6h->ip6_plen);
7675 
7676 	delta = tsol_remove_secopt_v6(ip6h, MBLKL(mp));
7677 	mp->b_wptr += delta;
7678 	plen += delta;
7679 
7680 	/* XXX XXX code copied from tsol_check_label_v6 */
7681 	/*
7682 	 * Make sure we have room for the worst-case addition. Add 2 bytes for
7683 	 * the hop-by-hop ext header's next header and length fields. Add
7684 	 * another 2 bytes for the label option type, len and then round
7685 	 * up to the next 8-byte multiple.
7686 	 */
7687 	sec_opt_len = opt_storage[1];
7688 
7689 	db = mp->b_datap;
7690 	hlen = (4 + sec_opt_len + 7) & ~7;
7691 
7692 	if ((db->db_ref != 1) || (mp->b_wptr + hlen > db->db_lim)) {
7693 		int copylen;
7694 		mblk_t *new_mp;
7695 		uint16_t hdr_len;
7696 
7697 		hdr_len = ip_hdr_length_v6(mp, ip6h);
7698 		/*
7699 		 * Allocate enough to be meaningful, but not *too* much.
7700 		 * Also all the IPv6 extension headers must be in the same mblk
7701 		 */
7702 		copylen = MBLKL(mp);
7703 		if (copylen > 256)
7704 			copylen = 256;
7705 		if (copylen < hdr_len)
7706 			copylen = hdr_len;
7707 		new_mp = allocb_tmpl(hlen + copylen +
7708 		    (mp->b_rptr - mp->b_datap->db_base), mp);
7709 		if (new_mp == NULL) {
7710 			ip_drop_packet(mp, B_FALSE, NULL, counter,  dropper);
7711 			return (NULL);
7712 		}
7713 
7714 		/* keep the bias */
7715 		new_mp->b_rptr += mp->b_rptr - mp->b_datap->db_base;
7716 		new_mp->b_wptr = new_mp->b_rptr + copylen;
7717 		bcopy(mp->b_rptr, new_mp->b_rptr, copylen);
7718 		new_mp->b_cont = mp;
7719 		if ((mp->b_rptr += copylen) >= mp->b_wptr) {
7720 			new_mp->b_cont = mp->b_cont;
7721 			freeb(mp);
7722 		}
7723 		mp = new_mp;
7724 		ip6h = (ip6_t *)mp->b_rptr;
7725 	}
7726 
7727 	delta = tsol_prepend_option_v6(assoc->ipsa_opt_storage,
7728 	    ip6h, MBLKL(mp));
7729 
7730 	ASSERT(delta != -1);
7731 
7732 	plen += delta;
7733 	mp->b_wptr += delta;
7734 
7735 	/*
7736 	 * Paranoia
7737 	 */
7738 	db = mp->b_datap;
7739 
7740 	ASSERT3P(mp->b_wptr, <=, db->db_lim);
7741 	ASSERT3P(mp->b_rptr, <=, db->db_lim);
7742 
7743 	ASSERT3P(mp->b_wptr, >=, db->db_base);
7744 	ASSERT3P(mp->b_rptr, >=, db->db_base);
7745 	/* End paranoia */
7746 
7747 	ip6h->ip6_plen = htons(plen);
7748 
7749 	return (mp);
7750 }
7751 
7752 /* Whack the labels and update ip_xmit_attr_t as needed */
7753 mblk_t *
7754 sadb_whack_label(mblk_t *mp, ipsa_t *assoc, ip_xmit_attr_t *ixa,
7755     kstat_named_t *counter, ipdropper_t *dropper)
7756 {
7757 	int adjust;
7758 	int iplen;
7759 
7760 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
7761 		ipha_t		*ipha = (ipha_t *)mp->b_rptr;
7762 
7763 		ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
7764 		iplen = ntohs(ipha->ipha_length);
7765 		mp = sadb_whack_label_v4(mp, assoc, counter, dropper);
7766 		if (mp == NULL)
7767 			return (NULL);
7768 
7769 		ipha = (ipha_t *)mp->b_rptr;
7770 		ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
7771 		adjust = (int)ntohs(ipha->ipha_length) - iplen;
7772 	} else {
7773 		ip6_t		*ip6h = (ip6_t *)mp->b_rptr;
7774 
7775 		ASSERT(IPH_HDR_VERSION(ip6h) == IPV6_VERSION);
7776 		iplen = ntohs(ip6h->ip6_plen);
7777 		mp = sadb_whack_label_v6(mp, assoc, counter, dropper);
7778 		if (mp == NULL)
7779 			return (NULL);
7780 
7781 		ip6h = (ip6_t *)mp->b_rptr;
7782 		ASSERT(IPH_HDR_VERSION(ip6h) == IPV6_VERSION);
7783 		adjust = (int)ntohs(ip6h->ip6_plen) - iplen;
7784 	}
7785 	ixa->ixa_pktlen += adjust;
7786 	ixa->ixa_ip_hdr_length += adjust;
7787 	return (mp);
7788 }
7789 
7790 /*
7791  * If this is an outgoing SA then add some fuzz to the
7792  * SOFT EXPIRE time. The reason for this is to stop
7793  * peers trying to renegotiate SOFT expiring SA's at
7794  * the same time. The amount of fuzz needs to be at
7795  * least 8 seconds which is the typical interval
7796  * sadb_ager(), although this is only a guide as it
7797  * selftunes.
7798  */
7799 static void
7800 lifetime_fuzz(ipsa_t *assoc)
7801 {
7802 	uint8_t rnd;
7803 
7804 	if (assoc->ipsa_softaddlt == 0)
7805 		return;
7806 
7807 	(void) random_get_pseudo_bytes(&rnd, sizeof (rnd));
7808 	rnd = (rnd & 0xF) + 8;
7809 	assoc->ipsa_softexpiretime -= rnd;
7810 	assoc->ipsa_softaddlt -= rnd;
7811 }
7812 
7813 static void
7814 destroy_ipsa_pair(ipsap_t *ipsapp)
7815 {
7816 	/*
7817 	 * Because of the multi-line macro nature of IPSA_REFRELE, keep
7818 	 * them in { }.
7819 	 */
7820 	if (ipsapp->ipsap_sa_ptr != NULL) {
7821 		IPSA_REFRELE(ipsapp->ipsap_sa_ptr);
7822 	}
7823 	if (ipsapp->ipsap_psa_ptr != NULL) {
7824 		IPSA_REFRELE(ipsapp->ipsap_psa_ptr);
7825 	}
7826 	init_ipsa_pair(ipsapp);
7827 }
7828 
7829 static void
7830 init_ipsa_pair(ipsap_t *ipsapp)
7831 {
7832 	ipsapp->ipsap_bucket = NULL;
7833 	ipsapp->ipsap_sa_ptr = NULL;
7834 	ipsapp->ipsap_pbucket = NULL;
7835 	ipsapp->ipsap_psa_ptr = NULL;
7836 }
7837 
7838 /*
7839  * The sadb_ager() function walks through the hash tables of SA's and ages
7840  * them, if the SA expires as a result, its marked as DEAD and will be reaped
7841  * the next time sadb_ager() runs. SA's which are paired or have a peer (same
7842  * SA appears in both the inbound and outbound tables because its not possible
7843  * to determine its direction) are placed on a list when they expire. This is
7844  * to ensure that pair/peer SA's are reaped at the same time, even if they
7845  * expire at different times.
7846  *
7847  * This function is called twice by sadb_ager(), one after processing the
7848  * inbound table, then again after processing the outbound table.
7849  */
7850 void
7851 age_pair_peer_list(templist_t *haspeerlist, sadb_t *sp, boolean_t outbound)
7852 {
7853 	templist_t *listptr;
7854 	int outhash;
7855 	isaf_t *bucket;
7856 	boolean_t haspeer;
7857 	ipsa_t *peer_assoc, *dying;
7858 	/*
7859 	 * Haspeer cases will contain both IPv4 and IPv6.  This code
7860 	 * is address independent.
7861 	 */
7862 	while (haspeerlist != NULL) {
7863 		/* "dying" contains the SA that has a peer. */
7864 		dying = haspeerlist->ipsa;
7865 		haspeer = (dying->ipsa_haspeer);
7866 		listptr = haspeerlist;
7867 		haspeerlist = listptr->next;
7868 		kmem_free(listptr, sizeof (*listptr));
7869 		/*
7870 		 * Pick peer bucket based on addrfam.
7871 		 */
7872 		if (outbound) {
7873 			if (haspeer)
7874 				bucket = INBOUND_BUCKET(sp, dying->ipsa_spi);
7875 			else
7876 				bucket = INBOUND_BUCKET(sp,
7877 				    dying->ipsa_otherspi);
7878 		} else { /* inbound */
7879 			if (haspeer) {
7880 				if (dying->ipsa_addrfam == AF_INET6) {
7881 					outhash = OUTBOUND_HASH_V6(sp,
7882 					    *((in6_addr_t *)&dying->
7883 					    ipsa_dstaddr));
7884 				} else {
7885 					outhash = OUTBOUND_HASH_V4(sp,
7886 					    *((ipaddr_t *)&dying->
7887 					    ipsa_dstaddr));
7888 				}
7889 			} else if (dying->ipsa_addrfam == AF_INET6) {
7890 				outhash = OUTBOUND_HASH_V6(sp,
7891 				    *((in6_addr_t *)&dying->
7892 				    ipsa_srcaddr));
7893 			} else {
7894 				outhash = OUTBOUND_HASH_V4(sp,
7895 				    *((ipaddr_t *)&dying->
7896 				    ipsa_srcaddr));
7897 			}
7898 			bucket = &(sp->sdb_of[outhash]);
7899 		}
7900 
7901 		mutex_enter(&bucket->isaf_lock);
7902 		/*
7903 		 * "haspeer" SA's have the same src/dst address ordering,
7904 		 * "paired" SA's have the src/dst addresses reversed.
7905 		 */
7906 		if (haspeer) {
7907 			peer_assoc = ipsec_getassocbyspi(bucket,
7908 			    dying->ipsa_spi, dying->ipsa_srcaddr,
7909 			    dying->ipsa_dstaddr, dying->ipsa_addrfam);
7910 		} else {
7911 			peer_assoc = ipsec_getassocbyspi(bucket,
7912 			    dying->ipsa_otherspi, dying->ipsa_dstaddr,
7913 			    dying->ipsa_srcaddr, dying->ipsa_addrfam);
7914 		}
7915 
7916 		mutex_exit(&bucket->isaf_lock);
7917 		if (peer_assoc != NULL) {
7918 			mutex_enter(&peer_assoc->ipsa_lock);
7919 			mutex_enter(&dying->ipsa_lock);
7920 			if (!haspeer) {
7921 				/*
7922 				 * Only SA's which have a "peer" or are
7923 				 * "paired" end up on this list, so this
7924 				 * must be a "paired" SA, update the flags
7925 				 * to break the pair.
7926 				 */
7927 				peer_assoc->ipsa_otherspi = 0;
7928 				peer_assoc->ipsa_flags &= ~IPSA_F_PAIRED;
7929 				dying->ipsa_otherspi = 0;
7930 				dying->ipsa_flags &= ~IPSA_F_PAIRED;
7931 			}
7932 			if (haspeer || outbound) {
7933 				/*
7934 				 * Update the state of the "inbound" SA when
7935 				 * the "outbound" SA has expired. Don't update
7936 				 * the "outbound" SA when the "inbound" SA
7937 				 * SA expires because setting the hard_addtime
7938 				 * below will cause this to happen.
7939 				 */
7940 				peer_assoc->ipsa_state = dying->ipsa_state;
7941 			}
7942 			if (dying->ipsa_state == IPSA_STATE_DEAD)
7943 				peer_assoc->ipsa_hardexpiretime = 1;
7944 
7945 			mutex_exit(&dying->ipsa_lock);
7946 			mutex_exit(&peer_assoc->ipsa_lock);
7947 			IPSA_REFRELE(peer_assoc);
7948 		}
7949 		IPSA_REFRELE(dying);
7950 	}
7951 }
7952 
7953 /*
7954  * Ensure that the IV used for CCM mode never repeats. The IV should
7955  * only be updated by this function. Also check to see if the IV
7956  * is about to wrap and generate a SOFT Expire. This function is only
7957  * called for outgoing packets, the IV for incomming packets is taken
7958  * from the wire. If the outgoing SA needs to be expired, update
7959  * the matching incomming SA.
7960  */
7961 boolean_t
7962 update_iv(uint8_t *iv_ptr, queue_t *pfkey_q, ipsa_t *assoc,
7963     ipsecesp_stack_t *espstack)
7964 {
7965 	boolean_t rc = B_TRUE;
7966 	isaf_t *inbound_bucket;
7967 	sadb_t *sp;
7968 	ipsa_t *pair_sa = NULL;
7969 	int sa_new_state = 0;
7970 
7971 	/* For non counter modes, the IV is random data. */
7972 	if (!(assoc->ipsa_flags & IPSA_F_COUNTERMODE)) {
7973 		(void) random_get_pseudo_bytes(iv_ptr, assoc->ipsa_iv_len);
7974 		return (rc);
7975 	}
7976 
7977 	mutex_enter(&assoc->ipsa_lock);
7978 
7979 	(*assoc->ipsa_iv)++;
7980 
7981 	if (*assoc->ipsa_iv == assoc->ipsa_iv_hardexpire) {
7982 		sa_new_state = IPSA_STATE_DEAD;
7983 		rc = B_FALSE;
7984 	} else if (*assoc->ipsa_iv == assoc->ipsa_iv_softexpire) {
7985 		if (assoc->ipsa_state != IPSA_STATE_DYING) {
7986 			/*
7987 			 * This SA may have already been expired when its
7988 			 * PAIR_SA expired.
7989 			 */
7990 			sa_new_state = IPSA_STATE_DYING;
7991 		}
7992 	}
7993 	if (sa_new_state) {
7994 		/*
7995 		 * If there is a state change, we need to update this SA
7996 		 * and its "pair", we can find the bucket for the "pair" SA
7997 		 * while holding the ipsa_t mutex, but we won't actually
7998 		 * update anything untill the ipsa_t mutex has been released
7999 		 * for _this_ SA.
8000 		 */
8001 		assoc->ipsa_state = sa_new_state;
8002 		if (assoc->ipsa_addrfam == AF_INET6) {
8003 			sp = &espstack->esp_sadb.s_v6;
8004 		} else {
8005 			sp = &espstack->esp_sadb.s_v4;
8006 		}
8007 		inbound_bucket = INBOUND_BUCKET(sp, assoc->ipsa_otherspi);
8008 		sadb_expire_assoc(pfkey_q, assoc);
8009 	}
8010 	if (rc == B_TRUE)
8011 		bcopy(assoc->ipsa_iv, iv_ptr, assoc->ipsa_iv_len);
8012 
8013 	mutex_exit(&assoc->ipsa_lock);
8014 
8015 	if (sa_new_state) {
8016 		/* Find the inbound SA, need to lock hash bucket. */
8017 		mutex_enter(&inbound_bucket->isaf_lock);
8018 		pair_sa = ipsec_getassocbyspi(inbound_bucket,
8019 		    assoc->ipsa_otherspi, assoc->ipsa_dstaddr,
8020 		    assoc->ipsa_srcaddr, assoc->ipsa_addrfam);
8021 		mutex_exit(&inbound_bucket->isaf_lock);
8022 		if (pair_sa != NULL) {
8023 			mutex_enter(&pair_sa->ipsa_lock);
8024 			pair_sa->ipsa_state = sa_new_state;
8025 			mutex_exit(&pair_sa->ipsa_lock);
8026 			IPSA_REFRELE(pair_sa);
8027 		}
8028 	}
8029 
8030 	return (rc);
8031 }
8032 
8033 void
8034 ccm_params_init(ipsa_t *assoc, uchar_t *esph, uint_t data_len, uchar_t *iv_ptr,
8035     ipsa_cm_mech_t *cm_mech, crypto_data_t *crypto_data)
8036 {
8037 	uchar_t *nonce;
8038 	crypto_mechanism_t *combined_mech;
8039 	CK_AES_CCM_PARAMS *params;
8040 
8041 	combined_mech = (crypto_mechanism_t *)cm_mech;
8042 	params = (CK_AES_CCM_PARAMS *)(combined_mech + 1);
8043 	nonce = (uchar_t *)(params + 1);
8044 	params->ulMACSize = assoc->ipsa_mac_len;
8045 	params->ulNonceSize = assoc->ipsa_nonce_len;
8046 	params->ulAuthDataSize = sizeof (esph_t);
8047 	params->ulDataSize = data_len;
8048 	params->nonce = nonce;
8049 	params->authData = esph;
8050 
8051 	cm_mech->combined_mech.cm_type = assoc->ipsa_emech.cm_type;
8052 	cm_mech->combined_mech.cm_param_len = sizeof (CK_AES_CCM_PARAMS);
8053 	cm_mech->combined_mech.cm_param = (caddr_t)params;
8054 	/* See gcm_params_init() for comments. */
8055 	bcopy(assoc->ipsa_nonce, nonce, assoc->ipsa_saltlen);
8056 	nonce += assoc->ipsa_saltlen;
8057 	bcopy(iv_ptr, nonce, assoc->ipsa_iv_len);
8058 	crypto_data->cd_miscdata = NULL;
8059 }
8060 
8061 /* ARGSUSED */
8062 void
8063 cbc_params_init(ipsa_t *assoc, uchar_t *esph, uint_t data_len, uchar_t *iv_ptr,
8064     ipsa_cm_mech_t *cm_mech, crypto_data_t *crypto_data)
8065 {
8066 	cm_mech->combined_mech.cm_type = assoc->ipsa_emech.cm_type;
8067 	cm_mech->combined_mech.cm_param_len = 0;
8068 	cm_mech->combined_mech.cm_param = NULL;
8069 	crypto_data->cd_miscdata = (char *)iv_ptr;
8070 }
8071 
8072 /* ARGSUSED */
8073 void
8074 gcm_params_init(ipsa_t *assoc, uchar_t *esph, uint_t data_len, uchar_t *iv_ptr,
8075     ipsa_cm_mech_t *cm_mech, crypto_data_t *crypto_data)
8076 {
8077 	uchar_t *nonce;
8078 	crypto_mechanism_t *combined_mech;
8079 	CK_AES_GCM_PARAMS *params;
8080 
8081 	combined_mech = (crypto_mechanism_t *)cm_mech;
8082 	params = (CK_AES_GCM_PARAMS *)(combined_mech + 1);
8083 	nonce = (uchar_t *)(params + 1);
8084 
8085 	params->pIv = nonce;
8086 	params->ulIvLen = assoc->ipsa_nonce_len;
8087 	params->ulIvBits = SADB_8TO1(assoc->ipsa_nonce_len);
8088 	params->pAAD = esph;
8089 	params->ulAADLen = sizeof (esph_t);
8090 	params->ulTagBits = SADB_8TO1(assoc->ipsa_mac_len);
8091 
8092 	cm_mech->combined_mech.cm_type = assoc->ipsa_emech.cm_type;
8093 	cm_mech->combined_mech.cm_param_len = sizeof (CK_AES_GCM_PARAMS);
8094 	cm_mech->combined_mech.cm_param = (caddr_t)params;
8095 	/*
8096 	 * Create the nonce, which is made up of the salt and the IV.
8097 	 * Copy the salt from the SA and the IV from the packet.
8098 	 * For inbound packets we copy the IV from the packet because it
8099 	 * was set by the sending system, for outbound packets we copy the IV
8100 	 * from the packet because the IV in the SA may be changed by another
8101 	 * thread, the IV in the packet was created while holding a mutex.
8102 	 */
8103 	bcopy(assoc->ipsa_nonce, nonce, assoc->ipsa_saltlen);
8104 	nonce += assoc->ipsa_saltlen;
8105 	bcopy(iv_ptr, nonce, assoc->ipsa_iv_len);
8106 	crypto_data->cd_miscdata = NULL;
8107 }
8108