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
37 * $FreeBSD: src/sys/nfs/nfs_syscalls.c,v 1.58.2.1 2000/11/26 02:30:06 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_syscalls.c,v 1.31 2008/01/05 14:02:41 swildner Exp $
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>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
55 #include <sys/resourcevar.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/domain.h>
59 #include <sys/protosw.h>
60 #include <sys/nlookup.h>
61 #include <sys/mutex.h>
62 #include <vm/vm_zone.h>
64 #include <sys/mutex2.h>
66 #include <netinet/in.h>
67 #include <netinet/tcp.h>
72 #include "nfsm_subs.h"
73 #include "nfsrvcache.h"
78 #include <sys/thread2.h>
80 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
82 static int nuidhash_max = NFS_MAXUIDHASH;
85 static void nfsrv_zapsock (struct nfssvc_sock *slp);
91 SYSCTL_DECL(_vfs_nfs);
95 static struct nfsdrt nfsdrt;
96 static int nfs_numnfsd = 0;
97 static void nfsd_rt (int sotype, struct nfsrv_descript *nd,
99 static int nfssvc_addsock (struct file *, struct sockaddr *,
101 static int nfssvc_nfsd (struct nfsd_srvargs *,caddr_t,struct thread *);
103 static int nfs_privport = 0;
104 SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
105 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0, "");
106 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0, "");
107 static int nfs_soreserve = 65535;
108 SYSCTL_INT(_vfs_nfs, OID_AUTO, soreserve, CTLFLAG_RW, &nfs_soreserve, 0, "");
111 * NFS server system calls
114 #endif /* NFS_NOSERVER */
116 * nfssvc_args(int flag, caddr_t argp)
118 * Nfs server psuedo system call for the nfsd's
119 * Based on the flag value it either:
120 * - adds a socket to the selection list
121 * - remains in the kernel as an nfsd
122 * - remains in the kernel as an nfsiod
125 sys_nfssvc(struct nfssvc_args *uap)
128 struct nlookupdata nd;
130 struct sockaddr *nam;
131 struct nfsd_args nfsdarg;
132 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
133 struct nfsd_cargs ncd;
135 struct nfssvc_sock *slp;
136 struct nfsuid *nuidp;
137 struct nfsmount *nmp;
139 #endif /* NFS_NOSERVER */
141 struct thread *td = curthread;
146 error = priv_check(td, PRIV_ROOT);
149 KKASSERT(td->td_proc); /* for ucred and p_fd */
150 while (nfssvc_sockhead_flag & SLP_INIT) {
151 nfssvc_sockhead_flag |= SLP_WANTINIT;
152 (void) tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
154 if (uap->flag & NFSSVC_BIOD)
155 error = ENXIO; /* no longer need nfsiod's */
159 #else /* !NFS_NOSERVER */
160 else if (uap->flag & NFSSVC_MNTD) {
161 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
165 error = nlookup_init(&nd, ncd.ncd_dirp, UIO_USERSPACE,
168 error = nlookup(&nd);
170 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
175 if ((vp->v_flag & VROOT) == 0)
177 nmp = VFSTONFS(vp->v_mount);
181 if ((nmp->nm_state & NFSSTA_MNTD) &&
182 (uap->flag & NFSSVC_GOTAUTH) == 0)
184 nmp->nm_state |= NFSSTA_MNTD;
185 error = nfs_clientd(nmp, td->td_proc->p_ucred, &ncd, uap->flag,
187 } else if (uap->flag & NFSSVC_ADDSOCK) {
188 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
191 error = holdsock(td->td_proc->p_fd, nfsdarg.sock, &fp);
195 * Get the client address for connected sockets.
197 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
200 error = getsockaddr(&nam, nfsdarg.name,
207 error = nfssvc_addsock(fp, nam, td);
210 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
213 if ((uap->flag & NFSSVC_AUTHIN) &&
214 ((nfsd = nsd->nsd_nfsd)) != NULL &&
215 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
216 slp = nfsd->nfsd_slp;
219 * First check to see if another nfsd has already
220 * added this credential.
222 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
223 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
224 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
225 (!nfsd->nfsd_nd->nd_nam2 ||
226 netaddr_match(NU_NETFAM(nuidp),
227 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
231 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
232 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
237 if (slp->ns_numuids < nuidhash_max) {
239 nuidp = (struct nfsuid *)
240 kmalloc(sizeof (struct nfsuid), M_NFSUID,
244 if ((slp->ns_flag & SLP_VALID) == 0) {
246 kfree((caddr_t)nuidp, M_NFSUID);
249 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
250 LIST_REMOVE(nuidp, nu_hash);
251 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
253 if (nuidp->nu_flag & NU_NAM)
254 FREE(nuidp->nu_nam, M_SONAME);
257 nuidp->nu_cr = nsd->nsd_cr;
258 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
259 nuidp->nu_cr.cr_ngroups = NGROUPS;
260 nuidp->nu_cr.cr_ref = 1;
261 nuidp->nu_timestamp = nsd->nsd_timestamp;
262 nuidp->nu_expire = time_second + nsd->nsd_ttl;
264 * and save the session key in nu_key.
266 bcopy(nsd->nsd_key, nuidp->nu_key,
267 sizeof (nsd->nsd_key));
268 if (nfsd->nfsd_nd->nd_nam2) {
269 struct sockaddr_in *saddr;
271 saddr = (struct sockaddr_in *)
272 nfsd->nfsd_nd->nd_nam2;
273 switch (saddr->sin_family) {
275 nuidp->nu_flag |= NU_INETADDR;
277 saddr->sin_addr.s_addr;
281 nuidp->nu_flag |= NU_NAM;
283 dup_sockaddr(nfsd->nfsd_nd->nd_nam2);
287 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
289 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
291 nfsrv_setcred(&nuidp->nu_cr,
292 &nfsd->nfsd_nd->nd_cr);
293 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
297 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
298 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
299 error = nfssvc_nfsd(nsd, uap->argp, td);
301 #endif /* NFS_NOSERVER */
302 if (error == EINTR || error == ERESTART)
309 * Adds a socket to the list for servicing by nfsds.
312 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
315 struct nfssvc_sock *slp;
319 so = (struct socket *)fp->f_data;
323 * Add it to the list, as required.
325 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
327 if (tslp->ns_flag & SLP_VALID) {
329 FREE(mynam, M_SONAME);
335 * Reserve buffer space in the socket. Note that due to bugs in
336 * Linux's delayed-ack code, serious performance degredation may
337 * occur with linux hosts if the minimum is used.
339 if (so->so_type == SOCK_STREAM)
340 siz = NFS_MAXPACKET + sizeof (u_long);
343 if (siz < nfs_soreserve)
345 if (siz > sb_max_adj) {
346 kprintf("Warning: vfs.nfs.soreserve (%d) "
347 "limited to adjusted sb_max (%ld)\n",
348 nfs_soreserve, sb_max_adj);
352 error = soreserve(so, siz, siz, &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
355 FREE(mynam, M_SONAME);
360 * Set protocol specific options { for now TCP only } and
361 * reserve some space. For datagram sockets, this can get called
362 * repeatedly for the same socket, but that isn't harmful.
364 if (so->so_type == SOCK_STREAM) {
368 bzero(&sopt, sizeof sopt);
369 sopt.sopt_level = SOL_SOCKET;
370 sopt.sopt_name = SO_KEEPALIVE;
371 sopt.sopt_val = &val;
372 sopt.sopt_valsize = sizeof val;
376 if (so->so_proto->pr_domain->dom_family == AF_INET &&
377 so->so_proto->pr_protocol == IPPROTO_TCP) {
381 bzero(&sopt, sizeof sopt);
382 sopt.sopt_level = IPPROTO_TCP;
383 sopt.sopt_name = TCP_NODELAY;
384 sopt.sopt_val = &val;
385 sopt.sopt_valsize = sizeof val;
389 so->so_rcv.ssb_flags &= ~SSB_NOINTR;
390 so->so_rcv.ssb_timeo = 0;
391 so->so_snd.ssb_flags &= ~SSB_NOINTR;
392 so->so_snd.ssb_timeo = 0;
394 slp = (struct nfssvc_sock *)kmalloc(sizeof (struct nfssvc_sock),
395 M_NFSSVC, M_WAITOK | M_ZERO);
396 mtx_init(&slp->ns_solock);
397 STAILQ_INIT(&slp->ns_rec);
398 TAILQ_INIT(&slp->ns_uidlruhead);
399 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
406 so->so_upcallarg = (caddr_t)slp;
407 so->so_upcall = nfsrv_rcv;
408 so->so_rcv.ssb_flags |= SSB_UPCALL;
409 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
410 nfsrv_wakenfsd(slp, 1);
416 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
417 * until it is killed by a signal.
420 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
423 struct nfssvc_sock *slp;
424 struct nfsd *nfsd = nsd->nsd_nfsd;
425 struct nfsrv_descript *nd = NULL;
426 struct mbuf *m, *mreq;
427 int error = 0, cacherep, sotype, writes_todo;
436 nsd->nsd_nfsd = nfsd = (struct nfsd *)
437 kmalloc(sizeof (struct nfsd), M_NFSD, M_WAITOK|M_ZERO);
440 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
446 * Loop getting rpc requests until SIGKILL.
449 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
450 while (nfsd->nfsd_slp == NULL &&
451 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
452 nfsd->nfsd_flag |= NFSD_WAITING;
454 error = tsleep((caddr_t)nfsd, PCATCH, "nfsd", 0);
459 if (nfsd->nfsd_slp == NULL &&
460 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
461 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
462 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
463 == (SLP_VALID | SLP_DOREC)) {
464 slp->ns_flag &= ~SLP_DOREC;
466 nfsd->nfsd_slp = slp;
471 nfsd_head_flag &= ~NFSD_CHECKSLP;
473 if ((slp = nfsd->nfsd_slp) == NULL)
475 if (slp->ns_flag & SLP_VALID) {
476 if (slp->ns_flag & SLP_DISCONN)
478 else if (slp->ns_flag & SLP_NEEDQ) {
479 slp->ns_flag &= ~SLP_NEEDQ;
480 (void) nfs_slplock(slp, 1);
481 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
485 error = nfsrv_dorec(slp, nfsd, &nd);
486 cur_usec = nfs_curusec();
487 if (error && slp->ns_tq.lh_first &&
488 slp->ns_tq.lh_first->nd_time <= cur_usec) {
494 nfsd->nfsd_flag |= NFSD_REQINPROG;
498 slp = nfsd->nfsd_slp;
500 if (error || (slp->ns_flag & SLP_VALID) == 0) {
502 kfree((caddr_t)nd, M_NFSRVDESC);
505 nfsd->nfsd_slp = NULL;
506 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
511 sotype = slp->ns_so->so_type;
513 getmicrotime(&nd->nd_starttime);
515 nd->nd_nam = nd->nd_nam2;
517 nd->nd_nam = slp->ns_nam;
520 * Check to see if authorization is needed.
522 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
523 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
525 ((struct sockaddr_in *)
526 nd->nd_nam)->sin_addr.s_addr;
527 nsd->nsd_authlen = nfsd->nfsd_authlen;
528 nsd->nsd_verflen = nfsd->nfsd_verflen;
529 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
530 nfsd->nfsd_authlen) &&
531 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
532 nfsd->nfsd_verflen) &&
533 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
535 cacherep = RC_DROPIT;
537 cacherep = nfsrv_getcache(nd, slp, &mreq);
540 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
541 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
542 nd->nd_procnum = NFSPROC_NOOP;
543 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
545 } else if (nfs_privport) {
546 /* Check if source port is privileged */
548 struct sockaddr *nam = nd->nd_nam;
549 struct sockaddr_in *sin;
551 sin = (struct sockaddr_in *)nam;
552 port = ntohs(sin->sin_port);
553 if (port >= IPPORT_RESERVED &&
554 nd->nd_procnum != NFSPROC_NULL) {
555 nd->nd_procnum = NFSPROC_NOOP;
556 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
558 kprintf("NFS request from unprivileged port (%s:%d)\n",
559 inet_ntoa(sin->sin_addr), port);
566 * Loop to get all the write rpc relies that have been
572 if (nd && (nd->nd_flag & ND_NFSV3))
573 procrastinate = nfsrvw_procrastinate_v3;
575 procrastinate = nfsrvw_procrastinate;
576 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
579 error = nfsrv_writegather(&nd, slp,
580 nfsd->nfsd_td, &mreq);
582 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
583 slp, nfsd->nfsd_td, &mreq);
587 if (error != 0 && error != NFSERR_RETVOID) {
588 if (nd->nd_procnum != NQNFSPROC_VACATED)
590 nfsrv_updatecache(nd, FALSE, mreq);
592 FREE(nd->nd_nam2, M_SONAME);
595 nfsstats.srvrpccnt[nd->nd_procnum]++;
596 nfsrv_updatecache(nd, TRUE, mreq);
605 if (siz <= 0 || siz > NFS_MAXPACKET) {
606 kprintf("mbuf siz=%d\n",siz);
607 panic("Bad nfs svc reply");
610 m->m_pkthdr.len = siz;
611 m->m_pkthdr.rcvif = NULL;
613 * For stream protocols, prepend a Sun RPC
616 if (sotype == SOCK_STREAM) {
617 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
620 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
622 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
623 (void) nfs_slplock(slp, 1);
624 if (slp->ns_flag & SLP_VALID)
625 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
631 nfsd_rt(sotype, nd, cacherep);
633 FREE(nd->nd_nam2, M_SONAME);
635 m_freem(nd->nd_mrep);
638 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
640 if (error == EINTR || error == ERESTART) {
641 kfree((caddr_t)nd, M_NFSRVDESC);
649 nfsd_rt(sotype, nd, cacherep);
650 m_freem(nd->nd_mrep);
652 FREE(nd->nd_nam2, M_SONAME);
656 FREE((caddr_t)nd, M_NFSRVDESC);
661 * Check to see if there are outstanding writes that
662 * need to be serviced.
664 cur_usec = nfs_curusec();
666 if (slp->ns_tq.lh_first &&
667 slp->ns_tq.lh_first->nd_time <= cur_usec) {
673 } while (writes_todo);
675 if (nfsrv_dorec(slp, nfsd, &nd)) {
676 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
677 nfsd->nfsd_slp = NULL;
682 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
684 kfree((caddr_t)nfsd, M_NFSD);
685 nsd->nsd_nfsd = NULL;
686 if (--nfs_numnfsd == 0)
687 nfsrv_init(TRUE); /* Reinitialize everything */
692 * Shut down a socket associated with an nfssvc_sock structure.
693 * Should be called with the send lock set, if required.
694 * The trick here is to increment the sref at the start, so that the nfsds
695 * will stop using it and clear ns_flag at the end so that it will not be
696 * reassigned during cleanup.
699 nfsrv_zapsock(struct nfssvc_sock *slp)
701 struct nfsuid *nuidp, *nnuidp;
702 struct nfsrv_descript *nwp, *nnwp;
705 struct nfsrv_rec *rec;
707 slp->ns_flag &= ~SLP_ALLFLAGS;
712 so->so_rcv.ssb_flags &= ~SSB_UPCALL;
713 so->so_upcall = NULL;
714 so->so_upcallarg = NULL;
715 soshutdown(so, SHUT_RDWR);
718 FREE(slp->ns_nam, M_SONAME);
719 m_freem(slp->ns_raw);
720 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
722 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
724 FREE(rec->nr_address, M_SONAME);
725 m_freem(rec->nr_packet);
726 kfree(rec, M_NFSRVDESC);
728 TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
730 LIST_REMOVE(nuidp, nu_hash);
731 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
732 if (nuidp->nu_flag & NU_NAM)
733 FREE(nuidp->nu_nam, M_SONAME);
734 kfree((caddr_t)nuidp, M_NFSUID);
737 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
738 nnwp = nwp->nd_tq.le_next;
739 LIST_REMOVE(nwp, nd_tq);
740 kfree((caddr_t)nwp, M_NFSRVDESC);
742 LIST_INIT(&slp->ns_tq);
748 * Derefence a server socket structure. If it has no more references and
749 * is no longer valid, you can throw it away.
752 nfsrv_slpderef(struct nfssvc_sock *slp)
754 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
755 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
756 kfree((caddr_t)slp, M_NFSSVC);
761 * Lock a socket against others.
763 * Returns 0 on failure, 1 on success.
766 nfs_slplock(struct nfssvc_sock *slp, int wait)
768 mtx_t mtx = &slp->ns_solock;
771 mtx_lock_ex(mtx, "nfsslplck", 0, 0);
773 } else if (mtx_lock_ex_try(mtx) == 0) {
781 * Unlock the stream socket for others.
784 nfs_slpunlock(struct nfssvc_sock *slp)
786 mtx_t mtx = &slp->ns_solock;
792 * Initialize the data structures for the server.
793 * Handshake with any new nfsds starting up to avoid any chance of
797 nfsrv_init(int terminating)
799 struct nfssvc_sock *slp, *nslp;
801 if (nfssvc_sockhead_flag & SLP_INIT)
803 nfssvc_sockhead_flag |= SLP_INIT;
805 TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
806 if (slp->ns_flag & SLP_VALID)
808 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
809 kfree((caddr_t)slp, M_NFSSVC);
811 nfsrv_cleancache(); /* And clear out server cache */
813 nfs_pub.np_valid = 0;
815 TAILQ_INIT(&nfssvc_sockhead);
816 nfssvc_sockhead_flag &= ~SLP_INIT;
817 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
818 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
819 wakeup((caddr_t)&nfssvc_sockhead);
822 TAILQ_INIT(&nfsd_head);
823 nfsd_head_flag &= ~NFSD_CHECKSLP;
826 nfs_udpsock = (struct nfssvc_sock *)
827 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
828 mtx_init(&nfs_udpsock->ns_solock);
829 STAILQ_INIT(&nfs_udpsock->ns_rec);
830 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
831 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
833 nfs_cltpsock = (struct nfssvc_sock *)
834 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
835 mtx_init(&nfs_cltpsock->ns_solock);
836 STAILQ_INIT(&nfs_cltpsock->ns_rec);
837 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
838 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
843 * Add entries to the server monitor log.
846 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
850 rt = &nfsdrt.drt[nfsdrt.pos];
851 if (cacherep == RC_DOIT)
853 else if (cacherep == RC_REPLY)
854 rt->flag = DRT_CACHEREPLY;
856 rt->flag = DRT_CACHEDROP;
857 if (sotype == SOCK_STREAM)
859 if (nd->nd_flag & ND_NFSV3)
860 rt->flag |= DRT_NFSV3;
861 rt->proc = nd->nd_procnum;
862 if (nd->nd_nam->sa_family == AF_INET)
863 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
865 rt->ipadr = INADDR_ANY;
866 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
867 getmicrotime(&rt->tstamp);
868 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
870 #endif /* NFS_NOSERVER */
872 static int nfs_defect = 0;
873 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
876 * Get an authorization string for the uid by having the mount_nfs sitting
877 * on this mount point porpous out of the kernel and do it.
880 nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
881 struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
882 int *verf_len, NFSKERBKEY_T key /* return session key */)
886 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
887 nmp->nm_state |= NFSSTA_WANTAUTH;
888 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
890 error = nfs_sigintr(nmp, rep, rep->r_td);
892 nmp->nm_state &= ~NFSSTA_WANTAUTH;
896 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
897 nmp->nm_authstr = *auth_str = (char *)kmalloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
898 nmp->nm_authlen = RPCAUTH_MAXSIZ;
899 nmp->nm_verfstr = verf_str;
900 nmp->nm_verflen = *verf_len;
901 nmp->nm_authuid = cred->cr_uid;
902 wakeup((caddr_t)&nmp->nm_authstr);
905 * And wait for mount_nfs to do its stuff.
907 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
908 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
910 error = nfs_sigintr(nmp, rep, rep->r_td);
912 if (nmp->nm_state & NFSSTA_AUTHERR) {
913 nmp->nm_state &= ~NFSSTA_AUTHERR;
917 kfree((caddr_t)*auth_str, M_TEMP);
919 *auth_len = nmp->nm_authlen;
920 *verf_len = nmp->nm_verflen;
921 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
923 nmp->nm_state &= ~NFSSTA_HASAUTH;
924 nmp->nm_state |= NFSSTA_WAITAUTH;
925 if (nmp->nm_state & NFSSTA_WANTAUTH) {
926 nmp->nm_state &= ~NFSSTA_WANTAUTH;
927 wakeup((caddr_t)&nmp->nm_authtype);
933 * Get a nickname authenticator and verifier.
936 nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
937 int *auth_len, char *verf_str, int verf_len)
939 struct nfsuid *nuidp;
940 u_int32_t *nickp, *verfp;
941 struct timeval ktvin, ktvout;
944 if (verf_len < (4 * NFSX_UNSIGNED))
945 panic("nfs_getnickauth verf too small");
947 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
948 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
949 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
952 if (!nuidp || nuidp->nu_expire < time_second)
956 * Move to the end of the lru list (end of lru == most recently used).
958 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
959 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
961 nickp = (u_int32_t *)kmalloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
962 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
963 *nickp = txdr_unsigned(nuidp->nu_nickname);
964 *auth_str = (char *)nickp;
965 *auth_len = 2 * NFSX_UNSIGNED;
968 * Now we must encrypt the verifier and package it up.
970 verfp = (u_int32_t *)verf_str;
971 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
972 if (time_second > nuidp->nu_timestamp.tv_sec ||
973 (time_second == nuidp->nu_timestamp.tv_sec &&
974 time_second > nuidp->nu_timestamp.tv_usec))
975 getmicrotime(&nuidp->nu_timestamp);
977 nuidp->nu_timestamp.tv_usec++;
978 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
979 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
982 * Now encrypt the timestamp verifier in ecb mode using the session
989 *verfp++ = ktvout.tv_sec;
990 *verfp++ = ktvout.tv_usec;
996 * Save the current nickname in a hash list entry on the mount point.
999 nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1000 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1003 struct nfsuid *nuidp;
1005 struct timeval ktvin, ktvout;
1007 int deltasec, error = 0;
1008 struct nfsm_info info;
1014 if (len == (3 * NFSX_UNSIGNED)) {
1015 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
1016 ktvin.tv_sec = *tl++;
1017 ktvin.tv_usec = *tl++;
1018 nick = fxdr_unsigned(u_int32_t, *tl);
1021 * Decrypt the timestamp in ecb mode.
1026 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1027 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1028 deltasec = time_second - ktvout.tv_sec;
1030 deltasec = -deltasec;
1032 * If ok, add it to the hash list for the mount point.
1034 if (deltasec <= NFS_KERBCLOCKSKEW) {
1035 if (nmp->nm_numuids < nuidhash_max) {
1037 nuidp = (struct nfsuid *)
1038 kmalloc(sizeof (struct nfsuid), M_NFSUID,
1041 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1042 LIST_REMOVE(nuidp, nu_hash);
1043 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1047 nuidp->nu_cr.cr_uid = cred->cr_uid;
1048 nuidp->nu_expire = time_second + NFS_KERBTTL;
1049 nuidp->nu_timestamp = ktvout;
1050 nuidp->nu_nickname = nick;
1051 bcopy(key, nuidp->nu_key, sizeof (key));
1052 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1054 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1058 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));