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_sndudata.c and t_sndvudata.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 <stdlib.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <stropts.h>
390Sstevel@tonic-gate #include <sys/stream.h>
400Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
410Sstevel@tonic-gate #include <sys/tihdr.h>
420Sstevel@tonic-gate #include <sys/timod.h>
430Sstevel@tonic-gate #include <xti.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_sndvudata(int fd,const struct t_unitdata * unitdata,struct t_iovec * tiov,unsigned int tiovcount,int api_semantics)490Sstevel@tonic-gate _tx_sndvudata(int fd, const struct t_unitdata *unitdata, struct t_iovec *tiov,
500Sstevel@tonic-gate unsigned int tiovcount, int api_semantics)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate struct T_unitdata_req *udreq;
530Sstevel@tonic-gate struct strbuf ctlbuf;
540Sstevel@tonic-gate struct strbuf databuf;
550Sstevel@tonic-gate int size;
560Sstevel@tonic-gate struct _ti_user *tiptr;
570Sstevel@tonic-gate int sv_errno;
580Sstevel@tonic-gate int didalloc;
590Sstevel@tonic-gate char *dataptr;
600Sstevel@tonic-gate unsigned int nbytes;
610Sstevel@tonic-gate
620Sstevel@tonic-gate assert(api_semantics == TX_XTI_XNS5_API);
63*132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
640Sstevel@tonic-gate return (-1);
650Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
660Sstevel@tonic-gate
670Sstevel@tonic-gate if (tiptr->ti_servtype != T_CLTS) {
680Sstevel@tonic-gate t_errno = TNOTSUPPORT;
690Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
700Sstevel@tonic-gate return (-1);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate if (tiovcount == 0 || tiovcount > T_IOV_MAX) {
740Sstevel@tonic-gate t_errno = TBADDATA;
750Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
760Sstevel@tonic-gate return (-1);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate if (tiptr->ti_state != T_IDLE) {
800Sstevel@tonic-gate t_errno = TOUTSTATE;
810Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
820Sstevel@tonic-gate return (-1);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate nbytes = _t_bytecount_upto_intmax(tiov, tiovcount);
860Sstevel@tonic-gate
870Sstevel@tonic-gate if ((nbytes == 0) &&
880Sstevel@tonic-gate !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
890Sstevel@tonic-gate t_errno = TBADDATA;
900Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
910Sstevel@tonic-gate return (-1);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate if ((tiptr->ti_maxpsz > 0) && (nbytes > (uint32_t)tiptr->ti_maxpsz)) {
950Sstevel@tonic-gate t_errno = TBADDATA;
960Sstevel@tonic-gate sv_errno = errno;
970Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
980Sstevel@tonic-gate errno = sv_errno;
990Sstevel@tonic-gate return (-1);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * Acquire ctlbuf for use in sending/receiving control part
1040Sstevel@tonic-gate * of the message.
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
1070Sstevel@tonic-gate sv_errno = errno;
1080Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1090Sstevel@tonic-gate errno = sv_errno;
1100Sstevel@tonic-gate return (-1);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
113*132Srobinson /* LINTED pointer cast */
1140Sstevel@tonic-gate udreq = (struct T_unitdata_req *)ctlbuf.buf;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate udreq->PRIM_type = T_UNITDATA_REQ;
1170Sstevel@tonic-gate udreq->DEST_length = unitdata->addr.len;
1180Sstevel@tonic-gate udreq->DEST_offset = 0;
1190Sstevel@tonic-gate udreq->OPT_length = unitdata->opt.len;
1200Sstevel@tonic-gate udreq->OPT_offset = 0;
1210Sstevel@tonic-gate size = (int)sizeof (struct T_unitdata_req);
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (unitdata->addr.len) {
1240Sstevel@tonic-gate if (_t_aligned_copy(&ctlbuf, unitdata->addr.len, size,
1250Sstevel@tonic-gate unitdata->addr.buf, &udreq->DEST_offset) < 0) {
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate * Aligned copy based will overflow buffer
1280Sstevel@tonic-gate * allocated based on maximum transport address
1290Sstevel@tonic-gate * size information
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate t_errno = TSYSERR;
1320Sstevel@tonic-gate errno = EPROTO;
1330Sstevel@tonic-gate goto err_out;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate size = udreq->DEST_offset + udreq->DEST_length;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate if (unitdata->opt.len) {
1380Sstevel@tonic-gate if (_t_aligned_copy(&ctlbuf, unitdata->opt.len, size,
1390Sstevel@tonic-gate unitdata->opt.buf, &udreq->OPT_offset) < 0) {
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * Aligned copy based will overflow buffer
1420Sstevel@tonic-gate * allocated based on maximum transport option
1430Sstevel@tonic-gate * size information
1440Sstevel@tonic-gate */
1450Sstevel@tonic-gate t_errno = TSYSERR;
1460Sstevel@tonic-gate errno = EPROTO;
1470Sstevel@tonic-gate goto err_out;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate size = udreq->OPT_offset + udreq->OPT_length;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (size > (int)ctlbuf.maxlen) {
1530Sstevel@tonic-gate t_errno = TSYSERR;
1540Sstevel@tonic-gate errno = EIO;
1550Sstevel@tonic-gate goto err_out;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate ctlbuf.len = size;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate dataptr = NULL;
1610Sstevel@tonic-gate if (nbytes != 0) {
1620Sstevel@tonic-gate if ((dataptr = malloc((size_t)nbytes)) == NULL) {
1630Sstevel@tonic-gate t_errno = TSYSERR;
1640Sstevel@tonic-gate goto err_out;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate _t_gather(dataptr, tiov, tiovcount);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate databuf.buf = dataptr;
1690Sstevel@tonic-gate databuf.len = nbytes;
1700Sstevel@tonic-gate databuf.maxlen = nbytes;
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate * Calls to send data (write or putmsg) can potentially
1730Sstevel@tonic-gate * block, for MT case, we drop the lock and enable signals here
1740Sstevel@tonic-gate * and acquire it back
1750Sstevel@tonic-gate */
1760Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1770Sstevel@tonic-gate if (putmsg(fd, &ctlbuf, &databuf, 0) < 0) {
1780Sstevel@tonic-gate if (errno == EAGAIN)
1790Sstevel@tonic-gate t_errno = TFLOW;
1800Sstevel@tonic-gate else
1810Sstevel@tonic-gate t_errno = TSYSERR;
1820Sstevel@tonic-gate sv_errno = errno;
1830Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1840Sstevel@tonic-gate errno = sv_errno;
1850Sstevel@tonic-gate goto err_out;
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate _T_TX_NEXTSTATE(T_SNDUDATA, tiptr,
1900Sstevel@tonic-gate "t_sndvudata: invalid state event T_SNDUDATA");
1910Sstevel@tonic-gate if (didalloc)
1920Sstevel@tonic-gate free(ctlbuf.buf);
1930Sstevel@tonic-gate else
1940Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
1950Sstevel@tonic-gate if (dataptr != NULL)
1960Sstevel@tonic-gate free(dataptr);
1970Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1980Sstevel@tonic-gate return (0);
1990Sstevel@tonic-gate err_out:
2000Sstevel@tonic-gate sv_errno = errno;
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 if (dataptr != NULL)
2060Sstevel@tonic-gate free(dataptr);
2070Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2080Sstevel@tonic-gate errno = sv_errno;
2090Sstevel@tonic-gate return (-1);
2100Sstevel@tonic-gate }
211