1// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/fs/lockd/host.c
4 *
5 * Management for NLM peer hosts. The nlm_host struct is shared
6 * between client and server implementation. The only reason to
7 * do so is to reduce code bloat.
8 *
9 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
10 */
11
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/in.h>
15#include <linux/in6.h>
16#include <linux/sunrpc/clnt.h>
17#include <linux/sunrpc/addr.h>
18#include <linux/sunrpc/svc.h>
19#include <linux/lockd/lockd.h>
20#include <linux/mutex.h>
21
22#include <linux/sunrpc/svc_xprt.h>
23
24#include <net/ipv6.h>
25
26#include "netns.h"
27
28#define NLMDBG_FACILITY		NLMDBG_HOSTCACHE
29#define NLM_HOST_NRHASH		32
30#define NLM_HOST_REBIND		(60 * HZ)
31#define NLM_HOST_EXPIRE		(300 * HZ)
32#define NLM_HOST_COLLECT	(120 * HZ)
33
34static struct hlist_head	nlm_server_hosts[NLM_HOST_NRHASH];
35static struct hlist_head	nlm_client_hosts[NLM_HOST_NRHASH];
36
37#define for_each_host(host, chain, table) \
38	for ((chain) = (table); \
39	     (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \
40		hlist_for_each_entry((host), (chain), h_hash)
41
42#define for_each_host_safe(host, next, chain, table) \
43	for ((chain) = (table); \
44	     (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \
45		hlist_for_each_entry_safe((host), (next), \
46						(chain), h_hash)
47
48static unsigned long		nrhosts;
49static DEFINE_MUTEX(nlm_host_mutex);
50
51static void			nlm_gc_hosts(struct net *net);
52
53struct nlm_lookup_host_info {
54	const int		server;		/* search for server|client */
55	const struct sockaddr	*sap;		/* address to search for */
56	const size_t		salen;		/* it's length */
57	const unsigned short	protocol;	/* transport to search for*/
58	const u32		version;	/* NLM version to search for */
59	const char		*hostname;	/* remote's hostname */
60	const size_t		hostname_len;	/* it's length */
61	const int		noresvport;	/* use non-priv port */
62	struct net		*net;		/* network namespace to bind */
63	const struct cred	*cred;
64};
65
66/*
67 * Hash function must work well on big- and little-endian platforms
68 */
69static unsigned int __nlm_hash32(const __be32 n)
70{
71	unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
72	return hash ^ (hash >> 8);
73}
74
75static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
76{
77	const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
78	return __nlm_hash32(sin->sin_addr.s_addr);
79}
80
81static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
82{
83	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
84	const struct in6_addr addr = sin6->sin6_addr;
85	return __nlm_hash32(addr.s6_addr32[0]) ^
86	       __nlm_hash32(addr.s6_addr32[1]) ^
87	       __nlm_hash32(addr.s6_addr32[2]) ^
88	       __nlm_hash32(addr.s6_addr32[3]);
89}
90
91static unsigned int nlm_hash_address(const struct sockaddr *sap)
92{
93	unsigned int hash;
94
95	switch (sap->sa_family) {
96	case AF_INET:
97		hash = __nlm_hash_addr4(sap);
98		break;
99	case AF_INET6:
100		hash = __nlm_hash_addr6(sap);
101		break;
102	default:
103		hash = 0;
104	}
105	return hash & (NLM_HOST_NRHASH - 1);
106}
107
108/*
109 * Allocate and initialize an nlm_host.  Common to both client and server.
110 */
111static struct nlm_host *nlm_alloc_host(struct nlm_lookup_host_info *ni,
112				       struct nsm_handle *nsm)
113{
114	struct nlm_host *host = NULL;
115	unsigned long now = jiffies;
116
117	if (nsm != NULL)
118		refcount_inc(&nsm->sm_count);
119	else {
120		host = NULL;
121		nsm = nsm_get_handle(ni->net, ni->sap, ni->salen,
122					ni->hostname, ni->hostname_len);
123		if (unlikely(nsm == NULL)) {
124			dprintk("lockd: %s failed; no nsm handle\n",
125				__func__);
126			goto out;
127		}
128	}
129
130	host = kmalloc(sizeof(*host), GFP_KERNEL);
131	if (unlikely(host == NULL)) {
132		dprintk("lockd: %s failed; no memory\n", __func__);
133		nsm_release(nsm);
134		goto out;
135	}
136
137	memcpy(nlm_addr(host), ni->sap, ni->salen);
138	host->h_addrlen    = ni->salen;
139	rpc_set_port(nlm_addr(host), 0);
140	host->h_srcaddrlen = 0;
141
142	host->h_rpcclnt    = NULL;
143	host->h_name	   = nsm->sm_name;
144	host->h_version    = ni->version;
145	host->h_proto      = ni->protocol;
146	host->h_reclaiming = 0;
147	host->h_server     = ni->server;
148	host->h_noresvport = ni->noresvport;
149	host->h_inuse      = 0;
150	init_waitqueue_head(&host->h_gracewait);
151	init_rwsem(&host->h_rwsem);
152	host->h_state      = 0;
153	host->h_nsmstate   = 0;
154	host->h_pidcount   = 0;
155	refcount_set(&host->h_count, 1);
156	mutex_init(&host->h_mutex);
157	host->h_nextrebind = now + NLM_HOST_REBIND;
158	host->h_expires    = now + NLM_HOST_EXPIRE;
159	INIT_LIST_HEAD(&host->h_lockowners);
160	spin_lock_init(&host->h_lock);
161	INIT_LIST_HEAD(&host->h_granted);
162	INIT_LIST_HEAD(&host->h_reclaim);
163	host->h_nsmhandle  = nsm;
164	host->h_addrbuf    = nsm->sm_addrbuf;
165	host->net	   = ni->net;
166	host->h_cred	   = get_cred(ni->cred);
167	strscpy(host->nodename, utsname()->nodename, sizeof(host->nodename));
168
169out:
170	return host;
171}
172
173/*
174 * Destroy an nlm_host and free associated resources
175 *
176 * Caller must hold nlm_host_mutex.
177 */
178static void nlm_destroy_host_locked(struct nlm_host *host)
179{
180	struct rpc_clnt	*clnt;
181	struct lockd_net *ln = net_generic(host->net, lockd_net_id);
182
183	dprintk("lockd: destroy host %s\n", host->h_name);
184
185	hlist_del_init(&host->h_hash);
186
187	nsm_unmonitor(host);
188	nsm_release(host->h_nsmhandle);
189
190	clnt = host->h_rpcclnt;
191	if (clnt != NULL)
192		rpc_shutdown_client(clnt);
193	put_cred(host->h_cred);
194	kfree(host);
195
196	ln->nrhosts--;
197	nrhosts--;
198}
199
200/**
201 * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
202 * @sap: network address of server
203 * @salen: length of server address
204 * @protocol: transport protocol to use
205 * @version: NLM protocol version
206 * @hostname: '\0'-terminated hostname of server
207 * @noresvport: 1 if non-privileged port should be used
208 * @net: pointer to net namespace
209 * @cred: pointer to cred
210 *
211 * Returns an nlm_host structure that matches the passed-in
212 * [server address, transport protocol, NLM version, server hostname].
213 * If one doesn't already exist in the host cache, a new handle is
214 * created and returned.
215 */
216struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
217				     const size_t salen,
218				     const unsigned short protocol,
219				     const u32 version,
220				     const char *hostname,
221				     int noresvport,
222				     struct net *net,
223				     const struct cred *cred)
224{
225	struct nlm_lookup_host_info ni = {
226		.server		= 0,
227		.sap		= sap,
228		.salen		= salen,
229		.protocol	= protocol,
230		.version	= version,
231		.hostname	= hostname,
232		.hostname_len	= strlen(hostname),
233		.noresvport	= noresvport,
234		.net		= net,
235		.cred		= cred,
236	};
237	struct hlist_head *chain;
238	struct nlm_host	*host;
239	struct nsm_handle *nsm = NULL;
240	struct lockd_net *ln = net_generic(net, lockd_net_id);
241
242	dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
243			(hostname ? hostname : "<none>"), version,
244			(protocol == IPPROTO_UDP ? "udp" : "tcp"));
245
246	mutex_lock(&nlm_host_mutex);
247
248	chain = &nlm_client_hosts[nlm_hash_address(sap)];
249	hlist_for_each_entry(host, chain, h_hash) {
250		if (host->net != net)
251			continue;
252		if (!rpc_cmp_addr(nlm_addr(host), sap))
253			continue;
254
255		/* Same address. Share an NSM handle if we already have one */
256		if (nsm == NULL)
257			nsm = host->h_nsmhandle;
258
259		if (host->h_proto != protocol)
260			continue;
261		if (host->h_version != version)
262			continue;
263
264		nlm_get_host(host);
265		dprintk("lockd: %s found host %s (%s)\n", __func__,
266			host->h_name, host->h_addrbuf);
267		goto out;
268	}
269
270	host = nlm_alloc_host(&ni, nsm);
271	if (unlikely(host == NULL))
272		goto out;
273
274	hlist_add_head(&host->h_hash, chain);
275	ln->nrhosts++;
276	nrhosts++;
277
278	dprintk("lockd: %s created host %s (%s)\n", __func__,
279		host->h_name, host->h_addrbuf);
280
281out:
282	mutex_unlock(&nlm_host_mutex);
283	return host;
284}
285
286/**
287 * nlmclnt_release_host - release client nlm_host
288 * @host: nlm_host to release
289 *
290 */
291void nlmclnt_release_host(struct nlm_host *host)
292{
293	if (host == NULL)
294		return;
295
296	dprintk("lockd: release client host %s\n", host->h_name);
297
298	WARN_ON_ONCE(host->h_server);
299
300	if (refcount_dec_and_mutex_lock(&host->h_count, &nlm_host_mutex)) {
301		WARN_ON_ONCE(!list_empty(&host->h_lockowners));
302		WARN_ON_ONCE(!list_empty(&host->h_granted));
303		WARN_ON_ONCE(!list_empty(&host->h_reclaim));
304
305		nlm_destroy_host_locked(host);
306		mutex_unlock(&nlm_host_mutex);
307	}
308}
309
310/**
311 * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
312 * @rqstp: incoming NLM request
313 * @hostname: name of client host
314 * @hostname_len: length of client hostname
315 *
316 * Returns an nlm_host structure that matches the [client address,
317 * transport protocol, NLM version, client hostname] of the passed-in
318 * NLM request.  If one doesn't already exist in the host cache, a
319 * new handle is created and returned.
320 *
321 * Before possibly creating a new nlm_host, construct a sockaddr
322 * for a specific source address in case the local system has
323 * multiple network addresses.  The family of the address in
324 * rq_daddr is guaranteed to be the same as the family of the
325 * address in rq_addr, so it's safe to use the same family for
326 * the source address.
327 */
328struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
329				    const char *hostname,
330				    const size_t hostname_len)
331{
332	struct hlist_head *chain;
333	struct nlm_host	*host = NULL;
334	struct nsm_handle *nsm = NULL;
335	struct sockaddr *src_sap = svc_daddr(rqstp);
336	size_t src_len = rqstp->rq_daddrlen;
337	struct net *net = SVC_NET(rqstp);
338	struct nlm_lookup_host_info ni = {
339		.server		= 1,
340		.sap		= svc_addr(rqstp),
341		.salen		= rqstp->rq_addrlen,
342		.protocol	= rqstp->rq_prot,
343		.version	= rqstp->rq_vers,
344		.hostname	= hostname,
345		.hostname_len	= hostname_len,
346		.net		= net,
347	};
348	struct lockd_net *ln = net_generic(net, lockd_net_id);
349
350	dprintk("lockd: %s(host='%.*s', vers=%u, proto=%s)\n", __func__,
351			(int)hostname_len, hostname, rqstp->rq_vers,
352			(rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
353
354	mutex_lock(&nlm_host_mutex);
355
356	if (time_after_eq(jiffies, ln->next_gc))
357		nlm_gc_hosts(net);
358
359	chain = &nlm_server_hosts[nlm_hash_address(ni.sap)];
360	hlist_for_each_entry(host, chain, h_hash) {
361		if (host->net != net)
362			continue;
363		if (!rpc_cmp_addr(nlm_addr(host), ni.sap))
364			continue;
365
366		/* Same address. Share an NSM handle if we already have one */
367		if (nsm == NULL)
368			nsm = host->h_nsmhandle;
369
370		if (host->h_proto != ni.protocol)
371			continue;
372		if (host->h_version != ni.version)
373			continue;
374		if (!rpc_cmp_addr(nlm_srcaddr(host), src_sap))
375			continue;
376
377		/* Move to head of hash chain. */
378		hlist_del(&host->h_hash);
379		hlist_add_head(&host->h_hash, chain);
380
381		nlm_get_host(host);
382		dprintk("lockd: %s found host %s (%s)\n",
383			__func__, host->h_name, host->h_addrbuf);
384		goto out;
385	}
386
387	host = nlm_alloc_host(&ni, nsm);
388	if (unlikely(host == NULL))
389		goto out;
390
391	memcpy(nlm_srcaddr(host), src_sap, src_len);
392	host->h_srcaddrlen = src_len;
393	hlist_add_head(&host->h_hash, chain);
394	ln->nrhosts++;
395	nrhosts++;
396
397	refcount_inc(&host->h_count);
398
399	dprintk("lockd: %s created host %s (%s)\n",
400		__func__, host->h_name, host->h_addrbuf);
401
402out:
403	mutex_unlock(&nlm_host_mutex);
404	return host;
405}
406
407/**
408 * nlmsvc_release_host - release server nlm_host
409 * @host: nlm_host to release
410 *
411 * Host is destroyed later in nlm_gc_host().
412 */
413void nlmsvc_release_host(struct nlm_host *host)
414{
415	if (host == NULL)
416		return;
417
418	dprintk("lockd: release server host %s\n", host->h_name);
419
420	WARN_ON_ONCE(!host->h_server);
421	refcount_dec(&host->h_count);
422}
423
424/*
425 * Create the NLM RPC client for an NLM peer
426 */
427struct rpc_clnt *
428nlm_bind_host(struct nlm_host *host)
429{
430	struct rpc_clnt	*clnt;
431
432	dprintk("lockd: nlm_bind_host %s (%s)\n",
433			host->h_name, host->h_addrbuf);
434
435	/* Lock host handle */
436	mutex_lock(&host->h_mutex);
437
438	/* If we've already created an RPC client, check whether
439	 * RPC rebind is required
440	 */
441	if ((clnt = host->h_rpcclnt) != NULL) {
442		nlm_rebind_host(host);
443	} else {
444		unsigned long increment = nlmsvc_timeout;
445		struct rpc_timeout timeparms = {
446			.to_initval	= increment,
447			.to_increment	= increment,
448			.to_maxval	= increment * 6UL,
449			.to_retries	= 5U,
450		};
451		struct rpc_create_args args = {
452			.net		= host->net,
453			.protocol	= host->h_proto,
454			.address	= nlm_addr(host),
455			.addrsize	= host->h_addrlen,
456			.timeout	= &timeparms,
457			.servername	= host->h_name,
458			.program	= &nlm_program,
459			.version	= host->h_version,
460			.authflavor	= RPC_AUTH_UNIX,
461			.flags		= (RPC_CLNT_CREATE_NOPING |
462					   RPC_CLNT_CREATE_AUTOBIND |
463					   RPC_CLNT_CREATE_REUSEPORT),
464			.cred		= host->h_cred,
465		};
466
467		/*
468		 * lockd retries server side blocks automatically so we want
469		 * those to be soft RPC calls. Client side calls need to be
470		 * hard RPC tasks.
471		 */
472		if (!host->h_server)
473			args.flags |= RPC_CLNT_CREATE_HARDRTRY;
474		if (host->h_noresvport)
475			args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
476		if (host->h_srcaddrlen)
477			args.saddress = nlm_srcaddr(host);
478
479		clnt = rpc_create(&args);
480		if (!IS_ERR(clnt))
481			host->h_rpcclnt = clnt;
482		else {
483			printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
484			clnt = NULL;
485		}
486	}
487
488	mutex_unlock(&host->h_mutex);
489	return clnt;
490}
491
492/**
493 * nlm_rebind_host - If needed, force a portmap lookup of the peer's lockd port
494 * @host: NLM host handle for peer
495 *
496 * This is not needed when using a connection-oriented protocol, such as TCP.
497 * The existing autobind mechanism is sufficient to force a rebind when
498 * required, e.g. on connection state transitions.
499 */
500void
501nlm_rebind_host(struct nlm_host *host)
502{
503	if (host->h_proto != IPPROTO_UDP)
504		return;
505
506	if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
507		rpc_force_rebind(host->h_rpcclnt);
508		host->h_nextrebind = jiffies + NLM_HOST_REBIND;
509	}
510}
511
512/*
513 * Increment NLM host count
514 */
515struct nlm_host * nlm_get_host(struct nlm_host *host)
516{
517	if (host) {
518		dprintk("lockd: get host %s\n", host->h_name);
519		refcount_inc(&host->h_count);
520		host->h_expires = jiffies + NLM_HOST_EXPIRE;
521	}
522	return host;
523}
524
525static struct nlm_host *next_host_state(struct hlist_head *cache,
526					struct nsm_handle *nsm,
527					const struct nlm_reboot *info)
528{
529	struct nlm_host *host;
530	struct hlist_head *chain;
531
532	mutex_lock(&nlm_host_mutex);
533	for_each_host(host, chain, cache) {
534		if (host->h_nsmhandle == nsm
535		    && host->h_nsmstate != info->state) {
536			host->h_nsmstate = info->state;
537			host->h_state++;
538
539			nlm_get_host(host);
540			mutex_unlock(&nlm_host_mutex);
541			return host;
542		}
543	}
544
545	mutex_unlock(&nlm_host_mutex);
546	return NULL;
547}
548
549/**
550 * nlm_host_rebooted - Release all resources held by rebooted host
551 * @net:  network namespace
552 * @info: pointer to decoded results of NLM_SM_NOTIFY call
553 *
554 * We were notified that the specified host has rebooted.  Release
555 * all resources held by that peer.
556 */
557void nlm_host_rebooted(const struct net *net, const struct nlm_reboot *info)
558{
559	struct nsm_handle *nsm;
560	struct nlm_host	*host;
561
562	nsm = nsm_reboot_lookup(net, info);
563	if (unlikely(nsm == NULL))
564		return;
565
566	/* Mark all hosts tied to this NSM state as having rebooted.
567	 * We run the loop repeatedly, because we drop the host table
568	 * lock for this.
569	 * To avoid processing a host several times, we match the nsmstate.
570	 */
571	while ((host = next_host_state(nlm_server_hosts, nsm, info)) != NULL) {
572		nlmsvc_free_host_resources(host);
573		nlmsvc_release_host(host);
574	}
575	while ((host = next_host_state(nlm_client_hosts, nsm, info)) != NULL) {
576		nlmclnt_recovery(host);
577		nlmclnt_release_host(host);
578	}
579
580	nsm_release(nsm);
581}
582
583static void nlm_complain_hosts(struct net *net)
584{
585	struct hlist_head *chain;
586	struct nlm_host	*host;
587
588	if (net) {
589		struct lockd_net *ln = net_generic(net, lockd_net_id);
590
591		if (ln->nrhosts == 0)
592			return;
593		pr_warn("lockd: couldn't shutdown host module for net %x!\n",
594			net->ns.inum);
595		dprintk("lockd: %lu hosts left in net %x:\n", ln->nrhosts,
596			net->ns.inum);
597	} else {
598		if (nrhosts == 0)
599			return;
600		printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
601		dprintk("lockd: %lu hosts left:\n", nrhosts);
602	}
603
604	for_each_host(host, chain, nlm_server_hosts) {
605		if (net && host->net != net)
606			continue;
607		dprintk("       %s (cnt %d use %d exp %ld net %x)\n",
608			host->h_name, refcount_read(&host->h_count),
609			host->h_inuse, host->h_expires, host->net->ns.inum);
610	}
611}
612
613void
614nlm_shutdown_hosts_net(struct net *net)
615{
616	struct hlist_head *chain;
617	struct nlm_host	*host;
618
619	mutex_lock(&nlm_host_mutex);
620
621	/* First, make all hosts eligible for gc */
622	dprintk("lockd: nuking all hosts in net %x...\n",
623		net ? net->ns.inum : 0);
624	for_each_host(host, chain, nlm_server_hosts) {
625		if (net && host->net != net)
626			continue;
627		host->h_expires = jiffies - 1;
628		if (host->h_rpcclnt) {
629			rpc_shutdown_client(host->h_rpcclnt);
630			host->h_rpcclnt = NULL;
631		}
632		nlmsvc_free_host_resources(host);
633	}
634
635	/* Then, perform a garbage collection pass */
636	nlm_gc_hosts(net);
637	nlm_complain_hosts(net);
638	mutex_unlock(&nlm_host_mutex);
639}
640
641/*
642 * Shut down the hosts module.
643 * Note that this routine is called only at server shutdown time.
644 */
645void
646nlm_shutdown_hosts(void)
647{
648	dprintk("lockd: shutting down host module\n");
649	nlm_shutdown_hosts_net(NULL);
650}
651
652/*
653 * Garbage collect any unused NLM hosts.
654 * This GC combines reference counting for async operations with
655 * mark & sweep for resources held by remote clients.
656 */
657static void
658nlm_gc_hosts(struct net *net)
659{
660	struct hlist_head *chain;
661	struct hlist_node *next;
662	struct nlm_host	*host;
663
664	dprintk("lockd: host garbage collection for net %x\n",
665		net ? net->ns.inum : 0);
666	for_each_host(host, chain, nlm_server_hosts) {
667		if (net && host->net != net)
668			continue;
669		host->h_inuse = 0;
670	}
671
672	/* Mark all hosts that hold locks, blocks or shares */
673	nlmsvc_mark_resources(net);
674
675	for_each_host_safe(host, next, chain, nlm_server_hosts) {
676		if (net && host->net != net)
677			continue;
678		if (host->h_inuse || time_before(jiffies, host->h_expires)) {
679			dprintk("nlm_gc_hosts skipping %s "
680				"(cnt %d use %d exp %ld net %x)\n",
681				host->h_name, refcount_read(&host->h_count),
682				host->h_inuse, host->h_expires,
683				host->net->ns.inum);
684			continue;
685		}
686		if (refcount_dec_if_one(&host->h_count))
687			nlm_destroy_host_locked(host);
688	}
689
690	if (net) {
691		struct lockd_net *ln = net_generic(net, lockd_net_id);
692
693		ln->next_gc = jiffies + NLM_HOST_COLLECT;
694	}
695}
696