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