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