17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52caf0dcdSrshoaib * Common Development and Distribution License (the "License").
62caf0dcdSrshoaib * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
212caf0dcdSrshoaib
227c478bd9Sstevel@tonic-gate /*
233e95bd4aSAnders Persson * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24acb55917SPatrick Mooney * Copyright 2015, Joyent, Inc.
25f012ee0cSGordon Ross * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
2615f90b02SGarrett D'Amore * Copyright 2022 Garrett D'Amore
27dfc0fed8SRobert Mustacchi */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/buf.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
370f1702c5SYu Xiangning #include <sys/kmem_impl.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/debug.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/time.h>
447c478bd9Sstevel@tonic-gate #include <sys/file.h>
457c478bd9Sstevel@tonic-gate #include <sys/open.h>
467c478bd9Sstevel@tonic-gate #include <sys/user.h>
477c478bd9Sstevel@tonic-gate #include <sys/termios.h>
487c478bd9Sstevel@tonic-gate #include <sys/stream.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
507c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
510f1702c5SYu Xiangning #include <sys/suntpi.h>
527c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
547c478bd9Sstevel@tonic-gate #include <sys/flock.h>
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
567c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
577c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
587c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #include <sys/socket.h>
617c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
62ff550d0eSmasputra #include <sys/sockio.h>
637c478bd9Sstevel@tonic-gate #include <netinet/in.h>
647c478bd9Sstevel@tonic-gate #include <sys/un.h>
657c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
687c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
697c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
707c478bd9Sstevel@tonic-gate #include <sys/timod.h> /* TI_GETMYNAME, TI_GETPEERNAME */
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate #include <c2/audit.h>
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate #include <inet/common.h>
757c478bd9Sstevel@tonic-gate #include <inet/ip.h>
767c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
777c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
78ff550d0eSmasputra #include <inet/udp_impl.h>
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate #include <sys/zone.h>
817c478bd9Sstevel@tonic-gate
820f1702c5SYu Xiangning #include <fs/sockfs/sockcommon.h>
830f1702c5SYu Xiangning #include <fs/sockfs/socktpi.h>
840f1702c5SYu Xiangning #include <fs/sockfs/socktpi_impl.h>
850f1702c5SYu Xiangning
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * Possible failures when memory can't be allocated. The documented behavior:
887c478bd9Sstevel@tonic-gate *
89e9f74ea5SToomas Soome * 5.5: 4.X: XNET:
907c478bd9Sstevel@tonic-gate * accept: ENOMEM/ENOSR/EINTR - (EINTR) ENOMEM/ENOBUFS/ENOSR/
917c478bd9Sstevel@tonic-gate * EINTR
927c478bd9Sstevel@tonic-gate * (4.X does not document EINTR but returns it)
937c478bd9Sstevel@tonic-gate * bind: ENOSR - ENOBUFS/ENOSR
94e9f74ea5SToomas Soome * connect: EINTR EINTR ENOBUFS/ENOSR/EINTR
957c478bd9Sstevel@tonic-gate * getpeername: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR
967c478bd9Sstevel@tonic-gate * getsockname: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR
977c478bd9Sstevel@tonic-gate * (4.X getpeername and getsockname do not fail in practice)
987c478bd9Sstevel@tonic-gate * getsockopt: ENOMEM/ENOSR - ENOBUFS/ENOSR
997c478bd9Sstevel@tonic-gate * listen: - - ENOBUFS
1007c478bd9Sstevel@tonic-gate * recv: ENOMEM/ENOSR/EINTR EINTR ENOBUFS/ENOMEM/ENOSR/
1017c478bd9Sstevel@tonic-gate * EINTR
1027c478bd9Sstevel@tonic-gate * send: ENOMEM/ENOSR/EINTR ENOBUFS/EINTR ENOBUFS/ENOMEM/ENOSR/
1037c478bd9Sstevel@tonic-gate * EINTR
1047c478bd9Sstevel@tonic-gate * setsockopt: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR
1057c478bd9Sstevel@tonic-gate * shutdown: ENOMEM/ENOSR - ENOBUFS/ENOSR
1067c478bd9Sstevel@tonic-gate * socket: ENOMEM/ENOSR ENOBUFS ENOBUFS/ENOMEM/ENOSR
1077c478bd9Sstevel@tonic-gate * socketpair: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR
1087c478bd9Sstevel@tonic-gate *
1097c478bd9Sstevel@tonic-gate * Resolution. When allocation fails:
1107c478bd9Sstevel@tonic-gate * recv: return EINTR
1117c478bd9Sstevel@tonic-gate * send: return EINTR
1127c478bd9Sstevel@tonic-gate * connect, accept: EINTR
1137c478bd9Sstevel@tonic-gate * bind, listen, shutdown (unbind, unix_close, disconnect): sleep
1147c478bd9Sstevel@tonic-gate * socket, socketpair: ENOBUFS
1157c478bd9Sstevel@tonic-gate * getpeername, getsockname: sleep
1167c478bd9Sstevel@tonic-gate * getsockopt, setsockopt: sleep
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * Variables that make sockfs do something other than the standard TPI
1227c478bd9Sstevel@tonic-gate * for the AF_INET transports.
1237c478bd9Sstevel@tonic-gate *
1247c478bd9Sstevel@tonic-gate * solisten_tpi_tcp:
1257c478bd9Sstevel@tonic-gate * TCP can handle a O_T_BIND_REQ with an increased backlog even though
1267c478bd9Sstevel@tonic-gate * the transport is already bound. This is needed to avoid loosing the
1277c478bd9Sstevel@tonic-gate * port number should listen() do a T_UNBIND_REQ followed by a
1287c478bd9Sstevel@tonic-gate * O_T_BIND_REQ.
1297c478bd9Sstevel@tonic-gate *
1307c478bd9Sstevel@tonic-gate * soconnect_tpi_udp:
1317c478bd9Sstevel@tonic-gate * UDP and ICMP can handle a T_CONN_REQ.
1327c478bd9Sstevel@tonic-gate * This is needed to make the sequence of connect(), getsockname()
1337c478bd9Sstevel@tonic-gate * return the local IP address used to send packets to the connected to
1347c478bd9Sstevel@tonic-gate * destination.
1357c478bd9Sstevel@tonic-gate *
1367c478bd9Sstevel@tonic-gate * soconnect_tpi_tcp:
1377c478bd9Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ.
1387c478bd9Sstevel@tonic-gate * Set this to non-zero to send TPI conformant messages to TCP in this
1397c478bd9Sstevel@tonic-gate * respect. This is a performance optimization.
1407c478bd9Sstevel@tonic-gate *
1417c478bd9Sstevel@tonic-gate * soaccept_tpi_tcp:
1427c478bd9Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without the acceptor being bound.
1437c478bd9Sstevel@tonic-gate * This is a performance optimization that has been picked up in XTI.
1447c478bd9Sstevel@tonic-gate *
1457c478bd9Sstevel@tonic-gate * soaccept_tpi_multioptions:
1467c478bd9Sstevel@tonic-gate * When inheriting SOL_SOCKET options from the listener to the accepting
1477c478bd9Sstevel@tonic-gate * socket send them as a single message for AF_INET{,6}.
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate int solisten_tpi_tcp = 0;
1507c478bd9Sstevel@tonic-gate int soconnect_tpi_udp = 0;
1517c478bd9Sstevel@tonic-gate int soconnect_tpi_tcp = 0;
1527c478bd9Sstevel@tonic-gate int soaccept_tpi_tcp = 0;
1537c478bd9Sstevel@tonic-gate int soaccept_tpi_multioptions = 1;
1547c478bd9Sstevel@tonic-gate #else /* SOCK_TEST */
1557c478bd9Sstevel@tonic-gate #define soconnect_tpi_tcp 0
1567c478bd9Sstevel@tonic-gate #define soconnect_tpi_udp 0
1577c478bd9Sstevel@tonic-gate #define solisten_tpi_tcp 0
1587c478bd9Sstevel@tonic-gate #define soaccept_tpi_tcp 0
1597c478bd9Sstevel@tonic-gate #define soaccept_tpi_multioptions 1
1607c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
1637c478bd9Sstevel@tonic-gate extern int do_useracc;
1647c478bd9Sstevel@tonic-gate extern clock_t sock_test_timelimit;
1657c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
1667c478bd9Sstevel@tonic-gate
167d28d4716SJerry Jelinek extern uint32_t ucredsize;
168d28d4716SJerry Jelinek
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate * Some X/Open added checks might have to be backed out to keep SunOS 4.X
1717c478bd9Sstevel@tonic-gate * applications working. Turn on this flag to disable these checks.
1727c478bd9Sstevel@tonic-gate */
1737c478bd9Sstevel@tonic-gate int xnet_skip_checks = 0;
1747c478bd9Sstevel@tonic-gate int xnet_check_print = 0;
1757c478bd9Sstevel@tonic-gate int xnet_truncate_print = 0;
1767c478bd9Sstevel@tonic-gate
1770f1702c5SYu Xiangning static void sotpi_destroy(struct sonode *);
1780f1702c5SYu Xiangning static struct sonode *sotpi_create(struct sockparams *, int, int, int, int,
1790f1702c5SYu Xiangning int, int *, cred_t *cr);
1800f1702c5SYu Xiangning
1810f1702c5SYu Xiangning static boolean_t sotpi_info_create(struct sonode *, int);
1820f1702c5SYu Xiangning static void sotpi_info_init(struct sonode *);
183e9f74ea5SToomas Soome static void sotpi_info_fini(struct sonode *);
184e9f74ea5SToomas Soome static void sotpi_info_destroy(struct sonode *);
1850f1702c5SYu Xiangning
1860f1702c5SYu Xiangning /*
1870f1702c5SYu Xiangning * Do direct function call to the transport layer below; this would
1880f1702c5SYu Xiangning * also allow the transport to utilize read-side synchronous stream
1890f1702c5SYu Xiangning * interface if necessary. This is a /etc/system tunable that must
1900f1702c5SYu Xiangning * not be modified on a running system. By default this is enabled
1910f1702c5SYu Xiangning * for performance reasons and may be disabled for debugging purposes.
1920f1702c5SYu Xiangning */
1930f1702c5SYu Xiangning boolean_t socktpi_direct = B_TRUE;
1940f1702c5SYu Xiangning
1950f1702c5SYu Xiangning static struct kmem_cache *socktpi_cache, *socktpi_unix_cache;
1960f1702c5SYu Xiangning
1977c478bd9Sstevel@tonic-gate extern void sigintr(k_sigset_t *, int);
1987c478bd9Sstevel@tonic-gate extern void sigunintr(k_sigset_t *);
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate static int sotpi_unbind(struct sonode *, int);
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate /* TPI sockfs sonode operations */
203e9f74ea5SToomas Soome int sotpi_init(struct sonode *, struct sonode *, struct cred *,
2047c478bd9Sstevel@tonic-gate int);
2050f1702c5SYu Xiangning static int sotpi_accept(struct sonode *, int, struct cred *,
2060f1702c5SYu Xiangning struct sonode **);
2070f1702c5SYu Xiangning static int sotpi_bind(struct sonode *, struct sockaddr *, socklen_t,
2080f1702c5SYu Xiangning int, struct cred *);
2090f1702c5SYu Xiangning static int sotpi_listen(struct sonode *, int, struct cred *);
2103e95bd4aSAnders Persson static int sotpi_connect(struct sonode *, struct sockaddr *,
2110f1702c5SYu Xiangning socklen_t, int, int, struct cred *);
2120f1702c5SYu Xiangning extern int sotpi_recvmsg(struct sonode *, struct nmsghdr *,
2130f1702c5SYu Xiangning struct uio *, struct cred *);
2147c478bd9Sstevel@tonic-gate static int sotpi_sendmsg(struct sonode *, struct nmsghdr *,
2150f1702c5SYu Xiangning struct uio *, struct cred *);
2160f1702c5SYu Xiangning static int sotpi_sendmblk(struct sonode *, struct nmsghdr *, int,
2170f1702c5SYu Xiangning struct cred *, mblk_t **);
218ff550d0eSmasputra static int sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t,
219ff550d0eSmasputra struct uio *, void *, t_uscalar_t, int);
220ff550d0eSmasputra static int sodgram_direct(struct sonode *, struct sockaddr *,
221ff550d0eSmasputra socklen_t, struct uio *, int);
2220f1702c5SYu Xiangning extern int sotpi_getpeername(struct sonode *, struct sockaddr *,
2230f1702c5SYu Xiangning socklen_t *, boolean_t, struct cred *);
2240f1702c5SYu Xiangning static int sotpi_getsockname(struct sonode *, struct sockaddr *,
2250f1702c5SYu Xiangning socklen_t *, struct cred *);
2260f1702c5SYu Xiangning static int sotpi_shutdown(struct sonode *, int, struct cred *);
2270f1702c5SYu Xiangning extern int sotpi_getsockopt(struct sonode *, int, int, void *,
2280f1702c5SYu Xiangning socklen_t *, int, struct cred *);
2290f1702c5SYu Xiangning extern int sotpi_setsockopt(struct sonode *, int, int, const void *,
2300f1702c5SYu Xiangning socklen_t, struct cred *);
231e9f74ea5SToomas Soome static int sotpi_ioctl(struct sonode *, int, intptr_t, int, struct cred *,
2320f1702c5SYu Xiangning int32_t *);
233e9f74ea5SToomas Soome static int socktpi_plumbioctl(struct vnode *, int, intptr_t, int,
2340f1702c5SYu Xiangning struct cred *, int32_t *);
235e9f74ea5SToomas Soome static int sotpi_poll(struct sonode *, short, int, short *,
2360f1702c5SYu Xiangning struct pollhead **);
237e9f74ea5SToomas Soome static int sotpi_close(struct sonode *, int, struct cred *);
2380f1702c5SYu Xiangning
2390f1702c5SYu Xiangning static int i_sotpi_info_constructor(sotpi_info_t *);
240e9f74ea5SToomas Soome static void i_sotpi_info_destructor(sotpi_info_t *);
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate sonodeops_t sotpi_sonodeops = {
2430f1702c5SYu Xiangning sotpi_init, /* sop_init */
2447c478bd9Sstevel@tonic-gate sotpi_accept, /* sop_accept */
2457c478bd9Sstevel@tonic-gate sotpi_bind, /* sop_bind */
2467c478bd9Sstevel@tonic-gate sotpi_listen, /* sop_listen */
2477c478bd9Sstevel@tonic-gate sotpi_connect, /* sop_connect */
2487c478bd9Sstevel@tonic-gate sotpi_recvmsg, /* sop_recvmsg */
2497c478bd9Sstevel@tonic-gate sotpi_sendmsg, /* sop_sendmsg */
2500f1702c5SYu Xiangning sotpi_sendmblk, /* sop_sendmblk */
2517c478bd9Sstevel@tonic-gate sotpi_getpeername, /* sop_getpeername */
2527c478bd9Sstevel@tonic-gate sotpi_getsockname, /* sop_getsockname */
2537c478bd9Sstevel@tonic-gate sotpi_shutdown, /* sop_shutdown */
2547c478bd9Sstevel@tonic-gate sotpi_getsockopt, /* sop_getsockopt */
2550f1702c5SYu Xiangning sotpi_setsockopt, /* sop_setsockopt */
2560f1702c5SYu Xiangning sotpi_ioctl, /* sop_ioctl */
2570f1702c5SYu Xiangning sotpi_poll, /* sop_poll */
2580f1702c5SYu Xiangning sotpi_close, /* sop_close */
2597c478bd9Sstevel@tonic-gate };
2607c478bd9Sstevel@tonic-gate
2610f1702c5SYu Xiangning /*
2620f1702c5SYu Xiangning * Return a TPI socket vnode.
2630f1702c5SYu Xiangning *
2640f1702c5SYu Xiangning * Note that sockets assume that the driver will clone (either itself
2650f1702c5SYu Xiangning * or by using the clone driver) i.e. a socket() call will always
2660f1702c5SYu Xiangning * result in a new vnode being created.
2670f1702c5SYu Xiangning */
2680f1702c5SYu Xiangning
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate * Common create code for socket and accept. If tso is set the values
2717c478bd9Sstevel@tonic-gate * from that node is used instead of issuing a T_INFO_REQ.
2727c478bd9Sstevel@tonic-gate */
2730f1702c5SYu Xiangning
2740f1702c5SYu Xiangning /* ARGSUSED */
2750f1702c5SYu Xiangning static struct sonode *
sotpi_create(struct sockparams * sp,int family,int type,int protocol,int version,int sflags,int * errorp,cred_t * cr)2760f1702c5SYu Xiangning sotpi_create(struct sockparams *sp, int family, int type, int protocol,
2770f1702c5SYu Xiangning int version, int sflags, int *errorp, cred_t *cr)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate struct sonode *so;
280e9f74ea5SToomas Soome kmem_cache_t *cp;
2817c478bd9Sstevel@tonic-gate
2820f1702c5SYu Xiangning ASSERT(sp->sp_sdev_info.sd_vnode != NULL);
2830f1702c5SYu Xiangning
2840f1702c5SYu Xiangning if (family == AF_NCA) {
2850f1702c5SYu Xiangning /*
2860f1702c5SYu Xiangning * The request is for an NCA socket so for NL7C use the
2870f1702c5SYu Xiangning * INET domain instead and mark NL7C_AF_NCA below.
2880f1702c5SYu Xiangning */
2890f1702c5SYu Xiangning family = AF_INET;
2900f1702c5SYu Xiangning /*
2910f1702c5SYu Xiangning * NL7C is not supported in the non-global zone,
2920f1702c5SYu Xiangning * we enforce this restriction here.
2930f1702c5SYu Xiangning */
2940f1702c5SYu Xiangning if (getzoneid() != GLOBAL_ZONEID) {
2950f1702c5SYu Xiangning *errorp = ENOTSUP;
2960f1702c5SYu Xiangning return (NULL);
2970f1702c5SYu Xiangning }
2980f1702c5SYu Xiangning }
2990f1702c5SYu Xiangning
3000f1702c5SYu Xiangning /*
3010f1702c5SYu Xiangning * to be compatible with old tpi socket implementation ignore
3020f1702c5SYu Xiangning * sleep flag (sflags) passed in
3030f1702c5SYu Xiangning */
3040f1702c5SYu Xiangning cp = (family == AF_UNIX) ? socktpi_unix_cache : socktpi_cache;
3050f1702c5SYu Xiangning so = kmem_cache_alloc(cp, KM_SLEEP);
3060f1702c5SYu Xiangning if (so == NULL) {
3070f1702c5SYu Xiangning *errorp = ENOMEM;
3080f1702c5SYu Xiangning return (NULL);
3090f1702c5SYu Xiangning }
3100f1702c5SYu Xiangning
3110f1702c5SYu Xiangning sonode_init(so, sp, family, type, protocol, &sotpi_sonodeops);
3120f1702c5SYu Xiangning sotpi_info_init(so);
3130f1702c5SYu Xiangning
3140f1702c5SYu Xiangning if (version == SOV_DEFAULT)
3150f1702c5SYu Xiangning version = so_default_version;
3160f1702c5SYu Xiangning
3170f1702c5SYu Xiangning so->so_version = (short)version;
3180f1702c5SYu Xiangning *errorp = 0;
3190f1702c5SYu Xiangning
3200f1702c5SYu Xiangning return (so);
3210f1702c5SYu Xiangning }
3220f1702c5SYu Xiangning
3230f1702c5SYu Xiangning static void
sotpi_destroy(struct sonode * so)3240f1702c5SYu Xiangning sotpi_destroy(struct sonode *so)
3250f1702c5SYu Xiangning {
3260f1702c5SYu Xiangning kmem_cache_t *cp;
3270f1702c5SYu Xiangning struct sockparams *origsp;
3280f1702c5SYu Xiangning
3290f1702c5SYu Xiangning /*
3300f1702c5SYu Xiangning * If there is a new dealloc function (ie. smod_destroy_func),
3310f1702c5SYu Xiangning * then it should check the correctness of the ops.
3320f1702c5SYu Xiangning */
3330f1702c5SYu Xiangning
3340f1702c5SYu Xiangning ASSERT(so->so_ops == &sotpi_sonodeops);
3350f1702c5SYu Xiangning
3360f1702c5SYu Xiangning origsp = SOTOTPI(so)->sti_orig_sp;
3370f1702c5SYu Xiangning
3380f1702c5SYu Xiangning sotpi_info_fini(so);
3390f1702c5SYu Xiangning
3400f1702c5SYu Xiangning if (so->so_state & SS_FALLBACK_COMP) {
3410f1702c5SYu Xiangning /*
3420f1702c5SYu Xiangning * A fallback happend, which means that a sotpi_info_t struct
3430f1702c5SYu Xiangning * was allocated (as opposed to being allocated from the TPI
3440f1702c5SYu Xiangning * sonode cache. Therefore we explicitly free the struct
3450f1702c5SYu Xiangning * here.
3460f1702c5SYu Xiangning */
3470f1702c5SYu Xiangning sotpi_info_destroy(so);
3480f1702c5SYu Xiangning ASSERT(origsp != NULL);
3490f1702c5SYu Xiangning
3500f1702c5SYu Xiangning origsp->sp_smod_info->smod_sock_destroy_func(so);
3510f1702c5SYu Xiangning SOCKPARAMS_DEC_REF(origsp);
3520f1702c5SYu Xiangning } else {
3530f1702c5SYu Xiangning sonode_fini(so);
3540f1702c5SYu Xiangning cp = (so->so_family == AF_UNIX) ? socktpi_unix_cache :
3550f1702c5SYu Xiangning socktpi_cache;
3560f1702c5SYu Xiangning kmem_cache_free(cp, so);
3570f1702c5SYu Xiangning }
3580f1702c5SYu Xiangning }
3590f1702c5SYu Xiangning
3600f1702c5SYu Xiangning /* ARGSUSED1 */
3610f1702c5SYu Xiangning int
sotpi_init(struct sonode * so,struct sonode * tso,struct cred * cr,int flags)3620f1702c5SYu Xiangning sotpi_init(struct sonode *so, struct sonode *tso, struct cred *cr, int flags)
3630f1702c5SYu Xiangning {
3640f1702c5SYu Xiangning major_t maj;
3650f1702c5SYu Xiangning dev_t newdev;
3660f1702c5SYu Xiangning struct vnode *vp;
3670f1702c5SYu Xiangning int error = 0;
3680f1702c5SYu Xiangning struct stdata *stp;
3690f1702c5SYu Xiangning
3700f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
3710f1702c5SYu Xiangning
3720f1702c5SYu Xiangning dprint(1, ("sotpi_init()\n"));
3730f1702c5SYu Xiangning
3740f1702c5SYu Xiangning /*
3750f1702c5SYu Xiangning * over write the sleep flag passed in but that is ok
3760f1702c5SYu Xiangning * as tpi socket does not honor sleep flag.
3770f1702c5SYu Xiangning */
3780f1702c5SYu Xiangning flags |= FREAD|FWRITE;
3797c478bd9Sstevel@tonic-gate
3800f1702c5SYu Xiangning /*
3810f1702c5SYu Xiangning * Record in so_flag that it is a clone.
3820f1702c5SYu Xiangning */
3830f1702c5SYu Xiangning if (getmajor(sti->sti_dev) == clone_major)
3840f1702c5SYu Xiangning so->so_flag |= SOCLONE;
385ff550d0eSmasputra
3860f1702c5SYu Xiangning if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM) &&
3870f1702c5SYu Xiangning (so->so_family == AF_INET || so->so_family == AF_INET6) &&
3880f1702c5SYu Xiangning (so->so_protocol == IPPROTO_TCP || so->so_protocol == IPPROTO_UDP ||
3890f1702c5SYu Xiangning so->so_protocol == IPPROTO_IP)) {
390ff550d0eSmasputra /* Tell tcp or udp that it's talking to sockets */
391ff550d0eSmasputra flags |= SO_SOCKSTR;
392ff550d0eSmasputra
393ff550d0eSmasputra /*
394ff550d0eSmasputra * Here we indicate to socktpi_open() our attempt to
395ff550d0eSmasputra * make direct calls between sockfs and transport.
396ff550d0eSmasputra * The final decision is left to socktpi_open().
397ff550d0eSmasputra */
3980f1702c5SYu Xiangning sti->sti_direct = 1;
399ff550d0eSmasputra
400ff550d0eSmasputra ASSERT(so->so_type != SOCK_DGRAM || tso == NULL);
401ff550d0eSmasputra if (so->so_type == SOCK_STREAM && tso != NULL) {
4020f1702c5SYu Xiangning if (SOTOTPI(tso)->sti_direct) {
403ff550d0eSmasputra /*
4040f1702c5SYu Xiangning * Inherit sti_direct from listener and pass
405ff550d0eSmasputra * SO_ACCEPTOR open flag to tcp, indicating
406ff550d0eSmasputra * that this is an accept fast-path instance.
407ff550d0eSmasputra */
408ff550d0eSmasputra flags |= SO_ACCEPTOR;
409ff550d0eSmasputra } else {
410ff550d0eSmasputra /*
4110f1702c5SYu Xiangning * sti_direct is not set on listener, meaning
412ff550d0eSmasputra * that the listener has been converted from
413ff550d0eSmasputra * a socket to a stream. Ensure that the
414ff550d0eSmasputra * acceptor inherits these settings.
415ff550d0eSmasputra */
4160f1702c5SYu Xiangning sti->sti_direct = 0;
417ff550d0eSmasputra flags &= ~SO_SOCKSTR;
418ff550d0eSmasputra }
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * Tell local transport that it is talking to sockets.
4247c478bd9Sstevel@tonic-gate */
4257c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
4267c478bd9Sstevel@tonic-gate flags |= SO_SOCKSTR;
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4290f1702c5SYu Xiangning vp = SOTOV(so);
4300f1702c5SYu Xiangning newdev = vp->v_rdev;
4310f1702c5SYu Xiangning maj = getmajor(newdev);
4320f1702c5SYu Xiangning ASSERT(STREAMSTAB(maj));
433655a2e99Skais
4340f1702c5SYu Xiangning error = stropen(vp, &newdev, flags, cr);
4357c478bd9Sstevel@tonic-gate
4360f1702c5SYu Xiangning stp = vp->v_stream;
4370f1702c5SYu Xiangning if (error == 0) {
4380f1702c5SYu Xiangning if (so->so_flag & SOCLONE)
4390f1702c5SYu Xiangning ASSERT(newdev != vp->v_rdev);
4400f1702c5SYu Xiangning mutex_enter(&so->so_lock);
4410f1702c5SYu Xiangning sti->sti_dev = newdev;
4420f1702c5SYu Xiangning vp->v_rdev = newdev;
4430f1702c5SYu Xiangning mutex_exit(&so->so_lock);
4447c478bd9Sstevel@tonic-gate
4450f1702c5SYu Xiangning if (stp->sd_flag & STRISTTY) {
4460f1702c5SYu Xiangning /*
4470f1702c5SYu Xiangning * this is a post SVR4 tty driver - a socket can not
4480f1702c5SYu Xiangning * be a controlling terminal. Fail the open.
4490f1702c5SYu Xiangning */
4500f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
4510f1702c5SYu Xiangning return (ENOTTY); /* XXX */
4520f1702c5SYu Xiangning }
4537c478bd9Sstevel@tonic-gate
4540f1702c5SYu Xiangning ASSERT(stp->sd_wrq != NULL);
4550f1702c5SYu Xiangning sti->sti_provinfo = tpi_findprov(stp->sd_wrq);
456c28749e9Skais
4570f1702c5SYu Xiangning /*
4580f1702c5SYu Xiangning * If caller is interested in doing direct function call
4590f1702c5SYu Xiangning * interface to/from transport module, probe the module
4600f1702c5SYu Xiangning * directly beneath the streamhead to see if it qualifies.
4610f1702c5SYu Xiangning *
4620f1702c5SYu Xiangning * We turn off the direct interface when qualifications fail.
4630f1702c5SYu Xiangning * In the acceptor case, we simply turn off the sti_direct
4640f1702c5SYu Xiangning * flag on the socket. We do the fallback after the accept
4650f1702c5SYu Xiangning * has completed, before the new socket is returned to the
4660f1702c5SYu Xiangning * application.
4670f1702c5SYu Xiangning */
4680f1702c5SYu Xiangning if (sti->sti_direct) {
4690f1702c5SYu Xiangning queue_t *tq = stp->sd_wrq->q_next;
4700f1702c5SYu Xiangning
4710f1702c5SYu Xiangning /*
4720f1702c5SYu Xiangning * sti_direct is currently supported and tested
4730f1702c5SYu Xiangning * only for tcp/udp; this is the main reason to
4740f1702c5SYu Xiangning * have the following assertions.
4750f1702c5SYu Xiangning */
4760f1702c5SYu Xiangning ASSERT(so->so_family == AF_INET ||
4770f1702c5SYu Xiangning so->so_family == AF_INET6);
4780f1702c5SYu Xiangning ASSERT(so->so_protocol == IPPROTO_UDP ||
4790f1702c5SYu Xiangning so->so_protocol == IPPROTO_TCP ||
4800f1702c5SYu Xiangning so->so_protocol == IPPROTO_IP);
4810f1702c5SYu Xiangning ASSERT(so->so_type == SOCK_DGRAM ||
4820f1702c5SYu Xiangning so->so_type == SOCK_STREAM);
4830f1702c5SYu Xiangning
4840f1702c5SYu Xiangning /*
4850f1702c5SYu Xiangning * Abort direct call interface if the module directly
4860f1702c5SYu Xiangning * underneath the stream head is not defined with the
4870f1702c5SYu Xiangning * _D_DIRECT flag. This could happen in the tcp or
4880f1702c5SYu Xiangning * udp case, when some other module is autopushed
4890f1702c5SYu Xiangning * above it, or for some reasons the expected module
4900f1702c5SYu Xiangning * isn't purely D_MP (which is the main requirement).
4910f1702c5SYu Xiangning */
4920f1702c5SYu Xiangning if (!socktpi_direct || !(tq->q_flag & _QDIRECT) ||
4930f1702c5SYu Xiangning !(_OTHERQ(tq)->q_flag & _QDIRECT)) {
4940f1702c5SYu Xiangning int rval;
4950f1702c5SYu Xiangning
4960f1702c5SYu Xiangning /* Continue on without direct calls */
4970f1702c5SYu Xiangning sti->sti_direct = 0;
4980f1702c5SYu Xiangning
4990f1702c5SYu Xiangning /*
5000f1702c5SYu Xiangning * Cannot issue ioctl on fallback socket since
5010f1702c5SYu Xiangning * there is no conn associated with the queue.
5020f1702c5SYu Xiangning * The fallback downcall will notify the proto
5030f1702c5SYu Xiangning * of the change.
5040f1702c5SYu Xiangning */
5050f1702c5SYu Xiangning if (!(flags & SO_ACCEPTOR) &&
5060f1702c5SYu Xiangning !(flags & SO_FALLBACK)) {
5070f1702c5SYu Xiangning if ((error = strioctl(vp,
5080f1702c5SYu Xiangning _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
5090f1702c5SYu Xiangning cr, &rval)) != 0) {
5100f1702c5SYu Xiangning (void) sotpi_close(so, flags,
5110f1702c5SYu Xiangning cr);
5120f1702c5SYu Xiangning return (error);
5130f1702c5SYu Xiangning }
5140f1702c5SYu Xiangning }
5150f1702c5SYu Xiangning }
5160f1702c5SYu Xiangning }
5170f1702c5SYu Xiangning
5180f1702c5SYu Xiangning if (flags & SO_FALLBACK) {
5190f1702c5SYu Xiangning /*
5200f1702c5SYu Xiangning * The stream created does not have a conn.
5210f1702c5SYu Xiangning * do stream set up after conn has been assigned
5220f1702c5SYu Xiangning */
5230f1702c5SYu Xiangning return (error);
5240f1702c5SYu Xiangning }
525*34793d0fSToomas Soome error = so_strinit(so, tso);
526*34793d0fSToomas Soome if (error != 0) {
5270f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
5280f1702c5SYu Xiangning return (error);
5290f1702c5SYu Xiangning }
5300f1702c5SYu Xiangning
531acb55917SPatrick Mooney /* Enable sendfile() on AF_UNIX streams */
532acb55917SPatrick Mooney if (so->so_family == AF_UNIX && so->so_type == SOCK_STREAM) {
533acb55917SPatrick Mooney mutex_enter(&so->so_lock);
534acb55917SPatrick Mooney so->so_mode |= SM_SENDFILESUPP;
535acb55917SPatrick Mooney mutex_exit(&so->so_lock);
536acb55917SPatrick Mooney }
537acb55917SPatrick Mooney
5380f1702c5SYu Xiangning /* Wildcard */
5390f1702c5SYu Xiangning if (so->so_protocol != so->so_sockparams->sp_protocol) {
5400f1702c5SYu Xiangning int protocol = so->so_protocol;
5410f1702c5SYu Xiangning /*
5420f1702c5SYu Xiangning * Issue SO_PROTOTYPE setsockopt.
5430f1702c5SYu Xiangning */
5440f1702c5SYu Xiangning error = sotpi_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
5450f1702c5SYu Xiangning &protocol, (t_uscalar_t)sizeof (protocol), cr);
5460f1702c5SYu Xiangning if (error != 0) {
5470f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
5480f1702c5SYu Xiangning /*
5490f1702c5SYu Xiangning * Setsockopt often fails with ENOPROTOOPT but
5500f1702c5SYu Xiangning * socket() should fail with
5510f1702c5SYu Xiangning * EPROTONOSUPPORT/EPROTOTYPE.
5520f1702c5SYu Xiangning */
5530f1702c5SYu Xiangning return (EPROTONOSUPPORT);
5540f1702c5SYu Xiangning }
5550f1702c5SYu Xiangning }
5560f1702c5SYu Xiangning
5570f1702c5SYu Xiangning } else {
5580f1702c5SYu Xiangning /*
5590f1702c5SYu Xiangning * While the same socket can not be reopened (unlike specfs)
5600f1702c5SYu Xiangning * the stream head sets STREOPENFAIL when the autopush fails.
5610f1702c5SYu Xiangning */
5620f1702c5SYu Xiangning if ((stp != NULL) &&
5630f1702c5SYu Xiangning (stp->sd_flag & STREOPENFAIL)) {
5640f1702c5SYu Xiangning /*
5650f1702c5SYu Xiangning * Open failed part way through.
5660f1702c5SYu Xiangning */
5670f1702c5SYu Xiangning mutex_enter(&stp->sd_lock);
5680f1702c5SYu Xiangning stp->sd_flag &= ~STREOPENFAIL;
5690f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
5700f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
5710f1702c5SYu Xiangning return (error);
5720f1702c5SYu Xiangning /*NOTREACHED*/
5730f1702c5SYu Xiangning }
5740f1702c5SYu Xiangning ASSERT(stp == NULL);
5750f1702c5SYu Xiangning }
5760f1702c5SYu Xiangning TRACE_4(TR_FAC_SOCKFS, TR_SOCKFS_OPEN,
5770f1702c5SYu Xiangning "sockfs open:maj %d vp %p so %p error %d",
5780f1702c5SYu Xiangning maj, vp, so, error);
5790f1702c5SYu Xiangning return (error);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate * Bind the socket to an unspecified address in sockfs only.
5847c478bd9Sstevel@tonic-gate * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't
5857c478bd9Sstevel@tonic-gate * required in all cases.
5867c478bd9Sstevel@tonic-gate */
5877c478bd9Sstevel@tonic-gate static void
so_automatic_bind(struct sonode * so)5887c478bd9Sstevel@tonic-gate so_automatic_bind(struct sonode *so)
5897c478bd9Sstevel@tonic-gate {
5900f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
5917c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
5947c478bd9Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND));
5950f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp);
5967c478bd9Sstevel@tonic-gate
5970f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
5980f1702c5SYu Xiangning bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
5990f1702c5SYu Xiangning sti->sti_laddr_sa->sa_family = so->so_family;
6007c478bd9Sstevel@tonic-gate so->so_state |= SS_ISBOUND;
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate * bind the socket.
6067c478bd9Sstevel@tonic-gate *
6077c478bd9Sstevel@tonic-gate * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2
6087c478bd9Sstevel@tonic-gate * are passed in we allow rebinding. Note that for backwards compatibility
6097c478bd9Sstevel@tonic-gate * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind.
6107c478bd9Sstevel@tonic-gate * Thus the rebinding code is currently not executed.
6117c478bd9Sstevel@tonic-gate *
6127c478bd9Sstevel@tonic-gate * The constraints for rebinding are:
6137c478bd9Sstevel@tonic-gate * - it is a SOCK_DGRAM, or
6147c478bd9Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6157c478bd9Sstevel@tonic-gate * and no listen() has been done.
6167c478bd9Sstevel@tonic-gate * This rebinding code was added based on some language in the XNET book
6177c478bd9Sstevel@tonic-gate * about not returning EINVAL it the protocol allows rebinding. However,
6187c478bd9Sstevel@tonic-gate * this language is not present in the Posix socket draft. Thus maybe the
6197c478bd9Sstevel@tonic-gate * rebinding logic should be deleted from the source.
6207c478bd9Sstevel@tonic-gate *
6217c478bd9Sstevel@tonic-gate * A null "name" can be used to unbind the socket if:
6227c478bd9Sstevel@tonic-gate * - it is a SOCK_DGRAM, or
6237c478bd9Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6247c478bd9Sstevel@tonic-gate * and no listen() has been done.
6257c478bd9Sstevel@tonic-gate */
6260f1702c5SYu Xiangning /* ARGSUSED */
6277c478bd9Sstevel@tonic-gate static int
sotpi_bindlisten(struct sonode * so,struct sockaddr * name,socklen_t namelen,int backlog,int flags,struct cred * cr)6287c478bd9Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name,
6290f1702c5SYu Xiangning socklen_t namelen, int backlog, int flags, struct cred *cr)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate struct T_bind_req bind_req;
6327c478bd9Sstevel@tonic-gate struct T_bind_ack *bind_ack;
6337c478bd9Sstevel@tonic-gate int error = 0;
6347c478bd9Sstevel@tonic-gate mblk_t *mp;
6357c478bd9Sstevel@tonic-gate void *addr;
6367c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
6377c478bd9Sstevel@tonic-gate int unbind_on_err = 1;
6387c478bd9Sstevel@tonic-gate boolean_t clear_acceptconn_on_err = B_FALSE;
6397c478bd9Sstevel@tonic-gate boolean_t restore_backlog_on_err = B_FALSE;
6407c478bd9Sstevel@tonic-gate int save_so_backlog;
6417c478bd9Sstevel@tonic-gate t_scalar_t PRIM_type = O_T_BIND_REQ;
6427c478bd9Sstevel@tonic-gate boolean_t tcp_udp_xport;
6430f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n",
646903a11ebSrh (void *)so, (void *)name, namelen, backlog, flags,
647fc80c0dfSnordmark pr_state(so->so_state, so->so_mode)));
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM;
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) {
6527c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
6537c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
6547c478bd9Sstevel@tonic-gate } else {
6557c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
6567c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate * Make sure that there is a preallocated unbind_req message
6617c478bd9Sstevel@tonic-gate * before binding. This message allocated when the socket is
6627c478bd9Sstevel@tonic-gate * created but it might be have been consumed.
6637c478bd9Sstevel@tonic-gate */
6640f1702c5SYu Xiangning if (sti->sti_unbind_mp == NULL) {
6657c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sobind: allocating unbind_req\n"));
6667c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
6670f1702c5SYu Xiangning sti->sti_unbind_mp =
668de8c4a14SErik Nordmark soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP,
669de8c4a14SErik Nordmark cr);
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate if (flags & _SOBIND_REBIND) {
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate * Called from solisten after doing an sotpi_unbind() or
6757c478bd9Sstevel@tonic-gate * potentially without the unbind (latter for AF_INET{,6}).
6767c478bd9Sstevel@tonic-gate */
6777c478bd9Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0);
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
6800f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp);
6810f1702c5SYu Xiangning addr = &sti->sti_ux_laddr;
6820f1702c5SYu Xiangning addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
683fc80c0dfSnordmark dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, "
684fc80c0dfSnordmark "addr 0x%p, vp %p\n",
6857c478bd9Sstevel@tonic-gate addrlen,
686903a11ebSrh (void *)((struct so_ux_addr *)addr)->soua_vp,
6870f1702c5SYu Xiangning (void *)sti->sti_ux_bound_vp));
6887c478bd9Sstevel@tonic-gate } else {
6890f1702c5SYu Xiangning addr = sti->sti_laddr_sa;
6900f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_laddr_len;
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate } else if (flags & _SOBIND_UNSPEC) {
6937c478bd9Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0);
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate /*
6967c478bd9Sstevel@tonic-gate * The caller checked SS_ISBOUND but not necessarily
6977c478bd9Sstevel@tonic-gate * under so_lock
6987c478bd9Sstevel@tonic-gate */
6997c478bd9Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) {
7007c478bd9Sstevel@tonic-gate /* No error */
7017c478bd9Sstevel@tonic-gate goto done;
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate /* Set an initial local address */
7057c478bd9Sstevel@tonic-gate switch (so->so_family) {
7067c478bd9Sstevel@tonic-gate case AF_UNIX:
7077c478bd9Sstevel@tonic-gate /*
7087c478bd9Sstevel@tonic-gate * Use an address with same size as struct sockaddr
7097c478bd9Sstevel@tonic-gate * just like BSD.
7107c478bd9Sstevel@tonic-gate */
7110f1702c5SYu Xiangning sti->sti_laddr_len =
712fc80c0dfSnordmark (socklen_t)sizeof (struct sockaddr);
7130f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7140f1702c5SYu Xiangning bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7150f1702c5SYu Xiangning sti->sti_laddr_sa->sa_family = so->so_family;
7167c478bd9Sstevel@tonic-gate
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate * Pass down an address with the implicit bind
7197c478bd9Sstevel@tonic-gate * magic number and the rest all zeros.
7207c478bd9Sstevel@tonic-gate * The transport will return a unique address.
7217c478bd9Sstevel@tonic-gate */
7220f1702c5SYu Xiangning sti->sti_ux_laddr.soua_vp = NULL;
7230f1702c5SYu Xiangning sti->sti_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT;
7240f1702c5SYu Xiangning addr = &sti->sti_ux_laddr;
7250f1702c5SYu Xiangning addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
7267c478bd9Sstevel@tonic-gate break;
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate case AF_INET:
7297c478bd9Sstevel@tonic-gate case AF_INET6:
7307c478bd9Sstevel@tonic-gate /*
7317c478bd9Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address.
7327c478bd9Sstevel@tonic-gate * Set the address in sockfs to have the sa_family.
7337c478bd9Sstevel@tonic-gate */
7340f1702c5SYu Xiangning sti->sti_laddr_len = (so->so_family == AF_INET) ?
7357c478bd9Sstevel@tonic-gate (socklen_t)sizeof (sin_t) :
7367c478bd9Sstevel@tonic-gate (socklen_t)sizeof (sin6_t);
7370f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7380f1702c5SYu Xiangning bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7390f1702c5SYu Xiangning sti->sti_laddr_sa->sa_family = so->so_family;
7407c478bd9Sstevel@tonic-gate addr = NULL;
7417c478bd9Sstevel@tonic-gate addrlen = 0;
7427c478bd9Sstevel@tonic-gate break;
7437c478bd9Sstevel@tonic-gate
7447c478bd9Sstevel@tonic-gate default:
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address.
7477c478bd9Sstevel@tonic-gate * Set the address in sockfs to be zero length.
7487c478bd9Sstevel@tonic-gate *
7497c478bd9Sstevel@tonic-gate * Can not assume there is a sa_family for all
7507c478bd9Sstevel@tonic-gate * protocol families. For example, AF_X25 does not
7517c478bd9Sstevel@tonic-gate * have a family field.
7527c478bd9Sstevel@tonic-gate */
7530f1702c5SYu Xiangning bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7540f1702c5SYu Xiangning sti->sti_laddr_len = 0; /* XXX correct? */
7557c478bd9Sstevel@tonic-gate addr = NULL;
7567c478bd9Sstevel@tonic-gate addrlen = 0;
7577c478bd9Sstevel@tonic-gate break;
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate } else {
7617c478bd9Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) {
7627c478bd9Sstevel@tonic-gate /*
7637c478bd9Sstevel@tonic-gate * If it is ok to rebind the socket, first unbind
7647c478bd9Sstevel@tonic-gate * with the transport. A rebind to the NULL address
7657c478bd9Sstevel@tonic-gate * is interpreted as an unbind.
7667c478bd9Sstevel@tonic-gate * Note that a bind to NULL in BSD does unbind the
7677c478bd9Sstevel@tonic-gate * socket but it fails with EINVAL.
7687c478bd9Sstevel@tonic-gate * Note that regular sockets set SOV_SOCKBSD i.e.
7697c478bd9Sstevel@tonic-gate * _SOBIND_SOCKBSD gets set here hence no type of
7707c478bd9Sstevel@tonic-gate * socket does currently allow rebinding.
7717c478bd9Sstevel@tonic-gate *
7727c478bd9Sstevel@tonic-gate * If the name is NULL just do an unbind.
7737c478bd9Sstevel@tonic-gate */
7747c478bd9Sstevel@tonic-gate if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) &&
7757c478bd9Sstevel@tonic-gate name != NULL) {
7767c478bd9Sstevel@tonic-gate error = EINVAL;
7777c478bd9Sstevel@tonic-gate unbind_on_err = 0;
7787c478bd9Sstevel@tonic-gate eprintsoline(so, error);
7797c478bd9Sstevel@tonic-gate goto done;
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate if ((so->so_mode & SM_CONNREQUIRED) &&
7827c478bd9Sstevel@tonic-gate (so->so_state & SS_CANTREBIND)) {
7837c478bd9Sstevel@tonic-gate error = EINVAL;
7847c478bd9Sstevel@tonic-gate unbind_on_err = 0;
7857c478bd9Sstevel@tonic-gate eprintsoline(so, error);
7867c478bd9Sstevel@tonic-gate goto done;
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate error = sotpi_unbind(so, 0);
7897c478bd9Sstevel@tonic-gate if (error) {
7907c478bd9Sstevel@tonic-gate eprintsoline(so, error);
7917c478bd9Sstevel@tonic-gate goto done;
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND));
7947c478bd9Sstevel@tonic-gate if (name == NULL) {
7957c478bd9Sstevel@tonic-gate so->so_state &=
796fc80c0dfSnordmark ~(SS_ISCONNECTED|SS_ISCONNECTING);
7977c478bd9Sstevel@tonic-gate goto done;
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate }
8000f1702c5SYu Xiangning
8017c478bd9Sstevel@tonic-gate /* X/Open requires this check */
8027c478bd9Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
8037c478bd9Sstevel@tonic-gate if (xnet_check_print) {
8047c478bd9Sstevel@tonic-gate printf("sockfs: X/Open bind state check "
8057c478bd9Sstevel@tonic-gate "caused EINVAL\n");
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate error = EINVAL;
8087c478bd9Sstevel@tonic-gate goto done;
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate switch (so->so_family) {
8127c478bd9Sstevel@tonic-gate case AF_UNIX:
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate * All AF_UNIX addresses are nul terminated
8157c478bd9Sstevel@tonic-gate * when copied (copyin_name) in so the minimum
8167c478bd9Sstevel@tonic-gate * length is 3 bytes.
8177c478bd9Sstevel@tonic-gate */
8187c478bd9Sstevel@tonic-gate if (name == NULL ||
8197c478bd9Sstevel@tonic-gate (ssize_t)namelen <= sizeof (short) + 1) {
8207c478bd9Sstevel@tonic-gate error = EISDIR;
8217c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8227c478bd9Sstevel@tonic-gate goto done;
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate /*
8257c478bd9Sstevel@tonic-gate * Verify so_family matches the bound family.
8267c478bd9Sstevel@tonic-gate * BSD does not check this for AF_UNIX resulting
8277c478bd9Sstevel@tonic-gate * in funny mknods.
8287c478bd9Sstevel@tonic-gate */
8297c478bd9Sstevel@tonic-gate if (name->sa_family != so->so_family) {
8307c478bd9Sstevel@tonic-gate error = EAFNOSUPPORT;
8317c478bd9Sstevel@tonic-gate goto done;
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate break;
8347c478bd9Sstevel@tonic-gate case AF_INET:
8357c478bd9Sstevel@tonic-gate if (name == NULL) {
8367c478bd9Sstevel@tonic-gate error = EINVAL;
8377c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8387c478bd9Sstevel@tonic-gate goto done;
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin_t)) {
8417c478bd9Sstevel@tonic-gate error = name->sa_family != so->so_family ?
8427c478bd9Sstevel@tonic-gate EAFNOSUPPORT : EINVAL;
8437c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8447c478bd9Sstevel@tonic-gate goto done;
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate if ((flags & _SOBIND_XPG4_2) &&
8477c478bd9Sstevel@tonic-gate (name->sa_family != so->so_family)) {
8487c478bd9Sstevel@tonic-gate /*
8497c478bd9Sstevel@tonic-gate * This check has to be made for X/Open
8507c478bd9Sstevel@tonic-gate * sockets however application failures have
8517c478bd9Sstevel@tonic-gate * been observed when it is applied to
8527c478bd9Sstevel@tonic-gate * all sockets.
8537c478bd9Sstevel@tonic-gate */
8547c478bd9Sstevel@tonic-gate error = EAFNOSUPPORT;
8557c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8567c478bd9Sstevel@tonic-gate goto done;
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate * Force a zero sa_family to match so_family.
8607c478bd9Sstevel@tonic-gate *
861bbf21555SRichard Lowe * Some programs like inetd(8) don't set the
8627c478bd9Sstevel@tonic-gate * family field. Other programs leave
8637c478bd9Sstevel@tonic-gate * sin_family set to garbage - SunOS 4.X does
8647c478bd9Sstevel@tonic-gate * not check the family field on a bind.
8657c478bd9Sstevel@tonic-gate * We use the family field that
8667c478bd9Sstevel@tonic-gate * was passed in to the socket() call.
8677c478bd9Sstevel@tonic-gate */
8687c478bd9Sstevel@tonic-gate name->sa_family = so->so_family;
8697c478bd9Sstevel@tonic-gate break;
8707c478bd9Sstevel@tonic-gate
8717c478bd9Sstevel@tonic-gate case AF_INET6: {
8727c478bd9Sstevel@tonic-gate #ifdef DEBUG
8737c478bd9Sstevel@tonic-gate sin6_t *sin6 = (sin6_t *)name;
8747c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8757c478bd9Sstevel@tonic-gate
8767c478bd9Sstevel@tonic-gate if (name == NULL) {
8777c478bd9Sstevel@tonic-gate error = EINVAL;
8787c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8797c478bd9Sstevel@tonic-gate goto done;
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin6_t)) {
8827c478bd9Sstevel@tonic-gate error = name->sa_family != so->so_family ?
8837c478bd9Sstevel@tonic-gate EAFNOSUPPORT : EINVAL;
8847c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8857c478bd9Sstevel@tonic-gate goto done;
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate if (name->sa_family != so->so_family) {
8887c478bd9Sstevel@tonic-gate /*
8897c478bd9Sstevel@tonic-gate * With IPv6 we require the family to match
8907c478bd9Sstevel@tonic-gate * unlike in IPv4.
8917c478bd9Sstevel@tonic-gate */
8927c478bd9Sstevel@tonic-gate error = EAFNOSUPPORT;
8937c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8947c478bd9Sstevel@tonic-gate goto done;
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate #ifdef DEBUG
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate * Verify that apps don't forget to clear
8997c478bd9Sstevel@tonic-gate * sin6_scope_id etc
9007c478bd9Sstevel@tonic-gate */
9017c478bd9Sstevel@tonic-gate if (sin6->sin6_scope_id != 0 &&
9027c478bd9Sstevel@tonic-gate !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
9032caf0dcdSrshoaib zcmn_err(getzoneid(), CE_WARN,
9047c478bd9Sstevel@tonic-gate "bind with uninitialized sin6_scope_id "
9057c478bd9Sstevel@tonic-gate "(%d) on socket. Pid = %d\n",
9067c478bd9Sstevel@tonic-gate (int)sin6->sin6_scope_id,
9077c478bd9Sstevel@tonic-gate (int)curproc->p_pid);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate if (sin6->__sin6_src_id != 0) {
9102caf0dcdSrshoaib zcmn_err(getzoneid(), CE_WARN,
9117c478bd9Sstevel@tonic-gate "bind with uninitialized __sin6_src_id "
9127c478bd9Sstevel@tonic-gate "(%d) on socket. Pid = %d\n",
9137c478bd9Sstevel@tonic-gate (int)sin6->__sin6_src_id,
9147c478bd9Sstevel@tonic-gate (int)curproc->p_pid);
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate #endif /* DEBUG */
9177c478bd9Sstevel@tonic-gate break;
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate default:
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate * Don't do any length or sa_family check to allow
9227c478bd9Sstevel@tonic-gate * non-sockaddr style addresses.
9237c478bd9Sstevel@tonic-gate */
9247c478bd9Sstevel@tonic-gate if (name == NULL) {
9257c478bd9Sstevel@tonic-gate error = EINVAL;
9267c478bd9Sstevel@tonic-gate eprintsoline(so, error);
9277c478bd9Sstevel@tonic-gate goto done;
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate break;
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate
9320f1702c5SYu Xiangning if (namelen > (t_uscalar_t)sti->sti_laddr_maxlen) {
9337c478bd9Sstevel@tonic-gate error = ENAMETOOLONG;
9347c478bd9Sstevel@tonic-gate eprintsoline(so, error);
9357c478bd9Sstevel@tonic-gate goto done;
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate * Save local address.
9397c478bd9Sstevel@tonic-gate */
9400f1702c5SYu Xiangning sti->sti_laddr_len = (socklen_t)namelen;
9410f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
9420f1702c5SYu Xiangning bcopy(name, sti->sti_laddr_sa, namelen);
9437c478bd9Sstevel@tonic-gate
9440f1702c5SYu Xiangning addr = sti->sti_laddr_sa;
9450f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_laddr_len;
9467c478bd9Sstevel@tonic-gate switch (so->so_family) {
9477c478bd9Sstevel@tonic-gate case AF_INET6:
9487c478bd9Sstevel@tonic-gate case AF_INET:
9497c478bd9Sstevel@tonic-gate break;
9507c478bd9Sstevel@tonic-gate case AF_UNIX: {
9517c478bd9Sstevel@tonic-gate struct sockaddr_un *soun =
9520f1702c5SYu Xiangning (struct sockaddr_un *)sti->sti_laddr_sa;
95392f45f6dSRic Aleshire struct vnode *vp, *rvp;
9547c478bd9Sstevel@tonic-gate struct vattr vattr;
9557c478bd9Sstevel@tonic-gate
9560f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
9577c478bd9Sstevel@tonic-gate /*
9587c478bd9Sstevel@tonic-gate * Create vnode for the specified path name.
9590f1702c5SYu Xiangning * Keep vnode held with a reference in sti_ux_bound_vp.
9607c478bd9Sstevel@tonic-gate * Use the vnode pointer as the address used in the
9617c478bd9Sstevel@tonic-gate * bind with the transport.
9627c478bd9Sstevel@tonic-gate *
9637c478bd9Sstevel@tonic-gate * Use the same mode as in BSD. In particular this does
9647c478bd9Sstevel@tonic-gate * not observe the umask.
9657c478bd9Sstevel@tonic-gate */
9667c478bd9Sstevel@tonic-gate /* MAXPATHLEN + soun_family + nul termination */
9670f1702c5SYu Xiangning if (sti->sti_laddr_len >
9687c478bd9Sstevel@tonic-gate (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
9697c478bd9Sstevel@tonic-gate error = ENAMETOOLONG;
9707c478bd9Sstevel@tonic-gate eprintsoline(so, error);
9717c478bd9Sstevel@tonic-gate goto done;
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate vattr.va_type = VSOCK;
974ae115bc7Smrj vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask;
9757c478bd9Sstevel@tonic-gate vattr.va_mask = AT_TYPE|AT_MODE;
9767c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock */
9777c478bd9Sstevel@tonic-gate error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr,
978fc80c0dfSnordmark EXCL, 0, &vp, CRMKNOD, 0, 0);
9797c478bd9Sstevel@tonic-gate if (error) {
9807c478bd9Sstevel@tonic-gate if (error == EEXIST)
9817c478bd9Sstevel@tonic-gate error = EADDRINUSE;
9827c478bd9Sstevel@tonic-gate eprintsoline(so, error);
9837c478bd9Sstevel@tonic-gate goto done;
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate * Establish pointer from the underlying filesystem
9877c478bd9Sstevel@tonic-gate * vnode to the socket node.
9880f1702c5SYu Xiangning * sti_ux_bound_vp and v_stream->sd_vnode form the
9897c478bd9Sstevel@tonic-gate * cross-linkage between the underlying filesystem
9907c478bd9Sstevel@tonic-gate * node and the socket node.
9917c478bd9Sstevel@tonic-gate */
99292f45f6dSRic Aleshire
99392f45f6dSRic Aleshire if ((VOP_REALVP(vp, &rvp, NULL) == 0) && (vp != rvp)) {
99492f45f6dSRic Aleshire VN_HOLD(rvp);
99592f45f6dSRic Aleshire VN_RELE(vp);
99692f45f6dSRic Aleshire vp = rvp;
99792f45f6dSRic Aleshire }
99892f45f6dSRic Aleshire
9997c478bd9Sstevel@tonic-gate ASSERT(SOTOV(so)->v_stream);
10007c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock);
10017c478bd9Sstevel@tonic-gate vp->v_stream = SOTOV(so)->v_stream;
10020f1702c5SYu Xiangning sti->sti_ux_bound_vp = vp;
10037c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock);
10047c478bd9Sstevel@tonic-gate
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate * Use the vnode pointer value as a unique address
10077c478bd9Sstevel@tonic-gate * (together with the magic number to avoid conflicts
10087c478bd9Sstevel@tonic-gate * with implicit binds) in the transport provider.
10097c478bd9Sstevel@tonic-gate */
10100f1702c5SYu Xiangning sti->sti_ux_laddr.soua_vp =
10110f1702c5SYu Xiangning (void *)sti->sti_ux_bound_vp;
10120f1702c5SYu Xiangning sti->sti_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT;
10130f1702c5SYu Xiangning addr = &sti->sti_ux_laddr;
10140f1702c5SYu Xiangning addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
10157c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n",
10167c478bd9Sstevel@tonic-gate addrlen,
10170f1702c5SYu Xiangning (void *)((struct so_ux_addr *)addr)->soua_vp));
10187c478bd9Sstevel@tonic-gate break;
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate } /* end switch (so->so_family) */
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate
10237c478bd9Sstevel@tonic-gate /*
10247c478bd9Sstevel@tonic-gate * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since
10257c478bd9Sstevel@tonic-gate * the transport can start passing up T_CONN_IND messages
10267c478bd9Sstevel@tonic-gate * as soon as it receives the bind req and strsock_proto()
10277c478bd9Sstevel@tonic-gate * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs.
10287c478bd9Sstevel@tonic-gate */
10297c478bd9Sstevel@tonic-gate if (flags & _SOBIND_LISTEN) {
10307c478bd9Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0)
10317c478bd9Sstevel@tonic-gate clear_acceptconn_on_err = B_TRUE;
10327c478bd9Sstevel@tonic-gate save_so_backlog = so->so_backlog;
10337c478bd9Sstevel@tonic-gate restore_backlog_on_err = B_TRUE;
10347c478bd9Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN;
10357c478bd9Sstevel@tonic-gate so->so_backlog = backlog;
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate
10387c478bd9Sstevel@tonic-gate /*
10397c478bd9Sstevel@tonic-gate * We send a T_BIND_REQ for TCP/UDP since we know it supports it,
10407c478bd9Sstevel@tonic-gate * for other transports we will send in a O_T_BIND_REQ.
10417c478bd9Sstevel@tonic-gate */
10427c478bd9Sstevel@tonic-gate if (tcp_udp_xport &&
10437c478bd9Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6))
10447c478bd9Sstevel@tonic-gate PRIM_type = T_BIND_REQ;
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate bind_req.PRIM_type = PRIM_type;
10477c478bd9Sstevel@tonic-gate bind_req.ADDR_length = addrlen;
10487c478bd9Sstevel@tonic-gate bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
10497c478bd9Sstevel@tonic-gate bind_req.CONIND_number = backlog;
10507c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
10517c478bd9Sstevel@tonic-gate mp = soallocproto2(&bind_req, sizeof (bind_req),
1052de8c4a14SErik Nordmark addr, addrlen, 0, _ALLOC_SLEEP, cr);
10530f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
1054c28749e9Skais
10550f1702c5SYu Xiangning /* Done using sti_laddr_sa - can drop the lock */
10567c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
10577c478bd9Sstevel@tonic-gate
10587c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
1059fc80c0dfSnordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
10607c478bd9Sstevel@tonic-gate if (error) {
10617c478bd9Sstevel@tonic-gate eprintsoline(so, error);
10627c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
10637c478bd9Sstevel@tonic-gate goto done;
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate
10667c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
10677c478bd9Sstevel@tonic-gate error = sowaitprim(so, PRIM_type, T_BIND_ACK,
10687c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (*bind_ack), &mp, 0);
10697c478bd9Sstevel@tonic-gate if (error) {
10707c478bd9Sstevel@tonic-gate eprintsoline(so, error);
10717c478bd9Sstevel@tonic-gate goto done;
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate ASSERT(mp);
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in
10767c478bd9Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the bind
10777c478bd9Sstevel@tonic-gate * is allowed to complete.
10787c478bd9Sstevel@tonic-gate */
10797c478bd9Sstevel@tonic-gate
10807c478bd9Sstevel@tonic-gate /* Mark as bound. This will be undone if we detect errors below. */
10817c478bd9Sstevel@tonic-gate if (flags & _SOBIND_NOXLATE) {
10827c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX);
10830f1702c5SYu Xiangning sti->sti_faddr_noxlate = 1;
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND));
10867c478bd9Sstevel@tonic-gate so->so_state |= SS_ISBOUND;
10870f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp);
10887c478bd9Sstevel@tonic-gate
10897c478bd9Sstevel@tonic-gate /* note that we've already set SS_ACCEPTCONN above */
10907c478bd9Sstevel@tonic-gate
10917c478bd9Sstevel@tonic-gate /*
10927c478bd9Sstevel@tonic-gate * Recompute addrlen - an unspecied bind sent down an
10937c478bd9Sstevel@tonic-gate * address of length zero but we expect the appropriate length
10947c478bd9Sstevel@tonic-gate * in return.
10957c478bd9Sstevel@tonic-gate */
10967c478bd9Sstevel@tonic-gate addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ?
10970f1702c5SYu Xiangning sizeof (sti->sti_ux_laddr) : sti->sti_laddr_len);
10987c478bd9Sstevel@tonic-gate
10997c478bd9Sstevel@tonic-gate bind_ack = (struct T_bind_ack *)mp->b_rptr;
11007c478bd9Sstevel@tonic-gate /*
11017c478bd9Sstevel@tonic-gate * The alignment restriction is really too strict but
11027c478bd9Sstevel@tonic-gate * we want enough alignment to inspect the fields of
11037c478bd9Sstevel@tonic-gate * a sockaddr_in.
11047c478bd9Sstevel@tonic-gate */
11057c478bd9Sstevel@tonic-gate addr = sogetoff(mp, bind_ack->ADDR_offset,
1106fc80c0dfSnordmark bind_ack->ADDR_length,
1107fc80c0dfSnordmark __TPI_ALIGN_SIZE);
11087c478bd9Sstevel@tonic-gate if (addr == NULL) {
11097c478bd9Sstevel@tonic-gate freemsg(mp);
11107c478bd9Sstevel@tonic-gate error = EPROTO;
11117c478bd9Sstevel@tonic-gate eprintsoline(so, error);
11127c478bd9Sstevel@tonic-gate goto done;
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate if (!(flags & _SOBIND_UNSPEC)) {
11157c478bd9Sstevel@tonic-gate /*
11167c478bd9Sstevel@tonic-gate * Verify that the transport didn't return something we
11177c478bd9Sstevel@tonic-gate * did not want e.g. an address other than what we asked for.
11187c478bd9Sstevel@tonic-gate *
11197c478bd9Sstevel@tonic-gate * NOTE: These checks would go away if/when we switch to
11207c478bd9Sstevel@tonic-gate * using the new TPI (in which the transport would fail
11217c478bd9Sstevel@tonic-gate * the request instead of assigning a different address).
11227c478bd9Sstevel@tonic-gate *
11237c478bd9Sstevel@tonic-gate * NOTE2: For protocols that we don't know (i.e. any
11247c478bd9Sstevel@tonic-gate * other than AF_INET6, AF_INET and AF_UNIX), we
11257c478bd9Sstevel@tonic-gate * cannot know if the transport should be expected to
11267c478bd9Sstevel@tonic-gate * return the same address as that requested.
11277c478bd9Sstevel@tonic-gate *
11287c478bd9Sstevel@tonic-gate * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send
11297c478bd9Sstevel@tonic-gate * down a T_BIND_REQ. We use O_T_BIND_REQ for others.
11307c478bd9Sstevel@tonic-gate *
11317c478bd9Sstevel@tonic-gate * For example, in the case of netatalk it may be
11327c478bd9Sstevel@tonic-gate * inappropriate for the transport to return the
11337c478bd9Sstevel@tonic-gate * requested address (as it may have allocated a local
11347c478bd9Sstevel@tonic-gate * port number in behaviour similar to that of an
11357c478bd9Sstevel@tonic-gate * AF_INET bind request with a port number of zero).
11367c478bd9Sstevel@tonic-gate *
11377c478bd9Sstevel@tonic-gate * Given the definition of O_T_BIND_REQ, where the
11387c478bd9Sstevel@tonic-gate * transport may bind to an address other than the
11397c478bd9Sstevel@tonic-gate * requested address, it's not possible to determine
11407c478bd9Sstevel@tonic-gate * whether a returned address that differs from the
11417c478bd9Sstevel@tonic-gate * requested address is a reason to fail (because the
11427c478bd9Sstevel@tonic-gate * requested address was not available) or succeed
11437c478bd9Sstevel@tonic-gate * (because the transport allocated an appropriate
11447c478bd9Sstevel@tonic-gate * address and/or port).
11457c478bd9Sstevel@tonic-gate *
11467c478bd9Sstevel@tonic-gate * sockfs currently requires that the transport return
11477c478bd9Sstevel@tonic-gate * the requested address in the T_BIND_ACK, unless
11487c478bd9Sstevel@tonic-gate * there is code here to allow for any discrepancy.
11497c478bd9Sstevel@tonic-gate * Such code exists for AF_INET and AF_INET6.
11507c478bd9Sstevel@tonic-gate *
11517c478bd9Sstevel@tonic-gate * Netatalk chooses to return the requested address
11527c478bd9Sstevel@tonic-gate * rather than the (correct) allocated address. This
11537c478bd9Sstevel@tonic-gate * means that netatalk violates the TPI specification
11547c478bd9Sstevel@tonic-gate * (and would not function correctly if used from a
11557c478bd9Sstevel@tonic-gate * TLI application), but it does mean that it works
11567c478bd9Sstevel@tonic-gate * with sockfs.
11577c478bd9Sstevel@tonic-gate *
11587c478bd9Sstevel@tonic-gate * As noted above, using the newer XTI bind primitive
11597c478bd9Sstevel@tonic-gate * (T_BIND_REQ) in preference to O_T_BIND_REQ would
11607c478bd9Sstevel@tonic-gate * allow sockfs to be more sure about whether or not
11617c478bd9Sstevel@tonic-gate * the bind request had succeeded (as transports are
11627c478bd9Sstevel@tonic-gate * not permitted to bind to a different address than
11637c478bd9Sstevel@tonic-gate * that requested - they must return failure).
11647c478bd9Sstevel@tonic-gate * Unfortunately, support for T_BIND_REQ may not be
11657c478bd9Sstevel@tonic-gate * present in all transport implementations (netatalk,
11667c478bd9Sstevel@tonic-gate * for example, doesn't have it), making the
11677c478bd9Sstevel@tonic-gate * transition difficult.
11687c478bd9Sstevel@tonic-gate */
11697c478bd9Sstevel@tonic-gate if (bind_ack->ADDR_length != addrlen) {
11707c478bd9Sstevel@tonic-gate /* Assumes that the requested address was in use */
11717c478bd9Sstevel@tonic-gate freemsg(mp);
11727c478bd9Sstevel@tonic-gate error = EADDRINUSE;
11737c478bd9Sstevel@tonic-gate eprintsoline(so, error);
11747c478bd9Sstevel@tonic-gate goto done;
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate
11777c478bd9Sstevel@tonic-gate switch (so->so_family) {
11787c478bd9Sstevel@tonic-gate case AF_INET6:
11797c478bd9Sstevel@tonic-gate case AF_INET: {
11807c478bd9Sstevel@tonic-gate sin_t *rname, *aname;
11817c478bd9Sstevel@tonic-gate
11827c478bd9Sstevel@tonic-gate rname = (sin_t *)addr;
11830f1702c5SYu Xiangning aname = (sin_t *)sti->sti_laddr_sa;
11847c478bd9Sstevel@tonic-gate
11857c478bd9Sstevel@tonic-gate /*
11867c478bd9Sstevel@tonic-gate * Take advantage of the alignment
11877c478bd9Sstevel@tonic-gate * of sin_port and sin6_port which fall
11887c478bd9Sstevel@tonic-gate * in the same place in their data structures.
11897c478bd9Sstevel@tonic-gate * Just use sin_port for either address family.
11907c478bd9Sstevel@tonic-gate *
11917c478bd9Sstevel@tonic-gate * This may become a problem if (heaven forbid)
11927c478bd9Sstevel@tonic-gate * there's a separate ipv6port_reserved... :-P
11937c478bd9Sstevel@tonic-gate *
11947c478bd9Sstevel@tonic-gate * Binding to port 0 has the semantics of letting
11957c478bd9Sstevel@tonic-gate * the transport bind to any port.
11967c478bd9Sstevel@tonic-gate *
11977c478bd9Sstevel@tonic-gate * If the transport is TCP or UDP since we had sent
11987c478bd9Sstevel@tonic-gate * a T_BIND_REQ we would not get a port other than
11997c478bd9Sstevel@tonic-gate * what we asked for.
12007c478bd9Sstevel@tonic-gate */
12017c478bd9Sstevel@tonic-gate if (tcp_udp_xport) {
12027c478bd9Sstevel@tonic-gate /*
12037c478bd9Sstevel@tonic-gate * Pick up the new port number if we bound to
12047c478bd9Sstevel@tonic-gate * port 0.
12057c478bd9Sstevel@tonic-gate */
12067c478bd9Sstevel@tonic-gate if (aname->sin_port == 0)
12077c478bd9Sstevel@tonic-gate aname->sin_port = rname->sin_port;
12080f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
12097c478bd9Sstevel@tonic-gate break;
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate if (aname->sin_port != 0 &&
12127c478bd9Sstevel@tonic-gate aname->sin_port != rname->sin_port) {
12137c478bd9Sstevel@tonic-gate freemsg(mp);
12147c478bd9Sstevel@tonic-gate error = EADDRINUSE;
12157c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12167c478bd9Sstevel@tonic-gate goto done;
12177c478bd9Sstevel@tonic-gate }
12187c478bd9Sstevel@tonic-gate /*
12197c478bd9Sstevel@tonic-gate * Pick up the new port number if we bound to port 0.
12207c478bd9Sstevel@tonic-gate */
12217c478bd9Sstevel@tonic-gate aname->sin_port = rname->sin_port;
12227c478bd9Sstevel@tonic-gate
12237c478bd9Sstevel@tonic-gate /*
12247c478bd9Sstevel@tonic-gate * Unfortunately, addresses aren't _quite_ the same.
12257c478bd9Sstevel@tonic-gate */
12267c478bd9Sstevel@tonic-gate if (so->so_family == AF_INET) {
12277c478bd9Sstevel@tonic-gate if (aname->sin_addr.s_addr !=
12287c478bd9Sstevel@tonic-gate rname->sin_addr.s_addr) {
12297c478bd9Sstevel@tonic-gate freemsg(mp);
12307c478bd9Sstevel@tonic-gate error = EADDRNOTAVAIL;
12317c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12327c478bd9Sstevel@tonic-gate goto done;
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate } else {
12357c478bd9Sstevel@tonic-gate sin6_t *rname6 = (sin6_t *)rname;
12367c478bd9Sstevel@tonic-gate sin6_t *aname6 = (sin6_t *)aname;
12377c478bd9Sstevel@tonic-gate
12387c478bd9Sstevel@tonic-gate if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr,
12397c478bd9Sstevel@tonic-gate &rname6->sin6_addr)) {
12407c478bd9Sstevel@tonic-gate freemsg(mp);
12417c478bd9Sstevel@tonic-gate error = EADDRNOTAVAIL;
12427c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12437c478bd9Sstevel@tonic-gate goto done;
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate break;
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate case AF_UNIX:
12490f1702c5SYu Xiangning if (bcmp(addr, &sti->sti_ux_laddr, addrlen) != 0) {
12507c478bd9Sstevel@tonic-gate freemsg(mp);
12517c478bd9Sstevel@tonic-gate error = EADDRINUSE;
12527c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12537c478bd9Sstevel@tonic-gate eprintso(so,
1254fc80c0dfSnordmark ("addrlen %d, addr 0x%x, vp %p\n",
1255fc80c0dfSnordmark addrlen, *((int *)addr),
12560f1702c5SYu Xiangning (void *)sti->sti_ux_bound_vp));
12577c478bd9Sstevel@tonic-gate goto done;
12587c478bd9Sstevel@tonic-gate }
12590f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
12607c478bd9Sstevel@tonic-gate break;
12617c478bd9Sstevel@tonic-gate default:
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate * NOTE: This assumes that addresses can be
12647c478bd9Sstevel@tonic-gate * byte-compared for equivalence.
12657c478bd9Sstevel@tonic-gate */
12660f1702c5SYu Xiangning if (bcmp(addr, sti->sti_laddr_sa, addrlen) != 0) {
12677c478bd9Sstevel@tonic-gate freemsg(mp);
12687c478bd9Sstevel@tonic-gate error = EADDRINUSE;
12697c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12707c478bd9Sstevel@tonic-gate goto done;
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate /*
12730f1702c5SYu Xiangning * Don't mark sti_laddr_valid, as we cannot be
12747c478bd9Sstevel@tonic-gate * sure that the returned address is the real
12757c478bd9Sstevel@tonic-gate * bound address when talking to an unknown
12767c478bd9Sstevel@tonic-gate * transport.
12777c478bd9Sstevel@tonic-gate */
12787c478bd9Sstevel@tonic-gate break;
12797c478bd9Sstevel@tonic-gate }
12807c478bd9Sstevel@tonic-gate } else {
12817c478bd9Sstevel@tonic-gate /*
12827c478bd9Sstevel@tonic-gate * Save for returned address for getsockname.
12837c478bd9Sstevel@tonic-gate * Needed for unspecific bind unless transport supports
12847c478bd9Sstevel@tonic-gate * the TI_GETMYNAME ioctl.
12857c478bd9Sstevel@tonic-gate * Do this for AF_INET{,6} even though they do, as
12867c478bd9Sstevel@tonic-gate * caching info here is much better performance than
12877c478bd9Sstevel@tonic-gate * a TPI/STREAMS trip to the transport for getsockname.
12887c478bd9Sstevel@tonic-gate * Any which can't for some reason _must_ _not_ set
12890f1702c5SYu Xiangning * sti_laddr_valid here for the caching version of
12900f1702c5SYu Xiangning * getsockname to not break;
12917c478bd9Sstevel@tonic-gate */
12927c478bd9Sstevel@tonic-gate switch (so->so_family) {
12937c478bd9Sstevel@tonic-gate case AF_UNIX:
12947c478bd9Sstevel@tonic-gate /*
12957c478bd9Sstevel@tonic-gate * Record the address bound with the transport
12967c478bd9Sstevel@tonic-gate * for use by socketpair.
12977c478bd9Sstevel@tonic-gate */
12980f1702c5SYu Xiangning bcopy(addr, &sti->sti_ux_laddr, addrlen);
12990f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
13007c478bd9Sstevel@tonic-gate break;
13017c478bd9Sstevel@tonic-gate case AF_INET:
13027c478bd9Sstevel@tonic-gate case AF_INET6:
13030f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
13040f1702c5SYu Xiangning bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
13050f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
13067c478bd9Sstevel@tonic-gate break;
13077c478bd9Sstevel@tonic-gate default:
13087c478bd9Sstevel@tonic-gate /*
13090f1702c5SYu Xiangning * Don't mark sti_laddr_valid, as we cannot be
13107c478bd9Sstevel@tonic-gate * sure that the returned address is the real
13117c478bd9Sstevel@tonic-gate * bound address when talking to an unknown
13127c478bd9Sstevel@tonic-gate * transport.
13137c478bd9Sstevel@tonic-gate */
13147c478bd9Sstevel@tonic-gate break;
13157c478bd9Sstevel@tonic-gate }
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate
13187c478bd9Sstevel@tonic-gate freemsg(mp);
13197c478bd9Sstevel@tonic-gate
13207c478bd9Sstevel@tonic-gate done:
13217c478bd9Sstevel@tonic-gate if (error) {
13227c478bd9Sstevel@tonic-gate /* reset state & backlog to values held on entry */
13237c478bd9Sstevel@tonic-gate if (clear_acceptconn_on_err == B_TRUE)
13247c478bd9Sstevel@tonic-gate so->so_state &= ~SS_ACCEPTCONN;
13257c478bd9Sstevel@tonic-gate if (restore_backlog_on_err == B_TRUE)
13267c478bd9Sstevel@tonic-gate so->so_backlog = save_so_backlog;
13277c478bd9Sstevel@tonic-gate
13287c478bd9Sstevel@tonic-gate if (unbind_on_err && so->so_state & SS_ISBOUND) {
13297c478bd9Sstevel@tonic-gate int err;
13307c478bd9Sstevel@tonic-gate
13317c478bd9Sstevel@tonic-gate err = sotpi_unbind(so, 0);
13327c478bd9Sstevel@tonic-gate /* LINTED - statement has no consequent: if */
13337c478bd9Sstevel@tonic-gate if (err) {
13347c478bd9Sstevel@tonic-gate eprintsoline(so, error);
13357c478bd9Sstevel@tonic-gate } else {
13367c478bd9Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND));
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) {
13417c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
13427c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
13437c478bd9Sstevel@tonic-gate } else {
13447c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
13457c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
13467c478bd9Sstevel@tonic-gate }
13477c478bd9Sstevel@tonic-gate return (error);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate /* bind the socket */
1351ff550d0eSmasputra static int
sotpi_bind(struct sonode * so,struct sockaddr * name,socklen_t namelen,int flags,struct cred * cr)13527c478bd9Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
13530f1702c5SYu Xiangning int flags, struct cred *cr)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate if ((flags & _SOBIND_SOCKETPAIR) == 0)
13560f1702c5SYu Xiangning return (sotpi_bindlisten(so, name, namelen, 0, flags, cr));
13577c478bd9Sstevel@tonic-gate
13587c478bd9Sstevel@tonic-gate flags &= ~_SOBIND_SOCKETPAIR;
13590f1702c5SYu Xiangning return (sotpi_bindlisten(so, name, namelen, 1, flags, cr));
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate
13627c478bd9Sstevel@tonic-gate /*
13637c478bd9Sstevel@tonic-gate * Unbind a socket - used when bind() fails, when bind() specifies a NULL
13647c478bd9Sstevel@tonic-gate * address, or when listen needs to unbind and bind.
13657c478bd9Sstevel@tonic-gate * If the _SOUNBIND_REBIND flag is specified the addresses are retained
13667c478bd9Sstevel@tonic-gate * so that a sobind can pick them up.
13677c478bd9Sstevel@tonic-gate */
13687c478bd9Sstevel@tonic-gate static int
sotpi_unbind(struct sonode * so,int flags)13697c478bd9Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate struct T_unbind_req unbind_req;
13727c478bd9Sstevel@tonic-gate int error = 0;
13737c478bd9Sstevel@tonic-gate mblk_t *mp;
13740f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
13757c478bd9Sstevel@tonic-gate
13767c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n",
1377903a11ebSrh (void *)so, flags, pr_state(so->so_state, so->so_mode)));
13787c478bd9Sstevel@tonic-gate
13797c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
13807c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
13817c478bd9Sstevel@tonic-gate
13827c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) {
13837c478bd9Sstevel@tonic-gate error = EINVAL;
13847c478bd9Sstevel@tonic-gate eprintsoline(so, error);
13857c478bd9Sstevel@tonic-gate goto done;
13867c478bd9Sstevel@tonic-gate }
13877c478bd9Sstevel@tonic-gate
13887c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
13897c478bd9Sstevel@tonic-gate
13907c478bd9Sstevel@tonic-gate /*
13917c478bd9Sstevel@tonic-gate * Flush the read and write side (except stream head read queue)
13927c478bd9Sstevel@tonic-gate * and send down T_UNBIND_REQ.
13937c478bd9Sstevel@tonic-gate */
13947c478bd9Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
13957c478bd9Sstevel@tonic-gate
13967c478bd9Sstevel@tonic-gate unbind_req.PRIM_type = T_UNBIND_REQ;
13977c478bd9Sstevel@tonic-gate mp = soallocproto1(&unbind_req, sizeof (unbind_req),
1398de8c4a14SErik Nordmark 0, _ALLOC_SLEEP, CRED());
13997c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
1400fc80c0dfSnordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
14017c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
14027c478bd9Sstevel@tonic-gate if (error) {
14037c478bd9Sstevel@tonic-gate eprintsoline(so, error);
14047c478bd9Sstevel@tonic-gate goto done;
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate
14077c478bd9Sstevel@tonic-gate error = sowaitokack(so, T_UNBIND_REQ);
14087c478bd9Sstevel@tonic-gate if (error) {
14097c478bd9Sstevel@tonic-gate eprintsoline(so, error);
14107c478bd9Sstevel@tonic-gate goto done;
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate
14137c478bd9Sstevel@tonic-gate /*
14147c478bd9Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in
14157c478bd9Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the unbind
14167c478bd9Sstevel@tonic-gate * is allowed to complete.
14177c478bd9Sstevel@tonic-gate */
14187c478bd9Sstevel@tonic-gate if (!(flags & _SOUNBIND_REBIND)) {
14197c478bd9Sstevel@tonic-gate /*
14207c478bd9Sstevel@tonic-gate * Clear out bound address.
14217c478bd9Sstevel@tonic-gate */
14227c478bd9Sstevel@tonic-gate vnode_t *vp;
14237c478bd9Sstevel@tonic-gate
14240f1702c5SYu Xiangning if ((vp = sti->sti_ux_bound_vp) != NULL) {
14250f1702c5SYu Xiangning sti->sti_ux_bound_vp = NULL;
14267c478bd9Sstevel@tonic-gate vn_rele_stream(vp);
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate /* Clear out address */
14290f1702c5SYu Xiangning sti->sti_laddr_len = 0;
14307c478bd9Sstevel@tonic-gate }
14310f1702c5SYu Xiangning so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN);
14320f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
14332c9e429eSbrutus
14347c478bd9Sstevel@tonic-gate done:
1435c28749e9Skais
14367c478bd9Sstevel@tonic-gate /* If the caller held the lock don't release it here */
14377c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
14387c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
14397c478bd9Sstevel@tonic-gate
14407c478bd9Sstevel@tonic-gate return (error);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate
14437c478bd9Sstevel@tonic-gate /*
14447c478bd9Sstevel@tonic-gate * listen on the socket.
14457c478bd9Sstevel@tonic-gate * For TPI conforming transports this has to first unbind with the transport
14467c478bd9Sstevel@tonic-gate * and then bind again using the new backlog.
14477c478bd9Sstevel@tonic-gate */
14480f1702c5SYu Xiangning /* ARGSUSED */
14497c478bd9Sstevel@tonic-gate int
sotpi_listen(struct sonode * so,int backlog,struct cred * cr)14500f1702c5SYu Xiangning sotpi_listen(struct sonode *so, int backlog, struct cred *cr)
14517c478bd9Sstevel@tonic-gate {
14527c478bd9Sstevel@tonic-gate int error = 0;
14530f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
14547c478bd9Sstevel@tonic-gate
14557c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n",
1456903a11ebSrh (void *)so, backlog, pr_state(so->so_state, so->so_mode)));
14577c478bd9Sstevel@tonic-gate
14580f1702c5SYu Xiangning if (sti->sti_serv_type == T_CLTS)
14597c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
14607c478bd9Sstevel@tonic-gate
14617c478bd9Sstevel@tonic-gate /*
14627c478bd9Sstevel@tonic-gate * If the socket is ready to accept connections already, then
14637c478bd9Sstevel@tonic-gate * return without doing anything. This avoids a problem where
14647c478bd9Sstevel@tonic-gate * a second listen() call fails if a connection is pending and
14657c478bd9Sstevel@tonic-gate * leaves the socket unbound. Only when we are not unbinding
14667c478bd9Sstevel@tonic-gate * with the transport can we safely increase the backlog.
14677c478bd9Sstevel@tonic-gate */
14687c478bd9Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN &&
14697c478bd9Sstevel@tonic-gate !((so->so_family == AF_INET || so->so_family == AF_INET6) &&
1470fc80c0dfSnordmark /*CONSTCOND*/
1471fc80c0dfSnordmark !solisten_tpi_tcp))
14727c478bd9Sstevel@tonic-gate return (0);
14737c478bd9Sstevel@tonic-gate
14747c478bd9Sstevel@tonic-gate if (so->so_state & SS_ISCONNECTED)
14757c478bd9Sstevel@tonic-gate return (EINVAL);
14767c478bd9Sstevel@tonic-gate
14777c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
14787c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
14797c478bd9Sstevel@tonic-gate
14807c478bd9Sstevel@tonic-gate /*
14817c478bd9Sstevel@tonic-gate * If the listen doesn't change the backlog we do nothing.
14827c478bd9Sstevel@tonic-gate * This avoids an EPROTO error from the transport.
14837c478bd9Sstevel@tonic-gate */
14847c478bd9Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) &&
14857c478bd9Sstevel@tonic-gate so->so_backlog == backlog)
14867c478bd9Sstevel@tonic-gate goto done;
14877c478bd9Sstevel@tonic-gate
14887c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) {
14897c478bd9Sstevel@tonic-gate /*
14907c478bd9Sstevel@tonic-gate * Must have been explicitly bound in the UNIX domain.
14917c478bd9Sstevel@tonic-gate */
14927c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
14937c478bd9Sstevel@tonic-gate error = EINVAL;
14947c478bd9Sstevel@tonic-gate goto done;
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog,
14970f1702c5SYu Xiangning _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
14987c478bd9Sstevel@tonic-gate } else if (backlog > 0) {
14997c478bd9Sstevel@tonic-gate /*
15007c478bd9Sstevel@tonic-gate * AF_INET{,6} hack to avoid losing the port.
15017c478bd9Sstevel@tonic-gate * Assumes that all AF_INET{,6} transports can handle a
15027c478bd9Sstevel@tonic-gate * O_T_BIND_REQ with a non-zero CONIND_number when the TPI
15037c478bd9Sstevel@tonic-gate * has already bound thus it is possible to avoid the unbind.
15047c478bd9Sstevel@tonic-gate */
15057c478bd9Sstevel@tonic-gate if (!((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15067c478bd9Sstevel@tonic-gate /*CONSTCOND*/
15077c478bd9Sstevel@tonic-gate !solisten_tpi_tcp)) {
15087c478bd9Sstevel@tonic-gate error = sotpi_unbind(so, _SOUNBIND_REBIND);
15097c478bd9Sstevel@tonic-gate if (error)
15107c478bd9Sstevel@tonic-gate goto done;
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog,
15130f1702c5SYu Xiangning _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15147c478bd9Sstevel@tonic-gate } else {
15157c478bd9Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN;
15167c478bd9Sstevel@tonic-gate so->so_backlog = backlog;
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate if (error)
15197c478bd9Sstevel@tonic-gate goto done;
15207c478bd9Sstevel@tonic-gate ASSERT(so->so_state & SS_ACCEPTCONN);
15217c478bd9Sstevel@tonic-gate done:
15227c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
15237c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
15247c478bd9Sstevel@tonic-gate return (error);
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate
15277c478bd9Sstevel@tonic-gate /*
15287c478bd9Sstevel@tonic-gate * Disconnect either a specified seqno or all (-1).
15297c478bd9Sstevel@tonic-gate * The former is used on listening sockets only.
15307c478bd9Sstevel@tonic-gate *
15317c478bd9Sstevel@tonic-gate * When seqno == -1 sodisconnect could call sotpi_unbind. However,
15327c478bd9Sstevel@tonic-gate * the current use of sodisconnect(seqno == -1) is only for shutdown
15337c478bd9Sstevel@tonic-gate * so there is no point (and potentially incorrect) to unbind.
15347c478bd9Sstevel@tonic-gate */
15350f1702c5SYu Xiangning static int
sodisconnect(struct sonode * so,t_scalar_t seqno,int flags)15367c478bd9Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate struct T_discon_req discon_req;
15397c478bd9Sstevel@tonic-gate int error = 0;
15407c478bd9Sstevel@tonic-gate mblk_t *mp;
15417c478bd9Sstevel@tonic-gate
15427c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n",
1543903a11ebSrh (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode)));
15447c478bd9Sstevel@tonic-gate
15457c478bd9Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) {
15467c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
15477c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
15487c478bd9Sstevel@tonic-gate } else {
15497c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
15507c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate
15537c478bd9Sstevel@tonic-gate if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) {
15547c478bd9Sstevel@tonic-gate error = EINVAL;
15557c478bd9Sstevel@tonic-gate eprintsoline(so, error);
15567c478bd9Sstevel@tonic-gate goto done;
15577c478bd9Sstevel@tonic-gate }
15587c478bd9Sstevel@tonic-gate
15597c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
15607c478bd9Sstevel@tonic-gate /*
15617c478bd9Sstevel@tonic-gate * Flush the write side (unless this is a listener)
15627c478bd9Sstevel@tonic-gate * and then send down a T_DISCON_REQ.
15637c478bd9Sstevel@tonic-gate * (Don't flush on listener since it could flush {O_}T_CONN_RES
15647c478bd9Sstevel@tonic-gate * and other messages.)
15657c478bd9Sstevel@tonic-gate */
15667c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ACCEPTCONN))
15677c478bd9Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW);
15687c478bd9Sstevel@tonic-gate
15697c478bd9Sstevel@tonic-gate discon_req.PRIM_type = T_DISCON_REQ;
15707c478bd9Sstevel@tonic-gate discon_req.SEQ_number = seqno;
15717c478bd9Sstevel@tonic-gate mp = soallocproto1(&discon_req, sizeof (discon_req),
1572de8c4a14SErik Nordmark 0, _ALLOC_SLEEP, CRED());
15737c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
1574fc80c0dfSnordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
15757c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
15767c478bd9Sstevel@tonic-gate if (error) {
15777c478bd9Sstevel@tonic-gate eprintsoline(so, error);
15787c478bd9Sstevel@tonic-gate goto done;
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate
15817c478bd9Sstevel@tonic-gate error = sowaitokack(so, T_DISCON_REQ);
15827c478bd9Sstevel@tonic-gate if (error) {
15837c478bd9Sstevel@tonic-gate eprintsoline(so, error);
15847c478bd9Sstevel@tonic-gate goto done;
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate /*
15877c478bd9Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in
15887c478bd9Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the disconnect
15897c478bd9Sstevel@tonic-gate * is allowed to complete. However, it is not possible to
15907c478bd9Sstevel@tonic-gate * assert that SS_ISCONNECTED|SS_ISCONNECTING are set.
15917c478bd9Sstevel@tonic-gate */
15920f1702c5SYu Xiangning so->so_state &= ~(SS_ISCONNECTED|SS_ISCONNECTING);
15930f1702c5SYu Xiangning SOTOTPI(so)->sti_laddr_valid = 0;
15940f1702c5SYu Xiangning SOTOTPI(so)->sti_faddr_valid = 0;
15957c478bd9Sstevel@tonic-gate done:
15967c478bd9Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) {
15977c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
15987c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
15997c478bd9Sstevel@tonic-gate } else {
16007c478bd9Sstevel@tonic-gate /* If the caller held the lock don't release it here */
16017c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
16027c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
16037c478bd9Sstevel@tonic-gate }
16047c478bd9Sstevel@tonic-gate return (error);
16057c478bd9Sstevel@tonic-gate }
16067c478bd9Sstevel@tonic-gate
16070f1702c5SYu Xiangning /* ARGSUSED */
16087c478bd9Sstevel@tonic-gate int
sotpi_accept(struct sonode * so,int fflag,struct cred * cr,struct sonode ** nsop)16090f1702c5SYu Xiangning sotpi_accept(struct sonode *so, int fflag, struct cred *cr,
16100f1702c5SYu Xiangning struct sonode **nsop)
16117c478bd9Sstevel@tonic-gate {
16127c478bd9Sstevel@tonic-gate struct T_conn_ind *conn_ind;
16137c478bd9Sstevel@tonic-gate struct T_conn_res *conn_res;
16147c478bd9Sstevel@tonic-gate int error = 0;
1615dd49f125SAnders Persson mblk_t *mp, *ack_mp;
16167c478bd9Sstevel@tonic-gate struct sonode *nso;
16177c478bd9Sstevel@tonic-gate vnode_t *nvp;
16187c478bd9Sstevel@tonic-gate void *src;
16197c478bd9Sstevel@tonic-gate t_uscalar_t srclen;
16207c478bd9Sstevel@tonic-gate void *opt;
16217c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
16227c478bd9Sstevel@tonic-gate t_scalar_t PRIM_type;
16237c478bd9Sstevel@tonic-gate t_scalar_t SEQ_number;
1624188658baSja size_t sinlen;
16250f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
16260f1702c5SYu Xiangning sotpi_info_t *nsti;
16277c478bd9Sstevel@tonic-gate
16287c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n",
1629903a11ebSrh (void *)so, fflag, (void *)nsop,
1630903a11ebSrh pr_state(so->so_state, so->so_mode)));
16317c478bd9Sstevel@tonic-gate
16327c478bd9Sstevel@tonic-gate /*
16337c478bd9Sstevel@tonic-gate * Defer single-threading the accepting socket until
16347c478bd9Sstevel@tonic-gate * the T_CONN_IND has been received and parsed and the
16357c478bd9Sstevel@tonic-gate * new sonode has been opened.
16367c478bd9Sstevel@tonic-gate */
16377c478bd9Sstevel@tonic-gate
16387c478bd9Sstevel@tonic-gate /* Check that we are not already connected */
16397c478bd9Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0)
16407c478bd9Sstevel@tonic-gate goto conn_bad;
16417c478bd9Sstevel@tonic-gate again:
16427c478bd9Sstevel@tonic-gate if ((error = sowaitconnind(so, fflag, &mp)) != 0)
16437c478bd9Sstevel@tonic-gate goto e_bad;
16447c478bd9Sstevel@tonic-gate
16450f1702c5SYu Xiangning ASSERT(mp != NULL);
16467c478bd9Sstevel@tonic-gate conn_ind = (struct T_conn_ind *)mp->b_rptr;
1647c28749e9Skais
16487c478bd9Sstevel@tonic-gate /*
16497c478bd9Sstevel@tonic-gate * Save SEQ_number for error paths.
16507c478bd9Sstevel@tonic-gate */
16517c478bd9Sstevel@tonic-gate SEQ_number = conn_ind->SEQ_number;
16527c478bd9Sstevel@tonic-gate
16537c478bd9Sstevel@tonic-gate srclen = conn_ind->SRC_length;
16547c478bd9Sstevel@tonic-gate src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1);
16557c478bd9Sstevel@tonic-gate if (src == NULL) {
16567c478bd9Sstevel@tonic-gate error = EPROTO;
16577c478bd9Sstevel@tonic-gate freemsg(mp);
16587c478bd9Sstevel@tonic-gate eprintsoline(so, error);
16597c478bd9Sstevel@tonic-gate goto disconnect_unlocked;
16607c478bd9Sstevel@tonic-gate }
16617c478bd9Sstevel@tonic-gate optlen = conn_ind->OPT_length;
16627c478bd9Sstevel@tonic-gate switch (so->so_family) {
16637c478bd9Sstevel@tonic-gate case AF_INET:
16647c478bd9Sstevel@tonic-gate case AF_INET6:
16650f1702c5SYu Xiangning if ((optlen == sizeof (intptr_t)) && (sti->sti_direct != 0)) {
16667c478bd9Sstevel@tonic-gate bcopy(mp->b_rptr + conn_ind->OPT_offset,
16677c478bd9Sstevel@tonic-gate &opt, conn_ind->OPT_length);
16687c478bd9Sstevel@tonic-gate } else {
16697c478bd9Sstevel@tonic-gate /*
16707c478bd9Sstevel@tonic-gate * The transport (in this case TCP) hasn't sent up
16717c478bd9Sstevel@tonic-gate * a pointer to an instance for the accept fast-path.
16727c478bd9Sstevel@tonic-gate * Disable fast-path completely because the call to
16737c478bd9Sstevel@tonic-gate * sotpi_create() below would otherwise create an
16747c478bd9Sstevel@tonic-gate * incomplete TCP instance, which would lead to
16757c478bd9Sstevel@tonic-gate * problems when sockfs sends a normal T_CONN_RES
16767c478bd9Sstevel@tonic-gate * message down the new stream.
16777c478bd9Sstevel@tonic-gate */
16780f1702c5SYu Xiangning if (sti->sti_direct) {
1679ff550d0eSmasputra int rval;
1680ff550d0eSmasputra /*
1681ff550d0eSmasputra * For consistency we inform tcp to disable
1682ff550d0eSmasputra * direct interface on the listener, though
1683ff550d0eSmasputra * we can certainly live without doing this
1684ff550d0eSmasputra * because no data will ever travel upstream
1685ff550d0eSmasputra * on the listening socket.
1686ff550d0eSmasputra */
16870f1702c5SYu Xiangning sti->sti_direct = 0;
1688ff550d0eSmasputra (void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK,
1689de8c4a14SErik Nordmark 0, 0, K_TO_K, cr, &rval);
1690ff550d0eSmasputra }
16917c478bd9Sstevel@tonic-gate opt = NULL;
16927c478bd9Sstevel@tonic-gate optlen = 0;
16937c478bd9Sstevel@tonic-gate }
16947c478bd9Sstevel@tonic-gate break;
16957c478bd9Sstevel@tonic-gate case AF_UNIX:
16967c478bd9Sstevel@tonic-gate default:
16977c478bd9Sstevel@tonic-gate if (optlen != 0) {
16987c478bd9Sstevel@tonic-gate opt = sogetoff(mp, conn_ind->OPT_offset, optlen,
16997c478bd9Sstevel@tonic-gate __TPI_ALIGN_SIZE);
17007c478bd9Sstevel@tonic-gate if (opt == NULL) {
17017c478bd9Sstevel@tonic-gate error = EPROTO;
17027c478bd9Sstevel@tonic-gate freemsg(mp);
17037c478bd9Sstevel@tonic-gate eprintsoline(so, error);
17047c478bd9Sstevel@tonic-gate goto disconnect_unlocked;
17057c478bd9Sstevel@tonic-gate }
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
17080f1702c5SYu Xiangning if (!sti->sti_faddr_noxlate) {
17097c478bd9Sstevel@tonic-gate src = NULL;
17107c478bd9Sstevel@tonic-gate srclen = 0;
17117c478bd9Sstevel@tonic-gate }
17127c478bd9Sstevel@tonic-gate /* Extract src address from options */
17137c478bd9Sstevel@tonic-gate if (optlen != 0)
17147c478bd9Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &src, &srclen);
17157c478bd9Sstevel@tonic-gate }
17167c478bd9Sstevel@tonic-gate break;
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate
17197c478bd9Sstevel@tonic-gate /*
17207c478bd9Sstevel@tonic-gate * Create the new socket.
17217c478bd9Sstevel@tonic-gate */
17220f1702c5SYu Xiangning nso = socket_newconn(so, NULL, NULL, SOCKET_SLEEP, &error);
17237c478bd9Sstevel@tonic-gate if (nso == NULL) {
17247c478bd9Sstevel@tonic-gate ASSERT(error != 0);
17257c478bd9Sstevel@tonic-gate /*
17267c478bd9Sstevel@tonic-gate * Accept can not fail with ENOBUFS. sotpi_create
17277c478bd9Sstevel@tonic-gate * sleeps waiting for memory until a signal is caught
17287c478bd9Sstevel@tonic-gate * so return EINTR.
17297c478bd9Sstevel@tonic-gate */
17307c478bd9Sstevel@tonic-gate freemsg(mp);
17317c478bd9Sstevel@tonic-gate if (error == ENOBUFS)
17327c478bd9Sstevel@tonic-gate error = EINTR;
17337c478bd9Sstevel@tonic-gate goto e_disc_unl;
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate nvp = SOTOV(nso);
17360f1702c5SYu Xiangning nsti = SOTOTPI(nso);
17377c478bd9Sstevel@tonic-gate
17387c478bd9Sstevel@tonic-gate #ifdef DEBUG
17397c478bd9Sstevel@tonic-gate /*
17407c478bd9Sstevel@tonic-gate * SO_DEBUG is used to trigger the dprint* and eprint* macros thus
17417c478bd9Sstevel@tonic-gate * it's inherited early to allow debugging of the accept code itself.
17427c478bd9Sstevel@tonic-gate */
17437c478bd9Sstevel@tonic-gate nso->so_options |= so->so_options & SO_DEBUG;
17447c478bd9Sstevel@tonic-gate #endif /* DEBUG */
17457c478bd9Sstevel@tonic-gate
17467c478bd9Sstevel@tonic-gate /*
17477c478bd9Sstevel@tonic-gate * Save the SRC address from the T_CONN_IND
17487c478bd9Sstevel@tonic-gate * for getpeername to work on AF_UNIX and on transports that do not
17497c478bd9Sstevel@tonic-gate * support TI_GETPEERNAME.
17507c478bd9Sstevel@tonic-gate *
17517c478bd9Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by the sender's
17527c478bd9Sstevel@tonic-gate * copyin_name().
17537c478bd9Sstevel@tonic-gate */
17540f1702c5SYu Xiangning if (srclen > (t_uscalar_t)nsti->sti_faddr_maxlen) {
17557c478bd9Sstevel@tonic-gate error = EINVAL;
17567c478bd9Sstevel@tonic-gate freemsg(mp);
17577c478bd9Sstevel@tonic-gate eprintsoline(so, error);
17587c478bd9Sstevel@tonic-gate goto disconnect_vp_unlocked;
17597c478bd9Sstevel@tonic-gate }
17600f1702c5SYu Xiangning nsti->sti_faddr_len = (socklen_t)srclen;
17610f1702c5SYu Xiangning ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
17620f1702c5SYu Xiangning bcopy(src, nsti->sti_faddr_sa, srclen);
17630f1702c5SYu Xiangning nsti->sti_faddr_valid = 1;
17647c478bd9Sstevel@tonic-gate
1765de8c4a14SErik Nordmark /*
1766de8c4a14SErik Nordmark * Record so_peercred and so_cpid from a cred in the T_CONN_IND.
1767de8c4a14SErik Nordmark */
17687c478bd9Sstevel@tonic-gate if ((DB_REF(mp) > 1) || MBLKSIZE(mp) <
17697c478bd9Sstevel@tonic-gate (sizeof (struct T_conn_res) + sizeof (intptr_t))) {
1770de8c4a14SErik Nordmark cred_t *cr;
1771de8c4a14SErik Nordmark pid_t cpid;
17727c478bd9Sstevel@tonic-gate
1773de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1774de8c4a14SErik Nordmark if (cr != NULL) {
17757c478bd9Sstevel@tonic-gate crhold(cr);
17767c478bd9Sstevel@tonic-gate nso->so_peercred = cr;
1777de8c4a14SErik Nordmark nso->so_cpid = cpid;
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate freemsg(mp);
17807c478bd9Sstevel@tonic-gate
17817c478bd9Sstevel@tonic-gate mp = soallocproto1(NULL, sizeof (struct T_conn_res) +
1782d4f98ef9SAnders Persson sizeof (intptr_t), 0, _ALLOC_INTR, cr);
17837c478bd9Sstevel@tonic-gate if (mp == NULL) {
17847c478bd9Sstevel@tonic-gate /*
17857c478bd9Sstevel@tonic-gate * Accept can not fail with ENOBUFS.
17867c478bd9Sstevel@tonic-gate * A signal was caught so return EINTR.
17877c478bd9Sstevel@tonic-gate */
17887c478bd9Sstevel@tonic-gate error = EINTR;
17897c478bd9Sstevel@tonic-gate eprintsoline(so, error);
17907c478bd9Sstevel@tonic-gate goto disconnect_vp_unlocked;
17917c478bd9Sstevel@tonic-gate }
17927c478bd9Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr;
17937c478bd9Sstevel@tonic-gate } else {
1794de8c4a14SErik Nordmark /*
1795de8c4a14SErik Nordmark * For efficency reasons we use msg_extractcred; no crhold
1796de8c4a14SErik Nordmark * needed since db_credp is cleared (i.e., we move the cred
1797de8c4a14SErik Nordmark * from the message to so_peercred.
1798de8c4a14SErik Nordmark */
1799de8c4a14SErik Nordmark nso->so_peercred = msg_extractcred(mp, &nso->so_cpid);
18007c478bd9Sstevel@tonic-gate
18017c478bd9Sstevel@tonic-gate mp->b_rptr = DB_BASE(mp);
18027c478bd9Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr;
18037c478bd9Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res);
1804d4f98ef9SAnders Persson
1805d4f98ef9SAnders Persson mblk_setcred(mp, cr, curproc->p_pid);
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate
18087c478bd9Sstevel@tonic-gate /*
18097c478bd9Sstevel@tonic-gate * New socket must be bound at least in sockfs and, except for AF_INET,
18107c478bd9Sstevel@tonic-gate * (or AF_INET6) it also has to be bound in the transport provider.
1811188658baSja * We set the local address in the sonode from the T_OK_ACK of the
1812188658baSja * T_CONN_RES. For this reason the address we bind to here isn't
1813188658baSja * important.
18147c478bd9Sstevel@tonic-gate */
18157c478bd9Sstevel@tonic-gate if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
18167c478bd9Sstevel@tonic-gate /*CONSTCOND*/
18177c478bd9Sstevel@tonic-gate nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) {
18187c478bd9Sstevel@tonic-gate /*
18197c478bd9Sstevel@tonic-gate * Optimization for AF_INET{,6} transports
18207c478bd9Sstevel@tonic-gate * that can handle a T_CONN_RES without being bound.
18217c478bd9Sstevel@tonic-gate */
18227c478bd9Sstevel@tonic-gate mutex_enter(&nso->so_lock);
18237c478bd9Sstevel@tonic-gate so_automatic_bind(nso);
18247c478bd9Sstevel@tonic-gate mutex_exit(&nso->so_lock);
18257c478bd9Sstevel@tonic-gate } else {
18267c478bd9Sstevel@tonic-gate /* Perform NULL bind with the transport provider. */
18270f1702c5SYu Xiangning if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC,
18280f1702c5SYu Xiangning cr)) != 0) {
18297c478bd9Sstevel@tonic-gate ASSERT(error != ENOBUFS);
18307c478bd9Sstevel@tonic-gate freemsg(mp);
18317c478bd9Sstevel@tonic-gate eprintsoline(nso, error);
18327c478bd9Sstevel@tonic-gate goto disconnect_vp_unlocked;
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate
18367c478bd9Sstevel@tonic-gate /*
18377c478bd9Sstevel@tonic-gate * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES
18387c478bd9Sstevel@tonic-gate * so that any data arriving on the new socket will cause the
18397c478bd9Sstevel@tonic-gate * appropriate signals to be delivered for the new socket.
18407c478bd9Sstevel@tonic-gate *
18417c478bd9Sstevel@tonic-gate * No other thread (except strsock_proto and strsock_misc)
18427c478bd9Sstevel@tonic-gate * can access the new socket thus we relax the locking.
18437c478bd9Sstevel@tonic-gate */
18447c478bd9Sstevel@tonic-gate nso->so_pgrp = so->so_pgrp;
18450f1702c5SYu Xiangning nso->so_state |= so->so_state & SS_ASYNC;
18460f1702c5SYu Xiangning nsti->sti_faddr_noxlate = sti->sti_faddr_noxlate;
18477c478bd9Sstevel@tonic-gate
18487c478bd9Sstevel@tonic-gate if (nso->so_pgrp != 0) {
1849de8c4a14SErik Nordmark if ((error = so_set_events(nso, nvp, cr)) != 0) {
18507c478bd9Sstevel@tonic-gate eprintsoline(nso, error);
18517c478bd9Sstevel@tonic-gate error = 0;
18527c478bd9Sstevel@tonic-gate nso->so_pgrp = 0;
18537c478bd9Sstevel@tonic-gate }
18547c478bd9Sstevel@tonic-gate }
18557c478bd9Sstevel@tonic-gate
18567c478bd9Sstevel@tonic-gate /*
18577c478bd9Sstevel@tonic-gate * Make note of the socket level options. TCP and IP level options
18587c478bd9Sstevel@tonic-gate * are already inherited. We could do all this after accept is
18597c478bd9Sstevel@tonic-gate * successful but doing it here simplifies code and no harm done
18607c478bd9Sstevel@tonic-gate * for error case.
18617c478bd9Sstevel@tonic-gate */
18627c478bd9Sstevel@tonic-gate nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE|
18637c478bd9Sstevel@tonic-gate SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
18647c478bd9Sstevel@tonic-gate SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
18657c478bd9Sstevel@tonic-gate nso->so_sndbuf = so->so_sndbuf;
18667c478bd9Sstevel@tonic-gate nso->so_rcvbuf = so->so_rcvbuf;
18677c478bd9Sstevel@tonic-gate if (nso->so_options & SO_LINGER)
18687c478bd9Sstevel@tonic-gate nso->so_linger = so->so_linger;
18697c478bd9Sstevel@tonic-gate
18700f1702c5SYu Xiangning /*
18710f1702c5SYu Xiangning * Note that the following sti_direct code path should be
18720f1702c5SYu Xiangning * removed once we are confident that the direct sockets
18730f1702c5SYu Xiangning * do not result in any degradation.
18740f1702c5SYu Xiangning */
18750f1702c5SYu Xiangning if (sti->sti_direct) {
18767c478bd9Sstevel@tonic-gate
18777c478bd9Sstevel@tonic-gate ASSERT(opt != NULL);
18787c478bd9Sstevel@tonic-gate
18797c478bd9Sstevel@tonic-gate conn_res->OPT_length = optlen;
18807c478bd9Sstevel@tonic-gate conn_res->OPT_offset = MBLKL(mp);
18817c478bd9Sstevel@tonic-gate bcopy(&opt, mp->b_wptr, optlen);
18827c478bd9Sstevel@tonic-gate mp->b_wptr += optlen;
18837c478bd9Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES;
18847c478bd9Sstevel@tonic-gate conn_res->ACCEPTOR_id = 0;
18857c478bd9Sstevel@tonic-gate PRIM_type = T_CONN_RES;
18867c478bd9Sstevel@tonic-gate
18877c478bd9Sstevel@tonic-gate /* Send down the T_CONN_RES on acceptor STREAM */
18887c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(nso), mp, NULL,
18897c478bd9Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
18907c478bd9Sstevel@tonic-gate if (error) {
18917c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
18927c478bd9Sstevel@tonic-gate so_lock_single(so);
18937c478bd9Sstevel@tonic-gate eprintsoline(so, error);
18947c478bd9Sstevel@tonic-gate goto disconnect_vp;
18957c478bd9Sstevel@tonic-gate }
18967c478bd9Sstevel@tonic-gate mutex_enter(&nso->so_lock);
18977c478bd9Sstevel@tonic-gate error = sowaitprim(nso, T_CONN_RES, T_OK_ACK,
18987c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
18997c478bd9Sstevel@tonic-gate if (error) {
19007c478bd9Sstevel@tonic-gate mutex_exit(&nso->so_lock);
19017c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
19027c478bd9Sstevel@tonic-gate so_lock_single(so);
19037c478bd9Sstevel@tonic-gate eprintsoline(so, error);
19047c478bd9Sstevel@tonic-gate goto disconnect_vp;
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate if (nso->so_family == AF_INET) {
19077c478bd9Sstevel@tonic-gate sin_t *sin;
19087c478bd9Sstevel@tonic-gate
19097c478bd9Sstevel@tonic-gate sin = (sin_t *)(ack_mp->b_rptr +
19107c478bd9Sstevel@tonic-gate sizeof (struct T_ok_ack));
19110f1702c5SYu Xiangning bcopy(sin, nsti->sti_laddr_sa, sizeof (sin_t));
19120f1702c5SYu Xiangning nsti->sti_laddr_len = sizeof (sin_t);
19137c478bd9Sstevel@tonic-gate } else {
19147c478bd9Sstevel@tonic-gate sin6_t *sin6;
19157c478bd9Sstevel@tonic-gate
19167c478bd9Sstevel@tonic-gate sin6 = (sin6_t *)(ack_mp->b_rptr +
19177c478bd9Sstevel@tonic-gate sizeof (struct T_ok_ack));
19180f1702c5SYu Xiangning bcopy(sin6, nsti->sti_laddr_sa, sizeof (sin6_t));
19190f1702c5SYu Xiangning nsti->sti_laddr_len = sizeof (sin6_t);
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate freemsg(ack_mp);
19227c478bd9Sstevel@tonic-gate
19230f1702c5SYu Xiangning nso->so_state |= SS_ISCONNECTED;
19240f1702c5SYu Xiangning nso->so_proto_handle = (sock_lower_handle_t)opt;
19250f1702c5SYu Xiangning nsti->sti_laddr_valid = 1;
19267c478bd9Sstevel@tonic-gate
19277c478bd9Sstevel@tonic-gate mutex_exit(&nso->so_lock);
19287c478bd9Sstevel@tonic-gate
19297d6c035bSja /*
19307d6c035bSja * It's possible, through the use of autopush for example,
19310f1702c5SYu Xiangning * that the acceptor stream may not support sti_direct
19320f1702c5SYu Xiangning * semantics. If the new socket does not support sti_direct
19337d6c035bSja * we issue a _SIOCSOCKFALLBACK to inform the transport
19347d6c035bSja * as we would in the I_PUSH case.
19357d6c035bSja */
19360f1702c5SYu Xiangning if (nsti->sti_direct == 0) {
19377d6c035bSja int rval;
19387d6c035bSja
19397d6c035bSja if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
1940de8c4a14SErik Nordmark 0, 0, K_TO_K, cr, &rval)) != 0) {
19417d6c035bSja mutex_enter(&so->so_lock);
19427d6c035bSja so_lock_single(so);
19437d6c035bSja eprintsoline(so, error);
19447d6c035bSja goto disconnect_vp;
19457d6c035bSja }
19467d6c035bSja }
19477d6c035bSja
19487c478bd9Sstevel@tonic-gate /*
19497c478bd9Sstevel@tonic-gate * Pass out new socket.
19507c478bd9Sstevel@tonic-gate */
19517c478bd9Sstevel@tonic-gate if (nsop != NULL)
19527c478bd9Sstevel@tonic-gate *nsop = nso;
19537c478bd9Sstevel@tonic-gate
19547c478bd9Sstevel@tonic-gate return (0);
19557c478bd9Sstevel@tonic-gate }
19567c478bd9Sstevel@tonic-gate
19577c478bd9Sstevel@tonic-gate /*
19587c478bd9Sstevel@tonic-gate * This is the non-performance case for sockets (e.g. AF_UNIX sockets)
19597c478bd9Sstevel@tonic-gate * which don't support the FireEngine accept fast-path. It is also
19607c478bd9Sstevel@tonic-gate * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd
19617c478bd9Sstevel@tonic-gate * again. Neither sockfs nor TCP attempt to find out if some other
19627c478bd9Sstevel@tonic-gate * random module has been inserted in between (in which case we
19637c478bd9Sstevel@tonic-gate * should follow TLI accept behaviour). We blindly assume the worst
19647c478bd9Sstevel@tonic-gate * case and revert back to old behaviour i.e. TCP will not send us
19657c478bd9Sstevel@tonic-gate * any option (eager) and the accept should happen on the listener
19667c478bd9Sstevel@tonic-gate * queue. Any queued T_conn_ind have already got their options removed
19677c478bd9Sstevel@tonic-gate * by so_sock2_stream() when "sockmod" was I_POP'd.
19687c478bd9Sstevel@tonic-gate */
19697c478bd9Sstevel@tonic-gate /*
19707c478bd9Sstevel@tonic-gate * Fill in the {O_}T_CONN_RES before getting SOLOCKED.
19717c478bd9Sstevel@tonic-gate */
19727c478bd9Sstevel@tonic-gate if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) {
19737c478bd9Sstevel@tonic-gate #ifdef _ILP32
19747c478bd9Sstevel@tonic-gate queue_t *q;
19757c478bd9Sstevel@tonic-gate
19767c478bd9Sstevel@tonic-gate /*
19777c478bd9Sstevel@tonic-gate * Find read queue in driver
19787c478bd9Sstevel@tonic-gate * Can safely do this since we "own" nso/nvp.
19797c478bd9Sstevel@tonic-gate */
19807c478bd9Sstevel@tonic-gate q = strvp2wq(nvp)->q_next;
19817c478bd9Sstevel@tonic-gate while (SAMESTR(q))
19827c478bd9Sstevel@tonic-gate q = q->q_next;
19837c478bd9Sstevel@tonic-gate q = RD(q);
19847c478bd9Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)q;
19857c478bd9Sstevel@tonic-gate #else
19867c478bd9Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev);
19877c478bd9Sstevel@tonic-gate #endif /* _ILP32 */
19887c478bd9Sstevel@tonic-gate conn_res->PRIM_type = O_T_CONN_RES;
19897c478bd9Sstevel@tonic-gate PRIM_type = O_T_CONN_RES;
19907c478bd9Sstevel@tonic-gate } else {
19910f1702c5SYu Xiangning conn_res->ACCEPTOR_id = nsti->sti_acceptor_id;
19927c478bd9Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES;
19937c478bd9Sstevel@tonic-gate PRIM_type = T_CONN_RES;
19947c478bd9Sstevel@tonic-gate }
19957c478bd9Sstevel@tonic-gate conn_res->SEQ_number = SEQ_number;
19967c478bd9Sstevel@tonic-gate conn_res->OPT_length = 0;
19977c478bd9Sstevel@tonic-gate conn_res->OPT_offset = 0;
19987c478bd9Sstevel@tonic-gate
19997c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
20007c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
20017c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
20027c478bd9Sstevel@tonic-gate
20037c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL,
20047c478bd9Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
20057c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
20067c478bd9Sstevel@tonic-gate if (error) {
20077c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20087c478bd9Sstevel@tonic-gate goto disconnect_vp;
20097c478bd9Sstevel@tonic-gate }
2010188658baSja error = sowaitprim(so, PRIM_type, T_OK_ACK,
2011188658baSja (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
20127c478bd9Sstevel@tonic-gate if (error) {
20137c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20147c478bd9Sstevel@tonic-gate goto disconnect_vp;
20157c478bd9Sstevel@tonic-gate }
2016881776cfSGeorge Shepherd mutex_exit(&so->so_lock);
2017188658baSja /*
2018188658baSja * If there is a sin/sin6 appended onto the T_OK_ACK use
2019188658baSja * that to set the local address. If this is not present
2020188658baSja * then we zero out the address and don't set the
20210f1702c5SYu Xiangning * sti_laddr_valid bit. For AF_UNIX endpoints we copy over
20226e81d8daSja * the pathname from the listening socket.
2023881776cfSGeorge Shepherd * In the case where this is TCP or an AF_UNIX socket the
2024881776cfSGeorge Shepherd * client side may have queued data or a T_ORDREL in the
2025881776cfSGeorge Shepherd * transport. Having now sent the T_CONN_RES we may receive
2026881776cfSGeorge Shepherd * those queued messages at any time. Hold the acceptor
2027881776cfSGeorge Shepherd * so_lock until its state and laddr are finalized.
2028188658baSja */
2029881776cfSGeorge Shepherd mutex_enter(&nso->so_lock);
2030188658baSja sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
2031*34793d0fSToomas Soome if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
2032188658baSja MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
2033188658baSja ack_mp->b_rptr += sizeof (struct T_ok_ack);
20340f1702c5SYu Xiangning bcopy(ack_mp->b_rptr, nsti->sti_laddr_sa, sinlen);
20350f1702c5SYu Xiangning nsti->sti_laddr_len = sinlen;
20360f1702c5SYu Xiangning nsti->sti_laddr_valid = 1;
20376e81d8daSja } else if (nso->so_family == AF_UNIX) {
20386e81d8daSja ASSERT(so->so_family == AF_UNIX);
20390f1702c5SYu Xiangning nsti->sti_laddr_len = sti->sti_laddr_len;
20400f1702c5SYu Xiangning ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
20410f1702c5SYu Xiangning bcopy(sti->sti_laddr_sa, nsti->sti_laddr_sa,
20420f1702c5SYu Xiangning nsti->sti_laddr_len);
20430f1702c5SYu Xiangning nsti->sti_laddr_valid = 1;
2044188658baSja } else {
20450f1702c5SYu Xiangning nsti->sti_laddr_len = sti->sti_laddr_len;
20460f1702c5SYu Xiangning ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
20470f1702c5SYu Xiangning bzero(nsti->sti_laddr_sa, nsti->sti_addr_size);
20480f1702c5SYu Xiangning nsti->sti_laddr_sa->sa_family = nso->so_family;
2049188658baSja }
2050881776cfSGeorge Shepherd nso->so_state |= SS_ISCONNECTED;
2051881776cfSGeorge Shepherd mutex_exit(&nso->so_lock);
2052881776cfSGeorge Shepherd
2053188658baSja freemsg(ack_mp);
2054188658baSja
2055881776cfSGeorge Shepherd mutex_enter(&so->so_lock);
20567c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
20577c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
20587c478bd9Sstevel@tonic-gate
20597c478bd9Sstevel@tonic-gate /*
20607c478bd9Sstevel@tonic-gate * Pass out new socket.
20617c478bd9Sstevel@tonic-gate */
20627c478bd9Sstevel@tonic-gate if (nsop != NULL)
20637c478bd9Sstevel@tonic-gate *nsop = nso;
20647c478bd9Sstevel@tonic-gate
20657c478bd9Sstevel@tonic-gate return (0);
20667c478bd9Sstevel@tonic-gate
20677c478bd9Sstevel@tonic-gate
20687c478bd9Sstevel@tonic-gate eproto_disc_unl:
20697c478bd9Sstevel@tonic-gate error = EPROTO;
20707c478bd9Sstevel@tonic-gate e_disc_unl:
20717c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20727c478bd9Sstevel@tonic-gate goto disconnect_unlocked;
20737c478bd9Sstevel@tonic-gate
20747c478bd9Sstevel@tonic-gate pr_disc_vp_unl:
20757c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20767c478bd9Sstevel@tonic-gate disconnect_vp_unlocked:
2077de8c4a14SErik Nordmark (void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
20787c478bd9Sstevel@tonic-gate VN_RELE(nvp);
20797c478bd9Sstevel@tonic-gate disconnect_unlocked:
20807c478bd9Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, 0);
20817c478bd9Sstevel@tonic-gate return (error);
20827c478bd9Sstevel@tonic-gate
20837c478bd9Sstevel@tonic-gate pr_disc_vp:
20847c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20857c478bd9Sstevel@tonic-gate disconnect_vp:
20867c478bd9Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
20877c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
20887c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
2089de8c4a14SErik Nordmark (void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
20907c478bd9Sstevel@tonic-gate VN_RELE(nvp);
20917c478bd9Sstevel@tonic-gate return (error);
20927c478bd9Sstevel@tonic-gate
20937c478bd9Sstevel@tonic-gate conn_bad: /* Note: SunOS 4/BSD unconditionally returns EINVAL here */
20947c478bd9Sstevel@tonic-gate error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
20957c478bd9Sstevel@tonic-gate ? EOPNOTSUPP : EINVAL;
20967c478bd9Sstevel@tonic-gate e_bad:
20977c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20987c478bd9Sstevel@tonic-gate return (error);
20997c478bd9Sstevel@tonic-gate }
21007c478bd9Sstevel@tonic-gate
21017c478bd9Sstevel@tonic-gate /*
21027c478bd9Sstevel@tonic-gate * connect a socket.
21037c478bd9Sstevel@tonic-gate *
21047c478bd9Sstevel@tonic-gate * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
21057c478bd9Sstevel@tonic-gate * unconnect (by specifying a null address).
21067c478bd9Sstevel@tonic-gate */
21077c478bd9Sstevel@tonic-gate int
sotpi_connect(struct sonode * so,struct sockaddr * name,socklen_t namelen,int fflag,int flags,struct cred * cr)21087c478bd9Sstevel@tonic-gate sotpi_connect(struct sonode *so,
2109f012ee0cSGordon Ross struct sockaddr *name,
2110f012ee0cSGordon Ross socklen_t namelen,
2111f012ee0cSGordon Ross int fflag,
2112f012ee0cSGordon Ross int flags,
2113f012ee0cSGordon Ross struct cred *cr)
21147c478bd9Sstevel@tonic-gate {
21157c478bd9Sstevel@tonic-gate struct T_conn_req conn_req;
21167c478bd9Sstevel@tonic-gate int error = 0;
21177c478bd9Sstevel@tonic-gate mblk_t *mp;
21187c478bd9Sstevel@tonic-gate void *src;
21197c478bd9Sstevel@tonic-gate socklen_t srclen;
21207c478bd9Sstevel@tonic-gate void *addr;
21217c478bd9Sstevel@tonic-gate socklen_t addrlen;
21227c478bd9Sstevel@tonic-gate boolean_t need_unlock;
21230f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
21247c478bd9Sstevel@tonic-gate
21257c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
2126903a11ebSrh (void *)so, (void *)name, namelen, fflag, flags,
2127fc80c0dfSnordmark pr_state(so->so_state, so->so_mode)));
21287c478bd9Sstevel@tonic-gate
21297c478bd9Sstevel@tonic-gate /*
21307c478bd9Sstevel@tonic-gate * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
21317c478bd9Sstevel@tonic-gate * avoid sleeping for memory with SOLOCKED held.
21320f1702c5SYu Xiangning * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen
21337c478bd9Sstevel@tonic-gate * + sizeof (struct T_opthdr).
21347c478bd9Sstevel@tonic-gate * (the AF_UNIX so_ux_addr_xlate() does not make the address
21350f1702c5SYu Xiangning * exceed sti_faddr_maxlen).
21367c478bd9Sstevel@tonic-gate */
21377c478bd9Sstevel@tonic-gate mp = soallocproto(sizeof (struct T_conn_req) +
2138de8c4a14SErik Nordmark 2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR,
2139de8c4a14SErik Nordmark cr);
21407c478bd9Sstevel@tonic-gate if (mp == NULL) {
21417c478bd9Sstevel@tonic-gate /*
21427c478bd9Sstevel@tonic-gate * Connect can not fail with ENOBUFS. A signal was
21437c478bd9Sstevel@tonic-gate * caught so return EINTR.
21447c478bd9Sstevel@tonic-gate */
21457c478bd9Sstevel@tonic-gate error = EINTR;
21467c478bd9Sstevel@tonic-gate eprintsoline(so, error);
21477c478bd9Sstevel@tonic-gate return (error);
21487c478bd9Sstevel@tonic-gate }
21497c478bd9Sstevel@tonic-gate
21507c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
21517c478bd9Sstevel@tonic-gate /*
2152ba3431dfSjprakash * Make sure there is a preallocated T_unbind_req message
2153ba3431dfSjprakash * before any binding. This message is allocated when the
2154ba3431dfSjprakash * socket is created. Since another thread can consume
2155ba3431dfSjprakash * so_unbind_mp by the time we return from so_lock_single(),
2156ba3431dfSjprakash * we should check the availability of so_unbind_mp after
2157ba3431dfSjprakash * we return from so_lock_single().
21587c478bd9Sstevel@tonic-gate */
2159ba3431dfSjprakash
2160ba3431dfSjprakash so_lock_single(so); /* Set SOLOCKED */
2161ba3431dfSjprakash need_unlock = B_TRUE;
2162ba3431dfSjprakash
21630f1702c5SYu Xiangning if (sti->sti_unbind_mp == NULL) {
21647c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
21657c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
21660f1702c5SYu Xiangning sti->sti_unbind_mp =
2167de8c4a14SErik Nordmark soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR, cr);
21680f1702c5SYu Xiangning if (sti->sti_unbind_mp == NULL) {
21697c478bd9Sstevel@tonic-gate error = EINTR;
21707c478bd9Sstevel@tonic-gate goto done;
21717c478bd9Sstevel@tonic-gate }
21727c478bd9Sstevel@tonic-gate }
21737c478bd9Sstevel@tonic-gate
21747c478bd9Sstevel@tonic-gate /*
21757c478bd9Sstevel@tonic-gate * Can't have done a listen before connecting.
21767c478bd9Sstevel@tonic-gate */
21777c478bd9Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) {
21787c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
21797c478bd9Sstevel@tonic-gate goto done;
21807c478bd9Sstevel@tonic-gate }
21817c478bd9Sstevel@tonic-gate
21827c478bd9Sstevel@tonic-gate /*
21837c478bd9Sstevel@tonic-gate * Must be bound with the transport
21847c478bd9Sstevel@tonic-gate */
21857c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) {
21867c478bd9Sstevel@tonic-gate if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
21877c478bd9Sstevel@tonic-gate /*CONSTCOND*/
21887c478bd9Sstevel@tonic-gate so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
21897c478bd9Sstevel@tonic-gate /*
21907c478bd9Sstevel@tonic-gate * Optimization for AF_INET{,6} transports
21917c478bd9Sstevel@tonic-gate * that can handle a T_CONN_REQ without being bound.
21927c478bd9Sstevel@tonic-gate */
21937c478bd9Sstevel@tonic-gate so_automatic_bind(so);
21947c478bd9Sstevel@tonic-gate } else {
21957c478bd9Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0,
21960f1702c5SYu Xiangning _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
21977c478bd9Sstevel@tonic-gate if (error)
21987c478bd9Sstevel@tonic-gate goto done;
21997c478bd9Sstevel@tonic-gate }
22007c478bd9Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND);
22017c478bd9Sstevel@tonic-gate flags |= _SOCONNECT_DID_BIND;
22027c478bd9Sstevel@tonic-gate }
22037c478bd9Sstevel@tonic-gate
22047c478bd9Sstevel@tonic-gate /*
22057c478bd9Sstevel@tonic-gate * Handle a connect to a name parameter of type AF_UNSPEC like a
22067c478bd9Sstevel@tonic-gate * connect to a null address. This is the portable method to
22077c478bd9Sstevel@tonic-gate * unconnect a socket.
22087c478bd9Sstevel@tonic-gate */
22097c478bd9Sstevel@tonic-gate if ((namelen >= sizeof (sa_family_t)) &&
22107c478bd9Sstevel@tonic-gate (name->sa_family == AF_UNSPEC)) {
22117c478bd9Sstevel@tonic-gate name = NULL;
22127c478bd9Sstevel@tonic-gate namelen = 0;
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate
22157c478bd9Sstevel@tonic-gate /*
22167c478bd9Sstevel@tonic-gate * Check that we are not already connected.
22177c478bd9Sstevel@tonic-gate * A connection-oriented socket cannot be reconnected.
22187c478bd9Sstevel@tonic-gate * A connected connection-less socket can be
22197c478bd9Sstevel@tonic-gate * - connected to a different address by a subsequent connect
22207c478bd9Sstevel@tonic-gate * - "unconnected" by a connect to the NULL address
22217c478bd9Sstevel@tonic-gate */
22227c478bd9Sstevel@tonic-gate if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
22237c478bd9Sstevel@tonic-gate ASSERT(!(flags & _SOCONNECT_DID_BIND));
22247c478bd9Sstevel@tonic-gate if (so->so_mode & SM_CONNREQUIRED) {
22257c478bd9Sstevel@tonic-gate /* Connection-oriented socket */
22267c478bd9Sstevel@tonic-gate error = so->so_state & SS_ISCONNECTED ?
22277c478bd9Sstevel@tonic-gate EISCONN : EALREADY;
22287c478bd9Sstevel@tonic-gate goto done;
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate /* Connection-less socket */
22317c478bd9Sstevel@tonic-gate if (name == NULL) {
22327c478bd9Sstevel@tonic-gate /*
22337c478bd9Sstevel@tonic-gate * Remove the connected state and clear SO_DGRAM_ERRIND
22347c478bd9Sstevel@tonic-gate * since it was set when the socket was connected.
22357c478bd9Sstevel@tonic-gate * If this is UDP also send down a T_DISCON_REQ.
22367c478bd9Sstevel@tonic-gate */
22377c478bd9Sstevel@tonic-gate int val;
22387c478bd9Sstevel@tonic-gate
22397c478bd9Sstevel@tonic-gate if ((so->so_family == AF_INET ||
2240fc80c0dfSnordmark so->so_family == AF_INET6) &&
22417c478bd9Sstevel@tonic-gate (so->so_type == SOCK_DGRAM ||
2242fc80c0dfSnordmark so->so_type == SOCK_RAW) &&
22437c478bd9Sstevel@tonic-gate /*CONSTCOND*/
22447c478bd9Sstevel@tonic-gate !soconnect_tpi_udp) {
22457c478bd9Sstevel@tonic-gate /* XXX What about implicitly unbinding here? */
22467c478bd9Sstevel@tonic-gate error = sodisconnect(so, -1,
2247fc80c0dfSnordmark _SODISCONNECT_LOCK_HELD);
22487c478bd9Sstevel@tonic-gate } else {
22497c478bd9Sstevel@tonic-gate so->so_state &=
22500f1702c5SYu Xiangning ~(SS_ISCONNECTED | SS_ISCONNECTING);
22510f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
22520f1702c5SYu Xiangning sti->sti_faddr_len = 0;
22537c478bd9Sstevel@tonic-gate }
22547c478bd9Sstevel@tonic-gate
22550f1702c5SYu Xiangning /* Remove SOLOCKED since setsockopt will grab it */
22567c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
22577c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
22587c478bd9Sstevel@tonic-gate
22597c478bd9Sstevel@tonic-gate val = 0;
22600f1702c5SYu Xiangning (void) sotpi_setsockopt(so, SOL_SOCKET,
22610f1702c5SYu Xiangning SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val),
22620f1702c5SYu Xiangning cr);
22637c478bd9Sstevel@tonic-gate
22647c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
22657c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
22667c478bd9Sstevel@tonic-gate goto done;
22677c478bd9Sstevel@tonic-gate }
22687c478bd9Sstevel@tonic-gate }
22697c478bd9Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND);
22707c478bd9Sstevel@tonic-gate
22717c478bd9Sstevel@tonic-gate if (name == NULL || namelen == 0) {
22727c478bd9Sstevel@tonic-gate error = EINVAL;
22737c478bd9Sstevel@tonic-gate goto done;
22747c478bd9Sstevel@tonic-gate }
22757c478bd9Sstevel@tonic-gate /*
22760f1702c5SYu Xiangning * Mark the socket if sti_faddr_sa represents the transport level
22777c478bd9Sstevel@tonic-gate * address.
22787c478bd9Sstevel@tonic-gate */
22797c478bd9Sstevel@tonic-gate if (flags & _SOCONNECT_NOXLATE) {
22807c478bd9Sstevel@tonic-gate struct sockaddr_ux *soaddr_ux;
22817c478bd9Sstevel@tonic-gate
22827c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX);
22837c478bd9Sstevel@tonic-gate if (namelen != sizeof (struct sockaddr_ux)) {
22847c478bd9Sstevel@tonic-gate error = EINVAL;
22857c478bd9Sstevel@tonic-gate goto done;
22867c478bd9Sstevel@tonic-gate }
22877c478bd9Sstevel@tonic-gate soaddr_ux = (struct sockaddr_ux *)name;
22887c478bd9Sstevel@tonic-gate name = (struct sockaddr *)&soaddr_ux->sou_addr;
22897c478bd9Sstevel@tonic-gate namelen = sizeof (soaddr_ux->sou_addr);
22900f1702c5SYu Xiangning sti->sti_faddr_noxlate = 1;
22917c478bd9Sstevel@tonic-gate }
22927c478bd9Sstevel@tonic-gate
22937c478bd9Sstevel@tonic-gate /*
22947c478bd9Sstevel@tonic-gate * Length and family checks.
22957c478bd9Sstevel@tonic-gate */
22967c478bd9Sstevel@tonic-gate error = so_addr_verify(so, name, namelen);
22977c478bd9Sstevel@tonic-gate if (error)
22987c478bd9Sstevel@tonic-gate goto bad;
22997c478bd9Sstevel@tonic-gate
23007c478bd9Sstevel@tonic-gate /*
23017c478bd9Sstevel@tonic-gate * Save foreign address. Needed for AF_UNIX as well as
23027c478bd9Sstevel@tonic-gate * transport providers that do not support TI_GETPEERNAME.
23037c478bd9Sstevel@tonic-gate * Also used for cached foreign address for TCP and UDP.
23047c478bd9Sstevel@tonic-gate */
23050f1702c5SYu Xiangning if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) {
23067c478bd9Sstevel@tonic-gate error = EINVAL;
23077c478bd9Sstevel@tonic-gate goto done;
23087c478bd9Sstevel@tonic-gate }
23090f1702c5SYu Xiangning sti->sti_faddr_len = (socklen_t)namelen;
23100f1702c5SYu Xiangning ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
23110f1702c5SYu Xiangning bcopy(name, sti->sti_faddr_sa, namelen);
23120f1702c5SYu Xiangning sti->sti_faddr_valid = 1;
23137c478bd9Sstevel@tonic-gate
23147c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
23150f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
23167c478bd9Sstevel@tonic-gate /*
2317b521c41bSGordon Ross * sti_faddr is a transport-level address, so
2318b521c41bSGordon Ross * don't pass it as an option. Do save it in
2319b521c41bSGordon Ross * sti_ux_faddr, used for connected DG send.
23207c478bd9Sstevel@tonic-gate */
23217c478bd9Sstevel@tonic-gate src = NULL;
23227c478bd9Sstevel@tonic-gate srclen = 0;
2323b521c41bSGordon Ross addr = sti->sti_faddr_sa;
2324b521c41bSGordon Ross addrlen = (t_uscalar_t)sti->sti_faddr_len;
2325b521c41bSGordon Ross bcopy(addr, &sti->sti_ux_faddr,
2326b521c41bSGordon Ross sizeof (sti->sti_ux_faddr));
23277c478bd9Sstevel@tonic-gate } else {
23287c478bd9Sstevel@tonic-gate /*
23297c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
23307c478bd9Sstevel@tonic-gate * and translate the remote address.
23310f1702c5SYu Xiangning * Holding so_lock thus sti_laddr_sa can not change.
23327c478bd9Sstevel@tonic-gate */
23330f1702c5SYu Xiangning src = sti->sti_laddr_sa;
23340f1702c5SYu Xiangning srclen = (t_uscalar_t)sti->sti_laddr_len;
23357c478bd9Sstevel@tonic-gate dprintso(so, 1,
2336fc80c0dfSnordmark ("sotpi_connect UNIX: srclen %d, src %p\n",
2337fc80c0dfSnordmark srclen, src));
2338f012ee0cSGordon Ross /*
2339f012ee0cSGordon Ross * Translate the destination address into our
2340f012ee0cSGordon Ross * internal form, and save it in sti_ux_faddr.
2341f012ee0cSGordon Ross * After this call, addr==&sti->sti_ux_taddr,
2342f012ee0cSGordon Ross * and we copy that to sti->sti_ux_faddr so
2343f012ee0cSGordon Ross * we save the connected peer address.
2344f012ee0cSGordon Ross */
23457c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so,
23460f1702c5SYu Xiangning sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len,
2347fc80c0dfSnordmark (flags & _SOCONNECT_XPG4_2),
2348fc80c0dfSnordmark &addr, &addrlen);
23497c478bd9Sstevel@tonic-gate if (error)
23507c478bd9Sstevel@tonic-gate goto bad;
2351f012ee0cSGordon Ross bcopy(&sti->sti_ux_taddr, &sti->sti_ux_faddr,
2352f012ee0cSGordon Ross sizeof (sti->sti_ux_faddr));
23537c478bd9Sstevel@tonic-gate }
23547c478bd9Sstevel@tonic-gate } else {
23550f1702c5SYu Xiangning addr = sti->sti_faddr_sa;
23560f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_len;
23577c478bd9Sstevel@tonic-gate src = NULL;
23587c478bd9Sstevel@tonic-gate srclen = 0;
23597c478bd9Sstevel@tonic-gate }
23607c478bd9Sstevel@tonic-gate /*
23617c478bd9Sstevel@tonic-gate * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
23627c478bd9Sstevel@tonic-gate * option which asks the transport provider to send T_UDERR_IND
23637c478bd9Sstevel@tonic-gate * messages. These T_UDERR_IND messages are used to return connected
23647c478bd9Sstevel@tonic-gate * style errors (e.g. ECONNRESET) for connected datagram sockets.
23657c478bd9Sstevel@tonic-gate *
23667c478bd9Sstevel@tonic-gate * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
23677c478bd9Sstevel@tonic-gate * we send down a T_CONN_REQ. This is needed to let the
23687c478bd9Sstevel@tonic-gate * transport assign a local address that is consistent with
23697c478bd9Sstevel@tonic-gate * the remote address. Applications depend on a getsockname()
23707c478bd9Sstevel@tonic-gate * after a connect() to retrieve the "source" IP address for
23717c478bd9Sstevel@tonic-gate * the connected socket. Invalidate the cached local address
23727c478bd9Sstevel@tonic-gate * to force getsockname() to enquire of the transport.
23737c478bd9Sstevel@tonic-gate */
23747c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_CONNREQUIRED)) {
23757c478bd9Sstevel@tonic-gate /*
23767c478bd9Sstevel@tonic-gate * Datagram socket.
23777c478bd9Sstevel@tonic-gate */
23787c478bd9Sstevel@tonic-gate int32_t val;
23797c478bd9Sstevel@tonic-gate
23807c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
23817c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
23827c478bd9Sstevel@tonic-gate
23837c478bd9Sstevel@tonic-gate val = 1;
23847c478bd9Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
23850f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
23867c478bd9Sstevel@tonic-gate
23877c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
23887c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
23897c478bd9Sstevel@tonic-gate if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
23907c478bd9Sstevel@tonic-gate (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
23917c478bd9Sstevel@tonic-gate soconnect_tpi_udp) {
23927c478bd9Sstevel@tonic-gate soisconnected(so);
23937c478bd9Sstevel@tonic-gate goto done;
23947c478bd9Sstevel@tonic-gate }
23957c478bd9Sstevel@tonic-gate /*
23967c478bd9Sstevel@tonic-gate * Send down T_CONN_REQ etc.
23977c478bd9Sstevel@tonic-gate * Clear fflag to avoid returning EWOULDBLOCK.
23987c478bd9Sstevel@tonic-gate */
23997c478bd9Sstevel@tonic-gate fflag = 0;
24007c478bd9Sstevel@tonic-gate ASSERT(so->so_family != AF_UNIX);
24010f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
24020f1702c5SYu Xiangning } else if (sti->sti_laddr_len != 0) {
24037c478bd9Sstevel@tonic-gate /*
24047c478bd9Sstevel@tonic-gate * If the local address or port was "any" then it may be
24057c478bd9Sstevel@tonic-gate * changed by the transport as a result of the
24067c478bd9Sstevel@tonic-gate * connect. Invalidate the cached version if we have one.
24077c478bd9Sstevel@tonic-gate */
24087c478bd9Sstevel@tonic-gate switch (so->so_family) {
24097c478bd9Sstevel@tonic-gate case AF_INET:
24100f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t));
24110f1702c5SYu Xiangning if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr ==
24127c478bd9Sstevel@tonic-gate INADDR_ANY ||
24130f1702c5SYu Xiangning ((sin_t *)sti->sti_laddr_sa)->sin_port == 0)
24140f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
24157c478bd9Sstevel@tonic-gate break;
24167c478bd9Sstevel@tonic-gate
24177c478bd9Sstevel@tonic-gate case AF_INET6:
24180f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len ==
24190f1702c5SYu Xiangning (socklen_t)sizeof (sin6_t));
24207c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED(
24210f1702c5SYu Xiangning &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) ||
24227c478bd9Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED_ANY(
24230f1702c5SYu Xiangning &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) ||
24240f1702c5SYu Xiangning ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0)
24250f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
24267c478bd9Sstevel@tonic-gate break;
24277c478bd9Sstevel@tonic-gate
24287c478bd9Sstevel@tonic-gate default:
24297c478bd9Sstevel@tonic-gate break;
24307c478bd9Sstevel@tonic-gate }
24317c478bd9Sstevel@tonic-gate }
24327c478bd9Sstevel@tonic-gate
24337c478bd9Sstevel@tonic-gate /*
24347c478bd9Sstevel@tonic-gate * Check for failure of an earlier call
24357c478bd9Sstevel@tonic-gate */
24367c478bd9Sstevel@tonic-gate if (so->so_error != 0)
24377c478bd9Sstevel@tonic-gate goto so_bad;
24387c478bd9Sstevel@tonic-gate
24397c478bd9Sstevel@tonic-gate /*
24407c478bd9Sstevel@tonic-gate * Send down T_CONN_REQ. Message was allocated above.
24417c478bd9Sstevel@tonic-gate */
24427c478bd9Sstevel@tonic-gate conn_req.PRIM_type = T_CONN_REQ;
24437c478bd9Sstevel@tonic-gate conn_req.DEST_length = addrlen;
24447c478bd9Sstevel@tonic-gate conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
24457c478bd9Sstevel@tonic-gate if (srclen == 0) {
24467c478bd9Sstevel@tonic-gate conn_req.OPT_length = 0;
24477c478bd9Sstevel@tonic-gate conn_req.OPT_offset = 0;
24487c478bd9Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req));
24497c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
24507c478bd9Sstevel@tonic-gate } else {
24517c478bd9Sstevel@tonic-gate /*
24527c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
24537c478bd9Sstevel@tonic-gate * address option.
24547c478bd9Sstevel@tonic-gate */
24557c478bd9Sstevel@tonic-gate struct T_opthdr toh;
24567c478bd9Sstevel@tonic-gate
24577c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
24587c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
24597c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
24607c478bd9Sstevel@tonic-gate toh.status = 0;
24617c478bd9Sstevel@tonic-gate conn_req.OPT_length =
2462fc80c0dfSnordmark (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
24637c478bd9Sstevel@tonic-gate conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
2464fc80c0dfSnordmark _TPI_ALIGN_TOPT(addrlen));
24657c478bd9Sstevel@tonic-gate
24667c478bd9Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req));
24677c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
24687c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
24697c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
24707c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
24717c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
24727c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
24737c478bd9Sstevel@tonic-gate }
24747c478bd9Sstevel@tonic-gate /*
24757c478bd9Sstevel@tonic-gate * Set SS_ISCONNECTING before sending down the T_CONN_REQ
24767c478bd9Sstevel@tonic-gate * in order to have the right state when the T_CONN_CON shows up.
24777c478bd9Sstevel@tonic-gate */
24787c478bd9Sstevel@tonic-gate soisconnecting(so);
24797c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
24807c478bd9Sstevel@tonic-gate
2481005d3febSMarek Pospisil if (AU_AUDITING())
24827c478bd9Sstevel@tonic-gate audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
24837c478bd9Sstevel@tonic-gate
24847c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2485fc80c0dfSnordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
24867c478bd9Sstevel@tonic-gate mp = NULL;
24877c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
24887c478bd9Sstevel@tonic-gate if (error != 0)
24897c478bd9Sstevel@tonic-gate goto bad;
24907c478bd9Sstevel@tonic-gate
24917c478bd9Sstevel@tonic-gate if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
24927c478bd9Sstevel@tonic-gate goto bad;
24937c478bd9Sstevel@tonic-gate
24947c478bd9Sstevel@tonic-gate /* Allow other threads to access the socket */
24957c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
24967c478bd9Sstevel@tonic-gate need_unlock = B_FALSE;
24977c478bd9Sstevel@tonic-gate
24987c478bd9Sstevel@tonic-gate /*
24997c478bd9Sstevel@tonic-gate * Wait until we get a T_CONN_CON or an error
25007c478bd9Sstevel@tonic-gate */
25017c478bd9Sstevel@tonic-gate if ((error = sowaitconnected(so, fflag, 0)) != 0) {
25027c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
25037c478bd9Sstevel@tonic-gate need_unlock = B_TRUE;
25047c478bd9Sstevel@tonic-gate }
25057c478bd9Sstevel@tonic-gate
25067c478bd9Sstevel@tonic-gate done:
25077c478bd9Sstevel@tonic-gate freemsg(mp);
25087c478bd9Sstevel@tonic-gate switch (error) {
25097c478bd9Sstevel@tonic-gate case EINPROGRESS:
25107c478bd9Sstevel@tonic-gate case EALREADY:
25117c478bd9Sstevel@tonic-gate case EISCONN:
25127c478bd9Sstevel@tonic-gate case EINTR:
25137c478bd9Sstevel@tonic-gate /* Non-fatal errors */
25140f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
25157c478bd9Sstevel@tonic-gate /* FALLTHRU */
25167c478bd9Sstevel@tonic-gate case 0:
25177c478bd9Sstevel@tonic-gate break;
25187c478bd9Sstevel@tonic-gate default:
25197c478bd9Sstevel@tonic-gate ASSERT(need_unlock);
25207c478bd9Sstevel@tonic-gate /*
25217c478bd9Sstevel@tonic-gate * Fatal errors: clear SS_ISCONNECTING in case it was set,
25227c478bd9Sstevel@tonic-gate * and invalidate local-address cache
25237c478bd9Sstevel@tonic-gate */
25240f1702c5SYu Xiangning so->so_state &= ~SS_ISCONNECTING;
25250f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
25267c478bd9Sstevel@tonic-gate /* A discon_ind might have already unbound us */
25277c478bd9Sstevel@tonic-gate if ((flags & _SOCONNECT_DID_BIND) &&
25287c478bd9Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) {
25297c478bd9Sstevel@tonic-gate int err;
25307c478bd9Sstevel@tonic-gate
25317c478bd9Sstevel@tonic-gate err = sotpi_unbind(so, 0);
25327c478bd9Sstevel@tonic-gate /* LINTED - statement has no conseq */
25337c478bd9Sstevel@tonic-gate if (err) {
25347c478bd9Sstevel@tonic-gate eprintsoline(so, err);
25357c478bd9Sstevel@tonic-gate }
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate break;
25387c478bd9Sstevel@tonic-gate }
25397c478bd9Sstevel@tonic-gate if (need_unlock)
25407c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
25417c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
25427c478bd9Sstevel@tonic-gate return (error);
25437c478bd9Sstevel@tonic-gate
25440f1702c5SYu Xiangning so_bad: error = sogeterr(so, B_TRUE);
25457c478bd9Sstevel@tonic-gate bad: eprintsoline(so, error);
25467c478bd9Sstevel@tonic-gate goto done;
25477c478bd9Sstevel@tonic-gate }
25487c478bd9Sstevel@tonic-gate
25490f1702c5SYu Xiangning /* ARGSUSED */
25507c478bd9Sstevel@tonic-gate int
sotpi_shutdown(struct sonode * so,int how,struct cred * cr)25510f1702c5SYu Xiangning sotpi_shutdown(struct sonode *so, int how, struct cred *cr)
25527c478bd9Sstevel@tonic-gate {
25537c478bd9Sstevel@tonic-gate struct T_ordrel_req ordrel_req;
25547c478bd9Sstevel@tonic-gate mblk_t *mp;
25557c478bd9Sstevel@tonic-gate uint_t old_state, state_change;
25567c478bd9Sstevel@tonic-gate int error = 0;
25570f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
25587c478bd9Sstevel@tonic-gate
25597c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
2560903a11ebSrh (void *)so, how, pr_state(so->so_state, so->so_mode)));
25617c478bd9Sstevel@tonic-gate
25627c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
25637c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
25647c478bd9Sstevel@tonic-gate
25657c478bd9Sstevel@tonic-gate /*
25667c478bd9Sstevel@tonic-gate * SunOS 4.X has no check for datagram sockets.
25677c478bd9Sstevel@tonic-gate * 5.X checks that it is connected (ENOTCONN)
25687c478bd9Sstevel@tonic-gate * X/Open requires that we check the connected state.
25697c478bd9Sstevel@tonic-gate */
25707c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) {
25717c478bd9Sstevel@tonic-gate if (!xnet_skip_checks) {
25727c478bd9Sstevel@tonic-gate error = ENOTCONN;
25737c478bd9Sstevel@tonic-gate if (xnet_check_print) {
25747c478bd9Sstevel@tonic-gate printf("sockfs: X/Open shutdown check "
2575fc80c0dfSnordmark "caused ENOTCONN\n");
25767c478bd9Sstevel@tonic-gate }
25777c478bd9Sstevel@tonic-gate }
25787c478bd9Sstevel@tonic-gate goto done;
25797c478bd9Sstevel@tonic-gate }
25807c478bd9Sstevel@tonic-gate /*
25817c478bd9Sstevel@tonic-gate * Record the current state and then perform any state changes.
25827c478bd9Sstevel@tonic-gate * Then use the difference between the old and new states to
25837c478bd9Sstevel@tonic-gate * determine which messages need to be sent.
25847c478bd9Sstevel@tonic-gate * This prevents e.g. duplicate T_ORDREL_REQ when there are
25857c478bd9Sstevel@tonic-gate * duplicate calls to shutdown().
25867c478bd9Sstevel@tonic-gate */
25877c478bd9Sstevel@tonic-gate old_state = so->so_state;
25887c478bd9Sstevel@tonic-gate
25897c478bd9Sstevel@tonic-gate switch (how) {
25907c478bd9Sstevel@tonic-gate case 0:
25917c478bd9Sstevel@tonic-gate socantrcvmore(so);
25927c478bd9Sstevel@tonic-gate break;
25937c478bd9Sstevel@tonic-gate case 1:
25947c478bd9Sstevel@tonic-gate socantsendmore(so);
25957c478bd9Sstevel@tonic-gate break;
25967c478bd9Sstevel@tonic-gate case 2:
25977c478bd9Sstevel@tonic-gate socantsendmore(so);
25987c478bd9Sstevel@tonic-gate socantrcvmore(so);
25997c478bd9Sstevel@tonic-gate break;
26007c478bd9Sstevel@tonic-gate default:
26017c478bd9Sstevel@tonic-gate error = EINVAL;
26027c478bd9Sstevel@tonic-gate goto done;
26037c478bd9Sstevel@tonic-gate }
26047c478bd9Sstevel@tonic-gate
26057c478bd9Sstevel@tonic-gate /*
26067c478bd9Sstevel@tonic-gate * Assumes that the SS_CANT* flags are never cleared in the above code.
26077c478bd9Sstevel@tonic-gate */
26087c478bd9Sstevel@tonic-gate state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
2609fc80c0dfSnordmark (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
26107c478bd9Sstevel@tonic-gate ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
26117c478bd9Sstevel@tonic-gate
26127c478bd9Sstevel@tonic-gate switch (state_change) {
26137c478bd9Sstevel@tonic-gate case 0:
26147c478bd9Sstevel@tonic-gate dprintso(so, 1,
26157c478bd9Sstevel@tonic-gate ("sotpi_shutdown: nothing to send in state 0x%x\n",
26167c478bd9Sstevel@tonic-gate so->so_state));
26177c478bd9Sstevel@tonic-gate goto done;
26187c478bd9Sstevel@tonic-gate
26197c478bd9Sstevel@tonic-gate case SS_CANTRCVMORE:
26207c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26217c478bd9Sstevel@tonic-gate strseteof(SOTOV(so), 1);
26227c478bd9Sstevel@tonic-gate /*
26237c478bd9Sstevel@tonic-gate * strseteof takes care of read side wakeups,
26247c478bd9Sstevel@tonic-gate * pollwakeups, and signals.
26257c478bd9Sstevel@tonic-gate */
26267c478bd9Sstevel@tonic-gate /*
26277c478bd9Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems
26287c478bd9Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
26297c478bd9Sstevel@tonic-gate */
26307c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26317c478bd9Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */
26327c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26337c478bd9Sstevel@tonic-gate
26347c478bd9Sstevel@tonic-gate /* Flush read side queue */
26357c478bd9Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL);
26367c478bd9Sstevel@tonic-gate
26377c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26387c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
26397c478bd9Sstevel@tonic-gate break;
26407c478bd9Sstevel@tonic-gate
26417c478bd9Sstevel@tonic-gate case SS_CANTSENDMORE:
26427c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26437c478bd9Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
26447c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26457c478bd9Sstevel@tonic-gate break;
26467c478bd9Sstevel@tonic-gate
26477c478bd9Sstevel@tonic-gate case SS_CANTSENDMORE|SS_CANTRCVMORE:
26487c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26497c478bd9Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
26507c478bd9Sstevel@tonic-gate strseteof(SOTOV(so), 1);
26517c478bd9Sstevel@tonic-gate /*
26527c478bd9Sstevel@tonic-gate * strseteof takes care of read side wakeups,
26537c478bd9Sstevel@tonic-gate * pollwakeups, and signals.
26547c478bd9Sstevel@tonic-gate */
26557c478bd9Sstevel@tonic-gate /*
26567c478bd9Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems
26577c478bd9Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
26587c478bd9Sstevel@tonic-gate */
26597c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26607c478bd9Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */
26617c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26627c478bd9Sstevel@tonic-gate
26637c478bd9Sstevel@tonic-gate /* Flush read side queue */
26647c478bd9Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL);
26657c478bd9Sstevel@tonic-gate
26667c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26677c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
26687c478bd9Sstevel@tonic-gate break;
26697c478bd9Sstevel@tonic-gate }
26707c478bd9Sstevel@tonic-gate
26717c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
26727c478bd9Sstevel@tonic-gate
26737c478bd9Sstevel@tonic-gate /*
26747c478bd9Sstevel@tonic-gate * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
26757c478bd9Sstevel@tonic-gate * was set due to this call and the new state has both of them set:
26767c478bd9Sstevel@tonic-gate * Send the AF_UNIX close indication
26777c478bd9Sstevel@tonic-gate * For T_COTS send a discon_ind
26787c478bd9Sstevel@tonic-gate *
26797c478bd9Sstevel@tonic-gate * If cantsend was set due to this call:
26807c478bd9Sstevel@tonic-gate * For T_COTSORD send an ordrel_ind
26817c478bd9Sstevel@tonic-gate *
26827c478bd9Sstevel@tonic-gate * Note that for T_CLTS there is no message sent here.
26837c478bd9Sstevel@tonic-gate */
26847c478bd9Sstevel@tonic-gate if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
26857c478bd9Sstevel@tonic-gate (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
26867c478bd9Sstevel@tonic-gate /*
26877c478bd9Sstevel@tonic-gate * For SunOS 4.X compatibility we tell the other end
26887c478bd9Sstevel@tonic-gate * that we are unable to receive at this point.
26897c478bd9Sstevel@tonic-gate */
26900f1702c5SYu Xiangning if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS)
26917c478bd9Sstevel@tonic-gate so_unix_close(so);
26927c478bd9Sstevel@tonic-gate
26930f1702c5SYu Xiangning if (sti->sti_serv_type == T_COTS)
26947c478bd9Sstevel@tonic-gate error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
26957c478bd9Sstevel@tonic-gate }
26967c478bd9Sstevel@tonic-gate if ((state_change & SS_CANTSENDMORE) &&
26970f1702c5SYu Xiangning (sti->sti_serv_type == T_COTS_ORD)) {
26987c478bd9Sstevel@tonic-gate /* Send an orderly release */
26997c478bd9Sstevel@tonic-gate ordrel_req.PRIM_type = T_ORDREL_REQ;
27007c478bd9Sstevel@tonic-gate
27017c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
27027c478bd9Sstevel@tonic-gate mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
2703de8c4a14SErik Nordmark 0, _ALLOC_SLEEP, cr);
27047c478bd9Sstevel@tonic-gate /*
27057c478bd9Sstevel@tonic-gate * Send down the T_ORDREL_REQ even if there is flow control.
27067c478bd9Sstevel@tonic-gate * This prevents shutdown from blocking.
27077c478bd9Sstevel@tonic-gate * Note that there is no T_OK_ACK for ordrel_req.
27087c478bd9Sstevel@tonic-gate */
27097c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2710fc80c0dfSnordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
27117c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
27127c478bd9Sstevel@tonic-gate if (error) {
27137c478bd9Sstevel@tonic-gate eprintsoline(so, error);
27147c478bd9Sstevel@tonic-gate goto done;
27157c478bd9Sstevel@tonic-gate }
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate
27187c478bd9Sstevel@tonic-gate done:
27197c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
27207c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
27217c478bd9Sstevel@tonic-gate return (error);
27227c478bd9Sstevel@tonic-gate }
27237c478bd9Sstevel@tonic-gate
27247c478bd9Sstevel@tonic-gate /*
27257c478bd9Sstevel@tonic-gate * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
27267c478bd9Sstevel@tonic-gate * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
27277c478bd9Sstevel@tonic-gate * that we have closed.
27287c478bd9Sstevel@tonic-gate * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
27297c478bd9Sstevel@tonic-gate * T_UNITDATA_REQ containing the same option.
27307c478bd9Sstevel@tonic-gate *
27317c478bd9Sstevel@tonic-gate * For SOCK_DGRAM half-connections (somebody connected to this end
27327c478bd9Sstevel@tonic-gate * but this end is not connect) we don't know where to send any
27337c478bd9Sstevel@tonic-gate * SO_UNIX_CLOSE.
27347c478bd9Sstevel@tonic-gate *
27357c478bd9Sstevel@tonic-gate * We have to ignore stream head errors just in case there has been
27367c478bd9Sstevel@tonic-gate * a shutdown(output).
27377c478bd9Sstevel@tonic-gate * Ignore any flow control to try to get the message more quickly to the peer.
27387c478bd9Sstevel@tonic-gate * While locally ignoring flow control solves the problem when there
27397c478bd9Sstevel@tonic-gate * is only the loopback transport on the stream it would not provide
27407c478bd9Sstevel@tonic-gate * the correct AF_UNIX socket semantics when one or more modules have
27417c478bd9Sstevel@tonic-gate * been pushed.
27427c478bd9Sstevel@tonic-gate */
27437c478bd9Sstevel@tonic-gate void
so_unix_close(struct sonode * so)27447c478bd9Sstevel@tonic-gate so_unix_close(struct sonode *so)
27457c478bd9Sstevel@tonic-gate {
27467c478bd9Sstevel@tonic-gate struct T_opthdr toh;
27477c478bd9Sstevel@tonic-gate mblk_t *mp;
27480f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
27497c478bd9Sstevel@tonic-gate
27507c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
27517c478bd9Sstevel@tonic-gate
27527c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX);
27537c478bd9Sstevel@tonic-gate
27547c478bd9Sstevel@tonic-gate if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
27557c478bd9Sstevel@tonic-gate (SS_ISCONNECTED|SS_ISBOUND))
27567c478bd9Sstevel@tonic-gate return;
27577c478bd9Sstevel@tonic-gate
27587c478bd9Sstevel@tonic-gate dprintso(so, 1, ("so_unix_close(%p) %s\n",
2759903a11ebSrh (void *)so, pr_state(so->so_state, so->so_mode)));
27607c478bd9Sstevel@tonic-gate
27617c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
27627c478bd9Sstevel@tonic-gate toh.name = SO_UNIX_CLOSE;
27637c478bd9Sstevel@tonic-gate
27647c478bd9Sstevel@tonic-gate /* zero length + header */
27657c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
27667c478bd9Sstevel@tonic-gate toh.status = 0;
27677c478bd9Sstevel@tonic-gate
27687c478bd9Sstevel@tonic-gate if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
27697c478bd9Sstevel@tonic-gate struct T_optdata_req tdr;
27707c478bd9Sstevel@tonic-gate
27717c478bd9Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ;
27727c478bd9Sstevel@tonic-gate tdr.DATA_flag = 0;
27737c478bd9Sstevel@tonic-gate
27747c478bd9Sstevel@tonic-gate tdr.OPT_length = (t_scalar_t)sizeof (toh);
27757c478bd9Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
27767c478bd9Sstevel@tonic-gate
27777c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
27787c478bd9Sstevel@tonic-gate mp = soallocproto2(&tdr, sizeof (tdr),
2779de8c4a14SErik Nordmark &toh, sizeof (toh), 0, _ALLOC_SLEEP, CRED());
27807c478bd9Sstevel@tonic-gate } else {
27817c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
27827c478bd9Sstevel@tonic-gate void *addr;
27837c478bd9Sstevel@tonic-gate socklen_t addrlen;
27847c478bd9Sstevel@tonic-gate void *src;
27857c478bd9Sstevel@tonic-gate socklen_t srclen;
27867c478bd9Sstevel@tonic-gate struct T_opthdr toh2;
27877c478bd9Sstevel@tonic-gate t_scalar_t size;
27887c478bd9Sstevel@tonic-gate
27897c478bd9Sstevel@tonic-gate /*
2790f012ee0cSGordon Ross * We know this is an AF_UNIX connected DGRAM socket.
2791f012ee0cSGordon Ross * We therefore already have the destination address
2792f012ee0cSGordon Ross * in the internal form needed for this send. This is
2793f012ee0cSGordon Ross * similar to the sosend_dgram call later in this file
2794f012ee0cSGordon Ross * when there's no user-specified destination address.
27957c478bd9Sstevel@tonic-gate */
27960f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
27977c478bd9Sstevel@tonic-gate /*
27987c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
27997c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
28007c478bd9Sstevel@tonic-gate */
28010f1702c5SYu Xiangning addr = sti->sti_faddr_sa;
28020f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_len;
28037c478bd9Sstevel@tonic-gate src = NULL;
28047c478bd9Sstevel@tonic-gate srclen = 0;
28057c478bd9Sstevel@tonic-gate } else {
28067c478bd9Sstevel@tonic-gate /*
28077c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
28087c478bd9Sstevel@tonic-gate * and translate the remote address.
28090f1702c5SYu Xiangning * Holding so_lock thus sti_laddr_sa can not change.
28107c478bd9Sstevel@tonic-gate */
28110f1702c5SYu Xiangning src = sti->sti_laddr_sa;
28120f1702c5SYu Xiangning srclen = (socklen_t)sti->sti_laddr_len;
28137c478bd9Sstevel@tonic-gate dprintso(so, 1,
2814fc80c0dfSnordmark ("so_ux_close: srclen %d, src %p\n",
2815fc80c0dfSnordmark srclen, src));
2816f012ee0cSGordon Ross /*
2817f012ee0cSGordon Ross * Use the destination address saved in connect.
2818f012ee0cSGordon Ross */
2819f012ee0cSGordon Ross addr = &sti->sti_ux_faddr;
2820f012ee0cSGordon Ross addrlen = sizeof (sti->sti_ux_faddr);
28217c478bd9Sstevel@tonic-gate }
28227c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
28237c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
28247c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
28257c478bd9Sstevel@tonic-gate if (srclen == 0) {
28267c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)sizeof (toh);
28277c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
2828fc80c0dfSnordmark _TPI_ALIGN_TOPT(addrlen));
28297c478bd9Sstevel@tonic-gate
28307c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
28317c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
28327c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
2833de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_SLEEP, CRED());
28347c478bd9Sstevel@tonic-gate mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
28357c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
28367c478bd9Sstevel@tonic-gate } else {
28377c478bd9Sstevel@tonic-gate /*
28387c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a
28397c478bd9Sstevel@tonic-gate * source address option.
28407c478bd9Sstevel@tonic-gate */
28417c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
28427c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen));
28437c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
28447c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
28457c478bd9Sstevel@tonic-gate
28467c478bd9Sstevel@tonic-gate toh2.level = SOL_SOCKET;
28477c478bd9Sstevel@tonic-gate toh2.name = SO_SRCADDR;
28487c478bd9Sstevel@tonic-gate toh2.len = (t_uscalar_t)(srclen +
2849fc80c0dfSnordmark sizeof (struct T_opthdr));
28507c478bd9Sstevel@tonic-gate toh2.status = 0;
28517c478bd9Sstevel@tonic-gate
28527c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
28537c478bd9Sstevel@tonic-gate
28547c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
28557c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
2856de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_SLEEP, CRED());
28577c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
28587c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
28597c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh2, sizeof (toh2));
28607c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
28617c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
28627c478bd9Sstevel@tonic-gate }
28637c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
28647c478bd9Sstevel@tonic-gate }
28657c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
2866f012ee0cSGordon Ross (void) kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2867fc80c0dfSnordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
28687c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
28697c478bd9Sstevel@tonic-gate }
28707c478bd9Sstevel@tonic-gate
28717c478bd9Sstevel@tonic-gate /*
28727c478bd9Sstevel@tonic-gate * Called by sotpi_recvmsg when reading a non-zero amount of data.
28737c478bd9Sstevel@tonic-gate * In addition, the caller typically verifies that there is some
28747c478bd9Sstevel@tonic-gate * potential state to clear by checking
28757c478bd9Sstevel@tonic-gate * if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
28767c478bd9Sstevel@tonic-gate * before calling this routine.
28777c478bd9Sstevel@tonic-gate * Note that such a check can be made without holding so_lock since
28787c478bd9Sstevel@tonic-gate * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
28790f1702c5SYu Xiangning * decrements sti_oobsigcnt.
28807c478bd9Sstevel@tonic-gate *
28817c478bd9Sstevel@tonic-gate * When data is read *after* the point that all pending
28827c478bd9Sstevel@tonic-gate * oob data has been consumed the oob indication is cleared.
28837c478bd9Sstevel@tonic-gate *
28847c478bd9Sstevel@tonic-gate * This logic keeps select/poll returning POLLRDBAND and
28857c478bd9Sstevel@tonic-gate * SIOCATMARK returning true until we have read past
28867c478bd9Sstevel@tonic-gate * the mark.
28877c478bd9Sstevel@tonic-gate */
28887c478bd9Sstevel@tonic-gate static void
sorecv_update_oobstate(struct sonode * so)28897c478bd9Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so)
28907c478bd9Sstevel@tonic-gate {
28910f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
28920f1702c5SYu Xiangning
28937c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
28947c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
28957c478bd9Sstevel@tonic-gate dprintso(so, 1,
2896fc80c0dfSnordmark ("sorecv_update_oobstate: counts %d/%d state %s\n",
28970f1702c5SYu Xiangning sti->sti_oobsigcnt,
28980f1702c5SYu Xiangning sti->sti_oobcnt, pr_state(so->so_state, so->so_mode)));
28990f1702c5SYu Xiangning if (sti->sti_oobsigcnt == 0) {
29007c478bd9Sstevel@tonic-gate /* No more pending oob indications */
29017c478bd9Sstevel@tonic-gate so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
29027c478bd9Sstevel@tonic-gate freemsg(so->so_oobmsg);
29037c478bd9Sstevel@tonic-gate so->so_oobmsg = NULL;
29047c478bd9Sstevel@tonic-gate }
29057c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
29067c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate
29097c478bd9Sstevel@tonic-gate /*
29107c478bd9Sstevel@tonic-gate * Receive the next message on the queue.
29117c478bd9Sstevel@tonic-gate * If msg_controllen is non-zero when called the caller is interested in
29127c478bd9Sstevel@tonic-gate * any received control info (options).
29137c478bd9Sstevel@tonic-gate * If msg_namelen is non-zero when called the caller is interested in
29147c478bd9Sstevel@tonic-gate * any received source address.
29157c478bd9Sstevel@tonic-gate * The routine returns with msg_control and msg_name pointing to
29167c478bd9Sstevel@tonic-gate * kmem_alloc'ed memory which the caller has to free.
29177c478bd9Sstevel@tonic-gate */
29180f1702c5SYu Xiangning /* ARGSUSED */
29197c478bd9Sstevel@tonic-gate int
sotpi_recvmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)29200f1702c5SYu Xiangning sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
29210f1702c5SYu Xiangning struct cred *cr)
29227c478bd9Sstevel@tonic-gate {
29237c478bd9Sstevel@tonic-gate union T_primitives *tpr;
29247c478bd9Sstevel@tonic-gate mblk_t *mp;
29257c478bd9Sstevel@tonic-gate uchar_t pri;
29267c478bd9Sstevel@tonic-gate int pflag, opflag;
29277c478bd9Sstevel@tonic-gate void *control;
29287c478bd9Sstevel@tonic-gate t_uscalar_t controllen;
29297c478bd9Sstevel@tonic-gate t_uscalar_t namelen;
29307c478bd9Sstevel@tonic-gate int so_state = so->so_state; /* Snapshot */
29317c478bd9Sstevel@tonic-gate ssize_t saved_resid;
29327c478bd9Sstevel@tonic-gate rval_t rval;
29337c478bd9Sstevel@tonic-gate int flags;
29347c478bd9Sstevel@tonic-gate clock_t timout;
293517169044Sbrutus int error = 0;
29360f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
29377c478bd9Sstevel@tonic-gate
29387c478bd9Sstevel@tonic-gate flags = msg->msg_flags;
29397c478bd9Sstevel@tonic-gate msg->msg_flags = 0;
29407c478bd9Sstevel@tonic-gate
29417c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
2942903a11ebSrh (void *)so, (void *)msg, flags,
2943fc80c0dfSnordmark pr_state(so->so_state, so->so_mode), so->so_error));
29447c478bd9Sstevel@tonic-gate
29450f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
29460f1702c5SYu Xiangning so_update_attrs(so, SOACC);
29470f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
29480f1702c5SYu Xiangning return (strread(SOTOV(so), uiop, cr));
29490f1702c5SYu Xiangning }
29500f1702c5SYu Xiangning
29517c478bd9Sstevel@tonic-gate /*
29527c478bd9Sstevel@tonic-gate * If we are not connected because we have never been connected
29537c478bd9Sstevel@tonic-gate * we return ENOTCONN. If we have been connected (but are no longer
29547c478bd9Sstevel@tonic-gate * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
29557c478bd9Sstevel@tonic-gate * the EOF.
29567c478bd9Sstevel@tonic-gate *
29577c478bd9Sstevel@tonic-gate * An alternative would be to post an ENOTCONN error in stream head
29587c478bd9Sstevel@tonic-gate * (read+write) and clear it when we're connected. However, that error
29597c478bd9Sstevel@tonic-gate * would cause incorrect poll/select behavior!
29607c478bd9Sstevel@tonic-gate */
29617c478bd9Sstevel@tonic-gate if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
29627c478bd9Sstevel@tonic-gate (so->so_mode & SM_CONNREQUIRED)) {
29637c478bd9Sstevel@tonic-gate return (ENOTCONN);
29647c478bd9Sstevel@tonic-gate }
29657c478bd9Sstevel@tonic-gate
29667c478bd9Sstevel@tonic-gate /*
29677c478bd9Sstevel@tonic-gate * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
29687c478bd9Sstevel@tonic-gate * after checking that the read queue is empty) and returns zero.
29697c478bd9Sstevel@tonic-gate * This implementation will sleep (in kstrgetmsg) even if uio_resid
29707c478bd9Sstevel@tonic-gate * is zero.
29717c478bd9Sstevel@tonic-gate */
29727c478bd9Sstevel@tonic-gate
29737c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
29747c478bd9Sstevel@tonic-gate /* Check that the transport supports OOB */
29757c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_EXDATA))
29767c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
29770f1702c5SYu Xiangning so_update_attrs(so, SOACC);
29780f1702c5SYu Xiangning return (sorecvoob(so, msg, uiop, flags,
29790f1702c5SYu Xiangning (so->so_options & SO_OOBINLINE)));
29807c478bd9Sstevel@tonic-gate }
29817c478bd9Sstevel@tonic-gate
29820f1702c5SYu Xiangning so_update_attrs(so, SOACC);
29830f1702c5SYu Xiangning
29847c478bd9Sstevel@tonic-gate /*
29857c478bd9Sstevel@tonic-gate * Set msg_controllen and msg_namelen to zero here to make it
29867c478bd9Sstevel@tonic-gate * simpler in the cases that no control or name is returned.
29877c478bd9Sstevel@tonic-gate */
29887c478bd9Sstevel@tonic-gate controllen = msg->msg_controllen;
29897c478bd9Sstevel@tonic-gate namelen = msg->msg_namelen;
29907c478bd9Sstevel@tonic-gate msg->msg_controllen = 0;
29917c478bd9Sstevel@tonic-gate msg->msg_namelen = 0;
29927c478bd9Sstevel@tonic-gate
29937c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
2994fc80c0dfSnordmark namelen, controllen));
29957c478bd9Sstevel@tonic-gate
29962c9e429eSbrutus mutex_enter(&so->so_lock);
29977c478bd9Sstevel@tonic-gate /*
29987c478bd9Sstevel@tonic-gate * Only one reader is allowed at any given time. This is needed
29997c478bd9Sstevel@tonic-gate * for T_EXDATA handling and, in the future, MSG_WAITALL.
30007c478bd9Sstevel@tonic-gate *
30017c478bd9Sstevel@tonic-gate * This is slightly different that BSD behavior in that it fails with
30027c478bd9Sstevel@tonic-gate * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
30037c478bd9Sstevel@tonic-gate * is single-threaded using sblock(), which is dropped while waiting
30047c478bd9Sstevel@tonic-gate * for data to appear. The difference shows up e.g. if one
30057c478bd9Sstevel@tonic-gate * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
30067c478bd9Sstevel@tonic-gate * does use nonblocking io and different threads are reading each
30077c478bd9Sstevel@tonic-gate * file descriptor. In BSD there would never be an EWOULDBLOCK error
30087c478bd9Sstevel@tonic-gate * in this case as long as the read queue doesn't get empty.
30097c478bd9Sstevel@tonic-gate * In this implementation the thread using nonblocking io can
30107c478bd9Sstevel@tonic-gate * get an EWOULDBLOCK error due to the blocking thread executing
30117c478bd9Sstevel@tonic-gate * e.g. in the uiomove in kstrgetmsg.
30127c478bd9Sstevel@tonic-gate * This difference is not believed to be significant.
30137c478bd9Sstevel@tonic-gate */
3014255daac4Sethindra /* Set SOREADLOCKED */
3015255daac4Sethindra error = so_lock_read_intr(so,
3016255daac4Sethindra uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
30177c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
30187c478bd9Sstevel@tonic-gate if (error)
30197c478bd9Sstevel@tonic-gate return (error);
30207c478bd9Sstevel@tonic-gate
30217c478bd9Sstevel@tonic-gate /*
30227c478bd9Sstevel@tonic-gate * Tell kstrgetmsg to not inspect the stream head errors until all
30237c478bd9Sstevel@tonic-gate * queued data has been consumed.
30247c478bd9Sstevel@tonic-gate * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
30257c478bd9Sstevel@tonic-gate * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
30267c478bd9Sstevel@tonic-gate *
30277c478bd9Sstevel@tonic-gate * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
30287c478bd9Sstevel@tonic-gate * to T_OPTDATA_IND that do not contain any user-visible control msg.
30297c478bd9Sstevel@tonic-gate * Note that MSG_WAITALL set with MSG_PEEK is a noop.
30307c478bd9Sstevel@tonic-gate */
30317c478bd9Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR;
30327c478bd9Sstevel@tonic-gate if (flags & MSG_PEEK) {
30337c478bd9Sstevel@tonic-gate pflag |= MSG_IPEEK;
30347c478bd9Sstevel@tonic-gate flags &= ~MSG_WAITALL;
30357c478bd9Sstevel@tonic-gate }
30367c478bd9Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC)
30377c478bd9Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL;
30387c478bd9Sstevel@tonic-gate
30397c478bd9Sstevel@tonic-gate if (flags & MSG_DONTWAIT)
30407c478bd9Sstevel@tonic-gate timout = 0;
3041412cc9e9SGordon Ross else if (so->so_rcvtimeo != 0)
3042412cc9e9SGordon Ross timout = TICK_TO_MSEC(so->so_rcvtimeo);
30437c478bd9Sstevel@tonic-gate else
30447c478bd9Sstevel@tonic-gate timout = -1;
30457c478bd9Sstevel@tonic-gate opflag = pflag;
30467c478bd9Sstevel@tonic-gate retry:
30477c478bd9Sstevel@tonic-gate saved_resid = uiop->uio_resid;
30487c478bd9Sstevel@tonic-gate pri = 0;
30497c478bd9Sstevel@tonic-gate mp = NULL;
305015f90b02SGarrett D'Amore error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
305115f90b02SGarrett D'Amore timout, &rval);
30520f1702c5SYu Xiangning if (error != 0) {
30530f1702c5SYu Xiangning /* kstrgetmsg returns ETIME when timeout expires */
30540f1702c5SYu Xiangning if (error == ETIME)
30550f1702c5SYu Xiangning error = EWOULDBLOCK;
305617169044Sbrutus goto out;
30577c478bd9Sstevel@tonic-gate }
30587c478bd9Sstevel@tonic-gate /*
30597c478bd9Sstevel@tonic-gate * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
30607c478bd9Sstevel@tonic-gate * For non-datagrams MOREDATA is used to set MSG_EOR.
30617c478bd9Sstevel@tonic-gate */
30627c478bd9Sstevel@tonic-gate ASSERT(!(rval.r_val1 & MORECTL));
30637c478bd9Sstevel@tonic-gate if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
30647c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_TRUNC;
30657c478bd9Sstevel@tonic-gate
30667c478bd9Sstevel@tonic-gate if (mp == NULL) {
30677c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
30687c478bd9Sstevel@tonic-gate /*
30697c478bd9Sstevel@tonic-gate * 4.3BSD and 4.4BSD clears the mark when peeking across it.
30707c478bd9Sstevel@tonic-gate * The draft Posix socket spec states that the mark should
30717c478bd9Sstevel@tonic-gate * not be cleared when peeking. We follow the latter.
30727c478bd9Sstevel@tonic-gate */
30737c478bd9Sstevel@tonic-gate if ((so->so_state &
30747c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
30757c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
30767c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
30777c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
30787c478bd9Sstevel@tonic-gate }
30797c478bd9Sstevel@tonic-gate
30807c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
30817c478bd9Sstevel@tonic-gate /* Set MSG_EOR based on MOREDATA */
30827c478bd9Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) {
30837c478bd9Sstevel@tonic-gate if (so->so_state & SS_SAVEDEOR) {
30847c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_EOR;
30857c478bd9Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR;
30867c478bd9Sstevel@tonic-gate }
30877c478bd9Sstevel@tonic-gate }
30887c478bd9Sstevel@tonic-gate /*
30897c478bd9Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the
30907c478bd9Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more.
30917c478bd9Sstevel@tonic-gate */
30927c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
30937c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
30947c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
30957c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
30967c478bd9Sstevel@tonic-gate goto retry;
30977c478bd9Sstevel@tonic-gate }
309817169044Sbrutus goto out_locked;
30997c478bd9Sstevel@tonic-gate }
31007c478bd9Sstevel@tonic-gate
31017c478bd9Sstevel@tonic-gate /* strsock_proto has already verified length and alignment */
31027c478bd9Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr;
31037c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
31047c478bd9Sstevel@tonic-gate
31057c478bd9Sstevel@tonic-gate switch (tpr->type) {
31067c478bd9Sstevel@tonic-gate case T_DATA_IND: {
31077c478bd9Sstevel@tonic-gate if ((so->so_state &
31087c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
31097c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
31107c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
31117c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
31127c478bd9Sstevel@tonic-gate }
31137c478bd9Sstevel@tonic-gate
31147c478bd9Sstevel@tonic-gate /*
31157c478bd9Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on
31167c478bd9Sstevel@tonic-gate * MORE_flag and MOREDATA.
31177c478bd9Sstevel@tonic-gate */
31187c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
31197c478bd9Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR;
31207c478bd9Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) {
31217c478bd9Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA))
31227c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_EOR;
31237c478bd9Sstevel@tonic-gate else
31247c478bd9Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR;
31257c478bd9Sstevel@tonic-gate }
31267c478bd9Sstevel@tonic-gate freemsg(mp);
31277c478bd9Sstevel@tonic-gate /*
31287c478bd9Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the
31297c478bd9Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more.
31307c478bd9Sstevel@tonic-gate */
31317c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
31327c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
31337c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
31347c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
31357c478bd9Sstevel@tonic-gate goto retry;
31367c478bd9Sstevel@tonic-gate }
313717169044Sbrutus goto out_locked;
31387c478bd9Sstevel@tonic-gate }
31397c478bd9Sstevel@tonic-gate case T_UNITDATA_IND: {
31407c478bd9Sstevel@tonic-gate void *addr;
31417c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
31427c478bd9Sstevel@tonic-gate void *abuf;
31437c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
31447c478bd9Sstevel@tonic-gate void *opt;
31457c478bd9Sstevel@tonic-gate
31467c478bd9Sstevel@tonic-gate if ((so->so_state &
31477c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
31487c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
31497c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
31507c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
31517c478bd9Sstevel@tonic-gate }
31527c478bd9Sstevel@tonic-gate
31537c478bd9Sstevel@tonic-gate if (namelen != 0) {
31547c478bd9Sstevel@tonic-gate /* Caller wants source address */
31557c478bd9Sstevel@tonic-gate addrlen = tpr->unitdata_ind.SRC_length;
31567c478bd9Sstevel@tonic-gate addr = sogetoff(mp,
3157fc80c0dfSnordmark tpr->unitdata_ind.SRC_offset,
3158fc80c0dfSnordmark addrlen, 1);
31597c478bd9Sstevel@tonic-gate if (addr == NULL) {
31607c478bd9Sstevel@tonic-gate freemsg(mp);
31617c478bd9Sstevel@tonic-gate error = EPROTO;
31627c478bd9Sstevel@tonic-gate eprintsoline(so, error);
316317169044Sbrutus goto out;
31647c478bd9Sstevel@tonic-gate }
31657c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
31667c478bd9Sstevel@tonic-gate /*
31677c478bd9Sstevel@tonic-gate * Can not use the transport level address.
31687c478bd9Sstevel@tonic-gate * If there is a SO_SRCADDR option carrying
31697c478bd9Sstevel@tonic-gate * the socket level address it will be
31707c478bd9Sstevel@tonic-gate * extracted below.
31717c478bd9Sstevel@tonic-gate */
31727c478bd9Sstevel@tonic-gate addr = NULL;
31737c478bd9Sstevel@tonic-gate addrlen = 0;
31747c478bd9Sstevel@tonic-gate }
31757c478bd9Sstevel@tonic-gate }
31767c478bd9Sstevel@tonic-gate optlen = tpr->unitdata_ind.OPT_length;
31777c478bd9Sstevel@tonic-gate if (optlen != 0) {
31787c478bd9Sstevel@tonic-gate t_uscalar_t ncontrollen;
31797c478bd9Sstevel@tonic-gate
31807c478bd9Sstevel@tonic-gate /*
31817c478bd9Sstevel@tonic-gate * Extract any source address option.
31827c478bd9Sstevel@tonic-gate * Determine how large cmsg buffer is needed.
31837c478bd9Sstevel@tonic-gate */
31847c478bd9Sstevel@tonic-gate opt = sogetoff(mp,
3185fc80c0dfSnordmark tpr->unitdata_ind.OPT_offset,
3186fc80c0dfSnordmark optlen, __TPI_ALIGN_SIZE);
31877c478bd9Sstevel@tonic-gate
31887c478bd9Sstevel@tonic-gate if (opt == NULL) {
31897c478bd9Sstevel@tonic-gate freemsg(mp);
31907c478bd9Sstevel@tonic-gate error = EPROTO;
31917c478bd9Sstevel@tonic-gate eprintsoline(so, error);
319217169044Sbrutus goto out;
31937c478bd9Sstevel@tonic-gate }
31947c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX)
31957c478bd9Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
31967c478bd9Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen,
3197fc80c0dfSnordmark !(flags & MSG_XPG4_2));
31987c478bd9Sstevel@tonic-gate if (controllen != 0)
31997c478bd9Sstevel@tonic-gate controllen = ncontrollen;
32007c478bd9Sstevel@tonic-gate else if (ncontrollen != 0)
32017c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC;
32027c478bd9Sstevel@tonic-gate } else {
32037c478bd9Sstevel@tonic-gate controllen = 0;
32047c478bd9Sstevel@tonic-gate }
32057c478bd9Sstevel@tonic-gate
32067c478bd9Sstevel@tonic-gate if (namelen != 0) {
32077c478bd9Sstevel@tonic-gate /*
32087c478bd9Sstevel@tonic-gate * Return address to caller.
32097c478bd9Sstevel@tonic-gate * Caller handles truncation if length
32107c478bd9Sstevel@tonic-gate * exceeds msg_namelen.
32117c478bd9Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by
32127c478bd9Sstevel@tonic-gate * the sender's copyin_name().
32137c478bd9Sstevel@tonic-gate */
32147c478bd9Sstevel@tonic-gate abuf = kmem_alloc(addrlen, KM_SLEEP);
32157c478bd9Sstevel@tonic-gate
32167c478bd9Sstevel@tonic-gate bcopy(addr, abuf, addrlen);
32177c478bd9Sstevel@tonic-gate msg->msg_name = abuf;
32187c478bd9Sstevel@tonic-gate msg->msg_namelen = addrlen;
32197c478bd9Sstevel@tonic-gate }
32207c478bd9Sstevel@tonic-gate
32217c478bd9Sstevel@tonic-gate if (controllen != 0) {
32227c478bd9Sstevel@tonic-gate /*
32237c478bd9Sstevel@tonic-gate * Return control msg to caller.
32247c478bd9Sstevel@tonic-gate * Caller handles truncation if length
32257c478bd9Sstevel@tonic-gate * exceeds msg_controllen.
32267c478bd9Sstevel@tonic-gate */
3227274af231Samehta control = kmem_zalloc(controllen, KM_SLEEP);
32287c478bd9Sstevel@tonic-gate
32297c478bd9Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen,
3230fc80c0dfSnordmark !(flags & MSG_XPG4_2),
3231fc80c0dfSnordmark control, controllen);
32327c478bd9Sstevel@tonic-gate if (error) {
32337c478bd9Sstevel@tonic-gate freemsg(mp);
32347c478bd9Sstevel@tonic-gate if (msg->msg_namelen != 0)
32357c478bd9Sstevel@tonic-gate kmem_free(msg->msg_name,
3236fc80c0dfSnordmark msg->msg_namelen);
32377c478bd9Sstevel@tonic-gate kmem_free(control, controllen);
32387c478bd9Sstevel@tonic-gate eprintsoline(so, error);
323917169044Sbrutus goto out;
32407c478bd9Sstevel@tonic-gate }
32417c478bd9Sstevel@tonic-gate msg->msg_control = control;
32427c478bd9Sstevel@tonic-gate msg->msg_controllen = controllen;
32437c478bd9Sstevel@tonic-gate }
32447c478bd9Sstevel@tonic-gate
32457c478bd9Sstevel@tonic-gate freemsg(mp);
324617169044Sbrutus goto out;
32477c478bd9Sstevel@tonic-gate }
32487c478bd9Sstevel@tonic-gate case T_OPTDATA_IND: {
32497c478bd9Sstevel@tonic-gate struct T_optdata_req *tdr;
32507c478bd9Sstevel@tonic-gate void *opt;
32517c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
32527c478bd9Sstevel@tonic-gate
32537c478bd9Sstevel@tonic-gate if ((so->so_state &
32547c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32557c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
32567c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
32577c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
32587c478bd9Sstevel@tonic-gate }
32597c478bd9Sstevel@tonic-gate
32607c478bd9Sstevel@tonic-gate tdr = (struct T_optdata_req *)mp->b_rptr;
32617c478bd9Sstevel@tonic-gate optlen = tdr->OPT_length;
32627c478bd9Sstevel@tonic-gate if (optlen != 0) {
32637c478bd9Sstevel@tonic-gate t_uscalar_t ncontrollen;
32647c478bd9Sstevel@tonic-gate /*
32657c478bd9Sstevel@tonic-gate * Determine how large cmsg buffer is needed.
32667c478bd9Sstevel@tonic-gate */
32677c478bd9Sstevel@tonic-gate opt = sogetoff(mp,
3268fc80c0dfSnordmark tpr->optdata_ind.OPT_offset,
3269fc80c0dfSnordmark optlen, __TPI_ALIGN_SIZE);
32707c478bd9Sstevel@tonic-gate
32717c478bd9Sstevel@tonic-gate if (opt == NULL) {
32727c478bd9Sstevel@tonic-gate freemsg(mp);
32737c478bd9Sstevel@tonic-gate error = EPROTO;
32747c478bd9Sstevel@tonic-gate eprintsoline(so, error);
327517169044Sbrutus goto out;
32767c478bd9Sstevel@tonic-gate }
32777c478bd9Sstevel@tonic-gate
32787c478bd9Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen,
3279fc80c0dfSnordmark !(flags & MSG_XPG4_2));
32807c478bd9Sstevel@tonic-gate if (controllen != 0)
32817c478bd9Sstevel@tonic-gate controllen = ncontrollen;
32827c478bd9Sstevel@tonic-gate else if (ncontrollen != 0)
32837c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC;
32847c478bd9Sstevel@tonic-gate } else {
32857c478bd9Sstevel@tonic-gate controllen = 0;
32867c478bd9Sstevel@tonic-gate }
32877c478bd9Sstevel@tonic-gate
32887c478bd9Sstevel@tonic-gate if (controllen != 0) {
32897c478bd9Sstevel@tonic-gate /*
32907c478bd9Sstevel@tonic-gate * Return control msg to caller.
32917c478bd9Sstevel@tonic-gate * Caller handles truncation if length
32927c478bd9Sstevel@tonic-gate * exceeds msg_controllen.
32937c478bd9Sstevel@tonic-gate */
3294274af231Samehta control = kmem_zalloc(controllen, KM_SLEEP);
32957c478bd9Sstevel@tonic-gate
32967c478bd9Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen,
3297fc80c0dfSnordmark !(flags & MSG_XPG4_2),
3298fc80c0dfSnordmark control, controllen);
32997c478bd9Sstevel@tonic-gate if (error) {
33007c478bd9Sstevel@tonic-gate freemsg(mp);
33017c478bd9Sstevel@tonic-gate kmem_free(control, controllen);
33027c478bd9Sstevel@tonic-gate eprintsoline(so, error);
330317169044Sbrutus goto out;
33047c478bd9Sstevel@tonic-gate }
33057c478bd9Sstevel@tonic-gate msg->msg_control = control;
33067c478bd9Sstevel@tonic-gate msg->msg_controllen = controllen;
33077c478bd9Sstevel@tonic-gate }
33087c478bd9Sstevel@tonic-gate
33097c478bd9Sstevel@tonic-gate /*
33107c478bd9Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on
33117c478bd9Sstevel@tonic-gate * DATA_flag and MOREDATA.
33127c478bd9Sstevel@tonic-gate */
33137c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
33147c478bd9Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR;
33157c478bd9Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) {
33167c478bd9Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA))
33177c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_EOR;
33187c478bd9Sstevel@tonic-gate else
33197c478bd9Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR;
33207c478bd9Sstevel@tonic-gate }
33217c478bd9Sstevel@tonic-gate freemsg(mp);
33227c478bd9Sstevel@tonic-gate /*
33237c478bd9Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the
33247c478bd9Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more.
33257c478bd9Sstevel@tonic-gate * Not possible to wait if control info was received.
33267c478bd9Sstevel@tonic-gate */
33277c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
33287c478bd9Sstevel@tonic-gate controllen == 0 &&
33297c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
33307c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
33317c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
33327c478bd9Sstevel@tonic-gate goto retry;
33337c478bd9Sstevel@tonic-gate }
333417169044Sbrutus goto out_locked;
33357c478bd9Sstevel@tonic-gate }
33367c478bd9Sstevel@tonic-gate case T_EXDATA_IND: {
33377c478bd9Sstevel@tonic-gate dprintso(so, 1,
3338fc80c0dfSnordmark ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
3339fc80c0dfSnordmark "state %s\n",
33400f1702c5SYu Xiangning sti->sti_oobsigcnt, sti->sti_oobcnt,
3341fc80c0dfSnordmark saved_resid - uiop->uio_resid,
3342fc80c0dfSnordmark pr_state(so->so_state, so->so_mode)));
33437c478bd9Sstevel@tonic-gate /*
33447c478bd9Sstevel@tonic-gate * kstrgetmsg handles MSGMARK so there is nothing to
33457c478bd9Sstevel@tonic-gate * inspect in the T_EXDATA_IND.
33467c478bd9Sstevel@tonic-gate * strsock_proto makes the stream head queue the T_EXDATA_IND
33477c478bd9Sstevel@tonic-gate * as a separate message with no M_DATA component. Furthermore,
33487c478bd9Sstevel@tonic-gate * the stream head does not consolidate M_DATA messages onto
33497c478bd9Sstevel@tonic-gate * an MSGMARK'ed message ensuring that the T_EXDATA_IND
33507c478bd9Sstevel@tonic-gate * remains a message by itself. This is needed since MSGMARK
33517c478bd9Sstevel@tonic-gate * marks both the whole message as well as the last byte
33527c478bd9Sstevel@tonic-gate * of the message.
33537c478bd9Sstevel@tonic-gate */
33547c478bd9Sstevel@tonic-gate freemsg(mp);
33557c478bd9Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); /* No data */
33567c478bd9Sstevel@tonic-gate if (flags & MSG_PEEK) {
33577c478bd9Sstevel@tonic-gate /*
33587c478bd9Sstevel@tonic-gate * Even though we are peeking we consume the
33597c478bd9Sstevel@tonic-gate * T_EXDATA_IND thereby moving the mark information
33607c478bd9Sstevel@tonic-gate * to SS_RCVATMARK. Then the oob code below will
33617c478bd9Sstevel@tonic-gate * retry the peeking kstrgetmsg.
33627c478bd9Sstevel@tonic-gate * Note that the stream head read queue is
33637c478bd9Sstevel@tonic-gate * never flushed without holding SOREADLOCKED
33647c478bd9Sstevel@tonic-gate * thus the T_EXDATA_IND can not disappear
33657c478bd9Sstevel@tonic-gate * underneath us.
33667c478bd9Sstevel@tonic-gate */
33677c478bd9Sstevel@tonic-gate dprintso(so, 1,
3368fc80c0dfSnordmark ("sotpi_recvmsg: consume EXDATA_IND "
3369fc80c0dfSnordmark "counts %d/%d state %s\n",
33700f1702c5SYu Xiangning sti->sti_oobsigcnt,
33710f1702c5SYu Xiangning sti->sti_oobcnt,
3372fc80c0dfSnordmark pr_state(so->so_state, so->so_mode)));
33737c478bd9Sstevel@tonic-gate
33747c478bd9Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR;
33757c478bd9Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC)
33767c478bd9Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL;
33777c478bd9Sstevel@tonic-gate
33787c478bd9Sstevel@tonic-gate pri = 0;
33797c478bd9Sstevel@tonic-gate mp = NULL;
33807c478bd9Sstevel@tonic-gate
33817c478bd9Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop,
3382fc80c0dfSnordmark &pri, &pflag, (clock_t)-1, &rval);
33837c478bd9Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid);
33847c478bd9Sstevel@tonic-gate
33857c478bd9Sstevel@tonic-gate if (error) {
33867c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
33877c478bd9Sstevel@tonic-gate if (error != EWOULDBLOCK && error != EINTR) {
33887c478bd9Sstevel@tonic-gate eprintsoline(so, error);
33897c478bd9Sstevel@tonic-gate }
33907c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
339117169044Sbrutus goto out;
33927c478bd9Sstevel@tonic-gate }
33937c478bd9Sstevel@tonic-gate ASSERT(mp);
33947c478bd9Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr;
33957c478bd9Sstevel@tonic-gate ASSERT(tpr->type == T_EXDATA_IND);
33967c478bd9Sstevel@tonic-gate freemsg(mp);
33977c478bd9Sstevel@tonic-gate } /* end "if (flags & MSG_PEEK)" */
33987c478bd9Sstevel@tonic-gate
33997c478bd9Sstevel@tonic-gate /*
34007c478bd9Sstevel@tonic-gate * Decrement the number of queued and pending oob.
34017c478bd9Sstevel@tonic-gate *
34027c478bd9Sstevel@tonic-gate * SS_RCVATMARK is cleared when we read past a mark.
34037c478bd9Sstevel@tonic-gate * SS_HAVEOOBDATA is cleared when we've read past the
34047c478bd9Sstevel@tonic-gate * last mark.
34057c478bd9Sstevel@tonic-gate * SS_OOBPEND is cleared if we've read past the last
34067c478bd9Sstevel@tonic-gate * mark and no (new) SIGURG has been posted.
34077c478bd9Sstevel@tonic-gate */
34087c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
34097c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
34100f1702c5SYu Xiangning ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
34110f1702c5SYu Xiangning ASSERT(sti->sti_oobsigcnt > 0);
34120f1702c5SYu Xiangning sti->sti_oobsigcnt--;
34130f1702c5SYu Xiangning ASSERT(sti->sti_oobcnt > 0);
34140f1702c5SYu Xiangning sti->sti_oobcnt--;
34157c478bd9Sstevel@tonic-gate /*
34167c478bd9Sstevel@tonic-gate * Since the T_EXDATA_IND has been removed from the stream
34177c478bd9Sstevel@tonic-gate * head, but we have not read data past the mark,
34187c478bd9Sstevel@tonic-gate * sockfs needs to track that the socket is still at the mark.
34197c478bd9Sstevel@tonic-gate *
34207c478bd9Sstevel@tonic-gate * Since no data was received call kstrgetmsg again to wait
34217c478bd9Sstevel@tonic-gate * for data.
34227c478bd9Sstevel@tonic-gate */
34237c478bd9Sstevel@tonic-gate so->so_state |= SS_RCVATMARK;
34247c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
34257c478bd9Sstevel@tonic-gate dprintso(so, 1,
34267c478bd9Sstevel@tonic-gate ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n",
34270f1702c5SYu Xiangning sti->sti_oobsigcnt, sti->sti_oobcnt,
34287c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
34297c478bd9Sstevel@tonic-gate pflag = opflag;
34307c478bd9Sstevel@tonic-gate goto retry;
34317c478bd9Sstevel@tonic-gate }
34327c478bd9Sstevel@tonic-gate default:
34330f1702c5SYu Xiangning cmn_err(CE_CONT, "sotpi_recvmsg: so %p prim %d mp %p\n",
34340f1702c5SYu Xiangning (void *)so, tpr->type, (void *)mp);
34357c478bd9Sstevel@tonic-gate ASSERT(0);
34367c478bd9Sstevel@tonic-gate freemsg(mp);
34377c478bd9Sstevel@tonic-gate error = EPROTO;
34387c478bd9Sstevel@tonic-gate eprintsoline(so, error);
343917169044Sbrutus goto out;
34407c478bd9Sstevel@tonic-gate }
34417c478bd9Sstevel@tonic-gate /* NOTREACHED */
344217169044Sbrutus out:
34437c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
344417169044Sbrutus out_locked:
34457c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
34467c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
34477c478bd9Sstevel@tonic-gate return (error);
34487c478bd9Sstevel@tonic-gate }
34497c478bd9Sstevel@tonic-gate
34507c478bd9Sstevel@tonic-gate /*
34517c478bd9Sstevel@tonic-gate * Sending data with options on a datagram socket.
34527c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set.
3453f012ee0cSGordon Ross *
3454f012ee0cSGordon Ross * For AF_UNIX the destination address may be already in
3455f012ee0cSGordon Ross * internal form, as indicated by sti->sti_faddr_noxlate
3456f012ee0cSGordon Ross * or the MSG_SENDTO_NOXLATE flag. Otherwise we need to
3457f012ee0cSGordon Ross * translate the destination address to internal form.
3458f012ee0cSGordon Ross *
3459f012ee0cSGordon Ross * The source address is passed as an option. If passing
3460f012ee0cSGordon Ross * file descriptors, those are passed as file pointers in
3461f012ee0cSGordon Ross * another option.
34627c478bd9Sstevel@tonic-gate */
34637c478bd9Sstevel@tonic-gate static int
sosend_dgramcmsg(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,void * control,t_uscalar_t controllen,int flags)3464ff550d0eSmasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3465ff550d0eSmasputra struct uio *uiop, void *control, t_uscalar_t controllen, int flags)
34667c478bd9Sstevel@tonic-gate {
34677c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
34687c478bd9Sstevel@tonic-gate mblk_t *mp;
34697c478bd9Sstevel@tonic-gate int error;
34707c478bd9Sstevel@tonic-gate void *addr;
34717c478bd9Sstevel@tonic-gate socklen_t addrlen;
34727c478bd9Sstevel@tonic-gate void *src;
34737c478bd9Sstevel@tonic-gate socklen_t srclen;
34747c478bd9Sstevel@tonic-gate ssize_t len;
34757c478bd9Sstevel@tonic-gate int size;
34767c478bd9Sstevel@tonic-gate struct T_opthdr toh;
34777c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf;
34787c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
34797c478bd9Sstevel@tonic-gate void *fds;
34807c478bd9Sstevel@tonic-gate int fdlen;
34810f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
34827c478bd9Sstevel@tonic-gate
34837c478bd9Sstevel@tonic-gate ASSERT(name && namelen);
34847c478bd9Sstevel@tonic-gate ASSERT(control && controllen);
34857c478bd9Sstevel@tonic-gate
34867c478bd9Sstevel@tonic-gate len = uiop->uio_resid;
34870f1702c5SYu Xiangning if (len > (ssize_t)sti->sti_tidu_size) {
34887c478bd9Sstevel@tonic-gate return (EMSGSIZE);
34897c478bd9Sstevel@tonic-gate }
34907c478bd9Sstevel@tonic-gate
3491f012ee0cSGordon Ross if (sti->sti_faddr_noxlate == 0 &&
3492f012ee0cSGordon Ross (flags & MSG_SENDTO_NOXLATE) == 0) {
3493f012ee0cSGordon Ross /*
3494f012ee0cSGordon Ross * Length and family checks.
3495f012ee0cSGordon Ross * Don't verify internal form.
3496f012ee0cSGordon Ross */
3497f012ee0cSGordon Ross error = so_addr_verify(so, name, namelen);
3498f012ee0cSGordon Ross if (error) {
3499f012ee0cSGordon Ross eprintsoline(so, error);
3500f012ee0cSGordon Ross return (error);
3501f012ee0cSGordon Ross }
35027c478bd9Sstevel@tonic-gate }
3503f012ee0cSGordon Ross
35047c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
35050f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
35067c478bd9Sstevel@tonic-gate /*
35077c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
35087c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
35097c478bd9Sstevel@tonic-gate */
35107c478bd9Sstevel@tonic-gate addr = name;
35117c478bd9Sstevel@tonic-gate addrlen = namelen;
35127c478bd9Sstevel@tonic-gate src = NULL;
35137c478bd9Sstevel@tonic-gate srclen = 0;
3514f012ee0cSGordon Ross } else if (flags & MSG_SENDTO_NOXLATE) {
3515f012ee0cSGordon Ross /*
3516f012ee0cSGordon Ross * Have an internal form dest. address.
3517f012ee0cSGordon Ross * Pass the source address as usual.
3518f012ee0cSGordon Ross */
3519f012ee0cSGordon Ross addr = name;
3520f012ee0cSGordon Ross addrlen = namelen;
3521f012ee0cSGordon Ross src = sti->sti_laddr_sa;
3522f012ee0cSGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
35237c478bd9Sstevel@tonic-gate } else {
35247c478bd9Sstevel@tonic-gate /*
35257c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
35267c478bd9Sstevel@tonic-gate * and translate the remote address.
35277c478bd9Sstevel@tonic-gate *
35280f1702c5SYu Xiangning * Note that this code does not prevent sti_laddr_sa
35297c478bd9Sstevel@tonic-gate * from changing while it is being used. Thus
35307c478bd9Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this
35317c478bd9Sstevel@tonic-gate * send the peer might see a partially new and a
35327c478bd9Sstevel@tonic-gate * partially old "from" address.
35337c478bd9Sstevel@tonic-gate */
35340f1702c5SYu Xiangning src = sti->sti_laddr_sa;
3535f012ee0cSGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
35367c478bd9Sstevel@tonic-gate dprintso(so, 1,
35377c478bd9Sstevel@tonic-gate ("sosend_dgramcmsg UNIX: srclen %d, src %p\n",
35387c478bd9Sstevel@tonic-gate srclen, src));
3539f012ee0cSGordon Ross /*
3540f012ee0cSGordon Ross * The sendmsg caller specified a destination
3541f012ee0cSGordon Ross * address, which we must translate into our
3542f012ee0cSGordon Ross * internal form. addr = &sti->sti_ux_taddr
3543f012ee0cSGordon Ross */
35447c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen,
3545fc80c0dfSnordmark (flags & MSG_XPG4_2),
3546fc80c0dfSnordmark &addr, &addrlen);
35477c478bd9Sstevel@tonic-gate if (error) {
35487c478bd9Sstevel@tonic-gate eprintsoline(so, error);
35497c478bd9Sstevel@tonic-gate return (error);
35507c478bd9Sstevel@tonic-gate }
35517c478bd9Sstevel@tonic-gate }
35527c478bd9Sstevel@tonic-gate } else {
35537c478bd9Sstevel@tonic-gate addr = name;
35547c478bd9Sstevel@tonic-gate addrlen = namelen;
35557c478bd9Sstevel@tonic-gate src = NULL;
35567c478bd9Sstevel@tonic-gate srclen = 0;
35577c478bd9Sstevel@tonic-gate }
35587c478bd9Sstevel@tonic-gate optlen = so_optlen(control, controllen,
3559fc80c0dfSnordmark !(flags & MSG_XPG4_2));
35607c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
35617c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
35627c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
35637c478bd9Sstevel@tonic-gate if (srclen != 0)
35647c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) +
35657c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen));
35667c478bd9Sstevel@tonic-gate else
35677c478bd9Sstevel@tonic-gate tudr.OPT_length = optlen;
35687c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
3569fc80c0dfSnordmark _TPI_ALIGN_TOPT(addrlen));
35707c478bd9Sstevel@tonic-gate
35717c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
35727c478bd9Sstevel@tonic-gate
35737c478bd9Sstevel@tonic-gate /*
35747c478bd9Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set.
35757c478bd9Sstevel@tonic-gate */
35767c478bd9Sstevel@tonic-gate error = so_getfdopt(control, controllen,
3577fc80c0dfSnordmark !(flags & MSG_XPG4_2), &fds, &fdlen);
35787c478bd9Sstevel@tonic-gate if (error)
35797c478bd9Sstevel@tonic-gate return (error);
35807c478bd9Sstevel@tonic-gate if (fdlen != -1) {
35817c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING))
35827c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
35837c478bd9Sstevel@tonic-gate
35847c478bd9Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf);
35857c478bd9Sstevel@tonic-gate if (error)
35867c478bd9Sstevel@tonic-gate return (error);
3587d28d4716SJerry Jelinek
3588d28d4716SJerry Jelinek /*
3589d28d4716SJerry Jelinek * Pre-allocate enough additional space for lower level modules
3590d28d4716SJerry Jelinek * to append an option (e.g. see tl_unitdata). The following
3591d28d4716SJerry Jelinek * is enough extra space for the largest option we might append.
3592d28d4716SJerry Jelinek */
3593d28d4716SJerry Jelinek size += sizeof (struct T_opthdr) + ucredsize;
35947c478bd9Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf);
35957c478bd9Sstevel@tonic-gate } else {
3596de8c4a14SErik Nordmark mp = soallocproto(size, _ALLOC_INTR, CRED());
3597bd118333Smeem if (mp == NULL) {
3598bd118333Smeem /*
3599bd118333Smeem * Caught a signal waiting for memory.
3600bd118333Smeem * Let send* return EINTR.
3601bd118333Smeem */
3602bd118333Smeem return (EINTR);
3603bd118333Smeem }
36047c478bd9Sstevel@tonic-gate }
36057c478bd9Sstevel@tonic-gate soappendmsg(mp, &tudr, sizeof (tudr));
36067c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
36077c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
36087c478bd9Sstevel@tonic-gate
36097c478bd9Sstevel@tonic-gate if (fdlen != -1) {
36107c478bd9Sstevel@tonic-gate ASSERT(fdbuf != NULL);
36117c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
36127c478bd9Sstevel@tonic-gate toh.name = SO_FILEP;
36137c478bd9Sstevel@tonic-gate toh.len = fdbuf->fd_size +
3614fc80c0dfSnordmark (t_uscalar_t)sizeof (struct T_opthdr);
36157c478bd9Sstevel@tonic-gate toh.status = 0;
36167c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
36177c478bd9Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size);
36187c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
36197c478bd9Sstevel@tonic-gate }
36207c478bd9Sstevel@tonic-gate if (srclen != 0) {
36217c478bd9Sstevel@tonic-gate /*
36227c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
36237c478bd9Sstevel@tonic-gate * address option.
36247c478bd9Sstevel@tonic-gate */
36257c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
36267c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
36277c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
36287c478bd9Sstevel@tonic-gate toh.status = 0;
36297c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
36307c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
36317c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
36327c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
36337c478bd9Sstevel@tonic-gate }
36347c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
36357c478bd9Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
3636d28d4716SJerry Jelinek /*
3637d28d4716SJerry Jelinek * Normally at most 3 bytes left in the message, but we might have
3638d28d4716SJerry Jelinek * allowed for extra space if we're passing fd's through.
3639d28d4716SJerry Jelinek */
36407c478bd9Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size);
36417c478bd9Sstevel@tonic-gate
36427c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3643005d3febSMarek Pospisil if (AU_AUDITING())
36447c478bd9Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
36457c478bd9Sstevel@tonic-gate
36467c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
36477c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
36487c478bd9Sstevel@tonic-gate if (error) {
36497c478bd9Sstevel@tonic-gate eprintsoline(so, error);
36507c478bd9Sstevel@tonic-gate }
36517c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
36527c478bd9Sstevel@tonic-gate return (error);
36537c478bd9Sstevel@tonic-gate }
36547c478bd9Sstevel@tonic-gate
36557c478bd9Sstevel@tonic-gate /*
36567c478bd9Sstevel@tonic-gate * Sending data with options on a connected stream socket.
36577c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set.
36587c478bd9Sstevel@tonic-gate */
36597c478bd9Sstevel@tonic-gate static int
sosend_svccmsg(struct sonode * so,struct uio * uiop,int more,void * control,t_uscalar_t controllen,int flags)36600f1702c5SYu Xiangning sosend_svccmsg(struct sonode *so, struct uio *uiop, int more, void *control,
36610f1702c5SYu Xiangning t_uscalar_t controllen, int flags)
36627c478bd9Sstevel@tonic-gate {
36637c478bd9Sstevel@tonic-gate struct T_optdata_req tdr;
36647c478bd9Sstevel@tonic-gate mblk_t *mp;
36657c478bd9Sstevel@tonic-gate int error;
36667c478bd9Sstevel@tonic-gate ssize_t iosize;
36677c478bd9Sstevel@tonic-gate int size;
36687c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf;
36697c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
36707c478bd9Sstevel@tonic-gate void *fds;
36717c478bd9Sstevel@tonic-gate int fdlen;
36727c478bd9Sstevel@tonic-gate struct T_opthdr toh;
36730f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
36747c478bd9Sstevel@tonic-gate
36757c478bd9Sstevel@tonic-gate dprintso(so, 1,
3676fc80c0dfSnordmark ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
36777c478bd9Sstevel@tonic-gate
36787c478bd9Sstevel@tonic-gate /*
36797c478bd9Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are
36807c478bd9Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it
36817c478bd9Sstevel@tonic-gate * thus it is not possible to ASSERT on the state.
36827c478bd9Sstevel@tonic-gate */
36837c478bd9Sstevel@tonic-gate
36847c478bd9Sstevel@tonic-gate /* Options on connection-oriented only when SM_OPTDATA set. */
36857c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_OPTDATA))
36867c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
36877c478bd9Sstevel@tonic-gate
36887c478bd9Sstevel@tonic-gate do {
36897c478bd9Sstevel@tonic-gate /*
36907c478bd9Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this
36917c478bd9Sstevel@tonic-gate * message or if the caller passed in "more".
36927c478bd9Sstevel@tonic-gate * Error for transports with zero tidu_size.
36937c478bd9Sstevel@tonic-gate */
36947c478bd9Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ;
36950f1702c5SYu Xiangning iosize = sti->sti_tidu_size;
36967c478bd9Sstevel@tonic-gate if (iosize <= 0)
36977c478bd9Sstevel@tonic-gate return (EMSGSIZE);
36987c478bd9Sstevel@tonic-gate if (uiop->uio_resid > iosize) {
36997c478bd9Sstevel@tonic-gate tdr.DATA_flag = 1;
37007c478bd9Sstevel@tonic-gate } else {
37017c478bd9Sstevel@tonic-gate if (more)
37027c478bd9Sstevel@tonic-gate tdr.DATA_flag = 1;
37037c478bd9Sstevel@tonic-gate else
37047c478bd9Sstevel@tonic-gate tdr.DATA_flag = 0;
37057c478bd9Sstevel@tonic-gate iosize = uiop->uio_resid;
37067c478bd9Sstevel@tonic-gate }
37077c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
3708fc80c0dfSnordmark tdr.DATA_flag, iosize));
37097c478bd9Sstevel@tonic-gate
37107c478bd9Sstevel@tonic-gate optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
37117c478bd9Sstevel@tonic-gate tdr.OPT_length = optlen;
37127c478bd9Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
37137c478bd9Sstevel@tonic-gate
37147c478bd9Sstevel@tonic-gate size = (int)sizeof (tdr) + optlen;
37157c478bd9Sstevel@tonic-gate /*
37167c478bd9Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set.
37177c478bd9Sstevel@tonic-gate */
37187c478bd9Sstevel@tonic-gate error = so_getfdopt(control, controllen,
3719fc80c0dfSnordmark !(flags & MSG_XPG4_2), &fds, &fdlen);
37207c478bd9Sstevel@tonic-gate if (error)
37217c478bd9Sstevel@tonic-gate return (error);
37227c478bd9Sstevel@tonic-gate if (fdlen != -1) {
37237c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING))
37247c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
37257c478bd9Sstevel@tonic-gate
37267c478bd9Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf);
37277c478bd9Sstevel@tonic-gate if (error)
37287c478bd9Sstevel@tonic-gate return (error);
3729d28d4716SJerry Jelinek
3730d28d4716SJerry Jelinek /*
3731d28d4716SJerry Jelinek * Pre-allocate enough additional space for lower level
3732d28d4716SJerry Jelinek * modules to append an option (e.g. see tl_unitdata).
3733d28d4716SJerry Jelinek * The following is enough extra space for the largest
3734d28d4716SJerry Jelinek * option we might append.
3735d28d4716SJerry Jelinek */
3736d28d4716SJerry Jelinek size += sizeof (struct T_opthdr) + ucredsize;
37377c478bd9Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf);
37387c478bd9Sstevel@tonic-gate } else {
3739de8c4a14SErik Nordmark mp = soallocproto(size, _ALLOC_INTR, CRED());
3740bd118333Smeem if (mp == NULL) {
3741bd118333Smeem /*
3742bd118333Smeem * Caught a signal waiting for memory.
3743bd118333Smeem * Let send* return EINTR.
3744bd118333Smeem */
37450f1702c5SYu Xiangning return (EINTR);
3746bd118333Smeem }
37477c478bd9Sstevel@tonic-gate }
37487c478bd9Sstevel@tonic-gate soappendmsg(mp, &tdr, sizeof (tdr));
37497c478bd9Sstevel@tonic-gate
37507c478bd9Sstevel@tonic-gate if (fdlen != -1) {
37517c478bd9Sstevel@tonic-gate ASSERT(fdbuf != NULL);
37527c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
37537c478bd9Sstevel@tonic-gate toh.name = SO_FILEP;
37547c478bd9Sstevel@tonic-gate toh.len = fdbuf->fd_size +
3755fc80c0dfSnordmark (t_uscalar_t)sizeof (struct T_opthdr);
37567c478bd9Sstevel@tonic-gate toh.status = 0;
37577c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
37587c478bd9Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size);
37597c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
37607c478bd9Sstevel@tonic-gate }
37617c478bd9Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
3762d28d4716SJerry Jelinek /*
3763d28d4716SJerry Jelinek * Normally at most 3 bytes left in the message, but we might
3764d28d4716SJerry Jelinek * have allowed for extra space if we're passing fd's through.
3765d28d4716SJerry Jelinek */
37667c478bd9Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size);
37677c478bd9Sstevel@tonic-gate
37687c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
37697c478bd9Sstevel@tonic-gate
37707c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
3771fc80c0dfSnordmark 0, MSG_BAND, 0);
37727c478bd9Sstevel@tonic-gate if (error) {
37737c478bd9Sstevel@tonic-gate eprintsoline(so, error);
37747c478bd9Sstevel@tonic-gate return (error);
37757c478bd9Sstevel@tonic-gate }
37767c478bd9Sstevel@tonic-gate control = NULL;
37777c478bd9Sstevel@tonic-gate if (uiop->uio_resid > 0) {
37787c478bd9Sstevel@tonic-gate /*
37797c478bd9Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though
37807c478bd9Sstevel@tonic-gate * some data have been written. This is consistent
37817c478bd9Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics.
37827c478bd9Sstevel@tonic-gate */
37837c478bd9Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) {
37847c478bd9Sstevel@tonic-gate eprintsoline(so, error);
37857c478bd9Sstevel@tonic-gate return (EPIPE);
37867c478bd9Sstevel@tonic-gate }
37877c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
37887c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
37890f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
37907c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
37917c478bd9Sstevel@tonic-gate if (error != 0) {
37927c478bd9Sstevel@tonic-gate eprintsoline(so, error);
37937c478bd9Sstevel@tonic-gate return (error);
37947c478bd9Sstevel@tonic-gate }
37957c478bd9Sstevel@tonic-gate }
37967c478bd9Sstevel@tonic-gate }
37977c478bd9Sstevel@tonic-gate } while (uiop->uio_resid > 0);
37987c478bd9Sstevel@tonic-gate return (0);
37997c478bd9Sstevel@tonic-gate }
38007c478bd9Sstevel@tonic-gate
38017c478bd9Sstevel@tonic-gate /*
38027c478bd9Sstevel@tonic-gate * Sending data on a datagram socket.
38037c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set.
38047c478bd9Sstevel@tonic-gate *
3805f012ee0cSGordon Ross * For AF_UNIX the destination address may be already in
3806f012ee0cSGordon Ross * internal form, as indicated by sti->sti_faddr_noxlate
3807f012ee0cSGordon Ross * or the MSG_SENDTO_NOXLATE flag. Otherwise we need to
3808f012ee0cSGordon Ross * translate the destination address to internal form.
3809f012ee0cSGordon Ross *
3810f012ee0cSGordon Ross * The source address is passed as an option.
38117c478bd9Sstevel@tonic-gate */
38127c478bd9Sstevel@tonic-gate int
sosend_dgram(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,int flags)3813ff550d0eSmasputra sosend_dgram(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3814ff550d0eSmasputra struct uio *uiop, int flags)
38157c478bd9Sstevel@tonic-gate {
38167c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
38177c478bd9Sstevel@tonic-gate mblk_t *mp;
38187c478bd9Sstevel@tonic-gate int error;
38197c478bd9Sstevel@tonic-gate void *addr;
38207c478bd9Sstevel@tonic-gate socklen_t addrlen;
38217c478bd9Sstevel@tonic-gate void *src;
38227c478bd9Sstevel@tonic-gate socklen_t srclen;
38237c478bd9Sstevel@tonic-gate ssize_t len;
38240f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
38257c478bd9Sstevel@tonic-gate
3826ff550d0eSmasputra ASSERT(name != NULL && namelen != 0);
38277c478bd9Sstevel@tonic-gate
38287c478bd9Sstevel@tonic-gate len = uiop->uio_resid;
38290f1702c5SYu Xiangning if (len > sti->sti_tidu_size) {
38307c478bd9Sstevel@tonic-gate error = EMSGSIZE;
38317c478bd9Sstevel@tonic-gate goto done;
38327c478bd9Sstevel@tonic-gate }
38337c478bd9Sstevel@tonic-gate
3834f012ee0cSGordon Ross if (sti->sti_faddr_noxlate == 0 &&
3835f012ee0cSGordon Ross (flags & MSG_SENDTO_NOXLATE) == 0) {
3836f012ee0cSGordon Ross /*
3837f012ee0cSGordon Ross * Length and family checks.
3838f012ee0cSGordon Ross * Don't verify internal form.
3839f012ee0cSGordon Ross */
3840f012ee0cSGordon Ross error = so_addr_verify(so, name, namelen);
3841f012ee0cSGordon Ross if (error != 0)
3842f012ee0cSGordon Ross goto done;
3843f012ee0cSGordon Ross }
3844ff550d0eSmasputra
3845f012ee0cSGordon Ross if (sti->sti_direct) /* Never on AF_UNIX */
3846ff550d0eSmasputra return (sodgram_direct(so, name, namelen, uiop, flags));
3847ff550d0eSmasputra
38487c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
38490f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
38507c478bd9Sstevel@tonic-gate /*
38517c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
38527c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
38537c478bd9Sstevel@tonic-gate */
38547c478bd9Sstevel@tonic-gate addr = name;
38557c478bd9Sstevel@tonic-gate addrlen = namelen;
38567c478bd9Sstevel@tonic-gate src = NULL;
38577c478bd9Sstevel@tonic-gate srclen = 0;
3858f012ee0cSGordon Ross } else if (flags & MSG_SENDTO_NOXLATE) {
3859f012ee0cSGordon Ross /*
3860f012ee0cSGordon Ross * Have an internal form dest. address.
3861f012ee0cSGordon Ross * Pass the source address as usual.
3862f012ee0cSGordon Ross */
3863f012ee0cSGordon Ross addr = name;
3864f012ee0cSGordon Ross addrlen = namelen;
3865f012ee0cSGordon Ross src = sti->sti_laddr_sa;
3866f012ee0cSGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
38677c478bd9Sstevel@tonic-gate } else {
38687c478bd9Sstevel@tonic-gate /*
38697c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
38707c478bd9Sstevel@tonic-gate * and translate the remote address.
38717c478bd9Sstevel@tonic-gate *
38720f1702c5SYu Xiangning * Note that this code does not prevent sti_laddr_sa
38737c478bd9Sstevel@tonic-gate * from changing while it is being used. Thus
38747c478bd9Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this
38757c478bd9Sstevel@tonic-gate * send the peer might see a partially new and a
38767c478bd9Sstevel@tonic-gate * partially old "from" address.
38777c478bd9Sstevel@tonic-gate */
38780f1702c5SYu Xiangning src = sti->sti_laddr_sa;
38790f1702c5SYu Xiangning srclen = (socklen_t)sti->sti_laddr_len;
38807c478bd9Sstevel@tonic-gate dprintso(so, 1,
3881fc80c0dfSnordmark ("sosend_dgram UNIX: srclen %d, src %p\n",
3882fc80c0dfSnordmark srclen, src));
3883f012ee0cSGordon Ross /*
3884f012ee0cSGordon Ross * The sendmsg caller specified a destination
3885f012ee0cSGordon Ross * address, which we must translate into our
3886f012ee0cSGordon Ross * internal form. addr = &sti->sti_ux_taddr
3887f012ee0cSGordon Ross */
38887c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen,
3889fc80c0dfSnordmark (flags & MSG_XPG4_2),
3890fc80c0dfSnordmark &addr, &addrlen);
38917c478bd9Sstevel@tonic-gate if (error) {
38927c478bd9Sstevel@tonic-gate eprintsoline(so, error);
38937c478bd9Sstevel@tonic-gate goto done;
38947c478bd9Sstevel@tonic-gate }
38957c478bd9Sstevel@tonic-gate }
38967c478bd9Sstevel@tonic-gate } else {
38977c478bd9Sstevel@tonic-gate addr = name;
38987c478bd9Sstevel@tonic-gate addrlen = namelen;
38997c478bd9Sstevel@tonic-gate src = NULL;
39007c478bd9Sstevel@tonic-gate srclen = 0;
39017c478bd9Sstevel@tonic-gate }
39027c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
39037c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
39047c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
39057c478bd9Sstevel@tonic-gate if (srclen == 0) {
39067c478bd9Sstevel@tonic-gate tudr.OPT_length = 0;
39077c478bd9Sstevel@tonic-gate tudr.OPT_offset = 0;
39087c478bd9Sstevel@tonic-gate
39097c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
3910de8c4a14SErik Nordmark addr, addrlen, 0, _ALLOC_INTR, CRED());
39117c478bd9Sstevel@tonic-gate if (mp == NULL) {
39127c478bd9Sstevel@tonic-gate /*
39137c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
39147c478bd9Sstevel@tonic-gate * Let send* return EINTR.
39157c478bd9Sstevel@tonic-gate */
39167c478bd9Sstevel@tonic-gate error = EINTR;
39177c478bd9Sstevel@tonic-gate goto done;
39187c478bd9Sstevel@tonic-gate }
39197c478bd9Sstevel@tonic-gate } else {
39207c478bd9Sstevel@tonic-gate /*
39217c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
39227c478bd9Sstevel@tonic-gate * address option.
39237c478bd9Sstevel@tonic-gate */
39247c478bd9Sstevel@tonic-gate struct T_opthdr toh;
39257c478bd9Sstevel@tonic-gate ssize_t size;
39267c478bd9Sstevel@tonic-gate
39277c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
3928fc80c0dfSnordmark _TPI_ALIGN_TOPT(srclen));
39297c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
3930fc80c0dfSnordmark _TPI_ALIGN_TOPT(addrlen));
39317c478bd9Sstevel@tonic-gate
39327c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
39337c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
39347c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
39357c478bd9Sstevel@tonic-gate toh.status = 0;
39367c478bd9Sstevel@tonic-gate
39377c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
39387c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
3939de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_INTR, CRED());
39407c478bd9Sstevel@tonic-gate if (mp == NULL) {
39417c478bd9Sstevel@tonic-gate /*
39427c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
39437c478bd9Sstevel@tonic-gate * Let send* return EINTR.
39447c478bd9Sstevel@tonic-gate */
39457c478bd9Sstevel@tonic-gate error = EINTR;
39467c478bd9Sstevel@tonic-gate goto done;
39477c478bd9Sstevel@tonic-gate }
39487c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
39497c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
39507c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
39517c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
39527c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
39537c478bd9Sstevel@tonic-gate }
39547c478bd9Sstevel@tonic-gate
3955005d3febSMarek Pospisil if (AU_AUDITING())
39567c478bd9Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
39577c478bd9Sstevel@tonic-gate
39587c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
39597c478bd9Sstevel@tonic-gate done:
39607c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
39617c478bd9Sstevel@tonic-gate if (error) {
39627c478bd9Sstevel@tonic-gate eprintsoline(so, error);
39637c478bd9Sstevel@tonic-gate }
39647c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
39657c478bd9Sstevel@tonic-gate return (error);
39667c478bd9Sstevel@tonic-gate }
39677c478bd9Sstevel@tonic-gate
39687c478bd9Sstevel@tonic-gate /*
39697c478bd9Sstevel@tonic-gate * Sending data on a connected stream socket.
39707c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set.
39717c478bd9Sstevel@tonic-gate */
39727c478bd9Sstevel@tonic-gate int
sosend_svc(struct sonode * so,struct uio * uiop,t_scalar_t prim,int more,int sflag)39730f1702c5SYu Xiangning sosend_svc(struct sonode *so, struct uio *uiop, t_scalar_t prim, int more,
39740f1702c5SYu Xiangning int sflag)
39757c478bd9Sstevel@tonic-gate {
39767c478bd9Sstevel@tonic-gate struct T_data_req tdr;
39777c478bd9Sstevel@tonic-gate mblk_t *mp;
39787c478bd9Sstevel@tonic-gate int error;
39797c478bd9Sstevel@tonic-gate ssize_t iosize;
39800f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
39817c478bd9Sstevel@tonic-gate
39827c478bd9Sstevel@tonic-gate dprintso(so, 1,
3983fc80c0dfSnordmark ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
3984903a11ebSrh (void *)so, uiop->uio_resid, prim, sflag));
39857c478bd9Sstevel@tonic-gate
39867c478bd9Sstevel@tonic-gate /*
39877c478bd9Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are
39887c478bd9Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it
39897c478bd9Sstevel@tonic-gate * thus it is not possible to ASSERT on the state.
39907c478bd9Sstevel@tonic-gate */
39917c478bd9Sstevel@tonic-gate
39927c478bd9Sstevel@tonic-gate do {
39937c478bd9Sstevel@tonic-gate /*
39947c478bd9Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this
39957c478bd9Sstevel@tonic-gate * message or if the caller passed in "more".
39967c478bd9Sstevel@tonic-gate * Error for transports with zero tidu_size.
39977c478bd9Sstevel@tonic-gate */
39987c478bd9Sstevel@tonic-gate tdr.PRIM_type = prim;
39990f1702c5SYu Xiangning iosize = sti->sti_tidu_size;
40007c478bd9Sstevel@tonic-gate if (iosize <= 0)
40017c478bd9Sstevel@tonic-gate return (EMSGSIZE);
40027c478bd9Sstevel@tonic-gate if (uiop->uio_resid > iosize) {
40037c478bd9Sstevel@tonic-gate tdr.MORE_flag = 1;
40047c478bd9Sstevel@tonic-gate } else {
40057c478bd9Sstevel@tonic-gate if (more)
40067c478bd9Sstevel@tonic-gate tdr.MORE_flag = 1;
40077c478bd9Sstevel@tonic-gate else
40087c478bd9Sstevel@tonic-gate tdr.MORE_flag = 0;
40097c478bd9Sstevel@tonic-gate iosize = uiop->uio_resid;
40107c478bd9Sstevel@tonic-gate }
40117c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
4012fc80c0dfSnordmark prim, tdr.MORE_flag, iosize));
4013de8c4a14SErik Nordmark mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR, CRED());
40147c478bd9Sstevel@tonic-gate if (mp == NULL) {
40157c478bd9Sstevel@tonic-gate /*
40167c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
40177c478bd9Sstevel@tonic-gate * Let send* return EINTR.
40187c478bd9Sstevel@tonic-gate */
40190f1702c5SYu Xiangning return (EINTR);
40207c478bd9Sstevel@tonic-gate }
40217c478bd9Sstevel@tonic-gate
40227c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
4023fc80c0dfSnordmark 0, sflag | MSG_BAND, 0);
40247c478bd9Sstevel@tonic-gate if (error) {
40257c478bd9Sstevel@tonic-gate eprintsoline(so, error);
40267c478bd9Sstevel@tonic-gate return (error);
40277c478bd9Sstevel@tonic-gate }
40287c478bd9Sstevel@tonic-gate if (uiop->uio_resid > 0) {
40297c478bd9Sstevel@tonic-gate /*
40307c478bd9Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though
40317c478bd9Sstevel@tonic-gate * some data have been written. This is consistent
40327c478bd9Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics.
40337c478bd9Sstevel@tonic-gate */
40347c478bd9Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) {
40357c478bd9Sstevel@tonic-gate eprintsoline(so, error);
40367c478bd9Sstevel@tonic-gate return (EPIPE);
40377c478bd9Sstevel@tonic-gate }
40387c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
40397c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
40400f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
40417c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
40427c478bd9Sstevel@tonic-gate if (error != 0) {
40437c478bd9Sstevel@tonic-gate eprintsoline(so, error);
40447c478bd9Sstevel@tonic-gate return (error);
40457c478bd9Sstevel@tonic-gate }
40467c478bd9Sstevel@tonic-gate }
40477c478bd9Sstevel@tonic-gate }
40487c478bd9Sstevel@tonic-gate } while (uiop->uio_resid > 0);
40497c478bd9Sstevel@tonic-gate return (0);
40507c478bd9Sstevel@tonic-gate }
40517c478bd9Sstevel@tonic-gate
40527c478bd9Sstevel@tonic-gate /*
40537c478bd9Sstevel@tonic-gate * Check the state for errors and call the appropriate send function.
40547c478bd9Sstevel@tonic-gate *
40557c478bd9Sstevel@tonic-gate * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
40567c478bd9Sstevel@tonic-gate * this function issues a setsockopt to toggle SO_DONTROUTE before and
40577c478bd9Sstevel@tonic-gate * after sending the message.
4058f012ee0cSGordon Ross *
4059f012ee0cSGordon Ross * The caller may optionally specify a destination address, for either
4060f012ee0cSGordon Ross * stream or datagram sockets. This table summarizes the cases:
4061f012ee0cSGordon Ross *
4062f012ee0cSGordon Ross * Socket type Dest. given Connected Result
4063f012ee0cSGordon Ross * ----------- ----------- --------- --------------
4064f012ee0cSGordon Ross * Stream * Yes send to conn. addr.
4065f012ee0cSGordon Ross * Stream * No error ENOTCONN
4066f012ee0cSGordon Ross * Dgram yes * send to given addr.
4067f012ee0cSGordon Ross * Dgram no yes send to conn. addr.
4068f012ee0cSGordon Ross * Dgram no no error EDESTADDRREQ
4069f012ee0cSGordon Ross *
4070f012ee0cSGordon Ross * There are subtleties around the destination address when using
4071f012ee0cSGordon Ross * AF_UNIX datagram sockets. When the sendmsg call specifies the
4072f012ee0cSGordon Ross * destination address, it's in (struct sockaddr_un) form and we
4073f012ee0cSGordon Ross * need to translate it to our internal form (struct so_ux_addr).
4074f012ee0cSGordon Ross *
4075f012ee0cSGordon Ross * When the sendmsg call does not specify a destination address
4076f012ee0cSGordon Ross * we're using the peer address saved during sotpi_connect, and
4077f012ee0cSGordon Ross * that address is already in internal form. In this case, the
4078f012ee0cSGordon Ross * (internal only) flag MSG_SENDTO_NOXLATE is set in the flags
4079f012ee0cSGordon Ross * passed to sosend_dgram or sosend_dgramcmsg to indicate that
4080f012ee0cSGordon Ross * those functions should skip translation to internal form.
4081f012ee0cSGordon Ross * Avoiding that translation is not only more efficient, but it's
4082f012ee0cSGordon Ross * also necessary when a process does a connect on an AF_UNIX
4083f012ee0cSGordon Ross * datagram socket and then drops privileges. After the process
4084f012ee0cSGordon Ross * has dropped privileges, it may no longer be able to lookup the
4085f012ee0cSGordon Ross * the external name in the filesystem, but it should still be
4086f012ee0cSGordon Ross * able to send messages on the connected socket by leaving the
4087f012ee0cSGordon Ross * destination name unspecified.
4088f012ee0cSGordon Ross *
4089f012ee0cSGordon Ross * Yet more subtleties arise with sockets connected by socketpair(),
4090f012ee0cSGordon Ross * which puts internal form addresses in the fields where normally
4091f012ee0cSGordon Ross * the external form is found, and sets sti_faddr_noxlate=1, which
4092f012ee0cSGordon Ross * (like flag MSG_SENDTO_NOXLATE) causes the sosend_dgram functions
4093f012ee0cSGordon Ross * to skip translation of destination addresses to internal form.
4094f012ee0cSGordon Ross * However, beware that the flag sti_faddr_noxlate=1 also triggers
4095f012ee0cSGordon Ross * different behaviour almost everywhere AF_UNIX addresses appear.
40967c478bd9Sstevel@tonic-gate */
40977c478bd9Sstevel@tonic-gate static int
sotpi_sendmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)40980f1702c5SYu Xiangning sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
40990f1702c5SYu Xiangning struct cred *cr)
41007c478bd9Sstevel@tonic-gate {
41017c478bd9Sstevel@tonic-gate int so_state;
41027c478bd9Sstevel@tonic-gate int so_mode;
41037c478bd9Sstevel@tonic-gate int error;
41047c478bd9Sstevel@tonic-gate struct sockaddr *name;
41057c478bd9Sstevel@tonic-gate t_uscalar_t namelen;
41067c478bd9Sstevel@tonic-gate int dontroute;
41077c478bd9Sstevel@tonic-gate int flags;
41080f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
41097c478bd9Sstevel@tonic-gate
41107c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
4111903a11ebSrh (void *)so, (void *)msg, msg->msg_flags,
4112fc80c0dfSnordmark pr_state(so->so_state, so->so_mode), so->so_error));
41137c478bd9Sstevel@tonic-gate
41140f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
41150f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
41160f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
41170f1702c5SYu Xiangning return (strwrite(SOTOV(so), uiop, cr));
41180f1702c5SYu Xiangning }
41190f1702c5SYu Xiangning
41207c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
41217c478bd9Sstevel@tonic-gate so_state = so->so_state;
41227c478bd9Sstevel@tonic-gate
41237c478bd9Sstevel@tonic-gate if (so_state & SS_CANTSENDMORE) {
41247c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
41257c478bd9Sstevel@tonic-gate return (EPIPE);
41267c478bd9Sstevel@tonic-gate }
41277c478bd9Sstevel@tonic-gate
41287c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
41290f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
41307c478bd9Sstevel@tonic-gate if (error != 0) {
41317c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
41327c478bd9Sstevel@tonic-gate return (error);
41337c478bd9Sstevel@tonic-gate }
41347c478bd9Sstevel@tonic-gate }
41357c478bd9Sstevel@tonic-gate
41367c478bd9Sstevel@tonic-gate name = (struct sockaddr *)msg->msg_name;
41377c478bd9Sstevel@tonic-gate namelen = msg->msg_namelen;
4138f012ee0cSGordon Ross flags = msg->msg_flags;
4139f012ee0cSGordon Ross
4140f012ee0cSGordon Ross /*
4141f012ee0cSGordon Ross * Historically, this function does not validate the flags
4142f012ee0cSGordon Ross * passed in, and any errant bits are ignored. However,
4143f012ee0cSGordon Ross * we would not want any such errant flag bits accidently
4144f012ee0cSGordon Ross * being treated as one of the internal-only flags, so
4145f012ee0cSGordon Ross * clear the internal-only flag bits.
4146f012ee0cSGordon Ross */
4147f012ee0cSGordon Ross flags &= ~MSG_SENDTO_NOXLATE;
41487c478bd9Sstevel@tonic-gate
41497c478bd9Sstevel@tonic-gate so_mode = so->so_mode;
41507c478bd9Sstevel@tonic-gate
41517c478bd9Sstevel@tonic-gate if (name == NULL) {
41527c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED)) {
41537c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
41547c478bd9Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED)
41557c478bd9Sstevel@tonic-gate return (ENOTCONN);
41567c478bd9Sstevel@tonic-gate else
41577c478bd9Sstevel@tonic-gate return (EDESTADDRREQ);
41587c478bd9Sstevel@tonic-gate }
4159f012ee0cSGordon Ross /*
4160f012ee0cSGordon Ross * This is a connected socket.
4161f012ee0cSGordon Ross */
41627c478bd9Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) {
4163f012ee0cSGordon Ross /*
4164f012ee0cSGordon Ross * This is a connected STREAM socket,
4165f012ee0cSGordon Ross * destination not specified.
4166f012ee0cSGordon Ross */
41677c478bd9Sstevel@tonic-gate name = NULL;
41687c478bd9Sstevel@tonic-gate namelen = 0;
41697c478bd9Sstevel@tonic-gate } else {
41707c478bd9Sstevel@tonic-gate /*
4171f012ee0cSGordon Ross * Datagram send on connected socket with
4172f012ee0cSGordon Ross * the destination name not specified.
4173f012ee0cSGordon Ross * Use the peer address from connect.
41747c478bd9Sstevel@tonic-gate */
4175f012ee0cSGordon Ross if (so->so_family == AF_UNIX) {
4176f012ee0cSGordon Ross /*
4177f012ee0cSGordon Ross * Use the (internal form) address saved
4178f012ee0cSGordon Ross * in sotpi_connect. See above.
4179f012ee0cSGordon Ross */
4180f012ee0cSGordon Ross name = (void *)&sti->sti_ux_faddr;
4181f012ee0cSGordon Ross namelen = sizeof (sti->sti_ux_faddr);
4182f012ee0cSGordon Ross flags |= MSG_SENDTO_NOXLATE;
4183f012ee0cSGordon Ross } else {
4184f012ee0cSGordon Ross ASSERT(sti->sti_faddr_sa);
4185f012ee0cSGordon Ross name = sti->sti_faddr_sa;
4186f012ee0cSGordon Ross namelen = (t_uscalar_t)sti->sti_faddr_len;
4187f012ee0cSGordon Ross }
41887c478bd9Sstevel@tonic-gate }
41897c478bd9Sstevel@tonic-gate } else {
4190f012ee0cSGordon Ross /*
4191f012ee0cSGordon Ross * Sendmsg specifies a destination name
4192f012ee0cSGordon Ross */
41937c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED) &&
41947c478bd9Sstevel@tonic-gate (so_mode & SM_CONNREQUIRED)) {
4195f012ee0cSGordon Ross /* i.e. TCP not connected */
41967c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
41977c478bd9Sstevel@tonic-gate return (ENOTCONN);
41987c478bd9Sstevel@tonic-gate }
41997c478bd9Sstevel@tonic-gate /*
42007c478bd9Sstevel@tonic-gate * Ignore the address on connection-oriented sockets.
42017c478bd9Sstevel@tonic-gate * Just like BSD this code does not generate an error for
42027c478bd9Sstevel@tonic-gate * TCP (a CONNREQUIRED socket) when sending to an address
42037c478bd9Sstevel@tonic-gate * passed in with sendto/sendmsg. Instead the data is
42047c478bd9Sstevel@tonic-gate * delivered on the connection as if no address had been
42057c478bd9Sstevel@tonic-gate * supplied.
42067c478bd9Sstevel@tonic-gate */
42077c478bd9Sstevel@tonic-gate if ((so_state & SS_ISCONNECTED) &&
42087c478bd9Sstevel@tonic-gate !(so_mode & SM_CONNREQUIRED)) {
42097c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42107c478bd9Sstevel@tonic-gate return (EISCONN);
42117c478bd9Sstevel@tonic-gate }
42127c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISBOUND)) {
42137c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
42147c478bd9Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0,
42150f1702c5SYu Xiangning _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
42167c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
42177c478bd9Sstevel@tonic-gate if (error) {
42187c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42197c478bd9Sstevel@tonic-gate eprintsoline(so, error);
42207c478bd9Sstevel@tonic-gate return (error);
42217c478bd9Sstevel@tonic-gate }
42227c478bd9Sstevel@tonic-gate }
42237c478bd9Sstevel@tonic-gate /*
42247c478bd9Sstevel@tonic-gate * Handle delayed datagram errors. These are only queued
42257c478bd9Sstevel@tonic-gate * when the application sets SO_DGRAM_ERRIND.
42267c478bd9Sstevel@tonic-gate * Return the error if we are sending to the address
42277c478bd9Sstevel@tonic-gate * that was returned in the last T_UDERROR_IND.
42287c478bd9Sstevel@tonic-gate * If sending to some other address discard the delayed
42297c478bd9Sstevel@tonic-gate * error indication.
42307c478bd9Sstevel@tonic-gate */
42310f1702c5SYu Xiangning if (sti->sti_delayed_error) {
42327c478bd9Sstevel@tonic-gate struct T_uderror_ind *tudi;
42337c478bd9Sstevel@tonic-gate void *addr;
42347c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
42357c478bd9Sstevel@tonic-gate boolean_t match = B_FALSE;
42367c478bd9Sstevel@tonic-gate
42370f1702c5SYu Xiangning ASSERT(sti->sti_eaddr_mp);
42380f1702c5SYu Xiangning error = sti->sti_delayed_error;
42390f1702c5SYu Xiangning sti->sti_delayed_error = 0;
42400f1702c5SYu Xiangning tudi =
42410f1702c5SYu Xiangning (struct T_uderror_ind *)sti->sti_eaddr_mp->b_rptr;
42427c478bd9Sstevel@tonic-gate addrlen = tudi->DEST_length;
42430f1702c5SYu Xiangning addr = sogetoff(sti->sti_eaddr_mp,
42440f1702c5SYu Xiangning tudi->DEST_offset, addrlen, 1);
42457c478bd9Sstevel@tonic-gate ASSERT(addr); /* Checked by strsock_proto */
42467c478bd9Sstevel@tonic-gate switch (so->so_family) {
42477c478bd9Sstevel@tonic-gate case AF_INET: {
42487c478bd9Sstevel@tonic-gate /* Compare just IP address and port */
42497c478bd9Sstevel@tonic-gate sin_t *sin1 = (sin_t *)name;
42507c478bd9Sstevel@tonic-gate sin_t *sin2 = (sin_t *)addr;
42517c478bd9Sstevel@tonic-gate
42527c478bd9Sstevel@tonic-gate if (addrlen == sizeof (sin_t) &&
42537c478bd9Sstevel@tonic-gate namelen == addrlen &&
42547c478bd9Sstevel@tonic-gate sin1->sin_port == sin2->sin_port &&
42557c478bd9Sstevel@tonic-gate sin1->sin_addr.s_addr ==
42567c478bd9Sstevel@tonic-gate sin2->sin_addr.s_addr)
42577c478bd9Sstevel@tonic-gate match = B_TRUE;
42587c478bd9Sstevel@tonic-gate break;
42597c478bd9Sstevel@tonic-gate }
42607c478bd9Sstevel@tonic-gate case AF_INET6: {
42617c478bd9Sstevel@tonic-gate /* Compare just IP address and port. Not flow */
42627c478bd9Sstevel@tonic-gate sin6_t *sin1 = (sin6_t *)name;
42637c478bd9Sstevel@tonic-gate sin6_t *sin2 = (sin6_t *)addr;
42647c478bd9Sstevel@tonic-gate
42657c478bd9Sstevel@tonic-gate if (addrlen == sizeof (sin6_t) &&
42667c478bd9Sstevel@tonic-gate namelen == addrlen &&
42677c478bd9Sstevel@tonic-gate sin1->sin6_port == sin2->sin6_port &&
42687c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
4269fc80c0dfSnordmark &sin2->sin6_addr))
42707c478bd9Sstevel@tonic-gate match = B_TRUE;
42717c478bd9Sstevel@tonic-gate break;
42727c478bd9Sstevel@tonic-gate }
42737c478bd9Sstevel@tonic-gate case AF_UNIX:
42747c478bd9Sstevel@tonic-gate default:
42757c478bd9Sstevel@tonic-gate if (namelen == addrlen &&
42767c478bd9Sstevel@tonic-gate bcmp(name, addr, namelen) == 0)
42777c478bd9Sstevel@tonic-gate match = B_TRUE;
42787c478bd9Sstevel@tonic-gate }
42797c478bd9Sstevel@tonic-gate if (match) {
42800f1702c5SYu Xiangning freemsg(sti->sti_eaddr_mp);
42810f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
42827c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42837c478bd9Sstevel@tonic-gate #ifdef DEBUG
42847c478bd9Sstevel@tonic-gate dprintso(so, 0,
4285fc80c0dfSnordmark ("sockfs delayed error %d for %s\n",
4286fc80c0dfSnordmark error,
4287fc80c0dfSnordmark pr_addr(so->so_family, name, namelen)));
42887c478bd9Sstevel@tonic-gate #endif /* DEBUG */
42897c478bd9Sstevel@tonic-gate return (error);
42907c478bd9Sstevel@tonic-gate }
42910f1702c5SYu Xiangning freemsg(sti->sti_eaddr_mp);
42920f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
42937c478bd9Sstevel@tonic-gate }
42947c478bd9Sstevel@tonic-gate }
42957c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42967c478bd9Sstevel@tonic-gate
42977c478bd9Sstevel@tonic-gate dontroute = 0;
42987c478bd9Sstevel@tonic-gate if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
42997c478bd9Sstevel@tonic-gate uint32_t val;
43007c478bd9Sstevel@tonic-gate
43017c478bd9Sstevel@tonic-gate val = 1;
43027c478bd9Sstevel@tonic-gate error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
43030f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
43047c478bd9Sstevel@tonic-gate if (error)
43057c478bd9Sstevel@tonic-gate return (error);
43067c478bd9Sstevel@tonic-gate dontroute = 1;
43077c478bd9Sstevel@tonic-gate }
43087c478bd9Sstevel@tonic-gate
43097c478bd9Sstevel@tonic-gate if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
43107c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
43117c478bd9Sstevel@tonic-gate goto done;
43127c478bd9Sstevel@tonic-gate }
43137c478bd9Sstevel@tonic-gate if (msg->msg_controllen != 0) {
43147c478bd9Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) {
43150f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
43167c478bd9Sstevel@tonic-gate error = sosend_dgramcmsg(so, name, namelen, uiop,
4317ff550d0eSmasputra msg->msg_control, msg->msg_controllen, flags);
43187c478bd9Sstevel@tonic-gate } else {
43197c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
43207c478bd9Sstevel@tonic-gate /* Can't generate T_EXDATA_REQ with options */
43217c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
43227c478bd9Sstevel@tonic-gate goto done;
43237c478bd9Sstevel@tonic-gate }
43240f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
43257c478bd9Sstevel@tonic-gate error = sosend_svccmsg(so, uiop,
4326fc80c0dfSnordmark !(flags & MSG_EOR),
4327fc80c0dfSnordmark msg->msg_control, msg->msg_controllen,
4328fc80c0dfSnordmark flags);
43297c478bd9Sstevel@tonic-gate }
43307c478bd9Sstevel@tonic-gate goto done;
43317c478bd9Sstevel@tonic-gate }
43327c478bd9Sstevel@tonic-gate
43330f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
43347c478bd9Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) {
43357c478bd9Sstevel@tonic-gate /*
43367c478bd9Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately
4337ff550d0eSmasputra * from send_dgram. This can allow tail-call optimizations.
43387c478bd9Sstevel@tonic-gate */
43397c478bd9Sstevel@tonic-gate if (!dontroute) {
43407c478bd9Sstevel@tonic-gate return (sosend_dgram(so, name, namelen, uiop, flags));
43417c478bd9Sstevel@tonic-gate }
43427c478bd9Sstevel@tonic-gate error = sosend_dgram(so, name, namelen, uiop, flags);
43437c478bd9Sstevel@tonic-gate } else {
43447c478bd9Sstevel@tonic-gate t_scalar_t prim;
43457c478bd9Sstevel@tonic-gate int sflag;
43467c478bd9Sstevel@tonic-gate
43477c478bd9Sstevel@tonic-gate /* Ignore msg_name in the connected state */
43487c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
43497c478bd9Sstevel@tonic-gate prim = T_EXDATA_REQ;
43507c478bd9Sstevel@tonic-gate /*
43517c478bd9Sstevel@tonic-gate * Send down T_EXDATA_REQ even if there is flow
43527c478bd9Sstevel@tonic-gate * control for data.
43537c478bd9Sstevel@tonic-gate */
43547c478bd9Sstevel@tonic-gate sflag = MSG_IGNFLOW;
43557c478bd9Sstevel@tonic-gate } else {
43567c478bd9Sstevel@tonic-gate if (so_mode & SM_BYTESTREAM) {
43577c478bd9Sstevel@tonic-gate /* Byte stream transport - use write */
43587c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg: write\n"));
43590f1702c5SYu Xiangning
43600f1702c5SYu Xiangning /* Send M_DATA messages */
43617c478bd9Sstevel@tonic-gate /*
4362ff550d0eSmasputra * If there is no SO_DONTROUTE to turn off,
43630f1702c5SYu Xiangning * sti_direct is on, and there is no flow
4364ff550d0eSmasputra * control, we can take the fast path.
43657c478bd9Sstevel@tonic-gate */
43660f1702c5SYu Xiangning if (!dontroute && sti->sti_direct != 0 &&
4367ff550d0eSmasputra canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4368ff550d0eSmasputra return (sostream_direct(so, uiop,
43690f1702c5SYu Xiangning NULL, cr));
4370ff550d0eSmasputra }
43710f1702c5SYu Xiangning error = strwrite(SOTOV(so), uiop, cr);
43727c478bd9Sstevel@tonic-gate goto done;
43737c478bd9Sstevel@tonic-gate }
43747c478bd9Sstevel@tonic-gate prim = T_DATA_REQ;
43757c478bd9Sstevel@tonic-gate sflag = 0;
43767c478bd9Sstevel@tonic-gate }
43777c478bd9Sstevel@tonic-gate /*
43787c478bd9Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately
43797c478bd9Sstevel@tonic-gate * from sosend_svc. This can allow tail-call optimizations.
43807c478bd9Sstevel@tonic-gate */
43817c478bd9Sstevel@tonic-gate if (!dontroute)
43827c478bd9Sstevel@tonic-gate return (sosend_svc(so, uiop, prim,
4383fc80c0dfSnordmark !(flags & MSG_EOR), sflag));
43847c478bd9Sstevel@tonic-gate error = sosend_svc(so, uiop, prim,
4385fc80c0dfSnordmark !(flags & MSG_EOR), sflag);
43867c478bd9Sstevel@tonic-gate }
43877c478bd9Sstevel@tonic-gate ASSERT(dontroute);
43887c478bd9Sstevel@tonic-gate done:
43897c478bd9Sstevel@tonic-gate if (dontroute) {
43907c478bd9Sstevel@tonic-gate uint32_t val;
43917c478bd9Sstevel@tonic-gate
43927c478bd9Sstevel@tonic-gate val = 0;
43937c478bd9Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
43940f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
43957c478bd9Sstevel@tonic-gate }
43967c478bd9Sstevel@tonic-gate return (error);
43977c478bd9Sstevel@tonic-gate }
43987c478bd9Sstevel@tonic-gate
4399ff550d0eSmasputra /*
44000f1702c5SYu Xiangning * kstrwritemp() has very similar semantics as that of strwrite().
44010f1702c5SYu Xiangning * The main difference is it obtains mblks from the caller and also
44020f1702c5SYu Xiangning * does not do any copy as done in strwrite() from user buffers to
44030f1702c5SYu Xiangning * kernel buffers.
44040f1702c5SYu Xiangning *
44050f1702c5SYu Xiangning * Currently, this routine is used by sendfile to send data allocated
44060f1702c5SYu Xiangning * within the kernel without any copying. This interface does not use the
44070f1702c5SYu Xiangning * synchronous stream interface as synch. stream interface implies
44080f1702c5SYu Xiangning * copying.
4409ff550d0eSmasputra */
44100f1702c5SYu Xiangning int
kstrwritemp(struct vnode * vp,mblk_t * mp,ushort_t fmode)44110f1702c5SYu Xiangning kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode)
4412ff550d0eSmasputra {
44130f1702c5SYu Xiangning struct stdata *stp;
44140f1702c5SYu Xiangning struct queue *wqp;
44150f1702c5SYu Xiangning mblk_t *newmp;
44160f1702c5SYu Xiangning char waitflag;
44170f1702c5SYu Xiangning int tempmode;
44180f1702c5SYu Xiangning int error = 0;
44190f1702c5SYu Xiangning int done = 0;
44200f1702c5SYu Xiangning struct sonode *so;
44210f1702c5SYu Xiangning boolean_t direct;
44220f1702c5SYu Xiangning
44230f1702c5SYu Xiangning ASSERT(vp->v_stream);
44240f1702c5SYu Xiangning stp = vp->v_stream;
44250f1702c5SYu Xiangning
44260f1702c5SYu Xiangning so = VTOSO(vp);
44270f1702c5SYu Xiangning direct = _SOTOTPI(so)->sti_direct;
44280f1702c5SYu Xiangning
44290f1702c5SYu Xiangning /*
44300f1702c5SYu Xiangning * This is the sockfs direct fast path. canputnext() need
44310f1702c5SYu Xiangning * not be accurate so we don't grab the sd_lock here. If
44320f1702c5SYu Xiangning * we get flow-controlled, we grab sd_lock just before the
44330f1702c5SYu Xiangning * do..while loop below to emulate what strwrite() does.
44340f1702c5SYu Xiangning */
44350f1702c5SYu Xiangning wqp = stp->sd_wrq;
44360f1702c5SYu Xiangning if (canputnext(wqp) && direct &&
44370f1702c5SYu Xiangning !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
44380f1702c5SYu Xiangning return (sostream_direct(so, NULL, mp, CRED()));
44390f1702c5SYu Xiangning } else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
44400f1702c5SYu Xiangning /* Fast check of flags before acquiring the lock */
44410f1702c5SYu Xiangning mutex_enter(&stp->sd_lock);
44420f1702c5SYu Xiangning error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
44430f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
44440f1702c5SYu Xiangning if (error != 0) {
44450f1702c5SYu Xiangning if (!(stp->sd_flag & STPLEX) &&
44460f1702c5SYu Xiangning (stp->sd_wput_opt & SW_SIGPIPE)) {
44470f1702c5SYu Xiangning error = EPIPE;
44480f1702c5SYu Xiangning }
44490f1702c5SYu Xiangning return (error);
44500f1702c5SYu Xiangning }
44510f1702c5SYu Xiangning }
44520f1702c5SYu Xiangning
44530f1702c5SYu Xiangning waitflag = WRITEWAIT;
44540f1702c5SYu Xiangning if (stp->sd_flag & OLDNDELAY)
44550f1702c5SYu Xiangning tempmode = fmode & ~FNDELAY;
44560f1702c5SYu Xiangning else
44570f1702c5SYu Xiangning tempmode = fmode;
44580f1702c5SYu Xiangning
44590f1702c5SYu Xiangning mutex_enter(&stp->sd_lock);
44600f1702c5SYu Xiangning do {
44610f1702c5SYu Xiangning if (canputnext(wqp)) {
44620f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
44630f1702c5SYu Xiangning if (stp->sd_wputdatafunc != NULL) {
44640f1702c5SYu Xiangning newmp = (stp->sd_wputdatafunc)(vp, mp, NULL,
44650f1702c5SYu Xiangning NULL, NULL, NULL);
44660f1702c5SYu Xiangning if (newmp == NULL) {
44670f1702c5SYu Xiangning /* The caller will free mp */
44680f1702c5SYu Xiangning return (ECOMM);
44690f1702c5SYu Xiangning }
44700f1702c5SYu Xiangning mp = newmp;
44710f1702c5SYu Xiangning }
44720f1702c5SYu Xiangning putnext(wqp, mp);
44730f1702c5SYu Xiangning return (0);
44740f1702c5SYu Xiangning }
44750f1702c5SYu Xiangning error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1,
44760f1702c5SYu Xiangning &done);
44770f1702c5SYu Xiangning } while (error == 0 && !done);
44780f1702c5SYu Xiangning
44790f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
44800f1702c5SYu Xiangning /*
44810f1702c5SYu Xiangning * EAGAIN tells the application to try again. ENOMEM
44820f1702c5SYu Xiangning * is returned only if the memory allocation size
44830f1702c5SYu Xiangning * exceeds the physical limits of the system. ENOMEM
44840f1702c5SYu Xiangning * can't be true here.
44850f1702c5SYu Xiangning */
44860f1702c5SYu Xiangning if (error == ENOMEM)
44870f1702c5SYu Xiangning error = EAGAIN;
44880f1702c5SYu Xiangning return (error);
44890f1702c5SYu Xiangning }
44900f1702c5SYu Xiangning
44910f1702c5SYu Xiangning /* ARGSUSED */
44920f1702c5SYu Xiangning static int
sotpi_sendmblk(struct sonode * so,struct nmsghdr * msg,int fflag,struct cred * cr,mblk_t ** mpp)44930f1702c5SYu Xiangning sotpi_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
44940f1702c5SYu Xiangning struct cred *cr, mblk_t **mpp)
44950f1702c5SYu Xiangning {
44960f1702c5SYu Xiangning int error;
44970f1702c5SYu Xiangning
4498acb55917SPatrick Mooney switch (so->so_family) {
4499acb55917SPatrick Mooney case AF_INET:
4500acb55917SPatrick Mooney case AF_INET6:
4501acb55917SPatrick Mooney case AF_UNIX:
4502acb55917SPatrick Mooney break;
4503acb55917SPatrick Mooney default:
45040f1702c5SYu Xiangning return (EAFNOSUPPORT);
45050f1702c5SYu Xiangning
4506acb55917SPatrick Mooney }
4507acb55917SPatrick Mooney
45080f1702c5SYu Xiangning if (so->so_state & SS_CANTSENDMORE)
45090f1702c5SYu Xiangning return (EPIPE);
45100f1702c5SYu Xiangning
45110f1702c5SYu Xiangning if (so->so_type != SOCK_STREAM)
45120f1702c5SYu Xiangning return (EOPNOTSUPP);
45130f1702c5SYu Xiangning
45140f1702c5SYu Xiangning if ((so->so_state & SS_ISCONNECTED) == 0)
45150f1702c5SYu Xiangning return (ENOTCONN);
45160f1702c5SYu Xiangning
45170f1702c5SYu Xiangning error = kstrwritemp(so->so_vnode, *mpp, fflag);
45180f1702c5SYu Xiangning if (error == 0)
45190f1702c5SYu Xiangning *mpp = NULL;
45200f1702c5SYu Xiangning return (error);
45210f1702c5SYu Xiangning }
45220f1702c5SYu Xiangning
45230f1702c5SYu Xiangning /*
45240f1702c5SYu Xiangning * Sending data on a datagram socket.
45250f1702c5SYu Xiangning * Assumes caller has verified that SS_ISBOUND etc. are set.
45260f1702c5SYu Xiangning */
45270f1702c5SYu Xiangning /* ARGSUSED */
45280f1702c5SYu Xiangning static int
sodgram_direct(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,int flags)45290f1702c5SYu Xiangning sodgram_direct(struct sonode *so, struct sockaddr *name,
45300f1702c5SYu Xiangning socklen_t namelen, struct uio *uiop, int flags)
45310f1702c5SYu Xiangning {
45320f1702c5SYu Xiangning struct T_unitdata_req tudr;
45330f1702c5SYu Xiangning mblk_t *mp = NULL;
45340f1702c5SYu Xiangning int error = 0;
4535ff550d0eSmasputra void *addr;
4536ff550d0eSmasputra socklen_t addrlen;
4537ff550d0eSmasputra ssize_t len;
4538ff550d0eSmasputra struct stdata *stp = SOTOV(so)->v_stream;
4539ff550d0eSmasputra int so_state;
4540ff550d0eSmasputra queue_t *udp_wq;
4541fc80c0dfSnordmark boolean_t connected;
4542fc80c0dfSnordmark mblk_t *mpdata = NULL;
45430f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
4544005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
4545ff550d0eSmasputra
4546ff550d0eSmasputra ASSERT(name != NULL && namelen != 0);
4547ff550d0eSmasputra ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4548ff550d0eSmasputra ASSERT(!(so->so_mode & SM_EXDATA));
4549ff550d0eSmasputra ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4550ff550d0eSmasputra ASSERT(SOTOV(so)->v_type == VSOCK);
4551ff550d0eSmasputra
4552ff550d0eSmasputra /* Caller checked for proper length */
4553ff550d0eSmasputra len = uiop->uio_resid;
45540f1702c5SYu Xiangning ASSERT(len <= sti->sti_tidu_size);
4555ff550d0eSmasputra
4556ff550d0eSmasputra /* Length and family checks have been done by caller */
4557ff550d0eSmasputra ASSERT(name->sa_family == so->so_family);
4558ff550d0eSmasputra ASSERT(so->so_family == AF_INET ||
4559ff550d0eSmasputra (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4560ff550d0eSmasputra ASSERT(so->so_family == AF_INET6 ||
4561ff550d0eSmasputra (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4562ff550d0eSmasputra
4563ff550d0eSmasputra addr = name;
4564ff550d0eSmasputra addrlen = namelen;
4565ff550d0eSmasputra
4566ff550d0eSmasputra if (stp->sd_sidp != NULL &&
4567ff550d0eSmasputra (error = straccess(stp, JCWRITE)) != 0)
4568ff550d0eSmasputra goto done;
4569ff550d0eSmasputra
4570ff550d0eSmasputra so_state = so->so_state;
4571ff550d0eSmasputra
4572fc80c0dfSnordmark connected = so_state & SS_ISCONNECTED;
4573fc80c0dfSnordmark if (!connected) {
4574fc80c0dfSnordmark tudr.PRIM_type = T_UNITDATA_REQ;
4575fc80c0dfSnordmark tudr.DEST_length = addrlen;
4576fc80c0dfSnordmark tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
4577fc80c0dfSnordmark tudr.OPT_length = 0;
4578fc80c0dfSnordmark tudr.OPT_offset = 0;
4579fc80c0dfSnordmark
4580fc80c0dfSnordmark mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0,
4581de8c4a14SErik Nordmark _ALLOC_INTR, CRED());
4582fc80c0dfSnordmark if (mp == NULL) {
4583fc80c0dfSnordmark /*
4584fc80c0dfSnordmark * Caught a signal waiting for memory.
4585fc80c0dfSnordmark * Let send* return EINTR.
4586fc80c0dfSnordmark */
4587fc80c0dfSnordmark error = EINTR;
4588fc80c0dfSnordmark goto done;
4589fc80c0dfSnordmark }
4590fc80c0dfSnordmark }
4591fc80c0dfSnordmark
4592ff550d0eSmasputra /*
4593ff550d0eSmasputra * For UDP we don't break up the copyin into smaller pieces
4594ff550d0eSmasputra * as in the TCP case. That means if ENOMEM is returned by
4595ff550d0eSmasputra * mcopyinuio() then the uio vector has not been modified at
4596ff550d0eSmasputra * all and we fallback to either strwrite() or kstrputmsg()
4597ff550d0eSmasputra * below. Note also that we never generate priority messages
4598ff550d0eSmasputra * from here.
4599ff550d0eSmasputra */
4600ff550d0eSmasputra udp_wq = stp->sd_wrq->q_next;
4601ff550d0eSmasputra if (canput(udp_wq) &&
4602fc80c0dfSnordmark (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
4603fc80c0dfSnordmark ASSERT(DB_TYPE(mpdata) == M_DATA);
4604ff550d0eSmasputra ASSERT(uiop->uio_resid == 0);
4605fc80c0dfSnordmark if (!connected)
4606fc80c0dfSnordmark linkb(mp, mpdata);
4607fc80c0dfSnordmark else
4608fc80c0dfSnordmark mp = mpdata;
4609005d3febSMarek Pospisil if (auditing)
4610ff550d0eSmasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4611fc80c0dfSnordmark
461252aec5b9SDan McDonald /* Always returns 0... */
461352aec5b9SDan McDonald return (udp_wput(udp_wq, mp));
4614ff550d0eSmasputra }
4615fc80c0dfSnordmark
4616fc80c0dfSnordmark ASSERT(mpdata == NULL);
4617fc80c0dfSnordmark if (error != 0 && error != ENOMEM) {
4618fc80c0dfSnordmark freemsg(mp);
4619ff550d0eSmasputra return (error);
4620fc80c0dfSnordmark }
4621ff550d0eSmasputra
4622ff550d0eSmasputra /*
4623ff550d0eSmasputra * For connected, let strwrite() handle the blocking case.
4624ff550d0eSmasputra * Otherwise we fall thru and use kstrputmsg().
4625ff550d0eSmasputra */
4626fc80c0dfSnordmark if (connected)
4627ff550d0eSmasputra return (strwrite(SOTOV(so), uiop, CRED()));
4628ff550d0eSmasputra
4629005d3febSMarek Pospisil if (auditing)
4630ff550d0eSmasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4631ff550d0eSmasputra
4632ff550d0eSmasputra error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4633ff550d0eSmasputra done:
4634ff550d0eSmasputra #ifdef SOCK_DEBUG
4635ff550d0eSmasputra if (error != 0) {
4636ff550d0eSmasputra eprintsoline(so, error);
4637ff550d0eSmasputra }
4638ff550d0eSmasputra #endif /* SOCK_DEBUG */
4639ff550d0eSmasputra return (error);
4640ff550d0eSmasputra }
4641ff550d0eSmasputra
4642ff550d0eSmasputra int
sostream_direct(struct sonode * so,struct uio * uiop,mblk_t * mp,cred_t * cr)4643ff550d0eSmasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4644ff550d0eSmasputra {
4645ff550d0eSmasputra struct stdata *stp = SOTOV(so)->v_stream;
4646ff550d0eSmasputra ssize_t iosize, rmax, maxblk;
4647ff550d0eSmasputra queue_t *tcp_wq = stp->sd_wrq->q_next;
4648c28749e9Skais mblk_t *newmp;
4649ff550d0eSmasputra int error = 0, wflag = 0;
4650ff550d0eSmasputra
4651ff550d0eSmasputra ASSERT(so->so_mode & SM_BYTESTREAM);
4652ff550d0eSmasputra ASSERT(SOTOV(so)->v_type == VSOCK);
4653ff550d0eSmasputra
4654ff550d0eSmasputra if (stp->sd_sidp != NULL &&
4655ff550d0eSmasputra (error = straccess(stp, JCWRITE)) != 0)
4656ff550d0eSmasputra return (error);
4657ff550d0eSmasputra
4658ff550d0eSmasputra if (uiop == NULL) {
4659ff550d0eSmasputra /*
4660ff550d0eSmasputra * kstrwritemp() should have checked sd_flag and
4661ff550d0eSmasputra * flow-control before coming here. If we end up
4662ff550d0eSmasputra * here it means that we can simply pass down the
4663ff550d0eSmasputra * data to tcp.
4664ff550d0eSmasputra */
4665ff550d0eSmasputra ASSERT(mp != NULL);
4666c28749e9Skais if (stp->sd_wputdatafunc != NULL) {
4667c28749e9Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4668c28749e9Skais NULL, NULL, NULL);
4669c28749e9Skais if (newmp == NULL) {
4670c28749e9Skais /* The caller will free mp */
4671c28749e9Skais return (ECOMM);
4672c28749e9Skais }
4673c28749e9Skais mp = newmp;
4674c28749e9Skais }
467552aec5b9SDan McDonald /* Always returns 0... */
467652aec5b9SDan McDonald return (tcp_wput(tcp_wq, mp));
4677ff550d0eSmasputra }
4678ff550d0eSmasputra
4679ff550d0eSmasputra /* Fallback to strwrite() to do proper error handling */
4680ff550d0eSmasputra if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4681ff550d0eSmasputra return (strwrite(SOTOV(so), uiop, cr));
4682ff550d0eSmasputra
4683ff550d0eSmasputra rmax = stp->sd_qn_maxpsz;
4684ff550d0eSmasputra ASSERT(rmax >= 0 || rmax == INFPSZ);
4685ff550d0eSmasputra if (rmax == 0 || uiop->uio_resid <= 0)
4686ff550d0eSmasputra return (0);
4687ff550d0eSmasputra
4688ff550d0eSmasputra if (rmax == INFPSZ)
4689ff550d0eSmasputra rmax = uiop->uio_resid;
4690ff550d0eSmasputra
4691ff550d0eSmasputra maxblk = stp->sd_maxblk;
4692ff550d0eSmasputra
4693ff550d0eSmasputra for (;;) {
4694ff550d0eSmasputra iosize = MIN(uiop->uio_resid, rmax);
4695ff550d0eSmasputra
4696ff550d0eSmasputra mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4697ff550d0eSmasputra if (mp == NULL) {
4698ff550d0eSmasputra /*
4699ff550d0eSmasputra * Fallback to strwrite() for ENOMEM; if this
4700ff550d0eSmasputra * is our first time in this routine and the uio
4701ff550d0eSmasputra * vector has not been modified, we will end up
4702ff550d0eSmasputra * calling strwrite() without any flag set.
4703ff550d0eSmasputra */
4704ff550d0eSmasputra if (error == ENOMEM)
4705ff550d0eSmasputra goto slow_send;
4706ff550d0eSmasputra else
4707ff550d0eSmasputra return (error);
4708ff550d0eSmasputra }
4709ff550d0eSmasputra ASSERT(uiop->uio_resid >= 0);
4710ff550d0eSmasputra /*
4711ff550d0eSmasputra * If mp is non-NULL and ENOMEM is set, it means that
4712ff550d0eSmasputra * mcopyinuio() was able to break down some of the user
4713ff550d0eSmasputra * data into one or more mblks. Send the partial data
4714ff550d0eSmasputra * to tcp and let the rest be handled in strwrite().
4715ff550d0eSmasputra */
4716ff550d0eSmasputra ASSERT(error == 0 || error == ENOMEM);
4717c28749e9Skais if (stp->sd_wputdatafunc != NULL) {
4718c28749e9Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4719c28749e9Skais NULL, NULL, NULL);
4720c28749e9Skais if (newmp == NULL) {
4721c28749e9Skais /* The caller will free mp */
4722c28749e9Skais return (ECOMM);
4723c28749e9Skais }
4724c28749e9Skais mp = newmp;
4725c28749e9Skais }
472652aec5b9SDan McDonald (void) tcp_wput(tcp_wq, mp); /* Always returns 0 anyway. */
4727ff550d0eSmasputra
4728ff550d0eSmasputra wflag |= NOINTR;
4729ff550d0eSmasputra
4730ff550d0eSmasputra if (uiop->uio_resid == 0) { /* No more data; we're done */
4731ff550d0eSmasputra ASSERT(error == 0);
4732ff550d0eSmasputra break;
4733ff550d0eSmasputra } else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4734ff550d0eSmasputra (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4735ff550d0eSmasputra slow_send:
4736ff550d0eSmasputra /*
4737ff550d0eSmasputra * We were able to send down partial data using
4738ff550d0eSmasputra * the direct call interface, but are now relying
4739ff550d0eSmasputra * on strwrite() to handle the non-fastpath cases.
4740ff550d0eSmasputra * If the socket is blocking we will sleep in
4741ff550d0eSmasputra * strwaitq() until write is permitted, otherwise,
4742ff550d0eSmasputra * we will need to return the amount of bytes
4743ff550d0eSmasputra * written so far back to the app. This is the
4744ff550d0eSmasputra * reason why we pass NOINTR flag to strwrite()
4745ff550d0eSmasputra * for non-blocking socket, because we don't want
4746ff550d0eSmasputra * to return EAGAIN when portion of the user data
4747ff550d0eSmasputra * has actually been sent down.
4748ff550d0eSmasputra */
4749ff550d0eSmasputra return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4750ff550d0eSmasputra }
4751ff550d0eSmasputra }
4752ff550d0eSmasputra return (0);
4753ff550d0eSmasputra }
4754ff550d0eSmasputra
47557c478bd9Sstevel@tonic-gate /*
47560f1702c5SYu Xiangning * Update sti_faddr by asking the transport (unless AF_UNIX).
47577c478bd9Sstevel@tonic-gate */
47580f1702c5SYu Xiangning /* ARGSUSED */
47597c478bd9Sstevel@tonic-gate int
sotpi_getpeername(struct sonode * so,struct sockaddr * name,socklen_t * namelen,boolean_t accept,struct cred * cr)47600f1702c5SYu Xiangning sotpi_getpeername(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
47610f1702c5SYu Xiangning boolean_t accept, struct cred *cr)
47627c478bd9Sstevel@tonic-gate {
47637c478bd9Sstevel@tonic-gate struct strbuf strbuf;
47647c478bd9Sstevel@tonic-gate int error = 0, res;
47657c478bd9Sstevel@tonic-gate void *addr;
47667c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
47677c478bd9Sstevel@tonic-gate k_sigset_t smask;
47680f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
47697c478bd9Sstevel@tonic-gate
47707c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
4771903a11ebSrh (void *)so, pr_state(so->so_state, so->so_mode)));
47727c478bd9Sstevel@tonic-gate
47730f1702c5SYu Xiangning ASSERT(*namelen > 0);
47747c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
47757c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
47760f1702c5SYu Xiangning
47770f1702c5SYu Xiangning if (accept) {
47780f1702c5SYu Xiangning bcopy(sti->sti_faddr_sa, name,
47790f1702c5SYu Xiangning MIN(*namelen, sti->sti_faddr_len));
47800f1702c5SYu Xiangning *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
47810f1702c5SYu Xiangning goto done;
47820f1702c5SYu Xiangning }
47830f1702c5SYu Xiangning
47847c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) {
47857c478bd9Sstevel@tonic-gate error = ENOTCONN;
47867c478bd9Sstevel@tonic-gate goto done;
47877c478bd9Sstevel@tonic-gate }
47887c478bd9Sstevel@tonic-gate /* Added this check for X/Open */
47897c478bd9Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
47907c478bd9Sstevel@tonic-gate error = EINVAL;
47917c478bd9Sstevel@tonic-gate if (xnet_check_print) {
47927c478bd9Sstevel@tonic-gate printf("sockfs: X/Open getpeername check => EINVAL\n");
47937c478bd9Sstevel@tonic-gate }
47947c478bd9Sstevel@tonic-gate goto done;
47957c478bd9Sstevel@tonic-gate }
47960f1702c5SYu Xiangning
47970f1702c5SYu Xiangning if (sti->sti_faddr_valid) {
47980f1702c5SYu Xiangning bcopy(sti->sti_faddr_sa, name,
47990f1702c5SYu Xiangning MIN(*namelen, sti->sti_faddr_len));
48000f1702c5SYu Xiangning *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
48010f1702c5SYu Xiangning goto done;
48020f1702c5SYu Xiangning }
48030f1702c5SYu Xiangning
48047c478bd9Sstevel@tonic-gate #ifdef DEBUG
48057c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
48060f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_faddr_sa,
48070f1702c5SYu Xiangning (t_uscalar_t)sti->sti_faddr_len)));
48087c478bd9Sstevel@tonic-gate #endif /* DEBUG */
48097c478bd9Sstevel@tonic-gate
48102caf0dcdSrshoaib if (so->so_family == AF_UNIX) {
48117c478bd9Sstevel@tonic-gate /* Transport has different name space - return local info */
48120f1702c5SYu Xiangning if (sti->sti_faddr_noxlate)
48130f1702c5SYu Xiangning *namelen = 0;
48147c478bd9Sstevel@tonic-gate error = 0;
48157c478bd9Sstevel@tonic-gate goto done;
48167c478bd9Sstevel@tonic-gate }
48177c478bd9Sstevel@tonic-gate
48180f1702c5SYu Xiangning ASSERT(so->so_family != AF_UNIX && sti->sti_faddr_noxlate == 0);
48190f1702c5SYu Xiangning
48200f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa);
48217c478bd9Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */
48220f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_maxlen;
48237c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
48247c478bd9Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP);
48257c478bd9Sstevel@tonic-gate
48267c478bd9Sstevel@tonic-gate /*
48277c478bd9Sstevel@tonic-gate * Issue TI_GETPEERNAME with signals masked.
48280f1702c5SYu Xiangning * Put the result in sti_faddr_sa so that getpeername works after
48297c478bd9Sstevel@tonic-gate * a shutdown(output).
48307c478bd9Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
48317c478bd9Sstevel@tonic-gate * back to the socket.
48327c478bd9Sstevel@tonic-gate */
48337c478bd9Sstevel@tonic-gate strbuf.buf = addr;
48347c478bd9Sstevel@tonic-gate strbuf.maxlen = addrlen;
48357c478bd9Sstevel@tonic-gate strbuf.len = 0;
48367c478bd9Sstevel@tonic-gate
48377c478bd9Sstevel@tonic-gate sigintr(&smask, 0);
48387c478bd9Sstevel@tonic-gate res = 0;
48390f1702c5SYu Xiangning ASSERT(cr);
48407c478bd9Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
48410f1702c5SYu Xiangning 0, K_TO_K, cr, &res);
48427c478bd9Sstevel@tonic-gate sigunintr(&smask);
48437c478bd9Sstevel@tonic-gate
48447c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
48457c478bd9Sstevel@tonic-gate /*
48467c478bd9Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail
48477c478bd9Sstevel@tonic-gate * the getpeername. Instead fallback on the recorded
48480f1702c5SYu Xiangning * sti->sti_faddr_sa.
48497c478bd9Sstevel@tonic-gate */
48507c478bd9Sstevel@tonic-gate if (error) {
48517c478bd9Sstevel@tonic-gate /*
48527c478bd9Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl.
48537c478bd9Sstevel@tonic-gate * However, it is impossible to determine which ones of
48547c478bd9Sstevel@tonic-gate * these are really socket level errors that were incorrectly
48557c478bd9Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the
48567c478bd9Sstevel@tonic-gate * error - to code explicitly does not reinstate the error
48577c478bd9Sstevel@tonic-gate * using soseterror().
48587c478bd9Sstevel@tonic-gate * Experiments have shows that at least this set of
48597c478bd9Sstevel@tonic-gate * errors are reported and should not be reinstated on the
48607c478bd9Sstevel@tonic-gate * socket:
48617c478bd9Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when
48627c478bd9Sstevel@tonic-gate * getpeername was called.
48637c478bd9Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write
48647c478bd9Sstevel@tonic-gate * side error over the read side error.
48657c478bd9Sstevel@tonic-gate * ENOTCONN The transport just got disconnected but
48667c478bd9Sstevel@tonic-gate * sockfs had not yet seen the T_DISCON_IND
48677c478bd9Sstevel@tonic-gate * when issuing the ioctl.
48687c478bd9Sstevel@tonic-gate */
48697c478bd9Sstevel@tonic-gate error = 0;
48707c478bd9Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 &&
48717c478bd9Sstevel@tonic-gate (so->so_state & SS_ISCONNECTED)) {
48720f1702c5SYu Xiangning ASSERT(strbuf.len <= (int)sti->sti_faddr_maxlen);
48730f1702c5SYu Xiangning sti->sti_faddr_len = (socklen_t)strbuf.len;
48740f1702c5SYu Xiangning bcopy(addr, sti->sti_faddr_sa, sti->sti_faddr_len);
48750f1702c5SYu Xiangning sti->sti_faddr_valid = 1;
48760f1702c5SYu Xiangning
48770f1702c5SYu Xiangning bcopy(addr, name, MIN(*namelen, sti->sti_faddr_len));
48780f1702c5SYu Xiangning *namelen = sti->sti_faddr_len;
48797c478bd9Sstevel@tonic-gate }
48807c478bd9Sstevel@tonic-gate kmem_free(addr, addrlen);
48817c478bd9Sstevel@tonic-gate #ifdef DEBUG
48827c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
48830f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_faddr_sa,
48840f1702c5SYu Xiangning (t_uscalar_t)sti->sti_faddr_len)));
48857c478bd9Sstevel@tonic-gate #endif /* DEBUG */
48867c478bd9Sstevel@tonic-gate done:
48877c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
48887c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
48897c478bd9Sstevel@tonic-gate return (error);
48907c478bd9Sstevel@tonic-gate }
48917c478bd9Sstevel@tonic-gate
48927c478bd9Sstevel@tonic-gate /*
48930f1702c5SYu Xiangning * Update sti_laddr by asking the transport (unless AF_UNIX).
48947c478bd9Sstevel@tonic-gate */
48957c478bd9Sstevel@tonic-gate int
sotpi_getsockname(struct sonode * so,struct sockaddr * name,socklen_t * namelen,struct cred * cr)48960f1702c5SYu Xiangning sotpi_getsockname(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
48970f1702c5SYu Xiangning struct cred *cr)
48987c478bd9Sstevel@tonic-gate {
48997c478bd9Sstevel@tonic-gate struct strbuf strbuf;
49007c478bd9Sstevel@tonic-gate int error = 0, res;
49017c478bd9Sstevel@tonic-gate void *addr;
49027c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
49037c478bd9Sstevel@tonic-gate k_sigset_t smask;
49040f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
49057c478bd9Sstevel@tonic-gate
49067c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
4907903a11ebSrh (void *)so, pr_state(so->so_state, so->so_mode)));
49087c478bd9Sstevel@tonic-gate
49090f1702c5SYu Xiangning ASSERT(*namelen > 0);
49107c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
49117c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
49120f1702c5SYu Xiangning
49137c478bd9Sstevel@tonic-gate #ifdef DEBUG
49140f1702c5SYu Xiangning
49157c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
49160f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_laddr_sa,
49170f1702c5SYu Xiangning (t_uscalar_t)sti->sti_laddr_len)));
49187c478bd9Sstevel@tonic-gate #endif /* DEBUG */
49190f1702c5SYu Xiangning if (sti->sti_laddr_valid) {
49200f1702c5SYu Xiangning bcopy(sti->sti_laddr_sa, name,
49210f1702c5SYu Xiangning MIN(*namelen, sti->sti_laddr_len));
49220f1702c5SYu Xiangning *namelen = sti->sti_laddr_len;
49230f1702c5SYu Xiangning goto done;
49240f1702c5SYu Xiangning }
49250f1702c5SYu Xiangning
49267c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
4927dfc0fed8SRobert Mustacchi /*
4928dfc0fed8SRobert Mustacchi * Transport has different name space - return local info. If we
4929dfc0fed8SRobert Mustacchi * have enough space, let consumers know the family.
4930dfc0fed8SRobert Mustacchi */
4931dfc0fed8SRobert Mustacchi if (*namelen >= sizeof (sa_family_t)) {
4932dfc0fed8SRobert Mustacchi name->sa_family = AF_UNIX;
4933dfc0fed8SRobert Mustacchi *namelen = sizeof (sa_family_t);
4934dfc0fed8SRobert Mustacchi } else {
4935dfc0fed8SRobert Mustacchi *namelen = 0;
4936dfc0fed8SRobert Mustacchi }
49377c478bd9Sstevel@tonic-gate error = 0;
49387c478bd9Sstevel@tonic-gate goto done;
49397c478bd9Sstevel@tonic-gate }
494018cbc865Sblu if (!(so->so_state & SS_ISBOUND)) {
494118cbc865Sblu /* If not bound, then nothing to return. */
494218cbc865Sblu error = 0;
494318cbc865Sblu goto done;
494418cbc865Sblu }
49450f1702c5SYu Xiangning
49467c478bd9Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */
49470f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_laddr_maxlen;
49487c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
49497c478bd9Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP);
49507c478bd9Sstevel@tonic-gate
49517c478bd9Sstevel@tonic-gate /*
49527c478bd9Sstevel@tonic-gate * Issue TI_GETMYNAME with signals masked.
49530f1702c5SYu Xiangning * Put the result in sti_laddr_sa so that getsockname works after
49547c478bd9Sstevel@tonic-gate * a shutdown(output).
49557c478bd9Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
49567c478bd9Sstevel@tonic-gate * back to the socket.
49577c478bd9Sstevel@tonic-gate */
49587c478bd9Sstevel@tonic-gate strbuf.buf = addr;
49597c478bd9Sstevel@tonic-gate strbuf.maxlen = addrlen;
49607c478bd9Sstevel@tonic-gate strbuf.len = 0;
49617c478bd9Sstevel@tonic-gate
49627c478bd9Sstevel@tonic-gate sigintr(&smask, 0);
49637c478bd9Sstevel@tonic-gate res = 0;
49640f1702c5SYu Xiangning ASSERT(cr);
49657c478bd9Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
49660f1702c5SYu Xiangning 0, K_TO_K, cr, &res);
49677c478bd9Sstevel@tonic-gate sigunintr(&smask);
49687c478bd9Sstevel@tonic-gate
49697c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
49707c478bd9Sstevel@tonic-gate /*
49717c478bd9Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail
49727c478bd9Sstevel@tonic-gate * the getsockname. Instead fallback on the recorded
49730f1702c5SYu Xiangning * sti->sti_laddr_sa.
49747c478bd9Sstevel@tonic-gate */
49757c478bd9Sstevel@tonic-gate if (error) {
49767c478bd9Sstevel@tonic-gate /*
49777c478bd9Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl.
49787c478bd9Sstevel@tonic-gate * However, it is impossible to determine which ones of
49797c478bd9Sstevel@tonic-gate * these are really socket level errors that were incorrectly
49807c478bd9Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the
49817c478bd9Sstevel@tonic-gate * error - to code explicitly does not reinstate the error
49827c478bd9Sstevel@tonic-gate * using soseterror().
49837c478bd9Sstevel@tonic-gate * Experiments have shows that at least this set of
49847c478bd9Sstevel@tonic-gate * errors are reported and should not be reinstated on the
49857c478bd9Sstevel@tonic-gate * socket:
49867c478bd9Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when
49877c478bd9Sstevel@tonic-gate * getsockname was called.
49887c478bd9Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write
49897c478bd9Sstevel@tonic-gate * side error over the read side error.
49907c478bd9Sstevel@tonic-gate */
49917c478bd9Sstevel@tonic-gate error = 0;
49927c478bd9Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 &&
49937c478bd9Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) {
49940f1702c5SYu Xiangning ASSERT(strbuf.len <= (int)sti->sti_laddr_maxlen);
49950f1702c5SYu Xiangning sti->sti_laddr_len = (socklen_t)strbuf.len;
49960f1702c5SYu Xiangning bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
49970f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
49980f1702c5SYu Xiangning
49990f1702c5SYu Xiangning bcopy(addr, name, MIN(sti->sti_laddr_len, *namelen));
50000f1702c5SYu Xiangning *namelen = sti->sti_laddr_len;
50017c478bd9Sstevel@tonic-gate }
50027c478bd9Sstevel@tonic-gate kmem_free(addr, addrlen);
50037c478bd9Sstevel@tonic-gate #ifdef DEBUG
50047c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
50050f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_laddr_sa,
50060f1702c5SYu Xiangning (t_uscalar_t)sti->sti_laddr_len)));
50077c478bd9Sstevel@tonic-gate #endif /* DEBUG */
50087c478bd9Sstevel@tonic-gate done:
50097c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
50107c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
50117c478bd9Sstevel@tonic-gate return (error);
50127c478bd9Sstevel@tonic-gate }
50137c478bd9Sstevel@tonic-gate
50147c478bd9Sstevel@tonic-gate /*
50157c478bd9Sstevel@tonic-gate * Get socket options. For SOL_SOCKET options some options are handled
50167c478bd9Sstevel@tonic-gate * by the sockfs while others use the value recorded in the sonode as a
50177c478bd9Sstevel@tonic-gate * fallback should the T_SVR4_OPTMGMT_REQ fail.
50187c478bd9Sstevel@tonic-gate *
50197c478bd9Sstevel@tonic-gate * On the return most *optlenp bytes are copied to optval.
50207c478bd9Sstevel@tonic-gate */
50210f1702c5SYu Xiangning /* ARGSUSED */
50227c478bd9Sstevel@tonic-gate int
sotpi_getsockopt(struct sonode * so,int level,int option_name,void * optval,socklen_t * optlenp,int flags,struct cred * cr)50237c478bd9Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name,
5024f012ee0cSGordon Ross void *optval, socklen_t *optlenp, int flags, struct cred *cr)
50257c478bd9Sstevel@tonic-gate {
50267c478bd9Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req;
50277c478bd9Sstevel@tonic-gate struct T_optmgmt_ack *optmgmt_ack;
50287c478bd9Sstevel@tonic-gate struct opthdr oh;
50297c478bd9Sstevel@tonic-gate struct opthdr *opt_res;
50307c478bd9Sstevel@tonic-gate mblk_t *mp = NULL;
50317c478bd9Sstevel@tonic-gate int error = 0;
50327c478bd9Sstevel@tonic-gate void *option = NULL; /* Set if fallback value */
50337c478bd9Sstevel@tonic-gate t_uscalar_t maxlen = *optlenp;
50347c478bd9Sstevel@tonic-gate t_uscalar_t len;
50357c478bd9Sstevel@tonic-gate uint32_t value;
50360f1702c5SYu Xiangning struct timeval tmo_val; /* used for SO_RCVTIMEO, SO_SNDTIMEO */
503722238f73Sshenjian struct timeval32 tmo_val32;
50380f1702c5SYu Xiangning struct so_snd_bufinfo snd_bufinfo; /* used for zero copy */
50397c478bd9Sstevel@tonic-gate
50407c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
5041903a11ebSrh (void *)so, level, option_name, optval, (void *)optlenp,
5042fc80c0dfSnordmark pr_state(so->so_state, so->so_mode)));
50437c478bd9Sstevel@tonic-gate
50447c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
50457c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
50467c478bd9Sstevel@tonic-gate
50477c478bd9Sstevel@tonic-gate /*
50487c478bd9Sstevel@tonic-gate * Check for SOL_SOCKET options.
50497c478bd9Sstevel@tonic-gate * Certain SOL_SOCKET options are returned directly whereas
50507c478bd9Sstevel@tonic-gate * others only provide a default (fallback) value should
50517c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ fail.
50527c478bd9Sstevel@tonic-gate */
50537c478bd9Sstevel@tonic-gate if (level == SOL_SOCKET) {
50547c478bd9Sstevel@tonic-gate /* Check parameters */
50557c478bd9Sstevel@tonic-gate switch (option_name) {
50567c478bd9Sstevel@tonic-gate case SO_TYPE:
50577c478bd9Sstevel@tonic-gate case SO_ERROR:
50587c478bd9Sstevel@tonic-gate case SO_DEBUG:
50597c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
50607c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
50617c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
50627c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
50637c478bd9Sstevel@tonic-gate case SO_BROADCAST:
50647c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
50657c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
50667c478bd9Sstevel@tonic-gate case SO_SNDBUF:
50677c478bd9Sstevel@tonic-gate case SO_RCVBUF:
50687c478bd9Sstevel@tonic-gate #ifdef notyet
50697c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
50707c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
50717c478bd9Sstevel@tonic-gate #endif /* notyet */
507288cda078Skcpoon case SO_DOMAIN:
50737c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
50747c478bd9Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
50757c478bd9Sstevel@tonic-gate error = EINVAL;
50767c478bd9Sstevel@tonic-gate eprintsoline(so, error);
50777c478bd9Sstevel@tonic-gate goto done2;
50787c478bd9Sstevel@tonic-gate }
50797c478bd9Sstevel@tonic-gate break;
50800f1702c5SYu Xiangning case SO_RCVTIMEO:
50810f1702c5SYu Xiangning case SO_SNDTIMEO:
5082e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5083e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
508422238f73Sshenjian if (maxlen < sizeof (struct timeval)) {
508522238f73Sshenjian error = EINVAL;
508622238f73Sshenjian eprintsoline(so, error);
508722238f73Sshenjian goto done2;
508822238f73Sshenjian }
508922238f73Sshenjian } else {
509022238f73Sshenjian if (maxlen < sizeof (struct timeval32)) {
509122238f73Sshenjian error = EINVAL;
509222238f73Sshenjian eprintsoline(so, error);
509322238f73Sshenjian goto done2;
509422238f73Sshenjian }
509522238f73Sshenjian
50960f1702c5SYu Xiangning }
50970f1702c5SYu Xiangning break;
50987c478bd9Sstevel@tonic-gate case SO_LINGER:
50997c478bd9Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
51007c478bd9Sstevel@tonic-gate error = EINVAL;
51017c478bd9Sstevel@tonic-gate eprintsoline(so, error);
51027c478bd9Sstevel@tonic-gate goto done2;
51037c478bd9Sstevel@tonic-gate }
51047c478bd9Sstevel@tonic-gate break;
51050f1702c5SYu Xiangning case SO_SND_BUFINFO:
51060f1702c5SYu Xiangning if (maxlen < (t_uscalar_t)
51070f1702c5SYu Xiangning sizeof (struct so_snd_bufinfo)) {
51080f1702c5SYu Xiangning error = EINVAL;
51090f1702c5SYu Xiangning eprintsoline(so, error);
51100f1702c5SYu Xiangning goto done2;
51110f1702c5SYu Xiangning }
51120f1702c5SYu Xiangning break;
51137c478bd9Sstevel@tonic-gate }
51147c478bd9Sstevel@tonic-gate
51157c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (uint32_t); /* Default */
51167c478bd9Sstevel@tonic-gate
51177c478bd9Sstevel@tonic-gate switch (option_name) {
51187c478bd9Sstevel@tonic-gate case SO_TYPE:
51197c478bd9Sstevel@tonic-gate value = so->so_type;
51207c478bd9Sstevel@tonic-gate option = &value;
51217c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51227c478bd9Sstevel@tonic-gate
51237c478bd9Sstevel@tonic-gate case SO_ERROR:
51240f1702c5SYu Xiangning value = sogeterr(so, B_TRUE);
51257c478bd9Sstevel@tonic-gate option = &value;
51267c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51277c478bd9Sstevel@tonic-gate
51287c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
51297c478bd9Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN)
51307c478bd9Sstevel@tonic-gate value = SO_ACCEPTCONN;
51317c478bd9Sstevel@tonic-gate else
51327c478bd9Sstevel@tonic-gate value = 0;
51337c478bd9Sstevel@tonic-gate #ifdef DEBUG
51347c478bd9Sstevel@tonic-gate if (value) {
51357c478bd9Sstevel@tonic-gate dprintso(so, 1,
51367c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n",
51377c478bd9Sstevel@tonic-gate option_name));
51387c478bd9Sstevel@tonic-gate } else {
51397c478bd9Sstevel@tonic-gate dprintso(so, 1,
51407c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n",
51417c478bd9Sstevel@tonic-gate option_name));
51427c478bd9Sstevel@tonic-gate }
51437c478bd9Sstevel@tonic-gate #endif /* DEBUG */
51447c478bd9Sstevel@tonic-gate option = &value;
51457c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51467c478bd9Sstevel@tonic-gate
51477c478bd9Sstevel@tonic-gate case SO_DEBUG:
51487c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
51497c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
51507c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
51517c478bd9Sstevel@tonic-gate case SO_BROADCAST:
51527c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
51537c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
51547c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
51557c478bd9Sstevel@tonic-gate value = (so->so_options & option_name);
51567c478bd9Sstevel@tonic-gate #ifdef DEBUG
51577c478bd9Sstevel@tonic-gate if (value) {
51587c478bd9Sstevel@tonic-gate dprintso(so, 1,
51597c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n",
51607c478bd9Sstevel@tonic-gate option_name));
51617c478bd9Sstevel@tonic-gate } else {
51627c478bd9Sstevel@tonic-gate dprintso(so, 1,
51637c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n",
51647c478bd9Sstevel@tonic-gate option_name));
51657c478bd9Sstevel@tonic-gate }
51667c478bd9Sstevel@tonic-gate #endif /* DEBUG */
51677c478bd9Sstevel@tonic-gate option = &value;
51687c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51697c478bd9Sstevel@tonic-gate
51707c478bd9Sstevel@tonic-gate /*
51717c478bd9Sstevel@tonic-gate * The following options are only returned by sockfs when the
51727c478bd9Sstevel@tonic-gate * T_SVR4_OPTMGMT_REQ fails.
51737c478bd9Sstevel@tonic-gate */
51747c478bd9Sstevel@tonic-gate case SO_LINGER:
51757c478bd9Sstevel@tonic-gate option = &so->so_linger;
51767c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (struct linger);
51777c478bd9Sstevel@tonic-gate break;
51787c478bd9Sstevel@tonic-gate case SO_SNDBUF: {
51797c478bd9Sstevel@tonic-gate ssize_t lvalue;
51807c478bd9Sstevel@tonic-gate
51817c478bd9Sstevel@tonic-gate /*
51827c478bd9Sstevel@tonic-gate * If the option has not been set then get a default
51837c478bd9Sstevel@tonic-gate * value from the read queue. This value is
51847c478bd9Sstevel@tonic-gate * returned if the transport fails
51857c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ.
51867c478bd9Sstevel@tonic-gate */
51877c478bd9Sstevel@tonic-gate lvalue = so->so_sndbuf;
51887c478bd9Sstevel@tonic-gate if (lvalue == 0) {
51897c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
51907c478bd9Sstevel@tonic-gate (void) strqget(strvp2wq(SOTOV(so))->q_next,
5191fc80c0dfSnordmark QHIWAT, 0, &lvalue);
51927c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
51937c478bd9Sstevel@tonic-gate dprintso(so, 1,
51947c478bd9Sstevel@tonic-gate ("got SO_SNDBUF %ld from q\n", lvalue));
51957c478bd9Sstevel@tonic-gate }
51967c478bd9Sstevel@tonic-gate value = (int)lvalue;
51977c478bd9Sstevel@tonic-gate option = &value;
51987c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_sndbuf);
51997c478bd9Sstevel@tonic-gate break;
52007c478bd9Sstevel@tonic-gate }
52017c478bd9Sstevel@tonic-gate case SO_RCVBUF: {
52027c478bd9Sstevel@tonic-gate ssize_t lvalue;
52037c478bd9Sstevel@tonic-gate
52047c478bd9Sstevel@tonic-gate /*
52057c478bd9Sstevel@tonic-gate * If the option has not been set then get a default
52067c478bd9Sstevel@tonic-gate * value from the read queue. This value is
52077c478bd9Sstevel@tonic-gate * returned if the transport fails
52087c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ.
52097c478bd9Sstevel@tonic-gate *
52107c478bd9Sstevel@tonic-gate * XXX If SO_RCVBUF has been set and this is an
52117c478bd9Sstevel@tonic-gate * XPG 4.2 application then do not ask the transport
52127c478bd9Sstevel@tonic-gate * since the transport might adjust the value and not
52137c478bd9Sstevel@tonic-gate * return exactly what was set by the application.
52147c478bd9Sstevel@tonic-gate * For non-XPG 4.2 application we return the value
52157c478bd9Sstevel@tonic-gate * that the transport is actually using.
52167c478bd9Sstevel@tonic-gate */
52177c478bd9Sstevel@tonic-gate lvalue = so->so_rcvbuf;
52187c478bd9Sstevel@tonic-gate if (lvalue == 0) {
52197c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
52207c478bd9Sstevel@tonic-gate (void) strqget(RD(strvp2wq(SOTOV(so))),
5221fc80c0dfSnordmark QHIWAT, 0, &lvalue);
52227c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
52237c478bd9Sstevel@tonic-gate dprintso(so, 1,
52247c478bd9Sstevel@tonic-gate ("got SO_RCVBUF %ld from q\n", lvalue));
52257c478bd9Sstevel@tonic-gate } else if (flags & _SOGETSOCKOPT_XPG4_2) {
52267c478bd9Sstevel@tonic-gate value = (int)lvalue;
52277c478bd9Sstevel@tonic-gate option = &value;
52287c478bd9Sstevel@tonic-gate goto copyout; /* skip asking transport */
52297c478bd9Sstevel@tonic-gate }
52307c478bd9Sstevel@tonic-gate value = (int)lvalue;
52317c478bd9Sstevel@tonic-gate option = &value;
52327c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_rcvbuf);
52337c478bd9Sstevel@tonic-gate break;
52347c478bd9Sstevel@tonic-gate }
523588cda078Skcpoon case SO_DOMAIN:
523688cda078Skcpoon value = so->so_family;
523788cda078Skcpoon option = &value;
523888cda078Skcpoon goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
523988cda078Skcpoon
52407c478bd9Sstevel@tonic-gate #ifdef notyet
52417c478bd9Sstevel@tonic-gate /*
52427c478bd9Sstevel@tonic-gate * We do not implement the semantics of these options
52437c478bd9Sstevel@tonic-gate * thus we shouldn't implement the options either.
52447c478bd9Sstevel@tonic-gate */
52457c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
52467c478bd9Sstevel@tonic-gate value = so->so_sndlowat;
52477c478bd9Sstevel@tonic-gate option = &value;
52487c478bd9Sstevel@tonic-gate break;
52497c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
52507c478bd9Sstevel@tonic-gate value = so->so_rcvlowat;
52517c478bd9Sstevel@tonic-gate option = &value;
52527c478bd9Sstevel@tonic-gate break;
52530f1702c5SYu Xiangning #endif /* notyet */
52547c478bd9Sstevel@tonic-gate case SO_SNDTIMEO:
52550f1702c5SYu Xiangning case SO_RCVTIMEO: {
52560f1702c5SYu Xiangning clock_t val;
525722238f73Sshenjian
52580f1702c5SYu Xiangning if (option_name == SO_RCVTIMEO)
52590f1702c5SYu Xiangning val = drv_hztousec(so->so_rcvtimeo);
52600f1702c5SYu Xiangning else
52610f1702c5SYu Xiangning val = drv_hztousec(so->so_sndtimeo);
52620f1702c5SYu Xiangning tmo_val.tv_sec = val / (1000 * 1000);
52630f1702c5SYu Xiangning tmo_val.tv_usec = val % (1000 * 1000);
5264e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5265e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
526622238f73Sshenjian option = &tmo_val;
526722238f73Sshenjian len = sizeof (struct timeval);
526822238f73Sshenjian } else {
526922238f73Sshenjian TIMEVAL_TO_TIMEVAL32(&tmo_val32, &tmo_val);
527022238f73Sshenjian option = &tmo_val32;
527122238f73Sshenjian len = sizeof (struct timeval32);
527222238f73Sshenjian }
52737c478bd9Sstevel@tonic-gate break;
52740f1702c5SYu Xiangning }
52750f1702c5SYu Xiangning case SO_SND_BUFINFO: {
52760f1702c5SYu Xiangning snd_bufinfo.sbi_wroff =
52770f1702c5SYu Xiangning (so->so_proto_props).sopp_wroff;
52780f1702c5SYu Xiangning snd_bufinfo.sbi_maxblk =
52790f1702c5SYu Xiangning (so->so_proto_props).sopp_maxblk;
52800f1702c5SYu Xiangning snd_bufinfo.sbi_maxpsz =
52810f1702c5SYu Xiangning (so->so_proto_props).sopp_maxpsz;
52820f1702c5SYu Xiangning snd_bufinfo.sbi_tail =
52830f1702c5SYu Xiangning (so->so_proto_props).sopp_tail;
52840f1702c5SYu Xiangning option = &snd_bufinfo;
52850f1702c5SYu Xiangning len = (t_uscalar_t)sizeof (struct so_snd_bufinfo);
52867c478bd9Sstevel@tonic-gate break;
52870f1702c5SYu Xiangning }
52887c478bd9Sstevel@tonic-gate }
52897c478bd9Sstevel@tonic-gate }
52907c478bd9Sstevel@tonic-gate
52917c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
52927c478bd9Sstevel@tonic-gate
52937c478bd9Sstevel@tonic-gate /* Send request */
52947c478bd9Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
52957c478bd9Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_CHECK;
52967c478bd9Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
52977c478bd9Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
52987c478bd9Sstevel@tonic-gate
52997c478bd9Sstevel@tonic-gate oh.level = level;
53007c478bd9Sstevel@tonic-gate oh.name = option_name;
53017c478bd9Sstevel@tonic-gate oh.len = maxlen;
53027c478bd9Sstevel@tonic-gate
53037c478bd9Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5304de8c4a14SErik Nordmark &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP, cr);
53057c478bd9Sstevel@tonic-gate /* Let option management work in the presence of data flow control */
53067c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
5307fc80c0dfSnordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
53087c478bd9Sstevel@tonic-gate mp = NULL;
53097c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
53107c478bd9Sstevel@tonic-gate if (error) {
53117c478bd9Sstevel@tonic-gate eprintsoline(so, error);
53127c478bd9Sstevel@tonic-gate goto done2;
53137c478bd9Sstevel@tonic-gate }
53147c478bd9Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
53157c478bd9Sstevel@tonic-gate (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
53167c478bd9Sstevel@tonic-gate if (error) {
53177c478bd9Sstevel@tonic-gate if (option != NULL) {
53187c478bd9Sstevel@tonic-gate /* We have a fallback value */
53197c478bd9Sstevel@tonic-gate error = 0;
53207c478bd9Sstevel@tonic-gate goto copyout;
53217c478bd9Sstevel@tonic-gate }
53227c478bd9Sstevel@tonic-gate eprintsoline(so, error);
53237c478bd9Sstevel@tonic-gate goto done2;
53247c478bd9Sstevel@tonic-gate }
53257c478bd9Sstevel@tonic-gate ASSERT(mp);
53267c478bd9Sstevel@tonic-gate optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
53277c478bd9Sstevel@tonic-gate opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
5328fc80c0dfSnordmark optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
53297c478bd9Sstevel@tonic-gate if (opt_res == NULL) {
53307c478bd9Sstevel@tonic-gate if (option != NULL) {
53317c478bd9Sstevel@tonic-gate /* We have a fallback value */
53327c478bd9Sstevel@tonic-gate error = 0;
53337c478bd9Sstevel@tonic-gate goto copyout;
53347c478bd9Sstevel@tonic-gate }
53357c478bd9Sstevel@tonic-gate error = EPROTO;
53367c478bd9Sstevel@tonic-gate eprintsoline(so, error);
53377c478bd9Sstevel@tonic-gate goto done;
53387c478bd9Sstevel@tonic-gate }
53397c478bd9Sstevel@tonic-gate option = &opt_res[1];
53407c478bd9Sstevel@tonic-gate
53417c478bd9Sstevel@tonic-gate /* check to ensure that the option is within bounds */
53427c478bd9Sstevel@tonic-gate if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
5343fc80c0dfSnordmark (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
53447c478bd9Sstevel@tonic-gate if (option != NULL) {
53457c478bd9Sstevel@tonic-gate /* We have a fallback value */
53467c478bd9Sstevel@tonic-gate error = 0;
53477c478bd9Sstevel@tonic-gate goto copyout;
53487c478bd9Sstevel@tonic-gate }
53497c478bd9Sstevel@tonic-gate error = EPROTO;
53507c478bd9Sstevel@tonic-gate eprintsoline(so, error);
53517c478bd9Sstevel@tonic-gate goto done;
53527c478bd9Sstevel@tonic-gate }
53537c478bd9Sstevel@tonic-gate
53547c478bd9Sstevel@tonic-gate len = opt_res->len;
53557c478bd9Sstevel@tonic-gate
53567c478bd9Sstevel@tonic-gate copyout: {
53577c478bd9Sstevel@tonic-gate t_uscalar_t size = MIN(len, maxlen);
53587c478bd9Sstevel@tonic-gate bcopy(option, optval, size);
53597c478bd9Sstevel@tonic-gate bcopy(&size, optlenp, sizeof (size));
53607c478bd9Sstevel@tonic-gate }
53617c478bd9Sstevel@tonic-gate done:
53627c478bd9Sstevel@tonic-gate freemsg(mp);
53637c478bd9Sstevel@tonic-gate done2:
53647c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
53657c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
53660f1702c5SYu Xiangning
53677c478bd9Sstevel@tonic-gate return (error);
53687c478bd9Sstevel@tonic-gate }
53697c478bd9Sstevel@tonic-gate
53707c478bd9Sstevel@tonic-gate /*
53717c478bd9Sstevel@tonic-gate * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
53727c478bd9Sstevel@tonic-gate * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
53737c478bd9Sstevel@tonic-gate * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
53747c478bd9Sstevel@tonic-gate * setsockopt has to work even if the transport does not support the option.
53757c478bd9Sstevel@tonic-gate */
53760f1702c5SYu Xiangning /* ARGSUSED */
53777c478bd9Sstevel@tonic-gate int
sotpi_setsockopt(struct sonode * so,int level,int option_name,const void * optval,t_uscalar_t optlen,struct cred * cr)53787c478bd9Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name,
5379f012ee0cSGordon Ross const void *optval, t_uscalar_t optlen, struct cred *cr)
53807c478bd9Sstevel@tonic-gate {
53817c478bd9Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req;
53827c478bd9Sstevel@tonic-gate struct opthdr oh;
53837c478bd9Sstevel@tonic-gate mblk_t *mp;
53847c478bd9Sstevel@tonic-gate int error = 0;
53857c478bd9Sstevel@tonic-gate boolean_t handled = B_FALSE;
53867c478bd9Sstevel@tonic-gate
53877c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
5388903a11ebSrh (void *)so, level, option_name, optval, optlen,
5389fc80c0dfSnordmark pr_state(so->so_state, so->so_mode)));
53907c478bd9Sstevel@tonic-gate
53917c478bd9Sstevel@tonic-gate /* X/Open requires this check */
53927c478bd9Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
53937c478bd9Sstevel@tonic-gate if (xnet_check_print)
53947c478bd9Sstevel@tonic-gate printf("sockfs: X/Open setsockopt check => EINVAL\n");
53957c478bd9Sstevel@tonic-gate return (EINVAL);
53967c478bd9Sstevel@tonic-gate }
53977c478bd9Sstevel@tonic-gate
53987c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
53997c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
54007c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
54017c478bd9Sstevel@tonic-gate
54027c478bd9Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
54037c478bd9Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_NEGOTIATE;
54047c478bd9Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
54057c478bd9Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
54067c478bd9Sstevel@tonic-gate
54077c478bd9Sstevel@tonic-gate oh.level = level;
54087c478bd9Sstevel@tonic-gate oh.name = option_name;
54097c478bd9Sstevel@tonic-gate oh.len = optlen;
54107c478bd9Sstevel@tonic-gate
54117c478bd9Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5412de8c4a14SErik Nordmark &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP, cr);
54137c478bd9Sstevel@tonic-gate /* Let option management work in the presence of data flow control */
54147c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
5415fc80c0dfSnordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
54167c478bd9Sstevel@tonic-gate mp = NULL;
54177c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
54187c478bd9Sstevel@tonic-gate if (error) {
54197c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54200f1702c5SYu Xiangning goto done2;
54217c478bd9Sstevel@tonic-gate }
54227c478bd9Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
54237c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
54247c478bd9Sstevel@tonic-gate if (error) {
54257c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54267c478bd9Sstevel@tonic-gate goto done;
54277c478bd9Sstevel@tonic-gate }
54287c478bd9Sstevel@tonic-gate ASSERT(mp);
54297c478bd9Sstevel@tonic-gate /* No need to verify T_optmgmt_ack */
54307c478bd9Sstevel@tonic-gate freemsg(mp);
54317c478bd9Sstevel@tonic-gate done:
54327c478bd9Sstevel@tonic-gate /*
54337c478bd9Sstevel@tonic-gate * Check for SOL_SOCKET options and record their values.
54347c478bd9Sstevel@tonic-gate * If we know about a SOL_SOCKET parameter and the transport
54357c478bd9Sstevel@tonic-gate * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
54367c478bd9Sstevel@tonic-gate * EPROTO) we let the setsockopt succeed.
54377c478bd9Sstevel@tonic-gate */
54387c478bd9Sstevel@tonic-gate if (level == SOL_SOCKET) {
54397c478bd9Sstevel@tonic-gate /* Check parameters */
54407c478bd9Sstevel@tonic-gate switch (option_name) {
54417c478bd9Sstevel@tonic-gate case SO_DEBUG:
54427c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
54437c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
54447c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
54457c478bd9Sstevel@tonic-gate case SO_BROADCAST:
54467c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
54477c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
54487c478bd9Sstevel@tonic-gate case SO_SNDBUF:
54497c478bd9Sstevel@tonic-gate case SO_RCVBUF:
54507c478bd9Sstevel@tonic-gate #ifdef notyet
54517c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
54527c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
54537c478bd9Sstevel@tonic-gate #endif /* notyet */
54547c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
54557c478bd9Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) {
54567c478bd9Sstevel@tonic-gate error = EINVAL;
54577c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54587c478bd9Sstevel@tonic-gate goto done2;
54597c478bd9Sstevel@tonic-gate }
54607c478bd9Sstevel@tonic-gate ASSERT(optval);
54617c478bd9Sstevel@tonic-gate handled = B_TRUE;
54627c478bd9Sstevel@tonic-gate break;
54630f1702c5SYu Xiangning case SO_SNDTIMEO:
54640f1702c5SYu Xiangning case SO_RCVTIMEO:
5465e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5466e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
546722238f73Sshenjian if (optlen != sizeof (struct timeval)) {
546822238f73Sshenjian error = EINVAL;
546922238f73Sshenjian eprintsoline(so, error);
547022238f73Sshenjian goto done2;
547122238f73Sshenjian }
547222238f73Sshenjian } else {
547322238f73Sshenjian if (optlen != sizeof (struct timeval32)) {
547422238f73Sshenjian error = EINVAL;
547522238f73Sshenjian eprintsoline(so, error);
547622238f73Sshenjian goto done2;
547722238f73Sshenjian }
54780f1702c5SYu Xiangning }
54790f1702c5SYu Xiangning ASSERT(optval);
54800f1702c5SYu Xiangning handled = B_TRUE;
54810f1702c5SYu Xiangning break;
54827c478bd9Sstevel@tonic-gate case SO_LINGER:
54837c478bd9Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (struct linger)) {
54847c478bd9Sstevel@tonic-gate error = EINVAL;
54857c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54867c478bd9Sstevel@tonic-gate goto done2;
54877c478bd9Sstevel@tonic-gate }
54887c478bd9Sstevel@tonic-gate ASSERT(optval);
54897c478bd9Sstevel@tonic-gate handled = B_TRUE;
54907c478bd9Sstevel@tonic-gate break;
54917c478bd9Sstevel@tonic-gate }
54927c478bd9Sstevel@tonic-gate
54937c478bd9Sstevel@tonic-gate #define intvalue (*(int32_t *)optval)
54947c478bd9Sstevel@tonic-gate
54957c478bd9Sstevel@tonic-gate switch (option_name) {
54967c478bd9Sstevel@tonic-gate case SO_TYPE:
54977c478bd9Sstevel@tonic-gate case SO_ERROR:
54987c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
54997c478bd9Sstevel@tonic-gate /* Can't be set */
55007c478bd9Sstevel@tonic-gate error = ENOPROTOOPT;
55017c478bd9Sstevel@tonic-gate goto done2;
55027c478bd9Sstevel@tonic-gate case SO_LINGER: {
55037c478bd9Sstevel@tonic-gate struct linger *l = (struct linger *)optval;
55047c478bd9Sstevel@tonic-gate
55057c478bd9Sstevel@tonic-gate so->so_linger.l_linger = l->l_linger;
55067c478bd9Sstevel@tonic-gate if (l->l_onoff) {
55077c478bd9Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER;
55087c478bd9Sstevel@tonic-gate so->so_options |= SO_LINGER;
55097c478bd9Sstevel@tonic-gate } else {
55107c478bd9Sstevel@tonic-gate so->so_linger.l_onoff = 0;
55117c478bd9Sstevel@tonic-gate so->so_options &= ~SO_LINGER;
55127c478bd9Sstevel@tonic-gate }
55137c478bd9Sstevel@tonic-gate break;
55147c478bd9Sstevel@tonic-gate }
55157c478bd9Sstevel@tonic-gate
55167c478bd9Sstevel@tonic-gate case SO_DEBUG:
55177c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
55187c478bd9Sstevel@tonic-gate if (intvalue & 2)
55197c478bd9Sstevel@tonic-gate sock_test_timelimit = 10 * hz;
55207c478bd9Sstevel@tonic-gate else
55217c478bd9Sstevel@tonic-gate sock_test_timelimit = 0;
55227c478bd9Sstevel@tonic-gate
55237c478bd9Sstevel@tonic-gate if (intvalue & 4)
55247c478bd9Sstevel@tonic-gate do_useracc = 0;
55257c478bd9Sstevel@tonic-gate else
55267c478bd9Sstevel@tonic-gate do_useracc = 1;
55277c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
55287c478bd9Sstevel@tonic-gate /* FALLTHRU */
55297c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
55307c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
55317c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
55327c478bd9Sstevel@tonic-gate case SO_BROADCAST:
55337c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
55347c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
55357c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
55367c478bd9Sstevel@tonic-gate if (intvalue != 0) {
55377c478bd9Sstevel@tonic-gate dprintso(so, 1,
55380f1702c5SYu Xiangning ("socket_setsockopt: setting 0x%x\n",
5539fc80c0dfSnordmark option_name));
55407c478bd9Sstevel@tonic-gate so->so_options |= option_name;
55417c478bd9Sstevel@tonic-gate } else {
55427c478bd9Sstevel@tonic-gate dprintso(so, 1,
55430f1702c5SYu Xiangning ("socket_setsockopt: clearing 0x%x\n",
5544fc80c0dfSnordmark option_name));
55457c478bd9Sstevel@tonic-gate so->so_options &= ~option_name;
55467c478bd9Sstevel@tonic-gate }
55477c478bd9Sstevel@tonic-gate break;
55487c478bd9Sstevel@tonic-gate /*
55497c478bd9Sstevel@tonic-gate * The following options are only returned by us when the
55500f1702c5SYu Xiangning * transport layer fails.
55517c478bd9Sstevel@tonic-gate * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
55527c478bd9Sstevel@tonic-gate * since the transport might adjust the value and not
55537c478bd9Sstevel@tonic-gate * return exactly what was set by the application.
55547c478bd9Sstevel@tonic-gate */
55557c478bd9Sstevel@tonic-gate case SO_SNDBUF:
55567c478bd9Sstevel@tonic-gate so->so_sndbuf = intvalue;
55577c478bd9Sstevel@tonic-gate break;
55587c478bd9Sstevel@tonic-gate case SO_RCVBUF:
55597c478bd9Sstevel@tonic-gate so->so_rcvbuf = intvalue;
55607c478bd9Sstevel@tonic-gate break;
55610f1702c5SYu Xiangning case SO_RCVPSH:
55620f1702c5SYu Xiangning so->so_rcv_timer_interval = intvalue;
55630f1702c5SYu Xiangning break;
55647c478bd9Sstevel@tonic-gate #ifdef notyet
55657c478bd9Sstevel@tonic-gate /*
55667c478bd9Sstevel@tonic-gate * We do not implement the semantics of these options
55677c478bd9Sstevel@tonic-gate * thus we shouldn't implement the options either.
55687c478bd9Sstevel@tonic-gate */
55697c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
55707c478bd9Sstevel@tonic-gate so->so_sndlowat = intvalue;
55717c478bd9Sstevel@tonic-gate break;
55727c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
55737c478bd9Sstevel@tonic-gate so->so_rcvlowat = intvalue;
55747c478bd9Sstevel@tonic-gate break;
55750f1702c5SYu Xiangning #endif /* notyet */
55767c478bd9Sstevel@tonic-gate case SO_SNDTIMEO:
55770f1702c5SYu Xiangning case SO_RCVTIMEO: {
557822238f73Sshenjian struct timeval tl;
557922238f73Sshenjian clock_t val;
558022238f73Sshenjian
5581e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5582e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE)
558322238f73Sshenjian bcopy(&tl, (struct timeval *)optval,
558422238f73Sshenjian sizeof (struct timeval));
558522238f73Sshenjian else
558622238f73Sshenjian TIMEVAL32_TO_TIMEVAL(&tl,
558722238f73Sshenjian (struct timeval32 *)optval);
558822238f73Sshenjian val = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
55890f1702c5SYu Xiangning if (option_name == SO_RCVTIMEO)
55900f1702c5SYu Xiangning so->so_rcvtimeo = drv_usectohz(val);
55910f1702c5SYu Xiangning else
55920f1702c5SYu Xiangning so->so_sndtimeo = drv_usectohz(val);
55937c478bd9Sstevel@tonic-gate break;
55940f1702c5SYu Xiangning }
55957c478bd9Sstevel@tonic-gate }
55967c478bd9Sstevel@tonic-gate #undef intvalue
55977c478bd9Sstevel@tonic-gate
55987c478bd9Sstevel@tonic-gate if (error) {
55997c478bd9Sstevel@tonic-gate if ((error == ENOPROTOOPT || error == EPROTO ||
56007c478bd9Sstevel@tonic-gate error == EINVAL) && handled) {
56017c478bd9Sstevel@tonic-gate dprintso(so, 1,
56027c478bd9Sstevel@tonic-gate ("setsockopt: ignoring error %d for 0x%x\n",
56037c478bd9Sstevel@tonic-gate error, option_name));
56047c478bd9Sstevel@tonic-gate error = 0;
56057c478bd9Sstevel@tonic-gate }
56067c478bd9Sstevel@tonic-gate }
56077c478bd9Sstevel@tonic-gate }
56087c478bd9Sstevel@tonic-gate done2:
56097c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
56107c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
56117c478bd9Sstevel@tonic-gate return (error);
56127c478bd9Sstevel@tonic-gate }
56130f1702c5SYu Xiangning
5614f0267584Sanders /*
5615f0267584Sanders * sotpi_close() is called when the last open reference goes away.
5616f0267584Sanders */
56170f1702c5SYu Xiangning /* ARGSUSED */
56180f1702c5SYu Xiangning int
sotpi_close(struct sonode * so,int flag,struct cred * cr)56190f1702c5SYu Xiangning sotpi_close(struct sonode *so, int flag, struct cred *cr)
56200f1702c5SYu Xiangning {
56210f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
56220f1702c5SYu Xiangning dev_t dev;
56230f1702c5SYu Xiangning int error = 0;
56240f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
56250f1702c5SYu Xiangning
56260f1702c5SYu Xiangning dprintso(so, 1, ("sotpi_close(%p, %x) %s\n",
56270f1702c5SYu Xiangning (void *)vp, flag, pr_state(so->so_state, so->so_mode)));
56280f1702c5SYu Xiangning
56290f1702c5SYu Xiangning dev = sti->sti_dev;
56300f1702c5SYu Xiangning
56310f1702c5SYu Xiangning ASSERT(STREAMSTAB(getmajor(dev)));
56320f1702c5SYu Xiangning
56330f1702c5SYu Xiangning mutex_enter(&so->so_lock);
56340f1702c5SYu Xiangning so_lock_single(so); /* Set SOLOCKED */
56350f1702c5SYu Xiangning
5636d36be52eSRao Shoaib ASSERT(so_verify_oobstate(so));
5637d36be52eSRao Shoaib
56380f1702c5SYu Xiangning if (vp->v_stream != NULL) {
56390f1702c5SYu Xiangning vnode_t *ux_vp;
56400f1702c5SYu Xiangning
56410f1702c5SYu Xiangning if (so->so_family == AF_UNIX) {
56420f1702c5SYu Xiangning /* Could avoid this when CANTSENDMORE for !dgram */
56430f1702c5SYu Xiangning so_unix_close(so);
56440f1702c5SYu Xiangning }
56450f1702c5SYu Xiangning
56460f1702c5SYu Xiangning mutex_exit(&so->so_lock);
56470f1702c5SYu Xiangning /*
56480f1702c5SYu Xiangning * Disassemble the linkage from the AF_UNIX underlying file
56490f1702c5SYu Xiangning * system vnode to this socket (by atomically clearing
56500f1702c5SYu Xiangning * v_stream in vn_rele_stream) before strclose clears sd_vnode
56510f1702c5SYu Xiangning * and frees the stream head.
56520f1702c5SYu Xiangning */
56530f1702c5SYu Xiangning if ((ux_vp = sti->sti_ux_bound_vp) != NULL) {
56540f1702c5SYu Xiangning ASSERT(ux_vp->v_stream);
56550f1702c5SYu Xiangning sti->sti_ux_bound_vp = NULL;
56560f1702c5SYu Xiangning vn_rele_stream(ux_vp);
56570f1702c5SYu Xiangning }
56580f1702c5SYu Xiangning error = strclose(vp, flag, cr);
56590f1702c5SYu Xiangning vp->v_stream = NULL;
56600f1702c5SYu Xiangning mutex_enter(&so->so_lock);
56610f1702c5SYu Xiangning }
56620f1702c5SYu Xiangning
56630f1702c5SYu Xiangning /*
56640f1702c5SYu Xiangning * Flush the T_DISCON_IND on sti_discon_ind_mp.
56650f1702c5SYu Xiangning */
56660f1702c5SYu Xiangning so_flush_discon_ind(so);
56670f1702c5SYu Xiangning
56680f1702c5SYu Xiangning so_unlock_single(so, SOLOCKED);
56690f1702c5SYu Xiangning mutex_exit(&so->so_lock);
56700f1702c5SYu Xiangning
56710f1702c5SYu Xiangning /*
56720f1702c5SYu Xiangning * Needed for STREAMs.
56730f1702c5SYu Xiangning * Decrement the device driver's reference count for streams
56740f1702c5SYu Xiangning * opened via the clone dip. The driver was held in clone_open().
56750f1702c5SYu Xiangning * The absence of clone_close() forces this asymmetry.
56760f1702c5SYu Xiangning */
56770f1702c5SYu Xiangning if (so->so_flag & SOCLONE)
56780f1702c5SYu Xiangning ddi_rele_driver(getmajor(dev));
56790f1702c5SYu Xiangning
56800f1702c5SYu Xiangning return (error);
56810f1702c5SYu Xiangning }
56820f1702c5SYu Xiangning
56830f1702c5SYu Xiangning static int
sotpi_ioctl(struct sonode * so,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)56840f1702c5SYu Xiangning sotpi_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
56850f1702c5SYu Xiangning struct cred *cr, int32_t *rvalp)
56860f1702c5SYu Xiangning {
56870f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
56880f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
56890f1702c5SYu Xiangning int error = 0;
56900f1702c5SYu Xiangning
56910f1702c5SYu Xiangning dprintso(so, 0, ("sotpi_ioctl: cmd 0x%x, arg 0x%lx, state %s\n",
56920f1702c5SYu Xiangning cmd, arg, pr_state(so->so_state, so->so_mode)));
56930f1702c5SYu Xiangning
56940f1702c5SYu Xiangning switch (cmd) {
5695bfcb55b8SRao Shoaib case SIOCSQPTR:
5696bfcb55b8SRao Shoaib /*
5697bfcb55b8SRao Shoaib * SIOCSQPTR is valid only when helper stream is created
5698bfcb55b8SRao Shoaib * by the protocol.
5699bfcb55b8SRao Shoaib */
57000f1702c5SYu Xiangning case _I_INSERT:
57010f1702c5SYu Xiangning case _I_REMOVE:
57020f1702c5SYu Xiangning /*
57030f1702c5SYu Xiangning * Since there's no compelling reason to support these ioctls
57040f1702c5SYu Xiangning * on sockets, and doing so would increase the complexity
57050f1702c5SYu Xiangning * markedly, prevent it.
57060f1702c5SYu Xiangning */
57070f1702c5SYu Xiangning return (EOPNOTSUPP);
57080f1702c5SYu Xiangning
57090f1702c5SYu Xiangning case I_FIND:
57100f1702c5SYu Xiangning case I_LIST:
57110f1702c5SYu Xiangning case I_LOOK:
57120f1702c5SYu Xiangning case I_POP:
57130f1702c5SYu Xiangning case I_PUSH:
57140f1702c5SYu Xiangning /*
57150f1702c5SYu Xiangning * To prevent races and inconsistencies between the actual
57160f1702c5SYu Xiangning * state of the stream and the state according to the sonode,
57170f1702c5SYu Xiangning * we serialize all operations which modify or operate on the
57180f1702c5SYu Xiangning * list of modules on the socket's stream.
57190f1702c5SYu Xiangning */
57200f1702c5SYu Xiangning mutex_enter(&sti->sti_plumb_lock);
57210f1702c5SYu Xiangning error = socktpi_plumbioctl(vp, cmd, arg, mode, cr, rvalp);
57220f1702c5SYu Xiangning mutex_exit(&sti->sti_plumb_lock);
57230f1702c5SYu Xiangning return (error);
57240f1702c5SYu Xiangning
57250f1702c5SYu Xiangning default:
57260f1702c5SYu Xiangning if (so->so_version != SOV_STREAM)
57270f1702c5SYu Xiangning break;
57280f1702c5SYu Xiangning
57290f1702c5SYu Xiangning /*
57300f1702c5SYu Xiangning * The imaginary "sockmod" has been popped; act as a stream.
57310f1702c5SYu Xiangning */
57320f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
57330f1702c5SYu Xiangning }
57340f1702c5SYu Xiangning
57350f1702c5SYu Xiangning ASSERT(so->so_version != SOV_STREAM);
57360f1702c5SYu Xiangning
57370f1702c5SYu Xiangning /*
57380f1702c5SYu Xiangning * Process socket-specific ioctls.
57390f1702c5SYu Xiangning */
57400f1702c5SYu Xiangning switch (cmd) {
57410f1702c5SYu Xiangning case FIONBIO: {
57420f1702c5SYu Xiangning int32_t value;
57430f1702c5SYu Xiangning
57440f1702c5SYu Xiangning if (so_copyin((void *)arg, &value, sizeof (int32_t),
57450f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
57460f1702c5SYu Xiangning return (EFAULT);
57470f1702c5SYu Xiangning
57480f1702c5SYu Xiangning mutex_enter(&so->so_lock);
57490f1702c5SYu Xiangning if (value) {
57500f1702c5SYu Xiangning so->so_state |= SS_NDELAY;
57510f1702c5SYu Xiangning } else {
57520f1702c5SYu Xiangning so->so_state &= ~SS_NDELAY;
57530f1702c5SYu Xiangning }
57540f1702c5SYu Xiangning mutex_exit(&so->so_lock);
57550f1702c5SYu Xiangning return (0);
57560f1702c5SYu Xiangning }
57570f1702c5SYu Xiangning
57580f1702c5SYu Xiangning case FIOASYNC: {
57590f1702c5SYu Xiangning int32_t value;
57600f1702c5SYu Xiangning
57610f1702c5SYu Xiangning if (so_copyin((void *)arg, &value, sizeof (int32_t),
57620f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
57630f1702c5SYu Xiangning return (EFAULT);
57640f1702c5SYu Xiangning
57650f1702c5SYu Xiangning mutex_enter(&so->so_lock);
57660f1702c5SYu Xiangning /*
57670f1702c5SYu Xiangning * SS_ASYNC flag not already set correctly?
57680f1702c5SYu Xiangning * (!value != !(so->so_state & SS_ASYNC))
57690f1702c5SYu Xiangning * but some engineers find that too hard to read.
57700f1702c5SYu Xiangning */
5771*34793d0fSToomas Soome if ((value == 0 && (so->so_state & SS_ASYNC) != 0) ||
5772*34793d0fSToomas Soome (value != 0 && (so->so_state & SS_ASYNC) == 0))
57730f1702c5SYu Xiangning error = so_flip_async(so, vp, mode, cr);
57740f1702c5SYu Xiangning mutex_exit(&so->so_lock);
57750f1702c5SYu Xiangning return (error);
57760f1702c5SYu Xiangning }
57770f1702c5SYu Xiangning
57780f1702c5SYu Xiangning case SIOCSPGRP:
57790f1702c5SYu Xiangning case FIOSETOWN: {
57800f1702c5SYu Xiangning pid_t pgrp;
57810f1702c5SYu Xiangning
57820f1702c5SYu Xiangning if (so_copyin((void *)arg, &pgrp, sizeof (pid_t),
57830f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
57840f1702c5SYu Xiangning return (EFAULT);
57850f1702c5SYu Xiangning
57860f1702c5SYu Xiangning mutex_enter(&so->so_lock);
57870f1702c5SYu Xiangning dprintso(so, 1, ("setown: new %d old %d\n", pgrp, so->so_pgrp));
57880f1702c5SYu Xiangning /* Any change? */
57890f1702c5SYu Xiangning if (pgrp != so->so_pgrp)
57900f1702c5SYu Xiangning error = so_set_siggrp(so, vp, pgrp, mode, cr);
57910f1702c5SYu Xiangning mutex_exit(&so->so_lock);
57920f1702c5SYu Xiangning return (error);
57930f1702c5SYu Xiangning }
57940f1702c5SYu Xiangning case SIOCGPGRP:
57950f1702c5SYu Xiangning case FIOGETOWN:
57960f1702c5SYu Xiangning if (so_copyout(&so->so_pgrp, (void *)arg,
57970f1702c5SYu Xiangning sizeof (pid_t), (mode & (int)FKIOCTL)))
57980f1702c5SYu Xiangning return (EFAULT);
57990f1702c5SYu Xiangning return (0);
58000f1702c5SYu Xiangning
58010f1702c5SYu Xiangning case SIOCATMARK: {
58020f1702c5SYu Xiangning int retval;
58030f1702c5SYu Xiangning uint_t so_state;
58040f1702c5SYu Xiangning
58050f1702c5SYu Xiangning /*
58060f1702c5SYu Xiangning * strwaitmark has a finite timeout after which it
58070f1702c5SYu Xiangning * returns -1 if the mark state is undetermined.
58080f1702c5SYu Xiangning * In order to avoid any race between the mark state
58090f1702c5SYu Xiangning * in sockfs and the mark state in the stream head this
58100f1702c5SYu Xiangning * routine loops until the mark state can be determined
58110f1702c5SYu Xiangning * (or the urgent data indication has been removed by some
58120f1702c5SYu Xiangning * other thread).
58130f1702c5SYu Xiangning */
58140f1702c5SYu Xiangning do {
58150f1702c5SYu Xiangning mutex_enter(&so->so_lock);
58160f1702c5SYu Xiangning so_state = so->so_state;
58170f1702c5SYu Xiangning mutex_exit(&so->so_lock);
58180f1702c5SYu Xiangning if (so_state & SS_RCVATMARK) {
58190f1702c5SYu Xiangning retval = 1;
58200f1702c5SYu Xiangning } else if (!(so_state & SS_OOBPEND)) {
58210f1702c5SYu Xiangning /*
58220f1702c5SYu Xiangning * No SIGURG has been generated -- there is no
58230f1702c5SYu Xiangning * pending or present urgent data. Thus can't
58240f1702c5SYu Xiangning * possibly be at the mark.
58250f1702c5SYu Xiangning */
58260f1702c5SYu Xiangning retval = 0;
58270f1702c5SYu Xiangning } else {
58280f1702c5SYu Xiangning /*
58290f1702c5SYu Xiangning * Have the stream head wait until there is
58300f1702c5SYu Xiangning * either some messages on the read queue, or
58310f1702c5SYu Xiangning * STRATMARK or STRNOTATMARK gets set. The
58320f1702c5SYu Xiangning * STRNOTATMARK flag is used so that the
58330f1702c5SYu Xiangning * transport can send up a MSGNOTMARKNEXT
58340f1702c5SYu Xiangning * M_DATA to indicate that it is not
58350f1702c5SYu Xiangning * at the mark and additional data is not about
58360f1702c5SYu Xiangning * to be send upstream.
58370f1702c5SYu Xiangning *
58380f1702c5SYu Xiangning * If the mark state is undetermined this will
58390f1702c5SYu Xiangning * return -1 and we will loop rechecking the
58400f1702c5SYu Xiangning * socket state.
58410f1702c5SYu Xiangning */
58420f1702c5SYu Xiangning retval = strwaitmark(vp);
58430f1702c5SYu Xiangning }
58440f1702c5SYu Xiangning } while (retval == -1);
58450f1702c5SYu Xiangning
58460f1702c5SYu Xiangning if (so_copyout(&retval, (void *)arg, sizeof (int),
58470f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
58480f1702c5SYu Xiangning return (EFAULT);
58490f1702c5SYu Xiangning return (0);
58500f1702c5SYu Xiangning }
58510f1702c5SYu Xiangning
58520f1702c5SYu Xiangning case I_FDINSERT:
58530f1702c5SYu Xiangning case I_SENDFD:
58540f1702c5SYu Xiangning case I_RECVFD:
58550f1702c5SYu Xiangning case I_ATMARK:
58560f1702c5SYu Xiangning case _SIOCSOCKFALLBACK:
58570f1702c5SYu Xiangning /*
58580f1702c5SYu Xiangning * These ioctls do not apply to sockets. I_FDINSERT can be
58590f1702c5SYu Xiangning * used to send M_PROTO messages without modifying the socket
58600f1702c5SYu Xiangning * state. I_SENDFD/RECVFD should not be used for socket file
58610f1702c5SYu Xiangning * descriptor passing since they assume a twisted stream.
58620f1702c5SYu Xiangning * SIOCATMARK must be used instead of I_ATMARK.
58630f1702c5SYu Xiangning *
58640f1702c5SYu Xiangning * _SIOCSOCKFALLBACK from an application should never be
58650f1702c5SYu Xiangning * processed. It is only generated by socktpi_open() or
58660f1702c5SYu Xiangning * in response to I_POP or I_PUSH.
58670f1702c5SYu Xiangning */
58680f1702c5SYu Xiangning #ifdef DEBUG
58690f1702c5SYu Xiangning zcmn_err(getzoneid(), CE_WARN,
58700f1702c5SYu Xiangning "Unsupported STREAMS ioctl 0x%x on socket. "
58710f1702c5SYu Xiangning "Pid = %d\n", cmd, curproc->p_pid);
58720f1702c5SYu Xiangning #endif /* DEBUG */
58730f1702c5SYu Xiangning return (EOPNOTSUPP);
58740f1702c5SYu Xiangning
58750f1702c5SYu Xiangning case _I_GETPEERCRED:
58760f1702c5SYu Xiangning if ((mode & FKIOCTL) == 0)
58770f1702c5SYu Xiangning return (EINVAL);
58780f1702c5SYu Xiangning
58790f1702c5SYu Xiangning mutex_enter(&so->so_lock);
58800f1702c5SYu Xiangning if ((so->so_mode & SM_CONNREQUIRED) == 0) {
58810f1702c5SYu Xiangning error = ENOTSUP;
58820f1702c5SYu Xiangning } else if ((so->so_state & SS_ISCONNECTED) == 0) {
58830f1702c5SYu Xiangning error = ENOTCONN;
58840f1702c5SYu Xiangning } else if (so->so_peercred != NULL) {
58850f1702c5SYu Xiangning k_peercred_t *kp = (k_peercred_t *)arg;
58860f1702c5SYu Xiangning kp->pc_cr = so->so_peercred;
58870f1702c5SYu Xiangning kp->pc_cpid = so->so_cpid;
58880f1702c5SYu Xiangning crhold(so->so_peercred);
58890f1702c5SYu Xiangning } else {
58900f1702c5SYu Xiangning error = EINVAL;
58910f1702c5SYu Xiangning }
58920f1702c5SYu Xiangning mutex_exit(&so->so_lock);
58930f1702c5SYu Xiangning return (error);
58940f1702c5SYu Xiangning
58950f1702c5SYu Xiangning default:
58960f1702c5SYu Xiangning /*
58970f1702c5SYu Xiangning * Do the higher-order bits of the ioctl cmd indicate
58980f1702c5SYu Xiangning * that it is an I_* streams ioctl?
58990f1702c5SYu Xiangning */
59000f1702c5SYu Xiangning if ((cmd & 0xffffff00U) == STR &&
59010f1702c5SYu Xiangning so->so_version == SOV_SOCKBSD) {
59020f1702c5SYu Xiangning #ifdef DEBUG
59030f1702c5SYu Xiangning zcmn_err(getzoneid(), CE_WARN,
59040f1702c5SYu Xiangning "Unsupported STREAMS ioctl 0x%x on socket. "
5905e9f74ea5SToomas Soome "Pid = %d\n", cmd, curproc->p_pid);
59060f1702c5SYu Xiangning #endif /* DEBUG */
59070f1702c5SYu Xiangning return (EOPNOTSUPP);
59080f1702c5SYu Xiangning }
59090f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
59100f1702c5SYu Xiangning }
59110f1702c5SYu Xiangning }
59120f1702c5SYu Xiangning
59130f1702c5SYu Xiangning /*
59140f1702c5SYu Xiangning * Handle plumbing-related ioctls.
59150f1702c5SYu Xiangning */
59160f1702c5SYu Xiangning static int
socktpi_plumbioctl(struct vnode * vp,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)59170f1702c5SYu Xiangning socktpi_plumbioctl(struct vnode *vp, int cmd, intptr_t arg, int mode,
59180f1702c5SYu Xiangning struct cred *cr, int32_t *rvalp)
59190f1702c5SYu Xiangning {
59200f1702c5SYu Xiangning static const char sockmod_name[] = "sockmod";
59210f1702c5SYu Xiangning struct sonode *so = VTOSO(vp);
59220f1702c5SYu Xiangning char mname[FMNAMESZ + 1];
59230f1702c5SYu Xiangning int error;
59240f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
59250f1702c5SYu Xiangning
59260f1702c5SYu Xiangning ASSERT(MUTEX_HELD(&sti->sti_plumb_lock));
59270f1702c5SYu Xiangning
59280f1702c5SYu Xiangning if (so->so_version == SOV_SOCKBSD)
59290f1702c5SYu Xiangning return (EOPNOTSUPP);
59300f1702c5SYu Xiangning
59310f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
59320f1702c5SYu Xiangning /*
59330f1702c5SYu Xiangning * The imaginary "sockmod" has been popped - act as a stream.
59340f1702c5SYu Xiangning * If this is a push of sockmod then change back to a socket.
59350f1702c5SYu Xiangning */
59360f1702c5SYu Xiangning if (cmd == I_PUSH) {
59370f1702c5SYu Xiangning error = ((mode & FKIOCTL) ? copystr : copyinstr)(
59380f1702c5SYu Xiangning (void *)arg, mname, sizeof (mname), NULL);
59390f1702c5SYu Xiangning
59400f1702c5SYu Xiangning if (error == 0 && strcmp(mname, sockmod_name) == 0) {
59410f1702c5SYu Xiangning dprintso(so, 0, ("socktpi_ioctl: going to "
59420f1702c5SYu Xiangning "socket version\n"));
59430f1702c5SYu Xiangning so_stream2sock(so);
59440f1702c5SYu Xiangning return (0);
59450f1702c5SYu Xiangning }
59460f1702c5SYu Xiangning }
59470f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
59480f1702c5SYu Xiangning }
59490f1702c5SYu Xiangning
59500f1702c5SYu Xiangning switch (cmd) {
59510f1702c5SYu Xiangning case I_PUSH:
59520f1702c5SYu Xiangning if (sti->sti_direct) {
59530f1702c5SYu Xiangning mutex_enter(&so->so_lock);
59540f1702c5SYu Xiangning so_lock_single(so);
59550f1702c5SYu Xiangning mutex_exit(&so->so_lock);
59560f1702c5SYu Xiangning
59570f1702c5SYu Xiangning error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
5958de8c4a14SErik Nordmark cr, rvalp);
59590f1702c5SYu Xiangning
59600f1702c5SYu Xiangning mutex_enter(&so->so_lock);
59610f1702c5SYu Xiangning if (error == 0)
59620f1702c5SYu Xiangning sti->sti_direct = 0;
59630f1702c5SYu Xiangning so_unlock_single(so, SOLOCKED);
59640f1702c5SYu Xiangning mutex_exit(&so->so_lock);
59650f1702c5SYu Xiangning
59660f1702c5SYu Xiangning if (error != 0)
59670f1702c5SYu Xiangning return (error);
59680f1702c5SYu Xiangning }
59690f1702c5SYu Xiangning
59700f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
59710f1702c5SYu Xiangning if (error == 0)
59720f1702c5SYu Xiangning sti->sti_pushcnt++;
59730f1702c5SYu Xiangning return (error);
59740f1702c5SYu Xiangning
59750f1702c5SYu Xiangning case I_POP:
59760f1702c5SYu Xiangning if (sti->sti_pushcnt == 0) {
59770f1702c5SYu Xiangning /* Emulate sockmod being popped */
59780f1702c5SYu Xiangning dprintso(so, 0,
59790f1702c5SYu Xiangning ("socktpi_ioctl: going to STREAMS version\n"));
59800f1702c5SYu Xiangning return (so_sock2stream(so));
59810f1702c5SYu Xiangning }
59820f1702c5SYu Xiangning
59830f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
59840f1702c5SYu Xiangning if (error == 0)
59850f1702c5SYu Xiangning sti->sti_pushcnt--;
59860f1702c5SYu Xiangning return (error);
59870f1702c5SYu Xiangning
59880f1702c5SYu Xiangning case I_LIST: {
59890f1702c5SYu Xiangning struct str_mlist *kmlistp, *umlistp;
59900f1702c5SYu Xiangning struct str_list kstrlist;
59910f1702c5SYu Xiangning ssize_t kstrlistsize;
59920f1702c5SYu Xiangning int i, nmods;
59930f1702c5SYu Xiangning
59940f1702c5SYu Xiangning STRUCT_DECL(str_list, ustrlist);
59950f1702c5SYu Xiangning STRUCT_INIT(ustrlist, mode);
59960f1702c5SYu Xiangning
5997e9f74ea5SToomas Soome if (arg == 0) {
59980f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
59990f1702c5SYu Xiangning if (error == 0)
60000f1702c5SYu Xiangning (*rvalp)++; /* Add one for sockmod */
60010f1702c5SYu Xiangning return (error);
60020f1702c5SYu Xiangning }
60030f1702c5SYu Xiangning
60040f1702c5SYu Xiangning error = so_copyin((void *)arg, STRUCT_BUF(ustrlist),
60050f1702c5SYu Xiangning STRUCT_SIZE(ustrlist), mode & FKIOCTL);
60060f1702c5SYu Xiangning if (error != 0)
60070f1702c5SYu Xiangning return (error);
60080f1702c5SYu Xiangning
60090f1702c5SYu Xiangning nmods = STRUCT_FGET(ustrlist, sl_nmods);
60100f1702c5SYu Xiangning if (nmods <= 0)
60110f1702c5SYu Xiangning return (EINVAL);
60120f1702c5SYu Xiangning /*
60130f1702c5SYu Xiangning * Ceiling nmods at nstrpush to prevent someone from
60140f1702c5SYu Xiangning * maliciously consuming lots of kernel memory.
60150f1702c5SYu Xiangning */
60160f1702c5SYu Xiangning nmods = MIN(nmods, nstrpush);
60170f1702c5SYu Xiangning
60180f1702c5SYu Xiangning kstrlistsize = (nmods + 1) * sizeof (struct str_mlist);
60190f1702c5SYu Xiangning kstrlist.sl_nmods = nmods;
60200f1702c5SYu Xiangning kstrlist.sl_modlist = kmem_zalloc(kstrlistsize, KM_SLEEP);
60210f1702c5SYu Xiangning
60220f1702c5SYu Xiangning error = strioctl(vp, cmd, (intptr_t)&kstrlist, mode, K_TO_K,
60230f1702c5SYu Xiangning cr, rvalp);
60240f1702c5SYu Xiangning if (error != 0)
60250f1702c5SYu Xiangning goto done;
60260f1702c5SYu Xiangning
60270f1702c5SYu Xiangning /*
60280f1702c5SYu Xiangning * Considering the module list as a 0-based array of sl_nmods
60290f1702c5SYu Xiangning * modules, sockmod should conceptually exist at slot
60300f1702c5SYu Xiangning * sti_pushcnt. Insert sockmod at this location by sliding all
60310f1702c5SYu Xiangning * of the module names after so_pushcnt over by one. We know
60320f1702c5SYu Xiangning * that there will be room to do this since we allocated
60330f1702c5SYu Xiangning * sl_modlist with an additional slot.
60340f1702c5SYu Xiangning */
60350f1702c5SYu Xiangning for (i = kstrlist.sl_nmods; i > sti->sti_pushcnt; i--)
60360f1702c5SYu Xiangning kstrlist.sl_modlist[i] = kstrlist.sl_modlist[i - 1];
60370f1702c5SYu Xiangning
60380f1702c5SYu Xiangning (void) strcpy(kstrlist.sl_modlist[i].l_name, sockmod_name);
60390f1702c5SYu Xiangning kstrlist.sl_nmods++;
60400f1702c5SYu Xiangning
60410f1702c5SYu Xiangning /*
60420f1702c5SYu Xiangning * Copy all of the entries out to ustrlist.
60430f1702c5SYu Xiangning */
60440f1702c5SYu Xiangning kmlistp = kstrlist.sl_modlist;
60450f1702c5SYu Xiangning umlistp = STRUCT_FGETP(ustrlist, sl_modlist);
60460f1702c5SYu Xiangning for (i = 0; i < nmods && i < kstrlist.sl_nmods; i++) {
60470f1702c5SYu Xiangning error = so_copyout(kmlistp++, umlistp++,
60480f1702c5SYu Xiangning sizeof (struct str_mlist), mode & FKIOCTL);
60490f1702c5SYu Xiangning if (error != 0)
60500f1702c5SYu Xiangning goto done;
60510f1702c5SYu Xiangning }
60520f1702c5SYu Xiangning
60530f1702c5SYu Xiangning error = so_copyout(&i, (void *)arg, sizeof (int32_t),
60540f1702c5SYu Xiangning mode & FKIOCTL);
60550f1702c5SYu Xiangning if (error == 0)
60560f1702c5SYu Xiangning *rvalp = 0;
60570f1702c5SYu Xiangning done:
60580f1702c5SYu Xiangning kmem_free(kstrlist.sl_modlist, kstrlistsize);
60590f1702c5SYu Xiangning return (error);
60600f1702c5SYu Xiangning }
60610f1702c5SYu Xiangning case I_LOOK:
60620f1702c5SYu Xiangning if (sti->sti_pushcnt == 0) {
60630f1702c5SYu Xiangning return (so_copyout(sockmod_name, (void *)arg,
60640f1702c5SYu Xiangning sizeof (sockmod_name), mode & FKIOCTL));
60650f1702c5SYu Xiangning }
60660f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
60670f1702c5SYu Xiangning
60680f1702c5SYu Xiangning case I_FIND:
60690f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60700f1702c5SYu Xiangning if (error && error != EINVAL)
60710f1702c5SYu Xiangning return (error);
60720f1702c5SYu Xiangning
60730f1702c5SYu Xiangning /* if not found and string was sockmod return 1 */
60740f1702c5SYu Xiangning if (*rvalp == 0 || error == EINVAL) {
60750f1702c5SYu Xiangning error = ((mode & FKIOCTL) ? copystr : copyinstr)(
60760f1702c5SYu Xiangning (void *)arg, mname, sizeof (mname), NULL);
60770f1702c5SYu Xiangning if (error == ENAMETOOLONG)
60780f1702c5SYu Xiangning error = EINVAL;
60790f1702c5SYu Xiangning
60800f1702c5SYu Xiangning if (error == 0 && strcmp(mname, sockmod_name) == 0)
60810f1702c5SYu Xiangning *rvalp = 1;
60820f1702c5SYu Xiangning }
60830f1702c5SYu Xiangning return (error);
60840f1702c5SYu Xiangning
60850f1702c5SYu Xiangning default:
60860f1702c5SYu Xiangning panic("socktpi_plumbioctl: unknown ioctl %d", cmd);
60870f1702c5SYu Xiangning break;
60880f1702c5SYu Xiangning }
60890f1702c5SYu Xiangning
60900f1702c5SYu Xiangning return (0);
60910f1702c5SYu Xiangning }
60920f1702c5SYu Xiangning
60930f1702c5SYu Xiangning /*
60940f1702c5SYu Xiangning * Wrapper around the streams poll routine that implements socket poll
60950f1702c5SYu Xiangning * semantics.
60960f1702c5SYu Xiangning * The sockfs never calls pollwakeup itself - the stream head take care
60970f1702c5SYu Xiangning * of all pollwakeups. Since sockfs never holds so_lock when calling the
60980f1702c5SYu Xiangning * stream head there can never be a deadlock due to holding so_lock across
60990f1702c5SYu Xiangning * pollwakeup and acquiring so_lock in this routine.
61000f1702c5SYu Xiangning *
61010f1702c5SYu Xiangning * However, since the performance of VOP_POLL is critical we avoid
61020f1702c5SYu Xiangning * acquiring so_lock here. This is based on two assumptions:
61030f1702c5SYu Xiangning * - The poll implementation holds locks to serialize the VOP_POLL call
61040f1702c5SYu Xiangning * and a pollwakeup for the same pollhead. This ensures that should
61050f1702c5SYu Xiangning * e.g. so_state change during a socktpi_poll call the pollwakeup
61060f1702c5SYu Xiangning * (which strsock_* and strrput conspire to issue) is issued after
61070f1702c5SYu Xiangning * the state change. Thus the pollwakeup will block until VOP_POLL has
61080f1702c5SYu Xiangning * returned and then wake up poll and have it call VOP_POLL again.
61090f1702c5SYu Xiangning * - The reading of so_state without holding so_lock does not result in
61100f1702c5SYu Xiangning * stale data that is older than the latest state change that has dropped
61110f1702c5SYu Xiangning * so_lock. This is ensured by the mutex_exit issuing the appropriate
61120f1702c5SYu Xiangning * memory barrier to force the data into the coherency domain.
61130f1702c5SYu Xiangning */
61140f1702c5SYu Xiangning static int
sotpi_poll(struct sonode * so,short events,int anyyet,short * reventsp,struct pollhead ** phpp)61150f1702c5SYu Xiangning sotpi_poll(
61160f1702c5SYu Xiangning struct sonode *so,
61170f1702c5SYu Xiangning short events,
61180f1702c5SYu Xiangning int anyyet,
61190f1702c5SYu Xiangning short *reventsp,
61200f1702c5SYu Xiangning struct pollhead **phpp)
61210f1702c5SYu Xiangning {
61220f1702c5SYu Xiangning short origevents = events;
61230f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
61240f1702c5SYu Xiangning int error;
61250f1702c5SYu Xiangning int so_state = so->so_state; /* snapshot */
61260f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
61270f1702c5SYu Xiangning
61280f1702c5SYu Xiangning dprintso(so, 0, ("socktpi_poll(%p): state %s err %d\n",
61290f1702c5SYu Xiangning (void *)vp, pr_state(so_state, so->so_mode), so->so_error));
61300f1702c5SYu Xiangning
61310f1702c5SYu Xiangning ASSERT(vp->v_type == VSOCK);
61320f1702c5SYu Xiangning ASSERT(vp->v_stream != NULL);
61330f1702c5SYu Xiangning
61340f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
61350f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
61360f1702c5SYu Xiangning return (strpoll(vp->v_stream, events, anyyet,
61370f1702c5SYu Xiangning reventsp, phpp));
61380f1702c5SYu Xiangning }
61390f1702c5SYu Xiangning
61400f1702c5SYu Xiangning if (!(so_state & SS_ISCONNECTED) &&
61410f1702c5SYu Xiangning (so->so_mode & SM_CONNREQUIRED)) {
61420f1702c5SYu Xiangning /* Not connected yet - turn off write side events */
61430f1702c5SYu Xiangning events &= ~(POLLOUT|POLLWRBAND);
61440f1702c5SYu Xiangning }
61450f1702c5SYu Xiangning /*
61460f1702c5SYu Xiangning * Check for errors without calling strpoll if the caller wants them.
61470f1702c5SYu Xiangning * In sockets the errors are represented as input/output events
61480f1702c5SYu Xiangning * and there is no need to ask the stream head for this information.
61490f1702c5SYu Xiangning */
61500f1702c5SYu Xiangning if (so->so_error != 0 &&
61510f1702c5SYu Xiangning ((POLLIN|POLLRDNORM|POLLOUT) & origevents) != 0) {
61520f1702c5SYu Xiangning *reventsp = (POLLIN|POLLRDNORM|POLLOUT) & origevents;
61530f1702c5SYu Xiangning return (0);
61540f1702c5SYu Xiangning }
61550f1702c5SYu Xiangning /*
61560f1702c5SYu Xiangning * Ignore M_PROTO only messages such as the T_EXDATA_IND messages.
61570f1702c5SYu Xiangning * These message with only an M_PROTO/M_PCPROTO part and no M_DATA
61580f1702c5SYu Xiangning * will not trigger a POLLIN event with POLLRDDATA set.
61590f1702c5SYu Xiangning * The handling of urgent data (causing POLLRDBAND) is done by
61600f1702c5SYu Xiangning * inspecting SS_OOBPEND below.
61610f1702c5SYu Xiangning */
61620f1702c5SYu Xiangning events |= POLLRDDATA;
61630f1702c5SYu Xiangning
61640f1702c5SYu Xiangning /*
61650f1702c5SYu Xiangning * After shutdown(output) a stream head write error is set.
61660f1702c5SYu Xiangning * However, we should not return output events.
61670f1702c5SYu Xiangning */
61680f1702c5SYu Xiangning events |= POLLNOERR;
61690f1702c5SYu Xiangning error = strpoll(vp->v_stream, events, anyyet,
61700f1702c5SYu Xiangning reventsp, phpp);
61710f1702c5SYu Xiangning if (error)
61720f1702c5SYu Xiangning return (error);
61730f1702c5SYu Xiangning
61740f1702c5SYu Xiangning ASSERT(!(*reventsp & POLLERR));
61750f1702c5SYu Xiangning
61760f1702c5SYu Xiangning /*
61770f1702c5SYu Xiangning * Notes on T_CONN_IND handling for sockets.
61780f1702c5SYu Xiangning *
61790f1702c5SYu Xiangning * If strpoll() returned without events, SR_POLLIN is guaranteed
61800f1702c5SYu Xiangning * to be set, ensuring any subsequent strrput() runs pollwakeup().
61810f1702c5SYu Xiangning *
61820f1702c5SYu Xiangning * Since the so_lock is not held, soqueueconnind() may have run
61830f1702c5SYu Xiangning * and a T_CONN_IND may be waiting. We now check for any queued
61840f1702c5SYu Xiangning * T_CONN_IND msgs on sti_conn_ind_head and set appropriate events
61850f1702c5SYu Xiangning * to ensure poll returns.
61860f1702c5SYu Xiangning *
61870f1702c5SYu Xiangning * However:
61880f1702c5SYu Xiangning * If the T_CONN_IND hasn't arrived by the time strpoll() returns,
61890f1702c5SYu Xiangning * when strrput() does run for an arriving M_PROTO with T_CONN_IND
61900f1702c5SYu Xiangning * the following actions will occur; taken together they ensure the
61910f1702c5SYu Xiangning * syscall will return.
61920f1702c5SYu Xiangning *
61930f1702c5SYu Xiangning * 1. If a socket, soqueueconnind() will queue the T_CONN_IND but if
61940f1702c5SYu Xiangning * the accept() was run on a non-blocking socket sowaitconnind()
61950f1702c5SYu Xiangning * may have already returned EWOULDBLOCK, so not be waiting to
61960f1702c5SYu Xiangning * process the message. Additionally socktpi_poll() has probably
61970f1702c5SYu Xiangning * proceeded past the sti_conn_ind_head check below.
61980f1702c5SYu Xiangning * 2. strrput() runs pollwakeup()->pollnotify()->cv_signal() to wake
61990f1702c5SYu Xiangning * this thread, however that could occur before poll_common()
62000f1702c5SYu Xiangning * has entered cv_wait.
62010f1702c5SYu Xiangning * 3. pollnotify() sets T_POLLWAKE, while holding the pc_lock.
62020f1702c5SYu Xiangning *
62030f1702c5SYu Xiangning * Before proceeding to cv_wait() in poll_common() for an event,
62040f1702c5SYu Xiangning * poll_common() atomically checks for T_POLLWAKE under the pc_lock,
62050f1702c5SYu Xiangning * and if set, re-calls strpoll() to ensure the late arriving
62060f1702c5SYu Xiangning * T_CONN_IND is recognized, and pollsys() returns.
62070f1702c5SYu Xiangning */
62080f1702c5SYu Xiangning
62090f1702c5SYu Xiangning if (sti->sti_conn_ind_head != NULL)
62100f1702c5SYu Xiangning *reventsp |= (POLLIN|POLLRDNORM) & events;
62110f1702c5SYu Xiangning
6212075fab9aSBryan Cantrill if (so->so_state & SS_CANTRCVMORE) {
6213075fab9aSBryan Cantrill *reventsp |= POLLRDHUP & events;
6214075fab9aSBryan Cantrill
6215075fab9aSBryan Cantrill if (so->so_state & SS_CANTSENDMORE)
6216075fab9aSBryan Cantrill *reventsp |= POLLHUP;
6217075fab9aSBryan Cantrill }
6218075fab9aSBryan Cantrill
62190f1702c5SYu Xiangning if (so->so_state & SS_OOBPEND)
62200f1702c5SYu Xiangning *reventsp |= POLLRDBAND & events;
62210f1702c5SYu Xiangning
62220f1702c5SYu Xiangning return (0);
62230f1702c5SYu Xiangning }
62240f1702c5SYu Xiangning
62250f1702c5SYu Xiangning /*ARGSUSED*/
62260f1702c5SYu Xiangning static int
socktpi_constructor(void * buf,void * cdrarg,int kmflags)62270f1702c5SYu Xiangning socktpi_constructor(void *buf, void *cdrarg, int kmflags)
62280f1702c5SYu Xiangning {
62290f1702c5SYu Xiangning sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
62300f1702c5SYu Xiangning int error = 0;
62310f1702c5SYu Xiangning
62320f1702c5SYu Xiangning error = sonode_constructor(buf, cdrarg, kmflags);
62330f1702c5SYu Xiangning if (error != 0)
62340f1702c5SYu Xiangning return (error);
62350f1702c5SYu Xiangning
62360f1702c5SYu Xiangning error = i_sotpi_info_constructor(&st->st_info);
62370f1702c5SYu Xiangning if (error != 0)
62380f1702c5SYu Xiangning sonode_destructor(buf, cdrarg);
62390f1702c5SYu Xiangning
62400f1702c5SYu Xiangning st->st_sonode.so_priv = &st->st_info;
62410f1702c5SYu Xiangning
62420f1702c5SYu Xiangning return (error);
62430f1702c5SYu Xiangning }
62440f1702c5SYu Xiangning
62450f1702c5SYu Xiangning /*ARGSUSED1*/
62460f1702c5SYu Xiangning static void
socktpi_destructor(void * buf,void * cdrarg)62470f1702c5SYu Xiangning socktpi_destructor(void *buf, void *cdrarg)
62480f1702c5SYu Xiangning {
62490f1702c5SYu Xiangning sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
62500f1702c5SYu Xiangning
62510f1702c5SYu Xiangning ASSERT(st->st_sonode.so_priv == &st->st_info);
62520f1702c5SYu Xiangning st->st_sonode.so_priv = NULL;
62530f1702c5SYu Xiangning
62540f1702c5SYu Xiangning i_sotpi_info_destructor(&st->st_info);
62550f1702c5SYu Xiangning sonode_destructor(buf, cdrarg);
62560f1702c5SYu Xiangning }
62570f1702c5SYu Xiangning
62580f1702c5SYu Xiangning static int
socktpi_unix_constructor(void * buf,void * cdrarg,int kmflags)62590f1702c5SYu Xiangning socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags)
62600f1702c5SYu Xiangning {
62610f1702c5SYu Xiangning int retval;
62620f1702c5SYu Xiangning
62630f1702c5SYu Xiangning if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) {
62640f1702c5SYu Xiangning struct sonode *so = (struct sonode *)buf;
62650f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
62660f1702c5SYu Xiangning
62670f1702c5SYu Xiangning mutex_enter(&socklist.sl_lock);
62680f1702c5SYu Xiangning
62690f1702c5SYu Xiangning sti->sti_next_so = socklist.sl_list;
62700f1702c5SYu Xiangning sti->sti_prev_so = NULL;
62710f1702c5SYu Xiangning if (sti->sti_next_so != NULL)
62720f1702c5SYu Xiangning SOTOTPI(sti->sti_next_so)->sti_prev_so = so;
62730f1702c5SYu Xiangning socklist.sl_list = so;
62740f1702c5SYu Xiangning
62750f1702c5SYu Xiangning mutex_exit(&socklist.sl_lock);
62760f1702c5SYu Xiangning
62770f1702c5SYu Xiangning }
62780f1702c5SYu Xiangning return (retval);
62790f1702c5SYu Xiangning }
62800f1702c5SYu Xiangning
62810f1702c5SYu Xiangning static void
socktpi_unix_destructor(void * buf,void * cdrarg)62820f1702c5SYu Xiangning socktpi_unix_destructor(void *buf, void *cdrarg)
62830f1702c5SYu Xiangning {
62840f1702c5SYu Xiangning struct sonode *so = (struct sonode *)buf;
62850f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
62860f1702c5SYu Xiangning
62870f1702c5SYu Xiangning mutex_enter(&socklist.sl_lock);
62880f1702c5SYu Xiangning
62890f1702c5SYu Xiangning if (sti->sti_next_so != NULL)
62900f1702c5SYu Xiangning SOTOTPI(sti->sti_next_so)->sti_prev_so = sti->sti_prev_so;
62910f1702c5SYu Xiangning if (sti->sti_prev_so != NULL)
62920f1702c5SYu Xiangning SOTOTPI(sti->sti_prev_so)->sti_next_so = sti->sti_next_so;
62930f1702c5SYu Xiangning else
62940f1702c5SYu Xiangning socklist.sl_list = sti->sti_next_so;
62950f1702c5SYu Xiangning
62960f1702c5SYu Xiangning mutex_exit(&socklist.sl_lock);
62970f1702c5SYu Xiangning
62980f1702c5SYu Xiangning socktpi_destructor(buf, cdrarg);
62990f1702c5SYu Xiangning }
63000f1702c5SYu Xiangning
63010f1702c5SYu Xiangning int
socktpi_init(void)63020f1702c5SYu Xiangning socktpi_init(void)
63030f1702c5SYu Xiangning {
63040f1702c5SYu Xiangning /*
63050f1702c5SYu Xiangning * Create sonode caches. We create a special one for AF_UNIX so
6306bbf21555SRichard Lowe * that we can track them for netstat(8).
63070f1702c5SYu Xiangning */
63080f1702c5SYu Xiangning socktpi_cache = kmem_cache_create("socktpi_cache",
63090f1702c5SYu Xiangning sizeof (struct sotpi_sonode), 0, socktpi_constructor,
63100f1702c5SYu Xiangning socktpi_destructor, NULL, NULL, NULL, 0);
63110f1702c5SYu Xiangning
63120f1702c5SYu Xiangning socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache",
63130f1702c5SYu Xiangning sizeof (struct sotpi_sonode), 0, socktpi_unix_constructor,
63140f1702c5SYu Xiangning socktpi_unix_destructor, NULL, NULL, NULL, 0);
63150f1702c5SYu Xiangning
63160f1702c5SYu Xiangning return (0);
63170f1702c5SYu Xiangning }
63180f1702c5SYu Xiangning
63190f1702c5SYu Xiangning /*
63200f1702c5SYu Xiangning * Given a non-TPI sonode, allocate and prep it to be ready for TPI.
63210f1702c5SYu Xiangning *
63220f1702c5SYu Xiangning * Caller must still update state and mode using sotpi_update_state().
63230f1702c5SYu Xiangning */
632441174437SAnders Persson int
sotpi_convert_sonode(struct sonode * so,struct sockparams * newsp,boolean_t * direct,queue_t ** qp,struct cred * cr)63250f1702c5SYu Xiangning sotpi_convert_sonode(struct sonode *so, struct sockparams *newsp,
632641174437SAnders Persson boolean_t *direct, queue_t **qp, struct cred *cr)
63270f1702c5SYu Xiangning {
63280f1702c5SYu Xiangning sotpi_info_t *sti;
63290f1702c5SYu Xiangning struct sockparams *origsp = so->so_sockparams;
63300f1702c5SYu Xiangning sock_lower_handle_t handle = so->so_proto_handle;
63310f1702c5SYu Xiangning struct stdata *stp;
63320f1702c5SYu Xiangning struct vnode *vp;
63330f1702c5SYu Xiangning queue_t *q;
633441174437SAnders Persson int error = 0;
63350f1702c5SYu Xiangning
633641174437SAnders Persson ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
633741174437SAnders Persson SS_FALLBACK_PENDING);
633841174437SAnders Persson ASSERT(SOCK_IS_NONSTR(so));
633941174437SAnders Persson
634041174437SAnders Persson *qp = NULL;
63410f1702c5SYu Xiangning *direct = B_FALSE;
63420f1702c5SYu Xiangning so->so_sockparams = newsp;
63430f1702c5SYu Xiangning /*
63440f1702c5SYu Xiangning * Allocate and initalize fields required by TPI.
63450f1702c5SYu Xiangning */
63460f1702c5SYu Xiangning (void) sotpi_info_create(so, KM_SLEEP);
63470f1702c5SYu Xiangning sotpi_info_init(so);
63480f1702c5SYu Xiangning
634941174437SAnders Persson if ((error = sotpi_init(so, NULL, cr, SO_FALLBACK)) != 0) {
63500f1702c5SYu Xiangning sotpi_info_fini(so);
63510f1702c5SYu Xiangning sotpi_info_destroy(so);
635241174437SAnders Persson return (error);
63530f1702c5SYu Xiangning }
63540f1702c5SYu Xiangning ASSERT(handle == so->so_proto_handle);
63550f1702c5SYu Xiangning sti = SOTOTPI(so);
63560f1702c5SYu Xiangning if (sti->sti_direct != 0)
63570f1702c5SYu Xiangning *direct = B_TRUE;
63580f1702c5SYu Xiangning
63590f1702c5SYu Xiangning /*
63600f1702c5SYu Xiangning * Keep the original sp around so we can properly dispose of the
63610f1702c5SYu Xiangning * sonode when the socket is being closed.
63620f1702c5SYu Xiangning */
63630f1702c5SYu Xiangning sti->sti_orig_sp = origsp;
63640f1702c5SYu Xiangning
63650f1702c5SYu Xiangning so_basic_strinit(so); /* skips the T_CAPABILITY_REQ */
63660f1702c5SYu Xiangning so_alloc_addr(so, so->so_max_addr_len);
63670f1702c5SYu Xiangning
63680f1702c5SYu Xiangning /*
63690f1702c5SYu Xiangning * If the application has done a SIOCSPGRP, make sure the
63700f1702c5SYu Xiangning * STREAM head is aware. This needs to take place before
63710f1702c5SYu Xiangning * the protocol start sending up messages. Otherwise we
63720f1702c5SYu Xiangning * might miss to generate SIGPOLL.
63730f1702c5SYu Xiangning *
63740f1702c5SYu Xiangning * It is possible that the application will receive duplicate
63750f1702c5SYu Xiangning * signals if some were already generated for either data or
63760f1702c5SYu Xiangning * connection indications.
63770f1702c5SYu Xiangning */
63780f1702c5SYu Xiangning if (so->so_pgrp != 0) {
63790f1702c5SYu Xiangning if (so_set_events(so, so->so_vnode, cr) != 0)
63800f1702c5SYu Xiangning so->so_pgrp = 0;
63810f1702c5SYu Xiangning }
63820f1702c5SYu Xiangning
63830f1702c5SYu Xiangning /*
63840f1702c5SYu Xiangning * Determine which queue to use.
63850f1702c5SYu Xiangning */
63860f1702c5SYu Xiangning vp = SOTOV(so);
63870f1702c5SYu Xiangning stp = vp->v_stream;
63880f1702c5SYu Xiangning ASSERT(stp != NULL);
63890f1702c5SYu Xiangning q = stp->sd_wrq->q_next;
63900f1702c5SYu Xiangning
63910f1702c5SYu Xiangning /*
63920f1702c5SYu Xiangning * Skip any modules that may have been auto pushed when the device
63930f1702c5SYu Xiangning * was opened
63940f1702c5SYu Xiangning */
63950f1702c5SYu Xiangning while (q->q_next != NULL)
63960f1702c5SYu Xiangning q = q->q_next;
639741174437SAnders Persson *qp = _RD(q);
63980f1702c5SYu Xiangning
639941174437SAnders Persson /* This is now a STREAMS sockets */
640041174437SAnders Persson so->so_not_str = B_FALSE;
640141174437SAnders Persson
640241174437SAnders Persson return (error);
640341174437SAnders Persson }
640441174437SAnders Persson
640541174437SAnders Persson /*
640641174437SAnders Persson * Revert a TPI sonode. It is only allowed to revert the sonode during
640741174437SAnders Persson * the fallback process.
640841174437SAnders Persson */
640941174437SAnders Persson void
sotpi_revert_sonode(struct sonode * so,struct cred * cr)641041174437SAnders Persson sotpi_revert_sonode(struct sonode *so, struct cred *cr)
641141174437SAnders Persson {
641241174437SAnders Persson vnode_t *vp = SOTOV(so);
641341174437SAnders Persson
641441174437SAnders Persson ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
641541174437SAnders Persson SS_FALLBACK_PENDING);
641641174437SAnders Persson ASSERT(!SOCK_IS_NONSTR(so));
641741174437SAnders Persson ASSERT(vp->v_stream != NULL);
641841174437SAnders Persson
641941174437SAnders Persson strclean(vp);
642041174437SAnders Persson (void) strclose(vp, FREAD|FWRITE|SO_FALLBACK, cr);
642141174437SAnders Persson
642241174437SAnders Persson /*
642341174437SAnders Persson * Restore the original sockparams. The caller is responsible for
642441174437SAnders Persson * dropping the ref to the new sp.
642541174437SAnders Persson */
642641174437SAnders Persson so->so_sockparams = SOTOTPI(so)->sti_orig_sp;
642741174437SAnders Persson
642841174437SAnders Persson sotpi_info_fini(so);
642941174437SAnders Persson sotpi_info_destroy(so);
643041174437SAnders Persson
643141174437SAnders Persson /* This is no longer a STREAMS sockets */
643241174437SAnders Persson so->so_not_str = B_TRUE;
64330f1702c5SYu Xiangning }
64340f1702c5SYu Xiangning
64350f1702c5SYu Xiangning void
sotpi_update_state(struct sonode * so,struct T_capability_ack * tcap,struct sockaddr * laddr,socklen_t laddrlen,struct sockaddr * faddr,socklen_t faddrlen,short opts)64360f1702c5SYu Xiangning sotpi_update_state(struct sonode *so, struct T_capability_ack *tcap,
64370f1702c5SYu Xiangning struct sockaddr *laddr, socklen_t laddrlen, struct sockaddr *faddr,
64380f1702c5SYu Xiangning socklen_t faddrlen, short opts)
64390f1702c5SYu Xiangning {
64400f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
64410f1702c5SYu Xiangning
64420f1702c5SYu Xiangning so_proc_tcapability_ack(so, tcap);
64430f1702c5SYu Xiangning
64440f1702c5SYu Xiangning so->so_options |= opts;
64450f1702c5SYu Xiangning
64460f1702c5SYu Xiangning /*
64470f1702c5SYu Xiangning * Determine whether the foreign and local address are valid
64480f1702c5SYu Xiangning */
64490f1702c5SYu Xiangning if (laddrlen != 0) {
64500f1702c5SYu Xiangning ASSERT(laddrlen <= sti->sti_laddr_maxlen);
64510f1702c5SYu Xiangning sti->sti_laddr_len = laddrlen;
64520f1702c5SYu Xiangning bcopy(laddr, sti->sti_laddr_sa, laddrlen);
64530f1702c5SYu Xiangning sti->sti_laddr_valid = (so->so_state & SS_ISBOUND);
64540f1702c5SYu Xiangning }
64550f1702c5SYu Xiangning
64560f1702c5SYu Xiangning if (faddrlen != 0) {
64570f1702c5SYu Xiangning ASSERT(faddrlen <= sti->sti_faddr_maxlen);
64580f1702c5SYu Xiangning sti->sti_faddr_len = faddrlen;
64590f1702c5SYu Xiangning bcopy(faddr, sti->sti_faddr_sa, faddrlen);
64600f1702c5SYu Xiangning sti->sti_faddr_valid = (so->so_state & SS_ISCONNECTED);
64610f1702c5SYu Xiangning }
64620f1702c5SYu Xiangning
64630f1702c5SYu Xiangning }
64640f1702c5SYu Xiangning
64650f1702c5SYu Xiangning /*
64660f1702c5SYu Xiangning * Allocate enough space to cache the local and foreign addresses.
64670f1702c5SYu Xiangning */
64680f1702c5SYu Xiangning void
so_alloc_addr(struct sonode * so,t_uscalar_t maxlen)64690f1702c5SYu Xiangning so_alloc_addr(struct sonode *so, t_uscalar_t maxlen)
64700f1702c5SYu Xiangning {
64710f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
64720f1702c5SYu Xiangning
64730f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
64740f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len == 0 && sti->sti_faddr_len == 0);
64750f1702c5SYu Xiangning sti->sti_laddr_maxlen = sti->sti_faddr_maxlen =
64760f1702c5SYu Xiangning P2ROUNDUP(maxlen, KMEM_ALIGN);
64770f1702c5SYu Xiangning so->so_max_addr_len = sti->sti_laddr_maxlen;
64780f1702c5SYu Xiangning sti->sti_laddr_sa = kmem_alloc(sti->sti_laddr_maxlen * 2, KM_SLEEP);
64790f1702c5SYu Xiangning sti->sti_faddr_sa = (struct sockaddr *)((caddr_t)sti->sti_laddr_sa
64800f1702c5SYu Xiangning + sti->sti_laddr_maxlen);
64810f1702c5SYu Xiangning
64820f1702c5SYu Xiangning if (so->so_family == AF_UNIX) {
64830f1702c5SYu Xiangning /*
64840f1702c5SYu Xiangning * Initialize AF_UNIX related fields.
64850f1702c5SYu Xiangning */
64860f1702c5SYu Xiangning bzero(&sti->sti_ux_laddr, sizeof (sti->sti_ux_laddr));
64870f1702c5SYu Xiangning bzero(&sti->sti_ux_faddr, sizeof (sti->sti_ux_faddr));
64880f1702c5SYu Xiangning }
64890f1702c5SYu Xiangning }
64900f1702c5SYu Xiangning
64910f1702c5SYu Xiangning
64920f1702c5SYu Xiangning sotpi_info_t *
sotpi_sototpi(struct sonode * so)64930f1702c5SYu Xiangning sotpi_sototpi(struct sonode *so)
64940f1702c5SYu Xiangning {
64950f1702c5SYu Xiangning sotpi_info_t *sti;
64960f1702c5SYu Xiangning
649741174437SAnders Persson ASSERT(so != NULL);
64980f1702c5SYu Xiangning
64990f1702c5SYu Xiangning sti = (sotpi_info_t *)so->so_priv;
65000f1702c5SYu Xiangning
65010f1702c5SYu Xiangning ASSERT(sti != NULL);
65020f1702c5SYu Xiangning ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
65030f1702c5SYu Xiangning
65040f1702c5SYu Xiangning return (sti);
65050f1702c5SYu Xiangning }
65060f1702c5SYu Xiangning
65070f1702c5SYu Xiangning static int
i_sotpi_info_constructor(sotpi_info_t * sti)65080f1702c5SYu Xiangning i_sotpi_info_constructor(sotpi_info_t *sti)
65090f1702c5SYu Xiangning {
65100f1702c5SYu Xiangning sti->sti_magic = SOTPI_INFO_MAGIC;
65110f1702c5SYu Xiangning sti->sti_ack_mp = NULL;
65120f1702c5SYu Xiangning sti->sti_discon_ind_mp = NULL;
65130f1702c5SYu Xiangning sti->sti_ux_bound_vp = NULL;
65140f1702c5SYu Xiangning sti->sti_unbind_mp = NULL;
65150f1702c5SYu Xiangning
65160f1702c5SYu Xiangning sti->sti_conn_ind_head = NULL;
65170f1702c5SYu Xiangning sti->sti_conn_ind_tail = NULL;
65180f1702c5SYu Xiangning
65190f1702c5SYu Xiangning sti->sti_laddr_sa = NULL;
65200f1702c5SYu Xiangning sti->sti_faddr_sa = NULL;
65210f1702c5SYu Xiangning
65220f1702c5SYu Xiangning mutex_init(&sti->sti_plumb_lock, NULL, MUTEX_DEFAULT, NULL);
65230f1702c5SYu Xiangning cv_init(&sti->sti_ack_cv, NULL, CV_DEFAULT, NULL);
65240f1702c5SYu Xiangning
65250f1702c5SYu Xiangning return (0);
65260f1702c5SYu Xiangning }
65270f1702c5SYu Xiangning
65280f1702c5SYu Xiangning static void
i_sotpi_info_destructor(sotpi_info_t * sti)65290f1702c5SYu Xiangning i_sotpi_info_destructor(sotpi_info_t *sti)
65300f1702c5SYu Xiangning {
65310f1702c5SYu Xiangning ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
65320f1702c5SYu Xiangning ASSERT(sti->sti_ack_mp == NULL);
65330f1702c5SYu Xiangning ASSERT(sti->sti_discon_ind_mp == NULL);
65340f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
65350f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp == NULL);
65360f1702c5SYu Xiangning
65370f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_head == NULL);
65380f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_tail == NULL);
65390f1702c5SYu Xiangning
65400f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL);
65410f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa == NULL);
65420f1702c5SYu Xiangning
65430f1702c5SYu Xiangning mutex_destroy(&sti->sti_plumb_lock);
65440f1702c5SYu Xiangning cv_destroy(&sti->sti_ack_cv);
65450f1702c5SYu Xiangning }
65460f1702c5SYu Xiangning
65470f1702c5SYu Xiangning /*
65480f1702c5SYu Xiangning * Creates and attaches TPI information to the given sonode
65490f1702c5SYu Xiangning */
65500f1702c5SYu Xiangning static boolean_t
sotpi_info_create(struct sonode * so,int kmflags)65510f1702c5SYu Xiangning sotpi_info_create(struct sonode *so, int kmflags)
65520f1702c5SYu Xiangning {
65530f1702c5SYu Xiangning sotpi_info_t *sti;
65540f1702c5SYu Xiangning
65550f1702c5SYu Xiangning ASSERT(so->so_priv == NULL);
65560f1702c5SYu Xiangning
65570f1702c5SYu Xiangning if ((sti = kmem_zalloc(sizeof (*sti), kmflags)) == NULL)
65580f1702c5SYu Xiangning return (B_FALSE);
65590f1702c5SYu Xiangning
65600f1702c5SYu Xiangning if (i_sotpi_info_constructor(sti) != 0) {
65610f1702c5SYu Xiangning kmem_free(sti, sizeof (*sti));
65620f1702c5SYu Xiangning return (B_FALSE);
65630f1702c5SYu Xiangning }
65640f1702c5SYu Xiangning
65650f1702c5SYu Xiangning so->so_priv = (void *)sti;
65660f1702c5SYu Xiangning return (B_TRUE);
65670f1702c5SYu Xiangning }
65680f1702c5SYu Xiangning
65690f1702c5SYu Xiangning /*
65700f1702c5SYu Xiangning * Initializes the TPI information.
65710f1702c5SYu Xiangning */
65720f1702c5SYu Xiangning static void
sotpi_info_init(struct sonode * so)65730f1702c5SYu Xiangning sotpi_info_init(struct sonode *so)
65740f1702c5SYu Xiangning {
65750f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
65760f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
65770f1702c5SYu Xiangning time_t now;
65780f1702c5SYu Xiangning
6579e9f74ea5SToomas Soome sti->sti_dev = so->so_sockparams->sp_sdev_info.sd_vnode->v_rdev;
65800f1702c5SYu Xiangning vp->v_rdev = sti->sti_dev;
65810f1702c5SYu Xiangning
65820f1702c5SYu Xiangning sti->sti_orig_sp = NULL;
65830f1702c5SYu Xiangning
65840f1702c5SYu Xiangning sti->sti_pushcnt = 0;
65850f1702c5SYu Xiangning
65860f1702c5SYu Xiangning now = gethrestime_sec();
65870f1702c5SYu Xiangning sti->sti_atime = now;
65880f1702c5SYu Xiangning sti->sti_mtime = now;
65890f1702c5SYu Xiangning sti->sti_ctime = now;
65900f1702c5SYu Xiangning
65910f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
65920f1702c5SYu Xiangning sti->sti_delayed_error = 0;
65930f1702c5SYu Xiangning
65940f1702c5SYu Xiangning sti->sti_provinfo = NULL;
65950f1702c5SYu Xiangning
65960f1702c5SYu Xiangning sti->sti_oobcnt = 0;
65970f1702c5SYu Xiangning sti->sti_oobsigcnt = 0;
65980f1702c5SYu Xiangning
65990f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
66000f1702c5SYu Xiangning
66010f1702c5SYu Xiangning sti->sti_laddr_sa = 0;
66020f1702c5SYu Xiangning sti->sti_faddr_sa = 0;
66030f1702c5SYu Xiangning sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 0;
66040f1702c5SYu Xiangning sti->sti_laddr_len = sti->sti_faddr_len = 0;
66050f1702c5SYu Xiangning
66060f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
66070f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
66080f1702c5SYu Xiangning sti->sti_faddr_noxlate = 0;
66090f1702c5SYu Xiangning
66100f1702c5SYu Xiangning sti->sti_direct = 0;
66110f1702c5SYu Xiangning
66120f1702c5SYu Xiangning ASSERT(sti->sti_ack_mp == NULL);
66130f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
66140f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp == NULL);
66150f1702c5SYu Xiangning
66160f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_head == NULL);
66170f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_tail == NULL);
66180f1702c5SYu Xiangning }
66190f1702c5SYu Xiangning
66200f1702c5SYu Xiangning /*
66210f1702c5SYu Xiangning * Given a sonode, grab the TPI info and free any data.
66220f1702c5SYu Xiangning */
66230f1702c5SYu Xiangning static void
sotpi_info_fini(struct sonode * so)66240f1702c5SYu Xiangning sotpi_info_fini(struct sonode *so)
66250f1702c5SYu Xiangning {
66260f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
66270f1702c5SYu Xiangning mblk_t *mp;
66280f1702c5SYu Xiangning
66290f1702c5SYu Xiangning ASSERT(sti->sti_discon_ind_mp == NULL);
66300f1702c5SYu Xiangning
66310f1702c5SYu Xiangning if ((mp = sti->sti_conn_ind_head) != NULL) {
66320f1702c5SYu Xiangning mblk_t *mp1;
66330f1702c5SYu Xiangning
66340f1702c5SYu Xiangning while (mp) {
66350f1702c5SYu Xiangning mp1 = mp->b_next;
66360f1702c5SYu Xiangning mp->b_next = NULL;
66370f1702c5SYu Xiangning freemsg(mp);
66380f1702c5SYu Xiangning mp = mp1;
66390f1702c5SYu Xiangning }
66400f1702c5SYu Xiangning sti->sti_conn_ind_head = sti->sti_conn_ind_tail = NULL;
66410f1702c5SYu Xiangning }
66420f1702c5SYu Xiangning
66430f1702c5SYu Xiangning /*
66440f1702c5SYu Xiangning * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely
66450f1702c5SYu Xiangning * indirect them. It also uses so_count as a validity test.
66460f1702c5SYu Xiangning */
66470f1702c5SYu Xiangning mutex_enter(&so->so_lock);
66480f1702c5SYu Xiangning
66490f1702c5SYu Xiangning if (sti->sti_laddr_sa) {
66500f1702c5SYu Xiangning ASSERT((caddr_t)sti->sti_faddr_sa ==
66510f1702c5SYu Xiangning (caddr_t)sti->sti_laddr_sa + sti->sti_laddr_maxlen);
66520f1702c5SYu Xiangning ASSERT(sti->sti_faddr_maxlen == sti->sti_laddr_maxlen);
66530f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
66540f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
66550f1702c5SYu Xiangning kmem_free(sti->sti_laddr_sa, sti->sti_laddr_maxlen * 2);
66560f1702c5SYu Xiangning sti->sti_laddr_sa = NULL;
66570f1702c5SYu Xiangning sti->sti_laddr_len = sti->sti_laddr_maxlen = 0;
66580f1702c5SYu Xiangning sti->sti_faddr_sa = NULL;
66590f1702c5SYu Xiangning sti->sti_faddr_len = sti->sti_faddr_maxlen = 0;
66600f1702c5SYu Xiangning }
66610f1702c5SYu Xiangning
66620f1702c5SYu Xiangning mutex_exit(&so->so_lock);
66630f1702c5SYu Xiangning
66640f1702c5SYu Xiangning if ((mp = sti->sti_eaddr_mp) != NULL) {
66650f1702c5SYu Xiangning freemsg(mp);
66660f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
66670f1702c5SYu Xiangning sti->sti_delayed_error = 0;
66680f1702c5SYu Xiangning }
66690f1702c5SYu Xiangning
66700f1702c5SYu Xiangning if ((mp = sti->sti_ack_mp) != NULL) {
66710f1702c5SYu Xiangning freemsg(mp);
66720f1702c5SYu Xiangning sti->sti_ack_mp = NULL;
66730f1702c5SYu Xiangning }
66740f1702c5SYu Xiangning
66750f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
66760f1702c5SYu Xiangning if ((mp = sti->sti_unbind_mp) != NULL) {
66770f1702c5SYu Xiangning freemsg(mp);
66780f1702c5SYu Xiangning sti->sti_unbind_mp = NULL;
66790f1702c5SYu Xiangning }
66800f1702c5SYu Xiangning }
66810f1702c5SYu Xiangning
66820f1702c5SYu Xiangning /*
66830f1702c5SYu Xiangning * Destroys the TPI information attached to a sonode.
66840f1702c5SYu Xiangning */
66850f1702c5SYu Xiangning static void
sotpi_info_destroy(struct sonode * so)66860f1702c5SYu Xiangning sotpi_info_destroy(struct sonode *so)
66870f1702c5SYu Xiangning {
66880f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
66890f1702c5SYu Xiangning
66900f1702c5SYu Xiangning i_sotpi_info_destructor(sti);
66910f1702c5SYu Xiangning kmem_free(sti, sizeof (*sti));
66920f1702c5SYu Xiangning
66930f1702c5SYu Xiangning so->so_priv = NULL;
66940f1702c5SYu Xiangning }
66950f1702c5SYu Xiangning
66960f1702c5SYu Xiangning /*
66972691240cSYu Xiangning * Create the global sotpi socket module entry. It will never be freed.
66980f1702c5SYu Xiangning */
66990f1702c5SYu Xiangning smod_info_t *
sotpi_smod_create(void)67000f1702c5SYu Xiangning sotpi_smod_create(void)
67010f1702c5SYu Xiangning {
67020f1702c5SYu Xiangning smod_info_t *smodp;
67030f1702c5SYu Xiangning
67040f1702c5SYu Xiangning smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP);
67052691240cSYu Xiangning smodp->smod_name = kmem_alloc(sizeof (SOTPI_SMOD_NAME), KM_SLEEP);
67062691240cSYu Xiangning (void) strcpy(smodp->smod_name, SOTPI_SMOD_NAME);
67070f1702c5SYu Xiangning /*
67082691240cSYu Xiangning * Initialize the smod_refcnt to 1 so it will never be freed.
67090f1702c5SYu Xiangning */
67100f1702c5SYu Xiangning smodp->smod_refcnt = 1;
67110f1702c5SYu Xiangning smodp->smod_uc_version = SOCK_UC_VERSION;
67120f1702c5SYu Xiangning smodp->smod_dc_version = SOCK_DC_VERSION;
67130f1702c5SYu Xiangning smodp->smod_sock_create_func = &sotpi_create;
67140f1702c5SYu Xiangning smodp->smod_sock_destroy_func = &sotpi_destroy;
67150f1702c5SYu Xiangning return (smodp);
67160f1702c5SYu Xiangning }
6717