xref: /titanic_52/usr/src/lib/libnsl/nsl/t_sndvudata.c (revision 9512fe850e98fdd448c638ca63fdd92a8a510255)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * t_sndudata.c and t_sndvudata.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 <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 	assert(api_semantics == TX_XTI_XNS5_API);
63 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
64 		return (-1);
65 	sig_mutex_lock(&tiptr->ti_lock);
66 
67 	if (tiptr->ti_servtype != T_CLTS) {
68 		t_errno = TNOTSUPPORT;
69 		sig_mutex_unlock(&tiptr->ti_lock);
70 		return (-1);
71 	}
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_IDLE) {
80 		t_errno = TOUTSTATE;
81 		sig_mutex_unlock(&tiptr->ti_lock);
82 		return (-1);
83 	}
84 
85 	nbytes = _t_bytecount_upto_intmax(tiov, tiovcount);
86 
87 	if ((nbytes == 0) &&
88 	    !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
89 		t_errno = TBADDATA;
90 		sig_mutex_unlock(&tiptr->ti_lock);
91 		return (-1);
92 	}
93 
94 	if ((tiptr->ti_maxpsz > 0) && (nbytes > (uint32_t)tiptr->ti_maxpsz)) {
95 		t_errno = TBADDATA;
96 		sv_errno = errno;
97 		sig_mutex_unlock(&tiptr->ti_lock);
98 		errno = sv_errno;
99 		return (-1);
100 	}
101 
102 	/*
103 	 * Acquire ctlbuf for use in sending/receiving control part
104 	 * of the message.
105 	 */
106 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
107 		sv_errno = errno;
108 		sig_mutex_unlock(&tiptr->ti_lock);
109 		errno = sv_errno;
110 		return (-1);
111 	}
112 
113 	/* LINTED pointer cast */
114 	udreq = (struct T_unitdata_req *)ctlbuf.buf;
115 
116 	udreq->PRIM_type = T_UNITDATA_REQ;
117 	udreq->DEST_length = unitdata->addr.len;
118 	udreq->DEST_offset = 0;
119 	udreq->OPT_length = unitdata->opt.len;
120 	udreq->OPT_offset = 0;
121 	size = (int)sizeof (struct T_unitdata_req);
122 
123 	if (unitdata->addr.len) {
124 		if (_t_aligned_copy(&ctlbuf, unitdata->addr.len, size,
125 		    unitdata->addr.buf, &udreq->DEST_offset) < 0) {
126 			/*
127 			 * Aligned copy based will overflow buffer
128 			 * allocated based on maximum transport address
129 			 * size information
130 			 */
131 			t_errno = TSYSERR;
132 			errno = EPROTO;
133 			goto err_out;
134 		}
135 		size = udreq->DEST_offset + udreq->DEST_length;
136 	}
137 	if (unitdata->opt.len) {
138 		if (_t_aligned_copy(&ctlbuf, unitdata->opt.len, size,
139 		    unitdata->opt.buf, &udreq->OPT_offset) < 0) {
140 			/*
141 			 * Aligned copy based will overflow buffer
142 			 * allocated based on maximum transport option
143 			 * size information
144 			 */
145 			t_errno = TSYSERR;
146 			errno = EPROTO;
147 			goto err_out;
148 		}
149 		size = udreq->OPT_offset + udreq->OPT_length;
150 	}
151 
152 	if (size > (int)ctlbuf.maxlen) {
153 		t_errno = TSYSERR;
154 		errno = EIO;
155 		goto err_out;
156 	}
157 
158 	ctlbuf.len = size;
159 
160 	dataptr = NULL;
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 	if (dataptr != NULL)
196 		free(dataptr);
197 	sig_mutex_unlock(&tiptr->ti_lock);
198 	return (0);
199 err_out:
200 	sv_errno = errno;
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 	errno = sv_errno;
209 	return (-1);
210 }
211