import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / clnt_bcast.c
blob1d56888b43abaffa1f0d1fa02703a4b76f8fecb9
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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
32 * California.
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.
42 #include "mt.h"
43 #include "rpc_mt.h"
44 #include <string.h>
45 #include <strings.h>
46 #include <rpc/rpc.h>
47 #include <rpc/nettype.h>
48 #include <sys/poll.h>
49 #include <netdir.h>
50 #include <rpc/pmap_prot.h>
51 #ifdef RPC_DEBUG
52 #include <stdio.h>
53 #endif
54 #include <errno.h>
55 #include <syslog.h>
56 #include <stdlib.h>
57 #include <unistd.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 */
68 #ifndef NETIDLEN
69 #define NETIDLEN 32
70 #endif
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.
96 enum clnt_stat
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 */
106 struct timeval t;
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();
111 int i, j;
112 void *handle;
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 */
117 struct {
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 */
125 } fdlist[MAXBCAST];
126 struct pollfd pfd[MAXBCAST];
127 int fdlistno = 0;
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;
133 int msec;
134 int pollretval;
135 int fds_found;
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;
145 int udpbufsz = 0;
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
153 * function.
156 if (netclass == NULL)
157 nettype = NULL;
158 else {
159 size_t len = strlen(netclass);
160 if (len >= sizeof (nettype_array))
161 return (RPC_UNKNOWNPROTO);
162 (void) strcpy(nettype, netclass);
165 if (nettype == NULL)
166 nettype = "datagram_n";
167 if ((handle = __rpc_setconf((char *)nettype)) == NULL)
168 return (RPC_UNKNOWNPROTO);
169 while (nconf = __rpc_getconf(handle)) {
170 struct t_info tinfo;
171 int fd;
172 uint_t addrlen;
174 if (nconf->nc_semantics != NC_TPI_CLTS)
175 continue;
176 if (fdlistno >= MAXBCAST)
177 break; /* No more slots available */
178 if ((fd = t_open(nconf->nc_device, O_RDWR, &tinfo)) == -1) {
179 stat = RPC_CANTSEND;
180 continue;
182 if (t_bind(fd, NULL, NULL) == -1) {
183 (void) t_close(fd);
184 stat = RPC_CANTSEND;
185 continue;
188 /* Do protocol specific negotiating for broadcast */
189 if (netdir_options(nconf, ND_SET_BROADCAST, fd, NULL)) {
190 (void) t_close(fd);
191 stat = RPC_NOBROADCAST;
192 continue;
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)) {
199 (void) t_close(fd);
200 stat = RPC_SYSTEMERROR;
201 goto done_broad;
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,
211 tinfo.tsdu)) == 0) {
212 (void) t_close(fd);
213 free(fdlist[fdlistno].raddr.buf);
214 stat = RPC_SYSTEMERROR; /* XXX */
215 goto done_broad;
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) {
224 (void) t_close(fd);
225 free(fdlist[fdlistno].raddr.buf);
226 stat = RPC_SYSTEMERROR;
227 goto done_broad;
229 pmap_flag = 1;
230 fdlist[fdlistno].udp_flag = TRUE;
232 fdlistno++;
235 if (fdlistno == 0) {
236 if (stat == RPC_SUCCESS)
237 stat = RPC_UNKNOWNPROTO;
238 goto done_broad;
240 if (maxbufsize == 0) {
241 if (stat == RPC_SUCCESS)
242 stat = RPC_CANTSEND;
243 goto done_broad;
245 inbuf = malloc((size_t)maxbufsize);
246 outbuf = malloc((size_t)maxbufsize);
247 if ((inbuf == NULL) || (outbuf == NULL)) {
248 stat = RPC_SYSTEMERROR;
249 goto done_broad;
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;
260 barg.prog = prog;
261 barg.vers = vers;
262 barg.proc = proc;
263 barg.args.args_val = argsp;
264 barg.xdr_args = xargs;
265 bres.addr = uaddrp;
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;
274 goto done_broad;
276 t_udata.opt.len = 0;
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;
282 xdr_destroy(xdrs);
284 /* Prepare the packet for version 2 PORTMAP */
285 if (pmap_flag) {
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;
302 goto done_broad;
304 t_udata_pmap.opt.len = 0;
305 t_udata_pmap.udata.buf = outbuf_pmap;
306 t_udata_pmap.udata.len = xdr_getpos(xdrs);
307 xdr_destroy(xdrs);
311 * Basic loop: broadcast the packets to transports which
312 * support data packets of size such that one can encode
313 * all the arguments.
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,
324 NC_INET6) == 0) {
325 /* if it's IPv6 */
327 struct netbuf addr;
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,
335 &sa6.sin6_addr);
336 addr.maxlen = sizeof (struct sockaddr_in6);
337 addr.len = addr.maxlen;
338 addr.buf = (char *)&sa6;
340 /* now send rpcbind message */
341 t_udata.addr = addr;
344 if (t_sndudata(fdlist[i].fd,
345 &t_udata)) {
346 (void) syslog(LOG_ERR,
347 "Cannot send broadcast\
348 packet: %m");
349 #ifdef RPC_DEBUG
350 t_error("rpc_broadcast: t_sndudata");
351 #endif
352 stat = RPC_CANTSEND;
353 continue;
356 } else {
358 struct nd_addrlist *addrlist;
360 if (fdlist[i].dsize < t_udata.udata.len) {
361 stat = RPC_CANTSEND;
362 continue;
364 if (netdir_getbyname(fdlist[i].nconf, &hs,
365 &addrlist) || (addrlist->n_cnt == 0)) {
366 stat = RPC_N2AXLATEFAILURE;
367 continue;
370 for (j = 0; j < addrlist->n_cnt; j++) {
371 #ifdef RPC_DEBUG
372 struct netconfig *nconf =
373 fdlist[i].nconf;
374 #endif
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,
385 &t_udata)) {
386 (void) syslog(LOG_ERR,
387 "Cannot send broadcast\
388 packet: %m");
389 #ifdef RPC_DEBUG
390 t_error("rpc_broadcast: t_sndudata");
391 #endif
392 stat = RPC_CANTSEND;
393 continue;
395 #ifdef RPC_DEBUG
396 if (!lowvers || !fdlist[i].udp_flag)
397 fprintf(stderr, "Broadcast\
398 packet sent for %s\n", nconf->nc_netid);
399 #endif
401 * Send the version 2 packet also
402 * for UDP/IP
404 if (fdlist[i].udp_flag) {
405 t_udata_pmap.addr =
406 t_udata.addr;
407 if (t_sndudata(fdlist[i].fd,
408 &t_udata_pmap)) {
409 (void) syslog(LOG_ERR,\
410 "Cannot send broadcast packet: %m");
411 #ifdef RPC_DEBUG
412 t_error("rpc_broadcast:\
413 t_sndudata");
414 #endif
415 stat = RPC_CANTSEND;
416 continue;
419 #ifdef RPC_DEBUG
420 fprintf(stderr, "PMAP Broadcast packet\
421 sent for %s\n", nconf->nc_netid);
422 #endif
424 /* End for sending all packets on this transport */
425 (void) netdir_free((char *)addrlist, ND_ADDRLIST);
426 } /* end non-IPv6 */
428 } /* End for sending on all transports */
430 if (eachresult == NULL) {
431 stat = RPC_SUCCESS;
432 goto done_broad;
436 * Get all the replies from these broadcast requests
438 recv_again:
440 switch (pollretval = poll(pfd, fdlistno, msec)) {
441 case 0: /* timed out */
442 stat = RPC_TIMEDOUT;
443 continue;
444 case -1: /* some kind of error - we ignore it */
445 goto recv_again;
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++) {
453 int flag;
454 bool_t done = FALSE;
456 if (pfd[i].revents == 0)
457 continue;
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[]
464 * arrays.
466 pfd[i].fd = -1;
467 fds_found++;
468 continue;
469 } else
470 fds_found++;
471 #ifdef RPC_DEBUG
472 fprintf(stderr, "response for %s\n",
473 fdlist[i].nconf->nc_netid);
474 #endif
475 try_again:
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)
481 goto try_again;
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
488 * implementations.
490 if (t_errno == TLOOK &&
491 t_look(fdlist[i].fd) == T_UDERR &&
492 t_rcvuderr(fdlist[i].fd, NULL) == 0)
493 goto recv_again;
495 (void) syslog(LOG_ERR,
496 "Cannot receive reply to \
497 broadcast: %m");
498 stat = RPC_CANTRECV;
499 continue;
502 * Not taking care of flag for T_MORE.
503 * We are assuming that
504 * such calls should not take more than one
505 * transport packet.
507 if (flag & T_MORE)
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))) {
518 pmap_reply_flag = 0;
519 msg.acpted_rply.ar_verf = _null_auth;
520 msg.acpted_rply.ar_results.where =
521 (caddr_t)&bres;
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))) {
529 pmap_reply_flag = 1;
530 msg.acpted_rply.ar_verf = _null_auth;
531 msg.acpted_rply.ar_results.where =
532 (caddr_t)&bres_pmap;
533 msg.acpted_rply.ar_results.proc =
534 (xdrproc_t)xdr_rmtcallres;
535 } else
536 continue;
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 */
551 taddr = uaddr2taddr(
552 fdlist[i].nconf,
553 uaddrp);
554 done = (*eachresult)(resultsp, taddr,
555 fdlist[i].nconf);
556 #ifdef RPC_DEBUG
558 int k;
560 printf("rmt addr = ");
561 for (k = 0; k < taddr->len; k++)
562 printf("%d ", taddr->buf[k]);
563 printf("\n");
565 #endif
566 if (taddr && !pmap_reply_flag)
567 netdir_free((char *)taddr,
568 ND_ADDR);
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);
578 XDR_DESTROY(xdrs);
579 if (done) {
580 stat = RPC_SUCCESS;
581 goto done_broad;
582 } else {
583 if (rpc_callerr.re_status == RPC_SYSTEMERROR) {
584 stat = RPC_SYSTEMERROR;
585 goto done_broad;
587 goto recv_again;
589 } /* The recv for loop */
590 } /* The giant for loop */
592 done_broad:
593 free(inbuf);
594 free(outbuf);
595 free(outbuf_pmap);
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);
603 return (stat);
606 enum clnt_stat
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));