xref: /freebsd/sys/nfs/nfs_nfssvc.c (revision 942815c54820783d3d4f7f6faa71ab7919b5f0e5)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_nfs.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysproto.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/module.h>
51 #include <sys/sysent.h>
52 #include <sys/syscall.h>
53 #include <sys/sysproto.h>
54 
55 #include <security/audit/audit.h>
56 
57 #include <nfs/nfssvc.h>
58 
59 static struct syscall_helper_data nfssvc_syscalls[] = {
60 	SYSCALL_INIT_HELPER(nfssvc),
61 	SYSCALL_INIT_LAST
62 };
63 
64 /*
65  * This tiny module simply handles the nfssvc() system call. The other
66  * nfs modules that use the system call register themselves by setting
67  * the nfsd_call_xxx function pointers non-NULL.
68  */
69 
70 int (*nfsd_call_nfsserver)(struct thread *, struct nfssvc_args *) = NULL;
71 int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *) = NULL;
72 int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *) = NULL;
73 int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *) = NULL;
74 
75 /*
76  * Nfs server pseudo system call for the nfsd's
77  */
78 int
79 sys_nfssvc(struct thread *td, struct nfssvc_args *uap)
80 {
81 	int error;
82 
83 	KASSERT(!mtx_owned(&Giant), ("nfssvc(): called with Giant"));
84 
85 	AUDIT_ARG_CMD(uap->flag);
86 
87 	/* Allow anyone to get the stats. */
88 	if ((uap->flag & ~(NFSSVC_GETSTATS | NFSSVC_NEWSTRUCT)) != 0) {
89 		error = priv_check(td, PRIV_NFS_DAEMON);
90 		if (error != 0)
91 			return (error);
92 	}
93 	error = EINVAL;
94 	if ((uap->flag & (NFSSVC_ADDSOCK | NFSSVC_OLDNFSD | NFSSVC_NFSD)) &&
95 	    nfsd_call_nfsserver != NULL)
96 		error = (*nfsd_call_nfsserver)(td, uap);
97 	else if ((uap->flag & (NFSSVC_CBADDSOCK | NFSSVC_NFSCBD |
98 	    NFSSVC_DUMPMNTOPTS | NFSSVC_FORCEDISM)) && nfsd_call_nfscl != NULL)
99 		error = (*nfsd_call_nfscl)(td, uap);
100 	else if ((uap->flag & (NFSSVC_IDNAME | NFSSVC_GETSTATS |
101 	    NFSSVC_GSSDADDPORT | NFSSVC_GSSDADDFIRST | NFSSVC_GSSDDELETEALL |
102 	    NFSSVC_NFSUSERDPORT | NFSSVC_NFSUSERDDELPORT)) &&
103 	    nfsd_call_nfscommon != NULL)
104 		error = (*nfsd_call_nfscommon)(td, uap);
105 	else if ((uap->flag & (NFSSVC_NFSDNFSD | NFSSVC_NFSDADDSOCK |
106 	    NFSSVC_PUBLICFH | NFSSVC_V4ROOTEXPORT | NFSSVC_NOPUBLICFH |
107 	    NFSSVC_STABLERESTART | NFSSVC_ADMINREVOKE |
108 	    NFSSVC_DUMPCLIENTS | NFSSVC_DUMPLOCKS | NFSSVC_BACKUPSTABLE |
109 	    NFSSVC_SUSPENDNFSD | NFSSVC_RESUMENFSD | NFSSVC_PNFSDS)) &&
110 	    nfsd_call_nfsd != NULL)
111 		error = (*nfsd_call_nfsd)(td, uap);
112 	if (error == EINTR || error == ERESTART)
113 		error = 0;
114 	return (error);
115 }
116 
117 /*
118  * Called once to initialize data structures...
119  */
120 static int
121 nfssvc_modevent(module_t mod, int type, void *data)
122 {
123 	int error = 0;
124 
125 	switch (type) {
126 	case MOD_LOAD:
127 		error = syscall_helper_register(nfssvc_syscalls,
128 		    SY_THR_STATIC_KLD);
129 		break;
130 
131 	case MOD_UNLOAD:
132 		if (nfsd_call_nfsserver != NULL || nfsd_call_nfscommon != NULL
133 		    || nfsd_call_nfscl != NULL || nfsd_call_nfsd != NULL) {
134 			error = EBUSY;
135 			break;
136 		}
137 		syscall_helper_unregister(nfssvc_syscalls);
138 		break;
139 	default:
140 		error = EOPNOTSUPP;
141 		break;
142 	}
143 	return error;
144 }
145 static moduledata_t nfssvc_mod = {
146 	"nfssvc",
147 	nfssvc_modevent,
148 	NULL,
149 };
150 DECLARE_MODULE(nfssvc, nfssvc_mod, SI_SUB_VFS, SI_ORDER_ANY);
151 
152 /* So that loader and kldload(2) can find us, wherever we are.. */
153 MODULE_VERSION(nfssvc, 1);
154