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