• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/fs/nfs/fs_context.c
4  *
5  * Copyright (C) 1992 Rick Sladkey
6  * Conversion to new mount api Copyright (C) David Howells
7  *
8  * NFS mount handling.
9  *
10  * Split from fs/nfs/super.c by David Howells <dhowells@redhat.com>
11  */
12 
13 #include <linux/module.h>
14 #include <linux/fs.h>
15 #include <linux/fs_context.h>
16 #include <linux/fs_parser.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_mount.h>
19 #include <linux/nfs4_mount.h>
20 #include "nfs.h"
21 #include "internal.h"
22 
23 #define NFSDBG_FACILITY		NFSDBG_MOUNT
24 
25 #if IS_ENABLED(CONFIG_NFS_V3)
26 #define NFS_DEFAULT_VERSION 3
27 #else
28 #define NFS_DEFAULT_VERSION 2
29 #endif
30 
31 #define NFS_MAX_CONNECTIONS 16
32 
33 enum nfs_param {
34 	Opt_ac,
35 	Opt_acdirmax,
36 	Opt_acdirmin,
37 	Opt_acl,
38 	Opt_acregmax,
39 	Opt_acregmin,
40 	Opt_actimeo,
41 	Opt_addr,
42 	Opt_bg,
43 	Opt_bsize,
44 	Opt_clientaddr,
45 	Opt_cto,
46 	Opt_fg,
47 	Opt_fscache,
48 	Opt_fscache_flag,
49 	Opt_hard,
50 	Opt_intr,
51 	Opt_local_lock,
52 	Opt_lock,
53 	Opt_lookupcache,
54 	Opt_migration,
55 	Opt_minorversion,
56 	Opt_mountaddr,
57 	Opt_mounthost,
58 	Opt_mountport,
59 	Opt_mountproto,
60 	Opt_mountvers,
61 	Opt_namelen,
62 	Opt_nconnect,
63 	Opt_port,
64 	Opt_posix,
65 	Opt_proto,
66 	Opt_rdirplus,
67 	Opt_rdma,
68 	Opt_resvport,
69 	Opt_retrans,
70 	Opt_retry,
71 	Opt_rsize,
72 	Opt_sec,
73 	Opt_sharecache,
74 	Opt_sloppy,
75 	Opt_soft,
76 	Opt_softerr,
77 	Opt_softreval,
78 	Opt_source,
79 	Opt_tcp,
80 	Opt_timeo,
81 	Opt_udp,
82 	Opt_v,
83 	Opt_vers,
84 	Opt_wsize,
85 };
86 
87 enum {
88 	Opt_local_lock_all,
89 	Opt_local_lock_flock,
90 	Opt_local_lock_none,
91 	Opt_local_lock_posix,
92 };
93 
94 static const struct constant_table nfs_param_enums_local_lock[] = {
95 	{ "all",		Opt_local_lock_all },
96 	{ "flock",	Opt_local_lock_flock },
97 	{ "posix",	Opt_local_lock_posix },
98 	{ "none",		Opt_local_lock_none },
99 	{}
100 };
101 
102 enum {
103 	Opt_lookupcache_all,
104 	Opt_lookupcache_none,
105 	Opt_lookupcache_positive,
106 };
107 
108 static const struct constant_table nfs_param_enums_lookupcache[] = {
109 	{ "all",		Opt_lookupcache_all },
110 	{ "none",		Opt_lookupcache_none },
111 	{ "pos",		Opt_lookupcache_positive },
112 	{ "positive",		Opt_lookupcache_positive },
113 	{}
114 };
115 
116 static const struct fs_parameter_spec nfs_fs_parameters[] = {
117 	fsparam_flag_no("ac",		Opt_ac),
118 	fsparam_u32   ("acdirmax",	Opt_acdirmax),
119 	fsparam_u32   ("acdirmin",	Opt_acdirmin),
120 	fsparam_flag_no("acl",		Opt_acl),
121 	fsparam_u32   ("acregmax",	Opt_acregmax),
122 	fsparam_u32   ("acregmin",	Opt_acregmin),
123 	fsparam_u32   ("actimeo",	Opt_actimeo),
124 	fsparam_string("addr",		Opt_addr),
125 	fsparam_flag  ("bg",		Opt_bg),
126 	fsparam_u32   ("bsize",		Opt_bsize),
127 	fsparam_string("clientaddr",	Opt_clientaddr),
128 	fsparam_flag_no("cto",		Opt_cto),
129 	fsparam_flag  ("fg",		Opt_fg),
130 	fsparam_flag_no("fsc",		Opt_fscache_flag),
131 	fsparam_string("fsc",		Opt_fscache),
132 	fsparam_flag  ("hard",		Opt_hard),
133 	__fsparam(NULL, "intr",		Opt_intr,
134 		  fs_param_neg_with_no|fs_param_deprecated, NULL),
135 	fsparam_enum  ("local_lock",	Opt_local_lock, nfs_param_enums_local_lock),
136 	fsparam_flag_no("lock",		Opt_lock),
137 	fsparam_enum  ("lookupcache",	Opt_lookupcache, nfs_param_enums_lookupcache),
138 	fsparam_flag_no("migration",	Opt_migration),
139 	fsparam_u32   ("minorversion",	Opt_minorversion),
140 	fsparam_string("mountaddr",	Opt_mountaddr),
141 	fsparam_string("mounthost",	Opt_mounthost),
142 	fsparam_u32   ("mountport",	Opt_mountport),
143 	fsparam_string("mountproto",	Opt_mountproto),
144 	fsparam_u32   ("mountvers",	Opt_mountvers),
145 	fsparam_u32   ("namlen",	Opt_namelen),
146 	fsparam_u32   ("nconnect",	Opt_nconnect),
147 	fsparam_string("nfsvers",	Opt_vers),
148 	fsparam_u32   ("port",		Opt_port),
149 	fsparam_flag_no("posix",	Opt_posix),
150 	fsparam_string("proto",		Opt_proto),
151 	fsparam_flag_no("rdirplus",	Opt_rdirplus),
152 	fsparam_flag  ("rdma",		Opt_rdma),
153 	fsparam_flag_no("resvport",	Opt_resvport),
154 	fsparam_u32   ("retrans",	Opt_retrans),
155 	fsparam_string("retry",		Opt_retry),
156 	fsparam_u32   ("rsize",		Opt_rsize),
157 	fsparam_string("sec",		Opt_sec),
158 	fsparam_flag_no("sharecache",	Opt_sharecache),
159 	fsparam_flag  ("sloppy",	Opt_sloppy),
160 	fsparam_flag  ("soft",		Opt_soft),
161 	fsparam_flag  ("softerr",	Opt_softerr),
162 	fsparam_flag  ("softreval",	Opt_softreval),
163 	fsparam_string("source",	Opt_source),
164 	fsparam_flag  ("tcp",		Opt_tcp),
165 	fsparam_u32   ("timeo",		Opt_timeo),
166 	fsparam_flag  ("udp",		Opt_udp),
167 	fsparam_flag  ("v2",		Opt_v),
168 	fsparam_flag  ("v3",		Opt_v),
169 	fsparam_flag  ("v4",		Opt_v),
170 	fsparam_flag  ("v4.0",		Opt_v),
171 	fsparam_flag  ("v4.1",		Opt_v),
172 	fsparam_flag  ("v4.2",		Opt_v),
173 	fsparam_string("vers",		Opt_vers),
174 	fsparam_u32   ("wsize",		Opt_wsize),
175 	{}
176 };
177 
178 enum {
179 	Opt_vers_2,
180 	Opt_vers_3,
181 	Opt_vers_4,
182 	Opt_vers_4_0,
183 	Opt_vers_4_1,
184 	Opt_vers_4_2,
185 };
186 
187 static const struct constant_table nfs_vers_tokens[] = {
188 	{ "2",		Opt_vers_2 },
189 	{ "3",		Opt_vers_3 },
190 	{ "4",		Opt_vers_4 },
191 	{ "4.0",	Opt_vers_4_0 },
192 	{ "4.1",	Opt_vers_4_1 },
193 	{ "4.2",	Opt_vers_4_2 },
194 	{}
195 };
196 
197 enum {
198 	Opt_xprt_rdma,
199 	Opt_xprt_rdma6,
200 	Opt_xprt_tcp,
201 	Opt_xprt_tcp6,
202 	Opt_xprt_udp,
203 	Opt_xprt_udp6,
204 	nr__Opt_xprt
205 };
206 
207 static const struct constant_table nfs_xprt_protocol_tokens[] = {
208 	{ "rdma",	Opt_xprt_rdma },
209 	{ "rdma6",	Opt_xprt_rdma6 },
210 	{ "tcp",	Opt_xprt_tcp },
211 	{ "tcp6",	Opt_xprt_tcp6 },
212 	{ "udp",	Opt_xprt_udp },
213 	{ "udp6",	Opt_xprt_udp6 },
214 	{}
215 };
216 
217 enum {
218 	Opt_sec_krb5,
219 	Opt_sec_krb5i,
220 	Opt_sec_krb5p,
221 	Opt_sec_lkey,
222 	Opt_sec_lkeyi,
223 	Opt_sec_lkeyp,
224 	Opt_sec_none,
225 	Opt_sec_spkm,
226 	Opt_sec_spkmi,
227 	Opt_sec_spkmp,
228 	Opt_sec_sys,
229 	nr__Opt_sec
230 };
231 
232 static const struct constant_table nfs_secflavor_tokens[] = {
233 	{ "krb5",	Opt_sec_krb5 },
234 	{ "krb5i",	Opt_sec_krb5i },
235 	{ "krb5p",	Opt_sec_krb5p },
236 	{ "lkey",	Opt_sec_lkey },
237 	{ "lkeyi",	Opt_sec_lkeyi },
238 	{ "lkeyp",	Opt_sec_lkeyp },
239 	{ "none",	Opt_sec_none },
240 	{ "null",	Opt_sec_none },
241 	{ "spkm3",	Opt_sec_spkm },
242 	{ "spkm3i",	Opt_sec_spkmi },
243 	{ "spkm3p",	Opt_sec_spkmp },
244 	{ "sys",	Opt_sec_sys },
245 	{}
246 };
247 
248 /*
249  * Sanity-check a server address provided by the mount command.
250  *
251  * Address family must be initialized, and address must not be
252  * the ANY address for that family.
253  */
nfs_verify_server_address(struct sockaddr * addr)254 static int nfs_verify_server_address(struct sockaddr *addr)
255 {
256 	switch (addr->sa_family) {
257 	case AF_INET: {
258 		struct sockaddr_in *sa = (struct sockaddr_in *)addr;
259 		return sa->sin_addr.s_addr != htonl(INADDR_ANY);
260 	}
261 	case AF_INET6: {
262 		struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
263 		return !ipv6_addr_any(sa);
264 	}
265 	}
266 
267 	dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
268 	return 0;
269 }
270 
271 /*
272  * Sanity check the NFS transport protocol.
273  *
274  */
nfs_validate_transport_protocol(struct nfs_fs_context * ctx)275 static void nfs_validate_transport_protocol(struct nfs_fs_context *ctx)
276 {
277 	switch (ctx->nfs_server.protocol) {
278 	case XPRT_TRANSPORT_UDP:
279 	case XPRT_TRANSPORT_TCP:
280 	case XPRT_TRANSPORT_RDMA:
281 		break;
282 	default:
283 		ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
284 	}
285 }
286 
287 /*
288  * For text based NFSv2/v3 mounts, the mount protocol transport default
289  * settings should depend upon the specified NFS transport.
290  */
nfs_set_mount_transport_protocol(struct nfs_fs_context * ctx)291 static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
292 {
293 	nfs_validate_transport_protocol(ctx);
294 
295 	if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
296 	    ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
297 			return;
298 	switch (ctx->nfs_server.protocol) {
299 	case XPRT_TRANSPORT_UDP:
300 		ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
301 		break;
302 	case XPRT_TRANSPORT_TCP:
303 	case XPRT_TRANSPORT_RDMA:
304 		ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
305 	}
306 }
307 
308 /*
309  * Add 'flavor' to 'auth_info' if not already present.
310  * Returns true if 'flavor' ends up in the list, false otherwise
311  */
nfs_auth_info_add(struct fs_context * fc,struct nfs_auth_info * auth_info,rpc_authflavor_t flavor)312 static int nfs_auth_info_add(struct fs_context *fc,
313 			     struct nfs_auth_info *auth_info,
314 			     rpc_authflavor_t flavor)
315 {
316 	unsigned int i;
317 	unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
318 
319 	/* make sure this flavor isn't already in the list */
320 	for (i = 0; i < auth_info->flavor_len; i++) {
321 		if (flavor == auth_info->flavors[i])
322 			return 0;
323 	}
324 
325 	if (auth_info->flavor_len + 1 >= max_flavor_len)
326 		return nfs_invalf(fc, "NFS: too many sec= flavors");
327 
328 	auth_info->flavors[auth_info->flavor_len++] = flavor;
329 	return 0;
330 }
331 
332 /*
333  * Parse the value of the 'sec=' option.
334  */
nfs_parse_security_flavors(struct fs_context * fc,struct fs_parameter * param)335 static int nfs_parse_security_flavors(struct fs_context *fc,
336 				      struct fs_parameter *param)
337 {
338 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
339 	rpc_authflavor_t pseudoflavor;
340 	char *string = param->string, *p;
341 	int ret;
342 
343 	dfprintk(MOUNT, "NFS: parsing %s=%s option\n", param->key, param->string);
344 
345 	while ((p = strsep(&string, ":")) != NULL) {
346 		if (!*p)
347 			continue;
348 		switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
349 		case Opt_sec_none:
350 			pseudoflavor = RPC_AUTH_NULL;
351 			break;
352 		case Opt_sec_sys:
353 			pseudoflavor = RPC_AUTH_UNIX;
354 			break;
355 		case Opt_sec_krb5:
356 			pseudoflavor = RPC_AUTH_GSS_KRB5;
357 			break;
358 		case Opt_sec_krb5i:
359 			pseudoflavor = RPC_AUTH_GSS_KRB5I;
360 			break;
361 		case Opt_sec_krb5p:
362 			pseudoflavor = RPC_AUTH_GSS_KRB5P;
363 			break;
364 		case Opt_sec_lkey:
365 			pseudoflavor = RPC_AUTH_GSS_LKEY;
366 			break;
367 		case Opt_sec_lkeyi:
368 			pseudoflavor = RPC_AUTH_GSS_LKEYI;
369 			break;
370 		case Opt_sec_lkeyp:
371 			pseudoflavor = RPC_AUTH_GSS_LKEYP;
372 			break;
373 		case Opt_sec_spkm:
374 			pseudoflavor = RPC_AUTH_GSS_SPKM;
375 			break;
376 		case Opt_sec_spkmi:
377 			pseudoflavor = RPC_AUTH_GSS_SPKMI;
378 			break;
379 		case Opt_sec_spkmp:
380 			pseudoflavor = RPC_AUTH_GSS_SPKMP;
381 			break;
382 		default:
383 			return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
384 		}
385 
386 		ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
387 		if (ret < 0)
388 			return ret;
389 	}
390 
391 	return 0;
392 }
393 
nfs_parse_version_string(struct fs_context * fc,const char * string)394 static int nfs_parse_version_string(struct fs_context *fc,
395 				    const char *string)
396 {
397 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
398 
399 	ctx->flags &= ~NFS_MOUNT_VER3;
400 	switch (lookup_constant(nfs_vers_tokens, string, -1)) {
401 	case Opt_vers_2:
402 		ctx->version = 2;
403 		break;
404 	case Opt_vers_3:
405 		ctx->flags |= NFS_MOUNT_VER3;
406 		ctx->version = 3;
407 		break;
408 	case Opt_vers_4:
409 		/* Backward compatibility option. In future,
410 		 * the mount program should always supply
411 		 * a NFSv4 minor version number.
412 		 */
413 		ctx->version = 4;
414 		break;
415 	case Opt_vers_4_0:
416 		ctx->version = 4;
417 		ctx->minorversion = 0;
418 		break;
419 	case Opt_vers_4_1:
420 		ctx->version = 4;
421 		ctx->minorversion = 1;
422 		break;
423 	case Opt_vers_4_2:
424 		ctx->version = 4;
425 		ctx->minorversion = 2;
426 		break;
427 	default:
428 		return nfs_invalf(fc, "NFS: Unsupported NFS version");
429 	}
430 	return 0;
431 }
432 
433 /*
434  * Parse a single mount parameter.
435  */
nfs_fs_context_parse_param(struct fs_context * fc,struct fs_parameter * param)436 static int nfs_fs_context_parse_param(struct fs_context *fc,
437 				      struct fs_parameter *param)
438 {
439 	struct fs_parse_result result;
440 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
441 	unsigned short protofamily, mountfamily;
442 	unsigned int len;
443 	int ret, opt;
444 
445 	dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", param->key);
446 
447 	opt = fs_parse(fc, nfs_fs_parameters, param, &result);
448 	if (opt < 0)
449 		return ctx->sloppy ? 1 : opt;
450 
451 	switch (opt) {
452 	case Opt_source:
453 		if (fc->source)
454 			return nfs_invalf(fc, "NFS: Multiple sources not supported");
455 		fc->source = param->string;
456 		param->string = NULL;
457 		break;
458 
459 		/*
460 		 * boolean options:  foo/nofoo
461 		 */
462 	case Opt_soft:
463 		ctx->flags |= NFS_MOUNT_SOFT;
464 		ctx->flags &= ~NFS_MOUNT_SOFTERR;
465 		break;
466 	case Opt_softerr:
467 		ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
468 		ctx->flags &= ~NFS_MOUNT_SOFT;
469 		break;
470 	case Opt_hard:
471 		ctx->flags &= ~(NFS_MOUNT_SOFT |
472 				NFS_MOUNT_SOFTERR |
473 				NFS_MOUNT_SOFTREVAL);
474 		break;
475 	case Opt_softreval:
476 		if (result.negated)
477 			ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
478 		else
479 			ctx->flags |= NFS_MOUNT_SOFTREVAL;
480 		break;
481 	case Opt_posix:
482 		if (result.negated)
483 			ctx->flags &= ~NFS_MOUNT_POSIX;
484 		else
485 			ctx->flags |= NFS_MOUNT_POSIX;
486 		break;
487 	case Opt_cto:
488 		if (result.negated)
489 			ctx->flags |= NFS_MOUNT_NOCTO;
490 		else
491 			ctx->flags &= ~NFS_MOUNT_NOCTO;
492 		break;
493 	case Opt_ac:
494 		if (result.negated)
495 			ctx->flags |= NFS_MOUNT_NOAC;
496 		else
497 			ctx->flags &= ~NFS_MOUNT_NOAC;
498 		break;
499 	case Opt_lock:
500 		if (result.negated) {
501 			ctx->flags |= NFS_MOUNT_NONLM;
502 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
503 		} else {
504 			ctx->flags &= ~NFS_MOUNT_NONLM;
505 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
506 		}
507 		break;
508 	case Opt_udp:
509 		ctx->flags &= ~NFS_MOUNT_TCP;
510 		ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
511 		break;
512 	case Opt_tcp:
513 		ctx->flags |= NFS_MOUNT_TCP;
514 		ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
515 		break;
516 	case Opt_rdma:
517 		ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
518 		ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
519 		xprt_load_transport(param->key);
520 		break;
521 	case Opt_acl:
522 		if (result.negated)
523 			ctx->flags |= NFS_MOUNT_NOACL;
524 		else
525 			ctx->flags &= ~NFS_MOUNT_NOACL;
526 		break;
527 	case Opt_rdirplus:
528 		if (result.negated)
529 			ctx->flags |= NFS_MOUNT_NORDIRPLUS;
530 		else
531 			ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
532 		break;
533 	case Opt_sharecache:
534 		if (result.negated)
535 			ctx->flags |= NFS_MOUNT_UNSHARED;
536 		else
537 			ctx->flags &= ~NFS_MOUNT_UNSHARED;
538 		break;
539 	case Opt_resvport:
540 		if (result.negated)
541 			ctx->flags |= NFS_MOUNT_NORESVPORT;
542 		else
543 			ctx->flags &= ~NFS_MOUNT_NORESVPORT;
544 		break;
545 	case Opt_fscache_flag:
546 		if (result.negated)
547 			ctx->options &= ~NFS_OPTION_FSCACHE;
548 		else
549 			ctx->options |= NFS_OPTION_FSCACHE;
550 		kfree(ctx->fscache_uniq);
551 		ctx->fscache_uniq = NULL;
552 		break;
553 	case Opt_fscache:
554 		ctx->options |= NFS_OPTION_FSCACHE;
555 		kfree(ctx->fscache_uniq);
556 		ctx->fscache_uniq = param->string;
557 		param->string = NULL;
558 		break;
559 	case Opt_migration:
560 		if (result.negated)
561 			ctx->options &= ~NFS_OPTION_MIGRATION;
562 		else
563 			ctx->options |= NFS_OPTION_MIGRATION;
564 		break;
565 
566 		/*
567 		 * options that take numeric values
568 		 */
569 	case Opt_port:
570 		if (result.uint_32 > USHRT_MAX)
571 			goto out_of_bounds;
572 		ctx->nfs_server.port = result.uint_32;
573 		break;
574 	case Opt_rsize:
575 		ctx->rsize = result.uint_32;
576 		break;
577 	case Opt_wsize:
578 		ctx->wsize = result.uint_32;
579 		break;
580 	case Opt_bsize:
581 		ctx->bsize = result.uint_32;
582 		break;
583 	case Opt_timeo:
584 		if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
585 			goto out_of_bounds;
586 		ctx->timeo = result.uint_32;
587 		break;
588 	case Opt_retrans:
589 		if (result.uint_32 > INT_MAX)
590 			goto out_of_bounds;
591 		ctx->retrans = result.uint_32;
592 		break;
593 	case Opt_acregmin:
594 		ctx->acregmin = result.uint_32;
595 		break;
596 	case Opt_acregmax:
597 		ctx->acregmax = result.uint_32;
598 		break;
599 	case Opt_acdirmin:
600 		ctx->acdirmin = result.uint_32;
601 		break;
602 	case Opt_acdirmax:
603 		ctx->acdirmax = result.uint_32;
604 		break;
605 	case Opt_actimeo:
606 		ctx->acregmin = result.uint_32;
607 		ctx->acregmax = result.uint_32;
608 		ctx->acdirmin = result.uint_32;
609 		ctx->acdirmax = result.uint_32;
610 		break;
611 	case Opt_namelen:
612 		ctx->namlen = result.uint_32;
613 		break;
614 	case Opt_mountport:
615 		if (result.uint_32 > USHRT_MAX)
616 			goto out_of_bounds;
617 		ctx->mount_server.port = result.uint_32;
618 		break;
619 	case Opt_mountvers:
620 		if (result.uint_32 < NFS_MNT_VERSION ||
621 		    result.uint_32 > NFS_MNT3_VERSION)
622 			goto out_of_bounds;
623 		ctx->mount_server.version = result.uint_32;
624 		break;
625 	case Opt_minorversion:
626 		if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
627 			goto out_of_bounds;
628 		ctx->minorversion = result.uint_32;
629 		break;
630 
631 		/*
632 		 * options that take text values
633 		 */
634 	case Opt_v:
635 		ret = nfs_parse_version_string(fc, param->key + 1);
636 		if (ret < 0)
637 			return ret;
638 		break;
639 	case Opt_vers:
640 		ret = nfs_parse_version_string(fc, param->string);
641 		if (ret < 0)
642 			return ret;
643 		break;
644 	case Opt_sec:
645 		ret = nfs_parse_security_flavors(fc, param);
646 		if (ret < 0)
647 			return ret;
648 		break;
649 
650 	case Opt_proto:
651 		protofamily = AF_INET;
652 		switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
653 		case Opt_xprt_udp6:
654 			protofamily = AF_INET6;
655 			fallthrough;
656 		case Opt_xprt_udp:
657 			ctx->flags &= ~NFS_MOUNT_TCP;
658 			ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
659 			break;
660 		case Opt_xprt_tcp6:
661 			protofamily = AF_INET6;
662 			fallthrough;
663 		case Opt_xprt_tcp:
664 			ctx->flags |= NFS_MOUNT_TCP;
665 			ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
666 			break;
667 		case Opt_xprt_rdma6:
668 			protofamily = AF_INET6;
669 			fallthrough;
670 		case Opt_xprt_rdma:
671 			/* vector side protocols to TCP */
672 			ctx->flags |= NFS_MOUNT_TCP;
673 			ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
674 			xprt_load_transport(param->string);
675 			break;
676 		default:
677 			return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
678 		}
679 
680 		ctx->protofamily = protofamily;
681 		break;
682 
683 	case Opt_mountproto:
684 		mountfamily = AF_INET;
685 		switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
686 		case Opt_xprt_udp6:
687 			mountfamily = AF_INET6;
688 			fallthrough;
689 		case Opt_xprt_udp:
690 			ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
691 			break;
692 		case Opt_xprt_tcp6:
693 			mountfamily = AF_INET6;
694 			fallthrough;
695 		case Opt_xprt_tcp:
696 			ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
697 			break;
698 		case Opt_xprt_rdma: /* not used for side protocols */
699 		default:
700 			return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
701 		}
702 		ctx->mountfamily = mountfamily;
703 		break;
704 
705 	case Opt_addr:
706 		len = rpc_pton(fc->net_ns, param->string, param->size,
707 			       &ctx->nfs_server.address,
708 			       sizeof(ctx->nfs_server._address));
709 		if (len == 0)
710 			goto out_invalid_address;
711 		ctx->nfs_server.addrlen = len;
712 		break;
713 	case Opt_clientaddr:
714 		kfree(ctx->client_address);
715 		ctx->client_address = param->string;
716 		param->string = NULL;
717 		break;
718 	case Opt_mounthost:
719 		kfree(ctx->mount_server.hostname);
720 		ctx->mount_server.hostname = param->string;
721 		param->string = NULL;
722 		break;
723 	case Opt_mountaddr:
724 		len = rpc_pton(fc->net_ns, param->string, param->size,
725 			       &ctx->mount_server.address,
726 			       sizeof(ctx->mount_server._address));
727 		if (len == 0)
728 			goto out_invalid_address;
729 		ctx->mount_server.addrlen = len;
730 		break;
731 	case Opt_nconnect:
732 		if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
733 			goto out_of_bounds;
734 		ctx->nfs_server.nconnect = result.uint_32;
735 		break;
736 	case Opt_lookupcache:
737 		switch (result.uint_32) {
738 		case Opt_lookupcache_all:
739 			ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
740 			break;
741 		case Opt_lookupcache_positive:
742 			ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
743 			ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
744 			break;
745 		case Opt_lookupcache_none:
746 			ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
747 			break;
748 		default:
749 			goto out_invalid_value;
750 		}
751 		break;
752 	case Opt_local_lock:
753 		switch (result.uint_32) {
754 		case Opt_local_lock_all:
755 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
756 				       NFS_MOUNT_LOCAL_FCNTL);
757 			break;
758 		case Opt_local_lock_flock:
759 			ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
760 			break;
761 		case Opt_local_lock_posix:
762 			ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
763 			break;
764 		case Opt_local_lock_none:
765 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
766 					NFS_MOUNT_LOCAL_FCNTL);
767 			break;
768 		default:
769 			goto out_invalid_value;
770 		}
771 		break;
772 
773 		/*
774 		 * Special options
775 		 */
776 	case Opt_sloppy:
777 		ctx->sloppy = true;
778 		dfprintk(MOUNT, "NFS:   relaxing parsing rules\n");
779 		break;
780 	}
781 
782 	return 0;
783 
784 out_invalid_value:
785 	return nfs_invalf(fc, "NFS: Bad mount option value specified");
786 out_invalid_address:
787 	return nfs_invalf(fc, "NFS: Bad IP address specified");
788 out_of_bounds:
789 	return nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
790 }
791 
792 /*
793  * Split fc->source into "hostname:export_path".
794  *
795  * The leftmost colon demarks the split between the server's hostname
796  * and the export path.  If the hostname starts with a left square
797  * bracket, then it may contain colons.
798  *
799  * Note: caller frees hostname and export path, even on error.
800  */
nfs_parse_source(struct fs_context * fc,size_t maxnamlen,size_t maxpathlen)801 static int nfs_parse_source(struct fs_context *fc,
802 			    size_t maxnamlen, size_t maxpathlen)
803 {
804 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
805 	const char *dev_name = fc->source;
806 	size_t len;
807 	const char *end;
808 
809 	if (unlikely(!dev_name || !*dev_name)) {
810 		dfprintk(MOUNT, "NFS: device name not specified\n");
811 		return -EINVAL;
812 	}
813 
814 	/* Is the host name protected with square brakcets? */
815 	if (*dev_name == '[') {
816 		end = strchr(++dev_name, ']');
817 		if (end == NULL || end[1] != ':')
818 			goto out_bad_devname;
819 
820 		len = end - dev_name;
821 		end++;
822 	} else {
823 		const char *comma;
824 
825 		end = strchr(dev_name, ':');
826 		if (end == NULL)
827 			goto out_bad_devname;
828 		len = end - dev_name;
829 
830 		/* kill possible hostname list: not supported */
831 		comma = memchr(dev_name, ',', len);
832 		if (comma)
833 			len = comma - dev_name;
834 	}
835 
836 	if (len > maxnamlen)
837 		goto out_hostname;
838 
839 	kfree(ctx->nfs_server.hostname);
840 
841 	/* N.B. caller will free nfs_server.hostname in all cases */
842 	ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
843 	if (!ctx->nfs_server.hostname)
844 		goto out_nomem;
845 	len = strlen(++end);
846 	if (len > maxpathlen)
847 		goto out_path;
848 	ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
849 	if (!ctx->nfs_server.export_path)
850 		goto out_nomem;
851 
852 	dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", ctx->nfs_server.export_path);
853 	return 0;
854 
855 out_bad_devname:
856 	return nfs_invalf(fc, "NFS: device name not in host:path format");
857 out_nomem:
858 	nfs_errorf(fc, "NFS: not enough memory to parse device name");
859 	return -ENOMEM;
860 out_hostname:
861 	nfs_errorf(fc, "NFS: server hostname too long");
862 	return -ENAMETOOLONG;
863 out_path:
864 	nfs_errorf(fc, "NFS: export pathname too long");
865 	return -ENAMETOOLONG;
866 }
867 
is_remount_fc(struct fs_context * fc)868 static inline bool is_remount_fc(struct fs_context *fc)
869 {
870 	return fc->root != NULL;
871 }
872 
873 /*
874  * Parse monolithic NFS2/NFS3 mount data
875  * - fills in the mount root filehandle
876  *
877  * For option strings, user space handles the following behaviors:
878  *
879  * + DNS: mapping server host name to IP address ("addr=" option)
880  *
881  * + failure mode: how to behave if a mount request can't be handled
882  *   immediately ("fg/bg" option)
883  *
884  * + retry: how often to retry a mount request ("retry=" option)
885  *
886  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
887  *   mountproto=tcp after mountproto=udp, and so on
888  */
nfs23_parse_monolithic(struct fs_context * fc,struct nfs_mount_data * data)889 static int nfs23_parse_monolithic(struct fs_context *fc,
890 				  struct nfs_mount_data *data)
891 {
892 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
893 	struct nfs_fh *mntfh = ctx->mntfh;
894 	struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
895 	int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
896 
897 	if (data == NULL)
898 		goto out_no_data;
899 
900 	ctx->version = NFS_DEFAULT_VERSION;
901 	switch (data->version) {
902 	case 1:
903 		data->namlen = 0;
904 		fallthrough;
905 	case 2:
906 		data->bsize = 0;
907 		fallthrough;
908 	case 3:
909 		if (data->flags & NFS_MOUNT_VER3)
910 			goto out_no_v3;
911 		data->root.size = NFS2_FHSIZE;
912 		memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
913 		/* Turn off security negotiation */
914 		extra_flags |= NFS_MOUNT_SECFLAVOUR;
915 		fallthrough;
916 	case 4:
917 		if (data->flags & NFS_MOUNT_SECFLAVOUR)
918 			goto out_no_sec;
919 		fallthrough;
920 	case 5:
921 		memset(data->context, 0, sizeof(data->context));
922 		fallthrough;
923 	case 6:
924 		if (data->flags & NFS_MOUNT_VER3) {
925 			if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
926 				goto out_invalid_fh;
927 			mntfh->size = data->root.size;
928 			ctx->version = 3;
929 		} else {
930 			mntfh->size = NFS2_FHSIZE;
931 			ctx->version = 2;
932 		}
933 
934 
935 		memcpy(mntfh->data, data->root.data, mntfh->size);
936 		if (mntfh->size < sizeof(mntfh->data))
937 			memset(mntfh->data + mntfh->size, 0,
938 			       sizeof(mntfh->data) - mntfh->size);
939 
940 		/*
941 		 * for proto == XPRT_TRANSPORT_UDP, which is what uses
942 		 * to_exponential, implying shift: limit the shift value
943 		 * to BITS_PER_LONG (majortimeo is unsigned long)
944 		 */
945 		if (!(data->flags & NFS_MOUNT_TCP)) /* this will be UDP */
946 			if (data->retrans >= 64) /* shift value is too large */
947 				goto out_invalid_data;
948 
949 		/*
950 		 * Translate to nfs_fs_context, which nfs_fill_super
951 		 * can deal with.
952 		 */
953 		ctx->flags	= data->flags & NFS_MOUNT_FLAGMASK;
954 		ctx->flags	|= extra_flags;
955 		ctx->rsize	= data->rsize;
956 		ctx->wsize	= data->wsize;
957 		ctx->timeo	= data->timeo;
958 		ctx->retrans	= data->retrans;
959 		ctx->acregmin	= data->acregmin;
960 		ctx->acregmax	= data->acregmax;
961 		ctx->acdirmin	= data->acdirmin;
962 		ctx->acdirmax	= data->acdirmax;
963 		ctx->need_mount	= false;
964 
965 		memcpy(sap, &data->addr, sizeof(data->addr));
966 		ctx->nfs_server.addrlen = sizeof(data->addr);
967 		ctx->nfs_server.port = ntohs(data->addr.sin_port);
968 		if (sap->sa_family != AF_INET ||
969 		    !nfs_verify_server_address(sap))
970 			goto out_no_address;
971 
972 		if (!(data->flags & NFS_MOUNT_TCP))
973 			ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
974 		/* N.B. caller will free nfs_server.hostname in all cases */
975 		ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
976 		if (!ctx->nfs_server.hostname)
977 			goto out_nomem;
978 
979 		ctx->namlen		= data->namlen;
980 		ctx->bsize		= data->bsize;
981 
982 		if (data->flags & NFS_MOUNT_SECFLAVOUR)
983 			ctx->selected_flavor = data->pseudoflavor;
984 		else
985 			ctx->selected_flavor = RPC_AUTH_UNIX;
986 
987 		if (!(data->flags & NFS_MOUNT_NONLM))
988 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
989 					 NFS_MOUNT_LOCAL_FCNTL);
990 		else
991 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
992 					NFS_MOUNT_LOCAL_FCNTL);
993 
994 		/*
995 		 * The legacy version 6 binary mount data from userspace has a
996 		 * field used only to transport selinux information into the
997 		 * kernel.  To continue to support that functionality we
998 		 * have a touch of selinux knowledge here in the NFS code. The
999 		 * userspace code converted context=blah to just blah so we are
1000 		 * converting back to the full string selinux understands.
1001 		 */
1002 		if (data->context[0]){
1003 #ifdef CONFIG_SECURITY_SELINUX
1004 			int ret;
1005 
1006 			data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1007 			ret = vfs_parse_fs_string(fc, "context",
1008 						  data->context, strlen(data->context));
1009 			if (ret < 0)
1010 				return ret;
1011 #else
1012 			return -EINVAL;
1013 #endif
1014 		}
1015 
1016 		break;
1017 	default:
1018 		goto generic;
1019 	}
1020 
1021 	ctx->skip_reconfig_option_check = true;
1022 	return 0;
1023 
1024 generic:
1025 	return generic_parse_monolithic(fc, data);
1026 
1027 out_no_data:
1028 	if (is_remount_fc(fc)) {
1029 		ctx->skip_reconfig_option_check = true;
1030 		return 0;
1031 	}
1032 	return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
1033 
1034 out_no_v3:
1035 	return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
1036 
1037 out_no_sec:
1038 	return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
1039 
1040 out_nomem:
1041 	dfprintk(MOUNT, "NFS: not enough memory to handle mount options");
1042 	return -ENOMEM;
1043 
1044 out_no_address:
1045 	return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1046 
1047 out_invalid_fh:
1048 	return nfs_invalf(fc, "NFS: invalid root filehandle");
1049 
1050 out_invalid_data:
1051 	return nfs_invalf(fc, "NFS: invalid binary mount data");
1052 }
1053 
1054 #if IS_ENABLED(CONFIG_NFS_V4)
1055 struct compat_nfs_string {
1056 	compat_uint_t len;
1057 	compat_uptr_t data;
1058 };
1059 
compat_nfs_string(struct nfs_string * dst,struct compat_nfs_string * src)1060 static inline void compat_nfs_string(struct nfs_string *dst,
1061 				     struct compat_nfs_string *src)
1062 {
1063 	dst->data = compat_ptr(src->data);
1064 	dst->len = src->len;
1065 }
1066 
1067 struct compat_nfs4_mount_data_v1 {
1068 	compat_int_t version;
1069 	compat_int_t flags;
1070 	compat_int_t rsize;
1071 	compat_int_t wsize;
1072 	compat_int_t timeo;
1073 	compat_int_t retrans;
1074 	compat_int_t acregmin;
1075 	compat_int_t acregmax;
1076 	compat_int_t acdirmin;
1077 	compat_int_t acdirmax;
1078 	struct compat_nfs_string client_addr;
1079 	struct compat_nfs_string mnt_path;
1080 	struct compat_nfs_string hostname;
1081 	compat_uint_t host_addrlen;
1082 	compat_uptr_t host_addr;
1083 	compat_int_t proto;
1084 	compat_int_t auth_flavourlen;
1085 	compat_uptr_t auth_flavours;
1086 };
1087 
nfs4_compat_mount_data_conv(struct nfs4_mount_data * data)1088 static void nfs4_compat_mount_data_conv(struct nfs4_mount_data *data)
1089 {
1090 	struct compat_nfs4_mount_data_v1 *compat =
1091 			(struct compat_nfs4_mount_data_v1 *)data;
1092 
1093 	/* copy the fields backwards */
1094 	data->auth_flavours = compat_ptr(compat->auth_flavours);
1095 	data->auth_flavourlen = compat->auth_flavourlen;
1096 	data->proto = compat->proto;
1097 	data->host_addr = compat_ptr(compat->host_addr);
1098 	data->host_addrlen = compat->host_addrlen;
1099 	compat_nfs_string(&data->hostname, &compat->hostname);
1100 	compat_nfs_string(&data->mnt_path, &compat->mnt_path);
1101 	compat_nfs_string(&data->client_addr, &compat->client_addr);
1102 	data->acdirmax = compat->acdirmax;
1103 	data->acdirmin = compat->acdirmin;
1104 	data->acregmax = compat->acregmax;
1105 	data->acregmin = compat->acregmin;
1106 	data->retrans = compat->retrans;
1107 	data->timeo = compat->timeo;
1108 	data->wsize = compat->wsize;
1109 	data->rsize = compat->rsize;
1110 	data->flags = compat->flags;
1111 	data->version = compat->version;
1112 }
1113 
1114 /*
1115  * Validate NFSv4 mount options
1116  */
nfs4_parse_monolithic(struct fs_context * fc,struct nfs4_mount_data * data)1117 static int nfs4_parse_monolithic(struct fs_context *fc,
1118 				 struct nfs4_mount_data *data)
1119 {
1120 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1121 	struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
1122 	char *c;
1123 
1124 	if (!data) {
1125 		if (is_remount_fc(fc))
1126 			goto done;
1127 		return nfs_invalf(fc,
1128 			"NFS4: mount program didn't pass any mount data");
1129 	}
1130 
1131 	ctx->version = 4;
1132 
1133 	if (data->version != 1)
1134 		return generic_parse_monolithic(fc, data);
1135 
1136 	if (in_compat_syscall())
1137 		nfs4_compat_mount_data_conv(data);
1138 
1139 	if (data->host_addrlen > sizeof(ctx->nfs_server.address))
1140 		goto out_no_address;
1141 	if (data->host_addrlen == 0)
1142 		goto out_no_address;
1143 	ctx->nfs_server.addrlen = data->host_addrlen;
1144 	if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1145 		return -EFAULT;
1146 	if (!nfs_verify_server_address(sap))
1147 		goto out_no_address;
1148 	ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1149 
1150 	if (data->auth_flavourlen) {
1151 		rpc_authflavor_t pseudoflavor;
1152 
1153 		if (data->auth_flavourlen > 1)
1154 			goto out_inval_auth;
1155 		if (copy_from_user(&pseudoflavor, data->auth_flavours,
1156 				   sizeof(pseudoflavor)))
1157 			return -EFAULT;
1158 		ctx->selected_flavor = pseudoflavor;
1159 	} else {
1160 		ctx->selected_flavor = RPC_AUTH_UNIX;
1161 	}
1162 
1163 	c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1164 	if (IS_ERR(c))
1165 		return PTR_ERR(c);
1166 	ctx->nfs_server.hostname = c;
1167 
1168 	c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1169 	if (IS_ERR(c))
1170 		return PTR_ERR(c);
1171 	ctx->nfs_server.export_path = c;
1172 	dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1173 
1174 	c = strndup_user(data->client_addr.data, 16);
1175 	if (IS_ERR(c))
1176 		return PTR_ERR(c);
1177 	ctx->client_address = c;
1178 
1179 	/*
1180 	 * Translate to nfs_fs_context, which nfs_fill_super
1181 	 * can deal with.
1182 	 */
1183 
1184 	ctx->flags	= data->flags & NFS4_MOUNT_FLAGMASK;
1185 	ctx->rsize	= data->rsize;
1186 	ctx->wsize	= data->wsize;
1187 	ctx->timeo	= data->timeo;
1188 	ctx->retrans	= data->retrans;
1189 	ctx->acregmin	= data->acregmin;
1190 	ctx->acregmax	= data->acregmax;
1191 	ctx->acdirmin	= data->acdirmin;
1192 	ctx->acdirmax	= data->acdirmax;
1193 	ctx->nfs_server.protocol = data->proto;
1194 	nfs_validate_transport_protocol(ctx);
1195 	if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1196 		goto out_invalid_transport_udp;
1197 done:
1198 	ctx->skip_reconfig_option_check = true;
1199 	return 0;
1200 
1201 out_inval_auth:
1202 	return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1203 		      data->auth_flavourlen);
1204 
1205 out_no_address:
1206 	return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
1207 
1208 out_invalid_transport_udp:
1209 	return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
1210 }
1211 #endif
1212 
1213 /*
1214  * Parse a monolithic block of data from sys_mount().
1215  */
nfs_fs_context_parse_monolithic(struct fs_context * fc,void * data)1216 static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1217 					   void *data)
1218 {
1219 	if (fc->fs_type == &nfs_fs_type)
1220 		return nfs23_parse_monolithic(fc, data);
1221 
1222 #if IS_ENABLED(CONFIG_NFS_V4)
1223 	if (fc->fs_type == &nfs4_fs_type)
1224 		return nfs4_parse_monolithic(fc, data);
1225 #endif
1226 
1227 	return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
1228 }
1229 
1230 /*
1231  * Validate the preparsed information in the config.
1232  */
nfs_fs_context_validate(struct fs_context * fc)1233 static int nfs_fs_context_validate(struct fs_context *fc)
1234 {
1235 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1236 	struct nfs_subversion *nfs_mod;
1237 	struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
1238 	int max_namelen = PAGE_SIZE;
1239 	int max_pathlen = NFS_MAXPATHLEN;
1240 	int port = 0;
1241 	int ret;
1242 
1243 	if (!fc->source)
1244 		goto out_no_device_name;
1245 
1246 	/* Check for sanity first. */
1247 	if (ctx->minorversion && ctx->version != 4)
1248 		goto out_minorversion_mismatch;
1249 
1250 	if (ctx->options & NFS_OPTION_MIGRATION &&
1251 	    (ctx->version != 4 || ctx->minorversion != 0))
1252 		goto out_migration_misuse;
1253 
1254 	/* Verify that any proto=/mountproto= options match the address
1255 	 * families in the addr=/mountaddr= options.
1256 	 */
1257 	if (ctx->protofamily != AF_UNSPEC &&
1258 	    ctx->protofamily != ctx->nfs_server.address.sa_family)
1259 		goto out_proto_mismatch;
1260 
1261 	if (ctx->mountfamily != AF_UNSPEC) {
1262 		if (ctx->mount_server.addrlen) {
1263 			if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1264 				goto out_mountproto_mismatch;
1265 		} else {
1266 			if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1267 				goto out_mountproto_mismatch;
1268 		}
1269 	}
1270 
1271 	if (!nfs_verify_server_address(sap))
1272 		goto out_no_address;
1273 
1274 	if (ctx->version == 4) {
1275 		if (IS_ENABLED(CONFIG_NFS_V4)) {
1276 			if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1277 				port = NFS_RDMA_PORT;
1278 			else
1279 				port = NFS_PORT;
1280 			max_namelen = NFS4_MAXNAMLEN;
1281 			max_pathlen = NFS4_MAXPATHLEN;
1282 			nfs_validate_transport_protocol(ctx);
1283 			if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1284 				goto out_invalid_transport_udp;
1285 			ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1286 					NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1287 					NFS_MOUNT_LOCAL_FCNTL);
1288 		} else {
1289 			goto out_v4_not_compiled;
1290 		}
1291 	} else {
1292 		nfs_set_mount_transport_protocol(ctx);
1293 #ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
1294 	       if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1295 		       goto out_invalid_transport_udp;
1296 #endif
1297 		if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1298 			port = NFS_RDMA_PORT;
1299 	}
1300 
1301 	nfs_set_port(sap, &ctx->nfs_server.port, port);
1302 
1303 	ret = nfs_parse_source(fc, max_namelen, max_pathlen);
1304 	if (ret < 0)
1305 		return ret;
1306 
1307 	/* Load the NFS protocol module if we haven't done so yet */
1308 	if (!ctx->nfs_mod) {
1309 		nfs_mod = get_nfs_version(ctx->version);
1310 		if (IS_ERR(nfs_mod)) {
1311 			ret = PTR_ERR(nfs_mod);
1312 			goto out_version_unavailable;
1313 		}
1314 		ctx->nfs_mod = nfs_mod;
1315 	}
1316 
1317 	/* Ensure the filesystem context has the correct fs_type */
1318 	if (fc->fs_type != ctx->nfs_mod->nfs_fs) {
1319 		module_put(fc->fs_type->owner);
1320 		__module_get(ctx->nfs_mod->nfs_fs->owner);
1321 		fc->fs_type = ctx->nfs_mod->nfs_fs;
1322 	}
1323 	return 0;
1324 
1325 out_no_device_name:
1326 	return nfs_invalf(fc, "NFS: Device name not specified");
1327 out_v4_not_compiled:
1328 	nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
1329 	return -EPROTONOSUPPORT;
1330 out_invalid_transport_udp:
1331 	return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
1332 out_no_address:
1333 	return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1334 out_mountproto_mismatch:
1335 	return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
1336 out_proto_mismatch:
1337 	return nfs_invalf(fc, "NFS: Server address does not match proto= option");
1338 out_minorversion_mismatch:
1339 	return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
1340 			  ctx->version, ctx->minorversion);
1341 out_migration_misuse:
1342 	return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
1343 out_version_unavailable:
1344 	nfs_errorf(fc, "NFS: Version unavailable");
1345 	return ret;
1346 }
1347 
1348 /*
1349  * Create an NFS superblock by the appropriate method.
1350  */
nfs_get_tree(struct fs_context * fc)1351 static int nfs_get_tree(struct fs_context *fc)
1352 {
1353 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1354 	int err = nfs_fs_context_validate(fc);
1355 
1356 	if (err)
1357 		return err;
1358 	if (!ctx->internal)
1359 		return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
1360 	else
1361 		return nfs_get_tree_common(fc);
1362 }
1363 
1364 /*
1365  * Handle duplication of a configuration.  The caller copied *src into *sc, but
1366  * it can't deal with resource pointers in the filesystem context, so we have
1367  * to do that.  We need to clear pointers, copy data or get extra refs as
1368  * appropriate.
1369  */
nfs_fs_context_dup(struct fs_context * fc,struct fs_context * src_fc)1370 static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1371 {
1372 	struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1373 
1374 	ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1375 	if (!ctx)
1376 		return -ENOMEM;
1377 
1378 	ctx->mntfh = nfs_alloc_fhandle();
1379 	if (!ctx->mntfh) {
1380 		kfree(ctx);
1381 		return -ENOMEM;
1382 	}
1383 	nfs_copy_fh(ctx->mntfh, src->mntfh);
1384 
1385 	__module_get(ctx->nfs_mod->owner);
1386 	ctx->client_address		= NULL;
1387 	ctx->mount_server.hostname	= NULL;
1388 	ctx->nfs_server.export_path	= NULL;
1389 	ctx->nfs_server.hostname	= NULL;
1390 	ctx->fscache_uniq		= NULL;
1391 	ctx->clone_data.fattr		= NULL;
1392 	fc->fs_private = ctx;
1393 	return 0;
1394 }
1395 
nfs_fs_context_free(struct fs_context * fc)1396 static void nfs_fs_context_free(struct fs_context *fc)
1397 {
1398 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1399 
1400 	if (ctx) {
1401 		if (ctx->server)
1402 			nfs_free_server(ctx->server);
1403 		if (ctx->nfs_mod)
1404 			put_nfs_version(ctx->nfs_mod);
1405 		kfree(ctx->client_address);
1406 		kfree(ctx->mount_server.hostname);
1407 		kfree(ctx->nfs_server.export_path);
1408 		kfree(ctx->nfs_server.hostname);
1409 		kfree(ctx->fscache_uniq);
1410 		nfs_free_fhandle(ctx->mntfh);
1411 		nfs_free_fattr(ctx->clone_data.fattr);
1412 		kfree(ctx);
1413 	}
1414 }
1415 
1416 static const struct fs_context_operations nfs_fs_context_ops = {
1417 	.free			= nfs_fs_context_free,
1418 	.dup			= nfs_fs_context_dup,
1419 	.parse_param		= nfs_fs_context_parse_param,
1420 	.parse_monolithic	= nfs_fs_context_parse_monolithic,
1421 	.get_tree		= nfs_get_tree,
1422 	.reconfigure		= nfs_reconfigure,
1423 };
1424 
1425 /*
1426  * Prepare superblock configuration.  We use the namespaces attached to the
1427  * context.  This may be the current process's namespaces, or it may be a
1428  * container's namespaces.
1429  */
nfs_init_fs_context(struct fs_context * fc)1430 static int nfs_init_fs_context(struct fs_context *fc)
1431 {
1432 	struct nfs_fs_context *ctx;
1433 
1434 	ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1435 	if (unlikely(!ctx))
1436 		return -ENOMEM;
1437 
1438 	ctx->mntfh = nfs_alloc_fhandle();
1439 	if (unlikely(!ctx->mntfh)) {
1440 		kfree(ctx);
1441 		return -ENOMEM;
1442 	}
1443 
1444 	ctx->protofamily	= AF_UNSPEC;
1445 	ctx->mountfamily	= AF_UNSPEC;
1446 	ctx->mount_server.port	= NFS_UNSPEC_PORT;
1447 
1448 	if (fc->root) {
1449 		/* reconfigure, start with the current config */
1450 		struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1451 		struct net *net = nfss->nfs_client->cl_net;
1452 
1453 		ctx->flags		= nfss->flags;
1454 		ctx->rsize		= nfss->rsize;
1455 		ctx->wsize		= nfss->wsize;
1456 		ctx->retrans		= nfss->client->cl_timeout->to_retries;
1457 		ctx->selected_flavor	= nfss->client->cl_auth->au_flavor;
1458 		ctx->acregmin		= nfss->acregmin / HZ;
1459 		ctx->acregmax		= nfss->acregmax / HZ;
1460 		ctx->acdirmin		= nfss->acdirmin / HZ;
1461 		ctx->acdirmax		= nfss->acdirmax / HZ;
1462 		ctx->timeo		= 10U * nfss->client->cl_timeout->to_initval / HZ;
1463 		ctx->nfs_server.port	= nfss->port;
1464 		ctx->nfs_server.addrlen	= nfss->nfs_client->cl_addrlen;
1465 		ctx->version		= nfss->nfs_client->rpc_ops->version;
1466 		ctx->minorversion	= nfss->nfs_client->cl_minorversion;
1467 
1468 		memcpy(&ctx->nfs_server.address, &nfss->nfs_client->cl_addr,
1469 			ctx->nfs_server.addrlen);
1470 
1471 		if (fc->net_ns != net) {
1472 			put_net(fc->net_ns);
1473 			fc->net_ns = get_net(net);
1474 		}
1475 
1476 		ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1477 		__module_get(ctx->nfs_mod->owner);
1478 	} else {
1479 		/* defaults */
1480 		ctx->timeo		= NFS_UNSPEC_TIMEO;
1481 		ctx->retrans		= NFS_UNSPEC_RETRANS;
1482 		ctx->acregmin		= NFS_DEF_ACREGMIN;
1483 		ctx->acregmax		= NFS_DEF_ACREGMAX;
1484 		ctx->acdirmin		= NFS_DEF_ACDIRMIN;
1485 		ctx->acdirmax		= NFS_DEF_ACDIRMAX;
1486 		ctx->nfs_server.port	= NFS_UNSPEC_PORT;
1487 		ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1488 		ctx->selected_flavor	= RPC_AUTH_MAXFLAVOR;
1489 		ctx->minorversion	= 0;
1490 		ctx->need_mount		= true;
1491 	}
1492 	fc->fs_private = ctx;
1493 	fc->ops = &nfs_fs_context_ops;
1494 	return 0;
1495 }
1496 
1497 struct file_system_type nfs_fs_type = {
1498 	.owner			= THIS_MODULE,
1499 	.name			= "nfs",
1500 	.init_fs_context	= nfs_init_fs_context,
1501 	.parameters		= nfs_fs_parameters,
1502 	.kill_sb		= nfs_kill_super,
1503 	.fs_flags		= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1504 };
1505 MODULE_ALIAS_FS("nfs");
1506 EXPORT_SYMBOL_GPL(nfs_fs_type);
1507 
1508 #if IS_ENABLED(CONFIG_NFS_V4)
1509 struct file_system_type nfs4_fs_type = {
1510 	.owner			= THIS_MODULE,
1511 	.name			= "nfs4",
1512 	.init_fs_context	= nfs_init_fs_context,
1513 	.parameters		= nfs_fs_parameters,
1514 	.kill_sb		= nfs_kill_super,
1515 	.fs_flags		= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1516 };
1517 MODULE_ALIAS_FS("nfs4");
1518 MODULE_ALIAS("nfs4");
1519 EXPORT_SYMBOL_GPL(nfs4_fs_type);
1520 #endif /* CONFIG_NFS_V4 */
1521