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 5*45916cd2Sjpk * Common Development and Distribution License (the "License"). 6*45916cd2Sjpk * 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 2061961e0fSrobinson */ 2161961e0fSrobinson 2261961e0fSrobinson /* 23e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 26e8031f0aSraf 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 * Client interface to broadcast service. 397c478bd9Sstevel@tonic-gate * 407c478bd9Sstevel@tonic-gate * The following is kludged-up support for simple rpc broadcasts. 417c478bd9Sstevel@tonic-gate * Someday a large, complicated system will replace these routines. 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 44e8031f0aSraf #include "mt.h" 45*45916cd2Sjpk #include "rpc_mt.h" 467c478bd9Sstevel@tonic-gate #include <string.h> 4761961e0fSrobinson #include <strings.h> 487c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 497c478bd9Sstevel@tonic-gate #include <rpc/nettype.h> 507c478bd9Sstevel@tonic-gate #include <sys/poll.h> 517c478bd9Sstevel@tonic-gate #include <netdir.h> 527c478bd9Sstevel@tonic-gate #ifdef PORTMAP 537c478bd9Sstevel@tonic-gate #include <rpc/pmap_prot.h> 547c478bd9Sstevel@tonic-gate #endif 557c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 567c478bd9Sstevel@tonic-gate #include <stdio.h> 577c478bd9Sstevel@tonic-gate #endif 587c478bd9Sstevel@tonic-gate #include <errno.h> 597c478bd9Sstevel@tonic-gate #include <syslog.h> 607c478bd9Sstevel@tonic-gate #include <stdlib.h> 617c478bd9Sstevel@tonic-gate #include <unistd.h> 6261961e0fSrobinson #include <sys/types.h> 6361961e0fSrobinson #include <sys/socket.h> 6461961e0fSrobinson #include <netinet/in.h> 6561961e0fSrobinson #include <arpa/inet.h> 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate #define MAXBCAST 20 /* Max no of broadcasting transports */ 687c478bd9Sstevel@tonic-gate #define INITTIME 4000 /* Time to wait initially */ 697c478bd9Sstevel@tonic-gate #define WAITTIME 8000 /* Maximum time to wait */ 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate int lowvers = 1; /* by default, broadcast only version 2 over UDP */ 727c478bd9Sstevel@tonic-gate #ifndef NETIDLEN 737c478bd9Sstevel@tonic-gate #define NETIDLEN 32 747c478bd9Sstevel@tonic-gate #endif 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * If nettype is NULL, it broadcasts on all the available 787c478bd9Sstevel@tonic-gate * datagram_n transports. May potentially lead to broadacst storms 797c478bd9Sstevel@tonic-gate * and hence should be used with caution, care and courage. 807c478bd9Sstevel@tonic-gate * 817c478bd9Sstevel@tonic-gate * The current parameter xdr packet size is limited by the max tsdu 827c478bd9Sstevel@tonic-gate * size of the transport. If the max tsdu size of any transport is 837c478bd9Sstevel@tonic-gate * smaller than the parameter xdr packet, then broadcast is not 847c478bd9Sstevel@tonic-gate * sent on that transport. 857c478bd9Sstevel@tonic-gate * 867c478bd9Sstevel@tonic-gate * Also, the packet size should be less the packet size of 877c478bd9Sstevel@tonic-gate * the data link layer (for ethernet it is 1400 bytes). There is 887c478bd9Sstevel@tonic-gate * no easy way to find out the max size of the data link layer and 897c478bd9Sstevel@tonic-gate * we are assuming that the args would be smaller than that. 907c478bd9Sstevel@tonic-gate * 917c478bd9Sstevel@tonic-gate * The result size has to be smaller than the transport tsdu size. 927c478bd9Sstevel@tonic-gate * 937c478bd9Sstevel@tonic-gate * If PORTMAP has been defined, we send two packets for UDP, one for 947c478bd9Sstevel@tonic-gate * rpcbind and one for portmap. For those machines which support 957c478bd9Sstevel@tonic-gate * both rpcbind and portmap, it will cause them to reply twice, and 967c478bd9Sstevel@tonic-gate * also here it will get two responses ... inefficient and clumsy. 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate enum clnt_stat 10161961e0fSrobinson rpc_broadcast_exp(const rpcprog_t prog, const rpcvers_t vers, 10261961e0fSrobinson const rpcproc_t proc, const xdrproc_t xargs, caddr_t argsp, 10361961e0fSrobinson const xdrproc_t xresults, caddr_t resultsp, const resultproc_t eachresult, 10461961e0fSrobinson const int inittime, const int waittime, const char *netclass) 1057c478bd9Sstevel@tonic-gate { 1067c478bd9Sstevel@tonic-gate enum clnt_stat stat = RPC_SUCCESS; /* Return status */ 1077c478bd9Sstevel@tonic-gate XDR xdr_stream; /* XDR stream */ 10861961e0fSrobinson XDR *xdrs = &xdr_stream; 1097c478bd9Sstevel@tonic-gate struct rpc_msg msg; /* RPC message */ 1107c478bd9Sstevel@tonic-gate struct timeval t; 1117c478bd9Sstevel@tonic-gate char *outbuf = NULL; /* Broadcast msg buffer */ 1127c478bd9Sstevel@tonic-gate char *inbuf = NULL; /* Reply buf */ 1137c478bd9Sstevel@tonic-gate uint_t maxbufsize = 0; 1147c478bd9Sstevel@tonic-gate AUTH *sys_auth = authsys_create_default(); 11561961e0fSrobinson int i, j; 1167c478bd9Sstevel@tonic-gate void *handle; 1177c478bd9Sstevel@tonic-gate char uaddress[1024]; /* A self imposed limit */ 1187c478bd9Sstevel@tonic-gate char *uaddrp = uaddress; 1197c478bd9Sstevel@tonic-gate int pmap_reply_flag; /* reply recvd from PORTMAP */ 1207c478bd9Sstevel@tonic-gate /* An array of all the suitable broadcast transports */ 1217c478bd9Sstevel@tonic-gate struct { 1227c478bd9Sstevel@tonic-gate int fd; /* File descriptor */ 1237c478bd9Sstevel@tonic-gate bool_t udp_flag; /* this is udp */ 1247c478bd9Sstevel@tonic-gate struct netconfig *nconf; /* Netconfig structure */ 1257c478bd9Sstevel@tonic-gate uint_t asize; /* Size of the addr buf */ 1267c478bd9Sstevel@tonic-gate uint_t dsize; /* Size of the data buf */ 1277c478bd9Sstevel@tonic-gate struct netbuf raddr; /* Remote address */ 1287c478bd9Sstevel@tonic-gate struct nd_addrlist *nal; /* Broadcast addrs */ 1297c478bd9Sstevel@tonic-gate } fdlist[MAXBCAST]; 1307c478bd9Sstevel@tonic-gate struct pollfd pfd[MAXBCAST]; 13161961e0fSrobinson int fdlistno = 0; 1327c478bd9Sstevel@tonic-gate struct r_rpcb_rmtcallargs barg; /* Remote arguments */ 1337c478bd9Sstevel@tonic-gate struct r_rpcb_rmtcallres bres; /* Remote results */ 1347c478bd9Sstevel@tonic-gate struct t_unitdata t_udata, t_rdata; 1357c478bd9Sstevel@tonic-gate struct netconfig *nconf; 1367c478bd9Sstevel@tonic-gate struct nd_hostserv hs; 1377c478bd9Sstevel@tonic-gate int msec; 1387c478bd9Sstevel@tonic-gate int pollretval; 1397c478bd9Sstevel@tonic-gate int fds_found; 1407c478bd9Sstevel@tonic-gate char nettype_array[NETIDLEN]; 1417c478bd9Sstevel@tonic-gate char *nettype = &nettype_array[0]; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate #ifdef PORTMAP 1447c478bd9Sstevel@tonic-gate rpcport_t *port; /* Remote port number */ 1457c478bd9Sstevel@tonic-gate int pmap_flag = 0; /* UDP exists ? */ 1467c478bd9Sstevel@tonic-gate char *outbuf_pmap = NULL; 1477c478bd9Sstevel@tonic-gate struct p_rmtcallargs barg_pmap; /* Remote arguments */ 1487c478bd9Sstevel@tonic-gate struct p_rmtcallres bres_pmap; /* Remote results */ 1497c478bd9Sstevel@tonic-gate struct t_unitdata t_udata_pmap; 1507c478bd9Sstevel@tonic-gate int udpbufsz = 0; 1517c478bd9Sstevel@tonic-gate #endif /* PORTMAP */ 1527c478bd9Sstevel@tonic-gate 15361961e0fSrobinson if (sys_auth == NULL) 1547c478bd9Sstevel@tonic-gate return (RPC_SYSTEMERROR); 1557c478bd9Sstevel@tonic-gate /* 1567c478bd9Sstevel@tonic-gate * initialization: create a fd, a broadcast address, and send the 1577c478bd9Sstevel@tonic-gate * request on the broadcast transport. 1587c478bd9Sstevel@tonic-gate * Listen on all of them and on replies, call the user supplied 1597c478bd9Sstevel@tonic-gate * function. 1607c478bd9Sstevel@tonic-gate */ 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate if (netclass == NULL) 1637c478bd9Sstevel@tonic-gate nettype = NULL; 1647c478bd9Sstevel@tonic-gate else { 1657c478bd9Sstevel@tonic-gate size_t len = strlen(netclass); 16661961e0fSrobinson if (len >= sizeof (nettype_array)) 1677c478bd9Sstevel@tonic-gate return (RPC_UNKNOWNPROTO); 16861961e0fSrobinson (void) strcpy(nettype, netclass); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if (nettype == NULL) 1727c478bd9Sstevel@tonic-gate nettype = "datagram_n"; 17361961e0fSrobinson if ((handle = __rpc_setconf((char *)nettype)) == NULL) 1747c478bd9Sstevel@tonic-gate return (RPC_UNKNOWNPROTO); 1757c478bd9Sstevel@tonic-gate while (nconf = __rpc_getconf(handle)) { 1767c478bd9Sstevel@tonic-gate struct t_info tinfo; 1777c478bd9Sstevel@tonic-gate int fd; 1787c478bd9Sstevel@tonic-gate uint_t addrlen; 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate if (nconf->nc_semantics != NC_TPI_CLTS) 1817c478bd9Sstevel@tonic-gate continue; 1827c478bd9Sstevel@tonic-gate if (fdlistno >= MAXBCAST) 1837c478bd9Sstevel@tonic-gate break; /* No more slots available */ 1847c478bd9Sstevel@tonic-gate if ((fd = t_open(nconf->nc_device, O_RDWR, &tinfo)) == -1) { 1857c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 1867c478bd9Sstevel@tonic-gate continue; 1877c478bd9Sstevel@tonic-gate } 188*45916cd2Sjpk __rpc_set_mac_options(fd, nconf, prog); 18961961e0fSrobinson 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 } 194*45916cd2Sjpk 1957c478bd9Sstevel@tonic-gate /* Do protocol specific negotiating for broadcast */ 1967c478bd9Sstevel@tonic-gate if (netdir_options(nconf, ND_SET_BROADCAST, fd, NULL)) { 1977c478bd9Sstevel@tonic-gate (void) t_close(fd); 1987c478bd9Sstevel@tonic-gate stat = RPC_NOBROADCAST; 1997c478bd9Sstevel@tonic-gate continue; 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate fdlist[fdlistno].fd = fd; 2027c478bd9Sstevel@tonic-gate fdlist[fdlistno].nconf = nconf; 2037c478bd9Sstevel@tonic-gate fdlist[fdlistno].udp_flag = FALSE; 2047c478bd9Sstevel@tonic-gate if (((addrlen = __rpc_get_a_size(tinfo.addr)) == 0) || 2057c478bd9Sstevel@tonic-gate ((fdlist[fdlistno].raddr.buf = malloc(addrlen)) == NULL)) { 20661961e0fSrobinson (void) t_close(fd); 2077c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; 2087c478bd9Sstevel@tonic-gate goto done_broad; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate fdlist[fdlistno].raddr.maxlen = addrlen; 2117c478bd9Sstevel@tonic-gate fdlist[fdlistno].raddr.len = addrlen; 2127c478bd9Sstevel@tonic-gate pfd[fdlistno].events = POLLIN | POLLPRI | 2137c478bd9Sstevel@tonic-gate POLLRDNORM | POLLRDBAND; 2147c478bd9Sstevel@tonic-gate pfd[fdlistno].fd = fdlist[fdlistno].fd = fd; 2157c478bd9Sstevel@tonic-gate fdlist[fdlistno].asize = addrlen; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if ((fdlist[fdlistno].dsize = __rpc_get_t_size(0, 2187c478bd9Sstevel@tonic-gate tinfo.tsdu)) == 0) { 21961961e0fSrobinson (void) t_close(fd); 2207c478bd9Sstevel@tonic-gate free(fdlist[fdlistno].raddr.buf); 2217c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; /* XXX */ 2227c478bd9Sstevel@tonic-gate goto done_broad; 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate if (maxbufsize <= fdlist[fdlistno].dsize) 2267c478bd9Sstevel@tonic-gate maxbufsize = fdlist[fdlistno].dsize; 2277c478bd9Sstevel@tonic-gate #ifdef PORTMAP 2287c478bd9Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET) == 0 && 2297c478bd9Sstevel@tonic-gate strcmp(nconf->nc_proto, NC_UDP) == 0) { 2307c478bd9Sstevel@tonic-gate udpbufsz = fdlist[fdlistno].dsize; 2317c478bd9Sstevel@tonic-gate if ((outbuf_pmap = malloc(udpbufsz)) == NULL) { 23261961e0fSrobinson (void) t_close(fd); 2337c478bd9Sstevel@tonic-gate free(fdlist[fdlistno].raddr.buf); 2347c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; 2357c478bd9Sstevel@tonic-gate goto done_broad; 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate pmap_flag = 1; 2387c478bd9Sstevel@tonic-gate fdlist[fdlistno].udp_flag = TRUE; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate #endif 2417c478bd9Sstevel@tonic-gate fdlistno++; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (fdlistno == 0) { 2457c478bd9Sstevel@tonic-gate if (stat == RPC_SUCCESS) 2467c478bd9Sstevel@tonic-gate stat = RPC_UNKNOWNPROTO; 2477c478bd9Sstevel@tonic-gate goto done_broad; 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate if (maxbufsize == 0) { 2507c478bd9Sstevel@tonic-gate if (stat == RPC_SUCCESS) 2517c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 2527c478bd9Sstevel@tonic-gate goto done_broad; 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate inbuf = malloc((size_t)maxbufsize); 2557c478bd9Sstevel@tonic-gate outbuf = malloc((size_t)maxbufsize); 2567c478bd9Sstevel@tonic-gate if ((inbuf == NULL) || (outbuf == NULL)) { 2577c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; 2587c478bd9Sstevel@tonic-gate goto done_broad; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate /* Serialize all the arguments which have to be sent */ 2627c478bd9Sstevel@tonic-gate (void) gettimeofday(&t, (struct timezone *)0); 2637c478bd9Sstevel@tonic-gate msg.rm_xid = getpid() ^ t.tv_sec ^ t.tv_usec; 2647c478bd9Sstevel@tonic-gate msg.rm_direction = CALL; 2657c478bd9Sstevel@tonic-gate msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 2667c478bd9Sstevel@tonic-gate msg.rm_call.cb_prog = RPCBPROG; 2677c478bd9Sstevel@tonic-gate msg.rm_call.cb_vers = RPCBVERS; 2687c478bd9Sstevel@tonic-gate msg.rm_call.cb_proc = RPCBPROC_CALLIT; 2697c478bd9Sstevel@tonic-gate barg.prog = prog; 2707c478bd9Sstevel@tonic-gate barg.vers = vers; 2717c478bd9Sstevel@tonic-gate barg.proc = proc; 2727c478bd9Sstevel@tonic-gate barg.args.args_val = argsp; 2737c478bd9Sstevel@tonic-gate barg.xdr_args = xargs; 2747c478bd9Sstevel@tonic-gate bres.addr = uaddrp; 2757c478bd9Sstevel@tonic-gate bres.results.results_val = resultsp; 2767c478bd9Sstevel@tonic-gate bres.xdr_res = xresults; 2777c478bd9Sstevel@tonic-gate msg.rm_call.cb_cred = sys_auth->ah_cred; 2787c478bd9Sstevel@tonic-gate msg.rm_call.cb_verf = sys_auth->ah_verf; 2797c478bd9Sstevel@tonic-gate xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE); 2807c478bd9Sstevel@tonic-gate if ((!xdr_callmsg(xdrs, &msg)) || 2817c478bd9Sstevel@tonic-gate (!xdr_rpcb_rmtcallargs(xdrs, &barg))) { 2827c478bd9Sstevel@tonic-gate stat = RPC_CANTENCODEARGS; 2837c478bd9Sstevel@tonic-gate goto done_broad; 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate t_udata.opt.len = 0; 2867c478bd9Sstevel@tonic-gate t_udata.udata.buf = outbuf; 2877c478bd9Sstevel@tonic-gate t_udata.udata.len = xdr_getpos(xdrs); 2887c478bd9Sstevel@tonic-gate t_udata.udata.maxlen = t_udata.udata.len; 2897c478bd9Sstevel@tonic-gate /* XXX Should have set opt to its legal maxlen. */ 2907c478bd9Sstevel@tonic-gate t_rdata.opt.len = t_rdata.opt.maxlen = 0; 2917c478bd9Sstevel@tonic-gate xdr_destroy(xdrs); 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate #ifdef PORTMAP 2947c478bd9Sstevel@tonic-gate /* Prepare the packet for version 2 PORTMAP */ 2957c478bd9Sstevel@tonic-gate if (pmap_flag) { 2967c478bd9Sstevel@tonic-gate msg.rm_xid++; /* One way to distinguish */ 2977c478bd9Sstevel@tonic-gate msg.rm_call.cb_prog = PMAPPROG; 2987c478bd9Sstevel@tonic-gate msg.rm_call.cb_vers = PMAPVERS; 2997c478bd9Sstevel@tonic-gate msg.rm_call.cb_proc = PMAPPROC_CALLIT; 3007c478bd9Sstevel@tonic-gate barg_pmap.prog = prog; 3017c478bd9Sstevel@tonic-gate barg_pmap.vers = vers; 3027c478bd9Sstevel@tonic-gate barg_pmap.proc = proc; 3037c478bd9Sstevel@tonic-gate barg_pmap.args.args_val = argsp; 3047c478bd9Sstevel@tonic-gate barg_pmap.xdr_args = xargs; 3057c478bd9Sstevel@tonic-gate port = &bres_pmap.port; /* for use later on */ 3067c478bd9Sstevel@tonic-gate bres_pmap.xdr_res = xresults; 3077c478bd9Sstevel@tonic-gate bres_pmap.res.res_val = resultsp; 3087c478bd9Sstevel@tonic-gate xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE); 3097c478bd9Sstevel@tonic-gate if ((!xdr_callmsg(xdrs, &msg)) || 3107c478bd9Sstevel@tonic-gate (!xdr_rmtcallargs(xdrs, &barg_pmap))) { 3117c478bd9Sstevel@tonic-gate stat = RPC_CANTENCODEARGS; 3127c478bd9Sstevel@tonic-gate goto done_broad; 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate t_udata_pmap.opt.len = 0; 3157c478bd9Sstevel@tonic-gate t_udata_pmap.udata.buf = outbuf_pmap; 3167c478bd9Sstevel@tonic-gate t_udata_pmap.udata.len = xdr_getpos(xdrs); 3177c478bd9Sstevel@tonic-gate xdr_destroy(xdrs); 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate #endif /* PORTMAP */ 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * Basic loop: broadcast the packets to transports which 3237c478bd9Sstevel@tonic-gate * support data packets of size such that one can encode 3247c478bd9Sstevel@tonic-gate * all the arguments. 3257c478bd9Sstevel@tonic-gate * Wait a while for response(s). 3267c478bd9Sstevel@tonic-gate * The response timeout grows larger per iteration. 3277c478bd9Sstevel@tonic-gate */ 3287c478bd9Sstevel@tonic-gate hs.h_host = HOST_BROADCAST; 3297c478bd9Sstevel@tonic-gate hs.h_serv = "rpcbind"; 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate for (msec = inittime; msec <= waittime; msec += msec) { 3327c478bd9Sstevel@tonic-gate /* Broadcast all the packets now */ 3337c478bd9Sstevel@tonic-gate for (i = 0; i < fdlistno; i++) { 3347c478bd9Sstevel@tonic-gate if (strcmp(fdlist[i].nconf->nc_protofmly, 3357c478bd9Sstevel@tonic-gate NC_INET6) == 0) { 3367c478bd9Sstevel@tonic-gate /* if it's IPv6 */ 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate struct netbuf addr; 3397c478bd9Sstevel@tonic-gate struct sockaddr_in6 sa6; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate /* fill in the multicast address */ 3427c478bd9Sstevel@tonic-gate bzero((char *)&sa6, sizeof (sa6)); 3437c478bd9Sstevel@tonic-gate sa6.sin6_family = AF_INET6; 3447c478bd9Sstevel@tonic-gate sa6.sin6_port = htons(PMAPPORT); 3457c478bd9Sstevel@tonic-gate (void) inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, 3467c478bd9Sstevel@tonic-gate &sa6.sin6_addr); 3477c478bd9Sstevel@tonic-gate addr.maxlen = sizeof (struct sockaddr_in6); 3487c478bd9Sstevel@tonic-gate addr.len = addr.maxlen; 3497c478bd9Sstevel@tonic-gate addr.buf = (char *)&sa6; 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate /* now send rpcbind message */ 3527c478bd9Sstevel@tonic-gate t_udata.addr = addr; 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd, 3567c478bd9Sstevel@tonic-gate &t_udata)) { 3577c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, 3587c478bd9Sstevel@tonic-gate "Cannot send broadcast\ 3597c478bd9Sstevel@tonic-gate packet: %m"); 3607c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 3617c478bd9Sstevel@tonic-gate t_error("rpc_broadcast: t_sndudata"); 3627c478bd9Sstevel@tonic-gate #endif 3637c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 3647c478bd9Sstevel@tonic-gate continue; 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate } else { 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate struct nd_addrlist *addrlist; 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate if (fdlist[i].dsize < t_udata.udata.len) { 3727c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 3737c478bd9Sstevel@tonic-gate continue; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate if (netdir_getbyname(fdlist[i].nconf, &hs, 3767c478bd9Sstevel@tonic-gate &addrlist) || (addrlist->n_cnt == 0)) { 3777c478bd9Sstevel@tonic-gate stat = RPC_N2AXLATEFAILURE; 3787c478bd9Sstevel@tonic-gate continue; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate for (j = 0; j < addrlist->n_cnt; j++) { 38261961e0fSrobinson #ifdef RPC_DEBUG 3837c478bd9Sstevel@tonic-gate struct netconfig *nconf = 3847c478bd9Sstevel@tonic-gate fdlist[i].nconf; 38561961e0fSrobinson #endif 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate t_udata.addr = addrlist->n_addrs[j]; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate /* 3907c478bd9Sstevel@tonic-gate * Only use version 3 if lowvers 3917c478bd9Sstevel@tonic-gate * is not set or transport is not UDP. 3927c478bd9Sstevel@tonic-gate */ 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate if (!lowvers || !fdlist[i].udp_flag) 3957c478bd9Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd, 3967c478bd9Sstevel@tonic-gate &t_udata)) { 3977c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, 3987c478bd9Sstevel@tonic-gate "Cannot send broadcast\ 3997c478bd9Sstevel@tonic-gate packet: %m"); 4007c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 4017c478bd9Sstevel@tonic-gate t_error("rpc_broadcast: t_sndudata"); 4027c478bd9Sstevel@tonic-gate #endif 4037c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 4047c478bd9Sstevel@tonic-gate continue; 4057c478bd9Sstevel@tonic-gate }; 4067c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 4077c478bd9Sstevel@tonic-gate if (!lowvers || !fdlist[i].udp_flag) 4087c478bd9Sstevel@tonic-gate fprintf(stderr, "Broadcast\ 4097c478bd9Sstevel@tonic-gate packet sent for %s\n", nconf->nc_netid); 4107c478bd9Sstevel@tonic-gate #endif 4117c478bd9Sstevel@tonic-gate #ifdef PORTMAP 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * Send the version 2 packet also 4147c478bd9Sstevel@tonic-gate * for UDP/IP 4157c478bd9Sstevel@tonic-gate */ 4167c478bd9Sstevel@tonic-gate if (fdlist[i].udp_flag) { 4177c478bd9Sstevel@tonic-gate t_udata_pmap.addr = 4187c478bd9Sstevel@tonic-gate t_udata.addr; 4197c478bd9Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd, 4207c478bd9Sstevel@tonic-gate &t_udata_pmap)) { 4217c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR,\ 4227c478bd9Sstevel@tonic-gate "Cannot send broadcast packet: %m"); 4237c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 4247c478bd9Sstevel@tonic-gate t_error("rpc_broadcast:\ 4257c478bd9Sstevel@tonic-gate t_sndudata"); 4267c478bd9Sstevel@tonic-gate #endif 4277c478bd9Sstevel@tonic-gate stat = RPC_CANTSEND; 4287c478bd9Sstevel@tonic-gate continue; 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 4327c478bd9Sstevel@tonic-gate fprintf(stderr, "PMAP Broadcast packet\ 4337c478bd9Sstevel@tonic-gate sent for %s\n", nconf->nc_netid); 4347c478bd9Sstevel@tonic-gate #endif 4357c478bd9Sstevel@tonic-gate #endif /* PORTMAP */ 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate /* End for sending all packets on this transport */ 4387c478bd9Sstevel@tonic-gate (void) netdir_free((char *)addrlist, ND_ADDRLIST); 4397c478bd9Sstevel@tonic-gate } /* end non-IPv6 */ 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate } /* End for sending on all transports */ 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate if (eachresult == NULL) { 4447c478bd9Sstevel@tonic-gate stat = RPC_SUCCESS; 4457c478bd9Sstevel@tonic-gate goto done_broad; 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate /* 4497c478bd9Sstevel@tonic-gate * Get all the replies from these broadcast requests 4507c478bd9Sstevel@tonic-gate */ 4517c478bd9Sstevel@tonic-gate recv_again: 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate switch (pollretval = poll(pfd, fdlistno, msec)) { 4547c478bd9Sstevel@tonic-gate case 0: /* timed out */ 4557c478bd9Sstevel@tonic-gate stat = RPC_TIMEDOUT; 4567c478bd9Sstevel@tonic-gate continue; 4577c478bd9Sstevel@tonic-gate case -1: /* some kind of error - we ignore it */ 4587c478bd9Sstevel@tonic-gate goto recv_again; 4597c478bd9Sstevel@tonic-gate } /* end of poll results switch */ 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate t_rdata.udata.buf = inbuf; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate for (i = fds_found = 0; 4647c478bd9Sstevel@tonic-gate i < fdlistno && fds_found < pollretval; i++) { 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate int flag; 4677c478bd9Sstevel@tonic-gate bool_t done = FALSE; 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate if (pfd[i].revents == 0) 4707c478bd9Sstevel@tonic-gate continue; 4717c478bd9Sstevel@tonic-gate else if (pfd[i].revents & POLLNVAL) { 4727c478bd9Sstevel@tonic-gate /* 4737c478bd9Sstevel@tonic-gate * Something bad has happened to this descri- 4747c478bd9Sstevel@tonic-gate * ptor. We can cause poll() to ignore 4757c478bd9Sstevel@tonic-gate * it simply by using a negative fd. We do that 4767c478bd9Sstevel@tonic-gate * rather than compacting the pfd[] and fdlist[] 4777c478bd9Sstevel@tonic-gate * arrays. 4787c478bd9Sstevel@tonic-gate */ 4797c478bd9Sstevel@tonic-gate pfd[i].fd = -1; 4807c478bd9Sstevel@tonic-gate fds_found++; 4817c478bd9Sstevel@tonic-gate continue; 4827c478bd9Sstevel@tonic-gate } else 4837c478bd9Sstevel@tonic-gate fds_found++; 4847c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 4857c478bd9Sstevel@tonic-gate fprintf(stderr, "response for %s\n", 4867c478bd9Sstevel@tonic-gate fdlist[i].nconf->nc_netid); 4877c478bd9Sstevel@tonic-gate #endif 4887c478bd9Sstevel@tonic-gate try_again: 4897c478bd9Sstevel@tonic-gate t_rdata.udata.maxlen = fdlist[i].dsize; 4907c478bd9Sstevel@tonic-gate t_rdata.udata.len = 0; 4917c478bd9Sstevel@tonic-gate t_rdata.addr = fdlist[i].raddr; 4927c478bd9Sstevel@tonic-gate if (t_rcvudata(fdlist[i].fd, &t_rdata, &flag) == -1) { 4937c478bd9Sstevel@tonic-gate if (t_errno == TSYSERR && errno == EINTR) 4947c478bd9Sstevel@tonic-gate goto try_again; 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate /* 4977c478bd9Sstevel@tonic-gate * Ignore any T_UDERR look errors. 4987c478bd9Sstevel@tonic-gate * We should never see any ICMP port 4997c478bd9Sstevel@tonic-gate * unreachables when broadcasting but it has 5007c478bd9Sstevel@tonic-gate * been observed with broken IP 5017c478bd9Sstevel@tonic-gate * implementations. 5027c478bd9Sstevel@tonic-gate */ 5037c478bd9Sstevel@tonic-gate if (t_errno == TLOOK && 5047c478bd9Sstevel@tonic-gate t_look(fdlist[i].fd) == T_UDERR && 5057c478bd9Sstevel@tonic-gate t_rcvuderr(fdlist[i].fd, NULL) == 0) 5067c478bd9Sstevel@tonic-gate goto recv_again; 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, 5097c478bd9Sstevel@tonic-gate "Cannot receive reply to \ 5107c478bd9Sstevel@tonic-gate broadcast: %m"); 5117c478bd9Sstevel@tonic-gate stat = RPC_CANTRECV; 5127c478bd9Sstevel@tonic-gate continue; 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate /* 5157c478bd9Sstevel@tonic-gate * Not taking care of flag for T_MORE. 5167c478bd9Sstevel@tonic-gate * We are assuming that 5177c478bd9Sstevel@tonic-gate * such calls should not take more than one 5187c478bd9Sstevel@tonic-gate * transport packet. 5197c478bd9Sstevel@tonic-gate */ 5207c478bd9Sstevel@tonic-gate if (flag & T_MORE) 5217c478bd9Sstevel@tonic-gate continue; /* Drop that and go ahead */ 5227c478bd9Sstevel@tonic-gate if (t_rdata.udata.len < (uint_t)sizeof (uint32_t)) 5237c478bd9Sstevel@tonic-gate continue; /* Drop that and go ahead */ 5247c478bd9Sstevel@tonic-gate /* 5257c478bd9Sstevel@tonic-gate * see if reply transaction id matches sent id. 5267c478bd9Sstevel@tonic-gate * If so, decode the results. If return id is xid + 1 5277c478bd9Sstevel@tonic-gate * it was a PORTMAP reply 5287c478bd9Sstevel@tonic-gate */ 52961961e0fSrobinson /* LINTED pointer cast */ 5307c478bd9Sstevel@tonic-gate if (*((uint32_t *)(inbuf)) == *((uint32_t *)(outbuf))) { 5317c478bd9Sstevel@tonic-gate pmap_reply_flag = 0; 5327c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_verf = _null_auth; 5337c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.where = 5347c478bd9Sstevel@tonic-gate (caddr_t)&bres; 5357c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.proc = 5367c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_rpcb_rmtcallres; 5377c478bd9Sstevel@tonic-gate #ifdef PORTMAP 5387c478bd9Sstevel@tonic-gate } else if (pmap_flag && 53961961e0fSrobinson /* LINTED pointer cast */ 5407c478bd9Sstevel@tonic-gate *((uint32_t *)(inbuf)) == 54161961e0fSrobinson /* LINTED pointer cast */ 5427c478bd9Sstevel@tonic-gate *((uint32_t *)(outbuf_pmap))) { 5437c478bd9Sstevel@tonic-gate pmap_reply_flag = 1; 5447c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_verf = _null_auth; 5457c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.where = 5467c478bd9Sstevel@tonic-gate (caddr_t)&bres_pmap; 5477c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.proc = 5487c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_rmtcallres; 5497c478bd9Sstevel@tonic-gate #endif /* PORTMAP */ 5507c478bd9Sstevel@tonic-gate } else 5517c478bd9Sstevel@tonic-gate continue; 5527c478bd9Sstevel@tonic-gate xdrmem_create(xdrs, inbuf, 5537c478bd9Sstevel@tonic-gate (uint_t)t_rdata.udata.len, XDR_DECODE); 5547c478bd9Sstevel@tonic-gate if (xdr_replymsg(xdrs, &msg)) { 5557c478bd9Sstevel@tonic-gate if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) && 5567c478bd9Sstevel@tonic-gate (msg.acpted_rply.ar_stat == SUCCESS)) { 5577c478bd9Sstevel@tonic-gate struct netbuf *taddr; 5587c478bd9Sstevel@tonic-gate #ifdef PORTMAP 5597c478bd9Sstevel@tonic-gate if (pmap_flag && pmap_reply_flag) { 5607c478bd9Sstevel@tonic-gate /* convert port to taddr */ 56161961e0fSrobinson /* LINTED pointer cast */ 5627c478bd9Sstevel@tonic-gate ((struct sockaddr_in *) 5637c478bd9Sstevel@tonic-gate t_rdata.addr.buf)->sin_port = 5647c478bd9Sstevel@tonic-gate htons((ushort_t)*port); 5657c478bd9Sstevel@tonic-gate taddr = &t_rdata.addr; 5667c478bd9Sstevel@tonic-gate } else /* Convert the uaddr to taddr */ 5677c478bd9Sstevel@tonic-gate #endif 5687c478bd9Sstevel@tonic-gate taddr = uaddr2taddr( 5697c478bd9Sstevel@tonic-gate fdlist[i].nconf, 5707c478bd9Sstevel@tonic-gate uaddrp); 5717c478bd9Sstevel@tonic-gate done = (*eachresult)(resultsp, taddr, 5727c478bd9Sstevel@tonic-gate fdlist[i].nconf); 5737c478bd9Sstevel@tonic-gate #ifdef RPC_DEBUG 5747c478bd9Sstevel@tonic-gate { 5757c478bd9Sstevel@tonic-gate int k; 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate printf("rmt addr = "); 5787c478bd9Sstevel@tonic-gate for (k = 0; k < taddr->len; k++) 5797c478bd9Sstevel@tonic-gate printf("%d ", taddr->buf[k]); 5807c478bd9Sstevel@tonic-gate printf("\n"); 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate #endif 5837c478bd9Sstevel@tonic-gate if (taddr && !pmap_reply_flag) 5847c478bd9Sstevel@tonic-gate netdir_free((char *)taddr, 5857c478bd9Sstevel@tonic-gate ND_ADDR); 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate /* otherwise, we just ignore the errors ... */ 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate /* else some kind of deserialization problem ... */ 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate xdrs->x_op = XDR_FREE; 5927c478bd9Sstevel@tonic-gate msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; 5937c478bd9Sstevel@tonic-gate (void) xdr_replymsg(xdrs, &msg); 5947c478bd9Sstevel@tonic-gate (void) (*xresults)(xdrs, resultsp); 5957c478bd9Sstevel@tonic-gate XDR_DESTROY(xdrs); 5967c478bd9Sstevel@tonic-gate if (done) { 5977c478bd9Sstevel@tonic-gate stat = RPC_SUCCESS; 5987c478bd9Sstevel@tonic-gate goto done_broad; 5997c478bd9Sstevel@tonic-gate } else { 6007c478bd9Sstevel@tonic-gate if (rpc_callerr.re_status == RPC_SYSTEMERROR) { 6017c478bd9Sstevel@tonic-gate stat = RPC_SYSTEMERROR; 6027c478bd9Sstevel@tonic-gate goto done_broad; 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate goto recv_again; 6057c478bd9Sstevel@tonic-gate } 6067c478bd9Sstevel@tonic-gate } /* The recv for loop */ 6077c478bd9Sstevel@tonic-gate } /* The giant for loop */ 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate done_broad: 6107c478bd9Sstevel@tonic-gate if (inbuf) 61161961e0fSrobinson free(inbuf); 6127c478bd9Sstevel@tonic-gate if (outbuf) 61361961e0fSrobinson free(outbuf); 6147c478bd9Sstevel@tonic-gate #ifdef PORTMAP 6157c478bd9Sstevel@tonic-gate if (outbuf_pmap) 61661961e0fSrobinson free(outbuf_pmap); 6177c478bd9Sstevel@tonic-gate #endif 6187c478bd9Sstevel@tonic-gate for (i = 0; i < fdlistno; i++) { 6197c478bd9Sstevel@tonic-gate (void) t_close(fdlist[i].fd); 62061961e0fSrobinson free(fdlist[i].raddr.buf); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate AUTH_DESTROY(sys_auth); 6237c478bd9Sstevel@tonic-gate (void) __rpc_endconf(handle); 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate return (stat); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate enum clnt_stat 62961961e0fSrobinson rpc_broadcast(const rpcprog_t prog, const rpcvers_t vers, const rpcproc_t proc, 63061961e0fSrobinson const xdrproc_t xargs, caddr_t argsp, xdrproc_t const xresults, 63161961e0fSrobinson caddr_t resultsp, const resultproc_t eachresult, const char *nettype) 6327c478bd9Sstevel@tonic-gate { 63361961e0fSrobinson return (rpc_broadcast_exp(prog, vers, proc, xargs, argsp, 6347c478bd9Sstevel@tonic-gate xresults, resultsp, eachresult, 63561961e0fSrobinson INITTIME, WAITTIME, nettype)); 6367c478bd9Sstevel@tonic-gate } 637