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