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