Print this page
Caution with use after exi_rele()
Be far more judicious in the use of curzone-using macros.
(Merge and extra asserts by danmcd.)
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/nfs/nfs_server.c
+++ new/usr/src/uts/common/fs/nfs/nfs_server.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
24 24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 25 * Copyright (c) 2017 Joyent Inc
26 26 * Copyright 2019 Nexenta by DDN, Inc.
27 27 */
28 28
29 29 /*
30 30 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
31 31 * All rights reserved.
32 32 * Use is subject to license terms.
33 33 */
34 34
35 35 /*
36 36 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
37 37 * Copyright (c) 2013 by Delphix. All rights reserved.
38 38 * Copyright 2018 Nexenta Systems, Inc.
39 39 * Copyright (c) 2017 Joyent Inc
40 40 */
41 41
42 42 #include <sys/param.h>
43 43 #include <sys/types.h>
44 44 #include <sys/systm.h>
45 45 #include <sys/cred.h>
46 46 #include <sys/proc.h>
47 47 #include <sys/user.h>
48 48 #include <sys/buf.h>
49 49 #include <sys/vfs.h>
50 50 #include <sys/vnode.h>
51 51 #include <sys/pathname.h>
52 52 #include <sys/uio.h>
53 53 #include <sys/file.h>
54 54 #include <sys/stat.h>
55 55 #include <sys/errno.h>
56 56 #include <sys/socket.h>
57 57 #include <sys/sysmacros.h>
58 58 #include <sys/siginfo.h>
59 59 #include <sys/tiuser.h>
60 60 #include <sys/statvfs.h>
61 61 #include <sys/stream.h>
62 62 #include <sys/strsun.h>
63 63 #include <sys/strsubr.h>
64 64 #include <sys/stropts.h>
65 65 #include <sys/timod.h>
66 66 #include <sys/t_kuser.h>
67 67 #include <sys/kmem.h>
68 68 #include <sys/kstat.h>
69 69 #include <sys/dirent.h>
70 70 #include <sys/cmn_err.h>
71 71 #include <sys/debug.h>
72 72 #include <sys/unistd.h>
73 73 #include <sys/vtrace.h>
74 74 #include <sys/mode.h>
75 75 #include <sys/acl.h>
76 76 #include <sys/sdt.h>
77 77 #include <sys/debug.h>
78 78
79 79 #include <rpc/types.h>
80 80 #include <rpc/auth.h>
81 81 #include <rpc/auth_unix.h>
82 82 #include <rpc/auth_des.h>
83 83 #include <rpc/svc.h>
84 84 #include <rpc/xdr.h>
85 85 #include <rpc/rpc_rdma.h>
86 86
87 87 #include <nfs/nfs.h>
88 88 #include <nfs/export.h>
89 89 #include <nfs/nfssys.h>
90 90 #include <nfs/nfs_clnt.h>
91 91 #include <nfs/nfs_acl.h>
92 92 #include <nfs/nfs_log.h>
93 93 #include <nfs/lm.h>
94 94 #include <nfs/nfs_dispatch.h>
95 95 #include <nfs/nfs4_drc.h>
96 96
97 97 #include <sys/modctl.h>
98 98 #include <sys/cladm.h>
99 99 #include <sys/clconf.h>
100 100
101 101 #include <sys/tsol/label.h>
102 102
103 103 #define MAXHOST 32
104 104 const char *kinet_ntop6(uchar_t *, char *, size_t);
105 105
106 106 /*
107 107 * Module linkage information.
108 108 */
109 109
110 110 static struct modlmisc modlmisc = {
111 111 &mod_miscops, "NFS server module"
112 112 };
113 113
114 114 static struct modlinkage modlinkage = {
115 115 MODREV_1, (void *)&modlmisc, NULL
116 116 };
117 117
118 118 zone_key_t nfssrv_zone_key;
119 119 list_t nfssrv_globals_list;
120 120 krwlock_t nfssrv_globals_rwl;
121 121
122 122 kmem_cache_t *nfs_xuio_cache;
123 123 int nfs_loaned_buffers = 0;
124 124
125 125 int
126 126 _init(void)
127 127 {
128 128 int status;
129 129
130 130 nfs_srvinit();
131 131
132 132 status = mod_install((struct modlinkage *)&modlinkage);
133 133 if (status != 0) {
134 134 /*
135 135 * Could not load module, cleanup previous
136 136 * initialization work.
137 137 */
138 138 nfs_srvfini();
139 139
140 140 return (status);
141 141 }
142 142
143 143 /*
144 144 * Initialise some placeholders for nfssys() calls. These have
145 145 * to be declared by the nfs module, since that handles nfssys()
146 146 * calls - also used by NFS clients - but are provided by this
147 147 * nfssrv module. These also then serve as confirmation to the
148 148 * relevant code in nfs that nfssrv has been loaded, as they're
149 149 * initially NULL.
150 150 */
151 151 nfs_srv_quiesce_func = nfs_srv_quiesce_all;
152 152 nfs_srv_dss_func = rfs4_dss_setpaths;
153 153
154 154 /* setup DSS paths here; must be done before initial server startup */
155 155 rfs4_dss_paths = rfs4_dss_oldpaths = NULL;
156 156
157 157 /* initialize the copy reduction caches */
158 158
159 159 nfs_xuio_cache = kmem_cache_create("nfs_xuio_cache",
160 160 sizeof (nfs_xuio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
161 161
162 162 return (status);
163 163 }
164 164
165 165 int
166 166 _fini()
167 167 {
168 168 return (EBUSY);
169 169 }
170 170
171 171 int
172 172 _info(struct modinfo *modinfop)
173 173 {
174 174 return (mod_info(&modlinkage, modinfop));
175 175 }
176 176
177 177 /*
178 178 * PUBLICFH_CHECK() checks if the dispatch routine supports
179 179 * RPC_PUBLICFH_OK, if the filesystem is exported public, and if the
180 180 * incoming request is using the public filehandle. The check duplicates
181 181 * the exportmatch() call done in checkexport(), and we should consider
182 182 * modifying those routines to avoid the duplication. For now, we optimize
183 183 * by calling exportmatch() only after checking that the dispatch routine
184 184 * supports RPC_PUBLICFH_OK, and if the filesystem is explicitly exported
185 185 * public (i.e., not the placeholder).
186 186 */
187 187 #define PUBLICFH_CHECK(ne, disp, exi, fsid, xfid) \
188 188 ((disp->dis_flags & RPC_PUBLICFH_OK) && \
189 189 ((exi->exi_export.ex_flags & EX_PUBLIC) || \
190 190 (exi == ne->exi_public && exportmatch(ne->exi_root, \
191 191 fsid, xfid))))
192 192
193 193 static void nfs_srv_shutdown_all(int);
194 194 static void rfs4_server_start(nfs_globals_t *, int);
195 195 static void nullfree(void);
196 196 static void rfs_dispatch(struct svc_req *, SVCXPRT *);
197 197 static void acl_dispatch(struct svc_req *, SVCXPRT *);
198 198 static void common_dispatch(struct svc_req *, SVCXPRT *,
199 199 rpcvers_t, rpcvers_t, char *,
200 200 struct rpc_disptable *);
201 201 static int checkauth(struct exportinfo *, struct svc_req *, cred_t *, int,
202 202 bool_t, bool_t *);
203 203 static char *client_name(struct svc_req *req);
204 204 static char *client_addr(struct svc_req *req, char *buf);
205 205 extern int sec_svc_getcred(struct svc_req *, cred_t *cr, char **, int *);
206 206 extern bool_t sec_svc_inrootlist(int, caddr_t, int, caddr_t *);
207 207 static void *nfs_server_zone_init(zoneid_t);
208 208 static void nfs_server_zone_fini(zoneid_t, void *);
209 209 static void nfs_server_zone_shutdown(zoneid_t, void *);
210 210
211 211 #define NFSLOG_COPY_NETBUF(exi, xprt, nb) { \
212 212 (nb)->maxlen = (xprt)->xp_rtaddr.maxlen; \
213 213 (nb)->len = (xprt)->xp_rtaddr.len; \
214 214 (nb)->buf = kmem_alloc((nb)->len, KM_SLEEP); \
215 215 bcopy((xprt)->xp_rtaddr.buf, (nb)->buf, (nb)->len); \
216 216 }
217 217
218 218 /*
219 219 * Public Filehandle common nfs routines
220 220 */
221 221 static int MCLpath(char **);
222 222 static void URLparse(char *);
223 223
224 224 /*
225 225 * NFS callout table.
226 226 * This table is used by svc_getreq() to dispatch a request with
227 227 * a given prog/vers pair to an appropriate service provider
228 228 * dispatch routine.
229 229 *
230 230 * NOTE: ordering is relied upon below when resetting the version min/max
231 231 * for NFS_PROGRAM. Careful, if this is ever changed.
232 232 */
233 233 static SVC_CALLOUT __nfs_sc_clts[] = {
234 234 { NFS_PROGRAM, NFS_VERSMIN, NFS_VERSMAX, rfs_dispatch },
235 235 { NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX, acl_dispatch }
236 236 };
237 237
238 238 static SVC_CALLOUT_TABLE nfs_sct_clts = {
239 239 sizeof (__nfs_sc_clts) / sizeof (__nfs_sc_clts[0]), FALSE,
240 240 __nfs_sc_clts
241 241 };
242 242
243 243 static SVC_CALLOUT __nfs_sc_cots[] = {
244 244 { NFS_PROGRAM, NFS_VERSMIN, NFS_VERSMAX, rfs_dispatch },
245 245 { NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX, acl_dispatch }
246 246 };
247 247
248 248 static SVC_CALLOUT_TABLE nfs_sct_cots = {
249 249 sizeof (__nfs_sc_cots) / sizeof (__nfs_sc_cots[0]), FALSE, __nfs_sc_cots
250 250 };
251 251
252 252 static SVC_CALLOUT __nfs_sc_rdma[] = {
253 253 { NFS_PROGRAM, NFS_VERSMIN, NFS_VERSMAX, rfs_dispatch },
254 254 { NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX, acl_dispatch }
255 255 };
256 256
257 257 static SVC_CALLOUT_TABLE nfs_sct_rdma = {
258 258 sizeof (__nfs_sc_rdma) / sizeof (__nfs_sc_rdma[0]), FALSE, __nfs_sc_rdma
259 259 };
260 260
261 261 /*
262 262 * DSS: distributed stable storage
263 263 * lists of all DSS paths: current, and before last warmstart
264 264 */
265 265 nvlist_t *rfs4_dss_paths, *rfs4_dss_oldpaths;
266 266
267 267 int rfs4_dispatch(struct rpcdisp *, struct svc_req *, SVCXPRT *, char *);
268 268 bool_t rfs4_minorvers_mismatch(struct svc_req *, SVCXPRT *, void *);
269 269
270 270 nfs_globals_t *
271 271 nfs_srv_getzg(void)
272 272 {
273 273 nfs_globals_t *ng;
274 274
275 275 ng = zone_getspecific(nfssrv_zone_key, curzone);
276 276 return (ng);
277 277 }
278 278
279 279 /*
280 280 * Will be called at the point the server pool is being unregistered
281 281 * from the pool list. From that point onwards, the pool is waiting
282 282 * to be drained and as such the server state is stale and pertains
283 283 * to the old instantiation of the NFS server pool.
284 284 */
285 285 void
286 286 nfs_srv_offline(void)
287 287 {
288 288 nfs_globals_t *ng;
289 289
290 290 ng = nfs_srv_getzg();
291 291
292 292 mutex_enter(&ng->nfs_server_upordown_lock);
293 293 if (ng->nfs_server_upordown == NFS_SERVER_RUNNING) {
294 294 ng->nfs_server_upordown = NFS_SERVER_OFFLINE;
295 295 }
296 296 mutex_exit(&ng->nfs_server_upordown_lock);
297 297 }
298 298
299 299 /*
300 300 * Will be called at the point the server pool is being destroyed so
301 301 * all transports have been closed and no service threads are in
302 302 * existence.
303 303 *
304 304 * If we quiesce the server, we're shutting it down without destroying the
305 305 * server state. This allows it to warm start subsequently.
306 306 */
307 307 void
308 308 nfs_srv_stop_all(void)
309 309 {
310 310 int quiesce = 0;
311 311 nfs_srv_shutdown_all(quiesce);
312 312 }
313 313
314 314 /*
315 315 * This alternative shutdown routine can be requested via nfssys()
316 316 */
317 317 void
318 318 nfs_srv_quiesce_all(void)
319 319 {
320 320 int quiesce = 1;
321 321 nfs_srv_shutdown_all(quiesce);
322 322 }
323 323
324 324 static void
325 325 nfs_srv_shutdown_all(int quiesce)
326 326 {
327 327 nfs_globals_t *ng = nfs_srv_getzg();
328 328
329 329 mutex_enter(&ng->nfs_server_upordown_lock);
330 330 if (quiesce) {
331 331 if (ng->nfs_server_upordown == NFS_SERVER_RUNNING ||
332 332 ng->nfs_server_upordown == NFS_SERVER_OFFLINE) {
333 333 ng->nfs_server_upordown = NFS_SERVER_QUIESCED;
334 334 cv_signal(&ng->nfs_server_upordown_cv);
335 335
336 336 /* reset DSS state */
337 337 rfs4_dss_numnewpaths = 0;
338 338 rfs4_dss_newpaths = NULL;
339 339
340 340 cmn_err(CE_NOTE, "nfs_server: server is now quiesced; "
341 341 "NFSv4 state has been preserved");
342 342 }
343 343 } else {
344 344 if (ng->nfs_server_upordown == NFS_SERVER_OFFLINE) {
345 345 ng->nfs_server_upordown = NFS_SERVER_STOPPING;
346 346 mutex_exit(&ng->nfs_server_upordown_lock);
347 347 rfs4_state_zone_fini();
348 348 rfs4_fini_drc();
349 349 mutex_enter(&ng->nfs_server_upordown_lock);
350 350 ng->nfs_server_upordown = NFS_SERVER_STOPPED;
351 351
352 352 /* reset DSS state */
353 353 rfs4_dss_numnewpaths = 0;
354 354 rfs4_dss_newpaths = NULL;
355 355
356 356 cv_signal(&ng->nfs_server_upordown_cv);
357 357 }
358 358 }
359 359 mutex_exit(&ng->nfs_server_upordown_lock);
360 360 }
361 361
362 362 static int
363 363 nfs_srv_set_sc_versions(struct file *fp, SVC_CALLOUT_TABLE **sctpp,
364 364 rpcvers_t versmin, rpcvers_t versmax)
365 365 {
366 366 struct strioctl strioc;
367 367 struct T_info_ack tinfo;
368 368 int error, retval;
369 369
370 370 /*
371 371 * Find out what type of transport this is.
372 372 */
373 373 strioc.ic_cmd = TI_GETINFO;
374 374 strioc.ic_timout = -1;
375 375 strioc.ic_len = sizeof (tinfo);
376 376 strioc.ic_dp = (char *)&tinfo;
377 377 tinfo.PRIM_type = T_INFO_REQ;
378 378
379 379 error = strioctl(fp->f_vnode, I_STR, (intptr_t)&strioc, 0, K_TO_K,
380 380 CRED(), &retval);
381 381 if (error || retval)
382 382 return (error);
383 383
384 384 /*
385 385 * Based on our query of the transport type...
386 386 *
387 387 * Reset the min/max versions based on the caller's request
388 388 * NOTE: This assumes that NFS_PROGRAM is first in the array!!
389 389 * And the second entry is the NFS_ACL_PROGRAM.
390 390 */
391 391 switch (tinfo.SERV_type) {
392 392 case T_CLTS:
393 393 if (versmax == NFS_V4)
394 394 return (EINVAL);
395 395 __nfs_sc_clts[0].sc_versmin = versmin;
396 396 __nfs_sc_clts[0].sc_versmax = versmax;
397 397 __nfs_sc_clts[1].sc_versmin = versmin;
398 398 __nfs_sc_clts[1].sc_versmax = versmax;
399 399 *sctpp = &nfs_sct_clts;
400 400 break;
401 401 case T_COTS:
402 402 case T_COTS_ORD:
403 403 __nfs_sc_cots[0].sc_versmin = versmin;
404 404 __nfs_sc_cots[0].sc_versmax = versmax;
405 405 /* For the NFS_ACL program, check the max version */
406 406 if (versmax > NFS_ACL_VERSMAX)
407 407 versmax = NFS_ACL_VERSMAX;
408 408 __nfs_sc_cots[1].sc_versmin = versmin;
409 409 __nfs_sc_cots[1].sc_versmax = versmax;
410 410 *sctpp = &nfs_sct_cots;
411 411 break;
412 412 default:
413 413 error = EINVAL;
414 414 }
415 415
416 416 return (error);
417 417 }
418 418
419 419 /*
420 420 * NFS Server system call.
421 421 * Does all of the work of running a NFS server.
422 422 * uap->fd is the fd of an open transport provider
423 423 */
424 424 int
425 425 nfs_svc(struct nfs_svc_args *arg, model_t model)
426 426 {
427 427 nfs_globals_t *ng;
428 428 file_t *fp;
429 429 SVCMASTERXPRT *xprt;
430 430 int error;
431 431 int readsize;
432 432 char buf[KNC_STRSIZE];
433 433 size_t len;
434 434 STRUCT_HANDLE(nfs_svc_args, uap);
435 435 struct netbuf addrmask;
436 436 SVC_CALLOUT_TABLE *sctp = NULL;
437 437
438 438 #ifdef lint
439 439 model = model; /* STRUCT macros don't always refer to it */
440 440 #endif
441 441
442 442 ng = nfs_srv_getzg();
443 443 STRUCT_SET_HANDLE(uap, model, arg);
444 444
445 445 /* Check privileges in nfssys() */
446 446
447 447 if ((fp = getf(STRUCT_FGET(uap, fd))) == NULL)
448 448 return (EBADF);
449 449
450 450 /* Setup global file handle in nfs_export */
451 451 if ((error = nfs_export_get_rootfh(ng)) != 0)
452 452 return (error);
453 453
454 454 /*
455 455 * Set read buffer size to rsize
456 456 * and add room for RPC headers.
457 457 */
458 458 readsize = nfs3tsize() + (RPC_MAXDATASIZE - NFS_MAXDATA);
459 459 if (readsize < RPC_MAXDATASIZE)
460 460 readsize = RPC_MAXDATASIZE;
461 461
462 462 error = copyinstr((const char *)STRUCT_FGETP(uap, netid), buf,
463 463 KNC_STRSIZE, &len);
464 464 if (error) {
465 465 releasef(STRUCT_FGET(uap, fd));
466 466 return (error);
467 467 }
468 468
469 469 addrmask.len = STRUCT_FGET(uap, addrmask.len);
470 470 addrmask.maxlen = STRUCT_FGET(uap, addrmask.maxlen);
471 471 addrmask.buf = kmem_alloc(addrmask.maxlen, KM_SLEEP);
472 472 error = copyin(STRUCT_FGETP(uap, addrmask.buf), addrmask.buf,
473 473 addrmask.len);
474 474 if (error) {
475 475 releasef(STRUCT_FGET(uap, fd));
476 476 kmem_free(addrmask.buf, addrmask.maxlen);
477 477 return (error);
478 478 }
479 479
480 480 ng->nfs_versmin = STRUCT_FGET(uap, versmin);
481 481 ng->nfs_versmax = STRUCT_FGET(uap, versmax);
482 482
483 483 /* Double check the vers min/max ranges */
484 484 if ((ng->nfs_versmin > ng->nfs_versmax) ||
485 485 (ng->nfs_versmin < NFS_VERSMIN) ||
486 486 (ng->nfs_versmax > NFS_VERSMAX)) {
487 487 ng->nfs_versmin = NFS_VERSMIN_DEFAULT;
488 488 ng->nfs_versmax = NFS_VERSMAX_DEFAULT;
489 489 }
490 490
491 491 if (error = nfs_srv_set_sc_versions(fp, &sctp, ng->nfs_versmin,
492 492 ng->nfs_versmax)) {
493 493 releasef(STRUCT_FGET(uap, fd));
494 494 kmem_free(addrmask.buf, addrmask.maxlen);
495 495 return (error);
496 496 }
497 497
498 498 /* Initialize nfsv4 server */
499 499 if (ng->nfs_versmax == (rpcvers_t)NFS_V4)
500 500 rfs4_server_start(ng, STRUCT_FGET(uap, delegation));
501 501
502 502 /* Create a transport handle. */
503 503 error = svc_tli_kcreate(fp, readsize, buf, &addrmask, &xprt,
504 504 sctp, NULL, NFS_SVCPOOL_ID, TRUE);
505 505
506 506 if (error)
507 507 kmem_free(addrmask.buf, addrmask.maxlen);
508 508
509 509 releasef(STRUCT_FGET(uap, fd));
510 510
511 511 /* HA-NFSv4: save the cluster nodeid */
512 512 if (cluster_bootflags & CLUSTER_BOOTED)
513 513 lm_global_nlmid = clconf_get_nodeid();
514 514
515 515 return (error);
516 516 }
517 517
518 518 static void
519 519 rfs4_server_start(nfs_globals_t *ng, int nfs4_srv_delegation)
520 520 {
521 521 /*
522 522 * Determine if the server has previously been "started" and
523 523 * if not, do the per instance initialization
524 524 */
525 525 mutex_enter(&ng->nfs_server_upordown_lock);
526 526
527 527 if (ng->nfs_server_upordown != NFS_SERVER_RUNNING) {
528 528 /* Do we need to stop and wait on the previous server? */
529 529 while (ng->nfs_server_upordown == NFS_SERVER_STOPPING ||
530 530 ng->nfs_server_upordown == NFS_SERVER_OFFLINE)
531 531 cv_wait(&ng->nfs_server_upordown_cv,
532 532 &ng->nfs_server_upordown_lock);
533 533
534 534 if (ng->nfs_server_upordown != NFS_SERVER_RUNNING) {
535 535 (void) svc_pool_control(NFS_SVCPOOL_ID,
536 536 SVCPSET_UNREGISTER_PROC, (void *)&nfs_srv_offline);
537 537 (void) svc_pool_control(NFS_SVCPOOL_ID,
538 538 SVCPSET_SHUTDOWN_PROC, (void *)&nfs_srv_stop_all);
539 539
540 540 rfs4_do_server_start(ng->nfs_server_upordown,
541 541 nfs4_srv_delegation,
542 542 cluster_bootflags & CLUSTER_BOOTED);
543 543
544 544 ng->nfs_server_upordown = NFS_SERVER_RUNNING;
545 545 }
546 546 cv_signal(&ng->nfs_server_upordown_cv);
547 547 }
548 548 mutex_exit(&ng->nfs_server_upordown_lock);
549 549 }
550 550
551 551 /*
552 552 * If RDMA device available,
553 553 * start RDMA listener.
554 554 */
555 555 int
556 556 rdma_start(struct rdma_svc_args *rsa)
557 557 {
558 558 nfs_globals_t *ng;
559 559 int error;
560 560 rdma_xprt_group_t started_rdma_xprts;
561 561 rdma_stat stat;
562 562 int svc_state = 0;
563 563
564 564 /* Double check the vers min/max ranges */
565 565 if ((rsa->nfs_versmin > rsa->nfs_versmax) ||
566 566 (rsa->nfs_versmin < NFS_VERSMIN) ||
567 567 (rsa->nfs_versmax > NFS_VERSMAX)) {
568 568 rsa->nfs_versmin = NFS_VERSMIN_DEFAULT;
569 569 rsa->nfs_versmax = NFS_VERSMAX_DEFAULT;
570 570 }
571 571
572 572 ng = nfs_srv_getzg();
573 573 ng->nfs_versmin = rsa->nfs_versmin;
574 574 ng->nfs_versmax = rsa->nfs_versmax;
575 575
576 576 /* Set the versions in the callout table */
577 577 __nfs_sc_rdma[0].sc_versmin = rsa->nfs_versmin;
578 578 __nfs_sc_rdma[0].sc_versmax = rsa->nfs_versmax;
579 579 /* For the NFS_ACL program, check the max version */
580 580 __nfs_sc_rdma[1].sc_versmin = rsa->nfs_versmin;
581 581 if (rsa->nfs_versmax > NFS_ACL_VERSMAX)
582 582 __nfs_sc_rdma[1].sc_versmax = NFS_ACL_VERSMAX;
583 583 else
584 584 __nfs_sc_rdma[1].sc_versmax = rsa->nfs_versmax;
585 585
586 586 /* Initialize nfsv4 server */
587 587 if (rsa->nfs_versmax == (rpcvers_t)NFS_V4)
588 588 rfs4_server_start(ng, rsa->delegation);
589 589
590 590 started_rdma_xprts.rtg_count = 0;
591 591 started_rdma_xprts.rtg_listhead = NULL;
592 592 started_rdma_xprts.rtg_poolid = rsa->poolid;
593 593
594 594 restart:
595 595 error = svc_rdma_kcreate(rsa->netid, &nfs_sct_rdma, rsa->poolid,
596 596 &started_rdma_xprts);
597 597
598 598 svc_state = !error;
599 599
600 600 while (!error) {
601 601
602 602 /*
603 603 * wait till either interrupted by a signal on
604 604 * nfs service stop/restart or signalled by a
605 605 * rdma attach/detatch.
606 606 */
607 607
608 608 stat = rdma_kwait();
609 609
610 610 /*
611 611 * stop services if running -- either on a HCA detach event
612 612 * or if the nfs service is stopped/restarted.
613 613 */
614 614
615 615 if ((stat == RDMA_HCA_DETACH || stat == RDMA_INTR) &&
616 616 svc_state) {
617 617 rdma_stop(&started_rdma_xprts);
618 618 svc_state = 0;
619 619 }
620 620
621 621 /*
622 622 * nfs service stop/restart, break out of the
623 623 * wait loop and return;
624 624 */
625 625 if (stat == RDMA_INTR)
626 626 return (0);
627 627
628 628 /*
629 629 * restart stopped services on a HCA attach event
630 630 * (if not already running)
631 631 */
632 632
633 633 if ((stat == RDMA_HCA_ATTACH) && (svc_state == 0))
634 634 goto restart;
635 635
636 636 /*
637 637 * loop until a nfs service stop/restart
638 638 */
639 639 }
640 640
641 641 return (error);
642 642 }
643 643
644 644 /* ARGSUSED */
645 645 void
646 646 rpc_null(caddr_t *argp, caddr_t *resp, struct exportinfo *exi,
647 647 struct svc_req *req, cred_t *cr, bool_t ro)
648 648 {
649 649 }
650 650
651 651 /* ARGSUSED */
652 652 void
653 653 rpc_null_v3(caddr_t *argp, caddr_t *resp, struct exportinfo *exi,
654 654 struct svc_req *req, cred_t *cr, bool_t ro)
655 655 {
656 656 DTRACE_NFSV3_4(op__null__start, struct svc_req *, req,
657 657 cred_t *, cr, vnode_t *, NULL, struct exportinfo *, exi);
658 658 DTRACE_NFSV3_4(op__null__done, struct svc_req *, req,
659 659 cred_t *, cr, vnode_t *, NULL, struct exportinfo *, exi);
660 660 }
661 661
662 662 /* ARGSUSED */
663 663 static void
664 664 rfs_error(caddr_t *argp, caddr_t *resp, struct exportinfo *exi,
665 665 struct svc_req *req, cred_t *cr, bool_t ro)
666 666 {
667 667 /* return (EOPNOTSUPP); */
668 668 }
669 669
670 670 static void
671 671 nullfree(void)
672 672 {
673 673 }
674 674
675 675 static char *rfscallnames_v2[] = {
676 676 "RFS2_NULL",
677 677 "RFS2_GETATTR",
678 678 "RFS2_SETATTR",
679 679 "RFS2_ROOT",
680 680 "RFS2_LOOKUP",
681 681 "RFS2_READLINK",
682 682 "RFS2_READ",
683 683 "RFS2_WRITECACHE",
684 684 "RFS2_WRITE",
685 685 "RFS2_CREATE",
686 686 "RFS2_REMOVE",
687 687 "RFS2_RENAME",
688 688 "RFS2_LINK",
689 689 "RFS2_SYMLINK",
690 690 "RFS2_MKDIR",
691 691 "RFS2_RMDIR",
692 692 "RFS2_READDIR",
693 693 "RFS2_STATFS"
694 694 };
695 695
696 696 static struct rpcdisp rfsdisptab_v2[] = {
697 697 /*
698 698 * NFS VERSION 2
699 699 */
700 700
701 701 /* RFS_NULL = 0 */
702 702 {rpc_null,
703 703 xdr_void, NULL_xdrproc_t, 0,
704 704 xdr_void, NULL_xdrproc_t, 0,
705 705 nullfree, RPC_IDEMPOTENT,
706 706 0},
707 707
708 708 /* RFS_GETATTR = 1 */
709 709 {rfs_getattr,
710 710 xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
711 711 xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
712 712 nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
713 713 rfs_getattr_getfh},
714 714
715 715 /* RFS_SETATTR = 2 */
716 716 {rfs_setattr,
717 717 xdr_saargs, NULL_xdrproc_t, sizeof (struct nfssaargs),
718 718 xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
719 719 nullfree, RPC_MAPRESP,
720 720 rfs_setattr_getfh},
721 721
722 722 /* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
723 723 {rfs_error,
724 724 xdr_void, NULL_xdrproc_t, 0,
725 725 xdr_void, NULL_xdrproc_t, 0,
726 726 nullfree, RPC_IDEMPOTENT,
727 727 0},
728 728
729 729 /* RFS_LOOKUP = 4 */
730 730 {rfs_lookup,
731 731 xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
732 732 xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
733 733 nullfree, RPC_IDEMPOTENT|RPC_MAPRESP|RPC_PUBLICFH_OK,
734 734 rfs_lookup_getfh},
735 735
736 736 /* RFS_READLINK = 5 */
737 737 {rfs_readlink,
738 738 xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
739 739 xdr_rdlnres, NULL_xdrproc_t, sizeof (struct nfsrdlnres),
740 740 rfs_rlfree, RPC_IDEMPOTENT,
741 741 rfs_readlink_getfh},
742 742
743 743 /* RFS_READ = 6 */
744 744 {rfs_read,
745 745 xdr_readargs, NULL_xdrproc_t, sizeof (struct nfsreadargs),
746 746 xdr_rdresult, NULL_xdrproc_t, sizeof (struct nfsrdresult),
747 747 rfs_rdfree, RPC_IDEMPOTENT,
748 748 rfs_read_getfh},
749 749
750 750 /* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
751 751 {rfs_error,
752 752 xdr_void, NULL_xdrproc_t, 0,
753 753 xdr_void, NULL_xdrproc_t, 0,
754 754 nullfree, RPC_IDEMPOTENT,
755 755 0},
756 756
757 757 /* RFS_WRITE = 8 */
758 758 {rfs_write,
759 759 xdr_writeargs, NULL_xdrproc_t, sizeof (struct nfswriteargs),
760 760 xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
761 761 nullfree, RPC_MAPRESP,
762 762 rfs_write_getfh},
763 763
764 764 /* RFS_CREATE = 9 */
765 765 {rfs_create,
766 766 xdr_creatargs, NULL_xdrproc_t, sizeof (struct nfscreatargs),
767 767 xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
768 768 nullfree, RPC_MAPRESP,
769 769 rfs_create_getfh},
770 770
771 771 /* RFS_REMOVE = 10 */
772 772 {rfs_remove,
773 773 xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
774 774 #ifdef _LITTLE_ENDIAN
775 775 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
776 776 #else
777 777 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
778 778 #endif
779 779 nullfree, RPC_MAPRESP,
780 780 rfs_remove_getfh},
781 781
782 782 /* RFS_RENAME = 11 */
783 783 {rfs_rename,
784 784 xdr_rnmargs, NULL_xdrproc_t, sizeof (struct nfsrnmargs),
785 785 #ifdef _LITTLE_ENDIAN
786 786 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
787 787 #else
788 788 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
789 789 #endif
790 790 nullfree, RPC_MAPRESP,
791 791 rfs_rename_getfh},
792 792
793 793 /* RFS_LINK = 12 */
794 794 {rfs_link,
795 795 xdr_linkargs, NULL_xdrproc_t, sizeof (struct nfslinkargs),
796 796 #ifdef _LITTLE_ENDIAN
797 797 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
798 798 #else
799 799 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
800 800 #endif
801 801 nullfree, RPC_MAPRESP,
802 802 rfs_link_getfh},
803 803
804 804 /* RFS_SYMLINK = 13 */
805 805 {rfs_symlink,
806 806 xdr_slargs, NULL_xdrproc_t, sizeof (struct nfsslargs),
807 807 #ifdef _LITTLE_ENDIAN
808 808 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
809 809 #else
810 810 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
811 811 #endif
812 812 nullfree, RPC_MAPRESP,
813 813 rfs_symlink_getfh},
814 814
815 815 /* RFS_MKDIR = 14 */
816 816 {rfs_mkdir,
817 817 xdr_creatargs, NULL_xdrproc_t, sizeof (struct nfscreatargs),
818 818 xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
819 819 nullfree, RPC_MAPRESP,
820 820 rfs_mkdir_getfh},
821 821
822 822 /* RFS_RMDIR = 15 */
823 823 {rfs_rmdir,
824 824 xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
825 825 #ifdef _LITTLE_ENDIAN
826 826 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
827 827 #else
828 828 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
829 829 #endif
830 830 nullfree, RPC_MAPRESP,
831 831 rfs_rmdir_getfh},
832 832
833 833 /* RFS_READDIR = 16 */
834 834 {rfs_readdir,
835 835 xdr_rddirargs, NULL_xdrproc_t, sizeof (struct nfsrddirargs),
836 836 xdr_putrddirres, NULL_xdrproc_t, sizeof (struct nfsrddirres),
837 837 rfs_rddirfree, RPC_IDEMPOTENT,
838 838 rfs_readdir_getfh},
839 839
840 840 /* RFS_STATFS = 17 */
841 841 {rfs_statfs,
842 842 xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
843 843 xdr_statfs, xdr_faststatfs, sizeof (struct nfsstatfs),
844 844 nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
845 845 rfs_statfs_getfh},
846 846 };
847 847
848 848 static char *rfscallnames_v3[] = {
849 849 "RFS3_NULL",
850 850 "RFS3_GETATTR",
851 851 "RFS3_SETATTR",
852 852 "RFS3_LOOKUP",
853 853 "RFS3_ACCESS",
854 854 "RFS3_READLINK",
855 855 "RFS3_READ",
856 856 "RFS3_WRITE",
857 857 "RFS3_CREATE",
858 858 "RFS3_MKDIR",
859 859 "RFS3_SYMLINK",
860 860 "RFS3_MKNOD",
861 861 "RFS3_REMOVE",
862 862 "RFS3_RMDIR",
863 863 "RFS3_RENAME",
864 864 "RFS3_LINK",
865 865 "RFS3_READDIR",
866 866 "RFS3_READDIRPLUS",
867 867 "RFS3_FSSTAT",
868 868 "RFS3_FSINFO",
869 869 "RFS3_PATHCONF",
870 870 "RFS3_COMMIT"
871 871 };
872 872
873 873 static struct rpcdisp rfsdisptab_v3[] = {
874 874 /*
875 875 * NFS VERSION 3
876 876 */
877 877
878 878 /* RFS_NULL = 0 */
879 879 {rpc_null_v3,
880 880 xdr_void, NULL_xdrproc_t, 0,
881 881 xdr_void, NULL_xdrproc_t, 0,
882 882 nullfree, RPC_IDEMPOTENT,
883 883 0},
884 884
885 885 /* RFS3_GETATTR = 1 */
886 886 {rfs3_getattr,
887 887 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (GETATTR3args),
888 888 xdr_GETATTR3res, NULL_xdrproc_t, sizeof (GETATTR3res),
889 889 nullfree, (RPC_IDEMPOTENT | RPC_ALLOWANON),
890 890 rfs3_getattr_getfh},
891 891
892 892 /* RFS3_SETATTR = 2 */
893 893 {rfs3_setattr,
894 894 xdr_SETATTR3args, NULL_xdrproc_t, sizeof (SETATTR3args),
895 895 xdr_SETATTR3res, NULL_xdrproc_t, sizeof (SETATTR3res),
896 896 nullfree, 0,
897 897 rfs3_setattr_getfh},
898 898
899 899 /* RFS3_LOOKUP = 3 */
900 900 {rfs3_lookup,
901 901 xdr_diropargs3, NULL_xdrproc_t, sizeof (LOOKUP3args),
902 902 xdr_LOOKUP3res, NULL_xdrproc_t, sizeof (LOOKUP3res),
903 903 nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK),
904 904 rfs3_lookup_getfh},
905 905
906 906 /* RFS3_ACCESS = 4 */
907 907 {rfs3_access,
908 908 xdr_ACCESS3args, NULL_xdrproc_t, sizeof (ACCESS3args),
909 909 xdr_ACCESS3res, NULL_xdrproc_t, sizeof (ACCESS3res),
910 910 nullfree, RPC_IDEMPOTENT,
911 911 rfs3_access_getfh},
912 912
913 913 /* RFS3_READLINK = 5 */
914 914 {rfs3_readlink,
915 915 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (READLINK3args),
916 916 xdr_READLINK3res, NULL_xdrproc_t, sizeof (READLINK3res),
917 917 rfs3_readlink_free, RPC_IDEMPOTENT,
918 918 rfs3_readlink_getfh},
919 919
920 920 /* RFS3_READ = 6 */
921 921 {rfs3_read,
922 922 xdr_READ3args, NULL_xdrproc_t, sizeof (READ3args),
923 923 xdr_READ3res, NULL_xdrproc_t, sizeof (READ3res),
924 924 rfs3_read_free, RPC_IDEMPOTENT,
925 925 rfs3_read_getfh},
926 926
927 927 /* RFS3_WRITE = 7 */
928 928 {rfs3_write,
929 929 xdr_WRITE3args, NULL_xdrproc_t, sizeof (WRITE3args),
930 930 xdr_WRITE3res, NULL_xdrproc_t, sizeof (WRITE3res),
931 931 nullfree, 0,
932 932 rfs3_write_getfh},
933 933
934 934 /* RFS3_CREATE = 8 */
935 935 {rfs3_create,
936 936 xdr_CREATE3args, NULL_xdrproc_t, sizeof (CREATE3args),
937 937 xdr_CREATE3res, NULL_xdrproc_t, sizeof (CREATE3res),
938 938 nullfree, 0,
939 939 rfs3_create_getfh},
940 940
941 941 /* RFS3_MKDIR = 9 */
942 942 {rfs3_mkdir,
943 943 xdr_MKDIR3args, NULL_xdrproc_t, sizeof (MKDIR3args),
944 944 xdr_MKDIR3res, NULL_xdrproc_t, sizeof (MKDIR3res),
945 945 nullfree, 0,
946 946 rfs3_mkdir_getfh},
947 947
948 948 /* RFS3_SYMLINK = 10 */
949 949 {rfs3_symlink,
950 950 xdr_SYMLINK3args, NULL_xdrproc_t, sizeof (SYMLINK3args),
951 951 xdr_SYMLINK3res, NULL_xdrproc_t, sizeof (SYMLINK3res),
952 952 nullfree, 0,
953 953 rfs3_symlink_getfh},
954 954
955 955 /* RFS3_MKNOD = 11 */
956 956 {rfs3_mknod,
957 957 xdr_MKNOD3args, NULL_xdrproc_t, sizeof (MKNOD3args),
958 958 xdr_MKNOD3res, NULL_xdrproc_t, sizeof (MKNOD3res),
959 959 nullfree, 0,
960 960 rfs3_mknod_getfh},
961 961
962 962 /* RFS3_REMOVE = 12 */
963 963 {rfs3_remove,
964 964 xdr_diropargs3, NULL_xdrproc_t, sizeof (REMOVE3args),
965 965 xdr_REMOVE3res, NULL_xdrproc_t, sizeof (REMOVE3res),
966 966 nullfree, 0,
967 967 rfs3_remove_getfh},
968 968
969 969 /* RFS3_RMDIR = 13 */
970 970 {rfs3_rmdir,
971 971 xdr_diropargs3, NULL_xdrproc_t, sizeof (RMDIR3args),
972 972 xdr_RMDIR3res, NULL_xdrproc_t, sizeof (RMDIR3res),
973 973 nullfree, 0,
974 974 rfs3_rmdir_getfh},
975 975
976 976 /* RFS3_RENAME = 14 */
977 977 {rfs3_rename,
978 978 xdr_RENAME3args, NULL_xdrproc_t, sizeof (RENAME3args),
979 979 xdr_RENAME3res, NULL_xdrproc_t, sizeof (RENAME3res),
980 980 nullfree, 0,
981 981 rfs3_rename_getfh},
982 982
983 983 /* RFS3_LINK = 15 */
984 984 {rfs3_link,
985 985 xdr_LINK3args, NULL_xdrproc_t, sizeof (LINK3args),
986 986 xdr_LINK3res, NULL_xdrproc_t, sizeof (LINK3res),
987 987 nullfree, 0,
988 988 rfs3_link_getfh},
989 989
990 990 /* RFS3_READDIR = 16 */
991 991 {rfs3_readdir,
992 992 xdr_READDIR3args, NULL_xdrproc_t, sizeof (READDIR3args),
993 993 xdr_READDIR3res, NULL_xdrproc_t, sizeof (READDIR3res),
994 994 rfs3_readdir_free, RPC_IDEMPOTENT,
995 995 rfs3_readdir_getfh},
996 996
997 997 /* RFS3_READDIRPLUS = 17 */
998 998 {rfs3_readdirplus,
999 999 xdr_READDIRPLUS3args, NULL_xdrproc_t, sizeof (READDIRPLUS3args),
1000 1000 xdr_READDIRPLUS3res, NULL_xdrproc_t, sizeof (READDIRPLUS3res),
1001 1001 rfs3_readdirplus_free, RPC_AVOIDWORK,
1002 1002 rfs3_readdirplus_getfh},
1003 1003
1004 1004 /* RFS3_FSSTAT = 18 */
1005 1005 {rfs3_fsstat,
1006 1006 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (FSSTAT3args),
1007 1007 xdr_FSSTAT3res, NULL_xdrproc_t, sizeof (FSSTAT3res),
1008 1008 nullfree, RPC_IDEMPOTENT,
1009 1009 rfs3_fsstat_getfh},
1010 1010
1011 1011 /* RFS3_FSINFO = 19 */
1012 1012 {rfs3_fsinfo,
1013 1013 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (FSINFO3args),
1014 1014 xdr_FSINFO3res, NULL_xdrproc_t, sizeof (FSINFO3res),
1015 1015 nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON,
1016 1016 rfs3_fsinfo_getfh},
1017 1017
1018 1018 /* RFS3_PATHCONF = 20 */
1019 1019 {rfs3_pathconf,
1020 1020 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (PATHCONF3args),
1021 1021 xdr_PATHCONF3res, NULL_xdrproc_t, sizeof (PATHCONF3res),
1022 1022 nullfree, RPC_IDEMPOTENT,
1023 1023 rfs3_pathconf_getfh},
1024 1024
1025 1025 /* RFS3_COMMIT = 21 */
1026 1026 {rfs3_commit,
1027 1027 xdr_COMMIT3args, NULL_xdrproc_t, sizeof (COMMIT3args),
1028 1028 xdr_COMMIT3res, NULL_xdrproc_t, sizeof (COMMIT3res),
1029 1029 nullfree, RPC_IDEMPOTENT,
1030 1030 rfs3_commit_getfh},
1031 1031 };
1032 1032
1033 1033 static char *rfscallnames_v4[] = {
1034 1034 "RFS4_NULL",
1035 1035 "RFS4_COMPOUND",
1036 1036 "RFS4_NULL",
1037 1037 "RFS4_NULL",
1038 1038 "RFS4_NULL",
1039 1039 "RFS4_NULL",
1040 1040 "RFS4_NULL",
1041 1041 "RFS4_NULL",
1042 1042 "RFS4_CREATE"
1043 1043 };
1044 1044
1045 1045 static struct rpcdisp rfsdisptab_v4[] = {
1046 1046 /*
1047 1047 * NFS VERSION 4
1048 1048 */
1049 1049
1050 1050 /* RFS_NULL = 0 */
1051 1051 {rpc_null,
1052 1052 xdr_void, NULL_xdrproc_t, 0,
1053 1053 xdr_void, NULL_xdrproc_t, 0,
1054 1054 nullfree, RPC_IDEMPOTENT, 0},
1055 1055
1056 1056 /* RFS4_compound = 1 */
1057 1057 {rfs4_compound,
1058 1058 xdr_COMPOUND4args_srv, NULL_xdrproc_t, sizeof (COMPOUND4args),
1059 1059 xdr_COMPOUND4res_srv, NULL_xdrproc_t, sizeof (COMPOUND4res),
1060 1060 rfs4_compound_free, 0, 0},
1061 1061 };
1062 1062
1063 1063 union rfs_args {
1064 1064 /*
1065 1065 * NFS VERSION 2
1066 1066 */
1067 1067
1068 1068 /* RFS_NULL = 0 */
1069 1069
1070 1070 /* RFS_GETATTR = 1 */
1071 1071 fhandle_t nfs2_getattr_args;
1072 1072
1073 1073 /* RFS_SETATTR = 2 */
1074 1074 struct nfssaargs nfs2_setattr_args;
1075 1075
1076 1076 /* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
1077 1077
1078 1078 /* RFS_LOOKUP = 4 */
1079 1079 struct nfsdiropargs nfs2_lookup_args;
1080 1080
1081 1081 /* RFS_READLINK = 5 */
1082 1082 fhandle_t nfs2_readlink_args;
1083 1083
1084 1084 /* RFS_READ = 6 */
1085 1085 struct nfsreadargs nfs2_read_args;
1086 1086
1087 1087 /* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
1088 1088
1089 1089 /* RFS_WRITE = 8 */
1090 1090 struct nfswriteargs nfs2_write_args;
1091 1091
1092 1092 /* RFS_CREATE = 9 */
1093 1093 struct nfscreatargs nfs2_create_args;
1094 1094
1095 1095 /* RFS_REMOVE = 10 */
1096 1096 struct nfsdiropargs nfs2_remove_args;
1097 1097
1098 1098 /* RFS_RENAME = 11 */
1099 1099 struct nfsrnmargs nfs2_rename_args;
1100 1100
1101 1101 /* RFS_LINK = 12 */
1102 1102 struct nfslinkargs nfs2_link_args;
1103 1103
1104 1104 /* RFS_SYMLINK = 13 */
1105 1105 struct nfsslargs nfs2_symlink_args;
1106 1106
1107 1107 /* RFS_MKDIR = 14 */
1108 1108 struct nfscreatargs nfs2_mkdir_args;
1109 1109
1110 1110 /* RFS_RMDIR = 15 */
1111 1111 struct nfsdiropargs nfs2_rmdir_args;
1112 1112
1113 1113 /* RFS_READDIR = 16 */
1114 1114 struct nfsrddirargs nfs2_readdir_args;
1115 1115
1116 1116 /* RFS_STATFS = 17 */
1117 1117 fhandle_t nfs2_statfs_args;
1118 1118
1119 1119 /*
1120 1120 * NFS VERSION 3
1121 1121 */
1122 1122
1123 1123 /* RFS_NULL = 0 */
1124 1124
1125 1125 /* RFS3_GETATTR = 1 */
1126 1126 GETATTR3args nfs3_getattr_args;
1127 1127
1128 1128 /* RFS3_SETATTR = 2 */
1129 1129 SETATTR3args nfs3_setattr_args;
1130 1130
1131 1131 /* RFS3_LOOKUP = 3 */
1132 1132 LOOKUP3args nfs3_lookup_args;
1133 1133
1134 1134 /* RFS3_ACCESS = 4 */
1135 1135 ACCESS3args nfs3_access_args;
1136 1136
1137 1137 /* RFS3_READLINK = 5 */
1138 1138 READLINK3args nfs3_readlink_args;
1139 1139
1140 1140 /* RFS3_READ = 6 */
1141 1141 READ3args nfs3_read_args;
1142 1142
1143 1143 /* RFS3_WRITE = 7 */
1144 1144 WRITE3args nfs3_write_args;
1145 1145
1146 1146 /* RFS3_CREATE = 8 */
1147 1147 CREATE3args nfs3_create_args;
1148 1148
1149 1149 /* RFS3_MKDIR = 9 */
1150 1150 MKDIR3args nfs3_mkdir_args;
1151 1151
1152 1152 /* RFS3_SYMLINK = 10 */
1153 1153 SYMLINK3args nfs3_symlink_args;
1154 1154
1155 1155 /* RFS3_MKNOD = 11 */
1156 1156 MKNOD3args nfs3_mknod_args;
1157 1157
1158 1158 /* RFS3_REMOVE = 12 */
1159 1159 REMOVE3args nfs3_remove_args;
1160 1160
1161 1161 /* RFS3_RMDIR = 13 */
1162 1162 RMDIR3args nfs3_rmdir_args;
1163 1163
1164 1164 /* RFS3_RENAME = 14 */
1165 1165 RENAME3args nfs3_rename_args;
1166 1166
1167 1167 /* RFS3_LINK = 15 */
1168 1168 LINK3args nfs3_link_args;
1169 1169
1170 1170 /* RFS3_READDIR = 16 */
1171 1171 READDIR3args nfs3_readdir_args;
1172 1172
1173 1173 /* RFS3_READDIRPLUS = 17 */
1174 1174 READDIRPLUS3args nfs3_readdirplus_args;
1175 1175
1176 1176 /* RFS3_FSSTAT = 18 */
1177 1177 FSSTAT3args nfs3_fsstat_args;
1178 1178
1179 1179 /* RFS3_FSINFO = 19 */
1180 1180 FSINFO3args nfs3_fsinfo_args;
1181 1181
1182 1182 /* RFS3_PATHCONF = 20 */
1183 1183 PATHCONF3args nfs3_pathconf_args;
1184 1184
1185 1185 /* RFS3_COMMIT = 21 */
1186 1186 COMMIT3args nfs3_commit_args;
1187 1187
1188 1188 /*
1189 1189 * NFS VERSION 4
1190 1190 */
1191 1191
1192 1192 /* RFS_NULL = 0 */
1193 1193
1194 1194 /* COMPUND = 1 */
1195 1195 COMPOUND4args nfs4_compound_args;
1196 1196 };
1197 1197
1198 1198 union rfs_res {
1199 1199 /*
1200 1200 * NFS VERSION 2
1201 1201 */
1202 1202
1203 1203 /* RFS_NULL = 0 */
1204 1204
1205 1205 /* RFS_GETATTR = 1 */
1206 1206 struct nfsattrstat nfs2_getattr_res;
1207 1207
1208 1208 /* RFS_SETATTR = 2 */
1209 1209 struct nfsattrstat nfs2_setattr_res;
1210 1210
1211 1211 /* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
1212 1212
1213 1213 /* RFS_LOOKUP = 4 */
1214 1214 struct nfsdiropres nfs2_lookup_res;
1215 1215
1216 1216 /* RFS_READLINK = 5 */
1217 1217 struct nfsrdlnres nfs2_readlink_res;
1218 1218
1219 1219 /* RFS_READ = 6 */
1220 1220 struct nfsrdresult nfs2_read_res;
1221 1221
1222 1222 /* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
1223 1223
1224 1224 /* RFS_WRITE = 8 */
1225 1225 struct nfsattrstat nfs2_write_res;
1226 1226
1227 1227 /* RFS_CREATE = 9 */
1228 1228 struct nfsdiropres nfs2_create_res;
1229 1229
1230 1230 /* RFS_REMOVE = 10 */
1231 1231 enum nfsstat nfs2_remove_res;
1232 1232
1233 1233 /* RFS_RENAME = 11 */
1234 1234 enum nfsstat nfs2_rename_res;
1235 1235
1236 1236 /* RFS_LINK = 12 */
1237 1237 enum nfsstat nfs2_link_res;
1238 1238
1239 1239 /* RFS_SYMLINK = 13 */
1240 1240 enum nfsstat nfs2_symlink_res;
1241 1241
1242 1242 /* RFS_MKDIR = 14 */
1243 1243 struct nfsdiropres nfs2_mkdir_res;
1244 1244
1245 1245 /* RFS_RMDIR = 15 */
1246 1246 enum nfsstat nfs2_rmdir_res;
1247 1247
1248 1248 /* RFS_READDIR = 16 */
1249 1249 struct nfsrddirres nfs2_readdir_res;
1250 1250
1251 1251 /* RFS_STATFS = 17 */
1252 1252 struct nfsstatfs nfs2_statfs_res;
1253 1253
1254 1254 /*
1255 1255 * NFS VERSION 3
1256 1256 */
1257 1257
1258 1258 /* RFS_NULL = 0 */
1259 1259
1260 1260 /* RFS3_GETATTR = 1 */
1261 1261 GETATTR3res nfs3_getattr_res;
1262 1262
1263 1263 /* RFS3_SETATTR = 2 */
1264 1264 SETATTR3res nfs3_setattr_res;
1265 1265
1266 1266 /* RFS3_LOOKUP = 3 */
1267 1267 LOOKUP3res nfs3_lookup_res;
1268 1268
1269 1269 /* RFS3_ACCESS = 4 */
1270 1270 ACCESS3res nfs3_access_res;
1271 1271
1272 1272 /* RFS3_READLINK = 5 */
1273 1273 READLINK3res nfs3_readlink_res;
1274 1274
1275 1275 /* RFS3_READ = 6 */
1276 1276 READ3res nfs3_read_res;
1277 1277
1278 1278 /* RFS3_WRITE = 7 */
1279 1279 WRITE3res nfs3_write_res;
1280 1280
1281 1281 /* RFS3_CREATE = 8 */
1282 1282 CREATE3res nfs3_create_res;
1283 1283
1284 1284 /* RFS3_MKDIR = 9 */
1285 1285 MKDIR3res nfs3_mkdir_res;
1286 1286
1287 1287 /* RFS3_SYMLINK = 10 */
1288 1288 SYMLINK3res nfs3_symlink_res;
1289 1289
1290 1290 /* RFS3_MKNOD = 11 */
1291 1291 MKNOD3res nfs3_mknod_res;
1292 1292
1293 1293 /* RFS3_REMOVE = 12 */
1294 1294 REMOVE3res nfs3_remove_res;
1295 1295
1296 1296 /* RFS3_RMDIR = 13 */
1297 1297 RMDIR3res nfs3_rmdir_res;
1298 1298
1299 1299 /* RFS3_RENAME = 14 */
1300 1300 RENAME3res nfs3_rename_res;
1301 1301
1302 1302 /* RFS3_LINK = 15 */
1303 1303 LINK3res nfs3_link_res;
1304 1304
1305 1305 /* RFS3_READDIR = 16 */
1306 1306 READDIR3res nfs3_readdir_res;
1307 1307
1308 1308 /* RFS3_READDIRPLUS = 17 */
1309 1309 READDIRPLUS3res nfs3_readdirplus_res;
1310 1310
1311 1311 /* RFS3_FSSTAT = 18 */
1312 1312 FSSTAT3res nfs3_fsstat_res;
1313 1313
1314 1314 /* RFS3_FSINFO = 19 */
1315 1315 FSINFO3res nfs3_fsinfo_res;
1316 1316
1317 1317 /* RFS3_PATHCONF = 20 */
1318 1318 PATHCONF3res nfs3_pathconf_res;
1319 1319
1320 1320 /* RFS3_COMMIT = 21 */
1321 1321 COMMIT3res nfs3_commit_res;
1322 1322
1323 1323 /*
1324 1324 * NFS VERSION 4
1325 1325 */
1326 1326
1327 1327 /* RFS_NULL = 0 */
1328 1328
1329 1329 /* RFS4_COMPOUND = 1 */
1330 1330 COMPOUND4res nfs4_compound_res;
1331 1331
1332 1332 };
1333 1333
1334 1334 static struct rpc_disptable rfs_disptable[] = {
1335 1335 {sizeof (rfsdisptab_v2) / sizeof (rfsdisptab_v2[0]),
1336 1336 rfscallnames_v2,
1337 1337 &rfsproccnt_v2_ptr, rfsdisptab_v2},
1338 1338 {sizeof (rfsdisptab_v3) / sizeof (rfsdisptab_v3[0]),
1339 1339 rfscallnames_v3,
1340 1340 &rfsproccnt_v3_ptr, rfsdisptab_v3},
1341 1341 {sizeof (rfsdisptab_v4) / sizeof (rfsdisptab_v4[0]),
1342 1342 rfscallnames_v4,
1343 1343 &rfsproccnt_v4_ptr, rfsdisptab_v4},
1344 1344 };
1345 1345
1346 1346 /*
1347 1347 * If nfs_portmon is set, then clients are required to use privileged
1348 1348 * ports (ports < IPPORT_RESERVED) in order to get NFS services.
1349 1349 *
1350 1350 * N.B.: this attempt to carry forward the already ill-conceived notion
1351 1351 * of privileged ports for TCP/UDP is really quite ineffectual. Not only
1352 1352 * is it transport-dependent, it's laughably easy to spoof. If you're
1353 1353 * really interested in security, you must start with secure RPC instead.
1354 1354 */
1355 1355 static int nfs_portmon = 0;
1356 1356
1357 1357 #ifdef DEBUG
1358 1358 static int cred_hits = 0;
1359 1359 static int cred_misses = 0;
1360 1360 #endif
1361 1361
1362 1362 #ifdef DEBUG
1363 1363 /*
1364 1364 * Debug code to allow disabling of rfs_dispatch() use of
1365 1365 * fastxdrargs() and fastxdrres() calls for testing purposes.
1366 1366 */
1367 1367 static int rfs_no_fast_xdrargs = 0;
1368 1368 static int rfs_no_fast_xdrres = 0;
1369 1369 #endif
1370 1370
1371 1371 union acl_args {
1372 1372 /*
1373 1373 * ACL VERSION 2
1374 1374 */
1375 1375
1376 1376 /* ACL2_NULL = 0 */
1377 1377
1378 1378 /* ACL2_GETACL = 1 */
1379 1379 GETACL2args acl2_getacl_args;
1380 1380
1381 1381 /* ACL2_SETACL = 2 */
1382 1382 SETACL2args acl2_setacl_args;
1383 1383
1384 1384 /* ACL2_GETATTR = 3 */
1385 1385 GETATTR2args acl2_getattr_args;
1386 1386
1387 1387 /* ACL2_ACCESS = 4 */
1388 1388 ACCESS2args acl2_access_args;
1389 1389
1390 1390 /* ACL2_GETXATTRDIR = 5 */
1391 1391 GETXATTRDIR2args acl2_getxattrdir_args;
1392 1392
1393 1393 /*
1394 1394 * ACL VERSION 3
1395 1395 */
1396 1396
1397 1397 /* ACL3_NULL = 0 */
1398 1398
1399 1399 /* ACL3_GETACL = 1 */
1400 1400 GETACL3args acl3_getacl_args;
1401 1401
1402 1402 /* ACL3_SETACL = 2 */
1403 1403 SETACL3args acl3_setacl;
1404 1404
1405 1405 /* ACL3_GETXATTRDIR = 3 */
1406 1406 GETXATTRDIR3args acl3_getxattrdir_args;
1407 1407
1408 1408 };
1409 1409
1410 1410 union acl_res {
1411 1411 /*
1412 1412 * ACL VERSION 2
1413 1413 */
1414 1414
1415 1415 /* ACL2_NULL = 0 */
1416 1416
1417 1417 /* ACL2_GETACL = 1 */
1418 1418 GETACL2res acl2_getacl_res;
1419 1419
1420 1420 /* ACL2_SETACL = 2 */
1421 1421 SETACL2res acl2_setacl_res;
1422 1422
1423 1423 /* ACL2_GETATTR = 3 */
1424 1424 GETATTR2res acl2_getattr_res;
1425 1425
1426 1426 /* ACL2_ACCESS = 4 */
1427 1427 ACCESS2res acl2_access_res;
1428 1428
1429 1429 /* ACL2_GETXATTRDIR = 5 */
1430 1430 GETXATTRDIR2args acl2_getxattrdir_res;
1431 1431
1432 1432 /*
1433 1433 * ACL VERSION 3
1434 1434 */
1435 1435
1436 1436 /* ACL3_NULL = 0 */
1437 1437
1438 1438 /* ACL3_GETACL = 1 */
1439 1439 GETACL3res acl3_getacl_res;
1440 1440
1441 1441 /* ACL3_SETACL = 2 */
1442 1442 SETACL3res acl3_setacl_res;
1443 1443
1444 1444 /* ACL3_GETXATTRDIR = 3 */
1445 1445 GETXATTRDIR3res acl3_getxattrdir_res;
1446 1446
1447 1447 };
1448 1448
1449 1449 static bool_t
1450 1450 auth_tooweak(struct svc_req *req, char *res)
1451 1451 {
1452 1452
1453 1453 if (req->rq_vers == NFS_VERSION && req->rq_proc == RFS_LOOKUP) {
1454 1454 struct nfsdiropres *dr = (struct nfsdiropres *)res;
1455 1455 if ((enum wnfsstat)dr->dr_status == WNFSERR_CLNT_FLAVOR)
1456 1456 return (TRUE);
1457 1457 } else if (req->rq_vers == NFS_V3 && req->rq_proc == NFSPROC3_LOOKUP) {
1458 1458 LOOKUP3res *resp = (LOOKUP3res *)res;
1459 1459 if ((enum wnfsstat)resp->status == WNFSERR_CLNT_FLAVOR)
1460 1460 return (TRUE);
1461 1461 }
1462 1462 return (FALSE);
1463 1463 }
1464 1464
1465 1465
1466 1466 static void
1467 1467 common_dispatch(struct svc_req *req, SVCXPRT *xprt, rpcvers_t min_vers,
1468 1468 rpcvers_t max_vers, char *pgmname, struct rpc_disptable *disptable)
1469 1469 {
1470 1470 int which;
1471 1471 rpcvers_t vers;
1472 1472 char *args;
1473 1473 union {
1474 1474 union rfs_args ra;
1475 1475 union acl_args aa;
1476 1476 } args_buf;
1477 1477 char *res;
1478 1478 union {
1479 1479 union rfs_res rr;
1480 1480 union acl_res ar;
1481 1481 } res_buf;
1482 1482 struct rpcdisp *disp = NULL;
1483 1483 int dis_flags = 0;
1484 1484 cred_t *cr;
1485 1485 int error = 0;
1486 1486 int anon_ok;
1487 1487 struct exportinfo *exi = NULL;
1488 1488 unsigned int nfslog_rec_id;
1489 1489 int dupstat;
1490 1490 struct dupreq *dr;
1491 1491 int authres;
1492 1492 bool_t publicfh_ok = FALSE;
1493 1493 enum_t auth_flavor;
1494 1494 bool_t dupcached = FALSE;
1495 1495 struct netbuf nb;
1496 1496 bool_t logging_enabled = FALSE;
1497 1497 struct exportinfo *nfslog_exi = NULL;
1498 1498 char **procnames;
1499 1499 char cbuf[INET6_ADDRSTRLEN]; /* to hold both IPv4 and IPv6 addr */
1500 1500 bool_t ro = FALSE;
1501 1501 nfs_export_t *ne = nfs_get_export();
1502 1502
1503 1503 vers = req->rq_vers;
1504 1504
1505 1505 if (vers < min_vers || vers > max_vers) {
1506 1506 svcerr_progvers(req->rq_xprt, min_vers, max_vers);
1507 1507 error++;
1508 1508 cmn_err(CE_NOTE, "%s: bad version number %u", pgmname, vers);
1509 1509 goto done;
1510 1510 }
1511 1511 vers -= min_vers;
1512 1512
1513 1513 which = req->rq_proc;
1514 1514 if (which < 0 || which >= disptable[(int)vers].dis_nprocs) {
1515 1515 svcerr_noproc(req->rq_xprt);
1516 1516 error++;
1517 1517 goto done;
1518 1518 }
1519 1519
1520 1520 (*(disptable[(int)vers].dis_proccntp))[which].value.ui64++;
1521 1521
1522 1522 disp = &disptable[(int)vers].dis_table[which];
1523 1523 procnames = disptable[(int)vers].dis_procnames;
1524 1524
1525 1525 auth_flavor = req->rq_cred.oa_flavor;
1526 1526
1527 1527 /*
1528 1528 * Deserialize into the args struct.
1529 1529 */
1530 1530 args = (char *)&args_buf;
1531 1531
1532 1532 #ifdef DEBUG
1533 1533 if (rfs_no_fast_xdrargs || (auth_flavor == RPCSEC_GSS) ||
1534 1534 disp->dis_fastxdrargs == NULL_xdrproc_t ||
1535 1535 !SVC_GETARGS(xprt, disp->dis_fastxdrargs, (char *)&args))
1536 1536 #else
1537 1537 if ((auth_flavor == RPCSEC_GSS) ||
1538 1538 disp->dis_fastxdrargs == NULL_xdrproc_t ||
1539 1539 !SVC_GETARGS(xprt, disp->dis_fastxdrargs, (char *)&args))
1540 1540 #endif
1541 1541 {
1542 1542 bzero(args, disp->dis_argsz);
1543 1543 if (!SVC_GETARGS(xprt, disp->dis_xdrargs, args)) {
1544 1544 error++;
1545 1545 /*
1546 1546 * Check if we are outside our capabilities.
1547 1547 */
1548 1548 if (rfs4_minorvers_mismatch(req, xprt, (void *)args))
1549 1549 goto done;
1550 1550
1551 1551 svcerr_decode(xprt);
1552 1552 cmn_err(CE_NOTE,
1553 1553 "Failed to decode arguments for %s version %u "
1554 1554 "procedure %s client %s%s",
1555 1555 pgmname, vers + min_vers, procnames[which],
1556 1556 client_name(req), client_addr(req, cbuf));
1557 1557 goto done;
1558 1558 }
1559 1559 }
1560 1560
1561 1561 /*
1562 1562 * If Version 4 use that specific dispatch function.
1563 1563 */
1564 1564 if (req->rq_vers == 4) {
1565 1565 error += rfs4_dispatch(disp, req, xprt, args);
1566 1566 goto done;
1567 1567 }
1568 1568
1569 1569 dis_flags = disp->dis_flags;
1570 1570
1571 1571 /*
1572 1572 * Find export information and check authentication,
1573 1573 * setting the credential if everything is ok.
1574 1574 */
1575 1575 if (disp->dis_getfh != NULL) {
1576 1576 void *fh;
1577 1577 fsid_t *fsid;
1578 1578 fid_t *fid, *xfid;
1579 1579 fhandle_t *fh2;
1580 1580 nfs_fh3 *fh3;
1581 1581
1582 1582 fh = (*disp->dis_getfh)(args);
1583 1583 switch (req->rq_vers) {
1584 1584 case NFS_VERSION:
1585 1585 fh2 = (fhandle_t *)fh;
1586 1586 fsid = &fh2->fh_fsid;
1587 1587 fid = (fid_t *)&fh2->fh_len;
1588 1588 xfid = (fid_t *)&fh2->fh_xlen;
1589 1589 break;
1590 1590 case NFS_V3:
1591 1591 fh3 = (nfs_fh3 *)fh;
1592 1592 fsid = &fh3->fh3_fsid;
1593 1593 fid = FH3TOFIDP(fh3);
1594 1594 xfid = FH3TOXFIDP(fh3);
1595 1595 break;
1596 1596 }
1597 1597
1598 1598 /*
1599 1599 * Fix for bug 1038302 - corbin
1600 1600 * There is a problem here if anonymous access is
1601 1601 * disallowed. If the current request is part of the
1602 1602 * client's mount process for the requested filesystem,
1603 1603 * then it will carry root (uid 0) credentials on it, and
1604 1604 * will be denied by checkauth if that client does not
1605 1605 * have explicit root=0 permission. This will cause the
1606 1606 * client's mount operation to fail. As a work-around,
1607 1607 * we check here to see if the request is a getattr or
1608 1608 * statfs operation on the exported vnode itself, and
1609 1609 * pass a flag to checkauth with the result of this test.
1610 1610 *
1611 1611 * The filehandle refers to the mountpoint itself if
1612 1612 * the fh_data and fh_xdata portions of the filehandle
1613 1613 * are equal.
1614 1614 *
1615 1615 * Added anon_ok argument to checkauth().
1616 1616 */
1617 1617
1618 1618 if ((dis_flags & RPC_ALLOWANON) && EQFID(fid, xfid))
1619 1619 anon_ok = 1;
1620 1620 else
1621 1621 anon_ok = 0;
1622 1622
1623 1623 cr = xprt->xp_cred;
1624 1624 ASSERT(cr != NULL);
1625 1625 #ifdef DEBUG
1626 1626 {
1627 1627 if (crgetref(cr) != 1) {
1628 1628 crfree(cr);
1629 1629 cr = crget();
1630 1630 xprt->xp_cred = cr;
1631 1631 cred_misses++;
1632 1632 } else
1633 1633 cred_hits++;
1634 1634 }
1635 1635 #else
1636 1636 if (crgetref(cr) != 1) {
1637 1637 crfree(cr);
1638 1638 cr = crget();
1639 1639 xprt->xp_cred = cr;
1640 1640 }
1641 1641 #endif
1642 1642
1643 1643 exi = checkexport(fsid, xfid);
1644 1644
1645 1645 if (exi != NULL) {
1646 1646 publicfh_ok = PUBLICFH_CHECK(ne, disp, exi, fsid, xfid);
1647 1647
1648 1648 /*
1649 1649 * Don't allow non-V4 clients access
1650 1650 * to pseudo exports
1651 1651 */
1652 1652 if (PSEUDO(exi)) {
1653 1653 svcerr_weakauth(xprt);
1654 1654 error++;
1655 1655 goto done;
1656 1656 }
1657 1657
1658 1658 authres = checkauth(exi, req, cr, anon_ok, publicfh_ok,
1659 1659 &ro);
1660 1660 /*
1661 1661 * authres > 0: authentication OK - proceed
1662 1662 * authres == 0: authentication weak - return error
1663 1663 * authres < 0: authentication timeout - drop
1664 1664 */
1665 1665 if (authres <= 0) {
1666 1666 if (authres == 0) {
1667 1667 svcerr_weakauth(xprt);
1668 1668 error++;
1669 1669 }
1670 1670 goto done;
1671 1671 }
1672 1672 }
1673 1673 } else
1674 1674 cr = NULL;
1675 1675
1676 1676 if ((dis_flags & RPC_MAPRESP) && (auth_flavor != RPCSEC_GSS)) {
1677 1677 res = (char *)SVC_GETRES(xprt, disp->dis_ressz);
1678 1678 if (res == NULL)
1679 1679 res = (char *)&res_buf;
1680 1680 } else
1681 1681 res = (char *)&res_buf;
1682 1682
1683 1683 if (!(dis_flags & RPC_IDEMPOTENT)) {
1684 1684 dupstat = SVC_DUP_EXT(xprt, req, res, disp->dis_ressz, &dr,
1685 1685 &dupcached);
1686 1686
1687 1687 switch (dupstat) {
1688 1688 case DUP_ERROR:
1689 1689 svcerr_systemerr(xprt);
1690 1690 error++;
1691 1691 goto done;
1692 1692 /* NOTREACHED */
1693 1693 case DUP_INPROGRESS:
1694 1694 if (res != (char *)&res_buf)
1695 1695 SVC_FREERES(xprt);
1696 1696 error++;
1697 1697 goto done;
1698 1698 /* NOTREACHED */
1699 1699 case DUP_NEW:
1700 1700 case DUP_DROP:
1701 1701 curthread->t_flag |= T_DONTPEND;
1702 1702
1703 1703 (*disp->dis_proc)(args, res, exi, req, cr, ro);
1704 1704
1705 1705 curthread->t_flag &= ~T_DONTPEND;
1706 1706 if (curthread->t_flag & T_WOULDBLOCK) {
1707 1707 curthread->t_flag &= ~T_WOULDBLOCK;
1708 1708 SVC_DUPDONE_EXT(xprt, dr, res, NULL,
1709 1709 disp->dis_ressz, DUP_DROP);
1710 1710 if (res != (char *)&res_buf)
1711 1711 SVC_FREERES(xprt);
1712 1712 error++;
1713 1713 goto done;
1714 1714 }
1715 1715 if (dis_flags & RPC_AVOIDWORK) {
1716 1716 SVC_DUPDONE_EXT(xprt, dr, res, NULL,
1717 1717 disp->dis_ressz, DUP_DROP);
1718 1718 } else {
1719 1719 SVC_DUPDONE_EXT(xprt, dr, res,
1720 1720 disp->dis_resfree == nullfree ? NULL :
1721 1721 disp->dis_resfree,
1722 1722 disp->dis_ressz, DUP_DONE);
1723 1723 dupcached = TRUE;
1724 1724 }
1725 1725 break;
1726 1726 case DUP_DONE:
1727 1727 break;
1728 1728 }
1729 1729
1730 1730 } else {
1731 1731 curthread->t_flag |= T_DONTPEND;
1732 1732
1733 1733 (*disp->dis_proc)(args, res, exi, req, cr, ro);
1734 1734
1735 1735 curthread->t_flag &= ~T_DONTPEND;
1736 1736 if (curthread->t_flag & T_WOULDBLOCK) {
1737 1737 curthread->t_flag &= ~T_WOULDBLOCK;
1738 1738 if (res != (char *)&res_buf)
1739 1739 SVC_FREERES(xprt);
1740 1740 error++;
1741 1741 goto done;
1742 1742 }
1743 1743 }
1744 1744
1745 1745 if (auth_tooweak(req, res)) {
1746 1746 svcerr_weakauth(xprt);
1747 1747 error++;
1748 1748 goto done;
1749 1749 }
1750 1750
1751 1751 /*
1752 1752 * Check to see if logging has been enabled on the server.
1753 1753 * If so, then obtain the export info struct to be used for
1754 1754 * the later writing of the log record. This is done for
1755 1755 * the case that a lookup is done across a non-logged public
1756 1756 * file system.
1757 1757 */
1758 1758 if (nfslog_buffer_list != NULL) {
1759 1759 nfslog_exi = nfslog_get_exi(ne, exi, req, res, &nfslog_rec_id);
1760 1760 /*
1761 1761 * Is logging enabled?
1762 1762 */
1763 1763 logging_enabled = (nfslog_exi != NULL);
1764 1764
1765 1765 /*
1766 1766 * Copy the netbuf for logging purposes, before it is
1767 1767 * freed by svc_sendreply().
1768 1768 */
1769 1769 if (logging_enabled) {
1770 1770 NFSLOG_COPY_NETBUF(nfslog_exi, xprt, &nb);
1771 1771 /*
1772 1772 * If RPC_MAPRESP flag set (i.e. in V2 ops) the
1773 1773 * res gets copied directly into the mbuf and
1774 1774 * may be freed soon after the sendreply. So we
1775 1775 * must copy it here to a safe place...
1776 1776 */
1777 1777 if (res != (char *)&res_buf) {
1778 1778 bcopy(res, (char *)&res_buf, disp->dis_ressz);
1779 1779 }
1780 1780 }
1781 1781 }
1782 1782
1783 1783 /*
1784 1784 * Serialize and send results struct
1785 1785 */
1786 1786 #ifdef DEBUG
1787 1787 if (rfs_no_fast_xdrres == 0 && res != (char *)&res_buf)
1788 1788 #else
1789 1789 if (res != (char *)&res_buf)
1790 1790 #endif
1791 1791 {
1792 1792 if (!svc_sendreply(xprt, disp->dis_fastxdrres, res)) {
1793 1793 cmn_err(CE_NOTE, "%s: bad sendreply", pgmname);
1794 1794 svcerr_systemerr(xprt);
1795 1795 error++;
1796 1796 }
1797 1797 } else {
1798 1798 if (!svc_sendreply(xprt, disp->dis_xdrres, res)) {
1799 1799 cmn_err(CE_NOTE, "%s: bad sendreply", pgmname);
1800 1800 svcerr_systemerr(xprt);
1801 1801 error++;
1802 1802 }
1803 1803 }
1804 1804
1805 1805 /*
1806 1806 * Log if needed
1807 1807 */
1808 1808 if (logging_enabled) {
1809 1809 nfslog_write_record(nfslog_exi, req, args, (char *)&res_buf,
1810 1810 cr, &nb, nfslog_rec_id, NFSLOG_ONE_BUFFER);
1811 1811 exi_rele(nfslog_exi);
1812 1812 kmem_free((&nb)->buf, (&nb)->len);
1813 1813 }
1814 1814
1815 1815 /*
1816 1816 * Free results struct. With the addition of NFS V4 we can
1817 1817 * have non-idempotent procedures with functions.
1818 1818 */
1819 1819 if (disp->dis_resfree != nullfree && dupcached == FALSE) {
1820 1820 (*disp->dis_resfree)(res);
1821 1821 }
1822 1822
1823 1823 done:
1824 1824 /*
1825 1825 * Free arguments struct
1826 1826 */
1827 1827 if (disp) {
1828 1828 if (!SVC_FREEARGS(xprt, disp->dis_xdrargs, args)) {
1829 1829 cmn_err(CE_NOTE, "%s: bad freeargs", pgmname);
1830 1830 error++;
1831 1831 }
1832 1832 } else {
1833 1833 if (!SVC_FREEARGS(xprt, (xdrproc_t)0, (caddr_t)0)) {
1834 1834 cmn_err(CE_NOTE, "%s: bad freeargs", pgmname);
1835 1835 error++;
1836 1836 }
1837 1837 }
1838 1838
1839 1839 if (exi != NULL)
1840 1840 exi_rele(exi);
1841 1841
1842 1842 global_svstat_ptr[req->rq_vers][NFS_BADCALLS].value.ui64 += error;
1843 1843
1844 1844 global_svstat_ptr[req->rq_vers][NFS_CALLS].value.ui64++;
1845 1845 }
1846 1846
1847 1847 static void
1848 1848 rfs_dispatch(struct svc_req *req, SVCXPRT *xprt)
1849 1849 {
1850 1850 common_dispatch(req, xprt, NFS_VERSMIN, NFS_VERSMAX,
1851 1851 "NFS", rfs_disptable);
1852 1852 }
1853 1853
1854 1854 static char *aclcallnames_v2[] = {
1855 1855 "ACL2_NULL",
1856 1856 "ACL2_GETACL",
1857 1857 "ACL2_SETACL",
1858 1858 "ACL2_GETATTR",
1859 1859 "ACL2_ACCESS",
1860 1860 "ACL2_GETXATTRDIR"
1861 1861 };
1862 1862
1863 1863 static struct rpcdisp acldisptab_v2[] = {
1864 1864 /*
1865 1865 * ACL VERSION 2
1866 1866 */
1867 1867
1868 1868 /* ACL2_NULL = 0 */
1869 1869 {rpc_null,
1870 1870 xdr_void, NULL_xdrproc_t, 0,
1871 1871 xdr_void, NULL_xdrproc_t, 0,
1872 1872 nullfree, RPC_IDEMPOTENT,
1873 1873 0},
1874 1874
1875 1875 /* ACL2_GETACL = 1 */
1876 1876 {acl2_getacl,
1877 1877 xdr_GETACL2args, xdr_fastGETACL2args, sizeof (GETACL2args),
1878 1878 xdr_GETACL2res, NULL_xdrproc_t, sizeof (GETACL2res),
1879 1879 acl2_getacl_free, RPC_IDEMPOTENT,
1880 1880 acl2_getacl_getfh},
1881 1881
1882 1882 /* ACL2_SETACL = 2 */
1883 1883 {acl2_setacl,
1884 1884 xdr_SETACL2args, NULL_xdrproc_t, sizeof (SETACL2args),
1885 1885 #ifdef _LITTLE_ENDIAN
1886 1886 xdr_SETACL2res, xdr_fastSETACL2res, sizeof (SETACL2res),
1887 1887 #else
1888 1888 xdr_SETACL2res, NULL_xdrproc_t, sizeof (SETACL2res),
1889 1889 #endif
1890 1890 nullfree, RPC_MAPRESP,
1891 1891 acl2_setacl_getfh},
1892 1892
1893 1893 /* ACL2_GETATTR = 3 */
1894 1894 {acl2_getattr,
1895 1895 xdr_GETATTR2args, xdr_fastGETATTR2args, sizeof (GETATTR2args),
1896 1896 #ifdef _LITTLE_ENDIAN
1897 1897 xdr_GETATTR2res, xdr_fastGETATTR2res, sizeof (GETATTR2res),
1898 1898 #else
1899 1899 xdr_GETATTR2res, NULL_xdrproc_t, sizeof (GETATTR2res),
1900 1900 #endif
1901 1901 nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
1902 1902 acl2_getattr_getfh},
1903 1903
1904 1904 /* ACL2_ACCESS = 4 */
1905 1905 {acl2_access,
1906 1906 xdr_ACCESS2args, xdr_fastACCESS2args, sizeof (ACCESS2args),
1907 1907 #ifdef _LITTLE_ENDIAN
1908 1908 xdr_ACCESS2res, xdr_fastACCESS2res, sizeof (ACCESS2res),
1909 1909 #else
1910 1910 xdr_ACCESS2res, NULL_xdrproc_t, sizeof (ACCESS2res),
1911 1911 #endif
1912 1912 nullfree, RPC_IDEMPOTENT|RPC_MAPRESP,
1913 1913 acl2_access_getfh},
1914 1914
1915 1915 /* ACL2_GETXATTRDIR = 5 */
1916 1916 {acl2_getxattrdir,
1917 1917 xdr_GETXATTRDIR2args, NULL_xdrproc_t, sizeof (GETXATTRDIR2args),
1918 1918 xdr_GETXATTRDIR2res, NULL_xdrproc_t, sizeof (GETXATTRDIR2res),
1919 1919 nullfree, RPC_IDEMPOTENT,
1920 1920 acl2_getxattrdir_getfh},
1921 1921 };
1922 1922
1923 1923 static char *aclcallnames_v3[] = {
1924 1924 "ACL3_NULL",
1925 1925 "ACL3_GETACL",
1926 1926 "ACL3_SETACL",
1927 1927 "ACL3_GETXATTRDIR"
1928 1928 };
1929 1929
1930 1930 static struct rpcdisp acldisptab_v3[] = {
1931 1931 /*
1932 1932 * ACL VERSION 3
1933 1933 */
1934 1934
1935 1935 /* ACL3_NULL = 0 */
1936 1936 {rpc_null,
1937 1937 xdr_void, NULL_xdrproc_t, 0,
1938 1938 xdr_void, NULL_xdrproc_t, 0,
1939 1939 nullfree, RPC_IDEMPOTENT,
1940 1940 0},
1941 1941
1942 1942 /* ACL3_GETACL = 1 */
1943 1943 {acl3_getacl,
1944 1944 xdr_GETACL3args, NULL_xdrproc_t, sizeof (GETACL3args),
1945 1945 xdr_GETACL3res, NULL_xdrproc_t, sizeof (GETACL3res),
1946 1946 acl3_getacl_free, RPC_IDEMPOTENT,
1947 1947 acl3_getacl_getfh},
1948 1948
1949 1949 /* ACL3_SETACL = 2 */
1950 1950 {acl3_setacl,
1951 1951 xdr_SETACL3args, NULL_xdrproc_t, sizeof (SETACL3args),
1952 1952 xdr_SETACL3res, NULL_xdrproc_t, sizeof (SETACL3res),
1953 1953 nullfree, 0,
1954 1954 acl3_setacl_getfh},
1955 1955
1956 1956 /* ACL3_GETXATTRDIR = 3 */
1957 1957 {acl3_getxattrdir,
1958 1958 xdr_GETXATTRDIR3args, NULL_xdrproc_t, sizeof (GETXATTRDIR3args),
1959 1959 xdr_GETXATTRDIR3res, NULL_xdrproc_t, sizeof (GETXATTRDIR3res),
1960 1960 nullfree, RPC_IDEMPOTENT,
1961 1961 acl3_getxattrdir_getfh},
1962 1962 };
1963 1963
1964 1964 static struct rpc_disptable acl_disptable[] = {
1965 1965 {sizeof (acldisptab_v2) / sizeof (acldisptab_v2[0]),
1966 1966 aclcallnames_v2,
1967 1967 &aclproccnt_v2_ptr, acldisptab_v2},
1968 1968 {sizeof (acldisptab_v3) / sizeof (acldisptab_v3[0]),
1969 1969 aclcallnames_v3,
1970 1970 &aclproccnt_v3_ptr, acldisptab_v3},
1971 1971 };
1972 1972
1973 1973 static void
1974 1974 acl_dispatch(struct svc_req *req, SVCXPRT *xprt)
1975 1975 {
1976 1976 common_dispatch(req, xprt, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX,
1977 1977 "ACL", acl_disptable);
1978 1978 }
1979 1979
1980 1980 int
1981 1981 checkwin(int flavor, int window, struct svc_req *req)
1982 1982 {
1983 1983 struct authdes_cred *adc;
1984 1984
1985 1985 switch (flavor) {
1986 1986 case AUTH_DES:
1987 1987 adc = (struct authdes_cred *)req->rq_clntcred;
1988 1988 CTASSERT(sizeof (struct authdes_cred) <= RQCRED_SIZE);
1989 1989 if (adc->adc_fullname.window > window)
1990 1990 return (0);
1991 1991 break;
1992 1992
1993 1993 default:
1994 1994 break;
1995 1995 }
1996 1996 return (1);
1997 1997 }
1998 1998
1999 1999
2000 2000 /*
2001 2001 * checkauth() will check the access permission against the export
2002 2002 * information. Then map root uid/gid to appropriate uid/gid.
2003 2003 *
2004 2004 * This routine is used by NFS V3 and V2 code.
2005 2005 */
2006 2006 static int
2007 2007 checkauth(struct exportinfo *exi, struct svc_req *req, cred_t *cr, int anon_ok,
2008 2008 bool_t publicfh_ok, bool_t *ro)
2009 2009 {
2010 2010 int i, nfsflavor, rpcflavor, stat, access;
2011 2011 struct secinfo *secp;
2012 2012 caddr_t principal;
2013 2013 char buf[INET6_ADDRSTRLEN]; /* to hold both IPv4 and IPv6 addr */
2014 2014 int anon_res = 0;
2015 2015
2016 2016 uid_t uid;
2017 2017 gid_t gid;
2018 2018 uint_t ngids;
2019 2019 gid_t *gids;
2020 2020
2021 2021 /*
2022 2022 * Check for privileged port number
2023 2023 * N.B.: this assumes that we know the format of a netbuf.
2024 2024 */
2025 2025 if (nfs_portmon) {
2026 2026 struct sockaddr *ca;
2027 2027 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2028 2028
2029 2029 if (ca == NULL)
2030 2030 return (0);
2031 2031
2032 2032 if ((ca->sa_family == AF_INET &&
2033 2033 ntohs(((struct sockaddr_in *)ca)->sin_port) >=
2034 2034 IPPORT_RESERVED) ||
2035 2035 (ca->sa_family == AF_INET6 &&
2036 2036 ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
2037 2037 IPPORT_RESERVED)) {
2038 2038 cmn_err(CE_NOTE,
2039 2039 "nfs_server: client %s%ssent NFS request from "
2040 2040 "unprivileged port",
2041 2041 client_name(req), client_addr(req, buf));
2042 2042 return (0);
2043 2043 }
2044 2044 }
2045 2045
2046 2046 /*
2047 2047 * return 1 on success or 0 on failure
2048 2048 */
2049 2049 stat = sec_svc_getcred(req, cr, &principal, &nfsflavor);
2050 2050
2051 2051 /*
2052 2052 * A failed AUTH_UNIX sec_svc_getcred() implies we couldn't set
2053 2053 * the credentials; below we map that to anonymous.
2054 2054 */
2055 2055 if (!stat && nfsflavor != AUTH_UNIX) {
2056 2056 cmn_err(CE_NOTE,
2057 2057 "nfs_server: couldn't get unix cred for %s",
2058 2058 client_name(req));
2059 2059 return (0);
2060 2060 }
2061 2061
2062 2062 /*
2063 2063 * Short circuit checkauth() on operations that support the
2064 2064 * public filehandle, and if the request for that operation
2065 2065 * is using the public filehandle. Note that we must call
2066 2066 * sec_svc_getcred() first so that xp_cookie is set to the
2067 2067 * right value. Normally xp_cookie is just the RPC flavor
2068 2068 * of the the request, but in the case of RPCSEC_GSS it
2069 2069 * could be a pseudo flavor.
2070 2070 */
2071 2071 if (publicfh_ok)
2072 2072 return (1);
2073 2073
2074 2074 rpcflavor = req->rq_cred.oa_flavor;
2075 2075 /*
2076 2076 * Check if the auth flavor is valid for this export
2077 2077 */
2078 2078 access = nfsauth_access(exi, req, cr, &uid, &gid, &ngids, &gids);
2079 2079 if (access & NFSAUTH_DROP)
2080 2080 return (-1); /* drop the request */
2081 2081
2082 2082 if (access & NFSAUTH_RO)
2083 2083 *ro = TRUE;
2084 2084
2085 2085 if (access & NFSAUTH_DENIED) {
2086 2086 /*
2087 2087 * If anon_ok == 1 and we got NFSAUTH_DENIED, it was
2088 2088 * probably due to the flavor not matching during
2089 2089 * the mount attempt. So map the flavor to AUTH_NONE
2090 2090 * so that the credentials get mapped to the anonymous
2091 2091 * user.
2092 2092 */
2093 2093 if (anon_ok == 1)
2094 2094 rpcflavor = AUTH_NONE;
2095 2095 else
2096 2096 return (0); /* deny access */
2097 2097
2098 2098 } else if (access & NFSAUTH_MAPNONE) {
2099 2099 /*
2100 2100 * Access was granted even though the flavor mismatched
2101 2101 * because AUTH_NONE was one of the exported flavors.
2102 2102 */
2103 2103 rpcflavor = AUTH_NONE;
2104 2104
2105 2105 } else if (access & NFSAUTH_WRONGSEC) {
2106 2106 /*
2107 2107 * NFSAUTH_WRONGSEC is used for NFSv4. If we get here,
2108 2108 * it means a client ignored the list of allowed flavors
2109 2109 * returned via the MOUNT protocol. So we just disallow it!
2110 2110 */
2111 2111 return (0);
2112 2112 }
2113 2113
2114 2114 if (rpcflavor != AUTH_SYS)
2115 2115 kmem_free(gids, ngids * sizeof (gid_t));
2116 2116
2117 2117 switch (rpcflavor) {
2118 2118 case AUTH_NONE:
2119 2119 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2120 2120 exi->exi_export.ex_anon);
2121 2121 (void) crsetgroups(cr, 0, NULL);
2122 2122 break;
2123 2123
2124 2124 case AUTH_UNIX:
2125 2125 if (!stat || crgetuid(cr) == 0 && !(access & NFSAUTH_UIDMAP)) {
2126 2126 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2127 2127 exi->exi_export.ex_anon);
2128 2128 (void) crsetgroups(cr, 0, NULL);
2129 2129 } else if (crgetuid(cr) == 0 && access & NFSAUTH_ROOT) {
2130 2130 /*
2131 2131 * It is root, so apply rootid to get real UID
2132 2132 * Find the secinfo structure. We should be able
2133 2133 * to find it by the time we reach here.
2134 2134 * nfsauth_access() has done the checking.
2135 2135 */
2136 2136 secp = NULL;
2137 2137 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2138 2138 struct secinfo *sptr;
2139 2139 sptr = &exi->exi_export.ex_secinfo[i];
2140 2140 if (sptr->s_secinfo.sc_nfsnum == nfsflavor) {
2141 2141 secp = sptr;
2142 2142 break;
2143 2143 }
2144 2144 }
2145 2145 if (secp != NULL) {
2146 2146 (void) crsetugid(cr, secp->s_rootid,
2147 2147 secp->s_rootid);
2148 2148 (void) crsetgroups(cr, 0, NULL);
2149 2149 }
2150 2150 } else if (crgetuid(cr) != uid || crgetgid(cr) != gid) {
2151 2151 if (crsetugid(cr, uid, gid) != 0)
2152 2152 anon_res = crsetugid(cr,
2153 2153 exi->exi_export.ex_anon,
2154 2154 exi->exi_export.ex_anon);
2155 2155 (void) crsetgroups(cr, 0, NULL);
2156 2156 } else if (access & NFSAUTH_GROUPS) {
2157 2157 (void) crsetgroups(cr, ngids, gids);
2158 2158 }
2159 2159
2160 2160 kmem_free(gids, ngids * sizeof (gid_t));
2161 2161
2162 2162 break;
2163 2163
2164 2164 case AUTH_DES:
2165 2165 case RPCSEC_GSS:
2166 2166 /*
2167 2167 * Find the secinfo structure. We should be able
2168 2168 * to find it by the time we reach here.
2169 2169 * nfsauth_access() has done the checking.
2170 2170 */
2171 2171 secp = NULL;
2172 2172 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2173 2173 if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2174 2174 nfsflavor) {
2175 2175 secp = &exi->exi_export.ex_secinfo[i];
2176 2176 break;
2177 2177 }
2178 2178 }
2179 2179
2180 2180 if (!secp) {
2181 2181 cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2182 2182 "no secinfo data for flavor %d",
2183 2183 client_name(req), client_addr(req, buf),
2184 2184 nfsflavor);
2185 2185 return (0);
2186 2186 }
2187 2187
2188 2188 if (!checkwin(rpcflavor, secp->s_window, req)) {
2189 2189 cmn_err(CE_NOTE,
2190 2190 "nfs_server: client %s%sused invalid "
2191 2191 "auth window value",
2192 2192 client_name(req), client_addr(req, buf));
2193 2193 return (0);
2194 2194 }
2195 2195
2196 2196 /*
2197 2197 * Map root principals listed in the share's root= list to root,
2198 2198 * and map any others principals that were mapped to root by RPC
2199 2199 * to anon.
2200 2200 */
2201 2201 if (principal && sec_svc_inrootlist(rpcflavor, principal,
2202 2202 secp->s_rootcnt, secp->s_rootnames)) {
2203 2203 if (crgetuid(cr) == 0 && secp->s_rootid == 0)
2204 2204 return (1);
2205 2205
2206 2206
2207 2207 (void) crsetugid(cr, secp->s_rootid, secp->s_rootid);
2208 2208
2209 2209 /*
2210 2210 * NOTE: If and when kernel-land privilege tracing is
2211 2211 * added this may have to be replaced with code that
2212 2212 * retrieves root's supplementary groups (e.g., using
2213 2213 * kgss_get_group_info(). In the meantime principals
2214 2214 * mapped to uid 0 get all privileges, so setting cr's
2215 2215 * supplementary groups for them does nothing.
2216 2216 */
2217 2217 (void) crsetgroups(cr, 0, NULL);
2218 2218
2219 2219 return (1);
2220 2220 }
2221 2221
2222 2222 /*
2223 2223 * Not a root princ, or not in root list, map UID 0/nobody to
2224 2224 * the anon ID for the share. (RPC sets cr's UIDs and GIDs to
2225 2225 * UID_NOBODY and GID_NOBODY, respectively.)
2226 2226 */
2227 2227 if (crgetuid(cr) != 0 &&
2228 2228 (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2229 2229 return (1);
2230 2230
2231 2231 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2232 2232 exi->exi_export.ex_anon);
2233 2233 (void) crsetgroups(cr, 0, NULL);
2234 2234 break;
2235 2235 default:
2236 2236 return (0);
2237 2237 } /* switch on rpcflavor */
2238 2238
2239 2239 /*
2240 2240 * Even if anon access is disallowed via ex_anon == -1, we allow
2241 2241 * this access if anon_ok is set. So set creds to the default
2242 2242 * "nobody" id.
2243 2243 */
2244 2244 if (anon_res != 0) {
2245 2245 if (anon_ok == 0) {
2246 2246 cmn_err(CE_NOTE,
2247 2247 "nfs_server: client %s%ssent wrong "
2248 2248 "authentication for %s",
2249 2249 client_name(req), client_addr(req, buf),
2250 2250 exi->exi_export.ex_path ?
2251 2251 exi->exi_export.ex_path : "?");
2252 2252 return (0);
2253 2253 }
2254 2254
2255 2255 if (crsetugid(cr, UID_NOBODY, GID_NOBODY) != 0)
2256 2256 return (0);
2257 2257 }
2258 2258
2259 2259 return (1);
2260 2260 }
2261 2261
2262 2262 /*
2263 2263 * returns 0 on failure, -1 on a drop, -2 on wrong security flavor,
2264 2264 * and 1 on success
2265 2265 */
2266 2266 int
2267 2267 checkauth4(struct compound_state *cs, struct svc_req *req)
2268 2268 {
2269 2269 int i, rpcflavor, access;
2270 2270 struct secinfo *secp;
2271 2271 char buf[MAXHOST + 1];
2272 2272 int anon_res = 0, nfsflavor;
2273 2273 struct exportinfo *exi;
2274 2274 cred_t *cr;
2275 2275 caddr_t principal;
2276 2276
2277 2277 uid_t uid;
2278 2278 gid_t gid;
2279 2279 uint_t ngids;
2280 2280 gid_t *gids;
2281 2281
2282 2282 exi = cs->exi;
2283 2283 cr = cs->cr;
2284 2284 principal = cs->principal;
2285 2285 nfsflavor = cs->nfsflavor;
2286 2286
2287 2287 ASSERT(cr != NULL);
2288 2288
2289 2289 rpcflavor = req->rq_cred.oa_flavor;
2290 2290 cs->access &= ~CS_ACCESS_LIMITED;
2291 2291
2292 2292 /*
2293 2293 * Check for privileged port number
2294 2294 * N.B.: this assumes that we know the format of a netbuf.
2295 2295 */
2296 2296 if (nfs_portmon) {
2297 2297 struct sockaddr *ca;
2298 2298 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2299 2299
2300 2300 if (ca == NULL)
2301 2301 return (0);
2302 2302
2303 2303 if ((ca->sa_family == AF_INET &&
2304 2304 ntohs(((struct sockaddr_in *)ca)->sin_port) >=
2305 2305 IPPORT_RESERVED) ||
2306 2306 (ca->sa_family == AF_INET6 &&
2307 2307 ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
2308 2308 IPPORT_RESERVED)) {
2309 2309 cmn_err(CE_NOTE,
2310 2310 "nfs_server: client %s%ssent NFSv4 request from "
2311 2311 "unprivileged port",
2312 2312 client_name(req), client_addr(req, buf));
2313 2313 return (0);
2314 2314 }
2315 2315 }
2316 2316
2317 2317 /*
2318 2318 * Check the access right per auth flavor on the vnode of
2319 2319 * this export for the given request.
2320 2320 */
2321 2321 access = nfsauth4_access(cs->exi, cs->vp, req, cr, &uid, &gid, &ngids,
2322 2322 &gids);
2323 2323
2324 2324 if (access & NFSAUTH_WRONGSEC)
2325 2325 return (-2); /* no access for this security flavor */
2326 2326
2327 2327 if (access & NFSAUTH_DROP)
2328 2328 return (-1); /* drop the request */
2329 2329
2330 2330 if (access & NFSAUTH_DENIED) {
2331 2331
2332 2332 if (exi->exi_export.ex_seccnt > 0)
2333 2333 return (0); /* deny access */
2334 2334
2335 2335 } else if (access & NFSAUTH_LIMITED) {
2336 2336
2337 2337 cs->access |= CS_ACCESS_LIMITED;
2338 2338
2339 2339 } else if (access & NFSAUTH_MAPNONE) {
2340 2340 /*
2341 2341 * Access was granted even though the flavor mismatched
2342 2342 * because AUTH_NONE was one of the exported flavors.
2343 2343 */
2344 2344 rpcflavor = AUTH_NONE;
2345 2345 }
2346 2346
2347 2347 /*
2348 2348 * XXX probably need to redo some of it for nfsv4?
2349 2349 * return 1 on success or 0 on failure
2350 2350 */
2351 2351
2352 2352 if (rpcflavor != AUTH_SYS)
2353 2353 kmem_free(gids, ngids * sizeof (gid_t));
2354 2354
2355 2355 switch (rpcflavor) {
2356 2356 case AUTH_NONE:
2357 2357 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2358 2358 exi->exi_export.ex_anon);
2359 2359 (void) crsetgroups(cr, 0, NULL);
2360 2360 break;
2361 2361
2362 2362 case AUTH_UNIX:
2363 2363 if (crgetuid(cr) == 0 && !(access & NFSAUTH_UIDMAP)) {
2364 2364 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2365 2365 exi->exi_export.ex_anon);
2366 2366 (void) crsetgroups(cr, 0, NULL);
2367 2367 } else if (crgetuid(cr) == 0 && access & NFSAUTH_ROOT) {
2368 2368 /*
2369 2369 * It is root, so apply rootid to get real UID
2370 2370 * Find the secinfo structure. We should be able
2371 2371 * to find it by the time we reach here.
2372 2372 * nfsauth_access() has done the checking.
2373 2373 */
2374 2374 secp = NULL;
2375 2375 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2376 2376 struct secinfo *sptr;
2377 2377 sptr = &exi->exi_export.ex_secinfo[i];
2378 2378 if (sptr->s_secinfo.sc_nfsnum == nfsflavor) {
2379 2379 secp = &exi->exi_export.ex_secinfo[i];
2380 2380 break;
2381 2381 }
2382 2382 }
2383 2383 if (secp != NULL) {
2384 2384 (void) crsetugid(cr, secp->s_rootid,
2385 2385 secp->s_rootid);
2386 2386 (void) crsetgroups(cr, 0, NULL);
2387 2387 }
2388 2388 } else if (crgetuid(cr) != uid || crgetgid(cr) != gid) {
2389 2389 if (crsetugid(cr, uid, gid) != 0)
2390 2390 anon_res = crsetugid(cr,
2391 2391 exi->exi_export.ex_anon,
2392 2392 exi->exi_export.ex_anon);
2393 2393 (void) crsetgroups(cr, 0, NULL);
2394 2394 } if (access & NFSAUTH_GROUPS) {
2395 2395 (void) crsetgroups(cr, ngids, gids);
2396 2396 }
2397 2397
2398 2398 kmem_free(gids, ngids * sizeof (gid_t));
2399 2399
2400 2400 break;
2401 2401
2402 2402 default:
2403 2403 /*
2404 2404 * Find the secinfo structure. We should be able
2405 2405 * to find it by the time we reach here.
2406 2406 * nfsauth_access() has done the checking.
2407 2407 */
2408 2408 secp = NULL;
2409 2409 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2410 2410 if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2411 2411 nfsflavor) {
2412 2412 secp = &exi->exi_export.ex_secinfo[i];
2413 2413 break;
2414 2414 }
2415 2415 }
2416 2416
2417 2417 if (!secp) {
2418 2418 cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2419 2419 "no secinfo data for flavor %d",
2420 2420 client_name(req), client_addr(req, buf),
2421 2421 nfsflavor);
2422 2422 return (0);
2423 2423 }
2424 2424
2425 2425 if (!checkwin(rpcflavor, secp->s_window, req)) {
2426 2426 cmn_err(CE_NOTE,
2427 2427 "nfs_server: client %s%sused invalid "
2428 2428 "auth window value",
2429 2429 client_name(req), client_addr(req, buf));
2430 2430 return (0);
2431 2431 }
2432 2432
2433 2433 /*
2434 2434 * Map root principals listed in the share's root= list to root,
2435 2435 * and map any others principals that were mapped to root by RPC
2436 2436 * to anon. If not going to anon, set to rootid (root_mapping).
2437 2437 */
2438 2438 if (principal && sec_svc_inrootlist(rpcflavor, principal,
2439 2439 secp->s_rootcnt, secp->s_rootnames)) {
2440 2440 if (crgetuid(cr) == 0 && secp->s_rootid == 0)
2441 2441 return (1);
2442 2442
2443 2443 (void) crsetugid(cr, secp->s_rootid, secp->s_rootid);
2444 2444
2445 2445 /*
2446 2446 * NOTE: If and when kernel-land privilege tracing is
2447 2447 * added this may have to be replaced with code that
2448 2448 * retrieves root's supplementary groups (e.g., using
2449 2449 * kgss_get_group_info(). In the meantime principals
2450 2450 * mapped to uid 0 get all privileges, so setting cr's
2451 2451 * supplementary groups for them does nothing.
2452 2452 */
2453 2453 (void) crsetgroups(cr, 0, NULL);
2454 2454
2455 2455 return (1);
2456 2456 }
2457 2457
2458 2458 /*
2459 2459 * Not a root princ, or not in root list, map UID 0/nobody to
2460 2460 * the anon ID for the share. (RPC sets cr's UIDs and GIDs to
2461 2461 * UID_NOBODY and GID_NOBODY, respectively.)
2462 2462 */
2463 2463 if (crgetuid(cr) != 0 &&
2464 2464 (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2465 2465 return (1);
2466 2466
2467 2467 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2468 2468 exi->exi_export.ex_anon);
2469 2469 (void) crsetgroups(cr, 0, NULL);
2470 2470 break;
2471 2471 } /* switch on rpcflavor */
2472 2472
2473 2473 /*
2474 2474 * Even if anon access is disallowed via ex_anon == -1, we allow
2475 2475 * this access if anon_ok is set. So set creds to the default
2476 2476 * "nobody" id.
2477 2477 */
2478 2478
2479 2479 if (anon_res != 0) {
2480 2480 cmn_err(CE_NOTE,
2481 2481 "nfs_server: client %s%ssent wrong "
2482 2482 "authentication for %s",
2483 2483 client_name(req), client_addr(req, buf),
2484 2484 exi->exi_export.ex_path ?
2485 2485 exi->exi_export.ex_path : "?");
2486 2486 return (0);
2487 2487 }
2488 2488
2489 2489 return (1);
2490 2490 }
2491 2491
2492 2492
2493 2493 static char *
2494 2494 client_name(struct svc_req *req)
2495 2495 {
2496 2496 char *hostname = NULL;
2497 2497
2498 2498 /*
2499 2499 * If it's a Unix cred then use the
2500 2500 * hostname from the credential.
2501 2501 */
2502 2502 if (req->rq_cred.oa_flavor == AUTH_UNIX) {
2503 2503 hostname = ((struct authunix_parms *)
2504 2504 req->rq_clntcred)->aup_machname;
2505 2505 }
2506 2506 if (hostname == NULL)
2507 2507 hostname = "";
2508 2508
2509 2509 return (hostname);
2510 2510 }
2511 2511
2512 2512 static char *
2513 2513 client_addr(struct svc_req *req, char *buf)
2514 2514 {
2515 2515 struct sockaddr *ca;
2516 2516 uchar_t *b;
2517 2517 char *frontspace = "";
2518 2518
2519 2519 /*
2520 2520 * We assume we are called in tandem with client_name and the
2521 2521 * format string looks like "...client %s%sblah blah..."
2522 2522 *
2523 2523 * If it's a Unix cred then client_name returned
2524 2524 * a host name, so we need insert a space between host name
2525 2525 * and IP address.
2526 2526 */
2527 2527 if (req->rq_cred.oa_flavor == AUTH_UNIX)
2528 2528 frontspace = " ";
2529 2529
2530 2530 /*
2531 2531 * Convert the caller's IP address to a dotted string
2532 2532 */
2533 2533 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2534 2534
2535 2535 if (ca->sa_family == AF_INET) {
2536 2536 b = (uchar_t *)&((struct sockaddr_in *)ca)->sin_addr;
2537 2537 (void) sprintf(buf, "%s(%d.%d.%d.%d) ", frontspace,
2538 2538 b[0] & 0xFF, b[1] & 0xFF, b[2] & 0xFF, b[3] & 0xFF);
2539 2539 } else if (ca->sa_family == AF_INET6) {
2540 2540 struct sockaddr_in6 *sin6;
2541 2541 sin6 = (struct sockaddr_in6 *)ca;
2542 2542 (void) kinet_ntop6((uchar_t *)&sin6->sin6_addr,
2543 2543 buf, INET6_ADDRSTRLEN);
2544 2544
2545 2545 } else {
2546 2546
2547 2547 /*
2548 2548 * No IP address to print. If there was a host name
2549 2549 * printed, then we print a space.
2550 2550 */
2551 2551 (void) sprintf(buf, frontspace);
2552 2552 }
2553 2553
2554 2554 return (buf);
2555 2555 }
2556 2556
2557 2557 /*
2558 2558 * NFS Server initialization routine. This routine should only be called
2559 2559 * once. It performs the following tasks:
2560 2560 * - Call sub-initialization routines (localize access to variables)
2561 2561 * - Initialize all locks
2562 2562 * - initialize the version 3 write verifier
2563 2563 */
2564 2564 void
2565 2565 nfs_srvinit(void)
2566 2566 {
2567 2567
2568 2568 /* Truly global stuff in this module (not per zone) */
2569 2569 rw_init(&nfssrv_globals_rwl, NULL, RW_DEFAULT, NULL);
2570 2570 list_create(&nfssrv_globals_list, sizeof (nfs_globals_t),
2571 2571 offsetof (nfs_globals_t, nfs_g_link));
2572 2572
2573 2573 /* The order here is important */
2574 2574 nfs_exportinit();
2575 2575 rfs_srvrinit();
2576 2576 rfs3_srvrinit();
2577 2577 rfs4_srvrinit();
2578 2578 nfsauth_init();
2579 2579
2580 2580 /*
2581 2581 * NFS server zone-specific global variables
2582 2582 * Note the zone_init is called for the GZ here.
2583 2583 */
2584 2584 zone_key_create(&nfssrv_zone_key, nfs_server_zone_init,
2585 2585 nfs_server_zone_shutdown, nfs_server_zone_fini);
2586 2586 }
2587 2587
2588 2588 /*
2589 2589 * NFS Server finalization routine. This routine is called to cleanup the
2590 2590 * initialization work previously performed if the NFS server module could
2591 2591 * not be loaded correctly.
2592 2592 */
2593 2593 void
2594 2594 nfs_srvfini(void)
2595 2595 {
2596 2596
2597 2597 /*
2598 2598 * NFS server zone-specific global variables
2599 2599 * Note the zone_fini is called for the GZ here.
2600 2600 */
2601 2601 (void) zone_key_delete(nfssrv_zone_key);
2602 2602
2603 2603 /* The order here is important (reverse of init) */
2604 2604 nfsauth_fini();
2605 2605 rfs4_srvrfini();
2606 2606 rfs3_srvrfini();
2607 2607 rfs_srvrfini();
2608 2608 nfs_exportfini();
2609 2609
2610 2610 /* Truly global stuff in this module (not per zone) */
2611 2611 list_destroy(&nfssrv_globals_list);
2612 2612 rw_destroy(&nfssrv_globals_rwl);
2613 2613 }
2614 2614
2615 2615 /*
2616 2616 * Zone init, shutdown, fini functions for the NFS server
2617 2617 *
2618 2618 * This design is careful to create the entire hierarhcy of
2619 2619 * NFS server "globals" (including those created by various
2620 2620 * per-module *_zone_init functions, etc.) so that all these
2621 2621 * objects have exactly the same lifetime.
2622 2622 *
2623 2623 * These objects are also kept on a list for two reasons:
2624 2624 * 1: It makes finding these in mdb _much_ easier.
2625 2625 * 2: It allows operating across all zone globals for
2626 2626 * functions like nfs_auth.c:exi_cache_reclaim
2627 2627 */
2628 2628 static void *
2629 2629 nfs_server_zone_init(zoneid_t zoneid)
2630 2630 {
2631 2631 nfs_globals_t *ng;
2632 2632
2633 2633 ng = kmem_zalloc(sizeof (*ng), KM_SLEEP);
2634 2634
2635 2635 ng->nfs_versmin = NFS_VERSMIN_DEFAULT;
2636 2636 ng->nfs_versmax = NFS_VERSMAX_DEFAULT;
2637 2637
2638 2638 /* Init the stuff to control start/stop */
2639 2639 ng->nfs_server_upordown = NFS_SERVER_STOPPED;
2640 2640 mutex_init(&ng->nfs_server_upordown_lock, NULL, MUTEX_DEFAULT, NULL);
2641 2641 cv_init(&ng->nfs_server_upordown_cv, NULL, CV_DEFAULT, NULL);
2642 2642 mutex_init(&ng->rdma_wait_mutex, NULL, MUTEX_DEFAULT, NULL);
2643 2643 cv_init(&ng->rdma_wait_cv, NULL, CV_DEFAULT, NULL);
2644 2644
2645 2645 ng->nfs_zoneid = zoneid;
2646 2646
2647 2647 /*
2648 2648 * Order here is important.
2649 2649 * export init must precede srv init calls.
2650 2650 */
2651 2651 nfs_export_zone_init(ng);
2652 2652 rfs_srv_zone_init(ng);
2653 2653 rfs3_srv_zone_init(ng);
2654 2654 rfs4_srv_zone_init(ng);
2655 2655 nfsauth_zone_init(ng);
2656 2656
2657 2657 rw_enter(&nfssrv_globals_rwl, RW_WRITER);
2658 2658 list_insert_tail(&nfssrv_globals_list, ng);
2659 2659 rw_exit(&nfssrv_globals_rwl);
2660 2660
2661 2661 return (ng);
2662 2662 }
2663 2663
2664 2664 /* ARGSUSED */
2665 2665 static void
2666 2666 nfs_server_zone_shutdown(zoneid_t zoneid, void *data)
2667 2667 {
2668 2668 nfs_globals_t *ng;
2669 2669
2670 2670 ng = (nfs_globals_t *)data;
2671 2671
2672 2672 /*
2673 2673 * Order is like _fini, but only
2674 2674 * some modules need this hook.
2675 2675 */
2676 2676 nfsauth_zone_shutdown(ng);
2677 2677 nfs_export_zone_shutdown(ng);
2678 2678 }
2679 2679
2680 2680 /* ARGSUSED */
2681 2681 static void
2682 2682 nfs_server_zone_fini(zoneid_t zoneid, void *data)
2683 2683 {
2684 2684 nfs_globals_t *ng;
2685 2685
2686 2686 ng = (nfs_globals_t *)data;
2687 2687
2688 2688 rw_enter(&nfssrv_globals_rwl, RW_WRITER);
2689 2689 list_remove(&nfssrv_globals_list, ng);
2690 2690 rw_exit(&nfssrv_globals_rwl);
2691 2691
2692 2692 /*
2693 2693 * Order here is important.
2694 2694 * reverse order from init
2695 2695 */
2696 2696 nfsauth_zone_fini(ng);
2697 2697 rfs4_srv_zone_fini(ng);
2698 2698 rfs3_srv_zone_fini(ng);
2699 2699 rfs_srv_zone_fini(ng);
2700 2700 nfs_export_zone_fini(ng);
2701 2701
2702 2702 mutex_destroy(&ng->nfs_server_upordown_lock);
2703 2703 cv_destroy(&ng->nfs_server_upordown_cv);
2704 2704 mutex_destroy(&ng->rdma_wait_mutex);
2705 2705 cv_destroy(&ng->rdma_wait_cv);
2706 2706
2707 2707 kmem_free(ng, sizeof (*ng));
2708 2708 }
2709 2709
2710 2710 /*
2711 2711 * Set up an iovec array of up to cnt pointers.
2712 2712 */
2713 2713 void
2714 2714 mblk_to_iov(mblk_t *m, int cnt, struct iovec *iovp)
2715 2715 {
2716 2716 while (m != NULL && cnt-- > 0) {
2717 2717 iovp->iov_base = (caddr_t)m->b_rptr;
2718 2718 iovp->iov_len = (m->b_wptr - m->b_rptr);
2719 2719 iovp++;
2720 2720 m = m->b_cont;
2721 2721 }
2722 2722 }
2723 2723
2724 2724 /*
2725 2725 * Common code between NFS Version 2 and NFS Version 3 for the public
2726 2726 * filehandle multicomponent lookups.
2727 2727 */
2728 2728
2729 2729 /*
2730 2730 * Public filehandle evaluation of a multi-component lookup, following
2731 2731 * symbolic links, if necessary. This may result in a vnode in another
2732 2732 * filesystem, which is OK as long as the other filesystem is exported.
2733 2733 *
2734 2734 * Note that the exi will be set either to NULL or a new reference to the
2735 2735 * exportinfo struct that corresponds to the vnode of the multi-component path.
2736 2736 * It is the callers responsibility to release this reference.
2737 2737 */
2738 2738 int
2739 2739 rfs_publicfh_mclookup(char *p, vnode_t *dvp, cred_t *cr, vnode_t **vpp,
2740 2740 struct exportinfo **exi, struct sec_ol *sec)
2741 2741 {
2742 2742 int pathflag;
2743 2743 vnode_t *mc_dvp = NULL;
2744 2744 vnode_t *realvp;
2745 2745 int error;
2746 2746
2747 2747 *exi = NULL;
2748 2748
2749 2749 /*
2750 2750 * check if the given path is a url or native path. Since p is
2751 2751 * modified by MCLpath(), it may be empty after returning from
2752 2752 * there, and should be checked.
2753 2753 */
2754 2754 if ((pathflag = MCLpath(&p)) == -1)
2755 2755 return (EIO);
2756 2756
2757 2757 /*
2758 2758 * If pathflag is SECURITY_QUERY, turn the SEC_QUERY bit
2759 2759 * on in sec->sec_flags. This bit will later serve as an
2760 2760 * indication in makefh_ol() or makefh3_ol() to overload the
2761 2761 * filehandle to contain the sec modes used by the server for
2762 2762 * the path.
2763 2763 */
2764 2764 if (pathflag == SECURITY_QUERY) {
2765 2765 if ((sec->sec_index = (uint_t)(*p)) > 0) {
2766 2766 sec->sec_flags |= SEC_QUERY;
2767 2767 p++;
2768 2768 if ((pathflag = MCLpath(&p)) == -1)
2769 2769 return (EIO);
2770 2770 } else {
2771 2771 cmn_err(CE_NOTE,
2772 2772 "nfs_server: invalid security index %d, "
2773 2773 "violating WebNFS SNEGO protocol.", sec->sec_index);
2774 2774 return (EIO);
2775 2775 }
2776 2776 }
2777 2777
2778 2778 if (p[0] == '\0') {
2779 2779 error = ENOENT;
2780 2780 goto publicfh_done;
2781 2781 }
2782 2782
2783 2783 error = rfs_pathname(p, &mc_dvp, vpp, dvp, cr, pathflag);
2784 2784
2785 2785 /*
2786 2786 * If name resolves to "/" we get EINVAL since we asked for
2787 2787 * the vnode of the directory that the file is in. Try again
2788 2788 * with NULL directory vnode.
2789 2789 */
2790 2790 if (error == EINVAL) {
2791 2791 error = rfs_pathname(p, NULL, vpp, dvp, cr, pathflag);
2792 2792 if (!error) {
2793 2793 ASSERT(*vpp != NULL);
2794 2794 if ((*vpp)->v_type == VDIR) {
2795 2795 VN_HOLD(*vpp);
2796 2796 mc_dvp = *vpp;
2797 2797 } else {
2798 2798 /*
2799 2799 * This should not happen, the filesystem is
2800 2800 * in an inconsistent state. Fail the lookup
2801 2801 * at this point.
2802 2802 */
2803 2803 VN_RELE(*vpp);
2804 2804 error = EINVAL;
2805 2805 }
2806 2806 }
2807 2807 }
2808 2808
2809 2809 if (error)
2810 2810 goto publicfh_done;
2811 2811
2812 2812 if (*vpp == NULL) {
2813 2813 error = ENOENT;
2814 2814 goto publicfh_done;
2815 2815 }
2816 2816
2817 2817 ASSERT(mc_dvp != NULL);
2818 2818 ASSERT(*vpp != NULL);
2819 2819
2820 2820 if ((*vpp)->v_type == VDIR) {
2821 2821 do {
2822 2822 /*
2823 2823 * *vpp may be an AutoFS node, so we perform
2824 2824 * a VOP_ACCESS() to trigger the mount of the intended
2825 2825 * filesystem, so we can perform the lookup in the
2826 2826 * intended filesystem.
2827 2827 */
2828 2828 (void) VOP_ACCESS(*vpp, 0, 0, cr, NULL);
2829 2829
2830 2830 /*
2831 2831 * If vnode is covered, get the
2832 2832 * the topmost vnode.
2833 2833 */
2834 2834 if (vn_mountedvfs(*vpp) != NULL) {
2835 2835 error = traverse(vpp);
2836 2836 if (error) {
2837 2837 VN_RELE(*vpp);
2838 2838 goto publicfh_done;
2839 2839 }
2840 2840 }
2841 2841
2842 2842 if (VOP_REALVP(*vpp, &realvp, NULL) == 0 &&
2843 2843 realvp != *vpp) {
2844 2844 /*
2845 2845 * If realvp is different from *vpp
2846 2846 * then release our reference on *vpp, so that
2847 2847 * the export access check be performed on the
2848 2848 * real filesystem instead.
2849 2849 */
2850 2850 VN_HOLD(realvp);
2851 2851 VN_RELE(*vpp);
2852 2852 *vpp = realvp;
2853 2853 } else {
2854 2854 break;
2855 2855 }
2856 2856 /* LINTED */
2857 2857 } while (TRUE);
2858 2858
2859 2859 /*
2860 2860 * Let nfs_vptexi() figure what the real parent is.
2861 2861 */
2862 2862 VN_RELE(mc_dvp);
2863 2863 mc_dvp = NULL;
2864 2864
2865 2865 } else {
2866 2866 /*
2867 2867 * If vnode is covered, get the
2868 2868 * the topmost vnode.
2869 2869 */
2870 2870 if (vn_mountedvfs(mc_dvp) != NULL) {
2871 2871 error = traverse(&mc_dvp);
2872 2872 if (error) {
2873 2873 VN_RELE(*vpp);
2874 2874 goto publicfh_done;
2875 2875 }
2876 2876 }
2877 2877
2878 2878 if (VOP_REALVP(mc_dvp, &realvp, NULL) == 0 &&
2879 2879 realvp != mc_dvp) {
2880 2880 /*
2881 2881 * *vpp is a file, obtain realvp of the parent
2882 2882 * directory vnode.
2883 2883 */
2884 2884 VN_HOLD(realvp);
2885 2885 VN_RELE(mc_dvp);
2886 2886 mc_dvp = realvp;
2887 2887 }
2888 2888 }
2889 2889
2890 2890 /*
2891 2891 * The pathname may take us from the public filesystem to another.
2892 2892 * If that's the case then just set the exportinfo to the new export
2893 2893 * and build filehandle for it. Thanks to per-access checking there's
2894 2894 * no security issues with doing this. If the client is not allowed
2895 2895 * access to this new export then it will get an access error when it
2896 2896 * tries to use the filehandle
2897 2897 */
2898 2898 if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2899 2899 VN_RELE(*vpp);
2900 2900 goto publicfh_done;
2901 2901 }
2902 2902
2903 2903 /*
2904 2904 * Not allowed access to pseudo exports.
2905 2905 */
2906 2906 if (PSEUDO(*exi)) {
2907 2907 error = ENOENT;
2908 2908 VN_RELE(*vpp);
2909 2909 goto publicfh_done;
2910 2910 }
2911 2911
2912 2912 /*
2913 2913 * Do a lookup for the index file. We know the index option doesn't
2914 2914 * allow paths through handling in the share command, so mc_dvp will
2915 2915 * be the parent for the index file vnode, if its present. Use
2916 2916 * temporary pointers to preserve and reuse the vnode pointers of the
2917 2917 * original directory in case there's no index file. Note that the
2918 2918 * index file is a native path, and should not be interpreted by
2919 2919 * the URL parser in rfs_pathname()
2920 2920 */
2921 2921 if (((*exi)->exi_export.ex_flags & EX_INDEX) &&
2922 2922 ((*vpp)->v_type == VDIR) && (pathflag == URLPATH)) {
2923 2923 vnode_t *tvp, *tmc_dvp; /* temporary vnode pointers */
2924 2924
2925 2925 tmc_dvp = mc_dvp;
2926 2926 mc_dvp = tvp = *vpp;
2927 2927
2928 2928 error = rfs_pathname((*exi)->exi_export.ex_index, NULL, vpp,
2929 2929 mc_dvp, cr, NATIVEPATH);
2930 2930
2931 2931 if (error == ENOENT) {
2932 2932 *vpp = tvp;
2933 2933 mc_dvp = tmc_dvp;
2934 2934 error = 0;
2935 2935 } else { /* ok or error other than ENOENT */
2936 2936 if (tmc_dvp)
2937 2937 VN_RELE(tmc_dvp);
2938 2938 if (error)
2939 2939 goto publicfh_done;
|
↓ open down ↓ |
2939 lines elided |
↑ open up ↑ |
2940 2940
2941 2941 /*
2942 2942 * Found a valid vp for index "filename". Sanity check
2943 2943 * for odd case where a directory is provided as index
2944 2944 * option argument and leads us to another filesystem
2945 2945 */
2946 2946
2947 2947 /* Release the reference on the old exi value */
2948 2948 ASSERT(*exi != NULL);
2949 2949 exi_rele(*exi);
2950 + *exi = NULL;
2950 2951
2951 2952 if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2952 2953 VN_RELE(*vpp);
2953 2954 goto publicfh_done;
2954 2955 }
2956 + /* Have a new *exi */
2955 2957 }
2956 2958 }
2957 2959
2958 2960 publicfh_done:
2959 2961 if (mc_dvp)
2960 2962 VN_RELE(mc_dvp);
2961 2963
2962 2964 return (error);
2963 2965 }
2964 2966
2965 2967 /*
2966 2968 * Evaluate a multi-component path
2967 2969 */
2968 2970 int
2969 2971 rfs_pathname(
2970 2972 char *path, /* pathname to evaluate */
|
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
2971 2973 vnode_t **dirvpp, /* ret for ptr to parent dir vnode */
2972 2974 vnode_t **compvpp, /* ret for ptr to component vnode */
2973 2975 vnode_t *startdvp, /* starting vnode */
2974 2976 cred_t *cr, /* user's credential */
2975 2977 int pathflag) /* flag to identify path, e.g. URL */
2976 2978 {
2977 2979 char namebuf[TYPICALMAXPATHLEN];
2978 2980 struct pathname pn;
2979 2981 int error;
2980 2982
2983 + ASSERT3U(crgetzoneid(cr), ==, curzone->zone_id);
2984 +
2981 2985 /*
2982 2986 * If pathname starts with '/', then set startdvp to root.
2983 2987 */
2984 2988 if (*path == '/') {
2985 2989 while (*path == '/')
2986 2990 path++;
2987 2991
2988 2992 startdvp = ZONE_ROOTVP();
2989 2993 }
2990 2994
2991 2995 error = pn_get_buf(path, UIO_SYSSPACE, &pn, namebuf, sizeof (namebuf));
2992 2996 if (error == 0) {
2993 2997 /*
2994 2998 * Call the URL parser for URL paths to modify the original
2995 2999 * string to handle any '%' encoded characters that exist.
2996 3000 * Done here to avoid an extra bcopy in the lookup.
2997 3001 * We need to be careful about pathlen's. We know that
2998 3002 * rfs_pathname() is called with a non-empty path. However,
2999 3003 * it could be emptied due to the path simply being all /'s,
3000 3004 * which is valid to proceed with the lookup, or due to the
3001 3005 * URL parser finding an encoded null character at the
3002 3006 * beginning of path which should not proceed with the lookup.
3003 3007 */
3004 3008 if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
3005 3009 URLparse(pn.pn_path);
3006 3010 if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0)
3007 3011 return (ENOENT);
3008 3012 }
3009 3013 VN_HOLD(startdvp);
3010 3014 error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
3011 3015 ZONE_ROOTVP(), startdvp, cr);
3012 3016 }
3013 3017 if (error == ENAMETOOLONG) {
3014 3018 /*
3015 3019 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
3016 3020 */
3017 3021 if (error = pn_get(path, UIO_SYSSPACE, &pn))
3018 3022 return (error);
3019 3023 if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
3020 3024 URLparse(pn.pn_path);
3021 3025 if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0) {
3022 3026 pn_free(&pn);
3023 3027 return (ENOENT);
3024 3028 }
3025 3029 }
3026 3030 VN_HOLD(startdvp);
3027 3031 error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
3028 3032 ZONE_ROOTVP(), startdvp, cr);
3029 3033 pn_free(&pn);
3030 3034 }
3031 3035
3032 3036 return (error);
3033 3037 }
3034 3038
3035 3039 /*
3036 3040 * Adapt the multicomponent lookup path depending on the pathtype
3037 3041 */
3038 3042 static int
3039 3043 MCLpath(char **path)
3040 3044 {
3041 3045 unsigned char c = (unsigned char)**path;
3042 3046
3043 3047 /*
3044 3048 * If the MCL path is between 0x20 and 0x7E (graphic printable
3045 3049 * character of the US-ASCII coded character set), its a URL path,
3046 3050 * per RFC 1738.
3047 3051 */
3048 3052 if (c >= 0x20 && c <= 0x7E)
3049 3053 return (URLPATH);
3050 3054
3051 3055 /*
3052 3056 * If the first octet of the MCL path is not an ASCII character
3053 3057 * then it must be interpreted as a tag value that describes the
3054 3058 * format of the remaining octets of the MCL path.
3055 3059 *
3056 3060 * If the first octet of the MCL path is 0x81 it is a query
3057 3061 * for the security info.
3058 3062 */
3059 3063 switch (c) {
3060 3064 case 0x80: /* native path, i.e. MCL via mount protocol */
3061 3065 (*path)++;
3062 3066 return (NATIVEPATH);
3063 3067 case 0x81: /* security query */
3064 3068 (*path)++;
3065 3069 return (SECURITY_QUERY);
3066 3070 default:
3067 3071 return (-1);
3068 3072 }
3069 3073 }
3070 3074
3071 3075 #define fromhex(c) ((c >= '0' && c <= '9') ? (c - '0') : \
3072 3076 ((c >= 'A' && c <= 'F') ? (c - 'A' + 10) :\
3073 3077 ((c >= 'a' && c <= 'f') ? (c - 'a' + 10) : 0)))
3074 3078
3075 3079 /*
3076 3080 * The implementation of URLparse guarantees that the final string will
3077 3081 * fit in the original one. Replaces '%' occurrences followed by 2 characters
3078 3082 * with its corresponding hexadecimal character.
3079 3083 */
3080 3084 static void
3081 3085 URLparse(char *str)
3082 3086 {
3083 3087 char *p, *q;
3084 3088
3085 3089 p = q = str;
3086 3090 while (*p) {
3087 3091 *q = *p;
3088 3092 if (*p++ == '%') {
3089 3093 if (*p) {
3090 3094 *q = fromhex(*p) * 16;
3091 3095 p++;
3092 3096 if (*p) {
3093 3097 *q += fromhex(*p);
3094 3098 p++;
3095 3099 }
3096 3100 }
3097 3101 }
3098 3102 q++;
3099 3103 }
3100 3104 *q = '\0';
3101 3105 }
3102 3106
3103 3107
3104 3108 /*
3105 3109 * Get the export information for the lookup vnode, and verify its
3106 3110 * useable.
3107 3111 */
3108 3112 int
3109 3113 nfs_check_vpexi(vnode_t *mc_dvp, vnode_t *vp, cred_t *cr,
3110 3114 struct exportinfo **exi)
3111 3115 {
3112 3116 int walk;
3113 3117 int error = 0;
3114 3118
3115 3119 *exi = nfs_vptoexi(mc_dvp, vp, cr, &walk, NULL, FALSE);
3116 3120 if (*exi == NULL)
3117 3121 error = EACCES;
3118 3122 else {
3119 3123 /*
3120 3124 * If nosub is set for this export then
3121 3125 * a lookup relative to the public fh
3122 3126 * must not terminate below the
3123 3127 * exported directory.
3124 3128 */
3125 3129 if ((*exi)->exi_export.ex_flags & EX_NOSUB && walk > 0)
3126 3130 error = EACCES;
3127 3131 }
3128 3132
3129 3133 return (error);
3130 3134 }
3131 3135
3132 3136 /*
3133 3137 * Used by NFSv3 and NFSv4 server to query label of
3134 3138 * a pathname component during lookup/access ops.
3135 3139 */
3136 3140 ts_label_t *
3137 3141 nfs_getflabel(vnode_t *vp, struct exportinfo *exi)
3138 3142 {
3139 3143 zone_t *zone;
3140 3144 ts_label_t *zone_label;
3141 3145 char *path;
3142 3146
3143 3147 mutex_enter(&vp->v_lock);
3144 3148 if (vp->v_path != vn_vpath_empty) {
3145 3149 zone = zone_find_by_any_path(vp->v_path, B_FALSE);
3146 3150 mutex_exit(&vp->v_lock);
3147 3151 } else {
3148 3152 /*
3149 3153 * v_path not cached. Fall back on pathname of exported
3150 3154 * file system as we rely on pathname from which we can
3151 3155 * derive a label. The exported file system portion of
3152 3156 * path is sufficient to obtain a label.
3153 3157 */
3154 3158 path = exi->exi_export.ex_path;
3155 3159 if (path == NULL) {
3156 3160 mutex_exit(&vp->v_lock);
3157 3161 return (NULL);
3158 3162 }
3159 3163 zone = zone_find_by_any_path(path, B_FALSE);
3160 3164 mutex_exit(&vp->v_lock);
3161 3165 }
3162 3166 /*
3163 3167 * Caller has verified that the file is either
3164 3168 * exported or visible. So if the path falls in
3165 3169 * global zone, admin_low is returned; otherwise
3166 3170 * the zone's label is returned.
3167 3171 */
3168 3172 zone_label = zone->zone_slabel;
3169 3173 label_hold(zone_label);
3170 3174 zone_rele(zone);
3171 3175 return (zone_label);
3172 3176 }
3173 3177
3174 3178 /*
3175 3179 * TX NFS routine used by NFSv3 and NFSv4 to do label check
3176 3180 * on client label and server's file object lable.
3177 3181 */
3178 3182 boolean_t
3179 3183 do_rfs_label_check(bslabel_t *clabel, vnode_t *vp, int flag,
3180 3184 struct exportinfo *exi)
3181 3185 {
3182 3186 bslabel_t *slabel;
3183 3187 ts_label_t *tslabel;
3184 3188 boolean_t result;
3185 3189
3186 3190 if ((tslabel = nfs_getflabel(vp, exi)) == NULL) {
3187 3191 return (B_FALSE);
3188 3192 }
3189 3193 slabel = label2bslabel(tslabel);
3190 3194 DTRACE_PROBE4(tx__rfs__log__info__labelcheck, char *,
3191 3195 "comparing server's file label(1) with client label(2) (vp(3))",
3192 3196 bslabel_t *, slabel, bslabel_t *, clabel, vnode_t *, vp);
3193 3197
3194 3198 if (flag == EQUALITY_CHECK)
3195 3199 result = blequal(clabel, slabel);
3196 3200 else
3197 3201 result = bldominates(clabel, slabel);
3198 3202 label_rele(tslabel);
3199 3203 return (result);
3200 3204 }
3201 3205
3202 3206 /*
3203 3207 * Callback function to return the loaned buffers.
3204 3208 * Calls VOP_RETZCBUF() only after all uio_iov[]
3205 3209 * buffers are returned. nu_ref maintains the count.
3206 3210 */
3207 3211 void
3208 3212 rfs_free_xuio(void *free_arg)
3209 3213 {
3210 3214 uint_t ref;
3211 3215 nfs_xuio_t *nfsuiop = (nfs_xuio_t *)free_arg;
3212 3216
3213 3217 ref = atomic_dec_uint_nv(&nfsuiop->nu_ref);
3214 3218
3215 3219 /*
3216 3220 * Call VOP_RETZCBUF() only when all the iov buffers
3217 3221 * are sent OTW.
3218 3222 */
3219 3223 if (ref != 0)
3220 3224 return;
3221 3225
3222 3226 if (((uio_t *)nfsuiop)->uio_extflg & UIO_XUIO) {
3223 3227 (void) VOP_RETZCBUF(nfsuiop->nu_vp, (xuio_t *)free_arg, NULL,
3224 3228 NULL);
3225 3229 VN_RELE(nfsuiop->nu_vp);
3226 3230 }
3227 3231
3228 3232 kmem_cache_free(nfs_xuio_cache, free_arg);
3229 3233 }
3230 3234
3231 3235 xuio_t *
3232 3236 rfs_setup_xuio(vnode_t *vp)
3233 3237 {
3234 3238 nfs_xuio_t *nfsuiop;
3235 3239
3236 3240 nfsuiop = kmem_cache_alloc(nfs_xuio_cache, KM_SLEEP);
3237 3241
3238 3242 bzero(nfsuiop, sizeof (nfs_xuio_t));
3239 3243 nfsuiop->nu_vp = vp;
3240 3244
3241 3245 /*
3242 3246 * ref count set to 1. more may be added
3243 3247 * if multiple mblks refer to multiple iov's.
3244 3248 * This is done in uio_to_mblk().
3245 3249 */
3246 3250
3247 3251 nfsuiop->nu_ref = 1;
3248 3252
3249 3253 nfsuiop->nu_frtn.free_func = rfs_free_xuio;
3250 3254 nfsuiop->nu_frtn.free_arg = (char *)nfsuiop;
3251 3255
3252 3256 nfsuiop->nu_uio.xu_type = UIOTYPE_ZEROCOPY;
3253 3257
3254 3258 return (&nfsuiop->nu_uio);
3255 3259 }
3256 3260
3257 3261 mblk_t *
3258 3262 uio_to_mblk(uio_t *uiop)
3259 3263 {
3260 3264 struct iovec *iovp;
3261 3265 int i;
3262 3266 mblk_t *mp, *mp1;
3263 3267 nfs_xuio_t *nfsuiop = (nfs_xuio_t *)uiop;
3264 3268
3265 3269 if (uiop->uio_iovcnt == 0)
3266 3270 return (NULL);
3267 3271
3268 3272 iovp = uiop->uio_iov;
3269 3273 mp = mp1 = esballoca((uchar_t *)iovp->iov_base, iovp->iov_len,
3270 3274 BPRI_MED, &nfsuiop->nu_frtn);
3271 3275 ASSERT(mp != NULL);
3272 3276
3273 3277 mp->b_wptr += iovp->iov_len;
3274 3278 mp->b_datap->db_type = M_DATA;
3275 3279
3276 3280 for (i = 1; i < uiop->uio_iovcnt; i++) {
3277 3281 iovp = (uiop->uio_iov + i);
3278 3282
3279 3283 mp1->b_cont = esballoca(
3280 3284 (uchar_t *)iovp->iov_base, iovp->iov_len, BPRI_MED,
3281 3285 &nfsuiop->nu_frtn);
3282 3286
3283 3287 mp1 = mp1->b_cont;
3284 3288 ASSERT(mp1 != NULL);
3285 3289 mp1->b_wptr += iovp->iov_len;
3286 3290 mp1->b_datap->db_type = M_DATA;
3287 3291 }
3288 3292
3289 3293 nfsuiop->nu_ref = uiop->uio_iovcnt;
3290 3294
3291 3295 return (mp);
3292 3296 }
3293 3297
3294 3298 /*
3295 3299 * Allocate memory to hold data for a read request of len bytes.
3296 3300 *
3297 3301 * We don't allocate buffers greater than kmem_max_cached in size to avoid
3298 3302 * allocating memory from the kmem_oversized arena. If we allocate oversized
3299 3303 * buffers, we incur heavy cross-call activity when freeing these large buffers
3300 3304 * in the TCP receive path. Note that we can't set b_wptr here since the
3301 3305 * length of the data returned may differ from the length requested when
3302 3306 * reading the end of a file; we set b_wptr in rfs_rndup_mblks() once the
3303 3307 * length of the read is known.
3304 3308 */
3305 3309 mblk_t *
3306 3310 rfs_read_alloc(uint_t len, struct iovec **iov, int *iovcnt)
3307 3311 {
3308 3312 struct iovec *iovarr;
3309 3313 mblk_t *mp, **mpp = ∓
3310 3314 size_t mpsize;
3311 3315 uint_t remain = len;
3312 3316 int i, err = 0;
3313 3317
3314 3318 *iovcnt = howmany(len, kmem_max_cached);
3315 3319
3316 3320 iovarr = kmem_alloc(*iovcnt * sizeof (struct iovec), KM_SLEEP);
3317 3321 *iov = iovarr;
3318 3322
3319 3323 for (i = 0; i < *iovcnt; remain -= mpsize, i++) {
3320 3324 ASSERT(remain <= len);
3321 3325 /*
3322 3326 * We roundup the size we allocate to a multiple of
3323 3327 * BYTES_PER_XDR_UNIT (4 bytes) so that the call to
3324 3328 * xdrmblk_putmblk() never fails.
3325 3329 */
3326 3330 ASSERT(kmem_max_cached % BYTES_PER_XDR_UNIT == 0);
3327 3331 mpsize = MIN(kmem_max_cached, remain);
3328 3332 *mpp = allocb_wait(RNDUP(mpsize), BPRI_MED, STR_NOSIG, &err);
3329 3333 ASSERT(*mpp != NULL);
3330 3334 ASSERT(err == 0);
3331 3335
3332 3336 iovarr[i].iov_base = (caddr_t)(*mpp)->b_rptr;
3333 3337 iovarr[i].iov_len = mpsize;
3334 3338 mpp = &(*mpp)->b_cont;
3335 3339 }
3336 3340 return (mp);
3337 3341 }
3338 3342
3339 3343 void
3340 3344 rfs_rndup_mblks(mblk_t *mp, uint_t len, int buf_loaned)
3341 3345 {
3342 3346 int i;
3343 3347 int alloc_err = 0;
3344 3348 mblk_t *rmp;
3345 3349 uint_t mpsize, remainder;
3346 3350
3347 3351 remainder = P2NPHASE(len, BYTES_PER_XDR_UNIT);
3348 3352
3349 3353 /*
3350 3354 * Non copy-reduction case. This function assumes that blocks were
3351 3355 * allocated in multiples of BYTES_PER_XDR_UNIT bytes, which makes this
3352 3356 * padding safe without bounds checking.
3353 3357 */
3354 3358 if (!buf_loaned) {
3355 3359 /*
3356 3360 * Set the size of each mblk in the chain until we've consumed
3357 3361 * the specified length for all but the last one.
3358 3362 */
3359 3363 while ((mpsize = MBLKSIZE(mp)) < len) {
3360 3364 ASSERT(mpsize % BYTES_PER_XDR_UNIT == 0);
3361 3365 mp->b_wptr += mpsize;
3362 3366 len -= mpsize;
3363 3367 mp = mp->b_cont;
3364 3368 ASSERT(mp != NULL);
3365 3369 }
3366 3370
3367 3371 ASSERT(len + remainder <= mpsize);
3368 3372 mp->b_wptr += len;
3369 3373 for (i = 0; i < remainder; i++)
3370 3374 *mp->b_wptr++ = '\0';
3371 3375 return;
3372 3376 }
3373 3377
3374 3378 /*
3375 3379 * No remainder mblk required.
3376 3380 */
3377 3381 if (remainder == 0)
3378 3382 return;
3379 3383
3380 3384 /*
3381 3385 * Get to the last mblk in the chain.
3382 3386 */
3383 3387 while (mp->b_cont != NULL)
3384 3388 mp = mp->b_cont;
3385 3389
3386 3390 /*
3387 3391 * In case of copy-reduction mblks, the size of the mblks are fixed
3388 3392 * and are of the size of the loaned buffers. Allocate a remainder
3389 3393 * mblk and chain it to the data buffers. This is sub-optimal, but not
3390 3394 * expected to happen commonly.
3391 3395 */
3392 3396 rmp = allocb_wait(remainder, BPRI_MED, STR_NOSIG, &alloc_err);
3393 3397 ASSERT(rmp != NULL);
3394 3398 ASSERT(alloc_err == 0);
3395 3399
3396 3400 for (i = 0; i < remainder; i++)
3397 3401 *rmp->b_wptr++ = '\0';
3398 3402
3399 3403 rmp->b_datap->db_type = M_DATA;
3400 3404 mp->b_cont = rmp;
3401 3405 }
|
↓ open down ↓ |
411 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX