#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nfs_export.c,v 1.63 2021/06/04 10:44:58 hannken Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/queue.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/domain.h>
#include <sys/mbuf.h>
#include <sys/dirent.h>
#include <sys/socket.h>
#include <sys/kauth.h>
#include <net/radix.h>
#include <netinet/in.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfs/nfs.h>
#include <nfs/nfs_var.h>
struct netcred {
struct radix_node netc_rnodes[2];
int netc_refcnt;
int netc_exflags;
kauth_cred_t netc_anon;
};
struct netexport {
TAILQ_ENTRY(netexport) ne_list;
struct mount *ne_mount;
struct netcred ne_defexported;
struct radix_node_head *ne_rtable[AF_MAX+1];
};
TAILQ_HEAD(, netexport) netexport_list =
TAILQ_HEAD_INITIALIZER(netexport_list);
struct nfs_public nfs_pub;
static int init_exports(struct mount *, struct netexport **);
static int hang_addrlist(struct mount *, struct netexport *,
const struct export_args *);
static int sacheck(struct sockaddr *);
static int free_netcred(struct radix_node *, void *);
static int export(struct netexport *, const struct export_args *);
static int setpublicfs(struct mount *, struct netexport *,
const struct export_args *);
static struct netcred *netcred_lookup(struct netexport *, struct mbuf *);
static struct netexport *netexport_lookup(const struct mount *);
static struct netexport *netexport_lookup_byfsid(const fsid_t *);
static void netexport_clear(struct netexport *);
static void netexport_insert(struct netexport *);
static void netexport_remove(struct netexport *);
static void netexport_wrlock(void);
static void netexport_wrunlock(void);
static int nfs_export_update_30(struct mount *mp, const char *path, void *);
static krwlock_t netexport_lock;
static void netexport_unmount(struct mount *);
struct vfs_hooks nfs_export_hooks = {
{ NULL, NULL },
.vh_unmount = netexport_unmount,
.vh_reexport = nfs_export_update_30,
};
static void
netexport_unmount(struct mount *mp)
{
struct netexport *ne;
KASSERT(mp != NULL);
netexport_wrlock();
ne = netexport_lookup(mp);
if (ne == NULL) {
netexport_wrunlock();
return;
}
netexport_clear(ne);
netexport_remove(ne);
netexport_wrunlock();
kmem_free(ne, sizeof(*ne));
}
void
netexport_init(void)
{
rw_init(&netexport_lock);
}
void
netexport_fini(void)
{
struct netexport *ne;
struct mount *mp;
int error;
while (!TAILQ_EMPTY(&netexport_list)) {
netexport_wrlock();
ne = TAILQ_FIRST(&netexport_list);
mp = ne->ne_mount;
error = vfs_busy(mp);
netexport_wrunlock();
if (error != 0) {
kpause("nfsfini", false, hz, NULL);
continue;
}
mutex_enter(mp->mnt_updating);
netexport_unmount(mp);
mutex_exit(mp->mnt_updating);
vfs_unbusy(mp);
}
rw_destroy(&netexport_lock);
}
int
mountd_set_exports_list(const struct mountd_exports_list *mel, struct lwp *l,
struct mount *nmp, int cmd)
{
int error;
size_t i;
struct mount *mp;
struct netexport *ne;
struct pathbuf *pb;
struct nameidata nd;
struct vnode *vp;
size_t fid_size;
if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
KAUTH_REQ_NETWORK_NFS_EXPORT, NULL, NULL, NULL) != 0)
return EPERM;
error = pathbuf_copyin(mel->mel_path, &pb);
if (error) {
return error;
}
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, pb);
error = namei(&nd);
if (error != 0) {
pathbuf_destroy(pb);
return error;
}
vp = nd.ni_vp;
mp = vp->v_mount;
KASSERT(nmp == NULL || nmp == mp);
pathbuf_destroy(pb);
fid_size = 0;
if ((error = VFS_VPTOFH(vp, NULL, &fid_size)) != E2BIG) {
vput(vp);
return EOPNOTSUPP;
}
error = vfs_busy(mp);
vput(vp);
if (error != 0)
return error;
if (nmp == NULL)
mutex_enter(mp->mnt_updating);
netexport_wrlock();
ne = netexport_lookup(mp);
if (ne == NULL) {
error = init_exports(mp, &ne);
if (error != 0) {
goto out;
}
}
KASSERT(ne != NULL);
KASSERT(ne->ne_mount == mp);
if (cmd == NFSSVC_SETEXPORTSLIST) {
if (mel->mel_nexports == 0)
netexport_clear(ne);
else if (mel->mel_nexports == 1)
error = export(ne, &mel->mel_exports[0]);
else {
printf("%s: Cannot set more than one "
"entry at once (unimplemented)\n", __func__);
error = EOPNOTSUPP;
}
} else if (cmd == NFSSVC_REPLACEEXPORTSLIST) {
netexport_clear(ne);
for (i = 0; error == 0 && i < mel->mel_nexports; i++)
error = export(ne, &mel->mel_exports[i]);
} else {
printf("%s: Command %#x not implemented\n", __func__, cmd);
error = EOPNOTSUPP;
}
out:
netexport_wrunlock();
if (nmp == NULL)
mutex_exit(mp->mnt_updating);
vfs_unbusy(mp);
return error;
}
static void
netexport_insert(struct netexport *ne)
{
TAILQ_INSERT_HEAD(&netexport_list, ne, ne_list);
}
static void
netexport_remove(struct netexport *ne)
{
TAILQ_REMOVE(&netexport_list, ne, ne_list);
}
static struct netexport *
netexport_lookup(const struct mount *mp)
{
struct netexport *ne;
TAILQ_FOREACH(ne, &netexport_list, ne_list) {
if (ne->ne_mount == mp) {
goto done;
}
}
ne = NULL;
done:
return ne;
}
static struct netexport *
netexport_lookup_byfsid(const fsid_t *fsid)
{
struct netexport *ne;
TAILQ_FOREACH(ne, &netexport_list, ne_list) {
const struct mount *mp = ne->ne_mount;
if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
goto done;
}
}
ne = NULL;
done:
return ne;
}
int
netexport_check(const fsid_t *fsid, struct mbuf *mb, struct mount **mpp,
int *wh, kauth_cred_t *anon)
{
struct netexport *ne;
struct netcred *np;
ne = netexport_lookup_byfsid(fsid);
if (ne == NULL) {
return EACCES;
}
np = netcred_lookup(ne, mb);
if (np == NULL) {
return EACCES;
}
*mpp = ne->ne_mount;
*wh = np->netc_exflags;
*anon = np->netc_anon;
return 0;
}
static int
nfs_export_update_30(struct mount *mp, const char *path, void *data)
{
struct mountd_exports_list mel;
struct mnt_export_args30 *args;
args = data;
mel.mel_path = path;
if (args->fspec != NULL)
return EJUSTRETURN;
if (args->eargs.ex_flags & 0x00020000) {
mel.mel_nexports = 0;
} else {
__CTASSERT(sizeof(args->eargs) == sizeof(*mel.mel_exports));
mel.mel_nexports = 1;
mel.mel_exports = (void *)&args->eargs;
}
return mountd_set_exports_list(&mel, curlwp, mp, NFSSVC_SETEXPORTSLIST);
}
static int
init_exports(struct mount *mp, struct netexport **nep)
{
int error;
struct export_args ea;
struct netexport *ne;
KASSERT(mp != NULL);
KASSERT(netexport_lookup(mp) == NULL);
ne = kmem_zalloc(sizeof(*ne), KM_SLEEP);
ne->ne_mount = mp;
memset(&ea, 0, sizeof(ea));
ea.ex_root = -2;
if (mp->mnt_flag & MNT_RDONLY)
ea.ex_flags |= MNT_EXRDONLY;
error = export(ne, &ea);
if (error != 0) {
kmem_free(ne, sizeof(*ne));
} else {
netexport_insert(ne);
*nep = ne;
}
return error;
}
static int
hang_addrlist(struct mount *mp, struct netexport *nep,
const struct export_args *argp)
{
int error, i;
struct netcred *np, *enp;
struct radix_node_head *rnh;
struct sockaddr *saddr, *smask;
struct domain *dom;
smask = NULL;
if (argp->ex_addrlen == 0) {
if (mp->mnt_flag & MNT_DEFEXPORTED)
return EPERM;
np = &nep->ne_defexported;
KASSERT(np->netc_anon == NULL);
np->netc_anon = kauth_cred_alloc();
np->netc_exflags = argp->ex_flags;
kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
mp->mnt_flag |= MNT_DEFEXPORTED;
return 0;
}
if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN)
return EINVAL;
i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
np = malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
np->netc_anon = kauth_cred_alloc();
saddr = (struct sockaddr *)(np + 1);
error = copyin(argp->ex_addr, saddr, argp->ex_addrlen);
if (error)
goto out;
if (saddr->sa_len > argp->ex_addrlen)
saddr->sa_len = argp->ex_addrlen;
if (sacheck(saddr) == -1) {
error = EINVAL;
goto out;
}
if (argp->ex_masklen) {
smask = (struct sockaddr *)((char *)saddr + argp->ex_addrlen);
error = copyin(argp->ex_mask, smask, argp->ex_masklen);
if (error)
goto out;
if (smask->sa_len > argp->ex_masklen)
smask->sa_len = argp->ex_masklen;
if (smask->sa_family != saddr->sa_family) {
error = EINVAL;
goto out;
}
if (sacheck(smask) == -1) {
error = EINVAL;
goto out;
}
}
i = saddr->sa_family;
if ((rnh = nep->ne_rtable[i]) == 0) {
DOMAIN_FOREACH(dom) {
if (dom->dom_family == i && dom->dom_rtattach) {
rn_inithead((void **)&nep->ne_rtable[i],
dom->dom_rtoffset);
break;
}
}
if ((rnh = nep->ne_rtable[i]) == 0) {
error = ENOBUFS;
goto out;
}
}
enp = (struct netcred *)(*rnh->rnh_addaddr)(saddr, smask, rnh,
np->netc_rnodes);
if (enp != np) {
if (enp == NULL) {
enp = (struct netcred *)(*rnh->rnh_lookup)(saddr,
smask, rnh);
if (enp == NULL) {
error = EPERM;
goto out;
}
} else
enp->netc_refcnt++;
goto check;
} else
enp->netc_refcnt = 1;
np->netc_exflags = argp->ex_flags;
kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
return 0;
check:
if (enp->netc_exflags != argp->ex_flags ||
kauth_cred_uucmp(enp->netc_anon, &argp->ex_anon) != 0)
error = EPERM;
else
error = 0;
out:
KASSERT(np->netc_anon != NULL);
kauth_cred_free(np->netc_anon);
free(np, M_NETADDR);
return error;
}
static int
sacheck(struct sockaddr *sa)
{
switch (sa->sa_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
char *p = (char *)sin->sin_zero;
size_t i;
if (sin->sin_len != sizeof(*sin))
return -1;
if (sin->sin_port != 0)
return -1;
for (i = 0; i < sizeof(sin->sin_zero); i++)
if (*p++ != '\0')
return -1;
return 0;
}
case AF_INET6: {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
if (sin6->sin6_len != sizeof(*sin6))
return -1;
if (sin6->sin6_port != 0)
return -1;
return 0;
}
default:
return -1;
}
}
static int
free_netcred(struct radix_node *rn, void *w)
{
struct radix_node_head *rnh = (struct radix_node_head *)w;
struct netcred *np = (struct netcred *)(void *)rn;
(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
if (--(np->netc_refcnt) <= 0) {
KASSERT(np->netc_anon != NULL);
kauth_cred_free(np->netc_anon);
free(np, M_NETADDR);
}
return 0;
}
static void
netexport_clear(struct netexport *ne)
{
struct radix_node_head *rnh;
struct mount *mp = ne->ne_mount;
int i;
if (mp->mnt_flag & MNT_EXPUBLIC) {
setpublicfs(NULL, NULL, NULL);
mp->mnt_flag &= ~MNT_EXPUBLIC;
}
for (i = 0; i <= AF_MAX; i++) {
if ((rnh = ne->ne_rtable[i]) != NULL) {
rn_walktree(rnh, free_netcred, rnh);
free(rnh, M_RTABLE);
ne->ne_rtable[i] = NULL;
}
}
if ((mp->mnt_flag & MNT_DEFEXPORTED) != 0) {
struct netcred *np = &ne->ne_defexported;
KASSERT(np->netc_anon != NULL);
kauth_cred_free(np->netc_anon);
np->netc_anon = NULL;
} else {
KASSERT(ne->ne_defexported.netc_anon == NULL);
}
mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
}
static int
export(struct netexport *nep, const struct export_args *argp)
{
struct mount *mp = nep->ne_mount;
int error;
if (argp->ex_flags & MNT_EXPORTED) {
if (argp->ex_flags & MNT_EXPUBLIC) {
if ((error = setpublicfs(mp, nep, argp)) != 0)
return error;
mp->mnt_flag |= MNT_EXPUBLIC;
}
if ((error = hang_addrlist(mp, nep, argp)) != 0)
return error;
mp->mnt_flag |= MNT_EXPORTED;
}
return 0;
}
static int
setpublicfs(struct mount *mp, struct netexport *nep,
const struct export_args *argp)
{
char *cp;
int error;
struct vnode *rvp;
size_t fhsize;
if (mp == NULL) {
if (nfs_pub.np_valid) {
nfs_pub.np_valid = 0;
if (nfs_pub.np_handle != NULL) {
free(nfs_pub.np_handle, M_TEMP);
nfs_pub.np_handle = NULL;
}
if (nfs_pub.np_index != NULL) {
free(nfs_pub.np_index, M_TEMP);
nfs_pub.np_index = NULL;
}
}
return 0;
}
if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
return EBUSY;
if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
return error;
fhsize = 0;
error = vfs_composefh(rvp, NULL, &fhsize);
if (error != E2BIG)
return error;
nfs_pub.np_handle = malloc(fhsize, M_TEMP, M_NOWAIT);
if (nfs_pub.np_handle == NULL)
error = ENOMEM;
else
error = vfs_composefh(rvp, nfs_pub.np_handle, &fhsize);
if (error)
return error;
vput(rvp);
if (argp->ex_indexfile != NULL) {
nfs_pub.np_index = malloc(NFS_MAXNAMLEN + 1, M_TEMP, M_WAITOK);
error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
NFS_MAXNAMLEN, (size_t *)0);
if (!error) {
for (cp = nfs_pub.np_index; *cp; cp++) {
if (*cp == '/') {
error = EINVAL;
break;
}
}
}
if (error) {
free(nfs_pub.np_index, M_TEMP);
return error;
}
}
nfs_pub.np_mount = mp;
nfs_pub.np_valid = 1;
return 0;
}
static struct netcred *
netcred_lookup(struct netexport *ne, struct mbuf *nam)
{
struct netcred *np;
struct radix_node_head *rnh;
struct sockaddr *saddr;
if ((ne->ne_mount->mnt_flag & MNT_EXPORTED) == 0) {
return NULL;
}
np = NULL;
if (nam != NULL) {
saddr = mtod(nam, struct sockaddr *);
rnh = ne->ne_rtable[saddr->sa_family];
if (rnh != NULL) {
np = (struct netcred *)
(*rnh->rnh_matchaddr)((void *)saddr,
rnh);
if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
np = NULL;
}
}
if (np == NULL && ne->ne_mount->mnt_flag & MNT_DEFEXPORTED)
np = &ne->ne_defexported;
return np;
}
void
netexport_rdlock(void)
{
rw_enter(&netexport_lock, RW_READER);
}
void
netexport_rdunlock(void)
{
rw_exit(&netexport_lock);
}
static void
netexport_wrlock(void)
{
rw_enter(&netexport_lock, RW_WRITER);
}
static void
netexport_wrunlock(void)
{
rw_exit(&netexport_lock);
}
bool
netexport_hasexports(void)
{
return nfs_pub.np_valid || !TAILQ_EMPTY(&netexport_list);
}