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_sndudata.c and t_sndvudata.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 <stdlib.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <stropts.h>
420Sstevel@tonic-gate #include <sys/stream.h>
430Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
440Sstevel@tonic-gate #include <sys/tihdr.h>
450Sstevel@tonic-gate #include <sys/timod.h>
460Sstevel@tonic-gate #include <xti.h>
470Sstevel@tonic-gate #include <syslog.h>
480Sstevel@tonic-gate #include "tx.h"
490Sstevel@tonic-gate
500Sstevel@tonic-gate int
_tx_sndudata(int fd,const struct t_unitdata * unitdata,int api_semantics)510Sstevel@tonic-gate _tx_sndudata(int fd, const struct t_unitdata *unitdata, int api_semantics)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate struct T_unitdata_req *udreq;
540Sstevel@tonic-gate struct strbuf ctlbuf;
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
60*132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
610Sstevel@tonic-gate return (-1);
620Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
630Sstevel@tonic-gate
640Sstevel@tonic-gate if (tiptr->ti_servtype != T_CLTS) {
650Sstevel@tonic-gate t_errno = TNOTSUPPORT;
660Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
670Sstevel@tonic-gate return (-1);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
700Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * User level state verification only done for XTI
730Sstevel@tonic-gate * because doing for TLI may break existing applications
740Sstevel@tonic-gate */
750Sstevel@tonic-gate if (tiptr->ti_state != T_IDLE) {
760Sstevel@tonic-gate t_errno = TOUTSTATE;
770Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
780Sstevel@tonic-gate return (-1);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate }
810Sstevel@tonic-gate
820Sstevel@tonic-gate if (((int)unitdata->udata.len == 0) &&
830Sstevel@tonic-gate !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
840Sstevel@tonic-gate t_errno = TBADDATA;
850Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
860Sstevel@tonic-gate return (-1);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate if ((tiptr->ti_maxpsz > 0) &&
900Sstevel@tonic-gate (unitdata->udata.len > (uint32_t)tiptr->ti_maxpsz)) {
910Sstevel@tonic-gate if (_T_IS_TLI(api_semantics)) {
920Sstevel@tonic-gate t_errno = TSYSERR;
930Sstevel@tonic-gate errno = EPROTO;
940Sstevel@tonic-gate } else
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 /*
1610Sstevel@tonic-gate * Calls to send data (write or putmsg) can potentially
1620Sstevel@tonic-gate * block, for MT case, we drop the lock and enable signals here
1630Sstevel@tonic-gate * and acquire it back.
1640Sstevel@tonic-gate * At this point, we are sure SENDZERO is supported and the
1650Sstevel@tonic-gate * putmsg below may send a zero length message,
1660Sstevel@tonic-gate * (i.e with valid control part, but zero data part)
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1690Sstevel@tonic-gate if (putmsg(fd, &ctlbuf, (struct strbuf *)&unitdata->udata, 0) < 0) {
1700Sstevel@tonic-gate if (errno == EAGAIN)
1710Sstevel@tonic-gate t_errno = TFLOW;
1720Sstevel@tonic-gate else
1730Sstevel@tonic-gate t_errno = TSYSERR;
1740Sstevel@tonic-gate sv_errno = errno;
1750Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1760Sstevel@tonic-gate errno = sv_errno;
1770Sstevel@tonic-gate goto err_out;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate _T_TX_NEXTSTATE(T_SNDUDATA, tiptr,
1820Sstevel@tonic-gate "t_sndudata: invalid state event T_SNDUDATA");
1830Sstevel@tonic-gate if (didalloc)
1840Sstevel@tonic-gate free(ctlbuf.buf);
1850Sstevel@tonic-gate else
1860Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
1870Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1880Sstevel@tonic-gate return (0);
1890Sstevel@tonic-gate err_out:
1900Sstevel@tonic-gate sv_errno = errno;
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 sig_mutex_unlock(&tiptr->ti_lock);
1960Sstevel@tonic-gate errno = sv_errno;
1970Sstevel@tonic-gate return (-1);
1980Sstevel@tonic-gate }
199