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