1 /*	$NetBSD: netbsd32_nfssvc.c,v 1.9 2021/12/11 19:24:21 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 2015 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_nfssvc.c,v 1.9 2021/12/11 19:24:21 mrg Exp $");
31 
32 #if defined(_KERNEL_OPT)
33 #include "opt_nfs.h"
34 #include "opt_nfsserver.h"
35 #include "opt_compat_netbsd.h"
36 #endif
37 
38 #include <sys/param.h>
39 #include <sys/vnode.h>
40 #include <sys/filedesc.h>
41 #include <sys/module.h>
42 #include <sys/syscallvar.h>
43 
44 #include <compat/netbsd32/netbsd32.h>
45 #include <compat/netbsd32/netbsd32_syscall.h>
46 #include <compat/netbsd32/netbsd32_syscallargs.h>
47 #include <compat/netbsd32/netbsd32_conv.h>
48 
49 #include <nfs/rpcv2.h>
50 #include <nfs/nfsproto.h>
51 #include <nfs/nfs.h>
52 #include <nfs/nfs_var.h>
53 
54 static int nfssvc32_addsock_in(struct nfsd_args *, const void *);
55 static int nfssvc32_setexports_in(struct mountd_exports_list *, const void *);
56 static int nfssvc32_nsd_in(struct nfsd_srvargs *, const void *);
57 static int nfssvc32_nsd_out(void *, const struct nfsd_srvargs *);
58 static int nfssvc32_exp_in(struct export_args *, const void *, size_t);
59 
60 static int
nfssvc32_addsock_in(struct nfsd_args * nfsdarg,const void * argp)61 nfssvc32_addsock_in(struct nfsd_args *nfsdarg, const void *argp)
62 {
63 	struct netbsd32_nfsd_args args32;
64 	int error;
65 
66 	error = copyin(argp, &args32, sizeof args32);
67 	if (!error) {
68 		nfsdarg->sock = args32.sock;
69 		nfsdarg->name = NETBSD32PTR64(args32.name);
70 		nfsdarg->namelen = args32.namelen;
71 	}
72 
73 	return error;
74 }
75 
76 static int
nfssvc32_setexports_in(struct mountd_exports_list * mel,const void * argp)77 nfssvc32_setexports_in(struct mountd_exports_list *mel, const void *argp)
78 {
79 	struct netbsd32_mountd_exports_list args32;
80 	int error;
81 
82 	error = copyin(argp, &args32, sizeof args32);
83 	if (!error) {
84 		mel->mel_path = NETBSD32PTR64(args32.mel_path);
85 		mel->mel_nexports = args32.mel_nexports;
86 		mel->mel_exports = NETBSD32PTR64(args32.mel_exports);
87 	}
88 
89 	return error;
90 }
91 
92 static int
nfssvc32_nsd_in(struct nfsd_srvargs * nsd,const void * argp)93 nfssvc32_nsd_in(struct nfsd_srvargs *nsd, const void *argp)
94 {
95 	struct netbsd32_nfsd_srvargs args32;
96 	int error;
97 
98 	error = copyin(argp, &args32, sizeof args32);
99 	if (!error) {
100 		nsd->nsd_nfsd = NETBSD32PTR64(args32.nsd_nfsd);
101 		nsd->nsd_uid = args32.nsd_uid;
102 		nsd->nsd_haddr = args32.nsd_haddr;
103 		nsd->nsd_cr = args32.nsd_cr;
104 		nsd->nsd_authlen = args32.nsd_authlen;
105 		nsd->nsd_authstr = NETBSD32PTR64(args32.nsd_authstr);
106 		nsd->nsd_verflen = args32.nsd_verflen;
107 		nsd->nsd_uid = args32.nsd_uid;
108 		netbsd32_to_timeval(&args32.nsd_timestamp, &nsd->nsd_timestamp);
109 		nsd->nsd_ttl = args32.nsd_ttl;
110 		nsd->nsd_key[0] = args32.nsd_key[0];
111 		nsd->nsd_key[1] = args32.nsd_key[1];
112 	}
113 
114 	return error;
115 }
116 
117 static int
nfssvc32_nsd_out(void * argp,const struct nfsd_srvargs * nsd)118 nfssvc32_nsd_out(void *argp, const struct nfsd_srvargs *nsd)
119 {
120 	struct netbsd32_nfsd_srvargs args32;
121 
122 	memset(&args32, 0, sizeof(args32));
123 	NETBSD32PTR32(args32.nsd_nfsd, nsd->nsd_nfsd);
124 	args32.nsd_uid = nsd->nsd_uid;
125 	args32.nsd_haddr = nsd->nsd_haddr;
126 	args32.nsd_cr = nsd->nsd_cr;
127 	args32.nsd_authlen = nsd->nsd_authlen;
128 	NETBSD32PTR32(args32.nsd_authstr, nsd->nsd_authstr);
129 	args32.nsd_verflen = nsd->nsd_verflen;
130 	args32.nsd_uid = nsd->nsd_uid;
131 	netbsd32_from_timeval(&nsd->nsd_timestamp, &args32.nsd_timestamp);
132 	args32.nsd_ttl = nsd->nsd_ttl;
133 	args32.nsd_key[0] = nsd->nsd_key[0];
134 	args32.nsd_key[1] = nsd->nsd_key[1];
135 
136 	return copyout(&args32, argp, sizeof args32);
137 }
138 
139 static int
nfssvc32_exp_in(struct export_args * exp,const void * argp,size_t nexports)140 nfssvc32_exp_in(struct export_args *exp, const void *argp, size_t nexports)
141 {
142 	struct netbsd32_export_args exp32;
143 	struct netbsd32_export_args const *argp32 = argp;
144 	int error = 0;
145 
146 	for (size_t i = 0; i < nexports; i++) {
147 		error = copyin(argp32, &exp32, sizeof exp32);
148 		if (error)
149 			break;
150 		exp->ex_flags = exp32.ex_flags;
151 		exp->ex_root = exp32.ex_root;
152 		exp->ex_anon = exp32.ex_anon;
153 		exp->ex_addr = NETBSD32PTR64(exp32.ex_addr);
154 		exp->ex_addrlen = exp32.ex_addrlen;
155 		exp->ex_mask = NETBSD32PTR64(exp32.ex_mask);
156 		exp->ex_masklen = exp32.ex_masklen;
157 		exp->ex_indexfile = NETBSD32PTR64(exp32.ex_indexfile);
158 		exp++;
159 		argp32++;
160 	}
161 
162 	return error;
163 }
164 
165 /*
166  * NFS server system calls
167  */
168 
169 static struct nfssvc_copy_ops netbsd32_ops = {
170 	.addsock_in = nfssvc32_addsock_in,
171 	.setexports_in = nfssvc32_setexports_in,
172 	.nsd_in = nfssvc32_nsd_in,
173 	.nsd_out = nfssvc32_nsd_out,
174 	.exp_in = nfssvc32_exp_in,
175 };
176 
177 int
netbsd32_nfssvc(struct lwp * l,const struct netbsd32_nfssvc_args * uap,register_t * retval)178 netbsd32_nfssvc(struct lwp *l, const struct netbsd32_nfssvc_args *uap,
179     register_t *retval)
180 {
181 	/* {
182 		syscallarg(int) flag;
183 		syscallarg(netbsd32_voidp *) argp;
184 	} */
185 	int	flag = SCARG(uap, flag);
186 	void	*argp = SCARG_P32(uap, argp);
187 
188 	return do_nfssvc(&netbsd32_ops, l, flag, argp, retval);
189 }
190 
191 static const struct syscall_package compat_nfssvc_syscalls[] = {
192 	{ NETBSD32_SYS_netbsd32_nfssvc, 0, (sy_call_t *)netbsd32_nfssvc },
193 	{ 0, 0, NULL },
194 };
195 
196 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_nfssrv, "nfsserver,compat_netbsd32");
197 
198 static int
compat_netbsd32_nfssrv_modcmd(modcmd_t cmd,void * arg)199 compat_netbsd32_nfssrv_modcmd(modcmd_t cmd, void *arg)
200 {
201 	int error;
202 
203 	switch (cmd) {
204 	case MODULE_CMD_INIT:
205 		error = syscall_establish(&emul_netbsd32,
206 		    compat_nfssvc_syscalls);
207 		break;
208 	case MODULE_CMD_FINI:
209 		error = syscall_disestablish(&emul_netbsd32,
210 		    compat_nfssvc_syscalls);
211 		break;
212 	default:
213 		error = ENOTTY;
214 		break;
215 	}
216 	return error;
217 }
218