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