1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/ 5 * Authors: Doug Rabson <dfr@rabson.org> 6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/limits.h> 34 #include <sys/lock.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/mutex.h> 38 #include <sys/pcpu.h> 39 #include <sys/proc.h> 40 #include <sys/socket.h> 41 #include <sys/socketvar.h> 42 #include <sys/time.h> 43 #include <sys/uio.h> 44 45 #include <netinet/tcp.h> 46 47 #include <rpc/rpc.h> 48 #include <rpc/rpc_com.h> 49 #include <rpc/krpc.h> 50 #include <rpc/rpcsec_tls.h> 51 52 static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *, 53 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval); 54 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *); 55 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *); 56 static void clnt_reconnect_abort(CLIENT *); 57 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *); 58 static void clnt_reconnect_close(CLIENT *); 59 static void clnt_reconnect_destroy(CLIENT *); 60 61 static const struct clnt_ops clnt_reconnect_ops = { 62 .cl_call = clnt_reconnect_call, 63 .cl_abort = clnt_reconnect_abort, 64 .cl_geterr = clnt_reconnect_geterr, 65 .cl_freeres = clnt_reconnect_freeres, 66 .cl_close = clnt_reconnect_close, 67 .cl_destroy = clnt_reconnect_destroy, 68 .cl_control = clnt_reconnect_control 69 }; 70 71 static int fake_wchan; 72 73 CLIENT * 74 clnt_reconnect_create( 75 struct netconfig *nconf, /* network type */ 76 struct sockaddr *svcaddr, /* servers address */ 77 rpcprog_t program, /* program number */ 78 rpcvers_t version, /* version number */ 79 size_t sendsz, /* buffer recv size */ 80 size_t recvsz) /* buffer send size */ 81 { 82 CLIENT *cl = NULL; /* client handle */ 83 struct rc_data *rc = NULL; /* private data */ 84 85 if (svcaddr == NULL) { 86 rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 87 return (NULL); 88 } 89 90 cl = mem_alloc(sizeof (CLIENT)); 91 rc = mem_alloc(sizeof (*rc)); 92 mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF); 93 (void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len); 94 rc->rc_nconf = nconf; 95 rc->rc_prog = program; 96 rc->rc_vers = version; 97 rc->rc_sendsz = sendsz; 98 rc->rc_recvsz = recvsz; 99 rc->rc_timeout.tv_sec = -1; 100 rc->rc_timeout.tv_usec = -1; 101 rc->rc_retry.tv_sec = 3; 102 rc->rc_retry.tv_usec = 0; 103 rc->rc_retries = INT_MAX; 104 rc->rc_privport = FALSE; 105 rc->rc_waitchan = "rpcrecv"; 106 rc->rc_intr = 0; 107 rc->rc_connecting = FALSE; 108 rc->rc_closed = FALSE; 109 rc->rc_ucred = crdup(curthread->td_ucred); 110 rc->rc_client = NULL; 111 rc->rc_tls = false; 112 rc->rc_tlscertname = NULL; 113 rc->rc_reconcall = NULL; 114 rc->rc_reconarg = NULL; 115 116 cl->cl_refs = 1; 117 cl->cl_ops = &clnt_reconnect_ops; 118 cl->cl_private = (caddr_t)(void *)rc; 119 cl->cl_auth = authnone_create(); 120 cl->cl_tp = NULL; 121 cl->cl_netid = NULL; 122 return (cl); 123 } 124 125 static enum clnt_stat 126 clnt_reconnect_connect(CLIENT *cl) 127 { 128 struct thread *td = curthread; 129 struct rc_data *rc = (struct rc_data *)cl->cl_private; 130 struct socket *so; 131 enum clnt_stat stat; 132 int error; 133 int one = 1; 134 struct ucred *oldcred; 135 CLIENT *newclient = NULL; 136 uint32_t reterr; 137 138 mtx_lock(&rc->rc_lock); 139 while (rc->rc_connecting) { 140 error = msleep(rc, &rc->rc_lock, 141 rc->rc_intr ? PCATCH : 0, "rpcrecon", 0); 142 if (error) { 143 mtx_unlock(&rc->rc_lock); 144 return (RPC_INTR); 145 } 146 } 147 if (rc->rc_closed) { 148 mtx_unlock(&rc->rc_lock); 149 return (RPC_CANTSEND); 150 } 151 if (rc->rc_client) { 152 mtx_unlock(&rc->rc_lock); 153 return (RPC_SUCCESS); 154 } 155 156 /* 157 * My turn to attempt a connect. The rc_connecting variable 158 * serializes the following code sequence, so it is guaranteed 159 * that rc_client will still be NULL after it is re-locked below, 160 * since that is the only place it is set non-NULL. 161 */ 162 rc->rc_connecting = TRUE; 163 mtx_unlock(&rc->rc_lock); 164 165 oldcred = td->td_ucred; 166 td->td_ucred = rc->rc_ucred; 167 so = __rpc_nconf2socket(rc->rc_nconf); 168 if (!so) { 169 stat = rpc_createerr.cf_stat = RPC_TLIERROR; 170 rpc_createerr.cf_error.re_errno = 0; 171 td->td_ucred = oldcred; 172 goto out; 173 } 174 175 if (rc->rc_privport) 176 bindresvport(so, NULL); 177 178 if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS) 179 newclient = clnt_dg_create(so, 180 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers, 181 rc->rc_sendsz, rc->rc_recvsz); 182 else { 183 /* 184 * I do not believe a timeout of less than 1sec would make 185 * sense here since short delays can occur when a server is 186 * temporarily overloaded. 187 */ 188 if (rc->rc_timeout.tv_sec > 0 && rc->rc_timeout.tv_usec >= 0) { 189 error = so_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO, 190 &rc->rc_timeout, sizeof(struct timeval)); 191 if (error != 0) { 192 stat = rpc_createerr.cf_stat = RPC_CANTSEND; 193 rpc_createerr.cf_error.re_errno = error; 194 td->td_ucred = oldcred; 195 goto out; 196 } 197 } 198 newclient = clnt_vc_create(so, 199 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers, 200 rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr); 201 if (rc->rc_tls && newclient != NULL) { 202 CURVNET_SET(so->so_vnet); 203 stat = rpctls_connect(newclient, rc->rc_tlscertname, so, 204 &reterr); 205 CURVNET_RESTORE(); 206 if (stat != RPC_SUCCESS || reterr != RPCTLSERR_OK) { 207 if (stat == RPC_SUCCESS) 208 stat = RPC_FAILED; 209 stat = rpc_createerr.cf_stat = stat; 210 rpc_createerr.cf_error.re_errno = 0; 211 CLNT_CLOSE(newclient); 212 CLNT_RELEASE(newclient); 213 newclient = NULL; 214 td->td_ucred = oldcred; 215 goto out; 216 } 217 CLNT_CONTROL(newclient, CLSET_TLS, 218 &(int){RPCTLS_COMPLETE}); 219 } 220 if (newclient != NULL) { 221 int optval = 1; 222 223 (void)so_setsockopt(so, IPPROTO_TCP, TCP_USE_DDP, 224 &optval, sizeof(optval)); 225 } 226 if (newclient != NULL && rc->rc_reconcall != NULL) 227 (*rc->rc_reconcall)(newclient, rc->rc_reconarg, 228 rc->rc_ucred); 229 } 230 td->td_ucred = oldcred; 231 232 if (!newclient) { 233 soclose(so); 234 rc->rc_err = rpc_createerr.cf_error; 235 stat = rpc_createerr.cf_stat; 236 goto out; 237 } 238 239 CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0); 240 CLNT_CONTROL(newclient, CLSET_CONNECT, &one); 241 CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout); 242 CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry); 243 CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan); 244 CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr); 245 if (rc->rc_backchannel != NULL) 246 CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel); 247 stat = RPC_SUCCESS; 248 249 out: 250 mtx_lock(&rc->rc_lock); 251 KASSERT(rc->rc_client == NULL, ("rc_client not null")); 252 if (!rc->rc_closed) { 253 rc->rc_client = newclient; 254 newclient = NULL; 255 } 256 rc->rc_connecting = FALSE; 257 wakeup(rc); 258 mtx_unlock(&rc->rc_lock); 259 260 if (newclient) { 261 /* 262 * It has been closed, so discard the new client. 263 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot 264 * be called with the rc_lock mutex held, since they may 265 * msleep() while holding a different mutex. 266 */ 267 CLNT_CLOSE(newclient); 268 CLNT_RELEASE(newclient); 269 } 270 271 return (stat); 272 } 273 274 static enum clnt_stat 275 clnt_reconnect_call( 276 CLIENT *cl, /* client handle */ 277 struct rpc_callextra *ext, /* call metadata */ 278 rpcproc_t proc, /* procedure number */ 279 struct mbuf *args, /* pointer to args */ 280 struct mbuf **resultsp, /* pointer to results */ 281 struct timeval utimeout) 282 { 283 struct rc_data *rc = (struct rc_data *)cl->cl_private; 284 CLIENT *client; 285 enum clnt_stat stat; 286 int tries, error; 287 288 tries = 0; 289 do { 290 mtx_lock(&rc->rc_lock); 291 if (rc->rc_closed) { 292 mtx_unlock(&rc->rc_lock); 293 return (RPC_CANTSEND); 294 } 295 296 if (!rc->rc_client) { 297 mtx_unlock(&rc->rc_lock); 298 stat = clnt_reconnect_connect(cl); 299 if (stat == RPC_SYSTEMERROR) { 300 error = tsleep(&fake_wchan, 301 rc->rc_intr ? PCATCH : 0, "rpccon", hz); 302 if (error == EINTR || error == ERESTART) 303 return (RPC_INTR); 304 tries++; 305 if (tries >= rc->rc_retries) 306 return (stat); 307 continue; 308 } 309 if (stat != RPC_SUCCESS) 310 return (stat); 311 mtx_lock(&rc->rc_lock); 312 } 313 314 if (!rc->rc_client) { 315 mtx_unlock(&rc->rc_lock); 316 stat = RPC_FAILED; 317 continue; 318 } 319 CLNT_ACQUIRE(rc->rc_client); 320 client = rc->rc_client; 321 mtx_unlock(&rc->rc_lock); 322 stat = CLNT_CALL_MBUF(client, ext, proc, args, 323 resultsp, utimeout); 324 325 if (stat != RPC_SUCCESS) { 326 if (!ext) 327 CLNT_GETERR(client, &rc->rc_err); 328 } 329 330 if (stat == RPC_TIMEDOUT) { 331 /* 332 * Check for async send misfeature for NLM 333 * protocol. 334 */ 335 if ((rc->rc_timeout.tv_sec == 0 336 && rc->rc_timeout.tv_usec == 0) 337 || (rc->rc_timeout.tv_sec == -1 338 && utimeout.tv_sec == 0 339 && utimeout.tv_usec == 0)) { 340 CLNT_RELEASE(client); 341 break; 342 } 343 } 344 345 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND 346 || stat == RPC_CANTRECV) { 347 tries++; 348 if (tries >= rc->rc_retries) { 349 CLNT_RELEASE(client); 350 break; 351 } 352 353 if (ext && ext->rc_feedback) 354 ext->rc_feedback(FEEDBACK_RECONNECT, proc, 355 ext->rc_feedback_arg); 356 357 mtx_lock(&rc->rc_lock); 358 /* 359 * Make sure that someone else hasn't already 360 * reconnected by checking if rc_client has changed. 361 * If not, we are done with the client and must 362 * do CLNT_RELEASE(client) twice to dispose of it, 363 * because there is both an initial refcnt and one 364 * acquired by CLNT_ACQUIRE() above. 365 */ 366 if (rc->rc_client == client) { 367 rc->rc_client = NULL; 368 mtx_unlock(&rc->rc_lock); 369 CLNT_RELEASE(client); 370 } else { 371 mtx_unlock(&rc->rc_lock); 372 } 373 CLNT_RELEASE(client); 374 } else { 375 CLNT_RELEASE(client); 376 break; 377 } 378 } while (stat != RPC_SUCCESS); 379 380 KASSERT(stat != RPC_SUCCESS || *resultsp, 381 ("RPC_SUCCESS without reply")); 382 383 return (stat); 384 } 385 386 static void 387 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp) 388 { 389 struct rc_data *rc = (struct rc_data *)cl->cl_private; 390 391 *errp = rc->rc_err; 392 } 393 394 /* 395 * Since this function requires that rc_client be valid, it can 396 * only be called when that is guaranteed to be the case. 397 */ 398 static bool_t 399 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr) 400 { 401 struct rc_data *rc = (struct rc_data *)cl->cl_private; 402 403 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr)); 404 } 405 406 /*ARGSUSED*/ 407 static void 408 clnt_reconnect_abort(CLIENT *h) 409 { 410 } 411 412 /* 413 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must 414 * always be called before CLNT_CALL_MBUF() by a single thread only. 415 */ 416 static bool_t 417 clnt_reconnect_control(CLIENT *cl, u_int request, void *info) 418 { 419 struct rc_data *rc = (struct rc_data *)cl->cl_private; 420 SVCXPRT *xprt; 421 size_t slen; 422 struct rpc_reconupcall *upcp; 423 424 if (info == NULL) { 425 return (FALSE); 426 } 427 switch (request) { 428 case CLSET_TIMEOUT: 429 rc->rc_timeout = *(struct timeval *)info; 430 if (rc->rc_client) 431 CLNT_CONTROL(rc->rc_client, request, info); 432 break; 433 434 case CLGET_TIMEOUT: 435 *(struct timeval *)info = rc->rc_timeout; 436 break; 437 438 case CLSET_RETRY_TIMEOUT: 439 rc->rc_retry = *(struct timeval *)info; 440 if (rc->rc_client) 441 CLNT_CONTROL(rc->rc_client, request, info); 442 break; 443 444 case CLGET_RETRY_TIMEOUT: 445 *(struct timeval *)info = rc->rc_retry; 446 break; 447 448 case CLGET_VERS: 449 *(uint32_t *)info = rc->rc_vers; 450 break; 451 452 case CLSET_VERS: 453 rc->rc_vers = *(uint32_t *) info; 454 if (rc->rc_client) 455 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info); 456 break; 457 458 case CLGET_PROG: 459 *(uint32_t *)info = rc->rc_prog; 460 break; 461 462 case CLSET_PROG: 463 rc->rc_prog = *(uint32_t *) info; 464 if (rc->rc_client) 465 CLNT_CONTROL(rc->rc_client, request, info); 466 break; 467 468 case CLSET_WAITCHAN: 469 rc->rc_waitchan = (char *)info; 470 if (rc->rc_client) 471 CLNT_CONTROL(rc->rc_client, request, info); 472 break; 473 474 case CLGET_WAITCHAN: 475 *(const char **) info = rc->rc_waitchan; 476 break; 477 478 case CLSET_INTERRUPTIBLE: 479 rc->rc_intr = *(int *) info; 480 if (rc->rc_client) 481 CLNT_CONTROL(rc->rc_client, request, info); 482 break; 483 484 case CLGET_INTERRUPTIBLE: 485 *(int *) info = rc->rc_intr; 486 break; 487 488 case CLSET_RETRIES: 489 rc->rc_retries = *(int *) info; 490 break; 491 492 case CLGET_RETRIES: 493 *(int *) info = rc->rc_retries; 494 break; 495 496 case CLSET_PRIVPORT: 497 rc->rc_privport = *(int *) info; 498 break; 499 500 case CLGET_PRIVPORT: 501 *(int *) info = rc->rc_privport; 502 break; 503 504 case CLSET_BACKCHANNEL: 505 xprt = (SVCXPRT *)info; 506 xprt_register(xprt); 507 rc->rc_backchannel = info; 508 break; 509 510 case CLSET_TLS: 511 rc->rc_tls = true; 512 break; 513 514 case CLSET_TLSCERTNAME: 515 slen = strlen(info) + 1; 516 /* 517 * tlscertname with "key.pem" appended to it forms a file 518 * name. As such, the maximum allowable strlen(info) is 519 * NAME_MAX - 7. However, "slen" includes the nul termination 520 * byte so it can be up to NAME_MAX - 6. 521 */ 522 if (slen <= 1 || slen > NAME_MAX - 6) 523 return (FALSE); 524 rc->rc_tlscertname = mem_alloc(slen); 525 strlcpy(rc->rc_tlscertname, info, slen); 526 break; 527 528 case CLSET_RECONUPCALL: 529 upcp = (struct rpc_reconupcall *)info; 530 rc->rc_reconcall = upcp->call; 531 rc->rc_reconarg = upcp->arg; 532 break; 533 534 default: 535 return (FALSE); 536 } 537 538 return (TRUE); 539 } 540 541 static void 542 clnt_reconnect_close(CLIENT *cl) 543 { 544 struct rc_data *rc = (struct rc_data *)cl->cl_private; 545 CLIENT *client; 546 547 mtx_lock(&rc->rc_lock); 548 549 if (rc->rc_closed) { 550 mtx_unlock(&rc->rc_lock); 551 return; 552 } 553 554 rc->rc_closed = TRUE; 555 client = rc->rc_client; 556 rc->rc_client = NULL; 557 558 mtx_unlock(&rc->rc_lock); 559 560 if (client) { 561 CLNT_CLOSE(client); 562 CLNT_RELEASE(client); 563 } 564 } 565 566 static void 567 clnt_reconnect_destroy(CLIENT *cl) 568 { 569 struct rc_data *rc = (struct rc_data *)cl->cl_private; 570 SVCXPRT *xprt; 571 572 if (rc->rc_client) 573 CLNT_DESTROY(rc->rc_client); 574 if (rc->rc_backchannel) { 575 xprt = (SVCXPRT *)rc->rc_backchannel; 576 KASSERT(xprt->xp_socket == NULL, 577 ("clnt_reconnect_destroy: xp_socket not NULL")); 578 xprt_unregister(xprt); 579 SVC_RELEASE(xprt); 580 } 581 crfree(rc->rc_ucred); 582 mtx_destroy(&rc->rc_lock); 583 mem_free(rc->rc_tlscertname, 0); /* 0 ok, since arg. ignored. */ 584 mem_free(rc->rc_reconarg, 0); 585 mem_free(rc, sizeof(*rc)); 586 mem_free(cl, sizeof (CLIENT)); 587 } 588