1 /* $NetBSD: nfs_syscalls.c,v 1.140.4.1 2009/04/13 21:21:30 snj 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.140.4.1 2009/04/13 21:21:30 snj 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 #ifdef INET6
322 case AF_INET6:
323 #endif
324 #ifdef ISO
325 case AF_ISO:
326 #endif
327 nuidp->nu_flag |= NU_NAM;
328 nuidp->nu_nam = m_copym(
329 nfsd->nfsd_nd->nd_nam2, 0,
330 M_COPYALL, M_WAIT);
331 break;
332 default:
333 return EAFNOSUPPORT;
334 };
335 }
336 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
337 nu_lru);
338 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
339 nuidp, nu_hash);
340 kauth_cred_hold(nuidp->nu_cr);
341 nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
342 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
343 }
344 }
345 }
346 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
347 (nfsd = nsd->nsd_nfsd))
348 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
349 error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
350 #endif /* !NFSSERVER */
351 }
352 if (error == EINTR || error == ERESTART)
353 error = 0;
354 return (error);
355 }
356
357 #ifdef NFSSERVER
358 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
359
360 static struct nfssvc_sock *
361 nfsrv_sockalloc()
362 {
363 struct nfssvc_sock *slp;
364
365 slp = kmem_alloc(sizeof(*slp), KM_SLEEP);
366 memset(slp, 0, sizeof (struct nfssvc_sock));
367 mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
368 mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET);
369 cv_init(&slp->ns_cv, "nfsdsock");
370 TAILQ_INIT(&slp->ns_uidlruhead);
371 LIST_INIT(&slp->ns_tq);
372 SIMPLEQ_INIT(&slp->ns_sendq);
373 mutex_enter(&nfsd_lock);
374 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
375 mutex_exit(&nfsd_lock);
376
377 return slp;
378 }
379
380 static void
381 nfsrv_sockfree(struct nfssvc_sock *slp)
382 {
383
384 KASSERT(slp->ns_so == NULL);
385 KASSERT(slp->ns_fp == NULL);
386 KASSERT((slp->ns_flags & SLP_VALID) == 0);
387 mutex_destroy(&slp->ns_lock);
388 mutex_destroy(&slp->ns_alock);
389 cv_destroy(&slp->ns_cv);
390 kmem_free(slp, sizeof(*slp));
391 }
392
393 /*
394 * Adds a socket to the list for servicing by nfsds.
395 */
396 int
397 nfssvc_addsock(fp, mynam)
398 file_t *fp;
399 struct mbuf *mynam;
400 {
401 int siz;
402 struct nfssvc_sock *slp;
403 struct socket *so;
404 struct nfssvc_sock *tslp;
405 int error;
406 int val;
407
408 so = (struct socket *)fp->f_data;
409 tslp = (struct nfssvc_sock *)0;
410 /*
411 * Add it to the list, as required.
412 */
413 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
414 #ifdef INET6
415 if (so->so_proto->pr_domain->dom_family == AF_INET6)
416 tslp = nfs_udp6sock;
417 else
418 #endif
419 tslp = nfs_udpsock;
420 if (tslp->ns_flags & SLP_VALID) {
421 m_freem(mynam);
422 return (EPERM);
423 }
424 #ifdef ISO
425 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
426 tslp = nfs_cltpsock;
427 if (tslp->ns_flags & SLP_VALID) {
428 m_freem(mynam);
429 return (EPERM);
430 }
431 #endif /* ISO */
432 }
433 if (so->so_type == SOCK_STREAM)
434 siz = NFS_MAXPACKET + sizeof (u_long);
435 else
436 siz = NFS_MAXPACKET;
437 solock(so);
438 error = soreserve(so, siz, siz);
439 sounlock(so);
440 if (error) {
441 m_freem(mynam);
442 return (error);
443 }
444
445 /*
446 * Set protocol specific options { for now TCP only } and
447 * reserve some space. For datagram sockets, this can get called
448 * repeatedly for the same socket, but that isn't harmful.
449 */
450 if (so->so_type == SOCK_STREAM) {
451 val = 1;
452 so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
453 sizeof(val));
454 }
455 if ((so->so_proto->pr_domain->dom_family == AF_INET
456 #ifdef INET6
457 || so->so_proto->pr_domain->dom_family == AF_INET6
458 #endif
459 ) &&
460 so->so_proto->pr_protocol == IPPROTO_TCP) {
461 val = 1;
462 so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
463 sizeof(val));
464 }
465 solock(so);
466 so->so_rcv.sb_flags &= ~SB_NOINTR;
467 so->so_rcv.sb_timeo = 0;
468 so->so_snd.sb_flags &= ~SB_NOINTR;
469 so->so_snd.sb_timeo = 0;
470 sounlock(so);
471 if (tslp) {
472 slp = tslp;
473 } else {
474 slp = nfsrv_sockalloc();
475 }
476 slp->ns_so = so;
477 slp->ns_nam = mynam;
478 mutex_enter(&fp->f_lock);
479 fp->f_count++;
480 mutex_exit(&fp->f_lock);
481 slp->ns_fp = fp;
482 slp->ns_flags = SLP_VALID;
483 slp->ns_aflags = SLP_A_NEEDQ;
484 slp->ns_gflags = 0;
485 slp->ns_sflags = 0;
486 solock(so);
487 so->so_upcallarg = (void *)slp;
488 so->so_upcall = nfsrv_soupcall;
489 so->so_rcv.sb_flags |= SB_UPCALL;
490 sounlock(so);
491 nfsrv_wakenfsd(slp);
492 return (0);
493 }
494
495 /*
496 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
497 * until it is killed by a signal.
498 */
499 int
500 nfssvc_nfsd(nsd, argp, l)
501 struct nfsd_srvargs *nsd;
502 void *argp;
503 struct lwp *l;
504 {
505 struct timeval tv;
506 struct mbuf *m;
507 struct nfssvc_sock *slp;
508 struct nfsd *nfsd = nsd->nsd_nfsd;
509 struct nfsrv_descript *nd = NULL;
510 struct mbuf *mreq;
511 u_quad_t cur_usec;
512 int error = 0, cacherep, siz, sotype, writes_todo;
513 struct proc *p = l->l_proc;
514 bool doreinit;
515
516 #ifndef nolint
517 cacherep = RC_DOIT;
518 writes_todo = 0;
519 #endif
520 uvm_lwp_hold(l);
521 if (nfsd == NULL) {
522 nsd->nsd_nfsd = nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP);
523 memset(nfsd, 0, sizeof (struct nfsd));
524 cv_init(&nfsd->nfsd_cv, "nfsd");
525 nfsd->nfsd_procp = p;
526 mutex_enter(&nfsd_lock);
527 while ((nfssvc_sockhead_flag & SLP_INIT) != 0) {
528 KASSERT(nfs_numnfsd == 0);
529 cv_wait(&nfsd_initcv, &nfsd_lock);
530 }
531 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
532 nfs_numnfsd++;
533 mutex_exit(&nfsd_lock);
534 }
535 /*
536 * Loop getting rpc requests until SIGKILL.
537 */
538 for (;;) {
539 bool dummy;
540
541 if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
542 != 0) {
543 preempt();
544 }
545 if (nfsd->nfsd_slp == NULL) {
546 mutex_enter(&nfsd_lock);
547 while (nfsd->nfsd_slp == NULL &&
548 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
549 SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
550 nfsd_idle);
551 error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
552 if (error) {
553 slp = nfsd->nfsd_slp;
554 nfsd->nfsd_slp = NULL;
555 if (!slp)
556 SLIST_REMOVE(&nfsd_idle_head,
557 nfsd, nfsd, nfsd_idle);
558 mutex_exit(&nfsd_lock);
559 if (slp) {
560 nfsrv_wakenfsd(slp);
561 nfsrv_slpderef(slp);
562 }
563 goto done;
564 }
565 }
566 if (nfsd->nfsd_slp == NULL &&
567 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
568 slp = TAILQ_FIRST(&nfssvc_sockpending);
569 if (slp) {
570 KASSERT((slp->ns_gflags & SLP_G_DOREC)
571 != 0);
572 TAILQ_REMOVE(&nfssvc_sockpending, slp,
573 ns_pending);
574 slp->ns_gflags &= ~SLP_G_DOREC;
575 slp->ns_sref++;
576 nfsd->nfsd_slp = slp;
577 } else
578 nfsd_head_flag &= ~NFSD_CHECKSLP;
579 }
580 KASSERT(nfsd->nfsd_slp == NULL ||
581 nfsd->nfsd_slp->ns_sref > 0);
582 mutex_exit(&nfsd_lock);
583 if ((slp = nfsd->nfsd_slp) == NULL)
584 continue;
585 if (slp->ns_flags & SLP_VALID) {
586 bool more;
587
588 if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) {
589 nfsrv_rcv(slp);
590 }
591 if (nfsdsock_testbits(slp, SLP_A_DISCONN)) {
592 nfsrv_zapsock(slp);
593 }
594 error = nfsrv_dorec(slp, nfsd, &nd, &more);
595 getmicrotime(&tv);
596 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
597 (u_quad_t)tv.tv_usec;
598 writes_todo = 0;
599 if (error) {
600 struct nfsrv_descript *nd2;
601
602 mutex_enter(&nfsd_lock);
603 nd2 = LIST_FIRST(&slp->ns_tq);
604 if (nd2 != NULL &&
605 nd2->nd_time <= cur_usec) {
606 error = 0;
607 cacherep = RC_DOIT;
608 writes_todo = 1;
609 }
610 mutex_exit(&nfsd_lock);
611 }
612 if (error == 0 && more) {
613 nfsrv_wakenfsd(slp);
614 }
615 }
616 } else {
617 error = 0;
618 slp = nfsd->nfsd_slp;
619 }
620 KASSERT(slp != NULL);
621 KASSERT(nfsd->nfsd_slp == slp);
622 if (error || (slp->ns_flags & SLP_VALID) == 0) {
623 if (nd) {
624 nfsdreq_free(nd);
625 nd = NULL;
626 }
627 nfsd->nfsd_slp = NULL;
628 nfsrv_slpderef(slp);
629 continue;
630 }
631 sotype = slp->ns_so->so_type;
632 if (nd) {
633 getmicrotime(&nd->nd_starttime);
634 if (nd->nd_nam2)
635 nd->nd_nam = nd->nd_nam2;
636 else
637 nd->nd_nam = slp->ns_nam;
638
639 /*
640 * Check to see if authorization is needed.
641 */
642 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
643 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
644 nsd->nsd_haddr = mtod(nd->nd_nam,
645 struct sockaddr_in *)->sin_addr.s_addr;
646 nsd->nsd_authlen = nfsd->nfsd_authlen;
647 nsd->nsd_verflen = nfsd->nfsd_verflen;
648 if (!copyout(nfsd->nfsd_authstr,
649 nsd->nsd_authstr, nfsd->nfsd_authlen) &&
650 !copyout(nfsd->nfsd_verfstr,
651 nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
652 !copyout(nsd, argp, sizeof (*nsd))) {
653 uvm_lwp_rele(l);
654 return (ENEEDAUTH);
655 }
656 cacherep = RC_DROPIT;
657 } else
658 cacherep = nfsrv_getcache(nd, slp, &mreq);
659
660 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
661 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
662 nd->nd_procnum = NFSPROC_NOOP;
663 nd->nd_repstat =
664 (NFSERR_AUTHERR | AUTH_TOOWEAK);
665 cacherep = RC_DOIT;
666 }
667 }
668
669 /*
670 * Loop to get all the write rpc relies that have been
671 * gathered together.
672 */
673 do {
674 switch (cacherep) {
675 case RC_DOIT:
676 mreq = NULL;
677 netexport_rdlock();
678 if (writes_todo || nd == NULL ||
679 (!(nd->nd_flag & ND_NFSV3) &&
680 nd->nd_procnum == NFSPROC_WRITE &&
681 nfsrvw_procrastinate > 0))
682 error = nfsrv_writegather(&nd, slp,
683 l, &mreq);
684 else
685 error =
686 (*(nfsrv3_procs[nd->nd_procnum]))
687 (nd, slp, l, &mreq);
688 netexport_rdunlock();
689 if (mreq == NULL) {
690 if (nd != NULL) {
691 if (nd->nd_nam2)
692 m_free(nd->nd_nam2);
693 }
694 break;
695 }
696 if (error) {
697 nfsstats.srv_errs++;
698 nfsrv_updatecache(nd, false, mreq);
699 if (nd->nd_nam2)
700 m_freem(nd->nd_nam2);
701 break;
702 }
703 nfsstats.srvrpccnt[nd->nd_procnum]++;
704 nfsrv_updatecache(nd, true, mreq);
705 nd->nd_mrep = (struct mbuf *)0;
706 case RC_REPLY:
707 m = mreq;
708 siz = 0;
709 while (m) {
710 siz += m->m_len;
711 m = m->m_next;
712 }
713 if (siz <= 0 || siz > NFS_MAXPACKET) {
714 printf("mbuf siz=%d\n",siz);
715 panic("Bad nfs svc reply");
716 }
717 m = mreq;
718 m->m_pkthdr.len = siz;
719 m->m_pkthdr.rcvif = (struct ifnet *)0;
720 /*
721 * For stream protocols, prepend a Sun RPC
722 * Record Mark.
723 */
724 if (sotype == SOCK_STREAM) {
725 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
726 *mtod(m, u_int32_t *) =
727 htonl(0x80000000 | siz);
728 }
729 nd->nd_mreq = m;
730 if (nfsrtton) {
731 nfsd_rt(slp->ns_so->so_type, nd,
732 cacherep);
733 }
734 error = nfsdsock_sendreply(slp, nd);
735 nd = NULL;
736 if (error == EPIPE)
737 nfsrv_zapsock(slp);
738 if (error == EINTR || error == ERESTART) {
739 nfsd->nfsd_slp = NULL;
740 nfsrv_slpderef(slp);
741 goto done;
742 }
743 break;
744 case RC_DROPIT:
745 if (nfsrtton)
746 nfsd_rt(sotype, nd, cacherep);
747 m_freem(nd->nd_mrep);
748 m_freem(nd->nd_nam2);
749 break;
750 }
751 if (nd) {
752 nfsdreq_free(nd);
753 nd = NULL;
754 }
755
756 /*
757 * Check to see if there are outstanding writes that
758 * need to be serviced.
759 */
760 getmicrotime(&tv);
761 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
762 (u_quad_t)tv.tv_usec;
763 mutex_enter(&nfsd_lock);
764 if (LIST_FIRST(&slp->ns_tq) &&
765 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
766 cacherep = RC_DOIT;
767 writes_todo = 1;
768 } else
769 writes_todo = 0;
770 mutex_exit(&nfsd_lock);
771 } while (writes_todo);
772 if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) {
773 nfsd->nfsd_slp = NULL;
774 nfsrv_slpderef(slp);
775 }
776 }
777 done:
778 mutex_enter(&nfsd_lock);
779 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
780 doreinit = --nfs_numnfsd == 0;
781 if (doreinit)
782 nfssvc_sockhead_flag |= SLP_INIT;
783 mutex_exit(&nfsd_lock);
784 cv_destroy(&nfsd->nfsd_cv);
785 kmem_free(nfsd, sizeof(*nfsd));
786 nsd->nsd_nfsd = NULL;
787 if (doreinit)
788 nfsrv_init(true); /* Reinitialize everything */
789 uvm_lwp_rele(l);
790 return (error);
791 }
792
793 /*
794 * Shut down a socket associated with an nfssvc_sock structure.
795 * Should be called with the send lock set, if required.
796 * The trick here is to increment the sref at the start, so that the nfsds
797 * will stop using it and clear ns_flag at the end so that it will not be
798 * reassigned during cleanup.
799 *
800 * called at splsoftnet.
801 */
802 void
803 nfsrv_zapsock(slp)
804 struct nfssvc_sock *slp;
805 {
806 struct nfsuid *nuidp, *nnuidp;
807 struct nfsrv_descript *nwp;
808 struct socket *so;
809 struct mbuf *m;
810
811 if (nfsdsock_drain(slp)) {
812 return;
813 }
814 mutex_enter(&nfsd_lock);
815 if (slp->ns_gflags & SLP_G_DOREC) {
816 TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
817 slp->ns_gflags &= ~SLP_G_DOREC;
818 }
819 mutex_exit(&nfsd_lock);
820
821 so = slp->ns_so;
822 KASSERT(so != NULL);
823 solock(so);
824 so->so_upcall = NULL;
825 so->so_upcallarg = NULL;
826 so->so_rcv.sb_flags &= ~SB_UPCALL;
827 soshutdown(so, SHUT_RDWR);
828 sounlock(so);
829
830 m_freem(slp->ns_raw);
831 m = slp->ns_rec;
832 while (m != NULL) {
833 struct mbuf *n;
834
835 n = m->m_nextpkt;
836 m_freem(m);
837 m = n;
838 }
839 /* XXX what about freeing ns_frag ? */
840 for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
841 nuidp = nnuidp) {
842 nnuidp = TAILQ_NEXT(nuidp, nu_lru);
843 LIST_REMOVE(nuidp, nu_hash);
844 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
845 if (nuidp->nu_flag & NU_NAM)
846 m_freem(nuidp->nu_nam);
847 kmem_free(nuidp, sizeof(*nuidp));
848 }
849 mutex_enter(&nfsd_lock);
850 while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) {
851 LIST_REMOVE(nwp, nd_tq);
852 mutex_exit(&nfsd_lock);
853 nfsdreq_free(nwp);
854 mutex_enter(&nfsd_lock);
855 }
856 mutex_exit(&nfsd_lock);
857 }
858
859 /*
860 * Derefence a server socket structure. If it has no more references and
861 * is no longer valid, you can throw it away.
862 */
863 void
864 nfsrv_slpderef(slp)
865 struct nfssvc_sock *slp;
866 {
867 uint32_t ref;
868
869 mutex_enter(&nfsd_lock);
870 KASSERT(slp->ns_sref > 0);
871 ref = --slp->ns_sref;
872 if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) {
873 file_t *fp;
874
875 KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0);
876 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
877 mutex_exit(&nfsd_lock);
878
879 fp = slp->ns_fp;
880 if (fp != NULL) {
881 slp->ns_fp = NULL;
882 KASSERT(fp != NULL);
883 KASSERT(fp->f_data == slp->ns_so);
884 KASSERT(fp->f_count > 0);
885 closef(fp);
886 slp->ns_so = NULL;
887 }
888
889 if (slp->ns_nam)
890 m_free(slp->ns_nam);
891 nfsrv_sockfree(slp);
892 } else
893 mutex_exit(&nfsd_lock);
894 }
895
896 /*
897 * Initialize the data structures for the server.
898 * Handshake with any new nfsds starting up to avoid any chance of
899 * corruption.
900 */
901 void
902 nfsrv_init(terminating)
903 int terminating;
904 {
905 struct nfssvc_sock *slp;
906
907 if (!terminating) {
908 mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
909 cv_init(&nfsd_initcv, "nfsdinit");
910 }
911
912 mutex_enter(&nfsd_lock);
913 if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0)
914 panic("nfsd init");
915 nfssvc_sockhead_flag |= SLP_INIT;
916
917 if (terminating) {
918 KASSERT(SLIST_EMPTY(&nfsd_idle_head));
919 KASSERT(TAILQ_EMPTY(&nfsd_head));
920 while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
921 mutex_exit(&nfsd_lock);
922 KASSERT(slp->ns_sref == 0);
923 slp->ns_sref++;
924 nfsrv_zapsock(slp);
925 nfsrv_slpderef(slp);
926 mutex_enter(&nfsd_lock);
927 }
928 KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
929 mutex_exit(&nfsd_lock);
930 nfsrv_cleancache(); /* And clear out server cache */
931 } else {
932 mutex_exit(&nfsd_lock);
933 nfs_pub.np_valid = 0;
934 }
935
936 TAILQ_INIT(&nfssvc_sockhead);
937 TAILQ_INIT(&nfssvc_sockpending);
938
939 TAILQ_INIT(&nfsd_head);
940 SLIST_INIT(&nfsd_idle_head);
941 nfsd_head_flag &= ~NFSD_CHECKSLP;
942
943 nfs_udpsock = nfsrv_sockalloc();
944
945 #ifdef INET6
946 nfs_udp6sock = nfsrv_sockalloc();
947 #endif
948
949 #ifdef ISO
950 nfs_cltpsock = nfsrv_sockalloc();
951 #endif
952
953 mutex_enter(&nfsd_lock);
954 nfssvc_sockhead_flag &= ~SLP_INIT;
955 cv_broadcast(&nfsd_initcv);
956 mutex_exit(&nfsd_lock);
957 }
958
959 /*
960 * Add entries to the server monitor log.
961 */
962 static void
963 nfsd_rt(sotype, nd, cacherep)
964 int sotype;
965 struct nfsrv_descript *nd;
966 int cacherep;
967 {
968 struct timeval tv;
969 struct drt *rt;
970
971 rt = &nfsdrt.drt[nfsdrt.pos];
972 if (cacherep == RC_DOIT)
973 rt->flag = 0;
974 else if (cacherep == RC_REPLY)
975 rt->flag = DRT_CACHEREPLY;
976 else
977 rt->flag = DRT_CACHEDROP;
978 if (sotype == SOCK_STREAM)
979 rt->flag |= DRT_TCP;
980 if (nd->nd_flag & ND_NFSV3)
981 rt->flag |= DRT_NFSV3;
982 rt->proc = nd->nd_procnum;
983 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
984 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
985 else
986 rt->ipadr = INADDR_ANY;
987 getmicrotime(&tv);
988 rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
989 (tv.tv_usec - nd->nd_starttime.tv_usec);
990 rt->tstamp = tv;
991 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
992 }
993 #endif /* NFSSERVER */
994
995 #ifdef NFS
996
997 int nfs_defect = 0;
998 /*
999 * Asynchronous I/O threads for client nfs.
1000 * They do read-ahead and write-behind operations on the block I/O cache.
1001 * Never returns unless it fails or gets killed.
1002 */
1003
1004 static void
1005 nfssvc_iod(void *arg)
1006 {
1007 struct buf *bp;
1008 struct nfs_iod *myiod;
1009 struct nfsmount *nmp;
1010
1011 myiod = kmem_alloc(sizeof(*myiod), KM_SLEEP);
1012 mutex_init(&myiod->nid_lock, MUTEX_DEFAULT, IPL_NONE);
1013 cv_init(&myiod->nid_cv, "nfsiod");
1014 myiod->nid_exiting = false;
1015 myiod->nid_mount = NULL;
1016 mutex_enter(&nfs_iodlist_lock);
1017 LIST_INSERT_HEAD(&nfs_iodlist_all, myiod, nid_all);
1018 mutex_exit(&nfs_iodlist_lock);
1019
1020 for (;;) {
1021 mutex_enter(&nfs_iodlist_lock);
1022 LIST_INSERT_HEAD(&nfs_iodlist_idle, myiod, nid_idle);
1023 mutex_exit(&nfs_iodlist_lock);
1024
1025 mutex_enter(&myiod->nid_lock);
1026 while (/*CONSTCOND*/ true) {
1027 nmp = myiod->nid_mount;
1028 if (nmp) {
1029 myiod->nid_mount = NULL;
1030 break;
1031 }
1032 if (__predict_false(myiod->nid_exiting)) {
1033 /*
1034 * drop nid_lock to preserve locking order.
1035 */
1036 mutex_exit(&myiod->nid_lock);
1037 mutex_enter(&nfs_iodlist_lock);
1038 mutex_enter(&myiod->nid_lock);
1039 /*
1040 * recheck nid_mount because nfs_asyncio can
1041 * pick us in the meantime as we are still on
1042 * nfs_iodlist_lock.
1043 */
1044 if (myiod->nid_mount != NULL) {
1045 mutex_exit(&nfs_iodlist_lock);
1046 continue;
1047 }
1048 LIST_REMOVE(myiod, nid_idle);
1049 mutex_exit(&nfs_iodlist_lock);
1050 goto quit;
1051 }
1052 cv_wait(&myiod->nid_cv, &myiod->nid_lock);
1053 }
1054 mutex_exit(&myiod->nid_lock);
1055
1056 mutex_enter(&nmp->nm_lock);
1057 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
1058 /* Take one off the front of the list */
1059 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
1060 nmp->nm_bufqlen--;
1061 if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
1062 cv_broadcast(&nmp->nm_aiocv);
1063 }
1064 mutex_exit(&nmp->nm_lock);
1065 KERNEL_LOCK(1, curlwp);
1066 (void)nfs_doio(bp);
1067 KERNEL_UNLOCK_LAST(curlwp);
1068 mutex_enter(&nmp->nm_lock);
1069 /*
1070 * If there are more than one iod on this mount,
1071 * then defect so that the iods can be shared out
1072 * fairly between the mounts
1073 */
1074 if (nfs_defect && nmp->nm_bufqiods > 1) {
1075 break;
1076 }
1077 }
1078 KASSERT(nmp->nm_bufqiods > 0);
1079 nmp->nm_bufqiods--;
1080 mutex_exit(&nmp->nm_lock);
1081 }
1082 quit:
1083 KASSERT(myiod->nid_mount == NULL);
1084 mutex_exit(&myiod->nid_lock);
1085
1086 cv_destroy(&myiod->nid_cv);
1087 mutex_destroy(&myiod->nid_lock);
1088 kmem_free(myiod, sizeof(*myiod));
1089
1090 kthread_exit(0);
1091 }
1092
1093 void
1094 nfs_iodinit()
1095 {
1096
1097 mutex_init(&nfs_iodlist_lock, MUTEX_DEFAULT, IPL_NONE);
1098 LIST_INIT(&nfs_iodlist_all);
1099 LIST_INIT(&nfs_iodlist_idle);
1100 }
1101
1102 int
1103 nfs_set_niothreads(int newval)
1104 {
1105 struct nfs_iod *nid;
1106 int error = 0;
1107 int hold_count;
1108
1109 KERNEL_UNLOCK_ALL(curlwp, &hold_count);
1110
1111 mutex_enter(&nfs_iodlist_lock);
1112 /* clamp to sane range */
1113 nfs_niothreads = max(0, min(newval, NFS_MAXASYNCDAEMON));
1114
1115 while (nfs_numasync != nfs_niothreads && error == 0) {
1116 while (nfs_numasync < nfs_niothreads) {
1117
1118 /*
1119 * kthread_create can wait for pagedaemon and
1120 * pagedaemon can wait for nfsiod which needs to acquire
1121 * nfs_iodlist_lock.
1122 */
1123
1124 mutex_exit(&nfs_iodlist_lock);
1125 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
1126 nfssvc_iod, NULL, NULL, "nfsio");
1127 mutex_enter(&nfs_iodlist_lock);
1128 if (error) {
1129 /* give up */
1130 nfs_niothreads = nfs_numasync;
1131 break;
1132 }
1133 nfs_numasync++;
1134 }
1135 while (nfs_numasync > nfs_niothreads) {
1136 nid = LIST_FIRST(&nfs_iodlist_all);
1137 if (nid == NULL) {
1138 /* iod has not started yet. */
1139 kpause("nfsiorm", false, hz, &nfs_iodlist_lock);
1140 continue;
1141 }
1142 LIST_REMOVE(nid, nid_all);
1143 mutex_enter(&nid->nid_lock);
1144 KASSERT(!nid->nid_exiting);
1145 nid->nid_exiting = true;
1146 cv_signal(&nid->nid_cv);
1147 mutex_exit(&nid->nid_lock);
1148 nfs_numasync--;
1149 }
1150 }
1151 mutex_exit(&nfs_iodlist_lock);
1152
1153 KERNEL_LOCK(hold_count, curlwp);
1154 return error;
1155 }
1156
1157 /*
1158 * Get an authorization string for the uid by having the mount_nfs sitting
1159 * on this mount point porpous out of the kernel and do it.
1160 */
1161 int
1162 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
1163 struct nfsmount *nmp;
1164 struct nfsreq *rep;
1165 kauth_cred_t cred;
1166 char **auth_str;
1167 int *auth_len;
1168 char *verf_str;
1169 int *verf_len;
1170 NFSKERBKEY_T key; /* return session key */
1171 {
1172 int error = 0;
1173
1174 while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
1175 nmp->nm_iflag |= NFSMNT_WANTAUTH;
1176 (void) tsleep((void *)&nmp->nm_authtype, PSOCK,
1177 "nfsauth1", 2 * hz);
1178 error = nfs_sigintr(nmp, rep, rep->r_lwp);
1179 if (error) {
1180 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1181 return (error);
1182 }
1183 }
1184 nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
1185 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1186 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1187 nmp->nm_verfstr = verf_str;
1188 nmp->nm_verflen = *verf_len;
1189 nmp->nm_authuid = kauth_cred_geteuid(cred);
1190 wakeup((void *)&nmp->nm_authstr);
1191
1192 /*
1193 * And wait for mount_nfs to do its stuff.
1194 */
1195 while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
1196 (void) tsleep((void *)&nmp->nm_authlen, PSOCK,
1197 "nfsauth2", 2 * hz);
1198 error = nfs_sigintr(nmp, rep, rep->r_lwp);
1199 }
1200 if (nmp->nm_iflag & NFSMNT_AUTHERR) {
1201 nmp->nm_iflag &= ~NFSMNT_AUTHERR;
1202 error = EAUTH;
1203 }
1204 if (error)
1205 free((void *)*auth_str, M_TEMP);
1206 else {
1207 *auth_len = nmp->nm_authlen;
1208 *verf_len = nmp->nm_verflen;
1209 memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
1210 }
1211 nmp->nm_iflag &= ~NFSMNT_HASAUTH;
1212 nmp->nm_iflag |= NFSMNT_WAITAUTH;
1213 if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
1214 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1215 wakeup((void *)&nmp->nm_authtype);
1216 }
1217 return (error);
1218 }
1219
1220 /*
1221 * Get a nickname authenticator and verifier.
1222 */
1223 int
1224 nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
1225 int *auth_len, char *verf_str, int verf_len)
1226 {
1227 struct timeval ktvin, ktvout, tv;
1228 struct nfsuid *nuidp;
1229 u_int32_t *nickp, *verfp;
1230
1231 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1232
1233 #ifdef DIAGNOSTIC
1234 if (verf_len < (4 * NFSX_UNSIGNED))
1235 panic("nfs_getnickauth verf too small");
1236 #endif
1237 LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
1238 if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
1239 break;
1240 }
1241 if (!nuidp || nuidp->nu_expire < time_second)
1242 return (EACCES);
1243
1244 /*
1245 * Move to the end of the lru list (end of lru == most recently used).
1246 */
1247 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1248 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1249
1250 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1251 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1252 *nickp = txdr_unsigned(nuidp->nu_nickname);
1253 *auth_str = (char *)nickp;
1254 *auth_len = 2 * NFSX_UNSIGNED;
1255
1256 /*
1257 * Now we must encrypt the verifier and package it up.
1258 */
1259 verfp = (u_int32_t *)verf_str;
1260 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1261 getmicrotime(&tv);
1262 if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
1263 (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
1264 tv.tv_usec > nuidp->nu_timestamp.tv_usec))
1265 nuidp->nu_timestamp = tv;
1266 else
1267 nuidp->nu_timestamp.tv_usec++;
1268 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1269 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1270
1271 /*
1272 * Now encrypt the timestamp verifier in ecb mode using the session
1273 * key.
1274 */
1275 #ifdef NFSKERB
1276 XXX
1277 #endif
1278
1279 *verfp++ = ktvout.tv_sec;
1280 *verfp++ = ktvout.tv_usec;
1281 *verfp = 0;
1282 return (0);
1283 }
1284
1285 /*
1286 * Save the current nickname in a hash list entry on the mount point.
1287 */
1288 int
1289 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1290 struct nfsmount *nmp;
1291 kauth_cred_t cred;
1292 int len;
1293 NFSKERBKEY_T key;
1294 struct mbuf **mdp;
1295 char **dposp;
1296 struct mbuf *mrep;
1297 {
1298 struct nfsuid *nuidp;
1299 u_int32_t *tl;
1300 int32_t t1;
1301 struct mbuf *md = *mdp;
1302 struct timeval ktvin, ktvout;
1303 u_int32_t nick;
1304 char *dpos = *dposp, *cp2;
1305 int deltasec, error = 0;
1306
1307 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1308
1309 if (len == (3 * NFSX_UNSIGNED)) {
1310 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1311 ktvin.tv_sec = *tl++;
1312 ktvin.tv_usec = *tl++;
1313 nick = fxdr_unsigned(u_int32_t, *tl);
1314
1315 /*
1316 * Decrypt the timestamp in ecb mode.
1317 */
1318 #ifdef NFSKERB
1319 XXX
1320 #endif
1321 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1322 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1323 deltasec = time_second - ktvout.tv_sec;
1324 if (deltasec < 0)
1325 deltasec = -deltasec;
1326 /*
1327 * If ok, add it to the hash list for the mount point.
1328 */
1329 if (deltasec <= NFS_KERBCLOCKSKEW) {
1330 if (nmp->nm_numuids < nuidhash_max) {
1331 nmp->nm_numuids++;
1332 nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
1333 } else {
1334 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1335 LIST_REMOVE(nuidp, nu_hash);
1336 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1337 nu_lru);
1338 }
1339 nuidp->nu_flag = 0;
1340 kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
1341 nuidp->nu_expire = time_second + NFS_KERBTTL;
1342 nuidp->nu_timestamp = ktvout;
1343 nuidp->nu_nickname = nick;
1344 memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
1345 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1346 nu_lru);
1347 LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
1348 nuidp, nu_hash);
1349 }
1350 } else
1351 nfsm_adv(nfsm_rndup(len));
1352 nfsmout:
1353 *mdp = md;
1354 *dposp = dpos;
1355 return (error);
1356 }
1357 #endif /* NFS */
Cache object: a9ade23b0b268ab7e224d73a09e72986
|