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.5 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * t_rcvudata.c and t_rcvvudata.c are very similar and contain common code.
350Sstevel@tonic-gate * Any changes to either of them should be reviewed to see whether they
360Sstevel@tonic-gate * are applicable to the other file.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate #include "mt.h"
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <stropts.h>
430Sstevel@tonic-gate #include <sys/stream.h>
440Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
450Sstevel@tonic-gate #include <sys/tihdr.h>
460Sstevel@tonic-gate #include <sys/timod.h>
470Sstevel@tonic-gate #include <xti.h>
480Sstevel@tonic-gate #include <syslog.h>
490Sstevel@tonic-gate #include "tx.h"
500Sstevel@tonic-gate
510Sstevel@tonic-gate
520Sstevel@tonic-gate int
_tx_rcvudata(int fd,struct t_unitdata * unitdata,int * flags,int api_semantics)530Sstevel@tonic-gate _tx_rcvudata(
540Sstevel@tonic-gate int fd,
550Sstevel@tonic-gate struct t_unitdata *unitdata,
560Sstevel@tonic-gate int *flags,
570Sstevel@tonic-gate int api_semantics
580Sstevel@tonic-gate )
590Sstevel@tonic-gate {
600Sstevel@tonic-gate struct strbuf ctlbuf;
610Sstevel@tonic-gate int retval;
620Sstevel@tonic-gate union T_primitives *pptr;
630Sstevel@tonic-gate struct _ti_user *tiptr;
640Sstevel@tonic-gate int sv_errno;
650Sstevel@tonic-gate int didalloc;
660Sstevel@tonic-gate int flg = 0;
670Sstevel@tonic-gate
68*132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
690Sstevel@tonic-gate return (-1);
700Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
710Sstevel@tonic-gate
720Sstevel@tonic-gate if (tiptr->ti_servtype != T_CLTS) {
730Sstevel@tonic-gate t_errno = TNOTSUPPORT;
740Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
750Sstevel@tonic-gate return (-1);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate
780Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate * User level state verification only done for XTI
810Sstevel@tonic-gate * because doing for TLI may break existing applications
820Sstevel@tonic-gate */
830Sstevel@tonic-gate if (tiptr->ti_state != T_IDLE) {
840Sstevel@tonic-gate t_errno = TOUTSTATE;
850Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
860Sstevel@tonic-gate return (-1);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * check if there is something in look buffer
930Sstevel@tonic-gate */
940Sstevel@tonic-gate if (tiptr->ti_lookcnt > 0) {
950Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
960Sstevel@tonic-gate t_errno = TLOOK;
970Sstevel@tonic-gate return (-1);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * Acquire ctlbuf for use in sending/receiving control part
1020Sstevel@tonic-gate * of the message.
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
1050Sstevel@tonic-gate sv_errno = errno;
1060Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1070Sstevel@tonic-gate errno = sv_errno;
1080Sstevel@tonic-gate return (-1);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate *flags = 0;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * This is a call that may block indefinitely so we drop the
1150Sstevel@tonic-gate * lock and allow signals in MT case here and reacquire it.
1160Sstevel@tonic-gate * Error case should roll back state changes done above
1170Sstevel@tonic-gate * (happens to be no state change here)
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1200Sstevel@tonic-gate if ((retval = getmsg(fd, &ctlbuf, (struct strbuf *)&unitdata->udata,
1210Sstevel@tonic-gate &flg)) < 0) {
1220Sstevel@tonic-gate if (errno == EAGAIN)
1230Sstevel@tonic-gate t_errno = TNODATA;
1240Sstevel@tonic-gate else
1250Sstevel@tonic-gate t_errno = TSYSERR;
1260Sstevel@tonic-gate sv_errno = errno;
1270Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1280Sstevel@tonic-gate errno = sv_errno;
1290Sstevel@tonic-gate goto err_out;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1320Sstevel@tonic-gate
133*132Srobinson if (((struct strbuf *)&unitdata->udata)->len == -1)
134*132Srobinson unitdata->udata.len = 0;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * is there control piece with data?
1380Sstevel@tonic-gate */
1390Sstevel@tonic-gate if (ctlbuf.len > 0) {
1400Sstevel@tonic-gate if (ctlbuf.len < (int)sizeof (t_scalar_t)) {
1410Sstevel@tonic-gate unitdata->udata.len = 0;
1420Sstevel@tonic-gate t_errno = TSYSERR;
1430Sstevel@tonic-gate errno = EPROTO;
1440Sstevel@tonic-gate goto err_out;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
147*132Srobinson /* LINTED pointer cast */
1480Sstevel@tonic-gate pptr = (union T_primitives *)ctlbuf.buf;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate switch (pptr->type) {
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate case T_UNITDATA_IND:
1530Sstevel@tonic-gate if ((ctlbuf.len <
1540Sstevel@tonic-gate (int)sizeof (struct T_unitdata_ind)) ||
1550Sstevel@tonic-gate (pptr->unitdata_ind.OPT_length &&
1560Sstevel@tonic-gate (ctlbuf.len < (int)(pptr->unitdata_ind.OPT_length
1570Sstevel@tonic-gate + pptr->unitdata_ind.OPT_offset)))) {
1580Sstevel@tonic-gate t_errno = TSYSERR;
1590Sstevel@tonic-gate unitdata->udata.len = 0;
1600Sstevel@tonic-gate errno = EPROTO;
1610Sstevel@tonic-gate goto err_out;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate if (_T_IS_TLI(api_semantics) ||
1650Sstevel@tonic-gate unitdata->addr.maxlen > 0) {
1660Sstevel@tonic-gate if (TLEN_GT_NLEN(pptr->unitdata_ind.SRC_length,
1670Sstevel@tonic-gate unitdata->addr.maxlen)) {
1680Sstevel@tonic-gate t_errno = TBUFOVFLW;
1690Sstevel@tonic-gate unitdata->udata.len = 0;
1700Sstevel@tonic-gate goto err_out;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate (void) memcpy(unitdata->addr.buf,
1730Sstevel@tonic-gate ctlbuf.buf + pptr->unitdata_ind.SRC_offset,
1740Sstevel@tonic-gate (size_t)pptr->unitdata_ind.SRC_length);
1750Sstevel@tonic-gate unitdata->addr.len =
1760Sstevel@tonic-gate pptr->unitdata_ind.SRC_length;
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate if (_T_IS_TLI(api_semantics) ||
1790Sstevel@tonic-gate unitdata->opt.maxlen > 0) {
1800Sstevel@tonic-gate if (TLEN_GT_NLEN(pptr->unitdata_ind.OPT_length,
1810Sstevel@tonic-gate unitdata->opt.maxlen)) {
1820Sstevel@tonic-gate t_errno = TBUFOVFLW;
1830Sstevel@tonic-gate unitdata->udata.len = 0;
1840Sstevel@tonic-gate goto err_out;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate (void) memcpy(unitdata->opt.buf, ctlbuf.buf +
1870Sstevel@tonic-gate pptr->unitdata_ind.OPT_offset,
1880Sstevel@tonic-gate (size_t)pptr->unitdata_ind.OPT_length);
1890Sstevel@tonic-gate unitdata->opt.len =
1900Sstevel@tonic-gate pptr->unitdata_ind.OPT_length;
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate if (retval & MOREDATA)
1930Sstevel@tonic-gate *flags |= T_MORE;
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate * No state changes happens on T_RCVUDATA
1960Sstevel@tonic-gate * event (NOOP). We do it only to log errors.
1970Sstevel@tonic-gate */
1980Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCVUDATA, tiptr,
1990Sstevel@tonic-gate "t_rcvudata: invalid state event T_RCVUDATA");
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (didalloc)
2020Sstevel@tonic-gate free(ctlbuf.buf);
2030Sstevel@tonic-gate else
2040Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2070Sstevel@tonic-gate return (0);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate case T_UDERROR_IND:
2100Sstevel@tonic-gate if (_t_register_lookevent(tiptr, 0, 0, ctlbuf.buf,
2110Sstevel@tonic-gate ctlbuf.len) < 0) {
2120Sstevel@tonic-gate t_errno = TSYSERR;
2130Sstevel@tonic-gate errno = ENOMEM;
2140Sstevel@tonic-gate goto err_out;
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate unitdata->udata.len = 0;
2170Sstevel@tonic-gate t_errno = TLOOK;
2180Sstevel@tonic-gate goto err_out;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate default:
2210Sstevel@tonic-gate break;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate t_errno = TSYSERR;
2250Sstevel@tonic-gate errno = EPROTO;
2260Sstevel@tonic-gate goto err_out;
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate } else { /* else part of "if (ctlbuf.len > 0)" */
2290Sstevel@tonic-gate unitdata->addr.len = 0;
2300Sstevel@tonic-gate unitdata->opt.len = 0;
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate * only data in message no control piece
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate if (retval & MOREDATA)
2350Sstevel@tonic-gate *flags = T_MORE;
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate * No state transition occurs on
2380Sstevel@tonic-gate * event T_RCVUDATA. We do it only to
2390Sstevel@tonic-gate * log errors.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCVUDATA, tiptr,
2420Sstevel@tonic-gate "t_rcvudata: invalid state event T_RCVUDATA");
2430Sstevel@tonic-gate if (didalloc)
2440Sstevel@tonic-gate free(ctlbuf.buf);
2450Sstevel@tonic-gate else
2460Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
2470Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2480Sstevel@tonic-gate return (0);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate /* NOTREACHED */
2510Sstevel@tonic-gate err_out:
2520Sstevel@tonic-gate sv_errno = errno;
2530Sstevel@tonic-gate if (didalloc)
2540Sstevel@tonic-gate free(ctlbuf.buf);
2550Sstevel@tonic-gate else
2560Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
2570Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2580Sstevel@tonic-gate errno = sv_errno;
2590Sstevel@tonic-gate return (-1);
2600Sstevel@tonic-gate }
261