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_auth = NULL;
176 xprt->xp_verf = _null_auth;
177 svc_vc_rendezvous_ops(xprt);
178 xprt->xp_port = (u_short)-1; /* It is the rendezvouser */
181 slen = sizeof (struct sockaddr_storage);
182 if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
183 warnx("svc_vc_create: could not retrieve local addr");
184 goto cleanup_svc_vc_create;
187 if (!__rpc_set_netbuf(&xprt->xp_ltaddr, &sslocal, sizeof(sslocal))) {
188 warnx("svc_vc_create: no mem for local addr");
189 goto cleanup_svc_vc_create;
193 cleanup_svc_vc_create:
195 mem_free(r, sizeof(*r));
200 * Like svtcp_create(), except the routine takes any *open* UNIX file
201 * descriptor as its first input.
204 svc_fd_create(fd, sendsize, recvsize)
209 struct sockaddr_storage ss;
215 ret = makefd_xprt(fd, sendsize, recvsize);
219 slen = sizeof (struct sockaddr_storage);
220 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
221 warnx("svc_fd_create: could not retrieve local addr");
224 if (!__rpc_set_netbuf(&ret->xp_ltaddr, &ss, sizeof(ss))) {
225 warnx("svc_fd_create: no mem for local addr");
229 slen = sizeof (struct sockaddr_storage);
230 if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
231 warnx("svc_fd_create: could not retrieve remote addr");
234 if (!__rpc_set_netbuf(&ret->xp_rtaddr, &ss, sizeof(ss))) {
235 warnx("svc_fd_create: no mem for local addr");
239 /* Set xp_raddr for compatibility */
240 __xprt_set_raddr(ret, &ss);
245 if (ret->xp_ltaddr.buf != NULL)
246 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
252 makefd_xprt(fd, sendsize, recvsize)
260 struct __rpc_sockinfo si;
264 if (fd >= FD_SETSIZE) {
265 warnx("svc_vc: makefd_xprt: fd too high\n");
270 xprt = mem_alloc(sizeof(SVCXPRT));
272 warnx("svc_vc: makefd_xprt: out of memory");
275 memset(xprt, 0, sizeof *xprt);
276 cd = mem_alloc(sizeof(struct cf_conn));
278 warnx("svc_tcp: makefd_xprt: out of memory");
279 mem_free(xprt, sizeof(SVCXPRT));
283 cd->strm_stat = XPRT_IDLE;
284 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
285 xprt, read_vc, write_vc);
287 xprt->xp_auth = NULL;
288 xprt->xp_verf.oa_base = cd->verf_body;
289 svc_vc_ops(xprt); /* truely deals with calls */
290 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
292 if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
293 xprt->xp_netid = strdup(netid);
302 rendezvous_request(xprt, msg)
307 struct cf_rendezvous *r;
309 struct sockaddr_storage addr;
311 struct __rpc_sockinfo si;
315 assert(xprt != NULL);
318 r = (struct cf_rendezvous *)xprt->xp_p1;
321 if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
326 * Clean out the most idle file descriptor when we're
329 if (errno == EMFILE || errno == ENFILE) {
330 cleanfds = svc_fdset;
331 __svc_clean_idle(&cleanfds, 0, FALSE);
337 * make a new transporter (re-uses xprt)
340 newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
342 if (!__rpc_set_netbuf(&newxprt->xp_rtaddr, &addr, len))
345 __xprt_set_raddr(newxprt, &addr);
347 if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
349 /* XXX fvdl - is this useful? */
350 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
353 cd = (struct cf_conn *)newxprt->xp_p1;
355 cd->recvsize = r->recvsize;
356 cd->sendsize = r->sendsize;
357 cd->maxrec = r->maxrec;
359 if (cd->maxrec != 0) {
360 flags = fcntl(sock, F_GETFL, 0);
363 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
365 if (cd->recvsize > cd->maxrec)
366 cd->recvsize = cd->maxrec;
368 __xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
370 cd->nonblock = FALSE;
372 gettimeofday(&cd->last_recv_time, NULL);
374 return (FALSE); /* there is never an rpc msg to be processed */
378 static enum xprt_stat
379 rendezvous_stat(xprt)
390 assert(xprt != NULL);
392 xprt_unregister(xprt);
393 __svc_vc_dodestroy(xprt);
397 __svc_vc_dodestroy(xprt)
401 struct cf_rendezvous *r;
403 cd = (struct cf_conn *)xprt->xp_p1;
405 if (xprt->xp_fd != RPC_ANYFD)
406 (void)close(xprt->xp_fd);
407 if (xprt->xp_port != 0) {
408 /* a rendezvouser socket */
409 r = (struct cf_rendezvous *)xprt->xp_p1;
410 mem_free(r, sizeof (struct cf_rendezvous));
413 /* an actual connection socket */
414 XDR_DESTROY(&(cd->xdrs));
415 mem_free(cd, sizeof(struct cf_conn));
417 if (xprt->xp_auth != NULL) {
418 SVCAUTH_DESTROY(xprt->xp_auth);
419 xprt->xp_auth = NULL;
421 if (xprt->xp_rtaddr.buf)
422 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
423 if (xprt->xp_ltaddr.buf)
424 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
428 free(xprt->xp_netid);
429 mem_free(xprt, sizeof(SVCXPRT));
434 svc_vc_control(xprt, rq, in)
443 svc_vc_rendezvous_control(xprt, rq, in)
448 struct cf_rendezvous *cfp;
450 cfp = (struct cf_rendezvous *)xprt->xp_p1;
454 case SVCGET_CONNMAXREC:
455 *(int *)in = cfp->maxrec;
457 case SVCSET_CONNMAXREC:
458 cfp->maxrec = *(int *)in;
467 * reads data from the tcp or uip connection.
468 * any error is fatal and the connection is closed.
469 * (And a read of zero bytes is a half closed stream => error.)
470 * All read operations timeout after 35 seconds. A timeout is
471 * fatal for the connection.
474 read_vc(xprtp, buf, len)
481 int milliseconds = 35 * 1000;
482 struct pollfd pollfd;
485 xprt = (SVCXPRT *)xprtp;
486 assert(xprt != NULL);
490 cfp = (struct cf_conn *)xprt->xp_p1;
493 len = read(sock, buf, (size_t)len);
501 gettimeofday(&cfp->last_recv_time, NULL);
507 pollfd.events = POLLIN;
509 switch (poll(&pollfd, 1, milliseconds)) {
520 } while ((pollfd.revents & POLLIN) == 0);
522 if ((len = read(sock, buf, (size_t)len)) > 0) {
523 gettimeofday(&cfp->last_recv_time, NULL);
528 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
533 * writes data to the tcp connection.
534 * Any error is fatal and the connection is closed.
537 write_vc(xprtp, buf, len)
545 struct timeval tv0, tv1;
547 xprt = (SVCXPRT *)xprtp;
548 assert(xprt != NULL);
550 cd = (struct cf_conn *)xprt->xp_p1;
553 gettimeofday(&tv0, NULL);
555 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
556 i = write(xprt->xp_fd, buf, (size_t)cnt);
558 if (errno != EAGAIN || !cd->nonblock) {
559 cd->strm_stat = XPRT_DIED;
562 if (cd->nonblock && i != cnt) {
564 * For non-blocking connections, do not
565 * take more than 2 seconds writing the
568 * XXX 2 is an arbitrary amount.
570 gettimeofday(&tv1, NULL);
571 if (tv1.tv_sec - tv0.tv_sec >= 2) {
572 cd->strm_stat = XPRT_DIED;
582 static enum xprt_stat
588 assert(xprt != NULL);
590 cd = (struct cf_conn *)(xprt->xp_p1);
592 if (cd->strm_stat == XPRT_DIED)
594 if (! xdrrec_eof(&(cd->xdrs)))
595 return (XPRT_MOREREQS);
600 svc_vc_recv(xprt, msg)
607 assert(xprt != NULL);
610 cd = (struct cf_conn *)(xprt->xp_p1);
614 if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
618 xdrs->x_op = XDR_DECODE;
620 * No need skip records with nonblocking connections
622 if (cd->nonblock == FALSE)
623 (void)xdrrec_skiprecord(xdrs);
624 if (xdr_callmsg(xdrs, msg)) {
625 cd->x_id = msg->rm_xid;
628 cd->strm_stat = XPRT_DIED;
633 svc_vc_getargs(xprt, xdr_args, args_ptr)
639 assert(xprt != NULL);
640 /* args_ptr may be NULL */
642 if (! SVCAUTH_UNWRAP(xprt->xp_auth,
643 &(((struct cf_conn *)(xprt->xp_p1))->xdrs),
644 xdr_args, args_ptr)) {
651 svc_vc_freeargs(xprt, xdr_args, args_ptr)
658 assert(xprt != NULL);
659 /* args_ptr may be NULL */
661 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
663 xdrs->x_op = XDR_FREE;
664 return ((*xdr_args)(xdrs, args_ptr));
668 svc_vc_reply(xprt, msg)
676 xdrproc_t xdr_results;
677 caddr_t xdr_location;
680 assert(xprt != NULL);
683 cd = (struct cf_conn *)(xprt->xp_p1);
686 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
687 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
689 xdr_results = msg->acpted_rply.ar_results.proc;
690 xdr_location = msg->acpted_rply.ar_results.where;
692 msg->acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
693 msg->acpted_rply.ar_results.where = NULL;
697 xdrs->x_op = XDR_ENCODE;
698 msg->rm_xid = cd->x_id;
700 if (xdr_replymsg(xdrs, msg) &&
701 (!has_args || (xprt->xp_auth &&
702 SVCAUTH_WRAP(xprt->xp_auth, xdrs, xdr_results, xdr_location)))) {
705 (void)xdrrec_endofrecord(xdrs, TRUE);
713 static struct xp_ops ops;
714 static struct xp_ops2 ops2;
715 extern mutex_t ops_lock;
717 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
719 mutex_lock(&ops_lock);
720 if (ops.xp_recv == NULL) {
721 ops.xp_recv = svc_vc_recv;
722 ops.xp_stat = svc_vc_stat;
723 ops.xp_getargs = svc_vc_getargs;
724 ops.xp_reply = svc_vc_reply;
725 ops.xp_freeargs = svc_vc_freeargs;
726 ops.xp_destroy = svc_vc_destroy;
727 ops2.xp_control = svc_vc_control;
730 xprt->xp_ops2 = &ops2;
731 mutex_unlock(&ops_lock);
735 svc_vc_rendezvous_ops(xprt)
738 static struct xp_ops ops;
739 static struct xp_ops2 ops2;
740 extern mutex_t ops_lock;
742 mutex_lock(&ops_lock);
743 if (ops.xp_recv == NULL) {
744 ops.xp_recv = rendezvous_request;
745 ops.xp_stat = rendezvous_stat;
747 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
749 (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
751 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
752 ops.xp_destroy = svc_vc_destroy;
753 ops2.xp_control = svc_vc_rendezvous_control;
756 xprt->xp_ops2 = &ops2;
757 mutex_unlock(&ops_lock);
761 * Get the effective UID of the sending process. Used by rpcbind, keyserv
762 * and rpc.yppasswdd on AF_LOCAL.
765 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
771 sock = transp->xp_fd;
772 sa = (struct sockaddr *)transp->xp_rtaddr.buf;
773 if (sa->sa_family == AF_LOCAL) {
774 ret = getpeereid(sock, &euid, &egid);
783 * Destroy xprts that have not have had any activity in 'timeout' seconds.
784 * If 'cleanblock' is true, blocking connections (the default) are also
785 * cleaned. If timeout is 0, the least active connection is picked.
787 * Though this is not a publicly documented interface, some versions of
788 * rpcbind are known to call this function. Do not alter or remove this
789 * API without changing the library's sonum.
792 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
795 SVCXPRT *xprt, *least_active;
796 struct timeval tv, tdiff, tmax;
799 gettimeofday(&tv, NULL);
800 tmax.tv_sec = tmax.tv_usec = 0;
802 rwlock_wrlock(&svc_fd_lock);
803 for (i = ncleaned = 0; i <= svc_maxfd; i++) {
804 if (FD_ISSET(i, fds)) {
805 xprt = __svc_xports[i];
806 if (xprt == NULL || xprt->xp_ops == NULL ||
807 xprt->xp_ops->xp_recv != svc_vc_recv)
809 cd = (struct cf_conn *)xprt->xp_p1;
810 if (!cleanblock && !cd->nonblock)
813 timersub(&tv, &cd->last_recv_time, &tdiff);
814 if (timercmp(&tdiff, &tmax, >)) {
820 if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
821 __xprt_unregister_unlocked(xprt);
822 __svc_vc_dodestroy(xprt);
827 if (timeout == 0 && least_active != NULL) {
828 __xprt_unregister_unlocked(least_active);
829 __svc_vc_dodestroy(least_active);
832 rwlock_unlock(&svc_fd_lock);
833 return ncleaned > 0 ? TRUE : FALSE;