3 * Copyright (c) 2009, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
33 * svc_vc.c, Server side for Connection Oriented based RPC.
35 * Actually implements two flavors of transporter -
36 * a tcp rendezvouser (a listner and connection establisher)
37 * and a record/tcp stream.
40 #include <reentrant.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
43 #include <sys/param.h>
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
64 #include <getpeereid.h>
67 extern rwlock_t svc_fd_lock;
69 static SVCXPRT *makefd_xprt(int, u_int, u_int);
70 static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
71 static enum xprt_stat rendezvous_stat(SVCXPRT *);
72 static void svc_vc_destroy(SVCXPRT *);
73 static void __svc_vc_dodestroy (SVCXPRT *);
74 static int read_vc(void *, void *, int);
75 static int write_vc(void *, void *, int);
76 static enum xprt_stat svc_vc_stat(SVCXPRT *);
77 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
78 static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
79 static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
80 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
81 static void svc_vc_rendezvous_ops(SVCXPRT *);
82 static void svc_vc_ops(SVCXPRT *);
83 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
84 static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
87 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
93 struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
94 enum xprt_stat strm_stat;
97 char verf_body[MAX_AUTH_BYTES];
102 struct timeval last_recv_time;
106 * This is used to set xprt->xp_raddr in a way legacy
110 __xprt_set_raddr(SVCXPRT *xprt, const struct sockaddr_storage *ss)
112 switch (ss->ss_family) {
114 memcpy(&xprt->xp_raddr, ss, sizeof(struct sockaddr_in6));
115 xprt->xp_addrlen = sizeof (struct sockaddr_in6);
118 memcpy(&xprt->xp_raddr, ss, sizeof(struct sockaddr_in));
119 xprt->xp_addrlen = sizeof (struct sockaddr_in);
122 xprt->xp_raddr.sin6_family = AF_UNSPEC;
123 xprt->xp_addrlen = sizeof (struct sockaddr);
130 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
132 * Creates, registers, and returns a (rpc) tcp based transporter.
133 * Once *xprt is initialized, it is registered as a transporter
134 * see (svc.h, xprt_register). This routine returns
135 * a NULL if a problem occurred.
137 * The filedescriptor passed in is expected to refer to a bound, but
138 * not yet connected socket.
140 * Since streams do buffered io similar to stdio, the caller can specify
141 * how big the send and receive buffers are via the second and third parms;
142 * 0 => use the system default.
145 svc_vc_create(fd, sendsize, recvsize)
151 struct cf_rendezvous *r = NULL;
152 struct __rpc_sockinfo si;
153 struct sockaddr_storage sslocal;
156 r = mem_alloc(sizeof(*r));
158 warnx("svc_vc_create: out of memory");
159 goto cleanup_svc_vc_create;
161 if (!__rpc_fd2sockinfo(fd, &si))
163 r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
164 r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
165 r->maxrec = __svc_maxrec;
166 xprt = mem_alloc(sizeof(SVCXPRT));
168 warnx("svc_vc_create: out of memory");
169 goto cleanup_svc_vc_create;
175 xprt->xp_verf = _null_auth;
176 svc_vc_rendezvous_ops(xprt);
177 xprt->xp_port = (u_short)-1; /* It is the rendezvouser */
180 slen = sizeof (struct sockaddr_storage);
181 if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
182 warnx("svc_vc_create: could not retrieve local addr");
183 goto cleanup_svc_vc_create;
186 if (!__rpc_set_netbuf(&xprt->xp_ltaddr, &sslocal, sizeof(sslocal))) {
187 warnx("svc_vc_create: no mem for local addr");
188 goto cleanup_svc_vc_create;
192 cleanup_svc_vc_create:
194 mem_free(r, sizeof(*r));
199 * Like svtcp_create(), except the routine takes any *open* UNIX file
200 * descriptor as its first input.
203 svc_fd_create(fd, sendsize, recvsize)
208 struct sockaddr_storage ss;
214 ret = makefd_xprt(fd, sendsize, recvsize);
218 slen = sizeof (struct sockaddr_storage);
219 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
220 warnx("svc_fd_create: could not retrieve local addr");
223 if (!__rpc_set_netbuf(&ret->xp_ltaddr, &ss, sizeof(ss))) {
224 warnx("svc_fd_create: no mem for local addr");
228 slen = sizeof (struct sockaddr_storage);
229 if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
230 warnx("svc_fd_create: could not retrieve remote addr");
233 if (!__rpc_set_netbuf(&ret->xp_rtaddr, &ss, sizeof(ss))) {
234 warnx("svc_fd_create: no mem for local addr");
238 /* Set xp_raddr for compatibility */
239 __xprt_set_raddr(ret, &ss);
244 if (ret->xp_ltaddr.buf != NULL)
245 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
251 makefd_xprt(fd, sendsize, recvsize)
259 struct __rpc_sockinfo si;
263 if (fd >= FD_SETSIZE) {
264 warnx("svc_vc: makefd_xprt: fd too high\n");
269 xprt = mem_alloc(sizeof(SVCXPRT));
271 warnx("svc_vc: makefd_xprt: out of memory");
274 memset(xprt, 0, sizeof *xprt);
275 cd = mem_alloc(sizeof(struct cf_conn));
277 warnx("svc_tcp: makefd_xprt: out of memory");
278 mem_free(xprt, sizeof(SVCXPRT));
282 cd->strm_stat = XPRT_IDLE;
283 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
284 xprt, read_vc, write_vc);
286 xprt->xp_verf.oa_base = cd->verf_body;
287 svc_vc_ops(xprt); /* truely deals with calls */
288 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
290 if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
291 xprt->xp_netid = strdup(netid);
300 rendezvous_request(xprt, msg)
305 struct cf_rendezvous *r;
307 struct sockaddr_storage addr;
309 struct __rpc_sockinfo si;
313 assert(xprt != NULL);
316 r = (struct cf_rendezvous *)xprt->xp_p1;
319 if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
324 * Clean out the most idle file descriptor when we're
327 if (errno == EMFILE || errno == ENFILE) {
328 cleanfds = svc_fdset;
329 __svc_clean_idle(&cleanfds, 0, FALSE);
335 * make a new transporter (re-uses xprt)
338 newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
340 if (!__rpc_set_netbuf(&newxprt->xp_rtaddr, &addr, len))
343 __xprt_set_raddr(newxprt, &addr);
345 if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
347 /* XXX fvdl - is this useful? */
348 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
351 cd = (struct cf_conn *)newxprt->xp_p1;
353 cd->recvsize = r->recvsize;
354 cd->sendsize = r->sendsize;
355 cd->maxrec = r->maxrec;
357 if (cd->maxrec != 0) {
358 flags = fcntl(sock, F_GETFL, 0);
361 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
363 if (cd->recvsize > cd->maxrec)
364 cd->recvsize = cd->maxrec;
366 __xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
368 cd->nonblock = FALSE;
370 gettimeofday(&cd->last_recv_time, NULL);
372 return (FALSE); /* there is never an rpc msg to be processed */
376 static enum xprt_stat
377 rendezvous_stat(xprt)
388 assert(xprt != NULL);
390 xprt_unregister(xprt);
391 __svc_vc_dodestroy(xprt);
395 __svc_vc_dodestroy(xprt)
399 struct cf_rendezvous *r;
401 cd = (struct cf_conn *)xprt->xp_p1;
403 if (xprt->xp_fd != RPC_ANYFD)
404 (void)close(xprt->xp_fd);
405 if (xprt->xp_port != 0) {
406 /* a rendezvouser socket */
407 r = (struct cf_rendezvous *)xprt->xp_p1;
408 mem_free(r, sizeof (struct cf_rendezvous));
411 /* an actual connection socket */
412 XDR_DESTROY(&(cd->xdrs));
413 mem_free(cd, sizeof(struct cf_conn));
415 if (xprt->xp_rtaddr.buf)
416 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
417 if (xprt->xp_ltaddr.buf)
418 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
422 free(xprt->xp_netid);
423 mem_free(xprt, sizeof(SVCXPRT));
428 svc_vc_control(xprt, rq, in)
437 svc_vc_rendezvous_control(xprt, rq, in)
442 struct cf_rendezvous *cfp;
444 cfp = (struct cf_rendezvous *)xprt->xp_p1;
448 case SVCGET_CONNMAXREC:
449 *(int *)in = cfp->maxrec;
451 case SVCSET_CONNMAXREC:
452 cfp->maxrec = *(int *)in;
461 * reads data from the tcp or uip connection.
462 * any error is fatal and the connection is closed.
463 * (And a read of zero bytes is a half closed stream => error.)
464 * All read operations timeout after 35 seconds. A timeout is
465 * fatal for the connection.
468 read_vc(xprtp, buf, len)
475 int milliseconds = 35 * 1000;
476 struct pollfd pollfd;
479 xprt = (SVCXPRT *)xprtp;
480 assert(xprt != NULL);
484 cfp = (struct cf_conn *)xprt->xp_p1;
487 len = read(sock, buf, (size_t)len);
495 gettimeofday(&cfp->last_recv_time, NULL);
501 pollfd.events = POLLIN;
503 switch (poll(&pollfd, 1, milliseconds)) {
514 } while ((pollfd.revents & POLLIN) == 0);
516 if ((len = read(sock, buf, (size_t)len)) > 0) {
517 gettimeofday(&cfp->last_recv_time, NULL);
522 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
527 * writes data to the tcp connection.
528 * Any error is fatal and the connection is closed.
531 write_vc(xprtp, buf, len)
539 struct timeval tv0, tv1;
541 xprt = (SVCXPRT *)xprtp;
542 assert(xprt != NULL);
544 cd = (struct cf_conn *)xprt->xp_p1;
547 gettimeofday(&tv0, NULL);
549 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
550 i = write(xprt->xp_fd, buf, (size_t)cnt);
552 if (errno != EAGAIN || !cd->nonblock) {
553 cd->strm_stat = XPRT_DIED;
556 if (cd->nonblock && i != cnt) {
558 * For non-blocking connections, do not
559 * take more than 2 seconds writing the
562 * XXX 2 is an arbitrary amount.
564 gettimeofday(&tv1, NULL);
565 if (tv1.tv_sec - tv0.tv_sec >= 2) {
566 cd->strm_stat = XPRT_DIED;
576 static enum xprt_stat
582 assert(xprt != NULL);
584 cd = (struct cf_conn *)(xprt->xp_p1);
586 if (cd->strm_stat == XPRT_DIED)
588 if (! xdrrec_eof(&(cd->xdrs)))
589 return (XPRT_MOREREQS);
594 svc_vc_recv(xprt, msg)
601 assert(xprt != NULL);
604 cd = (struct cf_conn *)(xprt->xp_p1);
608 if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
612 xdrs->x_op = XDR_DECODE;
613 (void)xdrrec_skiprecord(xdrs);
614 if (xdr_callmsg(xdrs, msg)) {
615 cd->x_id = msg->rm_xid;
618 cd->strm_stat = XPRT_DIED;
623 svc_vc_getargs(xprt, xdr_args, args_ptr)
629 assert(xprt != NULL);
630 /* args_ptr may be NULL */
631 return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
636 svc_vc_freeargs(xprt, xdr_args, args_ptr)
643 assert(xprt != NULL);
644 /* args_ptr may be NULL */
646 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
648 xdrs->x_op = XDR_FREE;
649 return ((*xdr_args)(xdrs, args_ptr));
653 svc_vc_reply(xprt, msg)
661 assert(xprt != NULL);
664 cd = (struct cf_conn *)(xprt->xp_p1);
667 xdrs->x_op = XDR_ENCODE;
668 msg->rm_xid = cd->x_id;
669 rstat = xdr_replymsg(xdrs, msg);
670 (void)xdrrec_endofrecord(xdrs, TRUE);
678 static struct xp_ops ops;
679 static struct xp_ops2 ops2;
680 extern mutex_t ops_lock;
682 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
684 mutex_lock(&ops_lock);
685 if (ops.xp_recv == NULL) {
686 ops.xp_recv = svc_vc_recv;
687 ops.xp_stat = svc_vc_stat;
688 ops.xp_getargs = svc_vc_getargs;
689 ops.xp_reply = svc_vc_reply;
690 ops.xp_freeargs = svc_vc_freeargs;
691 ops.xp_destroy = svc_vc_destroy;
692 ops2.xp_control = svc_vc_control;
695 xprt->xp_ops2 = &ops2;
696 mutex_unlock(&ops_lock);
700 svc_vc_rendezvous_ops(xprt)
703 static struct xp_ops ops;
704 static struct xp_ops2 ops2;
705 extern mutex_t ops_lock;
707 mutex_lock(&ops_lock);
708 if (ops.xp_recv == NULL) {
709 ops.xp_recv = rendezvous_request;
710 ops.xp_stat = rendezvous_stat;
712 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
714 (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
716 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
717 ops.xp_destroy = svc_vc_destroy;
718 ops2.xp_control = svc_vc_rendezvous_control;
721 xprt->xp_ops2 = &ops2;
722 mutex_unlock(&ops_lock);
726 * Get the effective UID of the sending process. Used by rpcbind, keyserv
727 * and rpc.yppasswdd on AF_LOCAL.
730 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
736 sock = transp->xp_fd;
737 sa = (struct sockaddr *)transp->xp_rtaddr.buf;
738 if (sa->sa_family == AF_LOCAL) {
739 ret = getpeereid(sock, &euid, &egid);
748 * Destroy xprts that have not have had any activity in 'timeout' seconds.
749 * If 'cleanblock' is true, blocking connections (the default) are also
750 * cleaned. If timeout is 0, the least active connection is picked.
752 * Though this is not a publicly documented interface, some versions of
753 * rpcbind are known to call this function. Do not alter or remove this
754 * API without changing the library's sonum.
757 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
760 SVCXPRT *xprt, *least_active;
761 struct timeval tv, tdiff, tmax;
764 gettimeofday(&tv, NULL);
765 tmax.tv_sec = tmax.tv_usec = 0;
767 rwlock_wrlock(&svc_fd_lock);
768 for (i = ncleaned = 0; i <= svc_maxfd; i++) {
769 if (FD_ISSET(i, fds)) {
770 xprt = __svc_xports[i];
771 if (xprt == NULL || xprt->xp_ops == NULL ||
772 xprt->xp_ops->xp_recv != svc_vc_recv)
774 cd = (struct cf_conn *)xprt->xp_p1;
775 if (!cleanblock && !cd->nonblock)
778 timersub(&tv, &cd->last_recv_time, &tdiff);
779 if (timercmp(&tdiff, &tmax, >)) {
785 if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
786 __xprt_unregister_unlocked(xprt);
787 __svc_vc_dodestroy(xprt);
792 if (timeout == 0 && least_active != NULL) {
793 __xprt_unregister_unlocked(least_active);
794 __svc_vc_dodestroy(least_active);
797 rwlock_unlock(&svc_fd_lock);
798 return ncleaned > 0 ? TRUE : FALSE;