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 1998-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * t_sndudata.c and t_sndvudata.c are very similar and contain common code.
31  * Any changes to either of them should be reviewed to see whether they
32  * are applicable to the other file.
33  */
34 #include "mt.h"
35 #include <rpc/trace.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <stropts.h>
39 #include <sys/stream.h>
40 #define	_SUN_TPI_VERSION 2
41 #include <sys/tihdr.h>
42 #include <sys/timod.h>
43 #include <xti.h>
44 #include <syslog.h>
45 #include <assert.h>
46 #include "tx.h"
47 
48 int
49 _tx_sndvudata(int fd, const struct t_unitdata *unitdata, struct t_iovec *tiov,
50     unsigned int tiovcount, int api_semantics)
51 {
52 	struct T_unitdata_req *udreq;
53 	struct strbuf ctlbuf;
54 	struct strbuf databuf;
55 	int size;
56 	struct _ti_user *tiptr;
57 	int sv_errno;
58 	int didalloc;
59 	char *dataptr;
60 	unsigned int nbytes;
61 
62 	trace2(TR_t_sndvudata, 0, fd);
63 	assert(api_semantics == TX_XTI_XNS5_API);
64 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL) {
65 		sv_errno = errno;
66 		trace2(TR_t_sndvudata, 1, fd);
67 		errno = sv_errno;
68 		return (-1);
69 	}
70 	sig_mutex_lock(&tiptr->ti_lock);
71 
72 	if (tiptr->ti_servtype != T_CLTS) {
73 		t_errno = TNOTSUPPORT;
74 		sig_mutex_unlock(&tiptr->ti_lock);
75 		trace2(TR_t_sndvudata, 1, fd);
76 		return (-1);
77 	}
78 
79 	if (tiovcount == 0 || tiovcount > T_IOV_MAX) {
80 		t_errno = TBADDATA;
81 		sig_mutex_unlock(&tiptr->ti_lock);
82 		trace2(TR_t_sndvudata, 1, fd);
83 		return (-1);
84 	}
85 
86 	if (tiptr->ti_state != T_IDLE) {
87 		t_errno = TOUTSTATE;
88 		sig_mutex_unlock(&tiptr->ti_lock);
89 		trace2(TR_t_sndvudata, 1, fd);
90 		return (-1);
91 	}
92 
93 	nbytes = _t_bytecount_upto_intmax(tiov, tiovcount);
94 
95 	if ((nbytes == 0) &&
96 	    !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
97 		t_errno = TBADDATA;
98 		sig_mutex_unlock(&tiptr->ti_lock);
99 		trace2(TR_t_sndvudata, 1, fd);
100 		return (-1);
101 	}
102 
103 	if ((tiptr->ti_maxpsz > 0) && (nbytes > (uint32_t)tiptr->ti_maxpsz)) {
104 		t_errno = TBADDATA;
105 		sv_errno = errno;
106 		sig_mutex_unlock(&tiptr->ti_lock);
107 		trace2(TR_t_sndvudata, 1, fd);
108 		errno = sv_errno;
109 		return (-1);
110 	}
111 
112 	/*
113 	 * Acquire ctlbuf for use in sending/receiving control part
114 	 * of the message.
115 	 */
116 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
117 		sv_errno = errno;
118 		sig_mutex_unlock(&tiptr->ti_lock);
119 		trace2(TR_t_sndvudata, 1, fd);
120 		errno = sv_errno;
121 		return (-1);
122 	}
123 
124 	udreq = (struct T_unitdata_req *)ctlbuf.buf;
125 
126 	udreq->PRIM_type = T_UNITDATA_REQ;
127 	udreq->DEST_length = unitdata->addr.len;
128 	udreq->DEST_offset = 0;
129 	udreq->OPT_length = unitdata->opt.len;
130 	udreq->OPT_offset = 0;
131 	size = (int)sizeof (struct T_unitdata_req);
132 
133 	if (unitdata->addr.len) {
134 		if (_t_aligned_copy(&ctlbuf, unitdata->addr.len, size,
135 		    unitdata->addr.buf, &udreq->DEST_offset) < 0) {
136 			/*
137 			 * Aligned copy based will overflow buffer
138 			 * allocated based on maximum transport address
139 			 * size information
140 			 */
141 			t_errno = TSYSERR;
142 			errno = EPROTO;
143 			goto err_out;
144 		}
145 		size = udreq->DEST_offset + udreq->DEST_length;
146 	}
147 	if (unitdata->opt.len) {
148 		if (_t_aligned_copy(&ctlbuf, unitdata->opt.len, size,
149 		    unitdata->opt.buf, &udreq->OPT_offset) < 0) {
150 			/*
151 			 * Aligned copy based will overflow buffer
152 			 * allocated based on maximum transport option
153 			 * size information
154 			 */
155 			t_errno = TSYSERR;
156 			errno = EPROTO;
157 			goto err_out;
158 		}
159 		size = udreq->OPT_offset + udreq->OPT_length;
160 	}
161 
162 	if (size > (int)ctlbuf.maxlen) {
163 		t_errno = TSYSERR;
164 		errno = EIO;
165 		goto err_out;
166 	}
167 
168 	ctlbuf.len = size;
169 
170 	dataptr = NULL;
171 	if (nbytes != 0) {
172 		if ((dataptr = malloc((size_t)nbytes)) == NULL) {
173 			t_errno = TSYSERR;
174 			goto err_out;
175 		}
176 		_t_gather(dataptr, tiov, tiovcount);
177 	}
178 	databuf.buf = dataptr;
179 	databuf.len = nbytes;
180 	databuf.maxlen = nbytes;
181 	/*
182 	 * Calls to send data (write or putmsg) can potentially
183 	 * block, for MT case, we drop the lock and enable signals here
184 	 * and acquire it back
185 	 */
186 	sig_mutex_unlock(&tiptr->ti_lock);
187 	if (putmsg(fd, &ctlbuf, &databuf, 0) < 0) {
188 		if (errno == EAGAIN)
189 			t_errno = TFLOW;
190 		else
191 			t_errno = TSYSERR;
192 		sv_errno = errno;
193 		sig_mutex_lock(&tiptr->ti_lock);
194 		errno = sv_errno;
195 		goto err_out;
196 	}
197 	sig_mutex_lock(&tiptr->ti_lock);
198 
199 	_T_TX_NEXTSTATE(T_SNDUDATA, tiptr,
200 			"t_sndvudata: invalid state event T_SNDUDATA");
201 	if (didalloc)
202 		free(ctlbuf.buf);
203 	else
204 		tiptr->ti_ctlbuf = ctlbuf.buf;
205 	if (dataptr != NULL)
206 		free(dataptr);
207 	sig_mutex_unlock(&tiptr->ti_lock);
208 	trace2(TR_t_sndvudata, 0, fd);
209 	return (0);
210 err_out:
211 	sv_errno = errno;
212 	if (didalloc)
213 		free(ctlbuf.buf);
214 	else
215 		tiptr->ti_ctlbuf = ctlbuf.buf;
216 	if (dataptr != NULL)
217 		free(dataptr);
218 	sig_mutex_unlock(&tiptr->ti_lock);
219 	trace2(TR_t_sndvudata, 1, fd);
220 	errno = sv_errno;
221 	return (-1);
222 }
223