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"
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>
47 #include <sys/filedesc.h>
48 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/domain.h>
59 #include <sys/protosw.h>
60 #include <sys/namei.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
64 #include <netinet/in.h>
65 #include <netinet/tcp.h>
68 #include <netinet6/in6_var.h>
70 #include <nfs/xdr_subs.h>
71 #include <nfs/rpcv2.h>
72 #include <nfs/nfsproto.h>
73 #include <nfsserver/nfs.h>
74 #include <nfsserver/nfsm_subs.h>
75 #include <nfsserver/nfsrvcache.h>
77 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
79 MALLOC_DEFINE(M_NFSRVDESC, "NFSV3 srvdesc", "NFS server socket descriptor");
80 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
86 SYSCTL_DECL(_vfs_nfsrv);
89 int nfsrv_numnfsd = 0;
90 static int notstarted = 1;
92 static int nfs_privport = 0;
93 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
94 &nfs_privport, 0, "");
95 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
96 &nfsrvw_procrastinate, 0, "");
97 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
98 &nfsrvw_procrastinate_v3, 0, "");
100 static int nfssvc_addsock(struct file *, struct sockaddr *,
102 static void nfsrv_zapsock(struct nfssvc_sock *slp);
103 static int nfssvc_nfsd(struct thread *);
106 * NFS server system calls
110 * Nfs server psuedo system call for the nfsd's
111 * Based on the flag value it either:
112 * - adds a socket to the selection list
113 * - remains in the kernel as an nfsd
114 * - remains in the kernel as an nfsiod
115 * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
116 * and that mountd provides
117 * - sockaddr with no IPv4-mapped addresses
118 * - mask for both INET and INET6 families if there is IPv4-mapped overlap
120 #ifndef _SYS_SYSPROTO_H_
130 nfssvc(struct thread *td, struct nfssvc_args *uap)
133 struct sockaddr *nam;
134 struct nfsd_args nfsdarg;
137 KASSERT(!mtx_owned(&Giant), ("nfssvc(): called with Giant"));
140 error = mac_check_system_nfsd(td->td_ucred);
149 while (nfssvc_sockhead_flag & SLP_INIT) {
150 nfssvc_sockhead_flag |= SLP_WANTINIT;
151 (void) msleep(&nfssvc_sockhead, &nfsd_mtx, PSOCK,
155 if (uap->flag & NFSSVC_ADDSOCK) {
156 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
159 if ((error = fget(td, nfsdarg.sock, &fp)) != 0)
161 if (fp->f_type != DTYPE_SOCKET) {
166 * Get the client address for connected sockets.
168 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
171 error = getsockaddr(&nam, nfsdarg.name,
178 error = nfssvc_addsock(fp, nam, td);
180 } else if (uap->flag & NFSSVC_NFSD) {
181 error = nfssvc_nfsd(td);
185 if (error == EINTR || error == ERESTART)
193 * Adds a socket to the list for servicing by nfsds.
196 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
199 struct nfssvc_sock *slp;
208 * XXXRW: If this code is ever enabled, there's a race when running
213 * Add it to the list, as required.
215 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
217 if (tslp->ns_flag & SLP_VALID) {
219 FREE(mynam, M_SONAME);
224 if (so->so_type == SOCK_STREAM)
225 siz = NFS_MAXPACKET + sizeof (u_long);
228 error = soreserve(so, siz, siz);
231 FREE(mynam, M_SONAME);
236 * Set protocol specific options { for now TCP only } and
237 * reserve some space. For datagram sockets, this can get called
238 * repeatedly for the same socket, but that isn't harmful.
240 if (so->so_type == SOCK_STREAM) {
244 bzero(&sopt, sizeof sopt);
245 sopt.sopt_dir = SOPT_SET;
246 sopt.sopt_level = SOL_SOCKET;
247 sopt.sopt_name = SO_KEEPALIVE;
248 sopt.sopt_val = &val;
249 sopt.sopt_valsize = sizeof val;
253 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
257 bzero(&sopt, sizeof sopt);
258 sopt.sopt_dir = SOPT_SET;
259 sopt.sopt_level = IPPROTO_TCP;
260 sopt.sopt_name = TCP_NODELAY;
261 sopt.sopt_val = &val;
262 sopt.sopt_valsize = sizeof val;
266 SOCKBUF_LOCK(&so->so_rcv);
267 so->so_rcv.sb_flags &= ~SB_NOINTR;
268 so->so_rcv.sb_timeo = 0;
269 SOCKBUF_UNLOCK(&so->so_rcv);
270 SOCKBUF_LOCK(&so->so_snd);
271 so->so_snd.sb_flags &= ~SB_NOINTR;
272 so->so_snd.sb_timeo = 0;
273 SOCKBUF_UNLOCK(&so->so_snd);
275 slp = (struct nfssvc_sock *)
276 malloc(sizeof (struct nfssvc_sock), M_NFSSVC,
278 STAILQ_INIT(&slp->ns_rec);
280 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
287 * XXXRW: Socket locking here?
290 so->so_upcallarg = (caddr_t)slp;
291 so->so_upcall = nfsrv_rcv;
292 SOCKBUF_LOCK(&so->so_rcv);
293 so->so_rcv.sb_flags |= SB_UPCALL;
294 SOCKBUF_UNLOCK(&so->so_rcv);
295 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
303 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
304 * until it is killed by a signal.
307 nfssvc_nfsd(struct thread *td)
310 struct nfssvc_sock *slp;
312 struct nfsrv_descript *nd = NULL;
313 struct mbuf *m, *mreq;
314 int error = 0, cacherep, s, sotype, writes_todo;
324 nfsd = (struct nfsd *)
325 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK | M_ZERO);
330 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
334 * Loop getting rpc requests until SIGKILL.
337 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
338 while (nfsd->nfsd_slp == NULL &&
339 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
340 nfsd->nfsd_flag |= NFSD_WAITING;
342 error = msleep(nfsd, &nfsd_mtx,
343 PSOCK | PCATCH, "-", 0);
348 if (nfsd->nfsd_slp == NULL &&
349 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
350 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
351 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
352 == (SLP_VALID | SLP_DOREC)) {
353 slp->ns_flag &= ~SLP_DOREC;
355 nfsd->nfsd_slp = slp;
360 nfsd_head_flag &= ~NFSD_CHECKSLP;
362 if ((slp = nfsd->nfsd_slp) == NULL)
364 if (slp->ns_flag & SLP_VALID) {
365 if (slp->ns_flag & SLP_DISCONN)
367 else if (slp->ns_flag & SLP_NEEDQ) {
368 slp->ns_flag &= ~SLP_NEEDQ;
369 (void) nfs_slplock(slp, 1);
371 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
376 error = nfsrv_dorec(slp, nfsd, &nd);
377 cur_usec = nfs_curusec();
378 if (error && LIST_FIRST(&slp->ns_tq) &&
379 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
385 nfsd->nfsd_flag |= NFSD_REQINPROG;
389 slp = nfsd->nfsd_slp;
391 if (error || (slp->ns_flag & SLP_VALID) == 0) {
393 free((caddr_t)nd, M_NFSRVDESC);
396 nfsd->nfsd_slp = NULL;
397 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
402 sotype = slp->ns_so->so_type;
404 getmicrotime(&nd->nd_starttime);
406 nd->nd_nam = nd->nd_nam2;
408 nd->nd_nam = slp->ns_nam;
411 * Check to see if authorization is needed.
413 cacherep = nfsrv_getcache(nd, &mreq);
416 /* Check if source port is privileged */
418 struct sockaddr *nam = nd->nd_nam;
419 struct sockaddr_in *sin;
421 sin = (struct sockaddr_in *)nam;
423 * INET/INET6 - same code:
424 * sin_port and sin6_port are at same offset
426 port = ntohs(sin->sin_port);
427 if (port >= IPPORT_RESERVED &&
428 nd->nd_procnum != NFSPROC_NULL) {
429 #if defined(INET6) && defined(KLD_MODULE)
430 /* do not use ip6_sprintf: the nfs module should work without INET6 */
431 char b6[INET6_ADDRSTRLEN];
432 #define ip6_sprintf(a) \
433 (sprintf(b6, "%x:%x:%x:%x:%x:%x:%x:%x", \
434 (a)->s6_addr16[0], (a)->s6_addr16[1], \
435 (a)->s6_addr16[2], (a)->s6_addr16[3], \
436 (a)->s6_addr16[4], (a)->s6_addr16[5], \
437 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
440 nd->nd_procnum = NFSPROC_NOOP;
441 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
443 printf("NFS request from unprivileged port (%s:%d)\n",
445 sin->sin_family == AF_INET6 ?
446 ip6_sprintf(&satosin6(sin)->sin6_addr) :
449 inet_ntoa(sin->sin_addr), port);
456 * Loop to get all the write rpc relies that have been
462 if (nd && (nd->nd_flag & ND_NFSV3))
463 procrastinate = nfsrvw_procrastinate_v3;
465 procrastinate = nfsrvw_procrastinate;
466 if (writes_todo || (!(nd->nd_flag & ND_NFSV3) &&
467 nd->nd_procnum == NFSPROC_WRITE &&
468 procrastinate > 0 && !notstarted))
469 error = nfsrv_writegather(&nd, slp,
470 nfsd->nfsd_td, &mreq);
472 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
473 slp, nfsd->nfsd_td, &mreq);
476 if (error != 0 && error != NFSERR_RETVOID) {
477 nfsrvstats.srv_errs++;
478 nfsrv_updatecache(nd, FALSE, mreq);
480 FREE(nd->nd_nam2, M_SONAME);
483 nfsrvstats.srvrpccnt[nd->nd_procnum]++;
484 nfsrv_updatecache(nd, TRUE, mreq);
489 siz = m_length(mreq, NULL);
490 if (siz <= 0 || siz > NFS_MAXPACKET) {
491 printf("mbuf siz=%d\n",siz);
492 panic("Bad nfs svc reply");
495 m->m_pkthdr.len = siz;
496 m->m_pkthdr.rcvif = NULL;
498 * For stream protocols, prepend a Sun RPC
501 if (sotype == SOCK_STREAM) {
502 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
503 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
506 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
507 (void) nfs_slplock(slp, 1);
508 if (slp->ns_flag & SLP_VALID) {
510 error = nfsrv_send(slp->ns_so, nd->nd_nam2, m);
517 FREE(nd->nd_nam2, M_SONAME);
519 m_freem(nd->nd_mrep);
522 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
524 if (error == EINTR || error == ERESTART) {
525 free((caddr_t)nd, M_NFSRVDESC);
532 m_freem(nd->nd_mrep);
534 FREE(nd->nd_nam2, M_SONAME);
538 FREE((caddr_t)nd, M_NFSRVDESC);
543 * Check to see if there are outstanding writes that
544 * need to be serviced.
546 cur_usec = nfs_curusec();
548 if (LIST_FIRST(&slp->ns_tq) &&
549 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
555 } while (writes_todo);
557 if (nfsrv_dorec(slp, nfsd, &nd)) {
558 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
559 nfsd->nfsd_slp = NULL;
562 KASSERT(!(debug_mpsafenet == 0 && !mtx_owned(&Giant)),
563 ("nfssvc_nfsd(): debug.mpsafenet=0 && !Giant"));
564 KASSERT(!(debug_mpsafenet == 1 && mtx_owned(&Giant)),
565 ("nfssvc_nfsd(): debug.mpsafenet=1 && Giant"));
568 KASSERT(!(debug_mpsafenet == 0 && !mtx_owned(&Giant)),
569 ("nfssvc_nfsd(): debug.mpsafenet=0 && !Giant"));
570 KASSERT(!(debug_mpsafenet == 1 && mtx_owned(&Giant)),
571 ("nfssvc_nfsd(): debug.mpsafenet=1 && Giant"));
572 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
574 free((caddr_t)nfsd, M_NFSD);
575 if (--nfsrv_numnfsd == 0)
576 nfsrv_init(TRUE); /* Reinitialize everything */
582 * Shut down a socket associated with an nfssvc_sock structure.
583 * Should be called with the send lock set, if required.
584 * The trick here is to increment the sref at the start, so that the nfsds
585 * will stop using it and clear ns_flag at the end so that it will not be
586 * reassigned during cleanup.
589 nfsrv_zapsock(struct nfssvc_sock *slp)
591 struct nfsrv_descript *nwp, *nnwp;
594 struct nfsrv_rec *rec;
601 * XXXRW: By clearing all flags, other threads/etc should ignore
602 * this slp and we can safely release nfsd_mtx so we can clean
605 slp->ns_flag &= ~SLP_ALLFLAGS;
611 SOCKBUF_LOCK(&so->so_rcv);
612 so->so_rcv.sb_flags &= ~SB_UPCALL;
613 SOCKBUF_UNLOCK(&so->so_rcv);
614 so->so_upcall = NULL;
615 so->so_upcallarg = NULL;
616 soshutdown(so, SHUT_RDWR);
620 FREE(slp->ns_nam, M_SONAME);
621 m_freem(slp->ns_raw);
622 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
623 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
625 FREE(rec->nr_address, M_SONAME);
626 m_freem(rec->nr_packet);
627 free(rec, M_NFSRVDESC);
630 for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
631 nnwp = LIST_NEXT(nwp, nd_tq);
632 LIST_REMOVE(nwp, nd_tq);
633 free((caddr_t)nwp, M_NFSRVDESC);
635 LIST_INIT(&slp->ns_tq);
641 * Derefence a server socket structure. If it has no more references and
642 * is no longer valid, you can throw it away.
645 nfsrv_slpderef(struct nfssvc_sock *slp)
650 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
651 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
652 free((caddr_t)slp, M_NFSSVC);
657 * Lock a socket against others.
659 * XXXRW: Wait argument is always 1 in the caller. Replace with a real
663 nfs_slplock(struct nfssvc_sock *slp, int wait)
665 int *statep = &slp->ns_solock;
669 if (!wait && (*statep & NFSRV_SNDLOCK))
670 return(0); /* already locked, fail */
671 while (*statep & NFSRV_SNDLOCK) {
672 *statep |= NFSRV_WANTSND;
673 (void) msleep(statep, &nfsd_mtx, PZERO - 1, "nfsslplck", 0);
675 *statep |= NFSRV_SNDLOCK;
680 * Unlock the stream socket for others.
683 nfs_slpunlock(struct nfssvc_sock *slp)
685 int *statep = &slp->ns_solock;
689 if ((*statep & NFSRV_SNDLOCK) == 0)
690 panic("nfs slpunlock");
691 *statep &= ~NFSRV_SNDLOCK;
692 if (*statep & NFSRV_WANTSND) {
693 *statep &= ~NFSRV_WANTSND;
699 * Initialize the data structures for the server.
700 * Handshake with any new nfsds starting up to avoid any chance of
704 nfsrv_init(int terminating)
706 struct nfssvc_sock *slp, *nslp;
711 if (nfssvc_sockhead_flag & SLP_INIT)
713 nfssvc_sockhead_flag |= SLP_INIT;
715 for (slp = TAILQ_FIRST(&nfssvc_sockhead); slp != NULL;
717 nslp = TAILQ_NEXT(slp, ns_chain);
718 if (slp->ns_flag & SLP_VALID)
720 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
721 free((caddr_t)slp, M_NFSSVC);
723 nfsrv_cleancache(); /* And clear out server cache */
725 nfs_pub.np_valid = 0;
727 TAILQ_INIT(&nfssvc_sockhead);
728 nfssvc_sockhead_flag &= ~SLP_INIT;
729 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
730 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
731 wakeup(&nfssvc_sockhead);
734 TAILQ_INIT(&nfsd_head);
735 nfsd_head_flag &= ~NFSD_CHECKSLP;
738 nfs_udpsock = (struct nfssvc_sock *)
739 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
740 STAILQ_INIT(&nfs_udpsock->ns_rec);
741 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
743 nfs_cltpsock = (struct nfssvc_sock *)
744 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
745 STAILQ_INIT(&nfs_cltpsock->ns_rec);
746 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);