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