10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22132Srobinson
230Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
240Sstevel@tonic-gate /* All Rights Reserved */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
27*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
280Sstevel@tonic-gate * Use is subject to license terms.
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.5.2.1 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "mt.h"
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <stropts.h>
380Sstevel@tonic-gate #include <sys/stream.h>
390Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
400Sstevel@tonic-gate #include <sys/tihdr.h>
410Sstevel@tonic-gate #include <sys/timod.h>
420Sstevel@tonic-gate #include <xti.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <syslog.h>
450Sstevel@tonic-gate #include <assert.h>
460Sstevel@tonic-gate #include "tx.h"
470Sstevel@tonic-gate
480Sstevel@tonic-gate int
_tx_accept(int fd,int resfd,const struct t_call * call,int api_semantics)490Sstevel@tonic-gate _tx_accept(
500Sstevel@tonic-gate int fd,
510Sstevel@tonic-gate int resfd,
520Sstevel@tonic-gate const struct t_call *call,
530Sstevel@tonic-gate int api_semantics
540Sstevel@tonic-gate )
550Sstevel@tonic-gate {
560Sstevel@tonic-gate struct T_conn_res *cres;
570Sstevel@tonic-gate struct strfdinsert strfdinsert;
580Sstevel@tonic-gate int size, retval, sv_errno;
590Sstevel@tonic-gate struct _ti_user *tiptr;
600Sstevel@tonic-gate struct _ti_user *restiptr;
610Sstevel@tonic-gate sigset_t mask;
620Sstevel@tonic-gate struct strbuf ctlbuf;
630Sstevel@tonic-gate int didalloc;
640Sstevel@tonic-gate t_scalar_t conn_res_prim;
650Sstevel@tonic-gate
66132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
670Sstevel@tonic-gate return (-1);
68132Srobinson if ((restiptr = _t_checkfd(resfd, 0, api_semantics)) == NULL)
690Sstevel@tonic-gate return (-1);
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * We need to block signals to perform the I_FDINSERT operation
730Sstevel@tonic-gate * (sending T_CONN_RES downstream) which is non-idempotent.
740Sstevel@tonic-gate * Note that sig_mutex_lock() only defers signals, it does not
750Sstevel@tonic-gate * block them, so interruptible syscalls could still get EINTR.
760Sstevel@tonic-gate */
770Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
780Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (tiptr->ti_servtype == T_CLTS) {
810Sstevel@tonic-gate t_errno = TNOTSUPPORT;
820Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
830Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
840Sstevel@tonic-gate return (-1);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * User level state verification only done for XTI
900Sstevel@tonic-gate * because doing for TLI may break existing applications
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * For fd == resfd, state should be T_INCON
930Sstevel@tonic-gate * For fd != resfd,
940Sstevel@tonic-gate * fd state should be T_INCON
950Sstevel@tonic-gate * resfd state should be T_IDLE (bound endpoint) or
960Sstevel@tonic-gate * it can be T_UNBND. The T_UNBND case is not (yet?)
970Sstevel@tonic-gate * allowed in the published XTI spec but fixed by the
980Sstevel@tonic-gate * corrigenda.
990Sstevel@tonic-gate */
1000Sstevel@tonic-gate if ((fd == resfd && tiptr->ti_state != T_INCON) ||
1010Sstevel@tonic-gate (fd != resfd &&
1020Sstevel@tonic-gate ((tiptr->ti_state != T_INCON) ||
103132Srobinson !(restiptr->ti_state == T_IDLE ||
1040Sstevel@tonic-gate restiptr->ti_state == T_UNBND)))) {
1050Sstevel@tonic-gate t_errno = TOUTSTATE;
1060Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1070Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1080Sstevel@tonic-gate return (-1);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate * XTI says:
1130Sstevel@tonic-gate * If fd != resfd, and a resfd bound to a protocol address is
1140Sstevel@tonic-gate * passed, then it better not have a qlen > 0.
1150Sstevel@tonic-gate * That is, an endpoint bound as if it will be a listener
1160Sstevel@tonic-gate * cannot be used as an acceptor.
1170Sstevel@tonic-gate */
1180Sstevel@tonic-gate if (fd != resfd && restiptr->ti_state == T_IDLE &&
1190Sstevel@tonic-gate restiptr->ti_qlen > 0) {
1200Sstevel@tonic-gate t_errno = TRESQLEN;
1210Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1220Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1230Sstevel@tonic-gate return (-1);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (fd == resfd && tiptr->ti_ocnt > 1) {
1270Sstevel@tonic-gate t_errno = TINDOUT;
1280Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1290Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1300Sstevel@tonic-gate return (-1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * Note: TRESADDR error is specified by XTI. It happens
1350Sstevel@tonic-gate * when resfd is bound and fd and resfd are not BOUND to
1360Sstevel@tonic-gate * the same protocol address. TCP obviously does allow
1370Sstevel@tonic-gate * two endpoints to bind to the same address. Why is the
1380Sstevel@tonic-gate * need for this error considering there is an address switch
1390Sstevel@tonic-gate * that can be done for the endpoint at accept time ? Go
1400Sstevel@tonic-gate * figure and ask the XTI folks.
1410Sstevel@tonic-gate * We interpret this to be a transport specific error condition
1420Sstevel@tonic-gate * to be be coveyed by the transport provider in T_ERROR_ACK
1430Sstevel@tonic-gate * to T_CONN_RES on transports that allow two endpoints to
1440Sstevel@tonic-gate * be bound to the same address and have trouble with the
1450Sstevel@tonic-gate * idea of accepting connections on a resfd that has a qlen > 0
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate if (fd != resfd) {
150*1219Sraf if ((retval = ioctl(resfd, I_NREAD, &size)) < 0) {
1510Sstevel@tonic-gate sv_errno = errno;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate t_errno = TSYSERR;
1540Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1550Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1560Sstevel@tonic-gate errno = sv_errno;
1570Sstevel@tonic-gate return (-1);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate if (retval > 0) {
1600Sstevel@tonic-gate t_errno = TBADF;
1610Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1620Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1630Sstevel@tonic-gate return (-1);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * Acquire ctlbuf for use in sending/receiving control part
1690Sstevel@tonic-gate * of the message.
1700Sstevel@tonic-gate */
1710Sstevel@tonic-gate if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
1720Sstevel@tonic-gate sv_errno = errno;
1730Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1740Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1750Sstevel@tonic-gate errno = sv_errno;
1760Sstevel@tonic-gate return (-1);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate * In Unix98 t_accept() need not return [TLOOK] if connect/disconnect
1810Sstevel@tonic-gate * indications are present. TLI and Unix95 need to return error.
1820Sstevel@tonic-gate */
1830Sstevel@tonic-gate if (_T_API_VER_LT(api_semantics, TX_XTI_XNS5_API)) {
1840Sstevel@tonic-gate if (_t_is_event(fd, tiptr) < 0)
1850Sstevel@tonic-gate goto err_out;
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
188132Srobinson /* LINTED pointer cast */
1890Sstevel@tonic-gate cres = (struct T_conn_res *)ctlbuf.buf;
1900Sstevel@tonic-gate cres->OPT_length = call->opt.len;
1910Sstevel@tonic-gate cres->OPT_offset = 0;
1920Sstevel@tonic-gate cres->SEQ_number = call->sequence;
1930Sstevel@tonic-gate if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) {
1940Sstevel@tonic-gate cres->ACCEPTOR_id = restiptr->acceptor_id;
1950Sstevel@tonic-gate cres->PRIM_type = conn_res_prim = T_CONN_RES;
1960Sstevel@tonic-gate } else {
1970Sstevel@tonic-gate /* I_FDINSERT should use O_T_CONN_RES. */
1980Sstevel@tonic-gate cres->ACCEPTOR_id = 0;
1990Sstevel@tonic-gate cres->PRIM_type = conn_res_prim = O_T_CONN_RES;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate size = (int)sizeof (struct T_conn_res);
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate if (call->opt.len) {
2050Sstevel@tonic-gate if (_t_aligned_copy(&ctlbuf, call->opt.len, size,
2060Sstevel@tonic-gate call->opt.buf, &cres->OPT_offset) < 0) {
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * Aligned copy will overflow buffer allocated based
2090Sstevel@tonic-gate * transport maximum options length.
2100Sstevel@tonic-gate * return error.
2110Sstevel@tonic-gate */
2120Sstevel@tonic-gate t_errno = TBADOPT;
2130Sstevel@tonic-gate goto err_out;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate size = cres->OPT_offset + cres->OPT_length;
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if (call->udata.len) {
2190Sstevel@tonic-gate if ((tiptr->ti_cdatasize == T_INVALID /* -2 */) ||
2200Sstevel@tonic-gate ((tiptr->ti_cdatasize != T_INFINITE /* -1 */) &&
2210Sstevel@tonic-gate (call->udata.len > (uint32_t)tiptr->ti_cdatasize))) {
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate * user data not valid with connect or it
2240Sstevel@tonic-gate * exceeds the limits specified by the transport
2250Sstevel@tonic-gate * provider
2260Sstevel@tonic-gate */
2270Sstevel@tonic-gate t_errno = TBADDATA;
2280Sstevel@tonic-gate goto err_out;
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate ctlbuf.len = size;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate * Assumes signals are blocked so putmsg() will not block
2370Sstevel@tonic-gate * indefinitely
2380Sstevel@tonic-gate */
2390Sstevel@tonic-gate if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) {
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate * Assumes signals are blocked so putmsg() will not block
2420Sstevel@tonic-gate * indefinitely
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate if (putmsg(fd, &ctlbuf,
2450Sstevel@tonic-gate (struct strbuf *)(call->udata.len? &call->udata: NULL), 0) <
2460Sstevel@tonic-gate 0) {
2470Sstevel@tonic-gate if (errno == EAGAIN)
2480Sstevel@tonic-gate t_errno = TFLOW;
2490Sstevel@tonic-gate else
2500Sstevel@tonic-gate t_errno = TSYSERR;
2510Sstevel@tonic-gate goto err_out;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate } else {
2540Sstevel@tonic-gate strfdinsert.ctlbuf.maxlen = ctlbuf.maxlen;
2550Sstevel@tonic-gate strfdinsert.ctlbuf.len = ctlbuf.len;
2560Sstevel@tonic-gate strfdinsert.ctlbuf.buf = ctlbuf.buf;
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate strfdinsert.databuf.maxlen = call->udata.maxlen;
2590Sstevel@tonic-gate strfdinsert.databuf.len =
2600Sstevel@tonic-gate (call->udata.len? call->udata.len: -1);
2610Sstevel@tonic-gate strfdinsert.databuf.buf = call->udata.buf;
2620Sstevel@tonic-gate strfdinsert.fildes = resfd;
2630Sstevel@tonic-gate strfdinsert.offset = (int)sizeof (t_scalar_t);
2640Sstevel@tonic-gate strfdinsert.flags = 0; /* could be EXPEDITED also */
2650Sstevel@tonic-gate
266*1219Sraf if (ioctl(fd, I_FDINSERT, &strfdinsert) < 0) {
2670Sstevel@tonic-gate if (errno == EAGAIN)
2680Sstevel@tonic-gate t_errno = TFLOW;
2690Sstevel@tonic-gate else
2700Sstevel@tonic-gate t_errno = TSYSERR;
2710Sstevel@tonic-gate goto err_out;
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate if (_t_is_ok(fd, tiptr, conn_res_prim) < 0) {
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate * At the TPI level, the error returned in a T_ERROR_ACK
2780Sstevel@tonic-gate * received in response to a T_CONN_RES for a listener and
2790Sstevel@tonic-gate * acceptor endpoints not being the same kind of endpoints
2800Sstevel@tonic-gate * has changed to a new t_errno code introduced with
2810Sstevel@tonic-gate * XTI (TPROVMISMATCH). We need to adjust TLI error code
2820Sstevel@tonic-gate * to be same as before.
2830Sstevel@tonic-gate */
2840Sstevel@tonic-gate if (_T_IS_TLI(api_semantics) && t_errno == TPROVMISMATCH) {
2850Sstevel@tonic-gate /* TLI only */
2860Sstevel@tonic-gate t_errno = TBADF;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate goto err_out;
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate if (tiptr->ti_ocnt == 1) {
2920Sstevel@tonic-gate if (fd == resfd) {
2930Sstevel@tonic-gate _T_TX_NEXTSTATE(T_ACCEPT1, tiptr,
2940Sstevel@tonic-gate "t_accept: invalid state event T_ACCEPT1");
2950Sstevel@tonic-gate } else {
2960Sstevel@tonic-gate _T_TX_NEXTSTATE(T_ACCEPT2, tiptr,
2970Sstevel@tonic-gate "t_accept: invalid state event T_ACCEPT2");
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate * XXX Here we lock the resfd lock also. This
3000Sstevel@tonic-gate * is an instance of holding two locks without
3010Sstevel@tonic-gate * any enforcement of a locking hiararchy.
3020Sstevel@tonic-gate * There is potential for deadlock in incorrect
3030Sstevel@tonic-gate * or buggy programs here but this is the safer
3040Sstevel@tonic-gate * choice in this case. Correct programs will not
3050Sstevel@tonic-gate * deadlock.
3060Sstevel@tonic-gate */
3070Sstevel@tonic-gate sig_mutex_lock(&restiptr->ti_lock);
3080Sstevel@tonic-gate _T_TX_NEXTSTATE(T_PASSCON, restiptr,
3090Sstevel@tonic-gate "t_accept: invalid state event T_PASSCON");
3100Sstevel@tonic-gate sig_mutex_unlock(&restiptr->ti_lock);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate } else {
3130Sstevel@tonic-gate _T_TX_NEXTSTATE(T_ACCEPT3, tiptr,
3140Sstevel@tonic-gate "t_accept: invalid state event T_ACCEPT3");
3150Sstevel@tonic-gate if (fd != resfd)
3160Sstevel@tonic-gate sig_mutex_lock(&restiptr->ti_lock);
3170Sstevel@tonic-gate _T_TX_NEXTSTATE(T_PASSCON, restiptr,
3180Sstevel@tonic-gate "t_accept: invalid state event T_PASSCON");
3190Sstevel@tonic-gate if (fd != resfd)
3200Sstevel@tonic-gate sig_mutex_unlock(&restiptr->ti_lock);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate tiptr->ti_ocnt--;
3240Sstevel@tonic-gate tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * Update attributes which may have been negotiated during
3280Sstevel@tonic-gate * connection establishment for protocols where we suspect
3290Sstevel@tonic-gate * such negotiation is likely (e.g. OSI). We do not do it for
3300Sstevel@tonic-gate * all endpoints for performance reasons. Also, this code is
3310Sstevel@tonic-gate * deliberately done after user level state changes so even
3320Sstevel@tonic-gate * the (unlikely) failure case reflects a connected endpoint.
3330Sstevel@tonic-gate */
3340Sstevel@tonic-gate if (restiptr->ti_tsdusize != 0) {
3350Sstevel@tonic-gate if (_t_do_postconn_sync(resfd, restiptr) < 0)
3360Sstevel@tonic-gate goto err_out;
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate if (didalloc)
3400Sstevel@tonic-gate free(ctlbuf.buf);
3410Sstevel@tonic-gate else
3420Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
3430Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
3440Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
3450Sstevel@tonic-gate return (0);
3460Sstevel@tonic-gate /* NOTREACHED */
3470Sstevel@tonic-gate err_out:
3480Sstevel@tonic-gate sv_errno = errno;
3490Sstevel@tonic-gate if (didalloc)
3500Sstevel@tonic-gate free(ctlbuf.buf);
3510Sstevel@tonic-gate else
3520Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
3530Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
3540Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
3550Sstevel@tonic-gate errno = sv_errno;
3560Sstevel@tonic-gate return (-1);
3570Sstevel@tonic-gate }
358