xref: /openbmc/linux/fs/nfs/mount_clnt.c (revision c8ec3743)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * In-kernel MOUNT protocol client
4  *
5  * Copyright (C) 1997, Olaf Kirch <okir@monad.swb.de>
6  */
7 
8 #include <linux/types.h>
9 #include <linux/socket.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/uio.h>
13 #include <linux/net.h>
14 #include <linux/in.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/sched.h>
17 #include <linux/nfs_fs.h>
18 #include "internal.h"
19 
20 #define NFSDBG_FACILITY	NFSDBG_MOUNT
21 
22 /*
23  * Defined by RFC 1094, section A.3; and RFC 1813, section 5.1.4
24  */
25 #define MNTPATHLEN		(1024)
26 
27 /*
28  * XDR data type sizes
29  */
30 #define encode_dirpath_sz	(1 + XDR_QUADLEN(MNTPATHLEN))
31 #define MNT_status_sz		(1)
32 #define MNT_fhs_status_sz	(1)
33 #define MNT_fhandle_sz		XDR_QUADLEN(NFS2_FHSIZE)
34 #define MNT_fhandle3_sz		(1 + XDR_QUADLEN(NFS3_FHSIZE))
35 #define MNT_authflav3_sz	(1 + NFS_MAX_SECFLAVORS)
36 
37 /*
38  * XDR argument and result sizes
39  */
40 #define MNT_enc_dirpath_sz	encode_dirpath_sz
41 #define MNT_dec_mountres_sz	(MNT_status_sz + MNT_fhandle_sz)
42 #define MNT_dec_mountres3_sz	(MNT_status_sz + MNT_fhandle_sz + \
43 				 MNT_authflav3_sz)
44 
45 /*
46  * Defined by RFC 1094, section A.5
47  */
48 enum {
49 	MOUNTPROC_NULL		= 0,
50 	MOUNTPROC_MNT		= 1,
51 	MOUNTPROC_DUMP		= 2,
52 	MOUNTPROC_UMNT		= 3,
53 	MOUNTPROC_UMNTALL	= 4,
54 	MOUNTPROC_EXPORT	= 5,
55 };
56 
57 /*
58  * Defined by RFC 1813, section 5.2
59  */
60 enum {
61 	MOUNTPROC3_NULL		= 0,
62 	MOUNTPROC3_MNT		= 1,
63 	MOUNTPROC3_DUMP		= 2,
64 	MOUNTPROC3_UMNT		= 3,
65 	MOUNTPROC3_UMNTALL	= 4,
66 	MOUNTPROC3_EXPORT	= 5,
67 };
68 
69 static const struct rpc_program mnt_program;
70 
71 /*
72  * Defined by OpenGroup XNFS Version 3W, chapter 8
73  */
74 enum mountstat {
75 	MNT_OK			= 0,
76 	MNT_EPERM		= 1,
77 	MNT_ENOENT		= 2,
78 	MNT_EACCES		= 13,
79 	MNT_EINVAL		= 22,
80 };
81 
82 static struct {
83 	u32 status;
84 	int errno;
85 } mnt_errtbl[] = {
86 	{ .status = MNT_OK,			.errno = 0,		},
87 	{ .status = MNT_EPERM,			.errno = -EPERM,	},
88 	{ .status = MNT_ENOENT,			.errno = -ENOENT,	},
89 	{ .status = MNT_EACCES,			.errno = -EACCES,	},
90 	{ .status = MNT_EINVAL,			.errno = -EINVAL,	},
91 };
92 
93 /*
94  * Defined by RFC 1813, section 5.1.5
95  */
96 enum mountstat3 {
97 	MNT3_OK			= 0,		/* no error */
98 	MNT3ERR_PERM		= 1,		/* Not owner */
99 	MNT3ERR_NOENT		= 2,		/* No such file or directory */
100 	MNT3ERR_IO		= 5,		/* I/O error */
101 	MNT3ERR_ACCES		= 13,		/* Permission denied */
102 	MNT3ERR_NOTDIR		= 20,		/* Not a directory */
103 	MNT3ERR_INVAL		= 22,		/* Invalid argument */
104 	MNT3ERR_NAMETOOLONG	= 63,		/* Filename too long */
105 	MNT3ERR_NOTSUPP		= 10004,	/* Operation not supported */
106 	MNT3ERR_SERVERFAULT	= 10006,	/* A failure on the server */
107 };
108 
109 static struct {
110 	u32 status;
111 	int errno;
112 } mnt3_errtbl[] = {
113 	{ .status = MNT3_OK,			.errno = 0,		},
114 	{ .status = MNT3ERR_PERM,		.errno = -EPERM,	},
115 	{ .status = MNT3ERR_NOENT,		.errno = -ENOENT,	},
116 	{ .status = MNT3ERR_IO,			.errno = -EIO,		},
117 	{ .status = MNT3ERR_ACCES,		.errno = -EACCES,	},
118 	{ .status = MNT3ERR_NOTDIR,		.errno = -ENOTDIR,	},
119 	{ .status = MNT3ERR_INVAL,		.errno = -EINVAL,	},
120 	{ .status = MNT3ERR_NAMETOOLONG,	.errno = -ENAMETOOLONG,	},
121 	{ .status = MNT3ERR_NOTSUPP,		.errno = -ENOTSUPP,	},
122 	{ .status = MNT3ERR_SERVERFAULT,	.errno = -EREMOTEIO,	},
123 };
124 
125 struct mountres {
126 	int errno;
127 	struct nfs_fh *fh;
128 	unsigned int *auth_count;
129 	rpc_authflavor_t *auth_flavors;
130 };
131 
132 struct mnt_fhstatus {
133 	u32 status;
134 	struct nfs_fh *fh;
135 };
136 
137 /**
138  * nfs_mount - Obtain an NFS file handle for the given host and path
139  * @info: pointer to mount request arguments
140  *
141  * Uses default timeout parameters specified by underlying transport. On
142  * successful return, the auth_flavs list and auth_flav_len will be populated
143  * with the list from the server or a faked-up list if the server didn't
144  * provide one.
145  */
146 int nfs_mount(struct nfs_mount_request *info)
147 {
148 	struct mountres	result = {
149 		.fh		= info->fh,
150 		.auth_count	= info->auth_flav_len,
151 		.auth_flavors	= info->auth_flavs,
152 	};
153 	struct rpc_message msg	= {
154 		.rpc_argp	= info->dirpath,
155 		.rpc_resp	= &result,
156 	};
157 	struct rpc_create_args args = {
158 		.net		= info->net,
159 		.protocol	= info->protocol,
160 		.address	= info->sap,
161 		.addrsize	= info->salen,
162 		.servername	= info->hostname,
163 		.program	= &mnt_program,
164 		.version	= info->version,
165 		.authflavor	= RPC_AUTH_UNIX,
166 	};
167 	struct rpc_clnt		*mnt_clnt;
168 	int			status;
169 
170 	dprintk("NFS: sending MNT request for %s:%s\n",
171 		(info->hostname ? info->hostname : "server"),
172 			info->dirpath);
173 
174 	if (strlen(info->dirpath) > MNTPATHLEN)
175 		return -ENAMETOOLONG;
176 
177 	if (info->noresvport)
178 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
179 
180 	mnt_clnt = rpc_create(&args);
181 	if (IS_ERR(mnt_clnt))
182 		goto out_clnt_err;
183 
184 	if (info->version == NFS_MNT3_VERSION)
185 		msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
186 	else
187 		msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC_MNT];
188 
189 	status = rpc_call_sync(mnt_clnt, &msg, RPC_TASK_SOFT|RPC_TASK_TIMEOUT);
190 	rpc_shutdown_client(mnt_clnt);
191 
192 	if (status < 0)
193 		goto out_call_err;
194 	if (result.errno != 0)
195 		goto out_mnt_err;
196 
197 	dprintk("NFS: MNT request succeeded\n");
198 	status = 0;
199 
200 	/*
201 	 * If the server didn't provide a flavor list, allow the
202 	 * client to try any flavor.
203 	 */
204 	if (info->version != NFS_MNT3_VERSION || *info->auth_flav_len == 0) {
205 		dprintk("NFS: Faking up auth_flavs list\n");
206 		info->auth_flavs[0] = RPC_AUTH_NULL;
207 		*info->auth_flav_len = 1;
208 	}
209 out:
210 	return status;
211 
212 out_clnt_err:
213 	status = PTR_ERR(mnt_clnt);
214 	dprintk("NFS: failed to create MNT RPC client, status=%d\n", status);
215 	goto out;
216 
217 out_call_err:
218 	dprintk("NFS: MNT request failed, status=%d\n", status);
219 	goto out;
220 
221 out_mnt_err:
222 	dprintk("NFS: MNT server returned result %d\n", result.errno);
223 	status = result.errno;
224 	goto out;
225 }
226 
227 /**
228  * nfs_umount - Notify a server that we have unmounted this export
229  * @info: pointer to umount request arguments
230  *
231  * MOUNTPROC_UMNT is advisory, so we set a short timeout, and always
232  * use UDP.
233  */
234 void nfs_umount(const struct nfs_mount_request *info)
235 {
236 	static const struct rpc_timeout nfs_umnt_timeout = {
237 		.to_initval = 1 * HZ,
238 		.to_maxval = 3 * HZ,
239 		.to_retries = 2,
240 	};
241 	struct rpc_create_args args = {
242 		.net		= info->net,
243 		.protocol	= IPPROTO_UDP,
244 		.address	= info->sap,
245 		.addrsize	= info->salen,
246 		.timeout	= &nfs_umnt_timeout,
247 		.servername	= info->hostname,
248 		.program	= &mnt_program,
249 		.version	= info->version,
250 		.authflavor	= RPC_AUTH_UNIX,
251 		.flags		= RPC_CLNT_CREATE_NOPING,
252 	};
253 	struct rpc_message msg	= {
254 		.rpc_argp	= info->dirpath,
255 	};
256 	struct rpc_clnt *clnt;
257 	int status;
258 
259 	if (strlen(info->dirpath) > MNTPATHLEN)
260 		return;
261 
262 	if (info->noresvport)
263 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
264 
265 	clnt = rpc_create(&args);
266 	if (IS_ERR(clnt))
267 		goto out_clnt_err;
268 
269 	dprintk("NFS: sending UMNT request for %s:%s\n",
270 		(info->hostname ? info->hostname : "server"), info->dirpath);
271 
272 	if (info->version == NFS_MNT3_VERSION)
273 		msg.rpc_proc = &clnt->cl_procinfo[MOUNTPROC3_UMNT];
274 	else
275 		msg.rpc_proc = &clnt->cl_procinfo[MOUNTPROC_UMNT];
276 
277 	status = rpc_call_sync(clnt, &msg, 0);
278 	rpc_shutdown_client(clnt);
279 
280 	if (unlikely(status < 0))
281 		goto out_call_err;
282 
283 	return;
284 
285 out_clnt_err:
286 	dprintk("NFS: failed to create UMNT RPC client, status=%ld\n",
287 			PTR_ERR(clnt));
288 	return;
289 
290 out_call_err:
291 	dprintk("NFS: UMNT request failed, status=%d\n", status);
292 }
293 
294 /*
295  * XDR encode/decode functions for MOUNT
296  */
297 
298 static void encode_mntdirpath(struct xdr_stream *xdr, const char *pathname)
299 {
300 	const u32 pathname_len = strlen(pathname);
301 	__be32 *p;
302 
303 	p = xdr_reserve_space(xdr, 4 + pathname_len);
304 	xdr_encode_opaque(p, pathname, pathname_len);
305 }
306 
307 static void mnt_xdr_enc_dirpath(struct rpc_rqst *req, struct xdr_stream *xdr,
308 				const void *dirpath)
309 {
310 	encode_mntdirpath(xdr, dirpath);
311 }
312 
313 /*
314  * RFC 1094: "A non-zero status indicates some sort of error.  In this
315  * case, the status is a UNIX error number."  This can be problematic
316  * if the server and client use different errno values for the same
317  * error.
318  *
319  * However, the OpenGroup XNFS spec provides a simple mapping that is
320  * independent of local errno values on the server and the client.
321  */
322 static int decode_status(struct xdr_stream *xdr, struct mountres *res)
323 {
324 	unsigned int i;
325 	u32 status;
326 	__be32 *p;
327 
328 	p = xdr_inline_decode(xdr, 4);
329 	if (unlikely(p == NULL))
330 		return -EIO;
331 	status = be32_to_cpup(p);
332 
333 	for (i = 0; i < ARRAY_SIZE(mnt_errtbl); i++) {
334 		if (mnt_errtbl[i].status == status) {
335 			res->errno = mnt_errtbl[i].errno;
336 			return 0;
337 		}
338 	}
339 
340 	dprintk("NFS: unrecognized MNT status code: %u\n", status);
341 	res->errno = -EACCES;
342 	return 0;
343 }
344 
345 static int decode_fhandle(struct xdr_stream *xdr, struct mountres *res)
346 {
347 	struct nfs_fh *fh = res->fh;
348 	__be32 *p;
349 
350 	p = xdr_inline_decode(xdr, NFS2_FHSIZE);
351 	if (unlikely(p == NULL))
352 		return -EIO;
353 
354 	fh->size = NFS2_FHSIZE;
355 	memcpy(fh->data, p, NFS2_FHSIZE);
356 	return 0;
357 }
358 
359 static int mnt_xdr_dec_mountres(struct rpc_rqst *req,
360 				struct xdr_stream *xdr,
361 				void *data)
362 {
363 	struct mountres *res = data;
364 	int status;
365 
366 	status = decode_status(xdr, res);
367 	if (unlikely(status != 0 || res->errno != 0))
368 		return status;
369 	return decode_fhandle(xdr, res);
370 }
371 
372 static int decode_fhs_status(struct xdr_stream *xdr, struct mountres *res)
373 {
374 	unsigned int i;
375 	u32 status;
376 	__be32 *p;
377 
378 	p = xdr_inline_decode(xdr, 4);
379 	if (unlikely(p == NULL))
380 		return -EIO;
381 	status = be32_to_cpup(p);
382 
383 	for (i = 0; i < ARRAY_SIZE(mnt3_errtbl); i++) {
384 		if (mnt3_errtbl[i].status == status) {
385 			res->errno = mnt3_errtbl[i].errno;
386 			return 0;
387 		}
388 	}
389 
390 	dprintk("NFS: unrecognized MNT3 status code: %u\n", status);
391 	res->errno = -EACCES;
392 	return 0;
393 }
394 
395 static int decode_fhandle3(struct xdr_stream *xdr, struct mountres *res)
396 {
397 	struct nfs_fh *fh = res->fh;
398 	u32 size;
399 	__be32 *p;
400 
401 	p = xdr_inline_decode(xdr, 4);
402 	if (unlikely(p == NULL))
403 		return -EIO;
404 
405 	size = be32_to_cpup(p);
406 	if (size > NFS3_FHSIZE || size == 0)
407 		return -EIO;
408 
409 	p = xdr_inline_decode(xdr, size);
410 	if (unlikely(p == NULL))
411 		return -EIO;
412 
413 	fh->size = size;
414 	memcpy(fh->data, p, size);
415 	return 0;
416 }
417 
418 static int decode_auth_flavors(struct xdr_stream *xdr, struct mountres *res)
419 {
420 	rpc_authflavor_t *flavors = res->auth_flavors;
421 	unsigned int *count = res->auth_count;
422 	u32 entries, i;
423 	__be32 *p;
424 
425 	if (*count == 0)
426 		return 0;
427 
428 	p = xdr_inline_decode(xdr, 4);
429 	if (unlikely(p == NULL))
430 		return -EIO;
431 	entries = be32_to_cpup(p);
432 	dprintk("NFS: received %u auth flavors\n", entries);
433 	if (entries > NFS_MAX_SECFLAVORS)
434 		entries = NFS_MAX_SECFLAVORS;
435 
436 	p = xdr_inline_decode(xdr, 4 * entries);
437 	if (unlikely(p == NULL))
438 		return -EIO;
439 
440 	if (entries > *count)
441 		entries = *count;
442 
443 	for (i = 0; i < entries; i++) {
444 		flavors[i] = be32_to_cpup(p++);
445 		dprintk("NFS:   auth flavor[%u]: %d\n", i, flavors[i]);
446 	}
447 	*count = i;
448 
449 	return 0;
450 }
451 
452 static int mnt_xdr_dec_mountres3(struct rpc_rqst *req,
453 				 struct xdr_stream *xdr,
454 				 void *data)
455 {
456 	struct mountres *res = data;
457 	int status;
458 
459 	status = decode_fhs_status(xdr, res);
460 	if (unlikely(status != 0 || res->errno != 0))
461 		return status;
462 	status = decode_fhandle3(xdr, res);
463 	if (unlikely(status != 0)) {
464 		res->errno = -EBADHANDLE;
465 		return 0;
466 	}
467 	return decode_auth_flavors(xdr, res);
468 }
469 
470 static const struct rpc_procinfo mnt_procedures[] = {
471 	[MOUNTPROC_MNT] = {
472 		.p_proc		= MOUNTPROC_MNT,
473 		.p_encode	= mnt_xdr_enc_dirpath,
474 		.p_decode	= mnt_xdr_dec_mountres,
475 		.p_arglen	= MNT_enc_dirpath_sz,
476 		.p_replen	= MNT_dec_mountres_sz,
477 		.p_statidx	= MOUNTPROC_MNT,
478 		.p_name		= "MOUNT",
479 	},
480 	[MOUNTPROC_UMNT] = {
481 		.p_proc		= MOUNTPROC_UMNT,
482 		.p_encode	= mnt_xdr_enc_dirpath,
483 		.p_arglen	= MNT_enc_dirpath_sz,
484 		.p_statidx	= MOUNTPROC_UMNT,
485 		.p_name		= "UMOUNT",
486 	},
487 };
488 
489 static const struct rpc_procinfo mnt3_procedures[] = {
490 	[MOUNTPROC3_MNT] = {
491 		.p_proc		= MOUNTPROC3_MNT,
492 		.p_encode	= mnt_xdr_enc_dirpath,
493 		.p_decode	= mnt_xdr_dec_mountres3,
494 		.p_arglen	= MNT_enc_dirpath_sz,
495 		.p_replen	= MNT_dec_mountres3_sz,
496 		.p_statidx	= MOUNTPROC3_MNT,
497 		.p_name		= "MOUNT",
498 	},
499 	[MOUNTPROC3_UMNT] = {
500 		.p_proc		= MOUNTPROC3_UMNT,
501 		.p_encode	= mnt_xdr_enc_dirpath,
502 		.p_arglen	= MNT_enc_dirpath_sz,
503 		.p_statidx	= MOUNTPROC3_UMNT,
504 		.p_name		= "UMOUNT",
505 	},
506 };
507 
508 static unsigned int mnt_counts[ARRAY_SIZE(mnt_procedures)];
509 static const struct rpc_version mnt_version1 = {
510 	.number		= 1,
511 	.nrprocs	= ARRAY_SIZE(mnt_procedures),
512 	.procs		= mnt_procedures,
513 	.counts		= mnt_counts,
514 };
515 
516 static unsigned int mnt3_counts[ARRAY_SIZE(mnt3_procedures)];
517 static const struct rpc_version mnt_version3 = {
518 	.number		= 3,
519 	.nrprocs	= ARRAY_SIZE(mnt3_procedures),
520 	.procs		= mnt3_procedures,
521 	.counts		= mnt3_counts,
522 };
523 
524 static const struct rpc_version *mnt_version[] = {
525 	NULL,
526 	&mnt_version1,
527 	NULL,
528 	&mnt_version3,
529 };
530 
531 static struct rpc_stat mnt_stats;
532 
533 static const struct rpc_program mnt_program = {
534 	.name		= "mount",
535 	.number		= NFS_MNT_PROGRAM,
536 	.nrvers		= ARRAY_SIZE(mnt_version),
537 	.version	= mnt_version,
538 	.stats		= &mnt_stats,
539 };
540