1*88b1d6a6Spgoyette /* $NetBSD: nfs_nfssvc.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $ */
26ca35587Sdholland /*-
36ca35587Sdholland * Copyright (c) 1989, 1993
46ca35587Sdholland * The Regents of the University of California. All rights reserved.
56ca35587Sdholland *
66ca35587Sdholland * This code is derived from software contributed to Berkeley by
76ca35587Sdholland * Rick Macklem at The University of Guelph.
86ca35587Sdholland *
96ca35587Sdholland * Redistribution and use in source and binary forms, with or without
106ca35587Sdholland * modification, are permitted provided that the following conditions
116ca35587Sdholland * are met:
126ca35587Sdholland * 1. Redistributions of source code must retain the above copyright
136ca35587Sdholland * notice, this list of conditions and the following disclaimer.
146ca35587Sdholland * 2. Redistributions in binary form must reproduce the above copyright
156ca35587Sdholland * notice, this list of conditions and the following disclaimer in the
166ca35587Sdholland * documentation and/or other materials provided with the distribution.
176ca35587Sdholland * 4. Neither the name of the University nor the names of its contributors
186ca35587Sdholland * may be used to endorse or promote products derived from this software
196ca35587Sdholland * without specific prior written permission.
206ca35587Sdholland *
216ca35587Sdholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
226ca35587Sdholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236ca35587Sdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246ca35587Sdholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
256ca35587Sdholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266ca35587Sdholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
276ca35587Sdholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
286ca35587Sdholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
296ca35587Sdholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
306ca35587Sdholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316ca35587Sdholland * SUCH DAMAGE.
326ca35587Sdholland *
336ca35587Sdholland */
346ca35587Sdholland
356ca35587Sdholland #include <sys/cdefs.h>
36e81f0ea2Spgoyette /* __FBSDID("FreeBSD: head/sys/nfs/nfs_nfssvc.c 298788 2016-04-29 16:07:25Z pfg "); */
37*88b1d6a6Spgoyette __RCSID("$NetBSD: nfs_nfssvc.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $");
386ca35587Sdholland
39*88b1d6a6Spgoyette #ifdef _KERNEL_OPT
40*88b1d6a6Spgoyette #include "opt_newnfs.h"
41*88b1d6a6Spgoyette #endif
426ca35587Sdholland
436ca35587Sdholland #include <sys/param.h>
446ca35587Sdholland #include <sys/systm.h>
456ca35587Sdholland #include <sys/sysproto.h>
466ca35587Sdholland #include <sys/kernel.h>
476ca35587Sdholland #include <sys/sysctl.h>
486ca35587Sdholland #include <sys/priv.h>
496ca35587Sdholland #include <sys/proc.h>
506ca35587Sdholland #include <sys/lock.h>
516ca35587Sdholland #include <sys/mutex.h>
526ca35587Sdholland #include <sys/module.h>
536ca35587Sdholland #include <sys/sysent.h>
546ca35587Sdholland #include <sys/syscall.h>
556ca35587Sdholland
566ca35587Sdholland #include <security/audit/audit.h>
576ca35587Sdholland
58*88b1d6a6Spgoyette #include <fs/nfs/common/nfssvc.h>
596ca35587Sdholland
606ca35587Sdholland static int nfssvc_offset = SYS_nfssvc;
616ca35587Sdholland static struct sysent nfssvc_prev_sysent;
626ca35587Sdholland MAKE_SYSENT(nfssvc);
636ca35587Sdholland
646ca35587Sdholland /*
656ca35587Sdholland * This tiny module simply handles the nfssvc() system call. The other
666ca35587Sdholland * nfs modules that use the system call register themselves by setting
676ca35587Sdholland * the nfsd_call_xxx function pointers non-NULL.
686ca35587Sdholland */
696ca35587Sdholland
706ca35587Sdholland int (*nfsd_call_nfsserver)(struct thread *, struct nfssvc_args *) = NULL;
716ca35587Sdholland int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *) = NULL;
726ca35587Sdholland int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *) = NULL;
736ca35587Sdholland int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *) = NULL;
746ca35587Sdholland
756ca35587Sdholland /*
76e81f0ea2Spgoyette * Nfs server pseudo system call for the nfsd's
776ca35587Sdholland */
786ca35587Sdholland int
sys_nfssvc(struct thread * td,struct nfssvc_args * uap)796ca35587Sdholland sys_nfssvc(struct thread *td, struct nfssvc_args *uap)
806ca35587Sdholland {
816ca35587Sdholland int error;
826ca35587Sdholland
836ca35587Sdholland KASSERT(!mtx_owned(&Giant), ("nfssvc(): called with Giant"));
846ca35587Sdholland
856ca35587Sdholland AUDIT_ARG_CMD(uap->flag);
866ca35587Sdholland
876ca35587Sdholland /* Allow anyone to get the stats. */
886ca35587Sdholland if ((uap->flag & ~NFSSVC_GETSTATS) != 0) {
896ca35587Sdholland error = priv_check(td, PRIV_NFS_DAEMON);
906ca35587Sdholland if (error != 0)
916ca35587Sdholland return (error);
926ca35587Sdholland }
936ca35587Sdholland error = EINVAL;
946ca35587Sdholland if ((uap->flag & (NFSSVC_ADDSOCK | NFSSVC_OLDNFSD | NFSSVC_NFSD)) &&
956ca35587Sdholland nfsd_call_nfsserver != NULL)
966ca35587Sdholland error = (*nfsd_call_nfsserver)(td, uap);
976ca35587Sdholland else if ((uap->flag & (NFSSVC_CBADDSOCK | NFSSVC_NFSCBD |
986ca35587Sdholland NFSSVC_DUMPMNTOPTS)) && nfsd_call_nfscl != NULL)
996ca35587Sdholland error = (*nfsd_call_nfscl)(td, uap);
1006ca35587Sdholland else if ((uap->flag & (NFSSVC_IDNAME | NFSSVC_GETSTATS |
1016ca35587Sdholland NFSSVC_GSSDADDPORT | NFSSVC_GSSDADDFIRST | NFSSVC_GSSDDELETEALL |
1026ca35587Sdholland NFSSVC_NFSUSERDPORT | NFSSVC_NFSUSERDDELPORT)) &&
1036ca35587Sdholland nfsd_call_nfscommon != NULL)
1046ca35587Sdholland error = (*nfsd_call_nfscommon)(td, uap);
1056ca35587Sdholland else if ((uap->flag & (NFSSVC_NFSDNFSD | NFSSVC_NFSDADDSOCK |
1066ca35587Sdholland NFSSVC_PUBLICFH | NFSSVC_V4ROOTEXPORT | NFSSVC_NOPUBLICFH |
1076ca35587Sdholland NFSSVC_STABLERESTART | NFSSVC_ADMINREVOKE |
1086ca35587Sdholland NFSSVC_DUMPCLIENTS | NFSSVC_DUMPLOCKS | NFSSVC_BACKUPSTABLE |
1096ca35587Sdholland NFSSVC_SUSPENDNFSD | NFSSVC_RESUMENFSD)) &&
1106ca35587Sdholland nfsd_call_nfsd != NULL)
1116ca35587Sdholland error = (*nfsd_call_nfsd)(td, uap);
1126ca35587Sdholland if (error == EINTR || error == ERESTART)
1136ca35587Sdholland error = 0;
1146ca35587Sdholland return (error);
1156ca35587Sdholland }
1166ca35587Sdholland
1176ca35587Sdholland /*
1186ca35587Sdholland * Called once to initialize data structures...
1196ca35587Sdholland */
1206ca35587Sdholland static int
nfssvc_modevent(module_t mod,int type,void * data)1216ca35587Sdholland nfssvc_modevent(module_t mod, int type, void *data)
1226ca35587Sdholland {
1236ca35587Sdholland static int registered;
1246ca35587Sdholland int error = 0;
1256ca35587Sdholland
1266ca35587Sdholland switch (type) {
1276ca35587Sdholland case MOD_LOAD:
1286ca35587Sdholland error = syscall_register(&nfssvc_offset, &nfssvc_sysent,
129e81f0ea2Spgoyette &nfssvc_prev_sysent, SY_THR_STATIC_KLD);
1306ca35587Sdholland if (error)
1316ca35587Sdholland break;
1326ca35587Sdholland registered = 1;
1336ca35587Sdholland break;
1346ca35587Sdholland
1356ca35587Sdholland case MOD_UNLOAD:
1366ca35587Sdholland if (nfsd_call_nfsserver != NULL || nfsd_call_nfscommon != NULL
1376ca35587Sdholland || nfsd_call_nfscl != NULL || nfsd_call_nfsd != NULL) {
1386ca35587Sdholland error = EBUSY;
1396ca35587Sdholland break;
1406ca35587Sdholland }
1416ca35587Sdholland if (registered)
1426ca35587Sdholland syscall_deregister(&nfssvc_offset, &nfssvc_prev_sysent);
1436ca35587Sdholland registered = 0;
1446ca35587Sdholland break;
1456ca35587Sdholland default:
1466ca35587Sdholland error = EOPNOTSUPP;
1476ca35587Sdholland break;
1486ca35587Sdholland }
1496ca35587Sdholland return error;
1506ca35587Sdholland }
1516ca35587Sdholland static moduledata_t nfssvc_mod = {
1526ca35587Sdholland "nfssvc",
1536ca35587Sdholland nfssvc_modevent,
1546ca35587Sdholland NULL,
1556ca35587Sdholland };
1566ca35587Sdholland DECLARE_MODULE(nfssvc, nfssvc_mod, SI_SUB_VFS, SI_ORDER_ANY);
1576ca35587Sdholland
1586ca35587Sdholland /* So that loader and kldload(2) can find us, wherever we are.. */
1596ca35587Sdholland MODULE_VERSION(nfssvc, 1);
1606ca35587Sdholland
161