Home | History | Annotate | Line # | Download | only in nfs
nfs_export.c revision 1.2
      1  1.2  jmmv /*	$NetBSD: nfs_export.c,v 1.2 2005/09/23 19:39:15 jmmv 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.2  jmmv __KERNEL_RCSID(0, "$NetBSD: nfs_export.c,v 1.2 2005/09/23 19:39:15 jmmv Exp $");
     86  1.1  jmmv 
     87  1.1  jmmv #include "opt_inet.h"
     88  1.1  jmmv 
     89  1.1  jmmv #include <sys/param.h>
     90  1.1  jmmv #include <sys/systm.h>
     91  1.1  jmmv #include <sys/queue.h>
     92  1.1  jmmv #include <sys/proc.h>
     93  1.1  jmmv #include <sys/mount.h>
     94  1.1  jmmv #include <sys/vnode.h>
     95  1.1  jmmv #include <sys/namei.h>
     96  1.1  jmmv #include <sys/errno.h>
     97  1.1  jmmv #include <sys/malloc.h>
     98  1.1  jmmv #include <sys/domain.h>
     99  1.1  jmmv #include <sys/mbuf.h>
    100  1.1  jmmv #include <sys/dirent.h>
    101  1.1  jmmv #include <sys/socket.h>		/* XXX for AF_MAX */
    102  1.1  jmmv 
    103  1.1  jmmv #include <net/radix.h>
    104  1.1  jmmv 
    105  1.1  jmmv #include <netinet/in.h>
    106  1.1  jmmv 
    107  1.1  jmmv #include <nfs/rpcv2.h>
    108  1.1  jmmv #include <nfs/nfsproto.h>
    109  1.1  jmmv #include <nfs/nfs.h>
    110  1.1  jmmv #include <nfs/nfs_var.h>
    111  1.1  jmmv 
    112  1.1  jmmv /*
    113  1.1  jmmv  * Network address lookup element.
    114  1.1  jmmv  */
    115  1.1  jmmv struct netcred {
    116  1.1  jmmv 	struct	radix_node netc_rnodes[2];
    117  1.1  jmmv 	int	netc_refcnt;
    118  1.1  jmmv 	int	netc_exflags;
    119  1.1  jmmv 	struct	ucred netc_anon;
    120  1.1  jmmv };
    121  1.1  jmmv 
    122  1.1  jmmv /*
    123  1.1  jmmv  * Network export information.
    124  1.1  jmmv  */
    125  1.1  jmmv struct netexport {
    126  1.1  jmmv 	struct	netcred ne_defexported;		      /* Default export */
    127  1.1  jmmv 	struct	radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
    128  1.1  jmmv };
    129  1.1  jmmv 
    130  1.1  jmmv /*
    131  1.1  jmmv  * Structures to map between standard mount points to their corresponding
    132  1.1  jmmv  * network export information.
    133  1.1  jmmv  */
    134  1.1  jmmv struct mount_netexport_pair {
    135  1.1  jmmv 	CIRCLEQ_ENTRY(mount_netexport_pair) mnp_entries;
    136  1.1  jmmv 	const struct mount *mnp_mount;
    137  1.1  jmmv 	struct netexport mnp_netexport;
    138  1.1  jmmv };
    139  1.1  jmmv CIRCLEQ_HEAD(mount_netexport_map, mount_netexport_pair)
    140  1.1  jmmv     mount_netexport_map = CIRCLEQ_HEAD_INITIALIZER(mount_netexport_map);
    141  1.1  jmmv 
    142  1.1  jmmv /* Malloc type used by the mount<->netexport map. */
    143  1.1  jmmv MALLOC_DEFINE(M_NFS_EXPORT, "nfs_export", "NFS export data");
    144  1.1  jmmv 
    145  1.1  jmmv /* Publicly exported file system. */
    146  1.1  jmmv struct nfs_public nfs_pub;
    147  1.1  jmmv 
    148  1.1  jmmv /*
    149  1.1  jmmv  * Local prototypes.
    150  1.1  jmmv  */
    151  1.1  jmmv static int init_exports(struct mount *, struct mount_netexport_pair **);
    152  1.1  jmmv static int hang_addrlist(struct mount *, struct netexport *,
    153  1.1  jmmv     const struct export_args *);
    154  1.1  jmmv static int sacheck(struct sockaddr *);
    155  1.1  jmmv static int free_netcred(struct radix_node *, void *);
    156  1.1  jmmv static void clear_exports(struct mount *, struct netexport *);
    157  1.1  jmmv static int export(struct mount *, struct netexport *,
    158  1.1  jmmv     const struct export_args *);
    159  1.1  jmmv static int setpublicfs(struct mount *, struct netexport *,
    160  1.1  jmmv     const struct export_args *);
    161  1.1  jmmv static struct netcred *export_lookup(struct mount *, struct netexport *,
    162  1.1  jmmv     struct mbuf *);
    163  1.1  jmmv 
    164  1.1  jmmv /*
    165  1.1  jmmv  * PUBLIC INTERFACE
    166  1.1  jmmv  */
    167  1.1  jmmv 
    168  1.1  jmmv /*
    169  1.1  jmmv  * Declare and initialize the file system export hooks.
    170  1.1  jmmv  */
    171  1.1  jmmv static void nfs_export_unmount(struct mount *);
    172  1.1  jmmv 
    173  1.1  jmmv struct vfs_hooks nfs_export_hooks = {
    174  1.1  jmmv 	nfs_export_unmount
    175  1.1  jmmv };
    176  1.1  jmmv VFS_HOOKS_ATTACH(nfs_export_hooks);
    177  1.1  jmmv 
    178  1.1  jmmv /*
    179  1.1  jmmv  * VFS unmount hook for NFS exports.
    180  1.1  jmmv  *
    181  1.1  jmmv  * Releases NFS exports list resources if the given mount point has some.
    182  1.1  jmmv  * As allocation happens lazily, it may be that it doesn't has this
    183  1.1  jmmv  * information, although it theorically should.
    184  1.1  jmmv  */
    185  1.1  jmmv static void
    186  1.1  jmmv nfs_export_unmount(struct mount *mp)
    187  1.1  jmmv {
    188  1.1  jmmv 	boolean_t found;
    189  1.1  jmmv 	struct mount_netexport_pair *mnp;
    190  1.1  jmmv 
    191  1.1  jmmv 	KASSERT(mp != NULL);
    192  1.1  jmmv 
    193  1.1  jmmv 	found = FALSE;
    194  1.1  jmmv 	CIRCLEQ_FOREACH(mnp, &mount_netexport_map, mnp_entries) {
    195  1.1  jmmv 		if (mnp->mnp_mount == mp) {
    196  1.1  jmmv 			found = TRUE;
    197  1.1  jmmv 			break;
    198  1.1  jmmv 		}
    199  1.1  jmmv 	}
    200  1.1  jmmv 
    201  1.1  jmmv 	if (mp->mnt_op->vfs_vptofh == NULL || mp->mnt_op->vfs_fhtovp == NULL)
    202  1.1  jmmv 		KASSERT(!found);
    203  1.1  jmmv 	else if (found) {
    204  1.1  jmmv 		if (mp->mnt_flag & MNT_EXPUBLIC)
    205  1.1  jmmv 			setpublicfs(NULL, NULL, NULL);
    206  1.1  jmmv 
    207  1.2  jmmv 		CIRCLEQ_REMOVE(&mount_netexport_map, mnp, mnp_entries);
    208  1.2  jmmv 
    209  1.1  jmmv 		free(mnp, M_NFS_EXPORT);
    210  1.1  jmmv 	}
    211  1.1  jmmv }
    212  1.1  jmmv 
    213  1.1  jmmv /*
    214  1.1  jmmv  * Atomically set the NFS exports list of the given file system, replacing
    215  1.1  jmmv  * it with a new list of entries.
    216  1.1  jmmv  *
    217  1.1  jmmv  * Returns zero on success or an appropriate error code otherwise.
    218  1.1  jmmv  *
    219  1.1  jmmv  * Helper function for the nfssvc(2) system call (NFSSVC_SETEXPORTSLIST
    220  1.1  jmmv  * command).
    221  1.1  jmmv  */
    222  1.1  jmmv int
    223  1.1  jmmv mountd_set_exports_list(const struct mountd_exports_list *mel, struct proc *p)
    224  1.1  jmmv {
    225  1.1  jmmv 	boolean_t found;
    226  1.1  jmmv 	int error;
    227  1.1  jmmv #ifdef notyet
    228  1.1  jmmv 	/* XXX: See below to see the reason why this is disabled. */
    229  1.1  jmmv 	size_t i;
    230  1.1  jmmv #endif
    231  1.1  jmmv 	struct mount *mp;
    232  1.1  jmmv 	struct mount_netexport_pair *mnp;
    233  1.1  jmmv 	struct nameidata nd;
    234  1.1  jmmv 	struct vnode *vp;
    235  1.1  jmmv 
    236  1.1  jmmv 	if (suser(p->p_ucred, &p->p_acflag) != 0)
    237  1.1  jmmv 		return EPERM;
    238  1.1  jmmv 
    239  1.1  jmmv 	/* Lookup the file system path. */
    240  1.1  jmmv 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, mel->mel_path, p);
    241  1.1  jmmv 	error = namei(&nd);
    242  1.1  jmmv 	if (error != 0)
    243  1.1  jmmv 		return error;
    244  1.1  jmmv 	vp = (struct vnode *)nd.ni_vp;
    245  1.1  jmmv 	mp = (struct mount *)vp->v_mount;
    246  1.1  jmmv 
    247  1.1  jmmv 	/* The selected file system may not support NFS exports, so ensure
    248  1.1  jmmv 	 * it does. */
    249  1.1  jmmv 	if (mp->mnt_op->vfs_vptofh == NULL && mp->mnt_op->vfs_fhtovp == NULL) {
    250  1.1  jmmv 		error = EOPNOTSUPP;
    251  1.1  jmmv 		goto out_locked;
    252  1.1  jmmv 	}
    253  1.1  jmmv 	KASSERT(mp->mnt_op->vfs_vptofh != NULL &&
    254  1.1  jmmv 	    mp->mnt_op->vfs_fhtovp != NULL);
    255  1.1  jmmv 
    256  1.1  jmmv 	/* Mark the file system busy. */
    257  1.1  jmmv 	error = vfs_busy(mp, LK_NOWAIT, NULL);
    258  1.1  jmmv 	if (error != 0)
    259  1.1  jmmv 		goto out_locked;
    260  1.1  jmmv 
    261  1.1  jmmv 	found = FALSE;
    262  1.1  jmmv 	CIRCLEQ_FOREACH(mnp, &mount_netexport_map, mnp_entries) {
    263  1.1  jmmv 		if (mnp->mnp_mount == mp) {
    264  1.1  jmmv 			found = TRUE;
    265  1.1  jmmv 			break;
    266  1.1  jmmv 		}
    267  1.1  jmmv 	}
    268  1.1  jmmv 	if (!found) {
    269  1.1  jmmv 		error = init_exports(mp, &mnp);
    270  1.1  jmmv 		if (error != 0) {
    271  1.1  jmmv 			vfs_unbusy(mp);
    272  1.1  jmmv 			goto out_locked;
    273  1.1  jmmv 		}
    274  1.1  jmmv 	}
    275  1.1  jmmv 
    276  1.1  jmmv 	/*
    277  1.1  jmmv 	 * XXX: The part marked as 'notyet' works fine from the kernel's
    278  1.1  jmmv 	 * point of view, in the sense that it is able to atomically update
    279  1.1  jmmv 	 * the complete exports list for a file system.  However, supporting
    280  1.1  jmmv 	 * this in mountd(8) requires a lot of work; so, for now, keep the
    281  1.1  jmmv 	 * old behavior of updating a single entry per call.
    282  1.1  jmmv 	 *
    283  1.1  jmmv 	 * When mountd(8) is fixed, just remove the second branch of this
    284  1.1  jmmv 	 * preprocessor conditional and enable the first one.
    285  1.1  jmmv 	 */
    286  1.1  jmmv #ifdef notyet
    287  1.1  jmmv 	clear_exports(mp, &mnp->mnp_netexport);
    288  1.1  jmmv 	for (i = 0; error == 0 && i < mel->mel_nexports; i++)
    289  1.1  jmmv 		error = export(mp, &mnp->mnp_netexport, &mel->mel_exports[i]);
    290  1.1  jmmv #else
    291  1.1  jmmv 	if (mel->mel_nexports == 0)
    292  1.1  jmmv 		clear_exports(mp, &mnp->mnp_netexport);
    293  1.1  jmmv 	else if (mel->mel_nexports == 1)
    294  1.1  jmmv 		error = export(mp, &mnp->mnp_netexport, &mel->mel_exports[0]);
    295  1.1  jmmv 	else {
    296  1.1  jmmv 		printf("mountd_set_exports_list: Cannot set more than one "
    297  1.1  jmmv 		    "entry at once (unimplemented)\n");
    298  1.1  jmmv 		error = EOPNOTSUPP;
    299  1.1  jmmv 	}
    300  1.1  jmmv #endif
    301  1.1  jmmv 
    302  1.1  jmmv 	vfs_unbusy(mp);
    303  1.1  jmmv 
    304  1.1  jmmv out_locked:
    305  1.1  jmmv 	vput(vp);
    306  1.1  jmmv 
    307  1.1  jmmv 	return 0;
    308  1.1  jmmv }
    309  1.1  jmmv 
    310  1.1  jmmv /*
    311  1.1  jmmv  * Check if the file system specified by the 'mp' mount structure is
    312  1.1  jmmv  * exported to a client with 'anon' anonymous credentials.  The 'mb'
    313  1.1  jmmv  * argument is an mbuf containing the network address of the client.
    314  1.1  jmmv  * The return parameters for the export flags for the client are returned
    315  1.1  jmmv  * in the address specified by 'wh'.
    316  1.1  jmmv  *
    317  1.1  jmmv  * This function is used exclusively by the NFS server.  It is generally
    318  1.1  jmmv  * invoked before VFS_FHTOVP to validate that client has access to the
    319  1.1  jmmv  * file system.
    320  1.1  jmmv  */
    321  1.1  jmmv int
    322  1.1  jmmv nfs_check_export(struct mount *mp, struct mbuf *mb, int *wh,
    323  1.1  jmmv     struct ucred **anon)
    324  1.1  jmmv {
    325  1.1  jmmv 	boolean_t found;
    326  1.1  jmmv 	struct mount_netexport_pair *mnp;
    327  1.1  jmmv 	struct netcred *np;
    328  1.1  jmmv 
    329  1.1  jmmv 	found = FALSE;
    330  1.1  jmmv 	CIRCLEQ_FOREACH(mnp, &mount_netexport_map, mnp_entries) {
    331  1.1  jmmv 		if (mnp->mnp_mount == mp) {
    332  1.1  jmmv 			found = TRUE;
    333  1.1  jmmv 			break;
    334  1.1  jmmv 		}
    335  1.1  jmmv 	}
    336  1.1  jmmv 	if (!found)
    337  1.1  jmmv 		return EACCES;
    338  1.1  jmmv 
    339  1.1  jmmv 	np = export_lookup(mp, &mnp->mnp_netexport, mb);
    340  1.1  jmmv 	if (np != NULL) {
    341  1.1  jmmv 		*wh = np->netc_exflags;
    342  1.1  jmmv 		*anon = &np->netc_anon;
    343  1.1  jmmv 	}
    344  1.1  jmmv 
    345  1.1  jmmv 	return np == NULL ? EACCES : 0;
    346  1.1  jmmv }
    347  1.1  jmmv 
    348  1.1  jmmv /*
    349  1.1  jmmv  * INTERNAL FUNCTIONS
    350  1.1  jmmv  */
    351  1.1  jmmv 
    352  1.1  jmmv /*
    353  1.1  jmmv  * Initializes NFS exports for the file system given in 'mp' if it supports
    354  1.1  jmmv  * file handles; this is determined by checking whether mp's vfs_vptofh and
    355  1.1  jmmv  * vfs_fhtovp operations are NULL or not.
    356  1.1  jmmv  *
    357  1.1  jmmv  * If successful, returns 0 and sets *mnpp to the address of the new
    358  1.1  jmmv  * mount_netexport_pair item; otherwise returns and appropriate error code
    359  1.1  jmmv  * and *mnpp remains unmodified.
    360  1.1  jmmv  */
    361  1.1  jmmv static int
    362  1.1  jmmv init_exports(struct mount *mp, struct mount_netexport_pair **mnpp)
    363  1.1  jmmv {
    364  1.1  jmmv 	int error;
    365  1.1  jmmv 	struct export_args ea;
    366  1.1  jmmv 	struct mount_netexport_pair *mnp;
    367  1.1  jmmv 
    368  1.1  jmmv 	KASSERT(mp != NULL);
    369  1.1  jmmv 	KASSERT(mp->mnt_op->vfs_vptofh != NULL &&
    370  1.1  jmmv 	    mp->mnt_op->vfs_fhtovp != NULL);
    371  1.1  jmmv 
    372  1.1  jmmv #ifdef DIAGNOSTIC
    373  1.1  jmmv 	/* Ensure that we do not already have this mount point. */
    374  1.1  jmmv 	CIRCLEQ_FOREACH(mnp, &mount_netexport_map, mnp_entries) {
    375  1.1  jmmv 		if (mnp->mnp_mount == mp)
    376  1.1  jmmv 			KASSERT(0);
    377  1.1  jmmv 	}
    378  1.1  jmmv #endif
    379  1.1  jmmv 
    380  1.1  jmmv 	mnp = (struct mount_netexport_pair *)
    381  1.1  jmmv 	    malloc(sizeof(struct mount_netexport_pair), M_NFS_EXPORT, M_WAITOK);
    382  1.1  jmmv 	KASSERT(mnp != NULL);
    383  1.1  jmmv 	mnp->mnp_mount = mp;
    384  1.1  jmmv 	memset(&mnp->mnp_netexport, 0, sizeof(mnp->mnp_netexport));
    385  1.1  jmmv 
    386  1.1  jmmv 	/* Set the default export entry.  Handled internally by export upon
    387  1.1  jmmv 	 * first call. */
    388  1.1  jmmv 	memset(&ea, 0, sizeof(ea));
    389  1.1  jmmv 	ea.ex_root = -2;
    390  1.1  jmmv 	if (mp->mnt_flag & MNT_RDONLY)
    391  1.1  jmmv 		ea.ex_flags |= MNT_EXRDONLY;
    392  1.1  jmmv 	error = export(mp, &mnp->mnp_netexport, &ea);
    393  1.1  jmmv 	if (error != 0)
    394  1.1  jmmv 		free(mnp, M_NFS_EXPORT);
    395  1.1  jmmv 	else {
    396  1.1  jmmv 		CIRCLEQ_INSERT_TAIL(&mount_netexport_map, mnp, mnp_entries);
    397  1.1  jmmv 		*mnpp = mnp;
    398  1.1  jmmv 	}
    399  1.1  jmmv 
    400  1.1  jmmv 	return error;
    401  1.1  jmmv }
    402  1.1  jmmv 
    403  1.1  jmmv /*
    404  1.1  jmmv  * Build hash lists of net addresses and hang them off the mount point.
    405  1.1  jmmv  * Called by export() to set up a new entry in the lists of export
    406  1.1  jmmv  * addresses.
    407  1.1  jmmv  */
    408  1.1  jmmv static int
    409  1.1  jmmv hang_addrlist(struct mount *mp, struct netexport *nep,
    410  1.1  jmmv     const struct export_args *argp)
    411  1.1  jmmv {
    412  1.1  jmmv 	int error, i;
    413  1.1  jmmv 	struct netcred *np, *enp;
    414  1.1  jmmv 	struct radix_node_head *rnh;
    415  1.1  jmmv 	struct sockaddr *saddr, *smask;
    416  1.1  jmmv 	struct domain *dom;
    417  1.1  jmmv 
    418  1.1  jmmv 	smask = NULL;
    419  1.1  jmmv 
    420  1.1  jmmv 	if (argp->ex_addrlen == 0) {
    421  1.1  jmmv 		if (mp->mnt_flag & MNT_DEFEXPORTED)
    422  1.1  jmmv 			return EPERM;
    423  1.1  jmmv 		np = &nep->ne_defexported;
    424  1.1  jmmv 		np->netc_exflags = argp->ex_flags;
    425  1.1  jmmv 		crcvt(&np->netc_anon, &argp->ex_anon);
    426  1.1  jmmv 		np->netc_anon.cr_ref = 1;
    427  1.1  jmmv 		mp->mnt_flag |= MNT_DEFEXPORTED;
    428  1.1  jmmv 		return 0;
    429  1.1  jmmv 	}
    430  1.1  jmmv 
    431  1.1  jmmv 	if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN)
    432  1.1  jmmv 		return EINVAL;
    433  1.1  jmmv 
    434  1.1  jmmv 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
    435  1.1  jmmv 	np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK);
    436  1.1  jmmv 	memset((caddr_t)np, 0, i);
    437  1.1  jmmv 	saddr = (struct sockaddr *)(np + 1);
    438  1.1  jmmv 	error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen);
    439  1.1  jmmv 	if (error)
    440  1.1  jmmv 		goto out;
    441  1.1  jmmv 	if (saddr->sa_len > argp->ex_addrlen)
    442  1.1  jmmv 		saddr->sa_len = argp->ex_addrlen;
    443  1.1  jmmv 	if (sacheck(saddr) == -1)
    444  1.1  jmmv 		return EINVAL;
    445  1.1  jmmv 	if (argp->ex_masklen) {
    446  1.1  jmmv 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
    447  1.1  jmmv 		error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
    448  1.1  jmmv 		if (error)
    449  1.1  jmmv 			goto out;
    450  1.1  jmmv 		if (smask->sa_len > argp->ex_masklen)
    451  1.1  jmmv 			smask->sa_len = argp->ex_masklen;
    452  1.1  jmmv 		if (smask->sa_family != saddr->sa_family)
    453  1.1  jmmv 			return EINVAL;
    454  1.1  jmmv 		if (sacheck(smask) == -1)
    455  1.1  jmmv 			return EINVAL;
    456  1.1  jmmv 	}
    457  1.1  jmmv 	i = saddr->sa_family;
    458  1.1  jmmv 	if ((rnh = nep->ne_rtable[i]) == 0) {
    459  1.1  jmmv 		/*
    460  1.1  jmmv 		 * Seems silly to initialize every AF when most are not
    461  1.1  jmmv 		 * used, do so on demand here
    462  1.1  jmmv 		 */
    463  1.1  jmmv 		DOMAIN_FOREACH(dom) {
    464  1.1  jmmv 			if (dom->dom_family == i && dom->dom_rtattach) {
    465  1.1  jmmv 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
    466  1.1  jmmv 					dom->dom_rtoffset);
    467  1.1  jmmv 				break;
    468  1.1  jmmv 			}
    469  1.1  jmmv 		}
    470  1.1  jmmv 		if ((rnh = nep->ne_rtable[i]) == 0) {
    471  1.1  jmmv 			error = ENOBUFS;
    472  1.1  jmmv 			goto out;
    473  1.1  jmmv 		}
    474  1.1  jmmv 	}
    475  1.1  jmmv 
    476  1.1  jmmv 	enp = (struct netcred *)(*rnh->rnh_addaddr)(saddr, smask, rnh,
    477  1.1  jmmv 	    np->netc_rnodes);
    478  1.1  jmmv 	if (enp != np) {
    479  1.1  jmmv 		if (enp == NULL) {
    480  1.1  jmmv 			enp = (struct netcred *)(*rnh->rnh_lookup)(saddr,
    481  1.1  jmmv 			    smask, rnh);
    482  1.1  jmmv 			if (enp == NULL) {
    483  1.1  jmmv 				error = EPERM;
    484  1.1  jmmv 				goto out;
    485  1.1  jmmv 			}
    486  1.1  jmmv 		} else
    487  1.1  jmmv 			enp->netc_refcnt++;
    488  1.1  jmmv 
    489  1.1  jmmv 		goto check;
    490  1.1  jmmv 	} else
    491  1.1  jmmv 		enp->netc_refcnt = 1;
    492  1.1  jmmv 
    493  1.1  jmmv 	np->netc_exflags = argp->ex_flags;
    494  1.1  jmmv 	crcvt(&np->netc_anon, &argp->ex_anon);
    495  1.1  jmmv 	np->netc_anon.cr_ref = 1;
    496  1.1  jmmv 	return 0;
    497  1.1  jmmv check:
    498  1.1  jmmv 	if (enp->netc_exflags != argp->ex_flags ||
    499  1.1  jmmv 	    crcmp(&enp->netc_anon, &argp->ex_anon) != 0)
    500  1.1  jmmv 		error = EPERM;
    501  1.1  jmmv 	else
    502  1.1  jmmv 		error = 0;
    503  1.1  jmmv out:
    504  1.1  jmmv 	free(np, M_NETADDR);
    505  1.1  jmmv 	return error;
    506  1.1  jmmv }
    507  1.1  jmmv 
    508  1.1  jmmv /*
    509  1.1  jmmv  * Ensure that the address stored in 'sa' is valid.
    510  1.1  jmmv  * Returns zero on success, otherwise -1.
    511  1.1  jmmv  */
    512  1.1  jmmv static int
    513  1.1  jmmv sacheck(struct sockaddr *sa)
    514  1.1  jmmv {
    515  1.1  jmmv 
    516  1.1  jmmv 	switch (sa->sa_family) {
    517  1.1  jmmv #ifdef INET
    518  1.1  jmmv 	case AF_INET: {
    519  1.1  jmmv 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    520  1.1  jmmv 		char *p = (char *)sin->sin_zero;
    521  1.1  jmmv 		size_t i;
    522  1.1  jmmv 
    523  1.1  jmmv 		if (sin->sin_len != sizeof(*sin))
    524  1.1  jmmv 			return -1;
    525  1.1  jmmv 		if (sin->sin_port != 0)
    526  1.1  jmmv 			return -1;
    527  1.1  jmmv 		for (i = 0; i < sizeof(sin->sin_zero); i++)
    528  1.1  jmmv 			if (*p++ != '\0')
    529  1.1  jmmv 				return -1;
    530  1.1  jmmv 		return 0;
    531  1.1  jmmv 	}
    532  1.1  jmmv #endif
    533  1.1  jmmv #ifdef INET6
    534  1.1  jmmv 	case AF_INET6: {
    535  1.1  jmmv 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    536  1.1  jmmv 
    537  1.1  jmmv 		if (sin6->sin6_len != sizeof(*sin6))
    538  1.1  jmmv 			return -1;
    539  1.1  jmmv 		if (sin6->sin6_port != 0)
    540  1.1  jmmv 			return -1;
    541  1.1  jmmv 		return 0;
    542  1.1  jmmv 	}
    543  1.1  jmmv #endif
    544  1.1  jmmv 	default:
    545  1.1  jmmv 		return -1;
    546  1.1  jmmv 	}
    547  1.1  jmmv }
    548  1.1  jmmv 
    549  1.1  jmmv /*
    550  1.1  jmmv  * Free the netcred object pointed to by the 'rn' radix node.
    551  1.1  jmmv  * 'w' holds a pointer to the radix tree head.
    552  1.1  jmmv  */
    553  1.1  jmmv static int
    554  1.1  jmmv free_netcred(struct radix_node *rn, void *w)
    555  1.1  jmmv {
    556  1.1  jmmv 	struct radix_node_head *rnh = (struct radix_node_head *)w;
    557  1.1  jmmv 	struct netcred *np = (struct netcred *)(void *)rn;
    558  1.1  jmmv 
    559  1.1  jmmv 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
    560  1.1  jmmv 	if (--(np->netc_refcnt) <= 0)
    561  1.1  jmmv 		free(np, M_NETADDR);
    562  1.1  jmmv 	return 0;
    563  1.1  jmmv }
    564  1.1  jmmv 
    565  1.1  jmmv /*
    566  1.1  jmmv  * Clears the exports list for a given file system.
    567  1.1  jmmv  */
    568  1.1  jmmv static void
    569  1.1  jmmv clear_exports(struct mount *mp, struct netexport *nep)
    570  1.1  jmmv {
    571  1.1  jmmv 	int i;
    572  1.1  jmmv 	struct radix_node_head *rnh;
    573  1.1  jmmv 
    574  1.1  jmmv 	if (mp->mnt_flag & MNT_EXPUBLIC) {
    575  1.1  jmmv 		setpublicfs(NULL, NULL, NULL);
    576  1.1  jmmv 		mp->mnt_flag &= ~MNT_EXPUBLIC;
    577  1.1  jmmv 	}
    578  1.1  jmmv 
    579  1.1  jmmv 	for (i = 0; i <= AF_MAX; i++) {
    580  1.1  jmmv 		if ((rnh = nep->ne_rtable[i]) != NULL) {
    581  1.1  jmmv 			(*rnh->rnh_walktree)(rnh, free_netcred, rnh);
    582  1.1  jmmv 			free((caddr_t)rnh, M_RTABLE);
    583  1.1  jmmv 			nep->ne_rtable[i] = 0;
    584  1.1  jmmv 		}
    585  1.1  jmmv 	}
    586  1.1  jmmv 
    587  1.1  jmmv 	mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
    588  1.1  jmmv }
    589  1.1  jmmv 
    590  1.1  jmmv /*
    591  1.1  jmmv  * Add a new export entry (described by an export_args structure) to the
    592  1.1  jmmv  * given file system.
    593  1.1  jmmv  */
    594  1.1  jmmv static int
    595  1.1  jmmv export(struct mount *mp, struct netexport *nep, const struct export_args *argp)
    596  1.1  jmmv {
    597  1.1  jmmv 	int error;
    598  1.1  jmmv 
    599  1.1  jmmv 	if (argp->ex_flags & MNT_EXPORTED) {
    600  1.1  jmmv 		if (argp->ex_flags & MNT_EXPUBLIC) {
    601  1.1  jmmv 			if ((error = setpublicfs(mp, nep, argp)) != 0)
    602  1.1  jmmv 				return error;
    603  1.1  jmmv 			mp->mnt_flag |= MNT_EXPUBLIC;
    604  1.1  jmmv 		}
    605  1.1  jmmv 		if ((error = hang_addrlist(mp, nep, argp)) != 0)
    606  1.1  jmmv 			return error;
    607  1.1  jmmv 		mp->mnt_flag |= MNT_EXPORTED;
    608  1.1  jmmv 	}
    609  1.1  jmmv 	return 0;
    610  1.1  jmmv }
    611  1.1  jmmv 
    612  1.1  jmmv /*
    613  1.1  jmmv  * Set the publicly exported filesystem (WebNFS).  Currently, only
    614  1.1  jmmv  * one public filesystem is possible in the spec (RFC 2054 and 2055)
    615  1.1  jmmv  */
    616  1.1  jmmv static int
    617  1.1  jmmv setpublicfs(struct mount *mp, struct netexport *nep,
    618  1.1  jmmv     const struct export_args *argp)
    619  1.1  jmmv {
    620  1.1  jmmv 	char *cp;
    621  1.1  jmmv 	int error;
    622  1.1  jmmv 	struct vnode *rvp;
    623  1.1  jmmv 
    624  1.1  jmmv 	/*
    625  1.1  jmmv 	 * mp == NULL -> invalidate the current info, the FS is
    626  1.1  jmmv 	 * no longer exported. May be called from either export
    627  1.1  jmmv 	 * or unmount, so check if it hasn't already been done.
    628  1.1  jmmv 	 */
    629  1.1  jmmv 	if (mp == NULL) {
    630  1.1  jmmv 		if (nfs_pub.np_valid) {
    631  1.1  jmmv 			nfs_pub.np_valid = 0;
    632  1.1  jmmv 			if (nfs_pub.np_index != NULL) {
    633  1.1  jmmv 				FREE(nfs_pub.np_index, M_TEMP);
    634  1.1  jmmv 				nfs_pub.np_index = NULL;
    635  1.1  jmmv 			}
    636  1.1  jmmv 		}
    637  1.1  jmmv 		return 0;
    638  1.1  jmmv 	}
    639  1.1  jmmv 
    640  1.1  jmmv 	/*
    641  1.1  jmmv 	 * Only one allowed at a time.
    642  1.1  jmmv 	 */
    643  1.1  jmmv 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
    644  1.1  jmmv 		return EBUSY;
    645  1.1  jmmv 
    646  1.1  jmmv 	/*
    647  1.1  jmmv 	 * Get real filehandle for root of exported FS.
    648  1.1  jmmv 	 */
    649  1.1  jmmv 	memset((caddr_t)&nfs_pub.np_handle, 0, sizeof(nfs_pub.np_handle));
    650  1.1  jmmv 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsidx;
    651  1.1  jmmv 
    652  1.1  jmmv 	if ((error = VFS_ROOT(mp, &rvp)))
    653  1.1  jmmv 		return error;
    654  1.1  jmmv 
    655  1.1  jmmv 	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
    656  1.1  jmmv 		return error;
    657  1.1  jmmv 
    658  1.1  jmmv 	vput(rvp);
    659  1.1  jmmv 
    660  1.1  jmmv 	/*
    661  1.1  jmmv 	 * If an indexfile was specified, pull it in.
    662  1.1  jmmv 	 */
    663  1.1  jmmv 	if (argp->ex_indexfile != NULL) {
    664  1.1  jmmv 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
    665  1.1  jmmv 		    M_WAITOK);
    666  1.1  jmmv 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
    667  1.1  jmmv 		    MAXNAMLEN, (size_t *)0);
    668  1.1  jmmv 		if (!error) {
    669  1.1  jmmv 			/*
    670  1.1  jmmv 			 * Check for illegal filenames.
    671  1.1  jmmv 			 */
    672  1.1  jmmv 			for (cp = nfs_pub.np_index; *cp; cp++) {
    673  1.1  jmmv 				if (*cp == '/') {
    674  1.1  jmmv 					error = EINVAL;
    675  1.1  jmmv 					break;
    676  1.1  jmmv 				}
    677  1.1  jmmv 			}
    678  1.1  jmmv 		}
    679  1.1  jmmv 		if (error) {
    680  1.1  jmmv 			FREE(nfs_pub.np_index, M_TEMP);
    681  1.1  jmmv 			return error;
    682  1.1  jmmv 		}
    683  1.1  jmmv 	}
    684  1.1  jmmv 
    685  1.1  jmmv 	nfs_pub.np_mount = mp;
    686  1.1  jmmv 	nfs_pub.np_valid = 1;
    687  1.1  jmmv 	return 0;
    688  1.1  jmmv }
    689  1.1  jmmv 
    690  1.1  jmmv /*
    691  1.1  jmmv  * Lookup an export entry in the exports list that matches the address
    692  1.1  jmmv  * stored in 'nam'.  If no entry is found, the default one is used instead
    693  1.1  jmmv  * (if available).
    694  1.1  jmmv  */
    695  1.1  jmmv static struct netcred *
    696  1.1  jmmv export_lookup(struct mount *mp, struct netexport *nep, struct mbuf *nam)
    697  1.1  jmmv {
    698  1.1  jmmv 	struct netcred *np;
    699  1.1  jmmv 	struct radix_node_head *rnh;
    700  1.1  jmmv 	struct sockaddr *saddr;
    701  1.1  jmmv 
    702  1.1  jmmv 	np = NULL;
    703  1.1  jmmv 	if (mp->mnt_flag & MNT_EXPORTED) {
    704  1.1  jmmv 		/*
    705  1.1  jmmv 		 * Lookup in the export list first.
    706  1.1  jmmv 		 */
    707  1.1  jmmv 		if (nam != NULL) {
    708  1.1  jmmv 			saddr = mtod(nam, struct sockaddr *);
    709  1.1  jmmv 			rnh = nep->ne_rtable[saddr->sa_family];
    710  1.1  jmmv 			if (rnh != NULL) {
    711  1.1  jmmv 				np = (struct netcred *)
    712  1.1  jmmv 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
    713  1.1  jmmv 							      rnh);
    714  1.1  jmmv 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
    715  1.1  jmmv 					np = NULL;
    716  1.1  jmmv 			}
    717  1.1  jmmv 		}
    718  1.1  jmmv 		/*
    719  1.1  jmmv 		 * If no address match, use the default if it exists.
    720  1.1  jmmv 		 */
    721  1.1  jmmv 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
    722  1.1  jmmv 			np = &nep->ne_defexported;
    723  1.1  jmmv 	}
    724  1.1  jmmv 	return np;
    725  1.1  jmmv }
    726