1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1993-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * t_rcvrel.c and t_rcvreldata.c are very similar and contain common code.
32  * Any changes to either of them should be reviewed to see whether they
33  * are applicable to the other file.
34  */
35 #include "mt.h"
36 #include <rpc/trace.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <stropts.h>
40 #include <sys/stream.h>
41 #define	_SUN_TPI_VERSION 2
42 #include <sys/tihdr.h>
43 #include <sys/timod.h>
44 #include <xti.h>
45 #include <signal.h>
46 #include <syslog.h>
47 #include <assert.h>
48 #include "tx.h"
49 
50 /* ARGSUSED */
51 int
52 _tx_rcvreldata(int fd, struct t_discon *discon, int api_semantics)
53 {
54 	struct strbuf ctlbuf;
55 	struct strbuf databuf;
56 	int retval;
57 	union T_primitives *pptr;
58 	struct _ti_user *tiptr;
59 	int sv_errno;
60 	int didalloc, didralloc;
61 
62 	int flg = 0;
63 
64 
65 	trace2(TR_t_rcvreldata, 0, fd);
66 	assert(api_semantics == TX_XTI_XNS5_API);
67 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0) {
68 		sv_errno = errno;
69 		trace2(TR_t_rcvreldata, 1, fd);
70 		errno = sv_errno;
71 		return (-1);
72 	}
73 	sig_mutex_lock(&tiptr->ti_lock);
74 
75 	if (tiptr->ti_servtype != T_COTS_ORD) {
76 		t_errno = TNOTSUPPORT;
77 		sig_mutex_unlock(&tiptr->ti_lock);
78 		trace2(TR_t_rcvreldata, 1, fd);
79 		return (-1);
80 	}
81 
82 	if (! (tiptr->ti_state == T_DATAXFER ||
83 	    tiptr->ti_state == T_OUTREL)) {
84 		t_errno = TOUTSTATE;
85 		sig_mutex_unlock(&tiptr->ti_lock);
86 		trace2(TR_t_rcvreldata, 1, fd);
87 		return (-1);
88 	}
89 
90 	if ((retval = _t_look_locked(fd, tiptr, 0, api_semantics)) < 0) {
91 		sv_errno = errno;
92 		sig_mutex_unlock(&tiptr->ti_lock);
93 		trace2(TR_t_rcvreldata, 1, fd);
94 		errno = sv_errno;
95 		return (-1);
96 	}
97 
98 	if (retval == T_DISCONNECT) {
99 		/*
100 		 * This ensures preference to T_DISCON_IND which is
101 		 * the design model for TPI
102 		 */
103 		t_errno = TLOOK;
104 		sig_mutex_unlock(&tiptr->ti_lock);
105 		trace2(TR_t_rcvreldata, 1, fd);
106 		return (-1);
107 	}
108 
109 	/*
110 	 * Someday there could be transport providers that support T_ORDRELDATA
111 	 * Until then this function behaves the same as t_rcvrel()
112 	 * Note: Currently only mOSI ("minimal OSI") provider is specified
113 	 * to use T_ORDRELDATA so probability of needing it is minimal.
114 	 */
115 
116 	if ((tiptr->ti_lookcnt > 0) &&
117 	    (*((t_scalar_t *)tiptr->ti_lookbufs.tl_lookcbuf) == T_ORDREL_IND)) {
118 		/*
119 		 * Current look buffer event is T_ORDREL_IND.
120 		 * Remove it from look buffer event list.
121 		 */
122 		_t_free_looklist_head(tiptr);
123 		_T_TX_NEXTSTATE(T_RCVREL, tiptr,
124 			"t_rcvreldata: invalid state event T_RCVREL");
125 		sig_mutex_unlock(&tiptr->ti_lock);
126 		trace2(TR_t_rcvreldata, 1, fd);
127 		return (0);
128 	} else {
129 		if (retval != T_ORDREL) {
130 			t_errno = TNOREL;
131 			sig_mutex_unlock(&tiptr->ti_lock);
132 			trace2(TR_t_rcvreldata, 1, fd);
133 			return (-1);
134 		}
135 	}
136 
137 	/*
138 	 * get ordrel off read queue.
139 	 * use ctl and rcv buffers
140 	 *
141 	 * Acquire ctlbuf for use in sending/receiving control part
142 	 * of the message.
143 	 */
144 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
145 		sv_errno = errno;
146 		sig_mutex_unlock(&tiptr->ti_lock);
147 		trace2(TR_t_rcvreldata, 1, fd);
148 		errno = sv_errno;
149 		return (-1);
150 	}
151 
152 	/*
153 	 * Acquire databuf for use in sending/receiving data part
154 	 */
155 	if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0) {
156 		sv_errno = errno;
157 		if (didalloc)
158 			free(ctlbuf.buf);
159 		else
160 			tiptr->ti_ctlbuf = ctlbuf.buf;
161 		sig_mutex_unlock(&tiptr->ti_lock);
162 		trace2(TR_t_rcvreldata, 1, fd);
163 		errno = sv_errno;
164 		return (-1);
165 	}
166 
167 	/*
168 	 * Since we have verified above that an orderly release event
169 	 * is pending on this endpoint, we assume that this getmsg()
170 	 * cannot block forever.
171 	 */
172 	do {
173 		retval = getmsg(fd, &ctlbuf, &databuf, &flg);
174 	} while (retval < 0 && errno == EINTR);
175 
176 	if (retval < 0) {
177 		t_errno = TSYSERR;
178 		goto err_out;
179 	}
180 
181 	/*
182 	 * did I get entire message?
183 	 */
184 	if (retval > 0) {
185 		t_errno = TSYSERR;
186 		errno = EIO;
187 		goto err_out;
188 	}
189 	pptr = (union T_primitives *)ctlbuf.buf;
190 
191 	if (ctlbuf.len < (int)sizeof (struct T_ordrel_ind)) {
192 		t_errno = TSYSERR;
193 		errno = EPROTO;
194 		goto err_out;
195 	}
196 	if (pptr->type != T_ORDREL_IND) {
197 		if (pptr->type == T_DISCON_IND) {
198 			/*
199 			 * T_DISCON_IND gets priority following
200 			 * TPI design philosphy.
201 			 *
202 			 * Add it to the events in the "look buffer"
203 			 * list of events. This routine may defer signals.
204 			 */
205 			if (_t_register_lookevent(tiptr, databuf.buf,
206 						databuf.len, ctlbuf.buf,
207 						ctlbuf.len) < 0) {
208 				t_errno = TSYSERR;
209 				errno = ENOMEM;
210 				goto err_out;
211 			}
212 			t_errno = TLOOK;
213 			goto err_out;
214 		} else {
215 			t_errno = TSYSERR;
216 			errno = EPROTO;
217 			goto err_out;
218 		}
219 	}
220 
221 	_T_TX_NEXTSTATE(T_RCVREL, tiptr,
222 		"t_rcvreldata: invalid state event T_RCVREL");
223 
224 	if (didalloc)
225 		free(ctlbuf.buf);
226 	else
227 		tiptr->ti_ctlbuf = ctlbuf.buf;
228 	if (didralloc)
229 		free(databuf.buf);
230 	else
231 		tiptr->ti_rcvbuf = databuf.buf;
232 	sig_mutex_unlock(&tiptr->ti_lock);
233 	trace2(TR_t_rcvreldata, 1, fd);
234 	return (0);
235 
236 err_out:
237 	sv_errno = errno;
238 
239 	if (didalloc)
240 		free(ctlbuf.buf);
241 	else
242 		tiptr->ti_ctlbuf = ctlbuf.buf;
243 	if (didralloc)
244 		free(databuf.buf);
245 	else
246 		tiptr->ti_rcvbuf = databuf.buf;
247 	sig_mutex_unlock(&tiptr->ti_lock);
248 	trace2(TR_t_rcvreldata, 1, fd);
249 	errno = sv_errno;
250 	return (-1);
251 }
252