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