2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.3.4.1 */
2N/A
2N/A#include "mt.h"
2N/A#include <stdlib.h>
2N/A#include <errno.h>
2N/A#include <string.h>
2N/A#include <stropts.h>
2N/A#include <sys/stream.h>
2N/A#define _SUN_TPI_VERSION 2
2N/A#include <sys/tihdr.h>
2N/A#include <sys/timod.h>
2N/A#include <xti.h>
2N/A#include <signal.h>
2N/A#include <syslog.h>
2N/A#include "tx.h"
2N/A
2N/Aint
2N/A_tx_bind(
2N/A int fd,
2N/A const struct t_bind *req,
2N/A struct t_bind *ret,
2N/A int api_semantics
2N/A)
2N/A{
2N/A struct T_bind_req *bind_reqp;
2N/A struct T_bind_ack *bind_ackp;
2N/A int size, sv_errno, retlen;
2N/A struct _ti_user *tiptr;
2N/A sigset_t mask;
2N/A
2N/A int didalloc;
2N/A int use_xpg41tpi;
2N/A struct strbuf ctlbuf;
2N/A
2N/A if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
2N/A return (-1);
2N/A
2N/A /*
2N/A * We block all signals since TI_BIND, which sends a TPI message
2N/A * O_T_BIND_REQ down, is not an idempotetent operation
2N/A * Note that sig_mutex_lock() only defers signals, it does not
2N/A * block them, so interruptible syscalls could still get EINTR.
2N/A */
2N/A (void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
2N/A sig_mutex_lock(&tiptr->ti_lock);
2N/A if (_T_IS_XTI(api_semantics)) {
2N/A /*
2N/A * User level state verification only done for XTI
2N/A * because doing for TLI may break existing applications
2N/A */
2N/A if (tiptr->ti_state != T_UNBND) {
2N/A t_errno = TOUTSTATE;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
2N/A return (-1);
2N/A }
2N/A }
2N/A /*
2N/A * Acquire buffer for use in sending/receiving the message.
2N/A * Note: assumes (correctly) that ti_ctlsize is large enough
2N/A * to hold sizeof (struct T_bind_req/ack)
2N/A */
2N/A if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
2N/A sv_errno = errno;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
2N/A errno = sv_errno;
2N/A return (-1);
2N/A }
2N/A
2N/A /* LINTED pointer cast */
2N/A bind_reqp = (struct T_bind_req *)ctlbuf.buf;
2N/A size = (int)sizeof (struct T_bind_req);
2N/A
2N/A use_xpg41tpi = (_T_IS_XTI(api_semantics)) &&
2N/A ((tiptr->ti_prov_flag & XPG4_1) != 0);
2N/A if (use_xpg41tpi)
2N/A /* XTI call and provider knows the XTI inspired TPI */
2N/A bind_reqp->PRIM_type = T_BIND_REQ;
2N/A else
2N/A /* TLI caller old TPI provider */
2N/A bind_reqp->PRIM_type = O_T_BIND_REQ;
2N/A
2N/A bind_reqp->ADDR_length = (req == NULL? 0: req->addr.len);
2N/A bind_reqp->ADDR_offset = 0;
2N/A bind_reqp->CONIND_number = (req == NULL? 0: req->qlen);
2N/A
2N/A
2N/A if (bind_reqp->ADDR_length) {
2N/A if (_t_aligned_copy(&ctlbuf, (int)bind_reqp->ADDR_length, size,
2N/A req->addr.buf, &bind_reqp->ADDR_offset) < 0) {
2N/A /*
2N/A * Aligned copy will overflow buffer allocated based
2N/A * on transport maximum address length.
2N/A * return error.
2N/A */
2N/A t_errno = TBADADDR;
2N/A goto err_out;
2N/A }
2N/A size = bind_reqp->ADDR_offset + bind_reqp->ADDR_length;
2N/A }
2N/A
2N/A if (_t_do_ioctl(fd, ctlbuf.buf, size, TI_BIND, &retlen) < 0) {
2N/A goto err_out;
2N/A }
2N/A
2N/A if (retlen < (int)sizeof (struct T_bind_ack)) {
2N/A t_errno = TSYSERR;
2N/A errno = EIO;
2N/A goto err_out;
2N/A }
2N/A
2N/A /* LINTED pointer cast */
2N/A bind_ackp = (struct T_bind_ack *)ctlbuf.buf;
2N/A
2N/A if ((req != NULL) && req->addr.len != 0 &&
2N/A (use_xpg41tpi == 0) && (_T_IS_XTI(api_semantics))) {
2N/A /*
2N/A * Best effort to do XTI on old TPI.
2N/A *
2N/A * Match address requested or unbind and fail with
2N/A * TADDRBUSY.
2N/A *
2N/A * XXX - Hack alert ! Should we do this at all ?
2N/A * Not "supported" as may not work if encoding of
2N/A * address is different in the returned address. This
2N/A * will also have trouble with TCP/UDP wildcard port
2N/A * requests
2N/A */
2N/A if ((req->addr.len != bind_ackp->ADDR_length) ||
2N/A (memcmp(req->addr.buf, ctlbuf.buf +
2N/A bind_ackp->ADDR_offset, req->addr.len) != 0)) {
2N/A (void) _tx_unbind_locked(fd, tiptr, &ctlbuf);
2N/A t_errno = TADDRBUSY;
2N/A goto err_out;
2N/A }
2N/A }
2N/A
2N/A tiptr->ti_ocnt = 0;
2N/A tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
2N/A
2N/A _T_TX_NEXTSTATE(T_BIND, tiptr, "t_bind: invalid state event T_BIND");
2N/A
2N/A if (ret != NULL) {
2N/A if (_T_IS_TLI(api_semantics) || ret->addr.maxlen > 0) {
2N/A if (TLEN_GT_NLEN(bind_reqp->ADDR_length,
2N/A ret->addr.maxlen)) {
2N/A t_errno = TBUFOVFLW;
2N/A goto err_out;
2N/A }
2N/A (void) memcpy(ret->addr.buf,
2N/A ctlbuf.buf + bind_ackp->ADDR_offset,
2N/A (size_t)bind_ackp->ADDR_length);
2N/A ret->addr.len = bind_ackp->ADDR_length;
2N/A }
2N/A ret->qlen = bind_ackp->CONIND_number;
2N/A }
2N/A
2N/A tiptr->ti_qlen = (uint_t)bind_ackp->CONIND_number;
2N/A
2N/A if (didalloc)
2N/A free(ctlbuf.buf);
2N/A else
2N/A tiptr->ti_ctlbuf = ctlbuf.buf;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
2N/A return (0);
2N/A /* NOTREACHED */
2N/Aerr_out:
2N/A sv_errno = errno;
2N/A if (didalloc)
2N/A free(ctlbuf.buf);
2N/A else
2N/A tiptr->ti_ctlbuf = ctlbuf.buf;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
2N/A errno = sv_errno;
2N/A return (-1);
2N/A}