nfs_nfsdkrpc.c revision 274027
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 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: stable/10/sys/fs/nfsserver/nfs_nfsdkrpc.c 274027 2014-11-03 10:30:44Z kib $");
36
37#include <sys/param.h>
38#include <sys/systm.h>
39
40#include "opt_inet6.h"
41#include "opt_kgssapi.h"
42
43#include <fs/nfs/nfsport.h>
44
45#include <rpc/rpc.h>
46#include <rpc/rpcsec_gss.h>
47
48#include <nfs/nfs_fha.h>
49#include <fs/nfsserver/nfs_fha_new.h>
50
51#include <security/mac/mac_framework.h>
52
53NFSDLOCKMUTEX;
54NFSV4ROOTLOCKMUTEX;
55struct nfsv4lock nfsd_suspend_lock;
56
57/*
58 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
59 */
60int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
61	NFSPROC_NULL,
62	NFSPROC_GETATTR,
63	NFSPROC_SETATTR,
64	NFSPROC_NOOP,
65	NFSPROC_LOOKUP,
66	NFSPROC_READLINK,
67	NFSPROC_READ,
68	NFSPROC_NOOP,
69	NFSPROC_WRITE,
70	NFSPROC_CREATE,
71	NFSPROC_REMOVE,
72	NFSPROC_RENAME,
73	NFSPROC_LINK,
74	NFSPROC_SYMLINK,
75	NFSPROC_MKDIR,
76	NFSPROC_RMDIR,
77	NFSPROC_READDIR,
78	NFSPROC_FSSTAT,
79	NFSPROC_NOOP,
80	NFSPROC_NOOP,
81	NFSPROC_NOOP,
82	NFSPROC_NOOP,
83};
84
85
86SYSCTL_DECL(_vfs_nfsd);
87
88SVCPOOL		*nfsrvd_pool;
89
90static int	nfs_privport = 0;
91SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN,
92    &nfs_privport, 0,
93    "Only allow clients using a privileged port for NFSv2 and 3");
94
95static int	nfs_minvers = NFS_VER2;
96SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RWTUN,
97    &nfs_minvers, 0, "The lowest version of NFS handled by the server");
98
99static int	nfs_maxvers = NFS_VER4;
100SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RWTUN,
101    &nfs_maxvers, 0, "The highest version of NFS handled by the server");
102
103static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
104    struct nfsrvcache **);
105
106extern u_long sb_max_adj;
107extern int newnfs_numnfsd;
108extern struct proc *nfsd_master_proc;
109
110/*
111 * NFS server system calls
112 */
113
114static void
115nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
116{
117	struct nfsrv_descript nd;
118	struct nfsrvcache *rp = NULL;
119	int cacherep, credflavor;
120
121	memset(&nd, 0, sizeof(nd));
122	if (rqst->rq_vers == NFS_VER2) {
123		if (rqst->rq_proc > NFSV2PROC_STATFS) {
124			svcerr_noproc(rqst);
125			svc_freereq(rqst);
126			goto out;
127		}
128		nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
129		nd.nd_flag = ND_NFSV2;
130	} else if (rqst->rq_vers == NFS_VER3) {
131		if (rqst->rq_proc >= NFS_V3NPROCS) {
132			svcerr_noproc(rqst);
133			svc_freereq(rqst);
134			goto out;
135		}
136		nd.nd_procnum = rqst->rq_proc;
137		nd.nd_flag = ND_NFSV3;
138	} else {
139		if (rqst->rq_proc != NFSPROC_NULL &&
140		    rqst->rq_proc != NFSV4PROC_COMPOUND) {
141			svcerr_noproc(rqst);
142			svc_freereq(rqst);
143			goto out;
144		}
145		nd.nd_procnum = rqst->rq_proc;
146		nd.nd_flag = ND_NFSV4;
147	}
148
149	/*
150	 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
151	 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
152	 * mounts.
153	 */
154	nd.nd_mrep = rqst->rq_args;
155	rqst->rq_args = NULL;
156	newnfs_realign(&nd.nd_mrep, M_WAITOK);
157	nd.nd_md = nd.nd_mrep;
158	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
159	nd.nd_nam = svc_getrpccaller(rqst);
160	nd.nd_nam2 = rqst->rq_addr;
161	nd.nd_mreq = NULL;
162	nd.nd_cred = NULL;
163
164	if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) {
165		/* Check if source port is privileged */
166		u_short port;
167		struct sockaddr *nam = nd.nd_nam;
168		struct sockaddr_in *sin;
169
170		sin = (struct sockaddr_in *)nam;
171		/*
172		 * INET/INET6 - same code:
173		 *    sin_port and sin6_port are at same offset
174		 */
175		port = ntohs(sin->sin_port);
176		if (port >= IPPORT_RESERVED &&
177		    nd.nd_procnum != NFSPROC_NULL) {
178#ifdef INET6
179			char b6[INET6_ADDRSTRLEN];
180#if defined(KLD_MODULE)
181			/* Do not use ip6_sprintf: the nfs module should work without INET6. */
182#define	ip6_sprintf(buf, a)						\
183			(sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x",	\
184			    (a)->s6_addr16[0], (a)->s6_addr16[1],	\
185			    (a)->s6_addr16[2], (a)->s6_addr16[3],	\
186			    (a)->s6_addr16[4], (a)->s6_addr16[5],	\
187			    (a)->s6_addr16[6], (a)->s6_addr16[7]),	\
188			    (buf))
189#endif
190#endif
191			printf("NFS request from unprivileged port (%s:%d)\n",
192#ifdef INET6
193			    sin->sin_family == AF_INET6 ?
194			    ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
195#if defined(KLD_MODULE)
196#undef ip6_sprintf
197#endif
198#endif
199			    inet_ntoa(sin->sin_addr), port);
200			svcerr_weakauth(rqst);
201			svc_freereq(rqst);
202			m_freem(nd.nd_mrep);
203			goto out;
204		}
205	}
206
207	if (nd.nd_procnum != NFSPROC_NULL) {
208		if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
209			svcerr_weakauth(rqst);
210			svc_freereq(rqst);
211			m_freem(nd.nd_mrep);
212			goto out;
213		}
214
215		/* Set the flag based on credflavor */
216		if (credflavor == RPCSEC_GSS_KRB5) {
217			nd.nd_flag |= ND_GSS;
218		} else if (credflavor == RPCSEC_GSS_KRB5I) {
219			nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
220		} else if (credflavor == RPCSEC_GSS_KRB5P) {
221			nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
222		} else if (credflavor != AUTH_SYS) {
223			svcerr_weakauth(rqst);
224			svc_freereq(rqst);
225			m_freem(nd.nd_mrep);
226			goto out;
227		}
228
229#ifdef MAC
230		mac_cred_associate_nfsd(nd.nd_cred);
231#endif
232		/*
233		 * Get a refcnt (shared lock) on nfsd_suspend_lock.
234		 * NFSSVC_SUSPENDNFSD will take an exclusive lock on
235		 * nfsd_suspend_lock to suspend these threads.
236		 * This must be done here, before the check of
237		 * nfsv4root exports by nfsvno_v4rootexport().
238		 */
239		NFSLOCKV4ROOTMUTEX();
240		nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
241		    NULL);
242		NFSUNLOCKV4ROOTMUTEX();
243
244		if ((nd.nd_flag & ND_NFSV4) != 0) {
245			nd.nd_repstat = nfsvno_v4rootexport(&nd);
246			if (nd.nd_repstat != 0) {
247				NFSLOCKV4ROOTMUTEX();
248				nfsv4_relref(&nfsd_suspend_lock);
249				NFSUNLOCKV4ROOTMUTEX();
250				svcerr_weakauth(rqst);
251				svc_freereq(rqst);
252				m_freem(nd.nd_mrep);
253				goto out;
254			}
255		}
256
257		cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
258		NFSLOCKV4ROOTMUTEX();
259		nfsv4_relref(&nfsd_suspend_lock);
260		NFSUNLOCKV4ROOTMUTEX();
261	} else {
262		NFSMGET(nd.nd_mreq);
263		nd.nd_mreq->m_len = 0;
264		cacherep = RC_REPLY;
265	}
266	if (nd.nd_mrep != NULL)
267		m_freem(nd.nd_mrep);
268
269	if (nd.nd_cred != NULL)
270		crfree(nd.nd_cred);
271
272	if (cacherep == RC_DROPIT) {
273		if (nd.nd_mreq != NULL)
274			m_freem(nd.nd_mreq);
275		svc_freereq(rqst);
276		goto out;
277	}
278
279	if (nd.nd_mreq == NULL) {
280		svcerr_decode(rqst);
281		svc_freereq(rqst);
282		goto out;
283	}
284
285	if (nd.nd_repstat & NFSERR_AUTHERR) {
286		svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
287		if (nd.nd_mreq != NULL)
288			m_freem(nd.nd_mreq);
289	} else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
290		svcerr_systemerr(rqst);
291	}
292	if (rp != NULL) {
293		nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
294		    SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
295	}
296	svc_freereq(rqst);
297
298out:
299	NFSEXITCODE(0);
300}
301
302/*
303 * Check the cache and, optionally, do the RPC.
304 * Return the appropriate cache response.
305 */
306static int
307nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
308    struct nfsrvcache **rpp)
309{
310	struct thread *td = curthread;
311	int cacherep = RC_DOIT, isdgram, taglen = -1;
312	struct mbuf *m;
313	u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
314	u_int32_t minorvers = 0;
315	uint32_t ack;
316
317	*rpp = NULL;
318	if (nd->nd_nam2 == NULL) {
319		nd->nd_flag |= ND_STREAMSOCK;
320		isdgram = 0;
321	} else {
322		isdgram = 1;
323	}
324
325	/*
326	 * Two cases:
327	 * 1 - For NFSv2 over UDP, if we are near our malloc/mget
328	 *     limit, just drop the request. There is no
329	 *     NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
330	 *     client will timeout/retry over UDP in a little while.
331	 * 2 - nd_repstat == 0 && nd_mreq == NULL, which
332	 *     means a normal nfs rpc, so check the cache
333	 */
334	if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
335	    nfsrv_mallocmget_limit()) {
336		cacherep = RC_DROPIT;
337	} else {
338		/*
339		 * For NFSv3, play it safe and assume that the client is
340		 * doing retries on the same TCP connection.
341		 */
342		if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
343		    ND_STREAMSOCK)
344			nd->nd_flag |= ND_SAMETCPCONN;
345		nd->nd_retxid = xid;
346		nd->nd_tcpconntime = NFSD_MONOSEC;
347		nd->nd_sockref = xprt->xp_sockref;
348		if ((nd->nd_flag & ND_NFSV4) != 0)
349			nfsd_getminorvers(nd, tag, &tagstr, &taglen,
350			    &minorvers);
351		if ((nd->nd_flag & ND_NFSV41) != 0)
352			/* NFSv4.1 caches replies in the session slots. */
353			cacherep = RC_DOIT;
354		else {
355			cacherep = nfsrvd_getcache(nd);
356			ack = 0;
357			SVC_ACK(xprt, &ack);
358			nfsrc_trimcache(xprt->xp_sockref, ack, 0);
359		}
360	}
361
362	/*
363	 * Handle the request. There are three cases.
364	 * RC_DOIT - do the RPC
365	 * RC_REPLY - return the reply already created
366	 * RC_DROPIT - just throw the request away
367	 */
368	if (cacherep == RC_DOIT) {
369		if ((nd->nd_flag & ND_NFSV41) != 0)
370			nd->nd_xprt = xprt;
371		nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers, td);
372		if ((nd->nd_flag & ND_NFSV41) != 0) {
373			if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
374			    (nd->nd_flag & ND_SAVEREPLY) != 0) {
375				/* Cache a copy of the reply. */
376				m = m_copym(nd->nd_mreq, 0, M_COPYALL,
377				    M_WAITOK);
378			} else
379				m = NULL;
380			if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
381				nfsrv_cache_session(nd->nd_sessionid,
382				    nd->nd_slotid, nd->nd_repstat, &m);
383			if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
384				nd->nd_repstat = 0;
385			cacherep = RC_REPLY;
386		} else {
387			if (nd->nd_repstat == NFSERR_DONTREPLY)
388				cacherep = RC_DROPIT;
389			else
390				cacherep = RC_REPLY;
391			*rpp = nfsrvd_updatecache(nd);
392		}
393	}
394	if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
395		free(tagstr, M_TEMP);
396
397	NFSEXITCODE2(0, nd);
398	return (cacherep);
399}
400
401static void
402nfssvc_loss(SVCXPRT *xprt)
403{
404	uint32_t ack;
405
406	ack = 0;
407	SVC_ACK(xprt, &ack);
408	nfsrc_trimcache(xprt->xp_sockref, ack, 1);
409}
410
411/*
412 * Adds a socket to the list for servicing by nfsds.
413 */
414int
415nfsrvd_addsock(struct file *fp)
416{
417	int siz;
418	struct socket *so;
419	int error = 0;
420	SVCXPRT *xprt;
421	static u_int64_t sockref = 0;
422
423	so = fp->f_data;
424
425	siz = sb_max_adj;
426	error = soreserve(so, siz, siz);
427	if (error)
428		goto out;
429
430	/*
431	 * Steal the socket from userland so that it doesn't close
432	 * unexpectedly.
433	 */
434	if (so->so_type == SOCK_DGRAM)
435		xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
436	else
437		xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
438	if (xprt) {
439		fp->f_ops = &badfileops;
440		fp->f_data = NULL;
441		xprt->xp_sockref = ++sockref;
442		if (nfs_minvers == NFS_VER2)
443			svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
444			    NULL);
445		if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
446			svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
447			    NULL);
448		if (nfs_maxvers >= NFS_VER4)
449			svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
450			    NULL);
451		if (so->so_type == SOCK_STREAM)
452			svc_loss_reg(xprt, nfssvc_loss);
453		SVC_RELEASE(xprt);
454	}
455
456out:
457	NFSEXITCODE(error);
458	return (error);
459}
460
461/*
462 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
463 * until it is killed by a signal.
464 */
465int
466nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
467{
468	char principal[MAXHOSTNAMELEN + 5];
469	int error = 0;
470	bool_t ret2, ret3, ret4;
471
472	error = copyinstr(args->principal, principal, sizeof (principal),
473	    NULL);
474	if (error)
475		goto out;
476
477	/*
478	 * Only the first nfsd actually does any work. The RPC code
479	 * adds threads to it as needed. Any extra processes offered
480	 * by nfsd just exit. If nfsd is new enough, it will call us
481	 * once with a structure that specifies how many threads to
482	 * use.
483	 */
484	NFSD_LOCK();
485	if (newnfs_numnfsd == 0) {
486		newnfs_numnfsd++;
487
488		NFSD_UNLOCK();
489
490		/* An empty string implies AUTH_SYS only. */
491		if (principal[0] != '\0') {
492			ret2 = rpc_gss_set_svc_name_call(principal,
493			    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
494			ret3 = rpc_gss_set_svc_name_call(principal,
495			    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
496			ret4 = rpc_gss_set_svc_name_call(principal,
497			    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4);
498
499			if (!ret2 || !ret3 || !ret4)
500				printf("nfsd: can't register svc name\n");
501		}
502
503		nfsrvd_pool->sp_minthreads = args->minthreads;
504		nfsrvd_pool->sp_maxthreads = args->maxthreads;
505
506		svc_run(nfsrvd_pool);
507
508		if (principal[0] != '\0') {
509			rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
510			rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
511			rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
512		}
513
514		NFSD_LOCK();
515		newnfs_numnfsd--;
516		nfsrvd_init(1);
517	}
518	NFSD_UNLOCK();
519
520out:
521	NFSEXITCODE(error);
522	return (error);
523}
524
525/*
526 * Initialize the data structures for the server.
527 * Handshake with any new nfsds starting up to avoid any chance of
528 * corruption.
529 */
530void
531nfsrvd_init(int terminating)
532{
533
534	NFSD_LOCK_ASSERT();
535
536	if (terminating) {
537		nfsd_master_proc = NULL;
538		NFSD_UNLOCK();
539		svcpool_destroy(nfsrvd_pool);
540		nfsrvd_pool = NULL;
541		NFSD_LOCK();
542	}
543
544	NFSD_UNLOCK();
545
546	nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
547	nfsrvd_pool->sp_rcache = NULL;
548	nfsrvd_pool->sp_assign = fhanew_assign;
549	nfsrvd_pool->sp_done = fha_nd_complete;
550
551	NFSD_LOCK();
552}
553
554