xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_sndudata.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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 /*
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 "tx.h"
47 
48 int
49 _tx_sndudata(int fd, const struct t_unitdata *unitdata, int api_semantics)
50 {
51 	struct T_unitdata_req *udreq;
52 	struct strbuf ctlbuf;
53 	int size;
54 	struct _ti_user *tiptr;
55 	int sv_errno;
56 	int didalloc;
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 	if (_T_IS_XTI(api_semantics)) {
69 		/*
70 		 * User level state verification only done for XTI
71 		 * because doing for TLI may break existing applications
72 		 */
73 		if (tiptr->ti_state != T_IDLE) {
74 			t_errno = TOUTSTATE;
75 			sig_mutex_unlock(&tiptr->ti_lock);
76 			return (-1);
77 		}
78 	}
79 
80 	if (((int)unitdata->udata.len == 0) &&
81 	    !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
82 		t_errno = TBADDATA;
83 		sig_mutex_unlock(&tiptr->ti_lock);
84 		return (-1);
85 	}
86 
87 	if ((tiptr->ti_maxpsz > 0) &&
88 	    (unitdata->udata.len > (uint32_t)tiptr->ti_maxpsz)) {
89 		if (_T_IS_TLI(api_semantics)) {
90 			t_errno = TSYSERR;
91 			errno = EPROTO;
92 		} else
93 			t_errno = TBADDATA;
94 		sv_errno = errno;
95 		sig_mutex_unlock(&tiptr->ti_lock);
96 		errno = sv_errno;
97 		return (-1);
98 	}
99 
100 	/*
101 	 * Acquire ctlbuf for use in sending/receiving control part
102 	 * of the message.
103 	 */
104 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
105 		sv_errno = errno;
106 		sig_mutex_unlock(&tiptr->ti_lock);
107 		errno = sv_errno;
108 		return (-1);
109 	}
110 
111 	/* LINTED pointer cast */
112 	udreq = (struct T_unitdata_req *)ctlbuf.buf;
113 
114 	udreq->PRIM_type = T_UNITDATA_REQ;
115 	udreq->DEST_length = unitdata->addr.len;
116 	udreq->DEST_offset = 0;
117 	udreq->OPT_length = unitdata->opt.len;
118 	udreq->OPT_offset = 0;
119 	size = (int)sizeof (struct T_unitdata_req);
120 
121 	if (unitdata->addr.len) {
122 		if (_t_aligned_copy(&ctlbuf, unitdata->addr.len, size,
123 		    unitdata->addr.buf, &udreq->DEST_offset) < 0) {
124 			/*
125 			 * Aligned copy based will overflow buffer
126 			 * allocated based on maximum transport address
127 			 * size information
128 			 */
129 			t_errno = TSYSERR;
130 			errno = EPROTO;
131 			goto err_out;
132 		}
133 		size = udreq->DEST_offset + udreq->DEST_length;
134 	}
135 	if (unitdata->opt.len) {
136 		if (_t_aligned_copy(&ctlbuf, unitdata->opt.len, size,
137 		    unitdata->opt.buf, &udreq->OPT_offset) < 0) {
138 			/*
139 			 * Aligned copy based will overflow buffer
140 			 * allocated based on maximum transport option
141 			 * size information
142 			 */
143 			t_errno = TSYSERR;
144 			errno = EPROTO;
145 			goto err_out;
146 		}
147 		size = udreq->OPT_offset + udreq->OPT_length;
148 	}
149 
150 	if (size > (int)ctlbuf.maxlen) {
151 		t_errno = TSYSERR;
152 		errno = EIO;
153 		goto err_out;
154 	}
155 
156 	ctlbuf.len = size;
157 
158 	/*
159 	 * Calls to send data (write or putmsg) can potentially
160 	 * block, for MT case, we drop the lock and enable signals here
161 	 * and acquire it back.
162 	 * At this point, we are sure SENDZERO is supported and the
163 	 * putmsg below may send a zero length message,
164 	 * (i.e with valid control part, but zero data part)
165 	 */
166 	sig_mutex_unlock(&tiptr->ti_lock);
167 	if (putmsg(fd, &ctlbuf, (struct strbuf *)&unitdata->udata, 0) < 0) {
168 		if (errno == EAGAIN)
169 			t_errno = TFLOW;
170 		else
171 			t_errno = TSYSERR;
172 		sv_errno = errno;
173 		sig_mutex_lock(&tiptr->ti_lock);
174 		errno = sv_errno;
175 		goto err_out;
176 	}
177 	sig_mutex_lock(&tiptr->ti_lock);
178 
179 	_T_TX_NEXTSTATE(T_SNDUDATA, tiptr,
180 			"t_sndudata: invalid state event T_SNDUDATA");
181 	if (didalloc)
182 		free(ctlbuf.buf);
183 	else
184 		tiptr->ti_ctlbuf = ctlbuf.buf;
185 	sig_mutex_unlock(&tiptr->ti_lock);
186 	return (0);
187 err_out:
188 	sv_errno = errno;
189 	if (didalloc)
190 		free(ctlbuf.buf);
191 	else
192 		tiptr->ti_ctlbuf = ctlbuf.buf;
193 	sig_mutex_unlock(&tiptr->ti_lock);
194 	errno = sv_errno;
195 	return (-1);
196 }
197