xref: /onnv-gate/usr/src/lib/libnsl/nsl/_conn_util.c (revision 132:e3f7eaf7dde4)
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"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "mt.h"
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <stdlib.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 <signal.h>
460Sstevel@tonic-gate #include <assert.h>
470Sstevel@tonic-gate #include "tx.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Snd_conn_req - send connect request message to transport provider.
520Sstevel@tonic-gate  * All signals for the caller are blocked during the call to simplify design.
530Sstevel@tonic-gate  * (This is OK for a bounded amount of time this routine is expected to
540Sstevel@tonic-gate  * execute).  Also, assumes tiptr->ti_lock is held.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate int
_t_snd_conn_req(struct _ti_user * tiptr,const struct t_call * call,struct strbuf * ctlbufp)570Sstevel@tonic-gate _t_snd_conn_req(
580Sstevel@tonic-gate 	struct _ti_user *tiptr,
590Sstevel@tonic-gate 	const struct t_call *call,
600Sstevel@tonic-gate 	struct strbuf *ctlbufp)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	struct T_conn_req *creq;
63*132Srobinson 	int size;
640Sstevel@tonic-gate 	int fd;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	assert(MUTEX_HELD(&tiptr->ti_lock));
670Sstevel@tonic-gate 	fd = tiptr->ti_fd;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (tiptr->ti_servtype == T_CLTS) {
700Sstevel@tonic-gate 		t_errno = TNOTSUPPORT;
710Sstevel@tonic-gate 		return (-1);
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 
74*132Srobinson 	if (_t_is_event(fd, tiptr) < 0)
750Sstevel@tonic-gate 		return (-1);
760Sstevel@tonic-gate 
77*132Srobinson 	/* LINTED pointer cast */
780Sstevel@tonic-gate 	creq = (struct T_conn_req *)ctlbufp->buf;
790Sstevel@tonic-gate 	creq->PRIM_type = T_CONN_REQ;
800Sstevel@tonic-gate 	creq->DEST_length = call->addr.len;
810Sstevel@tonic-gate 	creq->DEST_offset = 0;
820Sstevel@tonic-gate 	creq->OPT_length = call->opt.len;
830Sstevel@tonic-gate 	creq->OPT_offset = 0;
840Sstevel@tonic-gate 	size = (int)sizeof (struct T_conn_req); /* size without any buffers */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (call->addr.len) {
870Sstevel@tonic-gate 		if (_t_aligned_copy(ctlbufp, call->addr.len, size,
880Sstevel@tonic-gate 		    call->addr.buf, &creq->DEST_offset) < 0) {
890Sstevel@tonic-gate 			/*
900Sstevel@tonic-gate 			 * Aligned copy will overflow buffer allocated based
910Sstevel@tonic-gate 			 * based on transport maximum address size.
920Sstevel@tonic-gate 			 * return error.
930Sstevel@tonic-gate 			 */
940Sstevel@tonic-gate 			t_errno = TBADADDR;
950Sstevel@tonic-gate 			return (-1);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 		size = creq->DEST_offset + creq->DEST_length;
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 	if (call->opt.len) {
1000Sstevel@tonic-gate 		if (_t_aligned_copy(ctlbufp, call->opt.len, size,
1010Sstevel@tonic-gate 		    call->opt.buf, &creq->OPT_offset) < 0) {
1020Sstevel@tonic-gate 			/*
1030Sstevel@tonic-gate 			 * Aligned copy will overflow buffer allocated based
1040Sstevel@tonic-gate 			 * on maximum option size in transport.
1050Sstevel@tonic-gate 			 * return error.
1060Sstevel@tonic-gate 			 */
1070Sstevel@tonic-gate 			t_errno = TBADOPT;
1080Sstevel@tonic-gate 			return (-1);
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 		size = creq->OPT_offset + creq->OPT_length;
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 	if (call->udata.len) {
1130Sstevel@tonic-gate 		if ((tiptr->ti_cdatasize == T_INVALID /* -2 */) ||
1140Sstevel@tonic-gate 		    ((tiptr->ti_cdatasize != T_INFINITE /* -1 */) &&
1150Sstevel@tonic-gate 			(call->udata.len > (uint32_t)tiptr->ti_cdatasize))) {
1160Sstevel@tonic-gate 			/*
1170Sstevel@tonic-gate 			 * user data not valid with connect or it
1180Sstevel@tonic-gate 			 * exceeds the limits specified by the transport
1190Sstevel@tonic-gate 			 * provider.
1200Sstevel@tonic-gate 			 */
1210Sstevel@tonic-gate 			t_errno = TBADDATA;
1220Sstevel@tonic-gate 			return (-1);
1230Sstevel@tonic-gate 		}
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	ctlbufp->len = size;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/*
1290Sstevel@tonic-gate 	 * Assumes signals are blocked so putmsg() will not block
1300Sstevel@tonic-gate 	 * indefinitely
1310Sstevel@tonic-gate 	 */
1320Sstevel@tonic-gate 	if (putmsg(fd, ctlbufp,
1330Sstevel@tonic-gate 	    (struct strbuf *)(call->udata.len? &call->udata: NULL), 0) < 0) {
1340Sstevel@tonic-gate 		t_errno = TSYSERR;
1350Sstevel@tonic-gate 		return (-1);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
138*132Srobinson 	if (_t_is_ok(fd, tiptr, T_CONN_REQ) < 0)
1390Sstevel@tonic-gate 		return (-1);
1400Sstevel@tonic-gate 	return (0);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * Rcv_conn_con - get connection confirmation off
1470Sstevel@tonic-gate  * of read queue
1480Sstevel@tonic-gate  * Note:
1490Sstevel@tonic-gate  *      - called holding the tiptr->ti_lock
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate int
_t_rcv_conn_con(struct _ti_user * tiptr,struct t_call * call,struct strbuf * ctlbufp,int api_semantics)1520Sstevel@tonic-gate _t_rcv_conn_con(
1530Sstevel@tonic-gate 	struct _ti_user *tiptr,
1540Sstevel@tonic-gate 	struct t_call *call,
1550Sstevel@tonic-gate 	struct strbuf *ctlbufp,
1560Sstevel@tonic-gate 	int api_semantics)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	struct strbuf databuf;
1590Sstevel@tonic-gate 	union T_primitives *pptr;
1600Sstevel@tonic-gate 	int retval, fd, sv_errno;
1610Sstevel@tonic-gate 	int didralloc;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	int flg = 0;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	fd = tiptr->ti_fd;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (tiptr->ti_servtype == T_CLTS) {
1680Sstevel@tonic-gate 		t_errno = TNOTSUPPORT;
1690Sstevel@tonic-gate 		return (-1);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 * see if there is something in look buffer
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	if (tiptr->ti_lookcnt > 0) {
1760Sstevel@tonic-gate 		t_errno = TLOOK;
1770Sstevel@tonic-gate 		return (-1);
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	ctlbufp->len = 0;
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Acquire databuf for use in sending/receiving data part
1830Sstevel@tonic-gate 	 */
184*132Srobinson 	if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0)
1850Sstevel@tonic-gate 		return (-1);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/*
1880Sstevel@tonic-gate 	 * This is a call that may block indefinitely so we drop the
1890Sstevel@tonic-gate 	 * lock and allow signals in MT case here and reacquire it.
1900Sstevel@tonic-gate 	 * Error case should roll back state changes done above
1910Sstevel@tonic-gate 	 * (happens to be no state change here)
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
1940Sstevel@tonic-gate 	if ((retval = getmsg(fd, ctlbufp, &databuf, &flg)) < 0) {
1950Sstevel@tonic-gate 		sv_errno = errno;
1960Sstevel@tonic-gate 		if (errno == EAGAIN)
1970Sstevel@tonic-gate 			t_errno = TNODATA;
1980Sstevel@tonic-gate 		else
1990Sstevel@tonic-gate 			t_errno = TSYSERR;
2000Sstevel@tonic-gate 		sig_mutex_lock(&tiptr->ti_lock);
2010Sstevel@tonic-gate 		errno = sv_errno;
2020Sstevel@tonic-gate 		goto err_out;
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (databuf.len == -1) databuf.len = 0;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * did we get entire message
2100Sstevel@tonic-gate 	 */
2110Sstevel@tonic-gate 	if (retval > 0) {
2120Sstevel@tonic-gate 		t_errno = TSYSERR;
2130Sstevel@tonic-gate 		errno = EIO;
2140Sstevel@tonic-gate 		goto err_out;
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * is cntl part large enough to determine message type?
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	if (ctlbufp->len < (int)sizeof (t_scalar_t)) {
2210Sstevel@tonic-gate 		t_errno = TSYSERR;
2220Sstevel@tonic-gate 		errno = EPROTO;
2230Sstevel@tonic-gate 		goto err_out;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
226*132Srobinson 	/* LINTED pointer cast */
2270Sstevel@tonic-gate 	pptr = (union T_primitives *)ctlbufp->buf;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	switch (pptr->type) {
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	case T_CONN_CON:
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		if ((ctlbufp->len < (int)sizeof (struct T_conn_con)) ||
2340Sstevel@tonic-gate 		    (pptr->conn_con.OPT_length != 0 &&
2350Sstevel@tonic-gate 		    (ctlbufp->len < (int)(pptr->conn_con.OPT_length +
2360Sstevel@tonic-gate 		    pptr->conn_con.OPT_offset)))) {
2370Sstevel@tonic-gate 			t_errno = TSYSERR;
2380Sstevel@tonic-gate 			errno = EPROTO;
2390Sstevel@tonic-gate 			goto err_out;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		if (call != NULL) {
2430Sstevel@tonic-gate 			/*
2440Sstevel@tonic-gate 			 * Note: Buffer overflow is an error in XTI
2450Sstevel@tonic-gate 			 * only if netbuf.maxlen > 0
2460Sstevel@tonic-gate 			 */
2470Sstevel@tonic-gate 			if (_T_IS_TLI(api_semantics) || call->addr.maxlen > 0) {
2480Sstevel@tonic-gate 				if (TLEN_GT_NLEN(pptr->conn_con.RES_length,
2490Sstevel@tonic-gate 				    call->addr.maxlen)) {
2500Sstevel@tonic-gate 					t_errno = TBUFOVFLW;
2510Sstevel@tonic-gate 					goto err_out;
2520Sstevel@tonic-gate 				}
2530Sstevel@tonic-gate 				(void) memcpy(call->addr.buf,
2540Sstevel@tonic-gate 				    ctlbufp->buf + pptr->conn_con.RES_offset,
2550Sstevel@tonic-gate 				    (size_t)pptr->conn_con.RES_length);
2560Sstevel@tonic-gate 				call->addr.len = pptr->conn_con.RES_length;
2570Sstevel@tonic-gate 			}
2580Sstevel@tonic-gate 			if (_T_IS_TLI(api_semantics) || call->opt.maxlen > 0) {
2590Sstevel@tonic-gate 				if (TLEN_GT_NLEN(pptr->conn_con.OPT_length,
2600Sstevel@tonic-gate 				    call->opt.maxlen)) {
2610Sstevel@tonic-gate 					t_errno = TBUFOVFLW;
2620Sstevel@tonic-gate 					goto err_out;
2630Sstevel@tonic-gate 				}
2640Sstevel@tonic-gate 				(void) memcpy(call->opt.buf,
2650Sstevel@tonic-gate 				    ctlbufp->buf + pptr->conn_con.OPT_offset,
2660Sstevel@tonic-gate 				    (size_t)pptr->conn_con.OPT_length);
2670Sstevel@tonic-gate 				call->opt.len = pptr->conn_con.OPT_length;
2680Sstevel@tonic-gate 			}
2690Sstevel@tonic-gate 			if (_T_IS_TLI(api_semantics) ||
2700Sstevel@tonic-gate 			    call->udata.maxlen > 0) {
2710Sstevel@tonic-gate 				if (databuf.len > (int)call->udata.maxlen) {
2720Sstevel@tonic-gate 					t_errno = TBUFOVFLW;
2730Sstevel@tonic-gate 					goto err_out;
2740Sstevel@tonic-gate 				}
2750Sstevel@tonic-gate 				(void) memcpy(call->udata.buf, databuf.buf,
2760Sstevel@tonic-gate 				    (size_t)databuf.len);
2770Sstevel@tonic-gate 				call->udata.len = databuf.len;
2780Sstevel@tonic-gate 			}
2790Sstevel@tonic-gate 			/*
2800Sstevel@tonic-gate 			 * since a confirmation seq number
2810Sstevel@tonic-gate 			 * is -1 by default
2820Sstevel@tonic-gate 			 */
2830Sstevel@tonic-gate 			call->sequence = (int)-1;
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 		if (didralloc)
2860Sstevel@tonic-gate 			free(databuf.buf);
2870Sstevel@tonic-gate 		else
2880Sstevel@tonic-gate 			tiptr->ti_rcvbuf = databuf.buf;
2890Sstevel@tonic-gate 		return (0);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	case T_DISCON_IND:
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		/*
2940Sstevel@tonic-gate 		 * if disconnect indication then append it to
2950Sstevel@tonic-gate 		 * the "look bufffer" list.
2960Sstevel@tonic-gate 		 * This may result in MT case for the process
2970Sstevel@tonic-gate 		 * signal mask to be temporarily masked to
2980Sstevel@tonic-gate 		 * ensure safe memory allocation.
2990Sstevel@tonic-gate 		 */
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		if (_t_register_lookevent(tiptr, databuf.buf, databuf.len,
3020Sstevel@tonic-gate 					ctlbufp->buf, ctlbufp->len) < 0) {
3030Sstevel@tonic-gate 			t_errno = TSYSERR;
3040Sstevel@tonic-gate 			errno = ENOMEM;
3050Sstevel@tonic-gate 			goto err_out;
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 		t_errno = TLOOK;
3080Sstevel@tonic-gate 		goto err_out;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	default:
3110Sstevel@tonic-gate 		break;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	t_errno = TSYSERR;
3150Sstevel@tonic-gate 	errno = EPROTO;
3160Sstevel@tonic-gate err_out:
3170Sstevel@tonic-gate 	if (didralloc)
3180Sstevel@tonic-gate 		free(databuf.buf);
3190Sstevel@tonic-gate 	else
3200Sstevel@tonic-gate 		tiptr->ti_rcvbuf = databuf.buf;
3210Sstevel@tonic-gate 	return (-1);
3220Sstevel@tonic-gate }
323