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