1/* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */ 2 3/*- 4 * Copyright (c) 2009, Sun Microsystems, Inc. 5 * All rights reserved. 6 * 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. 17 * 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. 29 */ 30 31#if defined(LIBC_SCCS) && !defined(lint) 32static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro"; 33static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC"; 34static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro"; 35#endif 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD$"); 38 39/* 40 * clnt_tcp.c, Implements a TCP/IP based, client side RPC. 41 * 42 * Copyright (C) 1984, Sun Microsystems, Inc. 43 * 44 * TCP based RPC supports 'batched calls'. 45 * A sequence of calls may be batched-up in a send buffer. The rpc call 46 * return immediately to the client even though the call was not necessarily 47 * sent. The batching occurs if the results' xdr routine is NULL (0) AND 48 * the rpc timeout value is zero (see clnt.h, rpc). 49 * 50 * Clients should NOT casually batch calls that in fact return results; that is, 51 * the server side should be aware that a call is batched and not produce any 52 * return message. Batched calls that produce many result messages can 53 * deadlock (netlock) the client and the server.... 54 * 55 * Now go hang yourself. 56 */ 57 58#include <sys/param.h> 59#include <sys/systm.h> 60#include <sys/lock.h> 61#include <sys/malloc.h> 62#include <sys/mbuf.h> 63#include <sys/mutex.h> 64#include <sys/pcpu.h> 65#include <sys/proc.h> 66#include <sys/protosw.h> 67#include <sys/socket.h> 68#include <sys/socketvar.h> 69#include <sys/sx.h> 70#include <sys/syslog.h> 71#include <sys/time.h> 72#include <sys/uio.h> 73 74#include <net/vnet.h> 75 76#include <netinet/tcp.h> 77 78#include <rpc/rpc.h> 79#include <rpc/rpc_com.h> 80#include <rpc/krpc.h> 81 82struct cmessage { 83 struct cmsghdr cmsg; 84 struct cmsgcred cmcred; 85}; 86 87static enum clnt_stat clnt_vc_call(CLIENT *, struct rpc_callextra *, 88 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval); 89static void clnt_vc_geterr(CLIENT *, struct rpc_err *); 90static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *); 91static void clnt_vc_abort(CLIENT *); 92static bool_t clnt_vc_control(CLIENT *, u_int, void *); 93static void clnt_vc_close(CLIENT *); 94static void clnt_vc_destroy(CLIENT *); 95static bool_t time_not_ok(struct timeval *); 96static int clnt_vc_soupcall(struct socket *so, void *arg, int waitflag); 97 98static struct clnt_ops clnt_vc_ops = { 99 .cl_call = clnt_vc_call, 100 .cl_abort = clnt_vc_abort, 101 .cl_geterr = clnt_vc_geterr, 102 .cl_freeres = clnt_vc_freeres, 103 .cl_close = clnt_vc_close, 104 .cl_destroy = clnt_vc_destroy, 105 .cl_control = clnt_vc_control 106}; 107 108static void clnt_vc_upcallsdone(struct ct_data *); 109 110/* 111 * Create a client handle for a connection. 112 * Default options are set, which the user can change using clnt_control()'s. 113 * The rpc/vc package does buffering similar to stdio, so the client 114 * must pick send and receive buffer sizes, 0 => use the default. 115 * NB: fd is copied into a private area. 116 * NB: The rpch->cl_auth is set null authentication. Caller may wish to 117 * set this something more useful. 118 * 119 * fd should be an open socket 120 */ 121CLIENT * 122clnt_vc_create( 123 struct socket *so, /* open file descriptor */ 124 struct sockaddr *raddr, /* servers address */ 125 const rpcprog_t prog, /* program number */ 126 const rpcvers_t vers, /* version number */ 127 size_t sendsz, /* buffer recv size */ 128 size_t recvsz, /* buffer send size */ 129 int intrflag) /* interruptible */ 130{ 131 CLIENT *cl; /* client handle */ 132 struct ct_data *ct = NULL; /* client handle */ 133 struct timeval now; 134 struct rpc_msg call_msg; 135 static uint32_t disrupt; 136 struct __rpc_sockinfo si; 137 XDR xdrs; 138 int error, interrupted, one = 1, sleep_flag; 139 struct sockopt sopt; 140 141 if (disrupt == 0) 142 disrupt = (uint32_t)(long)raddr; 143 144 cl = (CLIENT *)mem_alloc(sizeof (*cl)); 145 ct = (struct ct_data *)mem_alloc(sizeof (*ct)); 146 147 mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF); 148 ct->ct_threads = 0; 149 ct->ct_closing = FALSE; 150 ct->ct_closed = FALSE; 151 ct->ct_upcallrefs = 0; 152 153 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) { 154 error = soconnect(so, raddr, curthread); 155 SOCK_LOCK(so); 156 interrupted = 0; 157 sleep_flag = PSOCK; 158 if (intrflag != 0) 159 sleep_flag |= (PCATCH | PBDRY); 160 while ((so->so_state & SS_ISCONNECTING) 161 && so->so_error == 0) { 162 error = msleep(&so->so_timeo, SOCK_MTX(so), 163 sleep_flag, "connec", 0); 164 if (error) { 165 if (error == EINTR || error == ERESTART) 166 interrupted = 1; 167 break; 168 } 169 } 170 if (error == 0) { 171 error = so->so_error; 172 so->so_error = 0; 173 } 174 SOCK_UNLOCK(so); 175 if (error) { 176 if (!interrupted) 177 so->so_state &= ~SS_ISCONNECTING; 178 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 179 rpc_createerr.cf_error.re_errno = error; 180 goto err; 181 } 182 } 183 184 if (!__rpc_socket2sockinfo(so, &si)) { 185 goto err; 186 } 187 188 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 189 bzero(&sopt, sizeof(sopt)); 190 sopt.sopt_dir = SOPT_SET; 191 sopt.sopt_level = SOL_SOCKET; 192 sopt.sopt_name = SO_KEEPALIVE; 193 sopt.sopt_val = &one; 194 sopt.sopt_valsize = sizeof(one); 195 sosetopt(so, &sopt); 196 } 197 198 if (so->so_proto->pr_protocol == IPPROTO_TCP) { 199 bzero(&sopt, sizeof(sopt)); 200 sopt.sopt_dir = SOPT_SET; 201 sopt.sopt_level = IPPROTO_TCP; 202 sopt.sopt_name = TCP_NODELAY; 203 sopt.sopt_val = &one; 204 sopt.sopt_valsize = sizeof(one); 205 sosetopt(so, &sopt); 206 } 207 208 ct->ct_closeit = FALSE; 209 210 /* 211 * Set up private data struct 212 */ 213 ct->ct_socket = so; 214 ct->ct_wait.tv_sec = -1; 215 ct->ct_wait.tv_usec = -1; 216 memcpy(&ct->ct_addr, raddr, raddr->sa_len); 217 218 /* 219 * Initialize call message 220 */ 221 getmicrotime(&now); 222 ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now); 223 call_msg.rm_xid = ct->ct_xid; 224 call_msg.rm_direction = CALL; 225 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 226 call_msg.rm_call.cb_prog = (uint32_t)prog; 227 call_msg.rm_call.cb_vers = (uint32_t)vers; 228 229 /* 230 * pre-serialize the static part of the call msg and stash it away 231 */ 232 xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE, 233 XDR_ENCODE); 234 if (! xdr_callhdr(&xdrs, &call_msg)) { 235 if (ct->ct_closeit) { 236 soclose(ct->ct_socket); 237 } 238 goto err; 239 } 240 ct->ct_mpos = XDR_GETPOS(&xdrs); 241 XDR_DESTROY(&xdrs); 242 ct->ct_waitchan = "rpcrecv"; 243 ct->ct_waitflag = 0; 244 245 /* 246 * Create a client handle which uses xdrrec for serialization 247 * and authnone for authentication. 248 */ 249 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 250 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 251 error = soreserve(ct->ct_socket, sendsz, recvsz); 252 if (error != 0) { 253 if (ct->ct_closeit) { 254 soclose(ct->ct_socket); 255 } 256 goto err; 257 } 258 cl->cl_refs = 1; 259 cl->cl_ops = &clnt_vc_ops; 260 cl->cl_private = ct; 261 cl->cl_auth = authnone_create(); 262 263 SOCKBUF_LOCK(&ct->ct_socket->so_rcv); 264 soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct); 265 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv); 266 267 ct->ct_record = NULL; 268 ct->ct_record_resid = 0; 269 TAILQ_INIT(&ct->ct_pending); 270 return (cl); 271 272err: 273 if (ct) { 274 mtx_destroy(&ct->ct_lock); 275 mem_free(ct, sizeof (struct ct_data)); 276 } 277 if (cl) 278 mem_free(cl, sizeof (CLIENT)); 279 return ((CLIENT *)NULL); 280} 281 282static enum clnt_stat 283clnt_vc_call( 284 CLIENT *cl, /* client handle */ 285 struct rpc_callextra *ext, /* call metadata */ 286 rpcproc_t proc, /* procedure number */ 287 struct mbuf *args, /* pointer to args */ 288 struct mbuf **resultsp, /* pointer to results */ 289 struct timeval utimeout) 290{ 291 struct ct_data *ct = (struct ct_data *) cl->cl_private; 292 AUTH *auth; 293 struct rpc_err *errp; 294 enum clnt_stat stat; 295 XDR xdrs; 296 struct rpc_msg reply_msg; 297 bool_t ok; 298 int nrefreshes = 2; /* number of times to refresh cred */ 299 struct timeval timeout; 300 uint32_t xid; 301 struct mbuf *mreq = NULL, *results; 302 struct ct_request *cr; 303 int error; 304 305 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK); 306 307 mtx_lock(&ct->ct_lock); 308 309 if (ct->ct_closing || ct->ct_closed) { 310 mtx_unlock(&ct->ct_lock); 311 free(cr, M_RPC); 312 return (RPC_CANTSEND); 313 } 314 ct->ct_threads++; 315 316 if (ext) { 317 auth = ext->rc_auth; 318 errp = &ext->rc_err; 319 } else { 320 auth = cl->cl_auth; 321 errp = &ct->ct_error; 322 } 323 324 cr->cr_mrep = NULL; 325 cr->cr_error = 0; 326 327 if (ct->ct_wait.tv_usec == -1) { 328 timeout = utimeout; /* use supplied timeout */ 329 } else { 330 timeout = ct->ct_wait; /* use default timeout */ 331 } 332 333call_again: 334 mtx_assert(&ct->ct_lock, MA_OWNED); 335 336 ct->ct_xid++; 337 xid = ct->ct_xid; 338 339 mtx_unlock(&ct->ct_lock); 340 341 /* 342 * Leave space to pre-pend the record mark. 343 */ 344 MGETHDR(mreq, M_WAIT, MT_DATA); 345 mreq->m_data += sizeof(uint32_t); 346 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN, 347 ("RPC header too big")); 348 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos); 349 mreq->m_len = ct->ct_mpos; 350 351 /* 352 * The XID is the first thing in the request. 353 */ 354 *mtod(mreq, uint32_t *) = htonl(xid); 355 356 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE); 357 358 errp->re_status = stat = RPC_SUCCESS; 359 360 if ((! XDR_PUTINT32(&xdrs, &proc)) || 361 (! AUTH_MARSHALL(auth, xid, &xdrs, 362 m_copym(args, 0, M_COPYALL, M_WAITOK)))) { 363 errp->re_status = stat = RPC_CANTENCODEARGS; 364 mtx_lock(&ct->ct_lock); 365 goto out; 366 } 367 mreq->m_pkthdr.len = m_length(mreq, NULL); 368 369 /* 370 * Prepend a record marker containing the packet length. 371 */ 372 M_PREPEND(mreq, sizeof(uint32_t), M_WAIT); 373 *mtod(mreq, uint32_t *) = 374 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t))); 375 376 cr->cr_xid = xid; 377 mtx_lock(&ct->ct_lock); 378 /* 379 * Check to see if the other end has already started to close down 380 * the connection. The upcall will have set ct_error.re_status 381 * to RPC_CANTRECV if this is the case. 382 * If the other end starts to close down the connection after this 383 * point, it will be detected later when cr_error is checked, 384 * since the request is in the ct_pending queue. 385 */ 386 if (ct->ct_error.re_status == RPC_CANTRECV) { 387 if (errp != &ct->ct_error) { 388 errp->re_errno = ct->ct_error.re_errno; 389 errp->re_status = RPC_CANTRECV; 390 } 391 stat = RPC_CANTRECV; 392 goto out; 393 } 394 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link); 395 mtx_unlock(&ct->ct_lock); 396 397 /* 398 * sosend consumes mreq. 399 */ 400 error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread); 401 mreq = NULL; 402 if (error == EMSGSIZE) { 403 SOCKBUF_LOCK(&ct->ct_socket->so_snd); 404 sbwait(&ct->ct_socket->so_snd); 405 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd); 406 AUTH_VALIDATE(auth, xid, NULL, NULL); 407 mtx_lock(&ct->ct_lock); 408 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link); 409 goto call_again; 410 } 411 412 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL; 413 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf; 414 reply_msg.acpted_rply.ar_verf.oa_length = 0; 415 reply_msg.acpted_rply.ar_results.where = NULL; 416 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; 417 418 mtx_lock(&ct->ct_lock); 419 if (error) { 420 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link); 421 errp->re_errno = error; 422 errp->re_status = stat = RPC_CANTSEND; 423 goto out; 424 } 425 426 /* 427 * Check to see if we got an upcall while waiting for the 428 * lock. In both these cases, the request has been removed 429 * from ct->ct_pending. 430 */ 431 if (cr->cr_error) { 432 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link); 433 errp->re_errno = cr->cr_error; 434 errp->re_status = stat = RPC_CANTRECV; 435 goto out; 436 } 437 if (cr->cr_mrep) { 438 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link); 439 goto got_reply; 440 } 441 442 /* 443 * Hack to provide rpc-based message passing 444 */ 445 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { 446 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link); 447 errp->re_status = stat = RPC_TIMEDOUT; 448 goto out; 449 } 450 451 error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan, 452 tvtohz(&timeout)); 453 454 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link); 455 456 if (error) { 457 /* 458 * The sleep returned an error so our request is still 459 * on the list. Turn the error code into an 460 * appropriate client status. 461 */ 462 errp->re_errno = error; 463 switch (error) { 464 case EINTR: 465 case ERESTART: 466 stat = RPC_INTR; 467 break; 468 case EWOULDBLOCK: 469 stat = RPC_TIMEDOUT; 470 break; 471 default: 472 stat = RPC_CANTRECV; 473 } 474 errp->re_status = stat; 475 goto out; 476 } else { 477 /* 478 * We were woken up by the upcall. If the 479 * upcall had a receive error, report that, 480 * otherwise we have a reply. 481 */ 482 if (cr->cr_error) { 483 errp->re_errno = cr->cr_error; 484 errp->re_status = stat = RPC_CANTRECV; 485 goto out; 486 } 487 } 488 489got_reply: 490 /* 491 * Now decode and validate the response. We need to drop the 492 * lock since xdr_replymsg may end up sleeping in malloc. 493 */ 494 mtx_unlock(&ct->ct_lock); 495 496 if (ext && ext->rc_feedback) 497 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg); 498 499 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE); 500 ok = xdr_replymsg(&xdrs, &reply_msg); 501 cr->cr_mrep = NULL; 502 503 if (ok) { 504 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 505 (reply_msg.acpted_rply.ar_stat == SUCCESS)) 506 errp->re_status = stat = RPC_SUCCESS; 507 else 508 stat = _seterr_reply(&reply_msg, errp); 509 510 if (stat == RPC_SUCCESS) { 511 results = xdrmbuf_getall(&xdrs); 512 if (!AUTH_VALIDATE(auth, xid, 513 &reply_msg.acpted_rply.ar_verf, 514 &results)) { 515 errp->re_status = stat = RPC_AUTHERROR; 516 errp->re_why = AUTH_INVALIDRESP; 517 } else { 518 KASSERT(results, 519 ("auth validated but no result")); 520 *resultsp = results; 521 } 522 } /* end successful completion */ 523 /* 524 * If unsuccesful AND error is an authentication error 525 * then refresh credentials and try again, else break 526 */ 527 else if (stat == RPC_AUTHERROR) 528 /* maybe our credentials need to be refreshed ... */ 529 if (nrefreshes > 0 && 530 AUTH_REFRESH(auth, &reply_msg)) { 531 nrefreshes--; 532 XDR_DESTROY(&xdrs); 533 mtx_lock(&ct->ct_lock); 534 goto call_again; 535 } 536 /* end of unsuccessful completion */ 537 } /* end of valid reply message */ 538 else { 539 errp->re_status = stat = RPC_CANTDECODERES; 540 } 541 XDR_DESTROY(&xdrs); 542 mtx_lock(&ct->ct_lock); 543out: 544 mtx_assert(&ct->ct_lock, MA_OWNED); 545 546 KASSERT(stat != RPC_SUCCESS || *resultsp, 547 ("RPC_SUCCESS without reply")); 548 549 if (mreq) 550 m_freem(mreq); 551 if (cr->cr_mrep) 552 m_freem(cr->cr_mrep); 553 554 ct->ct_threads--; 555 if (ct->ct_closing) 556 wakeup(ct); 557 558 mtx_unlock(&ct->ct_lock); 559 560 if (auth && stat != RPC_SUCCESS) 561 AUTH_VALIDATE(auth, xid, NULL, NULL); 562 563 free(cr, M_RPC); 564 565 return (stat); 566} 567 568static void 569clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp) 570{ 571 struct ct_data *ct = (struct ct_data *) cl->cl_private; 572 573 *errp = ct->ct_error; 574} 575 576static bool_t 577clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr) 578{ 579 XDR xdrs; 580 bool_t dummy; 581 582 xdrs.x_op = XDR_FREE; 583 dummy = (*xdr_res)(&xdrs, res_ptr); 584 585 return (dummy); 586} 587 588/*ARGSUSED*/ 589static void 590clnt_vc_abort(CLIENT *cl) 591{ 592} 593 594static bool_t 595clnt_vc_control(CLIENT *cl, u_int request, void *info) 596{ 597 struct ct_data *ct = (struct ct_data *)cl->cl_private; 598 void *infop = info; 599 SVCXPRT *xprt; 600 601 mtx_lock(&ct->ct_lock); 602 603 switch (request) { 604 case CLSET_FD_CLOSE: 605 ct->ct_closeit = TRUE; 606 mtx_unlock(&ct->ct_lock); 607 return (TRUE); 608 case CLSET_FD_NCLOSE: 609 ct->ct_closeit = FALSE; 610 mtx_unlock(&ct->ct_lock); 611 return (TRUE); 612 default: 613 break; 614 } 615 616 /* for other requests which use info */ 617 if (info == NULL) { 618 mtx_unlock(&ct->ct_lock); 619 return (FALSE); 620 } 621 switch (request) { 622 case CLSET_TIMEOUT: 623 if (time_not_ok((struct timeval *)info)) { 624 mtx_unlock(&ct->ct_lock); 625 return (FALSE); 626 } 627 ct->ct_wait = *(struct timeval *)infop; 628 break; 629 case CLGET_TIMEOUT: 630 *(struct timeval *)infop = ct->ct_wait; 631 break; 632 case CLGET_SERVER_ADDR: 633 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len); 634 break; 635 case CLGET_SVC_ADDR: 636 /* 637 * Slightly different semantics to userland - we use 638 * sockaddr instead of netbuf. 639 */ 640 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len); 641 break; 642 case CLSET_SVC_ADDR: /* set to new address */ 643 mtx_unlock(&ct->ct_lock); 644 return (FALSE); 645 case CLGET_XID: 646 *(uint32_t *)info = ct->ct_xid; 647 break; 648 case CLSET_XID: 649 /* This will set the xid of the NEXT call */ 650 /* decrement by 1 as clnt_vc_call() increments once */ 651 ct->ct_xid = *(uint32_t *)info - 1; 652 break; 653 case CLGET_VERS: 654 /* 655 * This RELIES on the information that, in the call body, 656 * the version number field is the fifth field from the 657 * begining of the RPC header. MUST be changed if the 658 * call_struct is changed 659 */ 660 *(uint32_t *)info = 661 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc + 662 4 * BYTES_PER_XDR_UNIT)); 663 break; 664 665 case CLSET_VERS: 666 *(uint32_t *)(void *)(ct->ct_mcallc + 667 4 * BYTES_PER_XDR_UNIT) = 668 htonl(*(uint32_t *)info); 669 break; 670 671 case CLGET_PROG: 672 /* 673 * This RELIES on the information that, in the call body, 674 * the program number field is the fourth field from the 675 * begining of the RPC header. MUST be changed if the 676 * call_struct is changed 677 */ 678 *(uint32_t *)info = 679 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc + 680 3 * BYTES_PER_XDR_UNIT)); 681 break; 682 683 case CLSET_PROG: 684 *(uint32_t *)(void *)(ct->ct_mcallc + 685 3 * BYTES_PER_XDR_UNIT) = 686 htonl(*(uint32_t *)info); 687 break; 688 689 case CLSET_WAITCHAN: 690 ct->ct_waitchan = (const char *)info; 691 break; 692 693 case CLGET_WAITCHAN: 694 *(const char **) info = ct->ct_waitchan; 695 break; 696 697 case CLSET_INTERRUPTIBLE: 698 if (*(int *) info) 699 ct->ct_waitflag = PCATCH | PBDRY; 700 else 701 ct->ct_waitflag = 0; 702 break; 703 704 case CLGET_INTERRUPTIBLE: 705 if (ct->ct_waitflag) 706 *(int *) info = TRUE; 707 else 708 *(int *) info = FALSE; 709 break; 710 711 case CLSET_BACKCHANNEL: 712 xprt = (SVCXPRT *)info; 713 if (ct->ct_backchannelxprt == NULL) { 714 xprt->xp_p2 = ct; 715 ct->ct_backchannelxprt = xprt; 716 } 717 break; 718 719 default: 720 mtx_unlock(&ct->ct_lock); 721 return (FALSE); 722 } 723 724 mtx_unlock(&ct->ct_lock); 725 return (TRUE); 726} 727 728static void 729clnt_vc_close(CLIENT *cl) 730{ 731 struct ct_data *ct = (struct ct_data *) cl->cl_private; 732 struct ct_request *cr; 733 734 mtx_lock(&ct->ct_lock); 735 736 if (ct->ct_closed) { 737 mtx_unlock(&ct->ct_lock); 738 return; 739 } 740 741 if (ct->ct_closing) { 742 while (ct->ct_closing) 743 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0); 744 KASSERT(ct->ct_closed, ("client should be closed")); 745 mtx_unlock(&ct->ct_lock); 746 return; 747 } 748 749 if (ct->ct_socket) { 750 ct->ct_closing = TRUE; 751 mtx_unlock(&ct->ct_lock); 752 753 SOCKBUF_LOCK(&ct->ct_socket->so_rcv); 754 soupcall_clear(ct->ct_socket, SO_RCV); 755 clnt_vc_upcallsdone(ct); 756 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv); 757 758 /* 759 * Abort any pending requests and wait until everyone 760 * has finished with clnt_vc_call. 761 */ 762 mtx_lock(&ct->ct_lock); 763 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) { 764 cr->cr_xid = 0; 765 cr->cr_error = ESHUTDOWN; 766 wakeup(cr); 767 } 768 769 while (ct->ct_threads) 770 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0); 771 } 772 773 ct->ct_closing = FALSE; 774 ct->ct_closed = TRUE; 775 mtx_unlock(&ct->ct_lock); 776 wakeup(ct); 777} 778 779static void 780clnt_vc_destroy(CLIENT *cl) 781{ 782 struct ct_data *ct = (struct ct_data *) cl->cl_private; 783 struct socket *so = NULL; 784 SVCXPRT *xprt; 785 786 clnt_vc_close(cl); 787 788 mtx_lock(&ct->ct_lock); 789 xprt = ct->ct_backchannelxprt; 790 ct->ct_backchannelxprt = NULL; 791 if (xprt != NULL) { 792 mtx_unlock(&ct->ct_lock); /* To avoid a LOR. */ 793 sx_xlock(&xprt->xp_lock); 794 mtx_lock(&ct->ct_lock); 795 xprt->xp_p2 = NULL; 796 xprt_unregister(xprt); 797 } 798 799 if (ct->ct_socket) { 800 if (ct->ct_closeit) { 801 so = ct->ct_socket; 802 } 803 } 804 805 mtx_unlock(&ct->ct_lock); 806 if (xprt != NULL) { 807 sx_xunlock(&xprt->xp_lock); 808 SVC_RELEASE(xprt); 809 } 810 811 mtx_destroy(&ct->ct_lock); 812 if (so) { 813 soshutdown(so, SHUT_WR); 814 soclose(so); 815 } 816 mem_free(ct, sizeof(struct ct_data)); 817 if (cl->cl_netid && cl->cl_netid[0]) 818 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 819 if (cl->cl_tp && cl->cl_tp[0]) 820 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 821 mem_free(cl, sizeof(CLIENT)); 822} 823 824/* 825 * Make sure that the time is not garbage. -1 value is disallowed. 826 * Note this is different from time_not_ok in clnt_dg.c 827 */ 828static bool_t 829time_not_ok(struct timeval *t) 830{ 831 return (t->tv_sec <= -1 || t->tv_sec > 100000000 || 832 t->tv_usec <= -1 || t->tv_usec > 1000000); 833} 834 835int 836clnt_vc_soupcall(struct socket *so, void *arg, int waitflag) 837{ 838 struct ct_data *ct = (struct ct_data *) arg; 839 struct uio uio; 840 struct mbuf *m, *m2; 841 struct ct_request *cr; 842 int error, rcvflag, foundreq; 843 uint32_t xid_plus_direction[2], header; 844 bool_t do_read; 845 SVCXPRT *xprt; 846 struct cf_conn *cd; 847 848 CTASSERT(sizeof(xid_plus_direction) == 2 * sizeof(uint32_t)); 849 ct->ct_upcallrefs++; 850 uio.uio_td = curthread; 851 do { 852 /* 853 * If ct_record_resid is zero, we are waiting for a 854 * record mark. 855 */ 856 if (ct->ct_record_resid == 0) { 857 858 /* 859 * Make sure there is either a whole record 860 * mark in the buffer or there is some other 861 * error condition 862 */ 863 do_read = FALSE; 864 if (so->so_rcv.sb_cc >= sizeof(uint32_t) 865 || (so->so_rcv.sb_state & SBS_CANTRCVMORE) 866 || so->so_error) 867 do_read = TRUE; 868 869 if (!do_read) 870 break; 871 872 SOCKBUF_UNLOCK(&so->so_rcv); 873 uio.uio_resid = sizeof(uint32_t); 874 m = NULL; 875 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK; 876 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag); 877 SOCKBUF_LOCK(&so->so_rcv); 878 879 if (error == EWOULDBLOCK) 880 break; 881 882 /* 883 * If there was an error, wake up all pending 884 * requests. 885 */ 886 if (error || uio.uio_resid > 0) { 887 wakeup_all: 888 mtx_lock(&ct->ct_lock); 889 if (!error) { 890 /* 891 * We must have got EOF trying 892 * to read from the stream. 893 */ 894 error = ECONNRESET; 895 } 896 ct->ct_error.re_status = RPC_CANTRECV; 897 ct->ct_error.re_errno = error; 898 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) { 899 cr->cr_error = error; 900 wakeup(cr); 901 } 902 mtx_unlock(&ct->ct_lock); 903 break; 904 } 905 m_copydata(m, 0, sizeof(uint32_t), (char *)&header); 906 header = ntohl(header); 907 ct->ct_record = NULL; 908 ct->ct_record_resid = header & 0x7fffffff; 909 ct->ct_record_eor = ((header & 0x80000000) != 0); 910 m_freem(m); 911 } else { 912 /* 913 * Wait until the socket has the whole record 914 * buffered. 915 */ 916 do_read = FALSE; 917 if (so->so_rcv.sb_cc >= ct->ct_record_resid 918 || (so->so_rcv.sb_state & SBS_CANTRCVMORE) 919 || so->so_error) 920 do_read = TRUE; 921 922 if (!do_read) 923 break; 924 925 /* 926 * We have the record mark. Read as much as 927 * the socket has buffered up to the end of 928 * this record. 929 */ 930 SOCKBUF_UNLOCK(&so->so_rcv); 931 uio.uio_resid = ct->ct_record_resid; 932 m = NULL; 933 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK; 934 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag); 935 SOCKBUF_LOCK(&so->so_rcv); 936 937 if (error == EWOULDBLOCK) 938 break; 939 940 if (error || uio.uio_resid == ct->ct_record_resid) 941 goto wakeup_all; 942 943 /* 944 * If we have part of the record already, 945 * chain this bit onto the end. 946 */ 947 if (ct->ct_record) 948 m_last(ct->ct_record)->m_next = m; 949 else 950 ct->ct_record = m; 951 952 ct->ct_record_resid = uio.uio_resid; 953 954 /* 955 * If we have the entire record, see if we can 956 * match it to a request. 957 */ 958 if (ct->ct_record_resid == 0 959 && ct->ct_record_eor) { 960 /* 961 * The XID is in the first uint32_t of 962 * the reply and the message direction 963 * is the second one. 964 */ 965 if (ct->ct_record->m_len < 966 sizeof(xid_plus_direction) && 967 m_length(ct->ct_record, NULL) < 968 sizeof(xid_plus_direction)) { 969 m_freem(ct->ct_record); 970 break; 971 } 972 m_copydata(ct->ct_record, 0, 973 sizeof(xid_plus_direction), 974 (char *)xid_plus_direction); 975 xid_plus_direction[0] = 976 ntohl(xid_plus_direction[0]); 977 xid_plus_direction[1] = 978 ntohl(xid_plus_direction[1]); 979 /* Check message direction. */ 980 if (xid_plus_direction[1] == CALL) { 981 /* This is a backchannel request. */ 982 mtx_lock(&ct->ct_lock); 983 xprt = ct->ct_backchannelxprt; 984 if (xprt == NULL) { 985 mtx_unlock(&ct->ct_lock); 986 /* Just throw it away. */ 987 m_freem(ct->ct_record); 988 ct->ct_record = NULL; 989 } else { 990 cd = (struct cf_conn *) 991 xprt->xp_p1; 992 m2 = cd->mreq; 993 /* 994 * The requests are chained 995 * in the m_nextpkt list. 996 */ 997 while (m2 != NULL && 998 m2->m_nextpkt != NULL) 999 /* Find end of list. */ 1000 m2 = m2->m_nextpkt; 1001 if (m2 != NULL) 1002 m2->m_nextpkt = 1003 ct->ct_record; 1004 else 1005 cd->mreq = 1006 ct->ct_record; 1007 ct->ct_record->m_nextpkt = 1008 NULL; 1009 ct->ct_record = NULL; 1010 xprt_active(xprt); 1011 mtx_unlock(&ct->ct_lock); 1012 } 1013 } else { 1014 mtx_lock(&ct->ct_lock); 1015 foundreq = 0; 1016 TAILQ_FOREACH(cr, &ct->ct_pending, 1017 cr_link) { 1018 if (cr->cr_xid == 1019 xid_plus_direction[0]) { 1020 /* 1021 * This one 1022 * matches. We leave 1023 * the reply mbuf in 1024 * cr->cr_mrep. Set 1025 * the XID to zero so 1026 * that we will ignore 1027 * any duplicated 1028 * replies. 1029 */ 1030 cr->cr_xid = 0; 1031 cr->cr_mrep = 1032 ct->ct_record; 1033 cr->cr_error = 0; 1034 foundreq = 1; 1035 wakeup(cr); 1036 break; 1037 } 1038 } 1039 mtx_unlock(&ct->ct_lock); 1040 1041 if (!foundreq) 1042 m_freem(ct->ct_record); 1043 ct->ct_record = NULL; 1044 } 1045 } 1046 } 1047 } while (m); 1048 ct->ct_upcallrefs--; 1049 if (ct->ct_upcallrefs < 0) 1050 panic("rpcvc upcall refcnt"); 1051 if (ct->ct_upcallrefs == 0) 1052 wakeup(&ct->ct_upcallrefs); 1053 return (SU_OK); 1054} 1055 1056/* 1057 * Wait for all upcalls in progress to complete. 1058 */ 1059static void 1060clnt_vc_upcallsdone(struct ct_data *ct) 1061{ 1062 1063 SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv); 1064 1065 while (ct->ct_upcallrefs > 0) 1066 (void) msleep(&ct->ct_upcallrefs, 1067 SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0); 1068} 1069