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