1 /* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 2009, Sun Microsystems, Inc. 7 * All rights reserved. 8 * 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. 19 * 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. 31 */ 32 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"; 36 #endif 37 #include <sys/cdefs.h> 38 /* 39 * svc_vc.c, Server side for Connection Oriented based RPC. 40 * 41 * Actually implements two flavors of transporter - 42 * a tcp rendezvouser (a listner and connection establisher) 43 * and a record/tcp stream. 44 */ 45 46 #include "opt_kern_tls.h" 47 48 #include <sys/param.h> 49 #include <sys/limits.h> 50 #include <sys/lock.h> 51 #include <sys/kernel.h> 52 #include <sys/ktls.h> 53 #include <sys/malloc.h> 54 #include <sys/mbuf.h> 55 #include <sys/mutex.h> 56 #include <sys/proc.h> 57 #include <sys/protosw.h> 58 #include <sys/queue.h> 59 #include <sys/socket.h> 60 #include <sys/socketvar.h> 61 #include <sys/sx.h> 62 #include <sys/systm.h> 63 #include <sys/uio.h> 64 65 #include <net/vnet.h> 66 67 #include <netinet/tcp.h> 68 69 #include <rpc/rpc.h> 70 #include <rpc/rpcsec_tls.h> 71 72 #include <rpc/krpc.h> 73 #include <rpc/rpc_com.h> 74 75 #include <security/mac/mac_framework.h> 76 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, 91 void *in); 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, 99 void *in); 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); 105 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 113 }; 114 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 122 }; 123 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 130 }; 131 132 /* 133 * Usage: 134 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size); 135 * 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. 140 * 141 * The filedescriptor passed in is expected to refer to a bound, but 142 * not yet connected socket. 143 * 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. 147 */ 148 SVCXPRT * 149 svc_vc_create(SVCPOOL *pool, struct socket *so, size_t sendsize, 150 size_t recvsize) 151 { 152 SVCXPRT *xprt; 153 struct sockaddr* sa; 154 int error; 155 156 SOCK_LOCK(so); 157 if (so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED)) { 158 SOCK_UNLOCK(so); 159 CURVNET_SET(so->so_vnet); 160 error = so->so_proto->pr_peeraddr(so, &sa); 161 CURVNET_RESTORE(); 162 if (error) 163 return (NULL); 164 xprt = svc_vc_create_conn(pool, so, sa); 165 free(sa, M_SONAME); 166 return (xprt); 167 } 168 SOCK_UNLOCK(so); 169 170 xprt = svc_xprt_alloc(); 171 sx_init(&xprt->xp_lock, "xprt->xp_lock"); 172 xprt->xp_pool = pool; 173 xprt->xp_socket = so; 174 xprt->xp_p1 = NULL; 175 xprt->xp_p2 = NULL; 176 xprt->xp_ops = &svc_vc_rendezvous_ops; 177 178 CURVNET_SET(so->so_vnet); 179 error = so->so_proto->pr_sockaddr(so, &sa); 180 CURVNET_RESTORE(); 181 if (error) { 182 goto cleanup_svc_vc_create; 183 } 184 185 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len); 186 free(sa, M_SONAME); 187 188 xprt_register(xprt); 189 190 solisten(so, -1, curthread); 191 192 SOLISTEN_LOCK(so); 193 xprt->xp_upcallset = 1; 194 solisten_upcall_set(so, svc_vc_rendezvous_soupcall, xprt); 195 SOLISTEN_UNLOCK(so); 196 197 return (xprt); 198 199 cleanup_svc_vc_create: 200 sx_destroy(&xprt->xp_lock); 201 svc_xprt_free(xprt); 202 203 return (NULL); 204 } 205 206 /* 207 * Create a new transport for a socket optained via soaccept(). 208 */ 209 SVCXPRT * 210 svc_vc_create_conn(SVCPOOL *pool, struct socket *so, struct sockaddr *raddr) 211 { 212 SVCXPRT *xprt; 213 struct cf_conn *cd; 214 struct sockaddr* sa = NULL; 215 struct sockopt opt; 216 int one = 1; 217 int error; 218 219 bzero(&opt, sizeof(struct sockopt)); 220 opt.sopt_dir = SOPT_SET; 221 opt.sopt_level = SOL_SOCKET; 222 opt.sopt_name = SO_KEEPALIVE; 223 opt.sopt_val = &one; 224 opt.sopt_valsize = sizeof(one); 225 error = sosetopt(so, &opt); 226 if (error) { 227 return (NULL); 228 } 229 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; 235 opt.sopt_val = &one; 236 opt.sopt_valsize = sizeof(one); 237 error = sosetopt(so, &opt); 238 if (error) { 239 return (NULL); 240 } 241 } 242 243 cd = mem_alloc(sizeof(*cd)); 244 cd->strm_stat = XPRT_IDLE; 245 246 xprt = svc_xprt_alloc(); 247 sx_init(&xprt->xp_lock, "xprt->xp_lock"); 248 xprt->xp_pool = pool; 249 xprt->xp_socket = so; 250 xprt->xp_p1 = cd; 251 xprt->xp_p2 = NULL; 252 xprt->xp_ops = &svc_vc_ops; 253 254 /* 255 * See http://www.connectathon.org/talks96/nfstcp.pdf - client 256 * has a 5 minute timer, server has a 6 minute timer. 257 */ 258 xprt->xp_idletimeout = 6 * 60; 259 260 memcpy(&xprt->xp_rtaddr, raddr, raddr->sa_len); 261 262 CURVNET_SET(so->so_vnet); 263 error = so->so_proto->pr_sockaddr(so, &sa); 264 CURVNET_RESTORE(); 265 if (error) 266 goto cleanup_svc_vc_create; 267 268 memcpy(&xprt->xp_ltaddr, sa, sa->sa_len); 269 free(sa, M_SONAME); 270 271 xprt_register(xprt); 272 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); 277 278 /* 279 * Throw the transport into the active list in case it already 280 * has some data buffered. 281 */ 282 sx_xlock(&xprt->xp_lock); 283 xprt_active(xprt); 284 sx_xunlock(&xprt->xp_lock); 285 286 return (xprt); 287 cleanup_svc_vc_create: 288 sx_destroy(&xprt->xp_lock); 289 svc_xprt_free(xprt); 290 mem_free(cd, sizeof(*cd)); 291 292 return (NULL); 293 } 294 295 /* 296 * Create a new transport for a backchannel on a clnt_vc socket. 297 */ 298 SVCXPRT * 299 svc_vc_create_backchannel(SVCPOOL *pool) 300 { 301 SVCXPRT *xprt = NULL; 302 struct cf_conn *cd = NULL; 303 304 cd = mem_alloc(sizeof(*cd)); 305 cd->strm_stat = XPRT_IDLE; 306 307 xprt = svc_xprt_alloc(); 308 sx_init(&xprt->xp_lock, "xprt->xp_lock"); 309 xprt->xp_pool = pool; 310 xprt->xp_socket = NULL; 311 xprt->xp_p1 = cd; 312 xprt->xp_p2 = NULL; 313 xprt->xp_ops = &svc_vc_backchannel_ops; 314 return (xprt); 315 } 316 317 /* 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 320 * call malloc). 321 */ 322 int 323 svc_vc_accept(struct socket *head, struct socket **sop) 324 { 325 struct socket *so; 326 int error = 0; 327 short nbio; 328 329 KASSERT(SOLISTENING(head), 330 ("%s: socket %p is not listening", __func__, head)); 331 332 #ifdef MAC 333 error = mac_socket_check_accept(curthread->td_ucred, head); 334 if (error != 0) 335 goto done; 336 #endif 337 /* 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. 343 */ 344 SOLISTEN_LOCK(head); 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); 349 if (error) 350 goto done; 351 352 so->so_state |= nbio; 353 *sop = so; 354 355 /* connection has been removed from the listen queue */ 356 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0); 357 done: 358 return (error); 359 } 360 361 /*ARGSUSED*/ 362 static bool_t 363 svc_vc_rendezvous_recv(SVCXPRT *xprt, struct rpc_msg *msg, 364 struct sockaddr **addrp, struct mbuf **mp) 365 { 366 struct socket *so = NULL; 367 struct sockaddr *sa = NULL; 368 int error; 369 SVCXPRT *new_xprt; 370 371 /* 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(). 377 */ 378 sx_xlock(&xprt->xp_lock); 379 380 error = svc_vc_accept(xprt->xp_socket, &so); 381 382 if (error == EWOULDBLOCK) { 383 /* 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 387 * with EWOULDBLOCK. 388 */ 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); 394 return (FALSE); 395 } 396 397 if (error) { 398 SOLISTEN_LOCK(xprt->xp_socket); 399 if (xprt->xp_upcallset) { 400 xprt->xp_upcallset = 0; 401 soupcall_clear(xprt->xp_socket, SO_RCV); 402 } 403 SOLISTEN_UNLOCK(xprt->xp_socket); 404 xprt_inactive_self(xprt); 405 sx_xunlock(&xprt->xp_lock); 406 return (FALSE); 407 } 408 409 sx_xunlock(&xprt->xp_lock); 410 411 sa = NULL; 412 error = soaccept(so, &sa); 413 414 if (error) { 415 /* 416 * XXX not sure if I need to call sofree or soclose here. 417 */ 418 if (sa) 419 free(sa, M_SONAME); 420 return (FALSE); 421 } 422 423 /* 424 * svc_vc_create_conn will call xprt_register - we don't need 425 * to do anything with the new connection except derefence it. 426 */ 427 new_xprt = svc_vc_create_conn(xprt->xp_pool, so, sa); 428 if (!new_xprt) { 429 soclose(so); 430 } else { 431 SVC_RELEASE(new_xprt); 432 } 433 434 free(sa, M_SONAME); 435 436 return (FALSE); /* there is never an rpc msg to be processed */ 437 } 438 439 /*ARGSUSED*/ 440 static enum xprt_stat 441 svc_vc_rendezvous_stat(SVCXPRT *xprt) 442 { 443 444 return (XPRT_IDLE); 445 } 446 447 static void 448 svc_vc_destroy_common(SVCXPRT *xprt) 449 { 450 uint32_t reterr; 451 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) { 456 /* 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. 461 */ 462 rpctls_srv_disconnect(xprt->xp_sslsec, 463 xprt->xp_sslusec, xprt->xp_sslrefno, 464 xprt->xp_sslproc, &reterr); 465 } 466 /* Must sorele() to get rid of reference. */ 467 CURVNET_SET(xprt->xp_socket->so_vnet); 468 sorele(xprt->xp_socket); 469 CURVNET_RESTORE(); 470 } else 471 (void)soclose(xprt->xp_socket); 472 } 473 474 if (xprt->xp_netid) 475 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1); 476 svc_xprt_free(xprt); 477 } 478 479 static void 480 svc_vc_rendezvous_destroy(SVCXPRT *xprt) 481 { 482 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); 487 } 488 SOLISTEN_UNLOCK(xprt->xp_socket); 489 490 svc_vc_destroy_common(xprt); 491 } 492 493 static void 494 svc_vc_destroy(SVCXPRT *xprt) 495 { 496 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1; 497 CLIENT *cl = (CLIENT *)xprt->xp_p2; 498 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); 504 } 505 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv); 506 507 if (cl != NULL) 508 CLNT_RELEASE(cl); 509 510 svc_vc_destroy_common(xprt); 511 512 if (cd->mreq) 513 m_freem(cd->mreq); 514 if (cd->mpending) 515 m_freem(cd->mpending); 516 mem_free(cd, sizeof(*cd)); 517 } 518 519 static void 520 svc_vc_backchannel_destroy(SVCXPRT *xprt) 521 { 522 struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1; 523 struct mbuf *m, *m2; 524 525 svc_xprt_free(xprt); 526 m = cd->mreq; 527 while (m != NULL) { 528 m2 = m; 529 m = m->m_nextpkt; 530 m_freem(m2); 531 } 532 mem_free(cd, sizeof(*cd)); 533 } 534 535 /*ARGSUSED*/ 536 static bool_t 537 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in) 538 { 539 return (FALSE); 540 } 541 542 static bool_t 543 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in) 544 { 545 546 return (FALSE); 547 } 548 549 static bool_t 550 svc_vc_backchannel_control(SVCXPRT *xprt, const u_int rq, void *in) 551 { 552 553 return (FALSE); 554 } 555 556 static enum xprt_stat 557 svc_vc_stat(SVCXPRT *xprt) 558 { 559 struct cf_conn *cd; 560 561 cd = (struct cf_conn *)(xprt->xp_p1); 562 563 if (cd->strm_stat == XPRT_DIED) 564 return (XPRT_DIED); 565 566 if (cd->mreq != NULL && cd->resid == 0 && cd->eor) 567 return (XPRT_MOREREQS); 568 569 if (soreadable(xprt->xp_socket)) 570 return (XPRT_MOREREQS); 571 572 return (XPRT_IDLE); 573 } 574 575 static bool_t 576 svc_vc_ack(SVCXPRT *xprt, uint32_t *ack) 577 { 578 579 *ack = atomic_load_acq_32(&xprt->xp_snt_cnt); 580 *ack -= sbused(&xprt->xp_socket->so_snd); 581 return (TRUE); 582 } 583 584 static enum xprt_stat 585 svc_vc_backchannel_stat(SVCXPRT *xprt) 586 { 587 struct cf_conn *cd; 588 589 cd = (struct cf_conn *)(xprt->xp_p1); 590 591 if (cd->mreq != NULL) 592 return (XPRT_MOREREQS); 593 594 return (XPRT_IDLE); 595 } 596 597 /* 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. 601 */ 602 static int 603 svc_vc_process_pending(SVCXPRT *xprt) 604 { 605 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1; 606 struct socket *so = xprt->xp_socket; 607 struct mbuf *m; 608 609 /* 610 * If cd->resid is non-zero, we have part of the 611 * record already, otherwise we are expecting a record 612 * marker. 613 */ 614 if (!cd->resid && cd->mpending) { 615 /* 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. 620 */ 621 size_t n = 0; 622 uint32_t header; 623 624 m = cd->mpending; 625 while (n < sizeof(uint32_t) && m) { 626 n += m->m_len; 627 m = m->m_next; 628 } 629 if (n < sizeof(uint32_t)) { 630 so->so_rcv.sb_lowat = sizeof(uint32_t) - n; 631 return (FALSE); 632 } 633 m_copydata(cd->mpending, 0, sizeof(header), 634 (char *)&header); 635 header = ntohl(header); 636 cd->eor = (header & 0x80000000) != 0; 637 cd->resid = header & 0x7fffffff; 638 m_adj(cd->mpending, sizeof(uint32_t)); 639 } 640 641 /* 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. 647 */ 648 while (cd->mpending && cd->resid) { 649 m = cd->mpending; 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); 654 else 655 cd->mpending = NULL; 656 if (cd->mreq) 657 m_last(cd->mreq)->m_next = m; 658 else 659 cd->mreq = m; 660 while (m) { 661 cd->resid -= m->m_len; 662 m = m->m_next; 663 } 664 } 665 666 /* 667 * Block receive upcalls if we have more data pending, 668 * otherwise report our need. 669 */ 670 if (cd->mpending) 671 so->so_rcv.sb_lowat = INT_MAX; 672 else 673 so->so_rcv.sb_lowat = 674 imax(1, imin(cd->resid, so->so_rcv.sb_hiwat / 2)); 675 return (TRUE); 676 } 677 678 static bool_t 679 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg, 680 struct sockaddr **addrp, struct mbuf **mp) 681 { 682 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1; 683 struct uio uio; 684 struct mbuf *m, *ctrl; 685 struct socket* so = xprt->xp_socket; 686 XDR xdrs; 687 int error, rcvflag; 688 uint32_t reterr, xid_plus_direction[2]; 689 struct cmsghdr *cmsg; 690 struct tls_get_record tgr; 691 enum clnt_stat ret; 692 693 /* 694 * Serialise access to the socket and our own record parsing 695 * state. 696 */ 697 sx_xlock(&xprt->xp_lock); 698 699 for (;;) { 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)) 704 break; 705 } 706 707 /* Process and return complete request in cd->mreq. */ 708 if (cd->mreq != NULL && cd->resid == 0 && cd->eor) { 709 710 /* 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. 714 */ 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, 729 cd->mreq, 730 xid_plus_direction[0]); 731 cd->mreq = NULL; 732 continue; 733 } 734 } 735 736 xdrmbuf_create(&xdrs, cd->mreq, XDR_DECODE); 737 cd->mreq = NULL; 738 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); 743 if (!soreadable(so)) 744 xprt_inactive_self(xprt); 745 SOCKBUF_UNLOCK(&so->so_rcv); 746 } 747 748 sx_xunlock(&xprt->xp_lock); 749 750 if (! xdr_callmsg(&xdrs, msg)) { 751 XDR_DESTROY(&xdrs); 752 return (FALSE); 753 } 754 755 *addrp = NULL; 756 *mp = xdrmbuf_getall(&xdrs); 757 XDR_DESTROY(&xdrs); 758 759 return (TRUE); 760 } 761 762 /* 763 * If receiving is disabled so that a TLS handshake can be 764 * done by the rpctlssd daemon, return FALSE here. 765 */ 766 rcvflag = MSG_DONTWAIT; 767 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) 768 rcvflag |= MSG_TLSAPPDATA; 769 tryagain: 770 if (xprt->xp_dontrcv) { 771 sx_xunlock(&xprt->xp_lock); 772 return (FALSE); 773 } 774 775 /* 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(). 782 */ 783 uio.uio_resid = 1000000000; 784 uio.uio_td = curthread; 785 ctrl = m = NULL; 786 error = soreceive(so, NULL, &uio, &m, &ctrl, &rcvflag); 787 788 if (error == EWOULDBLOCK) { 789 /* 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 794 * EWOULDBLOCK. 795 */ 796 SOCKBUF_LOCK(&so->so_rcv); 797 if (!soreadable(so)) 798 xprt_inactive_self(xprt); 799 SOCKBUF_UNLOCK(&so->so_rcv); 800 sx_xunlock(&xprt->xp_lock); 801 return (FALSE); 802 } 803 804 /* 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. 810 */ 811 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0 && 812 error == ENXIO) { 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) { 822 /* 823 * All we can do is soreceive() it and 824 * then toss it. 825 */ 826 rcvflag = MSG_DONTWAIT; 827 goto tryagain; 828 } 829 sx_xunlock(&xprt->xp_lock); 830 xprt_active(xprt); /* Harmless if already active. */ 831 return (FALSE); 832 } 833 834 if (error) { 835 SOCKBUF_LOCK(&so->so_rcv); 836 if (xprt->xp_upcallset) { 837 xprt->xp_upcallset = 0; 838 soupcall_clear(so, SO_RCV); 839 } 840 SOCKBUF_UNLOCK(&so->so_rcv); 841 xprt_inactive_self(xprt); 842 cd->strm_stat = XPRT_DIED; 843 sx_xunlock(&xprt->xp_lock); 844 return (FALSE); 845 } 846 847 if (!m) { 848 /* 849 * EOF - the other end has closed the socket. 850 */ 851 xprt_inactive_self(xprt); 852 cd->strm_stat = XPRT_DIED; 853 sx_xunlock(&xprt->xp_lock); 854 return (FALSE); 855 } 856 857 /* Process any record header(s). */ 858 if (ctrl != NULL) { 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)); 863 /* 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. 868 */ 869 if (tgr.tls_type != TLS_RLTYPE_APP) { 870 m_freem(m); 871 m_free(ctrl); 872 rcvflag = MSG_DONTWAIT | MSG_TLSAPPDATA; 873 goto tryagain; 874 } 875 } 876 m_free(ctrl); 877 } 878 879 if (cd->mpending) 880 m_last(cd->mpending)->m_next = m; 881 else 882 cd->mpending = m; 883 } 884 } 885 886 static bool_t 887 svc_vc_backchannel_recv(SVCXPRT *xprt, struct rpc_msg *msg, 888 struct sockaddr **addrp, struct mbuf **mp) 889 { 890 struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1; 891 struct ct_data *ct; 892 struct mbuf *m; 893 XDR xdrs; 894 895 sx_xlock(&xprt->xp_lock); 896 ct = (struct ct_data *)xprt->xp_p2; 897 if (ct == NULL) { 898 sx_xunlock(&xprt->xp_lock); 899 return (FALSE); 900 } 901 mtx_lock(&ct->ct_lock); 902 m = cd->mreq; 903 if (m == NULL) { 904 xprt_inactive_self(xprt); 905 mtx_unlock(&ct->ct_lock); 906 sx_xunlock(&xprt->xp_lock); 907 return (FALSE); 908 } 909 cd->mreq = m->m_nextpkt; 910 mtx_unlock(&ct->ct_lock); 911 sx_xunlock(&xprt->xp_lock); 912 913 xdrmbuf_create(&xdrs, m, XDR_DECODE); 914 if (! xdr_callmsg(&xdrs, msg)) { 915 XDR_DESTROY(&xdrs); 916 return (FALSE); 917 } 918 *addrp = NULL; 919 *mp = xdrmbuf_getall(&xdrs); 920 XDR_DESTROY(&xdrs); 921 return (TRUE); 922 } 923 924 static bool_t 925 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg, 926 struct sockaddr *addr, struct mbuf *m, uint32_t *seq) 927 { 928 XDR xdrs; 929 struct mbuf *mrep; 930 bool_t stat = TRUE; 931 int error, len, maxextsiz; 932 #ifdef KERN_TLS 933 u_int maxlen; 934 #endif 935 936 /* 937 * Leave space for record mark. 938 */ 939 mrep = m_gethdr(M_WAITOK, MT_DATA); 940 mrep->m_data += sizeof(uint32_t); 941 942 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE); 943 944 if (msg->rm_reply.rp_stat == MSG_ACCEPTED && 945 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) { 946 if (!xdr_replymsg(&xdrs, msg)) 947 stat = FALSE; 948 else 949 xdrmbuf_append(&xdrs, m); 950 } else { 951 stat = xdr_replymsg(&xdrs, msg); 952 } 953 954 if (stat) { 955 m_fixhdr(mrep); 956 957 /* 958 * Prepend a record marker containing the reply length. 959 */ 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))); 964 965 /* For RPC-over-TLS, copy mrep to a chain of ext_pgs. */ 966 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) { 967 /* 968 * Copy the mbuf chain to a chain of 969 * ext_pgs mbuf(s) as required by KERN_TLS. 970 */ 971 maxextsiz = TLS_MAX_MSG_SIZE_V10_2; 972 #ifdef KERN_TLS 973 if (rpctls_getinfo(&maxlen, false, false)) 974 maxextsiz = min(maxextsiz, maxlen); 975 #endif 976 mrep = _rpc_copym_into_ext_pgs(mrep, maxextsiz); 977 } 978 atomic_add_32(&xprt->xp_snd_cnt, len); 979 /* 980 * sosend consumes mreq. 981 */ 982 error = sosend(xprt->xp_socket, NULL, NULL, mrep, NULL, 983 0, curthread); 984 if (!error) { 985 atomic_add_rel_32(&xprt->xp_snt_cnt, len); 986 if (seq) 987 *seq = xprt->xp_snd_cnt; 988 stat = TRUE; 989 } else 990 atomic_subtract_32(&xprt->xp_snd_cnt, len); 991 } else { 992 m_freem(mrep); 993 } 994 995 XDR_DESTROY(&xdrs); 996 997 return (stat); 998 } 999 1000 static bool_t 1001 svc_vc_backchannel_reply(SVCXPRT *xprt, struct rpc_msg *msg, 1002 struct sockaddr *addr, struct mbuf *m, uint32_t *seq) 1003 { 1004 struct ct_data *ct; 1005 XDR xdrs; 1006 struct mbuf *mrep; 1007 bool_t stat = TRUE; 1008 int error, maxextsiz; 1009 #ifdef KERN_TLS 1010 u_int maxlen; 1011 #endif 1012 1013 /* 1014 * Leave space for record mark. 1015 */ 1016 mrep = m_gethdr(M_WAITOK, MT_DATA); 1017 mrep->m_data += sizeof(uint32_t); 1018 1019 xdrmbuf_create(&xdrs, mrep, XDR_ENCODE); 1020 1021 if (msg->rm_reply.rp_stat == MSG_ACCEPTED && 1022 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) { 1023 if (!xdr_replymsg(&xdrs, msg)) 1024 stat = FALSE; 1025 else 1026 xdrmbuf_append(&xdrs, m); 1027 } else { 1028 stat = xdr_replymsg(&xdrs, msg); 1029 } 1030 1031 if (stat) { 1032 m_fixhdr(mrep); 1033 1034 /* 1035 * Prepend a record marker containing the reply length. 1036 */ 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))); 1041 1042 /* For RPC-over-TLS, copy mrep to a chain of ext_pgs. */ 1043 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) { 1044 /* 1045 * Copy the mbuf chain to a chain of 1046 * ext_pgs mbuf(s) as required by KERN_TLS. 1047 */ 1048 maxextsiz = TLS_MAX_MSG_SIZE_V10_2; 1049 #ifdef KERN_TLS 1050 if (rpctls_getinfo(&maxlen, false, false)) 1051 maxextsiz = min(maxextsiz, maxlen); 1052 #endif 1053 mrep = _rpc_copym_into_ext_pgs(mrep, maxextsiz); 1054 } 1055 sx_xlock(&xprt->xp_lock); 1056 ct = (struct ct_data *)xprt->xp_p2; 1057 if (ct != NULL) 1058 error = sosend(ct->ct_socket, NULL, NULL, mrep, NULL, 1059 0, curthread); 1060 else 1061 error = EPIPE; 1062 sx_xunlock(&xprt->xp_lock); 1063 if (!error) { 1064 stat = TRUE; 1065 } 1066 } else { 1067 m_freem(mrep); 1068 } 1069 1070 XDR_DESTROY(&xdrs); 1071 1072 return (stat); 1073 } 1074 1075 static bool_t 1076 svc_vc_null(void) 1077 { 1078 1079 return (FALSE); 1080 } 1081 1082 static int 1083 svc_vc_soupcall(struct socket *so, void *arg, int waitflag) 1084 { 1085 SVCXPRT *xprt = (SVCXPRT *) arg; 1086 1087 if (soreadable(xprt->xp_socket)) 1088 xprt_active(xprt); 1089 return (SU_OK); 1090 } 1091 1092 static int 1093 svc_vc_rendezvous_soupcall(struct socket *head, void *arg, int waitflag) 1094 { 1095 SVCXPRT *xprt = (SVCXPRT *) arg; 1096 1097 if (!TAILQ_EMPTY(&head->sol_comp)) 1098 xprt_active(xprt); 1099 return (SU_OK); 1100 } 1101 1102 #if 0 1103 /* 1104 * Get the effective UID of the sending process. Used by rpcbind, keyserv 1105 * and rpc.yppasswdd on AF_LOCAL. 1106 */ 1107 int 1108 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) { 1109 int sock, ret; 1110 gid_t egid; 1111 uid_t euid; 1112 struct sockaddr *sa; 1113 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); 1118 if (ret == 0) 1119 *uid = euid; 1120 return (ret); 1121 } else 1122 return (-1); 1123 } 1124 #endif 1125