xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_rcvuderr.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
24 /*	  All Rights Reserved  	*/
25 
26 /*
27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #include "mt.h"
32 #include <errno.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stropts.h>
36 #include <sys/stream.h>
37 #define	_SUN_TPI_VERSION 2
38 #include <sys/tihdr.h>
39 #include <sys/timod.h>
40 #include <xti.h>
41 #include <syslog.h>
42 #include <assert.h>
43 #include "tx.h"
44 
45 int
46 _tx_rcvuderr(int fd, struct t_uderr *uderr, int api_semantics)
47 {
48 	struct strbuf ctlbuf, databuf;
49 	int flg;
50 	int retval;
51 	union T_primitives *pptr;
52 	struct _ti_user *tiptr;
53 	int sv_errno;
54 	int didalloc;
55 	int use_lookbufs = 0;
56 
57 
58 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
59 		return (-1);
60 	sig_mutex_lock(&tiptr->ti_lock);
61 
62 	if (tiptr->ti_servtype != T_CLTS) {
63 		t_errno = TNOTSUPPORT;
64 		sig_mutex_unlock(&tiptr->ti_lock);
65 		return (-1);
66 	}
67 	/*
68 	 * is there a unitdata error indication in look buffer
69 	 */
70 	if (tiptr->ti_lookcnt > 0) {
71 		ctlbuf.len = tiptr->ti_lookbufs.tl_lookclen;
72 		ctlbuf.buf = tiptr->ti_lookbufs.tl_lookcbuf;
73 		/* Note: cltbuf.maxlen not used in this case */
74 
75 		/* LINTED pointer cast */
76 		assert(((union T_primitives *)ctlbuf.buf)->type
77 			== T_UDERROR_IND);
78 
79 		databuf.maxlen = 0;
80 		databuf.len = 0;
81 		databuf.buf = NULL;
82 
83 		use_lookbufs = 1;
84 
85 	} else {
86 		if ((retval = _t_look_locked(fd, tiptr, 0,
87 		    api_semantics)) < 0) {
88 			sv_errno = errno;
89 			sig_mutex_unlock(&tiptr->ti_lock);
90 			errno = sv_errno;
91 			return (-1);
92 		}
93 		if (retval != T_UDERR) {
94 			t_errno = TNOUDERR;
95 			sig_mutex_unlock(&tiptr->ti_lock);
96 			return (-1);
97 		}
98 
99 		/*
100 		 * Acquire ctlbuf for use in sending/receiving control part
101 		 * of the message.
102 		 */
103 		if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
104 			sv_errno = errno;
105 			sig_mutex_unlock(&tiptr->ti_lock);
106 			errno = sv_errno;
107 			return (-1);
108 		}
109 
110 		databuf.maxlen = 0;
111 		databuf.len = 0;
112 		databuf.buf = NULL;
113 
114 		flg = 0;
115 
116 		/*
117 		 * Since we already verified that a unitdata error
118 		 * indication is pending, we assume that this getmsg()
119 		 * will not block indefinitely.
120 		 */
121 		if ((retval = getmsg(fd, &ctlbuf, &databuf, &flg)) < 0) {
122 
123 			t_errno = TSYSERR;
124 			goto err_out;
125 		}
126 		/*
127 		 * did I get entire message?
128 		 */
129 		if (retval > 0) {
130 			t_errno = TSYSERR;
131 			errno = EIO;
132 			goto err_out;
133 		}
134 
135 	}
136 
137 	/* LINTED pointer cast */
138 	pptr = (union T_primitives *)ctlbuf.buf;
139 
140 	if ((ctlbuf.len < (int)sizeof (struct T_uderror_ind)) ||
141 	    (pptr->type != T_UDERROR_IND)) {
142 		t_errno = TSYSERR;
143 		errno = EPROTO;
144 		goto err_out;
145 	}
146 
147 	if (uderr) {
148 		if (_T_IS_TLI(api_semantics) || uderr->addr.maxlen > 0) {
149 			if (TLEN_GT_NLEN(pptr->uderror_ind.DEST_length,
150 			    uderr->addr.maxlen)) {
151 				t_errno = TBUFOVFLW;
152 				goto err_out;
153 			}
154 			(void) memcpy(uderr->addr.buf, ctlbuf.buf +
155 			    pptr->uderror_ind.DEST_offset,
156 			    (size_t)pptr->uderror_ind.DEST_length);
157 			uderr->addr.len =
158 			    (unsigned int)pptr->uderror_ind.DEST_length;
159 		}
160 		if (_T_IS_TLI(api_semantics) || uderr->addr.maxlen > 0) {
161 			if (TLEN_GT_NLEN(pptr->uderror_ind.OPT_length,
162 			    uderr->opt.maxlen)) {
163 				t_errno = TBUFOVFLW;
164 				goto err_out;
165 			}
166 			(void) memcpy(uderr->opt.buf, ctlbuf.buf +
167 			    pptr->uderror_ind.OPT_offset,
168 			    (size_t)pptr->uderror_ind.OPT_length);
169 			uderr->opt.len =
170 			    (unsigned int)pptr->uderror_ind.OPT_length;
171 		}
172 		uderr->error = pptr->uderror_ind.ERROR_type;
173 	}
174 
175 	_T_TX_NEXTSTATE(T_RCVUDERR, tiptr,
176 			"t_rcvuderr: invalid state event T_RCVUDERR");
177 	if (use_lookbufs)
178 		_t_free_looklist_head(tiptr);
179 	else {
180 		if (didalloc)
181 			free(ctlbuf.buf);
182 		else
183 			tiptr->ti_ctlbuf = ctlbuf.buf;
184 	}
185 	sig_mutex_unlock(&tiptr->ti_lock);
186 	return (0);
187 
188 err_out:
189 	sv_errno = errno;
190 
191 	if (use_lookbufs)
192 		_t_free_looklist_head(tiptr);
193 	else {
194 		if (didalloc)
195 			free(ctlbuf.buf);
196 		else
197 			tiptr->ti_ctlbuf = ctlbuf.buf;
198 	}
199 	sig_mutex_unlock(&tiptr->ti_lock);
200 	errno = sv_errno;
201 	return (-1);
202 }
203