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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
51 #include <sys/filedesc.h>
52 #include <sys/vnode.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/domain.h>
62 #include <sys/protosw.h>
63 #include <sys/namei.h>
64 #include <sys/fcntl.h>
65 #include <sys/lockf.h>
67 #include <netinet/in.h>
68 #include <netinet/tcp.h>
71 #include <netinet6/in6_var.h>
73 #include <nfs/xdr_subs.h>
74 #include <nfs/rpcv2.h>
75 #include <nfs/nfsproto.h>
76 #include <nfsserver/nfs.h>
77 #include <nfsserver/nfsm_subs.h>
78 #include <nfsserver/nfsrvcache.h>
80 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
82 MALLOC_DEFINE(M_NFSRVDESC, "NFSV3 srvdesc", "NFS server socket descriptor");
83 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
89 SYSCTL_DECL(_vfs_nfsrv);
92 static int nfs_numnfsd = 0;
93 static int notstarted = 1;
95 static int nfs_privport = 0;
96 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
97 &nfs_privport, 0, "");
98 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
99 &nfsrvw_procrastinate, 0, "");
100 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
101 &nfsrvw_procrastinate_v3, 0, "");
103 static int nfssvc_addsock(struct file *, struct sockaddr *,
105 static void nfsrv_zapsock(struct nfssvc_sock *slp);
106 static int nfssvc_nfsd(struct thread *);
109 * NFS server system calls
113 * Nfs server psuedo system call for the nfsd's
114 * Based on the flag value it either:
115 * - adds a socket to the selection list
116 * - remains in the kernel as an nfsd
117 * - remains in the kernel as an nfsiod
118 * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
119 * and that mountd provides
120 * - sockaddr with no IPv4-mapped addresses
121 * - mask for both INET and INET6 families if there is IPv4-mapped overlap
123 #ifndef _SYS_SYSPROTO_H_
133 nfssvc(struct thread *td, struct nfssvc_args *uap)
136 struct sockaddr *nam;
137 struct nfsd_args nfsdarg;
144 while (nfssvc_sockhead_flag & SLP_INIT) {
145 nfssvc_sockhead_flag |= SLP_WANTINIT;
146 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
148 if (uap->flag & NFSSVC_ADDSOCK) {
149 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
152 if ((error = fget(td, nfsdarg.sock, &fp)) != 0)
154 if (fp->f_type != DTYPE_SOCKET) {
159 * Get the client address for connected sockets.
161 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
164 error = getsockaddr(&nam, nfsdarg.name,
171 error = nfssvc_addsock(fp, nam, td);
173 } else if (uap->flag & NFSSVC_NFSD) {
174 error = nfssvc_nfsd(td);
178 if (error == EINTR || error == ERESTART)
186 * Adds a socket to the list for servicing by nfsds.
189 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
192 struct nfssvc_sock *slp;
196 so = (struct socket *)fp->f_data;
200 * Add it to the list, as required.
202 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
204 if (tslp->ns_flag & SLP_VALID) {
206 FREE(mynam, M_SONAME);
211 if (so->so_type == SOCK_STREAM)
212 siz = NFS_MAXPACKET + sizeof (u_long);
215 error = soreserve(so, siz, siz);
218 FREE(mynam, M_SONAME);
223 * Set protocol specific options { for now TCP only } and
224 * reserve some space. For datagram sockets, this can get called
225 * repeatedly for the same socket, but that isn't harmful.
227 if (so->so_type == SOCK_STREAM) {
231 bzero(&sopt, sizeof sopt);
232 sopt.sopt_level = SOL_SOCKET;
233 sopt.sopt_name = SO_KEEPALIVE;
234 sopt.sopt_val = &val;
235 sopt.sopt_valsize = sizeof val;
239 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
243 bzero(&sopt, sizeof sopt);
244 sopt.sopt_level = IPPROTO_TCP;
245 sopt.sopt_name = TCP_NODELAY;
246 sopt.sopt_val = &val;
247 sopt.sopt_valsize = sizeof val;
251 so->so_rcv.sb_flags &= ~SB_NOINTR;
252 so->so_rcv.sb_timeo = 0;
253 so->so_snd.sb_flags &= ~SB_NOINTR;
254 so->so_snd.sb_timeo = 0;
256 slp = (struct nfssvc_sock *)
257 malloc(sizeof (struct nfssvc_sock), M_NFSSVC,
259 STAILQ_INIT(&slp->ns_rec);
260 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
267 so->so_upcallarg = (caddr_t)slp;
268 so->so_upcall = nfsrv_rcv;
269 so->so_rcv.sb_flags |= SB_UPCALL;
270 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
277 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
278 * until it is killed by a signal.
281 nfssvc_nfsd(struct thread *td)
284 struct nfssvc_sock *slp;
286 struct nfsrv_descript *nd = NULL;
287 struct mbuf *m, *mreq;
288 int error = 0, cacherep, s, sotype, writes_todo;
296 nfsd = (struct nfsd *)
297 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK | M_ZERO);
300 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
304 * Loop getting rpc requests until SIGKILL.
307 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
308 while (nfsd->nfsd_slp == NULL &&
309 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
310 nfsd->nfsd_flag |= NFSD_WAITING;
312 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
318 if (nfsd->nfsd_slp == NULL &&
319 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
320 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
321 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
322 == (SLP_VALID | SLP_DOREC)) {
323 slp->ns_flag &= ~SLP_DOREC;
325 nfsd->nfsd_slp = slp;
330 nfsd_head_flag &= ~NFSD_CHECKSLP;
332 if ((slp = nfsd->nfsd_slp) == NULL)
334 if (slp->ns_flag & SLP_VALID) {
335 if (slp->ns_flag & SLP_DISCONN)
337 else if (slp->ns_flag & SLP_NEEDQ) {
338 slp->ns_flag &= ~SLP_NEEDQ;
339 (void) nfs_slplock(slp, 1);
340 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
344 error = nfsrv_dorec(slp, nfsd, &nd);
345 cur_usec = nfs_curusec();
346 if (error && LIST_FIRST(&slp->ns_tq) &&
347 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
353 nfsd->nfsd_flag |= NFSD_REQINPROG;
357 slp = nfsd->nfsd_slp;
359 if (error || (slp->ns_flag & SLP_VALID) == 0) {
361 free((caddr_t)nd, M_NFSRVDESC);
364 nfsd->nfsd_slp = NULL;
365 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
370 sotype = slp->ns_so->so_type;
372 getmicrotime(&nd->nd_starttime);
374 nd->nd_nam = nd->nd_nam2;
376 nd->nd_nam = slp->ns_nam;
379 * Check to see if authorization is needed.
381 cacherep = nfsrv_getcache(nd, &mreq);
384 /* Check if source port is privileged */
386 struct sockaddr *nam = nd->nd_nam;
387 struct sockaddr_in *sin;
389 sin = (struct sockaddr_in *)nam;
391 * INET/INET6 - same code:
392 * sin_port and sin6_port are at same offset
394 port = ntohs(sin->sin_port);
395 if (port >= IPPORT_RESERVED &&
396 nd->nd_procnum != NFSPROC_NULL) {
397 #if defined(INET6) && defined(KLD_MODULE)
398 /* do not use ip6_sprintf: the nfs module should work without INET6 */
399 char b6[INET6_ADDRSTRLEN];
400 #define ip6_sprintf(a) \
401 (sprintf(b6, "%x:%x:%x:%x:%x:%x:%x:%x", \
402 (a)->s6_addr16[0], (a)->s6_addr16[1], \
403 (a)->s6_addr16[2], (a)->s6_addr16[3], \
404 (a)->s6_addr16[4], (a)->s6_addr16[5], \
405 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
408 nd->nd_procnum = NFSPROC_NOOP;
409 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
411 printf("NFS request from unprivileged port (%s:%d)\n",
413 sin->sin_family == AF_INET6 ?
414 ip6_sprintf(&satosin6(sin)->sin6_addr) :
417 inet_ntoa(sin->sin_addr), port);
424 * Loop to get all the write rpc relies that have been
430 if (nd && (nd->nd_flag & ND_NFSV3))
431 procrastinate = nfsrvw_procrastinate_v3;
433 procrastinate = nfsrvw_procrastinate;
434 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
435 procrastinate > 0 && !notstarted))
436 error = nfsrv_writegather(&nd, slp,
437 nfsd->nfsd_td, &mreq);
439 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
440 slp, nfsd->nfsd_td, &mreq);
443 if (error != 0 && error != NFSERR_RETVOID) {
444 nfsrvstats.srv_errs++;
445 nfsrv_updatecache(nd, FALSE, mreq);
447 FREE(nd->nd_nam2, M_SONAME);
450 nfsrvstats.srvrpccnt[nd->nd_procnum]++;
451 nfsrv_updatecache(nd, TRUE, mreq);
460 if (siz <= 0 || siz > NFS_MAXPACKET) {
461 printf("mbuf siz=%d\n",siz);
462 panic("Bad nfs svc reply");
465 m->m_pkthdr.len = siz;
466 m->m_pkthdr.rcvif = NULL;
468 * For stream protocols, prepend a Sun RPC
471 if (sotype == SOCK_STREAM) {
472 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
473 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
475 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
476 (void) nfs_slplock(slp, 1);
477 if (slp->ns_flag & SLP_VALID)
478 error = nfsrv_send(slp->ns_so, nd->nd_nam2, m);
484 FREE(nd->nd_nam2, M_SONAME);
486 m_freem(nd->nd_mrep);
489 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
491 if (error == EINTR || error == ERESTART) {
492 free((caddr_t)nd, M_NFSRVDESC);
499 m_freem(nd->nd_mrep);
501 FREE(nd->nd_nam2, M_SONAME);
505 FREE((caddr_t)nd, M_NFSRVDESC);
510 * Check to see if there are outstanding writes that
511 * need to be serviced.
513 cur_usec = nfs_curusec();
515 if (LIST_FIRST(&slp->ns_tq) &&
516 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
522 } while (writes_todo);
524 if (nfsrv_dorec(slp, nfsd, &nd)) {
525 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
526 nfsd->nfsd_slp = NULL;
531 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
533 free((caddr_t)nfsd, M_NFSD);
534 if (--nfs_numnfsd == 0)
535 nfsrv_init(TRUE); /* Reinitialize everything */
540 * Shut down a socket associated with an nfssvc_sock structure.
541 * Should be called with the send lock set, if required.
542 * The trick here is to increment the sref at the start, so that the nfsds
543 * will stop using it and clear ns_flag at the end so that it will not be
544 * reassigned during cleanup.
547 nfsrv_zapsock(struct nfssvc_sock *slp)
549 struct nfsrv_descript *nwp, *nnwp;
552 struct nfsrv_rec *rec;
555 slp->ns_flag &= ~SLP_ALLFLAGS;
560 so->so_rcv.sb_flags &= ~SB_UPCALL;
561 so->so_upcall = NULL;
562 so->so_upcallarg = NULL;
566 FREE(slp->ns_nam, M_SONAME);
567 m_freem(slp->ns_raw);
568 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
569 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
571 FREE(rec->nr_address, M_SONAME);
572 m_freem(rec->nr_packet);
573 free(rec, M_NFSRVDESC);
576 for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
577 nnwp = LIST_NEXT(nwp, nd_tq);
578 LIST_REMOVE(nwp, nd_tq);
579 free((caddr_t)nwp, M_NFSRVDESC);
581 LIST_INIT(&slp->ns_tq);
587 * Derefence a server socket structure. If it has no more references and
588 * is no longer valid, you can throw it away.
591 nfsrv_slpderef(struct nfssvc_sock *slp)
594 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
595 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
596 free((caddr_t)slp, M_NFSSVC);
601 * Lock a socket against others.
604 nfs_slplock(struct nfssvc_sock *slp, int wait)
606 int *statep = &slp->ns_solock;
608 if (!wait && (*statep & NFSRV_SNDLOCK))
609 return(0); /* already locked, fail */
610 while (*statep & NFSRV_SNDLOCK) {
611 *statep |= NFSRV_WANTSND;
612 (void) tsleep((caddr_t)statep, PZERO - 1, "nfsslplck", 0);
614 *statep |= NFSRV_SNDLOCK;
619 * Unlock the stream socket for others.
622 nfs_slpunlock(struct nfssvc_sock *slp)
624 int *statep = &slp->ns_solock;
626 if ((*statep & NFSRV_SNDLOCK) == 0)
627 panic("nfs slpunlock");
628 *statep &= ~NFSRV_SNDLOCK;
629 if (*statep & NFSRV_WANTSND) {
630 *statep &= ~NFSRV_WANTSND;
631 wakeup((caddr_t)statep);
636 * Initialize the data structures for the server.
637 * Handshake with any new nfsds starting up to avoid any chance of
641 nfsrv_init(int terminating)
643 struct nfssvc_sock *slp, *nslp;
645 if (nfssvc_sockhead_flag & SLP_INIT)
647 nfssvc_sockhead_flag |= SLP_INIT;
649 for (slp = TAILQ_FIRST(&nfssvc_sockhead); slp != 0; slp = nslp){
650 nslp = TAILQ_NEXT(slp, ns_chain);
651 if (slp->ns_flag & SLP_VALID)
653 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
654 free((caddr_t)slp, M_NFSSVC);
656 nfsrv_cleancache(); /* And clear out server cache */
658 nfs_pub.np_valid = 0;
660 TAILQ_INIT(&nfssvc_sockhead);
661 nfssvc_sockhead_flag &= ~SLP_INIT;
662 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
663 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
664 wakeup((caddr_t)&nfssvc_sockhead);
667 TAILQ_INIT(&nfsd_head);
668 nfsd_head_flag &= ~NFSD_CHECKSLP;
671 nfs_udpsock = (struct nfssvc_sock *)
672 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
673 STAILQ_INIT(&nfs_udpsock->ns_rec);
674 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
676 nfs_cltpsock = (struct nfssvc_sock *)
677 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
678 STAILQ_INIT(&nfs_cltpsock->ns_rec);
679 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);