10f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
20f1702cYu Xiangning<Eric.Yu@Sun.COM> * CDDL HEADER START
30f1702cYu Xiangning<Eric.Yu@Sun.COM> *
40f1702cYu Xiangning<Eric.Yu@Sun.COM> * The contents of this file are subject to the terms of the
50f1702cYu Xiangning<Eric.Yu@Sun.COM> * Common Development and Distribution License (the "License").
60f1702cYu Xiangning<Eric.Yu@Sun.COM> * You may not use this file except in compliance with the License.
70f1702cYu Xiangning<Eric.Yu@Sun.COM> *
80f1702cYu Xiangning<Eric.Yu@Sun.COM> * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90f1702cYu Xiangning<Eric.Yu@Sun.COM> * or http://www.opensolaris.org/os/licensing.
100f1702cYu Xiangning<Eric.Yu@Sun.COM> * See the License for the specific language governing permissions
110f1702cYu Xiangning<Eric.Yu@Sun.COM> * and limitations under the License.
120f1702cYu Xiangning<Eric.Yu@Sun.COM> *
130f1702cYu Xiangning<Eric.Yu@Sun.COM> * When distributing Covered Code, include this CDDL HEADER in each
140f1702cYu Xiangning<Eric.Yu@Sun.COM> * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150f1702cYu Xiangning<Eric.Yu@Sun.COM> * If applicable, add the following below this CDDL HEADER, with the
160f1702cYu Xiangning<Eric.Yu@Sun.COM> * fields enclosed by brackets "[]" replaced with your own identifying
170f1702cYu Xiangning<Eric.Yu@Sun.COM> * information: Portions Copyright [yyyy] [name of copyright owner]
180f1702cYu Xiangning<Eric.Yu@Sun.COM> *
190f1702cYu Xiangning<Eric.Yu@Sun.COM> * CDDL HEADER END
200f1702cYu Xiangning<Eric.Yu@Sun.COM> */
210f1702cYu Xiangning<Eric.Yu@Sun.COM>
220f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
235f1fdc1Anders Persson * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2419581f8Alexander Eremin * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
250f1702cYu Xiangning<Eric.Yu@Sun.COM> */
260f1702cYu Xiangning<Eric.Yu@Sun.COM>
270f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/types.h>
280f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/t_lock.h>
290f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/param.h>
300f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/systm.h>
310f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/sysmacros.h>
320f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/cmn_err.h>
330f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/list.h>
3419581f8Alexander Eremin#include <sys/sunddi.h>
350f1702cYu Xiangning<Eric.Yu@Sun.COM>
360f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/stropts.h>
370f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/socket.h>
380f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <sys/socketvar.h>
390f1702cYu Xiangning<Eric.Yu@Sun.COM>
400f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <fs/sockfs/sockcommon.h>
413e95bd4Anders Persson#include <fs/sockfs/sockfilter_impl.h>
420f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <fs/sockfs/socktpi.h>
430f1702cYu Xiangning<Eric.Yu@Sun.COM>
440f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
450f1702cYu Xiangning<Eric.Yu@Sun.COM> * Socket Parameters
460f1702cYu Xiangning<Eric.Yu@Sun.COM> *
470f1702cYu Xiangning<Eric.Yu@Sun.COM> * Socket parameter (struct sockparams) entries represent the socket types
480f1702cYu Xiangning<Eric.Yu@Sun.COM> * available on the system.
490f1702cYu Xiangning<Eric.Yu@Sun.COM> *
500f1702cYu Xiangning<Eric.Yu@Sun.COM> * Flags (sp_flags):
510f1702cYu Xiangning<Eric.Yu@Sun.COM> *
520f1702cYu Xiangning<Eric.Yu@Sun.COM> * SOCKPARAMS_EPHEMERAL: A temporary sockparams entry that will be deleted
530f1702cYu Xiangning<Eric.Yu@Sun.COM> * as soon as its' ref count drops to zero. In addition, ephemeral entries will
540f1702cYu Xiangning<Eric.Yu@Sun.COM> * never be hooked onto the global sockparams list. Ephemeral entries are
550f1702cYu Xiangning<Eric.Yu@Sun.COM> * created when application requests to create a socket using an application
560f1702cYu Xiangning<Eric.Yu@Sun.COM> * supplied device path, or when a socket is falling back to TPI.
570f1702cYu Xiangning<Eric.Yu@Sun.COM> *
580f1702cYu Xiangning<Eric.Yu@Sun.COM> * Lock order:
593e95bd4Anders Persson *   The lock order is sockconf_lock -> sp_lock.
600f1702cYu Xiangning<Eric.Yu@Sun.COM> */
610f1702cYu Xiangning<Eric.Yu@Sun.COM>extern int 	kobj_path_exists(char *, int);
620f1702cYu Xiangning<Eric.Yu@Sun.COM>
630f1702cYu Xiangning<Eric.Yu@Sun.COM>static int 	sockparams_sdev_init(struct sockparams *, char *, int);
640f1702cYu Xiangning<Eric.Yu@Sun.COM>static void 	sockparams_sdev_fini(struct sockparams *);
650f1702cYu Xiangning<Eric.Yu@Sun.COM>
660f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
670f1702cYu Xiangning<Eric.Yu@Sun.COM> * Global sockparams list (populated via soconfig(1M)).
680f1702cYu Xiangning<Eric.Yu@Sun.COM> */
690f1702cYu Xiangning<Eric.Yu@Sun.COM>static list_t sphead;
700f1702cYu Xiangning<Eric.Yu@Sun.COM>
710f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
720f1702cYu Xiangning<Eric.Yu@Sun.COM> * List of ephemeral sockparams.
730f1702cYu Xiangning<Eric.Yu@Sun.COM> */
740f1702cYu Xiangning<Eric.Yu@Sun.COM>static list_t sp_ephem_list;
750f1702cYu Xiangning<Eric.Yu@Sun.COM>
767d64f41Anders Persson/* Global kstats for sockparams */
777d64f41Anders Perssontypedef struct sockparams_g_stats {
787d64f41Anders Persson	kstat_named_t spgs_ephem_nalloc;
797d64f41Anders Persson	kstat_named_t spgs_ephem_nreuse;
807d64f41Anders Persson} sockparams_g_stats_t;
817d64f41Anders Persson
827d64f41Anders Perssonstatic sockparams_g_stats_t sp_g_stats;
837d64f41Anders Perssonstatic kstat_t *sp_g_kstat;
847d64f41Anders Persson
857d64f41Anders Persson
860f1702cYu Xiangning<Eric.Yu@Sun.COM>void
870f1702cYu Xiangning<Eric.Yu@Sun.COM>sockparams_init(void)
880f1702cYu Xiangning<Eric.Yu@Sun.COM>{
890f1702cYu Xiangning<Eric.Yu@Sun.COM>	list_create(&sphead, sizeof (struct sockparams),
900f1702cYu Xiangning<Eric.Yu@Sun.COM>	    offsetof(struct sockparams, sp_node));
910f1702cYu Xiangning<Eric.Yu@Sun.COM>	list_create(&sp_ephem_list, sizeof (struct sockparams),
920f1702cYu Xiangning<Eric.Yu@Sun.COM>	    offsetof(struct sockparams, sp_node));
930f1702cYu Xiangning<Eric.Yu@Sun.COM>
947d64f41Anders Persson	kstat_named_init(&sp_g_stats.spgs_ephem_nalloc, "ephemeral_nalloc",
957d64f41Anders Persson	    KSTAT_DATA_UINT64);
967d64f41Anders Persson	kstat_named_init(&sp_g_stats.spgs_ephem_nreuse, "ephemeral_nreuse",
977d64f41Anders Persson	    KSTAT_DATA_UINT64);
987d64f41Anders Persson
997d64f41Anders Persson	sp_g_kstat = kstat_create("sockfs", 0, "sockparams", "misc",
1007d64f41Anders Persson	    KSTAT_TYPE_NAMED, sizeof (sp_g_stats) / sizeof (kstat_named_t),
1017d64f41Anders Persson	    KSTAT_FLAG_VIRTUAL);
1027d64f41Anders Persson	if (sp_g_kstat == NULL)
1037d64f41Anders Persson		return;
1047d64f41Anders Persson
1057d64f41Anders Persson	sp_g_kstat->ks_data = &sp_g_stats;
1067d64f41Anders Persson
1077d64f41Anders Persson	kstat_install(sp_g_kstat);
1087d64f41Anders Persson}
1097d64f41Anders Persson
1107d64f41Anders Perssonstatic int
1117d64f41Anders Perssonsockparams_kstat_update(kstat_t *ksp, int rw)
1127d64f41Anders Persson{
1137d64f41Anders Persson	struct sockparams *sp = ksp->ks_private;
1147d64f41Anders Persson	sockparams_stats_t *sps = ksp->ks_data;
1157d64f41Anders Persson
1167d64f41Anders Persson	if (rw == KSTAT_WRITE)
1177d64f41Anders Persson		return (EACCES);
1187d64f41Anders Persson
1197d64f41Anders Persson	sps->sps_nactive.value.ui64 = sp->sp_refcnt;
1207d64f41Anders Persson
1217d64f41Anders Persson	return (0);
1227d64f41Anders Persson}
1237d64f41Anders Persson
1247d64f41Anders Persson/*
1257d64f41Anders Persson * Setup kstats for the given sockparams entry.
1267d64f41Anders Persson */
1277d64f41Anders Perssonstatic void
1287d64f41Anders Perssonsockparams_kstat_init(struct sockparams *sp)
1297d64f41Anders Persson{
1307d64f41Anders Persson	char name[KSTAT_STRLEN];
1317d64f41Anders Persson
1327d64f41Anders Persson	(void) snprintf(name, KSTAT_STRLEN, "socket_%d_%d_%d", sp->sp_family,
1337d64f41Anders Persson	    sp->sp_type, sp->sp_protocol);
1347d64f41Anders Persson
1357d64f41Anders Persson	sp->sp_kstat = kstat_create("sockfs", 0, name, "misc", KSTAT_TYPE_NAMED,
1367d64f41Anders Persson	    sizeof (sockparams_stats_t) / sizeof (kstat_named_t),
1377d64f41Anders Persson	    KSTAT_FLAG_VIRTUAL);
1387d64f41Anders Persson
1397d64f41Anders Persson	if (sp->sp_kstat == NULL)
1407d64f41Anders Persson		return;
1417d64f41Anders Persson
1427d64f41Anders Persson	sp->sp_kstat->ks_data = &sp->sp_stats;
1437d64f41Anders Persson	sp->sp_kstat->ks_update = sockparams_kstat_update;
1447d64f41Anders Persson	sp->sp_kstat->ks_private = sp;
1457d64f41Anders Persson	kstat_install(sp->sp_kstat);
1467d64f41Anders Persson}
1477d64f41Anders Persson
1487d64f41Anders Perssonstatic void
1497d64f41Anders Perssonsockparams_kstat_fini(struct sockparams *sp)
1507d64f41Anders Persson{
1517d64f41Anders Persson	if (sp->sp_kstat != NULL) {
1527d64f41Anders Persson		kstat_delete(sp->sp_kstat);
1537d64f41Anders Persson		sp->sp_kstat = NULL;
1547d64f41Anders Persson	}
1550f1702cYu Xiangning<Eric.Yu@Sun.COM>}
1560f1702cYu Xiangning<Eric.Yu@Sun.COM>
1570f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
1580f1702cYu Xiangning<Eric.Yu@Sun.COM> * sockparams_create(int family, int type, int protocol, char *modname,
1590f1702cYu Xiangning<Eric.Yu@Sun.COM> *     char *devpath, int devpathlen, int flags, int kmflags, int *errorp)
1600f1702cYu Xiangning<Eric.Yu@Sun.COM> *
1610f1702cYu Xiangning<Eric.Yu@Sun.COM> * Create a new sockparams entry.
1620f1702cYu Xiangning<Eric.Yu@Sun.COM> *
1630f1702cYu Xiangning<Eric.Yu@Sun.COM> * Arguments:
1640f1702cYu Xiangning<Eric.Yu@Sun.COM> *   family, type, protocol: specifies the socket type
1650f1702cYu Xiangning<Eric.Yu@Sun.COM> *   modname: Name of the module associated with the socket type. The
1660f1702cYu Xiangning<Eric.Yu@Sun.COM> *            module can be NULL if a device path is given, in which
1670f1702cYu Xiangning<Eric.Yu@Sun.COM> *            case the TPI module is used.
1683e95bd4Anders Persson *   devpath: Path to the STREAMS device. Must be NULL for non-STREAMS
1693e95bd4Anders Persson *            based transports.
1700f1702cYu Xiangning<Eric.Yu@Sun.COM> *   devpathlen: Length of the devpath string. The argument can be 0,
1710f1702cYu Xiangning<Eric.Yu@Sun.COM> *            indicating that devpath was allocated statically, and should
1720f1702cYu Xiangning<Eric.Yu@Sun.COM> *            not be freed when the sockparams entry is destroyed.
1730f1702cYu Xiangning<Eric.Yu@Sun.COM> *
1740f1702cYu Xiangning<Eric.Yu@Sun.COM> *   flags  : SOCKPARAMS_EPHEMERAL is the only flag that is allowed.
1750f1702cYu Xiangning<Eric.Yu@Sun.COM> *   kmflags: KM_{NO,}SLEEP
1760f1702cYu Xiangning<Eric.Yu@Sun.COM> *   errorp : Value-return argument, set when an error occurs.
1770f1702cYu Xiangning<Eric.Yu@Sun.COM> *
1780f1702cYu Xiangning<Eric.Yu@Sun.COM> * Returns:
1790f1702cYu Xiangning<Eric.Yu@Sun.COM> *   On success a new sockparams entry is returned, and *errorp is set
1800f1702cYu Xiangning<Eric.Yu@Sun.COM> *   to 0. On failure NULL is returned and *errorp is set to indicate the
1810f1702cYu Xiangning<Eric.Yu@Sun.COM> *   type of error that occured.
1820f1702cYu Xiangning<Eric.Yu@Sun.COM> *
1830f1702cYu Xiangning<Eric.Yu@Sun.COM> * Notes:
1840f1702cYu Xiangning<Eric.Yu@Sun.COM> *   devpath and modname are freed upon failure.
1850f1702cYu Xiangning<Eric.Yu@Sun.COM> */
1860f1702cYu Xiangning<Eric.Yu@Sun.COM>struct sockparams *
1870f1702cYu Xiangning<Eric.Yu@Sun.COM>sockparams_create(int family, int type, int protocol, char *modname,
1880f1702cYu Xiangning<Eric.Yu@Sun.COM>    char *devpath, int devpathlen, int flags, int kmflags, int *errorp)
1890f1702cYu Xiangning<Eric.Yu@Sun.COM>{
1900f1702cYu Xiangning<Eric.Yu@Sun.COM>	struct sockparams *sp = NULL;
1910f1702cYu Xiangning<Eric.Yu@Sun.COM>	size_t size;
1920f1702cYu Xiangning<Eric.Yu@Sun.COM>
1930f1702cYu Xiangning<Eric.Yu@Sun.COM>	ASSERT((flags & ~SOCKPARAMS_EPHEMERAL) == 0);
1940f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (flags & ~SOCKPARAMS_EPHEMERAL) {
1950f1702cYu Xiangning<Eric.Yu@Sun.COM>		*errorp = EINVAL;
1960f1702cYu Xiangning<Eric.Yu@Sun.COM>		goto error;
1970f1702cYu Xiangning<Eric.Yu@Sun.COM>	}
1980f1702cYu Xiangning<Eric.Yu@Sun.COM>
1993e95bd4Anders Persson	/* either a module or device must be given, but not both */
2000f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (modname == NULL && devpath == NULL) {
2010f1702cYu Xiangning<Eric.Yu@Sun.COM>		*errorp = EINVAL;
2020f1702cYu Xiangning<Eric.Yu@Sun.COM>		goto error;
2030f1702cYu Xiangning<Eric.Yu@Sun.COM>	}
2040f1702cYu Xiangning<Eric.Yu@Sun.COM>
2050f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp = kmem_zalloc(sizeof (*sp), kmflags);
2060f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (sp == NULL) {
2070f1702cYu Xiangning<Eric.Yu@Sun.COM>		*errorp = ENOMEM;
2080f1702cYu Xiangning<Eric.Yu@Sun.COM>		goto error;
2090f1702cYu Xiangning<Eric.Yu@Sun.COM>	}
2100f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_family = family;
2110f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_type = type;
2120f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_protocol = protocol;
2130f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_refcnt = 0;
2140f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_flags = flags;
2150f1702cYu Xiangning<Eric.Yu@Sun.COM>
2163e95bd4Anders Persson	list_create(&sp->sp_auto_filters, sizeof (sp_filter_t),
2173e95bd4Anders Persson	    offsetof(sp_filter_t, spf_node));
2183e95bd4Anders Persson	list_create(&sp->sp_prog_filters, sizeof (sp_filter_t),
2193e95bd4Anders Persson	    offsetof(sp_filter_t, spf_node));
2203e95bd4Anders Persson
22151899d8Anders Persson	kstat_named_init(&sp->sp_stats.sps_nfallback, "nfallback",
22251899d8Anders Persson	    KSTAT_DATA_UINT64);
22351899d8Anders Persson	kstat_named_init(&sp->sp_stats.sps_nactive, "nactive",
22451899d8Anders Persson	    KSTAT_DATA_UINT64);
22551899d8Anders Persson	kstat_named_init(&sp->sp_stats.sps_ncreate, "ncreate",
22651899d8Anders Persson	    KSTAT_DATA_UINT64);
22751899d8Anders Persson
2287d64f41Anders Persson	/*
22951899d8Anders Persson	 * Track how many ephemeral entries we have created.
2307d64f41Anders Persson	 */
2317d64f41Anders Persson	if (sp->sp_flags & SOCKPARAMS_EPHEMERAL)
2327d64f41Anders Persson		sp_g_stats.spgs_ephem_nalloc.value.ui64++;
2337d64f41Anders Persson
2340f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (modname != NULL) {
2350f1702cYu Xiangning<Eric.Yu@Sun.COM>		sp->sp_smod_name = modname;
2360f1702cYu Xiangning<Eric.Yu@Sun.COM>	} else {
2370f1702cYu Xiangning<Eric.Yu@Sun.COM>		size = strlen(SOTPI_SMOD_NAME) + 1;
2380f1702cYu Xiangning<Eric.Yu@Sun.COM>		modname = kmem_zalloc(size, kmflags);
2390f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (modname == NULL) {
2400f1702cYu Xiangning<Eric.Yu@Sun.COM>			*errorp = ENOMEM;
2410f1702cYu Xiangning<Eric.Yu@Sun.COM>			goto error;
2420f1702cYu Xiangning<Eric.Yu@Sun.COM>		}
2430f1702cYu Xiangning<Eric.Yu@Sun.COM>		sp->sp_smod_name = modname;
2440f1702cYu Xiangning<Eric.Yu@Sun.COM>		(void) sprintf(sp->sp_smod_name, "%s", SOTPI_SMOD_NAME);
2450f1702cYu Xiangning<Eric.Yu@Sun.COM>	}
2460f1702cYu Xiangning<Eric.Yu@Sun.COM>
2470f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (devpath != NULL) {
2480f1702cYu Xiangning<Eric.Yu@Sun.COM>		/* Set up the device entry. */
2490f1702cYu Xiangning<Eric.Yu@Sun.COM>		*errorp = sockparams_sdev_init(sp, devpath, devpathlen);
2500f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (*errorp != 0)
2510f1702cYu Xiangning<Eric.Yu@Sun.COM>			goto error;
2520f1702cYu Xiangning<Eric.Yu@Sun.COM>	}
2530f1702cYu Xiangning<Eric.Yu@Sun.COM>
2540f1702cYu Xiangning<Eric.Yu@Sun.COM>	mutex_init(&sp->sp_lock, NULL, MUTEX_DEFAULT, NULL);
2550f1702cYu Xiangning<Eric.Yu@Sun.COM>	*errorp = 0;
2560f1702cYu Xiangning<Eric.Yu@Sun.COM>	return (sp);
2570f1702cYu Xiangning<Eric.Yu@Sun.COM>error:
2580f1702cYu Xiangning<Eric.Yu@Sun.COM>	ASSERT(*errorp != 0);
2590f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (modname != NULL)
2600f1702cYu Xiangning<Eric.Yu@Sun.COM>		kmem_free(modname, strlen(modname) + 1);
2610f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (devpathlen != 0)
2620f1702cYu Xiangning<Eric.Yu@Sun.COM>		kmem_free(devpath, devpathlen);
26351899d8Anders Persson	if (sp != NULL)
2640f1702cYu Xiangning<Eric.Yu@Sun.COM>		kmem_free(sp, sizeof (*sp));
2650f1702cYu Xiangning<Eric.Yu@Sun.COM>	return (NULL);
2660f1702cYu Xiangning<Eric.Yu@Sun.COM>}
2670f1702cYu Xiangning<Eric.Yu@Sun.COM>
2680f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
2690f1702cYu Xiangning<Eric.Yu@Sun.COM> * Initialize the STREAMS device aspect of the sockparams entry.
2700f1702cYu Xiangning<Eric.Yu@Sun.COM> */
2710f1702cYu Xiangning<Eric.Yu@Sun.COM>static int
2720f1702cYu Xiangning<Eric.Yu@Sun.COM>sockparams_sdev_init(struct sockparams *sp, char *devpath, int devpathlen)
2730f1702cYu Xiangning<Eric.Yu@Sun.COM>{
2740f1702cYu Xiangning<Eric.Yu@Sun.COM>	vnode_t *vp = NULL;
2750f1702cYu Xiangning<Eric.Yu@Sun.COM>	int error;
2760f1702cYu Xiangning<Eric.Yu@Sun.COM>
2770f1702cYu Xiangning<Eric.Yu@Sun.COM>	ASSERT(devpath != NULL);
2780f1702cYu Xiangning<Eric.Yu@Sun.COM>
2790f1702cYu Xiangning<Eric.Yu@Sun.COM>	if ((error = sogetvp(devpath, &vp, UIO_SYSSPACE)) != 0) {
2800f1702cYu Xiangning<Eric.Yu@Sun.COM>		dprint(0, ("sockparams_sdev_init: vp %s failed with %d\n",
2810f1702cYu Xiangning<Eric.Yu@Sun.COM>		    devpath, error));
2820f1702cYu Xiangning<Eric.Yu@Sun.COM>		return (error);
2830f1702cYu Xiangning<Eric.Yu@Sun.COM>	}
2840f1702cYu Xiangning<Eric.Yu@Sun.COM>
2850f1702cYu Xiangning<Eric.Yu@Sun.COM>	ASSERT(vp != NULL);
2860f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_sdev_info.sd_vnode = vp;
2870f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_sdev_info.sd_devpath = devpath;
2880f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_sdev_info.sd_devpathlen = devpathlen;
2890f1702cYu Xiangning<Eric.Yu@Sun.COM>
2900f1702cYu Xiangning<Eric.Yu@Sun.COM>	return (0);
2910f1702cYu Xiangning<Eric.Yu@Sun.COM>}
2920f1702cYu Xiangning<Eric.Yu@Sun.COM>
2930f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
2940f1702cYu Xiangning<Eric.Yu@Sun.COM> * sockparams_destroy(struct sockparams *sp)
2950f1702cYu Xiangning<Eric.Yu@Sun.COM> *
2960f1702cYu Xiangning<Eric.Yu@Sun.COM> * Releases all the resources associated with the sockparams entry,
2970f1702cYu Xiangning<Eric.Yu@Sun.COM> * and frees the sockparams entry.
2980f1702cYu Xiangning<Eric.Yu@Sun.COM> *
2990f1702cYu Xiangning<Eric.Yu@Sun.COM> * Arguments:
3000f1702cYu Xiangning<Eric.Yu@Sun.COM> *   sp: the sockparams entry to destroy.
3010f1702cYu Xiangning<Eric.Yu@Sun.COM> *
3020f1702cYu Xiangning<Eric.Yu@Sun.COM> * Returns:
3030f1702cYu Xiangning<Eric.Yu@Sun.COM> *   Nothing.
3040f1702cYu Xiangning<Eric.Yu@Sun.COM> *
3050f1702cYu Xiangning<Eric.Yu@Sun.COM> * Locking:
3060f1702cYu Xiangning<Eric.Yu@Sun.COM> *   The sp_lock of the entry can not be held.
3070f1702cYu Xiangning<Eric.Yu@Sun.COM> */
3080f1702cYu Xiangning<Eric.Yu@Sun.COM>void
3090f1702cYu Xiangning<Eric.Yu@Sun.COM>sockparams_destroy(struct sockparams *sp)
3100f1702cYu Xiangning<Eric.Yu@Sun.COM>{
3110f1702cYu Xiangning<Eric.Yu@Sun.COM>	ASSERT(sp->sp_refcnt == 0);
3120f1702cYu Xiangning<Eric.Yu@Sun.COM>	ASSERT(!list_link_active(&sp->sp_node));
3130f1702cYu Xiangning<Eric.Yu@Sun.COM>
3140f1702cYu Xiangning<Eric.Yu@Sun.COM>	sockparams_sdev_fini(sp);
3150f1702cYu Xiangning<Eric.Yu@Sun.COM>
3160f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (sp->sp_smod_info != NULL)
3175f1fdc1Anders Persson		SMOD_DEC_REF(sp->sp_smod_info, sp->sp_smod_name);
3180f1702cYu Xiangning<Eric.Yu@Sun.COM>	kmem_free(sp->sp_smod_name, strlen(sp->sp_smod_name) + 1);
3190f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_smod_name = NULL;
3200f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_smod_info = NULL;
3210f1702cYu Xiangning<Eric.Yu@Sun.COM>	mutex_destroy(&sp->sp_lock);
3227d64f41Anders Persson	sockparams_kstat_fini(sp);
3230f1702cYu Xiangning<Eric.Yu@Sun.COM>
3243e95bd4Anders Persson	sof_sockparams_fini(sp);
3253e95bd4Anders Persson	list_destroy(&sp->sp_auto_filters);
3263e95bd4Anders Persson	list_destroy(&sp->sp_prog_filters);
3273e95bd4Anders Persson
3280f1702cYu Xiangning<Eric.Yu@Sun.COM>	kmem_free(sp, sizeof (*sp));
3290f1702cYu Xiangning<Eric.Yu@Sun.COM>}
3300f1702cYu Xiangning<Eric.Yu@Sun.COM>
3310f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
3320f1702cYu Xiangning<Eric.Yu@Sun.COM> * Clean up the STREAMS device part of the sockparams entry.
3330f1702cYu Xiangning<Eric.Yu@Sun.COM> */
3340f1702cYu Xiangning<Eric.Yu@Sun.COM>static void
3350f1702cYu Xiangning<Eric.Yu@Sun.COM>sockparams_sdev_fini(struct sockparams *sp)
3360f1702cYu Xiangning<Eric.Yu@Sun.COM>{
3370f1702cYu Xiangning<Eric.Yu@Sun.COM>	sdev_info_t sd;
3380f1702cYu Xiangning<Eric.Yu@Sun.COM>
3390f1702cYu Xiangning<Eric.Yu@Sun.COM>	/*
3400f1702cYu Xiangning<Eric.Yu@Sun.COM>	 * if the entry does not have a STREAMS device, then there
3410f1702cYu Xiangning<Eric.Yu@Sun.COM>	 * is nothing to do.
3420f1702cYu Xiangning<Eric.Yu@Sun.COM>	 */
3430f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (!SOCKPARAMS_HAS_DEVICE(sp))
3440f1702cYu Xiangning<Eric.Yu@Sun.COM>		return;
3450f1702cYu Xiangning<Eric.Yu@Sun.COM>
3460f1702cYu Xiangning<Eric.Yu@Sun.COM>	sd = sp->sp_sdev_info;
3470f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (sd.sd_vnode != NULL)
3480f1702cYu Xiangning<Eric.Yu@Sun.COM>		VN_RELE(sd.sd_vnode);
3490f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (sd.sd_devpathlen != 0)
3500f1702cYu Xiangning<Eric.Yu@Sun.COM>		kmem_free(sd.sd_devpath, sd.sd_devpathlen);
3510f1702cYu Xiangning<Eric.Yu@Sun.COM>
3520f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_sdev_info.sd_vnode = NULL;
3530f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp->sp_sdev_info.sd_devpath = NULL;
3540f1702cYu Xiangning<Eric.Yu@Sun.COM>}
3550f1702cYu Xiangning<Eric.Yu@Sun.COM>
3560f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
3570f1702cYu Xiangning<Eric.Yu@Sun.COM> * Look for a matching sockparams entry on the given list.
3580f1702cYu Xiangning<Eric.Yu@Sun.COM> * The caller must hold the associated list lock.
3590f1702cYu Xiangning<Eric.Yu@Sun.COM> */
3600f1702cYu Xiangning<Eric.Yu@Sun.COM>static struct sockparams *
3610f1702cYu Xiangning<Eric.Yu@Sun.COM>sockparams_find(list_t *list, int family, int type, int protocol,
36222238f7shenjian    boolean_t by_devpath, const char *name)
3630f1702cYu Xiangning<Eric.Yu@Sun.COM>{
3640f1702cYu Xiangning<Eric.Yu@Sun.COM>	struct sockparams *sp;
3650f1702cYu Xiangning<Eric.Yu@Sun.COM>
3660f1702cYu Xiangning<Eric.Yu@Sun.COM>	for (sp = list_head(list); sp != NULL; sp = list_next(list, sp)) {
36722238f7shenjian		if (sp->sp_family == family && sp->sp_type == type) {
3680f1702cYu Xiangning<Eric.Yu@Sun.COM>			if (sp->sp_protocol == protocol) {
36922238f7shenjian				if (name == NULL)
3700f1702cYu Xiangning<Eric.Yu@Sun.COM>					break;
37122238f7shenjian				else if (by_devpath &&
3720f1702cYu Xiangning<Eric.Yu@Sun.COM>				    sp->sp_sdev_info.sd_devpath != NULL &&
3730f1702cYu Xiangning<Eric.Yu@Sun.COM>				    strcmp(sp->sp_sdev_info.sd_devpath,
3740f1702cYu Xiangning<Eric.Yu@Sun.COM>				    name) == 0)
3750f1702cYu Xiangning<Eric.Yu@Sun.COM>					break;
37622238f7shenjian				else if (strcmp(sp->sp_smod_name, name) == 0)
3770f1702cYu Xiangning<Eric.Yu@Sun.COM>					break;
3780f1702cYu Xiangning<Eric.Yu@Sun.COM>			}
3790f1702cYu Xiangning<Eric.Yu@Sun.COM>		}
3800f1702cYu Xiangning<Eric.Yu@Sun.COM>	}
38122238f7shenjian	return (sp);
3820f1702cYu Xiangning<Eric.Yu@Sun.COM>}
3830f1702cYu Xiangning<Eric.Yu@Sun.COM>
3840f1702cYu Xiangning<Eric.Yu@Sun.COM>/*
3850f1702cYu Xiangning<Eric.Yu@Sun.COM> * sockparams_hold_ephemeral()
3860f1702cYu Xiangning<Eric.Yu@Sun.COM> *
3870f1702cYu Xiangning<Eric.Yu@Sun.COM> * Returns an ephemeral sockparams entry of the requested family, type and
3880f1702cYu Xiangning<Eric.Yu@Sun.COM> * protocol. The entry is returned held, and the caller is responsible for
3890f1702cYu Xiangning<Eric.Yu@Sun.COM> * dropping the reference using SOCKPARAMS_DEC_REF() once done.
3900f1702cYu Xiangning<Eric.Yu@Sun.COM> *
3910f1702cYu Xiangning<Eric.Yu@Sun.COM> * All ephemeral entries are on list (sp_ephem_list). If there is an
3920f1702cYu Xiangning<Eric.Yu@Sun.COM> * entry on the list that match the search criteria, then a reference is
3930f1702cYu Xiangning<Eric.Yu@Sun.COM> * placed on that entry. Otherwise, a new entry is created and inserted
3940f1702cYu Xiangning<Eric.Yu@Sun.COM> * in the list. The entry is removed from the list when the last reference
3950f1702cYu Xiangning<Eric.Yu@Sun.COM> * is dropped.
3960f1702cYu Xiangning<Eric.Yu@Sun.COM> *
3970f1702cYu Xiangning<Eric.Yu@Sun.COM> * The tpi flag is used to determine whether name refers to a device or
3980f1702cYu Xiangning<Eric.Yu@Sun.COM> * module name.
3990f1702cYu Xiangning<Eric.Yu@Sun.COM> */
4000f1702cYu Xiangning<Eric.Yu@Sun.COM>static struct sockparams *
4010f1702cYu Xiangning<Eric.Yu@Sun.COM>sockparams_hold_ephemeral(int family, int type, int protocol,
40222238f7shenjian    const char *name, boolean_t by_devpath, int kmflag, int *errorp)
4030f1702cYu Xiangning<Eric.Yu@Sun.COM>{
4040f1702cYu Xiangning<Eric.Yu@Sun.COM>	struct sockparams *sp = NULL;
4050f1702cYu Xiangning<Eric.Yu@Sun.COM>	*errorp = 0;
4060f1702cYu Xiangning<Eric.Yu@Sun.COM>
4070f1702cYu Xiangning<Eric.Yu@Sun.COM>	/*
4080f1702cYu Xiangning<Eric.Yu@Sun.COM>	 * First look for an existing entry
4090f1702cYu Xiangning<Eric.Yu@Sun.COM>	 */
4103e95bd4Anders Persson	rw_enter(&sockconf_lock, RW_READER);
4110f1702cYu Xiangning<Eric.Yu@Sun.COM>	sp = sockparams_find(&sp_ephem_list, family, type, protocol,
41222238f7shenjian	    by_devpath, name);
4130f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (sp != NULL) {
4140f1702cYu Xiangning<Eric.Yu@Sun.COM>		SOCKPARAMS_INC_REF(sp);
4153e95bd4Anders Persson		rw_exit(&sockconf_lock);
4167d64f41Anders Persson		sp_g_stats.spgs_ephem_nreuse.value.ui64++;
4170f1702cYu Xiangning<Eric.Yu@Sun.COM>
4180f1702cYu Xiangning<Eric.Yu@Sun.COM>		return (sp);
4190f1702cYu Xiangning<Eric.Yu@Sun.COM>	} else {
4200f1702cYu Xiangning<Eric.Yu@Sun.COM>		struct sockparams *newsp = NULL;
4210f1702cYu Xiangning<Eric.Yu@Sun.COM>		char *namebuf = NULL;
4220f1702cYu Xiangning<Eric.Yu@Sun.COM>		int namelen = 0;
4230f1702cYu Xiangning<Eric.Yu@Sun.COM>
4243e95bd4Anders Persson		rw_exit(&sockconf_lock);
4250f1702cYu Xiangning<Eric.Yu@Sun.COM>
4260f1702cYu Xiangning<Eric.Yu@Sun.COM>		namelen = strlen(name) + 1;
4270f1702cYu Xiangning<Eric.Yu@Sun.COM>		namebuf = kmem_alloc(namelen, kmflag);
4280f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (namebuf == NULL) {
4290f1702cYu Xiangning<Eric.Yu@Sun.COM>			*errorp = ENOMEM;
4300f1702cYu Xiangning<Eric.Yu@Sun.COM>			return (NULL);
4310f1702cYu Xiangning<Eric.Yu@Sun.COM>		}
4320f1702cYu Xiangning<Eric.Yu@Sun.COM>
4330f1702cYu Xiangning<Eric.Yu@Sun.COM>		(void *)strncpy(namebuf, name, namelen);
43422238f7shenjian		if (by_devpath) {
4350f1702cYu Xiangning<Eric.Yu@Sun.COM>			newsp = sockparams_create(family, type,
4360f1702cYu Xiangning<Eric.Yu@Sun.COM>			    protocol, NULL, namebuf, namelen,
4370f1702cYu Xiangning<Eric.Yu@Sun.COM>			    SOCKPARAMS_EPHEMERAL, kmflag, errorp);
4380f1702cYu Xiangning<Eric.Yu@Sun.COM>		} else {
4390f1702cYu Xiangning<Eric.Yu@Sun.COM>			newsp = sockparams_create(family, type,
4400f1702cYu Xiangning<Eric.Yu@Sun.COM>			    protocol, namebuf, NULL, 0,
4410f1702cYu Xiangning<Eric.Yu@Sun.COM>			    SOCKPARAMS_EPHEMERAL, kmflag, errorp);
4420f1702cYu Xiangning<Eric.Yu@Sun.COM>		}
4430f1702cYu Xiangning<Eric.Yu@Sun.COM>
4440f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (newsp == NULL) {
4450f1702cYu Xiangning<Eric.Yu@Sun.COM>			ASSERT(*errorp != 0);
4460f1702cYu Xiangning<Eric.Yu@Sun.COM>			return (NULL);
4470f1702cYu Xiangning<Eric.Yu@Sun.COM>		}
4480f1702cYu Xiangning<Eric.Yu@Sun.COM>
4490f1702cYu Xiangning<Eric.Yu@Sun.COM>		/*
4500f1702cYu Xiangning<Eric.Yu@Sun.COM>		 * Time to load the socket module.
4510f1702cYu Xiangning<Eric.Yu@Sun.COM>		 */
4520f1702cYu Xiangning<Eric.Yu@Sun.COM>		ASSERT(newsp->sp_smod_info == NULL);
4530f1702cYu Xiangning<Eric.Yu@Sun.COM>		newsp->sp_smod_info =
4540f1702cYu Xiangning<Eric.Yu@Sun.COM>		    smod_lookup_byname(newsp->sp_smod_name);
4550f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (newsp->sp_smod_info == NULL) {
4560f1702cYu Xiangning<Eric.Yu@Sun.COM>			/* Failed to load */
4570f1702cYu Xiangning<Eric.Yu@Sun.COM>			sockparams_destroy(newsp);
4580f1702cYu Xiangning<Eric.Yu@Sun.COM>			*errorp = ENXIO;
4590f1702cYu Xiangning<Eric.Yu@Sun.COM>			return (NULL);
4600f1702cYu Xiangning<Eric.Yu@Sun.COM>		}
4610f1702cYu Xiangning<Eric.Yu@Sun.COM>
4620f1702cYu Xiangning<Eric.Yu@Sun.COM>		/*
4630f1702cYu Xiangning<Eric.Yu@Sun.COM>		 * The sockparams entry was created, now try to add it
4640f1702cYu Xiangning<Eric.Yu@Sun.COM>		 * to the list. We need to hold the lock as a WRITER.
4650f1702cYu Xiangning<Eric.Yu@Sun.COM>		 */
4663e95bd4Anders Persson		rw_enter(&sockconf_lock, RW_WRITER);
4670f1702cYu Xiangning<Eric.Yu@Sun.COM>		sp = sockparams_find(&sp_ephem_list, family, type, protocol,
46822238f7shenjian		    by_devpath, name);
4690f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (sp != NULL) {
4700f1702cYu Xiangning<Eric.Yu@Sun.COM>			/*
4710f1702cYu Xiangning<Eric.Yu@Sun.COM>			 * Someone has requested a matching entry, so just
4720f1702cYu Xiangning<Eric.Yu@Sun.COM>			 * place a hold on it and release the entry we alloc'ed.
4730f1702cYu Xiangning<Eric.Yu@Sun.COM>			 */
4740f1702cYu Xiangning<Eric.Yu@Sun.COM>			SOCKPARAMS_INC_REF(sp);
4753e95bd4Anders Persson			rw_exit(&sockconf_lock);
4760f1702cYu Xiangning<Eric.Yu@Sun.COM>
4770f1702cYu Xiangning<Eric.Yu@Sun.COM>			sockparams_destroy(newsp);
478