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