xref: /onnv-gate/usr/src/uts/common/inet/ip/keysock.c (revision 11042:2d6e217af1b4)
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 /*
228730Sdanmcd@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/stream.h>
290Sstevel@tonic-gate #include <sys/strsubr.h>
300Sstevel@tonic-gate #include <sys/strsun.h>
310Sstevel@tonic-gate #include <sys/stropts.h>
320Sstevel@tonic-gate #include <sys/vnode.h>
333448Sdh155122 #include <sys/zone.h>
340Sstevel@tonic-gate #include <sys/strlog.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
370Sstevel@tonic-gate #include <sys/tihdr.h>
380Sstevel@tonic-gate #include <sys/timod.h>
390Sstevel@tonic-gate #include <sys/tiuser.h>
400Sstevel@tonic-gate #include <sys/ddi.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/sunldi.h>
430Sstevel@tonic-gate #include <sys/file.h>
440Sstevel@tonic-gate #include <sys/modctl.h>
450Sstevel@tonic-gate #include <sys/debug.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/cmn_err.h>
480Sstevel@tonic-gate #include <sys/proc.h>
490Sstevel@tonic-gate #include <sys/suntpi.h>
500Sstevel@tonic-gate #include <sys/atomic.h>
510Sstevel@tonic-gate #include <sys/mkdev.h>
520Sstevel@tonic-gate #include <sys/policy.h>
533448Sdh155122 #include <sys/disp.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <sys/socket.h>
560Sstevel@tonic-gate #include <netinet/in.h>
570Sstevel@tonic-gate #include <net/pfkeyv2.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <inet/common.h>
600Sstevel@tonic-gate #include <netinet/ip6.h>
610Sstevel@tonic-gate #include <inet/ip.h>
628348SEric.Yu@Sun.COM #include <inet/proto_set.h>
630Sstevel@tonic-gate #include <inet/nd.h>
640Sstevel@tonic-gate #include <inet/optcom.h>
650Sstevel@tonic-gate #include <inet/ipsec_info.h>
660Sstevel@tonic-gate #include <inet/ipsec_impl.h>
670Sstevel@tonic-gate #include <inet/keysock.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <sys/isa_defs.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * This is a transport provider for the PF_KEY key mangement socket.
730Sstevel@tonic-gate  * (See RFC 2367 for details.)
740Sstevel@tonic-gate  * Downstream messages are wrapped in a keysock consumer interface KEYSOCK_IN
750Sstevel@tonic-gate  * messages (see ipsec_info.h), and passed to the appropriate consumer.
760Sstevel@tonic-gate  * Upstream messages are generated for all open PF_KEY sockets, when
770Sstevel@tonic-gate  * appropriate, as well as the sender (as long as SO_USELOOPBACK is enabled)
780Sstevel@tonic-gate  * in reply to downstream messages.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  * Upstream messages must be created asynchronously for the following
810Sstevel@tonic-gate  * situations:
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  *	1.) A keysock consumer requires an SA, and there is currently none.
840Sstevel@tonic-gate  *	2.) An SA expires, either hard or soft lifetime.
850Sstevel@tonic-gate  *	3.) Other events a consumer deems fit.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * The MT model of this is PERMOD, with shared put procedures.  Two types of
880Sstevel@tonic-gate  * messages, SADB_FLUSH and SADB_DUMP, need to lock down the perimeter to send
890Sstevel@tonic-gate  * down the *multiple* messages they create.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static vmem_t *keysock_vmem;		/* for minor numbers. */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #define	KEYSOCK_MAX_CONSUMERS 256
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
970Sstevel@tonic-gate static struct T_info_ack keysock_g_t_info_ack = {
980Sstevel@tonic-gate 	T_INFO_ACK,
990Sstevel@tonic-gate 	T_INFINITE,	/* TSDU_size. Maximum size messages. */
1000Sstevel@tonic-gate 	T_INVALID,	/* ETSDU_size. No expedited data. */
1010Sstevel@tonic-gate 	T_INVALID,	/* CDATA_size. No connect data. */
1020Sstevel@tonic-gate 	T_INVALID,	/* DDATA_size. No disconnect data. */
1030Sstevel@tonic-gate 	0,		/* ADDR_size. */
1040Sstevel@tonic-gate 	0,		/* OPT_size. No user-settable options */
1050Sstevel@tonic-gate 	64 * 1024,	/* TIDU_size. keysock allows maximum size messages. */
1060Sstevel@tonic-gate 	T_COTS,		/* SERV_type. keysock supports connection oriented. */
1070Sstevel@tonic-gate 	TS_UNBND,	/* CURRENT_state. This is set from keysock_state. */
1080Sstevel@tonic-gate 	(XPG4_1)	/* Provider flags */
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
1123448Sdh155122 typedef struct keysockparam_s {
1130Sstevel@tonic-gate 	uint_t	keysock_param_min;
1140Sstevel@tonic-gate 	uint_t	keysock_param_max;
1150Sstevel@tonic-gate 	uint_t	keysock_param_value;
1160Sstevel@tonic-gate 	char	*keysock_param_name;
1170Sstevel@tonic-gate } keysockparam_t;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Table of NDD variables supported by keysock. These are loaded into
1210Sstevel@tonic-gate  * keysock_g_nd in keysock_init_nd.
1220Sstevel@tonic-gate  * All of these are alterable, within the min/max values given, at run time.
1230Sstevel@tonic-gate  */
1243448Sdh155122 static	keysockparam_t	lcl_param_arr[] = {
1250Sstevel@tonic-gate 	/* min	max	value	name */
1260Sstevel@tonic-gate 	{ 4096, 65536,	8192,	"keysock_xmit_hiwat"},
1270Sstevel@tonic-gate 	{ 0,	65536,	1024,	"keysock_xmit_lowat"},
1280Sstevel@tonic-gate 	{ 4096, 65536,	8192,	"keysock_recv_hiwat"},
1290Sstevel@tonic-gate 	{ 65536, 1024*1024*1024, 256*1024,	"keysock_max_buf"},
1300Sstevel@tonic-gate 	{ 0,	3,	0,	"keysock_debug"},
1310Sstevel@tonic-gate };
1323448Sdh155122 #define	keystack_xmit_hiwat	keystack_params[0].keysock_param_value
1333448Sdh155122 #define	keystack_xmit_lowat	keystack_params[1].keysock_param_value
1343448Sdh155122 #define	keystack_recv_hiwat	keystack_params[2].keysock_param_value
1353448Sdh155122 #define	keystack_max_buf	keystack_params[3].keysock_param_value
1363448Sdh155122 #define	keystack_debug	keystack_params[4].keysock_param_value
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate #define	ks0dbg(a)	printf a
1390Sstevel@tonic-gate /* NOTE:  != 0 instead of > 0 so lint doesn't complain. */
1403448Sdh155122 #define	ks1dbg(keystack, a)	if (keystack->keystack_debug != 0) printf a
1413448Sdh155122 #define	ks2dbg(keystack, a)	if (keystack->keystack_debug > 1) printf a
1423448Sdh155122 #define	ks3dbg(keystack, a)	if (keystack->keystack_debug > 2) printf a
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static int keysock_close(queue_t *);
1450Sstevel@tonic-gate static int keysock_open(queue_t *, dev_t *, int, int, cred_t *);
1460Sstevel@tonic-gate static void keysock_wput(queue_t *, mblk_t *);
1470Sstevel@tonic-gate static void keysock_rput(queue_t *, mblk_t *);
1480Sstevel@tonic-gate static void keysock_rsrv(queue_t *);
1490Sstevel@tonic-gate static void keysock_passup(mblk_t *, sadb_msg_t *, minor_t,
1503448Sdh155122     keysock_consumer_t *, boolean_t, keysock_stack_t *);
1513448Sdh155122 static void *keysock_stack_init(netstackid_t stackid, netstack_t *ns);
1523448Sdh155122 static void keysock_stack_fini(netstackid_t stackid, void *arg);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static struct module_info info = {
1550Sstevel@tonic-gate 	5138, "keysock", 1, INFPSZ, 512, 128
1560Sstevel@tonic-gate };
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static struct qinit rinit = {
1590Sstevel@tonic-gate 	(pfi_t)keysock_rput, (pfi_t)keysock_rsrv, keysock_open, keysock_close,
1600Sstevel@tonic-gate 	NULL, &info
1610Sstevel@tonic-gate };
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static struct qinit winit = {
1640Sstevel@tonic-gate 	(pfi_t)keysock_wput, NULL, NULL, NULL, NULL, &info
1650Sstevel@tonic-gate };
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate struct streamtab keysockinfo = {
1680Sstevel@tonic-gate 	&rinit, &winit
1690Sstevel@tonic-gate };
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate extern struct modlinkage *keysock_modlp;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * Plumb IPsec.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * NOTE:  New "default" modules will need to be loaded here if needed before
1770Sstevel@tonic-gate  *	  boot time.
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /* Keep these in global space to keep the lint from complaining. */
1810Sstevel@tonic-gate static char *IPSECESP = "ipsecesp";
1820Sstevel@tonic-gate static char *IPSECESPDEV = "/devices/pseudo/ipsecesp@0:ipsecesp";
1830Sstevel@tonic-gate static char *IPSECAH = "ipsecah";
1840Sstevel@tonic-gate static char *IPSECAHDEV = "/devices/pseudo/ipsecah@0:ipsecah";
1850Sstevel@tonic-gate static char *IP6DEV = "/devices/pseudo/ip6@0:ip6";
1860Sstevel@tonic-gate static char *KEYSOCK = "keysock";
1870Sstevel@tonic-gate static char *STRMOD = "strmod";
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * Load the other ipsec modules and plumb them together.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate int
keysock_plumb_ipsec(netstack_t * ns)1933448Sdh155122 keysock_plumb_ipsec(netstack_t *ns)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	ldi_handle_t	lh, ip6_lh = NULL;
1960Sstevel@tonic-gate 	ldi_ident_t	li = NULL;
1970Sstevel@tonic-gate 	int		err = 0;
1980Sstevel@tonic-gate 	int		muxid, rval;
1990Sstevel@tonic-gate 	boolean_t	esp_present = B_TRUE;
2003448Sdh155122 	cred_t		*cr;
2013448Sdh155122 	keysock_stack_t *keystack = ns->netstack_keysock;
2020Sstevel@tonic-gate 
2033448Sdh155122 #ifdef NS_DEBUG
2043448Sdh155122 	(void) printf("keysock_plumb_ipsec(%d)\n",
2053448Sdh155122 	    ns->netstack_stackid);
2063448Sdh155122 #endif
2070Sstevel@tonic-gate 
2083448Sdh155122 	keystack->keystack_plumbed = 0;	/* we're trying again.. */
2093448Sdh155122 
2103448Sdh155122 	cr = zone_get_kcred(netstackid_to_zoneid(
2115240Snordmark 	    keystack->keystack_netstack->netstack_stackid));
2123448Sdh155122 	ASSERT(cr != NULL);
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * Load up the drivers (AH/ESP).
2150Sstevel@tonic-gate 	 *
2160Sstevel@tonic-gate 	 * I do this separately from the actual plumbing in case this function
2170Sstevel@tonic-gate 	 * ever gets called from a diskless boot before the root filesystem is
2180Sstevel@tonic-gate 	 * up.  I don't have to worry about "keysock" because, well, if I'm
2190Sstevel@tonic-gate 	 * here, keysock must've loaded successfully.
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	if (i_ddi_attach_pseudo_node(IPSECAH) == NULL) {
2220Sstevel@tonic-gate 		ks0dbg(("IPsec:  AH failed to attach.\n"));
2230Sstevel@tonic-gate 		goto bail;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	if (i_ddi_attach_pseudo_node(IPSECESP) == NULL) {
2260Sstevel@tonic-gate 		ks0dbg(("IPsec:  ESP failed to attach.\n"));
2270Sstevel@tonic-gate 		esp_present = B_FALSE;
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * Set up the IP streams for AH and ESP, as well as tacking keysock
2320Sstevel@tonic-gate 	 * on top of them.  Assume keysock has set the autopushes up already.
2330Sstevel@tonic-gate 	 */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/* Open IP. */
2360Sstevel@tonic-gate 	err = ldi_ident_from_mod(keysock_modlp, &li);
2370Sstevel@tonic-gate 	if (err) {
2380Sstevel@tonic-gate 		ks0dbg(("IPsec:  lid_ident_from_mod failed (err %d).\n",
2390Sstevel@tonic-gate 		    err));
2400Sstevel@tonic-gate 		goto bail;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2433448Sdh155122 	err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, cr, &ip6_lh, li);
2440Sstevel@tonic-gate 	if (err) {
2450Sstevel@tonic-gate 		ks0dbg(("IPsec:  Open of IP6 failed (err %d).\n", err));
2460Sstevel@tonic-gate 		goto bail;
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/* PLINK KEYSOCK/AH */
2503448Sdh155122 	err = ldi_open_by_name(IPSECAHDEV, FREAD|FWRITE, cr, &lh, li);
2510Sstevel@tonic-gate 	if (err) {
2520Sstevel@tonic-gate 		ks0dbg(("IPsec:  Open of AH failed (err %d).\n", err));
2530Sstevel@tonic-gate 		goto bail;
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 	err = ldi_ioctl(lh,
2563448Sdh155122 	    I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, cr, &rval);
2570Sstevel@tonic-gate 	if (err) {
2580Sstevel@tonic-gate 		ks0dbg(("IPsec:  Push of KEYSOCK onto AH failed (err %d).\n",
2590Sstevel@tonic-gate 		    err));
2603448Sdh155122 		(void) ldi_close(lh, FREAD|FWRITE, cr);
2610Sstevel@tonic-gate 		goto bail;
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 	err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh,
2645240Snordmark 	    FREAD+FWRITE+FNOCTTY+FKIOCTL, cr, &muxid);
2650Sstevel@tonic-gate 	if (err) {
2660Sstevel@tonic-gate 		ks0dbg(("IPsec:  PLINK of KEYSOCK/AH failed (err %d).\n", err));
2673448Sdh155122 		(void) ldi_close(lh, FREAD|FWRITE, cr);
2680Sstevel@tonic-gate 		goto bail;
2690Sstevel@tonic-gate 	}
2703448Sdh155122 	(void) ldi_close(lh, FREAD|FWRITE, cr);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/* PLINK KEYSOCK/ESP */
2730Sstevel@tonic-gate 	if (esp_present) {
2740Sstevel@tonic-gate 		err = ldi_open_by_name(IPSECESPDEV,
2753448Sdh155122 		    FREAD|FWRITE, cr, &lh, li);
2760Sstevel@tonic-gate 		if (err) {
2770Sstevel@tonic-gate 			ks0dbg(("IPsec:  Open of ESP failed (err %d).\n", err));
2780Sstevel@tonic-gate 			goto bail;
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 		err = ldi_ioctl(lh,
2813448Sdh155122 		    I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, cr, &rval);
2820Sstevel@tonic-gate 		if (err) {
2830Sstevel@tonic-gate 			ks0dbg(("IPsec:  "
2840Sstevel@tonic-gate 			    "Push of KEYSOCK onto ESP failed (err %d).\n",
2850Sstevel@tonic-gate 			    err));
2863448Sdh155122 			(void) ldi_close(lh, FREAD|FWRITE, cr);
2870Sstevel@tonic-gate 			goto bail;
2880Sstevel@tonic-gate 		}
2890Sstevel@tonic-gate 		err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh,
2905240Snordmark 		    FREAD+FWRITE+FNOCTTY+FKIOCTL, cr, &muxid);
2910Sstevel@tonic-gate 		if (err) {
2920Sstevel@tonic-gate 			ks0dbg(("IPsec:  "
2930Sstevel@tonic-gate 			    "PLINK of KEYSOCK/ESP failed (err %d).\n", err));
2943448Sdh155122 			(void) ldi_close(lh, FREAD|FWRITE, cr);
2950Sstevel@tonic-gate 			goto bail;
2960Sstevel@tonic-gate 		}
2973448Sdh155122 		(void) ldi_close(lh, FREAD|FWRITE, cr);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate bail:
3013448Sdh155122 	keystack->keystack_plumbed = (err == 0) ? 1 : -1;
3020Sstevel@tonic-gate 	if (ip6_lh != NULL) {
3033448Sdh155122 		(void) ldi_close(ip6_lh, FREAD|FWRITE, cr);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	if (li != NULL)
3060Sstevel@tonic-gate 		ldi_ident_release(li);
3073448Sdh155122 #ifdef NS_DEBUG
3083448Sdh155122 	(void) printf("keysock_plumb_ipsec -> %d\n",
3093448Sdh155122 	    keystack->keystack_plumbed);
3103448Sdh155122 #endif
3113448Sdh155122 	crfree(cr);
3120Sstevel@tonic-gate 	return (err);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /* ARGSUSED */
3160Sstevel@tonic-gate static int
keysock_param_get(q,mp,cp,cr)3170Sstevel@tonic-gate keysock_param_get(q, mp, cp, cr)
3180Sstevel@tonic-gate 	queue_t	*q;
3190Sstevel@tonic-gate 	mblk_t	*mp;
3200Sstevel@tonic-gate 	caddr_t	cp;
3210Sstevel@tonic-gate 	cred_t *cr;
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	keysockparam_t	*keysockpa = (keysockparam_t *)cp;
3240Sstevel@tonic-gate 	uint_t value;
3253448Sdh155122 	keysock_t *ks = (keysock_t *)q->q_ptr;
3263448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
3270Sstevel@tonic-gate 
3283448Sdh155122 	mutex_enter(&keystack->keystack_param_lock);
3290Sstevel@tonic-gate 	value = keysockpa->keysock_param_value;
3303448Sdh155122 	mutex_exit(&keystack->keystack_param_lock);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	(void) mi_mpprintf(mp, "%u", value);
3330Sstevel@tonic-gate 	return (0);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /* This routine sets an NDD variable in a keysockparam_t structure. */
3370Sstevel@tonic-gate /* ARGSUSED */
3380Sstevel@tonic-gate static int
keysock_param_set(q,mp,value,cp,cr)3390Sstevel@tonic-gate keysock_param_set(q, mp, value, cp, cr)
3400Sstevel@tonic-gate 	queue_t	*q;
3410Sstevel@tonic-gate 	mblk_t	*mp;
3420Sstevel@tonic-gate 	char	*value;
3430Sstevel@tonic-gate 	caddr_t	cp;
3440Sstevel@tonic-gate 	cred_t *cr;
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	ulong_t	new_value;
3470Sstevel@tonic-gate 	keysockparam_t	*keysockpa = (keysockparam_t *)cp;
3483448Sdh155122 	keysock_t *ks = (keysock_t *)q->q_ptr;
3493448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	/* Convert the value from a string into a long integer. */
3520Sstevel@tonic-gate 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
3530Sstevel@tonic-gate 		return (EINVAL);
3540Sstevel@tonic-gate 
3553448Sdh155122 	mutex_enter(&keystack->keystack_param_lock);
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * Fail the request if the new value does not lie within the
3580Sstevel@tonic-gate 	 * required bounds.
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	if (new_value < keysockpa->keysock_param_min ||
3610Sstevel@tonic-gate 	    new_value > keysockpa->keysock_param_max) {
3623448Sdh155122 		mutex_exit(&keystack->keystack_param_lock);
3630Sstevel@tonic-gate 		return (EINVAL);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/* Set the new value */
3670Sstevel@tonic-gate 	keysockpa->keysock_param_value = new_value;
3683448Sdh155122 	mutex_exit(&keystack->keystack_param_lock);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	return (0);
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate /*
3743448Sdh155122  * Initialize keysock at module load time
3750Sstevel@tonic-gate  */
3760Sstevel@tonic-gate boolean_t
keysock_ddi_init(void)3770Sstevel@tonic-gate keysock_ddi_init(void)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	keysock_max_optsize = optcom_max_optsize(
3800Sstevel@tonic-gate 	    keysock_opt_obj.odb_opt_des_arr, keysock_opt_obj.odb_opt_arr_cnt);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	keysock_vmem = vmem_create("keysock", (void *)1, MAXMIN, 1,
3830Sstevel@tonic-gate 	    NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
3840Sstevel@tonic-gate 
3853448Sdh155122 	/*
3863448Sdh155122 	 * We want to be informed each time a stack is created or
3873448Sdh155122 	 * destroyed in the kernel, so we can maintain the
3883448Sdh155122 	 * set of keysock_stack_t's.
3893448Sdh155122 	 */
3903448Sdh155122 	netstack_register(NS_KEYSOCK, keysock_stack_init, NULL,
3913448Sdh155122 	    keysock_stack_fini);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	return (B_TRUE);
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate /*
3973448Sdh155122  * Walk through the param array specified registering each element with the
3983448Sdh155122  * named dispatch handler.
3993448Sdh155122  */
4003448Sdh155122 static boolean_t
keysock_param_register(IDP * ndp,keysockparam_t * ksp,int cnt)4013448Sdh155122 keysock_param_register(IDP *ndp, keysockparam_t *ksp, int cnt)
4023448Sdh155122 {
4033448Sdh155122 	for (; cnt-- > 0; ksp++) {
4043448Sdh155122 		if (ksp->keysock_param_name != NULL &&
4053448Sdh155122 		    ksp->keysock_param_name[0]) {
4063448Sdh155122 			if (!nd_load(ndp,
4073448Sdh155122 			    ksp->keysock_param_name,
4083448Sdh155122 			    keysock_param_get, keysock_param_set,
4093448Sdh155122 			    (caddr_t)ksp)) {
4103448Sdh155122 				nd_free(ndp);
4113448Sdh155122 				return (B_FALSE);
4123448Sdh155122 			}
4133448Sdh155122 		}
4143448Sdh155122 	}
4153448Sdh155122 	return (B_TRUE);
4163448Sdh155122 }
4173448Sdh155122 
4183448Sdh155122 /*
4193448Sdh155122  * Initialize keysock for one stack instance
4203448Sdh155122  */
4213448Sdh155122 /* ARGSUSED */
4223448Sdh155122 static void *
keysock_stack_init(netstackid_t stackid,netstack_t * ns)4233448Sdh155122 keysock_stack_init(netstackid_t stackid, netstack_t *ns)
4243448Sdh155122 {
4253448Sdh155122 	keysock_stack_t	*keystack;
4263448Sdh155122 	keysockparam_t *ksp;
4273448Sdh155122 
4283448Sdh155122 	keystack = (keysock_stack_t *)kmem_zalloc(sizeof (*keystack), KM_SLEEP);
4293448Sdh155122 	keystack->keystack_netstack = ns;
4303448Sdh155122 
4313448Sdh155122 	keystack->keystack_acquire_seq = 0xffffffff;
4323448Sdh155122 
4333448Sdh155122 	ksp = (keysockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
4343448Sdh155122 	keystack->keystack_params = ksp;
4353448Sdh155122 	bcopy(lcl_param_arr, ksp, sizeof (lcl_param_arr));
4363448Sdh155122 
4373448Sdh155122 	(void) keysock_param_register(&keystack->keystack_g_nd, ksp,
4383448Sdh155122 	    A_CNT(lcl_param_arr));
4393448Sdh155122 
4403448Sdh155122 	mutex_init(&keystack->keystack_list_lock, NULL, MUTEX_DEFAULT, NULL);
4413448Sdh155122 	mutex_init(&keystack->keystack_consumers_lock,
4423448Sdh155122 	    NULL, MUTEX_DEFAULT, NULL);
4433448Sdh155122 	mutex_init(&keystack->keystack_param_lock, NULL, MUTEX_DEFAULT, NULL);
4443448Sdh155122 	return (keystack);
4453448Sdh155122 }
4463448Sdh155122 
4473448Sdh155122 /*
4480Sstevel@tonic-gate  * Free NDD variable space, and other destructors, for keysock.
4490Sstevel@tonic-gate  */
4500Sstevel@tonic-gate void
keysock_ddi_destroy(void)4510Sstevel@tonic-gate keysock_ddi_destroy(void)
4520Sstevel@tonic-gate {
4533448Sdh155122 	netstack_unregister(NS_KEYSOCK);
4540Sstevel@tonic-gate 	vmem_destroy(keysock_vmem);
4553448Sdh155122 }
4563448Sdh155122 
4573448Sdh155122 /*
4583448Sdh155122  * Remove one stack instance from keysock
4593448Sdh155122  */
4603448Sdh155122 /* ARGSUSED */
4613448Sdh155122 static void
keysock_stack_fini(netstackid_t stackid,void * arg)4623448Sdh155122 keysock_stack_fini(netstackid_t stackid, void *arg)
4633448Sdh155122 {
4643448Sdh155122 	keysock_stack_t *keystack = (keysock_stack_t *)arg;
4653448Sdh155122 
4663448Sdh155122 	nd_free(&keystack->keystack_g_nd);
4673448Sdh155122 	kmem_free(keystack->keystack_params, sizeof (lcl_param_arr));
4683448Sdh155122 	keystack->keystack_params = NULL;
4693448Sdh155122 
4703448Sdh155122 	mutex_destroy(&keystack->keystack_list_lock);
4713448Sdh155122 	mutex_destroy(&keystack->keystack_consumers_lock);
4723448Sdh155122 	mutex_destroy(&keystack->keystack_param_lock);
4733448Sdh155122 
4743448Sdh155122 	kmem_free(keystack, sizeof (*keystack));
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate  * Close routine for keysock.
4790Sstevel@tonic-gate  */
4800Sstevel@tonic-gate static int
keysock_close(queue_t * q)4810Sstevel@tonic-gate keysock_close(queue_t *q)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	keysock_t *ks;
4840Sstevel@tonic-gate 	keysock_consumer_t *kc;
4850Sstevel@tonic-gate 	void *ptr = q->q_ptr;
4860Sstevel@tonic-gate 	int size;
4873448Sdh155122 	keysock_stack_t	*keystack;
4883448Sdh155122 
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	qprocsoff(q);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/* Safe assumption. */
4930Sstevel@tonic-gate 	ASSERT(ptr != NULL);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	if (WR(q)->q_next) {
4960Sstevel@tonic-gate 		kc = (keysock_consumer_t *)ptr;
4973448Sdh155122 		keystack = kc->kc_keystack;
4983448Sdh155122 
4993448Sdh155122 		ks1dbg(keystack, ("Module close, removing a consumer (%d).\n",
5000Sstevel@tonic-gate 		    kc->kc_sa_type));
5010Sstevel@tonic-gate 		/*
5020Sstevel@tonic-gate 		 * Because of PERMOD open/close exclusive perimeter, I
5030Sstevel@tonic-gate 		 * can inspect KC_FLUSHING w/o locking down kc->kc_lock.
5040Sstevel@tonic-gate 		 */
5050Sstevel@tonic-gate 		if (kc->kc_flags & KC_FLUSHING) {
5060Sstevel@tonic-gate 			/*
5070Sstevel@tonic-gate 			 * If this decrement was the last one, send
5080Sstevel@tonic-gate 			 * down the next pending one, if any.
5090Sstevel@tonic-gate 			 *
5100Sstevel@tonic-gate 			 * With a PERMOD perimeter, the mutexes ops aren't
5110Sstevel@tonic-gate 			 * really necessary, but if we ever loosen up, we will
5120Sstevel@tonic-gate 			 * have this bit covered already.
5130Sstevel@tonic-gate 			 */
5143448Sdh155122 			keystack->keystack_flushdump--;
5153448Sdh155122 			if (keystack->keystack_flushdump == 0) {
5160Sstevel@tonic-gate 				/*
5170Sstevel@tonic-gate 				 * The flush/dump terminated by having a
5180Sstevel@tonic-gate 				 * consumer go away.  I need to send up to the
5190Sstevel@tonic-gate 				 * appropriate keysock all of the relevant
5200Sstevel@tonic-gate 				 * information.  Unfortunately, I don't
5210Sstevel@tonic-gate 				 * have that handy.
5220Sstevel@tonic-gate 				 */
5230Sstevel@tonic-gate 				ks0dbg(("Consumer went away while flushing or"
5240Sstevel@tonic-gate 				    " dumping.\n"));
5250Sstevel@tonic-gate 			}
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 		size = sizeof (keysock_consumer_t);
5283448Sdh155122 		mutex_enter(&keystack->keystack_consumers_lock);
5293448Sdh155122 		keystack->keystack_consumers[kc->kc_sa_type] = NULL;
5303448Sdh155122 		mutex_exit(&keystack->keystack_consumers_lock);
5310Sstevel@tonic-gate 		mutex_destroy(&kc->kc_lock);
5323448Sdh155122 		netstack_rele(kc->kc_keystack->keystack_netstack);
5330Sstevel@tonic-gate 	} else {
5340Sstevel@tonic-gate 		ks = (keysock_t *)ptr;
5353448Sdh155122 		keystack = ks->keysock_keystack;
5363448Sdh155122 
5373448Sdh155122 		ks3dbg(keystack,
5383448Sdh155122 		    ("Driver close, PF_KEY socket is going away.\n"));
5390Sstevel@tonic-gate 		if ((ks->keysock_flags & KEYSOCK_EXTENDED) != 0)
5403448Sdh155122 			atomic_add_32(&keystack->keystack_num_extended, -1);
5410Sstevel@tonic-gate 		size = sizeof (keysock_t);
5423448Sdh155122 		mutex_enter(&keystack->keystack_list_lock);
5430Sstevel@tonic-gate 		*(ks->keysock_ptpn) = ks->keysock_next;
5440Sstevel@tonic-gate 		if (ks->keysock_next != NULL)
5450Sstevel@tonic-gate 			ks->keysock_next->keysock_ptpn = ks->keysock_ptpn;
5463448Sdh155122 		mutex_exit(&keystack->keystack_list_lock);
5470Sstevel@tonic-gate 		mutex_destroy(&ks->keysock_lock);
5482465Sdanmcd 		vmem_free(keysock_vmem, (void *)(uintptr_t)ks->keysock_serial,
5492465Sdanmcd 		    1);
5503448Sdh155122 		netstack_rele(ks->keysock_keystack->keystack_netstack);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/* Now I'm free. */
5540Sstevel@tonic-gate 	kmem_free(ptr, size);
5550Sstevel@tonic-gate 	return (0);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate  * Open routine for keysock.
5590Sstevel@tonic-gate  */
5600Sstevel@tonic-gate /* ARGSUSED */
5610Sstevel@tonic-gate static int
keysock_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)5620Sstevel@tonic-gate keysock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	keysock_t *ks;
5650Sstevel@tonic-gate 	keysock_consumer_t *kc;
5660Sstevel@tonic-gate 	mblk_t *mp;
5670Sstevel@tonic-gate 	ipsec_info_t *ii;
5683448Sdh155122 	netstack_t *ns;
5693448Sdh155122 	keysock_stack_t *keystack;
5700Sstevel@tonic-gate 
5713448Sdh155122 	if (secpolicy_ip_config(credp, B_FALSE) != 0) {
5720Sstevel@tonic-gate 		/* Privilege debugging will log the error */
5730Sstevel@tonic-gate 		return (EPERM);
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5770Sstevel@tonic-gate 		return (0);  /* Re-open of an already open instance. */
5780Sstevel@tonic-gate 
5793448Sdh155122 	ns = netstack_find_by_cred(credp);
5803448Sdh155122 	ASSERT(ns != NULL);
5813448Sdh155122 	keystack = ns->netstack_keysock;
5823448Sdh155122 	ASSERT(keystack != NULL);
5833448Sdh155122 
5843448Sdh155122 	ks3dbg(keystack, ("Entering keysock open.\n"));
5853448Sdh155122 
5863448Sdh155122 	if (keystack->keystack_plumbed < 1) {
5873448Sdh155122 		netstack_t *ns = keystack->keystack_netstack;
5883448Sdh155122 
5893448Sdh155122 		keystack->keystack_plumbed = 0;
5903448Sdh155122 #ifdef NS_DEBUG
5913448Sdh155122 		printf("keysock_open(%d) - plumb\n",
5923448Sdh155122 		    keystack->keystack_netstack->netstack_stackid);
5933448Sdh155122 #endif
5940Sstevel@tonic-gate 		/*
5950Sstevel@tonic-gate 		 * Don't worry about ipsec_failure being true here.
5960Sstevel@tonic-gate 		 * (See ip.c).  An open of keysock should try and force
5970Sstevel@tonic-gate 		 * the issue.  Maybe it was a transient failure.
5980Sstevel@tonic-gate 		 */
5993448Sdh155122 		ipsec_loader_loadnow(ns->netstack_ipsec);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	if (sflag & MODOPEN) {
6030Sstevel@tonic-gate 		/* Initialize keysock_consumer state here. */
6040Sstevel@tonic-gate 		kc = kmem_zalloc(sizeof (keysock_consumer_t), KM_NOSLEEP);
6053448Sdh155122 		if (kc == NULL) {
6063448Sdh155122 			netstack_rele(keystack->keystack_netstack);
6070Sstevel@tonic-gate 			return (ENOMEM);
6083448Sdh155122 		}
6090Sstevel@tonic-gate 		mutex_init(&kc->kc_lock, NULL, MUTEX_DEFAULT, 0);
6100Sstevel@tonic-gate 		kc->kc_rq = q;
6110Sstevel@tonic-gate 		kc->kc_wq = WR(q);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 		q->q_ptr = kc;
6140Sstevel@tonic-gate 		WR(q)->q_ptr = kc;
6150Sstevel@tonic-gate 
6163448Sdh155122 		kc->kc_keystack = keystack;
6170Sstevel@tonic-gate 		qprocson(q);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		/*
6200Sstevel@tonic-gate 		 * Send down initial message to whatever I was pushed on top
6210Sstevel@tonic-gate 		 * of asking for its consumer type.  The reply will set it.
6220Sstevel@tonic-gate 		 */
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		/* Allocate it. */
6250Sstevel@tonic-gate 		mp = allocb(sizeof (ipsec_info_t), BPRI_HI);
6260Sstevel@tonic-gate 		if (mp == NULL) {
6273448Sdh155122 			ks1dbg(keystack, (
6280Sstevel@tonic-gate 			    "keysock_open:  Cannot allocate KEYSOCK_HELLO.\n"));
6290Sstevel@tonic-gate 			/* Do I need to set these to null? */
6300Sstevel@tonic-gate 			q->q_ptr = NULL;
6310Sstevel@tonic-gate 			WR(q)->q_ptr = NULL;
6320Sstevel@tonic-gate 			mutex_destroy(&kc->kc_lock);
6330Sstevel@tonic-gate 			kmem_free(kc, sizeof (*kc));
6343448Sdh155122 			netstack_rele(keystack->keystack_netstack);
6350Sstevel@tonic-gate 			return (ENOMEM);
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 		/* If I allocated okay, putnext to what I was pushed atop. */
6390Sstevel@tonic-gate 		mp->b_wptr += sizeof (ipsec_info_t);
6400Sstevel@tonic-gate 		mp->b_datap->db_type = M_CTL;
6410Sstevel@tonic-gate 		ii = (ipsec_info_t *)mp->b_rptr;
6420Sstevel@tonic-gate 		ii->ipsec_info_type = KEYSOCK_HELLO;
6430Sstevel@tonic-gate 		/* Length only of type/len. */
6440Sstevel@tonic-gate 		ii->ipsec_info_len = sizeof (ii->ipsec_allu);
6453448Sdh155122 		ks2dbg(keystack, ("Ready to putnext KEYSOCK_HELLO.\n"));
6460Sstevel@tonic-gate 		putnext(kc->kc_wq, mp);
6470Sstevel@tonic-gate 	} else {
6480Sstevel@tonic-gate 		minor_t ksminor;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 		/* Initialize keysock state. */
6510Sstevel@tonic-gate 
6523448Sdh155122 		ks2dbg(keystack, ("Made it into PF_KEY socket open.\n"));
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		ksminor = (minor_t)(uintptr_t)
6550Sstevel@tonic-gate 		    vmem_alloc(keysock_vmem, 1, VM_NOSLEEP);
6563448Sdh155122 		if (ksminor == 0) {
6573448Sdh155122 			netstack_rele(keystack->keystack_netstack);
6580Sstevel@tonic-gate 			return (ENOMEM);
6593448Sdh155122 		}
6600Sstevel@tonic-gate 		ks = kmem_zalloc(sizeof (keysock_t), KM_NOSLEEP);
6610Sstevel@tonic-gate 		if (ks == NULL) {
6620Sstevel@tonic-gate 			vmem_free(keysock_vmem, (void *)(uintptr_t)ksminor, 1);
6633448Sdh155122 			netstack_rele(keystack->keystack_netstack);
6640Sstevel@tonic-gate 			return (ENOMEM);
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 		mutex_init(&ks->keysock_lock, NULL, MUTEX_DEFAULT, 0);
6680Sstevel@tonic-gate 		ks->keysock_rq = q;
6690Sstevel@tonic-gate 		ks->keysock_wq = WR(q);
6700Sstevel@tonic-gate 		ks->keysock_state = TS_UNBND;
6710Sstevel@tonic-gate 		ks->keysock_serial = ksminor;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		q->q_ptr = ks;
6740Sstevel@tonic-gate 		WR(q)->q_ptr = ks;
6753448Sdh155122 		ks->keysock_keystack = keystack;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		/*
6780Sstevel@tonic-gate 		 * The receive hiwat is only looked at on the stream head
6790Sstevel@tonic-gate 		 * queue.  Store in q_hiwat in order to return on SO_RCVBUF
6800Sstevel@tonic-gate 		 * getsockopts.
6810Sstevel@tonic-gate 		 */
6820Sstevel@tonic-gate 
6833448Sdh155122 		q->q_hiwat = keystack->keystack_recv_hiwat;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		/*
6860Sstevel@tonic-gate 		 * The transmit hiwat/lowat is only looked at on IP's queue.
6870Sstevel@tonic-gate 		 * Store in q_hiwat/q_lowat in order to return on
6880Sstevel@tonic-gate 		 * SO_SNDBUF/SO_SNDLOWAT getsockopts.
6890Sstevel@tonic-gate 		 */
6900Sstevel@tonic-gate 
6913448Sdh155122 		WR(q)->q_hiwat = keystack->keystack_xmit_hiwat;
6923448Sdh155122 		WR(q)->q_lowat = keystack->keystack_xmit_lowat;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		*devp = makedevice(getmajor(*devp), ksminor);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		/*
6970Sstevel@tonic-gate 		 * Thread keysock into the global keysock list.
6980Sstevel@tonic-gate 		 */
6993448Sdh155122 		mutex_enter(&keystack->keystack_list_lock);
7003448Sdh155122 		ks->keysock_next = keystack->keystack_list;
7013448Sdh155122 		ks->keysock_ptpn = &keystack->keystack_list;
7023448Sdh155122 		if (keystack->keystack_list != NULL) {
7033448Sdh155122 			keystack->keystack_list->keysock_ptpn =
7043448Sdh155122 			    &ks->keysock_next;
7053448Sdh155122 		}
7063448Sdh155122 		keystack->keystack_list = ks;
7073448Sdh155122 		mutex_exit(&keystack->keystack_list_lock);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		qprocson(q);
7108348SEric.Yu@Sun.COM 		(void) proto_set_rx_hiwat(q, NULL,
7118348SEric.Yu@Sun.COM 		    keystack->keystack_recv_hiwat);
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * Wait outside the keysock module perimeter for IPsec
7140Sstevel@tonic-gate 		 * plumbing to be completed.  If it fails, keysock_close()
7150Sstevel@tonic-gate 		 * undoes everything we just did.
7160Sstevel@tonic-gate 		 */
7173448Sdh155122 		if (!ipsec_loader_wait(q,
7183448Sdh155122 		    keystack->keystack_netstack->netstack_ipsec)) {
7190Sstevel@tonic-gate 			(void) keysock_close(q);
7200Sstevel@tonic-gate 			return (EPFNOSUPPORT);
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	return (0);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_wput(). */
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * Copy relevant state bits.
7310Sstevel@tonic-gate  */
7320Sstevel@tonic-gate static void
keysock_copy_info(struct T_info_ack * tap,keysock_t * ks)7330Sstevel@tonic-gate keysock_copy_info(struct T_info_ack *tap, keysock_t *ks)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate 	*tap = keysock_g_t_info_ack;
7360Sstevel@tonic-gate 	tap->CURRENT_state = ks->keysock_state;
7370Sstevel@tonic-gate 	tap->OPT_size = keysock_max_optsize;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate /*
7410Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
7420Sstevel@tonic-gate  * keysock_wput.  Much of the T_CAPABILITY_ACK information is copied from
7430Sstevel@tonic-gate  * keysock_g_t_info_ack.  The current state of the stream is copied from
7440Sstevel@tonic-gate  * keysock_state.
7450Sstevel@tonic-gate  */
7460Sstevel@tonic-gate static void
keysock_capability_req(queue_t * q,mblk_t * mp)7470Sstevel@tonic-gate keysock_capability_req(queue_t *q, mblk_t *mp)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
7500Sstevel@tonic-gate 	t_uscalar_t cap_bits1;
7510Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
7565240Snordmark 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
7570Sstevel@tonic-gate 	if (mp == NULL)
7580Sstevel@tonic-gate 		return;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)mp->b_rptr;
7610Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
7640Sstevel@tonic-gate 		keysock_copy_info(&tcap->INFO_ack, ks);
7650Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	qreply(q, mp);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate /*
7720Sstevel@tonic-gate  * This routine responds to T_INFO_REQ messages. It is called by
7730Sstevel@tonic-gate  * keysock_wput_other.
7740Sstevel@tonic-gate  * Most of the T_INFO_ACK information is copied from keysock_g_t_info_ack.
7750Sstevel@tonic-gate  * The current state of the stream is copied from keysock_state.
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate static void
keysock_info_req(q,mp)7780Sstevel@tonic-gate keysock_info_req(q, mp)
7790Sstevel@tonic-gate 	queue_t	*q;
7800Sstevel@tonic-gate 	mblk_t	*mp;
7810Sstevel@tonic-gate {
7820Sstevel@tonic-gate 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
7830Sstevel@tonic-gate 	    T_INFO_ACK);
7840Sstevel@tonic-gate 	if (mp == NULL)
7850Sstevel@tonic-gate 		return;
7860Sstevel@tonic-gate 	keysock_copy_info((struct T_info_ack *)mp->b_rptr,
7870Sstevel@tonic-gate 	    (keysock_t *)q->q_ptr);
7880Sstevel@tonic-gate 	qreply(q, mp);
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate  * keysock_err_ack. This routine creates a
7930Sstevel@tonic-gate  * T_ERROR_ACK message and passes it
7940Sstevel@tonic-gate  * upstream.
7950Sstevel@tonic-gate  */
7960Sstevel@tonic-gate static void
keysock_err_ack(q,mp,t_error,sys_error)7970Sstevel@tonic-gate keysock_err_ack(q, mp, t_error, sys_error)
7980Sstevel@tonic-gate 	queue_t	*q;
7990Sstevel@tonic-gate 	mblk_t	*mp;
8000Sstevel@tonic-gate 	int	t_error;
8010Sstevel@tonic-gate 	int	sys_error;
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
8040Sstevel@tonic-gate 		qreply(q, mp);
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate /*
8080Sstevel@tonic-gate  * This routine retrieves the current status of socket options.
8090Sstevel@tonic-gate  * It returns the size of the option retrieved.
8100Sstevel@tonic-gate  */
8110Sstevel@tonic-gate /* ARGSUSED */
8120Sstevel@tonic-gate int
keysock_opt_get(queue_t * q,int level,int name,uchar_t * ptr)8130Sstevel@tonic-gate keysock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	int *i1 = (int *)ptr;
8160Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	switch (level) {
8190Sstevel@tonic-gate 	case SOL_SOCKET:
8200Sstevel@tonic-gate 		mutex_enter(&ks->keysock_lock);
8210Sstevel@tonic-gate 		switch (name) {
8220Sstevel@tonic-gate 		case SO_TYPE:
8230Sstevel@tonic-gate 			*i1 = SOCK_RAW;
8240Sstevel@tonic-gate 			break;
8250Sstevel@tonic-gate 		case SO_USELOOPBACK:
8260Sstevel@tonic-gate 			*i1 = (int)(!((ks->keysock_flags & KEYSOCK_NOLOOP) ==
8270Sstevel@tonic-gate 			    KEYSOCK_NOLOOP));
8280Sstevel@tonic-gate 			break;
8290Sstevel@tonic-gate 		/*
8300Sstevel@tonic-gate 		 * The following two items can be manipulated,
8310Sstevel@tonic-gate 		 * but changing them should do nothing.
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		case SO_SNDBUF:
8340Sstevel@tonic-gate 			*i1 = (int)q->q_hiwat;
8350Sstevel@tonic-gate 			break;
8360Sstevel@tonic-gate 		case SO_RCVBUF:
8370Sstevel@tonic-gate 			*i1 = (int)(RD(q)->q_hiwat);
8380Sstevel@tonic-gate 			break;
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 		mutex_exit(&ks->keysock_lock);
8410Sstevel@tonic-gate 		break;
8420Sstevel@tonic-gate 	default:
8430Sstevel@tonic-gate 		return (0);
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 	return (sizeof (int));
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate /*
8490Sstevel@tonic-gate  * This routine sets socket options.
8500Sstevel@tonic-gate  */
8510Sstevel@tonic-gate /* ARGSUSED */
8520Sstevel@tonic-gate int
keysock_opt_set(queue_t * q,uint_t mgmt_flags,int level,int name,uint_t inlen,uchar_t * invalp,uint_t * outlenp,uchar_t * outvalp,void * thisdg_attrs,cred_t * cr)8530Sstevel@tonic-gate keysock_opt_set(queue_t *q, uint_t mgmt_flags, int level,
8540Sstevel@tonic-gate     int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
855*11042SErik.Nordmark@Sun.COM     uchar_t *outvalp, void *thisdg_attrs, cred_t *cr)
8560Sstevel@tonic-gate {
8578730Sdanmcd@sun.com 	int *i1 = (int *)invalp, errno = 0;
8580Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
8593448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	switch (level) {
8620Sstevel@tonic-gate 	case SOL_SOCKET:
8630Sstevel@tonic-gate 		mutex_enter(&ks->keysock_lock);
8640Sstevel@tonic-gate 		switch (name) {
8650Sstevel@tonic-gate 		case SO_USELOOPBACK:
8660Sstevel@tonic-gate 			if (!(*i1))
8670Sstevel@tonic-gate 				ks->keysock_flags |= KEYSOCK_NOLOOP;
8680Sstevel@tonic-gate 			else ks->keysock_flags &= ~KEYSOCK_NOLOOP;
8690Sstevel@tonic-gate 			break;
8700Sstevel@tonic-gate 		case SO_SNDBUF:
8713448Sdh155122 			if (*i1 > keystack->keystack_max_buf)
8728730Sdanmcd@sun.com 				errno = ENOBUFS;
8738730Sdanmcd@sun.com 			else q->q_hiwat = *i1;
8740Sstevel@tonic-gate 			break;
8750Sstevel@tonic-gate 		case SO_RCVBUF:
8768730Sdanmcd@sun.com 			if (*i1 > keystack->keystack_max_buf) {
8778730Sdanmcd@sun.com 				errno = ENOBUFS;
8788730Sdanmcd@sun.com 			} else {
8798730Sdanmcd@sun.com 				RD(q)->q_hiwat = *i1;
8808730Sdanmcd@sun.com 				(void) proto_set_rx_hiwat(RD(q), NULL, *i1);
8818730Sdanmcd@sun.com 			}
8820Sstevel@tonic-gate 			break;
8838730Sdanmcd@sun.com 		default:
8848730Sdanmcd@sun.com 			errno = EINVAL;
8850Sstevel@tonic-gate 		}
8860Sstevel@tonic-gate 		mutex_exit(&ks->keysock_lock);
8870Sstevel@tonic-gate 		break;
8888730Sdanmcd@sun.com 	default:
8898730Sdanmcd@sun.com 		errno = EINVAL;
8900Sstevel@tonic-gate 	}
8918730Sdanmcd@sun.com 	return (errno);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*
8950Sstevel@tonic-gate  * Handle STREAMS messages.
8960Sstevel@tonic-gate  */
8970Sstevel@tonic-gate static void
keysock_wput_other(queue_t * q,mblk_t * mp)8980Sstevel@tonic-gate keysock_wput_other(queue_t *q, mblk_t *mp)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate 	struct iocblk *iocp;
9010Sstevel@tonic-gate 	int error;
9023448Sdh155122 	keysock_t *ks = (keysock_t *)q->q_ptr;
9033448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
9043448Sdh155122 	cred_t		*cr;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
9070Sstevel@tonic-gate 	case M_PROTO:
9080Sstevel@tonic-gate 	case M_PCPROTO:
9090Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
9103448Sdh155122 			ks3dbg(keystack, (
9110Sstevel@tonic-gate 			    "keysock_wput_other: Not big enough M_PROTO\n"));
9120Sstevel@tonic-gate 			freemsg(mp);
9130Sstevel@tonic-gate 			return;
9140Sstevel@tonic-gate 		}
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);
9183448Sdh155122 			break;
9190Sstevel@tonic-gate 		case T_INFO_REQ:
9200Sstevel@tonic-gate 			keysock_info_req(q, mp);
9213448Sdh155122 			break;
9220Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
9230Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
9248778SErik.Nordmark@Sun.COM 			/*
9258778SErik.Nordmark@Sun.COM 			 * All Solaris components should pass a db_credp
9268778SErik.Nordmark@Sun.COM 			 * for this TPI message, hence we ASSERT.
9278778SErik.Nordmark@Sun.COM 			 * But in case there is some other M_PROTO that looks
9288778SErik.Nordmark@Sun.COM 			 * like a TPI message sent by some other kernel
9298778SErik.Nordmark@Sun.COM 			 * component, we check and return an error.
9308778SErik.Nordmark@Sun.COM 			 */
9318778SErik.Nordmark@Sun.COM 			cr = msg_getcred(mp, NULL);
9328778SErik.Nordmark@Sun.COM 			ASSERT(cr != NULL);
9338778SErik.Nordmark@Sun.COM 			if (cr == NULL) {
9348778SErik.Nordmark@Sun.COM 				keysock_err_ack(q, mp, TSYSERR, EINVAL);
9358778SErik.Nordmark@Sun.COM 				return;
9368778SErik.Nordmark@Sun.COM 			}
9378778SErik.Nordmark@Sun.COM 			if (((union T_primitives *)mp->b_rptr)->type ==
9388778SErik.Nordmark@Sun.COM 			    T_SVR4_OPTMGMT_REQ) {
939*11042SErik.Nordmark@Sun.COM 				svr4_optcom_req(q, mp, cr, &keysock_opt_obj);
9408778SErik.Nordmark@Sun.COM 			} else {
941*11042SErik.Nordmark@Sun.COM 				tpi_optcom_req(q, mp, cr, &keysock_opt_obj);
9428778SErik.Nordmark@Sun.COM 			}
9433448Sdh155122 			break;
9440Sstevel@tonic-gate 		case T_DATA_REQ:
9450Sstevel@tonic-gate 		case T_EXDATA_REQ:
9460Sstevel@tonic-gate 		case T_ORDREL_REQ:
9470Sstevel@tonic-gate 			/* Illegal for keysock. */
9480Sstevel@tonic-gate 			freemsg(mp);
9490Sstevel@tonic-gate 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
9503448Sdh155122 			break;
9510Sstevel@tonic-gate 		default:
9520Sstevel@tonic-gate 			/* Not supported by keysock. */
9530Sstevel@tonic-gate 			keysock_err_ack(q, mp, TNOTSUPPORT, 0);
9543448Sdh155122 			break;
9550Sstevel@tonic-gate 		}
9563448Sdh155122 		return;
9570Sstevel@tonic-gate 	case M_IOCTL:
9580Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
9590Sstevel@tonic-gate 		error = EINVAL;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
9620Sstevel@tonic-gate 		case ND_SET:
9630Sstevel@tonic-gate 		case ND_GET:
9643448Sdh155122 			if (nd_getset(q, keystack->keystack_g_nd, mp)) {
9650Sstevel@tonic-gate 				qreply(q, mp);
9660Sstevel@tonic-gate 				return;
9670Sstevel@tonic-gate 			} else
9680Sstevel@tonic-gate 				error = ENOENT;
9690Sstevel@tonic-gate 			/* FALLTHRU */
9700Sstevel@tonic-gate 		default:
9710Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
9720Sstevel@tonic-gate 			return;
9730Sstevel@tonic-gate 		}
9740Sstevel@tonic-gate 	case M_FLUSH:
9750Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
9760Sstevel@tonic-gate 			flushq(q, FLUSHALL);
9770Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
9780Sstevel@tonic-gate 		}
9790Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
9800Sstevel@tonic-gate 			qreply(q, mp);
9810Sstevel@tonic-gate 			return;
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 		/* Else FALLTHRU */
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	/* If fell through, just black-hole the message. */
9870Sstevel@tonic-gate 	freemsg(mp);
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate /*
9910Sstevel@tonic-gate  * Transmit a PF_KEY error message to the instance either pointed to
9920Sstevel@tonic-gate  * by ks, the instance with serial number serial, or more, depending.
9930Sstevel@tonic-gate  *
9940Sstevel@tonic-gate  * The faulty message (or a reasonable facsimile thereof) is in mp.
9950Sstevel@tonic-gate  * This function will free mp or recycle it for delivery, thereby causing
9960Sstevel@tonic-gate  * the stream head to free it.
9970Sstevel@tonic-gate  */
9980Sstevel@tonic-gate static void
keysock_error(keysock_t * ks,mblk_t * mp,int error,int diagnostic)9990Sstevel@tonic-gate keysock_error(keysock_t *ks, mblk_t *mp, int error, int diagnostic)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr;
10023448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_DATA);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (samsg->sadb_msg_type < SADB_GETSPI ||
10070Sstevel@tonic-gate 	    samsg->sadb_msg_type > SADB_MAX)
10080Sstevel@tonic-gate 		samsg->sadb_msg_type = SADB_RESERVED;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/*
10110Sstevel@tonic-gate 	 * Strip out extension headers.
10120Sstevel@tonic-gate 	 */
10130Sstevel@tonic-gate 	ASSERT(mp->b_rptr + sizeof (*samsg) <= mp->b_datap->db_lim);
10140Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + sizeof (*samsg);
10150Sstevel@tonic-gate 	samsg->sadb_msg_len = SADB_8TO64(sizeof (sadb_msg_t));
10160Sstevel@tonic-gate 	samsg->sadb_msg_errno = (uint8_t)error;
10170Sstevel@tonic-gate 	samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic;
10180Sstevel@tonic-gate 
10193448Sdh155122 	keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE, keystack);
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate /*
10230Sstevel@tonic-gate  * Pass down a message to a consumer.  Wrap it in KEYSOCK_IN, and copy
10240Sstevel@tonic-gate  * in the extv if passed in.
10250Sstevel@tonic-gate  */
10260Sstevel@tonic-gate static void
keysock_passdown(keysock_t * ks,mblk_t * mp,uint8_t satype,sadb_ext_t * extv[],boolean_t flushmsg)10270Sstevel@tonic-gate keysock_passdown(keysock_t *ks, mblk_t *mp, uint8_t satype, sadb_ext_t *extv[],
10280Sstevel@tonic-gate     boolean_t flushmsg)
10290Sstevel@tonic-gate {
10300Sstevel@tonic-gate 	keysock_consumer_t *kc;
10310Sstevel@tonic-gate 	mblk_t *wrapper;
10320Sstevel@tonic-gate 	keysock_in_t *ksi;
10330Sstevel@tonic-gate 	int i;
10343448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	wrapper = allocb(sizeof (ipsec_info_t), BPRI_HI);
10370Sstevel@tonic-gate 	if (wrapper == NULL) {
10383448Sdh155122 		ks3dbg(keystack, ("keysock_passdown: allocb failed.\n"));
10390Sstevel@tonic-gate 		if (extv[SADB_EXT_KEY_ENCRYPT] != NULL)
10400Sstevel@tonic-gate 			bzero(extv[SADB_EXT_KEY_ENCRYPT],
10410Sstevel@tonic-gate 			    SADB_64TO8(
10425240Snordmark 			    extv[SADB_EXT_KEY_ENCRYPT]->sadb_ext_len));
10430Sstevel@tonic-gate 		if (extv[SADB_EXT_KEY_AUTH] != NULL)
10440Sstevel@tonic-gate 			bzero(extv[SADB_EXT_KEY_AUTH],
10450Sstevel@tonic-gate 			    SADB_64TO8(
10465240Snordmark 			    extv[SADB_EXT_KEY_AUTH]->sadb_ext_len));
10470Sstevel@tonic-gate 		if (flushmsg) {
10480Sstevel@tonic-gate 			ks0dbg((
10490Sstevel@tonic-gate 			    "keysock: Downwards flush/dump message failed!\n"));
10500Sstevel@tonic-gate 			/* If this is true, I hold the perimeter. */
10513448Sdh155122 			keystack->keystack_flushdump--;
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 		freemsg(mp);
10540Sstevel@tonic-gate 		return;
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	wrapper->b_datap->db_type = M_CTL;
10580Sstevel@tonic-gate 	ksi = (keysock_in_t *)wrapper->b_rptr;
10590Sstevel@tonic-gate 	ksi->ks_in_type = KEYSOCK_IN;
10600Sstevel@tonic-gate 	ksi->ks_in_len = sizeof (keysock_in_t);
10610Sstevel@tonic-gate 	if (extv[SADB_EXT_ADDRESS_SRC] != NULL)
10620Sstevel@tonic-gate 		ksi->ks_in_srctype = KS_IN_ADDR_UNKNOWN;
10630Sstevel@tonic-gate 	else ksi->ks_in_srctype = KS_IN_ADDR_NOTTHERE;
10640Sstevel@tonic-gate 	if (extv[SADB_EXT_ADDRESS_DST] != NULL)
10650Sstevel@tonic-gate 		ksi->ks_in_dsttype = KS_IN_ADDR_UNKNOWN;
10660Sstevel@tonic-gate 	else ksi->ks_in_dsttype = KS_IN_ADDR_NOTTHERE;
10670Sstevel@tonic-gate 	for (i = 0; i <= SADB_EXT_MAX; i++)
10680Sstevel@tonic-gate 		ksi->ks_in_extv[i] = extv[i];
10690Sstevel@tonic-gate 	ksi->ks_in_serial = ks->keysock_serial;
10700Sstevel@tonic-gate 	wrapper->b_wptr += sizeof (ipsec_info_t);
10710Sstevel@tonic-gate 	wrapper->b_cont = mp;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	/*
10740Sstevel@tonic-gate 	 * Find the appropriate consumer where the message is passed down.
10750Sstevel@tonic-gate 	 */
10763448Sdh155122 	kc = keystack->keystack_consumers[satype];
10770Sstevel@tonic-gate 	if (kc == NULL) {
10780Sstevel@tonic-gate 		freeb(wrapper);
10790Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE);
10800Sstevel@tonic-gate 		if (flushmsg) {
10810Sstevel@tonic-gate 			ks0dbg((
10820Sstevel@tonic-gate 			    "keysock: Downwards flush/dump message failed!\n"));
10830Sstevel@tonic-gate 			/* If this is true, I hold the perimeter. */
10843448Sdh155122 			keystack->keystack_flushdump--;
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 		return;
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	/*
10900Sstevel@tonic-gate 	 * NOTE: There used to be code in here to spin while a flush or
10910Sstevel@tonic-gate 	 *	 dump finished.  Keysock now assumes that consumers have enough
10920Sstevel@tonic-gate 	 *	 MT-savviness to deal with that.
10930Sstevel@tonic-gate 	 */
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	/*
10960Sstevel@tonic-gate 	 * Current consumers (AH and ESP) are guaranteed to return a
10970Sstevel@tonic-gate 	 * FLUSH or DUMP message back, so when we reach here, we don't
10980Sstevel@tonic-gate 	 * have to worry about keysock_flushdumps.
10990Sstevel@tonic-gate 	 */
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	putnext(kc->kc_wq, wrapper);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate  * High-level reality checking of extensions.
11060Sstevel@tonic-gate  */
11070Sstevel@tonic-gate static boolean_t
ext_check(sadb_ext_t * ext,keysock_stack_t * keystack)11083448Sdh155122 ext_check(sadb_ext_t *ext, keysock_stack_t *keystack)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	int i;
11110Sstevel@tonic-gate 	uint64_t *lp;
11120Sstevel@tonic-gate 	sadb_ident_t *id;
11130Sstevel@tonic-gate 	char *idstr;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	switch (ext->sadb_ext_type) {
11160Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_SRC:
11170Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_DST:
11183055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
11193055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
11200Sstevel@tonic-gate 		/* Check for at least enough addtl length for a sockaddr. */
11210Sstevel@tonic-gate 		if (ext->sadb_ext_len <= SADB_8TO64(sizeof (sadb_address_t)))
11220Sstevel@tonic-gate 			return (B_FALSE);
11230Sstevel@tonic-gate 		break;
11240Sstevel@tonic-gate 	case SADB_EXT_LIFETIME_HARD:
11250Sstevel@tonic-gate 	case SADB_EXT_LIFETIME_SOFT:
11260Sstevel@tonic-gate 	case SADB_EXT_LIFETIME_CURRENT:
11270Sstevel@tonic-gate 		if (ext->sadb_ext_len != SADB_8TO64(sizeof (sadb_lifetime_t)))
11280Sstevel@tonic-gate 			return (B_FALSE);
11290Sstevel@tonic-gate 		break;
11300Sstevel@tonic-gate 	case SADB_EXT_SPIRANGE:
11310Sstevel@tonic-gate 		/* See if the SPI range is legit. */
11320Sstevel@tonic-gate 		if (htonl(((sadb_spirange_t *)ext)->sadb_spirange_min) >
11330Sstevel@tonic-gate 		    htonl(((sadb_spirange_t *)ext)->sadb_spirange_max))
11340Sstevel@tonic-gate 			return (B_FALSE);
11350Sstevel@tonic-gate 		break;
11360Sstevel@tonic-gate 	case SADB_EXT_KEY_AUTH:
11370Sstevel@tonic-gate 	case SADB_EXT_KEY_ENCRYPT:
11380Sstevel@tonic-gate 		/* Key length check. */
11390Sstevel@tonic-gate 		if (((sadb_key_t *)ext)->sadb_key_bits == 0)
11400Sstevel@tonic-gate 			return (B_FALSE);
11410Sstevel@tonic-gate 		/*
11420Sstevel@tonic-gate 		 * Check to see if the key length (in bits) is less than the
11430Sstevel@tonic-gate 		 * extension length (in 8-bits words).
11440Sstevel@tonic-gate 		 */
11450Sstevel@tonic-gate 		if ((roundup(SADB_1TO8(((sadb_key_t *)ext)->sadb_key_bits), 8) +
11460Sstevel@tonic-gate 		    sizeof (sadb_key_t)) != SADB_64TO8(ext->sadb_ext_len)) {
11473448Sdh155122 			ks1dbg(keystack, (
11480Sstevel@tonic-gate 			    "ext_check:  Key bits/length inconsistent.\n"));
11493448Sdh155122 			ks1dbg(keystack, ("%d bits, len is %d bytes.\n",
11500Sstevel@tonic-gate 			    ((sadb_key_t *)ext)->sadb_key_bits,
11510Sstevel@tonic-gate 			    SADB_64TO8(ext->sadb_ext_len)));
11520Sstevel@tonic-gate 			return (B_FALSE);
11530Sstevel@tonic-gate 		}
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		/* All-zeroes key check. */
11560Sstevel@tonic-gate 		lp = (uint64_t *)(((char *)ext) + sizeof (sadb_key_t));
11570Sstevel@tonic-gate 		for (i = 0;
11580Sstevel@tonic-gate 		    i < (ext->sadb_ext_len - SADB_8TO64(sizeof (sadb_key_t)));
11590Sstevel@tonic-gate 		    i++)
11600Sstevel@tonic-gate 			if (lp[i] != 0)
11610Sstevel@tonic-gate 				break;	/* Out of for loop. */
11620Sstevel@tonic-gate 		/* If finished the loop naturally, it's an all zero key. */
11630Sstevel@tonic-gate 		if (lp[i] == 0)
11640Sstevel@tonic-gate 			return (B_FALSE);
11650Sstevel@tonic-gate 		break;
11660Sstevel@tonic-gate 	case SADB_EXT_IDENTITY_SRC:
11670Sstevel@tonic-gate 	case SADB_EXT_IDENTITY_DST:
11680Sstevel@tonic-gate 		/*
11690Sstevel@tonic-gate 		 * Make sure the strings in these identities are
11700Sstevel@tonic-gate 		 * null-terminated.  RFC 2367 underspecified how to handle
11710Sstevel@tonic-gate 		 * such a case.  I "proactively" null-terminate the string
11720Sstevel@tonic-gate 		 * at the last byte if it's not terminated sooner.
11730Sstevel@tonic-gate 		 */
11740Sstevel@tonic-gate 		id = (sadb_ident_t *)ext;
11750Sstevel@tonic-gate 		i = SADB_64TO8(id->sadb_ident_len);
11760Sstevel@tonic-gate 		i -= sizeof (sadb_ident_t);
11770Sstevel@tonic-gate 		idstr = (char *)(id + 1);
11780Sstevel@tonic-gate 		while (*idstr != '\0' && i > 0) {
11790Sstevel@tonic-gate 			i--;
11800Sstevel@tonic-gate 			idstr++;
11810Sstevel@tonic-gate 		}
11820Sstevel@tonic-gate 		if (i == 0) {
11830Sstevel@tonic-gate 			/*
11840Sstevel@tonic-gate 			 * I.e., if the bozo user didn't NULL-terminate the
11850Sstevel@tonic-gate 			 * string...
11860Sstevel@tonic-gate 			 */
11870Sstevel@tonic-gate 			idstr--;
11880Sstevel@tonic-gate 			*idstr = '\0';
11890Sstevel@tonic-gate 		}
11900Sstevel@tonic-gate 		break;
11910Sstevel@tonic-gate 	}
11920Sstevel@tonic-gate 	return (B_TRUE);	/* For now... */
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate /* Return values for keysock_get_ext(). */
11960Sstevel@tonic-gate #define	KGE_OK	0
11970Sstevel@tonic-gate #define	KGE_DUP	1
11980Sstevel@tonic-gate #define	KGE_UNK	2
11990Sstevel@tonic-gate #define	KGE_LEN	3
12000Sstevel@tonic-gate #define	KGE_CHK	4
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate /*
12030Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
12040Sstevel@tonic-gate  * Return values include:
12050Sstevel@tonic-gate  *
12060Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
12070Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
12080Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
12090Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
12100Sstevel@tonic-gate  *		extv[0].
12110Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
12120Sstevel@tonic-gate  *		unknown header.
12130Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
12140Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
12150Sstevel@tonic-gate  *
12160Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
12170Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
12180Sstevel@tonic-gate  */
12190Sstevel@tonic-gate static int
keysock_get_ext(sadb_ext_t * extv[],sadb_msg_t * basehdr,uint_t msgsize,keysock_stack_t * keystack)12203448Sdh155122 keysock_get_ext(sadb_ext_t *extv[], sadb_msg_t *basehdr, uint_t msgsize,
12213448Sdh155122     keysock_stack_t *keystack)
12220Sstevel@tonic-gate {
12230Sstevel@tonic-gate 	bzero(extv, sizeof (sadb_ext_t *) * (SADB_EXT_MAX + 1));
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	extv[0] = (sadb_ext_t *)(basehdr + 1);
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	while (extv[0] < (sadb_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
12300Sstevel@tonic-gate 		/* Check for unknown headers. */
12310Sstevel@tonic-gate 		if (extv[0]->sadb_ext_type == 0 ||
12320Sstevel@tonic-gate 		    extv[0]->sadb_ext_type > SADB_EXT_MAX)
12330Sstevel@tonic-gate 			return (KGE_UNK);
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 		/*
12360Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
12370Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
12380Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
12390Sstevel@tonic-gate 		 */
12400Sstevel@tonic-gate 		if (extv[0]->sadb_ext_len == 0 ||
12410Sstevel@tonic-gate 		    (void *)((uint64_t *)extv[0] + extv[0]->sadb_ext_len) >
12420Sstevel@tonic-gate 		    (void *)((uint8_t *)basehdr + msgsize))
12430Sstevel@tonic-gate 			return (KGE_LEN);
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 		/* Check for redundant headers. */
12460Sstevel@tonic-gate 		if (extv[extv[0]->sadb_ext_type] != NULL)
12470Sstevel@tonic-gate 			return (KGE_DUP);
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 		/*
12500Sstevel@tonic-gate 		 * Reality check the extension if possible at the keysock
12510Sstevel@tonic-gate 		 * level.
12520Sstevel@tonic-gate 		 */
12533448Sdh155122 		if (!ext_check(extv[0], keystack))
12540Sstevel@tonic-gate 			return (KGE_CHK);
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
12570Sstevel@tonic-gate 		extv[extv[0]->sadb_ext_type] = extv[0];
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
12600Sstevel@tonic-gate 		extv[0] = (sadb_ext_t *)
12610Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->sadb_ext_len);
12620Sstevel@tonic-gate 	}
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	/* Everything's cool. */
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	/*
12670Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
12680Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
12690Sstevel@tonic-gate 	 */
12700Sstevel@tonic-gate 	if (extv[0] == (sadb_ext_t *)(basehdr + 1))
12710Sstevel@tonic-gate 		extv[0] = NULL;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	return (KGE_OK);
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate /*
12770Sstevel@tonic-gate  * qwriter() callback to handle flushes and dumps.  This routine will hold
12780Sstevel@tonic-gate  * the inner perimeter.
12790Sstevel@tonic-gate  */
12800Sstevel@tonic-gate void
keysock_do_flushdump(queue_t * q,mblk_t * mp)12810Sstevel@tonic-gate keysock_do_flushdump(queue_t *q, mblk_t *mp)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	int i, start, finish;
12840Sstevel@tonic-gate 	mblk_t *mp1 = NULL;
12850Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
12860Sstevel@tonic-gate 	sadb_ext_t *extv[SADB_EXT_MAX + 1];
12870Sstevel@tonic-gate 	sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr;
12883448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	/*
12910Sstevel@tonic-gate 	 * I am guaranteed this will work.  I did the work in keysock_parse()
12920Sstevel@tonic-gate 	 * already.
12930Sstevel@tonic-gate 	 */
12943448Sdh155122 	(void) keysock_get_ext(extv, samsg, SADB_64TO8(samsg->sadb_msg_len),
12953448Sdh155122 	    keystack);
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	/*
12980Sstevel@tonic-gate 	 * I hold the perimeter, therefore I don't need to use atomic ops.
12990Sstevel@tonic-gate 	 */
13003448Sdh155122 	if (keystack->keystack_flushdump != 0) {
13010Sstevel@tonic-gate 		/* XXX Should I instead use EBUSY? */
13020Sstevel@tonic-gate 		/* XXX Or is there a way to queue these up? */
13030Sstevel@tonic-gate 		keysock_error(ks, mp, ENOMEM, SADB_X_DIAGNOSTIC_NONE);
13040Sstevel@tonic-gate 		return;
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
13080Sstevel@tonic-gate 		start = 0;
13090Sstevel@tonic-gate 		finish = KEYSOCK_MAX_CONSUMERS - 1;
13100Sstevel@tonic-gate 	} else {
13110Sstevel@tonic-gate 		start = samsg->sadb_msg_satype;
13120Sstevel@tonic-gate 		finish = samsg->sadb_msg_satype;
13130Sstevel@tonic-gate 	}
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * Fill up keysock_flushdump with the number of outstanding dumps
13170Sstevel@tonic-gate 	 * and/or flushes.
13180Sstevel@tonic-gate 	 */
13190Sstevel@tonic-gate 
13203448Sdh155122 	keystack->keystack_flushdump_errno = 0;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * Okay, I hold the perimeter.  Eventually keysock_flushdump will
13240Sstevel@tonic-gate 	 * contain the number of consumers with outstanding flush operations.
13250Sstevel@tonic-gate 	 *
13260Sstevel@tonic-gate 	 * SO, here's the plan:
13270Sstevel@tonic-gate 	 *	* For each relevant consumer (Might be one, might be all)
13280Sstevel@tonic-gate 	 *		* Twiddle on the FLUSHING flag.
13290Sstevel@tonic-gate 	 *		* Pass down the FLUSH/DUMP message.
13300Sstevel@tonic-gate 	 *
13310Sstevel@tonic-gate 	 * When I see upbound FLUSH/DUMP messages, I will decrement the
13320Sstevel@tonic-gate 	 * keysock_flushdump.  When I decrement it to 0, I will pass the
13330Sstevel@tonic-gate 	 * FLUSH/DUMP message back up to the PF_KEY sockets.  Because I will
13340Sstevel@tonic-gate 	 * pass down the right SA type to the consumer (either its own, or
13350Sstevel@tonic-gate 	 * that of UNSPEC), the right one will be reflected from each consumer,
13360Sstevel@tonic-gate 	 * and accordingly back to the socket.
13370Sstevel@tonic-gate 	 */
13380Sstevel@tonic-gate 
13393448Sdh155122 	mutex_enter(&keystack->keystack_consumers_lock);
13400Sstevel@tonic-gate 	for (i = start; i <= finish; i++) {
13413448Sdh155122 		if (keystack->keystack_consumers[i] != NULL) {
13420Sstevel@tonic-gate 			mp1 = copymsg(mp);
13430Sstevel@tonic-gate 			if (mp1 == NULL) {
13440Sstevel@tonic-gate 				ks0dbg(("SADB_FLUSH copymsg() failed.\n"));
13450Sstevel@tonic-gate 				/*
13460Sstevel@tonic-gate 				 * Error?  And what about outstanding
13470Sstevel@tonic-gate 				 * flushes?  Oh, yeah, they get sucked up and
13480Sstevel@tonic-gate 				 * the counter is decremented.  Consumers
13490Sstevel@tonic-gate 				 * (see keysock_passdown()) are guaranteed
13500Sstevel@tonic-gate 				 * to deliver back a flush request, even if
13510Sstevel@tonic-gate 				 * it's an error.
13520Sstevel@tonic-gate 				 */
13530Sstevel@tonic-gate 				keysock_error(ks, mp, ENOMEM,
13540Sstevel@tonic-gate 				    SADB_X_DIAGNOSTIC_NONE);
13550Sstevel@tonic-gate 				return;
13560Sstevel@tonic-gate 			}
13570Sstevel@tonic-gate 			/*
13580Sstevel@tonic-gate 			 * Because my entry conditions are met above, the
13590Sstevel@tonic-gate 			 * following assertion should hold true.
13600Sstevel@tonic-gate 			 */
13613448Sdh155122 			mutex_enter(&keystack->keystack_consumers[i]->kc_lock);
13623448Sdh155122 			ASSERT((keystack->keystack_consumers[i]->kc_flags &
13635240Snordmark 			    KC_FLUSHING) == 0);
13643448Sdh155122 			keystack->keystack_consumers[i]->kc_flags |=
13653448Sdh155122 			    KC_FLUSHING;
13663448Sdh155122 			mutex_exit(&(keystack->keystack_consumers[i]->kc_lock));
13670Sstevel@tonic-gate 			/* Always increment the number of flushes... */
13683448Sdh155122 			keystack->keystack_flushdump++;
13690Sstevel@tonic-gate 			/* Guaranteed to return a message. */
13700Sstevel@tonic-gate 			keysock_passdown(ks, mp1, i, extv, B_TRUE);
13710Sstevel@tonic-gate 		} else if (start == finish) {
13720Sstevel@tonic-gate 			/*
13730Sstevel@tonic-gate 			 * In case where start == finish, and there's no
13740Sstevel@tonic-gate 			 * consumer, should we force an error?  Yes.
13750Sstevel@tonic-gate 			 */
13763448Sdh155122 			mutex_exit(&keystack->keystack_consumers_lock);
13770Sstevel@tonic-gate 			keysock_error(ks, mp, EINVAL,
13780Sstevel@tonic-gate 			    SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE);
13790Sstevel@tonic-gate 			return;
13800Sstevel@tonic-gate 		}
13810Sstevel@tonic-gate 	}
13823448Sdh155122 	mutex_exit(&keystack->keystack_consumers_lock);
13830Sstevel@tonic-gate 
13843448Sdh155122 	if (keystack->keystack_flushdump == 0) {
13850Sstevel@tonic-gate 		/*
13860Sstevel@tonic-gate 		 * There were no consumers at all for this message.
13870Sstevel@tonic-gate 		 * XXX For now return ESRCH.
13880Sstevel@tonic-gate 		 */
13890Sstevel@tonic-gate 		keysock_error(ks, mp, ESRCH, SADB_X_DIAGNOSTIC_NO_SADBS);
13900Sstevel@tonic-gate 	} else {
13910Sstevel@tonic-gate 		/* Otherwise, free the original message. */
13920Sstevel@tonic-gate 		freemsg(mp);
13930Sstevel@tonic-gate 	}
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate /*
13970Sstevel@tonic-gate  * Get the right diagnostic for a duplicate.  Should probably use a static
13980Sstevel@tonic-gate  * table lookup.
13990Sstevel@tonic-gate  */
14000Sstevel@tonic-gate int
keysock_duplicate(int ext_type)14010Sstevel@tonic-gate keysock_duplicate(int ext_type)
14020Sstevel@tonic-gate {
14030Sstevel@tonic-gate 	int rc = 0;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	switch (ext_type) {
14060Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_SRC:
14070Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_SRC;
14080Sstevel@tonic-gate 		break;
14090Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_DST:
14100Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_DST;
14110Sstevel@tonic-gate 		break;
14123055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
14133055Sdanmcd 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC;
14143055Sdanmcd 		break;
14153055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
14163055Sdanmcd 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST;
14173055Sdanmcd 		break;
14180Sstevel@tonic-gate 	case SADB_EXT_SA:
14190Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_SA;
14200Sstevel@tonic-gate 		break;
14210Sstevel@tonic-gate 	case SADB_EXT_SPIRANGE:
14220Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_RANGE;
14230Sstevel@tonic-gate 		break;
14240Sstevel@tonic-gate 	case SADB_EXT_KEY_AUTH:
14250Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_AKEY;
14260Sstevel@tonic-gate 		break;
14270Sstevel@tonic-gate 	case SADB_EXT_KEY_ENCRYPT:
14280Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_DUPLICATE_EKEY;
14290Sstevel@tonic-gate 		break;
14300Sstevel@tonic-gate 	}
14310Sstevel@tonic-gate 	return (rc);
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate /*
14350Sstevel@tonic-gate  * Get the right diagnostic for a reality check failure.  Should probably use
14360Sstevel@tonic-gate  * a static table lookup.
14370Sstevel@tonic-gate  */
14380Sstevel@tonic-gate int
keysock_malformed(int ext_type)14390Sstevel@tonic-gate keysock_malformed(int ext_type)
14400Sstevel@tonic-gate {
14410Sstevel@tonic-gate 	int rc = 0;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	switch (ext_type) {
14440Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_SRC:
14450Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_SRC;
14460Sstevel@tonic-gate 		break;
14470Sstevel@tonic-gate 	case SADB_EXT_ADDRESS_DST:
14480Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_DST;
14490Sstevel@tonic-gate 		break;
14503055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
14513055Sdanmcd 		rc = SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC;
14523055Sdanmcd 		break;
14533055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
14543055Sdanmcd 		rc = SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST;
14553055Sdanmcd 		break;
14560Sstevel@tonic-gate 	case SADB_EXT_SA:
14570Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_SA;
14580Sstevel@tonic-gate 		break;
14590Sstevel@tonic-gate 	case SADB_EXT_SPIRANGE:
14600Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_RANGE;
14610Sstevel@tonic-gate 		break;
14620Sstevel@tonic-gate 	case SADB_EXT_KEY_AUTH:
14630Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_AKEY;
14640Sstevel@tonic-gate 		break;
14650Sstevel@tonic-gate 	case SADB_EXT_KEY_ENCRYPT:
14660Sstevel@tonic-gate 		rc = SADB_X_DIAGNOSTIC_MALFORMED_EKEY;
14670Sstevel@tonic-gate 		break;
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 	return (rc);
14700Sstevel@tonic-gate }
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate /*
14730Sstevel@tonic-gate  * Keysock massaging of an inverse ACQUIRE.  Consult policy,
14740Sstevel@tonic-gate  * and construct an appropriate response.
14750Sstevel@tonic-gate  */
14760Sstevel@tonic-gate static void
keysock_inverse_acquire(mblk_t * mp,sadb_msg_t * samsg,sadb_ext_t * extv[],keysock_t * ks)14770Sstevel@tonic-gate keysock_inverse_acquire(mblk_t *mp, sadb_msg_t *samsg, sadb_ext_t *extv[],
14780Sstevel@tonic-gate     keysock_t *ks)
14790Sstevel@tonic-gate {
14800Sstevel@tonic-gate 	mblk_t *reply_mp;
14813448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	/*
14840Sstevel@tonic-gate 	 * Reality check things...
14850Sstevel@tonic-gate 	 */
14860Sstevel@tonic-gate 	if (extv[SADB_EXT_ADDRESS_SRC] == NULL) {
14870Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_MISSING_SRC);
14880Sstevel@tonic-gate 		return;
14890Sstevel@tonic-gate 	}
14900Sstevel@tonic-gate 	if (extv[SADB_EXT_ADDRESS_DST] == NULL) {
14910Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_MISSING_DST);
14923055Sdanmcd 		return;
14933055Sdanmcd 	}
14943055Sdanmcd 
14953055Sdanmcd 	if (extv[SADB_X_EXT_ADDRESS_INNER_SRC] != NULL &&
14963055Sdanmcd 	    extv[SADB_X_EXT_ADDRESS_INNER_DST] == NULL) {
14973055Sdanmcd 		keysock_error(ks, mp, EINVAL,
14983055Sdanmcd 		    SADB_X_DIAGNOSTIC_MISSING_INNER_DST);
14993055Sdanmcd 		return;
15003055Sdanmcd 	}
15013055Sdanmcd 
15023055Sdanmcd 	if (extv[SADB_X_EXT_ADDRESS_INNER_SRC] == NULL &&
15033055Sdanmcd 	    extv[SADB_X_EXT_ADDRESS_INNER_DST] != NULL) {
15043055Sdanmcd 		keysock_error(ks, mp, EINVAL,
15053055Sdanmcd 		    SADB_X_DIAGNOSTIC_MISSING_INNER_SRC);
15063055Sdanmcd 		return;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15093448Sdh155122 	reply_mp = ipsec_construct_inverse_acquire(samsg, extv,
15103448Sdh155122 	    keystack->keystack_netstack);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if (reply_mp != NULL) {
15130Sstevel@tonic-gate 		freemsg(mp);
15140Sstevel@tonic-gate 		keysock_passup(reply_mp, (sadb_msg_t *)reply_mp->b_rptr,
15153448Sdh155122 		    ks->keysock_serial, NULL, B_FALSE, keystack);
15160Sstevel@tonic-gate 	} else {
15170Sstevel@tonic-gate 		keysock_error(ks, mp, samsg->sadb_msg_errno,
15180Sstevel@tonic-gate 		    samsg->sadb_x_msg_diagnostic);
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate /*
15230Sstevel@tonic-gate  * Spew an extended REGISTER down to the relevant consumers.
15240Sstevel@tonic-gate  */
15250Sstevel@tonic-gate static void
keysock_extended_register(keysock_t * ks,mblk_t * mp,sadb_ext_t * extv[])15260Sstevel@tonic-gate keysock_extended_register(keysock_t *ks, mblk_t *mp, sadb_ext_t *extv[])
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	sadb_x_ereg_t *ereg = (sadb_x_ereg_t *)extv[SADB_X_EXT_EREG];
15290Sstevel@tonic-gate 	uint8_t *satypes, *fencepost;
15300Sstevel@tonic-gate 	mblk_t *downmp;
15310Sstevel@tonic-gate 	sadb_ext_t *downextv[SADB_EXT_MAX + 1];
15323448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	if (ks->keysock_registered[0] != 0 || ks->keysock_registered[1] != 0 ||
15350Sstevel@tonic-gate 	    ks->keysock_registered[2] != 0 || ks->keysock_registered[3] != 0) {
15360Sstevel@tonic-gate 		keysock_error(ks, mp, EBUSY, 0);
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	ks->keysock_flags |= KEYSOCK_EXTENDED;
15400Sstevel@tonic-gate 	if (ereg == NULL) {
15410Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
15420Sstevel@tonic-gate 	} else {
15430Sstevel@tonic-gate 		ASSERT(mp->b_rptr + msgdsize(mp) == mp->b_wptr);
15440Sstevel@tonic-gate 		fencepost = (uint8_t *)mp->b_wptr;
15450Sstevel@tonic-gate 		satypes = ereg->sadb_x_ereg_satypes;
15460Sstevel@tonic-gate 		while (*satypes != SADB_SATYPE_UNSPEC && satypes != fencepost) {
15470Sstevel@tonic-gate 			downmp = copymsg(mp);
15480Sstevel@tonic-gate 			if (downmp == NULL) {
15490Sstevel@tonic-gate 				keysock_error(ks, mp, ENOMEM, 0);
15500Sstevel@tonic-gate 				return;
15510Sstevel@tonic-gate 			}
15520Sstevel@tonic-gate 			/*
15530Sstevel@tonic-gate 			 * Since we've made it here, keysock_get_ext will work!
15540Sstevel@tonic-gate 			 */
15550Sstevel@tonic-gate 			(void) keysock_get_ext(downextv,
15563448Sdh155122 			    (sadb_msg_t *)downmp->b_rptr, msgdsize(downmp),
15573448Sdh155122 			    keystack);
15580Sstevel@tonic-gate 			keysock_passdown(ks, downmp, *satypes, downextv,
15590Sstevel@tonic-gate 			    B_FALSE);
15600Sstevel@tonic-gate 			++satypes;
15610Sstevel@tonic-gate 		}
15620Sstevel@tonic-gate 		freemsg(mp);
15630Sstevel@tonic-gate 	}
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	/*
15660Sstevel@tonic-gate 	 * Set global to indicate we prefer an extended ACQUIRE.
15670Sstevel@tonic-gate 	 */
15683448Sdh155122 	atomic_add_32(&keystack->keystack_num_extended, 1);
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate 
15717749SThejaswini.Singarajipura@Sun.COM static void
keysock_delpair_all(keysock_t * ks,mblk_t * mp,sadb_ext_t * extv[])15727749SThejaswini.Singarajipura@Sun.COM keysock_delpair_all(keysock_t *ks, mblk_t *mp, sadb_ext_t *extv[])
15737749SThejaswini.Singarajipura@Sun.COM {
15747749SThejaswini.Singarajipura@Sun.COM 	int i, start, finish;
15757749SThejaswini.Singarajipura@Sun.COM 	mblk_t *mp1 = NULL;
15767749SThejaswini.Singarajipura@Sun.COM 	keysock_stack_t *keystack = ks->keysock_keystack;
15777749SThejaswini.Singarajipura@Sun.COM 
15787749SThejaswini.Singarajipura@Sun.COM 	start = 0;
15797749SThejaswini.Singarajipura@Sun.COM 	finish = KEYSOCK_MAX_CONSUMERS - 1;
15807749SThejaswini.Singarajipura@Sun.COM 
15817749SThejaswini.Singarajipura@Sun.COM 	for (i = start; i <= finish; i++) {
15827749SThejaswini.Singarajipura@Sun.COM 		if (keystack->keystack_consumers[i] != NULL) {
15837749SThejaswini.Singarajipura@Sun.COM 			mp1 = copymsg(mp);
15847749SThejaswini.Singarajipura@Sun.COM 			if (mp1 == NULL) {
15857749SThejaswini.Singarajipura@Sun.COM 				keysock_error(ks, mp, ENOMEM,
15867749SThejaswini.Singarajipura@Sun.COM 				    SADB_X_DIAGNOSTIC_NONE);
15877749SThejaswini.Singarajipura@Sun.COM 				return;
15887749SThejaswini.Singarajipura@Sun.COM 			}
15897749SThejaswini.Singarajipura@Sun.COM 			keysock_passdown(ks, mp1, i, extv, B_FALSE);
15907749SThejaswini.Singarajipura@Sun.COM 		}
15917749SThejaswini.Singarajipura@Sun.COM 	}
15927749SThejaswini.Singarajipura@Sun.COM }
15937749SThejaswini.Singarajipura@Sun.COM 
15940Sstevel@tonic-gate /*
15950Sstevel@tonic-gate  * Handle PF_KEY messages.
15960Sstevel@tonic-gate  */
15970Sstevel@tonic-gate static void
keysock_parse(queue_t * q,mblk_t * mp)15980Sstevel@tonic-gate keysock_parse(queue_t *q, mblk_t *mp)
15990Sstevel@tonic-gate {
16000Sstevel@tonic-gate 	sadb_msg_t *samsg;
16010Sstevel@tonic-gate 	sadb_ext_t *extv[SADB_EXT_MAX + 1];
16020Sstevel@tonic-gate 	keysock_t *ks = (keysock_t *)q->q_ptr;
16030Sstevel@tonic-gate 	uint_t msgsize;
16040Sstevel@tonic-gate 	uint8_t satype;
16053448Sdh155122 	keysock_stack_t	*keystack = ks->keysock_keystack;
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	/* Make sure I'm a PF_KEY socket.  (i.e. nothing's below me) */
16080Sstevel@tonic-gate 	ASSERT(WR(q)->q_next == NULL);
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	samsg = (sadb_msg_t *)mp->b_rptr;
16113448Sdh155122 	ks2dbg(keystack, ("Received possible PF_KEY message, type %d.\n",
16120Sstevel@tonic-gate 	    samsg->sadb_msg_type));
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	msgsize = SADB_64TO8(samsg->sadb_msg_len);
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	if (msgdsize(mp) != msgsize) {
16170Sstevel@tonic-gate 		/*
16180Sstevel@tonic-gate 		 * Message len incorrect w.r.t. actual size.  Send an error
16190Sstevel@tonic-gate 		 * (EMSGSIZE).	It may be necessary to massage things a
16200Sstevel@tonic-gate 		 * bit.	 For example, if the sadb_msg_type is hosed,
16210Sstevel@tonic-gate 		 * I need to set it to SADB_RESERVED to get delivery to
16220Sstevel@tonic-gate 		 * do the right thing.	Then again, maybe just letting
16230Sstevel@tonic-gate 		 * the error delivery do the right thing.
16240Sstevel@tonic-gate 		 */
16253448Sdh155122 		ks2dbg(keystack,
16263448Sdh155122 		    ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
16270Sstevel@tonic-gate 		    msgdsize(mp), msgsize));
16280Sstevel@tonic-gate 		keysock_error(ks, mp, EMSGSIZE, SADB_X_DIAGNOSTIC_NONE);
16290Sstevel@tonic-gate 		return;
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
16330Sstevel@tonic-gate 		/* Get all message into one mblk. */
16340Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
16350Sstevel@tonic-gate 			/*
16360Sstevel@tonic-gate 			 * Something screwy happened.
16370Sstevel@tonic-gate 			 */
16383448Sdh155122 			ks3dbg(keystack,
16393448Sdh155122 			    ("keysock_parse: pullupmsg() failed.\n"));
16400Sstevel@tonic-gate 			return;
16410Sstevel@tonic-gate 		} else {
16420Sstevel@tonic-gate 			samsg = (sadb_msg_t *)mp->b_rptr;
16430Sstevel@tonic-gate 		}
16440Sstevel@tonic-gate 	}
16450Sstevel@tonic-gate 
16463448Sdh155122 	switch (keysock_get_ext(extv, samsg, msgsize, keystack)) {
16470Sstevel@tonic-gate 	case KGE_DUP:
16480Sstevel@tonic-gate 		/* Handle duplicate extension. */
16493448Sdh155122 		ks1dbg(keystack, ("Got duplicate extension of type %d.\n",
16500Sstevel@tonic-gate 		    extv[0]->sadb_ext_type));
16510Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL,
16520Sstevel@tonic-gate 		    keysock_duplicate(extv[0]->sadb_ext_type));
16530Sstevel@tonic-gate 		return;
16540Sstevel@tonic-gate 	case KGE_UNK:
16550Sstevel@tonic-gate 		/* Handle unknown extension. */
16563448Sdh155122 		ks1dbg(keystack, ("Got unknown extension of type %d.\n",
16570Sstevel@tonic-gate 		    extv[0]->sadb_ext_type));
16580Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_EXT);
16590Sstevel@tonic-gate 		return;
16600Sstevel@tonic-gate 	case KGE_LEN:
16610Sstevel@tonic-gate 		/* Length error. */
16623448Sdh155122 		ks1dbg(keystack,
16633448Sdh155122 		    ("Length %d on extension type %d overrun or 0.\n",
16640Sstevel@tonic-gate 		    extv[0]->sadb_ext_len, extv[0]->sadb_ext_type));
16650Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_BAD_EXTLEN);
16660Sstevel@tonic-gate 		return;
16670Sstevel@tonic-gate 	case KGE_CHK:
16680Sstevel@tonic-gate 		/* Reality check failed. */
16693448Sdh155122 		ks1dbg(keystack,
16703448Sdh155122 		    ("Reality check failed on extension type %d.\n",
16710Sstevel@tonic-gate 		    extv[0]->sadb_ext_type));
16720Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL,
16730Sstevel@tonic-gate 		    keysock_malformed(extv[0]->sadb_ext_type));
16740Sstevel@tonic-gate 		return;
16750Sstevel@tonic-gate 	default:
16760Sstevel@tonic-gate 		/* Default case is no errors. */
16770Sstevel@tonic-gate 		break;
16780Sstevel@tonic-gate 	}
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
16810Sstevel@tonic-gate 	case SADB_REGISTER:
16820Sstevel@tonic-gate 		/*
16830Sstevel@tonic-gate 		 * There's a semantic weirdness in that a message OTHER than
16840Sstevel@tonic-gate 		 * the return REGISTER message may be passed up if I set the
16850Sstevel@tonic-gate 		 * registered bit BEFORE I pass it down.
16860Sstevel@tonic-gate 		 *
16870Sstevel@tonic-gate 		 * SOOOO, I'll not twiddle any registered bits until I see
16880Sstevel@tonic-gate 		 * the upbound REGISTER (with a serial number in it).
16890Sstevel@tonic-gate 		 */
16900Sstevel@tonic-gate 		if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
16910Sstevel@tonic-gate 			/* Handle extended register here. */
16920Sstevel@tonic-gate 			keysock_extended_register(ks, mp, extv);
16930Sstevel@tonic-gate 			return;
16940Sstevel@tonic-gate 		} else if (ks->keysock_flags & KEYSOCK_EXTENDED) {
16950Sstevel@tonic-gate 			keysock_error(ks, mp, EBUSY, 0);
16960Sstevel@tonic-gate 			return;
16970Sstevel@tonic-gate 		}
16980Sstevel@tonic-gate 		/* FALLTHRU */
16990Sstevel@tonic-gate 	case SADB_GETSPI:
17000Sstevel@tonic-gate 	case SADB_ADD:
17010Sstevel@tonic-gate 	case SADB_UPDATE:
17026668Smarkfen 	case SADB_X_UPDATEPAIR:
17030Sstevel@tonic-gate 	case SADB_DELETE:
17046668Smarkfen 	case SADB_X_DELPAIR:
17050Sstevel@tonic-gate 	case SADB_GET:
17060Sstevel@tonic-gate 		/*
17070Sstevel@tonic-gate 		 * Pass down to appropriate consumer.
17080Sstevel@tonic-gate 		 */
17090Sstevel@tonic-gate 		if (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC)
17100Sstevel@tonic-gate 			keysock_passdown(ks, mp, samsg->sadb_msg_satype, extv,
17110Sstevel@tonic-gate 			    B_FALSE);
17120Sstevel@tonic-gate 		else keysock_error(ks, mp, EINVAL,
17130Sstevel@tonic-gate 		    SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
17140Sstevel@tonic-gate 		return;
17157749SThejaswini.Singarajipura@Sun.COM 	case SADB_X_DELPAIR_STATE:
17167749SThejaswini.Singarajipura@Sun.COM 		if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
17177749SThejaswini.Singarajipura@Sun.COM 			keysock_delpair_all(ks, mp, extv);
17187749SThejaswini.Singarajipura@Sun.COM 		} else {
17197749SThejaswini.Singarajipura@Sun.COM 			keysock_passdown(ks, mp, samsg->sadb_msg_satype, extv,
17207749SThejaswini.Singarajipura@Sun.COM 			    B_FALSE);
17217749SThejaswini.Singarajipura@Sun.COM 		}
17227749SThejaswini.Singarajipura@Sun.COM 		return;
17230Sstevel@tonic-gate 	case SADB_ACQUIRE:
17240Sstevel@tonic-gate 		/*
17250Sstevel@tonic-gate 		 * If I _receive_ an acquire, this means I should spread it
17260Sstevel@tonic-gate 		 * out to registered sockets.  Unless there's an errno...
17270Sstevel@tonic-gate 		 *
17280Sstevel@tonic-gate 		 * Need ADDRESS, may have ID, SENS, and PROP, unless errno,
17290Sstevel@tonic-gate 		 * in which case there should be NO extensions.
17300Sstevel@tonic-gate 		 *
17310Sstevel@tonic-gate 		 * Return to registered.
17320Sstevel@tonic-gate 		 */
17330Sstevel@tonic-gate 		if (samsg->sadb_msg_errno != 0) {
17340Sstevel@tonic-gate 			satype = samsg->sadb_msg_satype;
17350Sstevel@tonic-gate 			if (satype == SADB_SATYPE_UNSPEC) {
17360Sstevel@tonic-gate 				if (!(ks->keysock_flags & KEYSOCK_EXTENDED)) {
17370Sstevel@tonic-gate 					keysock_error(ks, mp, EINVAL,
17380Sstevel@tonic-gate 					    SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
17390Sstevel@tonic-gate 					return;
17400Sstevel@tonic-gate 				}
17410Sstevel@tonic-gate 				/*
17420Sstevel@tonic-gate 				 * Reassign satype based on the first
17430Sstevel@tonic-gate 				 * flags that KEYSOCK_SETREG says.
17440Sstevel@tonic-gate 				 */
17450Sstevel@tonic-gate 				while (satype <= SADB_SATYPE_MAX) {
17460Sstevel@tonic-gate 					if (KEYSOCK_ISREG(ks, satype))
17470Sstevel@tonic-gate 						break;
17480Sstevel@tonic-gate 					satype++;
17490Sstevel@tonic-gate 				}
17500Sstevel@tonic-gate 				if (satype > SADB_SATYPE_MAX) {
17510Sstevel@tonic-gate 					keysock_error(ks, mp, EBUSY, 0);
17520Sstevel@tonic-gate 					return;
17530Sstevel@tonic-gate 				}
17540Sstevel@tonic-gate 			}
17550Sstevel@tonic-gate 			keysock_passdown(ks, mp, satype, extv, B_FALSE);
17560Sstevel@tonic-gate 		} else {
17573448Sdh155122 			if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) {
17580Sstevel@tonic-gate 				keysock_error(ks, mp, EINVAL,
17590Sstevel@tonic-gate 				    SADB_X_DIAGNOSTIC_SATYPE_NEEDED);
17603448Sdh155122 			} else {
17613448Sdh155122 				keysock_passup(mp, samsg, 0, NULL, B_FALSE,
17623448Sdh155122 				    keystack);
17633448Sdh155122 			}
17640Sstevel@tonic-gate 		}
17650Sstevel@tonic-gate 		return;
17660Sstevel@tonic-gate 	case SADB_EXPIRE:
17670Sstevel@tonic-gate 		/*
17680Sstevel@tonic-gate 		 * If someone sends this in, then send out to all senders.
17690Sstevel@tonic-gate 		 * (Save maybe ESP or AH, I have to be careful here.)
17700Sstevel@tonic-gate 		 *
17710Sstevel@tonic-gate 		 * Need ADDRESS, may have ID and SENS.
17720Sstevel@tonic-gate 		 *
17730Sstevel@tonic-gate 		 * XXX for now this is unsupported.
17740Sstevel@tonic-gate 		 */
17750Sstevel@tonic-gate 		break;
17760Sstevel@tonic-gate 	case SADB_FLUSH:
17770Sstevel@tonic-gate 		/*
17787749SThejaswini.Singarajipura@Sun.COM 		 * Nuke all SAs.
17790Sstevel@tonic-gate 		 *
17800Sstevel@tonic-gate 		 * No extensions at all.  Return to all listeners.
17810Sstevel@tonic-gate 		 *
17820Sstevel@tonic-gate 		 * Question:	Should I hold a lock here to prevent
17830Sstevel@tonic-gate 		 *		additions/deletions while flushing?
17840Sstevel@tonic-gate 		 * Answer:	No.  (See keysock_passdown() for details.)
17850Sstevel@tonic-gate 		 */
17860Sstevel@tonic-gate 		if (extv[0] != NULL) {
17870Sstevel@tonic-gate 			/*
17887749SThejaswini.Singarajipura@Sun.COM 			 * FLUSH messages shouldn't have extensions.
17890Sstevel@tonic-gate 			 * Return EINVAL.
17900Sstevel@tonic-gate 			 */
17913448Sdh155122 			ks2dbg(keystack, ("FLUSH message with extension.\n"));
17920Sstevel@tonic-gate 			keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_NO_EXT);
17930Sstevel@tonic-gate 			return;
17940Sstevel@tonic-gate 		}
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 		/* Passing down of DUMP/FLUSH messages are special. */
17970Sstevel@tonic-gate 		qwriter(q, mp, keysock_do_flushdump, PERIM_INNER);
17980Sstevel@tonic-gate 		return;
17997749SThejaswini.Singarajipura@Sun.COM 	case SADB_DUMP:	 /* not used by normal applications */
18007749SThejaswini.Singarajipura@Sun.COM 		if ((extv[0] != NULL) &&
18017749SThejaswini.Singarajipura@Sun.COM 		    ((msgsize >
18027749SThejaswini.Singarajipura@Sun.COM 		    (sizeof (sadb_msg_t) + sizeof (sadb_x_edump_t))) ||
18037749SThejaswini.Singarajipura@Sun.COM 		    (extv[SADB_X_EXT_EDUMP] == NULL))) {
18047749SThejaswini.Singarajipura@Sun.COM 				keysock_error(ks, mp, EINVAL,
18057749SThejaswini.Singarajipura@Sun.COM 				    SADB_X_DIAGNOSTIC_NO_EXT);
18067749SThejaswini.Singarajipura@Sun.COM 				return;
18077749SThejaswini.Singarajipura@Sun.COM 		}
18087749SThejaswini.Singarajipura@Sun.COM 		qwriter(q, mp, keysock_do_flushdump, PERIM_INNER);
18097749SThejaswini.Singarajipura@Sun.COM 		return;
18100Sstevel@tonic-gate 	case SADB_X_PROMISC:
18110Sstevel@tonic-gate 		/*
18120Sstevel@tonic-gate 		 * Promiscuous processing message.
18130Sstevel@tonic-gate 		 */
18140Sstevel@tonic-gate 		if (samsg->sadb_msg_satype == 0)
18150Sstevel@tonic-gate 			ks->keysock_flags &= ~KEYSOCK_PROMISC;
18160Sstevel@tonic-gate 		else
18170Sstevel@tonic-gate 			ks->keysock_flags |= KEYSOCK_PROMISC;
18183448Sdh155122 		keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE,
18193448Sdh155122 		    keystack);
18200Sstevel@tonic-gate 		return;
18210Sstevel@tonic-gate 	case SADB_X_INVERSE_ACQUIRE:
18220Sstevel@tonic-gate 		keysock_inverse_acquire(mp, samsg, extv, ks);
18230Sstevel@tonic-gate 		return;
18240Sstevel@tonic-gate 	default:
18253448Sdh155122 		ks2dbg(keystack, ("Got unknown message type %d.\n",
18260Sstevel@tonic-gate 		    samsg->sadb_msg_type));
18270Sstevel@tonic-gate 		keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_MSG);
18280Sstevel@tonic-gate 		return;
18290Sstevel@tonic-gate 	}
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	/* As a placeholder... */
18320Sstevel@tonic-gate 	ks0dbg(("keysock_parse():  Hit EOPNOTSUPP\n"));
18330Sstevel@tonic-gate 	keysock_error(ks, mp, EOPNOTSUPP, SADB_X_DIAGNOSTIC_NONE);
18340Sstevel@tonic-gate }
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate /*
18370Sstevel@tonic-gate  * wput routing for PF_KEY/keysock/whatever.  Unlike the routing socket,
18380Sstevel@tonic-gate  * I don't convert to ioctl()'s for IP.  I am the end-all driver as far
18390Sstevel@tonic-gate  * as PF_KEY sockets are concerned.  I do some conversion, but not as much
18400Sstevel@tonic-gate  * as IP/rts does.
18410Sstevel@tonic-gate  */
18420Sstevel@tonic-gate static void
keysock_wput(queue_t * q,mblk_t * mp)18430Sstevel@tonic-gate keysock_wput(queue_t *q, mblk_t *mp)
18440Sstevel@tonic-gate {
18450Sstevel@tonic-gate 	uchar_t *rptr = mp->b_rptr;
18460Sstevel@tonic-gate 	mblk_t *mp1;
18473448Sdh155122 	keysock_t *ks;
18483448Sdh155122 	keysock_stack_t	*keystack;
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	if (WR(q)->q_next) {
18510Sstevel@tonic-gate 		keysock_consumer_t *kc = (keysock_consumer_t *)q->q_ptr;
18523448Sdh155122 		keystack = kc->kc_keystack;
18533448Sdh155122 
18543448Sdh155122 		ks3dbg(keystack, ("In keysock_wput\n"));
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 		/*
18570Sstevel@tonic-gate 		 * We shouldn't get writes on a consumer instance.
18580Sstevel@tonic-gate 		 * But for now, just passthru.
18590Sstevel@tonic-gate 		 */
18603448Sdh155122 		ks1dbg(keystack, ("Huh?  wput for an consumer instance (%d)?\n",
18610Sstevel@tonic-gate 		    kc->kc_sa_type));
18620Sstevel@tonic-gate 		putnext(q, mp);
18630Sstevel@tonic-gate 		return;
18640Sstevel@tonic-gate 	}
18653448Sdh155122 	ks = (keysock_t *)q->q_ptr;
18663448Sdh155122 	keystack = ks->keysock_keystack;
18673448Sdh155122 
18683448Sdh155122 	ks3dbg(keystack, ("In keysock_wput\n"));
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
18710Sstevel@tonic-gate 	case M_DATA:
18720Sstevel@tonic-gate 		/*
18730Sstevel@tonic-gate 		 * Silently discard.
18740Sstevel@tonic-gate 		 */
18753448Sdh155122 		ks2dbg(keystack, ("raw M_DATA in keysock.\n"));
18760Sstevel@tonic-gate 		freemsg(mp);
18770Sstevel@tonic-gate 		return;
18780Sstevel@tonic-gate 	case M_PROTO:
18790Sstevel@tonic-gate 	case M_PCPROTO:
18800Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
18810Sstevel@tonic-gate 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
18820Sstevel@tonic-gate 				if ((mp1 = mp->b_cont) == NULL) {
18830Sstevel@tonic-gate 					/* No data after T_DATA_REQ. */
18843448Sdh155122 					ks2dbg(keystack,
18853448Sdh155122 					    ("No data after DATA_REQ.\n"));
18860Sstevel@tonic-gate 					freemsg(mp);
18870Sstevel@tonic-gate 					return;
18880Sstevel@tonic-gate 				}
18890Sstevel@tonic-gate 				freeb(mp);
18900Sstevel@tonic-gate 				mp = mp1;
18913448Sdh155122 				ks2dbg(keystack, ("T_DATA_REQ\n"));
18920Sstevel@tonic-gate 				break;	/* Out of switch. */
18930Sstevel@tonic-gate 			}
18940Sstevel@tonic-gate 		}
18950Sstevel@tonic-gate 		/* FALLTHRU */
18960Sstevel@tonic-gate 	default:
18973448Sdh155122 		ks3dbg(keystack, ("In default wput case (%d %d).\n",
18980Sstevel@tonic-gate 		    mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
18990Sstevel@tonic-gate 		keysock_wput_other(q, mp);
19000Sstevel@tonic-gate 		return;
19010Sstevel@tonic-gate 	}
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	/* I now have a PF_KEY message in an M_DATA block, pointed to by mp. */
19040Sstevel@tonic-gate 	keysock_parse(q, mp);
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_rput(). */
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate /*
19100Sstevel@tonic-gate  * Called upon receipt of a KEYSOCK_HELLO_ACK to set up the appropriate
19110Sstevel@tonic-gate  * state vectors.
19120Sstevel@tonic-gate  */
19130Sstevel@tonic-gate static void
keysock_link_consumer(uint8_t satype,keysock_consumer_t * kc)19140Sstevel@tonic-gate keysock_link_consumer(uint8_t satype, keysock_consumer_t *kc)
19150Sstevel@tonic-gate {
19160Sstevel@tonic-gate 	keysock_t *ks;
19173448Sdh155122 	keysock_stack_t	*keystack = kc->kc_keystack;
19180Sstevel@tonic-gate 
19193448Sdh155122 	mutex_enter(&keystack->keystack_consumers_lock);
19200Sstevel@tonic-gate 	mutex_enter(&kc->kc_lock);
19213448Sdh155122 	if (keystack->keystack_consumers[satype] != NULL) {
19220Sstevel@tonic-gate 		ks0dbg((
19230Sstevel@tonic-gate 		    "Hmmmm, someone closed %d before the HELLO_ACK happened.\n",
19240Sstevel@tonic-gate 		    satype));
19250Sstevel@tonic-gate 		/*
19260Sstevel@tonic-gate 		 * Perhaps updating the new below-me consumer with what I have
19270Sstevel@tonic-gate 		 * so far would work too?
19280Sstevel@tonic-gate 		 */
19290Sstevel@tonic-gate 		mutex_exit(&kc->kc_lock);
19303448Sdh155122 		mutex_exit(&keystack->keystack_consumers_lock);
19310Sstevel@tonic-gate 	} else {
19320Sstevel@tonic-gate 		/* Add new below-me consumer. */
19333448Sdh155122 		keystack->keystack_consumers[satype] = kc;
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 		kc->kc_flags = 0;
19360Sstevel@tonic-gate 		kc->kc_sa_type = satype;
19370Sstevel@tonic-gate 		mutex_exit(&kc->kc_lock);
19383448Sdh155122 		mutex_exit(&keystack->keystack_consumers_lock);
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 		/* Scan the keysock list. */
19413448Sdh155122 		mutex_enter(&keystack->keystack_list_lock);
19423448Sdh155122 		for (ks = keystack->keystack_list; ks != NULL;
19433448Sdh155122 		    ks = ks->keysock_next) {
19440Sstevel@tonic-gate 			if (KEYSOCK_ISREG(ks, satype)) {
19450Sstevel@tonic-gate 				/*
19460Sstevel@tonic-gate 				 * XXX Perhaps send an SADB_REGISTER down on
19470Sstevel@tonic-gate 				 * the socket's behalf.
19480Sstevel@tonic-gate 				 */
19493448Sdh155122 				ks1dbg(keystack,
19503448Sdh155122 				    ("Socket %u registered already for "
19510Sstevel@tonic-gate 				    "new consumer.\n", ks->keysock_serial));
19520Sstevel@tonic-gate 			}
19530Sstevel@tonic-gate 		}
19543448Sdh155122 		mutex_exit(&keystack->keystack_list_lock);
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate }
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate /*
19590Sstevel@tonic-gate  * Generate a KEYSOCK_OUT_ERR message for my consumer.
19600Sstevel@tonic-gate  */
19610Sstevel@tonic-gate static void
keysock_out_err(keysock_consumer_t * kc,int ks_errno,mblk_t * mp)19620Sstevel@tonic-gate keysock_out_err(keysock_consumer_t *kc, int ks_errno, mblk_t *mp)
19630Sstevel@tonic-gate {
19640Sstevel@tonic-gate 	keysock_out_err_t *kse;
19650Sstevel@tonic-gate 	mblk_t *imp;
19663448Sdh155122 	keysock_stack_t	*keystack = kc->kc_keystack;
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	imp = allocb(sizeof (ipsec_info_t), BPRI_HI);
19690Sstevel@tonic-gate 	if (imp == NULL) {
19703448Sdh155122 		ks1dbg(keystack, ("keysock_out_err:  Can't alloc message.\n"));
19710Sstevel@tonic-gate 		return;
19720Sstevel@tonic-gate 	}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	imp->b_datap->db_type = M_CTL;
19750Sstevel@tonic-gate 	imp->b_wptr += sizeof (ipsec_info_t);
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	kse = (keysock_out_err_t *)imp->b_rptr;
19780Sstevel@tonic-gate 	imp->b_cont = mp;
19790Sstevel@tonic-gate 	kse->ks_err_type = KEYSOCK_OUT_ERR;
19800Sstevel@tonic-gate 	kse->ks_err_len = sizeof (*kse);
19810Sstevel@tonic-gate 	/* Is serial necessary? */
19820Sstevel@tonic-gate 	kse->ks_err_serial = 0;
19830Sstevel@tonic-gate 	kse->ks_err_errno = ks_errno;
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	/*
19860Sstevel@tonic-gate 	 * XXX What else do I need to do here w.r.t. information
19870Sstevel@tonic-gate 	 * to tell the consumer what caused this error?
19880Sstevel@tonic-gate 	 *
19890Sstevel@tonic-gate 	 * I believe the answer is the PF_KEY ACQUIRE (or other) message
19900Sstevel@tonic-gate 	 * attached in mp, which is appended at the end.  I believe the
19910Sstevel@tonic-gate 	 * db_ref won't matter here, because the PF_KEY message is only read
19920Sstevel@tonic-gate 	 * for KEYSOCK_OUT_ERR.
19930Sstevel@tonic-gate 	 */
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	putnext(kc->kc_wq, imp);
19960Sstevel@tonic-gate }
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate /* XXX this is a hack errno. */
19990Sstevel@tonic-gate #define	EIPSECNOSA 255
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate /*
20020Sstevel@tonic-gate  * Route message (pointed by mp, header in samsg) toward appropriate
20030Sstevel@tonic-gate  * sockets.  Assume the message's creator did its job correctly.
20040Sstevel@tonic-gate  *
20050Sstevel@tonic-gate  * This should be a function that is followed by a return in its caller.
20060Sstevel@tonic-gate  * The compiler _should_ be able to use tail-call optimizations to make the
20070Sstevel@tonic-gate  * large ## of parameters not a huge deal.
20080Sstevel@tonic-gate  */
20090Sstevel@tonic-gate static void
keysock_passup(mblk_t * mp,sadb_msg_t * samsg,minor_t serial,keysock_consumer_t * kc,boolean_t persistent,keysock_stack_t * keystack)20100Sstevel@tonic-gate keysock_passup(mblk_t *mp, sadb_msg_t *samsg, minor_t serial,
20113448Sdh155122     keysock_consumer_t *kc, boolean_t persistent, keysock_stack_t *keystack)
20120Sstevel@tonic-gate {
20130Sstevel@tonic-gate 	keysock_t *ks;
20140Sstevel@tonic-gate 	uint8_t satype = samsg->sadb_msg_satype;
20150Sstevel@tonic-gate 	boolean_t toall = B_FALSE, allreg = B_FALSE, allereg = B_FALSE,
20160Sstevel@tonic-gate 	    setalg = B_FALSE;
20170Sstevel@tonic-gate 	mblk_t *mp1;
20180Sstevel@tonic-gate 	int err = EIPSECNOSA;
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 	/* Convert mp, which is M_DATA, into an M_PROTO of type T_DATA_IND */
20210Sstevel@tonic-gate 	mp1 = allocb(sizeof (struct T_data_req), BPRI_HI);
20220Sstevel@tonic-gate 	if (mp1 == NULL) {
20230Sstevel@tonic-gate 		err = ENOMEM;
20240Sstevel@tonic-gate 		goto error;
20250Sstevel@tonic-gate 	}
20260Sstevel@tonic-gate 	mp1->b_wptr += sizeof (struct T_data_req);
20270Sstevel@tonic-gate 	((struct T_data_ind *)mp1->b_rptr)->PRIM_type = T_DATA_IND;
20280Sstevel@tonic-gate 	((struct T_data_ind *)mp1->b_rptr)->MORE_flag = 0;
20290Sstevel@tonic-gate 	mp1->b_datap->db_type = M_PROTO;
20300Sstevel@tonic-gate 	mp1->b_cont = mp;
20310Sstevel@tonic-gate 	mp = mp1;
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	switch (samsg->sadb_msg_type) {
20340Sstevel@tonic-gate 	case SADB_FLUSH:
20350Sstevel@tonic-gate 	case SADB_GETSPI:
20360Sstevel@tonic-gate 	case SADB_UPDATE:
20376668Smarkfen 	case SADB_X_UPDATEPAIR:
20380Sstevel@tonic-gate 	case SADB_ADD:
20390Sstevel@tonic-gate 	case SADB_DELETE:
20406668Smarkfen 	case SADB_X_DELPAIR:
20410Sstevel@tonic-gate 	case SADB_EXPIRE:
20420Sstevel@tonic-gate 		/*
20430Sstevel@tonic-gate 		 * These are most likely replies.  Don't worry about
20440Sstevel@tonic-gate 		 * KEYSOCK_OUT_ERR handling.  Deliver to all sockets.
20450Sstevel@tonic-gate 		 */
20463448Sdh155122 		ks3dbg(keystack,
20473448Sdh155122 		    ("Delivering normal message (%d) to all sockets.\n",
20480Sstevel@tonic-gate 		    samsg->sadb_msg_type));
20490Sstevel@tonic-gate 		toall = B_TRUE;
20500Sstevel@tonic-gate 		break;
20510Sstevel@tonic-gate 	case SADB_REGISTER:
20520Sstevel@tonic-gate 		/*
20530Sstevel@tonic-gate 		 * REGISTERs come up for one of three reasons:
20540Sstevel@tonic-gate 		 *
20550Sstevel@tonic-gate 		 *	1.) In response to a normal SADB_REGISTER
20560Sstevel@tonic-gate 		 *		(samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
20570Sstevel@tonic-gate 		 *		    serial != 0)
20580Sstevel@tonic-gate 		 *		Deliver to normal SADB_REGISTERed sockets.
20590Sstevel@tonic-gate 		 *	2.) In response to an extended REGISTER
20600Sstevel@tonic-gate 		 *		(samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
20610Sstevel@tonic-gate 		 *		Deliver to extended REGISTERed socket.
20620Sstevel@tonic-gate 		 *	3.) Spontaneous algorithm changes
20630Sstevel@tonic-gate 		 *		(samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
20640Sstevel@tonic-gate 		 *		    serial == 0)
20650Sstevel@tonic-gate 		 *		Deliver to REGISTERed sockets of all sorts.
20660Sstevel@tonic-gate 		 */
20670Sstevel@tonic-gate 		if (kc == NULL) {
20680Sstevel@tonic-gate 			/* Here because of keysock_error() call. */
20690Sstevel@tonic-gate 			ASSERT(samsg->sadb_msg_errno != 0);
20700Sstevel@tonic-gate 			break;	/* Out of switch. */
20710Sstevel@tonic-gate 		}
20723448Sdh155122 		ks3dbg(keystack, ("Delivering REGISTER.\n"));
20730Sstevel@tonic-gate 		if (satype == SADB_SATYPE_UNSPEC) {
20740Sstevel@tonic-gate 			/* REGISTER Reason #2 */
20750Sstevel@tonic-gate 			allereg = B_TRUE;
20760Sstevel@tonic-gate 			/*
20770Sstevel@tonic-gate 			 * Rewhack SA type so PF_KEY socket holder knows what
20780Sstevel@tonic-gate 			 * consumer generated this algorithm list.
20790Sstevel@tonic-gate 			 */
20800Sstevel@tonic-gate 			satype = kc->kc_sa_type;
20810Sstevel@tonic-gate 			samsg->sadb_msg_satype = satype;
20820Sstevel@tonic-gate 			setalg = B_TRUE;
20830Sstevel@tonic-gate 		} else if (serial == 0) {
20840Sstevel@tonic-gate 			/* REGISTER Reason #3 */
20850Sstevel@tonic-gate 			allreg = B_TRUE;
20860Sstevel@tonic-gate 			allereg = B_TRUE;
20870Sstevel@tonic-gate 		} else {
20880Sstevel@tonic-gate 			/* REGISTER Reason #1 */
20890Sstevel@tonic-gate 			allreg = B_TRUE;
20900Sstevel@tonic-gate 			setalg = B_TRUE;
20910Sstevel@tonic-gate 		}
20920Sstevel@tonic-gate 		break;
20930Sstevel@tonic-gate 	case SADB_ACQUIRE:
20940Sstevel@tonic-gate 		/*
20950Sstevel@tonic-gate 		 * ACQUIREs are either extended (sadb_msg_satype == 0) or
20960Sstevel@tonic-gate 		 * regular (sadb_msg_satype != 0).  And we're guaranteed
20970Sstevel@tonic-gate 		 * that serial == 0 for an ACQUIRE.
20980Sstevel@tonic-gate 		 */
20993448Sdh155122 		ks3dbg(keystack, ("Delivering ACQUIRE.\n"));
21000Sstevel@tonic-gate 		allereg = (satype == SADB_SATYPE_UNSPEC);
21010Sstevel@tonic-gate 		allreg = !allereg;
21020Sstevel@tonic-gate 		/*
21030Sstevel@tonic-gate 		 * Corner case - if we send a regular ACQUIRE and there's
21040Sstevel@tonic-gate 		 * extended ones registered, don't send an error down to
21050Sstevel@tonic-gate 		 * consumers if nobody's listening and prematurely destroy
21060Sstevel@tonic-gate 		 * their ACQUIRE record.  This might be too hackish of a
21070Sstevel@tonic-gate 		 * solution.
21080Sstevel@tonic-gate 		 */
21093448Sdh155122 		if (allreg && keystack->keystack_num_extended > 0)
21100Sstevel@tonic-gate 			err = 0;
21110Sstevel@tonic-gate 		break;
21120Sstevel@tonic-gate 	case SADB_X_PROMISC:
21130Sstevel@tonic-gate 	case SADB_X_INVERSE_ACQUIRE:
21140Sstevel@tonic-gate 	case SADB_DUMP:
21150Sstevel@tonic-gate 	case SADB_GET:
21160Sstevel@tonic-gate 	default:
21170Sstevel@tonic-gate 		/*
21180Sstevel@tonic-gate 		 * Deliver to the sender and promiscuous only.
21190Sstevel@tonic-gate 		 */
21203448Sdh155122 		ks3dbg(keystack, ("Delivering sender/promisc only (%d).\n",
21210Sstevel@tonic-gate 		    samsg->sadb_msg_type));
21220Sstevel@tonic-gate 		break;
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate 
21253448Sdh155122 	mutex_enter(&keystack->keystack_list_lock);
21263448Sdh155122 	for (ks = keystack->keystack_list; ks != NULL; ks = ks->keysock_next) {
21270Sstevel@tonic-gate 		/* Delivery loop. */
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 		/*
21300Sstevel@tonic-gate 		 * Check special keysock-setting cases (REGISTER replies)
21310Sstevel@tonic-gate 		 * here.
21320Sstevel@tonic-gate 		 */
21330Sstevel@tonic-gate 		if (setalg && serial == ks->keysock_serial) {
21340Sstevel@tonic-gate 			ASSERT(kc != NULL);
21350Sstevel@tonic-gate 			ASSERT(kc->kc_sa_type == satype);
21360Sstevel@tonic-gate 			KEYSOCK_SETREG(ks, satype);
21370Sstevel@tonic-gate 		}
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 		/*
21400Sstevel@tonic-gate 		 * NOLOOP takes precedence over PROMISC.  So if you've set
21410Sstevel@tonic-gate 		 * !SO_USELOOPBACK, don't expect to see any data...
21420Sstevel@tonic-gate 		 */
21430Sstevel@tonic-gate 		if (ks->keysock_flags & KEYSOCK_NOLOOP)
21440Sstevel@tonic-gate 			continue;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 		/*
21470Sstevel@tonic-gate 		 * Messages to all, or promiscuous sockets just GET the
21480Sstevel@tonic-gate 		 * message.  Perform rules-type checking iff it's not for all
21490Sstevel@tonic-gate 		 * listeners or the socket is in promiscuous mode.
21500Sstevel@tonic-gate 		 *
21510Sstevel@tonic-gate 		 * NOTE:Because of the (kc != NULL && ISREG()), make sure
21520Sstevel@tonic-gate 		 *	extended ACQUIREs arrive off a consumer that is
21530Sstevel@tonic-gate 		 *	part of the extended REGISTER set of consumers.
21540Sstevel@tonic-gate 		 */
21550Sstevel@tonic-gate 		if (serial != ks->keysock_serial &&
21560Sstevel@tonic-gate 		    !toall &&
21570Sstevel@tonic-gate 		    !(ks->keysock_flags & KEYSOCK_PROMISC) &&
21580Sstevel@tonic-gate 		    !((ks->keysock_flags & KEYSOCK_EXTENDED) ?
21595240Snordmark 		    allereg : allreg && kc != NULL &&
21605240Snordmark 		    KEYSOCK_ISREG(ks, kc->kc_sa_type)))
21610Sstevel@tonic-gate 			continue;
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 		mp1 = dupmsg(mp);
21640Sstevel@tonic-gate 		if (mp1 == NULL) {
21653448Sdh155122 			ks2dbg(keystack, (
21660Sstevel@tonic-gate 			    "keysock_passup():  dupmsg() failed.\n"));
21670Sstevel@tonic-gate 			mp1 = mp;
21680Sstevel@tonic-gate 			mp = NULL;
21690Sstevel@tonic-gate 			err = ENOMEM;
21700Sstevel@tonic-gate 		}
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 		/*
21730Sstevel@tonic-gate 		 * At this point, we can deliver or attempt to deliver
21740Sstevel@tonic-gate 		 * this message.  We're free of obligation to report
21750Sstevel@tonic-gate 		 * no listening PF_KEY sockets.  So set err to 0.
21760Sstevel@tonic-gate 		 */
21770Sstevel@tonic-gate 		err = 0;
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 		/*
21800Sstevel@tonic-gate 		 * See if we canputnext(), as well as see if the message
21810Sstevel@tonic-gate 		 * needs to be queued if we can't.
21820Sstevel@tonic-gate 		 */
21830Sstevel@tonic-gate 		if (!canputnext(ks->keysock_rq)) {
21840Sstevel@tonic-gate 			if (persistent) {
21850Sstevel@tonic-gate 				if (putq(ks->keysock_rq, mp1) == 0) {
21863448Sdh155122 					ks1dbg(keystack, (
21870Sstevel@tonic-gate 					    "keysock_passup: putq failed.\n"));
21880Sstevel@tonic-gate 				} else {
21890Sstevel@tonic-gate 					continue;
21900Sstevel@tonic-gate 				}
21910Sstevel@tonic-gate 			}
21920Sstevel@tonic-gate 			freemsg(mp1);
21930Sstevel@tonic-gate 			continue;
21940Sstevel@tonic-gate 		}
21950Sstevel@tonic-gate 
21963448Sdh155122 		ks3dbg(keystack,
21973448Sdh155122 		    ("Putting to serial %d.\n", ks->keysock_serial));
21980Sstevel@tonic-gate 		/*
21990Sstevel@tonic-gate 		 * Unlike the specific keysock instance case, this
22000Sstevel@tonic-gate 		 * will only hit for listeners, so we will only
22010Sstevel@tonic-gate 		 * putnext() if we can.
22020Sstevel@tonic-gate 		 */
22030Sstevel@tonic-gate 		putnext(ks->keysock_rq, mp1);
22040Sstevel@tonic-gate 		if (mp == NULL)
22050Sstevel@tonic-gate 			break;	/* out of for loop. */
22060Sstevel@tonic-gate 	}
22073448Sdh155122 	mutex_exit(&keystack->keystack_list_lock);
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate error:
22100Sstevel@tonic-gate 	if ((err != 0) && (kc != NULL)) {
22110Sstevel@tonic-gate 		/*
22120Sstevel@tonic-gate 		 * Generate KEYSOCK_OUT_ERR for consumer.
22130Sstevel@tonic-gate 		 * Basically, I send this back if I have not been able to
22140Sstevel@tonic-gate 		 * transmit (for whatever reason)
22150Sstevel@tonic-gate 		 */
22163448Sdh155122 		ks1dbg(keystack,
22173448Sdh155122 		    ("keysock_passup():  No registered of type %d.\n",
22180Sstevel@tonic-gate 		    satype));
22190Sstevel@tonic-gate 		if (mp != NULL) {
22200Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_PROTO) {
22210Sstevel@tonic-gate 				mp1 = mp;
22220Sstevel@tonic-gate 				mp = mp->b_cont;
22230Sstevel@tonic-gate 				freeb(mp1);
22240Sstevel@tonic-gate 			}
22250Sstevel@tonic-gate 			/*
22260Sstevel@tonic-gate 			 * Do a copymsg() because people who get
22270Sstevel@tonic-gate 			 * KEYSOCK_OUT_ERR may alter the message contents.
22280Sstevel@tonic-gate 			 */
22290Sstevel@tonic-gate 			mp1 = copymsg(mp);
22300Sstevel@tonic-gate 			if (mp1 == NULL) {
22313448Sdh155122 				ks2dbg(keystack,
22323448Sdh155122 				    ("keysock_passup: copymsg() failed.\n"));
22330Sstevel@tonic-gate 				mp1 = mp;
22340Sstevel@tonic-gate 				mp = NULL;
22350Sstevel@tonic-gate 			}
22360Sstevel@tonic-gate 			keysock_out_err(kc, err, mp1);
22370Sstevel@tonic-gate 		}
22380Sstevel@tonic-gate 	}
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 	/*
22410Sstevel@tonic-gate 	 * XXX Blank the message somehow.  This is difficult because we don't
22420Sstevel@tonic-gate 	 * know at this point if the message has db_ref > 1, etc.
22430Sstevel@tonic-gate 	 *
22440Sstevel@tonic-gate 	 * Optimally, keysock messages containing actual keying material would
22450Sstevel@tonic-gate 	 * be allocated with esballoc(), with a zeroing free function.
22460Sstevel@tonic-gate 	 */
22470Sstevel@tonic-gate 	if (mp != NULL)
22480Sstevel@tonic-gate 		freemsg(mp);
22490Sstevel@tonic-gate }
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate /*
22520Sstevel@tonic-gate  * Keysock's read service procedure is there only for PF_KEY reply
22530Sstevel@tonic-gate  * messages that really need to reach the top.
22540Sstevel@tonic-gate  */
22550Sstevel@tonic-gate static void
keysock_rsrv(queue_t * q)22560Sstevel@tonic-gate keysock_rsrv(queue_t *q)
22570Sstevel@tonic-gate {
22580Sstevel@tonic-gate 	mblk_t *mp;
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
22610Sstevel@tonic-gate 		if (canputnext(q)) {
22620Sstevel@tonic-gate 			putnext(q, mp);
22630Sstevel@tonic-gate 		} else {
22640Sstevel@tonic-gate 			(void) putbq(q, mp);
22650Sstevel@tonic-gate 			return;
22660Sstevel@tonic-gate 		}
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate }
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate /*
22710Sstevel@tonic-gate  * The read procedure should only be invoked by a keysock consumer, like
22720Sstevel@tonic-gate  * ESP, AH, etc.  I should only see KEYSOCK_OUT and KEYSOCK_HELLO_ACK
22730Sstevel@tonic-gate  * messages on my read queues.
22740Sstevel@tonic-gate  */
22750Sstevel@tonic-gate static void
keysock_rput(queue_t * q,mblk_t * mp)22760Sstevel@tonic-gate keysock_rput(queue_t *q, mblk_t *mp)
22770Sstevel@tonic-gate {
22780Sstevel@tonic-gate 	keysock_consumer_t *kc = (keysock_consumer_t *)q->q_ptr;
22790Sstevel@tonic-gate 	ipsec_info_t *ii;
22800Sstevel@tonic-gate 	keysock_hello_ack_t *ksa;
22810Sstevel@tonic-gate 	minor_t serial;
22820Sstevel@tonic-gate 	mblk_t *mp1;
22830Sstevel@tonic-gate 	sadb_msg_t *samsg;
22843448Sdh155122 	keysock_stack_t	*keystack = kc->kc_keystack;
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 	/* Make sure I'm a consumer instance.  (i.e. something's below me) */
22870Sstevel@tonic-gate 	ASSERT(WR(q)->q_next != NULL);
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_CTL) {
22900Sstevel@tonic-gate 		/*
22910Sstevel@tonic-gate 		 * Keysock should only see keysock consumer interface
22920Sstevel@tonic-gate 		 * messages (see ipsec_info.h) on its read procedure.
22930Sstevel@tonic-gate 		 * To be robust, however, putnext() up so the STREAM head can
22940Sstevel@tonic-gate 		 * deal with it appropriately.
22950Sstevel@tonic-gate 		 */
22963448Sdh155122 		ks1dbg(keystack,
22973448Sdh155122 		    ("Hmmm, a non M_CTL (%d, 0x%x) on keysock_rput.\n",
22980Sstevel@tonic-gate 		    mp->b_datap->db_type, mp->b_datap->db_type));
22990Sstevel@tonic-gate 		putnext(q, mp);
23000Sstevel@tonic-gate 		return;
23010Sstevel@tonic-gate 	}
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 	ii = (ipsec_info_t *)mp->b_rptr;
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	switch (ii->ipsec_info_type) {
23060Sstevel@tonic-gate 	case KEYSOCK_OUT:
23070Sstevel@tonic-gate 		/*
23080Sstevel@tonic-gate 		 * A consumer needs to pass a response message or an ACQUIRE
23090Sstevel@tonic-gate 		 * UP.  I assume that the consumer has done the right
23100Sstevel@tonic-gate 		 * thing w.r.t. message creation, etc.
23110Sstevel@tonic-gate 		 */
23120Sstevel@tonic-gate 		serial = ((keysock_out_t *)mp->b_rptr)->ks_out_serial;
23130Sstevel@tonic-gate 		mp1 = mp->b_cont;	/* Get M_DATA portion. */
23140Sstevel@tonic-gate 		freeb(mp);
23150Sstevel@tonic-gate 		samsg = (sadb_msg_t *)mp1->b_rptr;
23160Sstevel@tonic-gate 		if (samsg->sadb_msg_type == SADB_FLUSH ||
23170Sstevel@tonic-gate 		    (samsg->sadb_msg_type == SADB_DUMP &&
23185240Snordmark 		    samsg->sadb_msg_len == SADB_8TO64(sizeof (*samsg)))) {
23190Sstevel@tonic-gate 			/*
23200Sstevel@tonic-gate 			 * If I'm an end-of-FLUSH or an end-of-DUMP marker...
23210Sstevel@tonic-gate 			 */
23223448Sdh155122 			ASSERT(keystack->keystack_flushdump != 0);
23233448Sdh155122 						/* Am I flushing? */
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 			mutex_enter(&kc->kc_lock);
23260Sstevel@tonic-gate 			kc->kc_flags &= ~KC_FLUSHING;
23270Sstevel@tonic-gate 			mutex_exit(&kc->kc_lock);
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 			if (samsg->sadb_msg_errno != 0)
23303448Sdh155122 				keystack->keystack_flushdump_errno =
23313448Sdh155122 				    samsg->sadb_msg_errno;
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 			/*
23340Sstevel@tonic-gate 			 * Lower the atomic "flushing" count.  If it's
23350Sstevel@tonic-gate 			 * the last one, send up the end-of-{FLUSH,DUMP} to
23360Sstevel@tonic-gate 			 * the appropriate PF_KEY socket.
23370Sstevel@tonic-gate 			 */
23383448Sdh155122 			if (atomic_add_32_nv(&keystack->keystack_flushdump,
23393448Sdh155122 			    -1) != 0) {
23403448Sdh155122 				ks1dbg(keystack,
23413448Sdh155122 				    ("One flush/dump message back from %d,"
23420Sstevel@tonic-gate 				    " more to go.\n", samsg->sadb_msg_satype));
23430Sstevel@tonic-gate 				freemsg(mp1);
23440Sstevel@tonic-gate 				return;
23450Sstevel@tonic-gate 			}
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 			samsg->sadb_msg_errno =
23483448Sdh155122 			    (uint8_t)keystack->keystack_flushdump_errno;
23490Sstevel@tonic-gate 			if (samsg->sadb_msg_type == SADB_DUMP) {
23500Sstevel@tonic-gate 				samsg->sadb_msg_seq = 0;
23510Sstevel@tonic-gate 			}
23520Sstevel@tonic-gate 		}
23530Sstevel@tonic-gate 		keysock_passup(mp1, samsg, serial, kc,
23543448Sdh155122 		    (samsg->sadb_msg_type == SADB_DUMP), keystack);
23550Sstevel@tonic-gate 		return;
23560Sstevel@tonic-gate 	case KEYSOCK_HELLO_ACK:
23570Sstevel@tonic-gate 		/* Aha, now we can link in the consumer! */
23580Sstevel@tonic-gate 		ksa = (keysock_hello_ack_t *)ii;
23590Sstevel@tonic-gate 		keysock_link_consumer(ksa->ks_hello_satype, kc);
23600Sstevel@tonic-gate 		freemsg(mp);
23610Sstevel@tonic-gate 		return;
23620Sstevel@tonic-gate 	default:
23633448Sdh155122 		ks1dbg(keystack, ("Hmmm, an IPsec info I'm not used to, 0x%x\n",
23640Sstevel@tonic-gate 		    ii->ipsec_info_type));
23650Sstevel@tonic-gate 		putnext(q, mp);
23660Sstevel@tonic-gate 	}
23670Sstevel@tonic-gate }
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate /*
23700Sstevel@tonic-gate  * So we can avoid external linking problems....
23710Sstevel@tonic-gate  */
23720Sstevel@tonic-gate boolean_t
keysock_extended_reg(netstack_t * ns)23733448Sdh155122 keysock_extended_reg(netstack_t *ns)
23740Sstevel@tonic-gate {
23753448Sdh155122 	keysock_stack_t	*keystack = ns->netstack_keysock;
23763448Sdh155122 
23773448Sdh155122 	return (keystack->keystack_num_extended != 0);
23780Sstevel@tonic-gate }
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate uint32_t
keysock_next_seq(netstack_t * ns)23813448Sdh155122 keysock_next_seq(netstack_t *ns)
23820Sstevel@tonic-gate {
23833448Sdh155122 	keysock_stack_t	*keystack = ns->netstack_keysock;
23843448Sdh155122 
23853448Sdh155122 	return (atomic_add_32_nv(&keystack->keystack_acquire_seq, -1));
23860Sstevel@tonic-gate }
2387