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 int nfs_soreserve = NFS_MAXPACKET * NFS_MAXASYNCBIO;
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
127 sys_nfssvc(struct nfssvc_args *uap)
130 struct nlookupdata nd;
132 struct sockaddr *nam;
133 struct nfsd_args nfsdarg;
134 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
135 struct nfsd_cargs ncd;
137 struct nfssvc_sock *slp;
138 struct nfsuid *nuidp;
139 struct nfsmount *nmp;
141 #endif /* NFS_NOSERVER */
143 struct thread *td = curthread;
148 error = priv_check(td, PRIV_ROOT);
153 while (nfssvc_sockhead_flag & SLP_INIT) {
154 nfssvc_sockhead_flag |= SLP_WANTINIT;
155 tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
157 if (uap->flag & NFSSVC_BIOD)
158 error = ENXIO; /* no longer need nfsiod's */
162 #else /* !NFS_NOSERVER */
163 else if (uap->flag & NFSSVC_MNTD) {
164 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
168 error = nlookup_init(&nd, ncd.ncd_dirp, UIO_USERSPACE,
171 error = nlookup(&nd);
173 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
178 if ((vp->v_flag & VROOT) == 0)
180 nmp = VFSTONFS(vp->v_mount);
184 if ((nmp->nm_state & NFSSTA_MNTD) &&
185 (uap->flag & NFSSVC_GOTAUTH) == 0) {
189 nmp->nm_state |= NFSSTA_MNTD;
190 error = nfs_clientd(nmp, td->td_proc->p_ucred, &ncd, uap->flag,
192 } else if (uap->flag & NFSSVC_ADDSOCK) {
193 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
196 error = holdsock(td->td_proc->p_fd, nfsdarg.sock, &fp);
200 * Get the client address for connected sockets.
202 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
205 error = getsockaddr(&nam, nfsdarg.name,
212 error = nfssvc_addsock(fp, nam, td);
215 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
218 if ((uap->flag & NFSSVC_AUTHIN) &&
219 ((nfsd = nsd->nsd_nfsd)) != NULL &&
220 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
221 slp = nfsd->nfsd_slp;
224 * First check to see if another nfsd has already
225 * added this credential.
227 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
228 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
229 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
230 (!nfsd->nfsd_nd->nd_nam2 ||
231 netaddr_match(NU_NETFAM(nuidp),
232 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
236 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
237 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
242 if (slp->ns_numuids < nuidhash_max) {
244 nuidp = (struct nfsuid *)
245 kmalloc(sizeof (struct nfsuid), M_NFSUID,
249 if ((slp->ns_flag & SLP_VALID) == 0) {
251 kfree((caddr_t)nuidp, M_NFSUID);
254 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
255 LIST_REMOVE(nuidp, nu_hash);
256 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
258 if (nuidp->nu_flag & NU_NAM)
259 FREE(nuidp->nu_nam, M_SONAME);
262 nuidp->nu_cr = nsd->nsd_cr;
263 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
264 nuidp->nu_cr.cr_ngroups = NGROUPS;
265 nuidp->nu_cr.cr_ref = 1;
266 nuidp->nu_timestamp = nsd->nsd_timestamp;
267 nuidp->nu_expire = time_second + nsd->nsd_ttl;
269 * and save the session key in nu_key.
271 bcopy(nsd->nsd_key, nuidp->nu_key,
272 sizeof (nsd->nsd_key));
273 if (nfsd->nfsd_nd->nd_nam2) {
274 struct sockaddr_in *saddr;
276 saddr = (struct sockaddr_in *)
277 nfsd->nfsd_nd->nd_nam2;
278 switch (saddr->sin_family) {
280 nuidp->nu_flag |= NU_INETADDR;
282 saddr->sin_addr.s_addr;
286 nuidp->nu_flag |= NU_NAM;
288 dup_sockaddr(nfsd->nfsd_nd->nd_nam2);
292 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
294 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
296 nfsrv_setcred(&nuidp->nu_cr,
297 &nfsd->nfsd_nd->nd_cr);
298 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
302 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
303 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
304 error = nfssvc_nfsd(nsd, uap->argp, td);
306 #endif /* NFS_NOSERVER */
307 if (error == EINTR || error == ERESTART)
316 * Adds a socket to the list for servicing by nfsds.
319 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
322 struct nfssvc_sock *slp;
326 so = (struct socket *)fp->f_data;
330 * Add it to the list, as required.
332 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
334 if (tslp->ns_flag & SLP_VALID) {
336 FREE(mynam, M_SONAME);
342 * Reserve buffer space in the socket. Note that due to bugs in
343 * Linux's delayed-ack code, serious performance degredation may
344 * occur with linux hosts if the minimum is used.
346 * NFS sockets are not limited to the standard sb_max or by
349 if (so->so_type == SOCK_STREAM)
350 siz = NFS_MAXPACKET + sizeof (u_long);
353 if (siz < nfs_soreserve)
356 error = soreserve(so, siz, siz, NULL);
359 FREE(mynam, M_SONAME);
364 * Set protocol specific options { for now TCP only } and
365 * reserve some space. For datagram sockets, this can get called
366 * repeatedly for the same socket, but that isn't harmful.
368 if (so->so_type == SOCK_STREAM) {
372 bzero(&sopt, sizeof sopt);
373 sopt.sopt_level = SOL_SOCKET;
374 sopt.sopt_name = SO_KEEPALIVE;
375 sopt.sopt_val = &val;
376 sopt.sopt_valsize = sizeof val;
380 if (so->so_proto->pr_domain->dom_family == AF_INET &&
381 so->so_proto->pr_protocol == IPPROTO_TCP) {
385 bzero(&sopt, sizeof sopt);
386 sopt.sopt_level = IPPROTO_TCP;
387 sopt.sopt_name = TCP_NODELAY;
388 sopt.sopt_val = &val;
389 sopt.sopt_valsize = sizeof val;
393 so->so_rcv.ssb_flags &= ~SSB_NOINTR;
394 so->so_rcv.ssb_timeo = 0;
395 so->so_snd.ssb_flags &= ~SSB_NOINTR;
396 so->so_snd.ssb_timeo = 0;
398 slp = (struct nfssvc_sock *)kmalloc(sizeof (struct nfssvc_sock),
399 M_NFSSVC, M_WAITOK | M_ZERO);
400 mtx_init(&slp->ns_solock);
401 STAILQ_INIT(&slp->ns_rec);
402 TAILQ_INIT(&slp->ns_uidlruhead);
403 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
410 so->so_upcallarg = (caddr_t)slp;
411 so->so_upcall = nfsrv_rcv;
412 so->so_rcv.ssb_flags |= SSB_UPCALL;
413 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
414 nfsrv_wakenfsd(slp, 1);
420 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
421 * until it is killed by a signal.
424 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
427 struct nfssvc_sock *slp;
428 struct nfsd *nfsd = nsd->nsd_nfsd;
429 struct nfsrv_descript *nd = NULL;
430 struct mbuf *m, *mreq;
431 int error = 0, cacherep, sotype, writes_todo;
440 nsd->nsd_nfsd = nfsd = (struct nfsd *)
441 kmalloc(sizeof (struct nfsd), M_NFSD, M_WAITOK|M_ZERO);
444 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
450 * Loop getting rpc requests until SIGKILL.
453 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
454 while (nfsd->nfsd_slp == NULL &&
455 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
456 nfsd->nfsd_flag |= NFSD_WAITING;
458 error = tsleep((caddr_t)nfsd, PCATCH, "nfsd", 0);
463 if (nfsd->nfsd_slp == NULL &&
464 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
465 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
466 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
467 == (SLP_VALID | SLP_DOREC)) {
468 slp->ns_flag &= ~SLP_DOREC;
470 nfsd->nfsd_slp = slp;
475 nfsd_head_flag &= ~NFSD_CHECKSLP;
477 if ((slp = nfsd->nfsd_slp) == NULL)
479 if (slp->ns_flag & SLP_VALID) {
480 if (slp->ns_flag & SLP_DISCONN)
482 else if (slp->ns_flag & SLP_NEEDQ) {
483 slp->ns_flag &= ~SLP_NEEDQ;
484 (void) nfs_slplock(slp, 1);
485 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
489 error = nfsrv_dorec(slp, nfsd, &nd);
490 cur_usec = nfs_curusec();
491 if (error && slp->ns_tq.lh_first &&
492 slp->ns_tq.lh_first->nd_time <= cur_usec) {
498 nfsd->nfsd_flag |= NFSD_REQINPROG;
502 slp = nfsd->nfsd_slp;
504 if (error || (slp->ns_flag & SLP_VALID) == 0) {
506 kfree((caddr_t)nd, M_NFSRVDESC);
509 nfsd->nfsd_slp = NULL;
510 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
515 sotype = slp->ns_so->so_type;
517 getmicrotime(&nd->nd_starttime);
519 nd->nd_nam = nd->nd_nam2;
521 nd->nd_nam = slp->ns_nam;
524 * Check to see if authorization is needed.
526 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
527 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
529 ((struct sockaddr_in *)
530 nd->nd_nam)->sin_addr.s_addr;
531 nsd->nsd_authlen = nfsd->nfsd_authlen;
532 nsd->nsd_verflen = nfsd->nfsd_verflen;
533 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
534 nfsd->nfsd_authlen) &&
535 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
536 nfsd->nfsd_verflen) &&
537 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
539 cacherep = RC_DROPIT;
541 cacherep = nfsrv_getcache(nd, slp, &mreq);
544 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
545 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
546 nd->nd_procnum = NFSPROC_NOOP;
547 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
549 } else if (nfs_privport) {
550 /* Check if source port is privileged */
552 struct sockaddr *nam = nd->nd_nam;
553 struct sockaddr_in *sin;
555 sin = (struct sockaddr_in *)nam;
556 port = ntohs(sin->sin_port);
557 if (port >= IPPORT_RESERVED &&
558 nd->nd_procnum != NFSPROC_NULL) {
559 nd->nd_procnum = NFSPROC_NOOP;
560 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
562 kprintf("NFS request from unprivileged port (%s:%d)\n",
563 inet_ntoa(sin->sin_addr), port);
570 * Loop to get all the write rpc relies that have been
576 if (nd && (nd->nd_flag & ND_NFSV3))
577 procrastinate = nfsrvw_procrastinate_v3;
579 procrastinate = nfsrvw_procrastinate;
580 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
583 error = nfsrv_writegather(&nd, slp,
584 nfsd->nfsd_td, &mreq);
586 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
587 slp, nfsd->nfsd_td, &mreq);
591 if (error != 0 && error != NFSERR_RETVOID) {
592 if (nd->nd_procnum != NQNFSPROC_VACATED)
594 nfsrv_updatecache(nd, FALSE, mreq);
596 FREE(nd->nd_nam2, M_SONAME);
599 nfsstats.srvrpccnt[nd->nd_procnum]++;
600 nfsrv_updatecache(nd, TRUE, mreq);
609 if (siz <= 0 || siz > NFS_MAXPACKET) {
610 kprintf("mbuf siz=%d\n",siz);
611 panic("Bad nfs svc reply");
614 m->m_pkthdr.len = siz;
615 m->m_pkthdr.rcvif = NULL;
617 * For stream protocols, prepend a Sun RPC
620 if (sotype == SOCK_STREAM) {
621 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
624 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
626 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
627 (void) nfs_slplock(slp, 1);
628 if (slp->ns_flag & SLP_VALID)
629 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
635 nfsd_rt(sotype, nd, cacherep);
637 FREE(nd->nd_nam2, M_SONAME);
639 m_freem(nd->nd_mrep);
642 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
644 if (error == EINTR || error == ERESTART) {
645 kfree((caddr_t)nd, M_NFSRVDESC);
653 nfsd_rt(sotype, nd, cacherep);
654 m_freem(nd->nd_mrep);
656 FREE(nd->nd_nam2, M_SONAME);
660 FREE((caddr_t)nd, M_NFSRVDESC);
665 * Check to see if there are outstanding writes that
666 * need to be serviced.
668 cur_usec = nfs_curusec();
670 if (slp->ns_tq.lh_first &&
671 slp->ns_tq.lh_first->nd_time <= cur_usec) {
677 } while (writes_todo);
679 if (nfsrv_dorec(slp, nfsd, &nd)) {
680 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
681 nfsd->nfsd_slp = NULL;
686 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
688 kfree((caddr_t)nfsd, M_NFSD);
689 nsd->nsd_nfsd = NULL;
690 if (--nfs_numnfsd == 0)
691 nfsrv_init(TRUE); /* Reinitialize everything */
696 * Shut down a socket associated with an nfssvc_sock structure.
697 * Should be called with the send lock set, if required.
698 * The trick here is to increment the sref at the start, so that the nfsds
699 * will stop using it and clear ns_flag at the end so that it will not be
700 * reassigned during cleanup.
703 nfsrv_zapsock(struct nfssvc_sock *slp)
705 struct nfsuid *nuidp, *nnuidp;
706 struct nfsrv_descript *nwp, *nnwp;
709 struct nfsrv_rec *rec;
711 slp->ns_flag &= ~SLP_ALLFLAGS;
716 so->so_rcv.ssb_flags &= ~SSB_UPCALL;
717 so->so_upcall = NULL;
718 so->so_upcallarg = NULL;
719 soshutdown(so, SHUT_RDWR);
722 FREE(slp->ns_nam, M_SONAME);
723 m_freem(slp->ns_raw);
724 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
726 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
728 FREE(rec->nr_address, M_SONAME);
729 m_freem(rec->nr_packet);
730 kfree(rec, M_NFSRVDESC);
732 TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
734 LIST_REMOVE(nuidp, nu_hash);
735 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
736 if (nuidp->nu_flag & NU_NAM)
737 FREE(nuidp->nu_nam, M_SONAME);
738 kfree((caddr_t)nuidp, M_NFSUID);
741 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
742 nnwp = nwp->nd_tq.le_next;
743 LIST_REMOVE(nwp, nd_tq);
744 kfree((caddr_t)nwp, M_NFSRVDESC);
746 LIST_INIT(&slp->ns_tq);
752 * Derefence a server socket structure. If it has no more references and
753 * is no longer valid, you can throw it away.
756 nfsrv_slpderef(struct nfssvc_sock *slp)
758 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
759 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
760 kfree((caddr_t)slp, M_NFSSVC);
765 * Lock a socket against others.
767 * Returns 0 on failure, 1 on success.
770 nfs_slplock(struct nfssvc_sock *slp, int wait)
772 mtx_t mtx = &slp->ns_solock;
775 mtx_lock_ex(mtx, "nfsslplck", 0, 0);
777 } else if (mtx_lock_ex_try(mtx) == 0) {
785 * Unlock the stream socket for others.
788 nfs_slpunlock(struct nfssvc_sock *slp)
790 mtx_t mtx = &slp->ns_solock;
796 * Initialize the data structures for the server.
797 * Handshake with any new nfsds starting up to avoid any chance of
801 nfsrv_init(int terminating)
803 struct nfssvc_sock *slp, *nslp;
805 if (nfssvc_sockhead_flag & SLP_INIT)
807 nfssvc_sockhead_flag |= SLP_INIT;
809 TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
810 if (slp->ns_flag & SLP_VALID)
812 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
813 kfree((caddr_t)slp, M_NFSSVC);
815 nfsrv_cleancache(); /* And clear out server cache */
817 nfs_pub.np_valid = 0;
819 TAILQ_INIT(&nfssvc_sockhead);
820 nfssvc_sockhead_flag &= ~SLP_INIT;
821 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
822 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
823 wakeup((caddr_t)&nfssvc_sockhead);
826 TAILQ_INIT(&nfsd_head);
827 nfsd_head_flag &= ~NFSD_CHECKSLP;
830 nfs_udpsock = (struct nfssvc_sock *)
831 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
832 mtx_init(&nfs_udpsock->ns_solock);
833 STAILQ_INIT(&nfs_udpsock->ns_rec);
834 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
835 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
837 nfs_cltpsock = (struct nfssvc_sock *)
838 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
839 mtx_init(&nfs_cltpsock->ns_solock);
840 STAILQ_INIT(&nfs_cltpsock->ns_rec);
841 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
842 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
847 * Add entries to the server monitor log.
850 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
854 rt = &nfsdrt.drt[nfsdrt.pos];
855 if (cacherep == RC_DOIT)
857 else if (cacherep == RC_REPLY)
858 rt->flag = DRT_CACHEREPLY;
860 rt->flag = DRT_CACHEDROP;
861 if (sotype == SOCK_STREAM)
863 if (nd->nd_flag & ND_NFSV3)
864 rt->flag |= DRT_NFSV3;
865 rt->proc = nd->nd_procnum;
866 if (nd->nd_nam->sa_family == AF_INET)
867 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
869 rt->ipadr = INADDR_ANY;
870 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
871 getmicrotime(&rt->tstamp);
872 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
874 #endif /* NFS_NOSERVER */
876 static int nfs_defect = 0;
877 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
880 * Get an authorization string for the uid by having the mount_nfs sitting
881 * on this mount point porpous out of the kernel and do it.
884 nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
885 struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
886 int *verf_len, NFSKERBKEY_T key /* return session key */)
890 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
891 nmp->nm_state |= NFSSTA_WANTAUTH;
892 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
894 error = nfs_sigintr(nmp, rep, rep->r_td);
896 nmp->nm_state &= ~NFSSTA_WANTAUTH;
900 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
901 nmp->nm_authstr = *auth_str = (char *)kmalloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
902 nmp->nm_authlen = RPCAUTH_MAXSIZ;
903 nmp->nm_verfstr = verf_str;
904 nmp->nm_verflen = *verf_len;
905 nmp->nm_authuid = cred->cr_uid;
906 wakeup((caddr_t)&nmp->nm_authstr);
909 * And wait for mount_nfs to do its stuff.
911 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
912 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
914 error = nfs_sigintr(nmp, rep, rep->r_td);
916 if (nmp->nm_state & NFSSTA_AUTHERR) {
917 nmp->nm_state &= ~NFSSTA_AUTHERR;
921 kfree((caddr_t)*auth_str, M_TEMP);
923 *auth_len = nmp->nm_authlen;
924 *verf_len = nmp->nm_verflen;
925 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
927 nmp->nm_state &= ~NFSSTA_HASAUTH;
928 nmp->nm_state |= NFSSTA_WAITAUTH;
929 if (nmp->nm_state & NFSSTA_WANTAUTH) {
930 nmp->nm_state &= ~NFSSTA_WANTAUTH;
931 wakeup((caddr_t)&nmp->nm_authtype);
937 * Get a nickname authenticator and verifier.
940 nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
941 int *auth_len, char *verf_str, int verf_len)
943 struct nfsuid *nuidp;
944 u_int32_t *nickp, *verfp;
945 struct timeval ktvin, ktvout;
948 if (verf_len < (4 * NFSX_UNSIGNED))
949 panic("nfs_getnickauth verf too small");
951 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
952 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
953 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
956 if (!nuidp || nuidp->nu_expire < time_second)
960 * Move to the end of the lru list (end of lru == most recently used).
962 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
963 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
965 nickp = (u_int32_t *)kmalloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
966 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
967 *nickp = txdr_unsigned(nuidp->nu_nickname);
968 *auth_str = (char *)nickp;
969 *auth_len = 2 * NFSX_UNSIGNED;
972 * Now we must encrypt the verifier and package it up.
974 verfp = (u_int32_t *)verf_str;
975 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
976 if (time_second > nuidp->nu_timestamp.tv_sec ||
977 (time_second == nuidp->nu_timestamp.tv_sec &&
978 time_second > nuidp->nu_timestamp.tv_usec))
979 getmicrotime(&nuidp->nu_timestamp);
981 nuidp->nu_timestamp.tv_usec++;
982 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
983 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
986 * Now encrypt the timestamp verifier in ecb mode using the session
996 *verfp++ = ktvout.tv_sec;
997 *verfp++ = ktvout.tv_usec;
1003 * Save the current nickname in a hash list entry on the mount point.
1006 nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1007 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1010 struct nfsuid *nuidp;
1012 struct timeval ktvin, ktvout;
1014 int deltasec, error = 0;
1015 struct nfsm_info info;
1021 if (len == (3 * NFSX_UNSIGNED)) {
1022 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
1023 ktvin.tv_sec = *tl++;
1024 ktvin.tv_usec = *tl++;
1025 nick = fxdr_unsigned(u_int32_t, *tl);
1028 * Decrypt the timestamp in ecb mode.
1036 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1037 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1038 deltasec = time_second - ktvout.tv_sec;
1040 deltasec = -deltasec;
1042 * If ok, add it to the hash list for the mount point.
1044 if (deltasec <= NFS_KERBCLOCKSKEW) {
1045 if (nmp->nm_numuids < nuidhash_max) {
1047 nuidp = (struct nfsuid *)
1048 kmalloc(sizeof (struct nfsuid), M_NFSUID,
1051 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1052 LIST_REMOVE(nuidp, nu_hash);
1053 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1057 nuidp->nu_cr.cr_uid = cred->cr_uid;
1058 nuidp->nu_expire = time_second + NFS_KERBTTL;
1059 nuidp->nu_timestamp = ktvout;
1060 nuidp->nu_nickname = nick;
1061 bcopy(key, nuidp->nu_key, sizeof (key));
1062 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1064 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1068 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));