xref: /titanic_51/usr/src/lib/libnsl/nsl/t_bind.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 1993-2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3.4.1 */
33 
34 #include "mt.h"
35 #include <stdlib.h>
36 #include <rpc/trace.h>
37 #include <errno.h>
38 #include <string.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 <signal.h>
46 #include <syslog.h>
47 #include "tx.h"
48 
49 int
50 _tx_bind(
51 	int fd,
52 	const struct t_bind *req,
53 	struct t_bind *ret,
54 	int api_semantics
55 )
56 {
57 	struct T_bind_req *bind_reqp;
58 	struct T_bind_ack *bind_ackp;
59 	int size, sv_errno, retlen;
60 	struct _ti_user *tiptr;
61 	sigset_t mask;
62 
63 	int didalloc;
64 	int use_xpg41tpi;
65 	struct strbuf ctlbuf;
66 
67 	trace2(TR_t_bind, 0, fd);
68 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL) {
69 		sv_errno = errno;
70 		trace2(TR_t_bind, 1, fd);
71 		errno = sv_errno;
72 		return (-1);
73 	}
74 
75 	/*
76 	 * We block all signals since TI_BIND, which sends a TPI message
77 	 * O_T_BIND_REQ down, is not an idempotetent operation
78 	 * Note that sig_mutex_lock() only defers signals, it does not
79 	 * block them, so interruptible syscalls could still get EINTR.
80 	 */
81 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
82 	sig_mutex_lock(&tiptr->ti_lock);
83 	if (_T_IS_XTI(api_semantics)) {
84 		/*
85 		 * User level state verification only done for XTI
86 		 * because doing for TLI may break existing applications
87 		 */
88 		if (tiptr->ti_state != T_UNBND) {
89 			t_errno = TOUTSTATE;
90 			sig_mutex_unlock(&tiptr->ti_lock);
91 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
92 			trace2(TR_t_bind, 1, fd);
93 			return (-1);
94 		}
95 	}
96 	/*
97 	 * Acquire buffer for use in sending/receiving the message.
98 	 * Note: assumes (correctly) that ti_ctlsize is large enough
99 	 * to hold sizeof (struct T_bind_req/ack)
100 	 */
101 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
102 		sv_errno = errno;
103 		sig_mutex_unlock(&tiptr->ti_lock);
104 		(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
105 		trace2(TR_t_bind, 1, fd);
106 		errno = sv_errno;
107 		return (-1);
108 	}
109 
110 	bind_reqp = (struct T_bind_req *)ctlbuf.buf;
111 	size = (int)sizeof (struct T_bind_req);
112 
113 	use_xpg41tpi = (_T_IS_XTI(api_semantics)) &&
114 		((tiptr->ti_prov_flag & XPG4_1) != 0);
115 	if (use_xpg41tpi)
116 		/* XTI call and provider knows the XTI inspired TPI */
117 		bind_reqp->PRIM_type = T_BIND_REQ;
118 	else
119 		/* TLI caller old TPI provider */
120 		bind_reqp->PRIM_type = O_T_BIND_REQ;
121 
122 	bind_reqp->ADDR_length = (req == NULL? 0: req->addr.len);
123 	bind_reqp->ADDR_offset = 0;
124 	bind_reqp->CONIND_number = (req == NULL? 0: req->qlen);
125 
126 
127 	if (bind_reqp->ADDR_length) {
128 		if (_t_aligned_copy(&ctlbuf, (int)bind_reqp->ADDR_length, size,
129 		    req->addr.buf, &bind_reqp->ADDR_offset) < 0) {
130 			/*
131 			 * Aligned copy will overflow buffer allocated based
132 			 * on transport maximum address length.
133 			 * return error.
134 			 */
135 			t_errno = TBADADDR;
136 			goto err_out;
137 		}
138 		size = bind_reqp->ADDR_offset + bind_reqp->ADDR_length;
139 	}
140 
141 	if (_t_do_ioctl(fd, ctlbuf.buf, size, TI_BIND, &retlen) < 0) {
142 		goto err_out;
143 	}
144 
145 	if (retlen < (int)sizeof (struct T_bind_ack)) {
146 		t_errno = TSYSERR;
147 		errno = EIO;
148 		goto err_out;
149 	}
150 
151 	bind_ackp = (struct T_bind_ack *)ctlbuf.buf;
152 
153 	if ((req != NULL) && req->addr.len != 0 &&
154 	    (use_xpg41tpi == 0) && (_T_IS_XTI(api_semantics))) {
155 		/*
156 		 * Best effort to do XTI on old TPI.
157 		 *
158 		 * Match address requested or unbind and fail with
159 		 * TADDRBUSY.
160 		 *
161 		 * XXX - Hack alert ! Should we do this at all ?
162 		 * Not "supported" as may not work if encoding of
163 		 * address is different in the returned address. This
164 		 * will also have trouble with TCP/UDP wildcard port
165 		 * requests
166 		 */
167 		if ((req->addr.len != bind_ackp->ADDR_length) ||
168 		    (memcmp(req->addr.buf, ctlbuf.buf +
169 		    bind_ackp->ADDR_offset, req->addr.len) != 0)) {
170 			(void) _tx_unbind_locked(fd, tiptr, &ctlbuf);
171 			t_errno = TADDRBUSY;
172 			goto err_out;
173 		}
174 	}
175 
176 	tiptr->ti_ocnt = 0;
177 	tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
178 
179 	_T_TX_NEXTSTATE(T_BIND, tiptr, "t_bind: invalid state event T_BIND");
180 
181 	if (ret != NULL) {
182 		if (_T_IS_TLI(api_semantics) || ret->addr.maxlen > 0) {
183 			if (TLEN_GT_NLEN(bind_reqp->ADDR_length,
184 			    ret->addr.maxlen)) {
185 				t_errno = TBUFOVFLW;
186 				goto err_out;
187 			}
188 			(void) memcpy(ret->addr.buf,
189 			    ctlbuf.buf + bind_ackp->ADDR_offset,
190 			    (size_t)bind_ackp->ADDR_length);
191 			ret->addr.len = bind_ackp->ADDR_length;
192 		}
193 		ret->qlen = bind_ackp->CONIND_number;
194 	}
195 
196 	tiptr->ti_qlen = (uint_t)bind_ackp->CONIND_number;
197 
198 	if (didalloc)
199 		free(ctlbuf.buf);
200 	else
201 		tiptr->ti_ctlbuf = ctlbuf.buf;
202 	sig_mutex_unlock(&tiptr->ti_lock);
203 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
204 	trace2(TR_t_bind, 1, fd);
205 	return (0);
206 	/* NOTREACHED */
207 err_out:
208 	sv_errno = errno;
209 	if (didalloc)
210 		free(ctlbuf.buf);
211 	else
212 		tiptr->ti_ctlbuf = ctlbuf.buf;
213 	sig_mutex_unlock(&tiptr->ti_lock);
214 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
215 	trace2(TR_t_bind, 1, fd);
216 	errno = sv_errno;
217 	return (-1);
218 }
219