xref: /titanic_41/usr/src/cmd/fs.d/nfs/mount/mount.c (revision 5b7f77ad52bf657ba49d64d16f527e958d0fb820)
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 2007 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 (nfs_getseconfig_byname(val, &nfs_sec)) {
1161 				pr_err(gettext("can not get \"%s\" from %s\n"),
1162 				    val, NFSSEC_CONF);
1163 				return (RET_ERR);
1164 			}
1165 			sec_opt++;
1166 			break;
1167 
1168 		case OPT_PUBLIC:
1169 			public_opt = TRUE;
1170 			break;
1171 
1172 		case OPT_DIRECTIO:
1173 			args->flags |= NFSMNT_DIRECTIO;
1174 			break;
1175 
1176 		case OPT_NODIRECTIO:
1177 			args->flags &= ~(NFSMNT_DIRECTIO);
1178 			break;
1179 
1180 		case OPT_XATTR:
1181 		case OPT_NOXATTR:
1182 			/*
1183 			 * VFS options; just need to get them into the
1184 			 * new mount option string and note we've seen them
1185 			 */
1186 			attrpref = 1;
1187 			break;
1188 		default:
1189 			/*
1190 			 * Note that this could be a valid OPT_* option so
1191 			 * we can't use "val" but need to use "saveopt".
1192 			 */
1193 			if (fsisstdopt(saveopt))
1194 				break;
1195 			invalid = 1;
1196 			if (!qflg)
1197 				(void) fprintf(stderr, gettext(
1198 				    "mount: %s on %s - WARNING unknown option"
1199 				    " \"%s\"\n"), mnt->mnt_special,
1200 				    mnt->mnt_mountp, saveopt);
1201 			break;
1202 		}
1203 		if (!invalid) {
1204 			if (newopts[0])
1205 				strcat(newopts, ",");
1206 			strcat(newopts, saveopt);
1207 		}
1208 	}
1209 	/* Default is to turn extended attrs on */
1210 	if (!attrpref) {
1211 		if (newopts[0])
1212 			strcat(newopts, ",");
1213 		strcat(newopts, MNTOPT_XATTR);
1214 	}
1215 	strcpy(mnt->mnt_mntopts, newopts);
1216 	free(newopts);
1217 	free(optstr);
1218 
1219 	/* ensure that only one secure mode is requested */
1220 	if (sec_opt > 1) {
1221 		pr_err(gettext("Security options conflict\n"));
1222 		return (RET_ERR);
1223 	}
1224 
1225 	/* ensure that the user isn't trying to get large files over V2 */
1226 	if (nfsvers == NFS_VERSION && largefiles) {
1227 		pr_err(gettext("NFS V2 can't support \"largefiles\"\n"));
1228 		return (RET_ERR);
1229 	}
1230 
1231 	if (nfsvers == NFS_V4 &&
1232 	    nfs_proto != NULL &&
1233 	    strncasecmp(nfs_proto, NC_UDP, strlen(NC_UDP)) == 0) {
1234 		pr_err(gettext("NFS V4 does not support %s\n"), nfs_proto);
1235 		return (RET_ERR);
1236 	}
1237 
1238 	return (RET_OK);
1239 
1240 badopt:
1241 	pr_err(gettext("invalid option: \"%s\"\n"), saveopt);
1242 	free(optstr);
1243 	return (RET_ERR);
1244 }
1245 
1246 static int
1247 make_secure(struct nfs_args *args, char *hostname, struct netconfig *nconf,
1248 	bool_t use_pubfh, rpcvers_t vers)
1249 {
1250 	sec_data_t *secdata;
1251 	int flags;
1252 	struct netbuf *syncaddr = NULL;
1253 	struct nd_addrlist *retaddrs = NULL;
1254 	char netname[MAXNETNAMELEN+1];
1255 
1256 	/*
1257 	 * check to see if any secure mode is requested.
1258 	 * if not, use default security mode.
1259 	 */
1260 	if (!snego_done && !sec_opt) {
1261 		/*
1262 		 * Get default security mode.
1263 		 * AUTH_UNIX has been the default choice for a long time.
1264 		 * The better NFS security service becomes, the better chance
1265 		 * we will set stronger security service as the default NFS
1266 		 * security mode.
1267 		 */
1268 		if (nfs_getseconfig_default(&nfs_sec)) {
1269 			pr_err(gettext("error getting default"
1270 			    " security entry\n"));
1271 			return (-1);
1272 		}
1273 		args->flags |= NFSMNT_SECDEFAULT;
1274 	}
1275 
1276 	/*
1277 	 * Get the network address for the time service on the server.
1278 	 * If an RPC based time service is not available then try the
1279 	 * IP time service.
1280 	 *
1281 	 * This is for AUTH_DH processing. We will also pass down syncaddr
1282 	 * and netname for NFS V4 even if AUTH_DH is not requested right now.
1283 	 * NFS V4 does security negotiation in the kernel via SECINFO.
1284 	 * These information might be needed later in the kernel.
1285 	 *
1286 	 * Eventurally, we want to move this code to nfs_clnt_secdata()
1287 	 * when autod_nfs.c and mount.c can share the same get_the_addr()
1288 	 * routine.
1289 	 */
1290 	flags = 0;
1291 	syncaddr = NULL;
1292 
1293 	if (nfs_sec.sc_rpcnum == AUTH_DH || vers == NFS_V4) {
1294 		/*
1295 		 * If using the public fh or nfsv4, we will not contact the
1296 		 * remote RPCBINDer, since it is possibly behind a firewall.
1297 		 */
1298 		if (use_pubfh == FALSE && vers != NFS_V4)
1299 			syncaddr = get_the_addr(hostname, RPCBPROG, RPCBVERS,
1300 			    nconf, 0, NULL, NULL, FALSE, NULL, NULL);
1301 
1302 		if (syncaddr != NULL) {
1303 			/* for flags in sec_data */
1304 			flags |= AUTH_F_RPCTIMESYNC;
1305 		} else {
1306 			struct nd_hostserv hs;
1307 			int error;
1308 
1309 			hs.h_host = hostname;
1310 			hs.h_serv = "timserver";
1311 
1312 			error = netdir_getbyname(nconf, &hs, &retaddrs);
1313 
1314 			if (error != ND_OK && (nfs_sec.sc_rpcnum == AUTH_DH)) {
1315 				pr_err(gettext("%s: secure: no time service\n"),
1316 				    hostname);
1317 				return (-1);
1318 			}
1319 
1320 			if (error == ND_OK)
1321 				syncaddr = retaddrs->n_addrs;
1322 
1323 			/*
1324 			 * For NFS_V4 if AUTH_DH is negotiated later in the
1325 			 * kernel thru SECINFO, it will need syncaddr
1326 			 * and netname data.
1327 			 */
1328 			if (vers == NFS_V4 && syncaddr &&
1329 			    host2netname(netname, hostname, NULL)) {
1330 				args->syncaddr = malloc(sizeof (struct netbuf));
1331 				args->syncaddr->buf = malloc(syncaddr->len);
1332 				(void) memcpy(args->syncaddr->buf,
1333 				    syncaddr->buf, syncaddr->len);
1334 				args->syncaddr->len = syncaddr->len;
1335 				args->syncaddr->maxlen = syncaddr->maxlen;
1336 				args->netname = strdup(netname);
1337 				args->flags |= NFSMNT_SECURE;
1338 			}
1339 		}
1340 	}
1341 
1342 	/*
1343 	 * For the initial chosen flavor (any flavor defined in nfssec.conf),
1344 	 * the data will be stored in the sec_data structure via
1345 	 * nfs_clnt_secdata() and be passed to the kernel via nfs_args_*
1346 	 * extended data structure.
1347 	 */
1348 	if (!(secdata = nfs_clnt_secdata(&nfs_sec, hostname, args->knconf,
1349 	    syncaddr, flags))) {
1350 		pr_err(gettext("errors constructing security related data\n"));
1351 		if (flags & AUTH_F_RPCTIMESYNC) {
1352 			free(syncaddr->buf);
1353 			free(syncaddr);
1354 		} else if (retaddrs)
1355 			netdir_free((void *)retaddrs, ND_ADDRLIST);
1356 		return (-1);
1357 	}
1358 
1359 	NFS_ARGS_EXTB_secdata(args, secdata);
1360 	if (flags & AUTH_F_RPCTIMESYNC) {
1361 		free(syncaddr->buf);
1362 		free(syncaddr);
1363 	} else if (retaddrs)
1364 		netdir_free((void *)retaddrs, ND_ADDRLIST);
1365 	return (0);
1366 }
1367 
1368 /*
1369  * Get the network address on "hostname" for program "prog"
1370  * with version "vers" by using the nconf configuration data
1371  * passed in.
1372  *
1373  * If the address of a netconfig pointer is null then
1374  * information is not sufficient and no netbuf will be returned.
1375  *
1376  * Finally, ping the null procedure of that service.
1377  *
1378  * A similar routine is also defined in ../../autofs/autod_nfs.c.
1379  * This is a potential routine to move to ../lib for common usage.
1380  */
1381 static struct netbuf *
1382 get_the_addr(char *hostname, ulong_t prog, ulong_t vers,
1383 	struct netconfig *nconf, ushort_t port, struct t_info *tinfo,
1384 	caddr_t *fhp, bool_t get_pubfh, char *fspath, err_ret_t *error)
1385 {
1386 	struct netbuf *nb = NULL;
1387 	struct t_bind *tbind = NULL;
1388 	CLIENT *cl = NULL;
1389 	struct timeval tv;
1390 	int fd = -1;
1391 	AUTH *ah = NULL;
1392 	AUTH *new_ah = NULL;
1393 	struct snego_t snego;
1394 
1395 	if (nconf == NULL)
1396 		return (NULL);
1397 
1398 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) == -1)
1399 		goto done;
1400 
1401 	/* LINTED pointer alignment */
1402 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
1403 	    == NULL)
1404 		goto done;
1405 
1406 	/*
1407 	 * In the case of public filehandle usage or NFSv4 we want to
1408 	 * avoid use of the rpcbind/portmap protocol
1409 	 */
1410 	if ((get_pubfh == TRUE) || (vers == NFS_V4)) {
1411 		struct nd_hostserv hs;
1412 		struct nd_addrlist *retaddrs;
1413 		int retval;
1414 		hs.h_host = hostname;
1415 
1416 		/* NFS where vers==4 does not support UDP */
1417 		if (vers == NFS_V4 &&
1418 		    strncasecmp(nconf->nc_proto, NC_UDP,
1419 		    strlen(NC_UDP)) == 0) {
1420 			SET_ERR_RET(error, ERR_PROTO_UNSUPP, 0);
1421 			goto done;
1422 		}
1423 
1424 		if (port == 0)
1425 			hs.h_serv = "nfs";
1426 		else
1427 			hs.h_serv = NULL;
1428 
1429 		if ((retval = netdir_getbyname(nconf, &hs, &retaddrs))
1430 		    != ND_OK) {
1431 			/*
1432 			 * Carefully set the error value here. Want to signify
1433 			 * that the error was an unknown host.
1434 			 */
1435 			if (retval == ND_NOHOST) {
1436 				SET_ERR_RET(error, ERR_NOHOST, retval);
1437 			}
1438 
1439 			goto done;
1440 		}
1441 		memcpy(tbind->addr.buf, retaddrs->n_addrs->buf,
1442 		    retaddrs->n_addrs->len);
1443 		tbind->addr.len = retaddrs->n_addrs->len;
1444 		netdir_free((void *)retaddrs, ND_ADDRLIST);
1445 		(void) netdir_options(nconf, ND_SET_RESERVEDPORT, fd, NULL);
1446 
1447 	} else {
1448 		if (rpcb_getaddr(prog, vers, nconf, &tbind->addr,
1449 		    hostname) == FALSE) {
1450 			goto done;
1451 		}
1452 	}
1453 
1454 	if (port) {
1455 		/* LINTED pointer alignment */
1456 		if (strcmp(nconf->nc_protofmly, NC_INET) == 0)
1457 			((struct sockaddr_in *)tbind->addr.buf)->sin_port
1458 			    = port;
1459 		else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
1460 			((struct sockaddr_in6 *)tbind->addr.buf)->sin6_port
1461 			    = port;
1462 
1463 	}
1464 
1465 	cl = clnt_tli_create(fd, nconf, &tbind->addr, prog, vers, 0, 0);
1466 	if (cl == NULL) {
1467 		/*
1468 		 * clnt_tli_create() returns either RPC_SYSTEMERROR,
1469 		 * RPC_UNKNOWNPROTO or RPC_TLIERROR. The RPC_TLIERROR translates
1470 		 * to "Misc. TLI error". This is not too helpful. Most likely
1471 		 * the connection to the remote server timed out, so this
1472 		 * error is at least less perplexing.
1473 		 * See: usr/src/cmd/rpcinfo/rpcinfo.c
1474 		 */
1475 		if (rpc_createerr.cf_stat == RPC_TLIERROR) {
1476 			SET_ERR_RET(error, ERR_RPCERROR, RPC_PMAPFAILURE);
1477 		} else {
1478 			SET_ERR_RET(error, ERR_RPCERROR, rpc_createerr.cf_stat);
1479 		}
1480 		goto done;
1481 	}
1482 
1483 	ah = authsys_create_default();
1484 	if (ah != NULL)
1485 		cl->cl_auth = ah;
1486 
1487 	tv.tv_sec = 5;
1488 	tv.tv_usec = 0;
1489 
1490 	(void) clnt_control(cl, CLSET_TIMEOUT, (char *)&tv);
1491 
1492 	if ((get_pubfh == TRUE) && (vers != NFS_V4)) {
1493 		enum snego_stat sec;
1494 
1495 		if (!snego_done) {
1496 			/*
1497 			 * negotiate sec flavor.
1498 			 */
1499 			snego.cnt = 0;
1500 			if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
1501 			    SNEGO_SUCCESS) {
1502 				int jj;
1503 
1504 				/*
1505 				 * check if server supports the one
1506 				 * specified in the sec= option.
1507 				 */
1508 				if (sec_opt) {
1509 					for (jj = 0; jj < snego.cnt; jj++) {
1510 						if (snego.array[jj] ==
1511 						    nfs_sec.sc_nfsnum) {
1512 							snego_done = TRUE;
1513 							break;
1514 						}
1515 					}
1516 				}
1517 
1518 				/*
1519 				 * find a common sec flavor
1520 				 */
1521 				if (!snego_done) {
1522 					if (sec_opt) {
1523 						pr_err(gettext(
1524 						    "Server does not support"
1525 						    " the security flavor"
1526 						    " specified.\n"));
1527 					}
1528 
1529 					for (jj = 0; jj < snego.cnt; jj++) {
1530 						if (!nfs_getseconfig_bynumber(
1531 						    snego.array[jj],
1532 						    &nfs_sec)) {
1533 							snego_done = TRUE;
1534 #define	EMSG80SUX "Security flavor %d was negotiated and will be used.\n"
1535 							if (sec_opt)
1536 								pr_err(gettext(
1537 								    EMSG80SUX),
1538 								    nfs_sec.
1539 								    sc_nfsnum);
1540 							break;
1541 						}
1542 					}
1543 				}
1544 
1545 				if (!snego_done)
1546 					return (NULL);
1547 
1548 				/*
1549 				 * Now that the flavor has been
1550 				 * negotiated, get the fh.
1551 				 *
1552 				 * First, create an auth handle using the
1553 				 * negotiated sec flavor in the next lookup to
1554 				 * fetch the filehandle.
1555 				 */
1556 				new_ah = nfs_create_ah(cl, hostname, &nfs_sec);
1557 				if (new_ah == NULL)
1558 					goto done;
1559 				cl->cl_auth = new_ah;
1560 			} else if (sec == SNEGO_ARRAY_TOO_SMALL || sec ==
1561 			    SNEGO_FAILURE) {
1562 				goto done;
1563 			}
1564 
1565 			/*
1566 			 * Note that if sec == SNEGO_DEF_VALID
1567 			 * default sec flavor is acceptable.
1568 			 * Use it to get the filehandle.
1569 			 */
1570 		}
1571 
1572 		if (vers == NFS_VERSION) {
1573 			wnl_diropargs arg;
1574 			wnl_diropres *res;
1575 
1576 			memset((char *)&arg.dir, 0, sizeof (wnl_fh));
1577 			arg.name = fspath;
1578 			res = wnlproc_lookup_2(&arg, cl);
1579 
1580 			if (res == NULL || res->status != NFS_OK)
1581 				goto done;
1582 			*fhp = malloc(sizeof (wnl_fh));
1583 
1584 			if (*fhp == NULL) {
1585 				pr_err(gettext("no memory\n"));
1586 				goto done;
1587 			}
1588 
1589 			memcpy((char *)*fhp,
1590 			    (char *)&res->wnl_diropres_u.wnl_diropres.file,
1591 			    sizeof (wnl_fh));
1592 		} else {
1593 			WNL_LOOKUP3args arg;
1594 			WNL_LOOKUP3res *res;
1595 			nfs_fh3 *fh3p;
1596 
1597 			memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
1598 			arg.what.name = fspath;
1599 			res = wnlproc3_lookup_3(&arg, cl);
1600 
1601 			if (res == NULL || res->status != NFS3_OK)
1602 				goto done;
1603 
1604 			fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
1605 
1606 			if (fh3p == NULL) {
1607 				pr_err(gettext("no memory\n"));
1608 				CLNT_FREERES(cl, xdr_WNL_LOOKUP3res,
1609 				    (char *)res);
1610 				goto done;
1611 			}
1612 
1613 			fh3p->fh3_length =
1614 			    res->WNL_LOOKUP3res_u.res_ok.object.data.data_len;
1615 			memcpy(fh3p->fh3_u.data,
1616 			    res->WNL_LOOKUP3res_u.res_ok.object.data.data_val,
1617 			    fh3p->fh3_length);
1618 
1619 			*fhp = (caddr_t)fh3p;
1620 			CLNT_FREERES(cl, xdr_WNL_LOOKUP3res, (char *)res);
1621 		}
1622 	} else {
1623 		void *res;
1624 		struct rpc_err r_err;
1625 
1626 		if (vers == NFS_VERSION)
1627 			res = wnlproc_null_2(NULL, cl);
1628 		else if (vers == NFS_V3)
1629 			res = wnlproc3_null_3(NULL, cl);
1630 		else
1631 			res = wnlproc4_null_4(NULL, cl);
1632 
1633 		if (res == NULL) {
1634 			clnt_geterr(cl, &r_err);
1635 			if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
1636 				switch (r_err.re_status) {
1637 				case RPC_TLIERROR:
1638 				case RPC_CANTRECV:
1639 				case RPC_CANTSEND:
1640 					r_err.re_status = RPC_PROGVERSMISMATCH;
1641 				}
1642 			}
1643 			SET_ERR_RET(error, ERR_RPCERROR, r_err.re_status);
1644 			goto done;
1645 		}
1646 	}
1647 
1648 	/*
1649 	 * Make a copy of the netbuf to return
1650 	 */
1651 	nb = (struct netbuf *)malloc(sizeof (*nb));
1652 	if (nb == NULL) {
1653 		pr_err(gettext("no memory\n"));
1654 		goto done;
1655 	}
1656 	*nb = tbind->addr;
1657 	nb->buf = (char *)malloc(nb->maxlen);
1658 	if (nb->buf == NULL) {
1659 		pr_err(gettext("no memory\n"));
1660 		free(nb);
1661 		nb = NULL;
1662 		goto done;
1663 	}
1664 	(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
1665 
1666 done:
1667 	if (cl) {
1668 		if (ah != NULL) {
1669 			if (new_ah != NULL)
1670 				AUTH_DESTROY(ah);
1671 			AUTH_DESTROY(cl->cl_auth);
1672 			cl->cl_auth = NULL;
1673 		}
1674 		clnt_destroy(cl);
1675 		cl = NULL;
1676 	}
1677 	if (tbind) {
1678 		t_free((char *)tbind, T_BIND);
1679 		tbind = NULL;
1680 	}
1681 	if (fd >= 0)
1682 		(void) t_close(fd);
1683 	return (nb);
1684 }
1685 
1686 static int
1687 check_nconf(struct netconfig *nconf, int nthtry, int *valid_proto)
1688 {
1689 	int	try_test = 0;
1690 	int	valid_family;
1691 	char	*proto = NULL;
1692 
1693 
1694 	if (nthtry == FIRST_TRY) {
1695 		try_test = ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
1696 		    (nconf->nc_semantics == NC_TPI_COTS));
1697 		proto = NC_TCP;
1698 	} else if (nthtry == SECOND_TRY) {
1699 		try_test = (nconf->nc_semantics == NC_TPI_CLTS);
1700 		proto = NC_UDP;
1701 	}
1702 
1703 	if (proto &&
1704 	    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
1705 	    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
1706 	    (strcmp(nconf->nc_proto, proto) == 0))
1707 		*valid_proto = TRUE;
1708 	else
1709 		*valid_proto = FALSE;
1710 
1711 	return (try_test);
1712 }
1713 
1714 /*
1715  * Get a network address on "hostname" for program "prog"
1716  * with version "vers".  If the port number is specified (non zero)
1717  * then try for a TCP/UDP transport and set the port number of the
1718  * resulting IP address.
1719  *
1720  * If the address of a netconfig pointer was passed and
1721  * if it's not null, use it as the netconfig otherwise
1722  * assign the address of the netconfig that was used to
1723  * establish contact with the service.
1724  *
1725  * A similar routine is also defined in ../../autofs/autod_nfs.c.
1726  * This is a potential routine to move to ../lib for common usage.
1727  *
1728  * "error" refers to a more descriptive term when get_addr fails
1729  * and returns NULL: ERR_PROTO_NONE if no error introduced by
1730  * -o proto option, ERR_NETPATH if error found in NETPATH
1731  * environment variable, ERR_PROTO_INVALID if an unrecognized
1732  * protocol is specified by user, and ERR_PROTO_UNSUPP for a
1733  * recognized but invalid protocol (eg. ticlts, ticots, etc.).
1734  * "error" is ignored if get_addr returns non-NULL result.
1735  *
1736  */
1737 static struct netbuf *
1738 get_addr(char *hostname, ulong_t prog, ulong_t vers, struct netconfig **nconfp,
1739 	char *proto, ushort_t port, struct t_info *tinfo, caddr_t *fhp,
1740 	bool_t get_pubfh, char *fspath, err_ret_t *error)
1741 {
1742 	struct netbuf *nb = NULL;
1743 	struct netconfig *nconf = NULL;
1744 	NCONF_HANDLE *nc = NULL;
1745 	int nthtry = FIRST_TRY;
1746 	err_ret_t errsave_nohost, errsave_rpcerr;
1747 
1748 	SET_ERR_RET(&errsave_nohost, ERR_PROTO_NONE, 0);
1749 	SET_ERR_RET(&errsave_rpcerr, ERR_PROTO_NONE, 0);
1750 
1751 	SET_ERR_RET(error, ERR_PROTO_NONE, 0);
1752 
1753 	if (nconfp && *nconfp)
1754 		return (get_the_addr(hostname, prog, vers, *nconfp, port,
1755 		    tinfo, fhp, get_pubfh, fspath, error));
1756 	/*
1757 	 * No nconf passed in.
1758 	 *
1759 	 * Try to get a nconf from /etc/netconfig filtered by
1760 	 * the NETPATH environment variable.
1761 	 * First search for COTS, second for CLTS unless proto
1762 	 * is specified.  When we retry, we reset the
1763 	 * netconfig list so that we would search the whole list
1764 	 * all over again.
1765 	 */
1766 
1767 	if ((nc = setnetpath()) == NULL) {
1768 		/* should only return an error if problems with NETPATH */
1769 		/* In which case you are hosed */
1770 		SET_ERR_RET(error, ERR_NETPATH, 0);
1771 		goto done;
1772 	}
1773 
1774 	/*
1775 	 * If proto is specified, then only search for the match,
1776 	 * otherwise try COTS first, if failed, try CLTS.
1777 	 */
1778 	if (proto) {
1779 		/* no matching proto name */
1780 		SET_ERR_RET(error, ERR_PROTO_INVALID, 0);
1781 
1782 		while (nconf = getnetpath(nc)) {
1783 			if (strcmp(nconf->nc_netid, proto))
1784 				continue;
1785 
1786 			/* may be unsupported */
1787 			SET_ERR_RET(error, ERR_PROTO_UNSUPP, 0);
1788 
1789 			if ((port != 0) &&
1790 			    ((strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
1791 			    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
1792 			    (strcmp(nconf->nc_proto, NC_TCP) != 0 &&
1793 			    strcmp(nconf->nc_proto, NC_UDP) != 0))) {
1794 				continue;
1795 			} else {
1796 				nb = get_the_addr(hostname, prog,
1797 				    vers, nconf, port, tinfo,
1798 				    fhp, get_pubfh, fspath, error);
1799 
1800 				if (nb != NULL)
1801 					break;
1802 
1803 				/* nb is NULL - deal with errors */
1804 				if (error) {
1805 					if (error->error_type == ERR_NOHOST)
1806 						SET_ERR_RET(&errsave_nohost,
1807 						    error->error_type,
1808 						    error->error_value);
1809 					if (error->error_type == ERR_RPCERROR)
1810 						SET_ERR_RET(&errsave_rpcerr,
1811 						    error->error_type,
1812 						    error->error_value);
1813 				}
1814 				/*
1815 				 * continue with same protocol
1816 				 * selection
1817 				 */
1818 				continue;
1819 			}
1820 		} /* end of while */
1821 
1822 		if (nconf == NULL)
1823 			goto done;
1824 
1825 		if ((nb = get_the_addr(hostname, prog, vers, nconf, port,
1826 		    tinfo, fhp, get_pubfh, fspath, error)) == NULL)
1827 			goto done;
1828 	} else {
1829 retry:
1830 		SET_ERR_RET(error, ERR_NETPATH, 0);
1831 		while (nconf = getnetpath(nc)) {
1832 			SET_ERR_RET(error, ERR_PROTO_NONE, 0);
1833 
1834 			if (nconf->nc_flag & NC_VISIBLE) {
1835 				int	valid_proto;
1836 
1837 				if (check_nconf(nconf,
1838 				    nthtry, &valid_proto)) {
1839 					if (port == 0)
1840 						break;
1841 
1842 					if (valid_proto == TRUE)
1843 						break;
1844 				}
1845 			}
1846 		} /* while */
1847 		if (nconf == NULL) {
1848 			if (++nthtry <= MNT_PREF_LISTLEN) {
1849 				endnetpath(nc);
1850 				if ((nc = setnetpath()) == NULL)
1851 					goto done;
1852 				goto retry;
1853 			} else
1854 				goto done;
1855 		} else {
1856 			if ((nb = get_the_addr(hostname, prog, vers, nconf,
1857 			    port, tinfo, fhp, get_pubfh, fspath, error))
1858 			    == NULL) {
1859 				/* nb is NULL - deal with errors */
1860 				if (error) {
1861 					if (error->error_type == ERR_NOHOST)
1862 						SET_ERR_RET(&errsave_nohost,
1863 						    error->error_type,
1864 						    error->error_value);
1865 					if (error->error_type == ERR_RPCERROR)
1866 						SET_ERR_RET(&errsave_rpcerr,
1867 						    error->error_type,
1868 						    error->error_value);
1869 				}
1870 				/*
1871 				 * Continue the same search path in the
1872 				 * netconfig db until no more matched
1873 				 * nconf (nconf == NULL).
1874 				 */
1875 				goto retry;
1876 			}
1877 		}
1878 	}
1879 	SET_ERR_RET(error, ERR_PROTO_NONE, 0);
1880 
1881 	/*
1882 	 * Got nconf and nb.  Now dup the netconfig structure (nconf)
1883 	 * and return it thru nconfp.
1884 	 */
1885 	*nconfp = getnetconfigent(nconf->nc_netid);
1886 	if (*nconfp == NULL) {
1887 		syslog(LOG_ERR, "no memory\n");
1888 		free(nb);
1889 		nb = NULL;
1890 	}
1891 done:
1892 	if (nc)
1893 		endnetpath(nc);
1894 
1895 	if (nb == NULL) {
1896 		/*
1897 		 * Check the saved errors. The RPC error has *
1898 		 * precedence over the no host error.
1899 		 */
1900 		if (errsave_nohost.error_type != ERR_PROTO_NONE)
1901 			SET_ERR_RET(error, errsave_nohost.error_type,
1902 			    errsave_nohost.error_value);
1903 
1904 		if (errsave_rpcerr.error_type != ERR_PROTO_NONE)
1905 			SET_ERR_RET(error, errsave_rpcerr.error_type,
1906 			    errsave_rpcerr.error_value);
1907 	}
1908 
1909 	return (nb);
1910 }
1911 
1912 /*
1913  * Get a file handle usinging multi-component lookup with the public
1914  * file handle.
1915  */
1916 static int
1917 get_fh_via_pub(struct nfs_args *args, char *fshost, char *fspath, bool_t url,
1918 	bool_t loud, int *versp, struct netconfig **nconfp, ushort_t port)
1919 {
1920 	uint_t vers_min;
1921 	uint_t vers_max;
1922 	int r;
1923 	char *path;
1924 
1925 	if (nfsvers != 0) {
1926 		vers_max = vers_min = nfsvers;
1927 	} else {
1928 		vers_max = vers_max_default;
1929 		vers_min = vers_min_default;
1930 	}
1931 
1932 	if (url == FALSE) {
1933 		path = malloc(strlen(fspath) + 2);
1934 		if (path == NULL) {
1935 			if (loud == TRUE)
1936 				pr_err(gettext("no memory\n"));
1937 			return (RET_ERR);
1938 		}
1939 
1940 		path[0] = (char)WNL_NATIVEPATH;
1941 		(void) strcpy(&path[1], fspath);
1942 
1943 	} else  {
1944 		path = fspath;
1945 	}
1946 
1947 	for (nfsvers_to_use = vers_max; nfsvers_to_use >= vers_min;
1948 	    nfsvers_to_use--) {
1949 		/*
1950 		 * getaddr_nfs will also fill in the fh for us.
1951 		 */
1952 		r = getaddr_nfs(args, fshost, nconfp,
1953 		    TRUE, path, port, NULL, FALSE);
1954 
1955 		if (r == RET_OK) {
1956 			/*
1957 			 * Since we are using the public fh, and NLM is
1958 			 * not firewall friendly, use local locking.
1959 			 * Not the case for v4.
1960 			 */
1961 			*versp = nfsvers_to_use;
1962 			switch (nfsvers_to_use) {
1963 			case NFS_V4:
1964 				fstype = MNTTYPE_NFS4;
1965 				break;
1966 			case NFS_V3:
1967 				fstype = MNTTYPE_NFS3;
1968 				/* fall through to pick up llock option */
1969 			default:
1970 				args->flags |= NFSMNT_LLOCK;
1971 				break;
1972 			}
1973 			if (fspath != path)
1974 				free(path);
1975 
1976 			return (r);
1977 		}
1978 	}
1979 
1980 	if (fspath != path)
1981 		free(path);
1982 
1983 	if (loud == TRUE) {
1984 		pr_err(gettext("Could not use public filehandle in request to"
1985 		    " server %s\n"), fshost);
1986 	}
1987 
1988 	return (r);
1989 }
1990 
1991 /*
1992  * get fhandle of remote path from server's mountd
1993  */
1994 static int
1995 get_fh(struct nfs_args *args, char *fshost, char *fspath, int *versp,
1996 	bool_t loud_on_mnt_err, struct netconfig **nconfp, ushort_t port)
1997 {
1998 	static struct fhstatus fhs;
1999 	static struct mountres3 mountres3;
2000 	static struct pathcnf p;
2001 	nfs_fh3 *fh3p;
2002 	struct timeval timeout = { 25, 0};
2003 	CLIENT *cl;
2004 	enum clnt_stat rpc_stat;
2005 	rpcvers_t outvers = 0;
2006 	rpcvers_t vers_to_try;
2007 	rpcvers_t vers_min;
2008 	static int printed = 0;
2009 	int count, i, *auths;
2010 	char *msg;
2011 
2012 	switch (nfsvers) {
2013 	case 2: /* version 2 specified try that only */
2014 		vers_to_try = MOUNTVERS_POSIX;
2015 		vers_min = MOUNTVERS;
2016 		break;
2017 	case 3: /* version 3 specified try that only */
2018 		vers_to_try = MOUNTVERS3;
2019 		vers_min = MOUNTVERS3;
2020 		break;
2021 	case 4: /* version 4 specified try that only */
2022 		/*
2023 		 * This assignment is in the wrong version sequence.
2024 		 * The above are MOUNT program and this is NFS
2025 		 * program.  However, it happens to work out since the
2026 		 * two don't collide for NFSv4.
2027 		 */
2028 		vers_to_try = NFS_V4;
2029 		vers_min = NFS_V4;
2030 		break;
2031 	default: /* no version specified, start with default */
2032 		/*
2033 		 * If the retry version is set, use that. This will
2034 		 * be set if the last mount attempt returned any other
2035 		 * besides an RPC error.
2036 		 */
2037 		if (nfsretry_vers)
2038 			vers_to_try = nfsretry_vers;
2039 		else {
2040 			vers_to_try = vers_max_default;
2041 			vers_min = vers_min_default;
2042 		}
2043 
2044 		break;
2045 	}
2046 
2047 	/*
2048 	 * In the case of version 4, just NULL proc the server since
2049 	 * there is no MOUNT program.  If this fails, then decrease
2050 	 * vers_to_try and continue on with regular MOUNT program
2051 	 * processing.
2052 	 */
2053 	if (vers_to_try == NFS_V4) {
2054 		int savevers = nfsvers_to_use;
2055 		err_ret_t error;
2056 		int retval;
2057 		SET_ERR_RET(&error, ERR_PROTO_NONE, 0);
2058 
2059 		/* Let's hope for the best */
2060 		nfsvers_to_use = NFS_V4;
2061 		retval = getaddr_nfs(args, fshost, nconfp, FALSE,
2062 		    fspath, port, &error, vers_min == NFS_V4);
2063 
2064 		if (retval == RET_OK) {
2065 			*versp = nfsvers_to_use = NFS_V4;
2066 			fstype = MNTTYPE_NFS4;
2067 			args->fh = strdup(fspath);
2068 			if (args->fh == NULL) {
2069 				pr_err(gettext("no memory\n"));
2070 				*versp = nfsvers_to_use = savevers;
2071 				return (RET_ERR);
2072 			}
2073 			return (RET_OK);
2074 		}
2075 		nfsvers_to_use = savevers;
2076 
2077 		vers_to_try--;
2078 		/* If no more versions to try, let the user know. */
2079 		if (vers_to_try < vers_min)
2080 			return (retval);
2081 
2082 		/*
2083 		 * If we are here, there are more versions to try but
2084 		 * there has been an error of some sort.  If it is not
2085 		 * an RPC error (e.g. host unknown), we just stop and
2086 		 * return the error since the other versions would see
2087 		 * the same error as well.
2088 		 */
2089 		if (retval == RET_ERR && error.error_type != ERR_RPCERROR)
2090 			return (retval);
2091 	}
2092 
2093 	while ((cl = clnt_create_vers(fshost, MOUNTPROG, &outvers,
2094 	    vers_min, vers_to_try, "datagram_v")) == NULL) {
2095 		if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST) {
2096 			pr_err(gettext("%s: %s\n"), fshost,
2097 			    clnt_spcreateerror(""));
2098 			return (RET_ERR);
2099 		}
2100 
2101 		/*
2102 		 * We don't want to downgrade version on lost packets
2103 		 */
2104 		if ((rpc_createerr.cf_stat == RPC_TIMEDOUT) ||
2105 		    (rpc_createerr.cf_stat == RPC_PMAPFAILURE)) {
2106 			pr_err(gettext("%s: %s\n"), fshost,
2107 			    clnt_spcreateerror(""));
2108 			return (RET_RETRY);
2109 		}
2110 
2111 		/*
2112 		 * back off and try the previous version - patch to the
2113 		 * problem of version numbers not being contigous and
2114 		 * clnt_create_vers failing (SunOS4.1 clients & SGI servers)
2115 		 * The problem happens with most non-Sun servers who
2116 		 * don't support mountd protocol #2. So, in case the
2117 		 * call fails, we re-try the call anyway.
2118 		 */
2119 		vers_to_try--;
2120 		if (vers_to_try < vers_min) {
2121 			if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH) {
2122 				if (nfsvers == 0) {
2123 					pr_err(gettext(
2124 			"%s:%s: no applicable versions of NFS supported\n"),
2125 					    fshost, fspath);
2126 				} else {
2127 					pr_err(gettext(
2128 			"%s:%s: NFS Version %d not supported\n"),
2129 					    fshost, fspath, nfsvers);
2130 				}
2131 				return (RET_ERR);
2132 			}
2133 			if (!printed) {
2134 				pr_err(gettext("%s: %s\n"), fshost,
2135 				    clnt_spcreateerror(""));
2136 				printed = 1;
2137 			}
2138 			return (RET_RETRY);
2139 		}
2140 	}
2141 	if (posix && outvers < MOUNTVERS_POSIX) {
2142 		pr_err(gettext("%s: %s: no pathconf info\n"),
2143 		    fshost, clnt_sperror(cl, ""));
2144 		clnt_destroy(cl);
2145 		return (RET_ERR);
2146 	}
2147 
2148 	if (__clnt_bindresvport(cl) < 0) {
2149 		pr_err(gettext("Couldn't bind to reserved port\n"));
2150 		clnt_destroy(cl);
2151 		return (RET_RETRY);
2152 	}
2153 
2154 	if ((cl->cl_auth = authsys_create_default()) == NULL) {
2155 		pr_err(
2156 		    gettext("Couldn't create default authentication handle\n"));
2157 		clnt_destroy(cl);
2158 		return (RET_RETRY);
2159 	}
2160 
2161 	switch (outvers) {
2162 	case MOUNTVERS:
2163 	case MOUNTVERS_POSIX:
2164 		*versp = nfsvers_to_use = NFS_VERSION;
2165 		rpc_stat = clnt_call(cl, MOUNTPROC_MNT, xdr_dirpath,
2166 		    (caddr_t)&fspath, xdr_fhstatus, (caddr_t)&fhs, timeout);
2167 		if (rpc_stat != RPC_SUCCESS) {
2168 			pr_err(gettext("%s:%s: server not responding %s\n"),
2169 			    fshost, fspath, clnt_sperror(cl, ""));
2170 			clnt_destroy(cl);
2171 			return (RET_RETRY);
2172 		}
2173 
2174 		if ((errno = fhs.fhs_status) != MNT_OK) {
2175 			if (loud_on_mnt_err) {
2176 				if (errno == EACCES) {
2177 					pr_err(gettext(
2178 					    "%s:%s: access denied\n"),
2179 					    fshost, fspath);
2180 				} else {
2181 					pr_err(gettext("%s:%s: %s\n"), fshost,
2182 					    fspath, errno >= 0 ?
2183 					    strerror(errno) : "invalid error "
2184 					    "returned by server");
2185 				}
2186 			}
2187 			clnt_destroy(cl);
2188 			return (RET_MNTERR);
2189 		}
2190 		args->fh = malloc(sizeof (fhs.fhstatus_u.fhs_fhandle));
2191 		if (args->fh == NULL) {
2192 			pr_err(gettext("no memory\n"));
2193 			return (RET_ERR);
2194 		}
2195 		memcpy((caddr_t)args->fh, (caddr_t)&fhs.fhstatus_u.fhs_fhandle,
2196 		    sizeof (fhs.fhstatus_u.fhs_fhandle));
2197 		if (!errno && posix) {
2198 			rpc_stat = clnt_call(cl, MOUNTPROC_PATHCONF,
2199 			    xdr_dirpath, (caddr_t)&fspath, xdr_ppathcnf,
2200 			    (caddr_t)&p, timeout);
2201 			if (rpc_stat != RPC_SUCCESS) {
2202 				pr_err(gettext(
2203 				    "%s:%s: server not responding %s\n"),
2204 				    fshost, fspath, clnt_sperror(cl, ""));
2205 				free(args->fh);
2206 				clnt_destroy(cl);
2207 				return (RET_RETRY);
2208 			}
2209 			if (_PC_ISSET(_PC_ERROR, p.pc_mask)) {
2210 				pr_err(gettext(
2211 				    "%s:%s: no pathconf info\n"),
2212 				    fshost, fspath);
2213 				free(args->fh);
2214 				clnt_destroy(cl);
2215 				return (RET_ERR);
2216 			}
2217 			args->flags |= NFSMNT_POSIX;
2218 			args->pathconf = malloc(sizeof (p));
2219 			if (args->pathconf == NULL) {
2220 				pr_err(gettext("no memory\n"));
2221 				free(args->fh);
2222 				clnt_destroy(cl);
2223 				return (RET_ERR);
2224 			}
2225 			memcpy((caddr_t)args->pathconf, (caddr_t)&p,
2226 			    sizeof (p));
2227 		}
2228 		break;
2229 
2230 	case MOUNTVERS3:
2231 		*versp = nfsvers_to_use = NFS_V3;
2232 		rpc_stat = clnt_call(cl, MOUNTPROC_MNT, xdr_dirpath,
2233 		    (caddr_t)&fspath, xdr_mountres3, (caddr_t)&mountres3,
2234 		    timeout);
2235 		if (rpc_stat != RPC_SUCCESS) {
2236 			pr_err(gettext("%s:%s: server not responding %s\n"),
2237 			    fshost, fspath, clnt_sperror(cl, ""));
2238 			clnt_destroy(cl);
2239 			return (RET_RETRY);
2240 		}
2241 
2242 		/*
2243 		 * Assume here that most of the MNT3ERR_*
2244 		 * codes map into E* errors.
2245 		 */
2246 		if ((errno = mountres3.fhs_status) != MNT_OK) {
2247 			if (loud_on_mnt_err) {
2248 				switch (errno) {
2249 				case MNT3ERR_NAMETOOLONG:
2250 					msg = "path name is too long";
2251 					break;
2252 				case MNT3ERR_NOTSUPP:
2253 					msg = "operation not supported";
2254 					break;
2255 				case MNT3ERR_SERVERFAULT:
2256 					msg = "server fault";
2257 					break;
2258 				default:
2259 					if (errno >= 0)
2260 						msg = strerror(errno);
2261 					else
2262 						msg = "invalid error returned "
2263 						    "by server";
2264 					break;
2265 				}
2266 				pr_err(gettext("%s:%s: %s\n"), fshost,
2267 				    fspath, msg);
2268 			}
2269 			clnt_destroy(cl);
2270 			return (RET_MNTERR);
2271 		}
2272 
2273 		fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
2274 		if (fh3p == NULL) {
2275 			pr_err(gettext("no memory\n"));
2276 			return (RET_ERR);
2277 		}
2278 		fh3p->fh3_length =
2279 		    mountres3.mountres3_u.mountinfo.fhandle.fhandle3_len;
2280 		(void) memcpy(fh3p->fh3_u.data,
2281 		    mountres3.mountres3_u.mountinfo.fhandle.fhandle3_val,
2282 		    fh3p->fh3_length);
2283 		args->fh = (caddr_t)fh3p;
2284 		fstype = MNTTYPE_NFS3;
2285 
2286 		/*
2287 		 * Check the security flavor to be used.
2288 		 *
2289 		 * If "secure" or "sec=flavor" is a mount
2290 		 * option, check if the server supports the "flavor".
2291 		 * If the server does not support the flavor, return
2292 		 * error.
2293 		 *
2294 		 * If no mount option is given then use the first supported
2295 		 * security flavor (by the client) in the auth list returned
2296 		 * from the server.
2297 		 *
2298 		 */
2299 		auths =
2300 		    mountres3.mountres3_u.mountinfo.auth_flavors
2301 		    .auth_flavors_val;
2302 		count =
2303 		    mountres3.mountres3_u.mountinfo.auth_flavors
2304 		    .auth_flavors_len;
2305 
2306 		if (sec_opt) {
2307 			for (i = 0; i < count; i++) {
2308 				if (auths[i] == nfs_sec.sc_nfsnum)
2309 					break;
2310 			}
2311 			if (i >= count)
2312 				goto autherr;
2313 		} else {
2314 			if (count < 0)
2315 				break;
2316 
2317 			for (i = 0; i < count; i++) {
2318 				if (!nfs_getseconfig_bynumber(auths[i],
2319 				    &nfs_sec)) {
2320 					sec_opt++;
2321 					break;
2322 				}
2323 			}
2324 
2325 			if (i >= count)
2326 				goto autherr;
2327 		}
2328 		break;
2329 	default:
2330 		pr_err(gettext("%s:%s: Unknown MOUNT version %d\n"),
2331 		    fshost, fspath, outvers);
2332 		clnt_destroy(cl);
2333 		return (RET_ERR);
2334 	}
2335 
2336 	clnt_destroy(cl);
2337 	return (RET_OK);
2338 
2339 autherr:
2340 	pr_err(gettext(
2341 	    "security mode does not match the server exporting %s:%s\n"),
2342 	    fshost, fspath);
2343 	clnt_destroy(cl);
2344 	return (RET_ERR);
2345 }
2346 
2347 /*
2348  * Fill in the address for the server's NFS service and
2349  * fill in a knetconfig structure for the transport that
2350  * the service is available on.
2351  */
2352 static int
2353 getaddr_nfs(struct nfs_args *args, char *fshost, struct netconfig **nconfp,
2354 	    bool_t get_pubfh, char *fspath, ushort_t port, err_ret_t *error,
2355 	    bool_t print_rpcerror)
2356 {
2357 	struct stat sb;
2358 	struct netconfig *nconf;
2359 	struct knetconfig *knconfp;
2360 	static int printed = 0;
2361 	struct t_info tinfo;
2362 	err_ret_t addr_error;
2363 
2364 	SET_ERR_RET(error, ERR_PROTO_NONE, 0);
2365 	SET_ERR_RET(&addr_error, ERR_PROTO_NONE, 0);
2366 
2367 	if (nfs_proto) {
2368 		/*
2369 		 * If a proto is specified and its rdma try this. The kernel
2370 		 * will later do the reachablity test and fail form there
2371 		 * if rdma transport is not available to kernel rpc
2372 		 */
2373 		if (strcmp(nfs_proto, "rdma") == 0) {
2374 			args->addr = get_addr(fshost, NFS_PROGRAM,
2375 			    nfsvers_to_use, nconfp, NULL, port, &tinfo,
2376 			    &args->fh, get_pubfh, fspath, &addr_error);
2377 
2378 			args->flags |= NFSMNT_DORDMA;
2379 		} else {
2380 			args->addr = get_addr(fshost, NFS_PROGRAM,
2381 			    nfsvers_to_use, nconfp, nfs_proto, port, &tinfo,
2382 			    &args->fh, get_pubfh, fspath, &addr_error);
2383 		}
2384 	} else {
2385 		args->addr = get_addr(fshost, NFS_PROGRAM, nfsvers_to_use,
2386 		    nconfp, nfs_proto, port, &tinfo, &args->fh, get_pubfh,
2387 		    fspath, &addr_error);
2388 		/*
2389 		 * If no proto is specified set this flag.
2390 		 * Kernel mount code will try to use RDMA if its on the
2391 		 * system, otherwise it will keep on using the protocol
2392 		 * selected here, through the above get_addr call.
2393 		 */
2394 		if (nfs_proto == NULL)
2395 			args->flags |= NFSMNT_TRYRDMA;
2396 	}
2397 
2398 	if (args->addr == NULL) {
2399 		/*
2400 		 * We could have failed because the server had no public
2401 		 * file handle support. So don't print a message and don't
2402 		 * retry.
2403 		 */
2404 		if (get_pubfh == TRUE)
2405 			return (RET_ERR);
2406 
2407 		if (!printed) {
2408 			switch (addr_error.error_type) {
2409 			case 0:
2410 				printed = 1;
2411 				break;
2412 			case ERR_RPCERROR:
2413 				if (!print_rpcerror)
2414 					/* no error print at this time */
2415 					break;
2416 				pr_err(gettext("%s NFS service not"
2417 				    " available %s\n"), fshost,
2418 				    clnt_sperrno(addr_error.error_value));
2419 				printed = 1;
2420 				break;
2421 			case ERR_NETPATH:
2422 				pr_err(gettext("%s: Error in NETPATH.\n"),
2423 				    fshost);
2424 				printed = 1;
2425 				break;
2426 			case ERR_PROTO_INVALID:
2427 				pr_err(gettext("%s: NFS service does not"
2428 				    " recognize protocol: %s.\n"), fshost,
2429 				    nfs_proto);
2430 				printed = 1;
2431 				break;
2432 			case ERR_PROTO_UNSUPP:
2433 				if (nfsvers || nfsvers_to_use == NFS_VERSMIN) {
2434 					/*
2435 					 * Don't set "printed" here. Since we
2436 					 * have to keep checking here till we
2437 					 * exhaust transport errors on all vers.
2438 					 *
2439 					 * Print this message if:
2440 					 * 1. After we have tried all versions
2441 					 *    of NFS and none support the asked
2442 					 *    transport.
2443 					 *
2444 					 * 2. If a version is specified and it
2445 					 *    does'nt support the asked
2446 					 *    transport.
2447 					 *
2448 					 * Otherwise we decrement the version
2449 					 * and retry below.
2450 					 */
2451 					pr_err(gettext("%s: NFS service does"
2452 					    " not support protocol: %s.\n"),
2453 					    fshost, nfs_proto);
2454 				}
2455 				break;
2456 			case ERR_NOHOST:
2457 				pr_err("%s: %s\n", fshost, "Unknown host");
2458 				printed = 1;
2459 				break;
2460 			default:
2461 				/* case ERR_PROTO_NONE falls through */
2462 				pr_err(gettext("%s: NFS service not responding"
2463 				    "\n"), fshost);
2464 				printed = 1;
2465 				break;
2466 			}
2467 		}
2468 		SET_ERR_RET(error,
2469 		    addr_error.error_type, addr_error.error_value);
2470 		if (addr_error.error_type == ERR_PROTO_NONE)
2471 			return (RET_RETRY);
2472 		else if (addr_error.error_type == ERR_RPCERROR &&
2473 		    !IS_UNRECOVERABLE_RPC(addr_error.error_value)) {
2474 			return (RET_RETRY);
2475 		} else if (nfsvers == 0 && addr_error.error_type ==
2476 		    ERR_PROTO_UNSUPP && nfsvers_to_use != NFS_VERSMIN) {
2477 			/*
2478 			 * If no version is specified, and the error is due
2479 			 * to an unsupported transport, then decrement the
2480 			 * version and retry.
2481 			 */
2482 			return (RET_RETRY);
2483 		} else
2484 			return (RET_ERR);
2485 	}
2486 	nconf = *nconfp;
2487 
2488 	if (stat(nconf->nc_device, &sb) < 0) {
2489 		pr_err(gettext("getaddr_nfs: couldn't stat: %s: %s\n"),
2490 		    nconf->nc_device, strerror(errno));
2491 		return (RET_ERR);
2492 	}
2493 
2494 	knconfp = (struct knetconfig *)malloc(sizeof (*knconfp));
2495 	if (!knconfp) {
2496 		pr_err(gettext("no memory\n"));
2497 		return (RET_ERR);
2498 	}
2499 	knconfp->knc_semantics = nconf->nc_semantics;
2500 	knconfp->knc_protofmly = nconf->nc_protofmly;
2501 	knconfp->knc_proto = nconf->nc_proto;
2502 	knconfp->knc_rdev = sb.st_rdev;
2503 
2504 	/* make sure we don't overload the transport */
2505 	if (tinfo.tsdu > 0 && tinfo.tsdu < NFS_MAXDATA + NFS_RPC_HDR) {
2506 		args->flags |= (NFSMNT_RSIZE | NFSMNT_WSIZE);
2507 		if (args->rsize == 0 || args->rsize > tinfo.tsdu - NFS_RPC_HDR)
2508 			args->rsize = tinfo.tsdu - NFS_RPC_HDR;
2509 		if (args->wsize == 0 || args->wsize > tinfo.tsdu - NFS_RPC_HDR)
2510 			args->wsize = tinfo.tsdu - NFS_RPC_HDR;
2511 	}
2512 
2513 	args->flags |= NFSMNT_KNCONF;
2514 	args->knconf = knconfp;
2515 	return (RET_OK);
2516 }
2517 
2518 static int
2519 retry(struct mnttab *mntp, int ro)
2520 {
2521 	int delay = 5;
2522 	int count = retries;
2523 	int r;
2524 
2525 	/*
2526 	 * Please see comments on nfsretry_vers in the beginning of this file
2527 	 * and in main() routine.
2528 	 */
2529 
2530 	if (bg) {
2531 		if (fork() > 0)
2532 			return (RET_OK);
2533 		backgrounded = 1;
2534 		pr_err(gettext("backgrounding: %s\n"), mntp->mnt_mountp);
2535 	} else {
2536 		if (!nfsretry_vers)
2537 			pr_err(gettext("retrying: %s\n"), mntp->mnt_mountp);
2538 	}
2539 
2540 	while (count--) {
2541 		if ((r = mount_nfs(mntp, ro, NULL)) == RET_OK) {
2542 			pr_err(gettext("%s: mounted OK\n"), mntp->mnt_mountp);
2543 			return (RET_OK);
2544 		}
2545 		if (r != RET_RETRY)
2546 			break;
2547 
2548 		if (count > 0) {
2549 			(void) sleep(delay);
2550 			delay *= 2;
2551 			if (delay > 120)
2552 				delay = 120;
2553 		}
2554 	}
2555 
2556 	if (!nfsretry_vers)
2557 		pr_err(gettext("giving up on: %s\n"), mntp->mnt_mountp);
2558 
2559 	return (RET_ERR);
2560 }
2561 
2562 /*
2563  * Read the /etc/default/nfs configuration file to determine if the
2564  * client has been configured for a new min/max for the NFS version to
2565  * use.
2566  */
2567 static void
2568 read_default(void)
2569 {
2570 	char *defval;
2571 	int errno;
2572 	int tmp;
2573 
2574 	/* Fail silently if error in opening the default nfs config file */
2575 	if ((defopen(NFSADMIN)) == 0) {
2576 		if ((defval = defread("NFS_CLIENT_VERSMIN=")) != NULL) {
2577 			errno = 0;
2578 			tmp = strtol(defval, (char **)NULL, 10);
2579 			if (errno == 0) {
2580 				vers_min_default = tmp;
2581 			}
2582 		}
2583 		if ((defval = defread("NFS_CLIENT_VERSMAX=")) != NULL) {
2584 			errno = 0;
2585 			tmp = strtol(defval, (char **)NULL, 10);
2586 			if (errno == 0) {
2587 				vers_max_default = tmp;
2588 			}
2589 		}
2590 		/* close defaults file */
2591 		defopen(NULL);
2592 	}
2593 }
2594 
2595 static void
2596 sigusr1(int s)
2597 {
2598 }
2599