Deleted Added
full compact
vfs_export.c (191990) vfs_export.c (194498)
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>
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: head/sys/kern/vfs_export.c 191990 2009-05-11 15:33:26Z attilio $");
38__FBSDID("$FreeBSD: head/sys/kern/vfs_export.c 194498 2009-06-19 17:10:35Z brooks $");
39
40#include <sys/param.h>
41#include <sys/dirent.h>
42#include <sys/domain.h>
43#include <sys/kernel.h>
44#include <sys/lock.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/mount.h>
48#include <sys/mutex.h>
49#include <sys/rwlock.h>
50#include <sys/refcount.h>
51#include <sys/socket.h>
52#include <sys/systm.h>
53#include <sys/vnode.h>
54
55#include <net/radix.h>
56
57static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
58
59static void vfs_free_addrlist(struct netexport *nep);
60static int vfs_free_netcred(struct radix_node *rn, void *w);
61static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
62 struct export_args *argp);
63static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
64
65/*
66 * Network address lookup element
67 */
68struct netcred {
69 struct radix_node netc_rnodes[2];
70 int netc_exflags;
71 struct ucred *netc_anon;
72 int netc_numsecflavors;
73 int netc_secflavors[MAXSECFLAVORS];
74};
75
76/*
77 * Network export information
78 */
79struct netexport {
80 struct netcred ne_defexported; /* Default export */
81 struct radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
82};
83
84/*
85 * Build hash lists of net addresses and hang them off the mount point.
86 * Called by vfs_export() to set up the lists of export addresses.
87 */
88static int
89vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
90 struct export_args *argp)
91{
92 register struct netcred *np;
93 register struct radix_node_head *rnh;
94 register int i;
95 struct radix_node *rn;
96 struct sockaddr *saddr, *smask = 0;
97 struct domain *dom;
98 int error;
99
100 /*
101 * XXX: This routine converts from a `struct xucred'
102 * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This
103 * operation is questionable; for example, what should be done
104 * with fields like cr_uidinfo and cr_prison? Currently, this
105 * routine does not touch them (leaves them as NULL).
106 */
107 if (argp->ex_anon.cr_version != XUCRED_VERSION) {
108 vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
109 argp->ex_anon.cr_version, XUCRED_VERSION);
110 return (EINVAL);
111 }
112
113 if (argp->ex_addrlen == 0) {
114 if (mp->mnt_flag & MNT_DEFEXPORTED) {
115 vfs_mount_error(mp,
116 "MNT_DEFEXPORTED already set for mount %p", mp);
117 return (EPERM);
118 }
119 np = &nep->ne_defexported;
120 np->netc_exflags = argp->ex_flags;
121 np->netc_anon = crget();
122 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
39
40#include <sys/param.h>
41#include <sys/dirent.h>
42#include <sys/domain.h>
43#include <sys/kernel.h>
44#include <sys/lock.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/mount.h>
48#include <sys/mutex.h>
49#include <sys/rwlock.h>
50#include <sys/refcount.h>
51#include <sys/socket.h>
52#include <sys/systm.h>
53#include <sys/vnode.h>
54
55#include <net/radix.h>
56
57static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
58
59static void vfs_free_addrlist(struct netexport *nep);
60static int vfs_free_netcred(struct radix_node *rn, void *w);
61static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
62 struct export_args *argp);
63static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
64
65/*
66 * Network address lookup element
67 */
68struct netcred {
69 struct radix_node netc_rnodes[2];
70 int netc_exflags;
71 struct ucred *netc_anon;
72 int netc_numsecflavors;
73 int netc_secflavors[MAXSECFLAVORS];
74};
75
76/*
77 * Network export information
78 */
79struct netexport {
80 struct netcred ne_defexported; /* Default export */
81 struct radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
82};
83
84/*
85 * Build hash lists of net addresses and hang them off the mount point.
86 * Called by vfs_export() to set up the lists of export addresses.
87 */
88static int
89vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
90 struct export_args *argp)
91{
92 register struct netcred *np;
93 register struct radix_node_head *rnh;
94 register int i;
95 struct radix_node *rn;
96 struct sockaddr *saddr, *smask = 0;
97 struct domain *dom;
98 int error;
99
100 /*
101 * XXX: This routine converts from a `struct xucred'
102 * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This
103 * operation is questionable; for example, what should be done
104 * with fields like cr_uidinfo and cr_prison? Currently, this
105 * routine does not touch them (leaves them as NULL).
106 */
107 if (argp->ex_anon.cr_version != XUCRED_VERSION) {
108 vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
109 argp->ex_anon.cr_version, XUCRED_VERSION);
110 return (EINVAL);
111 }
112
113 if (argp->ex_addrlen == 0) {
114 if (mp->mnt_flag & MNT_DEFEXPORTED) {
115 vfs_mount_error(mp,
116 "MNT_DEFEXPORTED already set for mount %p", mp);
117 return (EPERM);
118 }
119 np = &nep->ne_defexported;
120 np->netc_exflags = argp->ex_flags;
121 np->netc_anon = crget();
122 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
123 np->netc_anon->cr_ngroups = argp->ex_anon.cr_ngroups;
124 bcopy(argp->ex_anon.cr_groups, np->netc_anon->cr_groups,
125 sizeof(np->netc_anon->cr_groups));
123 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
124 argp->ex_anon.cr_groups);
126 np->netc_numsecflavors = argp->ex_numsecflavors;
127 bcopy(argp->ex_secflavors, np->netc_secflavors,
128 sizeof(np->netc_secflavors));
129 MNT_ILOCK(mp);
130 mp->mnt_flag |= MNT_DEFEXPORTED;
131 MNT_IUNLOCK(mp);
132 return (0);
133 }
134
135#if MSIZE <= 256
136 if (argp->ex_addrlen > MLEN) {
137 vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
138 argp->ex_addrlen, MLEN);
139 return (EINVAL);
140 }
141#endif
142
143 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
144 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
145 saddr = (struct sockaddr *) (np + 1);
146 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
147 goto out;
148 if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
149 error = EINVAL;
150 vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
151 goto out;
152 }
153 if (saddr->sa_len > argp->ex_addrlen)
154 saddr->sa_len = argp->ex_addrlen;
155 if (argp->ex_masklen) {
156 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
157 error = copyin(argp->ex_mask, smask, argp->ex_masklen);
158 if (error)
159 goto out;
160 if (smask->sa_len > argp->ex_masklen)
161 smask->sa_len = argp->ex_masklen;
162 }
163 i = saddr->sa_family;
164 if ((rnh = nep->ne_rtable[i]) == NULL) {
165 /*
166 * Seems silly to initialize every AF when most are not used,
167 * do so on demand here
168 */
169 for (dom = domains; dom; dom = dom->dom_next) {
170 KASSERT(((i == AF_INET) || (i == AF_INET6)),
171 ("unexpected protocol in vfs_hang_addrlist"));
172 if (dom->dom_family == i && dom->dom_rtattach) {
173 /*
174 * XXX MRT
175 * The INET and INET6 domains know the
176 * offset already. We don't need to send it
177 * So we just use it as a flag to say that
178 * we are or are not setting up a real routing
179 * table. Only IP and IPV6 need have this
180 * be 0 so all other protocols can stay the
181 * same (ABI compatible).
182 */
183 dom->dom_rtattach(
184 (void **) &nep->ne_rtable[i], 0);
185 break;
186 }
187 }
188 if ((rnh = nep->ne_rtable[i]) == NULL) {
189 error = ENOBUFS;
190 vfs_mount_error(mp, "%s %s %d",
191 "Unable to initialize radix node head ",
192 "for address family", i);
193 goto out;
194 }
195 }
196 RADIX_NODE_HEAD_LOCK(rnh);
197 rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
198 RADIX_NODE_HEAD_UNLOCK(rnh);
199 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
200 error = EPERM;
201 vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
202 rn, np);
203 goto out;
204 }
205 np->netc_exflags = argp->ex_flags;
206 np->netc_anon = crget();
207 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
125 np->netc_numsecflavors = argp->ex_numsecflavors;
126 bcopy(argp->ex_secflavors, np->netc_secflavors,
127 sizeof(np->netc_secflavors));
128 MNT_ILOCK(mp);
129 mp->mnt_flag |= MNT_DEFEXPORTED;
130 MNT_IUNLOCK(mp);
131 return (0);
132 }
133
134#if MSIZE <= 256
135 if (argp->ex_addrlen > MLEN) {
136 vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
137 argp->ex_addrlen, MLEN);
138 return (EINVAL);
139 }
140#endif
141
142 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
143 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
144 saddr = (struct sockaddr *) (np + 1);
145 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
146 goto out;
147 if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
148 error = EINVAL;
149 vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
150 goto out;
151 }
152 if (saddr->sa_len > argp->ex_addrlen)
153 saddr->sa_len = argp->ex_addrlen;
154 if (argp->ex_masklen) {
155 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
156 error = copyin(argp->ex_mask, smask, argp->ex_masklen);
157 if (error)
158 goto out;
159 if (smask->sa_len > argp->ex_masklen)
160 smask->sa_len = argp->ex_masklen;
161 }
162 i = saddr->sa_family;
163 if ((rnh = nep->ne_rtable[i]) == NULL) {
164 /*
165 * Seems silly to initialize every AF when most are not used,
166 * do so on demand here
167 */
168 for (dom = domains; dom; dom = dom->dom_next) {
169 KASSERT(((i == AF_INET) || (i == AF_INET6)),
170 ("unexpected protocol in vfs_hang_addrlist"));
171 if (dom->dom_family == i && dom->dom_rtattach) {
172 /*
173 * XXX MRT
174 * The INET and INET6 domains know the
175 * offset already. We don't need to send it
176 * So we just use it as a flag to say that
177 * we are or are not setting up a real routing
178 * table. Only IP and IPV6 need have this
179 * be 0 so all other protocols can stay the
180 * same (ABI compatible).
181 */
182 dom->dom_rtattach(
183 (void **) &nep->ne_rtable[i], 0);
184 break;
185 }
186 }
187 if ((rnh = nep->ne_rtable[i]) == NULL) {
188 error = ENOBUFS;
189 vfs_mount_error(mp, "%s %s %d",
190 "Unable to initialize radix node head ",
191 "for address family", i);
192 goto out;
193 }
194 }
195 RADIX_NODE_HEAD_LOCK(rnh);
196 rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
197 RADIX_NODE_HEAD_UNLOCK(rnh);
198 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
199 error = EPERM;
200 vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
201 rn, np);
202 goto out;
203 }
204 np->netc_exflags = argp->ex_flags;
205 np->netc_anon = crget();
206 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
208 np->netc_anon->cr_ngroups = argp->ex_anon.cr_ngroups;
209 bcopy(argp->ex_anon.cr_groups, np->netc_anon->cr_groups,
210 sizeof(np->netc_anon->cr_groups));
207 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
208 np->netc_anon->cr_groups);
211 np->netc_numsecflavors = argp->ex_numsecflavors;
212 bcopy(argp->ex_secflavors, np->netc_secflavors,
213 sizeof(np->netc_secflavors));
214 return (0);
215out:
216 free(np, M_NETADDR);
217 return (error);
218}
219
220/* Helper for vfs_free_addrlist. */
221/* ARGSUSED */
222static int
223vfs_free_netcred(struct radix_node *rn, void *w)
224{
225 register struct radix_node_head *rnh = (struct radix_node_head *) w;
226
227 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
228 free(rn, M_NETADDR);
229 return (0);
230}
231
232/*
233 * Free the net address hash lists that are hanging off the mount points.
234 */
235static void
236vfs_free_addrlist(struct netexport *nep)
237{
238 register int i;
239 register struct radix_node_head *rnh;
240
241 for (i = 0; i <= AF_MAX; i++)
242 if ((rnh = nep->ne_rtable[i])) {
243 RADIX_NODE_HEAD_LOCK(rnh);
244 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
245 RADIX_NODE_HEAD_UNLOCK(rnh);
246 RADIX_NODE_HEAD_DESTROY(rnh);
247 free(rnh, M_RTABLE);
248 nep->ne_rtable[i] = NULL; /* not SMP safe XXX */
249 }
250}
251
252/*
253 * High level function to manipulate export options on a mount point
254 * and the passed in netexport.
255 * Struct export_args *argp is the variable used to twiddle options,
256 * the structure is described in sys/mount.h
257 */
258int
259vfs_export(struct mount *mp, struct export_args *argp)
260{
261 struct netexport *nep;
262 int error;
263
264 if (argp->ex_numsecflavors < 0
265 || argp->ex_numsecflavors >= MAXSECFLAVORS)
266 return (EINVAL);
267
268 error = 0;
269 lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
270 nep = mp->mnt_export;
271 if (argp->ex_flags & MNT_DELEXPORT) {
272 if (nep == NULL) {
273 error = ENOENT;
274 goto out;
275 }
276 if (mp->mnt_flag & MNT_EXPUBLIC) {
277 vfs_setpublicfs(NULL, NULL, NULL);
278 MNT_ILOCK(mp);
279 mp->mnt_flag &= ~MNT_EXPUBLIC;
280 MNT_IUNLOCK(mp);
281 }
282 vfs_free_addrlist(nep);
283 mp->mnt_export = NULL;
284 free(nep, M_MOUNT);
285 nep = NULL;
286 MNT_ILOCK(mp);
287 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
288 MNT_IUNLOCK(mp);
289 }
290 if (argp->ex_flags & MNT_EXPORTED) {
291 if (nep == NULL) {
292 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
293 mp->mnt_export = nep;
294 }
295 if (argp->ex_flags & MNT_EXPUBLIC) {
296 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
297 goto out;
298 MNT_ILOCK(mp);
299 mp->mnt_flag |= MNT_EXPUBLIC;
300 MNT_IUNLOCK(mp);
301 }
302 if ((error = vfs_hang_addrlist(mp, nep, argp)))
303 goto out;
304 MNT_ILOCK(mp);
305 mp->mnt_flag |= MNT_EXPORTED;
306 MNT_IUNLOCK(mp);
307 }
308
309out:
310 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
311 /*
312 * Once we have executed the vfs_export() command, we do
313 * not want to keep the "export" option around in the
314 * options list, since that will cause subsequent MNT_UPDATE
315 * calls to fail. The export information is saved in
316 * mp->mnt_export, so we can safely delete the "export" mount option
317 * here.
318 */
319 vfs_deleteopt(mp->mnt_optnew, "export");
320 vfs_deleteopt(mp->mnt_opt, "export");
321 return (error);
322}
323
324/*
325 * Set the publicly exported filesystem (WebNFS). Currently, only
326 * one public filesystem is possible in the spec (RFC 2054 and 2055)
327 */
328int
329vfs_setpublicfs(struct mount *mp, struct netexport *nep,
330 struct export_args *argp)
331{
332 int error;
333 struct vnode *rvp;
334 char *cp;
335
336 /*
337 * mp == NULL -> invalidate the current info, the FS is
338 * no longer exported. May be called from either vfs_export
339 * or unmount, so check if it hasn't already been done.
340 */
341 if (mp == NULL) {
342 if (nfs_pub.np_valid) {
343 nfs_pub.np_valid = 0;
344 if (nfs_pub.np_index != NULL) {
345 free(nfs_pub.np_index, M_TEMP);
346 nfs_pub.np_index = NULL;
347 }
348 }
349 return (0);
350 }
351
352 /*
353 * Only one allowed at a time.
354 */
355 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
356 return (EBUSY);
357
358 /*
359 * Get real filehandle for root of exported FS.
360 */
361 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
362 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
363
364 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
365 return (error);
366
367 if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
368 return (error);
369
370 vput(rvp);
371
372 /*
373 * If an indexfile was specified, pull it in.
374 */
375 if (argp->ex_indexfile != NULL) {
376 if (nfs_pub.np_index != NULL)
377 nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
378 M_WAITOK);
379 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
380 MAXNAMLEN, (size_t *)0);
381 if (!error) {
382 /*
383 * Check for illegal filenames.
384 */
385 for (cp = nfs_pub.np_index; *cp; cp++) {
386 if (*cp == '/') {
387 error = EINVAL;
388 break;
389 }
390 }
391 }
392 if (error) {
393 free(nfs_pub.np_index, M_TEMP);
394 nfs_pub.np_index = NULL;
395 return (error);
396 }
397 }
398
399 nfs_pub.np_mount = mp;
400 nfs_pub.np_valid = 1;
401 return (0);
402}
403
404/*
405 * Used by the filesystems to determine if a given network address
406 * (passed in 'nam') is present in their exports list, returns a pointer
407 * to struct netcred so that the filesystem can examine it for
408 * access rights (read/write/etc).
409 */
410static struct netcred *
411vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
412{
413 struct netexport *nep;
414 register struct netcred *np;
415 register struct radix_node_head *rnh;
416 struct sockaddr *saddr;
417
418 nep = mp->mnt_export;
419 if (nep == NULL)
420 return (NULL);
421 np = NULL;
422 if (mp->mnt_flag & MNT_EXPORTED) {
423 /*
424 * Lookup in the export list first.
425 */
426 if (nam != NULL) {
427 saddr = nam;
428 rnh = nep->ne_rtable[saddr->sa_family];
429 if (rnh != NULL) {
430 RADIX_NODE_HEAD_RLOCK(rnh);
431 np = (struct netcred *)
432 (*rnh->rnh_matchaddr)(saddr, rnh);
433 RADIX_NODE_HEAD_RUNLOCK(rnh);
434 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
435 np = NULL;
436 }
437 }
438 /*
439 * If no address match, use the default if it exists.
440 */
441 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
442 np = &nep->ne_defexported;
443 }
444 return (np);
445}
446
447/*
448 * XXX: This comment comes from the deprecated ufs_check_export()
449 * XXX: and may not entirely apply, but lacking something better:
450 * This is the generic part of fhtovp called after the underlying
451 * filesystem has validated the file handle.
452 *
453 * Verify that a host should have access to a filesystem.
454 */
455
456int
457vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
458 struct ucred **credanonp, int *numsecflavors, int **secflavors)
459{
460 struct netcred *np;
461
462 lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
463 np = vfs_export_lookup(mp, nam);
464 if (np == NULL) {
465 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
466 *credanonp = NULL;
467 return (EACCES);
468 }
469 *extflagsp = np->netc_exflags;
470 if ((*credanonp = np->netc_anon) != NULL)
471 crhold(*credanonp);
472 if (numsecflavors)
473 *numsecflavors = np->netc_numsecflavors;
474 if (secflavors)
475 *secflavors = np->netc_secflavors;
476 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
477 return (0);
478}
479
209 np->netc_numsecflavors = argp->ex_numsecflavors;
210 bcopy(argp->ex_secflavors, np->netc_secflavors,
211 sizeof(np->netc_secflavors));
212 return (0);
213out:
214 free(np, M_NETADDR);
215 return (error);
216}
217
218/* Helper for vfs_free_addrlist. */
219/* ARGSUSED */
220static int
221vfs_free_netcred(struct radix_node *rn, void *w)
222{
223 register struct radix_node_head *rnh = (struct radix_node_head *) w;
224
225 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
226 free(rn, M_NETADDR);
227 return (0);
228}
229
230/*
231 * Free the net address hash lists that are hanging off the mount points.
232 */
233static void
234vfs_free_addrlist(struct netexport *nep)
235{
236 register int i;
237 register struct radix_node_head *rnh;
238
239 for (i = 0; i <= AF_MAX; i++)
240 if ((rnh = nep->ne_rtable[i])) {
241 RADIX_NODE_HEAD_LOCK(rnh);
242 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
243 RADIX_NODE_HEAD_UNLOCK(rnh);
244 RADIX_NODE_HEAD_DESTROY(rnh);
245 free(rnh, M_RTABLE);
246 nep->ne_rtable[i] = NULL; /* not SMP safe XXX */
247 }
248}
249
250/*
251 * High level function to manipulate export options on a mount point
252 * and the passed in netexport.
253 * Struct export_args *argp is the variable used to twiddle options,
254 * the structure is described in sys/mount.h
255 */
256int
257vfs_export(struct mount *mp, struct export_args *argp)
258{
259 struct netexport *nep;
260 int error;
261
262 if (argp->ex_numsecflavors < 0
263 || argp->ex_numsecflavors >= MAXSECFLAVORS)
264 return (EINVAL);
265
266 error = 0;
267 lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
268 nep = mp->mnt_export;
269 if (argp->ex_flags & MNT_DELEXPORT) {
270 if (nep == NULL) {
271 error = ENOENT;
272 goto out;
273 }
274 if (mp->mnt_flag & MNT_EXPUBLIC) {
275 vfs_setpublicfs(NULL, NULL, NULL);
276 MNT_ILOCK(mp);
277 mp->mnt_flag &= ~MNT_EXPUBLIC;
278 MNT_IUNLOCK(mp);
279 }
280 vfs_free_addrlist(nep);
281 mp->mnt_export = NULL;
282 free(nep, M_MOUNT);
283 nep = NULL;
284 MNT_ILOCK(mp);
285 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
286 MNT_IUNLOCK(mp);
287 }
288 if (argp->ex_flags & MNT_EXPORTED) {
289 if (nep == NULL) {
290 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
291 mp->mnt_export = nep;
292 }
293 if (argp->ex_flags & MNT_EXPUBLIC) {
294 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
295 goto out;
296 MNT_ILOCK(mp);
297 mp->mnt_flag |= MNT_EXPUBLIC;
298 MNT_IUNLOCK(mp);
299 }
300 if ((error = vfs_hang_addrlist(mp, nep, argp)))
301 goto out;
302 MNT_ILOCK(mp);
303 mp->mnt_flag |= MNT_EXPORTED;
304 MNT_IUNLOCK(mp);
305 }
306
307out:
308 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
309 /*
310 * Once we have executed the vfs_export() command, we do
311 * not want to keep the "export" option around in the
312 * options list, since that will cause subsequent MNT_UPDATE
313 * calls to fail. The export information is saved in
314 * mp->mnt_export, so we can safely delete the "export" mount option
315 * here.
316 */
317 vfs_deleteopt(mp->mnt_optnew, "export");
318 vfs_deleteopt(mp->mnt_opt, "export");
319 return (error);
320}
321
322/*
323 * Set the publicly exported filesystem (WebNFS). Currently, only
324 * one public filesystem is possible in the spec (RFC 2054 and 2055)
325 */
326int
327vfs_setpublicfs(struct mount *mp, struct netexport *nep,
328 struct export_args *argp)
329{
330 int error;
331 struct vnode *rvp;
332 char *cp;
333
334 /*
335 * mp == NULL -> invalidate the current info, the FS is
336 * no longer exported. May be called from either vfs_export
337 * or unmount, so check if it hasn't already been done.
338 */
339 if (mp == NULL) {
340 if (nfs_pub.np_valid) {
341 nfs_pub.np_valid = 0;
342 if (nfs_pub.np_index != NULL) {
343 free(nfs_pub.np_index, M_TEMP);
344 nfs_pub.np_index = NULL;
345 }
346 }
347 return (0);
348 }
349
350 /*
351 * Only one allowed at a time.
352 */
353 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
354 return (EBUSY);
355
356 /*
357 * Get real filehandle for root of exported FS.
358 */
359 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
360 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
361
362 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
363 return (error);
364
365 if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
366 return (error);
367
368 vput(rvp);
369
370 /*
371 * If an indexfile was specified, pull it in.
372 */
373 if (argp->ex_indexfile != NULL) {
374 if (nfs_pub.np_index != NULL)
375 nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
376 M_WAITOK);
377 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
378 MAXNAMLEN, (size_t *)0);
379 if (!error) {
380 /*
381 * Check for illegal filenames.
382 */
383 for (cp = nfs_pub.np_index; *cp; cp++) {
384 if (*cp == '/') {
385 error = EINVAL;
386 break;
387 }
388 }
389 }
390 if (error) {
391 free(nfs_pub.np_index, M_TEMP);
392 nfs_pub.np_index = NULL;
393 return (error);
394 }
395 }
396
397 nfs_pub.np_mount = mp;
398 nfs_pub.np_valid = 1;
399 return (0);
400}
401
402/*
403 * Used by the filesystems to determine if a given network address
404 * (passed in 'nam') is present in their exports list, returns a pointer
405 * to struct netcred so that the filesystem can examine it for
406 * access rights (read/write/etc).
407 */
408static struct netcred *
409vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
410{
411 struct netexport *nep;
412 register struct netcred *np;
413 register struct radix_node_head *rnh;
414 struct sockaddr *saddr;
415
416 nep = mp->mnt_export;
417 if (nep == NULL)
418 return (NULL);
419 np = NULL;
420 if (mp->mnt_flag & MNT_EXPORTED) {
421 /*
422 * Lookup in the export list first.
423 */
424 if (nam != NULL) {
425 saddr = nam;
426 rnh = nep->ne_rtable[saddr->sa_family];
427 if (rnh != NULL) {
428 RADIX_NODE_HEAD_RLOCK(rnh);
429 np = (struct netcred *)
430 (*rnh->rnh_matchaddr)(saddr, rnh);
431 RADIX_NODE_HEAD_RUNLOCK(rnh);
432 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
433 np = NULL;
434 }
435 }
436 /*
437 * If no address match, use the default if it exists.
438 */
439 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
440 np = &nep->ne_defexported;
441 }
442 return (np);
443}
444
445/*
446 * XXX: This comment comes from the deprecated ufs_check_export()
447 * XXX: and may not entirely apply, but lacking something better:
448 * This is the generic part of fhtovp called after the underlying
449 * filesystem has validated the file handle.
450 *
451 * Verify that a host should have access to a filesystem.
452 */
453
454int
455vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
456 struct ucred **credanonp, int *numsecflavors, int **secflavors)
457{
458 struct netcred *np;
459
460 lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
461 np = vfs_export_lookup(mp, nam);
462 if (np == NULL) {
463 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
464 *credanonp = NULL;
465 return (EACCES);
466 }
467 *extflagsp = np->netc_exflags;
468 if ((*credanonp = np->netc_anon) != NULL)
469 crhold(*credanonp);
470 if (numsecflavors)
471 *numsecflavors = np->netc_numsecflavors;
472 if (secflavors)
473 *secflavors = np->netc_secflavors;
474 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
475 return (0);
476}
477