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 *
897c478bd9Sstevel@tonic-gate * 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
947c478bd9Sstevel@tonic-gate * 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 *);
1830f1702c5SYu Xiangning static void sotpi_info_fini(struct sonode *);
1840f1702c5SYu Xiangning 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 */
2030f1702c5SYu Xiangning 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 *);
2310f1702c5SYu Xiangning static int sotpi_ioctl(struct sonode *, int, intptr_t, int, struct cred *,
2320f1702c5SYu Xiangning int32_t *);
2330f1702c5SYu Xiangning static int socktpi_plumbioctl(struct vnode *, int, intptr_t, int,
2340f1702c5SYu Xiangning struct cred *, int32_t *);
2350f1702c5SYu Xiangning static int sotpi_poll(struct sonode *, short, int, short *,
2360f1702c5SYu Xiangning struct pollhead **);
2370f1702c5SYu Xiangning static int sotpi_close(struct sonode *, int, struct cred *);
2380f1702c5SYu Xiangning
2390f1702c5SYu Xiangning static int i_sotpi_info_constructor(sotpi_info_t *);
2400f1702c5SYu Xiangning 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
2617c478bd9Sstevel@tonic-gate /*
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
2690f1702c5SYu Xiangning /*
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;
2800f1702c5SYu Xiangning kmem_cache_t *cp;
2817c478bd9Sstevel@tonic-gate
2820f1702c5SYu Xiangning ASSERT(sp->sp_sdev_info.sd_vnode != NULL);
2837c478bd9Sstevel@tonic-gate
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 }
299ff550d0eSmasputra
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;
3790f1702c5SYu Xiangning
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;
3850f1702c5SYu Xiangning
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 */
3917c478bd9Sstevel@tonic-gate 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);
4350f1702c5SYu Xiangning
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);
4440f1702c5SYu Xiangning
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 */
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate
4540f1702c5SYu Xiangning ASSERT(stp->sd_wrq != NULL);
4550f1702c5SYu Xiangning sti->sti_provinfo = tpi_findprov(stp->sd_wrq);
4560f1702c5SYu Xiangning
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 }
52534793d0fSToomas Soome error = so_strinit(so, tso);
52634793d0fSToomas Soome if (error != 0) {
5270f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
5280f1702c5SYu Xiangning return (error);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate
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 }
5567c478bd9Sstevel@tonic-gate
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",
646903a11ebSrh87107 (void *)so, (void *)name, namelen, backlog, flags,
6477c478bd9Sstevel@tonic-gate 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,
686903a11ebSrh87107 (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 =
7127c478bd9Sstevel@tonic-gate (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 &=
7967c478bd9Sstevel@tonic-gate ~(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,
9787c478bd9Sstevel@tonic-gate 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,
10597c478bd9Sstevel@tonic-gate 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,
11067c478bd9Sstevel@tonic-gate bind_ack->ADDR_length,
11077c478bd9Sstevel@tonic-gate __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,
12547c478bd9Sstevel@tonic-gate ("addrlen %d, addr 0x%x, vp %p\n",
12557c478bd9Sstevel@tonic-gate 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",
1377903a11ebSrh87107 (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,
14007c478bd9Sstevel@tonic-gate 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",
1456903a11ebSrh87107 (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) &&
14707c478bd9Sstevel@tonic-gate /*CONSTCOND*/
14717c478bd9Sstevel@tonic-gate !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",
1543903a11ebSrh87107 (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,
15747c478bd9Sstevel@tonic-gate 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;
1624188658baSja97890 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",
1629903a11ebSrh87107 (void *)so, fflag, (void *)nsop,
1630903a11ebSrh87107 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;
1641*b9d2f05bSToomas Soome
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))) {
17707c478bd9Sstevel@tonic-gate 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.
1811188658baSja97890 * We set the local address in the sonode from the T_OK_ACK of the
1812188658baSja97890 * T_CONN_RES. For this reason the address we bind to here isn't
1813188658baSja97890 * 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
19297c478bd9Sstevel@tonic-gate /*
19307d6c035bSja97890 * 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
19337d6c035bSja97890 * we issue a _SIOCSOCKFALLBACK to inform the transport
19347d6c035bSja97890 * as we would in the I_PUSH case.
19357d6c035bSja97890 */
19360f1702c5SYu Xiangning if (nsti->sti_direct == 0) {
19377d6c035bSja97890 int rval;
19387d6c035bSja97890
19397d6c035bSja97890 if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
1940de8c4a14SErik Nordmark 0, 0, K_TO_K, cr, &rval)) != 0) {
19417d6c035bSja97890 mutex_enter(&so->so_lock);
19427d6c035bSja97890 so_lock_single(so);
19437d6c035bSja97890 eprintsoline(so, error);
19447d6c035bSja97890 goto disconnect_vp;
19457d6c035bSja97890 }
19467d6c035bSja97890 }
19477d6c035bSja97890
19487d6c035bSja97890 /*
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 }
2010188658baSja97890 error = sowaitprim(so, PRIM_type, T_OK_ACK,
2011188658baSja97890 (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);
2017188658baSja97890 /*
2018188658baSja97890 * If there is a sin/sin6 appended onto the T_OK_ACK use
2019188658baSja97890 * that to set the local address. If this is not present
2020188658baSja97890 * then we zero out the address and don't set the
20210f1702c5SYu Xiangning * sti_laddr_valid bit. For AF_UNIX endpoints we copy over
20226e81d8daSja97890 * 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.
2028188658baSja97890 */
2029881776cfSGeorge Shepherd mutex_enter(&nso->so_lock);
2030188658baSja97890 sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
203134793d0fSToomas Soome if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
2032188658baSja97890 MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
2033188658baSja97890 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;
20376e81d8daSja97890 } else if (nso->so_family == AF_UNIX) {
20386e81d8daSja97890 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;
2044188658baSja97890 } 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;
2049188658baSja97890 }
2050881776cfSGeorge Shepherd nso->so_state |= SS_ISCONNECTED;
2051881776cfSGeorge Shepherd mutex_exit(&nso->so_lock);
2052881776cfSGeorge Shepherd
2053188658baSja97890 freemsg(ack_mp);
2054188658baSja97890
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 e_disc_unl:
20687c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20697c478bd9Sstevel@tonic-gate goto disconnect_unlocked;
20707c478bd9Sstevel@tonic-gate
20717c478bd9Sstevel@tonic-gate disconnect_vp_unlocked:
2072de8c4a14SErik Nordmark (void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
20737c478bd9Sstevel@tonic-gate VN_RELE(nvp);
20747c478bd9Sstevel@tonic-gate disconnect_unlocked:
20757c478bd9Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, 0);
20767c478bd9Sstevel@tonic-gate return (error);
20777c478bd9Sstevel@tonic-gate
20787c478bd9Sstevel@tonic-gate disconnect_vp:
20797c478bd9Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
20807c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
20817c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
2082de8c4a14SErik Nordmark (void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
20837c478bd9Sstevel@tonic-gate VN_RELE(nvp);
20847c478bd9Sstevel@tonic-gate return (error);
20857c478bd9Sstevel@tonic-gate
20867c478bd9Sstevel@tonic-gate conn_bad: /* Note: SunOS 4/BSD unconditionally returns EINVAL here */
20877c478bd9Sstevel@tonic-gate error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
20887c478bd9Sstevel@tonic-gate ? EOPNOTSUPP : EINVAL;
20897c478bd9Sstevel@tonic-gate e_bad:
20907c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20917c478bd9Sstevel@tonic-gate return (error);
20927c478bd9Sstevel@tonic-gate }
20937c478bd9Sstevel@tonic-gate
20947c478bd9Sstevel@tonic-gate /*
20957c478bd9Sstevel@tonic-gate * connect a socket.
20967c478bd9Sstevel@tonic-gate *
20977c478bd9Sstevel@tonic-gate * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
20987c478bd9Sstevel@tonic-gate * unconnect (by specifying a null address).
20997c478bd9Sstevel@tonic-gate */
21007c478bd9Sstevel@tonic-gate int
sotpi_connect(struct sonode * so,struct sockaddr * name,socklen_t namelen,int fflag,int flags,struct cred * cr)21017c478bd9Sstevel@tonic-gate sotpi_connect(struct sonode *so,
21023e95bd4aSAnders Persson struct sockaddr *name,
21037c478bd9Sstevel@tonic-gate socklen_t namelen,
21047c478bd9Sstevel@tonic-gate int fflag,
21050f1702c5SYu Xiangning int flags,
21060f1702c5SYu Xiangning struct cred *cr)
21077c478bd9Sstevel@tonic-gate {
21087c478bd9Sstevel@tonic-gate struct T_conn_req conn_req;
21097c478bd9Sstevel@tonic-gate int error = 0;
21107c478bd9Sstevel@tonic-gate mblk_t *mp;
21117c478bd9Sstevel@tonic-gate void *src;
21127c478bd9Sstevel@tonic-gate socklen_t srclen;
21137c478bd9Sstevel@tonic-gate void *addr;
21147c478bd9Sstevel@tonic-gate socklen_t addrlen;
21157c478bd9Sstevel@tonic-gate boolean_t need_unlock;
21160f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
21177c478bd9Sstevel@tonic-gate
21187c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
2119903a11ebSrh87107 (void *)so, (void *)name, namelen, fflag, flags,
21207c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
21217c478bd9Sstevel@tonic-gate
21227c478bd9Sstevel@tonic-gate /*
21237c478bd9Sstevel@tonic-gate * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
21247c478bd9Sstevel@tonic-gate * avoid sleeping for memory with SOLOCKED held.
21250f1702c5SYu Xiangning * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen
21267c478bd9Sstevel@tonic-gate * + sizeof (struct T_opthdr).
21277c478bd9Sstevel@tonic-gate * (the AF_UNIX so_ux_addr_xlate() does not make the address
21280f1702c5SYu Xiangning * exceed sti_faddr_maxlen).
21297c478bd9Sstevel@tonic-gate */
21307c478bd9Sstevel@tonic-gate mp = soallocproto(sizeof (struct T_conn_req) +
2131de8c4a14SErik Nordmark 2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR,
2132de8c4a14SErik Nordmark cr);
21337c478bd9Sstevel@tonic-gate if (mp == NULL) {
21347c478bd9Sstevel@tonic-gate /*
21357c478bd9Sstevel@tonic-gate * Connect can not fail with ENOBUFS. A signal was
21367c478bd9Sstevel@tonic-gate * caught so return EINTR.
21377c478bd9Sstevel@tonic-gate */
21387c478bd9Sstevel@tonic-gate error = EINTR;
21397c478bd9Sstevel@tonic-gate eprintsoline(so, error);
21407c478bd9Sstevel@tonic-gate return (error);
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate
21437c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
21447c478bd9Sstevel@tonic-gate /*
2145ba3431dfSjprakash * Make sure there is a preallocated T_unbind_req message
2146ba3431dfSjprakash * before any binding. This message is allocated when the
2147ba3431dfSjprakash * socket is created. Since another thread can consume
2148ba3431dfSjprakash * so_unbind_mp by the time we return from so_lock_single(),
2149ba3431dfSjprakash * we should check the availability of so_unbind_mp after
2150ba3431dfSjprakash * we return from so_lock_single().
21517c478bd9Sstevel@tonic-gate */
2152ba3431dfSjprakash
2153ba3431dfSjprakash so_lock_single(so); /* Set SOLOCKED */
2154ba3431dfSjprakash need_unlock = B_TRUE;
2155ba3431dfSjprakash
21560f1702c5SYu Xiangning if (sti->sti_unbind_mp == NULL) {
21577c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
21587c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
21590f1702c5SYu Xiangning sti->sti_unbind_mp =
2160de8c4a14SErik Nordmark soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR, cr);
21610f1702c5SYu Xiangning if (sti->sti_unbind_mp == NULL) {
21627c478bd9Sstevel@tonic-gate error = EINTR;
21637c478bd9Sstevel@tonic-gate goto done;
21647c478bd9Sstevel@tonic-gate }
21657c478bd9Sstevel@tonic-gate }
21667c478bd9Sstevel@tonic-gate
21677c478bd9Sstevel@tonic-gate /*
21687c478bd9Sstevel@tonic-gate * Can't have done a listen before connecting.
21697c478bd9Sstevel@tonic-gate */
21707c478bd9Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) {
21717c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
21727c478bd9Sstevel@tonic-gate goto done;
21737c478bd9Sstevel@tonic-gate }
21747c478bd9Sstevel@tonic-gate
21757c478bd9Sstevel@tonic-gate /*
21767c478bd9Sstevel@tonic-gate * Must be bound with the transport
21777c478bd9Sstevel@tonic-gate */
21787c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) {
21797c478bd9Sstevel@tonic-gate if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
21807c478bd9Sstevel@tonic-gate /*CONSTCOND*/
21817c478bd9Sstevel@tonic-gate so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
21827c478bd9Sstevel@tonic-gate /*
21837c478bd9Sstevel@tonic-gate * Optimization for AF_INET{,6} transports
21847c478bd9Sstevel@tonic-gate * that can handle a T_CONN_REQ without being bound.
21857c478bd9Sstevel@tonic-gate */
21867c478bd9Sstevel@tonic-gate so_automatic_bind(so);
21877c478bd9Sstevel@tonic-gate } else {
21887c478bd9Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0,
21890f1702c5SYu Xiangning _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
21907c478bd9Sstevel@tonic-gate if (error)
21917c478bd9Sstevel@tonic-gate goto done;
21927c478bd9Sstevel@tonic-gate }
21937c478bd9Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND);
21947c478bd9Sstevel@tonic-gate flags |= _SOCONNECT_DID_BIND;
21957c478bd9Sstevel@tonic-gate }
21967c478bd9Sstevel@tonic-gate
21977c478bd9Sstevel@tonic-gate /*
21987c478bd9Sstevel@tonic-gate * Handle a connect to a name parameter of type AF_UNSPEC like a
21997c478bd9Sstevel@tonic-gate * connect to a null address. This is the portable method to
22007c478bd9Sstevel@tonic-gate * unconnect a socket.
22017c478bd9Sstevel@tonic-gate */
22027c478bd9Sstevel@tonic-gate if ((namelen >= sizeof (sa_family_t)) &&
22037c478bd9Sstevel@tonic-gate (name->sa_family == AF_UNSPEC)) {
22047c478bd9Sstevel@tonic-gate name = NULL;
22057c478bd9Sstevel@tonic-gate namelen = 0;
22067c478bd9Sstevel@tonic-gate }
22077c478bd9Sstevel@tonic-gate
22087c478bd9Sstevel@tonic-gate /*
22097c478bd9Sstevel@tonic-gate * Check that we are not already connected.
22107c478bd9Sstevel@tonic-gate * A connection-oriented socket cannot be reconnected.
22117c478bd9Sstevel@tonic-gate * A connected connection-less socket can be
22127c478bd9Sstevel@tonic-gate * - connected to a different address by a subsequent connect
22137c478bd9Sstevel@tonic-gate * - "unconnected" by a connect to the NULL address
22147c478bd9Sstevel@tonic-gate */
22157c478bd9Sstevel@tonic-gate if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
22167c478bd9Sstevel@tonic-gate ASSERT(!(flags & _SOCONNECT_DID_BIND));
22177c478bd9Sstevel@tonic-gate if (so->so_mode & SM_CONNREQUIRED) {
22187c478bd9Sstevel@tonic-gate /* Connection-oriented socket */
22197c478bd9Sstevel@tonic-gate error = so->so_state & SS_ISCONNECTED ?
22207c478bd9Sstevel@tonic-gate EISCONN : EALREADY;
22217c478bd9Sstevel@tonic-gate goto done;
22227c478bd9Sstevel@tonic-gate }
22237c478bd9Sstevel@tonic-gate /* Connection-less socket */
22247c478bd9Sstevel@tonic-gate if (name == NULL) {
22257c478bd9Sstevel@tonic-gate /*
22267c478bd9Sstevel@tonic-gate * Remove the connected state and clear SO_DGRAM_ERRIND
22277c478bd9Sstevel@tonic-gate * since it was set when the socket was connected.
22287c478bd9Sstevel@tonic-gate * If this is UDP also send down a T_DISCON_REQ.
22297c478bd9Sstevel@tonic-gate */
22307c478bd9Sstevel@tonic-gate int val;
22317c478bd9Sstevel@tonic-gate
22327c478bd9Sstevel@tonic-gate if ((so->so_family == AF_INET ||
22337c478bd9Sstevel@tonic-gate so->so_family == AF_INET6) &&
22347c478bd9Sstevel@tonic-gate (so->so_type == SOCK_DGRAM ||
22357c478bd9Sstevel@tonic-gate so->so_type == SOCK_RAW) &&
22367c478bd9Sstevel@tonic-gate /*CONSTCOND*/
22377c478bd9Sstevel@tonic-gate !soconnect_tpi_udp) {
22387c478bd9Sstevel@tonic-gate /* XXX What about implicitly unbinding here? */
22397c478bd9Sstevel@tonic-gate error = sodisconnect(so, -1,
22407c478bd9Sstevel@tonic-gate _SODISCONNECT_LOCK_HELD);
22417c478bd9Sstevel@tonic-gate } else {
22427c478bd9Sstevel@tonic-gate so->so_state &=
22430f1702c5SYu Xiangning ~(SS_ISCONNECTED | SS_ISCONNECTING);
22440f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
22450f1702c5SYu Xiangning sti->sti_faddr_len = 0;
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate
22480f1702c5SYu Xiangning /* Remove SOLOCKED since setsockopt will grab it */
22497c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
22507c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
22517c478bd9Sstevel@tonic-gate
22527c478bd9Sstevel@tonic-gate val = 0;
22530f1702c5SYu Xiangning (void) sotpi_setsockopt(so, SOL_SOCKET,
22540f1702c5SYu Xiangning SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val),
22550f1702c5SYu Xiangning cr);
22567c478bd9Sstevel@tonic-gate
22577c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
22587c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
22597c478bd9Sstevel@tonic-gate goto done;
22607c478bd9Sstevel@tonic-gate }
22617c478bd9Sstevel@tonic-gate }
22627c478bd9Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND);
22637c478bd9Sstevel@tonic-gate
22647c478bd9Sstevel@tonic-gate if (name == NULL || namelen == 0) {
22657c478bd9Sstevel@tonic-gate error = EINVAL;
22667c478bd9Sstevel@tonic-gate goto done;
22677c478bd9Sstevel@tonic-gate }
22687c478bd9Sstevel@tonic-gate /*
22690f1702c5SYu Xiangning * Mark the socket if sti_faddr_sa represents the transport level
22707c478bd9Sstevel@tonic-gate * address.
22717c478bd9Sstevel@tonic-gate */
22727c478bd9Sstevel@tonic-gate if (flags & _SOCONNECT_NOXLATE) {
22737c478bd9Sstevel@tonic-gate struct sockaddr_ux *soaddr_ux;
22747c478bd9Sstevel@tonic-gate
22757c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX);
22767c478bd9Sstevel@tonic-gate if (namelen != sizeof (struct sockaddr_ux)) {
22777c478bd9Sstevel@tonic-gate error = EINVAL;
22787c478bd9Sstevel@tonic-gate goto done;
22797c478bd9Sstevel@tonic-gate }
22807c478bd9Sstevel@tonic-gate soaddr_ux = (struct sockaddr_ux *)name;
22817c478bd9Sstevel@tonic-gate name = (struct sockaddr *)&soaddr_ux->sou_addr;
22827c478bd9Sstevel@tonic-gate namelen = sizeof (soaddr_ux->sou_addr);
22830f1702c5SYu Xiangning sti->sti_faddr_noxlate = 1;
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate
22867c478bd9Sstevel@tonic-gate /*
22877c478bd9Sstevel@tonic-gate * Length and family checks.
22887c478bd9Sstevel@tonic-gate */
22897c478bd9Sstevel@tonic-gate error = so_addr_verify(so, name, namelen);
22907c478bd9Sstevel@tonic-gate if (error)
22917c478bd9Sstevel@tonic-gate goto bad;
22927c478bd9Sstevel@tonic-gate
22937c478bd9Sstevel@tonic-gate /*
22947c478bd9Sstevel@tonic-gate * Save foreign address. Needed for AF_UNIX as well as
22957c478bd9Sstevel@tonic-gate * transport providers that do not support TI_GETPEERNAME.
22967c478bd9Sstevel@tonic-gate * Also used for cached foreign address for TCP and UDP.
22977c478bd9Sstevel@tonic-gate */
22980f1702c5SYu Xiangning if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) {
22997c478bd9Sstevel@tonic-gate error = EINVAL;
23007c478bd9Sstevel@tonic-gate goto done;
23017c478bd9Sstevel@tonic-gate }
23020f1702c5SYu Xiangning sti->sti_faddr_len = (socklen_t)namelen;
23030f1702c5SYu Xiangning ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
23040f1702c5SYu Xiangning bcopy(name, sti->sti_faddr_sa, namelen);
23050f1702c5SYu Xiangning sti->sti_faddr_valid = 1;
23067c478bd9Sstevel@tonic-gate
23077c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
23080f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
23097c478bd9Sstevel@tonic-gate /*
2310b521c41bSGordon Ross * sti_faddr is a transport-level address, so
2311b521c41bSGordon Ross * don't pass it as an option. Do save it in
2312b521c41bSGordon Ross * sti_ux_faddr, used for connected DG send.
23137c478bd9Sstevel@tonic-gate */
23147c478bd9Sstevel@tonic-gate src = NULL;
23157c478bd9Sstevel@tonic-gate srclen = 0;
2316b521c41bSGordon Ross addr = sti->sti_faddr_sa;
2317b521c41bSGordon Ross addrlen = (t_uscalar_t)sti->sti_faddr_len;
2318b521c41bSGordon Ross bcopy(addr, &sti->sti_ux_faddr,
2319b521c41bSGordon Ross sizeof (sti->sti_ux_faddr));
23207c478bd9Sstevel@tonic-gate } else {
23217c478bd9Sstevel@tonic-gate /*
23227c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
23237c478bd9Sstevel@tonic-gate * and translate the remote address.
23240f1702c5SYu Xiangning * Holding so_lock thus sti_laddr_sa can not change.
23257c478bd9Sstevel@tonic-gate */
23260f1702c5SYu Xiangning src = sti->sti_laddr_sa;
23270f1702c5SYu Xiangning srclen = (t_uscalar_t)sti->sti_laddr_len;
23287c478bd9Sstevel@tonic-gate dprintso(so, 1,
23297c478bd9Sstevel@tonic-gate ("sotpi_connect UNIX: srclen %d, src %p\n",
23307c478bd9Sstevel@tonic-gate srclen, src));
2331f012ee0cSGordon Ross /*
2332f012ee0cSGordon Ross * Translate the destination address into our
2333f012ee0cSGordon Ross * internal form, and save it in sti_ux_faddr.
2334f012ee0cSGordon Ross * After this call, addr==&sti->sti_ux_taddr,
2335f012ee0cSGordon Ross * and we copy that to sti->sti_ux_faddr so
2336f012ee0cSGordon Ross * we save the connected peer address.
2337f012ee0cSGordon Ross */
23387c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so,
23390f1702c5SYu Xiangning sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len,
23407c478bd9Sstevel@tonic-gate (flags & _SOCONNECT_XPG4_2),
23417c478bd9Sstevel@tonic-gate &addr, &addrlen);
23427c478bd9Sstevel@tonic-gate if (error)
23437c478bd9Sstevel@tonic-gate goto bad;
2344f012ee0cSGordon Ross bcopy(&sti->sti_ux_taddr, &sti->sti_ux_faddr,
2345f012ee0cSGordon Ross sizeof (sti->sti_ux_faddr));
23467c478bd9Sstevel@tonic-gate }
23477c478bd9Sstevel@tonic-gate } else {
23480f1702c5SYu Xiangning addr = sti->sti_faddr_sa;
23490f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_len;
23507c478bd9Sstevel@tonic-gate src = NULL;
23517c478bd9Sstevel@tonic-gate srclen = 0;
23527c478bd9Sstevel@tonic-gate }
23537c478bd9Sstevel@tonic-gate /*
23547c478bd9Sstevel@tonic-gate * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
23557c478bd9Sstevel@tonic-gate * option which asks the transport provider to send T_UDERR_IND
23567c478bd9Sstevel@tonic-gate * messages. These T_UDERR_IND messages are used to return connected
23577c478bd9Sstevel@tonic-gate * style errors (e.g. ECONNRESET) for connected datagram sockets.
23587c478bd9Sstevel@tonic-gate *
23597c478bd9Sstevel@tonic-gate * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
23607c478bd9Sstevel@tonic-gate * we send down a T_CONN_REQ. This is needed to let the
23617c478bd9Sstevel@tonic-gate * transport assign a local address that is consistent with
23627c478bd9Sstevel@tonic-gate * the remote address. Applications depend on a getsockname()
23637c478bd9Sstevel@tonic-gate * after a connect() to retrieve the "source" IP address for
23647c478bd9Sstevel@tonic-gate * the connected socket. Invalidate the cached local address
23657c478bd9Sstevel@tonic-gate * to force getsockname() to enquire of the transport.
23667c478bd9Sstevel@tonic-gate */
23677c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_CONNREQUIRED)) {
23687c478bd9Sstevel@tonic-gate /*
23697c478bd9Sstevel@tonic-gate * Datagram socket.
23707c478bd9Sstevel@tonic-gate */
23717c478bd9Sstevel@tonic-gate int32_t val;
23727c478bd9Sstevel@tonic-gate
23737c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
23747c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
23757c478bd9Sstevel@tonic-gate
23767c478bd9Sstevel@tonic-gate val = 1;
23777c478bd9Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
23780f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
23797c478bd9Sstevel@tonic-gate
23807c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
23817c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
23827c478bd9Sstevel@tonic-gate if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
23837c478bd9Sstevel@tonic-gate (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
23847c478bd9Sstevel@tonic-gate soconnect_tpi_udp) {
23857c478bd9Sstevel@tonic-gate soisconnected(so);
23867c478bd9Sstevel@tonic-gate goto done;
23877c478bd9Sstevel@tonic-gate }
23887c478bd9Sstevel@tonic-gate /*
23897c478bd9Sstevel@tonic-gate * Send down T_CONN_REQ etc.
23907c478bd9Sstevel@tonic-gate * Clear fflag to avoid returning EWOULDBLOCK.
23917c478bd9Sstevel@tonic-gate */
23927c478bd9Sstevel@tonic-gate fflag = 0;
23937c478bd9Sstevel@tonic-gate ASSERT(so->so_family != AF_UNIX);
23940f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
23950f1702c5SYu Xiangning } else if (sti->sti_laddr_len != 0) {
23967c478bd9Sstevel@tonic-gate /*
23977c478bd9Sstevel@tonic-gate * If the local address or port was "any" then it may be
23987c478bd9Sstevel@tonic-gate * changed by the transport as a result of the
23997c478bd9Sstevel@tonic-gate * connect. Invalidate the cached version if we have one.
24007c478bd9Sstevel@tonic-gate */
24017c478bd9Sstevel@tonic-gate switch (so->so_family) {
24027c478bd9Sstevel@tonic-gate case AF_INET:
24030f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t));
24040f1702c5SYu Xiangning if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr ==
24057c478bd9Sstevel@tonic-gate INADDR_ANY ||
24060f1702c5SYu Xiangning ((sin_t *)sti->sti_laddr_sa)->sin_port == 0)
24070f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
24087c478bd9Sstevel@tonic-gate break;
24097c478bd9Sstevel@tonic-gate
24107c478bd9Sstevel@tonic-gate case AF_INET6:
24110f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len ==
24120f1702c5SYu Xiangning (socklen_t)sizeof (sin6_t));
24137c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED(
24140f1702c5SYu Xiangning &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) ||
24157c478bd9Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED_ANY(
24160f1702c5SYu Xiangning &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) ||
24170f1702c5SYu Xiangning ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0)
24180f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
24197c478bd9Sstevel@tonic-gate break;
24207c478bd9Sstevel@tonic-gate
24217c478bd9Sstevel@tonic-gate default:
24227c478bd9Sstevel@tonic-gate break;
24237c478bd9Sstevel@tonic-gate }
24247c478bd9Sstevel@tonic-gate }
24257c478bd9Sstevel@tonic-gate
24267c478bd9Sstevel@tonic-gate /*
24277c478bd9Sstevel@tonic-gate * Check for failure of an earlier call
24287c478bd9Sstevel@tonic-gate */
24297c478bd9Sstevel@tonic-gate if (so->so_error != 0)
24307c478bd9Sstevel@tonic-gate goto so_bad;
24317c478bd9Sstevel@tonic-gate
24327c478bd9Sstevel@tonic-gate /*
24337c478bd9Sstevel@tonic-gate * Send down T_CONN_REQ. Message was allocated above.
24347c478bd9Sstevel@tonic-gate */
24357c478bd9Sstevel@tonic-gate conn_req.PRIM_type = T_CONN_REQ;
24367c478bd9Sstevel@tonic-gate conn_req.DEST_length = addrlen;
24377c478bd9Sstevel@tonic-gate conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
24387c478bd9Sstevel@tonic-gate if (srclen == 0) {
24397c478bd9Sstevel@tonic-gate conn_req.OPT_length = 0;
24407c478bd9Sstevel@tonic-gate conn_req.OPT_offset = 0;
24417c478bd9Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req));
24427c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
24437c478bd9Sstevel@tonic-gate } else {
24447c478bd9Sstevel@tonic-gate /*
24457c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
24467c478bd9Sstevel@tonic-gate * address option.
24477c478bd9Sstevel@tonic-gate */
24487c478bd9Sstevel@tonic-gate struct T_opthdr toh;
24497c478bd9Sstevel@tonic-gate
24507c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
24517c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
24527c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
24537c478bd9Sstevel@tonic-gate toh.status = 0;
24547c478bd9Sstevel@tonic-gate conn_req.OPT_length =
24557c478bd9Sstevel@tonic-gate (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
24567c478bd9Sstevel@tonic-gate conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
24577c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
24587c478bd9Sstevel@tonic-gate
24597c478bd9Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req));
24607c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
24617c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
24627c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
24637c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
24647c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
24657c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate /*
24687c478bd9Sstevel@tonic-gate * Set SS_ISCONNECTING before sending down the T_CONN_REQ
24697c478bd9Sstevel@tonic-gate * in order to have the right state when the T_CONN_CON shows up.
24707c478bd9Sstevel@tonic-gate */
24717c478bd9Sstevel@tonic-gate soisconnecting(so);
24727c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
24737c478bd9Sstevel@tonic-gate
2474005d3febSMarek Pospisil if (AU_AUDITING())
24757c478bd9Sstevel@tonic-gate audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
24767c478bd9Sstevel@tonic-gate
24777c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
24787c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
24797c478bd9Sstevel@tonic-gate mp = NULL;
24807c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
24817c478bd9Sstevel@tonic-gate if (error != 0)
24827c478bd9Sstevel@tonic-gate goto bad;
24837c478bd9Sstevel@tonic-gate
24847c478bd9Sstevel@tonic-gate if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
24857c478bd9Sstevel@tonic-gate goto bad;
24867c478bd9Sstevel@tonic-gate
24877c478bd9Sstevel@tonic-gate /* Allow other threads to access the socket */
24887c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
24897c478bd9Sstevel@tonic-gate need_unlock = B_FALSE;
24907c478bd9Sstevel@tonic-gate
24917c478bd9Sstevel@tonic-gate /*
24927c478bd9Sstevel@tonic-gate * Wait until we get a T_CONN_CON or an error
24937c478bd9Sstevel@tonic-gate */
24947c478bd9Sstevel@tonic-gate if ((error = sowaitconnected(so, fflag, 0)) != 0) {
24957c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
24967c478bd9Sstevel@tonic-gate need_unlock = B_TRUE;
24977c478bd9Sstevel@tonic-gate }
24987c478bd9Sstevel@tonic-gate
24997c478bd9Sstevel@tonic-gate done:
25007c478bd9Sstevel@tonic-gate freemsg(mp);
25017c478bd9Sstevel@tonic-gate switch (error) {
25027c478bd9Sstevel@tonic-gate case EINPROGRESS:
25037c478bd9Sstevel@tonic-gate case EALREADY:
25047c478bd9Sstevel@tonic-gate case EISCONN:
25057c478bd9Sstevel@tonic-gate case EINTR:
25067c478bd9Sstevel@tonic-gate /* Non-fatal errors */
25070f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
25087c478bd9Sstevel@tonic-gate /* FALLTHRU */
25097c478bd9Sstevel@tonic-gate case 0:
25107c478bd9Sstevel@tonic-gate break;
25117c478bd9Sstevel@tonic-gate default:
25127c478bd9Sstevel@tonic-gate ASSERT(need_unlock);
25137c478bd9Sstevel@tonic-gate /*
25147c478bd9Sstevel@tonic-gate * Fatal errors: clear SS_ISCONNECTING in case it was set,
25157c478bd9Sstevel@tonic-gate * and invalidate local-address cache
25167c478bd9Sstevel@tonic-gate */
25170f1702c5SYu Xiangning so->so_state &= ~SS_ISCONNECTING;
25180f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
25197c478bd9Sstevel@tonic-gate /* A discon_ind might have already unbound us */
25207c478bd9Sstevel@tonic-gate if ((flags & _SOCONNECT_DID_BIND) &&
25217c478bd9Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) {
25227c478bd9Sstevel@tonic-gate int err;
25237c478bd9Sstevel@tonic-gate
25247c478bd9Sstevel@tonic-gate err = sotpi_unbind(so, 0);
25257c478bd9Sstevel@tonic-gate /* LINTED - statement has no conseq */
25267c478bd9Sstevel@tonic-gate if (err) {
25277c478bd9Sstevel@tonic-gate eprintsoline(so, err);
25287c478bd9Sstevel@tonic-gate }
25297c478bd9Sstevel@tonic-gate }
25307c478bd9Sstevel@tonic-gate break;
25317c478bd9Sstevel@tonic-gate }
25327c478bd9Sstevel@tonic-gate if (need_unlock)
25337c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
25347c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
25357c478bd9Sstevel@tonic-gate return (error);
25367c478bd9Sstevel@tonic-gate
25370f1702c5SYu Xiangning so_bad: error = sogeterr(so, B_TRUE);
25387c478bd9Sstevel@tonic-gate bad: eprintsoline(so, error);
25397c478bd9Sstevel@tonic-gate goto done;
25407c478bd9Sstevel@tonic-gate }
25417c478bd9Sstevel@tonic-gate
25420f1702c5SYu Xiangning /* ARGSUSED */
25437c478bd9Sstevel@tonic-gate int
sotpi_shutdown(struct sonode * so,int how,struct cred * cr)25440f1702c5SYu Xiangning sotpi_shutdown(struct sonode *so, int how, struct cred *cr)
25457c478bd9Sstevel@tonic-gate {
25467c478bd9Sstevel@tonic-gate struct T_ordrel_req ordrel_req;
25477c478bd9Sstevel@tonic-gate mblk_t *mp;
25487c478bd9Sstevel@tonic-gate uint_t old_state, state_change;
25497c478bd9Sstevel@tonic-gate int error = 0;
25500f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
25517c478bd9Sstevel@tonic-gate
25527c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
2553903a11ebSrh87107 (void *)so, how, pr_state(so->so_state, so->so_mode)));
25547c478bd9Sstevel@tonic-gate
25557c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
25567c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
25577c478bd9Sstevel@tonic-gate
25587c478bd9Sstevel@tonic-gate /*
25597c478bd9Sstevel@tonic-gate * SunOS 4.X has no check for datagram sockets.
25607c478bd9Sstevel@tonic-gate * 5.X checks that it is connected (ENOTCONN)
25617c478bd9Sstevel@tonic-gate * X/Open requires that we check the connected state.
25627c478bd9Sstevel@tonic-gate */
25637c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) {
25647c478bd9Sstevel@tonic-gate if (!xnet_skip_checks) {
25657c478bd9Sstevel@tonic-gate error = ENOTCONN;
25667c478bd9Sstevel@tonic-gate if (xnet_check_print) {
25677c478bd9Sstevel@tonic-gate printf("sockfs: X/Open shutdown check "
25687c478bd9Sstevel@tonic-gate "caused ENOTCONN\n");
25697c478bd9Sstevel@tonic-gate }
25707c478bd9Sstevel@tonic-gate }
25717c478bd9Sstevel@tonic-gate goto done;
25727c478bd9Sstevel@tonic-gate }
25737c478bd9Sstevel@tonic-gate /*
25747c478bd9Sstevel@tonic-gate * Record the current state and then perform any state changes.
25757c478bd9Sstevel@tonic-gate * Then use the difference between the old and new states to
25767c478bd9Sstevel@tonic-gate * determine which messages need to be sent.
25777c478bd9Sstevel@tonic-gate * This prevents e.g. duplicate T_ORDREL_REQ when there are
25787c478bd9Sstevel@tonic-gate * duplicate calls to shutdown().
25797c478bd9Sstevel@tonic-gate */
25807c478bd9Sstevel@tonic-gate old_state = so->so_state;
25817c478bd9Sstevel@tonic-gate
25827c478bd9Sstevel@tonic-gate switch (how) {
25837c478bd9Sstevel@tonic-gate case 0:
25847c478bd9Sstevel@tonic-gate socantrcvmore(so);
25857c478bd9Sstevel@tonic-gate break;
25867c478bd9Sstevel@tonic-gate case 1:
25877c478bd9Sstevel@tonic-gate socantsendmore(so);
25887c478bd9Sstevel@tonic-gate break;
25897c478bd9Sstevel@tonic-gate case 2:
25907c478bd9Sstevel@tonic-gate socantsendmore(so);
25917c478bd9Sstevel@tonic-gate socantrcvmore(so);
25927c478bd9Sstevel@tonic-gate break;
25937c478bd9Sstevel@tonic-gate default:
25947c478bd9Sstevel@tonic-gate error = EINVAL;
25957c478bd9Sstevel@tonic-gate goto done;
25967c478bd9Sstevel@tonic-gate }
25977c478bd9Sstevel@tonic-gate
25987c478bd9Sstevel@tonic-gate /*
25997c478bd9Sstevel@tonic-gate * Assumes that the SS_CANT* flags are never cleared in the above code.
26007c478bd9Sstevel@tonic-gate */
26017c478bd9Sstevel@tonic-gate state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
26027c478bd9Sstevel@tonic-gate (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
26037c478bd9Sstevel@tonic-gate ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
26047c478bd9Sstevel@tonic-gate
26057c478bd9Sstevel@tonic-gate switch (state_change) {
26067c478bd9Sstevel@tonic-gate case 0:
26077c478bd9Sstevel@tonic-gate dprintso(so, 1,
26087c478bd9Sstevel@tonic-gate ("sotpi_shutdown: nothing to send in state 0x%x\n",
26097c478bd9Sstevel@tonic-gate so->so_state));
26107c478bd9Sstevel@tonic-gate goto done;
26117c478bd9Sstevel@tonic-gate
26127c478bd9Sstevel@tonic-gate case SS_CANTRCVMORE:
26137c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26147c478bd9Sstevel@tonic-gate strseteof(SOTOV(so), 1);
26157c478bd9Sstevel@tonic-gate /*
26167c478bd9Sstevel@tonic-gate * strseteof takes care of read side wakeups,
26177c478bd9Sstevel@tonic-gate * pollwakeups, and signals.
26187c478bd9Sstevel@tonic-gate */
26197c478bd9Sstevel@tonic-gate /*
26207c478bd9Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems
26217c478bd9Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
26227c478bd9Sstevel@tonic-gate */
26237c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26247c478bd9Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */
26257c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26267c478bd9Sstevel@tonic-gate
26277c478bd9Sstevel@tonic-gate /* Flush read side queue */
26287c478bd9Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL);
26297c478bd9Sstevel@tonic-gate
26307c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26317c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
26327c478bd9Sstevel@tonic-gate break;
26337c478bd9Sstevel@tonic-gate
26347c478bd9Sstevel@tonic-gate case SS_CANTSENDMORE:
26357c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26367c478bd9Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
26377c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26387c478bd9Sstevel@tonic-gate break;
26397c478bd9Sstevel@tonic-gate
26407c478bd9Sstevel@tonic-gate case SS_CANTSENDMORE|SS_CANTRCVMORE:
26417c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26427c478bd9Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
26437c478bd9Sstevel@tonic-gate strseteof(SOTOV(so), 1);
26447c478bd9Sstevel@tonic-gate /*
26457c478bd9Sstevel@tonic-gate * strseteof takes care of read side wakeups,
26467c478bd9Sstevel@tonic-gate * pollwakeups, and signals.
26477c478bd9Sstevel@tonic-gate */
26487c478bd9Sstevel@tonic-gate /*
26497c478bd9Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems
26507c478bd9Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
26517c478bd9Sstevel@tonic-gate */
26527c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26537c478bd9Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */
26547c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26557c478bd9Sstevel@tonic-gate
26567c478bd9Sstevel@tonic-gate /* Flush read side queue */
26577c478bd9Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL);
26587c478bd9Sstevel@tonic-gate
26597c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26607c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
26617c478bd9Sstevel@tonic-gate break;
26627c478bd9Sstevel@tonic-gate }
26637c478bd9Sstevel@tonic-gate
26647c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
26657c478bd9Sstevel@tonic-gate
26667c478bd9Sstevel@tonic-gate /*
26677c478bd9Sstevel@tonic-gate * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
26687c478bd9Sstevel@tonic-gate * was set due to this call and the new state has both of them set:
26697c478bd9Sstevel@tonic-gate * Send the AF_UNIX close indication
26707c478bd9Sstevel@tonic-gate * For T_COTS send a discon_ind
26717c478bd9Sstevel@tonic-gate *
26727c478bd9Sstevel@tonic-gate * If cantsend was set due to this call:
26737c478bd9Sstevel@tonic-gate * For T_COTSORD send an ordrel_ind
26747c478bd9Sstevel@tonic-gate *
26757c478bd9Sstevel@tonic-gate * Note that for T_CLTS there is no message sent here.
26767c478bd9Sstevel@tonic-gate */
26777c478bd9Sstevel@tonic-gate if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
26787c478bd9Sstevel@tonic-gate (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
26797c478bd9Sstevel@tonic-gate /*
26807c478bd9Sstevel@tonic-gate * For SunOS 4.X compatibility we tell the other end
26817c478bd9Sstevel@tonic-gate * that we are unable to receive at this point.
26827c478bd9Sstevel@tonic-gate */
26830f1702c5SYu Xiangning if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS)
26847c478bd9Sstevel@tonic-gate so_unix_close(so);
26857c478bd9Sstevel@tonic-gate
26860f1702c5SYu Xiangning if (sti->sti_serv_type == T_COTS)
26877c478bd9Sstevel@tonic-gate error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
26887c478bd9Sstevel@tonic-gate }
26897c478bd9Sstevel@tonic-gate if ((state_change & SS_CANTSENDMORE) &&
26900f1702c5SYu Xiangning (sti->sti_serv_type == T_COTS_ORD)) {
26917c478bd9Sstevel@tonic-gate /* Send an orderly release */
26927c478bd9Sstevel@tonic-gate ordrel_req.PRIM_type = T_ORDREL_REQ;
26937c478bd9Sstevel@tonic-gate
26947c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26957c478bd9Sstevel@tonic-gate mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
2696de8c4a14SErik Nordmark 0, _ALLOC_SLEEP, cr);
26977c478bd9Sstevel@tonic-gate /*
26987c478bd9Sstevel@tonic-gate * Send down the T_ORDREL_REQ even if there is flow control.
26997c478bd9Sstevel@tonic-gate * This prevents shutdown from blocking.
27007c478bd9Sstevel@tonic-gate * Note that there is no T_OK_ACK for ordrel_req.
27017c478bd9Sstevel@tonic-gate */
27027c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
27037c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
27047c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
27057c478bd9Sstevel@tonic-gate if (error) {
27067c478bd9Sstevel@tonic-gate eprintsoline(so, error);
27077c478bd9Sstevel@tonic-gate goto done;
27087c478bd9Sstevel@tonic-gate }
27097c478bd9Sstevel@tonic-gate }
27107c478bd9Sstevel@tonic-gate
27117c478bd9Sstevel@tonic-gate done:
27127c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
27137c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
27147c478bd9Sstevel@tonic-gate return (error);
27157c478bd9Sstevel@tonic-gate }
27167c478bd9Sstevel@tonic-gate
27177c478bd9Sstevel@tonic-gate /*
27187c478bd9Sstevel@tonic-gate * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
27197c478bd9Sstevel@tonic-gate * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
27207c478bd9Sstevel@tonic-gate * that we have closed.
27217c478bd9Sstevel@tonic-gate * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
27227c478bd9Sstevel@tonic-gate * T_UNITDATA_REQ containing the same option.
27237c478bd9Sstevel@tonic-gate *
27247c478bd9Sstevel@tonic-gate * For SOCK_DGRAM half-connections (somebody connected to this end
27257c478bd9Sstevel@tonic-gate * but this end is not connect) we don't know where to send any
27267c478bd9Sstevel@tonic-gate * SO_UNIX_CLOSE.
27277c478bd9Sstevel@tonic-gate *
27287c478bd9Sstevel@tonic-gate * We have to ignore stream head errors just in case there has been
27297c478bd9Sstevel@tonic-gate * a shutdown(output).
27307c478bd9Sstevel@tonic-gate * Ignore any flow control to try to get the message more quickly to the peer.
27317c478bd9Sstevel@tonic-gate * While locally ignoring flow control solves the problem when there
27327c478bd9Sstevel@tonic-gate * is only the loopback transport on the stream it would not provide
27337c478bd9Sstevel@tonic-gate * the correct AF_UNIX socket semantics when one or more modules have
27347c478bd9Sstevel@tonic-gate * been pushed.
27357c478bd9Sstevel@tonic-gate */
27367c478bd9Sstevel@tonic-gate void
so_unix_close(struct sonode * so)27377c478bd9Sstevel@tonic-gate so_unix_close(struct sonode *so)
27387c478bd9Sstevel@tonic-gate {
27397c478bd9Sstevel@tonic-gate struct T_opthdr toh;
27407c478bd9Sstevel@tonic-gate mblk_t *mp;
27410f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
27427c478bd9Sstevel@tonic-gate
27437c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
27447c478bd9Sstevel@tonic-gate
27457c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX);
27467c478bd9Sstevel@tonic-gate
27477c478bd9Sstevel@tonic-gate if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
27487c478bd9Sstevel@tonic-gate (SS_ISCONNECTED|SS_ISBOUND))
27497c478bd9Sstevel@tonic-gate return;
27507c478bd9Sstevel@tonic-gate
27517c478bd9Sstevel@tonic-gate dprintso(so, 1, ("so_unix_close(%p) %s\n",
2752903a11ebSrh87107 (void *)so, pr_state(so->so_state, so->so_mode)));
27537c478bd9Sstevel@tonic-gate
27547c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
27557c478bd9Sstevel@tonic-gate toh.name = SO_UNIX_CLOSE;
27567c478bd9Sstevel@tonic-gate
27577c478bd9Sstevel@tonic-gate /* zero length + header */
27587c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
27597c478bd9Sstevel@tonic-gate toh.status = 0;
27607c478bd9Sstevel@tonic-gate
27617c478bd9Sstevel@tonic-gate if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
27627c478bd9Sstevel@tonic-gate struct T_optdata_req tdr;
27637c478bd9Sstevel@tonic-gate
27647c478bd9Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ;
27657c478bd9Sstevel@tonic-gate tdr.DATA_flag = 0;
27667c478bd9Sstevel@tonic-gate
27677c478bd9Sstevel@tonic-gate tdr.OPT_length = (t_scalar_t)sizeof (toh);
27687c478bd9Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
27697c478bd9Sstevel@tonic-gate
27707c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
27717c478bd9Sstevel@tonic-gate mp = soallocproto2(&tdr, sizeof (tdr),
2772de8c4a14SErik Nordmark &toh, sizeof (toh), 0, _ALLOC_SLEEP, CRED());
27737c478bd9Sstevel@tonic-gate } else {
27747c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
27757c478bd9Sstevel@tonic-gate void *addr;
27767c478bd9Sstevel@tonic-gate socklen_t addrlen;
27777c478bd9Sstevel@tonic-gate void *src;
27787c478bd9Sstevel@tonic-gate socklen_t srclen;
27797c478bd9Sstevel@tonic-gate struct T_opthdr toh2;
27807c478bd9Sstevel@tonic-gate t_scalar_t size;
27817c478bd9Sstevel@tonic-gate
27827c478bd9Sstevel@tonic-gate /*
2783f012ee0cSGordon Ross * We know this is an AF_UNIX connected DGRAM socket.
2784f012ee0cSGordon Ross * We therefore already have the destination address
2785f012ee0cSGordon Ross * in the internal form needed for this send. This is
2786f012ee0cSGordon Ross * similar to the sosend_dgram call later in this file
2787f012ee0cSGordon Ross * when there's no user-specified destination address.
27887c478bd9Sstevel@tonic-gate */
27890f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
27907c478bd9Sstevel@tonic-gate /*
27917c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
27927c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
27937c478bd9Sstevel@tonic-gate */
27940f1702c5SYu Xiangning addr = sti->sti_faddr_sa;
27950f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_len;
27967c478bd9Sstevel@tonic-gate src = NULL;
27977c478bd9Sstevel@tonic-gate srclen = 0;
27987c478bd9Sstevel@tonic-gate } else {
27997c478bd9Sstevel@tonic-gate /*
28007c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
28017c478bd9Sstevel@tonic-gate * and translate the remote address.
28020f1702c5SYu Xiangning * Holding so_lock thus sti_laddr_sa can not change.
28037c478bd9Sstevel@tonic-gate */
28040f1702c5SYu Xiangning src = sti->sti_laddr_sa;
28050f1702c5SYu Xiangning srclen = (socklen_t)sti->sti_laddr_len;
28067c478bd9Sstevel@tonic-gate dprintso(so, 1,
28077c478bd9Sstevel@tonic-gate ("so_ux_close: srclen %d, src %p\n",
28087c478bd9Sstevel@tonic-gate srclen, src));
2809f012ee0cSGordon Ross /*
2810f012ee0cSGordon Ross * Use the destination address saved in connect.
2811f012ee0cSGordon Ross */
2812f012ee0cSGordon Ross addr = &sti->sti_ux_faddr;
2813f012ee0cSGordon Ross addrlen = sizeof (sti->sti_ux_faddr);
28147c478bd9Sstevel@tonic-gate }
28157c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
28167c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
28177c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
28187c478bd9Sstevel@tonic-gate if (srclen == 0) {
28197c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)sizeof (toh);
28207c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
28217c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
28227c478bd9Sstevel@tonic-gate
28237c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
28247c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
28257c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
2826de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_SLEEP, CRED());
28277c478bd9Sstevel@tonic-gate mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
28287c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
28297c478bd9Sstevel@tonic-gate } else {
28307c478bd9Sstevel@tonic-gate /*
28317c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a
28327c478bd9Sstevel@tonic-gate * source address option.
28337c478bd9Sstevel@tonic-gate */
28347c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
28357c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen));
28367c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
28377c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
28387c478bd9Sstevel@tonic-gate
28397c478bd9Sstevel@tonic-gate toh2.level = SOL_SOCKET;
28407c478bd9Sstevel@tonic-gate toh2.name = SO_SRCADDR;
28417c478bd9Sstevel@tonic-gate toh2.len = (t_uscalar_t)(srclen +
28427c478bd9Sstevel@tonic-gate sizeof (struct T_opthdr));
28437c478bd9Sstevel@tonic-gate toh2.status = 0;
28447c478bd9Sstevel@tonic-gate
28457c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
28467c478bd9Sstevel@tonic-gate
28477c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
28487c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
2849de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_SLEEP, CRED());
28507c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
28517c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
28527c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh2, sizeof (toh2));
28537c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
28547c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
28557c478bd9Sstevel@tonic-gate }
28567c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
28577c478bd9Sstevel@tonic-gate }
28587c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
2859f012ee0cSGordon Ross (void) kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
28607c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
28617c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
28627c478bd9Sstevel@tonic-gate }
28637c478bd9Sstevel@tonic-gate
28647c478bd9Sstevel@tonic-gate /*
28657c478bd9Sstevel@tonic-gate * Called by sotpi_recvmsg when reading a non-zero amount of data.
28667c478bd9Sstevel@tonic-gate * In addition, the caller typically verifies that there is some
28677c478bd9Sstevel@tonic-gate * potential state to clear by checking
28687c478bd9Sstevel@tonic-gate * if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
28697c478bd9Sstevel@tonic-gate * before calling this routine.
28707c478bd9Sstevel@tonic-gate * Note that such a check can be made without holding so_lock since
28717c478bd9Sstevel@tonic-gate * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
28720f1702c5SYu Xiangning * decrements sti_oobsigcnt.
28737c478bd9Sstevel@tonic-gate *
28747c478bd9Sstevel@tonic-gate * When data is read *after* the point that all pending
28757c478bd9Sstevel@tonic-gate * oob data has been consumed the oob indication is cleared.
28767c478bd9Sstevel@tonic-gate *
28777c478bd9Sstevel@tonic-gate * This logic keeps select/poll returning POLLRDBAND and
28787c478bd9Sstevel@tonic-gate * SIOCATMARK returning true until we have read past
28797c478bd9Sstevel@tonic-gate * the mark.
28807c478bd9Sstevel@tonic-gate */
28817c478bd9Sstevel@tonic-gate static void
sorecv_update_oobstate(struct sonode * so)28827c478bd9Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so)
28837c478bd9Sstevel@tonic-gate {
28840f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
28850f1702c5SYu Xiangning
28867c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
28877c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
28887c478bd9Sstevel@tonic-gate dprintso(so, 1,
28897c478bd9Sstevel@tonic-gate ("sorecv_update_oobstate: counts %d/%d state %s\n",
28900f1702c5SYu Xiangning sti->sti_oobsigcnt,
28910f1702c5SYu Xiangning sti->sti_oobcnt, pr_state(so->so_state, so->so_mode)));
28920f1702c5SYu Xiangning if (sti->sti_oobsigcnt == 0) {
28937c478bd9Sstevel@tonic-gate /* No more pending oob indications */
28947c478bd9Sstevel@tonic-gate so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
28957c478bd9Sstevel@tonic-gate freemsg(so->so_oobmsg);
28967c478bd9Sstevel@tonic-gate so->so_oobmsg = NULL;
28977c478bd9Sstevel@tonic-gate }
28987c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
28997c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
29007c478bd9Sstevel@tonic-gate }
29017c478bd9Sstevel@tonic-gate
29027c478bd9Sstevel@tonic-gate /*
29037c478bd9Sstevel@tonic-gate * Receive the next message on the queue.
29047c478bd9Sstevel@tonic-gate * If msg_controllen is non-zero when called the caller is interested in
29057c478bd9Sstevel@tonic-gate * any received control info (options).
29067c478bd9Sstevel@tonic-gate * If msg_namelen is non-zero when called the caller is interested in
29077c478bd9Sstevel@tonic-gate * any received source address.
29087c478bd9Sstevel@tonic-gate * The routine returns with msg_control and msg_name pointing to
29097c478bd9Sstevel@tonic-gate * kmem_alloc'ed memory which the caller has to free.
29107c478bd9Sstevel@tonic-gate */
29110f1702c5SYu Xiangning /* ARGSUSED */
29127c478bd9Sstevel@tonic-gate int
sotpi_recvmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)29130f1702c5SYu Xiangning sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
29140f1702c5SYu Xiangning struct cred *cr)
29157c478bd9Sstevel@tonic-gate {
29167c478bd9Sstevel@tonic-gate union T_primitives *tpr;
29177c478bd9Sstevel@tonic-gate mblk_t *mp;
29187c478bd9Sstevel@tonic-gate uchar_t pri;
29197c478bd9Sstevel@tonic-gate int pflag, opflag;
29207c478bd9Sstevel@tonic-gate void *control;
29217c478bd9Sstevel@tonic-gate t_uscalar_t controllen;
29227c478bd9Sstevel@tonic-gate t_uscalar_t namelen;
29237c478bd9Sstevel@tonic-gate int so_state = so->so_state; /* Snapshot */
29247c478bd9Sstevel@tonic-gate ssize_t saved_resid;
29257c478bd9Sstevel@tonic-gate rval_t rval;
29267c478bd9Sstevel@tonic-gate int flags;
29277c478bd9Sstevel@tonic-gate clock_t timout;
292817169044Sbrutus int error = 0;
29290f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
29307c478bd9Sstevel@tonic-gate
29317c478bd9Sstevel@tonic-gate flags = msg->msg_flags;
29327c478bd9Sstevel@tonic-gate msg->msg_flags = 0;
29337c478bd9Sstevel@tonic-gate
29347c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
2935903a11ebSrh87107 (void *)so, (void *)msg, flags,
29367c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode), so->so_error));
29377c478bd9Sstevel@tonic-gate
29380f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
29390f1702c5SYu Xiangning so_update_attrs(so, SOACC);
29400f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
29410f1702c5SYu Xiangning return (strread(SOTOV(so), uiop, cr));
29420f1702c5SYu Xiangning }
29430f1702c5SYu Xiangning
29447c478bd9Sstevel@tonic-gate /*
29457c478bd9Sstevel@tonic-gate * If we are not connected because we have never been connected
29467c478bd9Sstevel@tonic-gate * we return ENOTCONN. If we have been connected (but are no longer
29477c478bd9Sstevel@tonic-gate * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
29487c478bd9Sstevel@tonic-gate * the EOF.
29497c478bd9Sstevel@tonic-gate *
29507c478bd9Sstevel@tonic-gate * An alternative would be to post an ENOTCONN error in stream head
29517c478bd9Sstevel@tonic-gate * (read+write) and clear it when we're connected. However, that error
29527c478bd9Sstevel@tonic-gate * would cause incorrect poll/select behavior!
29537c478bd9Sstevel@tonic-gate */
29547c478bd9Sstevel@tonic-gate if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
29557c478bd9Sstevel@tonic-gate (so->so_mode & SM_CONNREQUIRED)) {
29567c478bd9Sstevel@tonic-gate return (ENOTCONN);
29577c478bd9Sstevel@tonic-gate }
29587c478bd9Sstevel@tonic-gate
29597c478bd9Sstevel@tonic-gate /*
29607c478bd9Sstevel@tonic-gate * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
29617c478bd9Sstevel@tonic-gate * after checking that the read queue is empty) and returns zero.
29627c478bd9Sstevel@tonic-gate * This implementation will sleep (in kstrgetmsg) even if uio_resid
29637c478bd9Sstevel@tonic-gate * is zero.
29647c478bd9Sstevel@tonic-gate */
29657c478bd9Sstevel@tonic-gate
29667c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
29677c478bd9Sstevel@tonic-gate /* Check that the transport supports OOB */
29687c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_EXDATA))
29697c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
29700f1702c5SYu Xiangning so_update_attrs(so, SOACC);
29710f1702c5SYu Xiangning return (sorecvoob(so, msg, uiop, flags,
29720f1702c5SYu Xiangning (so->so_options & SO_OOBINLINE)));
29737c478bd9Sstevel@tonic-gate }
29747c478bd9Sstevel@tonic-gate
29750f1702c5SYu Xiangning so_update_attrs(so, SOACC);
29760f1702c5SYu Xiangning
29777c478bd9Sstevel@tonic-gate /*
29787c478bd9Sstevel@tonic-gate * Set msg_controllen and msg_namelen to zero here to make it
29797c478bd9Sstevel@tonic-gate * simpler in the cases that no control or name is returned.
29807c478bd9Sstevel@tonic-gate */
29817c478bd9Sstevel@tonic-gate controllen = msg->msg_controllen;
29827c478bd9Sstevel@tonic-gate namelen = msg->msg_namelen;
29837c478bd9Sstevel@tonic-gate msg->msg_controllen = 0;
29847c478bd9Sstevel@tonic-gate msg->msg_namelen = 0;
29857c478bd9Sstevel@tonic-gate
29867c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
29877c478bd9Sstevel@tonic-gate namelen, controllen));
29887c478bd9Sstevel@tonic-gate
29892c9e429eSbrutus mutex_enter(&so->so_lock);
29907c478bd9Sstevel@tonic-gate /*
29917c478bd9Sstevel@tonic-gate * Only one reader is allowed at any given time. This is needed
29927c478bd9Sstevel@tonic-gate * for T_EXDATA handling and, in the future, MSG_WAITALL.
29937c478bd9Sstevel@tonic-gate *
29947c478bd9Sstevel@tonic-gate * This is slightly different that BSD behavior in that it fails with
29957c478bd9Sstevel@tonic-gate * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
29967c478bd9Sstevel@tonic-gate * is single-threaded using sblock(), which is dropped while waiting
29977c478bd9Sstevel@tonic-gate * for data to appear. The difference shows up e.g. if one
29987c478bd9Sstevel@tonic-gate * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
29997c478bd9Sstevel@tonic-gate * does use nonblocking io and different threads are reading each
30007c478bd9Sstevel@tonic-gate * file descriptor. In BSD there would never be an EWOULDBLOCK error
30017c478bd9Sstevel@tonic-gate * in this case as long as the read queue doesn't get empty.
30027c478bd9Sstevel@tonic-gate * In this implementation the thread using nonblocking io can
30037c478bd9Sstevel@tonic-gate * get an EWOULDBLOCK error due to the blocking thread executing
30047c478bd9Sstevel@tonic-gate * e.g. in the uiomove in kstrgetmsg.
30057c478bd9Sstevel@tonic-gate * This difference is not believed to be significant.
30067c478bd9Sstevel@tonic-gate */
3007255daac4Sethindra /* Set SOREADLOCKED */
3008255daac4Sethindra error = so_lock_read_intr(so,
3009255daac4Sethindra uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
30107c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
30117c478bd9Sstevel@tonic-gate if (error)
30127c478bd9Sstevel@tonic-gate return (error);
30137c478bd9Sstevel@tonic-gate
30147c478bd9Sstevel@tonic-gate /*
30157c478bd9Sstevel@tonic-gate * Tell kstrgetmsg to not inspect the stream head errors until all
30167c478bd9Sstevel@tonic-gate * queued data has been consumed.
30177c478bd9Sstevel@tonic-gate * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
30187c478bd9Sstevel@tonic-gate * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
30197c478bd9Sstevel@tonic-gate *
30207c478bd9Sstevel@tonic-gate * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
30217c478bd9Sstevel@tonic-gate * to T_OPTDATA_IND that do not contain any user-visible control msg.
30227c478bd9Sstevel@tonic-gate * Note that MSG_WAITALL set with MSG_PEEK is a noop.
30237c478bd9Sstevel@tonic-gate */
30247c478bd9Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR;
30257c478bd9Sstevel@tonic-gate if (flags & MSG_PEEK) {
30267c478bd9Sstevel@tonic-gate pflag |= MSG_IPEEK;
30277c478bd9Sstevel@tonic-gate flags &= ~MSG_WAITALL;
30287c478bd9Sstevel@tonic-gate }
30297c478bd9Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC)
30307c478bd9Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL;
30317c478bd9Sstevel@tonic-gate
30327c478bd9Sstevel@tonic-gate if (flags & MSG_DONTWAIT)
30337c478bd9Sstevel@tonic-gate timout = 0;
3034412cc9e9SGordon Ross else if (so->so_rcvtimeo != 0)
3035412cc9e9SGordon Ross timout = TICK_TO_MSEC(so->so_rcvtimeo);
30367c478bd9Sstevel@tonic-gate else
30377c478bd9Sstevel@tonic-gate timout = -1;
30387c478bd9Sstevel@tonic-gate opflag = pflag;
30397c478bd9Sstevel@tonic-gate retry:
30407c478bd9Sstevel@tonic-gate saved_resid = uiop->uio_resid;
30417c478bd9Sstevel@tonic-gate pri = 0;
30427c478bd9Sstevel@tonic-gate mp = NULL;
30437c478bd9Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
30447c478bd9Sstevel@tonic-gate timout, &rval);
30450f1702c5SYu Xiangning if (error != 0) {
30460f1702c5SYu Xiangning /* kstrgetmsg returns ETIME when timeout expires */
30470f1702c5SYu Xiangning if (error == ETIME)
30487c478bd9Sstevel@tonic-gate error = EWOULDBLOCK;
304917169044Sbrutus goto out;
30507c478bd9Sstevel@tonic-gate }
30517c478bd9Sstevel@tonic-gate /*
30527c478bd9Sstevel@tonic-gate * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
30537c478bd9Sstevel@tonic-gate * For non-datagrams MOREDATA is used to set MSG_EOR.
30547c478bd9Sstevel@tonic-gate */
30557c478bd9Sstevel@tonic-gate ASSERT(!(rval.r_val1 & MORECTL));
30567c478bd9Sstevel@tonic-gate if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
30577c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_TRUNC;
30587c478bd9Sstevel@tonic-gate
30597c478bd9Sstevel@tonic-gate if (mp == NULL) {
30607c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
30617c478bd9Sstevel@tonic-gate /*
30627c478bd9Sstevel@tonic-gate * 4.3BSD and 4.4BSD clears the mark when peeking across it.
30637c478bd9Sstevel@tonic-gate * The draft Posix socket spec states that the mark should
30647c478bd9Sstevel@tonic-gate * not be cleared when peeking. We follow the latter.
30657c478bd9Sstevel@tonic-gate */
30667c478bd9Sstevel@tonic-gate if ((so->so_state &
30677c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
30687c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
30697c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
30707c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
30717c478bd9Sstevel@tonic-gate }
30727c478bd9Sstevel@tonic-gate
30737c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
30747c478bd9Sstevel@tonic-gate /* Set MSG_EOR based on MOREDATA */
30757c478bd9Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) {
30767c478bd9Sstevel@tonic-gate if (so->so_state & SS_SAVEDEOR) {
30777c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_EOR;
30787c478bd9Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR;
30797c478bd9Sstevel@tonic-gate }
30807c478bd9Sstevel@tonic-gate }
30817c478bd9Sstevel@tonic-gate /*
30827c478bd9Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the
30837c478bd9Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more.
30847c478bd9Sstevel@tonic-gate */
30857c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
30867c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
30877c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
30887c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
30897c478bd9Sstevel@tonic-gate goto retry;
30907c478bd9Sstevel@tonic-gate }
309117169044Sbrutus goto out_locked;
30927c478bd9Sstevel@tonic-gate }
30937c478bd9Sstevel@tonic-gate
30947c478bd9Sstevel@tonic-gate /* strsock_proto has already verified length and alignment */
30957c478bd9Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr;
30967c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
30977c478bd9Sstevel@tonic-gate
30987c478bd9Sstevel@tonic-gate switch (tpr->type) {
30997c478bd9Sstevel@tonic-gate case T_DATA_IND: {
31007c478bd9Sstevel@tonic-gate if ((so->so_state &
31017c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
31027c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
31037c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
31047c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
31057c478bd9Sstevel@tonic-gate }
31067c478bd9Sstevel@tonic-gate
31077c478bd9Sstevel@tonic-gate /*
31087c478bd9Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on
31097c478bd9Sstevel@tonic-gate * MORE_flag and MOREDATA.
31107c478bd9Sstevel@tonic-gate */
31117c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
31127c478bd9Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR;
31137c478bd9Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) {
31147c478bd9Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA))
31157c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_EOR;
31167c478bd9Sstevel@tonic-gate else
31177c478bd9Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR;
31187c478bd9Sstevel@tonic-gate }
31197c478bd9Sstevel@tonic-gate freemsg(mp);
31207c478bd9Sstevel@tonic-gate /*
31217c478bd9Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the
31227c478bd9Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more.
31237c478bd9Sstevel@tonic-gate */
31247c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
31257c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
31267c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
31277c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
31287c478bd9Sstevel@tonic-gate goto retry;
31297c478bd9Sstevel@tonic-gate }
313017169044Sbrutus goto out_locked;
31317c478bd9Sstevel@tonic-gate }
31327c478bd9Sstevel@tonic-gate case T_UNITDATA_IND: {
31337c478bd9Sstevel@tonic-gate void *addr;
31347c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
31357c478bd9Sstevel@tonic-gate void *abuf;
31367c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
31377c478bd9Sstevel@tonic-gate void *opt;
31387c478bd9Sstevel@tonic-gate
31397c478bd9Sstevel@tonic-gate if ((so->so_state &
31407c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
31417c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
31427c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
31437c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
31447c478bd9Sstevel@tonic-gate }
31457c478bd9Sstevel@tonic-gate
31467c478bd9Sstevel@tonic-gate if (namelen != 0) {
31477c478bd9Sstevel@tonic-gate /* Caller wants source address */
31487c478bd9Sstevel@tonic-gate addrlen = tpr->unitdata_ind.SRC_length;
31497c478bd9Sstevel@tonic-gate addr = sogetoff(mp,
31507c478bd9Sstevel@tonic-gate tpr->unitdata_ind.SRC_offset,
31517c478bd9Sstevel@tonic-gate addrlen, 1);
31527c478bd9Sstevel@tonic-gate if (addr == NULL) {
31537c478bd9Sstevel@tonic-gate freemsg(mp);
31547c478bd9Sstevel@tonic-gate error = EPROTO;
31557c478bd9Sstevel@tonic-gate eprintsoline(so, error);
315617169044Sbrutus goto out;
31577c478bd9Sstevel@tonic-gate }
31587c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
31597c478bd9Sstevel@tonic-gate /*
31607c478bd9Sstevel@tonic-gate * Can not use the transport level address.
31617c478bd9Sstevel@tonic-gate * If there is a SO_SRCADDR option carrying
31627c478bd9Sstevel@tonic-gate * the socket level address it will be
31637c478bd9Sstevel@tonic-gate * extracted below.
31647c478bd9Sstevel@tonic-gate */
31657c478bd9Sstevel@tonic-gate addr = NULL;
31667c478bd9Sstevel@tonic-gate addrlen = 0;
31677c478bd9Sstevel@tonic-gate }
31687c478bd9Sstevel@tonic-gate }
31697c478bd9Sstevel@tonic-gate optlen = tpr->unitdata_ind.OPT_length;
31707c478bd9Sstevel@tonic-gate if (optlen != 0) {
31717c478bd9Sstevel@tonic-gate t_uscalar_t ncontrollen;
31727c478bd9Sstevel@tonic-gate
31737c478bd9Sstevel@tonic-gate /*
31747c478bd9Sstevel@tonic-gate * Extract any source address option.
31757c478bd9Sstevel@tonic-gate * Determine how large cmsg buffer is needed.
31767c478bd9Sstevel@tonic-gate */
31777c478bd9Sstevel@tonic-gate opt = sogetoff(mp,
31787c478bd9Sstevel@tonic-gate tpr->unitdata_ind.OPT_offset,
31797c478bd9Sstevel@tonic-gate optlen, __TPI_ALIGN_SIZE);
31807c478bd9Sstevel@tonic-gate
31817c478bd9Sstevel@tonic-gate if (opt == NULL) {
31827c478bd9Sstevel@tonic-gate freemsg(mp);
31837c478bd9Sstevel@tonic-gate error = EPROTO;
31847c478bd9Sstevel@tonic-gate eprintsoline(so, error);
318517169044Sbrutus goto out;
31867c478bd9Sstevel@tonic-gate }
31877c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX)
31887c478bd9Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
31897c478bd9Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen,
31907c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2));
31917c478bd9Sstevel@tonic-gate if (controllen != 0)
31927c478bd9Sstevel@tonic-gate controllen = ncontrollen;
31937c478bd9Sstevel@tonic-gate else if (ncontrollen != 0)
31947c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC;
31957c478bd9Sstevel@tonic-gate } else {
31967c478bd9Sstevel@tonic-gate controllen = 0;
31977c478bd9Sstevel@tonic-gate }
31987c478bd9Sstevel@tonic-gate
31997c478bd9Sstevel@tonic-gate if (namelen != 0) {
32007c478bd9Sstevel@tonic-gate /*
32017c478bd9Sstevel@tonic-gate * Return address to caller.
32027c478bd9Sstevel@tonic-gate * Caller handles truncation if length
32037c478bd9Sstevel@tonic-gate * exceeds msg_namelen.
32047c478bd9Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by
32057c478bd9Sstevel@tonic-gate * the sender's copyin_name().
32067c478bd9Sstevel@tonic-gate */
32077c478bd9Sstevel@tonic-gate abuf = kmem_alloc(addrlen, KM_SLEEP);
32087c478bd9Sstevel@tonic-gate
32097c478bd9Sstevel@tonic-gate bcopy(addr, abuf, addrlen);
32107c478bd9Sstevel@tonic-gate msg->msg_name = abuf;
32117c478bd9Sstevel@tonic-gate msg->msg_namelen = addrlen;
32127c478bd9Sstevel@tonic-gate }
32137c478bd9Sstevel@tonic-gate
32147c478bd9Sstevel@tonic-gate if (controllen != 0) {
32157c478bd9Sstevel@tonic-gate /*
32167c478bd9Sstevel@tonic-gate * Return control msg to caller.
32177c478bd9Sstevel@tonic-gate * Caller handles truncation if length
32187c478bd9Sstevel@tonic-gate * exceeds msg_controllen.
32197c478bd9Sstevel@tonic-gate */
3220274af231Samehta control = kmem_zalloc(controllen, KM_SLEEP);
32217c478bd9Sstevel@tonic-gate
32227c478bd9Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen,
32237c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2),
32247c478bd9Sstevel@tonic-gate control, controllen);
32257c478bd9Sstevel@tonic-gate if (error) {
32267c478bd9Sstevel@tonic-gate freemsg(mp);
32277c478bd9Sstevel@tonic-gate if (msg->msg_namelen != 0)
32287c478bd9Sstevel@tonic-gate kmem_free(msg->msg_name,
32297c478bd9Sstevel@tonic-gate msg->msg_namelen);
32307c478bd9Sstevel@tonic-gate kmem_free(control, controllen);
32317c478bd9Sstevel@tonic-gate eprintsoline(so, error);
323217169044Sbrutus goto out;
32337c478bd9Sstevel@tonic-gate }
32347c478bd9Sstevel@tonic-gate msg->msg_control = control;
32357c478bd9Sstevel@tonic-gate msg->msg_controllen = controllen;
32367c478bd9Sstevel@tonic-gate }
32377c478bd9Sstevel@tonic-gate
32387c478bd9Sstevel@tonic-gate freemsg(mp);
323917169044Sbrutus goto out;
32407c478bd9Sstevel@tonic-gate }
32417c478bd9Sstevel@tonic-gate case T_OPTDATA_IND: {
32427c478bd9Sstevel@tonic-gate struct T_optdata_req *tdr;
32437c478bd9Sstevel@tonic-gate void *opt;
32447c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
32457c478bd9Sstevel@tonic-gate
32467c478bd9Sstevel@tonic-gate if ((so->so_state &
32477c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32487c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
32497c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
32507c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
32517c478bd9Sstevel@tonic-gate }
32527c478bd9Sstevel@tonic-gate
32537c478bd9Sstevel@tonic-gate tdr = (struct T_optdata_req *)mp->b_rptr;
32547c478bd9Sstevel@tonic-gate optlen = tdr->OPT_length;
32557c478bd9Sstevel@tonic-gate if (optlen != 0) {
32567c478bd9Sstevel@tonic-gate t_uscalar_t ncontrollen;
32577c478bd9Sstevel@tonic-gate /*
32587c478bd9Sstevel@tonic-gate * Determine how large cmsg buffer is needed.
32597c478bd9Sstevel@tonic-gate */
32607c478bd9Sstevel@tonic-gate opt = sogetoff(mp,
32617c478bd9Sstevel@tonic-gate tpr->optdata_ind.OPT_offset,
32627c478bd9Sstevel@tonic-gate optlen, __TPI_ALIGN_SIZE);
32637c478bd9Sstevel@tonic-gate
32647c478bd9Sstevel@tonic-gate if (opt == NULL) {
32657c478bd9Sstevel@tonic-gate freemsg(mp);
32667c478bd9Sstevel@tonic-gate error = EPROTO;
32677c478bd9Sstevel@tonic-gate eprintsoline(so, error);
326817169044Sbrutus goto out;
32697c478bd9Sstevel@tonic-gate }
32707c478bd9Sstevel@tonic-gate
32717c478bd9Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen,
32727c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2));
32737c478bd9Sstevel@tonic-gate if (controllen != 0)
32747c478bd9Sstevel@tonic-gate controllen = ncontrollen;
32757c478bd9Sstevel@tonic-gate else if (ncontrollen != 0)
32767c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC;
32777c478bd9Sstevel@tonic-gate } else {
32787c478bd9Sstevel@tonic-gate controllen = 0;
32797c478bd9Sstevel@tonic-gate }
32807c478bd9Sstevel@tonic-gate
32817c478bd9Sstevel@tonic-gate if (controllen != 0) {
32827c478bd9Sstevel@tonic-gate /*
32837c478bd9Sstevel@tonic-gate * Return control msg to caller.
32847c478bd9Sstevel@tonic-gate * Caller handles truncation if length
32857c478bd9Sstevel@tonic-gate * exceeds msg_controllen.
32867c478bd9Sstevel@tonic-gate */
3287274af231Samehta control = kmem_zalloc(controllen, KM_SLEEP);
32887c478bd9Sstevel@tonic-gate
32897c478bd9Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen,
32907c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2),
32917c478bd9Sstevel@tonic-gate control, controllen);
32927c478bd9Sstevel@tonic-gate if (error) {
32937c478bd9Sstevel@tonic-gate freemsg(mp);
32947c478bd9Sstevel@tonic-gate kmem_free(control, controllen);
32957c478bd9Sstevel@tonic-gate eprintsoline(so, error);
329617169044Sbrutus goto out;
32977c478bd9Sstevel@tonic-gate }
32987c478bd9Sstevel@tonic-gate msg->msg_control = control;
32997c478bd9Sstevel@tonic-gate msg->msg_controllen = controllen;
33007c478bd9Sstevel@tonic-gate }
33017c478bd9Sstevel@tonic-gate
33027c478bd9Sstevel@tonic-gate /*
33037c478bd9Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on
33047c478bd9Sstevel@tonic-gate * DATA_flag and MOREDATA.
33057c478bd9Sstevel@tonic-gate */
33067c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
33077c478bd9Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR;
33087c478bd9Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) {
33097c478bd9Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA))
33107c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_EOR;
33117c478bd9Sstevel@tonic-gate else
33127c478bd9Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR;
33137c478bd9Sstevel@tonic-gate }
33147c478bd9Sstevel@tonic-gate freemsg(mp);
33157c478bd9Sstevel@tonic-gate /*
33167c478bd9Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the
33177c478bd9Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more.
33187c478bd9Sstevel@tonic-gate * Not possible to wait if control info was received.
33197c478bd9Sstevel@tonic-gate */
33207c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
33217c478bd9Sstevel@tonic-gate controllen == 0 &&
33227c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
33237c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
33247c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
33257c478bd9Sstevel@tonic-gate goto retry;
33267c478bd9Sstevel@tonic-gate }
332717169044Sbrutus goto out_locked;
33287c478bd9Sstevel@tonic-gate }
33297c478bd9Sstevel@tonic-gate case T_EXDATA_IND: {
33307c478bd9Sstevel@tonic-gate dprintso(so, 1,
33317c478bd9Sstevel@tonic-gate ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
33327c478bd9Sstevel@tonic-gate "state %s\n",
33330f1702c5SYu Xiangning sti->sti_oobsigcnt, sti->sti_oobcnt,
33347c478bd9Sstevel@tonic-gate saved_resid - uiop->uio_resid,
33357c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
33367c478bd9Sstevel@tonic-gate /*
33377c478bd9Sstevel@tonic-gate * kstrgetmsg handles MSGMARK so there is nothing to
33387c478bd9Sstevel@tonic-gate * inspect in the T_EXDATA_IND.
33397c478bd9Sstevel@tonic-gate * strsock_proto makes the stream head queue the T_EXDATA_IND
33407c478bd9Sstevel@tonic-gate * as a separate message with no M_DATA component. Furthermore,
33417c478bd9Sstevel@tonic-gate * the stream head does not consolidate M_DATA messages onto
33427c478bd9Sstevel@tonic-gate * an MSGMARK'ed message ensuring that the T_EXDATA_IND
33437c478bd9Sstevel@tonic-gate * remains a message by itself. This is needed since MSGMARK
33447c478bd9Sstevel@tonic-gate * marks both the whole message as well as the last byte
33457c478bd9Sstevel@tonic-gate * of the message.
33467c478bd9Sstevel@tonic-gate */
33477c478bd9Sstevel@tonic-gate freemsg(mp);
33487c478bd9Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); /* No data */
33497c478bd9Sstevel@tonic-gate if (flags & MSG_PEEK) {
33507c478bd9Sstevel@tonic-gate /*
33517c478bd9Sstevel@tonic-gate * Even though we are peeking we consume the
33527c478bd9Sstevel@tonic-gate * T_EXDATA_IND thereby moving the mark information
33537c478bd9Sstevel@tonic-gate * to SS_RCVATMARK. Then the oob code below will
33547c478bd9Sstevel@tonic-gate * retry the peeking kstrgetmsg.
33557c478bd9Sstevel@tonic-gate * Note that the stream head read queue is
33567c478bd9Sstevel@tonic-gate * never flushed without holding SOREADLOCKED
33577c478bd9Sstevel@tonic-gate * thus the T_EXDATA_IND can not disappear
33587c478bd9Sstevel@tonic-gate * underneath us.
33597c478bd9Sstevel@tonic-gate */
33607c478bd9Sstevel@tonic-gate dprintso(so, 1,
33617c478bd9Sstevel@tonic-gate ("sotpi_recvmsg: consume EXDATA_IND "
33627c478bd9Sstevel@tonic-gate "counts %d/%d state %s\n",
33630f1702c5SYu Xiangning sti->sti_oobsigcnt,
33640f1702c5SYu Xiangning sti->sti_oobcnt,
33657c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
33667c478bd9Sstevel@tonic-gate
33677c478bd9Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR;
33687c478bd9Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC)
33697c478bd9Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL;
33707c478bd9Sstevel@tonic-gate
33717c478bd9Sstevel@tonic-gate pri = 0;
33727c478bd9Sstevel@tonic-gate mp = NULL;
33737c478bd9Sstevel@tonic-gate
33747c478bd9Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop,
33757c478bd9Sstevel@tonic-gate &pri, &pflag, (clock_t)-1, &rval);
33767c478bd9Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid);
33777c478bd9Sstevel@tonic-gate
33787c478bd9Sstevel@tonic-gate if (error) {
33797c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
33807c478bd9Sstevel@tonic-gate if (error != EWOULDBLOCK && error != EINTR) {
33817c478bd9Sstevel@tonic-gate eprintsoline(so, error);
33827c478bd9Sstevel@tonic-gate }
33837c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
338417169044Sbrutus goto out;
33857c478bd9Sstevel@tonic-gate }
33867c478bd9Sstevel@tonic-gate ASSERT(mp);
33877c478bd9Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr;
33887c478bd9Sstevel@tonic-gate ASSERT(tpr->type == T_EXDATA_IND);
33897c478bd9Sstevel@tonic-gate freemsg(mp);
33907c478bd9Sstevel@tonic-gate } /* end "if (flags & MSG_PEEK)" */
33917c478bd9Sstevel@tonic-gate
33927c478bd9Sstevel@tonic-gate /*
33937c478bd9Sstevel@tonic-gate * Decrement the number of queued and pending oob.
33947c478bd9Sstevel@tonic-gate *
33957c478bd9Sstevel@tonic-gate * SS_RCVATMARK is cleared when we read past a mark.
33967c478bd9Sstevel@tonic-gate * SS_HAVEOOBDATA is cleared when we've read past the
33977c478bd9Sstevel@tonic-gate * last mark.
33987c478bd9Sstevel@tonic-gate * SS_OOBPEND is cleared if we've read past the last
33997c478bd9Sstevel@tonic-gate * mark and no (new) SIGURG has been posted.
34007c478bd9Sstevel@tonic-gate */
34017c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
34027c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
34030f1702c5SYu Xiangning ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
34040f1702c5SYu Xiangning ASSERT(sti->sti_oobsigcnt > 0);
34050f1702c5SYu Xiangning sti->sti_oobsigcnt--;
34060f1702c5SYu Xiangning ASSERT(sti->sti_oobcnt > 0);
34070f1702c5SYu Xiangning sti->sti_oobcnt--;
34087c478bd9Sstevel@tonic-gate /*
34097c478bd9Sstevel@tonic-gate * Since the T_EXDATA_IND has been removed from the stream
34107c478bd9Sstevel@tonic-gate * head, but we have not read data past the mark,
34117c478bd9Sstevel@tonic-gate * sockfs needs to track that the socket is still at the mark.
34127c478bd9Sstevel@tonic-gate *
34137c478bd9Sstevel@tonic-gate * Since no data was received call kstrgetmsg again to wait
34147c478bd9Sstevel@tonic-gate * for data.
34157c478bd9Sstevel@tonic-gate */
34167c478bd9Sstevel@tonic-gate so->so_state |= SS_RCVATMARK;
34177c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
34187c478bd9Sstevel@tonic-gate dprintso(so, 1,
34197c478bd9Sstevel@tonic-gate ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n",
34200f1702c5SYu Xiangning sti->sti_oobsigcnt, sti->sti_oobcnt,
34217c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
34227c478bd9Sstevel@tonic-gate pflag = opflag;
34237c478bd9Sstevel@tonic-gate goto retry;
34247c478bd9Sstevel@tonic-gate }
34257c478bd9Sstevel@tonic-gate default:
34260f1702c5SYu Xiangning cmn_err(CE_CONT, "sotpi_recvmsg: so %p prim %d mp %p\n",
34270f1702c5SYu Xiangning (void *)so, tpr->type, (void *)mp);
34287c478bd9Sstevel@tonic-gate ASSERT(0);
34297c478bd9Sstevel@tonic-gate freemsg(mp);
34307c478bd9Sstevel@tonic-gate error = EPROTO;
34317c478bd9Sstevel@tonic-gate eprintsoline(so, error);
343217169044Sbrutus goto out;
34337c478bd9Sstevel@tonic-gate }
34347c478bd9Sstevel@tonic-gate /* NOTREACHED */
343517169044Sbrutus out:
34367c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
343717169044Sbrutus out_locked:
34387c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
34397c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
34407c478bd9Sstevel@tonic-gate return (error);
34417c478bd9Sstevel@tonic-gate }
34427c478bd9Sstevel@tonic-gate
34437c478bd9Sstevel@tonic-gate /*
34447c478bd9Sstevel@tonic-gate * Sending data with options on a datagram socket.
34457c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set.
3446f012ee0cSGordon Ross *
3447f012ee0cSGordon Ross * For AF_UNIX the destination address may be already in
3448f012ee0cSGordon Ross * internal form, as indicated by sti->sti_faddr_noxlate
3449f012ee0cSGordon Ross * or the MSG_SENDTO_NOXLATE flag. Otherwise we need to
3450f012ee0cSGordon Ross * translate the destination address to internal form.
3451f012ee0cSGordon Ross *
3452f012ee0cSGordon Ross * The source address is passed as an option. If passing
3453f012ee0cSGordon Ross * file descriptors, those are passed as file pointers in
3454f012ee0cSGordon Ross * another option.
34557c478bd9Sstevel@tonic-gate */
34567c478bd9Sstevel@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)3457ff550d0eSmasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3458ff550d0eSmasputra struct uio *uiop, void *control, t_uscalar_t controllen, int flags)
34597c478bd9Sstevel@tonic-gate {
34607c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
34617c478bd9Sstevel@tonic-gate mblk_t *mp;
34627c478bd9Sstevel@tonic-gate int error;
34637c478bd9Sstevel@tonic-gate void *addr;
34647c478bd9Sstevel@tonic-gate socklen_t addrlen;
34657c478bd9Sstevel@tonic-gate void *src;
34667c478bd9Sstevel@tonic-gate socklen_t srclen;
34677c478bd9Sstevel@tonic-gate ssize_t len;
34687c478bd9Sstevel@tonic-gate int size;
34697c478bd9Sstevel@tonic-gate struct T_opthdr toh;
34707c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf;
34717c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
34727c478bd9Sstevel@tonic-gate void *fds;
34737c478bd9Sstevel@tonic-gate int fdlen;
34740f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
34757c478bd9Sstevel@tonic-gate
34767c478bd9Sstevel@tonic-gate ASSERT(name && namelen);
34777c478bd9Sstevel@tonic-gate ASSERT(control && controllen);
34787c478bd9Sstevel@tonic-gate
34797c478bd9Sstevel@tonic-gate len = uiop->uio_resid;
34800f1702c5SYu Xiangning if (len > (ssize_t)sti->sti_tidu_size) {
34817c478bd9Sstevel@tonic-gate return (EMSGSIZE);
34827c478bd9Sstevel@tonic-gate }
34837c478bd9Sstevel@tonic-gate
3484f012ee0cSGordon Ross if (sti->sti_faddr_noxlate == 0 &&
3485f012ee0cSGordon Ross (flags & MSG_SENDTO_NOXLATE) == 0) {
34867c478bd9Sstevel@tonic-gate /*
34877c478bd9Sstevel@tonic-gate * Length and family checks.
3488f012ee0cSGordon Ross * Don't verify internal form.
34897c478bd9Sstevel@tonic-gate */
34907c478bd9Sstevel@tonic-gate error = so_addr_verify(so, name, namelen);
34917c478bd9Sstevel@tonic-gate if (error) {
34927c478bd9Sstevel@tonic-gate eprintsoline(so, error);
34937c478bd9Sstevel@tonic-gate return (error);
34947c478bd9Sstevel@tonic-gate }
3495f012ee0cSGordon Ross }
3496f012ee0cSGordon Ross
34977c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
34980f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
34997c478bd9Sstevel@tonic-gate /*
35007c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
35017c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
35027c478bd9Sstevel@tonic-gate */
35037c478bd9Sstevel@tonic-gate addr = name;
35047c478bd9Sstevel@tonic-gate addrlen = namelen;
35057c478bd9Sstevel@tonic-gate src = NULL;
35067c478bd9Sstevel@tonic-gate srclen = 0;
3507f012ee0cSGordon Ross } else if (flags & MSG_SENDTO_NOXLATE) {
3508f012ee0cSGordon Ross /*
3509f012ee0cSGordon Ross * Have an internal form dest. address.
3510f012ee0cSGordon Ross * Pass the source address as usual.
3511f012ee0cSGordon Ross */
3512f012ee0cSGordon Ross addr = name;
3513f012ee0cSGordon Ross addrlen = namelen;
3514f012ee0cSGordon Ross src = sti->sti_laddr_sa;
3515f012ee0cSGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
35167c478bd9Sstevel@tonic-gate } else {
35177c478bd9Sstevel@tonic-gate /*
35187c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
35197c478bd9Sstevel@tonic-gate * and translate the remote address.
35207c478bd9Sstevel@tonic-gate *
35210f1702c5SYu Xiangning * Note that this code does not prevent sti_laddr_sa
35227c478bd9Sstevel@tonic-gate * from changing while it is being used. Thus
35237c478bd9Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this
35247c478bd9Sstevel@tonic-gate * send the peer might see a partially new and a
35257c478bd9Sstevel@tonic-gate * partially old "from" address.
35267c478bd9Sstevel@tonic-gate */
35270f1702c5SYu Xiangning src = sti->sti_laddr_sa;
3528f012ee0cSGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
35297c478bd9Sstevel@tonic-gate dprintso(so, 1,
35307c478bd9Sstevel@tonic-gate ("sosend_dgramcmsg UNIX: srclen %d, src %p\n",
35317c478bd9Sstevel@tonic-gate srclen, src));
3532f012ee0cSGordon Ross /*
3533f012ee0cSGordon Ross * The sendmsg caller specified a destination
3534f012ee0cSGordon Ross * address, which we must translate into our
3535f012ee0cSGordon Ross * internal form. addr = &sti->sti_ux_taddr
3536f012ee0cSGordon Ross */
35377c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen,
35387c478bd9Sstevel@tonic-gate (flags & MSG_XPG4_2),
35397c478bd9Sstevel@tonic-gate &addr, &addrlen);
35407c478bd9Sstevel@tonic-gate if (error) {
35417c478bd9Sstevel@tonic-gate eprintsoline(so, error);
35427c478bd9Sstevel@tonic-gate return (error);
35437c478bd9Sstevel@tonic-gate }
35447c478bd9Sstevel@tonic-gate }
35457c478bd9Sstevel@tonic-gate } else {
35467c478bd9Sstevel@tonic-gate addr = name;
35477c478bd9Sstevel@tonic-gate addrlen = namelen;
35487c478bd9Sstevel@tonic-gate src = NULL;
35497c478bd9Sstevel@tonic-gate srclen = 0;
35507c478bd9Sstevel@tonic-gate }
35517c478bd9Sstevel@tonic-gate optlen = so_optlen(control, controllen,
35527c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2));
35537c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
35547c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
35557c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
35567c478bd9Sstevel@tonic-gate if (srclen != 0)
35577c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) +
35587c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen));
35597c478bd9Sstevel@tonic-gate else
35607c478bd9Sstevel@tonic-gate tudr.OPT_length = optlen;
35617c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
35627c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
35637c478bd9Sstevel@tonic-gate
35647c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
35657c478bd9Sstevel@tonic-gate
35667c478bd9Sstevel@tonic-gate /*
35677c478bd9Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set.
35687c478bd9Sstevel@tonic-gate */
35697c478bd9Sstevel@tonic-gate error = so_getfdopt(control, controllen,
35707c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2), &fds, &fdlen);
35717c478bd9Sstevel@tonic-gate if (error)
35727c478bd9Sstevel@tonic-gate return (error);
35737c478bd9Sstevel@tonic-gate if (fdlen != -1) {
35747c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING))
35757c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
35767c478bd9Sstevel@tonic-gate
35777c478bd9Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf);
35787c478bd9Sstevel@tonic-gate if (error)
35797c478bd9Sstevel@tonic-gate return (error);
3580d28d4716SJerry Jelinek
3581d28d4716SJerry Jelinek /*
3582d28d4716SJerry Jelinek * Pre-allocate enough additional space for lower level modules
3583d28d4716SJerry Jelinek * to append an option (e.g. see tl_unitdata). The following
3584d28d4716SJerry Jelinek * is enough extra space for the largest option we might append.
3585d28d4716SJerry Jelinek */
3586d28d4716SJerry Jelinek size += sizeof (struct T_opthdr) + ucredsize;
35877c478bd9Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf);
35887c478bd9Sstevel@tonic-gate } else {
3589de8c4a14SErik Nordmark mp = soallocproto(size, _ALLOC_INTR, CRED());
35907c478bd9Sstevel@tonic-gate if (mp == NULL) {
35917c478bd9Sstevel@tonic-gate /*
35927c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
35937c478bd9Sstevel@tonic-gate * Let send* return EINTR.
35947c478bd9Sstevel@tonic-gate */
35957c478bd9Sstevel@tonic-gate return (EINTR);
35967c478bd9Sstevel@tonic-gate }
3597bd118333Smeem }
35987c478bd9Sstevel@tonic-gate soappendmsg(mp, &tudr, sizeof (tudr));
35997c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
36007c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
36017c478bd9Sstevel@tonic-gate
36027c478bd9Sstevel@tonic-gate if (fdlen != -1) {
36037c478bd9Sstevel@tonic-gate ASSERT(fdbuf != NULL);
36047c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
36057c478bd9Sstevel@tonic-gate toh.name = SO_FILEP;
36067c478bd9Sstevel@tonic-gate toh.len = fdbuf->fd_size +
36077c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_opthdr);
36087c478bd9Sstevel@tonic-gate toh.status = 0;
36097c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
36107c478bd9Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size);
36117c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
36127c478bd9Sstevel@tonic-gate }
36137c478bd9Sstevel@tonic-gate if (srclen != 0) {
36147c478bd9Sstevel@tonic-gate /*
36157c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
36167c478bd9Sstevel@tonic-gate * address option.
36177c478bd9Sstevel@tonic-gate */
36187c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
36197c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
36207c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
36217c478bd9Sstevel@tonic-gate toh.status = 0;
36227c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
36237c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
36247c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
36257c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
36267c478bd9Sstevel@tonic-gate }
36277c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
36287c478bd9Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
3629d28d4716SJerry Jelinek /*
3630d28d4716SJerry Jelinek * Normally at most 3 bytes left in the message, but we might have
3631d28d4716SJerry Jelinek * allowed for extra space if we're passing fd's through.
3632d28d4716SJerry Jelinek */
36337c478bd9Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size);
36347c478bd9Sstevel@tonic-gate
36357c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3636005d3febSMarek Pospisil if (AU_AUDITING())
36377c478bd9Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
36387c478bd9Sstevel@tonic-gate
36397c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
36407c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
36417c478bd9Sstevel@tonic-gate if (error) {
36427c478bd9Sstevel@tonic-gate eprintsoline(so, error);
36437c478bd9Sstevel@tonic-gate }
36447c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
36457c478bd9Sstevel@tonic-gate return (error);
36467c478bd9Sstevel@tonic-gate }
36477c478bd9Sstevel@tonic-gate
36487c478bd9Sstevel@tonic-gate /*
36497c478bd9Sstevel@tonic-gate * Sending data with options on a connected stream socket.
36507c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set.
36517c478bd9Sstevel@tonic-gate */
36527c478bd9Sstevel@tonic-gate static int
sosend_svccmsg(struct sonode * so,struct uio * uiop,int more,void * control,t_uscalar_t controllen,int flags)36530f1702c5SYu Xiangning sosend_svccmsg(struct sonode *so, struct uio *uiop, int more, void *control,
36540f1702c5SYu Xiangning t_uscalar_t controllen, int flags)
36557c478bd9Sstevel@tonic-gate {
36567c478bd9Sstevel@tonic-gate struct T_optdata_req tdr;
36577c478bd9Sstevel@tonic-gate mblk_t *mp;
36587c478bd9Sstevel@tonic-gate int error;
36597c478bd9Sstevel@tonic-gate ssize_t iosize;
36607c478bd9Sstevel@tonic-gate int size;
36617c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf;
36627c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
36637c478bd9Sstevel@tonic-gate void *fds;
36647c478bd9Sstevel@tonic-gate int fdlen;
36657c478bd9Sstevel@tonic-gate struct T_opthdr toh;
36660f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
36677c478bd9Sstevel@tonic-gate
36687c478bd9Sstevel@tonic-gate dprintso(so, 1,
36697c478bd9Sstevel@tonic-gate ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
36707c478bd9Sstevel@tonic-gate
36717c478bd9Sstevel@tonic-gate /*
36727c478bd9Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are
36737c478bd9Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it
36747c478bd9Sstevel@tonic-gate * thus it is not possible to ASSERT on the state.
36757c478bd9Sstevel@tonic-gate */
36767c478bd9Sstevel@tonic-gate
36777c478bd9Sstevel@tonic-gate /* Options on connection-oriented only when SM_OPTDATA set. */
36787c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_OPTDATA))
36797c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
36807c478bd9Sstevel@tonic-gate
36817c478bd9Sstevel@tonic-gate do {
36827c478bd9Sstevel@tonic-gate /*
36837c478bd9Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this
36847c478bd9Sstevel@tonic-gate * message or if the caller passed in "more".
36857c478bd9Sstevel@tonic-gate * Error for transports with zero tidu_size.
36867c478bd9Sstevel@tonic-gate */
36877c478bd9Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ;
36880f1702c5SYu Xiangning iosize = sti->sti_tidu_size;
36897c478bd9Sstevel@tonic-gate if (iosize <= 0)
36907c478bd9Sstevel@tonic-gate return (EMSGSIZE);
36917c478bd9Sstevel@tonic-gate if (uiop->uio_resid > iosize) {
36927c478bd9Sstevel@tonic-gate tdr.DATA_flag = 1;
36937c478bd9Sstevel@tonic-gate } else {
36947c478bd9Sstevel@tonic-gate if (more)
36957c478bd9Sstevel@tonic-gate tdr.DATA_flag = 1;
36967c478bd9Sstevel@tonic-gate else
36977c478bd9Sstevel@tonic-gate tdr.DATA_flag = 0;
36987c478bd9Sstevel@tonic-gate iosize = uiop->uio_resid;
36997c478bd9Sstevel@tonic-gate }
37007c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
37017c478bd9Sstevel@tonic-gate tdr.DATA_flag, iosize));
37027c478bd9Sstevel@tonic-gate
37037c478bd9Sstevel@tonic-gate optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
37047c478bd9Sstevel@tonic-gate tdr.OPT_length = optlen;
37057c478bd9Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
37067c478bd9Sstevel@tonic-gate
37077c478bd9Sstevel@tonic-gate size = (int)sizeof (tdr) + optlen;
37087c478bd9Sstevel@tonic-gate /*
37097c478bd9Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set.
37107c478bd9Sstevel@tonic-gate */
37117c478bd9Sstevel@tonic-gate error = so_getfdopt(control, controllen,
37127c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2), &fds, &fdlen);
37137c478bd9Sstevel@tonic-gate if (error)
37147c478bd9Sstevel@tonic-gate return (error);
37157c478bd9Sstevel@tonic-gate if (fdlen != -1) {
37167c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING))
37177c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
37187c478bd9Sstevel@tonic-gate
37197c478bd9Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf);
37207c478bd9Sstevel@tonic-gate if (error)
37217c478bd9Sstevel@tonic-gate return (error);
3722d28d4716SJerry Jelinek
3723d28d4716SJerry Jelinek /*
3724d28d4716SJerry Jelinek * Pre-allocate enough additional space for lower level
3725d28d4716SJerry Jelinek * modules to append an option (e.g. see tl_unitdata).
3726d28d4716SJerry Jelinek * The following is enough extra space for the largest
3727d28d4716SJerry Jelinek * option we might append.
3728d28d4716SJerry Jelinek */
3729d28d4716SJerry Jelinek size += sizeof (struct T_opthdr) + ucredsize;
37307c478bd9Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf);
37317c478bd9Sstevel@tonic-gate } else {
3732de8c4a14SErik Nordmark mp = soallocproto(size, _ALLOC_INTR, CRED());
37337c478bd9Sstevel@tonic-gate if (mp == NULL) {
37347c478bd9Sstevel@tonic-gate /*
37357c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
37367c478bd9Sstevel@tonic-gate * Let send* return EINTR.
37377c478bd9Sstevel@tonic-gate */
37380f1702c5SYu Xiangning return (EINTR);
3739bd118333Smeem }
37407c478bd9Sstevel@tonic-gate }
37417c478bd9Sstevel@tonic-gate soappendmsg(mp, &tdr, sizeof (tdr));
37427c478bd9Sstevel@tonic-gate
37437c478bd9Sstevel@tonic-gate if (fdlen != -1) {
37447c478bd9Sstevel@tonic-gate ASSERT(fdbuf != NULL);
37457c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
37467c478bd9Sstevel@tonic-gate toh.name = SO_FILEP;
37477c478bd9Sstevel@tonic-gate toh.len = fdbuf->fd_size +
37487c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_opthdr);
37497c478bd9Sstevel@tonic-gate toh.status = 0;
37507c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
37517c478bd9Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size);
37527c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
37537c478bd9Sstevel@tonic-gate }
37547c478bd9Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
3755d28d4716SJerry Jelinek /*
3756d28d4716SJerry Jelinek * Normally at most 3 bytes left in the message, but we might
3757d28d4716SJerry Jelinek * have allowed for extra space if we're passing fd's through.
3758d28d4716SJerry Jelinek */
37597c478bd9Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size);
37607c478bd9Sstevel@tonic-gate
37617c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
37627c478bd9Sstevel@tonic-gate
37637c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
37647c478bd9Sstevel@tonic-gate 0, MSG_BAND, 0);
37657c478bd9Sstevel@tonic-gate if (error) {
37667c478bd9Sstevel@tonic-gate eprintsoline(so, error);
37677c478bd9Sstevel@tonic-gate return (error);
37687c478bd9Sstevel@tonic-gate }
37697c478bd9Sstevel@tonic-gate control = NULL;
37707c478bd9Sstevel@tonic-gate if (uiop->uio_resid > 0) {
37717c478bd9Sstevel@tonic-gate /*
37727c478bd9Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though
37737c478bd9Sstevel@tonic-gate * some data have been written. This is consistent
37747c478bd9Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics.
37757c478bd9Sstevel@tonic-gate */
37767c478bd9Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) {
37777c478bd9Sstevel@tonic-gate eprintsoline(so, error);
37787c478bd9Sstevel@tonic-gate return (EPIPE);
37797c478bd9Sstevel@tonic-gate }
37807c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
37817c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
37820f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
37837c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
37847c478bd9Sstevel@tonic-gate if (error != 0) {
37857c478bd9Sstevel@tonic-gate eprintsoline(so, error);
37867c478bd9Sstevel@tonic-gate return (error);
37877c478bd9Sstevel@tonic-gate }
37887c478bd9Sstevel@tonic-gate }
37897c478bd9Sstevel@tonic-gate }
37907c478bd9Sstevel@tonic-gate } while (uiop->uio_resid > 0);
37917c478bd9Sstevel@tonic-gate return (0);
37927c478bd9Sstevel@tonic-gate }
37937c478bd9Sstevel@tonic-gate
37947c478bd9Sstevel@tonic-gate /*
37957c478bd9Sstevel@tonic-gate * Sending data on a datagram socket.
37967c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set.
37977c478bd9Sstevel@tonic-gate *
3798f012ee0cSGordon Ross * For AF_UNIX the destination address may be already in
3799f012ee0cSGordon Ross * internal form, as indicated by sti->sti_faddr_noxlate
3800f012ee0cSGordon Ross * or the MSG_SENDTO_NOXLATE flag. Otherwise we need to
3801f012ee0cSGordon Ross * translate the destination address to internal form.
3802f012ee0cSGordon Ross *
3803f012ee0cSGordon Ross * The source address is passed as an option.
38047c478bd9Sstevel@tonic-gate */
38057c478bd9Sstevel@tonic-gate int
sosend_dgram(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,int flags)3806ff550d0eSmasputra sosend_dgram(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3807ff550d0eSmasputra struct uio *uiop, int flags)
38087c478bd9Sstevel@tonic-gate {
38097c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
38107c478bd9Sstevel@tonic-gate mblk_t *mp;
38117c478bd9Sstevel@tonic-gate int error;
38127c478bd9Sstevel@tonic-gate void *addr;
38137c478bd9Sstevel@tonic-gate socklen_t addrlen;
38147c478bd9Sstevel@tonic-gate void *src;
38157c478bd9Sstevel@tonic-gate socklen_t srclen;
38167c478bd9Sstevel@tonic-gate ssize_t len;
38170f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
38187c478bd9Sstevel@tonic-gate
3819ff550d0eSmasputra ASSERT(name != NULL && namelen != 0);
38207c478bd9Sstevel@tonic-gate
38217c478bd9Sstevel@tonic-gate len = uiop->uio_resid;
38220f1702c5SYu Xiangning if (len > sti->sti_tidu_size) {
38237c478bd9Sstevel@tonic-gate error = EMSGSIZE;
38247c478bd9Sstevel@tonic-gate goto done;
38257c478bd9Sstevel@tonic-gate }
38267c478bd9Sstevel@tonic-gate
3827f012ee0cSGordon Ross if (sti->sti_faddr_noxlate == 0 &&
3828f012ee0cSGordon Ross (flags & MSG_SENDTO_NOXLATE) == 0) {
3829f012ee0cSGordon Ross /*
3830f012ee0cSGordon Ross * Length and family checks.
3831f012ee0cSGordon Ross * Don't verify internal form.
3832f012ee0cSGordon Ross */
38337c478bd9Sstevel@tonic-gate error = so_addr_verify(so, name, namelen);
3834ff550d0eSmasputra if (error != 0)
38357c478bd9Sstevel@tonic-gate goto done;
3836f012ee0cSGordon Ross }
3837ff550d0eSmasputra
3838f012ee0cSGordon Ross if (sti->sti_direct) /* Never on AF_UNIX */
3839ff550d0eSmasputra return (sodgram_direct(so, name, namelen, uiop, flags));
3840ff550d0eSmasputra
38417c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
38420f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
38437c478bd9Sstevel@tonic-gate /*
38447c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
38457c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
38467c478bd9Sstevel@tonic-gate */
38477c478bd9Sstevel@tonic-gate addr = name;
38487c478bd9Sstevel@tonic-gate addrlen = namelen;
38497c478bd9Sstevel@tonic-gate src = NULL;
38507c478bd9Sstevel@tonic-gate srclen = 0;
3851f012ee0cSGordon Ross } else if (flags & MSG_SENDTO_NOXLATE) {
3852f012ee0cSGordon Ross /*
3853f012ee0cSGordon Ross * Have an internal form dest. address.
3854f012ee0cSGordon Ross * Pass the source address as usual.
3855f012ee0cSGordon Ross */
3856f012ee0cSGordon Ross addr = name;
3857f012ee0cSGordon Ross addrlen = namelen;
3858f012ee0cSGordon Ross src = sti->sti_laddr_sa;
3859f012ee0cSGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
38607c478bd9Sstevel@tonic-gate } else {
38617c478bd9Sstevel@tonic-gate /*
38627c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
38637c478bd9Sstevel@tonic-gate * and translate the remote address.
38647c478bd9Sstevel@tonic-gate *
38650f1702c5SYu Xiangning * Note that this code does not prevent sti_laddr_sa
38667c478bd9Sstevel@tonic-gate * from changing while it is being used. Thus
38677c478bd9Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this
38687c478bd9Sstevel@tonic-gate * send the peer might see a partially new and a
38697c478bd9Sstevel@tonic-gate * partially old "from" address.
38707c478bd9Sstevel@tonic-gate */
38710f1702c5SYu Xiangning src = sti->sti_laddr_sa;
38720f1702c5SYu Xiangning srclen = (socklen_t)sti->sti_laddr_len;
38737c478bd9Sstevel@tonic-gate dprintso(so, 1,
38747c478bd9Sstevel@tonic-gate ("sosend_dgram UNIX: srclen %d, src %p\n",
38757c478bd9Sstevel@tonic-gate srclen, src));
3876f012ee0cSGordon Ross /*
3877f012ee0cSGordon Ross * The sendmsg caller specified a destination
3878f012ee0cSGordon Ross * address, which we must translate into our
3879f012ee0cSGordon Ross * internal form. addr = &sti->sti_ux_taddr
3880f012ee0cSGordon Ross */
38817c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen,
38827c478bd9Sstevel@tonic-gate (flags & MSG_XPG4_2),
38837c478bd9Sstevel@tonic-gate &addr, &addrlen);
38847c478bd9Sstevel@tonic-gate if (error) {
38857c478bd9Sstevel@tonic-gate eprintsoline(so, error);
38867c478bd9Sstevel@tonic-gate goto done;
38877c478bd9Sstevel@tonic-gate }
38887c478bd9Sstevel@tonic-gate }
38897c478bd9Sstevel@tonic-gate } else {
38907c478bd9Sstevel@tonic-gate addr = name;
38917c478bd9Sstevel@tonic-gate addrlen = namelen;
38927c478bd9Sstevel@tonic-gate src = NULL;
38937c478bd9Sstevel@tonic-gate srclen = 0;
38947c478bd9Sstevel@tonic-gate }
38957c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
38967c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
38977c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
38987c478bd9Sstevel@tonic-gate if (srclen == 0) {
38997c478bd9Sstevel@tonic-gate tudr.OPT_length = 0;
39007c478bd9Sstevel@tonic-gate tudr.OPT_offset = 0;
39017c478bd9Sstevel@tonic-gate
39027c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
3903de8c4a14SErik Nordmark addr, addrlen, 0, _ALLOC_INTR, CRED());
39047c478bd9Sstevel@tonic-gate if (mp == NULL) {
39057c478bd9Sstevel@tonic-gate /*
39067c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
39077c478bd9Sstevel@tonic-gate * Let send* return EINTR.
39087c478bd9Sstevel@tonic-gate */
39097c478bd9Sstevel@tonic-gate error = EINTR;
39107c478bd9Sstevel@tonic-gate goto done;
39117c478bd9Sstevel@tonic-gate }
39127c478bd9Sstevel@tonic-gate } else {
39137c478bd9Sstevel@tonic-gate /*
39147c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
39157c478bd9Sstevel@tonic-gate * address option.
39167c478bd9Sstevel@tonic-gate */
39177c478bd9Sstevel@tonic-gate struct T_opthdr toh;
39187c478bd9Sstevel@tonic-gate ssize_t size;
39197c478bd9Sstevel@tonic-gate
39207c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
39217c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen));
39227c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
39237c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
39247c478bd9Sstevel@tonic-gate
39257c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
39267c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
39277c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
39287c478bd9Sstevel@tonic-gate toh.status = 0;
39297c478bd9Sstevel@tonic-gate
39307c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
39317c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
3932de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_INTR, CRED());
39337c478bd9Sstevel@tonic-gate if (mp == NULL) {
39347c478bd9Sstevel@tonic-gate /*
39357c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
39367c478bd9Sstevel@tonic-gate * Let send* return EINTR.
39377c478bd9Sstevel@tonic-gate */
39387c478bd9Sstevel@tonic-gate error = EINTR;
39397c478bd9Sstevel@tonic-gate goto done;
39407c478bd9Sstevel@tonic-gate }
39417c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
39427c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
39437c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
39447c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
39457c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
39467c478bd9Sstevel@tonic-gate }
39477c478bd9Sstevel@tonic-gate
3948005d3febSMarek Pospisil if (AU_AUDITING())
39497c478bd9Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
39507c478bd9Sstevel@tonic-gate
39517c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
39527c478bd9Sstevel@tonic-gate done:
39537c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
39547c478bd9Sstevel@tonic-gate if (error) {
39557c478bd9Sstevel@tonic-gate eprintsoline(so, error);
39567c478bd9Sstevel@tonic-gate }
39577c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
39587c478bd9Sstevel@tonic-gate return (error);
39597c478bd9Sstevel@tonic-gate }
39607c478bd9Sstevel@tonic-gate
39617c478bd9Sstevel@tonic-gate /*
39627c478bd9Sstevel@tonic-gate * Sending data on a connected stream socket.
39637c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set.
39647c478bd9Sstevel@tonic-gate */
39657c478bd9Sstevel@tonic-gate int
sosend_svc(struct sonode * so,struct uio * uiop,t_scalar_t prim,int more,int sflag)39660f1702c5SYu Xiangning sosend_svc(struct sonode *so, struct uio *uiop, t_scalar_t prim, int more,
39677c478bd9Sstevel@tonic-gate int sflag)
39687c478bd9Sstevel@tonic-gate {
39697c478bd9Sstevel@tonic-gate struct T_data_req tdr;
39707c478bd9Sstevel@tonic-gate mblk_t *mp;
39717c478bd9Sstevel@tonic-gate int error;
39727c478bd9Sstevel@tonic-gate ssize_t iosize;
39730f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
39747c478bd9Sstevel@tonic-gate
39757c478bd9Sstevel@tonic-gate dprintso(so, 1,
39767c478bd9Sstevel@tonic-gate ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
3977903a11ebSrh87107 (void *)so, uiop->uio_resid, prim, sflag));
39787c478bd9Sstevel@tonic-gate
39797c478bd9Sstevel@tonic-gate /*
39807c478bd9Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are
39817c478bd9Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it
39827c478bd9Sstevel@tonic-gate * thus it is not possible to ASSERT on the state.
39837c478bd9Sstevel@tonic-gate */
39847c478bd9Sstevel@tonic-gate
39857c478bd9Sstevel@tonic-gate do {
39867c478bd9Sstevel@tonic-gate /*
39877c478bd9Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this
39887c478bd9Sstevel@tonic-gate * message or if the caller passed in "more".
39897c478bd9Sstevel@tonic-gate * Error for transports with zero tidu_size.
39907c478bd9Sstevel@tonic-gate */
39917c478bd9Sstevel@tonic-gate tdr.PRIM_type = prim;
39920f1702c5SYu Xiangning iosize = sti->sti_tidu_size;
39937c478bd9Sstevel@tonic-gate if (iosize <= 0)
39947c478bd9Sstevel@tonic-gate return (EMSGSIZE);
39957c478bd9Sstevel@tonic-gate if (uiop->uio_resid > iosize) {
39967c478bd9Sstevel@tonic-gate tdr.MORE_flag = 1;
39977c478bd9Sstevel@tonic-gate } else {
39987c478bd9Sstevel@tonic-gate if (more)
39997c478bd9Sstevel@tonic-gate tdr.MORE_flag = 1;
40007c478bd9Sstevel@tonic-gate else
40017c478bd9Sstevel@tonic-gate tdr.MORE_flag = 0;
40027c478bd9Sstevel@tonic-gate iosize = uiop->uio_resid;
40037c478bd9Sstevel@tonic-gate }
40047c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
40057c478bd9Sstevel@tonic-gate prim, tdr.MORE_flag, iosize));
4006de8c4a14SErik Nordmark mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR, CRED());
40077c478bd9Sstevel@tonic-gate if (mp == NULL) {
40087c478bd9Sstevel@tonic-gate /*
40097c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
40107c478bd9Sstevel@tonic-gate * Let send* return EINTR.
40117c478bd9Sstevel@tonic-gate */
40127c478bd9Sstevel@tonic-gate return (EINTR);
40137c478bd9Sstevel@tonic-gate }
40147c478bd9Sstevel@tonic-gate
40157c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
40167c478bd9Sstevel@tonic-gate 0, sflag | MSG_BAND, 0);
40177c478bd9Sstevel@tonic-gate if (error) {
40187c478bd9Sstevel@tonic-gate eprintsoline(so, error);
40197c478bd9Sstevel@tonic-gate return (error);
40207c478bd9Sstevel@tonic-gate }
40217c478bd9Sstevel@tonic-gate if (uiop->uio_resid > 0) {
40227c478bd9Sstevel@tonic-gate /*
40237c478bd9Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though
40247c478bd9Sstevel@tonic-gate * some data have been written. This is consistent
40257c478bd9Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics.
40267c478bd9Sstevel@tonic-gate */
40277c478bd9Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) {
40287c478bd9Sstevel@tonic-gate eprintsoline(so, error);
40297c478bd9Sstevel@tonic-gate return (EPIPE);
40307c478bd9Sstevel@tonic-gate }
40317c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
40327c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
40330f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
40347c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
40357c478bd9Sstevel@tonic-gate if (error != 0) {
40367c478bd9Sstevel@tonic-gate eprintsoline(so, error);
40377c478bd9Sstevel@tonic-gate return (error);
40387c478bd9Sstevel@tonic-gate }
40397c478bd9Sstevel@tonic-gate }
40407c478bd9Sstevel@tonic-gate }
40417c478bd9Sstevel@tonic-gate } while (uiop->uio_resid > 0);
40427c478bd9Sstevel@tonic-gate return (0);
40437c478bd9Sstevel@tonic-gate }
40447c478bd9Sstevel@tonic-gate
40457c478bd9Sstevel@tonic-gate /*
40467c478bd9Sstevel@tonic-gate * Check the state for errors and call the appropriate send function.
40477c478bd9Sstevel@tonic-gate *
40487c478bd9Sstevel@tonic-gate * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
40497c478bd9Sstevel@tonic-gate * this function issues a setsockopt to toggle SO_DONTROUTE before and
40507c478bd9Sstevel@tonic-gate * after sending the message.
4051f012ee0cSGordon Ross *
4052f012ee0cSGordon Ross * The caller may optionally specify a destination address, for either
4053f012ee0cSGordon Ross * stream or datagram sockets. This table summarizes the cases:
4054f012ee0cSGordon Ross *
4055f012ee0cSGordon Ross * Socket type Dest. given Connected Result
4056f012ee0cSGordon Ross * ----------- ----------- --------- --------------
4057f012ee0cSGordon Ross * Stream * Yes send to conn. addr.
4058f012ee0cSGordon Ross * Stream * No error ENOTCONN
4059f012ee0cSGordon Ross * Dgram yes * send to given addr.
4060f012ee0cSGordon Ross * Dgram no yes send to conn. addr.
4061f012ee0cSGordon Ross * Dgram no no error EDESTADDRREQ
4062f012ee0cSGordon Ross *
4063f012ee0cSGordon Ross * There are subtleties around the destination address when using
4064f012ee0cSGordon Ross * AF_UNIX datagram sockets. When the sendmsg call specifies the
4065f012ee0cSGordon Ross * destination address, it's in (struct sockaddr_un) form and we
4066f012ee0cSGordon Ross * need to translate it to our internal form (struct so_ux_addr).
4067f012ee0cSGordon Ross *
4068f012ee0cSGordon Ross * When the sendmsg call does not specify a destination address
4069f012ee0cSGordon Ross * we're using the peer address saved during sotpi_connect, and
4070f012ee0cSGordon Ross * that address is already in internal form. In this case, the
4071f012ee0cSGordon Ross * (internal only) flag MSG_SENDTO_NOXLATE is set in the flags
4072f012ee0cSGordon Ross * passed to sosend_dgram or sosend_dgramcmsg to indicate that
4073f012ee0cSGordon Ross * those functions should skip translation to internal form.
4074f012ee0cSGordon Ross * Avoiding that translation is not only more efficient, but it's
4075f012ee0cSGordon Ross * also necessary when a process does a connect on an AF_UNIX
4076f012ee0cSGordon Ross * datagram socket and then drops privileges. After the process
4077f012ee0cSGordon Ross * has dropped privileges, it may no longer be able to lookup the
4078f012ee0cSGordon Ross * the external name in the filesystem, but it should still be
4079f012ee0cSGordon Ross * able to send messages on the connected socket by leaving the
4080f012ee0cSGordon Ross * destination name unspecified.
4081f012ee0cSGordon Ross *
4082f012ee0cSGordon Ross * Yet more subtleties arise with sockets connected by socketpair(),
4083f012ee0cSGordon Ross * which puts internal form addresses in the fields where normally
4084f012ee0cSGordon Ross * the external form is found, and sets sti_faddr_noxlate=1, which
4085f012ee0cSGordon Ross * (like flag MSG_SENDTO_NOXLATE) causes the sosend_dgram functions
4086f012ee0cSGordon Ross * to skip translation of destination addresses to internal form.
4087f012ee0cSGordon Ross * However, beware that the flag sti_faddr_noxlate=1 also triggers
4088f012ee0cSGordon Ross * different behaviour almost everywhere AF_UNIX addresses appear.
40897c478bd9Sstevel@tonic-gate */
40907c478bd9Sstevel@tonic-gate static int
sotpi_sendmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)40910f1702c5SYu Xiangning sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
40920f1702c5SYu Xiangning struct cred *cr)
40937c478bd9Sstevel@tonic-gate {
40947c478bd9Sstevel@tonic-gate int so_state;
40957c478bd9Sstevel@tonic-gate int so_mode;
40967c478bd9Sstevel@tonic-gate int error;
40977c478bd9Sstevel@tonic-gate struct sockaddr *name;
40987c478bd9Sstevel@tonic-gate t_uscalar_t namelen;
40997c478bd9Sstevel@tonic-gate int dontroute;
41007c478bd9Sstevel@tonic-gate int flags;
41010f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
41027c478bd9Sstevel@tonic-gate
41037c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
4104903a11ebSrh87107 (void *)so, (void *)msg, msg->msg_flags,
41057c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode), so->so_error));
41067c478bd9Sstevel@tonic-gate
41070f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
41080f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
41090f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
41100f1702c5SYu Xiangning return (strwrite(SOTOV(so), uiop, cr));
41110f1702c5SYu Xiangning }
41120f1702c5SYu Xiangning
41137c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
41147c478bd9Sstevel@tonic-gate so_state = so->so_state;
41157c478bd9Sstevel@tonic-gate
41167c478bd9Sstevel@tonic-gate if (so_state & SS_CANTSENDMORE) {
41177c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
41187c478bd9Sstevel@tonic-gate return (EPIPE);
41197c478bd9Sstevel@tonic-gate }
41207c478bd9Sstevel@tonic-gate
41217c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
41220f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
41237c478bd9Sstevel@tonic-gate if (error != 0) {
41247c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
41257c478bd9Sstevel@tonic-gate return (error);
41267c478bd9Sstevel@tonic-gate }
41277c478bd9Sstevel@tonic-gate }
41287c478bd9Sstevel@tonic-gate
41297c478bd9Sstevel@tonic-gate name = (struct sockaddr *)msg->msg_name;
41307c478bd9Sstevel@tonic-gate namelen = msg->msg_namelen;
4131f012ee0cSGordon Ross flags = msg->msg_flags;
4132f012ee0cSGordon Ross
4133f012ee0cSGordon Ross /*
4134f012ee0cSGordon Ross * Historically, this function does not validate the flags
4135f012ee0cSGordon Ross * passed in, and any errant bits are ignored. However,
4136f012ee0cSGordon Ross * we would not want any such errant flag bits accidently
4137f012ee0cSGordon Ross * being treated as one of the internal-only flags, so
4138f012ee0cSGordon Ross * clear the internal-only flag bits.
4139f012ee0cSGordon Ross */
4140f012ee0cSGordon Ross flags &= ~MSG_SENDTO_NOXLATE;
41417c478bd9Sstevel@tonic-gate
41427c478bd9Sstevel@tonic-gate so_mode = so->so_mode;
41437c478bd9Sstevel@tonic-gate
41447c478bd9Sstevel@tonic-gate if (name == NULL) {
41457c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED)) {
41467c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
41477c478bd9Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED)
41487c478bd9Sstevel@tonic-gate return (ENOTCONN);
41497c478bd9Sstevel@tonic-gate else
41507c478bd9Sstevel@tonic-gate return (EDESTADDRREQ);
41517c478bd9Sstevel@tonic-gate }
4152f012ee0cSGordon Ross /*
4153f012ee0cSGordon Ross * This is a connected socket.
4154f012ee0cSGordon Ross */
41557c478bd9Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) {
4156f012ee0cSGordon Ross /*
4157f012ee0cSGordon Ross * This is a connected STREAM socket,
4158f012ee0cSGordon Ross * destination not specified.
4159f012ee0cSGordon Ross */
41607c478bd9Sstevel@tonic-gate name = NULL;
41617c478bd9Sstevel@tonic-gate namelen = 0;
41627c478bd9Sstevel@tonic-gate } else {
41637c478bd9Sstevel@tonic-gate /*
4164f012ee0cSGordon Ross * Datagram send on connected socket with
4165f012ee0cSGordon Ross * the destination name not specified.
4166f012ee0cSGordon Ross * Use the peer address from connect.
41677c478bd9Sstevel@tonic-gate */
4168f012ee0cSGordon Ross if (so->so_family == AF_UNIX) {
4169f012ee0cSGordon Ross /*
4170f012ee0cSGordon Ross * Use the (internal form) address saved
4171f012ee0cSGordon Ross * in sotpi_connect. See above.
4172f012ee0cSGordon Ross */
4173f012ee0cSGordon Ross name = (void *)&sti->sti_ux_faddr;
4174f012ee0cSGordon Ross namelen = sizeof (sti->sti_ux_faddr);
4175f012ee0cSGordon Ross flags |= MSG_SENDTO_NOXLATE;
4176f012ee0cSGordon Ross } else {
41770f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa);
41780f1702c5SYu Xiangning name = sti->sti_faddr_sa;
41790f1702c5SYu Xiangning namelen = (t_uscalar_t)sti->sti_faddr_len;
41807c478bd9Sstevel@tonic-gate }
4181f012ee0cSGordon Ross }
41827c478bd9Sstevel@tonic-gate } else {
4183f012ee0cSGordon Ross /*
4184f012ee0cSGordon Ross * Sendmsg specifies a destination name
4185f012ee0cSGordon Ross */
41867c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED) &&
41877c478bd9Sstevel@tonic-gate (so_mode & SM_CONNREQUIRED)) {
4188f012ee0cSGordon Ross /* i.e. TCP not connected */
41897c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
41907c478bd9Sstevel@tonic-gate return (ENOTCONN);
41917c478bd9Sstevel@tonic-gate }
41927c478bd9Sstevel@tonic-gate /*
41937c478bd9Sstevel@tonic-gate * Ignore the address on connection-oriented sockets.
41947c478bd9Sstevel@tonic-gate * Just like BSD this code does not generate an error for
41957c478bd9Sstevel@tonic-gate * TCP (a CONNREQUIRED socket) when sending to an address
41967c478bd9Sstevel@tonic-gate * passed in with sendto/sendmsg. Instead the data is
41977c478bd9Sstevel@tonic-gate * delivered on the connection as if no address had been
41987c478bd9Sstevel@tonic-gate * supplied.
41997c478bd9Sstevel@tonic-gate */
42007c478bd9Sstevel@tonic-gate if ((so_state & SS_ISCONNECTED) &&
42017c478bd9Sstevel@tonic-gate !(so_mode & SM_CONNREQUIRED)) {
42027c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42037c478bd9Sstevel@tonic-gate return (EISCONN);
42047c478bd9Sstevel@tonic-gate }
42057c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISBOUND)) {
42067c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
42077c478bd9Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0,
42080f1702c5SYu Xiangning _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
42097c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
42107c478bd9Sstevel@tonic-gate if (error) {
42117c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42127c478bd9Sstevel@tonic-gate eprintsoline(so, error);
42137c478bd9Sstevel@tonic-gate return (error);
42147c478bd9Sstevel@tonic-gate }
42157c478bd9Sstevel@tonic-gate }
42167c478bd9Sstevel@tonic-gate /*
42177c478bd9Sstevel@tonic-gate * Handle delayed datagram errors. These are only queued
42187c478bd9Sstevel@tonic-gate * when the application sets SO_DGRAM_ERRIND.
42197c478bd9Sstevel@tonic-gate * Return the error if we are sending to the address
42207c478bd9Sstevel@tonic-gate * that was returned in the last T_UDERROR_IND.
42217c478bd9Sstevel@tonic-gate * If sending to some other address discard the delayed
42227c478bd9Sstevel@tonic-gate * error indication.
42237c478bd9Sstevel@tonic-gate */
42240f1702c5SYu Xiangning if (sti->sti_delayed_error) {
42257c478bd9Sstevel@tonic-gate struct T_uderror_ind *tudi;
42267c478bd9Sstevel@tonic-gate void *addr;
42277c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
42287c478bd9Sstevel@tonic-gate boolean_t match = B_FALSE;
42297c478bd9Sstevel@tonic-gate
42300f1702c5SYu Xiangning ASSERT(sti->sti_eaddr_mp);
42310f1702c5SYu Xiangning error = sti->sti_delayed_error;
42320f1702c5SYu Xiangning sti->sti_delayed_error = 0;
42330f1702c5SYu Xiangning tudi =
42340f1702c5SYu Xiangning (struct T_uderror_ind *)sti->sti_eaddr_mp->b_rptr;
42357c478bd9Sstevel@tonic-gate addrlen = tudi->DEST_length;
42360f1702c5SYu Xiangning addr = sogetoff(sti->sti_eaddr_mp,
42370f1702c5SYu Xiangning tudi->DEST_offset, addrlen, 1);
42387c478bd9Sstevel@tonic-gate ASSERT(addr); /* Checked by strsock_proto */
42397c478bd9Sstevel@tonic-gate switch (so->so_family) {
42407c478bd9Sstevel@tonic-gate case AF_INET: {
42417c478bd9Sstevel@tonic-gate /* Compare just IP address and port */
42427c478bd9Sstevel@tonic-gate sin_t *sin1 = (sin_t *)name;
42437c478bd9Sstevel@tonic-gate sin_t *sin2 = (sin_t *)addr;
42447c478bd9Sstevel@tonic-gate
42457c478bd9Sstevel@tonic-gate if (addrlen == sizeof (sin_t) &&
42467c478bd9Sstevel@tonic-gate namelen == addrlen &&
42477c478bd9Sstevel@tonic-gate sin1->sin_port == sin2->sin_port &&
42487c478bd9Sstevel@tonic-gate sin1->sin_addr.s_addr ==
42497c478bd9Sstevel@tonic-gate sin2->sin_addr.s_addr)
42507c478bd9Sstevel@tonic-gate match = B_TRUE;
42517c478bd9Sstevel@tonic-gate break;
42527c478bd9Sstevel@tonic-gate }
42537c478bd9Sstevel@tonic-gate case AF_INET6: {
42547c478bd9Sstevel@tonic-gate /* Compare just IP address and port. Not flow */
42557c478bd9Sstevel@tonic-gate sin6_t *sin1 = (sin6_t *)name;
42567c478bd9Sstevel@tonic-gate sin6_t *sin2 = (sin6_t *)addr;
42577c478bd9Sstevel@tonic-gate
42587c478bd9Sstevel@tonic-gate if (addrlen == sizeof (sin6_t) &&
42597c478bd9Sstevel@tonic-gate namelen == addrlen &&
42607c478bd9Sstevel@tonic-gate sin1->sin6_port == sin2->sin6_port &&
42617c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
42627c478bd9Sstevel@tonic-gate &sin2->sin6_addr))
42637c478bd9Sstevel@tonic-gate match = B_TRUE;
42647c478bd9Sstevel@tonic-gate break;
42657c478bd9Sstevel@tonic-gate }
42667c478bd9Sstevel@tonic-gate case AF_UNIX:
42677c478bd9Sstevel@tonic-gate default:
42687c478bd9Sstevel@tonic-gate if (namelen == addrlen &&
42697c478bd9Sstevel@tonic-gate bcmp(name, addr, namelen) == 0)
42707c478bd9Sstevel@tonic-gate match = B_TRUE;
42717c478bd9Sstevel@tonic-gate }
42727c478bd9Sstevel@tonic-gate if (match) {
42730f1702c5SYu Xiangning freemsg(sti->sti_eaddr_mp);
42740f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
42757c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42767c478bd9Sstevel@tonic-gate #ifdef DEBUG
42777c478bd9Sstevel@tonic-gate dprintso(so, 0,
42787c478bd9Sstevel@tonic-gate ("sockfs delayed error %d for %s\n",
42797c478bd9Sstevel@tonic-gate error,
42807c478bd9Sstevel@tonic-gate pr_addr(so->so_family, name, namelen)));
42817c478bd9Sstevel@tonic-gate #endif /* DEBUG */
42827c478bd9Sstevel@tonic-gate return (error);
42837c478bd9Sstevel@tonic-gate }
42840f1702c5SYu Xiangning freemsg(sti->sti_eaddr_mp);
42850f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
42867c478bd9Sstevel@tonic-gate }
42877c478bd9Sstevel@tonic-gate }
42887c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42897c478bd9Sstevel@tonic-gate
42907c478bd9Sstevel@tonic-gate dontroute = 0;
42917c478bd9Sstevel@tonic-gate if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
42927c478bd9Sstevel@tonic-gate uint32_t val;
42937c478bd9Sstevel@tonic-gate
42947c478bd9Sstevel@tonic-gate val = 1;
42957c478bd9Sstevel@tonic-gate error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
42960f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
42977c478bd9Sstevel@tonic-gate if (error)
42987c478bd9Sstevel@tonic-gate return (error);
42997c478bd9Sstevel@tonic-gate dontroute = 1;
43007c478bd9Sstevel@tonic-gate }
43017c478bd9Sstevel@tonic-gate
43027c478bd9Sstevel@tonic-gate if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
43037c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
43047c478bd9Sstevel@tonic-gate goto done;
43057c478bd9Sstevel@tonic-gate }
43067c478bd9Sstevel@tonic-gate if (msg->msg_controllen != 0) {
43077c478bd9Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) {
43080f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
43097c478bd9Sstevel@tonic-gate error = sosend_dgramcmsg(so, name, namelen, uiop,
4310ff550d0eSmasputra msg->msg_control, msg->msg_controllen, flags);
43117c478bd9Sstevel@tonic-gate } else {
43127c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
43137c478bd9Sstevel@tonic-gate /* Can't generate T_EXDATA_REQ with options */
43147c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
43157c478bd9Sstevel@tonic-gate goto done;
43167c478bd9Sstevel@tonic-gate }
43170f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
43187c478bd9Sstevel@tonic-gate error = sosend_svccmsg(so, uiop,
43197c478bd9Sstevel@tonic-gate !(flags & MSG_EOR),
43207c478bd9Sstevel@tonic-gate msg->msg_control, msg->msg_controllen,
43217c478bd9Sstevel@tonic-gate flags);
43227c478bd9Sstevel@tonic-gate }
43237c478bd9Sstevel@tonic-gate goto done;
43247c478bd9Sstevel@tonic-gate }
43257c478bd9Sstevel@tonic-gate
43260f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
43277c478bd9Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) {
43287c478bd9Sstevel@tonic-gate /*
43297c478bd9Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately
4330ff550d0eSmasputra * from send_dgram. This can allow tail-call optimizations.
43317c478bd9Sstevel@tonic-gate */
43327c478bd9Sstevel@tonic-gate if (!dontroute) {
43337c478bd9Sstevel@tonic-gate return (sosend_dgram(so, name, namelen, uiop, flags));
43347c478bd9Sstevel@tonic-gate }
43357c478bd9Sstevel@tonic-gate error = sosend_dgram(so, name, namelen, uiop, flags);
43367c478bd9Sstevel@tonic-gate } else {
43377c478bd9Sstevel@tonic-gate t_scalar_t prim;
43387c478bd9Sstevel@tonic-gate int sflag;
43397c478bd9Sstevel@tonic-gate
43407c478bd9Sstevel@tonic-gate /* Ignore msg_name in the connected state */
43417c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
43427c478bd9Sstevel@tonic-gate prim = T_EXDATA_REQ;
43437c478bd9Sstevel@tonic-gate /*
43447c478bd9Sstevel@tonic-gate * Send down T_EXDATA_REQ even if there is flow
43457c478bd9Sstevel@tonic-gate * control for data.
43467c478bd9Sstevel@tonic-gate */
43477c478bd9Sstevel@tonic-gate sflag = MSG_IGNFLOW;
43487c478bd9Sstevel@tonic-gate } else {
43497c478bd9Sstevel@tonic-gate if (so_mode & SM_BYTESTREAM) {
43507c478bd9Sstevel@tonic-gate /* Byte stream transport - use write */
43517c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg: write\n"));
43520f1702c5SYu Xiangning
43530f1702c5SYu Xiangning /* Send M_DATA messages */
43547c478bd9Sstevel@tonic-gate /*
4355ff550d0eSmasputra * If there is no SO_DONTROUTE to turn off,
43560f1702c5SYu Xiangning * sti_direct is on, and there is no flow
4357ff550d0eSmasputra * control, we can take the fast path.
43587c478bd9Sstevel@tonic-gate */
43590f1702c5SYu Xiangning if (!dontroute && sti->sti_direct != 0 &&
4360ff550d0eSmasputra canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4361ff550d0eSmasputra return (sostream_direct(so, uiop,
43620f1702c5SYu Xiangning NULL, cr));
4363ff550d0eSmasputra }
43640f1702c5SYu Xiangning error = strwrite(SOTOV(so), uiop, cr);
43657c478bd9Sstevel@tonic-gate goto done;
43667c478bd9Sstevel@tonic-gate }
43677c478bd9Sstevel@tonic-gate prim = T_DATA_REQ;
43687c478bd9Sstevel@tonic-gate sflag = 0;
43697c478bd9Sstevel@tonic-gate }
43707c478bd9Sstevel@tonic-gate /*
43717c478bd9Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately
43727c478bd9Sstevel@tonic-gate * from sosend_svc. This can allow tail-call optimizations.
43737c478bd9Sstevel@tonic-gate */
43747c478bd9Sstevel@tonic-gate if (!dontroute)
43757c478bd9Sstevel@tonic-gate return (sosend_svc(so, uiop, prim,
43767c478bd9Sstevel@tonic-gate !(flags & MSG_EOR), sflag));
43777c478bd9Sstevel@tonic-gate error = sosend_svc(so, uiop, prim,
43787c478bd9Sstevel@tonic-gate !(flags & MSG_EOR), sflag);
43797c478bd9Sstevel@tonic-gate }
43807c478bd9Sstevel@tonic-gate ASSERT(dontroute);
43817c478bd9Sstevel@tonic-gate done:
43827c478bd9Sstevel@tonic-gate if (dontroute) {
43837c478bd9Sstevel@tonic-gate uint32_t val;
43847c478bd9Sstevel@tonic-gate
43857c478bd9Sstevel@tonic-gate val = 0;
43867c478bd9Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
43870f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
43887c478bd9Sstevel@tonic-gate }
43897c478bd9Sstevel@tonic-gate return (error);
43907c478bd9Sstevel@tonic-gate }
43917c478bd9Sstevel@tonic-gate
43927c478bd9Sstevel@tonic-gate /*
43930f1702c5SYu Xiangning * kstrwritemp() has very similar semantics as that of strwrite().
43940f1702c5SYu Xiangning * The main difference is it obtains mblks from the caller and also
43950f1702c5SYu Xiangning * does not do any copy as done in strwrite() from user buffers to
43960f1702c5SYu Xiangning * kernel buffers.
43970f1702c5SYu Xiangning *
43980f1702c5SYu Xiangning * Currently, this routine is used by sendfile to send data allocated
43990f1702c5SYu Xiangning * within the kernel without any copying. This interface does not use the
44000f1702c5SYu Xiangning * synchronous stream interface as synch. stream interface implies
44010f1702c5SYu Xiangning * copying.
44020f1702c5SYu Xiangning */
44030f1702c5SYu Xiangning int
kstrwritemp(struct vnode * vp,mblk_t * mp,ushort_t fmode)44040f1702c5SYu Xiangning kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode)
44050f1702c5SYu Xiangning {
44060f1702c5SYu Xiangning struct stdata *stp;
44070f1702c5SYu Xiangning struct queue *wqp;
44080f1702c5SYu Xiangning mblk_t *newmp;
44090f1702c5SYu Xiangning char waitflag;
44100f1702c5SYu Xiangning int tempmode;
44110f1702c5SYu Xiangning int error = 0;
44120f1702c5SYu Xiangning int done = 0;
44130f1702c5SYu Xiangning struct sonode *so;
44140f1702c5SYu Xiangning boolean_t direct;
44150f1702c5SYu Xiangning
44160f1702c5SYu Xiangning ASSERT(vp->v_stream);
44170f1702c5SYu Xiangning stp = vp->v_stream;
44180f1702c5SYu Xiangning
44190f1702c5SYu Xiangning so = VTOSO(vp);
44200f1702c5SYu Xiangning direct = _SOTOTPI(so)->sti_direct;
44210f1702c5SYu Xiangning
44220f1702c5SYu Xiangning /*
44230f1702c5SYu Xiangning * This is the sockfs direct fast path. canputnext() need
44240f1702c5SYu Xiangning * not be accurate so we don't grab the sd_lock here. If
44250f1702c5SYu Xiangning * we get flow-controlled, we grab sd_lock just before the
44260f1702c5SYu Xiangning * do..while loop below to emulate what strwrite() does.
44270f1702c5SYu Xiangning */
44280f1702c5SYu Xiangning wqp = stp->sd_wrq;
44290f1702c5SYu Xiangning if (canputnext(wqp) && direct &&
44300f1702c5SYu Xiangning !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
44310f1702c5SYu Xiangning return (sostream_direct(so, NULL, mp, CRED()));
44320f1702c5SYu Xiangning } else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
44330f1702c5SYu Xiangning /* Fast check of flags before acquiring the lock */
44340f1702c5SYu Xiangning mutex_enter(&stp->sd_lock);
44350f1702c5SYu Xiangning error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
44360f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
44370f1702c5SYu Xiangning if (error != 0) {
44380f1702c5SYu Xiangning if (!(stp->sd_flag & STPLEX) &&
44390f1702c5SYu Xiangning (stp->sd_wput_opt & SW_SIGPIPE)) {
44400f1702c5SYu Xiangning error = EPIPE;
44410f1702c5SYu Xiangning }
44420f1702c5SYu Xiangning return (error);
44430f1702c5SYu Xiangning }
44440f1702c5SYu Xiangning }
44450f1702c5SYu Xiangning
44460f1702c5SYu Xiangning waitflag = WRITEWAIT;
44470f1702c5SYu Xiangning if (stp->sd_flag & OLDNDELAY)
44480f1702c5SYu Xiangning tempmode = fmode & ~FNDELAY;
44490f1702c5SYu Xiangning else
44500f1702c5SYu Xiangning tempmode = fmode;
44510f1702c5SYu Xiangning
44520f1702c5SYu Xiangning mutex_enter(&stp->sd_lock);
44530f1702c5SYu Xiangning do {
44540f1702c5SYu Xiangning if (canputnext(wqp)) {
44550f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
44560f1702c5SYu Xiangning if (stp->sd_wputdatafunc != NULL) {
44570f1702c5SYu Xiangning newmp = (stp->sd_wputdatafunc)(vp, mp, NULL,
44580f1702c5SYu Xiangning NULL, NULL, NULL);
44590f1702c5SYu Xiangning if (newmp == NULL) {
44600f1702c5SYu Xiangning /* The caller will free mp */
44610f1702c5SYu Xiangning return (ECOMM);
44620f1702c5SYu Xiangning }
44630f1702c5SYu Xiangning mp = newmp;
44640f1702c5SYu Xiangning }
44650f1702c5SYu Xiangning putnext(wqp, mp);
44660f1702c5SYu Xiangning return (0);
44670f1702c5SYu Xiangning }
44680f1702c5SYu Xiangning error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1,
44690f1702c5SYu Xiangning &done);
44700f1702c5SYu Xiangning } while (error == 0 && !done);
44710f1702c5SYu Xiangning
44720f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
44730f1702c5SYu Xiangning /*
44740f1702c5SYu Xiangning * EAGAIN tells the application to try again. ENOMEM
44750f1702c5SYu Xiangning * is returned only if the memory allocation size
44760f1702c5SYu Xiangning * exceeds the physical limits of the system. ENOMEM
44770f1702c5SYu Xiangning * can't be true here.
44780f1702c5SYu Xiangning */
44790f1702c5SYu Xiangning if (error == ENOMEM)
44800f1702c5SYu Xiangning error = EAGAIN;
44810f1702c5SYu Xiangning return (error);
44820f1702c5SYu Xiangning }
44830f1702c5SYu Xiangning
44840f1702c5SYu Xiangning /* ARGSUSED */
44850f1702c5SYu Xiangning static int
sotpi_sendmblk(struct sonode * so,struct nmsghdr * msg,int fflag,struct cred * cr,mblk_t ** mpp)44860f1702c5SYu Xiangning sotpi_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
44870f1702c5SYu Xiangning struct cred *cr, mblk_t **mpp)
44880f1702c5SYu Xiangning {
44890f1702c5SYu Xiangning int error;
44900f1702c5SYu Xiangning
4491acb55917SPatrick Mooney switch (so->so_family) {
4492acb55917SPatrick Mooney case AF_INET:
4493acb55917SPatrick Mooney case AF_INET6:
4494acb55917SPatrick Mooney case AF_UNIX:
4495acb55917SPatrick Mooney break;
4496acb55917SPatrick Mooney default:
44970f1702c5SYu Xiangning return (EAFNOSUPPORT);
44980f1702c5SYu Xiangning
4499acb55917SPatrick Mooney }
4500acb55917SPatrick Mooney
45010f1702c5SYu Xiangning if (so->so_state & SS_CANTSENDMORE)
45020f1702c5SYu Xiangning return (EPIPE);
45030f1702c5SYu Xiangning
45040f1702c5SYu Xiangning if (so->so_type != SOCK_STREAM)
45050f1702c5SYu Xiangning return (EOPNOTSUPP);
45060f1702c5SYu Xiangning
45070f1702c5SYu Xiangning if ((so->so_state & SS_ISCONNECTED) == 0)
45080f1702c5SYu Xiangning return (ENOTCONN);
45090f1702c5SYu Xiangning
45100f1702c5SYu Xiangning error = kstrwritemp(so->so_vnode, *mpp, fflag);
45110f1702c5SYu Xiangning if (error == 0)
45120f1702c5SYu Xiangning *mpp = NULL;
45130f1702c5SYu Xiangning return (error);
45140f1702c5SYu Xiangning }
45150f1702c5SYu Xiangning
45160f1702c5SYu Xiangning /*
4517ff550d0eSmasputra * Sending data on a datagram socket.
4518ff550d0eSmasputra * Assumes caller has verified that SS_ISBOUND etc. are set.
4519ff550d0eSmasputra */
4520ff550d0eSmasputra /* ARGSUSED */
4521ff550d0eSmasputra static int
sodgram_direct(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,int flags)4522ff550d0eSmasputra sodgram_direct(struct sonode *so, struct sockaddr *name,
4523ff550d0eSmasputra socklen_t namelen, struct uio *uiop, int flags)
4524ff550d0eSmasputra {
4525ff550d0eSmasputra struct T_unitdata_req tudr;
4526fc80c0dfSnordmark mblk_t *mp = NULL;
4527ff550d0eSmasputra int error = 0;
4528ff550d0eSmasputra void *addr;
4529ff550d0eSmasputra socklen_t addrlen;
4530ff550d0eSmasputra ssize_t len;
4531ff550d0eSmasputra struct stdata *stp = SOTOV(so)->v_stream;
4532ff550d0eSmasputra int so_state;
4533ff550d0eSmasputra queue_t *udp_wq;
4534fc80c0dfSnordmark boolean_t connected;
4535fc80c0dfSnordmark mblk_t *mpdata = NULL;
45360f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
4537005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
4538ff550d0eSmasputra
4539ff550d0eSmasputra ASSERT(name != NULL && namelen != 0);
4540ff550d0eSmasputra ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4541ff550d0eSmasputra ASSERT(!(so->so_mode & SM_EXDATA));
4542ff550d0eSmasputra ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4543ff550d0eSmasputra ASSERT(SOTOV(so)->v_type == VSOCK);
4544ff550d0eSmasputra
4545ff550d0eSmasputra /* Caller checked for proper length */
4546ff550d0eSmasputra len = uiop->uio_resid;
45470f1702c5SYu Xiangning ASSERT(len <= sti->sti_tidu_size);
4548ff550d0eSmasputra
4549ff550d0eSmasputra /* Length and family checks have been done by caller */
4550ff550d0eSmasputra ASSERT(name->sa_family == so->so_family);
4551ff550d0eSmasputra ASSERT(so->so_family == AF_INET ||
4552ff550d0eSmasputra (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4553ff550d0eSmasputra ASSERT(so->so_family == AF_INET6 ||
4554ff550d0eSmasputra (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4555ff550d0eSmasputra
4556ff550d0eSmasputra addr = name;
4557ff550d0eSmasputra addrlen = namelen;
4558ff550d0eSmasputra
4559ff550d0eSmasputra if (stp->sd_sidp != NULL &&
4560ff550d0eSmasputra (error = straccess(stp, JCWRITE)) != 0)
4561ff550d0eSmasputra goto done;
4562ff550d0eSmasputra
4563ff550d0eSmasputra so_state = so->so_state;
4564ff550d0eSmasputra
4565fc80c0dfSnordmark connected = so_state & SS_ISCONNECTED;
4566fc80c0dfSnordmark if (!connected) {
4567fc80c0dfSnordmark tudr.PRIM_type = T_UNITDATA_REQ;
4568fc80c0dfSnordmark tudr.DEST_length = addrlen;
4569fc80c0dfSnordmark tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
4570fc80c0dfSnordmark tudr.OPT_length = 0;
4571fc80c0dfSnordmark tudr.OPT_offset = 0;
4572fc80c0dfSnordmark
4573fc80c0dfSnordmark mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0,
4574de8c4a14SErik Nordmark _ALLOC_INTR, CRED());
4575fc80c0dfSnordmark if (mp == NULL) {
4576fc80c0dfSnordmark /*
4577fc80c0dfSnordmark * Caught a signal waiting for memory.
4578fc80c0dfSnordmark * Let send* return EINTR.
4579fc80c0dfSnordmark */
4580fc80c0dfSnordmark error = EINTR;
4581fc80c0dfSnordmark goto done;
4582fc80c0dfSnordmark }
4583fc80c0dfSnordmark }
4584fc80c0dfSnordmark
4585ff550d0eSmasputra /*
4586ff550d0eSmasputra * For UDP we don't break up the copyin into smaller pieces
4587ff550d0eSmasputra * as in the TCP case. That means if ENOMEM is returned by
4588ff550d0eSmasputra * mcopyinuio() then the uio vector has not been modified at
4589ff550d0eSmasputra * all and we fallback to either strwrite() or kstrputmsg()
4590ff550d0eSmasputra * below. Note also that we never generate priority messages
4591ff550d0eSmasputra * from here.
4592ff550d0eSmasputra */
4593ff550d0eSmasputra udp_wq = stp->sd_wrq->q_next;
4594ff550d0eSmasputra if (canput(udp_wq) &&
4595fc80c0dfSnordmark (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
4596fc80c0dfSnordmark ASSERT(DB_TYPE(mpdata) == M_DATA);
4597ff550d0eSmasputra ASSERT(uiop->uio_resid == 0);
4598fc80c0dfSnordmark if (!connected)
4599fc80c0dfSnordmark linkb(mp, mpdata);
4600fc80c0dfSnordmark else
4601fc80c0dfSnordmark mp = mpdata;
4602005d3febSMarek Pospisil if (auditing)
4603ff550d0eSmasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4604fc80c0dfSnordmark
460552aec5b9SDan McDonald /* Always returns 0... */
460652aec5b9SDan McDonald return (udp_wput(udp_wq, mp));
4607ff550d0eSmasputra }
4608fc80c0dfSnordmark
4609fc80c0dfSnordmark ASSERT(mpdata == NULL);
4610fc80c0dfSnordmark if (error != 0 && error != ENOMEM) {
4611fc80c0dfSnordmark freemsg(mp);
4612ff550d0eSmasputra return (error);
4613fc80c0dfSnordmark }
4614ff550d0eSmasputra
4615ff550d0eSmasputra /*
4616ff550d0eSmasputra * For connected, let strwrite() handle the blocking case.
4617ff550d0eSmasputra * Otherwise we fall thru and use kstrputmsg().
4618ff550d0eSmasputra */
4619fc80c0dfSnordmark if (connected)
4620ff550d0eSmasputra return (strwrite(SOTOV(so), uiop, CRED()));
4621ff550d0eSmasputra
4622005d3febSMarek Pospisil if (auditing)
4623ff550d0eSmasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4624ff550d0eSmasputra
4625ff550d0eSmasputra error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4626ff550d0eSmasputra done:
4627ff550d0eSmasputra #ifdef SOCK_DEBUG
4628ff550d0eSmasputra if (error != 0) {
4629ff550d0eSmasputra eprintsoline(so, error);
4630ff550d0eSmasputra }
4631ff550d0eSmasputra #endif /* SOCK_DEBUG */
4632ff550d0eSmasputra return (error);
4633ff550d0eSmasputra }
4634ff550d0eSmasputra
4635ff550d0eSmasputra int
sostream_direct(struct sonode * so,struct uio * uiop,mblk_t * mp,cred_t * cr)4636ff550d0eSmasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4637ff550d0eSmasputra {
4638ff550d0eSmasputra struct stdata *stp = SOTOV(so)->v_stream;
4639ff550d0eSmasputra ssize_t iosize, rmax, maxblk;
4640ff550d0eSmasputra queue_t *tcp_wq = stp->sd_wrq->q_next;
4641c28749e9Skais mblk_t *newmp;
4642ff550d0eSmasputra int error = 0, wflag = 0;
4643ff550d0eSmasputra
4644ff550d0eSmasputra ASSERT(so->so_mode & SM_BYTESTREAM);
4645ff550d0eSmasputra ASSERT(SOTOV(so)->v_type == VSOCK);
4646ff550d0eSmasputra
4647ff550d0eSmasputra if (stp->sd_sidp != NULL &&
4648ff550d0eSmasputra (error = straccess(stp, JCWRITE)) != 0)
4649ff550d0eSmasputra return (error);
4650ff550d0eSmasputra
4651ff550d0eSmasputra if (uiop == NULL) {
4652ff550d0eSmasputra /*
4653ff550d0eSmasputra * kstrwritemp() should have checked sd_flag and
4654ff550d0eSmasputra * flow-control before coming here. If we end up
4655ff550d0eSmasputra * here it means that we can simply pass down the
4656ff550d0eSmasputra * data to tcp.
4657ff550d0eSmasputra */
4658ff550d0eSmasputra ASSERT(mp != NULL);
4659c28749e9Skais if (stp->sd_wputdatafunc != NULL) {
4660c28749e9Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4661c28749e9Skais NULL, NULL, NULL);
4662c28749e9Skais if (newmp == NULL) {
4663c28749e9Skais /* The caller will free mp */
4664c28749e9Skais return (ECOMM);
4665c28749e9Skais }
4666c28749e9Skais mp = newmp;
4667c28749e9Skais }
466852aec5b9SDan McDonald /* Always returns 0... */
466952aec5b9SDan McDonald return (tcp_wput(tcp_wq, mp));
4670ff550d0eSmasputra }
4671ff550d0eSmasputra
4672ff550d0eSmasputra /* Fallback to strwrite() to do proper error handling */
4673ff550d0eSmasputra if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4674ff550d0eSmasputra return (strwrite(SOTOV(so), uiop, cr));
4675ff550d0eSmasputra
4676ff550d0eSmasputra rmax = stp->sd_qn_maxpsz;
4677ff550d0eSmasputra ASSERT(rmax >= 0 || rmax == INFPSZ);
4678ff550d0eSmasputra if (rmax == 0 || uiop->uio_resid <= 0)
4679ff550d0eSmasputra return (0);
4680ff550d0eSmasputra
4681ff550d0eSmasputra if (rmax == INFPSZ)
4682ff550d0eSmasputra rmax = uiop->uio_resid;
4683ff550d0eSmasputra
4684ff550d0eSmasputra maxblk = stp->sd_maxblk;
4685ff550d0eSmasputra
4686ff550d0eSmasputra for (;;) {
4687ff550d0eSmasputra iosize = MIN(uiop->uio_resid, rmax);
4688ff550d0eSmasputra
4689ff550d0eSmasputra mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4690ff550d0eSmasputra if (mp == NULL) {
4691ff550d0eSmasputra /*
4692ff550d0eSmasputra * Fallback to strwrite() for ENOMEM; if this
4693ff550d0eSmasputra * is our first time in this routine and the uio
4694ff550d0eSmasputra * vector has not been modified, we will end up
4695ff550d0eSmasputra * calling strwrite() without any flag set.
4696ff550d0eSmasputra */
4697ff550d0eSmasputra if (error == ENOMEM)
4698ff550d0eSmasputra goto slow_send;
4699ff550d0eSmasputra else
4700ff550d0eSmasputra return (error);
4701ff550d0eSmasputra }
4702ff550d0eSmasputra ASSERT(uiop->uio_resid >= 0);
4703ff550d0eSmasputra /*
4704ff550d0eSmasputra * If mp is non-NULL and ENOMEM is set, it means that
4705ff550d0eSmasputra * mcopyinuio() was able to break down some of the user
4706ff550d0eSmasputra * data into one or more mblks. Send the partial data
4707ff550d0eSmasputra * to tcp and let the rest be handled in strwrite().
4708ff550d0eSmasputra */
4709ff550d0eSmasputra ASSERT(error == 0 || error == ENOMEM);
4710c28749e9Skais if (stp->sd_wputdatafunc != NULL) {
4711c28749e9Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4712c28749e9Skais NULL, NULL, NULL);
4713c28749e9Skais if (newmp == NULL) {
4714c28749e9Skais /* The caller will free mp */
4715c28749e9Skais return (ECOMM);
4716c28749e9Skais }
4717c28749e9Skais mp = newmp;
4718c28749e9Skais }
471952aec5b9SDan McDonald (void) tcp_wput(tcp_wq, mp); /* Always returns 0 anyway. */
4720ff550d0eSmasputra
4721ff550d0eSmasputra wflag |= NOINTR;
4722ff550d0eSmasputra
4723ff550d0eSmasputra if (uiop->uio_resid == 0) { /* No more data; we're done */
4724ff550d0eSmasputra ASSERT(error == 0);
4725ff550d0eSmasputra break;
4726ff550d0eSmasputra } else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4727ff550d0eSmasputra (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4728ff550d0eSmasputra slow_send:
4729ff550d0eSmasputra /*
4730ff550d0eSmasputra * We were able to send down partial data using
4731ff550d0eSmasputra * the direct call interface, but are now relying
4732ff550d0eSmasputra * on strwrite() to handle the non-fastpath cases.
4733ff550d0eSmasputra * If the socket is blocking we will sleep in
4734ff550d0eSmasputra * strwaitq() until write is permitted, otherwise,
4735ff550d0eSmasputra * we will need to return the amount of bytes
4736ff550d0eSmasputra * written so far back to the app. This is the
4737ff550d0eSmasputra * reason why we pass NOINTR flag to strwrite()
4738ff550d0eSmasputra * for non-blocking socket, because we don't want
4739ff550d0eSmasputra * to return EAGAIN when portion of the user data
4740ff550d0eSmasputra * has actually been sent down.
4741ff550d0eSmasputra */
4742ff550d0eSmasputra return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4743ff550d0eSmasputra }
4744ff550d0eSmasputra }
4745ff550d0eSmasputra return (0);
4746ff550d0eSmasputra }
4747ff550d0eSmasputra
4748ff550d0eSmasputra /*
47490f1702c5SYu Xiangning * Update sti_faddr by asking the transport (unless AF_UNIX).
47507c478bd9Sstevel@tonic-gate */
47510f1702c5SYu Xiangning /* ARGSUSED */
47527c478bd9Sstevel@tonic-gate int
sotpi_getpeername(struct sonode * so,struct sockaddr * name,socklen_t * namelen,boolean_t accept,struct cred * cr)47530f1702c5SYu Xiangning sotpi_getpeername(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
47540f1702c5SYu Xiangning boolean_t accept, struct cred *cr)
47557c478bd9Sstevel@tonic-gate {
47567c478bd9Sstevel@tonic-gate struct strbuf strbuf;
47577c478bd9Sstevel@tonic-gate int error = 0, res;
47587c478bd9Sstevel@tonic-gate void *addr;
47597c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
47607c478bd9Sstevel@tonic-gate k_sigset_t smask;
47610f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
47627c478bd9Sstevel@tonic-gate
47637c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
4764903a11ebSrh87107 (void *)so, pr_state(so->so_state, so->so_mode)));
47657c478bd9Sstevel@tonic-gate
47660f1702c5SYu Xiangning ASSERT(*namelen > 0);
47677c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
47687c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
47690f1702c5SYu Xiangning
47700f1702c5SYu Xiangning if (accept) {
47710f1702c5SYu Xiangning bcopy(sti->sti_faddr_sa, name,
47720f1702c5SYu Xiangning MIN(*namelen, sti->sti_faddr_len));
47730f1702c5SYu Xiangning *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
47740f1702c5SYu Xiangning goto done;
47750f1702c5SYu Xiangning }
47760f1702c5SYu Xiangning
47777c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) {
47787c478bd9Sstevel@tonic-gate error = ENOTCONN;
47797c478bd9Sstevel@tonic-gate goto done;
47807c478bd9Sstevel@tonic-gate }
47817c478bd9Sstevel@tonic-gate /* Added this check for X/Open */
47827c478bd9Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
47837c478bd9Sstevel@tonic-gate error = EINVAL;
47847c478bd9Sstevel@tonic-gate if (xnet_check_print) {
47857c478bd9Sstevel@tonic-gate printf("sockfs: X/Open getpeername check => EINVAL\n");
47867c478bd9Sstevel@tonic-gate }
47877c478bd9Sstevel@tonic-gate goto done;
47887c478bd9Sstevel@tonic-gate }
47890f1702c5SYu Xiangning
47900f1702c5SYu Xiangning if (sti->sti_faddr_valid) {
47910f1702c5SYu Xiangning bcopy(sti->sti_faddr_sa, name,
47920f1702c5SYu Xiangning MIN(*namelen, sti->sti_faddr_len));
47930f1702c5SYu Xiangning *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
47940f1702c5SYu Xiangning goto done;
47950f1702c5SYu Xiangning }
47960f1702c5SYu Xiangning
47977c478bd9Sstevel@tonic-gate #ifdef DEBUG
47987c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
47990f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_faddr_sa,
48000f1702c5SYu Xiangning (t_uscalar_t)sti->sti_faddr_len)));
48017c478bd9Sstevel@tonic-gate #endif /* DEBUG */
48027c478bd9Sstevel@tonic-gate
48032caf0dcdSrshoaib if (so->so_family == AF_UNIX) {
48047c478bd9Sstevel@tonic-gate /* Transport has different name space - return local info */
48050f1702c5SYu Xiangning if (sti->sti_faddr_noxlate)
48060f1702c5SYu Xiangning *namelen = 0;
48077c478bd9Sstevel@tonic-gate error = 0;
48087c478bd9Sstevel@tonic-gate goto done;
48097c478bd9Sstevel@tonic-gate }
48107c478bd9Sstevel@tonic-gate
48110f1702c5SYu Xiangning ASSERT(so->so_family != AF_UNIX && sti->sti_faddr_noxlate == 0);
48120f1702c5SYu Xiangning
48130f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa);
48147c478bd9Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */
48150f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_maxlen;
48167c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
48177c478bd9Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP);
48187c478bd9Sstevel@tonic-gate
48197c478bd9Sstevel@tonic-gate /*
48207c478bd9Sstevel@tonic-gate * Issue TI_GETPEERNAME with signals masked.
48210f1702c5SYu Xiangning * Put the result in sti_faddr_sa so that getpeername works after
48227c478bd9Sstevel@tonic-gate * a shutdown(output).
48237c478bd9Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
48247c478bd9Sstevel@tonic-gate * back to the socket.
48257c478bd9Sstevel@tonic-gate */
48267c478bd9Sstevel@tonic-gate strbuf.buf = addr;
48277c478bd9Sstevel@tonic-gate strbuf.maxlen = addrlen;
48287c478bd9Sstevel@tonic-gate strbuf.len = 0;
48297c478bd9Sstevel@tonic-gate
48307c478bd9Sstevel@tonic-gate sigintr(&smask, 0);
48317c478bd9Sstevel@tonic-gate res = 0;
48320f1702c5SYu Xiangning ASSERT(cr);
48337c478bd9Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
48340f1702c5SYu Xiangning 0, K_TO_K, cr, &res);
48357c478bd9Sstevel@tonic-gate sigunintr(&smask);
48367c478bd9Sstevel@tonic-gate
48377c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
48387c478bd9Sstevel@tonic-gate /*
48397c478bd9Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail
48407c478bd9Sstevel@tonic-gate * the getpeername. Instead fallback on the recorded
48410f1702c5SYu Xiangning * sti->sti_faddr_sa.
48427c478bd9Sstevel@tonic-gate */
48437c478bd9Sstevel@tonic-gate if (error) {
48447c478bd9Sstevel@tonic-gate /*
48457c478bd9Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl.
48467c478bd9Sstevel@tonic-gate * However, it is impossible to determine which ones of
48477c478bd9Sstevel@tonic-gate * these are really socket level errors that were incorrectly
48487c478bd9Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the
48497c478bd9Sstevel@tonic-gate * error - to code explicitly does not reinstate the error
48507c478bd9Sstevel@tonic-gate * using soseterror().
48517c478bd9Sstevel@tonic-gate * Experiments have shows that at least this set of
48527c478bd9Sstevel@tonic-gate * errors are reported and should not be reinstated on the
48537c478bd9Sstevel@tonic-gate * socket:
48547c478bd9Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when
48557c478bd9Sstevel@tonic-gate * getpeername was called.
48567c478bd9Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write
48577c478bd9Sstevel@tonic-gate * side error over the read side error.
48587c478bd9Sstevel@tonic-gate * ENOTCONN The transport just got disconnected but
48597c478bd9Sstevel@tonic-gate * sockfs had not yet seen the T_DISCON_IND
48607c478bd9Sstevel@tonic-gate * when issuing the ioctl.
48617c478bd9Sstevel@tonic-gate */
48627c478bd9Sstevel@tonic-gate error = 0;
48637c478bd9Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 &&
48647c478bd9Sstevel@tonic-gate (so->so_state & SS_ISCONNECTED)) {
48650f1702c5SYu Xiangning ASSERT(strbuf.len <= (int)sti->sti_faddr_maxlen);
48660f1702c5SYu Xiangning sti->sti_faddr_len = (socklen_t)strbuf.len;
48670f1702c5SYu Xiangning bcopy(addr, sti->sti_faddr_sa, sti->sti_faddr_len);
48680f1702c5SYu Xiangning sti->sti_faddr_valid = 1;
48690f1702c5SYu Xiangning
48700f1702c5SYu Xiangning bcopy(addr, name, MIN(*namelen, sti->sti_faddr_len));
48710f1702c5SYu Xiangning *namelen = sti->sti_faddr_len;
48727c478bd9Sstevel@tonic-gate }
48737c478bd9Sstevel@tonic-gate kmem_free(addr, addrlen);
48747c478bd9Sstevel@tonic-gate #ifdef DEBUG
48757c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
48760f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_faddr_sa,
48770f1702c5SYu Xiangning (t_uscalar_t)sti->sti_faddr_len)));
48787c478bd9Sstevel@tonic-gate #endif /* DEBUG */
48797c478bd9Sstevel@tonic-gate done:
48807c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
48817c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
48827c478bd9Sstevel@tonic-gate return (error);
48837c478bd9Sstevel@tonic-gate }
48847c478bd9Sstevel@tonic-gate
48857c478bd9Sstevel@tonic-gate /*
48860f1702c5SYu Xiangning * Update sti_laddr by asking the transport (unless AF_UNIX).
48877c478bd9Sstevel@tonic-gate */
48887c478bd9Sstevel@tonic-gate int
sotpi_getsockname(struct sonode * so,struct sockaddr * name,socklen_t * namelen,struct cred * cr)48890f1702c5SYu Xiangning sotpi_getsockname(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
48900f1702c5SYu Xiangning struct cred *cr)
48917c478bd9Sstevel@tonic-gate {
48927c478bd9Sstevel@tonic-gate struct strbuf strbuf;
48937c478bd9Sstevel@tonic-gate int error = 0, res;
48947c478bd9Sstevel@tonic-gate void *addr;
48957c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
48967c478bd9Sstevel@tonic-gate k_sigset_t smask;
48970f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
48987c478bd9Sstevel@tonic-gate
48997c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
4900903a11ebSrh87107 (void *)so, pr_state(so->so_state, so->so_mode)));
49017c478bd9Sstevel@tonic-gate
49020f1702c5SYu Xiangning ASSERT(*namelen > 0);
49037c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
49047c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
49050f1702c5SYu Xiangning
49067c478bd9Sstevel@tonic-gate #ifdef DEBUG
49070f1702c5SYu Xiangning
49087c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
49090f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_laddr_sa,
49100f1702c5SYu Xiangning (t_uscalar_t)sti->sti_laddr_len)));
49117c478bd9Sstevel@tonic-gate #endif /* DEBUG */
49120f1702c5SYu Xiangning if (sti->sti_laddr_valid) {
49130f1702c5SYu Xiangning bcopy(sti->sti_laddr_sa, name,
49140f1702c5SYu Xiangning MIN(*namelen, sti->sti_laddr_len));
49150f1702c5SYu Xiangning *namelen = sti->sti_laddr_len;
49160f1702c5SYu Xiangning goto done;
49170f1702c5SYu Xiangning }
49180f1702c5SYu Xiangning
49197c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
4920dfc0fed8SRobert Mustacchi /*
4921dfc0fed8SRobert Mustacchi * Transport has different name space - return local info. If we
4922dfc0fed8SRobert Mustacchi * have enough space, let consumers know the family.
4923dfc0fed8SRobert Mustacchi */
4924dfc0fed8SRobert Mustacchi if (*namelen >= sizeof (sa_family_t)) {
4925dfc0fed8SRobert Mustacchi name->sa_family = AF_UNIX;
4926dfc0fed8SRobert Mustacchi *namelen = sizeof (sa_family_t);
4927dfc0fed8SRobert Mustacchi } else {
4928a5adac4dSYu Xiangning *namelen = 0;
4929dfc0fed8SRobert Mustacchi }
4930dfc0fed8SRobert Mustacchi error = 0;
49317c478bd9Sstevel@tonic-gate goto done;
49327c478bd9Sstevel@tonic-gate }
493318cbc865Sblu if (!(so->so_state & SS_ISBOUND)) {
493418cbc865Sblu /* If not bound, then nothing to return. */
493518cbc865Sblu error = 0;
493618cbc865Sblu goto done;
493718cbc865Sblu }
49380f1702c5SYu Xiangning
49397c478bd9Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */
49400f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_laddr_maxlen;
49417c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
49427c478bd9Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP);
49437c478bd9Sstevel@tonic-gate
49447c478bd9Sstevel@tonic-gate /*
49457c478bd9Sstevel@tonic-gate * Issue TI_GETMYNAME with signals masked.
49460f1702c5SYu Xiangning * Put the result in sti_laddr_sa so that getsockname works after
49477c478bd9Sstevel@tonic-gate * a shutdown(output).
49487c478bd9Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
49497c478bd9Sstevel@tonic-gate * back to the socket.
49507c478bd9Sstevel@tonic-gate */
49517c478bd9Sstevel@tonic-gate strbuf.buf = addr;
49527c478bd9Sstevel@tonic-gate strbuf.maxlen = addrlen;
49537c478bd9Sstevel@tonic-gate strbuf.len = 0;
49547c478bd9Sstevel@tonic-gate
49557c478bd9Sstevel@tonic-gate sigintr(&smask, 0);
49567c478bd9Sstevel@tonic-gate res = 0;
49570f1702c5SYu Xiangning ASSERT(cr);
49587c478bd9Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
49590f1702c5SYu Xiangning 0, K_TO_K, cr, &res);
49607c478bd9Sstevel@tonic-gate sigunintr(&smask);
49617c478bd9Sstevel@tonic-gate
49627c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
49637c478bd9Sstevel@tonic-gate /*
49647c478bd9Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail
49657c478bd9Sstevel@tonic-gate * the getsockname. Instead fallback on the recorded
49660f1702c5SYu Xiangning * sti->sti_laddr_sa.
49677c478bd9Sstevel@tonic-gate */
49687c478bd9Sstevel@tonic-gate if (error) {
49697c478bd9Sstevel@tonic-gate /*
49707c478bd9Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl.
49717c478bd9Sstevel@tonic-gate * However, it is impossible to determine which ones of
49727c478bd9Sstevel@tonic-gate * these are really socket level errors that were incorrectly
49737c478bd9Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the
49747c478bd9Sstevel@tonic-gate * error - to code explicitly does not reinstate the error
49757c478bd9Sstevel@tonic-gate * using soseterror().
49767c478bd9Sstevel@tonic-gate * Experiments have shows that at least this set of
49777c478bd9Sstevel@tonic-gate * errors are reported and should not be reinstated on the
49787c478bd9Sstevel@tonic-gate * socket:
49797c478bd9Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when
49807c478bd9Sstevel@tonic-gate * getsockname was called.
49817c478bd9Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write
49827c478bd9Sstevel@tonic-gate * side error over the read side error.
49837c478bd9Sstevel@tonic-gate */
49847c478bd9Sstevel@tonic-gate error = 0;
49857c478bd9Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 &&
49867c478bd9Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) {
49870f1702c5SYu Xiangning ASSERT(strbuf.len <= (int)sti->sti_laddr_maxlen);
49880f1702c5SYu Xiangning sti->sti_laddr_len = (socklen_t)strbuf.len;
49890f1702c5SYu Xiangning bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
49900f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
49910f1702c5SYu Xiangning
49920f1702c5SYu Xiangning bcopy(addr, name, MIN(sti->sti_laddr_len, *namelen));
49930f1702c5SYu Xiangning *namelen = sti->sti_laddr_len;
49947c478bd9Sstevel@tonic-gate }
49957c478bd9Sstevel@tonic-gate kmem_free(addr, addrlen);
49967c478bd9Sstevel@tonic-gate #ifdef DEBUG
49977c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
49980f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_laddr_sa,
49990f1702c5SYu Xiangning (t_uscalar_t)sti->sti_laddr_len)));
50007c478bd9Sstevel@tonic-gate #endif /* DEBUG */
50017c478bd9Sstevel@tonic-gate done:
50027c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
50037c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
50047c478bd9Sstevel@tonic-gate return (error);
50057c478bd9Sstevel@tonic-gate }
50067c478bd9Sstevel@tonic-gate
50077c478bd9Sstevel@tonic-gate /*
50087c478bd9Sstevel@tonic-gate * Get socket options. For SOL_SOCKET options some options are handled
50097c478bd9Sstevel@tonic-gate * by the sockfs while others use the value recorded in the sonode as a
50107c478bd9Sstevel@tonic-gate * fallback should the T_SVR4_OPTMGMT_REQ fail.
50117c478bd9Sstevel@tonic-gate *
50127c478bd9Sstevel@tonic-gate * On the return most *optlenp bytes are copied to optval.
50137c478bd9Sstevel@tonic-gate */
50140f1702c5SYu Xiangning /* ARGSUSED */
50157c478bd9Sstevel@tonic-gate int
sotpi_getsockopt(struct sonode * so,int level,int option_name,void * optval,socklen_t * optlenp,int flags,struct cred * cr)50167c478bd9Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name,
50170f1702c5SYu Xiangning void *optval, socklen_t *optlenp, int flags, struct cred *cr)
50187c478bd9Sstevel@tonic-gate {
50197c478bd9Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req;
50207c478bd9Sstevel@tonic-gate struct T_optmgmt_ack *optmgmt_ack;
50217c478bd9Sstevel@tonic-gate struct opthdr oh;
50227c478bd9Sstevel@tonic-gate struct opthdr *opt_res;
50237c478bd9Sstevel@tonic-gate mblk_t *mp = NULL;
50247c478bd9Sstevel@tonic-gate int error = 0;
50257c478bd9Sstevel@tonic-gate void *option = NULL; /* Set if fallback value */
50267c478bd9Sstevel@tonic-gate t_uscalar_t maxlen = *optlenp;
50277c478bd9Sstevel@tonic-gate t_uscalar_t len;
50287c478bd9Sstevel@tonic-gate uint32_t value;
50290f1702c5SYu Xiangning struct timeval tmo_val; /* used for SO_RCVTIMEO, SO_SNDTIMEO */
503022238f73Sshenjian struct timeval32 tmo_val32;
50310f1702c5SYu Xiangning struct so_snd_bufinfo snd_bufinfo; /* used for zero copy */
50327c478bd9Sstevel@tonic-gate
50337c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
5034903a11ebSrh87107 (void *)so, level, option_name, optval, (void *)optlenp,
50357c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
50367c478bd9Sstevel@tonic-gate
50377c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
50387c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
50397c478bd9Sstevel@tonic-gate
5040369bcc48SToomas Soome len = (t_uscalar_t)sizeof (uint32_t); /* Default */
5041369bcc48SToomas Soome
50427c478bd9Sstevel@tonic-gate /*
50437c478bd9Sstevel@tonic-gate * Check for SOL_SOCKET options.
50447c478bd9Sstevel@tonic-gate * Certain SOL_SOCKET options are returned directly whereas
50457c478bd9Sstevel@tonic-gate * others only provide a default (fallback) value should
50467c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ fail.
50477c478bd9Sstevel@tonic-gate */
50487c478bd9Sstevel@tonic-gate if (level == SOL_SOCKET) {
50497c478bd9Sstevel@tonic-gate /* Check parameters */
50507c478bd9Sstevel@tonic-gate switch (option_name) {
50517c478bd9Sstevel@tonic-gate case SO_TYPE:
50527c478bd9Sstevel@tonic-gate case SO_ERROR:
50537c478bd9Sstevel@tonic-gate case SO_DEBUG:
50547c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
50557c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
50567c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
50577c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
50587c478bd9Sstevel@tonic-gate case SO_BROADCAST:
50597c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
50607c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
50617c478bd9Sstevel@tonic-gate case SO_SNDBUF:
50627c478bd9Sstevel@tonic-gate case SO_RCVBUF:
50637c478bd9Sstevel@tonic-gate #ifdef notyet
50647c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
50657c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
50667c478bd9Sstevel@tonic-gate #endif /* notyet */
506788cda078Skcpoon case SO_DOMAIN:
50687c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
50697c478bd9Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
50707c478bd9Sstevel@tonic-gate error = EINVAL;
50717c478bd9Sstevel@tonic-gate eprintsoline(so, error);
50727c478bd9Sstevel@tonic-gate goto done2;
50737c478bd9Sstevel@tonic-gate }
50747c478bd9Sstevel@tonic-gate break;
50750f1702c5SYu Xiangning case SO_RCVTIMEO:
50760f1702c5SYu Xiangning case SO_SNDTIMEO:
5077e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5078e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
507922238f73Sshenjian if (maxlen < sizeof (struct timeval)) {
50800f1702c5SYu Xiangning error = EINVAL;
50810f1702c5SYu Xiangning eprintsoline(so, error);
50820f1702c5SYu Xiangning goto done2;
50830f1702c5SYu Xiangning }
508422238f73Sshenjian } else {
508522238f73Sshenjian if (maxlen < sizeof (struct timeval32)) {
508622238f73Sshenjian error = EINVAL;
508722238f73Sshenjian eprintsoline(so, error);
508822238f73Sshenjian goto done2;
508922238f73Sshenjian }
509022238f73Sshenjian
509122238f73Sshenjian }
50920f1702c5SYu Xiangning break;
50937c478bd9Sstevel@tonic-gate case SO_LINGER:
50947c478bd9Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
50957c478bd9Sstevel@tonic-gate error = EINVAL;
50967c478bd9Sstevel@tonic-gate eprintsoline(so, error);
50977c478bd9Sstevel@tonic-gate goto done2;
50987c478bd9Sstevel@tonic-gate }
50997c478bd9Sstevel@tonic-gate break;
51000f1702c5SYu Xiangning case SO_SND_BUFINFO:
51010f1702c5SYu Xiangning if (maxlen < (t_uscalar_t)
51020f1702c5SYu Xiangning sizeof (struct so_snd_bufinfo)) {
51030f1702c5SYu Xiangning error = EINVAL;
51040f1702c5SYu Xiangning eprintsoline(so, error);
51050f1702c5SYu Xiangning goto done2;
51060f1702c5SYu Xiangning }
51070f1702c5SYu Xiangning break;
51087c478bd9Sstevel@tonic-gate }
51097c478bd9Sstevel@tonic-gate
51107c478bd9Sstevel@tonic-gate switch (option_name) {
51117c478bd9Sstevel@tonic-gate case SO_TYPE:
51127c478bd9Sstevel@tonic-gate value = so->so_type;
51137c478bd9Sstevel@tonic-gate option = &value;
51147c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51157c478bd9Sstevel@tonic-gate
51167c478bd9Sstevel@tonic-gate case SO_ERROR:
51170f1702c5SYu Xiangning value = sogeterr(so, B_TRUE);
51187c478bd9Sstevel@tonic-gate option = &value;
51197c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51207c478bd9Sstevel@tonic-gate
51217c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
51227c478bd9Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN)
51237c478bd9Sstevel@tonic-gate value = SO_ACCEPTCONN;
51247c478bd9Sstevel@tonic-gate else
51257c478bd9Sstevel@tonic-gate value = 0;
51267c478bd9Sstevel@tonic-gate #ifdef DEBUG
51277c478bd9Sstevel@tonic-gate if (value) {
51287c478bd9Sstevel@tonic-gate dprintso(so, 1,
51297c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n",
51307c478bd9Sstevel@tonic-gate option_name));
51317c478bd9Sstevel@tonic-gate } else {
51327c478bd9Sstevel@tonic-gate dprintso(so, 1,
51337c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n",
51347c478bd9Sstevel@tonic-gate option_name));
51357c478bd9Sstevel@tonic-gate }
51367c478bd9Sstevel@tonic-gate #endif /* DEBUG */
51377c478bd9Sstevel@tonic-gate option = &value;
51387c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51397c478bd9Sstevel@tonic-gate
51407c478bd9Sstevel@tonic-gate case SO_DEBUG:
51417c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
51427c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
51437c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
51447c478bd9Sstevel@tonic-gate case SO_BROADCAST:
51457c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
51467c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
51477c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
51487c478bd9Sstevel@tonic-gate value = (so->so_options & option_name);
51497c478bd9Sstevel@tonic-gate #ifdef DEBUG
51507c478bd9Sstevel@tonic-gate if (value) {
51517c478bd9Sstevel@tonic-gate dprintso(so, 1,
51527c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n",
51537c478bd9Sstevel@tonic-gate option_name));
51547c478bd9Sstevel@tonic-gate } else {
51557c478bd9Sstevel@tonic-gate dprintso(so, 1,
51567c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n",
51577c478bd9Sstevel@tonic-gate option_name));
51587c478bd9Sstevel@tonic-gate }
51597c478bd9Sstevel@tonic-gate #endif /* DEBUG */
51607c478bd9Sstevel@tonic-gate option = &value;
51617c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51627c478bd9Sstevel@tonic-gate
51637c478bd9Sstevel@tonic-gate /*
51647c478bd9Sstevel@tonic-gate * The following options are only returned by sockfs when the
51657c478bd9Sstevel@tonic-gate * T_SVR4_OPTMGMT_REQ fails.
51667c478bd9Sstevel@tonic-gate */
51677c478bd9Sstevel@tonic-gate case SO_LINGER:
51687c478bd9Sstevel@tonic-gate option = &so->so_linger;
51697c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (struct linger);
51707c478bd9Sstevel@tonic-gate break;
51717c478bd9Sstevel@tonic-gate case SO_SNDBUF: {
51727c478bd9Sstevel@tonic-gate ssize_t lvalue;
51737c478bd9Sstevel@tonic-gate
51747c478bd9Sstevel@tonic-gate /*
51757c478bd9Sstevel@tonic-gate * If the option has not been set then get a default
51767c478bd9Sstevel@tonic-gate * value from the read queue. This value is
51777c478bd9Sstevel@tonic-gate * returned if the transport fails
51787c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ.
51797c478bd9Sstevel@tonic-gate */
51807c478bd9Sstevel@tonic-gate lvalue = so->so_sndbuf;
51817c478bd9Sstevel@tonic-gate if (lvalue == 0) {
51827c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
51837c478bd9Sstevel@tonic-gate (void) strqget(strvp2wq(SOTOV(so))->q_next,
51847c478bd9Sstevel@tonic-gate QHIWAT, 0, &lvalue);
51857c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
51867c478bd9Sstevel@tonic-gate dprintso(so, 1,
51877c478bd9Sstevel@tonic-gate ("got SO_SNDBUF %ld from q\n", lvalue));
51887c478bd9Sstevel@tonic-gate }
51897c478bd9Sstevel@tonic-gate value = (int)lvalue;
51907c478bd9Sstevel@tonic-gate option = &value;
51917c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_sndbuf);
51927c478bd9Sstevel@tonic-gate break;
51937c478bd9Sstevel@tonic-gate }
51947c478bd9Sstevel@tonic-gate case SO_RCVBUF: {
51957c478bd9Sstevel@tonic-gate ssize_t lvalue;
51967c478bd9Sstevel@tonic-gate
51977c478bd9Sstevel@tonic-gate /*
51987c478bd9Sstevel@tonic-gate * If the option has not been set then get a default
51997c478bd9Sstevel@tonic-gate * value from the read queue. This value is
52007c478bd9Sstevel@tonic-gate * returned if the transport fails
52017c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ.
52027c478bd9Sstevel@tonic-gate *
52037c478bd9Sstevel@tonic-gate * XXX If SO_RCVBUF has been set and this is an
52047c478bd9Sstevel@tonic-gate * XPG 4.2 application then do not ask the transport
52057c478bd9Sstevel@tonic-gate * since the transport might adjust the value and not
52067c478bd9Sstevel@tonic-gate * return exactly what was set by the application.
52077c478bd9Sstevel@tonic-gate * For non-XPG 4.2 application we return the value
52087c478bd9Sstevel@tonic-gate * that the transport is actually using.
52097c478bd9Sstevel@tonic-gate */
52107c478bd9Sstevel@tonic-gate lvalue = so->so_rcvbuf;
52117c478bd9Sstevel@tonic-gate if (lvalue == 0) {
52127c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
52137c478bd9Sstevel@tonic-gate (void) strqget(RD(strvp2wq(SOTOV(so))),
52147c478bd9Sstevel@tonic-gate QHIWAT, 0, &lvalue);
52157c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
52167c478bd9Sstevel@tonic-gate dprintso(so, 1,
52177c478bd9Sstevel@tonic-gate ("got SO_RCVBUF %ld from q\n", lvalue));
52187c478bd9Sstevel@tonic-gate } else if (flags & _SOGETSOCKOPT_XPG4_2) {
52197c478bd9Sstevel@tonic-gate value = (int)lvalue;
52207c478bd9Sstevel@tonic-gate option = &value;
52217c478bd9Sstevel@tonic-gate goto copyout; /* skip asking transport */
52227c478bd9Sstevel@tonic-gate }
52237c478bd9Sstevel@tonic-gate value = (int)lvalue;
52247c478bd9Sstevel@tonic-gate option = &value;
52257c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_rcvbuf);
52267c478bd9Sstevel@tonic-gate break;
52277c478bd9Sstevel@tonic-gate }
522888cda078Skcpoon case SO_DOMAIN:
522988cda078Skcpoon value = so->so_family;
523088cda078Skcpoon option = &value;
523188cda078Skcpoon goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
523288cda078Skcpoon
52337c478bd9Sstevel@tonic-gate #ifdef notyet
52347c478bd9Sstevel@tonic-gate /*
52357c478bd9Sstevel@tonic-gate * We do not implement the semantics of these options
52367c478bd9Sstevel@tonic-gate * thus we shouldn't implement the options either.
52377c478bd9Sstevel@tonic-gate */
52387c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
52397c478bd9Sstevel@tonic-gate value = so->so_sndlowat;
52407c478bd9Sstevel@tonic-gate option = &value;
52417c478bd9Sstevel@tonic-gate break;
52427c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
52437c478bd9Sstevel@tonic-gate value = so->so_rcvlowat;
52447c478bd9Sstevel@tonic-gate option = &value;
52457c478bd9Sstevel@tonic-gate break;
52467c478bd9Sstevel@tonic-gate #endif /* notyet */
52470f1702c5SYu Xiangning case SO_SNDTIMEO:
52480f1702c5SYu Xiangning case SO_RCVTIMEO: {
52490f1702c5SYu Xiangning clock_t val;
525022238f73Sshenjian
52510f1702c5SYu Xiangning if (option_name == SO_RCVTIMEO)
52520f1702c5SYu Xiangning val = drv_hztousec(so->so_rcvtimeo);
52530f1702c5SYu Xiangning else
52540f1702c5SYu Xiangning val = drv_hztousec(so->so_sndtimeo);
52550f1702c5SYu Xiangning tmo_val.tv_sec = val / (1000 * 1000);
52560f1702c5SYu Xiangning tmo_val.tv_usec = val % (1000 * 1000);
5257e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5258e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
52590f1702c5SYu Xiangning option = &tmo_val;
526022238f73Sshenjian len = sizeof (struct timeval);
526122238f73Sshenjian } else {
526222238f73Sshenjian TIMEVAL_TO_TIMEVAL32(&tmo_val32, &tmo_val);
526322238f73Sshenjian option = &tmo_val32;
526422238f73Sshenjian len = sizeof (struct timeval32);
526522238f73Sshenjian }
52660f1702c5SYu Xiangning break;
52670f1702c5SYu Xiangning }
52680f1702c5SYu Xiangning case SO_SND_BUFINFO: {
52690f1702c5SYu Xiangning snd_bufinfo.sbi_wroff =
52700f1702c5SYu Xiangning (so->so_proto_props).sopp_wroff;
52710f1702c5SYu Xiangning snd_bufinfo.sbi_maxblk =
52720f1702c5SYu Xiangning (so->so_proto_props).sopp_maxblk;
52730f1702c5SYu Xiangning snd_bufinfo.sbi_maxpsz =
52740f1702c5SYu Xiangning (so->so_proto_props).sopp_maxpsz;
52750f1702c5SYu Xiangning snd_bufinfo.sbi_tail =
52760f1702c5SYu Xiangning (so->so_proto_props).sopp_tail;
52770f1702c5SYu Xiangning option = &snd_bufinfo;
52780f1702c5SYu Xiangning len = (t_uscalar_t)sizeof (struct so_snd_bufinfo);
52790f1702c5SYu Xiangning break;
52800f1702c5SYu Xiangning }
52817c478bd9Sstevel@tonic-gate }
52827c478bd9Sstevel@tonic-gate }
52837c478bd9Sstevel@tonic-gate
52847c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
52857c478bd9Sstevel@tonic-gate
52867c478bd9Sstevel@tonic-gate /* Send request */
52877c478bd9Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
52887c478bd9Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_CHECK;
52897c478bd9Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
52907c478bd9Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
52917c478bd9Sstevel@tonic-gate
52927c478bd9Sstevel@tonic-gate oh.level = level;
52937c478bd9Sstevel@tonic-gate oh.name = option_name;
52947c478bd9Sstevel@tonic-gate oh.len = maxlen;
52957c478bd9Sstevel@tonic-gate
52967c478bd9Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5297de8c4a14SErik Nordmark &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP, cr);
52987c478bd9Sstevel@tonic-gate /* Let option management work in the presence of data flow control */
52997c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
53007c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
53017c478bd9Sstevel@tonic-gate mp = NULL;
53027c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
53037c478bd9Sstevel@tonic-gate if (error) {
53047c478bd9Sstevel@tonic-gate eprintsoline(so, error);
53057c478bd9Sstevel@tonic-gate goto done2;
53067c478bd9Sstevel@tonic-gate }
53077c478bd9Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
53087c478bd9Sstevel@tonic-gate (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
53097c478bd9Sstevel@tonic-gate if (error) {
53107c478bd9Sstevel@tonic-gate if (option != NULL) {
53117c478bd9Sstevel@tonic-gate /* We have a fallback value */
53127c478bd9Sstevel@tonic-gate error = 0;
53137c478bd9Sstevel@tonic-gate goto copyout;
53147c478bd9Sstevel@tonic-gate }
53157c478bd9Sstevel@tonic-gate eprintsoline(so, error);
53167c478bd9Sstevel@tonic-gate goto done2;
53177c478bd9Sstevel@tonic-gate }
53187c478bd9Sstevel@tonic-gate ASSERT(mp);
53197c478bd9Sstevel@tonic-gate optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
53207c478bd9Sstevel@tonic-gate opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
53217c478bd9Sstevel@tonic-gate optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
53227c478bd9Sstevel@tonic-gate if (opt_res == NULL) {
53237c478bd9Sstevel@tonic-gate if (option != NULL) {
53247c478bd9Sstevel@tonic-gate /* We have a fallback value */
53257c478bd9Sstevel@tonic-gate error = 0;
53267c478bd9Sstevel@tonic-gate goto copyout;
53277c478bd9Sstevel@tonic-gate }
53287c478bd9Sstevel@tonic-gate error = EPROTO;
53297c478bd9Sstevel@tonic-gate eprintsoline(so, error);
53307c478bd9Sstevel@tonic-gate goto done;
53317c478bd9Sstevel@tonic-gate }
53327c478bd9Sstevel@tonic-gate option = &opt_res[1];
53337c478bd9Sstevel@tonic-gate
53347c478bd9Sstevel@tonic-gate /* check to ensure that the option is within bounds */
53357c478bd9Sstevel@tonic-gate if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
53367c478bd9Sstevel@tonic-gate (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
53377c478bd9Sstevel@tonic-gate if (option != NULL) {
53387c478bd9Sstevel@tonic-gate /* We have a fallback value */
53397c478bd9Sstevel@tonic-gate error = 0;
53407c478bd9Sstevel@tonic-gate goto copyout;
53417c478bd9Sstevel@tonic-gate }
53427c478bd9Sstevel@tonic-gate error = EPROTO;
53437c478bd9Sstevel@tonic-gate eprintsoline(so, error);
53447c478bd9Sstevel@tonic-gate goto done;
53457c478bd9Sstevel@tonic-gate }
53467c478bd9Sstevel@tonic-gate
53477c478bd9Sstevel@tonic-gate len = opt_res->len;
53487c478bd9Sstevel@tonic-gate
53497c478bd9Sstevel@tonic-gate copyout: {
53507c478bd9Sstevel@tonic-gate t_uscalar_t size = MIN(len, maxlen);
53517c478bd9Sstevel@tonic-gate bcopy(option, optval, size);
53527c478bd9Sstevel@tonic-gate bcopy(&size, optlenp, sizeof (size));
53537c478bd9Sstevel@tonic-gate }
53547c478bd9Sstevel@tonic-gate done:
53557c478bd9Sstevel@tonic-gate freemsg(mp);
53567c478bd9Sstevel@tonic-gate done2:
53577c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
53587c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
53590f1702c5SYu Xiangning
53607c478bd9Sstevel@tonic-gate return (error);
53617c478bd9Sstevel@tonic-gate }
53627c478bd9Sstevel@tonic-gate
53637c478bd9Sstevel@tonic-gate /*
53647c478bd9Sstevel@tonic-gate * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
53657c478bd9Sstevel@tonic-gate * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
53667c478bd9Sstevel@tonic-gate * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
53677c478bd9Sstevel@tonic-gate * setsockopt has to work even if the transport does not support the option.
53687c478bd9Sstevel@tonic-gate */
53690f1702c5SYu Xiangning /* ARGSUSED */
53707c478bd9Sstevel@tonic-gate int
sotpi_setsockopt(struct sonode * so,int level,int option_name,const void * optval,t_uscalar_t optlen,struct cred * cr)53717c478bd9Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name,
53720f1702c5SYu Xiangning const void *optval, t_uscalar_t optlen, struct cred *cr)
53737c478bd9Sstevel@tonic-gate {
53747c478bd9Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req;
53757c478bd9Sstevel@tonic-gate struct opthdr oh;
53767c478bd9Sstevel@tonic-gate mblk_t *mp;
53777c478bd9Sstevel@tonic-gate int error = 0;
53787c478bd9Sstevel@tonic-gate boolean_t handled = B_FALSE;
53797c478bd9Sstevel@tonic-gate
53807c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
5381903a11ebSrh87107 (void *)so, level, option_name, optval, optlen,
53827c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
53837c478bd9Sstevel@tonic-gate
53847c478bd9Sstevel@tonic-gate /* X/Open requires this check */
53857c478bd9Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
53867c478bd9Sstevel@tonic-gate if (xnet_check_print)
53877c478bd9Sstevel@tonic-gate printf("sockfs: X/Open setsockopt check => EINVAL\n");
53887c478bd9Sstevel@tonic-gate return (EINVAL);
53897c478bd9Sstevel@tonic-gate }
53907c478bd9Sstevel@tonic-gate
53917c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
53927c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
53937c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
53947c478bd9Sstevel@tonic-gate
53957c478bd9Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
53967c478bd9Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_NEGOTIATE;
53977c478bd9Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
53987c478bd9Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
53997c478bd9Sstevel@tonic-gate
54007c478bd9Sstevel@tonic-gate oh.level = level;
54017c478bd9Sstevel@tonic-gate oh.name = option_name;
54027c478bd9Sstevel@tonic-gate oh.len = optlen;
54037c478bd9Sstevel@tonic-gate
54047c478bd9Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5405de8c4a14SErik Nordmark &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP, cr);
54067c478bd9Sstevel@tonic-gate /* Let option management work in the presence of data flow control */
54077c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
54087c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
54097c478bd9Sstevel@tonic-gate mp = NULL;
54107c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
54117c478bd9Sstevel@tonic-gate if (error) {
54127c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54130f1702c5SYu Xiangning goto done2;
54147c478bd9Sstevel@tonic-gate }
54157c478bd9Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
54167c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
54177c478bd9Sstevel@tonic-gate if (error) {
54187c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54197c478bd9Sstevel@tonic-gate goto done;
54207c478bd9Sstevel@tonic-gate }
54217c478bd9Sstevel@tonic-gate ASSERT(mp);
54227c478bd9Sstevel@tonic-gate /* No need to verify T_optmgmt_ack */
54237c478bd9Sstevel@tonic-gate freemsg(mp);
54247c478bd9Sstevel@tonic-gate done:
54257c478bd9Sstevel@tonic-gate /*
54267c478bd9Sstevel@tonic-gate * Check for SOL_SOCKET options and record their values.
54277c478bd9Sstevel@tonic-gate * If we know about a SOL_SOCKET parameter and the transport
54287c478bd9Sstevel@tonic-gate * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
54297c478bd9Sstevel@tonic-gate * EPROTO) we let the setsockopt succeed.
54307c478bd9Sstevel@tonic-gate */
54317c478bd9Sstevel@tonic-gate if (level == SOL_SOCKET) {
54327c478bd9Sstevel@tonic-gate /* Check parameters */
54337c478bd9Sstevel@tonic-gate switch (option_name) {
54347c478bd9Sstevel@tonic-gate case SO_DEBUG:
54357c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
54367c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
54377c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
54387c478bd9Sstevel@tonic-gate case SO_BROADCAST:
54397c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
54407c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
54417c478bd9Sstevel@tonic-gate case SO_SNDBUF:
54427c478bd9Sstevel@tonic-gate case SO_RCVBUF:
54437c478bd9Sstevel@tonic-gate #ifdef notyet
54447c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
54457c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
54467c478bd9Sstevel@tonic-gate #endif /* notyet */
54477c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
54487c478bd9Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) {
54497c478bd9Sstevel@tonic-gate error = EINVAL;
54507c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54517c478bd9Sstevel@tonic-gate goto done2;
54527c478bd9Sstevel@tonic-gate }
54537c478bd9Sstevel@tonic-gate ASSERT(optval);
54547c478bd9Sstevel@tonic-gate handled = B_TRUE;
54557c478bd9Sstevel@tonic-gate break;
54560f1702c5SYu Xiangning case SO_SNDTIMEO:
54570f1702c5SYu Xiangning case SO_RCVTIMEO:
5458e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5459e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
546022238f73Sshenjian if (optlen != sizeof (struct timeval)) {
54610f1702c5SYu Xiangning error = EINVAL;
54620f1702c5SYu Xiangning eprintsoline(so, error);
54630f1702c5SYu Xiangning goto done2;
54640f1702c5SYu Xiangning }
546522238f73Sshenjian } else {
546622238f73Sshenjian if (optlen != sizeof (struct timeval32)) {
546722238f73Sshenjian error = EINVAL;
546822238f73Sshenjian eprintsoline(so, error);
546922238f73Sshenjian goto done2;
547022238f73Sshenjian }
547122238f73Sshenjian }
54720f1702c5SYu Xiangning ASSERT(optval);
54730f1702c5SYu Xiangning handled = B_TRUE;
54740f1702c5SYu Xiangning break;
54757c478bd9Sstevel@tonic-gate case SO_LINGER:
54767c478bd9Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (struct linger)) {
54777c478bd9Sstevel@tonic-gate error = EINVAL;
54787c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54797c478bd9Sstevel@tonic-gate goto done2;
54807c478bd9Sstevel@tonic-gate }
54817c478bd9Sstevel@tonic-gate ASSERT(optval);
54827c478bd9Sstevel@tonic-gate handled = B_TRUE;
54837c478bd9Sstevel@tonic-gate break;
54847c478bd9Sstevel@tonic-gate }
54857c478bd9Sstevel@tonic-gate
54867c478bd9Sstevel@tonic-gate #define intvalue (*(int32_t *)optval)
54877c478bd9Sstevel@tonic-gate
54887c478bd9Sstevel@tonic-gate switch (option_name) {
54897c478bd9Sstevel@tonic-gate case SO_TYPE:
54907c478bd9Sstevel@tonic-gate case SO_ERROR:
54917c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
54927c478bd9Sstevel@tonic-gate /* Can't be set */
54937c478bd9Sstevel@tonic-gate error = ENOPROTOOPT;
54947c478bd9Sstevel@tonic-gate goto done2;
54957c478bd9Sstevel@tonic-gate case SO_LINGER: {
54967c478bd9Sstevel@tonic-gate struct linger *l = (struct linger *)optval;
54977c478bd9Sstevel@tonic-gate
54987c478bd9Sstevel@tonic-gate so->so_linger.l_linger = l->l_linger;
54997c478bd9Sstevel@tonic-gate if (l->l_onoff) {
55007c478bd9Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER;
55017c478bd9Sstevel@tonic-gate so->so_options |= SO_LINGER;
55027c478bd9Sstevel@tonic-gate } else {
55037c478bd9Sstevel@tonic-gate so->so_linger.l_onoff = 0;
55047c478bd9Sstevel@tonic-gate so->so_options &= ~SO_LINGER;
55057c478bd9Sstevel@tonic-gate }
55067c478bd9Sstevel@tonic-gate break;
55077c478bd9Sstevel@tonic-gate }
55087c478bd9Sstevel@tonic-gate
55097c478bd9Sstevel@tonic-gate case SO_DEBUG:
55107c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
55117c478bd9Sstevel@tonic-gate if (intvalue & 2)
55127c478bd9Sstevel@tonic-gate sock_test_timelimit = 10 * hz;
55137c478bd9Sstevel@tonic-gate else
55147c478bd9Sstevel@tonic-gate sock_test_timelimit = 0;
55157c478bd9Sstevel@tonic-gate
55167c478bd9Sstevel@tonic-gate if (intvalue & 4)
55177c478bd9Sstevel@tonic-gate do_useracc = 0;
55187c478bd9Sstevel@tonic-gate else
55197c478bd9Sstevel@tonic-gate do_useracc = 1;
55207c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
55217c478bd9Sstevel@tonic-gate /* FALLTHRU */
55227c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
55237c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
55247c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
55257c478bd9Sstevel@tonic-gate case SO_BROADCAST:
55267c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
55277c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
55287c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
55297c478bd9Sstevel@tonic-gate if (intvalue != 0) {
55307c478bd9Sstevel@tonic-gate dprintso(so, 1,
55310f1702c5SYu Xiangning ("socket_setsockopt: setting 0x%x\n",
55327c478bd9Sstevel@tonic-gate option_name));
55337c478bd9Sstevel@tonic-gate so->so_options |= option_name;
55347c478bd9Sstevel@tonic-gate } else {
55357c478bd9Sstevel@tonic-gate dprintso(so, 1,
55360f1702c5SYu Xiangning ("socket_setsockopt: clearing 0x%x\n",
55377c478bd9Sstevel@tonic-gate option_name));
55387c478bd9Sstevel@tonic-gate so->so_options &= ~option_name;
55397c478bd9Sstevel@tonic-gate }
55407c478bd9Sstevel@tonic-gate break;
55417c478bd9Sstevel@tonic-gate /*
55427c478bd9Sstevel@tonic-gate * The following options are only returned by us when the
55430f1702c5SYu Xiangning * transport layer fails.
55447c478bd9Sstevel@tonic-gate * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
55457c478bd9Sstevel@tonic-gate * since the transport might adjust the value and not
55467c478bd9Sstevel@tonic-gate * return exactly what was set by the application.
55477c478bd9Sstevel@tonic-gate */
55487c478bd9Sstevel@tonic-gate case SO_SNDBUF:
55497c478bd9Sstevel@tonic-gate so->so_sndbuf = intvalue;
55507c478bd9Sstevel@tonic-gate break;
55517c478bd9Sstevel@tonic-gate case SO_RCVBUF:
55527c478bd9Sstevel@tonic-gate so->so_rcvbuf = intvalue;
55537c478bd9Sstevel@tonic-gate break;
55540f1702c5SYu Xiangning case SO_RCVPSH:
55550f1702c5SYu Xiangning so->so_rcv_timer_interval = intvalue;
55560f1702c5SYu Xiangning break;
55577c478bd9Sstevel@tonic-gate #ifdef notyet
55587c478bd9Sstevel@tonic-gate /*
55597c478bd9Sstevel@tonic-gate * We do not implement the semantics of these options
55607c478bd9Sstevel@tonic-gate * thus we shouldn't implement the options either.
55617c478bd9Sstevel@tonic-gate */
55627c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
55637c478bd9Sstevel@tonic-gate so->so_sndlowat = intvalue;
55647c478bd9Sstevel@tonic-gate break;
55657c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
55667c478bd9Sstevel@tonic-gate so->so_rcvlowat = intvalue;
55677c478bd9Sstevel@tonic-gate break;
55687c478bd9Sstevel@tonic-gate #endif /* notyet */
55690f1702c5SYu Xiangning case SO_SNDTIMEO:
55700f1702c5SYu Xiangning case SO_RCVTIMEO: {
557122238f73Sshenjian struct timeval tl;
557222238f73Sshenjian clock_t val;
557322238f73Sshenjian
5574e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5575e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE)
557622238f73Sshenjian bcopy(&tl, (struct timeval *)optval,
557722238f73Sshenjian sizeof (struct timeval));
557822238f73Sshenjian else
557922238f73Sshenjian TIMEVAL32_TO_TIMEVAL(&tl,
558022238f73Sshenjian (struct timeval32 *)optval);
558122238f73Sshenjian val = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
55820f1702c5SYu Xiangning if (option_name == SO_RCVTIMEO)
55830f1702c5SYu Xiangning so->so_rcvtimeo = drv_usectohz(val);
55840f1702c5SYu Xiangning else
55850f1702c5SYu Xiangning so->so_sndtimeo = drv_usectohz(val);
55860f1702c5SYu Xiangning break;
55870f1702c5SYu Xiangning }
55887c478bd9Sstevel@tonic-gate }
55897c478bd9Sstevel@tonic-gate #undef intvalue
55907c478bd9Sstevel@tonic-gate
55917c478bd9Sstevel@tonic-gate if (error) {
55927c478bd9Sstevel@tonic-gate if ((error == ENOPROTOOPT || error == EPROTO ||
55937c478bd9Sstevel@tonic-gate error == EINVAL) && handled) {
55947c478bd9Sstevel@tonic-gate dprintso(so, 1,
55957c478bd9Sstevel@tonic-gate ("setsockopt: ignoring error %d for 0x%x\n",
55967c478bd9Sstevel@tonic-gate error, option_name));
55977c478bd9Sstevel@tonic-gate error = 0;
55987c478bd9Sstevel@tonic-gate }
55997c478bd9Sstevel@tonic-gate }
56007c478bd9Sstevel@tonic-gate }
56017c478bd9Sstevel@tonic-gate done2:
56027c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
56037c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
56047c478bd9Sstevel@tonic-gate return (error);
56057c478bd9Sstevel@tonic-gate }
56060f1702c5SYu Xiangning
5607f0267584Sanders /*
5608f0267584Sanders * sotpi_close() is called when the last open reference goes away.
5609f0267584Sanders */
56100f1702c5SYu Xiangning /* ARGSUSED */
56110f1702c5SYu Xiangning int
sotpi_close(struct sonode * so,int flag,struct cred * cr)56120f1702c5SYu Xiangning sotpi_close(struct sonode *so, int flag, struct cred *cr)
56130f1702c5SYu Xiangning {
56140f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
56150f1702c5SYu Xiangning dev_t dev;
56160f1702c5SYu Xiangning int error = 0;
56170f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
56180f1702c5SYu Xiangning
56190f1702c5SYu Xiangning dprintso(so, 1, ("sotpi_close(%p, %x) %s\n",
56200f1702c5SYu Xiangning (void *)vp, flag, pr_state(so->so_state, so->so_mode)));
56210f1702c5SYu Xiangning
56220f1702c5SYu Xiangning dev = sti->sti_dev;
56230f1702c5SYu Xiangning
56240f1702c5SYu Xiangning ASSERT(STREAMSTAB(getmajor(dev)));
56250f1702c5SYu Xiangning
56260f1702c5SYu Xiangning mutex_enter(&so->so_lock);
56270f1702c5SYu Xiangning so_lock_single(so); /* Set SOLOCKED */
56280f1702c5SYu Xiangning
5629d36be52eSRao Shoaib ASSERT(so_verify_oobstate(so));
5630d36be52eSRao Shoaib
56310f1702c5SYu Xiangning if (vp->v_stream != NULL) {
56320f1702c5SYu Xiangning vnode_t *ux_vp;
56330f1702c5SYu Xiangning
56340f1702c5SYu Xiangning if (so->so_family == AF_UNIX) {
56350f1702c5SYu Xiangning /* Could avoid this when CANTSENDMORE for !dgram */
56360f1702c5SYu Xiangning so_unix_close(so);
56370f1702c5SYu Xiangning }
56380f1702c5SYu Xiangning
56390f1702c5SYu Xiangning mutex_exit(&so->so_lock);
56400f1702c5SYu Xiangning /*
56410f1702c5SYu Xiangning * Disassemble the linkage from the AF_UNIX underlying file
56420f1702c5SYu Xiangning * system vnode to this socket (by atomically clearing
56430f1702c5SYu Xiangning * v_stream in vn_rele_stream) before strclose clears sd_vnode
56440f1702c5SYu Xiangning * and frees the stream head.
56450f1702c5SYu Xiangning */
56460f1702c5SYu Xiangning if ((ux_vp = sti->sti_ux_bound_vp) != NULL) {
56470f1702c5SYu Xiangning ASSERT(ux_vp->v_stream);
56480f1702c5SYu Xiangning sti->sti_ux_bound_vp = NULL;
56490f1702c5SYu Xiangning vn_rele_stream(ux_vp);
56500f1702c5SYu Xiangning }
56510f1702c5SYu Xiangning error = strclose(vp, flag, cr);
56520f1702c5SYu Xiangning vp->v_stream = NULL;
56530f1702c5SYu Xiangning mutex_enter(&so->so_lock);
56540f1702c5SYu Xiangning }
56550f1702c5SYu Xiangning
56560f1702c5SYu Xiangning /*
56570f1702c5SYu Xiangning * Flush the T_DISCON_IND on sti_discon_ind_mp.
56580f1702c5SYu Xiangning */
56590f1702c5SYu Xiangning so_flush_discon_ind(so);
56600f1702c5SYu Xiangning
56610f1702c5SYu Xiangning so_unlock_single(so, SOLOCKED);
56620f1702c5SYu Xiangning mutex_exit(&so->so_lock);
56630f1702c5SYu Xiangning
56640f1702c5SYu Xiangning /*
56650f1702c5SYu Xiangning * Needed for STREAMs.
56660f1702c5SYu Xiangning * Decrement the device driver's reference count for streams
56670f1702c5SYu Xiangning * opened via the clone dip. The driver was held in clone_open().
56680f1702c5SYu Xiangning * The absence of clone_close() forces this asymmetry.
56690f1702c5SYu Xiangning */
56700f1702c5SYu Xiangning if (so->so_flag & SOCLONE)
56710f1702c5SYu Xiangning ddi_rele_driver(getmajor(dev));
56720f1702c5SYu Xiangning
56730f1702c5SYu Xiangning return (error);
56740f1702c5SYu Xiangning }
56750f1702c5SYu Xiangning
56760f1702c5SYu Xiangning static int
sotpi_ioctl(struct sonode * so,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)56770f1702c5SYu Xiangning sotpi_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
56780f1702c5SYu Xiangning struct cred *cr, int32_t *rvalp)
56790f1702c5SYu Xiangning {
56800f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
56810f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
56820f1702c5SYu Xiangning int error = 0;
56830f1702c5SYu Xiangning
56840f1702c5SYu Xiangning dprintso(so, 0, ("sotpi_ioctl: cmd 0x%x, arg 0x%lx, state %s\n",
56850f1702c5SYu Xiangning cmd, arg, pr_state(so->so_state, so->so_mode)));
56860f1702c5SYu Xiangning
56870f1702c5SYu Xiangning switch (cmd) {
5688bfcb55b8SRao Shoaib case SIOCSQPTR:
5689bfcb55b8SRao Shoaib /*
5690bfcb55b8SRao Shoaib * SIOCSQPTR is valid only when helper stream is created
5691bfcb55b8SRao Shoaib * by the protocol.
5692bfcb55b8SRao Shoaib */
56930f1702c5SYu Xiangning case _I_INSERT:
56940f1702c5SYu Xiangning case _I_REMOVE:
56950f1702c5SYu Xiangning /*
56960f1702c5SYu Xiangning * Since there's no compelling reason to support these ioctls
56970f1702c5SYu Xiangning * on sockets, and doing so would increase the complexity
56980f1702c5SYu Xiangning * markedly, prevent it.
56990f1702c5SYu Xiangning */
57000f1702c5SYu Xiangning return (EOPNOTSUPP);
57010f1702c5SYu Xiangning
57020f1702c5SYu Xiangning case I_FIND:
57030f1702c5SYu Xiangning case I_LIST:
57040f1702c5SYu Xiangning case I_LOOK:
57050f1702c5SYu Xiangning case I_POP:
57060f1702c5SYu Xiangning case I_PUSH:
57070f1702c5SYu Xiangning /*
57080f1702c5SYu Xiangning * To prevent races and inconsistencies between the actual
57090f1702c5SYu Xiangning * state of the stream and the state according to the sonode,
57100f1702c5SYu Xiangning * we serialize all operations which modify or operate on the
57110f1702c5SYu Xiangning * list of modules on the socket's stream.
57120f1702c5SYu Xiangning */
57130f1702c5SYu Xiangning mutex_enter(&sti->sti_plumb_lock);
57140f1702c5SYu Xiangning error = socktpi_plumbioctl(vp, cmd, arg, mode, cr, rvalp);
57150f1702c5SYu Xiangning mutex_exit(&sti->sti_plumb_lock);
57160f1702c5SYu Xiangning return (error);
57170f1702c5SYu Xiangning
57180f1702c5SYu Xiangning default:
57190f1702c5SYu Xiangning if (so->so_version != SOV_STREAM)
57200f1702c5SYu Xiangning break;
57210f1702c5SYu Xiangning
57220f1702c5SYu Xiangning /*
57230f1702c5SYu Xiangning * The imaginary "sockmod" has been popped; act as a stream.
57240f1702c5SYu Xiangning */
57250f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
57260f1702c5SYu Xiangning }
57270f1702c5SYu Xiangning
57280f1702c5SYu Xiangning ASSERT(so->so_version != SOV_STREAM);
57290f1702c5SYu Xiangning
57300f1702c5SYu Xiangning /*
57310f1702c5SYu Xiangning * Process socket-specific ioctls.
57320f1702c5SYu Xiangning */
57330f1702c5SYu Xiangning switch (cmd) {
57340f1702c5SYu Xiangning case FIONBIO: {
57350f1702c5SYu Xiangning int32_t value;
57360f1702c5SYu Xiangning
57370f1702c5SYu Xiangning if (so_copyin((void *)arg, &value, sizeof (int32_t),
57380f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
57390f1702c5SYu Xiangning return (EFAULT);
57400f1702c5SYu Xiangning
57410f1702c5SYu Xiangning mutex_enter(&so->so_lock);
57420f1702c5SYu Xiangning if (value) {
57430f1702c5SYu Xiangning so->so_state |= SS_NDELAY;
57440f1702c5SYu Xiangning } else {
57450f1702c5SYu Xiangning so->so_state &= ~SS_NDELAY;
57460f1702c5SYu Xiangning }
57470f1702c5SYu Xiangning mutex_exit(&so->so_lock);
57480f1702c5SYu Xiangning return (0);
57490f1702c5SYu Xiangning }
57500f1702c5SYu Xiangning
57510f1702c5SYu Xiangning case FIOASYNC: {
57520f1702c5SYu Xiangning int32_t value;
57530f1702c5SYu Xiangning
57540f1702c5SYu Xiangning if (so_copyin((void *)arg, &value, sizeof (int32_t),
57550f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
57560f1702c5SYu Xiangning return (EFAULT);
57570f1702c5SYu Xiangning
57580f1702c5SYu Xiangning mutex_enter(&so->so_lock);
57590f1702c5SYu Xiangning /*
57600f1702c5SYu Xiangning * SS_ASYNC flag not already set correctly?
57610f1702c5SYu Xiangning * (!value != !(so->so_state & SS_ASYNC))
57620f1702c5SYu Xiangning * but some engineers find that too hard to read.
57630f1702c5SYu Xiangning */
576434793d0fSToomas Soome if ((value == 0 && (so->so_state & SS_ASYNC) != 0) ||
576534793d0fSToomas Soome (value != 0 && (so->so_state & SS_ASYNC) == 0))
57660f1702c5SYu Xiangning error = so_flip_async(so, vp, mode, cr);
57670f1702c5SYu Xiangning mutex_exit(&so->so_lock);
57680f1702c5SYu Xiangning return (error);
57690f1702c5SYu Xiangning }
57700f1702c5SYu Xiangning
57710f1702c5SYu Xiangning case SIOCSPGRP:
57720f1702c5SYu Xiangning case FIOSETOWN: {
57730f1702c5SYu Xiangning pid_t pgrp;
57740f1702c5SYu Xiangning
57750f1702c5SYu Xiangning if (so_copyin((void *)arg, &pgrp, sizeof (pid_t),
57760f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
57770f1702c5SYu Xiangning return (EFAULT);
57780f1702c5SYu Xiangning
57790f1702c5SYu Xiangning mutex_enter(&so->so_lock);
57800f1702c5SYu Xiangning dprintso(so, 1, ("setown: new %d old %d\n", pgrp, so->so_pgrp));
57810f1702c5SYu Xiangning /* Any change? */
57820f1702c5SYu Xiangning if (pgrp != so->so_pgrp)
57830f1702c5SYu Xiangning error = so_set_siggrp(so, vp, pgrp, mode, cr);
57840f1702c5SYu Xiangning mutex_exit(&so->so_lock);
57850f1702c5SYu Xiangning return (error);
57860f1702c5SYu Xiangning }
57870f1702c5SYu Xiangning case SIOCGPGRP:
57880f1702c5SYu Xiangning case FIOGETOWN:
57890f1702c5SYu Xiangning if (so_copyout(&so->so_pgrp, (void *)arg,
57900f1702c5SYu Xiangning sizeof (pid_t), (mode & (int)FKIOCTL)))
57910f1702c5SYu Xiangning return (EFAULT);
57920f1702c5SYu Xiangning return (0);
57930f1702c5SYu Xiangning
57940f1702c5SYu Xiangning case SIOCATMARK: {
57950f1702c5SYu Xiangning int retval;
57960f1702c5SYu Xiangning uint_t so_state;
57970f1702c5SYu Xiangning
57980f1702c5SYu Xiangning /*
57990f1702c5SYu Xiangning * strwaitmark has a finite timeout after which it
58000f1702c5SYu Xiangning * returns -1 if the mark state is undetermined.
58010f1702c5SYu Xiangning * In order to avoid any race between the mark state
58020f1702c5SYu Xiangning * in sockfs and the mark state in the stream head this
58030f1702c5SYu Xiangning * routine loops until the mark state can be determined
58040f1702c5SYu Xiangning * (or the urgent data indication has been removed by some
58050f1702c5SYu Xiangning * other thread).
58060f1702c5SYu Xiangning */
58070f1702c5SYu Xiangning do {
58080f1702c5SYu Xiangning mutex_enter(&so->so_lock);
58090f1702c5SYu Xiangning so_state = so->so_state;
58100f1702c5SYu Xiangning mutex_exit(&so->so_lock);
58110f1702c5SYu Xiangning if (so_state & SS_RCVATMARK) {
58120f1702c5SYu Xiangning retval = 1;
58130f1702c5SYu Xiangning } else if (!(so_state & SS_OOBPEND)) {
58140f1702c5SYu Xiangning /*
58150f1702c5SYu Xiangning * No SIGURG has been generated -- there is no
58160f1702c5SYu Xiangning * pending or present urgent data. Thus can't
58170f1702c5SYu Xiangning * possibly be at the mark.
58180f1702c5SYu Xiangning */
58190f1702c5SYu Xiangning retval = 0;
58200f1702c5SYu Xiangning } else {
58210f1702c5SYu Xiangning /*
58220f1702c5SYu Xiangning * Have the stream head wait until there is
58230f1702c5SYu Xiangning * either some messages on the read queue, or
58240f1702c5SYu Xiangning * STRATMARK or STRNOTATMARK gets set. The
58250f1702c5SYu Xiangning * STRNOTATMARK flag is used so that the
58260f1702c5SYu Xiangning * transport can send up a MSGNOTMARKNEXT
58270f1702c5SYu Xiangning * M_DATA to indicate that it is not
58280f1702c5SYu Xiangning * at the mark and additional data is not about
58290f1702c5SYu Xiangning * to be send upstream.
58300f1702c5SYu Xiangning *
58310f1702c5SYu Xiangning * If the mark state is undetermined this will
58320f1702c5SYu Xiangning * return -1 and we will loop rechecking the
58330f1702c5SYu Xiangning * socket state.
58340f1702c5SYu Xiangning */
58350f1702c5SYu Xiangning retval = strwaitmark(vp);
58360f1702c5SYu Xiangning }
58370f1702c5SYu Xiangning } while (retval == -1);
58380f1702c5SYu Xiangning
58390f1702c5SYu Xiangning if (so_copyout(&retval, (void *)arg, sizeof (int),
58400f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
58410f1702c5SYu Xiangning return (EFAULT);
58420f1702c5SYu Xiangning return (0);
58430f1702c5SYu Xiangning }
58440f1702c5SYu Xiangning
58450f1702c5SYu Xiangning case I_FDINSERT:
58460f1702c5SYu Xiangning case I_SENDFD:
58470f1702c5SYu Xiangning case I_RECVFD:
58480f1702c5SYu Xiangning case I_ATMARK:
58490f1702c5SYu Xiangning case _SIOCSOCKFALLBACK:
58500f1702c5SYu Xiangning /*
58510f1702c5SYu Xiangning * These ioctls do not apply to sockets. I_FDINSERT can be
58520f1702c5SYu Xiangning * used to send M_PROTO messages without modifying the socket
58530f1702c5SYu Xiangning * state. I_SENDFD/RECVFD should not be used for socket file
58540f1702c5SYu Xiangning * descriptor passing since they assume a twisted stream.
58550f1702c5SYu Xiangning * SIOCATMARK must be used instead of I_ATMARK.
58560f1702c5SYu Xiangning *
58570f1702c5SYu Xiangning * _SIOCSOCKFALLBACK from an application should never be
58580f1702c5SYu Xiangning * processed. It is only generated by socktpi_open() or
58590f1702c5SYu Xiangning * in response to I_POP or I_PUSH.
58600f1702c5SYu Xiangning */
58610f1702c5SYu Xiangning #ifdef DEBUG
58620f1702c5SYu Xiangning zcmn_err(getzoneid(), CE_WARN,
58630f1702c5SYu Xiangning "Unsupported STREAMS ioctl 0x%x on socket. "
58640f1702c5SYu Xiangning "Pid = %d\n", cmd, curproc->p_pid);
58650f1702c5SYu Xiangning #endif /* DEBUG */
58660f1702c5SYu Xiangning return (EOPNOTSUPP);
58670f1702c5SYu Xiangning
58680f1702c5SYu Xiangning case _I_GETPEERCRED:
58690f1702c5SYu Xiangning if ((mode & FKIOCTL) == 0)
58700f1702c5SYu Xiangning return (EINVAL);
58710f1702c5SYu Xiangning
58720f1702c5SYu Xiangning mutex_enter(&so->so_lock);
58730f1702c5SYu Xiangning if ((so->so_mode & SM_CONNREQUIRED) == 0) {
58740f1702c5SYu Xiangning error = ENOTSUP;
58750f1702c5SYu Xiangning } else if ((so->so_state & SS_ISCONNECTED) == 0) {
58760f1702c5SYu Xiangning error = ENOTCONN;
58770f1702c5SYu Xiangning } else if (so->so_peercred != NULL) {
58780f1702c5SYu Xiangning k_peercred_t *kp = (k_peercred_t *)arg;
58790f1702c5SYu Xiangning kp->pc_cr = so->so_peercred;
58800f1702c5SYu Xiangning kp->pc_cpid = so->so_cpid;
58810f1702c5SYu Xiangning crhold(so->so_peercred);
58820f1702c5SYu Xiangning } else {
58830f1702c5SYu Xiangning error = EINVAL;
58840f1702c5SYu Xiangning }
58850f1702c5SYu Xiangning mutex_exit(&so->so_lock);
58860f1702c5SYu Xiangning return (error);
58870f1702c5SYu Xiangning
58880f1702c5SYu Xiangning default:
58890f1702c5SYu Xiangning /*
58900f1702c5SYu Xiangning * Do the higher-order bits of the ioctl cmd indicate
58910f1702c5SYu Xiangning * that it is an I_* streams ioctl?
58920f1702c5SYu Xiangning */
58930f1702c5SYu Xiangning if ((cmd & 0xffffff00U) == STR &&
58940f1702c5SYu Xiangning so->so_version == SOV_SOCKBSD) {
58950f1702c5SYu Xiangning #ifdef DEBUG
58960f1702c5SYu Xiangning zcmn_err(getzoneid(), CE_WARN,
58970f1702c5SYu Xiangning "Unsupported STREAMS ioctl 0x%x on socket. "
58980f1702c5SYu Xiangning "Pid = %d\n", cmd, curproc->p_pid);
58990f1702c5SYu Xiangning #endif /* DEBUG */
59000f1702c5SYu Xiangning return (EOPNOTSUPP);
59010f1702c5SYu Xiangning }
59020f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
59030f1702c5SYu Xiangning }
59040f1702c5SYu Xiangning }
59050f1702c5SYu Xiangning
59060f1702c5SYu Xiangning /*
59070f1702c5SYu Xiangning * Handle plumbing-related ioctls.
59080f1702c5SYu Xiangning */
59090f1702c5SYu Xiangning static int
socktpi_plumbioctl(struct vnode * vp,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)59100f1702c5SYu Xiangning socktpi_plumbioctl(struct vnode *vp, int cmd, intptr_t arg, int mode,
59110f1702c5SYu Xiangning struct cred *cr, int32_t *rvalp)
59120f1702c5SYu Xiangning {
59130f1702c5SYu Xiangning static const char sockmod_name[] = "sockmod";
59140f1702c5SYu Xiangning struct sonode *so = VTOSO(vp);
59150f1702c5SYu Xiangning char mname[FMNAMESZ + 1];
59160f1702c5SYu Xiangning int error;
59170f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
59180f1702c5SYu Xiangning
59190f1702c5SYu Xiangning ASSERT(MUTEX_HELD(&sti->sti_plumb_lock));
59200f1702c5SYu Xiangning
59210f1702c5SYu Xiangning if (so->so_version == SOV_SOCKBSD)
59220f1702c5SYu Xiangning return (EOPNOTSUPP);
59230f1702c5SYu Xiangning
59240f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
59250f1702c5SYu Xiangning /*
59260f1702c5SYu Xiangning * The imaginary "sockmod" has been popped - act as a stream.
59270f1702c5SYu Xiangning * If this is a push of sockmod then change back to a socket.
59280f1702c5SYu Xiangning */
59290f1702c5SYu Xiangning if (cmd == I_PUSH) {
59300f1702c5SYu Xiangning error = ((mode & FKIOCTL) ? copystr : copyinstr)(
59310f1702c5SYu Xiangning (void *)arg, mname, sizeof (mname), NULL);
59320f1702c5SYu Xiangning
59330f1702c5SYu Xiangning if (error == 0 && strcmp(mname, sockmod_name) == 0) {
59340f1702c5SYu Xiangning dprintso(so, 0, ("socktpi_ioctl: going to "
59350f1702c5SYu Xiangning "socket version\n"));
59360f1702c5SYu Xiangning so_stream2sock(so);
59370f1702c5SYu Xiangning return (0);
59380f1702c5SYu Xiangning }
59390f1702c5SYu Xiangning }
59400f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
59410f1702c5SYu Xiangning }
59420f1702c5SYu Xiangning
59430f1702c5SYu Xiangning switch (cmd) {
59440f1702c5SYu Xiangning case I_PUSH:
59450f1702c5SYu Xiangning if (sti->sti_direct) {
59460f1702c5SYu Xiangning mutex_enter(&so->so_lock);
59470f1702c5SYu Xiangning so_lock_single(so);
59480f1702c5SYu Xiangning mutex_exit(&so->so_lock);
59490f1702c5SYu Xiangning
59500f1702c5SYu Xiangning error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
5951de8c4a14SErik Nordmark cr, rvalp);
59520f1702c5SYu Xiangning
59530f1702c5SYu Xiangning mutex_enter(&so->so_lock);
59540f1702c5SYu Xiangning if (error == 0)
59550f1702c5SYu Xiangning sti->sti_direct = 0;
59560f1702c5SYu Xiangning so_unlock_single(so, SOLOCKED);
59570f1702c5SYu Xiangning mutex_exit(&so->so_lock);
59580f1702c5SYu Xiangning
59590f1702c5SYu Xiangning if (error != 0)
59600f1702c5SYu Xiangning return (error);
59610f1702c5SYu Xiangning }
59620f1702c5SYu Xiangning
59630f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
59640f1702c5SYu Xiangning if (error == 0)
59650f1702c5SYu Xiangning sti->sti_pushcnt++;
59660f1702c5SYu Xiangning return (error);
59670f1702c5SYu Xiangning
59680f1702c5SYu Xiangning case I_POP:
59690f1702c5SYu Xiangning if (sti->sti_pushcnt == 0) {
59700f1702c5SYu Xiangning /* Emulate sockmod being popped */
59710f1702c5SYu Xiangning dprintso(so, 0,
59720f1702c5SYu Xiangning ("socktpi_ioctl: going to STREAMS version\n"));
59730f1702c5SYu Xiangning return (so_sock2stream(so));
59740f1702c5SYu Xiangning }
59750f1702c5SYu Xiangning
59760f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
59770f1702c5SYu Xiangning if (error == 0)
59780f1702c5SYu Xiangning sti->sti_pushcnt--;
59790f1702c5SYu Xiangning return (error);
59800f1702c5SYu Xiangning
59810f1702c5SYu Xiangning case I_LIST: {
59820f1702c5SYu Xiangning struct str_mlist *kmlistp, *umlistp;
59830f1702c5SYu Xiangning struct str_list kstrlist;
59840f1702c5SYu Xiangning ssize_t kstrlistsize;
59850f1702c5SYu Xiangning int i, nmods;
59860f1702c5SYu Xiangning
59870f1702c5SYu Xiangning STRUCT_DECL(str_list, ustrlist);
59880f1702c5SYu Xiangning STRUCT_INIT(ustrlist, mode);
59890f1702c5SYu Xiangning
5990e9f74ea5SToomas Soome if (arg == 0) {
59910f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
59920f1702c5SYu Xiangning if (error == 0)
59930f1702c5SYu Xiangning (*rvalp)++; /* Add one for sockmod */
59940f1702c5SYu Xiangning return (error);
59950f1702c5SYu Xiangning }
59960f1702c5SYu Xiangning
59970f1702c5SYu Xiangning error = so_copyin((void *)arg, STRUCT_BUF(ustrlist),
59980f1702c5SYu Xiangning STRUCT_SIZE(ustrlist), mode & FKIOCTL);
59990f1702c5SYu Xiangning if (error != 0)
60000f1702c5SYu Xiangning return (error);
60010f1702c5SYu Xiangning
60020f1702c5SYu Xiangning nmods = STRUCT_FGET(ustrlist, sl_nmods);
60030f1702c5SYu Xiangning if (nmods <= 0)
60040f1702c5SYu Xiangning return (EINVAL);
60050f1702c5SYu Xiangning /*
60060f1702c5SYu Xiangning * Ceiling nmods at nstrpush to prevent someone from
60070f1702c5SYu Xiangning * maliciously consuming lots of kernel memory.
60080f1702c5SYu Xiangning */
60090f1702c5SYu Xiangning nmods = MIN(nmods, nstrpush);
60100f1702c5SYu Xiangning
60110f1702c5SYu Xiangning kstrlistsize = (nmods + 1) * sizeof (struct str_mlist);
60120f1702c5SYu Xiangning kstrlist.sl_nmods = nmods;
60130f1702c5SYu Xiangning kstrlist.sl_modlist = kmem_zalloc(kstrlistsize, KM_SLEEP);
60140f1702c5SYu Xiangning
60150f1702c5SYu Xiangning error = strioctl(vp, cmd, (intptr_t)&kstrlist, mode, K_TO_K,
60160f1702c5SYu Xiangning cr, rvalp);
60170f1702c5SYu Xiangning if (error != 0)
60180f1702c5SYu Xiangning goto done;
60190f1702c5SYu Xiangning
60200f1702c5SYu Xiangning /*
60210f1702c5SYu Xiangning * Considering the module list as a 0-based array of sl_nmods
60220f1702c5SYu Xiangning * modules, sockmod should conceptually exist at slot
60230f1702c5SYu Xiangning * sti_pushcnt. Insert sockmod at this location by sliding all
60240f1702c5SYu Xiangning * of the module names after so_pushcnt over by one. We know
60250f1702c5SYu Xiangning * that there will be room to do this since we allocated
60260f1702c5SYu Xiangning * sl_modlist with an additional slot.
60270f1702c5SYu Xiangning */
60280f1702c5SYu Xiangning for (i = kstrlist.sl_nmods; i > sti->sti_pushcnt; i--)
60290f1702c5SYu Xiangning kstrlist.sl_modlist[i] = kstrlist.sl_modlist[i - 1];
60300f1702c5SYu Xiangning
60310f1702c5SYu Xiangning (void) strcpy(kstrlist.sl_modlist[i].l_name, sockmod_name);
60320f1702c5SYu Xiangning kstrlist.sl_nmods++;
60330f1702c5SYu Xiangning
60340f1702c5SYu Xiangning /*
60350f1702c5SYu Xiangning * Copy all of the entries out to ustrlist.
60360f1702c5SYu Xiangning */
60370f1702c5SYu Xiangning kmlistp = kstrlist.sl_modlist;
60380f1702c5SYu Xiangning umlistp = STRUCT_FGETP(ustrlist, sl_modlist);
60390f1702c5SYu Xiangning for (i = 0; i < nmods && i < kstrlist.sl_nmods; i++) {
60400f1702c5SYu Xiangning error = so_copyout(kmlistp++, umlistp++,
60410f1702c5SYu Xiangning sizeof (struct str_mlist), mode & FKIOCTL);
60420f1702c5SYu Xiangning if (error != 0)
60430f1702c5SYu Xiangning goto done;
60440f1702c5SYu Xiangning }
60450f1702c5SYu Xiangning
60460f1702c5SYu Xiangning error = so_copyout(&i, (void *)arg, sizeof (int32_t),
60470f1702c5SYu Xiangning mode & FKIOCTL);
60480f1702c5SYu Xiangning if (error == 0)
60490f1702c5SYu Xiangning *rvalp = 0;
60500f1702c5SYu Xiangning done:
60510f1702c5SYu Xiangning kmem_free(kstrlist.sl_modlist, kstrlistsize);
60520f1702c5SYu Xiangning return (error);
60530f1702c5SYu Xiangning }
60540f1702c5SYu Xiangning case I_LOOK:
60550f1702c5SYu Xiangning if (sti->sti_pushcnt == 0) {
60560f1702c5SYu Xiangning return (so_copyout(sockmod_name, (void *)arg,
60570f1702c5SYu Xiangning sizeof (sockmod_name), mode & FKIOCTL));
60580f1702c5SYu Xiangning }
60590f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
60600f1702c5SYu Xiangning
60610f1702c5SYu Xiangning case I_FIND:
60620f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60630f1702c5SYu Xiangning if (error && error != EINVAL)
60640f1702c5SYu Xiangning return (error);
60650f1702c5SYu Xiangning
60660f1702c5SYu Xiangning /* if not found and string was sockmod return 1 */
60670f1702c5SYu Xiangning if (*rvalp == 0 || error == EINVAL) {
60680f1702c5SYu Xiangning error = ((mode & FKIOCTL) ? copystr : copyinstr)(
60690f1702c5SYu Xiangning (void *)arg, mname, sizeof (mname), NULL);
60700f1702c5SYu Xiangning if (error == ENAMETOOLONG)
60710f1702c5SYu Xiangning error = EINVAL;
60720f1702c5SYu Xiangning
60730f1702c5SYu Xiangning if (error == 0 && strcmp(mname, sockmod_name) == 0)
60740f1702c5SYu Xiangning *rvalp = 1;
60750f1702c5SYu Xiangning }
60760f1702c5SYu Xiangning return (error);
60770f1702c5SYu Xiangning
60780f1702c5SYu Xiangning default:
60790f1702c5SYu Xiangning panic("socktpi_plumbioctl: unknown ioctl %d", cmd);
60800f1702c5SYu Xiangning break;
60810f1702c5SYu Xiangning }
60820f1702c5SYu Xiangning
60830f1702c5SYu Xiangning return (0);
60840f1702c5SYu Xiangning }
60850f1702c5SYu Xiangning
60860f1702c5SYu Xiangning /*
60870f1702c5SYu Xiangning * Wrapper around the streams poll routine that implements socket poll
60880f1702c5SYu Xiangning * semantics.
60890f1702c5SYu Xiangning * The sockfs never calls pollwakeup itself - the stream head take care
60900f1702c5SYu Xiangning * of all pollwakeups. Since sockfs never holds so_lock when calling the
60910f1702c5SYu Xiangning * stream head there can never be a deadlock due to holding so_lock across
60920f1702c5SYu Xiangning * pollwakeup and acquiring so_lock in this routine.
60930f1702c5SYu Xiangning *
60940f1702c5SYu Xiangning * However, since the performance of VOP_POLL is critical we avoid
60950f1702c5SYu Xiangning * acquiring so_lock here. This is based on two assumptions:
60960f1702c5SYu Xiangning * - The poll implementation holds locks to serialize the VOP_POLL call
60970f1702c5SYu Xiangning * and a pollwakeup for the same pollhead. This ensures that should
60980f1702c5SYu Xiangning * e.g. so_state change during a socktpi_poll call the pollwakeup
60990f1702c5SYu Xiangning * (which strsock_* and strrput conspire to issue) is issued after
61000f1702c5SYu Xiangning * the state change. Thus the pollwakeup will block until VOP_POLL has
61010f1702c5SYu Xiangning * returned and then wake up poll and have it call VOP_POLL again.
61020f1702c5SYu Xiangning * - The reading of so_state without holding so_lock does not result in
61030f1702c5SYu Xiangning * stale data that is older than the latest state change that has dropped
61040f1702c5SYu Xiangning * so_lock. This is ensured by the mutex_exit issuing the appropriate
61050f1702c5SYu Xiangning * memory barrier to force the data into the coherency domain.
61060f1702c5SYu Xiangning */
61070f1702c5SYu Xiangning static int
sotpi_poll(struct sonode * so,short events,int anyyet,short * reventsp,struct pollhead ** phpp)61080f1702c5SYu Xiangning sotpi_poll(
61090f1702c5SYu Xiangning struct sonode *so,
61100f1702c5SYu Xiangning short events,
61110f1702c5SYu Xiangning int anyyet,
61120f1702c5SYu Xiangning short *reventsp,
61130f1702c5SYu Xiangning struct pollhead **phpp)
61140f1702c5SYu Xiangning {
61150f1702c5SYu Xiangning short origevents = events;
61160f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
61170f1702c5SYu Xiangning int error;
61180f1702c5SYu Xiangning int so_state = so->so_state; /* snapshot */
61190f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
61200f1702c5SYu Xiangning
61210f1702c5SYu Xiangning dprintso(so, 0, ("socktpi_poll(%p): state %s err %d\n",
61220f1702c5SYu Xiangning (void *)vp, pr_state(so_state, so->so_mode), so->so_error));
61230f1702c5SYu Xiangning
61240f1702c5SYu Xiangning ASSERT(vp->v_type == VSOCK);
61250f1702c5SYu Xiangning ASSERT(vp->v_stream != NULL);
61260f1702c5SYu Xiangning
61270f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
61280f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
61290f1702c5SYu Xiangning return (strpoll(vp->v_stream, events, anyyet,
61300f1702c5SYu Xiangning reventsp, phpp));
61310f1702c5SYu Xiangning }
61320f1702c5SYu Xiangning
61330f1702c5SYu Xiangning if (!(so_state & SS_ISCONNECTED) &&
61340f1702c5SYu Xiangning (so->so_mode & SM_CONNREQUIRED)) {
61350f1702c5SYu Xiangning /* Not connected yet - turn off write side events */
61360f1702c5SYu Xiangning events &= ~(POLLOUT|POLLWRBAND);
61370f1702c5SYu Xiangning }
61380f1702c5SYu Xiangning /*
61390f1702c5SYu Xiangning * Check for errors without calling strpoll if the caller wants them.
61400f1702c5SYu Xiangning * In sockets the errors are represented as input/output events
61410f1702c5SYu Xiangning * and there is no need to ask the stream head for this information.
61420f1702c5SYu Xiangning */
61430f1702c5SYu Xiangning if (so->so_error != 0 &&
61440f1702c5SYu Xiangning ((POLLIN|POLLRDNORM|POLLOUT) & origevents) != 0) {
61450f1702c5SYu Xiangning *reventsp = (POLLIN|POLLRDNORM|POLLOUT) & origevents;
61460f1702c5SYu Xiangning return (0);
61470f1702c5SYu Xiangning }
61480f1702c5SYu Xiangning /*
61490f1702c5SYu Xiangning * Ignore M_PROTO only messages such as the T_EXDATA_IND messages.
61500f1702c5SYu Xiangning * These message with only an M_PROTO/M_PCPROTO part and no M_DATA
61510f1702c5SYu Xiangning * will not trigger a POLLIN event with POLLRDDATA set.
61520f1702c5SYu Xiangning * The handling of urgent data (causing POLLRDBAND) is done by
61530f1702c5SYu Xiangning * inspecting SS_OOBPEND below.
61540f1702c5SYu Xiangning */
61550f1702c5SYu Xiangning events |= POLLRDDATA;
61560f1702c5SYu Xiangning
61570f1702c5SYu Xiangning /*
61580f1702c5SYu Xiangning * After shutdown(output) a stream head write error is set.
61590f1702c5SYu Xiangning * However, we should not return output events.
61600f1702c5SYu Xiangning */
61610f1702c5SYu Xiangning events |= POLLNOERR;
61620f1702c5SYu Xiangning error = strpoll(vp->v_stream, events, anyyet,
61630f1702c5SYu Xiangning reventsp, phpp);
61640f1702c5SYu Xiangning if (error)
61650f1702c5SYu Xiangning return (error);
61660f1702c5SYu Xiangning
61670f1702c5SYu Xiangning ASSERT(!(*reventsp & POLLERR));
61680f1702c5SYu Xiangning
61690f1702c5SYu Xiangning /*
61700f1702c5SYu Xiangning * Notes on T_CONN_IND handling for sockets.
61710f1702c5SYu Xiangning *
61720f1702c5SYu Xiangning * If strpoll() returned without events, SR_POLLIN is guaranteed
61730f1702c5SYu Xiangning * to be set, ensuring any subsequent strrput() runs pollwakeup().
61740f1702c5SYu Xiangning *
61750f1702c5SYu Xiangning * Since the so_lock is not held, soqueueconnind() may have run
61760f1702c5SYu Xiangning * and a T_CONN_IND may be waiting. We now check for any queued
61770f1702c5SYu Xiangning * T_CONN_IND msgs on sti_conn_ind_head and set appropriate events
61780f1702c5SYu Xiangning * to ensure poll returns.
61790f1702c5SYu Xiangning *
61800f1702c5SYu Xiangning * However:
61810f1702c5SYu Xiangning * If the T_CONN_IND hasn't arrived by the time strpoll() returns,
61820f1702c5SYu Xiangning * when strrput() does run for an arriving M_PROTO with T_CONN_IND
61830f1702c5SYu Xiangning * the following actions will occur; taken together they ensure the
61840f1702c5SYu Xiangning * syscall will return.
61850f1702c5SYu Xiangning *
61860f1702c5SYu Xiangning * 1. If a socket, soqueueconnind() will queue the T_CONN_IND but if
61870f1702c5SYu Xiangning * the accept() was run on a non-blocking socket sowaitconnind()
61880f1702c5SYu Xiangning * may have already returned EWOULDBLOCK, so not be waiting to
61890f1702c5SYu Xiangning * process the message. Additionally socktpi_poll() has probably
61900f1702c5SYu Xiangning * proceeded past the sti_conn_ind_head check below.
61910f1702c5SYu Xiangning * 2. strrput() runs pollwakeup()->pollnotify()->cv_signal() to wake
61920f1702c5SYu Xiangning * this thread, however that could occur before poll_common()
61930f1702c5SYu Xiangning * has entered cv_wait.
61940f1702c5SYu Xiangning * 3. pollnotify() sets T_POLLWAKE, while holding the pc_lock.
61950f1702c5SYu Xiangning *
61960f1702c5SYu Xiangning * Before proceeding to cv_wait() in poll_common() for an event,
61970f1702c5SYu Xiangning * poll_common() atomically checks for T_POLLWAKE under the pc_lock,
61980f1702c5SYu Xiangning * and if set, re-calls strpoll() to ensure the late arriving
61990f1702c5SYu Xiangning * T_CONN_IND is recognized, and pollsys() returns.
62000f1702c5SYu Xiangning */
62010f1702c5SYu Xiangning
62020f1702c5SYu Xiangning if (sti->sti_conn_ind_head != NULL)
62030f1702c5SYu Xiangning *reventsp |= (POLLIN|POLLRDNORM) & events;
62040f1702c5SYu Xiangning
6205075fab9aSBryan Cantrill if (so->so_state & SS_CANTRCVMORE) {
6206075fab9aSBryan Cantrill *reventsp |= POLLRDHUP & events;
6207075fab9aSBryan Cantrill
6208075fab9aSBryan Cantrill if (so->so_state & SS_CANTSENDMORE)
6209075fab9aSBryan Cantrill *reventsp |= POLLHUP;
6210075fab9aSBryan Cantrill }
6211075fab9aSBryan Cantrill
62120f1702c5SYu Xiangning if (so->so_state & SS_OOBPEND)
62130f1702c5SYu Xiangning *reventsp |= POLLRDBAND & events;
62140f1702c5SYu Xiangning
62150f1702c5SYu Xiangning return (0);
62160f1702c5SYu Xiangning }
62170f1702c5SYu Xiangning
62180f1702c5SYu Xiangning /*ARGSUSED*/
62190f1702c5SYu Xiangning static int
socktpi_constructor(void * buf,void * cdrarg,int kmflags)62200f1702c5SYu Xiangning socktpi_constructor(void *buf, void *cdrarg, int kmflags)
62210f1702c5SYu Xiangning {
62220f1702c5SYu Xiangning sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
62230f1702c5SYu Xiangning int error = 0;
62240f1702c5SYu Xiangning
62250f1702c5SYu Xiangning error = sonode_constructor(buf, cdrarg, kmflags);
62260f1702c5SYu Xiangning if (error != 0)
62270f1702c5SYu Xiangning return (error);
62280f1702c5SYu Xiangning
62290f1702c5SYu Xiangning error = i_sotpi_info_constructor(&st->st_info);
62300f1702c5SYu Xiangning if (error != 0)
62310f1702c5SYu Xiangning sonode_destructor(buf, cdrarg);
62320f1702c5SYu Xiangning
62330f1702c5SYu Xiangning st->st_sonode.so_priv = &st->st_info;
62340f1702c5SYu Xiangning
62350f1702c5SYu Xiangning return (error);
62360f1702c5SYu Xiangning }
62370f1702c5SYu Xiangning
62380f1702c5SYu Xiangning /*ARGSUSED1*/
62390f1702c5SYu Xiangning static void
socktpi_destructor(void * buf,void * cdrarg)62400f1702c5SYu Xiangning socktpi_destructor(void *buf, void *cdrarg)
62410f1702c5SYu Xiangning {
62420f1702c5SYu Xiangning sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
62430f1702c5SYu Xiangning
62440f1702c5SYu Xiangning ASSERT(st->st_sonode.so_priv == &st->st_info);
62450f1702c5SYu Xiangning st->st_sonode.so_priv = NULL;
62460f1702c5SYu Xiangning
62470f1702c5SYu Xiangning i_sotpi_info_destructor(&st->st_info);
62480f1702c5SYu Xiangning sonode_destructor(buf, cdrarg);
62490f1702c5SYu Xiangning }
62500f1702c5SYu Xiangning
62510f1702c5SYu Xiangning static int
socktpi_unix_constructor(void * buf,void * cdrarg,int kmflags)62520f1702c5SYu Xiangning socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags)
62530f1702c5SYu Xiangning {
62540f1702c5SYu Xiangning int retval;
62550f1702c5SYu Xiangning
62560f1702c5SYu Xiangning if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) {
62570f1702c5SYu Xiangning struct sonode *so = (struct sonode *)buf;
62580f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
62590f1702c5SYu Xiangning
62600f1702c5SYu Xiangning mutex_enter(&socklist.sl_lock);
62610f1702c5SYu Xiangning
62620f1702c5SYu Xiangning sti->sti_next_so = socklist.sl_list;
62630f1702c5SYu Xiangning sti->sti_prev_so = NULL;
62640f1702c5SYu Xiangning if (sti->sti_next_so != NULL)
62650f1702c5SYu Xiangning SOTOTPI(sti->sti_next_so)->sti_prev_so = so;
62660f1702c5SYu Xiangning socklist.sl_list = so;
62670f1702c5SYu Xiangning
62680f1702c5SYu Xiangning mutex_exit(&socklist.sl_lock);
62690f1702c5SYu Xiangning
62700f1702c5SYu Xiangning }
62710f1702c5SYu Xiangning return (retval);
62720f1702c5SYu Xiangning }
62730f1702c5SYu Xiangning
62740f1702c5SYu Xiangning static void
socktpi_unix_destructor(void * buf,void * cdrarg)62750f1702c5SYu Xiangning socktpi_unix_destructor(void *buf, void *cdrarg)
62760f1702c5SYu Xiangning {
62770f1702c5SYu Xiangning struct sonode *so = (struct sonode *)buf;
62780f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
62790f1702c5SYu Xiangning
62800f1702c5SYu Xiangning mutex_enter(&socklist.sl_lock);
62810f1702c5SYu Xiangning
62820f1702c5SYu Xiangning if (sti->sti_next_so != NULL)
62830f1702c5SYu Xiangning SOTOTPI(sti->sti_next_so)->sti_prev_so = sti->sti_prev_so;
62840f1702c5SYu Xiangning if (sti->sti_prev_so != NULL)
62850f1702c5SYu Xiangning SOTOTPI(sti->sti_prev_so)->sti_next_so = sti->sti_next_so;
62860f1702c5SYu Xiangning else
62870f1702c5SYu Xiangning socklist.sl_list = sti->sti_next_so;
62880f1702c5SYu Xiangning
62890f1702c5SYu Xiangning mutex_exit(&socklist.sl_lock);
62900f1702c5SYu Xiangning
62910f1702c5SYu Xiangning socktpi_destructor(buf, cdrarg);
62920f1702c5SYu Xiangning }
62930f1702c5SYu Xiangning
62940f1702c5SYu Xiangning int
socktpi_init(void)62950f1702c5SYu Xiangning socktpi_init(void)
62960f1702c5SYu Xiangning {
62970f1702c5SYu Xiangning /*
62980f1702c5SYu Xiangning * Create sonode caches. We create a special one for AF_UNIX so
6299bbf21555SRichard Lowe * that we can track them for netstat(8).
63000f1702c5SYu Xiangning */
63010f1702c5SYu Xiangning socktpi_cache = kmem_cache_create("socktpi_cache",
63020f1702c5SYu Xiangning sizeof (struct sotpi_sonode), 0, socktpi_constructor,
63030f1702c5SYu Xiangning socktpi_destructor, NULL, NULL, NULL, 0);
63040f1702c5SYu Xiangning
63050f1702c5SYu Xiangning socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache",
63060f1702c5SYu Xiangning sizeof (struct sotpi_sonode), 0, socktpi_unix_constructor,
63070f1702c5SYu Xiangning socktpi_unix_destructor, NULL, NULL, NULL, 0);
63080f1702c5SYu Xiangning
63090f1702c5SYu Xiangning return (0);
63100f1702c5SYu Xiangning }
63110f1702c5SYu Xiangning
63120f1702c5SYu Xiangning /*
63130f1702c5SYu Xiangning * Given a non-TPI sonode, allocate and prep it to be ready for TPI.
63140f1702c5SYu Xiangning *
63150f1702c5SYu Xiangning * Caller must still update state and mode using sotpi_update_state().
63160f1702c5SYu Xiangning */
631741174437SAnders Persson int
sotpi_convert_sonode(struct sonode * so,struct sockparams * newsp,boolean_t * direct,queue_t ** qp,struct cred * cr)63180f1702c5SYu Xiangning sotpi_convert_sonode(struct sonode *so, struct sockparams *newsp,
631941174437SAnders Persson boolean_t *direct, queue_t **qp, struct cred *cr)
63200f1702c5SYu Xiangning {
63210f1702c5SYu Xiangning sotpi_info_t *sti;
63220f1702c5SYu Xiangning struct sockparams *origsp = so->so_sockparams;
63230f1702c5SYu Xiangning sock_lower_handle_t handle = so->so_proto_handle;
63240f1702c5SYu Xiangning struct stdata *stp;
63250f1702c5SYu Xiangning struct vnode *vp;
63260f1702c5SYu Xiangning queue_t *q;
632741174437SAnders Persson int error = 0;
63280f1702c5SYu Xiangning
632941174437SAnders Persson ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
633041174437SAnders Persson SS_FALLBACK_PENDING);
633141174437SAnders Persson ASSERT(SOCK_IS_NONSTR(so));
633241174437SAnders Persson
633341174437SAnders Persson *qp = NULL;
63340f1702c5SYu Xiangning *direct = B_FALSE;
63350f1702c5SYu Xiangning so->so_sockparams = newsp;
63360f1702c5SYu Xiangning /*
63370f1702c5SYu Xiangning * Allocate and initalize fields required by TPI.
63380f1702c5SYu Xiangning */
63390f1702c5SYu Xiangning (void) sotpi_info_create(so, KM_SLEEP);
63400f1702c5SYu Xiangning sotpi_info_init(so);
63410f1702c5SYu Xiangning
634241174437SAnders Persson if ((error = sotpi_init(so, NULL, cr, SO_FALLBACK)) != 0) {
63430f1702c5SYu Xiangning sotpi_info_fini(so);
63440f1702c5SYu Xiangning sotpi_info_destroy(so);
634541174437SAnders Persson return (error);
63460f1702c5SYu Xiangning }
63470f1702c5SYu Xiangning ASSERT(handle == so->so_proto_handle);
63480f1702c5SYu Xiangning sti = SOTOTPI(so);
63490f1702c5SYu Xiangning if (sti->sti_direct != 0)
63500f1702c5SYu Xiangning *direct = B_TRUE;
63510f1702c5SYu Xiangning
63520f1702c5SYu Xiangning /*
63530f1702c5SYu Xiangning * Keep the original sp around so we can properly dispose of the
63540f1702c5SYu Xiangning * sonode when the socket is being closed.
63550f1702c5SYu Xiangning */
63560f1702c5SYu Xiangning sti->sti_orig_sp = origsp;
63570f1702c5SYu Xiangning
63580f1702c5SYu Xiangning so_basic_strinit(so); /* skips the T_CAPABILITY_REQ */
63590f1702c5SYu Xiangning so_alloc_addr(so, so->so_max_addr_len);
63600f1702c5SYu Xiangning
63610f1702c5SYu Xiangning /*
63620f1702c5SYu Xiangning * If the application has done a SIOCSPGRP, make sure the
63630f1702c5SYu Xiangning * STREAM head is aware. This needs to take place before
63640f1702c5SYu Xiangning * the protocol start sending up messages. Otherwise we
63650f1702c5SYu Xiangning * might miss to generate SIGPOLL.
63660f1702c5SYu Xiangning *
63670f1702c5SYu Xiangning * It is possible that the application will receive duplicate
63680f1702c5SYu Xiangning * signals if some were already generated for either data or
63690f1702c5SYu Xiangning * connection indications.
63700f1702c5SYu Xiangning */
63710f1702c5SYu Xiangning if (so->so_pgrp != 0) {
63720f1702c5SYu Xiangning if (so_set_events(so, so->so_vnode, cr) != 0)
63730f1702c5SYu Xiangning so->so_pgrp = 0;
63740f1702c5SYu Xiangning }
63750f1702c5SYu Xiangning
63760f1702c5SYu Xiangning /*
63770f1702c5SYu Xiangning * Determine which queue to use.
63780f1702c5SYu Xiangning */
63790f1702c5SYu Xiangning vp = SOTOV(so);
63800f1702c5SYu Xiangning stp = vp->v_stream;
63810f1702c5SYu Xiangning ASSERT(stp != NULL);
63820f1702c5SYu Xiangning q = stp->sd_wrq->q_next;
63830f1702c5SYu Xiangning
63840f1702c5SYu Xiangning /*
63850f1702c5SYu Xiangning * Skip any modules that may have been auto pushed when the device
63860f1702c5SYu Xiangning * was opened
63870f1702c5SYu Xiangning */
63880f1702c5SYu Xiangning while (q->q_next != NULL)
63890f1702c5SYu Xiangning q = q->q_next;
639041174437SAnders Persson *qp = _RD(q);
63910f1702c5SYu Xiangning
639241174437SAnders Persson /* This is now a STREAMS sockets */
639341174437SAnders Persson so->so_not_str = B_FALSE;
639441174437SAnders Persson
639541174437SAnders Persson return (error);
639641174437SAnders Persson }
639741174437SAnders Persson
639841174437SAnders Persson /*
639941174437SAnders Persson * Revert a TPI sonode. It is only allowed to revert the sonode during
640041174437SAnders Persson * the fallback process.
640141174437SAnders Persson */
640241174437SAnders Persson void
sotpi_revert_sonode(struct sonode * so,struct cred * cr)640341174437SAnders Persson sotpi_revert_sonode(struct sonode *so, struct cred *cr)
640441174437SAnders Persson {
640541174437SAnders Persson vnode_t *vp = SOTOV(so);
640641174437SAnders Persson
640741174437SAnders Persson ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
640841174437SAnders Persson SS_FALLBACK_PENDING);
640941174437SAnders Persson ASSERT(!SOCK_IS_NONSTR(so));
641041174437SAnders Persson ASSERT(vp->v_stream != NULL);
641141174437SAnders Persson
641241174437SAnders Persson strclean(vp);
641341174437SAnders Persson (void) strclose(vp, FREAD|FWRITE|SO_FALLBACK, cr);
641441174437SAnders Persson
641541174437SAnders Persson /*
641641174437SAnders Persson * Restore the original sockparams. The caller is responsible for
641741174437SAnders Persson * dropping the ref to the new sp.
641841174437SAnders Persson */
641941174437SAnders Persson so->so_sockparams = SOTOTPI(so)->sti_orig_sp;
642041174437SAnders Persson
642141174437SAnders Persson sotpi_info_fini(so);
642241174437SAnders Persson sotpi_info_destroy(so);
642341174437SAnders Persson
642441174437SAnders Persson /* This is no longer a STREAMS sockets */
642541174437SAnders Persson so->so_not_str = B_TRUE;
64260f1702c5SYu Xiangning }
64270f1702c5SYu Xiangning
64280f1702c5SYu 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)64290f1702c5SYu Xiangning sotpi_update_state(struct sonode *so, struct T_capability_ack *tcap,
64300f1702c5SYu Xiangning struct sockaddr *laddr, socklen_t laddrlen, struct sockaddr *faddr,
64310f1702c5SYu Xiangning socklen_t faddrlen, short opts)
64320f1702c5SYu Xiangning {
64330f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
64340f1702c5SYu Xiangning
64350f1702c5SYu Xiangning so_proc_tcapability_ack(so, tcap);
64360f1702c5SYu Xiangning
64370f1702c5SYu Xiangning so->so_options |= opts;
64380f1702c5SYu Xiangning
64390f1702c5SYu Xiangning /*
64400f1702c5SYu Xiangning * Determine whether the foreign and local address are valid
64410f1702c5SYu Xiangning */
64420f1702c5SYu Xiangning if (laddrlen != 0) {
64430f1702c5SYu Xiangning ASSERT(laddrlen <= sti->sti_laddr_maxlen);
64440f1702c5SYu Xiangning sti->sti_laddr_len = laddrlen;
64450f1702c5SYu Xiangning bcopy(laddr, sti->sti_laddr_sa, laddrlen);
64460f1702c5SYu Xiangning sti->sti_laddr_valid = (so->so_state & SS_ISBOUND);
64470f1702c5SYu Xiangning }
64480f1702c5SYu Xiangning
64490f1702c5SYu Xiangning if (faddrlen != 0) {
64500f1702c5SYu Xiangning ASSERT(faddrlen <= sti->sti_faddr_maxlen);
64510f1702c5SYu Xiangning sti->sti_faddr_len = faddrlen;
64520f1702c5SYu Xiangning bcopy(faddr, sti->sti_faddr_sa, faddrlen);
64530f1702c5SYu Xiangning sti->sti_faddr_valid = (so->so_state & SS_ISCONNECTED);
64540f1702c5SYu Xiangning }
64550f1702c5SYu Xiangning
64560f1702c5SYu Xiangning }
64570f1702c5SYu Xiangning
64580f1702c5SYu Xiangning /*
64590f1702c5SYu Xiangning * Allocate enough space to cache the local and foreign addresses.
64600f1702c5SYu Xiangning */
64610f1702c5SYu Xiangning void
so_alloc_addr(struct sonode * so,t_uscalar_t maxlen)64620f1702c5SYu Xiangning so_alloc_addr(struct sonode *so, t_uscalar_t maxlen)
64630f1702c5SYu Xiangning {
64640f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
64650f1702c5SYu Xiangning
64660f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
64670f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len == 0 && sti->sti_faddr_len == 0);
64680f1702c5SYu Xiangning sti->sti_laddr_maxlen = sti->sti_faddr_maxlen =
64690f1702c5SYu Xiangning P2ROUNDUP(maxlen, KMEM_ALIGN);
64700f1702c5SYu Xiangning so->so_max_addr_len = sti->sti_laddr_maxlen;
64710f1702c5SYu Xiangning sti->sti_laddr_sa = kmem_alloc(sti->sti_laddr_maxlen * 2, KM_SLEEP);
64720f1702c5SYu Xiangning sti->sti_faddr_sa = (struct sockaddr *)((caddr_t)sti->sti_laddr_sa
64730f1702c5SYu Xiangning + sti->sti_laddr_maxlen);
64740f1702c5SYu Xiangning
64750f1702c5SYu Xiangning if (so->so_family == AF_UNIX) {
64760f1702c5SYu Xiangning /*
64770f1702c5SYu Xiangning * Initialize AF_UNIX related fields.
64780f1702c5SYu Xiangning */
64790f1702c5SYu Xiangning bzero(&sti->sti_ux_laddr, sizeof (sti->sti_ux_laddr));
64800f1702c5SYu Xiangning bzero(&sti->sti_ux_faddr, sizeof (sti->sti_ux_faddr));
64810f1702c5SYu Xiangning }
64820f1702c5SYu Xiangning }
64830f1702c5SYu Xiangning
64840f1702c5SYu Xiangning
64850f1702c5SYu Xiangning sotpi_info_t *
sotpi_sototpi(struct sonode * so)64860f1702c5SYu Xiangning sotpi_sototpi(struct sonode *so)
64870f1702c5SYu Xiangning {
64880f1702c5SYu Xiangning sotpi_info_t *sti;
64890f1702c5SYu Xiangning
649041174437SAnders Persson ASSERT(so != NULL);
64910f1702c5SYu Xiangning
64920f1702c5SYu Xiangning sti = (sotpi_info_t *)so->so_priv;
64930f1702c5SYu Xiangning
64940f1702c5SYu Xiangning ASSERT(sti != NULL);
64950f1702c5SYu Xiangning ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
64960f1702c5SYu Xiangning
64970f1702c5SYu Xiangning return (sti);
64980f1702c5SYu Xiangning }
64990f1702c5SYu Xiangning
65000f1702c5SYu Xiangning static int
i_sotpi_info_constructor(sotpi_info_t * sti)65010f1702c5SYu Xiangning i_sotpi_info_constructor(sotpi_info_t *sti)
65020f1702c5SYu Xiangning {
65030f1702c5SYu Xiangning sti->sti_magic = SOTPI_INFO_MAGIC;
65040f1702c5SYu Xiangning sti->sti_ack_mp = NULL;
65050f1702c5SYu Xiangning sti->sti_discon_ind_mp = NULL;
65060f1702c5SYu Xiangning sti->sti_ux_bound_vp = NULL;
65070f1702c5SYu Xiangning sti->sti_unbind_mp = NULL;
65080f1702c5SYu Xiangning
65090f1702c5SYu Xiangning sti->sti_conn_ind_head = NULL;
65100f1702c5SYu Xiangning sti->sti_conn_ind_tail = NULL;
65110f1702c5SYu Xiangning
65120f1702c5SYu Xiangning sti->sti_laddr_sa = NULL;
65130f1702c5SYu Xiangning sti->sti_faddr_sa = NULL;
65140f1702c5SYu Xiangning
65150f1702c5SYu Xiangning mutex_init(&sti->sti_plumb_lock, NULL, MUTEX_DEFAULT, NULL);
65160f1702c5SYu Xiangning cv_init(&sti->sti_ack_cv, NULL, CV_DEFAULT, NULL);
65170f1702c5SYu Xiangning
65180f1702c5SYu Xiangning return (0);
65190f1702c5SYu Xiangning }
65200f1702c5SYu Xiangning
65210f1702c5SYu Xiangning static void
i_sotpi_info_destructor(sotpi_info_t * sti)65220f1702c5SYu Xiangning i_sotpi_info_destructor(sotpi_info_t *sti)
65230f1702c5SYu Xiangning {
65240f1702c5SYu Xiangning ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
65250f1702c5SYu Xiangning ASSERT(sti->sti_ack_mp == NULL);
65260f1702c5SYu Xiangning ASSERT(sti->sti_discon_ind_mp == NULL);
65270f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
65280f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp == NULL);
65290f1702c5SYu Xiangning
65300f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_head == NULL);
65310f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_tail == NULL);
65320f1702c5SYu Xiangning
65330f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL);
65340f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa == NULL);
65350f1702c5SYu Xiangning
65360f1702c5SYu Xiangning mutex_destroy(&sti->sti_plumb_lock);
65370f1702c5SYu Xiangning cv_destroy(&sti->sti_ack_cv);
65380f1702c5SYu Xiangning }
65390f1702c5SYu Xiangning
65400f1702c5SYu Xiangning /*
65410f1702c5SYu Xiangning * Creates and attaches TPI information to the given sonode
65420f1702c5SYu Xiangning */
65430f1702c5SYu Xiangning static boolean_t
sotpi_info_create(struct sonode * so,int kmflags)65440f1702c5SYu Xiangning sotpi_info_create(struct sonode *so, int kmflags)
65450f1702c5SYu Xiangning {
65460f1702c5SYu Xiangning sotpi_info_t *sti;
65470f1702c5SYu Xiangning
65480f1702c5SYu Xiangning ASSERT(so->so_priv == NULL);
65490f1702c5SYu Xiangning
65500f1702c5SYu Xiangning if ((sti = kmem_zalloc(sizeof (*sti), kmflags)) == NULL)
65510f1702c5SYu Xiangning return (B_FALSE);
65520f1702c5SYu Xiangning
65530f1702c5SYu Xiangning if (i_sotpi_info_constructor(sti) != 0) {
65540f1702c5SYu Xiangning kmem_free(sti, sizeof (*sti));
65550f1702c5SYu Xiangning return (B_FALSE);
65560f1702c5SYu Xiangning }
65570f1702c5SYu Xiangning
65580f1702c5SYu Xiangning so->so_priv = (void *)sti;
65590f1702c5SYu Xiangning return (B_TRUE);
65600f1702c5SYu Xiangning }
65610f1702c5SYu Xiangning
65620f1702c5SYu Xiangning /*
65630f1702c5SYu Xiangning * Initializes the TPI information.
65640f1702c5SYu Xiangning */
65650f1702c5SYu Xiangning static void
sotpi_info_init(struct sonode * so)65660f1702c5SYu Xiangning sotpi_info_init(struct sonode *so)
65670f1702c5SYu Xiangning {
65680f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
65690f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
65700f1702c5SYu Xiangning time_t now;
65710f1702c5SYu Xiangning
65720f1702c5SYu Xiangning sti->sti_dev = so->so_sockparams->sp_sdev_info.sd_vnode->v_rdev;
65730f1702c5SYu Xiangning vp->v_rdev = sti->sti_dev;
65740f1702c5SYu Xiangning
65750f1702c5SYu Xiangning sti->sti_orig_sp = NULL;
65760f1702c5SYu Xiangning
65770f1702c5SYu Xiangning sti->sti_pushcnt = 0;
65780f1702c5SYu Xiangning
65790f1702c5SYu Xiangning now = gethrestime_sec();
65800f1702c5SYu Xiangning sti->sti_atime = now;
65810f1702c5SYu Xiangning sti->sti_mtime = now;
65820f1702c5SYu Xiangning sti->sti_ctime = now;
65830f1702c5SYu Xiangning
65840f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
65850f1702c5SYu Xiangning sti->sti_delayed_error = 0;
65860f1702c5SYu Xiangning
65870f1702c5SYu Xiangning sti->sti_provinfo = NULL;
65880f1702c5SYu Xiangning
65890f1702c5SYu Xiangning sti->sti_oobcnt = 0;
65900f1702c5SYu Xiangning sti->sti_oobsigcnt = 0;
65910f1702c5SYu Xiangning
65920f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
65930f1702c5SYu Xiangning
65940f1702c5SYu Xiangning sti->sti_laddr_sa = 0;
65950f1702c5SYu Xiangning sti->sti_faddr_sa = 0;
65960f1702c5SYu Xiangning sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 0;
65970f1702c5SYu Xiangning sti->sti_laddr_len = sti->sti_faddr_len = 0;
65980f1702c5SYu Xiangning
65990f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
66000f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
66010f1702c5SYu Xiangning sti->sti_faddr_noxlate = 0;
66020f1702c5SYu Xiangning
66030f1702c5SYu Xiangning sti->sti_direct = 0;
66040f1702c5SYu Xiangning
66050f1702c5SYu Xiangning ASSERT(sti->sti_ack_mp == NULL);
66060f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
66070f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp == NULL);
66080f1702c5SYu Xiangning
66090f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_head == NULL);
66100f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_tail == NULL);
66110f1702c5SYu Xiangning }
66120f1702c5SYu Xiangning
66130f1702c5SYu Xiangning /*
66140f1702c5SYu Xiangning * Given a sonode, grab the TPI info and free any data.
66150f1702c5SYu Xiangning */
66160f1702c5SYu Xiangning static void
sotpi_info_fini(struct sonode * so)66170f1702c5SYu Xiangning sotpi_info_fini(struct sonode *so)
66180f1702c5SYu Xiangning {
66190f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
66200f1702c5SYu Xiangning mblk_t *mp;
66210f1702c5SYu Xiangning
66220f1702c5SYu Xiangning ASSERT(sti->sti_discon_ind_mp == NULL);
66230f1702c5SYu Xiangning
66240f1702c5SYu Xiangning if ((mp = sti->sti_conn_ind_head) != NULL) {
66250f1702c5SYu Xiangning mblk_t *mp1;
66260f1702c5SYu Xiangning
66270f1702c5SYu Xiangning while (mp) {
66280f1702c5SYu Xiangning mp1 = mp->b_next;
66290f1702c5SYu Xiangning mp->b_next = NULL;
66300f1702c5SYu Xiangning freemsg(mp);
66310f1702c5SYu Xiangning mp = mp1;
66320f1702c5SYu Xiangning }
66330f1702c5SYu Xiangning sti->sti_conn_ind_head = sti->sti_conn_ind_tail = NULL;
66340f1702c5SYu Xiangning }
66350f1702c5SYu Xiangning
66360f1702c5SYu Xiangning /*
66370f1702c5SYu Xiangning * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely
66380f1702c5SYu Xiangning * indirect them. It also uses so_count as a validity test.
66390f1702c5SYu Xiangning */
66400f1702c5SYu Xiangning mutex_enter(&so->so_lock);
66410f1702c5SYu Xiangning
66420f1702c5SYu Xiangning if (sti->sti_laddr_sa) {
66430f1702c5SYu Xiangning ASSERT((caddr_t)sti->sti_faddr_sa ==
66440f1702c5SYu Xiangning (caddr_t)sti->sti_laddr_sa + sti->sti_laddr_maxlen);
66450f1702c5SYu Xiangning ASSERT(sti->sti_faddr_maxlen == sti->sti_laddr_maxlen);
66460f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
66470f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
66480f1702c5SYu Xiangning kmem_free(sti->sti_laddr_sa, sti->sti_laddr_maxlen * 2);
66490f1702c5SYu Xiangning sti->sti_laddr_sa = NULL;
66500f1702c5SYu Xiangning sti->sti_laddr_len = sti->sti_laddr_maxlen = 0;
66510f1702c5SYu Xiangning sti->sti_faddr_sa = NULL;
66520f1702c5SYu Xiangning sti->sti_faddr_len = sti->sti_faddr_maxlen = 0;
66530f1702c5SYu Xiangning }
66540f1702c5SYu Xiangning
66550f1702c5SYu Xiangning mutex_exit(&so->so_lock);
66560f1702c5SYu Xiangning
66570f1702c5SYu Xiangning if ((mp = sti->sti_eaddr_mp) != NULL) {
66580f1702c5SYu Xiangning freemsg(mp);
66590f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
66600f1702c5SYu Xiangning sti->sti_delayed_error = 0;
66610f1702c5SYu Xiangning }
66620f1702c5SYu Xiangning
66630f1702c5SYu Xiangning if ((mp = sti->sti_ack_mp) != NULL) {
66640f1702c5SYu Xiangning freemsg(mp);
66650f1702c5SYu Xiangning sti->sti_ack_mp = NULL;
66660f1702c5SYu Xiangning }
66670f1702c5SYu Xiangning
66680f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
66690f1702c5SYu Xiangning if ((mp = sti->sti_unbind_mp) != NULL) {
66700f1702c5SYu Xiangning freemsg(mp);
66710f1702c5SYu Xiangning sti->sti_unbind_mp = NULL;
66720f1702c5SYu Xiangning }
66730f1702c5SYu Xiangning }
66740f1702c5SYu Xiangning
66750f1702c5SYu Xiangning /*
66760f1702c5SYu Xiangning * Destroys the TPI information attached to a sonode.
66770f1702c5SYu Xiangning */
66780f1702c5SYu Xiangning static void
sotpi_info_destroy(struct sonode * so)66790f1702c5SYu Xiangning sotpi_info_destroy(struct sonode *so)
66800f1702c5SYu Xiangning {
66810f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
66820f1702c5SYu Xiangning
66830f1702c5SYu Xiangning i_sotpi_info_destructor(sti);
66840f1702c5SYu Xiangning kmem_free(sti, sizeof (*sti));
66850f1702c5SYu Xiangning
66860f1702c5SYu Xiangning so->so_priv = NULL;
66870f1702c5SYu Xiangning }
66880f1702c5SYu Xiangning
66890f1702c5SYu Xiangning /*
66902691240cSYu Xiangning * Create the global sotpi socket module entry. It will never be freed.
66910f1702c5SYu Xiangning */
66920f1702c5SYu Xiangning smod_info_t *
sotpi_smod_create(void)66930f1702c5SYu Xiangning sotpi_smod_create(void)
66940f1702c5SYu Xiangning {
66950f1702c5SYu Xiangning smod_info_t *smodp;
66960f1702c5SYu Xiangning
66970f1702c5SYu Xiangning smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP);
66982691240cSYu Xiangning smodp->smod_name = kmem_alloc(sizeof (SOTPI_SMOD_NAME), KM_SLEEP);
66992691240cSYu Xiangning (void) strcpy(smodp->smod_name, SOTPI_SMOD_NAME);
67000f1702c5SYu Xiangning /*
67012691240cSYu Xiangning * Initialize the smod_refcnt to 1 so it will never be freed.
67020f1702c5SYu Xiangning */
67030f1702c5SYu Xiangning smodp->smod_refcnt = 1;
67040f1702c5SYu Xiangning smodp->smod_uc_version = SOCK_UC_VERSION;
67050f1702c5SYu Xiangning smodp->smod_dc_version = SOCK_DC_VERSION;
67060f1702c5SYu Xiangning smodp->smod_sock_create_func = &sotpi_create;
67070f1702c5SYu Xiangning smodp->smod_sock_destroy_func = &sotpi_destroy;
67080f1702c5SYu Xiangning return (smodp);
67090f1702c5SYu Xiangning }
6710