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