17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
22*61961e0fSrobinson
237c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
247c478bd9Sstevel@tonic-gate /* All Rights Reserved */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
27*61961e0fSrobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
287c478bd9Sstevel@tonic-gate * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include "mt.h"
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <stropts.h>
407c478bd9Sstevel@tonic-gate #include <sys/stream.h>
417c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
427c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
437c478bd9Sstevel@tonic-gate #include <sys/timod.h>
447c478bd9Sstevel@tonic-gate #include <xti.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <assert.h>
477c478bd9Sstevel@tonic-gate #include "tx.h"
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * Snd_conn_req - send connect request message to transport provider.
527c478bd9Sstevel@tonic-gate * All signals for the caller are blocked during the call to simplify design.
537c478bd9Sstevel@tonic-gate * (This is OK for a bounded amount of time this routine is expected to
547c478bd9Sstevel@tonic-gate * execute). Also, assumes tiptr->ti_lock is held.
557c478bd9Sstevel@tonic-gate */
567c478bd9Sstevel@tonic-gate int
_t_snd_conn_req(struct _ti_user * tiptr,const struct t_call * call,struct strbuf * ctlbufp)577c478bd9Sstevel@tonic-gate _t_snd_conn_req(
587c478bd9Sstevel@tonic-gate struct _ti_user *tiptr,
597c478bd9Sstevel@tonic-gate const struct t_call *call,
607c478bd9Sstevel@tonic-gate struct strbuf *ctlbufp)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate struct T_conn_req *creq;
63*61961e0fSrobinson int size;
647c478bd9Sstevel@tonic-gate int fd;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate assert(MUTEX_HELD(&tiptr->ti_lock));
677c478bd9Sstevel@tonic-gate fd = tiptr->ti_fd;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate if (tiptr->ti_servtype == T_CLTS) {
707c478bd9Sstevel@tonic-gate t_errno = TNOTSUPPORT;
717c478bd9Sstevel@tonic-gate return (-1);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate
74*61961e0fSrobinson if (_t_is_event(fd, tiptr) < 0)
757c478bd9Sstevel@tonic-gate return (-1);
767c478bd9Sstevel@tonic-gate
77*61961e0fSrobinson /* LINTED pointer cast */
787c478bd9Sstevel@tonic-gate creq = (struct T_conn_req *)ctlbufp->buf;
797c478bd9Sstevel@tonic-gate creq->PRIM_type = T_CONN_REQ;
807c478bd9Sstevel@tonic-gate creq->DEST_length = call->addr.len;
817c478bd9Sstevel@tonic-gate creq->DEST_offset = 0;
827c478bd9Sstevel@tonic-gate creq->OPT_length = call->opt.len;
837c478bd9Sstevel@tonic-gate creq->OPT_offset = 0;
847c478bd9Sstevel@tonic-gate size = (int)sizeof (struct T_conn_req); /* size without any buffers */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate if (call->addr.len) {
877c478bd9Sstevel@tonic-gate if (_t_aligned_copy(ctlbufp, call->addr.len, size,
887c478bd9Sstevel@tonic-gate call->addr.buf, &creq->DEST_offset) < 0) {
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * Aligned copy will overflow buffer allocated based
917c478bd9Sstevel@tonic-gate * based on transport maximum address size.
927c478bd9Sstevel@tonic-gate * return error.
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate t_errno = TBADADDR;
957c478bd9Sstevel@tonic-gate return (-1);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate size = creq->DEST_offset + creq->DEST_length;
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate if (call->opt.len) {
1007c478bd9Sstevel@tonic-gate if (_t_aligned_copy(ctlbufp, call->opt.len, size,
1017c478bd9Sstevel@tonic-gate call->opt.buf, &creq->OPT_offset) < 0) {
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * Aligned copy will overflow buffer allocated based
1047c478bd9Sstevel@tonic-gate * on maximum option size in transport.
1057c478bd9Sstevel@tonic-gate * return error.
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate t_errno = TBADOPT;
1087c478bd9Sstevel@tonic-gate return (-1);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate size = creq->OPT_offset + creq->OPT_length;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate if (call->udata.len) {
1137c478bd9Sstevel@tonic-gate if ((tiptr->ti_cdatasize == T_INVALID /* -2 */) ||
1147c478bd9Sstevel@tonic-gate ((tiptr->ti_cdatasize != T_INFINITE /* -1 */) &&
1157c478bd9Sstevel@tonic-gate (call->udata.len > (uint32_t)tiptr->ti_cdatasize))) {
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * user data not valid with connect or it
1187c478bd9Sstevel@tonic-gate * exceeds the limits specified by the transport
1197c478bd9Sstevel@tonic-gate * provider.
1207c478bd9Sstevel@tonic-gate */
1217c478bd9Sstevel@tonic-gate t_errno = TBADDATA;
1227c478bd9Sstevel@tonic-gate return (-1);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate ctlbufp->len = size;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * Assumes signals are blocked so putmsg() will not block
1307c478bd9Sstevel@tonic-gate * indefinitely
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate if (putmsg(fd, ctlbufp,
1337c478bd9Sstevel@tonic-gate (struct strbuf *)(call->udata.len? &call->udata: NULL), 0) < 0) {
1347c478bd9Sstevel@tonic-gate t_errno = TSYSERR;
1357c478bd9Sstevel@tonic-gate return (-1);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
138*61961e0fSrobinson if (_t_is_ok(fd, tiptr, T_CONN_REQ) < 0)
1397c478bd9Sstevel@tonic-gate return (-1);
1407c478bd9Sstevel@tonic-gate return (0);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate * Rcv_conn_con - get connection confirmation off
1477c478bd9Sstevel@tonic-gate * of read queue
1487c478bd9Sstevel@tonic-gate * Note:
1497c478bd9Sstevel@tonic-gate * - called holding the tiptr->ti_lock
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate int
_t_rcv_conn_con(struct _ti_user * tiptr,struct t_call * call,struct strbuf * ctlbufp,int api_semantics)1527c478bd9Sstevel@tonic-gate _t_rcv_conn_con(
1537c478bd9Sstevel@tonic-gate struct _ti_user *tiptr,
1547c478bd9Sstevel@tonic-gate struct t_call *call,
1557c478bd9Sstevel@tonic-gate struct strbuf *ctlbufp,
1567c478bd9Sstevel@tonic-gate int api_semantics)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate struct strbuf databuf;
1597c478bd9Sstevel@tonic-gate union T_primitives *pptr;
1607c478bd9Sstevel@tonic-gate int retval, fd, sv_errno;
1617c478bd9Sstevel@tonic-gate int didralloc;
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate int flg = 0;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate fd = tiptr->ti_fd;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if (tiptr->ti_servtype == T_CLTS) {
1687c478bd9Sstevel@tonic-gate t_errno = TNOTSUPPORT;
1697c478bd9Sstevel@tonic-gate return (-1);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate * see if there is something in look buffer
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate if (tiptr->ti_lookcnt > 0) {
1767c478bd9Sstevel@tonic-gate t_errno = TLOOK;
1777c478bd9Sstevel@tonic-gate return (-1);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate ctlbufp->len = 0;
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate * Acquire databuf for use in sending/receiving data part
1837c478bd9Sstevel@tonic-gate */
184*61961e0fSrobinson if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0)
1857c478bd9Sstevel@tonic-gate return (-1);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * This is a call that may block indefinitely so we drop the
1897c478bd9Sstevel@tonic-gate * lock and allow signals in MT case here and reacquire it.
1907c478bd9Sstevel@tonic-gate * Error case should roll back state changes done above
1917c478bd9Sstevel@tonic-gate * (happens to be no state change here)
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1947c478bd9Sstevel@tonic-gate if ((retval = getmsg(fd, ctlbufp, &databuf, &flg)) < 0) {
1957c478bd9Sstevel@tonic-gate sv_errno = errno;
1967c478bd9Sstevel@tonic-gate if (errno == EAGAIN)
1977c478bd9Sstevel@tonic-gate t_errno = TNODATA;
1987c478bd9Sstevel@tonic-gate else
1997c478bd9Sstevel@tonic-gate t_errno = TSYSERR;
2007c478bd9Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
2017c478bd9Sstevel@tonic-gate errno = sv_errno;
2027c478bd9Sstevel@tonic-gate goto err_out;
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate if (databuf.len == -1) databuf.len = 0;
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate * did we get entire message
2107c478bd9Sstevel@tonic-gate */
2117c478bd9Sstevel@tonic-gate if (retval > 0) {
2127c478bd9Sstevel@tonic-gate t_errno = TSYSERR;
2137c478bd9Sstevel@tonic-gate errno = EIO;
2147c478bd9Sstevel@tonic-gate goto err_out;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * is cntl part large enough to determine message type?
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate if (ctlbufp->len < (int)sizeof (t_scalar_t)) {
2217c478bd9Sstevel@tonic-gate t_errno = TSYSERR;
2227c478bd9Sstevel@tonic-gate errno = EPROTO;
2237c478bd9Sstevel@tonic-gate goto err_out;
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
226*61961e0fSrobinson /* LINTED pointer cast */
2277c478bd9Sstevel@tonic-gate pptr = (union T_primitives *)ctlbufp->buf;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate switch (pptr->type) {
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate case T_CONN_CON:
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate if ((ctlbufp->len < (int)sizeof (struct T_conn_con)) ||
2347c478bd9Sstevel@tonic-gate (pptr->conn_con.OPT_length != 0 &&
2357c478bd9Sstevel@tonic-gate (ctlbufp->len < (int)(pptr->conn_con.OPT_length +
2367c478bd9Sstevel@tonic-gate pptr->conn_con.OPT_offset)))) {
2377c478bd9Sstevel@tonic-gate t_errno = TSYSERR;
2387c478bd9Sstevel@tonic-gate errno = EPROTO;
2397c478bd9Sstevel@tonic-gate goto err_out;
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate if (call != NULL) {
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate * Note: Buffer overflow is an error in XTI
2457c478bd9Sstevel@tonic-gate * only if netbuf.maxlen > 0
2467c478bd9Sstevel@tonic-gate */
2477c478bd9Sstevel@tonic-gate if (_T_IS_TLI(api_semantics) || call->addr.maxlen > 0) {
2487c478bd9Sstevel@tonic-gate if (TLEN_GT_NLEN(pptr->conn_con.RES_length,
2497c478bd9Sstevel@tonic-gate call->addr.maxlen)) {
2507c478bd9Sstevel@tonic-gate t_errno = TBUFOVFLW;
2517c478bd9Sstevel@tonic-gate goto err_out;
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate (void) memcpy(call->addr.buf,
2547c478bd9Sstevel@tonic-gate ctlbufp->buf + pptr->conn_con.RES_offset,
2557c478bd9Sstevel@tonic-gate (size_t)pptr->conn_con.RES_length);
2567c478bd9Sstevel@tonic-gate call->addr.len = pptr->conn_con.RES_length;
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate if (_T_IS_TLI(api_semantics) || call->opt.maxlen > 0) {
2597c478bd9Sstevel@tonic-gate if (TLEN_GT_NLEN(pptr->conn_con.OPT_length,
2607c478bd9Sstevel@tonic-gate call->opt.maxlen)) {
2617c478bd9Sstevel@tonic-gate t_errno = TBUFOVFLW;
2627c478bd9Sstevel@tonic-gate goto err_out;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate (void) memcpy(call->opt.buf,
2657c478bd9Sstevel@tonic-gate ctlbufp->buf + pptr->conn_con.OPT_offset,
2667c478bd9Sstevel@tonic-gate (size_t)pptr->conn_con.OPT_length);
2677c478bd9Sstevel@tonic-gate call->opt.len = pptr->conn_con.OPT_length;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate if (_T_IS_TLI(api_semantics) ||
2707c478bd9Sstevel@tonic-gate call->udata.maxlen > 0) {
2717c478bd9Sstevel@tonic-gate if (databuf.len > (int)call->udata.maxlen) {
2727c478bd9Sstevel@tonic-gate t_errno = TBUFOVFLW;
2737c478bd9Sstevel@tonic-gate goto err_out;
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate (void) memcpy(call->udata.buf, databuf.buf,
2767c478bd9Sstevel@tonic-gate (size_t)databuf.len);
2777c478bd9Sstevel@tonic-gate call->udata.len = databuf.len;
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate * since a confirmation seq number
2817c478bd9Sstevel@tonic-gate * is -1 by default
2827c478bd9Sstevel@tonic-gate */
2837c478bd9Sstevel@tonic-gate call->sequence = (int)-1;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate if (didralloc)
2867c478bd9Sstevel@tonic-gate free(databuf.buf);
2877c478bd9Sstevel@tonic-gate else
2887c478bd9Sstevel@tonic-gate tiptr->ti_rcvbuf = databuf.buf;
2897c478bd9Sstevel@tonic-gate return (0);
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate case T_DISCON_IND:
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate * if disconnect indication then append it to
2957c478bd9Sstevel@tonic-gate * the "look bufffer" list.
2967c478bd9Sstevel@tonic-gate * This may result in MT case for the process
2977c478bd9Sstevel@tonic-gate * signal mask to be temporarily masked to
2987c478bd9Sstevel@tonic-gate * ensure safe memory allocation.
2997c478bd9Sstevel@tonic-gate */
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate if (_t_register_lookevent(tiptr, databuf.buf, databuf.len,
3027c478bd9Sstevel@tonic-gate ctlbufp->buf, ctlbufp->len) < 0) {
3037c478bd9Sstevel@tonic-gate t_errno = TSYSERR;
3047c478bd9Sstevel@tonic-gate errno = ENOMEM;
3057c478bd9Sstevel@tonic-gate goto err_out;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate t_errno = TLOOK;
3087c478bd9Sstevel@tonic-gate goto err_out;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate default:
3117c478bd9Sstevel@tonic-gate break;
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate t_errno = TSYSERR;
3157c478bd9Sstevel@tonic-gate errno = EPROTO;
3167c478bd9Sstevel@tonic-gate err_out:
3177c478bd9Sstevel@tonic-gate if (didralloc)
3187c478bd9Sstevel@tonic-gate free(databuf.buf);
3197c478bd9Sstevel@tonic-gate else
3207c478bd9Sstevel@tonic-gate tiptr->ti_rcvbuf = databuf.buf;
3217c478bd9Sstevel@tonic-gate return (-1);
3227c478bd9Sstevel@tonic-gate }
323