sockcommon.c revision 3e95bd4ab92abca814bd28e854607d1975c7dc88
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * CDDL HEADER START
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning *
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The contents of this file are subject to the terms of the
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Common Development and Distribution License (the "License").
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * You may not use this file except in compliance with the License.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning *
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * or http://www.opensolaris.org/os/licensing.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * See the License for the specific language governing permissions
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * and limitations under the License.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning *
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * When distributing Covered Code, include this CDDL HEADER in each
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * If applicable, add the following below this CDDL HEADER, with the
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * fields enclosed by brackets "[]" replaced with your own identifying
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * information: Portions Copyright [yyyy] [name of copyright owner]
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning *
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * CDDL HEADER END
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/types.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/param.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/systm.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/sysmacros.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/debug.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/cmn_err.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/vfs.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/policy.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/modctl.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/sunddi.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/strsun.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/stropts.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/strsubr.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/socket.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/socketvar.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <sys/uio.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <inet/ipclassifier.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <fs/sockfs/sockcommon.h>
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson#include <fs/sockfs/sockfilter_impl.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <fs/sockfs/nl7c.h>
d36be52ed67a633ea5724b688769f5c0e7e4c203Rao Shoaib#include <fs/sockfs/socktpi.h>
bbc000e522dec6271bdf858a8b013d7fc4c10ed4Anders Persson#include <fs/sockfs/sodirect.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#include <inet/ip.h>
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningextern int xnet_skip_checks, xnet_check_print, xnet_truncate_print;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Common socket access functions.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning *
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Instead of accessing the sonode switch directly (i.e., SOP_xxx()),
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the socket_xxx() function should be used.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Try to create a new sonode of the requested <family, type, protocol>.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/* ARGSUSED */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningstruct sonode *
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_create(int family, int type, int protocol, char *devpath, char *mod,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int flags, int version, struct cred *cr, int *errorp)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sonode *so;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sockparams *sp = NULL;
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian int saved_error;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Look for a sockparams entry that match the given criteria.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * solookup() returns with the entry held.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning *errorp = solookup(family, type, protocol, &sp);
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian saved_error = *errorp;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (sp == NULL) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int kmflags = (flags == SOCKET_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * There is no matching sockparams entry. An ephemeral entry is
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * created if the caller specifies a device or a socket module.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (devpath != NULL) {
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian saved_error = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sp = sockparams_hold_ephemeral_bydev(family, type,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning protocol, devpath, kmflags, errorp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else if (mod != NULL) {
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian saved_error = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sp = sockparams_hold_ephemeral_bymod(family, type,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning protocol, mod, kmflags, errorp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else {
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian *errorp = solookup(family, type, 0, &sp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian if (sp == NULL) {
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian if (saved_error && (*errorp == EPROTONOSUPPORT ||
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian *errorp == EPROTOTYPE || *errorp == ENOPROTOOPT))
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian *errorp = saved_error;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (NULL);
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(sp->sp_smod_info != NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(flags == SOCKET_SLEEP || flags == SOCKET_NOSLEEP);
7d64f41b87275bdc41b1f4cddb0fe3d951ef64bdAnders Persson sp->sp_stats.sps_ncreate.value.ui64++;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so = sp->sp_smod_info->smod_sock_create_func(sp, family, type,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning protocol, version, flags, errorp, cr);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so == NULL) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning SOCKPARAMS_DEC_REF(sp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if ((*errorp = SOP_INIT(so, NULL, cr, flags)) == 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Cannot fail, only bumps so_count */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (void) VOP_OPEN(&SOTOV(so), FREAD|FWRITE, cr, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else {
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian if (saved_error && (*errorp == EPROTONOSUPPORT ||
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian *errorp == EPROTOTYPE || *errorp == ENOPROTOOPT))
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian *errorp = saved_error;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning socket_destroy(so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningstruct sonode *
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_newconn(struct sonode *parent, sock_lower_handle_t lh,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sock_downcalls_t *dc, int flags, int *errorp)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sonode *so;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sockparams *sp;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct cred *cr;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if ((cr = CRED()) == NULL)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cr = kcred;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sp = parent->so_sockparams;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(sp != NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
7d64f41b87275bdc41b1f4cddb0fe3d951ef64bdAnders Persson sp->sp_stats.sps_ncreate.value.ui64++;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so = sp->sp_smod_info->smod_sock_create_func(sp, parent->so_family,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning parent->so_type, parent->so_protocol, parent->so_version, flags,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning errorp, cr);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so != NULL) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning SOCKPARAMS_INC_REF(sp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_proto_handle = lh;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_downcalls = dc;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * This function may be called in interrupt context, and CRED()
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * will be NULL. In this case, pass in kcred.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if ((*errorp = SOP_INIT(so, parent, cr, flags)) == 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Cannot fail, only bumps so_count */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (void) VOP_OPEN(&SOTOV(so), FREAD|FWRITE, cr, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning socket_destroy(so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Bind local endpoint.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int flags, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_BIND(so, name, namelen, flags, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Turn socket into a listen socket.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_listen(struct sonode *so, int backlog, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (backlog < 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning backlog = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Use the same qlimit as in BSD. BSD checks the qlimit
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * before queuing the next connection implying that a
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * listen(sock, 0) allows one connection to be queued.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * BSD also uses 1.5 times the requested backlog.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning *
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * XNS Issue 4 required a strict interpretation of the backlog.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * This has been waived subsequently for Issue 4 and the change
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * incorporated in XNS Issue 5. So we aren't required to do
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * anything special for XPG apps.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (backlog >= (INT_MAX - 1) / 3)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning backlog = INT_MAX;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning else
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning backlog = backlog * 3 / 2 + 1;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_LISTEN(so, backlog, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Accept incoming connection.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_accept(struct sonode *lso, int fflag, cred_t *cr, struct sonode **nsop)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_ACCEPT(lso, fflag, cr, nsop));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Active open.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Perssonsocket_connect(struct sonode *so, struct sockaddr *name,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning socklen_t namelen, int fflag, int flags, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int error;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Handle a connect to a name parameter of type AF_UNSPEC like a
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * connect to a null address. This is the portable method to
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * unconnect a socket.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if ((namelen >= sizeof (sa_family_t)) &&
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (name->sa_family == AF_UNSPEC)) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning name = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning namelen = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = SOP_CONNECT(so, name, namelen, fflag, flags, cr);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (error == EHOSTUNREACH && flags & _SOCONNECT_XPG4_2) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * X/Open specification contains a requirement that
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * ENETUNREACH be returned but does not require
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * EHOSTUNREACH. In order to keep the test suite
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * happy we mess with the errno here.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = ENETUNREACH;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (error);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Get address of remote node.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_getpeername(struct sonode *so, struct sockaddr *addr,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning socklen_t *addrlen, boolean_t accept, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(*addrlen > 0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_GETPEERNAME(so, addr, addrlen, accept, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Get local address.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_getsockname(struct sonode *so, struct sockaddr *addr,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning socklen_t *addrlen, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_GETSOCKNAME(so, addr, addrlen, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Called from shutdown().
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_shutdown(struct sonode *so, int how, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_SHUTDOWN(so, how, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Get socket options.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*ARGSUSED*/
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_getsockopt(struct sonode *so, int level, int option_name,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning void *optval, socklen_t *optlenp, int flags, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_GETSOCKOPT(so, level, option_name, optval,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning optlenp, flags, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Set socket options
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_setsockopt(struct sonode *so, int level, int option_name,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning const void *optval, t_uscalar_t optlen, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian int val = 1;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Caller allocates aligned optval, or passes null */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(((uintptr_t)optval & (sizeof (t_scalar_t) - 1)) == 0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* If optval is null optlen is 0, and vice-versa */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(optval != NULL || optlen == 0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(optlen != 0 || optval == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian if (optval == NULL && optlen == 0)
22238f73378cc4cb6fd470f00810959bdd55aff6shenjian optval = &val;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_SETSOCKOPT(so, level, option_name, optval, optlen, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int error = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ssize_t orig_resid = uiop->uio_resid;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Do not bypass the cache if we are doing a local (AF_UNIX) write.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_family == AF_UNIX)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning uiop->uio_extflg |= UIO_COPY_CACHED;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning else
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning uiop->uio_extflg &= ~UIO_COPY_CACHED;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = SOP_SENDMSG(so, msg, uiop, cr);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning switch (error) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning default:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning break;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning case EINTR:
26406123973b347c0418e55a8b8057f0665a16d6Anders Persson case ENOMEM:
34dfe6834cf56b6816de4f0528362e98e322c5a1shenjian /* EAGAIN is EWOULDBLOCK */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning case EWOULDBLOCK:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* We did a partial send */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (uiop->uio_resid != orig_resid)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning break;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning case EPIPE:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if ((so->so_mode & SM_KERNEL) == 0)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning tsignal(curthread, SIGPIPE);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning break;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (error);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct cred *cr, mblk_t **mpp)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int error = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = SOP_SENDMBLK(so, msg, fflag, cr, mpp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (error == EPIPE) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning tsignal(curthread, SIGPIPE);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (error);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int error;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ssize_t orig_resid = uiop->uio_resid;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Do not bypass the cache when reading data, as the application
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * is likely to access the data shortly.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning uiop->uio_extflg |= UIO_COPY_CACHED;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = SOP_RECVMSG(so, msg, uiop, cr);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning switch (error) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning case EINTR:
34dfe6834cf56b6816de4f0528362e98e322c5a1shenjian /* EAGAIN is EWOULDBLOCK */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning case EWOULDBLOCK:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* We did a partial read */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (uiop->uio_resid != orig_resid)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning break;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning default:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning break;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (error);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct cred *cr, int32_t *rvalp)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_IOCTL(so, cmd, arg, mode, cr, rvalp));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_poll(struct sonode *so, short events, int anyyet, short *reventsp,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct pollhead **phpp)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_POLL(so, events, anyyet, reventsp, phpp));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_close(struct sonode *so, int flag, struct cred *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (VOP_CLOSE(SOTOV(so), flag, 1, 0, cr, NULL));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_close_internal(struct sonode *so, int flag, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_count == 0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_CLOSE(so, flag, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningvoid
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_destroy(struct sonode *so)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vn_invalid(SOTOV(so));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning VN_RELE(SOTOV(so));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/* ARGSUSED */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningvoid
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_destroy_internal(struct sonode *so, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sockparams *sp = so->so_sockparams;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_count == 0 && sp != NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sp->sp_smod_info->smod_sock_destroy_func(so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning SOCKPARAMS_DEC_REF(sp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * TODO Once the common vnode ops is available, then the vnops argument
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * should be removed.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*ARGSUSED*/
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningint
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsonode_constructor(void *buf, void *cdrarg, int kmflags)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sonode *so = buf;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct vnode *vp;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vp = so->so_vnode = vn_alloc(kmflags);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (vp == NULL) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (-1);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vp->v_data = so;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vn_setops(vp, socket_vnodeops);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_priv = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_oobmsg = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_proto_handle = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_peercred = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_queued = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_q_head = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_q_last_head = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_head = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_last_head = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_wanted = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_timer_interval = SOCKET_NO_RCVTIMER;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_timer_tid = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_thresh = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson list_create(&so->so_acceptq_list, sizeof (struct sonode),
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson offsetof(struct sonode, so_acceptq_node));
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson list_create(&so->so_acceptq_defer, sizeof (struct sonode),
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson offsetof(struct sonode, so_acceptq_node));
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson list_link_init(&so->so_acceptq_node);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_acceptq_len = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_backlog = 0;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson so->so_listener = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_snd_qfull = B_FALSE;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson so->so_filter_active = 0;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson so->so_filter_tx = 0;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson so->so_filter_defertime = 0;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson so->so_filter_top = NULL;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson so->so_filter_bottom = NULL;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning mutex_init(&so->so_lock, NULL, MUTEX_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning mutex_init(&so->so_acceptq_lock, NULL, MUTEX_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning rw_init(&so->so_fallback_rwlock, NULL, RW_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_state_cv, NULL, CV_DEFAULT, NULL);
6a571a2dc9fd3dada6de18b4c74cc8dcfca99edaAnders Persson cv_init(&so->so_single_cv, NULL, CV_DEFAULT, NULL);
6a571a2dc9fd3dada6de18b4c74cc8dcfca99edaAnders Persson cv_init(&so->so_read_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_acceptq_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_snd_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_rcv_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_copy_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_closing_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/*ARGSUSED*/
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningvoid
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsonode_destructor(void *buf, void *cdrarg)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sonode *so = buf;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct vnode *vp = SOTOV(so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_priv == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_peercred == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_oobmsg == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_rcv_q_head == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson list_destroy(&so->so_acceptq_list);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson list_destroy(&so->so_acceptq_defer);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(!list_link_active(&so->so_acceptq_node));
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_listener == NULL);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_filter_active == 0);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_filter_tx == 0);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_filter_top == NULL);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_filter_bottom == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(vp->v_data == so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(vn_matchops(vp, socket_vnodeops));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vn_free(vp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning mutex_destroy(&so->so_lock);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning mutex_destroy(&so->so_acceptq_lock);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning rw_destroy(&so->so_fallback_rwlock);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_destroy(&so->so_state_cv);
6a571a2dc9fd3dada6de18b4c74cc8dcfca99edaAnders Persson cv_destroy(&so->so_single_cv);
6a571a2dc9fd3dada6de18b4c74cc8dcfca99edaAnders Persson cv_destroy(&so->so_read_cv);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_destroy(&so->so_acceptq_cv);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_destroy(&so->so_snd_cv);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_destroy(&so->so_rcv_cv);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_destroy(&so->so_closing_cv);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningvoid
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsonode_init(struct sonode *so, struct sockparams *sp, int family,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int type, int protocol, sonodeops_t *sops)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vnode_t *vp;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vp = SOTOV(so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_flag = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_state = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_mode = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_count = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_family = family;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_type = type;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_protocol = protocol;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning SOCK_CONNID_INIT(so->so_proto_connid);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_options = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_linger.l_onoff = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_linger.l_linger = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_sndbuf = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_error = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcvtimeo = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_sndtimeo = 0;
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning so->so_xpg_rcvbuf = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_oobmsg == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_oobmark = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_pgrp = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_peercred == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_zoneid = getzoneid();
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_sockparams = sp;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_ops = sops;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
d36be52ed67a633ea5724b688769f5c0e7e4c203Rao Shoaib so->so_not_str = (sops != &sotpi_sonodeops);
d36be52ed67a633ea5724b688769f5c0e7e4c203Rao Shoaib
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_proto_handle = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_downcalls = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_copyflag = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vn_reinit(vp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vp->v_vfsp = rootvfs;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vp->v_type = VSOCK;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vp->v_rdev = sockdev;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_snd_qfull = B_FALSE;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_minpsz = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_wakeup = B_FALSE;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_snd_wakeup = B_FALSE;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_flowctrld = B_FALSE;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_pollev = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning bzero(&so->so_poll_list, sizeof (so->so_poll_list));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning bzero(&so->so_proto_props, sizeof (struct sock_proto_props));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning bzero(&(so->so_ksock_callbacks), sizeof (ksocket_callbacks_t));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_ksock_cb_arg = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_max_addr_len = sizeof (struct sockaddr_storage);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_direct = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vn_exists(vp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningvoid
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsonode_fini(struct sonode *so)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning{
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vnode_t *vp;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_count == 0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_rcv_timer_tid) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(MUTEX_NOT_HELD(&so->so_lock));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (void) untimeout(so->so_rcv_timer_tid);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_timer_tid = 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_poll_list.ph_list != NULL) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning pollwakeup(&so->so_poll_list, POLLERR);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning pollhead_clean(&so->so_poll_list);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
bbc000e522dec6271bdf858a8b013d7fc4c10ed4Anders Persson if (so->so_direct != NULL)
bbc000e522dec6271bdf858a8b013d7fc4c10ed4Anders Persson sod_sock_fini(so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vp = SOTOV(so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning vn_invalid(vp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_peercred != NULL) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning crfree(so->so_peercred);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_peercred = NULL;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning }
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson /* Detach and destroy filters */
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson if (so->so_filter_top != NULL)
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson sof_sonode_cleanup(so);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(list_is_empty(&so->so_acceptq_list));
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(list_is_empty(&so->so_acceptq_defer));
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(!list_link_active(&so->so_acceptq_node));
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_rcv_queued == 0);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_rcv_q_head == NULL);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_rcv_q_last_head == NULL);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_rcv_head == NULL);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson ASSERT(so->so_rcv_last_head == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning}