1/*
2 * clnt_udp.c, Implements a UDP/IP based, client side RPC.
3 *
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <stdio.h>
35#include <unistd.h>
36#include <libintl.h>
37#include <rpc/rpc.h>
38#include <rpc/xdr.h>
39#include <rpc/clnt.h>
40#include <sys/poll.h>
41#include <sys/socket.h>
42#include <sys/ioctl.h>
43#include <netdb.h>
44#include <errno.h>
45#include <stdint.h>
46#include <rpc/pmap_clnt.h>
47#include <net/if.h>
48#include <ifaddrs.h>
49#include <wchar.h>
50#include <fcntl.h>
51
52#ifdef IP_RECVERR
53#include <errqueue.h>
54#include <sys/uio.h>
55#endif
56
57#include <kernel-features.h>
58#include <inet/net-internal.h>
59
60extern u_long _create_xid (void);
61
62/*
63 * UDP bases client side rpc operations
64 */
65static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
66 xdrproc_t, caddr_t, struct timeval);
67static void clntudp_abort (void);
68static void clntudp_geterr (CLIENT *, struct rpc_err *);
69static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
70static bool_t clntudp_control (CLIENT *, int, char *);
71static void clntudp_destroy (CLIENT *);
72
73static const struct clnt_ops udp_ops =
74{
75 clntudp_call,
76 clntudp_abort,
77 clntudp_geterr,
78 clntudp_freeres,
79 clntudp_destroy,
80 clntudp_control
81};
82
83/*
84 * Private data kept per client handle. This private struct is
85 * unfortunately part of the ABI; ypbind contains a copy of it and
86 * accesses it through CLIENT::cl_private field.
87 */
88struct cu_data
89 {
90 int cu_sock;
91 bool_t cu_closeit;
92 struct sockaddr_in cu_raddr;
93 int cu_rlen;
94 struct timeval cu_wait;
95 struct timeval cu_total;
96 struct rpc_err cu_error;
97 XDR cu_outxdrs;
98 u_int cu_xdrpos;
99 u_int cu_sendsz;
100 char *cu_outbuf;
101 u_int cu_recvsz;
102 char cu_inbuf[1];
103 };
104
105/*
106 * Create a UDP based client handle.
107 * If *sockp<0, *sockp is set to a newly created UPD socket.
108 * If raddr->sin_port is 0 a binder on the remote machine
109 * is consulted for the correct port number.
110 * NB: It is the clients responsibility to close *sockp.
111 * NB: The rpch->cl_auth is initialized to null authentication.
112 * Caller may wish to set this something more useful.
113 *
114 * wait is the amount of time used between retransmitting a call if
115 * no response has been heard; retransmission occurs until the actual
116 * rpc call times out.
117 *
118 * sendsz and recvsz are the maximum allowable packet sizes that can be
119 * sent and received.
120 */
121CLIENT *
122__libc_clntudp_bufcreate (struct sockaddr_in *raddr, u_long program,
123 u_long version, struct timeval wait, int *sockp,
124 u_int sendsz, u_int recvsz, int flags)
125{
126 CLIENT *cl;
127 struct cu_data *cu = NULL;
128 struct rpc_msg call_msg;
129
130 cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
131 sendsz = ((sendsz + 3) / 4) * 4;
132 recvsz = ((recvsz + 3) / 4) * 4;
133 cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
134 if (cl == NULL || cu == NULL)
135 {
136 struct rpc_createerr *ce = &get_rpc_createerr ();
137 (void) __fxprintf (NULL, "%s: %s",
138 "clntudp_create", _("out of memory\n"));
139 ce->cf_stat = RPC_SYSTEMERROR;
140 ce->cf_error.re_errno = ENOMEM;
141 goto fooy;
142 }
143 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
144
145 if (raddr->sin_port == 0)
146 {
147 u_short port;
148 if ((port =
149 pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
150 {
151 goto fooy;
152 }
153 raddr->sin_port = htons (port);
154 }
155 cl->cl_ops = (struct clnt_ops *) &udp_ops;
156 cl->cl_private = (caddr_t) cu;
157 cu->cu_raddr = *raddr;
158 cu->cu_rlen = sizeof (cu->cu_raddr);
159 cu->cu_wait = wait;
160 cu->cu_total.tv_sec = -1;
161 cu->cu_total.tv_usec = -1;
162 cu->cu_sendsz = sendsz;
163 cu->cu_recvsz = recvsz;
164 call_msg.rm_xid = _create_xid ();
165 call_msg.rm_direction = CALL;
166 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
167 call_msg.rm_call.cb_prog = program;
168 call_msg.rm_call.cb_vers = version;
169 xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
170 if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg))
171 {
172 goto fooy;
173 }
174 cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
175 if (*sockp < 0)
176 {
177 *sockp = __socket (AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|flags, IPPROTO_UDP);
178 if (__glibc_unlikely (*sockp < 0))
179 {
180 struct rpc_createerr *ce = &get_rpc_createerr ();
181 ce->cf_stat = RPC_SYSTEMERROR;
182 ce->cf_error.re_errno = errno;
183 goto fooy;
184 }
185 /* attempt to bind to prov port */
186 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
187#ifdef IP_RECVERR
188 {
189 int on = 1;
190 __setsockopt (*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
191 }
192#endif
193 cu->cu_closeit = TRUE;
194 }
195 else
196 {
197 cu->cu_closeit = FALSE;
198 }
199 cu->cu_sock = *sockp;
200 cl->cl_auth = authnone_create ();
201 return cl;
202fooy:
203 if (cu)
204 mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
205 if (cl)
206 mem_free ((caddr_t) cl, sizeof (CLIENT));
207 return (CLIENT *) NULL;
208}
209#ifdef EXPORT_RPC_SYMBOLS
210libc_hidden_def (__libc_clntudp_bufcreate)
211#else
212libc_hidden_nolink_sunrpc (__libc_clntudp_bufcreate, GLIBC_PRIVATE)
213#endif
214
215CLIENT *
216clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
217 struct timeval wait, int *sockp, u_int sendsz,
218 u_int recvsz)
219{
220 return __libc_clntudp_bufcreate (raddr, program, version, wait,
221 sockp, sendsz, recvsz, 0);
222}
223libc_hidden_nolink_sunrpc (clntudp_bufcreate, GLIBC_2_0)
224
225CLIENT *
226clntudp_create (struct sockaddr_in *raddr, u_long program, u_long version,
227 struct timeval wait, int *sockp)
228{
229 return __libc_clntudp_bufcreate (raddr, program, version, wait,
230 sockp, UDPMSGSIZE, UDPMSGSIZE, 0);
231}
232#ifdef EXPORT_RPC_SYMBOLS
233libc_hidden_def (clntudp_create)
234#else
235libc_hidden_nolink_sunrpc (clntudp_create, GLIBC_2_0)
236#endif
237
238static int
239is_network_up (int sock)
240{
241 struct ifaddrs *ifa;
242
243 if (getifaddrs (&ifa) != 0)
244 return 0;
245
246 struct ifaddrs *run = ifa;
247 while (run != NULL)
248 {
249 if ((run->ifa_flags & IFF_UP) != 0
250 && run->ifa_addr != NULL
251 && run->ifa_addr->sa_family == AF_INET)
252 break;
253
254 run = run->ifa_next;
255 }
256
257 freeifaddrs (ifa);
258
259 return run != NULL;
260}
261
262static enum clnt_stat
263clntudp_call (/* client handle */
264 CLIENT *cl,
265 /* procedure number */
266 u_long proc,
267 /* xdr routine for args */
268 xdrproc_t xargs,
269 /* pointer to args */
270 caddr_t argsp,
271 /* xdr routine for results */
272 xdrproc_t xresults,
273 /* pointer to results */
274 caddr_t resultsp,
275 /* seconds to wait before giving up */
276 struct timeval utimeout)
277{
278 struct cu_data *cu = (struct cu_data *) cl->cl_private;
279 XDR *xdrs;
280 int outlen = 0;
281 int inlen;
282 socklen_t fromlen;
283 struct pollfd fd;
284 struct sockaddr_in from;
285 struct rpc_msg reply_msg;
286 XDR reply_xdrs;
287 bool_t ok;
288 int nrefreshes = 2; /* number of times to refresh cred */
289 int anyup; /* any network interface up */
290
291 struct deadline_current_time current_time = __deadline_current_time ();
292 struct deadline total_deadline; /* Determined once by overall timeout. */
293 struct deadline response_deadline; /* Determined anew for each query. */
294
295 /* Choose the timeout value. For non-sending usage (xargs == NULL),
296 the total deadline does not matter, only cu->cu_wait is used
297 below. */
298 if (xargs != NULL)
299 {
300 struct timeval tv;
301 if (cu->cu_total.tv_usec == -1)
302 /* Use supplied timeout. */
303 tv = utimeout;
304 else
305 /* Use default timeout. */
306 tv = cu->cu_total;
307 if (!__is_timeval_valid_timeout (tv))
308 return (cu->cu_error.re_status = RPC_TIMEDOUT);
309 total_deadline = __deadline_from_timeval (current_time, tv);
310 }
311
312 /* Guard against bad timeout specification. */
313 if (!__is_timeval_valid_timeout (cu->cu_wait))
314 return (cu->cu_error.re_status = RPC_TIMEDOUT);
315
316call_again:
317 xdrs = &(cu->cu_outxdrs);
318 if (xargs == NULL)
319 goto get_reply;
320 xdrs->x_op = XDR_ENCODE;
321 XDR_SETPOS (xdrs, cu->cu_xdrpos);
322 /*
323 * the transaction is the first thing in the out buffer
324 */
325 (*(uint32_t *) (cu->cu_outbuf))++;
326 if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
327 (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
328 (!(*xargs) (xdrs, argsp)))
329 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
330 outlen = (int) XDR_GETPOS (xdrs);
331
332send_again:
333 if (__sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
334 (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
335 != outlen)
336 {
337 cu->cu_error.re_errno = errno;
338 return (cu->cu_error.re_status = RPC_CANTSEND);
339 }
340
341 /* sendto may have blocked, so recompute the current time. */
342 current_time = __deadline_current_time ();
343 get_reply:
344 response_deadline = __deadline_from_timeval (current_time, cu->cu_wait);
345
346 reply_msg.acpted_rply.ar_verf = _null_auth;
347 reply_msg.acpted_rply.ar_results.where = resultsp;
348 reply_msg.acpted_rply.ar_results.proc = xresults;
349 fd.fd = cu->cu_sock;
350 fd.events = POLLIN;
351 anyup = 0;
352
353 /* Per-response retry loop. current_time must be up-to-date at the
354 top of the loop. */
355 for (;;)
356 {
357 int milliseconds;
358 if (xargs != NULL)
359 {
360 if (__deadline_elapsed (current_time, total_deadline))
361 /* Overall timeout expired. */
362 return (cu->cu_error.re_status = RPC_TIMEDOUT);
363 milliseconds = __deadline_to_ms
364 (current_time, __deadline_first (total_deadline,
365 response_deadline));
366 if (milliseconds == 0)
367 /* Per-query timeout expired. */
368 goto send_again;
369 }
370 else
371 {
372 /* xatgs == NULL. Collect a response without sending a
373 query. In this mode, we need to ignore the total
374 deadline. */
375 milliseconds = __deadline_to_ms (current_time, response_deadline);
376 if (milliseconds == 0)
377 /* Cannot send again, so bail out. */
378 return (cu->cu_error.re_status = RPC_CANTSEND);
379 }
380
381 switch (__poll (&fd, 1, milliseconds))
382 {
383
384 case 0:
385 if (anyup == 0)
386 {
387 anyup = is_network_up (cu->cu_sock);
388 if (!anyup)
389 return (cu->cu_error.re_status = RPC_CANTRECV);
390 }
391 goto next_response;
392 case -1:
393 if (errno == EINTR)
394 goto next_response;
395 cu->cu_error.re_errno = errno;
396 return (cu->cu_error.re_status = RPC_CANTRECV);
397 }
398#ifdef IP_RECVERR
399 if (fd.revents & POLLERR)
400 {
401 struct msghdr msg;
402 struct cmsghdr *cmsg;
403 struct sock_extended_err *e;
404 struct sockaddr_in err_addr;
405 struct iovec iov;
406 char *cbuf = malloc (outlen + 256);
407 int ret;
408
409 if (cbuf == NULL)
410 {
411 cu->cu_error.re_errno = errno;
412 return (cu->cu_error.re_status = RPC_CANTRECV);
413 }
414
415 iov.iov_base = cbuf + 256;
416 iov.iov_len = outlen;
417 msg.msg_name = (void *) &err_addr;
418 msg.msg_namelen = sizeof (err_addr);
419 msg.msg_iov = &iov;
420 msg.msg_iovlen = 1;
421 msg.msg_flags = 0;
422 msg.msg_control = cbuf;
423 msg.msg_controllen = 256;
424 ret = __recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
425 if (ret >= 0
426 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
427 && (msg.msg_flags & MSG_ERRQUEUE)
428 && ((msg.msg_namelen == 0
429 && ret >= 12)
430 || (msg.msg_namelen == sizeof (err_addr)
431 && err_addr.sin_family == AF_INET
432 && memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
433 sizeof (err_addr.sin_addr)) == 0
434 && err_addr.sin_port == cu->cu_raddr.sin_port)))
435 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
436 cmsg = CMSG_NXTHDR (&msg, cmsg))
437 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
438 {
439 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
440 cu->cu_error.re_errno = e->ee_errno;
441 free (cbuf);
442 return (cu->cu_error.re_status = RPC_CANTRECV);
443 }
444 free (cbuf);
445 }
446#endif
447 do
448 {
449 fromlen = sizeof (struct sockaddr);
450 inlen = __recvfrom (cu->cu_sock, cu->cu_inbuf,
451 (int) cu->cu_recvsz, MSG_DONTWAIT,
452 (struct sockaddr *) &from, &fromlen);
453 }
454 while (inlen < 0 && errno == EINTR);
455 if (inlen < 0)
456 {
457 if (errno == EWOULDBLOCK)
458 goto next_response;
459 cu->cu_error.re_errno = errno;
460 return (cu->cu_error.re_status = RPC_CANTRECV);
461 }
462 /* Accept the response if the packet is sufficiently long and
463 the transaction ID matches the query (if available). */
464 if (inlen >= 4
465 && (xargs == NULL
466 || memcmp (cu->cu_inbuf, cu->cu_outbuf,
467 sizeof (u_int32_t)) == 0))
468 break;
469
470 next_response:
471 /* Update the current time because poll and recvmsg waited for
472 an unknown time. */
473 current_time = __deadline_current_time ();
474 }
475
476 /*
477 * now decode and validate the response
478 */
479 xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
480 ok = xdr_replymsg (&reply_xdrs, &reply_msg);
481 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
482 if (ok)
483 {
484 _seterr_reply (&reply_msg, &(cu->cu_error));
485 if (cu->cu_error.re_status == RPC_SUCCESS)
486 {
487 if (!AUTH_VALIDATE (cl->cl_auth,
488 &reply_msg.acpted_rply.ar_verf))
489 {
490 cu->cu_error.re_status = RPC_AUTHERROR;
491 cu->cu_error.re_why = AUTH_INVALIDRESP;
492 }
493 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
494 {
495 xdrs->x_op = XDR_FREE;
496 (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
497 }
498 } /* end successful completion */
499 else
500 {
501 /* maybe our credentials need to be refreshed ... */
502 if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
503 {
504 nrefreshes--;
505 goto call_again;
506 }
507 } /* end of unsuccessful completion */
508 } /* end of valid reply message */
509 else
510 {
511 cu->cu_error.re_status = RPC_CANTDECODERES;
512 }
513 return cu->cu_error.re_status;
514}
515
516static void
517clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
518{
519 struct cu_data *cu = (struct cu_data *) cl->cl_private;
520
521 *errp = cu->cu_error;
522}
523
524
525static bool_t
526clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
527{
528 struct cu_data *cu = (struct cu_data *) cl->cl_private;
529 XDR *xdrs = &(cu->cu_outxdrs);
530
531 xdrs->x_op = XDR_FREE;
532 return (*xdr_res) (xdrs, res_ptr);
533}
534
535static void
536clntudp_abort (void)
537{
538}
539
540static bool_t
541clntudp_control (CLIENT *cl, int request, char *info)
542{
543 struct cu_data *cu = (struct cu_data *) cl->cl_private;
544 u_long ul;
545 u_int32_t ui32;
546
547 switch (request)
548 {
549 case CLSET_FD_CLOSE:
550 cu->cu_closeit = TRUE;
551 break;
552 case CLSET_FD_NCLOSE:
553 cu->cu_closeit = FALSE;
554 break;
555 case CLSET_TIMEOUT:
556 cu->cu_total = *(struct timeval *) info;
557 break;
558 case CLGET_TIMEOUT:
559 *(struct timeval *) info = cu->cu_total;
560 break;
561 case CLSET_RETRY_TIMEOUT:
562 cu->cu_wait = *(struct timeval *) info;
563 break;
564 case CLGET_RETRY_TIMEOUT:
565 *(struct timeval *) info = cu->cu_wait;
566 break;
567 case CLGET_SERVER_ADDR:
568 *(struct sockaddr_in *) info = cu->cu_raddr;
569 break;
570 case CLGET_FD:
571 *(int *)info = cu->cu_sock;
572 break;
573 case CLGET_XID:
574 /*
575 * use the knowledge that xid is the
576 * first element in the call structure *.
577 * This will get the xid of the PREVIOUS call
578 */
579 memcpy (&ui32, cu->cu_outbuf, sizeof (ui32));
580 ul = ntohl (ui32);
581 memcpy (info, &ul, sizeof (ul));
582 break;
583 case CLSET_XID:
584 /* This will set the xid of the NEXT call */
585 memcpy (&ul, info, sizeof (ul));
586 ui32 = htonl (ul - 1);
587 memcpy (cu->cu_outbuf, &ui32, sizeof (ui32));
588 /* decrement by 1 as clntudp_call() increments once */
589 break;
590 case CLGET_VERS:
591 /*
592 * This RELIES on the information that, in the call body,
593 * the version number field is the fifth field from the
594 * beginning of the RPC header. MUST be changed if the
595 * call_struct is changed
596 */
597 memcpy (&ui32, cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT, sizeof (ui32));
598 ul = ntohl (ui32);
599 memcpy (info, &ul, sizeof (ul));
600 break;
601 case CLSET_VERS:
602 memcpy (&ul, info, sizeof (ul));
603 ui32 = htonl (ul);
604 memcpy (cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
605 break;
606 case CLGET_PROG:
607 /*
608 * This RELIES on the information that, in the call body,
609 * the program number field is the field from the
610 * beginning of the RPC header. MUST be changed if the
611 * call_struct is changed
612 */
613 memcpy (&ui32, cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT, sizeof (ui32));
614 ul = ntohl (ui32);
615 memcpy (info, &ul, sizeof (ul));
616 break;
617 case CLSET_PROG:
618 memcpy (&ul, info, sizeof (ul));
619 ui32 = htonl (ul);
620 memcpy (cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
621 break;
622 /* The following are only possible with TI-RPC */
623 case CLGET_SVC_ADDR:
624 case CLSET_SVC_ADDR:
625 case CLSET_PUSH_TIMOD:
626 case CLSET_POP_TIMOD:
627 default:
628 return FALSE;
629 }
630 return TRUE;
631}
632
633static void
634clntudp_destroy (CLIENT *cl)
635{
636 struct cu_data *cu = (struct cu_data *) cl->cl_private;
637
638 if (cu->cu_closeit)
639 {
640 (void) __close (cu->cu_sock);
641 }
642 XDR_DESTROY (&(cu->cu_outxdrs));
643 mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
644 mem_free ((caddr_t) cl, sizeof (CLIENT));
645}
646