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