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