nfs_nfsiod.c revision 25307
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 *
36 *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
37 * $Id: nfs_syscalls.c,v 1.21 1997/04/27 20:01:23 wollman Exp $
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/sysproto.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45#include <sys/file.h>
46#include <sys/filedesc.h>
47#include <sys/stat.h>
48#include <sys/vnode.h>
49#include <sys/mount.h>
50#include <sys/proc.h>
51#include <sys/uio.h>
52#include <sys/malloc.h>
53#include <sys/buf.h>
54#include <sys/mbuf.h>
55#include <sys/socket.h>
56#include <sys/socketvar.h>
57#include <sys/domain.h>
58#include <sys/protosw.h>
59#include <sys/namei.h>
60#include <sys/syslog.h>
61
62#include <netinet/in.h>
63#include <netinet/tcp.h>
64#ifdef ISO
65#include <netiso/iso.h>
66#endif
67#include <nfs/xdr_subs.h>
68#include <nfs/rpcv2.h>
69#include <nfs/nfsproto.h>
70#include <nfs/nfs.h>
71#include <nfs/nfsm_subs.h>
72#include <nfs/nfsrvcache.h>
73#include <nfs/nfsmount.h>
74#include <nfs/nfsnode.h>
75#include <nfs/nqnfs.h>
76#include <nfs/nfsrtt.h>
77
78/* Global defs. */
79extern int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *nd,
80					    struct nfssvc_sock *slp,
81					    struct proc *procp,
82					    struct mbuf **mreqp));
83extern int nfs_numasync;
84extern time_t nqnfsstarttime;
85extern int nqsrv_writeslack;
86extern int nfsrtton;
87extern struct nfsstats nfsstats;
88extern int nfsrvw_procrastinate;
89struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock;
90static int nuidhash_max = NFS_MAXUIDHASH;
91
92static void	nfsrv_zapsock __P((struct nfssvc_sock *slp));
93static int	nfssvc_iod __P((struct proc *));
94
95#define	TRUE	1
96#define	FALSE	0
97
98static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
99
100#ifndef NFS_NOSERVER
101int nfsd_waiting = 0;
102static struct nfsdrt nfsdrt;
103static int nfs_numnfsd = 0;
104static int notstarted = 1;
105static int modify_flag = 0;
106static void	nfsd_rt __P((int sotype, struct nfsrv_descript *nd,
107			     int cacherep));
108static int	nfssvc_addsock __P((struct file *, struct mbuf *,
109				    struct proc *));
110static int	nfssvc_nfsd __P((struct nfsd_srvargs *,caddr_t,struct proc *));
111
112static int nfs_privport = 0;
113SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
114
115/*
116 * NFS server system calls
117 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
118 */
119
120/*
121 * Get file handle system call
122 */
123#ifndef _SYS_SYSPROTO_H_
124struct getfh_args {
125	char	*fname;
126	fhandle_t *fhp;
127};
128#endif
129int
130getfh(p, uap, retval)
131	struct proc *p;
132	register struct getfh_args *uap;
133	int *retval;
134{
135	register struct vnode *vp;
136	fhandle_t fh;
137	int error;
138	struct nameidata nd;
139
140	/*
141	 * Must be super user
142	 */
143	error = suser(p->p_ucred, &p->p_acflag);
144	if(error)
145		return (error);
146	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
147	error = namei(&nd);
148	if (error)
149		return (error);
150	vp = nd.ni_vp;
151	bzero((caddr_t)&fh, sizeof(fh));
152	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
153	error = VFS_VPTOFH(vp, &fh.fh_fid);
154	vput(vp);
155	if (error)
156		return (error);
157	error = copyout((caddr_t)&fh, (caddr_t)uap->fhp, sizeof (fh));
158	return (error);
159}
160
161#endif /* NFS_NOSERVER */
162/*
163 * Nfs server psuedo system call for the nfsd's
164 * Based on the flag value it either:
165 * - adds a socket to the selection list
166 * - remains in the kernel as an nfsd
167 * - remains in the kernel as an nfsiod
168 */
169#ifndef _SYS_SYSPROTO_H_
170struct nfssvc_args {
171	int flag;
172	caddr_t argp;
173};
174#endif
175int
176nfssvc(p, uap, retval)
177	struct proc *p;
178	register struct nfssvc_args *uap;
179	int *retval;
180{
181#ifndef NFS_NOSERVER
182	struct nameidata nd;
183	struct file *fp;
184	struct mbuf *nam;
185	struct nfsd_args nfsdarg;
186	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
187	struct nfsd_cargs ncd;
188	struct nfsd *nfsd;
189	struct nfssvc_sock *slp;
190	struct nfsuid *nuidp;
191	struct nfsmount *nmp;
192#endif /* NFS_NOSERVER */
193	int error;
194
195	/*
196	 * Must be super user
197	 */
198	error = suser(p->p_ucred, &p->p_acflag);
199	if(error)
200		return (error);
201	while (nfssvc_sockhead_flag & SLP_INIT) {
202		 nfssvc_sockhead_flag |= SLP_WANTINIT;
203		(void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
204	}
205	if (uap->flag & NFSSVC_BIOD)
206		error = nfssvc_iod(p);
207#ifdef NFS_NOSERVER
208	else
209		error = ENXIO;
210#else /* !NFS_NOSERVER */
211	else if (uap->flag & NFSSVC_MNTD) {
212		error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
213		if (error)
214			return (error);
215		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
216			ncd.ncd_dirp, p);
217		error = namei(&nd);
218		if (error)
219			return (error);
220		if ((nd.ni_vp->v_flag & VROOT) == 0)
221			error = EINVAL;
222		nmp = VFSTONFS(nd.ni_vp->v_mount);
223		vput(nd.ni_vp);
224		if (error)
225			return (error);
226		if ((nmp->nm_flag & NFSMNT_MNTD) &&
227			(uap->flag & NFSSVC_GOTAUTH) == 0)
228			return (0);
229		nmp->nm_flag |= NFSMNT_MNTD;
230		error = nqnfs_clientd(nmp, p->p_ucred, &ncd, uap->flag,
231			uap->argp, p);
232	} else if (uap->flag & NFSSVC_ADDSOCK) {
233		error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
234		if (error)
235			return (error);
236		error = getsock(p->p_fd, nfsdarg.sock, &fp);
237		if (error)
238			return (error);
239		/*
240		 * Get the client address for connected sockets.
241		 */
242		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
243			nam = (struct mbuf *)0;
244		else {
245			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
246				MT_SONAME);
247			if (error)
248				return (error);
249		}
250		error = nfssvc_addsock(fp, nam, p);
251	} else {
252		error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
253		if (error)
254			return (error);
255		if ((uap->flag & NFSSVC_AUTHIN) && ((nfsd = nsd->nsd_nfsd)) &&
256			(nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
257			slp = nfsd->nfsd_slp;
258
259			/*
260			 * First check to see if another nfsd has already
261			 * added this credential.
262			 */
263			for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
264			    nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
265				if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
266				    (!nfsd->nfsd_nd->nd_nam2 ||
267				     netaddr_match(NU_NETFAM(nuidp),
268				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
269					break;
270			}
271			if (nuidp) {
272			    nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
273			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
274			} else {
275			    /*
276			     * Nope, so we will.
277			     */
278			    if (slp->ns_numuids < nuidhash_max) {
279				slp->ns_numuids++;
280				nuidp = (struct nfsuid *)
281				   malloc(sizeof (struct nfsuid), M_NFSUID,
282					M_WAITOK);
283			    } else
284				nuidp = (struct nfsuid *)0;
285			    if ((slp->ns_flag & SLP_VALID) == 0) {
286				if (nuidp)
287				    free((caddr_t)nuidp, M_NFSUID);
288			    } else {
289				if (nuidp == (struct nfsuid *)0) {
290				    nuidp = slp->ns_uidlruhead.tqh_first;
291				    LIST_REMOVE(nuidp, nu_hash);
292				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
293					nu_lru);
294				    if (nuidp->nu_flag & NU_NAM)
295					m_freem(nuidp->nu_nam);
296			        }
297				nuidp->nu_flag = 0;
298				nuidp->nu_cr = nsd->nsd_cr;
299				if (nuidp->nu_cr.cr_ngroups > NGROUPS)
300				    nuidp->nu_cr.cr_ngroups = NGROUPS;
301				nuidp->nu_cr.cr_ref = 1;
302				nuidp->nu_timestamp = nsd->nsd_timestamp;
303				nuidp->nu_expire = time.tv_sec + nsd->nsd_ttl;
304				/*
305				 * and save the session key in nu_key.
306				 */
307				bcopy(nsd->nsd_key, nuidp->nu_key,
308				    sizeof (nsd->nsd_key));
309				if (nfsd->nfsd_nd->nd_nam2) {
310				    struct sockaddr_in *saddr;
311
312				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
313					 struct sockaddr_in *);
314				    switch (saddr->sin_family) {
315				    case AF_INET:
316					nuidp->nu_flag |= NU_INETADDR;
317					nuidp->nu_inetaddr =
318					     saddr->sin_addr.s_addr;
319					break;
320				    case AF_ISO:
321				    default:
322					nuidp->nu_flag |= NU_NAM;
323					nuidp->nu_nam = m_copym(
324					    nfsd->nfsd_nd->nd_nam2, 0,
325					     M_COPYALL, M_WAIT);
326					break;
327				    };
328				}
329				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
330					nu_lru);
331				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
332					nuidp, nu_hash);
333				nfsrv_setcred(&nuidp->nu_cr,
334				    &nfsd->nfsd_nd->nd_cr);
335				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
336			    }
337			}
338		}
339		if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
340			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
341		error = nfssvc_nfsd(nsd, uap->argp, p);
342	}
343#endif /* NFS_NOSERVER */
344	if (error == EINTR || error == ERESTART)
345		error = 0;
346	return (error);
347}
348
349#ifndef NFS_NOSERVER
350/*
351 * Adds a socket to the list for servicing by nfsds.
352 */
353static int
354nfssvc_addsock(fp, mynam, p)
355	struct file *fp;
356	struct mbuf *mynam;
357	struct proc *p;
358{
359	register struct mbuf *m;
360	register int siz;
361	register struct nfssvc_sock *slp;
362	register struct socket *so;
363	struct nfssvc_sock *tslp;
364	int error, s;
365
366	so = (struct socket *)fp->f_data;
367	tslp = (struct nfssvc_sock *)0;
368	/*
369	 * Add it to the list, as required.
370	 */
371	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
372		tslp = nfs_udpsock;
373		if (tslp->ns_flag & SLP_VALID) {
374			m_freem(mynam);
375			return (EPERM);
376		}
377#ifdef ISO
378	} else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
379		tslp = nfs_cltpsock;
380		if (tslp->ns_flag & SLP_VALID) {
381			m_freem(mynam);
382			return (EPERM);
383		}
384#endif /* ISO */
385	}
386	if (so->so_type == SOCK_STREAM)
387		siz = NFS_MAXPACKET + sizeof (u_long);
388	else
389		siz = NFS_MAXPACKET;
390	error = soreserve(so, siz, siz);
391	if (error) {
392		m_freem(mynam);
393		return (error);
394	}
395
396	/*
397	 * Set protocol specific options { for now TCP only } and
398	 * reserve some space. For datagram sockets, this can get called
399	 * repeatedly for the same socket, but that isn't harmful.
400	 */
401	if (so->so_type == SOCK_STREAM) {
402		MGET(m, M_WAIT, MT_SOOPTS);
403		*mtod(m, int *) = 1;
404		m->m_len = sizeof(int);
405		sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m, p);
406	}
407	if (so->so_proto->pr_domain->dom_family == AF_INET &&
408	    so->so_proto->pr_protocol == IPPROTO_TCP) {
409		MGET(m, M_WAIT, MT_SOOPTS);
410		*mtod(m, int *) = 1;
411		m->m_len = sizeof(int);
412		sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m, p);
413	}
414	so->so_rcv.sb_flags &= ~SB_NOINTR;
415	so->so_rcv.sb_timeo = 0;
416	so->so_snd.sb_flags &= ~SB_NOINTR;
417	so->so_snd.sb_timeo = 0;
418	if (tslp)
419		slp = tslp;
420	else {
421		slp = (struct nfssvc_sock *)
422			malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
423		bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
424		TAILQ_INIT(&slp->ns_uidlruhead);
425		TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
426	}
427	slp->ns_so = so;
428	slp->ns_nam = mynam;
429	fp->f_count++;
430	slp->ns_fp = fp;
431	s = splnet();
432	so->so_upcallarg = (caddr_t)slp;
433	so->so_upcall = nfsrv_rcv;
434	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
435	nfsrv_wakenfsd(slp);
436	splx(s);
437	return (0);
438}
439
440/*
441 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
442 * until it is killed by a signal.
443 */
444static int
445nfssvc_nfsd(nsd, argp, p)
446	struct nfsd_srvargs *nsd;
447	caddr_t argp;
448	struct proc *p;
449{
450	register struct mbuf *m;
451	register int siz;
452	register struct nfssvc_sock *slp;
453	register struct socket *so;
454	register int *solockp;
455	struct nfsd *nfsd = nsd->nsd_nfsd;
456	struct nfsrv_descript *nd = NULL;
457	struct mbuf *mreq;
458	int error = 0, cacherep, s, sotype, writes_todo;
459	u_quad_t cur_usec;
460
461#ifndef nolint
462	cacherep = RC_DOIT;
463	writes_todo = 0;
464#endif
465	s = splnet();
466	if (nfsd == (struct nfsd *)0) {
467		nsd->nsd_nfsd = nfsd = (struct nfsd *)
468			malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
469		bzero((caddr_t)nfsd, sizeof (struct nfsd));
470		nfsd->nfsd_procp = p;
471		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
472		nfs_numnfsd++;
473	}
474	/*
475	 * Loop getting rpc requests until SIGKILL.
476	 */
477	for (;;) {
478		if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
479			while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
480			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
481				nfsd->nfsd_flag |= NFSD_WAITING;
482				nfsd_waiting++;
483				error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
484				    "nfsd", 0);
485				nfsd_waiting--;
486				if (error)
487					goto done;
488			}
489			if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
490			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
491				for (slp = nfssvc_sockhead.tqh_first; slp != 0;
492				    slp = slp->ns_chain.tqe_next) {
493				    if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
494					== (SLP_VALID | SLP_DOREC)) {
495					    slp->ns_flag &= ~SLP_DOREC;
496					    slp->ns_sref++;
497					    nfsd->nfsd_slp = slp;
498					    break;
499				    }
500				}
501				if (slp == 0)
502					nfsd_head_flag &= ~NFSD_CHECKSLP;
503			}
504			if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
505				continue;
506			if (slp->ns_flag & SLP_VALID) {
507				if (slp->ns_flag & SLP_DISCONN)
508					nfsrv_zapsock(slp);
509				else if (slp->ns_flag & SLP_NEEDQ) {
510					slp->ns_flag &= ~SLP_NEEDQ;
511					(void) nfs_sndlock(&slp->ns_solock,
512						(struct nfsreq *)0);
513					nfsrv_rcv(slp->ns_so, (caddr_t)slp,
514						M_WAIT);
515					nfs_sndunlock(&slp->ns_solock);
516				}
517				error = nfsrv_dorec(slp, nfsd, &nd);
518				cur_usec = (u_quad_t)time.tv_sec * 1000000 +
519					(u_quad_t)time.tv_usec;
520				if (error && slp->ns_tq.lh_first &&
521				    slp->ns_tq.lh_first->nd_time <= cur_usec) {
522					error = 0;
523					cacherep = RC_DOIT;
524					writes_todo = 1;
525				} else
526					writes_todo = 0;
527				nfsd->nfsd_flag |= NFSD_REQINPROG;
528			}
529		} else {
530			error = 0;
531			slp = nfsd->nfsd_slp;
532		}
533		if (error || (slp->ns_flag & SLP_VALID) == 0) {
534			if (nd) {
535				free((caddr_t)nd, M_NFSRVDESC);
536				nd = NULL;
537			}
538			nfsd->nfsd_slp = (struct nfssvc_sock *)0;
539			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
540			nfsrv_slpderef(slp);
541			continue;
542		}
543		splx(s);
544		so = slp->ns_so;
545		sotype = so->so_type;
546		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
547			solockp = &slp->ns_solock;
548		else
549			solockp = (int *)0;
550		if (nd) {
551		    gettime(&nd->nd_starttime);
552		    if (nd->nd_nam2)
553			nd->nd_nam = nd->nd_nam2;
554		    else
555			nd->nd_nam = slp->ns_nam;
556
557		    /*
558		     * Check to see if authorization is needed.
559		     */
560		    if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
561			nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
562			nsd->nsd_haddr = mtod(nd->nd_nam,
563			    struct sockaddr_in *)->sin_addr.s_addr;
564			nsd->nsd_authlen = nfsd->nfsd_authlen;
565			nsd->nsd_verflen = nfsd->nfsd_verflen;
566			if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
567				nfsd->nfsd_authlen) &&
568			    !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
569				nfsd->nfsd_verflen) &&
570			    !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
571			    return (ENEEDAUTH);
572			cacherep = RC_DROPIT;
573		    } else
574			cacherep = nfsrv_getcache(nd, slp, &mreq);
575
576		    /*
577		     * Check for just starting up for NQNFS and send
578		     * fake "try again later" replies to the NQNFS clients.
579		     */
580		    if (notstarted && nqnfsstarttime <= time.tv_sec) {
581			if (modify_flag) {
582				nqnfsstarttime = time.tv_sec + nqsrv_writeslack;
583				modify_flag = 0;
584			} else
585				notstarted = 0;
586		    }
587		    if (notstarted) {
588			if ((nd->nd_flag & ND_NQNFS) == 0)
589				cacherep = RC_DROPIT;
590			else if (nd->nd_procnum != NFSPROC_WRITE) {
591				nd->nd_procnum = NFSPROC_NOOP;
592				nd->nd_repstat = NQNFS_TRYLATER;
593				cacherep = RC_DOIT;
594			} else
595				modify_flag = 1;
596		    } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
597			nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
598			nd->nd_procnum = NFSPROC_NOOP;
599			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
600			cacherep = RC_DOIT;
601		    } else if (nfs_privport) {
602			/* Check if source port is privileged */
603			u_short port;
604			u_long  addr;
605			struct mbuf *nam = nd->nd_nam;
606			struct sockaddr_in *sin;
607
608			sin = mtod(nam, struct sockaddr_in *);
609			port = ntohs(sin->sin_port);
610			if (port >= IPPORT_RESERVED &&
611			    nd->nd_procnum != NFSPROC_NULL) {
612			    nd->nd_procnum = NFSPROC_NOOP;
613			    nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
614			    cacherep = RC_DOIT;
615			    printf("NFS request from unprivileged port (%s:%d)\n",
616				   inet_ntoa(sin->sin_addr), port);
617			}
618		    }
619
620		}
621
622		/*
623		 * Loop to get all the write rpc relies that have been
624		 * gathered together.
625		 */
626		do {
627		    switch (cacherep) {
628		    case RC_DOIT:
629			if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
630			    nfsrvw_procrastinate > 0 && !notstarted))
631			    error = nfsrv_writegather(&nd, slp,
632				nfsd->nfsd_procp, &mreq);
633			else
634			    error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
635				slp, nfsd->nfsd_procp, &mreq);
636			if (mreq == NULL)
637				break;
638			if (error) {
639				if (nd->nd_procnum != NQNFSPROC_VACATED)
640					nfsstats.srv_errs++;
641				nfsrv_updatecache(nd, FALSE, mreq);
642				if (nd->nd_nam2)
643					m_freem(nd->nd_nam2);
644				break;
645			}
646			nfsstats.srvrpccnt[nd->nd_procnum]++;
647			nfsrv_updatecache(nd, TRUE, mreq);
648			nd->nd_mrep = (struct mbuf *)0;
649		    case RC_REPLY:
650			m = mreq;
651			siz = 0;
652			while (m) {
653				siz += m->m_len;
654				m = m->m_next;
655			}
656			if (siz <= 0 || siz > NFS_MAXPACKET) {
657				printf("mbuf siz=%d\n",siz);
658				panic("Bad nfs svc reply");
659			}
660			m = mreq;
661			m->m_pkthdr.len = siz;
662			m->m_pkthdr.rcvif = (struct ifnet *)0;
663			/*
664			 * For stream protocols, prepend a Sun RPC
665			 * Record Mark.
666			 */
667			if (sotype == SOCK_STREAM) {
668				M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
669				*mtod(m, u_long *) = htonl(0x80000000 | siz);
670			}
671			if (solockp)
672				(void) nfs_sndlock(solockp, (struct nfsreq *)0);
673			if (slp->ns_flag & SLP_VALID)
674			    error = nfs_send(so, nd->nd_nam2, m, NULL);
675			else {
676			    error = EPIPE;
677			    m_freem(m);
678			}
679			if (nfsrtton)
680				nfsd_rt(sotype, nd, cacherep);
681			if (nd->nd_nam2)
682				MFREE(nd->nd_nam2, m);
683			if (nd->nd_mrep)
684				m_freem(nd->nd_mrep);
685			if (error == EPIPE)
686				nfsrv_zapsock(slp);
687			if (solockp)
688				nfs_sndunlock(solockp);
689			if (error == EINTR || error == ERESTART) {
690				free((caddr_t)nd, M_NFSRVDESC);
691				nfsrv_slpderef(slp);
692				s = splnet();
693				goto done;
694			}
695			break;
696		    case RC_DROPIT:
697			if (nfsrtton)
698				nfsd_rt(sotype, nd, cacherep);
699			m_freem(nd->nd_mrep);
700			m_freem(nd->nd_nam2);
701			break;
702		    };
703		    if (nd) {
704			FREE((caddr_t)nd, M_NFSRVDESC);
705			nd = NULL;
706		    }
707
708		    /*
709		     * Check to see if there are outstanding writes that
710		     * need to be serviced.
711		     */
712		    cur_usec = (u_quad_t)time.tv_sec * 1000000 +
713			(u_quad_t)time.tv_usec;
714		    s = splsoftclock();
715		    if (slp->ns_tq.lh_first &&
716			slp->ns_tq.lh_first->nd_time <= cur_usec) {
717			cacherep = RC_DOIT;
718			writes_todo = 1;
719		    } else
720			writes_todo = 0;
721		    splx(s);
722		} while (writes_todo);
723		s = splnet();
724		if (nfsrv_dorec(slp, nfsd, &nd)) {
725			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
726			nfsd->nfsd_slp = NULL;
727			nfsrv_slpderef(slp);
728		}
729	}
730done:
731	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
732	splx(s);
733	free((caddr_t)nfsd, M_NFSD);
734	nsd->nsd_nfsd = (struct nfsd *)0;
735	if (--nfs_numnfsd == 0)
736		nfsrv_init(TRUE);	/* Reinitialize everything */
737	return (error);
738}
739#endif /* NFS_NOSERVER */
740
741int nfs_defect = 0;
742SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
743
744/*
745 * Asynchronous I/O daemons for client nfs.
746 * They do read-ahead and write-behind operations on the block I/O cache.
747 * Never returns unless it fails or gets killed.
748 */
749static int
750nfssvc_iod(p)
751	struct proc *p;
752{
753	register struct buf *bp, *nbp;
754	register int i, myiod;
755	struct vnode *vp;
756	struct nfsmount *nmp;
757	int error = 0, s;
758
759	/*
760	 * Assign my position or return error if too many already running
761	 */
762	myiod = -1;
763	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
764		if (nfs_asyncdaemon[i] == 0) {
765			nfs_asyncdaemon[i]++;
766			myiod = i;
767			break;
768		}
769	if (myiod == -1)
770		return (EBUSY);
771	nfs_numasync++;
772	/*
773	 * Just loop around doin our stuff until SIGKILL
774	 */
775	for (;;) {
776	    while (((nmp = nfs_iodmount[myiod]) == NULL
777		    || nmp->nm_bufq.tqh_first == NULL)
778		   && error == 0) {
779		if (nmp)
780		    nmp->nm_bufqiods--;
781		nfs_iodwant[myiod] = p;
782		nfs_iodmount[myiod] = NULL;
783		error = tsleep((caddr_t)&nfs_iodwant[myiod],
784			PWAIT | PCATCH, "nfsidl", 0);
785	    }
786	    if (error) {
787		nfs_asyncdaemon[myiod] = 0;
788		if (nmp) nmp->nm_bufqiods--;
789		nfs_iodmount[myiod] = NULL;
790		nfs_numasync--;
791		return (error);
792	    }
793	    while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
794		/* Take one off the front of the list */
795		TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
796		nmp->nm_bufqlen--;
797		if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) {
798		    nmp->nm_bufqwant = FALSE;
799		    wakeup(&nmp->nm_bufq);
800		}
801		if (bp->b_flags & B_READ)
802		    (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0);
803		else
804		    (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0);
805
806		/*
807		 * If there are more than one iod on this mount, then defect
808		 * so that the iods can be shared out fairly between the mounts
809		 */
810		if (nfs_defect && nmp->nm_bufqiods > 1) {
811		    NFS_DPF(ASYNCIO,
812			    ("nfssvc_iod: iod %d defecting from mount %p\n",
813			     myiod, nmp));
814		    nfs_iodmount[myiod] = NULL;
815		    nmp->nm_bufqiods--;
816		    break;
817		}
818	    }
819	}
820}
821
822/*
823 * Shut down a socket associated with an nfssvc_sock structure.
824 * Should be called with the send lock set, if required.
825 * The trick here is to increment the sref at the start, so that the nfsds
826 * will stop using it and clear ns_flag at the end so that it will not be
827 * reassigned during cleanup.
828 */
829static void
830nfsrv_zapsock(slp)
831	register struct nfssvc_sock *slp;
832{
833	register struct nfsuid *nuidp, *nnuidp;
834	register struct nfsrv_descript *nwp, *nnwp;
835	struct socket *so;
836	struct file *fp;
837	struct mbuf *m;
838	int s;
839
840	slp->ns_flag &= ~SLP_ALLFLAGS;
841	fp = slp->ns_fp;
842	if (fp) {
843		slp->ns_fp = (struct file *)0;
844		so = slp->ns_so;
845		so->so_upcall = NULL;
846		soshutdown(so, 2);
847		closef(fp, (struct proc *)0);
848		if (slp->ns_nam)
849			MFREE(slp->ns_nam, m);
850		m_freem(slp->ns_raw);
851		m_freem(slp->ns_rec);
852		for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
853		    nuidp = nnuidp) {
854			nnuidp = nuidp->nu_lru.tqe_next;
855			LIST_REMOVE(nuidp, nu_hash);
856			TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
857			if (nuidp->nu_flag & NU_NAM)
858				m_freem(nuidp->nu_nam);
859			free((caddr_t)nuidp, M_NFSUID);
860		}
861		s = splsoftclock();
862		for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
863			nnwp = nwp->nd_tq.le_next;
864			LIST_REMOVE(nwp, nd_tq);
865			free((caddr_t)nwp, M_NFSRVDESC);
866		}
867		LIST_INIT(&slp->ns_tq);
868		splx(s);
869	}
870}
871
872/*
873 * Get an authorization string for the uid by having the mount_nfs sitting
874 * on this mount point porpous out of the kernel and do it.
875 */
876int
877nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
878	register struct nfsmount *nmp;
879	struct nfsreq *rep;
880	struct ucred *cred;
881	char **auth_str;
882	int *auth_len;
883	char *verf_str;
884	int *verf_len;
885	NFSKERBKEY_T key;		/* return session key */
886{
887	int error = 0;
888
889	while ((nmp->nm_flag & NFSMNT_WAITAUTH) == 0) {
890		nmp->nm_flag |= NFSMNT_WANTAUTH;
891		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
892			"nfsauth1", 2 * hz);
893		error = nfs_sigintr(nmp, rep, rep->r_procp);
894		if (error) {
895			nmp->nm_flag &= ~NFSMNT_WANTAUTH;
896			return (error);
897		}
898	}
899	nmp->nm_flag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
900	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
901	nmp->nm_authlen = RPCAUTH_MAXSIZ;
902	nmp->nm_verfstr = verf_str;
903	nmp->nm_verflen = *verf_len;
904	nmp->nm_authuid = cred->cr_uid;
905	wakeup((caddr_t)&nmp->nm_authstr);
906
907	/*
908	 * And wait for mount_nfs to do its stuff.
909	 */
910	while ((nmp->nm_flag & NFSMNT_HASAUTH) == 0 && error == 0) {
911		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
912			"nfsauth2", 2 * hz);
913		error = nfs_sigintr(nmp, rep, rep->r_procp);
914	}
915	if (nmp->nm_flag & NFSMNT_AUTHERR) {
916		nmp->nm_flag &= ~NFSMNT_AUTHERR;
917		error = EAUTH;
918	}
919	if (error)
920		free((caddr_t)*auth_str, M_TEMP);
921	else {
922		*auth_len = nmp->nm_authlen;
923		*verf_len = nmp->nm_verflen;
924		bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
925	}
926	nmp->nm_flag &= ~NFSMNT_HASAUTH;
927	nmp->nm_flag |= NFSMNT_WAITAUTH;
928	if (nmp->nm_flag & NFSMNT_WANTAUTH) {
929		nmp->nm_flag &= ~NFSMNT_WANTAUTH;
930		wakeup((caddr_t)&nmp->nm_authtype);
931	}
932	return (error);
933}
934
935/*
936 * Get a nickname authenticator and verifier.
937 */
938int
939nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
940	struct nfsmount *nmp;
941	struct ucred *cred;
942	char **auth_str;
943	int *auth_len;
944	char *verf_str;
945	int verf_len;
946{
947	register struct nfsuid *nuidp;
948	register u_long *nickp, *verfp;
949	struct timeval ktvin, ktvout;
950
951#ifdef DIAGNOSTIC
952	if (verf_len < (4 * NFSX_UNSIGNED))
953		panic("nfs_getnickauth verf too small");
954#endif
955	for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
956	    nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
957		if (nuidp->nu_cr.cr_uid == cred->cr_uid)
958			break;
959	}
960	if (!nuidp || nuidp->nu_expire < time.tv_sec)
961		return (EACCES);
962
963	/*
964	 * Move to the end of the lru list (end of lru == most recently used).
965	 */
966	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
967	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
968
969	nickp = (u_long *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
970	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
971	*nickp = txdr_unsigned(nuidp->nu_nickname);
972	*auth_str = (char *)nickp;
973	*auth_len = 2 * NFSX_UNSIGNED;
974
975	/*
976	 * Now we must encrypt the verifier and package it up.
977	 */
978	verfp = (u_long *)verf_str;
979	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
980	if (time.tv_sec > nuidp->nu_timestamp.tv_sec ||
981	    (time.tv_sec == nuidp->nu_timestamp.tv_sec &&
982	     time.tv_usec > nuidp->nu_timestamp.tv_usec))
983		gettime(&nuidp->nu_timestamp);
984	else
985		nuidp->nu_timestamp.tv_usec++;
986	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
987	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
988
989	/*
990	 * Now encrypt the timestamp verifier in ecb mode using the session
991	 * key.
992	 */
993#ifdef NFSKERB
994	XXX
995#endif
996
997	*verfp++ = ktvout.tv_sec;
998	*verfp++ = ktvout.tv_usec;
999	*verfp = 0;
1000	return (0);
1001}
1002
1003/*
1004 * Save the current nickname in a hash list entry on the mount point.
1005 */
1006int
1007nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1008	register struct nfsmount *nmp;
1009	struct ucred *cred;
1010	int len;
1011	NFSKERBKEY_T key;
1012	struct mbuf **mdp;
1013	char **dposp;
1014	struct mbuf *mrep;
1015{
1016	register struct nfsuid *nuidp;
1017	register u_long *tl;
1018	register long t1;
1019	struct mbuf *md = *mdp;
1020	struct timeval ktvin, ktvout;
1021	u_long nick;
1022	char *dpos = *dposp, *cp2;
1023	int deltasec, error = 0;
1024
1025	if (len == (3 * NFSX_UNSIGNED)) {
1026		nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
1027		ktvin.tv_sec = *tl++;
1028		ktvin.tv_usec = *tl++;
1029		nick = fxdr_unsigned(u_long, *tl);
1030
1031		/*
1032		 * Decrypt the timestamp in ecb mode.
1033		 */
1034#ifdef NFSKERB
1035		XXX
1036#endif
1037		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1038		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1039		deltasec = time.tv_sec - ktvout.tv_sec;
1040		if (deltasec < 0)
1041			deltasec = -deltasec;
1042		/*
1043		 * If ok, add it to the hash list for the mount point.
1044		 */
1045		if (deltasec <= NFS_KERBCLOCKSKEW) {
1046			if (nmp->nm_numuids < nuidhash_max) {
1047				nmp->nm_numuids++;
1048				nuidp = (struct nfsuid *)
1049				   malloc(sizeof (struct nfsuid), M_NFSUID,
1050					M_WAITOK);
1051			} else {
1052				nuidp = nmp->nm_uidlruhead.tqh_first;
1053				LIST_REMOVE(nuidp, nu_hash);
1054				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1055					nu_lru);
1056			}
1057			nuidp->nu_flag = 0;
1058			nuidp->nu_cr.cr_uid = cred->cr_uid;
1059			nuidp->nu_expire = time.tv_sec + NFS_KERBTTL;
1060			nuidp->nu_timestamp = ktvout;
1061			nuidp->nu_nickname = nick;
1062			bcopy(key, nuidp->nu_key, sizeof (key));
1063			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1064				nu_lru);
1065			LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1066				nuidp, nu_hash);
1067		}
1068	} else
1069		nfsm_adv(nfsm_rndup(len));
1070nfsmout:
1071	*mdp = md;
1072	*dposp = dpos;
1073	return (error);
1074}
1075
1076#ifndef NFS_NOSERVER
1077
1078/*
1079 * Derefence a server socket structure. If it has no more references and
1080 * is no longer valid, you can throw it away.
1081 */
1082void
1083nfsrv_slpderef(slp)
1084	register struct nfssvc_sock *slp;
1085{
1086	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
1087		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
1088		free((caddr_t)slp, M_NFSSVC);
1089	}
1090}
1091
1092/*
1093 * Initialize the data structures for the server.
1094 * Handshake with any new nfsds starting up to avoid any chance of
1095 * corruption.
1096 */
1097void
1098nfsrv_init(terminating)
1099	int terminating;
1100{
1101	register struct nfssvc_sock *slp, *nslp;
1102
1103	if (nfssvc_sockhead_flag & SLP_INIT)
1104		panic("nfsd init");
1105	nfssvc_sockhead_flag |= SLP_INIT;
1106	if (terminating) {
1107		for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
1108			nslp = slp->ns_chain.tqe_next;
1109			if (slp->ns_flag & SLP_VALID)
1110				nfsrv_zapsock(slp);
1111			TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
1112			free((caddr_t)slp, M_NFSSVC);
1113		}
1114		nfsrv_cleancache();	/* And clear out server cache */
1115	}
1116
1117	TAILQ_INIT(&nfssvc_sockhead);
1118	nfssvc_sockhead_flag &= ~SLP_INIT;
1119	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
1120		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
1121		wakeup((caddr_t)&nfssvc_sockhead);
1122	}
1123
1124	TAILQ_INIT(&nfsd_head);
1125	nfsd_head_flag &= ~NFSD_CHECKSLP;
1126
1127	nfs_udpsock = (struct nfssvc_sock *)
1128	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
1129	bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
1130	TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
1131	TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
1132
1133	nfs_cltpsock = (struct nfssvc_sock *)
1134	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
1135	bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
1136	TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
1137	TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
1138}
1139
1140/*
1141 * Add entries to the server monitor log.
1142 */
1143static void
1144nfsd_rt(sotype, nd, cacherep)
1145	int sotype;
1146	register struct nfsrv_descript *nd;
1147	int cacherep;
1148{
1149	register struct drt *rt;
1150
1151	rt = &nfsdrt.drt[nfsdrt.pos];
1152	if (cacherep == RC_DOIT)
1153		rt->flag = 0;
1154	else if (cacherep == RC_REPLY)
1155		rt->flag = DRT_CACHEREPLY;
1156	else
1157		rt->flag = DRT_CACHEDROP;
1158	if (sotype == SOCK_STREAM)
1159		rt->flag |= DRT_TCP;
1160	if (nd->nd_flag & ND_NQNFS)
1161		rt->flag |= DRT_NQNFS;
1162	else if (nd->nd_flag & ND_NFSV3)
1163		rt->flag |= DRT_NFSV3;
1164	rt->proc = nd->nd_procnum;
1165	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
1166	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
1167	else
1168	    rt->ipadr = INADDR_ANY;
1169	rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
1170		(time.tv_usec - nd->nd_starttime.tv_usec);
1171	gettime(&rt->tstamp);
1172	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
1173}
1174#endif /* NFS_NOSERVER */
1175