10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51548Srshoaib * Common Development and Distribution License (the "License"). 61548Srshoaib * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211548Srshoaib 220Sstevel@tonic-gate /* 236707Sbrutus * Copyright 2008 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> 60741Smasputra #include <sys/sockio.h> 616707Sbrutus #include <sys/sodirect.h> 620Sstevel@tonic-gate #include <netinet/in.h> 630Sstevel@tonic-gate #include <sys/un.h> 640Sstevel@tonic-gate #include <sys/strsun.h> 650Sstevel@tonic-gate 660Sstevel@tonic-gate #include <sys/tiuser.h> 670Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 680Sstevel@tonic-gate #include <sys/tihdr.h> 690Sstevel@tonic-gate #include <sys/timod.h> /* TI_GETMYNAME, TI_GETPEERNAME */ 700Sstevel@tonic-gate 710Sstevel@tonic-gate #include <c2/audit.h> 720Sstevel@tonic-gate 730Sstevel@tonic-gate #include <inet/common.h> 740Sstevel@tonic-gate #include <inet/ip.h> 750Sstevel@tonic-gate #include <inet/ip6.h> 760Sstevel@tonic-gate #include <inet/tcp.h> 77741Smasputra #include <inet/udp_impl.h> 780Sstevel@tonic-gate 791974Sbrutus #include <sys/zone.h> 801974Sbrutus 810Sstevel@tonic-gate #include <fs/sockfs/nl7c.h> 821974Sbrutus #include <fs/sockfs/nl7curi.h> 830Sstevel@tonic-gate 84898Skais #include <inet/kssl/ksslapi.h> 85898Skais 860Sstevel@tonic-gate /* 870Sstevel@tonic-gate * Possible failures when memory can't be allocated. The documented behavior: 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * 5.5: 4.X: XNET: 900Sstevel@tonic-gate * accept: ENOMEM/ENOSR/EINTR - (EINTR) ENOMEM/ENOBUFS/ENOSR/ 910Sstevel@tonic-gate * EINTR 920Sstevel@tonic-gate * (4.X does not document EINTR but returns it) 930Sstevel@tonic-gate * bind: ENOSR - ENOBUFS/ENOSR 940Sstevel@tonic-gate * connect: EINTR EINTR ENOBUFS/ENOSR/EINTR 950Sstevel@tonic-gate * getpeername: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 960Sstevel@tonic-gate * getsockname: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 970Sstevel@tonic-gate * (4.X getpeername and getsockname do not fail in practice) 980Sstevel@tonic-gate * getsockopt: ENOMEM/ENOSR - ENOBUFS/ENOSR 990Sstevel@tonic-gate * listen: - - ENOBUFS 1000Sstevel@tonic-gate * recv: ENOMEM/ENOSR/EINTR EINTR ENOBUFS/ENOMEM/ENOSR/ 1010Sstevel@tonic-gate * EINTR 1020Sstevel@tonic-gate * send: ENOMEM/ENOSR/EINTR ENOBUFS/EINTR ENOBUFS/ENOMEM/ENOSR/ 1030Sstevel@tonic-gate * EINTR 1040Sstevel@tonic-gate * setsockopt: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 1050Sstevel@tonic-gate * shutdown: ENOMEM/ENOSR - ENOBUFS/ENOSR 1060Sstevel@tonic-gate * socket: ENOMEM/ENOSR ENOBUFS ENOBUFS/ENOMEM/ENOSR 1070Sstevel@tonic-gate * socketpair: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 1080Sstevel@tonic-gate * 1090Sstevel@tonic-gate * Resolution. When allocation fails: 1100Sstevel@tonic-gate * recv: return EINTR 1110Sstevel@tonic-gate * send: return EINTR 1120Sstevel@tonic-gate * connect, accept: EINTR 1130Sstevel@tonic-gate * bind, listen, shutdown (unbind, unix_close, disconnect): sleep 1140Sstevel@tonic-gate * socket, socketpair: ENOBUFS 1150Sstevel@tonic-gate * getpeername, getsockname: sleep 1160Sstevel@tonic-gate * getsockopt, setsockopt: sleep 1170Sstevel@tonic-gate */ 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate #ifdef SOCK_TEST 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * Variables that make sockfs do something other than the standard TPI 1220Sstevel@tonic-gate * for the AF_INET transports. 1230Sstevel@tonic-gate * 1240Sstevel@tonic-gate * solisten_tpi_tcp: 1250Sstevel@tonic-gate * TCP can handle a O_T_BIND_REQ with an increased backlog even though 1260Sstevel@tonic-gate * the transport is already bound. This is needed to avoid loosing the 1270Sstevel@tonic-gate * port number should listen() do a T_UNBIND_REQ followed by a 1280Sstevel@tonic-gate * O_T_BIND_REQ. 1290Sstevel@tonic-gate * 1300Sstevel@tonic-gate * soconnect_tpi_udp: 1310Sstevel@tonic-gate * UDP and ICMP can handle a T_CONN_REQ. 1320Sstevel@tonic-gate * This is needed to make the sequence of connect(), getsockname() 1330Sstevel@tonic-gate * return the local IP address used to send packets to the connected to 1340Sstevel@tonic-gate * destination. 1350Sstevel@tonic-gate * 1360Sstevel@tonic-gate * soconnect_tpi_tcp: 1370Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ. 1380Sstevel@tonic-gate * Set this to non-zero to send TPI conformant messages to TCP in this 1390Sstevel@tonic-gate * respect. This is a performance optimization. 1400Sstevel@tonic-gate * 1410Sstevel@tonic-gate * soaccept_tpi_tcp: 1420Sstevel@tonic-gate * TCP can handle a T_CONN_REQ without the acceptor being bound. 1430Sstevel@tonic-gate * This is a performance optimization that has been picked up in XTI. 1440Sstevel@tonic-gate * 1450Sstevel@tonic-gate * soaccept_tpi_multioptions: 1460Sstevel@tonic-gate * When inheriting SOL_SOCKET options from the listener to the accepting 1470Sstevel@tonic-gate * socket send them as a single message for AF_INET{,6}. 1480Sstevel@tonic-gate */ 1490Sstevel@tonic-gate int solisten_tpi_tcp = 0; 1500Sstevel@tonic-gate int soconnect_tpi_udp = 0; 1510Sstevel@tonic-gate int soconnect_tpi_tcp = 0; 1520Sstevel@tonic-gate int soaccept_tpi_tcp = 0; 1530Sstevel@tonic-gate int soaccept_tpi_multioptions = 1; 1540Sstevel@tonic-gate #else /* SOCK_TEST */ 1550Sstevel@tonic-gate #define soconnect_tpi_tcp 0 1560Sstevel@tonic-gate #define soconnect_tpi_udp 0 1570Sstevel@tonic-gate #define solisten_tpi_tcp 0 1580Sstevel@tonic-gate #define soaccept_tpi_tcp 0 1590Sstevel@tonic-gate #define soaccept_tpi_multioptions 1 1600Sstevel@tonic-gate #endif /* SOCK_TEST */ 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate #ifdef SOCK_TEST 1630Sstevel@tonic-gate extern int do_useracc; 1640Sstevel@tonic-gate extern clock_t sock_test_timelimit; 1650Sstevel@tonic-gate #endif /* SOCK_TEST */ 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * Some X/Open added checks might have to be backed out to keep SunOS 4.X 1690Sstevel@tonic-gate * applications working. Turn on this flag to disable these checks. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate int xnet_skip_checks = 0; 1720Sstevel@tonic-gate int xnet_check_print = 0; 1730Sstevel@tonic-gate int xnet_truncate_print = 0; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate extern void sigintr(k_sigset_t *, int); 1760Sstevel@tonic-gate extern void sigunintr(k_sigset_t *); 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate extern void *nl7c_lookup_addr(void *, t_uscalar_t); 1790Sstevel@tonic-gate extern void *nl7c_add_addr(void *, t_uscalar_t); 1801974Sbrutus extern void nl7c_listener_addr(void *, struct sonode *); 1810Sstevel@tonic-gate 182898Skais /* Sockets acting as an in-kernel SSL proxy */ 183898Skais extern mblk_t *strsock_kssl_input(vnode_t *, mblk_t *, strwakeup_t *, 184898Skais strsigset_t *, strsigset_t *, strpollset_t *); 185898Skais extern mblk_t *strsock_kssl_output(vnode_t *, mblk_t *, strwakeup_t *, 186898Skais strsigset_t *, strsigset_t *, strpollset_t *); 187898Skais 1880Sstevel@tonic-gate static int sotpi_unbind(struct sonode *, int); 1890Sstevel@tonic-gate 1906707Sbrutus extern int sodput(sodirect_t *, mblk_t *); 1916707Sbrutus extern void sodwakeup(sodirect_t *); 1926707Sbrutus 1930Sstevel@tonic-gate /* TPI sockfs sonode operations */ 1940Sstevel@tonic-gate static int sotpi_accept(struct sonode *, int, struct sonode **); 1950Sstevel@tonic-gate static int sotpi_bind(struct sonode *, struct sockaddr *, socklen_t, 1960Sstevel@tonic-gate int); 1970Sstevel@tonic-gate static int sotpi_connect(struct sonode *, const struct sockaddr *, 1980Sstevel@tonic-gate socklen_t, int, int); 1990Sstevel@tonic-gate static int sotpi_listen(struct sonode *, int); 2000Sstevel@tonic-gate static int sotpi_sendmsg(struct sonode *, struct nmsghdr *, 2010Sstevel@tonic-gate struct uio *); 2020Sstevel@tonic-gate static int sotpi_shutdown(struct sonode *, int); 2030Sstevel@tonic-gate static int sotpi_getsockname(struct sonode *); 204741Smasputra static int sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t, 205741Smasputra struct uio *, void *, t_uscalar_t, int); 206741Smasputra static int sodgram_direct(struct sonode *, struct sockaddr *, 207741Smasputra socklen_t, struct uio *, int); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate sonodeops_t sotpi_sonodeops = { 2100Sstevel@tonic-gate sotpi_accept, /* sop_accept */ 2110Sstevel@tonic-gate sotpi_bind, /* sop_bind */ 2120Sstevel@tonic-gate sotpi_listen, /* sop_listen */ 2130Sstevel@tonic-gate sotpi_connect, /* sop_connect */ 2140Sstevel@tonic-gate sotpi_recvmsg, /* sop_recvmsg */ 2150Sstevel@tonic-gate sotpi_sendmsg, /* sop_sendmsg */ 2160Sstevel@tonic-gate sotpi_getpeername, /* sop_getpeername */ 2170Sstevel@tonic-gate sotpi_getsockname, /* sop_getsockname */ 2180Sstevel@tonic-gate sotpi_shutdown, /* sop_shutdown */ 2190Sstevel@tonic-gate sotpi_getsockopt, /* sop_getsockopt */ 2200Sstevel@tonic-gate sotpi_setsockopt /* sop_setsockopt */ 2210Sstevel@tonic-gate }; 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate /* 2240Sstevel@tonic-gate * Common create code for socket and accept. If tso is set the values 2250Sstevel@tonic-gate * from that node is used instead of issuing a T_INFO_REQ. 2260Sstevel@tonic-gate * 2270Sstevel@tonic-gate * Assumes that the caller has a VN_HOLD on accessvp. 2280Sstevel@tonic-gate * The VN_RELE will occur either when sotpi_create() fails or when 2290Sstevel@tonic-gate * the returned sonode is freed. 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate struct sonode * 2320Sstevel@tonic-gate sotpi_create(vnode_t *accessvp, int domain, int type, int protocol, int version, 2330Sstevel@tonic-gate struct sonode *tso, int *errorp) 2340Sstevel@tonic-gate { 2350Sstevel@tonic-gate struct sonode *so; 2360Sstevel@tonic-gate vnode_t *vp; 2370Sstevel@tonic-gate int flags, error; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate ASSERT(accessvp != NULL); 2400Sstevel@tonic-gate vp = makesockvp(accessvp, domain, type, protocol); 2410Sstevel@tonic-gate ASSERT(vp != NULL); 2420Sstevel@tonic-gate so = VTOSO(vp); 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate flags = FREAD|FWRITE; 245741Smasputra 246741Smasputra if ((type == SOCK_STREAM || type == SOCK_DGRAM) && 247741Smasputra (domain == AF_INET || domain == AF_INET6) && 248741Smasputra (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP || 249741Smasputra protocol == IPPROTO_IP)) { 250741Smasputra /* Tell tcp or udp that it's talking to sockets */ 251741Smasputra flags |= SO_SOCKSTR; 252741Smasputra 253741Smasputra /* 254741Smasputra * Here we indicate to socktpi_open() our attempt to 255741Smasputra * make direct calls between sockfs and transport. 256741Smasputra * The final decision is left to socktpi_open(). 257741Smasputra */ 258741Smasputra so->so_state |= SS_DIRECT; 259741Smasputra 260741Smasputra ASSERT(so->so_type != SOCK_DGRAM || tso == NULL); 261741Smasputra if (so->so_type == SOCK_STREAM && tso != NULL) { 262741Smasputra if (tso->so_state & SS_DIRECT) { 263741Smasputra /* 264741Smasputra * Inherit SS_DIRECT from listener and pass 265741Smasputra * SO_ACCEPTOR open flag to tcp, indicating 266741Smasputra * that this is an accept fast-path instance. 267741Smasputra */ 268741Smasputra flags |= SO_ACCEPTOR; 269741Smasputra } else { 270741Smasputra /* 271741Smasputra * SS_DIRECT is not set on listener, meaning 272741Smasputra * that the listener has been converted from 273741Smasputra * a socket to a stream. Ensure that the 274741Smasputra * acceptor inherits these settings. 275741Smasputra */ 276741Smasputra so->so_state &= ~SS_DIRECT; 277741Smasputra flags &= ~SO_SOCKSTR; 278741Smasputra } 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate /* 2830Sstevel@tonic-gate * Tell local transport that it is talking to sockets. 2840Sstevel@tonic-gate */ 2850Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 2860Sstevel@tonic-gate flags |= SO_SOCKSTR; 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2891092Skais /* Initialize the kernel SSL proxy fields */ 2901092Skais so->so_kssl_type = KSSL_NO_PROXY; 2911092Skais so->so_kssl_ent = NULL; 2921092Skais so->so_kssl_ctx = NULL; 2931092Skais 2945331Samw if (error = socktpi_open(&vp, flags, CRED(), NULL)) { 2950Sstevel@tonic-gate VN_RELE(vp); 2960Sstevel@tonic-gate *errorp = error; 2970Sstevel@tonic-gate return (NULL); 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate if (error = so_strinit(so, tso)) { 3015331Samw (void) VOP_CLOSE(vp, 0, 1, 0, CRED(), NULL); 3020Sstevel@tonic-gate VN_RELE(vp); 3030Sstevel@tonic-gate *errorp = error; 3040Sstevel@tonic-gate return (NULL); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate if (version == SOV_DEFAULT) 3080Sstevel@tonic-gate version = so_default_version; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate so->so_version = (short)version; 311898Skais 3120Sstevel@tonic-gate return (so); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * Bind the socket to an unspecified address in sockfs only. 3170Sstevel@tonic-gate * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't 3180Sstevel@tonic-gate * required in all cases. 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate static void 3210Sstevel@tonic-gate so_automatic_bind(struct sonode *so) 3220Sstevel@tonic-gate { 3230Sstevel@tonic-gate ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 3260Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 3270Sstevel@tonic-gate ASSERT(so->so_unbind_mp); 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 3300Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 3310Sstevel@tonic-gate so->so_laddr_sa->sa_family = so->so_family; 3320Sstevel@tonic-gate so->so_state |= SS_ISBOUND; 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* 3370Sstevel@tonic-gate * bind the socket. 3380Sstevel@tonic-gate * 3390Sstevel@tonic-gate * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2 3400Sstevel@tonic-gate * are passed in we allow rebinding. Note that for backwards compatibility 3410Sstevel@tonic-gate * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind. 3420Sstevel@tonic-gate * Thus the rebinding code is currently not executed. 3430Sstevel@tonic-gate * 3440Sstevel@tonic-gate * The constraints for rebinding are: 3450Sstevel@tonic-gate * - it is a SOCK_DGRAM, or 3460Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 3470Sstevel@tonic-gate * and no listen() has been done. 3480Sstevel@tonic-gate * This rebinding code was added based on some language in the XNET book 3490Sstevel@tonic-gate * about not returning EINVAL it the protocol allows rebinding. However, 3500Sstevel@tonic-gate * this language is not present in the Posix socket draft. Thus maybe the 3510Sstevel@tonic-gate * rebinding logic should be deleted from the source. 3520Sstevel@tonic-gate * 3530Sstevel@tonic-gate * A null "name" can be used to unbind the socket if: 3540Sstevel@tonic-gate * - it is a SOCK_DGRAM, or 3550Sstevel@tonic-gate * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 3560Sstevel@tonic-gate * and no listen() has been done. 3570Sstevel@tonic-gate */ 3580Sstevel@tonic-gate static int 3590Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name, 3600Sstevel@tonic-gate socklen_t namelen, int backlog, int flags) 3610Sstevel@tonic-gate { 3620Sstevel@tonic-gate struct T_bind_req bind_req; 3630Sstevel@tonic-gate struct T_bind_ack *bind_ack; 3640Sstevel@tonic-gate int error = 0; 3650Sstevel@tonic-gate mblk_t *mp; 3660Sstevel@tonic-gate void *addr; 3670Sstevel@tonic-gate t_uscalar_t addrlen; 3680Sstevel@tonic-gate int unbind_on_err = 1; 3690Sstevel@tonic-gate boolean_t clear_acceptconn_on_err = B_FALSE; 3700Sstevel@tonic-gate boolean_t restore_backlog_on_err = B_FALSE; 3710Sstevel@tonic-gate int save_so_backlog; 3720Sstevel@tonic-gate t_scalar_t PRIM_type = O_T_BIND_REQ; 3730Sstevel@tonic-gate boolean_t tcp_udp_xport; 3740Sstevel@tonic-gate void *nl7c = NULL; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n", 377*7240Srh87107 (void *)so, (void *)name, namelen, backlog, flags, 3785240Snordmark pr_state(so->so_state, so->so_mode))); 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM; 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) { 3830Sstevel@tonic-gate mutex_enter(&so->so_lock); 3840Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 3850Sstevel@tonic-gate } else { 3860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 3870Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate /* 3910Sstevel@tonic-gate * Make sure that there is a preallocated unbind_req message 3920Sstevel@tonic-gate * before binding. This message allocated when the socket is 3930Sstevel@tonic-gate * created but it might be have been consumed. 3940Sstevel@tonic-gate */ 3950Sstevel@tonic-gate if (so->so_unbind_mp == NULL) { 3960Sstevel@tonic-gate dprintso(so, 1, ("sobind: allocating unbind_req\n")); 3970Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 3980Sstevel@tonic-gate so->so_unbind_mp = 3990Sstevel@tonic-gate soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate if (flags & _SOBIND_REBIND) { 4030Sstevel@tonic-gate /* 4040Sstevel@tonic-gate * Called from solisten after doing an sotpi_unbind() or 4050Sstevel@tonic-gate * potentially without the unbind (latter for AF_INET{,6}). 4060Sstevel@tonic-gate */ 4070Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0); 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 4100Sstevel@tonic-gate ASSERT(so->so_ux_bound_vp); 4110Sstevel@tonic-gate addr = &so->so_ux_laddr; 4120Sstevel@tonic-gate addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 4135240Snordmark dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, " 4145240Snordmark "addr 0x%p, vp %p\n", 4150Sstevel@tonic-gate addrlen, 416*7240Srh87107 (void *)((struct so_ux_addr *)addr)->soua_vp, 417*7240Srh87107 (void *)so->so_ux_bound_vp)); 4180Sstevel@tonic-gate } else { 4190Sstevel@tonic-gate addr = so->so_laddr_sa; 4200Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_laddr_len; 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate } else if (flags & _SOBIND_UNSPEC) { 4230Sstevel@tonic-gate ASSERT(name == NULL && namelen == 0); 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate /* 4260Sstevel@tonic-gate * The caller checked SS_ISBOUND but not necessarily 4270Sstevel@tonic-gate * under so_lock 4280Sstevel@tonic-gate */ 4290Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) { 4300Sstevel@tonic-gate /* No error */ 4310Sstevel@tonic-gate goto done; 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate /* Set an initial local address */ 4350Sstevel@tonic-gate switch (so->so_family) { 4360Sstevel@tonic-gate case AF_UNIX: 4370Sstevel@tonic-gate /* 4380Sstevel@tonic-gate * Use an address with same size as struct sockaddr 4390Sstevel@tonic-gate * just like BSD. 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate so->so_laddr_len = 4425240Snordmark (socklen_t)sizeof (struct sockaddr); 4430Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 4440Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 4450Sstevel@tonic-gate so->so_laddr_sa->sa_family = so->so_family; 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate /* 4480Sstevel@tonic-gate * Pass down an address with the implicit bind 4490Sstevel@tonic-gate * magic number and the rest all zeros. 4500Sstevel@tonic-gate * The transport will return a unique address. 4510Sstevel@tonic-gate */ 4520Sstevel@tonic-gate so->so_ux_laddr.soua_vp = NULL; 4530Sstevel@tonic-gate so->so_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT; 4540Sstevel@tonic-gate addr = &so->so_ux_laddr; 4550Sstevel@tonic-gate addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 4560Sstevel@tonic-gate break; 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate case AF_INET: 4590Sstevel@tonic-gate case AF_INET6: 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address. 4620Sstevel@tonic-gate * Set the address in sockfs to have the sa_family. 4630Sstevel@tonic-gate */ 4640Sstevel@tonic-gate so->so_laddr_len = (so->so_family == AF_INET) ? 4650Sstevel@tonic-gate (socklen_t)sizeof (sin_t) : 4660Sstevel@tonic-gate (socklen_t)sizeof (sin6_t); 4670Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 4680Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 4690Sstevel@tonic-gate so->so_laddr_sa->sa_family = so->so_family; 4700Sstevel@tonic-gate addr = NULL; 4710Sstevel@tonic-gate addrlen = 0; 4720Sstevel@tonic-gate break; 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate default: 4750Sstevel@tonic-gate /* 4760Sstevel@tonic-gate * An unspecified bind in TPI has a NULL address. 4770Sstevel@tonic-gate * Set the address in sockfs to be zero length. 4780Sstevel@tonic-gate * 4790Sstevel@tonic-gate * Can not assume there is a sa_family for all 4800Sstevel@tonic-gate * protocol families. For example, AF_X25 does not 4810Sstevel@tonic-gate * have a family field. 4820Sstevel@tonic-gate */ 4831548Srshoaib bzero(so->so_laddr_sa, so->so_laddr_len); 4840Sstevel@tonic-gate so->so_laddr_len = 0; /* XXX correct? */ 4850Sstevel@tonic-gate addr = NULL; 4860Sstevel@tonic-gate addrlen = 0; 4870Sstevel@tonic-gate break; 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate } else { 4910Sstevel@tonic-gate if (so->so_state & SS_ISBOUND) { 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * If it is ok to rebind the socket, first unbind 4940Sstevel@tonic-gate * with the transport. A rebind to the NULL address 4950Sstevel@tonic-gate * is interpreted as an unbind. 4960Sstevel@tonic-gate * Note that a bind to NULL in BSD does unbind the 4970Sstevel@tonic-gate * socket but it fails with EINVAL. 4980Sstevel@tonic-gate * Note that regular sockets set SOV_SOCKBSD i.e. 4990Sstevel@tonic-gate * _SOBIND_SOCKBSD gets set here hence no type of 5000Sstevel@tonic-gate * socket does currently allow rebinding. 5010Sstevel@tonic-gate * 5020Sstevel@tonic-gate * If the name is NULL just do an unbind. 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) && 5050Sstevel@tonic-gate name != NULL) { 5060Sstevel@tonic-gate error = EINVAL; 5070Sstevel@tonic-gate unbind_on_err = 0; 5080Sstevel@tonic-gate eprintsoline(so, error); 5090Sstevel@tonic-gate goto done; 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate if ((so->so_mode & SM_CONNREQUIRED) && 5120Sstevel@tonic-gate (so->so_state & SS_CANTREBIND)) { 5130Sstevel@tonic-gate error = EINVAL; 5140Sstevel@tonic-gate unbind_on_err = 0; 5150Sstevel@tonic-gate eprintsoline(so, error); 5160Sstevel@tonic-gate goto done; 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate error = sotpi_unbind(so, 0); 5190Sstevel@tonic-gate if (error) { 5200Sstevel@tonic-gate eprintsoline(so, error); 5210Sstevel@tonic-gate goto done; 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 5240Sstevel@tonic-gate if (name == NULL) { 5250Sstevel@tonic-gate so->so_state &= 5265240Snordmark ~(SS_ISCONNECTED|SS_ISCONNECTING); 5270Sstevel@tonic-gate goto done; 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate /* X/Open requires this check */ 5310Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 5320Sstevel@tonic-gate if (xnet_check_print) { 5330Sstevel@tonic-gate printf("sockfs: X/Open bind state check " 5340Sstevel@tonic-gate "caused EINVAL\n"); 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate error = EINVAL; 5370Sstevel@tonic-gate goto done; 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate switch (so->so_family) { 5410Sstevel@tonic-gate case AF_UNIX: 5420Sstevel@tonic-gate /* 5430Sstevel@tonic-gate * All AF_UNIX addresses are nul terminated 5440Sstevel@tonic-gate * when copied (copyin_name) in so the minimum 5450Sstevel@tonic-gate * length is 3 bytes. 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate if (name == NULL || 5480Sstevel@tonic-gate (ssize_t)namelen <= sizeof (short) + 1) { 5490Sstevel@tonic-gate error = EISDIR; 5500Sstevel@tonic-gate eprintsoline(so, error); 5510Sstevel@tonic-gate goto done; 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * Verify so_family matches the bound family. 5550Sstevel@tonic-gate * BSD does not check this for AF_UNIX resulting 5560Sstevel@tonic-gate * in funny mknods. 5570Sstevel@tonic-gate */ 5580Sstevel@tonic-gate if (name->sa_family != so->so_family) { 5590Sstevel@tonic-gate error = EAFNOSUPPORT; 5600Sstevel@tonic-gate goto done; 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate break; 5630Sstevel@tonic-gate case AF_INET: 5640Sstevel@tonic-gate if (name == NULL) { 5650Sstevel@tonic-gate error = EINVAL; 5660Sstevel@tonic-gate eprintsoline(so, error); 5670Sstevel@tonic-gate goto done; 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin_t)) { 5700Sstevel@tonic-gate error = name->sa_family != so->so_family ? 5710Sstevel@tonic-gate EAFNOSUPPORT : EINVAL; 5720Sstevel@tonic-gate eprintsoline(so, error); 5730Sstevel@tonic-gate goto done; 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate if ((flags & _SOBIND_XPG4_2) && 5760Sstevel@tonic-gate (name->sa_family != so->so_family)) { 5770Sstevel@tonic-gate /* 5780Sstevel@tonic-gate * This check has to be made for X/Open 5790Sstevel@tonic-gate * sockets however application failures have 5800Sstevel@tonic-gate * been observed when it is applied to 5810Sstevel@tonic-gate * all sockets. 5820Sstevel@tonic-gate */ 5830Sstevel@tonic-gate error = EAFNOSUPPORT; 5840Sstevel@tonic-gate eprintsoline(so, error); 5850Sstevel@tonic-gate goto done; 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate /* 5880Sstevel@tonic-gate * Force a zero sa_family to match so_family. 5890Sstevel@tonic-gate * 5900Sstevel@tonic-gate * Some programs like inetd(1M) don't set the 5910Sstevel@tonic-gate * family field. Other programs leave 5920Sstevel@tonic-gate * sin_family set to garbage - SunOS 4.X does 5930Sstevel@tonic-gate * not check the family field on a bind. 5940Sstevel@tonic-gate * We use the family field that 5950Sstevel@tonic-gate * was passed in to the socket() call. 5960Sstevel@tonic-gate */ 5970Sstevel@tonic-gate name->sa_family = so->so_family; 5980Sstevel@tonic-gate break; 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate case AF_INET6: { 6010Sstevel@tonic-gate #ifdef DEBUG 6020Sstevel@tonic-gate sin6_t *sin6 = (sin6_t *)name; 6030Sstevel@tonic-gate #endif /* DEBUG */ 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate if (name == NULL) { 6060Sstevel@tonic-gate error = EINVAL; 6070Sstevel@tonic-gate eprintsoline(so, error); 6080Sstevel@tonic-gate goto done; 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate if ((size_t)namelen != sizeof (sin6_t)) { 6110Sstevel@tonic-gate error = name->sa_family != so->so_family ? 6120Sstevel@tonic-gate EAFNOSUPPORT : EINVAL; 6130Sstevel@tonic-gate eprintsoline(so, error); 6140Sstevel@tonic-gate goto done; 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate if (name->sa_family != so->so_family) { 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * With IPv6 we require the family to match 6190Sstevel@tonic-gate * unlike in IPv4. 6200Sstevel@tonic-gate */ 6210Sstevel@tonic-gate error = EAFNOSUPPORT; 6220Sstevel@tonic-gate eprintsoline(so, error); 6230Sstevel@tonic-gate goto done; 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate #ifdef DEBUG 6260Sstevel@tonic-gate /* 6270Sstevel@tonic-gate * Verify that apps don't forget to clear 6280Sstevel@tonic-gate * sin6_scope_id etc 6290Sstevel@tonic-gate */ 6300Sstevel@tonic-gate if (sin6->sin6_scope_id != 0 && 6310Sstevel@tonic-gate !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 6321548Srshoaib zcmn_err(getzoneid(), CE_WARN, 6330Sstevel@tonic-gate "bind with uninitialized sin6_scope_id " 6340Sstevel@tonic-gate "(%d) on socket. Pid = %d\n", 6350Sstevel@tonic-gate (int)sin6->sin6_scope_id, 6360Sstevel@tonic-gate (int)curproc->p_pid); 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate if (sin6->__sin6_src_id != 0) { 6391548Srshoaib zcmn_err(getzoneid(), CE_WARN, 6400Sstevel@tonic-gate "bind with uninitialized __sin6_src_id " 6410Sstevel@tonic-gate "(%d) on socket. Pid = %d\n", 6420Sstevel@tonic-gate (int)sin6->__sin6_src_id, 6430Sstevel@tonic-gate (int)curproc->p_pid); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate #endif /* DEBUG */ 6460Sstevel@tonic-gate break; 6470Sstevel@tonic-gate } 6480Sstevel@tonic-gate default: 6490Sstevel@tonic-gate /* 6500Sstevel@tonic-gate * Don't do any length or sa_family check to allow 6510Sstevel@tonic-gate * non-sockaddr style addresses. 6520Sstevel@tonic-gate */ 6530Sstevel@tonic-gate if (name == NULL) { 6540Sstevel@tonic-gate error = EINVAL; 6550Sstevel@tonic-gate eprintsoline(so, error); 6560Sstevel@tonic-gate goto done; 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate break; 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate if (namelen > (t_uscalar_t)so->so_laddr_maxlen) { 6620Sstevel@tonic-gate error = ENAMETOOLONG; 6630Sstevel@tonic-gate eprintsoline(so, error); 6640Sstevel@tonic-gate goto done; 6650Sstevel@tonic-gate } 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * Save local address. 6680Sstevel@tonic-gate */ 6690Sstevel@tonic-gate so->so_laddr_len = (socklen_t)namelen; 6700Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 6710Sstevel@tonic-gate bcopy(name, so->so_laddr_sa, namelen); 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate addr = so->so_laddr_sa; 6740Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_laddr_len; 6750Sstevel@tonic-gate switch (so->so_family) { 6760Sstevel@tonic-gate case AF_INET6: 6770Sstevel@tonic-gate case AF_INET: 6780Sstevel@tonic-gate break; 6790Sstevel@tonic-gate case AF_UNIX: { 6800Sstevel@tonic-gate struct sockaddr_un *soun = 6815240Snordmark (struct sockaddr_un *)so->so_laddr_sa; 6820Sstevel@tonic-gate struct vnode *vp; 6830Sstevel@tonic-gate struct vattr vattr; 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate ASSERT(so->so_ux_bound_vp == NULL); 6860Sstevel@tonic-gate /* 6870Sstevel@tonic-gate * Create vnode for the specified path name. 6880Sstevel@tonic-gate * Keep vnode held with a reference in so_ux_bound_vp. 6890Sstevel@tonic-gate * Use the vnode pointer as the address used in the 6900Sstevel@tonic-gate * bind with the transport. 6910Sstevel@tonic-gate * 6920Sstevel@tonic-gate * Use the same mode as in BSD. In particular this does 6930Sstevel@tonic-gate * not observe the umask. 6940Sstevel@tonic-gate */ 6950Sstevel@tonic-gate /* MAXPATHLEN + soun_family + nul termination */ 6960Sstevel@tonic-gate if (so->so_laddr_len > 6970Sstevel@tonic-gate (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) { 6980Sstevel@tonic-gate error = ENAMETOOLONG; 6990Sstevel@tonic-gate eprintsoline(so, error); 7000Sstevel@tonic-gate goto done; 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate vattr.va_type = VSOCK; 7033446Smrj vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask; 7040Sstevel@tonic-gate vattr.va_mask = AT_TYPE|AT_MODE; 7050Sstevel@tonic-gate /* NOTE: holding so_lock */ 7060Sstevel@tonic-gate error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr, 7075240Snordmark EXCL, 0, &vp, CRMKNOD, 0, 0); 7080Sstevel@tonic-gate if (error) { 7090Sstevel@tonic-gate if (error == EEXIST) 7100Sstevel@tonic-gate error = EADDRINUSE; 7110Sstevel@tonic-gate eprintsoline(so, error); 7120Sstevel@tonic-gate goto done; 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * Establish pointer from the underlying filesystem 7160Sstevel@tonic-gate * vnode to the socket node. 7170Sstevel@tonic-gate * so_ux_bound_vp and v_stream->sd_vnode form the 7180Sstevel@tonic-gate * cross-linkage between the underlying filesystem 7190Sstevel@tonic-gate * node and the socket node. 7200Sstevel@tonic-gate */ 7210Sstevel@tonic-gate ASSERT(SOTOV(so)->v_stream); 7220Sstevel@tonic-gate mutex_enter(&vp->v_lock); 7230Sstevel@tonic-gate vp->v_stream = SOTOV(so)->v_stream; 7240Sstevel@tonic-gate so->so_ux_bound_vp = vp; 7250Sstevel@tonic-gate mutex_exit(&vp->v_lock); 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate /* 7280Sstevel@tonic-gate * Use the vnode pointer value as a unique address 7290Sstevel@tonic-gate * (together with the magic number to avoid conflicts 7300Sstevel@tonic-gate * with implicit binds) in the transport provider. 7310Sstevel@tonic-gate */ 7320Sstevel@tonic-gate so->so_ux_laddr.soua_vp = (void *)so->so_ux_bound_vp; 7330Sstevel@tonic-gate so->so_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT; 7340Sstevel@tonic-gate addr = &so->so_ux_laddr; 7350Sstevel@tonic-gate addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 7360Sstevel@tonic-gate dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n", 7370Sstevel@tonic-gate addrlen, 7380Sstevel@tonic-gate ((struct so_ux_addr *)addr)->soua_vp)); 7390Sstevel@tonic-gate break; 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate } /* end switch (so->so_family) */ 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since 7460Sstevel@tonic-gate * the transport can start passing up T_CONN_IND messages 7470Sstevel@tonic-gate * as soon as it receives the bind req and strsock_proto() 7480Sstevel@tonic-gate * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs. 7490Sstevel@tonic-gate */ 7500Sstevel@tonic-gate if (flags & _SOBIND_LISTEN) { 7510Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0) 7520Sstevel@tonic-gate clear_acceptconn_on_err = B_TRUE; 7530Sstevel@tonic-gate save_so_backlog = so->so_backlog; 7540Sstevel@tonic-gate restore_backlog_on_err = B_TRUE; 7550Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN; 7560Sstevel@tonic-gate so->so_backlog = backlog; 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate /* 7600Sstevel@tonic-gate * If NL7C addr(s) have been configured check for addr/port match, 7610Sstevel@tonic-gate * or if an implicit NL7C socket via AF_NCA mark socket as NL7C. 7620Sstevel@tonic-gate * 7630Sstevel@tonic-gate * NL7C supports the TCP transport only so check AF_INET and AF_INET6 7640Sstevel@tonic-gate * family sockets only. If match mark as such. 7650Sstevel@tonic-gate */ 7661974Sbrutus if (nl7c_enabled && ((addr != NULL && 7670Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6) && 7680Sstevel@tonic-gate (nl7c = nl7c_lookup_addr(addr, addrlen))) || 7691974Sbrutus so->so_nl7c_flags == NL7C_AF_NCA)) { 7700Sstevel@tonic-gate /* 7710Sstevel@tonic-gate * NL7C is not supported in non-global zones, 7720Sstevel@tonic-gate * we enforce this restriction here. 7730Sstevel@tonic-gate */ 7740Sstevel@tonic-gate if (so->so_zoneid == GLOBAL_ZONEID) { 7750Sstevel@tonic-gate /* An NL7C socket, mark it */ 7760Sstevel@tonic-gate so->so_nl7c_flags |= NL7C_ENABLED; 7771974Sbrutus if (nl7c == NULL) { 7781974Sbrutus /* 7791974Sbrutus * Was an AF_NCA bind() so add it to the 7801974Sbrutus * addr list for reporting purposes. 7811974Sbrutus */ 7821974Sbrutus nl7c = nl7c_add_addr(addr, addrlen); 7831974Sbrutus } 7840Sstevel@tonic-gate } else 7850Sstevel@tonic-gate nl7c = NULL; 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate /* 7880Sstevel@tonic-gate * We send a T_BIND_REQ for TCP/UDP since we know it supports it, 7890Sstevel@tonic-gate * for other transports we will send in a O_T_BIND_REQ. 7900Sstevel@tonic-gate */ 7910Sstevel@tonic-gate if (tcp_udp_xport && 7920Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6)) 7930Sstevel@tonic-gate PRIM_type = T_BIND_REQ; 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate bind_req.PRIM_type = PRIM_type; 7960Sstevel@tonic-gate bind_req.ADDR_length = addrlen; 7970Sstevel@tonic-gate bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req); 7980Sstevel@tonic-gate bind_req.CONIND_number = backlog; 7990Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 8000Sstevel@tonic-gate mp = soallocproto2(&bind_req, sizeof (bind_req), 8015240Snordmark addr, addrlen, 0, _ALLOC_SLEEP); 8020Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 803898Skais 8040Sstevel@tonic-gate /* Done using so_laddr_sa - can drop the lock */ 8050Sstevel@tonic-gate mutex_exit(&so->so_lock); 8060Sstevel@tonic-gate 807898Skais /* 808898Skais * Intercept the bind_req message here to check if this <address/port> 809898Skais * was configured as an SSL proxy server, or if another endpoint was 810898Skais * already configured to act as a proxy for us. 8111974Sbrutus * 8121974Sbrutus * Note, only if NL7C not enabled for this socket. 813898Skais */ 8141974Sbrutus if (nl7c == NULL && 8151974Sbrutus (so->so_family == AF_INET || so->so_family == AF_INET6) && 816898Skais so->so_type == SOCK_STREAM) { 817898Skais 818898Skais if (so->so_kssl_ent != NULL) { 819898Skais kssl_release_ent(so->so_kssl_ent, so, so->so_kssl_type); 820898Skais so->so_kssl_ent = NULL; 821898Skais } 822898Skais 823898Skais so->so_kssl_type = kssl_check_proxy(mp, so, &so->so_kssl_ent); 824898Skais switch (so->so_kssl_type) { 825898Skais case KSSL_NO_PROXY: 826898Skais break; 827898Skais 828898Skais case KSSL_HAS_PROXY: 829898Skais mutex_enter(&so->so_lock); 830898Skais goto skip_transport; 831898Skais 832898Skais case KSSL_IS_PROXY: 833898Skais break; 834898Skais } 835898Skais } 836898Skais 8370Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 8385240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 8390Sstevel@tonic-gate if (error) { 8400Sstevel@tonic-gate eprintsoline(so, error); 8410Sstevel@tonic-gate mutex_enter(&so->so_lock); 8420Sstevel@tonic-gate goto done; 8430Sstevel@tonic-gate } 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate mutex_enter(&so->so_lock); 8460Sstevel@tonic-gate error = sowaitprim(so, PRIM_type, T_BIND_ACK, 8470Sstevel@tonic-gate (t_uscalar_t)sizeof (*bind_ack), &mp, 0); 8480Sstevel@tonic-gate if (error) { 8490Sstevel@tonic-gate eprintsoline(so, error); 8500Sstevel@tonic-gate goto done; 8510Sstevel@tonic-gate } 852898Skais skip_transport: 8530Sstevel@tonic-gate ASSERT(mp); 8540Sstevel@tonic-gate /* 8550Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 8560Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the bind 8570Sstevel@tonic-gate * is allowed to complete. 8580Sstevel@tonic-gate */ 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate /* Mark as bound. This will be undone if we detect errors below. */ 8610Sstevel@tonic-gate if (flags & _SOBIND_NOXLATE) { 8620Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 8630Sstevel@tonic-gate so->so_state |= SS_FADDR_NOXLATE; 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND)); 8660Sstevel@tonic-gate so->so_state |= SS_ISBOUND; 8670Sstevel@tonic-gate ASSERT(so->so_unbind_mp); 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate /* note that we've already set SS_ACCEPTCONN above */ 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate /* 8720Sstevel@tonic-gate * Recompute addrlen - an unspecied bind sent down an 8730Sstevel@tonic-gate * address of length zero but we expect the appropriate length 8740Sstevel@tonic-gate * in return. 8750Sstevel@tonic-gate */ 8760Sstevel@tonic-gate addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ? 8770Sstevel@tonic-gate sizeof (so->so_ux_laddr) : so->so_laddr_len); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate bind_ack = (struct T_bind_ack *)mp->b_rptr; 8800Sstevel@tonic-gate /* 8810Sstevel@tonic-gate * The alignment restriction is really too strict but 8820Sstevel@tonic-gate * we want enough alignment to inspect the fields of 8830Sstevel@tonic-gate * a sockaddr_in. 8840Sstevel@tonic-gate */ 8850Sstevel@tonic-gate addr = sogetoff(mp, bind_ack->ADDR_offset, 8865240Snordmark bind_ack->ADDR_length, 8875240Snordmark __TPI_ALIGN_SIZE); 8880Sstevel@tonic-gate if (addr == NULL) { 8890Sstevel@tonic-gate freemsg(mp); 8900Sstevel@tonic-gate error = EPROTO; 8910Sstevel@tonic-gate eprintsoline(so, error); 8920Sstevel@tonic-gate goto done; 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate if (!(flags & _SOBIND_UNSPEC)) { 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * Verify that the transport didn't return something we 8970Sstevel@tonic-gate * did not want e.g. an address other than what we asked for. 8980Sstevel@tonic-gate * 8990Sstevel@tonic-gate * NOTE: These checks would go away if/when we switch to 9000Sstevel@tonic-gate * using the new TPI (in which the transport would fail 9010Sstevel@tonic-gate * the request instead of assigning a different address). 9020Sstevel@tonic-gate * 9030Sstevel@tonic-gate * NOTE2: For protocols that we don't know (i.e. any 9040Sstevel@tonic-gate * other than AF_INET6, AF_INET and AF_UNIX), we 9050Sstevel@tonic-gate * cannot know if the transport should be expected to 9060Sstevel@tonic-gate * return the same address as that requested. 9070Sstevel@tonic-gate * 9080Sstevel@tonic-gate * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send 9090Sstevel@tonic-gate * down a T_BIND_REQ. We use O_T_BIND_REQ for others. 9100Sstevel@tonic-gate * 9110Sstevel@tonic-gate * For example, in the case of netatalk it may be 9120Sstevel@tonic-gate * inappropriate for the transport to return the 9130Sstevel@tonic-gate * requested address (as it may have allocated a local 9140Sstevel@tonic-gate * port number in behaviour similar to that of an 9150Sstevel@tonic-gate * AF_INET bind request with a port number of zero). 9160Sstevel@tonic-gate * 9170Sstevel@tonic-gate * Given the definition of O_T_BIND_REQ, where the 9180Sstevel@tonic-gate * transport may bind to an address other than the 9190Sstevel@tonic-gate * requested address, it's not possible to determine 9200Sstevel@tonic-gate * whether a returned address that differs from the 9210Sstevel@tonic-gate * requested address is a reason to fail (because the 9220Sstevel@tonic-gate * requested address was not available) or succeed 9230Sstevel@tonic-gate * (because the transport allocated an appropriate 9240Sstevel@tonic-gate * address and/or port). 9250Sstevel@tonic-gate * 9260Sstevel@tonic-gate * sockfs currently requires that the transport return 9270Sstevel@tonic-gate * the requested address in the T_BIND_ACK, unless 9280Sstevel@tonic-gate * there is code here to allow for any discrepancy. 9290Sstevel@tonic-gate * Such code exists for AF_INET and AF_INET6. 9300Sstevel@tonic-gate * 9310Sstevel@tonic-gate * Netatalk chooses to return the requested address 9320Sstevel@tonic-gate * rather than the (correct) allocated address. This 9330Sstevel@tonic-gate * means that netatalk violates the TPI specification 9340Sstevel@tonic-gate * (and would not function correctly if used from a 9350Sstevel@tonic-gate * TLI application), but it does mean that it works 9360Sstevel@tonic-gate * with sockfs. 9370Sstevel@tonic-gate * 9380Sstevel@tonic-gate * As noted above, using the newer XTI bind primitive 9390Sstevel@tonic-gate * (T_BIND_REQ) in preference to O_T_BIND_REQ would 9400Sstevel@tonic-gate * allow sockfs to be more sure about whether or not 9410Sstevel@tonic-gate * the bind request had succeeded (as transports are 9420Sstevel@tonic-gate * not permitted to bind to a different address than 9430Sstevel@tonic-gate * that requested - they must return failure). 9440Sstevel@tonic-gate * Unfortunately, support for T_BIND_REQ may not be 9450Sstevel@tonic-gate * present in all transport implementations (netatalk, 9460Sstevel@tonic-gate * for example, doesn't have it), making the 9470Sstevel@tonic-gate * transition difficult. 9480Sstevel@tonic-gate */ 9490Sstevel@tonic-gate if (bind_ack->ADDR_length != addrlen) { 9500Sstevel@tonic-gate /* Assumes that the requested address was in use */ 9510Sstevel@tonic-gate freemsg(mp); 9520Sstevel@tonic-gate error = EADDRINUSE; 9530Sstevel@tonic-gate eprintsoline(so, error); 9540Sstevel@tonic-gate goto done; 9550Sstevel@tonic-gate } 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate switch (so->so_family) { 9580Sstevel@tonic-gate case AF_INET6: 9590Sstevel@tonic-gate case AF_INET: { 9600Sstevel@tonic-gate sin_t *rname, *aname; 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate rname = (sin_t *)addr; 9630Sstevel@tonic-gate aname = (sin_t *)so->so_laddr_sa; 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate /* 9660Sstevel@tonic-gate * Take advantage of the alignment 9670Sstevel@tonic-gate * of sin_port and sin6_port which fall 9680Sstevel@tonic-gate * in the same place in their data structures. 9690Sstevel@tonic-gate * Just use sin_port for either address family. 9700Sstevel@tonic-gate * 9710Sstevel@tonic-gate * This may become a problem if (heaven forbid) 9720Sstevel@tonic-gate * there's a separate ipv6port_reserved... :-P 9730Sstevel@tonic-gate * 9740Sstevel@tonic-gate * Binding to port 0 has the semantics of letting 9750Sstevel@tonic-gate * the transport bind to any port. 9760Sstevel@tonic-gate * 9770Sstevel@tonic-gate * If the transport is TCP or UDP since we had sent 9780Sstevel@tonic-gate * a T_BIND_REQ we would not get a port other than 9790Sstevel@tonic-gate * what we asked for. 9800Sstevel@tonic-gate */ 9810Sstevel@tonic-gate if (tcp_udp_xport) { 9820Sstevel@tonic-gate /* 9830Sstevel@tonic-gate * Pick up the new port number if we bound to 9840Sstevel@tonic-gate * port 0. 9850Sstevel@tonic-gate */ 9860Sstevel@tonic-gate if (aname->sin_port == 0) 9870Sstevel@tonic-gate aname->sin_port = rname->sin_port; 9880Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 9890Sstevel@tonic-gate break; 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate if (aname->sin_port != 0 && 9920Sstevel@tonic-gate aname->sin_port != rname->sin_port) { 9930Sstevel@tonic-gate freemsg(mp); 9940Sstevel@tonic-gate error = EADDRINUSE; 9950Sstevel@tonic-gate eprintsoline(so, error); 9960Sstevel@tonic-gate goto done; 9970Sstevel@tonic-gate } 9980Sstevel@tonic-gate /* 9990Sstevel@tonic-gate * Pick up the new port number if we bound to port 0. 10000Sstevel@tonic-gate */ 10010Sstevel@tonic-gate aname->sin_port = rname->sin_port; 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate /* 10040Sstevel@tonic-gate * Unfortunately, addresses aren't _quite_ the same. 10050Sstevel@tonic-gate */ 10060Sstevel@tonic-gate if (so->so_family == AF_INET) { 10070Sstevel@tonic-gate if (aname->sin_addr.s_addr != 10080Sstevel@tonic-gate rname->sin_addr.s_addr) { 10090Sstevel@tonic-gate freemsg(mp); 10100Sstevel@tonic-gate error = EADDRNOTAVAIL; 10110Sstevel@tonic-gate eprintsoline(so, error); 10120Sstevel@tonic-gate goto done; 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate } else { 10150Sstevel@tonic-gate sin6_t *rname6 = (sin6_t *)rname; 10160Sstevel@tonic-gate sin6_t *aname6 = (sin6_t *)aname; 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr, 10190Sstevel@tonic-gate &rname6->sin6_addr)) { 10200Sstevel@tonic-gate freemsg(mp); 10210Sstevel@tonic-gate error = EADDRNOTAVAIL; 10220Sstevel@tonic-gate eprintsoline(so, error); 10230Sstevel@tonic-gate goto done; 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate break; 10270Sstevel@tonic-gate } 10280Sstevel@tonic-gate case AF_UNIX: 10290Sstevel@tonic-gate if (bcmp(addr, &so->so_ux_laddr, addrlen) != 0) { 10300Sstevel@tonic-gate freemsg(mp); 10310Sstevel@tonic-gate error = EADDRINUSE; 10320Sstevel@tonic-gate eprintsoline(so, error); 10330Sstevel@tonic-gate eprintso(so, 10345240Snordmark ("addrlen %d, addr 0x%x, vp %p\n", 10355240Snordmark addrlen, *((int *)addr), 1036*7240Srh87107 (void *)so->so_ux_bound_vp)); 10370Sstevel@tonic-gate goto done; 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 10400Sstevel@tonic-gate break; 10410Sstevel@tonic-gate default: 10420Sstevel@tonic-gate /* 10430Sstevel@tonic-gate * NOTE: This assumes that addresses can be 10440Sstevel@tonic-gate * byte-compared for equivalence. 10450Sstevel@tonic-gate */ 10460Sstevel@tonic-gate if (bcmp(addr, so->so_laddr_sa, addrlen) != 0) { 10470Sstevel@tonic-gate freemsg(mp); 10480Sstevel@tonic-gate error = EADDRINUSE; 10490Sstevel@tonic-gate eprintsoline(so, error); 10500Sstevel@tonic-gate goto done; 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate /* 10530Sstevel@tonic-gate * Don't mark SS_LADDR_VALID, as we cannot be 10540Sstevel@tonic-gate * sure that the returned address is the real 10550Sstevel@tonic-gate * bound address when talking to an unknown 10560Sstevel@tonic-gate * transport. 10570Sstevel@tonic-gate */ 10580Sstevel@tonic-gate break; 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate } else { 10610Sstevel@tonic-gate /* 10620Sstevel@tonic-gate * Save for returned address for getsockname. 10630Sstevel@tonic-gate * Needed for unspecific bind unless transport supports 10640Sstevel@tonic-gate * the TI_GETMYNAME ioctl. 10650Sstevel@tonic-gate * Do this for AF_INET{,6} even though they do, as 10660Sstevel@tonic-gate * caching info here is much better performance than 10670Sstevel@tonic-gate * a TPI/STREAMS trip to the transport for getsockname. 10680Sstevel@tonic-gate * Any which can't for some reason _must_ _not_ set 10690Sstevel@tonic-gate * LADDR_VALID here for the caching version of getsockname 10700Sstevel@tonic-gate * to not break; 10710Sstevel@tonic-gate */ 10720Sstevel@tonic-gate switch (so->so_family) { 10730Sstevel@tonic-gate case AF_UNIX: 10740Sstevel@tonic-gate /* 10750Sstevel@tonic-gate * Record the address bound with the transport 10760Sstevel@tonic-gate * for use by socketpair. 10770Sstevel@tonic-gate */ 10780Sstevel@tonic-gate bcopy(addr, &so->so_ux_laddr, addrlen); 10790Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 10800Sstevel@tonic-gate break; 10810Sstevel@tonic-gate case AF_INET: 10820Sstevel@tonic-gate case AF_INET6: 10830Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 10840Sstevel@tonic-gate bcopy(addr, so->so_laddr_sa, so->so_laddr_len); 10850Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 10860Sstevel@tonic-gate break; 10870Sstevel@tonic-gate default: 10880Sstevel@tonic-gate /* 10890Sstevel@tonic-gate * Don't mark SS_LADDR_VALID, as we cannot be 10900Sstevel@tonic-gate * sure that the returned address is the real 10910Sstevel@tonic-gate * bound address when talking to an unknown 10920Sstevel@tonic-gate * transport. 10930Sstevel@tonic-gate */ 10940Sstevel@tonic-gate break; 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate } 10970Sstevel@tonic-gate 10980Sstevel@tonic-gate if (nl7c != NULL) { 10991974Sbrutus /* Register listen()er sonode pointer with NL7C */ 11001974Sbrutus nl7c_listener_addr(nl7c, so); 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate freemsg(mp); 11040Sstevel@tonic-gate 11050Sstevel@tonic-gate done: 11060Sstevel@tonic-gate if (error) { 11070Sstevel@tonic-gate /* reset state & backlog to values held on entry */ 11080Sstevel@tonic-gate if (clear_acceptconn_on_err == B_TRUE) 11090Sstevel@tonic-gate so->so_state &= ~SS_ACCEPTCONN; 11100Sstevel@tonic-gate if (restore_backlog_on_err == B_TRUE) 11110Sstevel@tonic-gate so->so_backlog = save_so_backlog; 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate if (unbind_on_err && so->so_state & SS_ISBOUND) { 11140Sstevel@tonic-gate int err; 11150Sstevel@tonic-gate 11160Sstevel@tonic-gate err = sotpi_unbind(so, 0); 11170Sstevel@tonic-gate /* LINTED - statement has no consequent: if */ 11180Sstevel@tonic-gate if (err) { 11190Sstevel@tonic-gate eprintsoline(so, error); 11200Sstevel@tonic-gate } else { 11210Sstevel@tonic-gate ASSERT(!(so->so_state & SS_ISBOUND)); 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate if (!(flags & _SOBIND_LOCK_HELD)) { 11260Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 11270Sstevel@tonic-gate mutex_exit(&so->so_lock); 11280Sstevel@tonic-gate } else { 11290Sstevel@tonic-gate /* If the caller held the lock don't release it here */ 11300Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 11310Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate return (error); 11340Sstevel@tonic-gate } 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate /* bind the socket */ 1137741Smasputra static int 11380Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen, 11390Sstevel@tonic-gate int flags) 11400Sstevel@tonic-gate { 11410Sstevel@tonic-gate if ((flags & _SOBIND_SOCKETPAIR) == 0) 11420Sstevel@tonic-gate return (sotpi_bindlisten(so, name, namelen, 0, flags)); 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate flags &= ~_SOBIND_SOCKETPAIR; 11450Sstevel@tonic-gate return (sotpi_bindlisten(so, name, namelen, 1, flags)); 11460Sstevel@tonic-gate } 11470Sstevel@tonic-gate 11480Sstevel@tonic-gate /* 11490Sstevel@tonic-gate * Unbind a socket - used when bind() fails, when bind() specifies a NULL 11500Sstevel@tonic-gate * address, or when listen needs to unbind and bind. 11510Sstevel@tonic-gate * If the _SOUNBIND_REBIND flag is specified the addresses are retained 11520Sstevel@tonic-gate * so that a sobind can pick them up. 11530Sstevel@tonic-gate */ 11540Sstevel@tonic-gate static int 11550Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags) 11560Sstevel@tonic-gate { 11570Sstevel@tonic-gate struct T_unbind_req unbind_req; 11580Sstevel@tonic-gate int error = 0; 11590Sstevel@tonic-gate mblk_t *mp; 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n", 1162*7240Srh87107 (void *)so, flags, pr_state(so->so_state, so->so_mode))); 11630Sstevel@tonic-gate 11640Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 11650Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 11680Sstevel@tonic-gate error = EINVAL; 11690Sstevel@tonic-gate eprintsoline(so, error); 11700Sstevel@tonic-gate goto done; 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate mutex_exit(&so->so_lock); 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate /* 11760Sstevel@tonic-gate * Flush the read and write side (except stream head read queue) 11770Sstevel@tonic-gate * and send down T_UNBIND_REQ. 11780Sstevel@tonic-gate */ 11790Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW); 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate unbind_req.PRIM_type = T_UNBIND_REQ; 11820Sstevel@tonic-gate mp = soallocproto1(&unbind_req, sizeof (unbind_req), 11830Sstevel@tonic-gate 0, _ALLOC_SLEEP); 11840Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 11855240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 11860Sstevel@tonic-gate mutex_enter(&so->so_lock); 11870Sstevel@tonic-gate if (error) { 11880Sstevel@tonic-gate eprintsoline(so, error); 11890Sstevel@tonic-gate goto done; 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate error = sowaitokack(so, T_UNBIND_REQ); 11930Sstevel@tonic-gate if (error) { 11940Sstevel@tonic-gate eprintsoline(so, error); 11950Sstevel@tonic-gate goto done; 11960Sstevel@tonic-gate } 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate /* 11990Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 12000Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the unbind 12010Sstevel@tonic-gate * is allowed to complete. 12020Sstevel@tonic-gate */ 12030Sstevel@tonic-gate if (!(flags & _SOUNBIND_REBIND)) { 12040Sstevel@tonic-gate /* 12050Sstevel@tonic-gate * Clear out bound address. 12060Sstevel@tonic-gate */ 12070Sstevel@tonic-gate vnode_t *vp; 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate if ((vp = so->so_ux_bound_vp) != NULL) { 1210898Skais 1211898Skais /* Undo any SSL proxy setup */ 1212898Skais if ((so->so_family == AF_INET || 1213898Skais so->so_family == AF_INET6) && 1214898Skais (so->so_type == SOCK_STREAM) && 1215898Skais (so->so_kssl_ent != NULL)) { 1216898Skais kssl_release_ent(so->so_kssl_ent, so, 1217898Skais so->so_kssl_type); 1218898Skais so->so_kssl_ent = NULL; 1219898Skais so->so_kssl_type = KSSL_NO_PROXY; 1220898Skais } 1221898Skais 12220Sstevel@tonic-gate so->so_ux_bound_vp = NULL; 12230Sstevel@tonic-gate vn_rele_stream(vp); 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate /* Clear out address */ 12260Sstevel@tonic-gate so->so_laddr_len = 0; 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN|SS_LADDR_VALID); 12291974Sbrutus 12300Sstevel@tonic-gate done: 1231898Skais 12320Sstevel@tonic-gate /* If the caller held the lock don't release it here */ 12330Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 12340Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate return (error); 12370Sstevel@tonic-gate } 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate /* 12400Sstevel@tonic-gate * listen on the socket. 12410Sstevel@tonic-gate * For TPI conforming transports this has to first unbind with the transport 12420Sstevel@tonic-gate * and then bind again using the new backlog. 12430Sstevel@tonic-gate */ 12440Sstevel@tonic-gate int 12450Sstevel@tonic-gate sotpi_listen(struct sonode *so, int backlog) 12460Sstevel@tonic-gate { 12470Sstevel@tonic-gate int error = 0; 12480Sstevel@tonic-gate 12490Sstevel@tonic-gate dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n", 1250*7240Srh87107 (void *)so, backlog, pr_state(so->so_state, so->so_mode))); 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate if (so->so_serv_type == T_CLTS) 12530Sstevel@tonic-gate return (EOPNOTSUPP); 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* 12560Sstevel@tonic-gate * If the socket is ready to accept connections already, then 12570Sstevel@tonic-gate * return without doing anything. This avoids a problem where 12580Sstevel@tonic-gate * a second listen() call fails if a connection is pending and 12590Sstevel@tonic-gate * leaves the socket unbound. Only when we are not unbinding 12600Sstevel@tonic-gate * with the transport can we safely increase the backlog. 12610Sstevel@tonic-gate */ 12620Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN && 12630Sstevel@tonic-gate !((so->so_family == AF_INET || so->so_family == AF_INET6) && 12645240Snordmark /*CONSTCOND*/ 12655240Snordmark !solisten_tpi_tcp)) 12660Sstevel@tonic-gate return (0); 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate if (so->so_state & SS_ISCONNECTED) 12690Sstevel@tonic-gate return (EINVAL); 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate mutex_enter(&so->so_lock); 12720Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate if (backlog < 0) 12750Sstevel@tonic-gate backlog = 0; 12760Sstevel@tonic-gate /* 12770Sstevel@tonic-gate * Use the same qlimit as in BSD. BSD checks the qlimit 12780Sstevel@tonic-gate * before queuing the next connection implying that a 12790Sstevel@tonic-gate * listen(sock, 0) allows one connection to be queued. 12800Sstevel@tonic-gate * BSD also uses 1.5 times the requested backlog. 12810Sstevel@tonic-gate * 12820Sstevel@tonic-gate * XNS Issue 4 required a strict interpretation of the backlog. 12830Sstevel@tonic-gate * This has been waived subsequently for Issue 4 and the change 12840Sstevel@tonic-gate * incorporated in XNS Issue 5. So we aren't required to do 12850Sstevel@tonic-gate * anything special for XPG apps. 12860Sstevel@tonic-gate */ 12870Sstevel@tonic-gate if (backlog >= (INT_MAX - 1) / 3) 12880Sstevel@tonic-gate backlog = INT_MAX; 12890Sstevel@tonic-gate else 12900Sstevel@tonic-gate backlog = backlog * 3 / 2 + 1; 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate /* 12930Sstevel@tonic-gate * If the listen doesn't change the backlog we do nothing. 12940Sstevel@tonic-gate * This avoids an EPROTO error from the transport. 12950Sstevel@tonic-gate */ 12960Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) && 12970Sstevel@tonic-gate so->so_backlog == backlog) 12980Sstevel@tonic-gate goto done; 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 13010Sstevel@tonic-gate /* 13020Sstevel@tonic-gate * Must have been explicitly bound in the UNIX domain. 13030Sstevel@tonic-gate */ 13040Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 13050Sstevel@tonic-gate error = EINVAL; 13060Sstevel@tonic-gate goto done; 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog, 13095240Snordmark _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN); 13100Sstevel@tonic-gate } else if (backlog > 0) { 13110Sstevel@tonic-gate /* 13120Sstevel@tonic-gate * AF_INET{,6} hack to avoid losing the port. 13130Sstevel@tonic-gate * Assumes that all AF_INET{,6} transports can handle a 13140Sstevel@tonic-gate * O_T_BIND_REQ with a non-zero CONIND_number when the TPI 13150Sstevel@tonic-gate * has already bound thus it is possible to avoid the unbind. 13160Sstevel@tonic-gate */ 13170Sstevel@tonic-gate if (!((so->so_family == AF_INET || so->so_family == AF_INET6) && 13180Sstevel@tonic-gate /*CONSTCOND*/ 13190Sstevel@tonic-gate !solisten_tpi_tcp)) { 13200Sstevel@tonic-gate error = sotpi_unbind(so, _SOUNBIND_REBIND); 13210Sstevel@tonic-gate if (error) 13220Sstevel@tonic-gate goto done; 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate error = sotpi_bindlisten(so, NULL, 0, backlog, 13255240Snordmark _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN); 13260Sstevel@tonic-gate } else { 13270Sstevel@tonic-gate so->so_state |= SS_ACCEPTCONN; 13280Sstevel@tonic-gate so->so_backlog = backlog; 13290Sstevel@tonic-gate } 13300Sstevel@tonic-gate if (error) 13310Sstevel@tonic-gate goto done; 13320Sstevel@tonic-gate ASSERT(so->so_state & SS_ACCEPTCONN); 13330Sstevel@tonic-gate done: 13340Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 13350Sstevel@tonic-gate mutex_exit(&so->so_lock); 13360Sstevel@tonic-gate return (error); 13370Sstevel@tonic-gate } 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate /* 13400Sstevel@tonic-gate * Disconnect either a specified seqno or all (-1). 13410Sstevel@tonic-gate * The former is used on listening sockets only. 13420Sstevel@tonic-gate * 13430Sstevel@tonic-gate * When seqno == -1 sodisconnect could call sotpi_unbind. However, 13440Sstevel@tonic-gate * the current use of sodisconnect(seqno == -1) is only for shutdown 13450Sstevel@tonic-gate * so there is no point (and potentially incorrect) to unbind. 13460Sstevel@tonic-gate */ 13470Sstevel@tonic-gate int 13480Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags) 13490Sstevel@tonic-gate { 13500Sstevel@tonic-gate struct T_discon_req discon_req; 13510Sstevel@tonic-gate int error = 0; 13520Sstevel@tonic-gate mblk_t *mp; 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n", 1355*7240Srh87107 (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode))); 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) { 13580Sstevel@tonic-gate mutex_enter(&so->so_lock); 13590Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 13600Sstevel@tonic-gate } else { 13610Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 13620Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 13630Sstevel@tonic-gate } 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) { 13660Sstevel@tonic-gate error = EINVAL; 13670Sstevel@tonic-gate eprintsoline(so, error); 13680Sstevel@tonic-gate goto done; 13690Sstevel@tonic-gate } 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate mutex_exit(&so->so_lock); 13720Sstevel@tonic-gate /* 13730Sstevel@tonic-gate * Flush the write side (unless this is a listener) 13740Sstevel@tonic-gate * and then send down a T_DISCON_REQ. 13750Sstevel@tonic-gate * (Don't flush on listener since it could flush {O_}T_CONN_RES 13760Sstevel@tonic-gate * and other messages.) 13770Sstevel@tonic-gate */ 13780Sstevel@tonic-gate if (!(so->so_state & SS_ACCEPTCONN)) 13790Sstevel@tonic-gate (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW); 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate discon_req.PRIM_type = T_DISCON_REQ; 13820Sstevel@tonic-gate discon_req.SEQ_number = seqno; 13830Sstevel@tonic-gate mp = soallocproto1(&discon_req, sizeof (discon_req), 13840Sstevel@tonic-gate 0, _ALLOC_SLEEP); 13850Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 13865240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 13870Sstevel@tonic-gate mutex_enter(&so->so_lock); 13880Sstevel@tonic-gate if (error) { 13890Sstevel@tonic-gate eprintsoline(so, error); 13900Sstevel@tonic-gate goto done; 13910Sstevel@tonic-gate } 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate error = sowaitokack(so, T_DISCON_REQ); 13940Sstevel@tonic-gate if (error) { 13950Sstevel@tonic-gate eprintsoline(so, error); 13960Sstevel@tonic-gate goto done; 13970Sstevel@tonic-gate } 13980Sstevel@tonic-gate /* 13990Sstevel@tonic-gate * Even if some TPI message (e.g. T_DISCON_IND) was received in 14000Sstevel@tonic-gate * strsock_proto while the lock was dropped above, the disconnect 14010Sstevel@tonic-gate * is allowed to complete. However, it is not possible to 14020Sstevel@tonic-gate * assert that SS_ISCONNECTED|SS_ISCONNECTING are set. 14030Sstevel@tonic-gate */ 14040Sstevel@tonic-gate so->so_state &= 14050Sstevel@tonic-gate ~(SS_ISCONNECTED|SS_ISCONNECTING|SS_LADDR_VALID|SS_FADDR_VALID); 14060Sstevel@tonic-gate done: 14070Sstevel@tonic-gate if (!(flags & _SODISCONNECT_LOCK_HELD)) { 14080Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 14090Sstevel@tonic-gate mutex_exit(&so->so_lock); 14100Sstevel@tonic-gate } else { 14110Sstevel@tonic-gate /* If the caller held the lock don't release it here */ 14120Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 14130Sstevel@tonic-gate ASSERT(so->so_flag & SOLOCKED); 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate return (error); 14160Sstevel@tonic-gate } 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate int 14190Sstevel@tonic-gate sotpi_accept(struct sonode *so, int fflag, struct sonode **nsop) 14200Sstevel@tonic-gate { 14210Sstevel@tonic-gate struct T_conn_ind *conn_ind; 14220Sstevel@tonic-gate struct T_conn_res *conn_res; 14230Sstevel@tonic-gate int error = 0; 14244379Sja97890 mblk_t *mp, *ctxmp, *ack_mp; 14250Sstevel@tonic-gate struct sonode *nso; 14260Sstevel@tonic-gate vnode_t *nvp; 14270Sstevel@tonic-gate void *src; 14280Sstevel@tonic-gate t_uscalar_t srclen; 14290Sstevel@tonic-gate void *opt; 14300Sstevel@tonic-gate t_uscalar_t optlen; 14310Sstevel@tonic-gate t_scalar_t PRIM_type; 14320Sstevel@tonic-gate t_scalar_t SEQ_number; 14334379Sja97890 size_t sinlen; 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n", 1436*7240Srh87107 (void *)so, fflag, (void *)nsop, 1437*7240Srh87107 pr_state(so->so_state, so->so_mode))); 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate /* 14400Sstevel@tonic-gate * Defer single-threading the accepting socket until 14410Sstevel@tonic-gate * the T_CONN_IND has been received and parsed and the 14420Sstevel@tonic-gate * new sonode has been opened. 14430Sstevel@tonic-gate */ 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate /* Check that we are not already connected */ 14460Sstevel@tonic-gate if ((so->so_state & SS_ACCEPTCONN) == 0) 14470Sstevel@tonic-gate goto conn_bad; 14480Sstevel@tonic-gate again: 14490Sstevel@tonic-gate if ((error = sowaitconnind(so, fflag, &mp)) != 0) 14500Sstevel@tonic-gate goto e_bad; 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate ASSERT(mp); 14530Sstevel@tonic-gate conn_ind = (struct T_conn_ind *)mp->b_rptr; 1454898Skais ctxmp = mp->b_cont; 1455898Skais 14560Sstevel@tonic-gate /* 14570Sstevel@tonic-gate * Save SEQ_number for error paths. 14580Sstevel@tonic-gate */ 14590Sstevel@tonic-gate SEQ_number = conn_ind->SEQ_number; 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate srclen = conn_ind->SRC_length; 14620Sstevel@tonic-gate src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1); 14630Sstevel@tonic-gate if (src == NULL) { 14640Sstevel@tonic-gate error = EPROTO; 14650Sstevel@tonic-gate freemsg(mp); 14660Sstevel@tonic-gate eprintsoline(so, error); 14670Sstevel@tonic-gate goto disconnect_unlocked; 14680Sstevel@tonic-gate } 14690Sstevel@tonic-gate optlen = conn_ind->OPT_length; 14700Sstevel@tonic-gate switch (so->so_family) { 14710Sstevel@tonic-gate case AF_INET: 14720Sstevel@tonic-gate case AF_INET6: 14730Sstevel@tonic-gate if ((optlen == sizeof (intptr_t)) && 1474741Smasputra ((so->so_state & SS_DIRECT) != 0)) { 14750Sstevel@tonic-gate bcopy(mp->b_rptr + conn_ind->OPT_offset, 14760Sstevel@tonic-gate &opt, conn_ind->OPT_length); 14770Sstevel@tonic-gate } else { 14780Sstevel@tonic-gate /* 14790Sstevel@tonic-gate * The transport (in this case TCP) hasn't sent up 14800Sstevel@tonic-gate * a pointer to an instance for the accept fast-path. 14810Sstevel@tonic-gate * Disable fast-path completely because the call to 14820Sstevel@tonic-gate * sotpi_create() below would otherwise create an 14830Sstevel@tonic-gate * incomplete TCP instance, which would lead to 14840Sstevel@tonic-gate * problems when sockfs sends a normal T_CONN_RES 14850Sstevel@tonic-gate * message down the new stream. 14860Sstevel@tonic-gate */ 1487741Smasputra if (so->so_state & SS_DIRECT) { 1488741Smasputra int rval; 1489741Smasputra /* 1490741Smasputra * For consistency we inform tcp to disable 1491741Smasputra * direct interface on the listener, though 1492741Smasputra * we can certainly live without doing this 1493741Smasputra * because no data will ever travel upstream 1494741Smasputra * on the listening socket. 1495741Smasputra */ 1496741Smasputra so->so_state &= ~SS_DIRECT; 1497741Smasputra (void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK, 1498741Smasputra 0, 0, K_TO_K, CRED(), &rval); 1499741Smasputra } 15000Sstevel@tonic-gate opt = NULL; 15010Sstevel@tonic-gate optlen = 0; 15020Sstevel@tonic-gate } 15030Sstevel@tonic-gate break; 15040Sstevel@tonic-gate case AF_UNIX: 15050Sstevel@tonic-gate default: 15060Sstevel@tonic-gate if (optlen != 0) { 15070Sstevel@tonic-gate opt = sogetoff(mp, conn_ind->OPT_offset, optlen, 15080Sstevel@tonic-gate __TPI_ALIGN_SIZE); 15090Sstevel@tonic-gate if (opt == NULL) { 15100Sstevel@tonic-gate error = EPROTO; 15110Sstevel@tonic-gate freemsg(mp); 15120Sstevel@tonic-gate eprintsoline(so, error); 15130Sstevel@tonic-gate goto disconnect_unlocked; 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate } 15160Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 15170Sstevel@tonic-gate if (!(so->so_state & SS_FADDR_NOXLATE)) { 15180Sstevel@tonic-gate src = NULL; 15190Sstevel@tonic-gate srclen = 0; 15200Sstevel@tonic-gate } 15210Sstevel@tonic-gate /* Extract src address from options */ 15220Sstevel@tonic-gate if (optlen != 0) 15230Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &src, &srclen); 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate break; 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate /* 15290Sstevel@tonic-gate * Create the new socket. 15300Sstevel@tonic-gate */ 15310Sstevel@tonic-gate VN_HOLD(so->so_accessvp); 15320Sstevel@tonic-gate nso = sotpi_create(so->so_accessvp, so->so_family, so->so_type, 15335240Snordmark so->so_protocol, so->so_version, so, &error); 15340Sstevel@tonic-gate if (nso == NULL) { 15350Sstevel@tonic-gate ASSERT(error != 0); 15360Sstevel@tonic-gate /* 15370Sstevel@tonic-gate * Accept can not fail with ENOBUFS. sotpi_create 15380Sstevel@tonic-gate * sleeps waiting for memory until a signal is caught 15390Sstevel@tonic-gate * so return EINTR. 15400Sstevel@tonic-gate */ 15410Sstevel@tonic-gate freemsg(mp); 15420Sstevel@tonic-gate if (error == ENOBUFS) 15430Sstevel@tonic-gate error = EINTR; 15440Sstevel@tonic-gate goto e_disc_unl; 15450Sstevel@tonic-gate } 15460Sstevel@tonic-gate nvp = SOTOV(nso); 15470Sstevel@tonic-gate 1548898Skais /* 1549898Skais * If the transport sent up an SSL connection context, then attach 1550898Skais * it the new socket, and set the (sd_wputdatafunc)() and 1551898Skais * (sd_rputdatafunc)() stream head hooks to intercept and process 1552898Skais * SSL records. 1553898Skais */ 1554898Skais if (ctxmp != NULL) { 1555898Skais /* 1556898Skais * This kssl_ctx_t is already held for us by the transport. 1557898Skais * So, we don't need to do a kssl_hold_ctx() here. 1558898Skais */ 1559898Skais nso->so_kssl_ctx = *((kssl_ctx_t *)ctxmp->b_rptr); 1560898Skais freemsg(ctxmp); 1561898Skais mp->b_cont = NULL; 1562898Skais strsetrwputdatahooks(nvp, strsock_kssl_input, 1563898Skais strsock_kssl_output); 1564898Skais } 15650Sstevel@tonic-gate #ifdef DEBUG 15660Sstevel@tonic-gate /* 15670Sstevel@tonic-gate * SO_DEBUG is used to trigger the dprint* and eprint* macros thus 15680Sstevel@tonic-gate * it's inherited early to allow debugging of the accept code itself. 15690Sstevel@tonic-gate */ 15700Sstevel@tonic-gate nso->so_options |= so->so_options & SO_DEBUG; 15710Sstevel@tonic-gate #endif /* DEBUG */ 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate /* 15740Sstevel@tonic-gate * Save the SRC address from the T_CONN_IND 15750Sstevel@tonic-gate * for getpeername to work on AF_UNIX and on transports that do not 15760Sstevel@tonic-gate * support TI_GETPEERNAME. 15770Sstevel@tonic-gate * 15780Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by the sender's 15790Sstevel@tonic-gate * copyin_name(). 15800Sstevel@tonic-gate */ 15810Sstevel@tonic-gate if (srclen > (t_uscalar_t)nso->so_faddr_maxlen) { 15820Sstevel@tonic-gate error = EINVAL; 15830Sstevel@tonic-gate freemsg(mp); 15840Sstevel@tonic-gate eprintsoline(so, error); 15850Sstevel@tonic-gate goto disconnect_vp_unlocked; 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate nso->so_faddr_len = (socklen_t)srclen; 15880Sstevel@tonic-gate ASSERT(so->so_faddr_len <= so->so_faddr_maxlen); 15890Sstevel@tonic-gate bcopy(src, nso->so_faddr_sa, srclen); 15900Sstevel@tonic-gate nso->so_state |= SS_FADDR_VALID; 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate if ((DB_REF(mp) > 1) || MBLKSIZE(mp) < 15930Sstevel@tonic-gate (sizeof (struct T_conn_res) + sizeof (intptr_t))) { 15940Sstevel@tonic-gate cred_t *cr; 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate if ((cr = DB_CRED(mp)) != NULL) { 15970Sstevel@tonic-gate crhold(cr); 15980Sstevel@tonic-gate nso->so_peercred = cr; 15990Sstevel@tonic-gate nso->so_cpid = DB_CPID(mp); 16000Sstevel@tonic-gate } 16010Sstevel@tonic-gate freemsg(mp); 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate mp = soallocproto1(NULL, sizeof (struct T_conn_res) + 16040Sstevel@tonic-gate sizeof (intptr_t), 0, _ALLOC_INTR); 16050Sstevel@tonic-gate if (mp == NULL) { 16060Sstevel@tonic-gate /* 16070Sstevel@tonic-gate * Accept can not fail with ENOBUFS. 16080Sstevel@tonic-gate * A signal was caught so return EINTR. 16090Sstevel@tonic-gate */ 16100Sstevel@tonic-gate error = EINTR; 16110Sstevel@tonic-gate eprintsoline(so, error); 16120Sstevel@tonic-gate goto disconnect_vp_unlocked; 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr; 16150Sstevel@tonic-gate } else { 16160Sstevel@tonic-gate nso->so_peercred = DB_CRED(mp); 16170Sstevel@tonic-gate nso->so_cpid = DB_CPID(mp); 16180Sstevel@tonic-gate DB_CRED(mp) = NULL; 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate mp->b_rptr = DB_BASE(mp); 16210Sstevel@tonic-gate conn_res = (struct T_conn_res *)mp->b_rptr; 16220Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res); 16230Sstevel@tonic-gate } 16240Sstevel@tonic-gate 16250Sstevel@tonic-gate /* 16260Sstevel@tonic-gate * New socket must be bound at least in sockfs and, except for AF_INET, 16270Sstevel@tonic-gate * (or AF_INET6) it also has to be bound in the transport provider. 16284379Sja97890 * We set the local address in the sonode from the T_OK_ACK of the 16294379Sja97890 * T_CONN_RES. For this reason the address we bind to here isn't 16304379Sja97890 * important. 16310Sstevel@tonic-gate */ 16320Sstevel@tonic-gate if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) && 16330Sstevel@tonic-gate /*CONSTCOND*/ 16340Sstevel@tonic-gate nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) { 16350Sstevel@tonic-gate /* 16360Sstevel@tonic-gate * Optimization for AF_INET{,6} transports 16370Sstevel@tonic-gate * that can handle a T_CONN_RES without being bound. 16380Sstevel@tonic-gate */ 16390Sstevel@tonic-gate mutex_enter(&nso->so_lock); 16400Sstevel@tonic-gate so_automatic_bind(nso); 16410Sstevel@tonic-gate mutex_exit(&nso->so_lock); 16420Sstevel@tonic-gate } else { 16430Sstevel@tonic-gate /* Perform NULL bind with the transport provider. */ 16440Sstevel@tonic-gate if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC)) != 0) { 16450Sstevel@tonic-gate ASSERT(error != ENOBUFS); 16460Sstevel@tonic-gate freemsg(mp); 16470Sstevel@tonic-gate eprintsoline(nso, error); 16480Sstevel@tonic-gate goto disconnect_vp_unlocked; 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate } 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate /* 16530Sstevel@tonic-gate * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES 16540Sstevel@tonic-gate * so that any data arriving on the new socket will cause the 16550Sstevel@tonic-gate * appropriate signals to be delivered for the new socket. 16560Sstevel@tonic-gate * 16570Sstevel@tonic-gate * No other thread (except strsock_proto and strsock_misc) 16580Sstevel@tonic-gate * can access the new socket thus we relax the locking. 16590Sstevel@tonic-gate */ 16600Sstevel@tonic-gate nso->so_pgrp = so->so_pgrp; 16610Sstevel@tonic-gate nso->so_state |= so->so_state & (SS_ASYNC|SS_FADDR_NOXLATE); 16620Sstevel@tonic-gate 16630Sstevel@tonic-gate if (nso->so_pgrp != 0) { 16640Sstevel@tonic-gate if ((error = so_set_events(nso, nvp, CRED())) != 0) { 16650Sstevel@tonic-gate eprintsoline(nso, error); 16660Sstevel@tonic-gate error = 0; 16670Sstevel@tonic-gate nso->so_pgrp = 0; 16680Sstevel@tonic-gate } 16690Sstevel@tonic-gate } 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate /* 16720Sstevel@tonic-gate * Make note of the socket level options. TCP and IP level options 16730Sstevel@tonic-gate * are already inherited. We could do all this after accept is 16740Sstevel@tonic-gate * successful but doing it here simplifies code and no harm done 16750Sstevel@tonic-gate * for error case. 16760Sstevel@tonic-gate */ 16770Sstevel@tonic-gate nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE| 16780Sstevel@tonic-gate SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK| 16790Sstevel@tonic-gate SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER); 16800Sstevel@tonic-gate nso->so_sndbuf = so->so_sndbuf; 16810Sstevel@tonic-gate nso->so_rcvbuf = so->so_rcvbuf; 16820Sstevel@tonic-gate if (nso->so_options & SO_LINGER) 16830Sstevel@tonic-gate nso->so_linger = so->so_linger; 16840Sstevel@tonic-gate 1685741Smasputra if ((so->so_state & SS_DIRECT) != 0) { 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate ASSERT(opt != NULL); 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate conn_res->OPT_length = optlen; 16900Sstevel@tonic-gate conn_res->OPT_offset = MBLKL(mp); 16910Sstevel@tonic-gate bcopy(&opt, mp->b_wptr, optlen); 16920Sstevel@tonic-gate mp->b_wptr += optlen; 16930Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES; 16940Sstevel@tonic-gate conn_res->ACCEPTOR_id = 0; 16950Sstevel@tonic-gate PRIM_type = T_CONN_RES; 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate /* Send down the T_CONN_RES on acceptor STREAM */ 16980Sstevel@tonic-gate error = kstrputmsg(SOTOV(nso), mp, NULL, 16990Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 17000Sstevel@tonic-gate if (error) { 17010Sstevel@tonic-gate mutex_enter(&so->so_lock); 17020Sstevel@tonic-gate so_lock_single(so); 17030Sstevel@tonic-gate eprintsoline(so, error); 17040Sstevel@tonic-gate goto disconnect_vp; 17050Sstevel@tonic-gate } 17060Sstevel@tonic-gate mutex_enter(&nso->so_lock); 17070Sstevel@tonic-gate error = sowaitprim(nso, T_CONN_RES, T_OK_ACK, 17080Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 17090Sstevel@tonic-gate if (error) { 17100Sstevel@tonic-gate mutex_exit(&nso->so_lock); 17110Sstevel@tonic-gate mutex_enter(&so->so_lock); 17120Sstevel@tonic-gate so_lock_single(so); 17130Sstevel@tonic-gate eprintsoline(so, error); 17140Sstevel@tonic-gate goto disconnect_vp; 17150Sstevel@tonic-gate } 17160Sstevel@tonic-gate if (nso->so_family == AF_INET) { 17170Sstevel@tonic-gate sin_t *sin; 17180Sstevel@tonic-gate 17190Sstevel@tonic-gate sin = (sin_t *)(ack_mp->b_rptr + 17200Sstevel@tonic-gate sizeof (struct T_ok_ack)); 17210Sstevel@tonic-gate bcopy(sin, nso->so_laddr_sa, sizeof (sin_t)); 17220Sstevel@tonic-gate nso->so_laddr_len = sizeof (sin_t); 17230Sstevel@tonic-gate } else { 17240Sstevel@tonic-gate sin6_t *sin6; 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate sin6 = (sin6_t *)(ack_mp->b_rptr + 17270Sstevel@tonic-gate sizeof (struct T_ok_ack)); 17280Sstevel@tonic-gate bcopy(sin6, nso->so_laddr_sa, sizeof (sin6_t)); 17290Sstevel@tonic-gate nso->so_laddr_len = sizeof (sin6_t); 17300Sstevel@tonic-gate } 17310Sstevel@tonic-gate freemsg(ack_mp); 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate nso->so_state |= SS_ISCONNECTED | SS_LADDR_VALID; 17340Sstevel@tonic-gate nso->so_priv = opt; 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate if (so->so_nl7c_flags & NL7C_ENABLED) { 17370Sstevel@tonic-gate /* 17381974Sbrutus * A NL7C marked listen()er so the new socket 17391974Sbrutus * inherits the listen()er's NL7C state, except 17401974Sbrutus * for NL7C_POLLIN. 17410Sstevel@tonic-gate * 17421974Sbrutus * Only call NL7C to process the new socket if 17431974Sbrutus * the listen socket allows blocking i/o. 17440Sstevel@tonic-gate */ 17451974Sbrutus nso->so_nl7c_flags = so->so_nl7c_flags & (~NL7C_POLLIN); 17461974Sbrutus if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) { 17471974Sbrutus /* 17481974Sbrutus * Nonblocking accept() just make it 17491974Sbrutus * persist to defer processing to the 17501974Sbrutus * read-side syscall (e.g. read). 17511974Sbrutus */ 17521974Sbrutus nso->so_nl7c_flags |= NL7C_SOPERSIST; 17531974Sbrutus } else if (nl7c_process(nso, B_FALSE)) { 17540Sstevel@tonic-gate /* 17550Sstevel@tonic-gate * NL7C has completed processing on the 17560Sstevel@tonic-gate * socket, close the socket and back to 17570Sstevel@tonic-gate * the top to await the next T_CONN_IND. 17580Sstevel@tonic-gate */ 17590Sstevel@tonic-gate mutex_exit(&nso->so_lock); 17600Sstevel@tonic-gate (void) VOP_CLOSE(nvp, 0, 1, (offset_t)0, 17615331Samw CRED(), NULL); 17620Sstevel@tonic-gate VN_RELE(nvp); 17630Sstevel@tonic-gate goto again; 17640Sstevel@tonic-gate } 17650Sstevel@tonic-gate /* Pass the new socket out */ 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate mutex_exit(&nso->so_lock); 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate /* 17712811Sja97890 * It's possible, through the use of autopush for example, 17722811Sja97890 * that the acceptor stream may not support SS_DIRECT 17732811Sja97890 * semantics. If the new socket does not support SS_DIRECT 17742811Sja97890 * we issue a _SIOCSOCKFALLBACK to inform the transport 17752811Sja97890 * as we would in the I_PUSH case. 17762811Sja97890 */ 17772811Sja97890 if (!(nso->so_state & SS_DIRECT)) { 17782811Sja97890 int rval; 17792811Sja97890 17802811Sja97890 if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK, 17812811Sja97890 0, 0, K_TO_K, CRED(), &rval)) != 0) { 17822811Sja97890 mutex_enter(&so->so_lock); 17832811Sja97890 so_lock_single(so); 17842811Sja97890 eprintsoline(so, error); 17852811Sja97890 goto disconnect_vp; 17862811Sja97890 } 17872811Sja97890 } 17882811Sja97890 17892811Sja97890 /* 17900Sstevel@tonic-gate * Pass out new socket. 17910Sstevel@tonic-gate */ 17920Sstevel@tonic-gate if (nsop != NULL) 17930Sstevel@tonic-gate *nsop = nso; 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate return (0); 17960Sstevel@tonic-gate } 17970Sstevel@tonic-gate 17980Sstevel@tonic-gate /* 17990Sstevel@tonic-gate * This is the non-performance case for sockets (e.g. AF_UNIX sockets) 18000Sstevel@tonic-gate * which don't support the FireEngine accept fast-path. It is also 18010Sstevel@tonic-gate * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd 18020Sstevel@tonic-gate * again. Neither sockfs nor TCP attempt to find out if some other 18030Sstevel@tonic-gate * random module has been inserted in between (in which case we 18040Sstevel@tonic-gate * should follow TLI accept behaviour). We blindly assume the worst 18050Sstevel@tonic-gate * case and revert back to old behaviour i.e. TCP will not send us 18060Sstevel@tonic-gate * any option (eager) and the accept should happen on the listener 18070Sstevel@tonic-gate * queue. Any queued T_conn_ind have already got their options removed 18080Sstevel@tonic-gate * by so_sock2_stream() when "sockmod" was I_POP'd. 18090Sstevel@tonic-gate */ 18100Sstevel@tonic-gate /* 18110Sstevel@tonic-gate * Fill in the {O_}T_CONN_RES before getting SOLOCKED. 18120Sstevel@tonic-gate */ 18130Sstevel@tonic-gate if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) { 18140Sstevel@tonic-gate #ifdef _ILP32 18150Sstevel@tonic-gate queue_t *q; 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate /* 18180Sstevel@tonic-gate * Find read queue in driver 18190Sstevel@tonic-gate * Can safely do this since we "own" nso/nvp. 18200Sstevel@tonic-gate */ 18210Sstevel@tonic-gate q = strvp2wq(nvp)->q_next; 18220Sstevel@tonic-gate while (SAMESTR(q)) 18230Sstevel@tonic-gate q = q->q_next; 18240Sstevel@tonic-gate q = RD(q); 18250Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)q; 18260Sstevel@tonic-gate #else 18270Sstevel@tonic-gate conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev); 18280Sstevel@tonic-gate #endif /* _ILP32 */ 18290Sstevel@tonic-gate conn_res->PRIM_type = O_T_CONN_RES; 18300Sstevel@tonic-gate PRIM_type = O_T_CONN_RES; 18310Sstevel@tonic-gate } else { 18320Sstevel@tonic-gate conn_res->ACCEPTOR_id = nso->so_acceptor_id; 18330Sstevel@tonic-gate conn_res->PRIM_type = T_CONN_RES; 18340Sstevel@tonic-gate PRIM_type = T_CONN_RES; 18350Sstevel@tonic-gate } 18360Sstevel@tonic-gate conn_res->SEQ_number = SEQ_number; 18370Sstevel@tonic-gate conn_res->OPT_length = 0; 18380Sstevel@tonic-gate conn_res->OPT_offset = 0; 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate mutex_enter(&so->so_lock); 18410Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 18420Sstevel@tonic-gate mutex_exit(&so->so_lock); 18430Sstevel@tonic-gate 18440Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 18450Sstevel@tonic-gate 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 18460Sstevel@tonic-gate mutex_enter(&so->so_lock); 18470Sstevel@tonic-gate if (error) { 18480Sstevel@tonic-gate eprintsoline(so, error); 18490Sstevel@tonic-gate goto disconnect_vp; 18500Sstevel@tonic-gate } 18514379Sja97890 error = sowaitprim(so, PRIM_type, T_OK_ACK, 18524379Sja97890 (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 18530Sstevel@tonic-gate if (error) { 18540Sstevel@tonic-gate eprintsoline(so, error); 18550Sstevel@tonic-gate goto disconnect_vp; 18560Sstevel@tonic-gate } 18574379Sja97890 /* 18584379Sja97890 * If there is a sin/sin6 appended onto the T_OK_ACK use 18594379Sja97890 * that to set the local address. If this is not present 18604379Sja97890 * then we zero out the address and don't set the 18614678Sja97890 * SS_LADDR_VALID bit. For AF_UNIX endpoints we copy over 18624678Sja97890 * the pathname from the listening socket. 18634379Sja97890 */ 18644379Sja97890 sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t); 18654379Sja97890 if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) && 18664379Sja97890 MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) { 18674379Sja97890 ack_mp->b_rptr += sizeof (struct T_ok_ack); 18684379Sja97890 bcopy(ack_mp->b_rptr, nso->so_laddr_sa, sinlen); 18694379Sja97890 nso->so_laddr_len = sinlen; 18704379Sja97890 nso->so_state |= SS_LADDR_VALID; 18714678Sja97890 } else if (nso->so_family == AF_UNIX) { 18724678Sja97890 ASSERT(so->so_family == AF_UNIX); 18734678Sja97890 nso->so_laddr_len = so->so_laddr_len; 18744678Sja97890 ASSERT(nso->so_laddr_len <= nso->so_laddr_maxlen); 18754678Sja97890 bcopy(so->so_laddr_sa, nso->so_laddr_sa, nso->so_laddr_len); 18764678Sja97890 nso->so_state |= SS_LADDR_VALID; 18774379Sja97890 } else { 18784379Sja97890 nso->so_laddr_len = so->so_laddr_len; 18794379Sja97890 ASSERT(nso->so_laddr_len <= nso->so_laddr_maxlen); 18804379Sja97890 bzero(nso->so_laddr_sa, nso->so_addr_size); 18814678Sja97890 nso->so_laddr_sa->sa_family = nso->so_family; 18824379Sja97890 } 18834379Sja97890 freemsg(ack_mp); 18844379Sja97890 18850Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 18860Sstevel@tonic-gate mutex_exit(&so->so_lock); 18870Sstevel@tonic-gate 18880Sstevel@tonic-gate nso->so_state |= SS_ISCONNECTED; 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate /* 18910Sstevel@tonic-gate * Pass out new socket. 18920Sstevel@tonic-gate */ 18930Sstevel@tonic-gate if (nsop != NULL) 18940Sstevel@tonic-gate *nsop = nso; 18950Sstevel@tonic-gate 18960Sstevel@tonic-gate return (0); 18970Sstevel@tonic-gate 18980Sstevel@tonic-gate 18990Sstevel@tonic-gate eproto_disc_unl: 19000Sstevel@tonic-gate error = EPROTO; 19010Sstevel@tonic-gate e_disc_unl: 19020Sstevel@tonic-gate eprintsoline(so, error); 19030Sstevel@tonic-gate goto disconnect_unlocked; 19040Sstevel@tonic-gate 19050Sstevel@tonic-gate pr_disc_vp_unl: 19060Sstevel@tonic-gate eprintsoline(so, error); 19070Sstevel@tonic-gate disconnect_vp_unlocked: 19085331Samw (void) VOP_CLOSE(nvp, 0, 1, 0, CRED(), NULL); 19090Sstevel@tonic-gate VN_RELE(nvp); 19100Sstevel@tonic-gate disconnect_unlocked: 19110Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, 0); 19120Sstevel@tonic-gate return (error); 19130Sstevel@tonic-gate 19140Sstevel@tonic-gate pr_disc_vp: 19150Sstevel@tonic-gate eprintsoline(so, error); 19160Sstevel@tonic-gate disconnect_vp: 19170Sstevel@tonic-gate (void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD); 19180Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 19190Sstevel@tonic-gate mutex_exit(&so->so_lock); 19205331Samw (void) VOP_CLOSE(nvp, 0, 1, 0, CRED(), NULL); 19210Sstevel@tonic-gate VN_RELE(nvp); 19220Sstevel@tonic-gate return (error); 19230Sstevel@tonic-gate 19240Sstevel@tonic-gate conn_bad: /* Note: SunOS 4/BSD unconditionally returns EINVAL here */ 19250Sstevel@tonic-gate error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW) 19260Sstevel@tonic-gate ? EOPNOTSUPP : EINVAL; 19270Sstevel@tonic-gate e_bad: 19280Sstevel@tonic-gate eprintsoline(so, error); 19290Sstevel@tonic-gate return (error); 19300Sstevel@tonic-gate } 19310Sstevel@tonic-gate 19320Sstevel@tonic-gate /* 19330Sstevel@tonic-gate * connect a socket. 19340Sstevel@tonic-gate * 19350Sstevel@tonic-gate * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to 19360Sstevel@tonic-gate * unconnect (by specifying a null address). 19370Sstevel@tonic-gate */ 19380Sstevel@tonic-gate int 19390Sstevel@tonic-gate sotpi_connect(struct sonode *so, 19400Sstevel@tonic-gate const struct sockaddr *name, 19410Sstevel@tonic-gate socklen_t namelen, 19420Sstevel@tonic-gate int fflag, 19430Sstevel@tonic-gate int flags) 19440Sstevel@tonic-gate { 19450Sstevel@tonic-gate struct T_conn_req conn_req; 19460Sstevel@tonic-gate int error = 0; 19470Sstevel@tonic-gate mblk_t *mp; 19480Sstevel@tonic-gate void *src; 19490Sstevel@tonic-gate socklen_t srclen; 19500Sstevel@tonic-gate void *addr; 19510Sstevel@tonic-gate socklen_t addrlen; 19520Sstevel@tonic-gate boolean_t need_unlock; 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n", 1955*7240Srh87107 (void *)so, (void *)name, namelen, fflag, flags, 19565240Snordmark pr_state(so->so_state, so->so_mode))); 19570Sstevel@tonic-gate 19580Sstevel@tonic-gate /* 19590Sstevel@tonic-gate * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to 19600Sstevel@tonic-gate * avoid sleeping for memory with SOLOCKED held. 19610Sstevel@tonic-gate * We know that the T_CONN_REQ can't be larger than 2 * so_faddr_maxlen 19620Sstevel@tonic-gate * + sizeof (struct T_opthdr). 19630Sstevel@tonic-gate * (the AF_UNIX so_ux_addr_xlate() does not make the address 19640Sstevel@tonic-gate * exceed so_faddr_maxlen). 19650Sstevel@tonic-gate */ 19660Sstevel@tonic-gate mp = soallocproto(sizeof (struct T_conn_req) + 19670Sstevel@tonic-gate 2 * so->so_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR); 19680Sstevel@tonic-gate if (mp == NULL) { 19690Sstevel@tonic-gate /* 19700Sstevel@tonic-gate * Connect can not fail with ENOBUFS. A signal was 19710Sstevel@tonic-gate * caught so return EINTR. 19720Sstevel@tonic-gate */ 19730Sstevel@tonic-gate error = EINTR; 19740Sstevel@tonic-gate eprintsoline(so, error); 19750Sstevel@tonic-gate return (error); 19760Sstevel@tonic-gate } 19770Sstevel@tonic-gate 19780Sstevel@tonic-gate mutex_enter(&so->so_lock); 19790Sstevel@tonic-gate /* 19805694Sjprakash * Make sure there is a preallocated T_unbind_req message 19815694Sjprakash * before any binding. This message is allocated when the 19825694Sjprakash * socket is created. Since another thread can consume 19835694Sjprakash * so_unbind_mp by the time we return from so_lock_single(), 19845694Sjprakash * we should check the availability of so_unbind_mp after 19855694Sjprakash * we return from so_lock_single(). 19860Sstevel@tonic-gate */ 19875694Sjprakash 19885694Sjprakash so_lock_single(so); /* Set SOLOCKED */ 19895694Sjprakash need_unlock = B_TRUE; 19905694Sjprakash 19910Sstevel@tonic-gate if (so->so_unbind_mp == NULL) { 19920Sstevel@tonic-gate dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n")); 19930Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 19940Sstevel@tonic-gate so->so_unbind_mp = 19950Sstevel@tonic-gate soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR); 19960Sstevel@tonic-gate if (so->so_unbind_mp == NULL) { 19970Sstevel@tonic-gate error = EINTR; 19980Sstevel@tonic-gate goto done; 19990Sstevel@tonic-gate } 20000Sstevel@tonic-gate } 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate /* 20030Sstevel@tonic-gate * Can't have done a listen before connecting. 20040Sstevel@tonic-gate */ 20050Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) { 20060Sstevel@tonic-gate error = EOPNOTSUPP; 20070Sstevel@tonic-gate goto done; 20080Sstevel@tonic-gate } 20090Sstevel@tonic-gate 20100Sstevel@tonic-gate /* 20110Sstevel@tonic-gate * Must be bound with the transport 20120Sstevel@tonic-gate */ 20130Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND)) { 20140Sstevel@tonic-gate if ((so->so_family == AF_INET || so->so_family == AF_INET6) && 20150Sstevel@tonic-gate /*CONSTCOND*/ 20160Sstevel@tonic-gate so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) { 20170Sstevel@tonic-gate /* 20180Sstevel@tonic-gate * Optimization for AF_INET{,6} transports 20190Sstevel@tonic-gate * that can handle a T_CONN_REQ without being bound. 20200Sstevel@tonic-gate */ 20210Sstevel@tonic-gate so_automatic_bind(so); 20220Sstevel@tonic-gate } else { 20230Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0, 20240Sstevel@tonic-gate _SOBIND_UNSPEC|_SOBIND_LOCK_HELD); 20250Sstevel@tonic-gate if (error) 20260Sstevel@tonic-gate goto done; 20270Sstevel@tonic-gate } 20280Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND); 20290Sstevel@tonic-gate flags |= _SOCONNECT_DID_BIND; 20300Sstevel@tonic-gate } 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate /* 20330Sstevel@tonic-gate * Handle a connect to a name parameter of type AF_UNSPEC like a 20340Sstevel@tonic-gate * connect to a null address. This is the portable method to 20350Sstevel@tonic-gate * unconnect a socket. 20360Sstevel@tonic-gate */ 20370Sstevel@tonic-gate if ((namelen >= sizeof (sa_family_t)) && 20380Sstevel@tonic-gate (name->sa_family == AF_UNSPEC)) { 20390Sstevel@tonic-gate name = NULL; 20400Sstevel@tonic-gate namelen = 0; 20410Sstevel@tonic-gate } 20420Sstevel@tonic-gate 20430Sstevel@tonic-gate /* 20440Sstevel@tonic-gate * Check that we are not already connected. 20450Sstevel@tonic-gate * A connection-oriented socket cannot be reconnected. 20460Sstevel@tonic-gate * A connected connection-less socket can be 20470Sstevel@tonic-gate * - connected to a different address by a subsequent connect 20480Sstevel@tonic-gate * - "unconnected" by a connect to the NULL address 20490Sstevel@tonic-gate */ 20500Sstevel@tonic-gate if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) { 20510Sstevel@tonic-gate ASSERT(!(flags & _SOCONNECT_DID_BIND)); 20520Sstevel@tonic-gate if (so->so_mode & SM_CONNREQUIRED) { 20530Sstevel@tonic-gate /* Connection-oriented socket */ 20540Sstevel@tonic-gate error = so->so_state & SS_ISCONNECTED ? 20550Sstevel@tonic-gate EISCONN : EALREADY; 20560Sstevel@tonic-gate goto done; 20570Sstevel@tonic-gate } 20580Sstevel@tonic-gate /* Connection-less socket */ 20590Sstevel@tonic-gate if (name == NULL) { 20600Sstevel@tonic-gate /* 20610Sstevel@tonic-gate * Remove the connected state and clear SO_DGRAM_ERRIND 20620Sstevel@tonic-gate * since it was set when the socket was connected. 20630Sstevel@tonic-gate * If this is UDP also send down a T_DISCON_REQ. 20640Sstevel@tonic-gate */ 20650Sstevel@tonic-gate int val; 20660Sstevel@tonic-gate 20670Sstevel@tonic-gate if ((so->so_family == AF_INET || 20685240Snordmark so->so_family == AF_INET6) && 20690Sstevel@tonic-gate (so->so_type == SOCK_DGRAM || 20705240Snordmark so->so_type == SOCK_RAW) && 20710Sstevel@tonic-gate /*CONSTCOND*/ 20720Sstevel@tonic-gate !soconnect_tpi_udp) { 20730Sstevel@tonic-gate /* XXX What about implicitly unbinding here? */ 20740Sstevel@tonic-gate error = sodisconnect(so, -1, 20755240Snordmark _SODISCONNECT_LOCK_HELD); 20760Sstevel@tonic-gate } else { 20770Sstevel@tonic-gate so->so_state &= 20780Sstevel@tonic-gate ~(SS_ISCONNECTED | SS_ISCONNECTING | 20790Sstevel@tonic-gate SS_FADDR_VALID); 20800Sstevel@tonic-gate so->so_faddr_len = 0; 20810Sstevel@tonic-gate } 20820Sstevel@tonic-gate 20830Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 20840Sstevel@tonic-gate mutex_exit(&so->so_lock); 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate val = 0; 20870Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND, 20885240Snordmark &val, (t_uscalar_t)sizeof (val)); 20890Sstevel@tonic-gate 20900Sstevel@tonic-gate mutex_enter(&so->so_lock); 20910Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 20920Sstevel@tonic-gate goto done; 20930Sstevel@tonic-gate } 20940Sstevel@tonic-gate } 20950Sstevel@tonic-gate ASSERT(so->so_state & SS_ISBOUND); 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate if (name == NULL || namelen == 0) { 20980Sstevel@tonic-gate error = EINVAL; 20990Sstevel@tonic-gate goto done; 21000Sstevel@tonic-gate } 21010Sstevel@tonic-gate /* 21020Sstevel@tonic-gate * Mark the socket if so_faddr_sa represents the transport level 21030Sstevel@tonic-gate * address. 21040Sstevel@tonic-gate */ 21050Sstevel@tonic-gate if (flags & _SOCONNECT_NOXLATE) { 21060Sstevel@tonic-gate struct sockaddr_ux *soaddr_ux; 21070Sstevel@tonic-gate 21080Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 21090Sstevel@tonic-gate if (namelen != sizeof (struct sockaddr_ux)) { 21100Sstevel@tonic-gate error = EINVAL; 21110Sstevel@tonic-gate goto done; 21120Sstevel@tonic-gate } 21130Sstevel@tonic-gate soaddr_ux = (struct sockaddr_ux *)name; 21140Sstevel@tonic-gate name = (struct sockaddr *)&soaddr_ux->sou_addr; 21150Sstevel@tonic-gate namelen = sizeof (soaddr_ux->sou_addr); 21160Sstevel@tonic-gate so->so_state |= SS_FADDR_NOXLATE; 21170Sstevel@tonic-gate } 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate /* 21200Sstevel@tonic-gate * Length and family checks. 21210Sstevel@tonic-gate */ 21220Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 21230Sstevel@tonic-gate if (error) 21240Sstevel@tonic-gate goto bad; 21250Sstevel@tonic-gate 21260Sstevel@tonic-gate /* 21270Sstevel@tonic-gate * Save foreign address. Needed for AF_UNIX as well as 21280Sstevel@tonic-gate * transport providers that do not support TI_GETPEERNAME. 21290Sstevel@tonic-gate * Also used for cached foreign address for TCP and UDP. 21300Sstevel@tonic-gate */ 21310Sstevel@tonic-gate if (namelen > (t_uscalar_t)so->so_faddr_maxlen) { 21320Sstevel@tonic-gate error = EINVAL; 21330Sstevel@tonic-gate goto done; 21340Sstevel@tonic-gate } 21350Sstevel@tonic-gate so->so_faddr_len = (socklen_t)namelen; 21360Sstevel@tonic-gate ASSERT(so->so_faddr_len <= so->so_faddr_maxlen); 21370Sstevel@tonic-gate bcopy(name, so->so_faddr_sa, namelen); 21380Sstevel@tonic-gate so->so_state |= SS_FADDR_VALID; 21390Sstevel@tonic-gate 21400Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 21410Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 21420Sstevel@tonic-gate /* 21430Sstevel@tonic-gate * Already have a transport internal address. Do not 21440Sstevel@tonic-gate * pass any (transport internal) source address. 21450Sstevel@tonic-gate */ 21460Sstevel@tonic-gate addr = so->so_faddr_sa; 21470Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_faddr_len; 21480Sstevel@tonic-gate src = NULL; 21490Sstevel@tonic-gate srclen = 0; 21500Sstevel@tonic-gate } else { 21510Sstevel@tonic-gate /* 21520Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 21530Sstevel@tonic-gate * and translate the remote address. 21540Sstevel@tonic-gate * Holding so_lock thus so_laddr_sa can not change. 21550Sstevel@tonic-gate */ 21560Sstevel@tonic-gate src = so->so_laddr_sa; 21570Sstevel@tonic-gate srclen = (t_uscalar_t)so->so_laddr_len; 21580Sstevel@tonic-gate dprintso(so, 1, 21595240Snordmark ("sotpi_connect UNIX: srclen %d, src %p\n", 21605240Snordmark srclen, src)); 21610Sstevel@tonic-gate error = so_ux_addr_xlate(so, 21625240Snordmark so->so_faddr_sa, (socklen_t)so->so_faddr_len, 21635240Snordmark (flags & _SOCONNECT_XPG4_2), 21645240Snordmark &addr, &addrlen); 21650Sstevel@tonic-gate if (error) 21660Sstevel@tonic-gate goto bad; 21670Sstevel@tonic-gate } 21680Sstevel@tonic-gate } else { 21690Sstevel@tonic-gate addr = so->so_faddr_sa; 21700Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_faddr_len; 21710Sstevel@tonic-gate src = NULL; 21720Sstevel@tonic-gate srclen = 0; 21730Sstevel@tonic-gate } 21740Sstevel@tonic-gate /* 21750Sstevel@tonic-gate * When connecting a datagram socket we issue the SO_DGRAM_ERRIND 21760Sstevel@tonic-gate * option which asks the transport provider to send T_UDERR_IND 21770Sstevel@tonic-gate * messages. These T_UDERR_IND messages are used to return connected 21780Sstevel@tonic-gate * style errors (e.g. ECONNRESET) for connected datagram sockets. 21790Sstevel@tonic-gate * 21800Sstevel@tonic-gate * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets) 21810Sstevel@tonic-gate * we send down a T_CONN_REQ. This is needed to let the 21820Sstevel@tonic-gate * transport assign a local address that is consistent with 21830Sstevel@tonic-gate * the remote address. Applications depend on a getsockname() 21840Sstevel@tonic-gate * after a connect() to retrieve the "source" IP address for 21850Sstevel@tonic-gate * the connected socket. Invalidate the cached local address 21860Sstevel@tonic-gate * to force getsockname() to enquire of the transport. 21870Sstevel@tonic-gate */ 21880Sstevel@tonic-gate if (!(so->so_mode & SM_CONNREQUIRED)) { 21890Sstevel@tonic-gate /* 21900Sstevel@tonic-gate * Datagram socket. 21910Sstevel@tonic-gate */ 21920Sstevel@tonic-gate int32_t val; 21930Sstevel@tonic-gate 21940Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 21950Sstevel@tonic-gate mutex_exit(&so->so_lock); 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate val = 1; 21980Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND, 21995240Snordmark &val, (t_uscalar_t)sizeof (val)); 22000Sstevel@tonic-gate 22010Sstevel@tonic-gate mutex_enter(&so->so_lock); 22020Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 22030Sstevel@tonic-gate if ((so->so_family != AF_INET && so->so_family != AF_INET6) || 22040Sstevel@tonic-gate (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) || 22050Sstevel@tonic-gate soconnect_tpi_udp) { 22060Sstevel@tonic-gate soisconnected(so); 22070Sstevel@tonic-gate goto done; 22080Sstevel@tonic-gate } 22090Sstevel@tonic-gate /* 22100Sstevel@tonic-gate * Send down T_CONN_REQ etc. 22110Sstevel@tonic-gate * Clear fflag to avoid returning EWOULDBLOCK. 22120Sstevel@tonic-gate */ 22130Sstevel@tonic-gate fflag = 0; 22140Sstevel@tonic-gate ASSERT(so->so_family != AF_UNIX); 22150Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 22160Sstevel@tonic-gate } else if (so->so_laddr_len != 0) { 22170Sstevel@tonic-gate /* 22180Sstevel@tonic-gate * If the local address or port was "any" then it may be 22190Sstevel@tonic-gate * changed by the transport as a result of the 22200Sstevel@tonic-gate * connect. Invalidate the cached version if we have one. 22210Sstevel@tonic-gate */ 22220Sstevel@tonic-gate switch (so->so_family) { 22230Sstevel@tonic-gate case AF_INET: 22240Sstevel@tonic-gate ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin_t)); 22250Sstevel@tonic-gate if (((sin_t *)so->so_laddr_sa)->sin_addr.s_addr == 22260Sstevel@tonic-gate INADDR_ANY || 22270Sstevel@tonic-gate ((sin_t *)so->so_laddr_sa)->sin_port == 0) 22280Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 22290Sstevel@tonic-gate break; 22300Sstevel@tonic-gate 22310Sstevel@tonic-gate case AF_INET6: 22320Sstevel@tonic-gate ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin6_t)); 22330Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED( 22340Sstevel@tonic-gate &((sin6_t *)so->so_laddr_sa) ->sin6_addr) || 22350Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED_ANY( 22360Sstevel@tonic-gate &((sin6_t *)so->so_laddr_sa)->sin6_addr) || 22370Sstevel@tonic-gate ((sin6_t *)so->so_laddr_sa)->sin6_port == 0) 22385240Snordmark so->so_state &= ~SS_LADDR_VALID; 22390Sstevel@tonic-gate break; 22400Sstevel@tonic-gate 22410Sstevel@tonic-gate default: 22420Sstevel@tonic-gate break; 22430Sstevel@tonic-gate } 22440Sstevel@tonic-gate } 22450Sstevel@tonic-gate 22460Sstevel@tonic-gate /* 22470Sstevel@tonic-gate * Check for failure of an earlier call 22480Sstevel@tonic-gate */ 22490Sstevel@tonic-gate if (so->so_error != 0) 22500Sstevel@tonic-gate goto so_bad; 22510Sstevel@tonic-gate 22520Sstevel@tonic-gate /* 22530Sstevel@tonic-gate * Send down T_CONN_REQ. Message was allocated above. 22540Sstevel@tonic-gate */ 22550Sstevel@tonic-gate conn_req.PRIM_type = T_CONN_REQ; 22560Sstevel@tonic-gate conn_req.DEST_length = addrlen; 22570Sstevel@tonic-gate conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req); 22580Sstevel@tonic-gate if (srclen == 0) { 22590Sstevel@tonic-gate conn_req.OPT_length = 0; 22600Sstevel@tonic-gate conn_req.OPT_offset = 0; 22610Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req)); 22620Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 22630Sstevel@tonic-gate } else { 22640Sstevel@tonic-gate /* 22650Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 22660Sstevel@tonic-gate * address option. 22670Sstevel@tonic-gate */ 22680Sstevel@tonic-gate struct T_opthdr toh; 22690Sstevel@tonic-gate 22700Sstevel@tonic-gate toh.level = SOL_SOCKET; 22710Sstevel@tonic-gate toh.name = SO_SRCADDR; 22720Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 22730Sstevel@tonic-gate toh.status = 0; 22740Sstevel@tonic-gate conn_req.OPT_length = 22755240Snordmark (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen)); 22760Sstevel@tonic-gate conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) + 22775240Snordmark _TPI_ALIGN_TOPT(addrlen)); 22780Sstevel@tonic-gate 22790Sstevel@tonic-gate soappendmsg(mp, &conn_req, sizeof (conn_req)); 22800Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 22810Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 22820Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 22830Sstevel@tonic-gate soappendmsg(mp, src, srclen); 22840Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 22850Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 22860Sstevel@tonic-gate } 22870Sstevel@tonic-gate /* 22880Sstevel@tonic-gate * Set SS_ISCONNECTING before sending down the T_CONN_REQ 22890Sstevel@tonic-gate * in order to have the right state when the T_CONN_CON shows up. 22900Sstevel@tonic-gate */ 22910Sstevel@tonic-gate soisconnecting(so); 22920Sstevel@tonic-gate mutex_exit(&so->so_lock); 22930Sstevel@tonic-gate 22940Sstevel@tonic-gate if (audit_active) 22950Sstevel@tonic-gate audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0); 22960Sstevel@tonic-gate 22970Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 22985240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 22990Sstevel@tonic-gate mp = NULL; 23000Sstevel@tonic-gate mutex_enter(&so->so_lock); 23010Sstevel@tonic-gate if (error != 0) 23020Sstevel@tonic-gate goto bad; 23030Sstevel@tonic-gate 23040Sstevel@tonic-gate if ((error = sowaitokack(so, T_CONN_REQ)) != 0) 23050Sstevel@tonic-gate goto bad; 23060Sstevel@tonic-gate 23070Sstevel@tonic-gate /* Allow other threads to access the socket */ 23080Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 23090Sstevel@tonic-gate need_unlock = B_FALSE; 23100Sstevel@tonic-gate 23110Sstevel@tonic-gate /* 23120Sstevel@tonic-gate * Wait until we get a T_CONN_CON or an error 23130Sstevel@tonic-gate */ 23140Sstevel@tonic-gate if ((error = sowaitconnected(so, fflag, 0)) != 0) { 23150Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 23160Sstevel@tonic-gate need_unlock = B_TRUE; 23170Sstevel@tonic-gate } 23180Sstevel@tonic-gate 23190Sstevel@tonic-gate done: 23200Sstevel@tonic-gate freemsg(mp); 23210Sstevel@tonic-gate switch (error) { 23220Sstevel@tonic-gate case EINPROGRESS: 23230Sstevel@tonic-gate case EALREADY: 23240Sstevel@tonic-gate case EISCONN: 23250Sstevel@tonic-gate case EINTR: 23260Sstevel@tonic-gate /* Non-fatal errors */ 23270Sstevel@tonic-gate so->so_state &= ~SS_LADDR_VALID; 23280Sstevel@tonic-gate /* FALLTHRU */ 23290Sstevel@tonic-gate case 0: 23300Sstevel@tonic-gate break; 23310Sstevel@tonic-gate 23320Sstevel@tonic-gate case EHOSTUNREACH: 23330Sstevel@tonic-gate if (flags & _SOCONNECT_XPG4_2) { 23340Sstevel@tonic-gate /* 23350Sstevel@tonic-gate * X/Open specification contains a requirement that 23360Sstevel@tonic-gate * ENETUNREACH be returned but does not require 23370Sstevel@tonic-gate * EHOSTUNREACH. In order to keep the test suite 23380Sstevel@tonic-gate * happy we mess with the errno here. 23390Sstevel@tonic-gate */ 23400Sstevel@tonic-gate error = ENETUNREACH; 23410Sstevel@tonic-gate } 23420Sstevel@tonic-gate /* FALLTHRU */ 23430Sstevel@tonic-gate 23440Sstevel@tonic-gate default: 23450Sstevel@tonic-gate ASSERT(need_unlock); 23460Sstevel@tonic-gate /* 23470Sstevel@tonic-gate * Fatal errors: clear SS_ISCONNECTING in case it was set, 23480Sstevel@tonic-gate * and invalidate local-address cache 23490Sstevel@tonic-gate */ 23500Sstevel@tonic-gate so->so_state &= ~(SS_ISCONNECTING | SS_LADDR_VALID); 23510Sstevel@tonic-gate /* A discon_ind might have already unbound us */ 23520Sstevel@tonic-gate if ((flags & _SOCONNECT_DID_BIND) && 23530Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) { 23540Sstevel@tonic-gate int err; 23550Sstevel@tonic-gate 23560Sstevel@tonic-gate err = sotpi_unbind(so, 0); 23570Sstevel@tonic-gate /* LINTED - statement has no conseq */ 23580Sstevel@tonic-gate if (err) { 23590Sstevel@tonic-gate eprintsoline(so, err); 23600Sstevel@tonic-gate } 23610Sstevel@tonic-gate } 23620Sstevel@tonic-gate break; 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate if (need_unlock) 23650Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 23660Sstevel@tonic-gate mutex_exit(&so->so_lock); 23670Sstevel@tonic-gate return (error); 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate so_bad: error = sogeterr(so); 23700Sstevel@tonic-gate bad: eprintsoline(so, error); 23710Sstevel@tonic-gate goto done; 23720Sstevel@tonic-gate } 23730Sstevel@tonic-gate 23740Sstevel@tonic-gate int 23750Sstevel@tonic-gate sotpi_shutdown(struct sonode *so, int how) 23760Sstevel@tonic-gate { 23770Sstevel@tonic-gate struct T_ordrel_req ordrel_req; 23780Sstevel@tonic-gate mblk_t *mp; 23790Sstevel@tonic-gate uint_t old_state, state_change; 23800Sstevel@tonic-gate int error = 0; 23810Sstevel@tonic-gate 23820Sstevel@tonic-gate dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n", 2383*7240Srh87107 (void *)so, how, pr_state(so->so_state, so->so_mode))); 23840Sstevel@tonic-gate 23850Sstevel@tonic-gate mutex_enter(&so->so_lock); 23860Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 23870Sstevel@tonic-gate 23880Sstevel@tonic-gate /* 23890Sstevel@tonic-gate * SunOS 4.X has no check for datagram sockets. 23900Sstevel@tonic-gate * 5.X checks that it is connected (ENOTCONN) 23910Sstevel@tonic-gate * X/Open requires that we check the connected state. 23920Sstevel@tonic-gate */ 23930Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) { 23940Sstevel@tonic-gate if (!xnet_skip_checks) { 23950Sstevel@tonic-gate error = ENOTCONN; 23960Sstevel@tonic-gate if (xnet_check_print) { 23970Sstevel@tonic-gate printf("sockfs: X/Open shutdown check " 23985240Snordmark "caused ENOTCONN\n"); 23990Sstevel@tonic-gate } 24000Sstevel@tonic-gate } 24010Sstevel@tonic-gate goto done; 24020Sstevel@tonic-gate } 24030Sstevel@tonic-gate /* 24040Sstevel@tonic-gate * Record the current state and then perform any state changes. 24050Sstevel@tonic-gate * Then use the difference between the old and new states to 24060Sstevel@tonic-gate * determine which messages need to be sent. 24070Sstevel@tonic-gate * This prevents e.g. duplicate T_ORDREL_REQ when there are 24080Sstevel@tonic-gate * duplicate calls to shutdown(). 24090Sstevel@tonic-gate */ 24100Sstevel@tonic-gate old_state = so->so_state; 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate switch (how) { 24130Sstevel@tonic-gate case 0: 24140Sstevel@tonic-gate socantrcvmore(so); 24150Sstevel@tonic-gate break; 24160Sstevel@tonic-gate case 1: 24170Sstevel@tonic-gate socantsendmore(so); 24180Sstevel@tonic-gate break; 24190Sstevel@tonic-gate case 2: 24200Sstevel@tonic-gate socantsendmore(so); 24210Sstevel@tonic-gate socantrcvmore(so); 24220Sstevel@tonic-gate break; 24230Sstevel@tonic-gate default: 24240Sstevel@tonic-gate error = EINVAL; 24250Sstevel@tonic-gate goto done; 24260Sstevel@tonic-gate } 24270Sstevel@tonic-gate 24280Sstevel@tonic-gate /* 24290Sstevel@tonic-gate * Assumes that the SS_CANT* flags are never cleared in the above code. 24300Sstevel@tonic-gate */ 24310Sstevel@tonic-gate state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) - 24325240Snordmark (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)); 24330Sstevel@tonic-gate ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0); 24340Sstevel@tonic-gate 24350Sstevel@tonic-gate switch (state_change) { 24360Sstevel@tonic-gate case 0: 24370Sstevel@tonic-gate dprintso(so, 1, 24380Sstevel@tonic-gate ("sotpi_shutdown: nothing to send in state 0x%x\n", 24390Sstevel@tonic-gate so->so_state)); 24400Sstevel@tonic-gate goto done; 24410Sstevel@tonic-gate 24420Sstevel@tonic-gate case SS_CANTRCVMORE: 24430Sstevel@tonic-gate mutex_exit(&so->so_lock); 24440Sstevel@tonic-gate strseteof(SOTOV(so), 1); 24450Sstevel@tonic-gate /* 24460Sstevel@tonic-gate * strseteof takes care of read side wakeups, 24470Sstevel@tonic-gate * pollwakeups, and signals. 24480Sstevel@tonic-gate */ 24490Sstevel@tonic-gate /* 24500Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems 24510Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 24520Sstevel@tonic-gate */ 24530Sstevel@tonic-gate mutex_enter(&so->so_lock); 24540Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 24550Sstevel@tonic-gate mutex_exit(&so->so_lock); 24560Sstevel@tonic-gate 24570Sstevel@tonic-gate /* Flush read side queue */ 24580Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL); 24590Sstevel@tonic-gate 24600Sstevel@tonic-gate mutex_enter(&so->so_lock); 24610Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 24620Sstevel@tonic-gate break; 24630Sstevel@tonic-gate 24640Sstevel@tonic-gate case SS_CANTSENDMORE: 24650Sstevel@tonic-gate mutex_exit(&so->so_lock); 24660Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 24670Sstevel@tonic-gate mutex_enter(&so->so_lock); 24680Sstevel@tonic-gate break; 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate case SS_CANTSENDMORE|SS_CANTRCVMORE: 24710Sstevel@tonic-gate mutex_exit(&so->so_lock); 24720Sstevel@tonic-gate strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 24730Sstevel@tonic-gate strseteof(SOTOV(so), 1); 24740Sstevel@tonic-gate /* 24750Sstevel@tonic-gate * strseteof takes care of read side wakeups, 24760Sstevel@tonic-gate * pollwakeups, and signals. 24770Sstevel@tonic-gate */ 24780Sstevel@tonic-gate /* 24790Sstevel@tonic-gate * Get the read lock before flushing data to avoid problems 24800Sstevel@tonic-gate * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 24810Sstevel@tonic-gate */ 24820Sstevel@tonic-gate mutex_enter(&so->so_lock); 24830Sstevel@tonic-gate (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 24840Sstevel@tonic-gate mutex_exit(&so->so_lock); 24850Sstevel@tonic-gate 24860Sstevel@tonic-gate /* Flush read side queue */ 24870Sstevel@tonic-gate strflushrq(SOTOV(so), FLUSHALL); 24880Sstevel@tonic-gate 24890Sstevel@tonic-gate mutex_enter(&so->so_lock); 24900Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 24910Sstevel@tonic-gate break; 24920Sstevel@tonic-gate } 24930Sstevel@tonic-gate 24940Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 24950Sstevel@tonic-gate 24960Sstevel@tonic-gate /* 24970Sstevel@tonic-gate * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them 24980Sstevel@tonic-gate * was set due to this call and the new state has both of them set: 24990Sstevel@tonic-gate * Send the AF_UNIX close indication 25000Sstevel@tonic-gate * For T_COTS send a discon_ind 25010Sstevel@tonic-gate * 25020Sstevel@tonic-gate * If cantsend was set due to this call: 25030Sstevel@tonic-gate * For T_COTSORD send an ordrel_ind 25040Sstevel@tonic-gate * 25050Sstevel@tonic-gate * Note that for T_CLTS there is no message sent here. 25060Sstevel@tonic-gate */ 25070Sstevel@tonic-gate if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) == 25080Sstevel@tonic-gate (SS_CANTRCVMORE|SS_CANTSENDMORE)) { 25090Sstevel@tonic-gate /* 25100Sstevel@tonic-gate * For SunOS 4.X compatibility we tell the other end 25110Sstevel@tonic-gate * that we are unable to receive at this point. 25120Sstevel@tonic-gate */ 25130Sstevel@tonic-gate if (so->so_family == AF_UNIX && so->so_serv_type != T_CLTS) 25140Sstevel@tonic-gate so_unix_close(so); 25150Sstevel@tonic-gate 25160Sstevel@tonic-gate if (so->so_serv_type == T_COTS) 25170Sstevel@tonic-gate error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD); 25180Sstevel@tonic-gate } 25190Sstevel@tonic-gate if ((state_change & SS_CANTSENDMORE) && 25200Sstevel@tonic-gate (so->so_serv_type == T_COTS_ORD)) { 25210Sstevel@tonic-gate /* Send an orderly release */ 25220Sstevel@tonic-gate ordrel_req.PRIM_type = T_ORDREL_REQ; 25230Sstevel@tonic-gate 25240Sstevel@tonic-gate mutex_exit(&so->so_lock); 25250Sstevel@tonic-gate mp = soallocproto1(&ordrel_req, sizeof (ordrel_req), 25260Sstevel@tonic-gate 0, _ALLOC_SLEEP); 25270Sstevel@tonic-gate /* 25280Sstevel@tonic-gate * Send down the T_ORDREL_REQ even if there is flow control. 25290Sstevel@tonic-gate * This prevents shutdown from blocking. 25300Sstevel@tonic-gate * Note that there is no T_OK_ACK for ordrel_req. 25310Sstevel@tonic-gate */ 25320Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 25335240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 25340Sstevel@tonic-gate mutex_enter(&so->so_lock); 25350Sstevel@tonic-gate if (error) { 25360Sstevel@tonic-gate eprintsoline(so, error); 25370Sstevel@tonic-gate goto done; 25380Sstevel@tonic-gate } 25390Sstevel@tonic-gate } 25400Sstevel@tonic-gate 25410Sstevel@tonic-gate done: 25420Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 25430Sstevel@tonic-gate mutex_exit(&so->so_lock); 25440Sstevel@tonic-gate return (error); 25450Sstevel@tonic-gate } 25460Sstevel@tonic-gate 25470Sstevel@tonic-gate /* 25480Sstevel@tonic-gate * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send 25490Sstevel@tonic-gate * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer 25500Sstevel@tonic-gate * that we have closed. 25510Sstevel@tonic-gate * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length 25520Sstevel@tonic-gate * T_UNITDATA_REQ containing the same option. 25530Sstevel@tonic-gate * 25540Sstevel@tonic-gate * For SOCK_DGRAM half-connections (somebody connected to this end 25550Sstevel@tonic-gate * but this end is not connect) we don't know where to send any 25560Sstevel@tonic-gate * SO_UNIX_CLOSE. 25570Sstevel@tonic-gate * 25580Sstevel@tonic-gate * We have to ignore stream head errors just in case there has been 25590Sstevel@tonic-gate * a shutdown(output). 25600Sstevel@tonic-gate * Ignore any flow control to try to get the message more quickly to the peer. 25610Sstevel@tonic-gate * While locally ignoring flow control solves the problem when there 25620Sstevel@tonic-gate * is only the loopback transport on the stream it would not provide 25630Sstevel@tonic-gate * the correct AF_UNIX socket semantics when one or more modules have 25640Sstevel@tonic-gate * been pushed. 25650Sstevel@tonic-gate */ 25660Sstevel@tonic-gate void 25670Sstevel@tonic-gate so_unix_close(struct sonode *so) 25680Sstevel@tonic-gate { 25690Sstevel@tonic-gate int error; 25700Sstevel@tonic-gate struct T_opthdr toh; 25710Sstevel@tonic-gate mblk_t *mp; 25720Sstevel@tonic-gate 25730Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 25740Sstevel@tonic-gate 25750Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 25760Sstevel@tonic-gate 25770Sstevel@tonic-gate if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) != 25780Sstevel@tonic-gate (SS_ISCONNECTED|SS_ISBOUND)) 25790Sstevel@tonic-gate return; 25800Sstevel@tonic-gate 25810Sstevel@tonic-gate dprintso(so, 1, ("so_unix_close(%p) %s\n", 2582*7240Srh87107 (void *)so, pr_state(so->so_state, so->so_mode))); 25830Sstevel@tonic-gate 25840Sstevel@tonic-gate toh.level = SOL_SOCKET; 25850Sstevel@tonic-gate toh.name = SO_UNIX_CLOSE; 25860Sstevel@tonic-gate 25870Sstevel@tonic-gate /* zero length + header */ 25880Sstevel@tonic-gate toh.len = (t_uscalar_t)sizeof (struct T_opthdr); 25890Sstevel@tonic-gate toh.status = 0; 25900Sstevel@tonic-gate 25910Sstevel@tonic-gate if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) { 25920Sstevel@tonic-gate struct T_optdata_req tdr; 25930Sstevel@tonic-gate 25940Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ; 25950Sstevel@tonic-gate tdr.DATA_flag = 0; 25960Sstevel@tonic-gate 25970Sstevel@tonic-gate tdr.OPT_length = (t_scalar_t)sizeof (toh); 25980Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 25990Sstevel@tonic-gate 26000Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 26010Sstevel@tonic-gate mp = soallocproto2(&tdr, sizeof (tdr), 26020Sstevel@tonic-gate &toh, sizeof (toh), 0, _ALLOC_SLEEP); 26030Sstevel@tonic-gate } else { 26040Sstevel@tonic-gate struct T_unitdata_req tudr; 26050Sstevel@tonic-gate void *addr; 26060Sstevel@tonic-gate socklen_t addrlen; 26070Sstevel@tonic-gate void *src; 26080Sstevel@tonic-gate socklen_t srclen; 26090Sstevel@tonic-gate struct T_opthdr toh2; 26100Sstevel@tonic-gate t_scalar_t size; 26110Sstevel@tonic-gate 26120Sstevel@tonic-gate /* Connecteded DGRAM socket */ 26130Sstevel@tonic-gate 26140Sstevel@tonic-gate /* 26150Sstevel@tonic-gate * For AF_UNIX the destination address is translated to 26160Sstevel@tonic-gate * an internal name and the source address is passed as 26170Sstevel@tonic-gate * an option. 26180Sstevel@tonic-gate */ 26190Sstevel@tonic-gate /* 26200Sstevel@tonic-gate * Length and family checks. 26210Sstevel@tonic-gate */ 26220Sstevel@tonic-gate error = so_addr_verify(so, so->so_faddr_sa, 26235240Snordmark (t_uscalar_t)so->so_faddr_len); 26240Sstevel@tonic-gate if (error) { 26250Sstevel@tonic-gate eprintsoline(so, error); 26260Sstevel@tonic-gate return; 26270Sstevel@tonic-gate } 26280Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 26290Sstevel@tonic-gate /* 26300Sstevel@tonic-gate * Already have a transport internal address. Do not 26310Sstevel@tonic-gate * pass any (transport internal) source address. 26320Sstevel@tonic-gate */ 26330Sstevel@tonic-gate addr = so->so_faddr_sa; 26340Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_faddr_len; 26350Sstevel@tonic-gate src = NULL; 26360Sstevel@tonic-gate srclen = 0; 26370Sstevel@tonic-gate } else { 26380Sstevel@tonic-gate /* 26390Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 26400Sstevel@tonic-gate * and translate the remote address. 26410Sstevel@tonic-gate * Holding so_lock thus so_laddr_sa can not change. 26420Sstevel@tonic-gate */ 26430Sstevel@tonic-gate src = so->so_laddr_sa; 26440Sstevel@tonic-gate srclen = (socklen_t)so->so_laddr_len; 26450Sstevel@tonic-gate dprintso(so, 1, 26465240Snordmark ("so_ux_close: srclen %d, src %p\n", 26475240Snordmark srclen, src)); 26480Sstevel@tonic-gate error = so_ux_addr_xlate(so, 26495240Snordmark so->so_faddr_sa, 26505240Snordmark (socklen_t)so->so_faddr_len, 0, 26515240Snordmark &addr, &addrlen); 26520Sstevel@tonic-gate if (error) { 26530Sstevel@tonic-gate eprintsoline(so, error); 26540Sstevel@tonic-gate return; 26550Sstevel@tonic-gate } 26560Sstevel@tonic-gate } 26570Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 26580Sstevel@tonic-gate tudr.DEST_length = addrlen; 26590Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 26600Sstevel@tonic-gate if (srclen == 0) { 26610Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)sizeof (toh); 26620Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 26635240Snordmark _TPI_ALIGN_TOPT(addrlen)); 26640Sstevel@tonic-gate 26650Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 26660Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 26670Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 26680Sstevel@tonic-gate addr, addrlen, size, _ALLOC_SLEEP); 26690Sstevel@tonic-gate mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen); 26700Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 26710Sstevel@tonic-gate } else { 26720Sstevel@tonic-gate /* 26730Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a 26740Sstevel@tonic-gate * source address option. 26750Sstevel@tonic-gate */ 26760Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) + 26770Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen)); 26780Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 26790Sstevel@tonic-gate _TPI_ALIGN_TOPT(addrlen)); 26800Sstevel@tonic-gate 26810Sstevel@tonic-gate toh2.level = SOL_SOCKET; 26820Sstevel@tonic-gate toh2.name = SO_SRCADDR; 26830Sstevel@tonic-gate toh2.len = (t_uscalar_t)(srclen + 26845240Snordmark sizeof (struct T_opthdr)); 26850Sstevel@tonic-gate toh2.status = 0; 26860Sstevel@tonic-gate 26870Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 26880Sstevel@tonic-gate 26890Sstevel@tonic-gate /* NOTE: holding so_lock while sleeping */ 26900Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 26910Sstevel@tonic-gate addr, addrlen, size, _ALLOC_SLEEP); 26920Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 26930Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 26940Sstevel@tonic-gate soappendmsg(mp, &toh2, sizeof (toh2)); 26950Sstevel@tonic-gate soappendmsg(mp, src, srclen); 26960Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 26970Sstevel@tonic-gate } 26980Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 26990Sstevel@tonic-gate } 27000Sstevel@tonic-gate mutex_exit(&so->so_lock); 27010Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 27025240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 27030Sstevel@tonic-gate mutex_enter(&so->so_lock); 27040Sstevel@tonic-gate } 27050Sstevel@tonic-gate 27060Sstevel@tonic-gate /* 27070Sstevel@tonic-gate * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK. 27080Sstevel@tonic-gate */ 27090Sstevel@tonic-gate int 27100Sstevel@tonic-gate sorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags) 27110Sstevel@tonic-gate { 27120Sstevel@tonic-gate mblk_t *mp, *nmp; 27130Sstevel@tonic-gate int error; 27140Sstevel@tonic-gate 2715*7240Srh87107 dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", 2716*7240Srh87107 (void *)so, (void *)msg, flags)); 27170Sstevel@tonic-gate 27180Sstevel@tonic-gate /* 27190Sstevel@tonic-gate * There is never any oob data with addresses or control since 27200Sstevel@tonic-gate * the T_EXDATA_IND does not carry any options. 27210Sstevel@tonic-gate */ 27220Sstevel@tonic-gate msg->msg_controllen = 0; 27230Sstevel@tonic-gate msg->msg_namelen = 0; 27240Sstevel@tonic-gate 27250Sstevel@tonic-gate mutex_enter(&so->so_lock); 27260Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 27270Sstevel@tonic-gate if ((so->so_options & SO_OOBINLINE) || 27280Sstevel@tonic-gate (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) { 27290Sstevel@tonic-gate dprintso(so, 1, ("sorecvoob: inline or data consumed\n")); 27300Sstevel@tonic-gate mutex_exit(&so->so_lock); 27310Sstevel@tonic-gate return (EINVAL); 27320Sstevel@tonic-gate } 27330Sstevel@tonic-gate if (!(so->so_state & SS_HAVEOOBDATA)) { 27340Sstevel@tonic-gate dprintso(so, 1, ("sorecvoob: no data yet\n")); 27350Sstevel@tonic-gate mutex_exit(&so->so_lock); 27360Sstevel@tonic-gate return (EWOULDBLOCK); 27370Sstevel@tonic-gate } 27380Sstevel@tonic-gate ASSERT(so->so_oobmsg != NULL); 27390Sstevel@tonic-gate mp = so->so_oobmsg; 27400Sstevel@tonic-gate if (flags & MSG_PEEK) { 27410Sstevel@tonic-gate /* 27420Sstevel@tonic-gate * Since recv* can not return ENOBUFS we can not use dupmsg. 27430Sstevel@tonic-gate * Instead we revert to the consolidation private 27440Sstevel@tonic-gate * allocb_wait plus bcopy. 27450Sstevel@tonic-gate */ 27460Sstevel@tonic-gate mblk_t *mp1; 27470Sstevel@tonic-gate 27480Sstevel@tonic-gate mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL); 27490Sstevel@tonic-gate ASSERT(mp1); 27500Sstevel@tonic-gate 27510Sstevel@tonic-gate while (mp != NULL) { 27520Sstevel@tonic-gate ssize_t size; 27530Sstevel@tonic-gate 27540Sstevel@tonic-gate size = MBLKL(mp); 27550Sstevel@tonic-gate bcopy(mp->b_rptr, mp1->b_wptr, size); 27560Sstevel@tonic-gate mp1->b_wptr += size; 27570Sstevel@tonic-gate ASSERT(mp1->b_wptr <= mp1->b_datap->db_lim); 27580Sstevel@tonic-gate mp = mp->b_cont; 27590Sstevel@tonic-gate } 27600Sstevel@tonic-gate mp = mp1; 27610Sstevel@tonic-gate } else { 27620Sstevel@tonic-gate /* 27630Sstevel@tonic-gate * Update the state indicating that the data has been consumed. 27640Sstevel@tonic-gate * Keep SS_OOBPEND set until data is consumed past the mark. 27650Sstevel@tonic-gate */ 27660Sstevel@tonic-gate so->so_oobmsg = NULL; 27670Sstevel@tonic-gate so->so_state ^= SS_HAVEOOBDATA|SS_HADOOBDATA; 27680Sstevel@tonic-gate } 27690Sstevel@tonic-gate dprintso(so, 1, 27705240Snordmark ("after recvoob(%p): counts %d/%d state %s\n", 2771*7240Srh87107 (void *)so, so->so_oobsigcnt, 27725240Snordmark so->so_oobcnt, pr_state(so->so_state, so->so_mode))); 27730Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 27740Sstevel@tonic-gate mutex_exit(&so->so_lock); 27750Sstevel@tonic-gate 27760Sstevel@tonic-gate error = 0; 27770Sstevel@tonic-gate nmp = mp; 27780Sstevel@tonic-gate while (nmp != NULL && uiop->uio_resid > 0) { 27790Sstevel@tonic-gate ssize_t n = MBLKL(nmp); 27800Sstevel@tonic-gate 27810Sstevel@tonic-gate n = MIN(n, uiop->uio_resid); 27820Sstevel@tonic-gate if (n > 0) 27830Sstevel@tonic-gate error = uiomove(nmp->b_rptr, n, 27845240Snordmark UIO_READ, uiop); 27850Sstevel@tonic-gate if (error) 27860Sstevel@tonic-gate break; 27870Sstevel@tonic-gate nmp = nmp->b_cont; 27880Sstevel@tonic-gate } 27890Sstevel@tonic-gate freemsg(mp); 27900Sstevel@tonic-gate return (error); 27910Sstevel@tonic-gate } 27920Sstevel@tonic-gate 27930Sstevel@tonic-gate /* 27940Sstevel@tonic-gate * Called by sotpi_recvmsg when reading a non-zero amount of data. 27950Sstevel@tonic-gate * In addition, the caller typically verifies that there is some 27960Sstevel@tonic-gate * potential state to clear by checking 27970Sstevel@tonic-gate * if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) 27980Sstevel@tonic-gate * before calling this routine. 27990Sstevel@tonic-gate * Note that such a check can be made without holding so_lock since 28000Sstevel@tonic-gate * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg 28010Sstevel@tonic-gate * decrements so_oobsigcnt. 28020Sstevel@tonic-gate * 28030Sstevel@tonic-gate * When data is read *after* the point that all pending 28040Sstevel@tonic-gate * oob data has been consumed the oob indication is cleared. 28050Sstevel@tonic-gate * 28060Sstevel@tonic-gate * This logic keeps select/poll returning POLLRDBAND and 28070Sstevel@tonic-gate * SIOCATMARK returning true until we have read past 28080Sstevel@tonic-gate * the mark. 28090Sstevel@tonic-gate */ 28100Sstevel@tonic-gate static void 28110Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so) 28120Sstevel@tonic-gate { 28130Sstevel@tonic-gate mutex_enter(&so->so_lock); 28140Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 28150Sstevel@tonic-gate dprintso(so, 1, 28165240Snordmark ("sorecv_update_oobstate: counts %d/%d state %s\n", 28175240Snordmark so->so_oobsigcnt, 28185240Snordmark so->so_oobcnt, pr_state(so->so_state, so->so_mode))); 28190Sstevel@tonic-gate if (so->so_oobsigcnt == 0) { 28200Sstevel@tonic-gate /* No more pending oob indications */ 28210Sstevel@tonic-gate so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK); 28220Sstevel@tonic-gate freemsg(so->so_oobmsg); 28230Sstevel@tonic-gate so->so_oobmsg = NULL; 28240Sstevel@tonic-gate } 28250Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 28260Sstevel@tonic-gate mutex_exit(&so->so_lock); 28270Sstevel@tonic-gate } 28280Sstevel@tonic-gate 28290Sstevel@tonic-gate /* 28300Sstevel@tonic-gate * Handle recv* calls for an so which has NL7C saved recv mblk_t(s). 28310Sstevel@tonic-gate */ 28320Sstevel@tonic-gate static int 28330Sstevel@tonic-gate nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp) 28340Sstevel@tonic-gate { 28350Sstevel@tonic-gate int error = 0; 28360Sstevel@tonic-gate mblk_t *tmp = NULL; 28370Sstevel@tonic-gate mblk_t *pmp = NULL; 28380Sstevel@tonic-gate mblk_t *nmp = so->so_nl7c_rcv_mp; 28390Sstevel@tonic-gate 28400Sstevel@tonic-gate ASSERT(nmp != NULL); 28410Sstevel@tonic-gate 28420Sstevel@tonic-gate while (nmp != NULL && uiop->uio_resid > 0) { 28430Sstevel@tonic-gate ssize_t n; 28440Sstevel@tonic-gate 28450Sstevel@tonic-gate if (DB_TYPE(nmp) == M_DATA) { 28460Sstevel@tonic-gate /* 28470Sstevel@tonic-gate * We have some data, uiomove up to resid bytes. 28480Sstevel@tonic-gate */ 28490Sstevel@tonic-gate n = MIN(MBLKL(nmp), uiop->uio_resid); 28500Sstevel@tonic-gate if (n > 0) 28510Sstevel@tonic-gate error = uiomove(nmp->b_rptr, n, UIO_READ, uiop); 28520Sstevel@tonic-gate nmp->b_rptr += n; 28530Sstevel@tonic-gate if (nmp->b_rptr == nmp->b_wptr) { 28540Sstevel@tonic-gate pmp = nmp; 28550Sstevel@tonic-gate nmp = nmp->b_cont; 28560Sstevel@tonic-gate } 28571974Sbrutus if (error) 28581974Sbrutus break; 28590Sstevel@tonic-gate } else { 28600Sstevel@tonic-gate /* 28610Sstevel@tonic-gate * We only handle data, save for caller to handle. 28620Sstevel@tonic-gate */ 28630Sstevel@tonic-gate if (pmp != NULL) { 28640Sstevel@tonic-gate pmp->b_cont = nmp->b_cont; 28650Sstevel@tonic-gate } 28660Sstevel@tonic-gate nmp->b_cont = NULL; 28670Sstevel@tonic-gate if (*rmp == NULL) { 28680Sstevel@tonic-gate *rmp = nmp; 28690Sstevel@tonic-gate } else { 28701974Sbrutus tmp->b_cont = nmp; 28710Sstevel@tonic-gate } 28720Sstevel@tonic-gate nmp = nmp->b_cont; 28730Sstevel@tonic-gate tmp = nmp; 28740Sstevel@tonic-gate } 28750Sstevel@tonic-gate } 28760Sstevel@tonic-gate if (pmp != NULL) { 28770Sstevel@tonic-gate /* Free any mblk_t(s) which we have consumed */ 28780Sstevel@tonic-gate pmp->b_cont = NULL; 28790Sstevel@tonic-gate freemsg(so->so_nl7c_rcv_mp); 28800Sstevel@tonic-gate } 28810Sstevel@tonic-gate if ((so->so_nl7c_rcv_mp = nmp) == NULL) { 28821974Sbrutus /* Last mblk_t so return the saved kstrgetmsg() rval/error */ 28831974Sbrutus if (error == 0) { 28841974Sbrutus rval_t *p = (rval_t *)&so->so_nl7c_rcv_rval; 28851974Sbrutus 28861974Sbrutus error = p->r_v.r_v2; 28871974Sbrutus p->r_v.r_v2 = 0; 28881974Sbrutus } 28890Sstevel@tonic-gate rp->r_vals = so->so_nl7c_rcv_rval; 28900Sstevel@tonic-gate so->so_nl7c_rcv_rval = 0; 28910Sstevel@tonic-gate } else { 28920Sstevel@tonic-gate /* More mblk_t(s) to process so no rval to return */ 28930Sstevel@tonic-gate rp->r_vals = 0; 28940Sstevel@tonic-gate } 28950Sstevel@tonic-gate return (error); 28960Sstevel@tonic-gate } 28970Sstevel@tonic-gate 28980Sstevel@tonic-gate /* 28990Sstevel@tonic-gate * Receive the next message on the queue. 29000Sstevel@tonic-gate * If msg_controllen is non-zero when called the caller is interested in 29010Sstevel@tonic-gate * any received control info (options). 29020Sstevel@tonic-gate * If msg_namelen is non-zero when called the caller is interested in 29030Sstevel@tonic-gate * any received source address. 29040Sstevel@tonic-gate * The routine returns with msg_control and msg_name pointing to 29050Sstevel@tonic-gate * kmem_alloc'ed memory which the caller has to free. 29060Sstevel@tonic-gate */ 29070Sstevel@tonic-gate int 29080Sstevel@tonic-gate sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop) 29090Sstevel@tonic-gate { 29100Sstevel@tonic-gate union T_primitives *tpr; 29110Sstevel@tonic-gate mblk_t *mp; 29120Sstevel@tonic-gate uchar_t pri; 29130Sstevel@tonic-gate int pflag, opflag; 29140Sstevel@tonic-gate void *control; 29150Sstevel@tonic-gate t_uscalar_t controllen; 29160Sstevel@tonic-gate t_uscalar_t namelen; 29170Sstevel@tonic-gate int so_state = so->so_state; /* Snapshot */ 29180Sstevel@tonic-gate ssize_t saved_resid; 29190Sstevel@tonic-gate rval_t rval; 29200Sstevel@tonic-gate int flags; 29210Sstevel@tonic-gate clock_t timout; 29220Sstevel@tonic-gate int first; 29236707Sbrutus int error = 0; 29246707Sbrutus struct uio *suiop = NULL; 29256707Sbrutus sodirect_t *sodp = so->so_direct; 29260Sstevel@tonic-gate 29270Sstevel@tonic-gate flags = msg->msg_flags; 29280Sstevel@tonic-gate msg->msg_flags = 0; 29290Sstevel@tonic-gate 29300Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n", 2931*7240Srh87107 (void *)so, (void *)msg, flags, 29325240Snordmark pr_state(so->so_state, so->so_mode), so->so_error)); 29330Sstevel@tonic-gate 29340Sstevel@tonic-gate /* 29350Sstevel@tonic-gate * If we are not connected because we have never been connected 29360Sstevel@tonic-gate * we return ENOTCONN. If we have been connected (but are no longer 29370Sstevel@tonic-gate * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return 29380Sstevel@tonic-gate * the EOF. 29390Sstevel@tonic-gate * 29400Sstevel@tonic-gate * An alternative would be to post an ENOTCONN error in stream head 29410Sstevel@tonic-gate * (read+write) and clear it when we're connected. However, that error 29420Sstevel@tonic-gate * would cause incorrect poll/select behavior! 29430Sstevel@tonic-gate */ 29440Sstevel@tonic-gate if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 && 29450Sstevel@tonic-gate (so->so_mode & SM_CONNREQUIRED)) { 29460Sstevel@tonic-gate return (ENOTCONN); 29470Sstevel@tonic-gate } 29480Sstevel@tonic-gate 29490Sstevel@tonic-gate /* 29500Sstevel@tonic-gate * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but 29510Sstevel@tonic-gate * after checking that the read queue is empty) and returns zero. 29520Sstevel@tonic-gate * This implementation will sleep (in kstrgetmsg) even if uio_resid 29530Sstevel@tonic-gate * is zero. 29540Sstevel@tonic-gate */ 29550Sstevel@tonic-gate 29560Sstevel@tonic-gate if (flags & MSG_OOB) { 29570Sstevel@tonic-gate /* Check that the transport supports OOB */ 29580Sstevel@tonic-gate if (!(so->so_mode & SM_EXDATA)) 29590Sstevel@tonic-gate return (EOPNOTSUPP); 29600Sstevel@tonic-gate return (sorecvoob(so, msg, uiop, flags)); 29610Sstevel@tonic-gate } 29620Sstevel@tonic-gate 29630Sstevel@tonic-gate /* 29640Sstevel@tonic-gate * Set msg_controllen and msg_namelen to zero here to make it 29650Sstevel@tonic-gate * simpler in the cases that no control or name is returned. 29660Sstevel@tonic-gate */ 29670Sstevel@tonic-gate controllen = msg->msg_controllen; 29680Sstevel@tonic-gate namelen = msg->msg_namelen; 29690Sstevel@tonic-gate msg->msg_controllen = 0; 29700Sstevel@tonic-gate msg->msg_namelen = 0; 29710Sstevel@tonic-gate 29720Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n", 29735240Snordmark namelen, controllen)); 29740Sstevel@tonic-gate 29751974Sbrutus mutex_enter(&so->so_lock); 29760Sstevel@tonic-gate /* 29770Sstevel@tonic-gate * If an NL7C enabled socket and not waiting for write data. 29780Sstevel@tonic-gate */ 29791974Sbrutus if ((so->so_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) == 29800Sstevel@tonic-gate NL7C_ENABLED) { 29810Sstevel@tonic-gate if (so->so_nl7c_uri) { 29821974Sbrutus /* Close uri processing for a previous request */ 29830Sstevel@tonic-gate nl7c_close(so); 29840Sstevel@tonic-gate } 29851974Sbrutus if ((so_state & SS_CANTRCVMORE) && so->so_nl7c_rcv_mp == NULL) { 29861974Sbrutus /* Nothing to process, EOF */ 29871974Sbrutus mutex_exit(&so->so_lock); 29881974Sbrutus return (0); 29891974Sbrutus } else if (so->so_nl7c_flags & NL7C_SOPERSIST) { 29901974Sbrutus /* Persistent NL7C socket, try to process request */ 29911974Sbrutus boolean_t ret; 29921974Sbrutus 29931974Sbrutus ret = nl7c_process(so, 29941974Sbrutus (so->so_state & (SS_NONBLOCK|SS_NDELAY))); 29951974Sbrutus rval.r_vals = so->so_nl7c_rcv_rval; 29961974Sbrutus error = rval.r_v.r_v2; 29971974Sbrutus if (error) { 29981974Sbrutus /* Error of some sort, return it */ 29991974Sbrutus mutex_exit(&so->so_lock); 30001974Sbrutus return (error); 30011974Sbrutus } 30021974Sbrutus if (so->so_nl7c_flags && 30031974Sbrutus ! (so->so_nl7c_flags & NL7C_WAITWRITE)) { 30041974Sbrutus /* 30051974Sbrutus * Still an NL7C socket and no data 30061974Sbrutus * to pass up to the caller. 30071974Sbrutus */ 30081974Sbrutus mutex_exit(&so->so_lock); 30091974Sbrutus if (ret) { 30101974Sbrutus /* EOF */ 30111974Sbrutus return (0); 30121974Sbrutus } else { 30131974Sbrutus /* Need more data */ 30141974Sbrutus return (EAGAIN); 30151974Sbrutus } 30161974Sbrutus } 30171974Sbrutus } else { 30180Sstevel@tonic-gate /* 30191974Sbrutus * Not persistent so no further NL7C processing. 30200Sstevel@tonic-gate */ 30210Sstevel@tonic-gate so->so_nl7c_flags = 0; 30220Sstevel@tonic-gate } 30230Sstevel@tonic-gate } 30240Sstevel@tonic-gate /* 30250Sstevel@tonic-gate * Only one reader is allowed at any given time. This is needed 30260Sstevel@tonic-gate * for T_EXDATA handling and, in the future, MSG_WAITALL. 30270Sstevel@tonic-gate * 30280Sstevel@tonic-gate * This is slightly different that BSD behavior in that it fails with 30290Sstevel@tonic-gate * EWOULDBLOCK when using nonblocking io. In BSD the read queue access 30300Sstevel@tonic-gate * is single-threaded using sblock(), which is dropped while waiting 30310Sstevel@tonic-gate * for data to appear. The difference shows up e.g. if one 30320Sstevel@tonic-gate * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor 30330Sstevel@tonic-gate * does use nonblocking io and different threads are reading each 30340Sstevel@tonic-gate * file descriptor. In BSD there would never be an EWOULDBLOCK error 30350Sstevel@tonic-gate * in this case as long as the read queue doesn't get empty. 30360Sstevel@tonic-gate * In this implementation the thread using nonblocking io can 30370Sstevel@tonic-gate * get an EWOULDBLOCK error due to the blocking thread executing 30380Sstevel@tonic-gate * e.g. in the uiomove in kstrgetmsg. 30390Sstevel@tonic-gate * This difference is not believed to be significant. 30400Sstevel@tonic-gate */ 30413749Sethindra /* Set SOREADLOCKED */ 30423749Sethindra error = so_lock_read_intr(so, 30433749Sethindra uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0)); 30440Sstevel@tonic-gate mutex_exit(&so->so_lock); 30450Sstevel@tonic-gate if (error) 30460Sstevel@tonic-gate return (error); 30470Sstevel@tonic-gate 30480Sstevel@tonic-gate /* 30490Sstevel@tonic-gate * Tell kstrgetmsg to not inspect the stream head errors until all 30500Sstevel@tonic-gate * queued data has been consumed. 30510Sstevel@tonic-gate * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set. 30520Sstevel@tonic-gate * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block. 30530Sstevel@tonic-gate * 30540Sstevel@tonic-gate * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and 30550Sstevel@tonic-gate * to T_OPTDATA_IND that do not contain any user-visible control msg. 30560Sstevel@tonic-gate * Note that MSG_WAITALL set with MSG_PEEK is a noop. 30570Sstevel@tonic-gate */ 30580Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR; 30590Sstevel@tonic-gate if (flags & MSG_PEEK) { 30600Sstevel@tonic-gate pflag |= MSG_IPEEK; 30610Sstevel@tonic-gate flags &= ~MSG_WAITALL; 30620Sstevel@tonic-gate } 30630Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC) 30640Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL; 30650Sstevel@tonic-gate 30660Sstevel@tonic-gate if (flags & MSG_DONTWAIT) 30670Sstevel@tonic-gate timout = 0; 30680Sstevel@tonic-gate else 30690Sstevel@tonic-gate timout = -1; 30700Sstevel@tonic-gate opflag = pflag; 30710Sstevel@tonic-gate first = 1; 30720Sstevel@tonic-gate 30736707Sbrutus if (uiop->uio_resid >= uioasync.mincnt && 30746707Sbrutus sodp != NULL && (sodp->sod_state & SOD_ENABLED) && 30756707Sbrutus uioasync.enabled && !(flags & MSG_PEEK) && 30766707Sbrutus !(so_state & SS_CANTRCVMORE)) { 30776707Sbrutus /* 30786707Sbrutus * Big enough I/O for uioa min setup and an sodirect socket 30796707Sbrutus * and sodirect enabled and uioa enabled and I/O will be done 30806707Sbrutus * and not EOF so initialize the sodirect_t uioa_t with "uiop". 30816707Sbrutus */ 30826707Sbrutus mutex_enter(sodp->sod_lock); 30836707Sbrutus if (!uioainit(uiop, &sodp->sod_uioa)) { 30846707Sbrutus /* 30856707Sbrutus * Successful uioainit() so the uio_t part of the 30866707Sbrutus * uioa_t will be used for all uio_t work to follow, 30876707Sbrutus * we save the original "uiop" in "suiop". 30886707Sbrutus */ 30896707Sbrutus suiop = uiop; 30906707Sbrutus uiop = (uio_t *)&sodp->sod_uioa; 30916707Sbrutus /* 30926707Sbrutus * Before returning to the caller the passed in uio_t 30936707Sbrutus * "uiop" will be updated via a call to uioafini() 30946707Sbrutus * below. 30956707Sbrutus * 30966707Sbrutus * Note, the uioa.uioa_state isn't set to UIOA_ENABLED 30976707Sbrutus * here as first we have to uioamove() any currently 30986707Sbrutus * queued M_DATA mblk_t(s) so it will be done in 30996707Sbrutus * kstrgetmsg(). 31006707Sbrutus */ 31016707Sbrutus } 31026707Sbrutus /* 31036707Sbrutus * In either uioainit() success or not case note the number 31046707Sbrutus * of uio bytes the caller wants for sod framework and/or 31056707Sbrutus * transport (e.g. TCP) strategy. 31066707Sbrutus */ 31076707Sbrutus sodp->sod_want = uiop->uio_resid; 31086707Sbrutus mutex_exit(sodp->sod_lock); 31096707Sbrutus } else if (sodp != NULL && (sodp->sod_state & SOD_ENABLED)) { 31106707Sbrutus /* 31116707Sbrutus * No uioa but still using sodirect so note the number of 31126707Sbrutus * uio bytes the caller wants for sodirect framework and/or 31136707Sbrutus * transport (e.g. TCP) strategy. 31146707Sbrutus * 31156707Sbrutus * Note, sod_lock not held, only writer is in this function 31166707Sbrutus * and only one thread at a time so not needed just to init. 31176707Sbrutus */ 31186707Sbrutus sodp->sod_want = uiop->uio_resid; 31196707Sbrutus } 31200Sstevel@tonic-gate retry: 31210Sstevel@tonic-gate saved_resid = uiop->uio_resid; 31220Sstevel@tonic-gate pri = 0; 31230Sstevel@tonic-gate mp = NULL; 31240Sstevel@tonic-gate if (so->so_nl7c_rcv_mp != NULL) { 31251974Sbrutus /* Already kstrgetmsg()ed saved mblk(s) from NL7C */ 31260Sstevel@tonic-gate error = nl7c_sorecv(so, &mp, uiop, &rval); 31270Sstevel@tonic-gate } else { 31280Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag, 31290Sstevel@tonic-gate timout, &rval); 31300Sstevel@tonic-gate } 31310Sstevel@tonic-gate if (error) { 31320Sstevel@tonic-gate switch (error) { 31330Sstevel@tonic-gate case EINTR: 31340Sstevel@tonic-gate case EWOULDBLOCK: 31350Sstevel@tonic-gate if (!first) 31360Sstevel@tonic-gate error = 0; 31370Sstevel@tonic-gate break; 31380Sstevel@tonic-gate case ETIME: 31390Sstevel@tonic-gate /* Returned from kstrgetmsg when timeout expires */ 31400Sstevel@tonic-gate if (!first) 31410Sstevel@tonic-gate error = 0; 31420Sstevel@tonic-gate else 31430Sstevel@tonic-gate error = EWOULDBLOCK; 31440Sstevel@tonic-gate break; 31450Sstevel@tonic-gate default: 31460Sstevel@tonic-gate eprintsoline(so, error); 31470Sstevel@tonic-gate break; 31480Sstevel@tonic-gate } 31496707Sbrutus goto out; 31500Sstevel@tonic-gate } 31510Sstevel@tonic-gate /* 31520Sstevel@tonic-gate * For datagrams the MOREDATA flag is used to set MSG_TRUNC. 31530Sstevel@tonic-gate * For non-datagrams MOREDATA is used to set MSG_EOR. 31540Sstevel@tonic-gate */ 31550Sstevel@tonic-gate ASSERT(!(rval.r_val1 & MORECTL)); 31560Sstevel@tonic-gate if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC)) 31570Sstevel@tonic-gate msg->msg_flags |= MSG_TRUNC; 31580Sstevel@tonic-gate 31590Sstevel@tonic-gate if (mp == NULL) { 31600Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n")); 31610Sstevel@tonic-gate /* 31620Sstevel@tonic-gate * 4.3BSD and 4.4BSD clears the mark when peeking across it. 31630Sstevel@tonic-gate * The draft Posix socket spec states that the mark should 31640Sstevel@tonic-gate * not be cleared when peeking. We follow the latter. 31650Sstevel@tonic-gate */ 31660Sstevel@tonic-gate if ((so->so_state & 31670Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 31680Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 31690Sstevel@tonic-gate !(flags & MSG_PEEK)) { 31700Sstevel@tonic-gate sorecv_update_oobstate(so); 31710Sstevel@tonic-gate } 31720Sstevel@tonic-gate 31730Sstevel@tonic-gate mutex_enter(&so->so_lock); 31740Sstevel@tonic-gate /* Set MSG_EOR based on MOREDATA */ 31750Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) { 31760Sstevel@tonic-gate if (so->so_state & SS_SAVEDEOR) { 31770Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 31780Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 31790Sstevel@tonic-gate } 31800Sstevel@tonic-gate } 31810Sstevel@tonic-gate /* 31820Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 31830Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 31840Sstevel@tonic-gate */ 31850Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 31860Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 31870Sstevel@tonic-gate mutex_exit(&so->so_lock); 31880Sstevel@tonic-gate first = 0; 31890Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 31900Sstevel@tonic-gate goto retry; 31910Sstevel@tonic-gate } 31926707Sbrutus goto out_locked; 31930Sstevel@tonic-gate } 31940Sstevel@tonic-gate 31950Sstevel@tonic-gate /* strsock_proto has already verified length and alignment */ 31960Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr; 31970Sstevel@tonic-gate dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type)); 31980Sstevel@tonic-gate 31990Sstevel@tonic-gate switch (tpr->type) { 32000Sstevel@tonic-gate case T_DATA_IND: { 32010Sstevel@tonic-gate if ((so->so_state & 32020Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 32030Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 32040Sstevel@tonic-gate !(flags & MSG_PEEK)) { 32050Sstevel@tonic-gate sorecv_update_oobstate(so); 32060Sstevel@tonic-gate } 32070Sstevel@tonic-gate 32080Sstevel@tonic-gate /* 32090Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on 32100Sstevel@tonic-gate * MORE_flag and MOREDATA. 32110Sstevel@tonic-gate */ 32120Sstevel@tonic-gate mutex_enter(&so->so_lock); 32130Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 32140Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) { 32150Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) 32160Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 32170Sstevel@tonic-gate else 32180Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR; 32190Sstevel@tonic-gate } 32200Sstevel@tonic-gate freemsg(mp); 32210Sstevel@tonic-gate /* 32220Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 32230Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 32240Sstevel@tonic-gate */ 32250Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 32260Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 32270Sstevel@tonic-gate mutex_exit(&so->so_lock); 32280Sstevel@tonic-gate first = 0; 32290Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 32300Sstevel@tonic-gate goto retry; 32310Sstevel@tonic-gate } 32326707Sbrutus goto out_locked; 32330Sstevel@tonic-gate } 32340Sstevel@tonic-gate case T_UNITDATA_IND: { 32350Sstevel@tonic-gate void *addr; 32360Sstevel@tonic-gate t_uscalar_t addrlen; 32370Sstevel@tonic-gate void *abuf; 32380Sstevel@tonic-gate t_uscalar_t optlen; 32390Sstevel@tonic-gate void *opt; 32400Sstevel@tonic-gate 32410Sstevel@tonic-gate if ((so->so_state & 32420Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 32430Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 32440Sstevel@tonic-gate !(flags & MSG_PEEK)) { 32450Sstevel@tonic-gate sorecv_update_oobstate(so); 32460Sstevel@tonic-gate } 32470Sstevel@tonic-gate 32480Sstevel@tonic-gate if (namelen != 0) { 32490Sstevel@tonic-gate /* Caller wants source address */ 32500Sstevel@tonic-gate addrlen = tpr->unitdata_ind.SRC_length; 32510Sstevel@tonic-gate addr = sogetoff(mp, 32525240Snordmark tpr->unitdata_ind.SRC_offset, 32535240Snordmark addrlen, 1); 32540Sstevel@tonic-gate if (addr == NULL) { 32550Sstevel@tonic-gate freemsg(mp); 32560Sstevel@tonic-gate error = EPROTO; 32570Sstevel@tonic-gate eprintsoline(so, error); 32586707Sbrutus goto out; 32590Sstevel@tonic-gate } 32600Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 32610Sstevel@tonic-gate /* 32620Sstevel@tonic-gate * Can not use the transport level address. 32630Sstevel@tonic-gate * If there is a SO_SRCADDR option carrying 32640Sstevel@tonic-gate * the socket level address it will be 32650Sstevel@tonic-gate * extracted below. 32660Sstevel@tonic-gate */ 32670Sstevel@tonic-gate addr = NULL; 32680Sstevel@tonic-gate addrlen = 0; 32690Sstevel@tonic-gate } 32700Sstevel@tonic-gate } 32710Sstevel@tonic-gate optlen = tpr->unitdata_ind.OPT_length; 32720Sstevel@tonic-gate if (optlen != 0) { 32730Sstevel@tonic-gate t_uscalar_t ncontrollen; 32740Sstevel@tonic-gate 32750Sstevel@tonic-gate /* 32760Sstevel@tonic-gate * Extract any source address option. 32770Sstevel@tonic-gate * Determine how large cmsg buffer is needed. 32780Sstevel@tonic-gate */ 32790Sstevel@tonic-gate opt = sogetoff(mp, 32805240Snordmark tpr->unitdata_ind.OPT_offset, 32815240Snordmark optlen, __TPI_ALIGN_SIZE); 32820Sstevel@tonic-gate 32830Sstevel@tonic-gate if (opt == NULL) { 32840Sstevel@tonic-gate freemsg(mp); 32850Sstevel@tonic-gate error = EPROTO; 32860Sstevel@tonic-gate eprintsoline(so, error); 32876707Sbrutus goto out; 32880Sstevel@tonic-gate } 32890Sstevel@tonic-gate if (so->so_family == AF_UNIX) 32900Sstevel@tonic-gate so_getopt_srcaddr(opt, optlen, &addr, &addrlen); 32910Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen, 32925240Snordmark !(flags & MSG_XPG4_2)); 32930Sstevel@tonic-gate if (controllen != 0) 32940Sstevel@tonic-gate controllen = ncontrollen; 32950Sstevel@tonic-gate else if (ncontrollen != 0) 32960Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC; 32970Sstevel@tonic-gate } else { 32980Sstevel@tonic-gate controllen = 0; 32990Sstevel@tonic-gate } 33000Sstevel@tonic-gate 33010Sstevel@tonic-gate if (namelen != 0) { 33020Sstevel@tonic-gate /* 33030Sstevel@tonic-gate * Return address to caller. 33040Sstevel@tonic-gate * Caller handles truncation if length 33050Sstevel@tonic-gate * exceeds msg_namelen. 33060Sstevel@tonic-gate * NOTE: AF_UNIX NUL termination is ensured by 33070Sstevel@tonic-gate * the sender's copyin_name(). 33080Sstevel@tonic-gate */ 33090Sstevel@tonic-gate abuf = kmem_alloc(addrlen, KM_SLEEP); 33100Sstevel@tonic-gate 33110Sstevel@tonic-gate bcopy(addr, abuf, addrlen); 33120Sstevel@tonic-gate msg->msg_name = abuf; 33130Sstevel@tonic-gate msg->msg_namelen = addrlen; 33140Sstevel@tonic-gate } 33150Sstevel@tonic-gate 33160Sstevel@tonic-gate if (controllen != 0) { 33170Sstevel@tonic-gate /* 33180Sstevel@tonic-gate * Return control msg to caller. 33190Sstevel@tonic-gate * Caller handles truncation if length 33200Sstevel@tonic-gate * exceeds msg_controllen. 33210Sstevel@tonic-gate */ 33224420Samehta control = kmem_zalloc(controllen, KM_SLEEP); 33230Sstevel@tonic-gate 33240Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen, 33255240Snordmark !(flags & MSG_XPG4_2), 33265240Snordmark control, controllen); 33270Sstevel@tonic-gate if (error) { 33280Sstevel@tonic-gate freemsg(mp); 33290Sstevel@tonic-gate if (msg->msg_namelen != 0) 33300Sstevel@tonic-gate kmem_free(msg->msg_name, 33315240Snordmark msg->msg_namelen); 33320Sstevel@tonic-gate kmem_free(control, controllen); 33330Sstevel@tonic-gate eprintsoline(so, error); 33346707Sbrutus goto out; 33350Sstevel@tonic-gate } 33360Sstevel@tonic-gate msg->msg_control = control; 33370Sstevel@tonic-gate msg->msg_controllen = controllen; 33380Sstevel@tonic-gate } 33390Sstevel@tonic-gate 33400Sstevel@tonic-gate freemsg(mp); 33416707Sbrutus goto out; 33420Sstevel@tonic-gate } 33430Sstevel@tonic-gate case T_OPTDATA_IND: { 33440Sstevel@tonic-gate struct T_optdata_req *tdr; 33450Sstevel@tonic-gate void *opt; 33460Sstevel@tonic-gate t_uscalar_t optlen; 33470Sstevel@tonic-gate 33480Sstevel@tonic-gate if ((so->so_state & 33490Sstevel@tonic-gate (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 33500Sstevel@tonic-gate (uiop->uio_resid != saved_resid) && 33510Sstevel@tonic-gate !(flags & MSG_PEEK)) { 33520Sstevel@tonic-gate sorecv_update_oobstate(so); 33530Sstevel@tonic-gate } 33540Sstevel@tonic-gate 33550Sstevel@tonic-gate tdr = (struct T_optdata_req *)mp->b_rptr; 33560Sstevel@tonic-gate optlen = tdr->OPT_length; 33570Sstevel@tonic-gate if (optlen != 0) { 33580Sstevel@tonic-gate t_uscalar_t ncontrollen; 33590Sstevel@tonic-gate /* 33600Sstevel@tonic-gate * Determine how large cmsg buffer is needed. 33610Sstevel@tonic-gate */ 33620Sstevel@tonic-gate opt = sogetoff(mp, 33635240Snordmark tpr->optdata_ind.OPT_offset, 33645240Snordmark optlen, __TPI_ALIGN_SIZE); 33650Sstevel@tonic-gate 33660Sstevel@tonic-gate if (opt == NULL) { 33670Sstevel@tonic-gate freemsg(mp); 33680Sstevel@tonic-gate error = EPROTO; 33690Sstevel@tonic-gate eprintsoline(so, error); 33706707Sbrutus goto out; 33710Sstevel@tonic-gate } 33720Sstevel@tonic-gate 33730Sstevel@tonic-gate ncontrollen = so_cmsglen(mp, opt, optlen, 33745240Snordmark !(flags & MSG_XPG4_2)); 33750Sstevel@tonic-gate if (controllen != 0) 33760Sstevel@tonic-gate controllen = ncontrollen; 33770Sstevel@tonic-gate else if (ncontrollen != 0) 33780Sstevel@tonic-gate msg->msg_flags |= MSG_CTRUNC; 33790Sstevel@tonic-gate } else { 33800Sstevel@tonic-gate controllen = 0; 33810Sstevel@tonic-gate } 33820Sstevel@tonic-gate 33830Sstevel@tonic-gate if (controllen != 0) { 33840Sstevel@tonic-gate /* 33850Sstevel@tonic-gate * Return control msg to caller. 33860Sstevel@tonic-gate * Caller handles truncation if length 33870Sstevel@tonic-gate * exceeds msg_controllen. 33880Sstevel@tonic-gate */ 33894420Samehta control = kmem_zalloc(controllen, KM_SLEEP); 33900Sstevel@tonic-gate 33910Sstevel@tonic-gate error = so_opt2cmsg(mp, opt, optlen, 33925240Snordmark !(flags & MSG_XPG4_2), 33935240Snordmark control, controllen); 33940Sstevel@tonic-gate if (error) { 33950Sstevel@tonic-gate freemsg(mp); 33960Sstevel@tonic-gate kmem_free(control, controllen); 33970Sstevel@tonic-gate eprintsoline(so, error); 33986707Sbrutus goto out; 33990Sstevel@tonic-gate } 34000Sstevel@tonic-gate msg->msg_control = control; 34010Sstevel@tonic-gate msg->msg_controllen = controllen; 34020Sstevel@tonic-gate } 34030Sstevel@tonic-gate 34040Sstevel@tonic-gate /* 34050Sstevel@tonic-gate * Set msg_flags to MSG_EOR based on 34060Sstevel@tonic-gate * DATA_flag and MOREDATA. 34070Sstevel@tonic-gate */ 34080Sstevel@tonic-gate mutex_enter(&so->so_lock); 34090Sstevel@tonic-gate so->so_state &= ~SS_SAVEDEOR; 34100Sstevel@tonic-gate if (!(tpr->data_ind.MORE_flag & 1)) { 34110Sstevel@tonic-gate if (!(rval.r_val1 & MOREDATA)) 34120Sstevel@tonic-gate msg->msg_flags |= MSG_EOR; 34130Sstevel@tonic-gate else 34140Sstevel@tonic-gate so->so_state |= SS_SAVEDEOR; 34150Sstevel@tonic-gate } 34160Sstevel@tonic-gate freemsg(mp); 34170Sstevel@tonic-gate /* 34180Sstevel@tonic-gate * If some data was received (i.e. not EOF) and the 34190Sstevel@tonic-gate * read/recv* has not been satisfied wait for some more. 34200Sstevel@tonic-gate * Not possible to wait if control info was received. 34210Sstevel@tonic-gate */ 34220Sstevel@tonic-gate if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 34230Sstevel@tonic-gate controllen == 0 && 34240Sstevel@tonic-gate uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 34250Sstevel@tonic-gate mutex_exit(&so->so_lock); 34260Sstevel@tonic-gate first = 0; 34270Sstevel@tonic-gate pflag = opflag | MSG_NOMARK; 34280Sstevel@tonic-gate goto retry; 34290Sstevel@tonic-gate } 34306707Sbrutus goto out_locked; 34310Sstevel@tonic-gate } 34320Sstevel@tonic-gate case T_EXDATA_IND: { 34330Sstevel@tonic-gate dprintso(so, 1, 34345240Snordmark ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld " 34355240Snordmark "state %s\n", 34365240Snordmark so->so_oobsigcnt, so->so_oobcnt, 34375240Snordmark saved_resid - uiop->uio_resid, 34385240Snordmark pr_state(so->so_state, so->so_mode))); 34390Sstevel@tonic-gate /* 34400Sstevel@tonic-gate * kstrgetmsg handles MSGMARK so there is nothing to 34410Sstevel@tonic-gate * inspect in the T_EXDATA_IND. 34420Sstevel@tonic-gate * strsock_proto makes the stream head queue the T_EXDATA_IND 34430Sstevel@tonic-gate * as a separate message with no M_DATA component. Furthermore, 34440Sstevel@tonic-gate * the stream head does not consolidate M_DATA messages onto 34450Sstevel@tonic-gate * an MSGMARK'ed message ensuring that the T_EXDATA_IND 34460Sstevel@tonic-gate * remains a message by itself. This is needed since MSGMARK 34470Sstevel@tonic-gate * marks both the whole message as well as the last byte 34480Sstevel@tonic-gate * of the message. 34490Sstevel@tonic-gate */ 34500Sstevel@tonic-gate freemsg(mp); 34510Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); /* No data */ 34520Sstevel@tonic-gate if (flags & MSG_PEEK) { 34530Sstevel@tonic-gate /* 34540Sstevel@tonic-gate * Even though we are peeking we consume the 34550Sstevel@tonic-gate * T_EXDATA_IND thereby moving the mark information 34560Sstevel@tonic-gate * to SS_RCVATMARK. Then the oob code below will 34570Sstevel@tonic-gate * retry the peeking kstrgetmsg. 34580Sstevel@tonic-gate * Note that the stream head read queue is 34590Sstevel@tonic-gate * never flushed without holding SOREADLOCKED 34600Sstevel@tonic-gate * thus the T_EXDATA_IND can not disappear 34610Sstevel@tonic-gate * underneath us. 34620Sstevel@tonic-gate */ 34630Sstevel@tonic-gate dprintso(so, 1, 34645240Snordmark ("sotpi_recvmsg: consume EXDATA_IND " 34655240Snordmark "counts %d/%d state %s\n", 34665240Snordmark so->so_oobsigcnt, 34675240Snordmark so->so_oobcnt, 34685240Snordmark pr_state(so->so_state, so->so_mode))); 34690Sstevel@tonic-gate 34700Sstevel@tonic-gate pflag = MSG_ANY | MSG_DELAYERROR; 34710Sstevel@tonic-gate if (so->so_mode & SM_ATOMIC) 34720Sstevel@tonic-gate pflag |= MSG_DISCARDTAIL; 34730Sstevel@tonic-gate 34740Sstevel@tonic-gate pri = 0; 34750Sstevel@tonic-gate mp = NULL; 34760Sstevel@tonic-gate 34770Sstevel@tonic-gate error = kstrgetmsg(SOTOV(so), &mp, uiop, 34785240Snordmark &pri, &pflag, (clock_t)-1, &rval); 34790Sstevel@tonic-gate ASSERT(uiop->uio_resid == saved_resid); 34800Sstevel@tonic-gate 34810Sstevel@tonic-gate if (error) { 34820Sstevel@tonic-gate #ifdef SOCK_DEBUG 34830Sstevel@tonic-gate if (error != EWOULDBLOCK && error != EINTR) { 34840Sstevel@tonic-gate eprintsoline(so, error); 34850Sstevel@tonic-gate } 34860Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 34876707Sbrutus goto out; 34880Sstevel@tonic-gate } 34890Sstevel@tonic-gate ASSERT(mp); 34900Sstevel@tonic-gate tpr = (union T_primitives *)mp->b_rptr; 34910Sstevel@tonic-gate ASSERT(tpr->type == T_EXDATA_IND); 34920Sstevel@tonic-gate freemsg(mp); 34930Sstevel@tonic-gate } /* end "if (flags & MSG_PEEK)" */ 34940Sstevel@tonic-gate 34950Sstevel@tonic-gate /* 34960Sstevel@tonic-gate * Decrement the number of queued and pending oob. 34970Sstevel@tonic-gate * 34980Sstevel@tonic-gate * SS_RCVATMARK is cleared when we read past a mark. 34990Sstevel@tonic-gate * SS_HAVEOOBDATA is cleared when we've read past the 35000Sstevel@tonic-gate * last mark. 35010Sstevel@tonic-gate * SS_OOBPEND is cleared if we've read past the last 35020Sstevel@tonic-gate * mark and no (new) SIGURG has been posted. 35030Sstevel@tonic-gate */ 35040Sstevel@tonic-gate mutex_enter(&so->so_lock); 35050Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 35060Sstevel@tonic-gate ASSERT(so->so_oobsigcnt >= so->so_oobcnt); 35070Sstevel@tonic-gate ASSERT(so->so_oobsigcnt > 0); 35080Sstevel@tonic-gate so->so_oobsigcnt--; 35090Sstevel@tonic-gate ASSERT(so->so_oobcnt > 0); 35100Sstevel@tonic-gate so->so_oobcnt--; 35110Sstevel@tonic-gate /* 35120Sstevel@tonic-gate * Since the T_EXDATA_IND has been removed from the stream 35130Sstevel@tonic-gate * head, but we have not read data past the mark, 35140Sstevel@tonic-gate * sockfs needs to track that the socket is still at the mark. 35150Sstevel@tonic-gate * 35160Sstevel@tonic-gate * Since no data was received call kstrgetmsg again to wait 35170Sstevel@tonic-gate * for data. 35180Sstevel@tonic-gate */ 35190Sstevel@tonic-gate so->so_state |= SS_RCVATMARK; 35200Sstevel@tonic-gate mutex_exit(&so->so_lock); 35210Sstevel@tonic-gate dprintso(so, 1, 35220Sstevel@tonic-gate ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n", 35230Sstevel@tonic-gate so->so_oobsigcnt, so->so_oobcnt, 35240Sstevel@tonic-gate pr_state(so->so_state, so->so_mode))); 35250Sstevel@tonic-gate pflag = opflag; 35260Sstevel@tonic-gate goto retry; 35270Sstevel@tonic-gate } 35280Sstevel@tonic-gate default: 35290Sstevel@tonic-gate ASSERT(0); 35300Sstevel@tonic-gate freemsg(mp); 35310Sstevel@tonic-gate error = EPROTO; 35320Sstevel@tonic-gate eprintsoline(so, error); 35336707Sbrutus goto out; 35340Sstevel@tonic-gate } 35350Sstevel@tonic-gate /* NOTREACHED */ 35366707Sbrutus out: 35370Sstevel@tonic-gate mutex_enter(&so->so_lock); 35386707Sbrutus out_locked: 35396707Sbrutus if (sodp != NULL) { 35406707Sbrutus /* Finish any sodirect and uioa processing */ 35416707Sbrutus mutex_enter(sodp->sod_lock); 35426707Sbrutus if (suiop != NULL) { 35436707Sbrutus /* Finish any uioa_t processing */ 35446707Sbrutus int ret; 35456707Sbrutus 35466707Sbrutus ASSERT(uiop == (uio_t *)&sodp->sod_uioa); 35476707Sbrutus ret = uioafini(suiop, (uioa_t *)uiop); 35486707Sbrutus if (error == 0 && ret != 0) { 35496707Sbrutus /* If no error yet, set it */ 35506707Sbrutus error = ret; 35516707Sbrutus } 35526707Sbrutus if ((mp = sodp->sod_uioafh) != NULL) { 35536707Sbrutus sodp->sod_uioafh = NULL; 35546707Sbrutus sodp->sod_uioaft = NULL; 35556707Sbrutus freemsg(mp); 35566707Sbrutus } 35576707Sbrutus } 35586707Sbrutus if (!(sodp->sod_state & SOD_WAKE_NOT)) { 35596707Sbrutus /* Awoke */ 35606707Sbrutus sodp->sod_state &= SOD_WAKE_CLR; 35616707Sbrutus sodp->sod_state |= SOD_WAKE_NOT; 35626707Sbrutus } 35636707Sbrutus /* Last, clear sod_want value */ 35646707Sbrutus sodp->sod_want = 0; 35656707Sbrutus mutex_exit(sodp->sod_lock); 35666707Sbrutus } 35670Sstevel@tonic-gate so_unlock_read(so); /* Clear SOREADLOCKED */ 35680Sstevel@tonic-gate mutex_exit(&so->so_lock); 35690Sstevel@tonic-gate return (error); 35700Sstevel@tonic-gate } 35710Sstevel@tonic-gate 35720Sstevel@tonic-gate /* 35730Sstevel@tonic-gate * Sending data with options on a datagram socket. 35740Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set. 35750Sstevel@tonic-gate */ 35760Sstevel@tonic-gate static int 3577741Smasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3578741Smasputra struct uio *uiop, void *control, t_uscalar_t controllen, int flags) 35790Sstevel@tonic-gate { 35800Sstevel@tonic-gate struct T_unitdata_req tudr; 35810Sstevel@tonic-gate mblk_t *mp; 35820Sstevel@tonic-gate int error; 35830Sstevel@tonic-gate void *addr; 35840Sstevel@tonic-gate socklen_t addrlen; 35850Sstevel@tonic-gate void *src; 35860Sstevel@tonic-gate socklen_t srclen; 35870Sstevel@tonic-gate ssize_t len; 35880Sstevel@tonic-gate int size; 35890Sstevel@tonic-gate struct T_opthdr toh; 35900Sstevel@tonic-gate struct fdbuf *fdbuf; 35910Sstevel@tonic-gate t_uscalar_t optlen; 35920Sstevel@tonic-gate void *fds; 35930Sstevel@tonic-gate int fdlen; 35940Sstevel@tonic-gate 35950Sstevel@tonic-gate ASSERT(name && namelen); 35960Sstevel@tonic-gate ASSERT(control && controllen); 35970Sstevel@tonic-gate 35980Sstevel@tonic-gate len = uiop->uio_resid; 35990Sstevel@tonic-gate if (len > (ssize_t)so->so_tidu_size) { 36000Sstevel@tonic-gate return (EMSGSIZE); 36010Sstevel@tonic-gate } 36020Sstevel@tonic-gate 36030Sstevel@tonic-gate /* 36040Sstevel@tonic-gate * For AF_UNIX the destination address is translated to an internal 36050Sstevel@tonic-gate * name and the source address is passed as an option. 36060Sstevel@tonic-gate * Also, file descriptors are passed as file pointers in an 36070Sstevel@tonic-gate * option. 36080Sstevel@tonic-gate */ 36090Sstevel@tonic-gate 36100Sstevel@tonic-gate /* 36110Sstevel@tonic-gate * Length and family checks. 36120Sstevel@tonic-gate */ 36130Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 36140Sstevel@tonic-gate if (error) { 36150Sstevel@tonic-gate eprintsoline(so, error); 36160Sstevel@tonic-gate return (error); 36170Sstevel@tonic-gate } 36180Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 36190Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 36200Sstevel@tonic-gate /* 36210Sstevel@tonic-gate * Already have a transport internal address. Do not 36220Sstevel@tonic-gate * pass any (transport internal) source address. 36230Sstevel@tonic-gate */ 36240Sstevel@tonic-gate addr = name; 36250Sstevel@tonic-gate addrlen = namelen; 36260Sstevel@tonic-gate src = NULL; 36270Sstevel@tonic-gate srclen = 0; 36280Sstevel@tonic-gate } else { 36290Sstevel@tonic-gate /* 36300Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 36310Sstevel@tonic-gate * and translate the remote address. 36320Sstevel@tonic-gate * 36330Sstevel@tonic-gate * Note that this code does not prevent so_laddr_sa 36340Sstevel@tonic-gate * from changing while it is being used. Thus 36350Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this 36360Sstevel@tonic-gate * send the peer might see a partially new and a 36370Sstevel@tonic-gate * partially old "from" address. 36380Sstevel@tonic-gate */ 36390Sstevel@tonic-gate src = so->so_laddr_sa; 36400Sstevel@tonic-gate srclen = (t_uscalar_t)so->so_laddr_len; 36410Sstevel@tonic-gate dprintso(so, 1, 36420Sstevel@tonic-gate ("sosend_dgramcmsg UNIX: srclen %d, src %p\n", 36430Sstevel@tonic-gate srclen, src)); 36440Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen, 36455240Snordmark (flags & MSG_XPG4_2), 36465240Snordmark &addr, &addrlen); 36470Sstevel@tonic-gate if (error) { 36480Sstevel@tonic-gate eprintsoline(so, error); 36490Sstevel@tonic-gate return (error); 36500Sstevel@tonic-gate } 36510Sstevel@tonic-gate } 36520Sstevel@tonic-gate } else { 36530Sstevel@tonic-gate addr = name; 36540Sstevel@tonic-gate addrlen = namelen; 36550Sstevel@tonic-gate src = NULL; 36560Sstevel@tonic-gate srclen = 0; 36570Sstevel@tonic-gate } 36580Sstevel@tonic-gate optlen = so_optlen(control, controllen, 36595240Snordmark !(flags & MSG_XPG4_2)); 36600Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 36610Sstevel@tonic-gate tudr.DEST_length = addrlen; 36620Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 36630Sstevel@tonic-gate if (srclen != 0) 36640Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) + 36650Sstevel@tonic-gate _TPI_ALIGN_TOPT(srclen)); 36660Sstevel@tonic-gate else 36670Sstevel@tonic-gate tudr.OPT_length = optlen; 36680Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 36695240Snordmark _TPI_ALIGN_TOPT(addrlen)); 36700Sstevel@tonic-gate 36710Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 36720Sstevel@tonic-gate 36730Sstevel@tonic-gate /* 36740Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set. 36750Sstevel@tonic-gate */ 36760Sstevel@tonic-gate error = so_getfdopt(control, controllen, 36775240Snordmark !(flags & MSG_XPG4_2), &fds, &fdlen); 36780Sstevel@tonic-gate if (error) 36790Sstevel@tonic-gate return (error); 36800Sstevel@tonic-gate if (fdlen != -1) { 36810Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING)) 36820Sstevel@tonic-gate return (EOPNOTSUPP); 36830Sstevel@tonic-gate 36840Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf); 36850Sstevel@tonic-gate if (error) 36860Sstevel@tonic-gate return (error); 36870Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf); 36880Sstevel@tonic-gate } else { 36890Sstevel@tonic-gate mp = soallocproto(size, _ALLOC_INTR); 3690455Smeem if (mp == NULL) { 3691455Smeem /* 3692455Smeem * Caught a signal waiting for memory. 3693455Smeem * Let send* return EINTR. 3694455Smeem */ 3695455Smeem return (EINTR); 3696455Smeem } 36970Sstevel@tonic-gate } 36980Sstevel@tonic-gate soappendmsg(mp, &tudr, sizeof (tudr)); 36990Sstevel@tonic-gate soappendmsg(mp, addr, addrlen); 37000Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 37010Sstevel@tonic-gate 37020Sstevel@tonic-gate if (fdlen != -1) { 37030Sstevel@tonic-gate ASSERT(fdbuf != NULL); 37040Sstevel@tonic-gate toh.level = SOL_SOCKET; 37050Sstevel@tonic-gate toh.name = SO_FILEP; 37060Sstevel@tonic-gate toh.len = fdbuf->fd_size + 37075240Snordmark (t_uscalar_t)sizeof (struct T_opthdr); 37080Sstevel@tonic-gate toh.status = 0; 37090Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 37100Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size); 37110Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 37120Sstevel@tonic-gate } 37130Sstevel@tonic-gate if (srclen != 0) { 37140Sstevel@tonic-gate /* 37150Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 37160Sstevel@tonic-gate * address option. 37170Sstevel@tonic-gate */ 37180Sstevel@tonic-gate toh.level = SOL_SOCKET; 37190Sstevel@tonic-gate toh.name = SO_SRCADDR; 37200Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 37210Sstevel@tonic-gate toh.status = 0; 37220Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 37230Sstevel@tonic-gate soappendmsg(mp, src, srclen); 37240Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 37250Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 37260Sstevel@tonic-gate } 37270Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 37280Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 37290Sstevel@tonic-gate /* At most 3 bytes left in the message */ 37300Sstevel@tonic-gate ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE)); 37310Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size); 37320Sstevel@tonic-gate 37330Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 37340Sstevel@tonic-gate if (audit_active) 37350Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 37360Sstevel@tonic-gate 37370Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 37380Sstevel@tonic-gate #ifdef SOCK_DEBUG 37390Sstevel@tonic-gate if (error) { 37400Sstevel@tonic-gate eprintsoline(so, error); 37410Sstevel@tonic-gate } 37420Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 37430Sstevel@tonic-gate return (error); 37440Sstevel@tonic-gate } 37450Sstevel@tonic-gate 37460Sstevel@tonic-gate /* 37470Sstevel@tonic-gate * Sending data with options on a connected stream socket. 37480Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set. 37490Sstevel@tonic-gate */ 37500Sstevel@tonic-gate static int 37510Sstevel@tonic-gate sosend_svccmsg(struct sonode *so, 37520Sstevel@tonic-gate struct uio *uiop, 37530Sstevel@tonic-gate int more, 37540Sstevel@tonic-gate void *control, 37550Sstevel@tonic-gate t_uscalar_t controllen, 37560Sstevel@tonic-gate int flags) 37570Sstevel@tonic-gate { 37580Sstevel@tonic-gate struct T_optdata_req tdr; 37590Sstevel@tonic-gate mblk_t *mp; 37600Sstevel@tonic-gate int error; 37610Sstevel@tonic-gate ssize_t iosize; 37620Sstevel@tonic-gate int first = 1; 37630Sstevel@tonic-gate int size; 37640Sstevel@tonic-gate struct fdbuf *fdbuf; 37650Sstevel@tonic-gate t_uscalar_t optlen; 37660Sstevel@tonic-gate void *fds; 37670Sstevel@tonic-gate int fdlen; 37680Sstevel@tonic-gate struct T_opthdr toh; 37690Sstevel@tonic-gate 37700Sstevel@tonic-gate dprintso(so, 1, 37715240Snordmark ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid)); 37720Sstevel@tonic-gate 37730Sstevel@tonic-gate /* 37740Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are 37750Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it 37760Sstevel@tonic-gate * thus it is not possible to ASSERT on the state. 37770Sstevel@tonic-gate */ 37780Sstevel@tonic-gate 37790Sstevel@tonic-gate /* Options on connection-oriented only when SM_OPTDATA set. */ 37800Sstevel@tonic-gate if (!(so->so_mode & SM_OPTDATA)) 37810Sstevel@tonic-gate return (EOPNOTSUPP); 37820Sstevel@tonic-gate 37830Sstevel@tonic-gate do { 37840Sstevel@tonic-gate /* 37850Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this 37860Sstevel@tonic-gate * message or if the caller passed in "more". 37870Sstevel@tonic-gate * Error for transports with zero tidu_size. 37880Sstevel@tonic-gate */ 37890Sstevel@tonic-gate tdr.PRIM_type = T_OPTDATA_REQ; 37900Sstevel@tonic-gate iosize = so->so_tidu_size; 37910Sstevel@tonic-gate if (iosize <= 0) 37920Sstevel@tonic-gate return (EMSGSIZE); 37930Sstevel@tonic-gate if (uiop->uio_resid > iosize) { 37940Sstevel@tonic-gate tdr.DATA_flag = 1; 37950Sstevel@tonic-gate } else { 37960Sstevel@tonic-gate if (more) 37970Sstevel@tonic-gate tdr.DATA_flag = 1; 37980Sstevel@tonic-gate else 37990Sstevel@tonic-gate tdr.DATA_flag = 0; 38000Sstevel@tonic-gate iosize = uiop->uio_resid; 38010Sstevel@tonic-gate } 38020Sstevel@tonic-gate dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n", 38035240Snordmark tdr.DATA_flag, iosize)); 38040Sstevel@tonic-gate 38050Sstevel@tonic-gate optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2)); 38060Sstevel@tonic-gate tdr.OPT_length = optlen; 38070Sstevel@tonic-gate tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 38080Sstevel@tonic-gate 38090Sstevel@tonic-gate size = (int)sizeof (tdr) + optlen; 38100Sstevel@tonic-gate /* 38110Sstevel@tonic-gate * File descriptors only when SM_FDPASSING set. 38120Sstevel@tonic-gate */ 38130Sstevel@tonic-gate error = so_getfdopt(control, controllen, 38145240Snordmark !(flags & MSG_XPG4_2), &fds, &fdlen); 38150Sstevel@tonic-gate if (error) 38160Sstevel@tonic-gate return (error); 38170Sstevel@tonic-gate if (fdlen != -1) { 38180Sstevel@tonic-gate if (!(so->so_mode & SM_FDPASSING)) 38190Sstevel@tonic-gate return (EOPNOTSUPP); 38200Sstevel@tonic-gate 38210Sstevel@tonic-gate error = fdbuf_create(fds, fdlen, &fdbuf); 38220Sstevel@tonic-gate if (error) 38230Sstevel@tonic-gate return (error); 38240Sstevel@tonic-gate mp = fdbuf_allocmsg(size, fdbuf); 38250Sstevel@tonic-gate } else { 38260Sstevel@tonic-gate mp = soallocproto(size, _ALLOC_INTR); 3827455Smeem if (mp == NULL) { 3828455Smeem /* 3829455Smeem * Caught a signal waiting for memory. 3830455Smeem * Let send* return EINTR. 3831455Smeem */ 3832455Smeem return (first ? EINTR : 0); 3833455Smeem } 38340Sstevel@tonic-gate } 38350Sstevel@tonic-gate soappendmsg(mp, &tdr, sizeof (tdr)); 38360Sstevel@tonic-gate 38370Sstevel@tonic-gate if (fdlen != -1) { 38380Sstevel@tonic-gate ASSERT(fdbuf != NULL); 38390Sstevel@tonic-gate toh.level = SOL_SOCKET; 38400Sstevel@tonic-gate toh.name = SO_FILEP; 38410Sstevel@tonic-gate toh.len = fdbuf->fd_size + 38425240Snordmark (t_uscalar_t)sizeof (struct T_opthdr); 38430Sstevel@tonic-gate toh.status = 0; 38440Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 38450Sstevel@tonic-gate soappendmsg(mp, fdbuf, fdbuf->fd_size); 38460Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 38470Sstevel@tonic-gate } 38480Sstevel@tonic-gate so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 38490Sstevel@tonic-gate /* At most 3 bytes left in the message */ 38500Sstevel@tonic-gate ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE)); 38510Sstevel@tonic-gate ASSERT(MBLKL(mp) <= (ssize_t)size); 38520Sstevel@tonic-gate 38530Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 38540Sstevel@tonic-gate 38550Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 38565240Snordmark 0, MSG_BAND, 0); 38570Sstevel@tonic-gate if (error) { 38580Sstevel@tonic-gate if (!first && error == EWOULDBLOCK) 38590Sstevel@tonic-gate return (0); 38600Sstevel@tonic-gate eprintsoline(so, error); 38610Sstevel@tonic-gate return (error); 38620Sstevel@tonic-gate } 38630Sstevel@tonic-gate control = NULL; 38640Sstevel@tonic-gate first = 0; 38650Sstevel@tonic-gate if (uiop->uio_resid > 0) { 38660Sstevel@tonic-gate /* 38670Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though 38680Sstevel@tonic-gate * some data have been written. This is consistent 38690Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics. 38700Sstevel@tonic-gate */ 38710Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) { 38720Sstevel@tonic-gate tsignal(curthread, SIGPIPE); 38730Sstevel@tonic-gate eprintsoline(so, error); 38740Sstevel@tonic-gate return (EPIPE); 38750Sstevel@tonic-gate } 38760Sstevel@tonic-gate if (so->so_error != 0) { 38770Sstevel@tonic-gate mutex_enter(&so->so_lock); 38780Sstevel@tonic-gate error = sogeterr(so); 38790Sstevel@tonic-gate mutex_exit(&so->so_lock); 38800Sstevel@tonic-gate if (error != 0) { 38810Sstevel@tonic-gate eprintsoline(so, error); 38820Sstevel@tonic-gate return (error); 38830Sstevel@tonic-gate } 38840Sstevel@tonic-gate } 38850Sstevel@tonic-gate } 38860Sstevel@tonic-gate } while (uiop->uio_resid > 0); 38870Sstevel@tonic-gate return (0); 38880Sstevel@tonic-gate } 38890Sstevel@tonic-gate 38900Sstevel@tonic-gate /* 38910Sstevel@tonic-gate * Sending data on a datagram socket. 38920Sstevel@tonic-gate * Assumes caller has verified that SS_ISBOUND etc. are set. 38930Sstevel@tonic-gate * 38940Sstevel@tonic-gate * For AF_UNIX the destination address is translated to an internal 38950Sstevel@tonic-gate * name and the source address is passed as an option. 38960Sstevel@tonic-gate */ 38970Sstevel@tonic-gate int 3898741Smasputra sosend_dgram(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3899741Smasputra struct uio *uiop, int flags) 39000Sstevel@tonic-gate { 39010Sstevel@tonic-gate struct T_unitdata_req tudr; 39020Sstevel@tonic-gate mblk_t *mp; 39030Sstevel@tonic-gate int error; 39040Sstevel@tonic-gate void *addr; 39050Sstevel@tonic-gate socklen_t addrlen; 39060Sstevel@tonic-gate void *src; 39070Sstevel@tonic-gate socklen_t srclen; 39080Sstevel@tonic-gate ssize_t len; 39090Sstevel@tonic-gate 3910741Smasputra ASSERT(name != NULL && namelen != 0); 39110Sstevel@tonic-gate 39120Sstevel@tonic-gate len = uiop->uio_resid; 39130Sstevel@tonic-gate if (len > so->so_tidu_size) { 39140Sstevel@tonic-gate error = EMSGSIZE; 39150Sstevel@tonic-gate goto done; 39160Sstevel@tonic-gate } 39170Sstevel@tonic-gate 3918741Smasputra /* Length and family checks */ 39190Sstevel@tonic-gate error = so_addr_verify(so, name, namelen); 3920741Smasputra if (error != 0) 39210Sstevel@tonic-gate goto done; 3922741Smasputra 3923741Smasputra if (so->so_state & SS_DIRECT) 3924741Smasputra return (sodgram_direct(so, name, namelen, uiop, flags)); 3925741Smasputra 39260Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 39270Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 39280Sstevel@tonic-gate /* 39290Sstevel@tonic-gate * Already have a transport internal address. Do not 39300Sstevel@tonic-gate * pass any (transport internal) source address. 39310Sstevel@tonic-gate */ 39320Sstevel@tonic-gate addr = name; 39330Sstevel@tonic-gate addrlen = namelen; 39340Sstevel@tonic-gate src = NULL; 39350Sstevel@tonic-gate srclen = 0; 39360Sstevel@tonic-gate } else { 39370Sstevel@tonic-gate /* 39380Sstevel@tonic-gate * Pass the sockaddr_un source address as an option 39390Sstevel@tonic-gate * and translate the remote address. 39400Sstevel@tonic-gate * 39410Sstevel@tonic-gate * Note that this code does not prevent so_laddr_sa 39420Sstevel@tonic-gate * from changing while it is being used. Thus 39430Sstevel@tonic-gate * if an unbind+bind occurs concurrently with this 39440Sstevel@tonic-gate * send the peer might see a partially new and a 39450Sstevel@tonic-gate * partially old "from" address. 39460Sstevel@tonic-gate */ 39470Sstevel@tonic-gate src = so->so_laddr_sa; 39480Sstevel@tonic-gate srclen = (socklen_t)so->so_laddr_len; 39490Sstevel@tonic-gate dprintso(so, 1, 39505240Snordmark ("sosend_dgram UNIX: srclen %d, src %p\n", 39515240Snordmark srclen, src)); 39520Sstevel@tonic-gate error = so_ux_addr_xlate(so, name, namelen, 39535240Snordmark (flags & MSG_XPG4_2), 39545240Snordmark &addr, &addrlen); 39550Sstevel@tonic-gate if (error) { 39560Sstevel@tonic-gate eprintsoline(so, error); 39570Sstevel@tonic-gate goto done; 39580Sstevel@tonic-gate } 39590Sstevel@tonic-gate } 39600Sstevel@tonic-gate } else { 39610Sstevel@tonic-gate addr = name; 39620Sstevel@tonic-gate addrlen = namelen; 39630Sstevel@tonic-gate src = NULL; 39640Sstevel@tonic-gate srclen = 0; 39650Sstevel@tonic-gate } 39660Sstevel@tonic-gate tudr.PRIM_type = T_UNITDATA_REQ; 39670Sstevel@tonic-gate tudr.DEST_length = addrlen; 39680Sstevel@tonic-gate tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 39690Sstevel@tonic-gate if (srclen == 0) { 39700Sstevel@tonic-gate tudr.OPT_length = 0; 39710Sstevel@tonic-gate tudr.OPT_offset = 0; 39720Sstevel@tonic-gate 39730Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 39740Sstevel@tonic-gate addr, addrlen, 0, _ALLOC_INTR); 39750Sstevel@tonic-gate if (mp == NULL) { 39760Sstevel@tonic-gate /* 39770Sstevel@tonic-gate * Caught a signal waiting for memory. 39780Sstevel@tonic-gate * Let send* return EINTR. 39790Sstevel@tonic-gate */ 39800Sstevel@tonic-gate error = EINTR; 39810Sstevel@tonic-gate goto done; 39820Sstevel@tonic-gate } 39830Sstevel@tonic-gate } else { 39840Sstevel@tonic-gate /* 39850Sstevel@tonic-gate * There is a AF_UNIX sockaddr_un to include as a source 39860Sstevel@tonic-gate * address option. 39870Sstevel@tonic-gate */ 39880Sstevel@tonic-gate struct T_opthdr toh; 39890Sstevel@tonic-gate ssize_t size; 39900Sstevel@tonic-gate 39910Sstevel@tonic-gate tudr.OPT_length = (t_scalar_t)(sizeof (toh) + 39925240Snordmark _TPI_ALIGN_TOPT(srclen)); 39930Sstevel@tonic-gate tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 39945240Snordmark _TPI_ALIGN_TOPT(addrlen)); 39950Sstevel@tonic-gate 39960Sstevel@tonic-gate toh.level = SOL_SOCKET; 39970Sstevel@tonic-gate toh.name = SO_SRCADDR; 39980Sstevel@tonic-gate toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 39990Sstevel@tonic-gate toh.status = 0; 40000Sstevel@tonic-gate 40010Sstevel@tonic-gate size = tudr.OPT_offset + tudr.OPT_length; 40020Sstevel@tonic-gate mp = soallocproto2(&tudr, sizeof (tudr), 40030Sstevel@tonic-gate addr, addrlen, size, _ALLOC_INTR); 40040Sstevel@tonic-gate if (mp == NULL) { 40050Sstevel@tonic-gate /* 40060Sstevel@tonic-gate * Caught a signal waiting for memory. 40070Sstevel@tonic-gate * Let send* return EINTR. 40080Sstevel@tonic-gate */ 40090Sstevel@tonic-gate error = EINTR; 40100Sstevel@tonic-gate goto done; 40110Sstevel@tonic-gate } 40120Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 40130Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 40140Sstevel@tonic-gate soappendmsg(mp, src, srclen); 40150Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 40160Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 40170Sstevel@tonic-gate } 40180Sstevel@tonic-gate 40190Sstevel@tonic-gate if (audit_active) 40200Sstevel@tonic-gate audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 40210Sstevel@tonic-gate 40220Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 40230Sstevel@tonic-gate done: 40240Sstevel@tonic-gate #ifdef SOCK_DEBUG 40250Sstevel@tonic-gate if (error) { 40260Sstevel@tonic-gate eprintsoline(so, error); 40270Sstevel@tonic-gate } 40280Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 40290Sstevel@tonic-gate return (error); 40300Sstevel@tonic-gate } 40310Sstevel@tonic-gate 40320Sstevel@tonic-gate /* 40330Sstevel@tonic-gate * Sending data on a connected stream socket. 40340Sstevel@tonic-gate * Assumes caller has verified that SS_ISCONNECTED is set. 40350Sstevel@tonic-gate */ 40360Sstevel@tonic-gate int 40370Sstevel@tonic-gate sosend_svc(struct sonode *so, 40380Sstevel@tonic-gate struct uio *uiop, 40390Sstevel@tonic-gate t_scalar_t prim, 40400Sstevel@tonic-gate int more, 40410Sstevel@tonic-gate int sflag) 40420Sstevel@tonic-gate { 40430Sstevel@tonic-gate struct T_data_req tdr; 40440Sstevel@tonic-gate mblk_t *mp; 40450Sstevel@tonic-gate int error; 40460Sstevel@tonic-gate ssize_t iosize; 40470Sstevel@tonic-gate int first = 1; 40480Sstevel@tonic-gate 40490Sstevel@tonic-gate dprintso(so, 1, 40505240Snordmark ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n", 4051*7240Srh87107 (void *)so, uiop->uio_resid, prim, sflag)); 40520Sstevel@tonic-gate 40530Sstevel@tonic-gate /* 40540Sstevel@tonic-gate * Has to be bound and connected. However, since no locks are 40550Sstevel@tonic-gate * held the state could have changed after sotpi_sendmsg checked it 40560Sstevel@tonic-gate * thus it is not possible to ASSERT on the state. 40570Sstevel@tonic-gate */ 40580Sstevel@tonic-gate 40590Sstevel@tonic-gate do { 40600Sstevel@tonic-gate /* 40610Sstevel@tonic-gate * Set the MORE flag if uio_resid does not fit in this 40620Sstevel@tonic-gate * message or if the caller passed in "more". 40630Sstevel@tonic-gate * Error for transports with zero tidu_size. 40640Sstevel@tonic-gate */ 40650Sstevel@tonic-gate tdr.PRIM_type = prim; 40660Sstevel@tonic-gate iosize = so->so_tidu_size; 40670Sstevel@tonic-gate if (iosize <= 0) 40680Sstevel@tonic-gate return (EMSGSIZE); 40690Sstevel@tonic-gate if (uiop->uio_resid > iosize) { 40700Sstevel@tonic-gate tdr.MORE_flag = 1; 40710Sstevel@tonic-gate } else { 40720Sstevel@tonic-gate if (more) 40730Sstevel@tonic-gate tdr.MORE_flag = 1; 40740Sstevel@tonic-gate else 40750Sstevel@tonic-gate tdr.MORE_flag = 0; 40760Sstevel@tonic-gate iosize = uiop->uio_resid; 40770Sstevel@tonic-gate } 40780Sstevel@tonic-gate dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n", 40795240Snordmark prim, tdr.MORE_flag, iosize)); 40800Sstevel@tonic-gate mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR); 40810Sstevel@tonic-gate if (mp == NULL) { 40820Sstevel@tonic-gate /* 40830Sstevel@tonic-gate * Caught a signal waiting for memory. 40840Sstevel@tonic-gate * Let send* return EINTR. 40850Sstevel@tonic-gate */ 40860Sstevel@tonic-gate if (first) 40870Sstevel@tonic-gate return (EINTR); 40880Sstevel@tonic-gate else 40890Sstevel@tonic-gate return (0); 40900Sstevel@tonic-gate } 40910Sstevel@tonic-gate 40920Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 40935240Snordmark 0, sflag | MSG_BAND, 0); 40940Sstevel@tonic-gate if (error) { 40950Sstevel@tonic-gate if (!first && error == EWOULDBLOCK) 40960Sstevel@tonic-gate return (0); 40970Sstevel@tonic-gate eprintsoline(so, error); 40980Sstevel@tonic-gate return (error); 40990Sstevel@tonic-gate } 41000Sstevel@tonic-gate first = 0; 41010Sstevel@tonic-gate if (uiop->uio_resid > 0) { 41020Sstevel@tonic-gate /* 41030Sstevel@tonic-gate * Recheck for fatal errors. Fail write even though 41040Sstevel@tonic-gate * some data have been written. This is consistent 41050Sstevel@tonic-gate * with strwrite semantics and BSD sockets semantics. 41060Sstevel@tonic-gate */ 41070Sstevel@tonic-gate if (so->so_state & SS_CANTSENDMORE) { 41080Sstevel@tonic-gate tsignal(curthread, SIGPIPE); 41090Sstevel@tonic-gate eprintsoline(so, error); 41100Sstevel@tonic-gate return (EPIPE); 41110Sstevel@tonic-gate } 41120Sstevel@tonic-gate if (so->so_error != 0) { 41130Sstevel@tonic-gate mutex_enter(&so->so_lock); 41140Sstevel@tonic-gate error = sogeterr(so); 41150Sstevel@tonic-gate mutex_exit(&so->so_lock); 41160Sstevel@tonic-gate if (error != 0) { 41170Sstevel@tonic-gate eprintsoline(so, error); 41180Sstevel@tonic-gate return (error); 41190Sstevel@tonic-gate } 41200Sstevel@tonic-gate } 41210Sstevel@tonic-gate } 41220Sstevel@tonic-gate } while (uiop->uio_resid > 0); 41230Sstevel@tonic-gate return (0); 41240Sstevel@tonic-gate } 41250Sstevel@tonic-gate 41260Sstevel@tonic-gate /* 41270Sstevel@tonic-gate * Check the state for errors and call the appropriate send function. 41280Sstevel@tonic-gate * 41290Sstevel@tonic-gate * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set) 41300Sstevel@tonic-gate * this function issues a setsockopt to toggle SO_DONTROUTE before and 41310Sstevel@tonic-gate * after sending the message. 41320Sstevel@tonic-gate */ 41330Sstevel@tonic-gate static int 41340Sstevel@tonic-gate sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop) 41350Sstevel@tonic-gate { 41360Sstevel@tonic-gate int so_state; 41370Sstevel@tonic-gate int so_mode; 41380Sstevel@tonic-gate int error; 41390Sstevel@tonic-gate struct sockaddr *name; 41400Sstevel@tonic-gate t_uscalar_t namelen; 41410Sstevel@tonic-gate int dontroute; 41420Sstevel@tonic-gate int flags; 41430Sstevel@tonic-gate 41440Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n", 4145*7240Srh87107 (void *)so, (void *)msg, msg->msg_flags, 41465240Snordmark pr_state(so->so_state, so->so_mode), so->so_error)); 41470Sstevel@tonic-gate 41480Sstevel@tonic-gate mutex_enter(&so->so_lock); 41490Sstevel@tonic-gate so_state = so->so_state; 41500Sstevel@tonic-gate 41510Sstevel@tonic-gate if (so_state & SS_CANTSENDMORE) { 41520Sstevel@tonic-gate mutex_exit(&so->so_lock); 41530Sstevel@tonic-gate tsignal(curthread, SIGPIPE); 41540Sstevel@tonic-gate return (EPIPE); 41550Sstevel@tonic-gate } 41560Sstevel@tonic-gate 41570Sstevel@tonic-gate if (so->so_error != 0) { 41580Sstevel@tonic-gate error = sogeterr(so); 41590Sstevel@tonic-gate if (error != 0) { 41600Sstevel@tonic-gate mutex_exit(&so->so_lock); 41610Sstevel@tonic-gate return (error); 41620Sstevel@tonic-gate } 41630Sstevel@tonic-gate } 41640Sstevel@tonic-gate 41650Sstevel@tonic-gate name = (struct sockaddr *)msg->msg_name; 41660Sstevel@tonic-gate namelen = msg->msg_namelen; 41670Sstevel@tonic-gate 41680Sstevel@tonic-gate so_mode = so->so_mode; 41690Sstevel@tonic-gate 41700Sstevel@tonic-gate if (name == NULL) { 41710Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED)) { 41720Sstevel@tonic-gate mutex_exit(&so->so_lock); 41730Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) 41740Sstevel@tonic-gate return (ENOTCONN); 41750Sstevel@tonic-gate else 41760Sstevel@tonic-gate return (EDESTADDRREQ); 41770Sstevel@tonic-gate } 41780Sstevel@tonic-gate if (so_mode & SM_CONNREQUIRED) { 41790Sstevel@tonic-gate name = NULL; 41800Sstevel@tonic-gate namelen = 0; 41810Sstevel@tonic-gate } else { 41820Sstevel@tonic-gate /* 41830Sstevel@tonic-gate * Note that this code does not prevent so_faddr_sa 41840Sstevel@tonic-gate * from changing while it is being used. Thus 41850Sstevel@tonic-gate * if an "unconnect"+connect occurs concurrently with 41860Sstevel@tonic-gate * this send the datagram might be delivered to a 41870Sstevel@tonic-gate * garbaled address. 41880Sstevel@tonic-gate */ 41890Sstevel@tonic-gate ASSERT(so->so_faddr_sa); 41900Sstevel@tonic-gate name = so->so_faddr_sa; 41910Sstevel@tonic-gate namelen = (t_uscalar_t)so->so_faddr_len; 41920Sstevel@tonic-gate } 41930Sstevel@tonic-gate } else { 41940Sstevel@tonic-gate if (!(so_state & SS_ISCONNECTED) && 41950Sstevel@tonic-gate (so_mode & SM_CONNREQUIRED)) { 41960Sstevel@tonic-gate /* Required but not connected */ 41970Sstevel@tonic-gate mutex_exit(&so->so_lock); 41980Sstevel@tonic-gate return (ENOTCONN); 41990Sstevel@tonic-gate } 42000Sstevel@tonic-gate /* 42010Sstevel@tonic-gate * Ignore the address on connection-oriented sockets. 42020Sstevel@tonic-gate * Just like BSD this code does not generate an error for 42030Sstevel@tonic-gate * TCP (a CONNREQUIRED socket) when sending to an address 42040Sstevel@tonic-gate * passed in with sendto/sendmsg. Instead the data is 42050Sstevel@tonic-gate * delivered on the connection as if no address had been 42060Sstevel@tonic-gate * supplied. 42070Sstevel@tonic-gate */ 42080Sstevel@tonic-gate if ((so_state & SS_ISCONNECTED) && 42090Sstevel@tonic-gate !(so_mode & SM_CONNREQUIRED)) { 42100Sstevel@tonic-gate mutex_exit(&so->so_lock); 42110Sstevel@tonic-gate return (EISCONN); 42120Sstevel@tonic-gate } 42130Sstevel@tonic-gate if (!(so_state & SS_ISBOUND)) { 42140Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 42150Sstevel@tonic-gate error = sotpi_bind(so, NULL, 0, 42160Sstevel@tonic-gate _SOBIND_UNSPEC|_SOBIND_LOCK_HELD); 42170Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 42180Sstevel@tonic-gate if (error) { 42190Sstevel@tonic-gate mutex_exit(&so->so_lock); 42200Sstevel@tonic-gate eprintsoline(so, error); 42210Sstevel@tonic-gate return (error); 42220Sstevel@tonic-gate } 42230Sstevel@tonic-gate } 42240Sstevel@tonic-gate /* 42250Sstevel@tonic-gate * Handle delayed datagram errors. These are only queued 42260Sstevel@tonic-gate * when the application sets SO_DGRAM_ERRIND. 42270Sstevel@tonic-gate * Return the error if we are sending to the address 42280Sstevel@tonic-gate * that was returned in the last T_UDERROR_IND. 42290Sstevel@tonic-gate * If sending to some other address discard the delayed 42300Sstevel@tonic-gate * error indication. 42310Sstevel@tonic-gate */ 42320Sstevel@tonic-gate if (so->so_delayed_error) { 42330Sstevel@tonic-gate struct T_uderror_ind *tudi; 42340Sstevel@tonic-gate void *addr; 42350Sstevel@tonic-gate t_uscalar_t addrlen; 42360Sstevel@tonic-gate boolean_t match = B_FALSE; 42370Sstevel@tonic-gate 42380Sstevel@tonic-gate ASSERT(so->so_eaddr_mp); 42390Sstevel@tonic-gate error = so->so_delayed_error; 42400Sstevel@tonic-gate so->so_delayed_error = 0; 42410Sstevel@tonic-gate tudi = (struct T_uderror_ind *)so->so_eaddr_mp->b_rptr; 42420Sstevel@tonic-gate addrlen = tudi->DEST_length; 42430Sstevel@tonic-gate addr = sogetoff(so->so_eaddr_mp, 42445240Snordmark tudi->DEST_offset, 42455240Snordmark addrlen, 1); 42460Sstevel@tonic-gate ASSERT(addr); /* Checked by strsock_proto */ 42470Sstevel@tonic-gate switch (so->so_family) { 42480Sstevel@tonic-gate case AF_INET: { 42490Sstevel@tonic-gate /* Compare just IP address and port */ 42500Sstevel@tonic-gate sin_t *sin1 = (sin_t *)name; 42510Sstevel@tonic-gate sin_t *sin2 = (sin_t *)addr; 42520Sstevel@tonic-gate 42530Sstevel@tonic-gate if (addrlen == sizeof (sin_t) && 42540Sstevel@tonic-gate namelen == addrlen && 42550Sstevel@tonic-gate sin1->sin_port == sin2->sin_port && 42560Sstevel@tonic-gate sin1->sin_addr.s_addr == 42570Sstevel@tonic-gate sin2->sin_addr.s_addr) 42580Sstevel@tonic-gate match = B_TRUE; 42590Sstevel@tonic-gate break; 42600Sstevel@tonic-gate } 42610Sstevel@tonic-gate case AF_INET6: { 42620Sstevel@tonic-gate /* Compare just IP address and port. Not flow */ 42630Sstevel@tonic-gate sin6_t *sin1 = (sin6_t *)name; 42640Sstevel@tonic-gate sin6_t *sin2 = (sin6_t *)addr; 42650Sstevel@tonic-gate 42660Sstevel@tonic-gate if (addrlen == sizeof (sin6_t) && 42670Sstevel@tonic-gate namelen == addrlen && 42680Sstevel@tonic-gate sin1->sin6_port == sin2->sin6_port && 42690Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr, 42705240Snordmark &sin2->sin6_addr)) 42710Sstevel@tonic-gate match = B_TRUE; 42720Sstevel@tonic-gate break; 42730Sstevel@tonic-gate } 42740Sstevel@tonic-gate case AF_UNIX: 42750Sstevel@tonic-gate default: 42760Sstevel@tonic-gate if (namelen == addrlen && 42770Sstevel@tonic-gate bcmp(name, addr, namelen) == 0) 42780Sstevel@tonic-gate match = B_TRUE; 42790Sstevel@tonic-gate } 42800Sstevel@tonic-gate if (match) { 42810Sstevel@tonic-gate freemsg(so->so_eaddr_mp); 42820Sstevel@tonic-gate so->so_eaddr_mp = NULL; 42830Sstevel@tonic-gate mutex_exit(&so->so_lock); 42840Sstevel@tonic-gate #ifdef DEBUG 42850Sstevel@tonic-gate dprintso(so, 0, 42865240Snordmark ("sockfs delayed error %d for %s\n", 42875240Snordmark error, 42885240Snordmark pr_addr(so->so_family, name, namelen))); 42890Sstevel@tonic-gate #endif /* DEBUG */ 42900Sstevel@tonic-gate return (error); 42910Sstevel@tonic-gate } 42920Sstevel@tonic-gate freemsg(so->so_eaddr_mp); 42930Sstevel@tonic-gate so->so_eaddr_mp = NULL; 42940Sstevel@tonic-gate } 42950Sstevel@tonic-gate } 42960Sstevel@tonic-gate mutex_exit(&so->so_lock); 42970Sstevel@tonic-gate 42980Sstevel@tonic-gate flags = msg->msg_flags; 42990Sstevel@tonic-gate dontroute = 0; 43000Sstevel@tonic-gate if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) { 43010Sstevel@tonic-gate uint32_t val; 43020Sstevel@tonic-gate 43030Sstevel@tonic-gate val = 1; 43040Sstevel@tonic-gate error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 43055240Snordmark &val, (t_uscalar_t)sizeof (val)); 43060Sstevel@tonic-gate if (error) 43070Sstevel@tonic-gate return (error); 43080Sstevel@tonic-gate dontroute = 1; 43090Sstevel@tonic-gate } 43100Sstevel@tonic-gate 43110Sstevel@tonic-gate if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) { 43120Sstevel@tonic-gate error = EOPNOTSUPP; 43130Sstevel@tonic-gate goto done; 43140Sstevel@tonic-gate } 43150Sstevel@tonic-gate if (msg->msg_controllen != 0) { 43160Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) { 43170Sstevel@tonic-gate error = sosend_dgramcmsg(so, name, namelen, uiop, 4318741Smasputra msg->msg_control, msg->msg_controllen, flags); 43190Sstevel@tonic-gate } else { 43200Sstevel@tonic-gate if (flags & MSG_OOB) { 43210Sstevel@tonic-gate /* Can't generate T_EXDATA_REQ with options */ 43220Sstevel@tonic-gate error = EOPNOTSUPP; 43230Sstevel@tonic-gate goto done; 43240Sstevel@tonic-gate } 43250Sstevel@tonic-gate error = sosend_svccmsg(so, uiop, 43265240Snordmark !(flags & MSG_EOR), 43275240Snordmark msg->msg_control, msg->msg_controllen, 43285240Snordmark flags); 43290Sstevel@tonic-gate } 43300Sstevel@tonic-gate goto done; 43310Sstevel@tonic-gate } 43320Sstevel@tonic-gate 43330Sstevel@tonic-gate if (!(so_mode & SM_CONNREQUIRED)) { 43340Sstevel@tonic-gate /* 43350Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately 4336741Smasputra * from send_dgram. This can allow tail-call optimizations. 43370Sstevel@tonic-gate */ 43380Sstevel@tonic-gate if (!dontroute) { 43390Sstevel@tonic-gate return (sosend_dgram(so, name, namelen, uiop, flags)); 43400Sstevel@tonic-gate } 43410Sstevel@tonic-gate error = sosend_dgram(so, name, namelen, uiop, flags); 43420Sstevel@tonic-gate } else { 43430Sstevel@tonic-gate t_scalar_t prim; 43440Sstevel@tonic-gate int sflag; 43450Sstevel@tonic-gate 43460Sstevel@tonic-gate /* Ignore msg_name in the connected state */ 43470Sstevel@tonic-gate if (flags & MSG_OOB) { 43480Sstevel@tonic-gate prim = T_EXDATA_REQ; 43490Sstevel@tonic-gate /* 43500Sstevel@tonic-gate * Send down T_EXDATA_REQ even if there is flow 43510Sstevel@tonic-gate * control for data. 43520Sstevel@tonic-gate */ 43530Sstevel@tonic-gate sflag = MSG_IGNFLOW; 43540Sstevel@tonic-gate } else { 43550Sstevel@tonic-gate if (so_mode & SM_BYTESTREAM) { 43560Sstevel@tonic-gate /* Byte stream transport - use write */ 43570Sstevel@tonic-gate 43580Sstevel@tonic-gate dprintso(so, 1, ("sotpi_sendmsg: write\n")); 43590Sstevel@tonic-gate /* 4360741Smasputra * If there is no SO_DONTROUTE to turn off, 4361741Smasputra * SS_DIRECT is on, and there is no flow 4362741Smasputra * control, we can take the fast path. 43630Sstevel@tonic-gate */ 4364741Smasputra if (!dontroute && 4365741Smasputra (so_state & SS_DIRECT) && 4366741Smasputra canputnext(SOTOV(so)->v_stream->sd_wrq)) { 4367741Smasputra return (sostream_direct(so, uiop, 4368741Smasputra NULL, CRED())); 4369741Smasputra } 43700Sstevel@tonic-gate error = strwrite(SOTOV(so), uiop, CRED()); 43710Sstevel@tonic-gate goto done; 43720Sstevel@tonic-gate } 43730Sstevel@tonic-gate prim = T_DATA_REQ; 43740Sstevel@tonic-gate sflag = 0; 43750Sstevel@tonic-gate } 43760Sstevel@tonic-gate /* 43770Sstevel@tonic-gate * If there is no SO_DONTROUTE to turn off return immediately 43780Sstevel@tonic-gate * from sosend_svc. This can allow tail-call optimizations. 43790Sstevel@tonic-gate */ 43800Sstevel@tonic-gate if (!dontroute) 43810Sstevel@tonic-gate return (sosend_svc(so, uiop, prim, 43825240Snordmark !(flags & MSG_EOR), sflag)); 43830Sstevel@tonic-gate error = sosend_svc(so, uiop, prim, 43845240Snordmark !(flags & MSG_EOR), sflag); 43850Sstevel@tonic-gate } 43860Sstevel@tonic-gate ASSERT(dontroute); 43870Sstevel@tonic-gate done: 43880Sstevel@tonic-gate if (dontroute) { 43890Sstevel@tonic-gate uint32_t val; 43900Sstevel@tonic-gate 43910Sstevel@tonic-gate val = 0; 43920Sstevel@tonic-gate (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 43935240Snordmark &val, (t_uscalar_t)sizeof (val)); 43940Sstevel@tonic-gate } 43950Sstevel@tonic-gate return (error); 43960Sstevel@tonic-gate } 43970Sstevel@tonic-gate 43980Sstevel@tonic-gate /* 4399741Smasputra * Sending data on a datagram socket. 4400741Smasputra * Assumes caller has verified that SS_ISBOUND etc. are set. 4401741Smasputra */ 4402741Smasputra /* ARGSUSED */ 4403741Smasputra static int 4404741Smasputra sodgram_direct(struct sonode *so, struct sockaddr *name, 4405741Smasputra socklen_t namelen, struct uio *uiop, int flags) 4406741Smasputra { 4407741Smasputra struct T_unitdata_req tudr; 44085240Snordmark mblk_t *mp = NULL; 4409741Smasputra int error = 0; 4410741Smasputra void *addr; 4411741Smasputra socklen_t addrlen; 4412741Smasputra ssize_t len; 4413741Smasputra struct stdata *stp = SOTOV(so)->v_stream; 4414741Smasputra int so_state; 4415741Smasputra queue_t *udp_wq; 44165240Snordmark boolean_t connected; 44175240Snordmark mblk_t *mpdata = NULL; 4418741Smasputra 4419741Smasputra ASSERT(name != NULL && namelen != 0); 4420741Smasputra ASSERT(!(so->so_mode & SM_CONNREQUIRED)); 4421741Smasputra ASSERT(!(so->so_mode & SM_EXDATA)); 4422741Smasputra ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 4423741Smasputra ASSERT(SOTOV(so)->v_type == VSOCK); 4424741Smasputra 4425741Smasputra /* Caller checked for proper length */ 4426741Smasputra len = uiop->uio_resid; 4427741Smasputra ASSERT(len <= so->so_tidu_size); 4428741Smasputra 4429741Smasputra /* Length and family checks have been done by caller */ 4430741Smasputra ASSERT(name->sa_family == so->so_family); 4431741Smasputra ASSERT(so->so_family == AF_INET || 4432741Smasputra (namelen == (socklen_t)sizeof (struct sockaddr_in6))); 4433741Smasputra ASSERT(so->so_family == AF_INET6 || 4434741Smasputra (namelen == (socklen_t)sizeof (struct sockaddr_in))); 4435741Smasputra 4436741Smasputra addr = name; 4437741Smasputra addrlen = namelen; 4438741Smasputra 4439741Smasputra if (stp->sd_sidp != NULL && 4440741Smasputra (error = straccess(stp, JCWRITE)) != 0) 4441741Smasputra goto done; 4442741Smasputra 4443741Smasputra so_state = so->so_state; 4444741Smasputra 44455240Snordmark connected = so_state & SS_ISCONNECTED; 44465240Snordmark if (!connected) { 44475240Snordmark tudr.PRIM_type = T_UNITDATA_REQ; 44485240Snordmark tudr.DEST_length = addrlen; 44495240Snordmark tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 44505240Snordmark tudr.OPT_length = 0; 44515240Snordmark tudr.OPT_offset = 0; 44525240Snordmark 44535240Snordmark mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0, 44545240Snordmark _ALLOC_INTR); 44555240Snordmark if (mp == NULL) { 44565240Snordmark /* 44575240Snordmark * Caught a signal waiting for memory. 44585240Snordmark * Let send* return EINTR. 44595240Snordmark */ 44605240Snordmark error = EINTR; 44615240Snordmark goto done; 44625240Snordmark } 44635240Snordmark } 44645240Snordmark 4465741Smasputra /* 4466741Smasputra * For UDP we don't break up the copyin into smaller pieces 4467741Smasputra * as in the TCP case. That means if ENOMEM is returned by 4468741Smasputra * mcopyinuio() then the uio vector has not been modified at 4469741Smasputra * all and we fallback to either strwrite() or kstrputmsg() 4470741Smasputra * below. Note also that we never generate priority messages 4471741Smasputra * from here. 4472741Smasputra */ 4473741Smasputra udp_wq = stp->sd_wrq->q_next; 4474741Smasputra if (canput(udp_wq) && 44755240Snordmark (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) { 44765240Snordmark ASSERT(DB_TYPE(mpdata) == M_DATA); 4477741Smasputra ASSERT(uiop->uio_resid == 0); 44785240Snordmark if (!connected) 44795240Snordmark linkb(mp, mpdata); 44805240Snordmark else 44815240Snordmark mp = mpdata; 4482741Smasputra if (audit_active) 4483741Smasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 44845240Snordmark 44855240Snordmark udp_wput(udp_wq, mp); 4486741Smasputra return (0); 4487741Smasputra } 44885240Snordmark 44895240Snordmark ASSERT(mpdata == NULL); 44905240Snordmark if (error != 0 && error != ENOMEM) { 44915240Snordmark freemsg(mp); 4492741Smasputra return (error); 44935240Snordmark } 4494741Smasputra 4495741Smasputra /* 4496741Smasputra * For connected, let strwrite() handle the blocking case. 4497741Smasputra * Otherwise we fall thru and use kstrputmsg(). 4498741Smasputra */ 44995240Snordmark if (connected) 4500741Smasputra return (strwrite(SOTOV(so), uiop, CRED())); 4501741Smasputra 4502741Smasputra if (audit_active) 4503741Smasputra audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 4504741Smasputra 4505741Smasputra error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 4506741Smasputra done: 4507741Smasputra #ifdef SOCK_DEBUG 4508741Smasputra if (error != 0) { 4509741Smasputra eprintsoline(so, error); 4510741Smasputra } 4511741Smasputra #endif /* SOCK_DEBUG */ 4512741Smasputra return (error); 4513741Smasputra } 4514741Smasputra 4515741Smasputra int 4516741Smasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr) 4517741Smasputra { 4518741Smasputra struct stdata *stp = SOTOV(so)->v_stream; 4519741Smasputra ssize_t iosize, rmax, maxblk; 4520741Smasputra queue_t *tcp_wq = stp->sd_wrq->q_next; 4521898Skais mblk_t *newmp; 4522741Smasputra int error = 0, wflag = 0; 4523741Smasputra 4524741Smasputra ASSERT(so->so_mode & SM_BYTESTREAM); 4525741Smasputra ASSERT(SOTOV(so)->v_type == VSOCK); 4526741Smasputra 4527741Smasputra if (stp->sd_sidp != NULL && 4528741Smasputra (error = straccess(stp, JCWRITE)) != 0) 4529741Smasputra return (error); 4530741Smasputra 4531741Smasputra if (uiop == NULL) { 4532741Smasputra /* 4533741Smasputra * kstrwritemp() should have checked sd_flag and 4534741Smasputra * flow-control before coming here. If we end up 4535741Smasputra * here it means that we can simply pass down the 4536741Smasputra * data to tcp. 4537741Smasputra */ 4538741Smasputra ASSERT(mp != NULL); 4539898Skais if (stp->sd_wputdatafunc != NULL) { 4540898Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL, 4541898Skais NULL, NULL, NULL); 4542898Skais if (newmp == NULL) { 4543898Skais /* The caller will free mp */ 4544898Skais return (ECOMM); 4545898Skais } 4546898Skais mp = newmp; 4547898Skais } 4548741Smasputra tcp_wput(tcp_wq, mp); 4549741Smasputra return (0); 4550741Smasputra } 4551741Smasputra 4552741Smasputra /* Fallback to strwrite() to do proper error handling */ 4553741Smasputra if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY)) 4554741Smasputra return (strwrite(SOTOV(so), uiop, cr)); 4555741Smasputra 4556741Smasputra rmax = stp->sd_qn_maxpsz; 4557741Smasputra ASSERT(rmax >= 0 || rmax == INFPSZ); 4558741Smasputra if (rmax == 0 || uiop->uio_resid <= 0) 4559741Smasputra return (0); 4560741Smasputra 4561741Smasputra if (rmax == INFPSZ) 4562741Smasputra rmax = uiop->uio_resid; 4563741Smasputra 4564741Smasputra maxblk = stp->sd_maxblk; 4565741Smasputra 4566741Smasputra for (;;) { 4567741Smasputra iosize = MIN(uiop->uio_resid, rmax); 4568741Smasputra 4569741Smasputra mp = mcopyinuio(stp, uiop, iosize, maxblk, &error); 4570741Smasputra if (mp == NULL) { 4571741Smasputra /* 4572741Smasputra * Fallback to strwrite() for ENOMEM; if this 4573741Smasputra * is our first time in this routine and the uio 4574741Smasputra * vector has not been modified, we will end up 4575741Smasputra * calling strwrite() without any flag set. 4576741Smasputra */ 4577741Smasputra if (error == ENOMEM) 4578741Smasputra goto slow_send; 4579741Smasputra else 4580741Smasputra return (error); 4581741Smasputra } 4582741Smasputra ASSERT(uiop->uio_resid >= 0); 4583741Smasputra /* 4584741Smasputra * If mp is non-NULL and ENOMEM is set, it means that 4585741Smasputra * mcopyinuio() was able to break down some of the user 4586741Smasputra * data into one or more mblks. Send the partial data 4587741Smasputra * to tcp and let the rest be handled in strwrite(). 4588741Smasputra */ 4589741Smasputra ASSERT(error == 0 || error == ENOMEM); 4590898Skais if (stp->sd_wputdatafunc != NULL) { 4591898Skais newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL, 4592898Skais NULL, NULL, NULL); 4593898Skais if (newmp == NULL) { 4594898Skais /* The caller will free mp */ 4595898Skais return (ECOMM); 4596898Skais } 4597898Skais mp = newmp; 4598898Skais } 4599741Smasputra tcp_wput(tcp_wq, mp); 4600741Smasputra 4601741Smasputra wflag |= NOINTR; 4602741Smasputra 4603741Smasputra if (uiop->uio_resid == 0) { /* No more data; we're done */ 4604741Smasputra ASSERT(error == 0); 4605741Smasputra break; 4606741Smasputra } else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag & 4607741Smasputra (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) { 4608741Smasputra slow_send: 4609741Smasputra /* 4610741Smasputra * We were able to send down partial data using 4611741Smasputra * the direct call interface, but are now relying 4612741Smasputra * on strwrite() to handle the non-fastpath cases. 4613741Smasputra * If the socket is blocking we will sleep in 4614741Smasputra * strwaitq() until write is permitted, otherwise, 4615741Smasputra * we will need to return the amount of bytes 4616741Smasputra * written so far back to the app. This is the 4617741Smasputra * reason why we pass NOINTR flag to strwrite() 4618741Smasputra * for non-blocking socket, because we don't want 4619741Smasputra * to return EAGAIN when portion of the user data 4620741Smasputra * has actually been sent down. 4621741Smasputra */ 4622741Smasputra return (strwrite_common(SOTOV(so), uiop, cr, wflag)); 4623741Smasputra } 4624741Smasputra } 4625741Smasputra return (0); 4626741Smasputra } 4627741Smasputra 4628741Smasputra /* 46290Sstevel@tonic-gate * Update so_faddr by asking the transport (unless AF_UNIX). 46300Sstevel@tonic-gate */ 46310Sstevel@tonic-gate int 46320Sstevel@tonic-gate sotpi_getpeername(struct sonode *so) 46330Sstevel@tonic-gate { 46340Sstevel@tonic-gate struct strbuf strbuf; 46350Sstevel@tonic-gate int error = 0, res; 46360Sstevel@tonic-gate void *addr; 46370Sstevel@tonic-gate t_uscalar_t addrlen; 46380Sstevel@tonic-gate k_sigset_t smask; 46390Sstevel@tonic-gate 46400Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername(%p) %s\n", 4641*7240Srh87107 (void *)so, pr_state(so->so_state, so->so_mode))); 46420Sstevel@tonic-gate 46430Sstevel@tonic-gate mutex_enter(&so->so_lock); 46440Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 46450Sstevel@tonic-gate if (!(so->so_state & SS_ISCONNECTED)) { 46460Sstevel@tonic-gate error = ENOTCONN; 46470Sstevel@tonic-gate goto done; 46480Sstevel@tonic-gate } 46490Sstevel@tonic-gate /* Added this check for X/Open */ 46500Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 46510Sstevel@tonic-gate error = EINVAL; 46520Sstevel@tonic-gate if (xnet_check_print) { 46530Sstevel@tonic-gate printf("sockfs: X/Open getpeername check => EINVAL\n"); 46540Sstevel@tonic-gate } 46550Sstevel@tonic-gate goto done; 46560Sstevel@tonic-gate } 46570Sstevel@tonic-gate #ifdef DEBUG 46580Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (local): %s\n", 46595240Snordmark pr_addr(so->so_family, so->so_faddr_sa, 46605240Snordmark (t_uscalar_t)so->so_faddr_len))); 46610Sstevel@tonic-gate #endif /* DEBUG */ 46620Sstevel@tonic-gate 46631548Srshoaib if (so->so_family == AF_UNIX) { 46640Sstevel@tonic-gate /* Transport has different name space - return local info */ 46650Sstevel@tonic-gate error = 0; 46660Sstevel@tonic-gate goto done; 46670Sstevel@tonic-gate } 46680Sstevel@tonic-gate 46690Sstevel@tonic-gate ASSERT(so->so_faddr_sa); 46700Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */ 46710Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_faddr_maxlen; 46720Sstevel@tonic-gate mutex_exit(&so->so_lock); 46730Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP); 46740Sstevel@tonic-gate 46750Sstevel@tonic-gate /* 46760Sstevel@tonic-gate * Issue TI_GETPEERNAME with signals masked. 46770Sstevel@tonic-gate * Put the result in so_faddr_sa so that getpeername works after 46780Sstevel@tonic-gate * a shutdown(output). 46790Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 46800Sstevel@tonic-gate * back to the socket. 46810Sstevel@tonic-gate */ 46820Sstevel@tonic-gate strbuf.buf = addr; 46830Sstevel@tonic-gate strbuf.maxlen = addrlen; 46840Sstevel@tonic-gate strbuf.len = 0; 46850Sstevel@tonic-gate 46860Sstevel@tonic-gate sigintr(&smask, 0); 46870Sstevel@tonic-gate res = 0; 46880Sstevel@tonic-gate ASSERT(CRED()); 46890Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf, 46905240Snordmark 0, K_TO_K, CRED(), &res); 46910Sstevel@tonic-gate sigunintr(&smask); 46920Sstevel@tonic-gate 46930Sstevel@tonic-gate mutex_enter(&so->so_lock); 46940Sstevel@tonic-gate /* 46950Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail 46960Sstevel@tonic-gate * the getpeername. Instead fallback on the recorded 46970Sstevel@tonic-gate * so->so_faddr_sa. 46980Sstevel@tonic-gate */ 46990Sstevel@tonic-gate if (error) { 47000Sstevel@tonic-gate /* 47010Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl. 47020Sstevel@tonic-gate * However, it is impossible to determine which ones of 47030Sstevel@tonic-gate * these are really socket level errors that were incorrectly 47040Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the 47050Sstevel@tonic-gate * error - to code explicitly does not reinstate the error 47060Sstevel@tonic-gate * using soseterror(). 47070Sstevel@tonic-gate * Experiments have shows that at least this set of 47080Sstevel@tonic-gate * errors are reported and should not be reinstated on the 47090Sstevel@tonic-gate * socket: 47100Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when 47110Sstevel@tonic-gate * getpeername was called. 47120Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write 47130Sstevel@tonic-gate * side error over the read side error. 47140Sstevel@tonic-gate * ENOTCONN The transport just got disconnected but 47150Sstevel@tonic-gate * sockfs had not yet seen the T_DISCON_IND 47160Sstevel@tonic-gate * when issuing the ioctl. 47170Sstevel@tonic-gate */ 47180Sstevel@tonic-gate error = 0; 47190Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 && 47200Sstevel@tonic-gate (so->so_state & SS_ISCONNECTED)) { 47210Sstevel@tonic-gate ASSERT(strbuf.len <= (int)so->so_faddr_maxlen); 47220Sstevel@tonic-gate so->so_faddr_len = (socklen_t)strbuf.len; 47230Sstevel@tonic-gate bcopy(addr, so->so_faddr_sa, so->so_faddr_len); 47240Sstevel@tonic-gate so->so_state |= SS_FADDR_VALID; 47250Sstevel@tonic-gate } 47260Sstevel@tonic-gate kmem_free(addr, addrlen); 47270Sstevel@tonic-gate #ifdef DEBUG 47280Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getpeername (tp): %s\n", 47295240Snordmark pr_addr(so->so_family, so->so_faddr_sa, 47305240Snordmark (t_uscalar_t)so->so_faddr_len))); 47310Sstevel@tonic-gate #endif /* DEBUG */ 47320Sstevel@tonic-gate done: 47330Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 47340Sstevel@tonic-gate mutex_exit(&so->so_lock); 47350Sstevel@tonic-gate return (error); 47360Sstevel@tonic-gate } 47370Sstevel@tonic-gate 47380Sstevel@tonic-gate /* 47390Sstevel@tonic-gate * Update so_laddr by asking the transport (unless AF_UNIX). 47400Sstevel@tonic-gate */ 47410Sstevel@tonic-gate int 47420Sstevel@tonic-gate sotpi_getsockname(struct sonode *so) 47430Sstevel@tonic-gate { 47440Sstevel@tonic-gate struct strbuf strbuf; 47450Sstevel@tonic-gate int error = 0, res; 47460Sstevel@tonic-gate void *addr; 47470Sstevel@tonic-gate t_uscalar_t addrlen; 47480Sstevel@tonic-gate k_sigset_t smask; 47490Sstevel@tonic-gate 47500Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname(%p) %s\n", 4751*7240Srh87107 (void *)so, pr_state(so->so_state, so->so_mode))); 47520Sstevel@tonic-gate 47530Sstevel@tonic-gate mutex_enter(&so->so_lock); 47540Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 47550Sstevel@tonic-gate if (!(so->so_state & SS_ISBOUND) && so->so_family != AF_UNIX) { 47560Sstevel@tonic-gate /* Return an all zero address except for the family */ 47570Sstevel@tonic-gate if (so->so_family == AF_INET) 47580Sstevel@tonic-gate so->so_laddr_len = (socklen_t)sizeof (sin_t); 47590Sstevel@tonic-gate else if (so->so_family == AF_INET6) 47600Sstevel@tonic-gate so->so_laddr_len = (socklen_t)sizeof (sin6_t); 47610Sstevel@tonic-gate ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 47620Sstevel@tonic-gate bzero(so->so_laddr_sa, so->so_laddr_len); 47630Sstevel@tonic-gate /* 47640Sstevel@tonic-gate * Can not assume there is a sa_family for all 47650Sstevel@tonic-gate * protocol families. 47660Sstevel@tonic-gate */ 47670Sstevel@tonic-gate if (so->so_family == AF_INET || so->so_family == AF_INET6) 47680Sstevel@tonic-gate so->so_laddr_sa->sa_family = so->so_family; 47690Sstevel@tonic-gate } 47700Sstevel@tonic-gate #ifdef DEBUG 47710Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (local): %s\n", 47725240Snordmark pr_addr(so->so_family, so->so_laddr_sa, 47735240Snordmark (t_uscalar_t)so->so_laddr_len))); 47740Sstevel@tonic-gate #endif /* DEBUG */ 47750Sstevel@tonic-gate if (so->so_family == AF_UNIX) { 47760Sstevel@tonic-gate /* Transport has different name space - return local info */ 47770Sstevel@tonic-gate error = 0; 47780Sstevel@tonic-gate goto done; 47790Sstevel@tonic-gate } 47801156Sblu if (!(so->so_state & SS_ISBOUND)) { 47811156Sblu /* If not bound, then nothing to return. */ 47821156Sblu error = 0; 47831156Sblu goto done; 47841156Sblu } 47850Sstevel@tonic-gate /* Allocate local buffer to use with ioctl */ 47860Sstevel@tonic-gate addrlen = (t_uscalar_t)so->so_laddr_maxlen; 47870Sstevel@tonic-gate mutex_exit(&so->so_lock); 47880Sstevel@tonic-gate addr = kmem_alloc(addrlen, KM_SLEEP); 47890Sstevel@tonic-gate 47900Sstevel@tonic-gate /* 47910Sstevel@tonic-gate * Issue TI_GETMYNAME with signals masked. 47920Sstevel@tonic-gate * Put the result in so_laddr_sa so that getsockname works after 47930Sstevel@tonic-gate * a shutdown(output). 47940Sstevel@tonic-gate * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 47950Sstevel@tonic-gate * back to the socket. 47960Sstevel@tonic-gate */ 47970Sstevel@tonic-gate strbuf.buf = addr; 47980Sstevel@tonic-gate strbuf.maxlen = addrlen; 47990Sstevel@tonic-gate strbuf.len = 0; 48000Sstevel@tonic-gate 48010Sstevel@tonic-gate sigintr(&smask, 0); 48020Sstevel@tonic-gate res = 0; 48030Sstevel@tonic-gate ASSERT(CRED()); 48040Sstevel@tonic-gate error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf, 48055240Snordmark 0, K_TO_K, CRED(), &res); 48060Sstevel@tonic-gate sigunintr(&smask); 48070Sstevel@tonic-gate 48080Sstevel@tonic-gate mutex_enter(&so->so_lock); 48090Sstevel@tonic-gate /* 48100Sstevel@tonic-gate * If there is an error record the error in so_error put don't fail 48110Sstevel@tonic-gate * the getsockname. Instead fallback on the recorded 48120Sstevel@tonic-gate * so->so_laddr_sa. 48130Sstevel@tonic-gate */ 48140Sstevel@tonic-gate if (error) { 48150Sstevel@tonic-gate /* 48160Sstevel@tonic-gate * Various stream head errors can be returned to the ioctl. 48170Sstevel@tonic-gate * However, it is impossible to determine which ones of 48180Sstevel@tonic-gate * these are really socket level errors that were incorrectly 48190Sstevel@tonic-gate * consumed by the ioctl. Thus this code silently ignores the 48200Sstevel@tonic-gate * error - to code explicitly does not reinstate the error 48210Sstevel@tonic-gate * using soseterror(). 48220Sstevel@tonic-gate * Experiments have shows that at least this set of 48230Sstevel@tonic-gate * errors are reported and should not be reinstated on the 48240Sstevel@tonic-gate * socket: 48250Sstevel@tonic-gate * EINVAL E.g. if an I_LINK was in effect when 48260Sstevel@tonic-gate * getsockname was called. 48270Sstevel@tonic-gate * EPIPE The ioctl error semantics prefer the write 48280Sstevel@tonic-gate * side error over the read side error. 48290Sstevel@tonic-gate */ 48300Sstevel@tonic-gate error = 0; 48310Sstevel@tonic-gate } else if (res == 0 && strbuf.len > 0 && 48320Sstevel@tonic-gate (so->so_state & SS_ISBOUND)) { 48330Sstevel@tonic-gate ASSERT(strbuf.len <= (int)so->so_laddr_maxlen); 48340Sstevel@tonic-gate so->so_laddr_len = (socklen_t)strbuf.len; 48350Sstevel@tonic-gate bcopy(addr, so->so_laddr_sa, so->so_laddr_len); 48360Sstevel@tonic-gate so->so_state |= SS_LADDR_VALID; 48370Sstevel@tonic-gate } 48380Sstevel@tonic-gate kmem_free(addr, addrlen); 48390Sstevel@tonic-gate #ifdef DEBUG 48400Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockname (tp): %s\n", 48415240Snordmark pr_addr(so->so_family, so->so_laddr_sa, 48425240Snordmark (t_uscalar_t)so->so_laddr_len))); 48430Sstevel@tonic-gate #endif /* DEBUG */ 48440Sstevel@tonic-gate done: 48450Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 48460Sstevel@tonic-gate mutex_exit(&so->so_lock); 48470Sstevel@tonic-gate return (error); 48480Sstevel@tonic-gate } 48490Sstevel@tonic-gate 48500Sstevel@tonic-gate /* 48510Sstevel@tonic-gate * Get socket options. For SOL_SOCKET options some options are handled 48520Sstevel@tonic-gate * by the sockfs while others use the value recorded in the sonode as a 48530Sstevel@tonic-gate * fallback should the T_SVR4_OPTMGMT_REQ fail. 48540Sstevel@tonic-gate * 48550Sstevel@tonic-gate * On the return most *optlenp bytes are copied to optval. 48560Sstevel@tonic-gate */ 48570Sstevel@tonic-gate int 48580Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name, 48590Sstevel@tonic-gate void *optval, socklen_t *optlenp, int flags) 48600Sstevel@tonic-gate { 48610Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req; 48620Sstevel@tonic-gate struct T_optmgmt_ack *optmgmt_ack; 48630Sstevel@tonic-gate struct opthdr oh; 48640Sstevel@tonic-gate struct opthdr *opt_res; 48650Sstevel@tonic-gate mblk_t *mp = NULL; 48660Sstevel@tonic-gate int error = 0; 48670Sstevel@tonic-gate void *option = NULL; /* Set if fallback value */ 48680Sstevel@tonic-gate t_uscalar_t maxlen = *optlenp; 48690Sstevel@tonic-gate t_uscalar_t len; 48700Sstevel@tonic-gate uint32_t value; 48710Sstevel@tonic-gate 48720Sstevel@tonic-gate dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n", 4873*7240Srh87107 (void *)so, level, option_name, optval, (void *)optlenp, 48745240Snordmark pr_state(so->so_state, so->so_mode))); 48750Sstevel@tonic-gate 48760Sstevel@tonic-gate mutex_enter(&so->so_lock); 48770Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 48780Sstevel@tonic-gate 48790Sstevel@tonic-gate /* 48800Sstevel@tonic-gate * Check for SOL_SOCKET options. 48810Sstevel@tonic-gate * Certain SOL_SOCKET options are returned directly whereas 48820Sstevel@tonic-gate * others only provide a default (fallback) value should 48830Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ fail. 48840Sstevel@tonic-gate */ 48850Sstevel@tonic-gate if (level == SOL_SOCKET) { 48860Sstevel@tonic-gate /* Check parameters */ 48870Sstevel@tonic-gate switch (option_name) { 48880Sstevel@tonic-gate case SO_TYPE: 48890Sstevel@tonic-gate case SO_ERROR: 48900Sstevel@tonic-gate case SO_DEBUG: 48910Sstevel@tonic-gate case SO_ACCEPTCONN: 48920Sstevel@tonic-gate case SO_REUSEADDR: 48930Sstevel@tonic-gate case SO_KEEPALIVE: 48940Sstevel@tonic-gate case SO_DONTROUTE: 48950Sstevel@tonic-gate case SO_BROADCAST: 48960Sstevel@tonic-gate case SO_USELOOPBACK: 48970Sstevel@tonic-gate case SO_OOBINLINE: 48980Sstevel@tonic-gate case SO_SNDBUF: 48990Sstevel@tonic-gate case SO_RCVBUF: 49000Sstevel@tonic-gate #ifdef notyet 49010Sstevel@tonic-gate case SO_SNDLOWAT: 49020Sstevel@tonic-gate case SO_RCVLOWAT: 49030Sstevel@tonic-gate case SO_SNDTIMEO: 49040Sstevel@tonic-gate case SO_RCVTIMEO: 49050Sstevel@tonic-gate #endif /* notyet */ 49063388Skcpoon case SO_DOMAIN: 49070Sstevel@tonic-gate case SO_DGRAM_ERRIND: 49080Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (int32_t)) { 49090Sstevel@tonic-gate error = EINVAL; 49100Sstevel@tonic-gate eprintsoline(so, error); 49110Sstevel@tonic-gate goto done2; 49120Sstevel@tonic-gate } 49130Sstevel@tonic-gate break; 49140Sstevel@tonic-gate case SO_LINGER: 49150Sstevel@tonic-gate if (maxlen < (t_uscalar_t)sizeof (struct linger)) { 49160Sstevel@tonic-gate error = EINVAL; 49170Sstevel@tonic-gate eprintsoline(so, error); 49180Sstevel@tonic-gate goto done2; 49190Sstevel@tonic-gate } 49200Sstevel@tonic-gate break; 49210Sstevel@tonic-gate } 49220Sstevel@tonic-gate 49230Sstevel@tonic-gate len = (t_uscalar_t)sizeof (uint32_t); /* Default */ 49240Sstevel@tonic-gate 49250Sstevel@tonic-gate switch (option_name) { 49260Sstevel@tonic-gate case SO_TYPE: 49270Sstevel@tonic-gate value = so->so_type; 49280Sstevel@tonic-gate option = &value; 49290Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 49300Sstevel@tonic-gate 49310Sstevel@tonic-gate case SO_ERROR: 49320Sstevel@tonic-gate value = sogeterr(so); 49330Sstevel@tonic-gate option = &value; 49340Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 49350Sstevel@tonic-gate 49360Sstevel@tonic-gate case SO_ACCEPTCONN: 49370Sstevel@tonic-gate if (so->so_state & SS_ACCEPTCONN) 49380Sstevel@tonic-gate value = SO_ACCEPTCONN; 49390Sstevel@tonic-gate else 49400Sstevel@tonic-gate value = 0; 49410Sstevel@tonic-gate #ifdef DEBUG 49420Sstevel@tonic-gate if (value) { 49430Sstevel@tonic-gate dprintso(so, 1, 49440Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n", 49450Sstevel@tonic-gate option_name)); 49460Sstevel@tonic-gate } else { 49470Sstevel@tonic-gate dprintso(so, 1, 49480Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n", 49490Sstevel@tonic-gate option_name)); 49500Sstevel@tonic-gate } 49510Sstevel@tonic-gate #endif /* DEBUG */ 49520Sstevel@tonic-gate option = &value; 49530Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 49540Sstevel@tonic-gate 49550Sstevel@tonic-gate case SO_DEBUG: 49560Sstevel@tonic-gate case SO_REUSEADDR: 49570Sstevel@tonic-gate case SO_KEEPALIVE: 49580Sstevel@tonic-gate case SO_DONTROUTE: 49590Sstevel@tonic-gate case SO_BROADCAST: 49600Sstevel@tonic-gate case SO_USELOOPBACK: 49610Sstevel@tonic-gate case SO_OOBINLINE: 49620Sstevel@tonic-gate case SO_DGRAM_ERRIND: 49630Sstevel@tonic-gate value = (so->so_options & option_name); 49640Sstevel@tonic-gate #ifdef DEBUG 49650Sstevel@tonic-gate if (value) { 49660Sstevel@tonic-gate dprintso(so, 1, 49670Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x is set\n", 49680Sstevel@tonic-gate option_name)); 49690Sstevel@tonic-gate } else { 49700Sstevel@tonic-gate dprintso(so, 1, 49710Sstevel@tonic-gate ("sotpi_getsockopt: 0x%x not set\n", 49720Sstevel@tonic-gate option_name)); 49730Sstevel@tonic-gate } 49740Sstevel@tonic-gate #endif /* DEBUG */ 49750Sstevel@tonic-gate option = &value; 49760Sstevel@tonic-gate goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 49770Sstevel@tonic-gate 49780Sstevel@tonic-gate /* 49790Sstevel@tonic-gate * The following options are only returned by sockfs when the 49800Sstevel@tonic-gate * T_SVR4_OPTMGMT_REQ fails. 49810Sstevel@tonic-gate */ 49820Sstevel@tonic-gate case SO_LINGER: 49830Sstevel@tonic-gate option = &so->so_linger; 49840Sstevel@tonic-gate len = (t_uscalar_t)sizeof (struct linger); 49850Sstevel@tonic-gate break; 49860Sstevel@tonic-gate case SO_SNDBUF: { 49870Sstevel@tonic-gate ssize_t lvalue; 49880Sstevel@tonic-gate 49890Sstevel@tonic-gate /* 49900Sstevel@tonic-gate * If the option has not been set then get a default 49910Sstevel@tonic-gate * value from the read queue. This value is 49920Sstevel@tonic-gate * returned if the transport fails 49930Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ. 49940Sstevel@tonic-gate */ 49950Sstevel@tonic-gate lvalue = so->so_sndbuf; 49960Sstevel@tonic-gate if (lvalue == 0) { 49970Sstevel@tonic-gate mutex_exit(&so->so_lock); 49980Sstevel@tonic-gate (void) strqget(strvp2wq(SOTOV(so))->q_next, 49995240Snordmark QHIWAT, 0, &lvalue); 50000Sstevel@tonic-gate mutex_enter(&so->so_lock); 50010Sstevel@tonic-gate dprintso(so, 1, 50020Sstevel@tonic-gate ("got SO_SNDBUF %ld from q\n", lvalue)); 50030Sstevel@tonic-gate } 50040Sstevel@tonic-gate value = (int)lvalue; 50050Sstevel@tonic-gate option = &value; 50060Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_sndbuf); 50070Sstevel@tonic-gate break; 50080Sstevel@tonic-gate } 50090Sstevel@tonic-gate case SO_RCVBUF: { 50100Sstevel@tonic-gate ssize_t lvalue; 50110Sstevel@tonic-gate 50120Sstevel@tonic-gate /* 50130Sstevel@tonic-gate * If the option has not been set then get a default 50140Sstevel@tonic-gate * value from the read queue. This value is 50150Sstevel@tonic-gate * returned if the transport fails 50160Sstevel@tonic-gate * the T_SVR4_OPTMGMT_REQ. 50170Sstevel@tonic-gate * 50180Sstevel@tonic-gate * XXX If SO_RCVBUF has been set and this is an 50190Sstevel@tonic-gate * XPG 4.2 application then do not ask the transport 50200Sstevel@tonic-gate * since the transport might adjust the value and not 50210Sstevel@tonic-gate * return exactly what was set by the application. 50220Sstevel@tonic-gate * For non-XPG 4.2 application we return the value 50230Sstevel@tonic-gate * that the transport is actually using. 50240Sstevel@tonic-gate */ 50250Sstevel@tonic-gate lvalue = so->so_rcvbuf; 50260Sstevel@tonic-gate if (lvalue == 0) { 50270Sstevel@tonic-gate mutex_exit(&so->so_lock); 50280Sstevel@tonic-gate (void) strqget(RD(strvp2wq(SOTOV(so))), 50295240Snordmark QHIWAT, 0, &lvalue); 50300Sstevel@tonic-gate mutex_enter(&so->so_lock); 50310Sstevel@tonic-gate dprintso(so, 1, 50320Sstevel@tonic-gate ("got SO_RCVBUF %ld from q\n", lvalue)); 50330Sstevel@tonic-gate } else if (flags & _SOGETSOCKOPT_XPG4_2) { 50340Sstevel@tonic-gate value = (int)lvalue; 50350Sstevel@tonic-gate option = &value; 50360Sstevel@tonic-gate goto copyout; /* skip asking transport */ 50370Sstevel@tonic-gate } 50380Sstevel@tonic-gate value = (int)lvalue; 50390Sstevel@tonic-gate option = &value; 50400Sstevel@tonic-gate len = (t_uscalar_t)sizeof (so->so_rcvbuf); 50410Sstevel@tonic-gate break; 50420Sstevel@tonic-gate } 50433388Skcpoon case SO_DOMAIN: 50443388Skcpoon value = so->so_family; 50453388Skcpoon option = &value; 50463388Skcpoon goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 50473388Skcpoon 50480Sstevel@tonic-gate #ifdef notyet 50490Sstevel@tonic-gate /* 50500Sstevel@tonic-gate * We do not implement the semantics of these options 50510Sstevel@tonic-gate * thus we shouldn't implement the options either. 50520Sstevel@tonic-gate */ 50530Sstevel@tonic-gate case SO_SNDLOWAT: 50540Sstevel@tonic-gate value = so->so_sndlowat; 50550Sstevel@tonic-gate option = &value; 50560Sstevel@tonic-gate break; 50570Sstevel@tonic-gate case SO_RCVLOWAT: 50580Sstevel@tonic-gate value = so->so_rcvlowat; 50590Sstevel@tonic-gate option = &value; 50600Sstevel@tonic-gate break; 50610Sstevel@tonic-gate case SO_SNDTIMEO: 50620Sstevel@tonic-gate value = so->so_sndtimeo; 50630Sstevel@tonic-gate option = &value; 50640Sstevel@tonic-gate break; 50650Sstevel@tonic-gate case SO_RCVTIMEO: 50660Sstevel@tonic-gate value = so->so_rcvtimeo; 50670Sstevel@tonic-gate option = &value; 50680Sstevel@tonic-gate break; 50690Sstevel@tonic-gate #endif /* notyet */ 50700Sstevel@tonic-gate } 50710Sstevel@tonic-gate } 50720Sstevel@tonic-gate 50730Sstevel@tonic-gate mutex_exit(&so->so_lock); 50740Sstevel@tonic-gate 50750Sstevel@tonic-gate /* Send request */ 50760Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 50770Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_CHECK; 50780Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen); 50790Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 50800Sstevel@tonic-gate 50810Sstevel@tonic-gate oh.level = level; 50820Sstevel@tonic-gate oh.name = option_name; 50830Sstevel@tonic-gate oh.len = maxlen; 50840Sstevel@tonic-gate 50850Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 50860Sstevel@tonic-gate &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP); 50870Sstevel@tonic-gate /* Let option management work in the presence of data flow control */ 50880Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 50895240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 50900Sstevel@tonic-gate mp = NULL; 50910Sstevel@tonic-gate mutex_enter(&so->so_lock); 50920Sstevel@tonic-gate if (error) { 50930Sstevel@tonic-gate eprintsoline(so, error); 50940Sstevel@tonic-gate goto done2; 50950Sstevel@tonic-gate } 50960Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 50970Sstevel@tonic-gate (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0); 50980Sstevel@tonic-gate if (error) { 50990Sstevel@tonic-gate if (option != NULL) { 51000Sstevel@tonic-gate /* We have a fallback value */ 51010Sstevel@tonic-gate error = 0; 51020Sstevel@tonic-gate goto copyout; 51030Sstevel@tonic-gate } 51040Sstevel@tonic-gate eprintsoline(so, error); 51050Sstevel@tonic-gate goto done2; 51060Sstevel@tonic-gate } 51070Sstevel@tonic-gate ASSERT(mp); 51080Sstevel@tonic-gate optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr; 51090Sstevel@tonic-gate opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset, 51105240Snordmark optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE); 51110Sstevel@tonic-gate if (opt_res == NULL) { 51120Sstevel@tonic-gate if (option != NULL) { 51130Sstevel@tonic-gate /* We have a fallback value */ 51140Sstevel@tonic-gate error = 0; 51150Sstevel@tonic-gate goto copyout; 51160Sstevel@tonic-gate } 51170Sstevel@tonic-gate error = EPROTO; 51180Sstevel@tonic-gate eprintsoline(so, error); 51190Sstevel@tonic-gate goto done; 51200Sstevel@tonic-gate } 51210Sstevel@tonic-gate option = &opt_res[1]; 51220Sstevel@tonic-gate 51230Sstevel@tonic-gate /* check to ensure that the option is within bounds */ 51240Sstevel@tonic-gate if (((uintptr_t)option + opt_res->len < (uintptr_t)option) || 51255240Snordmark (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) { 51260Sstevel@tonic-gate if (option != NULL) { 51270Sstevel@tonic-gate /* We have a fallback value */ 51280Sstevel@tonic-gate error = 0; 51290Sstevel@tonic-gate goto copyout; 51300Sstevel@tonic-gate } 51310Sstevel@tonic-gate error = EPROTO; 51320Sstevel@tonic-gate eprintsoline(so, error); 51330Sstevel@tonic-gate goto done; 51340Sstevel@tonic-gate } 51350Sstevel@tonic-gate 51360Sstevel@tonic-gate len = opt_res->len; 51370Sstevel@tonic-gate 51380Sstevel@tonic-gate copyout: { 51390Sstevel@tonic-gate t_uscalar_t size = MIN(len, maxlen); 51400Sstevel@tonic-gate bcopy(option, optval, size); 51410Sstevel@tonic-gate bcopy(&size, optlenp, sizeof (size)); 51420Sstevel@tonic-gate } 51430Sstevel@tonic-gate done: 51440Sstevel@tonic-gate freemsg(mp); 51450Sstevel@tonic-gate done2: 51460Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 51470Sstevel@tonic-gate mutex_exit(&so->so_lock); 51480Sstevel@tonic-gate return (error); 51490Sstevel@tonic-gate } 51500Sstevel@tonic-gate 51510Sstevel@tonic-gate /* 51520Sstevel@tonic-gate * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ. 51530Sstevel@tonic-gate * SOL_SOCKET options are also recorded in the sonode. A setsockopt for 51540Sstevel@tonic-gate * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails - 51550Sstevel@tonic-gate * setsockopt has to work even if the transport does not support the option. 51560Sstevel@tonic-gate */ 51570Sstevel@tonic-gate int 51580Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name, 51590Sstevel@tonic-gate const void *optval, t_uscalar_t optlen) 51600Sstevel@tonic-gate { 51610Sstevel@tonic-gate struct T_optmgmt_req optmgmt_req; 51620Sstevel@tonic-gate struct opthdr oh; 51630Sstevel@tonic-gate mblk_t *mp; 51640Sstevel@tonic-gate int error = 0; 51650Sstevel@tonic-gate boolean_t handled = B_FALSE; 51660Sstevel@tonic-gate 51670Sstevel@tonic-gate dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n", 5168*7240Srh87107 (void *)so, level, option_name, optval, optlen, 51695240Snordmark pr_state(so->so_state, so->so_mode))); 51700Sstevel@tonic-gate 51710Sstevel@tonic-gate 51720Sstevel@tonic-gate /* X/Open requires this check */ 51730Sstevel@tonic-gate if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 51740Sstevel@tonic-gate if (xnet_check_print) 51750Sstevel@tonic-gate printf("sockfs: X/Open setsockopt check => EINVAL\n"); 51760Sstevel@tonic-gate return (EINVAL); 51770Sstevel@tonic-gate } 51780Sstevel@tonic-gate 51790Sstevel@tonic-gate /* Caller allocates aligned optval, or passes null */ 51800Sstevel@tonic-gate ASSERT(((uintptr_t)optval & (sizeof (t_scalar_t) - 1)) == 0); 51810Sstevel@tonic-gate /* If optval is null optlen is 0, and vice-versa */ 51820Sstevel@tonic-gate ASSERT(optval != NULL || optlen == 0); 51830Sstevel@tonic-gate ASSERT(optlen != 0 || optval == NULL); 51840Sstevel@tonic-gate 51850Sstevel@tonic-gate mutex_enter(&so->so_lock); 51860Sstevel@tonic-gate so_lock_single(so); /* Set SOLOCKED */ 51870Sstevel@tonic-gate mutex_exit(&so->so_lock); 51880Sstevel@tonic-gate 51890Sstevel@tonic-gate /* 51900Sstevel@tonic-gate * For SOCKET or TCP level options, try to set it here itself 51910Sstevel@tonic-gate * provided socket has not been popped and we know the tcp 51920Sstevel@tonic-gate * structure (stored in so_priv). 51930Sstevel@tonic-gate */ 51940Sstevel@tonic-gate if ((level == SOL_SOCKET || level == IPPROTO_TCP) && 51950Sstevel@tonic-gate (so->so_family == AF_INET || so->so_family == AF_INET6) && 51960Sstevel@tonic-gate (so->so_version == SOV_SOCKSTREAM) && (so->so_priv != NULL)) { 51970Sstevel@tonic-gate tcp_t *tcp = so->so_priv; 51980Sstevel@tonic-gate boolean_t onoff; 51990Sstevel@tonic-gate 52000Sstevel@tonic-gate #define intvalue (*(int32_t *)optval) 52010Sstevel@tonic-gate 52020Sstevel@tonic-gate switch (level) { 52030Sstevel@tonic-gate case SOL_SOCKET: 52040Sstevel@tonic-gate switch (option_name) { /* Check length param */ 52050Sstevel@tonic-gate case SO_DEBUG: 52060Sstevel@tonic-gate case SO_REUSEADDR: 52070Sstevel@tonic-gate case SO_DONTROUTE: 52080Sstevel@tonic-gate case SO_BROADCAST: 52090Sstevel@tonic-gate case SO_USELOOPBACK: 52100Sstevel@tonic-gate case SO_OOBINLINE: 52110Sstevel@tonic-gate case SO_DGRAM_ERRIND: 52120Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 52130Sstevel@tonic-gate error = EINVAL; 52140Sstevel@tonic-gate eprintsoline(so, error); 52150Sstevel@tonic-gate mutex_enter(&so->so_lock); 52160Sstevel@tonic-gate goto done2; 52170Sstevel@tonic-gate } 52180Sstevel@tonic-gate ASSERT(optval); 52190Sstevel@tonic-gate onoff = intvalue != 0; 52200Sstevel@tonic-gate handled = B_TRUE; 52210Sstevel@tonic-gate break; 52220Sstevel@tonic-gate case SO_LINGER: 52230Sstevel@tonic-gate if (optlen != 52240Sstevel@tonic-gate (t_uscalar_t)sizeof (struct linger)) { 52250Sstevel@tonic-gate error = EINVAL; 52260Sstevel@tonic-gate eprintsoline(so, error); 52270Sstevel@tonic-gate mutex_enter(&so->so_lock); 52280Sstevel@tonic-gate goto done2; 52290Sstevel@tonic-gate } 52300Sstevel@tonic-gate ASSERT(optval); 52310Sstevel@tonic-gate handled = B_TRUE; 52320Sstevel@tonic-gate break; 52330Sstevel@tonic-gate } 52340Sstevel@tonic-gate 52350Sstevel@tonic-gate switch (option_name) { /* Do actions */ 52360Sstevel@tonic-gate case SO_LINGER: { 52370Sstevel@tonic-gate struct linger *lgr = (struct linger *)optval; 52380Sstevel@tonic-gate 52390Sstevel@tonic-gate if (lgr->l_onoff) { 52400Sstevel@tonic-gate tcp->tcp_linger = 1; 52410Sstevel@tonic-gate tcp->tcp_lingertime = lgr->l_linger; 52420Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER; 52430Sstevel@tonic-gate so->so_options |= SO_LINGER; 52440Sstevel@tonic-gate } else { 52450Sstevel@tonic-gate tcp->tcp_linger = 0; 52460Sstevel@tonic-gate tcp->tcp_lingertime = 0; 52470Sstevel@tonic-gate so->so_linger.l_onoff = 0; 52480Sstevel@tonic-gate so->so_options &= ~SO_LINGER; 52490Sstevel@tonic-gate } 52500Sstevel@tonic-gate so->so_linger.l_linger = lgr->l_linger; 52510Sstevel@tonic-gate handled = B_TRUE; 52520Sstevel@tonic-gate break; 52530Sstevel@tonic-gate } 52540Sstevel@tonic-gate case SO_DEBUG: 52550Sstevel@tonic-gate tcp->tcp_debug = onoff; 52560Sstevel@tonic-gate #ifdef SOCK_TEST 52570Sstevel@tonic-gate if (intvalue & 2) 52580Sstevel@tonic-gate sock_test_timelimit = 10 * hz; 52590Sstevel@tonic-gate else 52600Sstevel@tonic-gate sock_test_timelimit = 0; 52610Sstevel@tonic-gate 52620Sstevel@tonic-gate if (intvalue & 4) 52630Sstevel@tonic-gate do_useracc = 0; 52640Sstevel@tonic-gate else 52650Sstevel@tonic-gate do_useracc = 1; 52660Sstevel@tonic-gate #endif /* SOCK_TEST */ 52670Sstevel@tonic-gate break; 52680Sstevel@tonic-gate case SO_DONTROUTE: 52690Sstevel@tonic-gate /* 52700Sstevel@tonic-gate * SO_DONTROUTE, SO_USELOOPBACK and 52710Sstevel@tonic-gate * SO_BROADCAST are only of interest to IP. 52720Sstevel@tonic-gate * We track them here only so 52730Sstevel@tonic-gate * that we can report their current value. 52740Sstevel@tonic-gate */ 52750Sstevel@tonic-gate tcp->tcp_dontroute = onoff; 52760Sstevel@tonic-gate if (onoff) 52770Sstevel@tonic-gate so->so_options |= option_name; 52780Sstevel@tonic-gate else 52790Sstevel@tonic-gate so->so_options &= ~option_name; 52800Sstevel@tonic-gate break; 52810Sstevel@tonic-gate case SO_USELOOPBACK: 52820Sstevel@tonic-gate tcp->tcp_useloopback = onoff; 52830Sstevel@tonic-gate if (onoff) 52840Sstevel@tonic-gate so->so_options |= option_name; 52850Sstevel@tonic-gate else 52860Sstevel@tonic-gate so->so_options &= ~option_name; 52870Sstevel@tonic-gate break; 52880Sstevel@tonic-gate case SO_BROADCAST: 52890Sstevel@tonic-gate tcp->tcp_broadcast = onoff; 52900Sstevel@tonic-gate if (onoff) 52910Sstevel@tonic-gate so->so_options |= option_name; 52920Sstevel@tonic-gate else 52930Sstevel@tonic-gate so->so_options &= ~option_name; 52940Sstevel@tonic-gate break; 52950Sstevel@tonic-gate case SO_REUSEADDR: 52960Sstevel@tonic-gate tcp->tcp_reuseaddr = onoff; 52970Sstevel@tonic-gate if (onoff) 52980Sstevel@tonic-gate so->so_options |= option_name; 52990Sstevel@tonic-gate else 53000Sstevel@tonic-gate so->so_options &= ~option_name; 53010Sstevel@tonic-gate break; 53020Sstevel@tonic-gate case SO_OOBINLINE: 53030Sstevel@tonic-gate tcp->tcp_oobinline = onoff; 53040Sstevel@tonic-gate if (onoff) 53050Sstevel@tonic-gate so->so_options |= option_name; 53060Sstevel@tonic-gate else 53070Sstevel@tonic-gate so->so_options &= ~option_name; 53080Sstevel@tonic-gate break; 53090Sstevel@tonic-gate case SO_DGRAM_ERRIND: 53100Sstevel@tonic-gate tcp->tcp_dgram_errind = onoff; 53110Sstevel@tonic-gate if (onoff) 53120Sstevel@tonic-gate so->so_options |= option_name; 53130Sstevel@tonic-gate else 53140Sstevel@tonic-gate so->so_options &= ~option_name; 53150Sstevel@tonic-gate break; 53160Sstevel@tonic-gate } 53170Sstevel@tonic-gate break; 53180Sstevel@tonic-gate case IPPROTO_TCP: 53190Sstevel@tonic-gate switch (option_name) { 53200Sstevel@tonic-gate case TCP_NODELAY: 53210Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 53220Sstevel@tonic-gate error = EINVAL; 53230Sstevel@tonic-gate eprintsoline(so, error); 53240Sstevel@tonic-gate mutex_enter(&so->so_lock); 53250Sstevel@tonic-gate goto done2; 53260Sstevel@tonic-gate } 53270Sstevel@tonic-gate ASSERT(optval); 53280Sstevel@tonic-gate tcp->tcp_naglim = intvalue ? 1 : tcp->tcp_mss; 53290Sstevel@tonic-gate handled = B_TRUE; 53300Sstevel@tonic-gate break; 53310Sstevel@tonic-gate } 53320Sstevel@tonic-gate break; 53330Sstevel@tonic-gate default: 53340Sstevel@tonic-gate handled = B_FALSE; 53350Sstevel@tonic-gate break; 53360Sstevel@tonic-gate } 53370Sstevel@tonic-gate } 53380Sstevel@tonic-gate 53390Sstevel@tonic-gate if (handled) { 53400Sstevel@tonic-gate mutex_enter(&so->so_lock); 53410Sstevel@tonic-gate goto done2; 53420Sstevel@tonic-gate } 53430Sstevel@tonic-gate 53440Sstevel@tonic-gate optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 53450Sstevel@tonic-gate optmgmt_req.MGMT_flags = T_NEGOTIATE; 53460Sstevel@tonic-gate optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen; 53470Sstevel@tonic-gate optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 53480Sstevel@tonic-gate 53490Sstevel@tonic-gate oh.level = level; 53500Sstevel@tonic-gate oh.name = option_name; 53510Sstevel@tonic-gate oh.len = optlen; 53520Sstevel@tonic-gate 53530Sstevel@tonic-gate mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 53540Sstevel@tonic-gate &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP); 53550Sstevel@tonic-gate /* Let option management work in the presence of data flow control */ 53560Sstevel@tonic-gate error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 53575240Snordmark MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 53580Sstevel@tonic-gate mp = NULL; 53590Sstevel@tonic-gate mutex_enter(&so->so_lock); 53600Sstevel@tonic-gate if (error) { 53610Sstevel@tonic-gate eprintsoline(so, error); 53620Sstevel@tonic-gate goto done; 53630Sstevel@tonic-gate } 53640Sstevel@tonic-gate error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 53650Sstevel@tonic-gate (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0); 53660Sstevel@tonic-gate if (error) { 53670Sstevel@tonic-gate eprintsoline(so, error); 53680Sstevel@tonic-gate goto done; 53690Sstevel@tonic-gate } 53700Sstevel@tonic-gate ASSERT(mp); 53710Sstevel@tonic-gate /* No need to verify T_optmgmt_ack */ 53720Sstevel@tonic-gate freemsg(mp); 53730Sstevel@tonic-gate done: 53740Sstevel@tonic-gate /* 53750Sstevel@tonic-gate * Check for SOL_SOCKET options and record their values. 53760Sstevel@tonic-gate * If we know about a SOL_SOCKET parameter and the transport 53770Sstevel@tonic-gate * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or 53780Sstevel@tonic-gate * EPROTO) we let the setsockopt succeed. 53790Sstevel@tonic-gate */ 53800Sstevel@tonic-gate if (level == SOL_SOCKET) { 53810Sstevel@tonic-gate /* Check parameters */ 53820Sstevel@tonic-gate switch (option_name) { 53830Sstevel@tonic-gate case SO_DEBUG: 53840Sstevel@tonic-gate case SO_REUSEADDR: 53850Sstevel@tonic-gate case SO_KEEPALIVE: 53860Sstevel@tonic-gate case SO_DONTROUTE: 53870Sstevel@tonic-gate case SO_BROADCAST: 53880Sstevel@tonic-gate case SO_USELOOPBACK: 53890Sstevel@tonic-gate case SO_OOBINLINE: 53900Sstevel@tonic-gate case SO_SNDBUF: 53910Sstevel@tonic-gate case SO_RCVBUF: 53920Sstevel@tonic-gate #ifdef notyet 53930Sstevel@tonic-gate case SO_SNDLOWAT: 53940Sstevel@tonic-gate case SO_RCVLOWAT: 53950Sstevel@tonic-gate case SO_SNDTIMEO: 53960Sstevel@tonic-gate case SO_RCVTIMEO: 53970Sstevel@tonic-gate #endif /* notyet */ 53980Sstevel@tonic-gate case SO_DGRAM_ERRIND: 53990Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (int32_t)) { 54000Sstevel@tonic-gate error = EINVAL; 54010Sstevel@tonic-gate eprintsoline(so, error); 54020Sstevel@tonic-gate goto done2; 54030Sstevel@tonic-gate } 54040Sstevel@tonic-gate ASSERT(optval); 54050Sstevel@tonic-gate handled = B_TRUE; 54060Sstevel@tonic-gate break; 54070Sstevel@tonic-gate case SO_LINGER: 54080Sstevel@tonic-gate if (optlen != (t_uscalar_t)sizeof (struct linger)) { 54090Sstevel@tonic-gate error = EINVAL; 54100Sstevel@tonic-gate eprintsoline(so, error); 54110Sstevel@tonic-gate goto done2; 54120Sstevel@tonic-gate } 54130Sstevel@tonic-gate ASSERT(optval); 54140Sstevel@tonic-gate handled = B_TRUE; 54150Sstevel@tonic-gate break; 54160Sstevel@tonic-gate } 54170Sstevel@tonic-gate 54180Sstevel@tonic-gate #define intvalue (*(int32_t *)optval) 54190Sstevel@tonic-gate 54200Sstevel@tonic-gate switch (option_name) { 54210Sstevel@tonic-gate case SO_TYPE: 54220Sstevel@tonic-gate case SO_ERROR: 54230Sstevel@tonic-gate case SO_ACCEPTCONN: 54240Sstevel@tonic-gate /* Can't be set */ 54250Sstevel@tonic-gate error = ENOPROTOOPT; 54260Sstevel@tonic-gate goto done2; 54270Sstevel@tonic-gate case SO_LINGER: { 54280Sstevel@tonic-gate struct linger *l = (struct linger *)optval; 54290Sstevel@tonic-gate 54300Sstevel@tonic-gate so->so_linger.l_linger = l->l_linger; 54310Sstevel@tonic-gate if (l->l_onoff) { 54320Sstevel@tonic-gate so->so_linger.l_onoff = SO_LINGER; 54330Sstevel@tonic-gate so->so_options |= SO_LINGER; 54340Sstevel@tonic-gate } else { 54350Sstevel@tonic-gate so->so_linger.l_onoff = 0; 54360Sstevel@tonic-gate so->so_options &= ~SO_LINGER; 54370Sstevel@tonic-gate } 54380Sstevel@tonic-gate break; 54390Sstevel@tonic-gate } 54400Sstevel@tonic-gate 54410Sstevel@tonic-gate case SO_DEBUG: 54420Sstevel@tonic-gate #ifdef SOCK_TEST 54430Sstevel@tonic-gate if (intvalue & 2) 54440Sstevel@tonic-gate sock_test_timelimit = 10 * hz; 54450Sstevel@tonic-gate else 54460Sstevel@tonic-gate sock_test_timelimit = 0; 54470Sstevel@tonic-gate 54480Sstevel@tonic-gate if (intvalue & 4) 54490Sstevel@tonic-gate do_useracc = 0; 54500Sstevel@tonic-gate else 54510Sstevel@tonic-gate do_useracc = 1; 54520Sstevel@tonic-gate #endif /* SOCK_TEST */ 54530Sstevel@tonic-gate /* FALLTHRU */ 54540Sstevel@tonic-gate case SO_REUSEADDR: 54550Sstevel@tonic-gate case SO_KEEPALIVE: 54560Sstevel@tonic-gate case SO_DONTROUTE: 54570Sstevel@tonic-gate case SO_BROADCAST: 54580Sstevel@tonic-gate case SO_USELOOPBACK: 54590Sstevel@tonic-gate case SO_OOBINLINE: 54600Sstevel@tonic-gate case SO_DGRAM_ERRIND: 54610Sstevel@tonic-gate if (intvalue != 0) { 54620Sstevel@tonic-gate dprintso(so, 1, 54635240Snordmark ("sotpi_setsockopt: setting 0x%x\n", 54645240Snordmark option_name)); 54650Sstevel@tonic-gate so->so_options |= option_name; 54660Sstevel@tonic-gate } else { 54670Sstevel@tonic-gate dprintso(so, 1, 54685240Snordmark ("sotpi_setsockopt: clearing 0x%x\n", 54695240Snordmark option_name)); 54700Sstevel@tonic-gate so->so_options &= ~option_name; 54710Sstevel@tonic-gate } 54720Sstevel@tonic-gate break; 54730Sstevel@tonic-gate /* 54740Sstevel@tonic-gate * The following options are only returned by us when the 54750Sstevel@tonic-gate * T_SVR4_OPTMGMT_REQ fails. 54760Sstevel@tonic-gate * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs 54770Sstevel@tonic-gate * since the transport might adjust the value and not 54780Sstevel@tonic-gate * return exactly what was set by the application. 54790Sstevel@tonic-gate */ 54800Sstevel@tonic-gate case SO_SNDBUF: 54810Sstevel@tonic-gate so->so_sndbuf = intvalue; 54820Sstevel@tonic-gate break; 54830Sstevel@tonic-gate case SO_RCVBUF: 54840Sstevel@tonic-gate so->so_rcvbuf = intvalue; 54850Sstevel@tonic-gate break; 54860Sstevel@tonic-gate #ifdef notyet 54870Sstevel@tonic-gate /* 54880Sstevel@tonic-gate * We do not implement the semantics of these options 54890Sstevel@tonic-gate * thus we shouldn't implement the options either. 54900Sstevel@tonic-gate */ 54910Sstevel@tonic-gate case SO_SNDLOWAT: 54920Sstevel@tonic-gate so->so_sndlowat = intvalue; 54930Sstevel@tonic-gate break; 54940Sstevel@tonic-gate case SO_RCVLOWAT: 54950Sstevel@tonic-gate so->so_rcvlowat = intvalue; 54960Sstevel@tonic-gate break; 54970Sstevel@tonic-gate case SO_SNDTIMEO: 54980Sstevel@tonic-gate so->so_sndtimeo = intvalue; 54990Sstevel@tonic-gate break; 55000Sstevel@tonic-gate case SO_RCVTIMEO: 55010Sstevel@tonic-gate so->so_rcvtimeo = intvalue; 55020Sstevel@tonic-gate break; 55030Sstevel@tonic-gate #endif /* notyet */ 55040Sstevel@tonic-gate } 55050Sstevel@tonic-gate #undef intvalue 55060Sstevel@tonic-gate 55070Sstevel@tonic-gate if (error) { 55080Sstevel@tonic-gate if ((error == ENOPROTOOPT || error == EPROTO || 55090Sstevel@tonic-gate error == EINVAL) && handled) { 55100Sstevel@tonic-gate dprintso(so, 1, 55110Sstevel@tonic-gate ("setsockopt: ignoring error %d for 0x%x\n", 55120Sstevel@tonic-gate error, option_name)); 55130Sstevel@tonic-gate error = 0; 55140Sstevel@tonic-gate } 55150Sstevel@tonic-gate } 55160Sstevel@tonic-gate } 55170Sstevel@tonic-gate done2: 55180Sstevel@tonic-gate ret: 55190Sstevel@tonic-gate so_unlock_single(so, SOLOCKED); 55200Sstevel@tonic-gate mutex_exit(&so->so_lock); 55210Sstevel@tonic-gate return (error); 55220Sstevel@tonic-gate } 5523