1 /* $NetBSD: nfs_nfsdkrpc.c,v 1.2 2014/03/25 16:30:28 christos 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 249596 2013-04-17 22:42:43Z ken "); */ 37 __RCSID("$NetBSD: nfs_nfsdkrpc.c,v 1.2 2014/03/25 16:30:28 christos 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_RW, 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_RW, 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_RW, 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, struct socket *, 103 u_int64_t, 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 svcerr_noproc(rqst); 124 svc_freereq(rqst); 125 goto out; 126 } 127 nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc]; 128 nd.nd_flag = ND_NFSV2; 129 } else if (rqst->rq_vers == NFS_VER3) { 130 if (rqst->rq_proc >= NFS_V3NPROCS) { 131 svcerr_noproc(rqst); 132 svc_freereq(rqst); 133 goto out; 134 } 135 nd.nd_procnum = rqst->rq_proc; 136 nd.nd_flag = ND_NFSV3; 137 } else { 138 if (rqst->rq_proc != NFSPROC_NULL && 139 rqst->rq_proc != NFSV4PROC_COMPOUND) { 140 svcerr_noproc(rqst); 141 svc_freereq(rqst); 142 goto out; 143 } 144 nd.nd_procnum = rqst->rq_proc; 145 nd.nd_flag = ND_NFSV4; 146 } 147 148 /* 149 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 - 150 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP 151 * mounts. 152 */ 153 nd.nd_mrep = rqst->rq_args; 154 rqst->rq_args = NULL; 155 newnfs_realign(&nd.nd_mrep, M_WAITOK); 156 nd.nd_md = nd.nd_mrep; 157 nd.nd_dpos = mtod(nd.nd_md, caddr_t); 158 nd.nd_nam = svc_getrpccaller(rqst); 159 nd.nd_nam2 = rqst->rq_addr; 160 nd.nd_mreq = NULL; 161 nd.nd_cred = NULL; 162 163 if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) { 164 /* Check if source port is privileged */ 165 u_short port; 166 struct sockaddr *nam = nd.nd_nam; 167 struct sockaddr_in *sin; 168 169 sin = (struct sockaddr_in *)nam; 170 /* 171 * INET/INET6 - same code: 172 * sin_port and sin6_port are at same offset 173 */ 174 port = ntohs(sin->sin_port); 175 if (port >= IPPORT_RESERVED && 176 nd.nd_procnum != NFSPROC_NULL) { 177 #ifdef INET6 178 char b6[INET6_ADDRSTRLEN]; 179 #if defined(KLD_MODULE) 180 /* Do not use ip6_sprintf: the nfs module should work without INET6. */ 181 #define ip6_sprintf(buf, a) \ 182 (snprintf((buf), sizeof(buf), "%x:%x:%x:%x:%x:%x:%x:%x", \ 183 (a)->s6_addr16[0], (a)->s6_addr16[1], \ 184 (a)->s6_addr16[2], (a)->s6_addr16[3], \ 185 (a)->s6_addr16[4], (a)->s6_addr16[5], \ 186 (a)->s6_addr16[6], (a)->s6_addr16[7]), \ 187 (buf)) 188 #endif 189 #endif 190 printf("NFS request from unprivileged port (%s:%d)\n", 191 #ifdef INET6 192 sin->sin_family == AF_INET6 ? 193 ip6_sprintf(b6, &satosin6(sin)->sin6_addr) : 194 #if defined(KLD_MODULE) 195 #undef ip6_sprintf 196 #endif 197 #endif 198 inet_ntoa(sin->sin_addr), port); 199 svcerr_weakauth(rqst); 200 svc_freereq(rqst); 201 m_freem(nd.nd_mrep); 202 goto out; 203 } 204 } 205 206 if (nd.nd_procnum != NFSPROC_NULL) { 207 if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) { 208 svcerr_weakauth(rqst); 209 svc_freereq(rqst); 210 m_freem(nd.nd_mrep); 211 goto out; 212 } 213 214 /* Set the flag based on credflavor */ 215 if (credflavor == RPCSEC_GSS_KRB5) { 216 nd.nd_flag |= ND_GSS; 217 } else if (credflavor == RPCSEC_GSS_KRB5I) { 218 nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY); 219 } else if (credflavor == RPCSEC_GSS_KRB5P) { 220 nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY); 221 } else if (credflavor != AUTH_SYS) { 222 svcerr_weakauth(rqst); 223 svc_freereq(rqst); 224 m_freem(nd.nd_mrep); 225 goto out; 226 } 227 228 #ifdef MAC 229 mac_cred_associate_nfsd(nd.nd_cred); 230 #endif 231 /* 232 * Get a refcnt (shared lock) on nfsd_suspend_lock. 233 * NFSSVC_SUSPENDNFSD will take an exclusive lock on 234 * nfsd_suspend_lock to suspend these threads. 235 * This must be done here, before the check of 236 * nfsv4root exports by nfsvno_v4rootexport(). 237 */ 238 NFSLOCKV4ROOTMUTEX(); 239 nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, 240 NULL); 241 NFSUNLOCKV4ROOTMUTEX(); 242 243 if ((nd.nd_flag & ND_NFSV4) != 0) { 244 nd.nd_repstat = nfsvno_v4rootexport(&nd); 245 if (nd.nd_repstat != 0) { 246 NFSLOCKV4ROOTMUTEX(); 247 nfsv4_relref(&nfsd_suspend_lock); 248 NFSUNLOCKV4ROOTMUTEX(); 249 svcerr_weakauth(rqst); 250 svc_freereq(rqst); 251 m_freem(nd.nd_mrep); 252 goto out; 253 } 254 } 255 256 cacherep = nfs_proc(&nd, rqst->rq_xid, xprt->xp_socket, 257 xprt->xp_sockref, &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, xprt->xp_socket, 0); 294 svc_freereq(rqst); 295 296 out: 297 NFSEXITCODE(0); 298 } 299 300 /* 301 * Check the cache and, optionally, do the RPC. 302 * Return the appropriate cache response. 303 */ 304 static int 305 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, struct socket *so, 306 u_int64_t sockref, struct nfsrvcache **rpp) 307 { 308 struct thread *td = curthread; 309 int cacherep = RC_DOIT, isdgram; 310 311 *rpp = NULL; 312 if (nd->nd_nam2 == NULL) { 313 nd->nd_flag |= ND_STREAMSOCK; 314 isdgram = 0; 315 } else { 316 isdgram = 1; 317 } 318 319 /* 320 * Two cases: 321 * 1 - For NFSv2 over UDP, if we are near our malloc/mget 322 * limit, just drop the request. There is no 323 * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the 324 * client will timeout/retry over UDP in a little while. 325 * 2 - nd_repstat == 0 && nd_mreq == NULL, which 326 * means a normal nfs rpc, so check the cache 327 */ 328 if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL && 329 nfsrv_mallocmget_limit()) { 330 cacherep = RC_DROPIT; 331 } else { 332 /* 333 * For NFSv3, play it safe and assume that the client is 334 * doing retries on the same TCP connection. 335 */ 336 if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) == 337 ND_STREAMSOCK) 338 nd->nd_flag |= ND_SAMETCPCONN; 339 nd->nd_retxid = xid; 340 nd->nd_tcpconntime = NFSD_MONOSEC; 341 nd->nd_sockref = sockref; 342 cacherep = nfsrvd_getcache(nd, so); 343 } 344 345 /* 346 * Handle the request. There are three cases. 347 * RC_DOIT - do the RPC 348 * RC_REPLY - return the reply already created 349 * RC_DROPIT - just throw the request away 350 */ 351 if (cacherep == RC_DOIT) { 352 nfsrvd_dorpc(nd, isdgram, td); 353 if (nd->nd_repstat == NFSERR_DONTREPLY) 354 cacherep = RC_DROPIT; 355 else 356 cacherep = RC_REPLY; 357 *rpp = nfsrvd_updatecache(nd, so); 358 } 359 360 NFSEXITCODE2(0, nd); 361 return (cacherep); 362 } 363 364 /* 365 * Adds a socket to the list for servicing by nfsds. 366 */ 367 int 368 nfsrvd_addsock(struct file *fp) 369 { 370 int siz; 371 struct socket *so; 372 int error = 0; 373 SVCXPRT *xprt; 374 static u_int64_t sockref = 0; 375 376 so = fp->f_data; 377 378 siz = sb_max_adj; 379 error = soreserve(so, siz, siz); 380 if (error) 381 goto out; 382 383 /* 384 * Steal the socket from userland so that it doesn't close 385 * unexpectedly. 386 */ 387 if (so->so_type == SOCK_DGRAM) 388 xprt = svc_dg_create(nfsrvd_pool, so, 0, 0); 389 else 390 xprt = svc_vc_create(nfsrvd_pool, so, 0, 0); 391 if (xprt) { 392 fp->f_ops = &badfileops; 393 fp->f_data = NULL; 394 xprt->xp_sockref = ++sockref; 395 if (nfs_minvers == NFS_VER2) 396 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, 397 NULL); 398 if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3) 399 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, 400 NULL); 401 if (nfs_maxvers >= NFS_VER4) 402 svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program, 403 NULL); 404 SVC_RELEASE(xprt); 405 } 406 407 out: 408 NFSEXITCODE(error); 409 return (error); 410 } 411 412 /* 413 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 414 * until it is killed by a signal. 415 */ 416 int 417 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args) 418 { 419 char principal[MAXHOSTNAMELEN + 5]; 420 int error = 0; 421 bool_t ret2, ret3, ret4; 422 423 error = copyinstr(args->principal, principal, sizeof (principal), 424 NULL); 425 if (error) 426 goto out; 427 428 /* 429 * Only the first nfsd actually does any work. The RPC code 430 * adds threads to it as needed. Any extra processes offered 431 * by nfsd just exit. If nfsd is new enough, it will call us 432 * once with a structure that specifies how many threads to 433 * use. 434 */ 435 NFSD_LOCK(); 436 if (newnfs_numnfsd == 0) { 437 newnfs_numnfsd++; 438 439 NFSD_UNLOCK(); 440 441 /* An empty string implies AUTH_SYS only. */ 442 if (principal[0] != '\0') { 443 ret2 = rpc_gss_set_svc_name_call(principal, 444 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2); 445 ret3 = rpc_gss_set_svc_name_call(principal, 446 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3); 447 ret4 = rpc_gss_set_svc_name_call(principal, 448 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4); 449 450 if (!ret2 || !ret3 || !ret4) 451 printf("nfsd: can't register svc name\n"); 452 } 453 454 nfsrvd_pool->sp_minthreads = args->minthreads; 455 nfsrvd_pool->sp_maxthreads = args->maxthreads; 456 457 svc_run(nfsrvd_pool); 458 459 if (principal[0] != '\0') { 460 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2); 461 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3); 462 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4); 463 } 464 465 NFSD_LOCK(); 466 newnfs_numnfsd--; 467 nfsrvd_init(1); 468 } 469 NFSD_UNLOCK(); 470 471 out: 472 NFSEXITCODE(error); 473 return (error); 474 } 475 476 /* 477 * Initialize the data structures for the server. 478 * Handshake with any new nfsds starting up to avoid any chance of 479 * corruption. 480 */ 481 void 482 nfsrvd_init(int terminating) 483 { 484 485 NFSD_LOCK_ASSERT(); 486 487 if (terminating) { 488 nfsd_master_proc = NULL; 489 NFSD_UNLOCK(); 490 svcpool_destroy(nfsrvd_pool); 491 nfsrvd_pool = NULL; 492 NFSD_LOCK(); 493 } 494 495 NFSD_UNLOCK(); 496 497 nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd)); 498 nfsrvd_pool->sp_rcache = NULL; 499 nfsrvd_pool->sp_assign = fhanew_assign; 500 nfsrvd_pool->sp_done = fha_nd_complete; 501 502 NFSD_LOCK(); 503 } 504 505