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