xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_rcvv.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  * t_rcv.c and t_rcvv.c are very similar and contain common code.
30  * Any changes to either of them should be reviewed to see whether they
31  * are applicable to the other file.
32  */
33 #include "mt.h"
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <stropts.h>
38 #include <sys/stream.h>
39 #define	_SUN_TPI_VERSION 2
40 #include <sys/tihdr.h>
41 #include <sys/timod.h>
42 #include <xti.h>
43 #include <syslog.h>
44 #include <assert.h>
45 #include "tx.h"
46 
47 int
48 _tx_rcvv(int fd, struct t_iovec *tiov, unsigned int tiovcount,  int *flags,
49     int api_semantics)
50 {
51 	struct strbuf ctlbuf, databuf;
52 	int retval, flg = 0;
53 	int msglen;
54 	union T_primitives *pptr;
55 	struct _ti_user *tiptr;
56 	int sv_errno;
57 	int didalloc;
58 	unsigned int nbytes;
59 	char *dataptr;
60 
61 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
62 		return (-1);
63 	sig_mutex_lock(&tiptr->ti_lock);
64 
65 	if (tiptr->ti_servtype == T_CLTS) {
66 		t_errno = TNOTSUPPORT;
67 		sig_mutex_unlock(&tiptr->ti_lock);
68 		return (-1);
69 	}
70 
71 	assert(api_semantics == TX_XTI_XNS5_API);
72 
73 	if (tiovcount == 0 || tiovcount > T_IOV_MAX) {
74 		t_errno = TBADDATA;
75 		sig_mutex_unlock(&tiptr->ti_lock);
76 		return (-1);
77 	}
78 
79 	if (!(tiptr->ti_state == T_DATAXFER ||
80 		tiptr->ti_state == T_OUTREL)) {
81 		t_errno = TOUTSTATE;
82 		sig_mutex_unlock(&tiptr->ti_lock);
83 		return (-1);
84 	}
85 
86 	/*
87 	 * Check in lookbuf for stuff
88 	 */
89 	if (tiptr->ti_lookcnt > 0) {
90 		/*
91 		 * Implied preference rules give priority to
92 		 * T_DISCON_IND over T_ORDREL_IND. Also certain errors like
93 		 * data received after T_ORDREL_IND or a duplicate T_ORDREL_IND
94 		 * after a T_ORDRELING have priority over TLOOK.
95 		 * This manifests in following code behavior.
96 		 *
97 		 * (1)  If something in lookbuf then check
98 		 *	the stream head also. This may result
99 		 *	in retuning a TLOOK error but only if there are
100 		 *	  - message at stream head but look buffer
101 		 *	    has a T_DISCON_IND event.
102 		 *	  - no messages are on the stream head
103 		 *
104 		 * (2)  If there are messages on the stream head and
105 		 *	all of them are T_ORDREL_IND(i.e. no message in
106 		 *	look buffer is T_DISCON_IND), there
107 		 *	could be data on stream head to be picked up and
108 		 *	we work on the stream head and not return TLOOK.
109 		 *	We remove the event on the stream head and queue it.
110 		 *
111 		 */
112 		do {
113 			retval = ioctl(fd, I_NREAD, &msglen);
114 		} while (retval < 0 && errno == EINTR);
115 
116 		if (retval < 0) {
117 			sv_errno = errno;
118 			t_errno = TSYSERR;
119 			sig_mutex_unlock(&tiptr->ti_lock);
120 			errno = sv_errno;
121 			return (-1);
122 		}
123 
124 		if (retval > 0) {
125 			/*
126 			 * If any T_DISCON_IND event in look buffer
127 			 * list then return TLOOK. Else continue
128 			 * processing as what could be on the stream
129 			 * head might be a possible T_DISCON_IND (which
130 			 * would have priority over the T_ORDREL_INDs
131 			 * on the look buffer.)
132 			 */
133 			struct _ti_lookbufs *tlbs;
134 
135 			tlbs = &tiptr->ti_lookbufs;
136 			do {
137 				/* LINTED pointer cast */
138 				if (*((t_scalar_t *)tlbs->tl_lookcbuf)
139 				    == T_DISCON_IND) {
140 					t_errno = TLOOK;
141 					sig_mutex_unlock(&tiptr->ti_lock);
142 					return (-1);
143 				}
144 			} while ((tlbs = tlbs->tl_next) != NULL);
145 
146 		} else {	/* retval == 0 */
147 			/*
148 			 * Nothing on stream head so whatever in
149 			 * look buffer has nothing that might override
150 			 * it.
151 			 */
152 			t_errno = TLOOK;
153 			sig_mutex_unlock(&tiptr->ti_lock);
154 			return (-1);
155 		}
156 	}
157 
158 	/*
159 	 * Acquire ctlbuf for use in sending/receiving control part
160 	 * of the message.
161 	 */
162 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
163 		sv_errno = errno;
164 		sig_mutex_unlock(&tiptr->ti_lock);
165 		errno = sv_errno;
166 		return (-1);
167 	}
168 
169 	nbytes = _t_bytecount_upto_intmax(tiov, tiovcount);
170 	dataptr = NULL;
171 	if (nbytes != 0 && ((dataptr = malloc(nbytes)) == NULL)) {
172 		sv_errno = errno;
173 		t_errno = TSYSERR;
174 		if (didalloc)
175 			free(ctlbuf.buf);
176 		else
177 			tiptr->ti_ctlbuf = ctlbuf.buf;
178 		sig_mutex_unlock(&tiptr->ti_lock);
179 		errno = sv_errno;
180 		return (-1);
181 	}
182 
183 	databuf.maxlen = (int)nbytes;
184 	databuf.len = 0;
185 	databuf.buf = dataptr;
186 
187 	*flags = 0;
188 
189 	/*
190 	 * This is a call that may block indefinitely so we drop the
191 	 * lock and allow signals in MT case here and reacquire it.
192 	 * Error case should roll back state changes done above
193 	 * (happens to be no state change here)
194 	 */
195 	sig_mutex_unlock(&tiptr->ti_lock);
196 	if ((retval = getmsg(fd, &ctlbuf, &databuf, &flg)) < 0) {
197 		if (errno == EAGAIN)
198 			t_errno = TNODATA;
199 		else
200 			t_errno = TSYSERR;
201 		sv_errno = errno;
202 		sig_mutex_lock(&tiptr->ti_lock);
203 		errno = sv_errno;
204 		goto err_out;
205 	}
206 	sig_mutex_lock(&tiptr->ti_lock);
207 
208 	assert((retval & MORECTL) == 0); /* MORECTL should not be on */
209 
210 	if (databuf.len == -1) databuf.len = 0;
211 
212 	if (ctlbuf.len > 0) {
213 		if (ctlbuf.len < (int)sizeof (t_scalar_t)) {
214 			t_errno = TSYSERR;
215 			errno = EPROTO;
216 			goto err_out;
217 		}
218 
219 		/* LINTED pointer cast */
220 		pptr = (union T_primitives *)ctlbuf.buf;
221 
222 		switch (pptr->type) {
223 
224 		case T_EXDATA_IND:
225 			*flags |= T_EXPEDITED;
226 			if (retval > 0)
227 				tiptr->ti_flags |= EXPEDITED;
228 			/* FALLTHROUGH */
229 		case T_DATA_IND:
230 			/*
231 			 * Uses the fact T_DATA_IND and T_EXDATA_IND
232 			 * are same in size
233 			 */
234 			if ((ctlbuf.len < (int)sizeof (struct T_data_ind)) ||
235 			    (tiptr->ti_lookcnt > 0)) {
236 				/*
237 				 * ti_lookcnt > 0 implies data
238 				 * received after T_DISCON_IND or
239 				 * T_ORDREL_IND hence error
240 				 */
241 				t_errno = TSYSERR;
242 				errno = EPROTO;
243 				goto err_out;
244 			}
245 
246 			if ((pptr->data_ind.MORE_flag) || retval)
247 				*flags |= T_MORE;
248 			if ((pptr->data_ind.MORE_flag) && retval)
249 				tiptr->ti_flags |= MORE;
250 			/*
251 			 * No real state change on T_RCV event (noop)
252 			 *
253 			 * We invoke the macro only for error logging
254 			 * part of its capabilities when in a bad state.
255 			 */
256 			_T_TX_NEXTSTATE(T_RCV, tiptr,
257 					"t_rcvv: invalid state event T_RCV");
258 			if (didalloc)
259 				free(ctlbuf.buf);
260 			else
261 				tiptr->ti_ctlbuf = ctlbuf.buf;
262 			_t_scatter(&databuf, tiov, tiovcount);
263 			if (dataptr != NULL)
264 				free(dataptr);
265 			sig_mutex_unlock(&tiptr->ti_lock);
266 			return (databuf.len);
267 
268 		case T_ORDREL_IND:
269 			if (tiptr->ti_lookcnt > 0) {
270 				/*
271 				 * ti_lookcnt > 0 implies T_ORDREL_IND
272 				 * received after T_DISCON_IND or
273 				 * another T_ORDREL_IND hence error.
274 				 */
275 				t_errno = TSYSERR;
276 				errno = EPROTO;
277 				goto err_out;
278 			}
279 			/* FALLTHROUGH */
280 		case T_DISCON_IND:
281 			/*
282 			 * Post event (T_ORDREL_IND/T_DISCON_IND) to
283 			 * the lookbuffer list.
284 			 */
285 
286 			if (_t_register_lookevent(tiptr, databuf.buf,
287 					databuf.len,
288 					ctlbuf.buf, ctlbuf.len) < 0) {
289 				t_errno = TSYSERR;
290 				errno = ENOMEM;
291 				goto err_out;
292 			}
293 			/*
294 			 * We know that T_DISCON_IND is stored in
295 			 * last look buffer. If there is more data
296 			 * that follows, we try to append it to
297 			 * the same look buffer
298 			 */
299 			if (retval & MOREDATA) {
300 				ctlbuf.maxlen = 0; /* XXX why ? */
301 				ctlbuf.len = 0;
302 
303 				/*
304 				 * XXX Will break (-ve maxlen) for
305 				 * transport provider with unbounded
306 				 * T_DISCON_IND data part (-1).
307 				 */
308 				databuf.maxlen =
309 					tiptr->ti_rcvsize - databuf.len;
310 
311 				databuf.len = 0;
312 				databuf.buf =
313 					tiptr->ti_lookbufs.tl_lookdbuf +
314 					tiptr->ti_lookbufs.tl_lookdlen;
315 				*flags = 0;
316 
317 				/*
318 				 * Since MOREDATA was set, we assume
319 				 * that this getmsg will not block
320 				 * indefinitely
321 				 */
322 				do {
323 					retval = getmsg(fd, &ctlbuf,
324 							&databuf, &flg);
325 				} while (retval < 0 && errno == EINTR);
326 
327 				if (retval < 0) {
328 					t_errno = TSYSERR;
329 					goto err_out;
330 				}
331 				if (databuf.len == -1) databuf.len = 0;
332 				if (retval > 0) {
333 					/* MORECTL should not be on */
334 					assert((retval & MORECTL) == 0);
335 					/*
336 					 * XXX - Why ?
337 					 * No support for unbounded data
338 					 * on T_DISCON_IND ?
339 					 */
340 					t_errno = TSYSERR;
341 					errno = EPROTO;
342 					goto err_out;
343 				}
344 				tiptr->ti_lookbufs.tl_lookdlen +=
345 					databuf.len;
346 			}
347 
348 			t_errno = TLOOK;
349 			goto err_out;
350 
351 		default:
352 			break;
353 		}
354 
355 		t_errno = TSYSERR;
356 		errno = EPROTO;
357 		goto err_out;
358 
359 	} else {		/* else for "if (ctlbuf.len > 0)" */
360 		if (!retval && (tiptr->ti_flags & MORE)) {
361 			*flags |= T_MORE;
362 			tiptr->ti_flags &= ~MORE;
363 		}
364 		if (retval & MOREDATA)
365 			*flags |= T_MORE;
366 
367 		/*
368 		 * If inside an ETSDU, set expedited flag and turn
369 		 * of internal version when reach end of "ETIDU".
370 		 */
371 		if (tiptr->ti_flags & EXPEDITED) {
372 			*flags |= T_EXPEDITED;
373 			if (!retval)
374 				tiptr->ti_flags &= ~EXPEDITED;
375 		}
376 
377 		/*
378 		 * No real state change on T_RCV events (It is a NOOP)
379 		 *
380 		 * We invoke the macro only for error logging
381 		 * part of its capabilities when in a bad state.
382 		 */
383 		_T_TX_NEXTSTATE(T_RCV, tiptr,
384 			"t_rcvv: state invalid T_RCV event");
385 		if (didalloc)
386 			free(ctlbuf.buf);
387 		else
388 			tiptr->ti_ctlbuf = ctlbuf.buf;
389 		_t_scatter(&databuf, tiov, tiovcount);
390 		if (dataptr != NULL)
391 			free(dataptr);
392 		sig_mutex_unlock(&tiptr->ti_lock);
393 		return (databuf.len);
394 	}
395 	/* NOTREACHED */
396 
397 err_out:
398 	sv_errno = errno;
399 	if (didalloc)
400 		free(ctlbuf.buf);
401 	else
402 		tiptr->ti_ctlbuf = ctlbuf.buf;
403 	if (dataptr != NULL)
404 		free(dataptr);
405 	sig_mutex_unlock(&tiptr->ti_lock);
406 
407 	errno = sv_errno;
408 	return (-1);
409 }
410