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