xref: /onnv-gate/usr/src/uts/common/fs/sockfs/sockparams.c (revision 12257:637bf44ff506)
18348SEric.Yu@Sun.COM /*
28348SEric.Yu@Sun.COM  * CDDL HEADER START
38348SEric.Yu@Sun.COM  *
48348SEric.Yu@Sun.COM  * The contents of this file are subject to the terms of the
58348SEric.Yu@Sun.COM  * Common Development and Distribution License (the "License").
68348SEric.Yu@Sun.COM  * You may not use this file except in compliance with the License.
78348SEric.Yu@Sun.COM  *
88348SEric.Yu@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98348SEric.Yu@Sun.COM  * or http://www.opensolaris.org/os/licensing.
108348SEric.Yu@Sun.COM  * See the License for the specific language governing permissions
118348SEric.Yu@Sun.COM  * and limitations under the License.
128348SEric.Yu@Sun.COM  *
138348SEric.Yu@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
148348SEric.Yu@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158348SEric.Yu@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
168348SEric.Yu@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
178348SEric.Yu@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
188348SEric.Yu@Sun.COM  *
198348SEric.Yu@Sun.COM  * CDDL HEADER END
208348SEric.Yu@Sun.COM  */
218348SEric.Yu@Sun.COM 
228348SEric.Yu@Sun.COM /*
23*12257SAnders.Persson@Sun.COM  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
248348SEric.Yu@Sun.COM  */
258348SEric.Yu@Sun.COM 
268348SEric.Yu@Sun.COM #include <sys/types.h>
278348SEric.Yu@Sun.COM #include <sys/t_lock.h>
288348SEric.Yu@Sun.COM #include <sys/param.h>
298348SEric.Yu@Sun.COM #include <sys/systm.h>
308348SEric.Yu@Sun.COM #include <sys/sysmacros.h>
318348SEric.Yu@Sun.COM #include <sys/cmn_err.h>
328348SEric.Yu@Sun.COM #include <sys/list.h>
338348SEric.Yu@Sun.COM 
348348SEric.Yu@Sun.COM #include <sys/stropts.h>
358348SEric.Yu@Sun.COM #include <sys/socket.h>
368348SEric.Yu@Sun.COM #include <sys/socketvar.h>
378348SEric.Yu@Sun.COM 
388348SEric.Yu@Sun.COM #include <fs/sockfs/sockcommon.h>
398348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h>
408348SEric.Yu@Sun.COM 
418348SEric.Yu@Sun.COM /*
428348SEric.Yu@Sun.COM  * Socket Parameters
438348SEric.Yu@Sun.COM  *
448348SEric.Yu@Sun.COM  * Socket parameter (struct sockparams) entries represent the socket types
458348SEric.Yu@Sun.COM  * available on the system.
468348SEric.Yu@Sun.COM  *
478348SEric.Yu@Sun.COM  * Flags (sp_flags):
488348SEric.Yu@Sun.COM  *
498348SEric.Yu@Sun.COM  * SOCKPARAMS_EPHEMERAL: A temporary sockparams entry that will be deleted
508348SEric.Yu@Sun.COM  * as soon as its' ref count drops to zero. In addition, ephemeral entries will
518348SEric.Yu@Sun.COM  * never be hooked onto the global sockparams list. Ephemeral entries are
528348SEric.Yu@Sun.COM  * created when application requests to create a socket using an application
538348SEric.Yu@Sun.COM  * supplied device path, or when a socket is falling back to TPI.
548348SEric.Yu@Sun.COM  *
558348SEric.Yu@Sun.COM  * Lock order:
568348SEric.Yu@Sun.COM  *   The lock order is splist_lock -> sp_lock.
578348SEric.Yu@Sun.COM  *   The lock order is sp_ephem_lock -> sp_lock.
588348SEric.Yu@Sun.COM  */
598348SEric.Yu@Sun.COM extern int 	kobj_path_exists(char *, int);
608348SEric.Yu@Sun.COM extern void	nl7c_init(void);
618348SEric.Yu@Sun.COM extern int	sockfs_defer_nl7c_init;
628348SEric.Yu@Sun.COM 
638348SEric.Yu@Sun.COM static int 	sockparams_sdev_init(struct sockparams *, char *, int);
648348SEric.Yu@Sun.COM static void 	sockparams_sdev_fini(struct sockparams *);
658348SEric.Yu@Sun.COM 
668348SEric.Yu@Sun.COM /*
678348SEric.Yu@Sun.COM  * Global sockparams list (populated via soconfig(1M)).
688348SEric.Yu@Sun.COM  */
698348SEric.Yu@Sun.COM static list_t sphead;
708348SEric.Yu@Sun.COM static krwlock_t splist_lock;
718348SEric.Yu@Sun.COM 
728348SEric.Yu@Sun.COM /*
738348SEric.Yu@Sun.COM  * List of ephemeral sockparams.
748348SEric.Yu@Sun.COM  */
758348SEric.Yu@Sun.COM static list_t sp_ephem_list;
768348SEric.Yu@Sun.COM static krwlock_t sp_ephem_lock;
778348SEric.Yu@Sun.COM 
788964SAnders.Persson@Sun.COM /* Global kstats for sockparams */
798964SAnders.Persson@Sun.COM typedef struct sockparams_g_stats {
808964SAnders.Persson@Sun.COM 	kstat_named_t spgs_ephem_nalloc;
818964SAnders.Persson@Sun.COM 	kstat_named_t spgs_ephem_nreuse;
828964SAnders.Persson@Sun.COM } sockparams_g_stats_t;
838964SAnders.Persson@Sun.COM 
848964SAnders.Persson@Sun.COM static sockparams_g_stats_t sp_g_stats;
858964SAnders.Persson@Sun.COM static kstat_t *sp_g_kstat;
868964SAnders.Persson@Sun.COM 
878964SAnders.Persson@Sun.COM 
888348SEric.Yu@Sun.COM void
898348SEric.Yu@Sun.COM sockparams_init(void)
908348SEric.Yu@Sun.COM {
918348SEric.Yu@Sun.COM 	list_create(&sphead, sizeof (struct sockparams),
928348SEric.Yu@Sun.COM 	    offsetof(struct sockparams, sp_node));
938348SEric.Yu@Sun.COM 	list_create(&sp_ephem_list, sizeof (struct sockparams),
948348SEric.Yu@Sun.COM 	    offsetof(struct sockparams, sp_node));
958348SEric.Yu@Sun.COM 
968348SEric.Yu@Sun.COM 	rw_init(&splist_lock, NULL, RW_DEFAULT, NULL);
978348SEric.Yu@Sun.COM 	rw_init(&sp_ephem_lock, NULL, RW_DEFAULT, NULL);
988964SAnders.Persson@Sun.COM 
998964SAnders.Persson@Sun.COM 	kstat_named_init(&sp_g_stats.spgs_ephem_nalloc, "ephemeral_nalloc",
1008964SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
1018964SAnders.Persson@Sun.COM 	kstat_named_init(&sp_g_stats.spgs_ephem_nreuse, "ephemeral_nreuse",
1028964SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
1038964SAnders.Persson@Sun.COM 
1048964SAnders.Persson@Sun.COM 	sp_g_kstat = kstat_create("sockfs", 0, "sockparams", "misc",
1058964SAnders.Persson@Sun.COM 	    KSTAT_TYPE_NAMED, sizeof (sp_g_stats) / sizeof (kstat_named_t),
1068964SAnders.Persson@Sun.COM 	    KSTAT_FLAG_VIRTUAL);
1078964SAnders.Persson@Sun.COM 	if (sp_g_kstat == NULL)
1088964SAnders.Persson@Sun.COM 		return;
1098964SAnders.Persson@Sun.COM 
1108964SAnders.Persson@Sun.COM 	sp_g_kstat->ks_data = &sp_g_stats;
1118964SAnders.Persson@Sun.COM 
1128964SAnders.Persson@Sun.COM 	kstat_install(sp_g_kstat);
1138964SAnders.Persson@Sun.COM }
1148964SAnders.Persson@Sun.COM 
1158964SAnders.Persson@Sun.COM static int
1168964SAnders.Persson@Sun.COM sockparams_kstat_update(kstat_t *ksp, int rw)
1178964SAnders.Persson@Sun.COM {
1188964SAnders.Persson@Sun.COM 	struct sockparams *sp = ksp->ks_private;
1198964SAnders.Persson@Sun.COM 	sockparams_stats_t *sps = ksp->ks_data;
1208964SAnders.Persson@Sun.COM 
1218964SAnders.Persson@Sun.COM 	if (rw == KSTAT_WRITE)
1228964SAnders.Persson@Sun.COM 		return (EACCES);
1238964SAnders.Persson@Sun.COM 
1248964SAnders.Persson@Sun.COM 	sps->sps_nactive.value.ui64 = sp->sp_refcnt;
1258964SAnders.Persson@Sun.COM 
1268964SAnders.Persson@Sun.COM 	return (0);
1278964SAnders.Persson@Sun.COM }
1288964SAnders.Persson@Sun.COM 
1298964SAnders.Persson@Sun.COM /*
1308964SAnders.Persson@Sun.COM  * Setup kstats for the given sockparams entry.
1318964SAnders.Persson@Sun.COM  */
1328964SAnders.Persson@Sun.COM static void
1338964SAnders.Persson@Sun.COM sockparams_kstat_init(struct sockparams *sp)
1348964SAnders.Persson@Sun.COM {
1358964SAnders.Persson@Sun.COM 	char name[KSTAT_STRLEN];
1368964SAnders.Persson@Sun.COM 
1378964SAnders.Persson@Sun.COM 	(void) snprintf(name, KSTAT_STRLEN, "socket_%d_%d_%d", sp->sp_family,
1388964SAnders.Persson@Sun.COM 	    sp->sp_type, sp->sp_protocol);
1398964SAnders.Persson@Sun.COM 
1408964SAnders.Persson@Sun.COM 	sp->sp_kstat = kstat_create("sockfs", 0, name, "misc", KSTAT_TYPE_NAMED,
1418964SAnders.Persson@Sun.COM 	    sizeof (sockparams_stats_t) / sizeof (kstat_named_t),
1428964SAnders.Persson@Sun.COM 	    KSTAT_FLAG_VIRTUAL);
1438964SAnders.Persson@Sun.COM 
1448964SAnders.Persson@Sun.COM 	if (sp->sp_kstat == NULL)
1458964SAnders.Persson@Sun.COM 		return;
1468964SAnders.Persson@Sun.COM 
1478964SAnders.Persson@Sun.COM 	sp->sp_kstat->ks_data = &sp->sp_stats;
1488964SAnders.Persson@Sun.COM 	sp->sp_kstat->ks_update = sockparams_kstat_update;
1498964SAnders.Persson@Sun.COM 	sp->sp_kstat->ks_private = sp;
1508964SAnders.Persson@Sun.COM 	kstat_install(sp->sp_kstat);
1518964SAnders.Persson@Sun.COM }
1528964SAnders.Persson@Sun.COM 
1538964SAnders.Persson@Sun.COM static void
1548964SAnders.Persson@Sun.COM sockparams_kstat_fini(struct sockparams *sp)
1558964SAnders.Persson@Sun.COM {
1568964SAnders.Persson@Sun.COM 	if (sp->sp_kstat != NULL) {
1578964SAnders.Persson@Sun.COM 		kstat_delete(sp->sp_kstat);
1588964SAnders.Persson@Sun.COM 		sp->sp_kstat = NULL;
1598964SAnders.Persson@Sun.COM 	}
1608348SEric.Yu@Sun.COM }
1618348SEric.Yu@Sun.COM 
1628348SEric.Yu@Sun.COM /*
1638348SEric.Yu@Sun.COM  * sockparams_create(int family, int type, int protocol, char *modname,
1648348SEric.Yu@Sun.COM  *     char *devpath, int devpathlen, int flags, int kmflags, int *errorp)
1658348SEric.Yu@Sun.COM  *
1668348SEric.Yu@Sun.COM  * Create a new sockparams entry.
1678348SEric.Yu@Sun.COM  *
1688348SEric.Yu@Sun.COM  * Arguments:
1698348SEric.Yu@Sun.COM  *   family, type, protocol: specifies the socket type
1708348SEric.Yu@Sun.COM  *   modname: Name of the module associated with the socket type. The
1718348SEric.Yu@Sun.COM  *            module can be NULL if a device path is given, in which
1728348SEric.Yu@Sun.COM  *            case the TPI module is used.
1738348SEric.Yu@Sun.COM  *   devpath: Path to the STREAMS device. May be NULL for non-STREAMS
1748348SEric.Yu@Sun.COM  *            based transports, or those transports that do not provide
1758348SEric.Yu@Sun.COM  *            the capability to fallback to STREAMS.
1768348SEric.Yu@Sun.COM  *   devpathlen: Length of the devpath string. The argument can be 0,
1778348SEric.Yu@Sun.COM  *            indicating that devpath was allocated statically, and should
1788348SEric.Yu@Sun.COM  *            not be freed when the sockparams entry is destroyed.
1798348SEric.Yu@Sun.COM  *
1808348SEric.Yu@Sun.COM  *   flags  : SOCKPARAMS_EPHEMERAL is the only flag that is allowed.
1818348SEric.Yu@Sun.COM  *   kmflags: KM_{NO,}SLEEP
1828348SEric.Yu@Sun.COM  *   errorp : Value-return argument, set when an error occurs.
1838348SEric.Yu@Sun.COM  *
1848348SEric.Yu@Sun.COM  * Returns:
1858348SEric.Yu@Sun.COM  *   On success a new sockparams entry is returned, and *errorp is set
1868348SEric.Yu@Sun.COM  *   to 0. On failure NULL is returned and *errorp is set to indicate the
1878348SEric.Yu@Sun.COM  *   type of error that occured.
1888348SEric.Yu@Sun.COM  *
1898348SEric.Yu@Sun.COM  * Notes:
1908348SEric.Yu@Sun.COM  *   devpath and modname are freed upon failure.
1918348SEric.Yu@Sun.COM  */
1928348SEric.Yu@Sun.COM struct sockparams *
1938348SEric.Yu@Sun.COM sockparams_create(int family, int type, int protocol, char *modname,
1948348SEric.Yu@Sun.COM     char *devpath, int devpathlen, int flags, int kmflags, int *errorp)
1958348SEric.Yu@Sun.COM {
1968348SEric.Yu@Sun.COM 	struct sockparams *sp = NULL;
1978348SEric.Yu@Sun.COM 	size_t size;
1988348SEric.Yu@Sun.COM 
1998348SEric.Yu@Sun.COM 	ASSERT((flags & ~SOCKPARAMS_EPHEMERAL) == 0);
2008348SEric.Yu@Sun.COM 	if (flags & ~SOCKPARAMS_EPHEMERAL) {
2018348SEric.Yu@Sun.COM 		*errorp = EINVAL;
2028348SEric.Yu@Sun.COM 		goto error;
2038348SEric.Yu@Sun.COM 	}
2048348SEric.Yu@Sun.COM 
2058348SEric.Yu@Sun.COM 	/* either a module or device must be given */
2068348SEric.Yu@Sun.COM 	if (modname == NULL && devpath == NULL) {
2078348SEric.Yu@Sun.COM 		*errorp = EINVAL;
2088348SEric.Yu@Sun.COM 		goto error;
2098348SEric.Yu@Sun.COM 	}
2108348SEric.Yu@Sun.COM 
2118348SEric.Yu@Sun.COM 	sp = kmem_zalloc(sizeof (*sp), kmflags);
2128348SEric.Yu@Sun.COM 	if (sp == NULL) {
2138348SEric.Yu@Sun.COM 		*errorp = ENOMEM;
2148348SEric.Yu@Sun.COM 		goto error;
2158348SEric.Yu@Sun.COM 	}
2168348SEric.Yu@Sun.COM 	sp->sp_family = family;
2178348SEric.Yu@Sun.COM 	sp->sp_type = type;
2188348SEric.Yu@Sun.COM 	sp->sp_protocol = protocol;
2198348SEric.Yu@Sun.COM 	sp->sp_refcnt = 0;
2208348SEric.Yu@Sun.COM 	sp->sp_flags = flags;
2218348SEric.Yu@Sun.COM 
2228981SAnders.Persson@Sun.COM 	kstat_named_init(&sp->sp_stats.sps_nfallback, "nfallback",
2238981SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
2248981SAnders.Persson@Sun.COM 	kstat_named_init(&sp->sp_stats.sps_nactive, "nactive",
2258981SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
2268981SAnders.Persson@Sun.COM 	kstat_named_init(&sp->sp_stats.sps_ncreate, "ncreate",
2278981SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
2288981SAnders.Persson@Sun.COM 
2298964SAnders.Persson@Sun.COM 	/*
2308981SAnders.Persson@Sun.COM 	 * Track how many ephemeral entries we have created.
2318964SAnders.Persson@Sun.COM 	 */
2328964SAnders.Persson@Sun.COM 	if (sp->sp_flags & SOCKPARAMS_EPHEMERAL)
2338964SAnders.Persson@Sun.COM 		sp_g_stats.spgs_ephem_nalloc.value.ui64++;
2348964SAnders.Persson@Sun.COM 
2358348SEric.Yu@Sun.COM 	if (modname != NULL) {
2368348SEric.Yu@Sun.COM 		sp->sp_smod_name = modname;
2378348SEric.Yu@Sun.COM 	} else {
2388348SEric.Yu@Sun.COM 		size = strlen(SOTPI_SMOD_NAME) + 1;
2398348SEric.Yu@Sun.COM 		modname = kmem_zalloc(size, kmflags);
2408348SEric.Yu@Sun.COM 		if (modname == NULL) {
2418348SEric.Yu@Sun.COM 			*errorp = ENOMEM;
2428348SEric.Yu@Sun.COM 			goto error;
2438348SEric.Yu@Sun.COM 		}
2448348SEric.Yu@Sun.COM 		sp->sp_smod_name = modname;
2458348SEric.Yu@Sun.COM 		(void) sprintf(sp->sp_smod_name, "%s", SOTPI_SMOD_NAME);
2468348SEric.Yu@Sun.COM 	}
2478348SEric.Yu@Sun.COM 
2488348SEric.Yu@Sun.COM 	if (devpath != NULL) {
2498348SEric.Yu@Sun.COM 		/* Set up the device entry. */
2508348SEric.Yu@Sun.COM 		*errorp = sockparams_sdev_init(sp, devpath, devpathlen);
2518348SEric.Yu@Sun.COM 		if (*errorp != 0)
2528348SEric.Yu@Sun.COM 			goto error;
2538348SEric.Yu@Sun.COM 	}
2548348SEric.Yu@Sun.COM 
2558348SEric.Yu@Sun.COM 	mutex_init(&sp->sp_lock, NULL, MUTEX_DEFAULT, NULL);
2568348SEric.Yu@Sun.COM 	*errorp = 0;
2578348SEric.Yu@Sun.COM 	return (sp);
2588348SEric.Yu@Sun.COM error:
2598348SEric.Yu@Sun.COM 	ASSERT(*errorp != 0);
2608348SEric.Yu@Sun.COM 	if (modname != NULL)
2618348SEric.Yu@Sun.COM 		kmem_free(modname, strlen(modname) + 1);
2628348SEric.Yu@Sun.COM 	if (devpathlen != 0)
2638348SEric.Yu@Sun.COM 		kmem_free(devpath, devpathlen);
2648981SAnders.Persson@Sun.COM 	if (sp != NULL)
2658348SEric.Yu@Sun.COM 		kmem_free(sp, sizeof (*sp));
2668348SEric.Yu@Sun.COM 	return (NULL);
2678348SEric.Yu@Sun.COM }
2688348SEric.Yu@Sun.COM 
2698348SEric.Yu@Sun.COM /*
2708348SEric.Yu@Sun.COM  * Initialize the STREAMS device aspect of the sockparams entry.
2718348SEric.Yu@Sun.COM  */
2728348SEric.Yu@Sun.COM static int
2738348SEric.Yu@Sun.COM sockparams_sdev_init(struct sockparams *sp, char *devpath, int devpathlen)
2748348SEric.Yu@Sun.COM {
2758348SEric.Yu@Sun.COM 	vnode_t *vp = NULL;
2768348SEric.Yu@Sun.COM 	int error;
2778348SEric.Yu@Sun.COM 
2788348SEric.Yu@Sun.COM 	ASSERT(devpath != NULL);
2798348SEric.Yu@Sun.COM 
2808348SEric.Yu@Sun.COM 	if ((error = sogetvp(devpath, &vp, UIO_SYSSPACE)) != 0) {
2818348SEric.Yu@Sun.COM 		dprint(0, ("sockparams_sdev_init: vp %s failed with %d\n",
2828348SEric.Yu@Sun.COM 		    devpath, error));
2838348SEric.Yu@Sun.COM 		return (error);
2848348SEric.Yu@Sun.COM 	}
2858348SEric.Yu@Sun.COM 
2868348SEric.Yu@Sun.COM 	ASSERT(vp != NULL);
2878348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_vnode = vp;
2888348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_devpath = devpath;
2898348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_devpathlen = devpathlen;
2908348SEric.Yu@Sun.COM 
2918348SEric.Yu@Sun.COM 	return (0);
2928348SEric.Yu@Sun.COM }
2938348SEric.Yu@Sun.COM 
2948348SEric.Yu@Sun.COM /*
2958348SEric.Yu@Sun.COM  * sockparams_destroy(struct sockparams *sp)
2968348SEric.Yu@Sun.COM  *
2978348SEric.Yu@Sun.COM  * Releases all the resources associated with the sockparams entry,
2988348SEric.Yu@Sun.COM  * and frees the sockparams entry.
2998348SEric.Yu@Sun.COM  *
3008348SEric.Yu@Sun.COM  * Arguments:
3018348SEric.Yu@Sun.COM  *   sp: the sockparams entry to destroy.
3028348SEric.Yu@Sun.COM  *
3038348SEric.Yu@Sun.COM  * Returns:
3048348SEric.Yu@Sun.COM  *   Nothing.
3058348SEric.Yu@Sun.COM  *
3068348SEric.Yu@Sun.COM  * Locking:
3078348SEric.Yu@Sun.COM  *   The sp_lock of the entry can not be held.
3088348SEric.Yu@Sun.COM  */
3098348SEric.Yu@Sun.COM void
3108348SEric.Yu@Sun.COM sockparams_destroy(struct sockparams *sp)
3118348SEric.Yu@Sun.COM {
3128348SEric.Yu@Sun.COM 	ASSERT(sp->sp_refcnt == 0);
3138348SEric.Yu@Sun.COM 	ASSERT(!list_link_active(&sp->sp_node));
3148348SEric.Yu@Sun.COM 
3158348SEric.Yu@Sun.COM 	sockparams_sdev_fini(sp);
3168348SEric.Yu@Sun.COM 
3178348SEric.Yu@Sun.COM 	if (sp->sp_smod_info != NULL)
318*12257SAnders.Persson@Sun.COM 		SMOD_DEC_REF(sp->sp_smod_info, sp->sp_smod_name);
3198348SEric.Yu@Sun.COM 	kmem_free(sp->sp_smod_name, strlen(sp->sp_smod_name) + 1);
3208348SEric.Yu@Sun.COM 	sp->sp_smod_name = NULL;
3218348SEric.Yu@Sun.COM 	sp->sp_smod_info = NULL;
3228348SEric.Yu@Sun.COM 	mutex_destroy(&sp->sp_lock);
3238964SAnders.Persson@Sun.COM 	sockparams_kstat_fini(sp);
3248348SEric.Yu@Sun.COM 
3258348SEric.Yu@Sun.COM 	kmem_free(sp, sizeof (*sp));
3268348SEric.Yu@Sun.COM }
3278348SEric.Yu@Sun.COM 
3288348SEric.Yu@Sun.COM /*
3298348SEric.Yu@Sun.COM  * Clean up the STREAMS device part of the sockparams entry.
3308348SEric.Yu@Sun.COM  */
3318348SEric.Yu@Sun.COM static void
3328348SEric.Yu@Sun.COM sockparams_sdev_fini(struct sockparams *sp)
3338348SEric.Yu@Sun.COM {
3348348SEric.Yu@Sun.COM 	sdev_info_t sd;
3358348SEric.Yu@Sun.COM 
3368348SEric.Yu@Sun.COM 	/*
3378348SEric.Yu@Sun.COM 	 * if the entry does not have a STREAMS device, then there
3388348SEric.Yu@Sun.COM 	 * is nothing to do.
3398348SEric.Yu@Sun.COM 	 */
3408348SEric.Yu@Sun.COM 	if (!SOCKPARAMS_HAS_DEVICE(sp))
3418348SEric.Yu@Sun.COM 		return;
3428348SEric.Yu@Sun.COM 
3438348SEric.Yu@Sun.COM 	sd = sp->sp_sdev_info;
3448348SEric.Yu@Sun.COM 	if (sd.sd_vnode != NULL)
3458348SEric.Yu@Sun.COM 		VN_RELE(sd.sd_vnode);
3468348SEric.Yu@Sun.COM 	if (sd.sd_devpathlen != 0)
3478348SEric.Yu@Sun.COM 		kmem_free(sd.sd_devpath, sd.sd_devpathlen);
3488348SEric.Yu@Sun.COM 
3498348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_vnode = NULL;
3508348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_devpath = NULL;
3518348SEric.Yu@Sun.COM }
3528348SEric.Yu@Sun.COM 
3538348SEric.Yu@Sun.COM /*
3548348SEric.Yu@Sun.COM  * Look for a matching sockparams entry on the given list.
3558348SEric.Yu@Sun.COM  * The caller must hold the associated list lock.
3568348SEric.Yu@Sun.COM  */
3578348SEric.Yu@Sun.COM static struct sockparams *
3588348SEric.Yu@Sun.COM sockparams_find(list_t *list, int family, int type, int protocol,
3598489Sshenjian     boolean_t by_devpath, const char *name)
3608348SEric.Yu@Sun.COM {
3618348SEric.Yu@Sun.COM 	struct sockparams *sp;
3628348SEric.Yu@Sun.COM 
3638348SEric.Yu@Sun.COM 	for (sp = list_head(list); sp != NULL; sp = list_next(list, sp)) {
3648489Sshenjian 		if (sp->sp_family == family && sp->sp_type == type) {
3658348SEric.Yu@Sun.COM 			if (sp->sp_protocol == protocol) {
3668489Sshenjian 				if (name == NULL)
3678348SEric.Yu@Sun.COM 					break;
3688489Sshenjian 				else if (by_devpath &&
3698348SEric.Yu@Sun.COM 				    sp->sp_sdev_info.sd_devpath != NULL &&
3708348SEric.Yu@Sun.COM 				    strcmp(sp->sp_sdev_info.sd_devpath,
3718348SEric.Yu@Sun.COM 				    name) == 0)
3728348SEric.Yu@Sun.COM 					break;
3738489Sshenjian 				else if (strcmp(sp->sp_smod_name, name) == 0)
3748348SEric.Yu@Sun.COM 					break;
3758348SEric.Yu@Sun.COM 			}
3768348SEric.Yu@Sun.COM 		}
3778348SEric.Yu@Sun.COM 	}
3788489Sshenjian 	return (sp);
3798348SEric.Yu@Sun.COM }
3808348SEric.Yu@Sun.COM 
3818348SEric.Yu@Sun.COM /*
3828348SEric.Yu@Sun.COM  * sockparams_hold_ephemeral()
3838348SEric.Yu@Sun.COM  *
3848348SEric.Yu@Sun.COM  * Returns an ephemeral sockparams entry of the requested family, type and
3858348SEric.Yu@Sun.COM  * protocol. The entry is returned held, and the caller is responsible for
3868348SEric.Yu@Sun.COM  * dropping the reference using SOCKPARAMS_DEC_REF() once done.
3878348SEric.Yu@Sun.COM  *
3888348SEric.Yu@Sun.COM  * All ephemeral entries are on list (sp_ephem_list). If there is an
3898348SEric.Yu@Sun.COM  * entry on the list that match the search criteria, then a reference is
3908348SEric.Yu@Sun.COM  * placed on that entry. Otherwise, a new entry is created and inserted
3918348SEric.Yu@Sun.COM  * in the list. The entry is removed from the list when the last reference
3928348SEric.Yu@Sun.COM  * is dropped.
3938348SEric.Yu@Sun.COM  *
3948348SEric.Yu@Sun.COM  * The tpi flag is used to determine whether name refers to a device or
3958348SEric.Yu@Sun.COM  * module name.
3968348SEric.Yu@Sun.COM  */
3978348SEric.Yu@Sun.COM static struct sockparams *
3988348SEric.Yu@Sun.COM sockparams_hold_ephemeral(int family, int type, int protocol,
3998489Sshenjian     const char *name, boolean_t by_devpath, int kmflag, int *errorp)
4008348SEric.Yu@Sun.COM {
4018348SEric.Yu@Sun.COM 	struct sockparams *sp = NULL;
4028348SEric.Yu@Sun.COM 	*errorp = 0;
4038348SEric.Yu@Sun.COM 
4048348SEric.Yu@Sun.COM 	/*
4058348SEric.Yu@Sun.COM 	 * First look for an existing entry
4068348SEric.Yu@Sun.COM 	 */
4078348SEric.Yu@Sun.COM 	rw_enter(&sp_ephem_lock, RW_READER);
4088348SEric.Yu@Sun.COM 	sp = sockparams_find(&sp_ephem_list, family, type, protocol,
4098489Sshenjian 	    by_devpath, name);
4108348SEric.Yu@Sun.COM 	if (sp != NULL) {
4118348SEric.Yu@Sun.COM 		SOCKPARAMS_INC_REF(sp);
4128348SEric.Yu@Sun.COM 		rw_exit(&sp_ephem_lock);
4138964SAnders.Persson@Sun.COM 		sp_g_stats.spgs_ephem_nreuse.value.ui64++;
4148348SEric.Yu@Sun.COM 
4158348SEric.Yu@Sun.COM 		return (sp);
4168348SEric.Yu@Sun.COM 	} else {
4178348SEric.Yu@Sun.COM 		struct sockparams *newsp = NULL;
4188348SEric.Yu@Sun.COM 		char *namebuf = NULL;
4198348SEric.Yu@Sun.COM 		int namelen = 0;
4208348SEric.Yu@Sun.COM 
4218348SEric.Yu@Sun.COM 		rw_exit(&sp_ephem_lock);
4228348SEric.Yu@Sun.COM 
4238348SEric.Yu@Sun.COM 		namelen = strlen(name) + 1;
4248348SEric.Yu@Sun.COM 		namebuf = kmem_alloc(namelen, kmflag);
4258348SEric.Yu@Sun.COM 		if (namebuf == NULL) {
4268348SEric.Yu@Sun.COM 			*errorp = ENOMEM;
4278348SEric.Yu@Sun.COM 			return (NULL);
4288348SEric.Yu@Sun.COM 		}
4298348SEric.Yu@Sun.COM 
4308348SEric.Yu@Sun.COM 		(void *)strncpy(namebuf, name, namelen);
4318489Sshenjian 		if (by_devpath) {
4328348SEric.Yu@Sun.COM 			newsp = sockparams_create(family, type,
4338348SEric.Yu@Sun.COM 			    protocol, NULL, namebuf, namelen,
4348348SEric.Yu@Sun.COM 			    SOCKPARAMS_EPHEMERAL, kmflag, errorp);
4358348SEric.Yu@Sun.COM 		} else {
4368348SEric.Yu@Sun.COM 			newsp = sockparams_create(family, type,
4378348SEric.Yu@Sun.COM 			    protocol, namebuf, NULL, 0,
4388348SEric.Yu@Sun.COM 			    SOCKPARAMS_EPHEMERAL, kmflag, errorp);
4398348SEric.Yu@Sun.COM 		}
4408348SEric.Yu@Sun.COM 
4418348SEric.Yu@Sun.COM 		if (newsp == NULL) {
4428348SEric.Yu@Sun.COM 			ASSERT(*errorp != 0);
4438348SEric.Yu@Sun.COM 			return (NULL);
4448348SEric.Yu@Sun.COM 		}
4458348SEric.Yu@Sun.COM 
4468348SEric.Yu@Sun.COM 		/*
4478348SEric.Yu@Sun.COM 		 * Time to load the socket module.
4488348SEric.Yu@Sun.COM 		 */
4498348SEric.Yu@Sun.COM 		ASSERT(newsp->sp_smod_info == NULL);
4508348SEric.Yu@Sun.COM 		newsp->sp_smod_info =
4518348SEric.Yu@Sun.COM 		    smod_lookup_byname(newsp->sp_smod_name);
4528348SEric.Yu@Sun.COM 		if (newsp->sp_smod_info == NULL) {
4538348SEric.Yu@Sun.COM 			/* Failed to load */
4548348SEric.Yu@Sun.COM 			sockparams_destroy(newsp);
4558348SEric.Yu@Sun.COM 			*errorp = ENXIO;
4568348SEric.Yu@Sun.COM 			return (NULL);
4578348SEric.Yu@Sun.COM 		}
4588348SEric.Yu@Sun.COM 
4598348SEric.Yu@Sun.COM 		/*
4608348SEric.Yu@Sun.COM 		 * The sockparams entry was created, now try to add it
4618348SEric.Yu@Sun.COM 		 * to the list. We need to hold the lock as a WRITER.
4628348SEric.Yu@Sun.COM 		 */
4638348SEric.Yu@Sun.COM 		rw_enter(&sp_ephem_lock, RW_WRITER);
4648348SEric.Yu@Sun.COM 		sp = sockparams_find(&sp_ephem_list, family, type, protocol,
4658489Sshenjian 		    by_devpath, name);
4668348SEric.Yu@Sun.COM 		if (sp != NULL) {
4678348SEric.Yu@Sun.COM 			/*
4688348SEric.Yu@Sun.COM 			 * Someone has requested a matching entry, so just
4698348SEric.Yu@Sun.COM 			 * place a hold on it and release the entry we alloc'ed.
4708348SEric.Yu@Sun.COM 			 */
4718348SEric.Yu@Sun.COM 			SOCKPARAMS_INC_REF(sp);
4728348SEric.Yu@Sun.COM 			rw_exit(&sp_ephem_lock);
4738348SEric.Yu@Sun.COM 
4748348SEric.Yu@Sun.COM 			sockparams_destroy(newsp);
4758348SEric.Yu@Sun.COM 		} else {
4768348SEric.Yu@Sun.COM 			SOCKPARAMS_INC_REF(newsp);
4778348SEric.Yu@Sun.COM 			list_insert_tail(&sp_ephem_list, newsp);
4788348SEric.Yu@Sun.COM 			rw_exit(&sp_ephem_lock);
4798348SEric.Yu@Sun.COM 
4808348SEric.Yu@Sun.COM 			sp = newsp;
4818348SEric.Yu@Sun.COM 		}
4828348SEric.Yu@Sun.COM 		ASSERT(*errorp == 0);
4838348SEric.Yu@Sun.COM 
4848348SEric.Yu@Sun.COM 		return (sp);
4858348SEric.Yu@Sun.COM 	}
4868348SEric.Yu@Sun.COM }
4878348SEric.Yu@Sun.COM 
4888348SEric.Yu@Sun.COM struct sockparams *
4898348SEric.Yu@Sun.COM sockparams_hold_ephemeral_bydev(int family, int type, int protocol,
4908348SEric.Yu@Sun.COM     const char *dev, int kmflag, int *errorp)
4918348SEric.Yu@Sun.COM {
4928348SEric.Yu@Sun.COM 	return (sockparams_hold_ephemeral(family, type, protocol, dev, B_TRUE,
4938348SEric.Yu@Sun.COM 	    kmflag, errorp));
4948348SEric.Yu@Sun.COM }
4958348SEric.Yu@Sun.COM 
4968348SEric.Yu@Sun.COM struct sockparams *
4978348SEric.Yu@Sun.COM sockparams_hold_ephemeral_bymod(int family, int type, int protocol,
4988348SEric.Yu@Sun.COM     const char *mod, int kmflag, int *errorp)
4998348SEric.Yu@Sun.COM {
5008348SEric.Yu@Sun.COM 	return (sockparams_hold_ephemeral(family, type, protocol, mod, B_FALSE,
5018348SEric.Yu@Sun.COM 	    kmflag, errorp));
5028348SEric.Yu@Sun.COM }
5038348SEric.Yu@Sun.COM 
5048348SEric.Yu@Sun.COM /*
5058348SEric.Yu@Sun.COM  * Called when the last socket using the ephemeral entry is dropping
5068348SEric.Yu@Sun.COM  * its' reference. To maintain lock order we must drop the sockparams
5078348SEric.Yu@Sun.COM  * lock before calling this function. As a result, a new reference
5088348SEric.Yu@Sun.COM  * might be placed on the entry, in which case there is nothing to
5098348SEric.Yu@Sun.COM  * do. However, if ref count goes to zero, we delete the entry.
5108348SEric.Yu@Sun.COM  */
5118348SEric.Yu@Sun.COM void
5128348SEric.Yu@Sun.COM sockparams_ephemeral_drop_last_ref(struct sockparams *sp)
5138348SEric.Yu@Sun.COM {
5148348SEric.Yu@Sun.COM 	ASSERT(sp->sp_flags & SOCKPARAMS_EPHEMERAL);
5158348SEric.Yu@Sun.COM 	ASSERT(MUTEX_NOT_HELD(&sp->sp_lock));
5168348SEric.Yu@Sun.COM 
5178348SEric.Yu@Sun.COM 	rw_enter(&sp_ephem_lock, RW_WRITER);
5188348SEric.Yu@Sun.COM 	mutex_enter(&sp->sp_lock);
5198348SEric.Yu@Sun.COM 
5208348SEric.Yu@Sun.COM 	if (--sp->sp_refcnt == 0) {
5218348SEric.Yu@Sun.COM 		list_remove(&sp_ephem_list, sp);
5228348SEric.Yu@Sun.COM 		mutex_exit(&sp->sp_lock);
5238348SEric.Yu@Sun.COM 		rw_exit(&sp_ephem_lock);
5248348SEric.Yu@Sun.COM 
5258348SEric.Yu@Sun.COM 		sockparams_destroy(sp);
5268348SEric.Yu@Sun.COM 	} else {
5278348SEric.Yu@Sun.COM 		mutex_exit(&sp->sp_lock);
5288348SEric.Yu@Sun.COM 		rw_exit(&sp_ephem_lock);
5298348SEric.Yu@Sun.COM 	}
5308348SEric.Yu@Sun.COM }
5318348SEric.Yu@Sun.COM 
5328348SEric.Yu@Sun.COM /*
5338348SEric.Yu@Sun.COM  * sockparams_add(struct sockparams *sp)
5348348SEric.Yu@Sun.COM  *
5358348SEric.Yu@Sun.COM  * Tries to add the given sockparams entry to the global list.
5368348SEric.Yu@Sun.COM  *
5378348SEric.Yu@Sun.COM  * Arguments:
5388348SEric.Yu@Sun.COM  *   sp: the sockparms entry to add
5398348SEric.Yu@Sun.COM  *
5408348SEric.Yu@Sun.COM  * Returns:
5418348SEric.Yu@Sun.COM  *   On success 0, but if an entry already exists, then EEXIST
5428348SEric.Yu@Sun.COM  *   is returned.
5438348SEric.Yu@Sun.COM  *
5448348SEric.Yu@Sun.COM  * Locking:
5458348SEric.Yu@Sun.COM  *   The caller can not be holding splist_lock.
5468348SEric.Yu@Sun.COM  */
5478348SEric.Yu@Sun.COM static int
5488348SEric.Yu@Sun.COM sockparams_add(struct sockparams *sp)
5498348SEric.Yu@Sun.COM {
5508348SEric.Yu@Sun.COM 	ASSERT(!(sp->sp_flags & SOCKPARAMS_EPHEMERAL));
5518348SEric.Yu@Sun.COM 
5528348SEric.Yu@Sun.COM 	rw_enter(&splist_lock, RW_WRITER);
5538348SEric.Yu@Sun.COM 	if (sockparams_find(&sphead, sp->sp_family, sp->sp_type,
5548489Sshenjian 	    sp->sp_protocol, B_TRUE, NULL) != 0) {
5558348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
5568348SEric.Yu@Sun.COM 		return (EEXIST);
5578348SEric.Yu@Sun.COM 	} else {
5588348SEric.Yu@Sun.COM 		list_insert_tail(&sphead, sp);
5598348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
5608348SEric.Yu@Sun.COM 		return (0);
5618348SEric.Yu@Sun.COM 	}
5628348SEric.Yu@Sun.COM }
5638348SEric.Yu@Sun.COM 
5648348SEric.Yu@Sun.COM /*
5658348SEric.Yu@Sun.COM  * sockparams_delete(int family, int type, int protocol)
5668348SEric.Yu@Sun.COM  *
5678348SEric.Yu@Sun.COM  * Marks the sockparams entry for a specific family, type and protocol
5688348SEric.Yu@Sun.COM  * for deletion. The entry is removed from the list and destroyed
5698348SEric.Yu@Sun.COM  * if no one is holding a reference to it.
5708348SEric.Yu@Sun.COM  *
5718348SEric.Yu@Sun.COM  * Arguments:
5728348SEric.Yu@Sun.COM  *   family, type, protocol: the socket type that should be removed.
5738348SEric.Yu@Sun.COM  *
5748348SEric.Yu@Sun.COM  * Returns:
5758348SEric.Yu@Sun.COM  *   On success 0, otherwise ENXIO.
5768348SEric.Yu@Sun.COM  *
5778348SEric.Yu@Sun.COM  * Locking:
5788348SEric.Yu@Sun.COM  *   Caller can not be holding splist_lock or the sp_lock of
5798348SEric.Yu@Sun.COM  *   any sockparams entry.
5808348SEric.Yu@Sun.COM  */
5818348SEric.Yu@Sun.COM static int
5828348SEric.Yu@Sun.COM sockparams_delete(int family, int type, int protocol)
5838348SEric.Yu@Sun.COM {
5848348SEric.Yu@Sun.COM 	struct sockparams *sp;
5858348SEric.Yu@Sun.COM 
5868348SEric.Yu@Sun.COM 	rw_enter(&splist_lock, RW_WRITER);
5878489Sshenjian 	sp = sockparams_find(&sphead, family, type, protocol, B_TRUE, NULL);
5888348SEric.Yu@Sun.COM 
5898348SEric.Yu@Sun.COM 	if (sp != NULL) {
5908348SEric.Yu@Sun.COM 		/*
5918348SEric.Yu@Sun.COM 		 * If no one is holding a reference to the entry, then
5928348SEric.Yu@Sun.COM 		 * we go ahead and remove it from the list and then
5938348SEric.Yu@Sun.COM 		 * destroy it.
5948348SEric.Yu@Sun.COM 		 */
5958348SEric.Yu@Sun.COM 		mutex_enter(&sp->sp_lock);
5968348SEric.Yu@Sun.COM 		if (sp->sp_refcnt != 0) {
5978348SEric.Yu@Sun.COM 			mutex_exit(&sp->sp_lock);
5988348SEric.Yu@Sun.COM 			rw_exit(&splist_lock);
5998348SEric.Yu@Sun.COM 			return (EBUSY);
6008348SEric.Yu@Sun.COM 		}
6018348SEric.Yu@Sun.COM 		mutex_exit(&sp->sp_lock);
6028348SEric.Yu@Sun.COM 		/* Delete the sockparams entry. */
6038348SEric.Yu@Sun.COM 		list_remove(&sphead, sp);
6048348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
6058348SEric.Yu@Sun.COM 
6068348SEric.Yu@Sun.COM 		sockparams_destroy(sp);
6078348SEric.Yu@Sun.COM 		return (0);
6088348SEric.Yu@Sun.COM 	} else {
6098348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
6108348SEric.Yu@Sun.COM 		return (ENXIO);
6118348SEric.Yu@Sun.COM 	}
6128348SEric.Yu@Sun.COM }
6138348SEric.Yu@Sun.COM 
6148348SEric.Yu@Sun.COM /*
6158348SEric.Yu@Sun.COM  * soconfig(int family, int type, int protocol,
6168348SEric.Yu@Sun.COM  *     char *devpath, int devpathlen, char *module)
6178348SEric.Yu@Sun.COM  *
6188348SEric.Yu@Sun.COM  * Add or delete an entry to the sockparams table.
6198348SEric.Yu@Sun.COM  * When devpath and module both are NULL, it will delete an entry.
6208348SEric.Yu@Sun.COM  *
6218348SEric.Yu@Sun.COM  * Arguments:
6228348SEric.Yu@Sun.COM  *   family, type, protocol: the tuple in question
6238348SEric.Yu@Sun.COM  *   devpath: STREAMS device path. Can be NULL for module based sockets.
6248348SEric.Yu@Sun.COM  *   module : Name of the socket module. Can be NULL for STREAMS
6258348SEric.Yu@Sun.COM  *            based sockets.
6268348SEric.Yu@Sun.COM  *   devpathlen: length of the devpath string, or 0 if devpath
6278348SEric.Yu@Sun.COM  *            was statically allocated.
6288348SEric.Yu@Sun.COM  *
6298348SEric.Yu@Sun.COM  * Note:
6308348SEric.Yu@Sun.COM  *   This routine assumes that the caller has kmem_alloced
6318348SEric.Yu@Sun.COM  *   devpath (if devpathlen > 0) and module for this routine to
6328348SEric.Yu@Sun.COM  *   consume.
6338348SEric.Yu@Sun.COM  */
6348348SEric.Yu@Sun.COM int
6358348SEric.Yu@Sun.COM soconfig(int family, int type, int protocol,
6368348SEric.Yu@Sun.COM     char *devpath, int devpathlen, char *module)
6378348SEric.Yu@Sun.COM {
6388348SEric.Yu@Sun.COM 	struct sockparams *sp;
6398348SEric.Yu@Sun.COM 	int error = 0;
6408348SEric.Yu@Sun.COM 
6418348SEric.Yu@Sun.COM 	dprint(0, ("soconfig(%d,%d,%d,%s,%d,%s)\n",
6428348SEric.Yu@Sun.COM 	    family, type, protocol, devpath, devpathlen,
6438348SEric.Yu@Sun.COM 	    module == NULL ? "NULL" : module));
6448348SEric.Yu@Sun.COM 
6458348SEric.Yu@Sun.COM 	if (sockfs_defer_nl7c_init) {
6468348SEric.Yu@Sun.COM 		nl7c_init();
6478348SEric.Yu@Sun.COM 		sockfs_defer_nl7c_init = 0;
6488348SEric.Yu@Sun.COM 	}
6498348SEric.Yu@Sun.COM 
6508348SEric.Yu@Sun.COM 	if (devpath == NULL && module == NULL) {
6518348SEric.Yu@Sun.COM 		/*
6528348SEric.Yu@Sun.COM 		 * Delete existing entry,
6538348SEric.Yu@Sun.COM 		 * both socket module and STEAMS device.
6548348SEric.Yu@Sun.COM 		 */
6558348SEric.Yu@Sun.COM 		ASSERT(module == NULL);
6568348SEric.Yu@Sun.COM 		error = sockparams_delete(family, type, protocol);
6578348SEric.Yu@Sun.COM 	} else {
6588348SEric.Yu@Sun.COM 		/*
6598348SEric.Yu@Sun.COM 		 * Adding an entry
6608348SEric.Yu@Sun.COM 		 * sockparams_create frees mod name and devpath upon failure.
6618348SEric.Yu@Sun.COM 		 */
6628348SEric.Yu@Sun.COM 		sp = sockparams_create(family, type, protocol, module,
6638348SEric.Yu@Sun.COM 		    devpath, devpathlen, 0, KM_SLEEP, &error);
6648348SEric.Yu@Sun.COM 
6658348SEric.Yu@Sun.COM 		if (sp != NULL) {
6668981SAnders.Persson@Sun.COM 			/*
6678981SAnders.Persson@Sun.COM 			 * The sockparams entry becomes globally visible once
6688981SAnders.Persson@Sun.COM 			 * we call sockparams_add(). So we add a reference so
6698981SAnders.Persson@Sun.COM 			 * we do not have to worry about the entry being
6708981SAnders.Persson@Sun.COM 			 * immediately deleted.
6718981SAnders.Persson@Sun.COM 			 */
6728981SAnders.Persson@Sun.COM 			SOCKPARAMS_INC_REF(sp);
6738348SEric.Yu@Sun.COM 			error = sockparams_add(sp);
6748981SAnders.Persson@Sun.COM 			if (error != 0) {
6758981SAnders.Persson@Sun.COM 				SOCKPARAMS_DEC_REF(sp);
6768348SEric.Yu@Sun.COM 				sockparams_destroy(sp);
6778981SAnders.Persson@Sun.COM 			} else {
6788981SAnders.Persson@Sun.COM 				/*
6798981SAnders.Persson@Sun.COM 				 * Unique sockparams entry, so init the kstats.
6808981SAnders.Persson@Sun.COM 				 */
6818981SAnders.Persson@Sun.COM 				sockparams_kstat_init(sp);
6828981SAnders.Persson@Sun.COM 				SOCKPARAMS_DEC_REF(sp);
6838981SAnders.Persson@Sun.COM 			}
6848348SEric.Yu@Sun.COM 		}
6858348SEric.Yu@Sun.COM 	}
6868348SEric.Yu@Sun.COM 
6878348SEric.Yu@Sun.COM 	return (error);
6888348SEric.Yu@Sun.COM }
6898348SEric.Yu@Sun.COM 
6908348SEric.Yu@Sun.COM /*
6918348SEric.Yu@Sun.COM  * solookup(int family, int type, int protocol, struct sockparams **spp)
6928348SEric.Yu@Sun.COM  *
6938348SEric.Yu@Sun.COM  * Lookup an entry in the sockparams list based on the triple. The returned
6948348SEric.Yu@Sun.COM  * entry either exactly match the given tuple, or it is the 'default' entry
6958348SEric.Yu@Sun.COM  * for the given <family, type>. A default entry is on with a protocol
6968348SEric.Yu@Sun.COM  * value of zero.
6978348SEric.Yu@Sun.COM  *
6988348SEric.Yu@Sun.COM  * Arguments:
6998348SEric.Yu@Sun.COM  *   family, type, protocol: tuple to search for
7008348SEric.Yu@Sun.COM  *   spp: Value-return argument
7018348SEric.Yu@Sun.COM  *
7028348SEric.Yu@Sun.COM  * Returns:
7038348SEric.Yu@Sun.COM  *   If an entry is found, 0 is returned and *spp is set to point to the
7048348SEric.Yu@Sun.COM  *   entry. In case an entry is not found, *spp is set to NULL, and an
7058348SEric.Yu@Sun.COM  *   error code is returned. The errors are (in decreasing precedence):
7068348SEric.Yu@Sun.COM  *	EAFNOSUPPORT - address family not in list
7078348SEric.Yu@Sun.COM  *	EPROTONOSUPPORT - address family supported but not protocol.
7088348SEric.Yu@Sun.COM  *	EPROTOTYPE - address family and protocol supported but not socket type.
7098348SEric.Yu@Sun.COM  *
7108348SEric.Yu@Sun.COM  * TODO: should use ddi_modopen()/ddi_modclose()
7118348SEric.Yu@Sun.COM  */
7128348SEric.Yu@Sun.COM int
7138348SEric.Yu@Sun.COM solookup(int family, int type, int protocol, struct sockparams **spp)
7148348SEric.Yu@Sun.COM {
7158348SEric.Yu@Sun.COM 	struct sockparams *sp = NULL;
7168348SEric.Yu@Sun.COM 	int error = 0;
7178348SEric.Yu@Sun.COM 
7188348SEric.Yu@Sun.COM 	*spp = NULL;
7198348SEric.Yu@Sun.COM 	rw_enter(&splist_lock, RW_READER);
7208348SEric.Yu@Sun.COM 
7218348SEric.Yu@Sun.COM 	/*
7228348SEric.Yu@Sun.COM 	 * Search the sockparams list for an appropiate entry.
7238348SEric.Yu@Sun.COM 	 * Hopefully we find an entry that match the exact family,
7248348SEric.Yu@Sun.COM 	 * type and protocol specified by the user, in which case
7258348SEric.Yu@Sun.COM 	 * we return that entry. However, we also keep track of
7268348SEric.Yu@Sun.COM 	 * the default entry for a specific family and type, the
7278348SEric.Yu@Sun.COM 	 * entry of which would have a protocol value of 0.
7288348SEric.Yu@Sun.COM 	 */
7298489Sshenjian 	sp = sockparams_find(&sphead, family, type, protocol, B_TRUE, NULL);
7308348SEric.Yu@Sun.COM 
7318348SEric.Yu@Sun.COM 	if (sp == NULL) {
7328348SEric.Yu@Sun.COM 		int found = 0;
7338348SEric.Yu@Sun.COM 
7348348SEric.Yu@Sun.COM 		/* Determine correct error code */
7358348SEric.Yu@Sun.COM 		for (sp = list_head(&sphead); sp != NULL;
7368348SEric.Yu@Sun.COM 		    sp = list_next(&sphead, sp)) {
7378348SEric.Yu@Sun.COM 			if (sp->sp_family == family && found < 1)
7388348SEric.Yu@Sun.COM 				found = 1;
7398348SEric.Yu@Sun.COM 			if (sp->sp_family == family &&
7408348SEric.Yu@Sun.COM 			    sp->sp_protocol == protocol && found < 2)
7418348SEric.Yu@Sun.COM 				found = 2;
7428348SEric.Yu@Sun.COM 		}
7438348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
7448348SEric.Yu@Sun.COM 		switch (found) {
7458348SEric.Yu@Sun.COM 		case 0:
7468348SEric.Yu@Sun.COM 			error = EAFNOSUPPORT;
7478348SEric.Yu@Sun.COM 			break;
7488348SEric.Yu@Sun.COM 		case 1:
7498348SEric.Yu@Sun.COM 			error = EPROTONOSUPPORT;
7508348SEric.Yu@Sun.COM 			break;
7518348SEric.Yu@Sun.COM 		case 2:
7528348SEric.Yu@Sun.COM 			error = EPROTOTYPE;
7538348SEric.Yu@Sun.COM 			break;
7548348SEric.Yu@Sun.COM 		}
7558348SEric.Yu@Sun.COM 		return (error);
7568348SEric.Yu@Sun.COM 	}
7578348SEric.Yu@Sun.COM 
7588348SEric.Yu@Sun.COM 	/*
7598348SEric.Yu@Sun.COM 	 * An entry was found.
7608348SEric.Yu@Sun.COM 	 *
7618348SEric.Yu@Sun.COM 	 * We put a hold on the entry early on, so if the
7628348SEric.Yu@Sun.COM 	 * sockmod is not loaded, and we have to exit
7638348SEric.Yu@Sun.COM 	 * splist_lock to call modload(), we know that the
7648348SEric.Yu@Sun.COM 	 * sockparams entry wont go away. That way we don't
7658348SEric.Yu@Sun.COM 	 * have to look up the entry once we come back from
7668348SEric.Yu@Sun.COM 	 * modload().
7678348SEric.Yu@Sun.COM 	 */
7688348SEric.Yu@Sun.COM 	SOCKPARAMS_INC_REF(sp);
7698348SEric.Yu@Sun.COM 	rw_exit(&splist_lock);
7708348SEric.Yu@Sun.COM 
7718348SEric.Yu@Sun.COM 	if (sp->sp_smod_info == NULL) {
772*12257SAnders.Persson@Sun.COM 		smod_info_t *smod = smod_lookup_byname(sp->sp_smod_name);
773*12257SAnders.Persson@Sun.COM 
774*12257SAnders.Persson@Sun.COM 		if (smod == NULL) {
7758348SEric.Yu@Sun.COM 			/*
7768348SEric.Yu@Sun.COM 			 * We put a hold on the sockparams entry
7778348SEric.Yu@Sun.COM 			 * earlier, hoping everything would work out.
7788348SEric.Yu@Sun.COM 			 * That obviously did not happen, so release
7798348SEric.Yu@Sun.COM 			 * the hold here.
7808348SEric.Yu@Sun.COM 			 */
7818348SEric.Yu@Sun.COM 			SOCKPARAMS_DEC_REF(sp);
7828348SEric.Yu@Sun.COM 			/*
7838348SEric.Yu@Sun.COM 			 * We should probably mark the sockparams as
7848348SEric.Yu@Sun.COM 			 * "bad", and redo the lookup skipping the
7858348SEric.Yu@Sun.COM 			 * "bad" entries. I.e., sp->sp_mod_state |= BAD,
7868348SEric.Yu@Sun.COM 			 * return (solookup(...))
7878348SEric.Yu@Sun.COM 			 */
7888348SEric.Yu@Sun.COM 			return (ENXIO);
7898348SEric.Yu@Sun.COM 		}
790*12257SAnders.Persson@Sun.COM 		/*
791*12257SAnders.Persson@Sun.COM 		 * Another thread might have already looked up the socket
792*12257SAnders.Persson@Sun.COM 		 * module for this entry. In that case we need to drop our
793*12257SAnders.Persson@Sun.COM 		 * reference to `smod' to ensure that the sockparams entry
794*12257SAnders.Persson@Sun.COM 		 * only holds one reference.
795*12257SAnders.Persson@Sun.COM 		 */
796*12257SAnders.Persson@Sun.COM 		mutex_enter(&sp->sp_lock);
797*12257SAnders.Persson@Sun.COM 		if (sp->sp_smod_info == NULL)
798*12257SAnders.Persson@Sun.COM 			sp->sp_smod_info = smod;
799*12257SAnders.Persson@Sun.COM 		else
800*12257SAnders.Persson@Sun.COM 			SMOD_DEC_REF(smod, sp->sp_smod_name);
801*12257SAnders.Persson@Sun.COM 		mutex_exit(&sp->sp_lock);
8028348SEric.Yu@Sun.COM 	}
8038348SEric.Yu@Sun.COM 
8048348SEric.Yu@Sun.COM 	/*
8058348SEric.Yu@Sun.COM 	 * Alright, we have a valid sockparams entry.
8068348SEric.Yu@Sun.COM 	 */
8078348SEric.Yu@Sun.COM 	*spp = sp;
8088348SEric.Yu@Sun.COM 	return (0);
8098348SEric.Yu@Sun.COM }
810