xref: /dragonfly/sys/vfs/nfs/nfs_syscalls.c (revision b272101a)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
33  * $FreeBSD: src/sys/nfs/nfs_syscalls.c,v 1.58.2.1 2000/11/26 02:30:06 dillon Exp $
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sysmsg.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
41 #include <sys/file.h>
42 #include <sys/filedesc.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/caps.h>
48 #include <sys/buf.h>
49 #include <sys/mbuf.h>
50 #include <sys/resourcevar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/domain.h>
54 #include <sys/protosw.h>
55 #include <sys/nlookup.h>
56 
57 #include <sys/mutex2.h>
58 #include <sys/thread2.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
62 #include "xdr_subs.h"
63 #include "rpcv2.h"
64 #include "nfsproto.h"
65 #include "nfs.h"
66 #include "nfsm_subs.h"
67 #include "nfsrvcache.h"
68 #include "nfsmount.h"
69 #include "nfsnode.h"
70 #include "nfsrtt.h"
71 
72 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
73 
74 static int nuidhash_max = NFS_MAXUIDHASH;
75 
76 #ifndef NFS_NOSERVER
77 static void	nfsrv_zapsock (struct nfssvc_sock *slp);
78 #endif
79 
80 #define	TRUE	1
81 #define	FALSE	0
82 
83 SYSCTL_DECL(_vfs_nfs);
84 
85 #ifndef NFS_NOSERVER
86 int nfsd_waiting = 0;
87 static struct nfsdrt nfsdrt;
88 static int nfs_numnfsd = 0;
89 static void	nfsd_rt (int sotype, struct nfsrv_descript *nd,
90 			     int cacherep);
91 static int	nfssvc_addsock (struct file *, struct sockaddr *,
92 				    struct thread *);
93 static int	nfssvc_nfsd (struct nfsd_srvargs *,caddr_t,struct thread *);
94 
95 static int nfs_privport = 0;
96 SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport,
97     0, "Enable privileged source port checks");
98 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0,
99     "Enable NFS request procrastination");
100 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0,
101     "Enable NFSv3 request procrastination");
102 int	nfs_soreserve = NFS_MAXPACKET * NFS_MAXASYNCBIO;
103 SYSCTL_INT(_vfs_nfs, OID_AUTO, soreserve, CTLFLAG_RW, &nfs_soreserve, 0,
104     "Minimum NFS socket buffer size reservation");
105 
106 /*
107  * NFS server system calls
108  */
109 
110 #endif /* NFS_NOSERVER */
111 /*
112  * nfssvc_args(int flag, caddr_t argp)
113  *
114  * Nfs server psuedo system call for the nfsd's
115  * Based on the flag value it either:
116  * - adds a socket to the selection list
117  * - remains in the kernel as an nfsd
118  * - remains in the kernel as an nfsiod
119  *
120  * MPALMOSTSAFE
121  */
122 int
sys_nfssvc(struct sysmsg * sysmsg,const struct nfssvc_args * uap)123 sys_nfssvc(struct sysmsg *sysmsg, const struct nfssvc_args *uap)
124 {
125 #ifndef NFS_NOSERVER
126 	struct nlookupdata nd;
127 	struct file *fp;
128 	struct sockaddr *nam;
129 	struct nfsd_args nfsdarg;
130 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
131 	struct nfsd_cargs ncd;
132 	struct nfsd *nfsd;
133 	struct nfssvc_sock *slp;
134 	struct nfsuid *nuidp;
135 	struct nfsmount *nmp;
136 	struct vnode *vp;
137 #endif /* NFS_NOSERVER */
138 	int error;
139 	struct thread *td = curthread;
140 
141 	/*
142 	 * Must be super user
143 	 */
144 	error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT);
145 	if (error)
146 		return (error);
147 
148 	lwkt_gettoken(&nfs_token);
149 
150 	while (nfssvc_sockhead_flag & SLP_INIT) {
151 		nfssvc_sockhead_flag |= SLP_WANTINIT;
152 		tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
153 	}
154 	if (uap->flag & NFSSVC_BIOD)
155 		error = ENXIO;		/* no longer need nfsiod's */
156 #ifdef NFS_NOSERVER
157 	else
158 		error = ENXIO;
159 #else /* !NFS_NOSERVER */
160 	else if (uap->flag & NFSSVC_MNTD) {
161 		error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
162 		if (error)
163 			goto done;
164 		vp = NULL;
165 		error = nlookup_init(&nd, ncd.ncd_dirp, UIO_USERSPACE,
166 					NLC_FOLLOW);
167 		if (error == 0)
168 			error = nlookup(&nd);
169 		if (error == 0)
170 			error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
171 		nlookup_done(&nd);
172 		if (error)
173 			goto done;
174 
175 		if ((vp->v_flag & VROOT) == 0)
176 			error = EINVAL;
177 		nmp = VFSTONFS(vp->v_mount);
178 		vput(vp);
179 		if (error)
180 			goto done;
181 		if ((nmp->nm_state & NFSSTA_MNTD) &&
182 			(uap->flag & NFSSVC_GOTAUTH) == 0) {
183 			error = 0;
184 			goto done;
185 		}
186 		nmp->nm_state |= NFSSTA_MNTD;
187 		error = nfs_clientd(nmp, td->td_ucred, &ncd, uap->flag,
188 				    uap->argp, td);
189 	} else if (uap->flag & NFSSVC_ADDSOCK) {
190 		error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
191 		if (error)
192 			goto done;
193 		error = holdsock(td, nfsdarg.sock, &fp);
194 		if (error)
195 			goto done;
196 		/*
197 		 * Get the client address for connected sockets.
198 		 */
199 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
200 			nam = NULL;
201 		else {
202 			error = getsockaddr(&nam, nfsdarg.name,
203 					    nfsdarg.namelen);
204 			if (error) {
205 				fdrop(fp);
206 				goto done;
207 			}
208 		}
209 		error = nfssvc_addsock(fp, nam, td);
210 		fdrop(fp);
211 	} else {
212 		error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
213 		if (error)
214 			goto done;
215 		if ((uap->flag & NFSSVC_AUTHIN) &&
216 		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
217 		    (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
218 			slp = nfsd->nfsd_slp;
219 
220 			/*
221 			 * First check to see if another nfsd has already
222 			 * added this credential.
223 			 */
224 			for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
225 			    nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
226 				if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
227 				    (!nfsd->nfsd_nd->nd_nam2 ||
228 				     netaddr_match(AF_INET,
229 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
230 					break;
231 			}
232 			if (nuidp) {
233 			    nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
234 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
235 			} else {
236 			    /*
237 			     * Nope, so we will.
238 			     */
239 			    if (slp->ns_numuids < nuidhash_max) {
240 				slp->ns_numuids++;
241 				nuidp = (struct nfsuid *)
242 				   kmalloc(sizeof (struct nfsuid), M_NFSUID,
243 					M_WAITOK);
244 			    } else
245 				nuidp = NULL;
246 			    if ((slp->ns_flag & SLP_VALID) == 0) {
247 				if (nuidp)
248 				    kfree((caddr_t)nuidp, M_NFSUID);
249 			    } else {
250 				if (nuidp == NULL) {
251 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
252 				    LIST_REMOVE(nuidp, nu_hash);
253 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
254 					nu_lru);
255 				    if (nuidp->nu_flag & NU_NAM)
256 					kfree(nuidp->nu_nam, M_SONAME);
257 			        }
258 				nuidp->nu_flag = 0;
259 				nuidp->nu_cr = nsd->nsd_cr;
260 				if (nuidp->nu_cr.cr_ngroups > NGROUPS)
261 				    nuidp->nu_cr.cr_ngroups = NGROUPS;
262 				nuidp->nu_cr.cr_ref = 1;
263 				nuidp->nu_timestamp = nsd->nsd_timestamp;
264 				nuidp->nu_expire = time_uptime + nsd->nsd_ttl;
265 				/*
266 				 * and save the session key in nu_key.
267 				 */
268 				bcopy(nsd->nsd_key, nuidp->nu_key,
269 				    sizeof (nsd->nsd_key));
270 				if (nfsd->nfsd_nd->nd_nam2) {
271 				    struct sockaddr_in *saddr;
272 
273 				    saddr = (struct sockaddr_in *)
274 					    nfsd->nfsd_nd->nd_nam2;
275 				    switch (saddr->sin_family) {
276 				    case AF_INET:
277 					nuidp->nu_flag |= NU_INETADDR;
278 					nuidp->nu_inetaddr =
279 					     saddr->sin_addr.s_addr;
280 					break;
281 				    default:
282 					nuidp->nu_flag |= NU_NAM;
283 					nuidp->nu_nam =
284 					  dup_sockaddr(nfsd->nfsd_nd->nd_nam2);
285 					break;
286 				    };
287 				}
288 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
289 					nu_lru);
290 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
291 					nuidp, nu_hash);
292 				nfsrv_setcred(&nuidp->nu_cr,
293 				    &nfsd->nfsd_nd->nd_cr);
294 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
295 			    }
296 			}
297 		}
298 		if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
299 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
300 		error = nfssvc_nfsd(nsd, uap->argp, td);
301 	}
302 #endif /* NFS_NOSERVER */
303 	if (error == EINTR || error == ERESTART)
304 		error = 0;
305 done:
306 	lwkt_reltoken(&nfs_token);
307 	return (error);
308 }
309 
310 #ifndef NFS_NOSERVER
311 /*
312  * Adds a socket to the list for servicing by nfsds.
313  */
314 static int
nfssvc_addsock(struct file * fp,struct sockaddr * mynam,struct thread * td)315 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
316 {
317 	int siz;
318 	struct nfssvc_sock *slp;
319 	struct socket *so;
320 	int error;
321 
322 	so = (struct socket *)fp->f_data;
323 #if 0
324 	tslp = NULL;
325 	/*
326 	 * Add it to the list, as required.
327 	 */
328 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
329 		tslp = nfs_udpsock;
330 		if (tslp->ns_flag & SLP_VALID) {
331 			if (mynam != NULL)
332 				kfree(mynam, M_SONAME);
333 			return (EPERM);
334 		}
335 	}
336 #endif
337 	/*
338 	 * Reserve buffer space in the socket.  Note that due to bugs in
339 	 * Linux's delayed-ack code, serious performance degredation may
340 	 * occur with linux hosts if the minimum is used.
341 	 *
342 	 * NFS sockets are not limited to the standard sb_max or by
343 	 * resource limits.
344 	 */
345 	if (so->so_type == SOCK_STREAM)
346 		siz = NFS_MAXPACKET + sizeof (u_long);
347 	else
348 		siz = NFS_MAXPACKET;
349 	if (siz < nfs_soreserve)
350 	    siz = nfs_soreserve;
351 
352 	error = soreserve(so, siz, siz, NULL);
353 	if (error) {
354 		if (mynam != NULL)
355 			kfree(mynam, M_SONAME);
356 		return (error);
357 	}
358 
359 	/*
360 	 * Set protocol specific options { for now TCP only } and
361 	 * reserve some space. For datagram sockets, this can get called
362 	 * repeatedly for the same socket, but that isn't harmful.
363 	 */
364 	if (so->so_type == SOCK_STREAM) {
365 		struct sockopt sopt;
366 		int val;
367 
368 		bzero(&sopt, sizeof sopt);
369 		sopt.sopt_level = SOL_SOCKET;
370 		sopt.sopt_name = SO_KEEPALIVE;
371 		sopt.sopt_val = &val;
372 		sopt.sopt_valsize = sizeof val;
373 		val = 1;
374 		sosetopt(so, &sopt);
375 	}
376 	if (so->so_proto->pr_domain->dom_family == AF_INET &&
377 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
378 		struct sockopt sopt;
379 		int val;
380 
381 		bzero(&sopt, sizeof sopt);
382 		sopt.sopt_level = IPPROTO_TCP;
383 		sopt.sopt_name = TCP_NODELAY;
384 		sopt.sopt_val = &val;
385 		sopt.sopt_valsize = sizeof val;
386 		val = 1;
387 		sosetopt(so, &sopt);
388 
389 		bzero(&sopt, sizeof sopt);
390 		sopt.sopt_level = IPPROTO_TCP;
391 		sopt.sopt_name = TCP_FASTKEEP;
392 		sopt.sopt_val = &val;
393 		sopt.sopt_valsize = sizeof val;
394 		val = 1;
395 		sosetopt(so, &sopt);
396 	}
397 	atomic_clear_int(&so->so_rcv.ssb_flags, SSB_NOINTR);
398 	so->so_rcv.ssb_timeo = 0;
399 	atomic_clear_int(&so->so_snd.ssb_flags, SSB_NOINTR);
400 	so->so_snd.ssb_timeo = 0;
401 
402 	/*
403 	 * Clear AUTOSIZE, otherwise the socket buffer could be reduced
404 	 * to the point where rpc's cannot be queued using the mbuf
405 	 * interface.
406 	 */
407 	atomic_clear_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
408 	atomic_clear_int(&so->so_snd.ssb_flags, SSB_AUTOSIZE);
409 
410 	slp = kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
411 	mtx_init(&slp->ns_solock, "nfsvc");
412 	STAILQ_INIT(&slp->ns_rec);
413 	TAILQ_INIT(&slp->ns_uidlruhead);
414 	lwkt_token_init(&slp->ns_token, "nfssrv_token");
415 
416 	lwkt_gettoken(&nfs_token);
417 	nfsrv_slpref(slp);
418 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
419 	lwkt_gettoken(&slp->ns_token);
420 
421 	slp->ns_so = so;
422 	slp->ns_nam = mynam;
423 	fhold(fp);
424 	slp->ns_fp = fp;
425 
426 	so->so_upcallarg = (caddr_t)slp;
427 	so->so_upcall = nfsrv_rcv_upcall;
428 	atomic_set_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
429 	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
430 	nfsrv_wakenfsd(slp, 1);
431 
432 	lwkt_reltoken(&slp->ns_token);
433 	lwkt_reltoken(&nfs_token);
434 
435 	return (0);
436 }
437 
438 /*
439  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
440  * until it is killed by a signal.
441  */
442 static int
nfssvc_nfsd(struct nfsd_srvargs * nsd,caddr_t argp,struct thread * td)443 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
444 {
445 	int siz;
446 	struct nfssvc_sock *slp;
447 	struct nfsd *nfsd = nsd->nsd_nfsd;
448 	struct nfsrv_descript *nd = NULL;
449 	struct mbuf *m, *mreq;
450 	int error, cacherep, sotype, writes_todo;
451 	int procrastinate;
452 	int slplocked;
453 	u_quad_t cur_usec;
454 
455 #ifndef nolint
456 	cacherep = RC_DOIT;
457 	writes_todo = 0;
458 #endif
459 	lwkt_gettoken(&nfs_token);
460 
461 	if (nfsd == NULL) {
462 		nsd->nsd_nfsd = nfsd = (struct nfsd *)
463 			kmalloc(sizeof (struct nfsd), M_NFSD, M_WAITOK|M_ZERO);
464 		nfsd->nfsd_td = td;
465 		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
466 		nfs_numnfsd++;
467 	}
468 
469 	/*
470 	 * Loop getting rpc requests until SIGKILL.
471 	 */
472 	for (;;) {
473 		if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
474 			while (nfsd->nfsd_slp == NULL &&
475 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
476 				nfsd->nfsd_flag |= NFSD_WAITING;
477 				nfsd_waiting++;
478 				error = tsleep(nfsd, PCATCH, "nfsd", 0);
479 				nfsd_waiting--;
480 				if (error && nfsd->nfsd_slp == NULL)
481 					goto done;
482 			}
483 			if (nfsd->nfsd_slp == NULL &&
484 			    (nfsd_head_flag & NFSD_CHECKSLP)) {
485 				TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
486 				    if ((slp->ns_flag & SLP_ACTION_MASK) ||
487 					slp->ns_needq_upcall) {
488 					    nfsrv_slpref(slp);
489 					    nfsd->nfsd_slp = slp;
490 					    break;
491 				    }
492 				}
493 				if (slp == NULL)
494 					nfsd_head_flag &= ~NFSD_CHECKSLP;
495 			}
496 			if ((slp = nfsd->nfsd_slp) == NULL)
497 				continue;
498 
499 			lwkt_reltoken(&nfs_token);
500 			lwkt_gettoken(&slp->ns_token);
501 
502 			if (slp->ns_needq_upcall) {
503 				slp->ns_needq_upcall = 0;
504 				slp->ns_flag |= SLP_NEEDQ;
505 			}
506 
507 			if (slp->ns_flag & SLP_VALID) {
508 				/*
509 				 * We can both process additional received
510 				 * data into new records and process existing
511 				 * records.  This keeps the pipeline hot by
512 				 * allowing the tcp socket to continue to
513 				 * drain while we are processing records.
514 				 */
515 				while (slp->ns_flag & (SLP_DISCONN|SLP_NEEDQ)) {
516 					if (slp->ns_flag & SLP_DISCONN) {
517 						nfsrv_zapsock(slp);
518 						break;
519 					}
520 					if (slp->ns_flag & SLP_NEEDQ) {
521 						nfs_slplock(slp, 1);
522 						if ((slp->ns_flag &
523 						     SLP_NEEDQ) == 0) {
524 							nfs_slpunlock(slp);
525 							continue;
526 						}
527 						nfsrv_rcv(slp->ns_so,
528 							  (caddr_t)slp,
529 							  M_WAITOK);
530 						nfs_slpunlock(slp);
531 						break;
532 					}
533 				}
534 				error = nfsrv_dorec(slp, nfsd, &nd);
535 				cur_usec = nfs_curusec();
536 				if (error && slp->ns_tq.lh_first &&
537 				    slp->ns_tq.lh_first->nd_time <= cur_usec) {
538 					error = 0;
539 					cacherep = RC_DOIT;
540 					writes_todo = 1;
541 				} else {
542 					writes_todo = 0;
543 				}
544 				nfsd->nfsd_flag |= NFSD_REQINPROG;
545 			} else {
546 				slp->ns_flag &= ~SLP_ACTION_MASK;
547 				error = 0;
548 			}
549 		} else {
550 			error = 0;
551 			slp = nfsd->nfsd_slp;
552 
553 			lwkt_reltoken(&nfs_token);
554 			lwkt_gettoken(&slp->ns_token);
555 
556 			if (slp->ns_needq_upcall) {
557 				slp->ns_needq_upcall = 0;
558 				slp->ns_flag |= SLP_NEEDQ;
559 			}
560 			if (NFSRV_RECLIMIT(slp) == 0 &&
561 			    (slp->ns_flag & SLP_NEEDQ)) {
562 				nfs_slplock(slp, 1);
563 				if (NFSRV_RECLIMIT(slp) == 0 &&
564 				    (slp->ns_flag & SLP_NEEDQ)) {
565 					nfsrv_rcv(slp->ns_so, (caddr_t)slp,
566 						  M_WAITOK);
567 				}
568 				nfs_slpunlock(slp);
569 			}
570 		}
571 
572 		/*
573 		 * nfs_token not held here.  slp token is held.
574 		 */
575 		if (error || (slp->ns_flag & SLP_VALID) == 0) {
576 			if (nd) {
577 				kfree((caddr_t)nd, M_NFSRVDESC);
578 				nd = NULL;
579 			}
580 			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
581 			if (slp->ns_flag & SLP_ACTION_MASK) {
582 				lwkt_reltoken(&slp->ns_token);
583 				lwkt_gettoken(&nfs_token);
584 			} else {
585 				nfsd->nfsd_slp = NULL;
586 				lwkt_reltoken(&slp->ns_token);
587 				lwkt_gettoken(&nfs_token);
588 				nfsrv_slpderef(slp);
589 			}
590 			continue;
591 		}
592 		sotype = slp->ns_so->so_type;
593 
594 		/*
595 		 * Execute the NFS request - handle the server side cache
596 		 *
597 		 * nfs_token not held here.  slp token is held.
598 		 */
599 		if (nd) {
600 		    getmicrotime(&nd->nd_starttime);
601 		    if (nd->nd_nam2)
602 			nd->nd_nam = nd->nd_nam2;
603 		    else
604 			nd->nd_nam = slp->ns_nam;
605 
606 		    /*
607 		     * Check to see if authorization is needed.
608 		     */
609 		    if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
610 			nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
611 			nsd->nsd_haddr =
612 				((struct sockaddr_in *)
613 				 nd->nd_nam)->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 			{
622 			    lwkt_reltoken(&slp->ns_token);
623 			    return (ENEEDAUTH);
624 			}
625 			cacherep = RC_DROPIT;
626 		    } else {
627 			cacherep = nfsrv_getcache(nd, slp, &mreq);
628 		    }
629 
630 		    if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
631 			nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
632 			nd->nd_procnum = NFSPROC_NOOP;
633 			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
634 			cacherep = RC_DOIT;
635 		    } else if (nfs_privport) {
636 			/* Check if source port is privileged */
637 			u_short port;
638 			struct sockaddr *nam = nd->nd_nam;
639 			struct sockaddr_in *sin;
640 
641 			sin = (struct sockaddr_in *)nam;
642 			port = ntohs(sin->sin_port);
643 			if (port >= IPPORT_RESERVED &&
644 			    nd->nd_procnum != NFSPROC_NULL) {
645 			    char addr[INET_ADDRSTRLEN];
646 
647 			    nd->nd_procnum = NFSPROC_NOOP;
648 			    nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
649 			    cacherep = RC_DOIT;
650 			    kprintf("NFS request from unprivileged port (%s:%d)\n",
651 				    kinet_ntoa(sin->sin_addr, addr), port);
652 			}
653 		    }
654 		}
655 
656 		/*
657 		 * Execute the NFS request - direct execution
658 		 *
659 		 * Loop to get all the write rpc replies that have been
660 		 * gathered together.
661 		 *
662 		 * nfs_token not held here.  slp token is held.
663 		 */
664 		do {
665 		    switch (cacherep) {
666 		    case RC_DOIT:
667 			if (nd && (nd->nd_flag & ND_NFSV3))
668 			    procrastinate = nfsrvw_procrastinate_v3;
669 			else
670 			    procrastinate = nfsrvw_procrastinate;
671 			if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
672 			    procrastinate > 0)
673 			) {
674 			    error = nfsrv_writegather(&nd, slp,
675 						      nfsd->nfsd_td, &mreq);
676 			} else {
677 			    /* NOT YET lwkt_reltoken(&slp->ns_token); */
678 			    error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
679 						slp, nfsd->nfsd_td, &mreq);
680 			    /* NOT YET lwkt_gettoken(&slp->ns_token); */
681 			    lwpkthreaddeferred();	/* vnlru issues */
682 			}
683 			if (mreq == NULL)
684 				break;
685 			if (error != 0 && error != NFSERR_RETVOID) {
686 				if (nd->nd_procnum != NQNFSPROC_VACATED)
687 					nfsstats.srv_errs++;
688 				nfsrv_updatecache(nd, FALSE, mreq);
689 				if (nd->nd_nam2)
690 					kfree(nd->nd_nam2, M_SONAME);
691 				break;
692 			}
693 			nfsstats.srvrpccnt[nd->nd_procnum]++;
694 			nfsrv_updatecache(nd, TRUE, mreq);
695 			nd->nd_mrep = NULL;
696 			/* FALL THROUGH */
697 		    case RC_REPLY:
698 			m = mreq;
699 			siz = 0;
700 			while (m) {
701 				siz += m->m_len;
702 				m = m->m_next;
703 			}
704 			if (siz <= 0 || siz > NFS_MAXPACKET) {
705 				kprintf("mbuf siz=%d\n",siz);
706 				panic("Bad nfs svc reply");
707 			}
708 			m = mreq;
709 			m->m_pkthdr.len = siz;
710 			m->m_pkthdr.rcvif = NULL;
711 			/*
712 			 * For stream protocols, prepend a Sun RPC
713 			 * Record Mark.
714 			 */
715 			if (sotype == SOCK_STREAM) {
716 				M_PREPEND(m, NFSX_UNSIGNED, M_WAITOK);
717 				*mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
718 			}
719 			if ((slp->ns_flag & SLP_VALID) &&
720 			    (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)){
721 				nfs_slplock(slp, 1);
722 				slplocked = 1;
723 			} else {
724 				slplocked = 0;
725 			}
726 			if (slp->ns_flag & SLP_VALID) {
727 			    error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
728 			} else {
729 			    error = EPIPE;
730 			    m_freem(m);
731 			}
732 			if (nfsrtton)
733 				nfsd_rt(sotype, nd, cacherep);
734 			if (nd->nd_nam2)
735 				kfree(nd->nd_nam2, M_SONAME);
736 			if (nd->nd_mrep)
737 				m_freem(nd->nd_mrep);
738 			if (error == EPIPE || error == ENOBUFS)
739 				nfsrv_zapsock(slp);
740 			if (slplocked)
741 				nfs_slpunlock(slp);
742 			if (error == EINTR || error == ERESTART) {
743 				kfree((caddr_t)nd, M_NFSRVDESC);
744 				lwkt_reltoken(&slp->ns_token);
745 				lwkt_gettoken(&nfs_token);
746 				nfsd->nfsd_slp = NULL;
747 				nfsrv_slpderef(slp);
748 				goto done;
749 			}
750 			break;
751 		    case RC_DROPIT:
752 			if (nfsrtton)
753 				nfsd_rt(sotype, nd, cacherep);
754 			m_freem(nd->nd_mrep);
755 			if (nd->nd_nam2)
756 				kfree(nd->nd_nam2, M_SONAME);
757 			break;
758 		    }
759 		    if (nd) {
760 			kfree((caddr_t)nd, M_NFSRVDESC);
761 			nd = NULL;
762 		    }
763 
764 		    /*
765 		     * Check to see if there are outstanding writes that
766 		     * need to be serviced.
767 		     */
768 		    cur_usec = nfs_curusec();
769 		    if (slp->ns_tq.lh_first &&
770 			slp->ns_tq.lh_first->nd_time <= cur_usec) {
771 			cacherep = RC_DOIT;
772 			writes_todo = 1;
773 		    } else {
774 			writes_todo = 0;
775 		    }
776 		} while (writes_todo);
777 
778 		/*
779 		 * nfs_token not held here.  slp token is held.
780 		 */
781 		if (nfsrv_dorec(slp, nfsd, &nd)) {
782 			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
783 			if (slp->ns_flag & SLP_ACTION_MASK) {
784 				lwkt_reltoken(&slp->ns_token);
785 				lwkt_gettoken(&nfs_token);
786 			} else {
787 				nfsd->nfsd_slp = NULL;
788 				lwkt_reltoken(&slp->ns_token);
789 				lwkt_gettoken(&nfs_token);
790 				nfsrv_slpderef(slp);
791 			}
792 		} else {
793 			lwkt_reltoken(&slp->ns_token);
794 			lwkt_gettoken(&nfs_token);
795 		}
796 	}
797 done:
798 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
799 	kfree((caddr_t)nfsd, M_NFSD);
800 	nsd->nsd_nfsd = NULL;
801 	if (--nfs_numnfsd == 0)
802 		nfsrv_init(TRUE);	/* Reinitialize everything */
803 
804 	lwkt_reltoken(&nfs_token);
805 	return (error);
806 }
807 
808 /*
809  * Shut down a socket associated with an nfssvc_sock structure.
810  * Should be called with the send lock set, if required.
811  *
812  * The trick here is to increment the sref at the start, so that the nfsds
813  * will stop using it and clear ns_flag at the end so that it will not be
814  * reassigned during cleanup.
815  *
816  * That said, while we shutdown() the socket here, we don't actually destroy
817  * it until the final deref as there might be other code in the middle of
818  * using it.
819  */
820 static void
nfsrv_zapsock(struct nfssvc_sock * slp)821 nfsrv_zapsock(struct nfssvc_sock *slp)
822 {
823 	struct nfsuid *nuidp, *nnuidp;
824 	struct nfsrv_descript *nwp, *nnwp;
825 	struct socket *so;
826 	struct nfsrv_rec *rec;
827 	int wasvalid;
828 
829 	wasvalid = slp->ns_flag & SLP_VALID;
830 	slp->ns_flag &= ~SLP_ALLFLAGS;
831 	if (wasvalid && slp->ns_fp) {
832 		so = slp->ns_so;
833 		atomic_clear_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
834 		so->so_upcall = NULL;
835 		so->so_upcallarg = NULL;
836 		soshutdown(so, SHUT_RDWR);
837 		if (slp->ns_nam) {
838 			kfree(slp->ns_nam, M_SONAME);
839 			slp->ns_nam = NULL;
840 		}
841 
842 		m_freem(slp->ns_raw);
843 		slp->ns_raw = NULL;
844 
845 		while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
846 			--slp->ns_numrec;
847 			STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
848 			if (rec->nr_address)
849 				kfree(rec->nr_address, M_SONAME);
850 			m_freem(rec->nr_packet);
851 			kfree(rec, M_NFSRVDESC);
852 		}
853 		KKASSERT(slp->ns_numrec == 0);
854 
855 		TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
856 				      nnuidp) {
857 			LIST_REMOVE(nuidp, nu_hash);
858 			TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
859 			if (nuidp->nu_flag & NU_NAM)
860 				kfree(nuidp->nu_nam, M_SONAME);
861 			kfree((caddr_t)nuidp, M_NFSUID);
862 		}
863 		crit_enter();	/* XXX doesn't do anything any more */
864 		for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
865 			nnwp = nwp->nd_tq.le_next;
866 			LIST_REMOVE(nwp, nd_tq);
867 			kfree((caddr_t)nwp, M_NFSRVDESC);
868 		}
869 		LIST_INIT(&slp->ns_tq);
870 		crit_exit();
871 
872 		nfsrv_slpderef(slp);
873 	}
874 }
875 
876 /*
877  * Derefence a server socket structure. If it has no more references and
878  * is no longer valid, you can throw it away.
879  *
880  * Must be holding nfs_token!
881  */
882 void
nfsrv_slpderef(struct nfssvc_sock * slp)883 nfsrv_slpderef(struct nfssvc_sock *slp)
884 {
885 	struct file *fp;
886 
887 	ASSERT_LWKT_TOKEN_HELD(&nfs_token);
888 	if (slp->ns_sref == 1) {
889 		KKASSERT((slp->ns_flag & SLP_VALID) == 0);
890 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
891 		slp->ns_sref = 0;
892 		fp = slp->ns_fp;
893 		slp->ns_fp = NULL;
894 		slp->ns_so = NULL;
895 		if (fp)
896 			closef(fp, NULL);
897 		kfree((caddr_t)slp, M_NFSSVC);
898 	} else {
899 		--slp->ns_sref;
900 	}
901 }
902 
903 void
nfsrv_slpref(struct nfssvc_sock * slp)904 nfsrv_slpref(struct nfssvc_sock *slp)
905 {
906 	ASSERT_LWKT_TOKEN_HELD(&nfs_token);
907 	++slp->ns_sref;
908 }
909 
910 /*
911  * Lock a socket against others.
912  *
913  * Returns 0 on failure, 1 on success.
914  */
915 int
nfs_slplock(struct nfssvc_sock * slp,int wait)916 nfs_slplock(struct nfssvc_sock *slp, int wait)
917 {
918 	mtx_t *mtx = &slp->ns_solock;
919 
920 	if (wait) {
921 		mtx_lock_ex(mtx, 0, 0);
922 		return(1);
923 	} else if (mtx_lock_ex_try(mtx) == 0) {
924 		return(1);
925 	} else {
926 		return(0);
927 	}
928 }
929 
930 /*
931  * Unlock the stream socket for others.
932  */
933 void
nfs_slpunlock(struct nfssvc_sock * slp)934 nfs_slpunlock(struct nfssvc_sock *slp)
935 {
936 	mtx_t *mtx = &slp->ns_solock;
937 
938 	mtx_unlock(mtx);
939 }
940 
941 /*
942  * Initialize the data structures for the server.
943  * Handshake with any new nfsds starting up to avoid any chance of
944  * corruption.
945  */
946 void
nfsrv_init(int terminating)947 nfsrv_init(int terminating)
948 {
949 	struct nfssvc_sock *slp, *nslp;
950 
951 	lwkt_gettoken(&nfs_token);
952 	if (nfssvc_sockhead_flag & SLP_INIT)
953 		panic("nfsd init");
954 	nfssvc_sockhead_flag |= SLP_INIT;
955 
956 	if (terminating) {
957 		TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
958 			nfsrv_slpref(slp);
959 			lwkt_gettoken(&slp->ns_token);
960 			if (slp->ns_flag & SLP_VALID)
961 				nfsrv_zapsock(slp);
962 			lwkt_reltoken(&slp->ns_token);
963 			nfsrv_slpderef(slp);
964 		}
965 		nfsrv_cleancache();	/* And clear out server cache */
966 	} else {
967 		nfs_pub.np_valid = 0;
968 	}
969 
970 	TAILQ_INIT(&nfssvc_sockhead);
971 	nfssvc_sockhead_flag &= ~SLP_INIT;
972 	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
973 		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
974 		wakeup((caddr_t)&nfssvc_sockhead);
975 	}
976 
977 	TAILQ_INIT(&nfsd_head);
978 	nfsd_head_flag &= ~NFSD_CHECKSLP;
979 
980 	lwkt_reltoken(&nfs_token);
981 
982 #if 0
983 	nfs_udpsock = (struct nfssvc_sock *)
984 	    kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
985 	mtx_init(&nfs_udpsock->ns_solock);
986 	STAILQ_INIT(&nfs_udpsock->ns_rec);
987 	TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
988 	TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
989 
990 	nfs_cltpsock = (struct nfssvc_sock *)
991 	    kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
992 	mtx_init(&nfs_cltpsock->ns_solock);
993 	STAILQ_INIT(&nfs_cltpsock->ns_rec);
994 	TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
995 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
996 #endif
997 }
998 
999 /*
1000  * Add entries to the server monitor log.
1001  */
1002 static void
nfsd_rt(int sotype,struct nfsrv_descript * nd,int cacherep)1003 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
1004 {
1005 	struct drt *rt;
1006 
1007 	rt = &nfsdrt.drt[nfsdrt.pos];
1008 	if (cacherep == RC_DOIT)
1009 		rt->flag = 0;
1010 	else if (cacherep == RC_REPLY)
1011 		rt->flag = DRT_CACHEREPLY;
1012 	else
1013 		rt->flag = DRT_CACHEDROP;
1014 	if (sotype == SOCK_STREAM)
1015 		rt->flag |= DRT_TCP;
1016 	if (nd->nd_flag & ND_NFSV3)
1017 		rt->flag |= DRT_NFSV3;
1018 	rt->proc = nd->nd_procnum;
1019 	if (nd->nd_nam->sa_family == AF_INET)
1020 	    rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
1021 	else
1022 	    rt->ipadr = INADDR_ANY;
1023 	rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
1024 	getmicrotime(&rt->tstamp);
1025 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
1026 }
1027 #endif /* NFS_NOSERVER */
1028 
1029 /*
1030  * Get an authorization string for the uid by having the mount_nfs sitting
1031  * on this mount point porpous out of the kernel and do it.
1032  */
1033 int
nfs_getauth(struct nfsmount * nmp,struct nfsreq * rep,struct ucred * cred,char ** auth_str,int * auth_len,char * verf_str,int * verf_len,NFSKERBKEY_T key)1034 nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
1035 	    struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
1036 	    int *verf_len, NFSKERBKEY_T key /* return session key */)
1037 {
1038 	int error = 0;
1039 
1040 	while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
1041 		nmp->nm_state |= NFSSTA_WANTAUTH;
1042 		(void) tsleep((caddr_t)&nmp->nm_authtype, 0,
1043 			"nfsauth1", 2 * hz);
1044 		error = nfs_sigintr(nmp, rep, rep->r_td);
1045 		if (error) {
1046 			nmp->nm_state &= ~NFSSTA_WANTAUTH;
1047 			return (error);
1048 		}
1049 	}
1050 	nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1051 	nmp->nm_authstr = *auth_str = (char *)kmalloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1052 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
1053 	nmp->nm_verfstr = verf_str;
1054 	nmp->nm_verflen = *verf_len;
1055 	nmp->nm_authuid = cred->cr_uid;
1056 	wakeup((caddr_t)&nmp->nm_authstr);
1057 
1058 	/*
1059 	 * And wait for mount_nfs to do its stuff.
1060 	 */
1061 	while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1062 		(void) tsleep((caddr_t)&nmp->nm_authlen, 0,
1063 			"nfsauth2", 2 * hz);
1064 		error = nfs_sigintr(nmp, rep, rep->r_td);
1065 	}
1066 	if (nmp->nm_state & NFSSTA_AUTHERR) {
1067 		nmp->nm_state &= ~NFSSTA_AUTHERR;
1068 		error = EAUTH;
1069 	}
1070 	if (error)
1071 		kfree((caddr_t)*auth_str, M_TEMP);
1072 	else {
1073 		*auth_len = nmp->nm_authlen;
1074 		*verf_len = nmp->nm_verflen;
1075 		bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (NFSKERBKEY_T));
1076 	}
1077 	nmp->nm_state &= ~NFSSTA_HASAUTH;
1078 	nmp->nm_state |= NFSSTA_WAITAUTH;
1079 	if (nmp->nm_state & NFSSTA_WANTAUTH) {
1080 		nmp->nm_state &= ~NFSSTA_WANTAUTH;
1081 		wakeup((caddr_t)&nmp->nm_authtype);
1082 	}
1083 	return (error);
1084 }
1085 
1086 /*
1087  * Get a nickname authenticator and verifier.
1088  */
1089 int
nfs_getnickauth(struct nfsmount * nmp,struct ucred * cred,char ** auth_str,int * auth_len,char * verf_str,int verf_len)1090 nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
1091 		int *auth_len, char *verf_str, int verf_len)
1092 {
1093 	struct nfsuid *nuidp;
1094 	u_int32_t *nickp, *verfp;
1095 	struct timeval ktvout;
1096 
1097 #ifdef DIAGNOSTIC
1098 	if (verf_len < (4 * NFSX_UNSIGNED))
1099 		panic("nfs_getnickauth verf too small");
1100 #endif
1101 	for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1102 	    nuidp != NULL; nuidp = nuidp->nu_hash.le_next) {
1103 		if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1104 			break;
1105 	}
1106 	if (!nuidp || nuidp->nu_expire < time_uptime)
1107 		return (EACCES);
1108 
1109 	/*
1110 	 * Move to the end of the lru list (end of lru == most recently used).
1111 	 */
1112 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1113 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1114 
1115 	nickp = (u_int32_t *)kmalloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1116 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1117 	*nickp = txdr_unsigned(nuidp->nu_nickname);
1118 	*auth_str = (char *)nickp;
1119 	*auth_len = 2 * NFSX_UNSIGNED;
1120 
1121 	/*
1122 	 * Now we must encrypt the verifier and package it up.
1123 	 */
1124 	verfp = (u_int32_t *)verf_str;
1125 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1126 	if (time_second != nuidp->nu_timestamp.tv_sec ||
1127 	    time_second > nuidp->nu_timestamp.tv_usec)		/* XXX */
1128 		getmicrotime(&nuidp->nu_timestamp);
1129 	else
1130 		nuidp->nu_timestamp.tv_usec++;
1131 
1132 	/*
1133 	 * Now encrypt the timestamp verifier in ecb mode using the session
1134 	 * key.
1135 	 */
1136 #ifdef NFSKERB
1137 	XXX
1138 #else
1139 	ktvout.tv_sec = 0;
1140 	ktvout.tv_usec = 0;
1141 #endif
1142 
1143 	*verfp++ = ktvout.tv_sec;
1144 	*verfp++ = ktvout.tv_usec;
1145 	*verfp = 0;
1146 	return (0);
1147 }
1148 
1149 /*
1150  * Save the current nickname in a hash list entry on the mount point.
1151  */
1152 int
nfs_savenickauth(struct nfsmount * nmp,struct ucred * cred,int len,NFSKERBKEY_T key,struct mbuf ** mdp,char ** dposp,struct mbuf * mrep)1153 nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1154 		 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1155 		 struct mbuf *mrep)
1156 {
1157 	struct nfsuid *nuidp;
1158 	u_int32_t *tl;
1159 	struct timeval ktvin, ktvout;
1160 	u_int32_t nick;
1161 	int deltasec, error = 0;
1162 	struct nfsm_info info;
1163 
1164 	info.md = *mdp;
1165 	info.dpos = *dposp;
1166 	info.mrep = mrep;
1167 
1168 	if (len == (3 * NFSX_UNSIGNED)) {
1169 		NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
1170 		ktvin.tv_sec = *tl++;
1171 		ktvin.tv_usec = *tl++;
1172 		nick = fxdr_unsigned(u_int32_t, *tl);
1173 
1174 		/*
1175 		 * Decrypt the timestamp in ecb mode.
1176 		 */
1177 #ifdef NFSKERB
1178 		XXX
1179 #else
1180 		ktvout.tv_sec = 0;
1181 		ktvout.tv_usec = 0;
1182 #endif
1183 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1184 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1185 		deltasec = time_second - ktvout.tv_sec;
1186 		if (deltasec < 0)
1187 			deltasec = -deltasec;
1188 		/*
1189 		 * If ok, add it to the hash list for the mount point.
1190 		 */
1191 		if (deltasec <= NFS_KERBCLOCKSKEW) {
1192 			if (nmp->nm_numuids < nuidhash_max) {
1193 				nmp->nm_numuids++;
1194 				nuidp = (struct nfsuid *)
1195 				   kmalloc(sizeof (struct nfsuid), M_NFSUID,
1196 					M_WAITOK);
1197 			} else {
1198 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1199 				LIST_REMOVE(nuidp, nu_hash);
1200 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1201 					nu_lru);
1202 			}
1203 			nuidp->nu_flag = 0;
1204 			nuidp->nu_cr.cr_uid = cred->cr_uid;
1205 			nuidp->nu_expire = time_uptime + NFS_KERBTTL;
1206 			nuidp->nu_timestamp = ktvout;
1207 			nuidp->nu_nickname = nick;
1208 			bcopy(key, nuidp->nu_key, sizeof (NFSKERBKEY_T));
1209 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1210 				nu_lru);
1211 			LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1212 				nuidp, nu_hash);
1213 		}
1214 	} else {
1215 		ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
1216 	}
1217 nfsmout:
1218 	*mdp = info.md;
1219 	*dposp = info.dpos;
1220 	return (error);
1221 }
1222