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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/t_lock.h> 310Sstevel@tonic-gate #include <sys/param.h> 320Sstevel@tonic-gate #include <sys/systm.h> 330Sstevel@tonic-gate #include <sys/buf.h> 340Sstevel@tonic-gate #include <sys/conf.h> 350Sstevel@tonic-gate #include <sys/cred.h> 360Sstevel@tonic-gate #include <sys/kmem.h> 370Sstevel@tonic-gate #include <sys/sysmacros.h> 380Sstevel@tonic-gate #include <sys/vfs.h> 390Sstevel@tonic-gate #include <sys/vnode.h> 400Sstevel@tonic-gate #include <sys/debug.h> 410Sstevel@tonic-gate #include <sys/errno.h> 420Sstevel@tonic-gate #include <sys/time.h> 430Sstevel@tonic-gate #include <sys/file.h> 440Sstevel@tonic-gate #include <sys/open.h> 450Sstevel@tonic-gate #include <sys/user.h> 460Sstevel@tonic-gate #include <sys/termios.h> 470Sstevel@tonic-gate #include <sys/stream.h> 480Sstevel@tonic-gate #include <sys/strsubr.h> 490Sstevel@tonic-gate #include <sys/strsun.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> 60*741Smasputra #include <sys/sockio.h> 610Sstevel@tonic-gate #include <netinet/in.h> 620Sstevel@tonic-gate #include <sys/un.h> 630Sstevel@tonic-gate #include <sys/strsun.h> 640Sstevel@tonic-gate 650Sstevel@tonic-gate #include <sys/tiuser.h> 660Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 670Sstevel@tonic-gate #include <sys/tihdr.h> 680Sstevel@tonic-gate #include <sys/timod.h> /* TI_GETMYNAME, TI_GETPEERNAME */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate #include <c2/audit.h> 710Sstevel@tonic-gate 720Sstevel@tonic-gate #include <inet/common.h> 730Sstevel@tonic-gate #include <inet/ip.h> 740Sstevel@tonic-gate #include <inet/ip6.h> 750Sstevel@tonic-gate #include <inet/tcp.h> 76*741Smasputra #include <inet/udp_impl.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate #include <fs/sockfs/nl7c.h> 790Sstevel@tonic-gate #include <sys/zone.h> 800Sstevel@tonic-gate 810Sstevel@tonic-gate /* 820Sstevel@tonic-gate * Possible failures when memory can't be allocated. The documented behavior: 830Sstevel@tonic-gate * 840Sstevel@tonic-gate * 5.5: 4.X: XNET: 850Sstevel@tonic-gate * accept: ENOMEM/ENOSR/EINTR - (EINTR) ENOMEM/ENOBUFS/ENOSR/ 860Sstevel@tonic-gate * EINTR 870Sstevel@tonic-gate * (4.X does not document EINTR but returns it) 880Sstevel@tonic-gate * bind: ENOSR - ENOBUFS/ENOSR 890Sstevel@tonic-gate * connect: EINTR EINTR ENOBUFS/ENOSR/EINTR 900Sstevel@tonic-gate * getpeername: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 910Sstevel@tonic-gate * getsockname: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 920Sstevel@tonic-gate * (4.X getpeername and getsockname do not fail in practice) 930Sstevel@tonic-gate * getsockopt: ENOMEM/ENOSR - ENOBUFS/ENOSR 940Sstevel@tonic-gate * listen: - - ENOBUFS 950Sstevel@tonic-gate * recv: ENOMEM/ENOSR/EINTR EINTR ENOBUFS/ENOMEM/ENOSR/ 960Sstevel@tonic-gate * EINTR 970Sstevel@tonic-gate * send: ENOMEM/ENOSR/EINTR ENOBUFS/EINTR ENOBUFS/ENOMEM/ENOSR/ 980Sstevel@tonic-gate * EINTR 990Sstevel@tonic-gate * setsockopt: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 1000Sstevel@tonic-gate * shutdown: ENOMEM/ENOSR - ENOBUFS/ENOSR 1010Sstevel@tonic-gate * socket: ENOMEM/ENOSR ENOBUFS ENOBUFS/ENOMEM/ENOSR 1020Sstevel@tonic-gate * socketpair: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 1030Sstevel@tonic-gate * 1040Sstevel@tonic-gate * Resolution. When allocation fails: 1050Sstevel@tonic-gate * recv: return EINTR 1060Sstevel@tonic-gate * send: return EINTR 1070Sstevel@tonic-gate * connect, accept: EINTR 1080Sstevel@tonic-gate * bind, listen, shutdown (unbind, unix_close, disconnect): sleep 1090Sstevel@tonic-gate * socket, socketpair: ENOBUFS 1100Sstevel@tonic-gate * getpeername, getsockname: sleep 1110Sstevel@tonic-gate * getsockopt, setsockopt: sleep 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate #ifdef SOCK_TEST 1150Sstevel@tonic-gate /* 1160Sstevel@tonic-gate * Variables that make sockfs do something other than the standard TPI 1170Sstevel@tonic-gate * for the AF_INET transports. 1180Sstevel@tonic-gate * 1190Sstevel@tonic-gate * solisten_tpi_tcp: 1200Sstevel@tonic-gate * TCP can handle a O_T_BIND_REQ with an increased backlog even though 1210Sstevel@tonic-gate * the transport is already bound. This is needed to avoid loosing the 1220Sstevel@tonic-gate * port number should listen() do a T_UNBIND_REQ followed by a 1230Sstevel@tonic-gate * O_T_BIND_REQ. 1240Sstevel@tonic-gate * 1250Sstevel@tonic-gate * soconnect_tpi_udp: 1260Sstevel@tonic-gate * UDP and ICMP can handle a T_CONN_REQ. 1270Sstevel@tonic-gate * This is needed to make the sequence of connect(), getsockname() 1280Sstevel@tonic-gate * return the local IP address used to send packets to the connected to 1290Sstevel@tonic-gate * destination. 1300Sstevel@tonic-gate * 1310Sstevel@tonic-gate * soconnect_tpi_tcp: 1320Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ. 1330Sstevel@tonic-gate * Set this to non-zero to send TPI conformant messages to TCP in this 1340Sstevel@tonic-gate * respect. This is a performance optimization. 1350Sstevel@tonic-gate * 1360Sstevel@tonic-gate * soaccept_tpi_tcp: 1370Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without the acceptor being bound. 1380Sstevel@tonic-gate * This is a performance optimization that has been picked up in XTI. 1390Sstevel@tonic-gate * 1400Sstevel@tonic-gate * soaccept_tpi_multioptions: 1410Sstevel@tonic-gate * When inheriting SOL_SOCKET options from the listener to the accepting 1420Sstevel@tonic-gate * socket send them as a single message for AF_INET{,6}. 1430Sstevel@tonic-gate */ 1440Sstevel@tonic-gate int solisten_tpi_tcp = 0; 1450Sstevel@tonic-gate int soconnect_tpi_udp = 0; 1460Sstevel@tonic-gate int soconnect_tpi_tcp = 0; 1470Sstevel@tonic-gate int soaccept_tpi_tcp = 0; 1480Sstevel@tonic-gate int soaccept_tpi_multioptions = 1; 1490Sstevel@tonic-gate #else /* SOCK_TEST */ 1500Sstevel@tonic-gate #define soconnect_tpi_tcp 0 1510Sstevel@tonic-gate #define soconnect_tpi_udp 0 1520Sstevel@tonic-gate #define solisten_tpi_tcp 0 1530Sstevel@tonic-gate #define soaccept_tpi_tcp 0 1540Sstevel@tonic-gate #define soaccept_tpi_multioptions 1 1550Sstevel@tonic-gate #endif /* SOCK_TEST */ 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate #ifdef SOCK_TEST 1580Sstevel@tonic-gate extern int do_useracc; 1590Sstevel@tonic-gate extern clock_t sock_test_timelimit; 1600Sstevel@tonic-gate #endif /* SOCK_TEST */ 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * Some X/Open added checks might have to be backed out to keep SunOS 4.X 1640Sstevel@tonic-gate * applications working. Turn on this flag to disable these checks. 1650Sstevel@tonic-gate */ 1660Sstevel@tonic-gate int xnet_skip_checks = 0; 1670Sstevel@tonic-gate int xnet_check_print = 0; 1680Sstevel@tonic-gate int xnet_truncate_print = 0; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate extern void sigintr(k_sigset_t *, int); 1710Sstevel@tonic-gate extern void sigunintr(k_sigset_t *); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate extern void *nl7c_lookup_addr(void *, t_uscalar_t); 1740Sstevel@tonic-gate extern void *nl7c_add_addr(void *, t_uscalar_t); 1750Sstevel@tonic-gate extern void nl7c_listener_addr(void *, queue_t *); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate static int sotpi_unbind(struct sonode *, int); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* TPI sockfs sonode operations */ 1800Sstevel@tonic-gate static int sotpi_accept(struct sonode *, int, struct sonode **); 1810Sstevel@tonic-gate static int sotpi_bind(struct sonode *, struct sockaddr *, socklen_t, 1820Sstevel@tonic-gate int); 1830Sstevel@tonic-gate static int sotpi_connect(struct sonode *, const struct sockaddr *, 1840Sstevel@tonic-gate socklen_t, int, int); 1850Sstevel@tonic-gate static int sotpi_listen(struct sonode *, int); 1860Sstevel@tonic-gate static int sotpi_sendmsg(struct sonode *, struct nmsghdr *, 1870Sstevel@tonic-gate struct uio *); 1880Sstevel@tonic-gate static int sotpi_shutdown(struct sonode *, int); 1890Sstevel@tonic-gate static int sotpi_getsockname(struct sonode *); 190*741Smasputra static int sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t, 191*741Smasputra struct uio *, void *, t_uscalar_t, int); 192*741Smasputra static int sodgram_direct(struct sonode *, struct sockaddr *, 193*741Smasputra socklen_t, struct uio *, int); 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate sonodeops_t sotpi_sonodeops = { 1960Sstevel@tonic-gate sotpi_accept, /* sop_accept */ 1970Sstevel@tonic-gate sotpi_bind, /* sop_bind */ 1980Sstevel@tonic-gate sotpi_listen, /* sop_listen */ 1990Sstevel@tonic-gate sotpi_connect, /* sop_connect */ 2000Sstevel@tonic-gate sotpi_recvmsg, /* sop_recvmsg */ 2010Sstevel@tonic-gate sotpi_sendmsg, /* sop_sendmsg */ 2020Sstevel@tonic-gate sotpi_getpeername, /* sop_getpeername */ 2030Sstevel@tonic-gate sotpi_getsockname, /* sop_getsockname */ 2040Sstevel@tonic-gate sotpi_shutdown, /* sop_shutdown */ 2050Sstevel@tonic-gate sotpi_getsockopt, /* sop_getsockopt */ 2060Sstevel@tonic-gate sotpi_setsockopt /* sop_setsockopt */ 2070Sstevel@tonic-gate }; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * Common create code for socket and accept. If tso is set the values 2110Sstevel@tonic-gate * from that node is used instead of issuing a T_INFO_REQ. 2120Sstevel@tonic-gate * 2130Sstevel@tonic-gate * Assumes that the caller has a VN_HOLD on accessvp. 2140Sstevel@tonic-gate * The VN_RELE will occur either when sotpi_create() fails or when 2150Sstevel@tonic-gate * the returned sonode is freed. 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate struct sonode * 2180Sstevel@tonic-gate sotpi_create(vnode_t *accessvp, int domain, int type, int protocol, int version, 2190Sstevel@tonic-gate struct sonode *tso, int *errorp) 2200Sstevel@tonic-gate { 2210Sstevel@tonic-gate struct sonode *so; 2220Sstevel@tonic-gate vnode_t *vp; 2230Sstevel@tonic-gate int flags, error; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate ASSERT(accessvp != NULL); 2260Sstevel@tonic-gate vp = makesockvp(accessvp, domain, type, protocol); 2270Sstevel@tonic-gate ASSERT(vp != NULL); 2280Sstevel@tonic-gate so = VTOSO(vp); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate flags = FREAD|FWRITE; 231*741Smasputra 232*741Smasputra if ((type == SOCK_STREAM || type == SOCK_DGRAM) && 233*741Smasputra (domain == AF_INET || domain == AF_INET6) && 234*741Smasputra (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP || 235*741Smasputra protocol == IPPROTO_IP)) { 236*741Smasputra /* Tell tcp or udp that it's talking to sockets */ 237*741Smasputra flags |= SO_SOCKSTR; 238*741Smasputra 239*741Smasputra /* 240*741Smasputra * Here we indicate to socktpi_open() our attempt to 241*741Smasputra * make direct calls between sockfs and transport. 242*741Smasputra * The final decision is left to socktpi_open(). 243*741Smasputra */ 244*741Smasputra so->so_state |= SS_DIRECT; 245*741Smasputra 246*741Smasputra ASSERT(so->so_type != SOCK_DGRAM || tso == NULL); 247*741Smasputra if (so->so_type == SOCK_STREAM && tso != NULL) { 248*741Smasputra if (tso->so_state & SS_DIRECT) { 249*741Smasputra /* 250*741Smasputra * Inherit SS_DIRECT from listener and pass 251*741Smasputra * SO_ACCEPTOR open flag to tcp, indicating 252*741Smasputra * that this is an accept fast-path instance. 253*741Smasputra */ 254*741Smasputra flags |= SO_ACCEPTOR; 255*741Smasputra } else { 256*741Smasputra /* 257*741Smasputra * SS_DIRECT is not set on listener, meaning 258*741Smasputra * that the listener has been converted from 259*741Smasputra * a socket to a stream. Ensure that the 260*741Smasputra * acceptor inherits these settings. 261*741Smasputra */ 262*741Smasputra so->so_state &= ~SS_DIRECT; 263*741Smasputra flags &= ~SO_SOCKSTR; 264*741Smasputra } 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * Tell local transport that it is talking to sockets. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 2720Sstevel@tonic-gate flags |= SO_SOCKSTR; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate if (error = socktpi_open(&vp, flags, CRED())) { 2760Sstevel@tonic-gate VN_RELE(vp); 2770Sstevel@tonic-gate *errorp = error; 2780Sstevel@tonic-gate return (NULL); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate if (error = so_strinit(so, tso)) { 2820Sstevel@tonic-gate (void) VOP_CLOSE(vp, 0, 1, 0, CRED()); 2830Sstevel@tonic-gate VN_RELE(vp); 2840Sstevel@tonic-gate *errorp = error; 2850Sstevel@tonic-gate return (NULL); 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate if (version == SOV_DEFAULT) 2890Sstevel@tonic-gate version = so_default_version; 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate so->so_version = (short)version; 2920Sstevel@tonic-gate return (so); 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate /* 2960Sstevel@tonic-gate * Bind the socket to an unspecified address in sockfs only. 2970Sstevel@tonic-gate * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't 2980Sstevel@tonic-gate * required in all cases. 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate static void 3010Sstevel@tonic-gate so_automatic_bind(struct sonode *so) 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 3060Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 3070Sstevel@tonic-gate ASSERT(so->so_unbind_mp); 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 3100Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 3110Sstevel@tonic-gate so->so_laddr_sa->sa_family = so->so_family; 3120Sstevel@tonic-gate so->so_state |= SS_ISBOUND; 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate /* 3170Sstevel@tonic-gate * bind the socket. 3180Sstevel@tonic-gate * 3190Sstevel@tonic-gate * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2 3200Sstevel@tonic-gate * are passed in we allow rebinding. Note that for backwards compatibility 3210Sstevel@tonic-gate * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind. 3220Sstevel@tonic-gate * Thus the rebinding code is currently not executed. 3230Sstevel@tonic-gate * 3240Sstevel@tonic-gate * The constraints for rebinding are: 3250Sstevel@tonic-gate * - it is a SOCK_DGRAM, or 3260Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 3270Sstevel@tonic-gate * and no listen() has been done. 3280Sstevel@tonic-gate * This rebinding code was added based on some language in the XNET book 3290Sstevel@tonic-gate * about not returning EINVAL it the protocol allows rebinding. However, 3300Sstevel@tonic-gate * this language is not present in the Posix socket draft. Thus maybe the 3310Sstevel@tonic-gate * rebinding logic should be deleted from the source. 3320Sstevel@tonic-gate * 3330Sstevel@tonic-gate * A null "name" can be used to unbind the socket if: 3340Sstevel@tonic-gate * - it is a SOCK_DGRAM, or 3350Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 3360Sstevel@tonic-gate * and no listen() has been done. 3370Sstevel@tonic-gate */ 3380Sstevel@tonic-gate static int 3390Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name, 3400Sstevel@tonic-gate socklen_t namelen, int backlog, int flags) 3410Sstevel@tonic-gate { 3420Sstevel@tonic-gate struct T_bind_req bind_req; 3430Sstevel@tonic-gate struct T_bind_ack *bind_ack; 3440Sstevel@tonic-gate int error = 0; 3450Sstevel@tonic-gate mblk_t *mp; 3460Sstevel@tonic-gate void *addr; 3470Sstevel@tonic-gate t_uscalar_t addrlen; 3480Sstevel@tonic-gate int unbind_on_err = 1; 3490Sstevel@tonic-gate boolean_t clear_acceptconn_on_err = B_FALSE; 3500Sstevel@tonic-gate boolean_t restore_backlog_on_err = B_FALSE; 3510Sstevel@tonic-gate int save_so_backlog; 3520Sstevel@tonic-gate t_scalar_t PRIM_type = O_T_BIND_REQ; 3530Sstevel@tonic-gate boolean_t tcp_udp_xport; 3540Sstevel@tonic-gate void *nl7c = NULL; 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n", 3570Sstevel@tonic-gate so, name, namelen, backlog, flags, 3580Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) { 3630Sstevel@tonic-gate mutex_enter(&so->so_lock); 3640Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 3650Sstevel@tonic-gate } else { 3660Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 3670Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * Make sure that there is a preallocated unbind_req message 3720Sstevel@tonic-gate * before binding. This message allocated when the socket is 3730Sstevel@tonic-gate * created but it might be have been consumed. 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate if (so->so_unbind_mp == NULL) { 3760Sstevel@tonic-gate dprintso(so, 1, ("sobind: allocating unbind_req\n")); 3770Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 3780Sstevel@tonic-gate so->so_unbind_mp = 3790Sstevel@tonic-gate soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP); 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate if (flags & _SOBIND_REBIND) { 3830Sstevel@tonic-gate /* 3840Sstevel@tonic-gate * Called from solisten after doing an sotpi_unbind() or 3850Sstevel@tonic-gate * potentially without the unbind (latter for AF_INET{,6}). 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0); 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 3900Sstevel@tonic-gate ASSERT(so->so_ux_bound_vp); 3910Sstevel@tonic-gate addr = &so->so_ux_laddr; 3920Sstevel@tonic-gate addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 3930Sstevel@tonic-gate dprintso(so, 1, 3940Sstevel@tonic-gate ("sobind rebind UNIX: addrlen %d, addr 0x%p, vp %p\n", 3950Sstevel@tonic-gate addrlen, 3960Sstevel@tonic-gate ((struct so_ux_addr *)addr)->soua_vp, 3970Sstevel@tonic-gate so->so_ux_bound_vp)); 3980Sstevel@tonic-gate } else { 3990Sstevel@tonic-gate addr = so->so_laddr_sa; 4000Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_laddr_len; 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate } else if (flags & _SOBIND_UNSPEC) { 4030Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0); 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate /* 4060Sstevel@tonic-gate * The caller checked SS_ISBOUND but not necessarily 4070Sstevel@tonic-gate * under so_lock 4080Sstevel@tonic-gate */ 4090Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) { 4100Sstevel@tonic-gate /* No error */ 4110Sstevel@tonic-gate goto done; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate /* Set an initial local address */ 4150Sstevel@tonic-gate switch (so->so_family) { 4160Sstevel@tonic-gate case AF_UNIX: 4170Sstevel@tonic-gate /* 4180Sstevel@tonic-gate * Use an address with same size as struct sockaddr 4190Sstevel@tonic-gate * just like BSD. 4200Sstevel@tonic-gate */ 4210Sstevel@tonic-gate so->so_laddr_len = 4220Sstevel@tonic-gate (socklen_t)sizeof (struct sockaddr); 4230Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 4240Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 4250Sstevel@tonic-gate so->so_laddr_sa->sa_family = so->so_family; 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * Pass down an address with the implicit bind 4290Sstevel@tonic-gate * magic number and the rest all zeros. 4300Sstevel@tonic-gate * The transport will return a unique address. 4310Sstevel@tonic-gate */ 4320Sstevel@tonic-gate so->so_ux_laddr.soua_vp = NULL; 4330Sstevel@tonic-gate so->so_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT; 4340Sstevel@tonic-gate addr = &so->so_ux_laddr; 4350Sstevel@tonic-gate addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 4360Sstevel@tonic-gate break; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate case AF_INET: 4390Sstevel@tonic-gate case AF_INET6: 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address. 4420Sstevel@tonic-gate * Set the address in sockfs to have the sa_family. 4430Sstevel@tonic-gate */ 4440Sstevel@tonic-gate so->so_laddr_len = (so->so_family == AF_INET) ? 4450Sstevel@tonic-gate (socklen_t)sizeof (sin_t) : 4460Sstevel@tonic-gate (socklen_t)sizeof (sin6_t); 4470Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 4480Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 4490Sstevel@tonic-gate so->so_laddr_sa->sa_family = so->so_family; 4500Sstevel@tonic-gate addr = NULL; 4510Sstevel@tonic-gate addrlen = 0; 4520Sstevel@tonic-gate break; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate default: 4550Sstevel@tonic-gate /* 4560Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address. 4570Sstevel@tonic-gate * Set the address in sockfs to be zero length. 4580Sstevel@tonic-gate * 4590Sstevel@tonic-gate * Can not assume there is a sa_family for all 4600Sstevel@tonic-gate * protocol families. For example, AF_X25 does not 4610Sstevel@tonic-gate * have a family field. 4620Sstevel@tonic-gate */ 4630Sstevel@tonic-gate so->so_laddr_len = 0; /* XXX correct? */ 4640Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 4650Sstevel@tonic-gate addr = NULL; 4660Sstevel@tonic-gate addrlen = 0; 4670Sstevel@tonic-gate break; 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate } else { 4710Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) { 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * If it is ok to rebind the socket, first unbind 4740Sstevel@tonic-gate * with the transport. A rebind to the NULL address 4750Sstevel@tonic-gate * is interpreted as an unbind. 4760Sstevel@tonic-gate * Note that a bind to NULL in BSD does unbind the 4770Sstevel@tonic-gate * socket but it fails with EINVAL. 4780Sstevel@tonic-gate * Note that regular sockets set SOV_SOCKBSD i.e. 4790Sstevel@tonic-gate * _SOBIND_SOCKBSD gets set here hence no type of 4800Sstevel@tonic-gate * socket does currently allow rebinding. 4810Sstevel@tonic-gate * 4820Sstevel@tonic-gate * If the name is NULL just do an unbind. 4830Sstevel@tonic-gate */ 4840Sstevel@tonic-gate if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) && 4850Sstevel@tonic-gate name != NULL) { 4860Sstevel@tonic-gate error = EINVAL; 4870Sstevel@tonic-gate unbind_on_err = 0; 4880Sstevel@tonic-gate eprintsoline(so, error); 4890Sstevel@tonic-gate goto done; 4900Sstevel@tonic-gate } 4910Sstevel@tonic-gate if ((so->so_mode & SM_CONNREQUIRED) && 4920Sstevel@tonic-gate (so->so_state & SS_CANTREBIND)) { 4930Sstevel@tonic-gate error = EINVAL; 4940Sstevel@tonic-gate unbind_on_err = 0; 4950Sstevel@tonic-gate eprintsoline(so, error); 4960Sstevel@tonic-gate goto done; 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate error = sotpi_unbind(so, 0); 4990Sstevel@tonic-gate if (error) { 5000Sstevel@tonic-gate eprintsoline(so, error); 5010Sstevel@tonic-gate goto done; 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 5040Sstevel@tonic-gate if (name == NULL) { 5050Sstevel@tonic-gate so->so_state &= 5060Sstevel@tonic-gate ~(SS_ISCONNECTED|SS_ISCONNECTING); 5070Sstevel@tonic-gate goto done; 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate /* X/Open requires this check */ 5110Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 5120Sstevel@tonic-gate if (xnet_check_print) { 5130Sstevel@tonic-gate printf("sockfs: X/Open bind state check " 5140Sstevel@tonic-gate "caused EINVAL\n"); 5150Sstevel@tonic-gate } 5160Sstevel@tonic-gate error = EINVAL; 5170Sstevel@tonic-gate goto done; 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate switch (so->so_family) { 5210Sstevel@tonic-gate case AF_UNIX: 5220Sstevel@tonic-gate /* 5230Sstevel@tonic-gate * All AF_UNIX addresses are nul terminated 5240Sstevel@tonic-gate * when copied (copyin_name) in so the minimum 5250Sstevel@tonic-gate * length is 3 bytes. 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate if (name == NULL || 5280Sstevel@tonic-gate (ssize_t)namelen <= sizeof (short) + 1) { 5290Sstevel@tonic-gate error = EISDIR; 5300Sstevel@tonic-gate eprintsoline(so, error); 5310Sstevel@tonic-gate goto done; 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate /* 5340Sstevel@tonic-gate * Verify so_family matches the bound family. 5350Sstevel@tonic-gate * BSD does not check this for AF_UNIX resulting 5360Sstevel@tonic-gate * in funny mknods. 5370Sstevel@tonic-gate */ 5380Sstevel@tonic-gate if (name->sa_family != so->so_family) { 5390Sstevel@tonic-gate error = EAFNOSUPPORT; 5400Sstevel@tonic-gate goto done; 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate break; 5430Sstevel@tonic-gate case AF_INET: 5440Sstevel@tonic-gate if (name == NULL) { 5450Sstevel@tonic-gate error = EINVAL; 5460Sstevel@tonic-gate eprintsoline(so, error); 5470Sstevel@tonic-gate goto done; 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin_t)) { 5500Sstevel@tonic-gate error = name->sa_family != so->so_family ? 5510Sstevel@tonic-gate EAFNOSUPPORT : EINVAL; 5520Sstevel@tonic-gate eprintsoline(so, error); 5530Sstevel@tonic-gate goto done; 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate if ((flags & _SOBIND_XPG4_2) && 5560Sstevel@tonic-gate (name->sa_family != so->so_family)) { 5570Sstevel@tonic-gate /* 5580Sstevel@tonic-gate * This check has to be made for X/Open 5590Sstevel@tonic-gate * sockets however application failures have 5600Sstevel@tonic-gate * been observed when it is applied to 5610Sstevel@tonic-gate * all sockets. 5620Sstevel@tonic-gate */ 5630Sstevel@tonic-gate error = EAFNOSUPPORT; 5640Sstevel@tonic-gate eprintsoline(so, error); 5650Sstevel@tonic-gate goto done; 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * Force a zero sa_family to match so_family. 5690Sstevel@tonic-gate * 5700Sstevel@tonic-gate * Some programs like inetd(1M) don't set the 5710Sstevel@tonic-gate * family field. Other programs leave 5720Sstevel@tonic-gate * sin_family set to garbage - SunOS 4.X does 5730Sstevel@tonic-gate * not check the family field on a bind. 5740Sstevel@tonic-gate * We use the family field that 5750Sstevel@tonic-gate * was passed in to the socket() call. 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate name->sa_family = so->so_family; 5780Sstevel@tonic-gate break; 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate case AF_INET6: { 5810Sstevel@tonic-gate #ifdef DEBUG 5820Sstevel@tonic-gate sin6_t *sin6 = (sin6_t *)name; 5830Sstevel@tonic-gate #endif /* DEBUG */ 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate if (name == NULL) { 5860Sstevel@tonic-gate error = EINVAL; 5870Sstevel@tonic-gate eprintsoline(so, error); 5880Sstevel@tonic-gate goto done; 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin6_t)) { 5910Sstevel@tonic-gate error = name->sa_family != so->so_family ? 5920Sstevel@tonic-gate EAFNOSUPPORT : EINVAL; 5930Sstevel@tonic-gate eprintsoline(so, error); 5940Sstevel@tonic-gate goto done; 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate if (name->sa_family != so->so_family) { 5970Sstevel@tonic-gate /* 5980Sstevel@tonic-gate * With IPv6 we require the family to match 5990Sstevel@tonic-gate * unlike in IPv4. 6000Sstevel@tonic-gate */ 6010Sstevel@tonic-gate error = EAFNOSUPPORT; 6020Sstevel@tonic-gate eprintsoline(so, error); 6030Sstevel@tonic-gate goto done; 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate #ifdef DEBUG 6060Sstevel@tonic-gate /* 6070Sstevel@tonic-gate * Verify that apps don't forget to clear 6080Sstevel@tonic-gate * sin6_scope_id etc 6090Sstevel@tonic-gate */ 6100Sstevel@tonic-gate if (sin6->sin6_scope_id != 0 && 6110Sstevel@tonic-gate !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 6120Sstevel@tonic-gate cmn_err(CE_WARN, 6130Sstevel@tonic-gate "bind with uninitialized sin6_scope_id " 6140Sstevel@tonic-gate "(%d) on socket. Pid = %d\n", 6150Sstevel@tonic-gate (int)sin6->sin6_scope_id, 6160Sstevel@tonic-gate (int)curproc->p_pid); 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate if (sin6->__sin6_src_id != 0) { 6190Sstevel@tonic-gate cmn_err(CE_WARN, 6200Sstevel@tonic-gate "bind with uninitialized __sin6_src_id " 6210Sstevel@tonic-gate "(%d) on socket. Pid = %d\n", 6220Sstevel@tonic-gate (int)sin6->__sin6_src_id, 6230Sstevel@tonic-gate (int)curproc->p_pid); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate #endif /* DEBUG */ 6260Sstevel@tonic-gate break; 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate default: 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * Don't do any length or sa_family check to allow 6310Sstevel@tonic-gate * non-sockaddr style addresses. 6320Sstevel@tonic-gate */ 6330Sstevel@tonic-gate if (name == NULL) { 6340Sstevel@tonic-gate error = EINVAL; 6350Sstevel@tonic-gate eprintsoline(so, error); 6360Sstevel@tonic-gate goto done; 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate break; 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate if (namelen > (t_uscalar_t)so->so_laddr_maxlen) { 6420Sstevel@tonic-gate error = ENAMETOOLONG; 6430Sstevel@tonic-gate eprintsoline(so, error); 6440Sstevel@tonic-gate goto done; 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate /* 6470Sstevel@tonic-gate * Save local address. 6480Sstevel@tonic-gate */ 6490Sstevel@tonic-gate so->so_laddr_len = (socklen_t)namelen; 6500Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 6510Sstevel@tonic-gate bcopy(name, so->so_laddr_sa, namelen); 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate addr = so->so_laddr_sa; 6540Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_laddr_len; 6550Sstevel@tonic-gate switch (so->so_family) { 6560Sstevel@tonic-gate case AF_INET6: 6570Sstevel@tonic-gate case AF_INET: 6580Sstevel@tonic-gate break; 6590Sstevel@tonic-gate case AF_UNIX: { 6600Sstevel@tonic-gate struct sockaddr_un *soun = 6610Sstevel@tonic-gate (struct sockaddr_un *)so->so_laddr_sa; 6620Sstevel@tonic-gate struct vnode *vp; 6630Sstevel@tonic-gate struct vattr vattr; 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate ASSERT(so->so_ux_bound_vp == NULL); 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * Create vnode for the specified path name. 6680Sstevel@tonic-gate * Keep vnode held with a reference in so_ux_bound_vp. 6690Sstevel@tonic-gate * Use the vnode pointer as the address used in the 6700Sstevel@tonic-gate * bind with the transport. 6710Sstevel@tonic-gate * 6720Sstevel@tonic-gate * Use the same mode as in BSD. In particular this does 6730Sstevel@tonic-gate * not observe the umask. 6740Sstevel@tonic-gate */ 6750Sstevel@tonic-gate /* MAXPATHLEN + soun_family + nul termination */ 6760Sstevel@tonic-gate if (so->so_laddr_len > 6770Sstevel@tonic-gate (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) { 6780Sstevel@tonic-gate error = ENAMETOOLONG; 6790Sstevel@tonic-gate eprintsoline(so, error); 6800Sstevel@tonic-gate goto done; 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate vattr.va_type = VSOCK; 6830Sstevel@tonic-gate vattr.va_mode = 0777 & ~u.u_cmask; 6840Sstevel@tonic-gate vattr.va_mask = AT_TYPE|AT_MODE; 6850Sstevel@tonic-gate /* NOTE: holding so_lock */ 6860Sstevel@tonic-gate error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr, 6870Sstevel@tonic-gate EXCL, 0, &vp, CRMKNOD, 0, 0); 6880Sstevel@tonic-gate if (error) { 6890Sstevel@tonic-gate if (error == EEXIST) 6900Sstevel@tonic-gate error = EADDRINUSE; 6910Sstevel@tonic-gate eprintsoline(so, error); 6920Sstevel@tonic-gate goto done; 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate /* 6950Sstevel@tonic-gate * Establish pointer from the underlying filesystem 6960Sstevel@tonic-gate * vnode to the socket node. 6970Sstevel@tonic-gate * so_ux_bound_vp and v_stream->sd_vnode form the 6980Sstevel@tonic-gate * cross-linkage between the underlying filesystem 6990Sstevel@tonic-gate * node and the socket node. 7000Sstevel@tonic-gate */ 7010Sstevel@tonic-gate ASSERT(SOTOV(so)->v_stream); 7020Sstevel@tonic-gate mutex_enter(&vp->v_lock); 7030Sstevel@tonic-gate vp->v_stream = SOTOV(so)->v_stream; 7040Sstevel@tonic-gate so->so_ux_bound_vp = vp; 7050Sstevel@tonic-gate mutex_exit(&vp->v_lock); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate /* 7080Sstevel@tonic-gate * Use the vnode pointer value as a unique address 7090Sstevel@tonic-gate * (together with the magic number to avoid conflicts 7100Sstevel@tonic-gate * with implicit binds) in the transport provider. 7110Sstevel@tonic-gate */ 7120Sstevel@tonic-gate so->so_ux_laddr.soua_vp = (void *)so->so_ux_bound_vp; 7130Sstevel@tonic-gate so->so_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT; 7140Sstevel@tonic-gate addr = &so->so_ux_laddr; 7150Sstevel@tonic-gate addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 7160Sstevel@tonic-gate dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n", 7170Sstevel@tonic-gate addrlen, 7180Sstevel@tonic-gate ((struct so_ux_addr *)addr)->soua_vp)); 7190Sstevel@tonic-gate break; 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate } /* end switch (so->so_family) */ 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate /* 7250Sstevel@tonic-gate * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since 7260Sstevel@tonic-gate * the transport can start passing up T_CONN_IND messages 7270Sstevel@tonic-gate * as soon as it receives the bind req and strsock_proto() 7280Sstevel@tonic-gate * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs. 7290Sstevel@tonic-gate */ 7300Sstevel@tonic-gate if (flags & _SOBIND_LISTEN) { 7310Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0) 7320Sstevel@tonic-gate clear_acceptconn_on_err = B_TRUE; 7330Sstevel@tonic-gate save_so_backlog = so->so_backlog; 7340Sstevel@tonic-gate restore_backlog_on_err = B_TRUE; 7350Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN; 7360Sstevel@tonic-gate so->so_backlog = backlog; 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate /* 7400Sstevel@tonic-gate * If NL7C addr(s) have been configured check for addr/port match, 7410Sstevel@tonic-gate * or if an implicit NL7C socket via AF_NCA mark socket as NL7C. 7420Sstevel@tonic-gate * 7430Sstevel@tonic-gate * NL7C supports the TCP transport only so check AF_INET and AF_INET6 7440Sstevel@tonic-gate * family sockets only. If match mark as such. 7450Sstevel@tonic-gate */ 7460Sstevel@tonic-gate if ((nl7c_enabled && addr != NULL && 7470Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6) && 7480Sstevel@tonic-gate (nl7c = nl7c_lookup_addr(addr, addrlen))) || 7490Sstevel@tonic-gate so->so_nl7c_flags == NL7C_AF_NCA) { 7500Sstevel@tonic-gate /* 7510Sstevel@tonic-gate * NL7C is not supported in non-global zones, 7520Sstevel@tonic-gate * we enforce this restriction here. 7530Sstevel@tonic-gate */ 7540Sstevel@tonic-gate if (so->so_zoneid == GLOBAL_ZONEID) { 7550Sstevel@tonic-gate /* An NL7C socket, mark it */ 7560Sstevel@tonic-gate so->so_nl7c_flags |= NL7C_ENABLED; 7570Sstevel@tonic-gate } else 7580Sstevel@tonic-gate nl7c = NULL; 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate /* 7610Sstevel@tonic-gate * We send a T_BIND_REQ for TCP/UDP since we know it supports it, 7620Sstevel@tonic-gate * for other transports we will send in a O_T_BIND_REQ. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate if (tcp_udp_xport && 7650Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6)) 7660Sstevel@tonic-gate PRIM_type = T_BIND_REQ; 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate bind_req.PRIM_type = PRIM_type; 7690Sstevel@tonic-gate bind_req.ADDR_length = addrlen; 7700Sstevel@tonic-gate bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req); 7710Sstevel@tonic-gate bind_req.CONIND_number = backlog; 7720Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 7730Sstevel@tonic-gate mp = soallocproto2(&bind_req, sizeof (bind_req), 7740Sstevel@tonic-gate addr, addrlen, 0, _ALLOC_SLEEP); 7750Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 7760Sstevel@tonic-gate /* Done using so_laddr_sa - can drop the lock */ 7770Sstevel@tonic-gate mutex_exit(&so->so_lock); 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 7800Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 7810Sstevel@tonic-gate if (error) { 7820Sstevel@tonic-gate eprintsoline(so, error); 7830Sstevel@tonic-gate mutex_enter(&so->so_lock); 7840Sstevel@tonic-gate goto done; 7850Sstevel@tonic-gate } 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate mutex_enter(&so->so_lock); 7880Sstevel@tonic-gate error = sowaitprim(so, PRIM_type, T_BIND_ACK, 7890Sstevel@tonic-gate (t_uscalar_t)sizeof (*bind_ack), &mp, 0); 7900Sstevel@tonic-gate if (error) { 7910Sstevel@tonic-gate eprintsoline(so, error); 7920Sstevel@tonic-gate goto done; 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate ASSERT(mp); 7950Sstevel@tonic-gate /* 7960Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 7970Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the bind 7980Sstevel@tonic-gate * is allowed to complete. 7990Sstevel@tonic-gate */ 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate /* Mark as bound. This will be undone if we detect errors below. */ 8020Sstevel@tonic-gate if (flags & _SOBIND_NOXLATE) { 8030Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 8040Sstevel@tonic-gate so->so_state |= SS_FADDR_NOXLATE; 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND)); 8070Sstevel@tonic-gate so->so_state |= SS_ISBOUND; 8080Sstevel@tonic-gate ASSERT(so->so_unbind_mp); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate /* note that we've already set SS_ACCEPTCONN above */ 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * Recompute addrlen - an unspecied bind sent down an 8140Sstevel@tonic-gate * address of length zero but we expect the appropriate length 8150Sstevel@tonic-gate * in return. 8160Sstevel@tonic-gate */ 8170Sstevel@tonic-gate addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ? 8180Sstevel@tonic-gate sizeof (so->so_ux_laddr) : so->so_laddr_len); 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate bind_ack = (struct T_bind_ack *)mp->b_rptr; 8210Sstevel@tonic-gate /* 8220Sstevel@tonic-gate * The alignment restriction is really too strict but 8230Sstevel@tonic-gate * we want enough alignment to inspect the fields of 8240Sstevel@tonic-gate * a sockaddr_in. 8250Sstevel@tonic-gate */ 8260Sstevel@tonic-gate addr = sogetoff(mp, bind_ack->ADDR_offset, 8270Sstevel@tonic-gate bind_ack->ADDR_length, 8280Sstevel@tonic-gate __TPI_ALIGN_SIZE); 8290Sstevel@tonic-gate if (addr == NULL) { 8300Sstevel@tonic-gate freemsg(mp); 8310Sstevel@tonic-gate error = EPROTO; 8320Sstevel@tonic-gate eprintsoline(so, error); 8330Sstevel@tonic-gate goto done; 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate if (!(flags & _SOBIND_UNSPEC)) { 8360Sstevel@tonic-gate /* 8370Sstevel@tonic-gate * Verify that the transport didn't return something we 8380Sstevel@tonic-gate * did not want e.g. an address other than what we asked for. 8390Sstevel@tonic-gate * 8400Sstevel@tonic-gate * NOTE: These checks would go away if/when we switch to 8410Sstevel@tonic-gate * using the new TPI (in which the transport would fail 8420Sstevel@tonic-gate * the request instead of assigning a different address). 8430Sstevel@tonic-gate * 8440Sstevel@tonic-gate * NOTE2: For protocols that we don't know (i.e. any 8450Sstevel@tonic-gate * other than AF_INET6, AF_INET and AF_UNIX), we 8460Sstevel@tonic-gate * cannot know if the transport should be expected to 8470Sstevel@tonic-gate * return the same address as that requested. 8480Sstevel@tonic-gate * 8490Sstevel@tonic-gate * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send 8500Sstevel@tonic-gate * down a T_BIND_REQ. We use O_T_BIND_REQ for others. 8510Sstevel@tonic-gate * 8520Sstevel@tonic-gate * For example, in the case of netatalk it may be 8530Sstevel@tonic-gate * inappropriate for the transport to return the 8540Sstevel@tonic-gate * requested address (as it may have allocated a local 8550Sstevel@tonic-gate * port number in behaviour similar to that of an 8560Sstevel@tonic-gate * AF_INET bind request with a port number of zero). 8570Sstevel@tonic-gate * 8580Sstevel@tonic-gate * Given the definition of O_T_BIND_REQ, where the 8590Sstevel@tonic-gate * transport may bind to an address other than the 8600Sstevel@tonic-gate * requested address, it's not possible to determine 8610Sstevel@tonic-gate * whether a returned address that differs from the 8620Sstevel@tonic-gate * requested address is a reason to fail (because the 8630Sstevel@tonic-gate * requested address was not available) or succeed 8640Sstevel@tonic-gate * (because the transport allocated an appropriate 8650Sstevel@tonic-gate * address and/or port). 8660Sstevel@tonic-gate * 8670Sstevel@tonic-gate * sockfs currently requires that the transport return 8680Sstevel@tonic-gate * the requested address in the T_BIND_ACK, unless 8690Sstevel@tonic-gate * there is code here to allow for any discrepancy. 8700Sstevel@tonic-gate * Such code exists for AF_INET and AF_INET6. 8710Sstevel@tonic-gate * 8720Sstevel@tonic-gate * Netatalk chooses to return the requested address 8730Sstevel@tonic-gate * rather than the (correct) allocated address. This 8740Sstevel@tonic-gate * means that netatalk violates the TPI specification 8750Sstevel@tonic-gate * (and would not function correctly if used from a 8760Sstevel@tonic-gate * TLI application), but it does mean that it works 8770Sstevel@tonic-gate * with sockfs. 8780Sstevel@tonic-gate * 8790Sstevel@tonic-gate * As noted above, using the newer XTI bind primitive 8800Sstevel@tonic-gate * (T_BIND_REQ) in preference to O_T_BIND_REQ would 8810Sstevel@tonic-gate * allow sockfs to be more sure about whether or not 8820Sstevel@tonic-gate * the bind request had succeeded (as transports are 8830Sstevel@tonic-gate * not permitted to bind to a different address than 8840Sstevel@tonic-gate * that requested - they must return failure). 8850Sstevel@tonic-gate * Unfortunately, support for T_BIND_REQ may not be 8860Sstevel@tonic-gate * present in all transport implementations (netatalk, 8870Sstevel@tonic-gate * for example, doesn't have it), making the 8880Sstevel@tonic-gate * transition difficult. 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate if (bind_ack->ADDR_length != addrlen) { 8910Sstevel@tonic-gate /* Assumes that the requested address was in use */ 8920Sstevel@tonic-gate freemsg(mp); 8930Sstevel@tonic-gate error = EADDRINUSE; 8940Sstevel@tonic-gate eprintsoline(so, error); 8950Sstevel@tonic-gate goto done; 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate switch (so->so_family) { 8990Sstevel@tonic-gate case AF_INET6: 9000Sstevel@tonic-gate case AF_INET: { 9010Sstevel@tonic-gate sin_t *rname, *aname; 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate rname = (sin_t *)addr; 9040Sstevel@tonic-gate aname = (sin_t *)so->so_laddr_sa; 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate /* 9070Sstevel@tonic-gate * Take advantage of the alignment 9080Sstevel@tonic-gate * of sin_port and sin6_port which fall 9090Sstevel@tonic-gate * in the same place in their data structures. 9100Sstevel@tonic-gate * Just use sin_port for either address family. 9110Sstevel@tonic-gate * 9120Sstevel@tonic-gate * This may become a problem if (heaven forbid) 9130Sstevel@tonic-gate * there's a separate ipv6port_reserved... :-P 9140Sstevel@tonic-gate * 9150Sstevel@tonic-gate * Binding to port 0 has the semantics of letting 9160Sstevel@tonic-gate * the transport bind to any port. 9170Sstevel@tonic-gate * 9180Sstevel@tonic-gate * If the transport is TCP or UDP since we had sent 9190Sstevel@tonic-gate * a T_BIND_REQ we would not get a port other than 9200Sstevel@tonic-gate * what we asked for. 9210Sstevel@tonic-gate */ 9220Sstevel@tonic-gate if (tcp_udp_xport) { 9230Sstevel@tonic-gate /* 9240Sstevel@tonic-gate * Pick up the new port number if we bound to 9250Sstevel@tonic-gate * port 0. 9260Sstevel@tonic-gate */ 9270Sstevel@tonic-gate if (aname->sin_port == 0) 9280Sstevel@tonic-gate aname->sin_port = rname->sin_port; 9290Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 9300Sstevel@tonic-gate break; 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate if (aname->sin_port != 0 && 9330Sstevel@tonic-gate aname->sin_port != rname->sin_port) { 9340Sstevel@tonic-gate freemsg(mp); 9350Sstevel@tonic-gate error = EADDRINUSE; 9360Sstevel@tonic-gate eprintsoline(so, error); 9370Sstevel@tonic-gate goto done; 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate /* 9400Sstevel@tonic-gate * Pick up the new port number if we bound to port 0. 9410Sstevel@tonic-gate */ 9420Sstevel@tonic-gate aname->sin_port = rname->sin_port; 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate /* 9450Sstevel@tonic-gate * Unfortunately, addresses aren't _quite_ the same. 9460Sstevel@tonic-gate */ 9470Sstevel@tonic-gate if (so->so_family == AF_INET) { 9480Sstevel@tonic-gate if (aname->sin_addr.s_addr != 9490Sstevel@tonic-gate rname->sin_addr.s_addr) { 9500Sstevel@tonic-gate freemsg(mp); 9510Sstevel@tonic-gate error = EADDRNOTAVAIL; 9520Sstevel@tonic-gate eprintsoline(so, error); 9530Sstevel@tonic-gate goto done; 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate } else { 9560Sstevel@tonic-gate sin6_t *rname6 = (sin6_t *)rname; 9570Sstevel@tonic-gate sin6_t *aname6 = (sin6_t *)aname; 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr, 9600Sstevel@tonic-gate &rname6->sin6_addr)) { 9610Sstevel@tonic-gate freemsg(mp); 9620Sstevel@tonic-gate error = EADDRNOTAVAIL; 9630Sstevel@tonic-gate eprintsoline(so, error); 9640Sstevel@tonic-gate goto done; 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate break; 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate case AF_UNIX: 9700Sstevel@tonic-gate if (bcmp(addr, &so->so_ux_laddr, addrlen) != 0) { 9710Sstevel@tonic-gate freemsg(mp); 9720Sstevel@tonic-gate error = EADDRINUSE; 9730Sstevel@tonic-gate eprintsoline(so, error); 9740Sstevel@tonic-gate eprintso(so, 9750Sstevel@tonic-gate ("addrlen %d, addr 0x%x, vp %p\n", 9760Sstevel@tonic-gate addrlen, *((int *)addr), 9770Sstevel@tonic-gate so->so_ux_bound_vp)); 9780Sstevel@tonic-gate goto done; 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 9810Sstevel@tonic-gate break; 9820Sstevel@tonic-gate default: 9830Sstevel@tonic-gate /* 9840Sstevel@tonic-gate * NOTE: This assumes that addresses can be 9850Sstevel@tonic-gate * byte-compared for equivalence. 9860Sstevel@tonic-gate */ 9870Sstevel@tonic-gate if (bcmp(addr, so->so_laddr_sa, addrlen) != 0) { 9880Sstevel@tonic-gate freemsg(mp); 9890Sstevel@tonic-gate error = EADDRINUSE; 9900Sstevel@tonic-gate eprintsoline(so, error); 9910Sstevel@tonic-gate goto done; 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate /* 9940Sstevel@tonic-gate * Don't mark SS_LADDR_VALID, as we cannot be 9950Sstevel@tonic-gate * sure that the returned address is the real 9960Sstevel@tonic-gate * bound address when talking to an unknown 9970Sstevel@tonic-gate * transport. 9980Sstevel@tonic-gate */ 9990Sstevel@tonic-gate break; 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate } else { 10020Sstevel@tonic-gate /* 10030Sstevel@tonic-gate * Save for returned address for getsockname. 10040Sstevel@tonic-gate * Needed for unspecific bind unless transport supports 10050Sstevel@tonic-gate * the TI_GETMYNAME ioctl. 10060Sstevel@tonic-gate * Do this for AF_INET{,6} even though they do, as 10070Sstevel@tonic-gate * caching info here is much better performance than 10080Sstevel@tonic-gate * a TPI/STREAMS trip to the transport for getsockname. 10090Sstevel@tonic-gate * Any which can't for some reason _must_ _not_ set 10100Sstevel@tonic-gate * LADDR_VALID here for the caching version of getsockname 10110Sstevel@tonic-gate * to not break; 10120Sstevel@tonic-gate */ 10130Sstevel@tonic-gate switch (so->so_family) { 10140Sstevel@tonic-gate case AF_UNIX: 10150Sstevel@tonic-gate /* 10160Sstevel@tonic-gate * Record the address bound with the transport 10170Sstevel@tonic-gate * for use by socketpair. 10180Sstevel@tonic-gate */ 10190Sstevel@tonic-gate bcopy(addr, &so->so_ux_laddr, addrlen); 10200Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 10210Sstevel@tonic-gate break; 10220Sstevel@tonic-gate case AF_INET: 10230Sstevel@tonic-gate case AF_INET6: 10240Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 10250Sstevel@tonic-gate bcopy(addr, so->so_laddr_sa, so->so_laddr_len); 10260Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 10270Sstevel@tonic-gate break; 10280Sstevel@tonic-gate default: 10290Sstevel@tonic-gate /* 10300Sstevel@tonic-gate * Don't mark SS_LADDR_VALID, as we cannot be 10310Sstevel@tonic-gate * sure that the returned address is the real 10320Sstevel@tonic-gate * bound address when talking to an unknown 10330Sstevel@tonic-gate * transport. 10340Sstevel@tonic-gate */ 10350Sstevel@tonic-gate break; 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate if (nl7c == NULL && (so->so_nl7c_flags & NL7C_AF_NCA) && 10400Sstevel@tonic-gate (so->so_nl7c_flags & NL7C_ENABLED)) { 10410Sstevel@tonic-gate /* 10420Sstevel@tonic-gate * Was an AF_NCA bind() so add it to the addr list for 10430Sstevel@tonic-gate * reporting purposes. 10440Sstevel@tonic-gate */ 10450Sstevel@tonic-gate nl7c = nl7c_add_addr(addr, addrlen); 10460Sstevel@tonic-gate } 10470Sstevel@tonic-gate if (nl7c != NULL) { 10480Sstevel@tonic-gate nl7c_listener_addr(nl7c, strvp2wq(SOTOV(so))); 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate freemsg(mp); 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate done: 10540Sstevel@tonic-gate if (error) { 10550Sstevel@tonic-gate /* reset state & backlog to values held on entry */ 10560Sstevel@tonic-gate if (clear_acceptconn_on_err == B_TRUE) 10570Sstevel@tonic-gate so->so_state &= ~SS_ACCEPTCONN; 10580Sstevel@tonic-gate if (restore_backlog_on_err == B_TRUE) 10590Sstevel@tonic-gate so->so_backlog = save_so_backlog; 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate if (unbind_on_err && so->so_state & SS_ISBOUND) { 10620Sstevel@tonic-gate int err; 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate err = sotpi_unbind(so, 0); 10650Sstevel@tonic-gate /* LINTED - statement has no consequent: if */ 10660Sstevel@tonic-gate if (err) { 10670Sstevel@tonic-gate eprintsoline(so, error); 10680Sstevel@tonic-gate } else { 10690Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate } 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) { 10740Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 10750Sstevel@tonic-gate mutex_exit(&so->so_lock); 10760Sstevel@tonic-gate } else { 10770Sstevel@tonic-gate /* If the caller held the lock don't release it here */ 10780Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 10790Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate return (error); 10820Sstevel@tonic-gate } 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate /* bind the socket */ 1085*741Smasputra static int 10860Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen, 10870Sstevel@tonic-gate int flags) 10880Sstevel@tonic-gate { 10890Sstevel@tonic-gate if ((flags & _SOBIND_SOCKETPAIR) == 0) 10900Sstevel@tonic-gate return (sotpi_bindlisten(so, name, namelen, 0, flags)); 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate flags &= ~_SOBIND_SOCKETPAIR; 10930Sstevel@tonic-gate return (sotpi_bindlisten(so, name, namelen, 1, flags)); 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate /* 10970Sstevel@tonic-gate * Unbind a socket - used when bind() fails, when bind() specifies a NULL 10980Sstevel@tonic-gate * address, or when listen needs to unbind and bind. 10990Sstevel@tonic-gate * If the _SOUNBIND_REBIND flag is specified the addresses are retained 11000Sstevel@tonic-gate * so that a sobind can pick them up. 11010Sstevel@tonic-gate */ 11020Sstevel@tonic-gate static int 11030Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags) 11040Sstevel@tonic-gate { 11050Sstevel@tonic-gate struct T_unbind_req unbind_req; 11060Sstevel@tonic-gate int error = 0; 11070Sstevel@tonic-gate mblk_t *mp; 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n", 11100Sstevel@tonic-gate so, flags, pr_state(so->so_state, so->so_mode))); 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 11130Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 11160Sstevel@tonic-gate error = EINVAL; 11170Sstevel@tonic-gate eprintsoline(so, error); 11180Sstevel@tonic-gate goto done; 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate mutex_exit(&so->so_lock); 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate /* 11240Sstevel@tonic-gate * Flush the read and write side (except stream head read queue) 11250Sstevel@tonic-gate * and send down T_UNBIND_REQ. 11260Sstevel@tonic-gate */ 11270Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW); 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate unbind_req.PRIM_type = T_UNBIND_REQ; 11300Sstevel@tonic-gate mp = soallocproto1(&unbind_req, sizeof (unbind_req), 11310Sstevel@tonic-gate 0, _ALLOC_SLEEP); 11320Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 11330Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 11340Sstevel@tonic-gate mutex_enter(&so->so_lock); 11350Sstevel@tonic-gate if (error) { 11360Sstevel@tonic-gate eprintsoline(so, error); 11370Sstevel@tonic-gate goto done; 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate error = sowaitokack(so, T_UNBIND_REQ); 11410Sstevel@tonic-gate if (error) { 11420Sstevel@tonic-gate eprintsoline(so, error); 11430Sstevel@tonic-gate goto done; 11440Sstevel@tonic-gate } 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate /* 11470Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 11480Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the unbind 11490Sstevel@tonic-gate * is allowed to complete. 11500Sstevel@tonic-gate */ 11510Sstevel@tonic-gate if (!(flags & _SOUNBIND_REBIND)) { 11520Sstevel@tonic-gate /* 11530Sstevel@tonic-gate * Clear out bound address. 11540Sstevel@tonic-gate */ 11550Sstevel@tonic-gate vnode_t *vp; 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate if ((vp = so->so_ux_bound_vp) != NULL) { 11580Sstevel@tonic-gate ASSERT(vp->v_stream); 11590Sstevel@tonic-gate so->so_ux_bound_vp = NULL; 11600Sstevel@tonic-gate vn_rele_stream(vp); 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate /* Clear out address */ 11630Sstevel@tonic-gate so->so_laddr_len = 0; 11640Sstevel@tonic-gate } 11650Sstevel@tonic-gate so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN|SS_LADDR_VALID); 11660Sstevel@tonic-gate done: 11670Sstevel@tonic-gate /* If the caller held the lock don't release it here */ 11680Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 11690Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate return (error); 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate /* 11750Sstevel@tonic-gate * listen on the socket. 11760Sstevel@tonic-gate * For TPI conforming transports this has to first unbind with the transport 11770Sstevel@tonic-gate * and then bind again using the new backlog. 11780Sstevel@tonic-gate */ 11790Sstevel@tonic-gate int 11800Sstevel@tonic-gate sotpi_listen(struct sonode *so, int backlog) 11810Sstevel@tonic-gate { 11820Sstevel@tonic-gate int error = 0; 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n", 11850Sstevel@tonic-gate so, backlog, pr_state(so->so_state, so->so_mode))); 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate if (so->so_serv_type == T_CLTS) 11880Sstevel@tonic-gate return (EOPNOTSUPP); 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate /* 11910Sstevel@tonic-gate * If the socket is ready to accept connections already, then 11920Sstevel@tonic-gate * return without doing anything. This avoids a problem where 11930Sstevel@tonic-gate * a second listen() call fails if a connection is pending and 11940Sstevel@tonic-gate * leaves the socket unbound. Only when we are not unbinding 11950Sstevel@tonic-gate * with the transport can we safely increase the backlog. 11960Sstevel@tonic-gate */ 11970Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN && 11980Sstevel@tonic-gate !((so->so_family == AF_INET || so->so_family == AF_INET6) && 11990Sstevel@tonic-gate /*CONSTCOND*/ 12000Sstevel@tonic-gate !solisten_tpi_tcp)) 12010Sstevel@tonic-gate return (0); 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate if (so->so_state & SS_ISCONNECTED) 12040Sstevel@tonic-gate return (EINVAL); 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate mutex_enter(&so->so_lock); 12070Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate if (backlog < 0) 12100Sstevel@tonic-gate backlog = 0; 12110Sstevel@tonic-gate /* 12120Sstevel@tonic-gate * Use the same qlimit as in BSD. BSD checks the qlimit 12130Sstevel@tonic-gate * before queuing the next connection implying that a 12140Sstevel@tonic-gate * listen(sock, 0) allows one connection to be queued. 12150Sstevel@tonic-gate * BSD also uses 1.5 times the requested backlog. 12160Sstevel@tonic-gate * 12170Sstevel@tonic-gate * XNS Issue 4 required a strict interpretation of the backlog. 12180Sstevel@tonic-gate * This has been waived subsequently for Issue 4 and the change 12190Sstevel@tonic-gate * incorporated in XNS Issue 5. So we aren't required to do 12200Sstevel@tonic-gate * anything special for XPG apps. 12210Sstevel@tonic-gate */ 12220Sstevel@tonic-gate if (backlog >= (INT_MAX - 1) / 3) 12230Sstevel@tonic-gate backlog = INT_MAX; 12240Sstevel@tonic-gate else 12250Sstevel@tonic-gate backlog = backlog * 3 / 2 + 1; 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate /* 12280Sstevel@tonic-gate * If the listen doesn't change the backlog we do nothing. 12290Sstevel@tonic-gate * This avoids an EPROTO error from the transport. 12300Sstevel@tonic-gate */ 12310Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) && 12320Sstevel@tonic-gate so->so_backlog == backlog) 12330Sstevel@tonic-gate goto done; 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 12360Sstevel@tonic-gate /* 12370Sstevel@tonic-gate * Must have been explicitly bound in the UNIX domain. 12380Sstevel@tonic-gate */ 12390Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 12400Sstevel@tonic-gate error = EINVAL; 12410Sstevel@tonic-gate goto done; 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog, 12440Sstevel@tonic-gate _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN); 12450Sstevel@tonic-gate } else if (backlog > 0) { 12460Sstevel@tonic-gate /* 12470Sstevel@tonic-gate * AF_INET{,6} hack to avoid losing the port. 12480Sstevel@tonic-gate * Assumes that all AF_INET{,6} transports can handle a 12490Sstevel@tonic-gate * O_T_BIND_REQ with a non-zero CONIND_number when the TPI 12500Sstevel@tonic-gate * has already bound thus it is possible to avoid the unbind. 12510Sstevel@tonic-gate */ 12520Sstevel@tonic-gate if (!((so->so_family == AF_INET || so->so_family == AF_INET6) && 12530Sstevel@tonic-gate /*CONSTCOND*/ 12540Sstevel@tonic-gate !solisten_tpi_tcp)) { 12550Sstevel@tonic-gate error = sotpi_unbind(so, _SOUNBIND_REBIND); 12560Sstevel@tonic-gate if (error) 12570Sstevel@tonic-gate goto done; 12580Sstevel@tonic-gate } 12590Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog, 12600Sstevel@tonic-gate _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN); 12610Sstevel@tonic-gate } else { 12620Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN; 12630Sstevel@tonic-gate so->so_backlog = backlog; 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate if (error) 12660Sstevel@tonic-gate goto done; 12670Sstevel@tonic-gate ASSERT(so->so_state & SS_ACCEPTCONN); 12680Sstevel@tonic-gate done: 12690Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 12700Sstevel@tonic-gate mutex_exit(&so->so_lock); 12710Sstevel@tonic-gate return (error); 12720Sstevel@tonic-gate } 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate /* 12750Sstevel@tonic-gate * Disconnect either a specified seqno or all (-1). 12760Sstevel@tonic-gate * The former is used on listening sockets only. 12770Sstevel@tonic-gate * 12780Sstevel@tonic-gate * When seqno == -1 sodisconnect could call sotpi_unbind. However, 12790Sstevel@tonic-gate * the current use of sodisconnect(seqno == -1) is only for shutdown 12800Sstevel@tonic-gate * so there is no point (and potentially incorrect) to unbind. 12810Sstevel@tonic-gate */ 12820Sstevel@tonic-gate int 12830Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags) 12840Sstevel@tonic-gate { 12850Sstevel@tonic-gate struct T_discon_req discon_req; 12860Sstevel@tonic-gate int error = 0; 12870Sstevel@tonic-gate mblk_t *mp; 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n", 12900Sstevel@tonic-gate so, seqno, flags, pr_state(so->so_state, so->so_mode))); 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) { 12930Sstevel@tonic-gate mutex_enter(&so->so_lock); 12940Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 12950Sstevel@tonic-gate } else { 12960Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 12970Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 12980Sstevel@tonic-gate } 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) { 13010Sstevel@tonic-gate error = EINVAL; 13020Sstevel@tonic-gate eprintsoline(so, error); 13030Sstevel@tonic-gate goto done; 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate mutex_exit(&so->so_lock); 13070Sstevel@tonic-gate /* 13080Sstevel@tonic-gate * Flush the write side (unless this is a listener) 13090Sstevel@tonic-gate * and then send down a T_DISCON_REQ. 13100Sstevel@tonic-gate * (Don't flush on listener since it could flush {O_}T_CONN_RES 13110Sstevel@tonic-gate * and other messages.) 13120Sstevel@tonic-gate */ 13130Sstevel@tonic-gate if (!(so->so_state & SS_ACCEPTCONN)) 13140Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW); 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate discon_req.PRIM_type = T_DISCON_REQ; 13170Sstevel@tonic-gate discon_req.SEQ_number = seqno; 13180Sstevel@tonic-gate mp = soallocproto1(&discon_req, sizeof (discon_req), 13190Sstevel@tonic-gate 0, _ALLOC_SLEEP); 13200Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 13210Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 13220Sstevel@tonic-gate mutex_enter(&so->so_lock); 13230Sstevel@tonic-gate if (error) { 13240Sstevel@tonic-gate eprintsoline(so, error); 13250Sstevel@tonic-gate goto done; 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate error = sowaitokack(so, T_DISCON_REQ); 13290Sstevel@tonic-gate if (error) { 13300Sstevel@tonic-gate eprintsoline(so, error); 13310Sstevel@tonic-gate goto done; 13320Sstevel@tonic-gate } 13330Sstevel@tonic-gate /* 13340Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 13350Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the disconnect 13360Sstevel@tonic-gate * is allowed to complete. However, it is not possible to 13370Sstevel@tonic-gate * assert that SS_ISCONNECTED|SS_ISCONNECTING are set. 13380Sstevel@tonic-gate */ 13390Sstevel@tonic-gate so->so_state &= 13400Sstevel@tonic-gate ~(SS_ISCONNECTED|SS_ISCONNECTING|SS_LADDR_VALID|SS_FADDR_VALID); 13410Sstevel@tonic-gate done: 13420Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) { 13430Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 13440Sstevel@tonic-gate mutex_exit(&so->so_lock); 13450Sstevel@tonic-gate } else { 13460Sstevel@tonic-gate /* If the caller held the lock don't release it here */ 13470Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 13480Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 13490Sstevel@tonic-gate } 13500Sstevel@tonic-gate return (error); 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate int 13540Sstevel@tonic-gate sotpi_accept(struct sonode *so, int fflag, struct sonode **nsop) 13550Sstevel@tonic-gate { 13560Sstevel@tonic-gate struct T_conn_ind *conn_ind; 13570Sstevel@tonic-gate struct T_conn_res *conn_res; 13580Sstevel@tonic-gate int error = 0; 13590Sstevel@tonic-gate mblk_t *mp; 13600Sstevel@tonic-gate struct sonode *nso; 13610Sstevel@tonic-gate vnode_t *nvp; 13620Sstevel@tonic-gate void *src; 13630Sstevel@tonic-gate t_uscalar_t srclen; 13640Sstevel@tonic-gate void *opt; 13650Sstevel@tonic-gate t_uscalar_t optlen; 13660Sstevel@tonic-gate t_scalar_t PRIM_type; 13670Sstevel@tonic-gate t_scalar_t SEQ_number; 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n", 13700Sstevel@tonic-gate so, fflag, nsop, pr_state(so->so_state, so->so_mode))); 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate /* 13730Sstevel@tonic-gate * Defer single-threading the accepting socket until 13740Sstevel@tonic-gate * the T_CONN_IND has been received and parsed and the 13750Sstevel@tonic-gate * new sonode has been opened. 13760Sstevel@tonic-gate */ 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate /* Check that we are not already connected */ 13790Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0) 13800Sstevel@tonic-gate goto conn_bad; 13810Sstevel@tonic-gate again: 13820Sstevel@tonic-gate if ((error = sowaitconnind(so, fflag, &mp)) != 0) 13830Sstevel@tonic-gate goto e_bad; 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate ASSERT(mp); 13860Sstevel@tonic-gate conn_ind = (struct T_conn_ind *)mp->b_rptr; 13870Sstevel@tonic-gate /* 13880Sstevel@tonic-gate * Save SEQ_number for error paths. 13890Sstevel@tonic-gate */ 13900Sstevel@tonic-gate SEQ_number = conn_ind->SEQ_number; 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate srclen = conn_ind->SRC_length; 13930Sstevel@tonic-gate src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1); 13940Sstevel@tonic-gate if (src == NULL) { 13950Sstevel@tonic-gate error = EPROTO; 13960Sstevel@tonic-gate freemsg(mp); 13970Sstevel@tonic-gate eprintsoline(so, error); 13980Sstevel@tonic-gate goto disconnect_unlocked; 13990Sstevel@tonic-gate } 14000Sstevel@tonic-gate optlen = conn_ind->OPT_length; 14010Sstevel@tonic-gate switch (so->so_family) { 14020Sstevel@tonic-gate case AF_INET: 14030Sstevel@tonic-gate case AF_INET6: 14040Sstevel@tonic-gate if ((optlen == sizeof (intptr_t)) && 1405*741Smasputra ((so->so_state & SS_DIRECT) != 0)) { 14060Sstevel@tonic-gate bcopy(mp->b_rptr + conn_ind->OPT_offset, 14070Sstevel@tonic-gate &opt, conn_ind->OPT_length); 14080Sstevel@tonic-gate } else { 14090Sstevel@tonic-gate /* 14100Sstevel@tonic-gate * The transport (in this case TCP) hasn't sent up 14110Sstevel@tonic-gate * a pointer to an instance for the accept fast-path. 14120Sstevel@tonic-gate * Disable fast-path completely because the call to 14130Sstevel@tonic-gate * sotpi_create() below would otherwise create an 14140Sstevel@tonic-gate * incomplete TCP instance, which would lead to 14150Sstevel@tonic-gate * problems when sockfs sends a normal T_CONN_RES 14160Sstevel@tonic-gate * message down the new stream. 14170Sstevel@tonic-gate */ 1418*741Smasputra if (so->so_state & SS_DIRECT) { 1419*741Smasputra int rval; 1420*741Smasputra /* 1421*741Smasputra * For consistency we inform tcp to disable 1422*741Smasputra * direct interface on the listener, though 1423*741Smasputra * we can certainly live without doing this 1424*741Smasputra * because no data will ever travel upstream 1425*741Smasputra * on the listening socket. 1426*741Smasputra */ 1427*741Smasputra so->so_state &= ~SS_DIRECT; 1428*741Smasputra (void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK, 1429*741Smasputra 0, 0, K_TO_K, CRED(), &rval); 1430*741Smasputra } 14310Sstevel@tonic-gate opt = NULL; 14320Sstevel@tonic-gate optlen = 0; 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate break; 14350Sstevel@tonic-gate case AF_UNIX: 14360Sstevel@tonic-gate default: 14370Sstevel@tonic-gate if (optlen != 0) { 14380Sstevel@tonic-gate opt = sogetoff(mp, conn_ind->OPT_offset, optlen, 14390Sstevel@tonic-gate __TPI_ALIGN_SIZE); 14400Sstevel@tonic-gate if (opt == NULL) { 14410Sstevel@tonic-gate error = EPROTO; 14420Sstevel@tonic-gate freemsg(mp); 14430Sstevel@tonic-gate eprintsoline(so, error); 14440Sstevel@tonic-gate goto disconnect_unlocked; 14450Sstevel@tonic-gate } 14460Sstevel@tonic-gate } 14470Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 14480Sstevel@tonic-gate if (!(so->so_state & SS_FADDR_NOXLATE)) { 14490Sstevel@tonic-gate src = NULL; 14500Sstevel@tonic-gate srclen = 0; 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate /* Extract src address from options */ 14530Sstevel@tonic-gate if (optlen != 0) 14540Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &src, &srclen); 14550Sstevel@tonic-gate } 14560Sstevel@tonic-gate break; 14570Sstevel@tonic-gate } 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate /* 14600Sstevel@tonic-gate * Create the new socket. 14610Sstevel@tonic-gate */ 14620Sstevel@tonic-gate VN_HOLD(so->so_accessvp); 14630Sstevel@tonic-gate nso = sotpi_create(so->so_accessvp, so->so_family, so->so_type, 14640Sstevel@tonic-gate so->so_protocol, so->so_version, so, &error); 14650Sstevel@tonic-gate if (nso == NULL) { 14660Sstevel@tonic-gate ASSERT(error != 0); 14670Sstevel@tonic-gate /* 14680Sstevel@tonic-gate * Accept can not fail with ENOBUFS. sotpi_create 14690Sstevel@tonic-gate * sleeps waiting for memory until a signal is caught 14700Sstevel@tonic-gate * so return EINTR. 14710Sstevel@tonic-gate */ 14720Sstevel@tonic-gate freemsg(mp); 14730Sstevel@tonic-gate if (error == ENOBUFS) 14740Sstevel@tonic-gate error = EINTR; 14750Sstevel@tonic-gate goto e_disc_unl; 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate nvp = SOTOV(nso); 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate #ifdef DEBUG 14800Sstevel@tonic-gate /* 14810Sstevel@tonic-gate * SO_DEBUG is used to trigger the dprint* and eprint* macros thus 14820Sstevel@tonic-gate * it's inherited early to allow debugging of the accept code itself. 14830Sstevel@tonic-gate */ 14840Sstevel@tonic-gate nso->so_options |= so->so_options & SO_DEBUG; 14850Sstevel@tonic-gate #endif /* DEBUG */ 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * Save the SRC address from the T_CONN_IND 14890Sstevel@tonic-gate * for getpeername to work on AF_UNIX and on transports that do not 14900Sstevel@tonic-gate * support TI_GETPEERNAME. 14910Sstevel@tonic-gate * 14920Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by the sender's 14930Sstevel@tonic-gate * copyin_name(). 14940Sstevel@tonic-gate */ 14950Sstevel@tonic-gate if (srclen > (t_uscalar_t)nso->so_faddr_maxlen) { 14960Sstevel@tonic-gate error = EINVAL; 14970Sstevel@tonic-gate freemsg(mp); 14980Sstevel@tonic-gate eprintsoline(so, error); 14990Sstevel@tonic-gate goto disconnect_vp_unlocked; 15000Sstevel@tonic-gate } 15010Sstevel@tonic-gate nso->so_faddr_len = (socklen_t)srclen; 15020Sstevel@tonic-gate ASSERT(so->so_faddr_len <= so->so_faddr_maxlen); 15030Sstevel@tonic-gate bcopy(src, nso->so_faddr_sa, srclen); 15040Sstevel@tonic-gate nso->so_state |= SS_FADDR_VALID; 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate if ((DB_REF(mp) > 1) || MBLKSIZE(mp) < 15070Sstevel@tonic-gate (sizeof (struct T_conn_res) + sizeof (intptr_t))) { 15080Sstevel@tonic-gate cred_t *cr; 15090Sstevel@tonic-gate 15100Sstevel@tonic-gate if ((cr = DB_CRED(mp)) != NULL) { 15110Sstevel@tonic-gate crhold(cr); 15120Sstevel@tonic-gate nso->so_peercred = cr; 15130Sstevel@tonic-gate nso->so_cpid = DB_CPID(mp); 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate freemsg(mp); 15160Sstevel@tonic-gate 15170Sstevel@tonic-gate mp = soallocproto1(NULL, sizeof (struct T_conn_res) + 15180Sstevel@tonic-gate sizeof (intptr_t), 0, _ALLOC_INTR); 15190Sstevel@tonic-gate if (mp == NULL) { 15200Sstevel@tonic-gate /* 15210Sstevel@tonic-gate * Accept can not fail with ENOBUFS. 15220Sstevel@tonic-gate * A signal was caught so return EINTR. 15230Sstevel@tonic-gate */ 15240Sstevel@tonic-gate error = EINTR; 15250Sstevel@tonic-gate eprintsoline(so, error); 15260Sstevel@tonic-gate goto disconnect_vp_unlocked; 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr; 15290Sstevel@tonic-gate } else { 15300Sstevel@tonic-gate nso->so_peercred = DB_CRED(mp); 15310Sstevel@tonic-gate nso->so_cpid = DB_CPID(mp); 15320Sstevel@tonic-gate DB_CRED(mp) = NULL; 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate mp->b_rptr = DB_BASE(mp); 15350Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr; 15360Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate /* 15400Sstevel@tonic-gate * New socket must be bound at least in sockfs and, except for AF_INET, 15410Sstevel@tonic-gate * (or AF_INET6) it also has to be bound in the transport provider. 15420Sstevel@tonic-gate * After accepting the connection on nso so_laddr_sa will be set to 15430Sstevel@tonic-gate * contain the same address as the listener's local address 15440Sstevel@tonic-gate * so the address we bind to isn't important. 15450Sstevel@tonic-gate */ 15460Sstevel@tonic-gate if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) && 15470Sstevel@tonic-gate /*CONSTCOND*/ 15480Sstevel@tonic-gate nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) { 15490Sstevel@tonic-gate /* 15500Sstevel@tonic-gate * Optimization for AF_INET{,6} transports 15510Sstevel@tonic-gate * that can handle a T_CONN_RES without being bound. 15520Sstevel@tonic-gate */ 15530Sstevel@tonic-gate mutex_enter(&nso->so_lock); 15540Sstevel@tonic-gate so_automatic_bind(nso); 15550Sstevel@tonic-gate mutex_exit(&nso->so_lock); 15560Sstevel@tonic-gate } else { 15570Sstevel@tonic-gate /* Perform NULL bind with the transport provider. */ 15580Sstevel@tonic-gate if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC)) != 0) { 15590Sstevel@tonic-gate ASSERT(error != ENOBUFS); 15600Sstevel@tonic-gate freemsg(mp); 15610Sstevel@tonic-gate eprintsoline(nso, error); 15620Sstevel@tonic-gate goto disconnect_vp_unlocked; 15630Sstevel@tonic-gate } 15640Sstevel@tonic-gate } 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate /* 15670Sstevel@tonic-gate * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES 15680Sstevel@tonic-gate * so that any data arriving on the new socket will cause the 15690Sstevel@tonic-gate * appropriate signals to be delivered for the new socket. 15700Sstevel@tonic-gate * 15710Sstevel@tonic-gate * No other thread (except strsock_proto and strsock_misc) 15720Sstevel@tonic-gate * can access the new socket thus we relax the locking. 15730Sstevel@tonic-gate */ 15740Sstevel@tonic-gate nso->so_pgrp = so->so_pgrp; 15750Sstevel@tonic-gate nso->so_state |= so->so_state & (SS_ASYNC|SS_FADDR_NOXLATE); 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate if (nso->so_pgrp != 0) { 15780Sstevel@tonic-gate if ((error = so_set_events(nso, nvp, CRED())) != 0) { 15790Sstevel@tonic-gate eprintsoline(nso, error); 15800Sstevel@tonic-gate error = 0; 15810Sstevel@tonic-gate nso->so_pgrp = 0; 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate } 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate /* 15860Sstevel@tonic-gate * Make note of the socket level options. TCP and IP level options 15870Sstevel@tonic-gate * are already inherited. We could do all this after accept is 15880Sstevel@tonic-gate * successful but doing it here simplifies code and no harm done 15890Sstevel@tonic-gate * for error case. 15900Sstevel@tonic-gate */ 15910Sstevel@tonic-gate nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE| 15920Sstevel@tonic-gate SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK| 15930Sstevel@tonic-gate SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER); 15940Sstevel@tonic-gate nso->so_sndbuf = so->so_sndbuf; 15950Sstevel@tonic-gate nso->so_rcvbuf = so->so_rcvbuf; 15960Sstevel@tonic-gate if (nso->so_options & SO_LINGER) 15970Sstevel@tonic-gate nso->so_linger = so->so_linger; 15980Sstevel@tonic-gate 1599*741Smasputra if ((so->so_state & SS_DIRECT) != 0) { 16000Sstevel@tonic-gate mblk_t *ack_mp; 16010Sstevel@tonic-gate 1602*741Smasputra ASSERT(nso->so_state & SS_DIRECT); 16030Sstevel@tonic-gate ASSERT(opt != NULL); 16040Sstevel@tonic-gate 16050Sstevel@tonic-gate conn_res->OPT_length = optlen; 16060Sstevel@tonic-gate conn_res->OPT_offset = MBLKL(mp); 16070Sstevel@tonic-gate bcopy(&opt, mp->b_wptr, optlen); 16080Sstevel@tonic-gate mp->b_wptr += optlen; 16090Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES; 16100Sstevel@tonic-gate conn_res->ACCEPTOR_id = 0; 16110Sstevel@tonic-gate PRIM_type = T_CONN_RES; 16120Sstevel@tonic-gate 16130Sstevel@tonic-gate /* Send down the T_CONN_RES on acceptor STREAM */ 16140Sstevel@tonic-gate error = kstrputmsg(SOTOV(nso), mp, NULL, 16150Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 16160Sstevel@tonic-gate if (error) { 16170Sstevel@tonic-gate mutex_enter(&so->so_lock); 16180Sstevel@tonic-gate so_lock_single(so); 16190Sstevel@tonic-gate eprintsoline(so, error); 16200Sstevel@tonic-gate goto disconnect_vp; 16210Sstevel@tonic-gate } 16220Sstevel@tonic-gate mutex_enter(&nso->so_lock); 16230Sstevel@tonic-gate error = sowaitprim(nso, T_CONN_RES, T_OK_ACK, 16240Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 16250Sstevel@tonic-gate if (error) { 16260Sstevel@tonic-gate mutex_exit(&nso->so_lock); 16270Sstevel@tonic-gate mutex_enter(&so->so_lock); 16280Sstevel@tonic-gate so_lock_single(so); 16290Sstevel@tonic-gate eprintsoline(so, error); 16300Sstevel@tonic-gate goto disconnect_vp; 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate if (nso->so_family == AF_INET) { 16330Sstevel@tonic-gate sin_t *sin; 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate sin = (sin_t *)(ack_mp->b_rptr + 16360Sstevel@tonic-gate sizeof (struct T_ok_ack)); 16370Sstevel@tonic-gate bcopy(sin, nso->so_laddr_sa, sizeof (sin_t)); 16380Sstevel@tonic-gate nso->so_laddr_len = sizeof (sin_t); 16390Sstevel@tonic-gate } else { 16400Sstevel@tonic-gate sin6_t *sin6; 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate sin6 = (sin6_t *)(ack_mp->b_rptr + 16430Sstevel@tonic-gate sizeof (struct T_ok_ack)); 16440Sstevel@tonic-gate bcopy(sin6, nso->so_laddr_sa, sizeof (sin6_t)); 16450Sstevel@tonic-gate nso->so_laddr_len = sizeof (sin6_t); 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate freemsg(ack_mp); 16480Sstevel@tonic-gate 16490Sstevel@tonic-gate nso->so_state |= SS_ISCONNECTED | SS_LADDR_VALID; 16500Sstevel@tonic-gate nso->so_priv = opt; 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate if (so->so_nl7c_flags & NL7C_ENABLED) { 16530Sstevel@tonic-gate /* 16540Sstevel@tonic-gate * An NL7C marked listen()er so the new socket 16550Sstevel@tonic-gate * inherits the listen()er's NL7C state. 16560Sstevel@tonic-gate * 16570Sstevel@tonic-gate * When calling NL7C to process the new socket 16580Sstevel@tonic-gate * pass the nonblocking i/o state of the listen 16590Sstevel@tonic-gate * socket as this is the context we are in. 16600Sstevel@tonic-gate */ 16610Sstevel@tonic-gate nso->so_nl7c_flags = so->so_nl7c_flags; 16620Sstevel@tonic-gate if (nl7c_process(nso, 16630Sstevel@tonic-gate (nso->so_state & (SS_NONBLOCK|SS_NDELAY)), 16640Sstevel@tonic-gate (int)((tcp_t *)nso->so_priv)->tcp_mss)) { 16650Sstevel@tonic-gate /* 16660Sstevel@tonic-gate * NL7C has completed processing on the 16670Sstevel@tonic-gate * socket, close the socket and back to 16680Sstevel@tonic-gate * the top to await the next T_CONN_IND. 16690Sstevel@tonic-gate */ 16700Sstevel@tonic-gate mutex_exit(&nso->so_lock); 16710Sstevel@tonic-gate (void) VOP_CLOSE(nvp, 0, 1, (offset_t)0, 16720Sstevel@tonic-gate CRED()); 16730Sstevel@tonic-gate VN_RELE(nvp); 16740Sstevel@tonic-gate goto again; 16750Sstevel@tonic-gate } 16760Sstevel@tonic-gate /* Pass the new socket out */ 16770Sstevel@tonic-gate } 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate mutex_exit(&nso->so_lock); 16800Sstevel@tonic-gate 16810Sstevel@tonic-gate /* 16820Sstevel@tonic-gate * Pass out new socket. 16830Sstevel@tonic-gate */ 16840Sstevel@tonic-gate if (nsop != NULL) 16850Sstevel@tonic-gate *nsop = nso; 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate return (0); 16880Sstevel@tonic-gate } 16890Sstevel@tonic-gate 16900Sstevel@tonic-gate /* 16910Sstevel@tonic-gate * Copy local address from listener. 16920Sstevel@tonic-gate */ 16930Sstevel@tonic-gate nso->so_laddr_len = so->so_laddr_len; 16940Sstevel@tonic-gate ASSERT(nso->so_laddr_len <= nso->so_laddr_maxlen); 16950Sstevel@tonic-gate bcopy(so->so_laddr_sa, nso->so_laddr_sa, nso->so_laddr_len); 16960Sstevel@tonic-gate nso->so_state |= SS_LADDR_VALID; 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate /* 16990Sstevel@tonic-gate * This is the non-performance case for sockets (e.g. AF_UNIX sockets) 17000Sstevel@tonic-gate * which don't support the FireEngine accept fast-path. It is also 17010Sstevel@tonic-gate * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd 17020Sstevel@tonic-gate * again. Neither sockfs nor TCP attempt to find out if some other 17030Sstevel@tonic-gate * random module has been inserted in between (in which case we 17040Sstevel@tonic-gate * should follow TLI accept behaviour). We blindly assume the worst 17050Sstevel@tonic-gate * case and revert back to old behaviour i.e. TCP will not send us 17060Sstevel@tonic-gate * any option (eager) and the accept should happen on the listener 17070Sstevel@tonic-gate * queue. Any queued T_conn_ind have already got their options removed 17080Sstevel@tonic-gate * by so_sock2_stream() when "sockmod" was I_POP'd. 17090Sstevel@tonic-gate */ 17100Sstevel@tonic-gate /* 17110Sstevel@tonic-gate * Fill in the {O_}T_CONN_RES before getting SOLOCKED. 17120Sstevel@tonic-gate */ 17130Sstevel@tonic-gate if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) { 17140Sstevel@tonic-gate #ifdef _ILP32 17150Sstevel@tonic-gate queue_t *q; 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate /* 17180Sstevel@tonic-gate * Find read queue in driver 17190Sstevel@tonic-gate * Can safely do this since we "own" nso/nvp. 17200Sstevel@tonic-gate */ 17210Sstevel@tonic-gate q = strvp2wq(nvp)->q_next; 17220Sstevel@tonic-gate while (SAMESTR(q)) 17230Sstevel@tonic-gate q = q->q_next; 17240Sstevel@tonic-gate q = RD(q); 17250Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)q; 17260Sstevel@tonic-gate #else 17270Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev); 17280Sstevel@tonic-gate #endif /* _ILP32 */ 17290Sstevel@tonic-gate conn_res->PRIM_type = O_T_CONN_RES; 17300Sstevel@tonic-gate PRIM_type = O_T_CONN_RES; 17310Sstevel@tonic-gate } else { 17320Sstevel@tonic-gate conn_res->ACCEPTOR_id = nso->so_acceptor_id; 17330Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES; 17340Sstevel@tonic-gate PRIM_type = T_CONN_RES; 17350Sstevel@tonic-gate } 17360Sstevel@tonic-gate conn_res->SEQ_number = SEQ_number; 17370Sstevel@tonic-gate conn_res->OPT_length = 0; 17380Sstevel@tonic-gate conn_res->OPT_offset = 0; 17390Sstevel@tonic-gate 17400Sstevel@tonic-gate mutex_enter(&so->so_lock); 17410Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 17420Sstevel@tonic-gate mutex_exit(&so->so_lock); 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 17450Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 17460Sstevel@tonic-gate mutex_enter(&so->so_lock); 17470Sstevel@tonic-gate if (error) { 17480Sstevel@tonic-gate eprintsoline(so, error); 17490Sstevel@tonic-gate goto disconnect_vp; 17500Sstevel@tonic-gate } 17510Sstevel@tonic-gate error = sowaitokack(so, PRIM_type); 17520Sstevel@tonic-gate if (error) { 17530Sstevel@tonic-gate eprintsoline(so, error); 17540Sstevel@tonic-gate goto disconnect_vp; 17550Sstevel@tonic-gate } 17560Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 17570Sstevel@tonic-gate mutex_exit(&so->so_lock); 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate nso->so_state |= SS_ISCONNECTED; 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate /* 17620Sstevel@tonic-gate * Pass out new socket. 17630Sstevel@tonic-gate */ 17640Sstevel@tonic-gate if (nsop != NULL) 17650Sstevel@tonic-gate *nsop = nso; 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate return (0); 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate eproto_disc_unl: 17710Sstevel@tonic-gate error = EPROTO; 17720Sstevel@tonic-gate e_disc_unl: 17730Sstevel@tonic-gate eprintsoline(so, error); 17740Sstevel@tonic-gate goto disconnect_unlocked; 17750Sstevel@tonic-gate 17760Sstevel@tonic-gate pr_disc_vp_unl: 17770Sstevel@tonic-gate eprintsoline(so, error); 17780Sstevel@tonic-gate disconnect_vp_unlocked: 17790Sstevel@tonic-gate (void) VOP_CLOSE(nvp, 0, 1, 0, CRED()); 17800Sstevel@tonic-gate VN_RELE(nvp); 17810Sstevel@tonic-gate disconnect_unlocked: 17820Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, 0); 17830Sstevel@tonic-gate return (error); 17840Sstevel@tonic-gate 17850Sstevel@tonic-gate pr_disc_vp: 17860Sstevel@tonic-gate eprintsoline(so, error); 17870Sstevel@tonic-gate disconnect_vp: 17880Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD); 17890Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 17900Sstevel@tonic-gate mutex_exit(&so->so_lock); 17910Sstevel@tonic-gate (void) VOP_CLOSE(nvp, 0, 1, 0, CRED()); 17920Sstevel@tonic-gate VN_RELE(nvp); 17930Sstevel@tonic-gate return (error); 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate conn_bad: /* Note: SunOS 4/BSD unconditionally returns EINVAL here */ 17960Sstevel@tonic-gate error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW) 17970Sstevel@tonic-gate ? EOPNOTSUPP : EINVAL; 17980Sstevel@tonic-gate e_bad: 17990Sstevel@tonic-gate eprintsoline(so, error); 18000Sstevel@tonic-gate return (error); 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate /* 18040Sstevel@tonic-gate * connect a socket. 18050Sstevel@tonic-gate * 18060Sstevel@tonic-gate * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to 18070Sstevel@tonic-gate * unconnect (by specifying a null address). 18080Sstevel@tonic-gate */ 18090Sstevel@tonic-gate int 18100Sstevel@tonic-gate sotpi_connect(struct sonode *so, 18110Sstevel@tonic-gate const struct sockaddr *name, 18120Sstevel@tonic-gate socklen_t namelen, 18130Sstevel@tonic-gate int fflag, 18140Sstevel@tonic-gate int flags) 18150Sstevel@tonic-gate { 18160Sstevel@tonic-gate struct T_conn_req conn_req; 18170Sstevel@tonic-gate int error = 0; 18180Sstevel@tonic-gate mblk_t *mp; 18190Sstevel@tonic-gate void *src; 18200Sstevel@tonic-gate socklen_t srclen; 18210Sstevel@tonic-gate void *addr; 18220Sstevel@tonic-gate socklen_t addrlen; 18230Sstevel@tonic-gate boolean_t need_unlock; 18240Sstevel@tonic-gate 18250Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n", 18260Sstevel@tonic-gate so, name, namelen, fflag, flags, 18270Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 18280Sstevel@tonic-gate 18290Sstevel@tonic-gate /* 18300Sstevel@tonic-gate * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to 18310Sstevel@tonic-gate * avoid sleeping for memory with SOLOCKED held. 18320Sstevel@tonic-gate * We know that the T_CONN_REQ can't be larger than 2 * so_faddr_maxlen 18330Sstevel@tonic-gate * + sizeof (struct T_opthdr). 18340Sstevel@tonic-gate * (the AF_UNIX so_ux_addr_xlate() does not make the address 18350Sstevel@tonic-gate * exceed so_faddr_maxlen). 18360Sstevel@tonic-gate */ 18370Sstevel@tonic-gate mp = soallocproto(sizeof (struct T_conn_req) + 18380Sstevel@tonic-gate 2 * so->so_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR); 18390Sstevel@tonic-gate if (mp == NULL) { 18400Sstevel@tonic-gate /* 18410Sstevel@tonic-gate * Connect can not fail with ENOBUFS. A signal was 18420Sstevel@tonic-gate * caught so return EINTR. 18430Sstevel@tonic-gate */ 18440Sstevel@tonic-gate error = EINTR; 18450Sstevel@tonic-gate eprintsoline(so, error); 18460Sstevel@tonic-gate return (error); 18470Sstevel@tonic-gate } 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate mutex_enter(&so->so_lock); 18500Sstevel@tonic-gate /* 18510Sstevel@tonic-gate * Make sure that there is a preallocated unbind_req 18520Sstevel@tonic-gate * message before any binding. This message allocated when 18530Sstevel@tonic-gate * the socket is created but it might be have been 18540Sstevel@tonic-gate * consumed. 18550Sstevel@tonic-gate */ 18560Sstevel@tonic-gate if (so->so_unbind_mp == NULL) { 18570Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n")); 18580Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 18590Sstevel@tonic-gate so->so_unbind_mp = 18600Sstevel@tonic-gate soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR); 18610Sstevel@tonic-gate if (so->so_unbind_mp == NULL) { 18620Sstevel@tonic-gate error = EINTR; 18630Sstevel@tonic-gate need_unlock = B_FALSE; 18640Sstevel@tonic-gate goto done; 18650Sstevel@tonic-gate } 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate 18680Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 18690Sstevel@tonic-gate need_unlock = B_TRUE; 18700Sstevel@tonic-gate 18710Sstevel@tonic-gate /* 18720Sstevel@tonic-gate * Can't have done a listen before connecting. 18730Sstevel@tonic-gate */ 18740Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) { 18750Sstevel@tonic-gate error = EOPNOTSUPP; 18760Sstevel@tonic-gate goto done; 18770Sstevel@tonic-gate } 18780Sstevel@tonic-gate 18790Sstevel@tonic-gate /* 18800Sstevel@tonic-gate * Must be bound with the transport 18810Sstevel@tonic-gate */ 18820Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 18830Sstevel@tonic-gate if ((so->so_family == AF_INET || so->so_family == AF_INET6) && 18840Sstevel@tonic-gate /*CONSTCOND*/ 18850Sstevel@tonic-gate so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) { 18860Sstevel@tonic-gate /* 18870Sstevel@tonic-gate * Optimization for AF_INET{,6} transports 18880Sstevel@tonic-gate * that can handle a T_CONN_REQ without being bound. 18890Sstevel@tonic-gate */ 18900Sstevel@tonic-gate so_automatic_bind(so); 18910Sstevel@tonic-gate } else { 18920Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0, 18930Sstevel@tonic-gate _SOBIND_UNSPEC|_SOBIND_LOCK_HELD); 18940Sstevel@tonic-gate if (error) 18950Sstevel@tonic-gate goto done; 18960Sstevel@tonic-gate } 18970Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND); 18980Sstevel@tonic-gate flags |= _SOCONNECT_DID_BIND; 18990Sstevel@tonic-gate } 19000Sstevel@tonic-gate 19010Sstevel@tonic-gate /* 19020Sstevel@tonic-gate * Handle a connect to a name parameter of type AF_UNSPEC like a 19030Sstevel@tonic-gate * connect to a null address. This is the portable method to 19040Sstevel@tonic-gate * unconnect a socket. 19050Sstevel@tonic-gate */ 19060Sstevel@tonic-gate if ((namelen >= sizeof (sa_family_t)) && 19070Sstevel@tonic-gate (name->sa_family == AF_UNSPEC)) { 19080Sstevel@tonic-gate name = NULL; 19090Sstevel@tonic-gate namelen = 0; 19100Sstevel@tonic-gate } 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate /* 19130Sstevel@tonic-gate * Check that we are not already connected. 19140Sstevel@tonic-gate * A connection-oriented socket cannot be reconnected. 19150Sstevel@tonic-gate * A connected connection-less socket can be 19160Sstevel@tonic-gate * - connected to a different address by a subsequent connect 19170Sstevel@tonic-gate * - "unconnected" by a connect to the NULL address 19180Sstevel@tonic-gate */ 19190Sstevel@tonic-gate if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) { 19200Sstevel@tonic-gate ASSERT(!(flags & _SOCONNECT_DID_BIND)); 19210Sstevel@tonic-gate if (so->so_mode & SM_CONNREQUIRED) { 19220Sstevel@tonic-gate /* Connection-oriented socket */ 19230Sstevel@tonic-gate error = so->so_state & SS_ISCONNECTED ? 19240Sstevel@tonic-gate EISCONN : EALREADY; 19250Sstevel@tonic-gate goto done; 19260Sstevel@tonic-gate } 19270Sstevel@tonic-gate /* Connection-less socket */ 19280Sstevel@tonic-gate if (name == NULL) { 19290Sstevel@tonic-gate /* 19300Sstevel@tonic-gate * Remove the connected state and clear SO_DGRAM_ERRIND 19310Sstevel@tonic-gate * since it was set when the socket was connected. 19320Sstevel@tonic-gate * If this is UDP also send down a T_DISCON_REQ. 19330Sstevel@tonic-gate */ 19340Sstevel@tonic-gate int val; 19350Sstevel@tonic-gate 19360Sstevel@tonic-gate if ((so->so_family == AF_INET || 19370Sstevel@tonic-gate so->so_family == AF_INET6) && 19380Sstevel@tonic-gate (so->so_type == SOCK_DGRAM || 19390Sstevel@tonic-gate so->so_type == SOCK_RAW) && 19400Sstevel@tonic-gate /*CONSTCOND*/ 19410Sstevel@tonic-gate !soconnect_tpi_udp) { 19420Sstevel@tonic-gate /* XXX What about implicitly unbinding here? */ 19430Sstevel@tonic-gate error = sodisconnect(so, -1, 19440Sstevel@tonic-gate _SODISCONNECT_LOCK_HELD); 19450Sstevel@tonic-gate } else { 19460Sstevel@tonic-gate so->so_state &= 19470Sstevel@tonic-gate ~(SS_ISCONNECTED | SS_ISCONNECTING | 19480Sstevel@tonic-gate SS_FADDR_VALID); 19490Sstevel@tonic-gate so->so_faddr_len = 0; 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 19530Sstevel@tonic-gate mutex_exit(&so->so_lock); 19540Sstevel@tonic-gate 19550Sstevel@tonic-gate val = 0; 19560Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND, 19570Sstevel@tonic-gate &val, (t_uscalar_t)sizeof (val)); 19580Sstevel@tonic-gate 19590Sstevel@tonic-gate mutex_enter(&so->so_lock); 19600Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 19610Sstevel@tonic-gate goto done; 19620Sstevel@tonic-gate } 19630Sstevel@tonic-gate } 19640Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND); 19650Sstevel@tonic-gate 19660Sstevel@tonic-gate if (name == NULL || namelen == 0) { 19670Sstevel@tonic-gate error = EINVAL; 19680Sstevel@tonic-gate goto done; 19690Sstevel@tonic-gate } 19700Sstevel@tonic-gate /* 19710Sstevel@tonic-gate * Mark the socket if so_faddr_sa represents the transport level 19720Sstevel@tonic-gate * address. 19730Sstevel@tonic-gate */ 19740Sstevel@tonic-gate if (flags & _SOCONNECT_NOXLATE) { 19750Sstevel@tonic-gate struct sockaddr_ux *soaddr_ux; 19760Sstevel@tonic-gate 19770Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 19780Sstevel@tonic-gate if (namelen != sizeof (struct sockaddr_ux)) { 19790Sstevel@tonic-gate error = EINVAL; 19800Sstevel@tonic-gate goto done; 19810Sstevel@tonic-gate } 19820Sstevel@tonic-gate soaddr_ux = (struct sockaddr_ux *)name; 19830Sstevel@tonic-gate name = (struct sockaddr *)&soaddr_ux->sou_addr; 19840Sstevel@tonic-gate namelen = sizeof (soaddr_ux->sou_addr); 19850Sstevel@tonic-gate so->so_state |= SS_FADDR_NOXLATE; 19860Sstevel@tonic-gate } 19870Sstevel@tonic-gate 19880Sstevel@tonic-gate /* 19890Sstevel@tonic-gate * Length and family checks. 19900Sstevel@tonic-gate */ 19910Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 19920Sstevel@tonic-gate if (error) 19930Sstevel@tonic-gate goto bad; 19940Sstevel@tonic-gate 19950Sstevel@tonic-gate /* 19960Sstevel@tonic-gate * Save foreign address. Needed for AF_UNIX as well as 19970Sstevel@tonic-gate * transport providers that do not support TI_GETPEERNAME. 19980Sstevel@tonic-gate * Also used for cached foreign address for TCP and UDP. 19990Sstevel@tonic-gate */ 20000Sstevel@tonic-gate if (namelen > (t_uscalar_t)so->so_faddr_maxlen) { 20010Sstevel@tonic-gate error = EINVAL; 20020Sstevel@tonic-gate goto done; 20030Sstevel@tonic-gate } 20040Sstevel@tonic-gate so->so_faddr_len = (socklen_t)namelen; 20050Sstevel@tonic-gate ASSERT(so->so_faddr_len <= so->so_faddr_maxlen); 20060Sstevel@tonic-gate bcopy(name, so->so_faddr_sa, namelen); 20070Sstevel@tonic-gate so->so_state |= SS_FADDR_VALID; 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 20100Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 20110Sstevel@tonic-gate /* 20120Sstevel@tonic-gate * Already have a transport internal address. Do not 20130Sstevel@tonic-gate * pass any (transport internal) source address. 20140Sstevel@tonic-gate */ 20150Sstevel@tonic-gate addr = so->so_faddr_sa; 20160Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_faddr_len; 20170Sstevel@tonic-gate src = NULL; 20180Sstevel@tonic-gate srclen = 0; 20190Sstevel@tonic-gate } else { 20200Sstevel@tonic-gate /* 20210Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 20220Sstevel@tonic-gate * and translate the remote address. 20230Sstevel@tonic-gate * Holding so_lock thus so_laddr_sa can not change. 20240Sstevel@tonic-gate */ 20250Sstevel@tonic-gate src = so->so_laddr_sa; 20260Sstevel@tonic-gate srclen = (t_uscalar_t)so->so_laddr_len; 20270Sstevel@tonic-gate dprintso(so, 1, 20280Sstevel@tonic-gate ("sotpi_connect UNIX: srclen %d, src %p\n", 20290Sstevel@tonic-gate srclen, src)); 20300Sstevel@tonic-gate error = so_ux_addr_xlate(so, 20310Sstevel@tonic-gate so->so_faddr_sa, (socklen_t)so->so_faddr_len, 20320Sstevel@tonic-gate (flags & _SOCONNECT_XPG4_2), 20330Sstevel@tonic-gate &addr, &addrlen); 20340Sstevel@tonic-gate if (error) 20350Sstevel@tonic-gate goto bad; 20360Sstevel@tonic-gate } 20370Sstevel@tonic-gate } else { 20380Sstevel@tonic-gate addr = so->so_faddr_sa; 20390Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_faddr_len; 20400Sstevel@tonic-gate src = NULL; 20410Sstevel@tonic-gate srclen = 0; 20420Sstevel@tonic-gate } 20430Sstevel@tonic-gate /* 20440Sstevel@tonic-gate * When connecting a datagram socket we issue the SO_DGRAM_ERRIND 20450Sstevel@tonic-gate * option which asks the transport provider to send T_UDERR_IND 20460Sstevel@tonic-gate * messages. These T_UDERR_IND messages are used to return connected 20470Sstevel@tonic-gate * style errors (e.g. ECONNRESET) for connected datagram sockets. 20480Sstevel@tonic-gate * 20490Sstevel@tonic-gate * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets) 20500Sstevel@tonic-gate * we send down a T_CONN_REQ. This is needed to let the 20510Sstevel@tonic-gate * transport assign a local address that is consistent with 20520Sstevel@tonic-gate * the remote address. Applications depend on a getsockname() 20530Sstevel@tonic-gate * after a connect() to retrieve the "source" IP address for 20540Sstevel@tonic-gate * the connected socket. Invalidate the cached local address 20550Sstevel@tonic-gate * to force getsockname() to enquire of the transport. 20560Sstevel@tonic-gate */ 20570Sstevel@tonic-gate if (!(so->so_mode & SM_CONNREQUIRED)) { 20580Sstevel@tonic-gate /* 20590Sstevel@tonic-gate * Datagram socket. 20600Sstevel@tonic-gate */ 20610Sstevel@tonic-gate int32_t val; 20620Sstevel@tonic-gate 20630Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 20640Sstevel@tonic-gate mutex_exit(&so->so_lock); 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate val = 1; 20670Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND, 20680Sstevel@tonic-gate &val, (t_uscalar_t)sizeof (val)); 20690Sstevel@tonic-gate 20700Sstevel@tonic-gate mutex_enter(&so->so_lock); 20710Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 20720Sstevel@tonic-gate if ((so->so_family != AF_INET && so->so_family != AF_INET6) || 20730Sstevel@tonic-gate (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) || 20740Sstevel@tonic-gate soconnect_tpi_udp) { 20750Sstevel@tonic-gate soisconnected(so); 20760Sstevel@tonic-gate goto done; 20770Sstevel@tonic-gate } 20780Sstevel@tonic-gate /* 20790Sstevel@tonic-gate * Send down T_CONN_REQ etc. 20800Sstevel@tonic-gate * Clear fflag to avoid returning EWOULDBLOCK. 20810Sstevel@tonic-gate */ 20820Sstevel@tonic-gate fflag = 0; 20830Sstevel@tonic-gate ASSERT(so->so_family != AF_UNIX); 20840Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 20850Sstevel@tonic-gate } else if (so->so_laddr_len != 0) { 20860Sstevel@tonic-gate /* 20870Sstevel@tonic-gate * If the local address or port was "any" then it may be 20880Sstevel@tonic-gate * changed by the transport as a result of the 20890Sstevel@tonic-gate * connect. Invalidate the cached version if we have one. 20900Sstevel@tonic-gate */ 20910Sstevel@tonic-gate switch (so->so_family) { 20920Sstevel@tonic-gate case AF_INET: 20930Sstevel@tonic-gate ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin_t)); 20940Sstevel@tonic-gate if (((sin_t *)so->so_laddr_sa)->sin_addr.s_addr == 20950Sstevel@tonic-gate INADDR_ANY || 20960Sstevel@tonic-gate ((sin_t *)so->so_laddr_sa)->sin_port == 0) 20970Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 20980Sstevel@tonic-gate break; 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate case AF_INET6: 21010Sstevel@tonic-gate ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin6_t)); 21020Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED( 21030Sstevel@tonic-gate &((sin6_t *)so->so_laddr_sa) ->sin6_addr) || 21040Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED_ANY( 21050Sstevel@tonic-gate &((sin6_t *)so->so_laddr_sa)->sin6_addr) || 21060Sstevel@tonic-gate ((sin6_t *)so->so_laddr_sa)->sin6_port == 0) 21070Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 21080Sstevel@tonic-gate break; 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate default: 21110Sstevel@tonic-gate break; 21120Sstevel@tonic-gate } 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate 21150Sstevel@tonic-gate /* 21160Sstevel@tonic-gate * Check for failure of an earlier call 21170Sstevel@tonic-gate */ 21180Sstevel@tonic-gate if (so->so_error != 0) 21190Sstevel@tonic-gate goto so_bad; 21200Sstevel@tonic-gate 21210Sstevel@tonic-gate /* 21220Sstevel@tonic-gate * Send down T_CONN_REQ. Message was allocated above. 21230Sstevel@tonic-gate */ 21240Sstevel@tonic-gate conn_req.PRIM_type = T_CONN_REQ; 21250Sstevel@tonic-gate conn_req.DEST_length = addrlen; 21260Sstevel@tonic-gate conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req); 21270Sstevel@tonic-gate if (srclen == 0) { 21280Sstevel@tonic-gate conn_req.OPT_length = 0; 21290Sstevel@tonic-gate conn_req.OPT_offset = 0; 21300Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req)); 21310Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 21320Sstevel@tonic-gate } else { 21330Sstevel@tonic-gate /* 21340Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 21350Sstevel@tonic-gate * address option. 21360Sstevel@tonic-gate */ 21370Sstevel@tonic-gate struct T_opthdr toh; 21380Sstevel@tonic-gate 21390Sstevel@tonic-gate toh.level = SOL_SOCKET; 21400Sstevel@tonic-gate toh.name = SO_SRCADDR; 21410Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 21420Sstevel@tonic-gate toh.status = 0; 21430Sstevel@tonic-gate conn_req.OPT_length = 21440Sstevel@tonic-gate (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen)); 21450Sstevel@tonic-gate conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) + 21460Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen)); 21470Sstevel@tonic-gate 21480Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req)); 21490Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 21500Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 21510Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 21520Sstevel@tonic-gate soappendmsg(mp, src, srclen); 21530Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 21540Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 21550Sstevel@tonic-gate } 21560Sstevel@tonic-gate /* 21570Sstevel@tonic-gate * Set SS_ISCONNECTING before sending down the T_CONN_REQ 21580Sstevel@tonic-gate * in order to have the right state when the T_CONN_CON shows up. 21590Sstevel@tonic-gate */ 21600Sstevel@tonic-gate soisconnecting(so); 21610Sstevel@tonic-gate mutex_exit(&so->so_lock); 21620Sstevel@tonic-gate 21630Sstevel@tonic-gate #ifdef C2_AUDIT 21640Sstevel@tonic-gate if (audit_active) 21650Sstevel@tonic-gate audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0); 21660Sstevel@tonic-gate #endif /* C2_AUDIT */ 21670Sstevel@tonic-gate 21680Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 21690Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 21700Sstevel@tonic-gate mp = NULL; 21710Sstevel@tonic-gate mutex_enter(&so->so_lock); 21720Sstevel@tonic-gate if (error != 0) 21730Sstevel@tonic-gate goto bad; 21740Sstevel@tonic-gate 21750Sstevel@tonic-gate if ((error = sowaitokack(so, T_CONN_REQ)) != 0) 21760Sstevel@tonic-gate goto bad; 21770Sstevel@tonic-gate 21780Sstevel@tonic-gate /* Allow other threads to access the socket */ 21790Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 21800Sstevel@tonic-gate need_unlock = B_FALSE; 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate /* 21830Sstevel@tonic-gate * Wait until we get a T_CONN_CON or an error 21840Sstevel@tonic-gate */ 21850Sstevel@tonic-gate if ((error = sowaitconnected(so, fflag, 0)) != 0) { 21860Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 21870Sstevel@tonic-gate need_unlock = B_TRUE; 21880Sstevel@tonic-gate } 21890Sstevel@tonic-gate 21900Sstevel@tonic-gate done: 21910Sstevel@tonic-gate freemsg(mp); 21920Sstevel@tonic-gate switch (error) { 21930Sstevel@tonic-gate case EINPROGRESS: 21940Sstevel@tonic-gate case EALREADY: 21950Sstevel@tonic-gate case EISCONN: 21960Sstevel@tonic-gate case EINTR: 21970Sstevel@tonic-gate /* Non-fatal errors */ 21980Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 21990Sstevel@tonic-gate /* FALLTHRU */ 22000Sstevel@tonic-gate case 0: 22010Sstevel@tonic-gate break; 22020Sstevel@tonic-gate 22030Sstevel@tonic-gate case EHOSTUNREACH: 22040Sstevel@tonic-gate if (flags & _SOCONNECT_XPG4_2) { 22050Sstevel@tonic-gate /* 22060Sstevel@tonic-gate * X/Open specification contains a requirement that 22070Sstevel@tonic-gate * ENETUNREACH be returned but does not require 22080Sstevel@tonic-gate * EHOSTUNREACH. In order to keep the test suite 22090Sstevel@tonic-gate * happy we mess with the errno here. 22100Sstevel@tonic-gate */ 22110Sstevel@tonic-gate error = ENETUNREACH; 22120Sstevel@tonic-gate } 22130Sstevel@tonic-gate /* FALLTHRU */ 22140Sstevel@tonic-gate 22150Sstevel@tonic-gate default: 22160Sstevel@tonic-gate ASSERT(need_unlock); 22170Sstevel@tonic-gate /* 22180Sstevel@tonic-gate * Fatal errors: clear SS_ISCONNECTING in case it was set, 22190Sstevel@tonic-gate * and invalidate local-address cache 22200Sstevel@tonic-gate */ 22210Sstevel@tonic-gate so->so_state &= ~(SS_ISCONNECTING | SS_LADDR_VALID); 22220Sstevel@tonic-gate /* A discon_ind might have already unbound us */ 22230Sstevel@tonic-gate if ((flags & _SOCONNECT_DID_BIND) && 22240Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) { 22250Sstevel@tonic-gate int err; 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate err = sotpi_unbind(so, 0); 22280Sstevel@tonic-gate /* LINTED - statement has no conseq */ 22290Sstevel@tonic-gate if (err) { 22300Sstevel@tonic-gate eprintsoline(so, err); 22310Sstevel@tonic-gate } 22320Sstevel@tonic-gate } 22330Sstevel@tonic-gate break; 22340Sstevel@tonic-gate } 22350Sstevel@tonic-gate if (need_unlock) 22360Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 22370Sstevel@tonic-gate mutex_exit(&so->so_lock); 22380Sstevel@tonic-gate return (error); 22390Sstevel@tonic-gate 22400Sstevel@tonic-gate so_bad: error = sogeterr(so); 22410Sstevel@tonic-gate bad: eprintsoline(so, error); 22420Sstevel@tonic-gate goto done; 22430Sstevel@tonic-gate } 22440Sstevel@tonic-gate 22450Sstevel@tonic-gate int 22460Sstevel@tonic-gate sotpi_shutdown(struct sonode *so, int how) 22470Sstevel@tonic-gate { 22480Sstevel@tonic-gate struct T_ordrel_req ordrel_req; 22490Sstevel@tonic-gate mblk_t *mp; 22500Sstevel@tonic-gate uint_t old_state, state_change; 22510Sstevel@tonic-gate int error = 0; 22520Sstevel@tonic-gate 22530Sstevel@tonic-gate dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n", 22540Sstevel@tonic-gate so, how, pr_state(so->so_state, so->so_mode))); 22550Sstevel@tonic-gate 22560Sstevel@tonic-gate mutex_enter(&so->so_lock); 22570Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 22580Sstevel@tonic-gate 22590Sstevel@tonic-gate /* 22600Sstevel@tonic-gate * SunOS 4.X has no check for datagram sockets. 22610Sstevel@tonic-gate * 5.X checks that it is connected (ENOTCONN) 22620Sstevel@tonic-gate * X/Open requires that we check the connected state. 22630Sstevel@tonic-gate */ 22640Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) { 22650Sstevel@tonic-gate if (!xnet_skip_checks) { 22660Sstevel@tonic-gate error = ENOTCONN; 22670Sstevel@tonic-gate if (xnet_check_print) { 22680Sstevel@tonic-gate printf("sockfs: X/Open shutdown check " 22690Sstevel@tonic-gate "caused ENOTCONN\n"); 22700Sstevel@tonic-gate } 22710Sstevel@tonic-gate } 22720Sstevel@tonic-gate goto done; 22730Sstevel@tonic-gate } 22740Sstevel@tonic-gate /* 22750Sstevel@tonic-gate * Record the current state and then perform any state changes. 22760Sstevel@tonic-gate * Then use the difference between the old and new states to 22770Sstevel@tonic-gate * determine which messages need to be sent. 22780Sstevel@tonic-gate * This prevents e.g. duplicate T_ORDREL_REQ when there are 22790Sstevel@tonic-gate * duplicate calls to shutdown(). 22800Sstevel@tonic-gate */ 22810Sstevel@tonic-gate old_state = so->so_state; 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate switch (how) { 22840Sstevel@tonic-gate case 0: 22850Sstevel@tonic-gate socantrcvmore(so); 22860Sstevel@tonic-gate break; 22870Sstevel@tonic-gate case 1: 22880Sstevel@tonic-gate socantsendmore(so); 22890Sstevel@tonic-gate break; 22900Sstevel@tonic-gate case 2: 22910Sstevel@tonic-gate socantsendmore(so); 22920Sstevel@tonic-gate socantrcvmore(so); 22930Sstevel@tonic-gate break; 22940Sstevel@tonic-gate default: 22950Sstevel@tonic-gate error = EINVAL; 22960Sstevel@tonic-gate goto done; 22970Sstevel@tonic-gate } 22980Sstevel@tonic-gate 22990Sstevel@tonic-gate /* 23000Sstevel@tonic-gate * Assumes that the SS_CANT* flags are never cleared in the above code. 23010Sstevel@tonic-gate */ 23020Sstevel@tonic-gate state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) - 23030Sstevel@tonic-gate (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)); 23040Sstevel@tonic-gate ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0); 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate switch (state_change) { 23070Sstevel@tonic-gate case 0: 23080Sstevel@tonic-gate dprintso(so, 1, 23090Sstevel@tonic-gate ("sotpi_shutdown: nothing to send in state 0x%x\n", 23100Sstevel@tonic-gate so->so_state)); 23110Sstevel@tonic-gate goto done; 23120Sstevel@tonic-gate 23130Sstevel@tonic-gate case SS_CANTRCVMORE: 23140Sstevel@tonic-gate mutex_exit(&so->so_lock); 23150Sstevel@tonic-gate strseteof(SOTOV(so), 1); 23160Sstevel@tonic-gate /* 23170Sstevel@tonic-gate * strseteof takes care of read side wakeups, 23180Sstevel@tonic-gate * pollwakeups, and signals. 23190Sstevel@tonic-gate */ 23200Sstevel@tonic-gate /* 23210Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems 23220Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 23230Sstevel@tonic-gate */ 23240Sstevel@tonic-gate mutex_enter(&so->so_lock); 23250Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 23260Sstevel@tonic-gate mutex_exit(&so->so_lock); 23270Sstevel@tonic-gate 23280Sstevel@tonic-gate /* Flush read side queue */ 23290Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL); 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate mutex_enter(&so->so_lock); 23320Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 23330Sstevel@tonic-gate break; 23340Sstevel@tonic-gate 23350Sstevel@tonic-gate case SS_CANTSENDMORE: 23360Sstevel@tonic-gate mutex_exit(&so->so_lock); 23370Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 23380Sstevel@tonic-gate mutex_enter(&so->so_lock); 23390Sstevel@tonic-gate break; 23400Sstevel@tonic-gate 23410Sstevel@tonic-gate case SS_CANTSENDMORE|SS_CANTRCVMORE: 23420Sstevel@tonic-gate mutex_exit(&so->so_lock); 23430Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 23440Sstevel@tonic-gate strseteof(SOTOV(so), 1); 23450Sstevel@tonic-gate /* 23460Sstevel@tonic-gate * strseteof takes care of read side wakeups, 23470Sstevel@tonic-gate * pollwakeups, and signals. 23480Sstevel@tonic-gate */ 23490Sstevel@tonic-gate /* 23500Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems 23510Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 23520Sstevel@tonic-gate */ 23530Sstevel@tonic-gate mutex_enter(&so->so_lock); 23540Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 23550Sstevel@tonic-gate mutex_exit(&so->so_lock); 23560Sstevel@tonic-gate 23570Sstevel@tonic-gate /* Flush read side queue */ 23580Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL); 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate mutex_enter(&so->so_lock); 23610Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 23620Sstevel@tonic-gate break; 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate 23650Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 23660Sstevel@tonic-gate 23670Sstevel@tonic-gate /* 23680Sstevel@tonic-gate * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them 23690Sstevel@tonic-gate * was set due to this call and the new state has both of them set: 23700Sstevel@tonic-gate * Send the AF_UNIX close indication 23710Sstevel@tonic-gate * For T_COTS send a discon_ind 23720Sstevel@tonic-gate * 23730Sstevel@tonic-gate * If cantsend was set due to this call: 23740Sstevel@tonic-gate * For T_COTSORD send an ordrel_ind 23750Sstevel@tonic-gate * 23760Sstevel@tonic-gate * Note that for T_CLTS there is no message sent here. 23770Sstevel@tonic-gate */ 23780Sstevel@tonic-gate if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) == 23790Sstevel@tonic-gate (SS_CANTRCVMORE|SS_CANTSENDMORE)) { 23800Sstevel@tonic-gate /* 23810Sstevel@tonic-gate * For SunOS 4.X compatibility we tell the other end 23820Sstevel@tonic-gate * that we are unable to receive at this point. 23830Sstevel@tonic-gate */ 23840Sstevel@tonic-gate if (so->so_family == AF_UNIX && so->so_serv_type != T_CLTS) 23850Sstevel@tonic-gate so_unix_close(so); 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate if (so->so_serv_type == T_COTS) 23880Sstevel@tonic-gate error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD); 23890Sstevel@tonic-gate } 23900Sstevel@tonic-gate if ((state_change & SS_CANTSENDMORE) && 23910Sstevel@tonic-gate (so->so_serv_type == T_COTS_ORD)) { 23920Sstevel@tonic-gate /* Send an orderly release */ 23930Sstevel@tonic-gate ordrel_req.PRIM_type = T_ORDREL_REQ; 23940Sstevel@tonic-gate 23950Sstevel@tonic-gate mutex_exit(&so->so_lock); 23960Sstevel@tonic-gate mp = soallocproto1(&ordrel_req, sizeof (ordrel_req), 23970Sstevel@tonic-gate 0, _ALLOC_SLEEP); 23980Sstevel@tonic-gate /* 23990Sstevel@tonic-gate * Send down the T_ORDREL_REQ even if there is flow control. 24000Sstevel@tonic-gate * This prevents shutdown from blocking. 24010Sstevel@tonic-gate * Note that there is no T_OK_ACK for ordrel_req. 24020Sstevel@tonic-gate */ 24030Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 24040Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 24050Sstevel@tonic-gate mutex_enter(&so->so_lock); 24060Sstevel@tonic-gate if (error) { 24070Sstevel@tonic-gate eprintsoline(so, error); 24080Sstevel@tonic-gate goto done; 24090Sstevel@tonic-gate } 24100Sstevel@tonic-gate } 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate done: 24130Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 24140Sstevel@tonic-gate mutex_exit(&so->so_lock); 24150Sstevel@tonic-gate return (error); 24160Sstevel@tonic-gate } 24170Sstevel@tonic-gate 24180Sstevel@tonic-gate /* 24190Sstevel@tonic-gate * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send 24200Sstevel@tonic-gate * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer 24210Sstevel@tonic-gate * that we have closed. 24220Sstevel@tonic-gate * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length 24230Sstevel@tonic-gate * T_UNITDATA_REQ containing the same option. 24240Sstevel@tonic-gate * 24250Sstevel@tonic-gate * For SOCK_DGRAM half-connections (somebody connected to this end 24260Sstevel@tonic-gate * but this end is not connect) we don't know where to send any 24270Sstevel@tonic-gate * SO_UNIX_CLOSE. 24280Sstevel@tonic-gate * 24290Sstevel@tonic-gate * We have to ignore stream head errors just in case there has been 24300Sstevel@tonic-gate * a shutdown(output). 24310Sstevel@tonic-gate * Ignore any flow control to try to get the message more quickly to the peer. 24320Sstevel@tonic-gate * While locally ignoring flow control solves the problem when there 24330Sstevel@tonic-gate * is only the loopback transport on the stream it would not provide 24340Sstevel@tonic-gate * the correct AF_UNIX socket semantics when one or more modules have 24350Sstevel@tonic-gate * been pushed. 24360Sstevel@tonic-gate */ 24370Sstevel@tonic-gate void 24380Sstevel@tonic-gate so_unix_close(struct sonode *so) 24390Sstevel@tonic-gate { 24400Sstevel@tonic-gate int error; 24410Sstevel@tonic-gate struct T_opthdr toh; 24420Sstevel@tonic-gate mblk_t *mp; 24430Sstevel@tonic-gate 24440Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 24450Sstevel@tonic-gate 24460Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 24470Sstevel@tonic-gate 24480Sstevel@tonic-gate if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) != 24490Sstevel@tonic-gate (SS_ISCONNECTED|SS_ISBOUND)) 24500Sstevel@tonic-gate return; 24510Sstevel@tonic-gate 24520Sstevel@tonic-gate dprintso(so, 1, ("so_unix_close(%p) %s\n", 24530Sstevel@tonic-gate so, pr_state(so->so_state, so->so_mode))); 24540Sstevel@tonic-gate 24550Sstevel@tonic-gate toh.level = SOL_SOCKET; 24560Sstevel@tonic-gate toh.name = SO_UNIX_CLOSE; 24570Sstevel@tonic-gate 24580Sstevel@tonic-gate /* zero length + header */ 24590Sstevel@tonic-gate toh.len = (t_uscalar_t)sizeof (struct T_opthdr); 24600Sstevel@tonic-gate toh.status = 0; 24610Sstevel@tonic-gate 24620Sstevel@tonic-gate if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) { 24630Sstevel@tonic-gate struct T_optdata_req tdr; 24640Sstevel@tonic-gate 24650Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ; 24660Sstevel@tonic-gate tdr.DATA_flag = 0; 24670Sstevel@tonic-gate 24680Sstevel@tonic-gate tdr.OPT_length = (t_scalar_t)sizeof (toh); 24690Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 24700Sstevel@tonic-gate 24710Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 24720Sstevel@tonic-gate mp = soallocproto2(&tdr, sizeof (tdr), 24730Sstevel@tonic-gate &toh, sizeof (toh), 0, _ALLOC_SLEEP); 24740Sstevel@tonic-gate } else { 24750Sstevel@tonic-gate struct T_unitdata_req tudr; 24760Sstevel@tonic-gate void *addr; 24770Sstevel@tonic-gate socklen_t addrlen; 24780Sstevel@tonic-gate void *src; 24790Sstevel@tonic-gate socklen_t srclen; 24800Sstevel@tonic-gate struct T_opthdr toh2; 24810Sstevel@tonic-gate t_scalar_t size; 24820Sstevel@tonic-gate 24830Sstevel@tonic-gate /* Connecteded DGRAM socket */ 24840Sstevel@tonic-gate 24850Sstevel@tonic-gate /* 24860Sstevel@tonic-gate * For AF_UNIX the destination address is translated to 24870Sstevel@tonic-gate * an internal name and the source address is passed as 24880Sstevel@tonic-gate * an option. 24890Sstevel@tonic-gate */ 24900Sstevel@tonic-gate /* 24910Sstevel@tonic-gate * Length and family checks. 24920Sstevel@tonic-gate */ 24930Sstevel@tonic-gate error = so_addr_verify(so, so->so_faddr_sa, 24940Sstevel@tonic-gate (t_uscalar_t)so->so_faddr_len); 24950Sstevel@tonic-gate if (error) { 24960Sstevel@tonic-gate eprintsoline(so, error); 24970Sstevel@tonic-gate return; 24980Sstevel@tonic-gate } 24990Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 25000Sstevel@tonic-gate /* 25010Sstevel@tonic-gate * Already have a transport internal address. Do not 25020Sstevel@tonic-gate * pass any (transport internal) source address. 25030Sstevel@tonic-gate */ 25040Sstevel@tonic-gate addr = so->so_faddr_sa; 25050Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_faddr_len; 25060Sstevel@tonic-gate src = NULL; 25070Sstevel@tonic-gate srclen = 0; 25080Sstevel@tonic-gate } else { 25090Sstevel@tonic-gate /* 25100Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 25110Sstevel@tonic-gate * and translate the remote address. 25120Sstevel@tonic-gate * Holding so_lock thus so_laddr_sa can not change. 25130Sstevel@tonic-gate */ 25140Sstevel@tonic-gate src = so->so_laddr_sa; 25150Sstevel@tonic-gate srclen = (socklen_t)so->so_laddr_len; 25160Sstevel@tonic-gate dprintso(so, 1, 25170Sstevel@tonic-gate ("so_ux_close: srclen %d, src %p\n", 25180Sstevel@tonic-gate srclen, src)); 25190Sstevel@tonic-gate error = so_ux_addr_xlate(so, 25200Sstevel@tonic-gate so->so_faddr_sa, 25210Sstevel@tonic-gate (socklen_t)so->so_faddr_len, 0, 25220Sstevel@tonic-gate &addr, &addrlen); 25230Sstevel@tonic-gate if (error) { 25240Sstevel@tonic-gate eprintsoline(so, error); 25250Sstevel@tonic-gate return; 25260Sstevel@tonic-gate } 25270Sstevel@tonic-gate } 25280Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 25290Sstevel@tonic-gate tudr.DEST_length = addrlen; 25300Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 25310Sstevel@tonic-gate if (srclen == 0) { 25320Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)sizeof (toh); 25330Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 25340Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen)); 25350Sstevel@tonic-gate 25360Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 25370Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 25380Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 25390Sstevel@tonic-gate addr, addrlen, size, _ALLOC_SLEEP); 25400Sstevel@tonic-gate mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen); 25410Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 25420Sstevel@tonic-gate } else { 25430Sstevel@tonic-gate /* 25440Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a 25450Sstevel@tonic-gate * source address option. 25460Sstevel@tonic-gate */ 25470Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) + 25480Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen)); 25490Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 25500Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen)); 25510Sstevel@tonic-gate 25520Sstevel@tonic-gate toh2.level = SOL_SOCKET; 25530Sstevel@tonic-gate toh2.name = SO_SRCADDR; 25540Sstevel@tonic-gate toh2.len = (t_uscalar_t)(srclen + 25550Sstevel@tonic-gate sizeof (struct T_opthdr)); 25560Sstevel@tonic-gate toh2.status = 0; 25570Sstevel@tonic-gate 25580Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 25590Sstevel@tonic-gate 25600Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 25610Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 25620Sstevel@tonic-gate addr, addrlen, size, _ALLOC_SLEEP); 25630Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 25640Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 25650Sstevel@tonic-gate soappendmsg(mp, &toh2, sizeof (toh2)); 25660Sstevel@tonic-gate soappendmsg(mp, src, srclen); 25670Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 25680Sstevel@tonic-gate } 25690Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 25700Sstevel@tonic-gate } 25710Sstevel@tonic-gate mutex_exit(&so->so_lock); 25720Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 25730Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 25740Sstevel@tonic-gate mutex_enter(&so->so_lock); 25750Sstevel@tonic-gate } 25760Sstevel@tonic-gate 25770Sstevel@tonic-gate /* 25780Sstevel@tonic-gate * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK. 25790Sstevel@tonic-gate */ 25800Sstevel@tonic-gate int 25810Sstevel@tonic-gate sorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags) 25820Sstevel@tonic-gate { 25830Sstevel@tonic-gate mblk_t *mp, *nmp; 25840Sstevel@tonic-gate int error; 25850Sstevel@tonic-gate 25860Sstevel@tonic-gate dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", so, msg, flags)); 25870Sstevel@tonic-gate 25880Sstevel@tonic-gate /* 25890Sstevel@tonic-gate * There is never any oob data with addresses or control since 25900Sstevel@tonic-gate * the T_EXDATA_IND does not carry any options. 25910Sstevel@tonic-gate */ 25920Sstevel@tonic-gate msg->msg_controllen = 0; 25930Sstevel@tonic-gate msg->msg_namelen = 0; 25940Sstevel@tonic-gate 25950Sstevel@tonic-gate mutex_enter(&so->so_lock); 25960Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 25970Sstevel@tonic-gate if ((so->so_options & SO_OOBINLINE) || 25980Sstevel@tonic-gate (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) { 25990Sstevel@tonic-gate dprintso(so, 1, ("sorecvoob: inline or data consumed\n")); 26000Sstevel@tonic-gate mutex_exit(&so->so_lock); 26010Sstevel@tonic-gate return (EINVAL); 26020Sstevel@tonic-gate } 26030Sstevel@tonic-gate if (!(so->so_state & SS_HAVEOOBDATA)) { 26040Sstevel@tonic-gate dprintso(so, 1, ("sorecvoob: no data yet\n")); 26050Sstevel@tonic-gate mutex_exit(&so->so_lock); 26060Sstevel@tonic-gate return (EWOULDBLOCK); 26070Sstevel@tonic-gate } 26080Sstevel@tonic-gate ASSERT(so->so_oobmsg != NULL); 26090Sstevel@tonic-gate mp = so->so_oobmsg; 26100Sstevel@tonic-gate if (flags & MSG_PEEK) { 26110Sstevel@tonic-gate /* 26120Sstevel@tonic-gate * Since recv* can not return ENOBUFS we can not use dupmsg. 26130Sstevel@tonic-gate * Instead we revert to the consolidation private 26140Sstevel@tonic-gate * allocb_wait plus bcopy. 26150Sstevel@tonic-gate */ 26160Sstevel@tonic-gate mblk_t *mp1; 26170Sstevel@tonic-gate 26180Sstevel@tonic-gate mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL); 26190Sstevel@tonic-gate ASSERT(mp1); 26200Sstevel@tonic-gate 26210Sstevel@tonic-gate while (mp != NULL) { 26220Sstevel@tonic-gate ssize_t size; 26230Sstevel@tonic-gate 26240Sstevel@tonic-gate size = MBLKL(mp); 26250Sstevel@tonic-gate bcopy(mp->b_rptr, mp1->b_wptr, size); 26260Sstevel@tonic-gate mp1->b_wptr += size; 26270Sstevel@tonic-gate ASSERT(mp1->b_wptr <= mp1->b_datap->db_lim); 26280Sstevel@tonic-gate mp = mp->b_cont; 26290Sstevel@tonic-gate } 26300Sstevel@tonic-gate mp = mp1; 26310Sstevel@tonic-gate } else { 26320Sstevel@tonic-gate /* 26330Sstevel@tonic-gate * Update the state indicating that the data has been consumed. 26340Sstevel@tonic-gate * Keep SS_OOBPEND set until data is consumed past the mark. 26350Sstevel@tonic-gate */ 26360Sstevel@tonic-gate so->so_oobmsg = NULL; 26370Sstevel@tonic-gate so->so_state ^= SS_HAVEOOBDATA|SS_HADOOBDATA; 26380Sstevel@tonic-gate } 26390Sstevel@tonic-gate dprintso(so, 1, 26400Sstevel@tonic-gate ("after recvoob(%p): counts %d/%d state %s\n", 26410Sstevel@tonic-gate so, so->so_oobsigcnt, 26420Sstevel@tonic-gate so->so_oobcnt, pr_state(so->so_state, so->so_mode))); 26430Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 26440Sstevel@tonic-gate mutex_exit(&so->so_lock); 26450Sstevel@tonic-gate 26460Sstevel@tonic-gate error = 0; 26470Sstevel@tonic-gate nmp = mp; 26480Sstevel@tonic-gate while (nmp != NULL && uiop->uio_resid > 0) { 26490Sstevel@tonic-gate ssize_t n = MBLKL(nmp); 26500Sstevel@tonic-gate 26510Sstevel@tonic-gate n = MIN(n, uiop->uio_resid); 26520Sstevel@tonic-gate if (n > 0) 26530Sstevel@tonic-gate error = uiomove(nmp->b_rptr, n, 26540Sstevel@tonic-gate UIO_READ, uiop); 26550Sstevel@tonic-gate if (error) 26560Sstevel@tonic-gate break; 26570Sstevel@tonic-gate nmp = nmp->b_cont; 26580Sstevel@tonic-gate } 26590Sstevel@tonic-gate freemsg(mp); 26600Sstevel@tonic-gate return (error); 26610Sstevel@tonic-gate } 26620Sstevel@tonic-gate 26630Sstevel@tonic-gate /* 26640Sstevel@tonic-gate * Called by sotpi_recvmsg when reading a non-zero amount of data. 26650Sstevel@tonic-gate * In addition, the caller typically verifies that there is some 26660Sstevel@tonic-gate * potential state to clear by checking 26670Sstevel@tonic-gate * if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) 26680Sstevel@tonic-gate * before calling this routine. 26690Sstevel@tonic-gate * Note that such a check can be made without holding so_lock since 26700Sstevel@tonic-gate * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg 26710Sstevel@tonic-gate * decrements so_oobsigcnt. 26720Sstevel@tonic-gate * 26730Sstevel@tonic-gate * When data is read *after* the point that all pending 26740Sstevel@tonic-gate * oob data has been consumed the oob indication is cleared. 26750Sstevel@tonic-gate * 26760Sstevel@tonic-gate * This logic keeps select/poll returning POLLRDBAND and 26770Sstevel@tonic-gate * SIOCATMARK returning true until we have read past 26780Sstevel@tonic-gate * the mark. 26790Sstevel@tonic-gate */ 26800Sstevel@tonic-gate static void 26810Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so) 26820Sstevel@tonic-gate { 26830Sstevel@tonic-gate mutex_enter(&so->so_lock); 26840Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 26850Sstevel@tonic-gate dprintso(so, 1, 26860Sstevel@tonic-gate ("sorecv_update_oobstate: counts %d/%d state %s\n", 26870Sstevel@tonic-gate so->so_oobsigcnt, 26880Sstevel@tonic-gate so->so_oobcnt, pr_state(so->so_state, so->so_mode))); 26890Sstevel@tonic-gate if (so->so_oobsigcnt == 0) { 26900Sstevel@tonic-gate /* No more pending oob indications */ 26910Sstevel@tonic-gate so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK); 26920Sstevel@tonic-gate freemsg(so->so_oobmsg); 26930Sstevel@tonic-gate so->so_oobmsg = NULL; 26940Sstevel@tonic-gate } 26950Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 26960Sstevel@tonic-gate mutex_exit(&so->so_lock); 26970Sstevel@tonic-gate } 26980Sstevel@tonic-gate 26990Sstevel@tonic-gate /* 27000Sstevel@tonic-gate * Handle recv* calls for an so which has NL7C saved recv mblk_t(s). 27010Sstevel@tonic-gate */ 27020Sstevel@tonic-gate static int 27030Sstevel@tonic-gate nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp) 27040Sstevel@tonic-gate { 27050Sstevel@tonic-gate int error = 0; 27060Sstevel@tonic-gate mblk_t *tmp = NULL; 27070Sstevel@tonic-gate mblk_t *pmp = NULL; 27080Sstevel@tonic-gate mblk_t *nmp = so->so_nl7c_rcv_mp; 27090Sstevel@tonic-gate 27100Sstevel@tonic-gate ASSERT(nmp != NULL); 27110Sstevel@tonic-gate 27120Sstevel@tonic-gate while (nmp != NULL && uiop->uio_resid > 0) { 27130Sstevel@tonic-gate ssize_t n; 27140Sstevel@tonic-gate 27150Sstevel@tonic-gate if (DB_TYPE(nmp) == M_DATA) { 27160Sstevel@tonic-gate /* 27170Sstevel@tonic-gate * We have some data, uiomove up to resid bytes. 27180Sstevel@tonic-gate */ 27190Sstevel@tonic-gate n = MIN(MBLKL(nmp), uiop->uio_resid); 27200Sstevel@tonic-gate if (n > 0) 27210Sstevel@tonic-gate error = uiomove(nmp->b_rptr, n, UIO_READ, uiop); 27220Sstevel@tonic-gate if (error) 27230Sstevel@tonic-gate break; 27240Sstevel@tonic-gate nmp->b_rptr += n; 27250Sstevel@tonic-gate if (nmp->b_rptr == nmp->b_wptr) { 27260Sstevel@tonic-gate pmp = nmp; 27270Sstevel@tonic-gate nmp = nmp->b_cont; 27280Sstevel@tonic-gate } 27290Sstevel@tonic-gate } else { 27300Sstevel@tonic-gate /* 27310Sstevel@tonic-gate * We only handle data, save for caller to handle. 27320Sstevel@tonic-gate */ 27330Sstevel@tonic-gate if (pmp != NULL) { 27340Sstevel@tonic-gate pmp->b_cont = nmp->b_cont; 27350Sstevel@tonic-gate } 27360Sstevel@tonic-gate nmp->b_cont = NULL; 27370Sstevel@tonic-gate if (*rmp == NULL) { 27380Sstevel@tonic-gate *rmp = nmp; 27390Sstevel@tonic-gate } else { 27400Sstevel@tonic-gate tmp->b_next = nmp; 27410Sstevel@tonic-gate } 27420Sstevel@tonic-gate nmp = nmp->b_cont; 27430Sstevel@tonic-gate tmp = nmp; 27440Sstevel@tonic-gate } 27450Sstevel@tonic-gate } 27460Sstevel@tonic-gate if (pmp != NULL) { 27470Sstevel@tonic-gate /* Free any mblk_t(s) which we have consumed */ 27480Sstevel@tonic-gate pmp->b_cont = NULL; 27490Sstevel@tonic-gate freemsg(so->so_nl7c_rcv_mp); 27500Sstevel@tonic-gate } 27510Sstevel@tonic-gate if ((so->so_nl7c_rcv_mp = nmp) == NULL) { 27520Sstevel@tonic-gate /* Last mblk_t so return the saved rval from kstrgetmsg() */ 27530Sstevel@tonic-gate rp->r_vals = so->so_nl7c_rcv_rval; 27540Sstevel@tonic-gate so->so_nl7c_rcv_rval = 0; 27550Sstevel@tonic-gate } else { 27560Sstevel@tonic-gate /* More mblk_t(s) to process so no rval to return */ 27570Sstevel@tonic-gate rp->r_vals = 0; 27580Sstevel@tonic-gate } 27590Sstevel@tonic-gate return (error); 27600Sstevel@tonic-gate } 27610Sstevel@tonic-gate 27620Sstevel@tonic-gate /* 27630Sstevel@tonic-gate * Receive the next message on the queue. 27640Sstevel@tonic-gate * If msg_controllen is non-zero when called the caller is interested in 27650Sstevel@tonic-gate * any received control info (options). 27660Sstevel@tonic-gate * If msg_namelen is non-zero when called the caller is interested in 27670Sstevel@tonic-gate * any received source address. 27680Sstevel@tonic-gate * The routine returns with msg_control and msg_name pointing to 27690Sstevel@tonic-gate * kmem_alloc'ed memory which the caller has to free. 27700Sstevel@tonic-gate */ 27710Sstevel@tonic-gate int 27720Sstevel@tonic-gate sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop) 27730Sstevel@tonic-gate { 27740Sstevel@tonic-gate union T_primitives *tpr; 27750Sstevel@tonic-gate mblk_t *mp; 27760Sstevel@tonic-gate uchar_t pri; 27770Sstevel@tonic-gate int pflag, opflag; 27780Sstevel@tonic-gate void *control; 27790Sstevel@tonic-gate t_uscalar_t controllen; 27800Sstevel@tonic-gate t_uscalar_t namelen; 27810Sstevel@tonic-gate int so_state = so->so_state; /* Snapshot */ 27820Sstevel@tonic-gate ssize_t saved_resid; 27830Sstevel@tonic-gate int error; 27840Sstevel@tonic-gate rval_t rval; 27850Sstevel@tonic-gate int flags; 27860Sstevel@tonic-gate clock_t timout; 27870Sstevel@tonic-gate int first; 27880Sstevel@tonic-gate 27890Sstevel@tonic-gate flags = msg->msg_flags; 27900Sstevel@tonic-gate msg->msg_flags = 0; 27910Sstevel@tonic-gate 27920Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n", 27930Sstevel@tonic-gate so, msg, flags, 27940Sstevel@tonic-gate pr_state(so->so_state, so->so_mode), so->so_error)); 27950Sstevel@tonic-gate 27960Sstevel@tonic-gate /* 27970Sstevel@tonic-gate * If we are not connected because we have never been connected 27980Sstevel@tonic-gate * we return ENOTCONN. If we have been connected (but are no longer 27990Sstevel@tonic-gate * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return 28000Sstevel@tonic-gate * the EOF. 28010Sstevel@tonic-gate * 28020Sstevel@tonic-gate * An alternative would be to post an ENOTCONN error in stream head 28030Sstevel@tonic-gate * (read+write) and clear it when we're connected. However, that error 28040Sstevel@tonic-gate * would cause incorrect poll/select behavior! 28050Sstevel@tonic-gate */ 28060Sstevel@tonic-gate if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 && 28070Sstevel@tonic-gate (so->so_mode & SM_CONNREQUIRED)) { 28080Sstevel@tonic-gate return (ENOTCONN); 28090Sstevel@tonic-gate } 28100Sstevel@tonic-gate 28110Sstevel@tonic-gate /* 28120Sstevel@tonic-gate * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but 28130Sstevel@tonic-gate * after checking that the read queue is empty) and returns zero. 28140Sstevel@tonic-gate * This implementation will sleep (in kstrgetmsg) even if uio_resid 28150Sstevel@tonic-gate * is zero. 28160Sstevel@tonic-gate */ 28170Sstevel@tonic-gate 28180Sstevel@tonic-gate if (flags & MSG_OOB) { 28190Sstevel@tonic-gate /* Check that the transport supports OOB */ 28200Sstevel@tonic-gate if (!(so->so_mode & SM_EXDATA)) 28210Sstevel@tonic-gate return (EOPNOTSUPP); 28220Sstevel@tonic-gate return (sorecvoob(so, msg, uiop, flags)); 28230Sstevel@tonic-gate } 28240Sstevel@tonic-gate 28250Sstevel@tonic-gate /* 28260Sstevel@tonic-gate * Set msg_controllen and msg_namelen to zero here to make it 28270Sstevel@tonic-gate * simpler in the cases that no control or name is returned. 28280Sstevel@tonic-gate */ 28290Sstevel@tonic-gate controllen = msg->msg_controllen; 28300Sstevel@tonic-gate namelen = msg->msg_namelen; 28310Sstevel@tonic-gate msg->msg_controllen = 0; 28320Sstevel@tonic-gate msg->msg_namelen = 0; 28330Sstevel@tonic-gate 28340Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n", 28350Sstevel@tonic-gate namelen, controllen)); 28360Sstevel@tonic-gate 28370Sstevel@tonic-gate /* 28380Sstevel@tonic-gate * If an NL7C enabled socket and not waiting for write data. 28390Sstevel@tonic-gate */ 28400Sstevel@tonic-gate mutex_enter(&so->so_lock); 28410Sstevel@tonic-gate if ((so->so_nl7c_flags & (NL7C_ENABLED|NL7C_WAITWRITE)) == 28420Sstevel@tonic-gate NL7C_ENABLED) { 28430Sstevel@tonic-gate if (so->so_nl7c_uri) { 28440Sstevel@tonic-gate /* 28450Sstevel@tonic-gate * Close uri processing for a previous request. 28460Sstevel@tonic-gate */ 28470Sstevel@tonic-gate nl7c_close(so); 28480Sstevel@tonic-gate } 28490Sstevel@tonic-gate if (nl7c_process(so, 28500Sstevel@tonic-gate (so->so_state & (SS_NONBLOCK|SS_NDELAY)), 28510Sstevel@tonic-gate (int)((tcp_t *)so->so_priv)->tcp_mss)) { 28520Sstevel@tonic-gate /* 28530Sstevel@tonic-gate * NL7C has completed processing on the socket, 28540Sstevel@tonic-gate * clear the enabled bit as no further NL7C 28550Sstevel@tonic-gate * processing will be needed. 28560Sstevel@tonic-gate */ 28570Sstevel@tonic-gate so->so_nl7c_flags = 0; 28580Sstevel@tonic-gate } 28590Sstevel@tonic-gate } 28600Sstevel@tonic-gate 28610Sstevel@tonic-gate /* 28620Sstevel@tonic-gate * Only one reader is allowed at any given time. This is needed 28630Sstevel@tonic-gate * for T_EXDATA handling and, in the future, MSG_WAITALL. 28640Sstevel@tonic-gate * 28650Sstevel@tonic-gate * This is slightly different that BSD behavior in that it fails with 28660Sstevel@tonic-gate * EWOULDBLOCK when using nonblocking io. In BSD the read queue access 28670Sstevel@tonic-gate * is single-threaded using sblock(), which is dropped while waiting 28680Sstevel@tonic-gate * for data to appear. The difference shows up e.g. if one 28690Sstevel@tonic-gate * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor 28700Sstevel@tonic-gate * does use nonblocking io and different threads are reading each 28710Sstevel@tonic-gate * file descriptor. In BSD there would never be an EWOULDBLOCK error 28720Sstevel@tonic-gate * in this case as long as the read queue doesn't get empty. 28730Sstevel@tonic-gate * In this implementation the thread using nonblocking io can 28740Sstevel@tonic-gate * get an EWOULDBLOCK error due to the blocking thread executing 28750Sstevel@tonic-gate * e.g. in the uiomove in kstrgetmsg. 28760Sstevel@tonic-gate * This difference is not believed to be significant. 28770Sstevel@tonic-gate */ 28780Sstevel@tonic-gate error = so_lock_read_intr(so, uiop->uio_fmode); /* Set SOREADLOCKED */ 28790Sstevel@tonic-gate mutex_exit(&so->so_lock); 28800Sstevel@tonic-gate if (error) 28810Sstevel@tonic-gate return (error); 28820Sstevel@tonic-gate 28830Sstevel@tonic-gate /* 28840Sstevel@tonic-gate * Tell kstrgetmsg to not inspect the stream head errors until all 28850Sstevel@tonic-gate * queued data has been consumed. 28860Sstevel@tonic-gate * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set. 28870Sstevel@tonic-gate * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block. 28880Sstevel@tonic-gate * 28890Sstevel@tonic-gate * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and 28900Sstevel@tonic-gate * to T_OPTDATA_IND that do not contain any user-visible control msg. 28910Sstevel@tonic-gate * Note that MSG_WAITALL set with MSG_PEEK is a noop. 28920Sstevel@tonic-gate */ 28930Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR; 28940Sstevel@tonic-gate if (flags & MSG_PEEK) { 28950Sstevel@tonic-gate pflag |= MSG_IPEEK; 28960Sstevel@tonic-gate flags &= ~MSG_WAITALL; 28970Sstevel@tonic-gate } 28980Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC) 28990Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL; 29000Sstevel@tonic-gate 29010Sstevel@tonic-gate if (flags & MSG_DONTWAIT) 29020Sstevel@tonic-gate timout = 0; 29030Sstevel@tonic-gate else 29040Sstevel@tonic-gate timout = -1; 29050Sstevel@tonic-gate opflag = pflag; 29060Sstevel@tonic-gate first = 1; 29070Sstevel@tonic-gate 29080Sstevel@tonic-gate /* 29090Sstevel@tonic-gate * If so saved NL7C rcv mblk_t(s) uiomove them first 29100Sstevel@tonic-gate * else get'm from the streamhead. 29110Sstevel@tonic-gate */ 29120Sstevel@tonic-gate retry: 29130Sstevel@tonic-gate saved_resid = uiop->uio_resid; 29140Sstevel@tonic-gate pri = 0; 29150Sstevel@tonic-gate mp = NULL; 29160Sstevel@tonic-gate if (so->so_nl7c_rcv_mp != NULL) { 29170Sstevel@tonic-gate error = nl7c_sorecv(so, &mp, uiop, &rval); 29180Sstevel@tonic-gate } else { 29190Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag, 29200Sstevel@tonic-gate timout, &rval); 29210Sstevel@tonic-gate } 29220Sstevel@tonic-gate if (error) { 29230Sstevel@tonic-gate switch (error) { 29240Sstevel@tonic-gate case EINTR: 29250Sstevel@tonic-gate case EWOULDBLOCK: 29260Sstevel@tonic-gate if (!first) 29270Sstevel@tonic-gate error = 0; 29280Sstevel@tonic-gate break; 29290Sstevel@tonic-gate case ETIME: 29300Sstevel@tonic-gate /* Returned from kstrgetmsg when timeout expires */ 29310Sstevel@tonic-gate if (!first) 29320Sstevel@tonic-gate error = 0; 29330Sstevel@tonic-gate else 29340Sstevel@tonic-gate error = EWOULDBLOCK; 29350Sstevel@tonic-gate break; 29360Sstevel@tonic-gate default: 29370Sstevel@tonic-gate eprintsoline(so, error); 29380Sstevel@tonic-gate break; 29390Sstevel@tonic-gate } 29400Sstevel@tonic-gate mutex_enter(&so->so_lock); 29410Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 29420Sstevel@tonic-gate mutex_exit(&so->so_lock); 29430Sstevel@tonic-gate return (error); 29440Sstevel@tonic-gate } 29450Sstevel@tonic-gate /* 29460Sstevel@tonic-gate * For datagrams the MOREDATA flag is used to set MSG_TRUNC. 29470Sstevel@tonic-gate * For non-datagrams MOREDATA is used to set MSG_EOR. 29480Sstevel@tonic-gate */ 29490Sstevel@tonic-gate ASSERT(!(rval.r_val1 & MORECTL)); 29500Sstevel@tonic-gate if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC)) 29510Sstevel@tonic-gate msg->msg_flags |= MSG_TRUNC; 29520Sstevel@tonic-gate 29530Sstevel@tonic-gate if (mp == NULL) { 29540Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n")); 29550Sstevel@tonic-gate /* 29560Sstevel@tonic-gate * 4.3BSD and 4.4BSD clears the mark when peeking across it. 29570Sstevel@tonic-gate * The draft Posix socket spec states that the mark should 29580Sstevel@tonic-gate * not be cleared when peeking. We follow the latter. 29590Sstevel@tonic-gate */ 29600Sstevel@tonic-gate if ((so->so_state & 29610Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 29620Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 29630Sstevel@tonic-gate !(flags & MSG_PEEK)) { 29640Sstevel@tonic-gate sorecv_update_oobstate(so); 29650Sstevel@tonic-gate } 29660Sstevel@tonic-gate 29670Sstevel@tonic-gate mutex_enter(&so->so_lock); 29680Sstevel@tonic-gate /* Set MSG_EOR based on MOREDATA */ 29690Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) { 29700Sstevel@tonic-gate if (so->so_state & SS_SAVEDEOR) { 29710Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 29720Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 29730Sstevel@tonic-gate } 29740Sstevel@tonic-gate } 29750Sstevel@tonic-gate /* 29760Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 29770Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 29780Sstevel@tonic-gate */ 29790Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 29800Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 29810Sstevel@tonic-gate mutex_exit(&so->so_lock); 29820Sstevel@tonic-gate first = 0; 29830Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 29840Sstevel@tonic-gate goto retry; 29850Sstevel@tonic-gate } 29860Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 29870Sstevel@tonic-gate mutex_exit(&so->so_lock); 29880Sstevel@tonic-gate return (0); 29890Sstevel@tonic-gate } 29900Sstevel@tonic-gate 29910Sstevel@tonic-gate /* strsock_proto has already verified length and alignment */ 29920Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr; 29930Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type)); 29940Sstevel@tonic-gate 29950Sstevel@tonic-gate switch (tpr->type) { 29960Sstevel@tonic-gate case T_DATA_IND: { 29970Sstevel@tonic-gate if ((so->so_state & 29980Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 29990Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 30000Sstevel@tonic-gate !(flags & MSG_PEEK)) { 30010Sstevel@tonic-gate sorecv_update_oobstate(so); 30020Sstevel@tonic-gate } 30030Sstevel@tonic-gate 30040Sstevel@tonic-gate /* 30050Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on 30060Sstevel@tonic-gate * MORE_flag and MOREDATA. 30070Sstevel@tonic-gate */ 30080Sstevel@tonic-gate mutex_enter(&so->so_lock); 30090Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 30100Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) { 30110Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) 30120Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 30130Sstevel@tonic-gate else 30140Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR; 30150Sstevel@tonic-gate } 30160Sstevel@tonic-gate freemsg(mp); 30170Sstevel@tonic-gate /* 30180Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 30190Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 30200Sstevel@tonic-gate */ 30210Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 30220Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 30230Sstevel@tonic-gate mutex_exit(&so->so_lock); 30240Sstevel@tonic-gate first = 0; 30250Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 30260Sstevel@tonic-gate goto retry; 30270Sstevel@tonic-gate } 30280Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 30290Sstevel@tonic-gate mutex_exit(&so->so_lock); 30300Sstevel@tonic-gate return (0); 30310Sstevel@tonic-gate } 30320Sstevel@tonic-gate case T_UNITDATA_IND: { 30330Sstevel@tonic-gate void *addr; 30340Sstevel@tonic-gate t_uscalar_t addrlen; 30350Sstevel@tonic-gate void *abuf; 30360Sstevel@tonic-gate t_uscalar_t optlen; 30370Sstevel@tonic-gate void *opt; 30380Sstevel@tonic-gate 30390Sstevel@tonic-gate if ((so->so_state & 30400Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 30410Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 30420Sstevel@tonic-gate !(flags & MSG_PEEK)) { 30430Sstevel@tonic-gate sorecv_update_oobstate(so); 30440Sstevel@tonic-gate } 30450Sstevel@tonic-gate 30460Sstevel@tonic-gate if (namelen != 0) { 30470Sstevel@tonic-gate /* Caller wants source address */ 30480Sstevel@tonic-gate addrlen = tpr->unitdata_ind.SRC_length; 30490Sstevel@tonic-gate addr = sogetoff(mp, 30500Sstevel@tonic-gate tpr->unitdata_ind.SRC_offset, 30510Sstevel@tonic-gate addrlen, 1); 30520Sstevel@tonic-gate if (addr == NULL) { 30530Sstevel@tonic-gate freemsg(mp); 30540Sstevel@tonic-gate error = EPROTO; 30550Sstevel@tonic-gate eprintsoline(so, error); 30560Sstevel@tonic-gate goto err; 30570Sstevel@tonic-gate } 30580Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 30590Sstevel@tonic-gate /* 30600Sstevel@tonic-gate * Can not use the transport level address. 30610Sstevel@tonic-gate * If there is a SO_SRCADDR option carrying 30620Sstevel@tonic-gate * the socket level address it will be 30630Sstevel@tonic-gate * extracted below. 30640Sstevel@tonic-gate */ 30650Sstevel@tonic-gate addr = NULL; 30660Sstevel@tonic-gate addrlen = 0; 30670Sstevel@tonic-gate } 30680Sstevel@tonic-gate } 30690Sstevel@tonic-gate optlen = tpr->unitdata_ind.OPT_length; 30700Sstevel@tonic-gate if (optlen != 0) { 30710Sstevel@tonic-gate t_uscalar_t ncontrollen; 30720Sstevel@tonic-gate 30730Sstevel@tonic-gate /* 30740Sstevel@tonic-gate * Extract any source address option. 30750Sstevel@tonic-gate * Determine how large cmsg buffer is needed. 30760Sstevel@tonic-gate */ 30770Sstevel@tonic-gate opt = sogetoff(mp, 30780Sstevel@tonic-gate tpr->unitdata_ind.OPT_offset, 30790Sstevel@tonic-gate optlen, __TPI_ALIGN_SIZE); 30800Sstevel@tonic-gate 30810Sstevel@tonic-gate if (opt == NULL) { 30820Sstevel@tonic-gate freemsg(mp); 30830Sstevel@tonic-gate error = EPROTO; 30840Sstevel@tonic-gate eprintsoline(so, error); 30850Sstevel@tonic-gate goto err; 30860Sstevel@tonic-gate } 30870Sstevel@tonic-gate if (so->so_family == AF_UNIX) 30880Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &addr, &addrlen); 30890Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen, 30900Sstevel@tonic-gate !(flags & MSG_XPG4_2)); 30910Sstevel@tonic-gate if (controllen != 0) 30920Sstevel@tonic-gate controllen = ncontrollen; 30930Sstevel@tonic-gate else if (ncontrollen != 0) 30940Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC; 30950Sstevel@tonic-gate } else { 30960Sstevel@tonic-gate controllen = 0; 30970Sstevel@tonic-gate } 30980Sstevel@tonic-gate 30990Sstevel@tonic-gate if (namelen != 0) { 31000Sstevel@tonic-gate /* 31010Sstevel@tonic-gate * Return address to caller. 31020Sstevel@tonic-gate * Caller handles truncation if length 31030Sstevel@tonic-gate * exceeds msg_namelen. 31040Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by 31050Sstevel@tonic-gate * the sender's copyin_name(). 31060Sstevel@tonic-gate */ 31070Sstevel@tonic-gate abuf = kmem_alloc(addrlen, KM_SLEEP); 31080Sstevel@tonic-gate 31090Sstevel@tonic-gate bcopy(addr, abuf, addrlen); 31100Sstevel@tonic-gate msg->msg_name = abuf; 31110Sstevel@tonic-gate msg->msg_namelen = addrlen; 31120Sstevel@tonic-gate } 31130Sstevel@tonic-gate 31140Sstevel@tonic-gate if (controllen != 0) { 31150Sstevel@tonic-gate /* 31160Sstevel@tonic-gate * Return control msg to caller. 31170Sstevel@tonic-gate * Caller handles truncation if length 31180Sstevel@tonic-gate * exceeds msg_controllen. 31190Sstevel@tonic-gate */ 31200Sstevel@tonic-gate control = kmem_alloc(controllen, KM_SLEEP); 31210Sstevel@tonic-gate 31220Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen, 31230Sstevel@tonic-gate !(flags & MSG_XPG4_2), 31240Sstevel@tonic-gate control, controllen); 31250Sstevel@tonic-gate if (error) { 31260Sstevel@tonic-gate freemsg(mp); 31270Sstevel@tonic-gate if (msg->msg_namelen != 0) 31280Sstevel@tonic-gate kmem_free(msg->msg_name, 31290Sstevel@tonic-gate msg->msg_namelen); 31300Sstevel@tonic-gate kmem_free(control, controllen); 31310Sstevel@tonic-gate eprintsoline(so, error); 31320Sstevel@tonic-gate goto err; 31330Sstevel@tonic-gate } 31340Sstevel@tonic-gate msg->msg_control = control; 31350Sstevel@tonic-gate msg->msg_controllen = controllen; 31360Sstevel@tonic-gate } 31370Sstevel@tonic-gate 31380Sstevel@tonic-gate freemsg(mp); 31390Sstevel@tonic-gate mutex_enter(&so->so_lock); 31400Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 31410Sstevel@tonic-gate mutex_exit(&so->so_lock); 31420Sstevel@tonic-gate return (0); 31430Sstevel@tonic-gate } 31440Sstevel@tonic-gate case T_OPTDATA_IND: { 31450Sstevel@tonic-gate struct T_optdata_req *tdr; 31460Sstevel@tonic-gate void *opt; 31470Sstevel@tonic-gate t_uscalar_t optlen; 31480Sstevel@tonic-gate 31490Sstevel@tonic-gate if ((so->so_state & 31500Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 31510Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 31520Sstevel@tonic-gate !(flags & MSG_PEEK)) { 31530Sstevel@tonic-gate sorecv_update_oobstate(so); 31540Sstevel@tonic-gate } 31550Sstevel@tonic-gate 31560Sstevel@tonic-gate tdr = (struct T_optdata_req *)mp->b_rptr; 31570Sstevel@tonic-gate optlen = tdr->OPT_length; 31580Sstevel@tonic-gate if (optlen != 0) { 31590Sstevel@tonic-gate t_uscalar_t ncontrollen; 31600Sstevel@tonic-gate /* 31610Sstevel@tonic-gate * Determine how large cmsg buffer is needed. 31620Sstevel@tonic-gate */ 31630Sstevel@tonic-gate opt = sogetoff(mp, 31640Sstevel@tonic-gate tpr->optdata_ind.OPT_offset, 31650Sstevel@tonic-gate optlen, __TPI_ALIGN_SIZE); 31660Sstevel@tonic-gate 31670Sstevel@tonic-gate if (opt == NULL) { 31680Sstevel@tonic-gate freemsg(mp); 31690Sstevel@tonic-gate error = EPROTO; 31700Sstevel@tonic-gate eprintsoline(so, error); 31710Sstevel@tonic-gate goto err; 31720Sstevel@tonic-gate } 31730Sstevel@tonic-gate 31740Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen, 31750Sstevel@tonic-gate !(flags & MSG_XPG4_2)); 31760Sstevel@tonic-gate if (controllen != 0) 31770Sstevel@tonic-gate controllen = ncontrollen; 31780Sstevel@tonic-gate else if (ncontrollen != 0) 31790Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC; 31800Sstevel@tonic-gate } else { 31810Sstevel@tonic-gate controllen = 0; 31820Sstevel@tonic-gate } 31830Sstevel@tonic-gate 31840Sstevel@tonic-gate if (controllen != 0) { 31850Sstevel@tonic-gate /* 31860Sstevel@tonic-gate * Return control msg to caller. 31870Sstevel@tonic-gate * Caller handles truncation if length 31880Sstevel@tonic-gate * exceeds msg_controllen. 31890Sstevel@tonic-gate */ 31900Sstevel@tonic-gate control = kmem_alloc(controllen, KM_SLEEP); 31910Sstevel@tonic-gate 31920Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen, 31930Sstevel@tonic-gate !(flags & MSG_XPG4_2), 31940Sstevel@tonic-gate control, controllen); 31950Sstevel@tonic-gate if (error) { 31960Sstevel@tonic-gate freemsg(mp); 31970Sstevel@tonic-gate kmem_free(control, controllen); 31980Sstevel@tonic-gate eprintsoline(so, error); 31990Sstevel@tonic-gate goto err; 32000Sstevel@tonic-gate } 32010Sstevel@tonic-gate msg->msg_control = control; 32020Sstevel@tonic-gate msg->msg_controllen = controllen; 32030Sstevel@tonic-gate } 32040Sstevel@tonic-gate 32050Sstevel@tonic-gate /* 32060Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on 32070Sstevel@tonic-gate * DATA_flag and MOREDATA. 32080Sstevel@tonic-gate */ 32090Sstevel@tonic-gate mutex_enter(&so->so_lock); 32100Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 32110Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) { 32120Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) 32130Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 32140Sstevel@tonic-gate else 32150Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR; 32160Sstevel@tonic-gate } 32170Sstevel@tonic-gate freemsg(mp); 32180Sstevel@tonic-gate /* 32190Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 32200Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 32210Sstevel@tonic-gate * Not possible to wait if control info was received. 32220Sstevel@tonic-gate */ 32230Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 32240Sstevel@tonic-gate controllen == 0 && 32250Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 32260Sstevel@tonic-gate mutex_exit(&so->so_lock); 32270Sstevel@tonic-gate first = 0; 32280Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 32290Sstevel@tonic-gate goto retry; 32300Sstevel@tonic-gate } 32310Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 32320Sstevel@tonic-gate mutex_exit(&so->so_lock); 32330Sstevel@tonic-gate return (0); 32340Sstevel@tonic-gate } 32350Sstevel@tonic-gate case T_EXDATA_IND: { 32360Sstevel@tonic-gate dprintso(so, 1, 32370Sstevel@tonic-gate ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld " 32380Sstevel@tonic-gate "state %s\n", 32390Sstevel@tonic-gate so->so_oobsigcnt, so->so_oobcnt, 32400Sstevel@tonic-gate saved_resid - uiop->uio_resid, 32410Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 32420Sstevel@tonic-gate /* 32430Sstevel@tonic-gate * kstrgetmsg handles MSGMARK so there is nothing to 32440Sstevel@tonic-gate * inspect in the T_EXDATA_IND. 32450Sstevel@tonic-gate * strsock_proto makes the stream head queue the T_EXDATA_IND 32460Sstevel@tonic-gate * as a separate message with no M_DATA component. Furthermore, 32470Sstevel@tonic-gate * the stream head does not consolidate M_DATA messages onto 32480Sstevel@tonic-gate * an MSGMARK'ed message ensuring that the T_EXDATA_IND 32490Sstevel@tonic-gate * remains a message by itself. This is needed since MSGMARK 32500Sstevel@tonic-gate * marks both the whole message as well as the last byte 32510Sstevel@tonic-gate * of the message. 32520Sstevel@tonic-gate */ 32530Sstevel@tonic-gate freemsg(mp); 32540Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); /* No data */ 32550Sstevel@tonic-gate if (flags & MSG_PEEK) { 32560Sstevel@tonic-gate /* 32570Sstevel@tonic-gate * Even though we are peeking we consume the 32580Sstevel@tonic-gate * T_EXDATA_IND thereby moving the mark information 32590Sstevel@tonic-gate * to SS_RCVATMARK. Then the oob code below will 32600Sstevel@tonic-gate * retry the peeking kstrgetmsg. 32610Sstevel@tonic-gate * Note that the stream head read queue is 32620Sstevel@tonic-gate * never flushed without holding SOREADLOCKED 32630Sstevel@tonic-gate * thus the T_EXDATA_IND can not disappear 32640Sstevel@tonic-gate * underneath us. 32650Sstevel@tonic-gate */ 32660Sstevel@tonic-gate dprintso(so, 1, 32670Sstevel@tonic-gate ("sotpi_recvmsg: consume EXDATA_IND " 32680Sstevel@tonic-gate "counts %d/%d state %s\n", 32690Sstevel@tonic-gate so->so_oobsigcnt, 32700Sstevel@tonic-gate so->so_oobcnt, 32710Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 32720Sstevel@tonic-gate 32730Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR; 32740Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC) 32750Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL; 32760Sstevel@tonic-gate 32770Sstevel@tonic-gate pri = 0; 32780Sstevel@tonic-gate mp = NULL; 32790Sstevel@tonic-gate 32800Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop, 32810Sstevel@tonic-gate &pri, &pflag, (clock_t)-1, &rval); 32820Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); 32830Sstevel@tonic-gate 32840Sstevel@tonic-gate if (error) { 32850Sstevel@tonic-gate #ifdef SOCK_DEBUG 32860Sstevel@tonic-gate if (error != EWOULDBLOCK && error != EINTR) { 32870Sstevel@tonic-gate eprintsoline(so, error); 32880Sstevel@tonic-gate } 32890Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 32900Sstevel@tonic-gate mutex_enter(&so->so_lock); 32910Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 32920Sstevel@tonic-gate mutex_exit(&so->so_lock); 32930Sstevel@tonic-gate return (error); 32940Sstevel@tonic-gate } 32950Sstevel@tonic-gate ASSERT(mp); 32960Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr; 32970Sstevel@tonic-gate ASSERT(tpr->type == T_EXDATA_IND); 32980Sstevel@tonic-gate freemsg(mp); 32990Sstevel@tonic-gate } /* end "if (flags & MSG_PEEK)" */ 33000Sstevel@tonic-gate 33010Sstevel@tonic-gate /* 33020Sstevel@tonic-gate * Decrement the number of queued and pending oob. 33030Sstevel@tonic-gate * 33040Sstevel@tonic-gate * SS_RCVATMARK is cleared when we read past a mark. 33050Sstevel@tonic-gate * SS_HAVEOOBDATA is cleared when we've read past the 33060Sstevel@tonic-gate * last mark. 33070Sstevel@tonic-gate * SS_OOBPEND is cleared if we've read past the last 33080Sstevel@tonic-gate * mark and no (new) SIGURG has been posted. 33090Sstevel@tonic-gate */ 33100Sstevel@tonic-gate mutex_enter(&so->so_lock); 33110Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 33120Sstevel@tonic-gate ASSERT(so->so_oobsigcnt >= so->so_oobcnt); 33130Sstevel@tonic-gate ASSERT(so->so_oobsigcnt > 0); 33140Sstevel@tonic-gate so->so_oobsigcnt--; 33150Sstevel@tonic-gate ASSERT(so->so_oobcnt > 0); 33160Sstevel@tonic-gate so->so_oobcnt--; 33170Sstevel@tonic-gate /* 33180Sstevel@tonic-gate * Since the T_EXDATA_IND has been removed from the stream 33190Sstevel@tonic-gate * head, but we have not read data past the mark, 33200Sstevel@tonic-gate * sockfs needs to track that the socket is still at the mark. 33210Sstevel@tonic-gate * 33220Sstevel@tonic-gate * Since no data was received call kstrgetmsg again to wait 33230Sstevel@tonic-gate * for data. 33240Sstevel@tonic-gate */ 33250Sstevel@tonic-gate so->so_state |= SS_RCVATMARK; 33260Sstevel@tonic-gate mutex_exit(&so->so_lock); 33270Sstevel@tonic-gate dprintso(so, 1, 33280Sstevel@tonic-gate ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n", 33290Sstevel@tonic-gate so->so_oobsigcnt, so->so_oobcnt, 33300Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 33310Sstevel@tonic-gate pflag = opflag; 33320Sstevel@tonic-gate goto retry; 33330Sstevel@tonic-gate } 33340Sstevel@tonic-gate default: 33350Sstevel@tonic-gate ASSERT(0); 33360Sstevel@tonic-gate freemsg(mp); 33370Sstevel@tonic-gate error = EPROTO; 33380Sstevel@tonic-gate eprintsoline(so, error); 33390Sstevel@tonic-gate goto err; 33400Sstevel@tonic-gate } 33410Sstevel@tonic-gate /* NOTREACHED */ 33420Sstevel@tonic-gate err: 33430Sstevel@tonic-gate mutex_enter(&so->so_lock); 33440Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 33450Sstevel@tonic-gate mutex_exit(&so->so_lock); 33460Sstevel@tonic-gate return (error); 33470Sstevel@tonic-gate } 33480Sstevel@tonic-gate 33490Sstevel@tonic-gate /* 33500Sstevel@tonic-gate * Sending data with options on a datagram socket. 33510Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set. 33520Sstevel@tonic-gate */ 33530Sstevel@tonic-gate static int 3354*741Smasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3355*741Smasputra struct uio *uiop, void *control, t_uscalar_t controllen, int flags) 33560Sstevel@tonic-gate { 33570Sstevel@tonic-gate struct T_unitdata_req tudr; 33580Sstevel@tonic-gate mblk_t *mp; 33590Sstevel@tonic-gate int error; 33600Sstevel@tonic-gate void *addr; 33610Sstevel@tonic-gate socklen_t addrlen; 33620Sstevel@tonic-gate void *src; 33630Sstevel@tonic-gate socklen_t srclen; 33640Sstevel@tonic-gate ssize_t len; 33650Sstevel@tonic-gate int size; 33660Sstevel@tonic-gate struct T_opthdr toh; 33670Sstevel@tonic-gate struct fdbuf *fdbuf; 33680Sstevel@tonic-gate t_uscalar_t optlen; 33690Sstevel@tonic-gate void *fds; 33700Sstevel@tonic-gate int fdlen; 33710Sstevel@tonic-gate 33720Sstevel@tonic-gate ASSERT(name && namelen); 33730Sstevel@tonic-gate ASSERT(control && controllen); 33740Sstevel@tonic-gate 33750Sstevel@tonic-gate len = uiop->uio_resid; 33760Sstevel@tonic-gate if (len > (ssize_t)so->so_tidu_size) { 33770Sstevel@tonic-gate return (EMSGSIZE); 33780Sstevel@tonic-gate } 33790Sstevel@tonic-gate 33800Sstevel@tonic-gate /* 33810Sstevel@tonic-gate * For AF_UNIX the destination address is translated to an internal 33820Sstevel@tonic-gate * name and the source address is passed as an option. 33830Sstevel@tonic-gate * Also, file descriptors are passed as file pointers in an 33840Sstevel@tonic-gate * option. 33850Sstevel@tonic-gate */ 33860Sstevel@tonic-gate 33870Sstevel@tonic-gate /* 33880Sstevel@tonic-gate * Length and family checks. 33890Sstevel@tonic-gate */ 33900Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 33910Sstevel@tonic-gate if (error) { 33920Sstevel@tonic-gate eprintsoline(so, error); 33930Sstevel@tonic-gate return (error); 33940Sstevel@tonic-gate } 33950Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 33960Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 33970Sstevel@tonic-gate /* 33980Sstevel@tonic-gate * Already have a transport internal address. Do not 33990Sstevel@tonic-gate * pass any (transport internal) source address. 34000Sstevel@tonic-gate */ 34010Sstevel@tonic-gate addr = name; 34020Sstevel@tonic-gate addrlen = namelen; 34030Sstevel@tonic-gate src = NULL; 34040Sstevel@tonic-gate srclen = 0; 34050Sstevel@tonic-gate } else { 34060Sstevel@tonic-gate /* 34070Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 34080Sstevel@tonic-gate * and translate the remote address. 34090Sstevel@tonic-gate * 34100Sstevel@tonic-gate * Note that this code does not prevent so_laddr_sa 34110Sstevel@tonic-gate * from changing while it is being used. Thus 34120Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this 34130Sstevel@tonic-gate * send the peer might see a partially new and a 34140Sstevel@tonic-gate * partially old "from" address. 34150Sstevel@tonic-gate */ 34160Sstevel@tonic-gate src = so->so_laddr_sa; 34170Sstevel@tonic-gate srclen = (t_uscalar_t)so->so_laddr_len; 34180Sstevel@tonic-gate dprintso(so, 1, 34190Sstevel@tonic-gate ("sosend_dgramcmsg UNIX: srclen %d, src %p\n", 34200Sstevel@tonic-gate srclen, src)); 34210Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen, 34220Sstevel@tonic-gate (flags & MSG_XPG4_2), 34230Sstevel@tonic-gate &addr, &addrlen); 34240Sstevel@tonic-gate if (error) { 34250Sstevel@tonic-gate eprintsoline(so, error); 34260Sstevel@tonic-gate return (error); 34270Sstevel@tonic-gate } 34280Sstevel@tonic-gate } 34290Sstevel@tonic-gate } else { 34300Sstevel@tonic-gate addr = name; 34310Sstevel@tonic-gate addrlen = namelen; 34320Sstevel@tonic-gate src = NULL; 34330Sstevel@tonic-gate srclen = 0; 34340Sstevel@tonic-gate } 34350Sstevel@tonic-gate optlen = so_optlen(control, controllen, 34360Sstevel@tonic-gate !(flags & MSG_XPG4_2)); 34370Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 34380Sstevel@tonic-gate tudr.DEST_length = addrlen; 34390Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 34400Sstevel@tonic-gate if (srclen != 0) 34410Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) + 34420Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen)); 34430Sstevel@tonic-gate else 34440Sstevel@tonic-gate tudr.OPT_length = optlen; 34450Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 34460Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen)); 34470Sstevel@tonic-gate 34480Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 34490Sstevel@tonic-gate 34500Sstevel@tonic-gate /* 34510Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set. 34520Sstevel@tonic-gate */ 34530Sstevel@tonic-gate error = so_getfdopt(control, controllen, 34540Sstevel@tonic-gate !(flags & MSG_XPG4_2), &fds, &fdlen); 34550Sstevel@tonic-gate if (error) 34560Sstevel@tonic-gate return (error); 34570Sstevel@tonic-gate if (fdlen != -1) { 34580Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING)) 34590Sstevel@tonic-gate return (EOPNOTSUPP); 34600Sstevel@tonic-gate 34610Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf); 34620Sstevel@tonic-gate if (error) 34630Sstevel@tonic-gate return (error); 34640Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf); 34650Sstevel@tonic-gate } else { 34660Sstevel@tonic-gate mp = soallocproto(size, _ALLOC_INTR); 3467455Smeem if (mp == NULL) { 3468455Smeem /* 3469455Smeem * Caught a signal waiting for memory. 3470455Smeem * Let send* return EINTR. 3471455Smeem */ 3472455Smeem return (EINTR); 3473455Smeem } 34740Sstevel@tonic-gate } 34750Sstevel@tonic-gate soappendmsg(mp, &tudr, sizeof (tudr)); 34760Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 34770Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 34780Sstevel@tonic-gate 34790Sstevel@tonic-gate if (fdlen != -1) { 34800Sstevel@tonic-gate ASSERT(fdbuf != NULL); 34810Sstevel@tonic-gate toh.level = SOL_SOCKET; 34820Sstevel@tonic-gate toh.name = SO_FILEP; 34830Sstevel@tonic-gate toh.len = fdbuf->fd_size + 34840Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_opthdr); 34850Sstevel@tonic-gate toh.status = 0; 34860Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 34870Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size); 34880Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 34890Sstevel@tonic-gate } 34900Sstevel@tonic-gate if (srclen != 0) { 34910Sstevel@tonic-gate /* 34920Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 34930Sstevel@tonic-gate * address option. 34940Sstevel@tonic-gate */ 34950Sstevel@tonic-gate toh.level = SOL_SOCKET; 34960Sstevel@tonic-gate toh.name = SO_SRCADDR; 34970Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 34980Sstevel@tonic-gate toh.status = 0; 34990Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 35000Sstevel@tonic-gate soappendmsg(mp, src, srclen); 35010Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 35020Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 35030Sstevel@tonic-gate } 35040Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 35050Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 35060Sstevel@tonic-gate /* At most 3 bytes left in the message */ 35070Sstevel@tonic-gate ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE)); 35080Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size); 35090Sstevel@tonic-gate 35100Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 35110Sstevel@tonic-gate #ifdef C2_AUDIT 35120Sstevel@tonic-gate if (audit_active) 35130Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 35140Sstevel@tonic-gate #endif /* C2_AUDIT */ 35150Sstevel@tonic-gate 35160Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 35170Sstevel@tonic-gate #ifdef SOCK_DEBUG 35180Sstevel@tonic-gate if (error) { 35190Sstevel@tonic-gate eprintsoline(so, error); 35200Sstevel@tonic-gate } 35210Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 35220Sstevel@tonic-gate return (error); 35230Sstevel@tonic-gate } 35240Sstevel@tonic-gate 35250Sstevel@tonic-gate /* 35260Sstevel@tonic-gate * Sending data with options on a connected stream socket. 35270Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set. 35280Sstevel@tonic-gate */ 35290Sstevel@tonic-gate static int 35300Sstevel@tonic-gate sosend_svccmsg(struct sonode *so, 35310Sstevel@tonic-gate struct uio *uiop, 35320Sstevel@tonic-gate int more, 35330Sstevel@tonic-gate void *control, 35340Sstevel@tonic-gate t_uscalar_t controllen, 35350Sstevel@tonic-gate int flags) 35360Sstevel@tonic-gate { 35370Sstevel@tonic-gate struct T_optdata_req tdr; 35380Sstevel@tonic-gate mblk_t *mp; 35390Sstevel@tonic-gate int error; 35400Sstevel@tonic-gate ssize_t iosize; 35410Sstevel@tonic-gate int first = 1; 35420Sstevel@tonic-gate int size; 35430Sstevel@tonic-gate struct fdbuf *fdbuf; 35440Sstevel@tonic-gate t_uscalar_t optlen; 35450Sstevel@tonic-gate void *fds; 35460Sstevel@tonic-gate int fdlen; 35470Sstevel@tonic-gate struct T_opthdr toh; 35480Sstevel@tonic-gate 35490Sstevel@tonic-gate dprintso(so, 1, 35500Sstevel@tonic-gate ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid)); 35510Sstevel@tonic-gate 35520Sstevel@tonic-gate /* 35530Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are 35540Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it 35550Sstevel@tonic-gate * thus it is not possible to ASSERT on the state. 35560Sstevel@tonic-gate */ 35570Sstevel@tonic-gate 35580Sstevel@tonic-gate /* Options on connection-oriented only when SM_OPTDATA set. */ 35590Sstevel@tonic-gate if (!(so->so_mode & SM_OPTDATA)) 35600Sstevel@tonic-gate return (EOPNOTSUPP); 35610Sstevel@tonic-gate 35620Sstevel@tonic-gate do { 35630Sstevel@tonic-gate /* 35640Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this 35650Sstevel@tonic-gate * message or if the caller passed in "more". 35660Sstevel@tonic-gate * Error for transports with zero tidu_size. 35670Sstevel@tonic-gate */ 35680Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ; 35690Sstevel@tonic-gate iosize = so->so_tidu_size; 35700Sstevel@tonic-gate if (iosize <= 0) 35710Sstevel@tonic-gate return (EMSGSIZE); 35720Sstevel@tonic-gate if (uiop->uio_resid > iosize) { 35730Sstevel@tonic-gate tdr.DATA_flag = 1; 35740Sstevel@tonic-gate } else { 35750Sstevel@tonic-gate if (more) 35760Sstevel@tonic-gate tdr.DATA_flag = 1; 35770Sstevel@tonic-gate else 35780Sstevel@tonic-gate tdr.DATA_flag = 0; 35790Sstevel@tonic-gate iosize = uiop->uio_resid; 35800Sstevel@tonic-gate } 35810Sstevel@tonic-gate dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n", 35820Sstevel@tonic-gate tdr.DATA_flag, iosize)); 35830Sstevel@tonic-gate 35840Sstevel@tonic-gate optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2)); 35850Sstevel@tonic-gate tdr.OPT_length = optlen; 35860Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 35870Sstevel@tonic-gate 35880Sstevel@tonic-gate size = (int)sizeof (tdr) + optlen; 35890Sstevel@tonic-gate /* 35900Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set. 35910Sstevel@tonic-gate */ 35920Sstevel@tonic-gate error = so_getfdopt(control, controllen, 35930Sstevel@tonic-gate !(flags & MSG_XPG4_2), &fds, &fdlen); 35940Sstevel@tonic-gate if (error) 35950Sstevel@tonic-gate return (error); 35960Sstevel@tonic-gate if (fdlen != -1) { 35970Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING)) 35980Sstevel@tonic-gate return (EOPNOTSUPP); 35990Sstevel@tonic-gate 36000Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf); 36010Sstevel@tonic-gate if (error) 36020Sstevel@tonic-gate return (error); 36030Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf); 36040Sstevel@tonic-gate } else { 36050Sstevel@tonic-gate mp = soallocproto(size, _ALLOC_INTR); 3606455Smeem if (mp == NULL) { 3607455Smeem /* 3608455Smeem * Caught a signal waiting for memory. 3609455Smeem * Let send* return EINTR. 3610455Smeem */ 3611455Smeem return (first ? EINTR : 0); 3612455Smeem } 36130Sstevel@tonic-gate } 36140Sstevel@tonic-gate soappendmsg(mp, &tdr, sizeof (tdr)); 36150Sstevel@tonic-gate 36160Sstevel@tonic-gate if (fdlen != -1) { 36170Sstevel@tonic-gate ASSERT(fdbuf != NULL); 36180Sstevel@tonic-gate toh.level = SOL_SOCKET; 36190Sstevel@tonic-gate toh.name = SO_FILEP; 36200Sstevel@tonic-gate toh.len = fdbuf->fd_size + 36210Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_opthdr); 36220Sstevel@tonic-gate toh.status = 0; 36230Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 36240Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size); 36250Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 36260Sstevel@tonic-gate } 36270Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 36280Sstevel@tonic-gate /* At most 3 bytes left in the message */ 36290Sstevel@tonic-gate ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE)); 36300Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size); 36310Sstevel@tonic-gate 36320Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 36330Sstevel@tonic-gate 36340Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 36350Sstevel@tonic-gate 0, MSG_BAND, 0); 36360Sstevel@tonic-gate if (error) { 36370Sstevel@tonic-gate if (!first && error == EWOULDBLOCK) 36380Sstevel@tonic-gate return (0); 36390Sstevel@tonic-gate eprintsoline(so, error); 36400Sstevel@tonic-gate return (error); 36410Sstevel@tonic-gate } 36420Sstevel@tonic-gate control = NULL; 36430Sstevel@tonic-gate first = 0; 36440Sstevel@tonic-gate if (uiop->uio_resid > 0) { 36450Sstevel@tonic-gate /* 36460Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though 36470Sstevel@tonic-gate * some data have been written. This is consistent 36480Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics. 36490Sstevel@tonic-gate */ 36500Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) { 36510Sstevel@tonic-gate tsignal(curthread, SIGPIPE); 36520Sstevel@tonic-gate eprintsoline(so, error); 36530Sstevel@tonic-gate return (EPIPE); 36540Sstevel@tonic-gate } 36550Sstevel@tonic-gate if (so->so_error != 0) { 36560Sstevel@tonic-gate mutex_enter(&so->so_lock); 36570Sstevel@tonic-gate error = sogeterr(so); 36580Sstevel@tonic-gate mutex_exit(&so->so_lock); 36590Sstevel@tonic-gate if (error != 0) { 36600Sstevel@tonic-gate eprintsoline(so, error); 36610Sstevel@tonic-gate return (error); 36620Sstevel@tonic-gate } 36630Sstevel@tonic-gate } 36640Sstevel@tonic-gate } 36650Sstevel@tonic-gate } while (uiop->uio_resid > 0); 36660Sstevel@tonic-gate return (0); 36670Sstevel@tonic-gate } 36680Sstevel@tonic-gate 36690Sstevel@tonic-gate /* 36700Sstevel@tonic-gate * Sending data on a datagram socket. 36710Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set. 36720Sstevel@tonic-gate * 36730Sstevel@tonic-gate * For AF_UNIX the destination address is translated to an internal 36740Sstevel@tonic-gate * name and the source address is passed as an option. 36750Sstevel@tonic-gate */ 36760Sstevel@tonic-gate int 3677*741Smasputra sosend_dgram(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3678*741Smasputra struct uio *uiop, int flags) 36790Sstevel@tonic-gate { 36800Sstevel@tonic-gate struct T_unitdata_req tudr; 36810Sstevel@tonic-gate mblk_t *mp; 36820Sstevel@tonic-gate int error; 36830Sstevel@tonic-gate void *addr; 36840Sstevel@tonic-gate socklen_t addrlen; 36850Sstevel@tonic-gate void *src; 36860Sstevel@tonic-gate socklen_t srclen; 36870Sstevel@tonic-gate ssize_t len; 36880Sstevel@tonic-gate 3689*741Smasputra ASSERT(name != NULL && namelen != 0); 36900Sstevel@tonic-gate 36910Sstevel@tonic-gate len = uiop->uio_resid; 36920Sstevel@tonic-gate if (len > so->so_tidu_size) { 36930Sstevel@tonic-gate error = EMSGSIZE; 36940Sstevel@tonic-gate goto done; 36950Sstevel@tonic-gate } 36960Sstevel@tonic-gate 3697*741Smasputra /* Length and family checks */ 36980Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 3699*741Smasputra if (error != 0) 37000Sstevel@tonic-gate goto done; 3701*741Smasputra 3702*741Smasputra if (so->so_state & SS_DIRECT) 3703*741Smasputra return (sodgram_direct(so, name, namelen, uiop, flags)); 3704*741Smasputra 37050Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 37060Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 37070Sstevel@tonic-gate /* 37080Sstevel@tonic-gate * Already have a transport internal address. Do not 37090Sstevel@tonic-gate * pass any (transport internal) source address. 37100Sstevel@tonic-gate */ 37110Sstevel@tonic-gate addr = name; 37120Sstevel@tonic-gate addrlen = namelen; 37130Sstevel@tonic-gate src = NULL; 37140Sstevel@tonic-gate srclen = 0; 37150Sstevel@tonic-gate } else { 37160Sstevel@tonic-gate /* 37170Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 37180Sstevel@tonic-gate * and translate the remote address. 37190Sstevel@tonic-gate * 37200Sstevel@tonic-gate * Note that this code does not prevent so_laddr_sa 37210Sstevel@tonic-gate * from changing while it is being used. Thus 37220Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this 37230Sstevel@tonic-gate * send the peer might see a partially new and a 37240Sstevel@tonic-gate * partially old "from" address. 37250Sstevel@tonic-gate */ 37260Sstevel@tonic-gate src = so->so_laddr_sa; 37270Sstevel@tonic-gate srclen = (socklen_t)so->so_laddr_len; 37280Sstevel@tonic-gate dprintso(so, 1, 37290Sstevel@tonic-gate ("sosend_dgram UNIX: srclen %d, src %p\n", 37300Sstevel@tonic-gate srclen, src)); 37310Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen, 37320Sstevel@tonic-gate (flags & MSG_XPG4_2), 37330Sstevel@tonic-gate &addr, &addrlen); 37340Sstevel@tonic-gate if (error) { 37350Sstevel@tonic-gate eprintsoline(so, error); 37360Sstevel@tonic-gate goto done; 37370Sstevel@tonic-gate } 37380Sstevel@tonic-gate } 37390Sstevel@tonic-gate } else { 37400Sstevel@tonic-gate addr = name; 37410Sstevel@tonic-gate addrlen = namelen; 37420Sstevel@tonic-gate src = NULL; 37430Sstevel@tonic-gate srclen = 0; 37440Sstevel@tonic-gate } 37450Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 37460Sstevel@tonic-gate tudr.DEST_length = addrlen; 37470Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 37480Sstevel@tonic-gate if (srclen == 0) { 37490Sstevel@tonic-gate tudr.OPT_length = 0; 37500Sstevel@tonic-gate tudr.OPT_offset = 0; 37510Sstevel@tonic-gate 37520Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 37530Sstevel@tonic-gate addr, addrlen, 0, _ALLOC_INTR); 37540Sstevel@tonic-gate if (mp == NULL) { 37550Sstevel@tonic-gate /* 37560Sstevel@tonic-gate * Caught a signal waiting for memory. 37570Sstevel@tonic-gate * Let send* return EINTR. 37580Sstevel@tonic-gate */ 37590Sstevel@tonic-gate error = EINTR; 37600Sstevel@tonic-gate goto done; 37610Sstevel@tonic-gate } 37620Sstevel@tonic-gate } else { 37630Sstevel@tonic-gate /* 37640Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 37650Sstevel@tonic-gate * address option. 37660Sstevel@tonic-gate */ 37670Sstevel@tonic-gate struct T_opthdr toh; 37680Sstevel@tonic-gate ssize_t size; 37690Sstevel@tonic-gate 37700Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(sizeof (toh) + 37710Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen)); 37720Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 37730Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen)); 37740Sstevel@tonic-gate 37750Sstevel@tonic-gate toh.level = SOL_SOCKET; 37760Sstevel@tonic-gate toh.name = SO_SRCADDR; 37770Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 37780Sstevel@tonic-gate toh.status = 0; 37790Sstevel@tonic-gate 37800Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 37810Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 37820Sstevel@tonic-gate addr, addrlen, size, _ALLOC_INTR); 37830Sstevel@tonic-gate if (mp == NULL) { 37840Sstevel@tonic-gate /* 37850Sstevel@tonic-gate * Caught a signal waiting for memory. 37860Sstevel@tonic-gate * Let send* return EINTR. 37870Sstevel@tonic-gate */ 37880Sstevel@tonic-gate error = EINTR; 37890Sstevel@tonic-gate goto done; 37900Sstevel@tonic-gate } 37910Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 37920Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 37930Sstevel@tonic-gate soappendmsg(mp, src, srclen); 37940Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 37950Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 37960Sstevel@tonic-gate } 37970Sstevel@tonic-gate 37980Sstevel@tonic-gate #ifdef C2_AUDIT 37990Sstevel@tonic-gate if (audit_active) 38000Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 38010Sstevel@tonic-gate #endif /* C2_AUDIT */ 38020Sstevel@tonic-gate 38030Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 38040Sstevel@tonic-gate done: 38050Sstevel@tonic-gate #ifdef SOCK_DEBUG 38060Sstevel@tonic-gate if (error) { 38070Sstevel@tonic-gate eprintsoline(so, error); 38080Sstevel@tonic-gate } 38090Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 38100Sstevel@tonic-gate return (error); 38110Sstevel@tonic-gate } 38120Sstevel@tonic-gate 38130Sstevel@tonic-gate /* 38140Sstevel@tonic-gate * Sending data on a connected stream socket. 38150Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set. 38160Sstevel@tonic-gate */ 38170Sstevel@tonic-gate int 38180Sstevel@tonic-gate sosend_svc(struct sonode *so, 38190Sstevel@tonic-gate struct uio *uiop, 38200Sstevel@tonic-gate t_scalar_t prim, 38210Sstevel@tonic-gate int more, 38220Sstevel@tonic-gate int sflag) 38230Sstevel@tonic-gate { 38240Sstevel@tonic-gate struct T_data_req tdr; 38250Sstevel@tonic-gate mblk_t *mp; 38260Sstevel@tonic-gate int error; 38270Sstevel@tonic-gate ssize_t iosize; 38280Sstevel@tonic-gate int first = 1; 38290Sstevel@tonic-gate 38300Sstevel@tonic-gate dprintso(so, 1, 38310Sstevel@tonic-gate ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n", 38320Sstevel@tonic-gate so, uiop->uio_resid, prim, sflag)); 38330Sstevel@tonic-gate 38340Sstevel@tonic-gate /* 38350Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are 38360Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it 38370Sstevel@tonic-gate * thus it is not possible to ASSERT on the state. 38380Sstevel@tonic-gate */ 38390Sstevel@tonic-gate 38400Sstevel@tonic-gate do { 38410Sstevel@tonic-gate /* 38420Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this 38430Sstevel@tonic-gate * message or if the caller passed in "more". 38440Sstevel@tonic-gate * Error for transports with zero tidu_size. 38450Sstevel@tonic-gate */ 38460Sstevel@tonic-gate tdr.PRIM_type = prim; 38470Sstevel@tonic-gate iosize = so->so_tidu_size; 38480Sstevel@tonic-gate if (iosize <= 0) 38490Sstevel@tonic-gate return (EMSGSIZE); 38500Sstevel@tonic-gate if (uiop->uio_resid > iosize) { 38510Sstevel@tonic-gate tdr.MORE_flag = 1; 38520Sstevel@tonic-gate } else { 38530Sstevel@tonic-gate if (more) 38540Sstevel@tonic-gate tdr.MORE_flag = 1; 38550Sstevel@tonic-gate else 38560Sstevel@tonic-gate tdr.MORE_flag = 0; 38570Sstevel@tonic-gate iosize = uiop->uio_resid; 38580Sstevel@tonic-gate } 38590Sstevel@tonic-gate dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n", 38600Sstevel@tonic-gate prim, tdr.MORE_flag, iosize)); 38610Sstevel@tonic-gate mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR); 38620Sstevel@tonic-gate if (mp == NULL) { 38630Sstevel@tonic-gate /* 38640Sstevel@tonic-gate * Caught a signal waiting for memory. 38650Sstevel@tonic-gate * Let send* return EINTR. 38660Sstevel@tonic-gate */ 38670Sstevel@tonic-gate if (first) 38680Sstevel@tonic-gate return (EINTR); 38690Sstevel@tonic-gate else 38700Sstevel@tonic-gate return (0); 38710Sstevel@tonic-gate } 38720Sstevel@tonic-gate 38730Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 38740Sstevel@tonic-gate 0, sflag | MSG_BAND, 0); 38750Sstevel@tonic-gate if (error) { 38760Sstevel@tonic-gate if (!first && error == EWOULDBLOCK) 38770Sstevel@tonic-gate return (0); 38780Sstevel@tonic-gate eprintsoline(so, error); 38790Sstevel@tonic-gate return (error); 38800Sstevel@tonic-gate } 38810Sstevel@tonic-gate first = 0; 38820Sstevel@tonic-gate if (uiop->uio_resid > 0) { 38830Sstevel@tonic-gate /* 38840Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though 38850Sstevel@tonic-gate * some data have been written. This is consistent 38860Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics. 38870Sstevel@tonic-gate */ 38880Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) { 38890Sstevel@tonic-gate tsignal(curthread, SIGPIPE); 38900Sstevel@tonic-gate eprintsoline(so, error); 38910Sstevel@tonic-gate return (EPIPE); 38920Sstevel@tonic-gate } 38930Sstevel@tonic-gate if (so->so_error != 0) { 38940Sstevel@tonic-gate mutex_enter(&so->so_lock); 38950Sstevel@tonic-gate error = sogeterr(so); 38960Sstevel@tonic-gate mutex_exit(&so->so_lock); 38970Sstevel@tonic-gate if (error != 0) { 38980Sstevel@tonic-gate eprintsoline(so, error); 38990Sstevel@tonic-gate return (error); 39000Sstevel@tonic-gate } 39010Sstevel@tonic-gate } 39020Sstevel@tonic-gate } 39030Sstevel@tonic-gate } while (uiop->uio_resid > 0); 39040Sstevel@tonic-gate return (0); 39050Sstevel@tonic-gate } 39060Sstevel@tonic-gate 39070Sstevel@tonic-gate /* 39080Sstevel@tonic-gate * Check the state for errors and call the appropriate send function. 39090Sstevel@tonic-gate * 39100Sstevel@tonic-gate * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set) 39110Sstevel@tonic-gate * this function issues a setsockopt to toggle SO_DONTROUTE before and 39120Sstevel@tonic-gate * after sending the message. 39130Sstevel@tonic-gate */ 39140Sstevel@tonic-gate static int 39150Sstevel@tonic-gate sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop) 39160Sstevel@tonic-gate { 39170Sstevel@tonic-gate int so_state; 39180Sstevel@tonic-gate int so_mode; 39190Sstevel@tonic-gate int error; 39200Sstevel@tonic-gate struct sockaddr *name; 39210Sstevel@tonic-gate t_uscalar_t namelen; 39220Sstevel@tonic-gate int dontroute; 39230Sstevel@tonic-gate int flags; 39240Sstevel@tonic-gate 39250Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n", 39260Sstevel@tonic-gate so, msg, msg->msg_flags, 39270Sstevel@tonic-gate pr_state(so->so_state, so->so_mode), so->so_error)); 39280Sstevel@tonic-gate 39290Sstevel@tonic-gate mutex_enter(&so->so_lock); 39300Sstevel@tonic-gate so_state = so->so_state; 39310Sstevel@tonic-gate 39320Sstevel@tonic-gate if (so_state & SS_CANTSENDMORE) { 39330Sstevel@tonic-gate mutex_exit(&so->so_lock); 39340Sstevel@tonic-gate tsignal(curthread, SIGPIPE); 39350Sstevel@tonic-gate return (EPIPE); 39360Sstevel@tonic-gate } 39370Sstevel@tonic-gate 39380Sstevel@tonic-gate if (so->so_error != 0) { 39390Sstevel@tonic-gate error = sogeterr(so); 39400Sstevel@tonic-gate if (error != 0) { 39410Sstevel@tonic-gate mutex_exit(&so->so_lock); 39420Sstevel@tonic-gate return (error); 39430Sstevel@tonic-gate } 39440Sstevel@tonic-gate } 39450Sstevel@tonic-gate 39460Sstevel@tonic-gate name = (struct sockaddr *)msg->msg_name; 39470Sstevel@tonic-gate namelen = msg->msg_namelen; 39480Sstevel@tonic-gate 39490Sstevel@tonic-gate so_mode = so->so_mode; 39500Sstevel@tonic-gate 39510Sstevel@tonic-gate if (name == NULL) { 39520Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED)) { 39530Sstevel@tonic-gate mutex_exit(&so->so_lock); 39540Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) 39550Sstevel@tonic-gate return (ENOTCONN); 39560Sstevel@tonic-gate else 39570Sstevel@tonic-gate return (EDESTADDRREQ); 39580Sstevel@tonic-gate } 39590Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) { 39600Sstevel@tonic-gate name = NULL; 39610Sstevel@tonic-gate namelen = 0; 39620Sstevel@tonic-gate } else { 39630Sstevel@tonic-gate /* 39640Sstevel@tonic-gate * Note that this code does not prevent so_faddr_sa 39650Sstevel@tonic-gate * from changing while it is being used. Thus 39660Sstevel@tonic-gate * if an "unconnect"+connect occurs concurrently with 39670Sstevel@tonic-gate * this send the datagram might be delivered to a 39680Sstevel@tonic-gate * garbaled address. 39690Sstevel@tonic-gate */ 39700Sstevel@tonic-gate ASSERT(so->so_faddr_sa); 39710Sstevel@tonic-gate name = so->so_faddr_sa; 39720Sstevel@tonic-gate namelen = (t_uscalar_t)so->so_faddr_len; 39730Sstevel@tonic-gate } 39740Sstevel@tonic-gate } else { 39750Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED) && 39760Sstevel@tonic-gate (so_mode & SM_CONNREQUIRED)) { 39770Sstevel@tonic-gate /* Required but not connected */ 39780Sstevel@tonic-gate mutex_exit(&so->so_lock); 39790Sstevel@tonic-gate return (ENOTCONN); 39800Sstevel@tonic-gate } 39810Sstevel@tonic-gate /* 39820Sstevel@tonic-gate * Ignore the address on connection-oriented sockets. 39830Sstevel@tonic-gate * Just like BSD this code does not generate an error for 39840Sstevel@tonic-gate * TCP (a CONNREQUIRED socket) when sending to an address 39850Sstevel@tonic-gate * passed in with sendto/sendmsg. Instead the data is 39860Sstevel@tonic-gate * delivered on the connection as if no address had been 39870Sstevel@tonic-gate * supplied. 39880Sstevel@tonic-gate */ 39890Sstevel@tonic-gate if ((so_state & SS_ISCONNECTED) && 39900Sstevel@tonic-gate !(so_mode & SM_CONNREQUIRED)) { 39910Sstevel@tonic-gate mutex_exit(&so->so_lock); 39920Sstevel@tonic-gate return (EISCONN); 39930Sstevel@tonic-gate } 39940Sstevel@tonic-gate if (!(so_state & SS_ISBOUND)) { 39950Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 39960Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0, 39970Sstevel@tonic-gate _SOBIND_UNSPEC|_SOBIND_LOCK_HELD); 39980Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 39990Sstevel@tonic-gate if (error) { 40000Sstevel@tonic-gate mutex_exit(&so->so_lock); 40010Sstevel@tonic-gate eprintsoline(so, error); 40020Sstevel@tonic-gate return (error); 40030Sstevel@tonic-gate } 40040Sstevel@tonic-gate } 40050Sstevel@tonic-gate /* 40060Sstevel@tonic-gate * Handle delayed datagram errors. These are only queued 40070Sstevel@tonic-gate * when the application sets SO_DGRAM_ERRIND. 40080Sstevel@tonic-gate * Return the error if we are sending to the address 40090Sstevel@tonic-gate * that was returned in the last T_UDERROR_IND. 40100Sstevel@tonic-gate * If sending to some other address discard the delayed 40110Sstevel@tonic-gate * error indication. 40120Sstevel@tonic-gate */ 40130Sstevel@tonic-gate if (so->so_delayed_error) { 40140Sstevel@tonic-gate struct T_uderror_ind *tudi; 40150Sstevel@tonic-gate void *addr; 40160Sstevel@tonic-gate t_uscalar_t addrlen; 40170Sstevel@tonic-gate boolean_t match = B_FALSE; 40180Sstevel@tonic-gate 40190Sstevel@tonic-gate ASSERT(so->so_eaddr_mp); 40200Sstevel@tonic-gate error = so->so_delayed_error; 40210Sstevel@tonic-gate so->so_delayed_error = 0; 40220Sstevel@tonic-gate tudi = (struct T_uderror_ind *)so->so_eaddr_mp->b_rptr; 40230Sstevel@tonic-gate addrlen = tudi->DEST_length; 40240Sstevel@tonic-gate addr = sogetoff(so->so_eaddr_mp, 40250Sstevel@tonic-gate tudi->DEST_offset, 40260Sstevel@tonic-gate addrlen, 1); 40270Sstevel@tonic-gate ASSERT(addr); /* Checked by strsock_proto */ 40280Sstevel@tonic-gate switch (so->so_family) { 40290Sstevel@tonic-gate case AF_INET: { 40300Sstevel@tonic-gate /* Compare just IP address and port */ 40310Sstevel@tonic-gate sin_t *sin1 = (sin_t *)name; 40320Sstevel@tonic-gate sin_t *sin2 = (sin_t *)addr; 40330Sstevel@tonic-gate 40340Sstevel@tonic-gate if (addrlen == sizeof (sin_t) && 40350Sstevel@tonic-gate namelen == addrlen && 40360Sstevel@tonic-gate sin1->sin_port == sin2->sin_port && 40370Sstevel@tonic-gate sin1->sin_addr.s_addr == 40380Sstevel@tonic-gate sin2->sin_addr.s_addr) 40390Sstevel@tonic-gate match = B_TRUE; 40400Sstevel@tonic-gate break; 40410Sstevel@tonic-gate } 40420Sstevel@tonic-gate case AF_INET6: { 40430Sstevel@tonic-gate /* Compare just IP address and port. Not flow */ 40440Sstevel@tonic-gate sin6_t *sin1 = (sin6_t *)name; 40450Sstevel@tonic-gate sin6_t *sin2 = (sin6_t *)addr; 40460Sstevel@tonic-gate 40470Sstevel@tonic-gate if (addrlen == sizeof (sin6_t) && 40480Sstevel@tonic-gate namelen == addrlen && 40490Sstevel@tonic-gate sin1->sin6_port == sin2->sin6_port && 40500Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr, 40510Sstevel@tonic-gate &sin2->sin6_addr)) 40520Sstevel@tonic-gate match = B_TRUE; 40530Sstevel@tonic-gate break; 40540Sstevel@tonic-gate } 40550Sstevel@tonic-gate case AF_UNIX: 40560Sstevel@tonic-gate default: 40570Sstevel@tonic-gate if (namelen == addrlen && 40580Sstevel@tonic-gate bcmp(name, addr, namelen) == 0) 40590Sstevel@tonic-gate match = B_TRUE; 40600Sstevel@tonic-gate } 40610Sstevel@tonic-gate if (match) { 40620Sstevel@tonic-gate freemsg(so->so_eaddr_mp); 40630Sstevel@tonic-gate so->so_eaddr_mp = NULL; 40640Sstevel@tonic-gate mutex_exit(&so->so_lock); 40650Sstevel@tonic-gate #ifdef DEBUG 40660Sstevel@tonic-gate dprintso(so, 0, 40670Sstevel@tonic-gate ("sockfs delayed error %d for %s\n", 40680Sstevel@tonic-gate error, 40690Sstevel@tonic-gate pr_addr(so->so_family, name, namelen))); 40700Sstevel@tonic-gate #endif /* DEBUG */ 40710Sstevel@tonic-gate return (error); 40720Sstevel@tonic-gate } 40730Sstevel@tonic-gate freemsg(so->so_eaddr_mp); 40740Sstevel@tonic-gate so->so_eaddr_mp = NULL; 40750Sstevel@tonic-gate } 40760Sstevel@tonic-gate } 40770Sstevel@tonic-gate mutex_exit(&so->so_lock); 40780Sstevel@tonic-gate 40790Sstevel@tonic-gate flags = msg->msg_flags; 40800Sstevel@tonic-gate dontroute = 0; 40810Sstevel@tonic-gate if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) { 40820Sstevel@tonic-gate uint32_t val; 40830Sstevel@tonic-gate 40840Sstevel@tonic-gate val = 1; 40850Sstevel@tonic-gate error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 40860Sstevel@tonic-gate &val, (t_uscalar_t)sizeof (val)); 40870Sstevel@tonic-gate if (error) 40880Sstevel@tonic-gate return (error); 40890Sstevel@tonic-gate dontroute = 1; 40900Sstevel@tonic-gate } 40910Sstevel@tonic-gate 40920Sstevel@tonic-gate if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) { 40930Sstevel@tonic-gate error = EOPNOTSUPP; 40940Sstevel@tonic-gate goto done; 40950Sstevel@tonic-gate } 40960Sstevel@tonic-gate if (msg->msg_controllen != 0) { 40970Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) { 40980Sstevel@tonic-gate error = sosend_dgramcmsg(so, name, namelen, uiop, 4099*741Smasputra msg->msg_control, msg->msg_controllen, flags); 41000Sstevel@tonic-gate } else { 41010Sstevel@tonic-gate if (flags & MSG_OOB) { 41020Sstevel@tonic-gate /* Can't generate T_EXDATA_REQ with options */ 41030Sstevel@tonic-gate error = EOPNOTSUPP; 41040Sstevel@tonic-gate goto done; 41050Sstevel@tonic-gate } 41060Sstevel@tonic-gate error = sosend_svccmsg(so, uiop, 41070Sstevel@tonic-gate !(flags & MSG_EOR), 41080Sstevel@tonic-gate msg->msg_control, msg->msg_controllen, 41090Sstevel@tonic-gate flags); 41100Sstevel@tonic-gate } 41110Sstevel@tonic-gate goto done; 41120Sstevel@tonic-gate } 41130Sstevel@tonic-gate 41140Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) { 41150Sstevel@tonic-gate /* 41160Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately 4117*741Smasputra * from send_dgram. This can allow tail-call optimizations. 41180Sstevel@tonic-gate */ 41190Sstevel@tonic-gate if (!dontroute) { 41200Sstevel@tonic-gate return (sosend_dgram(so, name, namelen, uiop, flags)); 41210Sstevel@tonic-gate } 41220Sstevel@tonic-gate error = sosend_dgram(so, name, namelen, uiop, flags); 41230Sstevel@tonic-gate } else { 41240Sstevel@tonic-gate t_scalar_t prim; 41250Sstevel@tonic-gate int sflag; 41260Sstevel@tonic-gate 41270Sstevel@tonic-gate /* Ignore msg_name in the connected state */ 41280Sstevel@tonic-gate if (flags & MSG_OOB) { 41290Sstevel@tonic-gate prim = T_EXDATA_REQ; 41300Sstevel@tonic-gate /* 41310Sstevel@tonic-gate * Send down T_EXDATA_REQ even if there is flow 41320Sstevel@tonic-gate * control for data. 41330Sstevel@tonic-gate */ 41340Sstevel@tonic-gate sflag = MSG_IGNFLOW; 41350Sstevel@tonic-gate } else { 41360Sstevel@tonic-gate if (so_mode & SM_BYTESTREAM) { 41370Sstevel@tonic-gate /* Byte stream transport - use write */ 41380Sstevel@tonic-gate 41390Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg: write\n")); 41400Sstevel@tonic-gate /* 4141*741Smasputra * If there is no SO_DONTROUTE to turn off, 4142*741Smasputra * SS_DIRECT is on, and there is no flow 4143*741Smasputra * control, we can take the fast path. 41440Sstevel@tonic-gate */ 4145*741Smasputra if (!dontroute && 4146*741Smasputra (so_state & SS_DIRECT) && 4147*741Smasputra canputnext(SOTOV(so)->v_stream->sd_wrq)) { 4148*741Smasputra return (sostream_direct(so, uiop, 4149*741Smasputra NULL, CRED())); 4150*741Smasputra } 41510Sstevel@tonic-gate error = strwrite(SOTOV(so), uiop, CRED()); 41520Sstevel@tonic-gate goto done; 41530Sstevel@tonic-gate } 41540Sstevel@tonic-gate prim = T_DATA_REQ; 41550Sstevel@tonic-gate sflag = 0; 41560Sstevel@tonic-gate } 41570Sstevel@tonic-gate /* 41580Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately 41590Sstevel@tonic-gate * from sosend_svc. This can allow tail-call optimizations. 41600Sstevel@tonic-gate */ 41610Sstevel@tonic-gate if (!dontroute) 41620Sstevel@tonic-gate return (sosend_svc(so, uiop, prim, 41630Sstevel@tonic-gate !(flags & MSG_EOR), sflag)); 41640Sstevel@tonic-gate error = sosend_svc(so, uiop, prim, 41650Sstevel@tonic-gate !(flags & MSG_EOR), sflag); 41660Sstevel@tonic-gate } 41670Sstevel@tonic-gate ASSERT(dontroute); 41680Sstevel@tonic-gate done: 41690Sstevel@tonic-gate if (dontroute) { 41700Sstevel@tonic-gate uint32_t val; 41710Sstevel@tonic-gate 41720Sstevel@tonic-gate val = 0; 41730Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 41740Sstevel@tonic-gate &val, (t_uscalar_t)sizeof (val)); 41750Sstevel@tonic-gate } 41760Sstevel@tonic-gate return (error); 41770Sstevel@tonic-gate } 41780Sstevel@tonic-gate 41790Sstevel@tonic-gate /* 4180*741Smasputra * Sending data on a datagram socket. 4181*741Smasputra * Assumes caller has verified that SS_ISBOUND etc. are set. 4182*741Smasputra */ 4183*741Smasputra /* ARGSUSED */ 4184*741Smasputra static int 4185*741Smasputra sodgram_direct(struct sonode *so, struct sockaddr *name, 4186*741Smasputra socklen_t namelen, struct uio *uiop, int flags) 4187*741Smasputra { 4188*741Smasputra struct T_unitdata_req tudr; 4189*741Smasputra mblk_t *mp; 4190*741Smasputra int error = 0; 4191*741Smasputra void *addr; 4192*741Smasputra socklen_t addrlen; 4193*741Smasputra ssize_t len; 4194*741Smasputra struct stdata *stp = SOTOV(so)->v_stream; 4195*741Smasputra int so_state; 4196*741Smasputra queue_t *udp_wq; 4197*741Smasputra 4198*741Smasputra ASSERT(name != NULL && namelen != 0); 4199*741Smasputra ASSERT(!(so->so_mode & SM_CONNREQUIRED)); 4200*741Smasputra ASSERT(!(so->so_mode & SM_EXDATA)); 4201*741Smasputra ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 4202*741Smasputra ASSERT(SOTOV(so)->v_type == VSOCK); 4203*741Smasputra 4204*741Smasputra /* Caller checked for proper length */ 4205*741Smasputra len = uiop->uio_resid; 4206*741Smasputra ASSERT(len <= so->so_tidu_size); 4207*741Smasputra 4208*741Smasputra /* Length and family checks have been done by caller */ 4209*741Smasputra ASSERT(name->sa_family == so->so_family); 4210*741Smasputra ASSERT(so->so_family == AF_INET || 4211*741Smasputra (namelen == (socklen_t)sizeof (struct sockaddr_in6))); 4212*741Smasputra ASSERT(so->so_family == AF_INET6 || 4213*741Smasputra (namelen == (socklen_t)sizeof (struct sockaddr_in))); 4214*741Smasputra 4215*741Smasputra addr = name; 4216*741Smasputra addrlen = namelen; 4217*741Smasputra 4218*741Smasputra if (stp->sd_sidp != NULL && 4219*741Smasputra (error = straccess(stp, JCWRITE)) != 0) 4220*741Smasputra goto done; 4221*741Smasputra 4222*741Smasputra so_state = so->so_state; 4223*741Smasputra 4224*741Smasputra /* 4225*741Smasputra * For UDP we don't break up the copyin into smaller pieces 4226*741Smasputra * as in the TCP case. That means if ENOMEM is returned by 4227*741Smasputra * mcopyinuio() then the uio vector has not been modified at 4228*741Smasputra * all and we fallback to either strwrite() or kstrputmsg() 4229*741Smasputra * below. Note also that we never generate priority messages 4230*741Smasputra * from here. 4231*741Smasputra */ 4232*741Smasputra udp_wq = stp->sd_wrq->q_next; 4233*741Smasputra if (canput(udp_wq) && 4234*741Smasputra (mp = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) { 4235*741Smasputra ASSERT(DB_TYPE(mp) == M_DATA); 4236*741Smasputra ASSERT(uiop->uio_resid == 0); 4237*741Smasputra #ifdef C2_AUDIT 4238*741Smasputra if (audit_active) 4239*741Smasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 4240*741Smasputra #endif /* C2_AUDIT */ 4241*741Smasputra udp_wput_data(udp_wq, mp, addr, addrlen); 4242*741Smasputra return (0); 4243*741Smasputra } 4244*741Smasputra if (error != 0 && error != ENOMEM) 4245*741Smasputra return (error); 4246*741Smasputra 4247*741Smasputra /* 4248*741Smasputra * For connected, let strwrite() handle the blocking case. 4249*741Smasputra * Otherwise we fall thru and use kstrputmsg(). 4250*741Smasputra */ 4251*741Smasputra if (so_state & SS_ISCONNECTED) 4252*741Smasputra return (strwrite(SOTOV(so), uiop, CRED())); 4253*741Smasputra 4254*741Smasputra tudr.PRIM_type = T_UNITDATA_REQ; 4255*741Smasputra tudr.DEST_length = addrlen; 4256*741Smasputra tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 4257*741Smasputra tudr.OPT_length = 0; 4258*741Smasputra tudr.OPT_offset = 0; 4259*741Smasputra 4260*741Smasputra mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0, _ALLOC_INTR); 4261*741Smasputra if (mp == NULL) { 4262*741Smasputra /* 4263*741Smasputra * Caught a signal waiting for memory. 4264*741Smasputra * Let send* return EINTR. 4265*741Smasputra */ 4266*741Smasputra error = EINTR; 4267*741Smasputra goto done; 4268*741Smasputra } 4269*741Smasputra 4270*741Smasputra #ifdef C2_AUDIT 4271*741Smasputra if (audit_active) 4272*741Smasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 4273*741Smasputra #endif /* C2_AUDIT */ 4274*741Smasputra 4275*741Smasputra error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 4276*741Smasputra done: 4277*741Smasputra #ifdef SOCK_DEBUG 4278*741Smasputra if (error != 0) { 4279*741Smasputra eprintsoline(so, error); 4280*741Smasputra } 4281*741Smasputra #endif /* SOCK_DEBUG */ 4282*741Smasputra return (error); 4283*741Smasputra } 4284*741Smasputra 4285*741Smasputra int 4286*741Smasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr) 4287*741Smasputra { 4288*741Smasputra struct stdata *stp = SOTOV(so)->v_stream; 4289*741Smasputra ssize_t iosize, rmax, maxblk; 4290*741Smasputra queue_t *tcp_wq = stp->sd_wrq->q_next; 4291*741Smasputra int error = 0, wflag = 0; 4292*741Smasputra 4293*741Smasputra ASSERT(so->so_mode & SM_BYTESTREAM); 4294*741Smasputra ASSERT(SOTOV(so)->v_type == VSOCK); 4295*741Smasputra 4296*741Smasputra if (stp->sd_sidp != NULL && 4297*741Smasputra (error = straccess(stp, JCWRITE)) != 0) 4298*741Smasputra return (error); 4299*741Smasputra 4300*741Smasputra if (uiop == NULL) { 4301*741Smasputra /* 4302*741Smasputra * kstrwritemp() should have checked sd_flag and 4303*741Smasputra * flow-control before coming here. If we end up 4304*741Smasputra * here it means that we can simply pass down the 4305*741Smasputra * data to tcp. 4306*741Smasputra */ 4307*741Smasputra ASSERT(mp != NULL); 4308*741Smasputra tcp_wput(tcp_wq, mp); 4309*741Smasputra return (0); 4310*741Smasputra } 4311*741Smasputra 4312*741Smasputra /* Fallback to strwrite() to do proper error handling */ 4313*741Smasputra if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY)) 4314*741Smasputra return (strwrite(SOTOV(so), uiop, cr)); 4315*741Smasputra 4316*741Smasputra rmax = stp->sd_qn_maxpsz; 4317*741Smasputra ASSERT(rmax >= 0 || rmax == INFPSZ); 4318*741Smasputra if (rmax == 0 || uiop->uio_resid <= 0) 4319*741Smasputra return (0); 4320*741Smasputra 4321*741Smasputra if (rmax == INFPSZ) 4322*741Smasputra rmax = uiop->uio_resid; 4323*741Smasputra 4324*741Smasputra maxblk = stp->sd_maxblk; 4325*741Smasputra 4326*741Smasputra for (;;) { 4327*741Smasputra iosize = MIN(uiop->uio_resid, rmax); 4328*741Smasputra 4329*741Smasputra mp = mcopyinuio(stp, uiop, iosize, maxblk, &error); 4330*741Smasputra if (mp == NULL) { 4331*741Smasputra /* 4332*741Smasputra * Fallback to strwrite() for ENOMEM; if this 4333*741Smasputra * is our first time in this routine and the uio 4334*741Smasputra * vector has not been modified, we will end up 4335*741Smasputra * calling strwrite() without any flag set. 4336*741Smasputra */ 4337*741Smasputra if (error == ENOMEM) 4338*741Smasputra goto slow_send; 4339*741Smasputra else 4340*741Smasputra return (error); 4341*741Smasputra } 4342*741Smasputra ASSERT(uiop->uio_resid >= 0); 4343*741Smasputra /* 4344*741Smasputra * If mp is non-NULL and ENOMEM is set, it means that 4345*741Smasputra * mcopyinuio() was able to break down some of the user 4346*741Smasputra * data into one or more mblks. Send the partial data 4347*741Smasputra * to tcp and let the rest be handled in strwrite(). 4348*741Smasputra */ 4349*741Smasputra ASSERT(error == 0 || error == ENOMEM); 4350*741Smasputra tcp_wput(tcp_wq, mp); 4351*741Smasputra 4352*741Smasputra wflag |= NOINTR; 4353*741Smasputra 4354*741Smasputra if (uiop->uio_resid == 0) { /* No more data; we're done */ 4355*741Smasputra ASSERT(error == 0); 4356*741Smasputra break; 4357*741Smasputra } else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag & 4358*741Smasputra (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) { 4359*741Smasputra slow_send: 4360*741Smasputra /* 4361*741Smasputra * We were able to send down partial data using 4362*741Smasputra * the direct call interface, but are now relying 4363*741Smasputra * on strwrite() to handle the non-fastpath cases. 4364*741Smasputra * If the socket is blocking we will sleep in 4365*741Smasputra * strwaitq() until write is permitted, otherwise, 4366*741Smasputra * we will need to return the amount of bytes 4367*741Smasputra * written so far back to the app. This is the 4368*741Smasputra * reason why we pass NOINTR flag to strwrite() 4369*741Smasputra * for non-blocking socket, because we don't want 4370*741Smasputra * to return EAGAIN when portion of the user data 4371*741Smasputra * has actually been sent down. 4372*741Smasputra */ 4373*741Smasputra return (strwrite_common(SOTOV(so), uiop, cr, wflag)); 4374*741Smasputra } 4375*741Smasputra } 4376*741Smasputra return (0); 4377*741Smasputra } 4378*741Smasputra 4379*741Smasputra /* 43800Sstevel@tonic-gate * Update so_faddr by asking the transport (unless AF_UNIX). 43810Sstevel@tonic-gate */ 43820Sstevel@tonic-gate int 43830Sstevel@tonic-gate sotpi_getpeername(struct sonode *so) 43840Sstevel@tonic-gate { 43850Sstevel@tonic-gate struct strbuf strbuf; 43860Sstevel@tonic-gate int error = 0, res; 43870Sstevel@tonic-gate void *addr; 43880Sstevel@tonic-gate t_uscalar_t addrlen; 43890Sstevel@tonic-gate k_sigset_t smask; 43900Sstevel@tonic-gate 43910Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername(%p) %s\n", 43920Sstevel@tonic-gate so, pr_state(so->so_state, so->so_mode))); 43930Sstevel@tonic-gate 43940Sstevel@tonic-gate mutex_enter(&so->so_lock); 43950Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 43960Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) { 43970Sstevel@tonic-gate error = ENOTCONN; 43980Sstevel@tonic-gate goto done; 43990Sstevel@tonic-gate } 44000Sstevel@tonic-gate /* Added this check for X/Open */ 44010Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 44020Sstevel@tonic-gate error = EINVAL; 44030Sstevel@tonic-gate if (xnet_check_print) { 44040Sstevel@tonic-gate printf("sockfs: X/Open getpeername check => EINVAL\n"); 44050Sstevel@tonic-gate } 44060Sstevel@tonic-gate goto done; 44070Sstevel@tonic-gate } 44080Sstevel@tonic-gate #ifdef DEBUG 44090Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (local): %s\n", 44100Sstevel@tonic-gate pr_addr(so->so_family, so->so_faddr_sa, 44110Sstevel@tonic-gate (t_uscalar_t)so->so_faddr_len))); 44120Sstevel@tonic-gate #endif /* DEBUG */ 44130Sstevel@tonic-gate 44140Sstevel@tonic-gate if (so->so_family == AF_UNIX || so->so_family == AF_NCA) { 44150Sstevel@tonic-gate /* Transport has different name space - return local info */ 44160Sstevel@tonic-gate error = 0; 44170Sstevel@tonic-gate goto done; 44180Sstevel@tonic-gate } 44190Sstevel@tonic-gate 44200Sstevel@tonic-gate ASSERT(so->so_faddr_sa); 44210Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */ 44220Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_faddr_maxlen; 44230Sstevel@tonic-gate mutex_exit(&so->so_lock); 44240Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP); 44250Sstevel@tonic-gate 44260Sstevel@tonic-gate /* 44270Sstevel@tonic-gate * Issue TI_GETPEERNAME with signals masked. 44280Sstevel@tonic-gate * Put the result in so_faddr_sa so that getpeername works after 44290Sstevel@tonic-gate * a shutdown(output). 44300Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 44310Sstevel@tonic-gate * back to the socket. 44320Sstevel@tonic-gate */ 44330Sstevel@tonic-gate strbuf.buf = addr; 44340Sstevel@tonic-gate strbuf.maxlen = addrlen; 44350Sstevel@tonic-gate strbuf.len = 0; 44360Sstevel@tonic-gate 44370Sstevel@tonic-gate sigintr(&smask, 0); 44380Sstevel@tonic-gate res = 0; 44390Sstevel@tonic-gate ASSERT(CRED()); 44400Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf, 44410Sstevel@tonic-gate 0, K_TO_K, CRED(), &res); 44420Sstevel@tonic-gate sigunintr(&smask); 44430Sstevel@tonic-gate 44440Sstevel@tonic-gate mutex_enter(&so->so_lock); 44450Sstevel@tonic-gate /* 44460Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail 44470Sstevel@tonic-gate * the getpeername. Instead fallback on the recorded 44480Sstevel@tonic-gate * so->so_faddr_sa. 44490Sstevel@tonic-gate */ 44500Sstevel@tonic-gate if (error) { 44510Sstevel@tonic-gate /* 44520Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl. 44530Sstevel@tonic-gate * However, it is impossible to determine which ones of 44540Sstevel@tonic-gate * these are really socket level errors that were incorrectly 44550Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the 44560Sstevel@tonic-gate * error - to code explicitly does not reinstate the error 44570Sstevel@tonic-gate * using soseterror(). 44580Sstevel@tonic-gate * Experiments have shows that at least this set of 44590Sstevel@tonic-gate * errors are reported and should not be reinstated on the 44600Sstevel@tonic-gate * socket: 44610Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when 44620Sstevel@tonic-gate * getpeername was called. 44630Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write 44640Sstevel@tonic-gate * side error over the read side error. 44650Sstevel@tonic-gate * ENOTCONN The transport just got disconnected but 44660Sstevel@tonic-gate * sockfs had not yet seen the T_DISCON_IND 44670Sstevel@tonic-gate * when issuing the ioctl. 44680Sstevel@tonic-gate */ 44690Sstevel@tonic-gate error = 0; 44700Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 && 44710Sstevel@tonic-gate (so->so_state & SS_ISCONNECTED)) { 44720Sstevel@tonic-gate ASSERT(strbuf.len <= (int)so->so_faddr_maxlen); 44730Sstevel@tonic-gate so->so_faddr_len = (socklen_t)strbuf.len; 44740Sstevel@tonic-gate bcopy(addr, so->so_faddr_sa, so->so_faddr_len); 44750Sstevel@tonic-gate so->so_state |= SS_FADDR_VALID; 44760Sstevel@tonic-gate } 44770Sstevel@tonic-gate kmem_free(addr, addrlen); 44780Sstevel@tonic-gate #ifdef DEBUG 44790Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (tp): %s\n", 44800Sstevel@tonic-gate pr_addr(so->so_family, so->so_faddr_sa, 44810Sstevel@tonic-gate (t_uscalar_t)so->so_faddr_len))); 44820Sstevel@tonic-gate #endif /* DEBUG */ 44830Sstevel@tonic-gate done: 44840Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 44850Sstevel@tonic-gate mutex_exit(&so->so_lock); 44860Sstevel@tonic-gate return (error); 44870Sstevel@tonic-gate } 44880Sstevel@tonic-gate 44890Sstevel@tonic-gate /* 44900Sstevel@tonic-gate * Update so_laddr by asking the transport (unless AF_UNIX). 44910Sstevel@tonic-gate */ 44920Sstevel@tonic-gate int 44930Sstevel@tonic-gate sotpi_getsockname(struct sonode *so) 44940Sstevel@tonic-gate { 44950Sstevel@tonic-gate struct strbuf strbuf; 44960Sstevel@tonic-gate int error = 0, res; 44970Sstevel@tonic-gate void *addr; 44980Sstevel@tonic-gate t_uscalar_t addrlen; 44990Sstevel@tonic-gate k_sigset_t smask; 45000Sstevel@tonic-gate 45010Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname(%p) %s\n", 45020Sstevel@tonic-gate so, pr_state(so->so_state, so->so_mode))); 45030Sstevel@tonic-gate 45040Sstevel@tonic-gate mutex_enter(&so->so_lock); 45050Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 45060Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND) && so->so_family != AF_UNIX) { 45070Sstevel@tonic-gate /* Return an all zero address except for the family */ 45080Sstevel@tonic-gate if (so->so_family == AF_INET) 45090Sstevel@tonic-gate so->so_laddr_len = (socklen_t)sizeof (sin_t); 45100Sstevel@tonic-gate else if (so->so_family == AF_INET6) 45110Sstevel@tonic-gate so->so_laddr_len = (socklen_t)sizeof (sin6_t); 45120Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 45130Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 45140Sstevel@tonic-gate /* 45150Sstevel@tonic-gate * Can not assume there is a sa_family for all 45160Sstevel@tonic-gate * protocol families. 45170Sstevel@tonic-gate */ 45180Sstevel@tonic-gate if (so->so_family == AF_INET || so->so_family == AF_INET6) 45190Sstevel@tonic-gate so->so_laddr_sa->sa_family = so->so_family; 45200Sstevel@tonic-gate } 45210Sstevel@tonic-gate #ifdef DEBUG 45220Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (local): %s\n", 45230Sstevel@tonic-gate pr_addr(so->so_family, so->so_laddr_sa, 45240Sstevel@tonic-gate (t_uscalar_t)so->so_laddr_len))); 45250Sstevel@tonic-gate #endif /* DEBUG */ 45260Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 45270Sstevel@tonic-gate /* Transport has different name space - return local info */ 45280Sstevel@tonic-gate error = 0; 45290Sstevel@tonic-gate goto done; 45300Sstevel@tonic-gate } 45310Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */ 45320Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_laddr_maxlen; 45330Sstevel@tonic-gate mutex_exit(&so->so_lock); 45340Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP); 45350Sstevel@tonic-gate 45360Sstevel@tonic-gate /* 45370Sstevel@tonic-gate * Issue TI_GETMYNAME with signals masked. 45380Sstevel@tonic-gate * Put the result in so_laddr_sa so that getsockname works after 45390Sstevel@tonic-gate * a shutdown(output). 45400Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 45410Sstevel@tonic-gate * back to the socket. 45420Sstevel@tonic-gate */ 45430Sstevel@tonic-gate strbuf.buf = addr; 45440Sstevel@tonic-gate strbuf.maxlen = addrlen; 45450Sstevel@tonic-gate strbuf.len = 0; 45460Sstevel@tonic-gate 45470Sstevel@tonic-gate sigintr(&smask, 0); 45480Sstevel@tonic-gate res = 0; 45490Sstevel@tonic-gate ASSERT(CRED()); 45500Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf, 45510Sstevel@tonic-gate 0, K_TO_K, CRED(), &res); 45520Sstevel@tonic-gate sigunintr(&smask); 45530Sstevel@tonic-gate 45540Sstevel@tonic-gate mutex_enter(&so->so_lock); 45550Sstevel@tonic-gate /* 45560Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail 45570Sstevel@tonic-gate * the getsockname. Instead fallback on the recorded 45580Sstevel@tonic-gate * so->so_laddr_sa. 45590Sstevel@tonic-gate */ 45600Sstevel@tonic-gate if (error) { 45610Sstevel@tonic-gate /* 45620Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl. 45630Sstevel@tonic-gate * However, it is impossible to determine which ones of 45640Sstevel@tonic-gate * these are really socket level errors that were incorrectly 45650Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the 45660Sstevel@tonic-gate * error - to code explicitly does not reinstate the error 45670Sstevel@tonic-gate * using soseterror(). 45680Sstevel@tonic-gate * Experiments have shows that at least this set of 45690Sstevel@tonic-gate * errors are reported and should not be reinstated on the 45700Sstevel@tonic-gate * socket: 45710Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when 45720Sstevel@tonic-gate * getsockname was called. 45730Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write 45740Sstevel@tonic-gate * side error over the read side error. 45750Sstevel@tonic-gate */ 45760Sstevel@tonic-gate error = 0; 45770Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 && 45780Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) { 45790Sstevel@tonic-gate ASSERT(strbuf.len <= (int)so->so_laddr_maxlen); 45800Sstevel@tonic-gate so->so_laddr_len = (socklen_t)strbuf.len; 45810Sstevel@tonic-gate bcopy(addr, so->so_laddr_sa, so->so_laddr_len); 45820Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 45830Sstevel@tonic-gate } 45840Sstevel@tonic-gate kmem_free(addr, addrlen); 45850Sstevel@tonic-gate #ifdef DEBUG 45860Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (tp): %s\n", 45870Sstevel@tonic-gate pr_addr(so->so_family, so->so_laddr_sa, 45880Sstevel@tonic-gate (t_uscalar_t)so->so_laddr_len))); 45890Sstevel@tonic-gate #endif /* DEBUG */ 45900Sstevel@tonic-gate done: 45910Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 45920Sstevel@tonic-gate mutex_exit(&so->so_lock); 45930Sstevel@tonic-gate return (error); 45940Sstevel@tonic-gate } 45950Sstevel@tonic-gate 45960Sstevel@tonic-gate /* 45970Sstevel@tonic-gate * Get socket options. For SOL_SOCKET options some options are handled 45980Sstevel@tonic-gate * by the sockfs while others use the value recorded in the sonode as a 45990Sstevel@tonic-gate * fallback should the T_SVR4_OPTMGMT_REQ fail. 46000Sstevel@tonic-gate * 46010Sstevel@tonic-gate * On the return most *optlenp bytes are copied to optval. 46020Sstevel@tonic-gate */ 46030Sstevel@tonic-gate int 46040Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name, 46050Sstevel@tonic-gate void *optval, socklen_t *optlenp, int flags) 46060Sstevel@tonic-gate { 46070Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req; 46080Sstevel@tonic-gate struct T_optmgmt_ack *optmgmt_ack; 46090Sstevel@tonic-gate struct opthdr oh; 46100Sstevel@tonic-gate struct opthdr *opt_res; 46110Sstevel@tonic-gate mblk_t *mp = NULL; 46120Sstevel@tonic-gate int error = 0; 46130Sstevel@tonic-gate void *option = NULL; /* Set if fallback value */ 46140Sstevel@tonic-gate t_uscalar_t maxlen = *optlenp; 46150Sstevel@tonic-gate t_uscalar_t len; 46160Sstevel@tonic-gate uint32_t value; 46170Sstevel@tonic-gate 46180Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n", 46190Sstevel@tonic-gate so, level, option_name, optval, optlenp, 46200Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 46210Sstevel@tonic-gate 46220Sstevel@tonic-gate mutex_enter(&so->so_lock); 46230Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 46240Sstevel@tonic-gate 46250Sstevel@tonic-gate /* 46260Sstevel@tonic-gate * Check for SOL_SOCKET options. 46270Sstevel@tonic-gate * Certain SOL_SOCKET options are returned directly whereas 46280Sstevel@tonic-gate * others only provide a default (fallback) value should 46290Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ fail. 46300Sstevel@tonic-gate */ 46310Sstevel@tonic-gate if (level == SOL_SOCKET) { 46320Sstevel@tonic-gate /* Check parameters */ 46330Sstevel@tonic-gate switch (option_name) { 46340Sstevel@tonic-gate case SO_TYPE: 46350Sstevel@tonic-gate case SO_ERROR: 46360Sstevel@tonic-gate case SO_DEBUG: 46370Sstevel@tonic-gate case SO_ACCEPTCONN: 46380Sstevel@tonic-gate case SO_REUSEADDR: 46390Sstevel@tonic-gate case SO_KEEPALIVE: 46400Sstevel@tonic-gate case SO_DONTROUTE: 46410Sstevel@tonic-gate case SO_BROADCAST: 46420Sstevel@tonic-gate case SO_USELOOPBACK: 46430Sstevel@tonic-gate case SO_OOBINLINE: 46440Sstevel@tonic-gate case SO_SNDBUF: 46450Sstevel@tonic-gate case SO_RCVBUF: 46460Sstevel@tonic-gate #ifdef notyet 46470Sstevel@tonic-gate case SO_SNDLOWAT: 46480Sstevel@tonic-gate case SO_RCVLOWAT: 46490Sstevel@tonic-gate case SO_SNDTIMEO: 46500Sstevel@tonic-gate case SO_RCVTIMEO: 46510Sstevel@tonic-gate #endif /* notyet */ 46520Sstevel@tonic-gate case SO_DGRAM_ERRIND: 46530Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (int32_t)) { 46540Sstevel@tonic-gate error = EINVAL; 46550Sstevel@tonic-gate eprintsoline(so, error); 46560Sstevel@tonic-gate goto done2; 46570Sstevel@tonic-gate } 46580Sstevel@tonic-gate break; 46590Sstevel@tonic-gate case SO_LINGER: 46600Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (struct linger)) { 46610Sstevel@tonic-gate error = EINVAL; 46620Sstevel@tonic-gate eprintsoline(so, error); 46630Sstevel@tonic-gate goto done2; 46640Sstevel@tonic-gate } 46650Sstevel@tonic-gate break; 46660Sstevel@tonic-gate } 46670Sstevel@tonic-gate 46680Sstevel@tonic-gate len = (t_uscalar_t)sizeof (uint32_t); /* Default */ 46690Sstevel@tonic-gate 46700Sstevel@tonic-gate switch (option_name) { 46710Sstevel@tonic-gate case SO_TYPE: 46720Sstevel@tonic-gate value = so->so_type; 46730Sstevel@tonic-gate option = &value; 46740Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 46750Sstevel@tonic-gate 46760Sstevel@tonic-gate case SO_ERROR: 46770Sstevel@tonic-gate value = sogeterr(so); 46780Sstevel@tonic-gate option = &value; 46790Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 46800Sstevel@tonic-gate 46810Sstevel@tonic-gate case SO_ACCEPTCONN: 46820Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) 46830Sstevel@tonic-gate value = SO_ACCEPTCONN; 46840Sstevel@tonic-gate else 46850Sstevel@tonic-gate value = 0; 46860Sstevel@tonic-gate #ifdef DEBUG 46870Sstevel@tonic-gate if (value) { 46880Sstevel@tonic-gate dprintso(so, 1, 46890Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n", 46900Sstevel@tonic-gate option_name)); 46910Sstevel@tonic-gate } else { 46920Sstevel@tonic-gate dprintso(so, 1, 46930Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n", 46940Sstevel@tonic-gate option_name)); 46950Sstevel@tonic-gate } 46960Sstevel@tonic-gate #endif /* DEBUG */ 46970Sstevel@tonic-gate option = &value; 46980Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 46990Sstevel@tonic-gate 47000Sstevel@tonic-gate case SO_DEBUG: 47010Sstevel@tonic-gate case SO_REUSEADDR: 47020Sstevel@tonic-gate case SO_KEEPALIVE: 47030Sstevel@tonic-gate case SO_DONTROUTE: 47040Sstevel@tonic-gate case SO_BROADCAST: 47050Sstevel@tonic-gate case SO_USELOOPBACK: 47060Sstevel@tonic-gate case SO_OOBINLINE: 47070Sstevel@tonic-gate case SO_DGRAM_ERRIND: 47080Sstevel@tonic-gate value = (so->so_options & option_name); 47090Sstevel@tonic-gate #ifdef DEBUG 47100Sstevel@tonic-gate if (value) { 47110Sstevel@tonic-gate dprintso(so, 1, 47120Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n", 47130Sstevel@tonic-gate option_name)); 47140Sstevel@tonic-gate } else { 47150Sstevel@tonic-gate dprintso(so, 1, 47160Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n", 47170Sstevel@tonic-gate option_name)); 47180Sstevel@tonic-gate } 47190Sstevel@tonic-gate #endif /* DEBUG */ 47200Sstevel@tonic-gate option = &value; 47210Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 47220Sstevel@tonic-gate 47230Sstevel@tonic-gate /* 47240Sstevel@tonic-gate * The following options are only returned by sockfs when the 47250Sstevel@tonic-gate * T_SVR4_OPTMGMT_REQ fails. 47260Sstevel@tonic-gate */ 47270Sstevel@tonic-gate case SO_LINGER: 47280Sstevel@tonic-gate option = &so->so_linger; 47290Sstevel@tonic-gate len = (t_uscalar_t)sizeof (struct linger); 47300Sstevel@tonic-gate break; 47310Sstevel@tonic-gate case SO_SNDBUF: { 47320Sstevel@tonic-gate ssize_t lvalue; 47330Sstevel@tonic-gate 47340Sstevel@tonic-gate /* 47350Sstevel@tonic-gate * If the option has not been set then get a default 47360Sstevel@tonic-gate * value from the read queue. This value is 47370Sstevel@tonic-gate * returned if the transport fails 47380Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ. 47390Sstevel@tonic-gate */ 47400Sstevel@tonic-gate lvalue = so->so_sndbuf; 47410Sstevel@tonic-gate if (lvalue == 0) { 47420Sstevel@tonic-gate mutex_exit(&so->so_lock); 47430Sstevel@tonic-gate (void) strqget(strvp2wq(SOTOV(so))->q_next, 47440Sstevel@tonic-gate QHIWAT, 0, &lvalue); 47450Sstevel@tonic-gate mutex_enter(&so->so_lock); 47460Sstevel@tonic-gate dprintso(so, 1, 47470Sstevel@tonic-gate ("got SO_SNDBUF %ld from q\n", lvalue)); 47480Sstevel@tonic-gate } 47490Sstevel@tonic-gate value = (int)lvalue; 47500Sstevel@tonic-gate option = &value; 47510Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_sndbuf); 47520Sstevel@tonic-gate break; 47530Sstevel@tonic-gate } 47540Sstevel@tonic-gate case SO_RCVBUF: { 47550Sstevel@tonic-gate ssize_t lvalue; 47560Sstevel@tonic-gate 47570Sstevel@tonic-gate /* 47580Sstevel@tonic-gate * If the option has not been set then get a default 47590Sstevel@tonic-gate * value from the read queue. This value is 47600Sstevel@tonic-gate * returned if the transport fails 47610Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ. 47620Sstevel@tonic-gate * 47630Sstevel@tonic-gate * XXX If SO_RCVBUF has been set and this is an 47640Sstevel@tonic-gate * XPG 4.2 application then do not ask the transport 47650Sstevel@tonic-gate * since the transport might adjust the value and not 47660Sstevel@tonic-gate * return exactly what was set by the application. 47670Sstevel@tonic-gate * For non-XPG 4.2 application we return the value 47680Sstevel@tonic-gate * that the transport is actually using. 47690Sstevel@tonic-gate */ 47700Sstevel@tonic-gate lvalue = so->so_rcvbuf; 47710Sstevel@tonic-gate if (lvalue == 0) { 47720Sstevel@tonic-gate mutex_exit(&so->so_lock); 47730Sstevel@tonic-gate (void) strqget(RD(strvp2wq(SOTOV(so))), 47740Sstevel@tonic-gate QHIWAT, 0, &lvalue); 47750Sstevel@tonic-gate mutex_enter(&so->so_lock); 47760Sstevel@tonic-gate dprintso(so, 1, 47770Sstevel@tonic-gate ("got SO_RCVBUF %ld from q\n", lvalue)); 47780Sstevel@tonic-gate } else if (flags & _SOGETSOCKOPT_XPG4_2) { 47790Sstevel@tonic-gate value = (int)lvalue; 47800Sstevel@tonic-gate option = &value; 47810Sstevel@tonic-gate goto copyout; /* skip asking transport */ 47820Sstevel@tonic-gate } 47830Sstevel@tonic-gate value = (int)lvalue; 47840Sstevel@tonic-gate option = &value; 47850Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_rcvbuf); 47860Sstevel@tonic-gate break; 47870Sstevel@tonic-gate } 47880Sstevel@tonic-gate #ifdef notyet 47890Sstevel@tonic-gate /* 47900Sstevel@tonic-gate * We do not implement the semantics of these options 47910Sstevel@tonic-gate * thus we shouldn't implement the options either. 47920Sstevel@tonic-gate */ 47930Sstevel@tonic-gate case SO_SNDLOWAT: 47940Sstevel@tonic-gate value = so->so_sndlowat; 47950Sstevel@tonic-gate option = &value; 47960Sstevel@tonic-gate break; 47970Sstevel@tonic-gate case SO_RCVLOWAT: 47980Sstevel@tonic-gate value = so->so_rcvlowat; 47990Sstevel@tonic-gate option = &value; 48000Sstevel@tonic-gate break; 48010Sstevel@tonic-gate case SO_SNDTIMEO: 48020Sstevel@tonic-gate value = so->so_sndtimeo; 48030Sstevel@tonic-gate option = &value; 48040Sstevel@tonic-gate break; 48050Sstevel@tonic-gate case SO_RCVTIMEO: 48060Sstevel@tonic-gate value = so->so_rcvtimeo; 48070Sstevel@tonic-gate option = &value; 48080Sstevel@tonic-gate break; 48090Sstevel@tonic-gate #endif /* notyet */ 48100Sstevel@tonic-gate } 48110Sstevel@tonic-gate } 48120Sstevel@tonic-gate 48130Sstevel@tonic-gate if (so->so_family == AF_NCA) { 48140Sstevel@tonic-gate goto done2; 48150Sstevel@tonic-gate } 48160Sstevel@tonic-gate 48170Sstevel@tonic-gate mutex_exit(&so->so_lock); 48180Sstevel@tonic-gate 48190Sstevel@tonic-gate /* Send request */ 48200Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 48210Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_CHECK; 48220Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen); 48230Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 48240Sstevel@tonic-gate 48250Sstevel@tonic-gate oh.level = level; 48260Sstevel@tonic-gate oh.name = option_name; 48270Sstevel@tonic-gate oh.len = maxlen; 48280Sstevel@tonic-gate 48290Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 48300Sstevel@tonic-gate &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP); 48310Sstevel@tonic-gate /* Let option management work in the presence of data flow control */ 48320Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 48330Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 48340Sstevel@tonic-gate mp = NULL; 48350Sstevel@tonic-gate mutex_enter(&so->so_lock); 48360Sstevel@tonic-gate if (error) { 48370Sstevel@tonic-gate eprintsoline(so, error); 48380Sstevel@tonic-gate goto done2; 48390Sstevel@tonic-gate } 48400Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 48410Sstevel@tonic-gate (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0); 48420Sstevel@tonic-gate if (error) { 48430Sstevel@tonic-gate if (option != NULL) { 48440Sstevel@tonic-gate /* We have a fallback value */ 48450Sstevel@tonic-gate error = 0; 48460Sstevel@tonic-gate goto copyout; 48470Sstevel@tonic-gate } 48480Sstevel@tonic-gate eprintsoline(so, error); 48490Sstevel@tonic-gate goto done2; 48500Sstevel@tonic-gate } 48510Sstevel@tonic-gate ASSERT(mp); 48520Sstevel@tonic-gate optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr; 48530Sstevel@tonic-gate opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset, 48540Sstevel@tonic-gate optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE); 48550Sstevel@tonic-gate if (opt_res == NULL) { 48560Sstevel@tonic-gate if (option != NULL) { 48570Sstevel@tonic-gate /* We have a fallback value */ 48580Sstevel@tonic-gate error = 0; 48590Sstevel@tonic-gate goto copyout; 48600Sstevel@tonic-gate } 48610Sstevel@tonic-gate error = EPROTO; 48620Sstevel@tonic-gate eprintsoline(so, error); 48630Sstevel@tonic-gate goto done; 48640Sstevel@tonic-gate } 48650Sstevel@tonic-gate option = &opt_res[1]; 48660Sstevel@tonic-gate 48670Sstevel@tonic-gate /* check to ensure that the option is within bounds */ 48680Sstevel@tonic-gate if (((uintptr_t)option + opt_res->len < (uintptr_t)option) || 48690Sstevel@tonic-gate (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) { 48700Sstevel@tonic-gate if (option != NULL) { 48710Sstevel@tonic-gate /* We have a fallback value */ 48720Sstevel@tonic-gate error = 0; 48730Sstevel@tonic-gate goto copyout; 48740Sstevel@tonic-gate } 48750Sstevel@tonic-gate error = EPROTO; 48760Sstevel@tonic-gate eprintsoline(so, error); 48770Sstevel@tonic-gate goto done; 48780Sstevel@tonic-gate } 48790Sstevel@tonic-gate 48800Sstevel@tonic-gate len = opt_res->len; 48810Sstevel@tonic-gate 48820Sstevel@tonic-gate copyout: { 48830Sstevel@tonic-gate t_uscalar_t size = MIN(len, maxlen); 48840Sstevel@tonic-gate bcopy(option, optval, size); 48850Sstevel@tonic-gate bcopy(&size, optlenp, sizeof (size)); 48860Sstevel@tonic-gate } 48870Sstevel@tonic-gate done: 48880Sstevel@tonic-gate freemsg(mp); 48890Sstevel@tonic-gate done2: 48900Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 48910Sstevel@tonic-gate mutex_exit(&so->so_lock); 48920Sstevel@tonic-gate return (error); 48930Sstevel@tonic-gate } 48940Sstevel@tonic-gate 48950Sstevel@tonic-gate /* 48960Sstevel@tonic-gate * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ. 48970Sstevel@tonic-gate * SOL_SOCKET options are also recorded in the sonode. A setsockopt for 48980Sstevel@tonic-gate * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails - 48990Sstevel@tonic-gate * setsockopt has to work even if the transport does not support the option. 49000Sstevel@tonic-gate */ 49010Sstevel@tonic-gate int 49020Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name, 49030Sstevel@tonic-gate const void *optval, t_uscalar_t optlen) 49040Sstevel@tonic-gate { 49050Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req; 49060Sstevel@tonic-gate struct opthdr oh; 49070Sstevel@tonic-gate mblk_t *mp; 49080Sstevel@tonic-gate int error = 0; 49090Sstevel@tonic-gate boolean_t handled = B_FALSE; 49100Sstevel@tonic-gate 49110Sstevel@tonic-gate dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n", 49120Sstevel@tonic-gate so, level, option_name, optval, optlen, 49130Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 49140Sstevel@tonic-gate 49150Sstevel@tonic-gate 49160Sstevel@tonic-gate /* X/Open requires this check */ 49170Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 49180Sstevel@tonic-gate if (xnet_check_print) 49190Sstevel@tonic-gate printf("sockfs: X/Open setsockopt check => EINVAL\n"); 49200Sstevel@tonic-gate return (EINVAL); 49210Sstevel@tonic-gate } 49220Sstevel@tonic-gate 49230Sstevel@tonic-gate /* Caller allocates aligned optval, or passes null */ 49240Sstevel@tonic-gate ASSERT(((uintptr_t)optval & (sizeof (t_scalar_t) - 1)) == 0); 49250Sstevel@tonic-gate /* If optval is null optlen is 0, and vice-versa */ 49260Sstevel@tonic-gate ASSERT(optval != NULL || optlen == 0); 49270Sstevel@tonic-gate ASSERT(optlen != 0 || optval == NULL); 49280Sstevel@tonic-gate 49290Sstevel@tonic-gate mutex_enter(&so->so_lock); 49300Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 49310Sstevel@tonic-gate mutex_exit(&so->so_lock); 49320Sstevel@tonic-gate 49330Sstevel@tonic-gate if (so->so_family == AF_NCA) { 49340Sstevel@tonic-gate /* Ignore any flow control problems with the transport. */ 49350Sstevel@tonic-gate mutex_enter(&so->so_lock); 49360Sstevel@tonic-gate goto done; 49370Sstevel@tonic-gate } 49380Sstevel@tonic-gate 49390Sstevel@tonic-gate /* 49400Sstevel@tonic-gate * For SOCKET or TCP level options, try to set it here itself 49410Sstevel@tonic-gate * provided socket has not been popped and we know the tcp 49420Sstevel@tonic-gate * structure (stored in so_priv). 49430Sstevel@tonic-gate */ 49440Sstevel@tonic-gate if ((level == SOL_SOCKET || level == IPPROTO_TCP) && 49450Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6) && 49460Sstevel@tonic-gate (so->so_version == SOV_SOCKSTREAM) && (so->so_priv != NULL)) { 49470Sstevel@tonic-gate tcp_t *tcp = so->so_priv; 49480Sstevel@tonic-gate boolean_t onoff; 49490Sstevel@tonic-gate 49500Sstevel@tonic-gate #define intvalue (*(int32_t *)optval) 49510Sstevel@tonic-gate 49520Sstevel@tonic-gate switch (level) { 49530Sstevel@tonic-gate case SOL_SOCKET: 49540Sstevel@tonic-gate switch (option_name) { /* Check length param */ 49550Sstevel@tonic-gate case SO_DEBUG: 49560Sstevel@tonic-gate case SO_REUSEADDR: 49570Sstevel@tonic-gate case SO_DONTROUTE: 49580Sstevel@tonic-gate case SO_BROADCAST: 49590Sstevel@tonic-gate case SO_USELOOPBACK: 49600Sstevel@tonic-gate case SO_OOBINLINE: 49610Sstevel@tonic-gate case SO_DGRAM_ERRIND: 49620Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 49630Sstevel@tonic-gate error = EINVAL; 49640Sstevel@tonic-gate eprintsoline(so, error); 49650Sstevel@tonic-gate mutex_enter(&so->so_lock); 49660Sstevel@tonic-gate goto done2; 49670Sstevel@tonic-gate } 49680Sstevel@tonic-gate ASSERT(optval); 49690Sstevel@tonic-gate onoff = intvalue != 0; 49700Sstevel@tonic-gate handled = B_TRUE; 49710Sstevel@tonic-gate break; 49720Sstevel@tonic-gate case SO_LINGER: 49730Sstevel@tonic-gate if (optlen != 49740Sstevel@tonic-gate (t_uscalar_t)sizeof (struct linger)) { 49750Sstevel@tonic-gate error = EINVAL; 49760Sstevel@tonic-gate eprintsoline(so, error); 49770Sstevel@tonic-gate mutex_enter(&so->so_lock); 49780Sstevel@tonic-gate goto done2; 49790Sstevel@tonic-gate } 49800Sstevel@tonic-gate ASSERT(optval); 49810Sstevel@tonic-gate handled = B_TRUE; 49820Sstevel@tonic-gate break; 49830Sstevel@tonic-gate } 49840Sstevel@tonic-gate 49850Sstevel@tonic-gate switch (option_name) { /* Do actions */ 49860Sstevel@tonic-gate case SO_LINGER: { 49870Sstevel@tonic-gate struct linger *lgr = (struct linger *)optval; 49880Sstevel@tonic-gate 49890Sstevel@tonic-gate if (lgr->l_onoff) { 49900Sstevel@tonic-gate tcp->tcp_linger = 1; 49910Sstevel@tonic-gate tcp->tcp_lingertime = lgr->l_linger; 49920Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER; 49930Sstevel@tonic-gate so->so_options |= SO_LINGER; 49940Sstevel@tonic-gate } else { 49950Sstevel@tonic-gate tcp->tcp_linger = 0; 49960Sstevel@tonic-gate tcp->tcp_lingertime = 0; 49970Sstevel@tonic-gate so->so_linger.l_onoff = 0; 49980Sstevel@tonic-gate so->so_options &= ~SO_LINGER; 49990Sstevel@tonic-gate } 50000Sstevel@tonic-gate so->so_linger.l_linger = lgr->l_linger; 50010Sstevel@tonic-gate handled = B_TRUE; 50020Sstevel@tonic-gate break; 50030Sstevel@tonic-gate } 50040Sstevel@tonic-gate case SO_DEBUG: 50050Sstevel@tonic-gate tcp->tcp_debug = onoff; 50060Sstevel@tonic-gate #ifdef SOCK_TEST 50070Sstevel@tonic-gate if (intvalue & 2) 50080Sstevel@tonic-gate sock_test_timelimit = 10 * hz; 50090Sstevel@tonic-gate else 50100Sstevel@tonic-gate sock_test_timelimit = 0; 50110Sstevel@tonic-gate 50120Sstevel@tonic-gate if (intvalue & 4) 50130Sstevel@tonic-gate do_useracc = 0; 50140Sstevel@tonic-gate else 50150Sstevel@tonic-gate do_useracc = 1; 50160Sstevel@tonic-gate #endif /* SOCK_TEST */ 50170Sstevel@tonic-gate break; 50180Sstevel@tonic-gate case SO_DONTROUTE: 50190Sstevel@tonic-gate /* 50200Sstevel@tonic-gate * SO_DONTROUTE, SO_USELOOPBACK and 50210Sstevel@tonic-gate * SO_BROADCAST are only of interest to IP. 50220Sstevel@tonic-gate * We track them here only so 50230Sstevel@tonic-gate * that we can report their current value. 50240Sstevel@tonic-gate */ 50250Sstevel@tonic-gate tcp->tcp_dontroute = onoff; 50260Sstevel@tonic-gate if (onoff) 50270Sstevel@tonic-gate so->so_options |= option_name; 50280Sstevel@tonic-gate else 50290Sstevel@tonic-gate so->so_options &= ~option_name; 50300Sstevel@tonic-gate break; 50310Sstevel@tonic-gate case SO_USELOOPBACK: 50320Sstevel@tonic-gate tcp->tcp_useloopback = onoff; 50330Sstevel@tonic-gate if (onoff) 50340Sstevel@tonic-gate so->so_options |= option_name; 50350Sstevel@tonic-gate else 50360Sstevel@tonic-gate so->so_options &= ~option_name; 50370Sstevel@tonic-gate break; 50380Sstevel@tonic-gate case SO_BROADCAST: 50390Sstevel@tonic-gate tcp->tcp_broadcast = onoff; 50400Sstevel@tonic-gate if (onoff) 50410Sstevel@tonic-gate so->so_options |= option_name; 50420Sstevel@tonic-gate else 50430Sstevel@tonic-gate so->so_options &= ~option_name; 50440Sstevel@tonic-gate break; 50450Sstevel@tonic-gate case SO_REUSEADDR: 50460Sstevel@tonic-gate tcp->tcp_reuseaddr = onoff; 50470Sstevel@tonic-gate if (onoff) 50480Sstevel@tonic-gate so->so_options |= option_name; 50490Sstevel@tonic-gate else 50500Sstevel@tonic-gate so->so_options &= ~option_name; 50510Sstevel@tonic-gate break; 50520Sstevel@tonic-gate case SO_OOBINLINE: 50530Sstevel@tonic-gate tcp->tcp_oobinline = onoff; 50540Sstevel@tonic-gate if (onoff) 50550Sstevel@tonic-gate so->so_options |= option_name; 50560Sstevel@tonic-gate else 50570Sstevel@tonic-gate so->so_options &= ~option_name; 50580Sstevel@tonic-gate break; 50590Sstevel@tonic-gate case SO_DGRAM_ERRIND: 50600Sstevel@tonic-gate tcp->tcp_dgram_errind = onoff; 50610Sstevel@tonic-gate if (onoff) 50620Sstevel@tonic-gate so->so_options |= option_name; 50630Sstevel@tonic-gate else 50640Sstevel@tonic-gate so->so_options &= ~option_name; 50650Sstevel@tonic-gate break; 50660Sstevel@tonic-gate } 50670Sstevel@tonic-gate break; 50680Sstevel@tonic-gate case IPPROTO_TCP: 50690Sstevel@tonic-gate switch (option_name) { 50700Sstevel@tonic-gate case TCP_NODELAY: 50710Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 50720Sstevel@tonic-gate error = EINVAL; 50730Sstevel@tonic-gate eprintsoline(so, error); 50740Sstevel@tonic-gate mutex_enter(&so->so_lock); 50750Sstevel@tonic-gate goto done2; 50760Sstevel@tonic-gate } 50770Sstevel@tonic-gate ASSERT(optval); 50780Sstevel@tonic-gate tcp->tcp_naglim = intvalue ? 1 : tcp->tcp_mss; 50790Sstevel@tonic-gate handled = B_TRUE; 50800Sstevel@tonic-gate break; 50810Sstevel@tonic-gate } 50820Sstevel@tonic-gate break; 50830Sstevel@tonic-gate default: 50840Sstevel@tonic-gate handled = B_FALSE; 50850Sstevel@tonic-gate break; 50860Sstevel@tonic-gate } 50870Sstevel@tonic-gate } 50880Sstevel@tonic-gate 50890Sstevel@tonic-gate if (handled) { 50900Sstevel@tonic-gate mutex_enter(&so->so_lock); 50910Sstevel@tonic-gate goto done2; 50920Sstevel@tonic-gate } 50930Sstevel@tonic-gate 50940Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 50950Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_NEGOTIATE; 50960Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen; 50970Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 50980Sstevel@tonic-gate 50990Sstevel@tonic-gate oh.level = level; 51000Sstevel@tonic-gate oh.name = option_name; 51010Sstevel@tonic-gate oh.len = optlen; 51020Sstevel@tonic-gate 51030Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 51040Sstevel@tonic-gate &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP); 51050Sstevel@tonic-gate /* Let option management work in the presence of data flow control */ 51060Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 51070Sstevel@tonic-gate MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 51080Sstevel@tonic-gate mp = NULL; 51090Sstevel@tonic-gate mutex_enter(&so->so_lock); 51100Sstevel@tonic-gate if (error) { 51110Sstevel@tonic-gate eprintsoline(so, error); 51120Sstevel@tonic-gate goto done; 51130Sstevel@tonic-gate } 51140Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 51150Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0); 51160Sstevel@tonic-gate if (error) { 51170Sstevel@tonic-gate eprintsoline(so, error); 51180Sstevel@tonic-gate goto done; 51190Sstevel@tonic-gate } 51200Sstevel@tonic-gate ASSERT(mp); 51210Sstevel@tonic-gate /* No need to verify T_optmgmt_ack */ 51220Sstevel@tonic-gate freemsg(mp); 51230Sstevel@tonic-gate done: 51240Sstevel@tonic-gate /* 51250Sstevel@tonic-gate * Check for SOL_SOCKET options and record their values. 51260Sstevel@tonic-gate * If we know about a SOL_SOCKET parameter and the transport 51270Sstevel@tonic-gate * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or 51280Sstevel@tonic-gate * EPROTO) we let the setsockopt succeed. 51290Sstevel@tonic-gate */ 51300Sstevel@tonic-gate if (level == SOL_SOCKET) { 51310Sstevel@tonic-gate /* Check parameters */ 51320Sstevel@tonic-gate switch (option_name) { 51330Sstevel@tonic-gate case SO_DEBUG: 51340Sstevel@tonic-gate case SO_REUSEADDR: 51350Sstevel@tonic-gate case SO_KEEPALIVE: 51360Sstevel@tonic-gate case SO_DONTROUTE: 51370Sstevel@tonic-gate case SO_BROADCAST: 51380Sstevel@tonic-gate case SO_USELOOPBACK: 51390Sstevel@tonic-gate case SO_OOBINLINE: 51400Sstevel@tonic-gate case SO_SNDBUF: 51410Sstevel@tonic-gate case SO_RCVBUF: 51420Sstevel@tonic-gate #ifdef notyet 51430Sstevel@tonic-gate case SO_SNDLOWAT: 51440Sstevel@tonic-gate case SO_RCVLOWAT: 51450Sstevel@tonic-gate case SO_SNDTIMEO: 51460Sstevel@tonic-gate case SO_RCVTIMEO: 51470Sstevel@tonic-gate #endif /* notyet */ 51480Sstevel@tonic-gate case SO_DGRAM_ERRIND: 51490Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 51500Sstevel@tonic-gate error = EINVAL; 51510Sstevel@tonic-gate eprintsoline(so, error); 51520Sstevel@tonic-gate goto done2; 51530Sstevel@tonic-gate } 51540Sstevel@tonic-gate ASSERT(optval); 51550Sstevel@tonic-gate handled = B_TRUE; 51560Sstevel@tonic-gate break; 51570Sstevel@tonic-gate case SO_LINGER: 51580Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (struct linger)) { 51590Sstevel@tonic-gate error = EINVAL; 51600Sstevel@tonic-gate eprintsoline(so, error); 51610Sstevel@tonic-gate goto done2; 51620Sstevel@tonic-gate } 51630Sstevel@tonic-gate ASSERT(optval); 51640Sstevel@tonic-gate handled = B_TRUE; 51650Sstevel@tonic-gate break; 51660Sstevel@tonic-gate } 51670Sstevel@tonic-gate 51680Sstevel@tonic-gate #define intvalue (*(int32_t *)optval) 51690Sstevel@tonic-gate 51700Sstevel@tonic-gate switch (option_name) { 51710Sstevel@tonic-gate case SO_TYPE: 51720Sstevel@tonic-gate case SO_ERROR: 51730Sstevel@tonic-gate case SO_ACCEPTCONN: 51740Sstevel@tonic-gate /* Can't be set */ 51750Sstevel@tonic-gate error = ENOPROTOOPT; 51760Sstevel@tonic-gate goto done2; 51770Sstevel@tonic-gate case SO_LINGER: { 51780Sstevel@tonic-gate struct linger *l = (struct linger *)optval; 51790Sstevel@tonic-gate 51800Sstevel@tonic-gate so->so_linger.l_linger = l->l_linger; 51810Sstevel@tonic-gate if (l->l_onoff) { 51820Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER; 51830Sstevel@tonic-gate so->so_options |= SO_LINGER; 51840Sstevel@tonic-gate } else { 51850Sstevel@tonic-gate so->so_linger.l_onoff = 0; 51860Sstevel@tonic-gate so->so_options &= ~SO_LINGER; 51870Sstevel@tonic-gate } 51880Sstevel@tonic-gate break; 51890Sstevel@tonic-gate } 51900Sstevel@tonic-gate 51910Sstevel@tonic-gate case SO_DEBUG: 51920Sstevel@tonic-gate #ifdef SOCK_TEST 51930Sstevel@tonic-gate if (intvalue & 2) 51940Sstevel@tonic-gate sock_test_timelimit = 10 * hz; 51950Sstevel@tonic-gate else 51960Sstevel@tonic-gate sock_test_timelimit = 0; 51970Sstevel@tonic-gate 51980Sstevel@tonic-gate if (intvalue & 4) 51990Sstevel@tonic-gate do_useracc = 0; 52000Sstevel@tonic-gate else 52010Sstevel@tonic-gate do_useracc = 1; 52020Sstevel@tonic-gate #endif /* SOCK_TEST */ 52030Sstevel@tonic-gate /* FALLTHRU */ 52040Sstevel@tonic-gate case SO_REUSEADDR: 52050Sstevel@tonic-gate case SO_KEEPALIVE: 52060Sstevel@tonic-gate case SO_DONTROUTE: 52070Sstevel@tonic-gate case SO_BROADCAST: 52080Sstevel@tonic-gate case SO_USELOOPBACK: 52090Sstevel@tonic-gate case SO_OOBINLINE: 52100Sstevel@tonic-gate case SO_DGRAM_ERRIND: 52110Sstevel@tonic-gate if (intvalue != 0) { 52120Sstevel@tonic-gate dprintso(so, 1, 52130Sstevel@tonic-gate ("sotpi_setsockopt: setting 0x%x\n", 52140Sstevel@tonic-gate option_name)); 52150Sstevel@tonic-gate so->so_options |= option_name; 52160Sstevel@tonic-gate } else { 52170Sstevel@tonic-gate dprintso(so, 1, 52180Sstevel@tonic-gate ("sotpi_setsockopt: clearing 0x%x\n", 52190Sstevel@tonic-gate option_name)); 52200Sstevel@tonic-gate so->so_options &= ~option_name; 52210Sstevel@tonic-gate } 52220Sstevel@tonic-gate break; 52230Sstevel@tonic-gate /* 52240Sstevel@tonic-gate * The following options are only returned by us when the 52250Sstevel@tonic-gate * T_SVR4_OPTMGMT_REQ fails. 52260Sstevel@tonic-gate * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs 52270Sstevel@tonic-gate * since the transport might adjust the value and not 52280Sstevel@tonic-gate * return exactly what was set by the application. 52290Sstevel@tonic-gate */ 52300Sstevel@tonic-gate case SO_SNDBUF: 52310Sstevel@tonic-gate so->so_sndbuf = intvalue; 52320Sstevel@tonic-gate break; 52330Sstevel@tonic-gate case SO_RCVBUF: 52340Sstevel@tonic-gate so->so_rcvbuf = intvalue; 52350Sstevel@tonic-gate break; 52360Sstevel@tonic-gate #ifdef notyet 52370Sstevel@tonic-gate /* 52380Sstevel@tonic-gate * We do not implement the semantics of these options 52390Sstevel@tonic-gate * thus we shouldn't implement the options either. 52400Sstevel@tonic-gate */ 52410Sstevel@tonic-gate case SO_SNDLOWAT: 52420Sstevel@tonic-gate so->so_sndlowat = intvalue; 52430Sstevel@tonic-gate break; 52440Sstevel@tonic-gate case SO_RCVLOWAT: 52450Sstevel@tonic-gate so->so_rcvlowat = intvalue; 52460Sstevel@tonic-gate break; 52470Sstevel@tonic-gate case SO_SNDTIMEO: 52480Sstevel@tonic-gate so->so_sndtimeo = intvalue; 52490Sstevel@tonic-gate break; 52500Sstevel@tonic-gate case SO_RCVTIMEO: 52510Sstevel@tonic-gate so->so_rcvtimeo = intvalue; 52520Sstevel@tonic-gate break; 52530Sstevel@tonic-gate #endif /* notyet */ 52540Sstevel@tonic-gate } 52550Sstevel@tonic-gate #undef intvalue 52560Sstevel@tonic-gate 52570Sstevel@tonic-gate if (error) { 52580Sstevel@tonic-gate if ((error == ENOPROTOOPT || error == EPROTO || 52590Sstevel@tonic-gate error == EINVAL) && handled) { 52600Sstevel@tonic-gate dprintso(so, 1, 52610Sstevel@tonic-gate ("setsockopt: ignoring error %d for 0x%x\n", 52620Sstevel@tonic-gate error, option_name)); 52630Sstevel@tonic-gate error = 0; 52640Sstevel@tonic-gate } 52650Sstevel@tonic-gate } 52660Sstevel@tonic-gate } 52670Sstevel@tonic-gate done2: 52680Sstevel@tonic-gate ret: 52690Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 52700Sstevel@tonic-gate mutex_exit(&so->so_lock); 52710Sstevel@tonic-gate return (error); 52720Sstevel@tonic-gate } 5273