xref: /openbmc/linux/fs/nfs/callback.c (revision 56b5b1c7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/nfs/callback.c
4  *
5  * Copyright (C) 2004 Trond Myklebust
6  *
7  * NFSv4 callback handling
8  */
9 
10 #include <linux/completion.h>
11 #include <linux/ip.h>
12 #include <linux/module.h>
13 #include <linux/sched/signal.h>
14 #include <linux/sunrpc/svc.h>
15 #include <linux/sunrpc/svcsock.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/errno.h>
18 #include <linux/mutex.h>
19 #include <linux/freezer.h>
20 #include <linux/kthread.h>
21 #include <linux/sunrpc/svcauth_gss.h>
22 #include <linux/sunrpc/bc_xprt.h>
23 
24 #include <net/inet_sock.h>
25 
26 #include "nfs4_fs.h"
27 #include "callback.h"
28 #include "internal.h"
29 #include "netns.h"
30 
31 #define NFSDBG_FACILITY NFSDBG_CALLBACK
32 
33 struct nfs_callback_data {
34 	unsigned int users;
35 	struct svc_serv *serv;
36 };
37 
38 static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1];
39 static DEFINE_MUTEX(nfs_callback_mutex);
40 static struct svc_program nfs4_callback_program;
41 
42 static int nfs4_callback_up_net(struct svc_serv *serv, struct net *net)
43 {
44 	const struct cred *cred = current_cred();
45 	int ret;
46 	struct nfs_net *nn = net_generic(net, nfs_net_id);
47 
48 	ret = svc_create_xprt(serv, "tcp", net, PF_INET,
49 				nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS,
50 				cred);
51 	if (ret <= 0)
52 		goto out_err;
53 	nn->nfs_callback_tcpport = ret;
54 	dprintk("NFS: Callback listener port = %u (af %u, net %x)\n",
55 		nn->nfs_callback_tcpport, PF_INET, net->ns.inum);
56 
57 	ret = svc_create_xprt(serv, "tcp", net, PF_INET6,
58 				nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS,
59 				cred);
60 	if (ret > 0) {
61 		nn->nfs_callback_tcpport6 = ret;
62 		dprintk("NFS: Callback listener port = %u (af %u, net %x)\n",
63 			nn->nfs_callback_tcpport6, PF_INET6, net->ns.inum);
64 	} else if (ret != -EAFNOSUPPORT)
65 		goto out_err;
66 	return 0;
67 
68 out_err:
69 	return (ret) ? ret : -ENOMEM;
70 }
71 
72 /*
73  * This is the NFSv4 callback kernel thread.
74  */
75 static int
76 nfs4_callback_svc(void *vrqstp)
77 {
78 	int err;
79 	struct svc_rqst *rqstp = vrqstp;
80 
81 	set_freezable();
82 
83 	while (!kthread_freezable_should_stop(NULL)) {
84 
85 		if (signal_pending(current))
86 			flush_signals(current);
87 		/*
88 		 * Listen for a request on the socket
89 		 */
90 		err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
91 		if (err == -EAGAIN || err == -EINTR)
92 			continue;
93 		svc_process(rqstp);
94 	}
95 	svc_exit_thread(rqstp);
96 	module_put_and_kthread_exit(0);
97 	return 0;
98 }
99 
100 #if defined(CONFIG_NFS_V4_1)
101 /*
102  * The callback service for NFSv4.1 callbacks
103  */
104 static int
105 nfs41_callback_svc(void *vrqstp)
106 {
107 	struct svc_rqst *rqstp = vrqstp;
108 	struct svc_serv *serv = rqstp->rq_server;
109 	struct rpc_rqst *req;
110 	int error;
111 	DEFINE_WAIT(wq);
112 
113 	set_freezable();
114 
115 	while (!kthread_freezable_should_stop(NULL)) {
116 
117 		if (signal_pending(current))
118 			flush_signals(current);
119 
120 		prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE);
121 		spin_lock_bh(&serv->sv_cb_lock);
122 		if (!list_empty(&serv->sv_cb_list)) {
123 			req = list_first_entry(&serv->sv_cb_list,
124 					struct rpc_rqst, rq_bc_list);
125 			list_del(&req->rq_bc_list);
126 			spin_unlock_bh(&serv->sv_cb_lock);
127 			finish_wait(&serv->sv_cb_waitq, &wq);
128 			dprintk("Invoking bc_svc_process()\n");
129 			error = bc_svc_process(serv, req, rqstp);
130 			dprintk("bc_svc_process() returned w/ error code= %d\n",
131 				error);
132 		} else {
133 			spin_unlock_bh(&serv->sv_cb_lock);
134 			if (!kthread_should_stop())
135 				schedule();
136 			finish_wait(&serv->sv_cb_waitq, &wq);
137 		}
138 	}
139 	svc_exit_thread(rqstp);
140 	module_put_and_kthread_exit(0);
141 	return 0;
142 }
143 
144 static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
145 		struct svc_serv *serv)
146 {
147 	if (minorversion)
148 		/*
149 		 * Save the svc_serv in the transport so that it can
150 		 * be referenced when the session backchannel is initialized
151 		 */
152 		xprt->bc_serv = serv;
153 }
154 #else
155 static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
156 		struct svc_serv *serv)
157 {
158 }
159 #endif /* CONFIG_NFS_V4_1 */
160 
161 static int nfs_callback_start_svc(int minorversion, struct rpc_xprt *xprt,
162 				  struct svc_serv *serv)
163 {
164 	int nrservs = nfs_callback_nr_threads;
165 	int ret;
166 
167 	nfs_callback_bc_serv(minorversion, xprt, serv);
168 
169 	if (nrservs < NFS4_MIN_NR_CALLBACK_THREADS)
170 		nrservs = NFS4_MIN_NR_CALLBACK_THREADS;
171 
172 	if (serv->sv_nrthreads == nrservs)
173 		return 0;
174 
175 	ret = svc_set_num_threads(serv, NULL, nrservs);
176 	if (ret) {
177 		svc_set_num_threads(serv, NULL, 0);
178 		return ret;
179 	}
180 	dprintk("nfs_callback_up: service started\n");
181 	return 0;
182 }
183 
184 static void nfs_callback_down_net(u32 minorversion, struct svc_serv *serv, struct net *net)
185 {
186 	struct nfs_net *nn = net_generic(net, nfs_net_id);
187 
188 	if (--nn->cb_users[minorversion])
189 		return;
190 
191 	dprintk("NFS: destroy per-net callback data; net=%x\n", net->ns.inum);
192 	svc_shutdown_net(serv, net);
193 }
194 
195 static int nfs_callback_up_net(int minorversion, struct svc_serv *serv,
196 			       struct net *net, struct rpc_xprt *xprt)
197 {
198 	struct nfs_net *nn = net_generic(net, nfs_net_id);
199 	int ret;
200 
201 	if (nn->cb_users[minorversion]++)
202 		return 0;
203 
204 	dprintk("NFS: create per-net callback data; net=%x\n", net->ns.inum);
205 
206 	ret = svc_bind(serv, net);
207 	if (ret < 0) {
208 		printk(KERN_WARNING "NFS: bind callback service failed\n");
209 		goto err_bind;
210 	}
211 
212 	ret = 0;
213 	if (!IS_ENABLED(CONFIG_NFS_V4_1) || minorversion == 0)
214 		ret = nfs4_callback_up_net(serv, net);
215 	else if (xprt->ops->bc_setup)
216 		set_bc_enabled(serv);
217 	else
218 		ret = -EPROTONOSUPPORT;
219 
220 	if (ret < 0) {
221 		printk(KERN_ERR "NFS: callback service start failed\n");
222 		goto err_socks;
223 	}
224 	return 0;
225 
226 err_socks:
227 	svc_rpcb_cleanup(serv, net);
228 err_bind:
229 	nn->cb_users[minorversion]--;
230 	dprintk("NFS: Couldn't create callback socket: err = %d; "
231 			"net = %x\n", ret, net->ns.inum);
232 	return ret;
233 }
234 
235 static const struct svc_serv_ops nfs40_cb_sv_ops = {
236 	.svo_function		= nfs4_callback_svc,
237 	.svo_enqueue_xprt	= svc_xprt_do_enqueue,
238 	.svo_module		= THIS_MODULE,
239 };
240 #if defined(CONFIG_NFS_V4_1)
241 static const struct svc_serv_ops nfs41_cb_sv_ops = {
242 	.svo_function		= nfs41_callback_svc,
243 	.svo_enqueue_xprt	= svc_xprt_do_enqueue,
244 	.svo_module		= THIS_MODULE,
245 };
246 
247 static const struct svc_serv_ops *nfs4_cb_sv_ops[] = {
248 	[0] = &nfs40_cb_sv_ops,
249 	[1] = &nfs41_cb_sv_ops,
250 };
251 #else
252 static const struct svc_serv_ops *nfs4_cb_sv_ops[] = {
253 	[0] = &nfs40_cb_sv_ops,
254 	[1] = NULL,
255 };
256 #endif
257 
258 static struct svc_serv *nfs_callback_create_svc(int minorversion)
259 {
260 	struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
261 	const struct svc_serv_ops *sv_ops;
262 	struct svc_serv *serv;
263 
264 	/*
265 	 * Check whether we're already up and running.
266 	 */
267 	if (cb_info->serv)
268 		return svc_get(cb_info->serv);
269 
270 	switch (minorversion) {
271 	case 0:
272 		sv_ops = nfs4_cb_sv_ops[0];
273 		break;
274 	default:
275 		sv_ops = nfs4_cb_sv_ops[1];
276 	}
277 
278 	if (sv_ops == NULL)
279 		return ERR_PTR(-ENOTSUPP);
280 
281 	/*
282 	 * Sanity check: if there's no task,
283 	 * we should be the first user ...
284 	 */
285 	if (cb_info->users)
286 		printk(KERN_WARNING "nfs_callback_create_svc: no kthread, %d users??\n",
287 			cb_info->users);
288 
289 	serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, sv_ops);
290 	if (!serv) {
291 		printk(KERN_ERR "nfs_callback_create_svc: create service failed\n");
292 		return ERR_PTR(-ENOMEM);
293 	}
294 	cb_info->serv = serv;
295 	/* As there is only one thread we need to over-ride the
296 	 * default maximum of 80 connections
297 	 */
298 	serv->sv_maxconn = 1024;
299 	dprintk("nfs_callback_create_svc: service created\n");
300 	return serv;
301 }
302 
303 /*
304  * Bring up the callback thread if it is not already up.
305  */
306 int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)
307 {
308 	struct svc_serv *serv;
309 	struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
310 	int ret;
311 	struct net *net = xprt->xprt_net;
312 
313 	mutex_lock(&nfs_callback_mutex);
314 
315 	serv = nfs_callback_create_svc(minorversion);
316 	if (IS_ERR(serv)) {
317 		ret = PTR_ERR(serv);
318 		goto err_create;
319 	}
320 
321 	ret = nfs_callback_up_net(minorversion, serv, net, xprt);
322 	if (ret < 0)
323 		goto err_net;
324 
325 	ret = nfs_callback_start_svc(minorversion, xprt, serv);
326 	if (ret < 0)
327 		goto err_start;
328 
329 	cb_info->users++;
330 err_net:
331 	if (!cb_info->users)
332 		cb_info->serv = NULL;
333 	svc_put(serv);
334 err_create:
335 	mutex_unlock(&nfs_callback_mutex);
336 	return ret;
337 
338 err_start:
339 	nfs_callback_down_net(minorversion, serv, net);
340 	dprintk("NFS: Couldn't create server thread; err = %d\n", ret);
341 	goto err_net;
342 }
343 
344 /*
345  * Kill the callback thread if it's no longer being used.
346  */
347 void nfs_callback_down(int minorversion, struct net *net)
348 {
349 	struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
350 	struct svc_serv *serv;
351 
352 	mutex_lock(&nfs_callback_mutex);
353 	serv = cb_info->serv;
354 	nfs_callback_down_net(minorversion, serv, net);
355 	cb_info->users--;
356 	if (cb_info->users == 0) {
357 		svc_get(serv);
358 		svc_set_num_threads(serv, NULL, 0);
359 		svc_put(serv);
360 		dprintk("nfs_callback_down: service destroyed\n");
361 		cb_info->serv = NULL;
362 	}
363 	mutex_unlock(&nfs_callback_mutex);
364 }
365 
366 /* Boolean check of RPC_AUTH_GSS principal */
367 int
368 check_gss_callback_principal(struct nfs_client *clp, struct svc_rqst *rqstp)
369 {
370 	char *p = rqstp->rq_cred.cr_principal;
371 
372 	if (rqstp->rq_authop->flavour != RPC_AUTH_GSS)
373 		return 1;
374 
375 	/* No RPC_AUTH_GSS on NFSv4.1 back channel yet */
376 	if (clp->cl_minorversion != 0)
377 		return 0;
378 	/*
379 	 * It might just be a normal user principal, in which case
380 	 * userspace won't bother to tell us the name at all.
381 	 */
382 	if (p == NULL)
383 		return 0;
384 
385 	/*
386 	 * Did we get the acceptor from userland during the SETCLIENID
387 	 * negotiation?
388 	 */
389 	if (clp->cl_acceptor)
390 		return !strcmp(p, clp->cl_acceptor);
391 
392 	/*
393 	 * Otherwise try to verify it using the cl_hostname. Note that this
394 	 * doesn't work if a non-canonical hostname was used in the devname.
395 	 */
396 
397 	/* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
398 
399 	if (memcmp(p, "nfs@", 4) != 0)
400 		return 0;
401 	p += 4;
402 	if (strcmp(p, clp->cl_hostname) != 0)
403 		return 0;
404 	return 1;
405 }
406 
407 /*
408  * pg_authenticate method for nfsv4 callback threads.
409  *
410  * The authflavor has been negotiated, so an incorrect flavor is a server
411  * bug. Deny packets with incorrect authflavor.
412  *
413  * All other checking done after NFS decoding where the nfs_client can be
414  * found in nfs4_callback_compound
415  */
416 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
417 {
418 	rqstp->rq_auth_stat = rpc_autherr_badcred;
419 
420 	switch (rqstp->rq_authop->flavour) {
421 	case RPC_AUTH_NULL:
422 		if (rqstp->rq_proc != CB_NULL)
423 			return SVC_DENIED;
424 		break;
425 	case RPC_AUTH_GSS:
426 		/* No RPC_AUTH_GSS support yet in NFSv4.1 */
427 		 if (svc_is_backchannel(rqstp))
428 			return SVC_DENIED;
429 	}
430 
431 	rqstp->rq_auth_stat = rpc_auth_ok;
432 	return SVC_OK;
433 }
434 
435 /*
436  * Define NFS4 callback program
437  */
438 static const struct svc_version *nfs4_callback_version[] = {
439 	[1] = &nfs4_callback_version1,
440 	[4] = &nfs4_callback_version4,
441 };
442 
443 static struct svc_stat nfs4_callback_stats;
444 
445 static struct svc_program nfs4_callback_program = {
446 	.pg_prog = NFS4_CALLBACK,			/* RPC service number */
447 	.pg_nvers = ARRAY_SIZE(nfs4_callback_version),	/* Number of entries */
448 	.pg_vers = nfs4_callback_version,		/* version table */
449 	.pg_name = "NFSv4 callback",			/* service name */
450 	.pg_class = "nfs",				/* authentication class */
451 	.pg_stats = &nfs4_callback_stats,
452 	.pg_authenticate = nfs_callback_authenticate,
453 	.pg_init_request = svc_generic_init_request,
454 	.pg_rpcbind_set	= svc_generic_rpcbind_set,
455 };
456