xref: /onnv-gate/usr/src/lib/libnsl/nsl/t_bind.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"	/* SVr4.0 1.3.4.1 */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "mt.h"
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <stropts.h>
380Sstevel@tonic-gate #include <sys/stream.h>
390Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
400Sstevel@tonic-gate #include <sys/tihdr.h>
410Sstevel@tonic-gate #include <sys/timod.h>
420Sstevel@tonic-gate #include <xti.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <syslog.h>
450Sstevel@tonic-gate #include "tx.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate int
_tx_bind(int fd,const struct t_bind * req,struct t_bind * ret,int api_semantics)480Sstevel@tonic-gate _tx_bind(
490Sstevel@tonic-gate 	int fd,
500Sstevel@tonic-gate 	const struct t_bind *req,
510Sstevel@tonic-gate 	struct t_bind *ret,
520Sstevel@tonic-gate 	int api_semantics
530Sstevel@tonic-gate )
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	struct T_bind_req *bind_reqp;
560Sstevel@tonic-gate 	struct T_bind_ack *bind_ackp;
570Sstevel@tonic-gate 	int size, sv_errno, retlen;
580Sstevel@tonic-gate 	struct _ti_user *tiptr;
590Sstevel@tonic-gate 	sigset_t mask;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	int didalloc;
620Sstevel@tonic-gate 	int use_xpg41tpi;
630Sstevel@tonic-gate 	struct strbuf ctlbuf;
640Sstevel@tonic-gate 
65*132Srobinson 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
660Sstevel@tonic-gate 		return (-1);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/*
690Sstevel@tonic-gate 	 * We block all signals since TI_BIND, which sends a TPI message
700Sstevel@tonic-gate 	 * O_T_BIND_REQ down, is not an idempotetent operation
710Sstevel@tonic-gate 	 * Note that sig_mutex_lock() only defers signals, it does not
720Sstevel@tonic-gate 	 * block them, so interruptible syscalls could still get EINTR.
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
750Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
760Sstevel@tonic-gate 	if (_T_IS_XTI(api_semantics)) {
770Sstevel@tonic-gate 		/*
780Sstevel@tonic-gate 		 * User level state verification only done for XTI
790Sstevel@tonic-gate 		 * because doing for TLI may break existing applications
800Sstevel@tonic-gate 		 */
810Sstevel@tonic-gate 		if (tiptr->ti_state != T_UNBND) {
820Sstevel@tonic-gate 			t_errno = TOUTSTATE;
830Sstevel@tonic-gate 			sig_mutex_unlock(&tiptr->ti_lock);
840Sstevel@tonic-gate 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
850Sstevel@tonic-gate 			return (-1);
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 	/*
890Sstevel@tonic-gate 	 * Acquire buffer for use in sending/receiving the message.
900Sstevel@tonic-gate 	 * Note: assumes (correctly) that ti_ctlsize is large enough
910Sstevel@tonic-gate 	 * to hold sizeof (struct T_bind_req/ack)
920Sstevel@tonic-gate 	 */
930Sstevel@tonic-gate 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
940Sstevel@tonic-gate 		sv_errno = errno;
950Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
960Sstevel@tonic-gate 		(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
970Sstevel@tonic-gate 		errno = sv_errno;
980Sstevel@tonic-gate 		return (-1);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
101*132Srobinson 	/* LINTED pointer cast */
1020Sstevel@tonic-gate 	bind_reqp = (struct T_bind_req *)ctlbuf.buf;
1030Sstevel@tonic-gate 	size = (int)sizeof (struct T_bind_req);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	use_xpg41tpi = (_T_IS_XTI(api_semantics)) &&
1060Sstevel@tonic-gate 		((tiptr->ti_prov_flag & XPG4_1) != 0);
1070Sstevel@tonic-gate 	if (use_xpg41tpi)
1080Sstevel@tonic-gate 		/* XTI call and provider knows the XTI inspired TPI */
1090Sstevel@tonic-gate 		bind_reqp->PRIM_type = T_BIND_REQ;
1100Sstevel@tonic-gate 	else
1110Sstevel@tonic-gate 		/* TLI caller old TPI provider */
1120Sstevel@tonic-gate 		bind_reqp->PRIM_type = O_T_BIND_REQ;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	bind_reqp->ADDR_length = (req == NULL? 0: req->addr.len);
1150Sstevel@tonic-gate 	bind_reqp->ADDR_offset = 0;
1160Sstevel@tonic-gate 	bind_reqp->CONIND_number = (req == NULL? 0: req->qlen);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (bind_reqp->ADDR_length) {
1200Sstevel@tonic-gate 		if (_t_aligned_copy(&ctlbuf, (int)bind_reqp->ADDR_length, size,
1210Sstevel@tonic-gate 		    req->addr.buf, &bind_reqp->ADDR_offset) < 0) {
1220Sstevel@tonic-gate 			/*
1230Sstevel@tonic-gate 			 * Aligned copy will overflow buffer allocated based
1240Sstevel@tonic-gate 			 * on transport maximum address length.
1250Sstevel@tonic-gate 			 * return error.
1260Sstevel@tonic-gate 			 */
1270Sstevel@tonic-gate 			t_errno = TBADADDR;
1280Sstevel@tonic-gate 			goto err_out;
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 		size = bind_reqp->ADDR_offset + bind_reqp->ADDR_length;
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if (_t_do_ioctl(fd, ctlbuf.buf, size, TI_BIND, &retlen) < 0) {
1340Sstevel@tonic-gate 		goto err_out;
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (retlen < (int)sizeof (struct T_bind_ack)) {
1380Sstevel@tonic-gate 		t_errno = TSYSERR;
1390Sstevel@tonic-gate 		errno = EIO;
1400Sstevel@tonic-gate 		goto err_out;
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
143*132Srobinson 	/* LINTED pointer cast */
1440Sstevel@tonic-gate 	bind_ackp = (struct T_bind_ack *)ctlbuf.buf;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if ((req != NULL) && req->addr.len != 0 &&
1470Sstevel@tonic-gate 	    (use_xpg41tpi == 0) && (_T_IS_XTI(api_semantics))) {
1480Sstevel@tonic-gate 		/*
1490Sstevel@tonic-gate 		 * Best effort to do XTI on old TPI.
1500Sstevel@tonic-gate 		 *
1510Sstevel@tonic-gate 		 * Match address requested or unbind and fail with
1520Sstevel@tonic-gate 		 * TADDRBUSY.
1530Sstevel@tonic-gate 		 *
1540Sstevel@tonic-gate 		 * XXX - Hack alert ! Should we do this at all ?
1550Sstevel@tonic-gate 		 * Not "supported" as may not work if encoding of
1560Sstevel@tonic-gate 		 * address is different in the returned address. This
1570Sstevel@tonic-gate 		 * will also have trouble with TCP/UDP wildcard port
1580Sstevel@tonic-gate 		 * requests
1590Sstevel@tonic-gate 		 */
1600Sstevel@tonic-gate 		if ((req->addr.len != bind_ackp->ADDR_length) ||
1610Sstevel@tonic-gate 		    (memcmp(req->addr.buf, ctlbuf.buf +
1620Sstevel@tonic-gate 		    bind_ackp->ADDR_offset, req->addr.len) != 0)) {
1630Sstevel@tonic-gate 			(void) _tx_unbind_locked(fd, tiptr, &ctlbuf);
1640Sstevel@tonic-gate 			t_errno = TADDRBUSY;
1650Sstevel@tonic-gate 			goto err_out;
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	tiptr->ti_ocnt = 0;
1700Sstevel@tonic-gate 	tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	_T_TX_NEXTSTATE(T_BIND, tiptr, "t_bind: invalid state event T_BIND");
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if (ret != NULL) {
1750Sstevel@tonic-gate 		if (_T_IS_TLI(api_semantics) || ret->addr.maxlen > 0) {
1760Sstevel@tonic-gate 			if (TLEN_GT_NLEN(bind_reqp->ADDR_length,
1770Sstevel@tonic-gate 			    ret->addr.maxlen)) {
1780Sstevel@tonic-gate 				t_errno = TBUFOVFLW;
1790Sstevel@tonic-gate 				goto err_out;
1800Sstevel@tonic-gate 			}
1810Sstevel@tonic-gate 			(void) memcpy(ret->addr.buf,
1820Sstevel@tonic-gate 			    ctlbuf.buf + bind_ackp->ADDR_offset,
1830Sstevel@tonic-gate 			    (size_t)bind_ackp->ADDR_length);
1840Sstevel@tonic-gate 			ret->addr.len = bind_ackp->ADDR_length;
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 		ret->qlen = bind_ackp->CONIND_number;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	tiptr->ti_qlen = (uint_t)bind_ackp->CONIND_number;
1900Sstevel@tonic-gate 
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 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1970Sstevel@tonic-gate 	return (0);
1980Sstevel@tonic-gate 	/* NOTREACHED */
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 	sig_mutex_unlock(&tiptr->ti_lock);
2060Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
2070Sstevel@tonic-gate 	errno = sv_errno;
2080Sstevel@tonic-gate 	return (-1);
2090Sstevel@tonic-gate }
210