xref: /titanic_50/usr/src/lib/libnsl/nsl/t_getname.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 		/* SVr4.0 1.1.1.1 */
34 
35 #include <stdlib.h>
36 #include "mt.h"
37 #include <errno.h>
38 #include <rpc/trace.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <stropts.h>
42 #include <sys/stream.h>
43 #include <xti.h>
44 #define	_SUN_TPI_VERSION 2
45 #include <sys/tihdr.h>
46 #include <sys/timod.h>
47 #include <assert.h>
48 #include <signal.h>
49 #include "tx.h"
50 
51 static int __tx_tlitpi_getprotaddr_locked(struct _ti_user *tiptr,
52 	struct t_bind *boundaddr, struct t_bind *peer);
53 
54 
55 static int __tx_getname_locked(int fd, struct netbuf *name, int type);
56 
57 int
58 _tx_getname(int fd, struct netbuf *name, int type, int api_semantics)
59 {
60 	struct _ti_user *tiptr;
61 	int retval, sv_errno;
62 
63 	trace3(TR_t_getname, 0, fd, type);
64 	assert(_T_IS_TLI(api_semantics)); /* TLI only interface */
65 	if (!name || ((type != LOCALNAME) && (type != REMOTENAME))) {
66 		trace3(TR_t_getname, 1, fd, type);
67 		errno = EINVAL;
68 		return (-1);
69 	}
70 
71 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0) {
72 		sv_errno = errno;
73 		trace3(TR_t_getname, 1, fd, type);
74 		errno = sv_errno;
75 		return (-1);
76 	}
77 	sig_mutex_lock(&tiptr->ti_lock);
78 
79 	retval = __tx_getname_locked(fd, name, type);
80 
81 	if (retval < 0) {
82 		sv_errno = errno;
83 		sig_mutex_unlock(&tiptr->ti_lock);
84 		trace3(TR_t_getname, 1, fd, type);
85 		errno = sv_errno;
86 		return (-1);
87 	}
88 
89 	sig_mutex_unlock(&tiptr->ti_lock);
90 
91 	trace3(TR_t_getname, 1, fd, type);
92 	return (0);
93 }
94 
95 
96 static int
97 __tx_getname_locked(int fd, struct netbuf *name, int type)
98 {
99 	int retval;
100 
101 	do {
102 		retval = _ioctl(fd,
103 		    (type == LOCALNAME) ? TI_GETMYNAME : TI_GETPEERNAME, name);
104 	} while (retval < 0 && errno == EINTR);
105 
106 	if (retval < 0) {
107 		t_errno = TSYSERR;
108 		return (-1);
109 	}
110 	return (0);
111 }
112 
113 
114 
115 int
116 _tx_getprotaddr(
117 	int fd,
118 	struct t_bind *boundaddr,
119 	struct t_bind *peeraddr,
120 	int api_semantics)
121 {
122 	struct _ti_user *tiptr;
123 	int retval, sv_errno;
124 	struct T_addr_req *addreqp;
125 	struct T_addr_ack *addrackp;
126 	int didalloc;
127 	struct strbuf ctlbuf;
128 	int retlen;
129 
130 	trace2(TR_t_getprotaddr, 0, fd);
131 
132 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0) {
133 		sv_errno = errno;
134 		trace2(TR_t_getprotaddr, 1, fd);
135 		errno = sv_errno;
136 		return (-1);
137 	}
138 
139 	sig_mutex_lock(&tiptr->ti_lock);
140 
141 	if ((tiptr->ti_prov_flag & XPG4_1) == 0) {
142 		/*
143 		 * Provider does not support XTI inspired TPI so we
144 		 * try to do operation assuming TLI inspired TPI
145 		 */
146 		retval = __tx_tlitpi_getprotaddr_locked(tiptr, boundaddr,
147 			peeraddr);
148 		sv_errno = errno;
149 		sig_mutex_unlock(&tiptr->ti_lock);
150 		errno = sv_errno;
151 		return (retval);
152 	}
153 
154 	/*
155 	 * Acquire buffer for use in sending/receiving the message.
156 	 * Note: assumes (correctly) that ti_ctlsize is large enough
157 	 * to hold sizeof (struct T_addr_req/ack)
158 	 */
159 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
160 		sv_errno = errno;
161 		sig_mutex_unlock(&tiptr->ti_lock);
162 		trace2(TR_t_bind, 1, fd);
163 		errno = sv_errno;
164 		return (-1);
165 	}
166 
167 	addreqp = (struct T_addr_req *)ctlbuf.buf;
168 	addreqp->PRIM_type = T_ADDR_REQ;
169 
170 	do {
171 		retval = _t_do_ioctl(fd, ctlbuf.buf,
172 			(int)sizeof (struct T_addr_req), TI_GETADDRS, &retlen);
173 	} while (retval < 0 && errno == EINTR);
174 
175 	/* retval can now be either 0 or -1 */
176 	if (retval < 0) {
177 		sv_errno = errno;
178 		sig_mutex_unlock(&tiptr->ti_lock);
179 		trace2(TR_t_getprotaddr, 1, fd);
180 		errno = sv_errno;
181 		goto err_out;
182 	}
183 	sig_mutex_unlock(&tiptr->ti_lock);
184 
185 	if (retlen < (int)sizeof (struct T_addr_ack)) {
186 		t_errno = TSYSERR;
187 		trace2(TR_t_getprotaddr, 1, fd);
188 		errno = EIO;
189 		retval = -1;
190 		goto err_out;
191 	}
192 
193 	addrackp = (struct T_addr_ack *)ctlbuf.buf;
194 
195 	/*
196 	 * We assume null parameters are OK and not errors
197 	 */
198 	if (boundaddr != NULL && boundaddr->addr.maxlen > 0) {
199 		if (TLEN_GT_NLEN(addrackp->LOCADDR_length,
200 		    boundaddr->addr.maxlen)) {
201 			t_errno = TBUFOVFLW;
202 			trace2(TR_t_getprotaddr, 1, fd);
203 			retval = -1;
204 			goto err_out;
205 		}
206 		boundaddr->addr.len = addrackp->LOCADDR_length;
207 		memcpy(boundaddr->addr.buf,
208 		    ctlbuf.buf + addrackp->LOCADDR_offset,
209 		    (size_t)addrackp->LOCADDR_length);
210 	}
211 
212 	/*
213 	 * Note: In states where there is no remote end of the connection
214 	 * the T_ADDR_REQ primitive does not return a remote address. However,
215 	 * in protcols such as TCP, the transport connection is established
216 	 * before the TLI/XTI level association is established. Therefore,
217 	 * in state T_OUTCON, the transport may return a remote address where
218 	 * TLI/XTI level thinks there is no remote end and therefore
219 	 * no remote address should be returned. We therefore do not look at
220 	 * address returned by transport provider in T_OUTCON state.
221 	 * Tested by XTI test suite.
222 	 */
223 	if (tiptr->ti_state != T_OUTCON &&
224 	    peeraddr != NULL && peeraddr->addr.maxlen > 0) {
225 		if (TLEN_GT_NLEN(addrackp->REMADDR_length,
226 		    peeraddr->addr.maxlen)) {
227 			t_errno = TBUFOVFLW;
228 			trace2(TR_t_getprotaddr, 1, fd);
229 			retval = -1;
230 			goto err_out;
231 		}
232 		peeraddr->addr.len = addrackp->REMADDR_length;
233 		memcpy(peeraddr->addr.buf,
234 		    ctlbuf.buf + addrackp->REMADDR_offset,
235 		    (size_t)addrackp->REMADDR_length);
236 	}
237 
238 	trace2(TR_t_getprotaddr, 1, fd);
239 
240 err_out:
241 	if (didalloc)
242 		free(ctlbuf.buf);
243 	else
244 		tiptr->ti_ctlbuf = ctlbuf.buf;
245 	return (retval);
246 }
247 
248 static int
249 __tx_tlitpi_getprotaddr_locked(
250 	struct _ti_user *tiptr,
251 	struct t_bind *boundaddr,
252 	struct t_bind *peeraddr)
253 {
254 	if (boundaddr) {
255 		boundaddr->addr.len = 0;
256 		if (tiptr->ti_state >= TS_IDLE) {
257 			/*
258 			 * assume bound endpoint .
259 			 * Note: TI_GETMYNAME can return
260 			 * a finite length all zeroes address for unbound
261 			 * endpoint so we avoid relying on it for bound
262 			 * endpoints for XTI t_getprotaddr() semantics.
263 			 */
264 			if (__tx_getname_locked(tiptr->ti_fd, &boundaddr->addr,
265 			    LOCALNAME) < 0)
266 				return (-1);
267 
268 		}
269 	}
270 	if (peeraddr) {
271 
272 		peeraddr->addr.len = 0;
273 
274 		if (tiptr->ti_state >= TS_DATA_XFER) {
275 			/*
276 			 * assume connected endpoint.
277 			 * The TI_GETPEERNAME call can fail with error
278 			 * if endpoint is not connected so we don't call it
279 			 * for XTI t_getprotaddr() semantics
280 			 */
281 			if (__tx_getname_locked(tiptr->ti_fd, &peeraddr->addr,
282 			    REMOTENAME) < 0)
283 				return (-1);
284 		}
285 	}
286 	return (0);
287 }
288