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>
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 SVCXPRT *svc_vc_create_conn(SVCPOOL *pool, struct socket *so,
87 struct sockaddr *raddr);
88 static int svc_vc_accept(struct socket *head, struct socket **sop);
89 static int svc_vc_soupcall(struct socket *so, void *arg, int waitflag);
91 static struct xp_ops svc_vc_rendezvous_ops = {
92 .xp_recv = svc_vc_rendezvous_recv,
93 .xp_stat = svc_vc_rendezvous_stat,
94 .xp_reply = (bool_t (*)(SVCXPRT *, struct rpc_msg *,
95 struct sockaddr *, struct mbuf *))svc_vc_null,
96 .xp_destroy = svc_vc_rendezvous_destroy,
97 .xp_control = svc_vc_rendezvous_control
100 static struct xp_ops svc_vc_ops = {
101 .xp_recv = svc_vc_recv,
102 .xp_stat = svc_vc_stat,
103 .xp_reply = svc_vc_reply,
104 .xp_destroy = svc_vc_destroy,
105 .xp_control = svc_vc_control
108 struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
109 enum xprt_stat strm_stat;
110 struct mbuf *mpending; /* unparsed data read from the socket */
111 struct mbuf *mreq; /* current record being built from mpending */
112 uint32_t resid; /* number of bytes needed for fragment */
113 bool_t eor; /* reading last fragment of current record */
118 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
120 * Creates, registers, and returns a (rpc) tcp based transporter.
121 * Once *xprt is initialized, it is registered as a transporter
122 * see (svc.h, xprt_register). This routine returns
123 * a NULL if a problem occurred.
125 * The filedescriptor passed in is expected to refer to a bound, but
126 * not yet connected socket.
128 * Since streams do buffered io similar to stdio, the caller can specify
129 * how big the send and receive buffers are via the second and third parms;
130 * 0 => use the system default.
133 svc_vc_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
141 if (so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED)) {
143 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
146 xprt = svc_vc_create_conn(pool, so, sa);
152 xprt = svc_xprt_alloc();
153 sx_init(&xprt->xp_lock, "xprt->xp_lock");
154 xprt->xp_pool = pool;
155 xprt->xp_socket = so;
158 xprt->xp_ops = &svc_vc_rendezvous_ops;
160 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
162 goto cleanup_svc_vc_create;
165 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
170 solisten(so, SOMAXCONN, curthread);
172 SOCKBUF_LOCK(&so->so_rcv);
173 xprt->xp_upcallset = 1;
174 soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
175 SOCKBUF_UNLOCK(&so->so_rcv);
178 cleanup_svc_vc_create:
185 * Create a new transport for a socket optained via soaccept().
188 svc_vc_create_conn(SVCPOOL *pool, struct socket *so, struct sockaddr *raddr)
190 SVCXPRT *xprt = NULL;
191 struct cf_conn *cd = NULL;
192 struct sockaddr* sa = NULL;
197 bzero(&opt, sizeof(struct sockopt));
198 opt.sopt_dir = SOPT_SET;
199 opt.sopt_level = SOL_SOCKET;
200 opt.sopt_name = SO_KEEPALIVE;
202 opt.sopt_valsize = sizeof(one);
203 error = sosetopt(so, &opt);
208 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
209 bzero(&opt, sizeof(struct sockopt));
210 opt.sopt_dir = SOPT_SET;
211 opt.sopt_level = IPPROTO_TCP;
212 opt.sopt_name = TCP_NODELAY;
214 opt.sopt_valsize = sizeof(one);
215 error = sosetopt(so, &opt);
221 cd = mem_alloc(sizeof(*cd));
222 cd->strm_stat = XPRT_IDLE;
224 xprt = svc_xprt_alloc();
225 sx_init(&xprt->xp_lock, "xprt->xp_lock");
226 xprt->xp_pool = pool;
227 xprt->xp_socket = so;
230 xprt->xp_ops = &svc_vc_ops;
233 * See http://www.connectathon.org/talks96/nfstcp.pdf - client
234 * has a 5 minute timer, server has a 6 minute timer.
236 xprt->xp_idletimeout = 6 * 60;
238 memcpy(&xprt->xp_rtaddr, raddr, raddr->sa_len);
240 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
242 goto cleanup_svc_vc_create;
244 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
249 SOCKBUF_LOCK(&so->so_rcv);
250 xprt->xp_upcallset = 1;
251 soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
252 SOCKBUF_UNLOCK(&so->so_rcv);
255 * Throw the transport into the active list in case it already
256 * has some data buffered.
258 sx_xlock(&xprt->xp_lock);
260 sx_xunlock(&xprt->xp_lock);
263 cleanup_svc_vc_create:
265 mem_free(xprt, sizeof(*xprt));
268 mem_free(cd, sizeof(*cd));
273 * This does all of the accept except the final call to soaccept. The
274 * caller will call soaccept after dropping its locks (soaccept may
278 svc_vc_accept(struct socket *head, struct socket **sop)
283 if ((head->so_options & SO_ACCEPTCONN) == 0) {
288 error = mac_socket_check_accept(curthread->td_ucred, head);
293 if (TAILQ_EMPTY(&head->so_comp)) {
298 so = TAILQ_FIRST(&head->so_comp);
299 KASSERT(!(so->so_qstate & SQ_INCOMP), ("svc_vc_accept: so SQ_INCOMP"));
300 KASSERT(so->so_qstate & SQ_COMP, ("svc_vc_accept: so not SQ_COMP"));
303 * Before changing the flags on the socket, we have to bump the
304 * reference count. Otherwise, if the protocol calls sofree(),
305 * the socket will be released due to a zero refcount.
306 * XXX might not need soref() since this is simpler than kern_accept.
308 SOCK_LOCK(so); /* soref() and so_state update */
309 soref(so); /* file descriptor reference */
311 TAILQ_REMOVE(&head->so_comp, so, so_list);
313 so->so_state |= (head->so_state & SS_NBIO);
314 so->so_qstate &= ~SQ_COMP;
322 /* connection has been removed from the listen queue */
323 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
330 svc_vc_rendezvous_recv(SVCXPRT *xprt, struct rpc_msg *msg,
331 struct sockaddr **addrp, struct mbuf **mp)
333 struct socket *so = NULL;
334 struct sockaddr *sa = NULL;
339 * The socket upcall calls xprt_active() which will eventually
340 * cause the server to call us here. We attempt to accept a
341 * connection from the socket and turn it into a new
342 * transport. If the accept fails, we have drained all pending
343 * connections so we call xprt_inactive().
345 sx_xlock(&xprt->xp_lock);
347 error = svc_vc_accept(xprt->xp_socket, &so);
349 if (error == EWOULDBLOCK) {
351 * We must re-test for new connections after taking
352 * the lock to protect us in the case where a new
353 * connection arrives after our call to accept fails
354 * with EWOULDBLOCK. The pool lock protects us from
355 * racing the upcall after our TAILQ_EMPTY() call
359 mtx_lock(&xprt->xp_pool->sp_lock);
360 if (TAILQ_EMPTY(&xprt->xp_socket->so_comp))
361 xprt_inactive_locked(xprt);
362 mtx_unlock(&xprt->xp_pool->sp_lock);
364 sx_xunlock(&xprt->xp_lock);
369 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
370 if (xprt->xp_upcallset) {
371 xprt->xp_upcallset = 0;
372 soupcall_clear(xprt->xp_socket, SO_RCV);
374 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
376 sx_xunlock(&xprt->xp_lock);
380 sx_xunlock(&xprt->xp_lock);
383 error = soaccept(so, &sa);
387 * XXX not sure if I need to call sofree or soclose here.
395 * svc_vc_create_conn will call xprt_register - we don't need
396 * to do anything with the new connection except derefence it.
398 new_xprt = svc_vc_create_conn(xprt->xp_pool, so, sa);
402 SVC_RELEASE(new_xprt);
407 return (FALSE); /* there is never an rpc msg to be processed */
411 static enum xprt_stat
412 svc_vc_rendezvous_stat(SVCXPRT *xprt)
419 svc_vc_destroy_common(SVCXPRT *xprt)
421 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
422 if (xprt->xp_upcallset) {
423 xprt->xp_upcallset = 0;
424 soupcall_clear(xprt->xp_socket, SO_RCV);
426 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
428 sx_destroy(&xprt->xp_lock);
430 (void)soclose(xprt->xp_socket);
433 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
438 svc_vc_rendezvous_destroy(SVCXPRT *xprt)
441 svc_vc_destroy_common(xprt);
445 svc_vc_destroy(SVCXPRT *xprt)
447 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
449 svc_vc_destroy_common(xprt);
454 m_freem(cd->mpending);
455 mem_free(cd, sizeof(*cd));
460 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
466 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
472 static enum xprt_stat
473 svc_vc_stat(SVCXPRT *xprt)
479 cd = (struct cf_conn *)(xprt->xp_p1);
481 if (cd->strm_stat == XPRT_DIED)
485 * Return XPRT_MOREREQS if we have buffered data and we are
486 * mid-record or if we have enough data for a record
487 * marker. Since this is only a hint, we read mpending and
488 * resid outside the lock. We do need to take the lock if we
489 * have to traverse the mbuf chain.
493 return (XPRT_MOREREQS);
495 sx_xlock(&xprt->xp_lock);
497 while (m && n < sizeof(uint32_t)) {
501 sx_xunlock(&xprt->xp_lock);
502 if (n >= sizeof(uint32_t))
503 return (XPRT_MOREREQS);
506 if (soreadable(xprt->xp_socket))
507 return (XPRT_MOREREQS);
513 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg,
514 struct sockaddr **addrp, struct mbuf **mp)
516 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
523 * Serialise access to the socket and our own record parsing
526 sx_xlock(&xprt->xp_lock);
530 * If we have an mbuf chain in cd->mpending, try to parse a
531 * record from it, leaving the result in cd->mreq. If we don't
532 * have a complete record, leave the partial result in
533 * cd->mreq and try to read more from the socket.
537 * If cd->resid is non-zero, we have part of the
538 * record already, otherwise we are expecting a record
543 * See if there is enough data buffered to
544 * make up a record marker. Make sure we can
545 * handle the case where the record marker is
546 * split across more than one mbuf.
552 while (n < sizeof(uint32_t) && m) {
556 if (n < sizeof(uint32_t))
558 m_copydata(cd->mpending, 0, sizeof(header),
560 header = ntohl(header);
561 cd->eor = (header & 0x80000000) != 0;
562 cd->resid = header & 0x7fffffff;
563 m_adj(cd->mpending, sizeof(uint32_t));
567 * Start pulling off mbufs from cd->mpending
568 * until we either have a complete record or
569 * we run out of data. We use m_split to pull
570 * data - it will pull as much as possible and
571 * split the last mbuf if necessary.
573 while (cd->mpending && cd->resid) {
575 if (cd->mpending->m_next
576 || cd->mpending->m_len > cd->resid)
577 cd->mpending = m_split(cd->mpending,
582 m_last(cd->mreq)->m_next = m;
586 cd->resid -= m->m_len;
592 * If cd->resid is zero now, we have managed to
593 * receive a record fragment from the stream. Check
594 * for the end-of-record mark to see if we need more.
596 if (cd->resid == 0) {
601 * Success - we have a complete record in
604 xdrmbuf_create(&xdrs, cd->mreq, XDR_DECODE);
606 sx_xunlock(&xprt->xp_lock);
608 if (! xdr_callmsg(&xdrs, msg)) {
614 *mp = xdrmbuf_getall(&xdrs);
623 * The socket upcall calls xprt_active() which will eventually
624 * cause the server to call us here. We attempt to
625 * read as much as possible from the socket and put
626 * the result in cd->mpending. If the read fails,
627 * we have drained both cd->mpending and the socket so
628 * we can call xprt_inactive().
630 uio.uio_resid = 1000000000;
631 uio.uio_td = curthread;
633 rcvflag = MSG_DONTWAIT;
634 error = soreceive(xprt->xp_socket, NULL, &uio, &m, NULL,
637 if (error == EWOULDBLOCK) {
639 * We must re-test for readability after
640 * taking the lock to protect us in the case
641 * where a new packet arrives on the socket
642 * after our call to soreceive fails with
643 * EWOULDBLOCK. The pool lock protects us from
644 * racing the upcall after our soreadable()
645 * call returns false.
647 mtx_lock(&xprt->xp_pool->sp_lock);
648 if (!soreadable(xprt->xp_socket))
649 xprt_inactive_locked(xprt);
650 mtx_unlock(&xprt->xp_pool->sp_lock);
651 sx_xunlock(&xprt->xp_lock);
656 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
657 if (xprt->xp_upcallset) {
658 xprt->xp_upcallset = 0;
659 soupcall_clear(xprt->xp_socket, SO_RCV);
661 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
663 cd->strm_stat = XPRT_DIED;
664 sx_xunlock(&xprt->xp_lock);
670 * EOF - the other end has closed the socket.
673 cd->strm_stat = XPRT_DIED;
674 sx_xunlock(&xprt->xp_lock);
679 m_last(cd->mpending)->m_next = m;
686 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg,
687 struct sockaddr *addr, struct mbuf *m)
695 * Leave space for record mark.
697 MGETHDR(mrep, M_WAIT, MT_DATA);
699 mrep->m_data += sizeof(uint32_t);
701 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
703 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
704 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
705 if (!xdr_replymsg(&xdrs, msg))
708 xdrmbuf_append(&xdrs, m);
710 stat = xdr_replymsg(&xdrs, msg);
717 * Prepend a record marker containing the reply length.
719 M_PREPEND(mrep, sizeof(uint32_t), M_WAIT);
720 *mtod(mrep, uint32_t *) =
721 htonl(0x80000000 | (mrep->m_pkthdr.len
722 - sizeof(uint32_t)));
723 error = sosend(xprt->xp_socket, NULL, NULL, mrep, NULL,
746 svc_vc_soupcall(struct socket *so, void *arg, int waitflag)
748 SVCXPRT *xprt = (SVCXPRT *) arg;
756 * Get the effective UID of the sending process. Used by rpcbind, keyserv
757 * and rpc.yppasswdd on AF_LOCAL.
760 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
766 sock = transp->xp_fd;
767 sa = (struct sockaddr *)transp->xp_rtaddr;
768 if (sa->sa_family == AF_LOCAL) {
769 ret = getpeereid(sock, &euid, &egid);