xref: /titanic_52/usr/src/cmd/fs.d/nfs/mount/mount.c (revision d62bc4badc1c1f1549c961cfb8b420e650e1272b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * nfs mount
44  */
45 
46 #define	NFSCLIENT
47 #include <locale.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <memory.h>
51 #include <stdarg.h>
52 #include <unistd.h>
53 #include <ctype.h>
54 #include <stdlib.h>
55 #include <signal.h>
56 #include <sys/param.h>
57 #include <rpc/rpc.h>
58 #include <errno.h>
59 #include <sys/stat.h>
60 #include <netdb.h>
61 #include <sys/mount.h>
62 #include <sys/mntent.h>
63 #include <sys/mnttab.h>
64 #include <nfs/nfs.h>
65 #include <nfs/mount.h>
66 #include <rpcsvc/mount.h>
67 #include <sys/pathconf.h>
68 #include <netdir.h>
69 #include <netconfig.h>
70 #include <sys/sockio.h>
71 #include <net/if.h>
72 #include <syslog.h>
73 #include <fslib.h>
74 #include <deflt.h>
75 #include <sys/wait.h>
76 #include "replica.h"
77 #include <netinet/in.h>
78 #include <nfs/nfs_sec.h>
79 #include <rpcsvc/daemon_utils.h>
80 #include <priv.h>
81 #include <tsol/label.h>
82 #include "nfs_subr.h"
83 #include "webnfs.h"
84 #include <rpcsvc/nfs4_prot.h>
85 
86 #include <nfs/nfssys.h>
87 extern int _nfssys(enum nfssys_op, void *);
88 
89 #ifndef	NFS_VERSMAX
90 #define	NFS_VERSMAX	4
91 #endif
92 #ifndef	NFS_VERSMIN
93 #define	NFS_VERSMIN	2
94 #endif
95 
96 #define	RET_OK		0
97 #define	RET_RETRY	32
98 #define	RET_ERR		33
99 #define	RET_MNTERR	1000
100 #define	ERR_PROTO_NONE		0
101 #define	ERR_PROTO_INVALID	901
102 #define	ERR_PROTO_UNSUPP	902
103 #define	ERR_NETPATH		903
104 #define	ERR_NOHOST		904
105 #define	ERR_RPCERROR		905
106 
107 typedef struct err_ret {
108 	int error_type;
109 	int error_value;
110 } err_ret_t;
111 
112 #define	SET_ERR_RET(errst, etype, eval) \
113 	if (errst) { \
114 		(errst)->error_type = etype; \
115 		(errst)->error_value = eval; \
116 	}
117 
118 /* number of transports to try */
119 #define	MNT_PREF_LISTLEN	2
120 #define	FIRST_TRY		1
121 #define	SECOND_TRY		2
122 
123 #define	BIGRETRY	10000
124 
125 /* maximum length of RPC header for NFS messages */
126 #define	NFS_RPC_HDR	432
127 
128 #define	NFS_ARGS_EXTB_secdata(args, secdata) \
129 	{ (args)->nfs_args_ext = NFS_ARGS_EXTB, \
130 	(args)->nfs_ext_u.nfs_extB.secdata = secdata; }
131 
132 extern int __clnt_bindresvport(CLIENT *);
133 extern char *nfs_get_qop_name();
134 extern AUTH * nfs_create_ah();
135 extern enum snego_stat nfs_sec_nego();
136 
137 static void usage(void);
138 static int retry(struct mnttab *, int);
139 static int set_args(int *, struct nfs_args *, char *, struct mnttab *);
140 static int get_fh_via_pub(struct nfs_args *, char *, char *, bool_t, bool_t,
141 	int *, struct netconfig **, ushort_t);
142 static int get_fh(struct nfs_args *, char *, char *, int *, bool_t,
143 	struct netconfig **, ushort_t);
144 static int make_secure(struct nfs_args *, char *, struct netconfig *,
145 	bool_t, rpcvers_t);
146 static int mount_nfs(struct mnttab *, int, err_ret_t *);
147 static int getaddr_nfs(struct nfs_args *, char *, struct netconfig **,
148 		    bool_t, char *, ushort_t, err_ret_t *, bool_t);
149 static void pr_err(const char *fmt, ...);
150 static void usage(void);
151 static struct netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
152 	struct netconfig **, char *, ushort_t, struct t_info *,
153 	caddr_t *, bool_t, char *, err_ret_t *);
154 
155 static struct netbuf *get_the_addr(char *, rpcprog_t, rpcvers_t,
156 	struct netconfig *, ushort_t, struct t_info *, caddr_t *,
157 	bool_t, char *, err_ret_t *);
158 
159 extern int self_check(char *);
160 
161 static void read_default(void);
162 
163 static char typename[64];
164 
165 static int bg = 0;
166 static int backgrounded = 0;
167 static int posix = 0;
168 static int retries = BIGRETRY;
169 static ushort_t nfs_port = 0;
170 static char *nfs_proto = NULL;
171 
172 static int mflg = 0;
173 static int Oflg = 0;	/* Overlay mounts */
174 static int qflg = 0;	/* quiet - don't print warnings on bad options */
175 
176 static char *fstype = MNTTYPE_NFS;
177 
178 static seconfig_t nfs_sec;
179 static int sec_opt = 0;	/* any security option ? */
180 static bool_t snego_done;
181 static void sigusr1(int);
182 
183 extern void set_nfsv4_ephemeral_mount_to(void);
184 
185 /*
186  * list of support services needed
187  */
188 static char	*service_list[] = { STATD, LOCKD, NULL };
189 static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };
190 
191 /*
192  * These two variables control the NFS version number to be used.
193  *
194  * nfsvers defaults to 0 which means to use the highest number that
195  * both the client and the server support.  It can also be set to
196  * a particular value, either 2, 3, or 4 to indicate the version
197  * number of choice.  If the server (or the client) do not support
198  * the version indicated, then the mount attempt will be failed.
199  *
200  * nfsvers_to_use is the actual version number found to use.  It
201  * is determined in get_fh by pinging the various versions of the
202  * NFS service on the server to see which responds positively.
203  *
204  * nfsretry_vers is the version number set when we retry the mount
205  * command with the version decremented from nfsvers_to_use.
206  * nfsretry_vers is set from nfsvers_to_use when we retry the mount
207  * for errors other than RPC errors; it helps un know why we are
208  * retrying. It is an indication that the retry is due to
209  * non-RPC errors.
210  */
211 static rpcvers_t nfsvers = 0;
212 static rpcvers_t nfsvers_to_use = 0;
213 static rpcvers_t nfsretry_vers = 0;
214 
215 /*
216  * There are the defaults (range) for the client when determining
217  * which NFS version to use when probing the server (see above).
218  * These will only be used when the vers mount option is not used and
219  * these may be reset if /etc/default/nfs is configured to do so.
220  */
221 static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
222 static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;
223 
224 /*
225  * This variable controls whether to try the public file handle.
226  */
227 static bool_t public_opt;
228 
229 int
230 main(int argc, char *argv[])
231 {
232 	struct mnttab mnt;
233 	extern char *optarg;
234 	extern int optind;
235 	char optbuf[MAX_MNTOPT_STR];
236 	int ro = 0;
237 	int r;
238 	int c;
239 	char *myname;
240 	err_ret_t retry_error;
241 
242 	(void) setlocale(LC_ALL, "");
243 #if !defined(TEXT_DOMAIN)
244 #define	TEXT_DOMAIN	"SYS_TEST"
245 #endif
246 	(void) textdomain(TEXT_DOMAIN);
247 
248 	myname = strrchr(argv[0], '/');
249 	myname = myname ? myname + 1 : argv[0];
250 	(void) snprintf(typename, sizeof (typename), "%s %s",
251 	    MNTTYPE_NFS, myname);
252 	argv[0] = typename;
253 
254 	mnt.mnt_mntopts = optbuf;
255 	(void) strcpy(optbuf, "rw");
256 
257 	/*
258 	 * Set options
259 	 */
260 	while ((c = getopt(argc, argv, "ro:mOq")) != EOF) {
261 		switch (c) {
262 		case 'r':
263 			ro++;
264 			break;
265 		case 'o':
266 			if (strlen(optarg) >= MAX_MNTOPT_STR) {
267 				pr_err(gettext("option string too long"));
268 				return (RET_ERR);
269 			}
270 			(void) strcpy(mnt.mnt_mntopts, optarg);
271 #ifdef LATER					/* XXX */
272 			if (strstr(optarg, MNTOPT_REMOUNT)) {
273 				/*
274 				 * If remount is specified, only rw is allowed.
275 				 */
276 				if ((strcmp(optarg, MNTOPT_REMOUNT) != 0) &&
277 				    (strcmp(optarg, "remount,rw") != 0) &&
278 				    (strcmp(optarg, "rw,remount") != 0)) {
279 					pr_err(gettext("Invalid options\n"));
280 					exit(RET_ERR);
281 				}
282 			}
283 #endif /* LATER */				/* XXX */
284 			break;
285 		case 'm':
286 			mflg++;
287 			break;
288 		case 'O':
289 			Oflg++;
290 			break;
291 		case 'q':
292 			qflg++;
293 			break;
294 		default:
295 			usage();
296 			exit(RET_ERR);
297 		}
298 	}
299 	if (argc - optind != 2) {
300 		usage();
301 		exit(RET_ERR);
302 	}
303 
304 	mnt.mnt_special = argv[optind];
305 	mnt.mnt_mountp = argv[optind+1];
306 
307 	if (!priv_ineffect(PRIV_SYS_MOUNT) ||
308 	    !priv_ineffect(PRIV_NET_PRIVADDR)) {
309 		pr_err(gettext("insufficient privileges\n"));
310 		exit(RET_ERR);
311 	}
312 
313 	/*
314 	 * On a labeled system, allow read-down nfs mounts if privileged
315 	 * (PRIV_NET_MAC_AWARE) to do so.  Otherwise, ignore the error
316 	 * and "mount equal label only" behavior will result.
317 	 */
318 	if (is_system_labeled())
319 		(void) setpflags(NET_MAC_AWARE, 1);
320 
321 	/*
322 	 * Read the defaults file to see if the min/max versions have
323 	 * been set and therefore would override the encoded defaults.
324 	 * Then check to make sure that if they were set that the
325 	 * values are reasonable.
326 	 */
327 	read_default();
328 	if (vers_min_default > vers_max_default ||
329 	    vers_min_default < NFS_VERSMIN ||
330 	    vers_max_default > NFS_VERSMAX) {
331 		pr_err("%s %s\n%s %s\n",
332 		    gettext("Incorrect configuration of client\'s"),
333 		    NFSADMIN,
334 		    gettext("NFS_CLIENT_VERSMIN or NFS_CLIENT_VERSMAX"),
335 		    gettext("is either out of range or overlaps."));
336 	}
337 
338 	SET_ERR_RET(&retry_error, ERR_PROTO_NONE, 0);
339 	r = mount_nfs(&mnt, ro, &retry_error);
340 	if (r == RET_RETRY && retries) {
341 		/*
342 		 * Check the error code from the last mount attempt if it was
343 		 * an RPC error, then retry as is. Otherwise we retry with the
344 		 * nfsretry_vers set. It is set by decrementing nfsvers_to_use.
345 		 * If we are retrying with nfsretry_vers then we don't print any
346 		 * retry messages, since we are not retrying due to an RPC
347 		 * error.
348 		 */
349 		if (retry_error.error_type) {
350 			if (retry_error.error_type != ERR_RPCERROR) {
351 				nfsretry_vers = nfsvers_to_use =
352 				    nfsvers_to_use - 1;
353 				if (nfsretry_vers < NFS_VERSMIN)
354 					return (r);
355 			}
356 		}
357 
358 		r = retry(&mnt, ro);
359 	}
360 	/*
361 	 * exit(r);
362 	 */
363 	return (r);
364 }
365 
366 static void
367 pr_err(const char *fmt, ...)
368 {
369 	va_list ap;
370 
371 	va_start(ap, fmt);
372 	if (backgrounded != 0) {
373 		(void) vsyslog(LOG_ERR, fmt, ap);
374 	} else {
375 		(void) fprintf(stderr, "%s: ", typename);
376 		(void) vfprintf(stderr, fmt, ap);
377 		(void) fflush(stderr);
378 	}
379 	va_end(ap);
380 }
381 
382 static void
383 usage()
384 {
385 	(void) fprintf(stderr,
386 	    gettext("Usage: nfs mount [-r] [-o opts] [server:]path dir\n"));
387 	exit(RET_ERR);
388 }
389 
390 static int
391 mount_nfs(struct mnttab *mntp, int ro, err_ret_t *retry_error)
392 {
393 	struct nfs_args *args = NULL, *argp = NULL, *prev_argp = NULL;
394 	struct netconfig *nconf = NULL;
395 	struct replica *list = NULL;
396 	int mntflags = 0;
397 	int i, r, n;
398 	int oldvers = 0, vers = 0;
399 	int last_error = RET_OK;
400 	int replicated = 0;
401 	char *p;
402 	bool_t url;
403 	bool_t use_pubfh;
404 	char *special = NULL;
405 	char *oldpath = NULL;
406 	char *newpath = NULL;
407 	char *service;
408 	pid_t pi;
409 	struct flock f;
410 	char *saveopts = NULL;
411 	char **sl = NULL;
412 
413 	mntp->mnt_fstype = MNTTYPE_NFS;
414 
415 	if (ro) {
416 		mntflags |= MS_RDONLY;
417 		/* convert "rw"->"ro" */
418 		if (p = strstr(mntp->mnt_mntopts, "rw")) {
419 			if (*(p+2) == ',' || *(p+2) == '\0')
420 				*(p+1) = 'o';
421 		}
422 	}
423 
424 	if (Oflg)
425 		mntflags |= MS_OVERLAY;
426 
427 	list = parse_replica(mntp->mnt_special, &n);
428 	if (list == NULL) {
429 		if (n < 0)
430 			pr_err(gettext("nfs file system; use [host:]path\n"));
431 		else
432 			pr_err(gettext("no memory\n"));
433 		return (RET_ERR);
434 	}
435 
436 	replicated = (n > 1);
437 
438 	/*
439 	 * There are some free() calls at the bottom of this loop, so be
440 	 * careful about adding continue statements.
441 	 */
442 	for (i = 0; i < n; i++) {
443 		char *path;
444 		char *host;
445 		ushort_t port;
446 
447 		argp = (struct nfs_args *)malloc(sizeof (*argp));
448 		if (argp == NULL) {
449 			pr_err(gettext("no memory\n"));
450 			last_error = RET_ERR;
451 			goto out;
452 		}
453 		memset(argp, 0, sizeof (*argp));
454 
455 		memset(&nfs_sec, 0, sizeof (nfs_sec));
456 		sec_opt = 0;
457 		use_pubfh = FALSE;
458 		url = FALSE;
459 		port = 0;
460 		snego_done = FALSE;
461 
462 		/*
463 		 * Looking for resources of the form
464 		 *	nfs://server_host[:port_number]/path_name
465 		 */
466 		if (strcmp(list[i].host, "nfs") == 0 && strncmp(list[i].path,
467 		    "//", 2) == 0) {
468 			char *sport, *cb;
469 			url = TRUE;
470 			oldpath = strdup(list[i].path);
471 			if (oldpath == NULL) {
472 				pr_err(gettext("memory allocation failure\n"));
473 				last_error = RET_ERR;
474 				goto out;
475 			}
476 			host = list[i].path+2;
477 			path = strchr(host, '/');
478 
479 			if (path == NULL) {
480 				pr_err(gettext(
481 				    "illegal nfs url syntax\n"));
482 				last_error = RET_ERR;
483 				goto out;
484 			}
485 
486 			*path = '\0';
487 			if (*host == '[') {
488 				cb = strchr(host, ']');
489 				if (cb == NULL) {
490 					pr_err(gettext(
491 					    "illegal nfs url syntax\n"));
492 					last_error = RET_ERR;
493 					goto out;
494 				} else {
495 					*cb = '\0';
496 					host++;
497 					cb++;
498 					if (*cb == ':')
499 						port = htons((ushort_t)
500 						    atoi(cb+1));
501 				}
502 			} else {
503 				sport = strchr(host, ':');
504 
505 				if (sport != NULL && sport < path) {
506 					*sport = '\0';
507 					port = htons((ushort_t)atoi(sport+1));
508 				}
509 			}
510 
511 			path++;
512 			if (*path == '\0')
513 				path = ".";
514 
515 		} else {
516 			host = list[i].host;
517 			path = list[i].path;
518 		}
519 
520 		if (r = set_args(&mntflags, argp, host, mntp)) {
521 			last_error = r;
522 			goto out;
523 		}
524 
525 		if (public_opt == TRUE)
526 			use_pubfh = TRUE;
527 
528 		if (port == 0) {
529 			port = nfs_port;
530 		} else if (nfs_port != 0 && nfs_port != port) {
531 			pr_err(gettext(
532 			    "port (%u) in nfs URL not the same"
533 			    " as port (%u) in port option\n"),
534 			    (unsigned int)ntohs(port),
535 			    (unsigned int)ntohs(nfs_port));
536 			last_error = RET_ERR;
537 			goto out;
538 		}
539 
540 
541 		if (replicated && !(mntflags & MS_RDONLY)) {
542 			pr_err(gettext(
543 			    "replicated mounts must be read-only\n"));
544 			last_error = RET_ERR;
545 			goto out;
546 		}
547 
548 		if (replicated && (argp->flags & NFSMNT_SOFT)) {
549 			pr_err(gettext(
550 			    "replicated mounts must not be soft\n"));
551 			last_error = RET_ERR;
552 			goto out;
553 		}
554 
555 		oldvers = vers;
556 		nconf = NULL;
557 
558 		r = RET_ERR;
559 
560 		/*
561 		 * If -o public was specified, and/or a URL was specified,
562 		 * then try the public file handle method.
563 		 */
564 		if ((use_pubfh == TRUE) || (url == TRUE)) {
565 			r = get_fh_via_pub(argp, host, path, url, use_pubfh,
566 			    &vers, &nconf, port);
567 
568 			if (r != RET_OK) {
569 				/*
570 				 * If -o public was specified, then return the
571 				 * error now.
572 				 */
573 				if (use_pubfh == TRUE) {
574 					last_error = r;
575 					goto out;
576 				}
577 			} else
578 				use_pubfh = TRUE;
579 			argp->flags |= NFSMNT_PUBLIC;
580 		}
581 
582 		if ((r != RET_OK) || (vers == NFS_V4)) {
583 			bool_t loud_on_mnt_err;
584 
585 			/*
586 			 * This can happen if -o public is not specified,
587 			 * special is a URL, and server doesn't support
588 			 * public file handle.
589 			 */
590 			if (url) {
591 				URLparse(path);
592 			}
593 
594 			/*
595 			 * If the path portion of the URL didn't have
596 			 * a leading / then there is good possibility
597 			 * that a mount without a leading slash will
598 			 * fail.
599 			 */
600 			if (url == TRUE && *path != '/')
601 				loud_on_mnt_err = FALSE;
602 			else
603 				loud_on_mnt_err = TRUE;
604 
605 			r = get_fh(argp, host, path, &vers,
606 			    loud_on_mnt_err, &nconf, port);
607 
608 			if (r != RET_OK) {
609 
610 				/*
611 				 * If there was no leading / and the path was
612 				 * derived from a URL, then try again
613 				 * with a leading /.
614 				 */
615 				if ((r == RET_MNTERR) &&
616 				    (loud_on_mnt_err == FALSE)) {
617 
618 					newpath = malloc(strlen(path)+2);
619 
620 					if (newpath == NULL) {
621 						pr_err(gettext("memory "
622 						    "allocation failure\n"));
623 						last_error = RET_ERR;
624 						goto out;
625 					}
626 
627 					strcpy(newpath, "/");
628 					strcat(newpath, path);
629 
630 					r = get_fh(argp, host, newpath, &vers,
631 					    TRUE, &nconf, port);
632 
633 					if (r == RET_OK)
634 						path = newpath;
635 				}
636 
637 				/*
638 				 * map exit code back to RET_ERR.
639 				 */
640 				if (r == RET_MNTERR)
641 					r = RET_ERR;
642 
643 				if (r != RET_OK) {
644 
645 					if (replicated) {
646 						if (argp->fh)
647 							free(argp->fh);
648 						if (argp->pathconf)
649 							free(argp->pathconf);
650 						free(argp);
651 						goto cont;
652 					}
653 
654 					last_error = r;
655 					goto out;
656 				}
657 			}
658 		}
659 
660 		if (oldvers && vers != oldvers) {
661 			pr_err(
662 			    gettext("replicas must have the same version\n"));
663 			last_error = RET_ERR;
664 			goto out;
665 		}
666 
667 		/*
668 		 * decide whether to use remote host's
669 		 * lockd or do local locking
670 		 */
671 		if (!(argp->flags & NFSMNT_LLOCK) && vers == NFS_VERSION &&
672 		    remote_lock(host, argp->fh)) {
673 			(void) fprintf(stderr, gettext(
674 			    "WARNING: No network locking on %s:%s:"),
675 			    host, path);
676 			(void) fprintf(stderr, gettext(
677 			    " contact admin to install server change\n"));
678 			argp->flags |= NFSMNT_LLOCK;
679 		}
680 
681 		if (self_check(host))
682 			argp->flags |= NFSMNT_LOOPBACK;
683 
684 		if (use_pubfh == FALSE) {
685 			/*
686 			 * Call to get_fh() above may have obtained the
687 			 * netconfig info and NULL proc'd the server.
688 			 * This would be the case with v4
689 			 */
690 			if (!(argp->flags & NFSMNT_KNCONF)) {
691 				nconf = NULL;
692 				if (r = getaddr_nfs(argp, host, &nconf,
693 				    FALSE, path, port, retry_error,
694 				    TRUE)) {
695 						last_error = r;
696 						goto out;
697 				}
698 			}
699 		}
700 
701 		if (make_secure(argp, host, nconf, use_pubfh, vers) < 0) {
702 			last_error = RET_ERR;
703 			goto out;
704 		}
705 
706 		if ((url == TRUE) && (use_pubfh == FALSE)) {
707 			/*
708 			 * Convert the special from
709 			 *	nfs://host/path
710 			 * to
711 			 *	host:path
712 			 */
713 			if (convert_special(&special, host, oldpath, path,
714 			    mntp->mnt_special) == -1) {
715 				(void) fprintf(stderr, gettext(
716 				    "could not convert URL nfs:%s to %s:%s\n"),
717 				    oldpath, host, path);
718 				last_error = RET_ERR;
719 				goto out;
720 			} else {
721 				mntp->mnt_special = special;
722 			}
723 		}
724 
725 		if (prev_argp == NULL)
726 			args = argp;
727 		else
728 			prev_argp->nfs_ext_u.nfs_extB.next = argp;
729 		prev_argp = argp;
730 
731 cont:
732 		if (oldpath != NULL) {
733 			free(oldpath);
734 			oldpath = NULL;
735 		}
736 
737 		if (newpath != NULL) {
738 			free(newpath);
739 			newpath = NULL;
740 		}
741 	}
742 
743 	argp = NULL;
744 
745 	if (args == NULL) {
746 		last_error = RET_RETRY;
747 		goto out;
748 	}
749 
750 	/* Determine which services are appropriate for the NFS version */
751 	if (strcmp(fstype, MNTTYPE_NFS4) == 0)
752 		sl = service_list_v4;
753 	else
754 		sl = service_list;
755 
756 	/*
757 	 * enable services as needed.
758 	 */
759 	_check_services(sl);
760 
761 	mntflags |= MS_DATA | MS_OPTIONSTR;
762 
763 	if (mflg)
764 		mntflags |= MS_NOMNTTAB;
765 
766 	if (!qflg)
767 		saveopts = strdup(mntp->mnt_mntopts);
768 
769 	/*
770 	 * And make sure that we have the ephemeral mount_to
771 	 * set for this zone.
772 	 */
773 	set_nfsv4_ephemeral_mount_to();
774 
775 	if (mount(mntp->mnt_special, mntp->mnt_mountp, mntflags, fstype, args,
776 	    sizeof (*args), mntp->mnt_mntopts, MAX_MNTOPT_STR) < 0) {
777 		if (errno != ENOENT) {
778 			pr_err(gettext("mount: %s: %s\n"),
779 			    mntp->mnt_mountp, strerror(errno));
780 		} else {
781 			struct stat sb;
782 			if (stat(mntp->mnt_mountp, &sb) < 0 && errno == ENOENT)
783 				pr_err(gettext("mount: %s: %s\n"),
784 				    mntp->mnt_mountp, strerror(ENOENT));
785 			else
786 				pr_err("%s: %s\n", mntp->mnt_special,
787 				    strerror(ENOENT));
788 		}
789 
790 		last_error = RET_ERR;
791 		goto out;
792 	}
793 
794 	if (!qflg && saveopts != NULL) {
795 		cmp_requested_to_actual_options(saveopts, mntp->mnt_mntopts,
796 		    mntp->mnt_special, mntp->mnt_mountp);
797 	}
798 
799 out:
800 	if (saveopts != NULL)
801 		free(saveopts);
802 	if (special != NULL)
803 		free(special);
804 	if (oldpath != NULL)
805 		free(oldpath);
806 	if (newpath != NULL)
807 		free(newpath);
808 
809 	free_replica(list, n);
810 
811 	if (argp != NULL) {
812 		/*
813 		 * If we had a new entry which was not added to the
814 		 * list yet, then add it now that it can be freed.
815 		 */
816 		if (prev_argp == NULL)
817 			args = argp;
818 		else
819 			prev_argp->nfs_ext_u.nfs_extB.next = argp;
820 	}
821 	argp = args;
822 	while (argp != NULL) {
823 		if (argp->fh)
824 			free(argp->fh);
825 		if (argp->pathconf)
826 			free(argp->pathconf);
827 		if (argp->knconf)
828 			free(argp->knconf);
829 		if (argp->addr) {
830 			free(argp->addr->buf);
831 			free(argp->addr);
832 		}
833 		nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
834 		if (argp->syncaddr) {
835 			free(argp->syncaddr->buf);
836 			free(argp->syncaddr);
837 		}
838 		if (argp->netname)
839 			free(argp->netname);
840 		prev_argp = argp;
841 		argp = argp->nfs_ext_u.nfs_extB.next;
842 		free(prev_argp);
843 	}
844 
845 	return (last_error);
846 }
847 
848 /*
849  * These options are duplicated in uts/common/fs/nfs/nfs_dlinet.c
850  * Changes must be made to both lists.
851  */
852 static char *optlist[] = {
853 #define	OPT_RO		0
854 	MNTOPT_RO,
855 #define	OPT_RW		1
856 	MNTOPT_RW,
857 #define	OPT_QUOTA	2
858 	MNTOPT_QUOTA,
859 #define	OPT_NOQUOTA	3
860 	MNTOPT_NOQUOTA,
861 #define	OPT_SOFT	4
862 	MNTOPT_SOFT,
863 #define	OPT_HARD	5
864 	MNTOPT_HARD,
865 #define	OPT_SUID	6
866 	MNTOPT_SUID,
867 #define	OPT_NOSUID	7
868 	MNTOPT_NOSUID,
869 #define	OPT_GRPID	8
870 	MNTOPT_GRPID,
871 #define	OPT_REMOUNT	9
872 	MNTOPT_REMOUNT,
873 #define	OPT_NOSUB	10
874 	MNTOPT_NOSUB,
875 #define	OPT_INTR	11
876 	MNTOPT_INTR,
877 #define	OPT_NOINTR	12
878 	MNTOPT_NOINTR,
879 #define	OPT_PORT	13
880 	MNTOPT_PORT,
881 #define	OPT_SECURE	14
882 	MNTOPT_SECURE,
883 #define	OPT_RSIZE	15
884 	MNTOPT_RSIZE,
885 #define	OPT_WSIZE	16
886 	MNTOPT_WSIZE,
887 #define	OPT_TIMEO	17
888 	MNTOPT_TIMEO,
889 #define	OPT_RETRANS	18
890 	MNTOPT_RETRANS,
891 #define	OPT_ACTIMEO	19
892 	MNTOPT_ACTIMEO,
893 #define	OPT_ACREGMIN	20
894 	MNTOPT_ACREGMIN,
895 #define	OPT_ACREGMAX	21
896 	MNTOPT_ACREGMAX,
897 #define	OPT_ACDIRMIN	22
898 	MNTOPT_ACDIRMIN,
899 #define	OPT_ACDIRMAX	23
900 	MNTOPT_ACDIRMAX,
901 #define	OPT_BG		24
902 	MNTOPT_BG,
903 #define	OPT_FG		25
904 	MNTOPT_FG,
905 #define	OPT_RETRY	26
906 	MNTOPT_RETRY,
907 #define	OPT_NOAC	27
908 	MNTOPT_NOAC,
909 #define	OPT_NOCTO	28
910 	MNTOPT_NOCTO,
911 #define	OPT_LLOCK	29
912 	MNTOPT_LLOCK,
913 #define	OPT_POSIX	30
914 	MNTOPT_POSIX,
915 #define	OPT_VERS	31
916 	MNTOPT_VERS,
917 #define	OPT_PROTO	32
918 	MNTOPT_PROTO,
919 #define	OPT_SEMISOFT	33
920 	MNTOPT_SEMISOFT,
921 #define	OPT_NOPRINT	34
922 	MNTOPT_NOPRINT,
923 #define	OPT_SEC		35
924 	MNTOPT_SEC,
925 #define	OPT_LARGEFILES	36
926 	MNTOPT_LARGEFILES,
927 #define	OPT_NOLARGEFILES 37
928 	MNTOPT_NOLARGEFILES,
929 #define	OPT_PUBLIC	38
930 	MNTOPT_PUBLIC,
931 #define	OPT_DIRECTIO	39
932 	MNTOPT_FORCEDIRECTIO,
933 #define	OPT_NODIRECTIO	40
934 	MNTOPT_NOFORCEDIRECTIO,
935 #define	OPT_XATTR	41
936 	MNTOPT_XATTR,
937 #define	OPT_NOXATTR	42
938 	MNTOPT_NOXATTR,
939 #define	OPT_DEVICES	43
940 	MNTOPT_DEVICES,
941 #define	OPT_NODEVICES	44
942 	MNTOPT_NODEVICES,
943 #define	OPT_SETUID	45
944 	MNTOPT_SETUID,
945 #define	OPT_NOSETUID	46
946 	MNTOPT_NOSETUID,
947 #define	OPT_EXEC	47
948 	MNTOPT_EXEC,
949 #define	OPT_NOEXEC	48
950 	MNTOPT_NOEXEC,
951 	NULL
952 };
953 
954 #define	bad(val) (val == NULL || !isdigit(*val))
955 
956 static int
957 set_args(int *mntflags, struct nfs_args *args, char *fshost, struct mnttab *mnt)
958 {
959 	char *saveopt, *optstr, *opts, *newopts, *val;
960 	int largefiles = 0;
961 	int invalid = 0;
962 	int attrpref = 0;
963 	int optlen;
964 
965 	args->flags = NFSMNT_INT;	/* default is "intr" */
966 	args->flags |= NFSMNT_HOSTNAME;
967 	args->flags |= NFSMNT_NEWARGS;	/* using extented nfs_args structure */
968 	args->hostname = fshost;
969 
970 	optstr = opts = strdup(mnt->mnt_mntopts);
971 	/* sizeof (MNTOPT_XXX) includes one extra byte we may need for "," */
972 	optlen = strlen(mnt->mnt_mntopts) + sizeof (MNTOPT_XATTR) + 1;
973 	if (optlen > MAX_MNTOPT_STR) {
974 		pr_err(gettext("option string too long"));
975 		return (RET_ERR);
976 	}
977 	newopts = malloc(optlen);
978 	if (opts == NULL || newopts == NULL) {
979 		pr_err(gettext("no memory"));
980 		if (opts)
981 			free(opts);
982 		if (newopts)
983 			free(newopts);
984 		return (RET_ERR);
985 	}
986 	newopts[0] = '\0';
987 
988 	while (*opts) {
989 		invalid = 0;
990 		saveopt = opts;
991 		switch (getsubopt(&opts, optlist, &val)) {
992 		case OPT_RO:
993 			*mntflags |= MS_RDONLY;
994 			break;
995 		case OPT_RW:
996 			*mntflags &= ~(MS_RDONLY);
997 			break;
998 		case OPT_QUOTA:
999 		case OPT_NOQUOTA:
1000 			break;
1001 		case OPT_SOFT:
1002 			args->flags |= NFSMNT_SOFT;
1003 			args->flags &= ~(NFSMNT_SEMISOFT);
1004 			break;
1005 		case OPT_SEMISOFT:
1006 			args->flags |= NFSMNT_SOFT;
1007 			args->flags |= NFSMNT_SEMISOFT;
1008 			break;
1009 		case OPT_HARD:
1010 			args->flags &= ~(NFSMNT_SOFT);
1011 			args->flags &= ~(NFSMNT_SEMISOFT);
1012 			break;
1013 		case OPT_SUID:
1014 			*mntflags &= ~(MS_NOSUID);
1015 			break;
1016 		case OPT_NOSUID:
1017 			*mntflags |= MS_NOSUID;
1018 			break;
1019 		case OPT_GRPID:
1020 			args->flags |= NFSMNT_GRPID;
1021 			break;
1022 		case OPT_REMOUNT:
1023 			*mntflags |= MS_REMOUNT;
1024 			break;
1025 		case OPT_INTR:
1026 			args->flags |= NFSMNT_INT;
1027 			break;
1028 		case OPT_NOINTR:
1029 			args->flags &= ~(NFSMNT_INT);
1030 			break;
1031 		case OPT_NOAC:
1032 			args->flags |= NFSMNT_NOAC;
1033 			break;
1034 		case OPT_PORT:
1035 			if (bad(val))
1036 				goto badopt;
1037 			nfs_port = htons((ushort_t)atoi(val));
1038 			break;
1039 
1040 		case OPT_SECURE:
1041 			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
1042 				pr_err(gettext("can not get \"dh\" from %s\n"),
1043 				    NFSSEC_CONF);
1044 				goto badopt;
1045 			}
1046 			sec_opt++;
1047 			break;
1048 
1049 		case OPT_NOCTO:
1050 			args->flags |= NFSMNT_NOCTO;
1051 			break;
1052 
1053 		case OPT_RSIZE:
1054 			args->flags |= NFSMNT_RSIZE;
1055 			if (bad(val))
1056 				goto badopt;
1057 			args->rsize = atoi(val);
1058 			break;
1059 		case OPT_WSIZE:
1060 			args->flags |= NFSMNT_WSIZE;
1061 			if (bad(val))
1062 				goto badopt;
1063 			args->wsize = atoi(val);
1064 			break;
1065 		case OPT_TIMEO:
1066 			args->flags |= NFSMNT_TIMEO;
1067 			if (bad(val))
1068 				goto badopt;
1069 			args->timeo = atoi(val);
1070 			break;
1071 		case OPT_RETRANS:
1072 			args->flags |= NFSMNT_RETRANS;
1073 			if (bad(val))
1074 				goto badopt;
1075 			args->retrans = atoi(val);
1076 			break;
1077 		case OPT_ACTIMEO:
1078 			args->flags |= NFSMNT_ACDIRMAX;
1079 			args->flags |= NFSMNT_ACREGMAX;
1080 			args->flags |= NFSMNT_ACDIRMIN;
1081 			args->flags |= NFSMNT_ACREGMIN;
1082 			if (bad(val))
1083 				goto badopt;
1084 			args->acdirmin = args->acregmin = args->acdirmax
1085 			    = args->acregmax = atoi(val);
1086 			break;
1087 		case OPT_ACREGMIN:
1088 			args->flags |= NFSMNT_ACREGMIN;
1089 			if (bad(val))
1090 				goto badopt;
1091 			args->acregmin = atoi(val);
1092 			break;
1093 		case OPT_ACREGMAX:
1094 			args->flags |= NFSMNT_ACREGMAX;
1095 			if (bad(val))
1096 				goto badopt;
1097 			args->acregmax = atoi(val);
1098 			break;
1099 		case OPT_ACDIRMIN:
1100 			args->flags |= NFSMNT_ACDIRMIN;
1101 			if (bad(val))
1102 				goto badopt;
1103 			args->acdirmin = atoi(val);
1104 			break;
1105 		case OPT_ACDIRMAX:
1106 			args->flags |= NFSMNT_ACDIRMAX;
1107 			if (bad(val))
1108 				goto badopt;
1109 			args->acdirmax = atoi(val);
1110 			break;
1111 		case OPT_BG:
1112 			bg++;
1113 			break;
1114 		case OPT_FG:
1115 			bg = 0;
1116 			break;
1117 		case OPT_RETRY:
1118 			if (bad(val))
1119 				goto badopt;
1120 			retries = atoi(val);
1121 			break;
1122 		case OPT_LLOCK:
1123 			args->flags |= NFSMNT_LLOCK;
1124 			break;
1125 		case OPT_POSIX:
1126 			posix = 1;
1127 			break;
1128 		case OPT_VERS:
1129 			if (bad(val))
1130 				goto badopt;
1131 			nfsvers = (rpcvers_t)atoi(val);
1132 			break;
1133 		case OPT_PROTO:
1134 			if (val == NULL)
1135 				goto badopt;
1136 
1137 			nfs_proto = (char *)malloc(strlen(val)+1);
1138 			if (!nfs_proto) {
1139 				pr_err(gettext("no memory"));
1140 				return (RET_ERR);
1141 			}
1142 
1143 			(void) strncpy(nfs_proto, val, strlen(val)+1);
1144 			break;
1145 
1146 		case OPT_NOPRINT:
1147 			args->flags |= NFSMNT_NOPRINT;
1148 			break;
1149 
1150 		case OPT_LARGEFILES:
1151 			largefiles = 1;
1152 			break;
1153 
1154 		case OPT_NOLARGEFILES:
1155 			pr_err(gettext("NFS can't support \"nolargefiles\"\n"));
1156 			free(optstr);
1157 			return (RET_ERR);
1158 
1159 		case OPT_SEC:
1160 			if (val == NULL) {
1161 				pr_err(gettext(
1162 				    "\"sec\" option requires argument\n"));
1163 				return (RET_ERR);
1164 			}
1165 			if (nfs_getseconfig_byname(val, &nfs_sec)) {
1166 				pr_err(gettext("can not get \"%s\" from %s\n"),
1167 				    val, NFSSEC_CONF);
1168 				return (RET_ERR);
1169 			}
1170 			sec_opt++;
1171 			break;
1172 
1173 		case OPT_PUBLIC:
1174 			public_opt = TRUE;
1175 			break;
1176 
1177 		case OPT_DIRECTIO:
1178 			args->flags |= NFSMNT_DIRECTIO;
1179 			break;
1180 
1181 		case OPT_NODIRECTIO:
1182 			args->flags &= ~(NFSMNT_DIRECTIO);
1183 			break;
1184 
1185 		case OPT_XATTR:
1186 		case OPT_NOXATTR:
1187 			/*
1188 			 * VFS options; just need to get them into the
1189 			 * new mount option string and note we've seen them
1190 			 */
1191 			attrpref = 1;
1192 			break;
1193 		default:
1194 			/*
1195 			 * Note that this could be a valid OPT_* option so
1196 			 * we can't use "val" but need to use "saveopt".
1197 			 */
1198 			if (fsisstdopt(saveopt))
1199 				break;
1200 			invalid = 1;
1201 			if (!qflg)
1202 				(void) fprintf(stderr, gettext(
1203 				    "mount: %s on %s - WARNING unknown option"
1204 				    " \"%s\"\n"), mnt->mnt_special,
1205 				    mnt->mnt_mountp, saveopt);
1206 			break;
1207 		}
1208 		if (!invalid) {
1209 			if (newopts[0])
1210 				strcat(newopts, ",");
1211 			strcat(newopts, saveopt);
1212 		}
1213 	}
1214 	/* Default is to turn extended attrs on */
1215 	if (!attrpref) {
1216 		if (newopts[0])
1217 			strcat(newopts, ",");
1218 		strcat(newopts, MNTOPT_XATTR);
1219 	}
1220 	strcpy(mnt->mnt_mntopts, newopts);
1221 	free(newopts);
1222 	free(optstr);
1223 
1224 	/* ensure that only one secure mode is requested */
1225 	if (sec_opt > 1) {
1226 		pr_err(gettext("Security options conflict\n"));
1227 		return (RET_ERR);
1228 	}
1229 
1230 	/* ensure that the user isn't trying to get large files over V2 */
1231 	if (nfsvers == NFS_VERSION && largefiles) {
1232 		pr_err(gettext("NFS V2 can't support \"largefiles\"\n"));
1233 		return (RET_ERR);
1234 	}
1235 
1236 	if (nfsvers == NFS_V4 &&
1237 	    nfs_proto != NULL &&
1238 	    strncasecmp(nfs_proto, NC_UDP, strlen(NC_UDP)) == 0) {
1239 		pr_err(gettext("NFS V4 does not support %s\n"), nfs_proto);
1240 		return (RET_ERR);
1241 	}
1242 
1243 	return (RET_OK);
1244 
1245 badopt:
1246 	pr_err(gettext("invalid option: \"%s\"\n"), saveopt);
1247 	free(optstr);
1248 	return (RET_ERR);
1249 }
1250 
1251 static int
1252 make_secure(struct nfs_args *args, char *hostname, struct netconfig *nconf,
1253 	bool_t use_pubfh, rpcvers_t vers)
1254 {
1255 	sec_data_t *secdata;
1256 	int flags;
1257 	struct netbuf *syncaddr = NULL;
1258 	struct nd_addrlist *retaddrs = NULL;
1259 	char netname[MAXNETNAMELEN+1];
1260 
1261 	/*
1262 	 * check to see if any secure mode is requested.
1263 	 * if not, use default security mode.
1264 	 */
1265 	if (!snego_done && !sec_opt) {
1266 		/*
1267 		 * Get default security mode.
1268 		 * AUTH_UNIX has been the default choice for a long time.
1269 		 * The better NFS security service becomes, the better chance
1270 		 * we will set stronger security service as the default NFS
1271 		 * security mode.
1272 		 */
1273 		if (nfs_getseconfig_default(&nfs_sec)) {
1274 			pr_err(gettext("error getting default"
1275 			    " security entry\n"));
1276 			return (-1);
1277 		}
1278 		args->flags |= NFSMNT_SECDEFAULT;
1279 	}
1280 
1281 	/*
1282 	 * Get the network address for the time service on the server.
1283 	 * If an RPC based time service is not available then try the
1284 	 * IP time service.
1285 	 *
1286 	 * This is for AUTH_DH processing. We will also pass down syncaddr
1287 	 * and netname for NFS V4 even if AUTH_DH is not requested right now.
1288 	 * NFS V4 does security negotiation in the kernel via SECINFO.
1289 	 * These information might be needed later in the kernel.
1290 	 *
1291 	 * Eventurally, we want to move this code to nfs_clnt_secdata()
1292 	 * when autod_nfs.c and mount.c can share the same get_the_addr()
1293 	 * routine.
1294 	 */
1295 	flags = 0;
1296 	syncaddr = NULL;
1297 
1298 	if (nfs_sec.sc_rpcnum == AUTH_DH || vers == NFS_V4) {
1299 		/*
1300 		 * If using the public fh or nfsv4, we will not contact the
1301 		 * remote RPCBINDer, since it is possibly behind a firewall.
1302 		 */
1303 		if (use_pubfh == FALSE && vers != NFS_V4)
1304 			syncaddr = get_the_addr(hostname, RPCBPROG, RPCBVERS,
1305 			    nconf, 0, NULL, NULL, FALSE, NULL, NULL);
1306 
1307 		if (syncaddr != NULL) {
1308 			/* for flags in sec_data */
1309 			flags |= AUTH_F_RPCTIMESYNC;
1310 		} else {
1311 			struct nd_hostserv hs;
1312 			int error;
1313 
1314 			hs.h_host = hostname;
1315 			hs.h_serv = "timserver";
1316 
1317 			error = netdir_getbyname(nconf, &hs, &retaddrs);
1318 
1319 			if (error != ND_OK && (nfs_sec.sc_rpcnum == AUTH_DH)) {
1320 				pr_err(gettext("%s: secure: no time service\n"),
1321 				    hostname);
1322 				return (-1);
1323 			}
1324 
1325 			if (error == ND_OK)
1326 				syncaddr = retaddrs->n_addrs;
1327 
1328 			/*
1329 			 * For NFS_V4 if AUTH_DH is negotiated later in the
1330 			 * kernel thru SECINFO, it will need syncaddr
1331 			 * and netname data.
1332 			 */
1333 			if (vers == NFS_V4 && syncaddr &&
1334 			    host2netname(netname, hostname, NULL)) {
1335 				args->syncaddr = malloc(sizeof (struct netbuf));
1336 				args->syncaddr->buf = malloc(syncaddr->len);
1337 				(void) memcpy(args->syncaddr->buf,
1338 				    syncaddr->buf, syncaddr->len);
1339 				args->syncaddr->len = syncaddr->len;
1340 				args->syncaddr->maxlen = syncaddr->maxlen;
1341 				args->netname = strdup(netname);
1342 				args->flags |= NFSMNT_SECURE;
1343 			}
1344 		}
1345 	}
1346 
1347 	/*
1348 	 * For the initial chosen flavor (any flavor defined in nfssec.conf),
1349 	 * the data will be stored in the sec_data structure via
1350 	 * nfs_clnt_secdata() and be passed to the kernel via nfs_args_*
1351 	 * extended data structure.
1352 	 */
1353 	if (!(secdata = nfs_clnt_secdata(&nfs_sec, hostname, args->knconf,
1354 	    syncaddr, flags))) {
1355 		pr_err(gettext("errors constructing security related data\n"));
1356 		if (flags & AUTH_F_RPCTIMESYNC) {
1357 			free(syncaddr->buf);
1358 			free(syncaddr);
1359 		} else if (retaddrs)
1360 			netdir_free((void *)retaddrs, ND_ADDRLIST);
1361 		return (-1);
1362 	}
1363 
1364 	NFS_ARGS_EXTB_secdata(args, secdata);
1365 	if (flags & AUTH_F_RPCTIMESYNC) {
1366 		free(syncaddr->buf);
1367 		free(syncaddr);
1368 	} else if (retaddrs)
1369 		netdir_free((void *)retaddrs, ND_ADDRLIST);
1370 	return (0);
1371 }
1372 
1373 /*
1374  * Get the network address on "hostname" for program "prog"
1375  * with version "vers" by using the nconf configuration data
1376  * passed in.
1377  *
1378  * If the address of a netconfig pointer is null then
1379  * information is not sufficient and no netbuf will be returned.
1380  *
1381  * Finally, ping the null procedure of that service.
1382  *
1383  * A similar routine is also defined in ../../autofs/autod_nfs.c.
1384  * This is a potential routine to move to ../lib for common usage.
1385  */
1386 static struct netbuf *
1387 get_the_addr(char *hostname, ulong_t prog, ulong_t vers,
1388 	struct netconfig *nconf, ushort_t port, struct t_info *tinfo,
1389 	caddr_t *fhp, bool_t get_pubfh, char *fspath, err_ret_t *error)
1390 {
1391 	struct netbuf *nb = NULL;
1392 	struct t_bind *tbind = NULL;
1393 	CLIENT *cl = NULL;
1394 	struct timeval tv;
1395 	int fd = -1;
1396 	AUTH *ah = NULL;
1397 	AUTH *new_ah = NULL;
1398 	struct snego_t snego;
1399 
1400 	if (nconf == NULL)
1401 		return (NULL);
1402 
1403 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) == -1)
1404 		goto done;
1405 
1406 	/* LINTED pointer alignment */
1407 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
1408 	    == NULL)
1409 		goto done;
1410 
1411 	/*
1412 	 * In the case of public filehandle usage or NFSv4 we want to
1413 	 * avoid use of the rpcbind/portmap protocol
1414 	 */
1415 	if ((get_pubfh == TRUE) || (vers == NFS_V4)) {
1416 		struct nd_hostserv hs;
1417 		struct nd_addrlist *retaddrs;
1418 		int retval;
1419 		hs.h_host = hostname;
1420 
1421 		/* NFS where vers==4 does not support UDP */
1422 		if (vers == NFS_V4 &&
1423 		    strncasecmp(nconf->nc_proto, NC_UDP,
1424 		    strlen(NC_UDP)) == 0) {
1425 			SET_ERR_RET(error, ERR_PROTO_UNSUPP, 0);
1426 			goto done;
1427 		}
1428 
1429 		if (port == 0)
1430 			hs.h_serv = "nfs";
1431 		else
1432 			hs.h_serv = NULL;
1433 
1434 		if ((retval = netdir_getbyname(nconf, &hs, &retaddrs))
1435 		    != ND_OK) {
1436 			/*
1437 			 * Carefully set the error value here. Want to signify
1438 			 * that the error was an unknown host.
1439 			 */
1440 			if (retval == ND_NOHOST) {
1441 				SET_ERR_RET(error, ERR_NOHOST, retval);
1442 			}
1443 
1444 			goto done;
1445 		}
1446 		memcpy(tbind->addr.buf, retaddrs->n_addrs->buf,
1447 		    retaddrs->n_addrs->len);
1448 		tbind->addr.len = retaddrs->n_addrs->len;
1449 		netdir_free((void *)retaddrs, ND_ADDRLIST);
1450 		(void) netdir_options(nconf, ND_SET_RESERVEDPORT, fd, NULL);
1451 
1452 	} else {
1453 		if (rpcb_getaddr(prog, vers, nconf, &tbind->addr,
1454 		    hostname) == FALSE) {
1455 			goto done;
1456 		}
1457 	}
1458 
1459 	if (port) {
1460 		/* LINTED pointer alignment */
1461 		if (strcmp(nconf->nc_protofmly, NC_INET) == 0)
1462 			((struct sockaddr_in *)tbind->addr.buf)->sin_port
1463 			    = port;
1464 		else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
1465 			((struct sockaddr_in6 *)tbind->addr.buf)->sin6_port
1466 			    = port;
1467 
1468 	}
1469 
1470 	cl = clnt_tli_create(fd, nconf, &tbind->addr, prog, vers, 0, 0);
1471 	if (cl == NULL) {
1472 		/*
1473 		 * clnt_tli_create() returns either RPC_SYSTEMERROR,
1474 		 * RPC_UNKNOWNPROTO or RPC_TLIERROR. The RPC_TLIERROR translates
1475 		 * to "Misc. TLI error". This is not too helpful. Most likely
1476 		 * the connection to the remote server timed out, so this
1477 		 * error is at least less perplexing.
1478 		 * See: usr/src/cmd/rpcinfo/rpcinfo.c
1479 		 */
1480 		if (rpc_createerr.cf_stat == RPC_TLIERROR) {
1481 			SET_ERR_RET(error, ERR_RPCERROR, RPC_PMAPFAILURE);
1482 		} else {
1483 			SET_ERR_RET(error, ERR_RPCERROR, rpc_createerr.cf_stat);
1484 		}
1485 		goto done;
1486 	}
1487 
1488 	ah = authsys_create_default();
1489 	if (ah != NULL)
1490 		cl->cl_auth = ah;
1491 
1492 	tv.tv_sec = 5;
1493 	tv.tv_usec = 0;
1494 
1495 	(void) clnt_control(cl, CLSET_TIMEOUT, (char *)&tv);
1496 
1497 	if ((get_pubfh == TRUE) && (vers != NFS_V4)) {
1498 		enum snego_stat sec;
1499 
1500 		if (!snego_done) {
1501 			/*
1502 			 * negotiate sec flavor.
1503 			 */
1504 			snego.cnt = 0;
1505 			if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
1506 			    SNEGO_SUCCESS) {
1507 				int jj;
1508 
1509 				/*
1510 				 * check if server supports the one
1511 				 * specified in the sec= option.
1512 				 */
1513 				if (sec_opt) {
1514 					for (jj = 0; jj < snego.cnt; jj++) {
1515 						if (snego.array[jj] ==
1516 						    nfs_sec.sc_nfsnum) {
1517 							snego_done = TRUE;
1518 							break;
1519 						}
1520 					}
1521 				}
1522 
1523 				/*
1524 				 * find a common sec flavor
1525 				 */
1526 				if (!snego_done) {
1527 					if (sec_opt) {
1528 						pr_err(gettext(
1529 						    "Server does not support"
1530 						    " the security flavor"
1531 						    " specified.\n"));
1532 					}
1533 
1534 					for (jj = 0; jj < snego.cnt; jj++) {
1535 						if (!nfs_getseconfig_bynumber(
1536 						    snego.array[jj],
1537 						    &nfs_sec)) {
1538 							snego_done = TRUE;
1539 #define	EMSG80SUX "Security flavor %d was negotiated and will be used.\n"
1540 							if (sec_opt)
1541 								pr_err(gettext(
1542 								    EMSG80SUX),
1543 								    nfs_sec.
1544 								    sc_nfsnum);
1545 							break;
1546 						}
1547 					}
1548 				}
1549 
1550 				if (!snego_done)
1551 					return (NULL);
1552 
1553 				/*
1554 				 * Now that the flavor has been
1555 				 * negotiated, get the fh.
1556 				 *
1557 				 * First, create an auth handle using the
1558 				 * negotiated sec flavor in the next lookup to
1559 				 * fetch the filehandle.
1560 				 */
1561 				new_ah = nfs_create_ah(cl, hostname, &nfs_sec);
1562 				if (new_ah == NULL)
1563 					goto done;
1564 				cl->cl_auth = new_ah;
1565 			} else if (sec == SNEGO_ARRAY_TOO_SMALL || sec ==
1566 			    SNEGO_FAILURE) {
1567 				goto done;
1568 			}
1569 
1570 			/*
1571 			 * Note that if sec == SNEGO_DEF_VALID
1572 			 * default sec flavor is acceptable.
1573 			 * Use it to get the filehandle.
1574 			 */
1575 		}
1576 
1577 		if (vers == NFS_VERSION) {
1578 			wnl_diropargs arg;
1579 			wnl_diropres *res;
1580 
1581 			memset((char *)&arg.dir, 0, sizeof (wnl_fh));
1582 			arg.name = fspath;
1583 			res = wnlproc_lookup_2(&arg, cl);
1584 
1585 			if (res == NULL || res->status != NFS_OK)
1586 				goto done;
1587 			*fhp = malloc(sizeof (wnl_fh));
1588 
1589 			if (*fhp == NULL) {
1590 				pr_err(gettext("no memory\n"));
1591 				goto done;
1592 			}
1593 
1594 			memcpy((char *)*fhp,
1595 			    (char *)&res->wnl_diropres_u.wnl_diropres.file,
1596 			    sizeof (wnl_fh));
1597 		} else {
1598 			WNL_LOOKUP3args arg;
1599 			WNL_LOOKUP3res *res;
1600 			nfs_fh3 *fh3p;
1601 
1602 			memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
1603 			arg.what.name = fspath;
1604 			res = wnlproc3_lookup_3(&arg, cl);
1605 
1606 			if (res == NULL || res->status != NFS3_OK)
1607 				goto done;
1608 
1609 			fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
1610 
1611 			if (fh3p == NULL) {
1612 				pr_err(gettext("no memory\n"));
1613 				CLNT_FREERES(cl, xdr_WNL_LOOKUP3res,
1614 				    (char *)res);
1615 				goto done;
1616 			}
1617 
1618 			fh3p->fh3_length =
1619 			    res->WNL_LOOKUP3res_u.res_ok.object.data.data_len;
1620 			memcpy(fh3p->fh3_u.data,
1621 			    res->WNL_LOOKUP3res_u.res_ok.object.data.data_val,
1622 			    fh3p->fh3_length);
1623 
1624 			*fhp = (caddr_t)fh3p;
1625 			CLNT_FREERES(cl, xdr_WNL_LOOKUP3res, (char *)res);
1626 		}
1627 	} else {
1628 		void *res;
1629 		struct rpc_err r_err;
1630 
1631 		if (vers == NFS_VERSION)
1632 			res = wnlproc_null_2(NULL, cl);
1633 		else if (vers == NFS_V3)
1634 			res = wnlproc3_null_3(NULL, cl);
1635 		else
1636 			res = wnlproc4_null_4(NULL, cl);
1637 
1638 		if (res == NULL) {
1639 			clnt_geterr(cl, &r_err);
1640 			if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
1641 				switch (r_err.re_status) {
1642 				case RPC_TLIERROR:
1643 				case RPC_CANTRECV:
1644 				case RPC_CANTSEND:
1645 					r_err.re_status = RPC_PROGVERSMISMATCH;
1646 				}
1647 			}
1648 			SET_ERR_RET(error, ERR_RPCERROR, r_err.re_status);
1649 			goto done;
1650 		}
1651 	}
1652 
1653 	/*
1654 	 * Make a copy of the netbuf to return
1655 	 */
1656 	nb = (struct netbuf *)malloc(sizeof (*nb));
1657 	if (nb == NULL) {
1658 		pr_err(gettext("no memory\n"));
1659 		goto done;
1660 	}
1661 	*nb = tbind->addr;
1662 	nb->buf = (char *)malloc(nb->maxlen);
1663 	if (nb->buf == NULL) {
1664 		pr_err(gettext("no memory\n"));
1665 		free(nb);
1666 		nb = NULL;
1667 		goto done;
1668 	}
1669 	(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
1670 
1671 done:
1672 	if (cl) {
1673 		if (ah != NULL) {
1674 			if (new_ah != NULL)
1675 				AUTH_DESTROY(ah);
1676 			AUTH_DESTROY(cl->cl_auth);
1677 			cl->cl_auth = NULL;
1678 		}
1679 		clnt_destroy(cl);
1680 		cl = NULL;
1681 	}
1682 	if (tbind) {
1683 		t_free((char *)tbind, T_BIND);
1684 		tbind = NULL;
1685 	}
1686 	if (fd >= 0)
1687 		(void) t_close(fd);
1688 	return (nb);
1689 }
1690 
1691 static int
1692 check_nconf(struct netconfig *nconf, int nthtry, int *valid_proto)
1693 {
1694 	int	try_test = 0;
1695 	int	valid_family;
1696 	char	*proto = NULL;
1697 
1698 
1699 	if (nthtry == FIRST_TRY) {
1700 		try_test = ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
1701 		    (nconf->nc_semantics == NC_TPI_COTS));
1702 		proto = NC_TCP;
1703 	} else if (nthtry == SECOND_TRY) {
1704 		try_test = (nconf->nc_semantics == NC_TPI_CLTS);
1705 		proto = NC_UDP;
1706 	}
1707 
1708 	if (proto &&
1709 	    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
1710 	    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
1711 	    (strcmp(nconf->nc_proto, proto) == 0))
1712 		*valid_proto = TRUE;
1713 	else
1714 		*valid_proto = FALSE;
1715 
1716 	return (try_test);
1717 }
1718 
1719 /*
1720  * Get a network address on "hostname" for program "prog"
1721  * with version "vers".  If the port number is specified (non zero)
1722  * then try for a TCP/UDP transport and set the port number of the
1723  * resulting IP address.
1724  *
1725  * If the address of a netconfig pointer was passed and
1726  * if it's not null, use it as the netconfig otherwise
1727  * assign the address of the netconfig that was used to
1728  * establish contact with the service.
1729  *
1730  * A similar routine is also defined in ../../autofs/autod_nfs.c.
1731  * This is a potential routine to move to ../lib for common usage.
1732  *
1733  * "error" refers to a more descriptive term when get_addr fails
1734  * and returns NULL: ERR_PROTO_NONE if no error introduced by
1735  * -o proto option, ERR_NETPATH if error found in NETPATH
1736  * environment variable, ERR_PROTO_INVALID if an unrecognized
1737  * protocol is specified by user, and ERR_PROTO_UNSUPP for a
1738  * recognized but invalid protocol (eg. ticlts, ticots, etc.).
1739  * "error" is ignored if get_addr returns non-NULL result.
1740  *
1741  */
1742 static struct netbuf *
1743 get_addr(char *hostname, ulong_t prog, ulong_t vers, struct netconfig **nconfp,
1744 	char *proto, ushort_t port, struct t_info *tinfo, caddr_t *fhp,
1745 	bool_t get_pubfh, char *fspath, err_ret_t *error)
1746 {
1747 	struct netbuf *nb = NULL;
1748 	struct netconfig *nconf = NULL;
1749 	NCONF_HANDLE *nc = NULL;
1750 	int nthtry = FIRST_TRY;
1751 	err_ret_t errsave_nohost, errsave_rpcerr;
1752 
1753 	SET_ERR_RET(&errsave_nohost, ERR_PROTO_NONE, 0);
1754 	SET_ERR_RET(&errsave_rpcerr, ERR_PROTO_NONE, 0);
1755 
1756 	SET_ERR_RET(error, ERR_PROTO_NONE, 0);
1757 
1758 	if (nconfp && *nconfp)
1759 		return (get_the_addr(hostname, prog, vers, *nconfp, port,
1760 		    tinfo, fhp, get_pubfh, fspath, error));
1761 	/*
1762 	 * No nconf passed in.
1763 	 *
1764 	 * Try to get a nconf from /etc/netconfig filtered by
1765 	 * the NETPATH environment variable.
1766 	 * First search for COTS, second for CLTS unless proto
1767 	 * is specified.  When we retry, we reset the
1768 	 * netconfig list so that we would search the whole list
1769 	 * all over again.
1770 	 */
1771 
1772 	if ((nc = setnetpath()) == NULL) {
1773 		/* should only return an error if problems with NETPATH */
1774 		/* In which case you are hosed */
1775 		SET_ERR_RET(error, ERR_NETPATH, 0);
1776 		goto done;
1777 	}
1778 
1779 	/*
1780 	 * If proto is specified, then only search for the match,
1781 	 * otherwise try COTS first, if failed, try CLTS.
1782 	 */
1783 	if (proto) {
1784 		/* no matching proto name */
1785 		SET_ERR_RET(error, ERR_PROTO_INVALID, 0);
1786 
1787 		while (nconf = getnetpath(nc)) {
1788 			if (strcmp(nconf->nc_netid, proto))
1789 				continue;
1790 
1791 			/* may be unsupported */
1792 			SET_ERR_RET(error, ERR_PROTO_UNSUPP, 0);
1793 
1794 			if ((port != 0) &&
1795 			    ((strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
1796 			    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
1797 			    (strcmp(nconf->nc_proto, NC_TCP) != 0 &&
1798 			    strcmp(nconf->nc_proto, NC_UDP) != 0))) {
1799 				continue;
1800 			} else {
1801 				nb = get_the_addr(hostname, prog,
1802 				    vers, nconf, port, tinfo,
1803 				    fhp, get_pubfh, fspath, error);
1804 
1805 				if (nb != NULL)
1806 					break;
1807 
1808 				/* nb is NULL - deal with errors */
1809 				if (error) {
1810 					if (error->error_type == ERR_NOHOST)
1811 						SET_ERR_RET(&errsave_nohost,
1812 						    error->error_type,
1813 						    error->error_value);
1814 					if (error->error_type == ERR_RPCERROR)
1815 						SET_ERR_RET(&errsave_rpcerr,
1816 						    error->error_type,
1817 						    error->error_value);
1818 				}
1819 				/*
1820 				 * continue with same protocol
1821 				 * selection
1822 				 */
1823 				continue;
1824 			}
1825 		} /* end of while */
1826 
1827 		if (nconf == NULL)
1828 			goto done;
1829 
1830 		if ((nb = get_the_addr(hostname, prog, vers, nconf, port,
1831 		    tinfo, fhp, get_pubfh, fspath, error)) == NULL)
1832 			goto done;
1833 	} else {
1834 retry:
1835 		SET_ERR_RET(error, ERR_NETPATH, 0);
1836 		while (nconf = getnetpath(nc)) {
1837 			SET_ERR_RET(error, ERR_PROTO_NONE, 0);
1838 
1839 			if (nconf->nc_flag & NC_VISIBLE) {
1840 				int	valid_proto;
1841 
1842 				if (check_nconf(nconf,
1843 				    nthtry, &valid_proto)) {
1844 					if (port == 0)
1845 						break;
1846 
1847 					if (valid_proto == TRUE)
1848 						break;
1849 				}
1850 			}
1851 		} /* while */
1852 		if (nconf == NULL) {
1853 			if (++nthtry <= MNT_PREF_LISTLEN) {
1854 				endnetpath(nc);
1855 				if ((nc = setnetpath()) == NULL)
1856 					goto done;
1857 				goto retry;
1858 			} else
1859 				goto done;
1860 		} else {
1861 			if ((nb = get_the_addr(hostname, prog, vers, nconf,
1862 			    port, tinfo, fhp, get_pubfh, fspath, error))
1863 			    == NULL) {
1864 				/* nb is NULL - deal with errors */
1865 				if (error) {
1866 					if (error->error_type == ERR_NOHOST)
1867 						SET_ERR_RET(&errsave_nohost,
1868 						    error->error_type,
1869 						    error->error_value);
1870 					if (error->error_type == ERR_RPCERROR)
1871 						SET_ERR_RET(&errsave_rpcerr,
1872 						    error->error_type,
1873 						    error->error_value);
1874 				}
1875 				/*
1876 				 * Continue the same search path in the
1877 				 * netconfig db until no more matched
1878 				 * nconf (nconf == NULL).
1879 				 */
1880 				goto retry;
1881 			}
1882 		}
1883 	}
1884 	SET_ERR_RET(error, ERR_PROTO_NONE, 0);
1885 
1886 	/*
1887 	 * Got nconf and nb.  Now dup the netconfig structure (nconf)
1888 	 * and return it thru nconfp.
1889 	 */
1890 	*nconfp = getnetconfigent(nconf->nc_netid);
1891 	if (*nconfp == NULL) {
1892 		syslog(LOG_ERR, "no memory\n");
1893 		free(nb);
1894 		nb = NULL;
1895 	}
1896 done:
1897 	if (nc)
1898 		endnetpath(nc);
1899 
1900 	if (nb == NULL) {
1901 		/*
1902 		 * Check the saved errors. The RPC error has *
1903 		 * precedence over the no host error.
1904 		 */
1905 		if (errsave_nohost.error_type != ERR_PROTO_NONE)
1906 			SET_ERR_RET(error, errsave_nohost.error_type,
1907 			    errsave_nohost.error_value);
1908 
1909 		if (errsave_rpcerr.error_type != ERR_PROTO_NONE)
1910 			SET_ERR_RET(error, errsave_rpcerr.error_type,
1911 			    errsave_rpcerr.error_value);
1912 	}
1913 
1914 	return (nb);
1915 }
1916 
1917 /*
1918  * Get a file handle usinging multi-component lookup with the public
1919  * file handle.
1920  */
1921 static int
1922 get_fh_via_pub(struct nfs_args *args, char *fshost, char *fspath, bool_t url,
1923 	bool_t loud, int *versp, struct netconfig **nconfp, ushort_t port)
1924 {
1925 	uint_t vers_min;
1926 	uint_t vers_max;
1927 	int r;
1928 	char *path;
1929 
1930 	if (nfsvers != 0) {
1931 		vers_max = vers_min = nfsvers;
1932 	} else {
1933 		vers_max = vers_max_default;
1934 		vers_min = vers_min_default;
1935 	}
1936 
1937 	if (url == FALSE) {
1938 		path = malloc(strlen(fspath) + 2);
1939 		if (path == NULL) {
1940 			if (loud == TRUE)
1941 				pr_err(gettext("no memory\n"));
1942 			return (RET_ERR);
1943 		}
1944 
1945 		path[0] = (char)WNL_NATIVEPATH;
1946 		(void) strcpy(&path[1], fspath);
1947 
1948 	} else  {
1949 		path = fspath;
1950 	}
1951 
1952 	for (nfsvers_to_use = vers_max; nfsvers_to_use >= vers_min;
1953 	    nfsvers_to_use--) {
1954 		/*
1955 		 * getaddr_nfs will also fill in the fh for us.
1956 		 */
1957 		r = getaddr_nfs(args, fshost, nconfp,
1958 		    TRUE, path, port, NULL, FALSE);
1959 
1960 		if (r == RET_OK) {
1961 			/*
1962 			 * Since we are using the public fh, and NLM is
1963 			 * not firewall friendly, use local locking.
1964 			 * Not the case for v4.
1965 			 */
1966 			*versp = nfsvers_to_use;
1967 			switch (nfsvers_to_use) {
1968 			case NFS_V4:
1969 				fstype = MNTTYPE_NFS4;
1970 				break;
1971 			case NFS_V3:
1972 				fstype = MNTTYPE_NFS3;
1973 				/* fall through to pick up llock option */
1974 			default:
1975 				args->flags |= NFSMNT_LLOCK;
1976 				break;
1977 			}
1978 			if (fspath != path)
1979 				free(path);
1980 
1981 			return (r);
1982 		}
1983 	}
1984 
1985 	if (fspath != path)
1986 		free(path);
1987 
1988 	if (loud == TRUE) {
1989 		pr_err(gettext("Could not use public filehandle in request to"
1990 		    " server %s\n"), fshost);
1991 	}
1992 
1993 	return (r);
1994 }
1995 
1996 /*
1997  * get fhandle of remote path from server's mountd
1998  */
1999 static int
2000 get_fh(struct nfs_args *args, char *fshost, char *fspath, int *versp,
2001 	bool_t loud_on_mnt_err, struct netconfig **nconfp, ushort_t port)
2002 {
2003 	static struct fhstatus fhs;
2004 	static struct mountres3 mountres3;
2005 	static struct pathcnf p;
2006 	nfs_fh3 *fh3p;
2007 	struct timeval timeout = { 25, 0};
2008 	CLIENT *cl;
2009 	enum clnt_stat rpc_stat;
2010 	rpcvers_t outvers = 0;
2011 	rpcvers_t vers_to_try;
2012 	rpcvers_t vers_min;
2013 	static int printed = 0;
2014 	int count, i, *auths;
2015 	char *msg;
2016 
2017 	switch (nfsvers) {
2018 	case 2: /* version 2 specified try that only */
2019 		vers_to_try = MOUNTVERS_POSIX;
2020 		vers_min = MOUNTVERS;
2021 		break;
2022 	case 3: /* version 3 specified try that only */
2023 		vers_to_try = MOUNTVERS3;
2024 		vers_min = MOUNTVERS3;
2025 		break;
2026 	case 4: /* version 4 specified try that only */
2027 		/*
2028 		 * This assignment is in the wrong version sequence.
2029 		 * The above are MOUNT program and this is NFS
2030 		 * program.  However, it happens to work out since the
2031 		 * two don't collide for NFSv4.
2032 		 */
2033 		vers_to_try = NFS_V4;
2034 		vers_min = NFS_V4;
2035 		break;
2036 	default: /* no version specified, start with default */
2037 		/*
2038 		 * If the retry version is set, use that. This will
2039 		 * be set if the last mount attempt returned any other
2040 		 * besides an RPC error.
2041 		 */
2042 		if (nfsretry_vers)
2043 			vers_to_try = nfsretry_vers;
2044 		else {
2045 			vers_to_try = vers_max_default;
2046 			vers_min = vers_min_default;
2047 		}
2048 
2049 		break;
2050 	}
2051 
2052 	/*
2053 	 * In the case of version 4, just NULL proc the server since
2054 	 * there is no MOUNT program.  If this fails, then decrease
2055 	 * vers_to_try and continue on with regular MOUNT program
2056 	 * processing.
2057 	 */
2058 	if (vers_to_try == NFS_V4) {
2059 		int savevers = nfsvers_to_use;
2060 		err_ret_t error;
2061 		int retval;
2062 		SET_ERR_RET(&error, ERR_PROTO_NONE, 0);
2063 
2064 		/* Let's hope for the best */
2065 		nfsvers_to_use = NFS_V4;
2066 		retval = getaddr_nfs(args, fshost, nconfp, FALSE,
2067 		    fspath, port, &error, vers_min == NFS_V4);
2068 
2069 		if (retval == RET_OK) {
2070 			*versp = nfsvers_to_use = NFS_V4;
2071 			fstype = MNTTYPE_NFS4;
2072 			args->fh = strdup(fspath);
2073 			if (args->fh == NULL) {
2074 				pr_err(gettext("no memory\n"));
2075 				*versp = nfsvers_to_use = savevers;
2076 				return (RET_ERR);
2077 			}
2078 			return (RET_OK);
2079 		}
2080 		nfsvers_to_use = savevers;
2081 
2082 		vers_to_try--;
2083 		/* If no more versions to try, let the user know. */
2084 		if (vers_to_try < vers_min)
2085 			return (retval);
2086 
2087 		/*
2088 		 * If we are here, there are more versions to try but
2089 		 * there has been an error of some sort.  If it is not
2090 		 * an RPC error (e.g. host unknown), we just stop and
2091 		 * return the error since the other versions would see
2092 		 * the same error as well.
2093 		 */
2094 		if (retval == RET_ERR && error.error_type != ERR_RPCERROR)
2095 			return (retval);
2096 	}
2097 
2098 	while ((cl = clnt_create_vers(fshost, MOUNTPROG, &outvers,
2099 	    vers_min, vers_to_try, "datagram_v")) == NULL) {
2100 		if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST) {
2101 			pr_err(gettext("%s: %s\n"), fshost,
2102 			    clnt_spcreateerror(""));
2103 			return (RET_ERR);
2104 		}
2105 
2106 		/*
2107 		 * We don't want to downgrade version on lost packets
2108 		 */
2109 		if ((rpc_createerr.cf_stat == RPC_TIMEDOUT) ||
2110 		    (rpc_createerr.cf_stat == RPC_PMAPFAILURE)) {
2111 			pr_err(gettext("%s: %s\n"), fshost,
2112 			    clnt_spcreateerror(""));
2113 			return (RET_RETRY);
2114 		}
2115 
2116 		/*
2117 		 * back off and try the previous version - patch to the
2118 		 * problem of version numbers not being contigous and
2119 		 * clnt_create_vers failing (SunOS4.1 clients & SGI servers)
2120 		 * The problem happens with most non-Sun servers who
2121 		 * don't support mountd protocol #2. So, in case the
2122 		 * call fails, we re-try the call anyway.
2123 		 */
2124 		vers_to_try--;
2125 		if (vers_to_try < vers_min) {
2126 			if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH) {
2127 				if (nfsvers == 0) {
2128 					pr_err(gettext(
2129 			"%s:%s: no applicable versions of NFS supported\n"),
2130 					    fshost, fspath);
2131 				} else {
2132 					pr_err(gettext(
2133 			"%s:%s: NFS Version %d not supported\n"),
2134 					    fshost, fspath, nfsvers);
2135 				}
2136 				return (RET_ERR);
2137 			}
2138 			if (!printed) {
2139 				pr_err(gettext("%s: %s\n"), fshost,
2140 				    clnt_spcreateerror(""));
2141 				printed = 1;
2142 			}
2143 			return (RET_RETRY);
2144 		}
2145 	}
2146 	if (posix && outvers < MOUNTVERS_POSIX) {
2147 		pr_err(gettext("%s: %s: no pathconf info\n"),
2148 		    fshost, clnt_sperror(cl, ""));
2149 		clnt_destroy(cl);
2150 		return (RET_ERR);
2151 	}
2152 
2153 	if (__clnt_bindresvport(cl) < 0) {
2154 		pr_err(gettext("Couldn't bind to reserved port\n"));
2155 		clnt_destroy(cl);
2156 		return (RET_RETRY);
2157 	}
2158 
2159 	if ((cl->cl_auth = authsys_create_default()) == NULL) {
2160 		pr_err(
2161 		    gettext("Couldn't create default authentication handle\n"));
2162 		clnt_destroy(cl);
2163 		return (RET_RETRY);
2164 	}
2165 
2166 	switch (outvers) {
2167 	case MOUNTVERS:
2168 	case MOUNTVERS_POSIX:
2169 		*versp = nfsvers_to_use = NFS_VERSION;
2170 		rpc_stat = clnt_call(cl, MOUNTPROC_MNT, xdr_dirpath,
2171 		    (caddr_t)&fspath, xdr_fhstatus, (caddr_t)&fhs, timeout);
2172 		if (rpc_stat != RPC_SUCCESS) {
2173 			pr_err(gettext("%s:%s: server not responding %s\n"),
2174 			    fshost, fspath, clnt_sperror(cl, ""));
2175 			clnt_destroy(cl);
2176 			return (RET_RETRY);
2177 		}
2178 
2179 		if ((errno = fhs.fhs_status) != MNT_OK) {
2180 			if (loud_on_mnt_err) {
2181 				if (errno == EACCES) {
2182 					pr_err(gettext(
2183 					    "%s:%s: access denied\n"),
2184 					    fshost, fspath);
2185 				} else {
2186 					pr_err(gettext("%s:%s: %s\n"), fshost,
2187 					    fspath, errno >= 0 ?
2188 					    strerror(errno) : "invalid error "
2189 					    "returned by server");
2190 				}
2191 			}
2192 			clnt_destroy(cl);
2193 			return (RET_MNTERR);
2194 		}
2195 		args->fh = malloc(sizeof (fhs.fhstatus_u.fhs_fhandle));
2196 		if (args->fh == NULL) {
2197 			pr_err(gettext("no memory\n"));
2198 			return (RET_ERR);
2199 		}
2200 		memcpy((caddr_t)args->fh, (caddr_t)&fhs.fhstatus_u.fhs_fhandle,
2201 		    sizeof (fhs.fhstatus_u.fhs_fhandle));
2202 		if (!errno && posix) {
2203 			rpc_stat = clnt_call(cl, MOUNTPROC_PATHCONF,
2204 			    xdr_dirpath, (caddr_t)&fspath, xdr_ppathcnf,
2205 			    (caddr_t)&p, timeout);
2206 			if (rpc_stat != RPC_SUCCESS) {
2207 				pr_err(gettext(
2208 				    "%s:%s: server not responding %s\n"),
2209 				    fshost, fspath, clnt_sperror(cl, ""));
2210 				free(args->fh);
2211 				clnt_destroy(cl);
2212 				return (RET_RETRY);
2213 			}
2214 			if (_PC_ISSET(_PC_ERROR, p.pc_mask)) {
2215 				pr_err(gettext(
2216 				    "%s:%s: no pathconf info\n"),
2217 				    fshost, fspath);
2218 				free(args->fh);
2219 				clnt_destroy(cl);
2220 				return (RET_ERR);
2221 			}
2222 			args->flags |= NFSMNT_POSIX;
2223 			args->pathconf = malloc(sizeof (p));
2224 			if (args->pathconf == NULL) {
2225 				pr_err(gettext("no memory\n"));
2226 				free(args->fh);
2227 				clnt_destroy(cl);
2228 				return (RET_ERR);
2229 			}
2230 			memcpy((caddr_t)args->pathconf, (caddr_t)&p,
2231 			    sizeof (p));
2232 		}
2233 		break;
2234 
2235 	case MOUNTVERS3:
2236 		*versp = nfsvers_to_use = NFS_V3;
2237 		rpc_stat = clnt_call(cl, MOUNTPROC_MNT, xdr_dirpath,
2238 		    (caddr_t)&fspath, xdr_mountres3, (caddr_t)&mountres3,
2239 		    timeout);
2240 		if (rpc_stat != RPC_SUCCESS) {
2241 			pr_err(gettext("%s:%s: server not responding %s\n"),
2242 			    fshost, fspath, clnt_sperror(cl, ""));
2243 			clnt_destroy(cl);
2244 			return (RET_RETRY);
2245 		}
2246 
2247 		/*
2248 		 * Assume here that most of the MNT3ERR_*
2249 		 * codes map into E* errors.
2250 		 */
2251 		if ((errno = mountres3.fhs_status) != MNT_OK) {
2252 			if (loud_on_mnt_err) {
2253 				switch (errno) {
2254 				case MNT3ERR_NAMETOOLONG:
2255 					msg = "path name is too long";
2256 					break;
2257 				case MNT3ERR_NOTSUPP:
2258 					msg = "operation not supported";
2259 					break;
2260 				case MNT3ERR_SERVERFAULT:
2261 					msg = "server fault";
2262 					break;
2263 				default:
2264 					if (errno >= 0)
2265 						msg = strerror(errno);
2266 					else
2267 						msg = "invalid error returned "
2268 						    "by server";
2269 					break;
2270 				}
2271 				pr_err(gettext("%s:%s: %s\n"), fshost,
2272 				    fspath, msg);
2273 			}
2274 			clnt_destroy(cl);
2275 			return (RET_MNTERR);
2276 		}
2277 
2278 		fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
2279 		if (fh3p == NULL) {
2280 			pr_err(gettext("no memory\n"));
2281 			return (RET_ERR);
2282 		}
2283 		fh3p->fh3_length =
2284 		    mountres3.mountres3_u.mountinfo.fhandle.fhandle3_len;
2285 		(void) memcpy(fh3p->fh3_u.data,
2286 		    mountres3.mountres3_u.mountinfo.fhandle.fhandle3_val,
2287 		    fh3p->fh3_length);
2288 		args->fh = (caddr_t)fh3p;
2289 		fstype = MNTTYPE_NFS3;
2290 
2291 		/*
2292 		 * Check the security flavor to be used.
2293 		 *
2294 		 * If "secure" or "sec=flavor" is a mount
2295 		 * option, check if the server supports the "flavor".
2296 		 * If the server does not support the flavor, return
2297 		 * error.
2298 		 *
2299 		 * If no mount option is given then use the first supported
2300 		 * security flavor (by the client) in the auth list returned
2301 		 * from the server.
2302 		 *
2303 		 */
2304 		auths =
2305 		    mountres3.mountres3_u.mountinfo.auth_flavors
2306 		    .auth_flavors_val;
2307 		count =
2308 		    mountres3.mountres3_u.mountinfo.auth_flavors
2309 		    .auth_flavors_len;
2310 
2311 		if (sec_opt) {
2312 			for (i = 0; i < count; i++) {
2313 				if (auths[i] == nfs_sec.sc_nfsnum)
2314 					break;
2315 			}
2316 			if (i >= count)
2317 				goto autherr;
2318 		} else {
2319 			if (count < 0)
2320 				break;
2321 
2322 			for (i = 0; i < count; i++) {
2323 				if (!nfs_getseconfig_bynumber(auths[i],
2324 				    &nfs_sec)) {
2325 					sec_opt++;
2326 					break;
2327 				}
2328 			}
2329 
2330 			if (i >= count)
2331 				goto autherr;
2332 		}
2333 		break;
2334 	default:
2335 		pr_err(gettext("%s:%s: Unknown MOUNT version %d\n"),
2336 		    fshost, fspath, outvers);
2337 		clnt_destroy(cl);
2338 		return (RET_ERR);
2339 	}
2340 
2341 	clnt_destroy(cl);
2342 	return (RET_OK);
2343 
2344 autherr:
2345 	pr_err(gettext(
2346 	    "security mode does not match the server exporting %s:%s\n"),
2347 	    fshost, fspath);
2348 	clnt_destroy(cl);
2349 	return (RET_ERR);
2350 }
2351 
2352 /*
2353  * Fill in the address for the server's NFS service and
2354  * fill in a knetconfig structure for the transport that
2355  * the service is available on.
2356  */
2357 static int
2358 getaddr_nfs(struct nfs_args *args, char *fshost, struct netconfig **nconfp,
2359 	    bool_t get_pubfh, char *fspath, ushort_t port, err_ret_t *error,
2360 	    bool_t print_rpcerror)
2361 {
2362 	struct stat sb;
2363 	struct netconfig *nconf;
2364 	struct knetconfig *knconfp;
2365 	static int printed = 0;
2366 	struct t_info tinfo;
2367 	err_ret_t addr_error;
2368 
2369 	SET_ERR_RET(error, ERR_PROTO_NONE, 0);
2370 	SET_ERR_RET(&addr_error, ERR_PROTO_NONE, 0);
2371 
2372 	if (nfs_proto) {
2373 		/*
2374 		 * If a proto is specified and its rdma try this. The kernel
2375 		 * will later do the reachablity test and fail form there
2376 		 * if rdma transport is not available to kernel rpc
2377 		 */
2378 		if (strcmp(nfs_proto, "rdma") == 0) {
2379 			args->addr = get_addr(fshost, NFS_PROGRAM,
2380 			    nfsvers_to_use, nconfp, NULL, port, &tinfo,
2381 			    &args->fh, get_pubfh, fspath, &addr_error);
2382 
2383 			args->flags |= NFSMNT_DORDMA;
2384 		} else {
2385 			args->addr = get_addr(fshost, NFS_PROGRAM,
2386 			    nfsvers_to_use, nconfp, nfs_proto, port, &tinfo,
2387 			    &args->fh, get_pubfh, fspath, &addr_error);
2388 		}
2389 	} else {
2390 		args->addr = get_addr(fshost, NFS_PROGRAM, nfsvers_to_use,
2391 		    nconfp, nfs_proto, port, &tinfo, &args->fh, get_pubfh,
2392 		    fspath, &addr_error);
2393 		/*
2394 		 * If no proto is specified set this flag.
2395 		 * Kernel mount code will try to use RDMA if its on the
2396 		 * system, otherwise it will keep on using the protocol
2397 		 * selected here, through the above get_addr call.
2398 		 */
2399 		if (nfs_proto == NULL)
2400 			args->flags |= NFSMNT_TRYRDMA;
2401 	}
2402 
2403 	if (args->addr == NULL) {
2404 		/*
2405 		 * We could have failed because the server had no public
2406 		 * file handle support. So don't print a message and don't
2407 		 * retry.
2408 		 */
2409 		if (get_pubfh == TRUE)
2410 			return (RET_ERR);
2411 
2412 		if (!printed) {
2413 			switch (addr_error.error_type) {
2414 			case 0:
2415 				printed = 1;
2416 				break;
2417 			case ERR_RPCERROR:
2418 				if (!print_rpcerror)
2419 					/* no error print at this time */
2420 					break;
2421 				pr_err(gettext("%s NFS service not"
2422 				    " available %s\n"), fshost,
2423 				    clnt_sperrno(addr_error.error_value));
2424 				printed = 1;
2425 				break;
2426 			case ERR_NETPATH:
2427 				pr_err(gettext("%s: Error in NETPATH.\n"),
2428 				    fshost);
2429 				printed = 1;
2430 				break;
2431 			case ERR_PROTO_INVALID:
2432 				pr_err(gettext("%s: NFS service does not"
2433 				    " recognize protocol: %s.\n"), fshost,
2434 				    nfs_proto);
2435 				printed = 1;
2436 				break;
2437 			case ERR_PROTO_UNSUPP:
2438 				if (nfsvers || nfsvers_to_use == NFS_VERSMIN) {
2439 					/*
2440 					 * Don't set "printed" here. Since we
2441 					 * have to keep checking here till we
2442 					 * exhaust transport errors on all vers.
2443 					 *
2444 					 * Print this message if:
2445 					 * 1. After we have tried all versions
2446 					 *    of NFS and none support the asked
2447 					 *    transport.
2448 					 *
2449 					 * 2. If a version is specified and it
2450 					 *    does'nt support the asked
2451 					 *    transport.
2452 					 *
2453 					 * Otherwise we decrement the version
2454 					 * and retry below.
2455 					 */
2456 					pr_err(gettext("%s: NFS service does"
2457 					    " not support protocol: %s.\n"),
2458 					    fshost, nfs_proto);
2459 				}
2460 				break;
2461 			case ERR_NOHOST:
2462 				pr_err("%s: %s\n", fshost, "Unknown host");
2463 				printed = 1;
2464 				break;
2465 			default:
2466 				/* case ERR_PROTO_NONE falls through */
2467 				pr_err(gettext("%s: NFS service not responding"
2468 				    "\n"), fshost);
2469 				printed = 1;
2470 				break;
2471 			}
2472 		}
2473 		SET_ERR_RET(error,
2474 		    addr_error.error_type, addr_error.error_value);
2475 		if (addr_error.error_type == ERR_PROTO_NONE)
2476 			return (RET_RETRY);
2477 		else if (addr_error.error_type == ERR_RPCERROR &&
2478 		    !IS_UNRECOVERABLE_RPC(addr_error.error_value)) {
2479 			return (RET_RETRY);
2480 		} else if (nfsvers == 0 && addr_error.error_type ==
2481 		    ERR_PROTO_UNSUPP && nfsvers_to_use != NFS_VERSMIN) {
2482 			/*
2483 			 * If no version is specified, and the error is due
2484 			 * to an unsupported transport, then decrement the
2485 			 * version and retry.
2486 			 */
2487 			return (RET_RETRY);
2488 		} else
2489 			return (RET_ERR);
2490 	}
2491 	nconf = *nconfp;
2492 
2493 	if (stat(nconf->nc_device, &sb) < 0) {
2494 		pr_err(gettext("getaddr_nfs: couldn't stat: %s: %s\n"),
2495 		    nconf->nc_device, strerror(errno));
2496 		return (RET_ERR);
2497 	}
2498 
2499 	knconfp = (struct knetconfig *)malloc(sizeof (*knconfp));
2500 	if (!knconfp) {
2501 		pr_err(gettext("no memory\n"));
2502 		return (RET_ERR);
2503 	}
2504 	knconfp->knc_semantics = nconf->nc_semantics;
2505 	knconfp->knc_protofmly = nconf->nc_protofmly;
2506 	knconfp->knc_proto = nconf->nc_proto;
2507 	knconfp->knc_rdev = sb.st_rdev;
2508 
2509 	/* make sure we don't overload the transport */
2510 	if (tinfo.tsdu > 0 && tinfo.tsdu < NFS_MAXDATA + NFS_RPC_HDR) {
2511 		args->flags |= (NFSMNT_RSIZE | NFSMNT_WSIZE);
2512 		if (args->rsize == 0 || args->rsize > tinfo.tsdu - NFS_RPC_HDR)
2513 			args->rsize = tinfo.tsdu - NFS_RPC_HDR;
2514 		if (args->wsize == 0 || args->wsize > tinfo.tsdu - NFS_RPC_HDR)
2515 			args->wsize = tinfo.tsdu - NFS_RPC_HDR;
2516 	}
2517 
2518 	args->flags |= NFSMNT_KNCONF;
2519 	args->knconf = knconfp;
2520 	return (RET_OK);
2521 }
2522 
2523 static int
2524 retry(struct mnttab *mntp, int ro)
2525 {
2526 	int delay = 5;
2527 	int count = retries;
2528 	int r;
2529 
2530 	/*
2531 	 * Please see comments on nfsretry_vers in the beginning of this file
2532 	 * and in main() routine.
2533 	 */
2534 
2535 	if (bg) {
2536 		if (fork() > 0)
2537 			return (RET_OK);
2538 		backgrounded = 1;
2539 		pr_err(gettext("backgrounding: %s\n"), mntp->mnt_mountp);
2540 	} else {
2541 		if (!nfsretry_vers)
2542 			pr_err(gettext("retrying: %s\n"), mntp->mnt_mountp);
2543 	}
2544 
2545 	while (count--) {
2546 		if ((r = mount_nfs(mntp, ro, NULL)) == RET_OK) {
2547 			pr_err(gettext("%s: mounted OK\n"), mntp->mnt_mountp);
2548 			return (RET_OK);
2549 		}
2550 		if (r != RET_RETRY)
2551 			break;
2552 
2553 		if (count > 0) {
2554 			(void) sleep(delay);
2555 			delay *= 2;
2556 			if (delay > 120)
2557 				delay = 120;
2558 		}
2559 	}
2560 
2561 	if (!nfsretry_vers)
2562 		pr_err(gettext("giving up on: %s\n"), mntp->mnt_mountp);
2563 
2564 	return (RET_ERR);
2565 }
2566 
2567 /*
2568  * Read the /etc/default/nfs configuration file to determine if the
2569  * client has been configured for a new min/max for the NFS version to
2570  * use.
2571  */
2572 static void
2573 read_default(void)
2574 {
2575 	char *defval;
2576 	int errno;
2577 	int tmp;
2578 
2579 	/* Fail silently if error in opening the default nfs config file */
2580 	if ((defopen(NFSADMIN)) == 0) {
2581 		if ((defval = defread("NFS_CLIENT_VERSMIN=")) != NULL) {
2582 			errno = 0;
2583 			tmp = strtol(defval, (char **)NULL, 10);
2584 			if (errno == 0) {
2585 				vers_min_default = tmp;
2586 			}
2587 		}
2588 		if ((defval = defread("NFS_CLIENT_VERSMAX=")) != NULL) {
2589 			errno = 0;
2590 			tmp = strtol(defval, (char **)NULL, 10);
2591 			if (errno == 0) {
2592 				vers_max_default = tmp;
2593 			}
2594 		}
2595 		/* close defaults file */
2596 		defopen(NULL);
2597 	}
2598 }
2599 
2600 static void
2601 sigusr1(int s)
2602 {
2603 }
2604