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