nfs_syscalls.c revision 1.139 1 /* $NetBSD: nfs_syscalls.c,v 1.139 2008/09/28 00:10:18 pooka Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.139 2008/09/28 00:10:18 pooka Exp $");
39
40 #include "fs_nfs.h"
41 #include "opt_nfs.h"
42 #include "opt_nfsserver.h"
43 #include "opt_iso.h"
44 #include "opt_inet.h"
45 #include "opt_compat_netbsd.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/proc.h>
55 #include <sys/uio.h>
56 #include <sys/malloc.h>
57 #include <sys/kmem.h>
58 #include <sys/buf.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/signalvar.h>
63 #include <sys/domain.h>
64 #include <sys/protosw.h>
65 #include <sys/namei.h>
66 #include <sys/syslog.h>
67 #include <sys/filedesc.h>
68 #include <sys/kthread.h>
69 #include <sys/kauth.h>
70 #include <sys/syscallargs.h>
71
72 #include <netinet/in.h>
73 #include <netinet/tcp.h>
74 #ifdef ISO
75 #include <netiso/iso.h>
76 #endif
77 #include <nfs/xdr_subs.h>
78 #include <nfs/rpcv2.h>
79 #include <nfs/nfsproto.h>
80 #include <nfs/nfs.h>
81 #include <nfs/nfsm_subs.h>
82 #include <nfs/nfsrvcache.h>
83 #include <nfs/nfsmount.h>
84 #include <nfs/nfsnode.h>
85 #include <nfs/nfsrtt.h>
86 #include <nfs/nfs_var.h>
87
88 /* Global defs. */
89 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
90 struct nfssvc_sock *,
91 struct lwp *, struct mbuf **));
92 extern int nfsrvw_procrastinate;
93
94 struct nfssvc_sock *nfs_udpsock;
95 #ifdef ISO
96 struct nfssvc_sock *nfs_cltpsock;
97 #endif
98 #ifdef INET6
99 struct nfssvc_sock *nfs_udp6sock;
100 #endif
101 int nuidhash_max = NFS_MAXUIDHASH;
102 #ifdef NFSSERVER
103 static int nfs_numnfsd = 0;
104 static struct nfsdrt nfsdrt;
105 #endif
106
107 #ifdef NFSSERVER
108 kmutex_t nfsd_lock;
109 struct nfssvc_sockhead nfssvc_sockhead;
110 kcondvar_t nfsd_initcv;
111 struct nfssvc_sockhead nfssvc_sockpending;
112 struct nfsdhead nfsd_head;
113 struct nfsdidlehead nfsd_idle_head;
114
115 int nfssvc_sockhead_flag;
116 int nfsd_head_flag;
117 #endif
118
119 #ifdef NFS
120 /*
121 * locking order:
122 * nfs_iodlist_lock -> nid_lock -> nm_lock
123 */
124 kmutex_t nfs_iodlist_lock;
125 struct nfs_iodlist nfs_iodlist_idle;
126 struct nfs_iodlist nfs_iodlist_all;
127 int nfs_niothreads = -1; /* == "0, and has never been set" */
128 #endif
129
130 #ifdef NFSSERVER
131 static struct nfssvc_sock *nfsrv_sockalloc __P((void));
132 static void nfsrv_sockfree __P((struct nfssvc_sock *));
133 static void nfsd_rt __P((int, struct nfsrv_descript *, int));
134 #endif
135
136 /*
137 * NFS server system calls
138 */
139
140
141 /*
142 * Nfs server pseudo system call for the nfsd's
143 * Based on the flag value it either:
144 * - adds a socket to the selection list
145 * - remains in the kernel as an nfsd
146 * - remains in the kernel as an nfsiod
147 */
148 int
149 sys_nfssvc(struct lwp *l, const struct sys_nfssvc_args *uap, register_t *retval)
150 {
151 /* {
152 syscallarg(int) flag;
153 syscallarg(void *) argp;
154 } */
155 int error;
156 #ifdef NFSSERVER
157 file_t *fp;
158 struct mbuf *nam;
159 struct nfsd_args nfsdarg;
160 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
161 struct nfsd *nfsd;
162 struct nfssvc_sock *slp;
163 struct nfsuid *nuidp;
164 #endif
165
166 /*
167 * Must be super user
168 */
169 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
170 KAUTH_REQ_NETWORK_NFS_SVC, NULL, NULL, NULL);
171 if (error)
172 return (error);
173
174 /* Initialize NFS server / client shared data. */
175 nfs_init();
176
177 #ifdef NFSSERVER
178 mutex_enter(&nfsd_lock);
179 while (nfssvc_sockhead_flag & SLP_INIT) {
180 cv_wait(&nfsd_initcv, &nfsd_lock);
181 }
182 mutex_exit(&nfsd_lock);
183 #endif
184 if (SCARG(uap, flag) & NFSSVC_BIOD) {
185 #if defined(NFS) && defined(COMPAT_14)
186 error = kpause("nfsbiod", true, 0, NULL); /* dummy impl */
187 #else
188 error = ENOSYS;
189 #endif
190 } else if (SCARG(uap, flag) & NFSSVC_MNTD) {
191 error = ENOSYS;
192 } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
193 #ifndef NFSSERVER
194 error = ENOSYS;
195 #else
196 error = copyin(SCARG(uap, argp), (void *)&nfsdarg,
197 sizeof(nfsdarg));
198 if (error)
199 return (error);
200 /* getsock() will use the descriptor for us */
201 if ((fp = fd_getfile(nfsdarg.sock)) == NULL)
202 return (EBADF);
203 if (fp->f_type != DTYPE_SOCKET) {
204 fd_putfile(nfsdarg.sock);
205 return (ENOTSOCK);
206 }
207 if (error)
208 return (error);
209 /*
210 * Get the client address for connected sockets.
211 */
212 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
213 nam = (struct mbuf *)0;
214 else {
215 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
216 MT_SONAME);
217 if (error) {
218 fd_putfile(nfsdarg.sock);
219 return (error);
220 }
221 }
222 error = nfssvc_addsock(fp, nam);
223 fd_putfile(nfsdarg.sock);
224 #endif /* !NFSSERVER */
225 } else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
226 #ifndef NFSSERVER
227 error = ENOSYS;
228 #else
229 struct export_args *args;
230 struct mountd_exports_list mel;
231
232 error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
233 if (error != 0)
234 return error;
235
236 args = (struct export_args *)malloc(mel.mel_nexports *
237 sizeof(struct export_args), M_TEMP, M_WAITOK);
238 error = copyin(mel.mel_exports, args, mel.mel_nexports *
239 sizeof(struct export_args));
240 if (error != 0) {
241 free(args, M_TEMP);
242 return error;
243 }
244 mel.mel_exports = args;
245
246 error = mountd_set_exports_list(&mel, l);
247
248 free(args, M_TEMP);
249 #endif /* !NFSSERVER */
250 } else {
251 #ifndef NFSSERVER
252 error = ENOSYS;
253 #else
254 error = copyin(SCARG(uap, argp), (void *)nsd, sizeof (*nsd));
255 if (error)
256 return (error);
257 if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
258 ((nfsd = nsd->nsd_nfsd)) != NULL &&
259 (nfsd->nfsd_slp->ns_flags & SLP_VALID)) {
260 slp = nfsd->nfsd_slp;
261
262 /*
263 * First check to see if another nfsd has already
264 * added this credential.
265 */
266 LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
267 nu_hash) {
268 if (kauth_cred_geteuid(nuidp->nu_cr) ==
269 nsd->nsd_cr.cr_uid &&
270 (!nfsd->nfsd_nd->nd_nam2 ||
271 netaddr_match(NU_NETFAM(nuidp),
272 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
273 break;
274 }
275 if (nuidp) {
276 kauth_cred_hold(nuidp->nu_cr);
277 nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
278 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
279 } else {
280 /*
281 * Nope, so we will.
282 */
283 if (slp->ns_numuids < nuidhash_max) {
284 slp->ns_numuids++;
285 nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
286 } else
287 nuidp = (struct nfsuid *)0;
288 if ((slp->ns_flags & SLP_VALID) == 0) {
289 if (nuidp)
290 kmem_free(nuidp, sizeof(*nuidp));
291 } else {
292 if (nuidp == (struct nfsuid *)0) {
293 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
294 LIST_REMOVE(nuidp, nu_hash);
295 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
296 nu_lru);
297 if (nuidp->nu_flag & NU_NAM)
298 m_freem(nuidp->nu_nam);
299 }
300 nuidp->nu_flag = 0;
301 kauth_uucred_to_cred(nuidp->nu_cr,
302 &nsd->nsd_cr);
303 nuidp->nu_timestamp = nsd->nsd_timestamp;
304 nuidp->nu_expire = time_second + nsd->nsd_ttl;
305 /*
306 * and save the session key in nu_key.
307 */
308 memcpy(nuidp->nu_key, nsd->nsd_key,
309 sizeof(nsd->nsd_key));
310 if (nfsd->nfsd_nd->nd_nam2) {
311 struct sockaddr_in *saddr;
312
313 saddr = mtod(nfsd->nfsd_nd->nd_nam2,
314 struct sockaddr_in *);
315 switch (saddr->sin_family) {
316 case AF_INET:
317 nuidp->nu_flag |= NU_INETADDR;
318 nuidp->nu_inetaddr =
319 saddr->sin_addr.s_addr;
320 break;
321 case AF_ISO:
322 default:
323 nuidp->nu_flag |= NU_NAM;
324 nuidp->nu_nam = m_copym(
325 nfsd->nfsd_nd->nd_nam2, 0,
326 M_COPYALL, M_WAIT);
327 break;
328 };
329 }
330 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
331 nu_lru);
332 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
333 nuidp, nu_hash);
334 kauth_cred_hold(nuidp->nu_cr);
335 nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
336 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
337 }
338 }
339 }
340 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
341 (nfsd = nsd->nsd_nfsd))
342 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
343 error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
344 #endif /* !NFSSERVER */
345 }
346 if (error == EINTR || error == ERESTART)
347 error = 0;
348 return (error);
349 }
350
351 #ifdef NFSSERVER
352 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
353
354 static struct nfssvc_sock *
355 nfsrv_sockalloc()
356 {
357 struct nfssvc_sock *slp;
358
359 slp = kmem_alloc(sizeof(*slp), KM_SLEEP);
360 memset(slp, 0, sizeof (struct nfssvc_sock));
361 mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
362 mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET);
363 cv_init(&slp->ns_cv, "nfsdsock");
364 TAILQ_INIT(&slp->ns_uidlruhead);
365 LIST_INIT(&slp->ns_tq);
366 SIMPLEQ_INIT(&slp->ns_sendq);
367 mutex_enter(&nfsd_lock);
368 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
369 mutex_exit(&nfsd_lock);
370
371 return slp;
372 }
373
374 static void
375 nfsrv_sockfree(struct nfssvc_sock *slp)
376 {
377
378 KASSERT(slp->ns_so == NULL);
379 KASSERT(slp->ns_fp == NULL);
380 KASSERT((slp->ns_flags & SLP_VALID) == 0);
381 mutex_destroy(&slp->ns_lock);
382 mutex_destroy(&slp->ns_alock);
383 cv_destroy(&slp->ns_cv);
384 kmem_free(slp, sizeof(*slp));
385 }
386
387 /*
388 * Adds a socket to the list for servicing by nfsds.
389 */
390 int
391 nfssvc_addsock(fp, mynam)
392 file_t *fp;
393 struct mbuf *mynam;
394 {
395 int siz;
396 struct nfssvc_sock *slp;
397 struct socket *so;
398 struct nfssvc_sock *tslp;
399 int error;
400 int val;
401
402 so = (struct socket *)fp->f_data;
403 tslp = (struct nfssvc_sock *)0;
404 /*
405 * Add it to the list, as required.
406 */
407 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
408 #ifdef INET6
409 if (so->so_proto->pr_domain->dom_family == AF_INET6)
410 tslp = nfs_udp6sock;
411 else
412 #endif
413 tslp = nfs_udpsock;
414 if (tslp->ns_flags & SLP_VALID) {
415 m_freem(mynam);
416 return (EPERM);
417 }
418 #ifdef ISO
419 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
420 tslp = nfs_cltpsock;
421 if (tslp->ns_flags & SLP_VALID) {
422 m_freem(mynam);
423 return (EPERM);
424 }
425 #endif /* ISO */
426 }
427 if (so->so_type == SOCK_STREAM)
428 siz = NFS_MAXPACKET + sizeof (u_long);
429 else
430 siz = NFS_MAXPACKET;
431 solock(so);
432 error = soreserve(so, siz, siz);
433 sounlock(so);
434 if (error) {
435 m_freem(mynam);
436 return (error);
437 }
438
439 /*
440 * Set protocol specific options { for now TCP only } and
441 * reserve some space. For datagram sockets, this can get called
442 * repeatedly for the same socket, but that isn't harmful.
443 */
444 if (so->so_type == SOCK_STREAM) {
445 val = 1;
446 so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
447 sizeof(val));
448 }
449 if ((so->so_proto->pr_domain->dom_family == AF_INET
450 #ifdef INET6
451 || so->so_proto->pr_domain->dom_family == AF_INET6
452 #endif
453 ) &&
454 so->so_proto->pr_protocol == IPPROTO_TCP) {
455 val = 1;
456 so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
457 sizeof(val));
458 }
459 solock(so);
460 so->so_rcv.sb_flags &= ~SB_NOINTR;
461 so->so_rcv.sb_timeo = 0;
462 so->so_snd.sb_flags &= ~SB_NOINTR;
463 so->so_snd.sb_timeo = 0;
464 sounlock(so);
465 if (tslp) {
466 slp = tslp;
467 } else {
468 slp = nfsrv_sockalloc();
469 }
470 slp->ns_so = so;
471 slp->ns_nam = mynam;
472 mutex_enter(&fp->f_lock);
473 fp->f_count++;
474 mutex_exit(&fp->f_lock);
475 slp->ns_fp = fp;
476 slp->ns_flags = SLP_VALID;
477 slp->ns_aflags = SLP_A_NEEDQ;
478 slp->ns_gflags = 0;
479 slp->ns_sflags = 0;
480 solock(so);
481 so->so_upcallarg = (void *)slp;
482 so->so_upcall = nfsrv_soupcall;
483 so->so_rcv.sb_flags |= SB_UPCALL;
484 sounlock(so);
485 nfsrv_wakenfsd(slp);
486 return (0);
487 }
488
489 /*
490 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
491 * until it is killed by a signal.
492 */
493 int
494 nfssvc_nfsd(nsd, argp, l)
495 struct nfsd_srvargs *nsd;
496 void *argp;
497 struct lwp *l;
498 {
499 struct timeval tv;
500 struct mbuf *m;
501 struct nfssvc_sock *slp;
502 struct nfsd *nfsd = nsd->nsd_nfsd;
503 struct nfsrv_descript *nd = NULL;
504 struct mbuf *mreq;
505 u_quad_t cur_usec;
506 int error = 0, cacherep, siz, sotype, writes_todo;
507 struct proc *p = l->l_proc;
508 int s;
509 bool doreinit;
510
511 #ifndef nolint
512 cacherep = RC_DOIT;
513 writes_todo = 0;
514 #endif
515 uvm_lwp_hold(l);
516 if (nfsd == NULL) {
517 nsd->nsd_nfsd = nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP);
518 memset(nfsd, 0, sizeof (struct nfsd));
519 cv_init(&nfsd->nfsd_cv, "nfsd");
520 nfsd->nfsd_procp = p;
521 mutex_enter(&nfsd_lock);
522 while ((nfssvc_sockhead_flag & SLP_INIT) != 0) {
523 KASSERT(nfs_numnfsd == 0);
524 cv_wait(&nfsd_initcv, &nfsd_lock);
525 }
526 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
527 nfs_numnfsd++;
528 mutex_exit(&nfsd_lock);
529 }
530 /*
531 * Loop getting rpc requests until SIGKILL.
532 */
533 for (;;) {
534 bool dummy;
535
536 if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
537 != 0) {
538 preempt();
539 }
540 if (nfsd->nfsd_slp == NULL) {
541 mutex_enter(&nfsd_lock);
542 while (nfsd->nfsd_slp == NULL &&
543 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
544 SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
545 nfsd_idle);
546 error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
547 if (error) {
548 slp = nfsd->nfsd_slp;
549 nfsd->nfsd_slp = NULL;
550 if (!slp)
551 SLIST_REMOVE(&nfsd_idle_head,
552 nfsd, nfsd, nfsd_idle);
553 mutex_exit(&nfsd_lock);
554 if (slp) {
555 nfsrv_wakenfsd(slp);
556 nfsrv_slpderef(slp);
557 }
558 goto done;
559 }
560 }
561 if (nfsd->nfsd_slp == NULL &&
562 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
563 slp = TAILQ_FIRST(&nfssvc_sockpending);
564 if (slp) {
565 KASSERT((slp->ns_gflags & SLP_G_DOREC)
566 != 0);
567 TAILQ_REMOVE(&nfssvc_sockpending, slp,
568 ns_pending);
569 slp->ns_gflags &= ~SLP_G_DOREC;
570 slp->ns_sref++;
571 nfsd->nfsd_slp = slp;
572 } else
573 nfsd_head_flag &= ~NFSD_CHECKSLP;
574 }
575 KASSERT(nfsd->nfsd_slp == NULL ||
576 nfsd->nfsd_slp->ns_sref > 0);
577 mutex_exit(&nfsd_lock);
578 if ((slp = nfsd->nfsd_slp) == NULL)
579 continue;
580 if (slp->ns_flags & SLP_VALID) {
581 bool more;
582
583 if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) {
584 nfsrv_rcv(slp);
585 }
586 if (nfsdsock_testbits(slp, SLP_A_DISCONN)) {
587 nfsrv_zapsock(slp);
588 }
589 error = nfsrv_dorec(slp, nfsd, &nd, &more);
590 getmicrotime(&tv);
591 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
592 (u_quad_t)tv.tv_usec;
593 writes_todo = 0;
594 if (error) {
595 struct nfsrv_descript *nd2;
596
597 mutex_enter(&nfsd_lock);
598 nd2 = LIST_FIRST(&slp->ns_tq);
599 if (nd2 != NULL &&
600 nd2->nd_time <= cur_usec) {
601 error = 0;
602 cacherep = RC_DOIT;
603 writes_todo = 1;
604 }
605 mutex_exit(&nfsd_lock);
606 }
607 if (error == 0 && more) {
608 nfsrv_wakenfsd(slp);
609 }
610 }
611 } else {
612 error = 0;
613 slp = nfsd->nfsd_slp;
614 }
615 KASSERT(slp != NULL);
616 KASSERT(nfsd->nfsd_slp == slp);
617 if (error || (slp->ns_flags & SLP_VALID) == 0) {
618 if (nd) {
619 nfsdreq_free(nd);
620 nd = NULL;
621 }
622 nfsd->nfsd_slp = NULL;
623 nfsrv_slpderef(slp);
624 continue;
625 }
626 sotype = slp->ns_so->so_type;
627 if (nd) {
628 getmicrotime(&nd->nd_starttime);
629 if (nd->nd_nam2)
630 nd->nd_nam = nd->nd_nam2;
631 else
632 nd->nd_nam = slp->ns_nam;
633
634 /*
635 * Check to see if authorization is needed.
636 */
637 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
638 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
639 nsd->nsd_haddr = mtod(nd->nd_nam,
640 struct sockaddr_in *)->sin_addr.s_addr;
641 nsd->nsd_authlen = nfsd->nfsd_authlen;
642 nsd->nsd_verflen = nfsd->nfsd_verflen;
643 if (!copyout(nfsd->nfsd_authstr,
644 nsd->nsd_authstr, nfsd->nfsd_authlen) &&
645 !copyout(nfsd->nfsd_verfstr,
646 nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
647 !copyout(nsd, argp, sizeof (*nsd))) {
648 uvm_lwp_rele(l);
649 return (ENEEDAUTH);
650 }
651 cacherep = RC_DROPIT;
652 } else
653 cacherep = nfsrv_getcache(nd, slp, &mreq);
654
655 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
656 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
657 nd->nd_procnum = NFSPROC_NOOP;
658 nd->nd_repstat =
659 (NFSERR_AUTHERR | AUTH_TOOWEAK);
660 cacherep = RC_DOIT;
661 }
662 }
663
664 /*
665 * Loop to get all the write rpc relies that have been
666 * gathered together.
667 */
668 do {
669 switch (cacherep) {
670 case RC_DOIT:
671 mreq = NULL;
672 netexport_rdlock();
673 if (writes_todo || nd == NULL ||
674 (!(nd->nd_flag & ND_NFSV3) &&
675 nd->nd_procnum == NFSPROC_WRITE &&
676 nfsrvw_procrastinate > 0))
677 error = nfsrv_writegather(&nd, slp,
678 l, &mreq);
679 else
680 error =
681 (*(nfsrv3_procs[nd->nd_procnum]))
682 (nd, slp, l, &mreq);
683 netexport_rdunlock();
684 if (mreq == NULL) {
685 if (nd != NULL) {
686 if (nd->nd_nam2)
687 m_free(nd->nd_nam2);
688 }
689 break;
690 }
691 if (error) {
692 nfsstats.srv_errs++;
693 nfsrv_updatecache(nd, false, mreq);
694 if (nd->nd_nam2)
695 m_freem(nd->nd_nam2);
696 break;
697 }
698 nfsstats.srvrpccnt[nd->nd_procnum]++;
699 nfsrv_updatecache(nd, true, mreq);
700 nd->nd_mrep = (struct mbuf *)0;
701 case RC_REPLY:
702 m = mreq;
703 siz = 0;
704 while (m) {
705 siz += m->m_len;
706 m = m->m_next;
707 }
708 if (siz <= 0 || siz > NFS_MAXPACKET) {
709 printf("mbuf siz=%d\n",siz);
710 panic("Bad nfs svc reply");
711 }
712 m = mreq;
713 m->m_pkthdr.len = siz;
714 m->m_pkthdr.rcvif = (struct ifnet *)0;
715 /*
716 * For stream protocols, prepend a Sun RPC
717 * Record Mark.
718 */
719 if (sotype == SOCK_STREAM) {
720 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
721 *mtod(m, u_int32_t *) =
722 htonl(0x80000000 | siz);
723 }
724 nd->nd_mreq = m;
725 if (nfsrtton) {
726 nfsd_rt(slp->ns_so->so_type, nd,
727 cacherep);
728 }
729 error = nfsdsock_sendreply(slp, nd);
730 nd = NULL;
731 if (error == EPIPE)
732 nfsrv_zapsock(slp);
733 if (error == EINTR || error == ERESTART) {
734 nfsd->nfsd_slp = NULL;
735 nfsrv_slpderef(slp);
736 goto done;
737 }
738 break;
739 case RC_DROPIT:
740 if (nfsrtton)
741 nfsd_rt(sotype, nd, cacherep);
742 m_freem(nd->nd_mrep);
743 m_freem(nd->nd_nam2);
744 break;
745 }
746 if (nd) {
747 nfsdreq_free(nd);
748 nd = NULL;
749 }
750
751 /*
752 * Check to see if there are outstanding writes that
753 * need to be serviced.
754 */
755 getmicrotime(&tv);
756 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
757 (u_quad_t)tv.tv_usec;
758 s = splsoftclock();
759 if (LIST_FIRST(&slp->ns_tq) &&
760 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
761 cacherep = RC_DOIT;
762 writes_todo = 1;
763 } else
764 writes_todo = 0;
765 splx(s);
766 } while (writes_todo);
767 if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) {
768 nfsd->nfsd_slp = NULL;
769 nfsrv_slpderef(slp);
770 }
771 }
772 done:
773 mutex_enter(&nfsd_lock);
774 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
775 doreinit = --nfs_numnfsd == 0;
776 if (doreinit)
777 nfssvc_sockhead_flag |= SLP_INIT;
778 mutex_exit(&nfsd_lock);
779 cv_destroy(&nfsd->nfsd_cv);
780 kmem_free(nfsd, sizeof(*nfsd));
781 nsd->nsd_nfsd = NULL;
782 if (doreinit)
783 nfsrv_init(true); /* Reinitialize everything */
784 uvm_lwp_rele(l);
785 return (error);
786 }
787
788 /*
789 * Shut down a socket associated with an nfssvc_sock structure.
790 * Should be called with the send lock set, if required.
791 * The trick here is to increment the sref at the start, so that the nfsds
792 * will stop using it and clear ns_flag at the end so that it will not be
793 * reassigned during cleanup.
794 *
795 * called at splsoftnet.
796 */
797 void
798 nfsrv_zapsock(slp)
799 struct nfssvc_sock *slp;
800 {
801 struct nfsuid *nuidp, *nnuidp;
802 struct nfsrv_descript *nwp;
803 struct socket *so;
804 struct mbuf *m;
805
806 if (nfsdsock_drain(slp)) {
807 return;
808 }
809 mutex_enter(&nfsd_lock);
810 if (slp->ns_gflags & SLP_G_DOREC) {
811 TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
812 slp->ns_gflags &= ~SLP_G_DOREC;
813 }
814 mutex_exit(&nfsd_lock);
815
816 so = slp->ns_so;
817 KASSERT(so != NULL);
818 solock(so);
819 so->so_upcall = NULL;
820 so->so_upcallarg = NULL;
821 so->so_rcv.sb_flags &= ~SB_UPCALL;
822 soshutdown(so, SHUT_RDWR);
823 sounlock(so);
824
825 if (slp->ns_nam)
826 m_free(slp->ns_nam);
827 m_freem(slp->ns_raw);
828 m = slp->ns_rec;
829 while (m != NULL) {
830 struct mbuf *n;
831
832 n = m->m_nextpkt;
833 m_freem(m);
834 m = n;
835 }
836 for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
837 nuidp = nnuidp) {
838 nnuidp = TAILQ_NEXT(nuidp, nu_lru);
839 LIST_REMOVE(nuidp, nu_hash);
840 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
841 if (nuidp->nu_flag & NU_NAM)
842 m_freem(nuidp->nu_nam);
843 kmem_free(nuidp, sizeof(*nuidp));
844 }
845 mutex_enter(&nfsd_lock);
846 while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) {
847 LIST_REMOVE(nwp, nd_tq);
848 mutex_exit(&nfsd_lock);
849 nfsdreq_free(nwp);
850 mutex_enter(&nfsd_lock);
851 }
852 mutex_exit(&nfsd_lock);
853 }
854
855 /*
856 * Derefence a server socket structure. If it has no more references and
857 * is no longer valid, you can throw it away.
858 */
859 void
860 nfsrv_slpderef(slp)
861 struct nfssvc_sock *slp;
862 {
863 uint32_t ref;
864
865 mutex_enter(&nfsd_lock);
866 KASSERT(slp->ns_sref > 0);
867 ref = --slp->ns_sref;
868 mutex_exit(&nfsd_lock);
869 if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) {
870 file_t *fp;
871
872 mutex_enter(&nfsd_lock);
873 KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0);
874 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
875 mutex_exit(&nfsd_lock);
876
877 fp = slp->ns_fp;
878 if (fp != NULL) {
879 slp->ns_fp = NULL;
880 KASSERT(fp != NULL);
881 KASSERT(fp->f_data == slp->ns_so);
882 KASSERT(fp->f_count > 0);
883 closef(fp);
884 slp->ns_so = NULL;
885 }
886
887 nfsrv_sockfree(slp);
888 }
889 }
890
891 /*
892 * Initialize the data structures for the server.
893 * Handshake with any new nfsds starting up to avoid any chance of
894 * corruption.
895 */
896 void
897 nfsrv_init(terminating)
898 int terminating;
899 {
900 struct nfssvc_sock *slp;
901
902 if (!terminating) {
903 mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
904 cv_init(&nfsd_initcv, "nfsdinit");
905 }
906
907 mutex_enter(&nfsd_lock);
908 if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0)
909 panic("nfsd init");
910 nfssvc_sockhead_flag |= SLP_INIT;
911
912 if (terminating) {
913 KASSERT(SLIST_EMPTY(&nfsd_idle_head));
914 KASSERT(TAILQ_EMPTY(&nfsd_head));
915 while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
916 mutex_exit(&nfsd_lock);
917 KASSERT(slp->ns_sref == 0);
918 slp->ns_sref++;
919 nfsrv_zapsock(slp);
920 nfsrv_slpderef(slp);
921 mutex_enter(&nfsd_lock);
922 }
923 KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
924 mutex_exit(&nfsd_lock);
925 nfsrv_cleancache(); /* And clear out server cache */
926 } else {
927 mutex_exit(&nfsd_lock);
928 nfs_pub.np_valid = 0;
929 }
930
931 TAILQ_INIT(&nfssvc_sockhead);
932 TAILQ_INIT(&nfssvc_sockpending);
933
934 TAILQ_INIT(&nfsd_head);
935 SLIST_INIT(&nfsd_idle_head);
936 nfsd_head_flag &= ~NFSD_CHECKSLP;
937
938 nfs_udpsock = nfsrv_sockalloc();
939
940 #ifdef INET6
941 nfs_udp6sock = nfsrv_sockalloc();
942 #endif
943
944 #ifdef ISO
945 nfs_cltpsock = nfsrv_sockalloc();
946 #endif
947
948 mutex_enter(&nfsd_lock);
949 nfssvc_sockhead_flag &= ~SLP_INIT;
950 cv_broadcast(&nfsd_initcv);
951 mutex_exit(&nfsd_lock);
952 }
953
954 /*
955 * Add entries to the server monitor log.
956 */
957 static void
958 nfsd_rt(sotype, nd, cacherep)
959 int sotype;
960 struct nfsrv_descript *nd;
961 int cacherep;
962 {
963 struct timeval tv;
964 struct drt *rt;
965
966 rt = &nfsdrt.drt[nfsdrt.pos];
967 if (cacherep == RC_DOIT)
968 rt->flag = 0;
969 else if (cacherep == RC_REPLY)
970 rt->flag = DRT_CACHEREPLY;
971 else
972 rt->flag = DRT_CACHEDROP;
973 if (sotype == SOCK_STREAM)
974 rt->flag |= DRT_TCP;
975 if (nd->nd_flag & ND_NFSV3)
976 rt->flag |= DRT_NFSV3;
977 rt->proc = nd->nd_procnum;
978 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
979 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
980 else
981 rt->ipadr = INADDR_ANY;
982 getmicrotime(&tv);
983 rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
984 (tv.tv_usec - nd->nd_starttime.tv_usec);
985 rt->tstamp = tv;
986 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
987 }
988 #endif /* NFSSERVER */
989
990 #ifdef NFS
991
992 int nfs_defect = 0;
993 /*
994 * Asynchronous I/O threads for client nfs.
995 * They do read-ahead and write-behind operations on the block I/O cache.
996 * Never returns unless it fails or gets killed.
997 */
998
999 static void
1000 nfssvc_iod(void *arg)
1001 {
1002 struct buf *bp;
1003 struct nfs_iod *myiod;
1004 struct nfsmount *nmp;
1005
1006 myiod = kmem_alloc(sizeof(*myiod), KM_SLEEP);
1007 mutex_init(&myiod->nid_lock, MUTEX_DEFAULT, IPL_NONE);
1008 cv_init(&myiod->nid_cv, "nfsiod");
1009 myiod->nid_exiting = false;
1010 myiod->nid_mount = NULL;
1011 mutex_enter(&nfs_iodlist_lock);
1012 LIST_INSERT_HEAD(&nfs_iodlist_all, myiod, nid_all);
1013 mutex_exit(&nfs_iodlist_lock);
1014
1015 for (;;) {
1016 mutex_enter(&nfs_iodlist_lock);
1017 LIST_INSERT_HEAD(&nfs_iodlist_idle, myiod, nid_idle);
1018 mutex_exit(&nfs_iodlist_lock);
1019
1020 mutex_enter(&myiod->nid_lock);
1021 while (/*CONSTCOND*/ true) {
1022 nmp = myiod->nid_mount;
1023 if (nmp) {
1024 myiod->nid_mount = NULL;
1025 break;
1026 }
1027 if (__predict_false(myiod->nid_exiting)) {
1028 /*
1029 * drop nid_lock to preserve locking order.
1030 */
1031 mutex_exit(&myiod->nid_lock);
1032 mutex_enter(&nfs_iodlist_lock);
1033 mutex_enter(&myiod->nid_lock);
1034 /*
1035 * recheck nid_mount because nfs_asyncio can
1036 * pick us in the meantime as we are still on
1037 * nfs_iodlist_lock.
1038 */
1039 if (myiod->nid_mount != NULL) {
1040 mutex_exit(&nfs_iodlist_lock);
1041 continue;
1042 }
1043 LIST_REMOVE(myiod, nid_idle);
1044 mutex_exit(&nfs_iodlist_lock);
1045 goto quit;
1046 }
1047 cv_wait(&myiod->nid_cv, &myiod->nid_lock);
1048 }
1049 mutex_exit(&myiod->nid_lock);
1050
1051 mutex_enter(&nmp->nm_lock);
1052 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
1053 /* Take one off the front of the list */
1054 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
1055 nmp->nm_bufqlen--;
1056 if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
1057 cv_broadcast(&nmp->nm_aiocv);
1058 }
1059 mutex_exit(&nmp->nm_lock);
1060 KERNEL_LOCK(1, curlwp);
1061 (void)nfs_doio(bp);
1062 KERNEL_UNLOCK_LAST(curlwp);
1063 mutex_enter(&nmp->nm_lock);
1064 /*
1065 * If there are more than one iod on this mount,
1066 * then defect so that the iods can be shared out
1067 * fairly between the mounts
1068 */
1069 if (nfs_defect && nmp->nm_bufqiods > 1) {
1070 break;
1071 }
1072 }
1073 KASSERT(nmp->nm_bufqiods > 0);
1074 nmp->nm_bufqiods--;
1075 mutex_exit(&nmp->nm_lock);
1076 }
1077 quit:
1078 KASSERT(myiod->nid_mount == NULL);
1079 mutex_exit(&myiod->nid_lock);
1080
1081 cv_destroy(&myiod->nid_cv);
1082 mutex_destroy(&myiod->nid_lock);
1083 kmem_free(myiod, sizeof(*myiod));
1084
1085 kthread_exit(0);
1086 }
1087
1088 void
1089 nfs_iodinit()
1090 {
1091
1092 mutex_init(&nfs_iodlist_lock, MUTEX_DEFAULT, IPL_NONE);
1093 LIST_INIT(&nfs_iodlist_all);
1094 LIST_INIT(&nfs_iodlist_idle);
1095 }
1096
1097 int
1098 nfs_set_niothreads(int newval)
1099 {
1100 struct nfs_iod *nid;
1101 int error = 0;
1102 int hold_count;
1103
1104 KERNEL_UNLOCK_ALL(curlwp, &hold_count);
1105
1106 mutex_enter(&nfs_iodlist_lock);
1107 /* clamp to sane range */
1108 nfs_niothreads = max(0, min(newval, NFS_MAXASYNCDAEMON));
1109
1110 while (nfs_numasync != nfs_niothreads && error == 0) {
1111 while (nfs_numasync < nfs_niothreads) {
1112
1113 /*
1114 * kthread_create can wait for pagedaemon and
1115 * pagedaemon can wait for nfsiod which needs to acquire
1116 * nfs_iodlist_lock.
1117 */
1118
1119 mutex_exit(&nfs_iodlist_lock);
1120 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
1121 nfssvc_iod, NULL, NULL, "nfsio");
1122 mutex_enter(&nfs_iodlist_lock);
1123 if (error) {
1124 /* give up */
1125 nfs_niothreads = nfs_numasync;
1126 break;
1127 }
1128 nfs_numasync++;
1129 }
1130 while (nfs_numasync > nfs_niothreads) {
1131 nid = LIST_FIRST(&nfs_iodlist_all);
1132 if (nid == NULL) {
1133 /* iod has not started yet. */
1134 kpause("nfsiorm", false, hz, &nfs_iodlist_lock);
1135 continue;
1136 }
1137 LIST_REMOVE(nid, nid_all);
1138 mutex_enter(&nid->nid_lock);
1139 KASSERT(!nid->nid_exiting);
1140 nid->nid_exiting = true;
1141 cv_signal(&nid->nid_cv);
1142 mutex_exit(&nid->nid_lock);
1143 nfs_numasync--;
1144 }
1145 }
1146 mutex_exit(&nfs_iodlist_lock);
1147
1148 KERNEL_LOCK(hold_count, curlwp);
1149 return error;
1150 }
1151
1152 /*
1153 * Get an authorization string for the uid by having the mount_nfs sitting
1154 * on this mount point porpous out of the kernel and do it.
1155 */
1156 int
1157 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
1158 struct nfsmount *nmp;
1159 struct nfsreq *rep;
1160 kauth_cred_t cred;
1161 char **auth_str;
1162 int *auth_len;
1163 char *verf_str;
1164 int *verf_len;
1165 NFSKERBKEY_T key; /* return session key */
1166 {
1167 int error = 0;
1168
1169 while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
1170 nmp->nm_iflag |= NFSMNT_WANTAUTH;
1171 (void) tsleep((void *)&nmp->nm_authtype, PSOCK,
1172 "nfsauth1", 2 * hz);
1173 error = nfs_sigintr(nmp, rep, rep->r_lwp);
1174 if (error) {
1175 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1176 return (error);
1177 }
1178 }
1179 nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
1180 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1181 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1182 nmp->nm_verfstr = verf_str;
1183 nmp->nm_verflen = *verf_len;
1184 nmp->nm_authuid = kauth_cred_geteuid(cred);
1185 wakeup((void *)&nmp->nm_authstr);
1186
1187 /*
1188 * And wait for mount_nfs to do its stuff.
1189 */
1190 while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
1191 (void) tsleep((void *)&nmp->nm_authlen, PSOCK,
1192 "nfsauth2", 2 * hz);
1193 error = nfs_sigintr(nmp, rep, rep->r_lwp);
1194 }
1195 if (nmp->nm_iflag & NFSMNT_AUTHERR) {
1196 nmp->nm_iflag &= ~NFSMNT_AUTHERR;
1197 error = EAUTH;
1198 }
1199 if (error)
1200 free((void *)*auth_str, M_TEMP);
1201 else {
1202 *auth_len = nmp->nm_authlen;
1203 *verf_len = nmp->nm_verflen;
1204 memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
1205 }
1206 nmp->nm_iflag &= ~NFSMNT_HASAUTH;
1207 nmp->nm_iflag |= NFSMNT_WAITAUTH;
1208 if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
1209 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1210 wakeup((void *)&nmp->nm_authtype);
1211 }
1212 return (error);
1213 }
1214
1215 /*
1216 * Get a nickname authenticator and verifier.
1217 */
1218 int
1219 nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
1220 int *auth_len, char *verf_str, int verf_len)
1221 {
1222 struct timeval ktvin, ktvout, tv;
1223 struct nfsuid *nuidp;
1224 u_int32_t *nickp, *verfp;
1225
1226 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1227
1228 #ifdef DIAGNOSTIC
1229 if (verf_len < (4 * NFSX_UNSIGNED))
1230 panic("nfs_getnickauth verf too small");
1231 #endif
1232 LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
1233 if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
1234 break;
1235 }
1236 if (!nuidp || nuidp->nu_expire < time_second)
1237 return (EACCES);
1238
1239 /*
1240 * Move to the end of the lru list (end of lru == most recently used).
1241 */
1242 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1243 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1244
1245 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1246 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1247 *nickp = txdr_unsigned(nuidp->nu_nickname);
1248 *auth_str = (char *)nickp;
1249 *auth_len = 2 * NFSX_UNSIGNED;
1250
1251 /*
1252 * Now we must encrypt the verifier and package it up.
1253 */
1254 verfp = (u_int32_t *)verf_str;
1255 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1256 getmicrotime(&tv);
1257 if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
1258 (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
1259 tv.tv_usec > nuidp->nu_timestamp.tv_usec))
1260 nuidp->nu_timestamp = tv;
1261 else
1262 nuidp->nu_timestamp.tv_usec++;
1263 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1264 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1265
1266 /*
1267 * Now encrypt the timestamp verifier in ecb mode using the session
1268 * key.
1269 */
1270 #ifdef NFSKERB
1271 XXX
1272 #endif
1273
1274 *verfp++ = ktvout.tv_sec;
1275 *verfp++ = ktvout.tv_usec;
1276 *verfp = 0;
1277 return (0);
1278 }
1279
1280 /*
1281 * Save the current nickname in a hash list entry on the mount point.
1282 */
1283 int
1284 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1285 struct nfsmount *nmp;
1286 kauth_cred_t cred;
1287 int len;
1288 NFSKERBKEY_T key;
1289 struct mbuf **mdp;
1290 char **dposp;
1291 struct mbuf *mrep;
1292 {
1293 struct nfsuid *nuidp;
1294 u_int32_t *tl;
1295 int32_t t1;
1296 struct mbuf *md = *mdp;
1297 struct timeval ktvin, ktvout;
1298 u_int32_t nick;
1299 char *dpos = *dposp, *cp2;
1300 int deltasec, error = 0;
1301
1302 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1303
1304 if (len == (3 * NFSX_UNSIGNED)) {
1305 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1306 ktvin.tv_sec = *tl++;
1307 ktvin.tv_usec = *tl++;
1308 nick = fxdr_unsigned(u_int32_t, *tl);
1309
1310 /*
1311 * Decrypt the timestamp in ecb mode.
1312 */
1313 #ifdef NFSKERB
1314 XXX
1315 #endif
1316 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1317 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1318 deltasec = time_second - ktvout.tv_sec;
1319 if (deltasec < 0)
1320 deltasec = -deltasec;
1321 /*
1322 * If ok, add it to the hash list for the mount point.
1323 */
1324 if (deltasec <= NFS_KERBCLOCKSKEW) {
1325 if (nmp->nm_numuids < nuidhash_max) {
1326 nmp->nm_numuids++;
1327 nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
1328 } else {
1329 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1330 LIST_REMOVE(nuidp, nu_hash);
1331 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1332 nu_lru);
1333 }
1334 nuidp->nu_flag = 0;
1335 kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
1336 nuidp->nu_expire = time_second + NFS_KERBTTL;
1337 nuidp->nu_timestamp = ktvout;
1338 nuidp->nu_nickname = nick;
1339 memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
1340 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1341 nu_lru);
1342 LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
1343 nuidp, nu_hash);
1344 }
1345 } else
1346 nfsm_adv(nfsm_rndup(len));
1347 nfsmout:
1348 *mdp = md;
1349 *dposp = dpos;
1350 return (error);
1351 }
1352 #endif /* NFS */
1353