1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 1993-2003 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.7 */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "mt.h"
34*0Sstevel@tonic-gate #include <rpc/trace.h>
35*0Sstevel@tonic-gate #include <stropts.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <sys/timod.h>
38*0Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
39*0Sstevel@tonic-gate #include <sys/tihdr.h>
40*0Sstevel@tonic-gate #include <xti.h>
41*0Sstevel@tonic-gate #include <fcntl.h>
42*0Sstevel@tonic-gate #include <signal.h>
43*0Sstevel@tonic-gate #include <errno.h>
44*0Sstevel@tonic-gate #include <syslog.h>
45*0Sstevel@tonic-gate #include "tx.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * If a system call fails with EINTR after T_CONN_REQ is sent out,
49*0Sstevel@tonic-gate  * we change state for caller to continue with t_rcvconnect(). This
50*0Sstevel@tonic-gate  * semantics is not documented for TLI but is the direction taken with
51*0Sstevel@tonic-gate  * XTI so we adopt it. With this the call establishment is completed
52*0Sstevel@tonic-gate  * by calling t_rcvconnect() even for synchronous endpoints.
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate int
55*0Sstevel@tonic-gate _tx_connect(
56*0Sstevel@tonic-gate 	int fd,
57*0Sstevel@tonic-gate 	const struct t_call *sndcall,
58*0Sstevel@tonic-gate 	struct t_call *rcvcall,
59*0Sstevel@tonic-gate 	int api_semantics
60*0Sstevel@tonic-gate )
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	int fctlflg;
63*0Sstevel@tonic-gate 	struct _ti_user *tiptr;
64*0Sstevel@tonic-gate 	sigset_t mask;
65*0Sstevel@tonic-gate 	struct strbuf ctlbuf;
66*0Sstevel@tonic-gate 	int sv_errno;
67*0Sstevel@tonic-gate 	int didalloc;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	trace2(TR_t_connect, 0, fd);
70*0Sstevel@tonic-gate 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL) {
71*0Sstevel@tonic-gate 		sv_errno = errno;
72*0Sstevel@tonic-gate 		trace2(TR_t_connect, 1, fd);
73*0Sstevel@tonic-gate 		errno = sv_errno;
74*0Sstevel@tonic-gate 		return (-1);
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
78*0Sstevel@tonic-gate 	if (_T_IS_XTI(api_semantics)) {
79*0Sstevel@tonic-gate 		/*
80*0Sstevel@tonic-gate 		 * User level state verification only done for XTI
81*0Sstevel@tonic-gate 		 * because doing for TLI may break existing applications
82*0Sstevel@tonic-gate 		 */
83*0Sstevel@tonic-gate 		if (tiptr->ti_state != T_IDLE) {
84*0Sstevel@tonic-gate 			t_errno = TOUTSTATE;
85*0Sstevel@tonic-gate 			sig_mutex_unlock(&tiptr->ti_lock);
86*0Sstevel@tonic-gate 			trace2(TR_t_connect, 1, fd);
87*0Sstevel@tonic-gate 			return (-1);
88*0Sstevel@tonic-gate 		}
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	/*
92*0Sstevel@tonic-gate 	 * Acquire ctlbuf for use in sending/receiving control part
93*0Sstevel@tonic-gate 	 * of the message.
94*0Sstevel@tonic-gate 	 */
95*0Sstevel@tonic-gate 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
96*0Sstevel@tonic-gate 		sv_errno = errno;
97*0Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
98*0Sstevel@tonic-gate 		trace2(TR_t_connect, 1, fd);
99*0Sstevel@tonic-gate 		errno = sv_errno;
100*0Sstevel@tonic-gate 		return (-1);
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 	/*
103*0Sstevel@tonic-gate 	 * Block all signals until T_CONN_REQ sent and
104*0Sstevel@tonic-gate 	 * acked with T_OK_ACK/ERROR_ACK
105*0Sstevel@tonic-gate 	 */
106*0Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
107*0Sstevel@tonic-gate 	if (_t_snd_conn_req(tiptr, sndcall, &ctlbuf) < 0) {
108*0Sstevel@tonic-gate 		sv_errno = errno;
109*0Sstevel@tonic-gate 		(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
110*0Sstevel@tonic-gate 		errno = sv_errno;
111*0Sstevel@tonic-gate 		/*
112*0Sstevel@tonic-gate 		 * At the TPI level, the error returned in a T_ERROR_ACK
113*0Sstevel@tonic-gate 		 * received in response to a T_CONN_REQ for an attempt to
114*0Sstevel@tonic-gate 		 * establish a duplicate conection has changed to a
115*0Sstevel@tonic-gate 		 * new t_errno code introduced with XTI (ADDRBUSY).
116*0Sstevel@tonic-gate 		 * We need to adjust TLI error code to be same as before.
117*0Sstevel@tonic-gate 		 */
118*0Sstevel@tonic-gate 		if (_T_IS_TLI(api_semantics) && t_errno == TADDRBUSY)
119*0Sstevel@tonic-gate 			/* TLI only */
120*0Sstevel@tonic-gate 			t_errno = TBADADDR;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		goto err_out;
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if ((fctlflg = _fcntl(fd, F_GETFL, 0)) < 0) {
127*0Sstevel@tonic-gate 		t_errno = TSYSERR;
128*0Sstevel@tonic-gate 		goto err_out;
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if (fctlflg & (O_NDELAY | O_NONBLOCK)) {
132*0Sstevel@tonic-gate 		_T_TX_NEXTSTATE(T_CONNECT2, tiptr,
133*0Sstevel@tonic-gate 				"t_connect: invalid state event T_CONNECT2");
134*0Sstevel@tonic-gate 		t_errno = TNODATA;
135*0Sstevel@tonic-gate 		goto err_out;
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/*
139*0Sstevel@tonic-gate 	 * Note: The following call to _t_rcv_conn_con blocks awaiting
140*0Sstevel@tonic-gate 	 * T_CONN_CON from remote client. Therefore it drops the
141*0Sstevel@tonic-gate 	 * tiptr->lock during the call (and reacquires it)
142*0Sstevel@tonic-gate 	 */
143*0Sstevel@tonic-gate 	if (_t_rcv_conn_con(tiptr, rcvcall, &ctlbuf, api_semantics) < 0) {
144*0Sstevel@tonic-gate 		if ((t_errno == TSYSERR && errno == EINTR) ||
145*0Sstevel@tonic-gate 		    t_errno == TLOOK) {
146*0Sstevel@tonic-gate 			_T_TX_NEXTSTATE(T_CONNECT2, tiptr,
147*0Sstevel@tonic-gate 				"t_connect: invalid state event T_CONNECT2");
148*0Sstevel@tonic-gate 		} else if (t_errno == TBUFOVFLW) {
149*0Sstevel@tonic-gate 			_T_TX_NEXTSTATE(T_CONNECT1, tiptr,
150*0Sstevel@tonic-gate 				"t_connect: invalid state event T_CONNECT1");
151*0Sstevel@tonic-gate 		}
152*0Sstevel@tonic-gate 		goto err_out;
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 	_T_TX_NEXTSTATE(T_CONNECT1, tiptr,
155*0Sstevel@tonic-gate 				"t_connect: invalid state event T_CONNECT1");
156*0Sstevel@tonic-gate 	/*
157*0Sstevel@tonic-gate 	 * Update attributes which may have been negotiated during
158*0Sstevel@tonic-gate 	 * connection establishment for protocols where we suspect
159*0Sstevel@tonic-gate 	 * such negotiation is likely (e.g. OSI). We do not do it for
160*0Sstevel@tonic-gate 	 * all endpoints for performance reasons. Also, this code is
161*0Sstevel@tonic-gate 	 * deliberately done after user level state changes so even
162*0Sstevel@tonic-gate 	 * the (unlikely) failure case reflects a connected endpoint.
163*0Sstevel@tonic-gate 	 */
164*0Sstevel@tonic-gate 	if (tiptr->ti_tsdusize != 0) {
165*0Sstevel@tonic-gate 		if (_t_do_postconn_sync(fd, tiptr) < 0)
166*0Sstevel@tonic-gate 		    goto err_out;
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	if (didalloc)
171*0Sstevel@tonic-gate 		free(ctlbuf.buf);
172*0Sstevel@tonic-gate 	else
173*0Sstevel@tonic-gate 		tiptr->ti_ctlbuf = ctlbuf.buf;
174*0Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
175*0Sstevel@tonic-gate 	trace2(TR_t_connect, 1, fd);
176*0Sstevel@tonic-gate 	return (0);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate err_out:
179*0Sstevel@tonic-gate 	sv_errno = errno;
180*0Sstevel@tonic-gate 	if (didalloc)
181*0Sstevel@tonic-gate 		free(ctlbuf.buf);
182*0Sstevel@tonic-gate 	else
183*0Sstevel@tonic-gate 		tiptr->ti_ctlbuf = ctlbuf.buf;
184*0Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	trace2(TR_t_connect, 1, fd);
187*0Sstevel@tonic-gate 	errno = sv_errno;
188*0Sstevel@tonic-gate 	return (-1);
189*0Sstevel@tonic-gate }
190