xref: /onnv-gate/usr/src/uts/common/fs/sockfs/sockparams.c (revision 8964:6f6c3509c6b6)
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 /*
238489Sshenjian  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
248348SEric.Yu@Sun.COM  * Use is subject to license terms.
258348SEric.Yu@Sun.COM  */
268348SEric.Yu@Sun.COM 
278348SEric.Yu@Sun.COM #include <sys/types.h>
288348SEric.Yu@Sun.COM #include <sys/t_lock.h>
298348SEric.Yu@Sun.COM #include <sys/param.h>
308348SEric.Yu@Sun.COM #include <sys/systm.h>
318348SEric.Yu@Sun.COM #include <sys/sysmacros.h>
328348SEric.Yu@Sun.COM #include <sys/cmn_err.h>
338348SEric.Yu@Sun.COM #include <sys/list.h>
348348SEric.Yu@Sun.COM 
358348SEric.Yu@Sun.COM #include <sys/stropts.h>
368348SEric.Yu@Sun.COM #include <sys/socket.h>
378348SEric.Yu@Sun.COM #include <sys/socketvar.h>
388348SEric.Yu@Sun.COM 
398348SEric.Yu@Sun.COM #include <fs/sockfs/sockcommon.h>
408348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h>
418348SEric.Yu@Sun.COM 
428348SEric.Yu@Sun.COM /*
438348SEric.Yu@Sun.COM  * Socket Parameters
448348SEric.Yu@Sun.COM  *
458348SEric.Yu@Sun.COM  * Socket parameter (struct sockparams) entries represent the socket types
468348SEric.Yu@Sun.COM  * available on the system.
478348SEric.Yu@Sun.COM  *
488348SEric.Yu@Sun.COM  * Flags (sp_flags):
498348SEric.Yu@Sun.COM  *
508348SEric.Yu@Sun.COM  * SOCKPARAMS_EPHEMERAL: A temporary sockparams entry that will be deleted
518348SEric.Yu@Sun.COM  * as soon as its' ref count drops to zero. In addition, ephemeral entries will
528348SEric.Yu@Sun.COM  * never be hooked onto the global sockparams list. Ephemeral entries are
538348SEric.Yu@Sun.COM  * created when application requests to create a socket using an application
548348SEric.Yu@Sun.COM  * supplied device path, or when a socket is falling back to TPI.
558348SEric.Yu@Sun.COM  *
568348SEric.Yu@Sun.COM  * Lock order:
578348SEric.Yu@Sun.COM  *   The lock order is splist_lock -> sp_lock.
588348SEric.Yu@Sun.COM  *   The lock order is sp_ephem_lock -> sp_lock.
598348SEric.Yu@Sun.COM  */
608348SEric.Yu@Sun.COM extern int 	kobj_path_exists(char *, int);
618348SEric.Yu@Sun.COM extern void	nl7c_init(void);
628348SEric.Yu@Sun.COM extern int	sockfs_defer_nl7c_init;
638348SEric.Yu@Sun.COM 
648348SEric.Yu@Sun.COM static int 	sockparams_sdev_init(struct sockparams *, char *, int);
658348SEric.Yu@Sun.COM static void 	sockparams_sdev_fini(struct sockparams *);
668348SEric.Yu@Sun.COM 
678348SEric.Yu@Sun.COM /*
688348SEric.Yu@Sun.COM  * Global sockparams list (populated via soconfig(1M)).
698348SEric.Yu@Sun.COM  */
708348SEric.Yu@Sun.COM static list_t sphead;
718348SEric.Yu@Sun.COM static krwlock_t splist_lock;
728348SEric.Yu@Sun.COM 
738348SEric.Yu@Sun.COM /*
748348SEric.Yu@Sun.COM  * List of ephemeral sockparams.
758348SEric.Yu@Sun.COM  */
768348SEric.Yu@Sun.COM static list_t sp_ephem_list;
778348SEric.Yu@Sun.COM static krwlock_t sp_ephem_lock;
788348SEric.Yu@Sun.COM 
79*8964SAnders.Persson@Sun.COM /* Global kstats for sockparams */
80*8964SAnders.Persson@Sun.COM typedef struct sockparams_g_stats {
81*8964SAnders.Persson@Sun.COM 	kstat_named_t spgs_ephem_nalloc;
82*8964SAnders.Persson@Sun.COM 	kstat_named_t spgs_ephem_nreuse;
83*8964SAnders.Persson@Sun.COM } sockparams_g_stats_t;
84*8964SAnders.Persson@Sun.COM 
85*8964SAnders.Persson@Sun.COM static sockparams_g_stats_t sp_g_stats;
86*8964SAnders.Persson@Sun.COM static kstat_t *sp_g_kstat;
87*8964SAnders.Persson@Sun.COM 
88*8964SAnders.Persson@Sun.COM 
898348SEric.Yu@Sun.COM void
908348SEric.Yu@Sun.COM sockparams_init(void)
918348SEric.Yu@Sun.COM {
928348SEric.Yu@Sun.COM 	list_create(&sphead, sizeof (struct sockparams),
938348SEric.Yu@Sun.COM 	    offsetof(struct sockparams, sp_node));
948348SEric.Yu@Sun.COM 	list_create(&sp_ephem_list, sizeof (struct sockparams),
958348SEric.Yu@Sun.COM 	    offsetof(struct sockparams, sp_node));
968348SEric.Yu@Sun.COM 
978348SEric.Yu@Sun.COM 	rw_init(&splist_lock, NULL, RW_DEFAULT, NULL);
988348SEric.Yu@Sun.COM 	rw_init(&sp_ephem_lock, NULL, RW_DEFAULT, NULL);
99*8964SAnders.Persson@Sun.COM 
100*8964SAnders.Persson@Sun.COM 	kstat_named_init(&sp_g_stats.spgs_ephem_nalloc, "ephemeral_nalloc",
101*8964SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
102*8964SAnders.Persson@Sun.COM 	kstat_named_init(&sp_g_stats.spgs_ephem_nreuse, "ephemeral_nreuse",
103*8964SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
104*8964SAnders.Persson@Sun.COM 
105*8964SAnders.Persson@Sun.COM 	sp_g_kstat = kstat_create("sockfs", 0, "sockparams", "misc",
106*8964SAnders.Persson@Sun.COM 	    KSTAT_TYPE_NAMED, sizeof (sp_g_stats) / sizeof (kstat_named_t),
107*8964SAnders.Persson@Sun.COM 	    KSTAT_FLAG_VIRTUAL);
108*8964SAnders.Persson@Sun.COM 	if (sp_g_kstat == NULL)
109*8964SAnders.Persson@Sun.COM 		return;
110*8964SAnders.Persson@Sun.COM 
111*8964SAnders.Persson@Sun.COM 	sp_g_kstat->ks_data = &sp_g_stats;
112*8964SAnders.Persson@Sun.COM 
113*8964SAnders.Persson@Sun.COM 	kstat_install(sp_g_kstat);
114*8964SAnders.Persson@Sun.COM }
115*8964SAnders.Persson@Sun.COM 
116*8964SAnders.Persson@Sun.COM static int
117*8964SAnders.Persson@Sun.COM sockparams_kstat_update(kstat_t *ksp, int rw)
118*8964SAnders.Persson@Sun.COM {
119*8964SAnders.Persson@Sun.COM 	struct sockparams *sp = ksp->ks_private;
120*8964SAnders.Persson@Sun.COM 	sockparams_stats_t *sps = ksp->ks_data;
121*8964SAnders.Persson@Sun.COM 
122*8964SAnders.Persson@Sun.COM 	if (rw == KSTAT_WRITE)
123*8964SAnders.Persson@Sun.COM 		return (EACCES);
124*8964SAnders.Persson@Sun.COM 
125*8964SAnders.Persson@Sun.COM 	sps->sps_nactive.value.ui64 = sp->sp_refcnt;
126*8964SAnders.Persson@Sun.COM 
127*8964SAnders.Persson@Sun.COM 	return (0);
128*8964SAnders.Persson@Sun.COM }
129*8964SAnders.Persson@Sun.COM 
130*8964SAnders.Persson@Sun.COM /*
131*8964SAnders.Persson@Sun.COM  * Setup kstats for the given sockparams entry.
132*8964SAnders.Persson@Sun.COM  */
133*8964SAnders.Persson@Sun.COM static void
134*8964SAnders.Persson@Sun.COM sockparams_kstat_init(struct sockparams *sp)
135*8964SAnders.Persson@Sun.COM {
136*8964SAnders.Persson@Sun.COM 	char name[KSTAT_STRLEN];
137*8964SAnders.Persson@Sun.COM 
138*8964SAnders.Persson@Sun.COM 	kstat_named_init(&sp->sp_stats.sps_nfallback, "nfallback",
139*8964SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
140*8964SAnders.Persson@Sun.COM 	kstat_named_init(&sp->sp_stats.sps_nactive, "nactive",
141*8964SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
142*8964SAnders.Persson@Sun.COM 	kstat_named_init(&sp->sp_stats.sps_ncreate, "ncreate",
143*8964SAnders.Persson@Sun.COM 	    KSTAT_DATA_UINT64);
144*8964SAnders.Persson@Sun.COM 
145*8964SAnders.Persson@Sun.COM 	(void) snprintf(name, KSTAT_STRLEN, "socket_%d_%d_%d", sp->sp_family,
146*8964SAnders.Persson@Sun.COM 	    sp->sp_type, sp->sp_protocol);
147*8964SAnders.Persson@Sun.COM 
148*8964SAnders.Persson@Sun.COM 	sp->sp_kstat = kstat_create("sockfs", 0, name, "misc", KSTAT_TYPE_NAMED,
149*8964SAnders.Persson@Sun.COM 	    sizeof (sockparams_stats_t) / sizeof (kstat_named_t),
150*8964SAnders.Persson@Sun.COM 	    KSTAT_FLAG_VIRTUAL);
151*8964SAnders.Persson@Sun.COM 
152*8964SAnders.Persson@Sun.COM 	if (sp->sp_kstat == NULL)
153*8964SAnders.Persson@Sun.COM 		return;
154*8964SAnders.Persson@Sun.COM 
155*8964SAnders.Persson@Sun.COM 	sp->sp_kstat->ks_data = &sp->sp_stats;
156*8964SAnders.Persson@Sun.COM 	sp->sp_kstat->ks_update = sockparams_kstat_update;
157*8964SAnders.Persson@Sun.COM 	sp->sp_kstat->ks_private = sp;
158*8964SAnders.Persson@Sun.COM 	kstat_install(sp->sp_kstat);
159*8964SAnders.Persson@Sun.COM }
160*8964SAnders.Persson@Sun.COM 
161*8964SAnders.Persson@Sun.COM static void
162*8964SAnders.Persson@Sun.COM sockparams_kstat_fini(struct sockparams *sp)
163*8964SAnders.Persson@Sun.COM {
164*8964SAnders.Persson@Sun.COM 	if (sp->sp_kstat != NULL) {
165*8964SAnders.Persson@Sun.COM 		kstat_delete(sp->sp_kstat);
166*8964SAnders.Persson@Sun.COM 		sp->sp_kstat = NULL;
167*8964SAnders.Persson@Sun.COM 	}
1688348SEric.Yu@Sun.COM }
1698348SEric.Yu@Sun.COM 
1708348SEric.Yu@Sun.COM /*
1718348SEric.Yu@Sun.COM  * sockparams_create(int family, int type, int protocol, char *modname,
1728348SEric.Yu@Sun.COM  *     char *devpath, int devpathlen, int flags, int kmflags, int *errorp)
1738348SEric.Yu@Sun.COM  *
1748348SEric.Yu@Sun.COM  * Create a new sockparams entry.
1758348SEric.Yu@Sun.COM  *
1768348SEric.Yu@Sun.COM  * Arguments:
1778348SEric.Yu@Sun.COM  *   family, type, protocol: specifies the socket type
1788348SEric.Yu@Sun.COM  *   modname: Name of the module associated with the socket type. The
1798348SEric.Yu@Sun.COM  *            module can be NULL if a device path is given, in which
1808348SEric.Yu@Sun.COM  *            case the TPI module is used.
1818348SEric.Yu@Sun.COM  *   devpath: Path to the STREAMS device. May be NULL for non-STREAMS
1828348SEric.Yu@Sun.COM  *            based transports, or those transports that do not provide
1838348SEric.Yu@Sun.COM  *            the capability to fallback to STREAMS.
1848348SEric.Yu@Sun.COM  *   devpathlen: Length of the devpath string. The argument can be 0,
1858348SEric.Yu@Sun.COM  *            indicating that devpath was allocated statically, and should
1868348SEric.Yu@Sun.COM  *            not be freed when the sockparams entry is destroyed.
1878348SEric.Yu@Sun.COM  *
1888348SEric.Yu@Sun.COM  *   flags  : SOCKPARAMS_EPHEMERAL is the only flag that is allowed.
1898348SEric.Yu@Sun.COM  *   kmflags: KM_{NO,}SLEEP
1908348SEric.Yu@Sun.COM  *   errorp : Value-return argument, set when an error occurs.
1918348SEric.Yu@Sun.COM  *
1928348SEric.Yu@Sun.COM  * Returns:
1938348SEric.Yu@Sun.COM  *   On success a new sockparams entry is returned, and *errorp is set
1948348SEric.Yu@Sun.COM  *   to 0. On failure NULL is returned and *errorp is set to indicate the
1958348SEric.Yu@Sun.COM  *   type of error that occured.
1968348SEric.Yu@Sun.COM  *
1978348SEric.Yu@Sun.COM  * Notes:
1988348SEric.Yu@Sun.COM  *   devpath and modname are freed upon failure.
1998348SEric.Yu@Sun.COM  */
2008348SEric.Yu@Sun.COM struct sockparams *
2018348SEric.Yu@Sun.COM sockparams_create(int family, int type, int protocol, char *modname,
2028348SEric.Yu@Sun.COM     char *devpath, int devpathlen, int flags, int kmflags, int *errorp)
2038348SEric.Yu@Sun.COM {
2048348SEric.Yu@Sun.COM 	struct sockparams *sp = NULL;
2058348SEric.Yu@Sun.COM 	size_t size;
2068348SEric.Yu@Sun.COM 
2078348SEric.Yu@Sun.COM 	ASSERT((flags & ~SOCKPARAMS_EPHEMERAL) == 0);
2088348SEric.Yu@Sun.COM 	if (flags & ~SOCKPARAMS_EPHEMERAL) {
2098348SEric.Yu@Sun.COM 		*errorp = EINVAL;
2108348SEric.Yu@Sun.COM 		goto error;
2118348SEric.Yu@Sun.COM 	}
2128348SEric.Yu@Sun.COM 
2138348SEric.Yu@Sun.COM 	/* either a module or device must be given */
2148348SEric.Yu@Sun.COM 	if (modname == NULL && devpath == NULL) {
2158348SEric.Yu@Sun.COM 		*errorp = EINVAL;
2168348SEric.Yu@Sun.COM 		goto error;
2178348SEric.Yu@Sun.COM 	}
2188348SEric.Yu@Sun.COM 
2198348SEric.Yu@Sun.COM 	sp = kmem_zalloc(sizeof (*sp), kmflags);
2208348SEric.Yu@Sun.COM 	if (sp == NULL) {
2218348SEric.Yu@Sun.COM 		*errorp = ENOMEM;
2228348SEric.Yu@Sun.COM 		goto error;
2238348SEric.Yu@Sun.COM 	}
2248348SEric.Yu@Sun.COM 	sp->sp_family = family;
2258348SEric.Yu@Sun.COM 	sp->sp_type = type;
2268348SEric.Yu@Sun.COM 	sp->sp_protocol = protocol;
2278348SEric.Yu@Sun.COM 	sp->sp_refcnt = 0;
2288348SEric.Yu@Sun.COM 	sp->sp_flags = flags;
2298348SEric.Yu@Sun.COM 
230*8964SAnders.Persson@Sun.COM 	/*
231*8964SAnders.Persson@Sun.COM 	 * We do not create kstats for ephemeral entries, but we do keep
232*8964SAnders.Persson@Sun.COM 	 * track how many we have created.
233*8964SAnders.Persson@Sun.COM 	 */
234*8964SAnders.Persson@Sun.COM 	if (sp->sp_flags & SOCKPARAMS_EPHEMERAL)
235*8964SAnders.Persson@Sun.COM 		sp_g_stats.spgs_ephem_nalloc.value.ui64++;
236*8964SAnders.Persson@Sun.COM 	else
237*8964SAnders.Persson@Sun.COM 		sockparams_kstat_init(sp);
238*8964SAnders.Persson@Sun.COM 
2398348SEric.Yu@Sun.COM 	if (modname != NULL) {
2408348SEric.Yu@Sun.COM 		sp->sp_smod_name = modname;
2418348SEric.Yu@Sun.COM 	} else {
2428348SEric.Yu@Sun.COM 		size = strlen(SOTPI_SMOD_NAME) + 1;
2438348SEric.Yu@Sun.COM 		modname = kmem_zalloc(size, kmflags);
2448348SEric.Yu@Sun.COM 		if (modname == NULL) {
2458348SEric.Yu@Sun.COM 			*errorp = ENOMEM;
2468348SEric.Yu@Sun.COM 			goto error;
2478348SEric.Yu@Sun.COM 		}
2488348SEric.Yu@Sun.COM 		sp->sp_smod_name = modname;
2498348SEric.Yu@Sun.COM 		(void) sprintf(sp->sp_smod_name, "%s", SOTPI_SMOD_NAME);
2508348SEric.Yu@Sun.COM 	}
2518348SEric.Yu@Sun.COM 
2528348SEric.Yu@Sun.COM 	if (devpath != NULL) {
2538348SEric.Yu@Sun.COM 		/* Set up the device entry. */
2548348SEric.Yu@Sun.COM 		*errorp = sockparams_sdev_init(sp, devpath, devpathlen);
2558348SEric.Yu@Sun.COM 		if (*errorp != 0)
2568348SEric.Yu@Sun.COM 			goto error;
2578348SEric.Yu@Sun.COM 	}
2588348SEric.Yu@Sun.COM 
2598348SEric.Yu@Sun.COM 	mutex_init(&sp->sp_lock, NULL, MUTEX_DEFAULT, NULL);
2608348SEric.Yu@Sun.COM 	*errorp = 0;
2618348SEric.Yu@Sun.COM 	return (sp);
2628348SEric.Yu@Sun.COM error:
2638348SEric.Yu@Sun.COM 	ASSERT(*errorp != 0);
2648348SEric.Yu@Sun.COM 	if (modname != NULL)
2658348SEric.Yu@Sun.COM 		kmem_free(modname, strlen(modname) + 1);
2668348SEric.Yu@Sun.COM 	if (devpathlen != 0)
2678348SEric.Yu@Sun.COM 		kmem_free(devpath, devpathlen);
268*8964SAnders.Persson@Sun.COM 	if (sp != NULL) {
269*8964SAnders.Persson@Sun.COM 		sockparams_kstat_fini(sp);
2708348SEric.Yu@Sun.COM 		kmem_free(sp, sizeof (*sp));
271*8964SAnders.Persson@Sun.COM 	}
2728348SEric.Yu@Sun.COM 	return (NULL);
2738348SEric.Yu@Sun.COM }
2748348SEric.Yu@Sun.COM 
2758348SEric.Yu@Sun.COM /*
2768348SEric.Yu@Sun.COM  * Initialize the STREAMS device aspect of the sockparams entry.
2778348SEric.Yu@Sun.COM  */
2788348SEric.Yu@Sun.COM static int
2798348SEric.Yu@Sun.COM sockparams_sdev_init(struct sockparams *sp, char *devpath, int devpathlen)
2808348SEric.Yu@Sun.COM {
2818348SEric.Yu@Sun.COM 	vnode_t *vp = NULL;
2828348SEric.Yu@Sun.COM 	int error;
2838348SEric.Yu@Sun.COM 
2848348SEric.Yu@Sun.COM 	ASSERT(devpath != NULL);
2858348SEric.Yu@Sun.COM 
2868348SEric.Yu@Sun.COM 	if ((error = sogetvp(devpath, &vp, UIO_SYSSPACE)) != 0) {
2878348SEric.Yu@Sun.COM 		dprint(0, ("sockparams_sdev_init: vp %s failed with %d\n",
2888348SEric.Yu@Sun.COM 		    devpath, error));
2898348SEric.Yu@Sun.COM 		return (error);
2908348SEric.Yu@Sun.COM 	}
2918348SEric.Yu@Sun.COM 
2928348SEric.Yu@Sun.COM 	ASSERT(vp != NULL);
2938348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_vnode = vp;
2948348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_devpath = devpath;
2958348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_devpathlen = devpathlen;
2968348SEric.Yu@Sun.COM 
2978348SEric.Yu@Sun.COM 	return (0);
2988348SEric.Yu@Sun.COM }
2998348SEric.Yu@Sun.COM 
3008348SEric.Yu@Sun.COM /*
3018348SEric.Yu@Sun.COM  * sockparams_destroy(struct sockparams *sp)
3028348SEric.Yu@Sun.COM  *
3038348SEric.Yu@Sun.COM  * Releases all the resources associated with the sockparams entry,
3048348SEric.Yu@Sun.COM  * and frees the sockparams entry.
3058348SEric.Yu@Sun.COM  *
3068348SEric.Yu@Sun.COM  * Arguments:
3078348SEric.Yu@Sun.COM  *   sp: the sockparams entry to destroy.
3088348SEric.Yu@Sun.COM  *
3098348SEric.Yu@Sun.COM  * Returns:
3108348SEric.Yu@Sun.COM  *   Nothing.
3118348SEric.Yu@Sun.COM  *
3128348SEric.Yu@Sun.COM  * Locking:
3138348SEric.Yu@Sun.COM  *   The sp_lock of the entry can not be held.
3148348SEric.Yu@Sun.COM  */
3158348SEric.Yu@Sun.COM void
3168348SEric.Yu@Sun.COM sockparams_destroy(struct sockparams *sp)
3178348SEric.Yu@Sun.COM {
3188348SEric.Yu@Sun.COM 	ASSERT(sp->sp_refcnt == 0);
3198348SEric.Yu@Sun.COM 	ASSERT(!list_link_active(&sp->sp_node));
3208348SEric.Yu@Sun.COM 
3218348SEric.Yu@Sun.COM 	sockparams_sdev_fini(sp);
3228348SEric.Yu@Sun.COM 
3238348SEric.Yu@Sun.COM 	if (sp->sp_smod_info != NULL)
3248348SEric.Yu@Sun.COM 		SMOD_DEC_REF(sp, sp->sp_smod_info);
3258348SEric.Yu@Sun.COM 	kmem_free(sp->sp_smod_name, strlen(sp->sp_smod_name) + 1);
3268348SEric.Yu@Sun.COM 	sp->sp_smod_name = NULL;
3278348SEric.Yu@Sun.COM 	sp->sp_smod_info = NULL;
3288348SEric.Yu@Sun.COM 	mutex_destroy(&sp->sp_lock);
329*8964SAnders.Persson@Sun.COM 	sockparams_kstat_fini(sp);
3308348SEric.Yu@Sun.COM 
3318348SEric.Yu@Sun.COM 	kmem_free(sp, sizeof (*sp));
3328348SEric.Yu@Sun.COM }
3338348SEric.Yu@Sun.COM 
3348348SEric.Yu@Sun.COM /*
3358348SEric.Yu@Sun.COM  * Clean up the STREAMS device part of the sockparams entry.
3368348SEric.Yu@Sun.COM  */
3378348SEric.Yu@Sun.COM static void
3388348SEric.Yu@Sun.COM sockparams_sdev_fini(struct sockparams *sp)
3398348SEric.Yu@Sun.COM {
3408348SEric.Yu@Sun.COM 	sdev_info_t sd;
3418348SEric.Yu@Sun.COM 
3428348SEric.Yu@Sun.COM 	/*
3438348SEric.Yu@Sun.COM 	 * if the entry does not have a STREAMS device, then there
3448348SEric.Yu@Sun.COM 	 * is nothing to do.
3458348SEric.Yu@Sun.COM 	 */
3468348SEric.Yu@Sun.COM 	if (!SOCKPARAMS_HAS_DEVICE(sp))
3478348SEric.Yu@Sun.COM 		return;
3488348SEric.Yu@Sun.COM 
3498348SEric.Yu@Sun.COM 	sd = sp->sp_sdev_info;
3508348SEric.Yu@Sun.COM 	if (sd.sd_vnode != NULL)
3518348SEric.Yu@Sun.COM 		VN_RELE(sd.sd_vnode);
3528348SEric.Yu@Sun.COM 	if (sd.sd_devpathlen != 0)
3538348SEric.Yu@Sun.COM 		kmem_free(sd.sd_devpath, sd.sd_devpathlen);
3548348SEric.Yu@Sun.COM 
3558348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_vnode = NULL;
3568348SEric.Yu@Sun.COM 	sp->sp_sdev_info.sd_devpath = NULL;
3578348SEric.Yu@Sun.COM }
3588348SEric.Yu@Sun.COM 
3598348SEric.Yu@Sun.COM /*
3608348SEric.Yu@Sun.COM  * Look for a matching sockparams entry on the given list.
3618348SEric.Yu@Sun.COM  * The caller must hold the associated list lock.
3628348SEric.Yu@Sun.COM  */
3638348SEric.Yu@Sun.COM static struct sockparams *
3648348SEric.Yu@Sun.COM sockparams_find(list_t *list, int family, int type, int protocol,
3658489Sshenjian     boolean_t by_devpath, const char *name)
3668348SEric.Yu@Sun.COM {
3678348SEric.Yu@Sun.COM 	struct sockparams *sp;
3688348SEric.Yu@Sun.COM 
3698348SEric.Yu@Sun.COM 	for (sp = list_head(list); sp != NULL; sp = list_next(list, sp)) {
3708489Sshenjian 		if (sp->sp_family == family && sp->sp_type == type) {
3718348SEric.Yu@Sun.COM 			if (sp->sp_protocol == protocol) {
3728489Sshenjian 				if (name == NULL)
3738348SEric.Yu@Sun.COM 					break;
3748489Sshenjian 				else if (by_devpath &&
3758348SEric.Yu@Sun.COM 				    sp->sp_sdev_info.sd_devpath != NULL &&
3768348SEric.Yu@Sun.COM 				    strcmp(sp->sp_sdev_info.sd_devpath,
3778348SEric.Yu@Sun.COM 				    name) == 0)
3788348SEric.Yu@Sun.COM 					break;
3798489Sshenjian 				else if (strcmp(sp->sp_smod_name, name) == 0)
3808348SEric.Yu@Sun.COM 					break;
3818348SEric.Yu@Sun.COM 			}
3828348SEric.Yu@Sun.COM 		}
3838348SEric.Yu@Sun.COM 	}
3848489Sshenjian 	return (sp);
3858348SEric.Yu@Sun.COM }
3868348SEric.Yu@Sun.COM 
3878348SEric.Yu@Sun.COM /*
3888348SEric.Yu@Sun.COM  * sockparams_hold_ephemeral()
3898348SEric.Yu@Sun.COM  *
3908348SEric.Yu@Sun.COM  * Returns an ephemeral sockparams entry of the requested family, type and
3918348SEric.Yu@Sun.COM  * protocol. The entry is returned held, and the caller is responsible for
3928348SEric.Yu@Sun.COM  * dropping the reference using SOCKPARAMS_DEC_REF() once done.
3938348SEric.Yu@Sun.COM  *
3948348SEric.Yu@Sun.COM  * All ephemeral entries are on list (sp_ephem_list). If there is an
3958348SEric.Yu@Sun.COM  * entry on the list that match the search criteria, then a reference is
3968348SEric.Yu@Sun.COM  * placed on that entry. Otherwise, a new entry is created and inserted
3978348SEric.Yu@Sun.COM  * in the list. The entry is removed from the list when the last reference
3988348SEric.Yu@Sun.COM  * is dropped.
3998348SEric.Yu@Sun.COM  *
4008348SEric.Yu@Sun.COM  * The tpi flag is used to determine whether name refers to a device or
4018348SEric.Yu@Sun.COM  * module name.
4028348SEric.Yu@Sun.COM  */
4038348SEric.Yu@Sun.COM static struct sockparams *
4048348SEric.Yu@Sun.COM sockparams_hold_ephemeral(int family, int type, int protocol,
4058489Sshenjian     const char *name, boolean_t by_devpath, int kmflag, int *errorp)
4068348SEric.Yu@Sun.COM {
4078348SEric.Yu@Sun.COM 	struct sockparams *sp = NULL;
4088348SEric.Yu@Sun.COM 	*errorp = 0;
4098348SEric.Yu@Sun.COM 
4108348SEric.Yu@Sun.COM 	/*
4118348SEric.Yu@Sun.COM 	 * First look for an existing entry
4128348SEric.Yu@Sun.COM 	 */
4138348SEric.Yu@Sun.COM 	rw_enter(&sp_ephem_lock, RW_READER);
4148348SEric.Yu@Sun.COM 	sp = sockparams_find(&sp_ephem_list, family, type, protocol,
4158489Sshenjian 	    by_devpath, name);
4168348SEric.Yu@Sun.COM 	if (sp != NULL) {
4178348SEric.Yu@Sun.COM 		SOCKPARAMS_INC_REF(sp);
4188348SEric.Yu@Sun.COM 		rw_exit(&sp_ephem_lock);
419*8964SAnders.Persson@Sun.COM 		sp_g_stats.spgs_ephem_nreuse.value.ui64++;
4208348SEric.Yu@Sun.COM 
4218348SEric.Yu@Sun.COM 		return (sp);
4228348SEric.Yu@Sun.COM 	} else {
4238348SEric.Yu@Sun.COM 		struct sockparams *newsp = NULL;
4248348SEric.Yu@Sun.COM 		char *namebuf = NULL;
4258348SEric.Yu@Sun.COM 		int namelen = 0;
4268348SEric.Yu@Sun.COM 
4278348SEric.Yu@Sun.COM 		rw_exit(&sp_ephem_lock);
4288348SEric.Yu@Sun.COM 
4298348SEric.Yu@Sun.COM 		namelen = strlen(name) + 1;
4308348SEric.Yu@Sun.COM 		namebuf = kmem_alloc(namelen, kmflag);
4318348SEric.Yu@Sun.COM 		if (namebuf == NULL) {
4328348SEric.Yu@Sun.COM 			*errorp = ENOMEM;
4338348SEric.Yu@Sun.COM 			return (NULL);
4348348SEric.Yu@Sun.COM 		}
4358348SEric.Yu@Sun.COM 
4368348SEric.Yu@Sun.COM 		(void *)strncpy(namebuf, name, namelen);
4378489Sshenjian 		if (by_devpath) {
4388348SEric.Yu@Sun.COM 			newsp = sockparams_create(family, type,
4398348SEric.Yu@Sun.COM 			    protocol, NULL, namebuf, namelen,
4408348SEric.Yu@Sun.COM 			    SOCKPARAMS_EPHEMERAL, kmflag, errorp);
4418348SEric.Yu@Sun.COM 		} else {
4428348SEric.Yu@Sun.COM 			newsp = sockparams_create(family, type,
4438348SEric.Yu@Sun.COM 			    protocol, namebuf, NULL, 0,
4448348SEric.Yu@Sun.COM 			    SOCKPARAMS_EPHEMERAL, kmflag, errorp);
4458348SEric.Yu@Sun.COM 		}
4468348SEric.Yu@Sun.COM 
4478348SEric.Yu@Sun.COM 		if (newsp == NULL) {
4488348SEric.Yu@Sun.COM 			ASSERT(*errorp != 0);
4498348SEric.Yu@Sun.COM 			return (NULL);
4508348SEric.Yu@Sun.COM 		}
4518348SEric.Yu@Sun.COM 
4528348SEric.Yu@Sun.COM 		/*
4538348SEric.Yu@Sun.COM 		 * Time to load the socket module.
4548348SEric.Yu@Sun.COM 		 */
4558348SEric.Yu@Sun.COM 		ASSERT(newsp->sp_smod_info == NULL);
4568348SEric.Yu@Sun.COM 		newsp->sp_smod_info =
4578348SEric.Yu@Sun.COM 		    smod_lookup_byname(newsp->sp_smod_name);
4588348SEric.Yu@Sun.COM 		if (newsp->sp_smod_info == NULL) {
4598348SEric.Yu@Sun.COM 			/* Failed to load */
4608348SEric.Yu@Sun.COM 			sockparams_destroy(newsp);
4618348SEric.Yu@Sun.COM 			*errorp = ENXIO;
4628348SEric.Yu@Sun.COM 			return (NULL);
4638348SEric.Yu@Sun.COM 		}
4648348SEric.Yu@Sun.COM 
4658348SEric.Yu@Sun.COM 		/*
4668348SEric.Yu@Sun.COM 		 * The sockparams entry was created, now try to add it
4678348SEric.Yu@Sun.COM 		 * to the list. We need to hold the lock as a WRITER.
4688348SEric.Yu@Sun.COM 		 */
4698348SEric.Yu@Sun.COM 		rw_enter(&sp_ephem_lock, RW_WRITER);
4708348SEric.Yu@Sun.COM 		sp = sockparams_find(&sp_ephem_list, family, type, protocol,
4718489Sshenjian 		    by_devpath, name);
4728348SEric.Yu@Sun.COM 		if (sp != NULL) {
4738348SEric.Yu@Sun.COM 			/*
4748348SEric.Yu@Sun.COM 			 * Someone has requested a matching entry, so just
4758348SEric.Yu@Sun.COM 			 * place a hold on it and release the entry we alloc'ed.
4768348SEric.Yu@Sun.COM 			 */
4778348SEric.Yu@Sun.COM 			SOCKPARAMS_INC_REF(sp);
4788348SEric.Yu@Sun.COM 			rw_exit(&sp_ephem_lock);
4798348SEric.Yu@Sun.COM 
4808348SEric.Yu@Sun.COM 			sockparams_destroy(newsp);
4818348SEric.Yu@Sun.COM 		} else {
4828348SEric.Yu@Sun.COM 			SOCKPARAMS_INC_REF(newsp);
4838348SEric.Yu@Sun.COM 			list_insert_tail(&sp_ephem_list, newsp);
4848348SEric.Yu@Sun.COM 			rw_exit(&sp_ephem_lock);
4858348SEric.Yu@Sun.COM 
4868348SEric.Yu@Sun.COM 			sp = newsp;
4878348SEric.Yu@Sun.COM 		}
4888348SEric.Yu@Sun.COM 		ASSERT(*errorp == 0);
4898348SEric.Yu@Sun.COM 
4908348SEric.Yu@Sun.COM 		return (sp);
4918348SEric.Yu@Sun.COM 	}
4928348SEric.Yu@Sun.COM }
4938348SEric.Yu@Sun.COM 
4948348SEric.Yu@Sun.COM struct sockparams *
4958348SEric.Yu@Sun.COM sockparams_hold_ephemeral_bydev(int family, int type, int protocol,
4968348SEric.Yu@Sun.COM     const char *dev, int kmflag, int *errorp)
4978348SEric.Yu@Sun.COM {
4988348SEric.Yu@Sun.COM 	return (sockparams_hold_ephemeral(family, type, protocol, dev, B_TRUE,
4998348SEric.Yu@Sun.COM 	    kmflag, errorp));
5008348SEric.Yu@Sun.COM }
5018348SEric.Yu@Sun.COM 
5028348SEric.Yu@Sun.COM struct sockparams *
5038348SEric.Yu@Sun.COM sockparams_hold_ephemeral_bymod(int family, int type, int protocol,
5048348SEric.Yu@Sun.COM     const char *mod, int kmflag, int *errorp)
5058348SEric.Yu@Sun.COM {
5068348SEric.Yu@Sun.COM 	return (sockparams_hold_ephemeral(family, type, protocol, mod, B_FALSE,
5078348SEric.Yu@Sun.COM 	    kmflag, errorp));
5088348SEric.Yu@Sun.COM }
5098348SEric.Yu@Sun.COM 
5108348SEric.Yu@Sun.COM /*
5118348SEric.Yu@Sun.COM  * Called when the last socket using the ephemeral entry is dropping
5128348SEric.Yu@Sun.COM  * its' reference. To maintain lock order we must drop the sockparams
5138348SEric.Yu@Sun.COM  * lock before calling this function. As a result, a new reference
5148348SEric.Yu@Sun.COM  * might be placed on the entry, in which case there is nothing to
5158348SEric.Yu@Sun.COM  * do. However, if ref count goes to zero, we delete the entry.
5168348SEric.Yu@Sun.COM  */
5178348SEric.Yu@Sun.COM void
5188348SEric.Yu@Sun.COM sockparams_ephemeral_drop_last_ref(struct sockparams *sp)
5198348SEric.Yu@Sun.COM {
5208348SEric.Yu@Sun.COM 	ASSERT(sp->sp_flags & SOCKPARAMS_EPHEMERAL);
5218348SEric.Yu@Sun.COM 	ASSERT(MUTEX_NOT_HELD(&sp->sp_lock));
5228348SEric.Yu@Sun.COM 
5238348SEric.Yu@Sun.COM 	rw_enter(&sp_ephem_lock, RW_WRITER);
5248348SEric.Yu@Sun.COM 	mutex_enter(&sp->sp_lock);
5258348SEric.Yu@Sun.COM 
5268348SEric.Yu@Sun.COM 	if (--sp->sp_refcnt == 0) {
5278348SEric.Yu@Sun.COM 		list_remove(&sp_ephem_list, sp);
5288348SEric.Yu@Sun.COM 		mutex_exit(&sp->sp_lock);
5298348SEric.Yu@Sun.COM 		rw_exit(&sp_ephem_lock);
5308348SEric.Yu@Sun.COM 
5318348SEric.Yu@Sun.COM 		sockparams_destroy(sp);
5328348SEric.Yu@Sun.COM 	} else {
5338348SEric.Yu@Sun.COM 		mutex_exit(&sp->sp_lock);
5348348SEric.Yu@Sun.COM 		rw_exit(&sp_ephem_lock);
5358348SEric.Yu@Sun.COM 	}
5368348SEric.Yu@Sun.COM }
5378348SEric.Yu@Sun.COM 
5388348SEric.Yu@Sun.COM /*
5398348SEric.Yu@Sun.COM  * sockparams_add(struct sockparams *sp)
5408348SEric.Yu@Sun.COM  *
5418348SEric.Yu@Sun.COM  * Tries to add the given sockparams entry to the global list.
5428348SEric.Yu@Sun.COM  *
5438348SEric.Yu@Sun.COM  * Arguments:
5448348SEric.Yu@Sun.COM  *   sp: the sockparms entry to add
5458348SEric.Yu@Sun.COM  *
5468348SEric.Yu@Sun.COM  * Returns:
5478348SEric.Yu@Sun.COM  *   On success 0, but if an entry already exists, then EEXIST
5488348SEric.Yu@Sun.COM  *   is returned.
5498348SEric.Yu@Sun.COM  *
5508348SEric.Yu@Sun.COM  * Locking:
5518348SEric.Yu@Sun.COM  *   The caller can not be holding splist_lock.
5528348SEric.Yu@Sun.COM  */
5538348SEric.Yu@Sun.COM static int
5548348SEric.Yu@Sun.COM sockparams_add(struct sockparams *sp)
5558348SEric.Yu@Sun.COM {
5568348SEric.Yu@Sun.COM 	ASSERT(!(sp->sp_flags & SOCKPARAMS_EPHEMERAL));
5578348SEric.Yu@Sun.COM 
5588348SEric.Yu@Sun.COM 	rw_enter(&splist_lock, RW_WRITER);
5598348SEric.Yu@Sun.COM 	if (sockparams_find(&sphead, sp->sp_family, sp->sp_type,
5608489Sshenjian 	    sp->sp_protocol, B_TRUE, NULL) != 0) {
5618348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
5628348SEric.Yu@Sun.COM 		return (EEXIST);
5638348SEric.Yu@Sun.COM 	} else {
5648348SEric.Yu@Sun.COM 		list_insert_tail(&sphead, sp);
5658348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
5668348SEric.Yu@Sun.COM 		return (0);
5678348SEric.Yu@Sun.COM 	}
5688348SEric.Yu@Sun.COM }
5698348SEric.Yu@Sun.COM 
5708348SEric.Yu@Sun.COM /*
5718348SEric.Yu@Sun.COM  * sockparams_delete(int family, int type, int protocol)
5728348SEric.Yu@Sun.COM  *
5738348SEric.Yu@Sun.COM  * Marks the sockparams entry for a specific family, type and protocol
5748348SEric.Yu@Sun.COM  * for deletion. The entry is removed from the list and destroyed
5758348SEric.Yu@Sun.COM  * if no one is holding a reference to it.
5768348SEric.Yu@Sun.COM  *
5778348SEric.Yu@Sun.COM  * Arguments:
5788348SEric.Yu@Sun.COM  *   family, type, protocol: the socket type that should be removed.
5798348SEric.Yu@Sun.COM  *
5808348SEric.Yu@Sun.COM  * Returns:
5818348SEric.Yu@Sun.COM  *   On success 0, otherwise ENXIO.
5828348SEric.Yu@Sun.COM  *
5838348SEric.Yu@Sun.COM  * Locking:
5848348SEric.Yu@Sun.COM  *   Caller can not be holding splist_lock or the sp_lock of
5858348SEric.Yu@Sun.COM  *   any sockparams entry.
5868348SEric.Yu@Sun.COM  */
5878348SEric.Yu@Sun.COM static int
5888348SEric.Yu@Sun.COM sockparams_delete(int family, int type, int protocol)
5898348SEric.Yu@Sun.COM {
5908348SEric.Yu@Sun.COM 	struct sockparams *sp;
5918348SEric.Yu@Sun.COM 
5928348SEric.Yu@Sun.COM 	rw_enter(&splist_lock, RW_WRITER);
5938489Sshenjian 	sp = sockparams_find(&sphead, family, type, protocol, B_TRUE, NULL);
5948348SEric.Yu@Sun.COM 
5958348SEric.Yu@Sun.COM 	if (sp != NULL) {
5968348SEric.Yu@Sun.COM 		/*
5978348SEric.Yu@Sun.COM 		 * If no one is holding a reference to the entry, then
5988348SEric.Yu@Sun.COM 		 * we go ahead and remove it from the list and then
5998348SEric.Yu@Sun.COM 		 * destroy it.
6008348SEric.Yu@Sun.COM 		 */
6018348SEric.Yu@Sun.COM 		mutex_enter(&sp->sp_lock);
6028348SEric.Yu@Sun.COM 		if (sp->sp_refcnt != 0) {
6038348SEric.Yu@Sun.COM 			mutex_exit(&sp->sp_lock);
6048348SEric.Yu@Sun.COM 			rw_exit(&splist_lock);
6058348SEric.Yu@Sun.COM 			return (EBUSY);
6068348SEric.Yu@Sun.COM 		}
6078348SEric.Yu@Sun.COM 		mutex_exit(&sp->sp_lock);
6088348SEric.Yu@Sun.COM 		/* Delete the sockparams entry. */
6098348SEric.Yu@Sun.COM 		list_remove(&sphead, sp);
6108348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
6118348SEric.Yu@Sun.COM 
6128348SEric.Yu@Sun.COM 		sockparams_destroy(sp);
6138348SEric.Yu@Sun.COM 		return (0);
6148348SEric.Yu@Sun.COM 	} else {
6158348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
6168348SEric.Yu@Sun.COM 		return (ENXIO);
6178348SEric.Yu@Sun.COM 	}
6188348SEric.Yu@Sun.COM }
6198348SEric.Yu@Sun.COM 
6208348SEric.Yu@Sun.COM /*
6218348SEric.Yu@Sun.COM  * soconfig(int family, int type, int protocol,
6228348SEric.Yu@Sun.COM  *     char *devpath, int devpathlen, char *module)
6238348SEric.Yu@Sun.COM  *
6248348SEric.Yu@Sun.COM  * Add or delete an entry to the sockparams table.
6258348SEric.Yu@Sun.COM  * When devpath and module both are NULL, it will delete an entry.
6268348SEric.Yu@Sun.COM  *
6278348SEric.Yu@Sun.COM  * Arguments:
6288348SEric.Yu@Sun.COM  *   family, type, protocol: the tuple in question
6298348SEric.Yu@Sun.COM  *   devpath: STREAMS device path. Can be NULL for module based sockets.
6308348SEric.Yu@Sun.COM  *   module : Name of the socket module. Can be NULL for STREAMS
6318348SEric.Yu@Sun.COM  *            based sockets.
6328348SEric.Yu@Sun.COM  *   devpathlen: length of the devpath string, or 0 if devpath
6338348SEric.Yu@Sun.COM  *            was statically allocated.
6348348SEric.Yu@Sun.COM  *
6358348SEric.Yu@Sun.COM  * Note:
6368348SEric.Yu@Sun.COM  *   This routine assumes that the caller has kmem_alloced
6378348SEric.Yu@Sun.COM  *   devpath (if devpathlen > 0) and module for this routine to
6388348SEric.Yu@Sun.COM  *   consume.
6398348SEric.Yu@Sun.COM  */
6408348SEric.Yu@Sun.COM int
6418348SEric.Yu@Sun.COM soconfig(int family, int type, int protocol,
6428348SEric.Yu@Sun.COM     char *devpath, int devpathlen, char *module)
6438348SEric.Yu@Sun.COM {
6448348SEric.Yu@Sun.COM 	struct sockparams *sp;
6458348SEric.Yu@Sun.COM 	int error = 0;
6468348SEric.Yu@Sun.COM 
6478348SEric.Yu@Sun.COM 	dprint(0, ("soconfig(%d,%d,%d,%s,%d,%s)\n",
6488348SEric.Yu@Sun.COM 	    family, type, protocol, devpath, devpathlen,
6498348SEric.Yu@Sun.COM 	    module == NULL ? "NULL" : module));
6508348SEric.Yu@Sun.COM 
6518348SEric.Yu@Sun.COM 	if (sockfs_defer_nl7c_init) {
6528348SEric.Yu@Sun.COM 		nl7c_init();
6538348SEric.Yu@Sun.COM 		sockfs_defer_nl7c_init = 0;
6548348SEric.Yu@Sun.COM 	}
6558348SEric.Yu@Sun.COM 
6568348SEric.Yu@Sun.COM 	if (devpath == NULL && module == NULL) {
6578348SEric.Yu@Sun.COM 		/*
6588348SEric.Yu@Sun.COM 		 * Delete existing entry,
6598348SEric.Yu@Sun.COM 		 * both socket module and STEAMS device.
6608348SEric.Yu@Sun.COM 		 */
6618348SEric.Yu@Sun.COM 		ASSERT(module == NULL);
6628348SEric.Yu@Sun.COM 		error = sockparams_delete(family, type, protocol);
6638348SEric.Yu@Sun.COM 	} else {
6648348SEric.Yu@Sun.COM 		/*
6658348SEric.Yu@Sun.COM 		 * Adding an entry
6668348SEric.Yu@Sun.COM 		 * sockparams_create frees mod name and devpath upon failure.
6678348SEric.Yu@Sun.COM 		 */
6688348SEric.Yu@Sun.COM 		sp = sockparams_create(family, type, protocol, module,
6698348SEric.Yu@Sun.COM 		    devpath, devpathlen, 0, KM_SLEEP, &error);
6708348SEric.Yu@Sun.COM 
6718348SEric.Yu@Sun.COM 		if (sp != NULL) {
6728348SEric.Yu@Sun.COM 			error = sockparams_add(sp);
6738348SEric.Yu@Sun.COM 			if (error != 0)
6748348SEric.Yu@Sun.COM 				sockparams_destroy(sp);
6758348SEric.Yu@Sun.COM 		}
6768348SEric.Yu@Sun.COM 	}
6778348SEric.Yu@Sun.COM 
6788348SEric.Yu@Sun.COM 	return (error);
6798348SEric.Yu@Sun.COM }
6808348SEric.Yu@Sun.COM 
6818348SEric.Yu@Sun.COM /*
6828348SEric.Yu@Sun.COM  * solookup(int family, int type, int protocol, struct sockparams **spp)
6838348SEric.Yu@Sun.COM  *
6848348SEric.Yu@Sun.COM  * Lookup an entry in the sockparams list based on the triple. The returned
6858348SEric.Yu@Sun.COM  * entry either exactly match the given tuple, or it is the 'default' entry
6868348SEric.Yu@Sun.COM  * for the given <family, type>. A default entry is on with a protocol
6878348SEric.Yu@Sun.COM  * value of zero.
6888348SEric.Yu@Sun.COM  *
6898348SEric.Yu@Sun.COM  * Arguments:
6908348SEric.Yu@Sun.COM  *   family, type, protocol: tuple to search for
6918348SEric.Yu@Sun.COM  *   spp: Value-return argument
6928348SEric.Yu@Sun.COM  *
6938348SEric.Yu@Sun.COM  * Returns:
6948348SEric.Yu@Sun.COM  *   If an entry is found, 0 is returned and *spp is set to point to the
6958348SEric.Yu@Sun.COM  *   entry. In case an entry is not found, *spp is set to NULL, and an
6968348SEric.Yu@Sun.COM  *   error code is returned. The errors are (in decreasing precedence):
6978348SEric.Yu@Sun.COM  *	EAFNOSUPPORT - address family not in list
6988348SEric.Yu@Sun.COM  *	EPROTONOSUPPORT - address family supported but not protocol.
6998348SEric.Yu@Sun.COM  *	EPROTOTYPE - address family and protocol supported but not socket type.
7008348SEric.Yu@Sun.COM  *
7018348SEric.Yu@Sun.COM  * TODO: should use ddi_modopen()/ddi_modclose()
7028348SEric.Yu@Sun.COM  */
7038348SEric.Yu@Sun.COM int
7048348SEric.Yu@Sun.COM solookup(int family, int type, int protocol, struct sockparams **spp)
7058348SEric.Yu@Sun.COM {
7068348SEric.Yu@Sun.COM 	struct sockparams *sp = NULL;
7078348SEric.Yu@Sun.COM 	int error = 0;
7088348SEric.Yu@Sun.COM 
7098348SEric.Yu@Sun.COM 	*spp = NULL;
7108348SEric.Yu@Sun.COM 	rw_enter(&splist_lock, RW_READER);
7118348SEric.Yu@Sun.COM 
7128348SEric.Yu@Sun.COM 	/*
7138348SEric.Yu@Sun.COM 	 * Search the sockparams list for an appropiate entry.
7148348SEric.Yu@Sun.COM 	 * Hopefully we find an entry that match the exact family,
7158348SEric.Yu@Sun.COM 	 * type and protocol specified by the user, in which case
7168348SEric.Yu@Sun.COM 	 * we return that entry. However, we also keep track of
7178348SEric.Yu@Sun.COM 	 * the default entry for a specific family and type, the
7188348SEric.Yu@Sun.COM 	 * entry of which would have a protocol value of 0.
7198348SEric.Yu@Sun.COM 	 */
7208489Sshenjian 	sp = sockparams_find(&sphead, family, type, protocol, B_TRUE, NULL);
7218348SEric.Yu@Sun.COM 
7228348SEric.Yu@Sun.COM 	if (sp == NULL) {
7238348SEric.Yu@Sun.COM 		int found = 0;
7248348SEric.Yu@Sun.COM 
7258348SEric.Yu@Sun.COM 		/* Determine correct error code */
7268348SEric.Yu@Sun.COM 		for (sp = list_head(&sphead); sp != NULL;
7278348SEric.Yu@Sun.COM 		    sp = list_next(&sphead, sp)) {
7288348SEric.Yu@Sun.COM 			if (sp->sp_family == family && found < 1)
7298348SEric.Yu@Sun.COM 				found = 1;
7308348SEric.Yu@Sun.COM 			if (sp->sp_family == family &&
7318348SEric.Yu@Sun.COM 			    sp->sp_protocol == protocol && found < 2)
7328348SEric.Yu@Sun.COM 				found = 2;
7338348SEric.Yu@Sun.COM 		}
7348348SEric.Yu@Sun.COM 		rw_exit(&splist_lock);
7358348SEric.Yu@Sun.COM 		switch (found) {
7368348SEric.Yu@Sun.COM 		case 0:
7378348SEric.Yu@Sun.COM 			error = EAFNOSUPPORT;
7388348SEric.Yu@Sun.COM 			break;
7398348SEric.Yu@Sun.COM 		case 1:
7408348SEric.Yu@Sun.COM 			error = EPROTONOSUPPORT;
7418348SEric.Yu@Sun.COM 			break;
7428348SEric.Yu@Sun.COM 		case 2:
7438348SEric.Yu@Sun.COM 			error = EPROTOTYPE;
7448348SEric.Yu@Sun.COM 			break;
7458348SEric.Yu@Sun.COM 		}
7468348SEric.Yu@Sun.COM 		return (error);
7478348SEric.Yu@Sun.COM 	}
7488348SEric.Yu@Sun.COM 
7498348SEric.Yu@Sun.COM 	/*
7508348SEric.Yu@Sun.COM 	 * An entry was found.
7518348SEric.Yu@Sun.COM 	 *
7528348SEric.Yu@Sun.COM 	 * We put a hold on the entry early on, so if the
7538348SEric.Yu@Sun.COM 	 * sockmod is not loaded, and we have to exit
7548348SEric.Yu@Sun.COM 	 * splist_lock to call modload(), we know that the
7558348SEric.Yu@Sun.COM 	 * sockparams entry wont go away. That way we don't
7568348SEric.Yu@Sun.COM 	 * have to look up the entry once we come back from
7578348SEric.Yu@Sun.COM 	 * modload().
7588348SEric.Yu@Sun.COM 	 */
7598348SEric.Yu@Sun.COM 	SOCKPARAMS_INC_REF(sp);
7608348SEric.Yu@Sun.COM 	rw_exit(&splist_lock);
7618348SEric.Yu@Sun.COM 
7628348SEric.Yu@Sun.COM 	if (sp->sp_smod_info == NULL) {
7638348SEric.Yu@Sun.COM 		sp->sp_smod_info = smod_lookup_byname(sp->sp_smod_name);
7648348SEric.Yu@Sun.COM 		if (sp->sp_smod_info == NULL) {
7658348SEric.Yu@Sun.COM 			/*
7668348SEric.Yu@Sun.COM 			 * We put a hold on the sockparams entry
7678348SEric.Yu@Sun.COM 			 * earlier, hoping everything would work out.
7688348SEric.Yu@Sun.COM 			 * That obviously did not happen, so release
7698348SEric.Yu@Sun.COM 			 * the hold here.
7708348SEric.Yu@Sun.COM 			 */
7718348SEric.Yu@Sun.COM 			SOCKPARAMS_DEC_REF(sp);
7728348SEric.Yu@Sun.COM 			/*
7738348SEric.Yu@Sun.COM 			 * We should probably mark the sockparams as
7748348SEric.Yu@Sun.COM 			 * "bad", and redo the lookup skipping the
7758348SEric.Yu@Sun.COM 			 * "bad" entries. I.e., sp->sp_mod_state |= BAD,
7768348SEric.Yu@Sun.COM 			 * return (solookup(...))
7778348SEric.Yu@Sun.COM 			 */
7788348SEric.Yu@Sun.COM 			return (ENXIO);
7798348SEric.Yu@Sun.COM 		}
7808348SEric.Yu@Sun.COM 	}
7818348SEric.Yu@Sun.COM 
7828348SEric.Yu@Sun.COM 	/*
7838348SEric.Yu@Sun.COM 	 * Alright, we have a valid sockparams entry.
7848348SEric.Yu@Sun.COM 	 */
7858348SEric.Yu@Sun.COM 	*spp = sp;
7868348SEric.Yu@Sun.COM 	return (0);
7878348SEric.Yu@Sun.COM }
788