FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_export.c
1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/dirent.h>
46 #include <sys/jail.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/rwlock.h>
54 #include <sys/refcount.h>
55 #include <sys/signalvar.h>
56 #include <sys/socket.h>
57 #include <sys/vnode.h>
58
59 #include <netinet/in.h>
60 #include <net/radix.h>
61
62 #include <rpc/types.h>
63 #include <rpc/auth.h>
64
65 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
66
67 static struct radix_node_head *vfs_create_addrlist_af(
68 struct radix_node_head **prnh, int off);
69 static void vfs_free_addrlist(struct netexport *nep);
70 static int vfs_free_netcred(struct radix_node *rn, void *w);
71 static void vfs_free_addrlist_af(struct radix_node_head **prnh);
72 static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
73 struct export_args *argp);
74 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
75
76 /*
77 * Network address lookup element
78 */
79 struct netcred {
80 struct radix_node netc_rnodes[2];
81 int netc_exflags;
82 struct ucred *netc_anon;
83 int netc_numsecflavors;
84 int netc_secflavors[MAXSECFLAVORS];
85 };
86
87 /*
88 * Network export information
89 */
90 struct netexport {
91 struct netcred ne_defexported; /* Default export */
92 struct radix_node_head *ne4;
93 struct radix_node_head *ne6;
94 };
95
96 /*
97 * Build hash lists of net addresses and hang them off the mount point.
98 * Called by vfs_export() to set up the lists of export addresses.
99 */
100 static int
101 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
102 struct export_args *argp)
103 {
104 struct netcred *np;
105 struct radix_node_head *rnh;
106 int i;
107 struct radix_node *rn;
108 struct sockaddr *saddr, *smask = NULL;
109 #if defined(INET6) || defined(INET)
110 int off;
111 #endif
112 int error;
113
114 KASSERT(argp->ex_numsecflavors > 0,
115 ("%s: numsecflavors <= 0", __func__));
116 KASSERT(argp->ex_numsecflavors < MAXSECFLAVORS,
117 ("%s: numsecflavors >= MAXSECFLAVORS", __func__));
118
119 /*
120 * XXX: This routine converts from a `struct xucred'
121 * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This
122 * operation is questionable; for example, what should be done
123 * with fields like cr_uidinfo and cr_prison? Currently, this
124 * routine does not touch them (leaves them as NULL).
125 */
126 if (argp->ex_anon.cr_version != XUCRED_VERSION) {
127 vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
128 argp->ex_anon.cr_version, XUCRED_VERSION);
129 return (EINVAL);
130 }
131
132 if (argp->ex_addrlen == 0) {
133 if (mp->mnt_flag & MNT_DEFEXPORTED) {
134 vfs_mount_error(mp,
135 "MNT_DEFEXPORTED already set for mount %p", mp);
136 return (EPERM);
137 }
138 np = &nep->ne_defexported;
139 np->netc_exflags = argp->ex_flags;
140 np->netc_anon = crget();
141 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
142 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
143 argp->ex_anon.cr_groups);
144 np->netc_anon->cr_prison = &prison0;
145 prison_hold(np->netc_anon->cr_prison);
146 np->netc_numsecflavors = argp->ex_numsecflavors;
147 bcopy(argp->ex_secflavors, np->netc_secflavors,
148 sizeof(np->netc_secflavors));
149 MNT_ILOCK(mp);
150 mp->mnt_flag |= MNT_DEFEXPORTED;
151 MNT_IUNLOCK(mp);
152 return (0);
153 }
154
155 #if MSIZE <= 256
156 if (argp->ex_addrlen > MLEN) {
157 vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
158 argp->ex_addrlen, MLEN);
159 return (EINVAL);
160 }
161 #endif
162
163 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
164 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
165 saddr = (struct sockaddr *) (np + 1);
166 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
167 goto out;
168 if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
169 error = EINVAL;
170 vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
171 goto out;
172 }
173 if (saddr->sa_len > argp->ex_addrlen)
174 saddr->sa_len = argp->ex_addrlen;
175 if (argp->ex_masklen) {
176 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
177 error = copyin(argp->ex_mask, smask, argp->ex_masklen);
178 if (error)
179 goto out;
180 if (smask->sa_len > argp->ex_masklen)
181 smask->sa_len = argp->ex_masklen;
182 }
183 rnh = NULL;
184 switch (saddr->sa_family) {
185 #ifdef INET
186 case AF_INET:
187 if ((rnh = nep->ne4) == NULL) {
188 off = offsetof(struct sockaddr_in, sin_addr) << 3;
189 rnh = vfs_create_addrlist_af(&nep->ne4, off);
190 }
191 break;
192 #endif
193 #ifdef INET6
194 case AF_INET6:
195 if ((rnh = nep->ne6) == NULL) {
196 off = offsetof(struct sockaddr_in6, sin6_addr) << 3;
197 rnh = vfs_create_addrlist_af(&nep->ne6, off);
198 }
199 break;
200 #endif
201 }
202 if (rnh == NULL) {
203 error = ENOBUFS;
204 vfs_mount_error(mp, "%s %s %d",
205 "Unable to initialize radix node head ",
206 "for address family", saddr->sa_family);
207 goto out;
208 }
209 RADIX_NODE_HEAD_LOCK(rnh);
210 rn = (*rnh->rnh_addaddr)(saddr, smask, &rnh->rh, np->netc_rnodes);
211 RADIX_NODE_HEAD_UNLOCK(rnh);
212 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
213 error = EPERM;
214 vfs_mount_error(mp,
215 "netcred already exists for given addr/mask");
216 goto out;
217 }
218 np->netc_exflags = argp->ex_flags;
219 np->netc_anon = crget();
220 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
221 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
222 argp->ex_anon.cr_groups);
223 np->netc_anon->cr_prison = &prison0;
224 prison_hold(np->netc_anon->cr_prison);
225 np->netc_numsecflavors = argp->ex_numsecflavors;
226 bcopy(argp->ex_secflavors, np->netc_secflavors,
227 sizeof(np->netc_secflavors));
228 return (0);
229 out:
230 free(np, M_NETADDR);
231 return (error);
232 }
233
234 /* Helper for vfs_free_addrlist. */
235 /* ARGSUSED */
236 static int
237 vfs_free_netcred(struct radix_node *rn, void *w)
238 {
239 struct radix_node_head *rnh = (struct radix_node_head *) w;
240 struct ucred *cred;
241
242 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh);
243 cred = ((struct netcred *)rn)->netc_anon;
244 if (cred != NULL)
245 crfree(cred);
246 free(rn, M_NETADDR);
247 return (0);
248 }
249
250 static struct radix_node_head *
251 vfs_create_addrlist_af(struct radix_node_head **prnh, int off)
252 {
253
254 if (rn_inithead((void **)prnh, off) == 0)
255 return (NULL);
256 RADIX_NODE_HEAD_LOCK_INIT(*prnh);
257 return (*prnh);
258 }
259
260 static void
261 vfs_free_addrlist_af(struct radix_node_head **prnh)
262 {
263 struct radix_node_head *rnh;
264
265 rnh = *prnh;
266 RADIX_NODE_HEAD_LOCK(rnh);
267 (*rnh->rnh_walktree)(&rnh->rh, vfs_free_netcred, rnh);
268 RADIX_NODE_HEAD_UNLOCK(rnh);
269 RADIX_NODE_HEAD_DESTROY(rnh);
270 rn_detachhead((void **)prnh);
271 prnh = NULL;
272 }
273
274 /*
275 * Free the net address hash lists that are hanging off the mount points.
276 */
277 static void
278 vfs_free_addrlist(struct netexport *nep)
279 {
280 struct ucred *cred;
281
282 if (nep->ne4 != NULL)
283 vfs_free_addrlist_af(&nep->ne4);
284 if (nep->ne6 != NULL)
285 vfs_free_addrlist_af(&nep->ne6);
286
287 cred = nep->ne_defexported.netc_anon;
288 if (cred != NULL)
289 crfree(cred);
290
291 }
292
293 /*
294 * High level function to manipulate export options on a mount point
295 * and the passed in netexport.
296 * Struct export_args *argp is the variable used to twiddle options,
297 * the structure is described in sys/mount.h
298 */
299 int
300 vfs_export(struct mount *mp, struct export_args *argp)
301 {
302 struct netexport *nep;
303 int error;
304
305 if ((argp->ex_flags & (MNT_DELEXPORT | MNT_EXPORTED)) == 0)
306 return (EINVAL);
307
308 if ((argp->ex_flags & MNT_EXPORTED) != 0 &&
309 (argp->ex_numsecflavors < 0
310 || argp->ex_numsecflavors >= MAXSECFLAVORS))
311 return (EINVAL);
312
313 error = 0;
314 lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
315 nep = mp->mnt_export;
316 if (argp->ex_flags & MNT_DELEXPORT) {
317 if (nep == NULL) {
318 error = ENOENT;
319 goto out;
320 }
321 if (mp->mnt_flag & MNT_EXPUBLIC) {
322 vfs_setpublicfs(NULL, NULL, NULL);
323 MNT_ILOCK(mp);
324 mp->mnt_flag &= ~MNT_EXPUBLIC;
325 MNT_IUNLOCK(mp);
326 }
327 vfs_free_addrlist(nep);
328 mp->mnt_export = NULL;
329 free(nep, M_MOUNT);
330 nep = NULL;
331 MNT_ILOCK(mp);
332 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
333 MNT_IUNLOCK(mp);
334 }
335 if (argp->ex_flags & MNT_EXPORTED) {
336 if (nep == NULL) {
337 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
338 mp->mnt_export = nep;
339 }
340 if (argp->ex_flags & MNT_EXPUBLIC) {
341 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
342 goto out;
343 MNT_ILOCK(mp);
344 mp->mnt_flag |= MNT_EXPUBLIC;
345 MNT_IUNLOCK(mp);
346 }
347 if (argp->ex_numsecflavors == 0) {
348 argp->ex_numsecflavors = 1;
349 argp->ex_secflavors[0] = AUTH_SYS;
350 }
351 if ((error = vfs_hang_addrlist(mp, nep, argp)))
352 goto out;
353 MNT_ILOCK(mp);
354 mp->mnt_flag |= MNT_EXPORTED;
355 MNT_IUNLOCK(mp);
356 }
357
358 out:
359 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
360 /*
361 * Once we have executed the vfs_export() command, we do
362 * not want to keep the "export" option around in the
363 * options list, since that will cause subsequent MNT_UPDATE
364 * calls to fail. The export information is saved in
365 * mp->mnt_export, so we can safely delete the "export" mount option
366 * here.
367 */
368 vfs_deleteopt(mp->mnt_optnew, "export");
369 vfs_deleteopt(mp->mnt_opt, "export");
370 return (error);
371 }
372
373 /*
374 * Set the publicly exported filesystem (WebNFS). Currently, only
375 * one public filesystem is possible in the spec (RFC 2054 and 2055)
376 */
377 int
378 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
379 struct export_args *argp)
380 {
381 int error;
382 struct vnode *rvp;
383 char *cp;
384
385 /*
386 * mp == NULL -> invalidate the current info, the FS is
387 * no longer exported. May be called from either vfs_export
388 * or unmount, so check if it hasn't already been done.
389 */
390 if (mp == NULL) {
391 if (nfs_pub.np_valid) {
392 nfs_pub.np_valid = 0;
393 if (nfs_pub.np_index != NULL) {
394 free(nfs_pub.np_index, M_TEMP);
395 nfs_pub.np_index = NULL;
396 }
397 }
398 return (0);
399 }
400
401 /*
402 * Only one allowed at a time.
403 */
404 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
405 return (EBUSY);
406
407 /*
408 * Get real filehandle for root of exported FS.
409 */
410 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
411 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
412
413 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
414 return (error);
415
416 if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
417 return (error);
418
419 vput(rvp);
420
421 /*
422 * If an indexfile was specified, pull it in.
423 */
424 if (argp->ex_indexfile != NULL) {
425 if (nfs_pub.np_index != NULL)
426 nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
427 M_WAITOK);
428 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
429 MAXNAMLEN, (size_t *)0);
430 if (!error) {
431 /*
432 * Check for illegal filenames.
433 */
434 for (cp = nfs_pub.np_index; *cp; cp++) {
435 if (*cp == '/') {
436 error = EINVAL;
437 break;
438 }
439 }
440 }
441 if (error) {
442 free(nfs_pub.np_index, M_TEMP);
443 nfs_pub.np_index = NULL;
444 return (error);
445 }
446 }
447
448 nfs_pub.np_mount = mp;
449 nfs_pub.np_valid = 1;
450 return (0);
451 }
452
453 /*
454 * Used by the filesystems to determine if a given network address
455 * (passed in 'nam') is present in their exports list, returns a pointer
456 * to struct netcred so that the filesystem can examine it for
457 * access rights (read/write/etc).
458 */
459 static struct netcred *
460 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
461 {
462 struct netexport *nep;
463 struct netcred *np;
464 struct radix_node_head *rnh;
465 struct sockaddr *saddr;
466
467 nep = mp->mnt_export;
468 if (nep == NULL)
469 return (NULL);
470 np = NULL;
471 if (mp->mnt_flag & MNT_EXPORTED) {
472 /*
473 * Lookup in the export list first.
474 */
475 if (nam != NULL) {
476 saddr = nam;
477 rnh = NULL;
478 switch (saddr->sa_family) {
479 case AF_INET:
480 rnh = nep->ne4;
481 break;
482 case AF_INET6:
483 rnh = nep->ne6;
484 break;
485 }
486 if (rnh != NULL) {
487 RADIX_NODE_HEAD_RLOCK(rnh);
488 np = (struct netcred *)
489 (*rnh->rnh_matchaddr)(saddr, &rnh->rh);
490 RADIX_NODE_HEAD_RUNLOCK(rnh);
491 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
492 np = NULL;
493 }
494 }
495 /*
496 * If no address match, use the default if it exists.
497 */
498 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
499 np = &nep->ne_defexported;
500 }
501 return (np);
502 }
503
504 /*
505 * XXX: This comment comes from the deprecated ufs_check_export()
506 * XXX: and may not entirely apply, but lacking something better:
507 * This is the generic part of fhtovp called after the underlying
508 * filesystem has validated the file handle.
509 *
510 * Verify that a host should have access to a filesystem.
511 */
512
513 int
514 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
515 struct ucred **credanonp, int *numsecflavors, int **secflavors)
516 {
517 struct netcred *np;
518
519 lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
520 np = vfs_export_lookup(mp, nam);
521 if (np == NULL) {
522 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
523 *credanonp = NULL;
524 return (EACCES);
525 }
526 *extflagsp = np->netc_exflags;
527 if ((*credanonp = np->netc_anon) != NULL)
528 crhold(*credanonp);
529 if (numsecflavors) {
530 *numsecflavors = np->netc_numsecflavors;
531 KASSERT(*numsecflavors > 0,
532 ("%s: numsecflavors <= 0", __func__));
533 KASSERT(*numsecflavors < MAXSECFLAVORS,
534 ("%s: numsecflavors >= MAXSECFLAVORS", __func__));
535 }
536 if (secflavors)
537 *secflavors = np->netc_secflavors;
538 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
539 return (0);
540 }
541
Cache object: 5998bc3621a23052b3d7b69491f745ff
|