1 /* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * svc_vc.c, Server side for Connection Oriented based RPC.
41 * Actually implements two flavors of transporter -
42 * a tcp rendezvouser (a listner and connection establisher)
43 * and a record/tcp stream.
46 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/mutex.h>
53 #include <sys/protosw.h>
54 #include <sys/queue.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
58 #include <sys/systm.h>
63 #include <netinet/tcp.h>
68 #include <rpc/rpc_com.h>
70 #include <security/mac/mac_framework.h>
72 static bool_t svc_vc_rendezvous_recv(SVCXPRT *, struct rpc_msg *,
73 struct sockaddr **, struct mbuf **);
74 static enum xprt_stat svc_vc_rendezvous_stat(SVCXPRT *);
75 static void svc_vc_rendezvous_destroy(SVCXPRT *);
76 static bool_t svc_vc_null(void);
77 static void svc_vc_destroy(SVCXPRT *);
78 static enum xprt_stat svc_vc_stat(SVCXPRT *);
79 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *,
80 struct sockaddr **, struct mbuf **);
81 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *,
82 struct sockaddr *, struct mbuf *);
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,
86 static void svc_vc_backchannel_destroy(SVCXPRT *);
87 static enum xprt_stat svc_vc_backchannel_stat(SVCXPRT *);
88 static bool_t svc_vc_backchannel_recv(SVCXPRT *, struct rpc_msg *,
89 struct sockaddr **, struct mbuf **);
90 static bool_t svc_vc_backchannel_reply(SVCXPRT *, struct rpc_msg *,
91 struct sockaddr *, struct mbuf *);
92 static bool_t svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq,
94 static SVCXPRT *svc_vc_create_conn(SVCPOOL *pool, struct socket *so,
95 struct sockaddr *raddr);
96 static int svc_vc_accept(struct socket *head, struct socket **sop);
97 static int svc_vc_soupcall(struct socket *so, void *arg, int waitflag);
99 static struct xp_ops svc_vc_rendezvous_ops = {
100 .xp_recv = svc_vc_rendezvous_recv,
101 .xp_stat = svc_vc_rendezvous_stat,
102 .xp_reply = (bool_t (*)(SVCXPRT *, struct rpc_msg *,
103 struct sockaddr *, struct mbuf *))svc_vc_null,
104 .xp_destroy = svc_vc_rendezvous_destroy,
105 .xp_control = svc_vc_rendezvous_control
108 static struct xp_ops svc_vc_ops = {
109 .xp_recv = svc_vc_recv,
110 .xp_stat = svc_vc_stat,
111 .xp_reply = svc_vc_reply,
112 .xp_destroy = svc_vc_destroy,
113 .xp_control = svc_vc_control
116 static struct xp_ops svc_vc_backchannel_ops = {
117 .xp_recv = svc_vc_backchannel_recv,
118 .xp_stat = svc_vc_backchannel_stat,
119 .xp_reply = svc_vc_backchannel_reply,
120 .xp_destroy = svc_vc_backchannel_destroy,
121 .xp_control = svc_vc_backchannel_control
126 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
128 * Creates, registers, and returns a (rpc) tcp based transporter.
129 * Once *xprt is initialized, it is registered as a transporter
130 * see (svc.h, xprt_register). This routine returns
131 * a NULL if a problem occurred.
133 * The filedescriptor passed in is expected to refer to a bound, but
134 * not yet connected socket.
136 * Since streams do buffered io similar to stdio, the caller can specify
137 * how big the send and receive buffers are via the second and third parms;
138 * 0 => use the system default.
141 svc_vc_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
149 if (so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED)) {
151 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
154 xprt = svc_vc_create_conn(pool, so, sa);
160 xprt = svc_xprt_alloc();
161 sx_init(&xprt->xp_lock, "xprt->xp_lock");
162 xprt->xp_pool = pool;
163 xprt->xp_socket = so;
166 xprt->xp_ops = &svc_vc_rendezvous_ops;
168 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
170 goto cleanup_svc_vc_create;
173 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
178 solisten(so, SOMAXCONN, curthread);
180 SOCKBUF_LOCK(&so->so_rcv);
181 xprt->xp_upcallset = 1;
182 soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
183 SOCKBUF_UNLOCK(&so->so_rcv);
186 cleanup_svc_vc_create:
193 * Create a new transport for a socket optained via soaccept().
196 svc_vc_create_conn(SVCPOOL *pool, struct socket *so, struct sockaddr *raddr)
198 SVCXPRT *xprt = NULL;
199 struct cf_conn *cd = NULL;
200 struct sockaddr* sa = NULL;
205 bzero(&opt, sizeof(struct sockopt));
206 opt.sopt_dir = SOPT_SET;
207 opt.sopt_level = SOL_SOCKET;
208 opt.sopt_name = SO_KEEPALIVE;
210 opt.sopt_valsize = sizeof(one);
211 error = sosetopt(so, &opt);
216 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
217 bzero(&opt, sizeof(struct sockopt));
218 opt.sopt_dir = SOPT_SET;
219 opt.sopt_level = IPPROTO_TCP;
220 opt.sopt_name = TCP_NODELAY;
222 opt.sopt_valsize = sizeof(one);
223 error = sosetopt(so, &opt);
229 cd = mem_alloc(sizeof(*cd));
230 cd->strm_stat = XPRT_IDLE;
232 xprt = svc_xprt_alloc();
233 sx_init(&xprt->xp_lock, "xprt->xp_lock");
234 xprt->xp_pool = pool;
235 xprt->xp_socket = so;
238 xprt->xp_ops = &svc_vc_ops;
241 * See http://www.connectathon.org/talks96/nfstcp.pdf - client
242 * has a 5 minute timer, server has a 6 minute timer.
244 xprt->xp_idletimeout = 6 * 60;
246 memcpy(&xprt->xp_rtaddr, raddr, raddr->sa_len);
248 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
250 goto cleanup_svc_vc_create;
252 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
257 SOCKBUF_LOCK(&so->so_rcv);
258 xprt->xp_upcallset = 1;
259 soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
260 SOCKBUF_UNLOCK(&so->so_rcv);
263 * Throw the transport into the active list in case it already
264 * has some data buffered.
266 sx_xlock(&xprt->xp_lock);
268 sx_xunlock(&xprt->xp_lock);
271 cleanup_svc_vc_create:
273 mem_free(xprt, sizeof(*xprt));
276 mem_free(cd, sizeof(*cd));
281 * Create a new transport for a backchannel on a clnt_vc socket.
284 svc_vc_create_backchannel(SVCPOOL *pool)
286 SVCXPRT *xprt = NULL;
287 struct cf_conn *cd = NULL;
289 cd = mem_alloc(sizeof(*cd));
290 cd->strm_stat = XPRT_IDLE;
292 xprt = svc_xprt_alloc();
293 sx_init(&xprt->xp_lock, "xprt->xp_lock");
294 xprt->xp_pool = pool;
295 xprt->xp_socket = NULL;
298 xprt->xp_ops = &svc_vc_backchannel_ops;
303 * This does all of the accept except the final call to soaccept. The
304 * caller will call soaccept after dropping its locks (soaccept may
308 svc_vc_accept(struct socket *head, struct socket **sop)
313 if ((head->so_options & SO_ACCEPTCONN) == 0) {
318 error = mac_socket_check_accept(curthread->td_ucred, head);
323 if (TAILQ_EMPTY(&head->so_comp)) {
328 so = TAILQ_FIRST(&head->so_comp);
329 KASSERT(!(so->so_qstate & SQ_INCOMP), ("svc_vc_accept: so SQ_INCOMP"));
330 KASSERT(so->so_qstate & SQ_COMP, ("svc_vc_accept: so not SQ_COMP"));
333 * Before changing the flags on the socket, we have to bump the
334 * reference count. Otherwise, if the protocol calls sofree(),
335 * the socket will be released due to a zero refcount.
336 * XXX might not need soref() since this is simpler than kern_accept.
338 SOCK_LOCK(so); /* soref() and so_state update */
339 soref(so); /* file descriptor reference */
341 TAILQ_REMOVE(&head->so_comp, so, so_list);
343 so->so_state |= (head->so_state & SS_NBIO);
344 so->so_qstate &= ~SQ_COMP;
352 /* connection has been removed from the listen queue */
353 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
360 svc_vc_rendezvous_recv(SVCXPRT *xprt, struct rpc_msg *msg,
361 struct sockaddr **addrp, struct mbuf **mp)
363 struct socket *so = NULL;
364 struct sockaddr *sa = NULL;
369 * The socket upcall calls xprt_active() which will eventually
370 * cause the server to call us here. We attempt to accept a
371 * connection from the socket and turn it into a new
372 * transport. If the accept fails, we have drained all pending
373 * connections so we call xprt_inactive().
375 sx_xlock(&xprt->xp_lock);
377 error = svc_vc_accept(xprt->xp_socket, &so);
379 if (error == EWOULDBLOCK) {
381 * We must re-test for new connections after taking
382 * the lock to protect us in the case where a new
383 * connection arrives after our call to accept fails
387 if (TAILQ_EMPTY(&xprt->xp_socket->so_comp))
390 sx_xunlock(&xprt->xp_lock);
395 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
396 if (xprt->xp_upcallset) {
397 xprt->xp_upcallset = 0;
398 soupcall_clear(xprt->xp_socket, SO_RCV);
400 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
402 sx_xunlock(&xprt->xp_lock);
406 sx_xunlock(&xprt->xp_lock);
409 error = soaccept(so, &sa);
413 * XXX not sure if I need to call sofree or soclose here.
421 * svc_vc_create_conn will call xprt_register - we don't need
422 * to do anything with the new connection except derefence it.
424 new_xprt = svc_vc_create_conn(xprt->xp_pool, so, sa);
428 SVC_RELEASE(new_xprt);
433 return (FALSE); /* there is never an rpc msg to be processed */
437 static enum xprt_stat
438 svc_vc_rendezvous_stat(SVCXPRT *xprt)
445 svc_vc_destroy_common(SVCXPRT *xprt)
447 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
448 if (xprt->xp_upcallset) {
449 xprt->xp_upcallset = 0;
450 soupcall_clear(xprt->xp_socket, SO_RCV);
452 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
454 sx_destroy(&xprt->xp_lock);
456 (void)soclose(xprt->xp_socket);
459 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
464 svc_vc_rendezvous_destroy(SVCXPRT *xprt)
467 svc_vc_destroy_common(xprt);
471 svc_vc_destroy(SVCXPRT *xprt)
473 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
475 svc_vc_destroy_common(xprt);
480 m_freem(cd->mpending);
481 mem_free(cd, sizeof(*cd));
485 svc_vc_backchannel_destroy(SVCXPRT *xprt)
487 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
497 mem_free(cd, sizeof(*cd));
502 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
508 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
515 svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq, void *in)
521 static enum xprt_stat
522 svc_vc_stat(SVCXPRT *xprt)
526 cd = (struct cf_conn *)(xprt->xp_p1);
528 if (cd->strm_stat == XPRT_DIED)
531 if (cd->mreq != NULL && cd->resid == 0 && cd->eor)
532 return (XPRT_MOREREQS);
534 if (soreadable(xprt->xp_socket))
535 return (XPRT_MOREREQS);
540 static enum xprt_stat
541 svc_vc_backchannel_stat(SVCXPRT *xprt)
545 cd = (struct cf_conn *)(xprt->xp_p1);
547 if (cd->mreq != NULL)
548 return (XPRT_MOREREQS);
554 * If we have an mbuf chain in cd->mpending, try to parse a record from it,
555 * leaving the result in cd->mreq. If we don't have a complete record, leave
556 * the partial result in cd->mreq and try to read more from the socket.
559 svc_vc_process_pending(SVCXPRT *xprt)
561 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
562 struct socket *so = xprt->xp_socket;
566 * If cd->resid is non-zero, we have part of the
567 * record already, otherwise we are expecting a record
570 if (!cd->resid && cd->mpending) {
572 * See if there is enough data buffered to
573 * make up a record marker. Make sure we can
574 * handle the case where the record marker is
575 * split across more than one mbuf.
581 while (n < sizeof(uint32_t) && m) {
585 if (n < sizeof(uint32_t)) {
586 so->so_rcv.sb_lowat = sizeof(uint32_t) - n;
589 m_copydata(cd->mpending, 0, sizeof(header),
591 header = ntohl(header);
592 cd->eor = (header & 0x80000000) != 0;
593 cd->resid = header & 0x7fffffff;
594 m_adj(cd->mpending, sizeof(uint32_t));
598 * Start pulling off mbufs from cd->mpending
599 * until we either have a complete record or
600 * we run out of data. We use m_split to pull
601 * data - it will pull as much as possible and
602 * split the last mbuf if necessary.
604 while (cd->mpending && cd->resid) {
606 if (cd->mpending->m_next
607 || cd->mpending->m_len > cd->resid)
608 cd->mpending = m_split(cd->mpending,
609 cd->resid, M_WAITOK);
613 m_last(cd->mreq)->m_next = m;
617 cd->resid -= m->m_len;
622 so->so_rcv.sb_lowat = imax(1, imin(cd->resid, so->so_rcv.sb_hiwat / 2));
626 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg,
627 struct sockaddr **addrp, struct mbuf **mp)
629 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
632 struct socket* so = xprt->xp_socket;
637 * Serialise access to the socket and our own record parsing
640 sx_xlock(&xprt->xp_lock);
643 /* If we have no request ready, check pending queue. */
644 while (cd->mpending &&
645 (cd->mreq == NULL || cd->resid != 0 || !cd->eor))
646 svc_vc_process_pending(xprt);
648 /* Process and return complete request in cd->mreq. */
649 if (cd->mreq != NULL && cd->resid == 0 && cd->eor) {
651 xdrmbuf_create(&xdrs, cd->mreq, XDR_DECODE);
654 /* Check for next request in a pending queue. */
655 svc_vc_process_pending(xprt);
656 if (cd->mreq == NULL || cd->resid != 0) {
657 SOCKBUF_LOCK(&so->so_rcv);
660 SOCKBUF_UNLOCK(&so->so_rcv);
663 sx_xunlock(&xprt->xp_lock);
665 if (! xdr_callmsg(&xdrs, msg)) {
671 *mp = xdrmbuf_getall(&xdrs);
678 * The socket upcall calls xprt_active() which will eventually
679 * cause the server to call us here. We attempt to
680 * read as much as possible from the socket and put
681 * the result in cd->mpending. If the read fails,
682 * we have drained both cd->mpending and the socket so
683 * we can call xprt_inactive().
685 uio.uio_resid = 1000000000;
686 uio.uio_td = curthread;
688 rcvflag = MSG_DONTWAIT;
689 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
691 if (error == EWOULDBLOCK) {
693 * We must re-test for readability after
694 * taking the lock to protect us in the case
695 * where a new packet arrives on the socket
696 * after our call to soreceive fails with
699 SOCKBUF_LOCK(&so->so_rcv);
702 SOCKBUF_UNLOCK(&so->so_rcv);
703 sx_xunlock(&xprt->xp_lock);
708 SOCKBUF_LOCK(&so->so_rcv);
709 if (xprt->xp_upcallset) {
710 xprt->xp_upcallset = 0;
711 soupcall_clear(so, SO_RCV);
713 SOCKBUF_UNLOCK(&so->so_rcv);
715 cd->strm_stat = XPRT_DIED;
716 sx_xunlock(&xprt->xp_lock);
722 * EOF - the other end has closed the socket.
725 cd->strm_stat = XPRT_DIED;
726 sx_xunlock(&xprt->xp_lock);
731 m_last(cd->mpending)->m_next = m;
738 svc_vc_backchannel_recv(SVCXPRT *xprt, struct rpc_msg *msg,
739 struct sockaddr **addrp, struct mbuf **mp)
741 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
746 sx_xlock(&xprt->xp_lock);
747 ct = (struct ct_data *)xprt->xp_p2;
749 sx_xunlock(&xprt->xp_lock);
752 mtx_lock(&ct->ct_lock);
756 mtx_unlock(&ct->ct_lock);
757 sx_xunlock(&xprt->xp_lock);
760 cd->mreq = m->m_nextpkt;
761 mtx_unlock(&ct->ct_lock);
762 sx_xunlock(&xprt->xp_lock);
764 xdrmbuf_create(&xdrs, m, XDR_DECODE);
765 if (! xdr_callmsg(&xdrs, msg)) {
770 *mp = xdrmbuf_getall(&xdrs);
776 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg,
777 struct sockaddr *addr, struct mbuf *m)
785 * Leave space for record mark.
787 mrep = m_gethdr(M_WAITOK, MT_DATA);
788 mrep->m_data += sizeof(uint32_t);
790 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
792 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
793 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
794 if (!xdr_replymsg(&xdrs, msg))
797 xdrmbuf_append(&xdrs, m);
799 stat = xdr_replymsg(&xdrs, msg);
806 * Prepend a record marker containing the reply length.
808 M_PREPEND(mrep, sizeof(uint32_t), M_WAITOK);
809 *mtod(mrep, uint32_t *) =
810 htonl(0x80000000 | (mrep->m_pkthdr.len
811 - sizeof(uint32_t)));
812 error = sosend(xprt->xp_socket, NULL, NULL, mrep, NULL,
828 svc_vc_backchannel_reply(SVCXPRT *xprt, struct rpc_msg *msg,
829 struct sockaddr *addr, struct mbuf *m)
838 * Leave space for record mark.
840 mrep = m_gethdr(M_WAITOK, MT_DATA);
841 mrep->m_data += sizeof(uint32_t);
843 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
845 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
846 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
847 if (!xdr_replymsg(&xdrs, msg))
850 xdrmbuf_append(&xdrs, m);
852 stat = xdr_replymsg(&xdrs, msg);
859 * Prepend a record marker containing the reply length.
861 M_PREPEND(mrep, sizeof(uint32_t), M_WAITOK);
862 *mtod(mrep, uint32_t *) =
863 htonl(0x80000000 | (mrep->m_pkthdr.len
864 - sizeof(uint32_t)));
865 sx_xlock(&xprt->xp_lock);
866 ct = (struct ct_data *)xprt->xp_p2;
868 error = sosend(ct->ct_socket, NULL, NULL, mrep, NULL,
872 sx_xunlock(&xprt->xp_lock);
893 svc_vc_soupcall(struct socket *so, void *arg, int waitflag)
895 SVCXPRT *xprt = (SVCXPRT *) arg;
897 if (soreadable(xprt->xp_socket))
904 * Get the effective UID of the sending process. Used by rpcbind, keyserv
905 * and rpc.yppasswdd on AF_LOCAL.
908 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
914 sock = transp->xp_fd;
915 sa = (struct sockaddr *)transp->xp_rtaddr;
916 if (sa->sa_family == AF_LOCAL) {
917 ret = getpeereid(sock, &euid, &egid);