1 /* $NetBSD: nfs_syscalls.c,v 1.19 1996/02/18 11:53:52 fvdl 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/file.h> 45 #include <sys/stat.h> 46 #include <sys/vnode.h> 47 #include <sys/mount.h> 48 #include <sys/proc.h> 49 #include <sys/uio.h> 50 #include <sys/malloc.h> 51 #include <sys/buf.h> 52 #include <sys/mbuf.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 #include <sys/domain.h> 56 #include <sys/protosw.h> 57 #include <sys/namei.h> 58 #include <sys/syslog.h> 59 #include <sys/filedesc.h> 60 61 #include <sys/syscallargs.h> 62 63 #include <netinet/in.h> 64 #include <netinet/tcp.h> 65 #ifdef ISO 66 #include <netiso/iso.h> 67 #endif 68 #include <nfs/xdr_subs.h> 69 #include <nfs/rpcv2.h> 70 #include <nfs/nfsproto.h> 71 #include <nfs/nfs.h> 72 #include <nfs/nfsm_subs.h> 73 #include <nfs/nfsrvcache.h> 74 #include <nfs/nfsmount.h> 75 #include <nfs/nfsnode.h> 76 #include <nfs/nqnfs.h> 77 #include <nfs/nfsrtt.h> 78 #include <nfs/nfs_var.h> 79 80 void nfsrv_zapsock __P((struct nfssvc_sock *)); 81 82 /* Global defs. */ 83 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *, 84 struct nfssvc_sock *, 85 struct proc *, struct mbuf **)); 86 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 87 extern int nfs_numasync; 88 extern time_t nqnfsstarttime; 89 extern int nqsrv_writeslack; 90 extern int nfsrtton; 91 extern struct nfsstats nfsstats; 92 extern int nfsrvw_procrastinate; 93 struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock; 94 int nuidhash_max = NFS_MAXUIDHASH; 95 int nfsd_waiting = 0; 96 #ifdef NFSSERVER 97 static int nfs_numnfsd = 0; 98 static int notstarted = 1; 99 static int modify_flag = 0; 100 static struct nfsdrt nfsdrt; 101 #endif 102 103 #define TRUE 1 104 #define FALSE 0 105 106 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; 107 108 #ifdef NFSSERVER 109 static void nfsd_rt __P((int, struct nfsrv_descript *, int)); 110 #endif 111 112 /* 113 * NFS server system calls 114 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c 115 */ 116 117 /* 118 * Get file handle system call 119 */ 120 int 121 sys_getfh(p, v, retval) 122 struct proc *p; 123 register void *v; 124 register_t *retval; 125 { 126 register struct sys_getfh_args /* { 127 syscallarg(char *) fname; 128 syscallarg(fhandle_t *) fhp; 129 } */ *uap = v; 130 register struct vnode *vp; 131 fhandle_t fh; 132 int error; 133 struct nameidata nd; 134 135 /* 136 * Must be super user 137 */ 138 error = suser(p->p_ucred, &p->p_acflag); 139 if(error) 140 return (error); 141 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 142 SCARG(uap, fname), p); 143 error = namei(&nd); 144 if (error) 145 return (error); 146 vp = nd.ni_vp; 147 bzero((caddr_t)&fh, sizeof(fh)); 148 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid; 149 error = VFS_VPTOFH(vp, &fh.fh_fid); 150 vput(vp); 151 if (error) 152 return (error); 153 error = copyout((caddr_t)&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh)); 154 return (error); 155 } 156 157 /* 158 * Nfs server pseudo system call for the nfsd's 159 * Based on the flag value it either: 160 * - adds a socket to the selection list 161 * - remains in the kernel as an nfsd 162 * - remains in the kernel as an nfsiod 163 */ 164 int 165 sys_nfssvc(p, v, retval) 166 struct proc *p; 167 void *v; 168 register_t *retval; 169 { 170 register struct sys_nfssvc_args /* { 171 syscallarg(int) flag; 172 syscallarg(caddr_t) argp; 173 } */ *uap = v; 174 struct nameidata nd; 175 struct nfsmount *nmp; 176 int error; 177 #ifdef NFSCLIENT 178 struct nfsd_cargs ncd; 179 #endif 180 #ifdef NFSSERVER 181 struct file *fp; 182 struct mbuf *nam; 183 struct nfsd_args nfsdarg; 184 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs; 185 struct nfsd *nfsd; 186 struct nfssvc_sock *slp; 187 struct nfsuid *nuidp; 188 #endif 189 190 /* 191 * Must be super user 192 */ 193 error = suser(p->p_ucred, &p->p_acflag); 194 if(error) 195 return (error); 196 while (nfssvc_sockhead_flag & SLP_INIT) { 197 nfssvc_sockhead_flag |= SLP_WANTINIT; 198 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0); 199 } 200 if (SCARG(uap, flag) & NFSSVC_BIOD) { 201 #ifdef NFSCLIENT 202 error = nfssvc_iod(p); 203 #else 204 error = ENOSYS; 205 #endif 206 } else if (SCARG(uap, flag) & NFSSVC_MNTD) { 207 #ifndef NFSCLIENT 208 error = ENOSYS; 209 #else 210 error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd)); 211 if (error) 212 return (error); 213 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 214 ncd.ncd_dirp, p); 215 error = namei(&nd); 216 if (error) 217 return (error); 218 if ((nd.ni_vp->v_flag & VROOT) == 0) 219 error = EINVAL; 220 nmp = VFSTONFS(nd.ni_vp->v_mount); 221 vput(nd.ni_vp); 222 if (error) 223 return (error); 224 if ((nmp->nm_flag & NFSMNT_MNTD) && 225 (SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0) 226 return (0); 227 nmp->nm_flag |= NFSMNT_MNTD; 228 error = nqnfs_clientd(nmp, p->p_ucred, &ncd, SCARG(uap, flag), 229 SCARG(uap, argp), p); 230 #endif /* NFSCLIENT */ 231 } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) { 232 #ifndef NFSSERVER 233 error = ENOSYS; 234 #else 235 error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg, 236 sizeof(nfsdarg)); 237 if (error) 238 return (error); 239 error = getsock(p->p_fd, nfsdarg.sock, &fp); 240 if (error) 241 return (error); 242 /* 243 * Get the client address for connected sockets. 244 */ 245 if (nfsdarg.name == NULL || nfsdarg.namelen == 0) 246 nam = (struct mbuf *)0; 247 else { 248 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen, 249 MT_SONAME); 250 if (error) 251 return (error); 252 } 253 error = nfssvc_addsock(fp, nam); 254 #endif /* !NFSSERVER */ 255 } else { 256 #ifndef NFSSERVER 257 error = ENOSYS; 258 #else 259 error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd)); 260 if (error) 261 return (error); 262 if ((SCARG(uap, flag) & NFSSVC_AUTHIN) && 263 ((nfsd = nsd->nsd_nfsd)) != NULL && 264 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) { 265 slp = nfsd->nfsd_slp; 266 267 /* 268 * First check to see if another nfsd has already 269 * added this credential. 270 */ 271 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first; 272 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 273 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid && 274 (!nfsd->nfsd_nd->nd_nam2 || 275 netaddr_match(NU_NETFAM(nuidp), 276 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2))) 277 break; 278 } 279 if (nuidp) { 280 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr); 281 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL; 282 } else { 283 /* 284 * Nope, so we will. 285 */ 286 if (slp->ns_numuids < nuidhash_max) { 287 slp->ns_numuids++; 288 nuidp = (struct nfsuid *) 289 malloc(sizeof (struct nfsuid), M_NFSUID, 290 M_WAITOK); 291 } else 292 nuidp = (struct nfsuid *)0; 293 if ((slp->ns_flag & SLP_VALID) == 0) { 294 if (nuidp) 295 free((caddr_t)nuidp, M_NFSUID); 296 } else { 297 if (nuidp == (struct nfsuid *)0) { 298 nuidp = slp->ns_uidlruhead.tqh_first; 299 LIST_REMOVE(nuidp, nu_hash); 300 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, 301 nu_lru); 302 if (nuidp->nu_flag & NU_NAM) 303 m_freem(nuidp->nu_nam); 304 } 305 nuidp->nu_flag = 0; 306 nuidp->nu_cr = nsd->nsd_cr; 307 if (nuidp->nu_cr.cr_ngroups > NGROUPS) 308 nuidp->nu_cr.cr_ngroups = NGROUPS; 309 nuidp->nu_cr.cr_ref = 1; 310 nuidp->nu_timestamp = nsd->nsd_timestamp; 311 nuidp->nu_expire = time.tv_sec + nsd->nsd_ttl; 312 /* 313 * and save the session key in nu_key. 314 */ 315 bcopy(nsd->nsd_key, nuidp->nu_key, 316 sizeof (nsd->nsd_key)); 317 if (nfsd->nfsd_nd->nd_nam2) { 318 struct sockaddr_in *saddr; 319 320 saddr = mtod(nfsd->nfsd_nd->nd_nam2, 321 struct sockaddr_in *); 322 switch (saddr->sin_family) { 323 case AF_INET: 324 nuidp->nu_flag |= NU_INETADDR; 325 nuidp->nu_inetaddr = 326 saddr->sin_addr.s_addr; 327 break; 328 case AF_ISO: 329 default: 330 nuidp->nu_flag |= NU_NAM; 331 nuidp->nu_nam = m_copym( 332 nfsd->nfsd_nd->nd_nam2, 0, 333 M_COPYALL, M_WAIT); 334 break; 335 }; 336 } 337 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp, 338 nu_lru); 339 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid), 340 nuidp, nu_hash); 341 nfsrv_setcred(&nuidp->nu_cr, 342 &nfsd->nfsd_nd->nd_cr); 343 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL; 344 } 345 } 346 } 347 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) && 348 (nfsd = nsd->nsd_nfsd)) 349 nfsd->nfsd_flag |= NFSD_AUTHFAIL; 350 error = nfssvc_nfsd(nsd, SCARG(uap, argp), p); 351 #endif /* !NFSSERVER */ 352 } 353 if (error == EINTR || error == ERESTART) 354 error = 0; 355 return (error); 356 } 357 358 #ifdef NFSSERVER 359 /* 360 * Adds a socket to the list for servicing by nfsds. 361 */ 362 int 363 nfssvc_addsock(fp, mynam) 364 struct file *fp; 365 struct mbuf *mynam; 366 { 367 register struct mbuf *m; 368 register int siz; 369 register struct nfssvc_sock *slp; 370 register struct socket *so; 371 struct nfssvc_sock *tslp; 372 int error, s; 373 374 so = (struct socket *)fp->f_data; 375 tslp = (struct nfssvc_sock *)0; 376 /* 377 * Add it to the list, as required. 378 */ 379 if (so->so_proto->pr_protocol == IPPROTO_UDP) { 380 tslp = nfs_udpsock; 381 if (tslp->ns_flag & SLP_VALID) { 382 m_freem(mynam); 383 return (EPERM); 384 } 385 #ifdef ISO 386 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) { 387 tslp = nfs_cltpsock; 388 if (tslp->ns_flag & SLP_VALID) { 389 m_freem(mynam); 390 return (EPERM); 391 } 392 #endif /* ISO */ 393 } 394 if (so->so_type == SOCK_STREAM) 395 siz = NFS_MAXPACKET + sizeof (u_long); 396 else 397 siz = NFS_MAXPACKET; 398 error = soreserve(so, siz, siz); 399 if (error) { 400 m_freem(mynam); 401 return (error); 402 } 403 404 /* 405 * Set protocol specific options { for now TCP only } and 406 * reserve some space. For datagram sockets, this can get called 407 * repeatedly for the same socket, but that isn't harmful. 408 */ 409 if (so->so_type == SOCK_STREAM) { 410 MGET(m, M_WAIT, MT_SOOPTS); 411 *mtod(m, int32_t *) = 1; 412 m->m_len = sizeof(int32_t); 413 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m); 414 } 415 if (so->so_proto->pr_domain->dom_family == AF_INET && 416 so->so_proto->pr_protocol == IPPROTO_TCP) { 417 MGET(m, M_WAIT, MT_SOOPTS); 418 *mtod(m, int32_t *) = 1; 419 m->m_len = sizeof(int32_t); 420 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m); 421 } 422 so->so_rcv.sb_flags &= ~SB_NOINTR; 423 so->so_rcv.sb_timeo = 0; 424 so->so_snd.sb_flags &= ~SB_NOINTR; 425 so->so_snd.sb_timeo = 0; 426 if (tslp) 427 slp = tslp; 428 else { 429 slp = (struct nfssvc_sock *) 430 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 431 bzero((caddr_t)slp, sizeof (struct nfssvc_sock)); 432 TAILQ_INIT(&slp->ns_uidlruhead); 433 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain); 434 } 435 slp->ns_so = so; 436 slp->ns_nam = mynam; 437 fp->f_count++; 438 slp->ns_fp = fp; 439 s = splsoftnet(); 440 so->so_upcallarg = (caddr_t)slp; 441 so->so_upcall = nfsrv_rcv; 442 slp->ns_flag = (SLP_VALID | SLP_NEEDQ); 443 nfsrv_wakenfsd(slp); 444 splx(s); 445 return (0); 446 } 447 448 /* 449 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 450 * until it is killed by a signal. 451 */ 452 int 453 nfssvc_nfsd(nsd, argp, p) 454 struct nfsd_srvargs *nsd; 455 caddr_t argp; 456 struct proc *p; 457 { 458 register struct mbuf *m; 459 register int siz; 460 register struct nfssvc_sock *slp; 461 register struct socket *so; 462 register int *solockp; 463 struct nfsd *nfsd = nsd->nsd_nfsd; 464 struct nfsrv_descript *nd = NULL; 465 struct mbuf *mreq; 466 int error = 0, cacherep, s, sotype, writes_todo; 467 u_quad_t cur_usec; 468 469 #ifndef nolint 470 cacherep = RC_DOIT; 471 writes_todo = 0; 472 #endif 473 s = splsoftnet(); 474 if (nfsd == (struct nfsd *)0) { 475 nsd->nsd_nfsd = nfsd = (struct nfsd *) 476 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK); 477 bzero((caddr_t)nfsd, sizeof (struct nfsd)); 478 nfsd->nfsd_procp = p; 479 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain); 480 nfs_numnfsd++; 481 } 482 /* 483 * Loop getting rpc requests until SIGKILL. 484 */ 485 for (;;) { 486 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) { 487 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 488 (nfsd_head_flag & NFSD_CHECKSLP) == 0) { 489 nfsd->nfsd_flag |= NFSD_WAITING; 490 nfsd_waiting++; 491 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH, 492 "nfsd", 0); 493 nfsd_waiting--; 494 if (error) 495 goto done; 496 } 497 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 498 (nfsd_head_flag & NFSD_CHECKSLP) != 0) { 499 for (slp = nfssvc_sockhead.tqh_first; slp != 0; 500 slp = slp->ns_chain.tqe_next) { 501 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC)) 502 == (SLP_VALID | SLP_DOREC)) { 503 slp->ns_flag &= ~SLP_DOREC; 504 slp->ns_sref++; 505 nfsd->nfsd_slp = slp; 506 break; 507 } 508 } 509 if (slp == 0) 510 nfsd_head_flag &= ~NFSD_CHECKSLP; 511 } 512 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0) 513 continue; 514 if (slp->ns_flag & SLP_VALID) { 515 if (slp->ns_flag & SLP_DISCONN) 516 nfsrv_zapsock(slp); 517 else if (slp->ns_flag & SLP_NEEDQ) { 518 slp->ns_flag &= ~SLP_NEEDQ; 519 (void) nfs_sndlock(&slp->ns_solock, 520 (struct nfsreq *)0); 521 nfsrv_rcv(slp->ns_so, (caddr_t)slp, 522 M_WAIT); 523 nfs_sndunlock(&slp->ns_solock); 524 } 525 error = nfsrv_dorec(slp, nfsd, &nd); 526 cur_usec = (u_quad_t)time.tv_sec * 1000000 + 527 (u_quad_t)time.tv_usec; 528 if (error && slp->ns_tq.lh_first && 529 slp->ns_tq.lh_first->nd_time <= cur_usec) { 530 error = 0; 531 cacherep = RC_DOIT; 532 writes_todo = 1; 533 } else 534 writes_todo = 0; 535 nfsd->nfsd_flag |= NFSD_REQINPROG; 536 } 537 } else { 538 error = 0; 539 slp = nfsd->nfsd_slp; 540 } 541 if (error || (slp->ns_flag & SLP_VALID) == 0) { 542 if (nd) { 543 free((caddr_t)nd, M_NFSRVDESC); 544 nd = NULL; 545 } 546 nfsd->nfsd_slp = (struct nfssvc_sock *)0; 547 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 548 nfsrv_slpderef(slp); 549 continue; 550 } 551 splx(s); 552 so = slp->ns_so; 553 sotype = so->so_type; 554 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 555 solockp = &slp->ns_solock; 556 else 557 solockp = (int *)0; 558 if (nd) { 559 nd->nd_starttime = time; 560 if (nd->nd_nam2) 561 nd->nd_nam = nd->nd_nam2; 562 else 563 nd->nd_nam = slp->ns_nam; 564 565 /* 566 * Check to see if authorization is needed. 567 */ 568 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) { 569 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH; 570 nsd->nsd_haddr = mtod(nd->nd_nam, 571 struct sockaddr_in *)->sin_addr.s_addr; 572 nsd->nsd_authlen = nfsd->nfsd_authlen; 573 nsd->nsd_verflen = nfsd->nfsd_verflen; 574 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr, 575 nfsd->nfsd_authlen) && 576 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr, 577 nfsd->nfsd_verflen) && 578 !copyout((caddr_t)nsd, argp, sizeof (*nsd))) 579 return (ENEEDAUTH); 580 cacherep = RC_DROPIT; 581 } else 582 cacherep = nfsrv_getcache(nd, slp, &mreq); 583 584 /* 585 * Check for just starting up for NQNFS and send 586 * fake "try again later" replies to the NQNFS clients. 587 */ 588 if (notstarted && nqnfsstarttime <= time.tv_sec) { 589 if (modify_flag) { 590 nqnfsstarttime = time.tv_sec + nqsrv_writeslack; 591 modify_flag = 0; 592 } else 593 notstarted = 0; 594 } 595 if (notstarted) { 596 if ((nd->nd_flag & ND_NQNFS) == 0) 597 cacherep = RC_DROPIT; 598 else if (nd->nd_procnum != NFSPROC_WRITE) { 599 nd->nd_procnum = NFSPROC_NOOP; 600 nd->nd_repstat = NQNFS_TRYLATER; 601 cacherep = RC_DOIT; 602 } else 603 modify_flag = 1; 604 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) { 605 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL; 606 nd->nd_procnum = NFSPROC_NOOP; 607 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 608 cacherep = RC_DOIT; 609 } 610 } 611 612 /* 613 * Loop to get all the write rpc relies that have been 614 * gathered together. 615 */ 616 do { 617 switch (cacherep) { 618 case RC_DOIT: 619 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE && 620 nfsrvw_procrastinate > 0 && !notstarted)) 621 error = nfsrv_writegather(&nd, slp, 622 nfsd->nfsd_procp, &mreq); 623 else 624 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd, 625 slp, nfsd->nfsd_procp, &mreq); 626 if (mreq == NULL) 627 break; 628 if (error) { 629 if (nd->nd_procnum != NQNFSPROC_VACATED) 630 nfsstats.srv_errs++; 631 nfsrv_updatecache(nd, FALSE, mreq); 632 if (nd->nd_nam2) 633 m_freem(nd->nd_nam2); 634 break; 635 } 636 nfsstats.srvrpccnt[nd->nd_procnum]++; 637 nfsrv_updatecache(nd, TRUE, mreq); 638 nd->nd_mrep = (struct mbuf *)0; 639 case RC_REPLY: 640 m = mreq; 641 siz = 0; 642 while (m) { 643 siz += m->m_len; 644 m = m->m_next; 645 } 646 if (siz <= 0 || siz > NFS_MAXPACKET) { 647 printf("mbuf siz=%d\n",siz); 648 panic("Bad nfs svc reply"); 649 } 650 m = mreq; 651 m->m_pkthdr.len = siz; 652 m->m_pkthdr.rcvif = (struct ifnet *)0; 653 /* 654 * For stream protocols, prepend a Sun RPC 655 * Record Mark. 656 */ 657 if (sotype == SOCK_STREAM) { 658 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 659 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz); 660 } 661 if (solockp) 662 (void) nfs_sndlock(solockp, (struct nfsreq *)0); 663 if (slp->ns_flag & SLP_VALID) 664 error = nfs_send(so, nd->nd_nam2, m, NULL); 665 else { 666 error = EPIPE; 667 m_freem(m); 668 } 669 if (nfsrtton) 670 nfsd_rt(sotype, nd, cacherep); 671 if (nd->nd_nam2) 672 MFREE(nd->nd_nam2, m); 673 if (nd->nd_mrep) 674 m_freem(nd->nd_mrep); 675 if (error == EPIPE) 676 nfsrv_zapsock(slp); 677 if (solockp) 678 nfs_sndunlock(solockp); 679 if (error == EINTR || error == ERESTART) { 680 free((caddr_t)nd, M_NFSRVDESC); 681 nfsrv_slpderef(slp); 682 s = splsoftnet(); 683 goto done; 684 } 685 break; 686 case RC_DROPIT: 687 if (nfsrtton) 688 nfsd_rt(sotype, nd, cacherep); 689 m_freem(nd->nd_mrep); 690 m_freem(nd->nd_nam2); 691 break; 692 }; 693 if (nd) { 694 FREE((caddr_t)nd, M_NFSRVDESC); 695 nd = NULL; 696 } 697 698 /* 699 * Check to see if there are outstanding writes that 700 * need to be serviced. 701 */ 702 cur_usec = (u_quad_t)time.tv_sec * 1000000 + 703 (u_quad_t)time.tv_usec; 704 s = splsoftclock(); 705 if (slp->ns_tq.lh_first && 706 slp->ns_tq.lh_first->nd_time <= cur_usec) { 707 cacherep = RC_DOIT; 708 writes_todo = 1; 709 } else 710 writes_todo = 0; 711 splx(s); 712 } while (writes_todo); 713 s = splsoftnet(); 714 if (nfsrv_dorec(slp, nfsd, &nd)) { 715 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 716 nfsd->nfsd_slp = NULL; 717 nfsrv_slpderef(slp); 718 } 719 } 720 done: 721 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain); 722 splx(s); 723 free((caddr_t)nfsd, M_NFSD); 724 nsd->nsd_nfsd = (struct nfsd *)0; 725 if (--nfs_numnfsd == 0) 726 nfsrv_init(TRUE); /* Reinitialize everything */ 727 return (error); 728 } 729 730 /* 731 * Shut down a socket associated with an nfssvc_sock structure. 732 * Should be called with the send lock set, if required. 733 * The trick here is to increment the sref at the start, so that the nfsds 734 * will stop using it and clear ns_flag at the end so that it will not be 735 * reassigned during cleanup. 736 */ 737 void 738 nfsrv_zapsock(slp) 739 register struct nfssvc_sock *slp; 740 { 741 register struct nfsuid *nuidp, *nnuidp; 742 register struct nfsrv_descript *nwp, *nnwp; 743 struct socket *so; 744 struct file *fp; 745 struct mbuf *m; 746 int s; 747 748 slp->ns_flag &= ~SLP_ALLFLAGS; 749 fp = slp->ns_fp; 750 if (fp) { 751 slp->ns_fp = (struct file *)0; 752 so = slp->ns_so; 753 so->so_upcall = NULL; 754 soshutdown(so, 2); 755 closef(fp, (struct proc *)0); 756 if (slp->ns_nam) 757 MFREE(slp->ns_nam, m); 758 m_freem(slp->ns_raw); 759 m_freem(slp->ns_rec); 760 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0; 761 nuidp = nnuidp) { 762 nnuidp = nuidp->nu_lru.tqe_next; 763 LIST_REMOVE(nuidp, nu_hash); 764 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru); 765 if (nuidp->nu_flag & NU_NAM) 766 m_freem(nuidp->nu_nam); 767 free((caddr_t)nuidp, M_NFSUID); 768 } 769 s = splsoftclock(); 770 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) { 771 nnwp = nwp->nd_tq.le_next; 772 LIST_REMOVE(nwp, nd_tq); 773 free((caddr_t)nwp, M_NFSRVDESC); 774 } 775 LIST_INIT(&slp->ns_tq); 776 splx(s); 777 } 778 } 779 780 /* 781 * Derefence a server socket structure. If it has no more references and 782 * is no longer valid, you can throw it away. 783 */ 784 void 785 nfsrv_slpderef(slp) 786 register struct nfssvc_sock *slp; 787 { 788 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) { 789 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 790 free((caddr_t)slp, M_NFSSVC); 791 } 792 } 793 794 /* 795 * Initialize the data structures for the server. 796 * Handshake with any new nfsds starting up to avoid any chance of 797 * corruption. 798 */ 799 void 800 nfsrv_init(terminating) 801 int terminating; 802 { 803 register struct nfssvc_sock *slp, *nslp; 804 805 if (nfssvc_sockhead_flag & SLP_INIT) 806 panic("nfsd init"); 807 nfssvc_sockhead_flag |= SLP_INIT; 808 if (terminating) { 809 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) { 810 nslp = slp->ns_chain.tqe_next; 811 if (slp->ns_flag & SLP_VALID) 812 nfsrv_zapsock(slp); 813 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 814 free((caddr_t)slp, M_NFSSVC); 815 } 816 nfsrv_cleancache(); /* And clear out server cache */ 817 } 818 819 TAILQ_INIT(&nfssvc_sockhead); 820 nfssvc_sockhead_flag &= ~SLP_INIT; 821 if (nfssvc_sockhead_flag & SLP_WANTINIT) { 822 nfssvc_sockhead_flag &= ~SLP_WANTINIT; 823 wakeup((caddr_t)&nfssvc_sockhead); 824 } 825 826 TAILQ_INIT(&nfsd_head); 827 nfsd_head_flag &= ~NFSD_CHECKSLP; 828 829 nfs_udpsock = (struct nfssvc_sock *) 830 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 831 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock)); 832 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead); 833 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain); 834 835 nfs_cltpsock = (struct nfssvc_sock *) 836 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 837 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock)); 838 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead); 839 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain); 840 } 841 842 /* 843 * Add entries to the server monitor log. 844 */ 845 static void 846 nfsd_rt(sotype, nd, cacherep) 847 int sotype; 848 register struct nfsrv_descript *nd; 849 int cacherep; 850 { 851 register struct drt *rt; 852 853 rt = &nfsdrt.drt[nfsdrt.pos]; 854 if (cacherep == RC_DOIT) 855 rt->flag = 0; 856 else if (cacherep == RC_REPLY) 857 rt->flag = DRT_CACHEREPLY; 858 else 859 rt->flag = DRT_CACHEDROP; 860 if (sotype == SOCK_STREAM) 861 rt->flag |= DRT_TCP; 862 if (nd->nd_flag & ND_NQNFS) 863 rt->flag |= DRT_NQNFS; 864 else if (nd->nd_flag & ND_NFSV3) 865 rt->flag |= DRT_NFSV3; 866 rt->proc = nd->nd_procnum; 867 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET) 868 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr; 869 else 870 rt->ipadr = INADDR_ANY; 871 rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) + 872 (time.tv_usec - nd->nd_starttime.tv_usec); 873 rt->tstamp = time; 874 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ; 875 } 876 #endif /* NFSSERVER */ 877 878 #ifdef NFSCLIENT 879 /* 880 * Asynchronous I/O daemons for client nfs. 881 * They do read-ahead and write-behind operations on the block I/O cache. 882 * Never returns unless it fails or gets killed. 883 */ 884 int 885 nfssvc_iod(p) 886 struct proc *p; 887 { 888 register struct buf *bp, *nbp; 889 register int i, myiod; 890 struct vnode *vp; 891 int error = 0, s; 892 893 /* 894 * Assign my position or return error if too many already running 895 */ 896 myiod = -1; 897 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 898 if (nfs_asyncdaemon[i] == 0) { 899 nfs_asyncdaemon[i]++; 900 myiod = i; 901 break; 902 } 903 if (myiod == -1) 904 return (EBUSY); 905 nfs_numasync++; 906 /* 907 * Just loop around doin our stuff until SIGKILL 908 */ 909 for (;;) { 910 while (nfs_bufq.tqh_first == NULL && error == 0) { 911 nfs_iodwant[myiod] = p; 912 error = tsleep((caddr_t)&nfs_iodwant[myiod], 913 PWAIT | PCATCH, "nfsidl", 0); 914 } 915 while ((bp = nfs_bufq.tqh_first) != NULL) { 916 /* Take one off the front of the list */ 917 TAILQ_REMOVE(&nfs_bufq, bp, b_freelist); 918 if (bp->b_flags & B_READ) 919 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0); 920 else do { 921 /* 922 * Look for a delayed write for the same vnode, so I can do 923 * it now. We must grab it before calling nfs_doio() to 924 * avoid any risk of the vnode getting vclean()'d while 925 * we are doing the write rpc. 926 */ 927 vp = bp->b_vp; 928 s = splbio(); 929 for (nbp = vp->v_dirtyblkhd.lh_first; nbp; 930 nbp = nbp->b_vnbufs.le_next) { 931 if ((nbp->b_flags & 932 (B_BUSY|B_DELWRI|B_NEEDCOMMIT|B_NOCACHE))!=B_DELWRI) 933 continue; 934 bremfree(nbp); 935 nbp->b_flags |= (B_BUSY|B_ASYNC); 936 break; 937 } 938 splx(s); 939 /* 940 * For the delayed write, do the first part of nfs_bwrite() 941 * up to, but not including nfs_strategy(). 942 */ 943 if (nbp) { 944 nbp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI); 945 reassignbuf(nbp, nbp->b_vp); 946 nbp->b_vp->v_numoutput++; 947 } 948 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0); 949 } while ((bp = nbp) != NULL); 950 } 951 if (error) { 952 nfs_asyncdaemon[myiod] = 0; 953 nfs_numasync--; 954 return (error); 955 } 956 } 957 } 958 959 960 /* 961 * Get an authorization string for the uid by having the mount_nfs sitting 962 * on this mount point porpous out of the kernel and do it. 963 */ 964 int 965 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key) 966 register struct nfsmount *nmp; 967 struct nfsreq *rep; 968 struct ucred *cred; 969 char **auth_str; 970 int *auth_len; 971 char *verf_str; 972 int *verf_len; 973 NFSKERBKEY_T key; /* return session key */ 974 { 975 int error = 0; 976 977 while ((nmp->nm_flag & NFSMNT_WAITAUTH) == 0) { 978 nmp->nm_flag |= NFSMNT_WANTAUTH; 979 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK, 980 "nfsauth1", 2 * hz); 981 error = nfs_sigintr(nmp, rep, rep->r_procp); 982 if (error) { 983 nmp->nm_flag &= ~NFSMNT_WANTAUTH; 984 return (error); 985 } 986 } 987 nmp->nm_flag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH); 988 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK); 989 nmp->nm_authlen = RPCAUTH_MAXSIZ; 990 nmp->nm_verfstr = verf_str; 991 nmp->nm_verflen = *verf_len; 992 nmp->nm_authuid = cred->cr_uid; 993 wakeup((caddr_t)&nmp->nm_authstr); 994 995 /* 996 * And wait for mount_nfs to do its stuff. 997 */ 998 while ((nmp->nm_flag & NFSMNT_HASAUTH) == 0 && error == 0) { 999 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK, 1000 "nfsauth2", 2 * hz); 1001 error = nfs_sigintr(nmp, rep, rep->r_procp); 1002 } 1003 if (nmp->nm_flag & NFSMNT_AUTHERR) { 1004 nmp->nm_flag &= ~NFSMNT_AUTHERR; 1005 error = EAUTH; 1006 } 1007 if (error) 1008 free((caddr_t)*auth_str, M_TEMP); 1009 else { 1010 *auth_len = nmp->nm_authlen; 1011 *verf_len = nmp->nm_verflen; 1012 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key)); 1013 } 1014 nmp->nm_flag &= ~NFSMNT_HASAUTH; 1015 nmp->nm_flag |= NFSMNT_WAITAUTH; 1016 if (nmp->nm_flag & NFSMNT_WANTAUTH) { 1017 nmp->nm_flag &= ~NFSMNT_WANTAUTH; 1018 wakeup((caddr_t)&nmp->nm_authtype); 1019 } 1020 return (error); 1021 } 1022 1023 /* 1024 * Get a nickname authenticator and verifier. 1025 */ 1026 int 1027 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len) 1028 struct nfsmount *nmp; 1029 struct ucred *cred; 1030 char **auth_str; 1031 int *auth_len; 1032 char *verf_str; 1033 int verf_len; 1034 { 1035 register struct nfsuid *nuidp; 1036 register u_int32_t *nickp, *verfp; 1037 struct timeval ktvin, ktvout; 1038 1039 #ifdef DIAGNOSTIC 1040 if (verf_len < (4 * NFSX_UNSIGNED)) 1041 panic("nfs_getnickauth verf too small"); 1042 #endif 1043 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first; 1044 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 1045 if (nuidp->nu_cr.cr_uid == cred->cr_uid) 1046 break; 1047 } 1048 if (!nuidp || nuidp->nu_expire < time.tv_sec) 1049 return (EACCES); 1050 1051 /* 1052 * Move to the end of the lru list (end of lru == most recently used). 1053 */ 1054 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru); 1055 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru); 1056 1057 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK); 1058 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME); 1059 *nickp = txdr_unsigned(nuidp->nu_nickname); 1060 *auth_str = (char *)nickp; 1061 *auth_len = 2 * NFSX_UNSIGNED; 1062 1063 /* 1064 * Now we must encrypt the verifier and package it up. 1065 */ 1066 verfp = (u_int32_t *)verf_str; 1067 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME); 1068 if (time.tv_sec > nuidp->nu_timestamp.tv_sec || 1069 (time.tv_sec == nuidp->nu_timestamp.tv_sec && 1070 time.tv_usec > nuidp->nu_timestamp.tv_usec)) 1071 nuidp->nu_timestamp = time; 1072 else 1073 nuidp->nu_timestamp.tv_usec++; 1074 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec); 1075 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1076 1077 /* 1078 * Now encrypt the timestamp verifier in ecb mode using the session 1079 * key. 1080 */ 1081 #ifdef NFSKERB 1082 XXX 1083 #endif 1084 1085 *verfp++ = ktvout.tv_sec; 1086 *verfp++ = ktvout.tv_usec; 1087 *verfp = 0; 1088 return (0); 1089 } 1090 1091 /* 1092 * Save the current nickname in a hash list entry on the mount point. 1093 */ 1094 int 1095 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep) 1096 register struct nfsmount *nmp; 1097 struct ucred *cred; 1098 int len; 1099 NFSKERBKEY_T key; 1100 struct mbuf **mdp; 1101 char **dposp; 1102 struct mbuf *mrep; 1103 { 1104 register struct nfsuid *nuidp; 1105 register u_int32_t *tl; 1106 register int32_t t1; 1107 struct mbuf *md = *mdp; 1108 struct timeval ktvin, ktvout; 1109 u_int32_t nick; 1110 char *dpos = *dposp, *cp2; 1111 int deltasec, error = 0; 1112 1113 if (len == (3 * NFSX_UNSIGNED)) { 1114 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1115 ktvin.tv_sec = *tl++; 1116 ktvin.tv_usec = *tl++; 1117 nick = fxdr_unsigned(u_int32_t, *tl); 1118 1119 /* 1120 * Decrypt the timestamp in ecb mode. 1121 */ 1122 #ifdef NFSKERB 1123 XXX 1124 #endif 1125 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec); 1126 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec); 1127 deltasec = time.tv_sec - ktvout.tv_sec; 1128 if (deltasec < 0) 1129 deltasec = -deltasec; 1130 /* 1131 * If ok, add it to the hash list for the mount point. 1132 */ 1133 if (deltasec <= NFS_KERBCLOCKSKEW) { 1134 if (nmp->nm_numuids < nuidhash_max) { 1135 nmp->nm_numuids++; 1136 nuidp = (struct nfsuid *) 1137 malloc(sizeof (struct nfsuid), M_NFSUID, 1138 M_WAITOK); 1139 } else { 1140 nuidp = nmp->nm_uidlruhead.tqh_first; 1141 LIST_REMOVE(nuidp, nu_hash); 1142 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, 1143 nu_lru); 1144 } 1145 nuidp->nu_flag = 0; 1146 nuidp->nu_cr.cr_uid = cred->cr_uid; 1147 nuidp->nu_expire = time.tv_sec + NFS_KERBTTL; 1148 nuidp->nu_timestamp = ktvout; 1149 nuidp->nu_nickname = nick; 1150 bcopy(key, nuidp->nu_key, sizeof (key)); 1151 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, 1152 nu_lru); 1153 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid), 1154 nuidp, nu_hash); 1155 } 1156 } else 1157 nfsm_adv(nfsm_rndup(len)); 1158 nfsmout: 1159 *mdp = md; 1160 *dposp = dpos; 1161 return (error); 1162 } 1163 #endif /* NFSCLIENT */ 1164