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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: releng/9.1/sys/nfsserver/nfs_srvkrpc.c 224778 2011-08-11 12:30:23Z rwatson $");
37
38 #include "opt_inet6.h"
39 #include "opt_kgssapi.h"
40
41 #include <sys/param.h>
42 #include <sys/capability.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/file.h>
48 #include <sys/filedesc.h>
49 #include <sys/jail.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/bio.h>
56 #include <sys/buf.h>
57 #include <sys/mbuf.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/domain.h>
61 #include <sys/protosw.h>
62 #include <sys/namei.h>
63 #include <sys/fcntl.h>
64 #include <sys/lockf.h>
65 #include <sys/eventhandler.h>
66
67 #include <netinet/in.h>
68 #include <netinet/tcp.h>
69 #ifdef INET6
70 #include <net/if.h>
71 #include <netinet6/in6_var.h>
72 #endif
73
74 #include <rpc/rpc.h>
75 #include <rpc/rpcsec_gss.h>
76 #include <rpc/replay.h>
77
78 #include <nfs/xdr_subs.h>
79 #include <nfs/nfsproto.h>
80 #include <nfsserver/nfs.h>
81 #include <nfsserver/nfsm_subs.h>
82 #include <nfsserver/nfsrvcache.h>
83 #include <nfsserver/nfs_fha.h>
84
85 #include <security/mac/mac_framework.h>
86
87 static MALLOC_DEFINE(M_NFSSVC, "nfss_srvsock", "Nfs server structure");
88
89 MALLOC_DEFINE(M_NFSRVDESC, "nfss_srvdesc", "NFS server socket descriptor");
90 MALLOC_DEFINE(M_NFSD, "nfss_daemon", "Nfs server daemon structure");
91
92 #define TRUE 1
93 #define FALSE 0
94
95 SYSCTL_DECL(_vfs_nfsrv);
96
97 SVCPOOL *nfsrv_pool;
98 int nfsd_waiting = 0;
99 int nfsrv_numnfsd = 0;
100 struct callout nfsrv_callout;
101 static eventhandler_tag nfsrv_nmbclusters_tag;
102
103 static int nfs_privport = 0;
104 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
105 &nfs_privport, 0,
106 "Only allow clients using a privileged port");
107 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
108 &nfsrvw_procrastinate, 0,
109 "Delay value for write gathering");
110 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
111 &nfsrvw_procrastinate_v3, 0,
112 "Delay in seconds for NFSv3 write gathering");
113
114 static int nfssvc_addsock(struct file *, struct thread *);
115 static int nfssvc_nfsd(struct thread *, struct nfsd_nfsd_args *);
116
117 extern u_long sb_max_adj;
118
119 int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd,
120 struct nfssvc_sock *slp, struct mbuf **mreqp) = {
121 nfsrv_null,
122 nfsrv_getattr,
123 nfsrv_setattr,
124 nfsrv_lookup,
125 nfsrv3_access,
126 nfsrv_readlink,
127 nfsrv_read,
128 nfsrv_write,
129 nfsrv_create,
130 nfsrv_mkdir,
131 nfsrv_symlink,
132 nfsrv_mknod,
133 nfsrv_remove,
134 nfsrv_rmdir,
135 nfsrv_rename,
136 nfsrv_link,
137 nfsrv_readdir,
138 nfsrv_readdirplus,
139 nfsrv_statfs,
140 nfsrv_fsinfo,
141 nfsrv_pathconf,
142 nfsrv_commit,
143 nfsrv_noop
144 };
145
146 /*
147 * NFS server system calls
148 */
149 /*
150 * This is now called from nfssvc() in nfs/nfs_nfssvc.c.
151 */
152
153 /*
154 * Nfs server psuedo system call for the nfsd's
155 * Based on the flag value it either:
156 * - adds a socket to the selection list
157 * - remains in the kernel as an nfsd
158 * - remains in the kernel as an nfsiod
159 * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
160 * and that mountd provides
161 * - sockaddr with no IPv4-mapped addresses
162 * - mask for both INET and INET6 families if there is IPv4-mapped overlap
163 */
164 int
165 nfssvc_nfsserver(struct thread *td, struct nfssvc_args *uap)
166 {
167 struct file *fp;
168 struct nfsd_addsock_args addsockarg;
169 struct nfsd_nfsd_args nfsdarg;
170 int error;
171
172 if (uap->flag & NFSSVC_ADDSOCK) {
173 error = copyin(uap->argp, (caddr_t)&addsockarg,
174 sizeof(addsockarg));
175 if (error)
176 return (error);
177 if ((error = fget(td, addsockarg.sock, CAP_SOCK_ALL, &fp)) != 0)
178 return (error);
179 if (fp->f_type != DTYPE_SOCKET) {
180 fdrop(fp, td);
181 return (error); /* XXXRW: Should be EINVAL? */
182 }
183 error = nfssvc_addsock(fp, td);
184 fdrop(fp, td);
185 } else if (uap->flag & NFSSVC_OLDNFSD)
186 error = nfssvc_nfsd(td, NULL);
187 else if (uap->flag & NFSSVC_NFSD) {
188 if (!uap->argp)
189 return (EINVAL);
190 error = copyin(uap->argp, (caddr_t)&nfsdarg,
191 sizeof(nfsdarg));
192 if (error)
193 return (error);
194 error = nfssvc_nfsd(td, &nfsdarg);
195 } else
196 error = ENXIO;
197 return (error);
198 }
199
200 /*
201 * Generate the rpc reply header
202 * siz arg. is used to decide if adding a cluster is worthwhile
203 */
204 struct mbuf *
205 nfs_rephead(int siz, struct nfsrv_descript *nd, int err,
206 struct mbuf **mbp, caddr_t *bposp)
207 {
208 u_int32_t *tl;
209 struct mbuf *mreq;
210 caddr_t bpos;
211 struct mbuf *mb;
212
213 if (err == EBADRPC)
214 return (NULL);
215
216 nd->nd_repstat = err;
217 if (err && (nd->nd_flag & ND_NFSV3) == 0) /* XXX recheck */
218 siz = 0;
219
220 MGET(mreq, M_WAIT, MT_DATA);
221
222 /*
223 * If this is a big reply, use a cluster
224 */
225 mreq->m_len = 0;
226 if (siz >= MINCLSIZE) {
227 MCLGET(mreq, M_WAIT);
228 }
229 mb = mreq;
230 bpos = mtod(mb, caddr_t);
231
232 if (err != NFSERR_RETVOID) {
233 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
234 if (err)
235 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
236 else
237 *tl = 0;
238 }
239
240 *mbp = mb;
241 *bposp = bpos;
242 if (err != 0 && err != NFSERR_RETVOID)
243 nfsrvstats.srvrpc_errs++;
244
245 return (mreq);
246 }
247
248 static void
249 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
250 {
251 rpcproc_t procnum;
252 int32_t (*proc)(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
253 struct mbuf **mreqp);
254 int flag;
255 struct nfsrv_descript nd;
256 struct mbuf *mreq, *mrep;
257 int error;
258
259 if (rqst->rq_vers == NFS_VER2) {
260 if (rqst->rq_proc > NFSV2PROC_STATFS) {
261 svcerr_noproc(rqst);
262 svc_freereq(rqst);
263 return;
264 }
265 procnum = nfsrv_nfsv3_procid[rqst->rq_proc];
266 flag = 0;
267 } else {
268 if (rqst->rq_proc >= NFS_NPROCS) {
269 svcerr_noproc(rqst);
270 svc_freereq(rqst);
271 return;
272 }
273 procnum = rqst->rq_proc;
274 flag = ND_NFSV3;
275 }
276 proc = nfsrv3_procs[procnum];
277
278 mreq = mrep = NULL;
279 mreq = rqst->rq_args;
280 rqst->rq_args = NULL;
281 (void)nfs_realign(&mreq, M_WAIT);
282
283 /*
284 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
285 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
286 * mounts.
287 */
288 memset(&nd, 0, sizeof(nd));
289 nd.nd_md = nd.nd_mrep = mreq;
290 nd.nd_dpos = mtod(mreq, caddr_t);
291 nd.nd_nam = svc_getrpccaller(rqst);
292 nd.nd_nam2 = rqst->rq_addr;
293 nd.nd_procnum = procnum;
294 nd.nd_cr = NULL;
295 nd.nd_flag = flag;
296
297 if (nfs_privport) {
298 /* Check if source port is privileged */
299 u_short port;
300 struct sockaddr *nam = nd.nd_nam;
301 struct sockaddr_in *sin;
302
303 sin = (struct sockaddr_in *)nam;
304 /*
305 * INET/INET6 - same code:
306 * sin_port and sin6_port are at same offset
307 */
308 port = ntohs(sin->sin_port);
309 if (port >= IPPORT_RESERVED &&
310 nd.nd_procnum != NFSPROC_NULL) {
311 #ifdef INET6
312 char b6[INET6_ADDRSTRLEN];
313 #if defined(KLD_MODULE)
314 /* Do not use ip6_sprintf: the nfs module should work without INET6. */
315 #define ip6_sprintf(buf, a) \
316 (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
317 (a)->s6_addr16[0], (a)->s6_addr16[1], \
318 (a)->s6_addr16[2], (a)->s6_addr16[3], \
319 (a)->s6_addr16[4], (a)->s6_addr16[5], \
320 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
321 (buf))
322 #endif
323 #endif
324 printf("NFS request from unprivileged port (%s:%d)\n",
325 #ifdef INET6
326 sin->sin_family == AF_INET6 ?
327 ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
328 #if defined(KLD_MODULE)
329 #undef ip6_sprintf
330 #endif
331 #endif
332 inet_ntoa(sin->sin_addr), port);
333 m_freem(mreq);
334 svcerr_weakauth(rqst);
335 svc_freereq(rqst);
336 return;
337 }
338 }
339
340 if (proc != nfsrv_null) {
341 if (!svc_getcred(rqst, &nd.nd_cr, &nd.nd_credflavor)) {
342 m_freem(mreq);
343 svcerr_weakauth(rqst);
344 svc_freereq(rqst);
345 return;
346 }
347 #ifdef MAC
348 mac_cred_associate_nfsd(nd.nd_cr);
349 #endif
350 }
351 nfsrvstats.srvrpccnt[nd.nd_procnum]++;
352
353 error = proc(&nd, NULL, &mrep);
354
355 if (nd.nd_cr)
356 crfree(nd.nd_cr);
357
358 if (mrep == NULL) {
359 svcerr_decode(rqst);
360 svc_freereq(rqst);
361 return;
362 }
363 if (error && error != NFSERR_RETVOID) {
364 svcerr_systemerr(rqst);
365 svc_freereq(rqst);
366 return;
367 }
368 if (nd.nd_repstat & NFSERR_AUTHERR) {
369 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
370 m_freem(mrep);
371 } else {
372 if (!svc_sendreply_mbuf(rqst, mrep))
373 svcerr_systemerr(rqst);
374 }
375 svc_freereq(rqst);
376 }
377
378 /*
379 * Adds a socket to the list for servicing by nfsds.
380 */
381 static int
382 nfssvc_addsock(struct file *fp, struct thread *td)
383 {
384 int siz;
385 struct socket *so;
386 int error;
387 SVCXPRT *xprt;
388
389 so = fp->f_data;
390
391 siz = sb_max_adj;
392 error = soreserve(so, siz, siz);
393 if (error)
394 return (error);
395
396 /*
397 * Steal the socket from userland so that it doesn't close
398 * unexpectedly.
399 */
400 if (so->so_type == SOCK_DGRAM)
401 xprt = svc_dg_create(nfsrv_pool, so, 0, 0);
402 else
403 xprt = svc_vc_create(nfsrv_pool, so, 0, 0);
404 if (xprt) {
405 fp->f_ops = &badfileops;
406 fp->f_data = NULL;
407 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, NULL);
408 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, NULL);
409 SVC_RELEASE(xprt);
410 }
411
412 return (0);
413 }
414
415 /*
416 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
417 * until it is killed by a signal.
418 */
419 static int
420 nfssvc_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
421 {
422 char principal[128];
423 int error;
424
425 if (args) {
426 error = copyinstr(args->principal, principal,
427 sizeof(principal), NULL);
428 if (error)
429 return (error);
430 } else {
431 memcpy(principal, "nfs@", 4);
432 getcredhostname(td->td_ucred, principal + 4,
433 sizeof(principal) - 4);
434 }
435
436 /*
437 * Only the first nfsd actually does any work. The RPC code
438 * adds threads to it as needed. Any extra processes offered
439 * by nfsd just exit. If nfsd is new enough, it will call us
440 * once with a structure that specifies how many threads to
441 * use.
442 */
443 NFSD_LOCK();
444 if (nfsrv_numnfsd == 0) {
445 nfsrv_numnfsd++;
446
447 NFSD_UNLOCK();
448
449 rpc_gss_set_svc_name_call(principal, "kerberosv5",
450 GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
451 rpc_gss_set_svc_name_call(principal, "kerberosv5",
452 GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
453
454 if (args) {
455 nfsrv_pool->sp_minthreads = args->minthreads;
456 nfsrv_pool->sp_maxthreads = args->maxthreads;
457 } else {
458 nfsrv_pool->sp_minthreads = 4;
459 nfsrv_pool->sp_maxthreads = 4;
460 }
461
462 svc_run(nfsrv_pool);
463
464 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
465 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
466
467 NFSD_LOCK();
468 nfsrv_numnfsd--;
469 nfsrv_init(TRUE);
470 }
471 NFSD_UNLOCK();
472
473 return (0);
474 }
475
476 /*
477 * Size the NFS server's duplicate request cache at 1/2 the
478 * nmbclusters, floating within a (64, 2048) range. This is to
479 * prevent all mbuf clusters being tied up in the NFS dupreq
480 * cache for small values of nmbclusters.
481 */
482 static size_t
483 nfsrv_replay_size(void)
484 {
485 size_t replaysiz;
486
487 replaysiz = nmbclusters / 2;
488 if (replaysiz > NFSRVCACHE_MAX_SIZE)
489 replaysiz = NFSRVCACHE_MAX_SIZE;
490 if (replaysiz < NFSRVCACHE_MIN_SIZE)
491 replaysiz = NFSRVCACHE_MIN_SIZE;
492 replaysiz *= MCLBYTES;
493
494 return (replaysiz);
495 }
496
497 /*
498 * Called when nmbclusters changes - we resize the replay cache
499 * accordingly.
500 */
501 static void
502 nfsrv_nmbclusters_change(void *tag)
503 {
504
505 if (nfsrv_pool)
506 replay_setsize(nfsrv_pool->sp_rcache, nfsrv_replay_size());
507 }
508
509 /*
510 * Initialize the data structures for the server.
511 * Handshake with any new nfsds starting up to avoid any chance of
512 * corruption.
513 */
514 void
515 nfsrv_init(int terminating)
516 {
517
518 NFSD_LOCK_ASSERT();
519
520 if (terminating) {
521 NFSD_UNLOCK();
522 EVENTHANDLER_DEREGISTER(nmbclusters_change,
523 nfsrv_nmbclusters_tag);
524 svcpool_destroy(nfsrv_pool);
525 nfsrv_pool = NULL;
526 NFSD_LOCK();
527 } else
528 nfs_pub.np_valid = 0;
529
530 NFSD_UNLOCK();
531
532 nfsrv_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsrv));
533 nfsrv_pool->sp_rcache = replay_newcache(nfsrv_replay_size());
534 nfsrv_pool->sp_assign = fha_assign;
535 nfsrv_pool->sp_done = fha_nd_complete;
536 nfsrv_nmbclusters_tag = EVENTHANDLER_REGISTER(nmbclusters_change,
537 nfsrv_nmbclusters_change, NULL, EVENTHANDLER_PRI_FIRST);
538
539 NFSD_LOCK();
540 }
Cache object: 608c69350c7a76a66b17801a0078cbf1
|