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