blob: 9a3162055d5d0821224d775de3a9dc10006efc7b [file] [log] [blame]
David Howells9954bf92019-12-10 07:31:04 -05001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/fs/nfs/fs_context.c
4 *
5 * Copyright (C) 1992 Rick Sladkey
6 *
7 * NFS mount handling.
8 *
9 * Split from fs/nfs/super.c by David Howells <dhowells@redhat.com>
10 */
11
12#include <linux/module.h>
13#include <linux/fs.h>
Scott Mayhewe38bb232019-12-10 07:31:12 -050014#include <linux/fs_context.h>
15#include <linux/fs_parser.h>
David Howells9954bf92019-12-10 07:31:04 -050016#include <linux/nfs_fs.h>
17#include <linux/nfs_mount.h>
18#include <linux/nfs4_mount.h>
19#include "nfs.h"
20#include "internal.h"
21
22#define NFSDBG_FACILITY NFSDBG_MOUNT
23
24#if IS_ENABLED(CONFIG_NFS_V3)
25#define NFS_DEFAULT_VERSION 3
26#else
27#define NFS_DEFAULT_VERSION 2
28#endif
29
30#define NFS_MAX_CONNECTIONS 16
31
Scott Mayhewe38bb232019-12-10 07:31:12 -050032enum nfs_param {
33 Opt_ac,
34 Opt_acdirmax,
35 Opt_acdirmin,
36 Opt_acl,
37 Opt_acregmax,
38 Opt_acregmin,
David Howells9954bf92019-12-10 07:31:04 -050039 Opt_actimeo,
Scott Mayhewe38bb232019-12-10 07:31:12 -050040 Opt_addr,
41 Opt_bg,
42 Opt_bsize,
43 Opt_clientaddr,
44 Opt_cto,
45 Opt_fg,
46 Opt_fscache,
47 Opt_hard,
48 Opt_intr,
David Howells9954bf92019-12-10 07:31:04 -050049 Opt_local_lock,
Scott Mayhewe38bb232019-12-10 07:31:12 -050050 Opt_lock,
51 Opt_lookupcache,
52 Opt_migration,
53 Opt_minorversion,
54 Opt_mountaddr,
55 Opt_mounthost,
56 Opt_mountport,
57 Opt_mountproto,
58 Opt_mountvers,
59 Opt_namelen,
60 Opt_nconnect,
61 Opt_port,
62 Opt_posix,
63 Opt_proto,
64 Opt_rdirplus,
65 Opt_rdma,
66 Opt_resvport,
67 Opt_retrans,
68 Opt_retry,
69 Opt_rsize,
70 Opt_sec,
71 Opt_sharecache,
72 Opt_sloppy,
73 Opt_soft,
74 Opt_softerr,
75 Opt_source,
76 Opt_tcp,
77 Opt_timeo,
78 Opt_udp,
79 Opt_v,
80 Opt_vers,
81 Opt_wsize,
David Howells9954bf92019-12-10 07:31:04 -050082};
83
Scott Mayhewe38bb232019-12-10 07:31:12 -050084static const struct fs_parameter_spec nfs_param_specs[] = {
85 fsparam_flag_no("ac", Opt_ac),
86 fsparam_u32 ("acdirmax", Opt_acdirmax),
87 fsparam_u32 ("acdirmin", Opt_acdirmin),
88 fsparam_flag_no("acl", Opt_acl),
89 fsparam_u32 ("acregmax", Opt_acregmax),
90 fsparam_u32 ("acregmin", Opt_acregmin),
91 fsparam_u32 ("actimeo", Opt_actimeo),
92 fsparam_string("addr", Opt_addr),
93 fsparam_flag ("bg", Opt_bg),
94 fsparam_u32 ("bsize", Opt_bsize),
95 fsparam_string("clientaddr", Opt_clientaddr),
96 fsparam_flag_no("cto", Opt_cto),
97 fsparam_flag ("fg", Opt_fg),
98 __fsparam(fs_param_is_string, "fsc", Opt_fscache,
99 fs_param_neg_with_no|fs_param_v_optional),
100 fsparam_flag ("hard", Opt_hard),
101 __fsparam(fs_param_is_flag, "intr", Opt_intr,
102 fs_param_neg_with_no|fs_param_deprecated),
103 fsparam_enum ("local_lock", Opt_local_lock),
104 fsparam_flag_no("lock", Opt_lock),
105 fsparam_enum ("lookupcache", Opt_lookupcache),
106 fsparam_flag_no("migration", Opt_migration),
107 fsparam_u32 ("minorversion", Opt_minorversion),
108 fsparam_string("mountaddr", Opt_mountaddr),
109 fsparam_string("mounthost", Opt_mounthost),
110 fsparam_u32 ("mountport", Opt_mountport),
111 fsparam_string("mountproto", Opt_mountproto),
112 fsparam_u32 ("mountvers", Opt_mountvers),
113 fsparam_u32 ("namlen", Opt_namelen),
114 fsparam_u32 ("nconnect", Opt_nconnect),
115 fsparam_string("nfsvers", Opt_vers),
116 fsparam_u32 ("port", Opt_port),
117 fsparam_flag_no("posix", Opt_posix),
118 fsparam_string("proto", Opt_proto),
119 fsparam_flag_no("rdirplus", Opt_rdirplus),
120 fsparam_flag ("rdma", Opt_rdma),
121 fsparam_flag_no("resvport", Opt_resvport),
122 fsparam_u32 ("retrans", Opt_retrans),
123 fsparam_string("retry", Opt_retry),
124 fsparam_u32 ("rsize", Opt_rsize),
125 fsparam_string("sec", Opt_sec),
126 fsparam_flag_no("sharecache", Opt_sharecache),
127 fsparam_flag ("sloppy", Opt_sloppy),
128 fsparam_flag ("soft", Opt_soft),
129 fsparam_flag ("softerr", Opt_softerr),
130 fsparam_string("source", Opt_source),
131 fsparam_flag ("tcp", Opt_tcp),
132 fsparam_u32 ("timeo", Opt_timeo),
133 fsparam_flag ("udp", Opt_udp),
134 fsparam_flag ("v2", Opt_v),
135 fsparam_flag ("v3", Opt_v),
136 fsparam_flag ("v4", Opt_v),
137 fsparam_flag ("v4.0", Opt_v),
138 fsparam_flag ("v4.1", Opt_v),
139 fsparam_flag ("v4.2", Opt_v),
140 fsparam_string("vers", Opt_vers),
141 fsparam_u32 ("wsize", Opt_wsize),
142 {}
David Howells9954bf92019-12-10 07:31:04 -0500143};
144
145enum {
Scott Mayhewe38bb232019-12-10 07:31:12 -0500146 Opt_local_lock_all,
147 Opt_local_lock_flock,
David Howells9954bf92019-12-10 07:31:04 -0500148 Opt_local_lock_none,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500149 Opt_local_lock_posix,
David Howells9954bf92019-12-10 07:31:04 -0500150};
151
152enum {
Scott Mayhewe38bb232019-12-10 07:31:12 -0500153 Opt_lookupcache_all,
154 Opt_lookupcache_none,
155 Opt_lookupcache_positive,
David Howells9954bf92019-12-10 07:31:04 -0500156};
157
Scott Mayhewe38bb232019-12-10 07:31:12 -0500158static const struct fs_parameter_enum nfs_param_enums[] = {
159 { Opt_local_lock, "all", Opt_local_lock_all },
160 { Opt_local_lock, "flock", Opt_local_lock_flock },
161 { Opt_local_lock, "none", Opt_local_lock_none },
162 { Opt_local_lock, "posix", Opt_local_lock_posix },
163 { Opt_lookupcache, "all", Opt_lookupcache_all },
164 { Opt_lookupcache, "none", Opt_lookupcache_none },
165 { Opt_lookupcache, "pos", Opt_lookupcache_positive },
166 { Opt_lookupcache, "positive", Opt_lookupcache_positive },
167 {}
168};
David Howells9954bf92019-12-10 07:31:04 -0500169
Scott Mayhewe38bb232019-12-10 07:31:12 -0500170static const struct fs_parameter_description nfs_fs_parameters = {
171 .name = "nfs",
172 .specs = nfs_param_specs,
173 .enums = nfs_param_enums,
174};
175
176enum {
177 Opt_vers_2,
178 Opt_vers_3,
179 Opt_vers_4,
180 Opt_vers_4_0,
181 Opt_vers_4_1,
182 Opt_vers_4_2,
183};
184
185static const struct constant_table nfs_vers_tokens[] = {
186 { "2", Opt_vers_2 },
187 { "3", Opt_vers_3 },
188 { "4", Opt_vers_4 },
189 { "4.0", Opt_vers_4_0 },
190 { "4.1", Opt_vers_4_1 },
191 { "4.2", Opt_vers_4_2 },
192};
193
194enum {
195 Opt_xprt_rdma,
196 Opt_xprt_rdma6,
197 Opt_xprt_tcp,
198 Opt_xprt_tcp6,
199 Opt_xprt_udp,
200 Opt_xprt_udp6,
201 nr__Opt_xprt
202};
203
204static const struct constant_table nfs_xprt_protocol_tokens[nr__Opt_xprt] = {
205 { "rdma", Opt_xprt_rdma },
206 { "rdma6", Opt_xprt_rdma6 },
207 { "tcp", Opt_xprt_tcp },
208 { "tcp6", Opt_xprt_tcp6 },
209 { "udp", Opt_xprt_udp },
210 { "udp6", Opt_xprt_udp6 },
211};
212
213enum {
214 Opt_sec_krb5,
215 Opt_sec_krb5i,
216 Opt_sec_krb5p,
217 Opt_sec_lkey,
218 Opt_sec_lkeyi,
219 Opt_sec_lkeyp,
220 Opt_sec_none,
221 Opt_sec_spkm,
222 Opt_sec_spkmi,
223 Opt_sec_spkmp,
224 Opt_sec_sys,
225 nr__Opt_sec
226};
227
228static const struct constant_table nfs_secflavor_tokens[] = {
229 { "krb5", Opt_sec_krb5 },
230 { "krb5i", Opt_sec_krb5i },
231 { "krb5p", Opt_sec_krb5p },
232 { "lkey", Opt_sec_lkey },
233 { "lkeyi", Opt_sec_lkeyi },
234 { "lkeyp", Opt_sec_lkeyp },
235 { "none", Opt_sec_none },
236 { "null", Opt_sec_none },
237 { "spkm3", Opt_sec_spkm },
238 { "spkm3i", Opt_sec_spkmi },
239 { "spkm3p", Opt_sec_spkmp },
240 { "sys", Opt_sec_sys },
David Howells9954bf92019-12-10 07:31:04 -0500241};
242
David Howells5eb005c2019-12-10 07:31:06 -0500243struct nfs_fs_context *nfs_alloc_parsed_mount_data(void)
David Howells9954bf92019-12-10 07:31:04 -0500244{
David Howells5eb005c2019-12-10 07:31:06 -0500245 struct nfs_fs_context *ctx;
David Howells9954bf92019-12-10 07:31:04 -0500246
David Howells5eb005c2019-12-10 07:31:06 -0500247 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
248 if (ctx) {
249 ctx->timeo = NFS_UNSPEC_TIMEO;
250 ctx->retrans = NFS_UNSPEC_RETRANS;
251 ctx->acregmin = NFS_DEF_ACREGMIN;
252 ctx->acregmax = NFS_DEF_ACREGMAX;
253 ctx->acdirmin = NFS_DEF_ACDIRMIN;
254 ctx->acdirmax = NFS_DEF_ACDIRMAX;
255 ctx->mount_server.port = NFS_UNSPEC_PORT;
256 ctx->nfs_server.port = NFS_UNSPEC_PORT;
257 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
258 ctx->selected_flavor = RPC_AUTH_MAXFLAVOR;
259 ctx->minorversion = 0;
260 ctx->need_mount = true;
261 ctx->net = current->nsproxy->net_ns;
262 ctx->lsm_opts = NULL;
David Howells9954bf92019-12-10 07:31:04 -0500263 }
David Howells5eb005c2019-12-10 07:31:06 -0500264 return ctx;
David Howells9954bf92019-12-10 07:31:04 -0500265}
266
David Howells5eb005c2019-12-10 07:31:06 -0500267void nfs_free_parsed_mount_data(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500268{
David Howells5eb005c2019-12-10 07:31:06 -0500269 if (ctx) {
270 kfree(ctx->client_address);
271 kfree(ctx->mount_server.hostname);
272 kfree(ctx->nfs_server.export_path);
273 kfree(ctx->nfs_server.hostname);
274 kfree(ctx->fscache_uniq);
275 security_free_mnt_opts(&ctx->lsm_opts);
276 kfree(ctx);
David Howells9954bf92019-12-10 07:31:04 -0500277 }
278}
279
280/*
281 * Sanity-check a server address provided by the mount command.
282 *
283 * Address family must be initialized, and address must not be
284 * the ANY address for that family.
285 */
286static int nfs_verify_server_address(struct sockaddr *addr)
287{
288 switch (addr->sa_family) {
289 case AF_INET: {
290 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
291 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
292 }
293 case AF_INET6: {
294 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
295 return !ipv6_addr_any(sa);
296 }
297 }
298
299 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
300 return 0;
301}
302
303/*
304 * Sanity check the NFS transport protocol.
305 *
306 */
David Howells5eb005c2019-12-10 07:31:06 -0500307static void nfs_validate_transport_protocol(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500308{
David Howells5eb005c2019-12-10 07:31:06 -0500309 switch (ctx->nfs_server.protocol) {
David Howells9954bf92019-12-10 07:31:04 -0500310 case XPRT_TRANSPORT_UDP:
311 case XPRT_TRANSPORT_TCP:
312 case XPRT_TRANSPORT_RDMA:
313 break;
314 default:
David Howells5eb005c2019-12-10 07:31:06 -0500315 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500316 }
317}
318
319/*
320 * For text based NFSv2/v3 mounts, the mount protocol transport default
321 * settings should depend upon the specified NFS transport.
322 */
David Howells5eb005c2019-12-10 07:31:06 -0500323static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500324{
David Howells5eb005c2019-12-10 07:31:06 -0500325 nfs_validate_transport_protocol(ctx);
David Howells9954bf92019-12-10 07:31:04 -0500326
David Howells5eb005c2019-12-10 07:31:06 -0500327 if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
328 ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
David Howells9954bf92019-12-10 07:31:04 -0500329 return;
David Howells5eb005c2019-12-10 07:31:06 -0500330 switch (ctx->nfs_server.protocol) {
David Howells9954bf92019-12-10 07:31:04 -0500331 case XPRT_TRANSPORT_UDP:
David Howells5eb005c2019-12-10 07:31:06 -0500332 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500333 break;
334 case XPRT_TRANSPORT_TCP:
335 case XPRT_TRANSPORT_RDMA:
David Howells5eb005c2019-12-10 07:31:06 -0500336 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500337 }
338}
339
340/*
341 * Add 'flavor' to 'auth_info' if not already present.
342 * Returns true if 'flavor' ends up in the list, false otherwise
343 */
David Howellse5581002019-12-10 07:31:10 -0500344static int nfs_auth_info_add(struct nfs_fs_context *ctx,
345 struct nfs_auth_info *auth_info,
346 rpc_authflavor_t flavor)
David Howells9954bf92019-12-10 07:31:04 -0500347{
348 unsigned int i;
349 unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
350
351 /* make sure this flavor isn't already in the list */
352 for (i = 0; i < auth_info->flavor_len; i++) {
353 if (flavor == auth_info->flavors[i])
David Howellse5581002019-12-10 07:31:10 -0500354 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500355 }
356
357 if (auth_info->flavor_len + 1 >= max_flavor_len) {
358 dfprintk(MOUNT, "NFS: too many sec= flavors\n");
David Howellse5581002019-12-10 07:31:10 -0500359 return -EINVAL;
David Howells9954bf92019-12-10 07:31:04 -0500360 }
361
362 auth_info->flavors[auth_info->flavor_len++] = flavor;
David Howellse5581002019-12-10 07:31:10 -0500363 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500364}
365
366/*
367 * Parse the value of the 'sec=' option.
368 */
Scott Mayhewe38bb232019-12-10 07:31:12 -0500369static int nfs_parse_security_flavors(struct nfs_fs_context *ctx,
370 struct fs_parameter *param)
David Howells9954bf92019-12-10 07:31:04 -0500371{
David Howells9954bf92019-12-10 07:31:04 -0500372 rpc_authflavor_t pseudoflavor;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500373 char *string = param->string, *p;
David Howellse5581002019-12-10 07:31:10 -0500374 int ret;
David Howells9954bf92019-12-10 07:31:04 -0500375
Scott Mayhewe38bb232019-12-10 07:31:12 -0500376 dfprintk(MOUNT, "NFS: parsing %s=%s option\n", param->key, param->string);
David Howells9954bf92019-12-10 07:31:04 -0500377
Scott Mayhewe38bb232019-12-10 07:31:12 -0500378 while ((p = strsep(&string, ":")) != NULL) {
379 if (!*p)
380 continue;
381 switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
David Howells9954bf92019-12-10 07:31:04 -0500382 case Opt_sec_none:
383 pseudoflavor = RPC_AUTH_NULL;
384 break;
385 case Opt_sec_sys:
386 pseudoflavor = RPC_AUTH_UNIX;
387 break;
388 case Opt_sec_krb5:
389 pseudoflavor = RPC_AUTH_GSS_KRB5;
390 break;
391 case Opt_sec_krb5i:
392 pseudoflavor = RPC_AUTH_GSS_KRB5I;
393 break;
394 case Opt_sec_krb5p:
395 pseudoflavor = RPC_AUTH_GSS_KRB5P;
396 break;
397 case Opt_sec_lkey:
398 pseudoflavor = RPC_AUTH_GSS_LKEY;
399 break;
400 case Opt_sec_lkeyi:
401 pseudoflavor = RPC_AUTH_GSS_LKEYI;
402 break;
403 case Opt_sec_lkeyp:
404 pseudoflavor = RPC_AUTH_GSS_LKEYP;
405 break;
406 case Opt_sec_spkm:
407 pseudoflavor = RPC_AUTH_GSS_SPKM;
408 break;
409 case Opt_sec_spkmi:
410 pseudoflavor = RPC_AUTH_GSS_SPKMI;
411 break;
412 case Opt_sec_spkmp:
413 pseudoflavor = RPC_AUTH_GSS_SPKMP;
414 break;
415 default:
416 dfprintk(MOUNT,
417 "NFS: sec= option '%s' not recognized\n", p);
David Howellse5581002019-12-10 07:31:10 -0500418 return -EINVAL;
David Howells9954bf92019-12-10 07:31:04 -0500419 }
420
David Howellse5581002019-12-10 07:31:10 -0500421 ret = nfs_auth_info_add(ctx, &ctx->auth_info, pseudoflavor);
422 if (ret < 0)
423 return ret;
David Howells9954bf92019-12-10 07:31:04 -0500424 }
425
David Howellse5581002019-12-10 07:31:10 -0500426 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500427}
428
David Howellse5581002019-12-10 07:31:10 -0500429static int nfs_parse_version_string(struct nfs_fs_context *ctx,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500430 const char *string)
David Howells9954bf92019-12-10 07:31:04 -0500431{
David Howells5eb005c2019-12-10 07:31:06 -0500432 ctx->flags &= ~NFS_MOUNT_VER3;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500433 switch (lookup_constant(nfs_vers_tokens, string, -1)) {
David Howells9954bf92019-12-10 07:31:04 -0500434 case Opt_vers_2:
David Howells5eb005c2019-12-10 07:31:06 -0500435 ctx->version = 2;
David Howells9954bf92019-12-10 07:31:04 -0500436 break;
437 case Opt_vers_3:
David Howells5eb005c2019-12-10 07:31:06 -0500438 ctx->flags |= NFS_MOUNT_VER3;
439 ctx->version = 3;
David Howells9954bf92019-12-10 07:31:04 -0500440 break;
441 case Opt_vers_4:
442 /* Backward compatibility option. In future,
443 * the mount program should always supply
444 * a NFSv4 minor version number.
445 */
David Howells5eb005c2019-12-10 07:31:06 -0500446 ctx->version = 4;
David Howells9954bf92019-12-10 07:31:04 -0500447 break;
448 case Opt_vers_4_0:
David Howells5eb005c2019-12-10 07:31:06 -0500449 ctx->version = 4;
450 ctx->minorversion = 0;
David Howells9954bf92019-12-10 07:31:04 -0500451 break;
452 case Opt_vers_4_1:
David Howells5eb005c2019-12-10 07:31:06 -0500453 ctx->version = 4;
454 ctx->minorversion = 1;
David Howells9954bf92019-12-10 07:31:04 -0500455 break;
456 case Opt_vers_4_2:
David Howells5eb005c2019-12-10 07:31:06 -0500457 ctx->version = 4;
458 ctx->minorversion = 2;
David Howells9954bf92019-12-10 07:31:04 -0500459 break;
460 default:
David Howellse5581002019-12-10 07:31:10 -0500461 dfprintk(MOUNT, "NFS: Unsupported NFS version\n");
462 return -EINVAL;
David Howells9954bf92019-12-10 07:31:04 -0500463 }
David Howellse5581002019-12-10 07:31:10 -0500464 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500465}
466
David Howells9954bf92019-12-10 07:31:04 -0500467/*
Scott Mayhewe38bb232019-12-10 07:31:12 -0500468 * Parse a single mount parameter.
David Howells9954bf92019-12-10 07:31:04 -0500469 */
Scott Mayhewe38bb232019-12-10 07:31:12 -0500470static int nfs_fs_context_parse_param(struct nfs_fs_context *ctx,
471 struct fs_parameter *param)
David Howells9954bf92019-12-10 07:31:04 -0500472{
Scott Mayhewe38bb232019-12-10 07:31:12 -0500473 struct fs_parse_result result;
474 unsigned short protofamily, mountfamily;
475 unsigned int len;
476 int ret, opt;
David Howells9954bf92019-12-10 07:31:04 -0500477
Scott Mayhewe38bb232019-12-10 07:31:12 -0500478 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", param->key);
David Howells9954bf92019-12-10 07:31:04 -0500479
Scott Mayhewe38bb232019-12-10 07:31:12 -0500480 opt = fs_parse(NULL, &nfs_fs_parameters, param, &result);
481 if (opt < 0)
482 return ctx->sloppy ? 1 : opt;
483
484 switch (opt) {
David Howells9954bf92019-12-10 07:31:04 -0500485 /*
486 * boolean options: foo/nofoo
487 */
David Howellscbd071b2019-12-10 07:31:08 -0500488 case Opt_soft:
489 ctx->flags |= NFS_MOUNT_SOFT;
490 ctx->flags &= ~NFS_MOUNT_SOFTERR;
491 break;
492 case Opt_softerr:
493 ctx->flags |= NFS_MOUNT_SOFTERR;
494 ctx->flags &= ~NFS_MOUNT_SOFT;
495 break;
496 case Opt_hard:
497 ctx->flags &= ~(NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR);
498 break;
499 case Opt_posix:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500500 if (result.negated)
501 ctx->flags &= ~NFS_MOUNT_POSIX;
502 else
503 ctx->flags |= NFS_MOUNT_POSIX;
David Howellscbd071b2019-12-10 07:31:08 -0500504 break;
505 case Opt_cto:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500506 if (result.negated)
507 ctx->flags |= NFS_MOUNT_NOCTO;
508 else
509 ctx->flags &= ~NFS_MOUNT_NOCTO;
David Howellscbd071b2019-12-10 07:31:08 -0500510 break;
511 case Opt_ac:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500512 if (result.negated)
513 ctx->flags |= NFS_MOUNT_NOAC;
514 else
515 ctx->flags &= ~NFS_MOUNT_NOAC;
David Howellscbd071b2019-12-10 07:31:08 -0500516 break;
517 case Opt_lock:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500518 if (result.negated) {
519 ctx->flags |= NFS_MOUNT_NONLM;
520 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
521 } else {
522 ctx->flags &= ~NFS_MOUNT_NONLM;
523 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
524 }
David Howellscbd071b2019-12-10 07:31:08 -0500525 break;
526 case Opt_udp:
527 ctx->flags &= ~NFS_MOUNT_TCP;
528 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
529 break;
530 case Opt_tcp:
531 ctx->flags |= NFS_MOUNT_TCP;
532 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
533 break;
534 case Opt_rdma:
535 ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
536 ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500537 xprt_load_transport(param->key);
David Howellscbd071b2019-12-10 07:31:08 -0500538 break;
539 case Opt_acl:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500540 if (result.negated)
541 ctx->flags |= NFS_MOUNT_NOACL;
542 else
543 ctx->flags &= ~NFS_MOUNT_NOACL;
David Howellscbd071b2019-12-10 07:31:08 -0500544 break;
545 case Opt_rdirplus:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500546 if (result.negated)
547 ctx->flags |= NFS_MOUNT_NORDIRPLUS;
548 else
549 ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
David Howellscbd071b2019-12-10 07:31:08 -0500550 break;
551 case Opt_sharecache:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500552 if (result.negated)
553 ctx->flags |= NFS_MOUNT_UNSHARED;
554 else
555 ctx->flags &= ~NFS_MOUNT_UNSHARED;
David Howellscbd071b2019-12-10 07:31:08 -0500556 break;
557 case Opt_resvport:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500558 if (result.negated)
559 ctx->flags |= NFS_MOUNT_NORESVPORT;
560 else
561 ctx->flags &= ~NFS_MOUNT_NORESVPORT;
David Howellscbd071b2019-12-10 07:31:08 -0500562 break;
563 case Opt_fscache:
David Howellscbd071b2019-12-10 07:31:08 -0500564 kfree(ctx->fscache_uniq);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500565 ctx->fscache_uniq = param->string;
566 param->string = NULL;
567 if (result.negated)
568 ctx->options &= ~NFS_OPTION_FSCACHE;
569 else
570 ctx->options |= NFS_OPTION_FSCACHE;
David Howellscbd071b2019-12-10 07:31:08 -0500571 break;
572 case Opt_migration:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500573 if (result.negated)
574 ctx->options &= ~NFS_OPTION_MIGRATION;
575 else
576 ctx->options |= NFS_OPTION_MIGRATION;
David Howellscbd071b2019-12-10 07:31:08 -0500577 break;
David Howells9954bf92019-12-10 07:31:04 -0500578
579 /*
580 * options that take numeric values
581 */
David Howellscbd071b2019-12-10 07:31:08 -0500582 case Opt_port:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500583 if (result.uint_32 > USHRT_MAX)
584 goto out_of_bounds;
585 ctx->nfs_server.port = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500586 break;
587 case Opt_rsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500588 ctx->rsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500589 break;
590 case Opt_wsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500591 ctx->wsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500592 break;
593 case Opt_bsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500594 ctx->bsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500595 break;
596 case Opt_timeo:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500597 if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
598 goto out_of_bounds;
599 ctx->timeo = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500600 break;
601 case Opt_retrans:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500602 if (result.uint_32 > INT_MAX)
603 goto out_of_bounds;
604 ctx->retrans = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500605 break;
606 case Opt_acregmin:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500607 ctx->acregmin = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500608 break;
609 case Opt_acregmax:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500610 ctx->acregmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500611 break;
612 case Opt_acdirmin:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500613 ctx->acdirmin = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500614 break;
615 case Opt_acdirmax:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500616 ctx->acdirmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500617 break;
618 case Opt_actimeo:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500619 ctx->acregmin = result.uint_32;
620 ctx->acregmax = result.uint_32;
621 ctx->acdirmin = result.uint_32;
622 ctx->acdirmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500623 break;
624 case Opt_namelen:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500625 ctx->namlen = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500626 break;
627 case Opt_mountport:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500628 if (result.uint_32 > USHRT_MAX)
629 goto out_of_bounds;
630 ctx->mount_server.port = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500631 break;
632 case Opt_mountvers:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500633 if (result.uint_32 < NFS_MNT_VERSION ||
634 result.uint_32 > NFS_MNT3_VERSION)
635 goto out_of_bounds;
636 ctx->mount_server.version = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500637 break;
638 case Opt_minorversion:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500639 if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
640 goto out_of_bounds;
641 ctx->minorversion = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500642 break;
David Howells9954bf92019-12-10 07:31:04 -0500643
644 /*
645 * options that take text values
646 */
Scott Mayhewe38bb232019-12-10 07:31:12 -0500647 case Opt_v:
648 ret = nfs_parse_version_string(ctx, param->key + 1);
649 if (ret < 0)
650 return ret;
651 break;
652 case Opt_vers:
653 ret = nfs_parse_version_string(ctx, param->string);
David Howellse5581002019-12-10 07:31:10 -0500654 if (ret < 0)
655 return ret;
David Howellscbd071b2019-12-10 07:31:08 -0500656 break;
657 case Opt_sec:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500658 ret = nfs_parse_security_flavors(ctx, param);
David Howellse5581002019-12-10 07:31:10 -0500659 if (ret < 0)
660 return ret;
David Howellscbd071b2019-12-10 07:31:08 -0500661 break;
David Howells9954bf92019-12-10 07:31:04 -0500662
Scott Mayhewe38bb232019-12-10 07:31:12 -0500663 case Opt_proto:
664 protofamily = AF_INET;
665 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
David Howellscbd071b2019-12-10 07:31:08 -0500666 case Opt_xprt_udp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500667 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500668 /* fall through */
669 case Opt_xprt_udp:
670 ctx->flags &= ~NFS_MOUNT_TCP;
671 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500672 break;
David Howellscbd071b2019-12-10 07:31:08 -0500673 case Opt_xprt_tcp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500674 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500675 /* fall through */
676 case Opt_xprt_tcp:
677 ctx->flags |= NFS_MOUNT_TCP;
678 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
679 break;
680 case Opt_xprt_rdma6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500681 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500682 /* fall through */
683 case Opt_xprt_rdma:
684 /* vector side protocols to TCP */
685 ctx->flags |= NFS_MOUNT_TCP;
686 ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500687 xprt_load_transport(param->string);
David Howellscbd071b2019-12-10 07:31:08 -0500688 break;
689 default:
David Howellse5581002019-12-10 07:31:10 -0500690 dfprintk(MOUNT, "NFS: unrecognized transport protocol\n");
David Howellscbd071b2019-12-10 07:31:08 -0500691 return -EINVAL;
692 }
David Howells9954bf92019-12-10 07:31:04 -0500693
Scott Mayhewe38bb232019-12-10 07:31:12 -0500694 ctx->protofamily = protofamily;
695 break;
696
697 case Opt_mountproto:
698 mountfamily = AF_INET;
699 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
David Howellscbd071b2019-12-10 07:31:08 -0500700 case Opt_xprt_udp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500701 mountfamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500702 /* fall through */
703 case Opt_xprt_udp:
704 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500705 break;
David Howellscbd071b2019-12-10 07:31:08 -0500706 case Opt_xprt_tcp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500707 mountfamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500708 /* fall through */
709 case Opt_xprt_tcp:
710 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500711 break;
David Howellscbd071b2019-12-10 07:31:08 -0500712 case Opt_xprt_rdma: /* not used for side protocols */
713 default:
David Howellse5581002019-12-10 07:31:10 -0500714 dfprintk(MOUNT, "NFS: unrecognized transport protocol\n");
David Howellscbd071b2019-12-10 07:31:08 -0500715 return -EINVAL;
716 }
Scott Mayhewe38bb232019-12-10 07:31:12 -0500717 ctx->mountfamily = mountfamily;
David Howellscbd071b2019-12-10 07:31:08 -0500718 break;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500719
David Howellscbd071b2019-12-10 07:31:08 -0500720 case Opt_addr:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500721 len = rpc_pton(ctx->net, param->string, param->size,
722 &ctx->nfs_server.address,
723 sizeof(ctx->nfs_server._address));
724 if (len == 0)
David Howellscbd071b2019-12-10 07:31:08 -0500725 goto out_invalid_address;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500726 ctx->nfs_server.addrlen = len;
David Howellscbd071b2019-12-10 07:31:08 -0500727 break;
728 case Opt_clientaddr:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500729 kfree(ctx->client_address);
730 ctx->client_address = param->string;
731 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500732 break;
733 case Opt_mounthost:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500734 kfree(ctx->mount_server.hostname);
735 ctx->mount_server.hostname = param->string;
736 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500737 break;
738 case Opt_mountaddr:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500739 len = rpc_pton(ctx->net, param->string, param->size,
740 &ctx->mount_server.address,
741 sizeof(ctx->mount_server._address));
742 if (len == 0)
David Howellscbd071b2019-12-10 07:31:08 -0500743 goto out_invalid_address;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500744 ctx->mount_server.addrlen = len;
David Howellscbd071b2019-12-10 07:31:08 -0500745 break;
746 case Opt_nconnect:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500747 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
748 goto out_of_bounds;
749 ctx->nfs_server.nconnect = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500750 break;
751 case Opt_lookupcache:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500752 switch (result.uint_32) {
David Howellscbd071b2019-12-10 07:31:08 -0500753 case Opt_lookupcache_all:
754 ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
David Howells9954bf92019-12-10 07:31:04 -0500755 break;
David Howellscbd071b2019-12-10 07:31:08 -0500756 case Opt_lookupcache_positive:
757 ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
758 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
David Howells9954bf92019-12-10 07:31:04 -0500759 break;
David Howellscbd071b2019-12-10 07:31:08 -0500760 case Opt_lookupcache_none:
761 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
David Howells9954bf92019-12-10 07:31:04 -0500762 break;
David Howellscbd071b2019-12-10 07:31:08 -0500763 default:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500764 goto out_invalid_value;
David Howellscbd071b2019-12-10 07:31:08 -0500765 }
766 break;
David Howellscbd071b2019-12-10 07:31:08 -0500767 case Opt_local_lock:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500768 switch (result.uint_32) {
David Howellscbd071b2019-12-10 07:31:08 -0500769 case Opt_local_lock_all:
770 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
771 NFS_MOUNT_LOCAL_FCNTL);
David Howells9954bf92019-12-10 07:31:04 -0500772 break;
David Howellscbd071b2019-12-10 07:31:08 -0500773 case Opt_local_lock_flock:
774 ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
David Howells9954bf92019-12-10 07:31:04 -0500775 break;
David Howellscbd071b2019-12-10 07:31:08 -0500776 case Opt_local_lock_posix:
777 ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
David Howells9954bf92019-12-10 07:31:04 -0500778 break;
David Howellscbd071b2019-12-10 07:31:08 -0500779 case Opt_local_lock_none:
780 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
781 NFS_MOUNT_LOCAL_FCNTL);
David Howells9954bf92019-12-10 07:31:04 -0500782 break;
David Howellscbd071b2019-12-10 07:31:08 -0500783 default:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500784 goto out_invalid_value;
David Howellscbd071b2019-12-10 07:31:08 -0500785 }
786 break;
David Howells9954bf92019-12-10 07:31:04 -0500787
788 /*
789 * Special options
790 */
David Howellscbd071b2019-12-10 07:31:08 -0500791 case Opt_sloppy:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500792 ctx->sloppy = true;
David Howellscbd071b2019-12-10 07:31:08 -0500793 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
794 break;
David Howells9954bf92019-12-10 07:31:04 -0500795 }
796
David Howellsf8ee01e2019-12-10 07:31:07 -0500797 return 0;
798
David Howellsf8ee01e2019-12-10 07:31:07 -0500799out_invalid_value:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500800 printk(KERN_INFO "NFS: Bad mount option value specified\n");
David Howellsf8ee01e2019-12-10 07:31:07 -0500801 return -EINVAL;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500802out_invalid_address:
803 printk(KERN_INFO "NFS: Bad IP address specified\n");
804 return -EINVAL;
805out_of_bounds:
806 printk(KERN_INFO "NFS: Value for '%s' out of range\n", param->key);
807 return -ERANGE;
808}
809
810/* cribbed from generic_parse_monolithic and vfs_parse_fs_string */
811static int nfs_fs_context_parse_option(struct nfs_fs_context *ctx, char *p)
812{
813 int ret;
814 char *key = p, *value;
815 size_t v_size = 0;
816 struct fs_parameter param;
817
818 memset(&param, 0, sizeof(param));
819 value = strchr(key, '=');
820 if (value && value != key) {
821 *value++ = 0;
822 v_size = strlen(value);
823 }
824 param.key = key;
825 param.type = fs_value_is_flag;
826 param.size = v_size;
827 if (v_size > 0) {
828 param.type = fs_value_is_string;
829 param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
830 if (!param.string)
831 return -ENOMEM;
832 }
833 ret = nfs_fs_context_parse_param(ctx, &param);
834 kfree(param.string);
835 return ret;
David Howellsf8ee01e2019-12-10 07:31:07 -0500836}
837
838/*
839 * Error-check and convert a string of mount options from user space into
840 * a data structure. The whole mount string is processed; bad options are
841 * skipped as they are encountered. If there were no errors, return 1;
842 * otherwise return 0 (zero).
843 */
844int nfs_parse_mount_options(char *raw, struct nfs_fs_context *ctx)
845{
846 char *p;
847 int rc, sloppy = 0, invalid_option = 0;
848
849 if (!raw) {
850 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
851 return 1;
852 }
853 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
854
855 rc = security_sb_eat_lsm_opts(raw, &ctx->lsm_opts);
856 if (rc)
857 goto out_security_failure;
858
859 while ((p = strsep(&raw, ",")) != NULL) {
860 if (!*p)
861 continue;
862 if (nfs_fs_context_parse_option(ctx, p) < 0)
863 invalid_option = true;
864 }
865
David Howells9954bf92019-12-10 07:31:04 -0500866 if (!sloppy && invalid_option)
867 return 0;
868
David Howells5eb005c2019-12-10 07:31:06 -0500869 if (ctx->minorversion && ctx->version != 4)
David Howells9954bf92019-12-10 07:31:04 -0500870 goto out_minorversion_mismatch;
871
David Howells5eb005c2019-12-10 07:31:06 -0500872 if (ctx->options & NFS_OPTION_MIGRATION &&
873 (ctx->version != 4 || ctx->minorversion != 0))
David Howells9954bf92019-12-10 07:31:04 -0500874 goto out_migration_misuse;
875
876 /*
877 * verify that any proto=/mountproto= options match the address
878 * families in the addr=/mountaddr= options.
879 */
David Howellsf8ee01e2019-12-10 07:31:07 -0500880 if (ctx->protofamily != AF_UNSPEC &&
David Howellse5581002019-12-10 07:31:10 -0500881 ctx->protofamily != ctx->nfs_server.address.sa_family)
David Howells9954bf92019-12-10 07:31:04 -0500882 goto out_proto_mismatch;
883
David Howellsf8ee01e2019-12-10 07:31:07 -0500884 if (ctx->mountfamily != AF_UNSPEC) {
David Howells5eb005c2019-12-10 07:31:06 -0500885 if (ctx->mount_server.addrlen) {
David Howellse5581002019-12-10 07:31:10 -0500886 if (ctx->mountfamily != ctx->mount_server.address.sa_family)
David Howells9954bf92019-12-10 07:31:04 -0500887 goto out_mountproto_mismatch;
888 } else {
David Howellse5581002019-12-10 07:31:10 -0500889 if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
David Howells9954bf92019-12-10 07:31:04 -0500890 goto out_mountproto_mismatch;
891 }
892 }
893
894 return 1;
895
David Howellsf8ee01e2019-12-10 07:31:07 -0500896out_minorversion_mismatch:
897 printk(KERN_INFO "NFS: mount option vers=%u does not support "
898 "minorversion=%u\n", ctx->version, ctx->minorversion);
899 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500900out_mountproto_mismatch:
901 printk(KERN_INFO "NFS: mount server address does not match mountproto= "
902 "option\n");
903 return 0;
904out_proto_mismatch:
905 printk(KERN_INFO "NFS: server address does not match proto= option\n");
906 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500907out_migration_misuse:
908 printk(KERN_INFO
909 "NFS: 'migration' not supported for this NFS version\n");
David Howellsf8ee01e2019-12-10 07:31:07 -0500910 return -EINVAL;
David Howells9954bf92019-12-10 07:31:04 -0500911out_security_failure:
912 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
913 return 0;
914}
915
916/*
917 * Split "dev_name" into "hostname:export_path".
918 *
919 * The leftmost colon demarks the split between the server's hostname
920 * and the export path. If the hostname starts with a left square
921 * bracket, then it may contain colons.
922 *
923 * Note: caller frees hostname and export path, even on error.
924 */
David Howellse5581002019-12-10 07:31:10 -0500925static int nfs_parse_devname(struct nfs_fs_context *ctx,
926 const char *dev_name,
927 size_t maxnamlen, size_t maxpathlen)
David Howells9954bf92019-12-10 07:31:04 -0500928{
929 size_t len;
930 char *end;
931
932 if (unlikely(!dev_name || !*dev_name)) {
933 dfprintk(MOUNT, "NFS: device name not specified\n");
934 return -EINVAL;
935 }
936
937 /* Is the host name protected with square brakcets? */
938 if (*dev_name == '[') {
939 end = strchr(++dev_name, ']');
940 if (end == NULL || end[1] != ':')
941 goto out_bad_devname;
942
943 len = end - dev_name;
944 end++;
945 } else {
946 char *comma;
947
948 end = strchr(dev_name, ':');
949 if (end == NULL)
950 goto out_bad_devname;
951 len = end - dev_name;
952
953 /* kill possible hostname list: not supported */
954 comma = strchr(dev_name, ',');
955 if (comma != NULL && comma < end)
956 len = comma - dev_name;
957 }
958
959 if (len > maxnamlen)
960 goto out_hostname;
961
962 /* N.B. caller will free nfs_server.hostname in all cases */
David Howellse5581002019-12-10 07:31:10 -0500963 ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
964 if (!ctx->nfs_server.hostname)
David Howells9954bf92019-12-10 07:31:04 -0500965 goto out_nomem;
966 len = strlen(++end);
967 if (len > maxpathlen)
968 goto out_path;
David Howellse5581002019-12-10 07:31:10 -0500969 ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
970 if (!ctx->nfs_server.export_path)
David Howells9954bf92019-12-10 07:31:04 -0500971 goto out_nomem;
972
David Howellse5581002019-12-10 07:31:10 -0500973 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", ctx->nfs_server.export_path);
David Howells9954bf92019-12-10 07:31:04 -0500974 return 0;
975
976out_bad_devname:
977 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
978 return -EINVAL;
979
980out_nomem:
981 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
982 return -ENOMEM;
983
984out_hostname:
985 dfprintk(MOUNT, "NFS: server hostname too long\n");
986 return -ENAMETOOLONG;
987
988out_path:
989 dfprintk(MOUNT, "NFS: export pathname too long\n");
990 return -ENAMETOOLONG;
991}
992
993/*
David Howellse5581002019-12-10 07:31:10 -0500994 * Parse monolithic NFS2/NFS3 mount data
David Howells9954bf92019-12-10 07:31:04 -0500995 * - fills in the mount root filehandle
996 *
997 * For option strings, user space handles the following behaviors:
998 *
999 * + DNS: mapping server host name to IP address ("addr=" option)
1000 *
1001 * + failure mode: how to behave if a mount request can't be handled
1002 * immediately ("fg/bg" option)
1003 *
1004 * + retry: how often to retry a mount request ("retry=" option)
1005 *
1006 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1007 * mountproto=tcp after mountproto=udp, and so on
1008 */
1009static int nfs23_validate_mount_data(void *options,
David Howells5eb005c2019-12-10 07:31:06 -05001010 struct nfs_fs_context *ctx,
David Howells9954bf92019-12-10 07:31:04 -05001011 struct nfs_fh *mntfh,
1012 const char *dev_name)
1013{
1014 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
David Howells5eb005c2019-12-10 07:31:06 -05001015 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001016 int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
1017
1018 if (data == NULL)
1019 goto out_no_data;
1020
David Howells5eb005c2019-12-10 07:31:06 -05001021 ctx->version = NFS_DEFAULT_VERSION;
David Howells9954bf92019-12-10 07:31:04 -05001022 switch (data->version) {
1023 case 1:
1024 data->namlen = 0; /* fall through */
1025 case 2:
1026 data->bsize = 0; /* fall through */
1027 case 3:
1028 if (data->flags & NFS_MOUNT_VER3)
1029 goto out_no_v3;
1030 data->root.size = NFS2_FHSIZE;
1031 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1032 /* Turn off security negotiation */
1033 extra_flags |= NFS_MOUNT_SECFLAVOUR;
1034 /* fall through */
1035 case 4:
1036 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1037 goto out_no_sec;
1038 /* fall through */
1039 case 5:
1040 memset(data->context, 0, sizeof(data->context));
1041 /* fall through */
1042 case 6:
1043 if (data->flags & NFS_MOUNT_VER3) {
1044 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1045 goto out_invalid_fh;
1046 mntfh->size = data->root.size;
David Howells5eb005c2019-12-10 07:31:06 -05001047 ctx->version = 3;
David Howells9954bf92019-12-10 07:31:04 -05001048 } else {
1049 mntfh->size = NFS2_FHSIZE;
David Howells5eb005c2019-12-10 07:31:06 -05001050 ctx->version = 2;
David Howells9954bf92019-12-10 07:31:04 -05001051 }
1052
1053
1054 memcpy(mntfh->data, data->root.data, mntfh->size);
1055 if (mntfh->size < sizeof(mntfh->data))
1056 memset(mntfh->data + mntfh->size, 0,
1057 sizeof(mntfh->data) - mntfh->size);
1058
1059 /*
David Howells5eb005c2019-12-10 07:31:06 -05001060 * Translate to nfs_fs_context, which nfs_fill_super
David Howells9954bf92019-12-10 07:31:04 -05001061 * can deal with.
1062 */
David Howells5eb005c2019-12-10 07:31:06 -05001063 ctx->flags = data->flags & NFS_MOUNT_FLAGMASK;
1064 ctx->flags |= extra_flags;
1065 ctx->rsize = data->rsize;
1066 ctx->wsize = data->wsize;
1067 ctx->timeo = data->timeo;
1068 ctx->retrans = data->retrans;
1069 ctx->acregmin = data->acregmin;
1070 ctx->acregmax = data->acregmax;
1071 ctx->acdirmin = data->acdirmin;
1072 ctx->acdirmax = data->acdirmax;
1073 ctx->need_mount = false;
David Howells9954bf92019-12-10 07:31:04 -05001074
1075 memcpy(sap, &data->addr, sizeof(data->addr));
David Howells5eb005c2019-12-10 07:31:06 -05001076 ctx->nfs_server.addrlen = sizeof(data->addr);
1077 ctx->nfs_server.port = ntohs(data->addr.sin_port);
David Howells9954bf92019-12-10 07:31:04 -05001078 if (sap->sa_family != AF_INET ||
1079 !nfs_verify_server_address(sap))
1080 goto out_no_address;
1081
1082 if (!(data->flags & NFS_MOUNT_TCP))
David Howells5eb005c2019-12-10 07:31:06 -05001083 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -05001084 /* N.B. caller will free nfs_server.hostname in all cases */
David Howells5eb005c2019-12-10 07:31:06 -05001085 ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1086 ctx->namlen = data->namlen;
1087 ctx->bsize = data->bsize;
David Howells9954bf92019-12-10 07:31:04 -05001088
1089 if (data->flags & NFS_MOUNT_SECFLAVOUR)
David Howells5eb005c2019-12-10 07:31:06 -05001090 ctx->selected_flavor = data->pseudoflavor;
David Howells9954bf92019-12-10 07:31:04 -05001091 else
David Howells5eb005c2019-12-10 07:31:06 -05001092 ctx->selected_flavor = RPC_AUTH_UNIX;
1093 if (!ctx->nfs_server.hostname)
David Howells9954bf92019-12-10 07:31:04 -05001094 goto out_nomem;
1095
1096 if (!(data->flags & NFS_MOUNT_NONLM))
David Howells5eb005c2019-12-10 07:31:06 -05001097 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
David Howells9954bf92019-12-10 07:31:04 -05001098 NFS_MOUNT_LOCAL_FCNTL);
1099 else
David Howells5eb005c2019-12-10 07:31:06 -05001100 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
David Howells9954bf92019-12-10 07:31:04 -05001101 NFS_MOUNT_LOCAL_FCNTL);
1102 /*
1103 * The legacy version 6 binary mount data from userspace has a
1104 * field used only to transport selinux information into the
1105 * the kernel. To continue to support that functionality we
1106 * have a touch of selinux knowledge here in the NFS code. The
1107 * userspace code converted context=blah to just blah so we are
1108 * converting back to the full string selinux understands.
1109 */
1110 if (data->context[0]){
1111#ifdef CONFIG_SECURITY_SELINUX
1112 int rc;
1113 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1114 rc = security_add_mnt_opt("context", data->context,
David Howells5eb005c2019-12-10 07:31:06 -05001115 strlen(data->context), ctx->lsm_opts);
David Howells9954bf92019-12-10 07:31:04 -05001116 if (rc)
1117 return rc;
1118#else
1119 return -EINVAL;
1120#endif
1121 }
1122
1123 break;
1124 default:
1125 return NFS_TEXT_DATA;
1126 }
1127
1128 return 0;
1129
1130out_no_data:
1131 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1132 return -EINVAL;
1133
1134out_no_v3:
1135 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1136 data->version);
1137 return -EINVAL;
1138
1139out_no_sec:
1140 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1141 return -EINVAL;
1142
1143out_nomem:
1144 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1145 return -ENOMEM;
1146
1147out_no_address:
1148 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1149 return -EINVAL;
1150
1151out_invalid_fh:
1152 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1153 return -EINVAL;
1154}
1155
1156#if IS_ENABLED(CONFIG_NFS_V4)
David Howells5eb005c2019-12-10 07:31:06 -05001157static void nfs4_validate_mount_flags(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -05001158{
David Howells5eb005c2019-12-10 07:31:06 -05001159 ctx->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
David Howells9954bf92019-12-10 07:31:04 -05001160 NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
1161}
1162
1163/*
1164 * Validate NFSv4 mount options
1165 */
1166static int nfs4_validate_mount_data(void *options,
David Howells5eb005c2019-12-10 07:31:06 -05001167 struct nfs_fs_context *ctx,
David Howells9954bf92019-12-10 07:31:04 -05001168 const char *dev_name)
1169{
David Howells5eb005c2019-12-10 07:31:06 -05001170 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001171 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
1172 char *c;
1173
1174 if (data == NULL)
1175 goto out_no_data;
1176
David Howells5eb005c2019-12-10 07:31:06 -05001177 ctx->version = 4;
David Howells9954bf92019-12-10 07:31:04 -05001178
1179 switch (data->version) {
1180 case 1:
David Howells5eb005c2019-12-10 07:31:06 -05001181 if (data->host_addrlen > sizeof(ctx->nfs_server.address))
David Howells9954bf92019-12-10 07:31:04 -05001182 goto out_no_address;
1183 if (data->host_addrlen == 0)
1184 goto out_no_address;
David Howells5eb005c2019-12-10 07:31:06 -05001185 ctx->nfs_server.addrlen = data->host_addrlen;
David Howells9954bf92019-12-10 07:31:04 -05001186 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1187 return -EFAULT;
1188 if (!nfs_verify_server_address(sap))
1189 goto out_no_address;
David Howells5eb005c2019-12-10 07:31:06 -05001190 ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
David Howells9954bf92019-12-10 07:31:04 -05001191
1192 if (data->auth_flavourlen) {
1193 rpc_authflavor_t pseudoflavor;
1194 if (data->auth_flavourlen > 1)
1195 goto out_inval_auth;
1196 if (copy_from_user(&pseudoflavor,
1197 data->auth_flavours,
1198 sizeof(pseudoflavor)))
1199 return -EFAULT;
David Howells5eb005c2019-12-10 07:31:06 -05001200 ctx->selected_flavor = pseudoflavor;
David Howells9954bf92019-12-10 07:31:04 -05001201 } else
David Howells5eb005c2019-12-10 07:31:06 -05001202 ctx->selected_flavor = RPC_AUTH_UNIX;
David Howells9954bf92019-12-10 07:31:04 -05001203
1204 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1205 if (IS_ERR(c))
1206 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001207 ctx->nfs_server.hostname = c;
David Howells9954bf92019-12-10 07:31:04 -05001208
1209 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1210 if (IS_ERR(c))
1211 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001212 ctx->nfs_server.export_path = c;
David Howells9954bf92019-12-10 07:31:04 -05001213 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1214
1215 c = strndup_user(data->client_addr.data, 16);
1216 if (IS_ERR(c))
1217 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001218 ctx->client_address = c;
David Howells9954bf92019-12-10 07:31:04 -05001219
1220 /*
David Howells5eb005c2019-12-10 07:31:06 -05001221 * Translate to nfs_fs_context, which nfs4_fill_super
David Howells9954bf92019-12-10 07:31:04 -05001222 * can deal with.
1223 */
1224
David Howells5eb005c2019-12-10 07:31:06 -05001225 ctx->flags = data->flags & NFS4_MOUNT_FLAGMASK;
1226 ctx->rsize = data->rsize;
1227 ctx->wsize = data->wsize;
1228 ctx->timeo = data->timeo;
1229 ctx->retrans = data->retrans;
1230 ctx->acregmin = data->acregmin;
1231 ctx->acregmax = data->acregmax;
1232 ctx->acdirmin = data->acdirmin;
1233 ctx->acdirmax = data->acdirmax;
1234 ctx->nfs_server.protocol = data->proto;
1235 nfs_validate_transport_protocol(ctx);
1236 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
David Howells9954bf92019-12-10 07:31:04 -05001237 goto out_invalid_transport_udp;
1238
1239 break;
1240 default:
1241 return NFS_TEXT_DATA;
1242 }
1243
1244 return 0;
1245
1246out_no_data:
1247 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
1248 return -EINVAL;
1249
1250out_inval_auth:
1251 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
1252 data->auth_flavourlen);
1253 return -EINVAL;
1254
1255out_no_address:
1256 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
1257 return -EINVAL;
1258
1259out_invalid_transport_udp:
1260 dfprintk(MOUNT, "NFSv4: Unsupported transport protocol udp\n");
1261 return -EINVAL;
1262}
1263
1264int nfs_validate_mount_data(struct file_system_type *fs_type,
1265 void *options,
David Howells5eb005c2019-12-10 07:31:06 -05001266 struct nfs_fs_context *ctx,
David Howells9954bf92019-12-10 07:31:04 -05001267 struct nfs_fh *mntfh,
1268 const char *dev_name)
1269{
1270 if (fs_type == &nfs_fs_type)
David Howells5eb005c2019-12-10 07:31:06 -05001271 return nfs23_validate_mount_data(options, ctx, mntfh, dev_name);
1272 return nfs4_validate_mount_data(options, ctx, dev_name);
David Howells9954bf92019-12-10 07:31:04 -05001273}
1274#else
1275int nfs_validate_mount_data(struct file_system_type *fs_type,
1276 void *options,
David Howells5eb005c2019-12-10 07:31:06 -05001277 struct nfs_fs_context *ctx,
David Howells9954bf92019-12-10 07:31:04 -05001278 struct nfs_fh *mntfh,
1279 const char *dev_name)
1280{
David Howells5eb005c2019-12-10 07:31:06 -05001281 return nfs23_validate_mount_data(options, ctx, mntfh, dev_name);
David Howells9954bf92019-12-10 07:31:04 -05001282}
1283#endif
1284
1285int nfs_validate_text_mount_data(void *options,
David Howells5eb005c2019-12-10 07:31:06 -05001286 struct nfs_fs_context *ctx,
David Howells9954bf92019-12-10 07:31:04 -05001287 const char *dev_name)
1288{
1289 int port = 0;
1290 int max_namelen = PAGE_SIZE;
1291 int max_pathlen = NFS_MAXPATHLEN;
David Howells5eb005c2019-12-10 07:31:06 -05001292 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001293
David Howells5eb005c2019-12-10 07:31:06 -05001294 if (nfs_parse_mount_options((char *)options, ctx) == 0)
David Howells9954bf92019-12-10 07:31:04 -05001295 return -EINVAL;
1296
1297 if (!nfs_verify_server_address(sap))
1298 goto out_no_address;
1299
David Howells5eb005c2019-12-10 07:31:06 -05001300 if (ctx->version == 4) {
David Howells9954bf92019-12-10 07:31:04 -05001301#if IS_ENABLED(CONFIG_NFS_V4)
David Howells5eb005c2019-12-10 07:31:06 -05001302 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
David Howells9954bf92019-12-10 07:31:04 -05001303 port = NFS_RDMA_PORT;
1304 else
1305 port = NFS_PORT;
1306 max_namelen = NFS4_MAXNAMLEN;
1307 max_pathlen = NFS4_MAXPATHLEN;
David Howells5eb005c2019-12-10 07:31:06 -05001308 nfs_validate_transport_protocol(ctx);
1309 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
David Howells9954bf92019-12-10 07:31:04 -05001310 goto out_invalid_transport_udp;
David Howells5eb005c2019-12-10 07:31:06 -05001311 nfs4_validate_mount_flags(ctx);
David Howells9954bf92019-12-10 07:31:04 -05001312#else
1313 goto out_v4_not_compiled;
1314#endif /* CONFIG_NFS_V4 */
1315 } else {
David Howells5eb005c2019-12-10 07:31:06 -05001316 nfs_set_mount_transport_protocol(ctx);
1317 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
David Howells9954bf92019-12-10 07:31:04 -05001318 port = NFS_RDMA_PORT;
1319 }
1320
David Howells5eb005c2019-12-10 07:31:06 -05001321 nfs_set_port(sap, &ctx->nfs_server.port, port);
David Howells9954bf92019-12-10 07:31:04 -05001322
David Howellse5581002019-12-10 07:31:10 -05001323 return nfs_parse_devname(ctx, dev_name, max_namelen, max_pathlen);
David Howells9954bf92019-12-10 07:31:04 -05001324
1325#if !IS_ENABLED(CONFIG_NFS_V4)
1326out_v4_not_compiled:
1327 dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
1328 return -EPROTONOSUPPORT;
1329#else
1330out_invalid_transport_udp:
1331 dfprintk(MOUNT, "NFSv4: Unsupported transport protocol udp\n");
1332 return -EINVAL;
1333#endif /* !CONFIG_NFS_V4 */
1334
1335out_no_address:
1336 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1337 return -EINVAL;
1338}