xref: /onnv-gate/usr/src/uts/common/inet/ip/keysock.c (revision 3448:aaf16568054b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52465Sdanmcd  * Common Development and Distribution License (the "License").
62465Sdanmcd  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/strsubr.h>
330Sstevel@tonic-gate #include <sys/strsun.h>
340Sstevel@tonic-gate #include <sys/stropts.h>
350Sstevel@tonic-gate #include <sys/vnode.h>
36*3448Sdh155122 #include <sys/zone.h>
370Sstevel@tonic-gate #include <sys/strlog.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
400Sstevel@tonic-gate #include <sys/tihdr.h>
410Sstevel@tonic-gate #include <sys/timod.h>
420Sstevel@tonic-gate #include <sys/tiuser.h>
430Sstevel@tonic-gate #include <sys/ddi.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/sunldi.h>
460Sstevel@tonic-gate #include <sys/file.h>
470Sstevel@tonic-gate #include <sys/modctl.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/cmn_err.h>
510Sstevel@tonic-gate #include <sys/proc.h>
520Sstevel@tonic-gate #include <sys/suntpi.h>
530Sstevel@tonic-gate #include <sys/atomic.h>
540Sstevel@tonic-gate #include <sys/mkdev.h>
550Sstevel@tonic-gate #include <sys/policy.h>
56*3448Sdh155122 #include <sys/disp.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/socket.h>
590Sstevel@tonic-gate #include <netinet/in.h>
600Sstevel@tonic-gate #include <net/pfkeyv2.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include <inet/common.h>
630Sstevel@tonic-gate #include <netinet/ip6.h>
640Sstevel@tonic-gate #include <inet/ip.h>
650Sstevel@tonic-gate #include <inet/mi.h>
660Sstevel@tonic-gate #include <inet/nd.h>
670Sstevel@tonic-gate #include <inet/optcom.h>
680Sstevel@tonic-gate #include <inet/ipsec_info.h>
690Sstevel@tonic-gate #include <inet/ipsec_impl.h>
700Sstevel@tonic-gate #include <inet/keysock.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <sys/isa_defs.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * This is a transport provider for the PF_KEY key mangement socket.
760Sstevel@tonic-gate  * (See RFC 2367 for details.)
770Sstevel@tonic-gate  * Downstream messages are wrapped in a keysock consumer interface KEYSOCK_IN
780Sstevel@tonic-gate  * messages (see ipsec_info.h), and passed to the appropriate consumer.
790Sstevel@tonic-gate  * Upstream messages are generated for all open PF_KEY sockets, when
800Sstevel@tonic-gate  * appropriate, as well as the sender (as long as SO_USELOOPBACK is enabled)
810Sstevel@tonic-gate  * in reply to downstream messages.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Upstream messages must be created asynchronously for the following
840Sstevel@tonic-gate  * situations:
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  *	1.) A keysock consumer requires an SA, and there is currently none.
870Sstevel@tonic-gate  *	2.) An SA expires, either hard or soft lifetime.
880Sstevel@tonic-gate  *	3.) Other events a consumer deems fit.
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  * The MT model of this is PERMOD, with shared put procedures.  Two types of
910Sstevel@tonic-gate  * messages, SADB_FLUSH and SADB_DUMP, need to lock down the perimeter to send
920Sstevel@tonic-gate  * down the *multiple* messages they create.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static vmem_t *keysock_vmem;		/* for minor numbers. */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #define	KEYSOCK_MAX_CONSUMERS 256
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
1000Sstevel@tonic-gate static struct T_info_ack keysock_g_t_info_ack = {
1010Sstevel@tonic-gate 	T_INFO_ACK,
1020Sstevel@tonic-gate 	T_INFINITE,	/* TSDU_size. Maximum size messages. */
1030Sstevel@tonic-gate 	T_INVALID,	/* ETSDU_size. No expedited data. */
1040Sstevel@tonic-gate 	T_INVALID,	/* CDATA_size. No connect data. */
1050Sstevel@tonic-gate 	T_INVALID,	/* DDATA_size. No disconnect data. */
1060Sstevel@tonic-gate 	0,		/* ADDR_size. */
1070Sstevel@tonic-gate 	0,		/* OPT_size. No user-settable options */
1080Sstevel@tonic-gate 	64 * 1024,	/* TIDU_size. keysock allows maximum size messages. */
1090Sstevel@tonic-gate 	T_COTS,		/* SERV_type. keysock supports connection oriented. */
1100Sstevel@tonic-gate 	TS_UNBND,	/* CURRENT_state. This is set from keysock_state. */
1110Sstevel@tonic-gate 	(XPG4_1)	/* Provider flags */
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
115*3448Sdh155122 typedef struct keysockparam_s {
1160Sstevel@tonic-gate 	uint_t	keysock_param_min;
1170Sstevel@tonic-gate 	uint_t	keysock_param_max;
1180Sstevel@tonic-gate 	uint_t	keysock_param_value;
1190Sstevel@tonic-gate 	char	*keysock_param_name;
1200Sstevel@tonic-gate } keysockparam_t;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * Table of NDD variables supported by keysock. These are loaded into
1240Sstevel@tonic-gate  * keysock_g_nd in keysock_init_nd.
1250Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
1260Sstevel@tonic-gate  */
127*3448Sdh155122 static	keysockparam_t	lcl_param_arr[] = {
1280Sstevel@tonic-gate 	/* min	max	value	name */
1290Sstevel@tonic-gate 	{ 4096, 65536,	8192,	"keysock_xmit_hiwat"},
1300Sstevel@tonic-gate 	{ 0,	65536,	1024,	"keysock_xmit_lowat"},
1310Sstevel@tonic-gate 	{ 4096, 65536,	8192,	"keysock_recv_hiwat"},
1320Sstevel@tonic-gate 	{ 65536, 1024*1024*1024, 256*1024,	"keysock_max_buf"},
1330Sstevel@tonic-gate 	{ 0,	3,	0,	"keysock_debug"},
1340Sstevel@tonic-gate };
135*3448Sdh155122 #define	keystack_xmit_hiwat	keystack_params[0].keysock_param_value
136*3448Sdh155122 #define	keystack_xmit_lowat	keystack_params[1].keysock_param_value
137*3448Sdh155122 #define	keystack_recv_hiwat	keystack_params[2].keysock_param_value
138*3448Sdh155122 #define	keystack_max_buf	keystack_params[3].keysock_param_value
139*3448Sdh155122 #define	keystack_debug	keystack_params[4].keysock_param_value
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #define	ks0dbg(a)	printf a
1420Sstevel@tonic-gate /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
143*3448Sdh155122 #define	ks1dbg(keystack, a)	if (keystack->keystack_debug != 0) printf a
144*3448Sdh155122 #define	ks2dbg(keystack, a)	if (keystack->keystack_debug > 1) printf a
145*3448Sdh155122 #define	ks3dbg(keystack, a)	if (keystack->keystack_debug > 2) printf a
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static int keysock_close(queue_t *);
1480Sstevel@tonic-gate static int keysock_open(queue_t *, dev_t *, int, int, cred_t *);
1490Sstevel@tonic-gate static void keysock_wput(queue_t *, mblk_t *);
1500Sstevel@tonic-gate static void keysock_rput(queue_t *, mblk_t *);
1510Sstevel@tonic-gate static void keysock_rsrv(queue_t *);
1520Sstevel@tonic-gate static void keysock_passup(mblk_t *, sadb_msg_t *, minor_t,
153*3448Sdh155122     keysock_consumer_t *, boolean_t, keysock_stack_t *);
154*3448Sdh155122 static void *keysock_stack_init(netstackid_t stackid, netstack_t *ns);
155*3448Sdh155122 static void keysock_stack_fini(netstackid_t stackid, void *arg);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static struct module_info info = {
1580Sstevel@tonic-gate 	5138, "keysock", 1, INFPSZ, 512, 128
1590Sstevel@tonic-gate };
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static struct qinit rinit = {
1620Sstevel@tonic-gate 	(pfi_t)keysock_rput, (pfi_t)keysock_rsrv, keysock_open, keysock_close,
1630Sstevel@tonic-gate 	NULL, &info
1640Sstevel@tonic-gate };
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static struct qinit winit = {
1670Sstevel@tonic-gate 	(pfi_t)keysock_wput, NULL, NULL, NULL, NULL, &info
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate struct streamtab keysockinfo = {
1710Sstevel@tonic-gate 	&rinit, &winit
1720Sstevel@tonic-gate };
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate extern struct modlinkage *keysock_modlp;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate  * Plumb IPsec.
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  * NOTE:  New "default" modules will need to be loaded here if needed before
1800Sstevel@tonic-gate  *	  boot time.
1810Sstevel@tonic-gate  */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /* Keep these in global space to keep the lint from complaining. */
1840Sstevel@tonic-gate static char *IPSECESP = "ipsecesp";
1850Sstevel@tonic-gate static char *IPSECESPDEV = "/devices/pseudo/ipsecesp@0:ipsecesp";
1860Sstevel@tonic-gate static char *IPSECAH = "ipsecah";
1870Sstevel@tonic-gate static char *IPSECAHDEV = "/devices/pseudo/ipsecah@0:ipsecah";
1880Sstevel@tonic-gate static char *IP6DEV = "/devices/pseudo/ip6@0:ip6";
1890Sstevel@tonic-gate static char *KEYSOCK = "keysock";
1900Sstevel@tonic-gate static char *STRMOD = "strmod";
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * Load the other ipsec modules and plumb them together.
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate int
196*3448Sdh155122 keysock_plumb_ipsec(netstack_t *ns)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	ldi_handle_t	lh, ip6_lh = NULL;
1990Sstevel@tonic-gate 	ldi_ident_t	li = NULL;
2000Sstevel@tonic-gate 	int		err = 0;
2010Sstevel@tonic-gate 	int		muxid, rval;
2020Sstevel@tonic-gate 	boolean_t	esp_present = B_TRUE;
203*3448Sdh155122 	cred_t		*cr;
204*3448Sdh155122 	keysock_stack_t *keystack = ns->netstack_keysock;
2050Sstevel@tonic-gate 
206*3448Sdh155122 #ifdef NS_DEBUG
207*3448Sdh155122 	(void) printf("keysock_plumb_ipsec(%d)\n",
208*3448Sdh155122 	    ns->netstack_stackid);
209*3448Sdh155122 #endif
2100Sstevel@tonic-gate 
211*3448Sdh155122 	keystack->keystack_plumbed = 0;	/* we're trying again.. */
212*3448Sdh155122 
213*3448Sdh155122 	cr = zone_get_kcred(netstackid_to_zoneid(
214*3448Sdh155122 		keystack->keystack_netstack->netstack_stackid));
215*3448Sdh155122 	ASSERT(cr != NULL);
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * Load up the drivers (AH/ESP).
2180Sstevel@tonic-gate 	 *
2190Sstevel@tonic-gate 	 * I do this separately from the actual plumbing in case this function
2200Sstevel@tonic-gate 	 * ever gets called from a diskless boot before the root filesystem is
2210Sstevel@tonic-gate 	 * up.  I don't have to worry about "keysock" because, well, if I'm
2220Sstevel@tonic-gate 	 * here, keysock must've loaded successfully.
2230Sstevel@tonic-gate 	 */
2240Sstevel@tonic-gate 	if (i_ddi_attach_pseudo_node(IPSECAH) == NULL) {
2250Sstevel@tonic-gate 		ks0dbg(("IPsec:  AH failed to attach.\n"));
2260Sstevel@tonic-gate 		goto bail;
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 	if (i_ddi_attach_pseudo_node(IPSECESP) == NULL) {
2290Sstevel@tonic-gate 		ks0dbg(("IPsec:  ESP failed to attach.\n"));
2300Sstevel@tonic-gate 		esp_present = B_FALSE;
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * Set up the IP streams for AH and ESP, as well as tacking keysock
2350Sstevel@tonic-gate 	 * on top of them.  Assume keysock has set the autopushes up already.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/* Open IP. */
2390Sstevel@tonic-gate 	err = ldi_ident_from_mod(keysock_modlp, &li);
2400Sstevel@tonic-gate 	if (err) {
2410Sstevel@tonic-gate 		ks0dbg(("IPsec:  lid_ident_from_mod failed (err %d).\n",
2420Sstevel@tonic-gate 		    err));
2430Sstevel@tonic-gate 		goto bail;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
246*3448Sdh155122 	err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, cr, &ip6_lh, li);
2470Sstevel@tonic-gate 	if (err) {
2480Sstevel@tonic-gate 		ks0dbg(("IPsec:  Open of IP6 failed (err %d).\n", err));
2490Sstevel@tonic-gate 		goto bail;
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/* PLINK KEYSOCK/AH */
253*3448Sdh155122 	err = ldi_open_by_name(IPSECAHDEV, FREAD|FWRITE, cr, &lh, li);
2540Sstevel@tonic-gate 	if (err) {
2550Sstevel@tonic-gate 		ks0dbg(("IPsec:  Open of AH failed (err %d).\n", err));
2560Sstevel@tonic-gate 		goto bail;
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 	err = ldi_ioctl(lh,
259*3448Sdh155122 	    I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, cr, &rval);
2600Sstevel@tonic-gate 	if (err) {
2610Sstevel@tonic-gate 		ks0dbg(("IPsec:  Push of KEYSOCK onto AH failed (err %d).\n",
2620Sstevel@tonic-gate 		    err));
263*3448Sdh155122 		(void) ldi_close(lh, FREAD|FWRITE, cr);
2640Sstevel@tonic-gate 		goto bail;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh,
267*3448Sdh155122 			FREAD+FWRITE+FNOCTTY+FKIOCTL, cr, &muxid);
2680Sstevel@tonic-gate 	if (err) {
2690Sstevel@tonic-gate 		ks0dbg(("IPsec:  PLINK of KEYSOCK/AH failed (err %d).\n", err));
270*3448Sdh155122 		(void) ldi_close(lh, FREAD|FWRITE, cr);
2710Sstevel@tonic-gate 		goto bail;
2720Sstevel@tonic-gate 	}
273*3448Sdh155122 	(void) ldi_close(lh, FREAD|FWRITE, cr);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	/* PLINK KEYSOCK/ESP */
2760Sstevel@tonic-gate 	if (esp_present) {
2770Sstevel@tonic-gate 		err = ldi_open_by_name(IPSECESPDEV,
278*3448Sdh155122 		    FREAD|FWRITE, cr, &lh, li);
2790Sstevel@tonic-gate 		if (err) {
2800Sstevel@tonic-gate 			ks0dbg(("IPsec:  Open of ESP failed (err %d).\n", err));
2810Sstevel@tonic-gate 			goto bail;
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 		err = ldi_ioctl(lh,
284*3448Sdh155122 		    I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, cr, &rval);
2850Sstevel@tonic-gate 		if (err) {
2860Sstevel@tonic-gate 			ks0dbg(("IPsec:  "
2870Sstevel@tonic-gate 			    "Push of KEYSOCK onto ESP failed (err %d).\n",
2880Sstevel@tonic-gate 			    err));
289*3448Sdh155122 			(void) ldi_close(lh, FREAD|FWRITE, cr);
2900Sstevel@tonic-gate 			goto bail;
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 		err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh,
293*3448Sdh155122 				FREAD+FWRITE+FNOCTTY+FKIOCTL, cr, &muxid);
2940Sstevel@tonic-gate 		if (err) {
2950Sstevel@tonic-gate 			ks0dbg(("IPsec:  "
2960Sstevel@tonic-gate 			    "PLINK of KEYSOCK/ESP failed (err %d).\n", err));
297*3448Sdh155122 			(void) ldi_close(lh, FREAD|FWRITE, cr);
2980Sstevel@tonic-gate 			goto bail;
2990Sstevel@tonic-gate 		}
300*3448Sdh155122 		(void) ldi_close(lh, FREAD|FWRITE, cr);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate bail:
304*3448Sdh155122 	keystack->keystack_plumbed = (err == 0) ? 1 : -1;
3050Sstevel@tonic-gate 	if (ip6_lh != NULL) {
306*3448Sdh155122 		(void) ldi_close(ip6_lh, FREAD|FWRITE, cr);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 	if (li != NULL)
3090Sstevel@tonic-gate 		ldi_ident_release(li);
310*3448Sdh155122 #ifdef NS_DEBUG
311*3448Sdh155122 	(void) printf("keysock_plumb_ipsec -> %d\n",
312*3448Sdh155122 	    keystack->keystack_plumbed);
313*3448Sdh155122 #endif
314*3448Sdh155122 	crfree(cr);
3150Sstevel@tonic-gate 	return (err);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate /* ARGSUSED */
3190Sstevel@tonic-gate static int
3200Sstevel@tonic-gate keysock_param_get(q, mp, cp, cr)
3210Sstevel@tonic-gate 	queue_t	*q;
3220Sstevel@tonic-gate 	mblk_t	*mp;
3230Sstevel@tonic-gate 	caddr_t	cp;
3240Sstevel@tonic-gate 	cred_t *cr;
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	keysockparam_t	*keysockpa = (keysockparam_t *)cp;
3270Sstevel@tonic-gate 	uint_t value;
328*3448Sdh155122 	keysock_t *ks = (keysock_t *)q->q_ptr;
329*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
3300Sstevel@tonic-gate 
331*3448Sdh155122 	mutex_enter(&keystack->keystack_param_lock);
3320Sstevel@tonic-gate 	value = keysockpa->keysock_param_value;
333*3448Sdh155122 	mutex_exit(&keystack->keystack_param_lock);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
3360Sstevel@tonic-gate 	return (0);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /* This routine sets an NDD variable in a keysockparam_t structure. */
3400Sstevel@tonic-gate /* ARGSUSED */
3410Sstevel@tonic-gate static int
3420Sstevel@tonic-gate keysock_param_set(q, mp, value, cp, cr)
3430Sstevel@tonic-gate 	queue_t	*q;
3440Sstevel@tonic-gate 	mblk_t	*mp;
3450Sstevel@tonic-gate 	char	*value;
3460Sstevel@tonic-gate 	caddr_t	cp;
3470Sstevel@tonic-gate 	cred_t *cr;
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	ulong_t	new_value;
3500Sstevel@tonic-gate 	keysockparam_t	*keysockpa = (keysockparam_t *)cp;
351*3448Sdh155122 	keysock_t *ks = (keysock_t *)q->q_ptr;
352*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/* Convert the value from a string into a long integer. */
3550Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
3560Sstevel@tonic-gate 		return (EINVAL);
3570Sstevel@tonic-gate 
358*3448Sdh155122 	mutex_enter(&keystack->keystack_param_lock);
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
3610Sstevel@tonic-gate 	 * required bounds.
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 	if (new_value < keysockpa->keysock_param_min ||
3640Sstevel@tonic-gate 	    new_value > keysockpa->keysock_param_max) {
365*3448Sdh155122 		mutex_exit(&keystack->keystack_param_lock);
3660Sstevel@tonic-gate 		return (EINVAL);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	/* Set the new value */
3700Sstevel@tonic-gate 	keysockpa->keysock_param_value = new_value;
371*3448Sdh155122 	mutex_exit(&keystack->keystack_param_lock);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	return (0);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /*
377*3448Sdh155122  * Initialize keysock at module load time
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate boolean_t
3800Sstevel@tonic-gate keysock_ddi_init(void)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	keysock_max_optsize = optcom_max_optsize(
3830Sstevel@tonic-gate 	    keysock_opt_obj.odb_opt_des_arr, keysock_opt_obj.odb_opt_arr_cnt);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	keysock_vmem = vmem_create("keysock", (void *)1, MAXMIN, 1,
3860Sstevel@tonic-gate 	    NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
3870Sstevel@tonic-gate 
388*3448Sdh155122 	/*
389*3448Sdh155122 	 * We want to be informed each time a stack is created or
390*3448Sdh155122 	 * destroyed in the kernel, so we can maintain the
391*3448Sdh155122 	 * set of keysock_stack_t's.
392*3448Sdh155122 	 */
393*3448Sdh155122 	netstack_register(NS_KEYSOCK, keysock_stack_init, NULL,
394*3448Sdh155122 	    keysock_stack_fini);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	return (B_TRUE);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate /*
400*3448Sdh155122  * Walk through the param array specified registering each element with the
401*3448Sdh155122  * named dispatch handler.
402*3448Sdh155122  */
403*3448Sdh155122 static boolean_t
404*3448Sdh155122 keysock_param_register(IDP *ndp, keysockparam_t *ksp, int cnt)
405*3448Sdh155122 {
406*3448Sdh155122 	for (; cnt-- > 0; ksp++) {
407*3448Sdh155122 		if (ksp->keysock_param_name != NULL &&
408*3448Sdh155122 		    ksp->keysock_param_name[0]) {
409*3448Sdh155122 			if (!nd_load(ndp,
410*3448Sdh155122 			    ksp->keysock_param_name,
411*3448Sdh155122 			    keysock_param_get, keysock_param_set,
412*3448Sdh155122 			    (caddr_t)ksp)) {
413*3448Sdh155122 				nd_free(ndp);
414*3448Sdh155122 				return (B_FALSE);
415*3448Sdh155122 			}
416*3448Sdh155122 		}
417*3448Sdh155122 	}
418*3448Sdh155122 	return (B_TRUE);
419*3448Sdh155122 }
420*3448Sdh155122 
421*3448Sdh155122 /*
422*3448Sdh155122  * Initialize keysock for one stack instance
423*3448Sdh155122  */
424*3448Sdh155122 /* ARGSUSED */
425*3448Sdh155122 static void *
426*3448Sdh155122 keysock_stack_init(netstackid_t stackid, netstack_t *ns)
427*3448Sdh155122 {
428*3448Sdh155122 	keysock_stack_t	*keystack;
429*3448Sdh155122 	keysockparam_t *ksp;
430*3448Sdh155122 
431*3448Sdh155122 	keystack = (keysock_stack_t *)kmem_zalloc(sizeof (*keystack), KM_SLEEP);
432*3448Sdh155122 	keystack->keystack_netstack = ns;
433*3448Sdh155122 
434*3448Sdh155122 	keystack->keystack_acquire_seq = 0xffffffff;
435*3448Sdh155122 
436*3448Sdh155122 	ksp = (keysockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
437*3448Sdh155122 	keystack->keystack_params = ksp;
438*3448Sdh155122 	bcopy(lcl_param_arr, ksp, sizeof (lcl_param_arr));
439*3448Sdh155122 
440*3448Sdh155122 	(void) keysock_param_register(&keystack->keystack_g_nd, ksp,
441*3448Sdh155122 	    A_CNT(lcl_param_arr));
442*3448Sdh155122 
443*3448Sdh155122 	mutex_init(&keystack->keystack_list_lock, NULL, MUTEX_DEFAULT, NULL);
444*3448Sdh155122 	mutex_init(&keystack->keystack_consumers_lock,
445*3448Sdh155122 	    NULL, MUTEX_DEFAULT, NULL);
446*3448Sdh155122 	mutex_init(&keystack->keystack_param_lock, NULL, MUTEX_DEFAULT, NULL);
447*3448Sdh155122 	return (keystack);
448*3448Sdh155122 }
449*3448Sdh155122 
450*3448Sdh155122 /*
4510Sstevel@tonic-gate  * Free NDD variable space, and other destructors, for keysock.
4520Sstevel@tonic-gate  */
4530Sstevel@tonic-gate void
4540Sstevel@tonic-gate keysock_ddi_destroy(void)
4550Sstevel@tonic-gate {
456*3448Sdh155122 	netstack_unregister(NS_KEYSOCK);
4570Sstevel@tonic-gate 	vmem_destroy(keysock_vmem);
458*3448Sdh155122 }
459*3448Sdh155122 
460*3448Sdh155122 /*
461*3448Sdh155122  * Remove one stack instance from keysock
462*3448Sdh155122  */
463*3448Sdh155122 /* ARGSUSED */
464*3448Sdh155122 static void
465*3448Sdh155122 keysock_stack_fini(netstackid_t stackid, void *arg)
466*3448Sdh155122 {
467*3448Sdh155122 	keysock_stack_t *keystack = (keysock_stack_t *)arg;
468*3448Sdh155122 
469*3448Sdh155122 	nd_free(&keystack->keystack_g_nd);
470*3448Sdh155122 	kmem_free(keystack->keystack_params, sizeof (lcl_param_arr));
471*3448Sdh155122 	keystack->keystack_params = NULL;
472*3448Sdh155122 
473*3448Sdh155122 	mutex_destroy(&keystack->keystack_list_lock);
474*3448Sdh155122 	mutex_destroy(&keystack->keystack_consumers_lock);
475*3448Sdh155122 	mutex_destroy(&keystack->keystack_param_lock);
476*3448Sdh155122 
477*3448Sdh155122 	kmem_free(keystack, sizeof (*keystack));
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate  * Close routine for keysock.
4820Sstevel@tonic-gate  */
4830Sstevel@tonic-gate static int
4840Sstevel@tonic-gate keysock_close(queue_t *q)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	keysock_t *ks;
4870Sstevel@tonic-gate 	keysock_consumer_t *kc;
4880Sstevel@tonic-gate 	void *ptr = q->q_ptr;
4890Sstevel@tonic-gate 	int size;
490*3448Sdh155122 	keysock_stack_t	*keystack;
491*3448Sdh155122 
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	qprocsoff(q);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/* Safe assumption. */
4960Sstevel@tonic-gate 	ASSERT(ptr != NULL);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	if (WR(q)->q_next) {
4990Sstevel@tonic-gate 		kc = (keysock_consumer_t *)ptr;
500*3448Sdh155122 		keystack = kc->kc_keystack;
501*3448Sdh155122 
502*3448Sdh155122 		ks1dbg(keystack, ("Module close, removing a consumer (%d).\n",
5030Sstevel@tonic-gate 		    kc->kc_sa_type));
5040Sstevel@tonic-gate 		/*
5050Sstevel@tonic-gate 		 * Because of PERMOD open/close exclusive perimeter, I
5060Sstevel@tonic-gate 		 * can inspect KC_FLUSHING w/o locking down kc->kc_lock.
5070Sstevel@tonic-gate 		 */
5080Sstevel@tonic-gate 		if (kc->kc_flags & KC_FLUSHING) {
5090Sstevel@tonic-gate 			/*
5100Sstevel@tonic-gate 			 * If this decrement was the last one, send
5110Sstevel@tonic-gate 			 * down the next pending one, if any.
5120Sstevel@tonic-gate 			 *
5130Sstevel@tonic-gate 			 * With a PERMOD perimeter, the mutexes ops aren't
5140Sstevel@tonic-gate 			 * really necessary, but if we ever loosen up, we will
5150Sstevel@tonic-gate 			 * have this bit covered already.
5160Sstevel@tonic-gate 			 */
517*3448Sdh155122 			keystack->keystack_flushdump--;
518*3448Sdh155122 			if (keystack->keystack_flushdump == 0) {
5190Sstevel@tonic-gate 				/*
5200Sstevel@tonic-gate 				 * The flush/dump terminated by having a
5210Sstevel@tonic-gate 				 * consumer go away.  I need to send up to the
5220Sstevel@tonic-gate 				 * appropriate keysock all of the relevant
5230Sstevel@tonic-gate 				 * information.  Unfortunately, I don't
5240Sstevel@tonic-gate 				 * have that handy.
5250Sstevel@tonic-gate 				 */
5260Sstevel@tonic-gate 				ks0dbg(("Consumer went away while flushing or"
5270Sstevel@tonic-gate 				    " dumping.\n"));
5280Sstevel@tonic-gate 			}
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 		size = sizeof (keysock_consumer_t);
531*3448Sdh155122 		mutex_enter(&keystack->keystack_consumers_lock);
532*3448Sdh155122 		keystack->keystack_consumers[kc->kc_sa_type] = NULL;
533*3448Sdh155122 		mutex_exit(&keystack->keystack_consumers_lock);
5340Sstevel@tonic-gate 		mutex_destroy(&kc->kc_lock);
535*3448Sdh155122 		netstack_rele(kc->kc_keystack->keystack_netstack);
5360Sstevel@tonic-gate 	} else {
5370Sstevel@tonic-gate 		ks = (keysock_t *)ptr;
538*3448Sdh155122 		keystack = ks->keysock_keystack;
539*3448Sdh155122 
540*3448Sdh155122 		ks3dbg(keystack,
541*3448Sdh155122 		    ("Driver close, PF_KEY socket is going away.\n"));
5420Sstevel@tonic-gate 		if ((ks->keysock_flags & KEYSOCK_EXTENDED) != 0)
543*3448Sdh155122 			atomic_add_32(&keystack->keystack_num_extended, -1);
5440Sstevel@tonic-gate 		size = sizeof (keysock_t);
545*3448Sdh155122 		mutex_enter(&keystack->keystack_list_lock);
5460Sstevel@tonic-gate 		*(ks->keysock_ptpn) = ks->keysock_next;
5470Sstevel@tonic-gate 		if (ks->keysock_next != NULL)
5480Sstevel@tonic-gate 			ks->keysock_next->keysock_ptpn = ks->keysock_ptpn;
549*3448Sdh155122 		mutex_exit(&keystack->keystack_list_lock);
5500Sstevel@tonic-gate 		mutex_destroy(&ks->keysock_lock);
5512465Sdanmcd 		vmem_free(keysock_vmem, (void *)(uintptr_t)ks->keysock_serial,
5522465Sdanmcd 		    1);
553*3448Sdh155122 		netstack_rele(ks->keysock_keystack->keystack_netstack);
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/* Now I'm free. */
5570Sstevel@tonic-gate 	kmem_free(ptr, size);
5580Sstevel@tonic-gate 	return (0);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate /*
5610Sstevel@tonic-gate  * Open routine for keysock.
5620Sstevel@tonic-gate  */
5630Sstevel@tonic-gate /* ARGSUSED */
5640Sstevel@tonic-gate static int
5650Sstevel@tonic-gate keysock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	keysock_t *ks;
5680Sstevel@tonic-gate 	keysock_consumer_t *kc;
5690Sstevel@tonic-gate 	mblk_t *mp;
5700Sstevel@tonic-gate 	ipsec_info_t *ii;
571*3448Sdh155122 	netstack_t *ns;
572*3448Sdh155122 	keysock_stack_t *keystack;
5730Sstevel@tonic-gate 
574*3448Sdh155122 	if (secpolicy_ip_config(credp, B_FALSE) != 0) {
5750Sstevel@tonic-gate 		/* Privilege debugging will log the error */
5760Sstevel@tonic-gate 		return (EPERM);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5800Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
5810Sstevel@tonic-gate 
582*3448Sdh155122 	ns = netstack_find_by_cred(credp);
583*3448Sdh155122 	ASSERT(ns != NULL);
584*3448Sdh155122 	keystack = ns->netstack_keysock;
585*3448Sdh155122 	ASSERT(keystack != NULL);
586*3448Sdh155122 
587*3448Sdh155122 	ks3dbg(keystack, ("Entering keysock open.\n"));
588*3448Sdh155122 
589*3448Sdh155122 	if (keystack->keystack_plumbed < 1) {
590*3448Sdh155122 		netstack_t *ns = keystack->keystack_netstack;
591*3448Sdh155122 
592*3448Sdh155122 		keystack->keystack_plumbed = 0;
593*3448Sdh155122 #ifdef NS_DEBUG
594*3448Sdh155122 		printf("keysock_open(%d) - plumb\n",
595*3448Sdh155122 		    keystack->keystack_netstack->netstack_stackid);
596*3448Sdh155122 #endif
5970Sstevel@tonic-gate 		/*
5980Sstevel@tonic-gate 		 * Don't worry about ipsec_failure being true here.
5990Sstevel@tonic-gate 		 * (See ip.c).  An open of keysock should try and force
6000Sstevel@tonic-gate 		 * the issue.  Maybe it was a transient failure.
6010Sstevel@tonic-gate 		 */
602*3448Sdh155122 		ipsec_loader_loadnow(ns->netstack_ipsec);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (sflag & MODOPEN) {
6060Sstevel@tonic-gate 		/* Initialize keysock_consumer state here. */
6070Sstevel@tonic-gate 		kc = kmem_zalloc(sizeof (keysock_consumer_t), KM_NOSLEEP);
608*3448Sdh155122 		if (kc == NULL) {
609*3448Sdh155122 			netstack_rele(keystack->keystack_netstack);
6100Sstevel@tonic-gate 			return (ENOMEM);
611*3448Sdh155122 		}
6120Sstevel@tonic-gate 		mutex_init(&kc->kc_lock, NULL, MUTEX_DEFAULT, 0);
6130Sstevel@tonic-gate 		kc->kc_rq = q;
6140Sstevel@tonic-gate 		kc->kc_wq = WR(q);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		q->q_ptr = kc;
6170Sstevel@tonic-gate 		WR(q)->q_ptr = kc;
6180Sstevel@tonic-gate 
619*3448Sdh155122 		kc->kc_keystack = keystack;
6200Sstevel@tonic-gate 		qprocson(q);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		/*
6230Sstevel@tonic-gate 		 * Send down initial message to whatever I was pushed on top
6240Sstevel@tonic-gate 		 * of asking for its consumer type.  The reply will set it.
6250Sstevel@tonic-gate 		 */
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		/* Allocate it. */
6280Sstevel@tonic-gate 		mp = allocb(sizeof (ipsec_info_t), BPRI_HI);
6290Sstevel@tonic-gate 		if (mp == NULL) {
630*3448Sdh155122 			ks1dbg(keystack, (
6310Sstevel@tonic-gate 			    "keysock_open:  Cannot allocate KEYSOCK_HELLO.\n"));
6320Sstevel@tonic-gate 			/* Do I need to set these to null? */
6330Sstevel@tonic-gate 			q->q_ptr = NULL;
6340Sstevel@tonic-gate 			WR(q)->q_ptr = NULL;
6350Sstevel@tonic-gate 			mutex_destroy(&kc->kc_lock);
6360Sstevel@tonic-gate 			kmem_free(kc, sizeof (*kc));
637*3448Sdh155122 			netstack_rele(keystack->keystack_netstack);
6380Sstevel@tonic-gate 			return (ENOMEM);
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		/* If I allocated okay, putnext to what I was pushed atop. */
6420Sstevel@tonic-gate 		mp->b_wptr += sizeof (ipsec_info_t);
6430Sstevel@tonic-gate 		mp->b_datap->db_type = M_CTL;
6440Sstevel@tonic-gate 		ii = (ipsec_info_t *)mp->b_rptr;
6450Sstevel@tonic-gate 		ii->ipsec_info_type = KEYSOCK_HELLO;
6460Sstevel@tonic-gate 		/* Length only of type/len. */
6470Sstevel@tonic-gate 		ii->ipsec_info_len = sizeof (ii->ipsec_allu);
648*3448Sdh155122 		ks2dbg(keystack, ("Ready to putnext KEYSOCK_HELLO.\n"));
6490Sstevel@tonic-gate 		putnext(kc->kc_wq, mp);
6500Sstevel@tonic-gate 	} else {
6510Sstevel@tonic-gate 		minor_t ksminor;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		/* Initialize keysock state. */
6540Sstevel@tonic-gate 
655*3448Sdh155122 		ks2dbg(keystack, ("Made it into PF_KEY socket open.\n"));
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 		ksminor = (minor_t)(uintptr_t)
6580Sstevel@tonic-gate 		    vmem_alloc(keysock_vmem, 1, VM_NOSLEEP);
659*3448Sdh155122 		if (ksminor == 0) {
660*3448Sdh155122 			netstack_rele(keystack->keystack_netstack);
6610Sstevel@tonic-gate 			return (ENOMEM);
662*3448Sdh155122 		}
6630Sstevel@tonic-gate 		ks = kmem_zalloc(sizeof (keysock_t), KM_NOSLEEP);
6640Sstevel@tonic-gate 		if (ks == NULL) {
6650Sstevel@tonic-gate 			vmem_free(keysock_vmem, (void *)(uintptr_t)ksminor, 1);
666*3448Sdh155122 			netstack_rele(keystack->keystack_netstack);
6670Sstevel@tonic-gate 			return (ENOMEM);
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 		mutex_init(&ks->keysock_lock, NULL, MUTEX_DEFAULT, 0);
6710Sstevel@tonic-gate 		ks->keysock_rq = q;
6720Sstevel@tonic-gate 		ks->keysock_wq = WR(q);
6730Sstevel@tonic-gate 		ks->keysock_state = TS_UNBND;
6740Sstevel@tonic-gate 		ks->keysock_serial = ksminor;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		q->q_ptr = ks;
6770Sstevel@tonic-gate 		WR(q)->q_ptr = ks;
678*3448Sdh155122 		ks->keysock_keystack = keystack;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		/*
6810Sstevel@tonic-gate 		 * The receive hiwat is only looked at on the stream head
6820Sstevel@tonic-gate 		 * queue.  Store in q_hiwat in order to return on SO_RCVBUF
6830Sstevel@tonic-gate 		 * getsockopts.
6840Sstevel@tonic-gate 		 */
6850Sstevel@tonic-gate 
686*3448Sdh155122 		q->q_hiwat = keystack->keystack_recv_hiwat;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		/*
6890Sstevel@tonic-gate 		 * The transmit hiwat/lowat is only looked at on IP's queue.
6900Sstevel@tonic-gate 		 * Store in q_hiwat/q_lowat in order to return on
6910Sstevel@tonic-gate 		 * SO_SNDBUF/SO_SNDLOWAT getsockopts.
6920Sstevel@tonic-gate 		 */
6930Sstevel@tonic-gate 
694*3448Sdh155122 		WR(q)->q_hiwat = keystack->keystack_xmit_hiwat;
695*3448Sdh155122 		WR(q)->q_lowat = keystack->keystack_xmit_lowat;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		*devp = makedevice(getmajor(*devp), ksminor);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		/*
7000Sstevel@tonic-gate 		 * Thread keysock into the global keysock list.
7010Sstevel@tonic-gate 		 */
702*3448Sdh155122 		mutex_enter(&keystack->keystack_list_lock);
703*3448Sdh155122 		ks->keysock_next = keystack->keystack_list;
704*3448Sdh155122 		ks->keysock_ptpn = &keystack->keystack_list;
705*3448Sdh155122 		if (keystack->keystack_list != NULL) {
706*3448Sdh155122 			keystack->keystack_list->keysock_ptpn =
707*3448Sdh155122 			    &ks->keysock_next;
708*3448Sdh155122 		}
709*3448Sdh155122 		keystack->keystack_list = ks;
710*3448Sdh155122 		mutex_exit(&keystack->keystack_list_lock);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		qprocson(q);
713*3448Sdh155122 		(void) mi_set_sth_hiwat(q, keystack->keystack_recv_hiwat);
7140Sstevel@tonic-gate 		/*
7150Sstevel@tonic-gate 		 * Wait outside the keysock module perimeter for IPsec
7160Sstevel@tonic-gate 		 * plumbing to be completed.  If it fails, keysock_close()
7170Sstevel@tonic-gate 		 * undoes everything we just did.
7180Sstevel@tonic-gate 		 */
719*3448Sdh155122 		if (!ipsec_loader_wait(q,
720*3448Sdh155122 		    keystack->keystack_netstack->netstack_ipsec)) {
7210Sstevel@tonic-gate 			(void) keysock_close(q);
7220Sstevel@tonic-gate 			return (EPFNOSUPPORT);
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	return (0);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_wput(). */
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate /*
7320Sstevel@tonic-gate  * Copy relevant state bits.
7330Sstevel@tonic-gate  */
7340Sstevel@tonic-gate static void
7350Sstevel@tonic-gate keysock_copy_info(struct T_info_ack *tap, keysock_t *ks)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	*tap = keysock_g_t_info_ack;
7380Sstevel@tonic-gate 	tap->CURRENT_state = ks->keysock_state;
7390Sstevel@tonic-gate 	tap->OPT_size = keysock_max_optsize;
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate /*
7430Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
7440Sstevel@tonic-gate  * keysock_wput.  Much of the T_CAPABILITY_ACK information is copied from
7450Sstevel@tonic-gate  * keysock_g_t_info_ack.  The current state of the stream is copied from
7460Sstevel@tonic-gate  * keysock_state.
7470Sstevel@tonic-gate  */
7480Sstevel@tonic-gate static void
7490Sstevel@tonic-gate keysock_capability_req(queue_t *q, mblk_t *mp)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
7520Sstevel@tonic-gate 	t_uscalar_t cap_bits1;
7530Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
7580Sstevel@tonic-gate 		mp->b_datap->db_type, T_CAPABILITY_ACK);
7590Sstevel@tonic-gate 	if (mp == NULL)
7600Sstevel@tonic-gate 		return;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
7630Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
7660Sstevel@tonic-gate 		keysock_copy_info(&tcap->INFO_ack, ks);
7670Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	qreply(q, mp);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages. It is called by
7750Sstevel@tonic-gate  * keysock_wput_other.
7760Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from keysock_g_t_info_ack.
7770Sstevel@tonic-gate  * The current state of the stream is copied from keysock_state.
7780Sstevel@tonic-gate  */
7790Sstevel@tonic-gate static void
7800Sstevel@tonic-gate keysock_info_req(q, mp)
7810Sstevel@tonic-gate 	queue_t	*q;
7820Sstevel@tonic-gate 	mblk_t	*mp;
7830Sstevel@tonic-gate {
7840Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
7850Sstevel@tonic-gate 	    T_INFO_ACK);
7860Sstevel@tonic-gate 	if (mp == NULL)
7870Sstevel@tonic-gate 		return;
7880Sstevel@tonic-gate 	keysock_copy_info((struct T_info_ack *)mp->b_rptr,
7890Sstevel@tonic-gate 	    (keysock_t *)q->q_ptr);
7900Sstevel@tonic-gate 	qreply(q, mp);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate /*
7940Sstevel@tonic-gate  * keysock_err_ack. This routine creates a
7950Sstevel@tonic-gate  * T_ERROR_ACK message and passes it
7960Sstevel@tonic-gate  * upstream.
7970Sstevel@tonic-gate  */
7980Sstevel@tonic-gate static void
7990Sstevel@tonic-gate keysock_err_ack(q, mp, t_error, sys_error)
8000Sstevel@tonic-gate 	queue_t	*q;
8010Sstevel@tonic-gate 	mblk_t	*mp;
8020Sstevel@tonic-gate 	int	t_error;
8030Sstevel@tonic-gate 	int	sys_error;
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
8060Sstevel@tonic-gate 		qreply(q, mp);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
8110Sstevel@tonic-gate  * It returns the size of the option retrieved.
8120Sstevel@tonic-gate  */
8130Sstevel@tonic-gate /* ARGSUSED */
8140Sstevel@tonic-gate int
8150Sstevel@tonic-gate keysock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate 	int *i1 = (int *)ptr;
8180Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	switch (level) {
8210Sstevel@tonic-gate 	case SOL_SOCKET:
8220Sstevel@tonic-gate 		mutex_enter(&ks->keysock_lock);
8230Sstevel@tonic-gate 		switch (name) {
8240Sstevel@tonic-gate 		case SO_TYPE:
8250Sstevel@tonic-gate 			*i1 = SOCK_RAW;
8260Sstevel@tonic-gate 			break;
8270Sstevel@tonic-gate 		case SO_USELOOPBACK:
8280Sstevel@tonic-gate 			*i1 = (int)(!((ks->keysock_flags & KEYSOCK_NOLOOP) ==
8290Sstevel@tonic-gate 			    KEYSOCK_NOLOOP));
8300Sstevel@tonic-gate 			break;
8310Sstevel@tonic-gate 		/*
8320Sstevel@tonic-gate 		 * The following two items can be manipulated,
8330Sstevel@tonic-gate 		 * but changing them should do nothing.
8340Sstevel@tonic-gate 		 */
8350Sstevel@tonic-gate 		case SO_SNDBUF:
8360Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
8370Sstevel@tonic-gate 			break;
8380Sstevel@tonic-gate 		case SO_RCVBUF:
8390Sstevel@tonic-gate 			*i1 = (int)(RD(q)->q_hiwat);
8400Sstevel@tonic-gate 			break;
8410Sstevel@tonic-gate 		}
8420Sstevel@tonic-gate 		mutex_exit(&ks->keysock_lock);
8430Sstevel@tonic-gate 		break;
8440Sstevel@tonic-gate 	default:
8450Sstevel@tonic-gate 		return (0);
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 	return (sizeof (int));
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate /*
8510Sstevel@tonic-gate  * This routine sets socket options.
8520Sstevel@tonic-gate  */
8530Sstevel@tonic-gate /* ARGSUSED */
8540Sstevel@tonic-gate int
8550Sstevel@tonic-gate keysock_opt_set(queue_t *q, uint_t mgmt_flags, int level,
8560Sstevel@tonic-gate     int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
8570Sstevel@tonic-gate     uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate 	int *i1 = (int *)invalp;
8600Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
861*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	switch (level) {
8640Sstevel@tonic-gate 	case SOL_SOCKET:
8650Sstevel@tonic-gate 		mutex_enter(&ks->keysock_lock);
8660Sstevel@tonic-gate 		switch (name) {
8670Sstevel@tonic-gate 		case SO_USELOOPBACK:
8680Sstevel@tonic-gate 			if (!(*i1))
8690Sstevel@tonic-gate 				ks->keysock_flags |= KEYSOCK_NOLOOP;
8700Sstevel@tonic-gate 			else ks->keysock_flags &= ~KEYSOCK_NOLOOP;
8710Sstevel@tonic-gate 			break;
8720Sstevel@tonic-gate 		case SO_SNDBUF:
873*3448Sdh155122 			if (*i1 > keystack->keystack_max_buf)
8740Sstevel@tonic-gate 				return (ENOBUFS);
8750Sstevel@tonic-gate 			q->q_hiwat = *i1;
8760Sstevel@tonic-gate 			break;
8770Sstevel@tonic-gate 		case SO_RCVBUF:
878*3448Sdh155122 			if (*i1 > keystack->keystack_max_buf)
8790Sstevel@tonic-gate 				return (ENOBUFS);
8800Sstevel@tonic-gate 			RD(q)->q_hiwat = *i1;
8810Sstevel@tonic-gate 			(void) mi_set_sth_hiwat(RD(q), *i1);
8820Sstevel@tonic-gate 			break;
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 		mutex_exit(&ks->keysock_lock);
8850Sstevel@tonic-gate 		break;
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	return (0);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate  * Handle STREAMS messages.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate static void
8940Sstevel@tonic-gate keysock_wput_other(queue_t *q, mblk_t *mp)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	struct iocblk *iocp;
8970Sstevel@tonic-gate 	int error;
898*3448Sdh155122 	keysock_t *ks = (keysock_t *)q->q_ptr;
899*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
900*3448Sdh155122 	cred_t		*cr;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
9030Sstevel@tonic-gate 	case M_PROTO:
9040Sstevel@tonic-gate 	case M_PCPROTO:
9050Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
906*3448Sdh155122 			ks3dbg(keystack, (
9070Sstevel@tonic-gate 			    "keysock_wput_other: Not big enough M_PROTO\n"));
9080Sstevel@tonic-gate 			freemsg(mp);
9090Sstevel@tonic-gate 			return;
9100Sstevel@tonic-gate 		}
911*3448Sdh155122 		cr = zone_get_kcred(netstackid_to_zoneid(
912*3448Sdh155122 			keystack->keystack_netstack->netstack_stackid));
913*3448Sdh155122 		ASSERT(cr != NULL);
914*3448Sdh155122 
9150Sstevel@tonic-gate 		switch (((union T_primitives *)mp->b_rptr)->type) {
9160Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
9170Sstevel@tonic-gate 			keysock_capability_req(q, mp);
918*3448Sdh155122 			break;
9190Sstevel@tonic-gate 		case T_INFO_REQ:
9200Sstevel@tonic-gate 			keysock_info_req(q, mp);
921*3448Sdh155122 			break;
9220Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
923*3448Sdh155122 			(void) svr4_optcom_req(q, mp, DB_CREDDEF(mp, cr),
9240Sstevel@tonic-gate 			    &keysock_opt_obj);
925*3448Sdh155122 			break;
9260Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
927*3448Sdh155122 			(void) tpi_optcom_req(q, mp, DB_CREDDEF(mp, cr),
9280Sstevel@tonic-gate 			    &keysock_opt_obj);
929*3448Sdh155122 			break;
9300Sstevel@tonic-gate 		case T_DATA_REQ:
9310Sstevel@tonic-gate 		case T_EXDATA_REQ:
9320Sstevel@tonic-gate 		case T_ORDREL_REQ:
9330Sstevel@tonic-gate 			/* Illegal for keysock. */
9340Sstevel@tonic-gate 			freemsg(mp);
9350Sstevel@tonic-gate 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
936*3448Sdh155122 			break;
9370Sstevel@tonic-gate 		default:
9380Sstevel@tonic-gate 			/* Not supported by keysock. */
9390Sstevel@tonic-gate 			keysock_err_ack(q, mp, TNOTSUPPORT, 0);
940*3448Sdh155122 			break;
9410Sstevel@tonic-gate 		}
942*3448Sdh155122 		crfree(cr);
943*3448Sdh155122 		return;
9440Sstevel@tonic-gate 	case M_IOCTL:
9450Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
9460Sstevel@tonic-gate 		error = EINVAL;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
9490Sstevel@tonic-gate 		case ND_SET:
9500Sstevel@tonic-gate 		case ND_GET:
951*3448Sdh155122 			if (nd_getset(q, keystack->keystack_g_nd, mp)) {
9520Sstevel@tonic-gate 				qreply(q, mp);
9530Sstevel@tonic-gate 				return;
9540Sstevel@tonic-gate 			} else
9550Sstevel@tonic-gate 				error = ENOENT;
9560Sstevel@tonic-gate 			/* FALLTHRU */
9570Sstevel@tonic-gate 		default:
9580Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
9590Sstevel@tonic-gate 			return;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 	case M_FLUSH:
9620Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
9630Sstevel@tonic-gate 			flushq(q, FLUSHALL);
9640Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
9670Sstevel@tonic-gate 			qreply(q, mp);
9680Sstevel@tonic-gate 			return;
9690Sstevel@tonic-gate 		}
9700Sstevel@tonic-gate 		/* Else FALLTHRU */
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	/* If fell through, just black-hole the message. */
9740Sstevel@tonic-gate 	freemsg(mp);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate  * Transmit a PF_KEY error message to the instance either pointed to
9790Sstevel@tonic-gate  * by ks, the instance with serial number serial, or more, depending.
9800Sstevel@tonic-gate  *
9810Sstevel@tonic-gate  * The faulty message (or a reasonable facsimile thereof) is in mp.
9820Sstevel@tonic-gate  * This function will free mp or recycle it for delivery, thereby causing
9830Sstevel@tonic-gate  * the stream head to free it.
9840Sstevel@tonic-gate  */
9850Sstevel@tonic-gate static void
9860Sstevel@tonic-gate keysock_error(keysock_t *ks, mblk_t *mp, int error, int diagnostic)
9870Sstevel@tonic-gate {
9880Sstevel@tonic-gate 	sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr;
989*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_DATA);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	if (samsg->sadb_msg_type < SADB_GETSPI ||
9940Sstevel@tonic-gate 	    samsg->sadb_msg_type > SADB_MAX)
9950Sstevel@tonic-gate 		samsg->sadb_msg_type = SADB_RESERVED;
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/*
9980Sstevel@tonic-gate 	 * Strip out extension headers.
9990Sstevel@tonic-gate 	 */
10000Sstevel@tonic-gate 	ASSERT(mp->b_rptr + sizeof (*samsg) <= mp->b_datap->db_lim);
10010Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (*samsg);
10020Sstevel@tonic-gate 	samsg->sadb_msg_len = SADB_8TO64(sizeof (sadb_msg_t));
10030Sstevel@tonic-gate 	samsg->sadb_msg_errno = (uint8_t)error;
10040Sstevel@tonic-gate 	samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic;
10050Sstevel@tonic-gate 
1006*3448Sdh155122 	keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE, keystack);
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate /*
10100Sstevel@tonic-gate  * Pass down a message to a consumer.  Wrap it in KEYSOCK_IN, and copy
10110Sstevel@tonic-gate  * in the extv if passed in.
10120Sstevel@tonic-gate  */
10130Sstevel@tonic-gate static void
10140Sstevel@tonic-gate keysock_passdown(keysock_t *ks, mblk_t *mp, uint8_t satype, sadb_ext_t *extv[],
10150Sstevel@tonic-gate     boolean_t flushmsg)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	keysock_consumer_t *kc;
10180Sstevel@tonic-gate 	mblk_t *wrapper;
10190Sstevel@tonic-gate 	keysock_in_t *ksi;
10200Sstevel@tonic-gate 	int i;
1021*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	wrapper = allocb(sizeof (ipsec_info_t), BPRI_HI);
10240Sstevel@tonic-gate 	if (wrapper == NULL) {
1025*3448Sdh155122 		ks3dbg(keystack, ("keysock_passdown: allocb failed.\n"));
10260Sstevel@tonic-gate 		if (extv[SADB_EXT_KEY_ENCRYPT] != NULL)
10270Sstevel@tonic-gate 			bzero(extv[SADB_EXT_KEY_ENCRYPT],
10280Sstevel@tonic-gate 			    SADB_64TO8(
10290Sstevel@tonic-gate 				extv[SADB_EXT_KEY_ENCRYPT]->sadb_ext_len));
10300Sstevel@tonic-gate 		if (extv[SADB_EXT_KEY_AUTH] != NULL)
10310Sstevel@tonic-gate 			bzero(extv[SADB_EXT_KEY_AUTH],
10320Sstevel@tonic-gate 			    SADB_64TO8(
10330Sstevel@tonic-gate 				extv[SADB_EXT_KEY_AUTH]->sadb_ext_len));
10340Sstevel@tonic-gate 		if (flushmsg) {
10350Sstevel@tonic-gate 			ks0dbg((
10360Sstevel@tonic-gate 			    "keysock: Downwards flush/dump message failed!\n"));
10370Sstevel@tonic-gate 			/* If this is true, I hold the perimeter. */
1038*3448Sdh155122 			keystack->keystack_flushdump--;
10390Sstevel@tonic-gate 		}
10400Sstevel@tonic-gate 		freemsg(mp);
10410Sstevel@tonic-gate 		return;
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	wrapper->b_datap->db_type = M_CTL;
10450Sstevel@tonic-gate 	ksi = (keysock_in_t *)wrapper->b_rptr;
10460Sstevel@tonic-gate 	ksi->ks_in_type = KEYSOCK_IN;
10470Sstevel@tonic-gate 	ksi->ks_in_len = sizeof (keysock_in_t);
10480Sstevel@tonic-gate 	if (extv[SADB_EXT_ADDRESS_SRC] != NULL)
10490Sstevel@tonic-gate 		ksi->ks_in_srctype = KS_IN_ADDR_UNKNOWN;
10500Sstevel@tonic-gate 	else ksi->ks_in_srctype = KS_IN_ADDR_NOTTHERE;
10510Sstevel@tonic-gate 	if (extv[SADB_EXT_ADDRESS_DST] != NULL)
10520Sstevel@tonic-gate 		ksi->ks_in_dsttype = KS_IN_ADDR_UNKNOWN;
10530Sstevel@tonic-gate 	else ksi->ks_in_dsttype = KS_IN_ADDR_NOTTHERE;
10540Sstevel@tonic-gate 	for (i = 0; i <= SADB_EXT_MAX; i++)
10550Sstevel@tonic-gate 		ksi->ks_in_extv[i] = extv[i];
10560Sstevel@tonic-gate 	ksi->ks_in_serial = ks->keysock_serial;
10570Sstevel@tonic-gate 	wrapper->b_wptr += sizeof (ipsec_info_t);
10580Sstevel@tonic-gate 	wrapper->b_cont = mp;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	/*
10610Sstevel@tonic-gate 	 * Find the appropriate consumer where the message is passed down.
10620Sstevel@tonic-gate 	 */
1063*3448Sdh155122 	kc = keystack->keystack_consumers[satype];
10640Sstevel@tonic-gate 	if (kc == NULL) {
10650Sstevel@tonic-gate 		freeb(wrapper);
10660Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE);
10670Sstevel@tonic-gate 		if (flushmsg) {
10680Sstevel@tonic-gate 			ks0dbg((
10690Sstevel@tonic-gate 			    "keysock: Downwards flush/dump message failed!\n"));
10700Sstevel@tonic-gate 			/* If this is true, I hold the perimeter. */
1071*3448Sdh155122 			keystack->keystack_flushdump--;
10720Sstevel@tonic-gate 		}
10730Sstevel@tonic-gate 		return;
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	/*
10770Sstevel@tonic-gate 	 * NOTE: There used to be code in here to spin while a flush or
10780Sstevel@tonic-gate 	 *	 dump finished.  Keysock now assumes that consumers have enough
10790Sstevel@tonic-gate 	 *	 MT-savviness to deal with that.
10800Sstevel@tonic-gate 	 */
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/*
10830Sstevel@tonic-gate 	 * Current consumers (AH and ESP) are guaranteed to return a
10840Sstevel@tonic-gate 	 * FLUSH or DUMP message back, so when we reach here, we don't
10850Sstevel@tonic-gate 	 * have to worry about keysock_flushdumps.
10860Sstevel@tonic-gate 	 */
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	putnext(kc->kc_wq, wrapper);
10890Sstevel@tonic-gate }
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate /*
10920Sstevel@tonic-gate  * High-level reality checking of extensions.
10930Sstevel@tonic-gate  */
10940Sstevel@tonic-gate static boolean_t
1095*3448Sdh155122 ext_check(sadb_ext_t *ext, keysock_stack_t *keystack)
10960Sstevel@tonic-gate {
10970Sstevel@tonic-gate 	int i;
10980Sstevel@tonic-gate 	uint64_t *lp;
10990Sstevel@tonic-gate 	sadb_ident_t *id;
11000Sstevel@tonic-gate 	char *idstr;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	switch (ext->sadb_ext_type) {
11030Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_SRC:
11040Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_DST:
11053055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
11063055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
11070Sstevel@tonic-gate 		/* Check for at least enough addtl length for a sockaddr. */
11080Sstevel@tonic-gate 		if (ext->sadb_ext_len <= SADB_8TO64(sizeof (sadb_address_t)))
11090Sstevel@tonic-gate 			return (B_FALSE);
11100Sstevel@tonic-gate 		break;
11110Sstevel@tonic-gate 	case SADB_EXT_LIFETIME_HARD:
11120Sstevel@tonic-gate 	case SADB_EXT_LIFETIME_SOFT:
11130Sstevel@tonic-gate 	case SADB_EXT_LIFETIME_CURRENT:
11140Sstevel@tonic-gate 		if (ext->sadb_ext_len != SADB_8TO64(sizeof (sadb_lifetime_t)))
11150Sstevel@tonic-gate 			return (B_FALSE);
11160Sstevel@tonic-gate 		break;
11170Sstevel@tonic-gate 	case SADB_EXT_SPIRANGE:
11180Sstevel@tonic-gate 		/* See if the SPI range is legit. */
11190Sstevel@tonic-gate 		if (htonl(((sadb_spirange_t *)ext)->sadb_spirange_min) >
11200Sstevel@tonic-gate 		    htonl(((sadb_spirange_t *)ext)->sadb_spirange_max))
11210Sstevel@tonic-gate 			return (B_FALSE);
11220Sstevel@tonic-gate 		break;
11230Sstevel@tonic-gate 	case SADB_EXT_KEY_AUTH:
11240Sstevel@tonic-gate 	case SADB_EXT_KEY_ENCRYPT:
11250Sstevel@tonic-gate 		/* Key length check. */
11260Sstevel@tonic-gate 		if (((sadb_key_t *)ext)->sadb_key_bits == 0)
11270Sstevel@tonic-gate 			return (B_FALSE);
11280Sstevel@tonic-gate 		/*
11290Sstevel@tonic-gate 		 * Check to see if the key length (in bits) is less than the
11300Sstevel@tonic-gate 		 * extension length (in 8-bits words).
11310Sstevel@tonic-gate 		 */
11320Sstevel@tonic-gate 		if ((roundup(SADB_1TO8(((sadb_key_t *)ext)->sadb_key_bits), 8) +
11330Sstevel@tonic-gate 		    sizeof (sadb_key_t)) != SADB_64TO8(ext->sadb_ext_len)) {
1134*3448Sdh155122 			ks1dbg(keystack, (
11350Sstevel@tonic-gate 			    "ext_check:  Key bits/length inconsistent.\n"));
1136*3448Sdh155122 			ks1dbg(keystack, ("%d bits, len is %d bytes.\n",
11370Sstevel@tonic-gate 			    ((sadb_key_t *)ext)->sadb_key_bits,
11380Sstevel@tonic-gate 			    SADB_64TO8(ext->sadb_ext_len)));
11390Sstevel@tonic-gate 			return (B_FALSE);
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 		/* All-zeroes key check. */
11430Sstevel@tonic-gate 		lp = (uint64_t *)(((char *)ext) + sizeof (sadb_key_t));
11440Sstevel@tonic-gate 		for (i = 0;
11450Sstevel@tonic-gate 		    i < (ext->sadb_ext_len - SADB_8TO64(sizeof (sadb_key_t)));
11460Sstevel@tonic-gate 		    i++)
11470Sstevel@tonic-gate 			if (lp[i] != 0)
11480Sstevel@tonic-gate 				break;	/* Out of for loop. */
11490Sstevel@tonic-gate 		/* If finished the loop naturally, it's an all zero key. */
11500Sstevel@tonic-gate 		if (lp[i] == 0)
11510Sstevel@tonic-gate 			return (B_FALSE);
11520Sstevel@tonic-gate 		break;
11530Sstevel@tonic-gate 	case SADB_EXT_IDENTITY_SRC:
11540Sstevel@tonic-gate 	case SADB_EXT_IDENTITY_DST:
11550Sstevel@tonic-gate 		/*
11560Sstevel@tonic-gate 		 * Make sure the strings in these identities are
11570Sstevel@tonic-gate 		 * null-terminated.  RFC 2367 underspecified how to handle
11580Sstevel@tonic-gate 		 * such a case.  I "proactively" null-terminate the string
11590Sstevel@tonic-gate 		 * at the last byte if it's not terminated sooner.
11600Sstevel@tonic-gate 		 */
11610Sstevel@tonic-gate 		id = (sadb_ident_t *)ext;
11620Sstevel@tonic-gate 		i = SADB_64TO8(id->sadb_ident_len);
11630Sstevel@tonic-gate 		i -= sizeof (sadb_ident_t);
11640Sstevel@tonic-gate 		idstr = (char *)(id + 1);
11650Sstevel@tonic-gate 		while (*idstr != '\0' && i > 0) {
11660Sstevel@tonic-gate 			i--;
11670Sstevel@tonic-gate 			idstr++;
11680Sstevel@tonic-gate 		}
11690Sstevel@tonic-gate 		if (i == 0) {
11700Sstevel@tonic-gate 			/*
11710Sstevel@tonic-gate 			 * I.e., if the bozo user didn't NULL-terminate the
11720Sstevel@tonic-gate 			 * string...
11730Sstevel@tonic-gate 			 */
11740Sstevel@tonic-gate 			idstr--;
11750Sstevel@tonic-gate 			*idstr = '\0';
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate 		break;
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 	return (B_TRUE);	/* For now... */
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate /* Return values for keysock_get_ext(). */
11830Sstevel@tonic-gate #define	KGE_OK	0
11840Sstevel@tonic-gate #define	KGE_DUP	1
11850Sstevel@tonic-gate #define	KGE_UNK	2
11860Sstevel@tonic-gate #define	KGE_LEN	3
11870Sstevel@tonic-gate #define	KGE_CHK	4
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate /*
11900Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
11910Sstevel@tonic-gate  * Return values include:
11920Sstevel@tonic-gate  *
11930Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
11940Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
11950Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
11960Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
11970Sstevel@tonic-gate  *		extv[0].
11980Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
11990Sstevel@tonic-gate  *		unknown header.
12000Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
12010Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
12020Sstevel@tonic-gate  *
12030Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
12040Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
12050Sstevel@tonic-gate  */
12060Sstevel@tonic-gate static int
1207*3448Sdh155122 keysock_get_ext(sadb_ext_t *extv[], sadb_msg_t *basehdr, uint_t msgsize,
1208*3448Sdh155122     keysock_stack_t *keystack)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	bzero(extv, sizeof (sadb_ext_t *) * (SADB_EXT_MAX + 1));
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	extv[0] = (sadb_ext_t *)(basehdr + 1);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	while (extv[0] < (sadb_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
12170Sstevel@tonic-gate 		/* Check for unknown headers. */
12180Sstevel@tonic-gate 		if (extv[0]->sadb_ext_type == 0 ||
12190Sstevel@tonic-gate 		    extv[0]->sadb_ext_type > SADB_EXT_MAX)
12200Sstevel@tonic-gate 			return (KGE_UNK);
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 		/*
12230Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
12240Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
12250Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
12260Sstevel@tonic-gate 		 */
12270Sstevel@tonic-gate 		if (extv[0]->sadb_ext_len == 0 ||
12280Sstevel@tonic-gate 		    (void *)((uint64_t *)extv[0] + extv[0]->sadb_ext_len) >
12290Sstevel@tonic-gate 		    (void *)((uint8_t *)basehdr + msgsize))
12300Sstevel@tonic-gate 			return (KGE_LEN);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		/* Check for redundant headers. */
12330Sstevel@tonic-gate 		if (extv[extv[0]->sadb_ext_type] != NULL)
12340Sstevel@tonic-gate 			return (KGE_DUP);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 		/*
12370Sstevel@tonic-gate 		 * Reality check the extension if possible at the keysock
12380Sstevel@tonic-gate 		 * level.
12390Sstevel@tonic-gate 		 */
1240*3448Sdh155122 		if (!ext_check(extv[0], keystack))
12410Sstevel@tonic-gate 			return (KGE_CHK);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
12440Sstevel@tonic-gate 		extv[extv[0]->sadb_ext_type] = extv[0];
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
12470Sstevel@tonic-gate 		extv[0] = (sadb_ext_t *)
12480Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->sadb_ext_len);
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	/* Everything's cool. */
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	/*
12540Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
12550Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
12560Sstevel@tonic-gate 	 */
12570Sstevel@tonic-gate 	if (extv[0] == (sadb_ext_t *)(basehdr + 1))
12580Sstevel@tonic-gate 		extv[0] = NULL;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	return (KGE_OK);
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate  * qwriter() callback to handle flushes and dumps.  This routine will hold
12650Sstevel@tonic-gate  * the inner perimeter.
12660Sstevel@tonic-gate  */
12670Sstevel@tonic-gate void
12680Sstevel@tonic-gate keysock_do_flushdump(queue_t *q, mblk_t *mp)
12690Sstevel@tonic-gate {
12700Sstevel@tonic-gate 	int i, start, finish;
12710Sstevel@tonic-gate 	mblk_t *mp1 = NULL;
12720Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
12730Sstevel@tonic-gate 	sadb_ext_t *extv[SADB_EXT_MAX + 1];
12740Sstevel@tonic-gate 	sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr;
1275*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	/*
12780Sstevel@tonic-gate 	 * I am guaranteed this will work.  I did the work in keysock_parse()
12790Sstevel@tonic-gate 	 * already.
12800Sstevel@tonic-gate 	 */
1281*3448Sdh155122 	(void) keysock_get_ext(extv, samsg, SADB_64TO8(samsg->sadb_msg_len),
1282*3448Sdh155122 	    keystack);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	/*
12850Sstevel@tonic-gate 	 * I hold the perimeter, therefore I don't need to use atomic ops.
12860Sstevel@tonic-gate 	 */
1287*3448Sdh155122 	if (keystack->keystack_flushdump != 0) {
12880Sstevel@tonic-gate 		/* XXX Should I instead use EBUSY? */
12890Sstevel@tonic-gate 		/* XXX Or is there a way to queue these up? */
12900Sstevel@tonic-gate 		keysock_error(ks, mp, ENOMEM, SADB_X_DIAGNOSTIC_NONE);
12910Sstevel@tonic-gate 		return;
12920Sstevel@tonic-gate 	}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
12950Sstevel@tonic-gate 		start = 0;
12960Sstevel@tonic-gate 		finish = KEYSOCK_MAX_CONSUMERS - 1;
12970Sstevel@tonic-gate 	} else {
12980Sstevel@tonic-gate 		start = samsg->sadb_msg_satype;
12990Sstevel@tonic-gate 		finish = samsg->sadb_msg_satype;
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/*
13030Sstevel@tonic-gate 	 * Fill up keysock_flushdump with the number of outstanding dumps
13040Sstevel@tonic-gate 	 * and/or flushes.
13050Sstevel@tonic-gate 	 */
13060Sstevel@tonic-gate 
1307*3448Sdh155122 	keystack->keystack_flushdump_errno = 0;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	/*
13100Sstevel@tonic-gate 	 * Okay, I hold the perimeter.  Eventually keysock_flushdump will
13110Sstevel@tonic-gate 	 * contain the number of consumers with outstanding flush operations.
13120Sstevel@tonic-gate 	 *
13130Sstevel@tonic-gate 	 * SO, here's the plan:
13140Sstevel@tonic-gate 	 *	* For each relevant consumer (Might be one, might be all)
13150Sstevel@tonic-gate 	 *		* Twiddle on the FLUSHING flag.
13160Sstevel@tonic-gate 	 *		* Pass down the FLUSH/DUMP message.
13170Sstevel@tonic-gate 	 *
13180Sstevel@tonic-gate 	 * When I see upbound FLUSH/DUMP messages, I will decrement the
13190Sstevel@tonic-gate 	 * keysock_flushdump.  When I decrement it to 0, I will pass the
13200Sstevel@tonic-gate 	 * FLUSH/DUMP message back up to the PF_KEY sockets.  Because I will
13210Sstevel@tonic-gate 	 * pass down the right SA type to the consumer (either its own, or
13220Sstevel@tonic-gate 	 * that of UNSPEC), the right one will be reflected from each consumer,
13230Sstevel@tonic-gate 	 * and accordingly back to the socket.
13240Sstevel@tonic-gate 	 */
13250Sstevel@tonic-gate 
1326*3448Sdh155122 	mutex_enter(&keystack->keystack_consumers_lock);
13270Sstevel@tonic-gate 	for (i = start; i <= finish; i++) {
1328*3448Sdh155122 		if (keystack->keystack_consumers[i] != NULL) {
13290Sstevel@tonic-gate 			mp1 = copymsg(mp);
13300Sstevel@tonic-gate 			if (mp1 == NULL) {
13310Sstevel@tonic-gate 				ks0dbg(("SADB_FLUSH copymsg() failed.\n"));
13320Sstevel@tonic-gate 				/*
13330Sstevel@tonic-gate 				 * Error?  And what about outstanding
13340Sstevel@tonic-gate 				 * flushes?  Oh, yeah, they get sucked up and
13350Sstevel@tonic-gate 				 * the counter is decremented.  Consumers
13360Sstevel@tonic-gate 				 * (see keysock_passdown()) are guaranteed
13370Sstevel@tonic-gate 				 * to deliver back a flush request, even if
13380Sstevel@tonic-gate 				 * it's an error.
13390Sstevel@tonic-gate 				 */
13400Sstevel@tonic-gate 				keysock_error(ks, mp, ENOMEM,
13410Sstevel@tonic-gate 				    SADB_X_DIAGNOSTIC_NONE);
13420Sstevel@tonic-gate 				return;
13430Sstevel@tonic-gate 			}
13440Sstevel@tonic-gate 			/*
13450Sstevel@tonic-gate 			 * Because my entry conditions are met above, the
13460Sstevel@tonic-gate 			 * following assertion should hold true.
13470Sstevel@tonic-gate 			 */
1348*3448Sdh155122 			mutex_enter(&keystack->keystack_consumers[i]->kc_lock);
1349*3448Sdh155122 			ASSERT((keystack->keystack_consumers[i]->kc_flags &
1350*3448Sdh155122 				KC_FLUSHING) == 0);
1351*3448Sdh155122 			keystack->keystack_consumers[i]->kc_flags |=
1352*3448Sdh155122 			    KC_FLUSHING;
1353*3448Sdh155122 			mutex_exit(&(keystack->keystack_consumers[i]->kc_lock));
13540Sstevel@tonic-gate 			/* Always increment the number of flushes... */
1355*3448Sdh155122 			keystack->keystack_flushdump++;
13560Sstevel@tonic-gate 			/* Guaranteed to return a message. */
13570Sstevel@tonic-gate 			keysock_passdown(ks, mp1, i, extv, B_TRUE);
13580Sstevel@tonic-gate 		} else if (start == finish) {
13590Sstevel@tonic-gate 			/*
13600Sstevel@tonic-gate 			 * In case where start == finish, and there's no
13610Sstevel@tonic-gate 			 * consumer, should we force an error?  Yes.
13620Sstevel@tonic-gate 			 */
1363*3448Sdh155122 			mutex_exit(&keystack->keystack_consumers_lock);
13640Sstevel@tonic-gate 			keysock_error(ks, mp, EINVAL,
13650Sstevel@tonic-gate 			    SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE);
13660Sstevel@tonic-gate 			return;
13670Sstevel@tonic-gate 		}
13680Sstevel@tonic-gate 	}
1369*3448Sdh155122 	mutex_exit(&keystack->keystack_consumers_lock);
13700Sstevel@tonic-gate 
1371*3448Sdh155122 	if (keystack->keystack_flushdump == 0) {
13720Sstevel@tonic-gate 		/*
13730Sstevel@tonic-gate 		 * There were no consumers at all for this message.
13740Sstevel@tonic-gate 		 * XXX For now return ESRCH.
13750Sstevel@tonic-gate 		 */
13760Sstevel@tonic-gate 		keysock_error(ks, mp, ESRCH, SADB_X_DIAGNOSTIC_NO_SADBS);
13770Sstevel@tonic-gate 	} else {
13780Sstevel@tonic-gate 		/* Otherwise, free the original message. */
13790Sstevel@tonic-gate 		freemsg(mp);
13800Sstevel@tonic-gate 	}
13810Sstevel@tonic-gate }
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate /*
13840Sstevel@tonic-gate  * Get the right diagnostic for a duplicate.  Should probably use a static
13850Sstevel@tonic-gate  * table lookup.
13860Sstevel@tonic-gate  */
13870Sstevel@tonic-gate int
13880Sstevel@tonic-gate keysock_duplicate(int ext_type)
13890Sstevel@tonic-gate {
13900Sstevel@tonic-gate 	int rc = 0;
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	switch (ext_type) {
13930Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_SRC:
13940Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_SRC;
13950Sstevel@tonic-gate 		break;
13960Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_DST:
13970Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_DST;
13980Sstevel@tonic-gate 		break;
13993055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
14003055Sdanmcd 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC;
14013055Sdanmcd 		break;
14023055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
14033055Sdanmcd 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST;
14043055Sdanmcd 		break;
14050Sstevel@tonic-gate 	case SADB_EXT_SA:
14060Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_SA;
14070Sstevel@tonic-gate 		break;
14080Sstevel@tonic-gate 	case SADB_EXT_SPIRANGE:
14090Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_RANGE;
14100Sstevel@tonic-gate 		break;
14110Sstevel@tonic-gate 	case SADB_EXT_KEY_AUTH:
14120Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_AKEY;
14130Sstevel@tonic-gate 		break;
14140Sstevel@tonic-gate 	case SADB_EXT_KEY_ENCRYPT:
14150Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_EKEY;
14160Sstevel@tonic-gate 		break;
14170Sstevel@tonic-gate 	}
14180Sstevel@tonic-gate 	return (rc);
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate /*
14220Sstevel@tonic-gate  * Get the right diagnostic for a reality check failure.  Should probably use
14230Sstevel@tonic-gate  * a static table lookup.
14240Sstevel@tonic-gate  */
14250Sstevel@tonic-gate int
14260Sstevel@tonic-gate keysock_malformed(int ext_type)
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate 	int rc = 0;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	switch (ext_type) {
14310Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_SRC:
14320Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_SRC;
14330Sstevel@tonic-gate 		break;
14340Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_DST:
14350Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_DST;
14360Sstevel@tonic-gate 		break;
14373055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
14383055Sdanmcd 		rc = SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC;
14393055Sdanmcd 		break;
14403055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
14413055Sdanmcd 		rc = SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST;
14423055Sdanmcd 		break;
14430Sstevel@tonic-gate 	case SADB_EXT_SA:
14440Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_SA;
14450Sstevel@tonic-gate 		break;
14460Sstevel@tonic-gate 	case SADB_EXT_SPIRANGE:
14470Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_RANGE;
14480Sstevel@tonic-gate 		break;
14490Sstevel@tonic-gate 	case SADB_EXT_KEY_AUTH:
14500Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_AKEY;
14510Sstevel@tonic-gate 		break;
14520Sstevel@tonic-gate 	case SADB_EXT_KEY_ENCRYPT:
14530Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_EKEY;
14540Sstevel@tonic-gate 		break;
14550Sstevel@tonic-gate 	}
14560Sstevel@tonic-gate 	return (rc);
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate /*
14600Sstevel@tonic-gate  * Keysock massaging of an inverse ACQUIRE.  Consult policy,
14610Sstevel@tonic-gate  * and construct an appropriate response.
14620Sstevel@tonic-gate  */
14630Sstevel@tonic-gate static void
14640Sstevel@tonic-gate keysock_inverse_acquire(mblk_t *mp, sadb_msg_t *samsg, sadb_ext_t *extv[],
14650Sstevel@tonic-gate     keysock_t *ks)
14660Sstevel@tonic-gate {
14670Sstevel@tonic-gate 	mblk_t *reply_mp;
1468*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	/*
14710Sstevel@tonic-gate 	 * Reality check things...
14720Sstevel@tonic-gate 	 */
14730Sstevel@tonic-gate 	if (extv[SADB_EXT_ADDRESS_SRC] == NULL) {
14740Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_MISSING_SRC);
14750Sstevel@tonic-gate 		return;
14760Sstevel@tonic-gate 	}
14770Sstevel@tonic-gate 	if (extv[SADB_EXT_ADDRESS_DST] == NULL) {
14780Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_MISSING_DST);
14793055Sdanmcd 		return;
14803055Sdanmcd 	}
14813055Sdanmcd 
14823055Sdanmcd 	if (extv[SADB_X_EXT_ADDRESS_INNER_SRC] != NULL &&
14833055Sdanmcd 	    extv[SADB_X_EXT_ADDRESS_INNER_DST] == NULL) {
14843055Sdanmcd 		keysock_error(ks, mp, EINVAL,
14853055Sdanmcd 		    SADB_X_DIAGNOSTIC_MISSING_INNER_DST);
14863055Sdanmcd 		return;
14873055Sdanmcd 	}
14883055Sdanmcd 
14893055Sdanmcd 	if (extv[SADB_X_EXT_ADDRESS_INNER_SRC] == NULL &&
14903055Sdanmcd 	    extv[SADB_X_EXT_ADDRESS_INNER_DST] != NULL) {
14913055Sdanmcd 		keysock_error(ks, mp, EINVAL,
14923055Sdanmcd 		    SADB_X_DIAGNOSTIC_MISSING_INNER_SRC);
14933055Sdanmcd 		return;
14940Sstevel@tonic-gate 	}
14950Sstevel@tonic-gate 
1496*3448Sdh155122 	reply_mp = ipsec_construct_inverse_acquire(samsg, extv,
1497*3448Sdh155122 	    keystack->keystack_netstack);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	if (reply_mp != NULL) {
15000Sstevel@tonic-gate 		freemsg(mp);
15010Sstevel@tonic-gate 		keysock_passup(reply_mp, (sadb_msg_t *)reply_mp->b_rptr,
1502*3448Sdh155122 		    ks->keysock_serial, NULL, B_FALSE, keystack);
15030Sstevel@tonic-gate 	} else {
15040Sstevel@tonic-gate 		keysock_error(ks, mp, samsg->sadb_msg_errno,
15050Sstevel@tonic-gate 		    samsg->sadb_x_msg_diagnostic);
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate /*
15100Sstevel@tonic-gate  * Spew an extended REGISTER down to the relevant consumers.
15110Sstevel@tonic-gate  */
15120Sstevel@tonic-gate static void
15130Sstevel@tonic-gate keysock_extended_register(keysock_t *ks, mblk_t *mp, sadb_ext_t *extv[])
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate 	sadb_x_ereg_t *ereg = (sadb_x_ereg_t *)extv[SADB_X_EXT_EREG];
15160Sstevel@tonic-gate 	uint8_t *satypes, *fencepost;
15170Sstevel@tonic-gate 	mblk_t *downmp;
15180Sstevel@tonic-gate 	sadb_ext_t *downextv[SADB_EXT_MAX + 1];
1519*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	if (ks->keysock_registered[0] != 0 || ks->keysock_registered[1] != 0 ||
15220Sstevel@tonic-gate 	    ks->keysock_registered[2] != 0 || ks->keysock_registered[3] != 0) {
15230Sstevel@tonic-gate 		keysock_error(ks, mp, EBUSY, 0);
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	ks->keysock_flags |= KEYSOCK_EXTENDED;
15270Sstevel@tonic-gate 	if (ereg == NULL) {
15280Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
15290Sstevel@tonic-gate 	} else {
15300Sstevel@tonic-gate 		ASSERT(mp->b_rptr + msgdsize(mp) == mp->b_wptr);
15310Sstevel@tonic-gate 		fencepost = (uint8_t *)mp->b_wptr;
15320Sstevel@tonic-gate 		satypes = ereg->sadb_x_ereg_satypes;
15330Sstevel@tonic-gate 		while (*satypes != SADB_SATYPE_UNSPEC && satypes != fencepost) {
15340Sstevel@tonic-gate 			downmp = copymsg(mp);
15350Sstevel@tonic-gate 			if (downmp == NULL) {
15360Sstevel@tonic-gate 				keysock_error(ks, mp, ENOMEM, 0);
15370Sstevel@tonic-gate 				return;
15380Sstevel@tonic-gate 			}
15390Sstevel@tonic-gate 			/*
15400Sstevel@tonic-gate 			 * Since we've made it here, keysock_get_ext will work!
15410Sstevel@tonic-gate 			 */
15420Sstevel@tonic-gate 			(void) keysock_get_ext(downextv,
1543*3448Sdh155122 			    (sadb_msg_t *)downmp->b_rptr, msgdsize(downmp),
1544*3448Sdh155122 			    keystack);
15450Sstevel@tonic-gate 			keysock_passdown(ks, downmp, *satypes, downextv,
15460Sstevel@tonic-gate 			    B_FALSE);
15470Sstevel@tonic-gate 			++satypes;
15480Sstevel@tonic-gate 		}
15490Sstevel@tonic-gate 		freemsg(mp);
15500Sstevel@tonic-gate 	}
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	/*
15530Sstevel@tonic-gate 	 * Set global to indicate we prefer an extended ACQUIRE.
15540Sstevel@tonic-gate 	 */
1555*3448Sdh155122 	atomic_add_32(&keystack->keystack_num_extended, 1);
15560Sstevel@tonic-gate }
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate /*
15590Sstevel@tonic-gate  * Handle PF_KEY messages.
15600Sstevel@tonic-gate  */
15610Sstevel@tonic-gate static void
15620Sstevel@tonic-gate keysock_parse(queue_t *q, mblk_t *mp)
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate 	sadb_msg_t *samsg;
15650Sstevel@tonic-gate 	sadb_ext_t *extv[SADB_EXT_MAX + 1];
15660Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
15670Sstevel@tonic-gate 	uint_t msgsize;
15680Sstevel@tonic-gate 	uint8_t satype;
1569*3448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	/* Make sure I'm a PF_KEY socket.  (i.e. nothing's below me) */
15720Sstevel@tonic-gate 	ASSERT(WR(q)->q_next == NULL);
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_rptr;
1575*3448Sdh155122 	ks2dbg(keystack, ("Received possible PF_KEY message, type %d.\n",
15760Sstevel@tonic-gate 	    samsg->sadb_msg_type));
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	msgsize = SADB_64TO8(samsg->sadb_msg_len);
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	if (msgdsize(mp) != msgsize) {
15810Sstevel@tonic-gate 		/*
15820Sstevel@tonic-gate 		 * Message len incorrect w.r.t. actual size.  Send an error
15830Sstevel@tonic-gate 		 * (EMSGSIZE).	It may be necessary to massage things a
15840Sstevel@tonic-gate 		 * bit.	 For example, if the sadb_msg_type is hosed,
15850Sstevel@tonic-gate 		 * I need to set it to SADB_RESERVED to get delivery to
15860Sstevel@tonic-gate 		 * do the right thing.	Then again, maybe just letting
15870Sstevel@tonic-gate 		 * the error delivery do the right thing.
15880Sstevel@tonic-gate 		 */
1589*3448Sdh155122 		ks2dbg(keystack,
1590*3448Sdh155122 		    ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
15910Sstevel@tonic-gate 		    msgdsize(mp), msgsize));
15920Sstevel@tonic-gate 		keysock_error(ks, mp, EMSGSIZE, SADB_X_DIAGNOSTIC_NONE);
15930Sstevel@tonic-gate 		return;
15940Sstevel@tonic-gate 	}
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
15970Sstevel@tonic-gate 		/* Get all message into one mblk. */
15980Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
15990Sstevel@tonic-gate 			/*
16000Sstevel@tonic-gate 			 * Something screwy happened.
16010Sstevel@tonic-gate 			 */
1602*3448Sdh155122 			ks3dbg(keystack,
1603*3448Sdh155122 			    ("keysock_parse: pullupmsg() failed.\n"));
16040Sstevel@tonic-gate 			return;
16050Sstevel@tonic-gate 		} else {
16060Sstevel@tonic-gate 			samsg = (sadb_msg_t *)mp->b_rptr;
16070Sstevel@tonic-gate 		}
16080Sstevel@tonic-gate 	}
16090Sstevel@tonic-gate 
1610*3448Sdh155122 	switch (keysock_get_ext(extv, samsg, msgsize, keystack)) {
16110Sstevel@tonic-gate 	case KGE_DUP:
16120Sstevel@tonic-gate 		/* Handle duplicate extension. */
1613*3448Sdh155122 		ks1dbg(keystack, ("Got duplicate extension of type %d.\n",
16140Sstevel@tonic-gate 		    extv[0]->sadb_ext_type));
16150Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL,
16160Sstevel@tonic-gate 		    keysock_duplicate(extv[0]->sadb_ext_type));
16170Sstevel@tonic-gate 		return;
16180Sstevel@tonic-gate 	case KGE_UNK:
16190Sstevel@tonic-gate 		/* Handle unknown extension. */
1620*3448Sdh155122 		ks1dbg(keystack, ("Got unknown extension of type %d.\n",
16210Sstevel@tonic-gate 		    extv[0]->sadb_ext_type));
16220Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_EXT);
16230Sstevel@tonic-gate 		return;
16240Sstevel@tonic-gate 	case KGE_LEN:
16250Sstevel@tonic-gate 		/* Length error. */
1626*3448Sdh155122 		ks1dbg(keystack,
1627*3448Sdh155122 		    ("Length %d on extension type %d overrun or 0.\n",
16280Sstevel@tonic-gate 		    extv[0]->sadb_ext_len, extv[0]->sadb_ext_type));
16290Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_BAD_EXTLEN);
16300Sstevel@tonic-gate 		return;
16310Sstevel@tonic-gate 	case KGE_CHK:
16320Sstevel@tonic-gate 		/* Reality check failed. */
1633*3448Sdh155122 		ks1dbg(keystack,
1634*3448Sdh155122 		    ("Reality check failed on extension type %d.\n",
16350Sstevel@tonic-gate 		    extv[0]->sadb_ext_type));
16360Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL,
16370Sstevel@tonic-gate 		    keysock_malformed(extv[0]->sadb_ext_type));
16380Sstevel@tonic-gate 		return;
16390Sstevel@tonic-gate 	default:
16400Sstevel@tonic-gate 		/* Default case is no errors. */
16410Sstevel@tonic-gate 		break;
16420Sstevel@tonic-gate 	}
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
16450Sstevel@tonic-gate 	case SADB_REGISTER:
16460Sstevel@tonic-gate 		/*
16470Sstevel@tonic-gate 		 * There's a semantic weirdness in that a message OTHER than
16480Sstevel@tonic-gate 		 * the return REGISTER message may be passed up if I set the
16490Sstevel@tonic-gate 		 * registered bit BEFORE I pass it down.
16500Sstevel@tonic-gate 		 *
16510Sstevel@tonic-gate 		 * SOOOO, I'll not twiddle any registered bits until I see
16520Sstevel@tonic-gate 		 * the upbound REGISTER (with a serial number in it).
16530Sstevel@tonic-gate 		 */
16540Sstevel@tonic-gate 		if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
16550Sstevel@tonic-gate 			/* Handle extended register here. */
16560Sstevel@tonic-gate 			keysock_extended_register(ks, mp, extv);
16570Sstevel@tonic-gate 			return;
16580Sstevel@tonic-gate 		} else if (ks->keysock_flags & KEYSOCK_EXTENDED) {
16590Sstevel@tonic-gate 			keysock_error(ks, mp, EBUSY, 0);
16600Sstevel@tonic-gate 			return;
16610Sstevel@tonic-gate 		}
16620Sstevel@tonic-gate 		/* FALLTHRU */
16630Sstevel@tonic-gate 	case SADB_GETSPI:
16640Sstevel@tonic-gate 	case SADB_ADD:
16650Sstevel@tonic-gate 	case SADB_UPDATE:
16660Sstevel@tonic-gate 	case SADB_DELETE:
16670Sstevel@tonic-gate 	case SADB_GET:
16680Sstevel@tonic-gate 		/*
16690Sstevel@tonic-gate 		 * Pass down to appropriate consumer.
16700Sstevel@tonic-gate 		 */
16710Sstevel@tonic-gate 		if (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC)
16720Sstevel@tonic-gate 			keysock_passdown(ks, mp, samsg->sadb_msg_satype, extv,
16730Sstevel@tonic-gate 			    B_FALSE);
16740Sstevel@tonic-gate 		else keysock_error(ks, mp, EINVAL,
16750Sstevel@tonic-gate 		    SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
16760Sstevel@tonic-gate 		return;
16770Sstevel@tonic-gate 	case SADB_ACQUIRE:
16780Sstevel@tonic-gate 		/*
16790Sstevel@tonic-gate 		 * If I _receive_ an acquire, this means I should spread it
16800Sstevel@tonic-gate 		 * out to registered sockets.  Unless there's an errno...
16810Sstevel@tonic-gate 		 *
16820Sstevel@tonic-gate 		 * Need ADDRESS, may have ID, SENS, and PROP, unless errno,
16830Sstevel@tonic-gate 		 * in which case there should be NO extensions.
16840Sstevel@tonic-gate 		 *
16850Sstevel@tonic-gate 		 * Return to registered.
16860Sstevel@tonic-gate 		 */
16870Sstevel@tonic-gate 		if (samsg->sadb_msg_errno != 0) {
16880Sstevel@tonic-gate 			satype = samsg->sadb_msg_satype;
16890Sstevel@tonic-gate 			if (satype == SADB_SATYPE_UNSPEC) {
16900Sstevel@tonic-gate 				if (!(ks->keysock_flags & KEYSOCK_EXTENDED)) {
16910Sstevel@tonic-gate 					keysock_error(ks, mp, EINVAL,
16920Sstevel@tonic-gate 					    SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
16930Sstevel@tonic-gate 					return;
16940Sstevel@tonic-gate 				}
16950Sstevel@tonic-gate 				/*
16960Sstevel@tonic-gate 				 * Reassign satype based on the first
16970Sstevel@tonic-gate 				 * flags that KEYSOCK_SETREG says.
16980Sstevel@tonic-gate 				 */
16990Sstevel@tonic-gate 				while (satype <= SADB_SATYPE_MAX) {
17000Sstevel@tonic-gate 					if (KEYSOCK_ISREG(ks, satype))
17010Sstevel@tonic-gate 						break;
17020Sstevel@tonic-gate 					satype++;
17030Sstevel@tonic-gate 				}
17040Sstevel@tonic-gate 				if (satype > SADB_SATYPE_MAX) {
17050Sstevel@tonic-gate 					keysock_error(ks, mp, EBUSY, 0);
17060Sstevel@tonic-gate 					return;
17070Sstevel@tonic-gate 				}
17080Sstevel@tonic-gate 			}
17090Sstevel@tonic-gate 			keysock_passdown(ks, mp, satype, extv, B_FALSE);
17100Sstevel@tonic-gate 		} else {
1711*3448Sdh155122 			if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
17120Sstevel@tonic-gate 				keysock_error(ks, mp, EINVAL,
17130Sstevel@tonic-gate 				    SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
1714*3448Sdh155122 			} else {
1715*3448Sdh155122 				keysock_passup(mp, samsg, 0, NULL, B_FALSE,
1716*3448Sdh155122 				    keystack);
1717*3448Sdh155122 			}
17180Sstevel@tonic-gate 		}
17190Sstevel@tonic-gate 		return;
17200Sstevel@tonic-gate 	case SADB_EXPIRE:
17210Sstevel@tonic-gate 		/*
17220Sstevel@tonic-gate 		 * If someone sends this in, then send out to all senders.
17230Sstevel@tonic-gate 		 * (Save maybe ESP or AH, I have to be careful here.)
17240Sstevel@tonic-gate 		 *
17250Sstevel@tonic-gate 		 * Need ADDRESS, may have ID and SENS.
17260Sstevel@tonic-gate 		 *
17270Sstevel@tonic-gate 		 * XXX for now this is unsupported.
17280Sstevel@tonic-gate 		 */
17290Sstevel@tonic-gate 		break;
17300Sstevel@tonic-gate 	case SADB_FLUSH:
17310Sstevel@tonic-gate 	case SADB_DUMP:	 /* not used by normal applications */
17320Sstevel@tonic-gate 		/*
17330Sstevel@tonic-gate 		 * Nuke all SAs, or dump out the whole SA table to sender only.
17340Sstevel@tonic-gate 		 *
17350Sstevel@tonic-gate 		 * No extensions at all.  Return to all listeners.
17360Sstevel@tonic-gate 		 *
17370Sstevel@tonic-gate 		 * Question:	Should I hold a lock here to prevent
17380Sstevel@tonic-gate 		 *		additions/deletions while flushing?
17390Sstevel@tonic-gate 		 * Answer:	No.  (See keysock_passdown() for details.)
17400Sstevel@tonic-gate 		 */
17410Sstevel@tonic-gate 		if (extv[0] != NULL) {
17420Sstevel@tonic-gate 			/*
17430Sstevel@tonic-gate 			 * FLUSH or DUMP messages shouldn't have extensions.
17440Sstevel@tonic-gate 			 * Return EINVAL.
17450Sstevel@tonic-gate 			 */
1746*3448Sdh155122 			ks2dbg(keystack, ("FLUSH message with extension.\n"));
17470Sstevel@tonic-gate 			keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_NO_EXT);
17480Sstevel@tonic-gate 			return;
17490Sstevel@tonic-gate 		}
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 		/* Passing down of DUMP/FLUSH messages are special. */
17520Sstevel@tonic-gate 		qwriter(q, mp, keysock_do_flushdump, PERIM_INNER);
17530Sstevel@tonic-gate 		return;
17540Sstevel@tonic-gate 	case SADB_X_PROMISC:
17550Sstevel@tonic-gate 		/*
17560Sstevel@tonic-gate 		 * Promiscuous processing message.
17570Sstevel@tonic-gate 		 */
17580Sstevel@tonic-gate 		if (samsg->sadb_msg_satype == 0)
17590Sstevel@tonic-gate 			ks->keysock_flags &= ~KEYSOCK_PROMISC;
17600Sstevel@tonic-gate 		else
17610Sstevel@tonic-gate 			ks->keysock_flags |= KEYSOCK_PROMISC;
1762*3448Sdh155122 		keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE,
1763*3448Sdh155122 		    keystack);
17640Sstevel@tonic-gate 		return;
17650Sstevel@tonic-gate 	case SADB_X_INVERSE_ACQUIRE:
17660Sstevel@tonic-gate 		keysock_inverse_acquire(mp, samsg, extv, ks);
17670Sstevel@tonic-gate 		return;
17680Sstevel@tonic-gate 	default:
1769*3448Sdh155122 		ks2dbg(keystack, ("Got unknown message type %d.\n",
17700Sstevel@tonic-gate 		    samsg->sadb_msg_type));
17710Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_MSG);
17720Sstevel@tonic-gate 		return;
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	/* As a placeholder... */
17760Sstevel@tonic-gate 	ks0dbg(("keysock_parse():  Hit EOPNOTSUPP\n"));
17770Sstevel@tonic-gate 	keysock_error(ks, mp, EOPNOTSUPP, SADB_X_DIAGNOSTIC_NONE);
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate /*
17810Sstevel@tonic-gate  * wput routing for PF_KEY/keysock/whatever.  Unlike the routing socket,
17820Sstevel@tonic-gate  * I don't convert to ioctl()'s for IP.  I am the end-all driver as far
17830Sstevel@tonic-gate  * as PF_KEY sockets are concerned.  I do some conversion, but not as much
17840Sstevel@tonic-gate  * as IP/rts does.
17850Sstevel@tonic-gate  */
17860Sstevel@tonic-gate static void
17870Sstevel@tonic-gate keysock_wput(queue_t *q, mblk_t *mp)
17880Sstevel@tonic-gate {
17890Sstevel@tonic-gate 	uchar_t *rptr = mp->b_rptr;
17900Sstevel@tonic-gate 	mblk_t *mp1;
1791*3448Sdh155122 	keysock_t *ks;
1792*3448Sdh155122 	keysock_stack_t	*keystack;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	if (WR(q)->q_next) {
17950Sstevel@tonic-gate 		keysock_consumer_t *kc = (keysock_consumer_t *)q->q_ptr;
1796*3448Sdh155122 		keystack = kc->kc_keystack;
1797*3448Sdh155122 
1798*3448Sdh155122 		ks3dbg(keystack, ("In keysock_wput\n"));
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 		/*
18010Sstevel@tonic-gate 		 * We shouldn't get writes on a consumer instance.
18020Sstevel@tonic-gate 		 * But for now, just passthru.
18030Sstevel@tonic-gate 		 */
1804*3448Sdh155122 		ks1dbg(keystack, ("Huh?  wput for an consumer instance (%d)?\n",
18050Sstevel@tonic-gate 		    kc->kc_sa_type));
18060Sstevel@tonic-gate 		putnext(q, mp);
18070Sstevel@tonic-gate 		return;
18080Sstevel@tonic-gate 	}
1809*3448Sdh155122 	ks = (keysock_t *)q->q_ptr;
1810*3448Sdh155122 	keystack = ks->keysock_keystack;
1811*3448Sdh155122 
1812*3448Sdh155122 	ks3dbg(keystack, ("In keysock_wput\n"));
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
18150Sstevel@tonic-gate 	case M_DATA:
18160Sstevel@tonic-gate 		/*
18170Sstevel@tonic-gate 		 * Silently discard.
18180Sstevel@tonic-gate 		 */
1819*3448Sdh155122 		ks2dbg(keystack, ("raw M_DATA in keysock.\n"));
18200Sstevel@tonic-gate 		freemsg(mp);
18210Sstevel@tonic-gate 		return;
18220Sstevel@tonic-gate 	case M_PROTO:
18230Sstevel@tonic-gate 	case M_PCPROTO:
18240Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
18250Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
18260Sstevel@tonic-gate 				if ((mp1 = mp->b_cont) == NULL) {
18270Sstevel@tonic-gate 					/* No data after T_DATA_REQ. */
1828*3448Sdh155122 					ks2dbg(keystack,
1829*3448Sdh155122 					    ("No data after DATA_REQ.\n"));
18300Sstevel@tonic-gate 					freemsg(mp);
18310Sstevel@tonic-gate 					return;
18320Sstevel@tonic-gate 				}
18330Sstevel@tonic-gate 				freeb(mp);
18340Sstevel@tonic-gate 				mp = mp1;
1835*3448Sdh155122 				ks2dbg(keystack, ("T_DATA_REQ\n"));
18360Sstevel@tonic-gate 				break;	/* Out of switch. */
18370Sstevel@tonic-gate 			}
18380Sstevel@tonic-gate 		}
18390Sstevel@tonic-gate 		/* FALLTHRU */
18400Sstevel@tonic-gate 	default:
1841*3448Sdh155122 		ks3dbg(keystack, ("In default wput case (%d %d).\n",
18420Sstevel@tonic-gate 		    mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
18430Sstevel@tonic-gate 		keysock_wput_other(q, mp);
18440Sstevel@tonic-gate 		return;
18450Sstevel@tonic-gate 	}
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	/* I now have a PF_KEY message in an M_DATA block, pointed to by mp. */
18480Sstevel@tonic-gate 	keysock_parse(q, mp);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_rput(). */
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate /*
18540Sstevel@tonic-gate  * Called upon receipt of a KEYSOCK_HELLO_ACK to set up the appropriate
18550Sstevel@tonic-gate  * state vectors.
18560Sstevel@tonic-gate  */
18570Sstevel@tonic-gate static void
18580Sstevel@tonic-gate keysock_link_consumer(uint8_t satype, keysock_consumer_t *kc)
18590Sstevel@tonic-gate {
18600Sstevel@tonic-gate 	keysock_t *ks;
1861*3448Sdh155122 	keysock_stack_t	*keystack = kc->kc_keystack;
18620Sstevel@tonic-gate 
1863*3448Sdh155122 	mutex_enter(&keystack->keystack_consumers_lock);
18640Sstevel@tonic-gate 	mutex_enter(&kc->kc_lock);
1865*3448Sdh155122 	if (keystack->keystack_consumers[satype] != NULL) {
18660Sstevel@tonic-gate 		ks0dbg((
18670Sstevel@tonic-gate 		    "Hmmmm, someone closed %d before the HELLO_ACK happened.\n",
18680Sstevel@tonic-gate 		    satype));
18690Sstevel@tonic-gate 		/*
18700Sstevel@tonic-gate 		 * Perhaps updating the new below-me consumer with what I have
18710Sstevel@tonic-gate 		 * so far would work too?
18720Sstevel@tonic-gate 		 */
18730Sstevel@tonic-gate 		mutex_exit(&kc->kc_lock);
1874*3448Sdh155122 		mutex_exit(&keystack->keystack_consumers_lock);
18750Sstevel@tonic-gate 	} else {
18760Sstevel@tonic-gate 		/* Add new below-me consumer. */
1877*3448Sdh155122 		keystack->keystack_consumers[satype] = kc;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 		kc->kc_flags = 0;
18800Sstevel@tonic-gate 		kc->kc_sa_type = satype;
18810Sstevel@tonic-gate 		mutex_exit(&kc->kc_lock);
1882*3448Sdh155122 		mutex_exit(&keystack->keystack_consumers_lock);
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 		/* Scan the keysock list. */
1885*3448Sdh155122 		mutex_enter(&keystack->keystack_list_lock);
1886*3448Sdh155122 		for (ks = keystack->keystack_list; ks != NULL;
1887*3448Sdh155122 		    ks = ks->keysock_next) {
18880Sstevel@tonic-gate 			if (KEYSOCK_ISREG(ks, satype)) {
18890Sstevel@tonic-gate 				/*
18900Sstevel@tonic-gate 				 * XXX Perhaps send an SADB_REGISTER down on
18910Sstevel@tonic-gate 				 * the socket's behalf.
18920Sstevel@tonic-gate 				 */
1893*3448Sdh155122 				ks1dbg(keystack,
1894*3448Sdh155122 				    ("Socket %u registered already for "
18950Sstevel@tonic-gate 				    "new consumer.\n", ks->keysock_serial));
18960Sstevel@tonic-gate 			}
18970Sstevel@tonic-gate 		}
1898*3448Sdh155122 		mutex_exit(&keystack->keystack_list_lock);
18990Sstevel@tonic-gate 	}
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate /*
19030Sstevel@tonic-gate  * Generate a KEYSOCK_OUT_ERR message for my consumer.
19040Sstevel@tonic-gate  */
19050Sstevel@tonic-gate static void
19060Sstevel@tonic-gate keysock_out_err(keysock_consumer_t *kc, int ks_errno, mblk_t *mp)
19070Sstevel@tonic-gate {
19080Sstevel@tonic-gate 	keysock_out_err_t *kse;
19090Sstevel@tonic-gate 	mblk_t *imp;
1910*3448Sdh155122 	keysock_stack_t	*keystack = kc->kc_keystack;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	imp = allocb(sizeof (ipsec_info_t), BPRI_HI);
19130Sstevel@tonic-gate 	if (imp == NULL) {
1914*3448Sdh155122 		ks1dbg(keystack, ("keysock_out_err:  Can't alloc message.\n"));
19150Sstevel@tonic-gate 		return;
19160Sstevel@tonic-gate 	}
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	imp->b_datap->db_type = M_CTL;
19190Sstevel@tonic-gate 	imp->b_wptr += sizeof (ipsec_info_t);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	kse = (keysock_out_err_t *)imp->b_rptr;
19220Sstevel@tonic-gate 	imp->b_cont = mp;
19230Sstevel@tonic-gate 	kse->ks_err_type = KEYSOCK_OUT_ERR;
19240Sstevel@tonic-gate 	kse->ks_err_len = sizeof (*kse);
19250Sstevel@tonic-gate 	/* Is serial necessary? */
19260Sstevel@tonic-gate 	kse->ks_err_serial = 0;
19270Sstevel@tonic-gate 	kse->ks_err_errno = ks_errno;
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	/*
19300Sstevel@tonic-gate 	 * XXX What else do I need to do here w.r.t. information
19310Sstevel@tonic-gate 	 * to tell the consumer what caused this error?
19320Sstevel@tonic-gate 	 *
19330Sstevel@tonic-gate 	 * I believe the answer is the PF_KEY ACQUIRE (or other) message
19340Sstevel@tonic-gate 	 * attached in mp, which is appended at the end.  I believe the
19350Sstevel@tonic-gate 	 * db_ref won't matter here, because the PF_KEY message is only read
19360Sstevel@tonic-gate 	 * for KEYSOCK_OUT_ERR.
19370Sstevel@tonic-gate 	 */
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	putnext(kc->kc_wq, imp);
19400Sstevel@tonic-gate }
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate /* XXX this is a hack errno. */
19430Sstevel@tonic-gate #define	EIPSECNOSA 255
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate /*
19460Sstevel@tonic-gate  * Route message (pointed by mp, header in samsg) toward appropriate
19470Sstevel@tonic-gate  * sockets.  Assume the message's creator did its job correctly.
19480Sstevel@tonic-gate  *
19490Sstevel@tonic-gate  * This should be a function that is followed by a return in its caller.
19500Sstevel@tonic-gate  * The compiler _should_ be able to use tail-call optimizations to make the
19510Sstevel@tonic-gate  * large ## of parameters not a huge deal.
19520Sstevel@tonic-gate  */
19530Sstevel@tonic-gate static void
19540Sstevel@tonic-gate keysock_passup(mblk_t *mp, sadb_msg_t *samsg, minor_t serial,
1955*3448Sdh155122     keysock_consumer_t *kc, boolean_t persistent, keysock_stack_t *keystack)
19560Sstevel@tonic-gate {
19570Sstevel@tonic-gate 	keysock_t *ks;
19580Sstevel@tonic-gate 	uint8_t satype = samsg->sadb_msg_satype;
19590Sstevel@tonic-gate 	boolean_t toall = B_FALSE, allreg = B_FALSE, allereg = B_FALSE,
19600Sstevel@tonic-gate 	    setalg = B_FALSE;
19610Sstevel@tonic-gate 	mblk_t *mp1;
19620Sstevel@tonic-gate 	int err = EIPSECNOSA;
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 	/* Convert mp, which is M_DATA, into an M_PROTO of type T_DATA_IND */
19650Sstevel@tonic-gate 	mp1 = allocb(sizeof (struct T_data_req), BPRI_HI);
19660Sstevel@tonic-gate 	if (mp1 == NULL) {
19670Sstevel@tonic-gate 		err = ENOMEM;
19680Sstevel@tonic-gate 		goto error;
19690Sstevel@tonic-gate 	}
19700Sstevel@tonic-gate 	mp1->b_wptr += sizeof (struct T_data_req);
19710Sstevel@tonic-gate 	((struct T_data_ind *)mp1->b_rptr)->PRIM_type = T_DATA_IND;
19720Sstevel@tonic-gate 	((struct T_data_ind *)mp1->b_rptr)->MORE_flag = 0;
19730Sstevel@tonic-gate 	mp1->b_datap->db_type = M_PROTO;
19740Sstevel@tonic-gate 	mp1->b_cont = mp;
19750Sstevel@tonic-gate 	mp = mp1;
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
19780Sstevel@tonic-gate 	case SADB_FLUSH:
19790Sstevel@tonic-gate 	case SADB_GETSPI:
19800Sstevel@tonic-gate 	case SADB_UPDATE:
19810Sstevel@tonic-gate 	case SADB_ADD:
19820Sstevel@tonic-gate 	case SADB_DELETE:
19830Sstevel@tonic-gate 	case SADB_EXPIRE:
19840Sstevel@tonic-gate 		/*
19850Sstevel@tonic-gate 		 * These are most likely replies.  Don't worry about
19860Sstevel@tonic-gate 		 * KEYSOCK_OUT_ERR handling.  Deliver to all sockets.
19870Sstevel@tonic-gate 		 */
1988*3448Sdh155122 		ks3dbg(keystack,
1989*3448Sdh155122 		    ("Delivering normal message (%d) to all sockets.\n",
19900Sstevel@tonic-gate 		    samsg->sadb_msg_type));
19910Sstevel@tonic-gate 		toall = B_TRUE;
19920Sstevel@tonic-gate 		break;
19930Sstevel@tonic-gate 	case SADB_REGISTER:
19940Sstevel@tonic-gate 		/*
19950Sstevel@tonic-gate 		 * REGISTERs come up for one of three reasons:
19960Sstevel@tonic-gate 		 *
19970Sstevel@tonic-gate 		 *	1.) In response to a normal SADB_REGISTER
19980Sstevel@tonic-gate 		 *		(samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
19990Sstevel@tonic-gate 		 *		    serial != 0)
20000Sstevel@tonic-gate 		 *		Deliver to normal SADB_REGISTERed sockets.
20010Sstevel@tonic-gate 		 *	2.) In response to an extended REGISTER
20020Sstevel@tonic-gate 		 *		(samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
20030Sstevel@tonic-gate 		 *		Deliver to extended REGISTERed socket.
20040Sstevel@tonic-gate 		 *	3.) Spontaneous algorithm changes
20050Sstevel@tonic-gate 		 *		(samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
20060Sstevel@tonic-gate 		 *		    serial == 0)
20070Sstevel@tonic-gate 		 *		Deliver to REGISTERed sockets of all sorts.
20080Sstevel@tonic-gate 		 */
20090Sstevel@tonic-gate 		if (kc == NULL) {
20100Sstevel@tonic-gate 			/* Here because of keysock_error() call. */
20110Sstevel@tonic-gate 			ASSERT(samsg->sadb_msg_errno != 0);
20120Sstevel@tonic-gate 			break;	/* Out of switch. */
20130Sstevel@tonic-gate 		}
2014*3448Sdh155122 		ks3dbg(keystack, ("Delivering REGISTER.\n"));
20150Sstevel@tonic-gate 		if (satype == SADB_SATYPE_UNSPEC) {
20160Sstevel@tonic-gate 			/* REGISTER Reason #2 */
20170Sstevel@tonic-gate 			allereg = B_TRUE;
20180Sstevel@tonic-gate 			/*
20190Sstevel@tonic-gate 			 * Rewhack SA type so PF_KEY socket holder knows what
20200Sstevel@tonic-gate 			 * consumer generated this algorithm list.
20210Sstevel@tonic-gate 			 */
20220Sstevel@tonic-gate 			satype = kc->kc_sa_type;
20230Sstevel@tonic-gate 			samsg->sadb_msg_satype = satype;
20240Sstevel@tonic-gate 			setalg = B_TRUE;
20250Sstevel@tonic-gate 		} else if (serial == 0) {
20260Sstevel@tonic-gate 			/* REGISTER Reason #3 */
20270Sstevel@tonic-gate 			allreg = B_TRUE;
20280Sstevel@tonic-gate 			allereg = B_TRUE;
20290Sstevel@tonic-gate 		} else {
20300Sstevel@tonic-gate 			/* REGISTER Reason #1 */
20310Sstevel@tonic-gate 			allreg = B_TRUE;
20320Sstevel@tonic-gate 			setalg = B_TRUE;
20330Sstevel@tonic-gate 		}
20340Sstevel@tonic-gate 		break;
20350Sstevel@tonic-gate 	case SADB_ACQUIRE:
20360Sstevel@tonic-gate 		/*
20370Sstevel@tonic-gate 		 * ACQUIREs are either extended (sadb_msg_satype == 0) or
20380Sstevel@tonic-gate 		 * regular (sadb_msg_satype != 0).  And we're guaranteed
20390Sstevel@tonic-gate 		 * that serial == 0 for an ACQUIRE.
20400Sstevel@tonic-gate 		 */
2041*3448Sdh155122 		ks3dbg(keystack, ("Delivering ACQUIRE.\n"));
20420Sstevel@tonic-gate 		allereg = (satype == SADB_SATYPE_UNSPEC);
20430Sstevel@tonic-gate 		allreg = !allereg;
20440Sstevel@tonic-gate 		/*
20450Sstevel@tonic-gate 		 * Corner case - if we send a regular ACQUIRE and there's
20460Sstevel@tonic-gate 		 * extended ones registered, don't send an error down to
20470Sstevel@tonic-gate 		 * consumers if nobody's listening and prematurely destroy
20480Sstevel@tonic-gate 		 * their ACQUIRE record.  This might be too hackish of a
20490Sstevel@tonic-gate 		 * solution.
20500Sstevel@tonic-gate 		 */
2051*3448Sdh155122 		if (allreg && keystack->keystack_num_extended > 0)
20520Sstevel@tonic-gate 			err = 0;
20530Sstevel@tonic-gate 		break;
20540Sstevel@tonic-gate 	case SADB_X_PROMISC:
20550Sstevel@tonic-gate 	case SADB_X_INVERSE_ACQUIRE:
20560Sstevel@tonic-gate 	case SADB_DUMP:
20570Sstevel@tonic-gate 	case SADB_GET:
20580Sstevel@tonic-gate 	default:
20590Sstevel@tonic-gate 		/*
20600Sstevel@tonic-gate 		 * Deliver to the sender and promiscuous only.
20610Sstevel@tonic-gate 		 */
2062*3448Sdh155122 		ks3dbg(keystack, ("Delivering sender/promisc only (%d).\n",
20630Sstevel@tonic-gate 		    samsg->sadb_msg_type));
20640Sstevel@tonic-gate 		break;
20650Sstevel@tonic-gate 	}
20660Sstevel@tonic-gate 
2067*3448Sdh155122 	mutex_enter(&keystack->keystack_list_lock);
2068*3448Sdh155122 	for (ks = keystack->keystack_list; ks != NULL; ks = ks->keysock_next) {
20690Sstevel@tonic-gate 		/* Delivery loop. */
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 		/*
20720Sstevel@tonic-gate 		 * Check special keysock-setting cases (REGISTER replies)
20730Sstevel@tonic-gate 		 * here.
20740Sstevel@tonic-gate 		 */
20750Sstevel@tonic-gate 		if (setalg && serial == ks->keysock_serial) {
20760Sstevel@tonic-gate 			ASSERT(kc != NULL);
20770Sstevel@tonic-gate 			ASSERT(kc->kc_sa_type == satype);
20780Sstevel@tonic-gate 			KEYSOCK_SETREG(ks, satype);
20790Sstevel@tonic-gate 		}
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 		/*
20820Sstevel@tonic-gate 		 * NOLOOP takes precedence over PROMISC.  So if you've set
20830Sstevel@tonic-gate 		 * !SO_USELOOPBACK, don't expect to see any data...
20840Sstevel@tonic-gate 		 */
20850Sstevel@tonic-gate 		if (ks->keysock_flags & KEYSOCK_NOLOOP)
20860Sstevel@tonic-gate 			continue;
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 		/*
20890Sstevel@tonic-gate 		 * Messages to all, or promiscuous sockets just GET the
20900Sstevel@tonic-gate 		 * message.  Perform rules-type checking iff it's not for all
20910Sstevel@tonic-gate 		 * listeners or the socket is in promiscuous mode.
20920Sstevel@tonic-gate 		 *
20930Sstevel@tonic-gate 		 * NOTE:Because of the (kc != NULL && ISREG()), make sure
20940Sstevel@tonic-gate 		 *	extended ACQUIREs arrive off a consumer that is
20950Sstevel@tonic-gate 		 *	part of the extended REGISTER set of consumers.
20960Sstevel@tonic-gate 		 */
20970Sstevel@tonic-gate 		if (serial != ks->keysock_serial &&
20980Sstevel@tonic-gate 		    !toall &&
20990Sstevel@tonic-gate 		    !(ks->keysock_flags & KEYSOCK_PROMISC) &&
21000Sstevel@tonic-gate 		    !((ks->keysock_flags & KEYSOCK_EXTENDED) ?
21010Sstevel@tonic-gate 			allereg : allreg && kc != NULL &&
21020Sstevel@tonic-gate 			KEYSOCK_ISREG(ks, kc->kc_sa_type)))
21030Sstevel@tonic-gate 			continue;
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 		mp1 = dupmsg(mp);
21060Sstevel@tonic-gate 		if (mp1 == NULL) {
2107*3448Sdh155122 			ks2dbg(keystack, (
21080Sstevel@tonic-gate 			    "keysock_passup():  dupmsg() failed.\n"));
21090Sstevel@tonic-gate 			mp1 = mp;
21100Sstevel@tonic-gate 			mp = NULL;
21110Sstevel@tonic-gate 			err = ENOMEM;
21120Sstevel@tonic-gate 		}
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 		/*
21150Sstevel@tonic-gate 		 * At this point, we can deliver or attempt to deliver
21160Sstevel@tonic-gate 		 * this message.  We're free of obligation to report
21170Sstevel@tonic-gate 		 * no listening PF_KEY sockets.  So set err to 0.
21180Sstevel@tonic-gate 		 */
21190Sstevel@tonic-gate 		err = 0;
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 		/*
21220Sstevel@tonic-gate 		 * See if we canputnext(), as well as see if the message
21230Sstevel@tonic-gate 		 * needs to be queued if we can't.
21240Sstevel@tonic-gate 		 */
21250Sstevel@tonic-gate 		if (!canputnext(ks->keysock_rq)) {
21260Sstevel@tonic-gate 			if (persistent) {
21270Sstevel@tonic-gate 				if (putq(ks->keysock_rq, mp1) == 0) {
2128*3448Sdh155122 					ks1dbg(keystack, (
21290Sstevel@tonic-gate 					    "keysock_passup: putq failed.\n"));
21300Sstevel@tonic-gate 				} else {
21310Sstevel@tonic-gate 					continue;
21320Sstevel@tonic-gate 				}
21330Sstevel@tonic-gate 			}
21340Sstevel@tonic-gate 			freemsg(mp1);
21350Sstevel@tonic-gate 			continue;
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 
2138*3448Sdh155122 		ks3dbg(keystack,
2139*3448Sdh155122 		    ("Putting to serial %d.\n", ks->keysock_serial));
21400Sstevel@tonic-gate 		/*
21410Sstevel@tonic-gate 		 * Unlike the specific keysock instance case, this
21420Sstevel@tonic-gate 		 * will only hit for listeners, so we will only
21430Sstevel@tonic-gate 		 * putnext() if we can.
21440Sstevel@tonic-gate 		 */
21450Sstevel@tonic-gate 		putnext(ks->keysock_rq, mp1);
21460Sstevel@tonic-gate 		if (mp == NULL)
21470Sstevel@tonic-gate 			break;	/* out of for loop. */
21480Sstevel@tonic-gate 	}
2149*3448Sdh155122 	mutex_exit(&keystack->keystack_list_lock);
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate error:
21520Sstevel@tonic-gate 	if ((err != 0) && (kc != NULL)) {
21530Sstevel@tonic-gate 		/*
21540Sstevel@tonic-gate 		 * Generate KEYSOCK_OUT_ERR for consumer.
21550Sstevel@tonic-gate 		 * Basically, I send this back if I have not been able to
21560Sstevel@tonic-gate 		 * transmit (for whatever reason)
21570Sstevel@tonic-gate 		 */
2158*3448Sdh155122 		ks1dbg(keystack,
2159*3448Sdh155122 		    ("keysock_passup():  No registered of type %d.\n",
21600Sstevel@tonic-gate 		    satype));
21610Sstevel@tonic-gate 		if (mp != NULL) {
21620Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_PROTO) {
21630Sstevel@tonic-gate 				mp1 = mp;
21640Sstevel@tonic-gate 				mp = mp->b_cont;
21650Sstevel@tonic-gate 				freeb(mp1);
21660Sstevel@tonic-gate 			}
21670Sstevel@tonic-gate 			/*
21680Sstevel@tonic-gate 			 * Do a copymsg() because people who get
21690Sstevel@tonic-gate 			 * KEYSOCK_OUT_ERR may alter the message contents.
21700Sstevel@tonic-gate 			 */
21710Sstevel@tonic-gate 			mp1 = copymsg(mp);
21720Sstevel@tonic-gate 			if (mp1 == NULL) {
2173*3448Sdh155122 				ks2dbg(keystack,
2174*3448Sdh155122 				    ("keysock_passup: copymsg() failed.\n"));
21750Sstevel@tonic-gate 				mp1 = mp;
21760Sstevel@tonic-gate 				mp = NULL;
21770Sstevel@tonic-gate 			}
21780Sstevel@tonic-gate 			keysock_out_err(kc, err, mp1);
21790Sstevel@tonic-gate 		}
21800Sstevel@tonic-gate 	}
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	/*
21830Sstevel@tonic-gate 	 * XXX Blank the message somehow.  This is difficult because we don't
21840Sstevel@tonic-gate 	 * know at this point if the message has db_ref > 1, etc.
21850Sstevel@tonic-gate 	 *
21860Sstevel@tonic-gate 	 * Optimally, keysock messages containing actual keying material would
21870Sstevel@tonic-gate 	 * be allocated with esballoc(), with a zeroing free function.
21880Sstevel@tonic-gate 	 */
21890Sstevel@tonic-gate 	if (mp != NULL)
21900Sstevel@tonic-gate 		freemsg(mp);
21910Sstevel@tonic-gate }
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate /*
21940Sstevel@tonic-gate  * Keysock's read service procedure is there only for PF_KEY reply
21950Sstevel@tonic-gate  * messages that really need to reach the top.
21960Sstevel@tonic-gate  */
21970Sstevel@tonic-gate static void
21980Sstevel@tonic-gate keysock_rsrv(queue_t *q)
21990Sstevel@tonic-gate {
22000Sstevel@tonic-gate 	mblk_t *mp;
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
22030Sstevel@tonic-gate 		if (canputnext(q)) {
22040Sstevel@tonic-gate 			putnext(q, mp);
22050Sstevel@tonic-gate 		} else {
22060Sstevel@tonic-gate 			(void) putbq(q, mp);
22070Sstevel@tonic-gate 			return;
22080Sstevel@tonic-gate 		}
22090Sstevel@tonic-gate 	}
22100Sstevel@tonic-gate }
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate /*
22130Sstevel@tonic-gate  * The read procedure should only be invoked by a keysock consumer, like
22140Sstevel@tonic-gate  * ESP, AH, etc.  I should only see KEYSOCK_OUT and KEYSOCK_HELLO_ACK
22150Sstevel@tonic-gate  * messages on my read queues.
22160Sstevel@tonic-gate  */
22170Sstevel@tonic-gate static void
22180Sstevel@tonic-gate keysock_rput(queue_t *q, mblk_t *mp)
22190Sstevel@tonic-gate {
22200Sstevel@tonic-gate 	keysock_consumer_t *kc = (keysock_consumer_t *)q->q_ptr;
22210Sstevel@tonic-gate 	ipsec_info_t *ii;
22220Sstevel@tonic-gate 	keysock_hello_ack_t *ksa;
22230Sstevel@tonic-gate 	minor_t serial;
22240Sstevel@tonic-gate 	mblk_t *mp1;
22250Sstevel@tonic-gate 	sadb_msg_t *samsg;
2226*3448Sdh155122 	keysock_stack_t	*keystack = kc->kc_keystack;
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	/* Make sure I'm a consumer instance.  (i.e. something's below me) */
22290Sstevel@tonic-gate 	ASSERT(WR(q)->q_next != NULL);
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_CTL) {
22320Sstevel@tonic-gate 		/*
22330Sstevel@tonic-gate 		 * Keysock should only see keysock consumer interface
22340Sstevel@tonic-gate 		 * messages (see ipsec_info.h) on its read procedure.
22350Sstevel@tonic-gate 		 * To be robust, however, putnext() up so the STREAM head can
22360Sstevel@tonic-gate 		 * deal with it appropriately.
22370Sstevel@tonic-gate 		 */
2238*3448Sdh155122 		ks1dbg(keystack,
2239*3448Sdh155122 		    ("Hmmm, a non M_CTL (%d, 0x%x) on keysock_rput.\n",
22400Sstevel@tonic-gate 		    mp->b_datap->db_type, mp->b_datap->db_type));
22410Sstevel@tonic-gate 		putnext(q, mp);
22420Sstevel@tonic-gate 		return;
22430Sstevel@tonic-gate 	}
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	ii = (ipsec_info_t *)mp->b_rptr;
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 	switch (ii->ipsec_info_type) {
22480Sstevel@tonic-gate 	case KEYSOCK_OUT:
22490Sstevel@tonic-gate 		/*
22500Sstevel@tonic-gate 		 * A consumer needs to pass a response message or an ACQUIRE
22510Sstevel@tonic-gate 		 * UP.  I assume that the consumer has done the right
22520Sstevel@tonic-gate 		 * thing w.r.t. message creation, etc.
22530Sstevel@tonic-gate 		 */
22540Sstevel@tonic-gate 		serial = ((keysock_out_t *)mp->b_rptr)->ks_out_serial;
22550Sstevel@tonic-gate 		mp1 = mp->b_cont;	/* Get M_DATA portion. */
22560Sstevel@tonic-gate 		freeb(mp);
22570Sstevel@tonic-gate 		samsg = (sadb_msg_t *)mp1->b_rptr;
22580Sstevel@tonic-gate 		if (samsg->sadb_msg_type == SADB_FLUSH ||
22590Sstevel@tonic-gate 		    (samsg->sadb_msg_type == SADB_DUMP &&
22600Sstevel@tonic-gate 			samsg->sadb_msg_len == SADB_8TO64(sizeof (*samsg)))) {
22610Sstevel@tonic-gate 			/*
22620Sstevel@tonic-gate 			 * If I'm an end-of-FLUSH or an end-of-DUMP marker...
22630Sstevel@tonic-gate 			 */
2264*3448Sdh155122 			ASSERT(keystack->keystack_flushdump != 0);
2265*3448Sdh155122 						/* Am I flushing? */
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 			mutex_enter(&kc->kc_lock);
22680Sstevel@tonic-gate 			kc->kc_flags &= ~KC_FLUSHING;
22690Sstevel@tonic-gate 			mutex_exit(&kc->kc_lock);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 			if (samsg->sadb_msg_errno != 0)
2272*3448Sdh155122 				keystack->keystack_flushdump_errno =
2273*3448Sdh155122 				    samsg->sadb_msg_errno;
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 			/*
22760Sstevel@tonic-gate 			 * Lower the atomic "flushing" count.  If it's
22770Sstevel@tonic-gate 			 * the last one, send up the end-of-{FLUSH,DUMP} to
22780Sstevel@tonic-gate 			 * the appropriate PF_KEY socket.
22790Sstevel@tonic-gate 			 */
2280*3448Sdh155122 			if (atomic_add_32_nv(&keystack->keystack_flushdump,
2281*3448Sdh155122 			    -1) != 0) {
2282*3448Sdh155122 				ks1dbg(keystack,
2283*3448Sdh155122 				    ("One flush/dump message back from %d,"
22840Sstevel@tonic-gate 				    " more to go.\n", samsg->sadb_msg_satype));
22850Sstevel@tonic-gate 				freemsg(mp1);
22860Sstevel@tonic-gate 				return;
22870Sstevel@tonic-gate 			}
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 			samsg->sadb_msg_errno =
2290*3448Sdh155122 			    (uint8_t)keystack->keystack_flushdump_errno;
22910Sstevel@tonic-gate 			if (samsg->sadb_msg_type == SADB_DUMP) {
22920Sstevel@tonic-gate 				samsg->sadb_msg_seq = 0;
22930Sstevel@tonic-gate 			}
22940Sstevel@tonic-gate 		}
22950Sstevel@tonic-gate 		keysock_passup(mp1, samsg, serial, kc,
2296*3448Sdh155122 		    (samsg->sadb_msg_type == SADB_DUMP), keystack);
22970Sstevel@tonic-gate 		return;
22980Sstevel@tonic-gate 	case KEYSOCK_HELLO_ACK:
22990Sstevel@tonic-gate 		/* Aha, now we can link in the consumer! */
23000Sstevel@tonic-gate 		ksa = (keysock_hello_ack_t *)ii;
23010Sstevel@tonic-gate 		keysock_link_consumer(ksa->ks_hello_satype, kc);
23020Sstevel@tonic-gate 		freemsg(mp);
23030Sstevel@tonic-gate 		return;
23040Sstevel@tonic-gate 	default:
2305*3448Sdh155122 		ks1dbg(keystack, ("Hmmm, an IPsec info I'm not used to, 0x%x\n",
23060Sstevel@tonic-gate 		    ii->ipsec_info_type));
23070Sstevel@tonic-gate 		putnext(q, mp);
23080Sstevel@tonic-gate 	}
23090Sstevel@tonic-gate }
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate /*
23120Sstevel@tonic-gate  * So we can avoid external linking problems....
23130Sstevel@tonic-gate  */
23140Sstevel@tonic-gate boolean_t
2315*3448Sdh155122 keysock_extended_reg(netstack_t *ns)
23160Sstevel@tonic-gate {
2317*3448Sdh155122 	keysock_stack_t	*keystack = ns->netstack_keysock;
2318*3448Sdh155122 
2319*3448Sdh155122 	return (keystack->keystack_num_extended != 0);
23200Sstevel@tonic-gate }
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate uint32_t
2323*3448Sdh155122 keysock_next_seq(netstack_t *ns)
23240Sstevel@tonic-gate {
2325*3448Sdh155122 	keysock_stack_t	*keystack = ns->netstack_keysock;
2326*3448Sdh155122 
2327*3448Sdh155122 	return (atomic_add_32_nv(&keystack->keystack_acquire_seq, -1));
23280Sstevel@tonic-gate }
2329