10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1676Sjpk * Common Development and Distribution License (the "License").
6*1676Sjpk * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
20132Srobinson */
21132Srobinson
22132Srobinson /*
231219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
261219Sraf
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate * California.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * Client interface to broadcast service.
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * The following is kludged-up support for simple rpc broadcasts.
410Sstevel@tonic-gate * Someday a large, complicated system will replace these routines.
420Sstevel@tonic-gate */
430Sstevel@tonic-gate
441219Sraf #include "mt.h"
45*1676Sjpk #include "rpc_mt.h"
460Sstevel@tonic-gate #include <string.h>
47132Srobinson #include <strings.h>
480Sstevel@tonic-gate #include <rpc/rpc.h>
490Sstevel@tonic-gate #include <rpc/nettype.h>
500Sstevel@tonic-gate #include <sys/poll.h>
510Sstevel@tonic-gate #include <netdir.h>
520Sstevel@tonic-gate #ifdef PORTMAP
530Sstevel@tonic-gate #include <rpc/pmap_prot.h>
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate #ifdef RPC_DEBUG
560Sstevel@tonic-gate #include <stdio.h>
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate #include <errno.h>
590Sstevel@tonic-gate #include <syslog.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate #include <unistd.h>
62132Srobinson #include <sys/types.h>
63132Srobinson #include <sys/socket.h>
64132Srobinson #include <netinet/in.h>
65132Srobinson #include <arpa/inet.h>
660Sstevel@tonic-gate
670Sstevel@tonic-gate #define MAXBCAST 20 /* Max no of broadcasting transports */
680Sstevel@tonic-gate #define INITTIME 4000 /* Time to wait initially */
690Sstevel@tonic-gate #define WAITTIME 8000 /* Maximum time to wait */
700Sstevel@tonic-gate
710Sstevel@tonic-gate int lowvers = 1; /* by default, broadcast only version 2 over UDP */
720Sstevel@tonic-gate #ifndef NETIDLEN
730Sstevel@tonic-gate #define NETIDLEN 32
740Sstevel@tonic-gate #endif
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate * If nettype is NULL, it broadcasts on all the available
780Sstevel@tonic-gate * datagram_n transports. May potentially lead to broadacst storms
790Sstevel@tonic-gate * and hence should be used with caution, care and courage.
800Sstevel@tonic-gate *
810Sstevel@tonic-gate * The current parameter xdr packet size is limited by the max tsdu
820Sstevel@tonic-gate * size of the transport. If the max tsdu size of any transport is
830Sstevel@tonic-gate * smaller than the parameter xdr packet, then broadcast is not
840Sstevel@tonic-gate * sent on that transport.
850Sstevel@tonic-gate *
860Sstevel@tonic-gate * Also, the packet size should be less the packet size of
870Sstevel@tonic-gate * the data link layer (for ethernet it is 1400 bytes). There is
880Sstevel@tonic-gate * no easy way to find out the max size of the data link layer and
890Sstevel@tonic-gate * we are assuming that the args would be smaller than that.
900Sstevel@tonic-gate *
910Sstevel@tonic-gate * The result size has to be smaller than the transport tsdu size.
920Sstevel@tonic-gate *
930Sstevel@tonic-gate * If PORTMAP has been defined, we send two packets for UDP, one for
940Sstevel@tonic-gate * rpcbind and one for portmap. For those machines which support
950Sstevel@tonic-gate * both rpcbind and portmap, it will cause them to reply twice, and
960Sstevel@tonic-gate * also here it will get two responses ... inefficient and clumsy.
970Sstevel@tonic-gate */
980Sstevel@tonic-gate
990Sstevel@tonic-gate
1000Sstevel@tonic-gate enum clnt_stat
rpc_broadcast_exp(const rpcprog_t prog,const rpcvers_t vers,const rpcproc_t proc,const xdrproc_t xargs,caddr_t argsp,const xdrproc_t xresults,caddr_t resultsp,const resultproc_t eachresult,const int inittime,const int waittime,const char * netclass)101132Srobinson rpc_broadcast_exp(const rpcprog_t prog, const rpcvers_t vers,
102132Srobinson const rpcproc_t proc, const xdrproc_t xargs, caddr_t argsp,
103132Srobinson const xdrproc_t xresults, caddr_t resultsp, const resultproc_t eachresult,
104132Srobinson const int inittime, const int waittime, const char *netclass)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate enum clnt_stat stat = RPC_SUCCESS; /* Return status */
107132Srobinson XDR xdr_stream; /* XDR stream */
108132Srobinson XDR *xdrs = &xdr_stream;
1090Sstevel@tonic-gate struct rpc_msg msg; /* RPC message */
1100Sstevel@tonic-gate struct timeval t;
111132Srobinson char *outbuf = NULL; /* Broadcast msg buffer */
1120Sstevel@tonic-gate char *inbuf = NULL; /* Reply buf */
1130Sstevel@tonic-gate uint_t maxbufsize = 0;
114132Srobinson AUTH *sys_auth = authsys_create_default();
115132Srobinson int i, j;
1160Sstevel@tonic-gate void *handle;
1170Sstevel@tonic-gate char uaddress[1024]; /* A self imposed limit */
1180Sstevel@tonic-gate char *uaddrp = uaddress;
119132Srobinson int pmap_reply_flag; /* reply recvd from PORTMAP */
1200Sstevel@tonic-gate /* An array of all the suitable broadcast transports */
1210Sstevel@tonic-gate struct {
1220Sstevel@tonic-gate int fd; /* File descriptor */
1230Sstevel@tonic-gate bool_t udp_flag; /* this is udp */
1240Sstevel@tonic-gate struct netconfig *nconf; /* Netconfig structure */
1250Sstevel@tonic-gate uint_t asize; /* Size of the addr buf */
1260Sstevel@tonic-gate uint_t dsize; /* Size of the data buf */
1270Sstevel@tonic-gate struct netbuf raddr; /* Remote address */
1280Sstevel@tonic-gate struct nd_addrlist *nal; /* Broadcast addrs */
1290Sstevel@tonic-gate } fdlist[MAXBCAST];
1300Sstevel@tonic-gate struct pollfd pfd[MAXBCAST];
131132Srobinson int fdlistno = 0;
1320Sstevel@tonic-gate struct r_rpcb_rmtcallargs barg; /* Remote arguments */
1330Sstevel@tonic-gate struct r_rpcb_rmtcallres bres; /* Remote results */
1340Sstevel@tonic-gate struct t_unitdata t_udata, t_rdata;
1350Sstevel@tonic-gate struct netconfig *nconf;
1360Sstevel@tonic-gate struct nd_hostserv hs;
1370Sstevel@tonic-gate int msec;
1380Sstevel@tonic-gate int pollretval;
1390Sstevel@tonic-gate int fds_found;
1400Sstevel@tonic-gate char nettype_array[NETIDLEN];
1410Sstevel@tonic-gate char *nettype = &nettype_array[0];
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate #ifdef PORTMAP
1440Sstevel@tonic-gate rpcport_t *port; /* Remote port number */
1450Sstevel@tonic-gate int pmap_flag = 0; /* UDP exists ? */
1460Sstevel@tonic-gate char *outbuf_pmap = NULL;
1470Sstevel@tonic-gate struct p_rmtcallargs barg_pmap; /* Remote arguments */
1480Sstevel@tonic-gate struct p_rmtcallres bres_pmap; /* Remote results */
1490Sstevel@tonic-gate struct t_unitdata t_udata_pmap;
1500Sstevel@tonic-gate int udpbufsz = 0;
1510Sstevel@tonic-gate #endif /* PORTMAP */
1520Sstevel@tonic-gate
153132Srobinson if (sys_auth == NULL)
1540Sstevel@tonic-gate return (RPC_SYSTEMERROR);
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate * initialization: create a fd, a broadcast address, and send the
1570Sstevel@tonic-gate * request on the broadcast transport.
1580Sstevel@tonic-gate * Listen on all of them and on replies, call the user supplied
1590Sstevel@tonic-gate * function.
1600Sstevel@tonic-gate */
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (netclass == NULL)
1630Sstevel@tonic-gate nettype = NULL;
1640Sstevel@tonic-gate else {
1650Sstevel@tonic-gate size_t len = strlen(netclass);
166132Srobinson if (len >= sizeof (nettype_array))
1670Sstevel@tonic-gate return (RPC_UNKNOWNPROTO);
168132Srobinson (void) strcpy(nettype, netclass);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (nettype == NULL)
1720Sstevel@tonic-gate nettype = "datagram_n";
173132Srobinson if ((handle = __rpc_setconf((char *)nettype)) == NULL)
1740Sstevel@tonic-gate return (RPC_UNKNOWNPROTO);
1750Sstevel@tonic-gate while (nconf = __rpc_getconf(handle)) {
1760Sstevel@tonic-gate struct t_info tinfo;
1770Sstevel@tonic-gate int fd;
1780Sstevel@tonic-gate uint_t addrlen;
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate if (nconf->nc_semantics != NC_TPI_CLTS)
1810Sstevel@tonic-gate continue;
1820Sstevel@tonic-gate if (fdlistno >= MAXBCAST)
1830Sstevel@tonic-gate break; /* No more slots available */
1840Sstevel@tonic-gate if ((fd = t_open(nconf->nc_device, O_RDWR, &tinfo)) == -1) {
1850Sstevel@tonic-gate stat = RPC_CANTSEND;
1860Sstevel@tonic-gate continue;
1870Sstevel@tonic-gate }
188*1676Sjpk __rpc_set_mac_options(fd, nconf, prog);
189132Srobinson if (t_bind(fd, NULL, NULL) == -1) {
1900Sstevel@tonic-gate (void) t_close(fd);
1910Sstevel@tonic-gate stat = RPC_CANTSEND;
1920Sstevel@tonic-gate continue;
1930Sstevel@tonic-gate }
194*1676Sjpk
1950Sstevel@tonic-gate /* Do protocol specific negotiating for broadcast */
1960Sstevel@tonic-gate if (netdir_options(nconf, ND_SET_BROADCAST, fd, NULL)) {
1970Sstevel@tonic-gate (void) t_close(fd);
1980Sstevel@tonic-gate stat = RPC_NOBROADCAST;
1990Sstevel@tonic-gate continue;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate fdlist[fdlistno].fd = fd;
2020Sstevel@tonic-gate fdlist[fdlistno].nconf = nconf;
2030Sstevel@tonic-gate fdlist[fdlistno].udp_flag = FALSE;
2040Sstevel@tonic-gate if (((addrlen = __rpc_get_a_size(tinfo.addr)) == 0) ||
2050Sstevel@tonic-gate ((fdlist[fdlistno].raddr.buf = malloc(addrlen)) == NULL)) {
206132Srobinson (void) t_close(fd);
2070Sstevel@tonic-gate stat = RPC_SYSTEMERROR;
2080Sstevel@tonic-gate goto done_broad;
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate fdlist[fdlistno].raddr.maxlen = addrlen;
2110Sstevel@tonic-gate fdlist[fdlistno].raddr.len = addrlen;
2120Sstevel@tonic-gate pfd[fdlistno].events = POLLIN | POLLPRI |
2130Sstevel@tonic-gate POLLRDNORM | POLLRDBAND;
2140Sstevel@tonic-gate pfd[fdlistno].fd = fdlist[fdlistno].fd = fd;
2150Sstevel@tonic-gate fdlist[fdlistno].asize = addrlen;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate if ((fdlist[fdlistno].dsize = __rpc_get_t_size(0,
2180Sstevel@tonic-gate tinfo.tsdu)) == 0) {
219132Srobinson (void) t_close(fd);
2200Sstevel@tonic-gate free(fdlist[fdlistno].raddr.buf);
2210Sstevel@tonic-gate stat = RPC_SYSTEMERROR; /* XXX */
2220Sstevel@tonic-gate goto done_broad;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate if (maxbufsize <= fdlist[fdlistno].dsize)
2260Sstevel@tonic-gate maxbufsize = fdlist[fdlistno].dsize;
2270Sstevel@tonic-gate #ifdef PORTMAP
2280Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
2290Sstevel@tonic-gate strcmp(nconf->nc_proto, NC_UDP) == 0) {
2300Sstevel@tonic-gate udpbufsz = fdlist[fdlistno].dsize;
2310Sstevel@tonic-gate if ((outbuf_pmap = malloc(udpbufsz)) == NULL) {
232132Srobinson (void) t_close(fd);
2330Sstevel@tonic-gate free(fdlist[fdlistno].raddr.buf);
2340Sstevel@tonic-gate stat = RPC_SYSTEMERROR;
2350Sstevel@tonic-gate goto done_broad;
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate pmap_flag = 1;
2380Sstevel@tonic-gate fdlist[fdlistno].udp_flag = TRUE;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate #endif
2410Sstevel@tonic-gate fdlistno++;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate if (fdlistno == 0) {
2450Sstevel@tonic-gate if (stat == RPC_SUCCESS)
2460Sstevel@tonic-gate stat = RPC_UNKNOWNPROTO;
2470Sstevel@tonic-gate goto done_broad;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate if (maxbufsize == 0) {
2500Sstevel@tonic-gate if (stat == RPC_SUCCESS)
2510Sstevel@tonic-gate stat = RPC_CANTSEND;
2520Sstevel@tonic-gate goto done_broad;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate inbuf = malloc((size_t)maxbufsize);
2550Sstevel@tonic-gate outbuf = malloc((size_t)maxbufsize);
2560Sstevel@tonic-gate if ((inbuf == NULL) || (outbuf == NULL)) {
2570Sstevel@tonic-gate stat = RPC_SYSTEMERROR;
2580Sstevel@tonic-gate goto done_broad;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /* Serialize all the arguments which have to be sent */
2620Sstevel@tonic-gate (void) gettimeofday(&t, (struct timezone *)0);
2630Sstevel@tonic-gate msg.rm_xid = getpid() ^ t.tv_sec ^ t.tv_usec;
2640Sstevel@tonic-gate msg.rm_direction = CALL;
2650Sstevel@tonic-gate msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
2660Sstevel@tonic-gate msg.rm_call.cb_prog = RPCBPROG;
2670Sstevel@tonic-gate msg.rm_call.cb_vers = RPCBVERS;
2680Sstevel@tonic-gate msg.rm_call.cb_proc = RPCBPROC_CALLIT;
2690Sstevel@tonic-gate barg.prog = prog;
2700Sstevel@tonic-gate barg.vers = vers;
2710Sstevel@tonic-gate barg.proc = proc;
2720Sstevel@tonic-gate barg.args.args_val = argsp;
2730Sstevel@tonic-gate barg.xdr_args = xargs;
2740Sstevel@tonic-gate bres.addr = uaddrp;
2750Sstevel@tonic-gate bres.results.results_val = resultsp;
2760Sstevel@tonic-gate bres.xdr_res = xresults;
2770Sstevel@tonic-gate msg.rm_call.cb_cred = sys_auth->ah_cred;
2780Sstevel@tonic-gate msg.rm_call.cb_verf = sys_auth->ah_verf;
2790Sstevel@tonic-gate xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE);
280132Srobinson if ((!xdr_callmsg(xdrs, &msg)) ||
281132Srobinson (!xdr_rpcb_rmtcallargs(xdrs, &barg))) {
2820Sstevel@tonic-gate stat = RPC_CANTENCODEARGS;
2830Sstevel@tonic-gate goto done_broad;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate t_udata.opt.len = 0;
2860Sstevel@tonic-gate t_udata.udata.buf = outbuf;
2870Sstevel@tonic-gate t_udata.udata.len = xdr_getpos(xdrs);
2880Sstevel@tonic-gate t_udata.udata.maxlen = t_udata.udata.len;
2890Sstevel@tonic-gate /* XXX Should have set opt to its legal maxlen. */
2900Sstevel@tonic-gate t_rdata.opt.len = t_rdata.opt.maxlen = 0;
2910Sstevel@tonic-gate xdr_destroy(xdrs);
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate #ifdef PORTMAP
2940Sstevel@tonic-gate /* Prepare the packet for version 2 PORTMAP */
2950Sstevel@tonic-gate if (pmap_flag) {
2960Sstevel@tonic-gate msg.rm_xid++; /* One way to distinguish */
2970Sstevel@tonic-gate msg.rm_call.cb_prog = PMAPPROG;
2980Sstevel@tonic-gate msg.rm_call.cb_vers = PMAPVERS;
2990Sstevel@tonic-gate msg.rm_call.cb_proc = PMAPPROC_CALLIT;
3000Sstevel@tonic-gate barg_pmap.prog = prog;
3010Sstevel@tonic-gate barg_pmap.vers = vers;
3020Sstevel@tonic-gate barg_pmap.proc = proc;
3030Sstevel@tonic-gate barg_pmap.args.args_val = argsp;
3040Sstevel@tonic-gate barg_pmap.xdr_args = xargs;
3050Sstevel@tonic-gate port = &bres_pmap.port; /* for use later on */
3060Sstevel@tonic-gate bres_pmap.xdr_res = xresults;
3070Sstevel@tonic-gate bres_pmap.res.res_val = resultsp;
3080Sstevel@tonic-gate xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE);
309132Srobinson if ((!xdr_callmsg(xdrs, &msg)) ||
310132Srobinson (!xdr_rmtcallargs(xdrs, &barg_pmap))) {
3110Sstevel@tonic-gate stat = RPC_CANTENCODEARGS;
3120Sstevel@tonic-gate goto done_broad;
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate t_udata_pmap.opt.len = 0;
3150Sstevel@tonic-gate t_udata_pmap.udata.buf = outbuf_pmap;
3160Sstevel@tonic-gate t_udata_pmap.udata.len = xdr_getpos(xdrs);
3170Sstevel@tonic-gate xdr_destroy(xdrs);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate #endif /* PORTMAP */
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate * Basic loop: broadcast the packets to transports which
3230Sstevel@tonic-gate * support data packets of size such that one can encode
3240Sstevel@tonic-gate * all the arguments.
3250Sstevel@tonic-gate * Wait a while for response(s).
3260Sstevel@tonic-gate * The response timeout grows larger per iteration.
3270Sstevel@tonic-gate */
3280Sstevel@tonic-gate hs.h_host = HOST_BROADCAST;
3290Sstevel@tonic-gate hs.h_serv = "rpcbind";
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate for (msec = inittime; msec <= waittime; msec += msec) {
3320Sstevel@tonic-gate /* Broadcast all the packets now */
3330Sstevel@tonic-gate for (i = 0; i < fdlistno; i++) {
3340Sstevel@tonic-gate if (strcmp(fdlist[i].nconf->nc_protofmly,
3350Sstevel@tonic-gate NC_INET6) == 0) {
3360Sstevel@tonic-gate /* if it's IPv6 */
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate struct netbuf addr;
3390Sstevel@tonic-gate struct sockaddr_in6 sa6;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /* fill in the multicast address */
3420Sstevel@tonic-gate bzero((char *)&sa6, sizeof (sa6));
3430Sstevel@tonic-gate sa6.sin6_family = AF_INET6;
3440Sstevel@tonic-gate sa6.sin6_port = htons(PMAPPORT);
3450Sstevel@tonic-gate (void) inet_pton(AF_INET6, RPCB_MULTICAST_ADDR,
3460Sstevel@tonic-gate &sa6.sin6_addr);
3470Sstevel@tonic-gate addr.maxlen = sizeof (struct sockaddr_in6);
3480Sstevel@tonic-gate addr.len = addr.maxlen;
3490Sstevel@tonic-gate addr.buf = (char *)&sa6;
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate /* now send rpcbind message */
3520Sstevel@tonic-gate t_udata.addr = addr;
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd,
3560Sstevel@tonic-gate &t_udata)) {
3570Sstevel@tonic-gate (void) syslog(LOG_ERR,
3580Sstevel@tonic-gate "Cannot send broadcast\
3590Sstevel@tonic-gate packet: %m");
360132Srobinson #ifdef RPC_DEBUG
3610Sstevel@tonic-gate t_error("rpc_broadcast: t_sndudata");
3620Sstevel@tonic-gate #endif
3630Sstevel@tonic-gate stat = RPC_CANTSEND;
3640Sstevel@tonic-gate continue;
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate } else {
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate struct nd_addrlist *addrlist;
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate if (fdlist[i].dsize < t_udata.udata.len) {
3720Sstevel@tonic-gate stat = RPC_CANTSEND;
3730Sstevel@tonic-gate continue;
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate if (netdir_getbyname(fdlist[i].nconf, &hs,
3760Sstevel@tonic-gate &addrlist) || (addrlist->n_cnt == 0)) {
3770Sstevel@tonic-gate stat = RPC_N2AXLATEFAILURE;
3780Sstevel@tonic-gate continue;
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate for (j = 0; j < addrlist->n_cnt; j++) {
382132Srobinson #ifdef RPC_DEBUG
3830Sstevel@tonic-gate struct netconfig *nconf =
3840Sstevel@tonic-gate fdlist[i].nconf;
385132Srobinson #endif
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate t_udata.addr = addrlist->n_addrs[j];
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate * Only use version 3 if lowvers
3910Sstevel@tonic-gate * is not set or transport is not UDP.
3920Sstevel@tonic-gate */
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if (!lowvers || !fdlist[i].udp_flag)
3950Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd,
3960Sstevel@tonic-gate &t_udata)) {
3970Sstevel@tonic-gate (void) syslog(LOG_ERR,
3980Sstevel@tonic-gate "Cannot send broadcast\
3990Sstevel@tonic-gate packet: %m");
400132Srobinson #ifdef RPC_DEBUG
4010Sstevel@tonic-gate t_error("rpc_broadcast: t_sndudata");
4020Sstevel@tonic-gate #endif
4030Sstevel@tonic-gate stat = RPC_CANTSEND;
4040Sstevel@tonic-gate continue;
4050Sstevel@tonic-gate };
406132Srobinson #ifdef RPC_DEBUG
4070Sstevel@tonic-gate if (!lowvers || !fdlist[i].udp_flag)
4080Sstevel@tonic-gate fprintf(stderr, "Broadcast\
4090Sstevel@tonic-gate packet sent for %s\n", nconf->nc_netid);
4100Sstevel@tonic-gate #endif
4110Sstevel@tonic-gate #ifdef PORTMAP
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate * Send the version 2 packet also
4140Sstevel@tonic-gate * for UDP/IP
4150Sstevel@tonic-gate */
4160Sstevel@tonic-gate if (fdlist[i].udp_flag) {
4170Sstevel@tonic-gate t_udata_pmap.addr =
4180Sstevel@tonic-gate t_udata.addr;
4190Sstevel@tonic-gate if (t_sndudata(fdlist[i].fd,
4200Sstevel@tonic-gate &t_udata_pmap)) {
4210Sstevel@tonic-gate (void) syslog(LOG_ERR,\
4220Sstevel@tonic-gate "Cannot send broadcast packet: %m");
4230Sstevel@tonic-gate #ifdef RPC_DEBUG
4240Sstevel@tonic-gate t_error("rpc_broadcast:\
4250Sstevel@tonic-gate t_sndudata");
4260Sstevel@tonic-gate #endif
4270Sstevel@tonic-gate stat = RPC_CANTSEND;
4280Sstevel@tonic-gate continue;
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate #ifdef RPC_DEBUG
4320Sstevel@tonic-gate fprintf(stderr, "PMAP Broadcast packet\
4330Sstevel@tonic-gate sent for %s\n", nconf->nc_netid);
4340Sstevel@tonic-gate #endif
4350Sstevel@tonic-gate #endif /* PORTMAP */
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate /* End for sending all packets on this transport */
4380Sstevel@tonic-gate (void) netdir_free((char *)addrlist, ND_ADDRLIST);
4390Sstevel@tonic-gate } /* end non-IPv6 */
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate } /* End for sending on all transports */
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if (eachresult == NULL) {
4440Sstevel@tonic-gate stat = RPC_SUCCESS;
4450Sstevel@tonic-gate goto done_broad;
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate * Get all the replies from these broadcast requests
4500Sstevel@tonic-gate */
4510Sstevel@tonic-gate recv_again:
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate switch (pollretval = poll(pfd, fdlistno, msec)) {
4540Sstevel@tonic-gate case 0: /* timed out */
4550Sstevel@tonic-gate stat = RPC_TIMEDOUT;
4560Sstevel@tonic-gate continue;
4570Sstevel@tonic-gate case -1: /* some kind of error - we ignore it */
4580Sstevel@tonic-gate goto recv_again;
4590Sstevel@tonic-gate } /* end of poll results switch */
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate t_rdata.udata.buf = inbuf;
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate for (i = fds_found = 0;
4640Sstevel@tonic-gate i < fdlistno && fds_found < pollretval; i++) {
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate int flag;
4670Sstevel@tonic-gate bool_t done = FALSE;
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate if (pfd[i].revents == 0)
4700Sstevel@tonic-gate continue;
4710Sstevel@tonic-gate else if (pfd[i].revents & POLLNVAL) {
4720Sstevel@tonic-gate /*
4730Sstevel@tonic-gate * Something bad has happened to this descri-
4740Sstevel@tonic-gate * ptor. We can cause poll() to ignore
4750Sstevel@tonic-gate * it simply by using a negative fd. We do that
4760Sstevel@tonic-gate * rather than compacting the pfd[] and fdlist[]
4770Sstevel@tonic-gate * arrays.
4780Sstevel@tonic-gate */
4790Sstevel@tonic-gate pfd[i].fd = -1;
4800Sstevel@tonic-gate fds_found++;
4810Sstevel@tonic-gate continue;
4820Sstevel@tonic-gate } else
4830Sstevel@tonic-gate fds_found++;
4840Sstevel@tonic-gate #ifdef RPC_DEBUG
4850Sstevel@tonic-gate fprintf(stderr, "response for %s\n",
4860Sstevel@tonic-gate fdlist[i].nconf->nc_netid);
4870Sstevel@tonic-gate #endif
4880Sstevel@tonic-gate try_again:
4890Sstevel@tonic-gate t_rdata.udata.maxlen = fdlist[i].dsize;
4900Sstevel@tonic-gate t_rdata.udata.len = 0;
4910Sstevel@tonic-gate t_rdata.addr = fdlist[i].raddr;
4920Sstevel@tonic-gate if (t_rcvudata(fdlist[i].fd, &t_rdata, &flag) == -1) {
4930Sstevel@tonic-gate if (t_errno == TSYSERR && errno == EINTR)
4940Sstevel@tonic-gate goto try_again;
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate * Ignore any T_UDERR look errors.
4980Sstevel@tonic-gate * We should never see any ICMP port
4990Sstevel@tonic-gate * unreachables when broadcasting but it has
5000Sstevel@tonic-gate * been observed with broken IP
5010Sstevel@tonic-gate * implementations.
5020Sstevel@tonic-gate */
5030Sstevel@tonic-gate if (t_errno == TLOOK &&
5040Sstevel@tonic-gate t_look(fdlist[i].fd) == T_UDERR &&
5050Sstevel@tonic-gate t_rcvuderr(fdlist[i].fd, NULL) == 0)
5060Sstevel@tonic-gate goto recv_again;
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate (void) syslog(LOG_ERR,
5090Sstevel@tonic-gate "Cannot receive reply to \
5100Sstevel@tonic-gate broadcast: %m");
5110Sstevel@tonic-gate stat = RPC_CANTRECV;
5120Sstevel@tonic-gate continue;
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate /*
5150Sstevel@tonic-gate * Not taking care of flag for T_MORE.
5160Sstevel@tonic-gate * We are assuming that
5170Sstevel@tonic-gate * such calls should not take more than one
5180Sstevel@tonic-gate * transport packet.
5190Sstevel@tonic-gate */
5200Sstevel@tonic-gate if (flag & T_MORE)
5210Sstevel@tonic-gate continue; /* Drop that and go ahead */
5220Sstevel@tonic-gate if (t_rdata.udata.len < (uint_t)sizeof (uint32_t))
5230Sstevel@tonic-gate continue; /* Drop that and go ahead */
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate * see if reply transaction id matches sent id.
5260Sstevel@tonic-gate * If so, decode the results. If return id is xid + 1
5270Sstevel@tonic-gate * it was a PORTMAP reply
5280Sstevel@tonic-gate */
529132Srobinson /* LINTED pointer cast */
5300Sstevel@tonic-gate if (*((uint32_t *)(inbuf)) == *((uint32_t *)(outbuf))) {
5310Sstevel@tonic-gate pmap_reply_flag = 0;
5320Sstevel@tonic-gate msg.acpted_rply.ar_verf = _null_auth;
5330Sstevel@tonic-gate msg.acpted_rply.ar_results.where =
5340Sstevel@tonic-gate (caddr_t)&bres;
5350Sstevel@tonic-gate msg.acpted_rply.ar_results.proc =
5360Sstevel@tonic-gate (xdrproc_t)xdr_rpcb_rmtcallres;
5370Sstevel@tonic-gate #ifdef PORTMAP
5380Sstevel@tonic-gate } else if (pmap_flag &&
539132Srobinson /* LINTED pointer cast */
5400Sstevel@tonic-gate *((uint32_t *)(inbuf)) ==
541132Srobinson /* LINTED pointer cast */
542132Srobinson *((uint32_t *)(outbuf_pmap))) {
5430Sstevel@tonic-gate pmap_reply_flag = 1;
5440Sstevel@tonic-gate msg.acpted_rply.ar_verf = _null_auth;
5450Sstevel@tonic-gate msg.acpted_rply.ar_results.where =
5460Sstevel@tonic-gate (caddr_t)&bres_pmap;
5470Sstevel@tonic-gate msg.acpted_rply.ar_results.proc =
5480Sstevel@tonic-gate (xdrproc_t)xdr_rmtcallres;
5490Sstevel@tonic-gate #endif /* PORTMAP */
5500Sstevel@tonic-gate } else
5510Sstevel@tonic-gate continue;
5520Sstevel@tonic-gate xdrmem_create(xdrs, inbuf,
5530Sstevel@tonic-gate (uint_t)t_rdata.udata.len, XDR_DECODE);
5540Sstevel@tonic-gate if (xdr_replymsg(xdrs, &msg)) {
5550Sstevel@tonic-gate if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
5560Sstevel@tonic-gate (msg.acpted_rply.ar_stat == SUCCESS)) {
5570Sstevel@tonic-gate struct netbuf *taddr;
5580Sstevel@tonic-gate #ifdef PORTMAP
5590Sstevel@tonic-gate if (pmap_flag && pmap_reply_flag) {
5600Sstevel@tonic-gate /* convert port to taddr */
561132Srobinson /* LINTED pointer cast */
5620Sstevel@tonic-gate ((struct sockaddr_in *)
5630Sstevel@tonic-gate t_rdata.addr.buf)->sin_port =
5640Sstevel@tonic-gate htons((ushort_t)*port);
5650Sstevel@tonic-gate taddr = &t_rdata.addr;
5660Sstevel@tonic-gate } else /* Convert the uaddr to taddr */
5670Sstevel@tonic-gate #endif
5680Sstevel@tonic-gate taddr = uaddr2taddr(
5690Sstevel@tonic-gate fdlist[i].nconf,
5700Sstevel@tonic-gate uaddrp);
5710Sstevel@tonic-gate done = (*eachresult)(resultsp, taddr,
5720Sstevel@tonic-gate fdlist[i].nconf);
5730Sstevel@tonic-gate #ifdef RPC_DEBUG
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate int k;
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate printf("rmt addr = ");
5780Sstevel@tonic-gate for (k = 0; k < taddr->len; k++)
5790Sstevel@tonic-gate printf("%d ", taddr->buf[k]);
5800Sstevel@tonic-gate printf("\n");
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate #endif
5830Sstevel@tonic-gate if (taddr && !pmap_reply_flag)
5840Sstevel@tonic-gate netdir_free((char *)taddr,
5850Sstevel@tonic-gate ND_ADDR);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate /* otherwise, we just ignore the errors ... */
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate /* else some kind of deserialization problem ... */
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate xdrs->x_op = XDR_FREE;
5920Sstevel@tonic-gate msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
5930Sstevel@tonic-gate (void) xdr_replymsg(xdrs, &msg);
5940Sstevel@tonic-gate (void) (*xresults)(xdrs, resultsp);
5950Sstevel@tonic-gate XDR_DESTROY(xdrs);
5960Sstevel@tonic-gate if (done) {
5970Sstevel@tonic-gate stat = RPC_SUCCESS;
5980Sstevel@tonic-gate goto done_broad;
5990Sstevel@tonic-gate } else {
6000Sstevel@tonic-gate if (rpc_callerr.re_status == RPC_SYSTEMERROR) {
6010Sstevel@tonic-gate stat = RPC_SYSTEMERROR;
6020Sstevel@tonic-gate goto done_broad;
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate goto recv_again;
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate } /* The recv for loop */
6070Sstevel@tonic-gate } /* The giant for loop */
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate done_broad:
6100Sstevel@tonic-gate if (inbuf)
611132Srobinson free(inbuf);
6120Sstevel@tonic-gate if (outbuf)
613132Srobinson free(outbuf);
6140Sstevel@tonic-gate #ifdef PORTMAP
6150Sstevel@tonic-gate if (outbuf_pmap)
616132Srobinson free(outbuf_pmap);
6170Sstevel@tonic-gate #endif
6180Sstevel@tonic-gate for (i = 0; i < fdlistno; i++) {
6190Sstevel@tonic-gate (void) t_close(fdlist[i].fd);
620132Srobinson free(fdlist[i].raddr.buf);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate AUTH_DESTROY(sys_auth);
6230Sstevel@tonic-gate (void) __rpc_endconf(handle);
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate return (stat);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate enum clnt_stat
rpc_broadcast(const rpcprog_t prog,const rpcvers_t vers,const rpcproc_t proc,const xdrproc_t xargs,caddr_t argsp,xdrproc_t const xresults,caddr_t resultsp,const resultproc_t eachresult,const char * nettype)629132Srobinson rpc_broadcast(const rpcprog_t prog, const rpcvers_t vers, const rpcproc_t proc,
630132Srobinson const xdrproc_t xargs, caddr_t argsp, xdrproc_t const xresults,
631132Srobinson caddr_t resultsp, const resultproc_t eachresult, const char *nettype)
6320Sstevel@tonic-gate {
633132Srobinson return (rpc_broadcast_exp(prog, vers, proc, xargs, argsp,
6340Sstevel@tonic-gate xresults, resultsp, eachresult,
635132Srobinson INITTIME, WAITTIME, nettype));
6360Sstevel@tonic-gate }
637