1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.5.2.1 */
#include "mt.h"
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stropts.h>
#include <sys/stream.h>
#define _SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/timod.h>
#include <xti.h>
#include <signal.h>
#include <syslog.h>
#include <assert.h>
#include "tx.h"
int
_tx_accept(
int fd,
int resfd,
const struct t_call *call,
int api_semantics
)
{
struct T_conn_res *cres;
struct strfdinsert strfdinsert;
int size, retval, sv_errno;
struct _ti_user *tiptr;
struct _ti_user *restiptr;
sigset_t mask;
struct strbuf ctlbuf;
int didalloc;
t_scalar_t conn_res_prim;
if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
return (-1);
if ((restiptr = _t_checkfd(resfd, 0, api_semantics)) == NULL)
return (-1);
/*
* We need to block signals to perform the I_FDINSERT operation
* (sending T_CONN_RES downstream) which is non-idempotent.
* Note that sig_mutex_lock() only defers signals, it does not
* block them, so interruptible syscalls could still get EINTR.
*/
(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
sig_mutex_lock(&tiptr->ti_lock);
if (tiptr->ti_servtype == T_CLTS) {
t_errno = TNOTSUPPORT;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
if (_T_IS_XTI(api_semantics)) {
/*
* User level state verification only done for XTI
* because doing for TLI may break existing applications
*
* For fd == resfd, state should be T_INCON
* For fd != resfd,
* fd state should be T_INCON
* resfd state should be T_IDLE (bound endpoint) or
* it can be T_UNBND. The T_UNBND case is not (yet?)
* allowed in the published XTI spec but fixed by the
* corrigenda.
*/
if ((fd == resfd && tiptr->ti_state != T_INCON) ||
(fd != resfd &&
((tiptr->ti_state != T_INCON) ||
!(restiptr->ti_state == T_IDLE ||
restiptr->ti_state == T_UNBND)))) {
t_errno = TOUTSTATE;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
/*
* XTI says:
* If fd != resfd, and a resfd bound to a protocol address is
* passed, then it better not have a qlen > 0.
* That is, an endpoint bound as if it will be a listener
* cannot be used as an acceptor.
*/
if (fd != resfd && restiptr->ti_state == T_IDLE &&
restiptr->ti_qlen > 0) {
t_errno = TRESQLEN;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
if (fd == resfd && tiptr->ti_ocnt > 1) {
t_errno = TINDOUT;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
/*
* Note: TRESADDR error is specified by XTI. It happens
* when resfd is bound and fd and resfd are not BOUND to
* the same protocol address. TCP obviously does allow
* two endpoints to bind to the same address. Why is the
* need for this error considering there is an address switch
* that can be done for the endpoint at accept time ? Go
* figure and ask the XTI folks.
* We interpret this to be a transport specific error condition
* to be be coveyed by the transport provider in T_ERROR_ACK
* to T_CONN_RES on transports that allow two endpoints to
* be bound to the same address and have trouble with the
* idea of accepting connections on a resfd that has a qlen > 0
*/
}
if (fd != resfd) {
if ((retval = ioctl(resfd, I_NREAD, &size)) < 0) {
sv_errno = errno;
t_errno = TSYSERR;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
errno = sv_errno;
return (-1);
}
if (retval > 0) {
t_errno = TBADF;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
}
/*
* Acquire ctlbuf for use in sending/receiving control part
* of the message.
*/
if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
errno = sv_errno;
return (-1);
}
/*
* In Unix98 t_accept() need not return [TLOOK] if connect/disconnect
* indications are present. TLI and Unix95 need to return error.
*/
if (_T_API_VER_LT(api_semantics, TX_XTI_XNS5_API)) {
if (_t_is_event(fd, tiptr) < 0)
goto err_out;
}
/* LINTED pointer cast */
cres = (struct T_conn_res *)ctlbuf.buf;
cres->OPT_length = call->opt.len;
cres->OPT_offset = 0;
cres->SEQ_number = call->sequence;
if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) {
cres->ACCEPTOR_id = restiptr->acceptor_id;
cres->PRIM_type = conn_res_prim = T_CONN_RES;
} else {
/* I_FDINSERT should use O_T_CONN_RES. */
cres->ACCEPTOR_id = 0;
cres->PRIM_type = conn_res_prim = O_T_CONN_RES;
}
size = (int)sizeof (struct T_conn_res);
if (call->opt.len) {
if (_t_aligned_copy(&ctlbuf, call->opt.len, size,
call->opt.buf, &cres->OPT_offset) < 0) {
/*
* Aligned copy will overflow buffer allocated based
* transport maximum options length.
* return error.
*/
t_errno = TBADOPT;
goto err_out;
}
size = cres->OPT_offset + cres->OPT_length;
}
if (call->udata.len) {
if ((tiptr->ti_cdatasize == T_INVALID /* -2 */) ||
((tiptr->ti_cdatasize != T_INFINITE /* -1 */) &&
(call->udata.len > (uint32_t)tiptr->ti_cdatasize))) {
/*
* user data not valid with connect or it
* exceeds the limits specified by the transport
* provider
*/
t_errno = TBADDATA;
goto err_out;
}
}
ctlbuf.len = size;
/*
* Assumes signals are blocked so putmsg() will not block
* indefinitely
*/
if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) {
/*
* Assumes signals are blocked so putmsg() will not block
* indefinitely
*/
if (putmsg(fd, &ctlbuf,
(struct strbuf *)(call->udata.len? &call->udata: NULL), 0) <
0) {
if (errno == EAGAIN)
t_errno = TFLOW;
else
t_errno = TSYSERR;
goto err_out;
}
} else {
strfdinsert.ctlbuf.maxlen = ctlbuf.maxlen;
strfdinsert.ctlbuf.len = ctlbuf.len;
strfdinsert.ctlbuf.buf = ctlbuf.buf;
strfdinsert.databuf.maxlen = call->udata.maxlen;
strfdinsert.databuf.len =
(call->udata.len? call->udata.len: -1);
strfdinsert.databuf.buf = call->udata.buf;
strfdinsert.fildes = resfd;
strfdinsert.offset = (int)sizeof (t_scalar_t);
strfdinsert.flags = 0; /* could be EXPEDITED also */
if (ioctl(fd, I_FDINSERT, &strfdinsert) < 0) {
if (errno == EAGAIN)
t_errno = TFLOW;
else
t_errno = TSYSERR;
goto err_out;
}
}
if (_t_is_ok(fd, tiptr, conn_res_prim) < 0) {
/*
* At the TPI level, the error returned in a T_ERROR_ACK
* received in response to a T_CONN_RES for a listener and
* acceptor endpoints not being the same kind of endpoints
* has changed to a new t_errno code introduced with
* XTI (TPROVMISMATCH). We need to adjust TLI error code
* to be same as before.
*/
if (_T_IS_TLI(api_semantics) && t_errno == TPROVMISMATCH) {
/* TLI only */
t_errno = TBADF;
}
goto err_out;
}
if (tiptr->ti_ocnt == 1) {
if (fd == resfd) {
_T_TX_NEXTSTATE(T_ACCEPT1, tiptr,
"t_accept: invalid state event T_ACCEPT1");
} else {
_T_TX_NEXTSTATE(T_ACCEPT2, tiptr,
"t_accept: invalid state event T_ACCEPT2");
/*
* XXX Here we lock the resfd lock also. This
* is an instance of holding two locks without
* any enforcement of a locking hiararchy.
* There is potential for deadlock in incorrect
* or buggy programs here but this is the safer
* choice in this case. Correct programs will not
* deadlock.
*/
sig_mutex_lock(&restiptr->ti_lock);
_T_TX_NEXTSTATE(T_PASSCON, restiptr,
"t_accept: invalid state event T_PASSCON");
sig_mutex_unlock(&restiptr->ti_lock);
}
} else {
_T_TX_NEXTSTATE(T_ACCEPT3, tiptr,
"t_accept: invalid state event T_ACCEPT3");
if (fd != resfd)
sig_mutex_lock(&restiptr->ti_lock);
_T_TX_NEXTSTATE(T_PASSCON, restiptr,
"t_accept: invalid state event T_PASSCON");
if (fd != resfd)
sig_mutex_unlock(&restiptr->ti_lock);
}
tiptr->ti_ocnt--;
tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
/*
* Update attributes which may have been negotiated during
* connection establishment for protocols where we suspect
* such negotiation is likely (e.g. OSI). We do not do it for
* all endpoints for performance reasons. Also, this code is
* deliberately done after user level state changes so even
* the (unlikely) failure case reflects a connected endpoint.
*/
if (restiptr->ti_tsdusize != 0) {
if (_t_do_postconn_sync(resfd, restiptr) < 0)
goto err_out;
}
if (didalloc)
free(ctlbuf.buf);
else
tiptr->ti_ctlbuf = ctlbuf.buf;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (0);
/* NOTREACHED */
err_out:
sv_errno = errno;
if (didalloc)
free(ctlbuf.buf);
else
tiptr->ti_ctlbuf = ctlbuf.buf;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
errno = sv_errno;
return (-1);
}
|