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.
24cbc6e898SGordon Ross * Copyright 2015, Joyent, Inc.
25cbc6e898SGordon Ross * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26dfc0fed8SRobert Mustacchi */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/buf.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
360f1702c5SYu Xiangning #include <sys/kmem_impl.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/time.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <sys/open.h>
457c478bd9Sstevel@tonic-gate #include <sys/user.h>
467c478bd9Sstevel@tonic-gate #include <sys/termios.h>
477c478bd9Sstevel@tonic-gate #include <sys/stream.h>
487c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
500f1702c5SYu Xiangning #include <sys/suntpi.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
527c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/flock.h>
547c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
557c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
567c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
577c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #include <sys/socket.h>
607c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
61ff550d0eSmasputra #include <sys/sockio.h>
627c478bd9Sstevel@tonic-gate #include <netinet/in.h>
637c478bd9Sstevel@tonic-gate #include <sys/un.h>
647c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
677c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
687c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
697c478bd9Sstevel@tonic-gate #include <sys/timod.h> /* TI_GETMYNAME, TI_GETPEERNAME */
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #include <c2/audit.h>
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #include <inet/common.h>
747c478bd9Sstevel@tonic-gate #include <inet/ip.h>
757c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
767c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
77ff550d0eSmasputra #include <inet/udp_impl.h>
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate #include <sys/zone.h>
807c478bd9Sstevel@tonic-gate
812c9e429eSbrutus #include <fs/sockfs/nl7c.h>
822c9e429eSbrutus #include <fs/sockfs/nl7curi.h>
832c9e429eSbrutus
840f1702c5SYu Xiangning #include <fs/sockfs/sockcommon.h>
850f1702c5SYu Xiangning #include <fs/sockfs/socktpi.h>
860f1702c5SYu Xiangning #include <fs/sockfs/socktpi_impl.h>
870f1702c5SYu Xiangning
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate * Possible failures when memory can't be allocated. The documented behavior:
907c478bd9Sstevel@tonic-gate *
917c478bd9Sstevel@tonic-gate * 5.5: 4.X: XNET:
927c478bd9Sstevel@tonic-gate * accept: ENOMEM/ENOSR/EINTR - (EINTR) ENOMEM/ENOBUFS/ENOSR/
937c478bd9Sstevel@tonic-gate * EINTR
947c478bd9Sstevel@tonic-gate * (4.X does not document EINTR but returns it)
957c478bd9Sstevel@tonic-gate * bind: ENOSR - ENOBUFS/ENOSR
967c478bd9Sstevel@tonic-gate * connect: EINTR EINTR ENOBUFS/ENOSR/EINTR
977c478bd9Sstevel@tonic-gate * getpeername: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR
987c478bd9Sstevel@tonic-gate * getsockname: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR
997c478bd9Sstevel@tonic-gate * (4.X getpeername and getsockname do not fail in practice)
1007c478bd9Sstevel@tonic-gate * getsockopt: ENOMEM/ENOSR - ENOBUFS/ENOSR
1017c478bd9Sstevel@tonic-gate * listen: - - ENOBUFS
1027c478bd9Sstevel@tonic-gate * recv: ENOMEM/ENOSR/EINTR EINTR ENOBUFS/ENOMEM/ENOSR/
1037c478bd9Sstevel@tonic-gate * EINTR
1047c478bd9Sstevel@tonic-gate * send: ENOMEM/ENOSR/EINTR ENOBUFS/EINTR ENOBUFS/ENOMEM/ENOSR/
1057c478bd9Sstevel@tonic-gate * EINTR
1067c478bd9Sstevel@tonic-gate * setsockopt: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR
1077c478bd9Sstevel@tonic-gate * shutdown: ENOMEM/ENOSR - ENOBUFS/ENOSR
1087c478bd9Sstevel@tonic-gate * socket: ENOMEM/ENOSR ENOBUFS ENOBUFS/ENOMEM/ENOSR
1097c478bd9Sstevel@tonic-gate * socketpair: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR
1107c478bd9Sstevel@tonic-gate *
1117c478bd9Sstevel@tonic-gate * Resolution. When allocation fails:
1127c478bd9Sstevel@tonic-gate * recv: return EINTR
1137c478bd9Sstevel@tonic-gate * send: return EINTR
1147c478bd9Sstevel@tonic-gate * connect, accept: EINTR
1157c478bd9Sstevel@tonic-gate * bind, listen, shutdown (unbind, unix_close, disconnect): sleep
1167c478bd9Sstevel@tonic-gate * socket, socketpair: ENOBUFS
1177c478bd9Sstevel@tonic-gate * getpeername, getsockname: sleep
1187c478bd9Sstevel@tonic-gate * getsockopt, setsockopt: sleep
1197c478bd9Sstevel@tonic-gate */
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * Variables that make sockfs do something other than the standard TPI
1247c478bd9Sstevel@tonic-gate * for the AF_INET transports.
1257c478bd9Sstevel@tonic-gate *
1267c478bd9Sstevel@tonic-gate * solisten_tpi_tcp:
1277c478bd9Sstevel@tonic-gate * TCP can handle a O_T_BIND_REQ with an increased backlog even though
1287c478bd9Sstevel@tonic-gate * the transport is already bound. This is needed to avoid loosing the
1297c478bd9Sstevel@tonic-gate * port number should listen() do a T_UNBIND_REQ followed by a
1307c478bd9Sstevel@tonic-gate * O_T_BIND_REQ.
1317c478bd9Sstevel@tonic-gate *
1327c478bd9Sstevel@tonic-gate * soconnect_tpi_udp:
1337c478bd9Sstevel@tonic-gate * UDP and ICMP can handle a T_CONN_REQ.
1347c478bd9Sstevel@tonic-gate * This is needed to make the sequence of connect(), getsockname()
1357c478bd9Sstevel@tonic-gate * return the local IP address used to send packets to the connected to
1367c478bd9Sstevel@tonic-gate * destination.
1377c478bd9Sstevel@tonic-gate *
1387c478bd9Sstevel@tonic-gate * soconnect_tpi_tcp:
1397c478bd9Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ.
1407c478bd9Sstevel@tonic-gate * Set this to non-zero to send TPI conformant messages to TCP in this
1417c478bd9Sstevel@tonic-gate * respect. This is a performance optimization.
1427c478bd9Sstevel@tonic-gate *
1437c478bd9Sstevel@tonic-gate * soaccept_tpi_tcp:
1447c478bd9Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without the acceptor being bound.
1457c478bd9Sstevel@tonic-gate * This is a performance optimization that has been picked up in XTI.
1467c478bd9Sstevel@tonic-gate *
1477c478bd9Sstevel@tonic-gate * soaccept_tpi_multioptions:
1487c478bd9Sstevel@tonic-gate * When inheriting SOL_SOCKET options from the listener to the accepting
1497c478bd9Sstevel@tonic-gate * socket send them as a single message for AF_INET{,6}.
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate int solisten_tpi_tcp = 0;
1527c478bd9Sstevel@tonic-gate int soconnect_tpi_udp = 0;
1537c478bd9Sstevel@tonic-gate int soconnect_tpi_tcp = 0;
1547c478bd9Sstevel@tonic-gate int soaccept_tpi_tcp = 0;
1557c478bd9Sstevel@tonic-gate int soaccept_tpi_multioptions = 1;
1567c478bd9Sstevel@tonic-gate #else /* SOCK_TEST */
1577c478bd9Sstevel@tonic-gate #define soconnect_tpi_tcp 0
1587c478bd9Sstevel@tonic-gate #define soconnect_tpi_udp 0
1597c478bd9Sstevel@tonic-gate #define solisten_tpi_tcp 0
1607c478bd9Sstevel@tonic-gate #define soaccept_tpi_tcp 0
1617c478bd9Sstevel@tonic-gate #define soaccept_tpi_multioptions 1
1627c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
1657c478bd9Sstevel@tonic-gate extern int do_useracc;
1667c478bd9Sstevel@tonic-gate extern clock_t sock_test_timelimit;
1677c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
1687c478bd9Sstevel@tonic-gate
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;
2810f1702c5SYu Xiangning int sfamily = family;
2827c478bd9Sstevel@tonic-gate
2830f1702c5SYu Xiangning ASSERT(sp->sp_sdev_info.sd_vnode != NULL);
2847c478bd9Sstevel@tonic-gate
2850f1702c5SYu Xiangning if (family == AF_NCA) {
2860f1702c5SYu Xiangning /*
2870f1702c5SYu Xiangning * The request is for an NCA socket so for NL7C use the
2880f1702c5SYu Xiangning * INET domain instead and mark NL7C_AF_NCA below.
2890f1702c5SYu Xiangning */
2900f1702c5SYu Xiangning family = AF_INET;
2910f1702c5SYu Xiangning /*
2920f1702c5SYu Xiangning * NL7C is not supported in the non-global zone,
2930f1702c5SYu Xiangning * we enforce this restriction here.
2940f1702c5SYu Xiangning */
2950f1702c5SYu Xiangning if (getzoneid() != GLOBAL_ZONEID) {
2960f1702c5SYu Xiangning *errorp = ENOTSUP;
2970f1702c5SYu Xiangning return (NULL);
2980f1702c5SYu Xiangning }
2990f1702c5SYu Xiangning }
300ff550d0eSmasputra
3010f1702c5SYu Xiangning /*
3020f1702c5SYu Xiangning * to be compatible with old tpi socket implementation ignore
3030f1702c5SYu Xiangning * sleep flag (sflags) passed in
3040f1702c5SYu Xiangning */
3050f1702c5SYu Xiangning cp = (family == AF_UNIX) ? socktpi_unix_cache : socktpi_cache;
3060f1702c5SYu Xiangning so = kmem_cache_alloc(cp, KM_SLEEP);
3070f1702c5SYu Xiangning if (so == NULL) {
3080f1702c5SYu Xiangning *errorp = ENOMEM;
3090f1702c5SYu Xiangning return (NULL);
3100f1702c5SYu Xiangning }
3110f1702c5SYu Xiangning
3120f1702c5SYu Xiangning sonode_init(so, sp, family, type, protocol, &sotpi_sonodeops);
3130f1702c5SYu Xiangning sotpi_info_init(so);
3140f1702c5SYu Xiangning
3150f1702c5SYu Xiangning if (sfamily == AF_NCA) {
3160f1702c5SYu Xiangning SOTOTPI(so)->sti_nl7c_flags = NL7C_AF_NCA;
3170f1702c5SYu Xiangning }
3180f1702c5SYu Xiangning
3190f1702c5SYu Xiangning if (version == SOV_DEFAULT)
3200f1702c5SYu Xiangning version = so_default_version;
3210f1702c5SYu Xiangning
3220f1702c5SYu Xiangning so->so_version = (short)version;
3230f1702c5SYu Xiangning *errorp = 0;
3240f1702c5SYu Xiangning
3250f1702c5SYu Xiangning return (so);
3260f1702c5SYu Xiangning }
3270f1702c5SYu Xiangning
3280f1702c5SYu Xiangning static void
sotpi_destroy(struct sonode * so)3290f1702c5SYu Xiangning sotpi_destroy(struct sonode *so)
3300f1702c5SYu Xiangning {
3310f1702c5SYu Xiangning kmem_cache_t *cp;
3320f1702c5SYu Xiangning struct sockparams *origsp;
3330f1702c5SYu Xiangning
3340f1702c5SYu Xiangning /*
3350f1702c5SYu Xiangning * If there is a new dealloc function (ie. smod_destroy_func),
3360f1702c5SYu Xiangning * then it should check the correctness of the ops.
3370f1702c5SYu Xiangning */
3380f1702c5SYu Xiangning
3390f1702c5SYu Xiangning ASSERT(so->so_ops == &sotpi_sonodeops);
3400f1702c5SYu Xiangning
3410f1702c5SYu Xiangning origsp = SOTOTPI(so)->sti_orig_sp;
3420f1702c5SYu Xiangning
3430f1702c5SYu Xiangning sotpi_info_fini(so);
3440f1702c5SYu Xiangning
3450f1702c5SYu Xiangning if (so->so_state & SS_FALLBACK_COMP) {
3460f1702c5SYu Xiangning /*
3470f1702c5SYu Xiangning * A fallback happend, which means that a sotpi_info_t struct
3480f1702c5SYu Xiangning * was allocated (as opposed to being allocated from the TPI
3490f1702c5SYu Xiangning * sonode cache. Therefore we explicitly free the struct
3500f1702c5SYu Xiangning * here.
3510f1702c5SYu Xiangning */
3520f1702c5SYu Xiangning sotpi_info_destroy(so);
3530f1702c5SYu Xiangning ASSERT(origsp != NULL);
3540f1702c5SYu Xiangning
3550f1702c5SYu Xiangning origsp->sp_smod_info->smod_sock_destroy_func(so);
3560f1702c5SYu Xiangning SOCKPARAMS_DEC_REF(origsp);
3570f1702c5SYu Xiangning } else {
3580f1702c5SYu Xiangning sonode_fini(so);
3590f1702c5SYu Xiangning cp = (so->so_family == AF_UNIX) ? socktpi_unix_cache :
3600f1702c5SYu Xiangning socktpi_cache;
3610f1702c5SYu Xiangning kmem_cache_free(cp, so);
3620f1702c5SYu Xiangning }
3630f1702c5SYu Xiangning }
3640f1702c5SYu Xiangning
3650f1702c5SYu Xiangning /* ARGSUSED1 */
3660f1702c5SYu Xiangning int
sotpi_init(struct sonode * so,struct sonode * tso,struct cred * cr,int flags)3670f1702c5SYu Xiangning sotpi_init(struct sonode *so, struct sonode *tso, struct cred *cr, int flags)
3680f1702c5SYu Xiangning {
3690f1702c5SYu Xiangning major_t maj;
3700f1702c5SYu Xiangning dev_t newdev;
3710f1702c5SYu Xiangning struct vnode *vp;
3720f1702c5SYu Xiangning int error = 0;
3730f1702c5SYu Xiangning struct stdata *stp;
3740f1702c5SYu Xiangning
3750f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
3760f1702c5SYu Xiangning
3770f1702c5SYu Xiangning dprint(1, ("sotpi_init()\n"));
3780f1702c5SYu Xiangning
3790f1702c5SYu Xiangning /*
3800f1702c5SYu Xiangning * over write the sleep flag passed in but that is ok
3810f1702c5SYu Xiangning * as tpi socket does not honor sleep flag.
3820f1702c5SYu Xiangning */
3830f1702c5SYu Xiangning flags |= FREAD|FWRITE;
3840f1702c5SYu Xiangning
3850f1702c5SYu Xiangning /*
3860f1702c5SYu Xiangning * Record in so_flag that it is a clone.
3870f1702c5SYu Xiangning */
3880f1702c5SYu Xiangning if (getmajor(sti->sti_dev) == clone_major)
3890f1702c5SYu Xiangning so->so_flag |= SOCLONE;
3900f1702c5SYu Xiangning
3910f1702c5SYu Xiangning if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM) &&
3920f1702c5SYu Xiangning (so->so_family == AF_INET || so->so_family == AF_INET6) &&
3930f1702c5SYu Xiangning (so->so_protocol == IPPROTO_TCP || so->so_protocol == IPPROTO_UDP ||
3940f1702c5SYu Xiangning so->so_protocol == IPPROTO_IP)) {
395ff550d0eSmasputra /* Tell tcp or udp that it's talking to sockets */
3967c478bd9Sstevel@tonic-gate flags |= SO_SOCKSTR;
397ff550d0eSmasputra
398ff550d0eSmasputra /*
399ff550d0eSmasputra * Here we indicate to socktpi_open() our attempt to
400ff550d0eSmasputra * make direct calls between sockfs and transport.
401ff550d0eSmasputra * The final decision is left to socktpi_open().
402ff550d0eSmasputra */
4030f1702c5SYu Xiangning sti->sti_direct = 1;
404ff550d0eSmasputra
405ff550d0eSmasputra ASSERT(so->so_type != SOCK_DGRAM || tso == NULL);
406ff550d0eSmasputra if (so->so_type == SOCK_STREAM && tso != NULL) {
4070f1702c5SYu Xiangning if (SOTOTPI(tso)->sti_direct) {
408ff550d0eSmasputra /*
4090f1702c5SYu Xiangning * Inherit sti_direct from listener and pass
410ff550d0eSmasputra * SO_ACCEPTOR open flag to tcp, indicating
411ff550d0eSmasputra * that this is an accept fast-path instance.
412ff550d0eSmasputra */
413ff550d0eSmasputra flags |= SO_ACCEPTOR;
414ff550d0eSmasputra } else {
415ff550d0eSmasputra /*
4160f1702c5SYu Xiangning * sti_direct is not set on listener, meaning
417ff550d0eSmasputra * that the listener has been converted from
418ff550d0eSmasputra * a socket to a stream. Ensure that the
419ff550d0eSmasputra * acceptor inherits these settings.
420ff550d0eSmasputra */
4210f1702c5SYu Xiangning sti->sti_direct = 0;
422ff550d0eSmasputra flags &= ~SO_SOCKSTR;
423ff550d0eSmasputra }
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate * Tell local transport that it is talking to sockets.
4297c478bd9Sstevel@tonic-gate */
4307c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
4317c478bd9Sstevel@tonic-gate flags |= SO_SOCKSTR;
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate
4340f1702c5SYu Xiangning vp = SOTOV(so);
4350f1702c5SYu Xiangning newdev = vp->v_rdev;
4360f1702c5SYu Xiangning maj = getmajor(newdev);
4370f1702c5SYu Xiangning ASSERT(STREAMSTAB(maj));
438655a2e99Skais
4390f1702c5SYu Xiangning error = stropen(vp, &newdev, flags, cr);
4400f1702c5SYu Xiangning
4410f1702c5SYu Xiangning stp = vp->v_stream;
4420f1702c5SYu Xiangning if (error == 0) {
4430f1702c5SYu Xiangning if (so->so_flag & SOCLONE)
4440f1702c5SYu Xiangning ASSERT(newdev != vp->v_rdev);
4450f1702c5SYu Xiangning mutex_enter(&so->so_lock);
4460f1702c5SYu Xiangning sti->sti_dev = newdev;
4470f1702c5SYu Xiangning vp->v_rdev = newdev;
4480f1702c5SYu Xiangning mutex_exit(&so->so_lock);
4490f1702c5SYu Xiangning
4500f1702c5SYu Xiangning if (stp->sd_flag & STRISTTY) {
4510f1702c5SYu Xiangning /*
4520f1702c5SYu Xiangning * this is a post SVR4 tty driver - a socket can not
4530f1702c5SYu Xiangning * be a controlling terminal. Fail the open.
4540f1702c5SYu Xiangning */
4550f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
4560f1702c5SYu Xiangning return (ENOTTY); /* XXX */
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate
4590f1702c5SYu Xiangning ASSERT(stp->sd_wrq != NULL);
4600f1702c5SYu Xiangning sti->sti_provinfo = tpi_findprov(stp->sd_wrq);
4610f1702c5SYu Xiangning
4620f1702c5SYu Xiangning /*
4630f1702c5SYu Xiangning * If caller is interested in doing direct function call
4640f1702c5SYu Xiangning * interface to/from transport module, probe the module
4650f1702c5SYu Xiangning * directly beneath the streamhead to see if it qualifies.
4660f1702c5SYu Xiangning *
4670f1702c5SYu Xiangning * We turn off the direct interface when qualifications fail.
4680f1702c5SYu Xiangning * In the acceptor case, we simply turn off the sti_direct
4690f1702c5SYu Xiangning * flag on the socket. We do the fallback after the accept
4700f1702c5SYu Xiangning * has completed, before the new socket is returned to the
4710f1702c5SYu Xiangning * application.
4720f1702c5SYu Xiangning */
4730f1702c5SYu Xiangning if (sti->sti_direct) {
4740f1702c5SYu Xiangning queue_t *tq = stp->sd_wrq->q_next;
4750f1702c5SYu Xiangning
4760f1702c5SYu Xiangning /*
4770f1702c5SYu Xiangning * sti_direct is currently supported and tested
4780f1702c5SYu Xiangning * only for tcp/udp; this is the main reason to
4790f1702c5SYu Xiangning * have the following assertions.
4800f1702c5SYu Xiangning */
4810f1702c5SYu Xiangning ASSERT(so->so_family == AF_INET ||
4820f1702c5SYu Xiangning so->so_family == AF_INET6);
4830f1702c5SYu Xiangning ASSERT(so->so_protocol == IPPROTO_UDP ||
4840f1702c5SYu Xiangning so->so_protocol == IPPROTO_TCP ||
4850f1702c5SYu Xiangning so->so_protocol == IPPROTO_IP);
4860f1702c5SYu Xiangning ASSERT(so->so_type == SOCK_DGRAM ||
4870f1702c5SYu Xiangning so->so_type == SOCK_STREAM);
4880f1702c5SYu Xiangning
4890f1702c5SYu Xiangning /*
4900f1702c5SYu Xiangning * Abort direct call interface if the module directly
4910f1702c5SYu Xiangning * underneath the stream head is not defined with the
4920f1702c5SYu Xiangning * _D_DIRECT flag. This could happen in the tcp or
4930f1702c5SYu Xiangning * udp case, when some other module is autopushed
4940f1702c5SYu Xiangning * above it, or for some reasons the expected module
4950f1702c5SYu Xiangning * isn't purely D_MP (which is the main requirement).
4960f1702c5SYu Xiangning */
4970f1702c5SYu Xiangning if (!socktpi_direct || !(tq->q_flag & _QDIRECT) ||
4980f1702c5SYu Xiangning !(_OTHERQ(tq)->q_flag & _QDIRECT)) {
4990f1702c5SYu Xiangning int rval;
5000f1702c5SYu Xiangning
5010f1702c5SYu Xiangning /* Continue on without direct calls */
5020f1702c5SYu Xiangning sti->sti_direct = 0;
5030f1702c5SYu Xiangning
5040f1702c5SYu Xiangning /*
5050f1702c5SYu Xiangning * Cannot issue ioctl on fallback socket since
5060f1702c5SYu Xiangning * there is no conn associated with the queue.
5070f1702c5SYu Xiangning * The fallback downcall will notify the proto
5080f1702c5SYu Xiangning * of the change.
5090f1702c5SYu Xiangning */
5100f1702c5SYu Xiangning if (!(flags & SO_ACCEPTOR) &&
5110f1702c5SYu Xiangning !(flags & SO_FALLBACK)) {
5120f1702c5SYu Xiangning if ((error = strioctl(vp,
5130f1702c5SYu Xiangning _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
5140f1702c5SYu Xiangning cr, &rval)) != 0) {
5150f1702c5SYu Xiangning (void) sotpi_close(so, flags,
5160f1702c5SYu Xiangning cr);
5170f1702c5SYu Xiangning return (error);
5180f1702c5SYu Xiangning }
5190f1702c5SYu Xiangning }
5200f1702c5SYu Xiangning }
5210f1702c5SYu Xiangning }
5220f1702c5SYu Xiangning
5230f1702c5SYu Xiangning if (flags & SO_FALLBACK) {
5240f1702c5SYu Xiangning /*
5250f1702c5SYu Xiangning * The stream created does not have a conn.
5260f1702c5SYu Xiangning * do stream set up after conn has been assigned
5270f1702c5SYu Xiangning */
5280f1702c5SYu Xiangning return (error);
5290f1702c5SYu Xiangning }
5307c478bd9Sstevel@tonic-gate if (error = so_strinit(so, tso)) {
5310f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
5320f1702c5SYu Xiangning return (error);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate
5350f1702c5SYu Xiangning /* Wildcard */
5360f1702c5SYu Xiangning if (so->so_protocol != so->so_sockparams->sp_protocol) {
5370f1702c5SYu Xiangning int protocol = so->so_protocol;
5380f1702c5SYu Xiangning /*
5390f1702c5SYu Xiangning * Issue SO_PROTOTYPE setsockopt.
5400f1702c5SYu Xiangning */
5410f1702c5SYu Xiangning error = sotpi_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
5420f1702c5SYu Xiangning &protocol, (t_uscalar_t)sizeof (protocol), cr);
5430f1702c5SYu Xiangning if (error != 0) {
5440f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
5450f1702c5SYu Xiangning /*
5460f1702c5SYu Xiangning * Setsockopt often fails with ENOPROTOOPT but
5470f1702c5SYu Xiangning * socket() should fail with
5480f1702c5SYu Xiangning * EPROTONOSUPPORT/EPROTOTYPE.
5490f1702c5SYu Xiangning */
5500f1702c5SYu Xiangning return (EPROTONOSUPPORT);
5510f1702c5SYu Xiangning }
5520f1702c5SYu Xiangning }
5537c478bd9Sstevel@tonic-gate
5540f1702c5SYu Xiangning } else {
5550f1702c5SYu Xiangning /*
5560f1702c5SYu Xiangning * While the same socket can not be reopened (unlike specfs)
5570f1702c5SYu Xiangning * the stream head sets STREOPENFAIL when the autopush fails.
5580f1702c5SYu Xiangning */
5590f1702c5SYu Xiangning if ((stp != NULL) &&
5600f1702c5SYu Xiangning (stp->sd_flag & STREOPENFAIL)) {
5610f1702c5SYu Xiangning /*
5620f1702c5SYu Xiangning * Open failed part way through.
5630f1702c5SYu Xiangning */
5640f1702c5SYu Xiangning mutex_enter(&stp->sd_lock);
5650f1702c5SYu Xiangning stp->sd_flag &= ~STREOPENFAIL;
5660f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
5670f1702c5SYu Xiangning (void) sotpi_close(so, flags, cr);
5680f1702c5SYu Xiangning return (error);
5690f1702c5SYu Xiangning /*NOTREACHED*/
5700f1702c5SYu Xiangning }
5710f1702c5SYu Xiangning ASSERT(stp == NULL);
5720f1702c5SYu Xiangning }
5730f1702c5SYu Xiangning TRACE_4(TR_FAC_SOCKFS, TR_SOCKFS_OPEN,
5740f1702c5SYu Xiangning "sockfs open:maj %d vp %p so %p error %d",
5750f1702c5SYu Xiangning maj, vp, so, error);
5760f1702c5SYu Xiangning return (error);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate * Bind the socket to an unspecified address in sockfs only.
5817c478bd9Sstevel@tonic-gate * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't
5827c478bd9Sstevel@tonic-gate * required in all cases.
5837c478bd9Sstevel@tonic-gate */
5847c478bd9Sstevel@tonic-gate static void
so_automatic_bind(struct sonode * so)5857c478bd9Sstevel@tonic-gate so_automatic_bind(struct sonode *so)
5867c478bd9Sstevel@tonic-gate {
5870f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
5887c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
5917c478bd9Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND));
5920f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp);
5937c478bd9Sstevel@tonic-gate
5940f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
5950f1702c5SYu Xiangning bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
5960f1702c5SYu Xiangning sti->sti_laddr_sa->sa_family = so->so_family;
5977c478bd9Sstevel@tonic-gate so->so_state |= SS_ISBOUND;
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate * bind the socket.
6037c478bd9Sstevel@tonic-gate *
6047c478bd9Sstevel@tonic-gate * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2
6057c478bd9Sstevel@tonic-gate * are passed in we allow rebinding. Note that for backwards compatibility
6067c478bd9Sstevel@tonic-gate * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind.
6077c478bd9Sstevel@tonic-gate * Thus the rebinding code is currently not executed.
6087c478bd9Sstevel@tonic-gate *
6097c478bd9Sstevel@tonic-gate * The constraints for rebinding are:
6107c478bd9Sstevel@tonic-gate * - it is a SOCK_DGRAM, or
6117c478bd9Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6127c478bd9Sstevel@tonic-gate * and no listen() has been done.
6137c478bd9Sstevel@tonic-gate * This rebinding code was added based on some language in the XNET book
6147c478bd9Sstevel@tonic-gate * about not returning EINVAL it the protocol allows rebinding. However,
6157c478bd9Sstevel@tonic-gate * this language is not present in the Posix socket draft. Thus maybe the
6167c478bd9Sstevel@tonic-gate * rebinding logic should be deleted from the source.
6177c478bd9Sstevel@tonic-gate *
6187c478bd9Sstevel@tonic-gate * A null "name" can be used to unbind the socket if:
6197c478bd9Sstevel@tonic-gate * - it is a SOCK_DGRAM, or
6207c478bd9Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6217c478bd9Sstevel@tonic-gate * and no listen() has been done.
6227c478bd9Sstevel@tonic-gate */
6230f1702c5SYu Xiangning /* ARGSUSED */
6247c478bd9Sstevel@tonic-gate static int
sotpi_bindlisten(struct sonode * so,struct sockaddr * name,socklen_t namelen,int backlog,int flags,struct cred * cr)6257c478bd9Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name,
6260f1702c5SYu Xiangning socklen_t namelen, int backlog, int flags, struct cred *cr)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate struct T_bind_req bind_req;
6297c478bd9Sstevel@tonic-gate struct T_bind_ack *bind_ack;
6307c478bd9Sstevel@tonic-gate int error = 0;
6317c478bd9Sstevel@tonic-gate mblk_t *mp;
6327c478bd9Sstevel@tonic-gate void *addr;
6337c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
6347c478bd9Sstevel@tonic-gate int unbind_on_err = 1;
6357c478bd9Sstevel@tonic-gate boolean_t clear_acceptconn_on_err = B_FALSE;
6367c478bd9Sstevel@tonic-gate boolean_t restore_backlog_on_err = B_FALSE;
6377c478bd9Sstevel@tonic-gate int save_so_backlog;
6387c478bd9Sstevel@tonic-gate t_scalar_t PRIM_type = O_T_BIND_REQ;
6397c478bd9Sstevel@tonic-gate boolean_t tcp_udp_xport;
6407c478bd9Sstevel@tonic-gate void *nl7c = NULL;
6410f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n",
644903a11ebSrh87107 (void *)so, (void *)name, namelen, backlog, flags,
6457c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM;
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) {
6507c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
6517c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
6527c478bd9Sstevel@tonic-gate } else {
6537c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
6547c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate * Make sure that there is a preallocated unbind_req message
6597c478bd9Sstevel@tonic-gate * before binding. This message allocated when the socket is
6607c478bd9Sstevel@tonic-gate * created but it might be have been consumed.
6617c478bd9Sstevel@tonic-gate */
6620f1702c5SYu Xiangning if (sti->sti_unbind_mp == NULL) {
6637c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sobind: allocating unbind_req\n"));
6647c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
6650f1702c5SYu Xiangning sti->sti_unbind_mp =
666de8c4a14SErik Nordmark soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP,
667de8c4a14SErik Nordmark cr);
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate if (flags & _SOBIND_REBIND) {
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate * Called from solisten after doing an sotpi_unbind() or
6737c478bd9Sstevel@tonic-gate * potentially without the unbind (latter for AF_INET{,6}).
6747c478bd9Sstevel@tonic-gate */
6757c478bd9Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0);
6767c478bd9Sstevel@tonic-gate
6777c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
6780f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp);
6790f1702c5SYu Xiangning addr = &sti->sti_ux_laddr;
6800f1702c5SYu Xiangning addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
681fc80c0dfSnordmark dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, "
682fc80c0dfSnordmark "addr 0x%p, vp %p\n",
6837c478bd9Sstevel@tonic-gate addrlen,
684903a11ebSrh87107 (void *)((struct so_ux_addr *)addr)->soua_vp,
6850f1702c5SYu Xiangning (void *)sti->sti_ux_bound_vp));
6867c478bd9Sstevel@tonic-gate } else {
6870f1702c5SYu Xiangning addr = sti->sti_laddr_sa;
6880f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_laddr_len;
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate } else if (flags & _SOBIND_UNSPEC) {
6917c478bd9Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0);
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate * The caller checked SS_ISBOUND but not necessarily
6957c478bd9Sstevel@tonic-gate * under so_lock
6967c478bd9Sstevel@tonic-gate */
6977c478bd9Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) {
6987c478bd9Sstevel@tonic-gate /* No error */
6997c478bd9Sstevel@tonic-gate goto done;
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate /* Set an initial local address */
7037c478bd9Sstevel@tonic-gate switch (so->so_family) {
7047c478bd9Sstevel@tonic-gate case AF_UNIX:
7057c478bd9Sstevel@tonic-gate /*
7067c478bd9Sstevel@tonic-gate * Use an address with same size as struct sockaddr
7077c478bd9Sstevel@tonic-gate * just like BSD.
7087c478bd9Sstevel@tonic-gate */
7090f1702c5SYu Xiangning sti->sti_laddr_len =
7107c478bd9Sstevel@tonic-gate (socklen_t)sizeof (struct sockaddr);
7110f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7120f1702c5SYu Xiangning bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7130f1702c5SYu Xiangning sti->sti_laddr_sa->sa_family = so->so_family;
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate /*
7167c478bd9Sstevel@tonic-gate * Pass down an address with the implicit bind
7177c478bd9Sstevel@tonic-gate * magic number and the rest all zeros.
7187c478bd9Sstevel@tonic-gate * The transport will return a unique address.
7197c478bd9Sstevel@tonic-gate */
7200f1702c5SYu Xiangning sti->sti_ux_laddr.soua_vp = NULL;
7210f1702c5SYu Xiangning sti->sti_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT;
7220f1702c5SYu Xiangning addr = &sti->sti_ux_laddr;
7230f1702c5SYu Xiangning addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
7247c478bd9Sstevel@tonic-gate break;
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate case AF_INET:
7277c478bd9Sstevel@tonic-gate case AF_INET6:
7287c478bd9Sstevel@tonic-gate /*
7297c478bd9Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address.
7307c478bd9Sstevel@tonic-gate * Set the address in sockfs to have the sa_family.
7317c478bd9Sstevel@tonic-gate */
7320f1702c5SYu Xiangning sti->sti_laddr_len = (so->so_family == AF_INET) ?
7337c478bd9Sstevel@tonic-gate (socklen_t)sizeof (sin_t) :
7347c478bd9Sstevel@tonic-gate (socklen_t)sizeof (sin6_t);
7350f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7360f1702c5SYu Xiangning bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7370f1702c5SYu Xiangning sti->sti_laddr_sa->sa_family = so->so_family;
7387c478bd9Sstevel@tonic-gate addr = NULL;
7397c478bd9Sstevel@tonic-gate addrlen = 0;
7407c478bd9Sstevel@tonic-gate break;
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate default:
7437c478bd9Sstevel@tonic-gate /*
7447c478bd9Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address.
7457c478bd9Sstevel@tonic-gate * Set the address in sockfs to be zero length.
7467c478bd9Sstevel@tonic-gate *
7477c478bd9Sstevel@tonic-gate * Can not assume there is a sa_family for all
7487c478bd9Sstevel@tonic-gate * protocol families. For example, AF_X25 does not
7497c478bd9Sstevel@tonic-gate * have a family field.
7507c478bd9Sstevel@tonic-gate */
7510f1702c5SYu Xiangning bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7520f1702c5SYu Xiangning sti->sti_laddr_len = 0; /* XXX correct? */
7537c478bd9Sstevel@tonic-gate addr = NULL;
7547c478bd9Sstevel@tonic-gate addrlen = 0;
7557c478bd9Sstevel@tonic-gate break;
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate } else {
7597c478bd9Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) {
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate * If it is ok to rebind the socket, first unbind
7627c478bd9Sstevel@tonic-gate * with the transport. A rebind to the NULL address
7637c478bd9Sstevel@tonic-gate * is interpreted as an unbind.
7647c478bd9Sstevel@tonic-gate * Note that a bind to NULL in BSD does unbind the
7657c478bd9Sstevel@tonic-gate * socket but it fails with EINVAL.
7667c478bd9Sstevel@tonic-gate * Note that regular sockets set SOV_SOCKBSD i.e.
7677c478bd9Sstevel@tonic-gate * _SOBIND_SOCKBSD gets set here hence no type of
7687c478bd9Sstevel@tonic-gate * socket does currently allow rebinding.
7697c478bd9Sstevel@tonic-gate *
7707c478bd9Sstevel@tonic-gate * If the name is NULL just do an unbind.
7717c478bd9Sstevel@tonic-gate */
7727c478bd9Sstevel@tonic-gate if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) &&
7737c478bd9Sstevel@tonic-gate name != NULL) {
7747c478bd9Sstevel@tonic-gate error = EINVAL;
7757c478bd9Sstevel@tonic-gate unbind_on_err = 0;
7767c478bd9Sstevel@tonic-gate eprintsoline(so, error);
7777c478bd9Sstevel@tonic-gate goto done;
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate if ((so->so_mode & SM_CONNREQUIRED) &&
7807c478bd9Sstevel@tonic-gate (so->so_state & SS_CANTREBIND)) {
7817c478bd9Sstevel@tonic-gate error = EINVAL;
7827c478bd9Sstevel@tonic-gate unbind_on_err = 0;
7837c478bd9Sstevel@tonic-gate eprintsoline(so, error);
7847c478bd9Sstevel@tonic-gate goto done;
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate error = sotpi_unbind(so, 0);
7877c478bd9Sstevel@tonic-gate if (error) {
7887c478bd9Sstevel@tonic-gate eprintsoline(so, error);
7897c478bd9Sstevel@tonic-gate goto done;
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND));
7927c478bd9Sstevel@tonic-gate if (name == NULL) {
7937c478bd9Sstevel@tonic-gate so->so_state &=
7947c478bd9Sstevel@tonic-gate ~(SS_ISCONNECTED|SS_ISCONNECTING);
7957c478bd9Sstevel@tonic-gate goto done;
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate }
7980f1702c5SYu Xiangning
7997c478bd9Sstevel@tonic-gate /* X/Open requires this check */
8007c478bd9Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
8017c478bd9Sstevel@tonic-gate if (xnet_check_print) {
8027c478bd9Sstevel@tonic-gate printf("sockfs: X/Open bind state check "
8037c478bd9Sstevel@tonic-gate "caused EINVAL\n");
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate error = EINVAL;
8067c478bd9Sstevel@tonic-gate goto done;
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate switch (so->so_family) {
8107c478bd9Sstevel@tonic-gate case AF_UNIX:
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate * All AF_UNIX addresses are nul terminated
8137c478bd9Sstevel@tonic-gate * when copied (copyin_name) in so the minimum
8147c478bd9Sstevel@tonic-gate * length is 3 bytes.
8157c478bd9Sstevel@tonic-gate */
8167c478bd9Sstevel@tonic-gate if (name == NULL ||
8177c478bd9Sstevel@tonic-gate (ssize_t)namelen <= sizeof (short) + 1) {
8187c478bd9Sstevel@tonic-gate error = EISDIR;
8197c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8207c478bd9Sstevel@tonic-gate goto done;
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate * Verify so_family matches the bound family.
8247c478bd9Sstevel@tonic-gate * BSD does not check this for AF_UNIX resulting
8257c478bd9Sstevel@tonic-gate * in funny mknods.
8267c478bd9Sstevel@tonic-gate */
8277c478bd9Sstevel@tonic-gate if (name->sa_family != so->so_family) {
8287c478bd9Sstevel@tonic-gate error = EAFNOSUPPORT;
8297c478bd9Sstevel@tonic-gate goto done;
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate break;
8327c478bd9Sstevel@tonic-gate case AF_INET:
8337c478bd9Sstevel@tonic-gate if (name == NULL) {
8347c478bd9Sstevel@tonic-gate error = EINVAL;
8357c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8367c478bd9Sstevel@tonic-gate goto done;
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin_t)) {
8397c478bd9Sstevel@tonic-gate error = name->sa_family != so->so_family ?
8407c478bd9Sstevel@tonic-gate EAFNOSUPPORT : EINVAL;
8417c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8427c478bd9Sstevel@tonic-gate goto done;
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate if ((flags & _SOBIND_XPG4_2) &&
8457c478bd9Sstevel@tonic-gate (name->sa_family != so->so_family)) {
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate * This check has to be made for X/Open
8487c478bd9Sstevel@tonic-gate * sockets however application failures have
8497c478bd9Sstevel@tonic-gate * been observed when it is applied to
8507c478bd9Sstevel@tonic-gate * all sockets.
8517c478bd9Sstevel@tonic-gate */
8527c478bd9Sstevel@tonic-gate error = EAFNOSUPPORT;
8537c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8547c478bd9Sstevel@tonic-gate goto done;
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate /*
8577c478bd9Sstevel@tonic-gate * Force a zero sa_family to match so_family.
8587c478bd9Sstevel@tonic-gate *
8597c478bd9Sstevel@tonic-gate * Some programs like inetd(1M) don't set the
8607c478bd9Sstevel@tonic-gate * family field. Other programs leave
8617c478bd9Sstevel@tonic-gate * sin_family set to garbage - SunOS 4.X does
8627c478bd9Sstevel@tonic-gate * not check the family field on a bind.
8637c478bd9Sstevel@tonic-gate * We use the family field that
8647c478bd9Sstevel@tonic-gate * was passed in to the socket() call.
8657c478bd9Sstevel@tonic-gate */
8667c478bd9Sstevel@tonic-gate name->sa_family = so->so_family;
8677c478bd9Sstevel@tonic-gate break;
8687c478bd9Sstevel@tonic-gate
8697c478bd9Sstevel@tonic-gate case AF_INET6: {
8707c478bd9Sstevel@tonic-gate #ifdef DEBUG
8717c478bd9Sstevel@tonic-gate sin6_t *sin6 = (sin6_t *)name;
8727c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8737c478bd9Sstevel@tonic-gate
8747c478bd9Sstevel@tonic-gate if (name == NULL) {
8757c478bd9Sstevel@tonic-gate error = EINVAL;
8767c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8777c478bd9Sstevel@tonic-gate goto done;
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin6_t)) {
8807c478bd9Sstevel@tonic-gate error = name->sa_family != so->so_family ?
8817c478bd9Sstevel@tonic-gate EAFNOSUPPORT : EINVAL;
8827c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8837c478bd9Sstevel@tonic-gate goto done;
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate if (name->sa_family != so->so_family) {
8867c478bd9Sstevel@tonic-gate /*
8877c478bd9Sstevel@tonic-gate * With IPv6 we require the family to match
8887c478bd9Sstevel@tonic-gate * unlike in IPv4.
8897c478bd9Sstevel@tonic-gate */
8907c478bd9Sstevel@tonic-gate error = EAFNOSUPPORT;
8917c478bd9Sstevel@tonic-gate eprintsoline(so, error);
8927c478bd9Sstevel@tonic-gate goto done;
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate #ifdef DEBUG
8957c478bd9Sstevel@tonic-gate /*
8967c478bd9Sstevel@tonic-gate * Verify that apps don't forget to clear
8977c478bd9Sstevel@tonic-gate * sin6_scope_id etc
8987c478bd9Sstevel@tonic-gate */
8997c478bd9Sstevel@tonic-gate if (sin6->sin6_scope_id != 0 &&
9007c478bd9Sstevel@tonic-gate !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
9012caf0dcdSrshoaib zcmn_err(getzoneid(), CE_WARN,
9027c478bd9Sstevel@tonic-gate "bind with uninitialized sin6_scope_id "
9037c478bd9Sstevel@tonic-gate "(%d) on socket. Pid = %d\n",
9047c478bd9Sstevel@tonic-gate (int)sin6->sin6_scope_id,
9057c478bd9Sstevel@tonic-gate (int)curproc->p_pid);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate if (sin6->__sin6_src_id != 0) {
9082caf0dcdSrshoaib zcmn_err(getzoneid(), CE_WARN,
9097c478bd9Sstevel@tonic-gate "bind with uninitialized __sin6_src_id "
9107c478bd9Sstevel@tonic-gate "(%d) on socket. Pid = %d\n",
9117c478bd9Sstevel@tonic-gate (int)sin6->__sin6_src_id,
9127c478bd9Sstevel@tonic-gate (int)curproc->p_pid);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate #endif /* DEBUG */
9157c478bd9Sstevel@tonic-gate break;
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate default:
9187c478bd9Sstevel@tonic-gate /*
9197c478bd9Sstevel@tonic-gate * Don't do any length or sa_family check to allow
9207c478bd9Sstevel@tonic-gate * non-sockaddr style addresses.
9217c478bd9Sstevel@tonic-gate */
9227c478bd9Sstevel@tonic-gate if (name == NULL) {
9237c478bd9Sstevel@tonic-gate error = EINVAL;
9247c478bd9Sstevel@tonic-gate eprintsoline(so, error);
9257c478bd9Sstevel@tonic-gate goto done;
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate break;
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate
9300f1702c5SYu Xiangning if (namelen > (t_uscalar_t)sti->sti_laddr_maxlen) {
9317c478bd9Sstevel@tonic-gate error = ENAMETOOLONG;
9327c478bd9Sstevel@tonic-gate eprintsoline(so, error);
9337c478bd9Sstevel@tonic-gate goto done;
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate /*
9367c478bd9Sstevel@tonic-gate * Save local address.
9377c478bd9Sstevel@tonic-gate */
9380f1702c5SYu Xiangning sti->sti_laddr_len = (socklen_t)namelen;
9390f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
9400f1702c5SYu Xiangning bcopy(name, sti->sti_laddr_sa, namelen);
9417c478bd9Sstevel@tonic-gate
9420f1702c5SYu Xiangning addr = sti->sti_laddr_sa;
9430f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_laddr_len;
9447c478bd9Sstevel@tonic-gate switch (so->so_family) {
9457c478bd9Sstevel@tonic-gate case AF_INET6:
9467c478bd9Sstevel@tonic-gate case AF_INET:
9477c478bd9Sstevel@tonic-gate break;
9487c478bd9Sstevel@tonic-gate case AF_UNIX: {
9497c478bd9Sstevel@tonic-gate struct sockaddr_un *soun =
9500f1702c5SYu Xiangning (struct sockaddr_un *)sti->sti_laddr_sa;
95192f45f6dSRic Aleshire struct vnode *vp, *rvp;
9527c478bd9Sstevel@tonic-gate struct vattr vattr;
9537c478bd9Sstevel@tonic-gate
9540f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
9557c478bd9Sstevel@tonic-gate /*
9567c478bd9Sstevel@tonic-gate * Create vnode for the specified path name.
9570f1702c5SYu Xiangning * Keep vnode held with a reference in sti_ux_bound_vp.
9587c478bd9Sstevel@tonic-gate * Use the vnode pointer as the address used in the
9597c478bd9Sstevel@tonic-gate * bind with the transport.
9607c478bd9Sstevel@tonic-gate *
9617c478bd9Sstevel@tonic-gate * Use the same mode as in BSD. In particular this does
9627c478bd9Sstevel@tonic-gate * not observe the umask.
9637c478bd9Sstevel@tonic-gate */
9647c478bd9Sstevel@tonic-gate /* MAXPATHLEN + soun_family + nul termination */
9650f1702c5SYu Xiangning if (sti->sti_laddr_len >
9667c478bd9Sstevel@tonic-gate (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
9677c478bd9Sstevel@tonic-gate error = ENAMETOOLONG;
9687c478bd9Sstevel@tonic-gate eprintsoline(so, error);
9697c478bd9Sstevel@tonic-gate goto done;
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate vattr.va_type = VSOCK;
972ae115bc7Smrj vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask;
9737c478bd9Sstevel@tonic-gate vattr.va_mask = AT_TYPE|AT_MODE;
9747c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock */
9757c478bd9Sstevel@tonic-gate error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr,
9767c478bd9Sstevel@tonic-gate EXCL, 0, &vp, CRMKNOD, 0, 0);
9777c478bd9Sstevel@tonic-gate if (error) {
9787c478bd9Sstevel@tonic-gate if (error == EEXIST)
9797c478bd9Sstevel@tonic-gate error = EADDRINUSE;
9807c478bd9Sstevel@tonic-gate eprintsoline(so, error);
9817c478bd9Sstevel@tonic-gate goto done;
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate /*
9847c478bd9Sstevel@tonic-gate * Establish pointer from the underlying filesystem
9857c478bd9Sstevel@tonic-gate * vnode to the socket node.
9860f1702c5SYu Xiangning * sti_ux_bound_vp and v_stream->sd_vnode form the
9877c478bd9Sstevel@tonic-gate * cross-linkage between the underlying filesystem
9887c478bd9Sstevel@tonic-gate * node and the socket node.
9897c478bd9Sstevel@tonic-gate */
99092f45f6dSRic Aleshire
99192f45f6dSRic Aleshire if ((VOP_REALVP(vp, &rvp, NULL) == 0) && (vp != rvp)) {
99292f45f6dSRic Aleshire VN_HOLD(rvp);
99392f45f6dSRic Aleshire VN_RELE(vp);
99492f45f6dSRic Aleshire vp = rvp;
99592f45f6dSRic Aleshire }
99692f45f6dSRic Aleshire
9977c478bd9Sstevel@tonic-gate ASSERT(SOTOV(so)->v_stream);
9987c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock);
9997c478bd9Sstevel@tonic-gate vp->v_stream = SOTOV(so)->v_stream;
10000f1702c5SYu Xiangning sti->sti_ux_bound_vp = vp;
10017c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock);
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate /*
10047c478bd9Sstevel@tonic-gate * Use the vnode pointer value as a unique address
10057c478bd9Sstevel@tonic-gate * (together with the magic number to avoid conflicts
10067c478bd9Sstevel@tonic-gate * with implicit binds) in the transport provider.
10077c478bd9Sstevel@tonic-gate */
10080f1702c5SYu Xiangning sti->sti_ux_laddr.soua_vp =
10090f1702c5SYu Xiangning (void *)sti->sti_ux_bound_vp;
10100f1702c5SYu Xiangning sti->sti_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT;
10110f1702c5SYu Xiangning addr = &sti->sti_ux_laddr;
10120f1702c5SYu Xiangning addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
10137c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n",
10147c478bd9Sstevel@tonic-gate addrlen,
10150f1702c5SYu Xiangning (void *)((struct so_ux_addr *)addr)->soua_vp));
10167c478bd9Sstevel@tonic-gate break;
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate } /* end switch (so->so_family) */
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate
10217c478bd9Sstevel@tonic-gate /*
10227c478bd9Sstevel@tonic-gate * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since
10237c478bd9Sstevel@tonic-gate * the transport can start passing up T_CONN_IND messages
10247c478bd9Sstevel@tonic-gate * as soon as it receives the bind req and strsock_proto()
10257c478bd9Sstevel@tonic-gate * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs.
10267c478bd9Sstevel@tonic-gate */
10277c478bd9Sstevel@tonic-gate if (flags & _SOBIND_LISTEN) {
10287c478bd9Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0)
10297c478bd9Sstevel@tonic-gate clear_acceptconn_on_err = B_TRUE;
10307c478bd9Sstevel@tonic-gate save_so_backlog = so->so_backlog;
10317c478bd9Sstevel@tonic-gate restore_backlog_on_err = B_TRUE;
10327c478bd9Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN;
10337c478bd9Sstevel@tonic-gate so->so_backlog = backlog;
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate
10367c478bd9Sstevel@tonic-gate /*
10377c478bd9Sstevel@tonic-gate * If NL7C addr(s) have been configured check for addr/port match,
10387c478bd9Sstevel@tonic-gate * or if an implicit NL7C socket via AF_NCA mark socket as NL7C.
10397c478bd9Sstevel@tonic-gate *
10407c478bd9Sstevel@tonic-gate * NL7C supports the TCP transport only so check AF_INET and AF_INET6
10417c478bd9Sstevel@tonic-gate * family sockets only. If match mark as such.
10427c478bd9Sstevel@tonic-gate */
10432c9e429eSbrutus if (nl7c_enabled && ((addr != NULL &&
10447c478bd9Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6) &&
10457c478bd9Sstevel@tonic-gate (nl7c = nl7c_lookup_addr(addr, addrlen))) ||
10460f1702c5SYu Xiangning sti->sti_nl7c_flags == NL7C_AF_NCA)) {
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate * NL7C is not supported in non-global zones,
10497c478bd9Sstevel@tonic-gate * we enforce this restriction here.
10507c478bd9Sstevel@tonic-gate */
10517c478bd9Sstevel@tonic-gate if (so->so_zoneid == GLOBAL_ZONEID) {
10527c478bd9Sstevel@tonic-gate /* An NL7C socket, mark it */
10530f1702c5SYu Xiangning sti->sti_nl7c_flags |= NL7C_ENABLED;
10542c9e429eSbrutus if (nl7c == NULL) {
10552c9e429eSbrutus /*
10562c9e429eSbrutus * Was an AF_NCA bind() so add it to the
10572c9e429eSbrutus * addr list for reporting purposes.
10582c9e429eSbrutus */
10592c9e429eSbrutus nl7c = nl7c_add_addr(addr, addrlen);
10602c9e429eSbrutus }
10617c478bd9Sstevel@tonic-gate } else
10627c478bd9Sstevel@tonic-gate nl7c = NULL;
10637c478bd9Sstevel@tonic-gate }
10640f1702c5SYu Xiangning
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate * We send a T_BIND_REQ for TCP/UDP since we know it supports it,
10677c478bd9Sstevel@tonic-gate * for other transports we will send in a O_T_BIND_REQ.
10687c478bd9Sstevel@tonic-gate */
10697c478bd9Sstevel@tonic-gate if (tcp_udp_xport &&
10707c478bd9Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6))
10717c478bd9Sstevel@tonic-gate PRIM_type = T_BIND_REQ;
10727c478bd9Sstevel@tonic-gate
10737c478bd9Sstevel@tonic-gate bind_req.PRIM_type = PRIM_type;
10747c478bd9Sstevel@tonic-gate bind_req.ADDR_length = addrlen;
10757c478bd9Sstevel@tonic-gate bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
10767c478bd9Sstevel@tonic-gate bind_req.CONIND_number = backlog;
10777c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
10787c478bd9Sstevel@tonic-gate mp = soallocproto2(&bind_req, sizeof (bind_req),
1079de8c4a14SErik Nordmark addr, addrlen, 0, _ALLOC_SLEEP, cr);
10800f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
1081c28749e9Skais
10820f1702c5SYu Xiangning /* Done using sti_laddr_sa - can drop the lock */
10837c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
10847c478bd9Sstevel@tonic-gate
10857c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
10867c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
10877c478bd9Sstevel@tonic-gate if (error) {
10887c478bd9Sstevel@tonic-gate eprintsoline(so, error);
10897c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
10907c478bd9Sstevel@tonic-gate goto done;
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate
10937c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
10947c478bd9Sstevel@tonic-gate error = sowaitprim(so, PRIM_type, T_BIND_ACK,
10957c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (*bind_ack), &mp, 0);
10967c478bd9Sstevel@tonic-gate if (error) {
10977c478bd9Sstevel@tonic-gate eprintsoline(so, error);
10987c478bd9Sstevel@tonic-gate goto done;
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate ASSERT(mp);
11017c478bd9Sstevel@tonic-gate /*
11027c478bd9Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in
11037c478bd9Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the bind
11047c478bd9Sstevel@tonic-gate * is allowed to complete.
11057c478bd9Sstevel@tonic-gate */
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate /* Mark as bound. This will be undone if we detect errors below. */
11087c478bd9Sstevel@tonic-gate if (flags & _SOBIND_NOXLATE) {
11097c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX);
11100f1702c5SYu Xiangning sti->sti_faddr_noxlate = 1;
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND));
11137c478bd9Sstevel@tonic-gate so->so_state |= SS_ISBOUND;
11140f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp);
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate /* note that we've already set SS_ACCEPTCONN above */
11177c478bd9Sstevel@tonic-gate
11187c478bd9Sstevel@tonic-gate /*
11197c478bd9Sstevel@tonic-gate * Recompute addrlen - an unspecied bind sent down an
11207c478bd9Sstevel@tonic-gate * address of length zero but we expect the appropriate length
11217c478bd9Sstevel@tonic-gate * in return.
11227c478bd9Sstevel@tonic-gate */
11237c478bd9Sstevel@tonic-gate addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ?
11240f1702c5SYu Xiangning sizeof (sti->sti_ux_laddr) : sti->sti_laddr_len);
11257c478bd9Sstevel@tonic-gate
11267c478bd9Sstevel@tonic-gate bind_ack = (struct T_bind_ack *)mp->b_rptr;
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate * The alignment restriction is really too strict but
11297c478bd9Sstevel@tonic-gate * we want enough alignment to inspect the fields of
11307c478bd9Sstevel@tonic-gate * a sockaddr_in.
11317c478bd9Sstevel@tonic-gate */
11327c478bd9Sstevel@tonic-gate addr = sogetoff(mp, bind_ack->ADDR_offset,
11337c478bd9Sstevel@tonic-gate bind_ack->ADDR_length,
11347c478bd9Sstevel@tonic-gate __TPI_ALIGN_SIZE);
11357c478bd9Sstevel@tonic-gate if (addr == NULL) {
11367c478bd9Sstevel@tonic-gate freemsg(mp);
11377c478bd9Sstevel@tonic-gate error = EPROTO;
11387c478bd9Sstevel@tonic-gate eprintsoline(so, error);
11397c478bd9Sstevel@tonic-gate goto done;
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate if (!(flags & _SOBIND_UNSPEC)) {
11427c478bd9Sstevel@tonic-gate /*
11437c478bd9Sstevel@tonic-gate * Verify that the transport didn't return something we
11447c478bd9Sstevel@tonic-gate * did not want e.g. an address other than what we asked for.
11457c478bd9Sstevel@tonic-gate *
11467c478bd9Sstevel@tonic-gate * NOTE: These checks would go away if/when we switch to
11477c478bd9Sstevel@tonic-gate * using the new TPI (in which the transport would fail
11487c478bd9Sstevel@tonic-gate * the request instead of assigning a different address).
11497c478bd9Sstevel@tonic-gate *
11507c478bd9Sstevel@tonic-gate * NOTE2: For protocols that we don't know (i.e. any
11517c478bd9Sstevel@tonic-gate * other than AF_INET6, AF_INET and AF_UNIX), we
11527c478bd9Sstevel@tonic-gate * cannot know if the transport should be expected to
11537c478bd9Sstevel@tonic-gate * return the same address as that requested.
11547c478bd9Sstevel@tonic-gate *
11557c478bd9Sstevel@tonic-gate * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send
11567c478bd9Sstevel@tonic-gate * down a T_BIND_REQ. We use O_T_BIND_REQ for others.
11577c478bd9Sstevel@tonic-gate *
11587c478bd9Sstevel@tonic-gate * For example, in the case of netatalk it may be
11597c478bd9Sstevel@tonic-gate * inappropriate for the transport to return the
11607c478bd9Sstevel@tonic-gate * requested address (as it may have allocated a local
11617c478bd9Sstevel@tonic-gate * port number in behaviour similar to that of an
11627c478bd9Sstevel@tonic-gate * AF_INET bind request with a port number of zero).
11637c478bd9Sstevel@tonic-gate *
11647c478bd9Sstevel@tonic-gate * Given the definition of O_T_BIND_REQ, where the
11657c478bd9Sstevel@tonic-gate * transport may bind to an address other than the
11667c478bd9Sstevel@tonic-gate * requested address, it's not possible to determine
11677c478bd9Sstevel@tonic-gate * whether a returned address that differs from the
11687c478bd9Sstevel@tonic-gate * requested address is a reason to fail (because the
11697c478bd9Sstevel@tonic-gate * requested address was not available) or succeed
11707c478bd9Sstevel@tonic-gate * (because the transport allocated an appropriate
11717c478bd9Sstevel@tonic-gate * address and/or port).
11727c478bd9Sstevel@tonic-gate *
11737c478bd9Sstevel@tonic-gate * sockfs currently requires that the transport return
11747c478bd9Sstevel@tonic-gate * the requested address in the T_BIND_ACK, unless
11757c478bd9Sstevel@tonic-gate * there is code here to allow for any discrepancy.
11767c478bd9Sstevel@tonic-gate * Such code exists for AF_INET and AF_INET6.
11777c478bd9Sstevel@tonic-gate *
11787c478bd9Sstevel@tonic-gate * Netatalk chooses to return the requested address
11797c478bd9Sstevel@tonic-gate * rather than the (correct) allocated address. This
11807c478bd9Sstevel@tonic-gate * means that netatalk violates the TPI specification
11817c478bd9Sstevel@tonic-gate * (and would not function correctly if used from a
11827c478bd9Sstevel@tonic-gate * TLI application), but it does mean that it works
11837c478bd9Sstevel@tonic-gate * with sockfs.
11847c478bd9Sstevel@tonic-gate *
11857c478bd9Sstevel@tonic-gate * As noted above, using the newer XTI bind primitive
11867c478bd9Sstevel@tonic-gate * (T_BIND_REQ) in preference to O_T_BIND_REQ would
11877c478bd9Sstevel@tonic-gate * allow sockfs to be more sure about whether or not
11887c478bd9Sstevel@tonic-gate * the bind request had succeeded (as transports are
11897c478bd9Sstevel@tonic-gate * not permitted to bind to a different address than
11907c478bd9Sstevel@tonic-gate * that requested - they must return failure).
11917c478bd9Sstevel@tonic-gate * Unfortunately, support for T_BIND_REQ may not be
11927c478bd9Sstevel@tonic-gate * present in all transport implementations (netatalk,
11937c478bd9Sstevel@tonic-gate * for example, doesn't have it), making the
11947c478bd9Sstevel@tonic-gate * transition difficult.
11957c478bd9Sstevel@tonic-gate */
11967c478bd9Sstevel@tonic-gate if (bind_ack->ADDR_length != addrlen) {
11977c478bd9Sstevel@tonic-gate /* Assumes that the requested address was in use */
11987c478bd9Sstevel@tonic-gate freemsg(mp);
11997c478bd9Sstevel@tonic-gate error = EADDRINUSE;
12007c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12017c478bd9Sstevel@tonic-gate goto done;
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate
12047c478bd9Sstevel@tonic-gate switch (so->so_family) {
12057c478bd9Sstevel@tonic-gate case AF_INET6:
12067c478bd9Sstevel@tonic-gate case AF_INET: {
12077c478bd9Sstevel@tonic-gate sin_t *rname, *aname;
12087c478bd9Sstevel@tonic-gate
12097c478bd9Sstevel@tonic-gate rname = (sin_t *)addr;
12100f1702c5SYu Xiangning aname = (sin_t *)sti->sti_laddr_sa;
12117c478bd9Sstevel@tonic-gate
12127c478bd9Sstevel@tonic-gate /*
12137c478bd9Sstevel@tonic-gate * Take advantage of the alignment
12147c478bd9Sstevel@tonic-gate * of sin_port and sin6_port which fall
12157c478bd9Sstevel@tonic-gate * in the same place in their data structures.
12167c478bd9Sstevel@tonic-gate * Just use sin_port for either address family.
12177c478bd9Sstevel@tonic-gate *
12187c478bd9Sstevel@tonic-gate * This may become a problem if (heaven forbid)
12197c478bd9Sstevel@tonic-gate * there's a separate ipv6port_reserved... :-P
12207c478bd9Sstevel@tonic-gate *
12217c478bd9Sstevel@tonic-gate * Binding to port 0 has the semantics of letting
12227c478bd9Sstevel@tonic-gate * the transport bind to any port.
12237c478bd9Sstevel@tonic-gate *
12247c478bd9Sstevel@tonic-gate * If the transport is TCP or UDP since we had sent
12257c478bd9Sstevel@tonic-gate * a T_BIND_REQ we would not get a port other than
12267c478bd9Sstevel@tonic-gate * what we asked for.
12277c478bd9Sstevel@tonic-gate */
12287c478bd9Sstevel@tonic-gate if (tcp_udp_xport) {
12297c478bd9Sstevel@tonic-gate /*
12307c478bd9Sstevel@tonic-gate * Pick up the new port number if we bound to
12317c478bd9Sstevel@tonic-gate * port 0.
12327c478bd9Sstevel@tonic-gate */
12337c478bd9Sstevel@tonic-gate if (aname->sin_port == 0)
12347c478bd9Sstevel@tonic-gate aname->sin_port = rname->sin_port;
12350f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
12367c478bd9Sstevel@tonic-gate break;
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate if (aname->sin_port != 0 &&
12397c478bd9Sstevel@tonic-gate aname->sin_port != rname->sin_port) {
12407c478bd9Sstevel@tonic-gate freemsg(mp);
12417c478bd9Sstevel@tonic-gate error = EADDRINUSE;
12427c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12437c478bd9Sstevel@tonic-gate goto done;
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate /*
12467c478bd9Sstevel@tonic-gate * Pick up the new port number if we bound to port 0.
12477c478bd9Sstevel@tonic-gate */
12487c478bd9Sstevel@tonic-gate aname->sin_port = rname->sin_port;
12497c478bd9Sstevel@tonic-gate
12507c478bd9Sstevel@tonic-gate /*
12517c478bd9Sstevel@tonic-gate * Unfortunately, addresses aren't _quite_ the same.
12527c478bd9Sstevel@tonic-gate */
12537c478bd9Sstevel@tonic-gate if (so->so_family == AF_INET) {
12547c478bd9Sstevel@tonic-gate if (aname->sin_addr.s_addr !=
12557c478bd9Sstevel@tonic-gate rname->sin_addr.s_addr) {
12567c478bd9Sstevel@tonic-gate freemsg(mp);
12577c478bd9Sstevel@tonic-gate error = EADDRNOTAVAIL;
12587c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12597c478bd9Sstevel@tonic-gate goto done;
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate } else {
12627c478bd9Sstevel@tonic-gate sin6_t *rname6 = (sin6_t *)rname;
12637c478bd9Sstevel@tonic-gate sin6_t *aname6 = (sin6_t *)aname;
12647c478bd9Sstevel@tonic-gate
12657c478bd9Sstevel@tonic-gate if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr,
12667c478bd9Sstevel@tonic-gate &rname6->sin6_addr)) {
12677c478bd9Sstevel@tonic-gate freemsg(mp);
12687c478bd9Sstevel@tonic-gate error = EADDRNOTAVAIL;
12697c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12707c478bd9Sstevel@tonic-gate goto done;
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate break;
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate case AF_UNIX:
12760f1702c5SYu Xiangning if (bcmp(addr, &sti->sti_ux_laddr, addrlen) != 0) {
12777c478bd9Sstevel@tonic-gate freemsg(mp);
12787c478bd9Sstevel@tonic-gate error = EADDRINUSE;
12797c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12807c478bd9Sstevel@tonic-gate eprintso(so,
12817c478bd9Sstevel@tonic-gate ("addrlen %d, addr 0x%x, vp %p\n",
12827c478bd9Sstevel@tonic-gate addrlen, *((int *)addr),
12830f1702c5SYu Xiangning (void *)sti->sti_ux_bound_vp));
12847c478bd9Sstevel@tonic-gate goto done;
12857c478bd9Sstevel@tonic-gate }
12860f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
12877c478bd9Sstevel@tonic-gate break;
12887c478bd9Sstevel@tonic-gate default:
12897c478bd9Sstevel@tonic-gate /*
12907c478bd9Sstevel@tonic-gate * NOTE: This assumes that addresses can be
12917c478bd9Sstevel@tonic-gate * byte-compared for equivalence.
12927c478bd9Sstevel@tonic-gate */
12930f1702c5SYu Xiangning if (bcmp(addr, sti->sti_laddr_sa, addrlen) != 0) {
12947c478bd9Sstevel@tonic-gate freemsg(mp);
12957c478bd9Sstevel@tonic-gate error = EADDRINUSE;
12967c478bd9Sstevel@tonic-gate eprintsoline(so, error);
12977c478bd9Sstevel@tonic-gate goto done;
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate /*
13000f1702c5SYu Xiangning * Don't mark sti_laddr_valid, as we cannot be
13017c478bd9Sstevel@tonic-gate * sure that the returned address is the real
13027c478bd9Sstevel@tonic-gate * bound address when talking to an unknown
13037c478bd9Sstevel@tonic-gate * transport.
13047c478bd9Sstevel@tonic-gate */
13057c478bd9Sstevel@tonic-gate break;
13067c478bd9Sstevel@tonic-gate }
13077c478bd9Sstevel@tonic-gate } else {
13087c478bd9Sstevel@tonic-gate /*
13097c478bd9Sstevel@tonic-gate * Save for returned address for getsockname.
13107c478bd9Sstevel@tonic-gate * Needed for unspecific bind unless transport supports
13117c478bd9Sstevel@tonic-gate * the TI_GETMYNAME ioctl.
13127c478bd9Sstevel@tonic-gate * Do this for AF_INET{,6} even though they do, as
13137c478bd9Sstevel@tonic-gate * caching info here is much better performance than
13147c478bd9Sstevel@tonic-gate * a TPI/STREAMS trip to the transport for getsockname.
13157c478bd9Sstevel@tonic-gate * Any which can't for some reason _must_ _not_ set
13160f1702c5SYu Xiangning * sti_laddr_valid here for the caching version of
13170f1702c5SYu Xiangning * getsockname to not break;
13187c478bd9Sstevel@tonic-gate */
13197c478bd9Sstevel@tonic-gate switch (so->so_family) {
13207c478bd9Sstevel@tonic-gate case AF_UNIX:
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate * Record the address bound with the transport
13237c478bd9Sstevel@tonic-gate * for use by socketpair.
13247c478bd9Sstevel@tonic-gate */
13250f1702c5SYu Xiangning bcopy(addr, &sti->sti_ux_laddr, addrlen);
13260f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
13277c478bd9Sstevel@tonic-gate break;
13287c478bd9Sstevel@tonic-gate case AF_INET:
13297c478bd9Sstevel@tonic-gate case AF_INET6:
13300f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
13310f1702c5SYu Xiangning bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
13320f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
13337c478bd9Sstevel@tonic-gate break;
13347c478bd9Sstevel@tonic-gate default:
13357c478bd9Sstevel@tonic-gate /*
13360f1702c5SYu Xiangning * Don't mark sti_laddr_valid, as we cannot be
13377c478bd9Sstevel@tonic-gate * sure that the returned address is the real
13387c478bd9Sstevel@tonic-gate * bound address when talking to an unknown
13397c478bd9Sstevel@tonic-gate * transport.
13407c478bd9Sstevel@tonic-gate */
13417c478bd9Sstevel@tonic-gate break;
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate
13457c478bd9Sstevel@tonic-gate if (nl7c != NULL) {
13462c9e429eSbrutus /* Register listen()er sonode pointer with NL7C */
13472c9e429eSbrutus nl7c_listener_addr(nl7c, so);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate freemsg(mp);
13517c478bd9Sstevel@tonic-gate
13527c478bd9Sstevel@tonic-gate done:
13537c478bd9Sstevel@tonic-gate if (error) {
13547c478bd9Sstevel@tonic-gate /* reset state & backlog to values held on entry */
13557c478bd9Sstevel@tonic-gate if (clear_acceptconn_on_err == B_TRUE)
13567c478bd9Sstevel@tonic-gate so->so_state &= ~SS_ACCEPTCONN;
13577c478bd9Sstevel@tonic-gate if (restore_backlog_on_err == B_TRUE)
13587c478bd9Sstevel@tonic-gate so->so_backlog = save_so_backlog;
13597c478bd9Sstevel@tonic-gate
13607c478bd9Sstevel@tonic-gate if (unbind_on_err && so->so_state & SS_ISBOUND) {
13617c478bd9Sstevel@tonic-gate int err;
13627c478bd9Sstevel@tonic-gate
13637c478bd9Sstevel@tonic-gate err = sotpi_unbind(so, 0);
13647c478bd9Sstevel@tonic-gate /* LINTED - statement has no consequent: if */
13657c478bd9Sstevel@tonic-gate if (err) {
13667c478bd9Sstevel@tonic-gate eprintsoline(so, error);
13677c478bd9Sstevel@tonic-gate } else {
13687c478bd9Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND));
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) {
13737c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
13747c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
13757c478bd9Sstevel@tonic-gate } else {
13767c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
13777c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate return (error);
13807c478bd9Sstevel@tonic-gate }
13817c478bd9Sstevel@tonic-gate
13827c478bd9Sstevel@tonic-gate /* bind the socket */
1383ff550d0eSmasputra static int
sotpi_bind(struct sonode * so,struct sockaddr * name,socklen_t namelen,int flags,struct cred * cr)13847c478bd9Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
13850f1702c5SYu Xiangning int flags, struct cred *cr)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate if ((flags & _SOBIND_SOCKETPAIR) == 0)
13880f1702c5SYu Xiangning return (sotpi_bindlisten(so, name, namelen, 0, flags, cr));
13897c478bd9Sstevel@tonic-gate
13907c478bd9Sstevel@tonic-gate flags &= ~_SOBIND_SOCKETPAIR;
13910f1702c5SYu Xiangning return (sotpi_bindlisten(so, name, namelen, 1, flags, cr));
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate
13947c478bd9Sstevel@tonic-gate /*
13957c478bd9Sstevel@tonic-gate * Unbind a socket - used when bind() fails, when bind() specifies a NULL
13967c478bd9Sstevel@tonic-gate * address, or when listen needs to unbind and bind.
13977c478bd9Sstevel@tonic-gate * If the _SOUNBIND_REBIND flag is specified the addresses are retained
13987c478bd9Sstevel@tonic-gate * so that a sobind can pick them up.
13997c478bd9Sstevel@tonic-gate */
14007c478bd9Sstevel@tonic-gate static int
sotpi_unbind(struct sonode * so,int flags)14017c478bd9Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags)
14027c478bd9Sstevel@tonic-gate {
14037c478bd9Sstevel@tonic-gate struct T_unbind_req unbind_req;
14047c478bd9Sstevel@tonic-gate int error = 0;
14057c478bd9Sstevel@tonic-gate mblk_t *mp;
14060f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
14077c478bd9Sstevel@tonic-gate
14087c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n",
1409903a11ebSrh87107 (void *)so, flags, pr_state(so->so_state, so->so_mode)));
14107c478bd9Sstevel@tonic-gate
14117c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
14127c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
14137c478bd9Sstevel@tonic-gate
14147c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) {
14157c478bd9Sstevel@tonic-gate error = EINVAL;
14167c478bd9Sstevel@tonic-gate eprintsoline(so, error);
14177c478bd9Sstevel@tonic-gate goto done;
14187c478bd9Sstevel@tonic-gate }
14197c478bd9Sstevel@tonic-gate
14207c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
14217c478bd9Sstevel@tonic-gate
14227c478bd9Sstevel@tonic-gate /*
14237c478bd9Sstevel@tonic-gate * Flush the read and write side (except stream head read queue)
14247c478bd9Sstevel@tonic-gate * and send down T_UNBIND_REQ.
14257c478bd9Sstevel@tonic-gate */
14267c478bd9Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
14277c478bd9Sstevel@tonic-gate
14287c478bd9Sstevel@tonic-gate unbind_req.PRIM_type = T_UNBIND_REQ;
14297c478bd9Sstevel@tonic-gate mp = soallocproto1(&unbind_req, sizeof (unbind_req),
1430de8c4a14SErik Nordmark 0, _ALLOC_SLEEP, CRED());
14317c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
14327c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
14337c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
14347c478bd9Sstevel@tonic-gate if (error) {
14357c478bd9Sstevel@tonic-gate eprintsoline(so, error);
14367c478bd9Sstevel@tonic-gate goto done;
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate
14397c478bd9Sstevel@tonic-gate error = sowaitokack(so, T_UNBIND_REQ);
14407c478bd9Sstevel@tonic-gate if (error) {
14417c478bd9Sstevel@tonic-gate eprintsoline(so, error);
14427c478bd9Sstevel@tonic-gate goto done;
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate
14457c478bd9Sstevel@tonic-gate /*
14467c478bd9Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in
14477c478bd9Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the unbind
14487c478bd9Sstevel@tonic-gate * is allowed to complete.
14497c478bd9Sstevel@tonic-gate */
14507c478bd9Sstevel@tonic-gate if (!(flags & _SOUNBIND_REBIND)) {
14517c478bd9Sstevel@tonic-gate /*
14527c478bd9Sstevel@tonic-gate * Clear out bound address.
14537c478bd9Sstevel@tonic-gate */
14547c478bd9Sstevel@tonic-gate vnode_t *vp;
14557c478bd9Sstevel@tonic-gate
14560f1702c5SYu Xiangning if ((vp = sti->sti_ux_bound_vp) != NULL) {
14570f1702c5SYu Xiangning sti->sti_ux_bound_vp = NULL;
14587c478bd9Sstevel@tonic-gate vn_rele_stream(vp);
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate /* Clear out address */
14610f1702c5SYu Xiangning sti->sti_laddr_len = 0;
14627c478bd9Sstevel@tonic-gate }
14630f1702c5SYu Xiangning so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN);
14640f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
14652c9e429eSbrutus
14667c478bd9Sstevel@tonic-gate done:
1467c28749e9Skais
14687c478bd9Sstevel@tonic-gate /* If the caller held the lock don't release it here */
14697c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
14707c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
14717c478bd9Sstevel@tonic-gate
14727c478bd9Sstevel@tonic-gate return (error);
14737c478bd9Sstevel@tonic-gate }
14747c478bd9Sstevel@tonic-gate
14757c478bd9Sstevel@tonic-gate /*
14767c478bd9Sstevel@tonic-gate * listen on the socket.
14777c478bd9Sstevel@tonic-gate * For TPI conforming transports this has to first unbind with the transport
14787c478bd9Sstevel@tonic-gate * and then bind again using the new backlog.
14797c478bd9Sstevel@tonic-gate */
14800f1702c5SYu Xiangning /* ARGSUSED */
14817c478bd9Sstevel@tonic-gate int
sotpi_listen(struct sonode * so,int backlog,struct cred * cr)14820f1702c5SYu Xiangning sotpi_listen(struct sonode *so, int backlog, struct cred *cr)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate int error = 0;
14850f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
14867c478bd9Sstevel@tonic-gate
14877c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n",
1488903a11ebSrh87107 (void *)so, backlog, pr_state(so->so_state, so->so_mode)));
14897c478bd9Sstevel@tonic-gate
14900f1702c5SYu Xiangning if (sti->sti_serv_type == T_CLTS)
14917c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
14927c478bd9Sstevel@tonic-gate
14937c478bd9Sstevel@tonic-gate /*
14947c478bd9Sstevel@tonic-gate * If the socket is ready to accept connections already, then
14957c478bd9Sstevel@tonic-gate * return without doing anything. This avoids a problem where
14967c478bd9Sstevel@tonic-gate * a second listen() call fails if a connection is pending and
14977c478bd9Sstevel@tonic-gate * leaves the socket unbound. Only when we are not unbinding
14987c478bd9Sstevel@tonic-gate * with the transport can we safely increase the backlog.
14997c478bd9Sstevel@tonic-gate */
15007c478bd9Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN &&
15017c478bd9Sstevel@tonic-gate !((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15027c478bd9Sstevel@tonic-gate /*CONSTCOND*/
15037c478bd9Sstevel@tonic-gate !solisten_tpi_tcp))
15047c478bd9Sstevel@tonic-gate return (0);
15057c478bd9Sstevel@tonic-gate
15067c478bd9Sstevel@tonic-gate if (so->so_state & SS_ISCONNECTED)
15077c478bd9Sstevel@tonic-gate return (EINVAL);
15087c478bd9Sstevel@tonic-gate
15097c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
15107c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
15117c478bd9Sstevel@tonic-gate
15127c478bd9Sstevel@tonic-gate /*
15137c478bd9Sstevel@tonic-gate * If the listen doesn't change the backlog we do nothing.
15147c478bd9Sstevel@tonic-gate * This avoids an EPROTO error from the transport.
15157c478bd9Sstevel@tonic-gate */
15167c478bd9Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) &&
15177c478bd9Sstevel@tonic-gate so->so_backlog == backlog)
15187c478bd9Sstevel@tonic-gate goto done;
15197c478bd9Sstevel@tonic-gate
15207c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) {
15217c478bd9Sstevel@tonic-gate /*
15227c478bd9Sstevel@tonic-gate * Must have been explicitly bound in the UNIX domain.
15237c478bd9Sstevel@tonic-gate */
15247c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
15257c478bd9Sstevel@tonic-gate error = EINVAL;
15267c478bd9Sstevel@tonic-gate goto done;
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog,
15290f1702c5SYu Xiangning _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15307c478bd9Sstevel@tonic-gate } else if (backlog > 0) {
15317c478bd9Sstevel@tonic-gate /*
15327c478bd9Sstevel@tonic-gate * AF_INET{,6} hack to avoid losing the port.
15337c478bd9Sstevel@tonic-gate * Assumes that all AF_INET{,6} transports can handle a
15347c478bd9Sstevel@tonic-gate * O_T_BIND_REQ with a non-zero CONIND_number when the TPI
15357c478bd9Sstevel@tonic-gate * has already bound thus it is possible to avoid the unbind.
15367c478bd9Sstevel@tonic-gate */
15377c478bd9Sstevel@tonic-gate if (!((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15387c478bd9Sstevel@tonic-gate /*CONSTCOND*/
15397c478bd9Sstevel@tonic-gate !solisten_tpi_tcp)) {
15407c478bd9Sstevel@tonic-gate error = sotpi_unbind(so, _SOUNBIND_REBIND);
15417c478bd9Sstevel@tonic-gate if (error)
15427c478bd9Sstevel@tonic-gate goto done;
15437c478bd9Sstevel@tonic-gate }
15447c478bd9Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog,
15450f1702c5SYu Xiangning _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15467c478bd9Sstevel@tonic-gate } else {
15477c478bd9Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN;
15487c478bd9Sstevel@tonic-gate so->so_backlog = backlog;
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate if (error)
15517c478bd9Sstevel@tonic-gate goto done;
15527c478bd9Sstevel@tonic-gate ASSERT(so->so_state & SS_ACCEPTCONN);
15537c478bd9Sstevel@tonic-gate done:
15547c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
15557c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
15567c478bd9Sstevel@tonic-gate return (error);
15577c478bd9Sstevel@tonic-gate }
15587c478bd9Sstevel@tonic-gate
15597c478bd9Sstevel@tonic-gate /*
15607c478bd9Sstevel@tonic-gate * Disconnect either a specified seqno or all (-1).
15617c478bd9Sstevel@tonic-gate * The former is used on listening sockets only.
15627c478bd9Sstevel@tonic-gate *
15637c478bd9Sstevel@tonic-gate * When seqno == -1 sodisconnect could call sotpi_unbind. However,
15647c478bd9Sstevel@tonic-gate * the current use of sodisconnect(seqno == -1) is only for shutdown
15657c478bd9Sstevel@tonic-gate * so there is no point (and potentially incorrect) to unbind.
15667c478bd9Sstevel@tonic-gate */
15670f1702c5SYu Xiangning static int
sodisconnect(struct sonode * so,t_scalar_t seqno,int flags)15687c478bd9Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags)
15697c478bd9Sstevel@tonic-gate {
15707c478bd9Sstevel@tonic-gate struct T_discon_req discon_req;
15717c478bd9Sstevel@tonic-gate int error = 0;
15727c478bd9Sstevel@tonic-gate mblk_t *mp;
15737c478bd9Sstevel@tonic-gate
15747c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n",
1575903a11ebSrh87107 (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode)));
15767c478bd9Sstevel@tonic-gate
15777c478bd9Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) {
15787c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
15797c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
15807c478bd9Sstevel@tonic-gate } else {
15817c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
15827c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
15837c478bd9Sstevel@tonic-gate }
15847c478bd9Sstevel@tonic-gate
15857c478bd9Sstevel@tonic-gate if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) {
15867c478bd9Sstevel@tonic-gate error = EINVAL;
15877c478bd9Sstevel@tonic-gate eprintsoline(so, error);
15887c478bd9Sstevel@tonic-gate goto done;
15897c478bd9Sstevel@tonic-gate }
15907c478bd9Sstevel@tonic-gate
15917c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
15927c478bd9Sstevel@tonic-gate /*
15937c478bd9Sstevel@tonic-gate * Flush the write side (unless this is a listener)
15947c478bd9Sstevel@tonic-gate * and then send down a T_DISCON_REQ.
15957c478bd9Sstevel@tonic-gate * (Don't flush on listener since it could flush {O_}T_CONN_RES
15967c478bd9Sstevel@tonic-gate * and other messages.)
15977c478bd9Sstevel@tonic-gate */
15987c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ACCEPTCONN))
15997c478bd9Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW);
16007c478bd9Sstevel@tonic-gate
16017c478bd9Sstevel@tonic-gate discon_req.PRIM_type = T_DISCON_REQ;
16027c478bd9Sstevel@tonic-gate discon_req.SEQ_number = seqno;
16037c478bd9Sstevel@tonic-gate mp = soallocproto1(&discon_req, sizeof (discon_req),
1604de8c4a14SErik Nordmark 0, _ALLOC_SLEEP, CRED());
16057c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
16067c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
16077c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
16087c478bd9Sstevel@tonic-gate if (error) {
16097c478bd9Sstevel@tonic-gate eprintsoline(so, error);
16107c478bd9Sstevel@tonic-gate goto done;
16117c478bd9Sstevel@tonic-gate }
16127c478bd9Sstevel@tonic-gate
16137c478bd9Sstevel@tonic-gate error = sowaitokack(so, T_DISCON_REQ);
16147c478bd9Sstevel@tonic-gate if (error) {
16157c478bd9Sstevel@tonic-gate eprintsoline(so, error);
16167c478bd9Sstevel@tonic-gate goto done;
16177c478bd9Sstevel@tonic-gate }
16187c478bd9Sstevel@tonic-gate /*
16197c478bd9Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in
16207c478bd9Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the disconnect
16217c478bd9Sstevel@tonic-gate * is allowed to complete. However, it is not possible to
16227c478bd9Sstevel@tonic-gate * assert that SS_ISCONNECTED|SS_ISCONNECTING are set.
16237c478bd9Sstevel@tonic-gate */
16240f1702c5SYu Xiangning so->so_state &= ~(SS_ISCONNECTED|SS_ISCONNECTING);
16250f1702c5SYu Xiangning SOTOTPI(so)->sti_laddr_valid = 0;
16260f1702c5SYu Xiangning SOTOTPI(so)->sti_faddr_valid = 0;
16277c478bd9Sstevel@tonic-gate done:
16287c478bd9Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) {
16297c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
16307c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
16317c478bd9Sstevel@tonic-gate } else {
16327c478bd9Sstevel@tonic-gate /* If the caller held the lock don't release it here */
16337c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
16347c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED);
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate return (error);
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate
16390f1702c5SYu Xiangning /* ARGSUSED */
16407c478bd9Sstevel@tonic-gate int
sotpi_accept(struct sonode * so,int fflag,struct cred * cr,struct sonode ** nsop)16410f1702c5SYu Xiangning sotpi_accept(struct sonode *so, int fflag, struct cred *cr,
16420f1702c5SYu Xiangning struct sonode **nsop)
16437c478bd9Sstevel@tonic-gate {
16447c478bd9Sstevel@tonic-gate struct T_conn_ind *conn_ind;
16457c478bd9Sstevel@tonic-gate struct T_conn_res *conn_res;
16467c478bd9Sstevel@tonic-gate int error = 0;
1647dd49f125SAnders Persson mblk_t *mp, *ack_mp;
16487c478bd9Sstevel@tonic-gate struct sonode *nso;
16497c478bd9Sstevel@tonic-gate vnode_t *nvp;
16507c478bd9Sstevel@tonic-gate void *src;
16517c478bd9Sstevel@tonic-gate t_uscalar_t srclen;
16527c478bd9Sstevel@tonic-gate void *opt;
16537c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
16547c478bd9Sstevel@tonic-gate t_scalar_t PRIM_type;
16557c478bd9Sstevel@tonic-gate t_scalar_t SEQ_number;
1656188658baSja97890 size_t sinlen;
16570f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
16580f1702c5SYu Xiangning sotpi_info_t *nsti;
16597c478bd9Sstevel@tonic-gate
16607c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n",
1661903a11ebSrh87107 (void *)so, fflag, (void *)nsop,
1662903a11ebSrh87107 pr_state(so->so_state, so->so_mode)));
16637c478bd9Sstevel@tonic-gate
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate * Defer single-threading the accepting socket until
16667c478bd9Sstevel@tonic-gate * the T_CONN_IND has been received and parsed and the
16677c478bd9Sstevel@tonic-gate * new sonode has been opened.
16687c478bd9Sstevel@tonic-gate */
16697c478bd9Sstevel@tonic-gate
16707c478bd9Sstevel@tonic-gate /* Check that we are not already connected */
16717c478bd9Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0)
16727c478bd9Sstevel@tonic-gate goto conn_bad;
16737c478bd9Sstevel@tonic-gate again:
16747c478bd9Sstevel@tonic-gate if ((error = sowaitconnind(so, fflag, &mp)) != 0)
16757c478bd9Sstevel@tonic-gate goto e_bad;
16767c478bd9Sstevel@tonic-gate
16770f1702c5SYu Xiangning ASSERT(mp != NULL);
16787c478bd9Sstevel@tonic-gate conn_ind = (struct T_conn_ind *)mp->b_rptr;
1679c28749e9Skais
16807c478bd9Sstevel@tonic-gate /*
16817c478bd9Sstevel@tonic-gate * Save SEQ_number for error paths.
16827c478bd9Sstevel@tonic-gate */
16837c478bd9Sstevel@tonic-gate SEQ_number = conn_ind->SEQ_number;
16847c478bd9Sstevel@tonic-gate
16857c478bd9Sstevel@tonic-gate srclen = conn_ind->SRC_length;
16867c478bd9Sstevel@tonic-gate src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1);
16877c478bd9Sstevel@tonic-gate if (src == NULL) {
16887c478bd9Sstevel@tonic-gate error = EPROTO;
16897c478bd9Sstevel@tonic-gate freemsg(mp);
16907c478bd9Sstevel@tonic-gate eprintsoline(so, error);
16917c478bd9Sstevel@tonic-gate goto disconnect_unlocked;
16927c478bd9Sstevel@tonic-gate }
16937c478bd9Sstevel@tonic-gate optlen = conn_ind->OPT_length;
16947c478bd9Sstevel@tonic-gate switch (so->so_family) {
16957c478bd9Sstevel@tonic-gate case AF_INET:
16967c478bd9Sstevel@tonic-gate case AF_INET6:
16970f1702c5SYu Xiangning if ((optlen == sizeof (intptr_t)) && (sti->sti_direct != 0)) {
16987c478bd9Sstevel@tonic-gate bcopy(mp->b_rptr + conn_ind->OPT_offset,
16997c478bd9Sstevel@tonic-gate &opt, conn_ind->OPT_length);
17007c478bd9Sstevel@tonic-gate } else {
17017c478bd9Sstevel@tonic-gate /*
17027c478bd9Sstevel@tonic-gate * The transport (in this case TCP) hasn't sent up
17037c478bd9Sstevel@tonic-gate * a pointer to an instance for the accept fast-path.
17047c478bd9Sstevel@tonic-gate * Disable fast-path completely because the call to
17057c478bd9Sstevel@tonic-gate * sotpi_create() below would otherwise create an
17067c478bd9Sstevel@tonic-gate * incomplete TCP instance, which would lead to
17077c478bd9Sstevel@tonic-gate * problems when sockfs sends a normal T_CONN_RES
17087c478bd9Sstevel@tonic-gate * message down the new stream.
17097c478bd9Sstevel@tonic-gate */
17100f1702c5SYu Xiangning if (sti->sti_direct) {
1711ff550d0eSmasputra int rval;
1712ff550d0eSmasputra /*
1713ff550d0eSmasputra * For consistency we inform tcp to disable
1714ff550d0eSmasputra * direct interface on the listener, though
1715ff550d0eSmasputra * we can certainly live without doing this
1716ff550d0eSmasputra * because no data will ever travel upstream
1717ff550d0eSmasputra * on the listening socket.
1718ff550d0eSmasputra */
17190f1702c5SYu Xiangning sti->sti_direct = 0;
1720ff550d0eSmasputra (void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK,
1721de8c4a14SErik Nordmark 0, 0, K_TO_K, cr, &rval);
1722ff550d0eSmasputra }
17237c478bd9Sstevel@tonic-gate opt = NULL;
17247c478bd9Sstevel@tonic-gate optlen = 0;
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate break;
17277c478bd9Sstevel@tonic-gate case AF_UNIX:
17287c478bd9Sstevel@tonic-gate default:
17297c478bd9Sstevel@tonic-gate if (optlen != 0) {
17307c478bd9Sstevel@tonic-gate opt = sogetoff(mp, conn_ind->OPT_offset, optlen,
17317c478bd9Sstevel@tonic-gate __TPI_ALIGN_SIZE);
17327c478bd9Sstevel@tonic-gate if (opt == NULL) {
17337c478bd9Sstevel@tonic-gate error = EPROTO;
17347c478bd9Sstevel@tonic-gate freemsg(mp);
17357c478bd9Sstevel@tonic-gate eprintsoline(so, error);
17367c478bd9Sstevel@tonic-gate goto disconnect_unlocked;
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
17400f1702c5SYu Xiangning if (!sti->sti_faddr_noxlate) {
17417c478bd9Sstevel@tonic-gate src = NULL;
17427c478bd9Sstevel@tonic-gate srclen = 0;
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate /* Extract src address from options */
17457c478bd9Sstevel@tonic-gate if (optlen != 0)
17467c478bd9Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &src, &srclen);
17477c478bd9Sstevel@tonic-gate }
17487c478bd9Sstevel@tonic-gate break;
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate
17517c478bd9Sstevel@tonic-gate /*
17527c478bd9Sstevel@tonic-gate * Create the new socket.
17537c478bd9Sstevel@tonic-gate */
17540f1702c5SYu Xiangning nso = socket_newconn(so, NULL, NULL, SOCKET_SLEEP, &error);
17557c478bd9Sstevel@tonic-gate if (nso == NULL) {
17567c478bd9Sstevel@tonic-gate ASSERT(error != 0);
17577c478bd9Sstevel@tonic-gate /*
17587c478bd9Sstevel@tonic-gate * Accept can not fail with ENOBUFS. sotpi_create
17597c478bd9Sstevel@tonic-gate * sleeps waiting for memory until a signal is caught
17607c478bd9Sstevel@tonic-gate * so return EINTR.
17617c478bd9Sstevel@tonic-gate */
17627c478bd9Sstevel@tonic-gate freemsg(mp);
17637c478bd9Sstevel@tonic-gate if (error == ENOBUFS)
17647c478bd9Sstevel@tonic-gate error = EINTR;
17657c478bd9Sstevel@tonic-gate goto e_disc_unl;
17667c478bd9Sstevel@tonic-gate }
17677c478bd9Sstevel@tonic-gate nvp = SOTOV(nso);
17680f1702c5SYu Xiangning nsti = SOTOTPI(nso);
17697c478bd9Sstevel@tonic-gate
17707c478bd9Sstevel@tonic-gate #ifdef DEBUG
17717c478bd9Sstevel@tonic-gate /*
17727c478bd9Sstevel@tonic-gate * SO_DEBUG is used to trigger the dprint* and eprint* macros thus
17737c478bd9Sstevel@tonic-gate * it's inherited early to allow debugging of the accept code itself.
17747c478bd9Sstevel@tonic-gate */
17757c478bd9Sstevel@tonic-gate nso->so_options |= so->so_options & SO_DEBUG;
17767c478bd9Sstevel@tonic-gate #endif /* DEBUG */
17777c478bd9Sstevel@tonic-gate
17787c478bd9Sstevel@tonic-gate /*
17797c478bd9Sstevel@tonic-gate * Save the SRC address from the T_CONN_IND
17807c478bd9Sstevel@tonic-gate * for getpeername to work on AF_UNIX and on transports that do not
17817c478bd9Sstevel@tonic-gate * support TI_GETPEERNAME.
17827c478bd9Sstevel@tonic-gate *
17837c478bd9Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by the sender's
17847c478bd9Sstevel@tonic-gate * copyin_name().
17857c478bd9Sstevel@tonic-gate */
17860f1702c5SYu Xiangning if (srclen > (t_uscalar_t)nsti->sti_faddr_maxlen) {
17877c478bd9Sstevel@tonic-gate error = EINVAL;
17887c478bd9Sstevel@tonic-gate freemsg(mp);
17897c478bd9Sstevel@tonic-gate eprintsoline(so, error);
17907c478bd9Sstevel@tonic-gate goto disconnect_vp_unlocked;
17917c478bd9Sstevel@tonic-gate }
17920f1702c5SYu Xiangning nsti->sti_faddr_len = (socklen_t)srclen;
17930f1702c5SYu Xiangning ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
17940f1702c5SYu Xiangning bcopy(src, nsti->sti_faddr_sa, srclen);
17950f1702c5SYu Xiangning nsti->sti_faddr_valid = 1;
17967c478bd9Sstevel@tonic-gate
1797de8c4a14SErik Nordmark /*
1798de8c4a14SErik Nordmark * Record so_peercred and so_cpid from a cred in the T_CONN_IND.
1799de8c4a14SErik Nordmark */
18007c478bd9Sstevel@tonic-gate if ((DB_REF(mp) > 1) || MBLKSIZE(mp) <
18017c478bd9Sstevel@tonic-gate (sizeof (struct T_conn_res) + sizeof (intptr_t))) {
18027c478bd9Sstevel@tonic-gate cred_t *cr;
1803de8c4a14SErik Nordmark pid_t cpid;
18047c478bd9Sstevel@tonic-gate
1805de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1806de8c4a14SErik Nordmark if (cr != NULL) {
18077c478bd9Sstevel@tonic-gate crhold(cr);
18087c478bd9Sstevel@tonic-gate nso->so_peercred = cr;
1809de8c4a14SErik Nordmark nso->so_cpid = cpid;
18107c478bd9Sstevel@tonic-gate }
18117c478bd9Sstevel@tonic-gate freemsg(mp);
18127c478bd9Sstevel@tonic-gate
18137c478bd9Sstevel@tonic-gate mp = soallocproto1(NULL, sizeof (struct T_conn_res) +
1814d4f98ef9SAnders Persson sizeof (intptr_t), 0, _ALLOC_INTR, cr);
18157c478bd9Sstevel@tonic-gate if (mp == NULL) {
18167c478bd9Sstevel@tonic-gate /*
18177c478bd9Sstevel@tonic-gate * Accept can not fail with ENOBUFS.
18187c478bd9Sstevel@tonic-gate * A signal was caught so return EINTR.
18197c478bd9Sstevel@tonic-gate */
18207c478bd9Sstevel@tonic-gate error = EINTR;
18217c478bd9Sstevel@tonic-gate eprintsoline(so, error);
18227c478bd9Sstevel@tonic-gate goto disconnect_vp_unlocked;
18237c478bd9Sstevel@tonic-gate }
18247c478bd9Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr;
18257c478bd9Sstevel@tonic-gate } else {
1826de8c4a14SErik Nordmark /*
1827de8c4a14SErik Nordmark * For efficency reasons we use msg_extractcred; no crhold
1828de8c4a14SErik Nordmark * needed since db_credp is cleared (i.e., we move the cred
1829de8c4a14SErik Nordmark * from the message to so_peercred.
1830de8c4a14SErik Nordmark */
1831de8c4a14SErik Nordmark nso->so_peercred = msg_extractcred(mp, &nso->so_cpid);
18327c478bd9Sstevel@tonic-gate
18337c478bd9Sstevel@tonic-gate mp->b_rptr = DB_BASE(mp);
18347c478bd9Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr;
18357c478bd9Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res);
1836d4f98ef9SAnders Persson
1837d4f98ef9SAnders Persson mblk_setcred(mp, cr, curproc->p_pid);
18387c478bd9Sstevel@tonic-gate }
18397c478bd9Sstevel@tonic-gate
18407c478bd9Sstevel@tonic-gate /*
18417c478bd9Sstevel@tonic-gate * New socket must be bound at least in sockfs and, except for AF_INET,
18427c478bd9Sstevel@tonic-gate * (or AF_INET6) it also has to be bound in the transport provider.
1843188658baSja97890 * We set the local address in the sonode from the T_OK_ACK of the
1844188658baSja97890 * T_CONN_RES. For this reason the address we bind to here isn't
1845188658baSja97890 * important.
18467c478bd9Sstevel@tonic-gate */
18477c478bd9Sstevel@tonic-gate if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
18487c478bd9Sstevel@tonic-gate /*CONSTCOND*/
18497c478bd9Sstevel@tonic-gate nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) {
18507c478bd9Sstevel@tonic-gate /*
18517c478bd9Sstevel@tonic-gate * Optimization for AF_INET{,6} transports
18527c478bd9Sstevel@tonic-gate * that can handle a T_CONN_RES without being bound.
18537c478bd9Sstevel@tonic-gate */
18547c478bd9Sstevel@tonic-gate mutex_enter(&nso->so_lock);
18557c478bd9Sstevel@tonic-gate so_automatic_bind(nso);
18567c478bd9Sstevel@tonic-gate mutex_exit(&nso->so_lock);
18577c478bd9Sstevel@tonic-gate } else {
18587c478bd9Sstevel@tonic-gate /* Perform NULL bind with the transport provider. */
18590f1702c5SYu Xiangning if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC,
18600f1702c5SYu Xiangning cr)) != 0) {
18617c478bd9Sstevel@tonic-gate ASSERT(error != ENOBUFS);
18627c478bd9Sstevel@tonic-gate freemsg(mp);
18637c478bd9Sstevel@tonic-gate eprintsoline(nso, error);
18647c478bd9Sstevel@tonic-gate goto disconnect_vp_unlocked;
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate }
18677c478bd9Sstevel@tonic-gate
18687c478bd9Sstevel@tonic-gate /*
18697c478bd9Sstevel@tonic-gate * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES
18707c478bd9Sstevel@tonic-gate * so that any data arriving on the new socket will cause the
18717c478bd9Sstevel@tonic-gate * appropriate signals to be delivered for the new socket.
18727c478bd9Sstevel@tonic-gate *
18737c478bd9Sstevel@tonic-gate * No other thread (except strsock_proto and strsock_misc)
18747c478bd9Sstevel@tonic-gate * can access the new socket thus we relax the locking.
18757c478bd9Sstevel@tonic-gate */
18767c478bd9Sstevel@tonic-gate nso->so_pgrp = so->so_pgrp;
18770f1702c5SYu Xiangning nso->so_state |= so->so_state & SS_ASYNC;
18780f1702c5SYu Xiangning nsti->sti_faddr_noxlate = sti->sti_faddr_noxlate;
18797c478bd9Sstevel@tonic-gate
18807c478bd9Sstevel@tonic-gate if (nso->so_pgrp != 0) {
1881de8c4a14SErik Nordmark if ((error = so_set_events(nso, nvp, cr)) != 0) {
18827c478bd9Sstevel@tonic-gate eprintsoline(nso, error);
18837c478bd9Sstevel@tonic-gate error = 0;
18847c478bd9Sstevel@tonic-gate nso->so_pgrp = 0;
18857c478bd9Sstevel@tonic-gate }
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate
18887c478bd9Sstevel@tonic-gate /*
18897c478bd9Sstevel@tonic-gate * Make note of the socket level options. TCP and IP level options
18907c478bd9Sstevel@tonic-gate * are already inherited. We could do all this after accept is
18917c478bd9Sstevel@tonic-gate * successful but doing it here simplifies code and no harm done
18927c478bd9Sstevel@tonic-gate * for error case.
18937c478bd9Sstevel@tonic-gate */
18947c478bd9Sstevel@tonic-gate nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE|
18957c478bd9Sstevel@tonic-gate SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
18967c478bd9Sstevel@tonic-gate SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
18977c478bd9Sstevel@tonic-gate nso->so_sndbuf = so->so_sndbuf;
18987c478bd9Sstevel@tonic-gate nso->so_rcvbuf = so->so_rcvbuf;
18997c478bd9Sstevel@tonic-gate if (nso->so_options & SO_LINGER)
19007c478bd9Sstevel@tonic-gate nso->so_linger = so->so_linger;
19017c478bd9Sstevel@tonic-gate
19020f1702c5SYu Xiangning /*
19030f1702c5SYu Xiangning * Note that the following sti_direct code path should be
19040f1702c5SYu Xiangning * removed once we are confident that the direct sockets
19050f1702c5SYu Xiangning * do not result in any degradation.
19060f1702c5SYu Xiangning */
19070f1702c5SYu Xiangning if (sti->sti_direct) {
19087c478bd9Sstevel@tonic-gate
19097c478bd9Sstevel@tonic-gate ASSERT(opt != NULL);
19107c478bd9Sstevel@tonic-gate
19117c478bd9Sstevel@tonic-gate conn_res->OPT_length = optlen;
19127c478bd9Sstevel@tonic-gate conn_res->OPT_offset = MBLKL(mp);
19137c478bd9Sstevel@tonic-gate bcopy(&opt, mp->b_wptr, optlen);
19147c478bd9Sstevel@tonic-gate mp->b_wptr += optlen;
19157c478bd9Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES;
19167c478bd9Sstevel@tonic-gate conn_res->ACCEPTOR_id = 0;
19177c478bd9Sstevel@tonic-gate PRIM_type = T_CONN_RES;
19187c478bd9Sstevel@tonic-gate
19197c478bd9Sstevel@tonic-gate /* Send down the T_CONN_RES on acceptor STREAM */
19207c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(nso), mp, NULL,
19217c478bd9Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
19227c478bd9Sstevel@tonic-gate if (error) {
19237c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
19247c478bd9Sstevel@tonic-gate so_lock_single(so);
19257c478bd9Sstevel@tonic-gate eprintsoline(so, error);
19267c478bd9Sstevel@tonic-gate goto disconnect_vp;
19277c478bd9Sstevel@tonic-gate }
19287c478bd9Sstevel@tonic-gate mutex_enter(&nso->so_lock);
19297c478bd9Sstevel@tonic-gate error = sowaitprim(nso, T_CONN_RES, T_OK_ACK,
19307c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
19317c478bd9Sstevel@tonic-gate if (error) {
19327c478bd9Sstevel@tonic-gate mutex_exit(&nso->so_lock);
19337c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
19347c478bd9Sstevel@tonic-gate so_lock_single(so);
19357c478bd9Sstevel@tonic-gate eprintsoline(so, error);
19367c478bd9Sstevel@tonic-gate goto disconnect_vp;
19377c478bd9Sstevel@tonic-gate }
19387c478bd9Sstevel@tonic-gate if (nso->so_family == AF_INET) {
19397c478bd9Sstevel@tonic-gate sin_t *sin;
19407c478bd9Sstevel@tonic-gate
19417c478bd9Sstevel@tonic-gate sin = (sin_t *)(ack_mp->b_rptr +
19427c478bd9Sstevel@tonic-gate sizeof (struct T_ok_ack));
19430f1702c5SYu Xiangning bcopy(sin, nsti->sti_laddr_sa, sizeof (sin_t));
19440f1702c5SYu Xiangning nsti->sti_laddr_len = sizeof (sin_t);
19457c478bd9Sstevel@tonic-gate } else {
19467c478bd9Sstevel@tonic-gate sin6_t *sin6;
19477c478bd9Sstevel@tonic-gate
19487c478bd9Sstevel@tonic-gate sin6 = (sin6_t *)(ack_mp->b_rptr +
19497c478bd9Sstevel@tonic-gate sizeof (struct T_ok_ack));
19500f1702c5SYu Xiangning bcopy(sin6, nsti->sti_laddr_sa, sizeof (sin6_t));
19510f1702c5SYu Xiangning nsti->sti_laddr_len = sizeof (sin6_t);
19527c478bd9Sstevel@tonic-gate }
19537c478bd9Sstevel@tonic-gate freemsg(ack_mp);
19547c478bd9Sstevel@tonic-gate
19550f1702c5SYu Xiangning nso->so_state |= SS_ISCONNECTED;
19560f1702c5SYu Xiangning nso->so_proto_handle = (sock_lower_handle_t)opt;
19570f1702c5SYu Xiangning nsti->sti_laddr_valid = 1;
19587c478bd9Sstevel@tonic-gate
19590f1702c5SYu Xiangning if (sti->sti_nl7c_flags & NL7C_ENABLED) {
19607c478bd9Sstevel@tonic-gate /*
19612c9e429eSbrutus * A NL7C marked listen()er so the new socket
19622c9e429eSbrutus * inherits the listen()er's NL7C state, except
19632c9e429eSbrutus * for NL7C_POLLIN.
19647c478bd9Sstevel@tonic-gate *
19652c9e429eSbrutus * Only call NL7C to process the new socket if
19662c9e429eSbrutus * the listen socket allows blocking i/o.
19677c478bd9Sstevel@tonic-gate */
19680f1702c5SYu Xiangning nsti->sti_nl7c_flags =
19690f1702c5SYu Xiangning sti->sti_nl7c_flags & (~NL7C_POLLIN);
19702c9e429eSbrutus if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) {
19712c9e429eSbrutus /*
19722c9e429eSbrutus * Nonblocking accept() just make it
19732c9e429eSbrutus * persist to defer processing to the
19742c9e429eSbrutus * read-side syscall (e.g. read).
19752c9e429eSbrutus */
19760f1702c5SYu Xiangning nsti->sti_nl7c_flags |= NL7C_SOPERSIST;
19772c9e429eSbrutus } else if (nl7c_process(nso, B_FALSE)) {
19787c478bd9Sstevel@tonic-gate /*
19797c478bd9Sstevel@tonic-gate * NL7C has completed processing on the
19807c478bd9Sstevel@tonic-gate * socket, close the socket and back to
19817c478bd9Sstevel@tonic-gate * the top to await the next T_CONN_IND.
19827c478bd9Sstevel@tonic-gate */
19837c478bd9Sstevel@tonic-gate mutex_exit(&nso->so_lock);
19847c478bd9Sstevel@tonic-gate (void) VOP_CLOSE(nvp, 0, 1, (offset_t)0,
1985de8c4a14SErik Nordmark cr, NULL);
19867c478bd9Sstevel@tonic-gate VN_RELE(nvp);
19877c478bd9Sstevel@tonic-gate goto again;
19887c478bd9Sstevel@tonic-gate }
19897c478bd9Sstevel@tonic-gate /* Pass the new socket out */
19907c478bd9Sstevel@tonic-gate }
19917c478bd9Sstevel@tonic-gate
19927c478bd9Sstevel@tonic-gate mutex_exit(&nso->so_lock);
19937c478bd9Sstevel@tonic-gate
19947c478bd9Sstevel@tonic-gate /*
19957d6c035bSja97890 * It's possible, through the use of autopush for example,
19960f1702c5SYu Xiangning * that the acceptor stream may not support sti_direct
19970f1702c5SYu Xiangning * semantics. If the new socket does not support sti_direct
19987d6c035bSja97890 * we issue a _SIOCSOCKFALLBACK to inform the transport
19997d6c035bSja97890 * as we would in the I_PUSH case.
20007d6c035bSja97890 */
20010f1702c5SYu Xiangning if (nsti->sti_direct == 0) {
20027d6c035bSja97890 int rval;
20037d6c035bSja97890
20047d6c035bSja97890 if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
2005de8c4a14SErik Nordmark 0, 0, K_TO_K, cr, &rval)) != 0) {
20067d6c035bSja97890 mutex_enter(&so->so_lock);
20077d6c035bSja97890 so_lock_single(so);
20087d6c035bSja97890 eprintsoline(so, error);
20097d6c035bSja97890 goto disconnect_vp;
20107d6c035bSja97890 }
20117d6c035bSja97890 }
20127d6c035bSja97890
20137d6c035bSja97890 /*
20147c478bd9Sstevel@tonic-gate * Pass out new socket.
20157c478bd9Sstevel@tonic-gate */
20167c478bd9Sstevel@tonic-gate if (nsop != NULL)
20177c478bd9Sstevel@tonic-gate *nsop = nso;
20187c478bd9Sstevel@tonic-gate
20197c478bd9Sstevel@tonic-gate return (0);
20207c478bd9Sstevel@tonic-gate }
20217c478bd9Sstevel@tonic-gate
20227c478bd9Sstevel@tonic-gate /*
20237c478bd9Sstevel@tonic-gate * This is the non-performance case for sockets (e.g. AF_UNIX sockets)
20247c478bd9Sstevel@tonic-gate * which don't support the FireEngine accept fast-path. It is also
20257c478bd9Sstevel@tonic-gate * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd
20267c478bd9Sstevel@tonic-gate * again. Neither sockfs nor TCP attempt to find out if some other
20277c478bd9Sstevel@tonic-gate * random module has been inserted in between (in which case we
20287c478bd9Sstevel@tonic-gate * should follow TLI accept behaviour). We blindly assume the worst
20297c478bd9Sstevel@tonic-gate * case and revert back to old behaviour i.e. TCP will not send us
20307c478bd9Sstevel@tonic-gate * any option (eager) and the accept should happen on the listener
20317c478bd9Sstevel@tonic-gate * queue. Any queued T_conn_ind have already got their options removed
20327c478bd9Sstevel@tonic-gate * by so_sock2_stream() when "sockmod" was I_POP'd.
20337c478bd9Sstevel@tonic-gate */
20347c478bd9Sstevel@tonic-gate /*
20357c478bd9Sstevel@tonic-gate * Fill in the {O_}T_CONN_RES before getting SOLOCKED.
20367c478bd9Sstevel@tonic-gate */
20377c478bd9Sstevel@tonic-gate if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) {
20387c478bd9Sstevel@tonic-gate #ifdef _ILP32
20397c478bd9Sstevel@tonic-gate queue_t *q;
20407c478bd9Sstevel@tonic-gate
20417c478bd9Sstevel@tonic-gate /*
20427c478bd9Sstevel@tonic-gate * Find read queue in driver
20437c478bd9Sstevel@tonic-gate * Can safely do this since we "own" nso/nvp.
20447c478bd9Sstevel@tonic-gate */
20457c478bd9Sstevel@tonic-gate q = strvp2wq(nvp)->q_next;
20467c478bd9Sstevel@tonic-gate while (SAMESTR(q))
20477c478bd9Sstevel@tonic-gate q = q->q_next;
20487c478bd9Sstevel@tonic-gate q = RD(q);
20497c478bd9Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)q;
20507c478bd9Sstevel@tonic-gate #else
20517c478bd9Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev);
20527c478bd9Sstevel@tonic-gate #endif /* _ILP32 */
20537c478bd9Sstevel@tonic-gate conn_res->PRIM_type = O_T_CONN_RES;
20547c478bd9Sstevel@tonic-gate PRIM_type = O_T_CONN_RES;
20557c478bd9Sstevel@tonic-gate } else {
20560f1702c5SYu Xiangning conn_res->ACCEPTOR_id = nsti->sti_acceptor_id;
20577c478bd9Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES;
20587c478bd9Sstevel@tonic-gate PRIM_type = T_CONN_RES;
20597c478bd9Sstevel@tonic-gate }
20607c478bd9Sstevel@tonic-gate conn_res->SEQ_number = SEQ_number;
20617c478bd9Sstevel@tonic-gate conn_res->OPT_length = 0;
20627c478bd9Sstevel@tonic-gate conn_res->OPT_offset = 0;
20637c478bd9Sstevel@tonic-gate
20647c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
20657c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
20667c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
20677c478bd9Sstevel@tonic-gate
20687c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL,
20697c478bd9Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
20707c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
20717c478bd9Sstevel@tonic-gate if (error) {
20727c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20737c478bd9Sstevel@tonic-gate goto disconnect_vp;
20747c478bd9Sstevel@tonic-gate }
2075188658baSja97890 error = sowaitprim(so, PRIM_type, T_OK_ACK,
2076188658baSja97890 (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
20777c478bd9Sstevel@tonic-gate if (error) {
20787c478bd9Sstevel@tonic-gate eprintsoline(so, error);
20797c478bd9Sstevel@tonic-gate goto disconnect_vp;
20807c478bd9Sstevel@tonic-gate }
2081881776cfSGeorge Shepherd mutex_exit(&so->so_lock);
2082188658baSja97890 /*
2083188658baSja97890 * If there is a sin/sin6 appended onto the T_OK_ACK use
2084188658baSja97890 * that to set the local address. If this is not present
2085188658baSja97890 * then we zero out the address and don't set the
20860f1702c5SYu Xiangning * sti_laddr_valid bit. For AF_UNIX endpoints we copy over
20876e81d8daSja97890 * the pathname from the listening socket.
2088881776cfSGeorge Shepherd * In the case where this is TCP or an AF_UNIX socket the
2089881776cfSGeorge Shepherd * client side may have queued data or a T_ORDREL in the
2090881776cfSGeorge Shepherd * transport. Having now sent the T_CONN_RES we may receive
2091881776cfSGeorge Shepherd * those queued messages at any time. Hold the acceptor
2092881776cfSGeorge Shepherd * so_lock until its state and laddr are finalized.
2093188658baSja97890 */
2094881776cfSGeorge Shepherd mutex_enter(&nso->so_lock);
2095188658baSja97890 sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
2096188658baSja97890 if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) &&
2097188658baSja97890 MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
2098188658baSja97890 ack_mp->b_rptr += sizeof (struct T_ok_ack);
20990f1702c5SYu Xiangning bcopy(ack_mp->b_rptr, nsti->sti_laddr_sa, sinlen);
21000f1702c5SYu Xiangning nsti->sti_laddr_len = sinlen;
21010f1702c5SYu Xiangning nsti->sti_laddr_valid = 1;
21026e81d8daSja97890 } else if (nso->so_family == AF_UNIX) {
21036e81d8daSja97890 ASSERT(so->so_family == AF_UNIX);
21040f1702c5SYu Xiangning nsti->sti_laddr_len = sti->sti_laddr_len;
21050f1702c5SYu Xiangning ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
21060f1702c5SYu Xiangning bcopy(sti->sti_laddr_sa, nsti->sti_laddr_sa,
21070f1702c5SYu Xiangning nsti->sti_laddr_len);
21080f1702c5SYu Xiangning nsti->sti_laddr_valid = 1;
2109188658baSja97890 } else {
21100f1702c5SYu Xiangning nsti->sti_laddr_len = sti->sti_laddr_len;
21110f1702c5SYu Xiangning ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
21120f1702c5SYu Xiangning bzero(nsti->sti_laddr_sa, nsti->sti_addr_size);
21130f1702c5SYu Xiangning nsti->sti_laddr_sa->sa_family = nso->so_family;
2114188658baSja97890 }
2115881776cfSGeorge Shepherd nso->so_state |= SS_ISCONNECTED;
2116881776cfSGeorge Shepherd mutex_exit(&nso->so_lock);
2117881776cfSGeorge Shepherd
2118188658baSja97890 freemsg(ack_mp);
2119188658baSja97890
2120881776cfSGeorge Shepherd mutex_enter(&so->so_lock);
21217c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
21227c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
21237c478bd9Sstevel@tonic-gate
21247c478bd9Sstevel@tonic-gate /*
21257c478bd9Sstevel@tonic-gate * Pass out new socket.
21267c478bd9Sstevel@tonic-gate */
21277c478bd9Sstevel@tonic-gate if (nsop != NULL)
21287c478bd9Sstevel@tonic-gate *nsop = nso;
21297c478bd9Sstevel@tonic-gate
21307c478bd9Sstevel@tonic-gate return (0);
21317c478bd9Sstevel@tonic-gate
21327c478bd9Sstevel@tonic-gate
21337c478bd9Sstevel@tonic-gate eproto_disc_unl:
21347c478bd9Sstevel@tonic-gate error = EPROTO;
21357c478bd9Sstevel@tonic-gate e_disc_unl:
21367c478bd9Sstevel@tonic-gate eprintsoline(so, error);
21377c478bd9Sstevel@tonic-gate goto disconnect_unlocked;
21387c478bd9Sstevel@tonic-gate
21397c478bd9Sstevel@tonic-gate pr_disc_vp_unl:
21407c478bd9Sstevel@tonic-gate eprintsoline(so, error);
21417c478bd9Sstevel@tonic-gate disconnect_vp_unlocked:
2142de8c4a14SErik Nordmark (void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
21437c478bd9Sstevel@tonic-gate VN_RELE(nvp);
21447c478bd9Sstevel@tonic-gate disconnect_unlocked:
21457c478bd9Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, 0);
21467c478bd9Sstevel@tonic-gate return (error);
21477c478bd9Sstevel@tonic-gate
21487c478bd9Sstevel@tonic-gate pr_disc_vp:
21497c478bd9Sstevel@tonic-gate eprintsoline(so, error);
21507c478bd9Sstevel@tonic-gate disconnect_vp:
21517c478bd9Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
21527c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
21537c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
2154de8c4a14SErik Nordmark (void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
21557c478bd9Sstevel@tonic-gate VN_RELE(nvp);
21567c478bd9Sstevel@tonic-gate return (error);
21577c478bd9Sstevel@tonic-gate
21587c478bd9Sstevel@tonic-gate conn_bad: /* Note: SunOS 4/BSD unconditionally returns EINVAL here */
21597c478bd9Sstevel@tonic-gate error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
21607c478bd9Sstevel@tonic-gate ? EOPNOTSUPP : EINVAL;
21617c478bd9Sstevel@tonic-gate e_bad:
21627c478bd9Sstevel@tonic-gate eprintsoline(so, error);
21637c478bd9Sstevel@tonic-gate return (error);
21647c478bd9Sstevel@tonic-gate }
21657c478bd9Sstevel@tonic-gate
21667c478bd9Sstevel@tonic-gate /*
21677c478bd9Sstevel@tonic-gate * connect a socket.
21687c478bd9Sstevel@tonic-gate *
21697c478bd9Sstevel@tonic-gate * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
21707c478bd9Sstevel@tonic-gate * unconnect (by specifying a null address).
21717c478bd9Sstevel@tonic-gate */
21727c478bd9Sstevel@tonic-gate int
sotpi_connect(struct sonode * so,struct sockaddr * name,socklen_t namelen,int fflag,int flags,struct cred * cr)21737c478bd9Sstevel@tonic-gate sotpi_connect(struct sonode *so,
21743e95bd4aSAnders Persson struct sockaddr *name,
21757c478bd9Sstevel@tonic-gate socklen_t namelen,
21767c478bd9Sstevel@tonic-gate int fflag,
21770f1702c5SYu Xiangning int flags,
21780f1702c5SYu Xiangning struct cred *cr)
21797c478bd9Sstevel@tonic-gate {
21807c478bd9Sstevel@tonic-gate struct T_conn_req conn_req;
21817c478bd9Sstevel@tonic-gate int error = 0;
21827c478bd9Sstevel@tonic-gate mblk_t *mp;
21837c478bd9Sstevel@tonic-gate void *src;
21847c478bd9Sstevel@tonic-gate socklen_t srclen;
21857c478bd9Sstevel@tonic-gate void *addr;
21867c478bd9Sstevel@tonic-gate socklen_t addrlen;
21877c478bd9Sstevel@tonic-gate boolean_t need_unlock;
21880f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
21897c478bd9Sstevel@tonic-gate
21907c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
2191903a11ebSrh87107 (void *)so, (void *)name, namelen, fflag, flags,
21927c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
21937c478bd9Sstevel@tonic-gate
21947c478bd9Sstevel@tonic-gate /*
21957c478bd9Sstevel@tonic-gate * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
21967c478bd9Sstevel@tonic-gate * avoid sleeping for memory with SOLOCKED held.
21970f1702c5SYu Xiangning * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen
21987c478bd9Sstevel@tonic-gate * + sizeof (struct T_opthdr).
21997c478bd9Sstevel@tonic-gate * (the AF_UNIX so_ux_addr_xlate() does not make the address
22000f1702c5SYu Xiangning * exceed sti_faddr_maxlen).
22017c478bd9Sstevel@tonic-gate */
22027c478bd9Sstevel@tonic-gate mp = soallocproto(sizeof (struct T_conn_req) +
2203de8c4a14SErik Nordmark 2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR,
2204de8c4a14SErik Nordmark cr);
22057c478bd9Sstevel@tonic-gate if (mp == NULL) {
22067c478bd9Sstevel@tonic-gate /*
22077c478bd9Sstevel@tonic-gate * Connect can not fail with ENOBUFS. A signal was
22087c478bd9Sstevel@tonic-gate * caught so return EINTR.
22097c478bd9Sstevel@tonic-gate */
22107c478bd9Sstevel@tonic-gate error = EINTR;
22117c478bd9Sstevel@tonic-gate eprintsoline(so, error);
22127c478bd9Sstevel@tonic-gate return (error);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate
22157c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
22167c478bd9Sstevel@tonic-gate /*
2217ba3431dfSjprakash * Make sure there is a preallocated T_unbind_req message
2218ba3431dfSjprakash * before any binding. This message is allocated when the
2219ba3431dfSjprakash * socket is created. Since another thread can consume
2220ba3431dfSjprakash * so_unbind_mp by the time we return from so_lock_single(),
2221ba3431dfSjprakash * we should check the availability of so_unbind_mp after
2222ba3431dfSjprakash * we return from so_lock_single().
22237c478bd9Sstevel@tonic-gate */
2224ba3431dfSjprakash
2225ba3431dfSjprakash so_lock_single(so); /* Set SOLOCKED */
2226ba3431dfSjprakash need_unlock = B_TRUE;
2227ba3431dfSjprakash
22280f1702c5SYu Xiangning if (sti->sti_unbind_mp == NULL) {
22297c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
22307c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
22310f1702c5SYu Xiangning sti->sti_unbind_mp =
2232de8c4a14SErik Nordmark soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR, cr);
22330f1702c5SYu Xiangning if (sti->sti_unbind_mp == NULL) {
22347c478bd9Sstevel@tonic-gate error = EINTR;
22357c478bd9Sstevel@tonic-gate goto done;
22367c478bd9Sstevel@tonic-gate }
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate
22397c478bd9Sstevel@tonic-gate /*
22407c478bd9Sstevel@tonic-gate * Can't have done a listen before connecting.
22417c478bd9Sstevel@tonic-gate */
22427c478bd9Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) {
22437c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
22447c478bd9Sstevel@tonic-gate goto done;
22457c478bd9Sstevel@tonic-gate }
22467c478bd9Sstevel@tonic-gate
22477c478bd9Sstevel@tonic-gate /*
22487c478bd9Sstevel@tonic-gate * Must be bound with the transport
22497c478bd9Sstevel@tonic-gate */
22507c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) {
22517c478bd9Sstevel@tonic-gate if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
22527c478bd9Sstevel@tonic-gate /*CONSTCOND*/
22537c478bd9Sstevel@tonic-gate so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
22547c478bd9Sstevel@tonic-gate /*
22557c478bd9Sstevel@tonic-gate * Optimization for AF_INET{,6} transports
22567c478bd9Sstevel@tonic-gate * that can handle a T_CONN_REQ without being bound.
22577c478bd9Sstevel@tonic-gate */
22587c478bd9Sstevel@tonic-gate so_automatic_bind(so);
22597c478bd9Sstevel@tonic-gate } else {
22607c478bd9Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0,
22610f1702c5SYu Xiangning _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
22627c478bd9Sstevel@tonic-gate if (error)
22637c478bd9Sstevel@tonic-gate goto done;
22647c478bd9Sstevel@tonic-gate }
22657c478bd9Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND);
22667c478bd9Sstevel@tonic-gate flags |= _SOCONNECT_DID_BIND;
22677c478bd9Sstevel@tonic-gate }
22687c478bd9Sstevel@tonic-gate
22697c478bd9Sstevel@tonic-gate /*
22707c478bd9Sstevel@tonic-gate * Handle a connect to a name parameter of type AF_UNSPEC like a
22717c478bd9Sstevel@tonic-gate * connect to a null address. This is the portable method to
22727c478bd9Sstevel@tonic-gate * unconnect a socket.
22737c478bd9Sstevel@tonic-gate */
22747c478bd9Sstevel@tonic-gate if ((namelen >= sizeof (sa_family_t)) &&
22757c478bd9Sstevel@tonic-gate (name->sa_family == AF_UNSPEC)) {
22767c478bd9Sstevel@tonic-gate name = NULL;
22777c478bd9Sstevel@tonic-gate namelen = 0;
22787c478bd9Sstevel@tonic-gate }
22797c478bd9Sstevel@tonic-gate
22807c478bd9Sstevel@tonic-gate /*
22817c478bd9Sstevel@tonic-gate * Check that we are not already connected.
22827c478bd9Sstevel@tonic-gate * A connection-oriented socket cannot be reconnected.
22837c478bd9Sstevel@tonic-gate * A connected connection-less socket can be
22847c478bd9Sstevel@tonic-gate * - connected to a different address by a subsequent connect
22857c478bd9Sstevel@tonic-gate * - "unconnected" by a connect to the NULL address
22867c478bd9Sstevel@tonic-gate */
22877c478bd9Sstevel@tonic-gate if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
22887c478bd9Sstevel@tonic-gate ASSERT(!(flags & _SOCONNECT_DID_BIND));
22897c478bd9Sstevel@tonic-gate if (so->so_mode & SM_CONNREQUIRED) {
22907c478bd9Sstevel@tonic-gate /* Connection-oriented socket */
22917c478bd9Sstevel@tonic-gate error = so->so_state & SS_ISCONNECTED ?
22927c478bd9Sstevel@tonic-gate EISCONN : EALREADY;
22937c478bd9Sstevel@tonic-gate goto done;
22947c478bd9Sstevel@tonic-gate }
22957c478bd9Sstevel@tonic-gate /* Connection-less socket */
22967c478bd9Sstevel@tonic-gate if (name == NULL) {
22977c478bd9Sstevel@tonic-gate /*
22987c478bd9Sstevel@tonic-gate * Remove the connected state and clear SO_DGRAM_ERRIND
22997c478bd9Sstevel@tonic-gate * since it was set when the socket was connected.
23007c478bd9Sstevel@tonic-gate * If this is UDP also send down a T_DISCON_REQ.
23017c478bd9Sstevel@tonic-gate */
23027c478bd9Sstevel@tonic-gate int val;
23037c478bd9Sstevel@tonic-gate
23047c478bd9Sstevel@tonic-gate if ((so->so_family == AF_INET ||
23057c478bd9Sstevel@tonic-gate so->so_family == AF_INET6) &&
23067c478bd9Sstevel@tonic-gate (so->so_type == SOCK_DGRAM ||
23077c478bd9Sstevel@tonic-gate so->so_type == SOCK_RAW) &&
23087c478bd9Sstevel@tonic-gate /*CONSTCOND*/
23097c478bd9Sstevel@tonic-gate !soconnect_tpi_udp) {
23107c478bd9Sstevel@tonic-gate /* XXX What about implicitly unbinding here? */
23117c478bd9Sstevel@tonic-gate error = sodisconnect(so, -1,
23127c478bd9Sstevel@tonic-gate _SODISCONNECT_LOCK_HELD);
23137c478bd9Sstevel@tonic-gate } else {
23147c478bd9Sstevel@tonic-gate so->so_state &=
23150f1702c5SYu Xiangning ~(SS_ISCONNECTED | SS_ISCONNECTING);
23160f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
23170f1702c5SYu Xiangning sti->sti_faddr_len = 0;
23187c478bd9Sstevel@tonic-gate }
23197c478bd9Sstevel@tonic-gate
23200f1702c5SYu Xiangning /* Remove SOLOCKED since setsockopt will grab it */
23217c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
23227c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
23237c478bd9Sstevel@tonic-gate
23247c478bd9Sstevel@tonic-gate val = 0;
23250f1702c5SYu Xiangning (void) sotpi_setsockopt(so, SOL_SOCKET,
23260f1702c5SYu Xiangning SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val),
23270f1702c5SYu Xiangning cr);
23287c478bd9Sstevel@tonic-gate
23297c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
23307c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
23317c478bd9Sstevel@tonic-gate goto done;
23327c478bd9Sstevel@tonic-gate }
23337c478bd9Sstevel@tonic-gate }
23347c478bd9Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND);
23357c478bd9Sstevel@tonic-gate
23367c478bd9Sstevel@tonic-gate if (name == NULL || namelen == 0) {
23377c478bd9Sstevel@tonic-gate error = EINVAL;
23387c478bd9Sstevel@tonic-gate goto done;
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate /*
23410f1702c5SYu Xiangning * Mark the socket if sti_faddr_sa represents the transport level
23427c478bd9Sstevel@tonic-gate * address.
23437c478bd9Sstevel@tonic-gate */
23447c478bd9Sstevel@tonic-gate if (flags & _SOCONNECT_NOXLATE) {
23457c478bd9Sstevel@tonic-gate struct sockaddr_ux *soaddr_ux;
23467c478bd9Sstevel@tonic-gate
23477c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX);
23487c478bd9Sstevel@tonic-gate if (namelen != sizeof (struct sockaddr_ux)) {
23497c478bd9Sstevel@tonic-gate error = EINVAL;
23507c478bd9Sstevel@tonic-gate goto done;
23517c478bd9Sstevel@tonic-gate }
23527c478bd9Sstevel@tonic-gate soaddr_ux = (struct sockaddr_ux *)name;
23537c478bd9Sstevel@tonic-gate name = (struct sockaddr *)&soaddr_ux->sou_addr;
23547c478bd9Sstevel@tonic-gate namelen = sizeof (soaddr_ux->sou_addr);
23550f1702c5SYu Xiangning sti->sti_faddr_noxlate = 1;
23567c478bd9Sstevel@tonic-gate }
23577c478bd9Sstevel@tonic-gate
23587c478bd9Sstevel@tonic-gate /*
23597c478bd9Sstevel@tonic-gate * Length and family checks.
23607c478bd9Sstevel@tonic-gate */
23617c478bd9Sstevel@tonic-gate error = so_addr_verify(so, name, namelen);
23627c478bd9Sstevel@tonic-gate if (error)
23637c478bd9Sstevel@tonic-gate goto bad;
23647c478bd9Sstevel@tonic-gate
23657c478bd9Sstevel@tonic-gate /*
23667c478bd9Sstevel@tonic-gate * Save foreign address. Needed for AF_UNIX as well as
23677c478bd9Sstevel@tonic-gate * transport providers that do not support TI_GETPEERNAME.
23687c478bd9Sstevel@tonic-gate * Also used for cached foreign address for TCP and UDP.
23697c478bd9Sstevel@tonic-gate */
23700f1702c5SYu Xiangning if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) {
23717c478bd9Sstevel@tonic-gate error = EINVAL;
23727c478bd9Sstevel@tonic-gate goto done;
23737c478bd9Sstevel@tonic-gate }
23740f1702c5SYu Xiangning sti->sti_faddr_len = (socklen_t)namelen;
23750f1702c5SYu Xiangning ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
23760f1702c5SYu Xiangning bcopy(name, sti->sti_faddr_sa, namelen);
23770f1702c5SYu Xiangning sti->sti_faddr_valid = 1;
23787c478bd9Sstevel@tonic-gate
23797c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
23800f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
23817c478bd9Sstevel@tonic-gate /*
2382*e359ab86SGordon Ross * sti_faddr is a transport-level address, so
2383*e359ab86SGordon Ross * don't pass it as an option. Do save it in
2384*e359ab86SGordon Ross * sti_ux_faddr, used for connected DG send.
23857c478bd9Sstevel@tonic-gate */
23867c478bd9Sstevel@tonic-gate src = NULL;
23877c478bd9Sstevel@tonic-gate srclen = 0;
2388*e359ab86SGordon Ross addr = sti->sti_faddr_sa;
2389*e359ab86SGordon Ross addrlen = (t_uscalar_t)sti->sti_faddr_len;
2390*e359ab86SGordon Ross bcopy(addr, &sti->sti_ux_faddr,
2391*e359ab86SGordon Ross sizeof (sti->sti_ux_faddr));
23927c478bd9Sstevel@tonic-gate } else {
23937c478bd9Sstevel@tonic-gate /*
23947c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
23957c478bd9Sstevel@tonic-gate * and translate the remote address.
23960f1702c5SYu Xiangning * Holding so_lock thus sti_laddr_sa can not change.
23977c478bd9Sstevel@tonic-gate */
23980f1702c5SYu Xiangning src = sti->sti_laddr_sa;
23990f1702c5SYu Xiangning srclen = (t_uscalar_t)sti->sti_laddr_len;
24007c478bd9Sstevel@tonic-gate dprintso(so, 1,
24017c478bd9Sstevel@tonic-gate ("sotpi_connect UNIX: srclen %d, src %p\n",
24027c478bd9Sstevel@tonic-gate srclen, src));
2403cbc6e898SGordon Ross /*
2404cbc6e898SGordon Ross * Translate the destination address into our
2405cbc6e898SGordon Ross * internal form, and save it in sti_ux_faddr.
2406cbc6e898SGordon Ross * After this call, addr==&sti->sti_ux_taddr,
2407cbc6e898SGordon Ross * and we copy that to sti->sti_ux_faddr so
2408cbc6e898SGordon Ross * we save the connected peer address.
2409cbc6e898SGordon Ross */
24107c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so,
24110f1702c5SYu Xiangning sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len,
24127c478bd9Sstevel@tonic-gate (flags & _SOCONNECT_XPG4_2),
24137c478bd9Sstevel@tonic-gate &addr, &addrlen);
24147c478bd9Sstevel@tonic-gate if (error)
24157c478bd9Sstevel@tonic-gate goto bad;
2416cbc6e898SGordon Ross bcopy(&sti->sti_ux_taddr, &sti->sti_ux_faddr,
2417cbc6e898SGordon Ross sizeof (sti->sti_ux_faddr));
24187c478bd9Sstevel@tonic-gate }
24197c478bd9Sstevel@tonic-gate } else {
24200f1702c5SYu Xiangning addr = sti->sti_faddr_sa;
24210f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_len;
24227c478bd9Sstevel@tonic-gate src = NULL;
24237c478bd9Sstevel@tonic-gate srclen = 0;
24247c478bd9Sstevel@tonic-gate }
24257c478bd9Sstevel@tonic-gate /*
24267c478bd9Sstevel@tonic-gate * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
24277c478bd9Sstevel@tonic-gate * option which asks the transport provider to send T_UDERR_IND
24287c478bd9Sstevel@tonic-gate * messages. These T_UDERR_IND messages are used to return connected
24297c478bd9Sstevel@tonic-gate * style errors (e.g. ECONNRESET) for connected datagram sockets.
24307c478bd9Sstevel@tonic-gate *
24317c478bd9Sstevel@tonic-gate * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
24327c478bd9Sstevel@tonic-gate * we send down a T_CONN_REQ. This is needed to let the
24337c478bd9Sstevel@tonic-gate * transport assign a local address that is consistent with
24347c478bd9Sstevel@tonic-gate * the remote address. Applications depend on a getsockname()
24357c478bd9Sstevel@tonic-gate * after a connect() to retrieve the "source" IP address for
24367c478bd9Sstevel@tonic-gate * the connected socket. Invalidate the cached local address
24377c478bd9Sstevel@tonic-gate * to force getsockname() to enquire of the transport.
24387c478bd9Sstevel@tonic-gate */
24397c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_CONNREQUIRED)) {
24407c478bd9Sstevel@tonic-gate /*
24417c478bd9Sstevel@tonic-gate * Datagram socket.
24427c478bd9Sstevel@tonic-gate */
24437c478bd9Sstevel@tonic-gate int32_t val;
24447c478bd9Sstevel@tonic-gate
24457c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
24467c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
24477c478bd9Sstevel@tonic-gate
24487c478bd9Sstevel@tonic-gate val = 1;
24497c478bd9Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
24500f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
24517c478bd9Sstevel@tonic-gate
24527c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
24537c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
24547c478bd9Sstevel@tonic-gate if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
24557c478bd9Sstevel@tonic-gate (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
24567c478bd9Sstevel@tonic-gate soconnect_tpi_udp) {
24577c478bd9Sstevel@tonic-gate soisconnected(so);
24587c478bd9Sstevel@tonic-gate goto done;
24597c478bd9Sstevel@tonic-gate }
24607c478bd9Sstevel@tonic-gate /*
24617c478bd9Sstevel@tonic-gate * Send down T_CONN_REQ etc.
24627c478bd9Sstevel@tonic-gate * Clear fflag to avoid returning EWOULDBLOCK.
24637c478bd9Sstevel@tonic-gate */
24647c478bd9Sstevel@tonic-gate fflag = 0;
24657c478bd9Sstevel@tonic-gate ASSERT(so->so_family != AF_UNIX);
24660f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
24670f1702c5SYu Xiangning } else if (sti->sti_laddr_len != 0) {
24687c478bd9Sstevel@tonic-gate /*
24697c478bd9Sstevel@tonic-gate * If the local address or port was "any" then it may be
24707c478bd9Sstevel@tonic-gate * changed by the transport as a result of the
24717c478bd9Sstevel@tonic-gate * connect. Invalidate the cached version if we have one.
24727c478bd9Sstevel@tonic-gate */
24737c478bd9Sstevel@tonic-gate switch (so->so_family) {
24747c478bd9Sstevel@tonic-gate case AF_INET:
24750f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t));
24760f1702c5SYu Xiangning if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr ==
24777c478bd9Sstevel@tonic-gate INADDR_ANY ||
24780f1702c5SYu Xiangning ((sin_t *)sti->sti_laddr_sa)->sin_port == 0)
24790f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
24807c478bd9Sstevel@tonic-gate break;
24817c478bd9Sstevel@tonic-gate
24827c478bd9Sstevel@tonic-gate case AF_INET6:
24830f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len ==
24840f1702c5SYu Xiangning (socklen_t)sizeof (sin6_t));
24857c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED(
24860f1702c5SYu Xiangning &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) ||
24877c478bd9Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED_ANY(
24880f1702c5SYu Xiangning &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) ||
24890f1702c5SYu Xiangning ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0)
24900f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
24917c478bd9Sstevel@tonic-gate break;
24927c478bd9Sstevel@tonic-gate
24937c478bd9Sstevel@tonic-gate default:
24947c478bd9Sstevel@tonic-gate break;
24957c478bd9Sstevel@tonic-gate }
24967c478bd9Sstevel@tonic-gate }
24977c478bd9Sstevel@tonic-gate
24987c478bd9Sstevel@tonic-gate /*
24997c478bd9Sstevel@tonic-gate * Check for failure of an earlier call
25007c478bd9Sstevel@tonic-gate */
25017c478bd9Sstevel@tonic-gate if (so->so_error != 0)
25027c478bd9Sstevel@tonic-gate goto so_bad;
25037c478bd9Sstevel@tonic-gate
25047c478bd9Sstevel@tonic-gate /*
25057c478bd9Sstevel@tonic-gate * Send down T_CONN_REQ. Message was allocated above.
25067c478bd9Sstevel@tonic-gate */
25077c478bd9Sstevel@tonic-gate conn_req.PRIM_type = T_CONN_REQ;
25087c478bd9Sstevel@tonic-gate conn_req.DEST_length = addrlen;
25097c478bd9Sstevel@tonic-gate conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
25107c478bd9Sstevel@tonic-gate if (srclen == 0) {
25117c478bd9Sstevel@tonic-gate conn_req.OPT_length = 0;
25127c478bd9Sstevel@tonic-gate conn_req.OPT_offset = 0;
25137c478bd9Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req));
25147c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
25157c478bd9Sstevel@tonic-gate } else {
25167c478bd9Sstevel@tonic-gate /*
25177c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
25187c478bd9Sstevel@tonic-gate * address option.
25197c478bd9Sstevel@tonic-gate */
25207c478bd9Sstevel@tonic-gate struct T_opthdr toh;
25217c478bd9Sstevel@tonic-gate
25227c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
25237c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
25247c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
25257c478bd9Sstevel@tonic-gate toh.status = 0;
25267c478bd9Sstevel@tonic-gate conn_req.OPT_length =
25277c478bd9Sstevel@tonic-gate (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
25287c478bd9Sstevel@tonic-gate conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
25297c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
25307c478bd9Sstevel@tonic-gate
25317c478bd9Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req));
25327c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
25337c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
25347c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
25357c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
25367c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
25377c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
25387c478bd9Sstevel@tonic-gate }
25397c478bd9Sstevel@tonic-gate /*
25407c478bd9Sstevel@tonic-gate * Set SS_ISCONNECTING before sending down the T_CONN_REQ
25417c478bd9Sstevel@tonic-gate * in order to have the right state when the T_CONN_CON shows up.
25427c478bd9Sstevel@tonic-gate */
25437c478bd9Sstevel@tonic-gate soisconnecting(so);
25447c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
25457c478bd9Sstevel@tonic-gate
2546005d3febSMarek Pospisil if (AU_AUDITING())
25477c478bd9Sstevel@tonic-gate audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
25487c478bd9Sstevel@tonic-gate
25497c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
25507c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
25517c478bd9Sstevel@tonic-gate mp = NULL;
25527c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
25537c478bd9Sstevel@tonic-gate if (error != 0)
25547c478bd9Sstevel@tonic-gate goto bad;
25557c478bd9Sstevel@tonic-gate
25567c478bd9Sstevel@tonic-gate if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
25577c478bd9Sstevel@tonic-gate goto bad;
25587c478bd9Sstevel@tonic-gate
25597c478bd9Sstevel@tonic-gate /* Allow other threads to access the socket */
25607c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
25617c478bd9Sstevel@tonic-gate need_unlock = B_FALSE;
25627c478bd9Sstevel@tonic-gate
25637c478bd9Sstevel@tonic-gate /*
25647c478bd9Sstevel@tonic-gate * Wait until we get a T_CONN_CON or an error
25657c478bd9Sstevel@tonic-gate */
25667c478bd9Sstevel@tonic-gate if ((error = sowaitconnected(so, fflag, 0)) != 0) {
25677c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
25687c478bd9Sstevel@tonic-gate need_unlock = B_TRUE;
25697c478bd9Sstevel@tonic-gate }
25707c478bd9Sstevel@tonic-gate
25717c478bd9Sstevel@tonic-gate done:
25727c478bd9Sstevel@tonic-gate freemsg(mp);
25737c478bd9Sstevel@tonic-gate switch (error) {
25747c478bd9Sstevel@tonic-gate case EINPROGRESS:
25757c478bd9Sstevel@tonic-gate case EALREADY:
25767c478bd9Sstevel@tonic-gate case EISCONN:
25777c478bd9Sstevel@tonic-gate case EINTR:
25787c478bd9Sstevel@tonic-gate /* Non-fatal errors */
25790f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
25807c478bd9Sstevel@tonic-gate /* FALLTHRU */
25817c478bd9Sstevel@tonic-gate case 0:
25827c478bd9Sstevel@tonic-gate break;
25837c478bd9Sstevel@tonic-gate default:
25847c478bd9Sstevel@tonic-gate ASSERT(need_unlock);
25857c478bd9Sstevel@tonic-gate /*
25867c478bd9Sstevel@tonic-gate * Fatal errors: clear SS_ISCONNECTING in case it was set,
25877c478bd9Sstevel@tonic-gate * and invalidate local-address cache
25887c478bd9Sstevel@tonic-gate */
25890f1702c5SYu Xiangning so->so_state &= ~SS_ISCONNECTING;
25900f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
25917c478bd9Sstevel@tonic-gate /* A discon_ind might have already unbound us */
25927c478bd9Sstevel@tonic-gate if ((flags & _SOCONNECT_DID_BIND) &&
25937c478bd9Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) {
25947c478bd9Sstevel@tonic-gate int err;
25957c478bd9Sstevel@tonic-gate
25967c478bd9Sstevel@tonic-gate err = sotpi_unbind(so, 0);
25977c478bd9Sstevel@tonic-gate /* LINTED - statement has no conseq */
25987c478bd9Sstevel@tonic-gate if (err) {
25997c478bd9Sstevel@tonic-gate eprintsoline(so, err);
26007c478bd9Sstevel@tonic-gate }
26017c478bd9Sstevel@tonic-gate }
26027c478bd9Sstevel@tonic-gate break;
26037c478bd9Sstevel@tonic-gate }
26047c478bd9Sstevel@tonic-gate if (need_unlock)
26057c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
26067c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26077c478bd9Sstevel@tonic-gate return (error);
26087c478bd9Sstevel@tonic-gate
26090f1702c5SYu Xiangning so_bad: error = sogeterr(so, B_TRUE);
26107c478bd9Sstevel@tonic-gate bad: eprintsoline(so, error);
26117c478bd9Sstevel@tonic-gate goto done;
26127c478bd9Sstevel@tonic-gate }
26137c478bd9Sstevel@tonic-gate
26140f1702c5SYu Xiangning /* ARGSUSED */
26157c478bd9Sstevel@tonic-gate int
sotpi_shutdown(struct sonode * so,int how,struct cred * cr)26160f1702c5SYu Xiangning sotpi_shutdown(struct sonode *so, int how, struct cred *cr)
26177c478bd9Sstevel@tonic-gate {
26187c478bd9Sstevel@tonic-gate struct T_ordrel_req ordrel_req;
26197c478bd9Sstevel@tonic-gate mblk_t *mp;
26207c478bd9Sstevel@tonic-gate uint_t old_state, state_change;
26217c478bd9Sstevel@tonic-gate int error = 0;
26220f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
26237c478bd9Sstevel@tonic-gate
26247c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
2625903a11ebSrh87107 (void *)so, how, pr_state(so->so_state, so->so_mode)));
26267c478bd9Sstevel@tonic-gate
26277c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26287c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
26297c478bd9Sstevel@tonic-gate
26307c478bd9Sstevel@tonic-gate /*
26317c478bd9Sstevel@tonic-gate * SunOS 4.X has no check for datagram sockets.
26327c478bd9Sstevel@tonic-gate * 5.X checks that it is connected (ENOTCONN)
26337c478bd9Sstevel@tonic-gate * X/Open requires that we check the connected state.
26347c478bd9Sstevel@tonic-gate */
26357c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) {
26367c478bd9Sstevel@tonic-gate if (!xnet_skip_checks) {
26377c478bd9Sstevel@tonic-gate error = ENOTCONN;
26387c478bd9Sstevel@tonic-gate if (xnet_check_print) {
26397c478bd9Sstevel@tonic-gate printf("sockfs: X/Open shutdown check "
26407c478bd9Sstevel@tonic-gate "caused ENOTCONN\n");
26417c478bd9Sstevel@tonic-gate }
26427c478bd9Sstevel@tonic-gate }
26437c478bd9Sstevel@tonic-gate goto done;
26447c478bd9Sstevel@tonic-gate }
26457c478bd9Sstevel@tonic-gate /*
26467c478bd9Sstevel@tonic-gate * Record the current state and then perform any state changes.
26477c478bd9Sstevel@tonic-gate * Then use the difference between the old and new states to
26487c478bd9Sstevel@tonic-gate * determine which messages need to be sent.
26497c478bd9Sstevel@tonic-gate * This prevents e.g. duplicate T_ORDREL_REQ when there are
26507c478bd9Sstevel@tonic-gate * duplicate calls to shutdown().
26517c478bd9Sstevel@tonic-gate */
26527c478bd9Sstevel@tonic-gate old_state = so->so_state;
26537c478bd9Sstevel@tonic-gate
26547c478bd9Sstevel@tonic-gate switch (how) {
26557c478bd9Sstevel@tonic-gate case 0:
26567c478bd9Sstevel@tonic-gate socantrcvmore(so);
26577c478bd9Sstevel@tonic-gate break;
26587c478bd9Sstevel@tonic-gate case 1:
26597c478bd9Sstevel@tonic-gate socantsendmore(so);
26607c478bd9Sstevel@tonic-gate break;
26617c478bd9Sstevel@tonic-gate case 2:
26627c478bd9Sstevel@tonic-gate socantsendmore(so);
26637c478bd9Sstevel@tonic-gate socantrcvmore(so);
26647c478bd9Sstevel@tonic-gate break;
26657c478bd9Sstevel@tonic-gate default:
26667c478bd9Sstevel@tonic-gate error = EINVAL;
26677c478bd9Sstevel@tonic-gate goto done;
26687c478bd9Sstevel@tonic-gate }
26697c478bd9Sstevel@tonic-gate
26707c478bd9Sstevel@tonic-gate /*
26717c478bd9Sstevel@tonic-gate * Assumes that the SS_CANT* flags are never cleared in the above code.
26727c478bd9Sstevel@tonic-gate */
26737c478bd9Sstevel@tonic-gate state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
26747c478bd9Sstevel@tonic-gate (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
26757c478bd9Sstevel@tonic-gate ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
26767c478bd9Sstevel@tonic-gate
26777c478bd9Sstevel@tonic-gate switch (state_change) {
26787c478bd9Sstevel@tonic-gate case 0:
26797c478bd9Sstevel@tonic-gate dprintso(so, 1,
26807c478bd9Sstevel@tonic-gate ("sotpi_shutdown: nothing to send in state 0x%x\n",
26817c478bd9Sstevel@tonic-gate so->so_state));
26827c478bd9Sstevel@tonic-gate goto done;
26837c478bd9Sstevel@tonic-gate
26847c478bd9Sstevel@tonic-gate case SS_CANTRCVMORE:
26857c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26867c478bd9Sstevel@tonic-gate strseteof(SOTOV(so), 1);
26877c478bd9Sstevel@tonic-gate /*
26887c478bd9Sstevel@tonic-gate * strseteof takes care of read side wakeups,
26897c478bd9Sstevel@tonic-gate * pollwakeups, and signals.
26907c478bd9Sstevel@tonic-gate */
26917c478bd9Sstevel@tonic-gate /*
26927c478bd9Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems
26937c478bd9Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
26947c478bd9Sstevel@tonic-gate */
26957c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
26967c478bd9Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */
26977c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
26987c478bd9Sstevel@tonic-gate
26997c478bd9Sstevel@tonic-gate /* Flush read side queue */
27007c478bd9Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL);
27017c478bd9Sstevel@tonic-gate
27027c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
27037c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
27047c478bd9Sstevel@tonic-gate break;
27057c478bd9Sstevel@tonic-gate
27067c478bd9Sstevel@tonic-gate case SS_CANTSENDMORE:
27077c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
27087c478bd9Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
27097c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
27107c478bd9Sstevel@tonic-gate break;
27117c478bd9Sstevel@tonic-gate
27127c478bd9Sstevel@tonic-gate case SS_CANTSENDMORE|SS_CANTRCVMORE:
27137c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
27147c478bd9Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
27157c478bd9Sstevel@tonic-gate strseteof(SOTOV(so), 1);
27167c478bd9Sstevel@tonic-gate /*
27177c478bd9Sstevel@tonic-gate * strseteof takes care of read side wakeups,
27187c478bd9Sstevel@tonic-gate * pollwakeups, and signals.
27197c478bd9Sstevel@tonic-gate */
27207c478bd9Sstevel@tonic-gate /*
27217c478bd9Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems
27227c478bd9Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
27237c478bd9Sstevel@tonic-gate */
27247c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
27257c478bd9Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */
27267c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
27277c478bd9Sstevel@tonic-gate
27287c478bd9Sstevel@tonic-gate /* Flush read side queue */
27297c478bd9Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL);
27307c478bd9Sstevel@tonic-gate
27317c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
27327c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
27337c478bd9Sstevel@tonic-gate break;
27347c478bd9Sstevel@tonic-gate }
27357c478bd9Sstevel@tonic-gate
27367c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
27377c478bd9Sstevel@tonic-gate
27387c478bd9Sstevel@tonic-gate /*
27397c478bd9Sstevel@tonic-gate * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
27407c478bd9Sstevel@tonic-gate * was set due to this call and the new state has both of them set:
27417c478bd9Sstevel@tonic-gate * Send the AF_UNIX close indication
27427c478bd9Sstevel@tonic-gate * For T_COTS send a discon_ind
27437c478bd9Sstevel@tonic-gate *
27447c478bd9Sstevel@tonic-gate * If cantsend was set due to this call:
27457c478bd9Sstevel@tonic-gate * For T_COTSORD send an ordrel_ind
27467c478bd9Sstevel@tonic-gate *
27477c478bd9Sstevel@tonic-gate * Note that for T_CLTS there is no message sent here.
27487c478bd9Sstevel@tonic-gate */
27497c478bd9Sstevel@tonic-gate if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
27507c478bd9Sstevel@tonic-gate (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
27517c478bd9Sstevel@tonic-gate /*
27527c478bd9Sstevel@tonic-gate * For SunOS 4.X compatibility we tell the other end
27537c478bd9Sstevel@tonic-gate * that we are unable to receive at this point.
27547c478bd9Sstevel@tonic-gate */
27550f1702c5SYu Xiangning if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS)
27567c478bd9Sstevel@tonic-gate so_unix_close(so);
27577c478bd9Sstevel@tonic-gate
27580f1702c5SYu Xiangning if (sti->sti_serv_type == T_COTS)
27597c478bd9Sstevel@tonic-gate error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
27607c478bd9Sstevel@tonic-gate }
27617c478bd9Sstevel@tonic-gate if ((state_change & SS_CANTSENDMORE) &&
27620f1702c5SYu Xiangning (sti->sti_serv_type == T_COTS_ORD)) {
27637c478bd9Sstevel@tonic-gate /* Send an orderly release */
27647c478bd9Sstevel@tonic-gate ordrel_req.PRIM_type = T_ORDREL_REQ;
27657c478bd9Sstevel@tonic-gate
27667c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
27677c478bd9Sstevel@tonic-gate mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
2768de8c4a14SErik Nordmark 0, _ALLOC_SLEEP, cr);
27697c478bd9Sstevel@tonic-gate /*
27707c478bd9Sstevel@tonic-gate * Send down the T_ORDREL_REQ even if there is flow control.
27717c478bd9Sstevel@tonic-gate * This prevents shutdown from blocking.
27727c478bd9Sstevel@tonic-gate * Note that there is no T_OK_ACK for ordrel_req.
27737c478bd9Sstevel@tonic-gate */
27747c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
27757c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
27767c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
27777c478bd9Sstevel@tonic-gate if (error) {
27787c478bd9Sstevel@tonic-gate eprintsoline(so, error);
27797c478bd9Sstevel@tonic-gate goto done;
27807c478bd9Sstevel@tonic-gate }
27817c478bd9Sstevel@tonic-gate }
27827c478bd9Sstevel@tonic-gate
27837c478bd9Sstevel@tonic-gate done:
27847c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
27857c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
27867c478bd9Sstevel@tonic-gate return (error);
27877c478bd9Sstevel@tonic-gate }
27887c478bd9Sstevel@tonic-gate
27897c478bd9Sstevel@tonic-gate /*
27907c478bd9Sstevel@tonic-gate * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
27917c478bd9Sstevel@tonic-gate * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
27927c478bd9Sstevel@tonic-gate * that we have closed.
27937c478bd9Sstevel@tonic-gate * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
27947c478bd9Sstevel@tonic-gate * T_UNITDATA_REQ containing the same option.
27957c478bd9Sstevel@tonic-gate *
27967c478bd9Sstevel@tonic-gate * For SOCK_DGRAM half-connections (somebody connected to this end
27977c478bd9Sstevel@tonic-gate * but this end is not connect) we don't know where to send any
27987c478bd9Sstevel@tonic-gate * SO_UNIX_CLOSE.
27997c478bd9Sstevel@tonic-gate *
28007c478bd9Sstevel@tonic-gate * We have to ignore stream head errors just in case there has been
28017c478bd9Sstevel@tonic-gate * a shutdown(output).
28027c478bd9Sstevel@tonic-gate * Ignore any flow control to try to get the message more quickly to the peer.
28037c478bd9Sstevel@tonic-gate * While locally ignoring flow control solves the problem when there
28047c478bd9Sstevel@tonic-gate * is only the loopback transport on the stream it would not provide
28057c478bd9Sstevel@tonic-gate * the correct AF_UNIX socket semantics when one or more modules have
28067c478bd9Sstevel@tonic-gate * been pushed.
28077c478bd9Sstevel@tonic-gate */
28087c478bd9Sstevel@tonic-gate void
so_unix_close(struct sonode * so)28097c478bd9Sstevel@tonic-gate so_unix_close(struct sonode *so)
28107c478bd9Sstevel@tonic-gate {
28117c478bd9Sstevel@tonic-gate struct T_opthdr toh;
28127c478bd9Sstevel@tonic-gate mblk_t *mp;
28130f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
28147c478bd9Sstevel@tonic-gate
28157c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock));
28167c478bd9Sstevel@tonic-gate
28177c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX);
28187c478bd9Sstevel@tonic-gate
28197c478bd9Sstevel@tonic-gate if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
28207c478bd9Sstevel@tonic-gate (SS_ISCONNECTED|SS_ISBOUND))
28217c478bd9Sstevel@tonic-gate return;
28227c478bd9Sstevel@tonic-gate
28237c478bd9Sstevel@tonic-gate dprintso(so, 1, ("so_unix_close(%p) %s\n",
2824903a11ebSrh87107 (void *)so, pr_state(so->so_state, so->so_mode)));
28257c478bd9Sstevel@tonic-gate
28267c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
28277c478bd9Sstevel@tonic-gate toh.name = SO_UNIX_CLOSE;
28287c478bd9Sstevel@tonic-gate
28297c478bd9Sstevel@tonic-gate /* zero length + header */
28307c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
28317c478bd9Sstevel@tonic-gate toh.status = 0;
28327c478bd9Sstevel@tonic-gate
28337c478bd9Sstevel@tonic-gate if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
28347c478bd9Sstevel@tonic-gate struct T_optdata_req tdr;
28357c478bd9Sstevel@tonic-gate
28367c478bd9Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ;
28377c478bd9Sstevel@tonic-gate tdr.DATA_flag = 0;
28387c478bd9Sstevel@tonic-gate
28397c478bd9Sstevel@tonic-gate tdr.OPT_length = (t_scalar_t)sizeof (toh);
28407c478bd9Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
28417c478bd9Sstevel@tonic-gate
28427c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
28437c478bd9Sstevel@tonic-gate mp = soallocproto2(&tdr, sizeof (tdr),
2844de8c4a14SErik Nordmark &toh, sizeof (toh), 0, _ALLOC_SLEEP, CRED());
28457c478bd9Sstevel@tonic-gate } else {
28467c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
28477c478bd9Sstevel@tonic-gate void *addr;
28487c478bd9Sstevel@tonic-gate socklen_t addrlen;
28497c478bd9Sstevel@tonic-gate void *src;
28507c478bd9Sstevel@tonic-gate socklen_t srclen;
28517c478bd9Sstevel@tonic-gate struct T_opthdr toh2;
28527c478bd9Sstevel@tonic-gate t_scalar_t size;
28537c478bd9Sstevel@tonic-gate
28547c478bd9Sstevel@tonic-gate /*
2855cbc6e898SGordon Ross * We know this is an AF_UNIX connected DGRAM socket.
2856cbc6e898SGordon Ross * We therefore already have the destination address
2857cbc6e898SGordon Ross * in the internal form needed for this send. This is
2858cbc6e898SGordon Ross * similar to the sosend_dgram call later in this file
2859cbc6e898SGordon Ross * when there's no user-specified destination address.
28607c478bd9Sstevel@tonic-gate */
28610f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
28627c478bd9Sstevel@tonic-gate /*
28637c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
28647c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
28657c478bd9Sstevel@tonic-gate */
28660f1702c5SYu Xiangning addr = sti->sti_faddr_sa;
28670f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_len;
28687c478bd9Sstevel@tonic-gate src = NULL;
28697c478bd9Sstevel@tonic-gate srclen = 0;
28707c478bd9Sstevel@tonic-gate } else {
28717c478bd9Sstevel@tonic-gate /*
28727c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
28737c478bd9Sstevel@tonic-gate * and translate the remote address.
28740f1702c5SYu Xiangning * Holding so_lock thus sti_laddr_sa can not change.
28757c478bd9Sstevel@tonic-gate */
28760f1702c5SYu Xiangning src = sti->sti_laddr_sa;
28770f1702c5SYu Xiangning srclen = (socklen_t)sti->sti_laddr_len;
28787c478bd9Sstevel@tonic-gate dprintso(so, 1,
28797c478bd9Sstevel@tonic-gate ("so_ux_close: srclen %d, src %p\n",
28807c478bd9Sstevel@tonic-gate srclen, src));
2881cbc6e898SGordon Ross /*
2882cbc6e898SGordon Ross * Use the destination address saved in connect.
2883cbc6e898SGordon Ross */
2884cbc6e898SGordon Ross addr = &sti->sti_ux_faddr;
2885cbc6e898SGordon Ross addrlen = sizeof (sti->sti_ux_faddr);
28867c478bd9Sstevel@tonic-gate }
28877c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
28887c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
28897c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
28907c478bd9Sstevel@tonic-gate if (srclen == 0) {
28917c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)sizeof (toh);
28927c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
28937c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
28947c478bd9Sstevel@tonic-gate
28957c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
28967c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
28977c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
2898de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_SLEEP, CRED());
28997c478bd9Sstevel@tonic-gate mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
29007c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
29017c478bd9Sstevel@tonic-gate } else {
29027c478bd9Sstevel@tonic-gate /*
29037c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a
29047c478bd9Sstevel@tonic-gate * source address option.
29057c478bd9Sstevel@tonic-gate */
29067c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
29077c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen));
29087c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
29097c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
29107c478bd9Sstevel@tonic-gate
29117c478bd9Sstevel@tonic-gate toh2.level = SOL_SOCKET;
29127c478bd9Sstevel@tonic-gate toh2.name = SO_SRCADDR;
29137c478bd9Sstevel@tonic-gate toh2.len = (t_uscalar_t)(srclen +
29147c478bd9Sstevel@tonic-gate sizeof (struct T_opthdr));
29157c478bd9Sstevel@tonic-gate toh2.status = 0;
29167c478bd9Sstevel@tonic-gate
29177c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
29187c478bd9Sstevel@tonic-gate
29197c478bd9Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */
29207c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
2921de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_SLEEP, CRED());
29227c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
29237c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
29247c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh2, sizeof (toh2));
29257c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
29267c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
29277c478bd9Sstevel@tonic-gate }
29287c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
29297c478bd9Sstevel@tonic-gate }
29307c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
2931cbc6e898SGordon Ross (void) kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
29327c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
29337c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
29347c478bd9Sstevel@tonic-gate }
29357c478bd9Sstevel@tonic-gate
29367c478bd9Sstevel@tonic-gate /*
29377c478bd9Sstevel@tonic-gate * Called by sotpi_recvmsg when reading a non-zero amount of data.
29387c478bd9Sstevel@tonic-gate * In addition, the caller typically verifies that there is some
29397c478bd9Sstevel@tonic-gate * potential state to clear by checking
29407c478bd9Sstevel@tonic-gate * if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
29417c478bd9Sstevel@tonic-gate * before calling this routine.
29427c478bd9Sstevel@tonic-gate * Note that such a check can be made without holding so_lock since
29437c478bd9Sstevel@tonic-gate * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
29440f1702c5SYu Xiangning * decrements sti_oobsigcnt.
29457c478bd9Sstevel@tonic-gate *
29467c478bd9Sstevel@tonic-gate * When data is read *after* the point that all pending
29477c478bd9Sstevel@tonic-gate * oob data has been consumed the oob indication is cleared.
29487c478bd9Sstevel@tonic-gate *
29497c478bd9Sstevel@tonic-gate * This logic keeps select/poll returning POLLRDBAND and
29507c478bd9Sstevel@tonic-gate * SIOCATMARK returning true until we have read past
29517c478bd9Sstevel@tonic-gate * the mark.
29527c478bd9Sstevel@tonic-gate */
29537c478bd9Sstevel@tonic-gate static void
sorecv_update_oobstate(struct sonode * so)29547c478bd9Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so)
29557c478bd9Sstevel@tonic-gate {
29560f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
29570f1702c5SYu Xiangning
29587c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
29597c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
29607c478bd9Sstevel@tonic-gate dprintso(so, 1,
29617c478bd9Sstevel@tonic-gate ("sorecv_update_oobstate: counts %d/%d state %s\n",
29620f1702c5SYu Xiangning sti->sti_oobsigcnt,
29630f1702c5SYu Xiangning sti->sti_oobcnt, pr_state(so->so_state, so->so_mode)));
29640f1702c5SYu Xiangning if (sti->sti_oobsigcnt == 0) {
29657c478bd9Sstevel@tonic-gate /* No more pending oob indications */
29667c478bd9Sstevel@tonic-gate so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
29677c478bd9Sstevel@tonic-gate freemsg(so->so_oobmsg);
29687c478bd9Sstevel@tonic-gate so->so_oobmsg = NULL;
29697c478bd9Sstevel@tonic-gate }
29707c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
29717c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
29727c478bd9Sstevel@tonic-gate }
29737c478bd9Sstevel@tonic-gate
29747c478bd9Sstevel@tonic-gate /*
29757c478bd9Sstevel@tonic-gate * Handle recv* calls for an so which has NL7C saved recv mblk_t(s).
29767c478bd9Sstevel@tonic-gate */
29777c478bd9Sstevel@tonic-gate static int
nl7c_sorecv(struct sonode * so,mblk_t ** rmp,uio_t * uiop,rval_t * rp)29787c478bd9Sstevel@tonic-gate nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp)
29797c478bd9Sstevel@tonic-gate {
29800f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
29817c478bd9Sstevel@tonic-gate int error = 0;
29827c478bd9Sstevel@tonic-gate mblk_t *tmp = NULL;
29837c478bd9Sstevel@tonic-gate mblk_t *pmp = NULL;
29840f1702c5SYu Xiangning mblk_t *nmp = sti->sti_nl7c_rcv_mp;
29857c478bd9Sstevel@tonic-gate
29867c478bd9Sstevel@tonic-gate ASSERT(nmp != NULL);
29877c478bd9Sstevel@tonic-gate
29887c478bd9Sstevel@tonic-gate while (nmp != NULL && uiop->uio_resid > 0) {
29897c478bd9Sstevel@tonic-gate ssize_t n;
29907c478bd9Sstevel@tonic-gate
29917c478bd9Sstevel@tonic-gate if (DB_TYPE(nmp) == M_DATA) {
29927c478bd9Sstevel@tonic-gate /*
29937c478bd9Sstevel@tonic-gate * We have some data, uiomove up to resid bytes.
29947c478bd9Sstevel@tonic-gate */
29957c478bd9Sstevel@tonic-gate n = MIN(MBLKL(nmp), uiop->uio_resid);
29967c478bd9Sstevel@tonic-gate if (n > 0)
29977c478bd9Sstevel@tonic-gate error = uiomove(nmp->b_rptr, n, UIO_READ, uiop);
29987c478bd9Sstevel@tonic-gate nmp->b_rptr += n;
29997c478bd9Sstevel@tonic-gate if (nmp->b_rptr == nmp->b_wptr) {
30007c478bd9Sstevel@tonic-gate pmp = nmp;
30017c478bd9Sstevel@tonic-gate nmp = nmp->b_cont;
30027c478bd9Sstevel@tonic-gate }
30032c9e429eSbrutus if (error)
30042c9e429eSbrutus break;
30057c478bd9Sstevel@tonic-gate } else {
30067c478bd9Sstevel@tonic-gate /*
30077c478bd9Sstevel@tonic-gate * We only handle data, save for caller to handle.
30087c478bd9Sstevel@tonic-gate */
30097c478bd9Sstevel@tonic-gate if (pmp != NULL) {
30107c478bd9Sstevel@tonic-gate pmp->b_cont = nmp->b_cont;
30117c478bd9Sstevel@tonic-gate }
30127c478bd9Sstevel@tonic-gate nmp->b_cont = NULL;
30137c478bd9Sstevel@tonic-gate if (*rmp == NULL) {
30147c478bd9Sstevel@tonic-gate *rmp = nmp;
30157c478bd9Sstevel@tonic-gate } else {
30162c9e429eSbrutus tmp->b_cont = nmp;
30177c478bd9Sstevel@tonic-gate }
30187c478bd9Sstevel@tonic-gate nmp = nmp->b_cont;
30197c478bd9Sstevel@tonic-gate tmp = nmp;
30207c478bd9Sstevel@tonic-gate }
30217c478bd9Sstevel@tonic-gate }
30227c478bd9Sstevel@tonic-gate if (pmp != NULL) {
30237c478bd9Sstevel@tonic-gate /* Free any mblk_t(s) which we have consumed */
30247c478bd9Sstevel@tonic-gate pmp->b_cont = NULL;
30250f1702c5SYu Xiangning freemsg(sti->sti_nl7c_rcv_mp);
30267c478bd9Sstevel@tonic-gate }
30270f1702c5SYu Xiangning if ((sti->sti_nl7c_rcv_mp = nmp) == NULL) {
30282c9e429eSbrutus /* Last mblk_t so return the saved kstrgetmsg() rval/error */
30292c9e429eSbrutus if (error == 0) {
30300f1702c5SYu Xiangning rval_t *p = (rval_t *)&sti->sti_nl7c_rcv_rval;
30312c9e429eSbrutus
30322c9e429eSbrutus error = p->r_v.r_v2;
30332c9e429eSbrutus p->r_v.r_v2 = 0;
30342c9e429eSbrutus }
30350f1702c5SYu Xiangning rp->r_vals = sti->sti_nl7c_rcv_rval;
30360f1702c5SYu Xiangning sti->sti_nl7c_rcv_rval = 0;
30377c478bd9Sstevel@tonic-gate } else {
30387c478bd9Sstevel@tonic-gate /* More mblk_t(s) to process so no rval to return */
30397c478bd9Sstevel@tonic-gate rp->r_vals = 0;
30407c478bd9Sstevel@tonic-gate }
30417c478bd9Sstevel@tonic-gate return (error);
30427c478bd9Sstevel@tonic-gate }
30437c478bd9Sstevel@tonic-gate /*
30447c478bd9Sstevel@tonic-gate * Receive the next message on the queue.
30457c478bd9Sstevel@tonic-gate * If msg_controllen is non-zero when called the caller is interested in
30467c478bd9Sstevel@tonic-gate * any received control info (options).
30477c478bd9Sstevel@tonic-gate * If msg_namelen is non-zero when called the caller is interested in
30487c478bd9Sstevel@tonic-gate * any received source address.
30497c478bd9Sstevel@tonic-gate * The routine returns with msg_control and msg_name pointing to
30507c478bd9Sstevel@tonic-gate * kmem_alloc'ed memory which the caller has to free.
30517c478bd9Sstevel@tonic-gate */
30520f1702c5SYu Xiangning /* ARGSUSED */
30537c478bd9Sstevel@tonic-gate int
sotpi_recvmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)30540f1702c5SYu Xiangning sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
30550f1702c5SYu Xiangning struct cred *cr)
30567c478bd9Sstevel@tonic-gate {
30577c478bd9Sstevel@tonic-gate union T_primitives *tpr;
30587c478bd9Sstevel@tonic-gate mblk_t *mp;
30597c478bd9Sstevel@tonic-gate uchar_t pri;
30607c478bd9Sstevel@tonic-gate int pflag, opflag;
30617c478bd9Sstevel@tonic-gate void *control;
30627c478bd9Sstevel@tonic-gate t_uscalar_t controllen;
30637c478bd9Sstevel@tonic-gate t_uscalar_t namelen;
30647c478bd9Sstevel@tonic-gate int so_state = so->so_state; /* Snapshot */
30657c478bd9Sstevel@tonic-gate ssize_t saved_resid;
30667c478bd9Sstevel@tonic-gate rval_t rval;
30677c478bd9Sstevel@tonic-gate int flags;
30687c478bd9Sstevel@tonic-gate clock_t timout;
306917169044Sbrutus int error = 0;
30700f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
30717c478bd9Sstevel@tonic-gate
30727c478bd9Sstevel@tonic-gate flags = msg->msg_flags;
30737c478bd9Sstevel@tonic-gate msg->msg_flags = 0;
30747c478bd9Sstevel@tonic-gate
30757c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
3076903a11ebSrh87107 (void *)so, (void *)msg, flags,
30777c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode), so->so_error));
30787c478bd9Sstevel@tonic-gate
30790f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
30800f1702c5SYu Xiangning so_update_attrs(so, SOACC);
30810f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
30820f1702c5SYu Xiangning return (strread(SOTOV(so), uiop, cr));
30830f1702c5SYu Xiangning }
30840f1702c5SYu Xiangning
30857c478bd9Sstevel@tonic-gate /*
30867c478bd9Sstevel@tonic-gate * If we are not connected because we have never been connected
30877c478bd9Sstevel@tonic-gate * we return ENOTCONN. If we have been connected (but are no longer
30887c478bd9Sstevel@tonic-gate * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
30897c478bd9Sstevel@tonic-gate * the EOF.
30907c478bd9Sstevel@tonic-gate *
30917c478bd9Sstevel@tonic-gate * An alternative would be to post an ENOTCONN error in stream head
30927c478bd9Sstevel@tonic-gate * (read+write) and clear it when we're connected. However, that error
30937c478bd9Sstevel@tonic-gate * would cause incorrect poll/select behavior!
30947c478bd9Sstevel@tonic-gate */
30957c478bd9Sstevel@tonic-gate if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
30967c478bd9Sstevel@tonic-gate (so->so_mode & SM_CONNREQUIRED)) {
30977c478bd9Sstevel@tonic-gate return (ENOTCONN);
30987c478bd9Sstevel@tonic-gate }
30997c478bd9Sstevel@tonic-gate
31007c478bd9Sstevel@tonic-gate /*
31017c478bd9Sstevel@tonic-gate * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
31027c478bd9Sstevel@tonic-gate * after checking that the read queue is empty) and returns zero.
31037c478bd9Sstevel@tonic-gate * This implementation will sleep (in kstrgetmsg) even if uio_resid
31047c478bd9Sstevel@tonic-gate * is zero.
31057c478bd9Sstevel@tonic-gate */
31067c478bd9Sstevel@tonic-gate
31077c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
31087c478bd9Sstevel@tonic-gate /* Check that the transport supports OOB */
31097c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_EXDATA))
31107c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
31110f1702c5SYu Xiangning so_update_attrs(so, SOACC);
31120f1702c5SYu Xiangning return (sorecvoob(so, msg, uiop, flags,
31130f1702c5SYu Xiangning (so->so_options & SO_OOBINLINE)));
31147c478bd9Sstevel@tonic-gate }
31157c478bd9Sstevel@tonic-gate
31160f1702c5SYu Xiangning so_update_attrs(so, SOACC);
31170f1702c5SYu Xiangning
31187c478bd9Sstevel@tonic-gate /*
31197c478bd9Sstevel@tonic-gate * Set msg_controllen and msg_namelen to zero here to make it
31207c478bd9Sstevel@tonic-gate * simpler in the cases that no control or name is returned.
31217c478bd9Sstevel@tonic-gate */
31227c478bd9Sstevel@tonic-gate controllen = msg->msg_controllen;
31237c478bd9Sstevel@tonic-gate namelen = msg->msg_namelen;
31247c478bd9Sstevel@tonic-gate msg->msg_controllen = 0;
31257c478bd9Sstevel@tonic-gate msg->msg_namelen = 0;
31267c478bd9Sstevel@tonic-gate
31277c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
31287c478bd9Sstevel@tonic-gate namelen, controllen));
31297c478bd9Sstevel@tonic-gate
31302c9e429eSbrutus mutex_enter(&so->so_lock);
31317c478bd9Sstevel@tonic-gate /*
31327c478bd9Sstevel@tonic-gate * If an NL7C enabled socket and not waiting for write data.
31337c478bd9Sstevel@tonic-gate */
31340f1702c5SYu Xiangning if ((sti->sti_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) ==
31357c478bd9Sstevel@tonic-gate NL7C_ENABLED) {
31360f1702c5SYu Xiangning if (sti->sti_nl7c_uri) {
31372c9e429eSbrutus /* Close uri processing for a previous request */
31387c478bd9Sstevel@tonic-gate nl7c_close(so);
31397c478bd9Sstevel@tonic-gate }
31400f1702c5SYu Xiangning if ((so_state & SS_CANTRCVMORE) &&
31410f1702c5SYu Xiangning sti->sti_nl7c_rcv_mp == NULL) {
31422c9e429eSbrutus /* Nothing to process, EOF */
31432c9e429eSbrutus mutex_exit(&so->so_lock);
31442c9e429eSbrutus return (0);
31450f1702c5SYu Xiangning } else if (sti->sti_nl7c_flags & NL7C_SOPERSIST) {
31462c9e429eSbrutus /* Persistent NL7C socket, try to process request */
31472c9e429eSbrutus boolean_t ret;
31482c9e429eSbrutus
31492c9e429eSbrutus ret = nl7c_process(so,
31502c9e429eSbrutus (so->so_state & (SS_NONBLOCK|SS_NDELAY)));
31510f1702c5SYu Xiangning rval.r_vals = sti->sti_nl7c_rcv_rval;
31522c9e429eSbrutus error = rval.r_v.r_v2;
31532c9e429eSbrutus if (error) {
31542c9e429eSbrutus /* Error of some sort, return it */
31552c9e429eSbrutus mutex_exit(&so->so_lock);
31562c9e429eSbrutus return (error);
31572c9e429eSbrutus }
31580f1702c5SYu Xiangning if (sti->sti_nl7c_flags &&
31590f1702c5SYu Xiangning ! (sti->sti_nl7c_flags & NL7C_WAITWRITE)) {
31607c478bd9Sstevel@tonic-gate /*
31612c9e429eSbrutus * Still an NL7C socket and no data
31622c9e429eSbrutus * to pass up to the caller.
31632c9e429eSbrutus */
31642c9e429eSbrutus mutex_exit(&so->so_lock);
31652c9e429eSbrutus if (ret) {
31662c9e429eSbrutus /* EOF */
31672c9e429eSbrutus return (0);
31682c9e429eSbrutus } else {
31692c9e429eSbrutus /* Need more data */
31702c9e429eSbrutus return (EAGAIN);
31712c9e429eSbrutus }
31722c9e429eSbrutus }
31732c9e429eSbrutus } else {
31742c9e429eSbrutus /*
31752c9e429eSbrutus * Not persistent so no further NL7C processing.
31767c478bd9Sstevel@tonic-gate */
31770f1702c5SYu Xiangning sti->sti_nl7c_flags = 0;
31787c478bd9Sstevel@tonic-gate }
31797c478bd9Sstevel@tonic-gate }
31807c478bd9Sstevel@tonic-gate /*
31817c478bd9Sstevel@tonic-gate * Only one reader is allowed at any given time. This is needed
31827c478bd9Sstevel@tonic-gate * for T_EXDATA handling and, in the future, MSG_WAITALL.
31837c478bd9Sstevel@tonic-gate *
31847c478bd9Sstevel@tonic-gate * This is slightly different that BSD behavior in that it fails with
31857c478bd9Sstevel@tonic-gate * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
31867c478bd9Sstevel@tonic-gate * is single-threaded using sblock(), which is dropped while waiting
31877c478bd9Sstevel@tonic-gate * for data to appear. The difference shows up e.g. if one
31887c478bd9Sstevel@tonic-gate * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
31897c478bd9Sstevel@tonic-gate * does use nonblocking io and different threads are reading each
31907c478bd9Sstevel@tonic-gate * file descriptor. In BSD there would never be an EWOULDBLOCK error
31917c478bd9Sstevel@tonic-gate * in this case as long as the read queue doesn't get empty.
31927c478bd9Sstevel@tonic-gate * In this implementation the thread using nonblocking io can
31937c478bd9Sstevel@tonic-gate * get an EWOULDBLOCK error due to the blocking thread executing
31947c478bd9Sstevel@tonic-gate * e.g. in the uiomove in kstrgetmsg.
31957c478bd9Sstevel@tonic-gate * This difference is not believed to be significant.
31967c478bd9Sstevel@tonic-gate */
3197255daac4Sethindra /* Set SOREADLOCKED */
3198255daac4Sethindra error = so_lock_read_intr(so,
3199255daac4Sethindra uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
32007c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
32017c478bd9Sstevel@tonic-gate if (error)
32027c478bd9Sstevel@tonic-gate return (error);
32037c478bd9Sstevel@tonic-gate
32047c478bd9Sstevel@tonic-gate /*
32057c478bd9Sstevel@tonic-gate * Tell kstrgetmsg to not inspect the stream head errors until all
32067c478bd9Sstevel@tonic-gate * queued data has been consumed.
32077c478bd9Sstevel@tonic-gate * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
32087c478bd9Sstevel@tonic-gate * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
32097c478bd9Sstevel@tonic-gate *
32107c478bd9Sstevel@tonic-gate * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
32117c478bd9Sstevel@tonic-gate * to T_OPTDATA_IND that do not contain any user-visible control msg.
32127c478bd9Sstevel@tonic-gate * Note that MSG_WAITALL set with MSG_PEEK is a noop.
32137c478bd9Sstevel@tonic-gate */
32147c478bd9Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR;
32157c478bd9Sstevel@tonic-gate if (flags & MSG_PEEK) {
32167c478bd9Sstevel@tonic-gate pflag |= MSG_IPEEK;
32177c478bd9Sstevel@tonic-gate flags &= ~MSG_WAITALL;
32187c478bd9Sstevel@tonic-gate }
32197c478bd9Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC)
32207c478bd9Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL;
32217c478bd9Sstevel@tonic-gate
32227c478bd9Sstevel@tonic-gate if (flags & MSG_DONTWAIT)
32237c478bd9Sstevel@tonic-gate timout = 0;
3224412cc9e9SGordon Ross else if (so->so_rcvtimeo != 0)
3225412cc9e9SGordon Ross timout = TICK_TO_MSEC(so->so_rcvtimeo);
32267c478bd9Sstevel@tonic-gate else
32277c478bd9Sstevel@tonic-gate timout = -1;
32287c478bd9Sstevel@tonic-gate opflag = pflag;
32297c478bd9Sstevel@tonic-gate retry:
32307c478bd9Sstevel@tonic-gate saved_resid = uiop->uio_resid;
32317c478bd9Sstevel@tonic-gate pri = 0;
32327c478bd9Sstevel@tonic-gate mp = NULL;
32330f1702c5SYu Xiangning if (sti->sti_nl7c_rcv_mp != NULL) {
32342c9e429eSbrutus /* Already kstrgetmsg()ed saved mblk(s) from NL7C */
32357c478bd9Sstevel@tonic-gate error = nl7c_sorecv(so, &mp, uiop, &rval);
32367c478bd9Sstevel@tonic-gate } else {
32377c478bd9Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
32387c478bd9Sstevel@tonic-gate timout, &rval);
32397c478bd9Sstevel@tonic-gate }
32400f1702c5SYu Xiangning if (error != 0) {
32410f1702c5SYu Xiangning /* kstrgetmsg returns ETIME when timeout expires */
32420f1702c5SYu Xiangning if (error == ETIME)
32437c478bd9Sstevel@tonic-gate error = EWOULDBLOCK;
324417169044Sbrutus goto out;
32457c478bd9Sstevel@tonic-gate }
32467c478bd9Sstevel@tonic-gate /*
32477c478bd9Sstevel@tonic-gate * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
32487c478bd9Sstevel@tonic-gate * For non-datagrams MOREDATA is used to set MSG_EOR.
32497c478bd9Sstevel@tonic-gate */
32507c478bd9Sstevel@tonic-gate ASSERT(!(rval.r_val1 & MORECTL));
32517c478bd9Sstevel@tonic-gate if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
32527c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_TRUNC;
32537c478bd9Sstevel@tonic-gate
32547c478bd9Sstevel@tonic-gate if (mp == NULL) {
32557c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
32567c478bd9Sstevel@tonic-gate /*
32577c478bd9Sstevel@tonic-gate * 4.3BSD and 4.4BSD clears the mark when peeking across it.
32587c478bd9Sstevel@tonic-gate * The draft Posix socket spec states that the mark should
32597c478bd9Sstevel@tonic-gate * not be cleared when peeking. We follow the latter.
32607c478bd9Sstevel@tonic-gate */
32617c478bd9Sstevel@tonic-gate if ((so->so_state &
32627c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32637c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
32647c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
32657c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
32667c478bd9Sstevel@tonic-gate }
32677c478bd9Sstevel@tonic-gate
32687c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
32697c478bd9Sstevel@tonic-gate /* Set MSG_EOR based on MOREDATA */
32707c478bd9Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) {
32717c478bd9Sstevel@tonic-gate if (so->so_state & SS_SAVEDEOR) {
32727c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_EOR;
32737c478bd9Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR;
32747c478bd9Sstevel@tonic-gate }
32757c478bd9Sstevel@tonic-gate }
32767c478bd9Sstevel@tonic-gate /*
32777c478bd9Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the
32787c478bd9Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more.
32797c478bd9Sstevel@tonic-gate */
32807c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
32817c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
32827c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
32837c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
32847c478bd9Sstevel@tonic-gate goto retry;
32857c478bd9Sstevel@tonic-gate }
328617169044Sbrutus goto out_locked;
32877c478bd9Sstevel@tonic-gate }
32887c478bd9Sstevel@tonic-gate
32897c478bd9Sstevel@tonic-gate /* strsock_proto has already verified length and alignment */
32907c478bd9Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr;
32917c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
32927c478bd9Sstevel@tonic-gate
32937c478bd9Sstevel@tonic-gate switch (tpr->type) {
32947c478bd9Sstevel@tonic-gate case T_DATA_IND: {
32957c478bd9Sstevel@tonic-gate if ((so->so_state &
32967c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32977c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
32987c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
32997c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
33007c478bd9Sstevel@tonic-gate }
33017c478bd9Sstevel@tonic-gate
33027c478bd9Sstevel@tonic-gate /*
33037c478bd9Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on
33047c478bd9Sstevel@tonic-gate * MORE_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 */
33197c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
33207c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
33217c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
33227c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
33237c478bd9Sstevel@tonic-gate goto retry;
33247c478bd9Sstevel@tonic-gate }
332517169044Sbrutus goto out_locked;
33267c478bd9Sstevel@tonic-gate }
33277c478bd9Sstevel@tonic-gate case T_UNITDATA_IND: {
33287c478bd9Sstevel@tonic-gate void *addr;
33297c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
33307c478bd9Sstevel@tonic-gate void *abuf;
33317c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
33327c478bd9Sstevel@tonic-gate void *opt;
33337c478bd9Sstevel@tonic-gate
33347c478bd9Sstevel@tonic-gate if ((so->so_state &
33357c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
33367c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
33377c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
33387c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
33397c478bd9Sstevel@tonic-gate }
33407c478bd9Sstevel@tonic-gate
33417c478bd9Sstevel@tonic-gate if (namelen != 0) {
33427c478bd9Sstevel@tonic-gate /* Caller wants source address */
33437c478bd9Sstevel@tonic-gate addrlen = tpr->unitdata_ind.SRC_length;
33447c478bd9Sstevel@tonic-gate addr = sogetoff(mp,
33457c478bd9Sstevel@tonic-gate tpr->unitdata_ind.SRC_offset,
33467c478bd9Sstevel@tonic-gate addrlen, 1);
33477c478bd9Sstevel@tonic-gate if (addr == NULL) {
33487c478bd9Sstevel@tonic-gate freemsg(mp);
33497c478bd9Sstevel@tonic-gate error = EPROTO;
33507c478bd9Sstevel@tonic-gate eprintsoline(so, error);
335117169044Sbrutus goto out;
33527c478bd9Sstevel@tonic-gate }
33537c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
33547c478bd9Sstevel@tonic-gate /*
33557c478bd9Sstevel@tonic-gate * Can not use the transport level address.
33567c478bd9Sstevel@tonic-gate * If there is a SO_SRCADDR option carrying
33577c478bd9Sstevel@tonic-gate * the socket level address it will be
33587c478bd9Sstevel@tonic-gate * extracted below.
33597c478bd9Sstevel@tonic-gate */
33607c478bd9Sstevel@tonic-gate addr = NULL;
33617c478bd9Sstevel@tonic-gate addrlen = 0;
33627c478bd9Sstevel@tonic-gate }
33637c478bd9Sstevel@tonic-gate }
33647c478bd9Sstevel@tonic-gate optlen = tpr->unitdata_ind.OPT_length;
33657c478bd9Sstevel@tonic-gate if (optlen != 0) {
33667c478bd9Sstevel@tonic-gate t_uscalar_t ncontrollen;
33677c478bd9Sstevel@tonic-gate
33687c478bd9Sstevel@tonic-gate /*
33697c478bd9Sstevel@tonic-gate * Extract any source address option.
33707c478bd9Sstevel@tonic-gate * Determine how large cmsg buffer is needed.
33717c478bd9Sstevel@tonic-gate */
33727c478bd9Sstevel@tonic-gate opt = sogetoff(mp,
33737c478bd9Sstevel@tonic-gate tpr->unitdata_ind.OPT_offset,
33747c478bd9Sstevel@tonic-gate optlen, __TPI_ALIGN_SIZE);
33757c478bd9Sstevel@tonic-gate
33767c478bd9Sstevel@tonic-gate if (opt == NULL) {
33777c478bd9Sstevel@tonic-gate freemsg(mp);
33787c478bd9Sstevel@tonic-gate error = EPROTO;
33797c478bd9Sstevel@tonic-gate eprintsoline(so, error);
338017169044Sbrutus goto out;
33817c478bd9Sstevel@tonic-gate }
33827c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX)
33837c478bd9Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
33847c478bd9Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen,
33857c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2));
33867c478bd9Sstevel@tonic-gate if (controllen != 0)
33877c478bd9Sstevel@tonic-gate controllen = ncontrollen;
33887c478bd9Sstevel@tonic-gate else if (ncontrollen != 0)
33897c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC;
33907c478bd9Sstevel@tonic-gate } else {
33917c478bd9Sstevel@tonic-gate controllen = 0;
33927c478bd9Sstevel@tonic-gate }
33937c478bd9Sstevel@tonic-gate
33947c478bd9Sstevel@tonic-gate if (namelen != 0) {
33957c478bd9Sstevel@tonic-gate /*
33967c478bd9Sstevel@tonic-gate * Return address to caller.
33977c478bd9Sstevel@tonic-gate * Caller handles truncation if length
33987c478bd9Sstevel@tonic-gate * exceeds msg_namelen.
33997c478bd9Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by
34007c478bd9Sstevel@tonic-gate * the sender's copyin_name().
34017c478bd9Sstevel@tonic-gate */
34027c478bd9Sstevel@tonic-gate abuf = kmem_alloc(addrlen, KM_SLEEP);
34037c478bd9Sstevel@tonic-gate
34047c478bd9Sstevel@tonic-gate bcopy(addr, abuf, addrlen);
34057c478bd9Sstevel@tonic-gate msg->msg_name = abuf;
34067c478bd9Sstevel@tonic-gate msg->msg_namelen = addrlen;
34077c478bd9Sstevel@tonic-gate }
34087c478bd9Sstevel@tonic-gate
34097c478bd9Sstevel@tonic-gate if (controllen != 0) {
34107c478bd9Sstevel@tonic-gate /*
34117c478bd9Sstevel@tonic-gate * Return control msg to caller.
34127c478bd9Sstevel@tonic-gate * Caller handles truncation if length
34137c478bd9Sstevel@tonic-gate * exceeds msg_controllen.
34147c478bd9Sstevel@tonic-gate */
3415274af231Samehta control = kmem_zalloc(controllen, KM_SLEEP);
34167c478bd9Sstevel@tonic-gate
34177c478bd9Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen,
34187c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2),
34197c478bd9Sstevel@tonic-gate control, controllen);
34207c478bd9Sstevel@tonic-gate if (error) {
34217c478bd9Sstevel@tonic-gate freemsg(mp);
34227c478bd9Sstevel@tonic-gate if (msg->msg_namelen != 0)
34237c478bd9Sstevel@tonic-gate kmem_free(msg->msg_name,
34247c478bd9Sstevel@tonic-gate msg->msg_namelen);
34257c478bd9Sstevel@tonic-gate kmem_free(control, controllen);
34267c478bd9Sstevel@tonic-gate eprintsoline(so, error);
342717169044Sbrutus goto out;
34287c478bd9Sstevel@tonic-gate }
34297c478bd9Sstevel@tonic-gate msg->msg_control = control;
34307c478bd9Sstevel@tonic-gate msg->msg_controllen = controllen;
34317c478bd9Sstevel@tonic-gate }
34327c478bd9Sstevel@tonic-gate
34337c478bd9Sstevel@tonic-gate freemsg(mp);
343417169044Sbrutus goto out;
34357c478bd9Sstevel@tonic-gate }
34367c478bd9Sstevel@tonic-gate case T_OPTDATA_IND: {
34377c478bd9Sstevel@tonic-gate struct T_optdata_req *tdr;
34387c478bd9Sstevel@tonic-gate void *opt;
34397c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
34407c478bd9Sstevel@tonic-gate
34417c478bd9Sstevel@tonic-gate if ((so->so_state &
34427c478bd9Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
34437c478bd9Sstevel@tonic-gate (uiop->uio_resid != saved_resid) &&
34447c478bd9Sstevel@tonic-gate !(flags & MSG_PEEK)) {
34457c478bd9Sstevel@tonic-gate sorecv_update_oobstate(so);
34467c478bd9Sstevel@tonic-gate }
34477c478bd9Sstevel@tonic-gate
34487c478bd9Sstevel@tonic-gate tdr = (struct T_optdata_req *)mp->b_rptr;
34497c478bd9Sstevel@tonic-gate optlen = tdr->OPT_length;
34507c478bd9Sstevel@tonic-gate if (optlen != 0) {
34517c478bd9Sstevel@tonic-gate t_uscalar_t ncontrollen;
34527c478bd9Sstevel@tonic-gate /*
34537c478bd9Sstevel@tonic-gate * Determine how large cmsg buffer is needed.
34547c478bd9Sstevel@tonic-gate */
34557c478bd9Sstevel@tonic-gate opt = sogetoff(mp,
34567c478bd9Sstevel@tonic-gate tpr->optdata_ind.OPT_offset,
34577c478bd9Sstevel@tonic-gate optlen, __TPI_ALIGN_SIZE);
34587c478bd9Sstevel@tonic-gate
34597c478bd9Sstevel@tonic-gate if (opt == NULL) {
34607c478bd9Sstevel@tonic-gate freemsg(mp);
34617c478bd9Sstevel@tonic-gate error = EPROTO;
34627c478bd9Sstevel@tonic-gate eprintsoline(so, error);
346317169044Sbrutus goto out;
34647c478bd9Sstevel@tonic-gate }
34657c478bd9Sstevel@tonic-gate
34667c478bd9Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen,
34677c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2));
34687c478bd9Sstevel@tonic-gate if (controllen != 0)
34697c478bd9Sstevel@tonic-gate controllen = ncontrollen;
34707c478bd9Sstevel@tonic-gate else if (ncontrollen != 0)
34717c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC;
34727c478bd9Sstevel@tonic-gate } else {
34737c478bd9Sstevel@tonic-gate controllen = 0;
34747c478bd9Sstevel@tonic-gate }
34757c478bd9Sstevel@tonic-gate
34767c478bd9Sstevel@tonic-gate if (controllen != 0) {
34777c478bd9Sstevel@tonic-gate /*
34787c478bd9Sstevel@tonic-gate * Return control msg to caller.
34797c478bd9Sstevel@tonic-gate * Caller handles truncation if length
34807c478bd9Sstevel@tonic-gate * exceeds msg_controllen.
34817c478bd9Sstevel@tonic-gate */
3482274af231Samehta control = kmem_zalloc(controllen, KM_SLEEP);
34837c478bd9Sstevel@tonic-gate
34847c478bd9Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen,
34857c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2),
34867c478bd9Sstevel@tonic-gate control, controllen);
34877c478bd9Sstevel@tonic-gate if (error) {
34887c478bd9Sstevel@tonic-gate freemsg(mp);
34897c478bd9Sstevel@tonic-gate kmem_free(control, controllen);
34907c478bd9Sstevel@tonic-gate eprintsoline(so, error);
349117169044Sbrutus goto out;
34927c478bd9Sstevel@tonic-gate }
34937c478bd9Sstevel@tonic-gate msg->msg_control = control;
34947c478bd9Sstevel@tonic-gate msg->msg_controllen = controllen;
34957c478bd9Sstevel@tonic-gate }
34967c478bd9Sstevel@tonic-gate
34977c478bd9Sstevel@tonic-gate /*
34987c478bd9Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on
34997c478bd9Sstevel@tonic-gate * DATA_flag and MOREDATA.
35007c478bd9Sstevel@tonic-gate */
35017c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
35027c478bd9Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR;
35037c478bd9Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) {
35047c478bd9Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA))
35057c478bd9Sstevel@tonic-gate msg->msg_flags |= MSG_EOR;
35067c478bd9Sstevel@tonic-gate else
35077c478bd9Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR;
35087c478bd9Sstevel@tonic-gate }
35097c478bd9Sstevel@tonic-gate freemsg(mp);
35107c478bd9Sstevel@tonic-gate /*
35117c478bd9Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the
35127c478bd9Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more.
35137c478bd9Sstevel@tonic-gate * Not possible to wait if control info was received.
35147c478bd9Sstevel@tonic-gate */
35157c478bd9Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
35167c478bd9Sstevel@tonic-gate controllen == 0 &&
35177c478bd9Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
35187c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
35197c478bd9Sstevel@tonic-gate pflag = opflag | MSG_NOMARK;
35207c478bd9Sstevel@tonic-gate goto retry;
35217c478bd9Sstevel@tonic-gate }
352217169044Sbrutus goto out_locked;
35237c478bd9Sstevel@tonic-gate }
35247c478bd9Sstevel@tonic-gate case T_EXDATA_IND: {
35257c478bd9Sstevel@tonic-gate dprintso(so, 1,
35267c478bd9Sstevel@tonic-gate ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
35277c478bd9Sstevel@tonic-gate "state %s\n",
35280f1702c5SYu Xiangning sti->sti_oobsigcnt, sti->sti_oobcnt,
35297c478bd9Sstevel@tonic-gate saved_resid - uiop->uio_resid,
35307c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
35317c478bd9Sstevel@tonic-gate /*
35327c478bd9Sstevel@tonic-gate * kstrgetmsg handles MSGMARK so there is nothing to
35337c478bd9Sstevel@tonic-gate * inspect in the T_EXDATA_IND.
35347c478bd9Sstevel@tonic-gate * strsock_proto makes the stream head queue the T_EXDATA_IND
35357c478bd9Sstevel@tonic-gate * as a separate message with no M_DATA component. Furthermore,
35367c478bd9Sstevel@tonic-gate * the stream head does not consolidate M_DATA messages onto
35377c478bd9Sstevel@tonic-gate * an MSGMARK'ed message ensuring that the T_EXDATA_IND
35387c478bd9Sstevel@tonic-gate * remains a message by itself. This is needed since MSGMARK
35397c478bd9Sstevel@tonic-gate * marks both the whole message as well as the last byte
35407c478bd9Sstevel@tonic-gate * of the message.
35417c478bd9Sstevel@tonic-gate */
35427c478bd9Sstevel@tonic-gate freemsg(mp);
35437c478bd9Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); /* No data */
35447c478bd9Sstevel@tonic-gate if (flags & MSG_PEEK) {
35457c478bd9Sstevel@tonic-gate /*
35467c478bd9Sstevel@tonic-gate * Even though we are peeking we consume the
35477c478bd9Sstevel@tonic-gate * T_EXDATA_IND thereby moving the mark information
35487c478bd9Sstevel@tonic-gate * to SS_RCVATMARK. Then the oob code below will
35497c478bd9Sstevel@tonic-gate * retry the peeking kstrgetmsg.
35507c478bd9Sstevel@tonic-gate * Note that the stream head read queue is
35517c478bd9Sstevel@tonic-gate * never flushed without holding SOREADLOCKED
35527c478bd9Sstevel@tonic-gate * thus the T_EXDATA_IND can not disappear
35537c478bd9Sstevel@tonic-gate * underneath us.
35547c478bd9Sstevel@tonic-gate */
35557c478bd9Sstevel@tonic-gate dprintso(so, 1,
35567c478bd9Sstevel@tonic-gate ("sotpi_recvmsg: consume EXDATA_IND "
35577c478bd9Sstevel@tonic-gate "counts %d/%d state %s\n",
35580f1702c5SYu Xiangning sti->sti_oobsigcnt,
35590f1702c5SYu Xiangning sti->sti_oobcnt,
35607c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
35617c478bd9Sstevel@tonic-gate
35627c478bd9Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR;
35637c478bd9Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC)
35647c478bd9Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL;
35657c478bd9Sstevel@tonic-gate
35667c478bd9Sstevel@tonic-gate pri = 0;
35677c478bd9Sstevel@tonic-gate mp = NULL;
35687c478bd9Sstevel@tonic-gate
35697c478bd9Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop,
35707c478bd9Sstevel@tonic-gate &pri, &pflag, (clock_t)-1, &rval);
35717c478bd9Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid);
35727c478bd9Sstevel@tonic-gate
35737c478bd9Sstevel@tonic-gate if (error) {
35747c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
35757c478bd9Sstevel@tonic-gate if (error != EWOULDBLOCK && error != EINTR) {
35767c478bd9Sstevel@tonic-gate eprintsoline(so, error);
35777c478bd9Sstevel@tonic-gate }
35787c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
357917169044Sbrutus goto out;
35807c478bd9Sstevel@tonic-gate }
35817c478bd9Sstevel@tonic-gate ASSERT(mp);
35827c478bd9Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr;
35837c478bd9Sstevel@tonic-gate ASSERT(tpr->type == T_EXDATA_IND);
35847c478bd9Sstevel@tonic-gate freemsg(mp);
35857c478bd9Sstevel@tonic-gate } /* end "if (flags & MSG_PEEK)" */
35867c478bd9Sstevel@tonic-gate
35877c478bd9Sstevel@tonic-gate /*
35887c478bd9Sstevel@tonic-gate * Decrement the number of queued and pending oob.
35897c478bd9Sstevel@tonic-gate *
35907c478bd9Sstevel@tonic-gate * SS_RCVATMARK is cleared when we read past a mark.
35917c478bd9Sstevel@tonic-gate * SS_HAVEOOBDATA is cleared when we've read past the
35927c478bd9Sstevel@tonic-gate * last mark.
35937c478bd9Sstevel@tonic-gate * SS_OOBPEND is cleared if we've read past the last
35947c478bd9Sstevel@tonic-gate * mark and no (new) SIGURG has been posted.
35957c478bd9Sstevel@tonic-gate */
35967c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
35977c478bd9Sstevel@tonic-gate ASSERT(so_verify_oobstate(so));
35980f1702c5SYu Xiangning ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
35990f1702c5SYu Xiangning ASSERT(sti->sti_oobsigcnt > 0);
36000f1702c5SYu Xiangning sti->sti_oobsigcnt--;
36010f1702c5SYu Xiangning ASSERT(sti->sti_oobcnt > 0);
36020f1702c5SYu Xiangning sti->sti_oobcnt--;
36037c478bd9Sstevel@tonic-gate /*
36047c478bd9Sstevel@tonic-gate * Since the T_EXDATA_IND has been removed from the stream
36057c478bd9Sstevel@tonic-gate * head, but we have not read data past the mark,
36067c478bd9Sstevel@tonic-gate * sockfs needs to track that the socket is still at the mark.
36077c478bd9Sstevel@tonic-gate *
36087c478bd9Sstevel@tonic-gate * Since no data was received call kstrgetmsg again to wait
36097c478bd9Sstevel@tonic-gate * for data.
36107c478bd9Sstevel@tonic-gate */
36117c478bd9Sstevel@tonic-gate so->so_state |= SS_RCVATMARK;
36127c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
36137c478bd9Sstevel@tonic-gate dprintso(so, 1,
36147c478bd9Sstevel@tonic-gate ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n",
36150f1702c5SYu Xiangning sti->sti_oobsigcnt, sti->sti_oobcnt,
36167c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
36177c478bd9Sstevel@tonic-gate pflag = opflag;
36187c478bd9Sstevel@tonic-gate goto retry;
36197c478bd9Sstevel@tonic-gate }
36207c478bd9Sstevel@tonic-gate default:
36210f1702c5SYu Xiangning cmn_err(CE_CONT, "sotpi_recvmsg: so %p prim %d mp %p\n",
36220f1702c5SYu Xiangning (void *)so, tpr->type, (void *)mp);
36237c478bd9Sstevel@tonic-gate ASSERT(0);
36247c478bd9Sstevel@tonic-gate freemsg(mp);
36257c478bd9Sstevel@tonic-gate error = EPROTO;
36267c478bd9Sstevel@tonic-gate eprintsoline(so, error);
362717169044Sbrutus goto out;
36287c478bd9Sstevel@tonic-gate }
36297c478bd9Sstevel@tonic-gate /* NOTREACHED */
363017169044Sbrutus out:
36317c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
363217169044Sbrutus out_locked:
36337c478bd9Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */
36347c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
36357c478bd9Sstevel@tonic-gate return (error);
36367c478bd9Sstevel@tonic-gate }
36377c478bd9Sstevel@tonic-gate
36387c478bd9Sstevel@tonic-gate /*
36397c478bd9Sstevel@tonic-gate * Sending data with options on a datagram socket.
36407c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set.
3641cbc6e898SGordon Ross *
3642cbc6e898SGordon Ross * For AF_UNIX the destination address may be already in
3643cbc6e898SGordon Ross * internal form, as indicated by sti->sti_faddr_noxlate
3644cbc6e898SGordon Ross * or the MSG_SENDTO_NOXLATE flag. Otherwise we need to
3645cbc6e898SGordon Ross * translate the destination address to internal form.
3646cbc6e898SGordon Ross *
3647cbc6e898SGordon Ross * The source address is passed as an option. If passing
3648cbc6e898SGordon Ross * file descriptors, those are passed as file pointers in
3649cbc6e898SGordon Ross * another option.
36507c478bd9Sstevel@tonic-gate */
36517c478bd9Sstevel@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)3652ff550d0eSmasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3653ff550d0eSmasputra struct uio *uiop, void *control, t_uscalar_t controllen, int flags)
36547c478bd9Sstevel@tonic-gate {
36557c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
36567c478bd9Sstevel@tonic-gate mblk_t *mp;
36577c478bd9Sstevel@tonic-gate int error;
36587c478bd9Sstevel@tonic-gate void *addr;
36597c478bd9Sstevel@tonic-gate socklen_t addrlen;
36607c478bd9Sstevel@tonic-gate void *src;
36617c478bd9Sstevel@tonic-gate socklen_t srclen;
36627c478bd9Sstevel@tonic-gate ssize_t len;
36637c478bd9Sstevel@tonic-gate int size;
36647c478bd9Sstevel@tonic-gate struct T_opthdr toh;
36657c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf;
36667c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
36677c478bd9Sstevel@tonic-gate void *fds;
36687c478bd9Sstevel@tonic-gate int fdlen;
36690f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
36707c478bd9Sstevel@tonic-gate
36717c478bd9Sstevel@tonic-gate ASSERT(name && namelen);
36727c478bd9Sstevel@tonic-gate ASSERT(control && controllen);
36737c478bd9Sstevel@tonic-gate
36747c478bd9Sstevel@tonic-gate len = uiop->uio_resid;
36750f1702c5SYu Xiangning if (len > (ssize_t)sti->sti_tidu_size) {
36767c478bd9Sstevel@tonic-gate return (EMSGSIZE);
36777c478bd9Sstevel@tonic-gate }
36787c478bd9Sstevel@tonic-gate
3679cbc6e898SGordon Ross if (sti->sti_faddr_noxlate == 0 &&
3680cbc6e898SGordon Ross (flags & MSG_SENDTO_NOXLATE) == 0) {
36817c478bd9Sstevel@tonic-gate /*
36827c478bd9Sstevel@tonic-gate * Length and family checks.
3683cbc6e898SGordon Ross * Don't verify internal form.
36847c478bd9Sstevel@tonic-gate */
36857c478bd9Sstevel@tonic-gate error = so_addr_verify(so, name, namelen);
36867c478bd9Sstevel@tonic-gate if (error) {
36877c478bd9Sstevel@tonic-gate eprintsoline(so, error);
36887c478bd9Sstevel@tonic-gate return (error);
36897c478bd9Sstevel@tonic-gate }
3690cbc6e898SGordon Ross }
3691cbc6e898SGordon Ross
36927c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
36930f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
36947c478bd9Sstevel@tonic-gate /*
36957c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
36967c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
36977c478bd9Sstevel@tonic-gate */
36987c478bd9Sstevel@tonic-gate addr = name;
36997c478bd9Sstevel@tonic-gate addrlen = namelen;
37007c478bd9Sstevel@tonic-gate src = NULL;
37017c478bd9Sstevel@tonic-gate srclen = 0;
3702cbc6e898SGordon Ross } else if (flags & MSG_SENDTO_NOXLATE) {
3703cbc6e898SGordon Ross /*
3704cbc6e898SGordon Ross * Have an internal form dest. address.
3705cbc6e898SGordon Ross * Pass the source address as usual.
3706cbc6e898SGordon Ross */
3707cbc6e898SGordon Ross addr = name;
3708cbc6e898SGordon Ross addrlen = namelen;
3709cbc6e898SGordon Ross src = sti->sti_laddr_sa;
3710cbc6e898SGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
37117c478bd9Sstevel@tonic-gate } else {
37127c478bd9Sstevel@tonic-gate /*
37137c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
37147c478bd9Sstevel@tonic-gate * and translate the remote address.
37157c478bd9Sstevel@tonic-gate *
37160f1702c5SYu Xiangning * Note that this code does not prevent sti_laddr_sa
37177c478bd9Sstevel@tonic-gate * from changing while it is being used. Thus
37187c478bd9Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this
37197c478bd9Sstevel@tonic-gate * send the peer might see a partially new and a
37207c478bd9Sstevel@tonic-gate * partially old "from" address.
37217c478bd9Sstevel@tonic-gate */
37220f1702c5SYu Xiangning src = sti->sti_laddr_sa;
3723cbc6e898SGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
37247c478bd9Sstevel@tonic-gate dprintso(so, 1,
37257c478bd9Sstevel@tonic-gate ("sosend_dgramcmsg UNIX: srclen %d, src %p\n",
37267c478bd9Sstevel@tonic-gate srclen, src));
3727cbc6e898SGordon Ross /*
3728cbc6e898SGordon Ross * The sendmsg caller specified a destination
3729cbc6e898SGordon Ross * address, which we must translate into our
3730cbc6e898SGordon Ross * internal form. addr = &sti->sti_ux_taddr
3731cbc6e898SGordon Ross */
37327c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen,
37337c478bd9Sstevel@tonic-gate (flags & MSG_XPG4_2),
37347c478bd9Sstevel@tonic-gate &addr, &addrlen);
37357c478bd9Sstevel@tonic-gate if (error) {
37367c478bd9Sstevel@tonic-gate eprintsoline(so, error);
37377c478bd9Sstevel@tonic-gate return (error);
37387c478bd9Sstevel@tonic-gate }
37397c478bd9Sstevel@tonic-gate }
37407c478bd9Sstevel@tonic-gate } else {
37417c478bd9Sstevel@tonic-gate addr = name;
37427c478bd9Sstevel@tonic-gate addrlen = namelen;
37437c478bd9Sstevel@tonic-gate src = NULL;
37447c478bd9Sstevel@tonic-gate srclen = 0;
37457c478bd9Sstevel@tonic-gate }
37467c478bd9Sstevel@tonic-gate optlen = so_optlen(control, controllen,
37477c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2));
37487c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
37497c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
37507c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
37517c478bd9Sstevel@tonic-gate if (srclen != 0)
37527c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) +
37537c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen));
37547c478bd9Sstevel@tonic-gate else
37557c478bd9Sstevel@tonic-gate tudr.OPT_length = optlen;
37567c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
37577c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
37587c478bd9Sstevel@tonic-gate
37597c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
37607c478bd9Sstevel@tonic-gate
37617c478bd9Sstevel@tonic-gate /*
37627c478bd9Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set.
37637c478bd9Sstevel@tonic-gate */
37647c478bd9Sstevel@tonic-gate error = so_getfdopt(control, controllen,
37657c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2), &fds, &fdlen);
37667c478bd9Sstevel@tonic-gate if (error)
37677c478bd9Sstevel@tonic-gate return (error);
37687c478bd9Sstevel@tonic-gate if (fdlen != -1) {
37697c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING))
37707c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
37717c478bd9Sstevel@tonic-gate
37727c478bd9Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf);
37737c478bd9Sstevel@tonic-gate if (error)
37747c478bd9Sstevel@tonic-gate return (error);
37757c478bd9Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf);
37767c478bd9Sstevel@tonic-gate } else {
3777de8c4a14SErik Nordmark mp = soallocproto(size, _ALLOC_INTR, CRED());
37787c478bd9Sstevel@tonic-gate if (mp == NULL) {
37797c478bd9Sstevel@tonic-gate /*
37807c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
37817c478bd9Sstevel@tonic-gate * Let send* return EINTR.
37827c478bd9Sstevel@tonic-gate */
37837c478bd9Sstevel@tonic-gate return (EINTR);
37847c478bd9Sstevel@tonic-gate }
3785bd118333Smeem }
37867c478bd9Sstevel@tonic-gate soappendmsg(mp, &tudr, sizeof (tudr));
37877c478bd9Sstevel@tonic-gate soappendmsg(mp, addr, addrlen);
37887c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
37897c478bd9Sstevel@tonic-gate
37907c478bd9Sstevel@tonic-gate if (fdlen != -1) {
37917c478bd9Sstevel@tonic-gate ASSERT(fdbuf != NULL);
37927c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
37937c478bd9Sstevel@tonic-gate toh.name = SO_FILEP;
37947c478bd9Sstevel@tonic-gate toh.len = fdbuf->fd_size +
37957c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_opthdr);
37967c478bd9Sstevel@tonic-gate toh.status = 0;
37977c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
37987c478bd9Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size);
37997c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
38007c478bd9Sstevel@tonic-gate }
38017c478bd9Sstevel@tonic-gate if (srclen != 0) {
38027c478bd9Sstevel@tonic-gate /*
38037c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
38047c478bd9Sstevel@tonic-gate * address option.
38057c478bd9Sstevel@tonic-gate */
38067c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
38077c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
38087c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
38097c478bd9Sstevel@tonic-gate toh.status = 0;
38107c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
38117c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
38127c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
38137c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
38147c478bd9Sstevel@tonic-gate }
38157c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
38167c478bd9Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
38177c478bd9Sstevel@tonic-gate /* At most 3 bytes left in the message */
38187c478bd9Sstevel@tonic-gate ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
38197c478bd9Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size);
38207c478bd9Sstevel@tonic-gate
38217c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3822005d3febSMarek Pospisil if (AU_AUDITING())
38237c478bd9Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
38247c478bd9Sstevel@tonic-gate
38257c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
38267c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
38277c478bd9Sstevel@tonic-gate if (error) {
38287c478bd9Sstevel@tonic-gate eprintsoline(so, error);
38297c478bd9Sstevel@tonic-gate }
38307c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
38317c478bd9Sstevel@tonic-gate return (error);
38327c478bd9Sstevel@tonic-gate }
38337c478bd9Sstevel@tonic-gate
38347c478bd9Sstevel@tonic-gate /*
38357c478bd9Sstevel@tonic-gate * Sending data with options on a connected stream socket.
38367c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set.
38377c478bd9Sstevel@tonic-gate */
38387c478bd9Sstevel@tonic-gate static int
sosend_svccmsg(struct sonode * so,struct uio * uiop,int more,void * control,t_uscalar_t controllen,int flags)38390f1702c5SYu Xiangning sosend_svccmsg(struct sonode *so, struct uio *uiop, int more, void *control,
38400f1702c5SYu Xiangning t_uscalar_t controllen, int flags)
38417c478bd9Sstevel@tonic-gate {
38427c478bd9Sstevel@tonic-gate struct T_optdata_req tdr;
38437c478bd9Sstevel@tonic-gate mblk_t *mp;
38447c478bd9Sstevel@tonic-gate int error;
38457c478bd9Sstevel@tonic-gate ssize_t iosize;
38467c478bd9Sstevel@tonic-gate int size;
38477c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf;
38487c478bd9Sstevel@tonic-gate t_uscalar_t optlen;
38497c478bd9Sstevel@tonic-gate void *fds;
38507c478bd9Sstevel@tonic-gate int fdlen;
38517c478bd9Sstevel@tonic-gate struct T_opthdr toh;
38520f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
38537c478bd9Sstevel@tonic-gate
38547c478bd9Sstevel@tonic-gate dprintso(so, 1,
38557c478bd9Sstevel@tonic-gate ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
38567c478bd9Sstevel@tonic-gate
38577c478bd9Sstevel@tonic-gate /*
38587c478bd9Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are
38597c478bd9Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it
38607c478bd9Sstevel@tonic-gate * thus it is not possible to ASSERT on the state.
38617c478bd9Sstevel@tonic-gate */
38627c478bd9Sstevel@tonic-gate
38637c478bd9Sstevel@tonic-gate /* Options on connection-oriented only when SM_OPTDATA set. */
38647c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_OPTDATA))
38657c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
38667c478bd9Sstevel@tonic-gate
38677c478bd9Sstevel@tonic-gate do {
38687c478bd9Sstevel@tonic-gate /*
38697c478bd9Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this
38707c478bd9Sstevel@tonic-gate * message or if the caller passed in "more".
38717c478bd9Sstevel@tonic-gate * Error for transports with zero tidu_size.
38727c478bd9Sstevel@tonic-gate */
38737c478bd9Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ;
38740f1702c5SYu Xiangning iosize = sti->sti_tidu_size;
38757c478bd9Sstevel@tonic-gate if (iosize <= 0)
38767c478bd9Sstevel@tonic-gate return (EMSGSIZE);
38777c478bd9Sstevel@tonic-gate if (uiop->uio_resid > iosize) {
38787c478bd9Sstevel@tonic-gate tdr.DATA_flag = 1;
38797c478bd9Sstevel@tonic-gate } else {
38807c478bd9Sstevel@tonic-gate if (more)
38817c478bd9Sstevel@tonic-gate tdr.DATA_flag = 1;
38827c478bd9Sstevel@tonic-gate else
38837c478bd9Sstevel@tonic-gate tdr.DATA_flag = 0;
38847c478bd9Sstevel@tonic-gate iosize = uiop->uio_resid;
38857c478bd9Sstevel@tonic-gate }
38867c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
38877c478bd9Sstevel@tonic-gate tdr.DATA_flag, iosize));
38887c478bd9Sstevel@tonic-gate
38897c478bd9Sstevel@tonic-gate optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
38907c478bd9Sstevel@tonic-gate tdr.OPT_length = optlen;
38917c478bd9Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
38927c478bd9Sstevel@tonic-gate
38937c478bd9Sstevel@tonic-gate size = (int)sizeof (tdr) + optlen;
38947c478bd9Sstevel@tonic-gate /*
38957c478bd9Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set.
38967c478bd9Sstevel@tonic-gate */
38977c478bd9Sstevel@tonic-gate error = so_getfdopt(control, controllen,
38987c478bd9Sstevel@tonic-gate !(flags & MSG_XPG4_2), &fds, &fdlen);
38997c478bd9Sstevel@tonic-gate if (error)
39007c478bd9Sstevel@tonic-gate return (error);
39017c478bd9Sstevel@tonic-gate if (fdlen != -1) {
39027c478bd9Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING))
39037c478bd9Sstevel@tonic-gate return (EOPNOTSUPP);
39047c478bd9Sstevel@tonic-gate
39057c478bd9Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf);
39067c478bd9Sstevel@tonic-gate if (error)
39077c478bd9Sstevel@tonic-gate return (error);
39087c478bd9Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf);
39097c478bd9Sstevel@tonic-gate } else {
3910de8c4a14SErik Nordmark mp = soallocproto(size, _ALLOC_INTR, CRED());
39117c478bd9Sstevel@tonic-gate if (mp == NULL) {
39127c478bd9Sstevel@tonic-gate /*
39137c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
39147c478bd9Sstevel@tonic-gate * Let send* return EINTR.
39157c478bd9Sstevel@tonic-gate */
39160f1702c5SYu Xiangning return (EINTR);
3917bd118333Smeem }
39187c478bd9Sstevel@tonic-gate }
39197c478bd9Sstevel@tonic-gate soappendmsg(mp, &tdr, sizeof (tdr));
39207c478bd9Sstevel@tonic-gate
39217c478bd9Sstevel@tonic-gate if (fdlen != -1) {
39227c478bd9Sstevel@tonic-gate ASSERT(fdbuf != NULL);
39237c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
39247c478bd9Sstevel@tonic-gate toh.name = SO_FILEP;
39257c478bd9Sstevel@tonic-gate toh.len = fdbuf->fd_size +
39267c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_opthdr);
39277c478bd9Sstevel@tonic-gate toh.status = 0;
39287c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
39297c478bd9Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size);
39307c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
39317c478bd9Sstevel@tonic-gate }
39327c478bd9Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
39337c478bd9Sstevel@tonic-gate /* At most 3 bytes left in the message */
39347c478bd9Sstevel@tonic-gate ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
39357c478bd9Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size);
39367c478bd9Sstevel@tonic-gate
39377c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
39387c478bd9Sstevel@tonic-gate
39397c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
39407c478bd9Sstevel@tonic-gate 0, MSG_BAND, 0);
39417c478bd9Sstevel@tonic-gate if (error) {
39427c478bd9Sstevel@tonic-gate eprintsoline(so, error);
39437c478bd9Sstevel@tonic-gate return (error);
39447c478bd9Sstevel@tonic-gate }
39457c478bd9Sstevel@tonic-gate control = NULL;
39467c478bd9Sstevel@tonic-gate if (uiop->uio_resid > 0) {
39477c478bd9Sstevel@tonic-gate /*
39487c478bd9Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though
39497c478bd9Sstevel@tonic-gate * some data have been written. This is consistent
39507c478bd9Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics.
39517c478bd9Sstevel@tonic-gate */
39527c478bd9Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) {
39537c478bd9Sstevel@tonic-gate eprintsoline(so, error);
39547c478bd9Sstevel@tonic-gate return (EPIPE);
39557c478bd9Sstevel@tonic-gate }
39567c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
39577c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
39580f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
39597c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
39607c478bd9Sstevel@tonic-gate if (error != 0) {
39617c478bd9Sstevel@tonic-gate eprintsoline(so, error);
39627c478bd9Sstevel@tonic-gate return (error);
39637c478bd9Sstevel@tonic-gate }
39647c478bd9Sstevel@tonic-gate }
39657c478bd9Sstevel@tonic-gate }
39667c478bd9Sstevel@tonic-gate } while (uiop->uio_resid > 0);
39677c478bd9Sstevel@tonic-gate return (0);
39687c478bd9Sstevel@tonic-gate }
39697c478bd9Sstevel@tonic-gate
39707c478bd9Sstevel@tonic-gate /*
39717c478bd9Sstevel@tonic-gate * Sending data on a datagram socket.
39727c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set.
39737c478bd9Sstevel@tonic-gate *
3974cbc6e898SGordon Ross * For AF_UNIX the destination address may be already in
3975cbc6e898SGordon Ross * internal form, as indicated by sti->sti_faddr_noxlate
3976cbc6e898SGordon Ross * or the MSG_SENDTO_NOXLATE flag. Otherwise we need to
3977cbc6e898SGordon Ross * translate the destination address to internal form.
3978cbc6e898SGordon Ross *
3979cbc6e898SGordon Ross * The source address is passed as an option.
39807c478bd9Sstevel@tonic-gate */
39817c478bd9Sstevel@tonic-gate int
sosend_dgram(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,int flags)3982ff550d0eSmasputra sosend_dgram(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3983ff550d0eSmasputra struct uio *uiop, int flags)
39847c478bd9Sstevel@tonic-gate {
39857c478bd9Sstevel@tonic-gate struct T_unitdata_req tudr;
39867c478bd9Sstevel@tonic-gate mblk_t *mp;
39877c478bd9Sstevel@tonic-gate int error;
39887c478bd9Sstevel@tonic-gate void *addr;
39897c478bd9Sstevel@tonic-gate socklen_t addrlen;
39907c478bd9Sstevel@tonic-gate void *src;
39917c478bd9Sstevel@tonic-gate socklen_t srclen;
39927c478bd9Sstevel@tonic-gate ssize_t len;
39930f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
39947c478bd9Sstevel@tonic-gate
3995ff550d0eSmasputra ASSERT(name != NULL && namelen != 0);
39967c478bd9Sstevel@tonic-gate
39977c478bd9Sstevel@tonic-gate len = uiop->uio_resid;
39980f1702c5SYu Xiangning if (len > sti->sti_tidu_size) {
39997c478bd9Sstevel@tonic-gate error = EMSGSIZE;
40007c478bd9Sstevel@tonic-gate goto done;
40017c478bd9Sstevel@tonic-gate }
40027c478bd9Sstevel@tonic-gate
4003cbc6e898SGordon Ross if (sti->sti_faddr_noxlate == 0 &&
4004cbc6e898SGordon Ross (flags & MSG_SENDTO_NOXLATE) == 0) {
4005cbc6e898SGordon Ross /*
4006cbc6e898SGordon Ross * Length and family checks.
4007cbc6e898SGordon Ross * Don't verify internal form.
4008cbc6e898SGordon Ross */
40097c478bd9Sstevel@tonic-gate error = so_addr_verify(so, name, namelen);
4010ff550d0eSmasputra if (error != 0)
40117c478bd9Sstevel@tonic-gate goto done;
4012cbc6e898SGordon Ross }
4013ff550d0eSmasputra
4014cbc6e898SGordon Ross if (sti->sti_direct) /* Never on AF_UNIX */
4015ff550d0eSmasputra return (sodgram_direct(so, name, namelen, uiop, flags));
4016ff550d0eSmasputra
40177c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
40180f1702c5SYu Xiangning if (sti->sti_faddr_noxlate) {
40197c478bd9Sstevel@tonic-gate /*
40207c478bd9Sstevel@tonic-gate * Already have a transport internal address. Do not
40217c478bd9Sstevel@tonic-gate * pass any (transport internal) source address.
40227c478bd9Sstevel@tonic-gate */
40237c478bd9Sstevel@tonic-gate addr = name;
40247c478bd9Sstevel@tonic-gate addrlen = namelen;
40257c478bd9Sstevel@tonic-gate src = NULL;
40267c478bd9Sstevel@tonic-gate srclen = 0;
4027cbc6e898SGordon Ross } else if (flags & MSG_SENDTO_NOXLATE) {
4028cbc6e898SGordon Ross /*
4029cbc6e898SGordon Ross * Have an internal form dest. address.
4030cbc6e898SGordon Ross * Pass the source address as usual.
4031cbc6e898SGordon Ross */
4032cbc6e898SGordon Ross addr = name;
4033cbc6e898SGordon Ross addrlen = namelen;
4034cbc6e898SGordon Ross src = sti->sti_laddr_sa;
4035cbc6e898SGordon Ross srclen = (socklen_t)sti->sti_laddr_len;
40367c478bd9Sstevel@tonic-gate } else {
40377c478bd9Sstevel@tonic-gate /*
40387c478bd9Sstevel@tonic-gate * Pass the sockaddr_un source address as an option
40397c478bd9Sstevel@tonic-gate * and translate the remote address.
40407c478bd9Sstevel@tonic-gate *
40410f1702c5SYu Xiangning * Note that this code does not prevent sti_laddr_sa
40427c478bd9Sstevel@tonic-gate * from changing while it is being used. Thus
40437c478bd9Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this
40447c478bd9Sstevel@tonic-gate * send the peer might see a partially new and a
40457c478bd9Sstevel@tonic-gate * partially old "from" address.
40467c478bd9Sstevel@tonic-gate */
40470f1702c5SYu Xiangning src = sti->sti_laddr_sa;
40480f1702c5SYu Xiangning srclen = (socklen_t)sti->sti_laddr_len;
40497c478bd9Sstevel@tonic-gate dprintso(so, 1,
40507c478bd9Sstevel@tonic-gate ("sosend_dgram UNIX: srclen %d, src %p\n",
40517c478bd9Sstevel@tonic-gate srclen, src));
4052cbc6e898SGordon Ross /*
4053cbc6e898SGordon Ross * The sendmsg caller specified a destination
4054cbc6e898SGordon Ross * address, which we must translate into our
4055cbc6e898SGordon Ross * internal form. addr = &sti->sti_ux_taddr
4056cbc6e898SGordon Ross */
40577c478bd9Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen,
40587c478bd9Sstevel@tonic-gate (flags & MSG_XPG4_2),
40597c478bd9Sstevel@tonic-gate &addr, &addrlen);
40607c478bd9Sstevel@tonic-gate if (error) {
40617c478bd9Sstevel@tonic-gate eprintsoline(so, error);
40627c478bd9Sstevel@tonic-gate goto done;
40637c478bd9Sstevel@tonic-gate }
40647c478bd9Sstevel@tonic-gate }
40657c478bd9Sstevel@tonic-gate } else {
40667c478bd9Sstevel@tonic-gate addr = name;
40677c478bd9Sstevel@tonic-gate addrlen = namelen;
40687c478bd9Sstevel@tonic-gate src = NULL;
40697c478bd9Sstevel@tonic-gate srclen = 0;
40707c478bd9Sstevel@tonic-gate }
40717c478bd9Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ;
40727c478bd9Sstevel@tonic-gate tudr.DEST_length = addrlen;
40737c478bd9Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
40747c478bd9Sstevel@tonic-gate if (srclen == 0) {
40757c478bd9Sstevel@tonic-gate tudr.OPT_length = 0;
40767c478bd9Sstevel@tonic-gate tudr.OPT_offset = 0;
40777c478bd9Sstevel@tonic-gate
40787c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
4079de8c4a14SErik Nordmark addr, addrlen, 0, _ALLOC_INTR, CRED());
40807c478bd9Sstevel@tonic-gate if (mp == NULL) {
40817c478bd9Sstevel@tonic-gate /*
40827c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
40837c478bd9Sstevel@tonic-gate * Let send* return EINTR.
40847c478bd9Sstevel@tonic-gate */
40857c478bd9Sstevel@tonic-gate error = EINTR;
40867c478bd9Sstevel@tonic-gate goto done;
40877c478bd9Sstevel@tonic-gate }
40887c478bd9Sstevel@tonic-gate } else {
40897c478bd9Sstevel@tonic-gate /*
40907c478bd9Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source
40917c478bd9Sstevel@tonic-gate * address option.
40927c478bd9Sstevel@tonic-gate */
40937c478bd9Sstevel@tonic-gate struct T_opthdr toh;
40947c478bd9Sstevel@tonic-gate ssize_t size;
40957c478bd9Sstevel@tonic-gate
40967c478bd9Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
40977c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen));
40987c478bd9Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
40997c478bd9Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen));
41007c478bd9Sstevel@tonic-gate
41017c478bd9Sstevel@tonic-gate toh.level = SOL_SOCKET;
41027c478bd9Sstevel@tonic-gate toh.name = SO_SRCADDR;
41037c478bd9Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
41047c478bd9Sstevel@tonic-gate toh.status = 0;
41057c478bd9Sstevel@tonic-gate
41067c478bd9Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length;
41077c478bd9Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr),
4108de8c4a14SErik Nordmark addr, addrlen, size, _ALLOC_INTR, CRED());
41097c478bd9Sstevel@tonic-gate if (mp == NULL) {
41107c478bd9Sstevel@tonic-gate /*
41117c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
41127c478bd9Sstevel@tonic-gate * Let send* return EINTR.
41137c478bd9Sstevel@tonic-gate */
41147c478bd9Sstevel@tonic-gate error = EINTR;
41157c478bd9Sstevel@tonic-gate goto done;
41167c478bd9Sstevel@tonic-gate }
41177c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
41187c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh));
41197c478bd9Sstevel@tonic-gate soappendmsg(mp, src, srclen);
41207c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
41217c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
41227c478bd9Sstevel@tonic-gate }
41237c478bd9Sstevel@tonic-gate
4124005d3febSMarek Pospisil if (AU_AUDITING())
41257c478bd9Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
41267c478bd9Sstevel@tonic-gate
41277c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
41287c478bd9Sstevel@tonic-gate done:
41297c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
41307c478bd9Sstevel@tonic-gate if (error) {
41317c478bd9Sstevel@tonic-gate eprintsoline(so, error);
41327c478bd9Sstevel@tonic-gate }
41337c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
41347c478bd9Sstevel@tonic-gate return (error);
41357c478bd9Sstevel@tonic-gate }
41367c478bd9Sstevel@tonic-gate
41377c478bd9Sstevel@tonic-gate /*
41387c478bd9Sstevel@tonic-gate * Sending data on a connected stream socket.
41397c478bd9Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set.
41407c478bd9Sstevel@tonic-gate */
41417c478bd9Sstevel@tonic-gate int
sosend_svc(struct sonode * so,struct uio * uiop,t_scalar_t prim,int more,int sflag)41420f1702c5SYu Xiangning sosend_svc(struct sonode *so, struct uio *uiop, t_scalar_t prim, int more,
41437c478bd9Sstevel@tonic-gate int sflag)
41447c478bd9Sstevel@tonic-gate {
41457c478bd9Sstevel@tonic-gate struct T_data_req tdr;
41467c478bd9Sstevel@tonic-gate mblk_t *mp;
41477c478bd9Sstevel@tonic-gate int error;
41487c478bd9Sstevel@tonic-gate ssize_t iosize;
41490f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
41507c478bd9Sstevel@tonic-gate
41517c478bd9Sstevel@tonic-gate dprintso(so, 1,
41527c478bd9Sstevel@tonic-gate ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
4153903a11ebSrh87107 (void *)so, uiop->uio_resid, prim, sflag));
41547c478bd9Sstevel@tonic-gate
41557c478bd9Sstevel@tonic-gate /*
41567c478bd9Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are
41577c478bd9Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it
41587c478bd9Sstevel@tonic-gate * thus it is not possible to ASSERT on the state.
41597c478bd9Sstevel@tonic-gate */
41607c478bd9Sstevel@tonic-gate
41617c478bd9Sstevel@tonic-gate do {
41627c478bd9Sstevel@tonic-gate /*
41637c478bd9Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this
41647c478bd9Sstevel@tonic-gate * message or if the caller passed in "more".
41657c478bd9Sstevel@tonic-gate * Error for transports with zero tidu_size.
41667c478bd9Sstevel@tonic-gate */
41677c478bd9Sstevel@tonic-gate tdr.PRIM_type = prim;
41680f1702c5SYu Xiangning iosize = sti->sti_tidu_size;
41697c478bd9Sstevel@tonic-gate if (iosize <= 0)
41707c478bd9Sstevel@tonic-gate return (EMSGSIZE);
41717c478bd9Sstevel@tonic-gate if (uiop->uio_resid > iosize) {
41727c478bd9Sstevel@tonic-gate tdr.MORE_flag = 1;
41737c478bd9Sstevel@tonic-gate } else {
41747c478bd9Sstevel@tonic-gate if (more)
41757c478bd9Sstevel@tonic-gate tdr.MORE_flag = 1;
41767c478bd9Sstevel@tonic-gate else
41777c478bd9Sstevel@tonic-gate tdr.MORE_flag = 0;
41787c478bd9Sstevel@tonic-gate iosize = uiop->uio_resid;
41797c478bd9Sstevel@tonic-gate }
41807c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
41817c478bd9Sstevel@tonic-gate prim, tdr.MORE_flag, iosize));
4182de8c4a14SErik Nordmark mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR, CRED());
41837c478bd9Sstevel@tonic-gate if (mp == NULL) {
41847c478bd9Sstevel@tonic-gate /*
41857c478bd9Sstevel@tonic-gate * Caught a signal waiting for memory.
41867c478bd9Sstevel@tonic-gate * Let send* return EINTR.
41877c478bd9Sstevel@tonic-gate */
41887c478bd9Sstevel@tonic-gate return (EINTR);
41897c478bd9Sstevel@tonic-gate }
41907c478bd9Sstevel@tonic-gate
41917c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
41927c478bd9Sstevel@tonic-gate 0, sflag | MSG_BAND, 0);
41937c478bd9Sstevel@tonic-gate if (error) {
41947c478bd9Sstevel@tonic-gate eprintsoline(so, error);
41957c478bd9Sstevel@tonic-gate return (error);
41967c478bd9Sstevel@tonic-gate }
41977c478bd9Sstevel@tonic-gate if (uiop->uio_resid > 0) {
41987c478bd9Sstevel@tonic-gate /*
41997c478bd9Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though
42007c478bd9Sstevel@tonic-gate * some data have been written. This is consistent
42017c478bd9Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics.
42027c478bd9Sstevel@tonic-gate */
42037c478bd9Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) {
42047c478bd9Sstevel@tonic-gate eprintsoline(so, error);
42057c478bd9Sstevel@tonic-gate return (EPIPE);
42067c478bd9Sstevel@tonic-gate }
42077c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
42087c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
42090f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
42107c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42117c478bd9Sstevel@tonic-gate if (error != 0) {
42127c478bd9Sstevel@tonic-gate eprintsoline(so, error);
42137c478bd9Sstevel@tonic-gate return (error);
42147c478bd9Sstevel@tonic-gate }
42157c478bd9Sstevel@tonic-gate }
42167c478bd9Sstevel@tonic-gate }
42177c478bd9Sstevel@tonic-gate } while (uiop->uio_resid > 0);
42187c478bd9Sstevel@tonic-gate return (0);
42197c478bd9Sstevel@tonic-gate }
42207c478bd9Sstevel@tonic-gate
42217c478bd9Sstevel@tonic-gate /*
42227c478bd9Sstevel@tonic-gate * Check the state for errors and call the appropriate send function.
42237c478bd9Sstevel@tonic-gate *
42247c478bd9Sstevel@tonic-gate * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
42257c478bd9Sstevel@tonic-gate * this function issues a setsockopt to toggle SO_DONTROUTE before and
42267c478bd9Sstevel@tonic-gate * after sending the message.
4227cbc6e898SGordon Ross *
4228cbc6e898SGordon Ross * The caller may optionally specify a destination address, for either
4229cbc6e898SGordon Ross * stream or datagram sockets. This table summarizes the cases:
4230cbc6e898SGordon Ross *
4231cbc6e898SGordon Ross * Socket type Dest. given Connected Result
4232cbc6e898SGordon Ross * ----------- ----------- --------- --------------
4233cbc6e898SGordon Ross * Stream * Yes send to conn. addr.
4234cbc6e898SGordon Ross * Stream * No error ENOTCONN
4235cbc6e898SGordon Ross * Dgram yes * send to given addr.
4236cbc6e898SGordon Ross * Dgram no yes send to conn. addr.
4237cbc6e898SGordon Ross * Dgram no no error EDESTADDRREQ
4238cbc6e898SGordon Ross *
4239cbc6e898SGordon Ross * There are subtleties around the destination address when using
4240cbc6e898SGordon Ross * AF_UNIX datagram sockets. When the sendmsg call specifies the
4241cbc6e898SGordon Ross * destination address, it's in (struct sockaddr_un) form and we
4242cbc6e898SGordon Ross * need to translate it to our internal form (struct so_ux_addr).
4243cbc6e898SGordon Ross *
4244cbc6e898SGordon Ross * When the sendmsg call does not specify a destination address
4245cbc6e898SGordon Ross * we're using the peer address saved during sotpi_connect, and
4246cbc6e898SGordon Ross * that address is already in internal form. In this case, the
4247cbc6e898SGordon Ross * (internal only) flag MSG_SENDTO_NOXLATE is set in the flags
4248cbc6e898SGordon Ross * passed to sosend_dgram or sosend_dgramcmsg to indicate that
4249cbc6e898SGordon Ross * those functions should skip translation to internal form.
4250cbc6e898SGordon Ross * Avoiding that translation is not only more efficient, but it's
4251cbc6e898SGordon Ross * also necessary when a process does a connect on an AF_UNIX
4252cbc6e898SGordon Ross * datagram socket and then drops privileges. After the process
4253cbc6e898SGordon Ross * has dropped privileges, it may no longer be able to lookup the
4254cbc6e898SGordon Ross * the external name in the filesystem, but it should still be
4255cbc6e898SGordon Ross * able to send messages on the connected socket by leaving the
4256cbc6e898SGordon Ross * destination name unspecified.
4257cbc6e898SGordon Ross *
4258cbc6e898SGordon Ross * Yet more subtleties arise with sockets connected by socketpair(),
4259cbc6e898SGordon Ross * which puts internal form addresses in the fields where normally
4260cbc6e898SGordon Ross * the external form is found, and sets sti_faddr_noxlate=1, which
4261cbc6e898SGordon Ross * (like flag MSG_SENDTO_NOXLATE) causes the sosend_dgram functions
4262cbc6e898SGordon Ross * to skip translation of destination addresses to internal form.
4263cbc6e898SGordon Ross * However, beware that the flag sti_faddr_noxlate=1 also triggers
4264cbc6e898SGordon Ross * different behaviour almost everywhere AF_UNIX addresses appear.
42657c478bd9Sstevel@tonic-gate */
42667c478bd9Sstevel@tonic-gate static int
sotpi_sendmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)42670f1702c5SYu Xiangning sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
42680f1702c5SYu Xiangning struct cred *cr)
42697c478bd9Sstevel@tonic-gate {
42707c478bd9Sstevel@tonic-gate int so_state;
42717c478bd9Sstevel@tonic-gate int so_mode;
42727c478bd9Sstevel@tonic-gate int error;
42737c478bd9Sstevel@tonic-gate struct sockaddr *name;
42747c478bd9Sstevel@tonic-gate t_uscalar_t namelen;
42757c478bd9Sstevel@tonic-gate int dontroute;
42767c478bd9Sstevel@tonic-gate int flags;
42770f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
42787c478bd9Sstevel@tonic-gate
42797c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
4280903a11ebSrh87107 (void *)so, (void *)msg, msg->msg_flags,
42817c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode), so->so_error));
42827c478bd9Sstevel@tonic-gate
42830f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
42840f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
42850f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
42860f1702c5SYu Xiangning return (strwrite(SOTOV(so), uiop, cr));
42870f1702c5SYu Xiangning }
42880f1702c5SYu Xiangning
42897c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
42907c478bd9Sstevel@tonic-gate so_state = so->so_state;
42917c478bd9Sstevel@tonic-gate
42927c478bd9Sstevel@tonic-gate if (so_state & SS_CANTSENDMORE) {
42937c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
42947c478bd9Sstevel@tonic-gate return (EPIPE);
42957c478bd9Sstevel@tonic-gate }
42967c478bd9Sstevel@tonic-gate
42977c478bd9Sstevel@tonic-gate if (so->so_error != 0) {
42980f1702c5SYu Xiangning error = sogeterr(so, B_TRUE);
42997c478bd9Sstevel@tonic-gate if (error != 0) {
43007c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
43017c478bd9Sstevel@tonic-gate return (error);
43027c478bd9Sstevel@tonic-gate }
43037c478bd9Sstevel@tonic-gate }
43047c478bd9Sstevel@tonic-gate
43057c478bd9Sstevel@tonic-gate name = (struct sockaddr *)msg->msg_name;
43067c478bd9Sstevel@tonic-gate namelen = msg->msg_namelen;
4307cbc6e898SGordon Ross flags = msg->msg_flags;
4308cbc6e898SGordon Ross
4309cbc6e898SGordon Ross /*
4310cbc6e898SGordon Ross * Historically, this function does not validate the flags
4311cbc6e898SGordon Ross * passed in, and any errant bits are ignored. However,
4312cbc6e898SGordon Ross * we would not want any such errant flag bits accidently
4313cbc6e898SGordon Ross * being treated as one of the internal-only flags, so
4314cbc6e898SGordon Ross * clear the internal-only flag bits.
4315cbc6e898SGordon Ross */
4316cbc6e898SGordon Ross flags &= ~MSG_SENDTO_NOXLATE;
43177c478bd9Sstevel@tonic-gate
43187c478bd9Sstevel@tonic-gate so_mode = so->so_mode;
43197c478bd9Sstevel@tonic-gate
43207c478bd9Sstevel@tonic-gate if (name == NULL) {
43217c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED)) {
43227c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
43237c478bd9Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED)
43247c478bd9Sstevel@tonic-gate return (ENOTCONN);
43257c478bd9Sstevel@tonic-gate else
43267c478bd9Sstevel@tonic-gate return (EDESTADDRREQ);
43277c478bd9Sstevel@tonic-gate }
4328cbc6e898SGordon Ross /*
4329cbc6e898SGordon Ross * This is a connected socket.
4330cbc6e898SGordon Ross */
43317c478bd9Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) {
4332cbc6e898SGordon Ross /*
4333cbc6e898SGordon Ross * This is a connected STREAM socket,
4334cbc6e898SGordon Ross * destination not specified.
4335cbc6e898SGordon Ross */
43367c478bd9Sstevel@tonic-gate name = NULL;
43377c478bd9Sstevel@tonic-gate namelen = 0;
43387c478bd9Sstevel@tonic-gate } else {
43397c478bd9Sstevel@tonic-gate /*
4340cbc6e898SGordon Ross * Datagram send on connected socket with
4341cbc6e898SGordon Ross * the destination name not specified.
4342cbc6e898SGordon Ross * Use the peer address from connect.
43437c478bd9Sstevel@tonic-gate */
4344cbc6e898SGordon Ross if (so->so_family == AF_UNIX) {
4345cbc6e898SGordon Ross /*
4346cbc6e898SGordon Ross * Use the (internal form) address saved
4347cbc6e898SGordon Ross * in sotpi_connect. See above.
4348cbc6e898SGordon Ross */
4349cbc6e898SGordon Ross name = (void *)&sti->sti_ux_faddr;
4350cbc6e898SGordon Ross namelen = sizeof (sti->sti_ux_faddr);
4351cbc6e898SGordon Ross flags |= MSG_SENDTO_NOXLATE;
4352cbc6e898SGordon Ross } else {
43530f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa);
43540f1702c5SYu Xiangning name = sti->sti_faddr_sa;
43550f1702c5SYu Xiangning namelen = (t_uscalar_t)sti->sti_faddr_len;
43567c478bd9Sstevel@tonic-gate }
4357cbc6e898SGordon Ross }
43587c478bd9Sstevel@tonic-gate } else {
4359cbc6e898SGordon Ross /*
4360cbc6e898SGordon Ross * Sendmsg specifies a destination name
4361cbc6e898SGordon Ross */
43627c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED) &&
43637c478bd9Sstevel@tonic-gate (so_mode & SM_CONNREQUIRED)) {
4364cbc6e898SGordon Ross /* i.e. TCP not connected */
43657c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
43667c478bd9Sstevel@tonic-gate return (ENOTCONN);
43677c478bd9Sstevel@tonic-gate }
43687c478bd9Sstevel@tonic-gate /*
43697c478bd9Sstevel@tonic-gate * Ignore the address on connection-oriented sockets.
43707c478bd9Sstevel@tonic-gate * Just like BSD this code does not generate an error for
43717c478bd9Sstevel@tonic-gate * TCP (a CONNREQUIRED socket) when sending to an address
43727c478bd9Sstevel@tonic-gate * passed in with sendto/sendmsg. Instead the data is
43737c478bd9Sstevel@tonic-gate * delivered on the connection as if no address had been
43747c478bd9Sstevel@tonic-gate * supplied.
43757c478bd9Sstevel@tonic-gate */
43767c478bd9Sstevel@tonic-gate if ((so_state & SS_ISCONNECTED) &&
43777c478bd9Sstevel@tonic-gate !(so_mode & SM_CONNREQUIRED)) {
43787c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
43797c478bd9Sstevel@tonic-gate return (EISCONN);
43807c478bd9Sstevel@tonic-gate }
43817c478bd9Sstevel@tonic-gate if (!(so_state & SS_ISBOUND)) {
43827c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
43837c478bd9Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0,
43840f1702c5SYu Xiangning _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
43857c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
43867c478bd9Sstevel@tonic-gate if (error) {
43877c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
43887c478bd9Sstevel@tonic-gate eprintsoline(so, error);
43897c478bd9Sstevel@tonic-gate return (error);
43907c478bd9Sstevel@tonic-gate }
43917c478bd9Sstevel@tonic-gate }
43927c478bd9Sstevel@tonic-gate /*
43937c478bd9Sstevel@tonic-gate * Handle delayed datagram errors. These are only queued
43947c478bd9Sstevel@tonic-gate * when the application sets SO_DGRAM_ERRIND.
43957c478bd9Sstevel@tonic-gate * Return the error if we are sending to the address
43967c478bd9Sstevel@tonic-gate * that was returned in the last T_UDERROR_IND.
43977c478bd9Sstevel@tonic-gate * If sending to some other address discard the delayed
43987c478bd9Sstevel@tonic-gate * error indication.
43997c478bd9Sstevel@tonic-gate */
44000f1702c5SYu Xiangning if (sti->sti_delayed_error) {
44017c478bd9Sstevel@tonic-gate struct T_uderror_ind *tudi;
44027c478bd9Sstevel@tonic-gate void *addr;
44037c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
44047c478bd9Sstevel@tonic-gate boolean_t match = B_FALSE;
44057c478bd9Sstevel@tonic-gate
44060f1702c5SYu Xiangning ASSERT(sti->sti_eaddr_mp);
44070f1702c5SYu Xiangning error = sti->sti_delayed_error;
44080f1702c5SYu Xiangning sti->sti_delayed_error = 0;
44090f1702c5SYu Xiangning tudi =
44100f1702c5SYu Xiangning (struct T_uderror_ind *)sti->sti_eaddr_mp->b_rptr;
44117c478bd9Sstevel@tonic-gate addrlen = tudi->DEST_length;
44120f1702c5SYu Xiangning addr = sogetoff(sti->sti_eaddr_mp,
44130f1702c5SYu Xiangning tudi->DEST_offset, addrlen, 1);
44147c478bd9Sstevel@tonic-gate ASSERT(addr); /* Checked by strsock_proto */
44157c478bd9Sstevel@tonic-gate switch (so->so_family) {
44167c478bd9Sstevel@tonic-gate case AF_INET: {
44177c478bd9Sstevel@tonic-gate /* Compare just IP address and port */
44187c478bd9Sstevel@tonic-gate sin_t *sin1 = (sin_t *)name;
44197c478bd9Sstevel@tonic-gate sin_t *sin2 = (sin_t *)addr;
44207c478bd9Sstevel@tonic-gate
44217c478bd9Sstevel@tonic-gate if (addrlen == sizeof (sin_t) &&
44227c478bd9Sstevel@tonic-gate namelen == addrlen &&
44237c478bd9Sstevel@tonic-gate sin1->sin_port == sin2->sin_port &&
44247c478bd9Sstevel@tonic-gate sin1->sin_addr.s_addr ==
44257c478bd9Sstevel@tonic-gate sin2->sin_addr.s_addr)
44267c478bd9Sstevel@tonic-gate match = B_TRUE;
44277c478bd9Sstevel@tonic-gate break;
44287c478bd9Sstevel@tonic-gate }
44297c478bd9Sstevel@tonic-gate case AF_INET6: {
44307c478bd9Sstevel@tonic-gate /* Compare just IP address and port. Not flow */
44317c478bd9Sstevel@tonic-gate sin6_t *sin1 = (sin6_t *)name;
44327c478bd9Sstevel@tonic-gate sin6_t *sin2 = (sin6_t *)addr;
44337c478bd9Sstevel@tonic-gate
44347c478bd9Sstevel@tonic-gate if (addrlen == sizeof (sin6_t) &&
44357c478bd9Sstevel@tonic-gate namelen == addrlen &&
44367c478bd9Sstevel@tonic-gate sin1->sin6_port == sin2->sin6_port &&
44377c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
44387c478bd9Sstevel@tonic-gate &sin2->sin6_addr))
44397c478bd9Sstevel@tonic-gate match = B_TRUE;
44407c478bd9Sstevel@tonic-gate break;
44417c478bd9Sstevel@tonic-gate }
44427c478bd9Sstevel@tonic-gate case AF_UNIX:
44437c478bd9Sstevel@tonic-gate default:
44447c478bd9Sstevel@tonic-gate if (namelen == addrlen &&
44457c478bd9Sstevel@tonic-gate bcmp(name, addr, namelen) == 0)
44467c478bd9Sstevel@tonic-gate match = B_TRUE;
44477c478bd9Sstevel@tonic-gate }
44487c478bd9Sstevel@tonic-gate if (match) {
44490f1702c5SYu Xiangning freemsg(sti->sti_eaddr_mp);
44500f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
44517c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
44527c478bd9Sstevel@tonic-gate #ifdef DEBUG
44537c478bd9Sstevel@tonic-gate dprintso(so, 0,
44547c478bd9Sstevel@tonic-gate ("sockfs delayed error %d for %s\n",
44557c478bd9Sstevel@tonic-gate error,
44567c478bd9Sstevel@tonic-gate pr_addr(so->so_family, name, namelen)));
44577c478bd9Sstevel@tonic-gate #endif /* DEBUG */
44587c478bd9Sstevel@tonic-gate return (error);
44597c478bd9Sstevel@tonic-gate }
44600f1702c5SYu Xiangning freemsg(sti->sti_eaddr_mp);
44610f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
44627c478bd9Sstevel@tonic-gate }
44637c478bd9Sstevel@tonic-gate }
44647c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
44657c478bd9Sstevel@tonic-gate
44667c478bd9Sstevel@tonic-gate dontroute = 0;
44677c478bd9Sstevel@tonic-gate if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
44687c478bd9Sstevel@tonic-gate uint32_t val;
44697c478bd9Sstevel@tonic-gate
44707c478bd9Sstevel@tonic-gate val = 1;
44717c478bd9Sstevel@tonic-gate error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
44720f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
44737c478bd9Sstevel@tonic-gate if (error)
44747c478bd9Sstevel@tonic-gate return (error);
44757c478bd9Sstevel@tonic-gate dontroute = 1;
44767c478bd9Sstevel@tonic-gate }
44777c478bd9Sstevel@tonic-gate
44787c478bd9Sstevel@tonic-gate if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
44797c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
44807c478bd9Sstevel@tonic-gate goto done;
44817c478bd9Sstevel@tonic-gate }
44827c478bd9Sstevel@tonic-gate if (msg->msg_controllen != 0) {
44837c478bd9Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) {
44840f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
44857c478bd9Sstevel@tonic-gate error = sosend_dgramcmsg(so, name, namelen, uiop,
4486ff550d0eSmasputra msg->msg_control, msg->msg_controllen, flags);
44877c478bd9Sstevel@tonic-gate } else {
44887c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
44897c478bd9Sstevel@tonic-gate /* Can't generate T_EXDATA_REQ with options */
44907c478bd9Sstevel@tonic-gate error = EOPNOTSUPP;
44917c478bd9Sstevel@tonic-gate goto done;
44927c478bd9Sstevel@tonic-gate }
44930f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
44947c478bd9Sstevel@tonic-gate error = sosend_svccmsg(so, uiop,
44957c478bd9Sstevel@tonic-gate !(flags & MSG_EOR),
44967c478bd9Sstevel@tonic-gate msg->msg_control, msg->msg_controllen,
44977c478bd9Sstevel@tonic-gate flags);
44987c478bd9Sstevel@tonic-gate }
44997c478bd9Sstevel@tonic-gate goto done;
45007c478bd9Sstevel@tonic-gate }
45017c478bd9Sstevel@tonic-gate
45020f1702c5SYu Xiangning so_update_attrs(so, SOMOD);
45037c478bd9Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) {
45047c478bd9Sstevel@tonic-gate /*
45057c478bd9Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately
4506ff550d0eSmasputra * from send_dgram. This can allow tail-call optimizations.
45077c478bd9Sstevel@tonic-gate */
45087c478bd9Sstevel@tonic-gate if (!dontroute) {
45097c478bd9Sstevel@tonic-gate return (sosend_dgram(so, name, namelen, uiop, flags));
45107c478bd9Sstevel@tonic-gate }
45117c478bd9Sstevel@tonic-gate error = sosend_dgram(so, name, namelen, uiop, flags);
45127c478bd9Sstevel@tonic-gate } else {
45137c478bd9Sstevel@tonic-gate t_scalar_t prim;
45147c478bd9Sstevel@tonic-gate int sflag;
45157c478bd9Sstevel@tonic-gate
45167c478bd9Sstevel@tonic-gate /* Ignore msg_name in the connected state */
45177c478bd9Sstevel@tonic-gate if (flags & MSG_OOB) {
45187c478bd9Sstevel@tonic-gate prim = T_EXDATA_REQ;
45197c478bd9Sstevel@tonic-gate /*
45207c478bd9Sstevel@tonic-gate * Send down T_EXDATA_REQ even if there is flow
45217c478bd9Sstevel@tonic-gate * control for data.
45227c478bd9Sstevel@tonic-gate */
45237c478bd9Sstevel@tonic-gate sflag = MSG_IGNFLOW;
45247c478bd9Sstevel@tonic-gate } else {
45257c478bd9Sstevel@tonic-gate if (so_mode & SM_BYTESTREAM) {
45267c478bd9Sstevel@tonic-gate /* Byte stream transport - use write */
45277c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg: write\n"));
45280f1702c5SYu Xiangning
45290f1702c5SYu Xiangning /* Send M_DATA messages */
45300f1702c5SYu Xiangning if ((sti->sti_nl7c_flags & NL7C_ENABLED) &&
45310f1702c5SYu Xiangning (error = nl7c_data(so, uiop)) >= 0) {
45320f1702c5SYu Xiangning /* NL7C consumed the data */
45330f1702c5SYu Xiangning return (error);
45340f1702c5SYu Xiangning }
45357c478bd9Sstevel@tonic-gate /*
4536ff550d0eSmasputra * If there is no SO_DONTROUTE to turn off,
45370f1702c5SYu Xiangning * sti_direct is on, and there is no flow
4538ff550d0eSmasputra * control, we can take the fast path.
45397c478bd9Sstevel@tonic-gate */
45400f1702c5SYu Xiangning if (!dontroute && sti->sti_direct != 0 &&
4541ff550d0eSmasputra canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4542ff550d0eSmasputra return (sostream_direct(so, uiop,
45430f1702c5SYu Xiangning NULL, cr));
4544ff550d0eSmasputra }
45450f1702c5SYu Xiangning error = strwrite(SOTOV(so), uiop, cr);
45467c478bd9Sstevel@tonic-gate goto done;
45477c478bd9Sstevel@tonic-gate }
45487c478bd9Sstevel@tonic-gate prim = T_DATA_REQ;
45497c478bd9Sstevel@tonic-gate sflag = 0;
45507c478bd9Sstevel@tonic-gate }
45517c478bd9Sstevel@tonic-gate /*
45527c478bd9Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately
45537c478bd9Sstevel@tonic-gate * from sosend_svc. This can allow tail-call optimizations.
45547c478bd9Sstevel@tonic-gate */
45557c478bd9Sstevel@tonic-gate if (!dontroute)
45567c478bd9Sstevel@tonic-gate return (sosend_svc(so, uiop, prim,
45577c478bd9Sstevel@tonic-gate !(flags & MSG_EOR), sflag));
45587c478bd9Sstevel@tonic-gate error = sosend_svc(so, uiop, prim,
45597c478bd9Sstevel@tonic-gate !(flags & MSG_EOR), sflag);
45607c478bd9Sstevel@tonic-gate }
45617c478bd9Sstevel@tonic-gate ASSERT(dontroute);
45627c478bd9Sstevel@tonic-gate done:
45637c478bd9Sstevel@tonic-gate if (dontroute) {
45647c478bd9Sstevel@tonic-gate uint32_t val;
45657c478bd9Sstevel@tonic-gate
45667c478bd9Sstevel@tonic-gate val = 0;
45677c478bd9Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
45680f1702c5SYu Xiangning &val, (t_uscalar_t)sizeof (val), cr);
45697c478bd9Sstevel@tonic-gate }
45707c478bd9Sstevel@tonic-gate return (error);
45717c478bd9Sstevel@tonic-gate }
45727c478bd9Sstevel@tonic-gate
45737c478bd9Sstevel@tonic-gate /*
45740f1702c5SYu Xiangning * kstrwritemp() has very similar semantics as that of strwrite().
45750f1702c5SYu Xiangning * The main difference is it obtains mblks from the caller and also
45760f1702c5SYu Xiangning * does not do any copy as done in strwrite() from user buffers to
45770f1702c5SYu Xiangning * kernel buffers.
45780f1702c5SYu Xiangning *
45790f1702c5SYu Xiangning * Currently, this routine is used by sendfile to send data allocated
45800f1702c5SYu Xiangning * within the kernel without any copying. This interface does not use the
45810f1702c5SYu Xiangning * synchronous stream interface as synch. stream interface implies
45820f1702c5SYu Xiangning * copying.
45830f1702c5SYu Xiangning */
45840f1702c5SYu Xiangning int
kstrwritemp(struct vnode * vp,mblk_t * mp,ushort_t fmode)45850f1702c5SYu Xiangning kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode)
45860f1702c5SYu Xiangning {
45870f1702c5SYu Xiangning struct stdata *stp;
45880f1702c5SYu Xiangning struct queue *wqp;
45890f1702c5SYu Xiangning mblk_t *newmp;
45900f1702c5SYu Xiangning char waitflag;
45910f1702c5SYu Xiangning int tempmode;
45920f1702c5SYu Xiangning int error = 0;
45930f1702c5SYu Xiangning int done = 0;
45940f1702c5SYu Xiangning struct sonode *so;
45950f1702c5SYu Xiangning boolean_t direct;
45960f1702c5SYu Xiangning
45970f1702c5SYu Xiangning ASSERT(vp->v_stream);
45980f1702c5SYu Xiangning stp = vp->v_stream;
45990f1702c5SYu Xiangning
46000f1702c5SYu Xiangning so = VTOSO(vp);
46010f1702c5SYu Xiangning direct = _SOTOTPI(so)->sti_direct;
46020f1702c5SYu Xiangning
46030f1702c5SYu Xiangning /*
46040f1702c5SYu Xiangning * This is the sockfs direct fast path. canputnext() need
46050f1702c5SYu Xiangning * not be accurate so we don't grab the sd_lock here. If
46060f1702c5SYu Xiangning * we get flow-controlled, we grab sd_lock just before the
46070f1702c5SYu Xiangning * do..while loop below to emulate what strwrite() does.
46080f1702c5SYu Xiangning */
46090f1702c5SYu Xiangning wqp = stp->sd_wrq;
46100f1702c5SYu Xiangning if (canputnext(wqp) && direct &&
46110f1702c5SYu Xiangning !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
46120f1702c5SYu Xiangning return (sostream_direct(so, NULL, mp, CRED()));
46130f1702c5SYu Xiangning } else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
46140f1702c5SYu Xiangning /* Fast check of flags before acquiring the lock */
46150f1702c5SYu Xiangning mutex_enter(&stp->sd_lock);
46160f1702c5SYu Xiangning error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
46170f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
46180f1702c5SYu Xiangning if (error != 0) {
46190f1702c5SYu Xiangning if (!(stp->sd_flag & STPLEX) &&
46200f1702c5SYu Xiangning (stp->sd_wput_opt & SW_SIGPIPE)) {
46210f1702c5SYu Xiangning error = EPIPE;
46220f1702c5SYu Xiangning }
46230f1702c5SYu Xiangning return (error);
46240f1702c5SYu Xiangning }
46250f1702c5SYu Xiangning }
46260f1702c5SYu Xiangning
46270f1702c5SYu Xiangning waitflag = WRITEWAIT;
46280f1702c5SYu Xiangning if (stp->sd_flag & OLDNDELAY)
46290f1702c5SYu Xiangning tempmode = fmode & ~FNDELAY;
46300f1702c5SYu Xiangning else
46310f1702c5SYu Xiangning tempmode = fmode;
46320f1702c5SYu Xiangning
46330f1702c5SYu Xiangning mutex_enter(&stp->sd_lock);
46340f1702c5SYu Xiangning do {
46350f1702c5SYu Xiangning if (canputnext(wqp)) {
46360f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
46370f1702c5SYu Xiangning if (stp->sd_wputdatafunc != NULL) {
46380f1702c5SYu Xiangning newmp = (stp->sd_wputdatafunc)(vp, mp, NULL,
46390f1702c5SYu Xiangning NULL, NULL, NULL);
46400f1702c5SYu Xiangning if (newmp == NULL) {
46410f1702c5SYu Xiangning /* The caller will free mp */
46420f1702c5SYu Xiangning return (ECOMM);
46430f1702c5SYu Xiangning }
46440f1702c5SYu Xiangning mp = newmp;
46450f1702c5SYu Xiangning }
46460f1702c5SYu Xiangning putnext(wqp, mp);
46470f1702c5SYu Xiangning return (0);
46480f1702c5SYu Xiangning }
46490f1702c5SYu Xiangning error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1,
46500f1702c5SYu Xiangning &done);
46510f1702c5SYu Xiangning } while (error == 0 && !done);
46520f1702c5SYu Xiangning
46530f1702c5SYu Xiangning mutex_exit(&stp->sd_lock);
46540f1702c5SYu Xiangning /*
46550f1702c5SYu Xiangning * EAGAIN tells the application to try again. ENOMEM
46560f1702c5SYu Xiangning * is returned only if the memory allocation size
46570f1702c5SYu Xiangning * exceeds the physical limits of the system. ENOMEM
46580f1702c5SYu Xiangning * can't be true here.
46590f1702c5SYu Xiangning */
46600f1702c5SYu Xiangning if (error == ENOMEM)
46610f1702c5SYu Xiangning error = EAGAIN;
46620f1702c5SYu Xiangning return (error);
46630f1702c5SYu Xiangning }
46640f1702c5SYu Xiangning
46650f1702c5SYu Xiangning /* ARGSUSED */
46660f1702c5SYu Xiangning static int
sotpi_sendmblk(struct sonode * so,struct nmsghdr * msg,int fflag,struct cred * cr,mblk_t ** mpp)46670f1702c5SYu Xiangning sotpi_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
46680f1702c5SYu Xiangning struct cred *cr, mblk_t **mpp)
46690f1702c5SYu Xiangning {
46700f1702c5SYu Xiangning int error;
46710f1702c5SYu Xiangning
46720f1702c5SYu Xiangning if (so->so_family != AF_INET && so->so_family != AF_INET6)
46730f1702c5SYu Xiangning return (EAFNOSUPPORT);
46740f1702c5SYu Xiangning
46750f1702c5SYu Xiangning if (so->so_state & SS_CANTSENDMORE)
46760f1702c5SYu Xiangning return (EPIPE);
46770f1702c5SYu Xiangning
46780f1702c5SYu Xiangning if (so->so_type != SOCK_STREAM)
46790f1702c5SYu Xiangning return (EOPNOTSUPP);
46800f1702c5SYu Xiangning
46810f1702c5SYu Xiangning if ((so->so_state & SS_ISCONNECTED) == 0)
46820f1702c5SYu Xiangning return (ENOTCONN);
46830f1702c5SYu Xiangning
46840f1702c5SYu Xiangning error = kstrwritemp(so->so_vnode, *mpp, fflag);
46850f1702c5SYu Xiangning if (error == 0)
46860f1702c5SYu Xiangning *mpp = NULL;
46870f1702c5SYu Xiangning return (error);
46880f1702c5SYu Xiangning }
46890f1702c5SYu Xiangning
46900f1702c5SYu Xiangning /*
4691ff550d0eSmasputra * Sending data on a datagram socket.
4692ff550d0eSmasputra * Assumes caller has verified that SS_ISBOUND etc. are set.
4693ff550d0eSmasputra */
4694ff550d0eSmasputra /* ARGSUSED */
4695ff550d0eSmasputra static int
sodgram_direct(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,int flags)4696ff550d0eSmasputra sodgram_direct(struct sonode *so, struct sockaddr *name,
4697ff550d0eSmasputra socklen_t namelen, struct uio *uiop, int flags)
4698ff550d0eSmasputra {
4699ff550d0eSmasputra struct T_unitdata_req tudr;
4700fc80c0dfSnordmark mblk_t *mp = NULL;
4701ff550d0eSmasputra int error = 0;
4702ff550d0eSmasputra void *addr;
4703ff550d0eSmasputra socklen_t addrlen;
4704ff550d0eSmasputra ssize_t len;
4705ff550d0eSmasputra struct stdata *stp = SOTOV(so)->v_stream;
4706ff550d0eSmasputra int so_state;
4707ff550d0eSmasputra queue_t *udp_wq;
4708fc80c0dfSnordmark boolean_t connected;
4709fc80c0dfSnordmark mblk_t *mpdata = NULL;
47100f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
4711005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
4712ff550d0eSmasputra
4713ff550d0eSmasputra ASSERT(name != NULL && namelen != 0);
4714ff550d0eSmasputra ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4715ff550d0eSmasputra ASSERT(!(so->so_mode & SM_EXDATA));
4716ff550d0eSmasputra ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4717ff550d0eSmasputra ASSERT(SOTOV(so)->v_type == VSOCK);
4718ff550d0eSmasputra
4719ff550d0eSmasputra /* Caller checked for proper length */
4720ff550d0eSmasputra len = uiop->uio_resid;
47210f1702c5SYu Xiangning ASSERT(len <= sti->sti_tidu_size);
4722ff550d0eSmasputra
4723ff550d0eSmasputra /* Length and family checks have been done by caller */
4724ff550d0eSmasputra ASSERT(name->sa_family == so->so_family);
4725ff550d0eSmasputra ASSERT(so->so_family == AF_INET ||
4726ff550d0eSmasputra (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4727ff550d0eSmasputra ASSERT(so->so_family == AF_INET6 ||
4728ff550d0eSmasputra (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4729ff550d0eSmasputra
4730ff550d0eSmasputra addr = name;
4731ff550d0eSmasputra addrlen = namelen;
4732ff550d0eSmasputra
4733ff550d0eSmasputra if (stp->sd_sidp != NULL &&
4734ff550d0eSmasputra (error = straccess(stp, JCWRITE)) != 0)
4735ff550d0eSmasputra goto done;
4736ff550d0eSmasputra
4737ff550d0eSmasputra so_state = so->so_state;
4738ff550d0eSmasputra
4739fc80c0dfSnordmark connected = so_state & SS_ISCONNECTED;
4740fc80c0dfSnordmark if (!connected) {
4741fc80c0dfSnordmark tudr.PRIM_type = T_UNITDATA_REQ;
4742fc80c0dfSnordmark tudr.DEST_length = addrlen;
4743fc80c0dfSnordmark tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
4744fc80c0dfSnordmark tudr.OPT_length = 0;
4745fc80c0dfSnordmark tudr.OPT_offset = 0;
4746fc80c0dfSnordmark
4747fc80c0dfSnordmark mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0,
4748de8c4a14SErik Nordmark _ALLOC_INTR, CRED());
4749fc80c0dfSnordmark if (mp == NULL) {
4750fc80c0dfSnordmark /*
4751fc80c0dfSnordmark * Caught a signal waiting for memory.
4752fc80c0dfSnordmark * Let send* return EINTR.
4753fc80c0dfSnordmark */
4754fc80c0dfSnordmark error = EINTR;
4755fc80c0dfSnordmark goto done;
4756fc80c0dfSnordmark }
4757fc80c0dfSnordmark }
4758fc80c0dfSnordmark
4759ff550d0eSmasputra /*
4760ff550d0eSmasputra * For UDP we don't break up the copyin into smaller pieces
4761ff550d0eSmasputra * as in the TCP case. That means if ENOMEM is returned by
4762ff550d0eSmasputra * mcopyinuio() then the uio vector has not been modified at
4763ff550d0eSmasputra * all and we fallback to either strwrite() or kstrputmsg()
4764ff550d0eSmasputra * below. Note also that we never generate priority messages
4765ff550d0eSmasputra * from here.
4766ff550d0eSmasputra */
4767ff550d0eSmasputra udp_wq = stp->sd_wrq->q_next;
4768ff550d0eSmasputra if (canput(udp_wq) &&
4769fc80c0dfSnordmark (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
4770fc80c0dfSnordmark ASSERT(DB_TYPE(mpdata) == M_DATA);
4771ff550d0eSmasputra ASSERT(uiop->uio_resid == 0);
4772fc80c0dfSnordmark if (!connected)
4773fc80c0dfSnordmark linkb(mp, mpdata);
4774fc80c0dfSnordmark else
4775fc80c0dfSnordmark mp = mpdata;
4776005d3febSMarek Pospisil if (auditing)
4777ff550d0eSmasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4778fc80c0dfSnordmark
4779fc80c0dfSnordmark udp_wput(udp_wq, mp);
4780ff550d0eSmasputra return (0);
4781ff550d0eSmasputra }
4782fc80c0dfSnordmark
4783fc80c0dfSnordmark ASSERT(mpdata == NULL);
4784fc80c0dfSnordmark if (error != 0 && error != ENOMEM) {
4785fc80c0dfSnordmark freemsg(mp);
4786ff550d0eSmasputra return (error);
4787fc80c0dfSnordmark }
4788ff550d0eSmasputra
4789ff550d0eSmasputra /*
4790ff550d0eSmasputra * For connected, let strwrite() handle the blocking case.
4791ff550d0eSmasputra * Otherwise we fall thru and use kstrputmsg().
4792ff550d0eSmasputra */
4793fc80c0dfSnordmark if (connected)
4794ff550d0eSmasputra return (strwrite(SOTOV(so), uiop, CRED()));
4795ff550d0eSmasputra
4796005d3febSMarek Pospisil if (auditing)
4797ff550d0eSmasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4798ff550d0eSmasputra
4799ff550d0eSmasputra error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4800ff550d0eSmasputra done:
4801ff550d0eSmasputra #ifdef SOCK_DEBUG
4802ff550d0eSmasputra if (error != 0) {
4803ff550d0eSmasputra eprintsoline(so, error);
4804ff550d0eSmasputra }
4805ff550d0eSmasputra #endif /* SOCK_DEBUG */
4806ff550d0eSmasputra return (error);
4807ff550d0eSmasputra }
4808ff550d0eSmasputra
4809ff550d0eSmasputra int
sostream_direct(struct sonode * so,struct uio * uiop,mblk_t * mp,cred_t * cr)4810ff550d0eSmasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4811ff550d0eSmasputra {
4812ff550d0eSmasputra struct stdata *stp = SOTOV(so)->v_stream;
4813ff550d0eSmasputra ssize_t iosize, rmax, maxblk;
4814ff550d0eSmasputra queue_t *tcp_wq = stp->sd_wrq->q_next;
4815c28749e9Skais mblk_t *newmp;
4816ff550d0eSmasputra int error = 0, wflag = 0;
4817ff550d0eSmasputra
4818ff550d0eSmasputra ASSERT(so->so_mode & SM_BYTESTREAM);
4819ff550d0eSmasputra ASSERT(SOTOV(so)->v_type == VSOCK);
4820ff550d0eSmasputra
4821ff550d0eSmasputra if (stp->sd_sidp != NULL &&
4822ff550d0eSmasputra (error = straccess(stp, JCWRITE)) != 0)
4823ff550d0eSmasputra return (error);
4824ff550d0eSmasputra
4825ff550d0eSmasputra if (uiop == NULL) {
4826ff550d0eSmasputra /*
4827ff550d0eSmasputra * kstrwritemp() should have checked sd_flag and
4828ff550d0eSmasputra * flow-control before coming here. If we end up
4829ff550d0eSmasputra * here it means that we can simply pass down the
4830ff550d0eSmasputra * data to tcp.
4831ff550d0eSmasputra */
4832ff550d0eSmasputra ASSERT(mp != NULL);
4833c28749e9Skais if (stp->sd_wputdatafunc != NULL) {
4834c28749e9Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4835c28749e9Skais NULL, NULL, NULL);
4836c28749e9Skais if (newmp == NULL) {
4837c28749e9Skais /* The caller will free mp */
4838c28749e9Skais return (ECOMM);
4839c28749e9Skais }
4840c28749e9Skais mp = newmp;
4841c28749e9Skais }
4842ff550d0eSmasputra tcp_wput(tcp_wq, mp);
4843ff550d0eSmasputra return (0);
4844ff550d0eSmasputra }
4845ff550d0eSmasputra
4846ff550d0eSmasputra /* Fallback to strwrite() to do proper error handling */
4847ff550d0eSmasputra if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4848ff550d0eSmasputra return (strwrite(SOTOV(so), uiop, cr));
4849ff550d0eSmasputra
4850ff550d0eSmasputra rmax = stp->sd_qn_maxpsz;
4851ff550d0eSmasputra ASSERT(rmax >= 0 || rmax == INFPSZ);
4852ff550d0eSmasputra if (rmax == 0 || uiop->uio_resid <= 0)
4853ff550d0eSmasputra return (0);
4854ff550d0eSmasputra
4855ff550d0eSmasputra if (rmax == INFPSZ)
4856ff550d0eSmasputra rmax = uiop->uio_resid;
4857ff550d0eSmasputra
4858ff550d0eSmasputra maxblk = stp->sd_maxblk;
4859ff550d0eSmasputra
4860ff550d0eSmasputra for (;;) {
4861ff550d0eSmasputra iosize = MIN(uiop->uio_resid, rmax);
4862ff550d0eSmasputra
4863ff550d0eSmasputra mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4864ff550d0eSmasputra if (mp == NULL) {
4865ff550d0eSmasputra /*
4866ff550d0eSmasputra * Fallback to strwrite() for ENOMEM; if this
4867ff550d0eSmasputra * is our first time in this routine and the uio
4868ff550d0eSmasputra * vector has not been modified, we will end up
4869ff550d0eSmasputra * calling strwrite() without any flag set.
4870ff550d0eSmasputra */
4871ff550d0eSmasputra if (error == ENOMEM)
4872ff550d0eSmasputra goto slow_send;
4873ff550d0eSmasputra else
4874ff550d0eSmasputra return (error);
4875ff550d0eSmasputra }
4876ff550d0eSmasputra ASSERT(uiop->uio_resid >= 0);
4877ff550d0eSmasputra /*
4878ff550d0eSmasputra * If mp is non-NULL and ENOMEM is set, it means that
4879ff550d0eSmasputra * mcopyinuio() was able to break down some of the user
4880ff550d0eSmasputra * data into one or more mblks. Send the partial data
4881ff550d0eSmasputra * to tcp and let the rest be handled in strwrite().
4882ff550d0eSmasputra */
4883ff550d0eSmasputra ASSERT(error == 0 || error == ENOMEM);
4884c28749e9Skais if (stp->sd_wputdatafunc != NULL) {
4885c28749e9Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4886c28749e9Skais NULL, NULL, NULL);
4887c28749e9Skais if (newmp == NULL) {
4888c28749e9Skais /* The caller will free mp */
4889c28749e9Skais return (ECOMM);
4890c28749e9Skais }
4891c28749e9Skais mp = newmp;
4892c28749e9Skais }
4893ff550d0eSmasputra tcp_wput(tcp_wq, mp);
4894ff550d0eSmasputra
4895ff550d0eSmasputra wflag |= NOINTR;
4896ff550d0eSmasputra
4897ff550d0eSmasputra if (uiop->uio_resid == 0) { /* No more data; we're done */
4898ff550d0eSmasputra ASSERT(error == 0);
4899ff550d0eSmasputra break;
4900ff550d0eSmasputra } else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4901ff550d0eSmasputra (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4902ff550d0eSmasputra slow_send:
4903ff550d0eSmasputra /*
4904ff550d0eSmasputra * We were able to send down partial data using
4905ff550d0eSmasputra * the direct call interface, but are now relying
4906ff550d0eSmasputra * on strwrite() to handle the non-fastpath cases.
4907ff550d0eSmasputra * If the socket is blocking we will sleep in
4908ff550d0eSmasputra * strwaitq() until write is permitted, otherwise,
4909ff550d0eSmasputra * we will need to return the amount of bytes
4910ff550d0eSmasputra * written so far back to the app. This is the
4911ff550d0eSmasputra * reason why we pass NOINTR flag to strwrite()
4912ff550d0eSmasputra * for non-blocking socket, because we don't want
4913ff550d0eSmasputra * to return EAGAIN when portion of the user data
4914ff550d0eSmasputra * has actually been sent down.
4915ff550d0eSmasputra */
4916ff550d0eSmasputra return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4917ff550d0eSmasputra }
4918ff550d0eSmasputra }
4919ff550d0eSmasputra return (0);
4920ff550d0eSmasputra }
4921ff550d0eSmasputra
4922ff550d0eSmasputra /*
49230f1702c5SYu Xiangning * Update sti_faddr by asking the transport (unless AF_UNIX).
49247c478bd9Sstevel@tonic-gate */
49250f1702c5SYu Xiangning /* ARGSUSED */
49267c478bd9Sstevel@tonic-gate int
sotpi_getpeername(struct sonode * so,struct sockaddr * name,socklen_t * namelen,boolean_t accept,struct cred * cr)49270f1702c5SYu Xiangning sotpi_getpeername(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
49280f1702c5SYu Xiangning boolean_t accept, struct cred *cr)
49297c478bd9Sstevel@tonic-gate {
49307c478bd9Sstevel@tonic-gate struct strbuf strbuf;
49317c478bd9Sstevel@tonic-gate int error = 0, res;
49327c478bd9Sstevel@tonic-gate void *addr;
49337c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
49347c478bd9Sstevel@tonic-gate k_sigset_t smask;
49350f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
49367c478bd9Sstevel@tonic-gate
49377c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
4938903a11ebSrh87107 (void *)so, pr_state(so->so_state, so->so_mode)));
49397c478bd9Sstevel@tonic-gate
49400f1702c5SYu Xiangning ASSERT(*namelen > 0);
49417c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
49427c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
49430f1702c5SYu Xiangning
49440f1702c5SYu Xiangning if (accept) {
49450f1702c5SYu Xiangning bcopy(sti->sti_faddr_sa, name,
49460f1702c5SYu Xiangning MIN(*namelen, sti->sti_faddr_len));
49470f1702c5SYu Xiangning *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
49480f1702c5SYu Xiangning goto done;
49490f1702c5SYu Xiangning }
49500f1702c5SYu Xiangning
49517c478bd9Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) {
49527c478bd9Sstevel@tonic-gate error = ENOTCONN;
49537c478bd9Sstevel@tonic-gate goto done;
49547c478bd9Sstevel@tonic-gate }
49557c478bd9Sstevel@tonic-gate /* Added this check for X/Open */
49567c478bd9Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
49577c478bd9Sstevel@tonic-gate error = EINVAL;
49587c478bd9Sstevel@tonic-gate if (xnet_check_print) {
49597c478bd9Sstevel@tonic-gate printf("sockfs: X/Open getpeername check => EINVAL\n");
49607c478bd9Sstevel@tonic-gate }
49617c478bd9Sstevel@tonic-gate goto done;
49627c478bd9Sstevel@tonic-gate }
49630f1702c5SYu Xiangning
49640f1702c5SYu Xiangning if (sti->sti_faddr_valid) {
49650f1702c5SYu Xiangning bcopy(sti->sti_faddr_sa, name,
49660f1702c5SYu Xiangning MIN(*namelen, sti->sti_faddr_len));
49670f1702c5SYu Xiangning *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
49680f1702c5SYu Xiangning goto done;
49690f1702c5SYu Xiangning }
49700f1702c5SYu Xiangning
49717c478bd9Sstevel@tonic-gate #ifdef DEBUG
49727c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
49730f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_faddr_sa,
49740f1702c5SYu Xiangning (t_uscalar_t)sti->sti_faddr_len)));
49757c478bd9Sstevel@tonic-gate #endif /* DEBUG */
49767c478bd9Sstevel@tonic-gate
49772caf0dcdSrshoaib if (so->so_family == AF_UNIX) {
49787c478bd9Sstevel@tonic-gate /* Transport has different name space - return local info */
49790f1702c5SYu Xiangning if (sti->sti_faddr_noxlate)
49800f1702c5SYu Xiangning *namelen = 0;
49817c478bd9Sstevel@tonic-gate error = 0;
49827c478bd9Sstevel@tonic-gate goto done;
49837c478bd9Sstevel@tonic-gate }
49847c478bd9Sstevel@tonic-gate
49850f1702c5SYu Xiangning ASSERT(so->so_family != AF_UNIX && sti->sti_faddr_noxlate == 0);
49860f1702c5SYu Xiangning
49870f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa);
49887c478bd9Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */
49890f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_faddr_maxlen;
49907c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
49917c478bd9Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP);
49927c478bd9Sstevel@tonic-gate
49937c478bd9Sstevel@tonic-gate /*
49947c478bd9Sstevel@tonic-gate * Issue TI_GETPEERNAME with signals masked.
49950f1702c5SYu Xiangning * Put the result in sti_faddr_sa so that getpeername works after
49967c478bd9Sstevel@tonic-gate * a shutdown(output).
49977c478bd9Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
49987c478bd9Sstevel@tonic-gate * back to the socket.
49997c478bd9Sstevel@tonic-gate */
50007c478bd9Sstevel@tonic-gate strbuf.buf = addr;
50017c478bd9Sstevel@tonic-gate strbuf.maxlen = addrlen;
50027c478bd9Sstevel@tonic-gate strbuf.len = 0;
50037c478bd9Sstevel@tonic-gate
50047c478bd9Sstevel@tonic-gate sigintr(&smask, 0);
50057c478bd9Sstevel@tonic-gate res = 0;
50060f1702c5SYu Xiangning ASSERT(cr);
50077c478bd9Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
50080f1702c5SYu Xiangning 0, K_TO_K, cr, &res);
50097c478bd9Sstevel@tonic-gate sigunintr(&smask);
50107c478bd9Sstevel@tonic-gate
50117c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
50127c478bd9Sstevel@tonic-gate /*
50137c478bd9Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail
50147c478bd9Sstevel@tonic-gate * the getpeername. Instead fallback on the recorded
50150f1702c5SYu Xiangning * sti->sti_faddr_sa.
50167c478bd9Sstevel@tonic-gate */
50177c478bd9Sstevel@tonic-gate if (error) {
50187c478bd9Sstevel@tonic-gate /*
50197c478bd9Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl.
50207c478bd9Sstevel@tonic-gate * However, it is impossible to determine which ones of
50217c478bd9Sstevel@tonic-gate * these are really socket level errors that were incorrectly
50227c478bd9Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the
50237c478bd9Sstevel@tonic-gate * error - to code explicitly does not reinstate the error
50247c478bd9Sstevel@tonic-gate * using soseterror().
50257c478bd9Sstevel@tonic-gate * Experiments have shows that at least this set of
50267c478bd9Sstevel@tonic-gate * errors are reported and should not be reinstated on the
50277c478bd9Sstevel@tonic-gate * socket:
50287c478bd9Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when
50297c478bd9Sstevel@tonic-gate * getpeername was called.
50307c478bd9Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write
50317c478bd9Sstevel@tonic-gate * side error over the read side error.
50327c478bd9Sstevel@tonic-gate * ENOTCONN The transport just got disconnected but
50337c478bd9Sstevel@tonic-gate * sockfs had not yet seen the T_DISCON_IND
50347c478bd9Sstevel@tonic-gate * when issuing the ioctl.
50357c478bd9Sstevel@tonic-gate */
50367c478bd9Sstevel@tonic-gate error = 0;
50377c478bd9Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 &&
50387c478bd9Sstevel@tonic-gate (so->so_state & SS_ISCONNECTED)) {
50390f1702c5SYu Xiangning ASSERT(strbuf.len <= (int)sti->sti_faddr_maxlen);
50400f1702c5SYu Xiangning sti->sti_faddr_len = (socklen_t)strbuf.len;
50410f1702c5SYu Xiangning bcopy(addr, sti->sti_faddr_sa, sti->sti_faddr_len);
50420f1702c5SYu Xiangning sti->sti_faddr_valid = 1;
50430f1702c5SYu Xiangning
50440f1702c5SYu Xiangning bcopy(addr, name, MIN(*namelen, sti->sti_faddr_len));
50450f1702c5SYu Xiangning *namelen = sti->sti_faddr_len;
50467c478bd9Sstevel@tonic-gate }
50477c478bd9Sstevel@tonic-gate kmem_free(addr, addrlen);
50487c478bd9Sstevel@tonic-gate #ifdef DEBUG
50497c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
50500f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_faddr_sa,
50510f1702c5SYu Xiangning (t_uscalar_t)sti->sti_faddr_len)));
50527c478bd9Sstevel@tonic-gate #endif /* DEBUG */
50537c478bd9Sstevel@tonic-gate done:
50547c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
50557c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
50567c478bd9Sstevel@tonic-gate return (error);
50577c478bd9Sstevel@tonic-gate }
50587c478bd9Sstevel@tonic-gate
50597c478bd9Sstevel@tonic-gate /*
50600f1702c5SYu Xiangning * Update sti_laddr by asking the transport (unless AF_UNIX).
50617c478bd9Sstevel@tonic-gate */
50627c478bd9Sstevel@tonic-gate int
sotpi_getsockname(struct sonode * so,struct sockaddr * name,socklen_t * namelen,struct cred * cr)50630f1702c5SYu Xiangning sotpi_getsockname(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
50640f1702c5SYu Xiangning struct cred *cr)
50657c478bd9Sstevel@tonic-gate {
50667c478bd9Sstevel@tonic-gate struct strbuf strbuf;
50677c478bd9Sstevel@tonic-gate int error = 0, res;
50687c478bd9Sstevel@tonic-gate void *addr;
50697c478bd9Sstevel@tonic-gate t_uscalar_t addrlen;
50707c478bd9Sstevel@tonic-gate k_sigset_t smask;
50710f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
50727c478bd9Sstevel@tonic-gate
50737c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
5074903a11ebSrh87107 (void *)so, pr_state(so->so_state, so->so_mode)));
50757c478bd9Sstevel@tonic-gate
50760f1702c5SYu Xiangning ASSERT(*namelen > 0);
50777c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
50787c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
50790f1702c5SYu Xiangning
50807c478bd9Sstevel@tonic-gate #ifdef DEBUG
50810f1702c5SYu Xiangning
50827c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
50830f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_laddr_sa,
50840f1702c5SYu Xiangning (t_uscalar_t)sti->sti_laddr_len)));
50857c478bd9Sstevel@tonic-gate #endif /* DEBUG */
50860f1702c5SYu Xiangning if (sti->sti_laddr_valid) {
50870f1702c5SYu Xiangning bcopy(sti->sti_laddr_sa, name,
50880f1702c5SYu Xiangning MIN(*namelen, sti->sti_laddr_len));
50890f1702c5SYu Xiangning *namelen = sti->sti_laddr_len;
50900f1702c5SYu Xiangning goto done;
50910f1702c5SYu Xiangning }
50920f1702c5SYu Xiangning
50937c478bd9Sstevel@tonic-gate if (so->so_family == AF_UNIX) {
5094dfc0fed8SRobert Mustacchi /*
5095dfc0fed8SRobert Mustacchi * Transport has different name space - return local info. If we
5096dfc0fed8SRobert Mustacchi * have enough space, let consumers know the family.
5097dfc0fed8SRobert Mustacchi */
5098dfc0fed8SRobert Mustacchi if (*namelen >= sizeof (sa_family_t)) {
5099dfc0fed8SRobert Mustacchi name->sa_family = AF_UNIX;
5100dfc0fed8SRobert Mustacchi *namelen = sizeof (sa_family_t);
5101dfc0fed8SRobert Mustacchi } else {
5102a5adac4dSYu Xiangning *namelen = 0;
5103dfc0fed8SRobert Mustacchi }
5104dfc0fed8SRobert Mustacchi error = 0;
51057c478bd9Sstevel@tonic-gate goto done;
51067c478bd9Sstevel@tonic-gate }
510718cbc865Sblu if (!(so->so_state & SS_ISBOUND)) {
510818cbc865Sblu /* If not bound, then nothing to return. */
510918cbc865Sblu error = 0;
511018cbc865Sblu goto done;
511118cbc865Sblu }
51120f1702c5SYu Xiangning
51137c478bd9Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */
51140f1702c5SYu Xiangning addrlen = (t_uscalar_t)sti->sti_laddr_maxlen;
51157c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
51167c478bd9Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP);
51177c478bd9Sstevel@tonic-gate
51187c478bd9Sstevel@tonic-gate /*
51197c478bd9Sstevel@tonic-gate * Issue TI_GETMYNAME with signals masked.
51200f1702c5SYu Xiangning * Put the result in sti_laddr_sa so that getsockname works after
51217c478bd9Sstevel@tonic-gate * a shutdown(output).
51227c478bd9Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
51237c478bd9Sstevel@tonic-gate * back to the socket.
51247c478bd9Sstevel@tonic-gate */
51257c478bd9Sstevel@tonic-gate strbuf.buf = addr;
51267c478bd9Sstevel@tonic-gate strbuf.maxlen = addrlen;
51277c478bd9Sstevel@tonic-gate strbuf.len = 0;
51287c478bd9Sstevel@tonic-gate
51297c478bd9Sstevel@tonic-gate sigintr(&smask, 0);
51307c478bd9Sstevel@tonic-gate res = 0;
51310f1702c5SYu Xiangning ASSERT(cr);
51327c478bd9Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
51330f1702c5SYu Xiangning 0, K_TO_K, cr, &res);
51347c478bd9Sstevel@tonic-gate sigunintr(&smask);
51357c478bd9Sstevel@tonic-gate
51367c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
51377c478bd9Sstevel@tonic-gate /*
51387c478bd9Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail
51397c478bd9Sstevel@tonic-gate * the getsockname. Instead fallback on the recorded
51400f1702c5SYu Xiangning * sti->sti_laddr_sa.
51417c478bd9Sstevel@tonic-gate */
51427c478bd9Sstevel@tonic-gate if (error) {
51437c478bd9Sstevel@tonic-gate /*
51447c478bd9Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl.
51457c478bd9Sstevel@tonic-gate * However, it is impossible to determine which ones of
51467c478bd9Sstevel@tonic-gate * these are really socket level errors that were incorrectly
51477c478bd9Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the
51487c478bd9Sstevel@tonic-gate * error - to code explicitly does not reinstate the error
51497c478bd9Sstevel@tonic-gate * using soseterror().
51507c478bd9Sstevel@tonic-gate * Experiments have shows that at least this set of
51517c478bd9Sstevel@tonic-gate * errors are reported and should not be reinstated on the
51527c478bd9Sstevel@tonic-gate * socket:
51537c478bd9Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when
51547c478bd9Sstevel@tonic-gate * getsockname was called.
51557c478bd9Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write
51567c478bd9Sstevel@tonic-gate * side error over the read side error.
51577c478bd9Sstevel@tonic-gate */
51587c478bd9Sstevel@tonic-gate error = 0;
51597c478bd9Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 &&
51607c478bd9Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) {
51610f1702c5SYu Xiangning ASSERT(strbuf.len <= (int)sti->sti_laddr_maxlen);
51620f1702c5SYu Xiangning sti->sti_laddr_len = (socklen_t)strbuf.len;
51630f1702c5SYu Xiangning bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
51640f1702c5SYu Xiangning sti->sti_laddr_valid = 1;
51650f1702c5SYu Xiangning
51660f1702c5SYu Xiangning bcopy(addr, name, MIN(sti->sti_laddr_len, *namelen));
51670f1702c5SYu Xiangning *namelen = sti->sti_laddr_len;
51687c478bd9Sstevel@tonic-gate }
51697c478bd9Sstevel@tonic-gate kmem_free(addr, addrlen);
51707c478bd9Sstevel@tonic-gate #ifdef DEBUG
51717c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
51720f1702c5SYu Xiangning pr_addr(so->so_family, sti->sti_laddr_sa,
51730f1702c5SYu Xiangning (t_uscalar_t)sti->sti_laddr_len)));
51747c478bd9Sstevel@tonic-gate #endif /* DEBUG */
51757c478bd9Sstevel@tonic-gate done:
51767c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
51777c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
51787c478bd9Sstevel@tonic-gate return (error);
51797c478bd9Sstevel@tonic-gate }
51807c478bd9Sstevel@tonic-gate
51817c478bd9Sstevel@tonic-gate /*
51827c478bd9Sstevel@tonic-gate * Get socket options. For SOL_SOCKET options some options are handled
51837c478bd9Sstevel@tonic-gate * by the sockfs while others use the value recorded in the sonode as a
51847c478bd9Sstevel@tonic-gate * fallback should the T_SVR4_OPTMGMT_REQ fail.
51857c478bd9Sstevel@tonic-gate *
51867c478bd9Sstevel@tonic-gate * On the return most *optlenp bytes are copied to optval.
51877c478bd9Sstevel@tonic-gate */
51880f1702c5SYu Xiangning /* ARGSUSED */
51897c478bd9Sstevel@tonic-gate int
sotpi_getsockopt(struct sonode * so,int level,int option_name,void * optval,socklen_t * optlenp,int flags,struct cred * cr)51907c478bd9Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name,
51910f1702c5SYu Xiangning void *optval, socklen_t *optlenp, int flags, struct cred *cr)
51927c478bd9Sstevel@tonic-gate {
51937c478bd9Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req;
51947c478bd9Sstevel@tonic-gate struct T_optmgmt_ack *optmgmt_ack;
51957c478bd9Sstevel@tonic-gate struct opthdr oh;
51967c478bd9Sstevel@tonic-gate struct opthdr *opt_res;
51977c478bd9Sstevel@tonic-gate mblk_t *mp = NULL;
51987c478bd9Sstevel@tonic-gate int error = 0;
51997c478bd9Sstevel@tonic-gate void *option = NULL; /* Set if fallback value */
52007c478bd9Sstevel@tonic-gate t_uscalar_t maxlen = *optlenp;
52017c478bd9Sstevel@tonic-gate t_uscalar_t len;
52027c478bd9Sstevel@tonic-gate uint32_t value;
52030f1702c5SYu Xiangning struct timeval tmo_val; /* used for SO_RCVTIMEO, SO_SNDTIMEO */
520422238f73Sshenjian struct timeval32 tmo_val32;
52050f1702c5SYu Xiangning struct so_snd_bufinfo snd_bufinfo; /* used for zero copy */
52067c478bd9Sstevel@tonic-gate
52077c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
5208903a11ebSrh87107 (void *)so, level, option_name, optval, (void *)optlenp,
52097c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
52107c478bd9Sstevel@tonic-gate
52117c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
52127c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
52137c478bd9Sstevel@tonic-gate
52147c478bd9Sstevel@tonic-gate /*
52157c478bd9Sstevel@tonic-gate * Check for SOL_SOCKET options.
52167c478bd9Sstevel@tonic-gate * Certain SOL_SOCKET options are returned directly whereas
52177c478bd9Sstevel@tonic-gate * others only provide a default (fallback) value should
52187c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ fail.
52197c478bd9Sstevel@tonic-gate */
52207c478bd9Sstevel@tonic-gate if (level == SOL_SOCKET) {
52217c478bd9Sstevel@tonic-gate /* Check parameters */
52227c478bd9Sstevel@tonic-gate switch (option_name) {
52237c478bd9Sstevel@tonic-gate case SO_TYPE:
52247c478bd9Sstevel@tonic-gate case SO_ERROR:
52257c478bd9Sstevel@tonic-gate case SO_DEBUG:
52267c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
52277c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
522878918900SArne Jansen case SO_REUSEPORT:
52297c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
52307c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
52317c478bd9Sstevel@tonic-gate case SO_BROADCAST:
52327c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
52337c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
52347c478bd9Sstevel@tonic-gate case SO_SNDBUF:
52357c478bd9Sstevel@tonic-gate case SO_RCVBUF:
52367c478bd9Sstevel@tonic-gate #ifdef notyet
52377c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
52387c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
52397c478bd9Sstevel@tonic-gate #endif /* notyet */
524088cda078Skcpoon case SO_DOMAIN:
52417c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
52427c478bd9Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
52437c478bd9Sstevel@tonic-gate error = EINVAL;
52447c478bd9Sstevel@tonic-gate eprintsoline(so, error);
52457c478bd9Sstevel@tonic-gate goto done2;
52467c478bd9Sstevel@tonic-gate }
52477c478bd9Sstevel@tonic-gate break;
52480f1702c5SYu Xiangning case SO_RCVTIMEO:
52490f1702c5SYu Xiangning case SO_SNDTIMEO:
5250e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5251e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
525222238f73Sshenjian if (maxlen < sizeof (struct timeval)) {
52530f1702c5SYu Xiangning error = EINVAL;
52540f1702c5SYu Xiangning eprintsoline(so, error);
52550f1702c5SYu Xiangning goto done2;
52560f1702c5SYu Xiangning }
525722238f73Sshenjian } else {
525822238f73Sshenjian if (maxlen < sizeof (struct timeval32)) {
525922238f73Sshenjian error = EINVAL;
526022238f73Sshenjian eprintsoline(so, error);
526122238f73Sshenjian goto done2;
526222238f73Sshenjian }
526322238f73Sshenjian
526422238f73Sshenjian }
52650f1702c5SYu Xiangning break;
52667c478bd9Sstevel@tonic-gate case SO_LINGER:
52677c478bd9Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
52687c478bd9Sstevel@tonic-gate error = EINVAL;
52697c478bd9Sstevel@tonic-gate eprintsoline(so, error);
52707c478bd9Sstevel@tonic-gate goto done2;
52717c478bd9Sstevel@tonic-gate }
52727c478bd9Sstevel@tonic-gate break;
52730f1702c5SYu Xiangning case SO_SND_BUFINFO:
52740f1702c5SYu Xiangning if (maxlen < (t_uscalar_t)
52750f1702c5SYu Xiangning sizeof (struct so_snd_bufinfo)) {
52760f1702c5SYu Xiangning error = EINVAL;
52770f1702c5SYu Xiangning eprintsoline(so, error);
52780f1702c5SYu Xiangning goto done2;
52790f1702c5SYu Xiangning }
52800f1702c5SYu Xiangning break;
52817c478bd9Sstevel@tonic-gate }
52827c478bd9Sstevel@tonic-gate
52837c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (uint32_t); /* Default */
52847c478bd9Sstevel@tonic-gate
52857c478bd9Sstevel@tonic-gate switch (option_name) {
52867c478bd9Sstevel@tonic-gate case SO_TYPE:
52877c478bd9Sstevel@tonic-gate value = so->so_type;
52887c478bd9Sstevel@tonic-gate option = &value;
52897c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52907c478bd9Sstevel@tonic-gate
52917c478bd9Sstevel@tonic-gate case SO_ERROR:
52920f1702c5SYu Xiangning value = sogeterr(so, B_TRUE);
52937c478bd9Sstevel@tonic-gate option = &value;
52947c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52957c478bd9Sstevel@tonic-gate
52967c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
52977c478bd9Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN)
52987c478bd9Sstevel@tonic-gate value = SO_ACCEPTCONN;
52997c478bd9Sstevel@tonic-gate else
53007c478bd9Sstevel@tonic-gate value = 0;
53017c478bd9Sstevel@tonic-gate #ifdef DEBUG
53027c478bd9Sstevel@tonic-gate if (value) {
53037c478bd9Sstevel@tonic-gate dprintso(so, 1,
53047c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n",
53057c478bd9Sstevel@tonic-gate option_name));
53067c478bd9Sstevel@tonic-gate } else {
53077c478bd9Sstevel@tonic-gate dprintso(so, 1,
53087c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n",
53097c478bd9Sstevel@tonic-gate option_name));
53107c478bd9Sstevel@tonic-gate }
53117c478bd9Sstevel@tonic-gate #endif /* DEBUG */
53127c478bd9Sstevel@tonic-gate option = &value;
53137c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
53147c478bd9Sstevel@tonic-gate
53157c478bd9Sstevel@tonic-gate case SO_DEBUG:
53167c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
531778918900SArne Jansen case SO_REUSEPORT:
53187c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
53197c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
53207c478bd9Sstevel@tonic-gate case SO_BROADCAST:
53217c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
53227c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
53237c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
53247c478bd9Sstevel@tonic-gate value = (so->so_options & option_name);
53257c478bd9Sstevel@tonic-gate #ifdef DEBUG
53267c478bd9Sstevel@tonic-gate if (value) {
53277c478bd9Sstevel@tonic-gate dprintso(so, 1,
53287c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n",
53297c478bd9Sstevel@tonic-gate option_name));
53307c478bd9Sstevel@tonic-gate } else {
53317c478bd9Sstevel@tonic-gate dprintso(so, 1,
53327c478bd9Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n",
53337c478bd9Sstevel@tonic-gate option_name));
53347c478bd9Sstevel@tonic-gate }
53357c478bd9Sstevel@tonic-gate #endif /* DEBUG */
53367c478bd9Sstevel@tonic-gate option = &value;
53377c478bd9Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
53387c478bd9Sstevel@tonic-gate
53397c478bd9Sstevel@tonic-gate /*
53407c478bd9Sstevel@tonic-gate * The following options are only returned by sockfs when the
53417c478bd9Sstevel@tonic-gate * T_SVR4_OPTMGMT_REQ fails.
53427c478bd9Sstevel@tonic-gate */
53437c478bd9Sstevel@tonic-gate case SO_LINGER:
53447c478bd9Sstevel@tonic-gate option = &so->so_linger;
53457c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (struct linger);
53467c478bd9Sstevel@tonic-gate break;
53477c478bd9Sstevel@tonic-gate case SO_SNDBUF: {
53487c478bd9Sstevel@tonic-gate ssize_t lvalue;
53497c478bd9Sstevel@tonic-gate
53507c478bd9Sstevel@tonic-gate /*
53517c478bd9Sstevel@tonic-gate * If the option has not been set then get a default
53527c478bd9Sstevel@tonic-gate * value from the read queue. This value is
53537c478bd9Sstevel@tonic-gate * returned if the transport fails
53547c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ.
53557c478bd9Sstevel@tonic-gate */
53567c478bd9Sstevel@tonic-gate lvalue = so->so_sndbuf;
53577c478bd9Sstevel@tonic-gate if (lvalue == 0) {
53587c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
53597c478bd9Sstevel@tonic-gate (void) strqget(strvp2wq(SOTOV(so))->q_next,
53607c478bd9Sstevel@tonic-gate QHIWAT, 0, &lvalue);
53617c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
53627c478bd9Sstevel@tonic-gate dprintso(so, 1,
53637c478bd9Sstevel@tonic-gate ("got SO_SNDBUF %ld from q\n", lvalue));
53647c478bd9Sstevel@tonic-gate }
53657c478bd9Sstevel@tonic-gate value = (int)lvalue;
53667c478bd9Sstevel@tonic-gate option = &value;
53677c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_sndbuf);
53687c478bd9Sstevel@tonic-gate break;
53697c478bd9Sstevel@tonic-gate }
53707c478bd9Sstevel@tonic-gate case SO_RCVBUF: {
53717c478bd9Sstevel@tonic-gate ssize_t lvalue;
53727c478bd9Sstevel@tonic-gate
53737c478bd9Sstevel@tonic-gate /*
53747c478bd9Sstevel@tonic-gate * If the option has not been set then get a default
53757c478bd9Sstevel@tonic-gate * value from the read queue. This value is
53767c478bd9Sstevel@tonic-gate * returned if the transport fails
53777c478bd9Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ.
53787c478bd9Sstevel@tonic-gate *
53797c478bd9Sstevel@tonic-gate * XXX If SO_RCVBUF has been set and this is an
53807c478bd9Sstevel@tonic-gate * XPG 4.2 application then do not ask the transport
53817c478bd9Sstevel@tonic-gate * since the transport might adjust the value and not
53827c478bd9Sstevel@tonic-gate * return exactly what was set by the application.
53837c478bd9Sstevel@tonic-gate * For non-XPG 4.2 application we return the value
53847c478bd9Sstevel@tonic-gate * that the transport is actually using.
53857c478bd9Sstevel@tonic-gate */
53867c478bd9Sstevel@tonic-gate lvalue = so->so_rcvbuf;
53877c478bd9Sstevel@tonic-gate if (lvalue == 0) {
53887c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
53897c478bd9Sstevel@tonic-gate (void) strqget(RD(strvp2wq(SOTOV(so))),
53907c478bd9Sstevel@tonic-gate QHIWAT, 0, &lvalue);
53917c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
53927c478bd9Sstevel@tonic-gate dprintso(so, 1,
53937c478bd9Sstevel@tonic-gate ("got SO_RCVBUF %ld from q\n", lvalue));
53947c478bd9Sstevel@tonic-gate } else if (flags & _SOGETSOCKOPT_XPG4_2) {
53957c478bd9Sstevel@tonic-gate value = (int)lvalue;
53967c478bd9Sstevel@tonic-gate option = &value;
53977c478bd9Sstevel@tonic-gate goto copyout; /* skip asking transport */
53987c478bd9Sstevel@tonic-gate }
53997c478bd9Sstevel@tonic-gate value = (int)lvalue;
54007c478bd9Sstevel@tonic-gate option = &value;
54017c478bd9Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_rcvbuf);
54027c478bd9Sstevel@tonic-gate break;
54037c478bd9Sstevel@tonic-gate }
540488cda078Skcpoon case SO_DOMAIN:
540588cda078Skcpoon value = so->so_family;
540688cda078Skcpoon option = &value;
540788cda078Skcpoon goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
540888cda078Skcpoon
54097c478bd9Sstevel@tonic-gate #ifdef notyet
54107c478bd9Sstevel@tonic-gate /*
54117c478bd9Sstevel@tonic-gate * We do not implement the semantics of these options
54127c478bd9Sstevel@tonic-gate * thus we shouldn't implement the options either.
54137c478bd9Sstevel@tonic-gate */
54147c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
54157c478bd9Sstevel@tonic-gate value = so->so_sndlowat;
54167c478bd9Sstevel@tonic-gate option = &value;
54177c478bd9Sstevel@tonic-gate break;
54187c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
54197c478bd9Sstevel@tonic-gate value = so->so_rcvlowat;
54207c478bd9Sstevel@tonic-gate option = &value;
54217c478bd9Sstevel@tonic-gate break;
54227c478bd9Sstevel@tonic-gate #endif /* notyet */
54230f1702c5SYu Xiangning case SO_SNDTIMEO:
54240f1702c5SYu Xiangning case SO_RCVTIMEO: {
54250f1702c5SYu Xiangning clock_t val;
542622238f73Sshenjian
54270f1702c5SYu Xiangning if (option_name == SO_RCVTIMEO)
54280f1702c5SYu Xiangning val = drv_hztousec(so->so_rcvtimeo);
54290f1702c5SYu Xiangning else
54300f1702c5SYu Xiangning val = drv_hztousec(so->so_sndtimeo);
54310f1702c5SYu Xiangning tmo_val.tv_sec = val / (1000 * 1000);
54320f1702c5SYu Xiangning tmo_val.tv_usec = val % (1000 * 1000);
5433e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5434e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
54350f1702c5SYu Xiangning option = &tmo_val;
543622238f73Sshenjian len = sizeof (struct timeval);
543722238f73Sshenjian } else {
543822238f73Sshenjian TIMEVAL_TO_TIMEVAL32(&tmo_val32, &tmo_val);
543922238f73Sshenjian option = &tmo_val32;
544022238f73Sshenjian len = sizeof (struct timeval32);
544122238f73Sshenjian }
54420f1702c5SYu Xiangning break;
54430f1702c5SYu Xiangning }
54440f1702c5SYu Xiangning case SO_SND_BUFINFO: {
54450f1702c5SYu Xiangning snd_bufinfo.sbi_wroff =
54460f1702c5SYu Xiangning (so->so_proto_props).sopp_wroff;
54470f1702c5SYu Xiangning snd_bufinfo.sbi_maxblk =
54480f1702c5SYu Xiangning (so->so_proto_props).sopp_maxblk;
54490f1702c5SYu Xiangning snd_bufinfo.sbi_maxpsz =
54500f1702c5SYu Xiangning (so->so_proto_props).sopp_maxpsz;
54510f1702c5SYu Xiangning snd_bufinfo.sbi_tail =
54520f1702c5SYu Xiangning (so->so_proto_props).sopp_tail;
54530f1702c5SYu Xiangning option = &snd_bufinfo;
54540f1702c5SYu Xiangning len = (t_uscalar_t)sizeof (struct so_snd_bufinfo);
54550f1702c5SYu Xiangning break;
54560f1702c5SYu Xiangning }
54577c478bd9Sstevel@tonic-gate }
54587c478bd9Sstevel@tonic-gate }
54597c478bd9Sstevel@tonic-gate
54607c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
54617c478bd9Sstevel@tonic-gate
54627c478bd9Sstevel@tonic-gate /* Send request */
54637c478bd9Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
54647c478bd9Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_CHECK;
54657c478bd9Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
54667c478bd9Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
54677c478bd9Sstevel@tonic-gate
54687c478bd9Sstevel@tonic-gate oh.level = level;
54697c478bd9Sstevel@tonic-gate oh.name = option_name;
54707c478bd9Sstevel@tonic-gate oh.len = maxlen;
54717c478bd9Sstevel@tonic-gate
54727c478bd9Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5473de8c4a14SErik Nordmark &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP, cr);
54747c478bd9Sstevel@tonic-gate /* Let option management work in the presence of data flow control */
54757c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
54767c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
54777c478bd9Sstevel@tonic-gate mp = NULL;
54787c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
54797c478bd9Sstevel@tonic-gate if (error) {
54807c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54817c478bd9Sstevel@tonic-gate goto done2;
54827c478bd9Sstevel@tonic-gate }
54837c478bd9Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
54847c478bd9Sstevel@tonic-gate (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
54857c478bd9Sstevel@tonic-gate if (error) {
54867c478bd9Sstevel@tonic-gate if (option != NULL) {
54877c478bd9Sstevel@tonic-gate /* We have a fallback value */
54887c478bd9Sstevel@tonic-gate error = 0;
54897c478bd9Sstevel@tonic-gate goto copyout;
54907c478bd9Sstevel@tonic-gate }
54917c478bd9Sstevel@tonic-gate eprintsoline(so, error);
54927c478bd9Sstevel@tonic-gate goto done2;
54937c478bd9Sstevel@tonic-gate }
54947c478bd9Sstevel@tonic-gate ASSERT(mp);
54957c478bd9Sstevel@tonic-gate optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
54967c478bd9Sstevel@tonic-gate opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
54977c478bd9Sstevel@tonic-gate optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
54987c478bd9Sstevel@tonic-gate if (opt_res == NULL) {
54997c478bd9Sstevel@tonic-gate if (option != NULL) {
55007c478bd9Sstevel@tonic-gate /* We have a fallback value */
55017c478bd9Sstevel@tonic-gate error = 0;
55027c478bd9Sstevel@tonic-gate goto copyout;
55037c478bd9Sstevel@tonic-gate }
55047c478bd9Sstevel@tonic-gate error = EPROTO;
55057c478bd9Sstevel@tonic-gate eprintsoline(so, error);
55067c478bd9Sstevel@tonic-gate goto done;
55077c478bd9Sstevel@tonic-gate }
55087c478bd9Sstevel@tonic-gate option = &opt_res[1];
55097c478bd9Sstevel@tonic-gate
55107c478bd9Sstevel@tonic-gate /* check to ensure that the option is within bounds */
55117c478bd9Sstevel@tonic-gate if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
55127c478bd9Sstevel@tonic-gate (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
55137c478bd9Sstevel@tonic-gate if (option != NULL) {
55147c478bd9Sstevel@tonic-gate /* We have a fallback value */
55157c478bd9Sstevel@tonic-gate error = 0;
55167c478bd9Sstevel@tonic-gate goto copyout;
55177c478bd9Sstevel@tonic-gate }
55187c478bd9Sstevel@tonic-gate error = EPROTO;
55197c478bd9Sstevel@tonic-gate eprintsoline(so, error);
55207c478bd9Sstevel@tonic-gate goto done;
55217c478bd9Sstevel@tonic-gate }
55227c478bd9Sstevel@tonic-gate
55237c478bd9Sstevel@tonic-gate len = opt_res->len;
55247c478bd9Sstevel@tonic-gate
55257c478bd9Sstevel@tonic-gate copyout: {
55267c478bd9Sstevel@tonic-gate t_uscalar_t size = MIN(len, maxlen);
55277c478bd9Sstevel@tonic-gate bcopy(option, optval, size);
55287c478bd9Sstevel@tonic-gate bcopy(&size, optlenp, sizeof (size));
55297c478bd9Sstevel@tonic-gate }
55307c478bd9Sstevel@tonic-gate done:
55317c478bd9Sstevel@tonic-gate freemsg(mp);
55327c478bd9Sstevel@tonic-gate done2:
55337c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
55347c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
55350f1702c5SYu Xiangning
55367c478bd9Sstevel@tonic-gate return (error);
55377c478bd9Sstevel@tonic-gate }
55387c478bd9Sstevel@tonic-gate
55397c478bd9Sstevel@tonic-gate /*
55407c478bd9Sstevel@tonic-gate * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
55417c478bd9Sstevel@tonic-gate * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
55427c478bd9Sstevel@tonic-gate * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
55437c478bd9Sstevel@tonic-gate * setsockopt has to work even if the transport does not support the option.
55447c478bd9Sstevel@tonic-gate */
55450f1702c5SYu Xiangning /* ARGSUSED */
55467c478bd9Sstevel@tonic-gate int
sotpi_setsockopt(struct sonode * so,int level,int option_name,const void * optval,t_uscalar_t optlen,struct cred * cr)55477c478bd9Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name,
55480f1702c5SYu Xiangning const void *optval, t_uscalar_t optlen, struct cred *cr)
55497c478bd9Sstevel@tonic-gate {
55507c478bd9Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req;
55517c478bd9Sstevel@tonic-gate struct opthdr oh;
55527c478bd9Sstevel@tonic-gate mblk_t *mp;
55537c478bd9Sstevel@tonic-gate int error = 0;
55547c478bd9Sstevel@tonic-gate boolean_t handled = B_FALSE;
55557c478bd9Sstevel@tonic-gate
55567c478bd9Sstevel@tonic-gate dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
5557903a11ebSrh87107 (void *)so, level, option_name, optval, optlen,
55587c478bd9Sstevel@tonic-gate pr_state(so->so_state, so->so_mode)));
55597c478bd9Sstevel@tonic-gate
55607c478bd9Sstevel@tonic-gate /* X/Open requires this check */
55617c478bd9Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
55627c478bd9Sstevel@tonic-gate if (xnet_check_print)
55637c478bd9Sstevel@tonic-gate printf("sockfs: X/Open setsockopt check => EINVAL\n");
55647c478bd9Sstevel@tonic-gate return (EINVAL);
55657c478bd9Sstevel@tonic-gate }
55667c478bd9Sstevel@tonic-gate
55677c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
55687c478bd9Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */
55697c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
55707c478bd9Sstevel@tonic-gate
55717c478bd9Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
55727c478bd9Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_NEGOTIATE;
55737c478bd9Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
55747c478bd9Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
55757c478bd9Sstevel@tonic-gate
55767c478bd9Sstevel@tonic-gate oh.level = level;
55777c478bd9Sstevel@tonic-gate oh.name = option_name;
55787c478bd9Sstevel@tonic-gate oh.len = optlen;
55797c478bd9Sstevel@tonic-gate
55807c478bd9Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5581de8c4a14SErik Nordmark &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP, cr);
55827c478bd9Sstevel@tonic-gate /* Let option management work in the presence of data flow control */
55837c478bd9Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
55847c478bd9Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
55857c478bd9Sstevel@tonic-gate mp = NULL;
55867c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock);
55877c478bd9Sstevel@tonic-gate if (error) {
55887c478bd9Sstevel@tonic-gate eprintsoline(so, error);
55890f1702c5SYu Xiangning goto done2;
55907c478bd9Sstevel@tonic-gate }
55917c478bd9Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
55927c478bd9Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
55937c478bd9Sstevel@tonic-gate if (error) {
55947c478bd9Sstevel@tonic-gate eprintsoline(so, error);
55957c478bd9Sstevel@tonic-gate goto done;
55967c478bd9Sstevel@tonic-gate }
55977c478bd9Sstevel@tonic-gate ASSERT(mp);
55987c478bd9Sstevel@tonic-gate /* No need to verify T_optmgmt_ack */
55997c478bd9Sstevel@tonic-gate freemsg(mp);
56007c478bd9Sstevel@tonic-gate done:
56017c478bd9Sstevel@tonic-gate /*
56027c478bd9Sstevel@tonic-gate * Check for SOL_SOCKET options and record their values.
56037c478bd9Sstevel@tonic-gate * If we know about a SOL_SOCKET parameter and the transport
56047c478bd9Sstevel@tonic-gate * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
56057c478bd9Sstevel@tonic-gate * EPROTO) we let the setsockopt succeed.
56067c478bd9Sstevel@tonic-gate */
56077c478bd9Sstevel@tonic-gate if (level == SOL_SOCKET) {
56087c478bd9Sstevel@tonic-gate /* Check parameters */
56097c478bd9Sstevel@tonic-gate switch (option_name) {
56107c478bd9Sstevel@tonic-gate case SO_DEBUG:
56117c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
561278918900SArne Jansen case SO_REUSEPORT:
56137c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
56147c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
56157c478bd9Sstevel@tonic-gate case SO_BROADCAST:
56167c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
56177c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
56187c478bd9Sstevel@tonic-gate case SO_SNDBUF:
56197c478bd9Sstevel@tonic-gate case SO_RCVBUF:
56207c478bd9Sstevel@tonic-gate #ifdef notyet
56217c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
56227c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
56237c478bd9Sstevel@tonic-gate #endif /* notyet */
56247c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
56257c478bd9Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) {
56267c478bd9Sstevel@tonic-gate error = EINVAL;
56277c478bd9Sstevel@tonic-gate eprintsoline(so, error);
56287c478bd9Sstevel@tonic-gate goto done2;
56297c478bd9Sstevel@tonic-gate }
56307c478bd9Sstevel@tonic-gate ASSERT(optval);
56317c478bd9Sstevel@tonic-gate handled = B_TRUE;
56327c478bd9Sstevel@tonic-gate break;
56330f1702c5SYu Xiangning case SO_SNDTIMEO:
56340f1702c5SYu Xiangning case SO_RCVTIMEO:
5635e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5636e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE) {
563722238f73Sshenjian if (optlen != sizeof (struct timeval)) {
56380f1702c5SYu Xiangning error = EINVAL;
56390f1702c5SYu Xiangning eprintsoline(so, error);
56400f1702c5SYu Xiangning goto done2;
56410f1702c5SYu Xiangning }
564222238f73Sshenjian } else {
564322238f73Sshenjian if (optlen != sizeof (struct timeval32)) {
564422238f73Sshenjian error = EINVAL;
564522238f73Sshenjian eprintsoline(so, error);
564622238f73Sshenjian goto done2;
564722238f73Sshenjian }
564822238f73Sshenjian }
56490f1702c5SYu Xiangning ASSERT(optval);
56500f1702c5SYu Xiangning handled = B_TRUE;
56510f1702c5SYu Xiangning break;
56527c478bd9Sstevel@tonic-gate case SO_LINGER:
56537c478bd9Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (struct linger)) {
56547c478bd9Sstevel@tonic-gate error = EINVAL;
56557c478bd9Sstevel@tonic-gate eprintsoline(so, error);
56567c478bd9Sstevel@tonic-gate goto done2;
56577c478bd9Sstevel@tonic-gate }
56587c478bd9Sstevel@tonic-gate ASSERT(optval);
56597c478bd9Sstevel@tonic-gate handled = B_TRUE;
56607c478bd9Sstevel@tonic-gate break;
56617c478bd9Sstevel@tonic-gate }
56627c478bd9Sstevel@tonic-gate
56637c478bd9Sstevel@tonic-gate #define intvalue (*(int32_t *)optval)
56647c478bd9Sstevel@tonic-gate
56657c478bd9Sstevel@tonic-gate switch (option_name) {
56667c478bd9Sstevel@tonic-gate case SO_TYPE:
56677c478bd9Sstevel@tonic-gate case SO_ERROR:
56687c478bd9Sstevel@tonic-gate case SO_ACCEPTCONN:
56697c478bd9Sstevel@tonic-gate /* Can't be set */
56707c478bd9Sstevel@tonic-gate error = ENOPROTOOPT;
56717c478bd9Sstevel@tonic-gate goto done2;
56727c478bd9Sstevel@tonic-gate case SO_LINGER: {
56737c478bd9Sstevel@tonic-gate struct linger *l = (struct linger *)optval;
56747c478bd9Sstevel@tonic-gate
56757c478bd9Sstevel@tonic-gate so->so_linger.l_linger = l->l_linger;
56767c478bd9Sstevel@tonic-gate if (l->l_onoff) {
56777c478bd9Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER;
56787c478bd9Sstevel@tonic-gate so->so_options |= SO_LINGER;
56797c478bd9Sstevel@tonic-gate } else {
56807c478bd9Sstevel@tonic-gate so->so_linger.l_onoff = 0;
56817c478bd9Sstevel@tonic-gate so->so_options &= ~SO_LINGER;
56827c478bd9Sstevel@tonic-gate }
56837c478bd9Sstevel@tonic-gate break;
56847c478bd9Sstevel@tonic-gate }
56857c478bd9Sstevel@tonic-gate
56867c478bd9Sstevel@tonic-gate case SO_DEBUG:
56877c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
56887c478bd9Sstevel@tonic-gate if (intvalue & 2)
56897c478bd9Sstevel@tonic-gate sock_test_timelimit = 10 * hz;
56907c478bd9Sstevel@tonic-gate else
56917c478bd9Sstevel@tonic-gate sock_test_timelimit = 0;
56927c478bd9Sstevel@tonic-gate
56937c478bd9Sstevel@tonic-gate if (intvalue & 4)
56947c478bd9Sstevel@tonic-gate do_useracc = 0;
56957c478bd9Sstevel@tonic-gate else
56967c478bd9Sstevel@tonic-gate do_useracc = 1;
56977c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
56987c478bd9Sstevel@tonic-gate /* FALLTHRU */
56997c478bd9Sstevel@tonic-gate case SO_REUSEADDR:
570078918900SArne Jansen case SO_REUSEPORT:
57017c478bd9Sstevel@tonic-gate case SO_KEEPALIVE:
57027c478bd9Sstevel@tonic-gate case SO_DONTROUTE:
57037c478bd9Sstevel@tonic-gate case SO_BROADCAST:
57047c478bd9Sstevel@tonic-gate case SO_USELOOPBACK:
57057c478bd9Sstevel@tonic-gate case SO_OOBINLINE:
57067c478bd9Sstevel@tonic-gate case SO_DGRAM_ERRIND:
57077c478bd9Sstevel@tonic-gate if (intvalue != 0) {
57087c478bd9Sstevel@tonic-gate dprintso(so, 1,
57090f1702c5SYu Xiangning ("socket_setsockopt: setting 0x%x\n",
57107c478bd9Sstevel@tonic-gate option_name));
57117c478bd9Sstevel@tonic-gate so->so_options |= option_name;
57127c478bd9Sstevel@tonic-gate } else {
57137c478bd9Sstevel@tonic-gate dprintso(so, 1,
57140f1702c5SYu Xiangning ("socket_setsockopt: clearing 0x%x\n",
57157c478bd9Sstevel@tonic-gate option_name));
57167c478bd9Sstevel@tonic-gate so->so_options &= ~option_name;
57177c478bd9Sstevel@tonic-gate }
57187c478bd9Sstevel@tonic-gate break;
57197c478bd9Sstevel@tonic-gate /*
57207c478bd9Sstevel@tonic-gate * The following options are only returned by us when the
57210f1702c5SYu Xiangning * transport layer fails.
57227c478bd9Sstevel@tonic-gate * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
57237c478bd9Sstevel@tonic-gate * since the transport might adjust the value and not
57247c478bd9Sstevel@tonic-gate * return exactly what was set by the application.
57257c478bd9Sstevel@tonic-gate */
57267c478bd9Sstevel@tonic-gate case SO_SNDBUF:
57277c478bd9Sstevel@tonic-gate so->so_sndbuf = intvalue;
57287c478bd9Sstevel@tonic-gate break;
57297c478bd9Sstevel@tonic-gate case SO_RCVBUF:
57307c478bd9Sstevel@tonic-gate so->so_rcvbuf = intvalue;
57317c478bd9Sstevel@tonic-gate break;
57320f1702c5SYu Xiangning case SO_RCVPSH:
57330f1702c5SYu Xiangning so->so_rcv_timer_interval = intvalue;
57340f1702c5SYu Xiangning break;
57357c478bd9Sstevel@tonic-gate #ifdef notyet
57367c478bd9Sstevel@tonic-gate /*
57377c478bd9Sstevel@tonic-gate * We do not implement the semantics of these options
57387c478bd9Sstevel@tonic-gate * thus we shouldn't implement the options either.
57397c478bd9Sstevel@tonic-gate */
57407c478bd9Sstevel@tonic-gate case SO_SNDLOWAT:
57417c478bd9Sstevel@tonic-gate so->so_sndlowat = intvalue;
57427c478bd9Sstevel@tonic-gate break;
57437c478bd9Sstevel@tonic-gate case SO_RCVLOWAT:
57447c478bd9Sstevel@tonic-gate so->so_rcvlowat = intvalue;
57457c478bd9Sstevel@tonic-gate break;
57467c478bd9Sstevel@tonic-gate #endif /* notyet */
57470f1702c5SYu Xiangning case SO_SNDTIMEO:
57480f1702c5SYu Xiangning case SO_RCVTIMEO: {
574922238f73Sshenjian struct timeval tl;
575022238f73Sshenjian clock_t val;
575122238f73Sshenjian
5752e5083e81Sshenjian if (get_udatamodel() == DATAMODEL_NONE ||
5753e5083e81Sshenjian get_udatamodel() == DATAMODEL_NATIVE)
575422238f73Sshenjian bcopy(&tl, (struct timeval *)optval,
575522238f73Sshenjian sizeof (struct timeval));
575622238f73Sshenjian else
575722238f73Sshenjian TIMEVAL32_TO_TIMEVAL(&tl,
575822238f73Sshenjian (struct timeval32 *)optval);
575922238f73Sshenjian val = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
57600f1702c5SYu Xiangning if (option_name == SO_RCVTIMEO)
57610f1702c5SYu Xiangning so->so_rcvtimeo = drv_usectohz(val);
57620f1702c5SYu Xiangning else
57630f1702c5SYu Xiangning so->so_sndtimeo = drv_usectohz(val);
57640f1702c5SYu Xiangning break;
57650f1702c5SYu Xiangning }
57667c478bd9Sstevel@tonic-gate }
57677c478bd9Sstevel@tonic-gate #undef intvalue
57687c478bd9Sstevel@tonic-gate
57697c478bd9Sstevel@tonic-gate if (error) {
57707c478bd9Sstevel@tonic-gate if ((error == ENOPROTOOPT || error == EPROTO ||
57717c478bd9Sstevel@tonic-gate error == EINVAL) && handled) {
57727c478bd9Sstevel@tonic-gate dprintso(so, 1,
57737c478bd9Sstevel@tonic-gate ("setsockopt: ignoring error %d for 0x%x\n",
57747c478bd9Sstevel@tonic-gate error, option_name));
57757c478bd9Sstevel@tonic-gate error = 0;
57767c478bd9Sstevel@tonic-gate }
57777c478bd9Sstevel@tonic-gate }
57787c478bd9Sstevel@tonic-gate }
57797c478bd9Sstevel@tonic-gate done2:
57807c478bd9Sstevel@tonic-gate so_unlock_single(so, SOLOCKED);
57817c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock);
57827c478bd9Sstevel@tonic-gate return (error);
57837c478bd9Sstevel@tonic-gate }
57840f1702c5SYu Xiangning
5785f0267584Sanders /*
5786f0267584Sanders * sotpi_close() is called when the last open reference goes away.
5787f0267584Sanders */
57880f1702c5SYu Xiangning /* ARGSUSED */
57890f1702c5SYu Xiangning int
sotpi_close(struct sonode * so,int flag,struct cred * cr)57900f1702c5SYu Xiangning sotpi_close(struct sonode *so, int flag, struct cred *cr)
57910f1702c5SYu Xiangning {
57920f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
57930f1702c5SYu Xiangning dev_t dev;
57940f1702c5SYu Xiangning int error = 0;
57950f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
57960f1702c5SYu Xiangning
57970f1702c5SYu Xiangning dprintso(so, 1, ("sotpi_close(%p, %x) %s\n",
57980f1702c5SYu Xiangning (void *)vp, flag, pr_state(so->so_state, so->so_mode)));
57990f1702c5SYu Xiangning
58000f1702c5SYu Xiangning dev = sti->sti_dev;
58010f1702c5SYu Xiangning
58020f1702c5SYu Xiangning ASSERT(STREAMSTAB(getmajor(dev)));
58030f1702c5SYu Xiangning
58040f1702c5SYu Xiangning mutex_enter(&so->so_lock);
58050f1702c5SYu Xiangning so_lock_single(so); /* Set SOLOCKED */
58060f1702c5SYu Xiangning
5807d36be52eSRao Shoaib ASSERT(so_verify_oobstate(so));
5808d36be52eSRao Shoaib
58090f1702c5SYu Xiangning if (sti->sti_nl7c_flags & NL7C_ENABLED) {
58100f1702c5SYu Xiangning sti->sti_nl7c_flags = 0;
58110f1702c5SYu Xiangning nl7c_close(so);
58120f1702c5SYu Xiangning }
58130f1702c5SYu Xiangning
58140f1702c5SYu Xiangning if (vp->v_stream != NULL) {
58150f1702c5SYu Xiangning vnode_t *ux_vp;
58160f1702c5SYu Xiangning
58170f1702c5SYu Xiangning if (so->so_family == AF_UNIX) {
58180f1702c5SYu Xiangning /* Could avoid this when CANTSENDMORE for !dgram */
58190f1702c5SYu Xiangning so_unix_close(so);
58200f1702c5SYu Xiangning }
58210f1702c5SYu Xiangning
58220f1702c5SYu Xiangning mutex_exit(&so->so_lock);
58230f1702c5SYu Xiangning /*
58240f1702c5SYu Xiangning * Disassemble the linkage from the AF_UNIX underlying file
58250f1702c5SYu Xiangning * system vnode to this socket (by atomically clearing
58260f1702c5SYu Xiangning * v_stream in vn_rele_stream) before strclose clears sd_vnode
58270f1702c5SYu Xiangning * and frees the stream head.
58280f1702c5SYu Xiangning */
58290f1702c5SYu Xiangning if ((ux_vp = sti->sti_ux_bound_vp) != NULL) {
58300f1702c5SYu Xiangning ASSERT(ux_vp->v_stream);
58310f1702c5SYu Xiangning sti->sti_ux_bound_vp = NULL;
58320f1702c5SYu Xiangning vn_rele_stream(ux_vp);
58330f1702c5SYu Xiangning }
58340f1702c5SYu Xiangning error = strclose(vp, flag, cr);
58350f1702c5SYu Xiangning vp->v_stream = NULL;
58360f1702c5SYu Xiangning mutex_enter(&so->so_lock);
58370f1702c5SYu Xiangning }
58380f1702c5SYu Xiangning
58390f1702c5SYu Xiangning /*
58400f1702c5SYu Xiangning * Flush the T_DISCON_IND on sti_discon_ind_mp.
58410f1702c5SYu Xiangning */
58420f1702c5SYu Xiangning so_flush_discon_ind(so);
58430f1702c5SYu Xiangning
58440f1702c5SYu Xiangning so_unlock_single(so, SOLOCKED);
58450f1702c5SYu Xiangning mutex_exit(&so->so_lock);
58460f1702c5SYu Xiangning
58470f1702c5SYu Xiangning /*
58480f1702c5SYu Xiangning * Needed for STREAMs.
58490f1702c5SYu Xiangning * Decrement the device driver's reference count for streams
58500f1702c5SYu Xiangning * opened via the clone dip. The driver was held in clone_open().
58510f1702c5SYu Xiangning * The absence of clone_close() forces this asymmetry.
58520f1702c5SYu Xiangning */
58530f1702c5SYu Xiangning if (so->so_flag & SOCLONE)
58540f1702c5SYu Xiangning ddi_rele_driver(getmajor(dev));
58550f1702c5SYu Xiangning
58560f1702c5SYu Xiangning return (error);
58570f1702c5SYu Xiangning }
58580f1702c5SYu Xiangning
58590f1702c5SYu Xiangning static int
sotpi_ioctl(struct sonode * so,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)58600f1702c5SYu Xiangning sotpi_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
58610f1702c5SYu Xiangning struct cred *cr, int32_t *rvalp)
58620f1702c5SYu Xiangning {
58630f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
58640f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
58650f1702c5SYu Xiangning int error = 0;
58660f1702c5SYu Xiangning
58670f1702c5SYu Xiangning dprintso(so, 0, ("sotpi_ioctl: cmd 0x%x, arg 0x%lx, state %s\n",
58680f1702c5SYu Xiangning cmd, arg, pr_state(so->so_state, so->so_mode)));
58690f1702c5SYu Xiangning
58700f1702c5SYu Xiangning switch (cmd) {
5871bfcb55b8SRao Shoaib case SIOCSQPTR:
5872bfcb55b8SRao Shoaib /*
5873bfcb55b8SRao Shoaib * SIOCSQPTR is valid only when helper stream is created
5874bfcb55b8SRao Shoaib * by the protocol.
5875bfcb55b8SRao Shoaib */
58760f1702c5SYu Xiangning case _I_INSERT:
58770f1702c5SYu Xiangning case _I_REMOVE:
58780f1702c5SYu Xiangning /*
58790f1702c5SYu Xiangning * Since there's no compelling reason to support these ioctls
58800f1702c5SYu Xiangning * on sockets, and doing so would increase the complexity
58810f1702c5SYu Xiangning * markedly, prevent it.
58820f1702c5SYu Xiangning */
58830f1702c5SYu Xiangning return (EOPNOTSUPP);
58840f1702c5SYu Xiangning
58850f1702c5SYu Xiangning case I_FIND:
58860f1702c5SYu Xiangning case I_LIST:
58870f1702c5SYu Xiangning case I_LOOK:
58880f1702c5SYu Xiangning case I_POP:
58890f1702c5SYu Xiangning case I_PUSH:
58900f1702c5SYu Xiangning /*
58910f1702c5SYu Xiangning * To prevent races and inconsistencies between the actual
58920f1702c5SYu Xiangning * state of the stream and the state according to the sonode,
58930f1702c5SYu Xiangning * we serialize all operations which modify or operate on the
58940f1702c5SYu Xiangning * list of modules on the socket's stream.
58950f1702c5SYu Xiangning */
58960f1702c5SYu Xiangning mutex_enter(&sti->sti_plumb_lock);
58970f1702c5SYu Xiangning error = socktpi_plumbioctl(vp, cmd, arg, mode, cr, rvalp);
58980f1702c5SYu Xiangning mutex_exit(&sti->sti_plumb_lock);
58990f1702c5SYu Xiangning return (error);
59000f1702c5SYu Xiangning
59010f1702c5SYu Xiangning default:
59020f1702c5SYu Xiangning if (so->so_version != SOV_STREAM)
59030f1702c5SYu Xiangning break;
59040f1702c5SYu Xiangning
59050f1702c5SYu Xiangning /*
59060f1702c5SYu Xiangning * The imaginary "sockmod" has been popped; act as a stream.
59070f1702c5SYu Xiangning */
59080f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
59090f1702c5SYu Xiangning }
59100f1702c5SYu Xiangning
59110f1702c5SYu Xiangning ASSERT(so->so_version != SOV_STREAM);
59120f1702c5SYu Xiangning
59130f1702c5SYu Xiangning /*
59140f1702c5SYu Xiangning * Process socket-specific ioctls.
59150f1702c5SYu Xiangning */
59160f1702c5SYu Xiangning switch (cmd) {
59170f1702c5SYu Xiangning case FIONBIO: {
59180f1702c5SYu Xiangning int32_t value;
59190f1702c5SYu Xiangning
59200f1702c5SYu Xiangning if (so_copyin((void *)arg, &value, sizeof (int32_t),
59210f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
59220f1702c5SYu Xiangning return (EFAULT);
59230f1702c5SYu Xiangning
59240f1702c5SYu Xiangning mutex_enter(&so->so_lock);
59250f1702c5SYu Xiangning if (value) {
59260f1702c5SYu Xiangning so->so_state |= SS_NDELAY;
59270f1702c5SYu Xiangning } else {
59280f1702c5SYu Xiangning so->so_state &= ~SS_NDELAY;
59290f1702c5SYu Xiangning }
59300f1702c5SYu Xiangning mutex_exit(&so->so_lock);
59310f1702c5SYu Xiangning return (0);
59320f1702c5SYu Xiangning }
59330f1702c5SYu Xiangning
59340f1702c5SYu Xiangning case FIOASYNC: {
59350f1702c5SYu Xiangning int32_t value;
59360f1702c5SYu Xiangning
59370f1702c5SYu Xiangning if (so_copyin((void *)arg, &value, sizeof (int32_t),
59380f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
59390f1702c5SYu Xiangning return (EFAULT);
59400f1702c5SYu Xiangning
59410f1702c5SYu Xiangning mutex_enter(&so->so_lock);
59420f1702c5SYu Xiangning /*
59430f1702c5SYu Xiangning * SS_ASYNC flag not already set correctly?
59440f1702c5SYu Xiangning * (!value != !(so->so_state & SS_ASYNC))
59450f1702c5SYu Xiangning * but some engineers find that too hard to read.
59460f1702c5SYu Xiangning */
59470f1702c5SYu Xiangning if (value == 0 && (so->so_state & SS_ASYNC) != 0 ||
59480f1702c5SYu Xiangning value != 0 && (so->so_state & SS_ASYNC) == 0)
59490f1702c5SYu Xiangning error = so_flip_async(so, vp, mode, cr);
59500f1702c5SYu Xiangning mutex_exit(&so->so_lock);
59510f1702c5SYu Xiangning return (error);
59520f1702c5SYu Xiangning }
59530f1702c5SYu Xiangning
59540f1702c5SYu Xiangning case SIOCSPGRP:
59550f1702c5SYu Xiangning case FIOSETOWN: {
59560f1702c5SYu Xiangning pid_t pgrp;
59570f1702c5SYu Xiangning
59580f1702c5SYu Xiangning if (so_copyin((void *)arg, &pgrp, sizeof (pid_t),
59590f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
59600f1702c5SYu Xiangning return (EFAULT);
59610f1702c5SYu Xiangning
59620f1702c5SYu Xiangning mutex_enter(&so->so_lock);
59630f1702c5SYu Xiangning dprintso(so, 1, ("setown: new %d old %d\n", pgrp, so->so_pgrp));
59640f1702c5SYu Xiangning /* Any change? */
59650f1702c5SYu Xiangning if (pgrp != so->so_pgrp)
59660f1702c5SYu Xiangning error = so_set_siggrp(so, vp, pgrp, mode, cr);
59670f1702c5SYu Xiangning mutex_exit(&so->so_lock);
59680f1702c5SYu Xiangning return (error);
59690f1702c5SYu Xiangning }
59700f1702c5SYu Xiangning case SIOCGPGRP:
59710f1702c5SYu Xiangning case FIOGETOWN:
59720f1702c5SYu Xiangning if (so_copyout(&so->so_pgrp, (void *)arg,
59730f1702c5SYu Xiangning sizeof (pid_t), (mode & (int)FKIOCTL)))
59740f1702c5SYu Xiangning return (EFAULT);
59750f1702c5SYu Xiangning return (0);
59760f1702c5SYu Xiangning
59770f1702c5SYu Xiangning case SIOCATMARK: {
59780f1702c5SYu Xiangning int retval;
59790f1702c5SYu Xiangning uint_t so_state;
59800f1702c5SYu Xiangning
59810f1702c5SYu Xiangning /*
59820f1702c5SYu Xiangning * strwaitmark has a finite timeout after which it
59830f1702c5SYu Xiangning * returns -1 if the mark state is undetermined.
59840f1702c5SYu Xiangning * In order to avoid any race between the mark state
59850f1702c5SYu Xiangning * in sockfs and the mark state in the stream head this
59860f1702c5SYu Xiangning * routine loops until the mark state can be determined
59870f1702c5SYu Xiangning * (or the urgent data indication has been removed by some
59880f1702c5SYu Xiangning * other thread).
59890f1702c5SYu Xiangning */
59900f1702c5SYu Xiangning do {
59910f1702c5SYu Xiangning mutex_enter(&so->so_lock);
59920f1702c5SYu Xiangning so_state = so->so_state;
59930f1702c5SYu Xiangning mutex_exit(&so->so_lock);
59940f1702c5SYu Xiangning if (so_state & SS_RCVATMARK) {
59950f1702c5SYu Xiangning retval = 1;
59960f1702c5SYu Xiangning } else if (!(so_state & SS_OOBPEND)) {
59970f1702c5SYu Xiangning /*
59980f1702c5SYu Xiangning * No SIGURG has been generated -- there is no
59990f1702c5SYu Xiangning * pending or present urgent data. Thus can't
60000f1702c5SYu Xiangning * possibly be at the mark.
60010f1702c5SYu Xiangning */
60020f1702c5SYu Xiangning retval = 0;
60030f1702c5SYu Xiangning } else {
60040f1702c5SYu Xiangning /*
60050f1702c5SYu Xiangning * Have the stream head wait until there is
60060f1702c5SYu Xiangning * either some messages on the read queue, or
60070f1702c5SYu Xiangning * STRATMARK or STRNOTATMARK gets set. The
60080f1702c5SYu Xiangning * STRNOTATMARK flag is used so that the
60090f1702c5SYu Xiangning * transport can send up a MSGNOTMARKNEXT
60100f1702c5SYu Xiangning * M_DATA to indicate that it is not
60110f1702c5SYu Xiangning * at the mark and additional data is not about
60120f1702c5SYu Xiangning * to be send upstream.
60130f1702c5SYu Xiangning *
60140f1702c5SYu Xiangning * If the mark state is undetermined this will
60150f1702c5SYu Xiangning * return -1 and we will loop rechecking the
60160f1702c5SYu Xiangning * socket state.
60170f1702c5SYu Xiangning */
60180f1702c5SYu Xiangning retval = strwaitmark(vp);
60190f1702c5SYu Xiangning }
60200f1702c5SYu Xiangning } while (retval == -1);
60210f1702c5SYu Xiangning
60220f1702c5SYu Xiangning if (so_copyout(&retval, (void *)arg, sizeof (int),
60230f1702c5SYu Xiangning (mode & (int)FKIOCTL)))
60240f1702c5SYu Xiangning return (EFAULT);
60250f1702c5SYu Xiangning return (0);
60260f1702c5SYu Xiangning }
60270f1702c5SYu Xiangning
60280f1702c5SYu Xiangning case I_FDINSERT:
60290f1702c5SYu Xiangning case I_SENDFD:
60300f1702c5SYu Xiangning case I_RECVFD:
60310f1702c5SYu Xiangning case I_ATMARK:
60320f1702c5SYu Xiangning case _SIOCSOCKFALLBACK:
60330f1702c5SYu Xiangning /*
60340f1702c5SYu Xiangning * These ioctls do not apply to sockets. I_FDINSERT can be
60350f1702c5SYu Xiangning * used to send M_PROTO messages without modifying the socket
60360f1702c5SYu Xiangning * state. I_SENDFD/RECVFD should not be used for socket file
60370f1702c5SYu Xiangning * descriptor passing since they assume a twisted stream.
60380f1702c5SYu Xiangning * SIOCATMARK must be used instead of I_ATMARK.
60390f1702c5SYu Xiangning *
60400f1702c5SYu Xiangning * _SIOCSOCKFALLBACK from an application should never be
60410f1702c5SYu Xiangning * processed. It is only generated by socktpi_open() or
60420f1702c5SYu Xiangning * in response to I_POP or I_PUSH.
60430f1702c5SYu Xiangning */
60440f1702c5SYu Xiangning #ifdef DEBUG
60450f1702c5SYu Xiangning zcmn_err(getzoneid(), CE_WARN,
60460f1702c5SYu Xiangning "Unsupported STREAMS ioctl 0x%x on socket. "
60470f1702c5SYu Xiangning "Pid = %d\n", cmd, curproc->p_pid);
60480f1702c5SYu Xiangning #endif /* DEBUG */
60490f1702c5SYu Xiangning return (EOPNOTSUPP);
60500f1702c5SYu Xiangning
60510f1702c5SYu Xiangning case _I_GETPEERCRED:
60520f1702c5SYu Xiangning if ((mode & FKIOCTL) == 0)
60530f1702c5SYu Xiangning return (EINVAL);
60540f1702c5SYu Xiangning
60550f1702c5SYu Xiangning mutex_enter(&so->so_lock);
60560f1702c5SYu Xiangning if ((so->so_mode & SM_CONNREQUIRED) == 0) {
60570f1702c5SYu Xiangning error = ENOTSUP;
60580f1702c5SYu Xiangning } else if ((so->so_state & SS_ISCONNECTED) == 0) {
60590f1702c5SYu Xiangning error = ENOTCONN;
60600f1702c5SYu Xiangning } else if (so->so_peercred != NULL) {
60610f1702c5SYu Xiangning k_peercred_t *kp = (k_peercred_t *)arg;
60620f1702c5SYu Xiangning kp->pc_cr = so->so_peercred;
60630f1702c5SYu Xiangning kp->pc_cpid = so->so_cpid;
60640f1702c5SYu Xiangning crhold(so->so_peercred);
60650f1702c5SYu Xiangning } else {
60660f1702c5SYu Xiangning error = EINVAL;
60670f1702c5SYu Xiangning }
60680f1702c5SYu Xiangning mutex_exit(&so->so_lock);
60690f1702c5SYu Xiangning return (error);
60700f1702c5SYu Xiangning
60710f1702c5SYu Xiangning default:
60720f1702c5SYu Xiangning /*
60730f1702c5SYu Xiangning * Do the higher-order bits of the ioctl cmd indicate
60740f1702c5SYu Xiangning * that it is an I_* streams ioctl?
60750f1702c5SYu Xiangning */
60760f1702c5SYu Xiangning if ((cmd & 0xffffff00U) == STR &&
60770f1702c5SYu Xiangning so->so_version == SOV_SOCKBSD) {
60780f1702c5SYu Xiangning #ifdef DEBUG
60790f1702c5SYu Xiangning zcmn_err(getzoneid(), CE_WARN,
60800f1702c5SYu Xiangning "Unsupported STREAMS ioctl 0x%x on socket. "
60810f1702c5SYu Xiangning "Pid = %d\n", cmd, curproc->p_pid);
60820f1702c5SYu Xiangning #endif /* DEBUG */
60830f1702c5SYu Xiangning return (EOPNOTSUPP);
60840f1702c5SYu Xiangning }
60850f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
60860f1702c5SYu Xiangning }
60870f1702c5SYu Xiangning }
60880f1702c5SYu Xiangning
60890f1702c5SYu Xiangning /*
60900f1702c5SYu Xiangning * Handle plumbing-related ioctls.
60910f1702c5SYu Xiangning */
60920f1702c5SYu Xiangning static int
socktpi_plumbioctl(struct vnode * vp,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)60930f1702c5SYu Xiangning socktpi_plumbioctl(struct vnode *vp, int cmd, intptr_t arg, int mode,
60940f1702c5SYu Xiangning struct cred *cr, int32_t *rvalp)
60950f1702c5SYu Xiangning {
60960f1702c5SYu Xiangning static const char sockmod_name[] = "sockmod";
60970f1702c5SYu Xiangning struct sonode *so = VTOSO(vp);
60980f1702c5SYu Xiangning char mname[FMNAMESZ + 1];
60990f1702c5SYu Xiangning int error;
61000f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
61010f1702c5SYu Xiangning
61020f1702c5SYu Xiangning ASSERT(MUTEX_HELD(&sti->sti_plumb_lock));
61030f1702c5SYu Xiangning
61040f1702c5SYu Xiangning if (so->so_version == SOV_SOCKBSD)
61050f1702c5SYu Xiangning return (EOPNOTSUPP);
61060f1702c5SYu Xiangning
61070f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
61080f1702c5SYu Xiangning /*
61090f1702c5SYu Xiangning * The imaginary "sockmod" has been popped - act as a stream.
61100f1702c5SYu Xiangning * If this is a push of sockmod then change back to a socket.
61110f1702c5SYu Xiangning */
61120f1702c5SYu Xiangning if (cmd == I_PUSH) {
61130f1702c5SYu Xiangning error = ((mode & FKIOCTL) ? copystr : copyinstr)(
61140f1702c5SYu Xiangning (void *)arg, mname, sizeof (mname), NULL);
61150f1702c5SYu Xiangning
61160f1702c5SYu Xiangning if (error == 0 && strcmp(mname, sockmod_name) == 0) {
61170f1702c5SYu Xiangning dprintso(so, 0, ("socktpi_ioctl: going to "
61180f1702c5SYu Xiangning "socket version\n"));
61190f1702c5SYu Xiangning so_stream2sock(so);
61200f1702c5SYu Xiangning return (0);
61210f1702c5SYu Xiangning }
61220f1702c5SYu Xiangning }
61230f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
61240f1702c5SYu Xiangning }
61250f1702c5SYu Xiangning
61260f1702c5SYu Xiangning switch (cmd) {
61270f1702c5SYu Xiangning case I_PUSH:
61280f1702c5SYu Xiangning if (sti->sti_direct) {
61290f1702c5SYu Xiangning mutex_enter(&so->so_lock);
61300f1702c5SYu Xiangning so_lock_single(so);
61310f1702c5SYu Xiangning mutex_exit(&so->so_lock);
61320f1702c5SYu Xiangning
61330f1702c5SYu Xiangning error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
6134de8c4a14SErik Nordmark cr, rvalp);
61350f1702c5SYu Xiangning
61360f1702c5SYu Xiangning mutex_enter(&so->so_lock);
61370f1702c5SYu Xiangning if (error == 0)
61380f1702c5SYu Xiangning sti->sti_direct = 0;
61390f1702c5SYu Xiangning so_unlock_single(so, SOLOCKED);
61400f1702c5SYu Xiangning mutex_exit(&so->so_lock);
61410f1702c5SYu Xiangning
61420f1702c5SYu Xiangning if (error != 0)
61430f1702c5SYu Xiangning return (error);
61440f1702c5SYu Xiangning }
61450f1702c5SYu Xiangning
61460f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
61470f1702c5SYu Xiangning if (error == 0)
61480f1702c5SYu Xiangning sti->sti_pushcnt++;
61490f1702c5SYu Xiangning return (error);
61500f1702c5SYu Xiangning
61510f1702c5SYu Xiangning case I_POP:
61520f1702c5SYu Xiangning if (sti->sti_pushcnt == 0) {
61530f1702c5SYu Xiangning /* Emulate sockmod being popped */
61540f1702c5SYu Xiangning dprintso(so, 0,
61550f1702c5SYu Xiangning ("socktpi_ioctl: going to STREAMS version\n"));
61560f1702c5SYu Xiangning return (so_sock2stream(so));
61570f1702c5SYu Xiangning }
61580f1702c5SYu Xiangning
61590f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
61600f1702c5SYu Xiangning if (error == 0)
61610f1702c5SYu Xiangning sti->sti_pushcnt--;
61620f1702c5SYu Xiangning return (error);
61630f1702c5SYu Xiangning
61640f1702c5SYu Xiangning case I_LIST: {
61650f1702c5SYu Xiangning struct str_mlist *kmlistp, *umlistp;
61660f1702c5SYu Xiangning struct str_list kstrlist;
61670f1702c5SYu Xiangning ssize_t kstrlistsize;
61680f1702c5SYu Xiangning int i, nmods;
61690f1702c5SYu Xiangning
61700f1702c5SYu Xiangning STRUCT_DECL(str_list, ustrlist);
61710f1702c5SYu Xiangning STRUCT_INIT(ustrlist, mode);
61720f1702c5SYu Xiangning
61730f1702c5SYu Xiangning if (arg == NULL) {
61740f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
61750f1702c5SYu Xiangning if (error == 0)
61760f1702c5SYu Xiangning (*rvalp)++; /* Add one for sockmod */
61770f1702c5SYu Xiangning return (error);
61780f1702c5SYu Xiangning }
61790f1702c5SYu Xiangning
61800f1702c5SYu Xiangning error = so_copyin((void *)arg, STRUCT_BUF(ustrlist),
61810f1702c5SYu Xiangning STRUCT_SIZE(ustrlist), mode & FKIOCTL);
61820f1702c5SYu Xiangning if (error != 0)
61830f1702c5SYu Xiangning return (error);
61840f1702c5SYu Xiangning
61850f1702c5SYu Xiangning nmods = STRUCT_FGET(ustrlist, sl_nmods);
61860f1702c5SYu Xiangning if (nmods <= 0)
61870f1702c5SYu Xiangning return (EINVAL);
61880f1702c5SYu Xiangning /*
61890f1702c5SYu Xiangning * Ceiling nmods at nstrpush to prevent someone from
61900f1702c5SYu Xiangning * maliciously consuming lots of kernel memory.
61910f1702c5SYu Xiangning */
61920f1702c5SYu Xiangning nmods = MIN(nmods, nstrpush);
61930f1702c5SYu Xiangning
61940f1702c5SYu Xiangning kstrlistsize = (nmods + 1) * sizeof (struct str_mlist);
61950f1702c5SYu Xiangning kstrlist.sl_nmods = nmods;
61960f1702c5SYu Xiangning kstrlist.sl_modlist = kmem_zalloc(kstrlistsize, KM_SLEEP);
61970f1702c5SYu Xiangning
61980f1702c5SYu Xiangning error = strioctl(vp, cmd, (intptr_t)&kstrlist, mode, K_TO_K,
61990f1702c5SYu Xiangning cr, rvalp);
62000f1702c5SYu Xiangning if (error != 0)
62010f1702c5SYu Xiangning goto done;
62020f1702c5SYu Xiangning
62030f1702c5SYu Xiangning /*
62040f1702c5SYu Xiangning * Considering the module list as a 0-based array of sl_nmods
62050f1702c5SYu Xiangning * modules, sockmod should conceptually exist at slot
62060f1702c5SYu Xiangning * sti_pushcnt. Insert sockmod at this location by sliding all
62070f1702c5SYu Xiangning * of the module names after so_pushcnt over by one. We know
62080f1702c5SYu Xiangning * that there will be room to do this since we allocated
62090f1702c5SYu Xiangning * sl_modlist with an additional slot.
62100f1702c5SYu Xiangning */
62110f1702c5SYu Xiangning for (i = kstrlist.sl_nmods; i > sti->sti_pushcnt; i--)
62120f1702c5SYu Xiangning kstrlist.sl_modlist[i] = kstrlist.sl_modlist[i - 1];
62130f1702c5SYu Xiangning
62140f1702c5SYu Xiangning (void) strcpy(kstrlist.sl_modlist[i].l_name, sockmod_name);
62150f1702c5SYu Xiangning kstrlist.sl_nmods++;
62160f1702c5SYu Xiangning
62170f1702c5SYu Xiangning /*
62180f1702c5SYu Xiangning * Copy all of the entries out to ustrlist.
62190f1702c5SYu Xiangning */
62200f1702c5SYu Xiangning kmlistp = kstrlist.sl_modlist;
62210f1702c5SYu Xiangning umlistp = STRUCT_FGETP(ustrlist, sl_modlist);
62220f1702c5SYu Xiangning for (i = 0; i < nmods && i < kstrlist.sl_nmods; i++) {
62230f1702c5SYu Xiangning error = so_copyout(kmlistp++, umlistp++,
62240f1702c5SYu Xiangning sizeof (struct str_mlist), mode & FKIOCTL);
62250f1702c5SYu Xiangning if (error != 0)
62260f1702c5SYu Xiangning goto done;
62270f1702c5SYu Xiangning }
62280f1702c5SYu Xiangning
62290f1702c5SYu Xiangning error = so_copyout(&i, (void *)arg, sizeof (int32_t),
62300f1702c5SYu Xiangning mode & FKIOCTL);
62310f1702c5SYu Xiangning if (error == 0)
62320f1702c5SYu Xiangning *rvalp = 0;
62330f1702c5SYu Xiangning done:
62340f1702c5SYu Xiangning kmem_free(kstrlist.sl_modlist, kstrlistsize);
62350f1702c5SYu Xiangning return (error);
62360f1702c5SYu Xiangning }
62370f1702c5SYu Xiangning case I_LOOK:
62380f1702c5SYu Xiangning if (sti->sti_pushcnt == 0) {
62390f1702c5SYu Xiangning return (so_copyout(sockmod_name, (void *)arg,
62400f1702c5SYu Xiangning sizeof (sockmod_name), mode & FKIOCTL));
62410f1702c5SYu Xiangning }
62420f1702c5SYu Xiangning return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
62430f1702c5SYu Xiangning
62440f1702c5SYu Xiangning case I_FIND:
62450f1702c5SYu Xiangning error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
62460f1702c5SYu Xiangning if (error && error != EINVAL)
62470f1702c5SYu Xiangning return (error);
62480f1702c5SYu Xiangning
62490f1702c5SYu Xiangning /* if not found and string was sockmod return 1 */
62500f1702c5SYu Xiangning if (*rvalp == 0 || error == EINVAL) {
62510f1702c5SYu Xiangning error = ((mode & FKIOCTL) ? copystr : copyinstr)(
62520f1702c5SYu Xiangning (void *)arg, mname, sizeof (mname), NULL);
62530f1702c5SYu Xiangning if (error == ENAMETOOLONG)
62540f1702c5SYu Xiangning error = EINVAL;
62550f1702c5SYu Xiangning
62560f1702c5SYu Xiangning if (error == 0 && strcmp(mname, sockmod_name) == 0)
62570f1702c5SYu Xiangning *rvalp = 1;
62580f1702c5SYu Xiangning }
62590f1702c5SYu Xiangning return (error);
62600f1702c5SYu Xiangning
62610f1702c5SYu Xiangning default:
62620f1702c5SYu Xiangning panic("socktpi_plumbioctl: unknown ioctl %d", cmd);
62630f1702c5SYu Xiangning break;
62640f1702c5SYu Xiangning }
62650f1702c5SYu Xiangning
62660f1702c5SYu Xiangning return (0);
62670f1702c5SYu Xiangning }
62680f1702c5SYu Xiangning
62690f1702c5SYu Xiangning /*
62700f1702c5SYu Xiangning * Wrapper around the streams poll routine that implements socket poll
62710f1702c5SYu Xiangning * semantics.
62720f1702c5SYu Xiangning * The sockfs never calls pollwakeup itself - the stream head take care
62730f1702c5SYu Xiangning * of all pollwakeups. Since sockfs never holds so_lock when calling the
62740f1702c5SYu Xiangning * stream head there can never be a deadlock due to holding so_lock across
62750f1702c5SYu Xiangning * pollwakeup and acquiring so_lock in this routine.
62760f1702c5SYu Xiangning *
62770f1702c5SYu Xiangning * However, since the performance of VOP_POLL is critical we avoid
62780f1702c5SYu Xiangning * acquiring so_lock here. This is based on two assumptions:
62790f1702c5SYu Xiangning * - The poll implementation holds locks to serialize the VOP_POLL call
62800f1702c5SYu Xiangning * and a pollwakeup for the same pollhead. This ensures that should
62810f1702c5SYu Xiangning * e.g. so_state change during a socktpi_poll call the pollwakeup
62820f1702c5SYu Xiangning * (which strsock_* and strrput conspire to issue) is issued after
62830f1702c5SYu Xiangning * the state change. Thus the pollwakeup will block until VOP_POLL has
62840f1702c5SYu Xiangning * returned and then wake up poll and have it call VOP_POLL again.
62850f1702c5SYu Xiangning * - The reading of so_state without holding so_lock does not result in
62860f1702c5SYu Xiangning * stale data that is older than the latest state change that has dropped
62870f1702c5SYu Xiangning * so_lock. This is ensured by the mutex_exit issuing the appropriate
62880f1702c5SYu Xiangning * memory barrier to force the data into the coherency domain.
62890f1702c5SYu Xiangning */
62900f1702c5SYu Xiangning static int
sotpi_poll(struct sonode * so,short events,int anyyet,short * reventsp,struct pollhead ** phpp)62910f1702c5SYu Xiangning sotpi_poll(
62920f1702c5SYu Xiangning struct sonode *so,
62930f1702c5SYu Xiangning short events,
62940f1702c5SYu Xiangning int anyyet,
62950f1702c5SYu Xiangning short *reventsp,
62960f1702c5SYu Xiangning struct pollhead **phpp)
62970f1702c5SYu Xiangning {
62980f1702c5SYu Xiangning short origevents = events;
62990f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
63000f1702c5SYu Xiangning int error;
63010f1702c5SYu Xiangning int so_state = so->so_state; /* snapshot */
63020f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
63030f1702c5SYu Xiangning
63040f1702c5SYu Xiangning dprintso(so, 0, ("socktpi_poll(%p): state %s err %d\n",
63050f1702c5SYu Xiangning (void *)vp, pr_state(so_state, so->so_mode), so->so_error));
63060f1702c5SYu Xiangning
63070f1702c5SYu Xiangning ASSERT(vp->v_type == VSOCK);
63080f1702c5SYu Xiangning ASSERT(vp->v_stream != NULL);
63090f1702c5SYu Xiangning
63100f1702c5SYu Xiangning if (so->so_version == SOV_STREAM) {
63110f1702c5SYu Xiangning /* The imaginary "sockmod" has been popped - act as a stream */
63120f1702c5SYu Xiangning return (strpoll(vp->v_stream, events, anyyet,
63130f1702c5SYu Xiangning reventsp, phpp));
63140f1702c5SYu Xiangning }
63150f1702c5SYu Xiangning
63160f1702c5SYu Xiangning if (!(so_state & SS_ISCONNECTED) &&
63170f1702c5SYu Xiangning (so->so_mode & SM_CONNREQUIRED)) {
63180f1702c5SYu Xiangning /* Not connected yet - turn off write side events */
63190f1702c5SYu Xiangning events &= ~(POLLOUT|POLLWRBAND);
63200f1702c5SYu Xiangning }
63210f1702c5SYu Xiangning /*
63220f1702c5SYu Xiangning * Check for errors without calling strpoll if the caller wants them.
63230f1702c5SYu Xiangning * In sockets the errors are represented as input/output events
63240f1702c5SYu Xiangning * and there is no need to ask the stream head for this information.
63250f1702c5SYu Xiangning */
63260f1702c5SYu Xiangning if (so->so_error != 0 &&
63270f1702c5SYu Xiangning ((POLLIN|POLLRDNORM|POLLOUT) & origevents) != 0) {
63280f1702c5SYu Xiangning *reventsp = (POLLIN|POLLRDNORM|POLLOUT) & origevents;
63290f1702c5SYu Xiangning return (0);
63300f1702c5SYu Xiangning }
63310f1702c5SYu Xiangning /*
63320f1702c5SYu Xiangning * Ignore M_PROTO only messages such as the T_EXDATA_IND messages.
63330f1702c5SYu Xiangning * These message with only an M_PROTO/M_PCPROTO part and no M_DATA
63340f1702c5SYu Xiangning * will not trigger a POLLIN event with POLLRDDATA set.
63350f1702c5SYu Xiangning * The handling of urgent data (causing POLLRDBAND) is done by
63360f1702c5SYu Xiangning * inspecting SS_OOBPEND below.
63370f1702c5SYu Xiangning */
63380f1702c5SYu Xiangning events |= POLLRDDATA;
63390f1702c5SYu Xiangning
63400f1702c5SYu Xiangning /*
63410f1702c5SYu Xiangning * After shutdown(output) a stream head write error is set.
63420f1702c5SYu Xiangning * However, we should not return output events.
63430f1702c5SYu Xiangning */
63440f1702c5SYu Xiangning events |= POLLNOERR;
63450f1702c5SYu Xiangning error = strpoll(vp->v_stream, events, anyyet,
63460f1702c5SYu Xiangning reventsp, phpp);
63470f1702c5SYu Xiangning if (error)
63480f1702c5SYu Xiangning return (error);
63490f1702c5SYu Xiangning
63500f1702c5SYu Xiangning ASSERT(!(*reventsp & POLLERR));
63510f1702c5SYu Xiangning
63520f1702c5SYu Xiangning /*
63530f1702c5SYu Xiangning * Notes on T_CONN_IND handling for sockets.
63540f1702c5SYu Xiangning *
63550f1702c5SYu Xiangning * If strpoll() returned without events, SR_POLLIN is guaranteed
63560f1702c5SYu Xiangning * to be set, ensuring any subsequent strrput() runs pollwakeup().
63570f1702c5SYu Xiangning *
63580f1702c5SYu Xiangning * Since the so_lock is not held, soqueueconnind() may have run
63590f1702c5SYu Xiangning * and a T_CONN_IND may be waiting. We now check for any queued
63600f1702c5SYu Xiangning * T_CONN_IND msgs on sti_conn_ind_head and set appropriate events
63610f1702c5SYu Xiangning * to ensure poll returns.
63620f1702c5SYu Xiangning *
63630f1702c5SYu Xiangning * However:
63640f1702c5SYu Xiangning * If the T_CONN_IND hasn't arrived by the time strpoll() returns,
63650f1702c5SYu Xiangning * when strrput() does run for an arriving M_PROTO with T_CONN_IND
63660f1702c5SYu Xiangning * the following actions will occur; taken together they ensure the
63670f1702c5SYu Xiangning * syscall will return.
63680f1702c5SYu Xiangning *
63690f1702c5SYu Xiangning * 1. If a socket, soqueueconnind() will queue the T_CONN_IND but if
63700f1702c5SYu Xiangning * the accept() was run on a non-blocking socket sowaitconnind()
63710f1702c5SYu Xiangning * may have already returned EWOULDBLOCK, so not be waiting to
63720f1702c5SYu Xiangning * process the message. Additionally socktpi_poll() has probably
63730f1702c5SYu Xiangning * proceeded past the sti_conn_ind_head check below.
63740f1702c5SYu Xiangning * 2. strrput() runs pollwakeup()->pollnotify()->cv_signal() to wake
63750f1702c5SYu Xiangning * this thread, however that could occur before poll_common()
63760f1702c5SYu Xiangning * has entered cv_wait.
63770f1702c5SYu Xiangning * 3. pollnotify() sets T_POLLWAKE, while holding the pc_lock.
63780f1702c5SYu Xiangning *
63790f1702c5SYu Xiangning * Before proceeding to cv_wait() in poll_common() for an event,
63800f1702c5SYu Xiangning * poll_common() atomically checks for T_POLLWAKE under the pc_lock,
63810f1702c5SYu Xiangning * and if set, re-calls strpoll() to ensure the late arriving
63820f1702c5SYu Xiangning * T_CONN_IND is recognized, and pollsys() returns.
63830f1702c5SYu Xiangning */
63840f1702c5SYu Xiangning
63850f1702c5SYu Xiangning if (sti->sti_conn_ind_head != NULL)
63860f1702c5SYu Xiangning *reventsp |= (POLLIN|POLLRDNORM) & events;
63870f1702c5SYu Xiangning
6388075fab9aSBryan Cantrill if (so->so_state & SS_CANTRCVMORE) {
6389075fab9aSBryan Cantrill *reventsp |= POLLRDHUP & events;
6390075fab9aSBryan Cantrill
6391075fab9aSBryan Cantrill if (so->so_state & SS_CANTSENDMORE)
6392075fab9aSBryan Cantrill *reventsp |= POLLHUP;
6393075fab9aSBryan Cantrill }
6394075fab9aSBryan Cantrill
63950f1702c5SYu Xiangning if (so->so_state & SS_OOBPEND)
63960f1702c5SYu Xiangning *reventsp |= POLLRDBAND & events;
63970f1702c5SYu Xiangning
63980f1702c5SYu Xiangning if (sti->sti_nl7c_rcv_mp != NULL) {
63990f1702c5SYu Xiangning *reventsp |= (POLLIN|POLLRDNORM) & events;
64000f1702c5SYu Xiangning }
64010f1702c5SYu Xiangning if ((sti->sti_nl7c_flags & NL7C_ENABLED) &&
64020f1702c5SYu Xiangning ((POLLIN|POLLRDNORM) & *reventsp)) {
64030f1702c5SYu Xiangning sti->sti_nl7c_flags |= NL7C_POLLIN;
64040f1702c5SYu Xiangning }
64050f1702c5SYu Xiangning
64060f1702c5SYu Xiangning return (0);
64070f1702c5SYu Xiangning }
64080f1702c5SYu Xiangning
64090f1702c5SYu Xiangning /*ARGSUSED*/
64100f1702c5SYu Xiangning static int
socktpi_constructor(void * buf,void * cdrarg,int kmflags)64110f1702c5SYu Xiangning socktpi_constructor(void *buf, void *cdrarg, int kmflags)
64120f1702c5SYu Xiangning {
64130f1702c5SYu Xiangning sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
64140f1702c5SYu Xiangning int error = 0;
64150f1702c5SYu Xiangning
64160f1702c5SYu Xiangning error = sonode_constructor(buf, cdrarg, kmflags);
64170f1702c5SYu Xiangning if (error != 0)
64180f1702c5SYu Xiangning return (error);
64190f1702c5SYu Xiangning
64200f1702c5SYu Xiangning error = i_sotpi_info_constructor(&st->st_info);
64210f1702c5SYu Xiangning if (error != 0)
64220f1702c5SYu Xiangning sonode_destructor(buf, cdrarg);
64230f1702c5SYu Xiangning
64240f1702c5SYu Xiangning st->st_sonode.so_priv = &st->st_info;
64250f1702c5SYu Xiangning
64260f1702c5SYu Xiangning return (error);
64270f1702c5SYu Xiangning }
64280f1702c5SYu Xiangning
64290f1702c5SYu Xiangning /*ARGSUSED1*/
64300f1702c5SYu Xiangning static void
socktpi_destructor(void * buf,void * cdrarg)64310f1702c5SYu Xiangning socktpi_destructor(void *buf, void *cdrarg)
64320f1702c5SYu Xiangning {
64330f1702c5SYu Xiangning sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
64340f1702c5SYu Xiangning
64350f1702c5SYu Xiangning ASSERT(st->st_sonode.so_priv == &st->st_info);
64360f1702c5SYu Xiangning st->st_sonode.so_priv = NULL;
64370f1702c5SYu Xiangning
64380f1702c5SYu Xiangning i_sotpi_info_destructor(&st->st_info);
64390f1702c5SYu Xiangning sonode_destructor(buf, cdrarg);
64400f1702c5SYu Xiangning }
64410f1702c5SYu Xiangning
64420f1702c5SYu Xiangning static int
socktpi_unix_constructor(void * buf,void * cdrarg,int kmflags)64430f1702c5SYu Xiangning socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags)
64440f1702c5SYu Xiangning {
64450f1702c5SYu Xiangning int retval;
64460f1702c5SYu Xiangning
64470f1702c5SYu Xiangning if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) {
64480f1702c5SYu Xiangning struct sonode *so = (struct sonode *)buf;
64490f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
64500f1702c5SYu Xiangning
64510f1702c5SYu Xiangning mutex_enter(&socklist.sl_lock);
64520f1702c5SYu Xiangning
64530f1702c5SYu Xiangning sti->sti_next_so = socklist.sl_list;
64540f1702c5SYu Xiangning sti->sti_prev_so = NULL;
64550f1702c5SYu Xiangning if (sti->sti_next_so != NULL)
64560f1702c5SYu Xiangning SOTOTPI(sti->sti_next_so)->sti_prev_so = so;
64570f1702c5SYu Xiangning socklist.sl_list = so;
64580f1702c5SYu Xiangning
64590f1702c5SYu Xiangning mutex_exit(&socklist.sl_lock);
64600f1702c5SYu Xiangning
64610f1702c5SYu Xiangning }
64620f1702c5SYu Xiangning return (retval);
64630f1702c5SYu Xiangning }
64640f1702c5SYu Xiangning
64650f1702c5SYu Xiangning static void
socktpi_unix_destructor(void * buf,void * cdrarg)64660f1702c5SYu Xiangning socktpi_unix_destructor(void *buf, void *cdrarg)
64670f1702c5SYu Xiangning {
64680f1702c5SYu Xiangning struct sonode *so = (struct sonode *)buf;
64690f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
64700f1702c5SYu Xiangning
64710f1702c5SYu Xiangning mutex_enter(&socklist.sl_lock);
64720f1702c5SYu Xiangning
64730f1702c5SYu Xiangning if (sti->sti_next_so != NULL)
64740f1702c5SYu Xiangning SOTOTPI(sti->sti_next_so)->sti_prev_so = sti->sti_prev_so;
64750f1702c5SYu Xiangning if (sti->sti_prev_so != NULL)
64760f1702c5SYu Xiangning SOTOTPI(sti->sti_prev_so)->sti_next_so = sti->sti_next_so;
64770f1702c5SYu Xiangning else
64780f1702c5SYu Xiangning socklist.sl_list = sti->sti_next_so;
64790f1702c5SYu Xiangning
64800f1702c5SYu Xiangning mutex_exit(&socklist.sl_lock);
64810f1702c5SYu Xiangning
64820f1702c5SYu Xiangning socktpi_destructor(buf, cdrarg);
64830f1702c5SYu Xiangning }
64840f1702c5SYu Xiangning
64850f1702c5SYu Xiangning int
socktpi_init(void)64860f1702c5SYu Xiangning socktpi_init(void)
64870f1702c5SYu Xiangning {
64880f1702c5SYu Xiangning /*
64890f1702c5SYu Xiangning * Create sonode caches. We create a special one for AF_UNIX so
64900f1702c5SYu Xiangning * that we can track them for netstat(1m).
64910f1702c5SYu Xiangning */
64920f1702c5SYu Xiangning socktpi_cache = kmem_cache_create("socktpi_cache",
64930f1702c5SYu Xiangning sizeof (struct sotpi_sonode), 0, socktpi_constructor,
64940f1702c5SYu Xiangning socktpi_destructor, NULL, NULL, NULL, 0);
64950f1702c5SYu Xiangning
64960f1702c5SYu Xiangning socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache",
64970f1702c5SYu Xiangning sizeof (struct sotpi_sonode), 0, socktpi_unix_constructor,
64980f1702c5SYu Xiangning socktpi_unix_destructor, NULL, NULL, NULL, 0);
64990f1702c5SYu Xiangning
65000f1702c5SYu Xiangning return (0);
65010f1702c5SYu Xiangning }
65020f1702c5SYu Xiangning
65030f1702c5SYu Xiangning /*
65040f1702c5SYu Xiangning * Given a non-TPI sonode, allocate and prep it to be ready for TPI.
65050f1702c5SYu Xiangning *
65060f1702c5SYu Xiangning * Caller must still update state and mode using sotpi_update_state().
65070f1702c5SYu Xiangning */
650841174437SAnders Persson int
sotpi_convert_sonode(struct sonode * so,struct sockparams * newsp,boolean_t * direct,queue_t ** qp,struct cred * cr)65090f1702c5SYu Xiangning sotpi_convert_sonode(struct sonode *so, struct sockparams *newsp,
651041174437SAnders Persson boolean_t *direct, queue_t **qp, struct cred *cr)
65110f1702c5SYu Xiangning {
65120f1702c5SYu Xiangning sotpi_info_t *sti;
65130f1702c5SYu Xiangning struct sockparams *origsp = so->so_sockparams;
65140f1702c5SYu Xiangning sock_lower_handle_t handle = so->so_proto_handle;
65150f1702c5SYu Xiangning struct stdata *stp;
65160f1702c5SYu Xiangning struct vnode *vp;
65170f1702c5SYu Xiangning queue_t *q;
651841174437SAnders Persson int error = 0;
65190f1702c5SYu Xiangning
652041174437SAnders Persson ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
652141174437SAnders Persson SS_FALLBACK_PENDING);
652241174437SAnders Persson ASSERT(SOCK_IS_NONSTR(so));
652341174437SAnders Persson
652441174437SAnders Persson *qp = NULL;
65250f1702c5SYu Xiangning *direct = B_FALSE;
65260f1702c5SYu Xiangning so->so_sockparams = newsp;
65270f1702c5SYu Xiangning /*
65280f1702c5SYu Xiangning * Allocate and initalize fields required by TPI.
65290f1702c5SYu Xiangning */
65300f1702c5SYu Xiangning (void) sotpi_info_create(so, KM_SLEEP);
65310f1702c5SYu Xiangning sotpi_info_init(so);
65320f1702c5SYu Xiangning
653341174437SAnders Persson if ((error = sotpi_init(so, NULL, cr, SO_FALLBACK)) != 0) {
65340f1702c5SYu Xiangning sotpi_info_fini(so);
65350f1702c5SYu Xiangning sotpi_info_destroy(so);
653641174437SAnders Persson return (error);
65370f1702c5SYu Xiangning }
65380f1702c5SYu Xiangning ASSERT(handle == so->so_proto_handle);
65390f1702c5SYu Xiangning sti = SOTOTPI(so);
65400f1702c5SYu Xiangning if (sti->sti_direct != 0)
65410f1702c5SYu Xiangning *direct = B_TRUE;
65420f1702c5SYu Xiangning
65430f1702c5SYu Xiangning /*
65440f1702c5SYu Xiangning * Keep the original sp around so we can properly dispose of the
65450f1702c5SYu Xiangning * sonode when the socket is being closed.
65460f1702c5SYu Xiangning */
65470f1702c5SYu Xiangning sti->sti_orig_sp = origsp;
65480f1702c5SYu Xiangning
65490f1702c5SYu Xiangning so_basic_strinit(so); /* skips the T_CAPABILITY_REQ */
65500f1702c5SYu Xiangning so_alloc_addr(so, so->so_max_addr_len);
65510f1702c5SYu Xiangning
65520f1702c5SYu Xiangning /*
65530f1702c5SYu Xiangning * If the application has done a SIOCSPGRP, make sure the
65540f1702c5SYu Xiangning * STREAM head is aware. This needs to take place before
65550f1702c5SYu Xiangning * the protocol start sending up messages. Otherwise we
65560f1702c5SYu Xiangning * might miss to generate SIGPOLL.
65570f1702c5SYu Xiangning *
65580f1702c5SYu Xiangning * It is possible that the application will receive duplicate
65590f1702c5SYu Xiangning * signals if some were already generated for either data or
65600f1702c5SYu Xiangning * connection indications.
65610f1702c5SYu Xiangning */
65620f1702c5SYu Xiangning if (so->so_pgrp != 0) {
65630f1702c5SYu Xiangning if (so_set_events(so, so->so_vnode, cr) != 0)
65640f1702c5SYu Xiangning so->so_pgrp = 0;
65650f1702c5SYu Xiangning }
65660f1702c5SYu Xiangning
65670f1702c5SYu Xiangning /*
65680f1702c5SYu Xiangning * Determine which queue to use.
65690f1702c5SYu Xiangning */
65700f1702c5SYu Xiangning vp = SOTOV(so);
65710f1702c5SYu Xiangning stp = vp->v_stream;
65720f1702c5SYu Xiangning ASSERT(stp != NULL);
65730f1702c5SYu Xiangning q = stp->sd_wrq->q_next;
65740f1702c5SYu Xiangning
65750f1702c5SYu Xiangning /*
65760f1702c5SYu Xiangning * Skip any modules that may have been auto pushed when the device
65770f1702c5SYu Xiangning * was opened
65780f1702c5SYu Xiangning */
65790f1702c5SYu Xiangning while (q->q_next != NULL)
65800f1702c5SYu Xiangning q = q->q_next;
658141174437SAnders Persson *qp = _RD(q);
65820f1702c5SYu Xiangning
658341174437SAnders Persson /* This is now a STREAMS sockets */
658441174437SAnders Persson so->so_not_str = B_FALSE;
658541174437SAnders Persson
658641174437SAnders Persson return (error);
658741174437SAnders Persson }
658841174437SAnders Persson
658941174437SAnders Persson /*
659041174437SAnders Persson * Revert a TPI sonode. It is only allowed to revert the sonode during
659141174437SAnders Persson * the fallback process.
659241174437SAnders Persson */
659341174437SAnders Persson void
sotpi_revert_sonode(struct sonode * so,struct cred * cr)659441174437SAnders Persson sotpi_revert_sonode(struct sonode *so, struct cred *cr)
659541174437SAnders Persson {
659641174437SAnders Persson vnode_t *vp = SOTOV(so);
659741174437SAnders Persson
659841174437SAnders Persson ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
659941174437SAnders Persson SS_FALLBACK_PENDING);
660041174437SAnders Persson ASSERT(!SOCK_IS_NONSTR(so));
660141174437SAnders Persson ASSERT(vp->v_stream != NULL);
660241174437SAnders Persson
660341174437SAnders Persson strclean(vp);
660441174437SAnders Persson (void) strclose(vp, FREAD|FWRITE|SO_FALLBACK, cr);
660541174437SAnders Persson
660641174437SAnders Persson /*
660741174437SAnders Persson * Restore the original sockparams. The caller is responsible for
660841174437SAnders Persson * dropping the ref to the new sp.
660941174437SAnders Persson */
661041174437SAnders Persson so->so_sockparams = SOTOTPI(so)->sti_orig_sp;
661141174437SAnders Persson
661241174437SAnders Persson sotpi_info_fini(so);
661341174437SAnders Persson sotpi_info_destroy(so);
661441174437SAnders Persson
661541174437SAnders Persson /* This is no longer a STREAMS sockets */
661641174437SAnders Persson so->so_not_str = B_TRUE;
66170f1702c5SYu Xiangning }
66180f1702c5SYu Xiangning
66190f1702c5SYu 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)66200f1702c5SYu Xiangning sotpi_update_state(struct sonode *so, struct T_capability_ack *tcap,
66210f1702c5SYu Xiangning struct sockaddr *laddr, socklen_t laddrlen, struct sockaddr *faddr,
66220f1702c5SYu Xiangning socklen_t faddrlen, short opts)
66230f1702c5SYu Xiangning {
66240f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
66250f1702c5SYu Xiangning
66260f1702c5SYu Xiangning so_proc_tcapability_ack(so, tcap);
66270f1702c5SYu Xiangning
66280f1702c5SYu Xiangning so->so_options |= opts;
66290f1702c5SYu Xiangning
66300f1702c5SYu Xiangning /*
66310f1702c5SYu Xiangning * Determine whether the foreign and local address are valid
66320f1702c5SYu Xiangning */
66330f1702c5SYu Xiangning if (laddrlen != 0) {
66340f1702c5SYu Xiangning ASSERT(laddrlen <= sti->sti_laddr_maxlen);
66350f1702c5SYu Xiangning sti->sti_laddr_len = laddrlen;
66360f1702c5SYu Xiangning bcopy(laddr, sti->sti_laddr_sa, laddrlen);
66370f1702c5SYu Xiangning sti->sti_laddr_valid = (so->so_state & SS_ISBOUND);
66380f1702c5SYu Xiangning }
66390f1702c5SYu Xiangning
66400f1702c5SYu Xiangning if (faddrlen != 0) {
66410f1702c5SYu Xiangning ASSERT(faddrlen <= sti->sti_faddr_maxlen);
66420f1702c5SYu Xiangning sti->sti_faddr_len = faddrlen;
66430f1702c5SYu Xiangning bcopy(faddr, sti->sti_faddr_sa, faddrlen);
66440f1702c5SYu Xiangning sti->sti_faddr_valid = (so->so_state & SS_ISCONNECTED);
66450f1702c5SYu Xiangning }
66460f1702c5SYu Xiangning
66470f1702c5SYu Xiangning }
66480f1702c5SYu Xiangning
66490f1702c5SYu Xiangning /*
66500f1702c5SYu Xiangning * Allocate enough space to cache the local and foreign addresses.
66510f1702c5SYu Xiangning */
66520f1702c5SYu Xiangning void
so_alloc_addr(struct sonode * so,t_uscalar_t maxlen)66530f1702c5SYu Xiangning so_alloc_addr(struct sonode *so, t_uscalar_t maxlen)
66540f1702c5SYu Xiangning {
66550f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
66560f1702c5SYu Xiangning
66570f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
66580f1702c5SYu Xiangning ASSERT(sti->sti_laddr_len == 0 && sti->sti_faddr_len == 0);
66590f1702c5SYu Xiangning sti->sti_laddr_maxlen = sti->sti_faddr_maxlen =
66600f1702c5SYu Xiangning P2ROUNDUP(maxlen, KMEM_ALIGN);
66610f1702c5SYu Xiangning so->so_max_addr_len = sti->sti_laddr_maxlen;
66620f1702c5SYu Xiangning sti->sti_laddr_sa = kmem_alloc(sti->sti_laddr_maxlen * 2, KM_SLEEP);
66630f1702c5SYu Xiangning sti->sti_faddr_sa = (struct sockaddr *)((caddr_t)sti->sti_laddr_sa
66640f1702c5SYu Xiangning + sti->sti_laddr_maxlen);
66650f1702c5SYu Xiangning
66660f1702c5SYu Xiangning if (so->so_family == AF_UNIX) {
66670f1702c5SYu Xiangning /*
66680f1702c5SYu Xiangning * Initialize AF_UNIX related fields.
66690f1702c5SYu Xiangning */
66700f1702c5SYu Xiangning bzero(&sti->sti_ux_laddr, sizeof (sti->sti_ux_laddr));
66710f1702c5SYu Xiangning bzero(&sti->sti_ux_faddr, sizeof (sti->sti_ux_faddr));
66720f1702c5SYu Xiangning }
66730f1702c5SYu Xiangning }
66740f1702c5SYu Xiangning
66750f1702c5SYu Xiangning
66760f1702c5SYu Xiangning sotpi_info_t *
sotpi_sototpi(struct sonode * so)66770f1702c5SYu Xiangning sotpi_sototpi(struct sonode *so)
66780f1702c5SYu Xiangning {
66790f1702c5SYu Xiangning sotpi_info_t *sti;
66800f1702c5SYu Xiangning
668141174437SAnders Persson ASSERT(so != NULL);
66820f1702c5SYu Xiangning
66830f1702c5SYu Xiangning sti = (sotpi_info_t *)so->so_priv;
66840f1702c5SYu Xiangning
66850f1702c5SYu Xiangning ASSERT(sti != NULL);
66860f1702c5SYu Xiangning ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
66870f1702c5SYu Xiangning
66880f1702c5SYu Xiangning return (sti);
66890f1702c5SYu Xiangning }
66900f1702c5SYu Xiangning
66910f1702c5SYu Xiangning static int
i_sotpi_info_constructor(sotpi_info_t * sti)66920f1702c5SYu Xiangning i_sotpi_info_constructor(sotpi_info_t *sti)
66930f1702c5SYu Xiangning {
66940f1702c5SYu Xiangning sti->sti_magic = SOTPI_INFO_MAGIC;
66950f1702c5SYu Xiangning sti->sti_ack_mp = NULL;
66960f1702c5SYu Xiangning sti->sti_discon_ind_mp = NULL;
66970f1702c5SYu Xiangning sti->sti_ux_bound_vp = NULL;
66980f1702c5SYu Xiangning sti->sti_unbind_mp = NULL;
66990f1702c5SYu Xiangning
67000f1702c5SYu Xiangning sti->sti_conn_ind_head = NULL;
67010f1702c5SYu Xiangning sti->sti_conn_ind_tail = NULL;
67020f1702c5SYu Xiangning
67030f1702c5SYu Xiangning sti->sti_laddr_sa = NULL;
67040f1702c5SYu Xiangning sti->sti_faddr_sa = NULL;
67050f1702c5SYu Xiangning
67060f1702c5SYu Xiangning sti->sti_nl7c_flags = 0;
67070f1702c5SYu Xiangning sti->sti_nl7c_uri = NULL;
67080f1702c5SYu Xiangning sti->sti_nl7c_rcv_mp = NULL;
67090f1702c5SYu Xiangning
67100f1702c5SYu Xiangning mutex_init(&sti->sti_plumb_lock, NULL, MUTEX_DEFAULT, NULL);
67110f1702c5SYu Xiangning cv_init(&sti->sti_ack_cv, NULL, CV_DEFAULT, NULL);
67120f1702c5SYu Xiangning
67130f1702c5SYu Xiangning return (0);
67140f1702c5SYu Xiangning }
67150f1702c5SYu Xiangning
67160f1702c5SYu Xiangning static void
i_sotpi_info_destructor(sotpi_info_t * sti)67170f1702c5SYu Xiangning i_sotpi_info_destructor(sotpi_info_t *sti)
67180f1702c5SYu Xiangning {
67190f1702c5SYu Xiangning ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
67200f1702c5SYu Xiangning ASSERT(sti->sti_ack_mp == NULL);
67210f1702c5SYu Xiangning ASSERT(sti->sti_discon_ind_mp == NULL);
67220f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
67230f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp == NULL);
67240f1702c5SYu Xiangning
67250f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_head == NULL);
67260f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_tail == NULL);
67270f1702c5SYu Xiangning
67280f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL);
67290f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa == NULL);
67300f1702c5SYu Xiangning
67310f1702c5SYu Xiangning ASSERT(sti->sti_nl7c_flags == 0);
67320f1702c5SYu Xiangning ASSERT(sti->sti_nl7c_uri == NULL);
67330f1702c5SYu Xiangning ASSERT(sti->sti_nl7c_rcv_mp == NULL);
67340f1702c5SYu Xiangning
67350f1702c5SYu Xiangning mutex_destroy(&sti->sti_plumb_lock);
67360f1702c5SYu Xiangning cv_destroy(&sti->sti_ack_cv);
67370f1702c5SYu Xiangning }
67380f1702c5SYu Xiangning
67390f1702c5SYu Xiangning /*
67400f1702c5SYu Xiangning * Creates and attaches TPI information to the given sonode
67410f1702c5SYu Xiangning */
67420f1702c5SYu Xiangning static boolean_t
sotpi_info_create(struct sonode * so,int kmflags)67430f1702c5SYu Xiangning sotpi_info_create(struct sonode *so, int kmflags)
67440f1702c5SYu Xiangning {
67450f1702c5SYu Xiangning sotpi_info_t *sti;
67460f1702c5SYu Xiangning
67470f1702c5SYu Xiangning ASSERT(so->so_priv == NULL);
67480f1702c5SYu Xiangning
67490f1702c5SYu Xiangning if ((sti = kmem_zalloc(sizeof (*sti), kmflags)) == NULL)
67500f1702c5SYu Xiangning return (B_FALSE);
67510f1702c5SYu Xiangning
67520f1702c5SYu Xiangning if (i_sotpi_info_constructor(sti) != 0) {
67530f1702c5SYu Xiangning kmem_free(sti, sizeof (*sti));
67540f1702c5SYu Xiangning return (B_FALSE);
67550f1702c5SYu Xiangning }
67560f1702c5SYu Xiangning
67570f1702c5SYu Xiangning so->so_priv = (void *)sti;
67580f1702c5SYu Xiangning return (B_TRUE);
67590f1702c5SYu Xiangning }
67600f1702c5SYu Xiangning
67610f1702c5SYu Xiangning /*
67620f1702c5SYu Xiangning * Initializes the TPI information.
67630f1702c5SYu Xiangning */
67640f1702c5SYu Xiangning static void
sotpi_info_init(struct sonode * so)67650f1702c5SYu Xiangning sotpi_info_init(struct sonode *so)
67660f1702c5SYu Xiangning {
67670f1702c5SYu Xiangning struct vnode *vp = SOTOV(so);
67680f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
67690f1702c5SYu Xiangning time_t now;
67700f1702c5SYu Xiangning
67710f1702c5SYu Xiangning sti->sti_dev = so->so_sockparams->sp_sdev_info.sd_vnode->v_rdev;
67720f1702c5SYu Xiangning vp->v_rdev = sti->sti_dev;
67730f1702c5SYu Xiangning
67740f1702c5SYu Xiangning sti->sti_orig_sp = NULL;
67750f1702c5SYu Xiangning
67760f1702c5SYu Xiangning sti->sti_pushcnt = 0;
67770f1702c5SYu Xiangning
67780f1702c5SYu Xiangning now = gethrestime_sec();
67790f1702c5SYu Xiangning sti->sti_atime = now;
67800f1702c5SYu Xiangning sti->sti_mtime = now;
67810f1702c5SYu Xiangning sti->sti_ctime = now;
67820f1702c5SYu Xiangning
67830f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
67840f1702c5SYu Xiangning sti->sti_delayed_error = 0;
67850f1702c5SYu Xiangning
67860f1702c5SYu Xiangning sti->sti_provinfo = NULL;
67870f1702c5SYu Xiangning
67880f1702c5SYu Xiangning sti->sti_oobcnt = 0;
67890f1702c5SYu Xiangning sti->sti_oobsigcnt = 0;
67900f1702c5SYu Xiangning
67910f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
67920f1702c5SYu Xiangning
67930f1702c5SYu Xiangning sti->sti_laddr_sa = 0;
67940f1702c5SYu Xiangning sti->sti_faddr_sa = 0;
67950f1702c5SYu Xiangning sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 0;
67960f1702c5SYu Xiangning sti->sti_laddr_len = sti->sti_faddr_len = 0;
67970f1702c5SYu Xiangning
67980f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
67990f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
68000f1702c5SYu Xiangning sti->sti_faddr_noxlate = 0;
68010f1702c5SYu Xiangning
68020f1702c5SYu Xiangning sti->sti_direct = 0;
68030f1702c5SYu Xiangning
68040f1702c5SYu Xiangning ASSERT(sti->sti_ack_mp == NULL);
68050f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
68060f1702c5SYu Xiangning ASSERT(sti->sti_unbind_mp == NULL);
68070f1702c5SYu Xiangning
68080f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_head == NULL);
68090f1702c5SYu Xiangning ASSERT(sti->sti_conn_ind_tail == NULL);
68100f1702c5SYu Xiangning }
68110f1702c5SYu Xiangning
68120f1702c5SYu Xiangning /*
68130f1702c5SYu Xiangning * Given a sonode, grab the TPI info and free any data.
68140f1702c5SYu Xiangning */
68150f1702c5SYu Xiangning static void
sotpi_info_fini(struct sonode * so)68160f1702c5SYu Xiangning sotpi_info_fini(struct sonode *so)
68170f1702c5SYu Xiangning {
68180f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
68190f1702c5SYu Xiangning mblk_t *mp;
68200f1702c5SYu Xiangning
68210f1702c5SYu Xiangning ASSERT(sti->sti_discon_ind_mp == NULL);
68220f1702c5SYu Xiangning
68230f1702c5SYu Xiangning if ((mp = sti->sti_conn_ind_head) != NULL) {
68240f1702c5SYu Xiangning mblk_t *mp1;
68250f1702c5SYu Xiangning
68260f1702c5SYu Xiangning while (mp) {
68270f1702c5SYu Xiangning mp1 = mp->b_next;
68280f1702c5SYu Xiangning mp->b_next = NULL;
68290f1702c5SYu Xiangning freemsg(mp);
68300f1702c5SYu Xiangning mp = mp1;
68310f1702c5SYu Xiangning }
68320f1702c5SYu Xiangning sti->sti_conn_ind_head = sti->sti_conn_ind_tail = NULL;
68330f1702c5SYu Xiangning }
68340f1702c5SYu Xiangning
68350f1702c5SYu Xiangning /*
68360f1702c5SYu Xiangning * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely
68370f1702c5SYu Xiangning * indirect them. It also uses so_count as a validity test.
68380f1702c5SYu Xiangning */
68390f1702c5SYu Xiangning mutex_enter(&so->so_lock);
68400f1702c5SYu Xiangning
68410f1702c5SYu Xiangning if (sti->sti_laddr_sa) {
68420f1702c5SYu Xiangning ASSERT((caddr_t)sti->sti_faddr_sa ==
68430f1702c5SYu Xiangning (caddr_t)sti->sti_laddr_sa + sti->sti_laddr_maxlen);
68440f1702c5SYu Xiangning ASSERT(sti->sti_faddr_maxlen == sti->sti_laddr_maxlen);
68450f1702c5SYu Xiangning sti->sti_laddr_valid = 0;
68460f1702c5SYu Xiangning sti->sti_faddr_valid = 0;
68470f1702c5SYu Xiangning kmem_free(sti->sti_laddr_sa, sti->sti_laddr_maxlen * 2);
68480f1702c5SYu Xiangning sti->sti_laddr_sa = NULL;
68490f1702c5SYu Xiangning sti->sti_laddr_len = sti->sti_laddr_maxlen = 0;
68500f1702c5SYu Xiangning sti->sti_faddr_sa = NULL;
68510f1702c5SYu Xiangning sti->sti_faddr_len = sti->sti_faddr_maxlen = 0;
68520f1702c5SYu Xiangning }
68530f1702c5SYu Xiangning
68540f1702c5SYu Xiangning mutex_exit(&so->so_lock);
68550f1702c5SYu Xiangning
68560f1702c5SYu Xiangning if ((mp = sti->sti_eaddr_mp) != NULL) {
68570f1702c5SYu Xiangning freemsg(mp);
68580f1702c5SYu Xiangning sti->sti_eaddr_mp = NULL;
68590f1702c5SYu Xiangning sti->sti_delayed_error = 0;
68600f1702c5SYu Xiangning }
68610f1702c5SYu Xiangning
68620f1702c5SYu Xiangning if ((mp = sti->sti_ack_mp) != NULL) {
68630f1702c5SYu Xiangning freemsg(mp);
68640f1702c5SYu Xiangning sti->sti_ack_mp = NULL;
68650f1702c5SYu Xiangning }
68660f1702c5SYu Xiangning
68670f1702c5SYu Xiangning if ((mp = sti->sti_nl7c_rcv_mp) != NULL) {
68680f1702c5SYu Xiangning sti->sti_nl7c_rcv_mp = NULL;
68690f1702c5SYu Xiangning freemsg(mp);
68700f1702c5SYu Xiangning }
68710f1702c5SYu Xiangning sti->sti_nl7c_rcv_rval = 0;
68720f1702c5SYu Xiangning if (sti->sti_nl7c_uri != NULL) {
68730f1702c5SYu Xiangning nl7c_urifree(so);
68740f1702c5SYu Xiangning /* urifree() cleared nl7c_uri */
68750f1702c5SYu Xiangning }
68760f1702c5SYu Xiangning if (sti->sti_nl7c_flags) {
68770f1702c5SYu Xiangning sti->sti_nl7c_flags = 0;
68780f1702c5SYu Xiangning }
68790f1702c5SYu Xiangning
68800f1702c5SYu Xiangning ASSERT(sti->sti_ux_bound_vp == NULL);
68810f1702c5SYu Xiangning if ((mp = sti->sti_unbind_mp) != NULL) {
68820f1702c5SYu Xiangning freemsg(mp);
68830f1702c5SYu Xiangning sti->sti_unbind_mp = NULL;
68840f1702c5SYu Xiangning }
68850f1702c5SYu Xiangning }
68860f1702c5SYu Xiangning
68870f1702c5SYu Xiangning /*
68880f1702c5SYu Xiangning * Destroys the TPI information attached to a sonode.
68890f1702c5SYu Xiangning */
68900f1702c5SYu Xiangning static void
sotpi_info_destroy(struct sonode * so)68910f1702c5SYu Xiangning sotpi_info_destroy(struct sonode *so)
68920f1702c5SYu Xiangning {
68930f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so);
68940f1702c5SYu Xiangning
68950f1702c5SYu Xiangning i_sotpi_info_destructor(sti);
68960f1702c5SYu Xiangning kmem_free(sti, sizeof (*sti));
68970f1702c5SYu Xiangning
68980f1702c5SYu Xiangning so->so_priv = NULL;
68990f1702c5SYu Xiangning }
69000f1702c5SYu Xiangning
69010f1702c5SYu Xiangning /*
69022691240cSYu Xiangning * Create the global sotpi socket module entry. It will never be freed.
69030f1702c5SYu Xiangning */
69040f1702c5SYu Xiangning smod_info_t *
sotpi_smod_create(void)69050f1702c5SYu Xiangning sotpi_smod_create(void)
69060f1702c5SYu Xiangning {
69070f1702c5SYu Xiangning smod_info_t *smodp;
69080f1702c5SYu Xiangning
69090f1702c5SYu Xiangning smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP);
69102691240cSYu Xiangning smodp->smod_name = kmem_alloc(sizeof (SOTPI_SMOD_NAME), KM_SLEEP);
69112691240cSYu Xiangning (void) strcpy(smodp->smod_name, SOTPI_SMOD_NAME);
69120f1702c5SYu Xiangning /*
69132691240cSYu Xiangning * Initialize the smod_refcnt to 1 so it will never be freed.
69140f1702c5SYu Xiangning */
69150f1702c5SYu Xiangning smodp->smod_refcnt = 1;
69160f1702c5SYu Xiangning smodp->smod_uc_version = SOCK_UC_VERSION;
69170f1702c5SYu Xiangning smodp->smod_dc_version = SOCK_DC_VERSION;
69180f1702c5SYu Xiangning smodp->smod_sock_create_func = &sotpi_create;
69190f1702c5SYu Xiangning smodp->smod_sock_destroy_func = &sotpi_destroy;
69200f1702c5SYu Xiangning return (smodp);
69210f1702c5SYu Xiangning }
6922