xref: /illumos-gate/usr/src/lib/libnsl/nsl/_conn_util.c (revision 1da57d55)
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 #include "mt.h"
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <stropts.h>
387c478bd9Sstevel@tonic-gate #include <sys/stream.h>
397c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
407c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
417c478bd9Sstevel@tonic-gate #include <sys/timod.h>
427c478bd9Sstevel@tonic-gate #include <xti.h>
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate #include <assert.h>
457c478bd9Sstevel@tonic-gate #include "tx.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Snd_conn_req - send connect request message to transport provider.
507c478bd9Sstevel@tonic-gate  * All signals for the caller are blocked during the call to simplify design.
517c478bd9Sstevel@tonic-gate  * (This is OK for a bounded amount of time this routine is expected to
527c478bd9Sstevel@tonic-gate  * execute).  Also, assumes tiptr->ti_lock is held.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate int
_t_snd_conn_req(struct _ti_user * tiptr,const struct t_call * call,struct strbuf * ctlbufp)557c478bd9Sstevel@tonic-gate _t_snd_conn_req(
567c478bd9Sstevel@tonic-gate 	struct _ti_user *tiptr,
577c478bd9Sstevel@tonic-gate 	const struct t_call *call,
587c478bd9Sstevel@tonic-gate 	struct strbuf *ctlbufp)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	struct T_conn_req *creq;
61*61961e0fSrobinson 	int size;
627c478bd9Sstevel@tonic-gate 	int fd;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&tiptr->ti_lock));
657c478bd9Sstevel@tonic-gate 	fd = tiptr->ti_fd;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	if (tiptr->ti_servtype == T_CLTS) {
687c478bd9Sstevel@tonic-gate 		t_errno = TNOTSUPPORT;
697c478bd9Sstevel@tonic-gate 		return (-1);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
72*61961e0fSrobinson 	if (_t_is_event(fd, tiptr) < 0)
737c478bd9Sstevel@tonic-gate 		return (-1);
747c478bd9Sstevel@tonic-gate 
75*61961e0fSrobinson 	/* LINTED pointer cast */
767c478bd9Sstevel@tonic-gate 	creq = (struct T_conn_req *)ctlbufp->buf;
777c478bd9Sstevel@tonic-gate 	creq->PRIM_type = T_CONN_REQ;
787c478bd9Sstevel@tonic-gate 	creq->DEST_length = call->addr.len;
797c478bd9Sstevel@tonic-gate 	creq->DEST_offset = 0;
807c478bd9Sstevel@tonic-gate 	creq->OPT_length = call->opt.len;
817c478bd9Sstevel@tonic-gate 	creq->OPT_offset = 0;
827c478bd9Sstevel@tonic-gate 	size = (int)sizeof (struct T_conn_req); /* size without any buffers */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (call->addr.len) {
857c478bd9Sstevel@tonic-gate 		if (_t_aligned_copy(ctlbufp, call->addr.len, size,
867c478bd9Sstevel@tonic-gate 		    call->addr.buf, &creq->DEST_offset) < 0) {
877c478bd9Sstevel@tonic-gate 			/*
887c478bd9Sstevel@tonic-gate 			 * Aligned copy will overflow buffer allocated based
897c478bd9Sstevel@tonic-gate 			 * based on transport maximum address size.
907c478bd9Sstevel@tonic-gate 			 * return error.
917c478bd9Sstevel@tonic-gate 			 */
927c478bd9Sstevel@tonic-gate 			t_errno = TBADADDR;
937c478bd9Sstevel@tonic-gate 			return (-1);
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 		size = creq->DEST_offset + creq->DEST_length;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	if (call->opt.len) {
987c478bd9Sstevel@tonic-gate 		if (_t_aligned_copy(ctlbufp, call->opt.len, size,
997c478bd9Sstevel@tonic-gate 		    call->opt.buf, &creq->OPT_offset) < 0) {
1007c478bd9Sstevel@tonic-gate 			/*
1017c478bd9Sstevel@tonic-gate 			 * Aligned copy will overflow buffer allocated based
1027c478bd9Sstevel@tonic-gate 			 * on maximum option size in transport.
1037c478bd9Sstevel@tonic-gate 			 * return error.
1047c478bd9Sstevel@tonic-gate 			 */
1057c478bd9Sstevel@tonic-gate 			t_errno = TBADOPT;
1067c478bd9Sstevel@tonic-gate 			return (-1);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 		size = creq->OPT_offset + creq->OPT_length;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	if (call->udata.len) {
1117c478bd9Sstevel@tonic-gate 		if ((tiptr->ti_cdatasize == T_INVALID /* -2 */) ||
1127c478bd9Sstevel@tonic-gate 		    ((tiptr->ti_cdatasize != T_INFINITE /* -1 */) &&
1137c478bd9Sstevel@tonic-gate 			(call->udata.len > (uint32_t)tiptr->ti_cdatasize))) {
1147c478bd9Sstevel@tonic-gate 			/*
1157c478bd9Sstevel@tonic-gate 			 * user data not valid with connect or it
1167c478bd9Sstevel@tonic-gate 			 * exceeds the limits specified by the transport
1177c478bd9Sstevel@tonic-gate 			 * provider.
1187c478bd9Sstevel@tonic-gate 			 */
1197c478bd9Sstevel@tonic-gate 			t_errno = TBADDATA;
1207c478bd9Sstevel@tonic-gate 			return (-1);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	ctlbufp->len = size;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/*
1277c478bd9Sstevel@tonic-gate 	 * Assumes signals are blocked so putmsg() will not block
1287c478bd9Sstevel@tonic-gate 	 * indefinitely
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	if (putmsg(fd, ctlbufp,
1317c478bd9Sstevel@tonic-gate 	    (struct strbuf *)(call->udata.len? &call->udata: NULL), 0) < 0) {
1327c478bd9Sstevel@tonic-gate 		t_errno = TSYSERR;
1337c478bd9Sstevel@tonic-gate 		return (-1);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
136*61961e0fSrobinson 	if (_t_is_ok(fd, tiptr, T_CONN_REQ) < 0)
1377c478bd9Sstevel@tonic-gate 		return (-1);
1387c478bd9Sstevel@tonic-gate 	return (0);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * Rcv_conn_con - get connection confirmation off
1457c478bd9Sstevel@tonic-gate  * of read queue
1467c478bd9Sstevel@tonic-gate  * Note:
1477c478bd9Sstevel@tonic-gate  *      - called holding the tiptr->ti_lock
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate int
_t_rcv_conn_con(struct _ti_user * tiptr,struct t_call * call,struct strbuf * ctlbufp,int api_semantics)1507c478bd9Sstevel@tonic-gate _t_rcv_conn_con(
1517c478bd9Sstevel@tonic-gate 	struct _ti_user *tiptr,
1527c478bd9Sstevel@tonic-gate 	struct t_call *call,
1537c478bd9Sstevel@tonic-gate 	struct strbuf *ctlbufp,
1547c478bd9Sstevel@tonic-gate 	int api_semantics)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	struct strbuf databuf;
1577c478bd9Sstevel@tonic-gate 	union T_primitives *pptr;
1587c478bd9Sstevel@tonic-gate 	int retval, fd, sv_errno;
1597c478bd9Sstevel@tonic-gate 	int didralloc;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	int flg = 0;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	fd = tiptr->ti_fd;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (tiptr->ti_servtype == T_CLTS) {
1667c478bd9Sstevel@tonic-gate 		t_errno = TNOTSUPPORT;
1677c478bd9Sstevel@tonic-gate 		return (-1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * see if there is something in look buffer
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	if (tiptr->ti_lookcnt > 0) {
1747c478bd9Sstevel@tonic-gate 		t_errno = TLOOK;
1757c478bd9Sstevel@tonic-gate 		return (-1);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	ctlbufp->len = 0;
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * Acquire databuf for use in sending/receiving data part
1817c478bd9Sstevel@tonic-gate 	 */
182*61961e0fSrobinson 	if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0)
1837c478bd9Sstevel@tonic-gate 		return (-1);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/*
1867c478bd9Sstevel@tonic-gate 	 * This is a call that may block indefinitely so we drop the
1877c478bd9Sstevel@tonic-gate 	 * lock and allow signals in MT case here and reacquire it.
1887c478bd9Sstevel@tonic-gate 	 * Error case should roll back state changes done above
1897c478bd9Sstevel@tonic-gate 	 * (happens to be no state change here)
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
1927c478bd9Sstevel@tonic-gate 	if ((retval = getmsg(fd, ctlbufp, &databuf, &flg)) < 0) {
1937c478bd9Sstevel@tonic-gate 		sv_errno = errno;
1947c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN)
1957c478bd9Sstevel@tonic-gate 			t_errno = TNODATA;
1967c478bd9Sstevel@tonic-gate 		else
1977c478bd9Sstevel@tonic-gate 			t_errno = TSYSERR;
1987c478bd9Sstevel@tonic-gate 		sig_mutex_lock(&tiptr->ti_lock);
1997c478bd9Sstevel@tonic-gate 		errno = sv_errno;
2007c478bd9Sstevel@tonic-gate 		goto err_out;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (databuf.len == -1) databuf.len = 0;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * did we get entire message
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	if (retval > 0) {
2107c478bd9Sstevel@tonic-gate 		t_errno = TSYSERR;
2117c478bd9Sstevel@tonic-gate 		errno = EIO;
2127c478bd9Sstevel@tonic-gate 		goto err_out;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * is cntl part large enough to determine message type?
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	if (ctlbufp->len < (int)sizeof (t_scalar_t)) {
2197c478bd9Sstevel@tonic-gate 		t_errno = TSYSERR;
2207c478bd9Sstevel@tonic-gate 		errno = EPROTO;
2217c478bd9Sstevel@tonic-gate 		goto err_out;
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
224*61961e0fSrobinson 	/* LINTED pointer cast */
2257c478bd9Sstevel@tonic-gate 	pptr = (union T_primitives *)ctlbufp->buf;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	switch (pptr->type) {
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	case T_CONN_CON:
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		if ((ctlbufp->len < (int)sizeof (struct T_conn_con)) ||
2327c478bd9Sstevel@tonic-gate 		    (pptr->conn_con.OPT_length != 0 &&
2337c478bd9Sstevel@tonic-gate 		    (ctlbufp->len < (int)(pptr->conn_con.OPT_length +
2347c478bd9Sstevel@tonic-gate 		    pptr->conn_con.OPT_offset)))) {
2357c478bd9Sstevel@tonic-gate 			t_errno = TSYSERR;
2367c478bd9Sstevel@tonic-gate 			errno = EPROTO;
2377c478bd9Sstevel@tonic-gate 			goto err_out;
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		if (call != NULL) {
2417c478bd9Sstevel@tonic-gate 			/*
2427c478bd9Sstevel@tonic-gate 			 * Note: Buffer overflow is an error in XTI
2437c478bd9Sstevel@tonic-gate 			 * only if netbuf.maxlen > 0
2447c478bd9Sstevel@tonic-gate 			 */
2457c478bd9Sstevel@tonic-gate 			if (_T_IS_TLI(api_semantics) || call->addr.maxlen > 0) {
2467c478bd9Sstevel@tonic-gate 				if (TLEN_GT_NLEN(pptr->conn_con.RES_length,
2477c478bd9Sstevel@tonic-gate 				    call->addr.maxlen)) {
2487c478bd9Sstevel@tonic-gate 					t_errno = TBUFOVFLW;
2497c478bd9Sstevel@tonic-gate 					goto err_out;
2507c478bd9Sstevel@tonic-gate 				}
2517c478bd9Sstevel@tonic-gate 				(void) memcpy(call->addr.buf,
2527c478bd9Sstevel@tonic-gate 				    ctlbufp->buf + pptr->conn_con.RES_offset,
2537c478bd9Sstevel@tonic-gate 				    (size_t)pptr->conn_con.RES_length);
2547c478bd9Sstevel@tonic-gate 				call->addr.len = pptr->conn_con.RES_length;
2557c478bd9Sstevel@tonic-gate 			}
2567c478bd9Sstevel@tonic-gate 			if (_T_IS_TLI(api_semantics) || call->opt.maxlen > 0) {
2577c478bd9Sstevel@tonic-gate 				if (TLEN_GT_NLEN(pptr->conn_con.OPT_length,
2587c478bd9Sstevel@tonic-gate 				    call->opt.maxlen)) {
2597c478bd9Sstevel@tonic-gate 					t_errno = TBUFOVFLW;
2607c478bd9Sstevel@tonic-gate 					goto err_out;
2617c478bd9Sstevel@tonic-gate 				}
2627c478bd9Sstevel@tonic-gate 				(void) memcpy(call->opt.buf,
2637c478bd9Sstevel@tonic-gate 				    ctlbufp->buf + pptr->conn_con.OPT_offset,
2647c478bd9Sstevel@tonic-gate 				    (size_t)pptr->conn_con.OPT_length);
2657c478bd9Sstevel@tonic-gate 				call->opt.len = pptr->conn_con.OPT_length;
2667c478bd9Sstevel@tonic-gate 			}
2677c478bd9Sstevel@tonic-gate 			if (_T_IS_TLI(api_semantics) ||
2687c478bd9Sstevel@tonic-gate 			    call->udata.maxlen > 0) {
2697c478bd9Sstevel@tonic-gate 				if (databuf.len > (int)call->udata.maxlen) {
2707c478bd9Sstevel@tonic-gate 					t_errno = TBUFOVFLW;
2717c478bd9Sstevel@tonic-gate 					goto err_out;
2727c478bd9Sstevel@tonic-gate 				}
2737c478bd9Sstevel@tonic-gate 				(void) memcpy(call->udata.buf, databuf.buf,
2747c478bd9Sstevel@tonic-gate 				    (size_t)databuf.len);
2757c478bd9Sstevel@tonic-gate 				call->udata.len = databuf.len;
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 			/*
2787c478bd9Sstevel@tonic-gate 			 * since a confirmation seq number
2797c478bd9Sstevel@tonic-gate 			 * is -1 by default
2807c478bd9Sstevel@tonic-gate 			 */
2817c478bd9Sstevel@tonic-gate 			call->sequence = (int)-1;
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 		if (didralloc)
2847c478bd9Sstevel@tonic-gate 			free(databuf.buf);
2857c478bd9Sstevel@tonic-gate 		else
2867c478bd9Sstevel@tonic-gate 			tiptr->ti_rcvbuf = databuf.buf;
2877c478bd9Sstevel@tonic-gate 		return (0);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	case T_DISCON_IND:
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 		/*
2927c478bd9Sstevel@tonic-gate 		 * if disconnect indication then append it to
2937c478bd9Sstevel@tonic-gate 		 * the "look bufffer" list.
2947c478bd9Sstevel@tonic-gate 		 * This may result in MT case for the process
2957c478bd9Sstevel@tonic-gate 		 * signal mask to be temporarily masked to
2967c478bd9Sstevel@tonic-gate 		 * ensure safe memory allocation.
2977c478bd9Sstevel@tonic-gate 		 */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		if (_t_register_lookevent(tiptr, databuf.buf, databuf.len,
3007c478bd9Sstevel@tonic-gate 					ctlbufp->buf, ctlbufp->len) < 0) {
3017c478bd9Sstevel@tonic-gate 			t_errno = TSYSERR;
3027c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
3037c478bd9Sstevel@tonic-gate 			goto err_out;
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 		t_errno = TLOOK;
3067c478bd9Sstevel@tonic-gate 		goto err_out;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	default:
3097c478bd9Sstevel@tonic-gate 		break;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	t_errno = TSYSERR;
3137c478bd9Sstevel@tonic-gate 	errno = EPROTO;
3147c478bd9Sstevel@tonic-gate err_out:
3157c478bd9Sstevel@tonic-gate 	if (didralloc)
3167c478bd9Sstevel@tonic-gate 		free(databuf.buf);
3177c478bd9Sstevel@tonic-gate 	else
3187c478bd9Sstevel@tonic-gate 		tiptr->ti_rcvbuf = databuf.buf;
3197c478bd9Sstevel@tonic-gate 	return (-1);
3207c478bd9Sstevel@tonic-gate }
321