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