nfs_export.c revision 1.28 1 1.28 dyoung /* $NetBSD: nfs_export.c,v 1.28 2007/06/09 02:33:41 dyoung Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*-
4 1.1 jmmv * Copyright (c) 1997, 1998, 2004, 2005 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jmmv * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 jmmv * NASA Ames Research Center.
10 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
11 1.1 jmmv * by Charles M. Hannum.
12 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
13 1.1 jmmv * by Julio M. Merino Vidal.
14 1.1 jmmv *
15 1.1 jmmv * Redistribution and use in source and binary forms, with or without
16 1.1 jmmv * modification, are permitted provided that the following conditions
17 1.1 jmmv * are met:
18 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
19 1.1 jmmv * notice, this list of conditions and the following disclaimer.
20 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
21 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
22 1.1 jmmv * documentation and/or other materials provided with the distribution.
23 1.1 jmmv * 3. All advertising materials mentioning features or use of this software
24 1.1 jmmv * must display the following acknowledgement:
25 1.1 jmmv * This product includes software developed by the NetBSD
26 1.1 jmmv * Foundation, Inc. and its contributors.
27 1.1 jmmv * 4. Neither the name of The NetBSD Foundation nor the names of its
28 1.1 jmmv * contributors may be used to endorse or promote products derived
29 1.1 jmmv * from this software without specific prior written permission.
30 1.1 jmmv *
31 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
42 1.1 jmmv */
43 1.1 jmmv
44 1.1 jmmv /*
45 1.1 jmmv * Copyright (c) 1989, 1993
46 1.1 jmmv * The Regents of the University of California. All rights reserved.
47 1.1 jmmv * (c) UNIX System Laboratories, Inc.
48 1.1 jmmv * All or some portions of this file are derived from material licensed
49 1.1 jmmv * to the University of California by American Telephone and Telegraph
50 1.1 jmmv * Co. or Unix System Laboratories, Inc. and are reproduced herein with
51 1.1 jmmv * the permission of UNIX System Laboratories, Inc.
52 1.1 jmmv *
53 1.1 jmmv * Redistribution and use in source and binary forms, with or without
54 1.1 jmmv * modification, are permitted provided that the following conditions
55 1.1 jmmv * are met:
56 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
57 1.1 jmmv * notice, this list of conditions and the following disclaimer.
58 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
59 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
60 1.1 jmmv * documentation and/or other materials provided with the distribution.
61 1.1 jmmv * 3. Neither the name of the University nor the names of its contributors
62 1.1 jmmv * may be used to endorse or promote products derived from this software
63 1.1 jmmv * without specific prior written permission.
64 1.1 jmmv *
65 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 1.1 jmmv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 1.1 jmmv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 1.1 jmmv * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 1.1 jmmv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 1.1 jmmv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 1.1 jmmv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 1.1 jmmv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 1.1 jmmv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 1.1 jmmv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 1.1 jmmv * SUCH DAMAGE.
76 1.1 jmmv *
77 1.1 jmmv * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94
78 1.1 jmmv */
79 1.1 jmmv
80 1.1 jmmv /*
81 1.1 jmmv * VFS exports list management.
82 1.1 jmmv */
83 1.1 jmmv
84 1.1 jmmv #include <sys/cdefs.h>
85 1.28 dyoung __KERNEL_RCSID(0, "$NetBSD: nfs_export.c,v 1.28 2007/06/09 02:33:41 dyoung Exp $");
86 1.1 jmmv
87 1.3 jmmv #include "opt_compat_netbsd.h"
88 1.1 jmmv #include "opt_inet.h"
89 1.1 jmmv
90 1.1 jmmv #include <sys/param.h>
91 1.1 jmmv #include <sys/systm.h>
92 1.1 jmmv #include <sys/queue.h>
93 1.1 jmmv #include <sys/proc.h>
94 1.1 jmmv #include <sys/mount.h>
95 1.1 jmmv #include <sys/vnode.h>
96 1.1 jmmv #include <sys/namei.h>
97 1.1 jmmv #include <sys/errno.h>
98 1.1 jmmv #include <sys/malloc.h>
99 1.1 jmmv #include <sys/domain.h>
100 1.1 jmmv #include <sys/mbuf.h>
101 1.1 jmmv #include <sys/dirent.h>
102 1.1 jmmv #include <sys/socket.h> /* XXX for AF_MAX */
103 1.11 elad #include <sys/kauth.h>
104 1.1 jmmv
105 1.1 jmmv #include <net/radix.h>
106 1.1 jmmv
107 1.1 jmmv #include <netinet/in.h>
108 1.1 jmmv
109 1.1 jmmv #include <nfs/rpcv2.h>
110 1.1 jmmv #include <nfs/nfsproto.h>
111 1.1 jmmv #include <nfs/nfs.h>
112 1.1 jmmv #include <nfs/nfs_var.h>
113 1.1 jmmv
114 1.1 jmmv /*
115 1.1 jmmv * Network address lookup element.
116 1.1 jmmv */
117 1.1 jmmv struct netcred {
118 1.1 jmmv struct radix_node netc_rnodes[2];
119 1.1 jmmv int netc_refcnt;
120 1.1 jmmv int netc_exflags;
121 1.11 elad kauth_cred_t netc_anon;
122 1.1 jmmv };
123 1.1 jmmv
124 1.1 jmmv /*
125 1.1 jmmv * Network export information.
126 1.1 jmmv */
127 1.1 jmmv struct netexport {
128 1.5 yamt CIRCLEQ_ENTRY(netexport) ne_list;
129 1.5 yamt struct mount *ne_mount;
130 1.5 yamt struct netcred ne_defexported; /* Default export */
131 1.5 yamt struct radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
132 1.1 jmmv };
133 1.5 yamt CIRCLEQ_HEAD(, netexport) netexport_list =
134 1.5 yamt CIRCLEQ_HEAD_INITIALIZER(netexport_list);
135 1.1 jmmv
136 1.1 jmmv /* Malloc type used by the mount<->netexport map. */
137 1.1 jmmv MALLOC_DEFINE(M_NFS_EXPORT, "nfs_export", "NFS export data");
138 1.1 jmmv
139 1.1 jmmv /* Publicly exported file system. */
140 1.1 jmmv struct nfs_public nfs_pub;
141 1.1 jmmv
142 1.1 jmmv /*
143 1.1 jmmv * Local prototypes.
144 1.1 jmmv */
145 1.5 yamt static int init_exports(struct mount *, struct netexport **);
146 1.1 jmmv static int hang_addrlist(struct mount *, struct netexport *,
147 1.1 jmmv const struct export_args *);
148 1.1 jmmv static int sacheck(struct sockaddr *);
149 1.1 jmmv static int free_netcred(struct radix_node *, void *);
150 1.5 yamt static int export(struct netexport *, const struct export_args *);
151 1.1 jmmv static int setpublicfs(struct mount *, struct netexport *,
152 1.1 jmmv const struct export_args *);
153 1.5 yamt static struct netcred *netcred_lookup(struct netexport *, struct mbuf *);
154 1.5 yamt static struct netexport *netexport_lookup(const struct mount *);
155 1.5 yamt static struct netexport *netexport_lookup_byfsid(const fsid_t *);
156 1.12 yamt static void netexport_clear(struct netexport *);
157 1.5 yamt static void netexport_insert(struct netexport *);
158 1.5 yamt static void netexport_remove(struct netexport *);
159 1.8 yamt static void netexport_wrlock(void);
160 1.8 yamt static void netexport_wrunlock(void);
161 1.1 jmmv
162 1.1 jmmv /*
163 1.1 jmmv * PUBLIC INTERFACE
164 1.1 jmmv */
165 1.1 jmmv
166 1.1 jmmv /*
167 1.1 jmmv * Declare and initialize the file system export hooks.
168 1.1 jmmv */
169 1.1 jmmv static void nfs_export_unmount(struct mount *);
170 1.1 jmmv
171 1.1 jmmv struct vfs_hooks nfs_export_hooks = {
172 1.1 jmmv nfs_export_unmount
173 1.1 jmmv };
174 1.1 jmmv VFS_HOOKS_ATTACH(nfs_export_hooks);
175 1.1 jmmv
176 1.1 jmmv /*
177 1.1 jmmv * VFS unmount hook for NFS exports.
178 1.1 jmmv *
179 1.1 jmmv * Releases NFS exports list resources if the given mount point has some.
180 1.1 jmmv * As allocation happens lazily, it may be that it doesn't has this
181 1.1 jmmv * information, although it theorically should.
182 1.1 jmmv */
183 1.1 jmmv static void
184 1.1 jmmv nfs_export_unmount(struct mount *mp)
185 1.1 jmmv {
186 1.5 yamt struct netexport *ne;
187 1.1 jmmv
188 1.1 jmmv KASSERT(mp != NULL);
189 1.1 jmmv
190 1.8 yamt netexport_wrlock();
191 1.5 yamt ne = netexport_lookup(mp);
192 1.5 yamt if (ne == NULL) {
193 1.8 yamt netexport_wrunlock();
194 1.5 yamt return;
195 1.1 jmmv }
196 1.12 yamt netexport_clear(ne);
197 1.5 yamt netexport_remove(ne);
198 1.8 yamt netexport_wrunlock();
199 1.5 yamt free(ne, M_NFS_EXPORT);
200 1.1 jmmv }
201 1.1 jmmv
202 1.1 jmmv /*
203 1.1 jmmv * Atomically set the NFS exports list of the given file system, replacing
204 1.1 jmmv * it with a new list of entries.
205 1.1 jmmv *
206 1.1 jmmv * Returns zero on success or an appropriate error code otherwise.
207 1.1 jmmv *
208 1.1 jmmv * Helper function for the nfssvc(2) system call (NFSSVC_SETEXPORTSLIST
209 1.1 jmmv * command).
210 1.1 jmmv */
211 1.1 jmmv int
212 1.6 christos mountd_set_exports_list(const struct mountd_exports_list *mel, struct lwp *l)
213 1.1 jmmv {
214 1.1 jmmv int error;
215 1.1 jmmv #ifdef notyet
216 1.1 jmmv /* XXX: See below to see the reason why this is disabled. */
217 1.1 jmmv size_t i;
218 1.1 jmmv #endif
219 1.1 jmmv struct mount *mp;
220 1.5 yamt struct netexport *ne;
221 1.1 jmmv struct nameidata nd;
222 1.1 jmmv struct vnode *vp;
223 1.15 martin struct fid *fid;
224 1.15 martin size_t fid_size;
225 1.1 jmmv
226 1.16 ad if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
227 1.24 elad NULL) != 0)
228 1.1 jmmv return EPERM;
229 1.1 jmmv
230 1.1 jmmv /* Lookup the file system path. */
231 1.6 christos NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, mel->mel_path, l);
232 1.1 jmmv error = namei(&nd);
233 1.1 jmmv if (error != 0)
234 1.1 jmmv return error;
235 1.5 yamt vp = nd.ni_vp;
236 1.5 yamt mp = vp->v_mount;
237 1.1 jmmv
238 1.15 martin fid_size = 0;
239 1.15 martin if ((error = VFS_VPTOFH(vp, NULL, &fid_size)) == E2BIG) {
240 1.15 martin fid = malloc(fid_size, M_TEMP, M_NOWAIT);
241 1.15 martin if (fid != NULL) {
242 1.15 martin error = VFS_VPTOFH(vp, fid, &fid_size);
243 1.15 martin free(fid, M_TEMP);
244 1.15 martin }
245 1.15 martin }
246 1.15 martin if (error != 0) {
247 1.26 chs vput(vp);
248 1.25 chs return EOPNOTSUPP;
249 1.1 jmmv }
250 1.1 jmmv
251 1.1 jmmv /* Mark the file system busy. */
252 1.1 jmmv error = vfs_busy(mp, LK_NOWAIT, NULL);
253 1.25 chs vput(vp);
254 1.1 jmmv if (error != 0)
255 1.25 chs return error;
256 1.1 jmmv
257 1.8 yamt netexport_wrlock();
258 1.5 yamt ne = netexport_lookup(mp);
259 1.5 yamt if (ne == NULL) {
260 1.5 yamt error = init_exports(mp, &ne);
261 1.1 jmmv if (error != 0) {
262 1.25 chs goto out;
263 1.1 jmmv }
264 1.1 jmmv }
265 1.1 jmmv
266 1.5 yamt KASSERT(ne != NULL);
267 1.5 yamt KASSERT(ne->ne_mount == mp);
268 1.5 yamt
269 1.1 jmmv /*
270 1.1 jmmv * XXX: The part marked as 'notyet' works fine from the kernel's
271 1.1 jmmv * point of view, in the sense that it is able to atomically update
272 1.1 jmmv * the complete exports list for a file system. However, supporting
273 1.1 jmmv * this in mountd(8) requires a lot of work; so, for now, keep the
274 1.1 jmmv * old behavior of updating a single entry per call.
275 1.1 jmmv *
276 1.1 jmmv * When mountd(8) is fixed, just remove the second branch of this
277 1.1 jmmv * preprocessor conditional and enable the first one.
278 1.1 jmmv */
279 1.1 jmmv #ifdef notyet
280 1.12 yamt netexport_clear(ne);
281 1.1 jmmv for (i = 0; error == 0 && i < mel->mel_nexports; i++)
282 1.5 yamt error = export(ne, &mel->mel_exports[i]);
283 1.1 jmmv #else
284 1.1 jmmv if (mel->mel_nexports == 0)
285 1.12 yamt netexport_clear(ne);
286 1.1 jmmv else if (mel->mel_nexports == 1)
287 1.5 yamt error = export(ne, &mel->mel_exports[0]);
288 1.1 jmmv else {
289 1.1 jmmv printf("mountd_set_exports_list: Cannot set more than one "
290 1.1 jmmv "entry at once (unimplemented)\n");
291 1.1 jmmv error = EOPNOTSUPP;
292 1.1 jmmv }
293 1.1 jmmv #endif
294 1.1 jmmv
295 1.25 chs out:
296 1.8 yamt netexport_wrunlock();
297 1.1 jmmv vfs_unbusy(mp);
298 1.4 yamt return error;
299 1.1 jmmv }
300 1.1 jmmv
301 1.5 yamt static void
302 1.5 yamt netexport_insert(struct netexport *ne)
303 1.5 yamt {
304 1.5 yamt
305 1.5 yamt CIRCLEQ_INSERT_HEAD(&netexport_list, ne, ne_list);
306 1.5 yamt }
307 1.5 yamt
308 1.5 yamt static void
309 1.5 yamt netexport_remove(struct netexport *ne)
310 1.5 yamt {
311 1.5 yamt
312 1.5 yamt CIRCLEQ_REMOVE(&netexport_list, ne, ne_list);
313 1.5 yamt }
314 1.5 yamt
315 1.5 yamt static struct netexport *
316 1.5 yamt netexport_lookup(const struct mount *mp)
317 1.5 yamt {
318 1.5 yamt struct netexport *ne;
319 1.5 yamt
320 1.5 yamt CIRCLEQ_FOREACH(ne, &netexport_list, ne_list) {
321 1.5 yamt if (ne->ne_mount == mp) {
322 1.5 yamt goto done;
323 1.5 yamt }
324 1.5 yamt }
325 1.5 yamt ne = NULL;
326 1.5 yamt done:
327 1.5 yamt return ne;
328 1.5 yamt }
329 1.5 yamt
330 1.5 yamt static struct netexport *
331 1.5 yamt netexport_lookup_byfsid(const fsid_t *fsid)
332 1.5 yamt {
333 1.5 yamt struct netexport *ne;
334 1.5 yamt
335 1.5 yamt CIRCLEQ_FOREACH(ne, &netexport_list, ne_list) {
336 1.5 yamt const struct mount *mp = ne->ne_mount;
337 1.5 yamt
338 1.5 yamt if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
339 1.5 yamt mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
340 1.5 yamt goto done;
341 1.5 yamt }
342 1.5 yamt }
343 1.5 yamt ne = NULL;
344 1.5 yamt done:
345 1.5 yamt
346 1.5 yamt return ne;
347 1.5 yamt }
348 1.5 yamt
349 1.1 jmmv /*
350 1.1 jmmv * Check if the file system specified by the 'mp' mount structure is
351 1.1 jmmv * exported to a client with 'anon' anonymous credentials. The 'mb'
352 1.1 jmmv * argument is an mbuf containing the network address of the client.
353 1.1 jmmv * The return parameters for the export flags for the client are returned
354 1.1 jmmv * in the address specified by 'wh'.
355 1.1 jmmv *
356 1.1 jmmv * This function is used exclusively by the NFS server. It is generally
357 1.1 jmmv * invoked before VFS_FHTOVP to validate that client has access to the
358 1.1 jmmv * file system.
359 1.1 jmmv */
360 1.5 yamt
361 1.1 jmmv int
362 1.5 yamt netexport_check(const fsid_t *fsid, struct mbuf *mb, struct mount **mpp,
363 1.11 elad int *wh, kauth_cred_t *anon)
364 1.1 jmmv {
365 1.5 yamt struct netexport *ne;
366 1.1 jmmv struct netcred *np;
367 1.1 jmmv
368 1.5 yamt ne = netexport_lookup_byfsid(fsid);
369 1.5 yamt if (ne == NULL) {
370 1.5 yamt return EACCES;
371 1.1 jmmv }
372 1.5 yamt np = netcred_lookup(ne, mb);
373 1.5 yamt if (np == NULL) {
374 1.1 jmmv return EACCES;
375 1.5 yamt }
376 1.1 jmmv
377 1.5 yamt *mpp = ne->ne_mount;
378 1.5 yamt *wh = np->netc_exflags;
379 1.11 elad *anon = np->netc_anon;
380 1.1 jmmv
381 1.5 yamt return 0;
382 1.1 jmmv }
383 1.1 jmmv
384 1.3 jmmv #ifdef COMPAT_30
385 1.3 jmmv /*
386 1.3 jmmv * Handles legacy export requests. In this case, the export information
387 1.3 jmmv * is hardcoded in a specific place of the mount arguments structure (given
388 1.3 jmmv * in data); the request for an update is given through the fspec field
389 1.3 jmmv * (also in a known location), which must be a null pointer.
390 1.3 jmmv *
391 1.3 jmmv * Returns EJUSTRETURN if the given command was not a export request.
392 1.3 jmmv * Otherwise, returns 0 on success or an appropriate error code otherwise.
393 1.3 jmmv */
394 1.3 jmmv int
395 1.22 yamt nfs_update_exports_30(struct mount *mp, const char *path, void *data,
396 1.6 christos struct lwp *l)
397 1.3 jmmv {
398 1.3 jmmv int error;
399 1.3 jmmv struct {
400 1.3 jmmv const char *fspec;
401 1.3 jmmv struct export_args30 eargs;
402 1.3 jmmv } args;
403 1.3 jmmv struct mountd_exports_list mel;
404 1.3 jmmv
405 1.3 jmmv mel.mel_path = path;
406 1.3 jmmv
407 1.3 jmmv error = copyin(data, &args, sizeof(args));
408 1.3 jmmv if (error != 0)
409 1.3 jmmv return EJUSTRETURN;
410 1.3 jmmv
411 1.3 jmmv if (args.fspec != NULL)
412 1.3 jmmv return EJUSTRETURN;
413 1.3 jmmv
414 1.4 yamt if (args.eargs.ex_flags & 0x00020000) {
415 1.3 jmmv /* Request to delete exports. The mask above holds the
416 1.3 jmmv * value that used to be in MNT_DELEXPORT. */
417 1.3 jmmv mel.mel_nexports = 0;
418 1.3 jmmv } else {
419 1.3 jmmv struct export_args eargs;
420 1.3 jmmv
421 1.3 jmmv /* The following assumes export_args has not changed since
422 1.3 jmmv * export_args30. */
423 1.3 jmmv memcpy(&eargs, &args.eargs, sizeof(struct export_args));
424 1.3 jmmv
425 1.3 jmmv mel.mel_nexports = 1;
426 1.3 jmmv mel.mel_exports = &eargs;
427 1.3 jmmv }
428 1.3 jmmv
429 1.6 christos return mountd_set_exports_list(&mel, l);
430 1.3 jmmv }
431 1.3 jmmv #endif
432 1.3 jmmv
433 1.1 jmmv /*
434 1.1 jmmv * INTERNAL FUNCTIONS
435 1.1 jmmv */
436 1.1 jmmv
437 1.1 jmmv /*
438 1.1 jmmv * Initializes NFS exports for the file system given in 'mp' if it supports
439 1.1 jmmv * file handles; this is determined by checking whether mp's vfs_vptofh and
440 1.1 jmmv * vfs_fhtovp operations are NULL or not.
441 1.1 jmmv *
442 1.1 jmmv * If successful, returns 0 and sets *mnpp to the address of the new
443 1.20 jmmv * mount_netexport_pair item; otherwise returns an appropriate error code
444 1.1 jmmv * and *mnpp remains unmodified.
445 1.1 jmmv */
446 1.1 jmmv static int
447 1.5 yamt init_exports(struct mount *mp, struct netexport **nep)
448 1.1 jmmv {
449 1.1 jmmv int error;
450 1.1 jmmv struct export_args ea;
451 1.5 yamt struct netexport *ne;
452 1.1 jmmv
453 1.1 jmmv KASSERT(mp != NULL);
454 1.1 jmmv
455 1.1 jmmv /* Ensure that we do not already have this mount point. */
456 1.5 yamt KASSERT(netexport_lookup(mp) == NULL);
457 1.1 jmmv
458 1.5 yamt ne = malloc(sizeof(*ne), M_NFS_EXPORT, M_WAITOK | M_ZERO);
459 1.5 yamt ne->ne_mount = mp;
460 1.1 jmmv
461 1.1 jmmv /* Set the default export entry. Handled internally by export upon
462 1.1 jmmv * first call. */
463 1.1 jmmv memset(&ea, 0, sizeof(ea));
464 1.1 jmmv ea.ex_root = -2;
465 1.1 jmmv if (mp->mnt_flag & MNT_RDONLY)
466 1.1 jmmv ea.ex_flags |= MNT_EXRDONLY;
467 1.5 yamt error = export(ne, &ea);
468 1.5 yamt if (error != 0) {
469 1.5 yamt free(ne, M_NFS_EXPORT);
470 1.5 yamt } else {
471 1.5 yamt netexport_insert(ne);
472 1.5 yamt *nep = ne;
473 1.1 jmmv }
474 1.1 jmmv
475 1.1 jmmv return error;
476 1.1 jmmv }
477 1.1 jmmv
478 1.1 jmmv /*
479 1.1 jmmv * Build hash lists of net addresses and hang them off the mount point.
480 1.1 jmmv * Called by export() to set up a new entry in the lists of export
481 1.1 jmmv * addresses.
482 1.1 jmmv */
483 1.1 jmmv static int
484 1.1 jmmv hang_addrlist(struct mount *mp, struct netexport *nep,
485 1.1 jmmv const struct export_args *argp)
486 1.1 jmmv {
487 1.1 jmmv int error, i;
488 1.1 jmmv struct netcred *np, *enp;
489 1.1 jmmv struct radix_node_head *rnh;
490 1.1 jmmv struct sockaddr *saddr, *smask;
491 1.1 jmmv struct domain *dom;
492 1.1 jmmv
493 1.1 jmmv smask = NULL;
494 1.1 jmmv
495 1.1 jmmv if (argp->ex_addrlen == 0) {
496 1.1 jmmv if (mp->mnt_flag & MNT_DEFEXPORTED)
497 1.1 jmmv return EPERM;
498 1.1 jmmv np = &nep->ne_defexported;
499 1.13 yamt KASSERT(np->netc_anon == NULL);
500 1.13 yamt np->netc_anon = kauth_cred_alloc();
501 1.1 jmmv np->netc_exflags = argp->ex_flags;
502 1.18 pooka kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
503 1.1 jmmv mp->mnt_flag |= MNT_DEFEXPORTED;
504 1.1 jmmv return 0;
505 1.1 jmmv }
506 1.1 jmmv
507 1.1 jmmv if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN)
508 1.1 jmmv return EINVAL;
509 1.1 jmmv
510 1.1 jmmv i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
511 1.5 yamt np = malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
512 1.13 yamt np->netc_anon = kauth_cred_alloc();
513 1.1 jmmv saddr = (struct sockaddr *)(np + 1);
514 1.5 yamt error = copyin(argp->ex_addr, saddr, argp->ex_addrlen);
515 1.1 jmmv if (error)
516 1.1 jmmv goto out;
517 1.1 jmmv if (saddr->sa_len > argp->ex_addrlen)
518 1.1 jmmv saddr->sa_len = argp->ex_addrlen;
519 1.1 jmmv if (sacheck(saddr) == -1)
520 1.1 jmmv return EINVAL;
521 1.1 jmmv if (argp->ex_masklen) {
522 1.27 christos smask = (struct sockaddr *)((char *)saddr + argp->ex_addrlen);
523 1.5 yamt error = copyin(argp->ex_mask, smask, argp->ex_masklen);
524 1.1 jmmv if (error)
525 1.1 jmmv goto out;
526 1.1 jmmv if (smask->sa_len > argp->ex_masklen)
527 1.1 jmmv smask->sa_len = argp->ex_masklen;
528 1.1 jmmv if (smask->sa_family != saddr->sa_family)
529 1.1 jmmv return EINVAL;
530 1.1 jmmv if (sacheck(smask) == -1)
531 1.1 jmmv return EINVAL;
532 1.1 jmmv }
533 1.1 jmmv i = saddr->sa_family;
534 1.1 jmmv if ((rnh = nep->ne_rtable[i]) == 0) {
535 1.1 jmmv /*
536 1.1 jmmv * Seems silly to initialize every AF when most are not
537 1.1 jmmv * used, do so on demand here
538 1.1 jmmv */
539 1.1 jmmv DOMAIN_FOREACH(dom) {
540 1.1 jmmv if (dom->dom_family == i && dom->dom_rtattach) {
541 1.1 jmmv dom->dom_rtattach((void **)&nep->ne_rtable[i],
542 1.1 jmmv dom->dom_rtoffset);
543 1.1 jmmv break;
544 1.1 jmmv }
545 1.1 jmmv }
546 1.1 jmmv if ((rnh = nep->ne_rtable[i]) == 0) {
547 1.1 jmmv error = ENOBUFS;
548 1.1 jmmv goto out;
549 1.1 jmmv }
550 1.1 jmmv }
551 1.1 jmmv
552 1.1 jmmv enp = (struct netcred *)(*rnh->rnh_addaddr)(saddr, smask, rnh,
553 1.1 jmmv np->netc_rnodes);
554 1.1 jmmv if (enp != np) {
555 1.1 jmmv if (enp == NULL) {
556 1.1 jmmv enp = (struct netcred *)(*rnh->rnh_lookup)(saddr,
557 1.1 jmmv smask, rnh);
558 1.1 jmmv if (enp == NULL) {
559 1.1 jmmv error = EPERM;
560 1.1 jmmv goto out;
561 1.1 jmmv }
562 1.1 jmmv } else
563 1.1 jmmv enp->netc_refcnt++;
564 1.1 jmmv
565 1.1 jmmv goto check;
566 1.1 jmmv } else
567 1.1 jmmv enp->netc_refcnt = 1;
568 1.1 jmmv
569 1.1 jmmv np->netc_exflags = argp->ex_flags;
570 1.18 pooka kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
571 1.1 jmmv return 0;
572 1.1 jmmv check:
573 1.1 jmmv if (enp->netc_exflags != argp->ex_flags ||
574 1.11 elad kauth_cred_uucmp(enp->netc_anon, &argp->ex_anon) != 0)
575 1.1 jmmv error = EPERM;
576 1.1 jmmv else
577 1.1 jmmv error = 0;
578 1.1 jmmv out:
579 1.13 yamt KASSERT(np->netc_anon != NULL);
580 1.13 yamt kauth_cred_free(np->netc_anon);
581 1.1 jmmv free(np, M_NETADDR);
582 1.1 jmmv return error;
583 1.1 jmmv }
584 1.1 jmmv
585 1.1 jmmv /*
586 1.1 jmmv * Ensure that the address stored in 'sa' is valid.
587 1.1 jmmv * Returns zero on success, otherwise -1.
588 1.1 jmmv */
589 1.1 jmmv static int
590 1.1 jmmv sacheck(struct sockaddr *sa)
591 1.1 jmmv {
592 1.1 jmmv
593 1.1 jmmv switch (sa->sa_family) {
594 1.1 jmmv #ifdef INET
595 1.1 jmmv case AF_INET: {
596 1.1 jmmv struct sockaddr_in *sin = (struct sockaddr_in *)sa;
597 1.1 jmmv char *p = (char *)sin->sin_zero;
598 1.1 jmmv size_t i;
599 1.1 jmmv
600 1.1 jmmv if (sin->sin_len != sizeof(*sin))
601 1.1 jmmv return -1;
602 1.1 jmmv if (sin->sin_port != 0)
603 1.1 jmmv return -1;
604 1.1 jmmv for (i = 0; i < sizeof(sin->sin_zero); i++)
605 1.1 jmmv if (*p++ != '\0')
606 1.1 jmmv return -1;
607 1.1 jmmv return 0;
608 1.1 jmmv }
609 1.1 jmmv #endif
610 1.1 jmmv #ifdef INET6
611 1.1 jmmv case AF_INET6: {
612 1.1 jmmv struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
613 1.1 jmmv
614 1.1 jmmv if (sin6->sin6_len != sizeof(*sin6))
615 1.1 jmmv return -1;
616 1.1 jmmv if (sin6->sin6_port != 0)
617 1.1 jmmv return -1;
618 1.1 jmmv return 0;
619 1.1 jmmv }
620 1.1 jmmv #endif
621 1.1 jmmv default:
622 1.1 jmmv return -1;
623 1.1 jmmv }
624 1.1 jmmv }
625 1.1 jmmv
626 1.1 jmmv /*
627 1.1 jmmv * Free the netcred object pointed to by the 'rn' radix node.
628 1.1 jmmv * 'w' holds a pointer to the radix tree head.
629 1.1 jmmv */
630 1.1 jmmv static int
631 1.1 jmmv free_netcred(struct radix_node *rn, void *w)
632 1.1 jmmv {
633 1.1 jmmv struct radix_node_head *rnh = (struct radix_node_head *)w;
634 1.1 jmmv struct netcred *np = (struct netcred *)(void *)rn;
635 1.1 jmmv
636 1.1 jmmv (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
637 1.13 yamt if (--(np->netc_refcnt) <= 0) {
638 1.13 yamt KASSERT(np->netc_anon != NULL);
639 1.13 yamt kauth_cred_free(np->netc_anon);
640 1.1 jmmv free(np, M_NETADDR);
641 1.13 yamt }
642 1.1 jmmv return 0;
643 1.1 jmmv }
644 1.1 jmmv
645 1.1 jmmv /*
646 1.1 jmmv * Clears the exports list for a given file system.
647 1.1 jmmv */
648 1.1 jmmv static void
649 1.12 yamt netexport_clear(struct netexport *ne)
650 1.1 jmmv {
651 1.5 yamt struct radix_node_head *rnh;
652 1.5 yamt struct mount *mp = ne->ne_mount;
653 1.1 jmmv int i;
654 1.1 jmmv
655 1.1 jmmv if (mp->mnt_flag & MNT_EXPUBLIC) {
656 1.1 jmmv setpublicfs(NULL, NULL, NULL);
657 1.1 jmmv mp->mnt_flag &= ~MNT_EXPUBLIC;
658 1.1 jmmv }
659 1.1 jmmv
660 1.1 jmmv for (i = 0; i <= AF_MAX; i++) {
661 1.5 yamt if ((rnh = ne->ne_rtable[i]) != NULL) {
662 1.28 dyoung rn_walktree(rnh, free_netcred, rnh);
663 1.5 yamt free(rnh, M_RTABLE);
664 1.5 yamt ne->ne_rtable[i] = NULL;
665 1.1 jmmv }
666 1.1 jmmv }
667 1.1 jmmv
668 1.13 yamt if ((mp->mnt_flag & MNT_DEFEXPORTED) != 0) {
669 1.13 yamt struct netcred *np = &ne->ne_defexported;
670 1.13 yamt
671 1.13 yamt KASSERT(np->netc_anon != NULL);
672 1.13 yamt kauth_cred_free(np->netc_anon);
673 1.13 yamt np->netc_anon = NULL;
674 1.13 yamt } else {
675 1.13 yamt KASSERT(ne->ne_defexported.netc_anon == NULL);
676 1.13 yamt }
677 1.13 yamt
678 1.1 jmmv mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
679 1.1 jmmv }
680 1.1 jmmv
681 1.1 jmmv /*
682 1.1 jmmv * Add a new export entry (described by an export_args structure) to the
683 1.1 jmmv * given file system.
684 1.1 jmmv */
685 1.1 jmmv static int
686 1.5 yamt export(struct netexport *nep, const struct export_args *argp)
687 1.1 jmmv {
688 1.5 yamt struct mount *mp = nep->ne_mount;
689 1.1 jmmv int error;
690 1.1 jmmv
691 1.1 jmmv if (argp->ex_flags & MNT_EXPORTED) {
692 1.1 jmmv if (argp->ex_flags & MNT_EXPUBLIC) {
693 1.1 jmmv if ((error = setpublicfs(mp, nep, argp)) != 0)
694 1.1 jmmv return error;
695 1.1 jmmv mp->mnt_flag |= MNT_EXPUBLIC;
696 1.1 jmmv }
697 1.1 jmmv if ((error = hang_addrlist(mp, nep, argp)) != 0)
698 1.1 jmmv return error;
699 1.1 jmmv mp->mnt_flag |= MNT_EXPORTED;
700 1.1 jmmv }
701 1.1 jmmv return 0;
702 1.1 jmmv }
703 1.1 jmmv
704 1.1 jmmv /*
705 1.1 jmmv * Set the publicly exported filesystem (WebNFS). Currently, only
706 1.1 jmmv * one public filesystem is possible in the spec (RFC 2054 and 2055)
707 1.1 jmmv */
708 1.1 jmmv static int
709 1.22 yamt setpublicfs(struct mount *mp, struct netexport *nep,
710 1.1 jmmv const struct export_args *argp)
711 1.1 jmmv {
712 1.1 jmmv char *cp;
713 1.1 jmmv int error;
714 1.1 jmmv struct vnode *rvp;
715 1.15 martin size_t fhsize;
716 1.1 jmmv
717 1.1 jmmv /*
718 1.1 jmmv * mp == NULL -> invalidate the current info, the FS is
719 1.1 jmmv * no longer exported. May be called from either export
720 1.1 jmmv * or unmount, so check if it hasn't already been done.
721 1.1 jmmv */
722 1.1 jmmv if (mp == NULL) {
723 1.1 jmmv if (nfs_pub.np_valid) {
724 1.1 jmmv nfs_pub.np_valid = 0;
725 1.15 martin if (nfs_pub.np_handle != NULL) {
726 1.15 martin free(nfs_pub.np_handle, M_TEMP);
727 1.15 martin nfs_pub.np_handle = NULL;
728 1.15 martin }
729 1.1 jmmv if (nfs_pub.np_index != NULL) {
730 1.1 jmmv FREE(nfs_pub.np_index, M_TEMP);
731 1.1 jmmv nfs_pub.np_index = NULL;
732 1.1 jmmv }
733 1.1 jmmv }
734 1.1 jmmv return 0;
735 1.1 jmmv }
736 1.1 jmmv
737 1.1 jmmv /*
738 1.1 jmmv * Only one allowed at a time.
739 1.1 jmmv */
740 1.1 jmmv if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
741 1.1 jmmv return EBUSY;
742 1.1 jmmv
743 1.1 jmmv /*
744 1.1 jmmv * Get real filehandle for root of exported FS.
745 1.1 jmmv */
746 1.1 jmmv if ((error = VFS_ROOT(mp, &rvp)))
747 1.1 jmmv return error;
748 1.1 jmmv
749 1.15 martin fhsize = 0;
750 1.15 martin error = vfs_composefh(rvp, NULL, &fhsize);
751 1.15 martin if (error != E2BIG)
752 1.15 martin return error;
753 1.15 martin nfs_pub.np_handle = malloc(fhsize, M_TEMP, M_NOWAIT);
754 1.15 martin if (nfs_pub.np_handle == NULL)
755 1.15 martin error = ENOMEM;
756 1.15 martin else
757 1.15 martin error = vfs_composefh(rvp, nfs_pub.np_handle, &fhsize);
758 1.14 yamt if (error)
759 1.1 jmmv return error;
760 1.1 jmmv
761 1.1 jmmv vput(rvp);
762 1.1 jmmv
763 1.1 jmmv /*
764 1.1 jmmv * If an indexfile was specified, pull it in.
765 1.1 jmmv */
766 1.1 jmmv if (argp->ex_indexfile != NULL) {
767 1.1 jmmv MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
768 1.1 jmmv M_WAITOK);
769 1.1 jmmv error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
770 1.1 jmmv MAXNAMLEN, (size_t *)0);
771 1.1 jmmv if (!error) {
772 1.1 jmmv /*
773 1.1 jmmv * Check for illegal filenames.
774 1.1 jmmv */
775 1.1 jmmv for (cp = nfs_pub.np_index; *cp; cp++) {
776 1.1 jmmv if (*cp == '/') {
777 1.1 jmmv error = EINVAL;
778 1.1 jmmv break;
779 1.1 jmmv }
780 1.1 jmmv }
781 1.1 jmmv }
782 1.1 jmmv if (error) {
783 1.1 jmmv FREE(nfs_pub.np_index, M_TEMP);
784 1.1 jmmv return error;
785 1.1 jmmv }
786 1.1 jmmv }
787 1.1 jmmv
788 1.1 jmmv nfs_pub.np_mount = mp;
789 1.1 jmmv nfs_pub.np_valid = 1;
790 1.1 jmmv return 0;
791 1.1 jmmv }
792 1.1 jmmv
793 1.1 jmmv /*
794 1.1 jmmv * Lookup an export entry in the exports list that matches the address
795 1.1 jmmv * stored in 'nam'. If no entry is found, the default one is used instead
796 1.1 jmmv * (if available).
797 1.1 jmmv */
798 1.1 jmmv static struct netcred *
799 1.5 yamt netcred_lookup(struct netexport *ne, struct mbuf *nam)
800 1.1 jmmv {
801 1.1 jmmv struct netcred *np;
802 1.1 jmmv struct radix_node_head *rnh;
803 1.1 jmmv struct sockaddr *saddr;
804 1.1 jmmv
805 1.7 yamt if ((ne->ne_mount->mnt_flag & MNT_EXPORTED) == 0) {
806 1.7 yamt return NULL;
807 1.7 yamt }
808 1.5 yamt
809 1.5 yamt /*
810 1.5 yamt * Lookup in the export list first.
811 1.5 yamt */
812 1.1 jmmv np = NULL;
813 1.5 yamt if (nam != NULL) {
814 1.5 yamt saddr = mtod(nam, struct sockaddr *);
815 1.5 yamt rnh = ne->ne_rtable[saddr->sa_family];
816 1.5 yamt if (rnh != NULL) {
817 1.5 yamt np = (struct netcred *)
818 1.27 christos (*rnh->rnh_matchaddr)((void *)saddr,
819 1.5 yamt rnh);
820 1.5 yamt if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
821 1.5 yamt np = NULL;
822 1.1 jmmv }
823 1.1 jmmv }
824 1.5 yamt /*
825 1.5 yamt * If no address match, use the default if it exists.
826 1.5 yamt */
827 1.5 yamt if (np == NULL && ne->ne_mount->mnt_flag & MNT_DEFEXPORTED)
828 1.5 yamt np = &ne->ne_defexported;
829 1.5 yamt
830 1.1 jmmv return np;
831 1.1 jmmv }
832 1.8 yamt
833 1.8 yamt static struct lock netexport_lock = LOCK_INITIALIZER(PVFS, "netexp", 0, 0);
834 1.8 yamt
835 1.8 yamt void
836 1.8 yamt netexport_rdlock(void)
837 1.8 yamt {
838 1.8 yamt
839 1.8 yamt lockmgr(&netexport_lock, LK_SHARED, NULL);
840 1.8 yamt }
841 1.8 yamt
842 1.8 yamt void
843 1.8 yamt netexport_rdunlock(void)
844 1.8 yamt {
845 1.8 yamt
846 1.8 yamt lockmgr(&netexport_lock, LK_RELEASE, NULL);
847 1.8 yamt }
848 1.8 yamt
849 1.8 yamt static void
850 1.8 yamt netexport_wrlock(void)
851 1.8 yamt {
852 1.8 yamt
853 1.8 yamt lockmgr(&netexport_lock, LK_EXCLUSIVE, NULL);
854 1.8 yamt }
855 1.8 yamt
856 1.8 yamt static void
857 1.8 yamt netexport_wrunlock(void)
858 1.8 yamt {
859 1.8 yamt
860 1.8 yamt lockmgr(&netexport_lock, LK_RELEASE, NULL);
861 1.8 yamt }
862