xref: /titanic_41/usr/src/uts/common/inet/ip/spdsock.c (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/stream.h>
29 #include <sys/strsubr.h>
30 #include <sys/strsun.h>
31 #include <sys/stropts.h>
32 #include <sys/zone.h>
33 #include <sys/vnode.h>
34 #include <sys/sysmacros.h>
35 #define	_SUN_TPI_VERSION 2
36 #include <sys/tihdr.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 #include <sys/mkdev.h>
40 #include <sys/debug.h>
41 #include <sys/kmem.h>
42 #include <sys/cmn_err.h>
43 #include <sys/suntpi.h>
44 #include <sys/policy.h>
45 #include <sys/dls.h>
46 
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <net/pfkeyv2.h>
50 #include <net/pfpolicy.h>
51 
52 #include <inet/common.h>
53 #include <netinet/ip6.h>
54 #include <inet/ip.h>
55 #include <inet/ip6.h>
56 #include <inet/mi.h>
57 #include <inet/proto_set.h>
58 #include <inet/nd.h>
59 #include <inet/ip_if.h>
60 #include <inet/optcom.h>
61 #include <inet/ipsec_impl.h>
62 #include <inet/spdsock.h>
63 #include <inet/sadb.h>
64 #include <inet/iptun.h>
65 #include <inet/iptun/iptun_impl.h>
66 
67 #include <sys/isa_defs.h>
68 
69 #include <c2/audit.h>
70 
71 /*
72  * This is a transport provider for the PF_POLICY IPsec policy
73  * management socket, which provides a management interface into the
74  * SPD, allowing policy rules to be added, deleted, and queried.
75  *
76  * This effectively replaces the old private SIOC*IPSECONFIG ioctls
77  * with an extensible interface which will hopefully be public some
78  * day.
79  *
80  * See <net/pfpolicy.h> for more details on the protocol.
81  *
82  * We link against drv/ip and call directly into it to manipulate the
83  * SPD; see ipsec_impl.h for the policy data structures and spd.c for
84  * the code which maintains them.
85  *
86  * The MT model of this is QPAIR with the addition of some explicit
87  * locking to protect system-wide policy data structures.
88  */
89 
90 static vmem_t *spdsock_vmem;		/* for minor numbers. */
91 
92 #define	ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t))
93 
94 /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
95 static struct T_info_ack spdsock_g_t_info_ack = {
96 	T_INFO_ACK,
97 	T_INFINITE,	/* TSDU_size. Maximum size messages. */
98 	T_INVALID,	/* ETSDU_size. No expedited data. */
99 	T_INVALID,	/* CDATA_size. No connect data. */
100 	T_INVALID,	/* DDATA_size. No disconnect data. */
101 	0,		/* ADDR_size. */
102 	0,		/* OPT_size. No user-settable options */
103 	64 * 1024,	/* TIDU_size. spdsock allows maximum size messages. */
104 	T_COTS,		/* SERV_type. spdsock supports connection oriented. */
105 	TS_UNBND,	/* CURRENT_state. This is set from spdsock_state. */
106 	(XPG4_1)	/* Provider flags */
107 };
108 
109 /* Named Dispatch Parameter Management Structure */
110 typedef struct spdsockparam_s {
111 	uint_t	spdsock_param_min;
112 	uint_t	spdsock_param_max;
113 	uint_t	spdsock_param_value;
114 	char *spdsock_param_name;
115 } spdsockparam_t;
116 
117 /*
118  * Table of NDD variables supported by spdsock. These are loaded into
119  * spdsock_g_nd in spdsock_init_nd.
120  * All of these are alterable, within the min/max values given, at run time.
121  */
122 static	spdsockparam_t	lcl_param_arr[] = {
123 	/* min	max	value	name */
124 	{ 4096, 65536,	8192,	"spdsock_xmit_hiwat"},
125 	{ 0,	65536,	1024,	"spdsock_xmit_lowat"},
126 	{ 4096, 65536,	8192,	"spdsock_recv_hiwat"},
127 	{ 65536, 1024*1024*1024, 256*1024,	"spdsock_max_buf"},
128 	{ 0,	3,	0,	"spdsock_debug"},
129 };
130 #define	spds_xmit_hiwat	spds_params[0].spdsock_param_value
131 #define	spds_xmit_lowat	spds_params[1].spdsock_param_value
132 #define	spds_recv_hiwat	spds_params[2].spdsock_param_value
133 #define	spds_max_buf	spds_params[3].spdsock_param_value
134 #define	spds_debug		spds_params[4].spdsock_param_value
135 
136 #define	ss0dbg(a)	printf a
137 /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
138 #define	ss1dbg(spds, a)	if (spds->spds_debug != 0) printf a
139 #define	ss2dbg(spds, a)	if (spds->spds_debug > 1) printf a
140 #define	ss3dbg(spds, a)	if (spds->spds_debug > 2) printf a
141 
142 #define	RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \
143 	ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \
144 	(ss)->spdsock_dump_head = (iph); \
145 	(ss)->spdsock_dump_gen = (iph)->iph_gen; \
146 	(ss)->spdsock_dump_cur_type = 0; \
147 	(ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \
148 	(ss)->spdsock_dump_cur_rule = NULL; \
149 	(ss)->spdsock_dump_count = 0; \
150 	(ss)->spdsock_dump_cur_chain = 0; \
151 }
152 
153 static int spdsock_close(queue_t *);
154 static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *);
155 static void spdsock_wput(queue_t *, mblk_t *);
156 static void spdsock_wsrv(queue_t *);
157 static void spdsock_rsrv(queue_t *);
158 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns);
159 static void spdsock_stack_fini(netstackid_t stackid, void *arg);
160 static void spdsock_loadcheck(void *);
161 static void spdsock_merge_algs(spd_stack_t *);
162 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *);
163 static mblk_t *spdsock_dump_next_record(spdsock_t *);
164 
165 static struct module_info info = {
166 	5138, "spdsock", 1, INFPSZ, 512, 128
167 };
168 
169 static struct qinit rinit = {
170 	NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close,
171 	NULL, &info
172 };
173 
174 static struct qinit winit = {
175 	(pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info
176 };
177 
178 struct streamtab spdsockinfo = {
179 	&rinit, &winit
180 };
181 
182 /* mapping from alg type to protocol number, as per RFC 2407 */
183 static const uint_t algproto[] = {
184 	PROTO_IPSEC_AH,
185 	PROTO_IPSEC_ESP,
186 };
187 
188 #define	NALGPROTOS	(sizeof (algproto) / sizeof (algproto[0]))
189 
190 /* mapping from kernel exec mode to spdsock exec mode */
191 static const uint_t execmodes[] = {
192 	SPD_ALG_EXEC_MODE_SYNC,
193 	SPD_ALG_EXEC_MODE_ASYNC
194 };
195 
196 #define	NEXECMODES	(sizeof (execmodes) / sizeof (execmodes[0]))
197 
198 #define	ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1)
199 #define	ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2)
200 
201 #define	ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL)
202 
203 /* ARGSUSED */
204 static int
205 spdsock_param_get(q, mp, cp, cr)
206 	queue_t	*q;
207 	mblk_t	*mp;
208 	caddr_t	cp;
209 	cred_t *cr;
210 {
211 	spdsockparam_t	*spdsockpa = (spdsockparam_t *)cp;
212 	uint_t value;
213 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
214 	spd_stack_t	*spds = ss->spdsock_spds;
215 
216 	mutex_enter(&spds->spds_param_lock);
217 	value = spdsockpa->spdsock_param_value;
218 	mutex_exit(&spds->spds_param_lock);
219 
220 	(void) mi_mpprintf(mp, "%u", value);
221 	return (0);
222 }
223 
224 /* This routine sets an NDD variable in a spdsockparam_t structure. */
225 /* ARGSUSED */
226 static int
227 spdsock_param_set(q, mp, value, cp, cr)
228 	queue_t	*q;
229 	mblk_t	*mp;
230 	char *value;
231 	caddr_t	cp;
232 	cred_t *cr;
233 {
234 	ulong_t	new_value;
235 	spdsockparam_t	*spdsockpa = (spdsockparam_t *)cp;
236 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
237 	spd_stack_t	*spds = ss->spdsock_spds;
238 
239 	/* Convert the value from a string into a long integer. */
240 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
241 		return (EINVAL);
242 
243 	mutex_enter(&spds->spds_param_lock);
244 	/*
245 	 * Fail the request if the new value does not lie within the
246 	 * required bounds.
247 	 */
248 	if (new_value < spdsockpa->spdsock_param_min ||
249 	    new_value > spdsockpa->spdsock_param_max) {
250 		mutex_exit(&spds->spds_param_lock);
251 		return (EINVAL);
252 	}
253 
254 	/* Set the new value */
255 	spdsockpa->spdsock_param_value = new_value;
256 	mutex_exit(&spds->spds_param_lock);
257 
258 	return (0);
259 }
260 
261 /*
262  * Initialize at module load time
263  */
264 boolean_t
265 spdsock_ddi_init(void)
266 {
267 	spdsock_max_optsize = optcom_max_optsize(
268 	    spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt);
269 
270 	spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1,
271 	    NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
272 
273 	/*
274 	 * We want to be informed each time a stack is created or
275 	 * destroyed in the kernel, so we can maintain the
276 	 * set of spd_stack_t's.
277 	 */
278 	netstack_register(NS_SPDSOCK, spdsock_stack_init, NULL,
279 	    spdsock_stack_fini);
280 
281 	return (B_TRUE);
282 }
283 
284 /*
285  * Walk through the param array specified registering each element with the
286  * named dispatch handler.
287  */
288 static boolean_t
289 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt)
290 {
291 	for (; cnt-- > 0; ssp++) {
292 		if (ssp->spdsock_param_name != NULL &&
293 		    ssp->spdsock_param_name[0]) {
294 			if (!nd_load(ndp,
295 			    ssp->spdsock_param_name,
296 			    spdsock_param_get, spdsock_param_set,
297 			    (caddr_t)ssp)) {
298 				nd_free(ndp);
299 				return (B_FALSE);
300 			}
301 		}
302 	}
303 	return (B_TRUE);
304 }
305 
306 /*
307  * Initialize for each stack instance
308  */
309 /* ARGSUSED */
310 static void *
311 spdsock_stack_init(netstackid_t stackid, netstack_t *ns)
312 {
313 	spd_stack_t	*spds;
314 	spdsockparam_t	*ssp;
315 
316 	spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP);
317 	spds->spds_netstack = ns;
318 
319 	ASSERT(spds->spds_g_nd == NULL);
320 
321 	ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
322 	spds->spds_params = ssp;
323 	bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr));
324 
325 	(void) spdsock_param_register(&spds->spds_g_nd, ssp,
326 	    A_CNT(lcl_param_arr));
327 
328 	mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL);
329 	mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL);
330 
331 	return (spds);
332 }
333 
334 void
335 spdsock_ddi_destroy(void)
336 {
337 	vmem_destroy(spdsock_vmem);
338 
339 	netstack_unregister(NS_SPDSOCK);
340 }
341 
342 /* ARGSUSED */
343 static void
344 spdsock_stack_fini(netstackid_t stackid, void *arg)
345 {
346 	spd_stack_t *spds = (spd_stack_t *)arg;
347 
348 	freemsg(spds->spds_mp_algs);
349 	mutex_destroy(&spds->spds_param_lock);
350 	mutex_destroy(&spds->spds_alg_lock);
351 	nd_free(&spds->spds_g_nd);
352 	kmem_free(spds->spds_params, sizeof (lcl_param_arr));
353 	spds->spds_params = NULL;
354 
355 	kmem_free(spds, sizeof (*spds));
356 }
357 
358 /*
359  * NOTE: large quantities of this should be shared with keysock.
360  * Would be nice to combine some of this into a common module, but
361  * not possible given time pressures.
362  */
363 
364 /*
365  * High-level reality checking of extensions.
366  */
367 /* ARGSUSED */ /* XXX */
368 static boolean_t
369 ext_check(spd_ext_t *ext)
370 {
371 	spd_if_t *tunname = (spd_if_t *)ext;
372 	int i;
373 	char *idstr;
374 
375 	if (ext->spd_ext_type == SPD_EXT_TUN_NAME) {
376 		/* (NOTE:  Modified from SADB_EXT_IDENTITY..) */
377 
378 		/*
379 		 * Make sure the strings in these identities are
380 		 * null-terminated.  Let's "proactively" null-terminate the
381 		 * string at the last byte if it's not terminated sooner.
382 		 */
383 		i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t);
384 		idstr = (char *)(tunname + 1);
385 		while (*idstr != '\0' && i > 0) {
386 			i--;
387 			idstr++;
388 		}
389 		if (i == 0) {
390 			/*
391 			 * I.e., if the bozo user didn't NULL-terminate the
392 			 * string...
393 			 */
394 			idstr--;
395 			*idstr = '\0';
396 		}
397 	}
398 	return (B_TRUE);	/* For now... */
399 }
400 
401 
402 
403 /* Return values for spdsock_get_ext(). */
404 #define	KGE_OK	0
405 #define	KGE_DUP	1
406 #define	KGE_UNK	2
407 #define	KGE_LEN	3
408 #define	KGE_CHK	4
409 
410 /*
411  * Parse basic extension headers and return in the passed-in pointer vector.
412  * Return values include:
413  *
414  *	KGE_OK	Everything's nice and parsed out.
415  *		If there are no extensions, place NULL in extv[0].
416  *	KGE_DUP	There is a duplicate extension.
417  *		First instance in appropriate bin.  First duplicate in
418  *		extv[0].
419  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
420  *		unknown header.
421  *	KGE_LEN	Extension length error.
422  *	KGE_CHK	High-level reality check failed on specific extension.
423  *
424  * My apologies for some of the pointer arithmetic in here.  I'm thinking
425  * like an assembly programmer, yet trying to make the compiler happy.
426  */
427 static int
428 spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize)
429 {
430 	bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
431 
432 	/* Use extv[0] as the "current working pointer". */
433 
434 	extv[0] = (spd_ext_t *)(basehdr + 1);
435 
436 	while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
437 		/* Check for unknown headers. */
438 		if (extv[0]->spd_ext_type == 0 ||
439 		    extv[0]->spd_ext_type > SPD_EXT_MAX)
440 			return (KGE_UNK);
441 
442 		/*
443 		 * Check length.  Use uint64_t because extlen is in units
444 		 * of 64-bit words.  If length goes beyond the msgsize,
445 		 * return an error.  (Zero length also qualifies here.)
446 		 */
447 		if (extv[0]->spd_ext_len == 0 ||
448 		    (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
449 		    (void *)((uint8_t *)basehdr + msgsize))
450 			return (KGE_LEN);
451 
452 		/* Check for redundant headers. */
453 		if (extv[extv[0]->spd_ext_type] != NULL)
454 			return (KGE_DUP);
455 
456 		/*
457 		 * Reality check the extension if possible at the spdsock
458 		 * level.
459 		 */
460 		if (!ext_check(extv[0]))
461 			return (KGE_CHK);
462 
463 		/* If I make it here, assign the appropriate bin. */
464 		extv[extv[0]->spd_ext_type] = extv[0];
465 
466 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
467 		extv[0] = (spd_ext_t *)
468 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
469 	}
470 
471 	/* Everything's cool. */
472 
473 	/*
474 	 * If extv[0] == NULL, then there are no extension headers in this
475 	 * message.  Ensure that this is the case.
476 	 */
477 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
478 		extv[0] = NULL;
479 
480 	return (KGE_OK);
481 }
482 
483 static const int bad_ext_diag[] = {
484 	SPD_DIAGNOSTIC_MALFORMED_LCLPORT,
485 	SPD_DIAGNOSTIC_MALFORMED_REMPORT,
486 	SPD_DIAGNOSTIC_MALFORMED_PROTO,
487 	SPD_DIAGNOSTIC_MALFORMED_LCLADDR,
488 	SPD_DIAGNOSTIC_MALFORMED_REMADDR,
489 	SPD_DIAGNOSTIC_MALFORMED_ACTION,
490 	SPD_DIAGNOSTIC_MALFORMED_RULE,
491 	SPD_DIAGNOSTIC_MALFORMED_RULESET,
492 	SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE
493 };
494 
495 static const int dup_ext_diag[] = {
496 	SPD_DIAGNOSTIC_DUPLICATE_LCLPORT,
497 	SPD_DIAGNOSTIC_DUPLICATE_REMPORT,
498 	SPD_DIAGNOSTIC_DUPLICATE_PROTO,
499 	SPD_DIAGNOSTIC_DUPLICATE_LCLADDR,
500 	SPD_DIAGNOSTIC_DUPLICATE_REMADDR,
501 	SPD_DIAGNOSTIC_DUPLICATE_ACTION,
502 	SPD_DIAGNOSTIC_DUPLICATE_RULE,
503 	SPD_DIAGNOSTIC_DUPLICATE_RULESET,
504 	SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE
505 };
506 
507 /*
508  * Transmit a PF_POLICY error message to the instance either pointed to
509  * by ks, the instance with serial number serial, or more, depending.
510  *
511  * The faulty message (or a reasonable facsimile thereof) is in mp.
512  * This function will free mp or recycle it for delivery, thereby causing
513  * the stream head to free it.
514  */
515 static void
516 spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic)
517 {
518 	spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
519 
520 	ASSERT(mp->b_datap->db_type == M_DATA);
521 
522 	if (spmsg->spd_msg_type < SPD_MIN ||
523 	    spmsg->spd_msg_type > SPD_MAX)
524 		spmsg->spd_msg_type = SPD_RESERVED;
525 
526 	/*
527 	 * Strip out extension headers.
528 	 */
529 	ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim);
530 	mp->b_wptr = mp->b_rptr + sizeof (*spmsg);
531 	spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t));
532 	spmsg->spd_msg_errno = (uint8_t)error;
533 	spmsg->spd_msg_diagnostic = (uint16_t)diagnostic;
534 
535 	qreply(q, mp);
536 }
537 
538 static void
539 spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic)
540 {
541 	spdsock_error(q, mp, EINVAL, diagnostic);
542 }
543 
544 static void
545 spd_echo(queue_t *q, mblk_t *mp)
546 {
547 	qreply(q, mp);
548 }
549 
550 /*
551  * Do NOT consume a reference to itp.
552  */
553 /*ARGSUSED*/
554 static void
555 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns)
556 {
557 	boolean_t active = (boolean_t)cookie;
558 	ipsec_policy_head_t *iph;
559 
560 	iph = active ? itp->itp_policy : itp->itp_inactive;
561 	IPPH_REFHOLD(iph);
562 	mutex_enter(&itp->itp_lock);
563 	spdsock_flush_one(iph, ns);
564 	if (active)
565 		itp->itp_flags &= ~ITPF_PFLAGS;
566 	else
567 		itp->itp_flags &= ~ITPF_IFLAGS;
568 	mutex_exit(&itp->itp_lock);
569 }
570 
571 /*
572  * Clear out one polhead.
573  */
574 static void
575 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns)
576 {
577 	rw_enter(&iph->iph_lock, RW_WRITER);
578 	ipsec_polhead_flush(iph, ns);
579 	rw_exit(&iph->iph_lock);
580 	IPPH_REFRELE(iph, ns);
581 }
582 
583 static void
584 spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp,
585     mblk_t *mp)
586 {
587 	boolean_t active;
588 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
589 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
590 
591 	if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) {
592 		spdsock_flush_one(iph, ns);
593 		if (audit_active) {
594 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
595 			cred_t *cr;
596 			pid_t cpid;
597 
598 			cr = msg_getcred(mp, &cpid);
599 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
600 			audit_pf_policy(SPD_FLUSH, cr, ns,
601 			    ITP_NAME(itp), active, 0, cpid);
602 		}
603 	} else {
604 		active = (iph == ALL_ACTIVE_POLHEADS);
605 
606 		/* First flush the global policy. */
607 		spdsock_flush_one(active ? ipsec_system_policy(ns) :
608 		    ipsec_inactive_policy(ns), ns);
609 		if (audit_active) {
610 			cred_t *cr;
611 			pid_t cpid;
612 
613 			cr = msg_getcred(mp, &cpid);
614 			audit_pf_policy(SPD_FLUSH, cr, ns, NULL,
615 			    active, 0, cpid);
616 		}
617 		/* Then flush every tunnel's appropriate one. */
618 		itp_walk(spdsock_flush_node, (void *)active, ns);
619 		if (audit_active) {
620 			cred_t *cr;
621 			pid_t cpid;
622 
623 			cr = msg_getcred(mp, &cpid);
624 			audit_pf_policy(SPD_FLUSH, cr, ns,
625 			    "all tunnels", active, 0, cpid);
626 		}
627 	}
628 
629 	spd_echo(q, mp);
630 }
631 
632 static boolean_t
633 spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag)
634 {
635 	bzero(sel, sizeof (*sel));
636 
637 	if (extv[SPD_EXT_PROTO] != NULL) {
638 		struct spd_proto *pr =
639 		    (struct spd_proto *)extv[SPD_EXT_PROTO];
640 		sel->ipsl_proto = pr->spd_proto_number;
641 		sel->ipsl_valid |= IPSL_PROTOCOL;
642 	}
643 	if (extv[SPD_EXT_LCLPORT] != NULL) {
644 		struct spd_portrange *pr =
645 		    (struct spd_portrange *)extv[SPD_EXT_LCLPORT];
646 		sel->ipsl_lport = pr->spd_ports_minport;
647 		sel->ipsl_valid |= IPSL_LOCAL_PORT;
648 	}
649 	if (extv[SPD_EXT_REMPORT] != NULL) {
650 		struct spd_portrange *pr =
651 		    (struct spd_portrange *)extv[SPD_EXT_REMPORT];
652 		sel->ipsl_rport = pr->spd_ports_minport;
653 		sel->ipsl_valid |= IPSL_REMOTE_PORT;
654 	}
655 
656 	if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) {
657 		struct spd_typecode *tc=
658 		    (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE];
659 
660 		sel->ipsl_valid |= IPSL_ICMP_TYPE;
661 		sel->ipsl_icmp_type = tc->spd_typecode_type;
662 		if (tc->spd_typecode_type_end < tc->spd_typecode_type)
663 			sel->ipsl_icmp_type_end = tc->spd_typecode_type;
664 		else
665 			sel->ipsl_icmp_type_end = tc->spd_typecode_type_end;
666 
667 		if (tc->spd_typecode_code != 255) {
668 			sel->ipsl_valid |= IPSL_ICMP_CODE;
669 			sel->ipsl_icmp_code = tc->spd_typecode_code;
670 			if (tc->spd_typecode_code_end < tc->spd_typecode_code)
671 				sel->ipsl_icmp_code_end = tc->spd_typecode_code;
672 			else
673 				sel->ipsl_icmp_code_end =
674 				    tc->spd_typecode_code_end;
675 		}
676 	}
677 #define	ADDR2SEL(sel, extv, field, pfield, extn, bit)			      \
678 	if ((extv)[(extn)] != NULL) {					      \
679 		uint_t addrlen;						      \
680 		struct spd_address *ap = 				      \
681 			(struct spd_address *)((extv)[(extn)]); 	      \
682 		addrlen = (ap->spd_address_af == AF_INET6) ? 		      \
683 			IPV6_ADDR_LEN : IP_ADDR_LEN;			      \
684 		if (SPD_64TO8(ap->spd_address_len) < 			      \
685 			(addrlen + sizeof (*ap))) {			      \
686 			*diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN;		      \
687 			return (B_FALSE);				      \
688 		}							      \
689 		bcopy((ap+1), &((sel)->field), addrlen);		      \
690 		(sel)->pfield = ap->spd_address_prefixlen;		      \
691 		(sel)->ipsl_valid |= (bit);				      \
692 		(sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ?	      \
693 			IPSL_IPV6 : IPSL_IPV4;				      \
694 	}
695 
696 	ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen,
697 	    SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR);
698 	ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen,
699 	    SPD_EXT_REMADDR, IPSL_REMOTE_ADDR);
700 
701 	if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) ==
702 	    (IPSL_IPV6|IPSL_IPV4)) {
703 		*diag = SPD_DIAGNOSTIC_MIXED_AF;
704 		return (B_FALSE);
705 	}
706 
707 #undef ADDR2SEL
708 
709 	return (B_TRUE);
710 }
711 
712 static boolean_t
713 spd_convert_type(uint32_t type, ipsec_act_t *act)
714 {
715 	switch (type) {
716 	case SPD_ACTTYPE_DROP:
717 		act->ipa_type = IPSEC_ACT_DISCARD;
718 		return (B_TRUE);
719 
720 	case SPD_ACTTYPE_PASS:
721 		act->ipa_type = IPSEC_ACT_CLEAR;
722 		return (B_TRUE);
723 
724 	case SPD_ACTTYPE_IPSEC:
725 		act->ipa_type = IPSEC_ACT_APPLY;
726 		return (B_TRUE);
727 	}
728 	return (B_FALSE);
729 }
730 
731 static boolean_t
732 spd_convert_flags(uint32_t flags, ipsec_act_t *act)
733 {
734 	/*
735 	 * Note use of !! for boolean canonicalization.
736 	 */
737 	act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH);
738 	act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP);
739 	act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA);
740 	act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE);
741 	act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE);
742 	return (B_TRUE);
743 }
744 
745 static void
746 spdsock_reset_act(ipsec_act_t *act)
747 {
748 	bzero(act, sizeof (*act));
749 	act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS;
750 	act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS;
751 	act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS;
752 }
753 
754 /*
755  * Sanity check action against reality, and shrink-wrap key sizes..
756  */
757 static boolean_t
758 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag,
759     spd_stack_t *spds)
760 {
761 	if (tunnel_polhead && act->ipa_apply.ipp_use_unique) {
762 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
763 		return (B_FALSE);
764 	}
765 	if ((act->ipa_type != IPSEC_ACT_APPLY) &&
766 	    (act->ipa_apply.ipp_use_ah ||
767 	    act->ipa_apply.ipp_use_esp ||
768 	    act->ipa_apply.ipp_use_espa ||
769 	    act->ipa_apply.ipp_use_se ||
770 	    act->ipa_apply.ipp_use_unique)) {
771 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
772 		return (B_FALSE);
773 	}
774 	if ((act->ipa_type == IPSEC_ACT_APPLY) &&
775 	    !act->ipa_apply.ipp_use_ah &&
776 	    !act->ipa_apply.ipp_use_esp) {
777 		*diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
778 		return (B_FALSE);
779 	}
780 	return (ipsec_check_action(act, diag, spds->spds_netstack));
781 }
782 
783 /*
784  * We may be short a few error checks here..
785  */
786 static boolean_t
787 spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp,
788     int *diag, spd_stack_t *spds)
789 {
790 	struct spd_ext_actions *sactp =
791 	    (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
792 	ipsec_act_t act, *actp, *endactp;
793 	struct spd_attribute *attrp, *endattrp;
794 	uint64_t *endp;
795 	int nact;
796 	boolean_t tunnel_polhead;
797 
798 	tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL &&
799 	    (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags &
800 	    SPD_RULE_FLAG_TUNNEL));
801 
802 	*actpp = NULL;
803 	*nactp = 0;
804 
805 	if (sactp == NULL) {
806 		*diag = SPD_DIAGNOSTIC_NO_ACTION_EXT;
807 		return (B_FALSE);
808 	}
809 
810 	/*
811 	 * Parse the "action" extension and convert into an action chain.
812 	 */
813 
814 	nact = sactp->spd_actions_count;
815 
816 	endp = (uint64_t *)sactp;
817 	endp += sactp->spd_actions_len;
818 	endattrp = (struct spd_attribute *)endp;
819 
820 	actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP);
821 	if (actp == NULL) {
822 		*diag = SPD_DIAGNOSTIC_ADD_NO_MEM;
823 		return (B_FALSE);
824 	}
825 	*actpp = actp;
826 	*nactp = nact;
827 	endactp = actp + nact;
828 
829 	spdsock_reset_act(&act);
830 	attrp = (struct spd_attribute *)(&sactp[1]);
831 
832 	for (; attrp < endattrp; attrp++) {
833 		switch (attrp->spd_attr_tag) {
834 		case SPD_ATTR_NOP:
835 			break;
836 
837 		case SPD_ATTR_EMPTY:
838 			spdsock_reset_act(&act);
839 			break;
840 
841 		case SPD_ATTR_END:
842 			attrp = endattrp;
843 			/* FALLTHRU */
844 		case SPD_ATTR_NEXT:
845 			if (actp >= endactp) {
846 				*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
847 				goto fail;
848 			}
849 			if (!spdsock_check_action(&act, tunnel_polhead,
850 			    diag, spds))
851 				goto fail;
852 			*actp++ = act;
853 			spdsock_reset_act(&act);
854 			break;
855 
856 		case SPD_ATTR_TYPE:
857 			if (!spd_convert_type(attrp->spd_attr_value, &act)) {
858 				*diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE;
859 				goto fail;
860 			}
861 			break;
862 
863 		case SPD_ATTR_FLAGS:
864 			if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) {
865 				/*
866 				 * Set "sa unique" for transport-mode
867 				 * tunnels whether we want to or not.
868 				 */
869 				attrp->spd_attr_value |= SPD_APPLY_UNIQUE;
870 			}
871 			if (!spd_convert_flags(attrp->spd_attr_value, &act)) {
872 				*diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS;
873 				goto fail;
874 			}
875 			break;
876 
877 		case SPD_ATTR_AH_AUTH:
878 			if (attrp->spd_attr_value == 0) {
879 				*diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG;
880 				goto fail;
881 			}
882 			act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value;
883 			break;
884 
885 		case SPD_ATTR_ESP_ENCR:
886 			if (attrp->spd_attr_value == 0) {
887 				*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG;
888 				goto fail;
889 			}
890 			act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value;
891 			break;
892 
893 		case SPD_ATTR_ESP_AUTH:
894 			if (attrp->spd_attr_value == 0) {
895 				*diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG;
896 				goto fail;
897 			}
898 			act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value;
899 			break;
900 
901 		case SPD_ATTR_ENCR_MINBITS:
902 			act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value;
903 			break;
904 
905 		case SPD_ATTR_ENCR_MAXBITS:
906 			act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value;
907 			break;
908 
909 		case SPD_ATTR_AH_MINBITS:
910 			act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value;
911 			break;
912 
913 		case SPD_ATTR_AH_MAXBITS:
914 			act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value;
915 			break;
916 
917 		case SPD_ATTR_ESPA_MINBITS:
918 			act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value;
919 			break;
920 
921 		case SPD_ATTR_ESPA_MAXBITS:
922 			act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value;
923 			break;
924 
925 		case SPD_ATTR_LIFE_SOFT_TIME:
926 		case SPD_ATTR_LIFE_HARD_TIME:
927 		case SPD_ATTR_LIFE_SOFT_BYTES:
928 		case SPD_ATTR_LIFE_HARD_BYTES:
929 			break;
930 
931 		case SPD_ATTR_KM_PROTO:
932 			act.ipa_apply.ipp_km_proto = attrp->spd_attr_value;
933 			break;
934 
935 		case SPD_ATTR_KM_COOKIE:
936 			act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value;
937 			break;
938 
939 		case SPD_ATTR_REPLAY_DEPTH:
940 			act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value;
941 			break;
942 		}
943 	}
944 	if (actp != endactp) {
945 		*diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
946 		goto fail;
947 	}
948 
949 	return (B_TRUE);
950 fail:
951 	ipsec_actvec_free(*actpp, nact);
952 	*actpp = NULL;
953 	return (B_FALSE);
954 }
955 
956 typedef struct
957 {
958 	ipsec_policy_t *pol;
959 	int dir;
960 } tmprule_t;
961 
962 static int
963 mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule,
964     ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af,
965     tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
966 {
967 	ipsec_policy_t *pol;
968 
969 	sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4);
970 	sel->ipsl_valid |= af;
971 
972 	pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority,
973 	    index, spds->spds_netstack);
974 	if (pol == NULL)
975 		return (ENOMEM);
976 
977 	(*rp)->pol = pol;
978 	(*rp)->dir = dir;
979 	(*rp)++;
980 
981 	if (!ipsec_check_policy(iph, pol, dir))
982 		return (EEXIST);
983 
984 	rule->spd_rule_index = pol->ipsp_index;
985 	return (0);
986 }
987 
988 static int
989 mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule,
990     ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs,
991     tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
992 {
993 	int error;
994 
995 	if (afs & IPSL_IPV4) {
996 		error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp,
997 		    index, spds);
998 		if (error != 0)
999 			return (error);
1000 	}
1001 	if (afs & IPSL_IPV6) {
1002 		error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp,
1003 		    index, spds);
1004 		if (error != 0)
1005 			return (error);
1006 	}
1007 	return (0);
1008 }
1009 
1010 
1011 static void
1012 spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
1013     spd_ext_t **extv, ipsec_tun_pol_t *itp)
1014 {
1015 	ipsec_selkey_t sel;
1016 	ipsec_act_t *actp;
1017 	uint_t nact;
1018 	int diag = 0, error, afs;
1019 	struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
1020 	tmprule_t rules[4], *rulep = &rules[0];
1021 	boolean_t tunnel_mode, empty_itp, active;
1022 	uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index;
1023 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
1024 	spd_stack_t	*spds = ss->spdsock_spds;
1025 
1026 	if (rule == NULL) {
1027 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
1028 		if (audit_active) {
1029 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1030 			cred_t *cr;
1031 			pid_t cpid;
1032 
1033 			cr = msg_getcred(mp, &cpid);
1034 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1035 			audit_pf_policy(SPD_ADDRULE, cr,
1036 			    spds->spds_netstack, ITP_NAME(itp), active,
1037 			    SPD_DIAGNOSTIC_NO_RULE_EXT, cpid);
1038 		}
1039 		return;
1040 	}
1041 
1042 	tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL);
1043 
1044 	if (itp != NULL) {
1045 		mutex_enter(&itp->itp_lock);
1046 		ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph);
1047 		active = (itp->itp_policy == iph);
1048 		if (ITP_P_ISACTIVE(itp, iph)) {
1049 			/* Check for mix-and-match of tunnel/transport. */
1050 			if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) ||
1051 			    (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) {
1052 				mutex_exit(&itp->itp_lock);
1053 				spdsock_error(q, mp, EBUSY, 0);
1054 				return;
1055 			}
1056 			empty_itp = B_FALSE;
1057 		} else {
1058 			empty_itp = B_TRUE;
1059 			itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE;
1060 			if (tunnel_mode)
1061 				itp->itp_flags |= active ? ITPF_P_TUNNEL :
1062 				    ITPF_I_TUNNEL;
1063 		}
1064 	} else {
1065 		empty_itp = B_FALSE;
1066 	}
1067 
1068 	if (rule->spd_rule_index != 0) {
1069 		diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX;
1070 		error = EINVAL;
1071 		goto fail2;
1072 	}
1073 
1074 	if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
1075 		error = EINVAL;
1076 		goto fail2;
1077 	}
1078 
1079 	if (itp != NULL) {
1080 		if (tunnel_mode) {
1081 			if (sel.ipsl_valid &
1082 			    (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) {
1083 				itp->itp_flags |= active ?
1084 				    ITPF_P_PER_PORT_SECURITY :
1085 				    ITPF_I_PER_PORT_SECURITY;
1086 			}
1087 		} else {
1088 			/*
1089 			 * For now, we don't allow transport-mode on a tunnel
1090 			 * with ANY specific selectors.  Bail if we have such
1091 			 * a request.
1092 			 */
1093 			if (sel.ipsl_valid & IPSL_WILDCARD) {
1094 				diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS;
1095 				error = EINVAL;
1096 				goto fail2;
1097 			}
1098 		}
1099 	}
1100 
1101 	if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) {
1102 		error = EINVAL;
1103 		goto fail2;
1104 	}
1105 	/*
1106 	 * If no addresses were specified, add both.
1107 	 */
1108 	afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4);
1109 	if (afs == 0)
1110 		afs = (IPSL_IPV6|IPSL_IPV4);
1111 
1112 	rw_enter(&iph->iph_lock, RW_WRITER);
1113 
1114 	if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) {
1115 		error = mkrulepair(iph, rule, &sel, actp, nact,
1116 		    IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds);
1117 		if (error != 0)
1118 			goto fail;
1119 	}
1120 
1121 	if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) {
1122 		error = mkrulepair(iph, rule, &sel, actp, nact,
1123 		    IPSEC_TYPE_INBOUND, afs, &rulep, index, spds);
1124 		if (error != 0)
1125 			goto fail;
1126 	}
1127 
1128 	while ((--rulep) >= &rules[0]) {
1129 		ipsec_enter_policy(iph, rulep->pol, rulep->dir,
1130 		    spds->spds_netstack);
1131 	}
1132 	rw_exit(&iph->iph_lock);
1133 	if (itp != NULL)
1134 		mutex_exit(&itp->itp_lock);
1135 
1136 	ipsec_actvec_free(actp, nact);
1137 	spd_echo(q, mp);
1138 	if (audit_active) {
1139 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1140 		cred_t *cr;
1141 		pid_t cpid;
1142 
1143 		cr = msg_getcred(mp, &cpid);
1144 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1145 		audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1146 		    ITP_NAME(itp), active, 0, cpid);
1147 	}
1148 	return;
1149 
1150 fail:
1151 	rw_exit(&iph->iph_lock);
1152 	while ((--rulep) >= &rules[0])
1153 		IPPOL_REFRELE(rulep->pol);
1154 	ipsec_actvec_free(actp, nact);
1155 fail2:
1156 	if (itp != NULL) {
1157 		if (empty_itp)
1158 			itp->itp_flags = 0;
1159 		mutex_exit(&itp->itp_lock);
1160 	}
1161 	spdsock_error(q, mp, error, diag);
1162 	if (audit_active) {
1163 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1164 		cred_t *cr;
1165 		pid_t cpid;
1166 
1167 		cr = msg_getcred(mp, &cpid);
1168 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1169 		audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1170 		    ITP_NAME(itp), active, error, cpid);
1171 	}
1172 }
1173 
1174 void
1175 spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
1176     spd_ext_t **extv, ipsec_tun_pol_t *itp)
1177 {
1178 	ipsec_selkey_t sel;
1179 	struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
1180 	int err, diag = 0;
1181 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
1182 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
1183 
1184 	if (rule == NULL) {
1185 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
1186 		if (audit_active) {
1187 			boolean_t active;
1188 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1189 			cred_t *cr;
1190 			pid_t cpid;
1191 
1192 			cr = msg_getcred(mp, &cpid);
1193 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1194 			audit_pf_policy(SPD_DELETERULE, cr, ns,
1195 			    ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT,
1196 			    cpid);
1197 		}
1198 		return;
1199 	}
1200 
1201 	/*
1202 	 * Must enter itp_lock first to avoid deadlock.  See tun.c's
1203 	 * set_sec_simple() for the other case of itp_lock and iph_lock.
1204 	 */
1205 	if (itp != NULL)
1206 		mutex_enter(&itp->itp_lock);
1207 
1208 	if (rule->spd_rule_index != 0) {
1209 		if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) !=
1210 		    0) {
1211 			err = ESRCH;
1212 			goto fail;
1213 		}
1214 	} else {
1215 		if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
1216 			err = EINVAL;	/* diag already set... */
1217 			goto fail;
1218 		}
1219 
1220 		if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) &&
1221 		    !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) {
1222 			err = ESRCH;
1223 			goto fail;
1224 		}
1225 
1226 		if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) &&
1227 		    !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) {
1228 			err = ESRCH;
1229 			goto fail;
1230 		}
1231 	}
1232 
1233 	if (itp != NULL) {
1234 		ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive);
1235 		rw_enter(&iph->iph_lock, RW_READER);
1236 		if (avl_numnodes(&iph->iph_rulebyid) == 0) {
1237 			if (iph == itp->itp_policy)
1238 				itp->itp_flags &= ~ITPF_PFLAGS;
1239 			else
1240 				itp->itp_flags &= ~ITPF_IFLAGS;
1241 		}
1242 		/* Can exit locks in any order. */
1243 		rw_exit(&iph->iph_lock);
1244 		mutex_exit(&itp->itp_lock);
1245 	}
1246 	spd_echo(q, mp);
1247 	if (audit_active) {
1248 		boolean_t active;
1249 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1250 		cred_t *cr;
1251 		pid_t cpid;
1252 
1253 		cr = msg_getcred(mp, &cpid);
1254 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1255 		audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1256 		    active, 0, cpid);
1257 	}
1258 	return;
1259 fail:
1260 	if (itp != NULL)
1261 		mutex_exit(&itp->itp_lock);
1262 	spdsock_error(q, mp, err, diag);
1263 	if (audit_active) {
1264 		boolean_t active;
1265 		spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1266 		cred_t *cr;
1267 		pid_t cpid;
1268 
1269 		cr = msg_getcred(mp, &cpid);
1270 		active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1271 		audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1272 		    active, err, cpid);
1273 	}
1274 }
1275 
1276 /* Do NOT consume a reference to itp. */
1277 /* ARGSUSED */
1278 static void
1279 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns)
1280 {
1281 	mutex_enter(&itp->itp_lock);
1282 	ITPF_SWAP(itp->itp_flags);
1283 	ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns);
1284 	mutex_exit(&itp->itp_lock);
1285 }
1286 
1287 void
1288 spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname)
1289 {
1290 	char *tname;
1291 	ipsec_tun_pol_t *itp;
1292 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
1293 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
1294 
1295 	if (tunname != NULL) {
1296 		tname = (char *)tunname->spd_if_name;
1297 		if (*tname == '\0') {
1298 			/* can't fail */
1299 			ipsec_swap_global_policy(ns);
1300 			if (audit_active) {
1301 				boolean_t active;
1302 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1303 				cred_t *cr;
1304 				pid_t cpid;
1305 
1306 				cr = msg_getcred(mp, &cpid);
1307 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1308 				audit_pf_policy(SPD_FLIP, cr, ns,
1309 				    NULL, active, 0, cpid);
1310 			}
1311 			itp_walk(spdsock_flip_node, NULL, ns);
1312 			if (audit_active) {
1313 				boolean_t active;
1314 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1315 				cred_t *cr;
1316 				pid_t cpid;
1317 
1318 				cr = msg_getcred(mp, &cpid);
1319 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1320 				audit_pf_policy(SPD_FLIP, cr, ns,
1321 				    "all tunnels", active, 0, cpid);
1322 			}
1323 		} else {
1324 			itp = get_tunnel_policy(tname, ns);
1325 			if (itp == NULL) {
1326 				/* Better idea for "tunnel not found"? */
1327 				spdsock_error(q, mp, ESRCH, 0);
1328 				if (audit_active) {
1329 					boolean_t active;
1330 					spd_msg_t *spmsg =
1331 					    (spd_msg_t *)mp->b_rptr;
1332 					cred_t *cr;
1333 					pid_t cpid;
1334 
1335 					cr = msg_getcred(mp, &cpid);
1336 					active = (spmsg->spd_msg_spdid ==
1337 					    SPD_ACTIVE);
1338 					audit_pf_policy(SPD_FLIP, cr, ns,
1339 					    ITP_NAME(itp), active,
1340 					    ESRCH, cpid);
1341 				}
1342 				return;
1343 			}
1344 			spdsock_flip_node(itp, NULL, NULL);
1345 			if (audit_active) {
1346 				boolean_t active;
1347 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1348 				cred_t *cr;
1349 				pid_t cpid;
1350 
1351 				cr = msg_getcred(mp, &cpid);
1352 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1353 				audit_pf_policy(SPD_FLIP, cr, ns,
1354 				    ITP_NAME(itp), active, 0, cpid);
1355 			}
1356 			ITP_REFRELE(itp, ns);
1357 		}
1358 	} else {
1359 		ipsec_swap_global_policy(ns);	/* can't fail */
1360 		if (audit_active) {
1361 			boolean_t active;
1362 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1363 			cred_t *cr;
1364 			pid_t cpid;
1365 
1366 			cr = msg_getcred(mp, &cpid);
1367 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1368 			audit_pf_policy(SPD_FLIP, cr,
1369 			    ns, NULL, active, 0, cpid);
1370 		}
1371 	}
1372 	spd_echo(q, mp);
1373 }
1374 
1375 /*
1376  * Unimplemented feature
1377  */
1378 /* ARGSUSED */
1379 static void
1380 spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
1381     spd_ext_t **extv, ipsec_tun_pol_t *itp)
1382 {
1383 	spdsock_error(q, mp, EINVAL, 0);
1384 }
1385 
1386 
1387 static mblk_t *
1388 spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph,
1389     uint32_t count, uint16_t error)
1390 {
1391 	size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t);
1392 	spd_msg_t *msg;
1393 	spd_ruleset_ext_t *ruleset;
1394 	mblk_t *m = allocb(len, BPRI_HI);
1395 
1396 	ASSERT(RW_READ_HELD(&iph->iph_lock));
1397 
1398 	if (m == NULL) {
1399 		return (NULL);
1400 	}
1401 	msg = (spd_msg_t *)m->b_rptr;
1402 	ruleset = (spd_ruleset_ext_t *)(&msg[1]);
1403 
1404 	m->b_wptr = (uint8_t *)&ruleset[1];
1405 
1406 	*msg = *(spd_msg_t *)(req->b_rptr);
1407 	msg->spd_msg_len = SPD_8TO64(len);
1408 	msg->spd_msg_errno = error;
1409 
1410 	ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset));
1411 	ruleset->spd_ruleset_type = SPD_EXT_RULESET;
1412 	ruleset->spd_ruleset_count = count;
1413 	ruleset->spd_ruleset_version = iph->iph_gen;
1414 	return (m);
1415 }
1416 
1417 static mblk_t *
1418 spdsock_dump_finish(spdsock_t *ss, int error)
1419 {
1420 	mblk_t *m;
1421 	ipsec_policy_head_t *iph = ss->spdsock_dump_head;
1422 	mblk_t *req = ss->spdsock_dump_req;
1423 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
1424 
1425 	rw_enter(&iph->iph_lock, RW_READER);
1426 	m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error);
1427 	rw_exit(&iph->iph_lock);
1428 	IPPH_REFRELE(iph, ns);
1429 	if (ss->spdsock_itp != NULL) {
1430 		ITP_REFRELE(ss->spdsock_itp, ns);
1431 		ss->spdsock_itp = NULL;
1432 	}
1433 	ss->spdsock_dump_req = NULL;
1434 	freemsg(req);
1435 
1436 	return (m);
1437 }
1438 
1439 /*
1440  * Rule encoding functions.
1441  * We do a two-pass encode.
1442  * If base != NULL, fill in encoded rule part starting at base+offset.
1443  * Always return "offset" plus length of to-be-encoded data.
1444  */
1445 static uint_t
1446 spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type,
1447     uint8_t type_end, uint8_t code, uint8_t code_end)
1448 {
1449 	struct spd_typecode *tcp;
1450 
1451 	ASSERT(ALIGNED64(offset));
1452 
1453 	if (base != NULL) {
1454 		tcp = (struct spd_typecode *)(base + offset);
1455 		tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp));
1456 		tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE;
1457 		tcp->spd_typecode_code = code;
1458 		tcp->spd_typecode_type = type;
1459 		tcp->spd_typecode_type_end = type_end;
1460 		tcp->spd_typecode_code_end = code_end;
1461 	}
1462 	offset += sizeof (*tcp);
1463 
1464 	ASSERT(ALIGNED64(offset));
1465 
1466 	return (offset);
1467 }
1468 
1469 static uint_t
1470 spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto)
1471 {
1472 	struct spd_proto *spp;
1473 
1474 	ASSERT(ALIGNED64(offset));
1475 
1476 	if (base != NULL) {
1477 		spp = (struct spd_proto *)(base + offset);
1478 		spp->spd_proto_len = SPD_8TO64(sizeof (*spp));
1479 		spp->spd_proto_exttype = SPD_EXT_PROTO;
1480 		spp->spd_proto_number = proto;
1481 		spp->spd_proto_reserved1 = 0;
1482 		spp->spd_proto_reserved2 = 0;
1483 	}
1484 	offset += sizeof (*spp);
1485 
1486 	ASSERT(ALIGNED64(offset));
1487 
1488 	return (offset);
1489 }
1490 
1491 static uint_t
1492 spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port)
1493 {
1494 	struct spd_portrange *spp;
1495 
1496 	ASSERT(ALIGNED64(offset));
1497 
1498 	if (base != NULL) {
1499 		spp = (struct spd_portrange *)(base + offset);
1500 		spp->spd_ports_len = SPD_8TO64(sizeof (*spp));
1501 		spp->spd_ports_exttype = ext;
1502 		spp->spd_ports_minport = port;
1503 		spp->spd_ports_maxport = port;
1504 	}
1505 	offset += sizeof (*spp);
1506 
1507 	ASSERT(ALIGNED64(offset));
1508 
1509 	return (offset);
1510 }
1511 
1512 static uint_t
1513 spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext,
1514     const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen)
1515 {
1516 	struct spd_address *sae;
1517 	ipsec_addr_t *spdaddr;
1518 	uint_t start = offset;
1519 	uint_t addrlen;
1520 	uint_t af;
1521 
1522 	if (sel->ipsl_valid & IPSL_IPV4) {
1523 		af = AF_INET;
1524 		addrlen = IP_ADDR_LEN;
1525 	} else {
1526 		af = AF_INET6;
1527 		addrlen = IPV6_ADDR_LEN;
1528 	}
1529 
1530 	ASSERT(ALIGNED64(offset));
1531 
1532 	if (base != NULL) {
1533 		sae = (struct spd_address *)(base + offset);
1534 		sae->spd_address_exttype = ext;
1535 		sae->spd_address_af = af;
1536 		sae->spd_address_prefixlen = pfxlen;
1537 		sae->spd_address_reserved2 = 0;
1538 
1539 		spdaddr = (ipsec_addr_t *)(&sae[1]);
1540 		bcopy(addr, spdaddr, addrlen);
1541 	}
1542 	offset += sizeof (*sae);
1543 	addrlen = roundup(addrlen, sizeof (uint64_t));
1544 	offset += addrlen;
1545 
1546 	ASSERT(ALIGNED64(offset));
1547 
1548 	if (base != NULL)
1549 		sae->spd_address_len = SPD_8TO64(offset - start);
1550 	return (offset);
1551 }
1552 
1553 static uint_t
1554 spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel)
1555 {
1556 	const ipsec_selkey_t *selkey = &sel->ipsl_key;
1557 
1558 	if (selkey->ipsl_valid & IPSL_PROTOCOL)
1559 		offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto);
1560 	if (selkey->ipsl_valid & IPSL_LOCAL_PORT)
1561 		offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT,
1562 		    selkey->ipsl_lport);
1563 	if (selkey->ipsl_valid & IPSL_REMOTE_PORT)
1564 		offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT,
1565 		    selkey->ipsl_rport);
1566 	if (selkey->ipsl_valid & IPSL_REMOTE_ADDR)
1567 		offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR,
1568 		    selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen);
1569 	if (selkey->ipsl_valid & IPSL_LOCAL_ADDR)
1570 		offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR,
1571 		    selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen);
1572 	if (selkey->ipsl_valid & IPSL_ICMP_TYPE) {
1573 		offset = spdsock_encode_typecode(base, offset,
1574 		    selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end,
1575 		    (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
1576 		    selkey->ipsl_icmp_code : 255,
1577 		    (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
1578 		    selkey->ipsl_icmp_code_end : 255);
1579 	}
1580 	return (offset);
1581 }
1582 
1583 static uint_t
1584 spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag,
1585     uint32_t value)
1586 {
1587 	struct spd_attribute *attr;
1588 
1589 	ASSERT(ALIGNED64(offset));
1590 
1591 	if (base != NULL) {
1592 		attr = (struct spd_attribute *)(base + offset);
1593 		attr->spd_attr_tag = tag;
1594 		attr->spd_attr_value = value;
1595 	}
1596 	offset += sizeof (struct spd_attribute);
1597 
1598 	ASSERT(ALIGNED64(offset));
1599 
1600 	return (offset);
1601 }
1602 
1603 
1604 #define	EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v))
1605 
1606 static uint_t
1607 spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap)
1608 {
1609 	const struct ipsec_act *act = &(ap->ipa_act);
1610 	uint_t flags;
1611 
1612 	EMIT(SPD_ATTR_EMPTY, 0);
1613 	switch (act->ipa_type) {
1614 	case IPSEC_ACT_DISCARD:
1615 	case IPSEC_ACT_REJECT:
1616 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP);
1617 		break;
1618 	case IPSEC_ACT_BYPASS:
1619 	case IPSEC_ACT_CLEAR:
1620 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS);
1621 		break;
1622 
1623 	case IPSEC_ACT_APPLY:
1624 		EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC);
1625 		flags = 0;
1626 		if (act->ipa_apply.ipp_use_ah)
1627 			flags |= SPD_APPLY_AH;
1628 		if (act->ipa_apply.ipp_use_esp)
1629 			flags |= SPD_APPLY_ESP;
1630 		if (act->ipa_apply.ipp_use_espa)
1631 			flags |= SPD_APPLY_ESPA;
1632 		if (act->ipa_apply.ipp_use_se)
1633 			flags |= SPD_APPLY_SE;
1634 		if (act->ipa_apply.ipp_use_unique)
1635 			flags |= SPD_APPLY_UNIQUE;
1636 		EMIT(SPD_ATTR_FLAGS, flags);
1637 		if (flags & SPD_APPLY_AH) {
1638 			EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg);
1639 			EMIT(SPD_ATTR_AH_MINBITS,
1640 			    act->ipa_apply.ipp_ah_minbits);
1641 			EMIT(SPD_ATTR_AH_MAXBITS,
1642 			    act->ipa_apply.ipp_ah_maxbits);
1643 		}
1644 		if (flags & SPD_APPLY_ESP) {
1645 			EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg);
1646 			EMIT(SPD_ATTR_ENCR_MINBITS,
1647 			    act->ipa_apply.ipp_espe_minbits);
1648 			EMIT(SPD_ATTR_ENCR_MAXBITS,
1649 			    act->ipa_apply.ipp_espe_maxbits);
1650 			if (flags & SPD_APPLY_ESPA) {
1651 				EMIT(SPD_ATTR_ESP_AUTH,
1652 				    act->ipa_apply.ipp_esp_auth_alg);
1653 				EMIT(SPD_ATTR_ESPA_MINBITS,
1654 				    act->ipa_apply.ipp_espa_minbits);
1655 				EMIT(SPD_ATTR_ESPA_MAXBITS,
1656 				    act->ipa_apply.ipp_espa_maxbits);
1657 			}
1658 		}
1659 		if (act->ipa_apply.ipp_km_proto != 0)
1660 			EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto);
1661 		if (act->ipa_apply.ipp_km_cookie != 0)
1662 			EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie);
1663 		if (act->ipa_apply.ipp_replay_depth != 0)
1664 			EMIT(SPD_ATTR_REPLAY_DEPTH,
1665 			    act->ipa_apply.ipp_replay_depth);
1666 		/* Add more here */
1667 		break;
1668 	}
1669 
1670 	return (offset);
1671 }
1672 
1673 static uint_t
1674 spdsock_encode_action_list(uint8_t *base, uint_t offset,
1675     const ipsec_action_t *ap)
1676 {
1677 	struct spd_ext_actions *act;
1678 	uint_t nact = 0;
1679 	uint_t start = offset;
1680 
1681 	ASSERT(ALIGNED64(offset));
1682 
1683 	if (base != NULL) {
1684 		act = (struct spd_ext_actions *)(base + offset);
1685 		act->spd_actions_len = 0;
1686 		act->spd_actions_exttype = SPD_EXT_ACTION;
1687 		act->spd_actions_count = 0;
1688 		act->spd_actions_reserved = 0;
1689 	}
1690 
1691 	offset += sizeof (*act);
1692 
1693 	ASSERT(ALIGNED64(offset));
1694 
1695 	while (ap != NULL) {
1696 		offset = spdsock_encode_action(base, offset, ap);
1697 		ap = ap->ipa_next;
1698 		nact++;
1699 		if (ap != NULL) {
1700 			EMIT(SPD_ATTR_NEXT, 0);
1701 		}
1702 	}
1703 	EMIT(SPD_ATTR_END, 0);
1704 
1705 	ASSERT(ALIGNED64(offset));
1706 
1707 	if (base != NULL) {
1708 		act->spd_actions_count = nact;
1709 		act->spd_actions_len = SPD_8TO64(offset - start);
1710 	}
1711 
1712 	return (offset);
1713 }
1714 
1715 #undef EMIT
1716 
1717 /* ARGSUSED */
1718 static uint_t
1719 spdsock_rule_flags(uint_t dir, uint_t af)
1720 {
1721 	uint_t flags = 0;
1722 
1723 	if (dir == IPSEC_TYPE_INBOUND)
1724 		flags |= SPD_RULE_FLAG_INBOUND;
1725 	if (dir == IPSEC_TYPE_OUTBOUND)
1726 		flags |= SPD_RULE_FLAG_OUTBOUND;
1727 
1728 	return (flags);
1729 }
1730 
1731 
1732 static uint_t
1733 spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req,
1734     const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name,
1735     boolean_t tunnel)
1736 {
1737 	struct spd_msg *spmsg;
1738 	struct spd_rule *spr;
1739 	spd_if_t *sid;
1740 
1741 	uint_t start = offset;
1742 
1743 	ASSERT(ALIGNED64(offset));
1744 
1745 	if (base != NULL) {
1746 		spmsg = (struct spd_msg *)(base + offset);
1747 		bzero(spmsg, sizeof (*spmsg));
1748 		spmsg->spd_msg_version = PF_POLICY_V1;
1749 		spmsg->spd_msg_type = SPD_DUMP;
1750 		spmsg->spd_msg_seq = req->spd_msg_seq;
1751 		spmsg->spd_msg_pid = req->spd_msg_pid;
1752 	}
1753 	offset += sizeof (struct spd_msg);
1754 
1755 	ASSERT(ALIGNED64(offset));
1756 
1757 	if (base != NULL) {
1758 		spr = (struct spd_rule *)(base + offset);
1759 		spr->spd_rule_type = SPD_EXT_RULE;
1760 		spr->spd_rule_priority = rule->ipsp_prio;
1761 		spr->spd_rule_flags = spdsock_rule_flags(dir, af);
1762 		if (tunnel)
1763 			spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL;
1764 		spr->spd_rule_unused = 0;
1765 		spr->spd_rule_len = SPD_8TO64(sizeof (*spr));
1766 		spr->spd_rule_index = rule->ipsp_index;
1767 	}
1768 	offset += sizeof (struct spd_rule);
1769 
1770 	/*
1771 	 * If we have an interface name (i.e. if this policy head came from
1772 	 * a tunnel), add the SPD_EXT_TUN_NAME extension.
1773 	 */
1774 	if (name != NULL) {
1775 
1776 		ASSERT(ALIGNED64(offset));
1777 
1778 		if (base != NULL) {
1779 			sid = (spd_if_t *)(base + offset);
1780 			sid->spd_if_exttype = SPD_EXT_TUN_NAME;
1781 			sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) +
1782 			    roundup((strlen(name) - 4), 8));
1783 			(void) strlcpy((char *)sid->spd_if_name, name,
1784 			    LIFNAMSIZ);
1785 		}
1786 
1787 		offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8);
1788 	}
1789 
1790 	offset = spdsock_encode_sel(base, offset, rule->ipsp_sel);
1791 	offset = spdsock_encode_action_list(base, offset, rule->ipsp_act);
1792 
1793 	ASSERT(ALIGNED64(offset));
1794 
1795 	if (base != NULL) {
1796 		spmsg->spd_msg_len = SPD_8TO64(offset - start);
1797 	}
1798 	return (offset);
1799 }
1800 
1801 /* ARGSUSED */
1802 static mblk_t *
1803 spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule,
1804     uint_t dir, uint_t af, char *name, boolean_t tunnel)
1805 {
1806 	mblk_t *m;
1807 	uint_t len;
1808 	spd_msg_t *mreq = (spd_msg_t *)req->b_rptr;
1809 
1810 	/*
1811 	 * Figure out how much space we'll need.
1812 	 */
1813 	len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name,
1814 	    tunnel);
1815 
1816 	/*
1817 	 * Allocate mblk.
1818 	 */
1819 	m = allocb(len, BPRI_HI);
1820 	if (m == NULL)
1821 		return (NULL);
1822 
1823 	/*
1824 	 * Fill it in..
1825 	 */
1826 	m->b_wptr = m->b_rptr + len;
1827 	bzero(m->b_rptr, len);
1828 	(void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af,
1829 	    name, tunnel);
1830 	return (m);
1831 }
1832 
1833 static ipsec_policy_t *
1834 spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph,
1835     ipsec_policy_t *cur)
1836 {
1837 	ASSERT(RW_READ_HELD(&iph->iph_lock));
1838 
1839 	ss->spdsock_dump_count++;
1840 	ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next;
1841 	return (cur);
1842 }
1843 
1844 static ipsec_policy_t *
1845 spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph)
1846 {
1847 	ipsec_policy_t *cur;
1848 	ipsec_policy_root_t *ipr;
1849 	int chain, nchains, type, af;
1850 
1851 	ASSERT(RW_READ_HELD(&iph->iph_lock));
1852 
1853 	cur = ss->spdsock_dump_cur_rule;
1854 
1855 	if (cur != NULL)
1856 		return (spdsock_dump_next_in_chain(ss, iph, cur));
1857 
1858 	type = ss->spdsock_dump_cur_type;
1859 
1860 next:
1861 	chain = ss->spdsock_dump_cur_chain;
1862 	ipr = &iph->iph_root[type];
1863 	nchains = ipr->ipr_nchains;
1864 
1865 	while (chain < nchains) {
1866 		cur = ipr->ipr_hash[chain].hash_head;
1867 		chain++;
1868 		if (cur != NULL) {
1869 			ss->spdsock_dump_cur_chain = chain;
1870 			return (spdsock_dump_next_in_chain(ss, iph, cur));
1871 		}
1872 	}
1873 	ss->spdsock_dump_cur_chain = nchains;
1874 
1875 	af = ss->spdsock_dump_cur_af;
1876 	while (af < IPSEC_NAF) {
1877 		cur = ipr->ipr_nonhash[af];
1878 		af++;
1879 		if (cur != NULL) {
1880 			ss->spdsock_dump_cur_af = af;
1881 			return (spdsock_dump_next_in_chain(ss, iph, cur));
1882 		}
1883 	}
1884 
1885 	type++;
1886 	if (type >= IPSEC_NTYPES)
1887 		return (NULL);
1888 
1889 	ss->spdsock_dump_cur_chain = 0;
1890 	ss->spdsock_dump_cur_type = type;
1891 	ss->spdsock_dump_cur_af = IPSEC_AF_V4;
1892 	goto next;
1893 
1894 }
1895 
1896 /*
1897  * If we're done with one policy head, but have more to go, we iterate through
1898  * another IPsec tunnel policy head (itp).  Return NULL if it is an error
1899  * worthy of returning EAGAIN via PF_POLICY.
1900  */
1901 static ipsec_tun_pol_t *
1902 spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss)
1903 {
1904 	ipsec_tun_pol_t *itp;
1905 
1906 	ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock));
1907 	if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) {
1908 		/* Oops, state of the tunnel polheads changed. */
1909 		itp = NULL;
1910 	} else if (ss->spdsock_itp == NULL) {
1911 		/* Just finished global, find first node. */
1912 		itp = avl_first(&ipss->ipsec_tunnel_policies);
1913 	} else {
1914 		/* We just finished current polhead, find the next one. */
1915 		itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp);
1916 	}
1917 	if (itp != NULL) {
1918 		ITP_REFHOLD(itp);
1919 	}
1920 	if (ss->spdsock_itp != NULL) {
1921 		ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack);
1922 	}
1923 	ss->spdsock_itp = itp;
1924 	return (itp);
1925 }
1926 
1927 static mblk_t *
1928 spdsock_dump_next_record(spdsock_t *ss)
1929 {
1930 	ipsec_policy_head_t *iph;
1931 	ipsec_policy_t *rule;
1932 	mblk_t *m;
1933 	ipsec_tun_pol_t *itp;
1934 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
1935 	ipsec_stack_t *ipss = ns->netstack_ipsec;
1936 
1937 	iph = ss->spdsock_dump_head;
1938 
1939 	ASSERT(iph != NULL);
1940 
1941 	rw_enter(&iph->iph_lock, RW_READER);
1942 
1943 	if (iph->iph_gen != ss->spdsock_dump_gen) {
1944 		rw_exit(&iph->iph_lock);
1945 		return (spdsock_dump_finish(ss, EAGAIN));
1946 	}
1947 
1948 	while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) {
1949 		rw_exit(&iph->iph_lock);
1950 		if (--(ss->spdsock_dump_remaining_polheads) == 0)
1951 			return (spdsock_dump_finish(ss, 0));
1952 
1953 
1954 		/*
1955 		 * If we reach here, we have more policy heads (tunnel
1956 		 * entries) to dump.  Let's reset to a new policy head
1957 		 * and get some more rules.
1958 		 *
1959 		 * An empty policy head will have spdsock_dump_next_rule()
1960 		 * return NULL, and we loop (while dropping the number of
1961 		 * remaining polheads).  If we loop to 0, we finish.  We
1962 		 * keep looping until we hit 0 or until we have a rule to
1963 		 * encode.
1964 		 *
1965 		 * NOTE:  No need for ITP_REF*() macros here as we're only
1966 		 * going after and refholding the policy head itself.
1967 		 */
1968 		rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
1969 		itp = spdsock_dump_iterate_next_tunnel(ss, ipss);
1970 		if (itp == NULL) {
1971 			rw_exit(&ipss->ipsec_tunnel_policy_lock);
1972 			return (spdsock_dump_finish(ss, EAGAIN));
1973 		}
1974 
1975 		/* Reset other spdsock_dump thingies. */
1976 		IPPH_REFRELE(ss->spdsock_dump_head, ns);
1977 		if (ss->spdsock_dump_active) {
1978 			ss->spdsock_dump_tunnel =
1979 			    itp->itp_flags & ITPF_P_TUNNEL;
1980 			iph = itp->itp_policy;
1981 		} else {
1982 			ss->spdsock_dump_tunnel =
1983 			    itp->itp_flags & ITPF_I_TUNNEL;
1984 			iph = itp->itp_inactive;
1985 		}
1986 		IPPH_REFHOLD(iph);
1987 		rw_exit(&ipss->ipsec_tunnel_policy_lock);
1988 
1989 		rw_enter(&iph->iph_lock, RW_READER);
1990 		RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
1991 	}
1992 
1993 	m = spdsock_encode_rule(ss->spdsock_dump_req, rule,
1994 	    ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af,
1995 	    (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name,
1996 	    ss->spdsock_dump_tunnel);
1997 	rw_exit(&iph->iph_lock);
1998 
1999 	if (m == NULL)
2000 		return (spdsock_dump_finish(ss, ENOMEM));
2001 	return (m);
2002 }
2003 
2004 /*
2005  * Dump records until we run into flow-control back-pressure.
2006  */
2007 static void
2008 spdsock_dump_some(queue_t *q, spdsock_t *ss)
2009 {
2010 	mblk_t *m, *dataind;
2011 
2012 	while ((ss->spdsock_dump_req != NULL) && canputnext(q)) {
2013 		m = spdsock_dump_next_record(ss);
2014 		if (m == NULL)
2015 			return;
2016 		dataind = allocb(sizeof (struct T_data_req), BPRI_HI);
2017 		if (dataind == NULL) {
2018 			freemsg(m);
2019 			return;
2020 		}
2021 		dataind->b_cont = m;
2022 		dataind->b_wptr += sizeof (struct T_data_req);
2023 		((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND;
2024 		((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0;
2025 		dataind->b_datap->db_type = M_PROTO;
2026 		putnext(q, dataind);
2027 	}
2028 }
2029 
2030 /*
2031  * Start dumping.
2032  * Format a start-of-dump record, and set up the stream and kick the rsrv
2033  * procedure to continue the job..
2034  */
2035 /* ARGSUSED */
2036 static void
2037 spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp)
2038 {
2039 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2040 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
2041 	ipsec_stack_t *ipss = ns->netstack_ipsec;
2042 	mblk_t *mr;
2043 
2044 	/* spdsock_open() already set spdsock_itp to NULL. */
2045 	if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
2046 		rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
2047 		ss->spdsock_dump_remaining_polheads = 1 +
2048 		    avl_numnodes(&ipss->ipsec_tunnel_policies);
2049 		ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen;
2050 		rw_exit(&ipss->ipsec_tunnel_policy_lock);
2051 		if (iph == ALL_ACTIVE_POLHEADS) {
2052 			iph = ipsec_system_policy(ns);
2053 			ss->spdsock_dump_active = B_TRUE;
2054 		} else {
2055 			iph = ipsec_inactive_policy(ns);
2056 			ss->spdsock_dump_active = B_FALSE;
2057 		}
2058 		ASSERT(ss->spdsock_itp == NULL);
2059 	} else {
2060 		ss->spdsock_dump_remaining_polheads = 1;
2061 	}
2062 
2063 	rw_enter(&iph->iph_lock, RW_READER);
2064 
2065 	mr = spdsock_dump_ruleset(mp, iph, 0, 0);
2066 
2067 	if (!mr) {
2068 		rw_exit(&iph->iph_lock);
2069 		spdsock_error(q, mp, ENOMEM, 0);
2070 		return;
2071 	}
2072 
2073 	ss->spdsock_dump_req = mp;
2074 	RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
2075 
2076 	rw_exit(&iph->iph_lock);
2077 
2078 	qreply(q, mr);
2079 	qenable(OTHERQ(q));
2080 }
2081 
2082 /* Do NOT consume a reference to ITP. */
2083 void
2084 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns)
2085 {
2086 	int *errptr = (int *)ep;
2087 
2088 	if (*errptr != 0)
2089 		return;	/* We've failed already for some reason. */
2090 	mutex_enter(&itp->itp_lock);
2091 	ITPF_CLONE(itp->itp_flags);
2092 	*errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns);
2093 	mutex_exit(&itp->itp_lock);
2094 }
2095 
2096 void
2097 spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname)
2098 {
2099 	int error;
2100 	char *tname;
2101 	ipsec_tun_pol_t *itp;
2102 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2103 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
2104 
2105 	if (tunname != NULL) {
2106 		tname = (char *)tunname->spd_if_name;
2107 		if (*tname == '\0') {
2108 			error = ipsec_clone_system_policy(ns);
2109 			if (audit_active) {
2110 				boolean_t active;
2111 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2112 				cred_t *cr;
2113 				pid_t cpid;
2114 
2115 				cr = msg_getcred(mp, &cpid);
2116 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2117 				audit_pf_policy(SPD_CLONE, cr, ns,
2118 				    NULL, active, error, cpid);
2119 			}
2120 			if (error == 0) {
2121 				itp_walk(spdsock_clone_node, &error, ns);
2122 				if (audit_active) {
2123 					boolean_t active;
2124 					spd_msg_t *spmsg =
2125 					    (spd_msg_t *)mp->b_rptr;
2126 					cred_t *cr;
2127 					pid_t cpid;
2128 
2129 					cr = msg_getcred(mp, &cpid);
2130 					active = (spmsg->spd_msg_spdid ==
2131 					    SPD_ACTIVE);
2132 					audit_pf_policy(SPD_CLONE, cr,
2133 					    ns, "all tunnels", active, 0,
2134 					    cpid);
2135 				}
2136 			}
2137 		} else {
2138 			itp = get_tunnel_policy(tname, ns);
2139 			if (itp == NULL) {
2140 				spdsock_error(q, mp, ENOENT, 0);
2141 				if (audit_active) {
2142 					boolean_t active;
2143 					spd_msg_t *spmsg =
2144 					    (spd_msg_t *)mp->b_rptr;
2145 					cred_t *cr;
2146 					pid_t cpid;
2147 
2148 					cr = msg_getcred(mp, &cpid);
2149 					active = (spmsg->spd_msg_spdid ==
2150 					    SPD_ACTIVE);
2151 					audit_pf_policy(SPD_CLONE, cr,
2152 					    ns, NULL, active, ENOENT, cpid);
2153 				}
2154 				return;
2155 			}
2156 			spdsock_clone_node(itp, &error, NULL);
2157 			if (audit_active) {
2158 				boolean_t active;
2159 				spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2160 				cred_t *cr;
2161 				pid_t cpid;
2162 
2163 				cr = msg_getcred(mp, &cpid);
2164 				active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2165 				audit_pf_policy(SPD_CLONE, cr, ns,
2166 				    ITP_NAME(itp), active, error, cpid);
2167 			}
2168 			ITP_REFRELE(itp, ns);
2169 		}
2170 	} else {
2171 		error = ipsec_clone_system_policy(ns);
2172 		if (audit_active) {
2173 			boolean_t active;
2174 			spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2175 			cred_t *cr;
2176 			pid_t cpid;
2177 
2178 			cr = msg_getcred(mp, &cpid);
2179 			active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2180 			audit_pf_policy(SPD_CLONE, cr, ns, NULL,
2181 			    active, error, cpid);
2182 		}
2183 	}
2184 
2185 	if (error != 0)
2186 		spdsock_error(q, mp, error, 0);
2187 	else
2188 		spd_echo(q, mp);
2189 }
2190 
2191 /*
2192  * Process a SPD_ALGLIST request. The caller expects separate alg entries
2193  * for AH authentication, ESP authentication, and ESP encryption.
2194  * The same distinction is then used when setting the min and max key
2195  * sizes when defining policies.
2196  */
2197 
2198 #define	SPDSOCK_AH_AUTH		0
2199 #define	SPDSOCK_ESP_AUTH	1
2200 #define	SPDSOCK_ESP_ENCR	2
2201 #define	SPDSOCK_NTYPES		3
2202 
2203 static const uint_t algattr[SPDSOCK_NTYPES] = {
2204 	SPD_ATTR_AH_AUTH,
2205 	SPD_ATTR_ESP_AUTH,
2206 	SPD_ATTR_ESP_ENCR
2207 };
2208 static const uint_t minbitsattr[SPDSOCK_NTYPES] = {
2209 	SPD_ATTR_AH_MINBITS,
2210 	SPD_ATTR_ESPA_MINBITS,
2211 	SPD_ATTR_ENCR_MINBITS
2212 };
2213 static const uint_t maxbitsattr[SPDSOCK_NTYPES] = {
2214 	SPD_ATTR_AH_MAXBITS,
2215 	SPD_ATTR_ESPA_MAXBITS,
2216 	SPD_ATTR_ENCR_MAXBITS
2217 };
2218 static const uint_t defbitsattr[SPDSOCK_NTYPES] = {
2219 	SPD_ATTR_AH_DEFBITS,
2220 	SPD_ATTR_ESPA_DEFBITS,
2221 	SPD_ATTR_ENCR_DEFBITS
2222 };
2223 static const uint_t incrbitsattr[SPDSOCK_NTYPES] = {
2224 	SPD_ATTR_AH_INCRBITS,
2225 	SPD_ATTR_ESPA_INCRBITS,
2226 	SPD_ATTR_ENCR_INCRBITS
2227 };
2228 
2229 #define	ATTRPERALG	6	/* fixed attributes per algs */
2230 
2231 void
2232 spdsock_alglist(queue_t *q, mblk_t *mp)
2233 {
2234 	uint_t algtype;
2235 	uint_t algidx;
2236 	uint_t algcount;
2237 	uint_t size;
2238 	mblk_t *m;
2239 	uint8_t *cur;
2240 	spd_msg_t *msg;
2241 	struct spd_ext_actions *act;
2242 	struct spd_attribute *attr;
2243 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2244 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
2245 
2246 	mutex_enter(&ipss->ipsec_alg_lock);
2247 	/*
2248 	 * The SPD client expects to receive separate entries for
2249 	 * AH authentication and ESP authentication supported algorithms.
2250 	 *
2251 	 * Don't return the "any" algorithms, if defined, as no
2252 	 * kernel policies can be set for these algorithms.
2253 	 */
2254 	algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
2255 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
2256 
2257 	if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL)
2258 		algcount--;
2259 	if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL)
2260 		algcount--;
2261 
2262 	/*
2263 	 * For each algorithm, we encode:
2264 	 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
2265 	 */
2266 
2267 	size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) +
2268 	    ATTRPERALG * sizeof (struct spd_attribute) * algcount;
2269 
2270 	ASSERT(ALIGNED64(size));
2271 
2272 	m = allocb(size, BPRI_HI);
2273 	if (m == NULL) {
2274 		mutex_exit(&ipss->ipsec_alg_lock);
2275 		spdsock_error(q, mp, ENOMEM, 0);
2276 		return;
2277 	}
2278 
2279 	m->b_wptr = m->b_rptr + size;
2280 	cur = m->b_rptr;
2281 
2282 	msg = (spd_msg_t *)cur;
2283 	bcopy(mp->b_rptr, cur, sizeof (*msg));
2284 
2285 	msg->spd_msg_len = SPD_8TO64(size);
2286 	msg->spd_msg_errno = 0;
2287 	msg->spd_msg_diagnostic = 0;
2288 
2289 	cur += sizeof (*msg);
2290 
2291 	act = (struct spd_ext_actions *)cur;
2292 	cur += sizeof (*act);
2293 
2294 	act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
2295 	act->spd_actions_exttype = SPD_EXT_ACTION;
2296 	act->spd_actions_count = algcount;
2297 	act->spd_actions_reserved = 0;
2298 
2299 	attr = (struct spd_attribute *)cur;
2300 
2301 #define	EMIT(tag, value) {					\
2302 		attr->spd_attr_tag = (tag); 			\
2303 		attr->spd_attr_value = (value); 		\
2304 		attr++;			  			\
2305 	}
2306 
2307 	/*
2308 	 * If you change the number of EMIT's here, change
2309 	 * ATTRPERALG above to match
2310 	 */
2311 #define	EMITALGATTRS(_type) {					\
2312 		EMIT(algattr[_type], algid); 		/* 1 */	\
2313 		EMIT(minbitsattr[_type], minbits);	/* 2 */	\
2314 		EMIT(maxbitsattr[_type], maxbits);	/* 3 */	\
2315 		EMIT(defbitsattr[_type], defbits);	/* 4 */	\
2316 		EMIT(incrbitsattr[_type], incr);	/* 5 */	\
2317 		EMIT(SPD_ATTR_NEXT, 0);			/* 6 */	\
2318 	}
2319 
2320 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2321 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2322 		    algidx++) {
2323 			int algid = ipss->ipsec_sortlist[algtype][algidx];
2324 			ipsec_alginfo_t *alg =
2325 			    ipss->ipsec_alglists[algtype][algid];
2326 			uint_t minbits = alg->alg_minbits;
2327 			uint_t maxbits = alg->alg_maxbits;
2328 			uint_t defbits = alg->alg_default_bits;
2329 			uint_t incr = alg->alg_increment;
2330 
2331 			if (algtype == IPSEC_ALG_AUTH) {
2332 				if (algid == SADB_AALG_NONE)
2333 					continue;
2334 				EMITALGATTRS(SPDSOCK_AH_AUTH);
2335 				EMITALGATTRS(SPDSOCK_ESP_AUTH);
2336 			} else {
2337 				if (algid == SADB_EALG_NONE)
2338 					continue;
2339 				ASSERT(algtype == IPSEC_ALG_ENCR);
2340 				EMITALGATTRS(SPDSOCK_ESP_ENCR);
2341 			}
2342 		}
2343 	}
2344 
2345 	mutex_exit(&ipss->ipsec_alg_lock);
2346 
2347 #undef EMITALGATTRS
2348 #undef EMIT
2349 #undef ATTRPERALG
2350 
2351 	attr--;
2352 	attr->spd_attr_tag = SPD_ATTR_END;
2353 
2354 	freemsg(mp);
2355 	qreply(q, m);
2356 }
2357 
2358 /*
2359  * Process a SPD_DUMPALGS request.
2360  */
2361 
2362 #define	ATTRPERALG	9	/* fixed attributes per algs */
2363 
2364 void
2365 spdsock_dumpalgs(queue_t *q, mblk_t *mp)
2366 {
2367 	uint_t algtype;
2368 	uint_t algidx;
2369 	uint_t size;
2370 	mblk_t *m;
2371 	uint8_t *cur;
2372 	spd_msg_t *msg;
2373 	struct spd_ext_actions *act;
2374 	struct spd_attribute *attr;
2375 	ipsec_alginfo_t *alg;
2376 	uint_t algid;
2377 	uint_t i;
2378 	uint_t alg_size;
2379 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2380 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
2381 
2382 	mutex_enter(&ipss->ipsec_alg_lock);
2383 
2384 	/*
2385 	 * For each algorithm, we encode:
2386 	 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
2387 	 *
2388 	 * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE*
2389 	 * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_NPARAMS / ALG_PARAMS* /
2390 	 * ALG_MECHNAME / ALG_FLAGS / {END, NEXT}
2391 	 */
2392 
2393 	/*
2394 	 * Compute the size of the SPD message.
2395 	 */
2396 	size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions);
2397 
2398 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2399 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2400 		    algidx++) {
2401 			algid = ipss->ipsec_sortlist[algtype][algidx];
2402 			alg = ipss->ipsec_alglists[algtype][algid];
2403 			alg_size = sizeof (struct spd_attribute) *
2404 			    (ATTRPERALG + alg->alg_nkey_sizes +
2405 			    alg->alg_nblock_sizes + alg->alg_nparams) +
2406 			    CRYPTO_MAX_MECH_NAME;
2407 			size += alg_size;
2408 		}
2409 	}
2410 
2411 	ASSERT(ALIGNED64(size));
2412 
2413 	m = allocb(size, BPRI_HI);
2414 	if (m == NULL) {
2415 		mutex_exit(&ipss->ipsec_alg_lock);
2416 		spdsock_error(q, mp, ENOMEM, 0);
2417 		return;
2418 	}
2419 
2420 	m->b_wptr = m->b_rptr + size;
2421 	cur = m->b_rptr;
2422 
2423 	msg = (spd_msg_t *)cur;
2424 	bcopy(mp->b_rptr, cur, sizeof (*msg));
2425 
2426 	msg->spd_msg_len = SPD_8TO64(size);
2427 	msg->spd_msg_errno = 0;
2428 	msg->spd_msg_type = SPD_ALGLIST;
2429 
2430 	msg->spd_msg_diagnostic = 0;
2431 
2432 	cur += sizeof (*msg);
2433 
2434 	act = (struct spd_ext_actions *)cur;
2435 	cur += sizeof (*act);
2436 
2437 	act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
2438 	act->spd_actions_exttype = SPD_EXT_ACTION;
2439 	act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
2440 	    ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
2441 	act->spd_actions_reserved = 0;
2442 
2443 	/*
2444 	 * If there aren't any algorithms registered, return an empty message.
2445 	 * spdsock_get_ext() knows how to deal with this.
2446 	 */
2447 	if (act->spd_actions_count == 0) {
2448 		act->spd_actions_len = 0;
2449 		mutex_exit(&ipss->ipsec_alg_lock);
2450 		goto error;
2451 	}
2452 
2453 	attr = (struct spd_attribute *)cur;
2454 
2455 #define	EMIT(tag, value) {					\
2456 		attr->spd_attr_tag = (tag); 			\
2457 		attr->spd_attr_value = (value); 		\
2458 		attr++;			  			\
2459 	}
2460 
2461 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2462 		for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2463 		    algidx++) {
2464 
2465 			algid = ipss->ipsec_sortlist[algtype][algidx];
2466 			alg = ipss->ipsec_alglists[algtype][algid];
2467 
2468 			/*
2469 			 * If you change the number of EMIT's here, change
2470 			 * ATTRPERALG above to match
2471 			 */
2472 			EMIT(SPD_ATTR_ALG_ID, algid);
2473 			EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]);
2474 			EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment);
2475 			EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes);
2476 			for (i = 0; i < alg->alg_nkey_sizes; i++)
2477 				EMIT(SPD_ATTR_ALG_KEYSIZE,
2478 				    alg->alg_key_sizes[i]);
2479 
2480 			EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes);
2481 			for (i = 0; i < alg->alg_nblock_sizes; i++)
2482 				EMIT(SPD_ATTR_ALG_BLOCKSIZE,
2483 				    alg->alg_block_sizes[i]);
2484 
2485 			EMIT(SPD_ATTR_ALG_NPARAMS, alg->alg_nparams);
2486 			for (i = 0; i < alg->alg_nparams; i++)
2487 				EMIT(SPD_ATTR_ALG_PARAMS,
2488 				    alg->alg_params[i]);
2489 
2490 			EMIT(SPD_ATTR_ALG_FLAGS, alg->alg_flags);
2491 
2492 			EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME);
2493 			bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME);
2494 			attr = (struct spd_attribute *)((char *)attr +
2495 			    CRYPTO_MAX_MECH_NAME);
2496 
2497 			EMIT(SPD_ATTR_NEXT, 0);
2498 		}
2499 	}
2500 
2501 	mutex_exit(&ipss->ipsec_alg_lock);
2502 
2503 #undef EMITALGATTRS
2504 #undef EMIT
2505 #undef ATTRPERALG
2506 
2507 	attr--;
2508 	attr->spd_attr_tag = SPD_ATTR_END;
2509 
2510 error:
2511 	freemsg(mp);
2512 	qreply(q, m);
2513 }
2514 
2515 /*
2516  * Do the actual work of processing an SPD_UPDATEALGS request. Can
2517  * be invoked either once IPsec is loaded on a cached request, or
2518  * when a request is received while IPsec is loaded.
2519  */
2520 static int
2521 spdsock_do_updatealg(spd_ext_t *extv[], spd_stack_t *spds)
2522 {
2523 	struct spd_ext_actions *actp;
2524 	struct spd_attribute *attr, *endattr;
2525 	uint64_t *start, *end;
2526 	ipsec_alginfo_t *alg = NULL;
2527 	ipsec_algtype_t alg_type = 0;
2528 	boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE;
2529 	uint_t i, cur_key, cur_block, algid;
2530 	int diag = -1;
2531 
2532 	ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
2533 
2534 	/* parse the message, building the list of algorithms */
2535 
2536 	actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
2537 	if (actp == NULL)
2538 		return (SPD_DIAGNOSTIC_NO_ACTION_EXT);
2539 
2540 	start = (uint64_t *)actp;
2541 	end = (start + actp->spd_actions_len);
2542 	endattr = (struct spd_attribute *)end;
2543 	attr = (struct spd_attribute *)&actp[1];
2544 
2545 	bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS *
2546 	    sizeof (ipsec_alginfo_t *));
2547 
2548 	alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
2549 
2550 #define	ALG_KEY_SIZES(a)   (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t))
2551 #define	ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t))
2552 
2553 	while (attr < endattr) {
2554 		switch (attr->spd_attr_tag) {
2555 		case SPD_ATTR_NOP:
2556 		case SPD_ATTR_EMPTY:
2557 			break;
2558 		case SPD_ATTR_END:
2559 			attr = endattr;
2560 			/* FALLTHRU */
2561 		case SPD_ATTR_NEXT:
2562 			if (doing_proto) {
2563 				doing_proto = B_FALSE;
2564 				break;
2565 			}
2566 			if (skip_alg) {
2567 				ipsec_alg_free(alg);
2568 			} else {
2569 				ipsec_alg_free(
2570 				    spds->spds_algs[alg_type][alg->alg_id]);
2571 				spds->spds_algs[alg_type][alg->alg_id] =
2572 				    alg;
2573 			}
2574 			alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
2575 			break;
2576 
2577 		case SPD_ATTR_ALG_ID:
2578 			if (attr->spd_attr_value >= IPSEC_MAX_ALGS) {
2579 				ss1dbg(spds, ("spdsock_do_updatealg: "
2580 				    "invalid alg id %d\n",
2581 				    attr->spd_attr_value));
2582 				diag = SPD_DIAGNOSTIC_ALG_ID_RANGE;
2583 				goto bail;
2584 			}
2585 			alg->alg_id = attr->spd_attr_value;
2586 			break;
2587 
2588 		case SPD_ATTR_ALG_PROTO:
2589 			/* find the alg type */
2590 			for (i = 0; i < NALGPROTOS; i++)
2591 				if (algproto[i] == attr->spd_attr_value)
2592 					break;
2593 			skip_alg = (i == NALGPROTOS);
2594 			if (!skip_alg)
2595 				alg_type = i;
2596 			break;
2597 
2598 		case SPD_ATTR_ALG_INCRBITS:
2599 			alg->alg_increment = attr->spd_attr_value;
2600 			break;
2601 
2602 		case SPD_ATTR_ALG_NKEYSIZES:
2603 			if (alg->alg_key_sizes != NULL) {
2604 				kmem_free(alg->alg_key_sizes,
2605 				    ALG_KEY_SIZES(alg));
2606 			}
2607 			alg->alg_nkey_sizes = attr->spd_attr_value;
2608 			/*
2609 			 * Allocate room for the trailing zero key size
2610 			 * value as well.
2611 			 */
2612 			alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg),
2613 			    KM_SLEEP);
2614 			cur_key = 0;
2615 			break;
2616 
2617 		case SPD_ATTR_ALG_KEYSIZE:
2618 			if (alg->alg_key_sizes == NULL ||
2619 			    cur_key >= alg->alg_nkey_sizes) {
2620 				ss1dbg(spds, ("spdsock_do_updatealg: "
2621 				    "too many key sizes\n"));
2622 				diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES;
2623 				goto bail;
2624 			}
2625 			alg->alg_key_sizes[cur_key++] = attr->spd_attr_value;
2626 			break;
2627 
2628 		case SPD_ATTR_ALG_FLAGS:
2629 			/*
2630 			 * Flags (bit mask). The alg_flags element of
2631 			 * ipsecalg_flags_t is only 8 bits wide. The
2632 			 * user can set the VALID bit, but we will ignore it
2633 			 * and make the decision is the algorithm is valid.
2634 			 */
2635 			alg->alg_flags |= (uint8_t)attr->spd_attr_value;
2636 			break;
2637 
2638 		case SPD_ATTR_ALG_NBLOCKSIZES:
2639 			if (alg->alg_block_sizes != NULL) {
2640 				kmem_free(alg->alg_block_sizes,
2641 				    ALG_BLOCK_SIZES(alg));
2642 			}
2643 			alg->alg_nblock_sizes = attr->spd_attr_value;
2644 			/*
2645 			 * Allocate room for the trailing zero block size
2646 			 * value as well.
2647 			 */
2648 			alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg),
2649 			    KM_SLEEP);
2650 			cur_block = 0;
2651 			break;
2652 
2653 		case SPD_ATTR_ALG_BLOCKSIZE:
2654 			if (alg->alg_block_sizes == NULL ||
2655 			    cur_block >= alg->alg_nblock_sizes) {
2656 				ss1dbg(spds, ("spdsock_do_updatealg: "
2657 				    "too many block sizes\n"));
2658 				diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
2659 				goto bail;
2660 			}
2661 			alg->alg_block_sizes[cur_block++] =
2662 			    attr->spd_attr_value;
2663 			break;
2664 
2665 		case SPD_ATTR_ALG_NPARAMS:
2666 			if (alg->alg_params != NULL) {
2667 				kmem_free(alg->alg_params,
2668 				    ALG_BLOCK_SIZES(alg));
2669 			}
2670 			alg->alg_nparams = attr->spd_attr_value;
2671 			/*
2672 			 * Allocate room for the trailing zero block size
2673 			 * value as well.
2674 			 */
2675 			alg->alg_params = kmem_zalloc(ALG_BLOCK_SIZES(alg),
2676 			    KM_SLEEP);
2677 			cur_block = 0;
2678 			break;
2679 
2680 		case SPD_ATTR_ALG_PARAMS:
2681 			if (alg->alg_params == NULL ||
2682 			    cur_block >= alg->alg_nparams) {
2683 				ss1dbg(spds, ("spdsock_do_updatealg: "
2684 				    "too many params\n"));
2685 				diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
2686 				goto bail;
2687 			}
2688 			/*
2689 			 * Array contains: iv_len, icv_len, salt_len
2690 			 * Any additional parameters are currently ignored.
2691 			 */
2692 			alg->alg_params[cur_block++] =
2693 			    attr->spd_attr_value;
2694 			break;
2695 
2696 		case SPD_ATTR_ALG_MECHNAME: {
2697 			char *mech_name;
2698 
2699 			if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) {
2700 				ss1dbg(spds, ("spdsock_do_updatealg: "
2701 				    "mech name too long\n"));
2702 				diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN;
2703 				goto bail;
2704 			}
2705 			mech_name = (char *)(attr + 1);
2706 			bcopy(mech_name, alg->alg_mech_name,
2707 			    attr->spd_attr_value);
2708 			alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0';
2709 			attr = (struct spd_attribute *)((char *)attr +
2710 			    attr->spd_attr_value);
2711 			break;
2712 		}
2713 
2714 		case SPD_ATTR_PROTO_ID:
2715 			doing_proto = B_TRUE;
2716 			for (i = 0; i < NALGPROTOS; i++) {
2717 				if (algproto[i] == attr->spd_attr_value) {
2718 					alg_type = i;
2719 					break;
2720 				}
2721 			}
2722 			break;
2723 
2724 		case SPD_ATTR_PROTO_EXEC_MODE:
2725 			if (!doing_proto)
2726 				break;
2727 			for (i = 0; i < NEXECMODES; i++) {
2728 				if (execmodes[i] == attr->spd_attr_value) {
2729 					spds->spds_algs_exec_mode[alg_type] = i;
2730 					break;
2731 				}
2732 			}
2733 			break;
2734 		}
2735 		attr++;
2736 	}
2737 
2738 #undef	ALG_KEY_SIZES
2739 #undef	ALG_BLOCK_SIZES
2740 
2741 	/* update the algorithm tables */
2742 	spdsock_merge_algs(spds);
2743 bail:
2744 	/* cleanup */
2745 	ipsec_alg_free(alg);
2746 	for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++)
2747 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++)
2748 		if (spds->spds_algs[alg_type][algid] != NULL)
2749 			ipsec_alg_free(spds->spds_algs[alg_type][algid]);
2750 	return (diag);
2751 }
2752 
2753 /*
2754  * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue
2755  * the request until IPsec loads. If IPsec is loaded, act on it
2756  * immediately.
2757  */
2758 
2759 static void
2760 spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[])
2761 {
2762 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2763 	spd_stack_t	*spds = ss->spdsock_spds;
2764 	ipsec_stack_t	*ipss = spds->spds_netstack->netstack_ipsec;
2765 
2766 	if (!ipsec_loaded(ipss)) {
2767 		/*
2768 		 * IPsec is not loaded, save request and return nicely,
2769 		 * the message will be processed once IPsec loads.
2770 		 */
2771 		mblk_t *new_mp;
2772 
2773 		/* last update message wins */
2774 		if ((new_mp = copymsg(mp)) == NULL) {
2775 			spdsock_error(q, mp, ENOMEM, 0);
2776 			return;
2777 		}
2778 		mutex_enter(&spds->spds_alg_lock);
2779 		bcopy(extv, spds->spds_extv_algs,
2780 		    sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
2781 		if (spds->spds_mp_algs != NULL)
2782 			freemsg(spds->spds_mp_algs);
2783 		spds->spds_mp_algs = mp;
2784 		spds->spds_algs_pending = B_TRUE;
2785 		mutex_exit(&spds->spds_alg_lock);
2786 		if (audit_active) {
2787 			cred_t *cr;
2788 			pid_t cpid;
2789 
2790 			cr = msg_getcred(mp, &cpid);
2791 			audit_pf_policy(SPD_UPDATEALGS, cr,
2792 			    spds->spds_netstack, NULL, B_TRUE, EAGAIN,
2793 			    cpid);
2794 		}
2795 		spd_echo(q, new_mp);
2796 	} else {
2797 		/*
2798 		 * IPsec is loaded, act on the message immediately.
2799 		 */
2800 		int diag;
2801 
2802 		mutex_enter(&spds->spds_alg_lock);
2803 		diag = spdsock_do_updatealg(extv, spds);
2804 		if (diag == -1) {
2805 			/* Keep the lock held while we walk the SA tables. */
2806 			sadb_alg_update(IPSEC_ALG_ALL, 0, 0,
2807 			    spds->spds_netstack);
2808 			mutex_exit(&spds->spds_alg_lock);
2809 			spd_echo(q, mp);
2810 			if (audit_active) {
2811 				cred_t *cr;
2812 				pid_t cpid;
2813 
2814 				cr = msg_getcred(mp, &cpid);
2815 				audit_pf_policy(SPD_UPDATEALGS, cr,
2816 				    spds->spds_netstack, NULL, B_TRUE, 0,
2817 				    cpid);
2818 			}
2819 		} else {
2820 			mutex_exit(&spds->spds_alg_lock);
2821 			spdsock_diag(q, mp, diag);
2822 			if (audit_active) {
2823 				cred_t *cr;
2824 				pid_t cpid;
2825 
2826 				cr = msg_getcred(mp, &cpid);
2827 				audit_pf_policy(SPD_UPDATEALGS, cr,
2828 				    spds->spds_netstack, NULL, B_TRUE, diag,
2829 				    cpid);
2830 			}
2831 		}
2832 	}
2833 }
2834 
2835 /*
2836  * Sort through the mess of polhead options to retrieve an appropriate one.
2837  * Returns NULL if we send an spdsock error.  Returns a valid pointer if we
2838  * found a valid polhead.  Returns ALL_ACTIVE_POLHEADS (aka. -1) or
2839  * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to
2840  * act on ALL policy heads.
2841  */
2842 static ipsec_policy_head_t *
2843 get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid,
2844     int msgtype, ipsec_tun_pol_t **itpp)
2845 {
2846 	ipsec_tun_pol_t *itp;
2847 	ipsec_policy_head_t *iph;
2848 	int errno;
2849 	char *tname;
2850 	boolean_t active;
2851 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2852 	netstack_t *ns = ss->spdsock_spds->spds_netstack;
2853 	uint64_t gen;	/* Placeholder */
2854 	datalink_id_t linkid;
2855 
2856 	active = (spdid == SPD_ACTIVE);
2857 	*itpp = NULL;
2858 	if (!active && spdid != SPD_STANDBY) {
2859 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID);
2860 		return (NULL);
2861 	}
2862 
2863 	if (tunname != NULL) {
2864 		/* Acting on a tunnel's SPD. */
2865 		tname = (char *)tunname->spd_if_name;
2866 		if (*tname == '\0') {
2867 			/* Handle all-polhead cases here. */
2868 			if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) {
2869 				spdsock_diag(q, mp,
2870 				    SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
2871 				return (NULL);
2872 			}
2873 			return (active ? ALL_ACTIVE_POLHEADS :
2874 			    ALL_INACTIVE_POLHEADS);
2875 		}
2876 
2877 		itp = get_tunnel_policy(tname, ns);
2878 		if (itp == NULL) {
2879 			if (msgtype != SPD_ADDRULE) {
2880 				/* "Tunnel not found" */
2881 				spdsock_error(q, mp, ENOENT, 0);
2882 				return (NULL);
2883 			}
2884 
2885 			errno = 0;
2886 			itp = create_tunnel_policy(tname, &errno, &gen, ns);
2887 			if (itp == NULL) {
2888 				/*
2889 				 * Something very bad happened, most likely
2890 				 * ENOMEM.  Return an indicator.
2891 				 */
2892 				spdsock_error(q, mp, errno, 0);
2893 				return (NULL);
2894 			}
2895 		}
2896 		/*
2897 		 * Troll the plumbed tunnels and see if we have a match.  We
2898 		 * need to do this always in case we add policy AFTER plumbing
2899 		 * a tunnel.
2900 		 */
2901 		if (dls_mgmt_get_linkid(tname, &linkid) == 0)
2902 			iptun_set_policy(linkid, itp);
2903 
2904 		*itpp = itp;
2905 		/* For spdsock dump state, set the polhead's name. */
2906 		if (msgtype == SPD_DUMP) {
2907 			ITP_REFHOLD(itp);
2908 			ss->spdsock_itp = itp;
2909 			ss->spdsock_dump_tunnel = itp->itp_flags &
2910 			    (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL);
2911 		}
2912 	} else {
2913 		itp = NULL;
2914 		/* For spdsock dump state, indicate it's global policy. */
2915 		if (msgtype == SPD_DUMP)
2916 			ss->spdsock_itp = NULL;
2917 	}
2918 
2919 	if (active)
2920 		iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy;
2921 	else
2922 		iph = (itp == NULL) ? ipsec_inactive_policy(ns) :
2923 		    itp->itp_inactive;
2924 
2925 	ASSERT(iph != NULL);
2926 	if (itp != NULL) {
2927 		IPPH_REFHOLD(iph);
2928 	}
2929 
2930 	return (iph);
2931 }
2932 
2933 static void
2934 spdsock_parse(queue_t *q, mblk_t *mp)
2935 {
2936 	spd_msg_t *spmsg;
2937 	spd_ext_t *extv[SPD_EXT_MAX + 1];
2938 	uint_t msgsize;
2939 	ipsec_policy_head_t *iph;
2940 	ipsec_tun_pol_t *itp;
2941 	spd_if_t *tunname;
2942 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
2943 	spd_stack_t *spds = ss->spdsock_spds;
2944 	netstack_t *ns = spds->spds_netstack;
2945 	ipsec_stack_t *ipss = ns->netstack_ipsec;
2946 
2947 	/* Make sure nothing's below me. */
2948 	ASSERT(WR(q)->q_next == NULL);
2949 
2950 	spmsg = (spd_msg_t *)mp->b_rptr;
2951 
2952 	msgsize = SPD_64TO8(spmsg->spd_msg_len);
2953 
2954 	if (msgdsize(mp) != msgsize) {
2955 		/*
2956 		 * Message len incorrect w.r.t. actual size.  Send an error
2957 		 * (EMSGSIZE).	It may be necessary to massage things a
2958 		 * bit.	 For example, if the spd_msg_type is hosed,
2959 		 * I need to set it to SPD_RESERVED to get delivery to
2960 		 * do the right thing.	Then again, maybe just letting
2961 		 * the error delivery do the right thing.
2962 		 */
2963 		ss2dbg(spds,
2964 		    ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
2965 		    msgdsize(mp), msgsize));
2966 		spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE);
2967 		return;
2968 	}
2969 
2970 	if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
2971 		/* Get all message into one mblk. */
2972 		if (pullupmsg(mp, -1) == 0) {
2973 			/*
2974 			 * Something screwy happened.
2975 			 */
2976 			ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n"));
2977 			return;
2978 		} else {
2979 			spmsg = (spd_msg_t *)mp->b_rptr;
2980 		}
2981 	}
2982 
2983 	switch (spdsock_get_ext(extv, spmsg, msgsize)) {
2984 	case KGE_DUP:
2985 		/* Handle duplicate extension. */
2986 		ss1dbg(spds, ("Got duplicate extension of type %d.\n",
2987 		    extv[0]->spd_ext_type));
2988 		spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]);
2989 		return;
2990 	case KGE_UNK:
2991 		/* Handle unknown extension. */
2992 		ss1dbg(spds, ("Got unknown extension of type %d.\n",
2993 		    extv[0]->spd_ext_type));
2994 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT);
2995 		return;
2996 	case KGE_LEN:
2997 		/* Length error. */
2998 		ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n",
2999 		    extv[0]->spd_ext_len, extv[0]->spd_ext_type));
3000 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN);
3001 		return;
3002 	case KGE_CHK:
3003 		/* Reality check failed. */
3004 		ss1dbg(spds, ("Reality check failed on extension type %d.\n",
3005 		    extv[0]->spd_ext_type));
3006 		spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]);
3007 		return;
3008 	default:
3009 		/* Default case is no errors. */
3010 		break;
3011 	}
3012 
3013 	/*
3014 	 * Special-case SPD_UPDATEALGS so as not to load IPsec.
3015 	 */
3016 	if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) {
3017 		spdsock_t *ss = (spdsock_t *)q->q_ptr;
3018 
3019 		ASSERT(ss != NULL);
3020 		ipsec_loader_loadnow(ipss);
3021 		ss->spdsock_timeout_arg = mp;
3022 		ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck,
3023 		    q, LOADCHECK_INTERVAL);
3024 		return;
3025 	}
3026 
3027 	/* First check for messages that need no polheads at all. */
3028 	switch (spmsg->spd_msg_type) {
3029 	case SPD_UPDATEALGS:
3030 		spdsock_updatealg(q, mp, extv);
3031 		return;
3032 	case SPD_ALGLIST:
3033 		spdsock_alglist(q, mp);
3034 		return;
3035 	case SPD_DUMPALGS:
3036 		spdsock_dumpalgs(q, mp);
3037 		return;
3038 	}
3039 
3040 	/*
3041 	 * Then check for ones that need both primary/secondary polheads,
3042 	 * finding the appropriate tunnel policy if need be.
3043 	 */
3044 	tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME];
3045 	switch (spmsg->spd_msg_type) {
3046 	case SPD_FLIP:
3047 		spdsock_flip(q, mp, tunname);
3048 		return;
3049 	case SPD_CLONE:
3050 		spdsock_clone(q, mp, tunname);
3051 		return;
3052 	}
3053 
3054 	/*
3055 	 * Finally, find ones that operate on exactly one polhead, or
3056 	 * "all polheads" of a given type (active/inactive).
3057 	 */
3058 	iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid,
3059 	    spmsg->spd_msg_type, &itp);
3060 	if (iph == NULL)
3061 		return;
3062 
3063 	/* All-polheads-ready operations. */
3064 	switch (spmsg->spd_msg_type) {
3065 	case SPD_FLUSH:
3066 		if (itp != NULL) {
3067 			mutex_enter(&itp->itp_lock);
3068 			if (spmsg->spd_msg_spdid == SPD_ACTIVE)
3069 				itp->itp_flags &= ~ITPF_PFLAGS;
3070 			else
3071 				itp->itp_flags &= ~ITPF_IFLAGS;
3072 			mutex_exit(&itp->itp_lock);
3073 			ITP_REFRELE(itp, ns);
3074 		}
3075 		spdsock_flush(q, iph, itp, mp);
3076 		return;
3077 	case SPD_DUMP:
3078 		if (itp != NULL)
3079 			ITP_REFRELE(itp, ns);
3080 		spdsock_dump(q, iph, mp);
3081 		return;
3082 	}
3083 
3084 	if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
3085 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
3086 		return;
3087 	}
3088 
3089 	/* Single-polhead-only operations. */
3090 	switch (spmsg->spd_msg_type) {
3091 	case SPD_ADDRULE:
3092 		spdsock_addrule(q, iph, mp, extv, itp);
3093 		break;
3094 	case SPD_DELETERULE:
3095 		spdsock_deleterule(q, iph, mp, extv, itp);
3096 		break;
3097 	case SPD_LOOKUP:
3098 		spdsock_lookup(q, iph, mp, extv, itp);
3099 		break;
3100 	default:
3101 		spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE);
3102 		break;
3103 	}
3104 
3105 	IPPH_REFRELE(iph, ns);
3106 	if (itp != NULL)
3107 		ITP_REFRELE(itp, ns);
3108 }
3109 
3110 /*
3111  * If an algorithm mapping was received before IPsec was loaded, process it.
3112  * Called from the IPsec loader.
3113  */
3114 void
3115 spdsock_update_pending_algs(netstack_t *ns)
3116 {
3117 	spd_stack_t *spds = ns->netstack_spdsock;
3118 
3119 	mutex_enter(&spds->spds_alg_lock);
3120 	if (spds->spds_algs_pending) {
3121 		(void) spdsock_do_updatealg(spds->spds_extv_algs, spds);
3122 		spds->spds_algs_pending = B_FALSE;
3123 	}
3124 	mutex_exit(&spds->spds_alg_lock);
3125 }
3126 
3127 static void
3128 spdsock_loadcheck(void *arg)
3129 {
3130 	queue_t *q = (queue_t *)arg;
3131 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
3132 	mblk_t *mp;
3133 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
3134 
3135 	ASSERT(ss != NULL);
3136 
3137 	ss->spdsock_timeout = 0;
3138 	mp = ss->spdsock_timeout_arg;
3139 	ASSERT(mp != NULL);
3140 	ss->spdsock_timeout_arg = NULL;
3141 	if (ipsec_failed(ipss))
3142 		spdsock_error(q, mp, EPROTONOSUPPORT, 0);
3143 	else
3144 		spdsock_parse(q, mp);
3145 }
3146 
3147 /*
3148  * Copy relevant state bits.
3149  */
3150 static void
3151 spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss)
3152 {
3153 	*tap = spdsock_g_t_info_ack;
3154 	tap->CURRENT_state = ss->spdsock_state;
3155 	tap->OPT_size = spdsock_max_optsize;
3156 }
3157 
3158 /*
3159  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
3160  * spdsock_wput.  Much of the T_CAPABILITY_ACK information is copied from
3161  * spdsock_g_t_info_ack.  The current state of the stream is copied from
3162  * spdsock_state.
3163  */
3164 static void
3165 spdsock_capability_req(queue_t *q, mblk_t *mp)
3166 {
3167 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
3168 	t_uscalar_t cap_bits1;
3169 	struct T_capability_ack	*tcap;
3170 
3171 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
3172 
3173 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
3174 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
3175 	if (mp == NULL)
3176 		return;
3177 
3178 	tcap = (struct T_capability_ack *)mp->b_rptr;
3179 	tcap->CAP_bits1 = 0;
3180 
3181 	if (cap_bits1 & TC1_INFO) {
3182 		spdsock_copy_info(&tcap->INFO_ack, ss);
3183 		tcap->CAP_bits1 |= TC1_INFO;
3184 	}
3185 
3186 	qreply(q, mp);
3187 }
3188 
3189 /*
3190  * This routine responds to T_INFO_REQ messages. It is called by
3191  * spdsock_wput_other.
3192  * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack.
3193  * The current state of the stream is copied from spdsock_state.
3194  */
3195 static void
3196 spdsock_info_req(q, mp)
3197 	queue_t	*q;
3198 	mblk_t	*mp;
3199 {
3200 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
3201 	    T_INFO_ACK);
3202 	if (mp == NULL)
3203 		return;
3204 	spdsock_copy_info((struct T_info_ack *)mp->b_rptr,
3205 	    (spdsock_t *)q->q_ptr);
3206 	qreply(q, mp);
3207 }
3208 
3209 /*
3210  * spdsock_err_ack. This routine creates a
3211  * T_ERROR_ACK message and passes it
3212  * upstream.
3213  */
3214 static void
3215 spdsock_err_ack(q, mp, t_error, sys_error)
3216 	queue_t	*q;
3217 	mblk_t	*mp;
3218 	int	t_error;
3219 	int	sys_error;
3220 {
3221 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
3222 		qreply(q, mp);
3223 }
3224 
3225 /*
3226  * This routine retrieves the current status of socket options.
3227  * It returns the size of the option retrieved.
3228  */
3229 /* ARGSUSED */
3230 int
3231 spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
3232 {
3233 	int *i1 = (int *)ptr;
3234 
3235 	switch (level) {
3236 	case SOL_SOCKET:
3237 		switch (name) {
3238 		case SO_TYPE:
3239 			*i1 = SOCK_RAW;
3240 			break;
3241 		/*
3242 		 * The following two items can be manipulated,
3243 		 * but changing them should do nothing.
3244 		 */
3245 		case SO_SNDBUF:
3246 			*i1 = (int)q->q_hiwat;
3247 			break;
3248 		case SO_RCVBUF:
3249 			*i1 = (int)(RD(q)->q_hiwat);
3250 			break;
3251 		}
3252 		break;
3253 	default:
3254 		return (0);
3255 	}
3256 	return (sizeof (int));
3257 }
3258 
3259 /*
3260  * This routine sets socket options.
3261  */
3262 /* ARGSUSED */
3263 int
3264 spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name,
3265     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
3266     void *thisdg_attrs, cred_t *cr)
3267 {
3268 	int *i1 = (int *)invalp;
3269 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
3270 	spd_stack_t	*spds = ss->spdsock_spds;
3271 
3272 	switch (level) {
3273 	case SOL_SOCKET:
3274 		switch (name) {
3275 		case SO_SNDBUF:
3276 			if (*i1 > spds->spds_max_buf)
3277 				return (ENOBUFS);
3278 			q->q_hiwat = *i1;
3279 			break;
3280 		case SO_RCVBUF:
3281 			if (*i1 > spds->spds_max_buf)
3282 				return (ENOBUFS);
3283 			RD(q)->q_hiwat = *i1;
3284 			(void) proto_set_rx_hiwat(RD(q), NULL, *i1);
3285 			break;
3286 		}
3287 		break;
3288 	}
3289 	return (0);
3290 }
3291 
3292 
3293 /*
3294  * Handle STREAMS messages.
3295  */
3296 static void
3297 spdsock_wput_other(queue_t *q, mblk_t *mp)
3298 {
3299 	struct iocblk *iocp;
3300 	int error;
3301 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
3302 	spd_stack_t	*spds = ss->spdsock_spds;
3303 	cred_t		*cr;
3304 
3305 	switch (mp->b_datap->db_type) {
3306 	case M_PROTO:
3307 	case M_PCPROTO:
3308 		if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
3309 			ss3dbg(spds, (
3310 			    "spdsock_wput_other: Not big enough M_PROTO\n"));
3311 			freemsg(mp);
3312 			return;
3313 		}
3314 		switch (((union T_primitives *)mp->b_rptr)->type) {
3315 		case T_CAPABILITY_REQ:
3316 			spdsock_capability_req(q, mp);
3317 			break;
3318 		case T_INFO_REQ:
3319 			spdsock_info_req(q, mp);
3320 			break;
3321 		case T_SVR4_OPTMGMT_REQ:
3322 		case T_OPTMGMT_REQ:
3323 			/*
3324 			 * All Solaris components should pass a db_credp
3325 			 * for this TPI message, hence we ASSERT.
3326 			 * But in case there is some other M_PROTO that looks
3327 			 * like a TPI message sent by some other kernel
3328 			 * component, we check and return an error.
3329 			 */
3330 			cr = msg_getcred(mp, NULL);
3331 			ASSERT(cr != NULL);
3332 			if (cr == NULL) {
3333 				spdsock_err_ack(q, mp, TSYSERR, EINVAL);
3334 				return;
3335 			}
3336 			if (((union T_primitives *)mp->b_rptr)->type ==
3337 			    T_SVR4_OPTMGMT_REQ) {
3338 				svr4_optcom_req(q, mp, cr, &spdsock_opt_obj);
3339 			} else {
3340 				tpi_optcom_req(q, mp, cr, &spdsock_opt_obj);
3341 			}
3342 			break;
3343 		case T_DATA_REQ:
3344 		case T_EXDATA_REQ:
3345 		case T_ORDREL_REQ:
3346 			/* Illegal for spdsock. */
3347 			freemsg(mp);
3348 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
3349 			break;
3350 		default:
3351 			/* Not supported by spdsock. */
3352 			spdsock_err_ack(q, mp, TNOTSUPPORT, 0);
3353 			break;
3354 		}
3355 		return;
3356 	case M_IOCTL:
3357 		iocp = (struct iocblk *)mp->b_rptr;
3358 		error = EINVAL;
3359 
3360 		switch (iocp->ioc_cmd) {
3361 		case ND_SET:
3362 		case ND_GET:
3363 			if (nd_getset(q, spds->spds_g_nd, mp)) {
3364 				qreply(q, mp);
3365 				return;
3366 			} else
3367 				error = ENOENT;
3368 			/* FALLTHRU */
3369 		default:
3370 			miocnak(q, mp, 0, error);
3371 			return;
3372 		}
3373 	case M_FLUSH:
3374 		if (*mp->b_rptr & FLUSHW) {
3375 			flushq(q, FLUSHALL);
3376 			*mp->b_rptr &= ~FLUSHW;
3377 		}
3378 		if (*mp->b_rptr & FLUSHR) {
3379 			qreply(q, mp);
3380 			return;
3381 		}
3382 		/* Else FALLTHRU */
3383 	}
3384 
3385 	/* If fell through, just black-hole the message. */
3386 	freemsg(mp);
3387 }
3388 
3389 static void
3390 spdsock_wput(queue_t *q, mblk_t *mp)
3391 {
3392 	uint8_t *rptr = mp->b_rptr;
3393 	mblk_t *mp1;
3394 	spdsock_t *ss = (spdsock_t *)q->q_ptr;
3395 	spd_stack_t	*spds = ss->spdsock_spds;
3396 
3397 	/*
3398 	 * If we're dumping, defer processing other messages until the
3399 	 * dump completes.
3400 	 */
3401 	if (ss->spdsock_dump_req != NULL) {
3402 		if (!putq(q, mp))
3403 			freemsg(mp);
3404 		return;
3405 	}
3406 
3407 	switch (mp->b_datap->db_type) {
3408 	case M_DATA:
3409 		/*
3410 		 * Silently discard.
3411 		 */
3412 		ss2dbg(spds, ("raw M_DATA in spdsock.\n"));
3413 		freemsg(mp);
3414 		return;
3415 	case M_PROTO:
3416 	case M_PCPROTO:
3417 		if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
3418 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
3419 				if ((mp1 = mp->b_cont) == NULL) {
3420 					/* No data after T_DATA_REQ. */
3421 					ss2dbg(spds,
3422 					    ("No data after DATA_REQ.\n"));
3423 					freemsg(mp);
3424 					return;
3425 				}
3426 				freeb(mp);
3427 				mp = mp1;
3428 				ss2dbg(spds, ("T_DATA_REQ\n"));
3429 				break;	/* Out of switch. */
3430 			}
3431 		}
3432 		/* FALLTHRU */
3433 	default:
3434 		ss3dbg(spds, ("In default wput case (%d %d).\n",
3435 		    mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
3436 		spdsock_wput_other(q, mp);
3437 		return;
3438 	}
3439 
3440 	/* I now have a PF_POLICY message in an M_DATA block. */
3441 	spdsock_parse(q, mp);
3442 }
3443 
3444 /*
3445  * Device open procedure, called when new queue pair created.
3446  * We are passed the read-side queue.
3447  */
3448 /* ARGSUSED */
3449 static int
3450 spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
3451 {
3452 	spdsock_t *ss;
3453 	queue_t *oq = OTHERQ(q);
3454 	minor_t ssminor;
3455 	netstack_t *ns;
3456 	spd_stack_t *spds;
3457 
3458 	if (secpolicy_ip_config(credp, B_FALSE) != 0)
3459 		return (EPERM);
3460 
3461 	if (q->q_ptr != NULL)
3462 		return (0);  /* Re-open of an already open instance. */
3463 
3464 	if (sflag & MODOPEN)
3465 		return (EINVAL);
3466 
3467 	ns = netstack_find_by_cred(credp);
3468 	ASSERT(ns != NULL);
3469 	spds = ns->netstack_spdsock;
3470 	ASSERT(spds != NULL);
3471 
3472 	ss2dbg(spds, ("Made it into PF_POLICY socket open.\n"));
3473 
3474 	ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP);
3475 	if (ssminor == 0) {
3476 		netstack_rele(spds->spds_netstack);
3477 		return (ENOMEM);
3478 	}
3479 	ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP);
3480 	if (ss == NULL) {
3481 		vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1);
3482 		netstack_rele(spds->spds_netstack);
3483 		return (ENOMEM);
3484 	}
3485 
3486 	ss->spdsock_minor = ssminor;
3487 	ss->spdsock_state = TS_UNBND;
3488 	ss->spdsock_dump_req = NULL;
3489 
3490 	ss->spdsock_spds = spds;
3491 
3492 	q->q_ptr = ss;
3493 	oq->q_ptr = ss;
3494 
3495 	q->q_hiwat = spds->spds_recv_hiwat;
3496 
3497 	oq->q_hiwat = spds->spds_xmit_hiwat;
3498 	oq->q_lowat = spds->spds_xmit_lowat;
3499 
3500 	qprocson(q);
3501 	(void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat);
3502 
3503 	*devp = makedevice(getmajor(*devp), ss->spdsock_minor);
3504 	return (0);
3505 }
3506 
3507 /*
3508  * Read-side service procedure, invoked when we get back-enabled
3509  * when buffer space becomes available.
3510  *
3511  * Dump another chunk if we were dumping before; when we finish, kick
3512  * the write-side queue in case it's waiting for read queue space.
3513  */
3514 void
3515 spdsock_rsrv(queue_t *q)
3516 {
3517 	spdsock_t *ss = q->q_ptr;
3518 
3519 	if (ss->spdsock_dump_req != NULL)
3520 		spdsock_dump_some(q, ss);
3521 
3522 	if (ss->spdsock_dump_req == NULL)
3523 		qenable(OTHERQ(q));
3524 }
3525 
3526 /*
3527  * Write-side service procedure, invoked when we defer processing
3528  * if another message is received while a dump is in progress.
3529  */
3530 void
3531 spdsock_wsrv(queue_t *q)
3532 {
3533 	spdsock_t *ss = q->q_ptr;
3534 	mblk_t *mp;
3535 	ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
3536 
3537 	if (ss->spdsock_dump_req != NULL) {
3538 		qenable(OTHERQ(q));
3539 		return;
3540 	}
3541 
3542 	while ((mp = getq(q)) != NULL) {
3543 		if (ipsec_loaded(ipss)) {
3544 			spdsock_wput(q, mp);
3545 			if (ss->spdsock_dump_req != NULL)
3546 				return;
3547 		} else if (!ipsec_failed(ipss)) {
3548 			(void) putq(q, mp);
3549 		} else {
3550 			spdsock_error(q, mp, EPFNOSUPPORT, 0);
3551 		}
3552 	}
3553 }
3554 
3555 static int
3556 spdsock_close(queue_t *q)
3557 {
3558 	spdsock_t *ss = q->q_ptr;
3559 	spd_stack_t	*spds = ss->spdsock_spds;
3560 
3561 	qprocsoff(q);
3562 
3563 	/* Safe assumption. */
3564 	ASSERT(ss != NULL);
3565 
3566 	if (ss->spdsock_timeout != 0)
3567 		(void) quntimeout(q, ss->spdsock_timeout);
3568 
3569 	ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n"));
3570 
3571 	vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1);
3572 	netstack_rele(ss->spdsock_spds->spds_netstack);
3573 
3574 	kmem_free(ss, sizeof (spdsock_t));
3575 	return (0);
3576 }
3577 
3578 /*
3579  * Merge the IPsec algorithms tables with the received algorithm information.
3580  */
3581 void
3582 spdsock_merge_algs(spd_stack_t *spds)
3583 {
3584 	ipsec_alginfo_t *alg, *oalg;
3585 	ipsec_algtype_t algtype;
3586 	uint_t algidx, algid, nalgs;
3587 	crypto_mech_name_t *mechs;
3588 	uint_t mech_count, mech_idx;
3589 	netstack_t	*ns = spds->spds_netstack;
3590 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
3591 
3592 	ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
3593 
3594 	/*
3595 	 * Get the list of supported mechanisms from the crypto framework.
3596 	 * If a mechanism is supported by KCF, resolve its mechanism
3597 	 * id and mark it as being valid. This operation must be done
3598 	 * without holding alg_lock, since it can cause a provider
3599 	 * module to be loaded and the provider notification callback to
3600 	 * be invoked.
3601 	 */
3602 	mechs = crypto_get_mech_list(&mech_count, KM_SLEEP);
3603 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3604 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
3605 			int algflags = 0;
3606 			crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID;
3607 
3608 			alg = spds->spds_algs[algtype][algid];
3609 			if (alg == NULL)
3610 				continue;
3611 
3612 			/*
3613 			 * The NULL encryption algorithm is a special
3614 			 * case because there are no mechanisms, yet
3615 			 * the algorithm is still valid.
3616 			 */
3617 			if (alg->alg_id == SADB_EALG_NULL) {
3618 				alg->alg_mech_type = CRYPTO_MECHANISM_INVALID;
3619 				alg->alg_flags |= ALG_FLAG_VALID;
3620 				continue;
3621 			}
3622 
3623 			for (mech_idx = 0; mech_idx < mech_count; mech_idx++) {
3624 				if (strncmp(alg->alg_mech_name, mechs[mech_idx],
3625 				    CRYPTO_MAX_MECH_NAME) == 0) {
3626 					mt = crypto_mech2id(alg->alg_mech_name);
3627 					ASSERT(mt != CRYPTO_MECHANISM_INVALID);
3628 					algflags = ALG_FLAG_VALID;
3629 					break;
3630 				}
3631 			}
3632 			alg->alg_mech_type = mt;
3633 			alg->alg_flags |= algflags;
3634 		}
3635 	}
3636 
3637 	mutex_enter(&ipss->ipsec_alg_lock);
3638 
3639 	/*
3640 	 * For each algorithm currently defined, check if it is
3641 	 * present in the new tables created from the SPD_UPDATEALGS
3642 	 * message received from user-space.
3643 	 * Delete the algorithm entries that are currently defined
3644 	 * but not part of the new tables.
3645 	 */
3646 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3647 		nalgs = ipss->ipsec_nalgs[algtype];
3648 		for (algidx = 0; algidx < nalgs; algidx++) {
3649 			algid = ipss->ipsec_sortlist[algtype][algidx];
3650 			if (spds->spds_algs[algtype][algid] == NULL)
3651 				ipsec_alg_unreg(algtype, algid, ns);
3652 		}
3653 	}
3654 
3655 	/*
3656 	 * For each algorithm we just received, check if it is
3657 	 * present in the currently defined tables. If it is, swap
3658 	 * the entry with the one we just allocated.
3659 	 * If the new algorithm is not in the current tables,
3660 	 * add it.
3661 	 */
3662 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3663 		for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
3664 			alg = spds->spds_algs[algtype][algid];
3665 			if (alg == NULL)
3666 				continue;
3667 
3668 			if ((oalg = ipss->ipsec_alglists[algtype][algid]) ==
3669 			    NULL) {
3670 				/*
3671 				 * New algorithm, add it to the algorithm
3672 				 * table.
3673 				 */
3674 				ipsec_alg_reg(algtype, alg, ns);
3675 			} else {
3676 				/*
3677 				 * Algorithm is already in the table. Swap
3678 				 * the existing entry with the new one.
3679 				 */
3680 				ipsec_alg_fix_min_max(alg, algtype, ns);
3681 				ipss->ipsec_alglists[algtype][algid] = alg;
3682 				ipsec_alg_free(oalg);
3683 			}
3684 			spds->spds_algs[algtype][algid] = NULL;
3685 		}
3686 	}
3687 
3688 	for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3689 		ipss->ipsec_algs_exec_mode[algtype] =
3690 		    spds->spds_algs_exec_mode[algtype];
3691 	}
3692 
3693 	mutex_exit(&ipss->ipsec_alg_lock);
3694 
3695 	crypto_free_mech_list(mechs, mech_count);
3696 
3697 	ipsecah_algs_changed(ns);
3698 	ipsecesp_algs_changed(ns);
3699 }
3700