xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_alloc.c (revision 67dbe2be)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*67dbe2beSCasper H.S. Dik  * Common Development and Distribution License (the "License").
6*67dbe2beSCasper H.S. Dik  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*67dbe2beSCasper H.S. Dik  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "mt.h"
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <stropts.h>
347c478bd9Sstevel@tonic-gate #include <sys/stream.h>
357c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
367c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
377c478bd9Sstevel@tonic-gate #include <sys/timod.h>
387c478bd9Sstevel@tonic-gate #include <xti.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
41*67dbe2beSCasper H.S. Dik #include <ucred.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include "tx.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Function protoypes
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate static int _alloc_buf(struct netbuf *buf, t_scalar_t n, int fields,
49*67dbe2beSCasper H.S. Dik     int api_semantics, boolean_t option);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate char *
_tx_alloc(int fd,int struct_type,int fields,int api_semantics)527c478bd9Sstevel@tonic-gate _tx_alloc(int fd, int struct_type, int fields, int api_semantics)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	struct strioctl strioc;
557c478bd9Sstevel@tonic-gate 	struct T_info_ack info;
567c478bd9Sstevel@tonic-gate 	union structptrs {
577c478bd9Sstevel@tonic-gate 		char	*caddr;
587c478bd9Sstevel@tonic-gate 		struct t_bind *bind;
597c478bd9Sstevel@tonic-gate 		struct t_call *call;
607c478bd9Sstevel@tonic-gate 		struct t_discon *dis;
617c478bd9Sstevel@tonic-gate 		struct t_optmgmt *opt;
627c478bd9Sstevel@tonic-gate 		struct t_unitdata *udata;
637c478bd9Sstevel@tonic-gate 		struct t_uderr *uderr;
647c478bd9Sstevel@tonic-gate 		struct t_info *info;
657c478bd9Sstevel@tonic-gate 	} p;
667c478bd9Sstevel@tonic-gate 	unsigned int dsize;
677c478bd9Sstevel@tonic-gate 	struct _ti_user *tiptr;
687c478bd9Sstevel@tonic-gate 	int retval, sv_errno;
697c478bd9Sstevel@tonic-gate 	t_scalar_t optsize;
707c478bd9Sstevel@tonic-gate 
7161961e0fSrobinson 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
727c478bd9Sstevel@tonic-gate 		return (NULL);
737c478bd9Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/*
767c478bd9Sstevel@tonic-gate 	 * Get size info for T_ADDR, T_OPT, and T_UDATA fields
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	info.PRIM_type = T_INFO_REQ;
797c478bd9Sstevel@tonic-gate 	strioc.ic_cmd = TI_GETINFO;
807c478bd9Sstevel@tonic-gate 	strioc.ic_timout = -1;
817c478bd9Sstevel@tonic-gate 	strioc.ic_len = (int)sizeof (struct T_info_req);
827c478bd9Sstevel@tonic-gate 	strioc.ic_dp = (char *)&info;
837c478bd9Sstevel@tonic-gate 	do {
84e8031f0aSraf 		retval = ioctl(fd, I_STR, &strioc);
857c478bd9Sstevel@tonic-gate 	} while (retval < 0 && errno == EINTR);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (retval < 0) {
887c478bd9Sstevel@tonic-gate 		sv_errno = errno;
897c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
907c478bd9Sstevel@tonic-gate 		t_errno = TSYSERR;
917c478bd9Sstevel@tonic-gate 		errno = sv_errno;
927c478bd9Sstevel@tonic-gate 		return (NULL);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (strioc.ic_len != (int)sizeof (struct T_info_ack)) {
967c478bd9Sstevel@tonic-gate 		t_errno = TSYSERR;
977c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
987c478bd9Sstevel@tonic-gate 		errno = EIO;
997c478bd9Sstevel@tonic-gate 		return (NULL);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * Malloc appropriate structure and the specified
1057c478bd9Sstevel@tonic-gate 	 * fields within each structure.  Initialize the
1067c478bd9Sstevel@tonic-gate 	 * 'buf' and 'maxlen' fields of each.
1077c478bd9Sstevel@tonic-gate 	 */
1087c478bd9Sstevel@tonic-gate 	switch (struct_type) {
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	case T_BIND:
11161961e0fSrobinson 		if ((p.bind = calloc(1, sizeof (struct t_bind))) == NULL)
11261961e0fSrobinson 			goto errout;
1137c478bd9Sstevel@tonic-gate 		if (fields & T_ADDR) {
1147c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.bind->addr,
1157c478bd9Sstevel@tonic-gate 			    info.ADDR_size,
116*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_FALSE) < 0)
1177c478bd9Sstevel@tonic-gate 				goto errout;
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1207c478bd9Sstevel@tonic-gate 		return ((char *)p.bind);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	case T_CALL:
12361961e0fSrobinson 		if ((p.call = calloc(1, sizeof (struct t_call))) == NULL)
12461961e0fSrobinson 			goto errout;
1257c478bd9Sstevel@tonic-gate 		if (fields & T_ADDR) {
1267c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.call->addr,
1277c478bd9Sstevel@tonic-gate 			    info.ADDR_size,
128*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_FALSE) < 0)
1297c478bd9Sstevel@tonic-gate 				goto errout;
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 		if (fields & T_OPT) {
1327c478bd9Sstevel@tonic-gate 			if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1337c478bd9Sstevel@tonic-gate 				/* compensate for XTI level options */
1347c478bd9Sstevel@tonic-gate 				optsize = info.OPT_size +
1357c478bd9Sstevel@tonic-gate 				    TX_XTI_LEVEL_MAX_OPTBUF;
1367c478bd9Sstevel@tonic-gate 			else
1377c478bd9Sstevel@tonic-gate 				optsize = info.OPT_size;
1387c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.call->opt, optsize,
139*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_TRUE) < 0)
1407c478bd9Sstevel@tonic-gate 				goto errout;
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		if (fields & T_UDATA) {
1437c478bd9Sstevel@tonic-gate 			dsize = _T_MAX((int)info.CDATA_size,
144*67dbe2beSCasper H.S. Dik 			    (int)info.DDATA_size);
1457c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.call->udata, (t_scalar_t)dsize,
146*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_FALSE) < 0)
1477c478bd9Sstevel@tonic-gate 				goto errout;
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1507c478bd9Sstevel@tonic-gate 		return ((char *)p.call);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	case T_OPTMGMT:
15361961e0fSrobinson 		if ((p.opt = calloc(1, sizeof (struct t_optmgmt))) == NULL)
15461961e0fSrobinson 			goto errout;
1557c478bd9Sstevel@tonic-gate 		if (fields & T_OPT) {
1567c478bd9Sstevel@tonic-gate 			if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1577c478bd9Sstevel@tonic-gate 				/* compensate for XTI level options */
1587c478bd9Sstevel@tonic-gate 				optsize = info.OPT_size +
1597c478bd9Sstevel@tonic-gate 				    TX_XTI_LEVEL_MAX_OPTBUF;
1607c478bd9Sstevel@tonic-gate 			else
1617c478bd9Sstevel@tonic-gate 				optsize = info.OPT_size;
1627c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.opt->opt, optsize,
163*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_TRUE) < 0)
1647c478bd9Sstevel@tonic-gate 				goto errout;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1677c478bd9Sstevel@tonic-gate 		return ((char *)p.opt);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	case T_DIS:
17061961e0fSrobinson 		if ((p.dis = calloc(1, sizeof (struct t_discon))) == NULL)
1717c478bd9Sstevel@tonic-gate 			goto errout;
1727c478bd9Sstevel@tonic-gate 		if (fields & T_UDATA) {
1737c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.dis->udata, info.DDATA_size,
174*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_FALSE) < 0)
1757c478bd9Sstevel@tonic-gate 				goto errout;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1787c478bd9Sstevel@tonic-gate 		return ((char *)p.dis);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	case T_UNITDATA:
18161961e0fSrobinson 		if ((p.udata = calloc(1, sizeof (struct t_unitdata))) == NULL)
18261961e0fSrobinson 			goto errout;
1837c478bd9Sstevel@tonic-gate 		if (fields & T_ADDR) {
1847c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.udata->addr, info.ADDR_size,
185*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_FALSE) < 0)
1867c478bd9Sstevel@tonic-gate 				goto errout;
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		if (fields & T_OPT) {
1897c478bd9Sstevel@tonic-gate 			if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
1907c478bd9Sstevel@tonic-gate 				/* compensate for XTI level options */
1917c478bd9Sstevel@tonic-gate 				optsize = info.OPT_size +
1927c478bd9Sstevel@tonic-gate 				    TX_XTI_LEVEL_MAX_OPTBUF;
1937c478bd9Sstevel@tonic-gate 			else
1947c478bd9Sstevel@tonic-gate 				optsize = info.OPT_size;
1957c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.udata->opt, optsize,
196*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_TRUE) < 0)
1977c478bd9Sstevel@tonic-gate 				goto errout;
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		if (fields & T_UDATA) {
2007c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.udata->udata, info.TSDU_size,
201*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_FALSE) < 0)
2027c478bd9Sstevel@tonic-gate 				goto errout;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
2057c478bd9Sstevel@tonic-gate 		return ((char *)p.udata);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	case T_UDERROR:
20861961e0fSrobinson 		if ((p.uderr = calloc(1, sizeof (struct t_uderr))) == NULL)
20961961e0fSrobinson 			goto errout;
2107c478bd9Sstevel@tonic-gate 		if (fields & T_ADDR) {
2117c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.uderr->addr, info.ADDR_size,
212*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_FALSE) < 0)
2137c478bd9Sstevel@tonic-gate 				goto errout;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		if (fields & T_OPT) {
2167c478bd9Sstevel@tonic-gate 			if (info.OPT_size >= 0 && _T_IS_XTI(api_semantics))
2177c478bd9Sstevel@tonic-gate 				/* compensate for XTI level options */
2187c478bd9Sstevel@tonic-gate 				optsize = info.OPT_size +
2197c478bd9Sstevel@tonic-gate 				    TX_XTI_LEVEL_MAX_OPTBUF;
2207c478bd9Sstevel@tonic-gate 			else
2217c478bd9Sstevel@tonic-gate 				optsize = info.OPT_size;
2227c478bd9Sstevel@tonic-gate 			if (_alloc_buf(&p.uderr->opt, optsize,
223*67dbe2beSCasper H.S. Dik 			    fields, api_semantics, B_FALSE) < 0)
2247c478bd9Sstevel@tonic-gate 				goto errout;
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
2277c478bd9Sstevel@tonic-gate 		return ((char *)p.uderr);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	case T_INFO:
23061961e0fSrobinson 		if ((p.info = calloc(1, sizeof (struct t_info))) == NULL)
23161961e0fSrobinson 			goto errout;
2327c478bd9Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
2337c478bd9Sstevel@tonic-gate 		return ((char *)p.info);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	default:
2367c478bd9Sstevel@tonic-gate 		if (_T_IS_XTI(api_semantics)) {
2377c478bd9Sstevel@tonic-gate 			t_errno = TNOSTRUCTYPE;
2387c478bd9Sstevel@tonic-gate 			sig_mutex_unlock(&tiptr->ti_lock);
2397c478bd9Sstevel@tonic-gate 		} else {	/* TX_TLI_API */
2407c478bd9Sstevel@tonic-gate 			t_errno = TSYSERR;
2417c478bd9Sstevel@tonic-gate 			sig_mutex_unlock(&tiptr->ti_lock);
2427c478bd9Sstevel@tonic-gate 			errno = EINVAL;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		return (NULL);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * Clean up. Set t_errno to TSYSERR.
2497c478bd9Sstevel@tonic-gate 	 * If it is because memory could not be allocated
2507c478bd9Sstevel@tonic-gate 	 * then errno already should have been set to
2517c478bd9Sstevel@tonic-gate 	 * ENOMEM
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate errout:
2547c478bd9Sstevel@tonic-gate 	if (p.caddr)
2557c478bd9Sstevel@tonic-gate 		(void) t_free(p.caddr, struct_type);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	t_errno = TSYSERR;
2587c478bd9Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
2597c478bd9Sstevel@tonic-gate 	return (NULL);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static int
_alloc_buf(struct netbuf * buf,t_scalar_t n,int fields,int api_semantics,boolean_t option)263*67dbe2beSCasper H.S. Dik _alloc_buf(struct netbuf *buf, t_scalar_t n, int fields, int api_semantics,
264*67dbe2beSCasper H.S. Dik     boolean_t option)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	switch (n) {
2677c478bd9Sstevel@tonic-gate 	case T_INFINITE /* -1 */:
2687c478bd9Sstevel@tonic-gate 		if (_T_IS_XTI(api_semantics)) {
2697c478bd9Sstevel@tonic-gate 			buf->buf = NULL;
2707c478bd9Sstevel@tonic-gate 			buf->maxlen = 0;
2717c478bd9Sstevel@tonic-gate 			if (fields != T_ALL) {
2727c478bd9Sstevel@tonic-gate 				/*
2737c478bd9Sstevel@tonic-gate 				 * Do not return error
2747c478bd9Sstevel@tonic-gate 				 * if T_ALL is used.
2757c478bd9Sstevel@tonic-gate 				 */
2767c478bd9Sstevel@tonic-gate 				errno = EINVAL;
2777c478bd9Sstevel@tonic-gate 				return (-1);
2787c478bd9Sstevel@tonic-gate 			}
279*67dbe2beSCasper H.S. Dik 		} else if (option) { /* TX_TLI_API */
280*67dbe2beSCasper H.S. Dik 			static size_t infalloc;
281*67dbe2beSCasper H.S. Dik 
282*67dbe2beSCasper H.S. Dik 			/*
283*67dbe2beSCasper H.S. Dik 			 * retain TLI behavior; ucred_t can vary in size,
284*67dbe2beSCasper H.S. Dik 			 * we need to make sure that we can receive one.
285*67dbe2beSCasper H.S. Dik 			 */
286*67dbe2beSCasper H.S. Dik 			if (infalloc == 0) {
287*67dbe2beSCasper H.S. Dik 				size_t uc = ucred_size();
288*67dbe2beSCasper H.S. Dik 				if (uc < 1024/2)
289*67dbe2beSCasper H.S. Dik 					infalloc = 1024;
290*67dbe2beSCasper H.S. Dik 				else
291*67dbe2beSCasper H.S. Dik 					infalloc = uc + 1024/2;
292*67dbe2beSCasper H.S. Dik 			}
293*67dbe2beSCasper H.S. Dik 			if ((buf->buf = calloc(1, infalloc)) == NULL) {
294*67dbe2beSCasper H.S. Dik 				errno = ENOMEM;
295*67dbe2beSCasper H.S. Dik 				return (-1);
296*67dbe2beSCasper H.S. Dik 			} else
297*67dbe2beSCasper H.S. Dik 				buf->maxlen = infalloc;
2987c478bd9Sstevel@tonic-gate 		} else {	/* TX_TLI_API */
2997c478bd9Sstevel@tonic-gate 			/*
3007c478bd9Sstevel@tonic-gate 			 * retain TLI behavior
3017c478bd9Sstevel@tonic-gate 			 */
3027c478bd9Sstevel@tonic-gate 			if ((buf->buf = calloc(1, 1024)) == NULL) {
3037c478bd9Sstevel@tonic-gate 				errno = ENOMEM;
3047c478bd9Sstevel@tonic-gate 				return (-1);
3057c478bd9Sstevel@tonic-gate 			} else
3067c478bd9Sstevel@tonic-gate 				buf->maxlen = 1024;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 		break;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	case 0:
3117c478bd9Sstevel@tonic-gate 		buf->buf = NULL;
3127c478bd9Sstevel@tonic-gate 		buf->maxlen = 0;
3137c478bd9Sstevel@tonic-gate 		break;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	case T_INVALID /* -2 */:
3167c478bd9Sstevel@tonic-gate 		if (_T_IS_XTI(api_semantics)) {
3177c478bd9Sstevel@tonic-gate 			buf->buf = NULL;
3187c478bd9Sstevel@tonic-gate 			buf->maxlen = 0;
3197c478bd9Sstevel@tonic-gate 			if (fields != T_ALL) {
3207c478bd9Sstevel@tonic-gate 				/*
3217c478bd9Sstevel@tonic-gate 				 * Do not return error
3227c478bd9Sstevel@tonic-gate 				 * if T_ALL is used.
3237c478bd9Sstevel@tonic-gate 				 */
3247c478bd9Sstevel@tonic-gate 				errno = EINVAL;
3257c478bd9Sstevel@tonic-gate 				return (-1);
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 		} else {	/* TX_TLI_API */
3287c478bd9Sstevel@tonic-gate 			/*
3297c478bd9Sstevel@tonic-gate 			 * retain TLI behavior
3307c478bd9Sstevel@tonic-gate 			 */
3317c478bd9Sstevel@tonic-gate 			buf->buf = NULL;
3327c478bd9Sstevel@tonic-gate 			buf->maxlen = 0;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		break;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	default:
3377c478bd9Sstevel@tonic-gate 		if ((buf->buf = calloc(1, (size_t)n)) == NULL) {
3387c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
3397c478bd9Sstevel@tonic-gate 			return (-1);
3407c478bd9Sstevel@tonic-gate 		} else
3417c478bd9Sstevel@tonic-gate 			buf->maxlen = n;
3427c478bd9Sstevel@tonic-gate 		break;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	return (0);
3457c478bd9Sstevel@tonic-gate }
346