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