xref: /titanic_52/usr/src/lib/libnsl/nsl/t_sndudata.c (revision 4bc0a2ef2b7ba50a7a717e7ddbf31472ad28e358)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.5 */
32 
33 /*
34  * t_sndudata.c and t_sndvudata.c are very similar and contain common code.
35  * Any changes to either of them should be reviewed to see whether they
36  * are applicable to the other file.
37  */
38 #include "mt.h"
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <stropts.h>
42 #include <sys/stream.h>
43 #define	_SUN_TPI_VERSION 2
44 #include <sys/tihdr.h>
45 #include <sys/timod.h>
46 #include <xti.h>
47 #include <syslog.h>
48 #include "tx.h"
49 
50 int
51 _tx_sndudata(int fd, const struct t_unitdata *unitdata, int api_semantics)
52 {
53 	struct T_unitdata_req *udreq;
54 	struct strbuf ctlbuf;
55 	int size;
56 	struct _ti_user *tiptr;
57 	int sv_errno;
58 	int didalloc;
59 
60 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
61 		return (-1);
62 	sig_mutex_lock(&tiptr->ti_lock);
63 
64 	if (tiptr->ti_servtype != T_CLTS) {
65 		t_errno = TNOTSUPPORT;
66 		sig_mutex_unlock(&tiptr->ti_lock);
67 		return (-1);
68 	}
69 
70 	if (_T_IS_XTI(api_semantics)) {
71 		/*
72 		 * User level state verification only done for XTI
73 		 * because doing for TLI may break existing applications
74 		 */
75 		if (tiptr->ti_state != T_IDLE) {
76 			t_errno = TOUTSTATE;
77 			sig_mutex_unlock(&tiptr->ti_lock);
78 			return (-1);
79 		}
80 	}
81 
82 	if (((int)unitdata->udata.len == 0) &&
83 	    !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
84 		t_errno = TBADDATA;
85 		sig_mutex_unlock(&tiptr->ti_lock);
86 		return (-1);
87 	}
88 
89 	if ((tiptr->ti_maxpsz > 0) &&
90 	    (unitdata->udata.len > (uint32_t)tiptr->ti_maxpsz)) {
91 		if (_T_IS_TLI(api_semantics)) {
92 			t_errno = TSYSERR;
93 			errno = EPROTO;
94 		} else
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 	/*
161 	 * Calls to send data (write or putmsg) can potentially
162 	 * block, for MT case, we drop the lock and enable signals here
163 	 * and acquire it back.
164 	 * At this point, we are sure SENDZERO is supported and the
165 	 * putmsg below may send a zero length message,
166 	 * (i.e with valid control part, but zero data part)
167 	 */
168 	sig_mutex_unlock(&tiptr->ti_lock);
169 	if (putmsg(fd, &ctlbuf, (struct strbuf *)&unitdata->udata, 0) < 0) {
170 		if (errno == EAGAIN)
171 			t_errno = TFLOW;
172 		else
173 			t_errno = TSYSERR;
174 		sv_errno = errno;
175 		sig_mutex_lock(&tiptr->ti_lock);
176 		errno = sv_errno;
177 		goto err_out;
178 	}
179 	sig_mutex_lock(&tiptr->ti_lock);
180 
181 	_T_TX_NEXTSTATE(T_SNDUDATA, tiptr,
182 			"t_sndudata: invalid state event T_SNDUDATA");
183 	if (didalloc)
184 		free(ctlbuf.buf);
185 	else
186 		tiptr->ti_ctlbuf = ctlbuf.buf;
187 	sig_mutex_unlock(&tiptr->ti_lock);
188 	return (0);
189 err_out:
190 	sv_errno = errno;
191 	if (didalloc)
192 		free(ctlbuf.buf);
193 	else
194 		tiptr->ti_ctlbuf = ctlbuf.buf;
195 	sig_mutex_unlock(&tiptr->ti_lock);
196 	errno = sv_errno;
197 	return (-1);
198 }
199