1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_inet6.h"
39 #include "opt_kgssapi.h"
40
41 #include <sys/param.h>
42 #include <sys/capability.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/file.h>
48 #include <sys/filedesc.h>
49 #include <sys/jail.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/bio.h>
56 #include <sys/buf.h>
57 #include <sys/mbuf.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/domain.h>
61 #include <sys/protosw.h>
62 #include <sys/namei.h>
63 #include <sys/fcntl.h>
64 #include <sys/lockf.h>
65 #include <sys/eventhandler.h>
66
67 #include <netinet/in.h>
68 #include <netinet/tcp.h>
69 #ifdef INET6
70 #include <net/if.h>
71 #include <netinet6/in6_var.h>
72 #endif
73
74 #include <rpc/rpc.h>
75 #include <rpc/rpcsec_gss.h>
76 #include <rpc/replay.h>
77
78 #include <nfs/xdr_subs.h>
79 #include <nfs/nfsproto.h>
80 #include <nfs/nfs_fha.h>
81 #include <nfsserver/nfs.h>
82 #include <nfsserver/nfsm_subs.h>
83 #include <nfsserver/nfsrvcache.h>
84 #include <nfsserver/nfs_fha_old.h>
85
86 #include <security/mac/mac_framework.h>
87
88 static MALLOC_DEFINE(M_NFSSVC, "nfss_srvsock", "Nfs server structure");
89
90 MALLOC_DEFINE(M_NFSRVDESC, "nfss_srvdesc", "NFS server socket descriptor");
91 MALLOC_DEFINE(M_NFSD, "nfss_daemon", "Nfs server daemon structure");
92
93 #define TRUE 1
94 #define FALSE 0
95
96 SYSCTL_DECL(_vfs_nfsrv);
97
98 SVCPOOL *nfsrv_pool;
99 int nfsd_waiting = 0;
100 int nfsrv_numnfsd = 0;
101 struct callout nfsrv_callout;
102 static eventhandler_tag nfsrv_nmbclusters_tag;
103
104 static int nfs_privport = 0;
105 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
106 &nfs_privport, 0,
107 "Only allow clients using a privileged port");
108 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
109 &nfsrvw_procrastinate, 0,
110 "Delay value for write gathering");
111 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
112 &nfsrvw_procrastinate_v3, 0,
113 "Delay in seconds for NFSv3 write gathering");
114
115 static int nfssvc_addsock(struct file *, struct thread *);
116 static int nfssvc_nfsd(struct thread *, struct nfsd_nfsd_args *);
117
118 extern u_long sb_max_adj;
119
120 int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd,
121 struct nfssvc_sock *slp, struct mbuf **mreqp) = {
122 nfsrv_null,
123 nfsrv_getattr,
124 nfsrv_setattr,
125 nfsrv_lookup,
126 nfsrv3_access,
127 nfsrv_readlink,
128 nfsrv_read,
129 nfsrv_write,
130 nfsrv_create,
131 nfsrv_mkdir,
132 nfsrv_symlink,
133 nfsrv_mknod,
134 nfsrv_remove,
135 nfsrv_rmdir,
136 nfsrv_rename,
137 nfsrv_link,
138 nfsrv_readdir,
139 nfsrv_readdirplus,
140 nfsrv_statfs,
141 nfsrv_fsinfo,
142 nfsrv_pathconf,
143 nfsrv_commit,
144 nfsrv_noop
145 };
146
147 /*
148 * NFS server system calls
149 */
150 /*
151 * This is now called from nfssvc() in nfs/nfs_nfssvc.c.
152 */
153
154 /*
155 * Nfs server psuedo system call for the nfsd's
156 * Based on the flag value it either:
157 * - adds a socket to the selection list
158 * - remains in the kernel as an nfsd
159 * - remains in the kernel as an nfsiod
160 * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
161 * and that mountd provides
162 * - sockaddr with no IPv4-mapped addresses
163 * - mask for both INET and INET6 families if there is IPv4-mapped overlap
164 */
165 int
166 nfssvc_nfsserver(struct thread *td, struct nfssvc_args *uap)
167 {
168 struct file *fp;
169 struct nfsd_addsock_args addsockarg;
170 struct nfsd_nfsd_args nfsdarg;
171 int error;
172
173 if (uap->flag & NFSSVC_ADDSOCK) {
174 error = copyin(uap->argp, (caddr_t)&addsockarg,
175 sizeof(addsockarg));
176 if (error)
177 return (error);
178 if ((error = fget(td, addsockarg.sock, CAP_SOCK_ALL, &fp)) != 0)
179 return (error);
180 if (fp->f_type != DTYPE_SOCKET) {
181 fdrop(fp, td);
182 return (error); /* XXXRW: Should be EINVAL? */
183 }
184 error = nfssvc_addsock(fp, td);
185 fdrop(fp, td);
186 } else if (uap->flag & NFSSVC_OLDNFSD)
187 error = nfssvc_nfsd(td, NULL);
188 else if (uap->flag & NFSSVC_NFSD) {
189 if (!uap->argp)
190 return (EINVAL);
191 error = copyin(uap->argp, (caddr_t)&nfsdarg,
192 sizeof(nfsdarg));
193 if (error)
194 return (error);
195 error = nfssvc_nfsd(td, &nfsdarg);
196 } else
197 error = ENXIO;
198 return (error);
199 }
200
201 /*
202 * Generate the rpc reply header
203 * siz arg. is used to decide if adding a cluster is worthwhile
204 */
205 struct mbuf *
206 nfs_rephead(int siz, struct nfsrv_descript *nd, int err,
207 struct mbuf **mbp, caddr_t *bposp)
208 {
209 u_int32_t *tl;
210 struct mbuf *mreq;
211 caddr_t bpos;
212 struct mbuf *mb;
213
214 if (err == EBADRPC)
215 return (NULL);
216
217 nd->nd_repstat = err;
218 if (err && (nd->nd_flag & ND_NFSV3) == 0) /* XXX recheck */
219 siz = 0;
220
221 MGET(mreq, M_WAIT, MT_DATA);
222
223 /*
224 * If this is a big reply, use a cluster
225 */
226 mreq->m_len = 0;
227 if (siz >= MINCLSIZE) {
228 MCLGET(mreq, M_WAIT);
229 }
230 mb = mreq;
231 bpos = mtod(mb, caddr_t);
232
233 if (err != NFSERR_RETVOID) {
234 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
235 if (err)
236 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
237 else
238 *tl = 0;
239 }
240
241 *mbp = mb;
242 *bposp = bpos;
243 if (err != 0 && err != NFSERR_RETVOID)
244 nfsrvstats.srvrpc_errs++;
245
246 return (mreq);
247 }
248
249 static void
250 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
251 {
252 rpcproc_t procnum;
253 int32_t (*proc)(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
254 struct mbuf **mreqp);
255 int flag;
256 struct nfsrv_descript nd;
257 struct mbuf *mreq, *mrep;
258 int error;
259
260 if (rqst->rq_vers == NFS_VER2) {
261 if (rqst->rq_proc > NFSV2PROC_STATFS) {
262 svcerr_noproc(rqst);
263 svc_freereq(rqst);
264 return;
265 }
266 procnum = nfsrv_nfsv3_procid[rqst->rq_proc];
267 flag = 0;
268 } else {
269 if (rqst->rq_proc >= NFS_NPROCS) {
270 svcerr_noproc(rqst);
271 svc_freereq(rqst);
272 return;
273 }
274 procnum = rqst->rq_proc;
275 flag = ND_NFSV3;
276 }
277 proc = nfsrv3_procs[procnum];
278
279 mreq = mrep = NULL;
280 mreq = rqst->rq_args;
281 rqst->rq_args = NULL;
282 (void)nfs_realign(&mreq, M_WAIT);
283
284 /*
285 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
286 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
287 * mounts.
288 */
289 memset(&nd, 0, sizeof(nd));
290 nd.nd_md = nd.nd_mrep = mreq;
291 nd.nd_dpos = mtod(mreq, caddr_t);
292 nd.nd_nam = svc_getrpccaller(rqst);
293 nd.nd_nam2 = rqst->rq_addr;
294 nd.nd_procnum = procnum;
295 nd.nd_cr = NULL;
296 nd.nd_flag = flag;
297
298 if (nfs_privport) {
299 /* Check if source port is privileged */
300 u_short port;
301 struct sockaddr *nam = nd.nd_nam;
302 struct sockaddr_in *sin;
303
304 sin = (struct sockaddr_in *)nam;
305 /*
306 * INET/INET6 - same code:
307 * sin_port and sin6_port are at same offset
308 */
309 port = ntohs(sin->sin_port);
310 if (port >= IPPORT_RESERVED &&
311 nd.nd_procnum != NFSPROC_NULL) {
312 #ifdef INET6
313 char b6[INET6_ADDRSTRLEN];
314 #if defined(KLD_MODULE)
315 /* Do not use ip6_sprintf: the nfs module should work without INET6. */
316 #define ip6_sprintf(buf, a) \
317 (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
318 (a)->s6_addr16[0], (a)->s6_addr16[1], \
319 (a)->s6_addr16[2], (a)->s6_addr16[3], \
320 (a)->s6_addr16[4], (a)->s6_addr16[5], \
321 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
322 (buf))
323 #endif
324 #endif
325 printf("NFS request from unprivileged port (%s:%d)\n",
326 #ifdef INET6
327 sin->sin_family == AF_INET6 ?
328 ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
329 #if defined(KLD_MODULE)
330 #undef ip6_sprintf
331 #endif
332 #endif
333 inet_ntoa(sin->sin_addr), port);
334 m_freem(mreq);
335 svcerr_weakauth(rqst);
336 svc_freereq(rqst);
337 return;
338 }
339 }
340
341 if (proc != nfsrv_null) {
342 if (!svc_getcred(rqst, &nd.nd_cr, &nd.nd_credflavor)) {
343 m_freem(mreq);
344 svcerr_weakauth(rqst);
345 svc_freereq(rqst);
346 return;
347 }
348 #ifdef MAC
349 mac_cred_associate_nfsd(nd.nd_cr);
350 #endif
351 }
352 nfsrvstats.srvrpccnt[nd.nd_procnum]++;
353
354 error = proc(&nd, NULL, &mrep);
355
356 if (nd.nd_cr)
357 crfree(nd.nd_cr);
358
359 if (mrep == NULL) {
360 svcerr_decode(rqst);
361 svc_freereq(rqst);
362 return;
363 }
364 if (error && error != NFSERR_RETVOID) {
365 svcerr_systemerr(rqst);
366 svc_freereq(rqst);
367 return;
368 }
369 if (nd.nd_repstat & NFSERR_AUTHERR) {
370 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
371 m_freem(mrep);
372 } else {
373 if (!svc_sendreply_mbuf(rqst, mrep))
374 svcerr_systemerr(rqst);
375 }
376 svc_freereq(rqst);
377 }
378
379 /*
380 * Adds a socket to the list for servicing by nfsds.
381 */
382 static int
383 nfssvc_addsock(struct file *fp, struct thread *td)
384 {
385 int siz;
386 struct socket *so;
387 int error;
388 SVCXPRT *xprt;
389
390 so = fp->f_data;
391
392 siz = sb_max_adj;
393 error = soreserve(so, siz, siz);
394 if (error)
395 return (error);
396
397 /*
398 * Steal the socket from userland so that it doesn't close
399 * unexpectedly.
400 */
401 if (so->so_type == SOCK_DGRAM)
402 xprt = svc_dg_create(nfsrv_pool, so, 0, 0);
403 else
404 xprt = svc_vc_create(nfsrv_pool, so, 0, 0);
405 if (xprt) {
406 fp->f_ops = &badfileops;
407 fp->f_data = NULL;
408 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, NULL);
409 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, NULL);
410 SVC_RELEASE(xprt);
411 }
412
413 return (0);
414 }
415
416 /*
417 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
418 * until it is killed by a signal.
419 */
420 static int
421 nfssvc_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
422 {
423 char principal[128];
424 int error;
425
426 if (args) {
427 error = copyinstr(args->principal, principal,
428 sizeof(principal), NULL);
429 if (error)
430 return (error);
431 } else {
432 memcpy(principal, "nfs@", 4);
433 getcredhostname(td->td_ucred, principal + 4,
434 sizeof(principal) - 4);
435 }
436
437 /*
438 * Only the first nfsd actually does any work. The RPC code
439 * adds threads to it as needed. Any extra processes offered
440 * by nfsd just exit. If nfsd is new enough, it will call us
441 * once with a structure that specifies how many threads to
442 * use.
443 */
444 NFSD_LOCK();
445 if (nfsrv_numnfsd == 0) {
446 nfsrv_numnfsd++;
447
448 NFSD_UNLOCK();
449
450 rpc_gss_set_svc_name_call(principal, "kerberosv5",
451 GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
452 rpc_gss_set_svc_name_call(principal, "kerberosv5",
453 GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
454
455 if (args) {
456 nfsrv_pool->sp_minthreads = args->minthreads;
457 nfsrv_pool->sp_maxthreads = args->maxthreads;
458 } else {
459 nfsrv_pool->sp_minthreads = 4;
460 nfsrv_pool->sp_maxthreads = 4;
461 }
462
463 svc_run(nfsrv_pool);
464
465 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
466 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
467
468 NFSD_LOCK();
469 nfsrv_numnfsd--;
470 nfsrv_init(TRUE);
471 }
472 NFSD_UNLOCK();
473
474 return (0);
475 }
476
477 /*
478 * Size the NFS server's duplicate request cache at 1/2 the
479 * nmbclusters, floating within a (64, 2048) range. This is to
480 * prevent all mbuf clusters being tied up in the NFS dupreq
481 * cache for small values of nmbclusters.
482 */
483 static size_t
484 nfsrv_replay_size(void)
485 {
486 size_t replaysiz;
487
488 replaysiz = nmbclusters / 2;
489 if (replaysiz > NFSRVCACHE_MAX_SIZE)
490 replaysiz = NFSRVCACHE_MAX_SIZE;
491 if (replaysiz < NFSRVCACHE_MIN_SIZE)
492 replaysiz = NFSRVCACHE_MIN_SIZE;
493 replaysiz *= MCLBYTES;
494
495 return (replaysiz);
496 }
497
498 /*
499 * Called when nmbclusters changes - we resize the replay cache
500 * accordingly.
501 */
502 static void
503 nfsrv_nmbclusters_change(void *tag)
504 {
505
506 if (nfsrv_pool)
507 replay_setsize(nfsrv_pool->sp_rcache, nfsrv_replay_size());
508 }
509
510 /*
511 * Initialize the data structures for the server.
512 * Handshake with any new nfsds starting up to avoid any chance of
513 * corruption.
514 */
515 void
516 nfsrv_init(int terminating)
517 {
518
519 NFSD_LOCK_ASSERT();
520
521 if (terminating) {
522 NFSD_UNLOCK();
523 EVENTHANDLER_DEREGISTER(nmbclusters_change,
524 nfsrv_nmbclusters_tag);
525 svcpool_destroy(nfsrv_pool);
526 nfsrv_pool = NULL;
527 NFSD_LOCK();
528 } else
529 nfs_pub.np_valid = 0;
530
531 NFSD_UNLOCK();
532
533 nfsrv_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsrv));
534 nfsrv_pool->sp_rcache = replay_newcache(nfsrv_replay_size());
535 nfsrv_pool->sp_assign = fhaold_assign;
536 nfsrv_pool->sp_done = fha_nd_complete;
537 nfsrv_nmbclusters_tag = EVENTHANDLER_REGISTER(nmbclusters_change,
538 nfsrv_nmbclusters_change, NULL, EVENTHANDLER_PRI_FIRST);
539
540 NFSD_LOCK();
541 }
Cache object: e92be8b38d17c519b735b32165553952
|