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