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