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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*61961e0fSrobinson */ 22*61961e0fSrobinson 23*61961e0fSrobinson /* 24*61961e0fSrobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 317c478bd9Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of 327c478bd9Sstevel@tonic-gate * California. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * clnt_bcast.c 397c478bd9Sstevel@tonic-gate * Client interface to broadcast service. 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * The following is kludged-up support for simple rpc broadcasts. 427c478bd9Sstevel@tonic-gate * Someday a large, complicated system will replace these routines. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #include <string.h> 46*61961e0fSrobinson #include <strings.h> 477c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 487c478bd9Sstevel@tonic-gate #include <rpc/nettype.h> 497c478bd9Sstevel@tonic-gate #include <sys/poll.h> 507c478bd9Sstevel@tonic-gate #include <netdir.h> 517c478bd9Sstevel@tonic-gate #ifdef PORTMAP 527c478bd9Sstevel@tonic-gate #include <rpc/pmap_prot.h> 537c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h> 547c478bd9Sstevel@tonic-gate #include <rpc/pmap_rmt.h> 557c478bd9Sstevel@tonic-gate #endif 567c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 577c478bd9Sstevel@tonic-gate #include <stdio.h> 587c478bd9Sstevel@tonic-gate #endif 597c478bd9Sstevel@tonic-gate #include <errno.h> 607c478bd9Sstevel@tonic-gate #include <syslog.h> 617c478bd9Sstevel@tonic-gate #include <stdlib.h> 627c478bd9Sstevel@tonic-gate #include <unistd.h> 63*61961e0fSrobinson #include <sys/types.h> 64*61961e0fSrobinson #include <sys/socket.h> 65*61961e0fSrobinson #include <netinet/in.h> 66*61961e0fSrobinson #include <arpa/inet.h> 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #define MAXBCAST 20 /* Max no of broadcasting transports */ 697c478bd9Sstevel@tonic-gate #define INITTIME 4000 /* Time to wait initially */ 707c478bd9Sstevel@tonic-gate #define WAITTIME 8000 /* Maximum time to wait */ 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate int lowvers = 1; /* by default, broadcast only version 2 over UDP */ 737c478bd9Sstevel@tonic-gate #ifndef NETIDLEN 747c478bd9Sstevel@tonic-gate #define NETIDLEN 32 757c478bd9Sstevel@tonic-gate #endif 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * If nettype is NULL, it broadcasts on all the available 797c478bd9Sstevel@tonic-gate * datagram_n transports. May potentially lead to broadacst storms 807c478bd9Sstevel@tonic-gate * and hence should be used with caution, care and courage. 817c478bd9Sstevel@tonic-gate * 827c478bd9Sstevel@tonic-gate * The current parameter xdr packet size is limited by the max tsdu 837c478bd9Sstevel@tonic-gate * size of the transport. If the max tsdu size of any transport is 847c478bd9Sstevel@tonic-gate * smaller than the parameter xdr packet, then broadcast is not 857c478bd9Sstevel@tonic-gate * sent on that transport. 867c478bd9Sstevel@tonic-gate * 877c478bd9Sstevel@tonic-gate * Also, the packet size should be less the packet size of 887c478bd9Sstevel@tonic-gate * the data link layer (for ethernet it is 1400 bytes). There is 897c478bd9Sstevel@tonic-gate * no easy way to find out the max size of the data link layer and 907c478bd9Sstevel@tonic-gate * we are assuming that the args would be smaller than that. 917c478bd9Sstevel@tonic-gate * 927c478bd9Sstevel@tonic-gate * The result size has to be smaller than the transport tsdu size. 937c478bd9Sstevel@tonic-gate * 947c478bd9Sstevel@tonic-gate * If PORTMAP has been defined, we send two packets for UDP, one for 957c478bd9Sstevel@tonic-gate * rpcbind and one for portmap. For those machines which support 967c478bd9Sstevel@tonic-gate * both rpcbind and portmap, it will cause them to reply twice, and 977c478bd9Sstevel@tonic-gate * also here it will get two responses ... inefficient and clumsy. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate enum clnt_stat 102*61961e0fSrobinson rpc_broadcast_exp(const rpcprog_t prog, const rpcvers_t vers, 103*61961e0fSrobinson const rpcproc_t proc, const xdrproc_t xargs, caddr_t argsp, 104*61961e0fSrobinson const xdrproc_t xresults, caddr_t resultsp, const resultproc_t eachresult, 105*61961e0fSrobinson const int inittime, const int waittime, const char *netclass) 1067c478bd9Sstevel@tonic-gate { 1077c478bd9Sstevel@tonic-gate enum clnt_stat stat = RPC_SUCCESS; /* Return status */ 108*61961e0fSrobinson XDR xdr_stream; /* XDR stream */ 109*61961e0fSrobinson XDR *xdrs = &xdr_stream; 1107c478bd9Sstevel@tonic-gate struct rpc_msg msg; /* RPC message */ 1117c478bd9Sstevel@tonic-gate struct timeval t; 112*61961e0fSrobinson char *outbuf = NULL; /* Broadcast msg buffer */ 1137c478bd9Sstevel@tonic-gate char *inbuf = NULL; /* Reply buf */ 1147c478bd9Sstevel@tonic-gate uint_t maxbufsize = 0; 115*61961e0fSrobinson AUTH *sys_auth = authsys_create_default(); 116*61961e0fSrobinson int i, j; 1177c478bd9Sstevel@tonic-gate void *handle; 1187c478bd9Sstevel@tonic-gate char uaddress[1024]; /* A self imposed limit */ 1197c478bd9Sstevel@tonic-gate char *uaddrp = uaddress; 120*61961e0fSrobinson int pmap_reply_flag; /* reply recvd from PORTMAP */ 1217c478bd9Sstevel@tonic-gate /* An array of all the suitable broadcast transports */ 1227c478bd9Sstevel@tonic-gate struct { 1237c478bd9Sstevel@tonic-gate int fd; /* File descriptor */ 1247c478bd9Sstevel@tonic-gate bool_t udp_flag; /* this is udp */ 1257c478bd9Sstevel@tonic-gate struct netconfig *nconf; /* Netconfig structure */ 1267c478bd9Sstevel@tonic-gate uint_t asize; /* Size of the addr buf */ 1277c478bd9Sstevel@tonic-gate uint_t dsize; /* Size of the data buf */ 1287c478bd9Sstevel@tonic-gate struct netbuf raddr; /* Remote address */ 1297c478bd9Sstevel@tonic-gate struct nd_addrlist *nal; /* Broadcast addrs */ 1307c478bd9Sstevel@tonic-gate } fdlist[MAXBCAST]; 1317c478bd9Sstevel@tonic-gate struct pollfd pfd[MAXBCAST]; 132*61961e0fSrobinson int fdlistno = 0; 1337c478bd9Sstevel@tonic-gate struct r_rpcb_rmtcallargs barg; /* Remote arguments */ 1347c478bd9Sstevel@tonic-gate struct r_rpcb_rmtcallres bres; /* Remote results */ 1357c478bd9Sstevel@tonic-gate struct t_unitdata t_udata, t_rdata; 1367c478bd9Sstevel@tonic-gate struct netconfig *nconf; 1377c478bd9Sstevel@tonic-gate struct nd_hostserv hs; 1387c478bd9Sstevel@tonic-gate int msec; 1397c478bd9Sstevel@tonic-gate int pollretval; 1407c478bd9Sstevel@tonic-gate int fds_found; 1417c478bd9Sstevel@tonic-gate char nettype_array[NETIDLEN]; 1427c478bd9Sstevel@tonic-gate char *nettype = &nettype_array[0]; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate #ifdef PORTMAP 1457c478bd9Sstevel@tonic-gate rpcport_t *port; /* Remote port number */ 1467c478bd9Sstevel@tonic-gate int pmap_flag = 0; /* UDP exists ? */ 1477c478bd9Sstevel@tonic-gate char *outbuf_pmap = NULL; 1487c478bd9Sstevel@tonic-gate struct p_rmtcallargs barg_pmap; /* Remote arguments */ 1497c478bd9Sstevel@tonic-gate struct p_rmtcallres bres_pmap; /* Remote results */ 1507c478bd9Sstevel@tonic-gate struct t_unitdata t_udata_pmap; 1517c478bd9Sstevel@tonic-gate int udpbufsz = 0; 1527c478bd9Sstevel@tonic-gate #endif /* PORTMAP */ 1537c478bd9Sstevel@tonic-gate 154*61961e0fSrobinson if (sys_auth == NULL) 1557c478bd9Sstevel@tonic-gate return (RPC_SYSTEMERROR); 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * initialization: create a fd, a broadcast address, and send the 1587c478bd9Sstevel@tonic-gate * request on the broadcast transport. 1597c478bd9Sstevel@tonic-gate * Listen on all of them and on replies, call the user supplied 1607c478bd9Sstevel@tonic-gate * function. 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (netclass == NULL) 1647c478bd9Sstevel@tonic-gate nettype = NULL; 1657c478bd9Sstevel@tonic-gate else { 1667c478bd9Sstevel@tonic-gate size_t len = strlen(netclass); 167*61961e0fSrobinson if (len >= sizeof (nettype_array)) 1687c478bd9Sstevel@tonic-gate return (RPC_UNKNOWNPROTO); 169*61961e0fSrobinson (void) strcpy(nettype, netclass); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate if (nettype == NULL) 1737c478bd9Sstevel@tonic-gate nettype = "datagram_n"; 174*61961e0fSrobinson if ((handle = __rpc_setconf((char *)nettype)) == NULL) 1757c478bd9Sstevel@tonic-gate return (RPC_UNKNOWNPROTO); 1767c478bd9Sstevel@tonic-gate while (nconf = __rpc_getconf(handle)) { 1777c478bd9Sstevel@tonic-gate struct t_info tinfo; 1787c478bd9Sstevel@tonic-gate int fd; 1797c478bd9Sstevel@tonic-gate uint_t addrlen; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate if (nconf->nc_semantics != NC_TPI_CLTS) 1827c478bd9Sstevel@tonic-gate continue; 1837c478bd9Sstevel@tonic-gate if (fdlistno >= MAXBCAST) 1847c478bd9Sstevel@tonic-gate break; /* No more slots available */ 1857c478bd9Sstevel@tonic-gate if ((fd = t_open(nconf->nc_device, O_RDWR, &tinfo)) == -1) { 1867c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 1877c478bd9Sstevel@tonic-gate continue; 1887c478bd9Sstevel@tonic-gate } 189*61961e0fSrobinson if (t_bind(fd, NULL, NULL) == -1) { 1907c478bd9Sstevel@tonic-gate (void) t_close(fd); 1917c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 1927c478bd9Sstevel@tonic-gate continue; 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate /* Do protocol specific negotiating for broadcast */ 1957c478bd9Sstevel@tonic-gate if (netdir_options(nconf, ND_SET_BROADCAST, fd, NULL)) { 1967c478bd9Sstevel@tonic-gate (void) t_close(fd); 1977c478bd9Sstevel@tonic-gate stat = RPC_NOBROADCAST; 1987c478bd9Sstevel@tonic-gate continue; 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate fdlist[fdlistno].fd = fd; 2017c478bd9Sstevel@tonic-gate fdlist[fdlistno].nconf = nconf; 2027c478bd9Sstevel@tonic-gate fdlist[fdlistno].udp_flag = FALSE; 2037c478bd9Sstevel@tonic-gate if (((addrlen = __rpc_get_a_size(tinfo.addr)) == 0) || 2047c478bd9Sstevel@tonic-gate ((fdlist[fdlistno].raddr.buf = malloc(addrlen)) == NULL)) { 205*61961e0fSrobinson (void) t_close(fd); 2067c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; 2077c478bd9Sstevel@tonic-gate goto done_broad; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate fdlist[fdlistno].raddr.maxlen = addrlen; 2107c478bd9Sstevel@tonic-gate fdlist[fdlistno].raddr.len = addrlen; 2117c478bd9Sstevel@tonic-gate pfd[fdlistno].events = POLLIN | POLLPRI | 2127c478bd9Sstevel@tonic-gate POLLRDNORM | POLLRDBAND; 2137c478bd9Sstevel@tonic-gate pfd[fdlistno].fd = fdlist[fdlistno].fd = fd; 2147c478bd9Sstevel@tonic-gate fdlist[fdlistno].asize = addrlen; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate if ((fdlist[fdlistno].dsize = __rpc_get_t_size(0, 2177c478bd9Sstevel@tonic-gate tinfo.tsdu)) == 0) { 218*61961e0fSrobinson (void) t_close(fd); 2197c478bd9Sstevel@tonic-gate free(fdlist[fdlistno].raddr.buf); 2207c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; /* XXX */ 2217c478bd9Sstevel@tonic-gate goto done_broad; 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate if (maxbufsize <= fdlist[fdlistno].dsize) 2257c478bd9Sstevel@tonic-gate maxbufsize = fdlist[fdlistno].dsize; 2267c478bd9Sstevel@tonic-gate #ifdef PORTMAP 2277c478bd9Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET) == 0 && 2287c478bd9Sstevel@tonic-gate strcmp(nconf->nc_proto, NC_UDP) == 0) { 2297c478bd9Sstevel@tonic-gate udpbufsz = fdlist[fdlistno].dsize; 2307c478bd9Sstevel@tonic-gate if ((outbuf_pmap = malloc(udpbufsz)) == NULL) { 231*61961e0fSrobinson (void) t_close(fd); 2327c478bd9Sstevel@tonic-gate free(fdlist[fdlistno].raddr.buf); 2337c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; 2347c478bd9Sstevel@tonic-gate goto done_broad; 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate pmap_flag = 1; 2377c478bd9Sstevel@tonic-gate fdlist[fdlistno].udp_flag = TRUE; 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate #endif 2407c478bd9Sstevel@tonic-gate fdlistno++; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate if (fdlistno == 0) { 2447c478bd9Sstevel@tonic-gate if (stat == RPC_SUCCESS) 2457c478bd9Sstevel@tonic-gate stat = RPC_UNKNOWNPROTO; 2467c478bd9Sstevel@tonic-gate goto done_broad; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate if (maxbufsize == 0) { 2497c478bd9Sstevel@tonic-gate if (stat == RPC_SUCCESS) 2507c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 2517c478bd9Sstevel@tonic-gate goto done_broad; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate inbuf = malloc((size_t)maxbufsize); 2547c478bd9Sstevel@tonic-gate outbuf = malloc((size_t)maxbufsize); 2557c478bd9Sstevel@tonic-gate if ((inbuf == NULL) || (outbuf == NULL)) { 2567c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; 2577c478bd9Sstevel@tonic-gate goto done_broad; 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate /* Serialize all the arguments which have to be sent */ 2617c478bd9Sstevel@tonic-gate (void) gettimeofday(&t, (struct timezone *)0); 2627c478bd9Sstevel@tonic-gate msg.rm_xid = getpid() ^ t.tv_sec ^ t.tv_usec; 2637c478bd9Sstevel@tonic-gate msg.rm_direction = CALL; 2647c478bd9Sstevel@tonic-gate msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 2657c478bd9Sstevel@tonic-gate msg.rm_call.cb_prog = RPCBPROG; 2667c478bd9Sstevel@tonic-gate msg.rm_call.cb_vers = RPCBVERS; 2677c478bd9Sstevel@tonic-gate msg.rm_call.cb_proc = RPCBPROC_CALLIT; 2687c478bd9Sstevel@tonic-gate barg.prog = prog; 2697c478bd9Sstevel@tonic-gate barg.vers = vers; 2707c478bd9Sstevel@tonic-gate barg.proc = proc; 2717c478bd9Sstevel@tonic-gate barg.args.args_val = argsp; 2727c478bd9Sstevel@tonic-gate barg.xdr_args = xargs; 2737c478bd9Sstevel@tonic-gate bres.addr = uaddrp; 2747c478bd9Sstevel@tonic-gate bres.results.results_val = resultsp; 2757c478bd9Sstevel@tonic-gate bres.xdr_res = xresults; 2767c478bd9Sstevel@tonic-gate msg.rm_call.cb_cred = sys_auth->ah_cred; 2777c478bd9Sstevel@tonic-gate msg.rm_call.cb_verf = sys_auth->ah_verf; 2787c478bd9Sstevel@tonic-gate xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE); 279*61961e0fSrobinson if ((!xdr_callmsg(xdrs, &msg)) || 280*61961e0fSrobinson (!xdr_rpcb_rmtcallargs(xdrs, &barg))) { 2817c478bd9Sstevel@tonic-gate stat = RPC_CANTENCODEARGS; 2827c478bd9Sstevel@tonic-gate goto done_broad; 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate t_udata.opt.len = 0; 2857c478bd9Sstevel@tonic-gate t_udata.udata.buf = outbuf; 2867c478bd9Sstevel@tonic-gate t_udata.udata.len = xdr_getpos(xdrs); 2877c478bd9Sstevel@tonic-gate t_udata.udata.maxlen = t_udata.udata.len; 2887c478bd9Sstevel@tonic-gate /* XXX Should have set opt to its legal maxlen. */ 2897c478bd9Sstevel@tonic-gate t_rdata.opt.len = t_rdata.opt.maxlen = 0; 2907c478bd9Sstevel@tonic-gate xdr_destroy(xdrs); 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate #ifdef PORTMAP 2937c478bd9Sstevel@tonic-gate /* Prepare the packet for version 2 PORTMAP */ 2947c478bd9Sstevel@tonic-gate if (pmap_flag) { 2957c478bd9Sstevel@tonic-gate msg.rm_xid++; /* One way to distinguish */ 2967c478bd9Sstevel@tonic-gate msg.rm_call.cb_prog = PMAPPROG; 2977c478bd9Sstevel@tonic-gate msg.rm_call.cb_vers = PMAPVERS; 2987c478bd9Sstevel@tonic-gate msg.rm_call.cb_proc = PMAPPROC_CALLIT; 2997c478bd9Sstevel@tonic-gate barg_pmap.prog = prog; 3007c478bd9Sstevel@tonic-gate barg_pmap.vers = vers; 3017c478bd9Sstevel@tonic-gate barg_pmap.proc = proc; 3027c478bd9Sstevel@tonic-gate barg_pmap.args.args_val = argsp; 3037c478bd9Sstevel@tonic-gate barg_pmap.xdr_args = xargs; 3047c478bd9Sstevel@tonic-gate port = &bres_pmap.port; /* for use later on */ 3057c478bd9Sstevel@tonic-gate bres_pmap.xdr_res = xresults; 3067c478bd9Sstevel@tonic-gate bres_pmap.res.res_val = resultsp; 3077c478bd9Sstevel@tonic-gate xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE); 308*61961e0fSrobinson if ((!xdr_callmsg(xdrs, &msg)) || 309*61961e0fSrobinson (!xdr_rmtcallargs(xdrs, &barg_pmap))) { 3107c478bd9Sstevel@tonic-gate stat = RPC_CANTENCODEARGS; 3117c478bd9Sstevel@tonic-gate goto done_broad; 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate t_udata_pmap.opt.len = 0; 3147c478bd9Sstevel@tonic-gate t_udata_pmap.udata.buf = outbuf_pmap; 3157c478bd9Sstevel@tonic-gate t_udata_pmap.udata.len = xdr_getpos(xdrs); 3167c478bd9Sstevel@tonic-gate xdr_destroy(xdrs); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate #endif /* PORTMAP */ 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate /* 3217c478bd9Sstevel@tonic-gate * Basic loop: broadcast the packets to transports which 3227c478bd9Sstevel@tonic-gate * support data packets of size such that one can encode 3237c478bd9Sstevel@tonic-gate * all the arguments. 3247c478bd9Sstevel@tonic-gate * Wait a while for response(s). 3257c478bd9Sstevel@tonic-gate * The response timeout grows larger per iteration. 3267c478bd9Sstevel@tonic-gate */ 3277c478bd9Sstevel@tonic-gate hs.h_host = HOST_BROADCAST; 3287c478bd9Sstevel@tonic-gate hs.h_serv = "rpcbind"; 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate for (msec = inittime; msec <= waittime; msec += msec) { 3317c478bd9Sstevel@tonic-gate /* Broadcast all the packets now */ 3327c478bd9Sstevel@tonic-gate for (i = 0; i < fdlistno; i++) { 3337c478bd9Sstevel@tonic-gate if (strcmp(fdlist[i].nconf->nc_protofmly, 3347c478bd9Sstevel@tonic-gate NC_INET6) == 0) { 3357c478bd9Sstevel@tonic-gate /* if it's IPv6 */ 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate struct netbuf addr; 3387c478bd9Sstevel@tonic-gate struct sockaddr_in6 sa6; 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* fill in the multicast address */ 3417c478bd9Sstevel@tonic-gate bzero((char *)&sa6, sizeof (sa6)); 3427c478bd9Sstevel@tonic-gate sa6.sin6_family = AF_INET6; 3437c478bd9Sstevel@tonic-gate sa6.sin6_port = htons(PMAPPORT); 3447c478bd9Sstevel@tonic-gate (void) inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, 3457c478bd9Sstevel@tonic-gate &sa6.sin6_addr); 3467c478bd9Sstevel@tonic-gate addr.maxlen = sizeof (struct sockaddr_in6); 3477c478bd9Sstevel@tonic-gate addr.len = addr.maxlen; 3487c478bd9Sstevel@tonic-gate addr.buf = (char *)&sa6; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* now send rpcbind message */ 3517c478bd9Sstevel@tonic-gate t_udata.addr = addr; 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd, 3557c478bd9Sstevel@tonic-gate &t_udata)) { 3567c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, 3577c478bd9Sstevel@tonic-gate "Cannot send broadcast\ 3587c478bd9Sstevel@tonic-gate packet: %m"); 359*61961e0fSrobinson #ifdef RPC_DEBUG 3607c478bd9Sstevel@tonic-gate t_error("rpc_broadcast: t_sndudata"); 3617c478bd9Sstevel@tonic-gate #endif 3627c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 3637c478bd9Sstevel@tonic-gate continue; 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate } else { 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate struct nd_addrlist *addrlist; 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate if (fdlist[i].dsize < t_udata.udata.len) { 3717c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 3727c478bd9Sstevel@tonic-gate continue; 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate if (netdir_getbyname(fdlist[i].nconf, &hs, 3757c478bd9Sstevel@tonic-gate &addrlist) || (addrlist->n_cnt == 0)) { 3767c478bd9Sstevel@tonic-gate stat = RPC_N2AXLATEFAILURE; 3777c478bd9Sstevel@tonic-gate continue; 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate for (j = 0; j < addrlist->n_cnt; j++) { 381*61961e0fSrobinson #ifdef RPC_DEBUG 3827c478bd9Sstevel@tonic-gate struct netconfig *nconf = 3837c478bd9Sstevel@tonic-gate fdlist[i].nconf; 384*61961e0fSrobinson #endif 3857c478bd9Sstevel@tonic-gate 3867c478bd9Sstevel@tonic-gate t_udata.addr = addrlist->n_addrs[j]; 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate /* 3897c478bd9Sstevel@tonic-gate * Only use version 3 if lowvers 3907c478bd9Sstevel@tonic-gate * is not set or transport is not UDP. 3917c478bd9Sstevel@tonic-gate */ 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate if (!lowvers || !fdlist[i].udp_flag) 3947c478bd9Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd, 3957c478bd9Sstevel@tonic-gate &t_udata)) { 3967c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, 3977c478bd9Sstevel@tonic-gate "Cannot send broadcast\ 3987c478bd9Sstevel@tonic-gate packet: %m"); 399*61961e0fSrobinson #ifdef RPC_DEBUG 4007c478bd9Sstevel@tonic-gate t_error("rpc_broadcast: t_sndudata"); 4017c478bd9Sstevel@tonic-gate #endif 4027c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 4037c478bd9Sstevel@tonic-gate continue; 4047c478bd9Sstevel@tonic-gate }; 405*61961e0fSrobinson #ifdef RPC_DEBUG 4067c478bd9Sstevel@tonic-gate if (!lowvers || !fdlist[i].udp_flag) 4077c478bd9Sstevel@tonic-gate fprintf(stderr, "Broadcast\ 4087c478bd9Sstevel@tonic-gate packet sent for %s\n", nconf->nc_netid); 4097c478bd9Sstevel@tonic-gate #endif 4107c478bd9Sstevel@tonic-gate #ifdef PORTMAP 4117c478bd9Sstevel@tonic-gate /* 4127c478bd9Sstevel@tonic-gate * Send the version 2 packet also 4137c478bd9Sstevel@tonic-gate * for UDP/IP 4147c478bd9Sstevel@tonic-gate */ 4157c478bd9Sstevel@tonic-gate if (fdlist[i].udp_flag) { 4167c478bd9Sstevel@tonic-gate t_udata_pmap.addr = 4177c478bd9Sstevel@tonic-gate t_udata.addr; 4187c478bd9Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd, 4197c478bd9Sstevel@tonic-gate &t_udata_pmap)) { 4207c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR,\ 4217c478bd9Sstevel@tonic-gate "Cannot send broadcast packet: %m"); 4227c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 4237c478bd9Sstevel@tonic-gate t_error("rpc_broadcast:\ 4247c478bd9Sstevel@tonic-gate t_sndudata"); 4257c478bd9Sstevel@tonic-gate #endif 4267c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 4277c478bd9Sstevel@tonic-gate continue; 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 4317c478bd9Sstevel@tonic-gate fprintf(stderr, "PMAP Broadcast packet\ 4327c478bd9Sstevel@tonic-gate sent for %s\n", nconf->nc_netid); 4337c478bd9Sstevel@tonic-gate #endif 4347c478bd9Sstevel@tonic-gate #endif /* PORTMAP */ 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate /* End for sending all packets on this transport */ 4377c478bd9Sstevel@tonic-gate (void) netdir_free((char *)addrlist, ND_ADDRLIST); 4387c478bd9Sstevel@tonic-gate } /* end non-IPv6 */ 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate } /* End for sending on all transports */ 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate if (eachresult == NULL) { 4437c478bd9Sstevel@tonic-gate stat = RPC_SUCCESS; 4447c478bd9Sstevel@tonic-gate goto done_broad; 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate /* 4487c478bd9Sstevel@tonic-gate * Get all the replies from these broadcast requests 4497c478bd9Sstevel@tonic-gate */ 4507c478bd9Sstevel@tonic-gate recv_again: 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate switch (pollretval = poll(pfd, fdlistno, msec)) { 4537c478bd9Sstevel@tonic-gate case 0: /* timed out */ 4547c478bd9Sstevel@tonic-gate stat = RPC_TIMEDOUT; 4557c478bd9Sstevel@tonic-gate continue; 4567c478bd9Sstevel@tonic-gate case -1: /* some kind of error - we ignore it */ 4577c478bd9Sstevel@tonic-gate goto recv_again; 4587c478bd9Sstevel@tonic-gate } /* end of poll results switch */ 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate t_rdata.udata.buf = inbuf; 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate for (i = fds_found = 0; 4637c478bd9Sstevel@tonic-gate i < fdlistno && fds_found < pollretval; i++) { 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate int flag; 4667c478bd9Sstevel@tonic-gate bool_t done = FALSE; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate if (pfd[i].revents == 0) 4697c478bd9Sstevel@tonic-gate continue; 4707c478bd9Sstevel@tonic-gate else if (pfd[i].revents & POLLNVAL) { 4717c478bd9Sstevel@tonic-gate /* 4727c478bd9Sstevel@tonic-gate * Something bad has happened to this descri- 4737c478bd9Sstevel@tonic-gate * ptor. We can cause poll() to ignore 4747c478bd9Sstevel@tonic-gate * it simply by using a negative fd. We do that 4757c478bd9Sstevel@tonic-gate * rather than compacting the pfd[] and fdlist[] 4767c478bd9Sstevel@tonic-gate * arrays. 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate pfd[i].fd = -1; 4797c478bd9Sstevel@tonic-gate fds_found++; 4807c478bd9Sstevel@tonic-gate continue; 4817c478bd9Sstevel@tonic-gate } else 4827c478bd9Sstevel@tonic-gate fds_found++; 4837c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 4847c478bd9Sstevel@tonic-gate fprintf(stderr, "response for %s\n", 4857c478bd9Sstevel@tonic-gate fdlist[i].nconf->nc_netid); 4867c478bd9Sstevel@tonic-gate #endif 4877c478bd9Sstevel@tonic-gate try_again: 4887c478bd9Sstevel@tonic-gate t_rdata.udata.maxlen = fdlist[i].dsize; 4897c478bd9Sstevel@tonic-gate t_rdata.udata.len = 0; 4907c478bd9Sstevel@tonic-gate t_rdata.addr = fdlist[i].raddr; 4917c478bd9Sstevel@tonic-gate if (t_rcvudata(fdlist[i].fd, &t_rdata, &flag) == -1) { 4927c478bd9Sstevel@tonic-gate if (t_errno == TSYSERR && errno == EINTR) 4937c478bd9Sstevel@tonic-gate goto try_again; 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate /* 4967c478bd9Sstevel@tonic-gate * Ignore any T_UDERR look errors. 4977c478bd9Sstevel@tonic-gate * We should never see any ICMP port 4987c478bd9Sstevel@tonic-gate * unreachables when broadcasting but it has 4997c478bd9Sstevel@tonic-gate * been observed with broken IP 5007c478bd9Sstevel@tonic-gate * implementations. 5017c478bd9Sstevel@tonic-gate */ 5027c478bd9Sstevel@tonic-gate if (t_errno == TLOOK && 5037c478bd9Sstevel@tonic-gate t_look(fdlist[i].fd) == T_UDERR && 5047c478bd9Sstevel@tonic-gate t_rcvuderr(fdlist[i].fd, NULL) == 0) 5057c478bd9Sstevel@tonic-gate goto recv_again; 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, 5087c478bd9Sstevel@tonic-gate "Cannot receive reply to \ 5097c478bd9Sstevel@tonic-gate broadcast: %m"); 5107c478bd9Sstevel@tonic-gate stat = RPC_CANTRECV; 5117c478bd9Sstevel@tonic-gate continue; 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate /* 5147c478bd9Sstevel@tonic-gate * Not taking care of flag for T_MORE. 5157c478bd9Sstevel@tonic-gate * We are assuming that 5167c478bd9Sstevel@tonic-gate * such calls should not take more than one 5177c478bd9Sstevel@tonic-gate * transport packet. 5187c478bd9Sstevel@tonic-gate */ 5197c478bd9Sstevel@tonic-gate if (flag & T_MORE) 5207c478bd9Sstevel@tonic-gate continue; /* Drop that and go ahead */ 5217c478bd9Sstevel@tonic-gate if (t_rdata.udata.len < (uint_t)sizeof (uint32_t)) 5227c478bd9Sstevel@tonic-gate continue; /* Drop that and go ahead */ 5237c478bd9Sstevel@tonic-gate /* 5247c478bd9Sstevel@tonic-gate * see if reply transaction id matches sent id. 5257c478bd9Sstevel@tonic-gate * If so, decode the results. If return id is xid + 1 5267c478bd9Sstevel@tonic-gate * it was a PORTMAP reply 5277c478bd9Sstevel@tonic-gate */ 528*61961e0fSrobinson /* LINTED pointer cast */ 5297c478bd9Sstevel@tonic-gate if (*((uint32_t *)(inbuf)) == *((uint32_t *)(outbuf))) { 5307c478bd9Sstevel@tonic-gate pmap_reply_flag = 0; 5317c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_verf = _null_auth; 5327c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.where = 5337c478bd9Sstevel@tonic-gate (caddr_t)&bres; 5347c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.proc = 5357c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_rpcb_rmtcallres; 5367c478bd9Sstevel@tonic-gate #ifdef PORTMAP 5377c478bd9Sstevel@tonic-gate } else if (pmap_flag && 538*61961e0fSrobinson /* LINTED pointer cast */ 5397c478bd9Sstevel@tonic-gate *((uint32_t *)(inbuf)) == 540*61961e0fSrobinson /* LINTED pointer cast */ 541*61961e0fSrobinson *((uint32_t *)(outbuf_pmap))) { 5427c478bd9Sstevel@tonic-gate pmap_reply_flag = 1; 5437c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_verf = _null_auth; 5447c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.where = 5457c478bd9Sstevel@tonic-gate (caddr_t)&bres_pmap; 5467c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.proc = 5477c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_rmtcallres; 5487c478bd9Sstevel@tonic-gate #endif /* PORTMAP */ 5497c478bd9Sstevel@tonic-gate } else 5507c478bd9Sstevel@tonic-gate continue; 5517c478bd9Sstevel@tonic-gate xdrmem_create(xdrs, inbuf, 5527c478bd9Sstevel@tonic-gate (uint_t)t_rdata.udata.len, XDR_DECODE); 5537c478bd9Sstevel@tonic-gate if (xdr_replymsg(xdrs, &msg)) { 5547c478bd9Sstevel@tonic-gate if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) && 5557c478bd9Sstevel@tonic-gate (msg.acpted_rply.ar_stat == SUCCESS)) { 5567c478bd9Sstevel@tonic-gate struct netbuf *taddr; 5577c478bd9Sstevel@tonic-gate #ifdef PORTMAP 5587c478bd9Sstevel@tonic-gate if (pmap_flag && pmap_reply_flag) { 5597c478bd9Sstevel@tonic-gate /* convert port to taddr */ 560*61961e0fSrobinson /* LINTED pointer cast */ 5617c478bd9Sstevel@tonic-gate ((struct sockaddr_in *) 5627c478bd9Sstevel@tonic-gate t_rdata.addr.buf)->sin_port = 5637c478bd9Sstevel@tonic-gate htons((ushort_t)*port); 5647c478bd9Sstevel@tonic-gate taddr = &t_rdata.addr; 5657c478bd9Sstevel@tonic-gate } else /* Convert the uaddr to taddr */ 5667c478bd9Sstevel@tonic-gate #endif 5677c478bd9Sstevel@tonic-gate taddr = uaddr2taddr( 5687c478bd9Sstevel@tonic-gate fdlist[i].nconf, 5697c478bd9Sstevel@tonic-gate uaddrp); 5707c478bd9Sstevel@tonic-gate done = (*eachresult)(resultsp, taddr, 5717c478bd9Sstevel@tonic-gate fdlist[i].nconf); 5727c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 5737c478bd9Sstevel@tonic-gate { 5747c478bd9Sstevel@tonic-gate int k; 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate printf("rmt addr = "); 5777c478bd9Sstevel@tonic-gate for (k = 0; k < taddr->len; k++) 5787c478bd9Sstevel@tonic-gate printf("%d ", taddr->buf[k]); 5797c478bd9Sstevel@tonic-gate printf("\n"); 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate #endif 5827c478bd9Sstevel@tonic-gate if (taddr && !pmap_reply_flag) 5837c478bd9Sstevel@tonic-gate netdir_free((char *)taddr, 5847c478bd9Sstevel@tonic-gate ND_ADDR); 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate /* otherwise, we just ignore the errors ... */ 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate /* else some kind of deserialization problem ... */ 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate xdrs->x_op = XDR_FREE; 5917c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; 5927c478bd9Sstevel@tonic-gate (void) xdr_replymsg(xdrs, &msg); 5937c478bd9Sstevel@tonic-gate (void) (*xresults)(xdrs, resultsp); 5947c478bd9Sstevel@tonic-gate XDR_DESTROY(xdrs); 5957c478bd9Sstevel@tonic-gate if (done) { 5967c478bd9Sstevel@tonic-gate stat = RPC_SUCCESS; 5977c478bd9Sstevel@tonic-gate goto done_broad; 5987c478bd9Sstevel@tonic-gate } else { 5997c478bd9Sstevel@tonic-gate if (rpc_callerr.re_status == RPC_SYSTEMERROR) { 6007c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; 6017c478bd9Sstevel@tonic-gate goto done_broad; 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate goto recv_again; 6047c478bd9Sstevel@tonic-gate } 6057c478bd9Sstevel@tonic-gate } /* The recv for loop */ 6067c478bd9Sstevel@tonic-gate } /* The giant for loop */ 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate done_broad: 6097c478bd9Sstevel@tonic-gate if (inbuf) 610*61961e0fSrobinson free(inbuf); 6117c478bd9Sstevel@tonic-gate if (outbuf) 612*61961e0fSrobinson free(outbuf); 6137c478bd9Sstevel@tonic-gate #ifdef PORTMAP 6147c478bd9Sstevel@tonic-gate if (outbuf_pmap) 615*61961e0fSrobinson free(outbuf_pmap); 6167c478bd9Sstevel@tonic-gate #endif 6177c478bd9Sstevel@tonic-gate for (i = 0; i < fdlistno; i++) { 6187c478bd9Sstevel@tonic-gate (void) t_close(fdlist[i].fd); 619*61961e0fSrobinson free(fdlist[i].raddr.buf); 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate AUTH_DESTROY(sys_auth); 6227c478bd9Sstevel@tonic-gate (void) __rpc_endconf(handle); 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate return (stat); 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate enum clnt_stat 628*61961e0fSrobinson rpc_broadcast(const rpcprog_t prog, const rpcvers_t vers, const rpcproc_t proc, 629*61961e0fSrobinson const xdrproc_t xargs, caddr_t argsp, xdrproc_t const xresults, 630*61961e0fSrobinson caddr_t resultsp, const resultproc_t eachresult, const char *nettype) 6317c478bd9Sstevel@tonic-gate { 632*61961e0fSrobinson return (rpc_broadcast_exp(prog, vers, proc, xargs, argsp, 6337c478bd9Sstevel@tonic-gate xresults, resultsp, eachresult, 634*61961e0fSrobinson INITTIME, WAITTIME, nettype)); 6357c478bd9Sstevel@tonic-gate } 636