10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51548Srshoaib * Common Development and Distribution License (the "License"). 61548Srshoaib * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211548Srshoaib 220Sstevel@tonic-gate /* 238477SRao.Shoaib@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <sys/types.h> 280Sstevel@tonic-gate #include <sys/t_lock.h> 290Sstevel@tonic-gate #include <sys/param.h> 300Sstevel@tonic-gate #include <sys/systm.h> 310Sstevel@tonic-gate #include <sys/buf.h> 320Sstevel@tonic-gate #include <sys/conf.h> 330Sstevel@tonic-gate #include <sys/cred.h> 340Sstevel@tonic-gate #include <sys/kmem.h> 358348SEric.Yu@Sun.COM #include <sys/kmem_impl.h> 360Sstevel@tonic-gate #include <sys/sysmacros.h> 370Sstevel@tonic-gate #include <sys/vfs.h> 380Sstevel@tonic-gate #include <sys/vnode.h> 390Sstevel@tonic-gate #include <sys/debug.h> 400Sstevel@tonic-gate #include <sys/errno.h> 410Sstevel@tonic-gate #include <sys/time.h> 420Sstevel@tonic-gate #include <sys/file.h> 430Sstevel@tonic-gate #include <sys/open.h> 440Sstevel@tonic-gate #include <sys/user.h> 450Sstevel@tonic-gate #include <sys/termios.h> 460Sstevel@tonic-gate #include <sys/stream.h> 470Sstevel@tonic-gate #include <sys/strsubr.h> 480Sstevel@tonic-gate #include <sys/strsun.h> 498348SEric.Yu@Sun.COM #include <sys/suntpi.h> 500Sstevel@tonic-gate #include <sys/ddi.h> 510Sstevel@tonic-gate #include <sys/esunddi.h> 520Sstevel@tonic-gate #include <sys/flock.h> 530Sstevel@tonic-gate #include <sys/modctl.h> 540Sstevel@tonic-gate #include <sys/vtrace.h> 550Sstevel@tonic-gate #include <sys/cmn_err.h> 560Sstevel@tonic-gate #include <sys/pathname.h> 570Sstevel@tonic-gate 580Sstevel@tonic-gate #include <sys/socket.h> 590Sstevel@tonic-gate #include <sys/socketvar.h> 60741Smasputra #include <sys/sockio.h> 616707Sbrutus #include <sys/sodirect.h> 620Sstevel@tonic-gate #include <netinet/in.h> 630Sstevel@tonic-gate #include <sys/un.h> 640Sstevel@tonic-gate #include <sys/strsun.h> 650Sstevel@tonic-gate 660Sstevel@tonic-gate #include <sys/tiuser.h> 670Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 680Sstevel@tonic-gate #include <sys/tihdr.h> 690Sstevel@tonic-gate #include <sys/timod.h> /* TI_GETMYNAME, TI_GETPEERNAME */ 700Sstevel@tonic-gate 710Sstevel@tonic-gate #include <c2/audit.h> 720Sstevel@tonic-gate 730Sstevel@tonic-gate #include <inet/common.h> 740Sstevel@tonic-gate #include <inet/ip.h> 750Sstevel@tonic-gate #include <inet/ip6.h> 760Sstevel@tonic-gate #include <inet/tcp.h> 77741Smasputra #include <inet/udp_impl.h> 780Sstevel@tonic-gate 791974Sbrutus #include <sys/zone.h> 801974Sbrutus 810Sstevel@tonic-gate #include <fs/sockfs/nl7c.h> 821974Sbrutus #include <fs/sockfs/nl7curi.h> 830Sstevel@tonic-gate 84898Skais #include <inet/kssl/ksslapi.h> 85898Skais 868348SEric.Yu@Sun.COM #include <fs/sockfs/sockcommon.h> 878348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h> 888348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi_impl.h> 898348SEric.Yu@Sun.COM 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * Possible failures when memory can't be allocated. The documented behavior: 920Sstevel@tonic-gate * 930Sstevel@tonic-gate * 5.5: 4.X: XNET: 940Sstevel@tonic-gate * accept: ENOMEM/ENOSR/EINTR - (EINTR) ENOMEM/ENOBUFS/ENOSR/ 950Sstevel@tonic-gate * EINTR 960Sstevel@tonic-gate * (4.X does not document EINTR but returns it) 970Sstevel@tonic-gate * bind: ENOSR - ENOBUFS/ENOSR 980Sstevel@tonic-gate * connect: EINTR EINTR ENOBUFS/ENOSR/EINTR 990Sstevel@tonic-gate * getpeername: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 1000Sstevel@tonic-gate * getsockname: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 1010Sstevel@tonic-gate * (4.X getpeername and getsockname do not fail in practice) 1020Sstevel@tonic-gate * getsockopt: ENOMEM/ENOSR - ENOBUFS/ENOSR 1030Sstevel@tonic-gate * listen: - - ENOBUFS 1040Sstevel@tonic-gate * recv: ENOMEM/ENOSR/EINTR EINTR ENOBUFS/ENOMEM/ENOSR/ 1050Sstevel@tonic-gate * EINTR 1060Sstevel@tonic-gate * send: ENOMEM/ENOSR/EINTR ENOBUFS/EINTR ENOBUFS/ENOMEM/ENOSR/ 1070Sstevel@tonic-gate * EINTR 1080Sstevel@tonic-gate * setsockopt: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 1090Sstevel@tonic-gate * shutdown: ENOMEM/ENOSR - ENOBUFS/ENOSR 1100Sstevel@tonic-gate * socket: ENOMEM/ENOSR ENOBUFS ENOBUFS/ENOMEM/ENOSR 1110Sstevel@tonic-gate * socketpair: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 1120Sstevel@tonic-gate * 1130Sstevel@tonic-gate * Resolution. When allocation fails: 1140Sstevel@tonic-gate * recv: return EINTR 1150Sstevel@tonic-gate * send: return EINTR 1160Sstevel@tonic-gate * connect, accept: EINTR 1170Sstevel@tonic-gate * bind, listen, shutdown (unbind, unix_close, disconnect): sleep 1180Sstevel@tonic-gate * socket, socketpair: ENOBUFS 1190Sstevel@tonic-gate * getpeername, getsockname: sleep 1200Sstevel@tonic-gate * getsockopt, setsockopt: sleep 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate #ifdef SOCK_TEST 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * Variables that make sockfs do something other than the standard TPI 1260Sstevel@tonic-gate * for the AF_INET transports. 1270Sstevel@tonic-gate * 1280Sstevel@tonic-gate * solisten_tpi_tcp: 1290Sstevel@tonic-gate * TCP can handle a O_T_BIND_REQ with an increased backlog even though 1300Sstevel@tonic-gate * the transport is already bound. This is needed to avoid loosing the 1310Sstevel@tonic-gate * port number should listen() do a T_UNBIND_REQ followed by a 1320Sstevel@tonic-gate * O_T_BIND_REQ. 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * soconnect_tpi_udp: 1350Sstevel@tonic-gate * UDP and ICMP can handle a T_CONN_REQ. 1360Sstevel@tonic-gate * This is needed to make the sequence of connect(), getsockname() 1370Sstevel@tonic-gate * return the local IP address used to send packets to the connected to 1380Sstevel@tonic-gate * destination. 1390Sstevel@tonic-gate * 1400Sstevel@tonic-gate * soconnect_tpi_tcp: 1410Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ. 1420Sstevel@tonic-gate * Set this to non-zero to send TPI conformant messages to TCP in this 1430Sstevel@tonic-gate * respect. This is a performance optimization. 1440Sstevel@tonic-gate * 1450Sstevel@tonic-gate * soaccept_tpi_tcp: 1460Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without the acceptor being bound. 1470Sstevel@tonic-gate * This is a performance optimization that has been picked up in XTI. 1480Sstevel@tonic-gate * 1490Sstevel@tonic-gate * soaccept_tpi_multioptions: 1500Sstevel@tonic-gate * When inheriting SOL_SOCKET options from the listener to the accepting 1510Sstevel@tonic-gate * socket send them as a single message for AF_INET{,6}. 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate int solisten_tpi_tcp = 0; 1540Sstevel@tonic-gate int soconnect_tpi_udp = 0; 1550Sstevel@tonic-gate int soconnect_tpi_tcp = 0; 1560Sstevel@tonic-gate int soaccept_tpi_tcp = 0; 1570Sstevel@tonic-gate int soaccept_tpi_multioptions = 1; 1580Sstevel@tonic-gate #else /* SOCK_TEST */ 1590Sstevel@tonic-gate #define soconnect_tpi_tcp 0 1600Sstevel@tonic-gate #define soconnect_tpi_udp 0 1610Sstevel@tonic-gate #define solisten_tpi_tcp 0 1620Sstevel@tonic-gate #define soaccept_tpi_tcp 0 1630Sstevel@tonic-gate #define soaccept_tpi_multioptions 1 1640Sstevel@tonic-gate #endif /* SOCK_TEST */ 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate #ifdef SOCK_TEST 1670Sstevel@tonic-gate extern int do_useracc; 1680Sstevel@tonic-gate extern clock_t sock_test_timelimit; 1690Sstevel@tonic-gate #endif /* SOCK_TEST */ 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * Some X/Open added checks might have to be backed out to keep SunOS 4.X 1730Sstevel@tonic-gate * applications working. Turn on this flag to disable these checks. 1740Sstevel@tonic-gate */ 1750Sstevel@tonic-gate int xnet_skip_checks = 0; 1760Sstevel@tonic-gate int xnet_check_print = 0; 1770Sstevel@tonic-gate int xnet_truncate_print = 0; 1780Sstevel@tonic-gate 1798348SEric.Yu@Sun.COM static void sotpi_destroy(struct sonode *); 1808348SEric.Yu@Sun.COM static struct sonode *sotpi_create(struct sockparams *, int, int, int, int, 1818348SEric.Yu@Sun.COM int, int *, cred_t *cr); 1828348SEric.Yu@Sun.COM 1838348SEric.Yu@Sun.COM static boolean_t sotpi_info_create(struct sonode *, int); 1848348SEric.Yu@Sun.COM static void sotpi_info_init(struct sonode *); 1858348SEric.Yu@Sun.COM static void sotpi_info_fini(struct sonode *); 1868348SEric.Yu@Sun.COM static void sotpi_info_destroy(struct sonode *); 1878348SEric.Yu@Sun.COM 1888348SEric.Yu@Sun.COM /* 1898348SEric.Yu@Sun.COM * Do direct function call to the transport layer below; this would 1908348SEric.Yu@Sun.COM * also allow the transport to utilize read-side synchronous stream 1918348SEric.Yu@Sun.COM * interface if necessary. This is a /etc/system tunable that must 1928348SEric.Yu@Sun.COM * not be modified on a running system. By default this is enabled 1938348SEric.Yu@Sun.COM * for performance reasons and may be disabled for debugging purposes. 1948348SEric.Yu@Sun.COM */ 1958348SEric.Yu@Sun.COM boolean_t socktpi_direct = B_TRUE; 1968348SEric.Yu@Sun.COM 1978348SEric.Yu@Sun.COM static struct kmem_cache *socktpi_cache, *socktpi_unix_cache; 1988348SEric.Yu@Sun.COM 1990Sstevel@tonic-gate extern void sigintr(k_sigset_t *, int); 2000Sstevel@tonic-gate extern void sigunintr(k_sigset_t *); 2010Sstevel@tonic-gate 202898Skais /* Sockets acting as an in-kernel SSL proxy */ 203898Skais extern mblk_t *strsock_kssl_input(vnode_t *, mblk_t *, strwakeup_t *, 204898Skais strsigset_t *, strsigset_t *, strpollset_t *); 205898Skais extern mblk_t *strsock_kssl_output(vnode_t *, mblk_t *, strwakeup_t *, 206898Skais strsigset_t *, strsigset_t *, strpollset_t *); 207898Skais 2080Sstevel@tonic-gate static int sotpi_unbind(struct sonode *, int); 2090Sstevel@tonic-gate 2106707Sbrutus extern int sodput(sodirect_t *, mblk_t *); 2116707Sbrutus extern void sodwakeup(sodirect_t *); 2126707Sbrutus 2130Sstevel@tonic-gate /* TPI sockfs sonode operations */ 2148348SEric.Yu@Sun.COM int sotpi_init(struct sonode *, struct sonode *, struct cred *, 2158348SEric.Yu@Sun.COM int); 2168348SEric.Yu@Sun.COM static int sotpi_accept(struct sonode *, int, struct cred *, 2178348SEric.Yu@Sun.COM struct sonode **); 2180Sstevel@tonic-gate static int sotpi_bind(struct sonode *, struct sockaddr *, socklen_t, 2198348SEric.Yu@Sun.COM int, struct cred *); 2208348SEric.Yu@Sun.COM static int sotpi_listen(struct sonode *, int, struct cred *); 2210Sstevel@tonic-gate static int sotpi_connect(struct sonode *, const struct sockaddr *, 2228348SEric.Yu@Sun.COM socklen_t, int, int, struct cred *); 2238348SEric.Yu@Sun.COM extern int sotpi_recvmsg(struct sonode *, struct nmsghdr *, 2248348SEric.Yu@Sun.COM struct uio *, struct cred *); 2250Sstevel@tonic-gate static int sotpi_sendmsg(struct sonode *, struct nmsghdr *, 2268348SEric.Yu@Sun.COM struct uio *, struct cred *); 2278348SEric.Yu@Sun.COM static int sotpi_sendmblk(struct sonode *, struct nmsghdr *, int, 2288348SEric.Yu@Sun.COM struct cred *, mblk_t **); 229741Smasputra static int sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t, 230741Smasputra struct uio *, void *, t_uscalar_t, int); 231741Smasputra static int sodgram_direct(struct sonode *, struct sockaddr *, 232741Smasputra socklen_t, struct uio *, int); 2338348SEric.Yu@Sun.COM extern int sotpi_getpeername(struct sonode *, struct sockaddr *, 2348348SEric.Yu@Sun.COM socklen_t *, boolean_t, struct cred *); 2358348SEric.Yu@Sun.COM static int sotpi_getsockname(struct sonode *, struct sockaddr *, 2368348SEric.Yu@Sun.COM socklen_t *, struct cred *); 2378348SEric.Yu@Sun.COM static int sotpi_shutdown(struct sonode *, int, struct cred *); 2388348SEric.Yu@Sun.COM extern int sotpi_getsockopt(struct sonode *, int, int, void *, 2398348SEric.Yu@Sun.COM socklen_t *, int, struct cred *); 2408348SEric.Yu@Sun.COM extern int sotpi_setsockopt(struct sonode *, int, int, const void *, 2418348SEric.Yu@Sun.COM socklen_t, struct cred *); 2428348SEric.Yu@Sun.COM static int sotpi_ioctl(struct sonode *, int, intptr_t, int, struct cred *, 2438348SEric.Yu@Sun.COM int32_t *); 2448348SEric.Yu@Sun.COM static int socktpi_plumbioctl(struct vnode *, int, intptr_t, int, 2458348SEric.Yu@Sun.COM struct cred *, int32_t *); 2468348SEric.Yu@Sun.COM static int sotpi_poll(struct sonode *, short, int, short *, 2478348SEric.Yu@Sun.COM struct pollhead **); 2488348SEric.Yu@Sun.COM static int sotpi_close(struct sonode *, int, struct cred *); 2498348SEric.Yu@Sun.COM 2508348SEric.Yu@Sun.COM static int i_sotpi_info_constructor(sotpi_info_t *); 2518348SEric.Yu@Sun.COM static void i_sotpi_info_destructor(sotpi_info_t *); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate sonodeops_t sotpi_sonodeops = { 2548348SEric.Yu@Sun.COM sotpi_init, /* sop_init */ 2550Sstevel@tonic-gate sotpi_accept, /* sop_accept */ 2560Sstevel@tonic-gate sotpi_bind, /* sop_bind */ 2570Sstevel@tonic-gate sotpi_listen, /* sop_listen */ 2580Sstevel@tonic-gate sotpi_connect, /* sop_connect */ 2590Sstevel@tonic-gate sotpi_recvmsg, /* sop_recvmsg */ 2600Sstevel@tonic-gate sotpi_sendmsg, /* sop_sendmsg */ 2618348SEric.Yu@Sun.COM sotpi_sendmblk, /* sop_sendmblk */ 2620Sstevel@tonic-gate sotpi_getpeername, /* sop_getpeername */ 2630Sstevel@tonic-gate sotpi_getsockname, /* sop_getsockname */ 2640Sstevel@tonic-gate sotpi_shutdown, /* sop_shutdown */ 2650Sstevel@tonic-gate sotpi_getsockopt, /* sop_getsockopt */ 2668348SEric.Yu@Sun.COM sotpi_setsockopt, /* sop_setsockopt */ 2678348SEric.Yu@Sun.COM sotpi_ioctl, /* sop_ioctl */ 2688348SEric.Yu@Sun.COM sotpi_poll, /* sop_poll */ 2698348SEric.Yu@Sun.COM sotpi_close, /* sop_close */ 2700Sstevel@tonic-gate }; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* 2738348SEric.Yu@Sun.COM * Return a TPI socket vnode. 2748348SEric.Yu@Sun.COM * 2758348SEric.Yu@Sun.COM * Note that sockets assume that the driver will clone (either itself 2768348SEric.Yu@Sun.COM * or by using the clone driver) i.e. a socket() call will always 2778348SEric.Yu@Sun.COM * result in a new vnode being created. 2788348SEric.Yu@Sun.COM */ 2798348SEric.Yu@Sun.COM 2808348SEric.Yu@Sun.COM /* 2810Sstevel@tonic-gate * Common create code for socket and accept. If tso is set the values 2820Sstevel@tonic-gate * from that node is used instead of issuing a T_INFO_REQ. 2830Sstevel@tonic-gate */ 2848348SEric.Yu@Sun.COM 2858348SEric.Yu@Sun.COM /* ARGSUSED */ 2868348SEric.Yu@Sun.COM static struct sonode * 2878348SEric.Yu@Sun.COM sotpi_create(struct sockparams *sp, int family, int type, int protocol, 2888348SEric.Yu@Sun.COM int version, int sflags, int *errorp, cred_t *cr) 2890Sstevel@tonic-gate { 2900Sstevel@tonic-gate struct sonode *so; 2918348SEric.Yu@Sun.COM kmem_cache_t *cp; 2928348SEric.Yu@Sun.COM int sfamily = family; 2938348SEric.Yu@Sun.COM 2948348SEric.Yu@Sun.COM ASSERT(sp->sp_sdev_info.sd_vnode != NULL); 2958348SEric.Yu@Sun.COM 2968348SEric.Yu@Sun.COM if (family == AF_NCA) { 2978348SEric.Yu@Sun.COM /* 2988348SEric.Yu@Sun.COM * The request is for an NCA socket so for NL7C use the 2998348SEric.Yu@Sun.COM * INET domain instead and mark NL7C_AF_NCA below. 3008348SEric.Yu@Sun.COM */ 3018348SEric.Yu@Sun.COM family = AF_INET; 3028348SEric.Yu@Sun.COM /* 3038348SEric.Yu@Sun.COM * NL7C is not supported in the non-global zone, 3048348SEric.Yu@Sun.COM * we enforce this restriction here. 3058348SEric.Yu@Sun.COM */ 3068348SEric.Yu@Sun.COM if (getzoneid() != GLOBAL_ZONEID) { 3078348SEric.Yu@Sun.COM *errorp = ENOTSUP; 3088348SEric.Yu@Sun.COM return (NULL); 3098348SEric.Yu@Sun.COM } 3108348SEric.Yu@Sun.COM } 3118348SEric.Yu@Sun.COM 3128348SEric.Yu@Sun.COM /* 3138348SEric.Yu@Sun.COM * to be compatible with old tpi socket implementation ignore 3148348SEric.Yu@Sun.COM * sleep flag (sflags) passed in 3158348SEric.Yu@Sun.COM */ 3168348SEric.Yu@Sun.COM cp = (family == AF_UNIX) ? socktpi_unix_cache : socktpi_cache; 3178348SEric.Yu@Sun.COM so = kmem_cache_alloc(cp, KM_SLEEP); 3188348SEric.Yu@Sun.COM if (so == NULL) { 3198348SEric.Yu@Sun.COM *errorp = ENOMEM; 3208348SEric.Yu@Sun.COM return (NULL); 3218348SEric.Yu@Sun.COM } 3228348SEric.Yu@Sun.COM 3238348SEric.Yu@Sun.COM sonode_init(so, sp, family, type, protocol, &sotpi_sonodeops); 3248348SEric.Yu@Sun.COM sotpi_info_init(so); 3258348SEric.Yu@Sun.COM 3268348SEric.Yu@Sun.COM if (sfamily == AF_NCA) { 3278348SEric.Yu@Sun.COM SOTOTPI(so)->sti_nl7c_flags = NL7C_AF_NCA; 3288348SEric.Yu@Sun.COM } 3298348SEric.Yu@Sun.COM 3308348SEric.Yu@Sun.COM if (version == SOV_DEFAULT) 3318348SEric.Yu@Sun.COM version = so_default_version; 3328348SEric.Yu@Sun.COM 3338348SEric.Yu@Sun.COM so->so_version = (short)version; 3348348SEric.Yu@Sun.COM *errorp = 0; 3358348SEric.Yu@Sun.COM 3368348SEric.Yu@Sun.COM return (so); 3378348SEric.Yu@Sun.COM } 3388348SEric.Yu@Sun.COM 3398348SEric.Yu@Sun.COM static void 3408348SEric.Yu@Sun.COM sotpi_destroy(struct sonode *so) 3418348SEric.Yu@Sun.COM { 3428348SEric.Yu@Sun.COM kmem_cache_t *cp; 3438348SEric.Yu@Sun.COM struct sockparams *origsp; 3448348SEric.Yu@Sun.COM 3458348SEric.Yu@Sun.COM /* 3468348SEric.Yu@Sun.COM * If there is a new dealloc function (ie. smod_destroy_func), 3478348SEric.Yu@Sun.COM * then it should check the correctness of the ops. 3488348SEric.Yu@Sun.COM */ 3498348SEric.Yu@Sun.COM 3508348SEric.Yu@Sun.COM ASSERT(so->so_ops == &sotpi_sonodeops); 3518348SEric.Yu@Sun.COM 3528348SEric.Yu@Sun.COM origsp = SOTOTPI(so)->sti_orig_sp; 3538348SEric.Yu@Sun.COM 3548348SEric.Yu@Sun.COM sotpi_info_fini(so); 3558348SEric.Yu@Sun.COM 3568348SEric.Yu@Sun.COM if (so->so_state & SS_FALLBACK_COMP) { 3578348SEric.Yu@Sun.COM /* 3588348SEric.Yu@Sun.COM * A fallback happend, which means that a sotpi_info_t struct 3598348SEric.Yu@Sun.COM * was allocated (as opposed to being allocated from the TPI 3608348SEric.Yu@Sun.COM * sonode cache. Therefore we explicitly free the struct 3618348SEric.Yu@Sun.COM * here. 3628348SEric.Yu@Sun.COM */ 3638348SEric.Yu@Sun.COM sotpi_info_destroy(so); 3648348SEric.Yu@Sun.COM ASSERT(origsp != NULL); 3658348SEric.Yu@Sun.COM 3668348SEric.Yu@Sun.COM origsp->sp_smod_info->smod_sock_destroy_func(so); 3678348SEric.Yu@Sun.COM SOCKPARAMS_DEC_REF(origsp); 3688348SEric.Yu@Sun.COM } else { 3698348SEric.Yu@Sun.COM sonode_fini(so); 3708348SEric.Yu@Sun.COM cp = (so->so_family == AF_UNIX) ? socktpi_unix_cache : 3718348SEric.Yu@Sun.COM socktpi_cache; 3728348SEric.Yu@Sun.COM kmem_cache_free(cp, so); 3738348SEric.Yu@Sun.COM } 3748348SEric.Yu@Sun.COM } 3758348SEric.Yu@Sun.COM 3768348SEric.Yu@Sun.COM /* ARGSUSED1 */ 3778348SEric.Yu@Sun.COM int 3788348SEric.Yu@Sun.COM sotpi_init(struct sonode *so, struct sonode *tso, struct cred *cr, int flags) 3798348SEric.Yu@Sun.COM { 3808348SEric.Yu@Sun.COM major_t maj; 3818348SEric.Yu@Sun.COM dev_t newdev; 3828348SEric.Yu@Sun.COM struct vnode *vp; 3838348SEric.Yu@Sun.COM int error = 0; 3848348SEric.Yu@Sun.COM struct stdata *stp; 3858348SEric.Yu@Sun.COM 3868348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 3878348SEric.Yu@Sun.COM 3888348SEric.Yu@Sun.COM dprint(1, ("sotpi_init()\n")); 3898348SEric.Yu@Sun.COM 3908348SEric.Yu@Sun.COM /* 3918348SEric.Yu@Sun.COM * over write the sleep flag passed in but that is ok 3928348SEric.Yu@Sun.COM * as tpi socket does not honor sleep flag. 3938348SEric.Yu@Sun.COM */ 3948348SEric.Yu@Sun.COM flags |= FREAD|FWRITE; 3958348SEric.Yu@Sun.COM 3968348SEric.Yu@Sun.COM /* 3978348SEric.Yu@Sun.COM * Record in so_flag that it is a clone. 3988348SEric.Yu@Sun.COM */ 3998348SEric.Yu@Sun.COM if (getmajor(sti->sti_dev) == clone_major) 4008348SEric.Yu@Sun.COM so->so_flag |= SOCLONE; 4018348SEric.Yu@Sun.COM 4028348SEric.Yu@Sun.COM if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM) && 4038348SEric.Yu@Sun.COM (so->so_family == AF_INET || so->so_family == AF_INET6) && 4048348SEric.Yu@Sun.COM (so->so_protocol == IPPROTO_TCP || so->so_protocol == IPPROTO_UDP || 4058348SEric.Yu@Sun.COM so->so_protocol == IPPROTO_IP)) { 406741Smasputra /* Tell tcp or udp that it's talking to sockets */ 407741Smasputra flags |= SO_SOCKSTR; 408741Smasputra 409741Smasputra /* 410741Smasputra * Here we indicate to socktpi_open() our attempt to 411741Smasputra * make direct calls between sockfs and transport. 412741Smasputra * The final decision is left to socktpi_open(). 413741Smasputra */ 4148348SEric.Yu@Sun.COM sti->sti_direct = 1; 415741Smasputra 416741Smasputra ASSERT(so->so_type != SOCK_DGRAM || tso == NULL); 417741Smasputra if (so->so_type == SOCK_STREAM && tso != NULL) { 4188348SEric.Yu@Sun.COM if (SOTOTPI(tso)->sti_direct) { 419741Smasputra /* 4208348SEric.Yu@Sun.COM * Inherit sti_direct from listener and pass 421741Smasputra * SO_ACCEPTOR open flag to tcp, indicating 422741Smasputra * that this is an accept fast-path instance. 423741Smasputra */ 424741Smasputra flags |= SO_ACCEPTOR; 425741Smasputra } else { 426741Smasputra /* 4278348SEric.Yu@Sun.COM * sti_direct is not set on listener, meaning 428741Smasputra * that the listener has been converted from 429741Smasputra * a socket to a stream. Ensure that the 430741Smasputra * acceptor inherits these settings. 431741Smasputra */ 4328348SEric.Yu@Sun.COM sti->sti_direct = 0; 433741Smasputra flags &= ~SO_SOCKSTR; 434741Smasputra } 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* 4390Sstevel@tonic-gate * Tell local transport that it is talking to sockets. 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 4420Sstevel@tonic-gate flags |= SO_SOCKSTR; 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate 4458348SEric.Yu@Sun.COM vp = SOTOV(so); 4468348SEric.Yu@Sun.COM newdev = vp->v_rdev; 4478348SEric.Yu@Sun.COM maj = getmajor(newdev); 4488348SEric.Yu@Sun.COM ASSERT(STREAMSTAB(maj)); 4498348SEric.Yu@Sun.COM 4508348SEric.Yu@Sun.COM error = stropen(vp, &newdev, flags, cr); 4518348SEric.Yu@Sun.COM 4528348SEric.Yu@Sun.COM stp = vp->v_stream; 4538348SEric.Yu@Sun.COM if (error == 0) { 4548348SEric.Yu@Sun.COM if (so->so_flag & SOCLONE) 4558348SEric.Yu@Sun.COM ASSERT(newdev != vp->v_rdev); 4568348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 4578348SEric.Yu@Sun.COM sti->sti_dev = newdev; 4588348SEric.Yu@Sun.COM vp->v_rdev = newdev; 4598348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 4608348SEric.Yu@Sun.COM 4618348SEric.Yu@Sun.COM if (stp->sd_flag & STRISTTY) { 4628348SEric.Yu@Sun.COM /* 4638348SEric.Yu@Sun.COM * this is a post SVR4 tty driver - a socket can not 4648348SEric.Yu@Sun.COM * be a controlling terminal. Fail the open. 4658348SEric.Yu@Sun.COM */ 4668348SEric.Yu@Sun.COM (void) sotpi_close(so, flags, cr); 4678348SEric.Yu@Sun.COM return (ENOTTY); /* XXX */ 4688348SEric.Yu@Sun.COM } 4698348SEric.Yu@Sun.COM 4708348SEric.Yu@Sun.COM ASSERT(stp->sd_wrq != NULL); 4718348SEric.Yu@Sun.COM sti->sti_provinfo = tpi_findprov(stp->sd_wrq); 4728348SEric.Yu@Sun.COM 4738348SEric.Yu@Sun.COM /* 4748348SEric.Yu@Sun.COM * If caller is interested in doing direct function call 4758348SEric.Yu@Sun.COM * interface to/from transport module, probe the module 4768348SEric.Yu@Sun.COM * directly beneath the streamhead to see if it qualifies. 4778348SEric.Yu@Sun.COM * 4788348SEric.Yu@Sun.COM * We turn off the direct interface when qualifications fail. 4798348SEric.Yu@Sun.COM * In the acceptor case, we simply turn off the sti_direct 4808348SEric.Yu@Sun.COM * flag on the socket. We do the fallback after the accept 4818348SEric.Yu@Sun.COM * has completed, before the new socket is returned to the 4828348SEric.Yu@Sun.COM * application. 4838348SEric.Yu@Sun.COM */ 4848348SEric.Yu@Sun.COM if (sti->sti_direct) { 4858348SEric.Yu@Sun.COM queue_t *tq = stp->sd_wrq->q_next; 4868348SEric.Yu@Sun.COM 4878348SEric.Yu@Sun.COM /* 4888348SEric.Yu@Sun.COM * sti_direct is currently supported and tested 4898348SEric.Yu@Sun.COM * only for tcp/udp; this is the main reason to 4908348SEric.Yu@Sun.COM * have the following assertions. 4918348SEric.Yu@Sun.COM */ 4928348SEric.Yu@Sun.COM ASSERT(so->so_family == AF_INET || 4938348SEric.Yu@Sun.COM so->so_family == AF_INET6); 4948348SEric.Yu@Sun.COM ASSERT(so->so_protocol == IPPROTO_UDP || 4958348SEric.Yu@Sun.COM so->so_protocol == IPPROTO_TCP || 4968348SEric.Yu@Sun.COM so->so_protocol == IPPROTO_IP); 4978348SEric.Yu@Sun.COM ASSERT(so->so_type == SOCK_DGRAM || 4988348SEric.Yu@Sun.COM so->so_type == SOCK_STREAM); 4998348SEric.Yu@Sun.COM 5008348SEric.Yu@Sun.COM /* 5018348SEric.Yu@Sun.COM * Abort direct call interface if the module directly 5028348SEric.Yu@Sun.COM * underneath the stream head is not defined with the 5038348SEric.Yu@Sun.COM * _D_DIRECT flag. This could happen in the tcp or 5048348SEric.Yu@Sun.COM * udp case, when some other module is autopushed 5058348SEric.Yu@Sun.COM * above it, or for some reasons the expected module 5068348SEric.Yu@Sun.COM * isn't purely D_MP (which is the main requirement). 5078348SEric.Yu@Sun.COM * 5088348SEric.Yu@Sun.COM * Else, SS_DIRECT is valid. If the read-side Q has 5098348SEric.Yu@Sun.COM * _QSODIRECT set then and uioasync is enabled then 5108348SEric.Yu@Sun.COM * set SS_SODIRECT to enable sodirect. 5118348SEric.Yu@Sun.COM */ 5128348SEric.Yu@Sun.COM if (!socktpi_direct || !(tq->q_flag & _QDIRECT) || 5138348SEric.Yu@Sun.COM !(_OTHERQ(tq)->q_flag & _QDIRECT)) { 5148348SEric.Yu@Sun.COM int rval; 5158348SEric.Yu@Sun.COM 5168348SEric.Yu@Sun.COM /* Continue on without direct calls */ 5178348SEric.Yu@Sun.COM sti->sti_direct = 0; 5188348SEric.Yu@Sun.COM 5198348SEric.Yu@Sun.COM /* 5208348SEric.Yu@Sun.COM * Cannot issue ioctl on fallback socket since 5218348SEric.Yu@Sun.COM * there is no conn associated with the queue. 5228348SEric.Yu@Sun.COM * The fallback downcall will notify the proto 5238348SEric.Yu@Sun.COM * of the change. 5248348SEric.Yu@Sun.COM */ 5258348SEric.Yu@Sun.COM if (!(flags & SO_ACCEPTOR) && 5268348SEric.Yu@Sun.COM !(flags & SO_FALLBACK)) { 5278348SEric.Yu@Sun.COM if ((error = strioctl(vp, 5288348SEric.Yu@Sun.COM _SIOCSOCKFALLBACK, 0, 0, K_TO_K, 5298348SEric.Yu@Sun.COM cr, &rval)) != 0) { 5308348SEric.Yu@Sun.COM (void) sotpi_close(so, flags, 5318348SEric.Yu@Sun.COM cr); 5328348SEric.Yu@Sun.COM return (error); 5338348SEric.Yu@Sun.COM } 5348348SEric.Yu@Sun.COM } 5358348SEric.Yu@Sun.COM } else if ((_OTHERQ(tq)->q_flag & _QSODIRECT) && 5368348SEric.Yu@Sun.COM uioasync.enabled) { 5378348SEric.Yu@Sun.COM /* Enable sodirect */ 5388348SEric.Yu@Sun.COM so->so_state |= SS_SODIRECT; 5398348SEric.Yu@Sun.COM } 5408348SEric.Yu@Sun.COM } 5418348SEric.Yu@Sun.COM 5428348SEric.Yu@Sun.COM if (flags & SO_FALLBACK) { 5438348SEric.Yu@Sun.COM /* 5448348SEric.Yu@Sun.COM * The stream created does not have a conn. 5458348SEric.Yu@Sun.COM * do stream set up after conn has been assigned 5468348SEric.Yu@Sun.COM */ 5478348SEric.Yu@Sun.COM return (error); 5488348SEric.Yu@Sun.COM } 5498348SEric.Yu@Sun.COM if (error = so_strinit(so, tso)) { 5508348SEric.Yu@Sun.COM (void) sotpi_close(so, flags, cr); 5518348SEric.Yu@Sun.COM return (error); 5528348SEric.Yu@Sun.COM } 5538348SEric.Yu@Sun.COM 5548348SEric.Yu@Sun.COM /* Wildcard */ 5558348SEric.Yu@Sun.COM if (so->so_protocol != so->so_sockparams->sp_protocol) { 5568348SEric.Yu@Sun.COM int protocol = so->so_protocol; 5578348SEric.Yu@Sun.COM /* 5588348SEric.Yu@Sun.COM * Issue SO_PROTOTYPE setsockopt. 5598348SEric.Yu@Sun.COM */ 5608348SEric.Yu@Sun.COM error = sotpi_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE, 5618348SEric.Yu@Sun.COM &protocol, (t_uscalar_t)sizeof (protocol), cr); 5628348SEric.Yu@Sun.COM if (error != 0) { 5638348SEric.Yu@Sun.COM (void) sotpi_close(so, flags, cr); 5648348SEric.Yu@Sun.COM /* 5658348SEric.Yu@Sun.COM * Setsockopt often fails with ENOPROTOOPT but 5668348SEric.Yu@Sun.COM * socket() should fail with 5678348SEric.Yu@Sun.COM * EPROTONOSUPPORT/EPROTOTYPE. 5688348SEric.Yu@Sun.COM */ 5698348SEric.Yu@Sun.COM return (EPROTONOSUPPORT); 5708348SEric.Yu@Sun.COM } 5718348SEric.Yu@Sun.COM } 5728348SEric.Yu@Sun.COM 5738348SEric.Yu@Sun.COM } else { 5748348SEric.Yu@Sun.COM /* 5758348SEric.Yu@Sun.COM * While the same socket can not be reopened (unlike specfs) 5768348SEric.Yu@Sun.COM * the stream head sets STREOPENFAIL when the autopush fails. 5778348SEric.Yu@Sun.COM */ 5788348SEric.Yu@Sun.COM if ((stp != NULL) && 5798348SEric.Yu@Sun.COM (stp->sd_flag & STREOPENFAIL)) { 5808348SEric.Yu@Sun.COM /* 5818348SEric.Yu@Sun.COM * Open failed part way through. 5828348SEric.Yu@Sun.COM */ 5838348SEric.Yu@Sun.COM mutex_enter(&stp->sd_lock); 5848348SEric.Yu@Sun.COM stp->sd_flag &= ~STREOPENFAIL; 5858348SEric.Yu@Sun.COM mutex_exit(&stp->sd_lock); 5868348SEric.Yu@Sun.COM (void) sotpi_close(so, flags, cr); 5878348SEric.Yu@Sun.COM return (error); 5888348SEric.Yu@Sun.COM /*NOTREACHED*/ 5898348SEric.Yu@Sun.COM } 5908348SEric.Yu@Sun.COM ASSERT(stp == NULL); 5918348SEric.Yu@Sun.COM } 5928348SEric.Yu@Sun.COM TRACE_4(TR_FAC_SOCKFS, TR_SOCKFS_OPEN, 5938348SEric.Yu@Sun.COM "sockfs open:maj %d vp %p so %p error %d", 5948348SEric.Yu@Sun.COM maj, vp, so, error); 5958348SEric.Yu@Sun.COM return (error); 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate /* 5990Sstevel@tonic-gate * Bind the socket to an unspecified address in sockfs only. 6000Sstevel@tonic-gate * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't 6010Sstevel@tonic-gate * required in all cases. 6020Sstevel@tonic-gate */ 6030Sstevel@tonic-gate static void 6040Sstevel@tonic-gate so_automatic_bind(struct sonode *so) 6050Sstevel@tonic-gate { 6068348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 6070Sstevel@tonic-gate ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 6100Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 6118348SEric.Yu@Sun.COM ASSERT(sti->sti_unbind_mp); 6128348SEric.Yu@Sun.COM 6138348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 6148348SEric.Yu@Sun.COM bzero(sti->sti_laddr_sa, sti->sti_laddr_len); 6158348SEric.Yu@Sun.COM sti->sti_laddr_sa->sa_family = so->so_family; 6160Sstevel@tonic-gate so->so_state |= SS_ISBOUND; 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate /* 6210Sstevel@tonic-gate * bind the socket. 6220Sstevel@tonic-gate * 6230Sstevel@tonic-gate * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2 6240Sstevel@tonic-gate * are passed in we allow rebinding. Note that for backwards compatibility 6250Sstevel@tonic-gate * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind. 6260Sstevel@tonic-gate * Thus the rebinding code is currently not executed. 6270Sstevel@tonic-gate * 6280Sstevel@tonic-gate * The constraints for rebinding are: 6290Sstevel@tonic-gate * - it is a SOCK_DGRAM, or 6300Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 6310Sstevel@tonic-gate * and no listen() has been done. 6320Sstevel@tonic-gate * This rebinding code was added based on some language in the XNET book 6330Sstevel@tonic-gate * about not returning EINVAL it the protocol allows rebinding. However, 6340Sstevel@tonic-gate * this language is not present in the Posix socket draft. Thus maybe the 6350Sstevel@tonic-gate * rebinding logic should be deleted from the source. 6360Sstevel@tonic-gate * 6370Sstevel@tonic-gate * A null "name" can be used to unbind the socket if: 6380Sstevel@tonic-gate * - it is a SOCK_DGRAM, or 6390Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 6400Sstevel@tonic-gate * and no listen() has been done. 6410Sstevel@tonic-gate */ 6428348SEric.Yu@Sun.COM /* ARGSUSED */ 6430Sstevel@tonic-gate static int 6440Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name, 6458348SEric.Yu@Sun.COM socklen_t namelen, int backlog, int flags, struct cred *cr) 6460Sstevel@tonic-gate { 6470Sstevel@tonic-gate struct T_bind_req bind_req; 6480Sstevel@tonic-gate struct T_bind_ack *bind_ack; 6490Sstevel@tonic-gate int error = 0; 6500Sstevel@tonic-gate mblk_t *mp; 6510Sstevel@tonic-gate void *addr; 6520Sstevel@tonic-gate t_uscalar_t addrlen; 6530Sstevel@tonic-gate int unbind_on_err = 1; 6540Sstevel@tonic-gate boolean_t clear_acceptconn_on_err = B_FALSE; 6550Sstevel@tonic-gate boolean_t restore_backlog_on_err = B_FALSE; 6560Sstevel@tonic-gate int save_so_backlog; 6570Sstevel@tonic-gate t_scalar_t PRIM_type = O_T_BIND_REQ; 6580Sstevel@tonic-gate boolean_t tcp_udp_xport; 6590Sstevel@tonic-gate void *nl7c = NULL; 6608348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n", 6637240Srh87107 (void *)so, (void *)name, namelen, backlog, flags, 6645240Snordmark pr_state(so->so_state, so->so_mode))); 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) { 6690Sstevel@tonic-gate mutex_enter(&so->so_lock); 6700Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 6710Sstevel@tonic-gate } else { 6720Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 6730Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate /* 6770Sstevel@tonic-gate * Make sure that there is a preallocated unbind_req message 6780Sstevel@tonic-gate * before binding. This message allocated when the socket is 6790Sstevel@tonic-gate * created but it might be have been consumed. 6800Sstevel@tonic-gate */ 6818348SEric.Yu@Sun.COM if (sti->sti_unbind_mp == NULL) { 6820Sstevel@tonic-gate dprintso(so, 1, ("sobind: allocating unbind_req\n")); 6830Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 6848348SEric.Yu@Sun.COM sti->sti_unbind_mp = 6850Sstevel@tonic-gate soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate if (flags & _SOBIND_REBIND) { 6890Sstevel@tonic-gate /* 6900Sstevel@tonic-gate * Called from solisten after doing an sotpi_unbind() or 6910Sstevel@tonic-gate * potentially without the unbind (latter for AF_INET{,6}). 6920Sstevel@tonic-gate */ 6930Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0); 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 6968348SEric.Yu@Sun.COM ASSERT(sti->sti_ux_bound_vp); 6978348SEric.Yu@Sun.COM addr = &sti->sti_ux_laddr; 6988348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr); 6995240Snordmark dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, " 7005240Snordmark "addr 0x%p, vp %p\n", 7010Sstevel@tonic-gate addrlen, 7027240Srh87107 (void *)((struct so_ux_addr *)addr)->soua_vp, 7038348SEric.Yu@Sun.COM (void *)sti->sti_ux_bound_vp)); 7040Sstevel@tonic-gate } else { 7058348SEric.Yu@Sun.COM addr = sti->sti_laddr_sa; 7068348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sti->sti_laddr_len; 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate } else if (flags & _SOBIND_UNSPEC) { 7090Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0); 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate /* 7120Sstevel@tonic-gate * The caller checked SS_ISBOUND but not necessarily 7130Sstevel@tonic-gate * under so_lock 7140Sstevel@tonic-gate */ 7150Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) { 7160Sstevel@tonic-gate /* No error */ 7170Sstevel@tonic-gate goto done; 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate /* Set an initial local address */ 7210Sstevel@tonic-gate switch (so->so_family) { 7220Sstevel@tonic-gate case AF_UNIX: 7230Sstevel@tonic-gate /* 7240Sstevel@tonic-gate * Use an address with same size as struct sockaddr 7250Sstevel@tonic-gate * just like BSD. 7260Sstevel@tonic-gate */ 7278348SEric.Yu@Sun.COM sti->sti_laddr_len = 7285240Snordmark (socklen_t)sizeof (struct sockaddr); 7298348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 7308348SEric.Yu@Sun.COM bzero(sti->sti_laddr_sa, sti->sti_laddr_len); 7318348SEric.Yu@Sun.COM sti->sti_laddr_sa->sa_family = so->so_family; 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * Pass down an address with the implicit bind 7350Sstevel@tonic-gate * magic number and the rest all zeros. 7360Sstevel@tonic-gate * The transport will return a unique address. 7370Sstevel@tonic-gate */ 7388348SEric.Yu@Sun.COM sti->sti_ux_laddr.soua_vp = NULL; 7398348SEric.Yu@Sun.COM sti->sti_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT; 7408348SEric.Yu@Sun.COM addr = &sti->sti_ux_laddr; 7418348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr); 7420Sstevel@tonic-gate break; 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate case AF_INET: 7450Sstevel@tonic-gate case AF_INET6: 7460Sstevel@tonic-gate /* 7470Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address. 7480Sstevel@tonic-gate * Set the address in sockfs to have the sa_family. 7490Sstevel@tonic-gate */ 7508348SEric.Yu@Sun.COM sti->sti_laddr_len = (so->so_family == AF_INET) ? 7510Sstevel@tonic-gate (socklen_t)sizeof (sin_t) : 7520Sstevel@tonic-gate (socklen_t)sizeof (sin6_t); 7538348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 7548348SEric.Yu@Sun.COM bzero(sti->sti_laddr_sa, sti->sti_laddr_len); 7558348SEric.Yu@Sun.COM sti->sti_laddr_sa->sa_family = so->so_family; 7560Sstevel@tonic-gate addr = NULL; 7570Sstevel@tonic-gate addrlen = 0; 7580Sstevel@tonic-gate break; 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate default: 7610Sstevel@tonic-gate /* 7620Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address. 7630Sstevel@tonic-gate * Set the address in sockfs to be zero length. 7640Sstevel@tonic-gate * 7650Sstevel@tonic-gate * Can not assume there is a sa_family for all 7660Sstevel@tonic-gate * protocol families. For example, AF_X25 does not 7670Sstevel@tonic-gate * have a family field. 7680Sstevel@tonic-gate */ 7698348SEric.Yu@Sun.COM bzero(sti->sti_laddr_sa, sti->sti_laddr_len); 7708348SEric.Yu@Sun.COM sti->sti_laddr_len = 0; /* XXX correct? */ 7710Sstevel@tonic-gate addr = NULL; 7720Sstevel@tonic-gate addrlen = 0; 7730Sstevel@tonic-gate break; 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate } else { 7770Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) { 7780Sstevel@tonic-gate /* 7790Sstevel@tonic-gate * If it is ok to rebind the socket, first unbind 7800Sstevel@tonic-gate * with the transport. A rebind to the NULL address 7810Sstevel@tonic-gate * is interpreted as an unbind. 7820Sstevel@tonic-gate * Note that a bind to NULL in BSD does unbind the 7830Sstevel@tonic-gate * socket but it fails with EINVAL. 7840Sstevel@tonic-gate * Note that regular sockets set SOV_SOCKBSD i.e. 7850Sstevel@tonic-gate * _SOBIND_SOCKBSD gets set here hence no type of 7860Sstevel@tonic-gate * socket does currently allow rebinding. 7870Sstevel@tonic-gate * 7880Sstevel@tonic-gate * If the name is NULL just do an unbind. 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) && 7910Sstevel@tonic-gate name != NULL) { 7920Sstevel@tonic-gate error = EINVAL; 7930Sstevel@tonic-gate unbind_on_err = 0; 7940Sstevel@tonic-gate eprintsoline(so, error); 7950Sstevel@tonic-gate goto done; 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate if ((so->so_mode & SM_CONNREQUIRED) && 7980Sstevel@tonic-gate (so->so_state & SS_CANTREBIND)) { 7990Sstevel@tonic-gate error = EINVAL; 8000Sstevel@tonic-gate unbind_on_err = 0; 8010Sstevel@tonic-gate eprintsoline(so, error); 8020Sstevel@tonic-gate goto done; 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate error = sotpi_unbind(so, 0); 8050Sstevel@tonic-gate if (error) { 8060Sstevel@tonic-gate eprintsoline(so, error); 8070Sstevel@tonic-gate goto done; 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 8100Sstevel@tonic-gate if (name == NULL) { 8110Sstevel@tonic-gate so->so_state &= 8125240Snordmark ~(SS_ISCONNECTED|SS_ISCONNECTING); 8130Sstevel@tonic-gate goto done; 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate } 8168348SEric.Yu@Sun.COM 8170Sstevel@tonic-gate /* X/Open requires this check */ 8180Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 8190Sstevel@tonic-gate if (xnet_check_print) { 8200Sstevel@tonic-gate printf("sockfs: X/Open bind state check " 8210Sstevel@tonic-gate "caused EINVAL\n"); 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate error = EINVAL; 8240Sstevel@tonic-gate goto done; 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate switch (so->so_family) { 8280Sstevel@tonic-gate case AF_UNIX: 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * All AF_UNIX addresses are nul terminated 8310Sstevel@tonic-gate * when copied (copyin_name) in so the minimum 8320Sstevel@tonic-gate * length is 3 bytes. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate if (name == NULL || 8350Sstevel@tonic-gate (ssize_t)namelen <= sizeof (short) + 1) { 8360Sstevel@tonic-gate error = EISDIR; 8370Sstevel@tonic-gate eprintsoline(so, error); 8380Sstevel@tonic-gate goto done; 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate /* 8410Sstevel@tonic-gate * Verify so_family matches the bound family. 8420Sstevel@tonic-gate * BSD does not check this for AF_UNIX resulting 8430Sstevel@tonic-gate * in funny mknods. 8440Sstevel@tonic-gate */ 8450Sstevel@tonic-gate if (name->sa_family != so->so_family) { 8460Sstevel@tonic-gate error = EAFNOSUPPORT; 8470Sstevel@tonic-gate goto done; 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate break; 8500Sstevel@tonic-gate case AF_INET: 8510Sstevel@tonic-gate if (name == NULL) { 8520Sstevel@tonic-gate error = EINVAL; 8530Sstevel@tonic-gate eprintsoline(so, error); 8540Sstevel@tonic-gate goto done; 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin_t)) { 8570Sstevel@tonic-gate error = name->sa_family != so->so_family ? 8580Sstevel@tonic-gate EAFNOSUPPORT : EINVAL; 8590Sstevel@tonic-gate eprintsoline(so, error); 8600Sstevel@tonic-gate goto done; 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate if ((flags & _SOBIND_XPG4_2) && 8630Sstevel@tonic-gate (name->sa_family != so->so_family)) { 8640Sstevel@tonic-gate /* 8650Sstevel@tonic-gate * This check has to be made for X/Open 8660Sstevel@tonic-gate * sockets however application failures have 8670Sstevel@tonic-gate * been observed when it is applied to 8680Sstevel@tonic-gate * all sockets. 8690Sstevel@tonic-gate */ 8700Sstevel@tonic-gate error = EAFNOSUPPORT; 8710Sstevel@tonic-gate eprintsoline(so, error); 8720Sstevel@tonic-gate goto done; 8730Sstevel@tonic-gate } 8740Sstevel@tonic-gate /* 8750Sstevel@tonic-gate * Force a zero sa_family to match so_family. 8760Sstevel@tonic-gate * 8770Sstevel@tonic-gate * Some programs like inetd(1M) don't set the 8780Sstevel@tonic-gate * family field. Other programs leave 8790Sstevel@tonic-gate * sin_family set to garbage - SunOS 4.X does 8800Sstevel@tonic-gate * not check the family field on a bind. 8810Sstevel@tonic-gate * We use the family field that 8820Sstevel@tonic-gate * was passed in to the socket() call. 8830Sstevel@tonic-gate */ 8840Sstevel@tonic-gate name->sa_family = so->so_family; 8850Sstevel@tonic-gate break; 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate case AF_INET6: { 8880Sstevel@tonic-gate #ifdef DEBUG 8890Sstevel@tonic-gate sin6_t *sin6 = (sin6_t *)name; 8900Sstevel@tonic-gate #endif /* DEBUG */ 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate if (name == NULL) { 8930Sstevel@tonic-gate error = EINVAL; 8940Sstevel@tonic-gate eprintsoline(so, error); 8950Sstevel@tonic-gate goto done; 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin6_t)) { 8980Sstevel@tonic-gate error = name->sa_family != so->so_family ? 8990Sstevel@tonic-gate EAFNOSUPPORT : EINVAL; 9000Sstevel@tonic-gate eprintsoline(so, error); 9010Sstevel@tonic-gate goto done; 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate if (name->sa_family != so->so_family) { 9040Sstevel@tonic-gate /* 9050Sstevel@tonic-gate * With IPv6 we require the family to match 9060Sstevel@tonic-gate * unlike in IPv4. 9070Sstevel@tonic-gate */ 9080Sstevel@tonic-gate error = EAFNOSUPPORT; 9090Sstevel@tonic-gate eprintsoline(so, error); 9100Sstevel@tonic-gate goto done; 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate #ifdef DEBUG 9130Sstevel@tonic-gate /* 9140Sstevel@tonic-gate * Verify that apps don't forget to clear 9150Sstevel@tonic-gate * sin6_scope_id etc 9160Sstevel@tonic-gate */ 9170Sstevel@tonic-gate if (sin6->sin6_scope_id != 0 && 9180Sstevel@tonic-gate !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 9191548Srshoaib zcmn_err(getzoneid(), CE_WARN, 9200Sstevel@tonic-gate "bind with uninitialized sin6_scope_id " 9210Sstevel@tonic-gate "(%d) on socket. Pid = %d\n", 9220Sstevel@tonic-gate (int)sin6->sin6_scope_id, 9230Sstevel@tonic-gate (int)curproc->p_pid); 9240Sstevel@tonic-gate } 9250Sstevel@tonic-gate if (sin6->__sin6_src_id != 0) { 9261548Srshoaib zcmn_err(getzoneid(), CE_WARN, 9270Sstevel@tonic-gate "bind with uninitialized __sin6_src_id " 9280Sstevel@tonic-gate "(%d) on socket. Pid = %d\n", 9290Sstevel@tonic-gate (int)sin6->__sin6_src_id, 9300Sstevel@tonic-gate (int)curproc->p_pid); 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate #endif /* DEBUG */ 9330Sstevel@tonic-gate break; 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate default: 9360Sstevel@tonic-gate /* 9370Sstevel@tonic-gate * Don't do any length or sa_family check to allow 9380Sstevel@tonic-gate * non-sockaddr style addresses. 9390Sstevel@tonic-gate */ 9400Sstevel@tonic-gate if (name == NULL) { 9410Sstevel@tonic-gate error = EINVAL; 9420Sstevel@tonic-gate eprintsoline(so, error); 9430Sstevel@tonic-gate goto done; 9440Sstevel@tonic-gate } 9450Sstevel@tonic-gate break; 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9488348SEric.Yu@Sun.COM if (namelen > (t_uscalar_t)sti->sti_laddr_maxlen) { 9490Sstevel@tonic-gate error = ENAMETOOLONG; 9500Sstevel@tonic-gate eprintsoline(so, error); 9510Sstevel@tonic-gate goto done; 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate /* 9540Sstevel@tonic-gate * Save local address. 9550Sstevel@tonic-gate */ 9568348SEric.Yu@Sun.COM sti->sti_laddr_len = (socklen_t)namelen; 9578348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 9588348SEric.Yu@Sun.COM bcopy(name, sti->sti_laddr_sa, namelen); 9598348SEric.Yu@Sun.COM 9608348SEric.Yu@Sun.COM addr = sti->sti_laddr_sa; 9618348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sti->sti_laddr_len; 9620Sstevel@tonic-gate switch (so->so_family) { 9630Sstevel@tonic-gate case AF_INET6: 9640Sstevel@tonic-gate case AF_INET: 9650Sstevel@tonic-gate break; 9660Sstevel@tonic-gate case AF_UNIX: { 9670Sstevel@tonic-gate struct sockaddr_un *soun = 9688348SEric.Yu@Sun.COM (struct sockaddr_un *)sti->sti_laddr_sa; 9698032SRic.Aleshire@Sun.COM struct vnode *vp, *rvp; 9700Sstevel@tonic-gate struct vattr vattr; 9710Sstevel@tonic-gate 9728348SEric.Yu@Sun.COM ASSERT(sti->sti_ux_bound_vp == NULL); 9730Sstevel@tonic-gate /* 9740Sstevel@tonic-gate * Create vnode for the specified path name. 9758348SEric.Yu@Sun.COM * Keep vnode held with a reference in sti_ux_bound_vp. 9760Sstevel@tonic-gate * Use the vnode pointer as the address used in the 9770Sstevel@tonic-gate * bind with the transport. 9780Sstevel@tonic-gate * 9790Sstevel@tonic-gate * Use the same mode as in BSD. In particular this does 9800Sstevel@tonic-gate * not observe the umask. 9810Sstevel@tonic-gate */ 9820Sstevel@tonic-gate /* MAXPATHLEN + soun_family + nul termination */ 9838348SEric.Yu@Sun.COM if (sti->sti_laddr_len > 9840Sstevel@tonic-gate (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) { 9850Sstevel@tonic-gate error = ENAMETOOLONG; 9860Sstevel@tonic-gate eprintsoline(so, error); 9870Sstevel@tonic-gate goto done; 9880Sstevel@tonic-gate } 9890Sstevel@tonic-gate vattr.va_type = VSOCK; 9903446Smrj vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask; 9910Sstevel@tonic-gate vattr.va_mask = AT_TYPE|AT_MODE; 9920Sstevel@tonic-gate /* NOTE: holding so_lock */ 9930Sstevel@tonic-gate error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr, 9945240Snordmark EXCL, 0, &vp, CRMKNOD, 0, 0); 9950Sstevel@tonic-gate if (error) { 9960Sstevel@tonic-gate if (error == EEXIST) 9970Sstevel@tonic-gate error = EADDRINUSE; 9980Sstevel@tonic-gate eprintsoline(so, error); 9990Sstevel@tonic-gate goto done; 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate /* 10020Sstevel@tonic-gate * Establish pointer from the underlying filesystem 10030Sstevel@tonic-gate * vnode to the socket node. 10048348SEric.Yu@Sun.COM * sti_ux_bound_vp and v_stream->sd_vnode form the 10050Sstevel@tonic-gate * cross-linkage between the underlying filesystem 10060Sstevel@tonic-gate * node and the socket node. 10070Sstevel@tonic-gate */ 10088032SRic.Aleshire@Sun.COM 10098032SRic.Aleshire@Sun.COM if ((VOP_REALVP(vp, &rvp, NULL) == 0) && (vp != rvp)) { 10108032SRic.Aleshire@Sun.COM VN_HOLD(rvp); 10118032SRic.Aleshire@Sun.COM VN_RELE(vp); 10128032SRic.Aleshire@Sun.COM vp = rvp; 10138032SRic.Aleshire@Sun.COM } 10148032SRic.Aleshire@Sun.COM 10150Sstevel@tonic-gate ASSERT(SOTOV(so)->v_stream); 10160Sstevel@tonic-gate mutex_enter(&vp->v_lock); 10170Sstevel@tonic-gate vp->v_stream = SOTOV(so)->v_stream; 10188348SEric.Yu@Sun.COM sti->sti_ux_bound_vp = vp; 10190Sstevel@tonic-gate mutex_exit(&vp->v_lock); 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate /* 10220Sstevel@tonic-gate * Use the vnode pointer value as a unique address 10230Sstevel@tonic-gate * (together with the magic number to avoid conflicts 10240Sstevel@tonic-gate * with implicit binds) in the transport provider. 10250Sstevel@tonic-gate */ 10268348SEric.Yu@Sun.COM sti->sti_ux_laddr.soua_vp = 10278348SEric.Yu@Sun.COM (void *)sti->sti_ux_bound_vp; 10288348SEric.Yu@Sun.COM sti->sti_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT; 10298348SEric.Yu@Sun.COM addr = &sti->sti_ux_laddr; 10308348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr); 10310Sstevel@tonic-gate dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n", 10320Sstevel@tonic-gate addrlen, 10338348SEric.Yu@Sun.COM (void *)((struct so_ux_addr *)addr)->soua_vp)); 10340Sstevel@tonic-gate break; 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate } /* end switch (so->so_family) */ 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since 10410Sstevel@tonic-gate * the transport can start passing up T_CONN_IND messages 10420Sstevel@tonic-gate * as soon as it receives the bind req and strsock_proto() 10430Sstevel@tonic-gate * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs. 10440Sstevel@tonic-gate */ 10450Sstevel@tonic-gate if (flags & _SOBIND_LISTEN) { 10460Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0) 10470Sstevel@tonic-gate clear_acceptconn_on_err = B_TRUE; 10480Sstevel@tonic-gate save_so_backlog = so->so_backlog; 10490Sstevel@tonic-gate restore_backlog_on_err = B_TRUE; 10500Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN; 10510Sstevel@tonic-gate so->so_backlog = backlog; 10520Sstevel@tonic-gate } 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate /* 10550Sstevel@tonic-gate * If NL7C addr(s) have been configured check for addr/port match, 10560Sstevel@tonic-gate * or if an implicit NL7C socket via AF_NCA mark socket as NL7C. 10570Sstevel@tonic-gate * 10580Sstevel@tonic-gate * NL7C supports the TCP transport only so check AF_INET and AF_INET6 10590Sstevel@tonic-gate * family sockets only. If match mark as such. 10600Sstevel@tonic-gate */ 10611974Sbrutus if (nl7c_enabled && ((addr != NULL && 10620Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6) && 10630Sstevel@tonic-gate (nl7c = nl7c_lookup_addr(addr, addrlen))) || 10648348SEric.Yu@Sun.COM sti->sti_nl7c_flags == NL7C_AF_NCA)) { 10650Sstevel@tonic-gate /* 10660Sstevel@tonic-gate * NL7C is not supported in non-global zones, 10670Sstevel@tonic-gate * we enforce this restriction here. 10680Sstevel@tonic-gate */ 10690Sstevel@tonic-gate if (so->so_zoneid == GLOBAL_ZONEID) { 10700Sstevel@tonic-gate /* An NL7C socket, mark it */ 10718348SEric.Yu@Sun.COM sti->sti_nl7c_flags |= NL7C_ENABLED; 10721974Sbrutus if (nl7c == NULL) { 10731974Sbrutus /* 10741974Sbrutus * Was an AF_NCA bind() so add it to the 10751974Sbrutus * addr list for reporting purposes. 10761974Sbrutus */ 10771974Sbrutus nl7c = nl7c_add_addr(addr, addrlen); 10781974Sbrutus } 10790Sstevel@tonic-gate } else 10800Sstevel@tonic-gate nl7c = NULL; 10810Sstevel@tonic-gate } 10828348SEric.Yu@Sun.COM 10830Sstevel@tonic-gate /* 10840Sstevel@tonic-gate * We send a T_BIND_REQ for TCP/UDP since we know it supports it, 10850Sstevel@tonic-gate * for other transports we will send in a O_T_BIND_REQ. 10860Sstevel@tonic-gate */ 10870Sstevel@tonic-gate if (tcp_udp_xport && 10880Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6)) 10890Sstevel@tonic-gate PRIM_type = T_BIND_REQ; 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate bind_req.PRIM_type = PRIM_type; 10920Sstevel@tonic-gate bind_req.ADDR_length = addrlen; 10930Sstevel@tonic-gate bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req); 10940Sstevel@tonic-gate bind_req.CONIND_number = backlog; 10950Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 10960Sstevel@tonic-gate mp = soallocproto2(&bind_req, sizeof (bind_req), 10975240Snordmark addr, addrlen, 0, _ALLOC_SLEEP); 10988348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 10998348SEric.Yu@Sun.COM 11008348SEric.Yu@Sun.COM /* Done using sti_laddr_sa - can drop the lock */ 11010Sstevel@tonic-gate mutex_exit(&so->so_lock); 11020Sstevel@tonic-gate 1103898Skais /* 1104898Skais * Intercept the bind_req message here to check if this <address/port> 1105898Skais * was configured as an SSL proxy server, or if another endpoint was 1106898Skais * already configured to act as a proxy for us. 11071974Sbrutus * 11081974Sbrutus * Note, only if NL7C not enabled for this socket. 1109898Skais */ 11101974Sbrutus if (nl7c == NULL && 11111974Sbrutus (so->so_family == AF_INET || so->so_family == AF_INET6) && 1112898Skais so->so_type == SOCK_STREAM) { 1113898Skais 11148348SEric.Yu@Sun.COM if (sti->sti_kssl_ent != NULL) { 11158348SEric.Yu@Sun.COM kssl_release_ent(sti->sti_kssl_ent, so, 11168348SEric.Yu@Sun.COM sti->sti_kssl_type); 11178348SEric.Yu@Sun.COM sti->sti_kssl_ent = NULL; 1118898Skais } 1119898Skais 11208348SEric.Yu@Sun.COM sti->sti_kssl_type = kssl_check_proxy(mp, so, 11218348SEric.Yu@Sun.COM &sti->sti_kssl_ent); 11228348SEric.Yu@Sun.COM switch (sti->sti_kssl_type) { 1123898Skais case KSSL_NO_PROXY: 1124898Skais break; 1125898Skais 1126898Skais case KSSL_HAS_PROXY: 1127898Skais mutex_enter(&so->so_lock); 1128898Skais goto skip_transport; 1129898Skais 1130898Skais case KSSL_IS_PROXY: 1131898Skais break; 1132898Skais } 1133898Skais } 1134898Skais 11350Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 11365240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 11370Sstevel@tonic-gate if (error) { 11380Sstevel@tonic-gate eprintsoline(so, error); 11390Sstevel@tonic-gate mutex_enter(&so->so_lock); 11400Sstevel@tonic-gate goto done; 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate mutex_enter(&so->so_lock); 11440Sstevel@tonic-gate error = sowaitprim(so, PRIM_type, T_BIND_ACK, 11450Sstevel@tonic-gate (t_uscalar_t)sizeof (*bind_ack), &mp, 0); 11460Sstevel@tonic-gate if (error) { 11470Sstevel@tonic-gate eprintsoline(so, error); 11480Sstevel@tonic-gate goto done; 11490Sstevel@tonic-gate } 1150898Skais skip_transport: 11510Sstevel@tonic-gate ASSERT(mp); 11520Sstevel@tonic-gate /* 11530Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 11540Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the bind 11550Sstevel@tonic-gate * is allowed to complete. 11560Sstevel@tonic-gate */ 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate /* Mark as bound. This will be undone if we detect errors below. */ 11590Sstevel@tonic-gate if (flags & _SOBIND_NOXLATE) { 11600Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 11618348SEric.Yu@Sun.COM sti->sti_faddr_noxlate = 1; 11620Sstevel@tonic-gate } 11630Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND)); 11640Sstevel@tonic-gate so->so_state |= SS_ISBOUND; 11658348SEric.Yu@Sun.COM ASSERT(sti->sti_unbind_mp); 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate /* note that we've already set SS_ACCEPTCONN above */ 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate /* 11700Sstevel@tonic-gate * Recompute addrlen - an unspecied bind sent down an 11710Sstevel@tonic-gate * address of length zero but we expect the appropriate length 11720Sstevel@tonic-gate * in return. 11730Sstevel@tonic-gate */ 11740Sstevel@tonic-gate addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ? 11758348SEric.Yu@Sun.COM sizeof (sti->sti_ux_laddr) : sti->sti_laddr_len); 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate bind_ack = (struct T_bind_ack *)mp->b_rptr; 11780Sstevel@tonic-gate /* 11790Sstevel@tonic-gate * The alignment restriction is really too strict but 11800Sstevel@tonic-gate * we want enough alignment to inspect the fields of 11810Sstevel@tonic-gate * a sockaddr_in. 11820Sstevel@tonic-gate */ 11830Sstevel@tonic-gate addr = sogetoff(mp, bind_ack->ADDR_offset, 11845240Snordmark bind_ack->ADDR_length, 11855240Snordmark __TPI_ALIGN_SIZE); 11860Sstevel@tonic-gate if (addr == NULL) { 11870Sstevel@tonic-gate freemsg(mp); 11880Sstevel@tonic-gate error = EPROTO; 11890Sstevel@tonic-gate eprintsoline(so, error); 11900Sstevel@tonic-gate goto done; 11910Sstevel@tonic-gate } 11920Sstevel@tonic-gate if (!(flags & _SOBIND_UNSPEC)) { 11930Sstevel@tonic-gate /* 11940Sstevel@tonic-gate * Verify that the transport didn't return something we 11950Sstevel@tonic-gate * did not want e.g. an address other than what we asked for. 11960Sstevel@tonic-gate * 11970Sstevel@tonic-gate * NOTE: These checks would go away if/when we switch to 11980Sstevel@tonic-gate * using the new TPI (in which the transport would fail 11990Sstevel@tonic-gate * the request instead of assigning a different address). 12000Sstevel@tonic-gate * 12010Sstevel@tonic-gate * NOTE2: For protocols that we don't know (i.e. any 12020Sstevel@tonic-gate * other than AF_INET6, AF_INET and AF_UNIX), we 12030Sstevel@tonic-gate * cannot know if the transport should be expected to 12040Sstevel@tonic-gate * return the same address as that requested. 12050Sstevel@tonic-gate * 12060Sstevel@tonic-gate * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send 12070Sstevel@tonic-gate * down a T_BIND_REQ. We use O_T_BIND_REQ for others. 12080Sstevel@tonic-gate * 12090Sstevel@tonic-gate * For example, in the case of netatalk it may be 12100Sstevel@tonic-gate * inappropriate for the transport to return the 12110Sstevel@tonic-gate * requested address (as it may have allocated a local 12120Sstevel@tonic-gate * port number in behaviour similar to that of an 12130Sstevel@tonic-gate * AF_INET bind request with a port number of zero). 12140Sstevel@tonic-gate * 12150Sstevel@tonic-gate * Given the definition of O_T_BIND_REQ, where the 12160Sstevel@tonic-gate * transport may bind to an address other than the 12170Sstevel@tonic-gate * requested address, it's not possible to determine 12180Sstevel@tonic-gate * whether a returned address that differs from the 12190Sstevel@tonic-gate * requested address is a reason to fail (because the 12200Sstevel@tonic-gate * requested address was not available) or succeed 12210Sstevel@tonic-gate * (because the transport allocated an appropriate 12220Sstevel@tonic-gate * address and/or port). 12230Sstevel@tonic-gate * 12240Sstevel@tonic-gate * sockfs currently requires that the transport return 12250Sstevel@tonic-gate * the requested address in the T_BIND_ACK, unless 12260Sstevel@tonic-gate * there is code here to allow for any discrepancy. 12270Sstevel@tonic-gate * Such code exists for AF_INET and AF_INET6. 12280Sstevel@tonic-gate * 12290Sstevel@tonic-gate * Netatalk chooses to return the requested address 12300Sstevel@tonic-gate * rather than the (correct) allocated address. This 12310Sstevel@tonic-gate * means that netatalk violates the TPI specification 12320Sstevel@tonic-gate * (and would not function correctly if used from a 12330Sstevel@tonic-gate * TLI application), but it does mean that it works 12340Sstevel@tonic-gate * with sockfs. 12350Sstevel@tonic-gate * 12360Sstevel@tonic-gate * As noted above, using the newer XTI bind primitive 12370Sstevel@tonic-gate * (T_BIND_REQ) in preference to O_T_BIND_REQ would 12380Sstevel@tonic-gate * allow sockfs to be more sure about whether or not 12390Sstevel@tonic-gate * the bind request had succeeded (as transports are 12400Sstevel@tonic-gate * not permitted to bind to a different address than 12410Sstevel@tonic-gate * that requested - they must return failure). 12420Sstevel@tonic-gate * Unfortunately, support for T_BIND_REQ may not be 12430Sstevel@tonic-gate * present in all transport implementations (netatalk, 12440Sstevel@tonic-gate * for example, doesn't have it), making the 12450Sstevel@tonic-gate * transition difficult. 12460Sstevel@tonic-gate */ 12470Sstevel@tonic-gate if (bind_ack->ADDR_length != addrlen) { 12480Sstevel@tonic-gate /* Assumes that the requested address was in use */ 12490Sstevel@tonic-gate freemsg(mp); 12500Sstevel@tonic-gate error = EADDRINUSE; 12510Sstevel@tonic-gate eprintsoline(so, error); 12520Sstevel@tonic-gate goto done; 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate switch (so->so_family) { 12560Sstevel@tonic-gate case AF_INET6: 12570Sstevel@tonic-gate case AF_INET: { 12580Sstevel@tonic-gate sin_t *rname, *aname; 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate rname = (sin_t *)addr; 12618348SEric.Yu@Sun.COM aname = (sin_t *)sti->sti_laddr_sa; 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate /* 12640Sstevel@tonic-gate * Take advantage of the alignment 12650Sstevel@tonic-gate * of sin_port and sin6_port which fall 12660Sstevel@tonic-gate * in the same place in their data structures. 12670Sstevel@tonic-gate * Just use sin_port for either address family. 12680Sstevel@tonic-gate * 12690Sstevel@tonic-gate * This may become a problem if (heaven forbid) 12700Sstevel@tonic-gate * there's a separate ipv6port_reserved... :-P 12710Sstevel@tonic-gate * 12720Sstevel@tonic-gate * Binding to port 0 has the semantics of letting 12730Sstevel@tonic-gate * the transport bind to any port. 12740Sstevel@tonic-gate * 12750Sstevel@tonic-gate * If the transport is TCP or UDP since we had sent 12760Sstevel@tonic-gate * a T_BIND_REQ we would not get a port other than 12770Sstevel@tonic-gate * what we asked for. 12780Sstevel@tonic-gate */ 12790Sstevel@tonic-gate if (tcp_udp_xport) { 12800Sstevel@tonic-gate /* 12810Sstevel@tonic-gate * Pick up the new port number if we bound to 12820Sstevel@tonic-gate * port 0. 12830Sstevel@tonic-gate */ 12840Sstevel@tonic-gate if (aname->sin_port == 0) 12850Sstevel@tonic-gate aname->sin_port = rname->sin_port; 12868348SEric.Yu@Sun.COM sti->sti_laddr_valid = 1; 12870Sstevel@tonic-gate break; 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate if (aname->sin_port != 0 && 12900Sstevel@tonic-gate aname->sin_port != rname->sin_port) { 12910Sstevel@tonic-gate freemsg(mp); 12920Sstevel@tonic-gate error = EADDRINUSE; 12930Sstevel@tonic-gate eprintsoline(so, error); 12940Sstevel@tonic-gate goto done; 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate /* 12970Sstevel@tonic-gate * Pick up the new port number if we bound to port 0. 12980Sstevel@tonic-gate */ 12990Sstevel@tonic-gate aname->sin_port = rname->sin_port; 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate /* 13020Sstevel@tonic-gate * Unfortunately, addresses aren't _quite_ the same. 13030Sstevel@tonic-gate */ 13040Sstevel@tonic-gate if (so->so_family == AF_INET) { 13050Sstevel@tonic-gate if (aname->sin_addr.s_addr != 13060Sstevel@tonic-gate rname->sin_addr.s_addr) { 13070Sstevel@tonic-gate freemsg(mp); 13080Sstevel@tonic-gate error = EADDRNOTAVAIL; 13090Sstevel@tonic-gate eprintsoline(so, error); 13100Sstevel@tonic-gate goto done; 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate } else { 13130Sstevel@tonic-gate sin6_t *rname6 = (sin6_t *)rname; 13140Sstevel@tonic-gate sin6_t *aname6 = (sin6_t *)aname; 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr, 13170Sstevel@tonic-gate &rname6->sin6_addr)) { 13180Sstevel@tonic-gate freemsg(mp); 13190Sstevel@tonic-gate error = EADDRNOTAVAIL; 13200Sstevel@tonic-gate eprintsoline(so, error); 13210Sstevel@tonic-gate goto done; 13220Sstevel@tonic-gate } 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate break; 13250Sstevel@tonic-gate } 13260Sstevel@tonic-gate case AF_UNIX: 13278348SEric.Yu@Sun.COM if (bcmp(addr, &sti->sti_ux_laddr, addrlen) != 0) { 13280Sstevel@tonic-gate freemsg(mp); 13290Sstevel@tonic-gate error = EADDRINUSE; 13300Sstevel@tonic-gate eprintsoline(so, error); 13310Sstevel@tonic-gate eprintso(so, 13325240Snordmark ("addrlen %d, addr 0x%x, vp %p\n", 13335240Snordmark addrlen, *((int *)addr), 13348348SEric.Yu@Sun.COM (void *)sti->sti_ux_bound_vp)); 13350Sstevel@tonic-gate goto done; 13360Sstevel@tonic-gate } 13378348SEric.Yu@Sun.COM sti->sti_laddr_valid = 1; 13380Sstevel@tonic-gate break; 13390Sstevel@tonic-gate default: 13400Sstevel@tonic-gate /* 13410Sstevel@tonic-gate * NOTE: This assumes that addresses can be 13420Sstevel@tonic-gate * byte-compared for equivalence. 13430Sstevel@tonic-gate */ 13448348SEric.Yu@Sun.COM if (bcmp(addr, sti->sti_laddr_sa, addrlen) != 0) { 13450Sstevel@tonic-gate freemsg(mp); 13460Sstevel@tonic-gate error = EADDRINUSE; 13470Sstevel@tonic-gate eprintsoline(so, error); 13480Sstevel@tonic-gate goto done; 13490Sstevel@tonic-gate } 13500Sstevel@tonic-gate /* 13518348SEric.Yu@Sun.COM * Don't mark sti_laddr_valid, as we cannot be 13520Sstevel@tonic-gate * sure that the returned address is the real 13530Sstevel@tonic-gate * bound address when talking to an unknown 13540Sstevel@tonic-gate * transport. 13550Sstevel@tonic-gate */ 13560Sstevel@tonic-gate break; 13570Sstevel@tonic-gate } 13580Sstevel@tonic-gate } else { 13590Sstevel@tonic-gate /* 13600Sstevel@tonic-gate * Save for returned address for getsockname. 13610Sstevel@tonic-gate * Needed for unspecific bind unless transport supports 13620Sstevel@tonic-gate * the TI_GETMYNAME ioctl. 13630Sstevel@tonic-gate * Do this for AF_INET{,6} even though they do, as 13640Sstevel@tonic-gate * caching info here is much better performance than 13650Sstevel@tonic-gate * a TPI/STREAMS trip to the transport for getsockname. 13660Sstevel@tonic-gate * Any which can't for some reason _must_ _not_ set 13678348SEric.Yu@Sun.COM * sti_laddr_valid here for the caching version of 13688348SEric.Yu@Sun.COM * getsockname to not break; 13690Sstevel@tonic-gate */ 13700Sstevel@tonic-gate switch (so->so_family) { 13710Sstevel@tonic-gate case AF_UNIX: 13720Sstevel@tonic-gate /* 13730Sstevel@tonic-gate * Record the address bound with the transport 13740Sstevel@tonic-gate * for use by socketpair. 13750Sstevel@tonic-gate */ 13768348SEric.Yu@Sun.COM bcopy(addr, &sti->sti_ux_laddr, addrlen); 13778348SEric.Yu@Sun.COM sti->sti_laddr_valid = 1; 13780Sstevel@tonic-gate break; 13790Sstevel@tonic-gate case AF_INET: 13800Sstevel@tonic-gate case AF_INET6: 13818348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 13828348SEric.Yu@Sun.COM bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len); 13838348SEric.Yu@Sun.COM sti->sti_laddr_valid = 1; 13840Sstevel@tonic-gate break; 13850Sstevel@tonic-gate default: 13860Sstevel@tonic-gate /* 13878348SEric.Yu@Sun.COM * Don't mark sti_laddr_valid, as we cannot be 13880Sstevel@tonic-gate * sure that the returned address is the real 13890Sstevel@tonic-gate * bound address when talking to an unknown 13900Sstevel@tonic-gate * transport. 13910Sstevel@tonic-gate */ 13920Sstevel@tonic-gate break; 13930Sstevel@tonic-gate } 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate if (nl7c != NULL) { 13971974Sbrutus /* Register listen()er sonode pointer with NL7C */ 13981974Sbrutus nl7c_listener_addr(nl7c, so); 13990Sstevel@tonic-gate } 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate freemsg(mp); 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate done: 14040Sstevel@tonic-gate if (error) { 14050Sstevel@tonic-gate /* reset state & backlog to values held on entry */ 14060Sstevel@tonic-gate if (clear_acceptconn_on_err == B_TRUE) 14070Sstevel@tonic-gate so->so_state &= ~SS_ACCEPTCONN; 14080Sstevel@tonic-gate if (restore_backlog_on_err == B_TRUE) 14090Sstevel@tonic-gate so->so_backlog = save_so_backlog; 14100Sstevel@tonic-gate 14110Sstevel@tonic-gate if (unbind_on_err && so->so_state & SS_ISBOUND) { 14120Sstevel@tonic-gate int err; 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate err = sotpi_unbind(so, 0); 14150Sstevel@tonic-gate /* LINTED - statement has no consequent: if */ 14160Sstevel@tonic-gate if (err) { 14170Sstevel@tonic-gate eprintsoline(so, error); 14180Sstevel@tonic-gate } else { 14190Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 14200Sstevel@tonic-gate } 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate } 14230Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) { 14240Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 14250Sstevel@tonic-gate mutex_exit(&so->so_lock); 14260Sstevel@tonic-gate } else { 14270Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 14280Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate return (error); 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate /* bind the socket */ 1434741Smasputra static int 14350Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen, 14368348SEric.Yu@Sun.COM int flags, struct cred *cr) 14370Sstevel@tonic-gate { 14380Sstevel@tonic-gate if ((flags & _SOBIND_SOCKETPAIR) == 0) 14398348SEric.Yu@Sun.COM return (sotpi_bindlisten(so, name, namelen, 0, flags, cr)); 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate flags &= ~_SOBIND_SOCKETPAIR; 14428348SEric.Yu@Sun.COM return (sotpi_bindlisten(so, name, namelen, 1, flags, cr)); 14430Sstevel@tonic-gate } 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate /* 14460Sstevel@tonic-gate * Unbind a socket - used when bind() fails, when bind() specifies a NULL 14470Sstevel@tonic-gate * address, or when listen needs to unbind and bind. 14480Sstevel@tonic-gate * If the _SOUNBIND_REBIND flag is specified the addresses are retained 14490Sstevel@tonic-gate * so that a sobind can pick them up. 14500Sstevel@tonic-gate */ 14510Sstevel@tonic-gate static int 14520Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags) 14530Sstevel@tonic-gate { 14540Sstevel@tonic-gate struct T_unbind_req unbind_req; 14550Sstevel@tonic-gate int error = 0; 14560Sstevel@tonic-gate mblk_t *mp; 14578348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n", 14607240Srh87107 (void *)so, flags, pr_state(so->so_state, so->so_mode))); 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 14630Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 14660Sstevel@tonic-gate error = EINVAL; 14670Sstevel@tonic-gate eprintsoline(so, error); 14680Sstevel@tonic-gate goto done; 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate mutex_exit(&so->so_lock); 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate /* 14740Sstevel@tonic-gate * Flush the read and write side (except stream head read queue) 14750Sstevel@tonic-gate * and send down T_UNBIND_REQ. 14760Sstevel@tonic-gate */ 14770Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW); 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate unbind_req.PRIM_type = T_UNBIND_REQ; 14800Sstevel@tonic-gate mp = soallocproto1(&unbind_req, sizeof (unbind_req), 14810Sstevel@tonic-gate 0, _ALLOC_SLEEP); 14820Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 14835240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 14840Sstevel@tonic-gate mutex_enter(&so->so_lock); 14850Sstevel@tonic-gate if (error) { 14860Sstevel@tonic-gate eprintsoline(so, error); 14870Sstevel@tonic-gate goto done; 14880Sstevel@tonic-gate } 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate error = sowaitokack(so, T_UNBIND_REQ); 14910Sstevel@tonic-gate if (error) { 14920Sstevel@tonic-gate eprintsoline(so, error); 14930Sstevel@tonic-gate goto done; 14940Sstevel@tonic-gate } 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate /* 14970Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 14980Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the unbind 14990Sstevel@tonic-gate * is allowed to complete. 15000Sstevel@tonic-gate */ 15010Sstevel@tonic-gate if (!(flags & _SOUNBIND_REBIND)) { 15020Sstevel@tonic-gate /* 15030Sstevel@tonic-gate * Clear out bound address. 15040Sstevel@tonic-gate */ 15050Sstevel@tonic-gate vnode_t *vp; 15060Sstevel@tonic-gate 15078348SEric.Yu@Sun.COM if ((vp = sti->sti_ux_bound_vp) != NULL) { 1508898Skais 1509898Skais /* Undo any SSL proxy setup */ 1510898Skais if ((so->so_family == AF_INET || 1511898Skais so->so_family == AF_INET6) && 1512898Skais (so->so_type == SOCK_STREAM) && 15138348SEric.Yu@Sun.COM (sti->sti_kssl_ent != NULL)) { 15148348SEric.Yu@Sun.COM kssl_release_ent(sti->sti_kssl_ent, so, 15158348SEric.Yu@Sun.COM sti->sti_kssl_type); 15168348SEric.Yu@Sun.COM sti->sti_kssl_ent = NULL; 15178348SEric.Yu@Sun.COM sti->sti_kssl_type = KSSL_NO_PROXY; 1518898Skais } 15198348SEric.Yu@Sun.COM sti->sti_ux_bound_vp = NULL; 15200Sstevel@tonic-gate vn_rele_stream(vp); 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate /* Clear out address */ 15238348SEric.Yu@Sun.COM sti->sti_laddr_len = 0; 15248348SEric.Yu@Sun.COM } 15258348SEric.Yu@Sun.COM so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN); 15268348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 15271974Sbrutus 15280Sstevel@tonic-gate done: 1529898Skais 15300Sstevel@tonic-gate /* If the caller held the lock don't release it here */ 15310Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 15320Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate return (error); 15350Sstevel@tonic-gate } 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate /* 15380Sstevel@tonic-gate * listen on the socket. 15390Sstevel@tonic-gate * For TPI conforming transports this has to first unbind with the transport 15400Sstevel@tonic-gate * and then bind again using the new backlog. 15410Sstevel@tonic-gate */ 15428348SEric.Yu@Sun.COM /* ARGSUSED */ 15430Sstevel@tonic-gate int 15448348SEric.Yu@Sun.COM sotpi_listen(struct sonode *so, int backlog, struct cred *cr) 15450Sstevel@tonic-gate { 15460Sstevel@tonic-gate int error = 0; 15478348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n", 15507240Srh87107 (void *)so, backlog, pr_state(so->so_state, so->so_mode))); 15510Sstevel@tonic-gate 15528348SEric.Yu@Sun.COM if (sti->sti_serv_type == T_CLTS) 15530Sstevel@tonic-gate return (EOPNOTSUPP); 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate /* 15560Sstevel@tonic-gate * If the socket is ready to accept connections already, then 15570Sstevel@tonic-gate * return without doing anything. This avoids a problem where 15580Sstevel@tonic-gate * a second listen() call fails if a connection is pending and 15590Sstevel@tonic-gate * leaves the socket unbound. Only when we are not unbinding 15600Sstevel@tonic-gate * with the transport can we safely increase the backlog. 15610Sstevel@tonic-gate */ 15620Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN && 15630Sstevel@tonic-gate !((so->so_family == AF_INET || so->so_family == AF_INET6) && 15645240Snordmark /*CONSTCOND*/ 15655240Snordmark !solisten_tpi_tcp)) 15660Sstevel@tonic-gate return (0); 15670Sstevel@tonic-gate 15680Sstevel@tonic-gate if (so->so_state & SS_ISCONNECTED) 15690Sstevel@tonic-gate return (EINVAL); 15700Sstevel@tonic-gate 15710Sstevel@tonic-gate mutex_enter(&so->so_lock); 15720Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 15730Sstevel@tonic-gate 15740Sstevel@tonic-gate /* 15750Sstevel@tonic-gate * If the listen doesn't change the backlog we do nothing. 15760Sstevel@tonic-gate * This avoids an EPROTO error from the transport. 15770Sstevel@tonic-gate */ 15780Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) && 15790Sstevel@tonic-gate so->so_backlog == backlog) 15800Sstevel@tonic-gate goto done; 15810Sstevel@tonic-gate 15820Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 15830Sstevel@tonic-gate /* 15840Sstevel@tonic-gate * Must have been explicitly bound in the UNIX domain. 15850Sstevel@tonic-gate */ 15860Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 15870Sstevel@tonic-gate error = EINVAL; 15880Sstevel@tonic-gate goto done; 15890Sstevel@tonic-gate } 15900Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog, 15918348SEric.Yu@Sun.COM _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr); 15920Sstevel@tonic-gate } else if (backlog > 0) { 15930Sstevel@tonic-gate /* 15940Sstevel@tonic-gate * AF_INET{,6} hack to avoid losing the port. 15950Sstevel@tonic-gate * Assumes that all AF_INET{,6} transports can handle a 15960Sstevel@tonic-gate * O_T_BIND_REQ with a non-zero CONIND_number when the TPI 15970Sstevel@tonic-gate * has already bound thus it is possible to avoid the unbind. 15980Sstevel@tonic-gate */ 15990Sstevel@tonic-gate if (!((so->so_family == AF_INET || so->so_family == AF_INET6) && 16000Sstevel@tonic-gate /*CONSTCOND*/ 16010Sstevel@tonic-gate !solisten_tpi_tcp)) { 16020Sstevel@tonic-gate error = sotpi_unbind(so, _SOUNBIND_REBIND); 16030Sstevel@tonic-gate if (error) 16040Sstevel@tonic-gate goto done; 16050Sstevel@tonic-gate } 16060Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog, 16078348SEric.Yu@Sun.COM _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr); 16080Sstevel@tonic-gate } else { 16090Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN; 16100Sstevel@tonic-gate so->so_backlog = backlog; 16110Sstevel@tonic-gate } 16120Sstevel@tonic-gate if (error) 16130Sstevel@tonic-gate goto done; 16140Sstevel@tonic-gate ASSERT(so->so_state & SS_ACCEPTCONN); 16150Sstevel@tonic-gate done: 16160Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 16170Sstevel@tonic-gate mutex_exit(&so->so_lock); 16180Sstevel@tonic-gate return (error); 16190Sstevel@tonic-gate } 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate /* 16220Sstevel@tonic-gate * Disconnect either a specified seqno or all (-1). 16230Sstevel@tonic-gate * The former is used on listening sockets only. 16240Sstevel@tonic-gate * 16250Sstevel@tonic-gate * When seqno == -1 sodisconnect could call sotpi_unbind. However, 16260Sstevel@tonic-gate * the current use of sodisconnect(seqno == -1) is only for shutdown 16270Sstevel@tonic-gate * so there is no point (and potentially incorrect) to unbind. 16280Sstevel@tonic-gate */ 16298348SEric.Yu@Sun.COM static int 16300Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags) 16310Sstevel@tonic-gate { 16320Sstevel@tonic-gate struct T_discon_req discon_req; 16330Sstevel@tonic-gate int error = 0; 16340Sstevel@tonic-gate mblk_t *mp; 16350Sstevel@tonic-gate 16360Sstevel@tonic-gate dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n", 16377240Srh87107 (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode))); 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) { 16400Sstevel@tonic-gate mutex_enter(&so->so_lock); 16410Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 16420Sstevel@tonic-gate } else { 16430Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 16440Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) { 16480Sstevel@tonic-gate error = EINVAL; 16490Sstevel@tonic-gate eprintsoline(so, error); 16500Sstevel@tonic-gate goto done; 16510Sstevel@tonic-gate } 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate mutex_exit(&so->so_lock); 16540Sstevel@tonic-gate /* 16550Sstevel@tonic-gate * Flush the write side (unless this is a listener) 16560Sstevel@tonic-gate * and then send down a T_DISCON_REQ. 16570Sstevel@tonic-gate * (Don't flush on listener since it could flush {O_}T_CONN_RES 16580Sstevel@tonic-gate * and other messages.) 16590Sstevel@tonic-gate */ 16600Sstevel@tonic-gate if (!(so->so_state & SS_ACCEPTCONN)) 16610Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW); 16620Sstevel@tonic-gate 16630Sstevel@tonic-gate discon_req.PRIM_type = T_DISCON_REQ; 16640Sstevel@tonic-gate discon_req.SEQ_number = seqno; 16650Sstevel@tonic-gate mp = soallocproto1(&discon_req, sizeof (discon_req), 16660Sstevel@tonic-gate 0, _ALLOC_SLEEP); 16670Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 16685240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 16690Sstevel@tonic-gate mutex_enter(&so->so_lock); 16700Sstevel@tonic-gate if (error) { 16710Sstevel@tonic-gate eprintsoline(so, error); 16720Sstevel@tonic-gate goto done; 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate error = sowaitokack(so, T_DISCON_REQ); 16760Sstevel@tonic-gate if (error) { 16770Sstevel@tonic-gate eprintsoline(so, error); 16780Sstevel@tonic-gate goto done; 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate /* 16810Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 16820Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the disconnect 16830Sstevel@tonic-gate * is allowed to complete. However, it is not possible to 16840Sstevel@tonic-gate * assert that SS_ISCONNECTED|SS_ISCONNECTING are set. 16850Sstevel@tonic-gate */ 16868348SEric.Yu@Sun.COM so->so_state &= ~(SS_ISCONNECTED|SS_ISCONNECTING); 16878348SEric.Yu@Sun.COM SOTOTPI(so)->sti_laddr_valid = 0; 16888348SEric.Yu@Sun.COM SOTOTPI(so)->sti_faddr_valid = 0; 16890Sstevel@tonic-gate done: 16900Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) { 16910Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 16920Sstevel@tonic-gate mutex_exit(&so->so_lock); 16930Sstevel@tonic-gate } else { 16940Sstevel@tonic-gate /* If the caller held the lock don't release it here */ 16950Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 16960Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 16970Sstevel@tonic-gate } 16980Sstevel@tonic-gate return (error); 16990Sstevel@tonic-gate } 17000Sstevel@tonic-gate 17018348SEric.Yu@Sun.COM /* ARGSUSED */ 17020Sstevel@tonic-gate int 17038348SEric.Yu@Sun.COM sotpi_accept(struct sonode *so, int fflag, struct cred *cr, 17048348SEric.Yu@Sun.COM struct sonode **nsop) 17050Sstevel@tonic-gate { 17060Sstevel@tonic-gate struct T_conn_ind *conn_ind; 17070Sstevel@tonic-gate struct T_conn_res *conn_res; 17080Sstevel@tonic-gate int error = 0; 17094379Sja97890 mblk_t *mp, *ctxmp, *ack_mp; 17100Sstevel@tonic-gate struct sonode *nso; 17110Sstevel@tonic-gate vnode_t *nvp; 17120Sstevel@tonic-gate void *src; 17130Sstevel@tonic-gate t_uscalar_t srclen; 17140Sstevel@tonic-gate void *opt; 17150Sstevel@tonic-gate t_uscalar_t optlen; 17160Sstevel@tonic-gate t_scalar_t PRIM_type; 17170Sstevel@tonic-gate t_scalar_t SEQ_number; 17184379Sja97890 size_t sinlen; 17198348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 17208348SEric.Yu@Sun.COM sotpi_info_t *nsti; 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n", 17237240Srh87107 (void *)so, fflag, (void *)nsop, 17247240Srh87107 pr_state(so->so_state, so->so_mode))); 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate /* 17270Sstevel@tonic-gate * Defer single-threading the accepting socket until 17280Sstevel@tonic-gate * the T_CONN_IND has been received and parsed and the 17290Sstevel@tonic-gate * new sonode has been opened. 17300Sstevel@tonic-gate */ 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate /* Check that we are not already connected */ 17330Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0) 17340Sstevel@tonic-gate goto conn_bad; 17350Sstevel@tonic-gate again: 17360Sstevel@tonic-gate if ((error = sowaitconnind(so, fflag, &mp)) != 0) 17370Sstevel@tonic-gate goto e_bad; 17380Sstevel@tonic-gate 17398348SEric.Yu@Sun.COM ASSERT(mp != NULL); 17400Sstevel@tonic-gate conn_ind = (struct T_conn_ind *)mp->b_rptr; 1741898Skais ctxmp = mp->b_cont; 1742898Skais 17430Sstevel@tonic-gate /* 17440Sstevel@tonic-gate * Save SEQ_number for error paths. 17450Sstevel@tonic-gate */ 17460Sstevel@tonic-gate SEQ_number = conn_ind->SEQ_number; 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate srclen = conn_ind->SRC_length; 17490Sstevel@tonic-gate src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1); 17500Sstevel@tonic-gate if (src == NULL) { 17510Sstevel@tonic-gate error = EPROTO; 17520Sstevel@tonic-gate freemsg(mp); 17530Sstevel@tonic-gate eprintsoline(so, error); 17540Sstevel@tonic-gate goto disconnect_unlocked; 17550Sstevel@tonic-gate } 17560Sstevel@tonic-gate optlen = conn_ind->OPT_length; 17570Sstevel@tonic-gate switch (so->so_family) { 17580Sstevel@tonic-gate case AF_INET: 17590Sstevel@tonic-gate case AF_INET6: 17608348SEric.Yu@Sun.COM if ((optlen == sizeof (intptr_t)) && (sti->sti_direct != 0)) { 17610Sstevel@tonic-gate bcopy(mp->b_rptr + conn_ind->OPT_offset, 17620Sstevel@tonic-gate &opt, conn_ind->OPT_length); 17630Sstevel@tonic-gate } else { 17640Sstevel@tonic-gate /* 17650Sstevel@tonic-gate * The transport (in this case TCP) hasn't sent up 17660Sstevel@tonic-gate * a pointer to an instance for the accept fast-path. 17670Sstevel@tonic-gate * Disable fast-path completely because the call to 17680Sstevel@tonic-gate * sotpi_create() below would otherwise create an 17690Sstevel@tonic-gate * incomplete TCP instance, which would lead to 17700Sstevel@tonic-gate * problems when sockfs sends a normal T_CONN_RES 17710Sstevel@tonic-gate * message down the new stream. 17720Sstevel@tonic-gate */ 17738348SEric.Yu@Sun.COM if (sti->sti_direct) { 1774741Smasputra int rval; 1775741Smasputra /* 1776741Smasputra * For consistency we inform tcp to disable 1777741Smasputra * direct interface on the listener, though 1778741Smasputra * we can certainly live without doing this 1779741Smasputra * because no data will ever travel upstream 1780741Smasputra * on the listening socket. 1781741Smasputra */ 17828348SEric.Yu@Sun.COM sti->sti_direct = 0; 1783741Smasputra (void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK, 1784741Smasputra 0, 0, K_TO_K, CRED(), &rval); 1785741Smasputra } 17860Sstevel@tonic-gate opt = NULL; 17870Sstevel@tonic-gate optlen = 0; 17880Sstevel@tonic-gate } 17890Sstevel@tonic-gate break; 17900Sstevel@tonic-gate case AF_UNIX: 17910Sstevel@tonic-gate default: 17920Sstevel@tonic-gate if (optlen != 0) { 17930Sstevel@tonic-gate opt = sogetoff(mp, conn_ind->OPT_offset, optlen, 17940Sstevel@tonic-gate __TPI_ALIGN_SIZE); 17950Sstevel@tonic-gate if (opt == NULL) { 17960Sstevel@tonic-gate error = EPROTO; 17970Sstevel@tonic-gate freemsg(mp); 17980Sstevel@tonic-gate eprintsoline(so, error); 17990Sstevel@tonic-gate goto disconnect_unlocked; 18000Sstevel@tonic-gate } 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 18038348SEric.Yu@Sun.COM if (!sti->sti_faddr_noxlate) { 18040Sstevel@tonic-gate src = NULL; 18050Sstevel@tonic-gate srclen = 0; 18060Sstevel@tonic-gate } 18070Sstevel@tonic-gate /* Extract src address from options */ 18080Sstevel@tonic-gate if (optlen != 0) 18090Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &src, &srclen); 18100Sstevel@tonic-gate } 18110Sstevel@tonic-gate break; 18120Sstevel@tonic-gate } 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate /* 18150Sstevel@tonic-gate * Create the new socket. 18160Sstevel@tonic-gate */ 18178348SEric.Yu@Sun.COM nso = socket_newconn(so, NULL, NULL, SOCKET_SLEEP, &error); 18180Sstevel@tonic-gate if (nso == NULL) { 18190Sstevel@tonic-gate ASSERT(error != 0); 18200Sstevel@tonic-gate /* 18210Sstevel@tonic-gate * Accept can not fail with ENOBUFS. sotpi_create 18220Sstevel@tonic-gate * sleeps waiting for memory until a signal is caught 18230Sstevel@tonic-gate * so return EINTR. 18240Sstevel@tonic-gate */ 18250Sstevel@tonic-gate freemsg(mp); 18260Sstevel@tonic-gate if (error == ENOBUFS) 18270Sstevel@tonic-gate error = EINTR; 18280Sstevel@tonic-gate goto e_disc_unl; 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate nvp = SOTOV(nso); 18318348SEric.Yu@Sun.COM nsti = SOTOTPI(nso); 18320Sstevel@tonic-gate 1833898Skais /* 1834898Skais * If the transport sent up an SSL connection context, then attach 1835898Skais * it the new socket, and set the (sd_wputdatafunc)() and 1836898Skais * (sd_rputdatafunc)() stream head hooks to intercept and process 1837898Skais * SSL records. 1838898Skais */ 1839898Skais if (ctxmp != NULL) { 1840898Skais /* 1841898Skais * This kssl_ctx_t is already held for us by the transport. 1842898Skais * So, we don't need to do a kssl_hold_ctx() here. 1843898Skais */ 18448348SEric.Yu@Sun.COM nsti->sti_kssl_ctx = *((kssl_ctx_t *)ctxmp->b_rptr); 1845898Skais freemsg(ctxmp); 1846898Skais mp->b_cont = NULL; 1847898Skais strsetrwputdatahooks(nvp, strsock_kssl_input, 1848898Skais strsock_kssl_output); 18497660SEric.Yu@Sun.COM 18507660SEric.Yu@Sun.COM /* Disable sodirect if any */ 18517660SEric.Yu@Sun.COM if (nso->so_direct != NULL) { 18527660SEric.Yu@Sun.COM mutex_enter(nso->so_direct->sod_lockp); 18537660SEric.Yu@Sun.COM SOD_DISABLE(nso->so_direct); 18547660SEric.Yu@Sun.COM mutex_exit(nso->so_direct->sod_lockp); 18557660SEric.Yu@Sun.COM } 1856898Skais } 18570Sstevel@tonic-gate #ifdef DEBUG 18580Sstevel@tonic-gate /* 18590Sstevel@tonic-gate * SO_DEBUG is used to trigger the dprint* and eprint* macros thus 18600Sstevel@tonic-gate * it's inherited early to allow debugging of the accept code itself. 18610Sstevel@tonic-gate */ 18620Sstevel@tonic-gate nso->so_options |= so->so_options & SO_DEBUG; 18630Sstevel@tonic-gate #endif /* DEBUG */ 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate /* 18660Sstevel@tonic-gate * Save the SRC address from the T_CONN_IND 18670Sstevel@tonic-gate * for getpeername to work on AF_UNIX and on transports that do not 18680Sstevel@tonic-gate * support TI_GETPEERNAME. 18690Sstevel@tonic-gate * 18700Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by the sender's 18710Sstevel@tonic-gate * copyin_name(). 18720Sstevel@tonic-gate */ 18738348SEric.Yu@Sun.COM if (srclen > (t_uscalar_t)nsti->sti_faddr_maxlen) { 18740Sstevel@tonic-gate error = EINVAL; 18750Sstevel@tonic-gate freemsg(mp); 18760Sstevel@tonic-gate eprintsoline(so, error); 18770Sstevel@tonic-gate goto disconnect_vp_unlocked; 18780Sstevel@tonic-gate } 18798348SEric.Yu@Sun.COM nsti->sti_faddr_len = (socklen_t)srclen; 18808348SEric.Yu@Sun.COM ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen); 18818348SEric.Yu@Sun.COM bcopy(src, nsti->sti_faddr_sa, srclen); 18828348SEric.Yu@Sun.COM nsti->sti_faddr_valid = 1; 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate if ((DB_REF(mp) > 1) || MBLKSIZE(mp) < 18850Sstevel@tonic-gate (sizeof (struct T_conn_res) + sizeof (intptr_t))) { 18860Sstevel@tonic-gate cred_t *cr; 18870Sstevel@tonic-gate 18880Sstevel@tonic-gate if ((cr = DB_CRED(mp)) != NULL) { 18890Sstevel@tonic-gate crhold(cr); 18900Sstevel@tonic-gate nso->so_peercred = cr; 18910Sstevel@tonic-gate nso->so_cpid = DB_CPID(mp); 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate freemsg(mp); 18940Sstevel@tonic-gate 18950Sstevel@tonic-gate mp = soallocproto1(NULL, sizeof (struct T_conn_res) + 18960Sstevel@tonic-gate sizeof (intptr_t), 0, _ALLOC_INTR); 18970Sstevel@tonic-gate if (mp == NULL) { 18980Sstevel@tonic-gate /* 18990Sstevel@tonic-gate * Accept can not fail with ENOBUFS. 19000Sstevel@tonic-gate * A signal was caught so return EINTR. 19010Sstevel@tonic-gate */ 19020Sstevel@tonic-gate error = EINTR; 19030Sstevel@tonic-gate eprintsoline(so, error); 19040Sstevel@tonic-gate goto disconnect_vp_unlocked; 19050Sstevel@tonic-gate } 19060Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr; 19070Sstevel@tonic-gate } else { 19080Sstevel@tonic-gate nso->so_peercred = DB_CRED(mp); 19090Sstevel@tonic-gate nso->so_cpid = DB_CPID(mp); 19100Sstevel@tonic-gate DB_CRED(mp) = NULL; 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate mp->b_rptr = DB_BASE(mp); 19130Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr; 19140Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res); 19150Sstevel@tonic-gate } 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate /* 19180Sstevel@tonic-gate * New socket must be bound at least in sockfs and, except for AF_INET, 19190Sstevel@tonic-gate * (or AF_INET6) it also has to be bound in the transport provider. 19204379Sja97890 * We set the local address in the sonode from the T_OK_ACK of the 19214379Sja97890 * T_CONN_RES. For this reason the address we bind to here isn't 19224379Sja97890 * important. 19230Sstevel@tonic-gate */ 19240Sstevel@tonic-gate if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) && 19250Sstevel@tonic-gate /*CONSTCOND*/ 19260Sstevel@tonic-gate nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) { 19270Sstevel@tonic-gate /* 19280Sstevel@tonic-gate * Optimization for AF_INET{,6} transports 19290Sstevel@tonic-gate * that can handle a T_CONN_RES without being bound. 19300Sstevel@tonic-gate */ 19310Sstevel@tonic-gate mutex_enter(&nso->so_lock); 19320Sstevel@tonic-gate so_automatic_bind(nso); 19330Sstevel@tonic-gate mutex_exit(&nso->so_lock); 19340Sstevel@tonic-gate } else { 19350Sstevel@tonic-gate /* Perform NULL bind with the transport provider. */ 19368348SEric.Yu@Sun.COM if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC, 19378348SEric.Yu@Sun.COM cr)) != 0) { 19380Sstevel@tonic-gate ASSERT(error != ENOBUFS); 19390Sstevel@tonic-gate freemsg(mp); 19400Sstevel@tonic-gate eprintsoline(nso, error); 19410Sstevel@tonic-gate goto disconnect_vp_unlocked; 19420Sstevel@tonic-gate } 19430Sstevel@tonic-gate } 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate /* 19460Sstevel@tonic-gate * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES 19470Sstevel@tonic-gate * so that any data arriving on the new socket will cause the 19480Sstevel@tonic-gate * appropriate signals to be delivered for the new socket. 19490Sstevel@tonic-gate * 19500Sstevel@tonic-gate * No other thread (except strsock_proto and strsock_misc) 19510Sstevel@tonic-gate * can access the new socket thus we relax the locking. 19520Sstevel@tonic-gate */ 19530Sstevel@tonic-gate nso->so_pgrp = so->so_pgrp; 19548348SEric.Yu@Sun.COM nso->so_state |= so->so_state & SS_ASYNC; 19558348SEric.Yu@Sun.COM nsti->sti_faddr_noxlate = sti->sti_faddr_noxlate; 19560Sstevel@tonic-gate 19570Sstevel@tonic-gate if (nso->so_pgrp != 0) { 19580Sstevel@tonic-gate if ((error = so_set_events(nso, nvp, CRED())) != 0) { 19590Sstevel@tonic-gate eprintsoline(nso, error); 19600Sstevel@tonic-gate error = 0; 19610Sstevel@tonic-gate nso->so_pgrp = 0; 19620Sstevel@tonic-gate } 19630Sstevel@tonic-gate } 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate /* 19660Sstevel@tonic-gate * Make note of the socket level options. TCP and IP level options 19670Sstevel@tonic-gate * are already inherited. We could do all this after accept is 19680Sstevel@tonic-gate * successful but doing it here simplifies code and no harm done 19690Sstevel@tonic-gate * for error case. 19700Sstevel@tonic-gate */ 19710Sstevel@tonic-gate nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE| 19720Sstevel@tonic-gate SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK| 19730Sstevel@tonic-gate SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER); 19740Sstevel@tonic-gate nso->so_sndbuf = so->so_sndbuf; 19750Sstevel@tonic-gate nso->so_rcvbuf = so->so_rcvbuf; 19760Sstevel@tonic-gate if (nso->so_options & SO_LINGER) 19770Sstevel@tonic-gate nso->so_linger = so->so_linger; 19780Sstevel@tonic-gate 19798348SEric.Yu@Sun.COM /* 19808348SEric.Yu@Sun.COM * Note that the following sti_direct code path should be 19818348SEric.Yu@Sun.COM * removed once we are confident that the direct sockets 19828348SEric.Yu@Sun.COM * do not result in any degradation. 19838348SEric.Yu@Sun.COM */ 19848348SEric.Yu@Sun.COM if (sti->sti_direct) { 19850Sstevel@tonic-gate 19860Sstevel@tonic-gate ASSERT(opt != NULL); 19870Sstevel@tonic-gate 19880Sstevel@tonic-gate conn_res->OPT_length = optlen; 19890Sstevel@tonic-gate conn_res->OPT_offset = MBLKL(mp); 19900Sstevel@tonic-gate bcopy(&opt, mp->b_wptr, optlen); 19910Sstevel@tonic-gate mp->b_wptr += optlen; 19920Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES; 19930Sstevel@tonic-gate conn_res->ACCEPTOR_id = 0; 19940Sstevel@tonic-gate PRIM_type = T_CONN_RES; 19950Sstevel@tonic-gate 19960Sstevel@tonic-gate /* Send down the T_CONN_RES on acceptor STREAM */ 19970Sstevel@tonic-gate error = kstrputmsg(SOTOV(nso), mp, NULL, 19980Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 19990Sstevel@tonic-gate if (error) { 20000Sstevel@tonic-gate mutex_enter(&so->so_lock); 20010Sstevel@tonic-gate so_lock_single(so); 20020Sstevel@tonic-gate eprintsoline(so, error); 20030Sstevel@tonic-gate goto disconnect_vp; 20040Sstevel@tonic-gate } 20050Sstevel@tonic-gate mutex_enter(&nso->so_lock); 20060Sstevel@tonic-gate error = sowaitprim(nso, T_CONN_RES, T_OK_ACK, 20070Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 20080Sstevel@tonic-gate if (error) { 20090Sstevel@tonic-gate mutex_exit(&nso->so_lock); 20100Sstevel@tonic-gate mutex_enter(&so->so_lock); 20110Sstevel@tonic-gate so_lock_single(so); 20120Sstevel@tonic-gate eprintsoline(so, error); 20130Sstevel@tonic-gate goto disconnect_vp; 20140Sstevel@tonic-gate } 20150Sstevel@tonic-gate if (nso->so_family == AF_INET) { 20160Sstevel@tonic-gate sin_t *sin; 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate sin = (sin_t *)(ack_mp->b_rptr + 20190Sstevel@tonic-gate sizeof (struct T_ok_ack)); 20208348SEric.Yu@Sun.COM bcopy(sin, nsti->sti_laddr_sa, sizeof (sin_t)); 20218348SEric.Yu@Sun.COM nsti->sti_laddr_len = sizeof (sin_t); 20220Sstevel@tonic-gate } else { 20230Sstevel@tonic-gate sin6_t *sin6; 20240Sstevel@tonic-gate 20250Sstevel@tonic-gate sin6 = (sin6_t *)(ack_mp->b_rptr + 20260Sstevel@tonic-gate sizeof (struct T_ok_ack)); 20278348SEric.Yu@Sun.COM bcopy(sin6, nsti->sti_laddr_sa, sizeof (sin6_t)); 20288348SEric.Yu@Sun.COM nsti->sti_laddr_len = sizeof (sin6_t); 20290Sstevel@tonic-gate } 20300Sstevel@tonic-gate freemsg(ack_mp); 20310Sstevel@tonic-gate 20328348SEric.Yu@Sun.COM nso->so_state |= SS_ISCONNECTED; 20338348SEric.Yu@Sun.COM nso->so_proto_handle = (sock_lower_handle_t)opt; 20348348SEric.Yu@Sun.COM nsti->sti_laddr_valid = 1; 20358348SEric.Yu@Sun.COM 20368348SEric.Yu@Sun.COM if (sti->sti_nl7c_flags & NL7C_ENABLED) { 20370Sstevel@tonic-gate /* 20381974Sbrutus * A NL7C marked listen()er so the new socket 20391974Sbrutus * inherits the listen()er's NL7C state, except 20401974Sbrutus * for NL7C_POLLIN. 20410Sstevel@tonic-gate * 20421974Sbrutus * Only call NL7C to process the new socket if 20431974Sbrutus * the listen socket allows blocking i/o. 20440Sstevel@tonic-gate */ 20458348SEric.Yu@Sun.COM nsti->sti_nl7c_flags = 20468348SEric.Yu@Sun.COM sti->sti_nl7c_flags & (~NL7C_POLLIN); 20471974Sbrutus if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) { 20481974Sbrutus /* 20491974Sbrutus * Nonblocking accept() just make it 20501974Sbrutus * persist to defer processing to the 20511974Sbrutus * read-side syscall (e.g. read). 20521974Sbrutus */ 20538348SEric.Yu@Sun.COM nsti->sti_nl7c_flags |= NL7C_SOPERSIST; 20541974Sbrutus } else if (nl7c_process(nso, B_FALSE)) { 20550Sstevel@tonic-gate /* 20560Sstevel@tonic-gate * NL7C has completed processing on the 20570Sstevel@tonic-gate * socket, close the socket and back to 20580Sstevel@tonic-gate * the top to await the next T_CONN_IND. 20590Sstevel@tonic-gate */ 20600Sstevel@tonic-gate mutex_exit(&nso->so_lock); 20610Sstevel@tonic-gate (void) VOP_CLOSE(nvp, 0, 1, (offset_t)0, 20625331Samw CRED(), NULL); 20630Sstevel@tonic-gate VN_RELE(nvp); 20640Sstevel@tonic-gate goto again; 20650Sstevel@tonic-gate } 20660Sstevel@tonic-gate /* Pass the new socket out */ 20670Sstevel@tonic-gate } 20680Sstevel@tonic-gate 20690Sstevel@tonic-gate mutex_exit(&nso->so_lock); 20700Sstevel@tonic-gate 20710Sstevel@tonic-gate /* 20722811Sja97890 * It's possible, through the use of autopush for example, 20738348SEric.Yu@Sun.COM * that the acceptor stream may not support sti_direct 20748348SEric.Yu@Sun.COM * semantics. If the new socket does not support sti_direct 20752811Sja97890 * we issue a _SIOCSOCKFALLBACK to inform the transport 20762811Sja97890 * as we would in the I_PUSH case. 20772811Sja97890 */ 20788348SEric.Yu@Sun.COM if (nsti->sti_direct == 0) { 20792811Sja97890 int rval; 20802811Sja97890 20812811Sja97890 if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK, 20822811Sja97890 0, 0, K_TO_K, CRED(), &rval)) != 0) { 20832811Sja97890 mutex_enter(&so->so_lock); 20842811Sja97890 so_lock_single(so); 20852811Sja97890 eprintsoline(so, error); 20862811Sja97890 goto disconnect_vp; 20872811Sja97890 } 20882811Sja97890 } 20892811Sja97890 20902811Sja97890 /* 20910Sstevel@tonic-gate * Pass out new socket. 20920Sstevel@tonic-gate */ 20930Sstevel@tonic-gate if (nsop != NULL) 20940Sstevel@tonic-gate *nsop = nso; 20950Sstevel@tonic-gate 20960Sstevel@tonic-gate return (0); 20970Sstevel@tonic-gate } 20980Sstevel@tonic-gate 20990Sstevel@tonic-gate /* 21000Sstevel@tonic-gate * This is the non-performance case for sockets (e.g. AF_UNIX sockets) 21010Sstevel@tonic-gate * which don't support the FireEngine accept fast-path. It is also 21020Sstevel@tonic-gate * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd 21030Sstevel@tonic-gate * again. Neither sockfs nor TCP attempt to find out if some other 21040Sstevel@tonic-gate * random module has been inserted in between (in which case we 21050Sstevel@tonic-gate * should follow TLI accept behaviour). We blindly assume the worst 21060Sstevel@tonic-gate * case and revert back to old behaviour i.e. TCP will not send us 21070Sstevel@tonic-gate * any option (eager) and the accept should happen on the listener 21080Sstevel@tonic-gate * queue. Any queued T_conn_ind have already got their options removed 21090Sstevel@tonic-gate * by so_sock2_stream() when "sockmod" was I_POP'd. 21100Sstevel@tonic-gate */ 21110Sstevel@tonic-gate /* 21120Sstevel@tonic-gate * Fill in the {O_}T_CONN_RES before getting SOLOCKED. 21130Sstevel@tonic-gate */ 21140Sstevel@tonic-gate if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) { 21150Sstevel@tonic-gate #ifdef _ILP32 21160Sstevel@tonic-gate queue_t *q; 21170Sstevel@tonic-gate 21180Sstevel@tonic-gate /* 21190Sstevel@tonic-gate * Find read queue in driver 21200Sstevel@tonic-gate * Can safely do this since we "own" nso/nvp. 21210Sstevel@tonic-gate */ 21220Sstevel@tonic-gate q = strvp2wq(nvp)->q_next; 21230Sstevel@tonic-gate while (SAMESTR(q)) 21240Sstevel@tonic-gate q = q->q_next; 21250Sstevel@tonic-gate q = RD(q); 21260Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)q; 21270Sstevel@tonic-gate #else 21280Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev); 21290Sstevel@tonic-gate #endif /* _ILP32 */ 21300Sstevel@tonic-gate conn_res->PRIM_type = O_T_CONN_RES; 21310Sstevel@tonic-gate PRIM_type = O_T_CONN_RES; 21320Sstevel@tonic-gate } else { 21338348SEric.Yu@Sun.COM conn_res->ACCEPTOR_id = nsti->sti_acceptor_id; 21340Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES; 21350Sstevel@tonic-gate PRIM_type = T_CONN_RES; 21360Sstevel@tonic-gate } 21370Sstevel@tonic-gate conn_res->SEQ_number = SEQ_number; 21380Sstevel@tonic-gate conn_res->OPT_length = 0; 21390Sstevel@tonic-gate conn_res->OPT_offset = 0; 21400Sstevel@tonic-gate 21410Sstevel@tonic-gate mutex_enter(&so->so_lock); 21420Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 21430Sstevel@tonic-gate mutex_exit(&so->so_lock); 21440Sstevel@tonic-gate 21450Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 21460Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 21470Sstevel@tonic-gate mutex_enter(&so->so_lock); 21480Sstevel@tonic-gate if (error) { 21490Sstevel@tonic-gate eprintsoline(so, error); 21500Sstevel@tonic-gate goto disconnect_vp; 21510Sstevel@tonic-gate } 21524379Sja97890 error = sowaitprim(so, PRIM_type, T_OK_ACK, 21534379Sja97890 (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 21540Sstevel@tonic-gate if (error) { 21550Sstevel@tonic-gate eprintsoline(so, error); 21560Sstevel@tonic-gate goto disconnect_vp; 21570Sstevel@tonic-gate } 21584379Sja97890 /* 21594379Sja97890 * If there is a sin/sin6 appended onto the T_OK_ACK use 21604379Sja97890 * that to set the local address. If this is not present 21614379Sja97890 * then we zero out the address and don't set the 21628348SEric.Yu@Sun.COM * sti_laddr_valid bit. For AF_UNIX endpoints we copy over 21634678Sja97890 * the pathname from the listening socket. 21644379Sja97890 */ 21654379Sja97890 sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t); 21664379Sja97890 if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) && 21674379Sja97890 MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) { 21684379Sja97890 ack_mp->b_rptr += sizeof (struct T_ok_ack); 21698348SEric.Yu@Sun.COM bcopy(ack_mp->b_rptr, nsti->sti_laddr_sa, sinlen); 21708348SEric.Yu@Sun.COM nsti->sti_laddr_len = sinlen; 21718348SEric.Yu@Sun.COM nsti->sti_laddr_valid = 1; 21724678Sja97890 } else if (nso->so_family == AF_UNIX) { 21734678Sja97890 ASSERT(so->so_family == AF_UNIX); 21748348SEric.Yu@Sun.COM nsti->sti_laddr_len = sti->sti_laddr_len; 21758348SEric.Yu@Sun.COM ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen); 21768348SEric.Yu@Sun.COM bcopy(sti->sti_laddr_sa, nsti->sti_laddr_sa, 21778348SEric.Yu@Sun.COM nsti->sti_laddr_len); 21788348SEric.Yu@Sun.COM nsti->sti_laddr_valid = 1; 21794379Sja97890 } else { 21808348SEric.Yu@Sun.COM nsti->sti_laddr_len = sti->sti_laddr_len; 21818348SEric.Yu@Sun.COM ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen); 21828348SEric.Yu@Sun.COM bzero(nsti->sti_laddr_sa, nsti->sti_addr_size); 21838348SEric.Yu@Sun.COM nsti->sti_laddr_sa->sa_family = nso->so_family; 21844379Sja97890 } 21854379Sja97890 freemsg(ack_mp); 21864379Sja97890 21870Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 21880Sstevel@tonic-gate mutex_exit(&so->so_lock); 21890Sstevel@tonic-gate 21900Sstevel@tonic-gate nso->so_state |= SS_ISCONNECTED; 21910Sstevel@tonic-gate 21920Sstevel@tonic-gate /* 21930Sstevel@tonic-gate * Pass out new socket. 21940Sstevel@tonic-gate */ 21950Sstevel@tonic-gate if (nsop != NULL) 21960Sstevel@tonic-gate *nsop = nso; 21970Sstevel@tonic-gate 21980Sstevel@tonic-gate return (0); 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate 22010Sstevel@tonic-gate eproto_disc_unl: 22020Sstevel@tonic-gate error = EPROTO; 22030Sstevel@tonic-gate e_disc_unl: 22040Sstevel@tonic-gate eprintsoline(so, error); 22050Sstevel@tonic-gate goto disconnect_unlocked; 22060Sstevel@tonic-gate 22070Sstevel@tonic-gate pr_disc_vp_unl: 22080Sstevel@tonic-gate eprintsoline(so, error); 22090Sstevel@tonic-gate disconnect_vp_unlocked: 22105331Samw (void) VOP_CLOSE(nvp, 0, 1, 0, CRED(), NULL); 22110Sstevel@tonic-gate VN_RELE(nvp); 22120Sstevel@tonic-gate disconnect_unlocked: 22130Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, 0); 22140Sstevel@tonic-gate return (error); 22150Sstevel@tonic-gate 22160Sstevel@tonic-gate pr_disc_vp: 22170Sstevel@tonic-gate eprintsoline(so, error); 22180Sstevel@tonic-gate disconnect_vp: 22190Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD); 22200Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 22210Sstevel@tonic-gate mutex_exit(&so->so_lock); 22225331Samw (void) VOP_CLOSE(nvp, 0, 1, 0, CRED(), NULL); 22230Sstevel@tonic-gate VN_RELE(nvp); 22240Sstevel@tonic-gate return (error); 22250Sstevel@tonic-gate 22260Sstevel@tonic-gate conn_bad: /* Note: SunOS 4/BSD unconditionally returns EINVAL here */ 22270Sstevel@tonic-gate error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW) 22280Sstevel@tonic-gate ? EOPNOTSUPP : EINVAL; 22290Sstevel@tonic-gate e_bad: 22300Sstevel@tonic-gate eprintsoline(so, error); 22310Sstevel@tonic-gate return (error); 22320Sstevel@tonic-gate } 22330Sstevel@tonic-gate 22340Sstevel@tonic-gate /* 22350Sstevel@tonic-gate * connect a socket. 22360Sstevel@tonic-gate * 22370Sstevel@tonic-gate * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to 22380Sstevel@tonic-gate * unconnect (by specifying a null address). 22390Sstevel@tonic-gate */ 22400Sstevel@tonic-gate int 22410Sstevel@tonic-gate sotpi_connect(struct sonode *so, 22420Sstevel@tonic-gate const struct sockaddr *name, 22430Sstevel@tonic-gate socklen_t namelen, 22440Sstevel@tonic-gate int fflag, 22458348SEric.Yu@Sun.COM int flags, 22468348SEric.Yu@Sun.COM struct cred *cr) 22470Sstevel@tonic-gate { 22480Sstevel@tonic-gate struct T_conn_req conn_req; 22490Sstevel@tonic-gate int error = 0; 22500Sstevel@tonic-gate mblk_t *mp; 22510Sstevel@tonic-gate void *src; 22520Sstevel@tonic-gate socklen_t srclen; 22530Sstevel@tonic-gate void *addr; 22540Sstevel@tonic-gate socklen_t addrlen; 22550Sstevel@tonic-gate boolean_t need_unlock; 22568348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 22570Sstevel@tonic-gate 22580Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n", 22597240Srh87107 (void *)so, (void *)name, namelen, fflag, flags, 22605240Snordmark pr_state(so->so_state, so->so_mode))); 22610Sstevel@tonic-gate 22620Sstevel@tonic-gate /* 22630Sstevel@tonic-gate * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to 22640Sstevel@tonic-gate * avoid sleeping for memory with SOLOCKED held. 22658348SEric.Yu@Sun.COM * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen 22660Sstevel@tonic-gate * + sizeof (struct T_opthdr). 22670Sstevel@tonic-gate * (the AF_UNIX so_ux_addr_xlate() does not make the address 22688348SEric.Yu@Sun.COM * exceed sti_faddr_maxlen). 22690Sstevel@tonic-gate */ 22700Sstevel@tonic-gate mp = soallocproto(sizeof (struct T_conn_req) + 22718348SEric.Yu@Sun.COM 2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR); 22720Sstevel@tonic-gate if (mp == NULL) { 22730Sstevel@tonic-gate /* 22740Sstevel@tonic-gate * Connect can not fail with ENOBUFS. A signal was 22750Sstevel@tonic-gate * caught so return EINTR. 22760Sstevel@tonic-gate */ 22770Sstevel@tonic-gate error = EINTR; 22780Sstevel@tonic-gate eprintsoline(so, error); 22790Sstevel@tonic-gate return (error); 22800Sstevel@tonic-gate } 22810Sstevel@tonic-gate 22820Sstevel@tonic-gate mutex_enter(&so->so_lock); 22830Sstevel@tonic-gate /* 22845694Sjprakash * Make sure there is a preallocated T_unbind_req message 22855694Sjprakash * before any binding. This message is allocated when the 22865694Sjprakash * socket is created. Since another thread can consume 22875694Sjprakash * so_unbind_mp by the time we return from so_lock_single(), 22885694Sjprakash * we should check the availability of so_unbind_mp after 22895694Sjprakash * we return from so_lock_single(). 22900Sstevel@tonic-gate */ 22915694Sjprakash 22925694Sjprakash so_lock_single(so); /* Set SOLOCKED */ 22935694Sjprakash need_unlock = B_TRUE; 22945694Sjprakash 22958348SEric.Yu@Sun.COM if (sti->sti_unbind_mp == NULL) { 22960Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n")); 22970Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 22988348SEric.Yu@Sun.COM sti->sti_unbind_mp = 22990Sstevel@tonic-gate soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR); 23008348SEric.Yu@Sun.COM if (sti->sti_unbind_mp == NULL) { 23010Sstevel@tonic-gate error = EINTR; 23020Sstevel@tonic-gate goto done; 23030Sstevel@tonic-gate } 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate /* 23070Sstevel@tonic-gate * Can't have done a listen before connecting. 23080Sstevel@tonic-gate */ 23090Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) { 23100Sstevel@tonic-gate error = EOPNOTSUPP; 23110Sstevel@tonic-gate goto done; 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate 23140Sstevel@tonic-gate /* 23150Sstevel@tonic-gate * Must be bound with the transport 23160Sstevel@tonic-gate */ 23170Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 23180Sstevel@tonic-gate if ((so->so_family == AF_INET || so->so_family == AF_INET6) && 23190Sstevel@tonic-gate /*CONSTCOND*/ 23200Sstevel@tonic-gate so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) { 23210Sstevel@tonic-gate /* 23220Sstevel@tonic-gate * Optimization for AF_INET{,6} transports 23230Sstevel@tonic-gate * that can handle a T_CONN_REQ without being bound. 23240Sstevel@tonic-gate */ 23250Sstevel@tonic-gate so_automatic_bind(so); 23260Sstevel@tonic-gate } else { 23270Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0, 23288348SEric.Yu@Sun.COM _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr); 23290Sstevel@tonic-gate if (error) 23300Sstevel@tonic-gate goto done; 23310Sstevel@tonic-gate } 23320Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND); 23330Sstevel@tonic-gate flags |= _SOCONNECT_DID_BIND; 23340Sstevel@tonic-gate } 23350Sstevel@tonic-gate 23360Sstevel@tonic-gate /* 23370Sstevel@tonic-gate * Handle a connect to a name parameter of type AF_UNSPEC like a 23380Sstevel@tonic-gate * connect to a null address. This is the portable method to 23390Sstevel@tonic-gate * unconnect a socket. 23400Sstevel@tonic-gate */ 23410Sstevel@tonic-gate if ((namelen >= sizeof (sa_family_t)) && 23420Sstevel@tonic-gate (name->sa_family == AF_UNSPEC)) { 23430Sstevel@tonic-gate name = NULL; 23440Sstevel@tonic-gate namelen = 0; 23450Sstevel@tonic-gate } 23460Sstevel@tonic-gate 23470Sstevel@tonic-gate /* 23480Sstevel@tonic-gate * Check that we are not already connected. 23490Sstevel@tonic-gate * A connection-oriented socket cannot be reconnected. 23500Sstevel@tonic-gate * A connected connection-less socket can be 23510Sstevel@tonic-gate * - connected to a different address by a subsequent connect 23520Sstevel@tonic-gate * - "unconnected" by a connect to the NULL address 23530Sstevel@tonic-gate */ 23540Sstevel@tonic-gate if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) { 23550Sstevel@tonic-gate ASSERT(!(flags & _SOCONNECT_DID_BIND)); 23560Sstevel@tonic-gate if (so->so_mode & SM_CONNREQUIRED) { 23570Sstevel@tonic-gate /* Connection-oriented socket */ 23580Sstevel@tonic-gate error = so->so_state & SS_ISCONNECTED ? 23590Sstevel@tonic-gate EISCONN : EALREADY; 23600Sstevel@tonic-gate goto done; 23610Sstevel@tonic-gate } 23620Sstevel@tonic-gate /* Connection-less socket */ 23630Sstevel@tonic-gate if (name == NULL) { 23640Sstevel@tonic-gate /* 23650Sstevel@tonic-gate * Remove the connected state and clear SO_DGRAM_ERRIND 23660Sstevel@tonic-gate * since it was set when the socket was connected. 23670Sstevel@tonic-gate * If this is UDP also send down a T_DISCON_REQ. 23680Sstevel@tonic-gate */ 23690Sstevel@tonic-gate int val; 23700Sstevel@tonic-gate 23710Sstevel@tonic-gate if ((so->so_family == AF_INET || 23725240Snordmark so->so_family == AF_INET6) && 23730Sstevel@tonic-gate (so->so_type == SOCK_DGRAM || 23745240Snordmark so->so_type == SOCK_RAW) && 23750Sstevel@tonic-gate /*CONSTCOND*/ 23760Sstevel@tonic-gate !soconnect_tpi_udp) { 23770Sstevel@tonic-gate /* XXX What about implicitly unbinding here? */ 23780Sstevel@tonic-gate error = sodisconnect(so, -1, 23795240Snordmark _SODISCONNECT_LOCK_HELD); 23800Sstevel@tonic-gate } else { 23810Sstevel@tonic-gate so->so_state &= 23828348SEric.Yu@Sun.COM ~(SS_ISCONNECTED | SS_ISCONNECTING); 23838348SEric.Yu@Sun.COM sti->sti_faddr_valid = 0; 23848348SEric.Yu@Sun.COM sti->sti_faddr_len = 0; 23850Sstevel@tonic-gate } 23860Sstevel@tonic-gate 23878348SEric.Yu@Sun.COM /* Remove SOLOCKED since setsockopt will grab it */ 23880Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 23890Sstevel@tonic-gate mutex_exit(&so->so_lock); 23900Sstevel@tonic-gate 23910Sstevel@tonic-gate val = 0; 23928348SEric.Yu@Sun.COM (void) sotpi_setsockopt(so, SOL_SOCKET, 23938348SEric.Yu@Sun.COM SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val), 23948348SEric.Yu@Sun.COM cr); 23950Sstevel@tonic-gate 23960Sstevel@tonic-gate mutex_enter(&so->so_lock); 23970Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 23980Sstevel@tonic-gate goto done; 23990Sstevel@tonic-gate } 24000Sstevel@tonic-gate } 24010Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND); 24020Sstevel@tonic-gate 24030Sstevel@tonic-gate if (name == NULL || namelen == 0) { 24040Sstevel@tonic-gate error = EINVAL; 24050Sstevel@tonic-gate goto done; 24060Sstevel@tonic-gate } 24070Sstevel@tonic-gate /* 24088348SEric.Yu@Sun.COM * Mark the socket if sti_faddr_sa represents the transport level 24090Sstevel@tonic-gate * address. 24100Sstevel@tonic-gate */ 24110Sstevel@tonic-gate if (flags & _SOCONNECT_NOXLATE) { 24120Sstevel@tonic-gate struct sockaddr_ux *soaddr_ux; 24130Sstevel@tonic-gate 24140Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 24150Sstevel@tonic-gate if (namelen != sizeof (struct sockaddr_ux)) { 24160Sstevel@tonic-gate error = EINVAL; 24170Sstevel@tonic-gate goto done; 24180Sstevel@tonic-gate } 24190Sstevel@tonic-gate soaddr_ux = (struct sockaddr_ux *)name; 24200Sstevel@tonic-gate name = (struct sockaddr *)&soaddr_ux->sou_addr; 24210Sstevel@tonic-gate namelen = sizeof (soaddr_ux->sou_addr); 24228348SEric.Yu@Sun.COM sti->sti_faddr_noxlate = 1; 24230Sstevel@tonic-gate } 24240Sstevel@tonic-gate 24250Sstevel@tonic-gate /* 24260Sstevel@tonic-gate * Length and family checks. 24270Sstevel@tonic-gate */ 24280Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 24290Sstevel@tonic-gate if (error) 24300Sstevel@tonic-gate goto bad; 24310Sstevel@tonic-gate 24320Sstevel@tonic-gate /* 24330Sstevel@tonic-gate * Save foreign address. Needed for AF_UNIX as well as 24340Sstevel@tonic-gate * transport providers that do not support TI_GETPEERNAME. 24350Sstevel@tonic-gate * Also used for cached foreign address for TCP and UDP. 24360Sstevel@tonic-gate */ 24378348SEric.Yu@Sun.COM if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) { 24380Sstevel@tonic-gate error = EINVAL; 24390Sstevel@tonic-gate goto done; 24400Sstevel@tonic-gate } 24418348SEric.Yu@Sun.COM sti->sti_faddr_len = (socklen_t)namelen; 24428348SEric.Yu@Sun.COM ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen); 24438348SEric.Yu@Sun.COM bcopy(name, sti->sti_faddr_sa, namelen); 24448348SEric.Yu@Sun.COM sti->sti_faddr_valid = 1; 24450Sstevel@tonic-gate 24460Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 24478348SEric.Yu@Sun.COM if (sti->sti_faddr_noxlate) { 24480Sstevel@tonic-gate /* 24490Sstevel@tonic-gate * Already have a transport internal address. Do not 24500Sstevel@tonic-gate * pass any (transport internal) source address. 24510Sstevel@tonic-gate */ 24528348SEric.Yu@Sun.COM addr = sti->sti_faddr_sa; 24538348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sti->sti_faddr_len; 24540Sstevel@tonic-gate src = NULL; 24550Sstevel@tonic-gate srclen = 0; 24560Sstevel@tonic-gate } else { 24570Sstevel@tonic-gate /* 24580Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 24590Sstevel@tonic-gate * and translate the remote address. 24608348SEric.Yu@Sun.COM * Holding so_lock thus sti_laddr_sa can not change. 24610Sstevel@tonic-gate */ 24628348SEric.Yu@Sun.COM src = sti->sti_laddr_sa; 24638348SEric.Yu@Sun.COM srclen = (t_uscalar_t)sti->sti_laddr_len; 24640Sstevel@tonic-gate dprintso(so, 1, 24655240Snordmark ("sotpi_connect UNIX: srclen %d, src %p\n", 24665240Snordmark srclen, src)); 24670Sstevel@tonic-gate error = so_ux_addr_xlate(so, 24688348SEric.Yu@Sun.COM sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len, 24695240Snordmark (flags & _SOCONNECT_XPG4_2), 24705240Snordmark &addr, &addrlen); 24710Sstevel@tonic-gate if (error) 24720Sstevel@tonic-gate goto bad; 24730Sstevel@tonic-gate } 24740Sstevel@tonic-gate } else { 24758348SEric.Yu@Sun.COM addr = sti->sti_faddr_sa; 24768348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sti->sti_faddr_len; 24770Sstevel@tonic-gate src = NULL; 24780Sstevel@tonic-gate srclen = 0; 24790Sstevel@tonic-gate } 24800Sstevel@tonic-gate /* 24810Sstevel@tonic-gate * When connecting a datagram socket we issue the SO_DGRAM_ERRIND 24820Sstevel@tonic-gate * option which asks the transport provider to send T_UDERR_IND 24830Sstevel@tonic-gate * messages. These T_UDERR_IND messages are used to return connected 24840Sstevel@tonic-gate * style errors (e.g. ECONNRESET) for connected datagram sockets. 24850Sstevel@tonic-gate * 24860Sstevel@tonic-gate * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets) 24870Sstevel@tonic-gate * we send down a T_CONN_REQ. This is needed to let the 24880Sstevel@tonic-gate * transport assign a local address that is consistent with 24890Sstevel@tonic-gate * the remote address. Applications depend on a getsockname() 24900Sstevel@tonic-gate * after a connect() to retrieve the "source" IP address for 24910Sstevel@tonic-gate * the connected socket. Invalidate the cached local address 24920Sstevel@tonic-gate * to force getsockname() to enquire of the transport. 24930Sstevel@tonic-gate */ 24940Sstevel@tonic-gate if (!(so->so_mode & SM_CONNREQUIRED)) { 24950Sstevel@tonic-gate /* 24960Sstevel@tonic-gate * Datagram socket. 24970Sstevel@tonic-gate */ 24980Sstevel@tonic-gate int32_t val; 24990Sstevel@tonic-gate 25000Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 25010Sstevel@tonic-gate mutex_exit(&so->so_lock); 25020Sstevel@tonic-gate 25030Sstevel@tonic-gate val = 1; 25040Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND, 25058348SEric.Yu@Sun.COM &val, (t_uscalar_t)sizeof (val), cr); 25060Sstevel@tonic-gate 25070Sstevel@tonic-gate mutex_enter(&so->so_lock); 25080Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 25090Sstevel@tonic-gate if ((so->so_family != AF_INET && so->so_family != AF_INET6) || 25100Sstevel@tonic-gate (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) || 25110Sstevel@tonic-gate soconnect_tpi_udp) { 25120Sstevel@tonic-gate soisconnected(so); 25130Sstevel@tonic-gate goto done; 25140Sstevel@tonic-gate } 25150Sstevel@tonic-gate /* 25160Sstevel@tonic-gate * Send down T_CONN_REQ etc. 25170Sstevel@tonic-gate * Clear fflag to avoid returning EWOULDBLOCK. 25180Sstevel@tonic-gate */ 25190Sstevel@tonic-gate fflag = 0; 25200Sstevel@tonic-gate ASSERT(so->so_family != AF_UNIX); 25218348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 25228348SEric.Yu@Sun.COM } else if (sti->sti_laddr_len != 0) { 25230Sstevel@tonic-gate /* 25240Sstevel@tonic-gate * If the local address or port was "any" then it may be 25250Sstevel@tonic-gate * changed by the transport as a result of the 25260Sstevel@tonic-gate * connect. Invalidate the cached version if we have one. 25270Sstevel@tonic-gate */ 25280Sstevel@tonic-gate switch (so->so_family) { 25290Sstevel@tonic-gate case AF_INET: 25308348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t)); 25318348SEric.Yu@Sun.COM if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr == 25320Sstevel@tonic-gate INADDR_ANY || 25338348SEric.Yu@Sun.COM ((sin_t *)sti->sti_laddr_sa)->sin_port == 0) 25348348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 25350Sstevel@tonic-gate break; 25360Sstevel@tonic-gate 25370Sstevel@tonic-gate case AF_INET6: 25388348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_len == 25398348SEric.Yu@Sun.COM (socklen_t)sizeof (sin6_t)); 25400Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED( 25418348SEric.Yu@Sun.COM &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) || 25420Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED_ANY( 25438348SEric.Yu@Sun.COM &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) || 25448348SEric.Yu@Sun.COM ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0) 25458348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 25460Sstevel@tonic-gate break; 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate default: 25490Sstevel@tonic-gate break; 25500Sstevel@tonic-gate } 25510Sstevel@tonic-gate } 25520Sstevel@tonic-gate 25530Sstevel@tonic-gate /* 25540Sstevel@tonic-gate * Check for failure of an earlier call 25550Sstevel@tonic-gate */ 25560Sstevel@tonic-gate if (so->so_error != 0) 25570Sstevel@tonic-gate goto so_bad; 25580Sstevel@tonic-gate 25590Sstevel@tonic-gate /* 25600Sstevel@tonic-gate * Send down T_CONN_REQ. Message was allocated above. 25610Sstevel@tonic-gate */ 25620Sstevel@tonic-gate conn_req.PRIM_type = T_CONN_REQ; 25630Sstevel@tonic-gate conn_req.DEST_length = addrlen; 25640Sstevel@tonic-gate conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req); 25650Sstevel@tonic-gate if (srclen == 0) { 25660Sstevel@tonic-gate conn_req.OPT_length = 0; 25670Sstevel@tonic-gate conn_req.OPT_offset = 0; 25680Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req)); 25690Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 25700Sstevel@tonic-gate } else { 25710Sstevel@tonic-gate /* 25720Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 25730Sstevel@tonic-gate * address option. 25740Sstevel@tonic-gate */ 25750Sstevel@tonic-gate struct T_opthdr toh; 25760Sstevel@tonic-gate 25770Sstevel@tonic-gate toh.level = SOL_SOCKET; 25780Sstevel@tonic-gate toh.name = SO_SRCADDR; 25790Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 25800Sstevel@tonic-gate toh.status = 0; 25810Sstevel@tonic-gate conn_req.OPT_length = 25825240Snordmark (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen)); 25830Sstevel@tonic-gate conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) + 25845240Snordmark _TPI_ALIGN_TOPT(addrlen)); 25850Sstevel@tonic-gate 25860Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req)); 25870Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 25880Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 25890Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 25900Sstevel@tonic-gate soappendmsg(mp, src, srclen); 25910Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 25920Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 25930Sstevel@tonic-gate } 25940Sstevel@tonic-gate /* 25950Sstevel@tonic-gate * Set SS_ISCONNECTING before sending down the T_CONN_REQ 25960Sstevel@tonic-gate * in order to have the right state when the T_CONN_CON shows up. 25970Sstevel@tonic-gate */ 25980Sstevel@tonic-gate soisconnecting(so); 25990Sstevel@tonic-gate mutex_exit(&so->so_lock); 26000Sstevel@tonic-gate 26010Sstevel@tonic-gate if (audit_active) 26020Sstevel@tonic-gate audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0); 26030Sstevel@tonic-gate 26040Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 26055240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 26060Sstevel@tonic-gate mp = NULL; 26070Sstevel@tonic-gate mutex_enter(&so->so_lock); 26080Sstevel@tonic-gate if (error != 0) 26090Sstevel@tonic-gate goto bad; 26100Sstevel@tonic-gate 26110Sstevel@tonic-gate if ((error = sowaitokack(so, T_CONN_REQ)) != 0) 26120Sstevel@tonic-gate goto bad; 26130Sstevel@tonic-gate 26140Sstevel@tonic-gate /* Allow other threads to access the socket */ 26150Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 26160Sstevel@tonic-gate need_unlock = B_FALSE; 26170Sstevel@tonic-gate 26180Sstevel@tonic-gate /* 26190Sstevel@tonic-gate * Wait until we get a T_CONN_CON or an error 26200Sstevel@tonic-gate */ 26210Sstevel@tonic-gate if ((error = sowaitconnected(so, fflag, 0)) != 0) { 26220Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 26230Sstevel@tonic-gate need_unlock = B_TRUE; 26240Sstevel@tonic-gate } 26250Sstevel@tonic-gate 26260Sstevel@tonic-gate done: 26270Sstevel@tonic-gate freemsg(mp); 26280Sstevel@tonic-gate switch (error) { 26290Sstevel@tonic-gate case EINPROGRESS: 26300Sstevel@tonic-gate case EALREADY: 26310Sstevel@tonic-gate case EISCONN: 26320Sstevel@tonic-gate case EINTR: 26330Sstevel@tonic-gate /* Non-fatal errors */ 26348348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 26350Sstevel@tonic-gate /* FALLTHRU */ 26360Sstevel@tonic-gate case 0: 26370Sstevel@tonic-gate break; 26380Sstevel@tonic-gate default: 26390Sstevel@tonic-gate ASSERT(need_unlock); 26400Sstevel@tonic-gate /* 26410Sstevel@tonic-gate * Fatal errors: clear SS_ISCONNECTING in case it was set, 26420Sstevel@tonic-gate * and invalidate local-address cache 26430Sstevel@tonic-gate */ 26448348SEric.Yu@Sun.COM so->so_state &= ~SS_ISCONNECTING; 26458348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 26460Sstevel@tonic-gate /* A discon_ind might have already unbound us */ 26470Sstevel@tonic-gate if ((flags & _SOCONNECT_DID_BIND) && 26480Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) { 26490Sstevel@tonic-gate int err; 26500Sstevel@tonic-gate 26510Sstevel@tonic-gate err = sotpi_unbind(so, 0); 26520Sstevel@tonic-gate /* LINTED - statement has no conseq */ 26530Sstevel@tonic-gate if (err) { 26540Sstevel@tonic-gate eprintsoline(so, err); 26550Sstevel@tonic-gate } 26560Sstevel@tonic-gate } 26570Sstevel@tonic-gate break; 26580Sstevel@tonic-gate } 26590Sstevel@tonic-gate if (need_unlock) 26600Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 26610Sstevel@tonic-gate mutex_exit(&so->so_lock); 26620Sstevel@tonic-gate return (error); 26630Sstevel@tonic-gate 26648348SEric.Yu@Sun.COM so_bad: error = sogeterr(so, B_TRUE); 26650Sstevel@tonic-gate bad: eprintsoline(so, error); 26660Sstevel@tonic-gate goto done; 26670Sstevel@tonic-gate } 26680Sstevel@tonic-gate 26698348SEric.Yu@Sun.COM /* ARGSUSED */ 26700Sstevel@tonic-gate int 26718348SEric.Yu@Sun.COM sotpi_shutdown(struct sonode *so, int how, struct cred *cr) 26720Sstevel@tonic-gate { 26730Sstevel@tonic-gate struct T_ordrel_req ordrel_req; 26740Sstevel@tonic-gate mblk_t *mp; 26750Sstevel@tonic-gate uint_t old_state, state_change; 26760Sstevel@tonic-gate int error = 0; 26778348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 26780Sstevel@tonic-gate 26790Sstevel@tonic-gate dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n", 26807240Srh87107 (void *)so, how, pr_state(so->so_state, so->so_mode))); 26810Sstevel@tonic-gate 26820Sstevel@tonic-gate mutex_enter(&so->so_lock); 26830Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 26840Sstevel@tonic-gate 26850Sstevel@tonic-gate /* 26860Sstevel@tonic-gate * SunOS 4.X has no check for datagram sockets. 26870Sstevel@tonic-gate * 5.X checks that it is connected (ENOTCONN) 26880Sstevel@tonic-gate * X/Open requires that we check the connected state. 26890Sstevel@tonic-gate */ 26900Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) { 26910Sstevel@tonic-gate if (!xnet_skip_checks) { 26920Sstevel@tonic-gate error = ENOTCONN; 26930Sstevel@tonic-gate if (xnet_check_print) { 26940Sstevel@tonic-gate printf("sockfs: X/Open shutdown check " 26955240Snordmark "caused ENOTCONN\n"); 26960Sstevel@tonic-gate } 26970Sstevel@tonic-gate } 26980Sstevel@tonic-gate goto done; 26990Sstevel@tonic-gate } 27000Sstevel@tonic-gate /* 27010Sstevel@tonic-gate * Record the current state and then perform any state changes. 27020Sstevel@tonic-gate * Then use the difference between the old and new states to 27030Sstevel@tonic-gate * determine which messages need to be sent. 27040Sstevel@tonic-gate * This prevents e.g. duplicate T_ORDREL_REQ when there are 27050Sstevel@tonic-gate * duplicate calls to shutdown(). 27060Sstevel@tonic-gate */ 27070Sstevel@tonic-gate old_state = so->so_state; 27080Sstevel@tonic-gate 27090Sstevel@tonic-gate switch (how) { 27100Sstevel@tonic-gate case 0: 27110Sstevel@tonic-gate socantrcvmore(so); 27120Sstevel@tonic-gate break; 27130Sstevel@tonic-gate case 1: 27140Sstevel@tonic-gate socantsendmore(so); 27150Sstevel@tonic-gate break; 27160Sstevel@tonic-gate case 2: 27170Sstevel@tonic-gate socantsendmore(so); 27180Sstevel@tonic-gate socantrcvmore(so); 27190Sstevel@tonic-gate break; 27200Sstevel@tonic-gate default: 27210Sstevel@tonic-gate error = EINVAL; 27220Sstevel@tonic-gate goto done; 27230Sstevel@tonic-gate } 27240Sstevel@tonic-gate 27250Sstevel@tonic-gate /* 27260Sstevel@tonic-gate * Assumes that the SS_CANT* flags are never cleared in the above code. 27270Sstevel@tonic-gate */ 27280Sstevel@tonic-gate state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) - 27295240Snordmark (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)); 27300Sstevel@tonic-gate ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0); 27310Sstevel@tonic-gate 27320Sstevel@tonic-gate switch (state_change) { 27330Sstevel@tonic-gate case 0: 27340Sstevel@tonic-gate dprintso(so, 1, 27350Sstevel@tonic-gate ("sotpi_shutdown: nothing to send in state 0x%x\n", 27360Sstevel@tonic-gate so->so_state)); 27370Sstevel@tonic-gate goto done; 27380Sstevel@tonic-gate 27390Sstevel@tonic-gate case SS_CANTRCVMORE: 27400Sstevel@tonic-gate mutex_exit(&so->so_lock); 27410Sstevel@tonic-gate strseteof(SOTOV(so), 1); 27420Sstevel@tonic-gate /* 27430Sstevel@tonic-gate * strseteof takes care of read side wakeups, 27440Sstevel@tonic-gate * pollwakeups, and signals. 27450Sstevel@tonic-gate */ 27460Sstevel@tonic-gate /* 27470Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems 27480Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 27490Sstevel@tonic-gate */ 27500Sstevel@tonic-gate mutex_enter(&so->so_lock); 27510Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 27520Sstevel@tonic-gate mutex_exit(&so->so_lock); 27530Sstevel@tonic-gate 27540Sstevel@tonic-gate /* Flush read side queue */ 27550Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL); 27560Sstevel@tonic-gate 27570Sstevel@tonic-gate mutex_enter(&so->so_lock); 27580Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 27590Sstevel@tonic-gate break; 27600Sstevel@tonic-gate 27610Sstevel@tonic-gate case SS_CANTSENDMORE: 27620Sstevel@tonic-gate mutex_exit(&so->so_lock); 27630Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 27640Sstevel@tonic-gate mutex_enter(&so->so_lock); 27650Sstevel@tonic-gate break; 27660Sstevel@tonic-gate 27670Sstevel@tonic-gate case SS_CANTSENDMORE|SS_CANTRCVMORE: 27680Sstevel@tonic-gate mutex_exit(&so->so_lock); 27690Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 27700Sstevel@tonic-gate strseteof(SOTOV(so), 1); 27710Sstevel@tonic-gate /* 27720Sstevel@tonic-gate * strseteof takes care of read side wakeups, 27730Sstevel@tonic-gate * pollwakeups, and signals. 27740Sstevel@tonic-gate */ 27750Sstevel@tonic-gate /* 27760Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems 27770Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 27780Sstevel@tonic-gate */ 27790Sstevel@tonic-gate mutex_enter(&so->so_lock); 27800Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 27810Sstevel@tonic-gate mutex_exit(&so->so_lock); 27820Sstevel@tonic-gate 27830Sstevel@tonic-gate /* Flush read side queue */ 27840Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL); 27850Sstevel@tonic-gate 27860Sstevel@tonic-gate mutex_enter(&so->so_lock); 27870Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 27880Sstevel@tonic-gate break; 27890Sstevel@tonic-gate } 27900Sstevel@tonic-gate 27910Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 27920Sstevel@tonic-gate 27930Sstevel@tonic-gate /* 27940Sstevel@tonic-gate * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them 27950Sstevel@tonic-gate * was set due to this call and the new state has both of them set: 27960Sstevel@tonic-gate * Send the AF_UNIX close indication 27970Sstevel@tonic-gate * For T_COTS send a discon_ind 27980Sstevel@tonic-gate * 27990Sstevel@tonic-gate * If cantsend was set due to this call: 28000Sstevel@tonic-gate * For T_COTSORD send an ordrel_ind 28010Sstevel@tonic-gate * 28020Sstevel@tonic-gate * Note that for T_CLTS there is no message sent here. 28030Sstevel@tonic-gate */ 28040Sstevel@tonic-gate if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) == 28050Sstevel@tonic-gate (SS_CANTRCVMORE|SS_CANTSENDMORE)) { 28060Sstevel@tonic-gate /* 28070Sstevel@tonic-gate * For SunOS 4.X compatibility we tell the other end 28080Sstevel@tonic-gate * that we are unable to receive at this point. 28090Sstevel@tonic-gate */ 28108348SEric.Yu@Sun.COM if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS) 28110Sstevel@tonic-gate so_unix_close(so); 28120Sstevel@tonic-gate 28138348SEric.Yu@Sun.COM if (sti->sti_serv_type == T_COTS) 28140Sstevel@tonic-gate error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD); 28150Sstevel@tonic-gate } 28160Sstevel@tonic-gate if ((state_change & SS_CANTSENDMORE) && 28178348SEric.Yu@Sun.COM (sti->sti_serv_type == T_COTS_ORD)) { 28180Sstevel@tonic-gate /* Send an orderly release */ 28190Sstevel@tonic-gate ordrel_req.PRIM_type = T_ORDREL_REQ; 28200Sstevel@tonic-gate 28210Sstevel@tonic-gate mutex_exit(&so->so_lock); 28220Sstevel@tonic-gate mp = soallocproto1(&ordrel_req, sizeof (ordrel_req), 28230Sstevel@tonic-gate 0, _ALLOC_SLEEP); 28240Sstevel@tonic-gate /* 28250Sstevel@tonic-gate * Send down the T_ORDREL_REQ even if there is flow control. 28260Sstevel@tonic-gate * This prevents shutdown from blocking. 28270Sstevel@tonic-gate * Note that there is no T_OK_ACK for ordrel_req. 28280Sstevel@tonic-gate */ 28290Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 28305240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 28310Sstevel@tonic-gate mutex_enter(&so->so_lock); 28320Sstevel@tonic-gate if (error) { 28330Sstevel@tonic-gate eprintsoline(so, error); 28340Sstevel@tonic-gate goto done; 28350Sstevel@tonic-gate } 28360Sstevel@tonic-gate } 28370Sstevel@tonic-gate 28380Sstevel@tonic-gate done: 28390Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 28400Sstevel@tonic-gate mutex_exit(&so->so_lock); 28410Sstevel@tonic-gate return (error); 28420Sstevel@tonic-gate } 28430Sstevel@tonic-gate 28440Sstevel@tonic-gate /* 28450Sstevel@tonic-gate * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send 28460Sstevel@tonic-gate * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer 28470Sstevel@tonic-gate * that we have closed. 28480Sstevel@tonic-gate * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length 28490Sstevel@tonic-gate * T_UNITDATA_REQ containing the same option. 28500Sstevel@tonic-gate * 28510Sstevel@tonic-gate * For SOCK_DGRAM half-connections (somebody connected to this end 28520Sstevel@tonic-gate * but this end is not connect) we don't know where to send any 28530Sstevel@tonic-gate * SO_UNIX_CLOSE. 28540Sstevel@tonic-gate * 28550Sstevel@tonic-gate * We have to ignore stream head errors just in case there has been 28560Sstevel@tonic-gate * a shutdown(output). 28570Sstevel@tonic-gate * Ignore any flow control to try to get the message more quickly to the peer. 28580Sstevel@tonic-gate * While locally ignoring flow control solves the problem when there 28590Sstevel@tonic-gate * is only the loopback transport on the stream it would not provide 28600Sstevel@tonic-gate * the correct AF_UNIX socket semantics when one or more modules have 28610Sstevel@tonic-gate * been pushed. 28620Sstevel@tonic-gate */ 28630Sstevel@tonic-gate void 28640Sstevel@tonic-gate so_unix_close(struct sonode *so) 28650Sstevel@tonic-gate { 28660Sstevel@tonic-gate int error; 28670Sstevel@tonic-gate struct T_opthdr toh; 28680Sstevel@tonic-gate mblk_t *mp; 28698348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 28700Sstevel@tonic-gate 28710Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 28720Sstevel@tonic-gate 28730Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 28740Sstevel@tonic-gate 28750Sstevel@tonic-gate if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) != 28760Sstevel@tonic-gate (SS_ISCONNECTED|SS_ISBOUND)) 28770Sstevel@tonic-gate return; 28780Sstevel@tonic-gate 28790Sstevel@tonic-gate dprintso(so, 1, ("so_unix_close(%p) %s\n", 28807240Srh87107 (void *)so, pr_state(so->so_state, so->so_mode))); 28810Sstevel@tonic-gate 28820Sstevel@tonic-gate toh.level = SOL_SOCKET; 28830Sstevel@tonic-gate toh.name = SO_UNIX_CLOSE; 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate /* zero length + header */ 28860Sstevel@tonic-gate toh.len = (t_uscalar_t)sizeof (struct T_opthdr); 28870Sstevel@tonic-gate toh.status = 0; 28880Sstevel@tonic-gate 28890Sstevel@tonic-gate if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) { 28900Sstevel@tonic-gate struct T_optdata_req tdr; 28910Sstevel@tonic-gate 28920Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ; 28930Sstevel@tonic-gate tdr.DATA_flag = 0; 28940Sstevel@tonic-gate 28950Sstevel@tonic-gate tdr.OPT_length = (t_scalar_t)sizeof (toh); 28960Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 28970Sstevel@tonic-gate 28980Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 28990Sstevel@tonic-gate mp = soallocproto2(&tdr, sizeof (tdr), 29000Sstevel@tonic-gate &toh, sizeof (toh), 0, _ALLOC_SLEEP); 29010Sstevel@tonic-gate } else { 29020Sstevel@tonic-gate struct T_unitdata_req tudr; 29030Sstevel@tonic-gate void *addr; 29040Sstevel@tonic-gate socklen_t addrlen; 29050Sstevel@tonic-gate void *src; 29060Sstevel@tonic-gate socklen_t srclen; 29070Sstevel@tonic-gate struct T_opthdr toh2; 29080Sstevel@tonic-gate t_scalar_t size; 29090Sstevel@tonic-gate 29100Sstevel@tonic-gate /* Connecteded DGRAM socket */ 29110Sstevel@tonic-gate 29120Sstevel@tonic-gate /* 29130Sstevel@tonic-gate * For AF_UNIX the destination address is translated to 29140Sstevel@tonic-gate * an internal name and the source address is passed as 29150Sstevel@tonic-gate * an option. 29160Sstevel@tonic-gate */ 29170Sstevel@tonic-gate /* 29180Sstevel@tonic-gate * Length and family checks. 29190Sstevel@tonic-gate */ 29208348SEric.Yu@Sun.COM error = so_addr_verify(so, sti->sti_faddr_sa, 29218348SEric.Yu@Sun.COM (t_uscalar_t)sti->sti_faddr_len); 29220Sstevel@tonic-gate if (error) { 29230Sstevel@tonic-gate eprintsoline(so, error); 29240Sstevel@tonic-gate return; 29250Sstevel@tonic-gate } 29268348SEric.Yu@Sun.COM if (sti->sti_faddr_noxlate) { 29270Sstevel@tonic-gate /* 29280Sstevel@tonic-gate * Already have a transport internal address. Do not 29290Sstevel@tonic-gate * pass any (transport internal) source address. 29300Sstevel@tonic-gate */ 29318348SEric.Yu@Sun.COM addr = sti->sti_faddr_sa; 29328348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sti->sti_faddr_len; 29330Sstevel@tonic-gate src = NULL; 29340Sstevel@tonic-gate srclen = 0; 29350Sstevel@tonic-gate } else { 29360Sstevel@tonic-gate /* 29370Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 29380Sstevel@tonic-gate * and translate the remote address. 29398348SEric.Yu@Sun.COM * Holding so_lock thus sti_laddr_sa can not change. 29400Sstevel@tonic-gate */ 29418348SEric.Yu@Sun.COM src = sti->sti_laddr_sa; 29428348SEric.Yu@Sun.COM srclen = (socklen_t)sti->sti_laddr_len; 29430Sstevel@tonic-gate dprintso(so, 1, 29445240Snordmark ("so_ux_close: srclen %d, src %p\n", 29455240Snordmark srclen, src)); 29460Sstevel@tonic-gate error = so_ux_addr_xlate(so, 29478348SEric.Yu@Sun.COM sti->sti_faddr_sa, 29488348SEric.Yu@Sun.COM (socklen_t)sti->sti_faddr_len, 0, 29495240Snordmark &addr, &addrlen); 29500Sstevel@tonic-gate if (error) { 29510Sstevel@tonic-gate eprintsoline(so, error); 29520Sstevel@tonic-gate return; 29530Sstevel@tonic-gate } 29540Sstevel@tonic-gate } 29550Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 29560Sstevel@tonic-gate tudr.DEST_length = addrlen; 29570Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 29580Sstevel@tonic-gate if (srclen == 0) { 29590Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)sizeof (toh); 29600Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 29615240Snordmark _TPI_ALIGN_TOPT(addrlen)); 29620Sstevel@tonic-gate 29630Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 29640Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 29650Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 29660Sstevel@tonic-gate addr, addrlen, size, _ALLOC_SLEEP); 29670Sstevel@tonic-gate mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen); 29680Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 29690Sstevel@tonic-gate } else { 29700Sstevel@tonic-gate /* 29710Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a 29720Sstevel@tonic-gate * source address option. 29730Sstevel@tonic-gate */ 29740Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) + 29750Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen)); 29760Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 29770Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen)); 29780Sstevel@tonic-gate 29790Sstevel@tonic-gate toh2.level = SOL_SOCKET; 29800Sstevel@tonic-gate toh2.name = SO_SRCADDR; 29810Sstevel@tonic-gate toh2.len = (t_uscalar_t)(srclen + 29825240Snordmark sizeof (struct T_opthdr)); 29830Sstevel@tonic-gate toh2.status = 0; 29840Sstevel@tonic-gate 29850Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 29860Sstevel@tonic-gate 29870Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 29880Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 29890Sstevel@tonic-gate addr, addrlen, size, _ALLOC_SLEEP); 29900Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 29910Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 29920Sstevel@tonic-gate soappendmsg(mp, &toh2, sizeof (toh2)); 29930Sstevel@tonic-gate soappendmsg(mp, src, srclen); 29940Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 29950Sstevel@tonic-gate } 29960Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 29970Sstevel@tonic-gate } 29980Sstevel@tonic-gate mutex_exit(&so->so_lock); 29990Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 30005240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 30010Sstevel@tonic-gate mutex_enter(&so->so_lock); 30020Sstevel@tonic-gate } 30030Sstevel@tonic-gate 30040Sstevel@tonic-gate /* 30050Sstevel@tonic-gate * Called by sotpi_recvmsg when reading a non-zero amount of data. 30060Sstevel@tonic-gate * In addition, the caller typically verifies that there is some 30070Sstevel@tonic-gate * potential state to clear by checking 30080Sstevel@tonic-gate * if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) 30090Sstevel@tonic-gate * before calling this routine. 30100Sstevel@tonic-gate * Note that such a check can be made without holding so_lock since 30110Sstevel@tonic-gate * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg 30128348SEric.Yu@Sun.COM * decrements sti_oobsigcnt. 30130Sstevel@tonic-gate * 30140Sstevel@tonic-gate * When data is read *after* the point that all pending 30150Sstevel@tonic-gate * oob data has been consumed the oob indication is cleared. 30160Sstevel@tonic-gate * 30170Sstevel@tonic-gate * This logic keeps select/poll returning POLLRDBAND and 30180Sstevel@tonic-gate * SIOCATMARK returning true until we have read past 30190Sstevel@tonic-gate * the mark. 30200Sstevel@tonic-gate */ 30210Sstevel@tonic-gate static void 30220Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so) 30230Sstevel@tonic-gate { 30248348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 30258348SEric.Yu@Sun.COM 30260Sstevel@tonic-gate mutex_enter(&so->so_lock); 30270Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 30280Sstevel@tonic-gate dprintso(so, 1, 30295240Snordmark ("sorecv_update_oobstate: counts %d/%d state %s\n", 30308348SEric.Yu@Sun.COM sti->sti_oobsigcnt, 30318348SEric.Yu@Sun.COM sti->sti_oobcnt, pr_state(so->so_state, so->so_mode))); 30328348SEric.Yu@Sun.COM if (sti->sti_oobsigcnt == 0) { 30330Sstevel@tonic-gate /* No more pending oob indications */ 30340Sstevel@tonic-gate so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK); 30350Sstevel@tonic-gate freemsg(so->so_oobmsg); 30360Sstevel@tonic-gate so->so_oobmsg = NULL; 30370Sstevel@tonic-gate } 30380Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 30390Sstevel@tonic-gate mutex_exit(&so->so_lock); 30400Sstevel@tonic-gate } 30410Sstevel@tonic-gate 30420Sstevel@tonic-gate /* 30430Sstevel@tonic-gate * Handle recv* calls for an so which has NL7C saved recv mblk_t(s). 30440Sstevel@tonic-gate */ 30450Sstevel@tonic-gate static int 30460Sstevel@tonic-gate nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp) 30470Sstevel@tonic-gate { 30488348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 30490Sstevel@tonic-gate int error = 0; 30500Sstevel@tonic-gate mblk_t *tmp = NULL; 30510Sstevel@tonic-gate mblk_t *pmp = NULL; 30528348SEric.Yu@Sun.COM mblk_t *nmp = sti->sti_nl7c_rcv_mp; 30530Sstevel@tonic-gate 30540Sstevel@tonic-gate ASSERT(nmp != NULL); 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate while (nmp != NULL && uiop->uio_resid > 0) { 30570Sstevel@tonic-gate ssize_t n; 30580Sstevel@tonic-gate 30590Sstevel@tonic-gate if (DB_TYPE(nmp) == M_DATA) { 30600Sstevel@tonic-gate /* 30610Sstevel@tonic-gate * We have some data, uiomove up to resid bytes. 30620Sstevel@tonic-gate */ 30630Sstevel@tonic-gate n = MIN(MBLKL(nmp), uiop->uio_resid); 30640Sstevel@tonic-gate if (n > 0) 30650Sstevel@tonic-gate error = uiomove(nmp->b_rptr, n, UIO_READ, uiop); 30660Sstevel@tonic-gate nmp->b_rptr += n; 30670Sstevel@tonic-gate if (nmp->b_rptr == nmp->b_wptr) { 30680Sstevel@tonic-gate pmp = nmp; 30690Sstevel@tonic-gate nmp = nmp->b_cont; 30700Sstevel@tonic-gate } 30711974Sbrutus if (error) 30721974Sbrutus break; 30730Sstevel@tonic-gate } else { 30740Sstevel@tonic-gate /* 30750Sstevel@tonic-gate * We only handle data, save for caller to handle. 30760Sstevel@tonic-gate */ 30770Sstevel@tonic-gate if (pmp != NULL) { 30780Sstevel@tonic-gate pmp->b_cont = nmp->b_cont; 30790Sstevel@tonic-gate } 30800Sstevel@tonic-gate nmp->b_cont = NULL; 30810Sstevel@tonic-gate if (*rmp == NULL) { 30820Sstevel@tonic-gate *rmp = nmp; 30830Sstevel@tonic-gate } else { 30841974Sbrutus tmp->b_cont = nmp; 30850Sstevel@tonic-gate } 30860Sstevel@tonic-gate nmp = nmp->b_cont; 30870Sstevel@tonic-gate tmp = nmp; 30880Sstevel@tonic-gate } 30890Sstevel@tonic-gate } 30900Sstevel@tonic-gate if (pmp != NULL) { 30910Sstevel@tonic-gate /* Free any mblk_t(s) which we have consumed */ 30920Sstevel@tonic-gate pmp->b_cont = NULL; 30938348SEric.Yu@Sun.COM freemsg(sti->sti_nl7c_rcv_mp); 30948348SEric.Yu@Sun.COM } 30958348SEric.Yu@Sun.COM if ((sti->sti_nl7c_rcv_mp = nmp) == NULL) { 30961974Sbrutus /* Last mblk_t so return the saved kstrgetmsg() rval/error */ 30971974Sbrutus if (error == 0) { 30988348SEric.Yu@Sun.COM rval_t *p = (rval_t *)&sti->sti_nl7c_rcv_rval; 30991974Sbrutus 31001974Sbrutus error = p->r_v.r_v2; 31011974Sbrutus p->r_v.r_v2 = 0; 31021974Sbrutus } 31038348SEric.Yu@Sun.COM rp->r_vals = sti->sti_nl7c_rcv_rval; 31048348SEric.Yu@Sun.COM sti->sti_nl7c_rcv_rval = 0; 31050Sstevel@tonic-gate } else { 31060Sstevel@tonic-gate /* More mblk_t(s) to process so no rval to return */ 31070Sstevel@tonic-gate rp->r_vals = 0; 31080Sstevel@tonic-gate } 31090Sstevel@tonic-gate return (error); 31100Sstevel@tonic-gate } 31110Sstevel@tonic-gate /* 31120Sstevel@tonic-gate * Receive the next message on the queue. 31130Sstevel@tonic-gate * If msg_controllen is non-zero when called the caller is interested in 31140Sstevel@tonic-gate * any received control info (options). 31150Sstevel@tonic-gate * If msg_namelen is non-zero when called the caller is interested in 31160Sstevel@tonic-gate * any received source address. 31170Sstevel@tonic-gate * The routine returns with msg_control and msg_name pointing to 31180Sstevel@tonic-gate * kmem_alloc'ed memory which the caller has to free. 31190Sstevel@tonic-gate */ 31208348SEric.Yu@Sun.COM /* ARGSUSED */ 31210Sstevel@tonic-gate int 31228348SEric.Yu@Sun.COM sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, 31238348SEric.Yu@Sun.COM struct cred *cr) 31240Sstevel@tonic-gate { 31250Sstevel@tonic-gate union T_primitives *tpr; 31260Sstevel@tonic-gate mblk_t *mp; 31270Sstevel@tonic-gate uchar_t pri; 31280Sstevel@tonic-gate int pflag, opflag; 31290Sstevel@tonic-gate void *control; 31300Sstevel@tonic-gate t_uscalar_t controllen; 31310Sstevel@tonic-gate t_uscalar_t namelen; 31320Sstevel@tonic-gate int so_state = so->so_state; /* Snapshot */ 31330Sstevel@tonic-gate ssize_t saved_resid; 31340Sstevel@tonic-gate rval_t rval; 31350Sstevel@tonic-gate int flags; 31360Sstevel@tonic-gate clock_t timout; 31376707Sbrutus int error = 0; 31388348SEric.Yu@Sun.COM int reterr = 0; 31396707Sbrutus struct uio *suiop = NULL; 31408348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 31410Sstevel@tonic-gate 31420Sstevel@tonic-gate flags = msg->msg_flags; 31430Sstevel@tonic-gate msg->msg_flags = 0; 31440Sstevel@tonic-gate 31450Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n", 31467240Srh87107 (void *)so, (void *)msg, flags, 31475240Snordmark pr_state(so->so_state, so->so_mode), so->so_error)); 31480Sstevel@tonic-gate 31498348SEric.Yu@Sun.COM if (so->so_version == SOV_STREAM) { 31508348SEric.Yu@Sun.COM so_update_attrs(so, SOACC); 31518348SEric.Yu@Sun.COM /* The imaginary "sockmod" has been popped - act as a stream */ 31528348SEric.Yu@Sun.COM return (strread(SOTOV(so), uiop, cr)); 31538348SEric.Yu@Sun.COM } 31548348SEric.Yu@Sun.COM 31550Sstevel@tonic-gate /* 31560Sstevel@tonic-gate * If we are not connected because we have never been connected 31570Sstevel@tonic-gate * we return ENOTCONN. If we have been connected (but are no longer 31580Sstevel@tonic-gate * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return 31590Sstevel@tonic-gate * the EOF. 31600Sstevel@tonic-gate * 31610Sstevel@tonic-gate * An alternative would be to post an ENOTCONN error in stream head 31620Sstevel@tonic-gate * (read+write) and clear it when we're connected. However, that error 31630Sstevel@tonic-gate * would cause incorrect poll/select behavior! 31640Sstevel@tonic-gate */ 31650Sstevel@tonic-gate if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 && 31660Sstevel@tonic-gate (so->so_mode & SM_CONNREQUIRED)) { 31670Sstevel@tonic-gate return (ENOTCONN); 31680Sstevel@tonic-gate } 31690Sstevel@tonic-gate 31700Sstevel@tonic-gate /* 31710Sstevel@tonic-gate * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but 31720Sstevel@tonic-gate * after checking that the read queue is empty) and returns zero. 31730Sstevel@tonic-gate * This implementation will sleep (in kstrgetmsg) even if uio_resid 31740Sstevel@tonic-gate * is zero. 31750Sstevel@tonic-gate */ 31760Sstevel@tonic-gate 31770Sstevel@tonic-gate if (flags & MSG_OOB) { 31780Sstevel@tonic-gate /* Check that the transport supports OOB */ 31790Sstevel@tonic-gate if (!(so->so_mode & SM_EXDATA)) 31800Sstevel@tonic-gate return (EOPNOTSUPP); 31818348SEric.Yu@Sun.COM so_update_attrs(so, SOACC); 31828348SEric.Yu@Sun.COM return (sorecvoob(so, msg, uiop, flags, 31838348SEric.Yu@Sun.COM (so->so_options & SO_OOBINLINE))); 31848348SEric.Yu@Sun.COM } 31858348SEric.Yu@Sun.COM 31868348SEric.Yu@Sun.COM so_update_attrs(so, SOACC); 31870Sstevel@tonic-gate 31880Sstevel@tonic-gate /* 31890Sstevel@tonic-gate * Set msg_controllen and msg_namelen to zero here to make it 31900Sstevel@tonic-gate * simpler in the cases that no control or name is returned. 31910Sstevel@tonic-gate */ 31920Sstevel@tonic-gate controllen = msg->msg_controllen; 31930Sstevel@tonic-gate namelen = msg->msg_namelen; 31940Sstevel@tonic-gate msg->msg_controllen = 0; 31950Sstevel@tonic-gate msg->msg_namelen = 0; 31960Sstevel@tonic-gate 31970Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n", 31985240Snordmark namelen, controllen)); 31990Sstevel@tonic-gate 32001974Sbrutus mutex_enter(&so->so_lock); 32010Sstevel@tonic-gate /* 32020Sstevel@tonic-gate * If an NL7C enabled socket and not waiting for write data. 32030Sstevel@tonic-gate */ 32048348SEric.Yu@Sun.COM if ((sti->sti_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) == 32050Sstevel@tonic-gate NL7C_ENABLED) { 32068348SEric.Yu@Sun.COM if (sti->sti_nl7c_uri) { 32071974Sbrutus /* Close uri processing for a previous request */ 32080Sstevel@tonic-gate nl7c_close(so); 32090Sstevel@tonic-gate } 32108348SEric.Yu@Sun.COM if ((so_state & SS_CANTRCVMORE) && 32118348SEric.Yu@Sun.COM sti->sti_nl7c_rcv_mp == NULL) { 32121974Sbrutus /* Nothing to process, EOF */ 32131974Sbrutus mutex_exit(&so->so_lock); 32141974Sbrutus return (0); 32158348SEric.Yu@Sun.COM } else if (sti->sti_nl7c_flags & NL7C_SOPERSIST) { 32161974Sbrutus /* Persistent NL7C socket, try to process request */ 32171974Sbrutus boolean_t ret; 32181974Sbrutus 32191974Sbrutus ret = nl7c_process(so, 32201974Sbrutus (so->so_state & (SS_NONBLOCK|SS_NDELAY))); 32218348SEric.Yu@Sun.COM rval.r_vals = sti->sti_nl7c_rcv_rval; 32221974Sbrutus error = rval.r_v.r_v2; 32231974Sbrutus if (error) { 32241974Sbrutus /* Error of some sort, return it */ 32251974Sbrutus mutex_exit(&so->so_lock); 32261974Sbrutus return (error); 32271974Sbrutus } 32288348SEric.Yu@Sun.COM if (sti->sti_nl7c_flags && 32298348SEric.Yu@Sun.COM ! (sti->sti_nl7c_flags & NL7C_WAITWRITE)) { 32301974Sbrutus /* 32311974Sbrutus * Still an NL7C socket and no data 32321974Sbrutus * to pass up to the caller. 32331974Sbrutus */ 32341974Sbrutus mutex_exit(&so->so_lock); 32351974Sbrutus if (ret) { 32361974Sbrutus /* EOF */ 32371974Sbrutus return (0); 32381974Sbrutus } else { 32391974Sbrutus /* Need more data */ 32401974Sbrutus return (EAGAIN); 32411974Sbrutus } 32421974Sbrutus } 32431974Sbrutus } else { 32440Sstevel@tonic-gate /* 32451974Sbrutus * Not persistent so no further NL7C processing. 32460Sstevel@tonic-gate */ 32478348SEric.Yu@Sun.COM sti->sti_nl7c_flags = 0; 32480Sstevel@tonic-gate } 32490Sstevel@tonic-gate } 32500Sstevel@tonic-gate /* 32510Sstevel@tonic-gate * Only one reader is allowed at any given time. This is needed 32520Sstevel@tonic-gate * for T_EXDATA handling and, in the future, MSG_WAITALL. 32530Sstevel@tonic-gate * 32540Sstevel@tonic-gate * This is slightly different that BSD behavior in that it fails with 32550Sstevel@tonic-gate * EWOULDBLOCK when using nonblocking io. In BSD the read queue access 32560Sstevel@tonic-gate * is single-threaded using sblock(), which is dropped while waiting 32570Sstevel@tonic-gate * for data to appear. The difference shows up e.g. if one 32580Sstevel@tonic-gate * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor 32590Sstevel@tonic-gate * does use nonblocking io and different threads are reading each 32600Sstevel@tonic-gate * file descriptor. In BSD there would never be an EWOULDBLOCK error 32610Sstevel@tonic-gate * in this case as long as the read queue doesn't get empty. 32620Sstevel@tonic-gate * In this implementation the thread using nonblocking io can 32630Sstevel@tonic-gate * get an EWOULDBLOCK error due to the blocking thread executing 32640Sstevel@tonic-gate * e.g. in the uiomove in kstrgetmsg. 32650Sstevel@tonic-gate * This difference is not believed to be significant. 32660Sstevel@tonic-gate */ 32673749Sethindra /* Set SOREADLOCKED */ 32683749Sethindra error = so_lock_read_intr(so, 32693749Sethindra uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0)); 32700Sstevel@tonic-gate mutex_exit(&so->so_lock); 32710Sstevel@tonic-gate if (error) 32720Sstevel@tonic-gate return (error); 32730Sstevel@tonic-gate 32740Sstevel@tonic-gate /* 32750Sstevel@tonic-gate * Tell kstrgetmsg to not inspect the stream head errors until all 32760Sstevel@tonic-gate * queued data has been consumed. 32770Sstevel@tonic-gate * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set. 32780Sstevel@tonic-gate * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block. 32790Sstevel@tonic-gate * 32800Sstevel@tonic-gate * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and 32810Sstevel@tonic-gate * to T_OPTDATA_IND that do not contain any user-visible control msg. 32820Sstevel@tonic-gate * Note that MSG_WAITALL set with MSG_PEEK is a noop. 32830Sstevel@tonic-gate */ 32840Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR; 32850Sstevel@tonic-gate if (flags & MSG_PEEK) { 32860Sstevel@tonic-gate pflag |= MSG_IPEEK; 32870Sstevel@tonic-gate flags &= ~MSG_WAITALL; 32880Sstevel@tonic-gate } 32890Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC) 32900Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL; 32910Sstevel@tonic-gate 32920Sstevel@tonic-gate if (flags & MSG_DONTWAIT) 32930Sstevel@tonic-gate timout = 0; 32940Sstevel@tonic-gate else 32950Sstevel@tonic-gate timout = -1; 32960Sstevel@tonic-gate opflag = pflag; 32978348SEric.Yu@Sun.COM 32988348SEric.Yu@Sun.COM suiop = sod_rcv_init(so, flags, &uiop); 32990Sstevel@tonic-gate retry: 33000Sstevel@tonic-gate saved_resid = uiop->uio_resid; 33010Sstevel@tonic-gate pri = 0; 33020Sstevel@tonic-gate mp = NULL; 33038348SEric.Yu@Sun.COM if (sti->sti_nl7c_rcv_mp != NULL) { 33041974Sbrutus /* Already kstrgetmsg()ed saved mblk(s) from NL7C */ 33050Sstevel@tonic-gate error = nl7c_sorecv(so, &mp, uiop, &rval); 33060Sstevel@tonic-gate } else { 33070Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag, 33080Sstevel@tonic-gate timout, &rval); 33090Sstevel@tonic-gate } 33108348SEric.Yu@Sun.COM if (error != 0) { 33118348SEric.Yu@Sun.COM /* kstrgetmsg returns ETIME when timeout expires */ 33128348SEric.Yu@Sun.COM if (error == ETIME) 33138348SEric.Yu@Sun.COM error = EWOULDBLOCK; 33146707Sbrutus goto out; 33150Sstevel@tonic-gate } 33160Sstevel@tonic-gate /* 33170Sstevel@tonic-gate * For datagrams the MOREDATA flag is used to set MSG_TRUNC. 33180Sstevel@tonic-gate * For non-datagrams MOREDATA is used to set MSG_EOR. 33190Sstevel@tonic-gate */ 33200Sstevel@tonic-gate ASSERT(!(rval.r_val1 & MORECTL)); 33210Sstevel@tonic-gate if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC)) 33220Sstevel@tonic-gate msg->msg_flags |= MSG_TRUNC; 33230Sstevel@tonic-gate 33240Sstevel@tonic-gate if (mp == NULL) { 33250Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n")); 33260Sstevel@tonic-gate /* 33270Sstevel@tonic-gate * 4.3BSD and 4.4BSD clears the mark when peeking across it. 33280Sstevel@tonic-gate * The draft Posix socket spec states that the mark should 33290Sstevel@tonic-gate * not be cleared when peeking. We follow the latter. 33300Sstevel@tonic-gate */ 33310Sstevel@tonic-gate if ((so->so_state & 33320Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 33330Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 33340Sstevel@tonic-gate !(flags & MSG_PEEK)) { 33350Sstevel@tonic-gate sorecv_update_oobstate(so); 33360Sstevel@tonic-gate } 33370Sstevel@tonic-gate 33380Sstevel@tonic-gate mutex_enter(&so->so_lock); 33390Sstevel@tonic-gate /* Set MSG_EOR based on MOREDATA */ 33400Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) { 33410Sstevel@tonic-gate if (so->so_state & SS_SAVEDEOR) { 33420Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 33430Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 33440Sstevel@tonic-gate } 33450Sstevel@tonic-gate } 33460Sstevel@tonic-gate /* 33470Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 33480Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 33490Sstevel@tonic-gate */ 33500Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 33510Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 33520Sstevel@tonic-gate mutex_exit(&so->so_lock); 33530Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 33540Sstevel@tonic-gate goto retry; 33550Sstevel@tonic-gate } 33566707Sbrutus goto out_locked; 33570Sstevel@tonic-gate } 33580Sstevel@tonic-gate 33590Sstevel@tonic-gate /* strsock_proto has already verified length and alignment */ 33600Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr; 33610Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type)); 33620Sstevel@tonic-gate 33630Sstevel@tonic-gate switch (tpr->type) { 33640Sstevel@tonic-gate case T_DATA_IND: { 33650Sstevel@tonic-gate if ((so->so_state & 33660Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 33670Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 33680Sstevel@tonic-gate !(flags & MSG_PEEK)) { 33690Sstevel@tonic-gate sorecv_update_oobstate(so); 33700Sstevel@tonic-gate } 33710Sstevel@tonic-gate 33720Sstevel@tonic-gate /* 33730Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on 33740Sstevel@tonic-gate * MORE_flag and MOREDATA. 33750Sstevel@tonic-gate */ 33760Sstevel@tonic-gate mutex_enter(&so->so_lock); 33770Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 33780Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) { 33790Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) 33800Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 33810Sstevel@tonic-gate else 33820Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR; 33830Sstevel@tonic-gate } 33840Sstevel@tonic-gate freemsg(mp); 33850Sstevel@tonic-gate /* 33860Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 33870Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 33880Sstevel@tonic-gate */ 33890Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 33900Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 33910Sstevel@tonic-gate mutex_exit(&so->so_lock); 33920Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 33930Sstevel@tonic-gate goto retry; 33940Sstevel@tonic-gate } 33956707Sbrutus goto out_locked; 33960Sstevel@tonic-gate } 33970Sstevel@tonic-gate case T_UNITDATA_IND: { 33980Sstevel@tonic-gate void *addr; 33990Sstevel@tonic-gate t_uscalar_t addrlen; 34000Sstevel@tonic-gate void *abuf; 34010Sstevel@tonic-gate t_uscalar_t optlen; 34020Sstevel@tonic-gate void *opt; 34030Sstevel@tonic-gate 34040Sstevel@tonic-gate if ((so->so_state & 34050Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 34060Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 34070Sstevel@tonic-gate !(flags & MSG_PEEK)) { 34080Sstevel@tonic-gate sorecv_update_oobstate(so); 34090Sstevel@tonic-gate } 34100Sstevel@tonic-gate 34110Sstevel@tonic-gate if (namelen != 0) { 34120Sstevel@tonic-gate /* Caller wants source address */ 34130Sstevel@tonic-gate addrlen = tpr->unitdata_ind.SRC_length; 34140Sstevel@tonic-gate addr = sogetoff(mp, 34155240Snordmark tpr->unitdata_ind.SRC_offset, 34165240Snordmark addrlen, 1); 34170Sstevel@tonic-gate if (addr == NULL) { 34180Sstevel@tonic-gate freemsg(mp); 34190Sstevel@tonic-gate error = EPROTO; 34200Sstevel@tonic-gate eprintsoline(so, error); 34216707Sbrutus goto out; 34220Sstevel@tonic-gate } 34230Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 34240Sstevel@tonic-gate /* 34250Sstevel@tonic-gate * Can not use the transport level address. 34260Sstevel@tonic-gate * If there is a SO_SRCADDR option carrying 34270Sstevel@tonic-gate * the socket level address it will be 34280Sstevel@tonic-gate * extracted below. 34290Sstevel@tonic-gate */ 34300Sstevel@tonic-gate addr = NULL; 34310Sstevel@tonic-gate addrlen = 0; 34320Sstevel@tonic-gate } 34330Sstevel@tonic-gate } 34340Sstevel@tonic-gate optlen = tpr->unitdata_ind.OPT_length; 34350Sstevel@tonic-gate if (optlen != 0) { 34360Sstevel@tonic-gate t_uscalar_t ncontrollen; 34370Sstevel@tonic-gate 34380Sstevel@tonic-gate /* 34390Sstevel@tonic-gate * Extract any source address option. 34400Sstevel@tonic-gate * Determine how large cmsg buffer is needed. 34410Sstevel@tonic-gate */ 34420Sstevel@tonic-gate opt = sogetoff(mp, 34435240Snordmark tpr->unitdata_ind.OPT_offset, 34445240Snordmark optlen, __TPI_ALIGN_SIZE); 34450Sstevel@tonic-gate 34460Sstevel@tonic-gate if (opt == NULL) { 34470Sstevel@tonic-gate freemsg(mp); 34480Sstevel@tonic-gate error = EPROTO; 34490Sstevel@tonic-gate eprintsoline(so, error); 34506707Sbrutus goto out; 34510Sstevel@tonic-gate } 34520Sstevel@tonic-gate if (so->so_family == AF_UNIX) 34530Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &addr, &addrlen); 34540Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen, 34555240Snordmark !(flags & MSG_XPG4_2)); 34560Sstevel@tonic-gate if (controllen != 0) 34570Sstevel@tonic-gate controllen = ncontrollen; 34580Sstevel@tonic-gate else if (ncontrollen != 0) 34590Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC; 34600Sstevel@tonic-gate } else { 34610Sstevel@tonic-gate controllen = 0; 34620Sstevel@tonic-gate } 34630Sstevel@tonic-gate 34640Sstevel@tonic-gate if (namelen != 0) { 34650Sstevel@tonic-gate /* 34660Sstevel@tonic-gate * Return address to caller. 34670Sstevel@tonic-gate * Caller handles truncation if length 34680Sstevel@tonic-gate * exceeds msg_namelen. 34690Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by 34700Sstevel@tonic-gate * the sender's copyin_name(). 34710Sstevel@tonic-gate */ 34720Sstevel@tonic-gate abuf = kmem_alloc(addrlen, KM_SLEEP); 34730Sstevel@tonic-gate 34740Sstevel@tonic-gate bcopy(addr, abuf, addrlen); 34750Sstevel@tonic-gate msg->msg_name = abuf; 34760Sstevel@tonic-gate msg->msg_namelen = addrlen; 34770Sstevel@tonic-gate } 34780Sstevel@tonic-gate 34790Sstevel@tonic-gate if (controllen != 0) { 34800Sstevel@tonic-gate /* 34810Sstevel@tonic-gate * Return control msg to caller. 34820Sstevel@tonic-gate * Caller handles truncation if length 34830Sstevel@tonic-gate * exceeds msg_controllen. 34840Sstevel@tonic-gate */ 34854420Samehta control = kmem_zalloc(controllen, KM_SLEEP); 34860Sstevel@tonic-gate 34870Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen, 34885240Snordmark !(flags & MSG_XPG4_2), 34895240Snordmark control, controllen); 34900Sstevel@tonic-gate if (error) { 34910Sstevel@tonic-gate freemsg(mp); 34920Sstevel@tonic-gate if (msg->msg_namelen != 0) 34930Sstevel@tonic-gate kmem_free(msg->msg_name, 34945240Snordmark msg->msg_namelen); 34950Sstevel@tonic-gate kmem_free(control, controllen); 34960Sstevel@tonic-gate eprintsoline(so, error); 34976707Sbrutus goto out; 34980Sstevel@tonic-gate } 34990Sstevel@tonic-gate msg->msg_control = control; 35000Sstevel@tonic-gate msg->msg_controllen = controllen; 35010Sstevel@tonic-gate } 35020Sstevel@tonic-gate 35030Sstevel@tonic-gate freemsg(mp); 35046707Sbrutus goto out; 35050Sstevel@tonic-gate } 35060Sstevel@tonic-gate case T_OPTDATA_IND: { 35070Sstevel@tonic-gate struct T_optdata_req *tdr; 35080Sstevel@tonic-gate void *opt; 35090Sstevel@tonic-gate t_uscalar_t optlen; 35100Sstevel@tonic-gate 35110Sstevel@tonic-gate if ((so->so_state & 35120Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 35130Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 35140Sstevel@tonic-gate !(flags & MSG_PEEK)) { 35150Sstevel@tonic-gate sorecv_update_oobstate(so); 35160Sstevel@tonic-gate } 35170Sstevel@tonic-gate 35180Sstevel@tonic-gate tdr = (struct T_optdata_req *)mp->b_rptr; 35190Sstevel@tonic-gate optlen = tdr->OPT_length; 35200Sstevel@tonic-gate if (optlen != 0) { 35210Sstevel@tonic-gate t_uscalar_t ncontrollen; 35220Sstevel@tonic-gate /* 35230Sstevel@tonic-gate * Determine how large cmsg buffer is needed. 35240Sstevel@tonic-gate */ 35250Sstevel@tonic-gate opt = sogetoff(mp, 35265240Snordmark tpr->optdata_ind.OPT_offset, 35275240Snordmark optlen, __TPI_ALIGN_SIZE); 35280Sstevel@tonic-gate 35290Sstevel@tonic-gate if (opt == NULL) { 35300Sstevel@tonic-gate freemsg(mp); 35310Sstevel@tonic-gate error = EPROTO; 35320Sstevel@tonic-gate eprintsoline(so, error); 35336707Sbrutus goto out; 35340Sstevel@tonic-gate } 35350Sstevel@tonic-gate 35360Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen, 35375240Snordmark !(flags & MSG_XPG4_2)); 35380Sstevel@tonic-gate if (controllen != 0) 35390Sstevel@tonic-gate controllen = ncontrollen; 35400Sstevel@tonic-gate else if (ncontrollen != 0) 35410Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC; 35420Sstevel@tonic-gate } else { 35430Sstevel@tonic-gate controllen = 0; 35440Sstevel@tonic-gate } 35450Sstevel@tonic-gate 35460Sstevel@tonic-gate if (controllen != 0) { 35470Sstevel@tonic-gate /* 35480Sstevel@tonic-gate * Return control msg to caller. 35490Sstevel@tonic-gate * Caller handles truncation if length 35500Sstevel@tonic-gate * exceeds msg_controllen. 35510Sstevel@tonic-gate */ 35524420Samehta control = kmem_zalloc(controllen, KM_SLEEP); 35530Sstevel@tonic-gate 35540Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen, 35555240Snordmark !(flags & MSG_XPG4_2), 35565240Snordmark control, controllen); 35570Sstevel@tonic-gate if (error) { 35580Sstevel@tonic-gate freemsg(mp); 35590Sstevel@tonic-gate kmem_free(control, controllen); 35600Sstevel@tonic-gate eprintsoline(so, error); 35616707Sbrutus goto out; 35620Sstevel@tonic-gate } 35630Sstevel@tonic-gate msg->msg_control = control; 35640Sstevel@tonic-gate msg->msg_controllen = controllen; 35650Sstevel@tonic-gate } 35660Sstevel@tonic-gate 35670Sstevel@tonic-gate /* 35680Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on 35690Sstevel@tonic-gate * DATA_flag and MOREDATA. 35700Sstevel@tonic-gate */ 35710Sstevel@tonic-gate mutex_enter(&so->so_lock); 35720Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 35730Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) { 35740Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) 35750Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 35760Sstevel@tonic-gate else 35770Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR; 35780Sstevel@tonic-gate } 35790Sstevel@tonic-gate freemsg(mp); 35800Sstevel@tonic-gate /* 35810Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 35820Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 35830Sstevel@tonic-gate * Not possible to wait if control info was received. 35840Sstevel@tonic-gate */ 35850Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 35860Sstevel@tonic-gate controllen == 0 && 35870Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 35880Sstevel@tonic-gate mutex_exit(&so->so_lock); 35890Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 35900Sstevel@tonic-gate goto retry; 35910Sstevel@tonic-gate } 35926707Sbrutus goto out_locked; 35930Sstevel@tonic-gate } 35940Sstevel@tonic-gate case T_EXDATA_IND: { 35950Sstevel@tonic-gate dprintso(so, 1, 35965240Snordmark ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld " 35975240Snordmark "state %s\n", 35988348SEric.Yu@Sun.COM sti->sti_oobsigcnt, sti->sti_oobcnt, 35995240Snordmark saved_resid - uiop->uio_resid, 36005240Snordmark pr_state(so->so_state, so->so_mode))); 36010Sstevel@tonic-gate /* 36020Sstevel@tonic-gate * kstrgetmsg handles MSGMARK so there is nothing to 36030Sstevel@tonic-gate * inspect in the T_EXDATA_IND. 36040Sstevel@tonic-gate * strsock_proto makes the stream head queue the T_EXDATA_IND 36050Sstevel@tonic-gate * as a separate message with no M_DATA component. Furthermore, 36060Sstevel@tonic-gate * the stream head does not consolidate M_DATA messages onto 36070Sstevel@tonic-gate * an MSGMARK'ed message ensuring that the T_EXDATA_IND 36080Sstevel@tonic-gate * remains a message by itself. This is needed since MSGMARK 36090Sstevel@tonic-gate * marks both the whole message as well as the last byte 36100Sstevel@tonic-gate * of the message. 36110Sstevel@tonic-gate */ 36120Sstevel@tonic-gate freemsg(mp); 36130Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); /* No data */ 36140Sstevel@tonic-gate if (flags & MSG_PEEK) { 36150Sstevel@tonic-gate /* 36160Sstevel@tonic-gate * Even though we are peeking we consume the 36170Sstevel@tonic-gate * T_EXDATA_IND thereby moving the mark information 36180Sstevel@tonic-gate * to SS_RCVATMARK. Then the oob code below will 36190Sstevel@tonic-gate * retry the peeking kstrgetmsg. 36200Sstevel@tonic-gate * Note that the stream head read queue is 36210Sstevel@tonic-gate * never flushed without holding SOREADLOCKED 36220Sstevel@tonic-gate * thus the T_EXDATA_IND can not disappear 36230Sstevel@tonic-gate * underneath us. 36240Sstevel@tonic-gate */ 36250Sstevel@tonic-gate dprintso(so, 1, 36265240Snordmark ("sotpi_recvmsg: consume EXDATA_IND " 36275240Snordmark "counts %d/%d state %s\n", 36288348SEric.Yu@Sun.COM sti->sti_oobsigcnt, 36298348SEric.Yu@Sun.COM sti->sti_oobcnt, 36305240Snordmark pr_state(so->so_state, so->so_mode))); 36310Sstevel@tonic-gate 36320Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR; 36330Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC) 36340Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL; 36350Sstevel@tonic-gate 36360Sstevel@tonic-gate pri = 0; 36370Sstevel@tonic-gate mp = NULL; 36380Sstevel@tonic-gate 36390Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop, 36405240Snordmark &pri, &pflag, (clock_t)-1, &rval); 36410Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); 36420Sstevel@tonic-gate 36430Sstevel@tonic-gate if (error) { 36440Sstevel@tonic-gate #ifdef SOCK_DEBUG 36450Sstevel@tonic-gate if (error != EWOULDBLOCK && error != EINTR) { 36460Sstevel@tonic-gate eprintsoline(so, error); 36470Sstevel@tonic-gate } 36480Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 36496707Sbrutus goto out; 36500Sstevel@tonic-gate } 36510Sstevel@tonic-gate ASSERT(mp); 36520Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr; 36530Sstevel@tonic-gate ASSERT(tpr->type == T_EXDATA_IND); 36540Sstevel@tonic-gate freemsg(mp); 36550Sstevel@tonic-gate } /* end "if (flags & MSG_PEEK)" */ 36560Sstevel@tonic-gate 36570Sstevel@tonic-gate /* 36580Sstevel@tonic-gate * Decrement the number of queued and pending oob. 36590Sstevel@tonic-gate * 36600Sstevel@tonic-gate * SS_RCVATMARK is cleared when we read past a mark. 36610Sstevel@tonic-gate * SS_HAVEOOBDATA is cleared when we've read past the 36620Sstevel@tonic-gate * last mark. 36630Sstevel@tonic-gate * SS_OOBPEND is cleared if we've read past the last 36640Sstevel@tonic-gate * mark and no (new) SIGURG has been posted. 36650Sstevel@tonic-gate */ 36660Sstevel@tonic-gate mutex_enter(&so->so_lock); 36670Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 36688348SEric.Yu@Sun.COM ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt); 36698348SEric.Yu@Sun.COM ASSERT(sti->sti_oobsigcnt > 0); 36708348SEric.Yu@Sun.COM sti->sti_oobsigcnt--; 36718348SEric.Yu@Sun.COM ASSERT(sti->sti_oobcnt > 0); 36728348SEric.Yu@Sun.COM sti->sti_oobcnt--; 36730Sstevel@tonic-gate /* 36740Sstevel@tonic-gate * Since the T_EXDATA_IND has been removed from the stream 36750Sstevel@tonic-gate * head, but we have not read data past the mark, 36760Sstevel@tonic-gate * sockfs needs to track that the socket is still at the mark. 36770Sstevel@tonic-gate * 36780Sstevel@tonic-gate * Since no data was received call kstrgetmsg again to wait 36790Sstevel@tonic-gate * for data. 36800Sstevel@tonic-gate */ 36810Sstevel@tonic-gate so->so_state |= SS_RCVATMARK; 36820Sstevel@tonic-gate mutex_exit(&so->so_lock); 36830Sstevel@tonic-gate dprintso(so, 1, 36840Sstevel@tonic-gate ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n", 36858348SEric.Yu@Sun.COM sti->sti_oobsigcnt, sti->sti_oobcnt, 36860Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 36870Sstevel@tonic-gate pflag = opflag; 36880Sstevel@tonic-gate goto retry; 36890Sstevel@tonic-gate } 36900Sstevel@tonic-gate default: 36918348SEric.Yu@Sun.COM cmn_err(CE_CONT, "sotpi_recvmsg: so %p prim %d mp %p\n", 36928348SEric.Yu@Sun.COM (void *)so, tpr->type, (void *)mp); 36930Sstevel@tonic-gate ASSERT(0); 36940Sstevel@tonic-gate freemsg(mp); 36950Sstevel@tonic-gate error = EPROTO; 36960Sstevel@tonic-gate eprintsoline(so, error); 36976707Sbrutus goto out; 36980Sstevel@tonic-gate } 36990Sstevel@tonic-gate /* NOTREACHED */ 37006707Sbrutus out: 37010Sstevel@tonic-gate mutex_enter(&so->so_lock); 37026707Sbrutus out_locked: 37038348SEric.Yu@Sun.COM if (so->so_direct != NULL) { 37048348SEric.Yu@Sun.COM mutex_enter(so->so_direct->sod_lockp); 37058348SEric.Yu@Sun.COM reterr = sod_rcv_done(so, suiop, uiop); 37068348SEric.Yu@Sun.COM mutex_exit(so->so_direct->sod_lockp); 37078348SEric.Yu@Sun.COM } 37088348SEric.Yu@Sun.COM if (reterr != 0 && error == 0) 37098348SEric.Yu@Sun.COM error = reterr; 37100Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 37110Sstevel@tonic-gate mutex_exit(&so->so_lock); 37120Sstevel@tonic-gate return (error); 37130Sstevel@tonic-gate } 37140Sstevel@tonic-gate 37150Sstevel@tonic-gate /* 37160Sstevel@tonic-gate * Sending data with options on a datagram socket. 37170Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set. 37180Sstevel@tonic-gate */ 37190Sstevel@tonic-gate static int 3720741Smasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3721741Smasputra struct uio *uiop, void *control, t_uscalar_t controllen, int flags) 37220Sstevel@tonic-gate { 37230Sstevel@tonic-gate struct T_unitdata_req tudr; 37240Sstevel@tonic-gate mblk_t *mp; 37250Sstevel@tonic-gate int error; 37260Sstevel@tonic-gate void *addr; 37270Sstevel@tonic-gate socklen_t addrlen; 37280Sstevel@tonic-gate void *src; 37290Sstevel@tonic-gate socklen_t srclen; 37300Sstevel@tonic-gate ssize_t len; 37310Sstevel@tonic-gate int size; 37320Sstevel@tonic-gate struct T_opthdr toh; 37330Sstevel@tonic-gate struct fdbuf *fdbuf; 37340Sstevel@tonic-gate t_uscalar_t optlen; 37350Sstevel@tonic-gate void *fds; 37360Sstevel@tonic-gate int fdlen; 37378348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 37380Sstevel@tonic-gate 37390Sstevel@tonic-gate ASSERT(name && namelen); 37400Sstevel@tonic-gate ASSERT(control && controllen); 37410Sstevel@tonic-gate 37420Sstevel@tonic-gate len = uiop->uio_resid; 37438348SEric.Yu@Sun.COM if (len > (ssize_t)sti->sti_tidu_size) { 37440Sstevel@tonic-gate return (EMSGSIZE); 37450Sstevel@tonic-gate } 37460Sstevel@tonic-gate 37470Sstevel@tonic-gate /* 37480Sstevel@tonic-gate * For AF_UNIX the destination address is translated to an internal 37490Sstevel@tonic-gate * name and the source address is passed as an option. 37500Sstevel@tonic-gate * Also, file descriptors are passed as file pointers in an 37510Sstevel@tonic-gate * option. 37520Sstevel@tonic-gate */ 37530Sstevel@tonic-gate 37540Sstevel@tonic-gate /* 37550Sstevel@tonic-gate * Length and family checks. 37560Sstevel@tonic-gate */ 37570Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 37580Sstevel@tonic-gate if (error) { 37590Sstevel@tonic-gate eprintsoline(so, error); 37600Sstevel@tonic-gate return (error); 37610Sstevel@tonic-gate } 37620Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 37638348SEric.Yu@Sun.COM if (sti->sti_faddr_noxlate) { 37640Sstevel@tonic-gate /* 37650Sstevel@tonic-gate * Already have a transport internal address. Do not 37660Sstevel@tonic-gate * pass any (transport internal) source address. 37670Sstevel@tonic-gate */ 37680Sstevel@tonic-gate addr = name; 37690Sstevel@tonic-gate addrlen = namelen; 37700Sstevel@tonic-gate src = NULL; 37710Sstevel@tonic-gate srclen = 0; 37720Sstevel@tonic-gate } else { 37730Sstevel@tonic-gate /* 37740Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 37750Sstevel@tonic-gate * and translate the remote address. 37760Sstevel@tonic-gate * 37778348SEric.Yu@Sun.COM * Note that this code does not prevent sti_laddr_sa 37780Sstevel@tonic-gate * from changing while it is being used. Thus 37790Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this 37800Sstevel@tonic-gate * send the peer might see a partially new and a 37810Sstevel@tonic-gate * partially old "from" address. 37820Sstevel@tonic-gate */ 37838348SEric.Yu@Sun.COM src = sti->sti_laddr_sa; 37848348SEric.Yu@Sun.COM srclen = (t_uscalar_t)sti->sti_laddr_len; 37850Sstevel@tonic-gate dprintso(so, 1, 37860Sstevel@tonic-gate ("sosend_dgramcmsg UNIX: srclen %d, src %p\n", 37870Sstevel@tonic-gate srclen, src)); 37880Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen, 37895240Snordmark (flags & MSG_XPG4_2), 37905240Snordmark &addr, &addrlen); 37910Sstevel@tonic-gate if (error) { 37920Sstevel@tonic-gate eprintsoline(so, error); 37930Sstevel@tonic-gate return (error); 37940Sstevel@tonic-gate } 37950Sstevel@tonic-gate } 37960Sstevel@tonic-gate } else { 37970Sstevel@tonic-gate addr = name; 37980Sstevel@tonic-gate addrlen = namelen; 37990Sstevel@tonic-gate src = NULL; 38000Sstevel@tonic-gate srclen = 0; 38010Sstevel@tonic-gate } 38020Sstevel@tonic-gate optlen = so_optlen(control, controllen, 38035240Snordmark !(flags & MSG_XPG4_2)); 38040Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 38050Sstevel@tonic-gate tudr.DEST_length = addrlen; 38060Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 38070Sstevel@tonic-gate if (srclen != 0) 38080Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) + 38090Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen)); 38100Sstevel@tonic-gate else 38110Sstevel@tonic-gate tudr.OPT_length = optlen; 38120Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 38135240Snordmark _TPI_ALIGN_TOPT(addrlen)); 38140Sstevel@tonic-gate 38150Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 38160Sstevel@tonic-gate 38170Sstevel@tonic-gate /* 38180Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set. 38190Sstevel@tonic-gate */ 38200Sstevel@tonic-gate error = so_getfdopt(control, controllen, 38215240Snordmark !(flags & MSG_XPG4_2), &fds, &fdlen); 38220Sstevel@tonic-gate if (error) 38230Sstevel@tonic-gate return (error); 38240Sstevel@tonic-gate if (fdlen != -1) { 38250Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING)) 38260Sstevel@tonic-gate return (EOPNOTSUPP); 38270Sstevel@tonic-gate 38280Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf); 38290Sstevel@tonic-gate if (error) 38300Sstevel@tonic-gate return (error); 38310Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf); 38320Sstevel@tonic-gate } else { 38330Sstevel@tonic-gate mp = soallocproto(size, _ALLOC_INTR); 3834455Smeem if (mp == NULL) { 3835455Smeem /* 3836455Smeem * Caught a signal waiting for memory. 3837455Smeem * Let send* return EINTR. 3838455Smeem */ 3839455Smeem return (EINTR); 3840455Smeem } 38410Sstevel@tonic-gate } 38420Sstevel@tonic-gate soappendmsg(mp, &tudr, sizeof (tudr)); 38430Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 38440Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 38450Sstevel@tonic-gate 38460Sstevel@tonic-gate if (fdlen != -1) { 38470Sstevel@tonic-gate ASSERT(fdbuf != NULL); 38480Sstevel@tonic-gate toh.level = SOL_SOCKET; 38490Sstevel@tonic-gate toh.name = SO_FILEP; 38500Sstevel@tonic-gate toh.len = fdbuf->fd_size + 38515240Snordmark (t_uscalar_t)sizeof (struct T_opthdr); 38520Sstevel@tonic-gate toh.status = 0; 38530Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 38540Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size); 38550Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 38560Sstevel@tonic-gate } 38570Sstevel@tonic-gate if (srclen != 0) { 38580Sstevel@tonic-gate /* 38590Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 38600Sstevel@tonic-gate * address option. 38610Sstevel@tonic-gate */ 38620Sstevel@tonic-gate toh.level = SOL_SOCKET; 38630Sstevel@tonic-gate toh.name = SO_SRCADDR; 38640Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 38650Sstevel@tonic-gate toh.status = 0; 38660Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 38670Sstevel@tonic-gate soappendmsg(mp, src, srclen); 38680Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 38690Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 38700Sstevel@tonic-gate } 38710Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 38720Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 38730Sstevel@tonic-gate /* At most 3 bytes left in the message */ 38740Sstevel@tonic-gate ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE)); 38750Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size); 38760Sstevel@tonic-gate 38770Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 38780Sstevel@tonic-gate if (audit_active) 38790Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 38800Sstevel@tonic-gate 38810Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 38820Sstevel@tonic-gate #ifdef SOCK_DEBUG 38830Sstevel@tonic-gate if (error) { 38840Sstevel@tonic-gate eprintsoline(so, error); 38850Sstevel@tonic-gate } 38860Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 38870Sstevel@tonic-gate return (error); 38880Sstevel@tonic-gate } 38890Sstevel@tonic-gate 38900Sstevel@tonic-gate /* 38910Sstevel@tonic-gate * Sending data with options on a connected stream socket. 38920Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set. 38930Sstevel@tonic-gate */ 38940Sstevel@tonic-gate static int 38958348SEric.Yu@Sun.COM sosend_svccmsg(struct sonode *so, struct uio *uiop, int more, void *control, 38968348SEric.Yu@Sun.COM t_uscalar_t controllen, int flags) 38970Sstevel@tonic-gate { 38980Sstevel@tonic-gate struct T_optdata_req tdr; 38990Sstevel@tonic-gate mblk_t *mp; 39000Sstevel@tonic-gate int error; 39010Sstevel@tonic-gate ssize_t iosize; 39020Sstevel@tonic-gate int size; 39030Sstevel@tonic-gate struct fdbuf *fdbuf; 39040Sstevel@tonic-gate t_uscalar_t optlen; 39050Sstevel@tonic-gate void *fds; 39060Sstevel@tonic-gate int fdlen; 39070Sstevel@tonic-gate struct T_opthdr toh; 39088348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 39090Sstevel@tonic-gate 39100Sstevel@tonic-gate dprintso(so, 1, 39115240Snordmark ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid)); 39120Sstevel@tonic-gate 39130Sstevel@tonic-gate /* 39140Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are 39150Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it 39160Sstevel@tonic-gate * thus it is not possible to ASSERT on the state. 39170Sstevel@tonic-gate */ 39180Sstevel@tonic-gate 39190Sstevel@tonic-gate /* Options on connection-oriented only when SM_OPTDATA set. */ 39200Sstevel@tonic-gate if (!(so->so_mode & SM_OPTDATA)) 39210Sstevel@tonic-gate return (EOPNOTSUPP); 39220Sstevel@tonic-gate 39230Sstevel@tonic-gate do { 39240Sstevel@tonic-gate /* 39250Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this 39260Sstevel@tonic-gate * message or if the caller passed in "more". 39270Sstevel@tonic-gate * Error for transports with zero tidu_size. 39280Sstevel@tonic-gate */ 39290Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ; 39308348SEric.Yu@Sun.COM iosize = sti->sti_tidu_size; 39310Sstevel@tonic-gate if (iosize <= 0) 39320Sstevel@tonic-gate return (EMSGSIZE); 39330Sstevel@tonic-gate if (uiop->uio_resid > iosize) { 39340Sstevel@tonic-gate tdr.DATA_flag = 1; 39350Sstevel@tonic-gate } else { 39360Sstevel@tonic-gate if (more) 39370Sstevel@tonic-gate tdr.DATA_flag = 1; 39380Sstevel@tonic-gate else 39390Sstevel@tonic-gate tdr.DATA_flag = 0; 39400Sstevel@tonic-gate iosize = uiop->uio_resid; 39410Sstevel@tonic-gate } 39420Sstevel@tonic-gate dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n", 39435240Snordmark tdr.DATA_flag, iosize)); 39440Sstevel@tonic-gate 39450Sstevel@tonic-gate optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2)); 39460Sstevel@tonic-gate tdr.OPT_length = optlen; 39470Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 39480Sstevel@tonic-gate 39490Sstevel@tonic-gate size = (int)sizeof (tdr) + optlen; 39500Sstevel@tonic-gate /* 39510Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set. 39520Sstevel@tonic-gate */ 39530Sstevel@tonic-gate error = so_getfdopt(control, controllen, 39545240Snordmark !(flags & MSG_XPG4_2), &fds, &fdlen); 39550Sstevel@tonic-gate if (error) 39560Sstevel@tonic-gate return (error); 39570Sstevel@tonic-gate if (fdlen != -1) { 39580Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING)) 39590Sstevel@tonic-gate return (EOPNOTSUPP); 39600Sstevel@tonic-gate 39610Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf); 39620Sstevel@tonic-gate if (error) 39630Sstevel@tonic-gate return (error); 39640Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf); 39650Sstevel@tonic-gate } else { 39660Sstevel@tonic-gate mp = soallocproto(size, _ALLOC_INTR); 3967455Smeem if (mp == NULL) { 3968455Smeem /* 3969455Smeem * Caught a signal waiting for memory. 3970455Smeem * Let send* return EINTR. 3971455Smeem */ 39728348SEric.Yu@Sun.COM return (EINTR); 3973455Smeem } 39740Sstevel@tonic-gate } 39750Sstevel@tonic-gate soappendmsg(mp, &tdr, sizeof (tdr)); 39760Sstevel@tonic-gate 39770Sstevel@tonic-gate if (fdlen != -1) { 39780Sstevel@tonic-gate ASSERT(fdbuf != NULL); 39790Sstevel@tonic-gate toh.level = SOL_SOCKET; 39800Sstevel@tonic-gate toh.name = SO_FILEP; 39810Sstevel@tonic-gate toh.len = fdbuf->fd_size + 39825240Snordmark (t_uscalar_t)sizeof (struct T_opthdr); 39830Sstevel@tonic-gate toh.status = 0; 39840Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 39850Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size); 39860Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 39870Sstevel@tonic-gate } 39880Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 39890Sstevel@tonic-gate /* At most 3 bytes left in the message */ 39900Sstevel@tonic-gate ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE)); 39910Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size); 39920Sstevel@tonic-gate 39930Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 39940Sstevel@tonic-gate 39950Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 39965240Snordmark 0, MSG_BAND, 0); 39970Sstevel@tonic-gate if (error) { 39980Sstevel@tonic-gate eprintsoline(so, error); 39990Sstevel@tonic-gate return (error); 40000Sstevel@tonic-gate } 40010Sstevel@tonic-gate control = NULL; 40020Sstevel@tonic-gate if (uiop->uio_resid > 0) { 40030Sstevel@tonic-gate /* 40040Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though 40050Sstevel@tonic-gate * some data have been written. This is consistent 40060Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics. 40070Sstevel@tonic-gate */ 40080Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) { 40090Sstevel@tonic-gate eprintsoline(so, error); 40100Sstevel@tonic-gate return (EPIPE); 40110Sstevel@tonic-gate } 40120Sstevel@tonic-gate if (so->so_error != 0) { 40130Sstevel@tonic-gate mutex_enter(&so->so_lock); 40148348SEric.Yu@Sun.COM error = sogeterr(so, B_TRUE); 40150Sstevel@tonic-gate mutex_exit(&so->so_lock); 40160Sstevel@tonic-gate if (error != 0) { 40170Sstevel@tonic-gate eprintsoline(so, error); 40180Sstevel@tonic-gate return (error); 40190Sstevel@tonic-gate } 40200Sstevel@tonic-gate } 40210Sstevel@tonic-gate } 40220Sstevel@tonic-gate } while (uiop->uio_resid > 0); 40230Sstevel@tonic-gate return (0); 40240Sstevel@tonic-gate } 40250Sstevel@tonic-gate 40260Sstevel@tonic-gate /* 40270Sstevel@tonic-gate * Sending data on a datagram socket. 40280Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set. 40290Sstevel@tonic-gate * 40300Sstevel@tonic-gate * For AF_UNIX the destination address is translated to an internal 40310Sstevel@tonic-gate * name and the source address is passed as an option. 40320Sstevel@tonic-gate */ 40330Sstevel@tonic-gate int 4034741Smasputra sosend_dgram(struct sonode *so, struct sockaddr *name, socklen_t namelen, 4035741Smasputra struct uio *uiop, int flags) 40360Sstevel@tonic-gate { 40370Sstevel@tonic-gate struct T_unitdata_req tudr; 40380Sstevel@tonic-gate mblk_t *mp; 40390Sstevel@tonic-gate int error; 40400Sstevel@tonic-gate void *addr; 40410Sstevel@tonic-gate socklen_t addrlen; 40420Sstevel@tonic-gate void *src; 40430Sstevel@tonic-gate socklen_t srclen; 40440Sstevel@tonic-gate ssize_t len; 40458348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 40460Sstevel@tonic-gate 4047741Smasputra ASSERT(name != NULL && namelen != 0); 40480Sstevel@tonic-gate 40490Sstevel@tonic-gate len = uiop->uio_resid; 40508348SEric.Yu@Sun.COM if (len > sti->sti_tidu_size) { 40510Sstevel@tonic-gate error = EMSGSIZE; 40520Sstevel@tonic-gate goto done; 40530Sstevel@tonic-gate } 40540Sstevel@tonic-gate 4055741Smasputra /* Length and family checks */ 40560Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 4057741Smasputra if (error != 0) 40580Sstevel@tonic-gate goto done; 4059741Smasputra 40608348SEric.Yu@Sun.COM if (sti->sti_direct) 4061741Smasputra return (sodgram_direct(so, name, namelen, uiop, flags)); 4062741Smasputra 40630Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 40648348SEric.Yu@Sun.COM if (sti->sti_faddr_noxlate) { 40650Sstevel@tonic-gate /* 40660Sstevel@tonic-gate * Already have a transport internal address. Do not 40670Sstevel@tonic-gate * pass any (transport internal) source address. 40680Sstevel@tonic-gate */ 40690Sstevel@tonic-gate addr = name; 40700Sstevel@tonic-gate addrlen = namelen; 40710Sstevel@tonic-gate src = NULL; 40720Sstevel@tonic-gate srclen = 0; 40730Sstevel@tonic-gate } else { 40740Sstevel@tonic-gate /* 40750Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 40760Sstevel@tonic-gate * and translate the remote address. 40770Sstevel@tonic-gate * 40788348SEric.Yu@Sun.COM * Note that this code does not prevent sti_laddr_sa 40790Sstevel@tonic-gate * from changing while it is being used. Thus 40800Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this 40810Sstevel@tonic-gate * send the peer might see a partially new and a 40820Sstevel@tonic-gate * partially old "from" address. 40830Sstevel@tonic-gate */ 40848348SEric.Yu@Sun.COM src = sti->sti_laddr_sa; 40858348SEric.Yu@Sun.COM srclen = (socklen_t)sti->sti_laddr_len; 40860Sstevel@tonic-gate dprintso(so, 1, 40875240Snordmark ("sosend_dgram UNIX: srclen %d, src %p\n", 40885240Snordmark srclen, src)); 40890Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen, 40905240Snordmark (flags & MSG_XPG4_2), 40915240Snordmark &addr, &addrlen); 40920Sstevel@tonic-gate if (error) { 40930Sstevel@tonic-gate eprintsoline(so, error); 40940Sstevel@tonic-gate goto done; 40950Sstevel@tonic-gate } 40960Sstevel@tonic-gate } 40970Sstevel@tonic-gate } else { 40980Sstevel@tonic-gate addr = name; 40990Sstevel@tonic-gate addrlen = namelen; 41000Sstevel@tonic-gate src = NULL; 41010Sstevel@tonic-gate srclen = 0; 41020Sstevel@tonic-gate } 41030Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 41040Sstevel@tonic-gate tudr.DEST_length = addrlen; 41050Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 41060Sstevel@tonic-gate if (srclen == 0) { 41070Sstevel@tonic-gate tudr.OPT_length = 0; 41080Sstevel@tonic-gate tudr.OPT_offset = 0; 41090Sstevel@tonic-gate 41100Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 41110Sstevel@tonic-gate addr, addrlen, 0, _ALLOC_INTR); 41120Sstevel@tonic-gate if (mp == NULL) { 41130Sstevel@tonic-gate /* 41140Sstevel@tonic-gate * Caught a signal waiting for memory. 41150Sstevel@tonic-gate * Let send* return EINTR. 41160Sstevel@tonic-gate */ 41170Sstevel@tonic-gate error = EINTR; 41180Sstevel@tonic-gate goto done; 41190Sstevel@tonic-gate } 41200Sstevel@tonic-gate } else { 41210Sstevel@tonic-gate /* 41220Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 41230Sstevel@tonic-gate * address option. 41240Sstevel@tonic-gate */ 41250Sstevel@tonic-gate struct T_opthdr toh; 41260Sstevel@tonic-gate ssize_t size; 41270Sstevel@tonic-gate 41280Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(sizeof (toh) + 41295240Snordmark _TPI_ALIGN_TOPT(srclen)); 41300Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 41315240Snordmark _TPI_ALIGN_TOPT(addrlen)); 41320Sstevel@tonic-gate 41330Sstevel@tonic-gate toh.level = SOL_SOCKET; 41340Sstevel@tonic-gate toh.name = SO_SRCADDR; 41350Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 41360Sstevel@tonic-gate toh.status = 0; 41370Sstevel@tonic-gate 41380Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 41390Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 41400Sstevel@tonic-gate addr, addrlen, size, _ALLOC_INTR); 41410Sstevel@tonic-gate if (mp == NULL) { 41420Sstevel@tonic-gate /* 41430Sstevel@tonic-gate * Caught a signal waiting for memory. 41440Sstevel@tonic-gate * Let send* return EINTR. 41450Sstevel@tonic-gate */ 41460Sstevel@tonic-gate error = EINTR; 41470Sstevel@tonic-gate goto done; 41480Sstevel@tonic-gate } 41490Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 41500Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 41510Sstevel@tonic-gate soappendmsg(mp, src, srclen); 41520Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 41530Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 41540Sstevel@tonic-gate } 41550Sstevel@tonic-gate 41560Sstevel@tonic-gate if (audit_active) 41570Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 41580Sstevel@tonic-gate 41590Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 41600Sstevel@tonic-gate done: 41610Sstevel@tonic-gate #ifdef SOCK_DEBUG 41620Sstevel@tonic-gate if (error) { 41630Sstevel@tonic-gate eprintsoline(so, error); 41640Sstevel@tonic-gate } 41650Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 41660Sstevel@tonic-gate return (error); 41670Sstevel@tonic-gate } 41680Sstevel@tonic-gate 41690Sstevel@tonic-gate /* 41700Sstevel@tonic-gate * Sending data on a connected stream socket. 41710Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set. 41720Sstevel@tonic-gate */ 41730Sstevel@tonic-gate int 41748348SEric.Yu@Sun.COM sosend_svc(struct sonode *so, struct uio *uiop, t_scalar_t prim, int more, 41758348SEric.Yu@Sun.COM int sflag) 41760Sstevel@tonic-gate { 41770Sstevel@tonic-gate struct T_data_req tdr; 41780Sstevel@tonic-gate mblk_t *mp; 41790Sstevel@tonic-gate int error; 41800Sstevel@tonic-gate ssize_t iosize; 41818348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 41820Sstevel@tonic-gate 41830Sstevel@tonic-gate dprintso(so, 1, 41845240Snordmark ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n", 41857240Srh87107 (void *)so, uiop->uio_resid, prim, sflag)); 41860Sstevel@tonic-gate 41870Sstevel@tonic-gate /* 41880Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are 41890Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it 41900Sstevel@tonic-gate * thus it is not possible to ASSERT on the state. 41910Sstevel@tonic-gate */ 41920Sstevel@tonic-gate 41930Sstevel@tonic-gate do { 41940Sstevel@tonic-gate /* 41950Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this 41960Sstevel@tonic-gate * message or if the caller passed in "more". 41970Sstevel@tonic-gate * Error for transports with zero tidu_size. 41980Sstevel@tonic-gate */ 41990Sstevel@tonic-gate tdr.PRIM_type = prim; 42008348SEric.Yu@Sun.COM iosize = sti->sti_tidu_size; 42010Sstevel@tonic-gate if (iosize <= 0) 42020Sstevel@tonic-gate return (EMSGSIZE); 42030Sstevel@tonic-gate if (uiop->uio_resid > iosize) { 42040Sstevel@tonic-gate tdr.MORE_flag = 1; 42050Sstevel@tonic-gate } else { 42060Sstevel@tonic-gate if (more) 42070Sstevel@tonic-gate tdr.MORE_flag = 1; 42080Sstevel@tonic-gate else 42090Sstevel@tonic-gate tdr.MORE_flag = 0; 42100Sstevel@tonic-gate iosize = uiop->uio_resid; 42110Sstevel@tonic-gate } 42120Sstevel@tonic-gate dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n", 42135240Snordmark prim, tdr.MORE_flag, iosize)); 42140Sstevel@tonic-gate mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR); 42150Sstevel@tonic-gate if (mp == NULL) { 42160Sstevel@tonic-gate /* 42170Sstevel@tonic-gate * Caught a signal waiting for memory. 42180Sstevel@tonic-gate * Let send* return EINTR. 42190Sstevel@tonic-gate */ 42208348SEric.Yu@Sun.COM return (EINTR); 42210Sstevel@tonic-gate } 42220Sstevel@tonic-gate 42230Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 42245240Snordmark 0, sflag | MSG_BAND, 0); 42250Sstevel@tonic-gate if (error) { 42260Sstevel@tonic-gate eprintsoline(so, error); 42270Sstevel@tonic-gate return (error); 42280Sstevel@tonic-gate } 42290Sstevel@tonic-gate if (uiop->uio_resid > 0) { 42300Sstevel@tonic-gate /* 42310Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though 42320Sstevel@tonic-gate * some data have been written. This is consistent 42330Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics. 42340Sstevel@tonic-gate */ 42350Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) { 42360Sstevel@tonic-gate eprintsoline(so, error); 42370Sstevel@tonic-gate return (EPIPE); 42380Sstevel@tonic-gate } 42390Sstevel@tonic-gate if (so->so_error != 0) { 42400Sstevel@tonic-gate mutex_enter(&so->so_lock); 42418348SEric.Yu@Sun.COM error = sogeterr(so, B_TRUE); 42420Sstevel@tonic-gate mutex_exit(&so->so_lock); 42430Sstevel@tonic-gate if (error != 0) { 42440Sstevel@tonic-gate eprintsoline(so, error); 42450Sstevel@tonic-gate return (error); 42460Sstevel@tonic-gate } 42470Sstevel@tonic-gate } 42480Sstevel@tonic-gate } 42490Sstevel@tonic-gate } while (uiop->uio_resid > 0); 42500Sstevel@tonic-gate return (0); 42510Sstevel@tonic-gate } 42520Sstevel@tonic-gate 42530Sstevel@tonic-gate /* 42540Sstevel@tonic-gate * Check the state for errors and call the appropriate send function. 42550Sstevel@tonic-gate * 42560Sstevel@tonic-gate * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set) 42570Sstevel@tonic-gate * this function issues a setsockopt to toggle SO_DONTROUTE before and 42580Sstevel@tonic-gate * after sending the message. 42590Sstevel@tonic-gate */ 42600Sstevel@tonic-gate static int 42618348SEric.Yu@Sun.COM sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, 42628348SEric.Yu@Sun.COM struct cred *cr) 42630Sstevel@tonic-gate { 42640Sstevel@tonic-gate int so_state; 42650Sstevel@tonic-gate int so_mode; 42660Sstevel@tonic-gate int error; 42670Sstevel@tonic-gate struct sockaddr *name; 42680Sstevel@tonic-gate t_uscalar_t namelen; 42690Sstevel@tonic-gate int dontroute; 42700Sstevel@tonic-gate int flags; 42718348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 42720Sstevel@tonic-gate 42730Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n", 42747240Srh87107 (void *)so, (void *)msg, msg->msg_flags, 42755240Snordmark pr_state(so->so_state, so->so_mode), so->so_error)); 42760Sstevel@tonic-gate 42778348SEric.Yu@Sun.COM if (so->so_version == SOV_STREAM) { 42788348SEric.Yu@Sun.COM /* The imaginary "sockmod" has been popped - act as a stream */ 42798348SEric.Yu@Sun.COM so_update_attrs(so, SOMOD); 42808348SEric.Yu@Sun.COM return (strwrite(SOTOV(so), uiop, cr)); 42818348SEric.Yu@Sun.COM } 42828348SEric.Yu@Sun.COM 42830Sstevel@tonic-gate mutex_enter(&so->so_lock); 42840Sstevel@tonic-gate so_state = so->so_state; 42850Sstevel@tonic-gate 42860Sstevel@tonic-gate if (so_state & SS_CANTSENDMORE) { 42870Sstevel@tonic-gate mutex_exit(&so->so_lock); 42880Sstevel@tonic-gate return (EPIPE); 42890Sstevel@tonic-gate } 42900Sstevel@tonic-gate 42910Sstevel@tonic-gate if (so->so_error != 0) { 42928348SEric.Yu@Sun.COM error = sogeterr(so, B_TRUE); 42930Sstevel@tonic-gate if (error != 0) { 42940Sstevel@tonic-gate mutex_exit(&so->so_lock); 42950Sstevel@tonic-gate return (error); 42960Sstevel@tonic-gate } 42970Sstevel@tonic-gate } 42980Sstevel@tonic-gate 42990Sstevel@tonic-gate name = (struct sockaddr *)msg->msg_name; 43000Sstevel@tonic-gate namelen = msg->msg_namelen; 43010Sstevel@tonic-gate 43020Sstevel@tonic-gate so_mode = so->so_mode; 43030Sstevel@tonic-gate 43040Sstevel@tonic-gate if (name == NULL) { 43050Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED)) { 43060Sstevel@tonic-gate mutex_exit(&so->so_lock); 43070Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) 43080Sstevel@tonic-gate return (ENOTCONN); 43090Sstevel@tonic-gate else 43100Sstevel@tonic-gate return (EDESTADDRREQ); 43110Sstevel@tonic-gate } 43120Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) { 43130Sstevel@tonic-gate name = NULL; 43140Sstevel@tonic-gate namelen = 0; 43150Sstevel@tonic-gate } else { 43160Sstevel@tonic-gate /* 43178348SEric.Yu@Sun.COM * Note that this code does not prevent sti_faddr_sa 43180Sstevel@tonic-gate * from changing while it is being used. Thus 43190Sstevel@tonic-gate * if an "unconnect"+connect occurs concurrently with 43200Sstevel@tonic-gate * this send the datagram might be delivered to a 43210Sstevel@tonic-gate * garbaled address. 43220Sstevel@tonic-gate */ 43238348SEric.Yu@Sun.COM ASSERT(sti->sti_faddr_sa); 43248348SEric.Yu@Sun.COM name = sti->sti_faddr_sa; 43258348SEric.Yu@Sun.COM namelen = (t_uscalar_t)sti->sti_faddr_len; 43260Sstevel@tonic-gate } 43270Sstevel@tonic-gate } else { 43280Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED) && 43290Sstevel@tonic-gate (so_mode & SM_CONNREQUIRED)) { 43300Sstevel@tonic-gate /* Required but not connected */ 43310Sstevel@tonic-gate mutex_exit(&so->so_lock); 43320Sstevel@tonic-gate return (ENOTCONN); 43330Sstevel@tonic-gate } 43340Sstevel@tonic-gate /* 43350Sstevel@tonic-gate * Ignore the address on connection-oriented sockets. 43360Sstevel@tonic-gate * Just like BSD this code does not generate an error for 43370Sstevel@tonic-gate * TCP (a CONNREQUIRED socket) when sending to an address 43380Sstevel@tonic-gate * passed in with sendto/sendmsg. Instead the data is 43390Sstevel@tonic-gate * delivered on the connection as if no address had been 43400Sstevel@tonic-gate * supplied. 43410Sstevel@tonic-gate */ 43420Sstevel@tonic-gate if ((so_state & SS_ISCONNECTED) && 43430Sstevel@tonic-gate !(so_mode & SM_CONNREQUIRED)) { 43440Sstevel@tonic-gate mutex_exit(&so->so_lock); 43450Sstevel@tonic-gate return (EISCONN); 43460Sstevel@tonic-gate } 43470Sstevel@tonic-gate if (!(so_state & SS_ISBOUND)) { 43480Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 43490Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0, 43508348SEric.Yu@Sun.COM _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr); 43510Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 43520Sstevel@tonic-gate if (error) { 43530Sstevel@tonic-gate mutex_exit(&so->so_lock); 43540Sstevel@tonic-gate eprintsoline(so, error); 43550Sstevel@tonic-gate return (error); 43560Sstevel@tonic-gate } 43570Sstevel@tonic-gate } 43580Sstevel@tonic-gate /* 43590Sstevel@tonic-gate * Handle delayed datagram errors. These are only queued 43600Sstevel@tonic-gate * when the application sets SO_DGRAM_ERRIND. 43610Sstevel@tonic-gate * Return the error if we are sending to the address 43620Sstevel@tonic-gate * that was returned in the last T_UDERROR_IND. 43630Sstevel@tonic-gate * If sending to some other address discard the delayed 43640Sstevel@tonic-gate * error indication. 43650Sstevel@tonic-gate */ 43668348SEric.Yu@Sun.COM if (sti->sti_delayed_error) { 43670Sstevel@tonic-gate struct T_uderror_ind *tudi; 43680Sstevel@tonic-gate void *addr; 43690Sstevel@tonic-gate t_uscalar_t addrlen; 43700Sstevel@tonic-gate boolean_t match = B_FALSE; 43710Sstevel@tonic-gate 43728348SEric.Yu@Sun.COM ASSERT(sti->sti_eaddr_mp); 43738348SEric.Yu@Sun.COM error = sti->sti_delayed_error; 43748348SEric.Yu@Sun.COM sti->sti_delayed_error = 0; 43758348SEric.Yu@Sun.COM tudi = 43768348SEric.Yu@Sun.COM (struct T_uderror_ind *)sti->sti_eaddr_mp->b_rptr; 43770Sstevel@tonic-gate addrlen = tudi->DEST_length; 43788348SEric.Yu@Sun.COM addr = sogetoff(sti->sti_eaddr_mp, 43798348SEric.Yu@Sun.COM tudi->DEST_offset, addrlen, 1); 43800Sstevel@tonic-gate ASSERT(addr); /* Checked by strsock_proto */ 43810Sstevel@tonic-gate switch (so->so_family) { 43820Sstevel@tonic-gate case AF_INET: { 43830Sstevel@tonic-gate /* Compare just IP address and port */ 43840Sstevel@tonic-gate sin_t *sin1 = (sin_t *)name; 43850Sstevel@tonic-gate sin_t *sin2 = (sin_t *)addr; 43860Sstevel@tonic-gate 43870Sstevel@tonic-gate if (addrlen == sizeof (sin_t) && 43880Sstevel@tonic-gate namelen == addrlen && 43890Sstevel@tonic-gate sin1->sin_port == sin2->sin_port && 43900Sstevel@tonic-gate sin1->sin_addr.s_addr == 43910Sstevel@tonic-gate sin2->sin_addr.s_addr) 43920Sstevel@tonic-gate match = B_TRUE; 43930Sstevel@tonic-gate break; 43940Sstevel@tonic-gate } 43950Sstevel@tonic-gate case AF_INET6: { 43960Sstevel@tonic-gate /* Compare just IP address and port. Not flow */ 43970Sstevel@tonic-gate sin6_t *sin1 = (sin6_t *)name; 43980Sstevel@tonic-gate sin6_t *sin2 = (sin6_t *)addr; 43990Sstevel@tonic-gate 44000Sstevel@tonic-gate if (addrlen == sizeof (sin6_t) && 44010Sstevel@tonic-gate namelen == addrlen && 44020Sstevel@tonic-gate sin1->sin6_port == sin2->sin6_port && 44030Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr, 44045240Snordmark &sin2->sin6_addr)) 44050Sstevel@tonic-gate match = B_TRUE; 44060Sstevel@tonic-gate break; 44070Sstevel@tonic-gate } 44080Sstevel@tonic-gate case AF_UNIX: 44090Sstevel@tonic-gate default: 44100Sstevel@tonic-gate if (namelen == addrlen && 44110Sstevel@tonic-gate bcmp(name, addr, namelen) == 0) 44120Sstevel@tonic-gate match = B_TRUE; 44130Sstevel@tonic-gate } 44140Sstevel@tonic-gate if (match) { 44158348SEric.Yu@Sun.COM freemsg(sti->sti_eaddr_mp); 44168348SEric.Yu@Sun.COM sti->sti_eaddr_mp = NULL; 44170Sstevel@tonic-gate mutex_exit(&so->so_lock); 44180Sstevel@tonic-gate #ifdef DEBUG 44190Sstevel@tonic-gate dprintso(so, 0, 44205240Snordmark ("sockfs delayed error %d for %s\n", 44215240Snordmark error, 44225240Snordmark pr_addr(so->so_family, name, namelen))); 44230Sstevel@tonic-gate #endif /* DEBUG */ 44240Sstevel@tonic-gate return (error); 44250Sstevel@tonic-gate } 44268348SEric.Yu@Sun.COM freemsg(sti->sti_eaddr_mp); 44278348SEric.Yu@Sun.COM sti->sti_eaddr_mp = NULL; 44280Sstevel@tonic-gate } 44290Sstevel@tonic-gate } 44300Sstevel@tonic-gate mutex_exit(&so->so_lock); 44310Sstevel@tonic-gate 44320Sstevel@tonic-gate flags = msg->msg_flags; 44330Sstevel@tonic-gate dontroute = 0; 44340Sstevel@tonic-gate if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) { 44350Sstevel@tonic-gate uint32_t val; 44360Sstevel@tonic-gate 44370Sstevel@tonic-gate val = 1; 44380Sstevel@tonic-gate error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 44398348SEric.Yu@Sun.COM &val, (t_uscalar_t)sizeof (val), cr); 44400Sstevel@tonic-gate if (error) 44410Sstevel@tonic-gate return (error); 44420Sstevel@tonic-gate dontroute = 1; 44430Sstevel@tonic-gate } 44440Sstevel@tonic-gate 44450Sstevel@tonic-gate if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) { 44460Sstevel@tonic-gate error = EOPNOTSUPP; 44470Sstevel@tonic-gate goto done; 44480Sstevel@tonic-gate } 44490Sstevel@tonic-gate if (msg->msg_controllen != 0) { 44500Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) { 44518348SEric.Yu@Sun.COM so_update_attrs(so, SOMOD); 44520Sstevel@tonic-gate error = sosend_dgramcmsg(so, name, namelen, uiop, 4453741Smasputra msg->msg_control, msg->msg_controllen, flags); 44540Sstevel@tonic-gate } else { 44550Sstevel@tonic-gate if (flags & MSG_OOB) { 44560Sstevel@tonic-gate /* Can't generate T_EXDATA_REQ with options */ 44570Sstevel@tonic-gate error = EOPNOTSUPP; 44580Sstevel@tonic-gate goto done; 44590Sstevel@tonic-gate } 44608348SEric.Yu@Sun.COM so_update_attrs(so, SOMOD); 44610Sstevel@tonic-gate error = sosend_svccmsg(so, uiop, 44625240Snordmark !(flags & MSG_EOR), 44635240Snordmark msg->msg_control, msg->msg_controllen, 44645240Snordmark flags); 44650Sstevel@tonic-gate } 44660Sstevel@tonic-gate goto done; 44670Sstevel@tonic-gate } 44680Sstevel@tonic-gate 44698348SEric.Yu@Sun.COM so_update_attrs(so, SOMOD); 44700Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) { 44710Sstevel@tonic-gate /* 44720Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately 4473741Smasputra * from send_dgram. This can allow tail-call optimizations. 44740Sstevel@tonic-gate */ 44750Sstevel@tonic-gate if (!dontroute) { 44760Sstevel@tonic-gate return (sosend_dgram(so, name, namelen, uiop, flags)); 44770Sstevel@tonic-gate } 44780Sstevel@tonic-gate error = sosend_dgram(so, name, namelen, uiop, flags); 44790Sstevel@tonic-gate } else { 44800Sstevel@tonic-gate t_scalar_t prim; 44810Sstevel@tonic-gate int sflag; 44820Sstevel@tonic-gate 44830Sstevel@tonic-gate /* Ignore msg_name in the connected state */ 44840Sstevel@tonic-gate if (flags & MSG_OOB) { 44850Sstevel@tonic-gate prim = T_EXDATA_REQ; 44860Sstevel@tonic-gate /* 44870Sstevel@tonic-gate * Send down T_EXDATA_REQ even if there is flow 44880Sstevel@tonic-gate * control for data. 44890Sstevel@tonic-gate */ 44900Sstevel@tonic-gate sflag = MSG_IGNFLOW; 44910Sstevel@tonic-gate } else { 44920Sstevel@tonic-gate if (so_mode & SM_BYTESTREAM) { 44930Sstevel@tonic-gate /* Byte stream transport - use write */ 44940Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg: write\n")); 44958348SEric.Yu@Sun.COM 44968348SEric.Yu@Sun.COM /* Send M_DATA messages */ 44978348SEric.Yu@Sun.COM if ((sti->sti_nl7c_flags & NL7C_ENABLED) && 44988348SEric.Yu@Sun.COM (error = nl7c_data(so, uiop)) >= 0) { 44998348SEric.Yu@Sun.COM /* NL7C consumed the data */ 45008348SEric.Yu@Sun.COM return (error); 45018348SEric.Yu@Sun.COM } 45020Sstevel@tonic-gate /* 4503741Smasputra * If there is no SO_DONTROUTE to turn off, 45048348SEric.Yu@Sun.COM * sti_direct is on, and there is no flow 4505741Smasputra * control, we can take the fast path. 45060Sstevel@tonic-gate */ 45078348SEric.Yu@Sun.COM if (!dontroute && sti->sti_direct != 0 && 4508741Smasputra canputnext(SOTOV(so)->v_stream->sd_wrq)) { 4509741Smasputra return (sostream_direct(so, uiop, 45108348SEric.Yu@Sun.COM NULL, cr)); 4511741Smasputra } 45128348SEric.Yu@Sun.COM error = strwrite(SOTOV(so), uiop, cr); 45130Sstevel@tonic-gate goto done; 45140Sstevel@tonic-gate } 45150Sstevel@tonic-gate prim = T_DATA_REQ; 45160Sstevel@tonic-gate sflag = 0; 45170Sstevel@tonic-gate } 45180Sstevel@tonic-gate /* 45190Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately 45200Sstevel@tonic-gate * from sosend_svc. This can allow tail-call optimizations. 45210Sstevel@tonic-gate */ 45220Sstevel@tonic-gate if (!dontroute) 45230Sstevel@tonic-gate return (sosend_svc(so, uiop, prim, 45245240Snordmark !(flags & MSG_EOR), sflag)); 45250Sstevel@tonic-gate error = sosend_svc(so, uiop, prim, 45265240Snordmark !(flags & MSG_EOR), sflag); 45270Sstevel@tonic-gate } 45280Sstevel@tonic-gate ASSERT(dontroute); 45290Sstevel@tonic-gate done: 45300Sstevel@tonic-gate if (dontroute) { 45310Sstevel@tonic-gate uint32_t val; 45320Sstevel@tonic-gate 45330Sstevel@tonic-gate val = 0; 45340Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 45358348SEric.Yu@Sun.COM &val, (t_uscalar_t)sizeof (val), cr); 45368348SEric.Yu@Sun.COM } 45378348SEric.Yu@Sun.COM return (error); 45388348SEric.Yu@Sun.COM } 45398348SEric.Yu@Sun.COM 45408348SEric.Yu@Sun.COM /* 45418348SEric.Yu@Sun.COM * kstrwritemp() has very similar semantics as that of strwrite(). 45428348SEric.Yu@Sun.COM * The main difference is it obtains mblks from the caller and also 45438348SEric.Yu@Sun.COM * does not do any copy as done in strwrite() from user buffers to 45448348SEric.Yu@Sun.COM * kernel buffers. 45458348SEric.Yu@Sun.COM * 45468348SEric.Yu@Sun.COM * Currently, this routine is used by sendfile to send data allocated 45478348SEric.Yu@Sun.COM * within the kernel without any copying. This interface does not use the 45488348SEric.Yu@Sun.COM * synchronous stream interface as synch. stream interface implies 45498348SEric.Yu@Sun.COM * copying. 45508348SEric.Yu@Sun.COM */ 45518348SEric.Yu@Sun.COM int 45528348SEric.Yu@Sun.COM kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode) 45538348SEric.Yu@Sun.COM { 45548348SEric.Yu@Sun.COM struct stdata *stp; 45558348SEric.Yu@Sun.COM struct queue *wqp; 45568348SEric.Yu@Sun.COM mblk_t *newmp; 45578348SEric.Yu@Sun.COM char waitflag; 45588348SEric.Yu@Sun.COM int tempmode; 45598348SEric.Yu@Sun.COM int error = 0; 45608348SEric.Yu@Sun.COM int done = 0; 45618348SEric.Yu@Sun.COM struct sonode *so; 45628348SEric.Yu@Sun.COM boolean_t direct; 45638348SEric.Yu@Sun.COM 45648348SEric.Yu@Sun.COM ASSERT(vp->v_stream); 45658348SEric.Yu@Sun.COM stp = vp->v_stream; 45668348SEric.Yu@Sun.COM 45678348SEric.Yu@Sun.COM so = VTOSO(vp); 45688348SEric.Yu@Sun.COM direct = _SOTOTPI(so)->sti_direct; 45698348SEric.Yu@Sun.COM 45708348SEric.Yu@Sun.COM /* 45718348SEric.Yu@Sun.COM * This is the sockfs direct fast path. canputnext() need 45728348SEric.Yu@Sun.COM * not be accurate so we don't grab the sd_lock here. If 45738348SEric.Yu@Sun.COM * we get flow-controlled, we grab sd_lock just before the 45748348SEric.Yu@Sun.COM * do..while loop below to emulate what strwrite() does. 45758348SEric.Yu@Sun.COM */ 45768348SEric.Yu@Sun.COM wqp = stp->sd_wrq; 45778348SEric.Yu@Sun.COM if (canputnext(wqp) && direct && 45788348SEric.Yu@Sun.COM !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) { 45798348SEric.Yu@Sun.COM return (sostream_direct(so, NULL, mp, CRED())); 45808348SEric.Yu@Sun.COM } else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) { 45818348SEric.Yu@Sun.COM /* Fast check of flags before acquiring the lock */ 45828348SEric.Yu@Sun.COM mutex_enter(&stp->sd_lock); 45838348SEric.Yu@Sun.COM error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0); 45848348SEric.Yu@Sun.COM mutex_exit(&stp->sd_lock); 45858348SEric.Yu@Sun.COM if (error != 0) { 45868348SEric.Yu@Sun.COM if (!(stp->sd_flag & STPLEX) && 45878348SEric.Yu@Sun.COM (stp->sd_wput_opt & SW_SIGPIPE)) { 45888348SEric.Yu@Sun.COM error = EPIPE; 45898348SEric.Yu@Sun.COM } 45908348SEric.Yu@Sun.COM return (error); 45918348SEric.Yu@Sun.COM } 45928348SEric.Yu@Sun.COM } 45938348SEric.Yu@Sun.COM 45948348SEric.Yu@Sun.COM waitflag = WRITEWAIT; 45958348SEric.Yu@Sun.COM if (stp->sd_flag & OLDNDELAY) 45968348SEric.Yu@Sun.COM tempmode = fmode & ~FNDELAY; 45978348SEric.Yu@Sun.COM else 45988348SEric.Yu@Sun.COM tempmode = fmode; 45998348SEric.Yu@Sun.COM 46008348SEric.Yu@Sun.COM mutex_enter(&stp->sd_lock); 46018348SEric.Yu@Sun.COM do { 46028348SEric.Yu@Sun.COM if (canputnext(wqp)) { 46038348SEric.Yu@Sun.COM mutex_exit(&stp->sd_lock); 46048348SEric.Yu@Sun.COM if (stp->sd_wputdatafunc != NULL) { 46058348SEric.Yu@Sun.COM newmp = (stp->sd_wputdatafunc)(vp, mp, NULL, 46068348SEric.Yu@Sun.COM NULL, NULL, NULL); 46078348SEric.Yu@Sun.COM if (newmp == NULL) { 46088348SEric.Yu@Sun.COM /* The caller will free mp */ 46098348SEric.Yu@Sun.COM return (ECOMM); 46108348SEric.Yu@Sun.COM } 46118348SEric.Yu@Sun.COM mp = newmp; 46128348SEric.Yu@Sun.COM } 46138348SEric.Yu@Sun.COM putnext(wqp, mp); 46148348SEric.Yu@Sun.COM return (0); 46158348SEric.Yu@Sun.COM } 46168348SEric.Yu@Sun.COM error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1, 46178348SEric.Yu@Sun.COM &done); 46188348SEric.Yu@Sun.COM } while (error == 0 && !done); 46198348SEric.Yu@Sun.COM 46208348SEric.Yu@Sun.COM mutex_exit(&stp->sd_lock); 46218348SEric.Yu@Sun.COM /* 46228348SEric.Yu@Sun.COM * EAGAIN tells the application to try again. ENOMEM 46238348SEric.Yu@Sun.COM * is returned only if the memory allocation size 46248348SEric.Yu@Sun.COM * exceeds the physical limits of the system. ENOMEM 46258348SEric.Yu@Sun.COM * can't be true here. 46268348SEric.Yu@Sun.COM */ 46278348SEric.Yu@Sun.COM if (error == ENOMEM) 46288348SEric.Yu@Sun.COM error = EAGAIN; 46298348SEric.Yu@Sun.COM return (error); 46308348SEric.Yu@Sun.COM } 46318348SEric.Yu@Sun.COM 46328348SEric.Yu@Sun.COM /* ARGSUSED */ 46338348SEric.Yu@Sun.COM static int 46348348SEric.Yu@Sun.COM sotpi_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag, 46358348SEric.Yu@Sun.COM struct cred *cr, mblk_t **mpp) 46368348SEric.Yu@Sun.COM { 46378348SEric.Yu@Sun.COM int error; 46388348SEric.Yu@Sun.COM 46398348SEric.Yu@Sun.COM if (so->so_family != AF_INET && so->so_family != AF_INET6) 46408348SEric.Yu@Sun.COM return (EAFNOSUPPORT); 46418348SEric.Yu@Sun.COM 46428348SEric.Yu@Sun.COM if (so->so_state & SS_CANTSENDMORE) 46438348SEric.Yu@Sun.COM return (EPIPE); 46448348SEric.Yu@Sun.COM 46458348SEric.Yu@Sun.COM if (so->so_type != SOCK_STREAM) 46468348SEric.Yu@Sun.COM return (EOPNOTSUPP); 46478348SEric.Yu@Sun.COM 46488348SEric.Yu@Sun.COM if ((so->so_state & SS_ISCONNECTED) == 0) 46498348SEric.Yu@Sun.COM return (ENOTCONN); 46508348SEric.Yu@Sun.COM 46518348SEric.Yu@Sun.COM error = kstrwritemp(so->so_vnode, *mpp, fflag); 46528348SEric.Yu@Sun.COM if (error == 0) 46538348SEric.Yu@Sun.COM *mpp = NULL; 46540Sstevel@tonic-gate return (error); 46550Sstevel@tonic-gate } 46560Sstevel@tonic-gate 46570Sstevel@tonic-gate /* 4658741Smasputra * Sending data on a datagram socket. 4659741Smasputra * Assumes caller has verified that SS_ISBOUND etc. are set. 4660741Smasputra */ 4661741Smasputra /* ARGSUSED */ 4662741Smasputra static int 4663741Smasputra sodgram_direct(struct sonode *so, struct sockaddr *name, 4664741Smasputra socklen_t namelen, struct uio *uiop, int flags) 4665741Smasputra { 4666741Smasputra struct T_unitdata_req tudr; 46675240Snordmark mblk_t *mp = NULL; 4668741Smasputra int error = 0; 4669741Smasputra void *addr; 4670741Smasputra socklen_t addrlen; 4671741Smasputra ssize_t len; 4672741Smasputra struct stdata *stp = SOTOV(so)->v_stream; 4673741Smasputra int so_state; 4674741Smasputra queue_t *udp_wq; 46755240Snordmark boolean_t connected; 46765240Snordmark mblk_t *mpdata = NULL; 46778348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 4678741Smasputra 4679741Smasputra ASSERT(name != NULL && namelen != 0); 4680741Smasputra ASSERT(!(so->so_mode & SM_CONNREQUIRED)); 4681741Smasputra ASSERT(!(so->so_mode & SM_EXDATA)); 4682741Smasputra ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 4683741Smasputra ASSERT(SOTOV(so)->v_type == VSOCK); 4684741Smasputra 4685741Smasputra /* Caller checked for proper length */ 4686741Smasputra len = uiop->uio_resid; 46878348SEric.Yu@Sun.COM ASSERT(len <= sti->sti_tidu_size); 4688741Smasputra 4689741Smasputra /* Length and family checks have been done by caller */ 4690741Smasputra ASSERT(name->sa_family == so->so_family); 4691741Smasputra ASSERT(so->so_family == AF_INET || 4692741Smasputra (namelen == (socklen_t)sizeof (struct sockaddr_in6))); 4693741Smasputra ASSERT(so->so_family == AF_INET6 || 4694741Smasputra (namelen == (socklen_t)sizeof (struct sockaddr_in))); 4695741Smasputra 4696741Smasputra addr = name; 4697741Smasputra addrlen = namelen; 4698741Smasputra 4699741Smasputra if (stp->sd_sidp != NULL && 4700741Smasputra (error = straccess(stp, JCWRITE)) != 0) 4701741Smasputra goto done; 4702741Smasputra 4703741Smasputra so_state = so->so_state; 4704741Smasputra 47055240Snordmark connected = so_state & SS_ISCONNECTED; 47065240Snordmark if (!connected) { 47075240Snordmark tudr.PRIM_type = T_UNITDATA_REQ; 47085240Snordmark tudr.DEST_length = addrlen; 47095240Snordmark tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 47105240Snordmark tudr.OPT_length = 0; 47115240Snordmark tudr.OPT_offset = 0; 47125240Snordmark 47135240Snordmark mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0, 47145240Snordmark _ALLOC_INTR); 47155240Snordmark if (mp == NULL) { 47165240Snordmark /* 47175240Snordmark * Caught a signal waiting for memory. 47185240Snordmark * Let send* return EINTR. 47195240Snordmark */ 47205240Snordmark error = EINTR; 47215240Snordmark goto done; 47225240Snordmark } 47235240Snordmark } 47245240Snordmark 4725741Smasputra /* 4726741Smasputra * For UDP we don't break up the copyin into smaller pieces 4727741Smasputra * as in the TCP case. That means if ENOMEM is returned by 4728741Smasputra * mcopyinuio() then the uio vector has not been modified at 4729741Smasputra * all and we fallback to either strwrite() or kstrputmsg() 4730741Smasputra * below. Note also that we never generate priority messages 4731741Smasputra * from here. 4732741Smasputra */ 4733741Smasputra udp_wq = stp->sd_wrq->q_next; 4734741Smasputra if (canput(udp_wq) && 47355240Snordmark (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) { 47365240Snordmark ASSERT(DB_TYPE(mpdata) == M_DATA); 4737741Smasputra ASSERT(uiop->uio_resid == 0); 47385240Snordmark if (!connected) 47395240Snordmark linkb(mp, mpdata); 47405240Snordmark else 47415240Snordmark mp = mpdata; 4742741Smasputra if (audit_active) 4743741Smasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 47445240Snordmark 47455240Snordmark udp_wput(udp_wq, mp); 4746741Smasputra return (0); 4747741Smasputra } 47485240Snordmark 47495240Snordmark ASSERT(mpdata == NULL); 47505240Snordmark if (error != 0 && error != ENOMEM) { 47515240Snordmark freemsg(mp); 4752741Smasputra return (error); 47535240Snordmark } 4754741Smasputra 4755741Smasputra /* 4756741Smasputra * For connected, let strwrite() handle the blocking case. 4757741Smasputra * Otherwise we fall thru and use kstrputmsg(). 4758741Smasputra */ 47595240Snordmark if (connected) 4760741Smasputra return (strwrite(SOTOV(so), uiop, CRED())); 4761741Smasputra 4762741Smasputra if (audit_active) 4763741Smasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 4764741Smasputra 4765741Smasputra error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 4766741Smasputra done: 4767741Smasputra #ifdef SOCK_DEBUG 4768741Smasputra if (error != 0) { 4769741Smasputra eprintsoline(so, error); 4770741Smasputra } 4771741Smasputra #endif /* SOCK_DEBUG */ 4772741Smasputra return (error); 4773741Smasputra } 4774741Smasputra 4775741Smasputra int 4776741Smasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr) 4777741Smasputra { 4778741Smasputra struct stdata *stp = SOTOV(so)->v_stream; 4779741Smasputra ssize_t iosize, rmax, maxblk; 4780741Smasputra queue_t *tcp_wq = stp->sd_wrq->q_next; 4781898Skais mblk_t *newmp; 4782741Smasputra int error = 0, wflag = 0; 4783741Smasputra 4784741Smasputra ASSERT(so->so_mode & SM_BYTESTREAM); 4785741Smasputra ASSERT(SOTOV(so)->v_type == VSOCK); 4786741Smasputra 4787741Smasputra if (stp->sd_sidp != NULL && 4788741Smasputra (error = straccess(stp, JCWRITE)) != 0) 4789741Smasputra return (error); 4790741Smasputra 4791741Smasputra if (uiop == NULL) { 4792741Smasputra /* 4793741Smasputra * kstrwritemp() should have checked sd_flag and 4794741Smasputra * flow-control before coming here. If we end up 4795741Smasputra * here it means that we can simply pass down the 4796741Smasputra * data to tcp. 4797741Smasputra */ 4798741Smasputra ASSERT(mp != NULL); 4799898Skais if (stp->sd_wputdatafunc != NULL) { 4800898Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL, 4801898Skais NULL, NULL, NULL); 4802898Skais if (newmp == NULL) { 4803898Skais /* The caller will free mp */ 4804898Skais return (ECOMM); 4805898Skais } 4806898Skais mp = newmp; 4807898Skais } 4808741Smasputra tcp_wput(tcp_wq, mp); 4809741Smasputra return (0); 4810741Smasputra } 4811741Smasputra 4812741Smasputra /* Fallback to strwrite() to do proper error handling */ 4813741Smasputra if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY)) 4814741Smasputra return (strwrite(SOTOV(so), uiop, cr)); 4815741Smasputra 4816741Smasputra rmax = stp->sd_qn_maxpsz; 4817741Smasputra ASSERT(rmax >= 0 || rmax == INFPSZ); 4818741Smasputra if (rmax == 0 || uiop->uio_resid <= 0) 4819741Smasputra return (0); 4820741Smasputra 4821741Smasputra if (rmax == INFPSZ) 4822741Smasputra rmax = uiop->uio_resid; 4823741Smasputra 4824741Smasputra maxblk = stp->sd_maxblk; 4825741Smasputra 4826741Smasputra for (;;) { 4827741Smasputra iosize = MIN(uiop->uio_resid, rmax); 4828741Smasputra 4829741Smasputra mp = mcopyinuio(stp, uiop, iosize, maxblk, &error); 4830741Smasputra if (mp == NULL) { 4831741Smasputra /* 4832741Smasputra * Fallback to strwrite() for ENOMEM; if this 4833741Smasputra * is our first time in this routine and the uio 4834741Smasputra * vector has not been modified, we will end up 4835741Smasputra * calling strwrite() without any flag set. 4836741Smasputra */ 4837741Smasputra if (error == ENOMEM) 4838741Smasputra goto slow_send; 4839741Smasputra else 4840741Smasputra return (error); 4841741Smasputra } 4842741Smasputra ASSERT(uiop->uio_resid >= 0); 4843741Smasputra /* 4844741Smasputra * If mp is non-NULL and ENOMEM is set, it means that 4845741Smasputra * mcopyinuio() was able to break down some of the user 4846741Smasputra * data into one or more mblks. Send the partial data 4847741Smasputra * to tcp and let the rest be handled in strwrite(). 4848741Smasputra */ 4849741Smasputra ASSERT(error == 0 || error == ENOMEM); 4850898Skais if (stp->sd_wputdatafunc != NULL) { 4851898Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL, 4852898Skais NULL, NULL, NULL); 4853898Skais if (newmp == NULL) { 4854898Skais /* The caller will free mp */ 4855898Skais return (ECOMM); 4856898Skais } 4857898Skais mp = newmp; 4858898Skais } 4859741Smasputra tcp_wput(tcp_wq, mp); 4860741Smasputra 4861741Smasputra wflag |= NOINTR; 4862741Smasputra 4863741Smasputra if (uiop->uio_resid == 0) { /* No more data; we're done */ 4864741Smasputra ASSERT(error == 0); 4865741Smasputra break; 4866741Smasputra } else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag & 4867741Smasputra (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) { 4868741Smasputra slow_send: 4869741Smasputra /* 4870741Smasputra * We were able to send down partial data using 4871741Smasputra * the direct call interface, but are now relying 4872741Smasputra * on strwrite() to handle the non-fastpath cases. 4873741Smasputra * If the socket is blocking we will sleep in 4874741Smasputra * strwaitq() until write is permitted, otherwise, 4875741Smasputra * we will need to return the amount of bytes 4876741Smasputra * written so far back to the app. This is the 4877741Smasputra * reason why we pass NOINTR flag to strwrite() 4878741Smasputra * for non-blocking socket, because we don't want 4879741Smasputra * to return EAGAIN when portion of the user data 4880741Smasputra * has actually been sent down. 4881741Smasputra */ 4882741Smasputra return (strwrite_common(SOTOV(so), uiop, cr, wflag)); 4883741Smasputra } 4884741Smasputra } 4885741Smasputra return (0); 4886741Smasputra } 4887741Smasputra 4888741Smasputra /* 48898348SEric.Yu@Sun.COM * Update sti_faddr by asking the transport (unless AF_UNIX). 48900Sstevel@tonic-gate */ 48918348SEric.Yu@Sun.COM /* ARGSUSED */ 48920Sstevel@tonic-gate int 48938348SEric.Yu@Sun.COM sotpi_getpeername(struct sonode *so, struct sockaddr *name, socklen_t *namelen, 48948348SEric.Yu@Sun.COM boolean_t accept, struct cred *cr) 48950Sstevel@tonic-gate { 48960Sstevel@tonic-gate struct strbuf strbuf; 48970Sstevel@tonic-gate int error = 0, res; 48980Sstevel@tonic-gate void *addr; 48990Sstevel@tonic-gate t_uscalar_t addrlen; 49000Sstevel@tonic-gate k_sigset_t smask; 49018348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 49020Sstevel@tonic-gate 49030Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername(%p) %s\n", 49047240Srh87107 (void *)so, pr_state(so->so_state, so->so_mode))); 49050Sstevel@tonic-gate 49068348SEric.Yu@Sun.COM ASSERT(*namelen > 0); 49070Sstevel@tonic-gate mutex_enter(&so->so_lock); 49080Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 49098348SEric.Yu@Sun.COM 49108348SEric.Yu@Sun.COM if (accept) { 49118348SEric.Yu@Sun.COM bcopy(sti->sti_faddr_sa, name, 49128348SEric.Yu@Sun.COM MIN(*namelen, sti->sti_faddr_len)); 49138348SEric.Yu@Sun.COM *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len; 49148348SEric.Yu@Sun.COM goto done; 49158348SEric.Yu@Sun.COM } 49168348SEric.Yu@Sun.COM 49170Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) { 49180Sstevel@tonic-gate error = ENOTCONN; 49190Sstevel@tonic-gate goto done; 49200Sstevel@tonic-gate } 49210Sstevel@tonic-gate /* Added this check for X/Open */ 49220Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 49230Sstevel@tonic-gate error = EINVAL; 49240Sstevel@tonic-gate if (xnet_check_print) { 49250Sstevel@tonic-gate printf("sockfs: X/Open getpeername check => EINVAL\n"); 49260Sstevel@tonic-gate } 49270Sstevel@tonic-gate goto done; 49280Sstevel@tonic-gate } 49298348SEric.Yu@Sun.COM 49308348SEric.Yu@Sun.COM if (sti->sti_faddr_valid) { 49318348SEric.Yu@Sun.COM bcopy(sti->sti_faddr_sa, name, 49328348SEric.Yu@Sun.COM MIN(*namelen, sti->sti_faddr_len)); 49338348SEric.Yu@Sun.COM *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len; 49348348SEric.Yu@Sun.COM goto done; 49358348SEric.Yu@Sun.COM } 49368348SEric.Yu@Sun.COM 49370Sstevel@tonic-gate #ifdef DEBUG 49380Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (local): %s\n", 49398348SEric.Yu@Sun.COM pr_addr(so->so_family, sti->sti_faddr_sa, 49408348SEric.Yu@Sun.COM (t_uscalar_t)sti->sti_faddr_len))); 49410Sstevel@tonic-gate #endif /* DEBUG */ 49420Sstevel@tonic-gate 49431548Srshoaib if (so->so_family == AF_UNIX) { 49440Sstevel@tonic-gate /* Transport has different name space - return local info */ 49458348SEric.Yu@Sun.COM if (sti->sti_faddr_noxlate) 49468348SEric.Yu@Sun.COM *namelen = 0; 49470Sstevel@tonic-gate error = 0; 49480Sstevel@tonic-gate goto done; 49490Sstevel@tonic-gate } 49500Sstevel@tonic-gate 49518348SEric.Yu@Sun.COM ASSERT(so->so_family != AF_UNIX && sti->sti_faddr_noxlate == 0); 49528348SEric.Yu@Sun.COM 49538348SEric.Yu@Sun.COM ASSERT(sti->sti_faddr_sa); 49540Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */ 49558348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sti->sti_faddr_maxlen; 49560Sstevel@tonic-gate mutex_exit(&so->so_lock); 49570Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP); 49580Sstevel@tonic-gate 49590Sstevel@tonic-gate /* 49600Sstevel@tonic-gate * Issue TI_GETPEERNAME with signals masked. 49618348SEric.Yu@Sun.COM * Put the result in sti_faddr_sa so that getpeername works after 49620Sstevel@tonic-gate * a shutdown(output). 49630Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 49640Sstevel@tonic-gate * back to the socket. 49650Sstevel@tonic-gate */ 49660Sstevel@tonic-gate strbuf.buf = addr; 49670Sstevel@tonic-gate strbuf.maxlen = addrlen; 49680Sstevel@tonic-gate strbuf.len = 0; 49690Sstevel@tonic-gate 49700Sstevel@tonic-gate sigintr(&smask, 0); 49710Sstevel@tonic-gate res = 0; 49728348SEric.Yu@Sun.COM ASSERT(cr); 49730Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf, 49748348SEric.Yu@Sun.COM 0, K_TO_K, cr, &res); 49750Sstevel@tonic-gate sigunintr(&smask); 49760Sstevel@tonic-gate 49770Sstevel@tonic-gate mutex_enter(&so->so_lock); 49780Sstevel@tonic-gate /* 49790Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail 49800Sstevel@tonic-gate * the getpeername. Instead fallback on the recorded 49818348SEric.Yu@Sun.COM * sti->sti_faddr_sa. 49820Sstevel@tonic-gate */ 49830Sstevel@tonic-gate if (error) { 49840Sstevel@tonic-gate /* 49850Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl. 49860Sstevel@tonic-gate * However, it is impossible to determine which ones of 49870Sstevel@tonic-gate * these are really socket level errors that were incorrectly 49880Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the 49890Sstevel@tonic-gate * error - to code explicitly does not reinstate the error 49900Sstevel@tonic-gate * using soseterror(). 49910Sstevel@tonic-gate * Experiments have shows that at least this set of 49920Sstevel@tonic-gate * errors are reported and should not be reinstated on the 49930Sstevel@tonic-gate * socket: 49940Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when 49950Sstevel@tonic-gate * getpeername was called. 49960Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write 49970Sstevel@tonic-gate * side error over the read side error. 49980Sstevel@tonic-gate * ENOTCONN The transport just got disconnected but 49990Sstevel@tonic-gate * sockfs had not yet seen the T_DISCON_IND 50000Sstevel@tonic-gate * when issuing the ioctl. 50010Sstevel@tonic-gate */ 50020Sstevel@tonic-gate error = 0; 50030Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 && 50040Sstevel@tonic-gate (so->so_state & SS_ISCONNECTED)) { 50058348SEric.Yu@Sun.COM ASSERT(strbuf.len <= (int)sti->sti_faddr_maxlen); 50068348SEric.Yu@Sun.COM sti->sti_faddr_len = (socklen_t)strbuf.len; 50078348SEric.Yu@Sun.COM bcopy(addr, sti->sti_faddr_sa, sti->sti_faddr_len); 50088348SEric.Yu@Sun.COM sti->sti_faddr_valid = 1; 50098348SEric.Yu@Sun.COM 50108348SEric.Yu@Sun.COM bcopy(addr, name, MIN(*namelen, sti->sti_faddr_len)); 50118348SEric.Yu@Sun.COM *namelen = sti->sti_faddr_len; 50120Sstevel@tonic-gate } 50130Sstevel@tonic-gate kmem_free(addr, addrlen); 50140Sstevel@tonic-gate #ifdef DEBUG 50150Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (tp): %s\n", 50168348SEric.Yu@Sun.COM pr_addr(so->so_family, sti->sti_faddr_sa, 50178348SEric.Yu@Sun.COM (t_uscalar_t)sti->sti_faddr_len))); 50180Sstevel@tonic-gate #endif /* DEBUG */ 50190Sstevel@tonic-gate done: 50200Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 50210Sstevel@tonic-gate mutex_exit(&so->so_lock); 50220Sstevel@tonic-gate return (error); 50230Sstevel@tonic-gate } 50240Sstevel@tonic-gate 50250Sstevel@tonic-gate /* 50268348SEric.Yu@Sun.COM * Update sti_laddr by asking the transport (unless AF_UNIX). 50270Sstevel@tonic-gate */ 50280Sstevel@tonic-gate int 50298348SEric.Yu@Sun.COM sotpi_getsockname(struct sonode *so, struct sockaddr *name, socklen_t *namelen, 50308348SEric.Yu@Sun.COM struct cred *cr) 50310Sstevel@tonic-gate { 50320Sstevel@tonic-gate struct strbuf strbuf; 50330Sstevel@tonic-gate int error = 0, res; 50340Sstevel@tonic-gate void *addr; 50350Sstevel@tonic-gate t_uscalar_t addrlen; 50360Sstevel@tonic-gate k_sigset_t smask; 50378348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 50380Sstevel@tonic-gate 50390Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname(%p) %s\n", 50407240Srh87107 (void *)so, pr_state(so->so_state, so->so_mode))); 50410Sstevel@tonic-gate 50428348SEric.Yu@Sun.COM ASSERT(*namelen > 0); 50430Sstevel@tonic-gate mutex_enter(&so->so_lock); 50440Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 50458348SEric.Yu@Sun.COM 50460Sstevel@tonic-gate #ifdef DEBUG 50478348SEric.Yu@Sun.COM 50480Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (local): %s\n", 50498348SEric.Yu@Sun.COM pr_addr(so->so_family, sti->sti_laddr_sa, 50508348SEric.Yu@Sun.COM (t_uscalar_t)sti->sti_laddr_len))); 50510Sstevel@tonic-gate #endif /* DEBUG */ 50528348SEric.Yu@Sun.COM if (sti->sti_laddr_valid) { 50538348SEric.Yu@Sun.COM bcopy(sti->sti_laddr_sa, name, 50548348SEric.Yu@Sun.COM MIN(*namelen, sti->sti_laddr_len)); 50558348SEric.Yu@Sun.COM *namelen = sti->sti_laddr_len; 50568348SEric.Yu@Sun.COM goto done; 50578348SEric.Yu@Sun.COM } 50588348SEric.Yu@Sun.COM 50590Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 50600Sstevel@tonic-gate /* Transport has different name space - return local info */ 50610Sstevel@tonic-gate error = 0; 50628465SEric.Yu@Sun.COM *namelen = 0; 50630Sstevel@tonic-gate goto done; 50640Sstevel@tonic-gate } 50651156Sblu if (!(so->so_state & SS_ISBOUND)) { 50661156Sblu /* If not bound, then nothing to return. */ 50671156Sblu error = 0; 50681156Sblu goto done; 50691156Sblu } 50708348SEric.Yu@Sun.COM 50710Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */ 50728348SEric.Yu@Sun.COM addrlen = (t_uscalar_t)sti->sti_laddr_maxlen; 50730Sstevel@tonic-gate mutex_exit(&so->so_lock); 50740Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP); 50750Sstevel@tonic-gate 50760Sstevel@tonic-gate /* 50770Sstevel@tonic-gate * Issue TI_GETMYNAME with signals masked. 50788348SEric.Yu@Sun.COM * Put the result in sti_laddr_sa so that getsockname works after 50790Sstevel@tonic-gate * a shutdown(output). 50800Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 50810Sstevel@tonic-gate * back to the socket. 50820Sstevel@tonic-gate */ 50830Sstevel@tonic-gate strbuf.buf = addr; 50840Sstevel@tonic-gate strbuf.maxlen = addrlen; 50850Sstevel@tonic-gate strbuf.len = 0; 50860Sstevel@tonic-gate 50870Sstevel@tonic-gate sigintr(&smask, 0); 50880Sstevel@tonic-gate res = 0; 50898348SEric.Yu@Sun.COM ASSERT(cr); 50900Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf, 50918348SEric.Yu@Sun.COM 0, K_TO_K, cr, &res); 50920Sstevel@tonic-gate sigunintr(&smask); 50930Sstevel@tonic-gate 50940Sstevel@tonic-gate mutex_enter(&so->so_lock); 50950Sstevel@tonic-gate /* 50960Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail 50970Sstevel@tonic-gate * the getsockname. Instead fallback on the recorded 50988348SEric.Yu@Sun.COM * sti->sti_laddr_sa. 50990Sstevel@tonic-gate */ 51000Sstevel@tonic-gate if (error) { 51010Sstevel@tonic-gate /* 51020Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl. 51030Sstevel@tonic-gate * However, it is impossible to determine which ones of 51040Sstevel@tonic-gate * these are really socket level errors that were incorrectly 51050Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the 51060Sstevel@tonic-gate * error - to code explicitly does not reinstate the error 51070Sstevel@tonic-gate * using soseterror(). 51080Sstevel@tonic-gate * Experiments have shows that at least this set of 51090Sstevel@tonic-gate * errors are reported and should not be reinstated on the 51100Sstevel@tonic-gate * socket: 51110Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when 51120Sstevel@tonic-gate * getsockname was called. 51130Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write 51140Sstevel@tonic-gate * side error over the read side error. 51150Sstevel@tonic-gate */ 51160Sstevel@tonic-gate error = 0; 51170Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 && 51180Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) { 51198348SEric.Yu@Sun.COM ASSERT(strbuf.len <= (int)sti->sti_laddr_maxlen); 51208348SEric.Yu@Sun.COM sti->sti_laddr_len = (socklen_t)strbuf.len; 51218348SEric.Yu@Sun.COM bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len); 51228348SEric.Yu@Sun.COM sti->sti_laddr_valid = 1; 51238348SEric.Yu@Sun.COM 51248348SEric.Yu@Sun.COM bcopy(addr, name, MIN(sti->sti_laddr_len, *namelen)); 51258348SEric.Yu@Sun.COM *namelen = sti->sti_laddr_len; 51260Sstevel@tonic-gate } 51270Sstevel@tonic-gate kmem_free(addr, addrlen); 51280Sstevel@tonic-gate #ifdef DEBUG 51290Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (tp): %s\n", 51308348SEric.Yu@Sun.COM pr_addr(so->so_family, sti->sti_laddr_sa, 51318348SEric.Yu@Sun.COM (t_uscalar_t)sti->sti_laddr_len))); 51320Sstevel@tonic-gate #endif /* DEBUG */ 51330Sstevel@tonic-gate done: 51340Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 51350Sstevel@tonic-gate mutex_exit(&so->so_lock); 51360Sstevel@tonic-gate return (error); 51370Sstevel@tonic-gate } 51380Sstevel@tonic-gate 51390Sstevel@tonic-gate /* 51400Sstevel@tonic-gate * Get socket options. For SOL_SOCKET options some options are handled 51410Sstevel@tonic-gate * by the sockfs while others use the value recorded in the sonode as a 51420Sstevel@tonic-gate * fallback should the T_SVR4_OPTMGMT_REQ fail. 51430Sstevel@tonic-gate * 51440Sstevel@tonic-gate * On the return most *optlenp bytes are copied to optval. 51450Sstevel@tonic-gate */ 51468348SEric.Yu@Sun.COM /* ARGSUSED */ 51470Sstevel@tonic-gate int 51480Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name, 51498348SEric.Yu@Sun.COM void *optval, socklen_t *optlenp, int flags, struct cred *cr) 51500Sstevel@tonic-gate { 51510Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req; 51520Sstevel@tonic-gate struct T_optmgmt_ack *optmgmt_ack; 51530Sstevel@tonic-gate struct opthdr oh; 51540Sstevel@tonic-gate struct opthdr *opt_res; 51550Sstevel@tonic-gate mblk_t *mp = NULL; 51560Sstevel@tonic-gate int error = 0; 51570Sstevel@tonic-gate void *option = NULL; /* Set if fallback value */ 51580Sstevel@tonic-gate t_uscalar_t maxlen = *optlenp; 51590Sstevel@tonic-gate t_uscalar_t len; 51600Sstevel@tonic-gate uint32_t value; 51618348SEric.Yu@Sun.COM struct timeval tmo_val; /* used for SO_RCVTIMEO, SO_SNDTIMEO */ 51628489Sshenjian struct timeval32 tmo_val32; 51638348SEric.Yu@Sun.COM struct so_snd_bufinfo snd_bufinfo; /* used for zero copy */ 51640Sstevel@tonic-gate 51650Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n", 51667240Srh87107 (void *)so, level, option_name, optval, (void *)optlenp, 51675240Snordmark pr_state(so->so_state, so->so_mode))); 51680Sstevel@tonic-gate 51690Sstevel@tonic-gate mutex_enter(&so->so_lock); 51700Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 51710Sstevel@tonic-gate 51720Sstevel@tonic-gate /* 51730Sstevel@tonic-gate * Check for SOL_SOCKET options. 51740Sstevel@tonic-gate * Certain SOL_SOCKET options are returned directly whereas 51750Sstevel@tonic-gate * others only provide a default (fallback) value should 51760Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ fail. 51770Sstevel@tonic-gate */ 51780Sstevel@tonic-gate if (level == SOL_SOCKET) { 51790Sstevel@tonic-gate /* Check parameters */ 51800Sstevel@tonic-gate switch (option_name) { 51810Sstevel@tonic-gate case SO_TYPE: 51820Sstevel@tonic-gate case SO_ERROR: 51830Sstevel@tonic-gate case SO_DEBUG: 51840Sstevel@tonic-gate case SO_ACCEPTCONN: 51850Sstevel@tonic-gate case SO_REUSEADDR: 51860Sstevel@tonic-gate case SO_KEEPALIVE: 51870Sstevel@tonic-gate case SO_DONTROUTE: 51880Sstevel@tonic-gate case SO_BROADCAST: 51890Sstevel@tonic-gate case SO_USELOOPBACK: 51900Sstevel@tonic-gate case SO_OOBINLINE: 51910Sstevel@tonic-gate case SO_SNDBUF: 51920Sstevel@tonic-gate case SO_RCVBUF: 51930Sstevel@tonic-gate #ifdef notyet 51940Sstevel@tonic-gate case SO_SNDLOWAT: 51950Sstevel@tonic-gate case SO_RCVLOWAT: 51960Sstevel@tonic-gate #endif /* notyet */ 51973388Skcpoon case SO_DOMAIN: 51980Sstevel@tonic-gate case SO_DGRAM_ERRIND: 51990Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (int32_t)) { 52000Sstevel@tonic-gate error = EINVAL; 52010Sstevel@tonic-gate eprintsoline(so, error); 52020Sstevel@tonic-gate goto done2; 52030Sstevel@tonic-gate } 52040Sstevel@tonic-gate break; 52058348SEric.Yu@Sun.COM case SO_RCVTIMEO: 52068348SEric.Yu@Sun.COM case SO_SNDTIMEO: 5207*8575Sshenjian if (get_udatamodel() == DATAMODEL_NONE || 5208*8575Sshenjian get_udatamodel() == DATAMODEL_NATIVE) { 52098489Sshenjian if (maxlen < sizeof (struct timeval)) { 52108489Sshenjian error = EINVAL; 52118489Sshenjian eprintsoline(so, error); 52128489Sshenjian goto done2; 52138489Sshenjian } 52148489Sshenjian } else { 52158489Sshenjian if (maxlen < sizeof (struct timeval32)) { 52168489Sshenjian error = EINVAL; 52178489Sshenjian eprintsoline(so, error); 52188489Sshenjian goto done2; 52198489Sshenjian } 52208489Sshenjian 52218348SEric.Yu@Sun.COM } 52228348SEric.Yu@Sun.COM break; 52230Sstevel@tonic-gate case SO_LINGER: 52240Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (struct linger)) { 52250Sstevel@tonic-gate error = EINVAL; 52260Sstevel@tonic-gate eprintsoline(so, error); 52270Sstevel@tonic-gate goto done2; 52280Sstevel@tonic-gate } 52290Sstevel@tonic-gate break; 52308348SEric.Yu@Sun.COM case SO_SND_BUFINFO: 52318348SEric.Yu@Sun.COM if (maxlen < (t_uscalar_t) 52328348SEric.Yu@Sun.COM sizeof (struct so_snd_bufinfo)) { 52338348SEric.Yu@Sun.COM error = EINVAL; 52348348SEric.Yu@Sun.COM eprintsoline(so, error); 52358348SEric.Yu@Sun.COM goto done2; 52368348SEric.Yu@Sun.COM } 52378348SEric.Yu@Sun.COM break; 52380Sstevel@tonic-gate } 52390Sstevel@tonic-gate 52400Sstevel@tonic-gate len = (t_uscalar_t)sizeof (uint32_t); /* Default */ 52410Sstevel@tonic-gate 52420Sstevel@tonic-gate switch (option_name) { 52430Sstevel@tonic-gate case SO_TYPE: 52440Sstevel@tonic-gate value = so->so_type; 52450Sstevel@tonic-gate option = &value; 52460Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 52470Sstevel@tonic-gate 52480Sstevel@tonic-gate case SO_ERROR: 52498348SEric.Yu@Sun.COM value = sogeterr(so, B_TRUE); 52500Sstevel@tonic-gate option = &value; 52510Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 52520Sstevel@tonic-gate 52530Sstevel@tonic-gate case SO_ACCEPTCONN: 52540Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) 52550Sstevel@tonic-gate value = SO_ACCEPTCONN; 52560Sstevel@tonic-gate else 52570Sstevel@tonic-gate value = 0; 52580Sstevel@tonic-gate #ifdef DEBUG 52590Sstevel@tonic-gate if (value) { 52600Sstevel@tonic-gate dprintso(so, 1, 52610Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n", 52620Sstevel@tonic-gate option_name)); 52630Sstevel@tonic-gate } else { 52640Sstevel@tonic-gate dprintso(so, 1, 52650Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n", 52660Sstevel@tonic-gate option_name)); 52670Sstevel@tonic-gate } 52680Sstevel@tonic-gate #endif /* DEBUG */ 52690Sstevel@tonic-gate option = &value; 52700Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 52710Sstevel@tonic-gate 52720Sstevel@tonic-gate case SO_DEBUG: 52730Sstevel@tonic-gate case SO_REUSEADDR: 52740Sstevel@tonic-gate case SO_KEEPALIVE: 52750Sstevel@tonic-gate case SO_DONTROUTE: 52760Sstevel@tonic-gate case SO_BROADCAST: 52770Sstevel@tonic-gate case SO_USELOOPBACK: 52780Sstevel@tonic-gate case SO_OOBINLINE: 52790Sstevel@tonic-gate case SO_DGRAM_ERRIND: 52800Sstevel@tonic-gate value = (so->so_options & option_name); 52810Sstevel@tonic-gate #ifdef DEBUG 52820Sstevel@tonic-gate if (value) { 52830Sstevel@tonic-gate dprintso(so, 1, 52840Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n", 52850Sstevel@tonic-gate option_name)); 52860Sstevel@tonic-gate } else { 52870Sstevel@tonic-gate dprintso(so, 1, 52880Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n", 52890Sstevel@tonic-gate option_name)); 52900Sstevel@tonic-gate } 52910Sstevel@tonic-gate #endif /* DEBUG */ 52920Sstevel@tonic-gate option = &value; 52930Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 52940Sstevel@tonic-gate 52950Sstevel@tonic-gate /* 52960Sstevel@tonic-gate * The following options are only returned by sockfs when the 52970Sstevel@tonic-gate * T_SVR4_OPTMGMT_REQ fails. 52980Sstevel@tonic-gate */ 52990Sstevel@tonic-gate case SO_LINGER: 53000Sstevel@tonic-gate option = &so->so_linger; 53010Sstevel@tonic-gate len = (t_uscalar_t)sizeof (struct linger); 53020Sstevel@tonic-gate break; 53030Sstevel@tonic-gate case SO_SNDBUF: { 53040Sstevel@tonic-gate ssize_t lvalue; 53050Sstevel@tonic-gate 53060Sstevel@tonic-gate /* 53070Sstevel@tonic-gate * If the option has not been set then get a default 53080Sstevel@tonic-gate * value from the read queue. This value is 53090Sstevel@tonic-gate * returned if the transport fails 53100Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ. 53110Sstevel@tonic-gate */ 53120Sstevel@tonic-gate lvalue = so->so_sndbuf; 53130Sstevel@tonic-gate if (lvalue == 0) { 53140Sstevel@tonic-gate mutex_exit(&so->so_lock); 53150Sstevel@tonic-gate (void) strqget(strvp2wq(SOTOV(so))->q_next, 53165240Snordmark QHIWAT, 0, &lvalue); 53170Sstevel@tonic-gate mutex_enter(&so->so_lock); 53180Sstevel@tonic-gate dprintso(so, 1, 53190Sstevel@tonic-gate ("got SO_SNDBUF %ld from q\n", lvalue)); 53200Sstevel@tonic-gate } 53210Sstevel@tonic-gate value = (int)lvalue; 53220Sstevel@tonic-gate option = &value; 53230Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_sndbuf); 53240Sstevel@tonic-gate break; 53250Sstevel@tonic-gate } 53260Sstevel@tonic-gate case SO_RCVBUF: { 53270Sstevel@tonic-gate ssize_t lvalue; 53280Sstevel@tonic-gate 53290Sstevel@tonic-gate /* 53300Sstevel@tonic-gate * If the option has not been set then get a default 53310Sstevel@tonic-gate * value from the read queue. This value is 53320Sstevel@tonic-gate * returned if the transport fails 53330Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ. 53340Sstevel@tonic-gate * 53350Sstevel@tonic-gate * XXX If SO_RCVBUF has been set and this is an 53360Sstevel@tonic-gate * XPG 4.2 application then do not ask the transport 53370Sstevel@tonic-gate * since the transport might adjust the value and not 53380Sstevel@tonic-gate * return exactly what was set by the application. 53390Sstevel@tonic-gate * For non-XPG 4.2 application we return the value 53400Sstevel@tonic-gate * that the transport is actually using. 53410Sstevel@tonic-gate */ 53420Sstevel@tonic-gate lvalue = so->so_rcvbuf; 53430Sstevel@tonic-gate if (lvalue == 0) { 53440Sstevel@tonic-gate mutex_exit(&so->so_lock); 53450Sstevel@tonic-gate (void) strqget(RD(strvp2wq(SOTOV(so))), 53465240Snordmark QHIWAT, 0, &lvalue); 53470Sstevel@tonic-gate mutex_enter(&so->so_lock); 53480Sstevel@tonic-gate dprintso(so, 1, 53490Sstevel@tonic-gate ("got SO_RCVBUF %ld from q\n", lvalue)); 53500Sstevel@tonic-gate } else if (flags & _SOGETSOCKOPT_XPG4_2) { 53510Sstevel@tonic-gate value = (int)lvalue; 53520Sstevel@tonic-gate option = &value; 53530Sstevel@tonic-gate goto copyout; /* skip asking transport */ 53540Sstevel@tonic-gate } 53550Sstevel@tonic-gate value = (int)lvalue; 53560Sstevel@tonic-gate option = &value; 53570Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_rcvbuf); 53580Sstevel@tonic-gate break; 53590Sstevel@tonic-gate } 53603388Skcpoon case SO_DOMAIN: 53613388Skcpoon value = so->so_family; 53623388Skcpoon option = &value; 53633388Skcpoon goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 53643388Skcpoon 53650Sstevel@tonic-gate #ifdef notyet 53660Sstevel@tonic-gate /* 53670Sstevel@tonic-gate * We do not implement the semantics of these options 53680Sstevel@tonic-gate * thus we shouldn't implement the options either. 53690Sstevel@tonic-gate */ 53700Sstevel@tonic-gate case SO_SNDLOWAT: 53710Sstevel@tonic-gate value = so->so_sndlowat; 53720Sstevel@tonic-gate option = &value; 53730Sstevel@tonic-gate break; 53740Sstevel@tonic-gate case SO_RCVLOWAT: 53750Sstevel@tonic-gate value = so->so_rcvlowat; 53760Sstevel@tonic-gate option = &value; 53770Sstevel@tonic-gate break; 53788348SEric.Yu@Sun.COM #endif /* notyet */ 53790Sstevel@tonic-gate case SO_SNDTIMEO: 53808348SEric.Yu@Sun.COM case SO_RCVTIMEO: { 53818348SEric.Yu@Sun.COM clock_t val; 53828489Sshenjian 53838348SEric.Yu@Sun.COM if (option_name == SO_RCVTIMEO) 53848348SEric.Yu@Sun.COM val = drv_hztousec(so->so_rcvtimeo); 53858348SEric.Yu@Sun.COM else 53868348SEric.Yu@Sun.COM val = drv_hztousec(so->so_sndtimeo); 53878348SEric.Yu@Sun.COM tmo_val.tv_sec = val / (1000 * 1000); 53888348SEric.Yu@Sun.COM tmo_val.tv_usec = val % (1000 * 1000); 5389*8575Sshenjian if (get_udatamodel() == DATAMODEL_NONE || 5390*8575Sshenjian get_udatamodel() == DATAMODEL_NATIVE) { 53918489Sshenjian option = &tmo_val; 53928489Sshenjian len = sizeof (struct timeval); 53938489Sshenjian } else { 53948489Sshenjian TIMEVAL_TO_TIMEVAL32(&tmo_val32, &tmo_val); 53958489Sshenjian option = &tmo_val32; 53968489Sshenjian len = sizeof (struct timeval32); 53978489Sshenjian } 53980Sstevel@tonic-gate break; 53998348SEric.Yu@Sun.COM } 54008348SEric.Yu@Sun.COM case SO_SND_BUFINFO: { 54018348SEric.Yu@Sun.COM snd_bufinfo.sbi_wroff = 54028348SEric.Yu@Sun.COM (so->so_proto_props).sopp_wroff; 54038348SEric.Yu@Sun.COM snd_bufinfo.sbi_maxblk = 54048348SEric.Yu@Sun.COM (so->so_proto_props).sopp_maxblk; 54058348SEric.Yu@Sun.COM snd_bufinfo.sbi_maxpsz = 54068348SEric.Yu@Sun.COM (so->so_proto_props).sopp_maxpsz; 54078348SEric.Yu@Sun.COM snd_bufinfo.sbi_tail = 54088348SEric.Yu@Sun.COM (so->so_proto_props).sopp_tail; 54098348SEric.Yu@Sun.COM option = &snd_bufinfo; 54108348SEric.Yu@Sun.COM len = (t_uscalar_t)sizeof (struct so_snd_bufinfo); 54110Sstevel@tonic-gate break; 54128348SEric.Yu@Sun.COM } 54130Sstevel@tonic-gate } 54140Sstevel@tonic-gate } 54150Sstevel@tonic-gate 54160Sstevel@tonic-gate mutex_exit(&so->so_lock); 54170Sstevel@tonic-gate 54180Sstevel@tonic-gate /* Send request */ 54190Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 54200Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_CHECK; 54210Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen); 54220Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 54230Sstevel@tonic-gate 54240Sstevel@tonic-gate oh.level = level; 54250Sstevel@tonic-gate oh.name = option_name; 54260Sstevel@tonic-gate oh.len = maxlen; 54270Sstevel@tonic-gate 54280Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 54290Sstevel@tonic-gate &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP); 54300Sstevel@tonic-gate /* Let option management work in the presence of data flow control */ 54310Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 54325240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 54330Sstevel@tonic-gate mp = NULL; 54340Sstevel@tonic-gate mutex_enter(&so->so_lock); 54350Sstevel@tonic-gate if (error) { 54360Sstevel@tonic-gate eprintsoline(so, error); 54370Sstevel@tonic-gate goto done2; 54380Sstevel@tonic-gate } 54390Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 54400Sstevel@tonic-gate (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0); 54410Sstevel@tonic-gate if (error) { 54420Sstevel@tonic-gate if (option != NULL) { 54430Sstevel@tonic-gate /* We have a fallback value */ 54440Sstevel@tonic-gate error = 0; 54450Sstevel@tonic-gate goto copyout; 54460Sstevel@tonic-gate } 54470Sstevel@tonic-gate eprintsoline(so, error); 54480Sstevel@tonic-gate goto done2; 54490Sstevel@tonic-gate } 54500Sstevel@tonic-gate ASSERT(mp); 54510Sstevel@tonic-gate optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr; 54520Sstevel@tonic-gate opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset, 54535240Snordmark optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE); 54540Sstevel@tonic-gate if (opt_res == NULL) { 54550Sstevel@tonic-gate if (option != NULL) { 54560Sstevel@tonic-gate /* We have a fallback value */ 54570Sstevel@tonic-gate error = 0; 54580Sstevel@tonic-gate goto copyout; 54590Sstevel@tonic-gate } 54600Sstevel@tonic-gate error = EPROTO; 54610Sstevel@tonic-gate eprintsoline(so, error); 54620Sstevel@tonic-gate goto done; 54630Sstevel@tonic-gate } 54640Sstevel@tonic-gate option = &opt_res[1]; 54650Sstevel@tonic-gate 54660Sstevel@tonic-gate /* check to ensure that the option is within bounds */ 54670Sstevel@tonic-gate if (((uintptr_t)option + opt_res->len < (uintptr_t)option) || 54685240Snordmark (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) { 54690Sstevel@tonic-gate if (option != NULL) { 54700Sstevel@tonic-gate /* We have a fallback value */ 54710Sstevel@tonic-gate error = 0; 54720Sstevel@tonic-gate goto copyout; 54730Sstevel@tonic-gate } 54740Sstevel@tonic-gate error = EPROTO; 54750Sstevel@tonic-gate eprintsoline(so, error); 54760Sstevel@tonic-gate goto done; 54770Sstevel@tonic-gate } 54780Sstevel@tonic-gate 54790Sstevel@tonic-gate len = opt_res->len; 54800Sstevel@tonic-gate 54810Sstevel@tonic-gate copyout: { 54820Sstevel@tonic-gate t_uscalar_t size = MIN(len, maxlen); 54830Sstevel@tonic-gate bcopy(option, optval, size); 54840Sstevel@tonic-gate bcopy(&size, optlenp, sizeof (size)); 54850Sstevel@tonic-gate } 54860Sstevel@tonic-gate done: 54870Sstevel@tonic-gate freemsg(mp); 54880Sstevel@tonic-gate done2: 54890Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 54900Sstevel@tonic-gate mutex_exit(&so->so_lock); 54918348SEric.Yu@Sun.COM 54920Sstevel@tonic-gate return (error); 54930Sstevel@tonic-gate } 54940Sstevel@tonic-gate 54950Sstevel@tonic-gate /* 54960Sstevel@tonic-gate * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ. 54970Sstevel@tonic-gate * SOL_SOCKET options are also recorded in the sonode. A setsockopt for 54980Sstevel@tonic-gate * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails - 54990Sstevel@tonic-gate * setsockopt has to work even if the transport does not support the option. 55000Sstevel@tonic-gate */ 55018348SEric.Yu@Sun.COM /* ARGSUSED */ 55020Sstevel@tonic-gate int 55030Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name, 55048348SEric.Yu@Sun.COM const void *optval, t_uscalar_t optlen, struct cred *cr) 55050Sstevel@tonic-gate { 55060Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req; 55070Sstevel@tonic-gate struct opthdr oh; 55080Sstevel@tonic-gate mblk_t *mp; 55090Sstevel@tonic-gate int error = 0; 55100Sstevel@tonic-gate boolean_t handled = B_FALSE; 55110Sstevel@tonic-gate 55120Sstevel@tonic-gate dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n", 55137240Srh87107 (void *)so, level, option_name, optval, optlen, 55145240Snordmark pr_state(so->so_state, so->so_mode))); 55150Sstevel@tonic-gate 55160Sstevel@tonic-gate /* X/Open requires this check */ 55170Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 55180Sstevel@tonic-gate if (xnet_check_print) 55190Sstevel@tonic-gate printf("sockfs: X/Open setsockopt check => EINVAL\n"); 55200Sstevel@tonic-gate return (EINVAL); 55210Sstevel@tonic-gate } 55220Sstevel@tonic-gate 55230Sstevel@tonic-gate mutex_enter(&so->so_lock); 55240Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 55250Sstevel@tonic-gate mutex_exit(&so->so_lock); 55260Sstevel@tonic-gate 55270Sstevel@tonic-gate /* 55280Sstevel@tonic-gate * For SOCKET or TCP level options, try to set it here itself 55290Sstevel@tonic-gate * provided socket has not been popped and we know the tcp 55300Sstevel@tonic-gate * structure (stored in so_priv). 55310Sstevel@tonic-gate */ 55320Sstevel@tonic-gate if ((level == SOL_SOCKET || level == IPPROTO_TCP) && 55330Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6) && 55348348SEric.Yu@Sun.COM (so->so_version == SOV_SOCKSTREAM) && 55358348SEric.Yu@Sun.COM (so->so_proto_handle != NULL)) { 55368348SEric.Yu@Sun.COM tcp_t *tcp = (tcp_t *)so->so_proto_handle; 55370Sstevel@tonic-gate boolean_t onoff; 55380Sstevel@tonic-gate 55390Sstevel@tonic-gate #define intvalue (*(int32_t *)optval) 55400Sstevel@tonic-gate 55410Sstevel@tonic-gate switch (level) { 55420Sstevel@tonic-gate case SOL_SOCKET: 55430Sstevel@tonic-gate switch (option_name) { /* Check length param */ 55440Sstevel@tonic-gate case SO_DEBUG: 55450Sstevel@tonic-gate case SO_REUSEADDR: 55460Sstevel@tonic-gate case SO_DONTROUTE: 55470Sstevel@tonic-gate case SO_BROADCAST: 55480Sstevel@tonic-gate case SO_USELOOPBACK: 55490Sstevel@tonic-gate case SO_OOBINLINE: 55500Sstevel@tonic-gate case SO_DGRAM_ERRIND: 55510Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 55520Sstevel@tonic-gate error = EINVAL; 55530Sstevel@tonic-gate eprintsoline(so, error); 55540Sstevel@tonic-gate mutex_enter(&so->so_lock); 55550Sstevel@tonic-gate goto done2; 55560Sstevel@tonic-gate } 55570Sstevel@tonic-gate ASSERT(optval); 55580Sstevel@tonic-gate onoff = intvalue != 0; 55590Sstevel@tonic-gate handled = B_TRUE; 55600Sstevel@tonic-gate break; 55618348SEric.Yu@Sun.COM case SO_SNDTIMEO: 55628348SEric.Yu@Sun.COM case SO_RCVTIMEO: 5563*8575Sshenjian if (get_udatamodel() == DATAMODEL_NONE || 5564*8575Sshenjian get_udatamodel() == DATAMODEL_NATIVE) { 55658489Sshenjian if (optlen != 55668489Sshenjian sizeof (struct timeval)) { 55678489Sshenjian error = EINVAL; 55688489Sshenjian eprintsoline(so, error); 55698489Sshenjian mutex_enter(&so->so_lock); 55708489Sshenjian goto done2; 55718489Sshenjian } 55728489Sshenjian } else { 55738489Sshenjian if (optlen != 55748489Sshenjian sizeof (struct timeval32)) { 55758489Sshenjian error = EINVAL; 55768489Sshenjian eprintsoline(so, error); 55778489Sshenjian mutex_enter(&so->so_lock); 55788489Sshenjian goto done2; 55798489Sshenjian } 55808348SEric.Yu@Sun.COM } 55818348SEric.Yu@Sun.COM ASSERT(optval); 55828348SEric.Yu@Sun.COM handled = B_TRUE; 55838348SEric.Yu@Sun.COM break; 55840Sstevel@tonic-gate case SO_LINGER: 55850Sstevel@tonic-gate if (optlen != 55860Sstevel@tonic-gate (t_uscalar_t)sizeof (struct linger)) { 55870Sstevel@tonic-gate error = EINVAL; 55880Sstevel@tonic-gate eprintsoline(so, error); 55890Sstevel@tonic-gate mutex_enter(&so->so_lock); 55900Sstevel@tonic-gate goto done2; 55910Sstevel@tonic-gate } 55920Sstevel@tonic-gate ASSERT(optval); 55930Sstevel@tonic-gate handled = B_TRUE; 55940Sstevel@tonic-gate break; 55950Sstevel@tonic-gate } 55960Sstevel@tonic-gate 55970Sstevel@tonic-gate switch (option_name) { /* Do actions */ 55980Sstevel@tonic-gate case SO_LINGER: { 55990Sstevel@tonic-gate struct linger *lgr = (struct linger *)optval; 56000Sstevel@tonic-gate 56010Sstevel@tonic-gate if (lgr->l_onoff) { 56020Sstevel@tonic-gate tcp->tcp_linger = 1; 56030Sstevel@tonic-gate tcp->tcp_lingertime = lgr->l_linger; 56040Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER; 56050Sstevel@tonic-gate so->so_options |= SO_LINGER; 56060Sstevel@tonic-gate } else { 56070Sstevel@tonic-gate tcp->tcp_linger = 0; 56080Sstevel@tonic-gate tcp->tcp_lingertime = 0; 56090Sstevel@tonic-gate so->so_linger.l_onoff = 0; 56100Sstevel@tonic-gate so->so_options &= ~SO_LINGER; 56110Sstevel@tonic-gate } 56120Sstevel@tonic-gate so->so_linger.l_linger = lgr->l_linger; 56130Sstevel@tonic-gate handled = B_TRUE; 56140Sstevel@tonic-gate break; 56150Sstevel@tonic-gate } 56168489Sshenjian case SO_SNDTIMEO: 56178489Sshenjian case SO_RCVTIMEO: { 56188489Sshenjian struct timeval tl; 56198489Sshenjian clock_t val; 56208489Sshenjian 5621*8575Sshenjian if (get_udatamodel() == DATAMODEL_NONE || 5622*8575Sshenjian get_udatamodel() == DATAMODEL_NATIVE) 56238489Sshenjian bcopy(&tl, (struct timeval *)optval, 56248489Sshenjian sizeof (struct timeval)); 56258489Sshenjian else 56268489Sshenjian TIMEVAL32_TO_TIMEVAL(&tl, 56278489Sshenjian (struct timeval32 *)optval); 56288489Sshenjian val = tl.tv_sec * 1000 * 1000 + tl.tv_usec; 56298489Sshenjian if (option_name == SO_RCVTIMEO) 56308489Sshenjian so->so_rcvtimeo = drv_usectohz(val); 56318489Sshenjian else 56328489Sshenjian so->so_sndtimeo = drv_usectohz(val); 56338489Sshenjian break; 56348489Sshenjian } 56358489Sshenjian 56360Sstevel@tonic-gate case SO_DEBUG: 56370Sstevel@tonic-gate tcp->tcp_debug = onoff; 56380Sstevel@tonic-gate #ifdef SOCK_TEST 56390Sstevel@tonic-gate if (intvalue & 2) 56400Sstevel@tonic-gate sock_test_timelimit = 10 * hz; 56410Sstevel@tonic-gate else 56420Sstevel@tonic-gate sock_test_timelimit = 0; 56430Sstevel@tonic-gate 56440Sstevel@tonic-gate if (intvalue & 4) 56450Sstevel@tonic-gate do_useracc = 0; 56460Sstevel@tonic-gate else 56470Sstevel@tonic-gate do_useracc = 1; 56480Sstevel@tonic-gate #endif /* SOCK_TEST */ 56490Sstevel@tonic-gate break; 56500Sstevel@tonic-gate case SO_DONTROUTE: 56510Sstevel@tonic-gate /* 56520Sstevel@tonic-gate * SO_DONTROUTE, SO_USELOOPBACK and 56530Sstevel@tonic-gate * SO_BROADCAST are only of interest to IP. 56540Sstevel@tonic-gate * We track them here only so 56550Sstevel@tonic-gate * that we can report their current value. 56560Sstevel@tonic-gate */ 56570Sstevel@tonic-gate tcp->tcp_dontroute = onoff; 56580Sstevel@tonic-gate if (onoff) 56590Sstevel@tonic-gate so->so_options |= option_name; 56600Sstevel@tonic-gate else 56610Sstevel@tonic-gate so->so_options &= ~option_name; 56620Sstevel@tonic-gate break; 56630Sstevel@tonic-gate case SO_USELOOPBACK: 56640Sstevel@tonic-gate tcp->tcp_useloopback = onoff; 56650Sstevel@tonic-gate if (onoff) 56660Sstevel@tonic-gate so->so_options |= option_name; 56670Sstevel@tonic-gate else 56680Sstevel@tonic-gate so->so_options &= ~option_name; 56690Sstevel@tonic-gate break; 56700Sstevel@tonic-gate case SO_BROADCAST: 56710Sstevel@tonic-gate tcp->tcp_broadcast = onoff; 56720Sstevel@tonic-gate if (onoff) 56730Sstevel@tonic-gate so->so_options |= option_name; 56740Sstevel@tonic-gate else 56750Sstevel@tonic-gate so->so_options &= ~option_name; 56760Sstevel@tonic-gate break; 56770Sstevel@tonic-gate case SO_REUSEADDR: 56780Sstevel@tonic-gate tcp->tcp_reuseaddr = onoff; 56790Sstevel@tonic-gate if (onoff) 56800Sstevel@tonic-gate so->so_options |= option_name; 56810Sstevel@tonic-gate else 56820Sstevel@tonic-gate so->so_options &= ~option_name; 56830Sstevel@tonic-gate break; 56840Sstevel@tonic-gate case SO_OOBINLINE: 56850Sstevel@tonic-gate tcp->tcp_oobinline = onoff; 56860Sstevel@tonic-gate if (onoff) 56870Sstevel@tonic-gate so->so_options |= option_name; 56880Sstevel@tonic-gate else 56890Sstevel@tonic-gate so->so_options &= ~option_name; 56900Sstevel@tonic-gate break; 56910Sstevel@tonic-gate case SO_DGRAM_ERRIND: 56920Sstevel@tonic-gate tcp->tcp_dgram_errind = onoff; 56930Sstevel@tonic-gate if (onoff) 56940Sstevel@tonic-gate so->so_options |= option_name; 56950Sstevel@tonic-gate else 56960Sstevel@tonic-gate so->so_options &= ~option_name; 56970Sstevel@tonic-gate break; 56980Sstevel@tonic-gate } 56990Sstevel@tonic-gate break; 57000Sstevel@tonic-gate case IPPROTO_TCP: 57010Sstevel@tonic-gate switch (option_name) { 57020Sstevel@tonic-gate case TCP_NODELAY: 57030Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 57040Sstevel@tonic-gate error = EINVAL; 57050Sstevel@tonic-gate eprintsoline(so, error); 57060Sstevel@tonic-gate mutex_enter(&so->so_lock); 57070Sstevel@tonic-gate goto done2; 57080Sstevel@tonic-gate } 57090Sstevel@tonic-gate ASSERT(optval); 57100Sstevel@tonic-gate tcp->tcp_naglim = intvalue ? 1 : tcp->tcp_mss; 57110Sstevel@tonic-gate handled = B_TRUE; 57120Sstevel@tonic-gate break; 57130Sstevel@tonic-gate } 57140Sstevel@tonic-gate break; 57150Sstevel@tonic-gate default: 57160Sstevel@tonic-gate handled = B_FALSE; 57170Sstevel@tonic-gate break; 57180Sstevel@tonic-gate } 57190Sstevel@tonic-gate } 57200Sstevel@tonic-gate 57210Sstevel@tonic-gate if (handled) { 57220Sstevel@tonic-gate mutex_enter(&so->so_lock); 57230Sstevel@tonic-gate goto done2; 57240Sstevel@tonic-gate } 57250Sstevel@tonic-gate 57260Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 57270Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_NEGOTIATE; 57280Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen; 57290Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 57300Sstevel@tonic-gate 57310Sstevel@tonic-gate oh.level = level; 57320Sstevel@tonic-gate oh.name = option_name; 57330Sstevel@tonic-gate oh.len = optlen; 57340Sstevel@tonic-gate 57350Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 57360Sstevel@tonic-gate &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP); 57370Sstevel@tonic-gate /* Let option management work in the presence of data flow control */ 57380Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 57395240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 57400Sstevel@tonic-gate mp = NULL; 57410Sstevel@tonic-gate mutex_enter(&so->so_lock); 57420Sstevel@tonic-gate if (error) { 57430Sstevel@tonic-gate eprintsoline(so, error); 57448348SEric.Yu@Sun.COM goto done2; 57450Sstevel@tonic-gate } 57460Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 57470Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0); 57480Sstevel@tonic-gate if (error) { 57490Sstevel@tonic-gate eprintsoline(so, error); 57500Sstevel@tonic-gate goto done; 57510Sstevel@tonic-gate } 57520Sstevel@tonic-gate ASSERT(mp); 57530Sstevel@tonic-gate /* No need to verify T_optmgmt_ack */ 57540Sstevel@tonic-gate freemsg(mp); 57550Sstevel@tonic-gate done: 57560Sstevel@tonic-gate /* 57570Sstevel@tonic-gate * Check for SOL_SOCKET options and record their values. 57580Sstevel@tonic-gate * If we know about a SOL_SOCKET parameter and the transport 57590Sstevel@tonic-gate * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or 57600Sstevel@tonic-gate * EPROTO) we let the setsockopt succeed. 57610Sstevel@tonic-gate */ 57620Sstevel@tonic-gate if (level == SOL_SOCKET) { 57630Sstevel@tonic-gate /* Check parameters */ 57640Sstevel@tonic-gate switch (option_name) { 57650Sstevel@tonic-gate case SO_DEBUG: 57660Sstevel@tonic-gate case SO_REUSEADDR: 57670Sstevel@tonic-gate case SO_KEEPALIVE: 57680Sstevel@tonic-gate case SO_DONTROUTE: 57690Sstevel@tonic-gate case SO_BROADCAST: 57700Sstevel@tonic-gate case SO_USELOOPBACK: 57710Sstevel@tonic-gate case SO_OOBINLINE: 57720Sstevel@tonic-gate case SO_SNDBUF: 57730Sstevel@tonic-gate case SO_RCVBUF: 57740Sstevel@tonic-gate #ifdef notyet 57750Sstevel@tonic-gate case SO_SNDLOWAT: 57760Sstevel@tonic-gate case SO_RCVLOWAT: 57770Sstevel@tonic-gate #endif /* notyet */ 57780Sstevel@tonic-gate case SO_DGRAM_ERRIND: 57790Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 57800Sstevel@tonic-gate error = EINVAL; 57810Sstevel@tonic-gate eprintsoline(so, error); 57820Sstevel@tonic-gate goto done2; 57830Sstevel@tonic-gate } 57840Sstevel@tonic-gate ASSERT(optval); 57850Sstevel@tonic-gate handled = B_TRUE; 57860Sstevel@tonic-gate break; 57878348SEric.Yu@Sun.COM case SO_SNDTIMEO: 57888348SEric.Yu@Sun.COM case SO_RCVTIMEO: 5789*8575Sshenjian if (get_udatamodel() == DATAMODEL_NONE || 5790*8575Sshenjian get_udatamodel() == DATAMODEL_NATIVE) { 57918489Sshenjian if (optlen != sizeof (struct timeval)) { 57928489Sshenjian error = EINVAL; 57938489Sshenjian eprintsoline(so, error); 57948489Sshenjian goto done2; 57958489Sshenjian } 57968489Sshenjian } else { 57978489Sshenjian if (optlen != sizeof (struct timeval32)) { 57988489Sshenjian error = EINVAL; 57998489Sshenjian eprintsoline(so, error); 58008489Sshenjian goto done2; 58018489Sshenjian } 58028348SEric.Yu@Sun.COM } 58038348SEric.Yu@Sun.COM ASSERT(optval); 58048348SEric.Yu@Sun.COM handled = B_TRUE; 58058348SEric.Yu@Sun.COM break; 58060Sstevel@tonic-gate case SO_LINGER: 58070Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (struct linger)) { 58080Sstevel@tonic-gate error = EINVAL; 58090Sstevel@tonic-gate eprintsoline(so, error); 58100Sstevel@tonic-gate goto done2; 58110Sstevel@tonic-gate } 58120Sstevel@tonic-gate ASSERT(optval); 58130Sstevel@tonic-gate handled = B_TRUE; 58140Sstevel@tonic-gate break; 58150Sstevel@tonic-gate } 58160Sstevel@tonic-gate 58170Sstevel@tonic-gate #define intvalue (*(int32_t *)optval) 58180Sstevel@tonic-gate 58190Sstevel@tonic-gate switch (option_name) { 58200Sstevel@tonic-gate case SO_TYPE: 58210Sstevel@tonic-gate case SO_ERROR: 58220Sstevel@tonic-gate case SO_ACCEPTCONN: 58230Sstevel@tonic-gate /* Can't be set */ 58240Sstevel@tonic-gate error = ENOPROTOOPT; 58250Sstevel@tonic-gate goto done2; 58260Sstevel@tonic-gate case SO_LINGER: { 58270Sstevel@tonic-gate struct linger *l = (struct linger *)optval; 58280Sstevel@tonic-gate 58290Sstevel@tonic-gate so->so_linger.l_linger = l->l_linger; 58300Sstevel@tonic-gate if (l->l_onoff) { 58310Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER; 58320Sstevel@tonic-gate so->so_options |= SO_LINGER; 58330Sstevel@tonic-gate } else { 58340Sstevel@tonic-gate so->so_linger.l_onoff = 0; 58350Sstevel@tonic-gate so->so_options &= ~SO_LINGER; 58360Sstevel@tonic-gate } 58370Sstevel@tonic-gate break; 58380Sstevel@tonic-gate } 58390Sstevel@tonic-gate 58400Sstevel@tonic-gate case SO_DEBUG: 58410Sstevel@tonic-gate #ifdef SOCK_TEST 58420Sstevel@tonic-gate if (intvalue & 2) 58430Sstevel@tonic-gate sock_test_timelimit = 10 * hz; 58440Sstevel@tonic-gate else 58450Sstevel@tonic-gate sock_test_timelimit = 0; 58460Sstevel@tonic-gate 58470Sstevel@tonic-gate if (intvalue & 4) 58480Sstevel@tonic-gate do_useracc = 0; 58490Sstevel@tonic-gate else 58500Sstevel@tonic-gate do_useracc = 1; 58510Sstevel@tonic-gate #endif /* SOCK_TEST */ 58520Sstevel@tonic-gate /* FALLTHRU */ 58530Sstevel@tonic-gate case SO_REUSEADDR: 58540Sstevel@tonic-gate case SO_KEEPALIVE: 58550Sstevel@tonic-gate case SO_DONTROUTE: 58560Sstevel@tonic-gate case SO_BROADCAST: 58570Sstevel@tonic-gate case SO_USELOOPBACK: 58580Sstevel@tonic-gate case SO_OOBINLINE: 58590Sstevel@tonic-gate case SO_DGRAM_ERRIND: 58600Sstevel@tonic-gate if (intvalue != 0) { 58610Sstevel@tonic-gate dprintso(so, 1, 58628348SEric.Yu@Sun.COM ("socket_setsockopt: setting 0x%x\n", 58635240Snordmark option_name)); 58640Sstevel@tonic-gate so->so_options |= option_name; 58650Sstevel@tonic-gate } else { 58660Sstevel@tonic-gate dprintso(so, 1, 58678348SEric.Yu@Sun.COM ("socket_setsockopt: clearing 0x%x\n", 58685240Snordmark option_name)); 58690Sstevel@tonic-gate so->so_options &= ~option_name; 58700Sstevel@tonic-gate } 58710Sstevel@tonic-gate break; 58720Sstevel@tonic-gate /* 58730Sstevel@tonic-gate * The following options are only returned by us when the 58748348SEric.Yu@Sun.COM * transport layer fails. 58750Sstevel@tonic-gate * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs 58760Sstevel@tonic-gate * since the transport might adjust the value and not 58770Sstevel@tonic-gate * return exactly what was set by the application. 58780Sstevel@tonic-gate */ 58790Sstevel@tonic-gate case SO_SNDBUF: 58800Sstevel@tonic-gate so->so_sndbuf = intvalue; 58810Sstevel@tonic-gate break; 58820Sstevel@tonic-gate case SO_RCVBUF: 58830Sstevel@tonic-gate so->so_rcvbuf = intvalue; 58840Sstevel@tonic-gate break; 58858348SEric.Yu@Sun.COM case SO_RCVPSH: 58868348SEric.Yu@Sun.COM so->so_rcv_timer_interval = intvalue; 58878348SEric.Yu@Sun.COM break; 58880Sstevel@tonic-gate #ifdef notyet 58890Sstevel@tonic-gate /* 58900Sstevel@tonic-gate * We do not implement the semantics of these options 58910Sstevel@tonic-gate * thus we shouldn't implement the options either. 58920Sstevel@tonic-gate */ 58930Sstevel@tonic-gate case SO_SNDLOWAT: 58940Sstevel@tonic-gate so->so_sndlowat = intvalue; 58950Sstevel@tonic-gate break; 58960Sstevel@tonic-gate case SO_RCVLOWAT: 58970Sstevel@tonic-gate so->so_rcvlowat = intvalue; 58980Sstevel@tonic-gate break; 58998348SEric.Yu@Sun.COM #endif /* notyet */ 59000Sstevel@tonic-gate case SO_SNDTIMEO: 59018348SEric.Yu@Sun.COM case SO_RCVTIMEO: { 59028489Sshenjian struct timeval tl; 59038489Sshenjian clock_t val; 59048489Sshenjian 5905*8575Sshenjian if (get_udatamodel() == DATAMODEL_NONE || 5906*8575Sshenjian get_udatamodel() == DATAMODEL_NATIVE) 59078489Sshenjian bcopy(&tl, (struct timeval *)optval, 59088489Sshenjian sizeof (struct timeval)); 59098489Sshenjian else 59108489Sshenjian TIMEVAL32_TO_TIMEVAL(&tl, 59118489Sshenjian (struct timeval32 *)optval); 59128489Sshenjian val = tl.tv_sec * 1000 * 1000 + tl.tv_usec; 59138348SEric.Yu@Sun.COM if (option_name == SO_RCVTIMEO) 59148348SEric.Yu@Sun.COM so->so_rcvtimeo = drv_usectohz(val); 59158348SEric.Yu@Sun.COM else 59168348SEric.Yu@Sun.COM so->so_sndtimeo = drv_usectohz(val); 59170Sstevel@tonic-gate break; 59188348SEric.Yu@Sun.COM } 59190Sstevel@tonic-gate } 59200Sstevel@tonic-gate #undef intvalue 59210Sstevel@tonic-gate 59220Sstevel@tonic-gate if (error) { 59230Sstevel@tonic-gate if ((error == ENOPROTOOPT || error == EPROTO || 59240Sstevel@tonic-gate error == EINVAL) && handled) { 59250Sstevel@tonic-gate dprintso(so, 1, 59260Sstevel@tonic-gate ("setsockopt: ignoring error %d for 0x%x\n", 59270Sstevel@tonic-gate error, option_name)); 59280Sstevel@tonic-gate error = 0; 59290Sstevel@tonic-gate } 59300Sstevel@tonic-gate } 59310Sstevel@tonic-gate } 59320Sstevel@tonic-gate done2: 59330Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 59340Sstevel@tonic-gate mutex_exit(&so->so_lock); 59350Sstevel@tonic-gate return (error); 59360Sstevel@tonic-gate } 59378348SEric.Yu@Sun.COM 59388427SAnders.Persson@Sun.COM /* 59398427SAnders.Persson@Sun.COM * sotpi_close() is called when the last open reference goes away. 59408427SAnders.Persson@Sun.COM */ 59418348SEric.Yu@Sun.COM /* ARGSUSED */ 59428348SEric.Yu@Sun.COM int 59438348SEric.Yu@Sun.COM sotpi_close(struct sonode *so, int flag, struct cred *cr) 59448348SEric.Yu@Sun.COM { 59458348SEric.Yu@Sun.COM struct vnode *vp = SOTOV(so); 59468348SEric.Yu@Sun.COM dev_t dev; 59478348SEric.Yu@Sun.COM int error = 0; 59488348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 59498348SEric.Yu@Sun.COM 59508348SEric.Yu@Sun.COM dprintso(so, 1, ("sotpi_close(%p, %x) %s\n", 59518348SEric.Yu@Sun.COM (void *)vp, flag, pr_state(so->so_state, so->so_mode))); 59528348SEric.Yu@Sun.COM 59538348SEric.Yu@Sun.COM dev = sti->sti_dev; 59548348SEric.Yu@Sun.COM 59558348SEric.Yu@Sun.COM ASSERT(STREAMSTAB(getmajor(dev))); 59568348SEric.Yu@Sun.COM 59578348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 59588348SEric.Yu@Sun.COM so_lock_single(so); /* Set SOLOCKED */ 59598348SEric.Yu@Sun.COM 59608399SRao.Shoaib@Sun.COM ASSERT(so_verify_oobstate(so)); 59618399SRao.Shoaib@Sun.COM 59628348SEric.Yu@Sun.COM if (sti->sti_nl7c_flags & NL7C_ENABLED) { 59638348SEric.Yu@Sun.COM sti->sti_nl7c_flags = 0; 59648348SEric.Yu@Sun.COM nl7c_close(so); 59658348SEric.Yu@Sun.COM } 59668348SEric.Yu@Sun.COM 59678348SEric.Yu@Sun.COM if (vp->v_stream != NULL) { 59688348SEric.Yu@Sun.COM vnode_t *ux_vp; 59698348SEric.Yu@Sun.COM 59708348SEric.Yu@Sun.COM if (so->so_family == AF_UNIX) { 59718348SEric.Yu@Sun.COM /* Could avoid this when CANTSENDMORE for !dgram */ 59728348SEric.Yu@Sun.COM so_unix_close(so); 59738348SEric.Yu@Sun.COM } 59748348SEric.Yu@Sun.COM 59758348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 59768348SEric.Yu@Sun.COM /* 59778348SEric.Yu@Sun.COM * Disassemble the linkage from the AF_UNIX underlying file 59788348SEric.Yu@Sun.COM * system vnode to this socket (by atomically clearing 59798348SEric.Yu@Sun.COM * v_stream in vn_rele_stream) before strclose clears sd_vnode 59808348SEric.Yu@Sun.COM * and frees the stream head. 59818348SEric.Yu@Sun.COM */ 59828348SEric.Yu@Sun.COM if ((ux_vp = sti->sti_ux_bound_vp) != NULL) { 59838348SEric.Yu@Sun.COM ASSERT(ux_vp->v_stream); 59848348SEric.Yu@Sun.COM sti->sti_ux_bound_vp = NULL; 59858348SEric.Yu@Sun.COM vn_rele_stream(ux_vp); 59868348SEric.Yu@Sun.COM } 59878348SEric.Yu@Sun.COM if (so->so_family == AF_INET || so->so_family == AF_INET6) { 59888348SEric.Yu@Sun.COM strsetrwputdatahooks(SOTOV(so), NULL, NULL); 59898348SEric.Yu@Sun.COM if (sti->sti_kssl_ent != NULL) { 59908348SEric.Yu@Sun.COM kssl_release_ent(sti->sti_kssl_ent, so, 59918348SEric.Yu@Sun.COM sti->sti_kssl_type); 59928348SEric.Yu@Sun.COM sti->sti_kssl_ent = NULL; 59938348SEric.Yu@Sun.COM } 59948348SEric.Yu@Sun.COM if (sti->sti_kssl_ctx != NULL) { 59958348SEric.Yu@Sun.COM kssl_release_ctx(sti->sti_kssl_ctx); 59968348SEric.Yu@Sun.COM sti->sti_kssl_ctx = NULL; 59978348SEric.Yu@Sun.COM } 59988348SEric.Yu@Sun.COM sti->sti_kssl_type = KSSL_NO_PROXY; 59998348SEric.Yu@Sun.COM } 60008348SEric.Yu@Sun.COM error = strclose(vp, flag, cr); 60018348SEric.Yu@Sun.COM vp->v_stream = NULL; 60028348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 60038348SEric.Yu@Sun.COM } 60048348SEric.Yu@Sun.COM 60058348SEric.Yu@Sun.COM /* 60068348SEric.Yu@Sun.COM * Flush the T_DISCON_IND on sti_discon_ind_mp. 60078348SEric.Yu@Sun.COM */ 60088348SEric.Yu@Sun.COM so_flush_discon_ind(so); 60098348SEric.Yu@Sun.COM 60108348SEric.Yu@Sun.COM so_unlock_single(so, SOLOCKED); 60118348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 60128348SEric.Yu@Sun.COM 60138348SEric.Yu@Sun.COM /* 60148348SEric.Yu@Sun.COM * Needed for STREAMs. 60158348SEric.Yu@Sun.COM * Decrement the device driver's reference count for streams 60168348SEric.Yu@Sun.COM * opened via the clone dip. The driver was held in clone_open(). 60178348SEric.Yu@Sun.COM * The absence of clone_close() forces this asymmetry. 60188348SEric.Yu@Sun.COM */ 60198348SEric.Yu@Sun.COM if (so->so_flag & SOCLONE) 60208348SEric.Yu@Sun.COM ddi_rele_driver(getmajor(dev)); 60218348SEric.Yu@Sun.COM 60228348SEric.Yu@Sun.COM return (error); 60238348SEric.Yu@Sun.COM } 60248348SEric.Yu@Sun.COM 60258348SEric.Yu@Sun.COM static int 60268348SEric.Yu@Sun.COM sotpi_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode, 60278348SEric.Yu@Sun.COM struct cred *cr, int32_t *rvalp) 60288348SEric.Yu@Sun.COM { 60298348SEric.Yu@Sun.COM struct vnode *vp = SOTOV(so); 60308348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 60318348SEric.Yu@Sun.COM int error = 0; 60328348SEric.Yu@Sun.COM 60338348SEric.Yu@Sun.COM dprintso(so, 0, ("sotpi_ioctl: cmd 0x%x, arg 0x%lx, state %s\n", 60348348SEric.Yu@Sun.COM cmd, arg, pr_state(so->so_state, so->so_mode))); 60358348SEric.Yu@Sun.COM 60368348SEric.Yu@Sun.COM switch (cmd) { 60378477SRao.Shoaib@Sun.COM case SIOCSQPTR: 60388477SRao.Shoaib@Sun.COM /* 60398477SRao.Shoaib@Sun.COM * SIOCSQPTR is valid only when helper stream is created 60408477SRao.Shoaib@Sun.COM * by the protocol. 60418477SRao.Shoaib@Sun.COM */ 60428348SEric.Yu@Sun.COM case _I_INSERT: 60438348SEric.Yu@Sun.COM case _I_REMOVE: 60448348SEric.Yu@Sun.COM /* 60458348SEric.Yu@Sun.COM * Since there's no compelling reason to support these ioctls 60468348SEric.Yu@Sun.COM * on sockets, and doing so would increase the complexity 60478348SEric.Yu@Sun.COM * markedly, prevent it. 60488348SEric.Yu@Sun.COM */ 60498348SEric.Yu@Sun.COM return (EOPNOTSUPP); 60508348SEric.Yu@Sun.COM 60518348SEric.Yu@Sun.COM case I_FIND: 60528348SEric.Yu@Sun.COM case I_LIST: 60538348SEric.Yu@Sun.COM case I_LOOK: 60548348SEric.Yu@Sun.COM case I_POP: 60558348SEric.Yu@Sun.COM case I_PUSH: 60568348SEric.Yu@Sun.COM /* 60578348SEric.Yu@Sun.COM * To prevent races and inconsistencies between the actual 60588348SEric.Yu@Sun.COM * state of the stream and the state according to the sonode, 60598348SEric.Yu@Sun.COM * we serialize all operations which modify or operate on the 60608348SEric.Yu@Sun.COM * list of modules on the socket's stream. 60618348SEric.Yu@Sun.COM */ 60628348SEric.Yu@Sun.COM mutex_enter(&sti->sti_plumb_lock); 60638348SEric.Yu@Sun.COM error = socktpi_plumbioctl(vp, cmd, arg, mode, cr, rvalp); 60648348SEric.Yu@Sun.COM mutex_exit(&sti->sti_plumb_lock); 60658348SEric.Yu@Sun.COM return (error); 60668348SEric.Yu@Sun.COM 60678348SEric.Yu@Sun.COM default: 60688348SEric.Yu@Sun.COM if (so->so_version != SOV_STREAM) 60698348SEric.Yu@Sun.COM break; 60708348SEric.Yu@Sun.COM 60718348SEric.Yu@Sun.COM /* 60728348SEric.Yu@Sun.COM * The imaginary "sockmod" has been popped; act as a stream. 60738348SEric.Yu@Sun.COM */ 60748348SEric.Yu@Sun.COM return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 60758348SEric.Yu@Sun.COM } 60768348SEric.Yu@Sun.COM 60778348SEric.Yu@Sun.COM ASSERT(so->so_version != SOV_STREAM); 60788348SEric.Yu@Sun.COM 60798348SEric.Yu@Sun.COM /* 60808348SEric.Yu@Sun.COM * Process socket-specific ioctls. 60818348SEric.Yu@Sun.COM */ 60828348SEric.Yu@Sun.COM switch (cmd) { 60838348SEric.Yu@Sun.COM case FIONBIO: { 60848348SEric.Yu@Sun.COM int32_t value; 60858348SEric.Yu@Sun.COM 60868348SEric.Yu@Sun.COM if (so_copyin((void *)arg, &value, sizeof (int32_t), 60878348SEric.Yu@Sun.COM (mode & (int)FKIOCTL))) 60888348SEric.Yu@Sun.COM return (EFAULT); 60898348SEric.Yu@Sun.COM 60908348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 60918348SEric.Yu@Sun.COM if (value) { 60928348SEric.Yu@Sun.COM so->so_state |= SS_NDELAY; 60938348SEric.Yu@Sun.COM } else { 60948348SEric.Yu@Sun.COM so->so_state &= ~SS_NDELAY; 60958348SEric.Yu@Sun.COM } 60968348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 60978348SEric.Yu@Sun.COM return (0); 60988348SEric.Yu@Sun.COM } 60998348SEric.Yu@Sun.COM 61008348SEric.Yu@Sun.COM case FIOASYNC: { 61018348SEric.Yu@Sun.COM int32_t value; 61028348SEric.Yu@Sun.COM 61038348SEric.Yu@Sun.COM if (so_copyin((void *)arg, &value, sizeof (int32_t), 61048348SEric.Yu@Sun.COM (mode & (int)FKIOCTL))) 61058348SEric.Yu@Sun.COM return (EFAULT); 61068348SEric.Yu@Sun.COM 61078348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 61088348SEric.Yu@Sun.COM /* 61098348SEric.Yu@Sun.COM * SS_ASYNC flag not already set correctly? 61108348SEric.Yu@Sun.COM * (!value != !(so->so_state & SS_ASYNC)) 61118348SEric.Yu@Sun.COM * but some engineers find that too hard to read. 61128348SEric.Yu@Sun.COM */ 61138348SEric.Yu@Sun.COM if (value == 0 && (so->so_state & SS_ASYNC) != 0 || 61148348SEric.Yu@Sun.COM value != 0 && (so->so_state & SS_ASYNC) == 0) 61158348SEric.Yu@Sun.COM error = so_flip_async(so, vp, mode, cr); 61168348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 61178348SEric.Yu@Sun.COM return (error); 61188348SEric.Yu@Sun.COM } 61198348SEric.Yu@Sun.COM 61208348SEric.Yu@Sun.COM case SIOCSPGRP: 61218348SEric.Yu@Sun.COM case FIOSETOWN: { 61228348SEric.Yu@Sun.COM pid_t pgrp; 61238348SEric.Yu@Sun.COM 61248348SEric.Yu@Sun.COM if (so_copyin((void *)arg, &pgrp, sizeof (pid_t), 61258348SEric.Yu@Sun.COM (mode & (int)FKIOCTL))) 61268348SEric.Yu@Sun.COM return (EFAULT); 61278348SEric.Yu@Sun.COM 61288348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 61298348SEric.Yu@Sun.COM dprintso(so, 1, ("setown: new %d old %d\n", pgrp, so->so_pgrp)); 61308348SEric.Yu@Sun.COM /* Any change? */ 61318348SEric.Yu@Sun.COM if (pgrp != so->so_pgrp) 61328348SEric.Yu@Sun.COM error = so_set_siggrp(so, vp, pgrp, mode, cr); 61338348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 61348348SEric.Yu@Sun.COM return (error); 61358348SEric.Yu@Sun.COM } 61368348SEric.Yu@Sun.COM case SIOCGPGRP: 61378348SEric.Yu@Sun.COM case FIOGETOWN: 61388348SEric.Yu@Sun.COM if (so_copyout(&so->so_pgrp, (void *)arg, 61398348SEric.Yu@Sun.COM sizeof (pid_t), (mode & (int)FKIOCTL))) 61408348SEric.Yu@Sun.COM return (EFAULT); 61418348SEric.Yu@Sun.COM return (0); 61428348SEric.Yu@Sun.COM 61438348SEric.Yu@Sun.COM case SIOCATMARK: { 61448348SEric.Yu@Sun.COM int retval; 61458348SEric.Yu@Sun.COM uint_t so_state; 61468348SEric.Yu@Sun.COM 61478348SEric.Yu@Sun.COM /* 61488348SEric.Yu@Sun.COM * strwaitmark has a finite timeout after which it 61498348SEric.Yu@Sun.COM * returns -1 if the mark state is undetermined. 61508348SEric.Yu@Sun.COM * In order to avoid any race between the mark state 61518348SEric.Yu@Sun.COM * in sockfs and the mark state in the stream head this 61528348SEric.Yu@Sun.COM * routine loops until the mark state can be determined 61538348SEric.Yu@Sun.COM * (or the urgent data indication has been removed by some 61548348SEric.Yu@Sun.COM * other thread). 61558348SEric.Yu@Sun.COM */ 61568348SEric.Yu@Sun.COM do { 61578348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 61588348SEric.Yu@Sun.COM so_state = so->so_state; 61598348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 61608348SEric.Yu@Sun.COM if (so_state & SS_RCVATMARK) { 61618348SEric.Yu@Sun.COM retval = 1; 61628348SEric.Yu@Sun.COM } else if (!(so_state & SS_OOBPEND)) { 61638348SEric.Yu@Sun.COM /* 61648348SEric.Yu@Sun.COM * No SIGURG has been generated -- there is no 61658348SEric.Yu@Sun.COM * pending or present urgent data. Thus can't 61668348SEric.Yu@Sun.COM * possibly be at the mark. 61678348SEric.Yu@Sun.COM */ 61688348SEric.Yu@Sun.COM retval = 0; 61698348SEric.Yu@Sun.COM } else { 61708348SEric.Yu@Sun.COM /* 61718348SEric.Yu@Sun.COM * Have the stream head wait until there is 61728348SEric.Yu@Sun.COM * either some messages on the read queue, or 61738348SEric.Yu@Sun.COM * STRATMARK or STRNOTATMARK gets set. The 61748348SEric.Yu@Sun.COM * STRNOTATMARK flag is used so that the 61758348SEric.Yu@Sun.COM * transport can send up a MSGNOTMARKNEXT 61768348SEric.Yu@Sun.COM * M_DATA to indicate that it is not 61778348SEric.Yu@Sun.COM * at the mark and additional data is not about 61788348SEric.Yu@Sun.COM * to be send upstream. 61798348SEric.Yu@Sun.COM * 61808348SEric.Yu@Sun.COM * If the mark state is undetermined this will 61818348SEric.Yu@Sun.COM * return -1 and we will loop rechecking the 61828348SEric.Yu@Sun.COM * socket state. 61838348SEric.Yu@Sun.COM */ 61848348SEric.Yu@Sun.COM retval = strwaitmark(vp); 61858348SEric.Yu@Sun.COM } 61868348SEric.Yu@Sun.COM } while (retval == -1); 61878348SEric.Yu@Sun.COM 61888348SEric.Yu@Sun.COM if (so_copyout(&retval, (void *)arg, sizeof (int), 61898348SEric.Yu@Sun.COM (mode & (int)FKIOCTL))) 61908348SEric.Yu@Sun.COM return (EFAULT); 61918348SEric.Yu@Sun.COM return (0); 61928348SEric.Yu@Sun.COM } 61938348SEric.Yu@Sun.COM 61948348SEric.Yu@Sun.COM case I_FDINSERT: 61958348SEric.Yu@Sun.COM case I_SENDFD: 61968348SEric.Yu@Sun.COM case I_RECVFD: 61978348SEric.Yu@Sun.COM case I_ATMARK: 61988348SEric.Yu@Sun.COM case _SIOCSOCKFALLBACK: 61998348SEric.Yu@Sun.COM /* 62008348SEric.Yu@Sun.COM * These ioctls do not apply to sockets. I_FDINSERT can be 62018348SEric.Yu@Sun.COM * used to send M_PROTO messages without modifying the socket 62028348SEric.Yu@Sun.COM * state. I_SENDFD/RECVFD should not be used for socket file 62038348SEric.Yu@Sun.COM * descriptor passing since they assume a twisted stream. 62048348SEric.Yu@Sun.COM * SIOCATMARK must be used instead of I_ATMARK. 62058348SEric.Yu@Sun.COM * 62068348SEric.Yu@Sun.COM * _SIOCSOCKFALLBACK from an application should never be 62078348SEric.Yu@Sun.COM * processed. It is only generated by socktpi_open() or 62088348SEric.Yu@Sun.COM * in response to I_POP or I_PUSH. 62098348SEric.Yu@Sun.COM */ 62108348SEric.Yu@Sun.COM #ifdef DEBUG 62118348SEric.Yu@Sun.COM zcmn_err(getzoneid(), CE_WARN, 62128348SEric.Yu@Sun.COM "Unsupported STREAMS ioctl 0x%x on socket. " 62138348SEric.Yu@Sun.COM "Pid = %d\n", cmd, curproc->p_pid); 62148348SEric.Yu@Sun.COM #endif /* DEBUG */ 62158348SEric.Yu@Sun.COM return (EOPNOTSUPP); 62168348SEric.Yu@Sun.COM 62178348SEric.Yu@Sun.COM case _I_GETPEERCRED: 62188348SEric.Yu@Sun.COM if ((mode & FKIOCTL) == 0) 62198348SEric.Yu@Sun.COM return (EINVAL); 62208348SEric.Yu@Sun.COM 62218348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 62228348SEric.Yu@Sun.COM if ((so->so_mode & SM_CONNREQUIRED) == 0) { 62238348SEric.Yu@Sun.COM error = ENOTSUP; 62248348SEric.Yu@Sun.COM } else if ((so->so_state & SS_ISCONNECTED) == 0) { 62258348SEric.Yu@Sun.COM error = ENOTCONN; 62268348SEric.Yu@Sun.COM } else if (so->so_peercred != NULL) { 62278348SEric.Yu@Sun.COM k_peercred_t *kp = (k_peercred_t *)arg; 62288348SEric.Yu@Sun.COM kp->pc_cr = so->so_peercred; 62298348SEric.Yu@Sun.COM kp->pc_cpid = so->so_cpid; 62308348SEric.Yu@Sun.COM crhold(so->so_peercred); 62318348SEric.Yu@Sun.COM } else { 62328348SEric.Yu@Sun.COM error = EINVAL; 62338348SEric.Yu@Sun.COM } 62348348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 62358348SEric.Yu@Sun.COM return (error); 62368348SEric.Yu@Sun.COM 62378348SEric.Yu@Sun.COM default: 62388348SEric.Yu@Sun.COM /* 62398348SEric.Yu@Sun.COM * Do the higher-order bits of the ioctl cmd indicate 62408348SEric.Yu@Sun.COM * that it is an I_* streams ioctl? 62418348SEric.Yu@Sun.COM */ 62428348SEric.Yu@Sun.COM if ((cmd & 0xffffff00U) == STR && 62438348SEric.Yu@Sun.COM so->so_version == SOV_SOCKBSD) { 62448348SEric.Yu@Sun.COM #ifdef DEBUG 62458348SEric.Yu@Sun.COM zcmn_err(getzoneid(), CE_WARN, 62468348SEric.Yu@Sun.COM "Unsupported STREAMS ioctl 0x%x on socket. " 62478348SEric.Yu@Sun.COM "Pid = %d\n", cmd, curproc->p_pid); 62488348SEric.Yu@Sun.COM #endif /* DEBUG */ 62498348SEric.Yu@Sun.COM return (EOPNOTSUPP); 62508348SEric.Yu@Sun.COM } 62518348SEric.Yu@Sun.COM return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 62528348SEric.Yu@Sun.COM } 62538348SEric.Yu@Sun.COM } 62548348SEric.Yu@Sun.COM 62558348SEric.Yu@Sun.COM /* 62568348SEric.Yu@Sun.COM * Handle plumbing-related ioctls. 62578348SEric.Yu@Sun.COM */ 62588348SEric.Yu@Sun.COM static int 62598348SEric.Yu@Sun.COM socktpi_plumbioctl(struct vnode *vp, int cmd, intptr_t arg, int mode, 62608348SEric.Yu@Sun.COM struct cred *cr, int32_t *rvalp) 62618348SEric.Yu@Sun.COM { 62628348SEric.Yu@Sun.COM static const char sockmod_name[] = "sockmod"; 62638348SEric.Yu@Sun.COM struct sonode *so = VTOSO(vp); 62648348SEric.Yu@Sun.COM char mname[FMNAMESZ + 1]; 62658348SEric.Yu@Sun.COM int error; 62668348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 62678348SEric.Yu@Sun.COM 62688348SEric.Yu@Sun.COM ASSERT(MUTEX_HELD(&sti->sti_plumb_lock)); 62698348SEric.Yu@Sun.COM 62708348SEric.Yu@Sun.COM if (so->so_version == SOV_SOCKBSD) 62718348SEric.Yu@Sun.COM return (EOPNOTSUPP); 62728348SEric.Yu@Sun.COM 62738348SEric.Yu@Sun.COM if (so->so_version == SOV_STREAM) { 62748348SEric.Yu@Sun.COM /* 62758348SEric.Yu@Sun.COM * The imaginary "sockmod" has been popped - act as a stream. 62768348SEric.Yu@Sun.COM * If this is a push of sockmod then change back to a socket. 62778348SEric.Yu@Sun.COM */ 62788348SEric.Yu@Sun.COM if (cmd == I_PUSH) { 62798348SEric.Yu@Sun.COM error = ((mode & FKIOCTL) ? copystr : copyinstr)( 62808348SEric.Yu@Sun.COM (void *)arg, mname, sizeof (mname), NULL); 62818348SEric.Yu@Sun.COM 62828348SEric.Yu@Sun.COM if (error == 0 && strcmp(mname, sockmod_name) == 0) { 62838348SEric.Yu@Sun.COM dprintso(so, 0, ("socktpi_ioctl: going to " 62848348SEric.Yu@Sun.COM "socket version\n")); 62858348SEric.Yu@Sun.COM so_stream2sock(so); 62868348SEric.Yu@Sun.COM return (0); 62878348SEric.Yu@Sun.COM } 62888348SEric.Yu@Sun.COM } 62898348SEric.Yu@Sun.COM return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 62908348SEric.Yu@Sun.COM } 62918348SEric.Yu@Sun.COM 62928348SEric.Yu@Sun.COM switch (cmd) { 62938348SEric.Yu@Sun.COM case I_PUSH: 62948348SEric.Yu@Sun.COM if (sti->sti_direct) { 62958348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 62968348SEric.Yu@Sun.COM so_lock_single(so); 62978348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 62988348SEric.Yu@Sun.COM 62998348SEric.Yu@Sun.COM error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K, 63008348SEric.Yu@Sun.COM CRED(), rvalp); 63018348SEric.Yu@Sun.COM 63028348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 63038348SEric.Yu@Sun.COM if (error == 0) 63048348SEric.Yu@Sun.COM sti->sti_direct = 0; 63058348SEric.Yu@Sun.COM so_unlock_single(so, SOLOCKED); 63068348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 63078348SEric.Yu@Sun.COM 63088348SEric.Yu@Sun.COM if (error != 0) 63098348SEric.Yu@Sun.COM return (error); 63108348SEric.Yu@Sun.COM } 63118348SEric.Yu@Sun.COM 63128348SEric.Yu@Sun.COM error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 63138348SEric.Yu@Sun.COM if (error == 0) 63148348SEric.Yu@Sun.COM sti->sti_pushcnt++; 63158348SEric.Yu@Sun.COM return (error); 63168348SEric.Yu@Sun.COM 63178348SEric.Yu@Sun.COM case I_POP: 63188348SEric.Yu@Sun.COM if (sti->sti_pushcnt == 0) { 63198348SEric.Yu@Sun.COM /* Emulate sockmod being popped */ 63208348SEric.Yu@Sun.COM dprintso(so, 0, 63218348SEric.Yu@Sun.COM ("socktpi_ioctl: going to STREAMS version\n")); 63228348SEric.Yu@Sun.COM return (so_sock2stream(so)); 63238348SEric.Yu@Sun.COM } 63248348SEric.Yu@Sun.COM 63258348SEric.Yu@Sun.COM error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 63268348SEric.Yu@Sun.COM if (error == 0) 63278348SEric.Yu@Sun.COM sti->sti_pushcnt--; 63288348SEric.Yu@Sun.COM return (error); 63298348SEric.Yu@Sun.COM 63308348SEric.Yu@Sun.COM case I_LIST: { 63318348SEric.Yu@Sun.COM struct str_mlist *kmlistp, *umlistp; 63328348SEric.Yu@Sun.COM struct str_list kstrlist; 63338348SEric.Yu@Sun.COM ssize_t kstrlistsize; 63348348SEric.Yu@Sun.COM int i, nmods; 63358348SEric.Yu@Sun.COM 63368348SEric.Yu@Sun.COM STRUCT_DECL(str_list, ustrlist); 63378348SEric.Yu@Sun.COM STRUCT_INIT(ustrlist, mode); 63388348SEric.Yu@Sun.COM 63398348SEric.Yu@Sun.COM if (arg == NULL) { 63408348SEric.Yu@Sun.COM error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 63418348SEric.Yu@Sun.COM if (error == 0) 63428348SEric.Yu@Sun.COM (*rvalp)++; /* Add one for sockmod */ 63438348SEric.Yu@Sun.COM return (error); 63448348SEric.Yu@Sun.COM } 63458348SEric.Yu@Sun.COM 63468348SEric.Yu@Sun.COM error = so_copyin((void *)arg, STRUCT_BUF(ustrlist), 63478348SEric.Yu@Sun.COM STRUCT_SIZE(ustrlist), mode & FKIOCTL); 63488348SEric.Yu@Sun.COM if (error != 0) 63498348SEric.Yu@Sun.COM return (error); 63508348SEric.Yu@Sun.COM 63518348SEric.Yu@Sun.COM nmods = STRUCT_FGET(ustrlist, sl_nmods); 63528348SEric.Yu@Sun.COM if (nmods <= 0) 63538348SEric.Yu@Sun.COM return (EINVAL); 63548348SEric.Yu@Sun.COM /* 63558348SEric.Yu@Sun.COM * Ceiling nmods at nstrpush to prevent someone from 63568348SEric.Yu@Sun.COM * maliciously consuming lots of kernel memory. 63578348SEric.Yu@Sun.COM */ 63588348SEric.Yu@Sun.COM nmods = MIN(nmods, nstrpush); 63598348SEric.Yu@Sun.COM 63608348SEric.Yu@Sun.COM kstrlistsize = (nmods + 1) * sizeof (struct str_mlist); 63618348SEric.Yu@Sun.COM kstrlist.sl_nmods = nmods; 63628348SEric.Yu@Sun.COM kstrlist.sl_modlist = kmem_zalloc(kstrlistsize, KM_SLEEP); 63638348SEric.Yu@Sun.COM 63648348SEric.Yu@Sun.COM error = strioctl(vp, cmd, (intptr_t)&kstrlist, mode, K_TO_K, 63658348SEric.Yu@Sun.COM cr, rvalp); 63668348SEric.Yu@Sun.COM if (error != 0) 63678348SEric.Yu@Sun.COM goto done; 63688348SEric.Yu@Sun.COM 63698348SEric.Yu@Sun.COM /* 63708348SEric.Yu@Sun.COM * Considering the module list as a 0-based array of sl_nmods 63718348SEric.Yu@Sun.COM * modules, sockmod should conceptually exist at slot 63728348SEric.Yu@Sun.COM * sti_pushcnt. Insert sockmod at this location by sliding all 63738348SEric.Yu@Sun.COM * of the module names after so_pushcnt over by one. We know 63748348SEric.Yu@Sun.COM * that there will be room to do this since we allocated 63758348SEric.Yu@Sun.COM * sl_modlist with an additional slot. 63768348SEric.Yu@Sun.COM */ 63778348SEric.Yu@Sun.COM for (i = kstrlist.sl_nmods; i > sti->sti_pushcnt; i--) 63788348SEric.Yu@Sun.COM kstrlist.sl_modlist[i] = kstrlist.sl_modlist[i - 1]; 63798348SEric.Yu@Sun.COM 63808348SEric.Yu@Sun.COM (void) strcpy(kstrlist.sl_modlist[i].l_name, sockmod_name); 63818348SEric.Yu@Sun.COM kstrlist.sl_nmods++; 63828348SEric.Yu@Sun.COM 63838348SEric.Yu@Sun.COM /* 63848348SEric.Yu@Sun.COM * Copy all of the entries out to ustrlist. 63858348SEric.Yu@Sun.COM */ 63868348SEric.Yu@Sun.COM kmlistp = kstrlist.sl_modlist; 63878348SEric.Yu@Sun.COM umlistp = STRUCT_FGETP(ustrlist, sl_modlist); 63888348SEric.Yu@Sun.COM for (i = 0; i < nmods && i < kstrlist.sl_nmods; i++) { 63898348SEric.Yu@Sun.COM error = so_copyout(kmlistp++, umlistp++, 63908348SEric.Yu@Sun.COM sizeof (struct str_mlist), mode & FKIOCTL); 63918348SEric.Yu@Sun.COM if (error != 0) 63928348SEric.Yu@Sun.COM goto done; 63938348SEric.Yu@Sun.COM } 63948348SEric.Yu@Sun.COM 63958348SEric.Yu@Sun.COM error = so_copyout(&i, (void *)arg, sizeof (int32_t), 63968348SEric.Yu@Sun.COM mode & FKIOCTL); 63978348SEric.Yu@Sun.COM if (error == 0) 63988348SEric.Yu@Sun.COM *rvalp = 0; 63998348SEric.Yu@Sun.COM done: 64008348SEric.Yu@Sun.COM kmem_free(kstrlist.sl_modlist, kstrlistsize); 64018348SEric.Yu@Sun.COM return (error); 64028348SEric.Yu@Sun.COM } 64038348SEric.Yu@Sun.COM case I_LOOK: 64048348SEric.Yu@Sun.COM if (sti->sti_pushcnt == 0) { 64058348SEric.Yu@Sun.COM return (so_copyout(sockmod_name, (void *)arg, 64068348SEric.Yu@Sun.COM sizeof (sockmod_name), mode & FKIOCTL)); 64078348SEric.Yu@Sun.COM } 64088348SEric.Yu@Sun.COM return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 64098348SEric.Yu@Sun.COM 64108348SEric.Yu@Sun.COM case I_FIND: 64118348SEric.Yu@Sun.COM error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 64128348SEric.Yu@Sun.COM if (error && error != EINVAL) 64138348SEric.Yu@Sun.COM return (error); 64148348SEric.Yu@Sun.COM 64158348SEric.Yu@Sun.COM /* if not found and string was sockmod return 1 */ 64168348SEric.Yu@Sun.COM if (*rvalp == 0 || error == EINVAL) { 64178348SEric.Yu@Sun.COM error = ((mode & FKIOCTL) ? copystr : copyinstr)( 64188348SEric.Yu@Sun.COM (void *)arg, mname, sizeof (mname), NULL); 64198348SEric.Yu@Sun.COM if (error == ENAMETOOLONG) 64208348SEric.Yu@Sun.COM error = EINVAL; 64218348SEric.Yu@Sun.COM 64228348SEric.Yu@Sun.COM if (error == 0 && strcmp(mname, sockmod_name) == 0) 64238348SEric.Yu@Sun.COM *rvalp = 1; 64248348SEric.Yu@Sun.COM } 64258348SEric.Yu@Sun.COM return (error); 64268348SEric.Yu@Sun.COM 64278348SEric.Yu@Sun.COM default: 64288348SEric.Yu@Sun.COM panic("socktpi_plumbioctl: unknown ioctl %d", cmd); 64298348SEric.Yu@Sun.COM break; 64308348SEric.Yu@Sun.COM } 64318348SEric.Yu@Sun.COM 64328348SEric.Yu@Sun.COM return (0); 64338348SEric.Yu@Sun.COM } 64348348SEric.Yu@Sun.COM 64358348SEric.Yu@Sun.COM /* 64368348SEric.Yu@Sun.COM * Wrapper around the streams poll routine that implements socket poll 64378348SEric.Yu@Sun.COM * semantics. 64388348SEric.Yu@Sun.COM * The sockfs never calls pollwakeup itself - the stream head take care 64398348SEric.Yu@Sun.COM * of all pollwakeups. Since sockfs never holds so_lock when calling the 64408348SEric.Yu@Sun.COM * stream head there can never be a deadlock due to holding so_lock across 64418348SEric.Yu@Sun.COM * pollwakeup and acquiring so_lock in this routine. 64428348SEric.Yu@Sun.COM * 64438348SEric.Yu@Sun.COM * However, since the performance of VOP_POLL is critical we avoid 64448348SEric.Yu@Sun.COM * acquiring so_lock here. This is based on two assumptions: 64458348SEric.Yu@Sun.COM * - The poll implementation holds locks to serialize the VOP_POLL call 64468348SEric.Yu@Sun.COM * and a pollwakeup for the same pollhead. This ensures that should 64478348SEric.Yu@Sun.COM * e.g. so_state change during a socktpi_poll call the pollwakeup 64488348SEric.Yu@Sun.COM * (which strsock_* and strrput conspire to issue) is issued after 64498348SEric.Yu@Sun.COM * the state change. Thus the pollwakeup will block until VOP_POLL has 64508348SEric.Yu@Sun.COM * returned and then wake up poll and have it call VOP_POLL again. 64518348SEric.Yu@Sun.COM * - The reading of so_state without holding so_lock does not result in 64528348SEric.Yu@Sun.COM * stale data that is older than the latest state change that has dropped 64538348SEric.Yu@Sun.COM * so_lock. This is ensured by the mutex_exit issuing the appropriate 64548348SEric.Yu@Sun.COM * memory barrier to force the data into the coherency domain. 64558348SEric.Yu@Sun.COM */ 64568348SEric.Yu@Sun.COM static int 64578348SEric.Yu@Sun.COM sotpi_poll( 64588348SEric.Yu@Sun.COM struct sonode *so, 64598348SEric.Yu@Sun.COM short events, 64608348SEric.Yu@Sun.COM int anyyet, 64618348SEric.Yu@Sun.COM short *reventsp, 64628348SEric.Yu@Sun.COM struct pollhead **phpp) 64638348SEric.Yu@Sun.COM { 64648348SEric.Yu@Sun.COM short origevents = events; 64658348SEric.Yu@Sun.COM struct vnode *vp = SOTOV(so); 64668348SEric.Yu@Sun.COM int error; 64678348SEric.Yu@Sun.COM int so_state = so->so_state; /* snapshot */ 64688348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 64698348SEric.Yu@Sun.COM 64708348SEric.Yu@Sun.COM dprintso(so, 0, ("socktpi_poll(%p): state %s err %d\n", 64718348SEric.Yu@Sun.COM (void *)vp, pr_state(so_state, so->so_mode), so->so_error)); 64728348SEric.Yu@Sun.COM 64738348SEric.Yu@Sun.COM ASSERT(vp->v_type == VSOCK); 64748348SEric.Yu@Sun.COM ASSERT(vp->v_stream != NULL); 64758348SEric.Yu@Sun.COM 64768348SEric.Yu@Sun.COM if (so->so_version == SOV_STREAM) { 64778348SEric.Yu@Sun.COM /* The imaginary "sockmod" has been popped - act as a stream */ 64788348SEric.Yu@Sun.COM return (strpoll(vp->v_stream, events, anyyet, 64798348SEric.Yu@Sun.COM reventsp, phpp)); 64808348SEric.Yu@Sun.COM } 64818348SEric.Yu@Sun.COM 64828348SEric.Yu@Sun.COM if (!(so_state & SS_ISCONNECTED) && 64838348SEric.Yu@Sun.COM (so->so_mode & SM_CONNREQUIRED)) { 64848348SEric.Yu@Sun.COM /* Not connected yet - turn off write side events */ 64858348SEric.Yu@Sun.COM events &= ~(POLLOUT|POLLWRBAND); 64868348SEric.Yu@Sun.COM } 64878348SEric.Yu@Sun.COM /* 64888348SEric.Yu@Sun.COM * Check for errors without calling strpoll if the caller wants them. 64898348SEric.Yu@Sun.COM * In sockets the errors are represented as input/output events 64908348SEric.Yu@Sun.COM * and there is no need to ask the stream head for this information. 64918348SEric.Yu@Sun.COM */ 64928348SEric.Yu@Sun.COM if (so->so_error != 0 && 64938348SEric.Yu@Sun.COM ((POLLIN|POLLRDNORM|POLLOUT) & origevents) != 0) { 64948348SEric.Yu@Sun.COM *reventsp = (POLLIN|POLLRDNORM|POLLOUT) & origevents; 64958348SEric.Yu@Sun.COM return (0); 64968348SEric.Yu@Sun.COM } 64978348SEric.Yu@Sun.COM /* 64988348SEric.Yu@Sun.COM * Ignore M_PROTO only messages such as the T_EXDATA_IND messages. 64998348SEric.Yu@Sun.COM * These message with only an M_PROTO/M_PCPROTO part and no M_DATA 65008348SEric.Yu@Sun.COM * will not trigger a POLLIN event with POLLRDDATA set. 65018348SEric.Yu@Sun.COM * The handling of urgent data (causing POLLRDBAND) is done by 65028348SEric.Yu@Sun.COM * inspecting SS_OOBPEND below. 65038348SEric.Yu@Sun.COM */ 65048348SEric.Yu@Sun.COM events |= POLLRDDATA; 65058348SEric.Yu@Sun.COM 65068348SEric.Yu@Sun.COM /* 65078348SEric.Yu@Sun.COM * After shutdown(output) a stream head write error is set. 65088348SEric.Yu@Sun.COM * However, we should not return output events. 65098348SEric.Yu@Sun.COM */ 65108348SEric.Yu@Sun.COM events |= POLLNOERR; 65118348SEric.Yu@Sun.COM error = strpoll(vp->v_stream, events, anyyet, 65128348SEric.Yu@Sun.COM reventsp, phpp); 65138348SEric.Yu@Sun.COM if (error) 65148348SEric.Yu@Sun.COM return (error); 65158348SEric.Yu@Sun.COM 65168348SEric.Yu@Sun.COM ASSERT(!(*reventsp & POLLERR)); 65178348SEric.Yu@Sun.COM 65188348SEric.Yu@Sun.COM /* 65198348SEric.Yu@Sun.COM * Notes on T_CONN_IND handling for sockets. 65208348SEric.Yu@Sun.COM * 65218348SEric.Yu@Sun.COM * If strpoll() returned without events, SR_POLLIN is guaranteed 65228348SEric.Yu@Sun.COM * to be set, ensuring any subsequent strrput() runs pollwakeup(). 65238348SEric.Yu@Sun.COM * 65248348SEric.Yu@Sun.COM * Since the so_lock is not held, soqueueconnind() may have run 65258348SEric.Yu@Sun.COM * and a T_CONN_IND may be waiting. We now check for any queued 65268348SEric.Yu@Sun.COM * T_CONN_IND msgs on sti_conn_ind_head and set appropriate events 65278348SEric.Yu@Sun.COM * to ensure poll returns. 65288348SEric.Yu@Sun.COM * 65298348SEric.Yu@Sun.COM * However: 65308348SEric.Yu@Sun.COM * If the T_CONN_IND hasn't arrived by the time strpoll() returns, 65318348SEric.Yu@Sun.COM * when strrput() does run for an arriving M_PROTO with T_CONN_IND 65328348SEric.Yu@Sun.COM * the following actions will occur; taken together they ensure the 65338348SEric.Yu@Sun.COM * syscall will return. 65348348SEric.Yu@Sun.COM * 65358348SEric.Yu@Sun.COM * 1. If a socket, soqueueconnind() will queue the T_CONN_IND but if 65368348SEric.Yu@Sun.COM * the accept() was run on a non-blocking socket sowaitconnind() 65378348SEric.Yu@Sun.COM * may have already returned EWOULDBLOCK, so not be waiting to 65388348SEric.Yu@Sun.COM * process the message. Additionally socktpi_poll() has probably 65398348SEric.Yu@Sun.COM * proceeded past the sti_conn_ind_head check below. 65408348SEric.Yu@Sun.COM * 2. strrput() runs pollwakeup()->pollnotify()->cv_signal() to wake 65418348SEric.Yu@Sun.COM * this thread, however that could occur before poll_common() 65428348SEric.Yu@Sun.COM * has entered cv_wait. 65438348SEric.Yu@Sun.COM * 3. pollnotify() sets T_POLLWAKE, while holding the pc_lock. 65448348SEric.Yu@Sun.COM * 65458348SEric.Yu@Sun.COM * Before proceeding to cv_wait() in poll_common() for an event, 65468348SEric.Yu@Sun.COM * poll_common() atomically checks for T_POLLWAKE under the pc_lock, 65478348SEric.Yu@Sun.COM * and if set, re-calls strpoll() to ensure the late arriving 65488348SEric.Yu@Sun.COM * T_CONN_IND is recognized, and pollsys() returns. 65498348SEric.Yu@Sun.COM */ 65508348SEric.Yu@Sun.COM 65518348SEric.Yu@Sun.COM if (sti->sti_conn_ind_head != NULL) 65528348SEric.Yu@Sun.COM *reventsp |= (POLLIN|POLLRDNORM) & events; 65538348SEric.Yu@Sun.COM 65548348SEric.Yu@Sun.COM if (so->so_state & SS_OOBPEND) 65558348SEric.Yu@Sun.COM *reventsp |= POLLRDBAND & events; 65568348SEric.Yu@Sun.COM 65578348SEric.Yu@Sun.COM if (sti->sti_nl7c_rcv_mp != NULL) { 65588348SEric.Yu@Sun.COM *reventsp |= (POLLIN|POLLRDNORM) & events; 65598348SEric.Yu@Sun.COM } 65608348SEric.Yu@Sun.COM if ((sti->sti_nl7c_flags & NL7C_ENABLED) && 65618348SEric.Yu@Sun.COM ((POLLIN|POLLRDNORM) & *reventsp)) { 65628348SEric.Yu@Sun.COM sti->sti_nl7c_flags |= NL7C_POLLIN; 65638348SEric.Yu@Sun.COM } 65648348SEric.Yu@Sun.COM 65658348SEric.Yu@Sun.COM return (0); 65668348SEric.Yu@Sun.COM } 65678348SEric.Yu@Sun.COM 65688348SEric.Yu@Sun.COM /*ARGSUSED*/ 65698348SEric.Yu@Sun.COM static int 65708348SEric.Yu@Sun.COM socktpi_constructor(void *buf, void *cdrarg, int kmflags) 65718348SEric.Yu@Sun.COM { 65728348SEric.Yu@Sun.COM sotpi_sonode_t *st = (sotpi_sonode_t *)buf; 65738348SEric.Yu@Sun.COM int error = 0; 65748348SEric.Yu@Sun.COM 65758348SEric.Yu@Sun.COM error = sonode_constructor(buf, cdrarg, kmflags); 65768348SEric.Yu@Sun.COM if (error != 0) 65778348SEric.Yu@Sun.COM return (error); 65788348SEric.Yu@Sun.COM 65798348SEric.Yu@Sun.COM error = i_sotpi_info_constructor(&st->st_info); 65808348SEric.Yu@Sun.COM if (error != 0) 65818348SEric.Yu@Sun.COM sonode_destructor(buf, cdrarg); 65828348SEric.Yu@Sun.COM 65838348SEric.Yu@Sun.COM st->st_sonode.so_priv = &st->st_info; 65848348SEric.Yu@Sun.COM 65858348SEric.Yu@Sun.COM return (error); 65868348SEric.Yu@Sun.COM } 65878348SEric.Yu@Sun.COM 65888348SEric.Yu@Sun.COM /*ARGSUSED1*/ 65898348SEric.Yu@Sun.COM static void 65908348SEric.Yu@Sun.COM socktpi_destructor(void *buf, void *cdrarg) 65918348SEric.Yu@Sun.COM { 65928348SEric.Yu@Sun.COM sotpi_sonode_t *st = (sotpi_sonode_t *)buf; 65938348SEric.Yu@Sun.COM 65948348SEric.Yu@Sun.COM ASSERT(st->st_sonode.so_priv == &st->st_info); 65958348SEric.Yu@Sun.COM st->st_sonode.so_priv = NULL; 65968348SEric.Yu@Sun.COM 65978348SEric.Yu@Sun.COM i_sotpi_info_destructor(&st->st_info); 65988348SEric.Yu@Sun.COM sonode_destructor(buf, cdrarg); 65998348SEric.Yu@Sun.COM } 66008348SEric.Yu@Sun.COM 66018348SEric.Yu@Sun.COM static int 66028348SEric.Yu@Sun.COM socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags) 66038348SEric.Yu@Sun.COM { 66048348SEric.Yu@Sun.COM int retval; 66058348SEric.Yu@Sun.COM 66068348SEric.Yu@Sun.COM if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) { 66078348SEric.Yu@Sun.COM struct sonode *so = (struct sonode *)buf; 66088348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 66098348SEric.Yu@Sun.COM 66108348SEric.Yu@Sun.COM mutex_enter(&socklist.sl_lock); 66118348SEric.Yu@Sun.COM 66128348SEric.Yu@Sun.COM sti->sti_next_so = socklist.sl_list; 66138348SEric.Yu@Sun.COM sti->sti_prev_so = NULL; 66148348SEric.Yu@Sun.COM if (sti->sti_next_so != NULL) 66158348SEric.Yu@Sun.COM SOTOTPI(sti->sti_next_so)->sti_prev_so = so; 66168348SEric.Yu@Sun.COM socklist.sl_list = so; 66178348SEric.Yu@Sun.COM 66188348SEric.Yu@Sun.COM mutex_exit(&socklist.sl_lock); 66198348SEric.Yu@Sun.COM 66208348SEric.Yu@Sun.COM } 66218348SEric.Yu@Sun.COM return (retval); 66228348SEric.Yu@Sun.COM } 66238348SEric.Yu@Sun.COM 66248348SEric.Yu@Sun.COM static void 66258348SEric.Yu@Sun.COM socktpi_unix_destructor(void *buf, void *cdrarg) 66268348SEric.Yu@Sun.COM { 66278348SEric.Yu@Sun.COM struct sonode *so = (struct sonode *)buf; 66288348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 66298348SEric.Yu@Sun.COM 66308348SEric.Yu@Sun.COM mutex_enter(&socklist.sl_lock); 66318348SEric.Yu@Sun.COM 66328348SEric.Yu@Sun.COM if (sti->sti_next_so != NULL) 66338348SEric.Yu@Sun.COM SOTOTPI(sti->sti_next_so)->sti_prev_so = sti->sti_prev_so; 66348348SEric.Yu@Sun.COM if (sti->sti_prev_so != NULL) 66358348SEric.Yu@Sun.COM SOTOTPI(sti->sti_prev_so)->sti_next_so = sti->sti_next_so; 66368348SEric.Yu@Sun.COM else 66378348SEric.Yu@Sun.COM socklist.sl_list = sti->sti_next_so; 66388348SEric.Yu@Sun.COM 66398348SEric.Yu@Sun.COM mutex_exit(&socklist.sl_lock); 66408348SEric.Yu@Sun.COM 66418348SEric.Yu@Sun.COM socktpi_destructor(buf, cdrarg); 66428348SEric.Yu@Sun.COM } 66438348SEric.Yu@Sun.COM 66448348SEric.Yu@Sun.COM int 66458348SEric.Yu@Sun.COM socktpi_init(void) 66468348SEric.Yu@Sun.COM { 66478348SEric.Yu@Sun.COM /* 66488348SEric.Yu@Sun.COM * Create sonode caches. We create a special one for AF_UNIX so 66498348SEric.Yu@Sun.COM * that we can track them for netstat(1m). 66508348SEric.Yu@Sun.COM */ 66518348SEric.Yu@Sun.COM socktpi_cache = kmem_cache_create("socktpi_cache", 66528348SEric.Yu@Sun.COM sizeof (struct sotpi_sonode), 0, socktpi_constructor, 66538348SEric.Yu@Sun.COM socktpi_destructor, NULL, NULL, NULL, 0); 66548348SEric.Yu@Sun.COM 66558348SEric.Yu@Sun.COM socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache", 66568348SEric.Yu@Sun.COM sizeof (struct sotpi_sonode), 0, socktpi_unix_constructor, 66578348SEric.Yu@Sun.COM socktpi_unix_destructor, NULL, NULL, NULL, 0); 66588348SEric.Yu@Sun.COM 66598348SEric.Yu@Sun.COM return (0); 66608348SEric.Yu@Sun.COM } 66618348SEric.Yu@Sun.COM 66628348SEric.Yu@Sun.COM /* 66638348SEric.Yu@Sun.COM * Given a non-TPI sonode, allocate and prep it to be ready for TPI. 66648348SEric.Yu@Sun.COM * 66658348SEric.Yu@Sun.COM * Caller must still update state and mode using sotpi_update_state(). 66668348SEric.Yu@Sun.COM * 66678348SEric.Yu@Sun.COM * Returns the STREAM queue that the protocol should use. 66688348SEric.Yu@Sun.COM */ 66698348SEric.Yu@Sun.COM queue_t * 66708348SEric.Yu@Sun.COM sotpi_convert_sonode(struct sonode *so, struct sockparams *newsp, 66718348SEric.Yu@Sun.COM boolean_t *direct, struct cred *cr) 66728348SEric.Yu@Sun.COM { 66738348SEric.Yu@Sun.COM sotpi_info_t *sti; 66748348SEric.Yu@Sun.COM struct sockparams *origsp = so->so_sockparams; 66758348SEric.Yu@Sun.COM sock_lower_handle_t handle = so->so_proto_handle; 66768348SEric.Yu@Sun.COM uint_t old_state = so->so_state; 66778348SEric.Yu@Sun.COM struct stdata *stp; 66788348SEric.Yu@Sun.COM struct vnode *vp; 66798348SEric.Yu@Sun.COM queue_t *q; 66808348SEric.Yu@Sun.COM 66818348SEric.Yu@Sun.COM *direct = B_FALSE; 66828348SEric.Yu@Sun.COM so->so_sockparams = newsp; 66838348SEric.Yu@Sun.COM /* 66848348SEric.Yu@Sun.COM * Allocate and initalize fields required by TPI. 66858348SEric.Yu@Sun.COM */ 66868348SEric.Yu@Sun.COM (void) sotpi_info_create(so, KM_SLEEP); 66878348SEric.Yu@Sun.COM sotpi_info_init(so); 66888348SEric.Yu@Sun.COM 66898348SEric.Yu@Sun.COM if (sotpi_init(so, NULL, cr, SO_FALLBACK) != 0) { 66908348SEric.Yu@Sun.COM sotpi_info_fini(so); 66918348SEric.Yu@Sun.COM sotpi_info_destroy(so); 66928348SEric.Yu@Sun.COM so->so_state = old_state; 66938348SEric.Yu@Sun.COM return (NULL); 66948348SEric.Yu@Sun.COM } 66958348SEric.Yu@Sun.COM ASSERT(handle == so->so_proto_handle); 66968348SEric.Yu@Sun.COM sti = SOTOTPI(so); 66978348SEric.Yu@Sun.COM if (sti->sti_direct != 0) 66988348SEric.Yu@Sun.COM *direct = B_TRUE; 66998348SEric.Yu@Sun.COM 67008348SEric.Yu@Sun.COM /* 67018348SEric.Yu@Sun.COM * Keep the original sp around so we can properly dispose of the 67028348SEric.Yu@Sun.COM * sonode when the socket is being closed. 67038348SEric.Yu@Sun.COM */ 67048348SEric.Yu@Sun.COM sti->sti_orig_sp = origsp; 67058348SEric.Yu@Sun.COM 67068348SEric.Yu@Sun.COM so_basic_strinit(so); /* skips the T_CAPABILITY_REQ */ 67078348SEric.Yu@Sun.COM so_alloc_addr(so, so->so_max_addr_len); 67088348SEric.Yu@Sun.COM 67098348SEric.Yu@Sun.COM /* 67108348SEric.Yu@Sun.COM * If the application has done a SIOCSPGRP, make sure the 67118348SEric.Yu@Sun.COM * STREAM head is aware. This needs to take place before 67128348SEric.Yu@Sun.COM * the protocol start sending up messages. Otherwise we 67138348SEric.Yu@Sun.COM * might miss to generate SIGPOLL. 67148348SEric.Yu@Sun.COM * 67158348SEric.Yu@Sun.COM * It is possible that the application will receive duplicate 67168348SEric.Yu@Sun.COM * signals if some were already generated for either data or 67178348SEric.Yu@Sun.COM * connection indications. 67188348SEric.Yu@Sun.COM */ 67198348SEric.Yu@Sun.COM if (so->so_pgrp != 0) { 67208348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 67218348SEric.Yu@Sun.COM if (so_set_events(so, so->so_vnode, cr) != 0) 67228348SEric.Yu@Sun.COM so->so_pgrp = 0; 67238348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 67248348SEric.Yu@Sun.COM } 67258348SEric.Yu@Sun.COM 67268348SEric.Yu@Sun.COM /* 67278348SEric.Yu@Sun.COM * Determine which queue to use. 67288348SEric.Yu@Sun.COM */ 67298348SEric.Yu@Sun.COM vp = SOTOV(so); 67308348SEric.Yu@Sun.COM stp = vp->v_stream; 67318348SEric.Yu@Sun.COM ASSERT(stp != NULL); 67328348SEric.Yu@Sun.COM q = stp->sd_wrq->q_next; 67338348SEric.Yu@Sun.COM 67348348SEric.Yu@Sun.COM /* 67358348SEric.Yu@Sun.COM * Skip any modules that may have been auto pushed when the device 67368348SEric.Yu@Sun.COM * was opened 67378348SEric.Yu@Sun.COM */ 67388348SEric.Yu@Sun.COM while (q->q_next != NULL) 67398348SEric.Yu@Sun.COM q = q->q_next; 67408348SEric.Yu@Sun.COM q = _RD(q); 67418348SEric.Yu@Sun.COM 67428348SEric.Yu@Sun.COM return (q); 67438348SEric.Yu@Sun.COM } 67448348SEric.Yu@Sun.COM 67458348SEric.Yu@Sun.COM void 67468348SEric.Yu@Sun.COM sotpi_update_state(struct sonode *so, struct T_capability_ack *tcap, 67478348SEric.Yu@Sun.COM struct sockaddr *laddr, socklen_t laddrlen, struct sockaddr *faddr, 67488348SEric.Yu@Sun.COM socklen_t faddrlen, short opts) 67498348SEric.Yu@Sun.COM { 67508348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 67518348SEric.Yu@Sun.COM 67528348SEric.Yu@Sun.COM so_proc_tcapability_ack(so, tcap); 67538348SEric.Yu@Sun.COM 67548348SEric.Yu@Sun.COM so->so_options |= opts; 67558348SEric.Yu@Sun.COM 67568348SEric.Yu@Sun.COM /* 67578348SEric.Yu@Sun.COM * Determine whether the foreign and local address are valid 67588348SEric.Yu@Sun.COM */ 67598348SEric.Yu@Sun.COM if (laddrlen != 0) { 67608348SEric.Yu@Sun.COM ASSERT(laddrlen <= sti->sti_laddr_maxlen); 67618348SEric.Yu@Sun.COM sti->sti_laddr_len = laddrlen; 67628348SEric.Yu@Sun.COM bcopy(laddr, sti->sti_laddr_sa, laddrlen); 67638348SEric.Yu@Sun.COM sti->sti_laddr_valid = (so->so_state & SS_ISBOUND); 67648348SEric.Yu@Sun.COM } 67658348SEric.Yu@Sun.COM 67668348SEric.Yu@Sun.COM if (faddrlen != 0) { 67678348SEric.Yu@Sun.COM ASSERT(faddrlen <= sti->sti_faddr_maxlen); 67688348SEric.Yu@Sun.COM sti->sti_faddr_len = faddrlen; 67698348SEric.Yu@Sun.COM bcopy(faddr, sti->sti_faddr_sa, faddrlen); 67708348SEric.Yu@Sun.COM sti->sti_faddr_valid = (so->so_state & SS_ISCONNECTED); 67718348SEric.Yu@Sun.COM } 67728348SEric.Yu@Sun.COM 67738348SEric.Yu@Sun.COM } 67748348SEric.Yu@Sun.COM 67758348SEric.Yu@Sun.COM /* 67768348SEric.Yu@Sun.COM * Allocate enough space to cache the local and foreign addresses. 67778348SEric.Yu@Sun.COM */ 67788348SEric.Yu@Sun.COM void 67798348SEric.Yu@Sun.COM so_alloc_addr(struct sonode *so, t_uscalar_t maxlen) 67808348SEric.Yu@Sun.COM { 67818348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 67828348SEric.Yu@Sun.COM 67838348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL); 67848348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_len == 0 && sti->sti_faddr_len == 0); 67858348SEric.Yu@Sun.COM sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 67868348SEric.Yu@Sun.COM P2ROUNDUP(maxlen, KMEM_ALIGN); 67878348SEric.Yu@Sun.COM so->so_max_addr_len = sti->sti_laddr_maxlen; 67888348SEric.Yu@Sun.COM sti->sti_laddr_sa = kmem_alloc(sti->sti_laddr_maxlen * 2, KM_SLEEP); 67898348SEric.Yu@Sun.COM sti->sti_faddr_sa = (struct sockaddr *)((caddr_t)sti->sti_laddr_sa 67908348SEric.Yu@Sun.COM + sti->sti_laddr_maxlen); 67918348SEric.Yu@Sun.COM 67928348SEric.Yu@Sun.COM if (so->so_family == AF_UNIX) { 67938348SEric.Yu@Sun.COM /* 67948348SEric.Yu@Sun.COM * Initialize AF_UNIX related fields. 67958348SEric.Yu@Sun.COM */ 67968348SEric.Yu@Sun.COM bzero(&sti->sti_ux_laddr, sizeof (sti->sti_ux_laddr)); 67978348SEric.Yu@Sun.COM bzero(&sti->sti_ux_faddr, sizeof (sti->sti_ux_faddr)); 67988348SEric.Yu@Sun.COM } 67998348SEric.Yu@Sun.COM } 68008348SEric.Yu@Sun.COM 68018348SEric.Yu@Sun.COM 68028348SEric.Yu@Sun.COM sotpi_info_t * 68038348SEric.Yu@Sun.COM sotpi_sototpi(struct sonode *so) 68048348SEric.Yu@Sun.COM { 68058348SEric.Yu@Sun.COM sotpi_info_t *sti; 68068348SEric.Yu@Sun.COM 68078348SEric.Yu@Sun.COM if (so == NULL) 68088348SEric.Yu@Sun.COM return (NULL); 68098348SEric.Yu@Sun.COM 68108348SEric.Yu@Sun.COM sti = (sotpi_info_t *)so->so_priv; 68118348SEric.Yu@Sun.COM 68128348SEric.Yu@Sun.COM ASSERT(sti != NULL); 68138348SEric.Yu@Sun.COM ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC); 68148348SEric.Yu@Sun.COM 68158348SEric.Yu@Sun.COM return (sti); 68168348SEric.Yu@Sun.COM } 68178348SEric.Yu@Sun.COM 68188348SEric.Yu@Sun.COM static int 68198348SEric.Yu@Sun.COM i_sotpi_info_constructor(sotpi_info_t *sti) 68208348SEric.Yu@Sun.COM { 68218348SEric.Yu@Sun.COM sti->sti_magic = SOTPI_INFO_MAGIC; 68228348SEric.Yu@Sun.COM sti->sti_ack_mp = NULL; 68238348SEric.Yu@Sun.COM sti->sti_discon_ind_mp = NULL; 68248348SEric.Yu@Sun.COM sti->sti_ux_bound_vp = NULL; 68258348SEric.Yu@Sun.COM sti->sti_unbind_mp = NULL; 68268348SEric.Yu@Sun.COM 68278348SEric.Yu@Sun.COM sti->sti_conn_ind_head = NULL; 68288348SEric.Yu@Sun.COM sti->sti_conn_ind_tail = NULL; 68298348SEric.Yu@Sun.COM 68308348SEric.Yu@Sun.COM sti->sti_laddr_sa = NULL; 68318348SEric.Yu@Sun.COM sti->sti_faddr_sa = NULL; 68328348SEric.Yu@Sun.COM 68338348SEric.Yu@Sun.COM sti->sti_nl7c_flags = 0; 68348348SEric.Yu@Sun.COM sti->sti_nl7c_uri = NULL; 68358348SEric.Yu@Sun.COM sti->sti_nl7c_rcv_mp = NULL; 68368348SEric.Yu@Sun.COM 68378348SEric.Yu@Sun.COM mutex_init(&sti->sti_plumb_lock, NULL, MUTEX_DEFAULT, NULL); 68388348SEric.Yu@Sun.COM cv_init(&sti->sti_ack_cv, NULL, CV_DEFAULT, NULL); 68398348SEric.Yu@Sun.COM 68408348SEric.Yu@Sun.COM return (0); 68418348SEric.Yu@Sun.COM } 68428348SEric.Yu@Sun.COM 68438348SEric.Yu@Sun.COM static void 68448348SEric.Yu@Sun.COM i_sotpi_info_destructor(sotpi_info_t *sti) 68458348SEric.Yu@Sun.COM { 68468348SEric.Yu@Sun.COM ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC); 68478348SEric.Yu@Sun.COM ASSERT(sti->sti_ack_mp == NULL); 68488348SEric.Yu@Sun.COM ASSERT(sti->sti_discon_ind_mp == NULL); 68498348SEric.Yu@Sun.COM ASSERT(sti->sti_ux_bound_vp == NULL); 68508348SEric.Yu@Sun.COM ASSERT(sti->sti_unbind_mp == NULL); 68518348SEric.Yu@Sun.COM 68528348SEric.Yu@Sun.COM ASSERT(sti->sti_conn_ind_head == NULL); 68538348SEric.Yu@Sun.COM ASSERT(sti->sti_conn_ind_tail == NULL); 68548348SEric.Yu@Sun.COM 68558348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_sa == NULL); 68568348SEric.Yu@Sun.COM ASSERT(sti->sti_faddr_sa == NULL); 68578348SEric.Yu@Sun.COM 68588348SEric.Yu@Sun.COM ASSERT(sti->sti_nl7c_flags == 0); 68598348SEric.Yu@Sun.COM ASSERT(sti->sti_nl7c_uri == NULL); 68608348SEric.Yu@Sun.COM ASSERT(sti->sti_nl7c_rcv_mp == NULL); 68618348SEric.Yu@Sun.COM 68628348SEric.Yu@Sun.COM mutex_destroy(&sti->sti_plumb_lock); 68638348SEric.Yu@Sun.COM cv_destroy(&sti->sti_ack_cv); 68648348SEric.Yu@Sun.COM } 68658348SEric.Yu@Sun.COM 68668348SEric.Yu@Sun.COM /* 68678348SEric.Yu@Sun.COM * Creates and attaches TPI information to the given sonode 68688348SEric.Yu@Sun.COM */ 68698348SEric.Yu@Sun.COM static boolean_t 68708348SEric.Yu@Sun.COM sotpi_info_create(struct sonode *so, int kmflags) 68718348SEric.Yu@Sun.COM { 68728348SEric.Yu@Sun.COM sotpi_info_t *sti; 68738348SEric.Yu@Sun.COM 68748348SEric.Yu@Sun.COM ASSERT(so->so_priv == NULL); 68758348SEric.Yu@Sun.COM 68768348SEric.Yu@Sun.COM if ((sti = kmem_zalloc(sizeof (*sti), kmflags)) == NULL) 68778348SEric.Yu@Sun.COM return (B_FALSE); 68788348SEric.Yu@Sun.COM 68798348SEric.Yu@Sun.COM if (i_sotpi_info_constructor(sti) != 0) { 68808348SEric.Yu@Sun.COM kmem_free(sti, sizeof (*sti)); 68818348SEric.Yu@Sun.COM return (B_FALSE); 68828348SEric.Yu@Sun.COM } 68838348SEric.Yu@Sun.COM 68848348SEric.Yu@Sun.COM so->so_priv = (void *)sti; 68858348SEric.Yu@Sun.COM return (B_TRUE); 68868348SEric.Yu@Sun.COM } 68878348SEric.Yu@Sun.COM 68888348SEric.Yu@Sun.COM /* 68898348SEric.Yu@Sun.COM * Initializes the TPI information. 68908348SEric.Yu@Sun.COM */ 68918348SEric.Yu@Sun.COM static void 68928348SEric.Yu@Sun.COM sotpi_info_init(struct sonode *so) 68938348SEric.Yu@Sun.COM { 68948348SEric.Yu@Sun.COM struct vnode *vp = SOTOV(so); 68958348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 68968348SEric.Yu@Sun.COM time_t now; 68978348SEric.Yu@Sun.COM 68988348SEric.Yu@Sun.COM sti->sti_dev = so->so_sockparams->sp_sdev_info.sd_vnode->v_rdev; 68998348SEric.Yu@Sun.COM vp->v_rdev = sti->sti_dev; 69008348SEric.Yu@Sun.COM 69018348SEric.Yu@Sun.COM sti->sti_orig_sp = NULL; 69028348SEric.Yu@Sun.COM 69038348SEric.Yu@Sun.COM sti->sti_pushcnt = 0; 69048348SEric.Yu@Sun.COM 69058348SEric.Yu@Sun.COM now = gethrestime_sec(); 69068348SEric.Yu@Sun.COM sti->sti_atime = now; 69078348SEric.Yu@Sun.COM sti->sti_mtime = now; 69088348SEric.Yu@Sun.COM sti->sti_ctime = now; 69098348SEric.Yu@Sun.COM 69108348SEric.Yu@Sun.COM sti->sti_eaddr_mp = NULL; 69118348SEric.Yu@Sun.COM sti->sti_delayed_error = 0; 69128348SEric.Yu@Sun.COM 69138348SEric.Yu@Sun.COM sti->sti_provinfo = NULL; 69148348SEric.Yu@Sun.COM 69158348SEric.Yu@Sun.COM sti->sti_oobcnt = 0; 69168348SEric.Yu@Sun.COM sti->sti_oobsigcnt = 0; 69178348SEric.Yu@Sun.COM 69188348SEric.Yu@Sun.COM ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL); 69198348SEric.Yu@Sun.COM 69208348SEric.Yu@Sun.COM sti->sti_laddr_sa = 0; 69218348SEric.Yu@Sun.COM sti->sti_faddr_sa = 0; 69228348SEric.Yu@Sun.COM sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 0; 69238348SEric.Yu@Sun.COM sti->sti_laddr_len = sti->sti_faddr_len = 0; 69248348SEric.Yu@Sun.COM 69258348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 69268348SEric.Yu@Sun.COM sti->sti_faddr_valid = 0; 69278348SEric.Yu@Sun.COM sti->sti_faddr_noxlate = 0; 69288348SEric.Yu@Sun.COM 69298348SEric.Yu@Sun.COM sti->sti_direct = 0; 69308348SEric.Yu@Sun.COM 69318348SEric.Yu@Sun.COM ASSERT(sti->sti_ack_mp == NULL); 69328348SEric.Yu@Sun.COM ASSERT(sti->sti_ux_bound_vp == NULL); 69338348SEric.Yu@Sun.COM ASSERT(sti->sti_unbind_mp == NULL); 69348348SEric.Yu@Sun.COM 69358348SEric.Yu@Sun.COM ASSERT(sti->sti_conn_ind_head == NULL); 69368348SEric.Yu@Sun.COM ASSERT(sti->sti_conn_ind_tail == NULL); 69378348SEric.Yu@Sun.COM 69388348SEric.Yu@Sun.COM /* Initialize the kernel SSL proxy fields */ 69398348SEric.Yu@Sun.COM sti->sti_kssl_type = KSSL_NO_PROXY; 69408348SEric.Yu@Sun.COM sti->sti_kssl_ent = NULL; 69418348SEric.Yu@Sun.COM sti->sti_kssl_ctx = NULL; 69428348SEric.Yu@Sun.COM } 69438348SEric.Yu@Sun.COM 69448348SEric.Yu@Sun.COM /* 69458348SEric.Yu@Sun.COM * Given a sonode, grab the TPI info and free any data. 69468348SEric.Yu@Sun.COM */ 69478348SEric.Yu@Sun.COM static void 69488348SEric.Yu@Sun.COM sotpi_info_fini(struct sonode *so) 69498348SEric.Yu@Sun.COM { 69508348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 69518348SEric.Yu@Sun.COM mblk_t *mp; 69528348SEric.Yu@Sun.COM 69538348SEric.Yu@Sun.COM ASSERT(sti->sti_discon_ind_mp == NULL); 69548348SEric.Yu@Sun.COM 69558348SEric.Yu@Sun.COM if ((mp = sti->sti_conn_ind_head) != NULL) { 69568348SEric.Yu@Sun.COM mblk_t *mp1; 69578348SEric.Yu@Sun.COM 69588348SEric.Yu@Sun.COM while (mp) { 69598348SEric.Yu@Sun.COM mp1 = mp->b_next; 69608348SEric.Yu@Sun.COM mp->b_next = NULL; 69618348SEric.Yu@Sun.COM freemsg(mp); 69628348SEric.Yu@Sun.COM mp = mp1; 69638348SEric.Yu@Sun.COM } 69648348SEric.Yu@Sun.COM sti->sti_conn_ind_head = sti->sti_conn_ind_tail = NULL; 69658348SEric.Yu@Sun.COM } 69668348SEric.Yu@Sun.COM 69678348SEric.Yu@Sun.COM /* 69688348SEric.Yu@Sun.COM * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely 69698348SEric.Yu@Sun.COM * indirect them. It also uses so_count as a validity test. 69708348SEric.Yu@Sun.COM */ 69718348SEric.Yu@Sun.COM mutex_enter(&so->so_lock); 69728348SEric.Yu@Sun.COM 69738348SEric.Yu@Sun.COM if (sti->sti_laddr_sa) { 69748348SEric.Yu@Sun.COM ASSERT((caddr_t)sti->sti_faddr_sa == 69758348SEric.Yu@Sun.COM (caddr_t)sti->sti_laddr_sa + sti->sti_laddr_maxlen); 69768348SEric.Yu@Sun.COM ASSERT(sti->sti_faddr_maxlen == sti->sti_laddr_maxlen); 69778348SEric.Yu@Sun.COM sti->sti_laddr_valid = 0; 69788348SEric.Yu@Sun.COM sti->sti_faddr_valid = 0; 69798348SEric.Yu@Sun.COM kmem_free(sti->sti_laddr_sa, sti->sti_laddr_maxlen * 2); 69808348SEric.Yu@Sun.COM sti->sti_laddr_sa = NULL; 69818348SEric.Yu@Sun.COM sti->sti_laddr_len = sti->sti_laddr_maxlen = 0; 69828348SEric.Yu@Sun.COM sti->sti_faddr_sa = NULL; 69838348SEric.Yu@Sun.COM sti->sti_faddr_len = sti->sti_faddr_maxlen = 0; 69848348SEric.Yu@Sun.COM } 69858348SEric.Yu@Sun.COM 69868348SEric.Yu@Sun.COM mutex_exit(&so->so_lock); 69878348SEric.Yu@Sun.COM 69888348SEric.Yu@Sun.COM if ((mp = sti->sti_eaddr_mp) != NULL) { 69898348SEric.Yu@Sun.COM freemsg(mp); 69908348SEric.Yu@Sun.COM sti->sti_eaddr_mp = NULL; 69918348SEric.Yu@Sun.COM sti->sti_delayed_error = 0; 69928348SEric.Yu@Sun.COM } 69938348SEric.Yu@Sun.COM 69948348SEric.Yu@Sun.COM if ((mp = sti->sti_ack_mp) != NULL) { 69958348SEric.Yu@Sun.COM freemsg(mp); 69968348SEric.Yu@Sun.COM sti->sti_ack_mp = NULL; 69978348SEric.Yu@Sun.COM } 69988348SEric.Yu@Sun.COM 69998348SEric.Yu@Sun.COM if ((mp = sti->sti_nl7c_rcv_mp) != NULL) { 70008348SEric.Yu@Sun.COM sti->sti_nl7c_rcv_mp = NULL; 70018348SEric.Yu@Sun.COM freemsg(mp); 70028348SEric.Yu@Sun.COM } 70038348SEric.Yu@Sun.COM sti->sti_nl7c_rcv_rval = 0; 70048348SEric.Yu@Sun.COM if (sti->sti_nl7c_uri != NULL) { 70058348SEric.Yu@Sun.COM nl7c_urifree(so); 70068348SEric.Yu@Sun.COM /* urifree() cleared nl7c_uri */ 70078348SEric.Yu@Sun.COM } 70088348SEric.Yu@Sun.COM if (sti->sti_nl7c_flags) { 70098348SEric.Yu@Sun.COM sti->sti_nl7c_flags = 0; 70108348SEric.Yu@Sun.COM } 70118348SEric.Yu@Sun.COM 70128348SEric.Yu@Sun.COM ASSERT(sti->sti_ux_bound_vp == NULL); 70138348SEric.Yu@Sun.COM if ((mp = sti->sti_unbind_mp) != NULL) { 70148348SEric.Yu@Sun.COM freemsg(mp); 70158348SEric.Yu@Sun.COM sti->sti_unbind_mp = NULL; 70168348SEric.Yu@Sun.COM } 70178348SEric.Yu@Sun.COM } 70188348SEric.Yu@Sun.COM 70198348SEric.Yu@Sun.COM /* 70208348SEric.Yu@Sun.COM * Destroys the TPI information attached to a sonode. 70218348SEric.Yu@Sun.COM */ 70228348SEric.Yu@Sun.COM static void 70238348SEric.Yu@Sun.COM sotpi_info_destroy(struct sonode *so) 70248348SEric.Yu@Sun.COM { 70258348SEric.Yu@Sun.COM sotpi_info_t *sti = SOTOTPI(so); 70268348SEric.Yu@Sun.COM 70278348SEric.Yu@Sun.COM i_sotpi_info_destructor(sti); 70288348SEric.Yu@Sun.COM kmem_free(sti, sizeof (*sti)); 70298348SEric.Yu@Sun.COM 70308348SEric.Yu@Sun.COM so->so_priv = NULL; 70318348SEric.Yu@Sun.COM } 70328348SEric.Yu@Sun.COM 70338348SEric.Yu@Sun.COM /* 70348429SEric.Yu@Sun.COM * Create the global sotpi socket module entry. It will never be freed. 70358348SEric.Yu@Sun.COM */ 70368348SEric.Yu@Sun.COM smod_info_t * 70378348SEric.Yu@Sun.COM sotpi_smod_create(void) 70388348SEric.Yu@Sun.COM { 70398348SEric.Yu@Sun.COM smod_info_t *smodp; 70408348SEric.Yu@Sun.COM 70418348SEric.Yu@Sun.COM smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP); 70428429SEric.Yu@Sun.COM smodp->smod_name = kmem_alloc(sizeof (SOTPI_SMOD_NAME), KM_SLEEP); 70438429SEric.Yu@Sun.COM (void) strcpy(smodp->smod_name, SOTPI_SMOD_NAME); 70448348SEric.Yu@Sun.COM /* 70458429SEric.Yu@Sun.COM * Initialize the smod_refcnt to 1 so it will never be freed. 70468348SEric.Yu@Sun.COM */ 70478348SEric.Yu@Sun.COM smodp->smod_refcnt = 1; 70488348SEric.Yu@Sun.COM smodp->smod_uc_version = SOCK_UC_VERSION; 70498348SEric.Yu@Sun.COM smodp->smod_dc_version = SOCK_DC_VERSION; 70508348SEric.Yu@Sun.COM smodp->smod_sock_create_func = &sotpi_create; 70518348SEric.Yu@Sun.COM smodp->smod_sock_destroy_func = &sotpi_destroy; 70528348SEric.Yu@Sun.COM return (smodp); 70538348SEric.Yu@Sun.COM } 7054