nfs_nfsdkrpc.c revision 1.3 1 /* $NetBSD: nfs_nfsdkrpc.c,v 1.3 2016/11/18 08:31:30 pgoyette Exp $ */
2 /*-
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Rick Macklem at The University of Guelph.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 */
34
35 #include <sys/cdefs.h>
36 /* __FBSDID("FreeBSD: head/sys/fs/nfsserver/nfs_nfsdkrpc.c 299203 2016-05-06 23:40:37Z pfg "); */
37 __RCSID("$NetBSD: nfs_nfsdkrpc.c,v 1.3 2016/11/18 08:31:30 pgoyette Exp $");
38
39 #include "opt_inet6.h"
40 #include "opt_kgssapi.h"
41
42 #include <fs/nfs/nfsport.h>
43
44 #include <rpc/rpc.h>
45 #include <rpc/rpcsec_gss.h>
46
47 #include <nfs/nfs_fha.h>
48 #include <fs/nfsserver/nfs_fha_new.h>
49
50 #include <security/mac/mac_framework.h>
51
52 NFSDLOCKMUTEX;
53 NFSV4ROOTLOCKMUTEX;
54 struct nfsv4lock nfsd_suspend_lock;
55
56 /*
57 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
58 */
59 int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
60 NFSPROC_NULL,
61 NFSPROC_GETATTR,
62 NFSPROC_SETATTR,
63 NFSPROC_NOOP,
64 NFSPROC_LOOKUP,
65 NFSPROC_READLINK,
66 NFSPROC_READ,
67 NFSPROC_NOOP,
68 NFSPROC_WRITE,
69 NFSPROC_CREATE,
70 NFSPROC_REMOVE,
71 NFSPROC_RENAME,
72 NFSPROC_LINK,
73 NFSPROC_SYMLINK,
74 NFSPROC_MKDIR,
75 NFSPROC_RMDIR,
76 NFSPROC_READDIR,
77 NFSPROC_FSSTAT,
78 NFSPROC_NOOP,
79 NFSPROC_NOOP,
80 NFSPROC_NOOP,
81 NFSPROC_NOOP,
82 };
83
84
85 SYSCTL_DECL(_vfs_nfsd);
86
87 SVCPOOL *nfsrvd_pool;
88
89 static int nfs_privport = 0;
90 SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN,
91 &nfs_privport, 0,
92 "Only allow clients using a privileged port for NFSv2 and 3");
93
94 static int nfs_minvers = NFS_VER2;
95 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RWTUN,
96 &nfs_minvers, 0, "The lowest version of NFS handled by the server");
97
98 static int nfs_maxvers = NFS_VER4;
99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RWTUN,
100 &nfs_maxvers, 0, "The highest version of NFS handled by the server");
101
102 static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
103 struct nfsrvcache **);
104
105 extern u_long sb_max_adj;
106 extern int newnfs_numnfsd;
107 extern struct proc *nfsd_master_proc;
108
109 /*
110 * NFS server system calls
111 */
112
113 static void
114 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
115 {
116 struct nfsrv_descript nd;
117 struct nfsrvcache *rp = NULL;
118 int cacherep, credflavor;
119
120 memset(&nd, 0, sizeof(nd));
121 if (rqst->rq_vers == NFS_VER2) {
122 if (rqst->rq_proc > NFSV2PROC_STATFS ||
123 newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
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 (snprintf((buf), sizeof(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 * The call to nfsv4_lock() that precedes nfsv4_getref()
237 * ensures that the acquisition of the exclusive lock
238 * takes priority over acquisition of the shared lock by
239 * waiting for any exclusive lock request to complete.
240 * This must be done here, before the check of
241 * nfsv4root exports by nfsvno_v4rootexport().
242 */
243 NFSLOCKV4ROOTMUTEX();
244 nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR,
245 NULL);
246 nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
247 NULL);
248 NFSUNLOCKV4ROOTMUTEX();
249
250 if ((nd.nd_flag & ND_NFSV4) != 0) {
251 nd.nd_repstat = nfsvno_v4rootexport(&nd);
252 if (nd.nd_repstat != 0) {
253 NFSLOCKV4ROOTMUTEX();
254 nfsv4_relref(&nfsd_suspend_lock);
255 NFSUNLOCKV4ROOTMUTEX();
256 svcerr_weakauth(rqst);
257 svc_freereq(rqst);
258 m_freem(nd.nd_mrep);
259 goto out;
260 }
261 }
262
263 cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
264 NFSLOCKV4ROOTMUTEX();
265 nfsv4_relref(&nfsd_suspend_lock);
266 NFSUNLOCKV4ROOTMUTEX();
267 } else {
268 NFSMGET(nd.nd_mreq);
269 nd.nd_mreq->m_len = 0;
270 cacherep = RC_REPLY;
271 }
272 if (nd.nd_mrep != NULL)
273 m_freem(nd.nd_mrep);
274
275 if (nd.nd_cred != NULL)
276 crfree(nd.nd_cred);
277
278 if (cacherep == RC_DROPIT) {
279 if (nd.nd_mreq != NULL)
280 m_freem(nd.nd_mreq);
281 svc_freereq(rqst);
282 goto out;
283 }
284
285 if (nd.nd_mreq == NULL) {
286 svcerr_decode(rqst);
287 svc_freereq(rqst);
288 goto out;
289 }
290
291 if (nd.nd_repstat & NFSERR_AUTHERR) {
292 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
293 if (nd.nd_mreq != NULL)
294 m_freem(nd.nd_mreq);
295 } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
296 svcerr_systemerr(rqst);
297 }
298 if (rp != NULL) {
299 nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
300 SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
301 }
302 svc_freereq(rqst);
303
304 out:
305 if (softdep_ast_cleanup != NULL)
306 softdep_ast_cleanup();
307 NFSEXITCODE(0);
308 }
309
310 /*
311 * Check the cache and, optionally, do the RPC.
312 * Return the appropriate cache response.
313 */
314 static int
315 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
316 struct nfsrvcache **rpp)
317 {
318 struct thread *td = curthread;
319 int cacherep = RC_DOIT, isdgram, taglen = -1;
320 struct mbuf *m;
321 u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
322 u_int32_t minorvers = 0;
323 uint32_t ack;
324
325 *rpp = NULL;
326 if (nd->nd_nam2 == NULL) {
327 nd->nd_flag |= ND_STREAMSOCK;
328 isdgram = 0;
329 } else {
330 isdgram = 1;
331 }
332
333 /*
334 * Two cases:
335 * 1 - For NFSv2 over UDP, if we are near our malloc/mget
336 * limit, just drop the request. There is no
337 * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
338 * client will timeout/retry over UDP in a little while.
339 * 2 - nd_repstat == 0 && nd_mreq == NULL, which
340 * means a normal nfs rpc, so check the cache
341 */
342 if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
343 nfsrv_mallocmget_limit()) {
344 cacherep = RC_DROPIT;
345 } else {
346 /*
347 * For NFSv3, play it safe and assume that the client is
348 * doing retries on the same TCP connection.
349 */
350 if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
351 ND_STREAMSOCK)
352 nd->nd_flag |= ND_SAMETCPCONN;
353 nd->nd_retxid = xid;
354 nd->nd_tcpconntime = NFSD_MONOSEC;
355 nd->nd_sockref = xprt->xp_sockref;
356 if ((nd->nd_flag & ND_NFSV4) != 0)
357 nfsd_getminorvers(nd, tag, &tagstr, &taglen,
358 &minorvers);
359 if ((nd->nd_flag & ND_NFSV41) != 0)
360 /* NFSv4.1 caches replies in the session slots. */
361 cacherep = RC_DOIT;
362 else {
363 cacherep = nfsrvd_getcache(nd);
364 ack = 0;
365 SVC_ACK(xprt, &ack);
366 nfsrc_trimcache(xprt->xp_sockref, ack, 0);
367 }
368 }
369
370 /*
371 * Handle the request. There are three cases.
372 * RC_DOIT - do the RPC
373 * RC_REPLY - return the reply already created
374 * RC_DROPIT - just throw the request away
375 */
376 if (cacherep == RC_DOIT) {
377 if ((nd->nd_flag & ND_NFSV41) != 0)
378 nd->nd_xprt = xprt;
379 nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers, td);
380 if ((nd->nd_flag & ND_NFSV41) != 0) {
381 if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
382 (nd->nd_flag & ND_SAVEREPLY) != 0) {
383 /* Cache a copy of the reply. */
384 m = m_copym(nd->nd_mreq, 0, M_COPYALL,
385 M_WAITOK);
386 } else
387 m = NULL;
388 if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
389 nfsrv_cache_session(nd->nd_sessionid,
390 nd->nd_slotid, nd->nd_repstat, &m);
391 if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
392 nd->nd_repstat = 0;
393 cacherep = RC_REPLY;
394 } else {
395 if (nd->nd_repstat == NFSERR_DONTREPLY)
396 cacherep = RC_DROPIT;
397 else
398 cacherep = RC_REPLY;
399 *rpp = nfsrvd_updatecache(nd);
400 }
401 }
402 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
403 free(tagstr, M_TEMP);
404
405 NFSEXITCODE2(0, nd);
406 return (cacherep);
407 }
408
409 static void
410 nfssvc_loss(SVCXPRT *xprt)
411 {
412 uint32_t ack;
413
414 ack = 0;
415 SVC_ACK(xprt, &ack);
416 nfsrc_trimcache(xprt->xp_sockref, ack, 1);
417 }
418
419 /*
420 * Adds a socket to the list for servicing by nfsds.
421 */
422 int
423 nfsrvd_addsock(struct file *fp)
424 {
425 int siz;
426 struct socket *so;
427 int error = 0;
428 SVCXPRT *xprt;
429 static u_int64_t sockref = 0;
430
431 so = fp->f_data;
432
433 siz = sb_max_adj;
434 error = soreserve(so, siz, siz);
435 if (error)
436 goto out;
437
438 /*
439 * Steal the socket from userland so that it doesn't close
440 * unexpectedly.
441 */
442 if (so->so_type == SOCK_DGRAM)
443 xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
444 else
445 xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
446 if (xprt) {
447 fp->f_ops = &badfileops;
448 fp->f_data = NULL;
449 xprt->xp_sockref = ++sockref;
450 if (nfs_minvers == NFS_VER2)
451 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
452 NULL);
453 if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
454 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
455 NULL);
456 if (nfs_maxvers >= NFS_VER4)
457 svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
458 NULL);
459 if (so->so_type == SOCK_STREAM)
460 svc_loss_reg(xprt, nfssvc_loss);
461 SVC_RELEASE(xprt);
462 }
463
464 out:
465 NFSEXITCODE(error);
466 return (error);
467 }
468
469 /*
470 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
471 * until it is killed by a signal.
472 */
473 int
474 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
475 {
476 char principal[MAXHOSTNAMELEN + 5];
477 struct proc *p;
478 int error = 0;
479 bool_t ret2, ret3, ret4;
480
481 error = copyinstr(args->principal, principal, sizeof (principal),
482 NULL);
483 if (error)
484 goto out;
485
486 /*
487 * Only the first nfsd actually does any work. The RPC code
488 * adds threads to it as needed. Any extra processes offered
489 * by nfsd just exit. If nfsd is new enough, it will call us
490 * once with a structure that specifies how many threads to
491 * use.
492 */
493 NFSD_LOCK();
494 if (newnfs_numnfsd == 0) {
495 p = td->td_proc;
496 PROC_LOCK(p);
497 p->p_flag2 |= P2_AST_SU;
498 PROC_UNLOCK(p);
499 newnfs_numnfsd++;
500
501 NFSD_UNLOCK();
502
503 /* An empty string implies AUTH_SYS only. */
504 if (principal[0] != '\0') {
505 ret2 = rpc_gss_set_svc_name_call(principal,
506 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
507 ret3 = rpc_gss_set_svc_name_call(principal,
508 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
509 ret4 = rpc_gss_set_svc_name_call(principal,
510 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4);
511
512 if (!ret2 || !ret3 || !ret4)
513 printf("nfsd: can't register svc name\n");
514 }
515
516 nfsrvd_pool->sp_minthreads = args->minthreads;
517 nfsrvd_pool->sp_maxthreads = args->maxthreads;
518
519 svc_run(nfsrvd_pool);
520
521 if (principal[0] != '\0') {
522 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
523 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
524 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
525 }
526
527 NFSD_LOCK();
528 newnfs_numnfsd--;
529 nfsrvd_init(1);
530 PROC_LOCK(p);
531 p->p_flag2 &= ~P2_AST_SU;
532 PROC_UNLOCK(p);
533 }
534 NFSD_UNLOCK();
535
536 out:
537 NFSEXITCODE(error);
538 return (error);
539 }
540
541 /*
542 * Initialize the data structures for the server.
543 * Handshake with any new nfsds starting up to avoid any chance of
544 * corruption.
545 */
546 void
547 nfsrvd_init(int terminating)
548 {
549
550 NFSD_LOCK_ASSERT();
551
552 if (terminating) {
553 nfsd_master_proc = NULL;
554 NFSD_UNLOCK();
555 nfsrv_freeallbackchannel_xprts();
556 svcpool_destroy(nfsrvd_pool);
557 nfsrvd_pool = NULL;
558 NFSD_LOCK();
559 }
560
561 NFSD_UNLOCK();
562
563 nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
564 nfsrvd_pool->sp_rcache = NULL;
565 nfsrvd_pool->sp_assign = fhanew_assign;
566 nfsrvd_pool->sp_done = fha_nd_complete;
567
568 NFSD_LOCK();
569 }
570
571