xref: /onnv-gate/usr/src/lib/libnsl/nsl/t_alloc.c (revision 1219:f89f56c2d9ac)
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  */
22132Srobinson 
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*1219Sraf  * Copyright 2006 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.4.1.2 */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "mt.h"
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <stropts.h>
370Sstevel@tonic-gate #include <sys/stream.h>
380Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
390Sstevel@tonic-gate #include <sys/tihdr.h>
400Sstevel@tonic-gate #include <sys/timod.h>
410Sstevel@tonic-gate #include <xti.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <signal.h>
450Sstevel@tonic-gate #include "tx.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Function protoypes
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate static int _alloc_buf(struct netbuf *buf, t_scalar_t n, int fields,
510Sstevel@tonic-gate     int api_semantics);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate char *
540Sstevel@tonic-gate _tx_alloc(int fd, int struct_type, int fields, int api_semantics)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	struct strioctl strioc;
570Sstevel@tonic-gate 	struct T_info_ack info;
580Sstevel@tonic-gate 	union structptrs {
590Sstevel@tonic-gate 		char	*caddr;
600Sstevel@tonic-gate 		struct t_bind *bind;
610Sstevel@tonic-gate 		struct t_call *call;
620Sstevel@tonic-gate 		struct t_discon *dis;
630Sstevel@tonic-gate 		struct t_optmgmt *opt;
640Sstevel@tonic-gate 		struct t_unitdata *udata;
650Sstevel@tonic-gate 		struct t_uderr *uderr;
660Sstevel@tonic-gate 		struct t_info *info;
670Sstevel@tonic-gate 	} p;
680Sstevel@tonic-gate 	unsigned int dsize;
690Sstevel@tonic-gate 	struct _ti_user *tiptr;
700Sstevel@tonic-gate 	int retval, sv_errno;
710Sstevel@tonic-gate 	t_scalar_t optsize;
720Sstevel@tonic-gate 
73132Srobinson 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
740Sstevel@tonic-gate 		return (NULL);
750Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * Get size info for T_ADDR, T_OPT, and T_UDATA fields
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	info.PRIM_type = T_INFO_REQ;
810Sstevel@tonic-gate 	strioc.ic_cmd = TI_GETINFO;
820Sstevel@tonic-gate 	strioc.ic_timout = -1;
830Sstevel@tonic-gate 	strioc.ic_len = (int)sizeof (struct T_info_req);
840Sstevel@tonic-gate 	strioc.ic_dp = (char *)&info;
850Sstevel@tonic-gate 	do {
86*1219Sraf 		retval = ioctl(fd, I_STR, &strioc);
870Sstevel@tonic-gate 	} while (retval < 0 && errno == EINTR);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (retval < 0) {
900Sstevel@tonic-gate 		sv_errno = errno;
910Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
920Sstevel@tonic-gate 		t_errno = TSYSERR;
930Sstevel@tonic-gate 		errno = sv_errno;
940Sstevel@tonic-gate 		return (NULL);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if (strioc.ic_len != (int)sizeof (struct T_info_ack)) {
980Sstevel@tonic-gate 		t_errno = TSYSERR;
990Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1000Sstevel@tonic-gate 		errno = EIO;
1010Sstevel@tonic-gate 		return (NULL);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	/*
1060Sstevel@tonic-gate 	 * Malloc appropriate structure and the specified
1070Sstevel@tonic-gate 	 * fields within each structure.  Initialize the
1080Sstevel@tonic-gate 	 * 'buf' and 'maxlen' fields of each.
1090Sstevel@tonic-gate 	 */
1100Sstevel@tonic-gate 	switch (struct_type) {
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	case T_BIND:
113132Srobinson 		if ((p.bind = calloc(1, sizeof (struct t_bind))) == NULL)
114132Srobinson 			goto errout;
1150Sstevel@tonic-gate 		if (fields & T_ADDR) {
1160Sstevel@tonic-gate 			if (_alloc_buf(&p.bind->addr,
1170Sstevel@tonic-gate 			    info.ADDR_size,
1180Sstevel@tonic-gate 			    fields, api_semantics) < 0)
1190Sstevel@tonic-gate 				goto errout;
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1220Sstevel@tonic-gate 		return ((char *)p.bind);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	case T_CALL:
125132Srobinson 		if ((p.call = calloc(1, sizeof (struct t_call))) == NULL)
126132Srobinson 			goto errout;
1270Sstevel@tonic-gate 		if (fields & T_ADDR) {
1280Sstevel@tonic-gate 			if (_alloc_buf(&p.call->addr,
1290Sstevel@tonic-gate 			    info.ADDR_size,
1300Sstevel@tonic-gate 			    fields, api_semantics) < 0)
1310Sstevel@tonic-gate 				goto errout;
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 		if (fields & T_OPT) {
1340Sstevel@tonic-gate 			if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1350Sstevel@tonic-gate 				/* compensate for XTI level options */
1360Sstevel@tonic-gate 				optsize = info.OPT_size +
1370Sstevel@tonic-gate 				    TX_XTI_LEVEL_MAX_OPTBUF;
1380Sstevel@tonic-gate 			else
1390Sstevel@tonic-gate 				optsize = info.OPT_size;
1400Sstevel@tonic-gate 			if (_alloc_buf(&p.call->opt, optsize,
1410Sstevel@tonic-gate 			    fields, api_semantics) < 0)
1420Sstevel@tonic-gate 				goto errout;
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 		if (fields & T_UDATA) {
1450Sstevel@tonic-gate 			dsize = _T_MAX((int)info.CDATA_size,
1460Sstevel@tonic-gate 					(int)info.DDATA_size);
1470Sstevel@tonic-gate 			if (_alloc_buf(&p.call->udata, (t_scalar_t)dsize,
1480Sstevel@tonic-gate 					fields, api_semantics) < 0)
1490Sstevel@tonic-gate 				goto errout;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1520Sstevel@tonic-gate 		return ((char *)p.call);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	case T_OPTMGMT:
155132Srobinson 		if ((p.opt = calloc(1, sizeof (struct t_optmgmt))) == NULL)
156132Srobinson 			goto errout;
1570Sstevel@tonic-gate 		if (fields & T_OPT) {
1580Sstevel@tonic-gate 			if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1590Sstevel@tonic-gate 				/* compensate for XTI level options */
1600Sstevel@tonic-gate 				optsize = info.OPT_size +
1610Sstevel@tonic-gate 				    TX_XTI_LEVEL_MAX_OPTBUF;
1620Sstevel@tonic-gate 			else
1630Sstevel@tonic-gate 				optsize = info.OPT_size;
1640Sstevel@tonic-gate 			if (_alloc_buf(&p.opt->opt, optsize,
1650Sstevel@tonic-gate 				fields, api_semantics) < 0)
1660Sstevel@tonic-gate 				goto errout;
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1690Sstevel@tonic-gate 		return ((char *)p.opt);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	case T_DIS:
172132Srobinson 		if ((p.dis = calloc(1, sizeof (struct t_discon))) == NULL)
1730Sstevel@tonic-gate 			goto errout;
1740Sstevel@tonic-gate 		if (fields & T_UDATA) {
1750Sstevel@tonic-gate 			if (_alloc_buf(&p.dis->udata, info.DDATA_size,
1760Sstevel@tonic-gate 				fields, api_semantics) < 0)
1770Sstevel@tonic-gate 				goto errout;
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1800Sstevel@tonic-gate 		return ((char *)p.dis);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	case T_UNITDATA:
183132Srobinson 		if ((p.udata = calloc(1, sizeof (struct t_unitdata))) == NULL)
184132Srobinson 			goto errout;
1850Sstevel@tonic-gate 		if (fields & T_ADDR) {
1860Sstevel@tonic-gate 			if (_alloc_buf(&p.udata->addr, info.ADDR_size,
1870Sstevel@tonic-gate 				fields, api_semantics) < 0)
1880Sstevel@tonic-gate 				goto errout;
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 		if (fields & T_OPT) {
1910Sstevel@tonic-gate 			if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1920Sstevel@tonic-gate 				/* compensate for XTI level options */
1930Sstevel@tonic-gate 				optsize = info.OPT_size +
1940Sstevel@tonic-gate 				    TX_XTI_LEVEL_MAX_OPTBUF;
1950Sstevel@tonic-gate 			else
1960Sstevel@tonic-gate 				optsize = info.OPT_size;
1970Sstevel@tonic-gate 			if (_alloc_buf(&p.udata->opt, optsize,
1980Sstevel@tonic-gate 				fields, api_semantics) < 0)
1990Sstevel@tonic-gate 				goto errout;
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 		if (fields & T_UDATA) {
2020Sstevel@tonic-gate 			if (_alloc_buf(&p.udata->udata, info.TSDU_size,
2030Sstevel@tonic-gate 				fields, api_semantics) < 0)
2040Sstevel@tonic-gate 				goto errout;
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
2070Sstevel@tonic-gate 		return ((char *)p.udata);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	case T_UDERROR:
210132Srobinson 		if ((p.uderr = calloc(1, sizeof (struct t_uderr))) == NULL)
211132Srobinson 			goto errout;
2120Sstevel@tonic-gate 		if (fields & T_ADDR) {
2130Sstevel@tonic-gate 			if (_alloc_buf(&p.uderr->addr, info.ADDR_size,
2140Sstevel@tonic-gate 				fields, api_semantics) < 0)
2150Sstevel@tonic-gate 				goto errout;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		if (fields & T_OPT) {
2180Sstevel@tonic-gate 			if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
2190Sstevel@tonic-gate 				/* compensate for XTI level options */
2200Sstevel@tonic-gate 				optsize = info.OPT_size +
2210Sstevel@tonic-gate 				    TX_XTI_LEVEL_MAX_OPTBUF;
2220Sstevel@tonic-gate 			else
2230Sstevel@tonic-gate 				optsize = info.OPT_size;
2240Sstevel@tonic-gate 			if (_alloc_buf(&p.uderr->opt, optsize,
2250Sstevel@tonic-gate 				fields, api_semantics) < 0)
2260Sstevel@tonic-gate 				goto errout;
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
2290Sstevel@tonic-gate 		return ((char *)p.uderr);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	case T_INFO:
232132Srobinson 		if ((p.info = calloc(1, sizeof (struct t_info))) == NULL)
233132Srobinson 			goto errout;
2340Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
2350Sstevel@tonic-gate 		return ((char *)p.info);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	default:
2380Sstevel@tonic-gate 		if (_T_IS_XTI(api_semantics)) {
2390Sstevel@tonic-gate 			t_errno = TNOSTRUCTYPE;
2400Sstevel@tonic-gate 			sig_mutex_unlock(&tiptr->ti_lock);
2410Sstevel@tonic-gate 		} else {	/* TX_TLI_API */
2420Sstevel@tonic-gate 			t_errno = TSYSERR;
2430Sstevel@tonic-gate 			sig_mutex_unlock(&tiptr->ti_lock);
2440Sstevel@tonic-gate 			errno = EINVAL;
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 		return (NULL);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/*
2500Sstevel@tonic-gate 	 * Clean up. Set t_errno to TSYSERR.
2510Sstevel@tonic-gate 	 * If it is because memory could not be allocated
2520Sstevel@tonic-gate 	 * then errno already should have been set to
2530Sstevel@tonic-gate 	 * ENOMEM
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate errout:
2560Sstevel@tonic-gate 	if (p.caddr)
2570Sstevel@tonic-gate 		(void) t_free(p.caddr, struct_type);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	t_errno = TSYSERR;
2600Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
2610Sstevel@tonic-gate 	return (NULL);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate static int
2650Sstevel@tonic-gate _alloc_buf(struct netbuf *buf, t_scalar_t n, int fields, int api_semantics)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	switch (n) {
2680Sstevel@tonic-gate 	case T_INFINITE /* -1 */:
2690Sstevel@tonic-gate 		if (_T_IS_XTI(api_semantics)) {
2700Sstevel@tonic-gate 			buf->buf = NULL;
2710Sstevel@tonic-gate 			buf->maxlen = 0;
2720Sstevel@tonic-gate 			if (fields != T_ALL) {
2730Sstevel@tonic-gate 				/*
2740Sstevel@tonic-gate 				 * Do not return error
2750Sstevel@tonic-gate 				 * if T_ALL is used.
2760Sstevel@tonic-gate 				 */
2770Sstevel@tonic-gate 				errno = EINVAL;
2780Sstevel@tonic-gate 				return (-1);
2790Sstevel@tonic-gate 			}
2800Sstevel@tonic-gate 		} else {	/* TX_TLI_API */
2810Sstevel@tonic-gate 			/*
2820Sstevel@tonic-gate 			 * retain TLI behavior
2830Sstevel@tonic-gate 			 */
2840Sstevel@tonic-gate 			if ((buf->buf = calloc(1, 1024)) == NULL) {
2850Sstevel@tonic-gate 				errno = ENOMEM;
2860Sstevel@tonic-gate 				return (-1);
2870Sstevel@tonic-gate 			} else
2880Sstevel@tonic-gate 				buf->maxlen = 1024;
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 		break;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	case 0:
2930Sstevel@tonic-gate 		buf->buf = NULL;
2940Sstevel@tonic-gate 		buf->maxlen = 0;
2950Sstevel@tonic-gate 		break;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	case T_INVALID /* -2 */:
2980Sstevel@tonic-gate 		if (_T_IS_XTI(api_semantics)) {
2990Sstevel@tonic-gate 			buf->buf = NULL;
3000Sstevel@tonic-gate 			buf->maxlen = 0;
3010Sstevel@tonic-gate 			if (fields != T_ALL) {
3020Sstevel@tonic-gate 				/*
3030Sstevel@tonic-gate 				 * Do not return error
3040Sstevel@tonic-gate 				 * if T_ALL is used.
3050Sstevel@tonic-gate 				 */
3060Sstevel@tonic-gate 				errno = EINVAL;
3070Sstevel@tonic-gate 				return (-1);
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 		} else {	/* TX_TLI_API */
3100Sstevel@tonic-gate 			/*
3110Sstevel@tonic-gate 			 * retain TLI behavior
3120Sstevel@tonic-gate 			 */
3130Sstevel@tonic-gate 			buf->buf = NULL;
3140Sstevel@tonic-gate 			buf->maxlen = 0;
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 		break;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	default:
3190Sstevel@tonic-gate 		if ((buf->buf = calloc(1, (size_t)n)) == NULL) {
3200Sstevel@tonic-gate 			errno = ENOMEM;
3210Sstevel@tonic-gate 			return (-1);
3220Sstevel@tonic-gate 		} else
3230Sstevel@tonic-gate 			buf->maxlen = n;
3240Sstevel@tonic-gate 		break;
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 	return (0);
3270Sstevel@tonic-gate }
328