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 <unistd.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 <stdio.h>
2N/A#include <errno.h>
2N/A#include <ucred.h>
2N/A#include <signal.h>
2N/A#include "tx.h"
2N/A
2N/A/*
2N/A * Function protoypes
2N/A */
2N/Astatic int _alloc_buf(struct netbuf *buf, t_scalar_t n, int fields,
2N/A int api_semantics, boolean_t option);
2N/A
2N/Avoid *
2N/A_tx_alloc(int fd, int struct_type, int fields, int api_semantics)
2N/A{
2N/A struct strioctl strioc;
2N/A struct T_info_ack info;
2N/A union structptrs {
2N/A char *caddr;
2N/A struct t_bind *bind;
2N/A struct t_call *call;
2N/A struct t_discon *dis;
2N/A struct t_optmgmt *opt;
2N/A struct t_unitdata *udata;
2N/A struct t_uderr *uderr;
2N/A struct t_info *info;
2N/A } p;
2N/A unsigned int dsize;
2N/A struct _ti_user *tiptr;
2N/A int retval, sv_errno;
2N/A t_scalar_t optsize;
2N/A
2N/A if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
2N/A return (NULL);
2N/A sig_mutex_lock(&tiptr->ti_lock);
2N/A
2N/A /*
2N/A * Get size info for T_ADDR, T_OPT, and T_UDATA fields
2N/A */
2N/A info.PRIM_type = T_INFO_REQ;
2N/A strioc.ic_cmd = TI_GETINFO;
2N/A strioc.ic_timout = -1;
2N/A strioc.ic_len = (int)sizeof (struct T_info_req);
2N/A strioc.ic_dp = (char *)&info;
2N/A do {
2N/A retval = ioctl(fd, I_STR, &strioc);
2N/A } while (retval < 0 && errno == EINTR);
2N/A
2N/A if (retval < 0) {
2N/A sv_errno = errno;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A t_errno = TSYSERR;
2N/A errno = sv_errno;
2N/A return (NULL);
2N/A }
2N/A
2N/A if (strioc.ic_len != (int)sizeof (struct T_info_ack)) {
2N/A t_errno = TSYSERR;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A errno = EIO;
2N/A return (NULL);
2N/A }
2N/A
2N/A
2N/A /*
2N/A * Malloc appropriate structure and the specified
2N/A * fields within each structure. Initialize the
2N/A * 'buf' and 'maxlen' fields of each.
2N/A */
2N/A switch (struct_type) {
2N/A
2N/A case T_BIND:
2N/A if ((p.bind = calloc(1, sizeof (struct t_bind))) == NULL)
2N/A goto errout;
2N/A if (fields & T_ADDR) {
2N/A if (_alloc_buf(&p.bind->addr,
2N/A info.ADDR_size,
2N/A fields, api_semantics, B_FALSE) < 0)
2N/A goto errout;
2N/A }
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A return (p.bind);
2N/A
2N/A case T_CALL:
2N/A if ((p.call = calloc(1, sizeof (struct t_call))) == NULL)
2N/A goto errout;
2N/A if (fields & T_ADDR) {
2N/A if (_alloc_buf(&p.call->addr,
2N/A info.ADDR_size,
2N/A fields, api_semantics, B_FALSE) < 0)
2N/A goto errout;
2N/A }
2N/A if (fields & T_OPT) {
2N/A if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
2N/A /* compensate for XTI level options */
2N/A optsize = info.OPT_size +
2N/A TX_XTI_LEVEL_MAX_OPTBUF;
2N/A else
2N/A optsize = info.OPT_size;
2N/A if (_alloc_buf(&p.call->opt, optsize,
2N/A fields, api_semantics, B_TRUE) < 0)
2N/A goto errout;
2N/A }
2N/A if (fields & T_UDATA) {
2N/A dsize = _T_MAX((int)info.CDATA_size,
2N/A (int)info.DDATA_size);
2N/A if (_alloc_buf(&p.call->udata, (t_scalar_t)dsize,
2N/A fields, api_semantics, B_FALSE) < 0)
2N/A goto errout;
2N/A }
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A return (p.call);
2N/A
2N/A case T_OPTMGMT:
2N/A if ((p.opt = calloc(1, sizeof (struct t_optmgmt))) == NULL)
2N/A goto errout;
2N/A if (fields & T_OPT) {
2N/A if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
2N/A /* compensate for XTI level options */
2N/A optsize = info.OPT_size +
2N/A TX_XTI_LEVEL_MAX_OPTBUF;
2N/A else
2N/A optsize = info.OPT_size;
2N/A if (_alloc_buf(&p.opt->opt, optsize,
2N/A fields, api_semantics, B_TRUE) < 0)
2N/A goto errout;
2N/A }
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A return (p.opt);
2N/A
2N/A case T_DIS:
2N/A if ((p.dis = calloc(1, sizeof (struct t_discon))) == NULL)
2N/A goto errout;
2N/A if (fields & T_UDATA) {
2N/A if (_alloc_buf(&p.dis->udata, info.DDATA_size,
2N/A fields, api_semantics, B_FALSE) < 0)
2N/A goto errout;
2N/A }
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A return (p.dis);
2N/A
2N/A case T_UNITDATA:
2N/A if ((p.udata = calloc(1, sizeof (struct t_unitdata))) == NULL)
2N/A goto errout;
2N/A if (fields & T_ADDR) {
2N/A if (_alloc_buf(&p.udata->addr, info.ADDR_size,
2N/A fields, api_semantics, B_FALSE) < 0)
2N/A goto errout;
2N/A }
2N/A if (fields & T_OPT) {
2N/A if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
2N/A /* compensate for XTI level options */
2N/A optsize = info.OPT_size +
2N/A TX_XTI_LEVEL_MAX_OPTBUF;
2N/A else
2N/A optsize = info.OPT_size;
2N/A if (_alloc_buf(&p.udata->opt, optsize,
2N/A fields, api_semantics, B_TRUE) < 0)
2N/A goto errout;
2N/A }
2N/A if (fields & T_UDATA) {
2N/A if (_alloc_buf(&p.udata->udata, info.TSDU_size,
2N/A fields, api_semantics, B_FALSE) < 0)
2N/A goto errout;
2N/A }
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A return (p.udata);
2N/A
2N/A case T_UDERROR:
2N/A if ((p.uderr = calloc(1, sizeof (struct t_uderr))) == NULL)
2N/A goto errout;
2N/A if (fields & T_ADDR) {
2N/A if (_alloc_buf(&p.uderr->addr, info.ADDR_size,
2N/A fields, api_semantics, B_FALSE) < 0)
2N/A goto errout;
2N/A }
2N/A if (fields & T_OPT) {
2N/A if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
2N/A /* compensate for XTI level options */
2N/A optsize = info.OPT_size +
2N/A TX_XTI_LEVEL_MAX_OPTBUF;
2N/A else
2N/A optsize = info.OPT_size;
2N/A if (_alloc_buf(&p.uderr->opt, optsize,
2N/A fields, api_semantics, B_FALSE) < 0)
2N/A goto errout;
2N/A }
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A return (p.uderr);
2N/A
2N/A case T_INFO:
2N/A if ((p.info = calloc(1, sizeof (struct t_info))) == NULL)
2N/A goto errout;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A return (p.info);
2N/A
2N/A default:
2N/A if (_T_IS_XTI(api_semantics)) {
2N/A t_errno = TNOSTRUCTYPE;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A } else { /* TX_TLI_API */
2N/A t_errno = TSYSERR;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A errno = EINVAL;
2N/A }
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * Clean up. Set t_errno to TSYSERR.
2N/A * If it is because memory could not be allocated
2N/A * then errno already should have been set to
2N/A * ENOMEM
2N/A */
2N/Aerrout:
2N/A if (p.caddr)
2N/A (void) t_free(p.caddr, struct_type);
2N/A
2N/A t_errno = TSYSERR;
2N/A sig_mutex_unlock(&tiptr->ti_lock);
2N/A return (NULL);
2N/A}
2N/A
2N/Astatic int
2N/A_alloc_buf(struct netbuf *buf, t_scalar_t n, int fields, int api_semantics,
2N/A boolean_t option)
2N/A{
2N/A switch (n) {
2N/A case T_INFINITE /* -1 */:
2N/A if (_T_IS_XTI(api_semantics)) {
2N/A buf->buf = NULL;
2N/A buf->maxlen = 0;
2N/A if (fields != T_ALL) {
2N/A /*
2N/A * Do not return error
2N/A * if T_ALL is used.
2N/A */
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A } else if (option) { /* TX_TLI_API */
2N/A static size_t infalloc;
2N/A
2N/A /*
2N/A * retain TLI behavior; ucred_t can vary in size,
2N/A * we need to make sure that we can receive one.
2N/A */
2N/A if (infalloc == 0) {
2N/A size_t uc = ucred_size();
2N/A if (uc < 1024/2)
2N/A infalloc = 1024;
2N/A else
2N/A infalloc = uc + 1024/2;
2N/A }
2N/A if ((buf->buf = calloc(1, infalloc)) == NULL) {
2N/A errno = ENOMEM;
2N/A return (-1);
2N/A } else
2N/A buf->maxlen = infalloc;
2N/A } else { /* TX_TLI_API */
2N/A /*
2N/A * retain TLI behavior
2N/A */
2N/A if ((buf->buf = calloc(1, 1024)) == NULL) {
2N/A errno = ENOMEM;
2N/A return (-1);
2N/A } else
2N/A buf->maxlen = 1024;
2N/A }
2N/A break;
2N/A
2N/A case 0:
2N/A buf->buf = NULL;
2N/A buf->maxlen = 0;
2N/A break;
2N/A
2N/A case T_INVALID /* -2 */:
2N/A if (_T_IS_XTI(api_semantics)) {
2N/A buf->buf = NULL;
2N/A buf->maxlen = 0;
2N/A if (fields != T_ALL) {
2N/A /*
2N/A * Do not return error
2N/A * if T_ALL is used.
2N/A */
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A } else { /* TX_TLI_API */
2N/A /*
2N/A * retain TLI behavior
2N/A */
2N/A buf->buf = NULL;
2N/A buf->maxlen = 0;
2N/A }
2N/A break;
2N/A
2N/A default:
2N/A if ((buf->buf = calloc(1, (size_t)n)) == NULL) {
2N/A errno = ENOMEM;
2N/A return (-1);
2N/A } else
2N/A buf->maxlen = n;
2N/A break;
2N/A }
2N/A return (0);
2N/A}