1 /* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * svc_vc.c, Server side for Connection Oriented based RPC.
42 * Actually implements two flavors of transporter -
43 * a tcp rendezvouser (a listner and connection establisher)
44 * and a record/tcp stream.
47 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/mutex.h>
54 #include <sys/protosw.h>
55 #include <sys/queue.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
59 #include <sys/systm.h>
64 #include <netinet/tcp.h>
69 #include <rpc/rpc_com.h>
71 #include <security/mac/mac_framework.h>
73 static bool_t svc_vc_rendezvous_recv(SVCXPRT *, struct rpc_msg *,
74 struct sockaddr **, struct mbuf **);
75 static enum xprt_stat svc_vc_rendezvous_stat(SVCXPRT *);
76 static void svc_vc_rendezvous_destroy(SVCXPRT *);
77 static bool_t svc_vc_null(void);
78 static void svc_vc_destroy(SVCXPRT *);
79 static enum xprt_stat svc_vc_stat(SVCXPRT *);
80 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *,
81 struct sockaddr **, struct mbuf **);
82 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *,
83 struct sockaddr *, struct mbuf *);
84 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
85 static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
87 static void svc_vc_backchannel_destroy(SVCXPRT *);
88 static enum xprt_stat svc_vc_backchannel_stat(SVCXPRT *);
89 static bool_t svc_vc_backchannel_recv(SVCXPRT *, struct rpc_msg *,
90 struct sockaddr **, struct mbuf **);
91 static bool_t svc_vc_backchannel_reply(SVCXPRT *, struct rpc_msg *,
92 struct sockaddr *, struct mbuf *);
93 static bool_t svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq,
95 static SVCXPRT *svc_vc_create_conn(SVCPOOL *pool, struct socket *so,
96 struct sockaddr *raddr);
97 static int svc_vc_accept(struct socket *head, struct socket **sop);
98 static int svc_vc_soupcall(struct socket *so, void *arg, int waitflag);
100 static struct xp_ops svc_vc_rendezvous_ops = {
101 .xp_recv = svc_vc_rendezvous_recv,
102 .xp_stat = svc_vc_rendezvous_stat,
103 .xp_reply = (bool_t (*)(SVCXPRT *, struct rpc_msg *,
104 struct sockaddr *, struct mbuf *))svc_vc_null,
105 .xp_destroy = svc_vc_rendezvous_destroy,
106 .xp_control = svc_vc_rendezvous_control
109 static struct xp_ops svc_vc_ops = {
110 .xp_recv = svc_vc_recv,
111 .xp_stat = svc_vc_stat,
112 .xp_reply = svc_vc_reply,
113 .xp_destroy = svc_vc_destroy,
114 .xp_control = svc_vc_control
117 static struct xp_ops svc_vc_backchannel_ops = {
118 .xp_recv = svc_vc_backchannel_recv,
119 .xp_stat = svc_vc_backchannel_stat,
120 .xp_reply = svc_vc_backchannel_reply,
121 .xp_destroy = svc_vc_backchannel_destroy,
122 .xp_control = svc_vc_backchannel_control
127 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
129 * Creates, registers, and returns a (rpc) tcp based transporter.
130 * Once *xprt is initialized, it is registered as a transporter
131 * see (svc.h, xprt_register). This routine returns
132 * a NULL if a problem occurred.
134 * The filedescriptor passed in is expected to refer to a bound, but
135 * not yet connected socket.
137 * Since streams do buffered io similar to stdio, the caller can specify
138 * how big the send and receive buffers are via the second and third parms;
139 * 0 => use the system default.
142 svc_vc_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
149 if (so->so_state & SS_ISCONNECTED) {
150 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
153 xprt = svc_vc_create_conn(pool, so, sa);
158 xprt = svc_xprt_alloc();
159 sx_init(&xprt->xp_lock, "xprt->xp_lock");
160 xprt->xp_pool = pool;
161 xprt->xp_socket = so;
164 xprt->xp_ops = &svc_vc_rendezvous_ops;
166 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
168 goto cleanup_svc_vc_create;
171 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
176 solisten(so, SOMAXCONN, curthread);
178 SOCKBUF_LOCK(&so->so_rcv);
179 xprt->xp_upcallset = 1;
180 soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
181 SOCKBUF_UNLOCK(&so->so_rcv);
184 cleanup_svc_vc_create:
191 * Create a new transport for a socket optained via soaccept().
194 svc_vc_create_conn(SVCPOOL *pool, struct socket *so, struct sockaddr *raddr)
196 SVCXPRT *xprt = NULL;
197 struct cf_conn *cd = NULL;
198 struct sockaddr* sa = NULL;
203 bzero(&opt, sizeof(struct sockopt));
204 opt.sopt_dir = SOPT_SET;
205 opt.sopt_level = SOL_SOCKET;
206 opt.sopt_name = SO_KEEPALIVE;
208 opt.sopt_valsize = sizeof(one);
209 error = sosetopt(so, &opt);
214 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
215 bzero(&opt, sizeof(struct sockopt));
216 opt.sopt_dir = SOPT_SET;
217 opt.sopt_level = IPPROTO_TCP;
218 opt.sopt_name = TCP_NODELAY;
220 opt.sopt_valsize = sizeof(one);
221 error = sosetopt(so, &opt);
227 cd = mem_alloc(sizeof(*cd));
228 cd->strm_stat = XPRT_IDLE;
230 xprt = svc_xprt_alloc();
231 sx_init(&xprt->xp_lock, "xprt->xp_lock");
232 xprt->xp_pool = pool;
233 xprt->xp_socket = so;
236 xprt->xp_ops = &svc_vc_ops;
239 * See http://www.connectathon.org/talks96/nfstcp.pdf - client
240 * has a 5 minute timer, server has a 6 minute timer.
242 xprt->xp_idletimeout = 6 * 60;
244 memcpy(&xprt->xp_rtaddr, raddr, raddr->sa_len);
246 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
248 goto cleanup_svc_vc_create;
250 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
255 SOCKBUF_LOCK(&so->so_rcv);
256 xprt->xp_upcallset = 1;
257 soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
258 SOCKBUF_UNLOCK(&so->so_rcv);
261 * Throw the transport into the active list in case it already
262 * has some data buffered.
264 sx_xlock(&xprt->xp_lock);
266 sx_xunlock(&xprt->xp_lock);
269 cleanup_svc_vc_create:
271 mem_free(xprt, sizeof(*xprt));
274 mem_free(cd, sizeof(*cd));
279 * Create a new transport for a backchannel on a clnt_vc socket.
282 svc_vc_create_backchannel(SVCPOOL *pool)
284 SVCXPRT *xprt = NULL;
285 struct cf_conn *cd = NULL;
287 cd = mem_alloc(sizeof(*cd));
288 cd->strm_stat = XPRT_IDLE;
290 xprt = svc_xprt_alloc();
291 sx_init(&xprt->xp_lock, "xprt->xp_lock");
292 xprt->xp_pool = pool;
293 xprt->xp_socket = NULL;
296 xprt->xp_ops = &svc_vc_backchannel_ops;
301 * This does all of the accept except the final call to soaccept. The
302 * caller will call soaccept after dropping its locks (soaccept may
306 svc_vc_accept(struct socket *head, struct socket **sop)
311 if ((head->so_options & SO_ACCEPTCONN) == 0) {
316 error = mac_socket_check_accept(curthread->td_ucred, head);
321 if (TAILQ_EMPTY(&head->so_comp)) {
326 so = TAILQ_FIRST(&head->so_comp);
327 KASSERT(!(so->so_qstate & SQ_INCOMP), ("svc_vc_accept: so SQ_INCOMP"));
328 KASSERT(so->so_qstate & SQ_COMP, ("svc_vc_accept: so not SQ_COMP"));
331 * Before changing the flags on the socket, we have to bump the
332 * reference count. Otherwise, if the protocol calls sofree(),
333 * the socket will be released due to a zero refcount.
334 * XXX might not need soref() since this is simpler than kern_accept.
336 SOCK_LOCK(so); /* soref() and so_state update */
337 soref(so); /* file descriptor reference */
339 TAILQ_REMOVE(&head->so_comp, so, so_list);
341 so->so_state |= (head->so_state & SS_NBIO);
342 so->so_qstate &= ~SQ_COMP;
350 /* connection has been removed from the listen queue */
351 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
358 svc_vc_rendezvous_recv(SVCXPRT *xprt, struct rpc_msg *msg,
359 struct sockaddr **addrp, struct mbuf **mp)
361 struct socket *so = NULL;
362 struct sockaddr *sa = NULL;
367 * The socket upcall calls xprt_active() which will eventually
368 * cause the server to call us here. We attempt to accept a
369 * connection from the socket and turn it into a new
370 * transport. If the accept fails, we have drained all pending
371 * connections so we call xprt_inactive().
373 sx_xlock(&xprt->xp_lock);
375 error = svc_vc_accept(xprt->xp_socket, &so);
377 if (error == EWOULDBLOCK) {
379 * We must re-test for new connections after taking
380 * the lock to protect us in the case where a new
381 * connection arrives after our call to accept fails
382 * with EWOULDBLOCK. The pool lock protects us from
383 * racing the upcall after our TAILQ_EMPTY() call
387 mtx_lock(&xprt->xp_pool->sp_lock);
388 if (TAILQ_EMPTY(&xprt->xp_socket->so_comp))
389 xprt_inactive_locked(xprt);
390 mtx_unlock(&xprt->xp_pool->sp_lock);
392 sx_xunlock(&xprt->xp_lock);
397 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
398 if (xprt->xp_upcallset) {
399 xprt->xp_upcallset = 0;
400 soupcall_clear(xprt->xp_socket, SO_RCV);
402 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
404 sx_xunlock(&xprt->xp_lock);
408 sx_xunlock(&xprt->xp_lock);
411 error = soaccept(so, &sa);
415 * XXX not sure if I need to call sofree or soclose here.
423 * svc_vc_create_conn will call xprt_register - we don't need
424 * to do anything with the new connection except derefence it.
426 new_xprt = svc_vc_create_conn(xprt->xp_pool, so, sa);
430 SVC_RELEASE(new_xprt);
435 return (FALSE); /* there is never an rpc msg to be processed */
439 static enum xprt_stat
440 svc_vc_rendezvous_stat(SVCXPRT *xprt)
447 svc_vc_destroy_common(SVCXPRT *xprt)
449 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
450 if (xprt->xp_upcallset) {
451 xprt->xp_upcallset = 0;
452 soupcall_clear(xprt->xp_socket, SO_RCV);
454 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
456 sx_destroy(&xprt->xp_lock);
458 (void)soclose(xprt->xp_socket);
461 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
466 svc_vc_rendezvous_destroy(SVCXPRT *xprt)
469 svc_vc_destroy_common(xprt);
473 svc_vc_destroy(SVCXPRT *xprt)
475 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
477 svc_vc_destroy_common(xprt);
482 m_freem(cd->mpending);
483 mem_free(cd, sizeof(*cd));
487 svc_vc_backchannel_destroy(SVCXPRT *xprt)
489 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
499 mem_free(cd, sizeof(*cd));
504 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
510 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
517 svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq, void *in)
523 static enum xprt_stat
524 svc_vc_stat(SVCXPRT *xprt)
530 cd = (struct cf_conn *)(xprt->xp_p1);
532 if (cd->strm_stat == XPRT_DIED)
536 * Return XPRT_MOREREQS if we have buffered data and we are
537 * mid-record or if we have enough data for a record
538 * marker. Since this is only a hint, we read mpending and
539 * resid outside the lock. We do need to take the lock if we
540 * have to traverse the mbuf chain.
544 return (XPRT_MOREREQS);
546 sx_xlock(&xprt->xp_lock);
548 while (m && n < sizeof(uint32_t)) {
552 sx_xunlock(&xprt->xp_lock);
553 if (n >= sizeof(uint32_t))
554 return (XPRT_MOREREQS);
557 if (soreadable(xprt->xp_socket))
558 return (XPRT_MOREREQS);
563 static enum xprt_stat
564 svc_vc_backchannel_stat(SVCXPRT *xprt)
568 cd = (struct cf_conn *)(xprt->xp_p1);
570 if (cd->mreq != NULL)
571 return (XPRT_MOREREQS);
577 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg,
578 struct sockaddr **addrp, struct mbuf **mp)
580 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
587 * Serialise access to the socket and our own record parsing
590 sx_xlock(&xprt->xp_lock);
594 * If we have an mbuf chain in cd->mpending, try to parse a
595 * record from it, leaving the result in cd->mreq. If we don't
596 * have a complete record, leave the partial result in
597 * cd->mreq and try to read more from the socket.
601 * If cd->resid is non-zero, we have part of the
602 * record already, otherwise we are expecting a record
607 * See if there is enough data buffered to
608 * make up a record marker. Make sure we can
609 * handle the case where the record marker is
610 * split across more than one mbuf.
616 while (n < sizeof(uint32_t) && m) {
620 if (n < sizeof(uint32_t))
622 m_copydata(cd->mpending, 0, sizeof(header),
624 header = ntohl(header);
625 cd->eor = (header & 0x80000000) != 0;
626 cd->resid = header & 0x7fffffff;
627 m_adj(cd->mpending, sizeof(uint32_t));
631 * Start pulling off mbufs from cd->mpending
632 * until we either have a complete record or
633 * we run out of data. We use m_split to pull
634 * data - it will pull as much as possible and
635 * split the last mbuf if necessary.
637 while (cd->mpending && cd->resid) {
639 if (cd->mpending->m_next
640 || cd->mpending->m_len > cd->resid)
641 cd->mpending = m_split(cd->mpending,
642 cd->resid, M_WAITOK);
646 m_last(cd->mreq)->m_next = m;
650 cd->resid -= m->m_len;
656 * If cd->resid is zero now, we have managed to
657 * receive a record fragment from the stream. Check
658 * for the end-of-record mark to see if we need more.
660 if (cd->resid == 0) {
665 * Success - we have a complete record in
668 xdrmbuf_create(&xdrs, cd->mreq, XDR_DECODE);
670 sx_xunlock(&xprt->xp_lock);
672 if (! xdr_callmsg(&xdrs, msg)) {
678 *mp = xdrmbuf_getall(&xdrs);
687 * The socket upcall calls xprt_active() which will eventually
688 * cause the server to call us here. We attempt to
689 * read as much as possible from the socket and put
690 * the result in cd->mpending. If the read fails,
691 * we have drained both cd->mpending and the socket so
692 * we can call xprt_inactive().
694 uio.uio_resid = 1000000000;
695 uio.uio_td = curthread;
697 rcvflag = MSG_DONTWAIT;
698 error = soreceive(xprt->xp_socket, NULL, &uio, &m, NULL,
701 if (error == EWOULDBLOCK) {
703 * We must re-test for readability after
704 * taking the lock to protect us in the case
705 * where a new packet arrives on the socket
706 * after our call to soreceive fails with
707 * EWOULDBLOCK. The pool lock protects us from
708 * racing the upcall after our soreadable()
709 * call returns false.
711 mtx_lock(&xprt->xp_pool->sp_lock);
712 if (!soreadable(xprt->xp_socket))
713 xprt_inactive_locked(xprt);
714 mtx_unlock(&xprt->xp_pool->sp_lock);
715 sx_xunlock(&xprt->xp_lock);
720 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
721 if (xprt->xp_upcallset) {
722 xprt->xp_upcallset = 0;
723 soupcall_clear(xprt->xp_socket, SO_RCV);
725 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
727 cd->strm_stat = XPRT_DIED;
728 sx_xunlock(&xprt->xp_lock);
734 * EOF - the other end has closed the socket.
737 cd->strm_stat = XPRT_DIED;
738 sx_xunlock(&xprt->xp_lock);
743 m_last(cd->mpending)->m_next = m;
750 svc_vc_backchannel_recv(SVCXPRT *xprt, struct rpc_msg *msg,
751 struct sockaddr **addrp, struct mbuf **mp)
753 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
758 sx_xlock(&xprt->xp_lock);
759 ct = (struct ct_data *)xprt->xp_p2;
761 sx_xunlock(&xprt->xp_lock);
764 mtx_lock(&ct->ct_lock);
768 mtx_unlock(&ct->ct_lock);
769 sx_xunlock(&xprt->xp_lock);
772 cd->mreq = m->m_nextpkt;
773 mtx_unlock(&ct->ct_lock);
774 sx_xunlock(&xprt->xp_lock);
776 xdrmbuf_create(&xdrs, m, XDR_DECODE);
777 if (! xdr_callmsg(&xdrs, msg)) {
782 *mp = xdrmbuf_getall(&xdrs);
788 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg,
789 struct sockaddr *addr, struct mbuf *m)
797 * Leave space for record mark.
799 MGETHDR(mrep, M_WAITOK, MT_DATA);
801 mrep->m_data += sizeof(uint32_t);
803 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
805 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
806 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
807 if (!xdr_replymsg(&xdrs, msg))
810 xdrmbuf_append(&xdrs, m);
812 stat = xdr_replymsg(&xdrs, msg);
819 * Prepend a record marker containing the reply length.
821 M_PREPEND(mrep, sizeof(uint32_t), M_WAITOK);
822 *mtod(mrep, uint32_t *) =
823 htonl(0x80000000 | (mrep->m_pkthdr.len
824 - sizeof(uint32_t)));
825 error = sosend(xprt->xp_socket, NULL, NULL, mrep, NULL,
841 svc_vc_backchannel_reply(SVCXPRT *xprt, struct rpc_msg *msg,
842 struct sockaddr *addr, struct mbuf *m)
851 * Leave space for record mark.
853 MGETHDR(mrep, M_WAITOK, MT_DATA);
855 mrep->m_data += sizeof(uint32_t);
857 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
859 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
860 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
861 if (!xdr_replymsg(&xdrs, msg))
864 xdrmbuf_append(&xdrs, m);
866 stat = xdr_replymsg(&xdrs, msg);
873 * Prepend a record marker containing the reply length.
875 M_PREPEND(mrep, sizeof(uint32_t), M_WAITOK);
876 *mtod(mrep, uint32_t *) =
877 htonl(0x80000000 | (mrep->m_pkthdr.len
878 - sizeof(uint32_t)));
879 sx_xlock(&xprt->xp_lock);
880 ct = (struct ct_data *)xprt->xp_p2;
882 error = sosend(ct->ct_socket, NULL, NULL, mrep, NULL,
886 sx_xunlock(&xprt->xp_lock);
907 svc_vc_soupcall(struct socket *so, void *arg, int waitflag)
909 SVCXPRT *xprt = (SVCXPRT *) arg;
917 * Get the effective UID of the sending process. Used by rpcbind, keyserv
918 * and rpc.yppasswdd on AF_LOCAL.
921 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
927 sock = transp->xp_fd;
928 sa = (struct sockaddr *)transp->xp_rtaddr;
929 if (sa->sa_family == AF_LOCAL) {
930 ret = getpeereid(sock, &euid, &egid);