10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22132Srobinson
230Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
240Sstevel@tonic-gate /* All Rights Reserved */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
27*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
280Sstevel@tonic-gate * Use is subject to license terms.
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.7 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "mt.h"
340Sstevel@tonic-gate #include <stropts.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <sys/timod.h>
370Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
380Sstevel@tonic-gate #include <sys/tihdr.h>
390Sstevel@tonic-gate #include <xti.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include <signal.h>
420Sstevel@tonic-gate #include <errno.h>
430Sstevel@tonic-gate #include <syslog.h>
440Sstevel@tonic-gate #include "tx.h"
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * If a system call fails with EINTR after T_CONN_REQ is sent out,
480Sstevel@tonic-gate * we change state for caller to continue with t_rcvconnect(). This
490Sstevel@tonic-gate * semantics is not documented for TLI but is the direction taken with
500Sstevel@tonic-gate * XTI so we adopt it. With this the call establishment is completed
510Sstevel@tonic-gate * by calling t_rcvconnect() even for synchronous endpoints.
520Sstevel@tonic-gate */
530Sstevel@tonic-gate int
_tx_connect(int fd,const struct t_call * sndcall,struct t_call * rcvcall,int api_semantics)540Sstevel@tonic-gate _tx_connect(
550Sstevel@tonic-gate int fd,
560Sstevel@tonic-gate const struct t_call *sndcall,
570Sstevel@tonic-gate struct t_call *rcvcall,
580Sstevel@tonic-gate int api_semantics
590Sstevel@tonic-gate )
600Sstevel@tonic-gate {
610Sstevel@tonic-gate int fctlflg;
620Sstevel@tonic-gate struct _ti_user *tiptr;
630Sstevel@tonic-gate sigset_t mask;
640Sstevel@tonic-gate struct strbuf ctlbuf;
650Sstevel@tonic-gate int sv_errno;
660Sstevel@tonic-gate int didalloc;
670Sstevel@tonic-gate
68132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
690Sstevel@tonic-gate return (-1);
700Sstevel@tonic-gate
710Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
720Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * User level state verification only done for XTI
750Sstevel@tonic-gate * because doing for TLI may break existing applications
760Sstevel@tonic-gate */
770Sstevel@tonic-gate if (tiptr->ti_state != T_IDLE) {
780Sstevel@tonic-gate t_errno = TOUTSTATE;
790Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
800Sstevel@tonic-gate return (-1);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate * Acquire ctlbuf for use in sending/receiving control part
860Sstevel@tonic-gate * of the message.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
890Sstevel@tonic-gate sv_errno = errno;
900Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
910Sstevel@tonic-gate errno = sv_errno;
920Sstevel@tonic-gate return (-1);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate * Block all signals until T_CONN_REQ sent and
960Sstevel@tonic-gate * acked with T_OK_ACK/ERROR_ACK
970Sstevel@tonic-gate */
980Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
990Sstevel@tonic-gate if (_t_snd_conn_req(tiptr, sndcall, &ctlbuf) < 0) {
1000Sstevel@tonic-gate sv_errno = errno;
1010Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1020Sstevel@tonic-gate errno = sv_errno;
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * At the TPI level, the error returned in a T_ERROR_ACK
1050Sstevel@tonic-gate * received in response to a T_CONN_REQ for an attempt to
1060Sstevel@tonic-gate * establish a duplicate conection has changed to a
1070Sstevel@tonic-gate * new t_errno code introduced with XTI (ADDRBUSY).
1080Sstevel@tonic-gate * We need to adjust TLI error code to be same as before.
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate if (_T_IS_TLI(api_semantics) && t_errno == TADDRBUSY)
1110Sstevel@tonic-gate /* TLI only */
1120Sstevel@tonic-gate t_errno = TBADADDR;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate goto err_out;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1170Sstevel@tonic-gate
118*1219Sraf if ((fctlflg = fcntl(fd, F_GETFL, 0)) < 0) {
1190Sstevel@tonic-gate t_errno = TSYSERR;
1200Sstevel@tonic-gate goto err_out;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (fctlflg & (O_NDELAY | O_NONBLOCK)) {
1240Sstevel@tonic-gate _T_TX_NEXTSTATE(T_CONNECT2, tiptr,
1250Sstevel@tonic-gate "t_connect: invalid state event T_CONNECT2");
1260Sstevel@tonic-gate t_errno = TNODATA;
1270Sstevel@tonic-gate goto err_out;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * Note: The following call to _t_rcv_conn_con blocks awaiting
1320Sstevel@tonic-gate * T_CONN_CON from remote client. Therefore it drops the
1330Sstevel@tonic-gate * tiptr->lock during the call (and reacquires it)
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate if (_t_rcv_conn_con(tiptr, rcvcall, &ctlbuf, api_semantics) < 0) {
1360Sstevel@tonic-gate if ((t_errno == TSYSERR && errno == EINTR) ||
1370Sstevel@tonic-gate t_errno == TLOOK) {
1380Sstevel@tonic-gate _T_TX_NEXTSTATE(T_CONNECT2, tiptr,
1390Sstevel@tonic-gate "t_connect: invalid state event T_CONNECT2");
1400Sstevel@tonic-gate } else if (t_errno == TBUFOVFLW) {
1410Sstevel@tonic-gate _T_TX_NEXTSTATE(T_CONNECT1, tiptr,
1420Sstevel@tonic-gate "t_connect: invalid state event T_CONNECT1");
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate goto err_out;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate _T_TX_NEXTSTATE(T_CONNECT1, tiptr,
1470Sstevel@tonic-gate "t_connect: invalid state event T_CONNECT1");
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * Update attributes which may have been negotiated during
1500Sstevel@tonic-gate * connection establishment for protocols where we suspect
1510Sstevel@tonic-gate * such negotiation is likely (e.g. OSI). We do not do it for
1520Sstevel@tonic-gate * all endpoints for performance reasons. Also, this code is
1530Sstevel@tonic-gate * deliberately done after user level state changes so even
1540Sstevel@tonic-gate * the (unlikely) failure case reflects a connected endpoint.
1550Sstevel@tonic-gate */
1560Sstevel@tonic-gate if (tiptr->ti_tsdusize != 0) {
1570Sstevel@tonic-gate if (_t_do_postconn_sync(fd, tiptr) < 0)
1580Sstevel@tonic-gate goto err_out;
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (didalloc)
1630Sstevel@tonic-gate free(ctlbuf.buf);
1640Sstevel@tonic-gate else
1650Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
1660Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1670Sstevel@tonic-gate return (0);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate err_out:
1700Sstevel@tonic-gate sv_errno = errno;
1710Sstevel@tonic-gate if (didalloc)
1720Sstevel@tonic-gate free(ctlbuf.buf);
1730Sstevel@tonic-gate else
1740Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
1750Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate errno = sv_errno;
1780Sstevel@tonic-gate return (-1);
1790Sstevel@tonic-gate }
180