1 /* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";
37 #include <sys/cdefs.h>
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 "opt_kern_tls.h"
48 #include <sys/param.h>
49 #include <sys/limits.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
55 #include <sys/mutex.h>
57 #include <sys/protosw.h>
58 #include <sys/queue.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
62 #include <sys/systm.h>
67 #include <netinet/tcp.h>
70 #include <rpc/rpcsec_tls.h>
73 #include <rpc/rpc_com.h>
75 #include <security/mac/mac_framework.h>
77 static bool_t svc_vc_rendezvous_recv(SVCXPRT *, struct rpc_msg *,
78 struct sockaddr **, struct mbuf **);
79 static enum xprt_stat svc_vc_rendezvous_stat(SVCXPRT *);
80 static void svc_vc_rendezvous_destroy(SVCXPRT *);
81 static bool_t svc_vc_null(void);
82 static void svc_vc_destroy(SVCXPRT *);
83 static enum xprt_stat svc_vc_stat(SVCXPRT *);
84 static bool_t svc_vc_ack(SVCXPRT *, uint32_t *);
85 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *,
86 struct sockaddr **, struct mbuf **);
87 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *,
88 struct sockaddr *, struct mbuf *, uint32_t *seq);
89 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
90 static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
92 static void svc_vc_backchannel_destroy(SVCXPRT *);
93 static enum xprt_stat svc_vc_backchannel_stat(SVCXPRT *);
94 static bool_t svc_vc_backchannel_recv(SVCXPRT *, struct rpc_msg *,
95 struct sockaddr **, struct mbuf **);
96 static bool_t svc_vc_backchannel_reply(SVCXPRT *, struct rpc_msg *,
97 struct sockaddr *, struct mbuf *, uint32_t *);
98 static bool_t svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq,
100 static SVCXPRT *svc_vc_create_conn(SVCPOOL *pool, struct socket *so,
101 struct sockaddr *raddr);
102 static int svc_vc_accept(struct socket *head, struct socket **sop);
103 static int svc_vc_soupcall(struct socket *so, void *arg, int waitflag);
104 static int svc_vc_rendezvous_soupcall(struct socket *, void *, int);
106 static const struct xp_ops svc_vc_rendezvous_ops = {
107 .xp_recv = svc_vc_rendezvous_recv,
108 .xp_stat = svc_vc_rendezvous_stat,
109 .xp_reply = (bool_t (*)(SVCXPRT *, struct rpc_msg *,
110 struct sockaddr *, struct mbuf *, uint32_t *))svc_vc_null,
111 .xp_destroy = svc_vc_rendezvous_destroy,
112 .xp_control = svc_vc_rendezvous_control
115 static const struct xp_ops svc_vc_ops = {
116 .xp_recv = svc_vc_recv,
117 .xp_stat = svc_vc_stat,
118 .xp_ack = svc_vc_ack,
119 .xp_reply = svc_vc_reply,
120 .xp_destroy = svc_vc_destroy,
121 .xp_control = svc_vc_control
124 static const struct xp_ops svc_vc_backchannel_ops = {
125 .xp_recv = svc_vc_backchannel_recv,
126 .xp_stat = svc_vc_backchannel_stat,
127 .xp_reply = svc_vc_backchannel_reply,
128 .xp_destroy = svc_vc_backchannel_destroy,
129 .xp_control = svc_vc_backchannel_control
134 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
136 * Creates, registers, and returns a (rpc) tcp based transporter.
137 * Once *xprt is initialized, it is registered as a transporter
138 * see (svc.h, xprt_register). This routine returns
139 * a NULL if a problem occurred.
141 * The filedescriptor passed in is expected to refer to a bound, but
142 * not yet connected socket.
144 * Since streams do buffered io similar to stdio, the caller can specify
145 * how big the send and receive buffers are via the second and third parms;
146 * 0 => use the system default.
149 svc_vc_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
157 if (so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED)) {
159 CURVNET_SET(so->so_vnet);
160 error = so->so_proto->pr_peeraddr(so, &sa);
164 xprt = svc_vc_create_conn(pool, so, sa);
170 xprt = svc_xprt_alloc();
171 sx_init(&xprt->xp_lock, "xprt->xp_lock");
172 xprt->xp_pool = pool;
173 xprt->xp_socket = so;
176 xprt->xp_ops = &svc_vc_rendezvous_ops;
178 CURVNET_SET(so->so_vnet);
179 error = so->so_proto->pr_sockaddr(so, &sa);
182 goto cleanup_svc_vc_create;
185 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
190 solisten(so, -1, curthread);
193 xprt->xp_upcallset = 1;
194 solisten_upcall_set(so, svc_vc_rendezvous_soupcall, xprt);
199 cleanup_svc_vc_create:
200 sx_destroy(&xprt->xp_lock);
207 * Create a new transport for a socket optained via soaccept().
210 svc_vc_create_conn(SVCPOOL *pool, struct socket *so, struct sockaddr *raddr)
214 struct sockaddr* sa = NULL;
219 bzero(&opt, sizeof(struct sockopt));
220 opt.sopt_dir = SOPT_SET;
221 opt.sopt_level = SOL_SOCKET;
222 opt.sopt_name = SO_KEEPALIVE;
224 opt.sopt_valsize = sizeof(one);
225 error = sosetopt(so, &opt);
230 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
231 bzero(&opt, sizeof(struct sockopt));
232 opt.sopt_dir = SOPT_SET;
233 opt.sopt_level = IPPROTO_TCP;
234 opt.sopt_name = TCP_NODELAY;
236 opt.sopt_valsize = sizeof(one);
237 error = sosetopt(so, &opt);
243 cd = mem_alloc(sizeof(*cd));
244 cd->strm_stat = XPRT_IDLE;
246 xprt = svc_xprt_alloc();
247 sx_init(&xprt->xp_lock, "xprt->xp_lock");
248 xprt->xp_pool = pool;
249 xprt->xp_socket = so;
252 xprt->xp_ops = &svc_vc_ops;
255 * See http://www.connectathon.org/talks96/nfstcp.pdf - client
256 * has a 5 minute timer, server has a 6 minute timer.
258 xprt->xp_idletimeout = 6 * 60;
260 memcpy(&xprt->xp_rtaddr, raddr, raddr->sa_len);
262 CURVNET_SET(so->so_vnet);
263 error = so->so_proto->pr_sockaddr(so, &sa);
266 goto cleanup_svc_vc_create;
268 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
273 SOCKBUF_LOCK(&so->so_rcv);
274 xprt->xp_upcallset = 1;
275 soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
276 SOCKBUF_UNLOCK(&so->so_rcv);
279 * Throw the transport into the active list in case it already
280 * has some data buffered.
282 sx_xlock(&xprt->xp_lock);
284 sx_xunlock(&xprt->xp_lock);
287 cleanup_svc_vc_create:
288 sx_destroy(&xprt->xp_lock);
290 mem_free(cd, sizeof(*cd));
296 * Create a new transport for a backchannel on a clnt_vc socket.
299 svc_vc_create_backchannel(SVCPOOL *pool)
301 SVCXPRT *xprt = NULL;
302 struct cf_conn *cd = NULL;
304 cd = mem_alloc(sizeof(*cd));
305 cd->strm_stat = XPRT_IDLE;
307 xprt = svc_xprt_alloc();
308 sx_init(&xprt->xp_lock, "xprt->xp_lock");
309 xprt->xp_pool = pool;
310 xprt->xp_socket = NULL;
313 xprt->xp_ops = &svc_vc_backchannel_ops;
318 * This does all of the accept except the final call to soaccept. The
319 * caller will call soaccept after dropping its locks (soaccept may
323 svc_vc_accept(struct socket *head, struct socket **sop)
329 KASSERT(SOLISTENING(head),
330 ("%s: socket %p is not listening", __func__, head));
333 error = mac_socket_check_accept(curthread->td_ucred, head);
338 * XXXGL: we want non-blocking semantics. The socket could be a
339 * socket created by kernel as well as socket shared with userland,
340 * so we can't be sure about presense of SS_NBIO. We also shall not
341 * toggle it on the socket, since that may surprise userland. So we
342 * set SS_NBIO only temporarily.
345 nbio = head->so_state & SS_NBIO;
346 head->so_state |= SS_NBIO;
347 error = solisten_dequeue(head, &so, 0);
348 head->so_state &= (nbio & ~SS_NBIO);
352 so->so_state |= nbio;
355 /* connection has been removed from the listen queue */
356 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
363 svc_vc_rendezvous_recv(SVCXPRT *xprt, struct rpc_msg *msg,
364 struct sockaddr **addrp, struct mbuf **mp)
366 struct socket *so = NULL;
367 struct sockaddr *sa = NULL;
372 * The socket upcall calls xprt_active() which will eventually
373 * cause the server to call us here. We attempt to accept a
374 * connection from the socket and turn it into a new
375 * transport. If the accept fails, we have drained all pending
376 * connections so we call xprt_inactive().
378 sx_xlock(&xprt->xp_lock);
380 error = svc_vc_accept(xprt->xp_socket, &so);
382 if (error == EWOULDBLOCK) {
384 * We must re-test for new connections after taking
385 * the lock to protect us in the case where a new
386 * connection arrives after our call to accept fails
389 SOLISTEN_LOCK(xprt->xp_socket);
390 if (TAILQ_EMPTY(&xprt->xp_socket->sol_comp))
391 xprt_inactive_self(xprt);
392 SOLISTEN_UNLOCK(xprt->xp_socket);
393 sx_xunlock(&xprt->xp_lock);
398 SOLISTEN_LOCK(xprt->xp_socket);
399 if (xprt->xp_upcallset) {
400 xprt->xp_upcallset = 0;
401 soupcall_clear(xprt->xp_socket, SO_RCV);
403 SOLISTEN_UNLOCK(xprt->xp_socket);
404 xprt_inactive_self(xprt);
405 sx_xunlock(&xprt->xp_lock);
409 sx_xunlock(&xprt->xp_lock);
412 error = soaccept(so, &sa);
416 * XXX not sure if I need to call sofree or soclose here.
424 * svc_vc_create_conn will call xprt_register - we don't need
425 * to do anything with the new connection except derefence it.
427 new_xprt = svc_vc_create_conn(xprt->xp_pool, so, sa);
431 SVC_RELEASE(new_xprt);
436 return (FALSE); /* there is never an rpc msg to be processed */
440 static enum xprt_stat
441 svc_vc_rendezvous_stat(SVCXPRT *xprt)
448 svc_vc_destroy_common(SVCXPRT *xprt)
452 if (xprt->xp_socket) {
453 if ((xprt->xp_tls & (RPCTLS_FLAGS_HANDSHAKE |
454 RPCTLS_FLAGS_HANDSHFAIL)) != 0) {
455 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) {
457 * If the upcall fails, the socket has
458 * probably been closed via the rpctlssd
459 * daemon having crashed or been
460 * restarted, so just ignore returned stat.
462 rpctls_srv_disconnect(xprt->xp_sslsec,
463 xprt->xp_sslusec, xprt->xp_sslrefno,
464 xprt->xp_sslproc, &reterr);
466 /* Must sorele() to get rid of reference. */
467 CURVNET_SET(xprt->xp_socket->so_vnet);
468 sorele(xprt->xp_socket);
471 (void)soclose(xprt->xp_socket);
475 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
480 svc_vc_rendezvous_destroy(SVCXPRT *xprt)
483 SOLISTEN_LOCK(xprt->xp_socket);
484 if (xprt->xp_upcallset) {
485 xprt->xp_upcallset = 0;
486 solisten_upcall_set(xprt->xp_socket, NULL, NULL);
488 SOLISTEN_UNLOCK(xprt->xp_socket);
490 svc_vc_destroy_common(xprt);
494 svc_vc_destroy(SVCXPRT *xprt)
496 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
497 CLIENT *cl = (CLIENT *)xprt->xp_p2;
499 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
500 if (xprt->xp_upcallset) {
501 xprt->xp_upcallset = 0;
502 if (xprt->xp_socket->so_rcv.sb_upcall != NULL)
503 soupcall_clear(xprt->xp_socket, SO_RCV);
505 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
510 svc_vc_destroy_common(xprt);
515 m_freem(cd->mpending);
516 mem_free(cd, sizeof(*cd));
520 svc_vc_backchannel_destroy(SVCXPRT *xprt)
522 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
532 mem_free(cd, sizeof(*cd));
537 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
543 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
550 svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq, void *in)
556 static enum xprt_stat
557 svc_vc_stat(SVCXPRT *xprt)
561 cd = (struct cf_conn *)(xprt->xp_p1);
563 if (cd->strm_stat == XPRT_DIED)
566 if (cd->mreq != NULL && cd->resid == 0 && cd->eor)
567 return (XPRT_MOREREQS);
569 if (soreadable(xprt->xp_socket))
570 return (XPRT_MOREREQS);
576 svc_vc_ack(SVCXPRT *xprt, uint32_t *ack)
579 *ack = atomic_load_acq_32(&xprt->xp_snt_cnt);
580 *ack -= sbused(&xprt->xp_socket->so_snd);
584 static enum xprt_stat
585 svc_vc_backchannel_stat(SVCXPRT *xprt)
589 cd = (struct cf_conn *)(xprt->xp_p1);
591 if (cd->mreq != NULL)
592 return (XPRT_MOREREQS);
598 * If we have an mbuf chain in cd->mpending, try to parse a record from it,
599 * leaving the result in cd->mreq. If we don't have a complete record, leave
600 * the partial result in cd->mreq and try to read more from the socket.
603 svc_vc_process_pending(SVCXPRT *xprt)
605 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
606 struct socket *so = xprt->xp_socket;
610 * If cd->resid is non-zero, we have part of the
611 * record already, otherwise we are expecting a record
614 if (!cd->resid && cd->mpending) {
616 * See if there is enough data buffered to
617 * make up a record marker. Make sure we can
618 * handle the case where the record marker is
619 * split across more than one mbuf.
625 while (n < sizeof(uint32_t) && m) {
629 if (n < sizeof(uint32_t)) {
630 so->so_rcv.sb_lowat = sizeof(uint32_t) - n;
633 m_copydata(cd->mpending, 0, sizeof(header),
635 header = ntohl(header);
636 cd->eor = (header & 0x80000000) != 0;
637 cd->resid = header & 0x7fffffff;
638 m_adj(cd->mpending, sizeof(uint32_t));
642 * Start pulling off mbufs from cd->mpending
643 * until we either have a complete record or
644 * we run out of data. We use m_split to pull
645 * data - it will pull as much as possible and
646 * split the last mbuf if necessary.
648 while (cd->mpending && cd->resid) {
650 if (cd->mpending->m_next
651 || cd->mpending->m_len > cd->resid)
652 cd->mpending = m_split(cd->mpending,
653 cd->resid, M_WAITOK);
657 m_last(cd->mreq)->m_next = m;
661 cd->resid -= m->m_len;
667 * Block receive upcalls if we have more data pending,
668 * otherwise report our need.
671 so->so_rcv.sb_lowat = INT_MAX;
673 so->so_rcv.sb_lowat =
674 imax(1, imin(cd->resid, so->so_rcv.sb_hiwat / 2));
679 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg,
680 struct sockaddr **addrp, struct mbuf **mp)
682 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
684 struct mbuf *m, *ctrl;
685 struct socket* so = xprt->xp_socket;
688 uint32_t reterr, xid_plus_direction[2];
689 struct cmsghdr *cmsg;
690 struct tls_get_record tgr;
694 * Serialise access to the socket and our own record parsing
697 sx_xlock(&xprt->xp_lock);
700 /* If we have no request ready, check pending queue. */
701 while (cd->mpending &&
702 (cd->mreq == NULL || cd->resid != 0 || !cd->eor)) {
703 if (!svc_vc_process_pending(xprt))
707 /* Process and return complete request in cd->mreq. */
708 if (cd->mreq != NULL && cd->resid == 0 && cd->eor) {
711 * Now, check for a backchannel reply.
712 * The XID is in the first uint32_t of the reply
713 * and the message direction is the second one.
715 if ((cd->mreq->m_len >= sizeof(xid_plus_direction) ||
716 m_length(cd->mreq, NULL) >=
717 sizeof(xid_plus_direction)) &&
718 xprt->xp_p2 != NULL) {
719 m_copydata(cd->mreq, 0,
720 sizeof(xid_plus_direction),
721 (char *)xid_plus_direction);
722 xid_plus_direction[0] =
723 ntohl(xid_plus_direction[0]);
724 xid_plus_direction[1] =
725 ntohl(xid_plus_direction[1]);
726 /* Check message direction. */
727 if (xid_plus_direction[1] == REPLY) {
728 clnt_bck_svccall(xprt->xp_p2,
730 xid_plus_direction[0]);
736 xdrmbuf_create(&xdrs, cd->mreq, XDR_DECODE);
739 /* Check for next request in a pending queue. */
740 svc_vc_process_pending(xprt);
741 if (cd->mreq == NULL || cd->resid != 0) {
742 SOCKBUF_LOCK(&so->so_rcv);
744 xprt_inactive_self(xprt);
745 SOCKBUF_UNLOCK(&so->so_rcv);
748 sx_xunlock(&xprt->xp_lock);
750 if (! xdr_callmsg(&xdrs, msg)) {
756 *mp = xdrmbuf_getall(&xdrs);
763 * If receiving is disabled so that a TLS handshake can be
764 * done by the rpctlssd daemon, return FALSE here.
766 rcvflag = MSG_DONTWAIT;
767 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0)
768 rcvflag |= MSG_TLSAPPDATA;
770 if (xprt->xp_dontrcv) {
771 sx_xunlock(&xprt->xp_lock);
776 * The socket upcall calls xprt_active() which will eventually
777 * cause the server to call us here. We attempt to
778 * read as much as possible from the socket and put
779 * the result in cd->mpending. If the read fails,
780 * we have drained both cd->mpending and the socket so
781 * we can call xprt_inactive().
783 uio.uio_resid = 1000000000;
784 uio.uio_td = curthread;
786 error = soreceive(so, NULL, &uio, &m, &ctrl, &rcvflag);
788 if (error == EWOULDBLOCK) {
790 * We must re-test for readability after
791 * taking the lock to protect us in the case
792 * where a new packet arrives on the socket
793 * after our call to soreceive fails with
796 SOCKBUF_LOCK(&so->so_rcv);
798 xprt_inactive_self(xprt);
799 SOCKBUF_UNLOCK(&so->so_rcv);
800 sx_xunlock(&xprt->xp_lock);
805 * A return of ENXIO indicates that there is an
806 * alert record at the head of the
807 * socket's receive queue, for TLS connections.
808 * This record needs to be handled in userland
809 * via an SSL_read() call, so do an upcall to the daemon.
811 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0 &&
813 /* Disable reception. */
814 xprt->xp_dontrcv = TRUE;
815 sx_xunlock(&xprt->xp_lock);
816 ret = rpctls_srv_handlerecord(xprt->xp_sslsec,
817 xprt->xp_sslusec, xprt->xp_sslrefno,
818 xprt->xp_sslproc, &reterr);
819 sx_xlock(&xprt->xp_lock);
820 xprt->xp_dontrcv = FALSE;
821 if (ret != RPC_SUCCESS || reterr != RPCTLSERR_OK) {
823 * All we can do is soreceive() it and
826 rcvflag = MSG_DONTWAIT;
829 sx_xunlock(&xprt->xp_lock);
830 xprt_active(xprt); /* Harmless if already active. */
835 SOCKBUF_LOCK(&so->so_rcv);
836 if (xprt->xp_upcallset) {
837 xprt->xp_upcallset = 0;
838 soupcall_clear(so, SO_RCV);
840 SOCKBUF_UNLOCK(&so->so_rcv);
841 xprt_inactive_self(xprt);
842 cd->strm_stat = XPRT_DIED;
843 sx_xunlock(&xprt->xp_lock);
849 * EOF - the other end has closed the socket.
851 xprt_inactive_self(xprt);
852 cd->strm_stat = XPRT_DIED;
853 sx_xunlock(&xprt->xp_lock);
857 /* Process any record header(s). */
859 cmsg = mtod(ctrl, struct cmsghdr *);
860 if (cmsg->cmsg_type == TLS_GET_RECORD &&
861 cmsg->cmsg_len == CMSG_LEN(sizeof(tgr))) {
862 memcpy(&tgr, CMSG_DATA(cmsg), sizeof(tgr));
864 * TLS_RLTYPE_ALERT records should be handled
865 * since soreceive() would have returned
866 * ENXIO. Just throw any other
867 * non-TLS_RLTYPE_APP records away.
869 if (tgr.tls_type != TLS_RLTYPE_APP) {
872 rcvflag = MSG_DONTWAIT | MSG_TLSAPPDATA;
880 m_last(cd->mpending)->m_next = m;
887 svc_vc_backchannel_recv(SVCXPRT *xprt, struct rpc_msg *msg,
888 struct sockaddr **addrp, struct mbuf **mp)
890 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
895 sx_xlock(&xprt->xp_lock);
896 ct = (struct ct_data *)xprt->xp_p2;
898 sx_xunlock(&xprt->xp_lock);
901 mtx_lock(&ct->ct_lock);
904 xprt_inactive_self(xprt);
905 mtx_unlock(&ct->ct_lock);
906 sx_xunlock(&xprt->xp_lock);
909 cd->mreq = m->m_nextpkt;
910 mtx_unlock(&ct->ct_lock);
911 sx_xunlock(&xprt->xp_lock);
913 xdrmbuf_create(&xdrs, m, XDR_DECODE);
914 if (! xdr_callmsg(&xdrs, msg)) {
919 *mp = xdrmbuf_getall(&xdrs);
925 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg,
926 struct sockaddr *addr, struct mbuf *m, uint32_t *seq)
931 int error, len, maxextsiz;
937 * Leave space for record mark.
939 mrep = m_gethdr(M_WAITOK, MT_DATA);
940 mrep->m_data += sizeof(uint32_t);
942 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
944 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
945 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
946 if (!xdr_replymsg(&xdrs, msg))
949 xdrmbuf_append(&xdrs, m);
951 stat = xdr_replymsg(&xdrs, msg);
958 * Prepend a record marker containing the reply length.
960 M_PREPEND(mrep, sizeof(uint32_t), M_WAITOK);
961 len = mrep->m_pkthdr.len;
962 *mtod(mrep, uint32_t *) =
963 htonl(0x80000000 | (len - sizeof(uint32_t)));
965 /* For RPC-over-TLS, copy mrep to a chain of ext_pgs. */
966 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) {
968 * Copy the mbuf chain to a chain of
969 * ext_pgs mbuf(s) as required by KERN_TLS.
971 maxextsiz = TLS_MAX_MSG_SIZE_V10_2;
973 if (rpctls_getinfo(&maxlen, false, false))
974 maxextsiz = min(maxextsiz, maxlen);
976 mrep = _rpc_copym_into_ext_pgs(mrep, maxextsiz);
978 atomic_add_32(&xprt->xp_snd_cnt, len);
980 * sosend consumes mreq.
982 error = sosend(xprt->xp_socket, NULL, NULL, mrep, NULL,
985 atomic_add_rel_32(&xprt->xp_snt_cnt, len);
987 *seq = xprt->xp_snd_cnt;
990 atomic_subtract_32(&xprt->xp_snd_cnt, len);
1001 svc_vc_backchannel_reply(SVCXPRT *xprt, struct rpc_msg *msg,
1002 struct sockaddr *addr, struct mbuf *m, uint32_t *seq)
1008 int error, maxextsiz;
1014 * Leave space for record mark.
1016 mrep = m_gethdr(M_WAITOK, MT_DATA);
1017 mrep->m_data += sizeof(uint32_t);
1019 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
1021 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
1022 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
1023 if (!xdr_replymsg(&xdrs, msg))
1026 xdrmbuf_append(&xdrs, m);
1028 stat = xdr_replymsg(&xdrs, msg);
1035 * Prepend a record marker containing the reply length.
1037 M_PREPEND(mrep, sizeof(uint32_t), M_WAITOK);
1038 *mtod(mrep, uint32_t *) =
1039 htonl(0x80000000 | (mrep->m_pkthdr.len
1040 - sizeof(uint32_t)));
1042 /* For RPC-over-TLS, copy mrep to a chain of ext_pgs. */
1043 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) {
1045 * Copy the mbuf chain to a chain of
1046 * ext_pgs mbuf(s) as required by KERN_TLS.
1048 maxextsiz = TLS_MAX_MSG_SIZE_V10_2;
1050 if (rpctls_getinfo(&maxlen, false, false))
1051 maxextsiz = min(maxextsiz, maxlen);
1053 mrep = _rpc_copym_into_ext_pgs(mrep, maxextsiz);
1055 sx_xlock(&xprt->xp_lock);
1056 ct = (struct ct_data *)xprt->xp_p2;
1058 error = sosend(ct->ct_socket, NULL, NULL, mrep, NULL,
1062 sx_xunlock(&xprt->xp_lock);
1083 svc_vc_soupcall(struct socket *so, void *arg, int waitflag)
1085 SVCXPRT *xprt = (SVCXPRT *) arg;
1087 if (soreadable(xprt->xp_socket))
1093 svc_vc_rendezvous_soupcall(struct socket *head, void *arg, int waitflag)
1095 SVCXPRT *xprt = (SVCXPRT *) arg;
1097 if (!TAILQ_EMPTY(&head->sol_comp))
1104 * Get the effective UID of the sending process. Used by rpcbind, keyserv
1105 * and rpc.yppasswdd on AF_LOCAL.
1108 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
1112 struct sockaddr *sa;
1114 sock = transp->xp_fd;
1115 sa = (struct sockaddr *)transp->xp_rtaddr;
1116 if (sa->sa_family == AF_LOCAL) {
1117 ret = getpeereid(sock, &euid, &egid);