xref: /titanic_41/usr/src/lib/libnsl/nsl/t_accept.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
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 2006 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.2.1 */
32 
33 #include "mt.h"
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <stropts.h>
38 #include <sys/stream.h>
39 #define	_SUN_TPI_VERSION 2
40 #include <sys/tihdr.h>
41 #include <sys/timod.h>
42 #include <xti.h>
43 #include <signal.h>
44 #include <syslog.h>
45 #include <assert.h>
46 #include "tx.h"
47 
48 int
49 _tx_accept(
50 	int fd,
51 	int resfd,
52 	const struct t_call *call,
53 	int api_semantics
54 )
55 {
56 	struct T_conn_res *cres;
57 	struct strfdinsert strfdinsert;
58 	int size, retval, sv_errno;
59 	struct _ti_user *tiptr;
60 	struct _ti_user *restiptr;
61 	sigset_t mask;
62 	struct strbuf ctlbuf;
63 	int didalloc;
64 	t_scalar_t conn_res_prim;
65 
66 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
67 		return (-1);
68 	if ((restiptr = _t_checkfd(resfd, 0, api_semantics)) == NULL)
69 		return (-1);
70 
71 	/*
72 	 * We need to block signals to perform the I_FDINSERT operation
73 	 * (sending T_CONN_RES downstream) which is non-idempotent.
74 	 * Note that sig_mutex_lock() only defers signals, it does not
75 	 * block them, so interruptible syscalls could still get EINTR.
76 	 */
77 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
78 	sig_mutex_lock(&tiptr->ti_lock);
79 
80 	if (tiptr->ti_servtype == T_CLTS) {
81 		t_errno = TNOTSUPPORT;
82 		sig_mutex_unlock(&tiptr->ti_lock);
83 		(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
84 		return (-1);
85 	}
86 
87 	if (_T_IS_XTI(api_semantics)) {
88 		/*
89 		 * User level state verification only done for XTI
90 		 * because doing for TLI may break existing applications
91 		 *
92 		 * For fd == resfd, state should be T_INCON
93 		 * For fd != resfd,
94 		 *	    fd state should be T_INCON
95 		 *	    resfd state should be T_IDLE (bound endpoint) or
96 		 *	    it can be T_UNBND. The T_UNBND case is not (yet?)
97 		 *	    allowed in the published XTI spec but fixed by the
98 		 *	    corrigenda.
99 		 */
100 		if ((fd == resfd && tiptr->ti_state != T_INCON) ||
101 		    (fd != resfd &&
102 			((tiptr->ti_state != T_INCON) ||
103 		    !(restiptr->ti_state == T_IDLE ||
104 			restiptr->ti_state == T_UNBND)))) {
105 			t_errno = TOUTSTATE;
106 			sig_mutex_unlock(&tiptr->ti_lock);
107 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
108 			return (-1);
109 		}
110 
111 		/*
112 		 * XTI says:
113 		 * If fd != resfd, and a resfd bound to a protocol address is
114 		 * passed, then it better not have a qlen > 0.
115 		 * That is, an endpoint bound as if it will be a listener
116 		 * cannot be used as an acceptor.
117 		 */
118 		if (fd != resfd && restiptr->ti_state == T_IDLE &&
119 		    restiptr->ti_qlen > 0) {
120 			t_errno = TRESQLEN;
121 			sig_mutex_unlock(&tiptr->ti_lock);
122 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
123 			return (-1);
124 		}
125 
126 		if (fd == resfd && tiptr->ti_ocnt > 1) {
127 			t_errno = TINDOUT;
128 			sig_mutex_unlock(&tiptr->ti_lock);
129 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
130 			return (-1);
131 		}
132 
133 		/*
134 		 * Note: TRESADDR error is specified by XTI. It happens
135 		 * when resfd is bound and fd and resfd are not BOUND to
136 		 * the same protocol address. TCP obviously does allow
137 		 * two endpoints to bind to the same address. Why is the
138 		 * need for this error considering there is an address switch
139 		 * that can be done for the endpoint at accept time ? Go
140 		 * figure and ask the XTI folks.
141 		 * We interpret this to be a transport specific error condition
142 		 * to be be coveyed by the transport provider in T_ERROR_ACK
143 		 * to T_CONN_RES on transports that allow two endpoints to
144 		 * be bound to the same address and have trouble with the
145 		 * idea of accepting connections on a resfd that has a qlen > 0
146 		 */
147 	}
148 
149 	if (fd != resfd) {
150 		if ((retval = ioctl(resfd, I_NREAD, &size)) < 0) {
151 			sv_errno = errno;
152 
153 			t_errno = TSYSERR;
154 			sig_mutex_unlock(&tiptr->ti_lock);
155 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
156 			errno = sv_errno;
157 			return (-1);
158 		}
159 		if (retval > 0) {
160 			t_errno = TBADF;
161 			sig_mutex_unlock(&tiptr->ti_lock);
162 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
163 			return (-1);
164 		}
165 	}
166 
167 	/*
168 	 * Acquire ctlbuf for use in sending/receiving control part
169 	 * of the message.
170 	 */
171 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
172 		sv_errno = errno;
173 		sig_mutex_unlock(&tiptr->ti_lock);
174 		(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
175 		errno = sv_errno;
176 		return (-1);
177 	}
178 
179 	/*
180 	 * In Unix98 t_accept() need not return [TLOOK] if connect/disconnect
181 	 * indications are present. TLI and Unix95 need to return error.
182 	 */
183 	if (_T_API_VER_LT(api_semantics, TX_XTI_XNS5_API)) {
184 		if (_t_is_event(fd, tiptr) < 0)
185 			goto err_out;
186 	}
187 
188 	/* LINTED pointer cast */
189 	cres = (struct T_conn_res *)ctlbuf.buf;
190 	cres->OPT_length = call->opt.len;
191 	cres->OPT_offset = 0;
192 	cres->SEQ_number = call->sequence;
193 	if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) {
194 		cres->ACCEPTOR_id = restiptr->acceptor_id;
195 		cres->PRIM_type = conn_res_prim = T_CONN_RES;
196 	} else {
197 		/* I_FDINSERT should use O_T_CONN_RES. */
198 		cres->ACCEPTOR_id = 0;
199 		cres->PRIM_type = conn_res_prim = O_T_CONN_RES;
200 	}
201 
202 	size = (int)sizeof (struct T_conn_res);
203 
204 	if (call->opt.len) {
205 		if (_t_aligned_copy(&ctlbuf, call->opt.len, size,
206 		    call->opt.buf, &cres->OPT_offset) < 0) {
207 			/*
208 			 * Aligned copy will overflow buffer allocated based
209 			 * transport maximum options length.
210 			 * return error.
211 			 */
212 			t_errno = TBADOPT;
213 			goto err_out;
214 		}
215 		size = cres->OPT_offset + cres->OPT_length;
216 	}
217 
218 	if (call->udata.len) {
219 		if ((tiptr->ti_cdatasize == T_INVALID /* -2 */) ||
220 		    ((tiptr->ti_cdatasize != T_INFINITE /* -1 */) &&
221 			(call->udata.len > (uint32_t)tiptr->ti_cdatasize))) {
222 			/*
223 			 * user data not valid with connect or it
224 			 * exceeds the limits specified by the transport
225 			 * provider
226 			 */
227 			t_errno = TBADDATA;
228 			goto err_out;
229 		}
230 	}
231 
232 
233 	ctlbuf.len = size;
234 
235 	/*
236 	 * Assumes signals are blocked so putmsg() will not block
237 	 * indefinitely
238 	 */
239 	if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) {
240 		/*
241 		 * Assumes signals are blocked so putmsg() will not block
242 		 * indefinitely
243 		 */
244 		if (putmsg(fd, &ctlbuf,
245 		    (struct strbuf *)(call->udata.len? &call->udata: NULL), 0) <
246 		    0) {
247 			if (errno == EAGAIN)
248 				t_errno = TFLOW;
249 			else
250 				t_errno = TSYSERR;
251 			goto err_out;
252 		}
253 	} else {
254 		strfdinsert.ctlbuf.maxlen = ctlbuf.maxlen;
255 		strfdinsert.ctlbuf.len = ctlbuf.len;
256 		strfdinsert.ctlbuf.buf = ctlbuf.buf;
257 
258 		strfdinsert.databuf.maxlen = call->udata.maxlen;
259 		strfdinsert.databuf.len =
260 		    (call->udata.len? call->udata.len: -1);
261 		strfdinsert.databuf.buf = call->udata.buf;
262 		strfdinsert.fildes = resfd;
263 		strfdinsert.offset = (int)sizeof (t_scalar_t);
264 		strfdinsert.flags = 0;		/* could be EXPEDITED also */
265 
266 		if (ioctl(fd, I_FDINSERT, &strfdinsert) < 0) {
267 			if (errno == EAGAIN)
268 				t_errno = TFLOW;
269 			else
270 				t_errno = TSYSERR;
271 			goto err_out;
272 		}
273 	}
274 
275 	if (_t_is_ok(fd, tiptr, conn_res_prim) < 0) {
276 		/*
277 		 * At the TPI level, the error returned in a T_ERROR_ACK
278 		 * received in response to a T_CONN_RES for a listener and
279 		 * acceptor endpoints not being the same kind of endpoints
280 		 * has changed to a new t_errno code introduced with
281 		 * XTI (TPROVMISMATCH). We need to adjust TLI error code
282 		 * to be same as before.
283 		 */
284 		if (_T_IS_TLI(api_semantics) && t_errno == TPROVMISMATCH) {
285 			/* TLI only */
286 			t_errno = TBADF;
287 		}
288 		goto err_out;
289 	}
290 
291 	if (tiptr->ti_ocnt == 1) {
292 		if (fd == resfd) {
293 			_T_TX_NEXTSTATE(T_ACCEPT1, tiptr,
294 				"t_accept: invalid state event T_ACCEPT1");
295 		} else {
296 			_T_TX_NEXTSTATE(T_ACCEPT2, tiptr,
297 				"t_accept: invalid state event T_ACCEPT2");
298 			/*
299 			 * XXX Here we lock the resfd lock also. This
300 			 * is an instance of holding two locks without
301 			 * any enforcement of a locking hiararchy.
302 			 * There is potential for deadlock in incorrect
303 			 * or buggy programs here but this is the safer
304 			 * choice in this case. Correct programs will not
305 			 * deadlock.
306 			 */
307 			sig_mutex_lock(&restiptr->ti_lock);
308 			_T_TX_NEXTSTATE(T_PASSCON, restiptr,
309 				"t_accept: invalid state event T_PASSCON");
310 			sig_mutex_unlock(&restiptr->ti_lock);
311 		}
312 	} else {
313 		_T_TX_NEXTSTATE(T_ACCEPT3, tiptr,
314 				"t_accept: invalid state event T_ACCEPT3");
315 		if (fd != resfd)
316 			sig_mutex_lock(&restiptr->ti_lock);
317 		_T_TX_NEXTSTATE(T_PASSCON, restiptr,
318 				"t_accept: invalid state event T_PASSCON");
319 		if (fd != resfd)
320 			sig_mutex_unlock(&restiptr->ti_lock);
321 	}
322 
323 	tiptr->ti_ocnt--;
324 	tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
325 
326 	/*
327 	 * Update attributes which may have been negotiated during
328 	 * connection establishment for protocols where we suspect
329 	 * such negotiation is likely (e.g. OSI). We do not do it for
330 	 * all endpoints for performance reasons. Also, this code is
331 	 * deliberately done after user level state changes so even
332 	 * the (unlikely) failure case reflects a connected endpoint.
333 	 */
334 	if (restiptr->ti_tsdusize != 0) {
335 		if (_t_do_postconn_sync(resfd, restiptr) < 0)
336 			goto err_out;
337 	}
338 
339 	if (didalloc)
340 		free(ctlbuf.buf);
341 	else
342 		tiptr->ti_ctlbuf = ctlbuf.buf;
343 	sig_mutex_unlock(&tiptr->ti_lock);
344 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
345 	return (0);
346 	/* NOTREACHED */
347 err_out:
348 	sv_errno = errno;
349 	if (didalloc)
350 		free(ctlbuf.buf);
351 	else
352 		tiptr->ti_ctlbuf = ctlbuf.buf;
353 	sig_mutex_unlock(&tiptr->ti_lock);
354 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
355 	errno = sv_errno;
356 	return (-1);
357 }
358