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