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 5fff9db26Svp157776 * Common Development and Distribution License (the "License"). 6fff9db26Svp157776 * 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 */ 217c478bd9Sstevel@tonic-gate /* 22eed64e98Sgm209912 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * Contains routines that deal with TLI/XTI endpoints and rpc services. 287c478bd9Sstevel@tonic-gate */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <fcntl.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <libintl.h> 357c478bd9Sstevel@tonic-gate #include <unistd.h> 367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 377c478bd9Sstevel@tonic-gate #include <netconfig.h> 387c478bd9Sstevel@tonic-gate #include <errno.h> 397c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 407c478bd9Sstevel@tonic-gate #include "inetd_impl.h" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate uu_list_pool_t *conn_ind_pool = NULL; 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * RPC functions. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * Returns B_TRUE if the non-address components of the 2 rpc_info_t structures 507c478bd9Sstevel@tonic-gate * are equivalent, else B_FALSE. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate boolean_t 537c478bd9Sstevel@tonic-gate rpc_info_equal(const rpc_info_t *ri, const rpc_info_t *ri2) 547c478bd9Sstevel@tonic-gate { 557c478bd9Sstevel@tonic-gate return ((ri->prognum == ri2->prognum) && 567c478bd9Sstevel@tonic-gate (ri->lowver == ri2->lowver) && 577c478bd9Sstevel@tonic-gate (ri->highver == ri2->highver) && 587c478bd9Sstevel@tonic-gate (strcmp(ri->netid, ri2->netid) == 0)); 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate * Determine if we have a configured interface for the specified address 637c478bd9Sstevel@tonic-gate * family. This code is a mirror of libnsl's __can_use_af(). We mirror 647c478bd9Sstevel@tonic-gate * it because we need an exact duplicate of its behavior, yet the 657c478bd9Sstevel@tonic-gate * function isn't exported by libnsl, and this fix is considered short- 667c478bd9Sstevel@tonic-gate * term, so it's not worth exporting it. 677c478bd9Sstevel@tonic-gate * 687c478bd9Sstevel@tonic-gate * We need to duplicate __can_use_af() so we can accurately determine 697c478bd9Sstevel@tonic-gate * when getnetconfigent() returns failure for a v6 netid due to no IPv6 707c478bd9Sstevel@tonic-gate * interfaces being configured: getnetconfigent() returns failure 717c478bd9Sstevel@tonic-gate * if a netid is either 'tcp6' or 'udp6' and __can_use_af() returns 0, 727c478bd9Sstevel@tonic-gate * but it doesn't return a return code to uniquely determine this 737c478bd9Sstevel@tonic-gate * failure. If we don't accurately determine these failures, we could 747c478bd9Sstevel@tonic-gate * output error messages in a case when they weren't justified. 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate static int 777c478bd9Sstevel@tonic-gate can_use_af(sa_family_t af) 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate struct lifnum lifn; 807c478bd9Sstevel@tonic-gate int fd; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if ((fd = open("/dev/udp", O_RDONLY)) < 0) { 837c478bd9Sstevel@tonic-gate return (0); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate lifn.lifn_family = af; 867c478bd9Sstevel@tonic-gate /* LINTED ECONST_EXPR */ 877c478bd9Sstevel@tonic-gate lifn.lifn_flags = IFF_UP & !(IFF_NOXMIT | IFF_DEPRECATED); 887c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCGLIFNUM, &lifn, sizeof (lifn)) < 0) { 897c478bd9Sstevel@tonic-gate lifn.lifn_count = 0; 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate (void) close(fd); 937c478bd9Sstevel@tonic-gate return (lifn.lifn_count); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate static boolean_t 977c478bd9Sstevel@tonic-gate is_v6_netid(const char *netid) 987c478bd9Sstevel@tonic-gate { 997c478bd9Sstevel@tonic-gate return ((strcmp(netid, SOCKET_PROTO_TCP6) == 0) || 1007c478bd9Sstevel@tonic-gate (strcmp(netid, SOCKET_PROTO_UDP6) == 0)); 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* 1047c478bd9Sstevel@tonic-gate * Registers with rpcbind the program number with all versions, from low to 1057c478bd9Sstevel@tonic-gate * high, with the netid, all specified in 'rpc'. If registration fails, 1067c478bd9Sstevel@tonic-gate * returns -1, else 0. 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate int 1097c478bd9Sstevel@tonic-gate register_rpc_service(const char *fmri, const rpc_info_t *rpc) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate struct netconfig *nconf; 1127c478bd9Sstevel@tonic-gate int ver; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate if ((nconf = getnetconfigent(rpc->netid)) == NULL) { 1157c478bd9Sstevel@tonic-gate /* 1167c478bd9Sstevel@tonic-gate * Check whether getnetconfigent() failed as a result of 1177c478bd9Sstevel@tonic-gate * having no IPv6 interfaces configured for a v6 netid, or 1187c478bd9Sstevel@tonic-gate * as a result of a 'real' error, and output an appropriate 1197c478bd9Sstevel@tonic-gate * message with an appropriate severity. 1207c478bd9Sstevel@tonic-gate */ 1217c478bd9Sstevel@tonic-gate if (is_v6_netid(rpc->netid) && !can_use_af(AF_INET6)) { 1227c478bd9Sstevel@tonic-gate warn_msg(gettext( 1237c478bd9Sstevel@tonic-gate "Couldn't register netid %s for RPC instance %s " 1247c478bd9Sstevel@tonic-gate "because no IPv6 interfaces are plumbed"), 1257c478bd9Sstevel@tonic-gate rpc->netid, fmri); 1267c478bd9Sstevel@tonic-gate } else { 1277c478bd9Sstevel@tonic-gate error_msg(gettext( 1287c478bd9Sstevel@tonic-gate "Failed to lookup netid '%s' for instance %s: %s"), 1297c478bd9Sstevel@tonic-gate rpc->netid, fmri, nc_sperror()); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate return (-1); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate for (ver = rpc->lowver; ver <= rpc->highver; ver++) { 1357c478bd9Sstevel@tonic-gate if (!rpcb_set(rpc->prognum, ver, nconf, &(rpc->netbuf))) { 1367c478bd9Sstevel@tonic-gate error_msg(gettext("Failed to register version %d " 1377c478bd9Sstevel@tonic-gate "of RPC service instance %s, netid %s"), ver, 1387c478bd9Sstevel@tonic-gate fmri, rpc->netid); 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate for (ver--; ver >= rpc->lowver; ver--) 1417c478bd9Sstevel@tonic-gate (void) rpcb_unset(rpc->prognum, ver, nconf); 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 1447c478bd9Sstevel@tonic-gate return (-1); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 1497c478bd9Sstevel@tonic-gate return (0); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* Unregister all the registrations done by register_rpc_service */ 1537c478bd9Sstevel@tonic-gate void 1547c478bd9Sstevel@tonic-gate unregister_rpc_service(const char *fmri, const rpc_info_t *rpc) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate int ver; 1577c478bd9Sstevel@tonic-gate struct netconfig *nconf; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if ((nconf = getnetconfigent(rpc->netid)) == NULL) { 1607c478bd9Sstevel@tonic-gate /* 1617c478bd9Sstevel@tonic-gate * Don't output an error message if getnetconfigent() fails for 1627c478bd9Sstevel@tonic-gate * a v6 netid when an IPv6 interface isn't configured. 1637c478bd9Sstevel@tonic-gate */ 1647c478bd9Sstevel@tonic-gate if (!(is_v6_netid(rpc->netid) && !can_use_af(AF_INET6))) { 1657c478bd9Sstevel@tonic-gate error_msg(gettext( 1667c478bd9Sstevel@tonic-gate "Failed to lookup netid '%s' for instance %s: %s"), 1677c478bd9Sstevel@tonic-gate rpc->netid, fmri, nc_sperror()); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate return; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate for (ver = rpc->lowver; ver <= rpc->highver; ver++) 1737c478bd9Sstevel@tonic-gate (void) rpcb_unset(rpc->prognum, ver, nconf); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate /* 1797c478bd9Sstevel@tonic-gate * TLI/XTI functions. 1807c478bd9Sstevel@tonic-gate */ 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate int 1837c478bd9Sstevel@tonic-gate tlx_init(void) 1847c478bd9Sstevel@tonic-gate { 1857c478bd9Sstevel@tonic-gate if ((conn_ind_pool = uu_list_pool_create("conn_ind_pool", 1867c478bd9Sstevel@tonic-gate sizeof (tlx_conn_ind_t), offsetof(tlx_conn_ind_t, link), 1877c478bd9Sstevel@tonic-gate NULL, UU_LIST_POOL_DEBUG)) == NULL) { 1887c478bd9Sstevel@tonic-gate error_msg("%s: %s", gettext("Failed to create uu pool"), 1897c478bd9Sstevel@tonic-gate uu_strerror(uu_error())); 1907c478bd9Sstevel@tonic-gate return (-1); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate return (0); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate void 1977c478bd9Sstevel@tonic-gate tlx_fini(void) 1987c478bd9Sstevel@tonic-gate { 1997c478bd9Sstevel@tonic-gate if (conn_ind_pool != NULL) { 2007c478bd9Sstevel@tonic-gate uu_list_pool_destroy(conn_ind_pool); 2017c478bd9Sstevel@tonic-gate conn_ind_pool = NULL; 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* 2067c478bd9Sstevel@tonic-gate * Checks if the contents of the 2 tlx_info_t structures are equivalent. 2077c478bd9Sstevel@tonic-gate * If 'isrpc' is false, the address components of the two structures are 2087c478bd9Sstevel@tonic-gate * compared for equality as part of this. If the two structures are 2097c478bd9Sstevel@tonic-gate * equivalent B_TRUE is returned, else B_FALSE. 2107c478bd9Sstevel@tonic-gate */ 2117c478bd9Sstevel@tonic-gate boolean_t 2127c478bd9Sstevel@tonic-gate tlx_info_equal(const tlx_info_t *ti, const tlx_info_t *ti2, boolean_t isrpc) 2137c478bd9Sstevel@tonic-gate { 2147c478bd9Sstevel@tonic-gate return ((isrpc || (memcmp(ti->local_addr.buf, ti2->local_addr.buf, 2157c478bd9Sstevel@tonic-gate sizeof (struct sockaddr_storage)) == 0)) && 2167c478bd9Sstevel@tonic-gate (strcmp(ti->dev_name, ti2->dev_name) == 0)); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate /* 2207c478bd9Sstevel@tonic-gate * Attempts to bind an address to the network fd 'fd'. If 'reqaddr' is non-NULL, 2217c478bd9Sstevel@tonic-gate * it attempts to bind to that requested address, else it binds to a kernel 2227c478bd9Sstevel@tonic-gate * selected address. In the former case, the function returning success 2237c478bd9Sstevel@tonic-gate * doesn't guarantee that the requested address was bound (the caller needs to 2247c478bd9Sstevel@tonic-gate * check). If 'retaddr' is non-NULL, the bound address is returned in it. The 2257c478bd9Sstevel@tonic-gate * 'qlen' parameter is used to set the connection backlog. If the bind 2267c478bd9Sstevel@tonic-gate * succeeds 0 is returned, else -1. 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate static int 2297c478bd9Sstevel@tonic-gate tlx_bind(int fd, const struct netbuf *reqaddr, struct netbuf *retaddr, int qlen) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate struct t_bind breq; 2327c478bd9Sstevel@tonic-gate struct t_bind bret; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if (retaddr != NULL) { /* caller requests bound address be returned */ 2357c478bd9Sstevel@tonic-gate bret.addr.buf = retaddr->buf; 2367c478bd9Sstevel@tonic-gate bret.addr.maxlen = retaddr->maxlen; 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if (reqaddr != NULL) { /* caller requests specific address */ 2407c478bd9Sstevel@tonic-gate breq.addr.buf = reqaddr->buf; 2417c478bd9Sstevel@tonic-gate breq.addr.len = reqaddr->len; 2427c478bd9Sstevel@tonic-gate } else { 2437c478bd9Sstevel@tonic-gate breq.addr.len = 0; 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate breq.qlen = qlen; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate if (t_bind(fd, &breq, retaddr != NULL ? &bret : NULL) < 0) 2487c478bd9Sstevel@tonic-gate return (-1); 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate if (retaddr != NULL) 2517c478bd9Sstevel@tonic-gate retaddr->len = bret.addr.len; 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate return (0); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate static int 2577c478bd9Sstevel@tonic-gate tlx_setsockopt(int fd, int level, int optname, const void *optval, 2587c478bd9Sstevel@tonic-gate socklen_t optlen) 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate struct t_optmgmt request, reply; 2617c478bd9Sstevel@tonic-gate struct { 2627c478bd9Sstevel@tonic-gate struct opthdr sockopt; 2637c478bd9Sstevel@tonic-gate char data[256]; 2647c478bd9Sstevel@tonic-gate } optbuf; 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if (optlen > sizeof (optbuf.data)) { 2677c478bd9Sstevel@tonic-gate error_msg(gettext("t_optmgmt request too long")); 2687c478bd9Sstevel@tonic-gate return (-1); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate optbuf.sockopt.level = level; 2727c478bd9Sstevel@tonic-gate optbuf.sockopt.name = optname; 2737c478bd9Sstevel@tonic-gate optbuf.sockopt.len = optlen; 2747c478bd9Sstevel@tonic-gate (void) memcpy(optbuf.data, optval, optlen); 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate request.opt.len = sizeof (struct opthdr) + optlen; 2777c478bd9Sstevel@tonic-gate request.opt.buf = (char *)&optbuf; 2787c478bd9Sstevel@tonic-gate request.flags = T_NEGOTIATE; 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate reply.opt.maxlen = sizeof (struct opthdr) + optlen; 2817c478bd9Sstevel@tonic-gate reply.opt.buf = (char *)&optbuf; 2827c478bd9Sstevel@tonic-gate reply.flags = 0; 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate if ((t_optmgmt(fd, &request, &reply) == -1) || 2857c478bd9Sstevel@tonic-gate (reply.flags != T_SUCCESS)) { 2867c478bd9Sstevel@tonic-gate error_msg("t_optmgmt: %s", t_strerror(t_errno)); 2877c478bd9Sstevel@tonic-gate return (-1); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate return (0); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* 2937c478bd9Sstevel@tonic-gate * Compare contents of netbuf for equality. Return B_TRUE on a match and 2947c478bd9Sstevel@tonic-gate * B_FALSE for mismatch. 2957c478bd9Sstevel@tonic-gate */ 2967c478bd9Sstevel@tonic-gate static boolean_t 2977c478bd9Sstevel@tonic-gate netbufs_equal(struct netbuf *n1, struct netbuf *n2) 2987c478bd9Sstevel@tonic-gate { 2997c478bd9Sstevel@tonic-gate return ((n1->len == n2->len) && 3007c478bd9Sstevel@tonic-gate (memcmp(n1->buf, n2->buf, (size_t)n1->len) == 0)); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate /* 3047c478bd9Sstevel@tonic-gate * Create a tli/xti endpoint, either bound to the address specified in 3057c478bd9Sstevel@tonic-gate * 'instance' for non-RPC services, else a kernel chosen address. 3067c478bd9Sstevel@tonic-gate * Returns -1 on failure, else 0. 3077c478bd9Sstevel@tonic-gate */ 3087c478bd9Sstevel@tonic-gate int 309fff9db26Svp157776 create_bound_endpoint(const instance_t *inst, tlx_info_t *tlx_info) 3107c478bd9Sstevel@tonic-gate { 3117c478bd9Sstevel@tonic-gate int fd; 3127c478bd9Sstevel@tonic-gate int qlen; 313fff9db26Svp157776 const char *fmri = inst->fmri; 3147c478bd9Sstevel@tonic-gate struct netbuf *reqaddr; 3157c478bd9Sstevel@tonic-gate struct netbuf *retaddr; 3167c478bd9Sstevel@tonic-gate struct netbuf netbuf; 3177c478bd9Sstevel@tonic-gate struct sockaddr_storage ss; 3187c478bd9Sstevel@tonic-gate rpc_info_t *rpc = tlx_info->pr_info.ri; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) { 3217c478bd9Sstevel@tonic-gate error_msg(gettext("Failed to open transport %s for " 3227c478bd9Sstevel@tonic-gate "instance %s, proto %s: %s"), tlx_info->dev_name, 3237c478bd9Sstevel@tonic-gate fmri, tlx_info->pr_info.proto, t_strerror(t_errno)); 3247c478bd9Sstevel@tonic-gate return (-1); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate if (tlx_info->pr_info.v6only) { 3287c478bd9Sstevel@tonic-gate int on = 1; 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /* restrict to IPv6 communications only */ 3317c478bd9Sstevel@tonic-gate if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, 3327c478bd9Sstevel@tonic-gate sizeof (on)) == -1) { 3337c478bd9Sstevel@tonic-gate (void) t_close(fd); 3347c478bd9Sstevel@tonic-gate return (-1); 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* 3397c478bd9Sstevel@tonic-gate * Negotiate for the returning of the remote uid for loopback 3407c478bd9Sstevel@tonic-gate * transports for RPC services. This needs to be done before the 3417c478bd9Sstevel@tonic-gate * endpoint is bound using t_bind(), so that any requests to it 3427c478bd9Sstevel@tonic-gate * contain the uid. 3437c478bd9Sstevel@tonic-gate */ 3447c478bd9Sstevel@tonic-gate if ((rpc != NULL) && (rpc->is_loopback)) 3457c478bd9Sstevel@tonic-gate svc_fd_negotiate_ucred(fd); 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * Bind the service's address to the endpoint and setup connection 3497c478bd9Sstevel@tonic-gate * backlog. In the case of RPC services, we specify a NULL requested 3507c478bd9Sstevel@tonic-gate * address and accept what we're given, storing the returned address 3517c478bd9Sstevel@tonic-gate * for later RPC binding. In the case of non-RPC services we specify 3527c478bd9Sstevel@tonic-gate * the service's associated address. 3537c478bd9Sstevel@tonic-gate */ 3547c478bd9Sstevel@tonic-gate if (rpc != NULL) { 3557c478bd9Sstevel@tonic-gate reqaddr = NULL; 3567c478bd9Sstevel@tonic-gate retaddr = &(rpc->netbuf); 3577c478bd9Sstevel@tonic-gate } else { 3587c478bd9Sstevel@tonic-gate reqaddr = &(tlx_info->local_addr); 3597c478bd9Sstevel@tonic-gate netbuf.buf = (char *)&ss; 3607c478bd9Sstevel@tonic-gate netbuf.maxlen = sizeof (ss); 3617c478bd9Sstevel@tonic-gate retaddr = &netbuf; 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 364fff9db26Svp157776 /* ignored for conn/less services */ 365fff9db26Svp157776 qlen = inst->config->basic->conn_backlog; 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate if ((tlx_bind(fd, reqaddr, retaddr, qlen) == -1) || 3687c478bd9Sstevel@tonic-gate ((reqaddr != NULL) && !netbufs_equal(reqaddr, retaddr))) { 3697c478bd9Sstevel@tonic-gate error_msg(gettext("Failed to bind to the requested address " 3707c478bd9Sstevel@tonic-gate "for instance %s, proto %s"), fmri, 3717c478bd9Sstevel@tonic-gate tlx_info->pr_info.proto); 3727c478bd9Sstevel@tonic-gate (void) t_close(fd); 3737c478bd9Sstevel@tonic-gate return (-1); 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate return (fd); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate /* 3807c478bd9Sstevel@tonic-gate * Takes a connection request off 'fd' in the form of a t_call structure 3817c478bd9Sstevel@tonic-gate * and returns a pointer to it. 3827c478bd9Sstevel@tonic-gate * Returns NULL on failure, else pointer to t_call structure on success. 3837c478bd9Sstevel@tonic-gate */ 3847c478bd9Sstevel@tonic-gate static struct t_call * 3857c478bd9Sstevel@tonic-gate get_new_conind(int fd) 3867c478bd9Sstevel@tonic-gate { 3877c478bd9Sstevel@tonic-gate struct t_call *call; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 3907c478bd9Sstevel@tonic-gate if ((call = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL) { 3917c478bd9Sstevel@tonic-gate error_msg("t_alloc: %s", t_strerror(t_errno)); 3927c478bd9Sstevel@tonic-gate return (NULL); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate if (t_listen(fd, call) < 0) { 3957c478bd9Sstevel@tonic-gate error_msg("t_listen: %s", t_strerror(t_errno)); 3967c478bd9Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 3977c478bd9Sstevel@tonic-gate return (NULL); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate return (call); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate /* Add 'call' to the connection indication queue 'queue'. */ 4047c478bd9Sstevel@tonic-gate int 4057c478bd9Sstevel@tonic-gate queue_conind(uu_list_t *queue, struct t_call *call) 4067c478bd9Sstevel@tonic-gate { 4077c478bd9Sstevel@tonic-gate tlx_conn_ind_t *ci; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate if ((ci = malloc(sizeof (tlx_conn_ind_t))) == NULL) { 4107c478bd9Sstevel@tonic-gate error_msg(strerror(errno)); 4117c478bd9Sstevel@tonic-gate return (-1); 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate ci->call = call; 4157c478bd9Sstevel@tonic-gate uu_list_node_init(ci, &ci->link, conn_ind_pool); 4167c478bd9Sstevel@tonic-gate (void) uu_list_insert_after(queue, NULL, ci); 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate return (0); 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate /* 4227c478bd9Sstevel@tonic-gate * Remove and return a pointer to the first call on queue 'queue'. However, 4237c478bd9Sstevel@tonic-gate * if the queue is empty returns NULL. 4247c478bd9Sstevel@tonic-gate */ 4257c478bd9Sstevel@tonic-gate struct t_call * 4267c478bd9Sstevel@tonic-gate dequeue_conind(uu_list_t *queue) 4277c478bd9Sstevel@tonic-gate { 4287c478bd9Sstevel@tonic-gate struct t_call *ret; 4297c478bd9Sstevel@tonic-gate tlx_conn_ind_t *ci = uu_list_first(queue); 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate if (ci == NULL) 4327c478bd9Sstevel@tonic-gate return (NULL); 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate ret = ci->call; 4357c478bd9Sstevel@tonic-gate uu_list_remove(queue, ci); 4367c478bd9Sstevel@tonic-gate free(ci); 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate return (ret); 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate /* 4427c478bd9Sstevel@tonic-gate * Handle a TLOOK notification received during a t_accept() call. 4437c478bd9Sstevel@tonic-gate * Returns -1 on failure, else 0. 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate static int 4467c478bd9Sstevel@tonic-gate process_tlook(const char *fmri, tlx_info_t *tlx_info) 4477c478bd9Sstevel@tonic-gate { 4487c478bd9Sstevel@tonic-gate int event; 4497c478bd9Sstevel@tonic-gate int fd = tlx_info->pr_info.listen_fd; 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate switch (event = t_look(fd)) { 4527c478bd9Sstevel@tonic-gate case T_LISTEN: { 4537c478bd9Sstevel@tonic-gate struct t_call *call; 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate debug_msg("process_tlook: T_LISTEN event"); 4567c478bd9Sstevel@tonic-gate if ((call = get_new_conind(fd)) == NULL) 4577c478bd9Sstevel@tonic-gate return (-1); 4587c478bd9Sstevel@tonic-gate if (queue_conind(tlx_info->conn_ind_queue, call) == -1) { 4597c478bd9Sstevel@tonic-gate error_msg(gettext("Failed to queue connection " 4607c478bd9Sstevel@tonic-gate "indication for instance %s"), fmri); 4617c478bd9Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 4627c478bd9Sstevel@tonic-gate return (-1); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate break; 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate case T_DISCONNECT: { 4677c478bd9Sstevel@tonic-gate /* 4687c478bd9Sstevel@tonic-gate * Note: In Solaris 2.X (SunOS 5.X) bundled 4697c478bd9Sstevel@tonic-gate * connection-oriented transport drivers 4707c478bd9Sstevel@tonic-gate * [ e.g /dev/tcp and /dev/ticots and 4717c478bd9Sstevel@tonic-gate * /dev/ticotsord (tl)] we do not send disconnect 4727c478bd9Sstevel@tonic-gate * indications to listening endpoints. 4737c478bd9Sstevel@tonic-gate * So this will not be seen with endpoints on Solaris 4747c478bd9Sstevel@tonic-gate * bundled transport devices. However, Streams TPI 4757c478bd9Sstevel@tonic-gate * allows for this (broken?) behavior and so we account 4767c478bd9Sstevel@tonic-gate * for it here because of the possibility of unbundled 4777c478bd9Sstevel@tonic-gate * transport drivers causing this. 4787c478bd9Sstevel@tonic-gate */ 4797c478bd9Sstevel@tonic-gate tlx_conn_ind_t *cip; 4807c478bd9Sstevel@tonic-gate struct t_discon *discon; 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate debug_msg("process_tlook: T_DISCONNECT event"); 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate /* LINTED */ 4857c478bd9Sstevel@tonic-gate if ((discon = (struct t_discon *) 4867c478bd9Sstevel@tonic-gate t_alloc(fd, T_DIS, T_ALL)) == NULL) { 4877c478bd9Sstevel@tonic-gate error_msg("t_alloc: %s", t_strerror(t_errno)); 4887c478bd9Sstevel@tonic-gate return (-1); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate if (t_rcvdis(fd, discon) < 0) { 4917c478bd9Sstevel@tonic-gate error_msg("t_rcvdis: %s", t_strerror(t_errno)); 4927c478bd9Sstevel@tonic-gate (void) t_free((char *)discon, T_DIS); 4937c478bd9Sstevel@tonic-gate return (-1); 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate /* 4977c478bd9Sstevel@tonic-gate * Find any queued connection pending that matches this 4987c478bd9Sstevel@tonic-gate * disconnect notice and remove from the pending queue. 4997c478bd9Sstevel@tonic-gate */ 5007c478bd9Sstevel@tonic-gate cip = uu_list_first(tlx_info->conn_ind_queue); 5017c478bd9Sstevel@tonic-gate while ((cip != NULL) && 5027c478bd9Sstevel@tonic-gate (cip->call->sequence != discon->sequence)) { 5037c478bd9Sstevel@tonic-gate cip = uu_list_next(tlx_info->conn_ind_queue, cip); 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate if (cip != NULL) { /* match found */ 5067c478bd9Sstevel@tonic-gate uu_list_remove(tlx_info->conn_ind_queue, cip); 5077c478bd9Sstevel@tonic-gate (void) t_free((char *)cip->call, T_CALL); 5087c478bd9Sstevel@tonic-gate free(cip); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate (void) t_free((char *)discon, T_DIS); 5127c478bd9Sstevel@tonic-gate break; 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate case -1: 515*8793b36bSNick Todd error_msg("t_look: %s", t_strerror(t_errno)); 5167c478bd9Sstevel@tonic-gate return (-1); 5177c478bd9Sstevel@tonic-gate default: 5187c478bd9Sstevel@tonic-gate error_msg(gettext("do_tlook: unexpected t_look event: %d"), 5197c478bd9Sstevel@tonic-gate event); 5207c478bd9Sstevel@tonic-gate return (-1); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate return (0); 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate /* 5277c478bd9Sstevel@tonic-gate * This call attempts to t_accept() an incoming/pending TLI connection. 5287c478bd9Sstevel@tonic-gate * If it is thwarted by a TLOOK, it is deferred and whatever is on the 5297c478bd9Sstevel@tonic-gate * file descriptor, removed after a t_look. (Incoming connect indications 5307c478bd9Sstevel@tonic-gate * get queued for later processing and disconnect indications remove a 5317c478bd9Sstevel@tonic-gate * a queued connection request if a match found). 5327c478bd9Sstevel@tonic-gate * Returns -1 on failure, else 0. 5337c478bd9Sstevel@tonic-gate */ 5347c478bd9Sstevel@tonic-gate int 5357c478bd9Sstevel@tonic-gate tlx_accept(const char *fmri, tlx_info_t *tlx_info, 5367c478bd9Sstevel@tonic-gate struct sockaddr_storage *remote_addr) 5377c478bd9Sstevel@tonic-gate { 5387c478bd9Sstevel@tonic-gate tlx_conn_ind_t *conind; 5397c478bd9Sstevel@tonic-gate struct t_call *call; 5407c478bd9Sstevel@tonic-gate int fd; 5417c478bd9Sstevel@tonic-gate int listen_fd = tlx_info->pr_info.listen_fd; 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) { 5447c478bd9Sstevel@tonic-gate error_msg("t_open: %s", t_strerror(t_errno)); 5457c478bd9Sstevel@tonic-gate return (-1); 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate if (tlx_info->pr_info.v6only) { 5497c478bd9Sstevel@tonic-gate int on = 1; 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* restrict to IPv6 communications only */ 5527c478bd9Sstevel@tonic-gate if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, 5537c478bd9Sstevel@tonic-gate sizeof (on)) == -1) { 5547c478bd9Sstevel@tonic-gate (void) t_close(fd); 5557c478bd9Sstevel@tonic-gate return (-1); 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate if (t_bind(fd, NULL, NULL) == -1) { 5607c478bd9Sstevel@tonic-gate error_msg("t_bind: %s", t_strerror(t_errno)); 5617c478bd9Sstevel@tonic-gate (void) t_close(fd); 5627c478bd9Sstevel@tonic-gate return (-1); 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate /* 5667c478bd9Sstevel@tonic-gate * Get the next connection indication - first try the pending 5677c478bd9Sstevel@tonic-gate * queue, then, if none there, get a new one from the file descriptor. 5687c478bd9Sstevel@tonic-gate */ 5697c478bd9Sstevel@tonic-gate if ((conind = uu_list_first(tlx_info->conn_ind_queue)) != NULL) { 5707c478bd9Sstevel@tonic-gate debug_msg("taking con off queue"); 5717c478bd9Sstevel@tonic-gate call = conind->call; 5727c478bd9Sstevel@tonic-gate } else if ((call = get_new_conind(listen_fd)) == NULL) { 5737c478bd9Sstevel@tonic-gate (void) t_close(fd); 5747c478bd9Sstevel@tonic-gate return (-1); 5757c478bd9Sstevel@tonic-gate } 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate /* 5787c478bd9Sstevel@tonic-gate * Accept the connection indication on the newly created endpoint. 5797c478bd9Sstevel@tonic-gate * If we fail, and it's the result of a tlook, queue the indication 5807c478bd9Sstevel@tonic-gate * if it isn't already, and go and process the t_look. 5817c478bd9Sstevel@tonic-gate */ 5827c478bd9Sstevel@tonic-gate if (t_accept(listen_fd, fd, call) == -1) { 5837c478bd9Sstevel@tonic-gate if (t_errno == TLOOK) { 5847c478bd9Sstevel@tonic-gate if (uu_list_first(tlx_info->conn_ind_queue) == NULL) { 5857c478bd9Sstevel@tonic-gate /* 5867c478bd9Sstevel@tonic-gate * We are first one to have to defer accepting 5877c478bd9Sstevel@tonic-gate * and start the pending connections list. 5887c478bd9Sstevel@tonic-gate */ 5897c478bd9Sstevel@tonic-gate if (queue_conind(tlx_info->conn_ind_queue, 5907c478bd9Sstevel@tonic-gate call) == -1) { 5917c478bd9Sstevel@tonic-gate error_msg(gettext( 5927c478bd9Sstevel@tonic-gate "Failed to queue connection " 5937c478bd9Sstevel@tonic-gate "indication for instance %s"), 5947c478bd9Sstevel@tonic-gate fmri); 5957c478bd9Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 5967c478bd9Sstevel@tonic-gate return (-1); 5977c478bd9Sstevel@tonic-gate } 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate (void) process_tlook(fmri, tlx_info); 6007c478bd9Sstevel@tonic-gate } else { /* non-TLOOK accept failure */ 6017c478bd9Sstevel@tonic-gate error_msg("%s: %s", "t_accept failed", 6027c478bd9Sstevel@tonic-gate t_strerror(t_errno)); 6037c478bd9Sstevel@tonic-gate /* 6047c478bd9Sstevel@tonic-gate * If we were accepting a queued connection, dequeue 6057c478bd9Sstevel@tonic-gate * it. 6067c478bd9Sstevel@tonic-gate */ 6077c478bd9Sstevel@tonic-gate if (uu_list_first(tlx_info->conn_ind_queue) != NULL) 6087c478bd9Sstevel@tonic-gate (void) dequeue_conind(tlx_info->conn_ind_queue); 6097c478bd9Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate (void) t_close(fd); 6137c478bd9Sstevel@tonic-gate return (-1); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate /* Copy remote address into address parameter */ 6177c478bd9Sstevel@tonic-gate (void) memcpy(remote_addr, call->addr.buf, 6187c478bd9Sstevel@tonic-gate MIN(call->addr.len, sizeof (*remote_addr))); 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate /* If we were accepting a queued connection, dequeue it. */ 6217c478bd9Sstevel@tonic-gate if (uu_list_first(tlx_info->conn_ind_queue) != NULL) 6227c478bd9Sstevel@tonic-gate (void) dequeue_conind(tlx_info->conn_ind_queue); 6237c478bd9Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate return (fd); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate /* protocol independent network fd close routine */ 6297c478bd9Sstevel@tonic-gate void 6307c478bd9Sstevel@tonic-gate close_net_fd(instance_t *inst, int fd) 6317c478bd9Sstevel@tonic-gate { 6327c478bd9Sstevel@tonic-gate if (inst->config->basic->istlx) { 6337c478bd9Sstevel@tonic-gate (void) t_close(fd); 6347c478bd9Sstevel@tonic-gate } else { 6357c478bd9Sstevel@tonic-gate (void) close(fd); 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate /* 6407c478bd9Sstevel@tonic-gate * Consume some data from the given endpoint of the given wait-based instance. 6417c478bd9Sstevel@tonic-gate */ 6427c478bd9Sstevel@tonic-gate void 6437c478bd9Sstevel@tonic-gate consume_wait_data(instance_t *inst, int fd) 6447c478bd9Sstevel@tonic-gate { 6457c478bd9Sstevel@tonic-gate int flag; 6467c478bd9Sstevel@tonic-gate char buf[50]; /* same arbitrary size as old inetd */ 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate if (inst->config->basic->istlx) { 6497c478bd9Sstevel@tonic-gate (void) t_rcv(fd, buf, sizeof (buf), &flag); 6507c478bd9Sstevel@tonic-gate } else { 6517c478bd9Sstevel@tonic-gate (void) recv(fd, buf, sizeof (buf), 0); 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate } 654