2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
39 #include "opt_kgssapi.h"
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>
48 #include <sys/filedesc.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.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>
67 #include <netinet/in.h>
68 #include <netinet/tcp.h>
71 #include <netinet6/in6_var.h>
75 #include <rpc/rpcsec_gss.h>
76 #include <rpc/replay.h>
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>
86 #include <security/mac/mac_framework.h>
88 static MALLOC_DEFINE(M_NFSSVC, "nfss_srvsock", "Nfs server structure");
90 MALLOC_DEFINE(M_NFSRVDESC, "nfss_srvdesc", "NFS server socket descriptor");
91 MALLOC_DEFINE(M_NFSD, "nfss_daemon", "Nfs server daemon structure");
96 SYSCTL_DECL(_vfs_nfsrv);
100 int nfsrv_numnfsd = 0;
101 struct callout nfsrv_callout;
102 static eventhandler_tag nfsrv_nmbclusters_tag;
104 static int nfs_privport = 0;
105 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
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");
115 static int nfssvc_addsock(struct file *, struct thread *);
116 static int nfssvc_nfsd(struct thread *, struct nfsd_nfsd_args *);
118 extern u_long sb_max_adj;
120 int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd,
121 struct nfssvc_sock *slp, struct mbuf **mreqp) = {
148 * NFS server system calls
151 * This is now called from nfssvc() in nfs/nfs_nfssvc.c.
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
166 nfssvc_nfsserver(struct thread *td, struct nfssvc_args *uap)
169 struct nfsd_addsock_args addsockarg;
170 struct nfsd_nfsd_args nfsdarg;
173 if (uap->flag & NFSSVC_ADDSOCK) {
174 error = copyin(uap->argp, (caddr_t)&addsockarg,
178 error = fget(td, addsockarg.sock, CAP_SOCK_SERVER, &fp);
181 if (fp->f_type != DTYPE_SOCKET) {
183 return (error); /* XXXRW: Should be EINVAL? */
185 error = nfssvc_addsock(fp, td);
187 } else if (uap->flag & NFSSVC_OLDNFSD)
188 error = nfssvc_nfsd(td, NULL);
189 else if (uap->flag & NFSSVC_NFSD) {
192 error = copyin(uap->argp, (caddr_t)&nfsdarg,
196 error = nfssvc_nfsd(td, &nfsdarg);
203 * Generate the rpc reply header
204 * siz arg. is used to decide if adding a cluster is worthwhile
207 nfs_rephead(int siz, struct nfsrv_descript *nd, int err,
208 struct mbuf **mbp, caddr_t *bposp)
218 nd->nd_repstat = err;
219 if (err && (nd->nd_flag & ND_NFSV3) == 0) /* XXX recheck */
222 MGET(mreq, M_WAITOK, MT_DATA);
225 * If this is a big reply, use a cluster
228 if (siz >= MINCLSIZE) {
229 MCLGET(mreq, M_WAITOK);
232 bpos = mtod(mb, caddr_t);
234 if (err != NFSERR_RETVOID) {
235 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
237 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
244 if (err != 0 && err != NFSERR_RETVOID)
245 nfsrvstats.srvrpc_errs++;
251 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
254 int32_t (*proc)(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
255 struct mbuf **mreqp);
257 struct nfsrv_descript nd;
258 struct mbuf *mreq, *mrep;
261 if (rqst->rq_vers == NFS_VER2) {
262 if (rqst->rq_proc > NFSV2PROC_STATFS) {
267 procnum = nfsrv_nfsv3_procid[rqst->rq_proc];
270 if (rqst->rq_proc >= NFS_NPROCS) {
275 procnum = rqst->rq_proc;
278 proc = nfsrv3_procs[procnum];
281 mreq = rqst->rq_args;
282 rqst->rq_args = NULL;
283 (void)nfs_realign(&mreq, M_WAITOK);
286 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
287 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
290 memset(&nd, 0, sizeof(nd));
291 nd.nd_md = nd.nd_mrep = mreq;
292 nd.nd_dpos = mtod(mreq, caddr_t);
293 nd.nd_nam = svc_getrpccaller(rqst);
294 nd.nd_nam2 = rqst->rq_addr;
295 nd.nd_procnum = procnum;
300 /* Check if source port is privileged */
302 struct sockaddr *nam = nd.nd_nam;
303 struct sockaddr_in *sin;
305 sin = (struct sockaddr_in *)nam;
307 * INET/INET6 - same code:
308 * sin_port and sin6_port are at same offset
310 port = ntohs(sin->sin_port);
311 if (port >= IPPORT_RESERVED &&
312 nd.nd_procnum != NFSPROC_NULL) {
314 char b6[INET6_ADDRSTRLEN];
315 #if defined(KLD_MODULE)
316 /* Do not use ip6_sprintf: the nfs module should work without INET6. */
317 #define ip6_sprintf(buf, a) \
318 (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
319 (a)->s6_addr16[0], (a)->s6_addr16[1], \
320 (a)->s6_addr16[2], (a)->s6_addr16[3], \
321 (a)->s6_addr16[4], (a)->s6_addr16[5], \
322 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
326 printf("NFS request from unprivileged port (%s:%d)\n",
328 sin->sin_family == AF_INET6 ?
329 ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
330 #if defined(KLD_MODULE)
334 inet_ntoa(sin->sin_addr), port);
336 svcerr_weakauth(rqst);
342 if (proc != nfsrv_null) {
343 if (!svc_getcred(rqst, &nd.nd_cr, &nd.nd_credflavor)) {
345 svcerr_weakauth(rqst);
350 mac_cred_associate_nfsd(nd.nd_cr);
353 nfsrvstats.srvrpccnt[nd.nd_procnum]++;
355 error = proc(&nd, NULL, &mrep);
365 if (error && error != NFSERR_RETVOID) {
366 svcerr_systemerr(rqst);
370 if (nd.nd_repstat & NFSERR_AUTHERR) {
371 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
374 if (!svc_sendreply_mbuf(rqst, mrep))
375 svcerr_systemerr(rqst);
381 * Adds a socket to the list for servicing by nfsds.
384 nfssvc_addsock(struct file *fp, struct thread *td)
394 error = soreserve(so, siz, siz);
399 * Steal the socket from userland so that it doesn't close
402 if (so->so_type == SOCK_DGRAM)
403 xprt = svc_dg_create(nfsrv_pool, so, 0, 0);
405 xprt = svc_vc_create(nfsrv_pool, so, 0, 0);
407 fp->f_ops = &badfileops;
409 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, NULL);
410 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, NULL);
418 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
419 * until it is killed by a signal.
422 nfssvc_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
428 error = copyinstr(args->principal, principal,
429 sizeof(principal), NULL);
433 memcpy(principal, "nfs@", 4);
434 getcredhostname(td->td_ucred, principal + 4,
435 sizeof(principal) - 4);
439 * Only the first nfsd actually does any work. The RPC code
440 * adds threads to it as needed. Any extra processes offered
441 * by nfsd just exit. If nfsd is new enough, it will call us
442 * once with a structure that specifies how many threads to
446 if (nfsrv_numnfsd == 0) {
451 rpc_gss_set_svc_name_call(principal, "kerberosv5",
452 GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
453 rpc_gss_set_svc_name_call(principal, "kerberosv5",
454 GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
457 nfsrv_pool->sp_minthreads = args->minthreads;
458 nfsrv_pool->sp_maxthreads = args->maxthreads;
460 nfsrv_pool->sp_minthreads = 4;
461 nfsrv_pool->sp_maxthreads = 4;
466 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
467 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
479 * Size the NFS server's duplicate request cache at 1/2 the
480 * nmbclusters, floating within a (64, 2048) range. This is to
481 * prevent all mbuf clusters being tied up in the NFS dupreq
482 * cache for small values of nmbclusters.
485 nfsrv_replay_size(void)
489 replaysiz = nmbclusters / 2;
490 if (replaysiz > NFSRVCACHE_MAX_SIZE)
491 replaysiz = NFSRVCACHE_MAX_SIZE;
492 if (replaysiz < NFSRVCACHE_MIN_SIZE)
493 replaysiz = NFSRVCACHE_MIN_SIZE;
494 replaysiz *= MCLBYTES;
500 * Called when nmbclusters changes - we resize the replay cache
504 nfsrv_nmbclusters_change(void *tag)
508 replay_setsize(nfsrv_pool->sp_rcache, nfsrv_replay_size());
512 * Initialize the data structures for the server.
513 * Handshake with any new nfsds starting up to avoid any chance of
517 nfsrv_init(int terminating)
524 EVENTHANDLER_DEREGISTER(nmbclusters_change,
525 nfsrv_nmbclusters_tag);
526 svcpool_destroy(nfsrv_pool);
530 nfs_pub.np_valid = 0;
534 nfsrv_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsrv));
535 nfsrv_pool->sp_rcache = replay_newcache(nfsrv_replay_size());
536 nfsrv_pool->sp_assign = fhaold_assign;
537 nfsrv_pool->sp_done = fha_nd_complete;
538 nfsrv_nmbclusters_tag = EVENTHANDLER_REGISTER(nmbclusters_change,
539 nfsrv_nmbclusters_change, NULL, EVENTHANDLER_PRI_FIRST);