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 */
22*132Srobinson
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*132Srobinson * Copyright 2005 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.10 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "mt.h"
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <errno.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 "tx.h"
460Sstevel@tonic-gate
470Sstevel@tonic-gate int
_tx_rcvdis(int fd,struct t_discon * discon,int api_semantics)480Sstevel@tonic-gate _tx_rcvdis(int fd, struct t_discon *discon, int api_semantics)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate struct strbuf ctlbuf;
510Sstevel@tonic-gate struct strbuf databuf;
520Sstevel@tonic-gate int retval;
530Sstevel@tonic-gate union T_primitives *pptr;
540Sstevel@tonic-gate struct _ti_user *tiptr;
550Sstevel@tonic-gate int sv_errno;
560Sstevel@tonic-gate int flg = 0;
570Sstevel@tonic-gate int didalloc, didralloc;
580Sstevel@tonic-gate int use_lookbufs = 0;
590Sstevel@tonic-gate
60*132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
610Sstevel@tonic-gate return (-1);
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Acquire per thread lock.
650Sstevel@tonic-gate * Note: Lock is held across most of this routine
660Sstevel@tonic-gate * including the blocking getmsg() call. This is fine
670Sstevel@tonic-gate * because it is first verfied that an event is pending
680Sstevel@tonic-gate */
690Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
700Sstevel@tonic-gate
710Sstevel@tonic-gate if (tiptr->ti_servtype == T_CLTS) {
720Sstevel@tonic-gate t_errno = TNOTSUPPORT;
730Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
740Sstevel@tonic-gate return (-1);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate * User level state verification only done for XTI
800Sstevel@tonic-gate * because doing for TLI may break existing applications
810Sstevel@tonic-gate */
82*132Srobinson if (!(tiptr->ti_state == T_DATAXFER ||
830Sstevel@tonic-gate tiptr->ti_state == T_OUTCON ||
840Sstevel@tonic-gate tiptr->ti_state == T_OUTREL ||
850Sstevel@tonic-gate tiptr->ti_state == T_INREL ||
860Sstevel@tonic-gate (tiptr->ti_state == T_INCON && tiptr->ti_ocnt > 0))) {
870Sstevel@tonic-gate t_errno = TOUTSTATE;
880Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
890Sstevel@tonic-gate return (-1);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate }
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate * Handle likely scenario as special case:
940Sstevel@tonic-gate * Is there a discon in look buffer as the first
950Sstevel@tonic-gate * event in the lookbuffer, is so just get it.
960Sstevel@tonic-gate */
970Sstevel@tonic-gate if ((tiptr->ti_lookcnt > 0) &&
98*132Srobinson /* LINTED pointer cast */
990Sstevel@tonic-gate (*((t_scalar_t *)tiptr->ti_lookbufs.tl_lookcbuf) == T_DISCON_IND)) {
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * The T_DISCON_IND is already in the look buffer
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate ctlbuf.len = tiptr->ti_lookbufs.tl_lookclen;
1040Sstevel@tonic-gate ctlbuf.buf = tiptr->ti_lookbufs.tl_lookcbuf;
1050Sstevel@tonic-gate /* Note: ctlbuf.maxlen not used in this case */
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate databuf.len = tiptr->ti_lookbufs.tl_lookdlen;
1080Sstevel@tonic-gate databuf.buf = tiptr->ti_lookbufs.tl_lookdbuf;
1090Sstevel@tonic-gate /* Note databuf.maxlen not used in this case */
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate use_lookbufs = 1;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate } else {
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate if ((retval = _t_look_locked(fd, tiptr, 0,
1160Sstevel@tonic-gate api_semantics)) < 0) {
1170Sstevel@tonic-gate sv_errno = errno;
1180Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1190Sstevel@tonic-gate errno = sv_errno;
1200Sstevel@tonic-gate return (-1);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (retval != T_DISCONNECT) {
1240Sstevel@tonic-gate t_errno = TNODIS;
1250Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1260Sstevel@tonic-gate return (-1);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate * get disconnect off read queue.
1310Sstevel@tonic-gate * use ctl and rcv buffers
1320Sstevel@tonic-gate *
1330Sstevel@tonic-gate * Acquire ctlbuf for use in sending/receiving control part
1340Sstevel@tonic-gate * of the message.
1350Sstevel@tonic-gate */
1360Sstevel@tonic-gate if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
1370Sstevel@tonic-gate sv_errno = errno;
1380Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1390Sstevel@tonic-gate errno = sv_errno;
1400Sstevel@tonic-gate return (-1);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate * Acquire databuf for use in sending/receiving data part
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0) {
1470Sstevel@tonic-gate sv_errno = errno;
1480Sstevel@tonic-gate if (didalloc)
1490Sstevel@tonic-gate free(ctlbuf.buf);
1500Sstevel@tonic-gate else
1510Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
1520Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1530Sstevel@tonic-gate errno = sv_errno;
1540Sstevel@tonic-gate return (-1);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * Since we already verified that a disconnect event
1590Sstevel@tonic-gate * is present, we assume that this getmsg() cannot
1600Sstevel@tonic-gate * block indefinitely
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate do {
1630Sstevel@tonic-gate retval = getmsg(fd, &ctlbuf, &databuf, &flg);
1640Sstevel@tonic-gate } while (retval < 0 && errno == EINTR);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if (retval < 0) {
1670Sstevel@tonic-gate t_errno = TSYSERR;
1680Sstevel@tonic-gate goto err_out;
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate if (databuf.len == -1) databuf.len = 0;
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * did I get entire message?
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate if (retval > 0) {
1760Sstevel@tonic-gate t_errno = TSYSERR;
1770Sstevel@tonic-gate errno = EIO;
1780Sstevel@tonic-gate goto err_out;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate
183*132Srobinson /* LINTED pointer cast */
1840Sstevel@tonic-gate pptr = (union T_primitives *)ctlbuf.buf;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if ((ctlbuf.len < (int)sizeof (struct T_discon_ind)) ||
1870Sstevel@tonic-gate (pptr->type != T_DISCON_IND)) {
1880Sstevel@tonic-gate t_errno = TSYSERR;
1890Sstevel@tonic-gate errno = EPROTO;
1900Sstevel@tonic-gate goto err_out;
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * clear more and expedited flags
1950Sstevel@tonic-gate */
1960Sstevel@tonic-gate tiptr->ti_flags &= ~(MORE | EXPEDITED);
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if (tiptr->ti_ocnt <= 0) {
1990Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCVDIS1, tiptr,
2000Sstevel@tonic-gate "t_rcvdis: invalid state event T_RCVDIS1");
2010Sstevel@tonic-gate } else {
2020Sstevel@tonic-gate if (tiptr->ti_ocnt == 1) {
2030Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCVDIS2, tiptr,
2040Sstevel@tonic-gate "t_rcvdis: invalid state event T_RCVDIS2");
2050Sstevel@tonic-gate } else {
2060Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCVDIS3, tiptr,
2070Sstevel@tonic-gate "t_rcvdis: invalid state event T_RCVDIS3");
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate tiptr->ti_ocnt--;
2100Sstevel@tonic-gate tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if (discon != NULL) {
2140Sstevel@tonic-gate if (_T_IS_TLI(api_semantics) || discon->udata.maxlen > 0) {
2150Sstevel@tonic-gate if (databuf.len > (int)discon->udata.maxlen) {
2160Sstevel@tonic-gate t_errno = TBUFOVFLW;
2170Sstevel@tonic-gate goto err_out;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate (void) memcpy(discon->udata.buf, databuf.buf,
2200Sstevel@tonic-gate (size_t)databuf.len);
2210Sstevel@tonic-gate discon->udata.len = databuf.len;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate discon->reason = pptr->discon_ind.DISCON_reason;
2240Sstevel@tonic-gate discon->sequence = pptr->discon_ind.SEQ_number;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate if (use_lookbufs)
2270Sstevel@tonic-gate _t_free_looklist_head(tiptr);
2280Sstevel@tonic-gate else {
2290Sstevel@tonic-gate if (didalloc)
2300Sstevel@tonic-gate free(ctlbuf.buf);
2310Sstevel@tonic-gate else
2320Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
2330Sstevel@tonic-gate if (didralloc)
2340Sstevel@tonic-gate free(databuf.buf);
2350Sstevel@tonic-gate else
2360Sstevel@tonic-gate tiptr->ti_rcvbuf = databuf.buf;
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2390Sstevel@tonic-gate return (0);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate err_out:
2420Sstevel@tonic-gate sv_errno = errno;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate if (use_lookbufs)
2450Sstevel@tonic-gate _t_free_looklist_head(tiptr);
2460Sstevel@tonic-gate else {
2470Sstevel@tonic-gate if (didalloc)
2480Sstevel@tonic-gate free(ctlbuf.buf);
2490Sstevel@tonic-gate else
2500Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
2510Sstevel@tonic-gate if (didralloc)
2520Sstevel@tonic-gate free(databuf.buf);
2530Sstevel@tonic-gate else
2540Sstevel@tonic-gate tiptr->ti_rcvbuf = databuf.buf;
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2570Sstevel@tonic-gate errno = sv_errno;
2580Sstevel@tonic-gate return (-1);
2590Sstevel@tonic-gate }
260