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.4.3.1 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "mt.h"
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <stropts.h>
370Sstevel@tonic-gate #include <sys/stream.h>
380Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
390Sstevel@tonic-gate #include <sys/tihdr.h>
400Sstevel@tonic-gate #include <sys/timod.h>
410Sstevel@tonic-gate #include <xti.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate #include <syslog.h>
440Sstevel@tonic-gate #include "tx.h"
450Sstevel@tonic-gate
460Sstevel@tonic-gate int
_tx_snddis(int fd,const struct t_call * call,int api_semantics)470Sstevel@tonic-gate _tx_snddis(int fd, const struct t_call *call, int api_semantics)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate struct T_discon_req dreq;
500Sstevel@tonic-gate struct strbuf ctlbuf;
510Sstevel@tonic-gate struct strbuf databuf;
520Sstevel@tonic-gate struct _ti_user *tiptr;
530Sstevel@tonic-gate int sv_errno;
540Sstevel@tonic-gate int retval;
550Sstevel@tonic-gate
56132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
570Sstevel@tonic-gate return (-1);
580Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
590Sstevel@tonic-gate
600Sstevel@tonic-gate if (tiptr->ti_servtype == T_CLTS) {
610Sstevel@tonic-gate t_errno = TNOTSUPPORT;
620Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
630Sstevel@tonic-gate return (-1);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate
660Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * User level state verification only done for XTI
690Sstevel@tonic-gate * because doing for TLI may break existing applications
700Sstevel@tonic-gate * Note: This is documented in TLI man page but never
710Sstevel@tonic-gate * done.
720Sstevel@tonic-gate */
73132Srobinson if (!(tiptr->ti_state == T_DATAXFER ||
740Sstevel@tonic-gate tiptr->ti_state == T_OUTCON ||
750Sstevel@tonic-gate tiptr->ti_state == T_OUTREL ||
760Sstevel@tonic-gate tiptr->ti_state == T_INREL ||
770Sstevel@tonic-gate (tiptr->ti_state == T_INCON && tiptr->ti_ocnt > 0))) {
780Sstevel@tonic-gate t_errno = TOUTSTATE;
790Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
800Sstevel@tonic-gate return (-1);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate * Following check only done for XTI as it may be a risk
850Sstevel@tonic-gate * to existing buggy TLI applications.
860Sstevel@tonic-gate */
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate if (call != NULL && call->udata.len) {
900Sstevel@tonic-gate if ((tiptr->ti_ddatasize == T_INVALID /* -2 */) ||
910Sstevel@tonic-gate ((tiptr->ti_ddatasize != T_INFINITE /* -1*/) &&
920Sstevel@tonic-gate (call->udata.len >
930Sstevel@tonic-gate (uint32_t)tiptr->ti_ddatasize))) {
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate * user data not valid with disconnect or it
960Sstevel@tonic-gate * exceeds the limits specified by the
970Sstevel@tonic-gate * transport provider
980Sstevel@tonic-gate */
990Sstevel@tonic-gate t_errno = TBADDATA;
1000Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1010Sstevel@tonic-gate return (-1);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate * If disconnect is done on a listener, the 'call' parameter
1070Sstevel@tonic-gate * must be non-null
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate if ((tiptr->ti_state == T_INCON) &&
1100Sstevel@tonic-gate (call == NULL)) {
1110Sstevel@tonic-gate t_errno = TBADSEQ;
1120Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1130Sstevel@tonic-gate return (-1);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * look at look buffer to see if there is a discon there
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate if (_t_look_locked(fd, tiptr, 0, api_semantics) == T_DISCONNECT) {
1210Sstevel@tonic-gate t_errno = TLOOK;
1220Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1230Sstevel@tonic-gate return (-1);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if ((tiptr->ti_lookcnt > 0) && (call == 0))
1270Sstevel@tonic-gate _t_flush_lookevents(tiptr); /* flush but not on listener */
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate do {
130*1219Sraf retval = ioctl(fd, I_FLUSH, FLUSHW);
1310Sstevel@tonic-gate } while (retval < 0 && errno == EINTR);
1320Sstevel@tonic-gate if (retval < 0) {
1330Sstevel@tonic-gate sv_errno = errno;
1340Sstevel@tonic-gate t_errno = TSYSERR;
1350Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1360Sstevel@tonic-gate errno = sv_errno;
1370Sstevel@tonic-gate return (-1);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate ctlbuf.len = (int)sizeof (struct T_discon_req);
1410Sstevel@tonic-gate ctlbuf.maxlen = (int)sizeof (struct T_discon_req);
1420Sstevel@tonic-gate ctlbuf.buf = (char *)&dreq;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate dreq.PRIM_type = T_DISCON_REQ;
1450Sstevel@tonic-gate dreq.SEQ_number = (call? call->sequence: -1);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate databuf.maxlen = (call? call->udata.len: 0);
1480Sstevel@tonic-gate databuf.len = (call? call->udata.len: 0);
1490Sstevel@tonic-gate databuf.buf = (call? call->udata.buf: NULL);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate * Calls to send data (write or putmsg) can potentially
1530Sstevel@tonic-gate * block, for MT case, we drop the lock and enable signals here
1540Sstevel@tonic-gate * and acquire it back
1550Sstevel@tonic-gate */
1560Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1570Sstevel@tonic-gate if (putmsg(fd, &ctlbuf, (databuf.len? &databuf: NULL), 0) < 0) {
1580Sstevel@tonic-gate t_errno = TSYSERR;
1590Sstevel@tonic-gate return (-1);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate if (_t_is_ok(fd, tiptr, T_DISCON_REQ) < 0) {
1640Sstevel@tonic-gate sv_errno = errno;
1650Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1660Sstevel@tonic-gate errno = sv_errno;
1670Sstevel@tonic-gate return (-1);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate tiptr->ti_flags &= ~(MORE|EXPEDITED);
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate if (tiptr->ti_ocnt <= 1) {
1730Sstevel@tonic-gate if (tiptr->ti_state == T_INCON) {
1740Sstevel@tonic-gate tiptr->ti_ocnt--;
1750Sstevel@tonic-gate tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate _T_TX_NEXTSTATE(T_SNDDIS1, tiptr,
1780Sstevel@tonic-gate "t_snddis: invalid state event T_SNDDIS1");
1790Sstevel@tonic-gate } else {
1800Sstevel@tonic-gate if (tiptr->ti_state == T_INCON) {
1810Sstevel@tonic-gate tiptr->ti_ocnt--;
1820Sstevel@tonic-gate tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate _T_TX_NEXTSTATE(T_SNDDIS2, tiptr,
1850Sstevel@tonic-gate "t_snddis: invalid state event T_SNDDIS2");
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1890Sstevel@tonic-gate return (0);
1900Sstevel@tonic-gate }
191