nfs_nfsiod.c revision 36503
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.38 1998/05/19 07:11:25 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 slp->ns_flag = (SLP_VALID | SLP_NEEDQ); 437 nfsrv_wakenfsd(slp); 438 splx(s); 439 return (0); 440} 441 442/* 443 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 444 * until it is killed by a signal. 445 */ 446static int 447nfssvc_nfsd(nsd, argp, p) 448 struct nfsd_srvargs *nsd; 449 caddr_t argp; 450 struct proc *p; 451{ 452 register struct mbuf *m; 453 register int siz; 454 register struct nfssvc_sock *slp; 455 register struct socket *so; 456 register int *solockp; 457 struct nfsd *nfsd = nsd->nsd_nfsd; 458 struct nfsrv_descript *nd = NULL; 459 struct mbuf *mreq; 460 int error = 0, cacherep, s, sotype, writes_todo; 461 int procrastinate; 462 u_quad_t cur_usec; 463 464#ifndef nolint 465 cacherep = RC_DOIT; 466 writes_todo = 0; 467#endif 468 if (nfsd == (struct nfsd *)0) { 469 nsd->nsd_nfsd = nfsd = (struct nfsd *) 470 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK); 471 bzero((caddr_t)nfsd, sizeof (struct nfsd)); 472 s = splnet(); 473 nfsd->nfsd_procp = p; 474 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain); 475 nfs_numnfsd++; 476 } else 477 s = splnet(); 478 479 /* 480 * Loop getting rpc requests until SIGKILL. 481 */ 482 for (;;) { 483 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) { 484 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 485 (nfsd_head_flag & NFSD_CHECKSLP) == 0) { 486 nfsd->nfsd_flag |= NFSD_WAITING; 487 nfsd_waiting++; 488 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH, 489 "nfsd", 0); 490 nfsd_waiting--; 491 if (error) 492 goto done; 493 } 494 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 495 (nfsd_head_flag & NFSD_CHECKSLP) != 0) { 496 for (slp = nfssvc_sockhead.tqh_first; slp != 0; 497 slp = slp->ns_chain.tqe_next) { 498 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC)) 499 == (SLP_VALID | SLP_DOREC)) { 500 slp->ns_flag &= ~SLP_DOREC; 501 slp->ns_sref++; 502 nfsd->nfsd_slp = slp; 503 break; 504 } 505 } 506 if (slp == 0) 507 nfsd_head_flag &= ~NFSD_CHECKSLP; 508 } 509 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0) 510 continue; 511 if (slp->ns_flag & SLP_VALID) { 512 if (slp->ns_flag & SLP_DISCONN) 513 nfsrv_zapsock(slp); 514 else if (slp->ns_flag & SLP_NEEDQ) { 515 slp->ns_flag &= ~SLP_NEEDQ; 516 (void) nfs_sndlock(&slp->ns_solock, 517 &slp->ns_solock, 518 (struct nfsreq *)0); 519 nfsrv_rcv(slp->ns_so, (caddr_t)slp, 520 M_WAIT); 521 nfs_sndunlock(&slp->ns_solock, 522 &slp->ns_solock); 523 } 524 error = nfsrv_dorec(slp, nfsd, &nd); 525 cur_usec = nfs_curusec(); 526 if (error && slp->ns_tq.lh_first && 527 slp->ns_tq.lh_first->nd_time <= cur_usec) { 528 error = 0; 529 cacherep = RC_DOIT; 530 writes_todo = 1; 531 } else 532 writes_todo = 0; 533 nfsd->nfsd_flag |= NFSD_REQINPROG; 534 } 535 } else { 536 error = 0; 537 slp = nfsd->nfsd_slp; 538 } 539 if (error || (slp->ns_flag & SLP_VALID) == 0) { 540 if (nd) { 541 free((caddr_t)nd, M_NFSRVDESC); 542 nd = NULL; 543 } 544 nfsd->nfsd_slp = (struct nfssvc_sock *)0; 545 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 546 nfsrv_slpderef(slp); 547 continue; 548 } 549 splx(s); 550 so = slp->ns_so; 551 sotype = so->so_type; 552 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 553 solockp = &slp->ns_solock; 554 else 555 solockp = (int *)0; 556 if (nd) { 557 getmicrotime(&nd->nd_starttime); 558 if (nd->nd_nam2) 559 nd->nd_nam = nd->nd_nam2; 560 else 561 nd->nd_nam = slp->ns_nam; 562 563 /* 564 * Check to see if authorization is needed. 565 */ 566 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) { 567 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH; 568 nsd->nsd_haddr = 569 ((struct sockaddr_in *) 570 nd->nd_nam)->sin_addr.s_addr; 571 nsd->nsd_authlen = nfsd->nfsd_authlen; 572 nsd->nsd_verflen = nfsd->nfsd_verflen; 573 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr, 574 nfsd->nfsd_authlen) && 575 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr, 576 nfsd->nfsd_verflen) && 577 !copyout((caddr_t)nsd, argp, sizeof (*nsd))) 578 return (ENEEDAUTH); 579 cacherep = RC_DROPIT; 580 } else 581 cacherep = nfsrv_getcache(nd, slp, &mreq); 582 583 /* 584 * Check for just starting up for NQNFS and send 585 * fake "try again later" replies to the NQNFS clients. 586 */ 587 if (notstarted && nqnfsstarttime <= time_second) { 588 if (modify_flag) { 589 nqnfsstarttime = time_second + nqsrv_writeslack; 590 modify_flag = 0; 591 } else 592 notstarted = 0; 593 } 594 if (notstarted) { 595 if ((nd->nd_flag & ND_NQNFS) == 0) 596 cacherep = RC_DROPIT; 597 else if (nd->nd_procnum != NFSPROC_WRITE) { 598 nd->nd_procnum = NFSPROC_NOOP; 599 nd->nd_repstat = NQNFS_TRYLATER; 600 cacherep = RC_DOIT; 601 } else 602 modify_flag = 1; 603 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) { 604 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL; 605 nd->nd_procnum = NFSPROC_NOOP; 606 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 607 cacherep = RC_DOIT; 608 } else if (nfs_privport) { 609 /* Check if source port is privileged */ 610 u_short port; 611 struct sockaddr *nam = nd->nd_nam; 612 struct sockaddr_in *sin; 613 614 sin = (struct sockaddr_in *)nam; 615 port = ntohs(sin->sin_port); 616 if (port >= IPPORT_RESERVED && 617 nd->nd_procnum != NFSPROC_NULL) { 618 nd->nd_procnum = NFSPROC_NOOP; 619 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 620 cacherep = RC_DOIT; 621 printf("NFS request from unprivileged port (%s:%d)\n", 622 inet_ntoa(sin->sin_addr), port); 623 } 624 } 625 626 } 627 628 /* 629 * Loop to get all the write rpc relies that have been 630 * gathered together. 631 */ 632 do { 633 switch (cacherep) { 634 case RC_DOIT: 635 if (nd && (nd->nd_flag & ND_NFSV3)) 636 procrastinate = nfsrvw_procrastinate_v3; 637 else 638 procrastinate = nfsrvw_procrastinate; 639 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE && 640 procrastinate > 0 && !notstarted)) 641 error = nfsrv_writegather(&nd, slp, 642 nfsd->nfsd_procp, &mreq); 643 else 644 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd, 645 slp, nfsd->nfsd_procp, &mreq); 646 if (mreq == NULL) 647 break; 648 if (error) { 649 if (nd->nd_procnum != NQNFSPROC_VACATED) 650 nfsstats.srv_errs++; 651 nfsrv_updatecache(nd, FALSE, mreq); 652 if (nd->nd_nam2) 653 FREE(nd->nd_nam2, M_SONAME); 654 break; 655 } 656 nfsstats.srvrpccnt[nd->nd_procnum]++; 657 nfsrv_updatecache(nd, TRUE, mreq); 658 nd->nd_mrep = (struct mbuf *)0; 659 case RC_REPLY: 660 m = mreq; 661 siz = 0; 662 while (m) { 663 siz += m->m_len; 664 m = m->m_next; 665 } 666 if (siz <= 0 || siz > NFS_MAXPACKET) { 667 printf("mbuf siz=%d\n",siz); 668 panic("Bad nfs svc reply"); 669 } 670 m = mreq; 671 m->m_pkthdr.len = siz; 672 m->m_pkthdr.rcvif = (struct ifnet *)0; 673 /* 674 * For stream protocols, prepend a Sun RPC 675 * Record Mark. 676 */ 677 if (sotype == SOCK_STREAM) { 678 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 679 *mtod(m, u_long *) = htonl(0x80000000 | siz); 680 } 681 if (solockp) 682 (void) nfs_sndlock(solockp, solockp, 683 (struct nfsreq *)0); 684 if (slp->ns_flag & SLP_VALID) 685 error = nfs_send(so, nd->nd_nam2, m, NULL); 686 else { 687 error = EPIPE; 688 m_freem(m); 689 } 690 if (nfsrtton) 691 nfsd_rt(sotype, nd, cacherep); 692 if (nd->nd_nam2) 693 FREE(nd->nd_nam2, M_SONAME); 694 if (nd->nd_mrep) 695 m_freem(nd->nd_mrep); 696 if (error == EPIPE) 697 nfsrv_zapsock(slp); 698 if (solockp) 699 nfs_sndunlock(solockp, solockp); 700 if (error == EINTR || error == ERESTART) { 701 free((caddr_t)nd, M_NFSRVDESC); 702 nfsrv_slpderef(slp); 703 s = splnet(); 704 goto done; 705 } 706 break; 707 case RC_DROPIT: 708 if (nfsrtton) 709 nfsd_rt(sotype, nd, cacherep); 710 m_freem(nd->nd_mrep); 711 FREE(nd->nd_nam2, M_SONAME); 712 break; 713 }; 714 if (nd) { 715 FREE((caddr_t)nd, M_NFSRVDESC); 716 nd = NULL; 717 } 718 719 /* 720 * Check to see if there are outstanding writes that 721 * need to be serviced. 722 */ 723 cur_usec = nfs_curusec(); 724 s = splsoftclock(); 725 if (slp->ns_tq.lh_first && 726 slp->ns_tq.lh_first->nd_time <= cur_usec) { 727 cacherep = RC_DOIT; 728 writes_todo = 1; 729 } else 730 writes_todo = 0; 731 splx(s); 732 } while (writes_todo); 733 s = splnet(); 734 if (nfsrv_dorec(slp, nfsd, &nd)) { 735 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 736 nfsd->nfsd_slp = NULL; 737 nfsrv_slpderef(slp); 738 } 739 } 740done: 741 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain); 742 splx(s); 743 free((caddr_t)nfsd, M_NFSD); 744 nsd->nsd_nfsd = (struct nfsd *)0; 745 if (--nfs_numnfsd == 0) 746 nfsrv_init(TRUE); /* Reinitialize everything */ 747 return (error); 748} 749 750/* 751 * Shut down a socket associated with an nfssvc_sock structure. 752 * Should be called with the send lock set, if required. 753 * The trick here is to increment the sref at the start, so that the nfsds 754 * will stop using it and clear ns_flag at the end so that it will not be 755 * reassigned during cleanup. 756 */ 757static void 758nfsrv_zapsock(slp) 759 register struct nfssvc_sock *slp; 760{ 761 register struct nfsuid *nuidp, *nnuidp; 762 register struct nfsrv_descript *nwp, *nnwp; 763 struct socket *so; 764 struct file *fp; 765 struct nfsrv_rec *rec; 766 int s; 767 768 slp->ns_flag &= ~SLP_ALLFLAGS; 769 fp = slp->ns_fp; 770 if (fp) { 771 slp->ns_fp = (struct file *)0; 772 so = slp->ns_so; 773 so->so_upcall = NULL; 774 soshutdown(so, 2); 775 closef(fp, (struct proc *)0); 776 if (slp->ns_nam) 777 FREE(slp->ns_nam, M_SONAME); 778 m_freem(slp->ns_raw); 779 while (rec = STAILQ_FIRST(&slp->ns_rec)) { 780 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link); 781 if (rec->nr_address) 782 FREE(rec->nr_address, M_SONAME); 783 m_freem(rec->nr_packet); 784 free(rec, M_NFSRVDESC); 785 } 786 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0; 787 nuidp = nnuidp) { 788 nnuidp = nuidp->nu_lru.tqe_next; 789 LIST_REMOVE(nuidp, nu_hash); 790 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru); 791 if (nuidp->nu_flag & NU_NAM) 792 FREE(nuidp->nu_nam, M_SONAME); 793 free((caddr_t)nuidp, M_NFSUID); 794 } 795 s = splsoftclock(); 796 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) { 797 nnwp = nwp->nd_tq.le_next; 798 LIST_REMOVE(nwp, nd_tq); 799 free((caddr_t)nwp, M_NFSRVDESC); 800 } 801 LIST_INIT(&slp->ns_tq); 802 splx(s); 803 } 804} 805 806/* 807 * Derefence a server socket structure. If it has no more references and 808 * is no longer valid, you can throw it away. 809 */ 810void 811nfsrv_slpderef(slp) 812 register struct nfssvc_sock *slp; 813{ 814 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) { 815 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 816 free((caddr_t)slp, M_NFSSVC); 817 } 818} 819 820/* 821 * Initialize the data structures for the server. 822 * Handshake with any new nfsds starting up to avoid any chance of 823 * corruption. 824 */ 825void 826nfsrv_init(terminating) 827 int terminating; 828{ 829 register struct nfssvc_sock *slp, *nslp; 830 831 if (nfssvc_sockhead_flag & SLP_INIT) 832 panic("nfsd init"); 833 nfssvc_sockhead_flag |= SLP_INIT; 834 if (terminating) { 835 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) { 836 nslp = slp->ns_chain.tqe_next; 837 if (slp->ns_flag & SLP_VALID) 838 nfsrv_zapsock(slp); 839 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 840 free((caddr_t)slp, M_NFSSVC); 841 } 842 nfsrv_cleancache(); /* And clear out server cache */ 843 } else 844 nfs_pub.np_valid = 0; 845 846 TAILQ_INIT(&nfssvc_sockhead); 847 nfssvc_sockhead_flag &= ~SLP_INIT; 848 if (nfssvc_sockhead_flag & SLP_WANTINIT) { 849 nfssvc_sockhead_flag &= ~SLP_WANTINIT; 850 wakeup((caddr_t)&nfssvc_sockhead); 851 } 852 853 TAILQ_INIT(&nfsd_head); 854 nfsd_head_flag &= ~NFSD_CHECKSLP; 855 856 nfs_udpsock = (struct nfssvc_sock *) 857 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 858 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock)); 859 STAILQ_INIT(&nfs_udpsock->ns_rec); 860 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead); 861 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain); 862 863 nfs_cltpsock = (struct nfssvc_sock *) 864 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 865 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock)); 866 STAILQ_INIT(&nfs_cltpsock->ns_rec); 867 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead); 868 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain); 869} 870 871/* 872 * Add entries to the server monitor log. 873 */ 874static void 875nfsd_rt(sotype, nd, cacherep) 876 int sotype; 877 register struct nfsrv_descript *nd; 878 int cacherep; 879{ 880 register struct drt *rt; 881 882 rt = &nfsdrt.drt[nfsdrt.pos]; 883 if (cacherep == RC_DOIT) 884 rt->flag = 0; 885 else if (cacherep == RC_REPLY) 886 rt->flag = DRT_CACHEREPLY; 887 else 888 rt->flag = DRT_CACHEDROP; 889 if (sotype == SOCK_STREAM) 890 rt->flag |= DRT_TCP; 891 if (nd->nd_flag & ND_NQNFS) 892 rt->flag |= DRT_NQNFS; 893 else if (nd->nd_flag & ND_NFSV3) 894 rt->flag |= DRT_NFSV3; 895 rt->proc = nd->nd_procnum; 896 if (nd->nd_nam->sa_family == AF_INET) 897 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr; 898 else 899 rt->ipadr = INADDR_ANY; 900 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec); 901 getmicrotime(&rt->tstamp); 902 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ; 903} 904#endif /* NFS_NOSERVER */ 905 906static int nfs_defect = 0; 907SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, ""); 908 909/* 910 * Asynchronous I/O daemons for client nfs. 911 * They do read-ahead and write-behind operations on the block I/O cache. 912 * Never returns unless it fails or gets killed. 913 */ 914static int 915nfssvc_iod(p) 916 struct proc *p; 917{ 918 register struct buf *bp; 919 register int i, myiod; 920 struct nfsmount *nmp; 921 int error = 0; 922 923 /* 924 * Assign my position or return error if too many already running 925 */ 926 myiod = -1; 927 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 928 if (nfs_asyncdaemon[i] == 0) { 929 nfs_asyncdaemon[i]++; 930 myiod = i; 931 break; 932 } 933 if (myiod == -1) 934 return (EBUSY); 935 nfs_numasync++; 936 /* 937 * Just loop around doin our stuff until SIGKILL 938 */ 939 for (;;) { 940 while (((nmp = nfs_iodmount[myiod]) == NULL 941 || nmp->nm_bufq.tqh_first == NULL) 942 && error == 0) { 943 if (nmp) 944 nmp->nm_bufqiods--; 945 nfs_iodwant[myiod] = p; 946 nfs_iodmount[myiod] = NULL; 947 error = tsleep((caddr_t)&nfs_iodwant[myiod], 948 PWAIT | PCATCH, "nfsidl", 0); 949 } 950 if (error) { 951 nfs_asyncdaemon[myiod] = 0; 952 if (nmp) 953 nmp->nm_bufqiods--; 954 nfs_iodwant[myiod] = NULL; 955 nfs_iodmount[myiod] = NULL; 956 nfs_numasync--; 957 return (error); 958 } 959 while ((bp = nmp->nm_bufq.tqh_first) != NULL) { 960 /* Take one off the front of the list */ 961 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist); 962 nmp->nm_bufqlen--; 963 if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) { 964 nmp->nm_bufqwant = FALSE; 965 wakeup(&nmp->nm_bufq); 966 } 967 if (bp->b_flags & B_READ) 968 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0); 969 else 970 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0); 971 /* 972 * If there are more than one iod on this mount, then defect 973 * so that the iods can be shared out fairly between the mounts 974 */ 975 if (nfs_defect && nmp->nm_bufqiods > 1) { 976 NFS_DPF(ASYNCIO, 977 ("nfssvc_iod: iod %d defecting from mount %p\n", 978 myiod, nmp)); 979 nfs_iodmount[myiod] = NULL; 980 nmp->nm_bufqiods--; 981 break; 982 } 983 } 984 } 985} 986 987 988/* 989 * Get an authorization string for the uid by having the mount_nfs sitting 990 * on this mount point porpous out of the kernel and do it. 991 */ 992int 993nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key) 994 register struct nfsmount *nmp; 995 struct nfsreq *rep; 996 struct ucred *cred; 997 char **auth_str; 998 int *auth_len; 999 char *verf_str; 1000 int *verf_len; 1001 NFSKERBKEY_T key; /* return session key */ 1002{ 1003 int error = 0; 1004 1005 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) { 1006 nmp->nm_state |= NFSSTA_WANTAUTH; 1007 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK, 1008 "nfsauth1", 2 * hz); 1009 error = nfs_sigintr(nmp, rep, rep->r_procp); 1010 if (error) { 1011 nmp->nm_state &= ~NFSSTA_WANTAUTH; 1012 return (error); 1013 } 1014 } 1015 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH); 1016 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK); 1017 nmp->nm_authlen = RPCAUTH_MAXSIZ; 1018 nmp->nm_verfstr = verf_str; 1019 nmp->nm_verflen = *verf_len; 1020 nmp->nm_authuid = cred->cr_uid; 1021 wakeup((caddr_t)&nmp->nm_authstr); 1022 1023 /* 1024 * And wait for mount_nfs to do its stuff. 1025 */ 1026 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) { 1027 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK, 1028 "nfsauth2", 2 * hz); 1029 error = nfs_sigintr(nmp, rep, rep->r_procp); 1030 } 1031 if (nmp->nm_state & NFSSTA_AUTHERR) { 1032 nmp->nm_state &= ~NFSSTA_AUTHERR; 1033 error = EAUTH; 1034 } 1035 if (error) 1036 free((caddr_t)*auth_str, M_TEMP); 1037 else { 1038 *auth_len = nmp->nm_authlen; 1039 *verf_len = nmp->nm_verflen; 1040 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key)); 1041 } 1042 nmp->nm_state &= ~NFSSTA_HASAUTH; 1043 nmp->nm_state |= NFSSTA_WAITAUTH; 1044 if (nmp->nm_state & NFSSTA_WANTAUTH) { 1045 nmp->nm_state &= ~NFSSTA_WANTAUTH; 1046 wakeup((caddr_t)&nmp->nm_authtype); 1047 } 1048 return (error); 1049} 1050 1051/* 1052 * Get a nickname authenticator and verifier. 1053 */ 1054int 1055nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len) 1056 struct nfsmount *nmp; 1057 struct ucred *cred; 1058 char **auth_str; 1059 int *auth_len; 1060 char *verf_str; 1061 int verf_len; 1062{ 1063 register struct nfsuid *nuidp; 1064 register u_long *nickp, *verfp; 1065 struct timeval ktvin, ktvout; 1066 1067#ifdef DIAGNOSTIC 1068 if (verf_len < (4 * NFSX_UNSIGNED)) 1069 panic("nfs_getnickauth verf too small"); 1070#endif 1071 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first; 1072 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 1073 if (nuidp->nu_cr.cr_uid == cred->cr_uid) 1074 break; 1075 } 1076 if (!nuidp || nuidp->nu_expire < time_second) 1077 return (EACCES); 1078 1079 /* 1080 * Move to the end of the lru list (end of lru == most recently used). 1081 */ 1082 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru); 1083 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru); 1084 1085 nickp = (u_long *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK); 1086 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME); 1087 *nickp = txdr_unsigned(nuidp->nu_nickname); 1088 *auth_str = (char *)nickp; 1089 *auth_len = 2 * NFSX_UNSIGNED; 1090 1091 /* 1092 * Now we must encrypt the verifier and package it up. 1093 */ 1094 verfp = (u_long *)verf_str; 1095 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME); 1096 if (time_second > nuidp->nu_timestamp.tv_sec || 1097 (time_second == nuidp->nu_timestamp.tv_sec && 1098 time_second > nuidp->nu_timestamp.tv_usec)) 1099 getmicrotime(&nuidp->nu_timestamp); 1100 else 1101 nuidp->nu_timestamp.tv_usec++; 1102 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec); 1103 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1104 1105 /* 1106 * Now encrypt the timestamp verifier in ecb mode using the session 1107 * key. 1108 */ 1109#ifdef NFSKERB 1110 XXX 1111#endif 1112 1113 *verfp++ = ktvout.tv_sec; 1114 *verfp++ = ktvout.tv_usec; 1115 *verfp = 0; 1116 return (0); 1117} 1118 1119/* 1120 * Save the current nickname in a hash list entry on the mount point. 1121 */ 1122int 1123nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep) 1124 register struct nfsmount *nmp; 1125 struct ucred *cred; 1126 int len; 1127 NFSKERBKEY_T key; 1128 struct mbuf **mdp; 1129 char **dposp; 1130 struct mbuf *mrep; 1131{ 1132 register struct nfsuid *nuidp; 1133 register u_long *tl; 1134 register long t1; 1135 struct mbuf *md = *mdp; 1136 struct timeval ktvin, ktvout; 1137 u_long nick; 1138 char *dpos = *dposp, *cp2; 1139 int deltasec, error = 0; 1140 1141 if (len == (3 * NFSX_UNSIGNED)) { 1142 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED); 1143 ktvin.tv_sec = *tl++; 1144 ktvin.tv_usec = *tl++; 1145 nick = fxdr_unsigned(u_long, *tl); 1146 1147 /* 1148 * Decrypt the timestamp in ecb mode. 1149 */ 1150#ifdef NFSKERB 1151 XXX 1152#endif 1153 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec); 1154 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec); 1155 deltasec = time_second - ktvout.tv_sec; 1156 if (deltasec < 0) 1157 deltasec = -deltasec; 1158 /* 1159 * If ok, add it to the hash list for the mount point. 1160 */ 1161 if (deltasec <= NFS_KERBCLOCKSKEW) { 1162 if (nmp->nm_numuids < nuidhash_max) { 1163 nmp->nm_numuids++; 1164 nuidp = (struct nfsuid *) 1165 malloc(sizeof (struct nfsuid), M_NFSUID, 1166 M_WAITOK); 1167 } else { 1168 nuidp = nmp->nm_uidlruhead.tqh_first; 1169 LIST_REMOVE(nuidp, nu_hash); 1170 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, 1171 nu_lru); 1172 } 1173 nuidp->nu_flag = 0; 1174 nuidp->nu_cr.cr_uid = cred->cr_uid; 1175 nuidp->nu_expire = time_second + NFS_KERBTTL; 1176 nuidp->nu_timestamp = ktvout; 1177 nuidp->nu_nickname = nick; 1178 bcopy(key, nuidp->nu_key, sizeof (key)); 1179 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, 1180 nu_lru); 1181 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid), 1182 nuidp, nu_hash); 1183 } 1184 } else 1185 nfsm_adv(nfsm_rndup(len)); 1186nfsmout: 1187 *mdp = md; 1188 *dposp = dpos; 1189 return (error); 1190} 1191