1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Contains routines that deal with TLI/XTI endpoints and rpc services. 31*0Sstevel@tonic-gate */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <sys/types.h> 34*0Sstevel@tonic-gate #include <string.h> 35*0Sstevel@tonic-gate #include <fcntl.h> 36*0Sstevel@tonic-gate #include <stdlib.h> 37*0Sstevel@tonic-gate #include <libintl.h> 38*0Sstevel@tonic-gate #include <unistd.h> 39*0Sstevel@tonic-gate #include <sys/sysmacros.h> 40*0Sstevel@tonic-gate #include <netconfig.h> 41*0Sstevel@tonic-gate #include <errno.h> 42*0Sstevel@tonic-gate #include <sys/sockio.h> 43*0Sstevel@tonic-gate #include "inetd_impl.h" 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate uu_list_pool_t *conn_ind_pool = NULL; 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate /* 48*0Sstevel@tonic-gate * RPC functions. 49*0Sstevel@tonic-gate */ 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /* 52*0Sstevel@tonic-gate * Returns B_TRUE if the non-address components of the 2 rpc_info_t structures 53*0Sstevel@tonic-gate * are equivalent, else B_FALSE. 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate boolean_t 56*0Sstevel@tonic-gate rpc_info_equal(const rpc_info_t *ri, const rpc_info_t *ri2) 57*0Sstevel@tonic-gate { 58*0Sstevel@tonic-gate return ((ri->prognum == ri2->prognum) && 59*0Sstevel@tonic-gate (ri->lowver == ri2->lowver) && 60*0Sstevel@tonic-gate (ri->highver == ri2->highver) && 61*0Sstevel@tonic-gate (strcmp(ri->netid, ri2->netid) == 0)); 62*0Sstevel@tonic-gate } 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate /* 65*0Sstevel@tonic-gate * Determine if we have a configured interface for the specified address 66*0Sstevel@tonic-gate * family. This code is a mirror of libnsl's __can_use_af(). We mirror 67*0Sstevel@tonic-gate * it because we need an exact duplicate of its behavior, yet the 68*0Sstevel@tonic-gate * function isn't exported by libnsl, and this fix is considered short- 69*0Sstevel@tonic-gate * term, so it's not worth exporting it. 70*0Sstevel@tonic-gate * 71*0Sstevel@tonic-gate * We need to duplicate __can_use_af() so we can accurately determine 72*0Sstevel@tonic-gate * when getnetconfigent() returns failure for a v6 netid due to no IPv6 73*0Sstevel@tonic-gate * interfaces being configured: getnetconfigent() returns failure 74*0Sstevel@tonic-gate * if a netid is either 'tcp6' or 'udp6' and __can_use_af() returns 0, 75*0Sstevel@tonic-gate * but it doesn't return a return code to uniquely determine this 76*0Sstevel@tonic-gate * failure. If we don't accurately determine these failures, we could 77*0Sstevel@tonic-gate * output error messages in a case when they weren't justified. 78*0Sstevel@tonic-gate */ 79*0Sstevel@tonic-gate static int 80*0Sstevel@tonic-gate can_use_af(sa_family_t af) 81*0Sstevel@tonic-gate { 82*0Sstevel@tonic-gate struct lifnum lifn; 83*0Sstevel@tonic-gate int fd; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate if ((fd = open("/dev/udp", O_RDONLY)) < 0) { 86*0Sstevel@tonic-gate return (0); 87*0Sstevel@tonic-gate } 88*0Sstevel@tonic-gate lifn.lifn_family = af; 89*0Sstevel@tonic-gate /* LINTED ECONST_EXPR */ 90*0Sstevel@tonic-gate lifn.lifn_flags = IFF_UP & !(IFF_NOXMIT | IFF_DEPRECATED); 91*0Sstevel@tonic-gate if (ioctl(fd, SIOCGLIFNUM, &lifn, sizeof (lifn)) < 0) { 92*0Sstevel@tonic-gate lifn.lifn_count = 0; 93*0Sstevel@tonic-gate } 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate (void) close(fd); 96*0Sstevel@tonic-gate return (lifn.lifn_count); 97*0Sstevel@tonic-gate } 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate static boolean_t 100*0Sstevel@tonic-gate is_v6_netid(const char *netid) 101*0Sstevel@tonic-gate { 102*0Sstevel@tonic-gate return ((strcmp(netid, SOCKET_PROTO_TCP6) == 0) || 103*0Sstevel@tonic-gate (strcmp(netid, SOCKET_PROTO_UDP6) == 0)); 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* 107*0Sstevel@tonic-gate * Registers with rpcbind the program number with all versions, from low to 108*0Sstevel@tonic-gate * high, with the netid, all specified in 'rpc'. If registration fails, 109*0Sstevel@tonic-gate * returns -1, else 0. 110*0Sstevel@tonic-gate */ 111*0Sstevel@tonic-gate int 112*0Sstevel@tonic-gate register_rpc_service(const char *fmri, const rpc_info_t *rpc) 113*0Sstevel@tonic-gate { 114*0Sstevel@tonic-gate struct netconfig *nconf; 115*0Sstevel@tonic-gate int ver; 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate debug_msg("Entering register_rpc_service: instance: %s", fmri); 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate if ((nconf = getnetconfigent(rpc->netid)) == NULL) { 120*0Sstevel@tonic-gate /* 121*0Sstevel@tonic-gate * Check whether getnetconfigent() failed as a result of 122*0Sstevel@tonic-gate * having no IPv6 interfaces configured for a v6 netid, or 123*0Sstevel@tonic-gate * as a result of a 'real' error, and output an appropriate 124*0Sstevel@tonic-gate * message with an appropriate severity. 125*0Sstevel@tonic-gate */ 126*0Sstevel@tonic-gate if (is_v6_netid(rpc->netid) && !can_use_af(AF_INET6)) { 127*0Sstevel@tonic-gate warn_msg(gettext( 128*0Sstevel@tonic-gate "Couldn't register netid %s for RPC instance %s " 129*0Sstevel@tonic-gate "because no IPv6 interfaces are plumbed"), 130*0Sstevel@tonic-gate rpc->netid, fmri); 131*0Sstevel@tonic-gate } else { 132*0Sstevel@tonic-gate error_msg(gettext( 133*0Sstevel@tonic-gate "Failed to lookup netid '%s' for instance %s: %s"), 134*0Sstevel@tonic-gate rpc->netid, fmri, nc_sperror()); 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate return (-1); 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate for (ver = rpc->lowver; ver <= rpc->highver; ver++) { 140*0Sstevel@tonic-gate if (!rpcb_set(rpc->prognum, ver, nconf, &(rpc->netbuf))) { 141*0Sstevel@tonic-gate error_msg(gettext("Failed to register version %d " 142*0Sstevel@tonic-gate "of RPC service instance %s, netid %s"), ver, 143*0Sstevel@tonic-gate fmri, rpc->netid); 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate for (ver--; ver >= rpc->lowver; ver--) 146*0Sstevel@tonic-gate (void) rpcb_unset(rpc->prognum, ver, nconf); 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate freenetconfigent(nconf); 149*0Sstevel@tonic-gate return (-1); 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate freenetconfigent(nconf); 154*0Sstevel@tonic-gate return (0); 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate /* Unregister all the registrations done by register_rpc_service */ 158*0Sstevel@tonic-gate void 159*0Sstevel@tonic-gate unregister_rpc_service(const char *fmri, const rpc_info_t *rpc) 160*0Sstevel@tonic-gate { 161*0Sstevel@tonic-gate int ver; 162*0Sstevel@tonic-gate struct netconfig *nconf; 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate debug_msg("Entering unregister_rpc_service, instance: %s", fmri); 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate if ((nconf = getnetconfigent(rpc->netid)) == NULL) { 167*0Sstevel@tonic-gate /* 168*0Sstevel@tonic-gate * Don't output an error message if getnetconfigent() fails for 169*0Sstevel@tonic-gate * a v6 netid when an IPv6 interface isn't configured. 170*0Sstevel@tonic-gate */ 171*0Sstevel@tonic-gate if (!(is_v6_netid(rpc->netid) && !can_use_af(AF_INET6))) { 172*0Sstevel@tonic-gate error_msg(gettext( 173*0Sstevel@tonic-gate "Failed to lookup netid '%s' for instance %s: %s"), 174*0Sstevel@tonic-gate rpc->netid, fmri, nc_sperror()); 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate return; 177*0Sstevel@tonic-gate } 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate for (ver = rpc->lowver; ver <= rpc->highver; ver++) 180*0Sstevel@tonic-gate (void) rpcb_unset(rpc->prognum, ver, nconf); 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate freenetconfigent(nconf); 183*0Sstevel@tonic-gate } 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate /* 186*0Sstevel@tonic-gate * TLI/XTI functions. 187*0Sstevel@tonic-gate */ 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate int 190*0Sstevel@tonic-gate tlx_init(void) 191*0Sstevel@tonic-gate { 192*0Sstevel@tonic-gate if ((conn_ind_pool = uu_list_pool_create("conn_ind_pool", 193*0Sstevel@tonic-gate sizeof (tlx_conn_ind_t), offsetof(tlx_conn_ind_t, link), 194*0Sstevel@tonic-gate NULL, UU_LIST_POOL_DEBUG)) == NULL) { 195*0Sstevel@tonic-gate error_msg("%s: %s", gettext("Failed to create uu pool"), 196*0Sstevel@tonic-gate uu_strerror(uu_error())); 197*0Sstevel@tonic-gate return (-1); 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate return (0); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate void 204*0Sstevel@tonic-gate tlx_fini(void) 205*0Sstevel@tonic-gate { 206*0Sstevel@tonic-gate if (conn_ind_pool != NULL) { 207*0Sstevel@tonic-gate uu_list_pool_destroy(conn_ind_pool); 208*0Sstevel@tonic-gate conn_ind_pool = NULL; 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate /* 213*0Sstevel@tonic-gate * Checks if the contents of the 2 tlx_info_t structures are equivalent. 214*0Sstevel@tonic-gate * If 'isrpc' is false, the address components of the two structures are 215*0Sstevel@tonic-gate * compared for equality as part of this. If the two structures are 216*0Sstevel@tonic-gate * equivalent B_TRUE is returned, else B_FALSE. 217*0Sstevel@tonic-gate */ 218*0Sstevel@tonic-gate boolean_t 219*0Sstevel@tonic-gate tlx_info_equal(const tlx_info_t *ti, const tlx_info_t *ti2, boolean_t isrpc) 220*0Sstevel@tonic-gate { 221*0Sstevel@tonic-gate return ((isrpc || (memcmp(ti->local_addr.buf, ti2->local_addr.buf, 222*0Sstevel@tonic-gate sizeof (struct sockaddr_storage)) == 0)) && 223*0Sstevel@tonic-gate (strcmp(ti->dev_name, ti2->dev_name) == 0)); 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate /* 227*0Sstevel@tonic-gate * Attempts to bind an address to the network fd 'fd'. If 'reqaddr' is non-NULL, 228*0Sstevel@tonic-gate * it attempts to bind to that requested address, else it binds to a kernel 229*0Sstevel@tonic-gate * selected address. In the former case, the function returning success 230*0Sstevel@tonic-gate * doesn't guarantee that the requested address was bound (the caller needs to 231*0Sstevel@tonic-gate * check). If 'retaddr' is non-NULL, the bound address is returned in it. The 232*0Sstevel@tonic-gate * 'qlen' parameter is used to set the connection backlog. If the bind 233*0Sstevel@tonic-gate * succeeds 0 is returned, else -1. 234*0Sstevel@tonic-gate */ 235*0Sstevel@tonic-gate static int 236*0Sstevel@tonic-gate tlx_bind(int fd, const struct netbuf *reqaddr, struct netbuf *retaddr, int qlen) 237*0Sstevel@tonic-gate { 238*0Sstevel@tonic-gate struct t_bind breq; 239*0Sstevel@tonic-gate struct t_bind bret; 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate debug_msg("Entering tlx_bind: req: %x, ret: %x, qlen: %d", reqaddr, 242*0Sstevel@tonic-gate retaddr, qlen); 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate if (retaddr != NULL) { /* caller requests bound address be returned */ 246*0Sstevel@tonic-gate bret.addr.buf = retaddr->buf; 247*0Sstevel@tonic-gate bret.addr.maxlen = retaddr->maxlen; 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate if (reqaddr != NULL) { /* caller requests specific address */ 251*0Sstevel@tonic-gate breq.addr.buf = reqaddr->buf; 252*0Sstevel@tonic-gate breq.addr.len = reqaddr->len; 253*0Sstevel@tonic-gate } else { 254*0Sstevel@tonic-gate breq.addr.len = 0; 255*0Sstevel@tonic-gate } 256*0Sstevel@tonic-gate breq.qlen = qlen; 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate if (t_bind(fd, &breq, retaddr != NULL ? &bret : NULL) < 0) 259*0Sstevel@tonic-gate return (-1); 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate if (retaddr != NULL) 262*0Sstevel@tonic-gate retaddr->len = bret.addr.len; 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate return (0); 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate static int 268*0Sstevel@tonic-gate tlx_setsockopt(int fd, int level, int optname, const void *optval, 269*0Sstevel@tonic-gate socklen_t optlen) 270*0Sstevel@tonic-gate { 271*0Sstevel@tonic-gate struct t_optmgmt request, reply; 272*0Sstevel@tonic-gate struct { 273*0Sstevel@tonic-gate struct opthdr sockopt; 274*0Sstevel@tonic-gate char data[256]; 275*0Sstevel@tonic-gate } optbuf; 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate debug_msg("Entering tlx_setsockopt, " 278*0Sstevel@tonic-gate "fd: %d, level: %d, optname: %d, optval: %x, optlen: %d", 279*0Sstevel@tonic-gate fd, level, optname, optval, optlen); 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate if (optlen > sizeof (optbuf.data)) { 282*0Sstevel@tonic-gate error_msg(gettext("t_optmgmt request too long")); 283*0Sstevel@tonic-gate return (-1); 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate optbuf.sockopt.level = level; 287*0Sstevel@tonic-gate optbuf.sockopt.name = optname; 288*0Sstevel@tonic-gate optbuf.sockopt.len = optlen; 289*0Sstevel@tonic-gate (void) memcpy(optbuf.data, optval, optlen); 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate request.opt.len = sizeof (struct opthdr) + optlen; 292*0Sstevel@tonic-gate request.opt.buf = (char *)&optbuf; 293*0Sstevel@tonic-gate request.flags = T_NEGOTIATE; 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate reply.opt.maxlen = sizeof (struct opthdr) + optlen; 296*0Sstevel@tonic-gate reply.opt.buf = (char *)&optbuf; 297*0Sstevel@tonic-gate reply.flags = 0; 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate if ((t_optmgmt(fd, &request, &reply) == -1) || 300*0Sstevel@tonic-gate (reply.flags != T_SUCCESS)) { 301*0Sstevel@tonic-gate error_msg("t_optmgmt: %s", t_strerror(t_errno)); 302*0Sstevel@tonic-gate return (-1); 303*0Sstevel@tonic-gate } 304*0Sstevel@tonic-gate return (0); 305*0Sstevel@tonic-gate } 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate /* 308*0Sstevel@tonic-gate * Compare contents of netbuf for equality. Return B_TRUE on a match and 309*0Sstevel@tonic-gate * B_FALSE for mismatch. 310*0Sstevel@tonic-gate */ 311*0Sstevel@tonic-gate static boolean_t 312*0Sstevel@tonic-gate netbufs_equal(struct netbuf *n1, struct netbuf *n2) 313*0Sstevel@tonic-gate { 314*0Sstevel@tonic-gate return ((n1->len == n2->len) && 315*0Sstevel@tonic-gate (memcmp(n1->buf, n2->buf, (size_t)n1->len) == 0)); 316*0Sstevel@tonic-gate } 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate /* 319*0Sstevel@tonic-gate * Create a tli/xti endpoint, either bound to the address specified in 320*0Sstevel@tonic-gate * 'instance' for non-RPC services, else a kernel chosen address. 321*0Sstevel@tonic-gate * Returns -1 on failure, else 0. 322*0Sstevel@tonic-gate */ 323*0Sstevel@tonic-gate int 324*0Sstevel@tonic-gate create_bound_endpoint(const char *fmri, tlx_info_t *tlx_info) 325*0Sstevel@tonic-gate { 326*0Sstevel@tonic-gate int fd; 327*0Sstevel@tonic-gate int qlen; 328*0Sstevel@tonic-gate struct netbuf *reqaddr; 329*0Sstevel@tonic-gate struct netbuf *retaddr; 330*0Sstevel@tonic-gate struct netbuf netbuf; 331*0Sstevel@tonic-gate struct sockaddr_storage ss; 332*0Sstevel@tonic-gate rpc_info_t *rpc = tlx_info->pr_info.ri; 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gate debug_msg("Entering create_bound_endpoint"); 335*0Sstevel@tonic-gate 336*0Sstevel@tonic-gate if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) { 337*0Sstevel@tonic-gate error_msg(gettext("Failed to open transport %s for " 338*0Sstevel@tonic-gate "instance %s, proto %s: %s"), tlx_info->dev_name, 339*0Sstevel@tonic-gate fmri, tlx_info->pr_info.proto, t_strerror(t_errno)); 340*0Sstevel@tonic-gate return (-1); 341*0Sstevel@tonic-gate } 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate if (tlx_info->pr_info.v6only) { 344*0Sstevel@tonic-gate int on = 1; 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gate /* restrict to IPv6 communications only */ 347*0Sstevel@tonic-gate if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, 348*0Sstevel@tonic-gate sizeof (on)) == -1) { 349*0Sstevel@tonic-gate (void) t_close(fd); 350*0Sstevel@tonic-gate return (-1); 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate } 353*0Sstevel@tonic-gate 354*0Sstevel@tonic-gate /* 355*0Sstevel@tonic-gate * Negotiate for the returning of the remote uid for loopback 356*0Sstevel@tonic-gate * transports for RPC services. This needs to be done before the 357*0Sstevel@tonic-gate * endpoint is bound using t_bind(), so that any requests to it 358*0Sstevel@tonic-gate * contain the uid. 359*0Sstevel@tonic-gate */ 360*0Sstevel@tonic-gate if ((rpc != NULL) && (rpc->is_loopback)) 361*0Sstevel@tonic-gate svc_fd_negotiate_ucred(fd); 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate /* 364*0Sstevel@tonic-gate * Bind the service's address to the endpoint and setup connection 365*0Sstevel@tonic-gate * backlog. In the case of RPC services, we specify a NULL requested 366*0Sstevel@tonic-gate * address and accept what we're given, storing the returned address 367*0Sstevel@tonic-gate * for later RPC binding. In the case of non-RPC services we specify 368*0Sstevel@tonic-gate * the service's associated address. 369*0Sstevel@tonic-gate */ 370*0Sstevel@tonic-gate if (rpc != NULL) { 371*0Sstevel@tonic-gate reqaddr = NULL; 372*0Sstevel@tonic-gate retaddr = &(rpc->netbuf); 373*0Sstevel@tonic-gate } else { 374*0Sstevel@tonic-gate reqaddr = &(tlx_info->local_addr); 375*0Sstevel@tonic-gate netbuf.buf = (char *)&ss; 376*0Sstevel@tonic-gate netbuf.maxlen = sizeof (ss); 377*0Sstevel@tonic-gate retaddr = &netbuf; 378*0Sstevel@tonic-gate } 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gate qlen = CONNECTION_BACKLOG; /* ignored for conn/less services */ 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate if ((tlx_bind(fd, reqaddr, retaddr, qlen) == -1) || 383*0Sstevel@tonic-gate ((reqaddr != NULL) && !netbufs_equal(reqaddr, retaddr))) { 384*0Sstevel@tonic-gate error_msg(gettext("Failed to bind to the requested address " 385*0Sstevel@tonic-gate "for instance %s, proto %s"), fmri, 386*0Sstevel@tonic-gate tlx_info->pr_info.proto); 387*0Sstevel@tonic-gate (void) t_close(fd); 388*0Sstevel@tonic-gate return (-1); 389*0Sstevel@tonic-gate } 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate return (fd); 392*0Sstevel@tonic-gate } 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate /* 395*0Sstevel@tonic-gate * Takes a connection request off 'fd' in the form of a t_call structure 396*0Sstevel@tonic-gate * and returns a pointer to it. 397*0Sstevel@tonic-gate * Returns NULL on failure, else pointer to t_call structure on success. 398*0Sstevel@tonic-gate */ 399*0Sstevel@tonic-gate static struct t_call * 400*0Sstevel@tonic-gate get_new_conind(int fd) 401*0Sstevel@tonic-gate { 402*0Sstevel@tonic-gate struct t_call *call; 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate debug_msg("Entering get_new_conind"); 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 407*0Sstevel@tonic-gate if ((call = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL) { 408*0Sstevel@tonic-gate error_msg("t_alloc: %s", t_strerror(t_errno)); 409*0Sstevel@tonic-gate return (NULL); 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate if (t_listen(fd, call) < 0) { 412*0Sstevel@tonic-gate error_msg("t_listen: %s", t_strerror(t_errno)); 413*0Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 414*0Sstevel@tonic-gate return (NULL); 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gate return (call); 418*0Sstevel@tonic-gate } 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate /* Add 'call' to the connection indication queue 'queue'. */ 421*0Sstevel@tonic-gate int 422*0Sstevel@tonic-gate queue_conind(uu_list_t *queue, struct t_call *call) 423*0Sstevel@tonic-gate { 424*0Sstevel@tonic-gate tlx_conn_ind_t *ci; 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate if ((ci = malloc(sizeof (tlx_conn_ind_t))) == NULL) { 427*0Sstevel@tonic-gate error_msg(strerror(errno)); 428*0Sstevel@tonic-gate return (-1); 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate ci->call = call; 432*0Sstevel@tonic-gate uu_list_node_init(ci, &ci->link, conn_ind_pool); 433*0Sstevel@tonic-gate (void) uu_list_insert_after(queue, NULL, ci); 434*0Sstevel@tonic-gate 435*0Sstevel@tonic-gate return (0); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate /* 439*0Sstevel@tonic-gate * Remove and return a pointer to the first call on queue 'queue'. However, 440*0Sstevel@tonic-gate * if the queue is empty returns NULL. 441*0Sstevel@tonic-gate */ 442*0Sstevel@tonic-gate struct t_call * 443*0Sstevel@tonic-gate dequeue_conind(uu_list_t *queue) 444*0Sstevel@tonic-gate { 445*0Sstevel@tonic-gate struct t_call *ret; 446*0Sstevel@tonic-gate tlx_conn_ind_t *ci = uu_list_first(queue); 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate if (ci == NULL) 449*0Sstevel@tonic-gate return (NULL); 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gate ret = ci->call; 452*0Sstevel@tonic-gate uu_list_remove(queue, ci); 453*0Sstevel@tonic-gate free(ci); 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gate return (ret); 456*0Sstevel@tonic-gate } 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate /* 459*0Sstevel@tonic-gate * Handle a TLOOK notification received during a t_accept() call. 460*0Sstevel@tonic-gate * Returns -1 on failure, else 0. 461*0Sstevel@tonic-gate */ 462*0Sstevel@tonic-gate static int 463*0Sstevel@tonic-gate process_tlook(const char *fmri, tlx_info_t *tlx_info) 464*0Sstevel@tonic-gate { 465*0Sstevel@tonic-gate int event; 466*0Sstevel@tonic-gate int fd = tlx_info->pr_info.listen_fd; 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate debug_msg("Entering process_tlook:"); 469*0Sstevel@tonic-gate 470*0Sstevel@tonic-gate switch (event = t_look(fd)) { 471*0Sstevel@tonic-gate case T_LISTEN: { 472*0Sstevel@tonic-gate struct t_call *call; 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate debug_msg("process_tlook: T_LISTEN event"); 475*0Sstevel@tonic-gate if ((call = get_new_conind(fd)) == NULL) 476*0Sstevel@tonic-gate return (-1); 477*0Sstevel@tonic-gate if (queue_conind(tlx_info->conn_ind_queue, call) == -1) { 478*0Sstevel@tonic-gate error_msg(gettext("Failed to queue connection " 479*0Sstevel@tonic-gate "indication for instance %s"), fmri); 480*0Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 481*0Sstevel@tonic-gate return (-1); 482*0Sstevel@tonic-gate } 483*0Sstevel@tonic-gate break; 484*0Sstevel@tonic-gate } 485*0Sstevel@tonic-gate case T_DISCONNECT: { 486*0Sstevel@tonic-gate /* 487*0Sstevel@tonic-gate * Note: In Solaris 2.X (SunOS 5.X) bundled 488*0Sstevel@tonic-gate * connection-oriented transport drivers 489*0Sstevel@tonic-gate * [ e.g /dev/tcp and /dev/ticots and 490*0Sstevel@tonic-gate * /dev/ticotsord (tl)] we do not send disconnect 491*0Sstevel@tonic-gate * indications to listening endpoints. 492*0Sstevel@tonic-gate * So this will not be seen with endpoints on Solaris 493*0Sstevel@tonic-gate * bundled transport devices. However, Streams TPI 494*0Sstevel@tonic-gate * allows for this (broken?) behavior and so we account 495*0Sstevel@tonic-gate * for it here because of the possibility of unbundled 496*0Sstevel@tonic-gate * transport drivers causing this. 497*0Sstevel@tonic-gate */ 498*0Sstevel@tonic-gate tlx_conn_ind_t *cip; 499*0Sstevel@tonic-gate struct t_discon *discon; 500*0Sstevel@tonic-gate 501*0Sstevel@tonic-gate debug_msg("process_tlook: T_DISCONNECT event"); 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate /* LINTED */ 504*0Sstevel@tonic-gate if ((discon = (struct t_discon *) 505*0Sstevel@tonic-gate t_alloc(fd, T_DIS, T_ALL)) == NULL) { 506*0Sstevel@tonic-gate error_msg("t_alloc: %s", t_strerror(t_errno)); 507*0Sstevel@tonic-gate return (-1); 508*0Sstevel@tonic-gate } 509*0Sstevel@tonic-gate if (t_rcvdis(fd, discon) < 0) { 510*0Sstevel@tonic-gate error_msg("t_rcvdis: %s", t_strerror(t_errno)); 511*0Sstevel@tonic-gate (void) t_free((char *)discon, T_DIS); 512*0Sstevel@tonic-gate return (-1); 513*0Sstevel@tonic-gate } 514*0Sstevel@tonic-gate 515*0Sstevel@tonic-gate /* 516*0Sstevel@tonic-gate * Find any queued connection pending that matches this 517*0Sstevel@tonic-gate * disconnect notice and remove from the pending queue. 518*0Sstevel@tonic-gate */ 519*0Sstevel@tonic-gate cip = uu_list_first(tlx_info->conn_ind_queue); 520*0Sstevel@tonic-gate while ((cip != NULL) && 521*0Sstevel@tonic-gate (cip->call->sequence != discon->sequence)) { 522*0Sstevel@tonic-gate cip = uu_list_next(tlx_info->conn_ind_queue, cip); 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate if (cip != NULL) { /* match found */ 525*0Sstevel@tonic-gate uu_list_remove(tlx_info->conn_ind_queue, cip); 526*0Sstevel@tonic-gate (void) t_free((char *)cip->call, T_CALL); 527*0Sstevel@tonic-gate free(cip); 528*0Sstevel@tonic-gate } 529*0Sstevel@tonic-gate 530*0Sstevel@tonic-gate (void) t_free((char *)discon, T_DIS); 531*0Sstevel@tonic-gate break; 532*0Sstevel@tonic-gate } 533*0Sstevel@tonic-gate case -1: 534*0Sstevel@tonic-gate error_msg("t_look: %s", t_errno); 535*0Sstevel@tonic-gate return (-1); 536*0Sstevel@tonic-gate default: 537*0Sstevel@tonic-gate error_msg(gettext("do_tlook: unexpected t_look event: %d"), 538*0Sstevel@tonic-gate event); 539*0Sstevel@tonic-gate return (-1); 540*0Sstevel@tonic-gate } 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate return (0); 543*0Sstevel@tonic-gate } 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate /* 546*0Sstevel@tonic-gate * This call attempts to t_accept() an incoming/pending TLI connection. 547*0Sstevel@tonic-gate * If it is thwarted by a TLOOK, it is deferred and whatever is on the 548*0Sstevel@tonic-gate * file descriptor, removed after a t_look. (Incoming connect indications 549*0Sstevel@tonic-gate * get queued for later processing and disconnect indications remove a 550*0Sstevel@tonic-gate * a queued connection request if a match found). 551*0Sstevel@tonic-gate * Returns -1 on failure, else 0. 552*0Sstevel@tonic-gate */ 553*0Sstevel@tonic-gate int 554*0Sstevel@tonic-gate tlx_accept(const char *fmri, tlx_info_t *tlx_info, 555*0Sstevel@tonic-gate struct sockaddr_storage *remote_addr) 556*0Sstevel@tonic-gate { 557*0Sstevel@tonic-gate tlx_conn_ind_t *conind; 558*0Sstevel@tonic-gate struct t_call *call; 559*0Sstevel@tonic-gate int fd; 560*0Sstevel@tonic-gate int listen_fd = tlx_info->pr_info.listen_fd; 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gate debug_msg("Entering tlx_accept: instance: %s", fmri); 563*0Sstevel@tonic-gate 564*0Sstevel@tonic-gate if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) { 565*0Sstevel@tonic-gate error_msg("t_open: %s", t_strerror(t_errno)); 566*0Sstevel@tonic-gate return (-1); 567*0Sstevel@tonic-gate } 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate if (tlx_info->pr_info.v6only) { 570*0Sstevel@tonic-gate int on = 1; 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gate /* restrict to IPv6 communications only */ 573*0Sstevel@tonic-gate if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, 574*0Sstevel@tonic-gate sizeof (on)) == -1) { 575*0Sstevel@tonic-gate (void) t_close(fd); 576*0Sstevel@tonic-gate return (-1); 577*0Sstevel@tonic-gate } 578*0Sstevel@tonic-gate } 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate if (t_bind(fd, NULL, NULL) == -1) { 581*0Sstevel@tonic-gate error_msg("t_bind: %s", t_strerror(t_errno)); 582*0Sstevel@tonic-gate (void) t_close(fd); 583*0Sstevel@tonic-gate return (-1); 584*0Sstevel@tonic-gate } 585*0Sstevel@tonic-gate 586*0Sstevel@tonic-gate /* 587*0Sstevel@tonic-gate * Get the next connection indication - first try the pending 588*0Sstevel@tonic-gate * queue, then, if none there, get a new one from the file descriptor. 589*0Sstevel@tonic-gate */ 590*0Sstevel@tonic-gate if ((conind = uu_list_first(tlx_info->conn_ind_queue)) != NULL) { 591*0Sstevel@tonic-gate debug_msg("taking con off queue"); 592*0Sstevel@tonic-gate call = conind->call; 593*0Sstevel@tonic-gate } else if ((call = get_new_conind(listen_fd)) == NULL) { 594*0Sstevel@tonic-gate (void) t_close(fd); 595*0Sstevel@tonic-gate return (-1); 596*0Sstevel@tonic-gate } 597*0Sstevel@tonic-gate 598*0Sstevel@tonic-gate /* 599*0Sstevel@tonic-gate * Accept the connection indication on the newly created endpoint. 600*0Sstevel@tonic-gate * If we fail, and it's the result of a tlook, queue the indication 601*0Sstevel@tonic-gate * if it isn't already, and go and process the t_look. 602*0Sstevel@tonic-gate */ 603*0Sstevel@tonic-gate if (t_accept(listen_fd, fd, call) == -1) { 604*0Sstevel@tonic-gate if (t_errno == TLOOK) { 605*0Sstevel@tonic-gate if (uu_list_first(tlx_info->conn_ind_queue) == NULL) { 606*0Sstevel@tonic-gate /* 607*0Sstevel@tonic-gate * We are first one to have to defer accepting 608*0Sstevel@tonic-gate * and start the pending connections list. 609*0Sstevel@tonic-gate */ 610*0Sstevel@tonic-gate if (queue_conind(tlx_info->conn_ind_queue, 611*0Sstevel@tonic-gate call) == -1) { 612*0Sstevel@tonic-gate error_msg(gettext( 613*0Sstevel@tonic-gate "Failed to queue connection " 614*0Sstevel@tonic-gate "indication for instance %s"), 615*0Sstevel@tonic-gate fmri); 616*0Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 617*0Sstevel@tonic-gate return (-1); 618*0Sstevel@tonic-gate } 619*0Sstevel@tonic-gate } 620*0Sstevel@tonic-gate (void) process_tlook(fmri, tlx_info); 621*0Sstevel@tonic-gate } else { /* non-TLOOK accept failure */ 622*0Sstevel@tonic-gate error_msg("%s: %s", "t_accept failed", 623*0Sstevel@tonic-gate t_strerror(t_errno)); 624*0Sstevel@tonic-gate /* 625*0Sstevel@tonic-gate * If we were accepting a queued connection, dequeue 626*0Sstevel@tonic-gate * it. 627*0Sstevel@tonic-gate */ 628*0Sstevel@tonic-gate if (uu_list_first(tlx_info->conn_ind_queue) != NULL) 629*0Sstevel@tonic-gate (void) dequeue_conind(tlx_info->conn_ind_queue); 630*0Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 631*0Sstevel@tonic-gate } 632*0Sstevel@tonic-gate 633*0Sstevel@tonic-gate (void) t_close(fd); 634*0Sstevel@tonic-gate return (-1); 635*0Sstevel@tonic-gate } 636*0Sstevel@tonic-gate 637*0Sstevel@tonic-gate /* Copy remote address into address parameter */ 638*0Sstevel@tonic-gate (void) memcpy(remote_addr, call->addr.buf, 639*0Sstevel@tonic-gate MIN(call->addr.len, sizeof (*remote_addr))); 640*0Sstevel@tonic-gate 641*0Sstevel@tonic-gate /* If we were accepting a queued connection, dequeue it. */ 642*0Sstevel@tonic-gate if (uu_list_first(tlx_info->conn_ind_queue) != NULL) 643*0Sstevel@tonic-gate (void) dequeue_conind(tlx_info->conn_ind_queue); 644*0Sstevel@tonic-gate (void) t_free((char *)call, T_CALL); 645*0Sstevel@tonic-gate 646*0Sstevel@tonic-gate return (fd); 647*0Sstevel@tonic-gate } 648*0Sstevel@tonic-gate 649*0Sstevel@tonic-gate /* protocol independent network fd close routine */ 650*0Sstevel@tonic-gate void 651*0Sstevel@tonic-gate close_net_fd(instance_t *inst, int fd) 652*0Sstevel@tonic-gate { 653*0Sstevel@tonic-gate debug_msg("Entering close_net_fd: inst: %s, fd: %d", inst->fmri, fd); 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gate if (inst->config->basic->istlx) { 656*0Sstevel@tonic-gate (void) t_close(fd); 657*0Sstevel@tonic-gate } else { 658*0Sstevel@tonic-gate (void) close(fd); 659*0Sstevel@tonic-gate } 660*0Sstevel@tonic-gate } 661*0Sstevel@tonic-gate 662*0Sstevel@tonic-gate /* 663*0Sstevel@tonic-gate * Consume some data from the given endpoint of the given wait-based instance. 664*0Sstevel@tonic-gate */ 665*0Sstevel@tonic-gate void 666*0Sstevel@tonic-gate consume_wait_data(instance_t *inst, int fd) 667*0Sstevel@tonic-gate { 668*0Sstevel@tonic-gate int flag; 669*0Sstevel@tonic-gate char buf[50]; /* same arbitrary size as old inetd */ 670*0Sstevel@tonic-gate 671*0Sstevel@tonic-gate debug_msg("Entering consume_wait_data: inst: %s, fd: %d", inst->fmri, 672*0Sstevel@tonic-gate fd); 673*0Sstevel@tonic-gate 674*0Sstevel@tonic-gate if (inst->config->basic->istlx) { 675*0Sstevel@tonic-gate (void) t_rcv(fd, buf, sizeof (buf), &flag); 676*0Sstevel@tonic-gate } else { 677*0Sstevel@tonic-gate (void) recv(fd, buf, sizeof (buf), 0); 678*0Sstevel@tonic-gate } 679*0Sstevel@tonic-gate } 680