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
5*11134SCasper.Dik@Sun.COM * Common Development and Distribution License (the "License").
6*11134SCasper.Dik@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21132Srobinson
220Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
230Sstevel@tonic-gate /* All Rights Reserved */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
26*11134SCasper.Dik@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
270Sstevel@tonic-gate * Use is subject to license terms.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include "mt.h"
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <stropts.h>
340Sstevel@tonic-gate #include <sys/stream.h>
350Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
360Sstevel@tonic-gate #include <sys/tihdr.h>
370Sstevel@tonic-gate #include <sys/timod.h>
380Sstevel@tonic-gate #include <xti.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <errno.h>
41*11134SCasper.Dik@Sun.COM #include <ucred.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate #include "tx.h"
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Function protoypes
470Sstevel@tonic-gate */
480Sstevel@tonic-gate static int _alloc_buf(struct netbuf *buf, t_scalar_t n, int fields,
49*11134SCasper.Dik@Sun.COM int api_semantics, boolean_t option);
500Sstevel@tonic-gate
510Sstevel@tonic-gate char *
_tx_alloc(int fd,int struct_type,int fields,int api_semantics)520Sstevel@tonic-gate _tx_alloc(int fd, int struct_type, int fields, int api_semantics)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate struct strioctl strioc;
550Sstevel@tonic-gate struct T_info_ack info;
560Sstevel@tonic-gate union structptrs {
570Sstevel@tonic-gate char *caddr;
580Sstevel@tonic-gate struct t_bind *bind;
590Sstevel@tonic-gate struct t_call *call;
600Sstevel@tonic-gate struct t_discon *dis;
610Sstevel@tonic-gate struct t_optmgmt *opt;
620Sstevel@tonic-gate struct t_unitdata *udata;
630Sstevel@tonic-gate struct t_uderr *uderr;
640Sstevel@tonic-gate struct t_info *info;
650Sstevel@tonic-gate } p;
660Sstevel@tonic-gate unsigned int dsize;
670Sstevel@tonic-gate struct _ti_user *tiptr;
680Sstevel@tonic-gate int retval, sv_errno;
690Sstevel@tonic-gate t_scalar_t optsize;
700Sstevel@tonic-gate
71132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
720Sstevel@tonic-gate return (NULL);
730Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Get size info for T_ADDR, T_OPT, and T_UDATA fields
770Sstevel@tonic-gate */
780Sstevel@tonic-gate info.PRIM_type = T_INFO_REQ;
790Sstevel@tonic-gate strioc.ic_cmd = TI_GETINFO;
800Sstevel@tonic-gate strioc.ic_timout = -1;
810Sstevel@tonic-gate strioc.ic_len = (int)sizeof (struct T_info_req);
820Sstevel@tonic-gate strioc.ic_dp = (char *)&info;
830Sstevel@tonic-gate do {
841219Sraf retval = ioctl(fd, I_STR, &strioc);
850Sstevel@tonic-gate } while (retval < 0 && errno == EINTR);
860Sstevel@tonic-gate
870Sstevel@tonic-gate if (retval < 0) {
880Sstevel@tonic-gate sv_errno = errno;
890Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
900Sstevel@tonic-gate t_errno = TSYSERR;
910Sstevel@tonic-gate errno = sv_errno;
920Sstevel@tonic-gate return (NULL);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate if (strioc.ic_len != (int)sizeof (struct T_info_ack)) {
960Sstevel@tonic-gate t_errno = TSYSERR;
970Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
980Sstevel@tonic-gate errno = EIO;
990Sstevel@tonic-gate return (NULL);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * Malloc appropriate structure and the specified
1050Sstevel@tonic-gate * fields within each structure. Initialize the
1060Sstevel@tonic-gate * 'buf' and 'maxlen' fields of each.
1070Sstevel@tonic-gate */
1080Sstevel@tonic-gate switch (struct_type) {
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate case T_BIND:
111132Srobinson if ((p.bind = calloc(1, sizeof (struct t_bind))) == NULL)
112132Srobinson goto errout;
1130Sstevel@tonic-gate if (fields & T_ADDR) {
1140Sstevel@tonic-gate if (_alloc_buf(&p.bind->addr,
1150Sstevel@tonic-gate info.ADDR_size,
116*11134SCasper.Dik@Sun.COM fields, api_semantics, B_FALSE) < 0)
1170Sstevel@tonic-gate goto errout;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1200Sstevel@tonic-gate return ((char *)p.bind);
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate case T_CALL:
123132Srobinson if ((p.call = calloc(1, sizeof (struct t_call))) == NULL)
124132Srobinson goto errout;
1250Sstevel@tonic-gate if (fields & T_ADDR) {
1260Sstevel@tonic-gate if (_alloc_buf(&p.call->addr,
1270Sstevel@tonic-gate info.ADDR_size,
128*11134SCasper.Dik@Sun.COM fields, api_semantics, B_FALSE) < 0)
1290Sstevel@tonic-gate goto errout;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate if (fields & T_OPT) {
1320Sstevel@tonic-gate if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1330Sstevel@tonic-gate /* compensate for XTI level options */
1340Sstevel@tonic-gate optsize = info.OPT_size +
1350Sstevel@tonic-gate TX_XTI_LEVEL_MAX_OPTBUF;
1360Sstevel@tonic-gate else
1370Sstevel@tonic-gate optsize = info.OPT_size;
1380Sstevel@tonic-gate if (_alloc_buf(&p.call->opt, optsize,
139*11134SCasper.Dik@Sun.COM fields, api_semantics, B_TRUE) < 0)
1400Sstevel@tonic-gate goto errout;
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate if (fields & T_UDATA) {
1430Sstevel@tonic-gate dsize = _T_MAX((int)info.CDATA_size,
144*11134SCasper.Dik@Sun.COM (int)info.DDATA_size);
1450Sstevel@tonic-gate if (_alloc_buf(&p.call->udata, (t_scalar_t)dsize,
146*11134SCasper.Dik@Sun.COM fields, api_semantics, B_FALSE) < 0)
1470Sstevel@tonic-gate goto errout;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1500Sstevel@tonic-gate return ((char *)p.call);
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate case T_OPTMGMT:
153132Srobinson if ((p.opt = calloc(1, sizeof (struct t_optmgmt))) == NULL)
154132Srobinson goto errout;
1550Sstevel@tonic-gate if (fields & T_OPT) {
1560Sstevel@tonic-gate if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1570Sstevel@tonic-gate /* compensate for XTI level options */
1580Sstevel@tonic-gate optsize = info.OPT_size +
1590Sstevel@tonic-gate TX_XTI_LEVEL_MAX_OPTBUF;
1600Sstevel@tonic-gate else
1610Sstevel@tonic-gate optsize = info.OPT_size;
1620Sstevel@tonic-gate if (_alloc_buf(&p.opt->opt, optsize,
163*11134SCasper.Dik@Sun.COM fields, api_semantics, B_TRUE) < 0)
1640Sstevel@tonic-gate goto errout;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1670Sstevel@tonic-gate return ((char *)p.opt);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate case T_DIS:
170132Srobinson if ((p.dis = calloc(1, sizeof (struct t_discon))) == NULL)
1710Sstevel@tonic-gate goto errout;
1720Sstevel@tonic-gate if (fields & T_UDATA) {
1730Sstevel@tonic-gate if (_alloc_buf(&p.dis->udata, info.DDATA_size,
174*11134SCasper.Dik@Sun.COM fields, api_semantics, B_FALSE) < 0)
1750Sstevel@tonic-gate goto errout;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1780Sstevel@tonic-gate return ((char *)p.dis);
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate case T_UNITDATA:
181132Srobinson if ((p.udata = calloc(1, sizeof (struct t_unitdata))) == NULL)
182132Srobinson goto errout;
1830Sstevel@tonic-gate if (fields & T_ADDR) {
1840Sstevel@tonic-gate if (_alloc_buf(&p.udata->addr, info.ADDR_size,
185*11134SCasper.Dik@Sun.COM fields, api_semantics, B_FALSE) < 0)
1860Sstevel@tonic-gate goto errout;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate if (fields & T_OPT) {
1890Sstevel@tonic-gate if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1900Sstevel@tonic-gate /* compensate for XTI level options */
1910Sstevel@tonic-gate optsize = info.OPT_size +
1920Sstevel@tonic-gate TX_XTI_LEVEL_MAX_OPTBUF;
1930Sstevel@tonic-gate else
1940Sstevel@tonic-gate optsize = info.OPT_size;
1950Sstevel@tonic-gate if (_alloc_buf(&p.udata->opt, optsize,
196*11134SCasper.Dik@Sun.COM fields, api_semantics, B_TRUE) < 0)
1970Sstevel@tonic-gate goto errout;
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate if (fields & T_UDATA) {
2000Sstevel@tonic-gate if (_alloc_buf(&p.udata->udata, info.TSDU_size,
201*11134SCasper.Dik@Sun.COM fields, api_semantics, B_FALSE) < 0)
2020Sstevel@tonic-gate goto errout;
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2050Sstevel@tonic-gate return ((char *)p.udata);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate case T_UDERROR:
208132Srobinson if ((p.uderr = calloc(1, sizeof (struct t_uderr))) == NULL)
209132Srobinson goto errout;
2100Sstevel@tonic-gate if (fields & T_ADDR) {
2110Sstevel@tonic-gate if (_alloc_buf(&p.uderr->addr, info.ADDR_size,
212*11134SCasper.Dik@Sun.COM fields, api_semantics, B_FALSE) < 0)
2130Sstevel@tonic-gate goto errout;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate if (fields & T_OPT) {
2160Sstevel@tonic-gate if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
2170Sstevel@tonic-gate /* compensate for XTI level options */
2180Sstevel@tonic-gate optsize = info.OPT_size +
2190Sstevel@tonic-gate TX_XTI_LEVEL_MAX_OPTBUF;
2200Sstevel@tonic-gate else
2210Sstevel@tonic-gate optsize = info.OPT_size;
2220Sstevel@tonic-gate if (_alloc_buf(&p.uderr->opt, optsize,
223*11134SCasper.Dik@Sun.COM fields, api_semantics, B_FALSE) < 0)
2240Sstevel@tonic-gate goto errout;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2270Sstevel@tonic-gate return ((char *)p.uderr);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate case T_INFO:
230132Srobinson if ((p.info = calloc(1, sizeof (struct t_info))) == NULL)
231132Srobinson goto errout;
2320Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2330Sstevel@tonic-gate return ((char *)p.info);
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate default:
2360Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
2370Sstevel@tonic-gate t_errno = TNOSTRUCTYPE;
2380Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2390Sstevel@tonic-gate } else { /* TX_TLI_API */
2400Sstevel@tonic-gate t_errno = TSYSERR;
2410Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2420Sstevel@tonic-gate errno = EINVAL;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate return (NULL);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate * Clean up. Set t_errno to TSYSERR.
2490Sstevel@tonic-gate * If it is because memory could not be allocated
2500Sstevel@tonic-gate * then errno already should have been set to
2510Sstevel@tonic-gate * ENOMEM
2520Sstevel@tonic-gate */
2530Sstevel@tonic-gate errout:
2540Sstevel@tonic-gate if (p.caddr)
2550Sstevel@tonic-gate (void) t_free(p.caddr, struct_type);
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate t_errno = TSYSERR;
2580Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2590Sstevel@tonic-gate return (NULL);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate static int
_alloc_buf(struct netbuf * buf,t_scalar_t n,int fields,int api_semantics,boolean_t option)263*11134SCasper.Dik@Sun.COM _alloc_buf(struct netbuf *buf, t_scalar_t n, int fields, int api_semantics,
264*11134SCasper.Dik@Sun.COM boolean_t option)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate switch (n) {
2670Sstevel@tonic-gate case T_INFINITE /* -1 */:
2680Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
2690Sstevel@tonic-gate buf->buf = NULL;
2700Sstevel@tonic-gate buf->maxlen = 0;
2710Sstevel@tonic-gate if (fields != T_ALL) {
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * Do not return error
2740Sstevel@tonic-gate * if T_ALL is used.
2750Sstevel@tonic-gate */
2760Sstevel@tonic-gate errno = EINVAL;
2770Sstevel@tonic-gate return (-1);
2780Sstevel@tonic-gate }
279*11134SCasper.Dik@Sun.COM } else if (option) { /* TX_TLI_API */
280*11134SCasper.Dik@Sun.COM static size_t infalloc;
281*11134SCasper.Dik@Sun.COM
282*11134SCasper.Dik@Sun.COM /*
283*11134SCasper.Dik@Sun.COM * retain TLI behavior; ucred_t can vary in size,
284*11134SCasper.Dik@Sun.COM * we need to make sure that we can receive one.
285*11134SCasper.Dik@Sun.COM */
286*11134SCasper.Dik@Sun.COM if (infalloc == 0) {
287*11134SCasper.Dik@Sun.COM size_t uc = ucred_size();
288*11134SCasper.Dik@Sun.COM if (uc < 1024/2)
289*11134SCasper.Dik@Sun.COM infalloc = 1024;
290*11134SCasper.Dik@Sun.COM else
291*11134SCasper.Dik@Sun.COM infalloc = uc + 1024/2;
292*11134SCasper.Dik@Sun.COM }
293*11134SCasper.Dik@Sun.COM if ((buf->buf = calloc(1, infalloc)) == NULL) {
294*11134SCasper.Dik@Sun.COM errno = ENOMEM;
295*11134SCasper.Dik@Sun.COM return (-1);
296*11134SCasper.Dik@Sun.COM } else
297*11134SCasper.Dik@Sun.COM buf->maxlen = infalloc;
2980Sstevel@tonic-gate } else { /* TX_TLI_API */
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate * retain TLI behavior
3010Sstevel@tonic-gate */
3020Sstevel@tonic-gate if ((buf->buf = calloc(1, 1024)) == NULL) {
3030Sstevel@tonic-gate errno = ENOMEM;
3040Sstevel@tonic-gate return (-1);
3050Sstevel@tonic-gate } else
3060Sstevel@tonic-gate buf->maxlen = 1024;
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate break;
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate case 0:
3110Sstevel@tonic-gate buf->buf = NULL;
3120Sstevel@tonic-gate buf->maxlen = 0;
3130Sstevel@tonic-gate break;
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate case T_INVALID /* -2 */:
3160Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
3170Sstevel@tonic-gate buf->buf = NULL;
3180Sstevel@tonic-gate buf->maxlen = 0;
3190Sstevel@tonic-gate if (fields != T_ALL) {
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate * Do not return error
3220Sstevel@tonic-gate * if T_ALL is used.
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate errno = EINVAL;
3250Sstevel@tonic-gate return (-1);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate } else { /* TX_TLI_API */
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate * retain TLI behavior
3300Sstevel@tonic-gate */
3310Sstevel@tonic-gate buf->buf = NULL;
3320Sstevel@tonic-gate buf->maxlen = 0;
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate break;
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate default:
3370Sstevel@tonic-gate if ((buf->buf = calloc(1, (size_t)n)) == NULL) {
3380Sstevel@tonic-gate errno = ENOMEM;
3390Sstevel@tonic-gate return (-1);
3400Sstevel@tonic-gate } else
3410Sstevel@tonic-gate buf->maxlen = n;
3420Sstevel@tonic-gate break;
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate return (0);
3450Sstevel@tonic-gate }
346