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 (the "License").
2N/A * You may not use this file except in compliance 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/*
2N/A * Copyright (c) 1988, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#include "mt.h"
2N/A#include <stdlib.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <errno.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 <stropts.h>
2N/A#include "tx.h"
2N/A
2N/A/*
2N/A * The following is based on XTI standard.
2N/A */
2N/A#define ALIGN_XTI_opthdr_size (sizeof (t_uscalar_t))
2N/A
2N/A#define ROUNDUP_XTI_opthdr(p) (((p) +\
2N/A (ALIGN_XTI_opthdr_size-1)) & ~(ALIGN_XTI_opthdr_size-1))
2N/A#define ISALIGNED_XTI_opthdr(p) \
2N/A (((ulong_t)(p) & (ALIGN_XTI_opthdr_size - 1)) == 0)
2N/A
2N/Aint
2N/A_tx_optmgmt(
2N/A int fd,
2N/A const struct t_optmgmt *req,
2N/A struct t_optmgmt *ret,
2N/A int api_semantics
2N/A)
2N/A{
2N/A int size, sv_errno;
2N/A struct strbuf ctlbuf;
2N/A struct T_optmgmt_req *optreq;
2N/A struct T_optmgmt_ack *optack;
2N/A struct _ti_user *tiptr;
2N/A sigset_t mask;
2N/A int didalloc, retlen;
2N/A struct t_opthdr *opt, *next_opt;
2N/A struct t_opthdr *opt_start, *opt_end;
2N/A t_uscalar_t first_opt_level;
2N/A t_scalar_t optlen;
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 during the TI_OPTMGMT operation
2N/A * as option change being done could potentially be a
2N/A * non-idempotent 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
2N/A /*
2N/A * Acquire buf for use in sending/receiving of the message.
2N/A * Note: assumes (correctly) that ti_ctlsize is large enough
2N/A * to hold sizeof (struct T_bind_req)
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 /*
2N/A * effective option length in local variable "optlen"
2N/A * Note: can change for XTI for T_ALLOPT. XTI spec states
2N/A * that options after the T_ALLOPT option are to be ignored
2N/A * therefore we trncate the option buffer there and modify
2N/A * the effective length accordingly later.
2N/A */
2N/A optlen = req->opt.len;
2N/A
2N/A if (_T_IS_XTI(api_semantics) && (optlen > 0)) {
2N/A /*
2N/A * Verify integrity of option buffer according to
2N/A * XTI t_optmgmt() semantics.
2N/A */
2N/A
2N/A if (req->opt.buf == NULL ||
2N/A optlen < (t_scalar_t)sizeof (struct t_opthdr)) {
2N/A /* option buffer should atleast have an t_opthdr */
2N/A t_errno = TBADOPT;
2N/A goto err_out;
2N/A }
2N/A
2N/A opt_start = (struct t_opthdr *)req->opt.buf;
2N/A
2N/A /*
2N/A * XXX We interpret that an option has to start on an
2N/A * aligned buffer boundary. This is not very explcit in
2N/A * XTI spec in text but the picture in Section 6.2 shows
2N/A * "opt.buf" at start of buffer and in combination with
2N/A * text can be construed to be restricting it to start
2N/A * on an aligned boundary. [Whether similar restriction
2N/A * applies to output buffer "ret->opt.buf" is an "interesting
2N/A * question" but we ignore it for now as that is the problem
2N/A * for the application not our implementation which will
2N/A * does not enforce any alignment requirement.]
2N/A *
2N/A * If start of buffer is not aligned, we signal an error.
2N/A */
2N/A if (!(ISALIGNED_XTI_opthdr(opt_start))) {
2N/A t_errno = TBADOPT;
2N/A goto err_out;
2N/A }
2N/A
2N/A /* LINTED pointer cast */
2N/A opt_end = (struct t_opthdr *)((char *)opt_start + optlen);
2N/A
2N/A /*
2N/A * Make sure we have enough in the message to dereference
2N/A * the option header.
2N/A */
2N/A if ((uchar_t *)opt_start + sizeof (struct t_opthdr)
2N/A > (uchar_t *)opt_end) {
2N/A t_errno = TBADOPT;
2N/A goto err_out;
2N/A }
2N/A /*
2N/A * If there are multiple options, they all have to be
2N/A * the same level (so says XTI semantics).
2N/A */
2N/A first_opt_level = opt_start->level;
2N/A
2N/A for (opt = opt_start; opt < opt_end; opt = next_opt) {
2N/A /*
2N/A * Make sure we have enough in the message to
2N/A * dereference the option header.
2N/A */
2N/A if ((uchar_t *)opt_start + sizeof (struct t_opthdr)
2N/A > (uchar_t *)opt_end) {
2N/A t_errno = TBADOPT;
2N/A goto err_out;
2N/A }
2N/A /*
2N/A * We now compute pointer to next option in buffer
2N/A * 'next_opt' the next_opt computation above below
2N/A * 'opt->len' initialized by application which cannot
2N/A * be trusted. The usual value too large will be
2N/A * captured by the loop termination condition above.
2N/A * We check for the following which it will miss.
2N/A * (1)pointer space wraparound arithmetic overflow
2N/A * (2)last option in buffer with 'opt->len' being
2N/A * too large
2N/A * (only reason 'next_opt' should equal or exceed
2N/A * 'opt_end' for last option is roundup unless
2N/A * length is too-large/invalid)
2N/A * (3) we also enforce the XTI restriction that
2N/A * all options in the buffer have to be the
2N/A * same level.
2N/A */
2N/A /* LINTED pointer cast */
2N/A next_opt = (struct t_opthdr *)((uchar_t *)opt +
2N/A ROUNDUP_XTI_opthdr(opt->len));
2N/A
2N/A if ((uchar_t *)next_opt < (uchar_t *)opt || /* (1) */
2N/A ((next_opt >= opt_end) &&
2N/A (((uchar_t *)next_opt - (uchar_t *)opt_end) >=
2N/A ALIGN_XTI_opthdr_size)) || /* (2) */
2N/A (opt->level != first_opt_level)) { /* (3) */
2N/A t_errno = TBADOPT;
2N/A goto err_out;
2N/A }
2N/A
2N/A /*
2N/A * XTI semantics: options in the buffer after
2N/A * the T_ALLOPT option can be ignored
2N/A */
2N/A if (opt->name == T_ALLOPT) {
2N/A if (next_opt < opt_end) {
2N/A /*
2N/A * there are options following, ignore
2N/A * them and truncate input
2N/A */
2N/A optlen = (t_scalar_t)((uchar_t *)
2N/A next_opt - (uchar_t *)opt_start);
2N/A opt_end = next_opt;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* LINTED pointer cast */
2N/A optreq = (struct T_optmgmt_req *)ctlbuf.buf;
2N/A if (_T_IS_XTI(api_semantics))
2N/A optreq->PRIM_type = T_OPTMGMT_REQ;
2N/A else
2N/A optreq->PRIM_type = T_SVR4_OPTMGMT_REQ;
2N/A
2N/A optreq->OPT_length = optlen;
2N/A optreq->OPT_offset = 0;
2N/A optreq->MGMT_flags = req->flags;
2N/A size = (int)sizeof (struct T_optmgmt_req);
2N/A
2N/A if (optlen) {
2N/A if (_t_aligned_copy(&ctlbuf, optlen, size,
2N/A req->opt.buf, &optreq->OPT_offset) < 0) {
2N/A /*
2N/A * Aligned copy will overflow buffer allocated
2N/A * based on maximum transport option size information
2N/A */
2N/A t_errno = TBADOPT;
2N/A goto err_out;
2N/A }
2N/A size = optreq->OPT_offset + optreq->OPT_length;
2N/A }
2N/A
2N/A if (_t_do_ioctl(fd, ctlbuf.buf, size, TI_OPTMGMT, &retlen) < 0)
2N/A goto err_out;
2N/A
2N/A if (retlen < (int)sizeof (struct T_optmgmt_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 optack = (struct T_optmgmt_ack *)ctlbuf.buf;
2N/A
2N/A if (_T_IS_TLI(api_semantics) || ret->opt.maxlen > 0) {
2N/A if (TLEN_GT_NLEN(optack->OPT_length, ret->opt.maxlen)) {
2N/A t_errno = TBUFOVFLW;
2N/A goto err_out;
2N/A }
2N/A (void) memcpy(ret->opt.buf,
2N/A (char *)(ctlbuf.buf + optack->OPT_offset),
2N/A (unsigned int) optack->OPT_length);
2N/A ret->opt.len = optack->OPT_length;
2N/A }
2N/A
2N/A /*
2N/A * Note: TPI is not clear about what really is carries in the
2N/A * T_OPTMGMT_ACK MGMT_flags fields. For T_OPTMGMT_ACK in response
2N/A * to T_SVR4_OPTMGMT_REQ, the Internet protocols in Solaris 2.X return
2N/A * the result code only (T_SUCCESS). For T_OPTMGMT_ACK in response
2N/A * to T_OPTMGMT_REQ, currently "worst status" code required for
2N/A * XTI is carried from the set of options OR'd with request flag.
2N/A * (This can change in future and "worst status" computation done
2N/A * with a scan in this routine.
2N/A *
2N/A * Note: Even for T_OPTMGMT_ACK is response to T_SVR4_OPTMGMT_REQ,
2N/A * removing request flag should be OK though it will not be set.
2N/A */
2N/A ret->flags = optack->MGMT_flags & ~req->flags;
2N/A
2N/A /*
2N/A * NOTE:
2N/A * There is no real change of state in state table for option
2N/A * management. The state change macro is used below only for its
2N/A * debugging and logging capabilities.
2N/A * The TLI "(mis)feature" (option management only in T_IDLE state)
2N/A * has been deprecated in XTI and our state table reflect updated for
2N/A * both TLI and XTI to reflect that.
2N/A * TLI semantics can be enforced by the transport providers that
2N/A * desire it at TPI level.
2N/A * There is no need to enforce this in the library since
2N/A * sane transport providers that do allow it (e.g TCP and it *needs*
2N/A * to allow it) should be allowed to work fine.
2N/A * The only transport providers that return TOUTSTATE for TLI
2N/A * t_optmgmt() are the drivers used for conformance testing to the
2N/A * broken TLI standard.
2N/A * These are /dev/{ticots,ticotsord,ticlts} used by the Sparc ABI test
2N/A * suite. Others are /dev/{tivc,tidg} used by the SVVS test suite.
2N/A */
2N/A
2N/A _T_TX_NEXTSTATE(T_OPTMGMT, tiptr,
2N/A "t_optmgmt: invalid state event T_OPTMGMT");
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/A
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}