4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * 4.3 BSD under license from the Regents of the University of
36 * Client interface to broadcast service.
38 * The following is kludged-up support for simple rpc broadcasts.
39 * Someday a large, complicated system will replace these routines.
47 #include <rpc/nettype.h>
50 #include <rpc/pmap_prot.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
63 #define MAXBCAST 20 /* Max no of broadcasting transports */
64 #define INITTIME 4000 /* Time to wait initially */
65 #define WAITTIME 8000 /* Maximum time to wait */
67 int lowvers
= 1; /* by default, broadcast only version 2 over UDP */
73 * If nettype is NULL, it broadcasts on all the available
74 * datagram_n transports. May potentially lead to broadacst storms
75 * and hence should be used with caution, care and courage.
77 * The current parameter xdr packet size is limited by the max tsdu
78 * size of the transport. If the max tsdu size of any transport is
79 * smaller than the parameter xdr packet, then broadcast is not
80 * sent on that transport.
82 * Also, the packet size should be less the packet size of
83 * the data link layer (for ethernet it is 1400 bytes). There is
84 * no easy way to find out the max size of the data link layer and
85 * we are assuming that the args would be smaller than that.
87 * The result size has to be smaller than the transport tsdu size.
89 * We send two packets for UDP, one for rpcbind and one for portmap.
90 * For those machines which support both rpcbind and portmap, it will
91 * cause them to reply twice, and also here it will get two responses
92 * ... inefficient and clumsy.
97 rpc_broadcast_exp(const rpcprog_t prog
, const rpcvers_t vers
,
98 const rpcproc_t proc
, const xdrproc_t xargs
, caddr_t argsp
,
99 const xdrproc_t xresults
, caddr_t resultsp
, const resultproc_t eachresult
,
100 const int inittime
, const int waittime
, const char *netclass
)
102 enum clnt_stat stat
= RPC_SUCCESS
; /* Return status */
103 XDR xdr_stream
; /* XDR stream */
104 XDR
*xdrs
= &xdr_stream
;
105 struct rpc_msg msg
; /* RPC message */
107 char *outbuf
= NULL
; /* Broadcast msg buffer */
108 char *inbuf
= NULL
; /* Reply buf */
109 uint_t maxbufsize
= 0;
110 AUTH
*sys_auth
= authsys_create_default();
113 char uaddress
[1024]; /* A self imposed limit */
114 char *uaddrp
= uaddress
;
115 int pmap_reply_flag
; /* reply recvd from PORTMAP */
116 /* An array of all the suitable broadcast transports */
118 int fd
; /* File descriptor */
119 bool_t udp_flag
; /* this is udp */
120 struct netconfig
*nconf
; /* Netconfig structure */
121 uint_t asize
; /* Size of the addr buf */
122 uint_t dsize
; /* Size of the data buf */
123 struct netbuf raddr
; /* Remote address */
124 struct nd_addrlist
*nal
; /* Broadcast addrs */
126 struct pollfd pfd
[MAXBCAST
];
128 struct r_rpcb_rmtcallargs barg
; /* Remote arguments */
129 struct r_rpcb_rmtcallres bres
; /* Remote results */
130 struct t_unitdata t_udata
, t_rdata
;
131 struct netconfig
*nconf
;
132 struct nd_hostserv hs
;
136 char nettype_array
[NETIDLEN
];
137 char *nettype
= &nettype_array
[0];
139 rpcport_t
*port
; /* Remote port number */
140 int pmap_flag
= 0; /* UDP exists ? */
141 char *outbuf_pmap
= NULL
;
142 struct p_rmtcallargs barg_pmap
; /* Remote arguments */
143 struct p_rmtcallres bres_pmap
; /* Remote results */
144 struct t_unitdata t_udata_pmap
;
147 if (sys_auth
== NULL
)
148 return (RPC_SYSTEMERROR
);
150 * initialization: create a fd, a broadcast address, and send the
151 * request on the broadcast transport.
152 * Listen on all of them and on replies, call the user supplied
156 if (netclass
== NULL
)
159 size_t len
= strlen(netclass
);
160 if (len
>= sizeof (nettype_array
))
161 return (RPC_UNKNOWNPROTO
);
162 (void) strcpy(nettype
, netclass
);
166 nettype
= "datagram_n";
167 if ((handle
= __rpc_setconf((char *)nettype
)) == NULL
)
168 return (RPC_UNKNOWNPROTO
);
169 while (nconf
= __rpc_getconf(handle
)) {
174 if (nconf
->nc_semantics
!= NC_TPI_CLTS
)
176 if (fdlistno
>= MAXBCAST
)
177 break; /* No more slots available */
178 if ((fd
= t_open(nconf
->nc_device
, O_RDWR
, &tinfo
)) == -1) {
182 if (t_bind(fd
, NULL
, NULL
) == -1) {
188 /* Do protocol specific negotiating for broadcast */
189 if (netdir_options(nconf
, ND_SET_BROADCAST
, fd
, NULL
)) {
191 stat
= RPC_NOBROADCAST
;
194 fdlist
[fdlistno
].fd
= fd
;
195 fdlist
[fdlistno
].nconf
= nconf
;
196 fdlist
[fdlistno
].udp_flag
= FALSE
;
197 if (((addrlen
= __rpc_get_a_size(tinfo
.addr
)) == 0) ||
198 ((fdlist
[fdlistno
].raddr
.buf
= malloc(addrlen
)) == NULL
)) {
200 stat
= RPC_SYSTEMERROR
;
203 fdlist
[fdlistno
].raddr
.maxlen
= addrlen
;
204 fdlist
[fdlistno
].raddr
.len
= addrlen
;
205 pfd
[fdlistno
].events
= POLLIN
| POLLPRI
|
206 POLLRDNORM
| POLLRDBAND
;
207 pfd
[fdlistno
].fd
= fdlist
[fdlistno
].fd
= fd
;
208 fdlist
[fdlistno
].asize
= addrlen
;
210 if ((fdlist
[fdlistno
].dsize
= __rpc_get_t_size(0,
213 free(fdlist
[fdlistno
].raddr
.buf
);
214 stat
= RPC_SYSTEMERROR
; /* XXX */
218 if (maxbufsize
<= fdlist
[fdlistno
].dsize
)
219 maxbufsize
= fdlist
[fdlistno
].dsize
;
220 if (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0 &&
221 strcmp(nconf
->nc_proto
, NC_UDP
) == 0) {
222 udpbufsz
= fdlist
[fdlistno
].dsize
;
223 if ((outbuf_pmap
= malloc(udpbufsz
)) == NULL
) {
225 free(fdlist
[fdlistno
].raddr
.buf
);
226 stat
= RPC_SYSTEMERROR
;
230 fdlist
[fdlistno
].udp_flag
= TRUE
;
236 if (stat
== RPC_SUCCESS
)
237 stat
= RPC_UNKNOWNPROTO
;
240 if (maxbufsize
== 0) {
241 if (stat
== RPC_SUCCESS
)
245 inbuf
= malloc((size_t)maxbufsize
);
246 outbuf
= malloc((size_t)maxbufsize
);
247 if ((inbuf
== NULL
) || (outbuf
== NULL
)) {
248 stat
= RPC_SYSTEMERROR
;
252 /* Serialize all the arguments which have to be sent */
253 (void) gettimeofday(&t
, NULL
);
254 msg
.rm_xid
= getpid() ^ t
.tv_sec
^ t
.tv_usec
;
255 msg
.rm_direction
= CALL
;
256 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
257 msg
.rm_call
.cb_prog
= RPCBPROG
;
258 msg
.rm_call
.cb_vers
= RPCBVERS
;
259 msg
.rm_call
.cb_proc
= RPCBPROC_CALLIT
;
263 barg
.args
.args_val
= argsp
;
264 barg
.xdr_args
= xargs
;
266 bres
.results
.results_val
= resultsp
;
267 bres
.xdr_res
= xresults
;
268 msg
.rm_call
.cb_cred
= sys_auth
->ah_cred
;
269 msg
.rm_call
.cb_verf
= sys_auth
->ah_verf
;
270 xdrmem_create(xdrs
, outbuf
, maxbufsize
, XDR_ENCODE
);
271 if ((!xdr_callmsg(xdrs
, &msg
)) ||
272 (!xdr_rpcb_rmtcallargs(xdrs
, &barg
))) {
273 stat
= RPC_CANTENCODEARGS
;
277 t_udata
.udata
.buf
= outbuf
;
278 t_udata
.udata
.len
= xdr_getpos(xdrs
);
279 t_udata
.udata
.maxlen
= t_udata
.udata
.len
;
280 /* XXX Should have set opt to its legal maxlen. */
281 t_rdata
.opt
.len
= t_rdata
.opt
.maxlen
= 0;
284 /* Prepare the packet for version 2 PORTMAP */
286 msg
.rm_xid
++; /* One way to distinguish */
287 msg
.rm_call
.cb_prog
= PMAPPROG
;
288 msg
.rm_call
.cb_vers
= PMAPVERS
;
289 msg
.rm_call
.cb_proc
= PMAPPROC_CALLIT
;
290 barg_pmap
.prog
= prog
;
291 barg_pmap
.vers
= vers
;
292 barg_pmap
.proc
= proc
;
293 barg_pmap
.args
.args_val
= argsp
;
294 barg_pmap
.xdr_args
= xargs
;
295 port
= &bres_pmap
.port
; /* for use later on */
296 bres_pmap
.xdr_res
= xresults
;
297 bres_pmap
.res
.res_val
= resultsp
;
298 xdrmem_create(xdrs
, outbuf_pmap
, udpbufsz
, XDR_ENCODE
);
299 if ((!xdr_callmsg(xdrs
, &msg
)) ||
300 (!xdr_rmtcallargs(xdrs
, &barg_pmap
))) {
301 stat
= RPC_CANTENCODEARGS
;
304 t_udata_pmap
.opt
.len
= 0;
305 t_udata_pmap
.udata
.buf
= outbuf_pmap
;
306 t_udata_pmap
.udata
.len
= xdr_getpos(xdrs
);
311 * Basic loop: broadcast the packets to transports which
312 * support data packets of size such that one can encode
314 * Wait a while for response(s).
315 * The response timeout grows larger per iteration.
317 hs
.h_host
= HOST_BROADCAST
;
318 hs
.h_serv
= "rpcbind";
320 for (msec
= inittime
; msec
<= waittime
; msec
+= msec
) {
321 /* Broadcast all the packets now */
322 for (i
= 0; i
< fdlistno
; i
++) {
323 if (strcmp(fdlist
[i
].nconf
->nc_protofmly
,
328 struct sockaddr_in6 sa6
;
330 /* fill in the multicast address */
331 bzero((char *)&sa6
, sizeof (sa6
));
332 sa6
.sin6_family
= AF_INET6
;
333 sa6
.sin6_port
= htons(PMAPPORT
);
334 (void) inet_pton(AF_INET6
, RPCB_MULTICAST_ADDR
,
336 addr
.maxlen
= sizeof (struct sockaddr_in6
);
337 addr
.len
= addr
.maxlen
;
338 addr
.buf
= (char *)&sa6
;
340 /* now send rpcbind message */
344 if (t_sndudata(fdlist
[i
].fd
,
346 (void) syslog(LOG_ERR
,
347 "Cannot send broadcast\
350 t_error("rpc_broadcast: t_sndudata");
358 struct nd_addrlist
*addrlist
;
360 if (fdlist
[i
].dsize
< t_udata
.udata
.len
) {
364 if (netdir_getbyname(fdlist
[i
].nconf
, &hs
,
365 &addrlist
) || (addrlist
->n_cnt
== 0)) {
366 stat
= RPC_N2AXLATEFAILURE
;
370 for (j
= 0; j
< addrlist
->n_cnt
; j
++) {
372 struct netconfig
*nconf
=
376 t_udata
.addr
= addrlist
->n_addrs
[j
];
379 * Only use version 3 if lowvers
380 * is not set or transport is not UDP.
383 if (!lowvers
|| !fdlist
[i
].udp_flag
)
384 if (t_sndudata(fdlist
[i
].fd
,
386 (void) syslog(LOG_ERR
,
387 "Cannot send broadcast\
390 t_error("rpc_broadcast: t_sndudata");
396 if (!lowvers
|| !fdlist
[i
].udp_flag
)
397 fprintf(stderr
, "Broadcast\
398 packet sent for %s\n", nconf
->nc_netid
);
401 * Send the version 2 packet also
404 if (fdlist
[i
].udp_flag
) {
407 if (t_sndudata(fdlist
[i
].fd
,
409 (void) syslog(LOG_ERR
,\
410 "Cannot send broadcast packet: %m");
412 t_error("rpc_broadcast:\
420 fprintf(stderr
, "PMAP Broadcast packet\
421 sent for %s\n", nconf
->nc_netid
);
424 /* End for sending all packets on this transport */
425 (void) netdir_free((char *)addrlist
, ND_ADDRLIST
);
428 } /* End for sending on all transports */
430 if (eachresult
== NULL
) {
436 * Get all the replies from these broadcast requests
440 switch (pollretval
= poll(pfd
, fdlistno
, msec
)) {
441 case 0: /* timed out */
444 case -1: /* some kind of error - we ignore it */
446 } /* end of poll results switch */
448 t_rdata
.udata
.buf
= inbuf
;
450 for (i
= fds_found
= 0;
451 i
< fdlistno
&& fds_found
< pollretval
; i
++) {
456 if (pfd
[i
].revents
== 0)
458 else if (pfd
[i
].revents
& POLLNVAL
) {
460 * Something bad has happened to this descri-
461 * ptor. We can cause poll() to ignore
462 * it simply by using a negative fd. We do that
463 * rather than compacting the pfd[] and fdlist[]
472 fprintf(stderr
, "response for %s\n",
473 fdlist
[i
].nconf
->nc_netid
);
476 t_rdata
.udata
.maxlen
= fdlist
[i
].dsize
;
477 t_rdata
.udata
.len
= 0;
478 t_rdata
.addr
= fdlist
[i
].raddr
;
479 if (t_rcvudata(fdlist
[i
].fd
, &t_rdata
, &flag
) == -1) {
480 if (t_errno
== TSYSERR
&& errno
== EINTR
)
484 * Ignore any T_UDERR look errors.
485 * We should never see any ICMP port
486 * unreachables when broadcasting but it has
487 * been observed with broken IP
490 if (t_errno
== TLOOK
&&
491 t_look(fdlist
[i
].fd
) == T_UDERR
&&
492 t_rcvuderr(fdlist
[i
].fd
, NULL
) == 0)
495 (void) syslog(LOG_ERR
,
496 "Cannot receive reply to \
502 * Not taking care of flag for T_MORE.
503 * We are assuming that
504 * such calls should not take more than one
508 continue; /* Drop that and go ahead */
509 if (t_rdata
.udata
.len
< (uint_t
)sizeof (uint32_t))
510 continue; /* Drop that and go ahead */
512 * see if reply transaction id matches sent id.
513 * If so, decode the results. If return id is xid + 1
514 * it was a PORTMAP reply
516 /* LINTED pointer cast */
517 if (*((uint32_t *)(inbuf
)) == *((uint32_t *)(outbuf
))) {
519 msg
.acpted_rply
.ar_verf
= _null_auth
;
520 msg
.acpted_rply
.ar_results
.where
=
522 msg
.acpted_rply
.ar_results
.proc
=
523 (xdrproc_t
)xdr_rpcb_rmtcallres
;
524 } else if (pmap_flag
&&
525 /* LINTED pointer cast */
526 *((uint32_t *)(inbuf
)) ==
527 /* LINTED pointer cast */
528 *((uint32_t *)(outbuf_pmap
))) {
530 msg
.acpted_rply
.ar_verf
= _null_auth
;
531 msg
.acpted_rply
.ar_results
.where
=
533 msg
.acpted_rply
.ar_results
.proc
=
534 (xdrproc_t
)xdr_rmtcallres
;
537 xdrmem_create(xdrs
, inbuf
,
538 (uint_t
)t_rdata
.udata
.len
, XDR_DECODE
);
539 if (xdr_replymsg(xdrs
, &msg
)) {
540 if ((msg
.rm_reply
.rp_stat
== MSG_ACCEPTED
) &&
541 (msg
.acpted_rply
.ar_stat
== SUCCESS
)) {
542 struct netbuf
*taddr
;
543 if (pmap_flag
&& pmap_reply_flag
) {
544 /* convert port to taddr */
545 /* LINTED pointer cast */
546 ((struct sockaddr_in
*)
547 t_rdata
.addr
.buf
)->sin_port
=
548 htons((ushort_t
)*port
);
549 taddr
= &t_rdata
.addr
;
550 } else /* Convert the uaddr to taddr */
554 done
= (*eachresult
)(resultsp
, taddr
,
560 printf("rmt addr = ");
561 for (k
= 0; k
< taddr
->len
; k
++)
562 printf("%d ", taddr
->buf
[k
]);
566 if (taddr
&& !pmap_reply_flag
)
567 netdir_free((char *)taddr
,
570 /* otherwise, we just ignore the errors ... */
572 /* else some kind of deserialization problem ... */
574 xdrs
->x_op
= XDR_FREE
;
575 msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
)xdr_void
;
576 (void) xdr_replymsg(xdrs
, &msg
);
577 (void) (*xresults
)(xdrs
, resultsp
);
583 if (rpc_callerr
.re_status
== RPC_SYSTEMERROR
) {
584 stat
= RPC_SYSTEMERROR
;
589 } /* The recv for loop */
590 } /* The giant for loop */
596 for (i
= 0; i
< fdlistno
; i
++) {
597 (void) t_close(fdlist
[i
].fd
);
598 free(fdlist
[i
].raddr
.buf
);
600 AUTH_DESTROY(sys_auth
);
601 (void) __rpc_endconf(handle
);
607 rpc_broadcast(const rpcprog_t prog
, const rpcvers_t vers
, const rpcproc_t proc
,
608 const xdrproc_t xargs
, caddr_t argsp
, xdrproc_t
const xresults
,
609 caddr_t resultsp
, const resultproc_t eachresult
, const char *nettype
)
611 return (rpc_broadcast_exp(prog
, vers
, proc
, xargs
, argsp
,
612 xresults
, resultsp
, eachresult
,
613 INITTIME
, WAITTIME
, nettype
));