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*4754Svp157776  * Common Development and Distribution License (the "License").
6*4754Svp157776  * 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  */
210Sstevel@tonic-gate /*
22*4754Svp157776  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Contains routines that deal with TLI/XTI endpoints and rpc services.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <libintl.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <netconfig.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <sys/sockio.h>
420Sstevel@tonic-gate #include "inetd_impl.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate uu_list_pool_t *conn_ind_pool = NULL;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * RPC functions.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Returns B_TRUE if the non-address components of the 2 rpc_info_t structures
520Sstevel@tonic-gate  * are equivalent, else B_FALSE.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate boolean_t
550Sstevel@tonic-gate rpc_info_equal(const rpc_info_t *ri, const rpc_info_t *ri2)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	return ((ri->prognum == ri2->prognum) &&
580Sstevel@tonic-gate 	    (ri->lowver == ri2->lowver) &&
590Sstevel@tonic-gate 	    (ri->highver == ri2->highver) &&
600Sstevel@tonic-gate 	    (strcmp(ri->netid, ri2->netid) == 0));
610Sstevel@tonic-gate }
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * Determine if we have a configured interface for the specified address
650Sstevel@tonic-gate  * family. This code is a mirror of libnsl's __can_use_af(). We mirror
660Sstevel@tonic-gate  * it because we need an exact duplicate of its behavior, yet the
670Sstevel@tonic-gate  * function isn't exported by libnsl, and this fix is considered short-
680Sstevel@tonic-gate  * term, so it's not worth exporting it.
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  * We need to duplicate __can_use_af() so we can accurately determine
710Sstevel@tonic-gate  * when getnetconfigent() returns failure for a v6 netid due to no IPv6
720Sstevel@tonic-gate  * interfaces being configured: getnetconfigent() returns failure
730Sstevel@tonic-gate  * if a netid is either 'tcp6' or 'udp6' and __can_use_af() returns 0,
740Sstevel@tonic-gate  * but it doesn't return a return code to uniquely determine this
750Sstevel@tonic-gate  * failure. If we don't accurately determine these failures, we could
760Sstevel@tonic-gate  * output error messages in a case when they weren't justified.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate static int
790Sstevel@tonic-gate can_use_af(sa_family_t af)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	struct lifnum	lifn;
820Sstevel@tonic-gate 	int		fd;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if ((fd =  open("/dev/udp", O_RDONLY)) < 0) {
850Sstevel@tonic-gate 		return (0);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	lifn.lifn_family = af;
880Sstevel@tonic-gate 	/* LINTED ECONST_EXPR */
890Sstevel@tonic-gate 	lifn.lifn_flags = IFF_UP & !(IFF_NOXMIT | IFF_DEPRECATED);
900Sstevel@tonic-gate 	if (ioctl(fd, SIOCGLIFNUM, &lifn, sizeof (lifn)) < 0) {
910Sstevel@tonic-gate 		lifn.lifn_count = 0;
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	(void) close(fd);
950Sstevel@tonic-gate 	return (lifn.lifn_count);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static boolean_t
990Sstevel@tonic-gate is_v6_netid(const char *netid)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	return ((strcmp(netid, SOCKET_PROTO_TCP6) == 0) ||
1020Sstevel@tonic-gate 	    (strcmp(netid, SOCKET_PROTO_UDP6) == 0));
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Registers with rpcbind the program number with all versions, from low to
1070Sstevel@tonic-gate  * high, with the netid, all specified in 'rpc'. If registration fails,
1080Sstevel@tonic-gate  * returns -1, else 0.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate int
1110Sstevel@tonic-gate register_rpc_service(const char *fmri, const rpc_info_t *rpc)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	struct netconfig	*nconf;
1140Sstevel@tonic-gate 	int			ver;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	debug_msg("Entering register_rpc_service: instance: %s", fmri);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if ((nconf = getnetconfigent(rpc->netid)) == NULL) {
1190Sstevel@tonic-gate 		/*
1200Sstevel@tonic-gate 		 * Check whether getnetconfigent() failed as a result of
1210Sstevel@tonic-gate 		 * having no IPv6 interfaces configured for a v6 netid, or
1220Sstevel@tonic-gate 		 * as a result of a 'real' error, and output an appropriate
1230Sstevel@tonic-gate 		 * message with an appropriate severity.
1240Sstevel@tonic-gate 		 */
1250Sstevel@tonic-gate 		if (is_v6_netid(rpc->netid) && !can_use_af(AF_INET6)) {
1260Sstevel@tonic-gate 			warn_msg(gettext(
1270Sstevel@tonic-gate 			    "Couldn't register netid %s for RPC instance %s "
1280Sstevel@tonic-gate 			    "because no IPv6 interfaces are plumbed"),
1290Sstevel@tonic-gate 			    rpc->netid, fmri);
1300Sstevel@tonic-gate 		} else {
1310Sstevel@tonic-gate 			error_msg(gettext(
1320Sstevel@tonic-gate 			    "Failed to lookup netid '%s' for instance %s: %s"),
1330Sstevel@tonic-gate 			    rpc->netid, fmri, nc_sperror());
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 		return (-1);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	for (ver = rpc->lowver; ver <= rpc->highver; ver++) {
1390Sstevel@tonic-gate 		if (!rpcb_set(rpc->prognum, ver, nconf, &(rpc->netbuf))) {
1400Sstevel@tonic-gate 			error_msg(gettext("Failed to register version %d "
1410Sstevel@tonic-gate 			    "of RPC service instance %s, netid %s"), ver,
1420Sstevel@tonic-gate 			    fmri, rpc->netid);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 			for (ver--; ver >= rpc->lowver; ver--)
1450Sstevel@tonic-gate 				(void) rpcb_unset(rpc->prognum, ver, nconf);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 			freenetconfigent(nconf);
1480Sstevel@tonic-gate 			return (-1);
1490Sstevel@tonic-gate 		}
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	freenetconfigent(nconf);
1530Sstevel@tonic-gate 	return (0);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /* Unregister all the registrations done by register_rpc_service */
1570Sstevel@tonic-gate void
1580Sstevel@tonic-gate unregister_rpc_service(const char *fmri, const rpc_info_t *rpc)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	int			ver;
1610Sstevel@tonic-gate 	struct netconfig	*nconf;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	debug_msg("Entering unregister_rpc_service, instance: %s", fmri);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if ((nconf = getnetconfigent(rpc->netid)) == NULL) {
1660Sstevel@tonic-gate 		/*
1670Sstevel@tonic-gate 		 * Don't output an error message if getnetconfigent() fails for
1680Sstevel@tonic-gate 		 * a v6 netid when an IPv6 interface isn't configured.
1690Sstevel@tonic-gate 		 */
1700Sstevel@tonic-gate 		if (!(is_v6_netid(rpc->netid) && !can_use_af(AF_INET6))) {
1710Sstevel@tonic-gate 			error_msg(gettext(
1720Sstevel@tonic-gate 			    "Failed to lookup netid '%s' for instance %s: %s"),
1730Sstevel@tonic-gate 			    rpc->netid, fmri, nc_sperror());
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 		return;
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	for (ver = rpc->lowver; ver <= rpc->highver; ver++)
1790Sstevel@tonic-gate 		(void) rpcb_unset(rpc->prognum, ver, nconf);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	freenetconfigent(nconf);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * TLI/XTI functions.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate int
1890Sstevel@tonic-gate tlx_init(void)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	if ((conn_ind_pool = uu_list_pool_create("conn_ind_pool",
1920Sstevel@tonic-gate 	    sizeof (tlx_conn_ind_t), offsetof(tlx_conn_ind_t, link),
1930Sstevel@tonic-gate 	    NULL, UU_LIST_POOL_DEBUG)) == NULL) {
1940Sstevel@tonic-gate 		error_msg("%s: %s", gettext("Failed to create uu pool"),
1950Sstevel@tonic-gate 		    uu_strerror(uu_error()));
1960Sstevel@tonic-gate 		return (-1);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	return (0);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate void
2030Sstevel@tonic-gate tlx_fini(void)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	if (conn_ind_pool != NULL) {
2060Sstevel@tonic-gate 		uu_list_pool_destroy(conn_ind_pool);
2070Sstevel@tonic-gate 		conn_ind_pool = NULL;
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Checks if the contents of the 2 tlx_info_t structures are equivalent.
2130Sstevel@tonic-gate  * If 'isrpc' is false, the address components of the two structures are
2140Sstevel@tonic-gate  * compared for equality as part of this. If the two structures are
2150Sstevel@tonic-gate  * equivalent B_TRUE is returned, else B_FALSE.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate boolean_t
2180Sstevel@tonic-gate tlx_info_equal(const tlx_info_t *ti, const tlx_info_t *ti2, boolean_t isrpc)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	return ((isrpc || (memcmp(ti->local_addr.buf, ti2->local_addr.buf,
2210Sstevel@tonic-gate 	    sizeof (struct sockaddr_storage)) == 0)) &&
2220Sstevel@tonic-gate 	    (strcmp(ti->dev_name, ti2->dev_name) == 0));
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * Attempts to bind an address to the network fd 'fd'. If 'reqaddr' is non-NULL,
2270Sstevel@tonic-gate  * it attempts to bind to that requested address, else it binds to a kernel
2280Sstevel@tonic-gate  * selected address. In the former case, the function returning success
2290Sstevel@tonic-gate  * doesn't guarantee that the requested address was bound (the caller needs to
2300Sstevel@tonic-gate  * check). If 'retaddr' is non-NULL, the bound address is returned in it. The
2310Sstevel@tonic-gate  * 'qlen' parameter is used to set the connection backlog. If the bind
2320Sstevel@tonic-gate  * succeeds 0 is returned, else -1.
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate static int
2350Sstevel@tonic-gate tlx_bind(int fd, const struct netbuf *reqaddr, struct netbuf *retaddr, int qlen)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	struct t_bind breq;
2380Sstevel@tonic-gate 	struct t_bind bret;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	debug_msg("Entering tlx_bind: req: %x, ret: %x, qlen: %d", reqaddr,
2410Sstevel@tonic-gate 	    retaddr, qlen);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	if (retaddr != NULL) {	/* caller requests bound address be returned */
2450Sstevel@tonic-gate 		bret.addr.buf = retaddr->buf;
2460Sstevel@tonic-gate 		bret.addr.maxlen = retaddr->maxlen;
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if (reqaddr != NULL) {  /* caller requests specific address */
2500Sstevel@tonic-gate 		breq.addr.buf = reqaddr->buf;
2510Sstevel@tonic-gate 		breq.addr.len = reqaddr->len;
2520Sstevel@tonic-gate 	} else {
2530Sstevel@tonic-gate 		breq.addr.len = 0;
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 	breq.qlen = qlen;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (t_bind(fd, &breq, retaddr != NULL ? &bret : NULL) < 0)
2580Sstevel@tonic-gate 		return (-1);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (retaddr != NULL)
2610Sstevel@tonic-gate 		retaddr->len = bret.addr.len;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	return (0);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static int
2670Sstevel@tonic-gate tlx_setsockopt(int fd, int level, int optname, const void *optval,
2680Sstevel@tonic-gate     socklen_t optlen)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	struct t_optmgmt request, reply;
2710Sstevel@tonic-gate 	struct {
2720Sstevel@tonic-gate 		struct opthdr sockopt;
2730Sstevel@tonic-gate 		char data[256];
2740Sstevel@tonic-gate 	} optbuf;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	debug_msg("Entering tlx_setsockopt, "
2770Sstevel@tonic-gate 	    "fd: %d, level: %d, optname: %d, optval: %x, optlen: %d",
2780Sstevel@tonic-gate 	    fd, level, optname, optval, optlen);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (optlen > sizeof (optbuf.data)) {
2810Sstevel@tonic-gate 		error_msg(gettext("t_optmgmt request too long"));
2820Sstevel@tonic-gate 		return (-1);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	optbuf.sockopt.level = level;
2860Sstevel@tonic-gate 	optbuf.sockopt.name = optname;
2870Sstevel@tonic-gate 	optbuf.sockopt.len = optlen;
2880Sstevel@tonic-gate 	(void) memcpy(optbuf.data, optval, optlen);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	request.opt.len = sizeof (struct opthdr) + optlen;
2910Sstevel@tonic-gate 	request.opt.buf = (char *)&optbuf;
2920Sstevel@tonic-gate 	request.flags = T_NEGOTIATE;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	reply.opt.maxlen = sizeof (struct opthdr) + optlen;
2950Sstevel@tonic-gate 	reply.opt.buf = (char *)&optbuf;
2960Sstevel@tonic-gate 	reply.flags = 0;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if ((t_optmgmt(fd, &request, &reply) == -1) ||
2990Sstevel@tonic-gate 	    (reply.flags != T_SUCCESS)) {
3000Sstevel@tonic-gate 		error_msg("t_optmgmt: %s", t_strerror(t_errno));
3010Sstevel@tonic-gate 		return (-1);
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	return (0);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate  * Compare contents of netbuf for equality. Return B_TRUE on a match and
3080Sstevel@tonic-gate  * B_FALSE for mismatch.
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate static boolean_t
3110Sstevel@tonic-gate netbufs_equal(struct netbuf *n1, struct netbuf *n2)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	return ((n1->len == n2->len) &&
3140Sstevel@tonic-gate 	    (memcmp(n1->buf, n2->buf, (size_t)n1->len) == 0));
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate  * Create a tli/xti endpoint, either bound to the address specified in
3190Sstevel@tonic-gate  * 'instance' for non-RPC services, else a kernel chosen address.
3200Sstevel@tonic-gate  * Returns -1 on failure, else 0.
3210Sstevel@tonic-gate  */
3220Sstevel@tonic-gate int
323*4754Svp157776 create_bound_endpoint(const instance_t *inst, tlx_info_t *tlx_info)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	int			fd;
3260Sstevel@tonic-gate 	int			qlen;
327*4754Svp157776 	const char		*fmri = inst->fmri;
3280Sstevel@tonic-gate 	struct netbuf		*reqaddr;
3290Sstevel@tonic-gate 	struct netbuf		*retaddr;
3300Sstevel@tonic-gate 	struct netbuf		netbuf;
3310Sstevel@tonic-gate 	struct sockaddr_storage	ss;
3320Sstevel@tonic-gate 	rpc_info_t		*rpc = tlx_info->pr_info.ri;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	debug_msg("Entering create_bound_endpoint");
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) {
3370Sstevel@tonic-gate 		error_msg(gettext("Failed to open transport %s for "
3380Sstevel@tonic-gate 		    "instance %s, proto %s: %s"), tlx_info->dev_name,
3390Sstevel@tonic-gate 		    fmri, tlx_info->pr_info.proto, t_strerror(t_errno));
3400Sstevel@tonic-gate 		return (-1);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (tlx_info->pr_info.v6only) {
3440Sstevel@tonic-gate 		int	on = 1;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		/* restrict to IPv6 communications only */
3470Sstevel@tonic-gate 		if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
3480Sstevel@tonic-gate 		    sizeof (on)) == -1) {
3490Sstevel@tonic-gate 			(void) t_close(fd);
3500Sstevel@tonic-gate 			return (-1);
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * Negotiate for the returning of the remote uid for loopback
3560Sstevel@tonic-gate 	 * transports for RPC services. This needs to be done before the
3570Sstevel@tonic-gate 	 * endpoint is bound using t_bind(), so that any requests to it
3580Sstevel@tonic-gate 	 * contain the uid.
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	if ((rpc != NULL) && (rpc->is_loopback))
3610Sstevel@tonic-gate 		svc_fd_negotiate_ucred(fd);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/*
3640Sstevel@tonic-gate 	 * Bind the service's address to the endpoint and setup connection
3650Sstevel@tonic-gate 	 * backlog. In the case of RPC services, we specify a NULL requested
3660Sstevel@tonic-gate 	 * address and accept what we're given, storing the returned address
3670Sstevel@tonic-gate 	 * for later RPC binding. In the case of non-RPC services we specify
3680Sstevel@tonic-gate 	 * the service's associated address.
3690Sstevel@tonic-gate 	 */
3700Sstevel@tonic-gate 	if (rpc != NULL) {
3710Sstevel@tonic-gate 		reqaddr = NULL;
3720Sstevel@tonic-gate 		retaddr =  &(rpc->netbuf);
3730Sstevel@tonic-gate 	} else {
3740Sstevel@tonic-gate 		reqaddr = &(tlx_info->local_addr);
3750Sstevel@tonic-gate 		netbuf.buf = (char *)&ss;
3760Sstevel@tonic-gate 		netbuf.maxlen = sizeof (ss);
3770Sstevel@tonic-gate 		retaddr = &netbuf;
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
380*4754Svp157776 	/* ignored for conn/less services */
381*4754Svp157776 	qlen = inst->config->basic->conn_backlog;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if ((tlx_bind(fd, reqaddr, retaddr, qlen) == -1) ||
3840Sstevel@tonic-gate 	    ((reqaddr != NULL) && !netbufs_equal(reqaddr, retaddr))) {
3850Sstevel@tonic-gate 		error_msg(gettext("Failed to bind to the requested address "
3860Sstevel@tonic-gate 		    "for instance %s, proto %s"), fmri,
3870Sstevel@tonic-gate 		    tlx_info->pr_info.proto);
3880Sstevel@tonic-gate 		(void) t_close(fd);
3890Sstevel@tonic-gate 		return (-1);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (fd);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * Takes a connection request off 'fd' in the form of a t_call structure
3970Sstevel@tonic-gate  * and returns a pointer to it.
3980Sstevel@tonic-gate  * Returns NULL on failure, else pointer to t_call structure on success.
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate static struct t_call *
4010Sstevel@tonic-gate get_new_conind(int fd)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	struct t_call *call;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	debug_msg("Entering get_new_conind");
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
4080Sstevel@tonic-gate 	if ((call = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL) {
4090Sstevel@tonic-gate 		error_msg("t_alloc: %s", t_strerror(t_errno));
4100Sstevel@tonic-gate 		return (NULL);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 	if (t_listen(fd, call) < 0) {
4130Sstevel@tonic-gate 		error_msg("t_listen: %s", t_strerror(t_errno));
4140Sstevel@tonic-gate 		(void) t_free((char *)call, T_CALL);
4150Sstevel@tonic-gate 		return (NULL);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	return (call);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /* Add 'call' to the connection indication queue 'queue'. */
4220Sstevel@tonic-gate int
4230Sstevel@tonic-gate queue_conind(uu_list_t *queue, struct t_call *call)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	tlx_conn_ind_t *ci;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if ((ci = malloc(sizeof (tlx_conn_ind_t))) == NULL) {
4280Sstevel@tonic-gate 		error_msg(strerror(errno));
4290Sstevel@tonic-gate 		return (-1);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	ci->call = call;
4330Sstevel@tonic-gate 	uu_list_node_init(ci, &ci->link, conn_ind_pool);
4340Sstevel@tonic-gate 	(void) uu_list_insert_after(queue, NULL, ci);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	return (0);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /*
4400Sstevel@tonic-gate  * Remove and return a pointer to the first call on queue 'queue'. However,
4410Sstevel@tonic-gate  * if the queue is empty returns NULL.
4420Sstevel@tonic-gate  */
4430Sstevel@tonic-gate struct t_call *
4440Sstevel@tonic-gate dequeue_conind(uu_list_t *queue)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	struct t_call   *ret;
4470Sstevel@tonic-gate 	tlx_conn_ind_t	*ci = uu_list_first(queue);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if (ci == NULL)
4500Sstevel@tonic-gate 		return (NULL);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	ret = ci->call;
4530Sstevel@tonic-gate 	uu_list_remove(queue, ci);
4540Sstevel@tonic-gate 	free(ci);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	return (ret);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate  * Handle a TLOOK notification received during a t_accept() call.
4610Sstevel@tonic-gate  * Returns -1 on failure, else 0.
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate static int
4640Sstevel@tonic-gate process_tlook(const char *fmri, tlx_info_t *tlx_info)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	int	event;
4670Sstevel@tonic-gate 	int	fd = tlx_info->pr_info.listen_fd;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	debug_msg("Entering process_tlook:");
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	switch (event = t_look(fd)) {
4720Sstevel@tonic-gate 	case T_LISTEN: {
4730Sstevel@tonic-gate 		struct t_call *call;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		debug_msg("process_tlook: T_LISTEN event");
4760Sstevel@tonic-gate 		if ((call = get_new_conind(fd)) == NULL)
4770Sstevel@tonic-gate 			return (-1);
4780Sstevel@tonic-gate 		if (queue_conind(tlx_info->conn_ind_queue, call) == -1) {
4790Sstevel@tonic-gate 			error_msg(gettext("Failed to queue connection "
4800Sstevel@tonic-gate 			    "indication for instance %s"), fmri);
4810Sstevel@tonic-gate 			(void) t_free((char *)call, T_CALL);
4820Sstevel@tonic-gate 			return (-1);
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 		break;
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 	case T_DISCONNECT: {
4870Sstevel@tonic-gate 		/*
4880Sstevel@tonic-gate 		 * Note: In Solaris 2.X (SunOS 5.X) bundled
4890Sstevel@tonic-gate 		 * connection-oriented transport drivers
4900Sstevel@tonic-gate 		 * [ e.g /dev/tcp and /dev/ticots and
4910Sstevel@tonic-gate 		 * /dev/ticotsord (tl)] we do not send disconnect
4920Sstevel@tonic-gate 		 * indications to listening endpoints.
4930Sstevel@tonic-gate 		 * So this will not be seen with endpoints on Solaris
4940Sstevel@tonic-gate 		 * bundled transport devices. However, Streams TPI
4950Sstevel@tonic-gate 		 * allows for this (broken?) behavior and so we account
4960Sstevel@tonic-gate 		 * for it here because of the possibility of unbundled
4970Sstevel@tonic-gate 		 * transport drivers causing this.
4980Sstevel@tonic-gate 		 */
4990Sstevel@tonic-gate 		tlx_conn_ind_t	*cip;
5000Sstevel@tonic-gate 		struct t_discon	*discon;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		debug_msg("process_tlook: T_DISCONNECT event");
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 		/* LINTED */
5050Sstevel@tonic-gate 		if ((discon = (struct t_discon *)
5060Sstevel@tonic-gate 		    t_alloc(fd, T_DIS, T_ALL)) == NULL) {
5070Sstevel@tonic-gate 			error_msg("t_alloc: %s", t_strerror(t_errno));
5080Sstevel@tonic-gate 			return (-1);
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 		if (t_rcvdis(fd, discon) < 0) {
5110Sstevel@tonic-gate 			error_msg("t_rcvdis: %s", t_strerror(t_errno));
5120Sstevel@tonic-gate 			(void) t_free((char *)discon, T_DIS);
5130Sstevel@tonic-gate 			return (-1);
5140Sstevel@tonic-gate 		}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		/*
5170Sstevel@tonic-gate 		 * Find any queued connection pending that matches this
5180Sstevel@tonic-gate 		 * disconnect notice and remove from the pending queue.
5190Sstevel@tonic-gate 		 */
5200Sstevel@tonic-gate 		cip = uu_list_first(tlx_info->conn_ind_queue);
5210Sstevel@tonic-gate 		while ((cip != NULL) &&
5220Sstevel@tonic-gate 		    (cip->call->sequence != discon->sequence)) {
5230Sstevel@tonic-gate 			cip = uu_list_next(tlx_info->conn_ind_queue, cip);
5240Sstevel@tonic-gate 		}
5250Sstevel@tonic-gate 		if (cip != NULL) {	/* match found */
5260Sstevel@tonic-gate 			uu_list_remove(tlx_info->conn_ind_queue, cip);
5270Sstevel@tonic-gate 			(void) t_free((char *)cip->call, T_CALL);
5280Sstevel@tonic-gate 			free(cip);
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 		(void) t_free((char *)discon, T_DIS);
5320Sstevel@tonic-gate 		break;
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 	case -1:
5350Sstevel@tonic-gate 		error_msg("t_look: %s", t_errno);
5360Sstevel@tonic-gate 		return (-1);
5370Sstevel@tonic-gate 	default:
5380Sstevel@tonic-gate 		error_msg(gettext("do_tlook: unexpected t_look event: %d"),
5390Sstevel@tonic-gate 		    event);
5400Sstevel@tonic-gate 		return (-1);
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	return (0);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate /*
5470Sstevel@tonic-gate  * This call attempts to t_accept() an incoming/pending TLI connection.
5480Sstevel@tonic-gate  * If it is thwarted by a TLOOK, it is deferred and whatever is on the
5490Sstevel@tonic-gate  * file descriptor, removed after a t_look. (Incoming connect indications
5500Sstevel@tonic-gate  * get queued for later processing and disconnect indications remove a
5510Sstevel@tonic-gate  * a queued connection request if a match found).
5520Sstevel@tonic-gate  * Returns -1 on failure, else 0.
5530Sstevel@tonic-gate  */
5540Sstevel@tonic-gate int
5550Sstevel@tonic-gate tlx_accept(const char *fmri, tlx_info_t *tlx_info,
5560Sstevel@tonic-gate     struct sockaddr_storage *remote_addr)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	tlx_conn_ind_t	*conind;
5590Sstevel@tonic-gate 	struct t_call	*call;
5600Sstevel@tonic-gate 	int		fd;
5610Sstevel@tonic-gate 	int		listen_fd = tlx_info->pr_info.listen_fd;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	debug_msg("Entering tlx_accept: instance: %s", fmri);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) {
5660Sstevel@tonic-gate 		error_msg("t_open: %s", t_strerror(t_errno));
5670Sstevel@tonic-gate 		return (-1);
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if (tlx_info->pr_info.v6only) {
5710Sstevel@tonic-gate 		int	on = 1;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		/* restrict to IPv6 communications only */
5740Sstevel@tonic-gate 		if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
5750Sstevel@tonic-gate 		    sizeof (on)) == -1) {
5760Sstevel@tonic-gate 			(void) t_close(fd);
5770Sstevel@tonic-gate 			return (-1);
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (t_bind(fd, NULL, NULL) == -1) {
5820Sstevel@tonic-gate 		error_msg("t_bind: %s", t_strerror(t_errno));
5830Sstevel@tonic-gate 		(void) t_close(fd);
5840Sstevel@tonic-gate 		return (-1);
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * Get the next connection indication - first try the pending
5890Sstevel@tonic-gate 	 * queue, then, if none there, get a new one from the file descriptor.
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	if ((conind = uu_list_first(tlx_info->conn_ind_queue)) != NULL) {
5920Sstevel@tonic-gate 		debug_msg("taking con off queue");
5930Sstevel@tonic-gate 		call = conind->call;
5940Sstevel@tonic-gate 	} else if ((call = get_new_conind(listen_fd)) == NULL) {
5950Sstevel@tonic-gate 		(void) t_close(fd);
5960Sstevel@tonic-gate 		return (-1);
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/*
6000Sstevel@tonic-gate 	 * Accept the connection indication on the newly created endpoint.
6010Sstevel@tonic-gate 	 * If we fail, and it's the result of a tlook, queue the indication
6020Sstevel@tonic-gate 	 * if it isn't already, and go and process the t_look.
6030Sstevel@tonic-gate 	 */
6040Sstevel@tonic-gate 	if (t_accept(listen_fd, fd, call) == -1) {
6050Sstevel@tonic-gate 		if (t_errno == TLOOK) {
6060Sstevel@tonic-gate 			if (uu_list_first(tlx_info->conn_ind_queue) == NULL) {
6070Sstevel@tonic-gate 				/*
6080Sstevel@tonic-gate 				 * We are first one to have to defer accepting
6090Sstevel@tonic-gate 				 * and start the pending connections list.
6100Sstevel@tonic-gate 				 */
6110Sstevel@tonic-gate 				if (queue_conind(tlx_info->conn_ind_queue,
6120Sstevel@tonic-gate 				    call) == -1) {
6130Sstevel@tonic-gate 					error_msg(gettext(
6140Sstevel@tonic-gate 					    "Failed to queue connection "
6150Sstevel@tonic-gate 					    "indication for instance %s"),
6160Sstevel@tonic-gate 					    fmri);
6170Sstevel@tonic-gate 					(void) t_free((char *)call, T_CALL);
6180Sstevel@tonic-gate 					return (-1);
6190Sstevel@tonic-gate 				}
6200Sstevel@tonic-gate 			}
6210Sstevel@tonic-gate 			(void) process_tlook(fmri, tlx_info);
6220Sstevel@tonic-gate 		} else {		  /* non-TLOOK accept failure */
6230Sstevel@tonic-gate 			error_msg("%s: %s", "t_accept failed",
6240Sstevel@tonic-gate 			    t_strerror(t_errno));
6250Sstevel@tonic-gate 			/*
6260Sstevel@tonic-gate 			 * If we were accepting a queued connection, dequeue
6270Sstevel@tonic-gate 			 * it.
6280Sstevel@tonic-gate 			 */
6290Sstevel@tonic-gate 			if (uu_list_first(tlx_info->conn_ind_queue) != NULL)
6300Sstevel@tonic-gate 				(void) dequeue_conind(tlx_info->conn_ind_queue);
6310Sstevel@tonic-gate 			(void) t_free((char *)call, T_CALL);
6320Sstevel@tonic-gate 		}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 		(void) t_close(fd);
6350Sstevel@tonic-gate 		return (-1);
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/* Copy remote address into address parameter */
6390Sstevel@tonic-gate 	(void) memcpy(remote_addr, call->addr.buf,
6400Sstevel@tonic-gate 	    MIN(call->addr.len, sizeof (*remote_addr)));
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/* If we were accepting a queued connection, dequeue it. */
6430Sstevel@tonic-gate 	if (uu_list_first(tlx_info->conn_ind_queue) != NULL)
6440Sstevel@tonic-gate 		(void) dequeue_conind(tlx_info->conn_ind_queue);
6450Sstevel@tonic-gate 	(void) t_free((char *)call, T_CALL);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	return (fd);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /* protocol independent network fd close routine */
6510Sstevel@tonic-gate void
6520Sstevel@tonic-gate close_net_fd(instance_t *inst, int fd)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	debug_msg("Entering close_net_fd: inst: %s, fd: %d", inst->fmri, fd);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	if (inst->config->basic->istlx) {
6570Sstevel@tonic-gate 		(void) t_close(fd);
6580Sstevel@tonic-gate 	} else {
6590Sstevel@tonic-gate 		(void) close(fd);
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate /*
6640Sstevel@tonic-gate  * Consume some data from the given endpoint of the given wait-based instance.
6650Sstevel@tonic-gate  */
6660Sstevel@tonic-gate void
6670Sstevel@tonic-gate consume_wait_data(instance_t *inst, int fd)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	int	flag;
6700Sstevel@tonic-gate 	char	buf[50];	/* same arbitrary size as old inetd */
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	debug_msg("Entering consume_wait_data: inst: %s, fd: %d", inst->fmri,
6730Sstevel@tonic-gate 	    fd);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if (inst->config->basic->istlx) {
6760Sstevel@tonic-gate 		(void) t_rcv(fd, buf, sizeof (buf), &flag);
6770Sstevel@tonic-gate 	} else {
6780Sstevel@tonic-gate 		(void) recv(fd, buf, sizeof (buf), 0);
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate }
681