xref: /titanic_44/usr/src/cmd/fs.d/nfs/mount/mount.c (revision a4aeef46cda1835da2b19f8f62b4526de6521e6c)
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 2009 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 /*
41  * nfs mount
42  */
43 
44 #define	NFSCLIENT
45 #include <locale.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <memory.h>
49 #include <stdarg.h>
50 #include <unistd.h>
51 #include <ctype.h>
52 #include <stdlib.h>
53 #include <signal.h>
54 #include <sys/param.h>
55 #include <rpc/rpc.h>
56 #include <errno.h>
57 #include <sys/stat.h>
58 #include <netdb.h>
59 #include <sys/mount.h>
60 #include <sys/mntent.h>
61 #include <sys/mnttab.h>
62 #include <nfs/nfs.h>
63 #include <nfs/mount.h>
64 #include <rpcsvc/mount.h>
65 #include <sys/pathconf.h>
66 #include <netdir.h>
67 #include <netconfig.h>
68 #include <sys/sockio.h>
69 #include <net/if.h>
70 #include <syslog.h>
71 #include <fslib.h>
72 #include <deflt.h>
73 #include <sys/wait.h>
74 #include "replica.h"
75 #include <netinet/in.h>
76 #include <nfs/nfs_sec.h>
77 #include <rpcsvc/daemon_utils.h>
78 #include <priv.h>
79 #include <tsol/label.h>
80 #include "nfs_subr.h"
81 #include "webnfs.h"
82 #include <rpcsvc/nfs4_prot.h>
83 
84 #include <nfs/nfssys.h>
85 extern int _nfssys(enum nfssys_op, void *);
86 
87 #ifndef	NFS_VERSMAX
88 #define	NFS_VERSMAX	4
89 #endif
90 #ifndef	NFS_VERSMIN
91 #define	NFS_VERSMIN	2
92 #endif
93 
94 #define	RET_OK		0
95 #define	RET_RETRY	32
96 #define	RET_ERR		33
97 #define	RET_MNTERR	1000
98 #define	ERR_PROTO_NONE		0
99 #define	ERR_PROTO_INVALID	901
100 #define	ERR_PROTO_UNSUPP	902
101 #define	ERR_NETPATH		903
102 #define	ERR_NOHOST		904
103 #define	ERR_RPCERROR		905
104 
105 typedef struct err_ret {
106 	int error_type;
107 	int error_value;
108 } err_ret_t;
109 
110 #define	SET_ERR_RET(errst, etype, eval) \
111 	if (errst) { \
112 		(errst)->error_type = etype; \
113 		(errst)->error_value = eval; \
114 	}
115 
116 /* number of transports to try */
117 #define	MNT_PREF_LISTLEN	2
118 #define	FIRST_TRY		1
119 #define	SECOND_TRY		2
120 
121 #define	BIGRETRY	10000
122 
123 /* maximum length of RPC header for NFS messages */
124 #define	NFS_RPC_HDR	432
125 
126 #define	NFS_ARGS_EXTB_secdata(args, secdata) \
127 	{ (args)->nfs_args_ext = NFS_ARGS_EXTB, \
128 	(args)->nfs_ext_u.nfs_extB.secdata = secdata; }
129 
130 extern int __clnt_bindresvport(CLIENT *);
131 extern char *nfs_get_qop_name();
132 extern AUTH * nfs_create_ah();
133 extern enum snego_stat nfs_sec_nego();
134 
135 static void usage(void);
136 static int retry(struct mnttab *, int);
137 static int set_args(int *, struct nfs_args *, char *, struct mnttab *);
138 static int get_fh_via_pub(struct nfs_args *, char *, char *, bool_t, bool_t,
139 	int *, struct netconfig **, ushort_t);
140 static int get_fh(struct nfs_args *, char *, char *, int *, bool_t,
141 	struct netconfig **, ushort_t);
142 static int make_secure(struct nfs_args *, char *, struct netconfig *,
143 	bool_t, rpcvers_t);
144 static int mount_nfs(struct mnttab *, int, err_ret_t *);
145 static int getaddr_nfs(struct nfs_args *, char *, struct netconfig **,
146 		    bool_t, char *, ushort_t, err_ret_t *, bool_t);
147 static void pr_err(const char *fmt, ...);
148 static void usage(void);
149 static struct netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
150 	struct netconfig **, char *, ushort_t, struct t_info *,
151 	caddr_t *, bool_t, char *, err_ret_t *);
152 
153 static struct netbuf *get_the_addr(char *, rpcprog_t, rpcvers_t,
154 	struct netconfig *, ushort_t, struct t_info *, caddr_t *,
155 	bool_t, char *, err_ret_t *);
156 
157 extern int self_check(char *);
158 
159 static void read_default(void);
160 
161 static char typename[64];
162 
163 static int bg = 0;
164 static int backgrounded = 0;
165 static int posix = 0;
166 static int retries = BIGRETRY;
167 static ushort_t nfs_port = 0;
168 static char *nfs_proto = NULL;
169 
170 static int mflg = 0;
171 static int Oflg = 0;	/* Overlay mounts */
172 static int qflg = 0;	/* quiet - don't print warnings on bad options */
173 
174 static char *fstype = MNTTYPE_NFS;
175 
176 static seconfig_t nfs_sec;
177 static int sec_opt = 0;	/* any security option ? */
178 static bool_t snego_done;
179 static void sigusr1(int);
180 
181 extern void set_nfsv4_ephemeral_mount_to(void);
182 
183 /*
184  * list of support services needed
185  */
186 static char	*service_list[] = { STATD, LOCKD, NULL };
187 static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };
188 
189 /*
190  * These two variables control the NFS version number to be used.
191  *
192  * nfsvers defaults to 0 which means to use the highest number that
193  * both the client and the server support.  It can also be set to
194  * a particular value, either 2, 3, or 4 to indicate the version
195  * number of choice.  If the server (or the client) do not support
196  * the version indicated, then the mount attempt will be failed.
197  *
198  * nfsvers_to_use is the actual version number found to use.  It
199  * is determined in get_fh by pinging the various versions of the
200  * NFS service on the server to see which responds positively.
201  *
202  * nfsretry_vers is the version number set when we retry the mount
203  * command with the version decremented from nfsvers_to_use.
204  * nfsretry_vers is set from nfsvers_to_use when we retry the mount
205  * for errors other than RPC errors; it helps un know why we are
206  * retrying. It is an indication that the retry is due to
207  * non-RPC errors.
208  */
209 static rpcvers_t nfsvers = 0;
210 static rpcvers_t nfsvers_to_use = 0;
211 static rpcvers_t nfsretry_vers = 0;
212 
213 /*
214  * There are the defaults (range) for the client when determining
215  * which NFS version to use when probing the server (see above).
216  * These will only be used when the vers mount option is not used and
217  * these may be reset if /etc/default/nfs is configured to do so.
218  */
219 static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
220 static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;
221 
222 /*
223  * This variable controls whether to try the public file handle.
224  */
225 static bool_t public_opt;
226 
227 int
228 main(int argc, char *argv[])
229 {
230 	struct mnttab mnt;
231 	extern char *optarg;
232 	extern int optind;
233 	char optbuf[MAX_MNTOPT_STR];
234 	int ro = 0;
235 	int r;
236 	int c;
237 	char *myname;
238 	err_ret_t retry_error;
239 
240 	(void) setlocale(LC_ALL, "");
241 #if !defined(TEXT_DOMAIN)
242 #define	TEXT_DOMAIN	"SYS_TEST"
243 #endif
244 	(void) textdomain(TEXT_DOMAIN);
245 
246 	myname = strrchr(argv[0], '/');
247 	myname = myname ? myname + 1 : argv[0];
248 	(void) snprintf(typename, sizeof (typename), "%s %s",
249 	    MNTTYPE_NFS, myname);
250 	argv[0] = typename;
251 
252 	mnt.mnt_mntopts = optbuf;
253 	(void) strcpy(optbuf, "rw");
254 
255 	/*
256 	 * Set options
257 	 */
258 	while ((c = getopt(argc, argv, "ro:mOq")) != EOF) {
259 		switch (c) {
260 		case 'r':
261 			ro++;
262 			break;
263 		case 'o':
264 			if (strlen(optarg) >= MAX_MNTOPT_STR) {
265 				pr_err(gettext("option string too long"));
266 				return (RET_ERR);
267 			}
268 			(void) strcpy(mnt.mnt_mntopts, optarg);
269 #ifdef LATER					/* XXX */
270 			if (strstr(optarg, MNTOPT_REMOUNT)) {
271 				/*
272 				 * If remount is specified, only rw is allowed.
273 				 */
274 				if ((strcmp(optarg, MNTOPT_REMOUNT) != 0) &&
275 				    (strcmp(optarg, "remount,rw") != 0) &&
276 				    (strcmp(optarg, "rw,remount") != 0)) {
277 					pr_err(gettext("Invalid options\n"));
278 					exit(RET_ERR);
279 				}
280 			}
281 #endif /* LATER */				/* XXX */
282 			break;
283 		case 'm':
284 			mflg++;
285 			break;
286 		case 'O':
287 			Oflg++;
288 			break;
289 		case 'q':
290 			qflg++;
291 			break;
292 		default:
293 			usage();
294 			exit(RET_ERR);
295 		}
296 	}
297 	if (argc - optind != 2) {
298 		usage();
299 		exit(RET_ERR);
300 	}
301 
302 	mnt.mnt_special = argv[optind];
303 	mnt.mnt_mountp = argv[optind+1];
304 
305 	if (!priv_ineffect(PRIV_SYS_MOUNT) ||
306 	    !priv_ineffect(PRIV_NET_PRIVADDR)) {
307 		pr_err(gettext("insufficient privileges\n"));
308 		exit(RET_ERR);
309 	}
310 
311 	/*
312 	 * On a labeled system, allow read-down nfs mounts if privileged
313 	 * (PRIV_NET_MAC_AWARE) to do so.  Otherwise, ignore the error
314 	 * and "mount equal label only" behavior will result.
315 	 */
316 	if (is_system_labeled())
317 		(void) setpflags(NET_MAC_AWARE, 1);
318 
319 	/*
320 	 * Read the defaults file to see if the min/max versions have
321 	 * been set and therefore would override the encoded defaults.
322 	 * Then check to make sure that if they were set that the
323 	 * values are reasonable.
324 	 */
325 	read_default();
326 	if (vers_min_default > vers_max_default ||
327 	    vers_min_default < NFS_VERSMIN ||
328 	    vers_max_default > NFS_VERSMAX) {
329 		pr_err("%s %s\n%s %s\n",
330 		    gettext("Incorrect configuration of client\'s"),
331 		    NFSADMIN,
332 		    gettext("NFS_CLIENT_VERSMIN or NFS_CLIENT_VERSMAX"),
333 		    gettext("is either out of range or overlaps."));
334 	}
335 
336 	SET_ERR_RET(&retry_error, ERR_PROTO_NONE, 0);
337 	r = mount_nfs(&mnt, ro, &retry_error);
338 	if (r == RET_RETRY && retries) {
339 		/*
340 		 * Check the error code from the last mount attempt if it was
341 		 * an RPC error, then retry as is. Otherwise we retry with the
342 		 * nfsretry_vers set. It is set by decrementing nfsvers_to_use.
343 		 * If we are retrying with nfsretry_vers then we don't print any
344 		 * retry messages, since we are not retrying due to an RPC
345 		 * error.
346 		 */
347 		if (retry_error.error_type) {
348 			if (retry_error.error_type != ERR_RPCERROR) {
349 				nfsretry_vers = nfsvers_to_use =
350 				    nfsvers_to_use - 1;
351 				if (nfsretry_vers < NFS_VERSMIN)
352 					return (r);
353 			}
354 		}
355 
356 		r = retry(&mnt, ro);
357 	}
358 	/*
359 	 * exit(r);
360 	 */
361 	return (r);
362 }
363 
364 static void
365 pr_err(const char *fmt, ...)
366 {
367 	va_list ap;
368 
369 	va_start(ap, fmt);
370 	if (backgrounded != 0) {
371 		(void) vsyslog(LOG_ERR, fmt, ap);
372 	} else {
373 		(void) fprintf(stderr, "%s: ", typename);
374 		(void) vfprintf(stderr, fmt, ap);
375 		(void) fflush(stderr);
376 	}
377 	va_end(ap);
378 }
379 
380 static void
381 usage()
382 {
383 	(void) fprintf(stderr,
384 	    gettext("Usage: nfs mount [-r] [-o opts] [server:]path dir\n"));
385 	exit(RET_ERR);
386 }
387 
388 static int
389 mount_nfs(struct mnttab *mntp, int ro, err_ret_t *retry_error)
390 {
391 	struct nfs_args *args = NULL, *argp = NULL, *prev_argp = NULL;
392 	struct netconfig *nconf = NULL;
393 	struct replica *list = NULL;
394 	int mntflags = 0;
395 	int i, r, n;
396 	int oldvers = 0, vers = 0;
397 	int last_error = RET_OK;
398 	int replicated = 0;
399 	char *p;
400 	bool_t url;
401 	bool_t use_pubfh;
402 	char *special = NULL;
403 	char *oldpath = NULL;
404 	char *newpath = NULL;
405 	char *service;
406 	pid_t pi;
407 	struct flock f;
408 	char *saveopts = NULL;
409 	char **sl = NULL;
410 
411 	mntp->mnt_fstype = MNTTYPE_NFS;
412 
413 	if (ro) {
414 		mntflags |= MS_RDONLY;
415 		/* convert "rw"->"ro" */
416 		if (p = strstr(mntp->mnt_mntopts, "rw")) {
417 			if (*(p+2) == ',' || *(p+2) == '\0')
418 				*(p+1) = 'o';
419 		}
420 	}
421 
422 	if (Oflg)
423 		mntflags |= MS_OVERLAY;
424 
425 	list = parse_replica(mntp->mnt_special, &n);
426 	if (list == NULL) {
427 		if (n < 0)
428 			pr_err(gettext("nfs file system; use [host:]path\n"));
429 		else
430 			pr_err(gettext("no memory\n"));
431 		return (RET_ERR);
432 	}
433 
434 	replicated = (n > 1);
435 
436 	/*
437 	 * There are some free() calls at the bottom of this loop, so be
438 	 * careful about adding continue statements.
439 	 */
440 	for (i = 0; i < n; i++) {
441 		char *path;
442 		char *host;
443 		ushort_t port;
444 
445 		argp = (struct nfs_args *)malloc(sizeof (*argp));
446 		if (argp == NULL) {
447 			pr_err(gettext("no memory\n"));
448 			last_error = RET_ERR;
449 			goto out;
450 		}
451 		memset(argp, 0, sizeof (*argp));
452 
453 		memset(&nfs_sec, 0, sizeof (nfs_sec));
454 		sec_opt = 0;
455 		use_pubfh = FALSE;
456 		url = FALSE;
457 		port = 0;
458 		snego_done = FALSE;
459 
460 		/*
461 		 * Looking for resources of the form
462 		 *	nfs://server_host[:port_number]/path_name
463 		 */
464 		if (strcmp(list[i].host, "nfs") == 0 && strncmp(list[i].path,
465 		    "//", 2) == 0) {
466 			char *sport, *cb;
467 			url = TRUE;
468 			oldpath = strdup(list[i].path);
469 			if (oldpath == NULL) {
470 				pr_err(gettext("memory allocation failure\n"));
471 				last_error = RET_ERR;
472 				goto out;
473 			}
474 			host = list[i].path+2;
475 			path = strchr(host, '/');
476 
477 			if (path == NULL) {
478 				pr_err(gettext(
479 				    "illegal nfs url syntax\n"));
480 				last_error = RET_ERR;
481 				goto out;
482 			}
483 
484 			*path = '\0';
485 			if (*host == '[') {
486 				cb = strchr(host, ']');
487 				if (cb == NULL) {
488 					pr_err(gettext(
489 					    "illegal nfs url syntax\n"));
490 					last_error = RET_ERR;
491 					goto out;
492 				} else {
493 					*cb = '\0';
494 					host++;
495 					cb++;
496 					if (*cb == ':')
497 						port = htons((ushort_t)
498 						    atoi(cb+1));
499 				}
500 			} else {
501 				sport = strchr(host, ':');
502 
503 				if (sport != NULL && sport < path) {
504 					*sport = '\0';
505 					port = htons((ushort_t)atoi(sport+1));
506 				}
507 			}
508 
509 			path++;
510 			if (*path == '\0')
511 				path = ".";
512 
513 		} else {
514 			host = list[i].host;
515 			path = list[i].path;
516 		}
517 
518 		if (r = set_args(&mntflags, argp, host, mntp)) {
519 			last_error = r;
520 			goto out;
521 		}
522 
523 		if (public_opt == TRUE)
524 			use_pubfh = TRUE;
525 
526 		if (port == 0) {
527 			port = nfs_port;
528 		} else if (nfs_port != 0 && nfs_port != port) {
529 			pr_err(gettext(
530 			    "port (%u) in nfs URL not the same"
531 			    " as port (%u) in port option\n"),
532 			    (unsigned int)ntohs(port),
533 			    (unsigned int)ntohs(nfs_port));
534 			last_error = RET_ERR;
535 			goto out;
536 		}
537 
538 
539 		if (replicated && !(mntflags & MS_RDONLY)) {
540 			pr_err(gettext(
541 			    "replicated mounts must be read-only\n"));
542 			last_error = RET_ERR;
543 			goto out;
544 		}
545 
546 		if (replicated && (argp->flags & NFSMNT_SOFT)) {
547 			pr_err(gettext(
548 			    "replicated mounts must not be soft\n"));
549 			last_error = RET_ERR;
550 			goto out;
551 		}
552 
553 		oldvers = vers;
554 		nconf = NULL;
555 
556 		r = RET_ERR;
557 
558 		/*
559 		 * If -o public was specified, and/or a URL was specified,
560 		 * then try the public file handle method.
561 		 */
562 		if ((use_pubfh == TRUE) || (url == TRUE)) {
563 			r = get_fh_via_pub(argp, host, path, url, use_pubfh,
564 			    &vers, &nconf, port);
565 
566 			if (r != RET_OK) {
567 				/*
568 				 * If -o public was specified, then return the
569 				 * error now.
570 				 */
571 				if (use_pubfh == TRUE) {
572 					last_error = r;
573 					goto out;
574 				}
575 			} else
576 				use_pubfh = TRUE;
577 			argp->flags |= NFSMNT_PUBLIC;
578 		}
579 
580 		if ((r != RET_OK) || (vers == NFS_V4)) {
581 			bool_t loud_on_mnt_err;
582 
583 			/*
584 			 * This can happen if -o public is not specified,
585 			 * special is a URL, and server doesn't support
586 			 * public file handle.
587 			 */
588 			if (url) {
589 				URLparse(path);
590 			}
591 
592 			/*
593 			 * If the path portion of the URL didn't have
594 			 * a leading / then there is good possibility
595 			 * that a mount without a leading slash will
596 			 * fail.
597 			 */
598 			if (url == TRUE && *path != '/')
599 				loud_on_mnt_err = FALSE;
600 			else
601 				loud_on_mnt_err = TRUE;
602 
603 			r = get_fh(argp, host, path, &vers,
604 			    loud_on_mnt_err, &nconf, port);
605 
606 			if (r != RET_OK) {
607 
608 				/*
609 				 * If there was no leading / and the path was
610 				 * derived from a URL, then try again
611 				 * with a leading /.
612 				 */
613 				if ((r == RET_MNTERR) &&
614 				    (loud_on_mnt_err == FALSE)) {
615 
616 					newpath = malloc(strlen(path)+2);
617 
618 					if (newpath == NULL) {
619 						pr_err(gettext("memory "
620 						    "allocation failure\n"));
621 						last_error = RET_ERR;
622 						goto out;
623 					}
624 
625 					strcpy(newpath, "/");
626 					strcat(newpath, path);
627 
628 					r = get_fh(argp, host, newpath, &vers,
629 					    TRUE, &nconf, port);
630 
631 					if (r == RET_OK)
632 						path = newpath;
633 				}
634 
635 				/*
636 				 * map exit code back to RET_ERR.
637 				 */
638 				if (r == RET_MNTERR)
639 					r = RET_ERR;
640 
641 				if (r != RET_OK) {
642 
643 					if (replicated) {
644 						if (argp->fh)
645 							free(argp->fh);
646 						if (argp->pathconf)
647 							free(argp->pathconf);
648 						free(argp);
649 						goto cont;
650 					}
651 
652 					last_error = r;
653 					goto out;
654 				}
655 			}
656 		}
657 
658 		if (oldvers && vers != oldvers) {
659 			pr_err(
660 			    gettext("replicas must have the same version\n"));
661 			last_error = RET_ERR;
662 			goto out;
663 		}
664 
665 		/*
666 		 * decide whether to use remote host's
667 		 * lockd or do local locking
668 		 */
669 		if (!(argp->flags & NFSMNT_LLOCK) && vers == NFS_VERSION &&
670 		    remote_lock(host, argp->fh)) {
671 			(void) fprintf(stderr, gettext(
672 			    "WARNING: No network locking on %s:%s:"),
673 			    host, path);
674 			(void) fprintf(stderr, gettext(
675 			    " contact admin to install server change\n"));
676 			argp->flags |= NFSMNT_LLOCK;
677 		}
678 
679 		if (self_check(host))
680 			argp->flags |= NFSMNT_LOOPBACK;
681 
682 		if (use_pubfh == FALSE) {
683 			/*
684 			 * Call to get_fh() above may have obtained the
685 			 * netconfig info and NULL proc'd the server.
686 			 * This would be the case with v4
687 			 */
688 			if (!(argp->flags & NFSMNT_KNCONF)) {
689 				nconf = NULL;
690 				if (r = getaddr_nfs(argp, host, &nconf,
691 				    FALSE, path, port, retry_error,
692 				    TRUE)) {
693 						last_error = r;
694 						goto out;
695 				}
696 			}
697 		}
698 
699 		if (make_secure(argp, host, nconf, use_pubfh, vers) < 0) {
700 			last_error = RET_ERR;
701 			goto out;
702 		}
703 
704 		if ((url == TRUE) && (use_pubfh == FALSE)) {
705 			/*
706 			 * Convert the special from
707 			 *	nfs://host/path
708 			 * to
709 			 *	host:path
710 			 */
711 			if (convert_special(&special, host, oldpath, path,
712 			    mntp->mnt_special) == -1) {
713 				(void) fprintf(stderr, gettext(
714 				    "could not convert URL nfs:%s to %s:%s\n"),
715 				    oldpath, host, path);
716 				last_error = RET_ERR;
717 				goto out;
718 			} else {
719 				mntp->mnt_special = special;
720 			}
721 		}
722 
723 		if (prev_argp == NULL)
724 			args = argp;
725 		else
726 			prev_argp->nfs_ext_u.nfs_extB.next = argp;
727 		prev_argp = argp;
728 
729 cont:
730 		if (oldpath != NULL) {
731 			free(oldpath);
732 			oldpath = NULL;
733 		}
734 
735 		if (newpath != NULL) {
736 			free(newpath);
737 			newpath = NULL;
738 		}
739 	}
740 
741 	argp = NULL;
742 
743 	if (args == NULL) {
744 		last_error = RET_RETRY;
745 		goto out;
746 	}
747 
748 	/* Determine which services are appropriate for the NFS version */
749 	if (strcmp(fstype, MNTTYPE_NFS4) == 0)
750 		sl = service_list_v4;
751 	else
752 		sl = service_list;
753 
754 	/*
755 	 * enable services as needed.
756 	 */
757 	_check_services(sl);
758 
759 	mntflags |= MS_DATA | MS_OPTIONSTR;
760 
761 	if (mflg)
762 		mntflags |= MS_NOMNTTAB;
763 
764 	if (!qflg)
765 		saveopts = strdup(mntp->mnt_mntopts);
766 
767 	/*
768 	 * And make sure that we have the ephemeral mount_to
769 	 * set for this zone.
770 	 */
771 	set_nfsv4_ephemeral_mount_to();
772 
773 	if (mount(mntp->mnt_special, mntp->mnt_mountp, mntflags, fstype, args,
774 	    sizeof (*args), mntp->mnt_mntopts, MAX_MNTOPT_STR) < 0) {
775 		if (errno != ENOENT) {
776 			pr_err(gettext("mount: %s: %s\n"),
777 			    mntp->mnt_mountp, strerror(errno));
778 		} else {
779 			struct stat sb;
780 			if (stat(mntp->mnt_mountp, &sb) < 0 && errno == ENOENT)
781 				pr_err(gettext("mount: %s: %s\n"),
782 				    mntp->mnt_mountp, strerror(ENOENT));
783 			else
784 				pr_err("%s: %s\n", mntp->mnt_special,
785 				    strerror(ENOENT));
786 		}
787 
788 		last_error = RET_ERR;
789 		goto out;
790 	}
791 
792 	if (!qflg && saveopts != NULL) {
793 		cmp_requested_to_actual_options(saveopts, mntp->mnt_mntopts,
794 		    mntp->mnt_special, mntp->mnt_mountp);
795 	}
796 
797 out:
798 	if (saveopts != NULL)
799 		free(saveopts);
800 	if (special != NULL)
801 		free(special);
802 	if (oldpath != NULL)
803 		free(oldpath);
804 	if (newpath != NULL)
805 		free(newpath);
806 
807 	free_replica(list, n);
808 
809 	if (argp != NULL) {
810 		/*
811 		 * If we had a new entry which was not added to the
812 		 * list yet, then add it now that it can be freed.
813 		 */
814 		if (prev_argp == NULL)
815 			args = argp;
816 		else
817 			prev_argp->nfs_ext_u.nfs_extB.next = argp;
818 	}
819 	argp = args;
820 	while (argp != NULL) {
821 		if (argp->fh)
822 			free(argp->fh);
823 		if (argp->pathconf)
824 			free(argp->pathconf);
825 		if (argp->knconf)
826 			free(argp->knconf);
827 		if (argp->addr) {
828 			free(argp->addr->buf);
829 			free(argp->addr);
830 		}
831 		nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
832 		if (argp->syncaddr) {
833 			free(argp->syncaddr->buf);
834 			free(argp->syncaddr);
835 		}
836 		if (argp->netname)
837 			free(argp->netname);
838 		prev_argp = argp;
839 		argp = argp->nfs_ext_u.nfs_extB.next;
840 		free(prev_argp);
841 	}
842 
843 	return (last_error);
844 }
845 
846 /*
847  * These options are duplicated in uts/common/fs/nfs/nfs_dlinet.c
848  * Changes must be made to both lists.
849  */
850 static char *optlist[] = {
851 #define	OPT_RO		0
852 	MNTOPT_RO,
853 #define	OPT_RW		1
854 	MNTOPT_RW,
855 #define	OPT_QUOTA	2
856 	MNTOPT_QUOTA,
857 #define	OPT_NOQUOTA	3
858 	MNTOPT_NOQUOTA,
859 #define	OPT_SOFT	4
860 	MNTOPT_SOFT,
861 #define	OPT_HARD	5
862 	MNTOPT_HARD,
863 #define	OPT_SUID	6
864 	MNTOPT_SUID,
865 #define	OPT_NOSUID	7
866 	MNTOPT_NOSUID,
867 #define	OPT_GRPID	8
868 	MNTOPT_GRPID,
869 #define	OPT_REMOUNT	9
870 	MNTOPT_REMOUNT,
871 #define	OPT_NOSUB	10
872 	MNTOPT_NOSUB,
873 #define	OPT_INTR	11
874 	MNTOPT_INTR,
875 #define	OPT_NOINTR	12
876 	MNTOPT_NOINTR,
877 #define	OPT_PORT	13
878 	MNTOPT_PORT,
879 #define	OPT_SECURE	14
880 	MNTOPT_SECURE,
881 #define	OPT_RSIZE	15
882 	MNTOPT_RSIZE,
883 #define	OPT_WSIZE	16
884 	MNTOPT_WSIZE,
885 #define	OPT_TIMEO	17
886 	MNTOPT_TIMEO,
887 #define	OPT_RETRANS	18
888 	MNTOPT_RETRANS,
889 #define	OPT_ACTIMEO	19
890 	MNTOPT_ACTIMEO,
891 #define	OPT_ACREGMIN	20
892 	MNTOPT_ACREGMIN,
893 #define	OPT_ACREGMAX	21
894 	MNTOPT_ACREGMAX,
895 #define	OPT_ACDIRMIN	22
896 	MNTOPT_ACDIRMIN,
897 #define	OPT_ACDIRMAX	23
898 	MNTOPT_ACDIRMAX,
899 #define	OPT_BG		24
900 	MNTOPT_BG,
901 #define	OPT_FG		25
902 	MNTOPT_FG,
903 #define	OPT_RETRY	26
904 	MNTOPT_RETRY,
905 #define	OPT_NOAC	27
906 	MNTOPT_NOAC,
907 #define	OPT_NOCTO	28
908 	MNTOPT_NOCTO,
909 #define	OPT_LLOCK	29
910 	MNTOPT_LLOCK,
911 #define	OPT_POSIX	30
912 	MNTOPT_POSIX,
913 #define	OPT_VERS	31
914 	MNTOPT_VERS,
915 #define	OPT_PROTO	32
916 	MNTOPT_PROTO,
917 #define	OPT_SEMISOFT	33
918 	MNTOPT_SEMISOFT,
919 #define	OPT_NOPRINT	34
920 	MNTOPT_NOPRINT,
921 #define	OPT_SEC		35
922 	MNTOPT_SEC,
923 #define	OPT_LARGEFILES	36
924 	MNTOPT_LARGEFILES,
925 #define	OPT_NOLARGEFILES 37
926 	MNTOPT_NOLARGEFILES,
927 #define	OPT_PUBLIC	38
928 	MNTOPT_PUBLIC,
929 #define	OPT_DIRECTIO	39
930 	MNTOPT_FORCEDIRECTIO,
931 #define	OPT_NODIRECTIO	40
932 	MNTOPT_NOFORCEDIRECTIO,
933 #define	OPT_XATTR	41
934 	MNTOPT_XATTR,
935 #define	OPT_NOXATTR	42
936 	MNTOPT_NOXATTR,
937 #define	OPT_DEVICES	43
938 	MNTOPT_DEVICES,
939 #define	OPT_NODEVICES	44
940 	MNTOPT_NODEVICES,
941 #define	OPT_SETUID	45
942 	MNTOPT_SETUID,
943 #define	OPT_NOSETUID	46
944 	MNTOPT_NOSETUID,
945 #define	OPT_EXEC	47
946 	MNTOPT_EXEC,
947 #define	OPT_NOEXEC	48
948 	MNTOPT_NOEXEC,
949 	NULL
950 };
951 
952 #define	bad(val) (val == NULL || !isdigit(*val))
953 
954 static int
955 set_args(int *mntflags, struct nfs_args *args, char *fshost, struct mnttab *mnt)
956 {
957 	char *saveopt, *optstr, *opts, *newopts, *val;
958 	int largefiles = 0;
959 	int invalid = 0;
960 	int attrpref = 0;
961 	int optlen;
962 
963 	args->flags = NFSMNT_INT;	/* default is "intr" */
964 	args->flags |= NFSMNT_HOSTNAME;
965 	args->flags |= NFSMNT_NEWARGS;	/* using extented nfs_args structure */
966 	args->hostname = fshost;
967 
968 	optstr = opts = strdup(mnt->mnt_mntopts);
969 	/* sizeof (MNTOPT_XXX) includes one extra byte we may need for "," */
970 	optlen = strlen(mnt->mnt_mntopts) + sizeof (MNTOPT_XATTR) + 1;
971 	if (optlen > MAX_MNTOPT_STR) {
972 		pr_err(gettext("option string too long"));
973 		return (RET_ERR);
974 	}
975 	newopts = malloc(optlen);
976 	if (opts == NULL || newopts == NULL) {
977 		pr_err(gettext("no memory"));
978 		if (opts)
979 			free(opts);
980 		if (newopts)
981 			free(newopts);
982 		return (RET_ERR);
983 	}
984 	newopts[0] = '\0';
985 
986 	while (*opts) {
987 		invalid = 0;
988 		saveopt = opts;
989 		switch (getsubopt(&opts, optlist, &val)) {
990 		case OPT_RO:
991 			*mntflags |= MS_RDONLY;
992 			break;
993 		case OPT_RW:
994 			*mntflags &= ~(MS_RDONLY);
995 			break;
996 		case OPT_QUOTA:
997 		case OPT_NOQUOTA:
998 			break;
999 		case OPT_SOFT:
1000 			args->flags |= NFSMNT_SOFT;
1001 			args->flags &= ~(NFSMNT_SEMISOFT);
1002 			break;
1003 		case OPT_SEMISOFT:
1004 			args->flags |= NFSMNT_SOFT;
1005 			args->flags |= NFSMNT_SEMISOFT;
1006 			break;
1007 		case OPT_HARD:
1008 			args->flags &= ~(NFSMNT_SOFT);
1009 			args->flags &= ~(NFSMNT_SEMISOFT);
1010 			break;
1011 		case OPT_SUID:
1012 			*mntflags &= ~(MS_NOSUID);
1013 			break;
1014 		case OPT_NOSUID:
1015 			*mntflags |= MS_NOSUID;
1016 			break;
1017 		case OPT_GRPID:
1018 			args->flags |= NFSMNT_GRPID;
1019 			break;
1020 		case OPT_REMOUNT:
1021 			*mntflags |= MS_REMOUNT;
1022 			break;
1023 		case OPT_INTR:
1024 			args->flags |= NFSMNT_INT;
1025 			break;
1026 		case OPT_NOINTR:
1027 			args->flags &= ~(NFSMNT_INT);
1028 			break;
1029 		case OPT_NOAC:
1030 			args->flags |= NFSMNT_NOAC;
1031 			break;
1032 		case OPT_PORT:
1033 			if (bad(val))
1034 				goto badopt;
1035 			nfs_port = htons((ushort_t)atoi(val));
1036 			break;
1037 
1038 		case OPT_SECURE:
1039 			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
1040 				pr_err(gettext("can not get \"dh\" from %s\n"),
1041 				    NFSSEC_CONF);
1042 				goto badopt;
1043 			}
1044 			sec_opt++;
1045 			break;
1046 
1047 		case OPT_NOCTO:
1048 			args->flags |= NFSMNT_NOCTO;
1049 			break;
1050 
1051 		case OPT_RSIZE:
1052 			args->flags |= NFSMNT_RSIZE;
1053 			if (bad(val))
1054 				goto badopt;
1055 			args->rsize = atoi(val);
1056 			break;
1057 		case OPT_WSIZE:
1058 			args->flags |= NFSMNT_WSIZE;
1059 			if (bad(val))
1060 				goto badopt;
1061 			args->wsize = atoi(val);
1062 			break;
1063 		case OPT_TIMEO:
1064 			args->flags |= NFSMNT_TIMEO;
1065 			if (bad(val))
1066 				goto badopt;
1067 			args->timeo = atoi(val);
1068 			break;
1069 		case OPT_RETRANS:
1070 			args->flags |= NFSMNT_RETRANS;
1071 			if (bad(val))
1072 				goto badopt;
1073 			args->retrans = atoi(val);
1074 			break;
1075 		case OPT_ACTIMEO:
1076 			args->flags |= NFSMNT_ACDIRMAX;
1077 			args->flags |= NFSMNT_ACREGMAX;
1078 			args->flags |= NFSMNT_ACDIRMIN;
1079 			args->flags |= NFSMNT_ACREGMIN;
1080 			if (bad(val))
1081 				goto badopt;
1082 			args->acdirmin = args->acregmin = args->acdirmax
1083 			    = args->acregmax = atoi(val);
1084 			break;
1085 		case OPT_ACREGMIN:
1086 			args->flags |= NFSMNT_ACREGMIN;
1087 			if (bad(val))
1088 				goto badopt;
1089 			args->acregmin = atoi(val);
1090 			break;
1091 		case OPT_ACREGMAX:
1092 			args->flags |= NFSMNT_ACREGMAX;
1093 			if (bad(val))
1094 				goto badopt;
1095 			args->acregmax = atoi(val);
1096 			break;
1097 		case OPT_ACDIRMIN:
1098 			args->flags |= NFSMNT_ACDIRMIN;
1099 			if (bad(val))
1100 				goto badopt;
1101 			args->acdirmin = atoi(val);
1102 			break;
1103 		case OPT_ACDIRMAX:
1104 			args->flags |= NFSMNT_ACDIRMAX;
1105 			if (bad(val))
1106 				goto badopt;
1107 			args->acdirmax = atoi(val);
1108 			break;
1109 		case OPT_BG:
1110 			bg++;
1111 			break;
1112 		case OPT_FG:
1113 			bg = 0;
1114 			break;
1115 		case OPT_RETRY:
1116 			if (bad(val))
1117 				goto badopt;
1118 			retries = atoi(val);
1119 			break;
1120 		case OPT_LLOCK:
1121 			args->flags |= NFSMNT_LLOCK;
1122 			break;
1123 		case OPT_POSIX:
1124 			posix = 1;
1125 			break;
1126 		case OPT_VERS:
1127 			if (bad(val))
1128 				goto badopt;
1129 			nfsvers = (rpcvers_t)atoi(val);
1130 			break;
1131 		case OPT_PROTO:
1132 			if (val == NULL)
1133 				goto badopt;
1134 
1135 			nfs_proto = (char *)malloc(strlen(val)+1);
1136 			if (!nfs_proto) {
1137 				pr_err(gettext("no memory"));
1138 				return (RET_ERR);
1139 			}
1140 
1141 			(void) strncpy(nfs_proto, val, strlen(val)+1);
1142 			break;
1143 
1144 		case OPT_NOPRINT:
1145 			args->flags |= NFSMNT_NOPRINT;
1146 			break;
1147 
1148 		case OPT_LARGEFILES:
1149 			largefiles = 1;
1150 			break;
1151 
1152 		case OPT_NOLARGEFILES:
1153 			pr_err(gettext("NFS can't support \"nolargefiles\"\n"));
1154 			free(optstr);
1155 			return (RET_ERR);
1156 
1157 		case OPT_SEC:
1158 			if (val == NULL) {
1159 				pr_err(gettext(
1160 				    "\"sec\" option requires argument\n"));
1161 				return (RET_ERR);
1162 			}
1163 			if (nfs_getseconfig_byname(val, &nfs_sec)) {
1164 				pr_err(gettext("can not get \"%s\" from %s\n"),
1165 				    val, NFSSEC_CONF);
1166 				return (RET_ERR);
1167 			}
1168 			sec_opt++;
1169 			break;
1170 
1171 		case OPT_PUBLIC:
1172 			public_opt = TRUE;
1173 			break;
1174 
1175 		case OPT_DIRECTIO:
1176 			args->flags |= NFSMNT_DIRECTIO;
1177 			break;
1178 
1179 		case OPT_NODIRECTIO:
1180 			args->flags &= ~(NFSMNT_DIRECTIO);
1181 			break;
1182 
1183 		case OPT_XATTR:
1184 		case OPT_NOXATTR:
1185 			/*
1186 			 * VFS options; just need to get them into the
1187 			 * new mount option string and note we've seen them
1188 			 */
1189 			attrpref = 1;
1190 			break;
1191 		default:
1192 			/*
1193 			 * Note that this could be a valid OPT_* option so
1194 			 * we can't use "val" but need to use "saveopt".
1195 			 */
1196 			if (fsisstdopt(saveopt))
1197 				break;
1198 			invalid = 1;
1199 			if (!qflg)
1200 				(void) fprintf(stderr, gettext(
1201 				    "mount: %s on %s - WARNING unknown option"
1202 				    " \"%s\"\n"), mnt->mnt_special,
1203 				    mnt->mnt_mountp, saveopt);
1204 			break;
1205 		}
1206 		if (!invalid) {
1207 			if (newopts[0])
1208 				strcat(newopts, ",");
1209 			strcat(newopts, saveopt);
1210 		}
1211 	}
1212 	/* Default is to turn extended attrs on */
1213 	if (!attrpref) {
1214 		if (newopts[0])
1215 			strcat(newopts, ",");
1216 		strcat(newopts, MNTOPT_XATTR);
1217 	}
1218 	strcpy(mnt->mnt_mntopts, newopts);
1219 	free(newopts);
1220 	free(optstr);
1221 
1222 	/* ensure that only one secure mode is requested */
1223 	if (sec_opt > 1) {
1224 		pr_err(gettext("Security options conflict\n"));
1225 		return (RET_ERR);
1226 	}
1227 
1228 	/* ensure that the user isn't trying to get large files over V2 */
1229 	if (nfsvers == NFS_VERSION && largefiles) {
1230 		pr_err(gettext("NFS V2 can't support \"largefiles\"\n"));
1231 		return (RET_ERR);
1232 	}
1233 
1234 	if (nfsvers == NFS_V4 &&
1235 	    nfs_proto != NULL &&
1236 	    strncasecmp(nfs_proto, NC_UDP, strlen(NC_UDP)) == 0) {
1237 		pr_err(gettext("NFS V4 does not support %s\n"), nfs_proto);
1238 		return (RET_ERR);
1239 	}
1240 
1241 	return (RET_OK);
1242 
1243 badopt:
1244 	pr_err(gettext("invalid option: \"%s\"\n"), saveopt);
1245 	free(optstr);
1246 	return (RET_ERR);
1247 }
1248 
1249 static int
1250 make_secure(struct nfs_args *args, char *hostname, struct netconfig *nconf,
1251 	bool_t use_pubfh, rpcvers_t vers)
1252 {
1253 	sec_data_t *secdata;
1254 	int flags;
1255 	struct netbuf *syncaddr = NULL;
1256 	struct nd_addrlist *retaddrs = NULL;
1257 	char netname[MAXNETNAMELEN+1];
1258 
1259 	/*
1260 	 * check to see if any secure mode is requested.
1261 	 * if not, use default security mode.
1262 	 */
1263 	if (!snego_done && !sec_opt) {
1264 		/*
1265 		 * Get default security mode.
1266 		 * AUTH_UNIX has been the default choice for a long time.
1267 		 * The better NFS security service becomes, the better chance
1268 		 * we will set stronger security service as the default NFS
1269 		 * security mode.
1270 		 */
1271 		if (nfs_getseconfig_default(&nfs_sec)) {
1272 			pr_err(gettext("error getting default"
1273 			    " security entry\n"));
1274 			return (-1);
1275 		}
1276 		args->flags |= NFSMNT_SECDEFAULT;
1277 	}
1278 
1279 	/*
1280 	 * Get the network address for the time service on the server.
1281 	 * If an RPC based time service is not available then try the
1282 	 * IP time service.
1283 	 *
1284 	 * This is for AUTH_DH processing. We will also pass down syncaddr
1285 	 * and netname for NFS V4 even if AUTH_DH is not requested right now.
1286 	 * NFS V4 does security negotiation in the kernel via SECINFO.
1287 	 * These information might be needed later in the kernel.
1288 	 *
1289 	 * Eventurally, we want to move this code to nfs_clnt_secdata()
1290 	 * when autod_nfs.c and mount.c can share the same get_the_addr()
1291 	 * routine.
1292 	 */
1293 	flags = 0;
1294 	syncaddr = NULL;
1295 
1296 	if (nfs_sec.sc_rpcnum == AUTH_DH || vers == NFS_V4) {
1297 		/*
1298 		 * If using the public fh or nfsv4, we will not contact the
1299 		 * remote RPCBINDer, since it is possibly behind a firewall.
1300 		 */
1301 		if (use_pubfh == FALSE && vers != NFS_V4)
1302 			syncaddr = get_the_addr(hostname, RPCBPROG, RPCBVERS,
1303 			    nconf, 0, NULL, NULL, FALSE, NULL, NULL);
1304 
1305 		if (syncaddr != NULL) {
1306 			/* for flags in sec_data */
1307 			flags |= AUTH_F_RPCTIMESYNC;
1308 		} else {
1309 			struct nd_hostserv hs;
1310 			int error;
1311 
1312 			hs.h_host = hostname;
1313 			hs.h_serv = "timserver";
1314 
1315 			error = netdir_getbyname(nconf, &hs, &retaddrs);
1316 
1317 			if (error != ND_OK && (nfs_sec.sc_rpcnum == AUTH_DH)) {
1318 				pr_err(gettext("%s: secure: no time service\n"),
1319 				    hostname);
1320 				return (-1);
1321 			}
1322 
1323 			if (error == ND_OK)
1324 				syncaddr = retaddrs->n_addrs;
1325 
1326 			/*
1327 			 * For NFS_V4 if AUTH_DH is negotiated later in the
1328 			 * kernel thru SECINFO, it will need syncaddr
1329 			 * and netname data.
1330 			 */
1331 			if (vers == NFS_V4 && syncaddr &&
1332 			    host2netname(netname, hostname, NULL)) {
1333 				args->syncaddr = malloc(sizeof (struct netbuf));
1334 				args->syncaddr->buf = malloc(syncaddr->len);
1335 				(void) memcpy(args->syncaddr->buf,
1336 				    syncaddr->buf, syncaddr->len);
1337 				args->syncaddr->len = syncaddr->len;
1338 				args->syncaddr->maxlen = syncaddr->maxlen;
1339 				args->netname = strdup(netname);
1340 				args->flags |= NFSMNT_SECURE;
1341 			}
1342 		}
1343 	}
1344 
1345 	/*
1346 	 * For the initial chosen flavor (any flavor defined in nfssec.conf),
1347 	 * the data will be stored in the sec_data structure via
1348 	 * nfs_clnt_secdata() and be passed to the kernel via nfs_args_*
1349 	 * extended data structure.
1350 	 */
1351 	if (!(secdata = nfs_clnt_secdata(&nfs_sec, hostname, args->knconf,
1352 	    syncaddr, flags))) {
1353 		pr_err(gettext("errors constructing security related data\n"));
1354 		if (flags & AUTH_F_RPCTIMESYNC) {
1355 			free(syncaddr->buf);
1356 			free(syncaddr);
1357 		} else if (retaddrs)
1358 			netdir_free((void *)retaddrs, ND_ADDRLIST);
1359 		return (-1);
1360 	}
1361 
1362 	NFS_ARGS_EXTB_secdata(args, secdata);
1363 	if (flags & AUTH_F_RPCTIMESYNC) {
1364 		free(syncaddr->buf);
1365 		free(syncaddr);
1366 	} else if (retaddrs)
1367 		netdir_free((void *)retaddrs, ND_ADDRLIST);
1368 	return (0);
1369 }
1370 
1371 /*
1372  * Get the network address on "hostname" for program "prog"
1373  * with version "vers" by using the nconf configuration data
1374  * passed in.
1375  *
1376  * If the address of a netconfig pointer is null then
1377  * information is not sufficient and no netbuf will be returned.
1378  *
1379  * Finally, ping the null procedure of that service.
1380  *
1381  * A similar routine is also defined in ../../autofs/autod_nfs.c.
1382  * This is a potential routine to move to ../lib for common usage.
1383  */
1384 static struct netbuf *
1385 get_the_addr(char *hostname, ulong_t prog, ulong_t vers,
1386 	struct netconfig *nconf, ushort_t port, struct t_info *tinfo,
1387 	caddr_t *fhp, bool_t get_pubfh, char *fspath, err_ret_t *error)
1388 {
1389 	struct netbuf *nb = NULL;
1390 	struct t_bind *tbind = NULL;
1391 	CLIENT *cl = NULL;
1392 	struct timeval tv;
1393 	int fd = -1;
1394 	AUTH *ah = NULL;
1395 	AUTH *new_ah = NULL;
1396 	struct snego_t snego;
1397 
1398 	if (nconf == NULL)
1399 		return (NULL);
1400 
1401 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) == -1)
1402 		goto done;
1403 
1404 	/* LINTED pointer alignment */
1405 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
1406 	    == NULL)
1407 		goto done;
1408 
1409 	/*
1410 	 * In the case of public filehandle usage or NFSv4 we want to
1411 	 * avoid use of the rpcbind/portmap protocol
1412 	 */
1413 	if ((get_pubfh == TRUE) || (vers == NFS_V4)) {
1414 		struct nd_hostserv hs;
1415 		struct nd_addrlist *retaddrs;
1416 		int retval;
1417 		hs.h_host = hostname;
1418 
1419 		/* NFS where vers==4 does not support UDP */
1420 		if (vers == NFS_V4 &&
1421 		    strncasecmp(nconf->nc_proto, NC_UDP,
1422 		    strlen(NC_UDP)) == 0) {
1423 			SET_ERR_RET(error, ERR_PROTO_UNSUPP, 0);
1424 			goto done;
1425 		}
1426 
1427 		if (port == 0)
1428 			hs.h_serv = "nfs";
1429 		else
1430 			hs.h_serv = NULL;
1431 
1432 		if ((retval = netdir_getbyname(nconf, &hs, &retaddrs))
1433 		    != ND_OK) {
1434 			/*
1435 			 * Carefully set the error value here. Want to signify
1436 			 * that the error was an unknown host.
1437 			 */
1438 			if (retval == ND_NOHOST) {
1439 				SET_ERR_RET(error, ERR_NOHOST, retval);
1440 			}
1441 
1442 			goto done;
1443 		}
1444 		memcpy(tbind->addr.buf, retaddrs->n_addrs->buf,
1445 		    retaddrs->n_addrs->len);
1446 		tbind->addr.len = retaddrs->n_addrs->len;
1447 		netdir_free((void *)retaddrs, ND_ADDRLIST);
1448 		(void) netdir_options(nconf, ND_SET_RESERVEDPORT, fd, NULL);
1449 
1450 	} else {
1451 		if (rpcb_getaddr(prog, vers, nconf, &tbind->addr,
1452 		    hostname) == FALSE) {
1453 			goto done;
1454 		}
1455 	}
1456 
1457 	if (port) {
1458 		/* LINTED pointer alignment */
1459 		if (strcmp(nconf->nc_protofmly, NC_INET) == 0)
1460 			((struct sockaddr_in *)tbind->addr.buf)->sin_port
1461 			    = port;
1462 		else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
1463 			((struct sockaddr_in6 *)tbind->addr.buf)->sin6_port
1464 			    = port;
1465 
1466 	}
1467 
1468 	cl = clnt_tli_create(fd, nconf, &tbind->addr, prog, vers, 0, 0);
1469 	if (cl == NULL) {
1470 		/*
1471 		 * clnt_tli_create() returns either RPC_SYSTEMERROR,
1472 		 * RPC_UNKNOWNPROTO or RPC_TLIERROR. The RPC_TLIERROR translates
1473 		 * to "Misc. TLI error". This is not too helpful. Most likely
1474 		 * the connection to the remote server timed out, so this
1475 		 * error is at least less perplexing.
1476 		 * See: usr/src/cmd/rpcinfo/rpcinfo.c
1477 		 */
1478 		if (rpc_createerr.cf_stat == RPC_TLIERROR) {
1479 			SET_ERR_RET(error, ERR_RPCERROR, RPC_PMAPFAILURE);
1480 		} else {
1481 			SET_ERR_RET(error, ERR_RPCERROR, rpc_createerr.cf_stat);
1482 		}
1483 		goto done;
1484 	}
1485 
1486 	ah = authsys_create_default();
1487 	if (ah != NULL)
1488 		cl->cl_auth = ah;
1489 
1490 	tv.tv_sec = 5;
1491 	tv.tv_usec = 0;
1492 
1493 	(void) clnt_control(cl, CLSET_TIMEOUT, (char *)&tv);
1494 
1495 	if ((get_pubfh == TRUE) && (vers != NFS_V4)) {
1496 		enum snego_stat sec;
1497 
1498 		if (!snego_done) {
1499 			/*
1500 			 * negotiate sec flavor.
1501 			 */
1502 			snego.cnt = 0;
1503 			if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
1504 			    SNEGO_SUCCESS) {
1505 				int jj;
1506 
1507 				/*
1508 				 * check if server supports the one
1509 				 * specified in the sec= option.
1510 				 */
1511 				if (sec_opt) {
1512 					for (jj = 0; jj < snego.cnt; jj++) {
1513 						if (snego.array[jj] ==
1514 						    nfs_sec.sc_nfsnum) {
1515 							snego_done = TRUE;
1516 							break;
1517 						}
1518 					}
1519 				}
1520 
1521 				/*
1522 				 * find a common sec flavor
1523 				 */
1524 				if (!snego_done) {
1525 					if (sec_opt) {
1526 						pr_err(gettext(
1527 						    "Server does not support"
1528 						    " the security flavor"
1529 						    " specified.\n"));
1530 					}
1531 
1532 					for (jj = 0; jj < snego.cnt; jj++) {
1533 						if (!nfs_getseconfig_bynumber(
1534 						    snego.array[jj],
1535 						    &nfs_sec)) {
1536 							snego_done = TRUE;
1537 #define	EMSG80SUX "Security flavor %d was negotiated and will be used.\n"
1538 							if (sec_opt)
1539 								pr_err(gettext(
1540 								    EMSG80SUX),
1541 								    nfs_sec.
1542 								    sc_nfsnum);
1543 							break;
1544 						}
1545 					}
1546 				}
1547 
1548 				if (!snego_done)
1549 					return (NULL);
1550 
1551 				/*
1552 				 * Now that the flavor has been
1553 				 * negotiated, get the fh.
1554 				 *
1555 				 * First, create an auth handle using the
1556 				 * negotiated sec flavor in the next lookup to
1557 				 * fetch the filehandle.
1558 				 */
1559 				new_ah = nfs_create_ah(cl, hostname, &nfs_sec);
1560 				if (new_ah == NULL)
1561 					goto done;
1562 				cl->cl_auth = new_ah;
1563 			} else if (sec == SNEGO_ARRAY_TOO_SMALL || sec ==
1564 			    SNEGO_FAILURE) {
1565 				goto done;
1566 			}
1567 
1568 			/*
1569 			 * Note that if sec == SNEGO_DEF_VALID
1570 			 * default sec flavor is acceptable.
1571 			 * Use it to get the filehandle.
1572 			 */
1573 		}
1574 
1575 		if (vers == NFS_VERSION) {
1576 			wnl_diropargs arg;
1577 			wnl_diropres res;
1578 
1579 			memset((char *)&arg.dir, 0, sizeof (wnl_fh));
1580 			arg.name = fspath;
1581 			memset((char *)&res, 0, sizeof (wnl_diropres));
1582 			if (wnlproc_lookup_2(&arg, &res, cl) !=
1583 			    RPC_SUCCESS || res.status != NFS_OK)
1584 				goto done;
1585 
1586 			*fhp = malloc(sizeof (wnl_fh));
1587 
1588 			if (*fhp == NULL) {
1589 				pr_err(gettext("no memory\n"));
1590 				goto done;
1591 			}
1592 
1593 			memcpy((char *)*fhp,
1594 			    (char *)&res.wnl_diropres_u.wnl_diropres.file,
1595 			    sizeof (wnl_fh));
1596 		} else {
1597 			WNL_LOOKUP3args arg;
1598 			WNL_LOOKUP3res res;
1599 			nfs_fh3 *fh3p;
1600 
1601 			memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
1602 			arg.what.name = fspath;
1603 			memset((char *)&res, 0, sizeof (WNL_LOOKUP3res));
1604 			if (wnlproc3_lookup_3(&arg, &res, cl) !=
1605 			    RPC_SUCCESS || res.status != NFS3_OK)
1606 				goto done;
1607 
1608 			fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
1609 
1610 			if (fh3p == NULL) {
1611 				pr_err(gettext("no memory\n"));
1612 				goto done;
1613 			}
1614 
1615 			fh3p->fh3_length =
1616 			    res.WNL_LOOKUP3res_u.res_ok.object.data.data_len;
1617 			memcpy(fh3p->fh3_u.data,
1618 			    res.WNL_LOOKUP3res_u.res_ok.object.data.data_val,
1619 			    fh3p->fh3_length);
1620 
1621 			*fhp = (caddr_t)fh3p;
1622 		}
1623 	} else {
1624 		struct rpc_err r_err;
1625 		enum clnt_stat rc;
1626 
1627 		/*
1628 		 * NULL procedures need not have an argument or
1629 		 * result param.
1630 		 */
1631 		if (vers == NFS_VERSION)
1632 			rc = wnlproc_null_2(NULL, NULL, cl);
1633 		else if (vers == NFS_V3)
1634 			rc = wnlproc3_null_3(NULL, NULL, cl);
1635 		else
1636 			rc = wnlproc4_null_4(NULL, NULL, cl);
1637 
1638 		if (rc != RPC_SUCCESS) {
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 look for default auth
2300 		 * (default auth entry in /etc/nfssec.conf) in the auth list
2301 		 * returned from server. If default auth not found, then use
2302 		 * the first supported security flavor (by the client) in the
2303 		 * auth list returned from the server.
2304 		 *
2305 		 */
2306 		auths =
2307 		    mountres3.mountres3_u.mountinfo.auth_flavors
2308 		    .auth_flavors_val;
2309 		count =
2310 		    mountres3.mountres3_u.mountinfo.auth_flavors
2311 		    .auth_flavors_len;
2312 
2313 		if (count <= 0) {
2314 			pr_err(gettext(
2315 			    "server %s did not return any security mode\n"),
2316 			    fshost);
2317 			clnt_destroy(cl);
2318 			return (RET_ERR);
2319 		}
2320 
2321 		if (sec_opt) {
2322 			for (i = 0; i < count; i++) {
2323 				if (auths[i] == nfs_sec.sc_nfsnum)
2324 					break;
2325 			}
2326 			if (i == count)
2327 				goto autherr;
2328 		} else {
2329 			seconfig_t default_sec;
2330 
2331 			/*
2332 			 * Get client configured default auth.
2333 			 */
2334 			nfs_sec.sc_nfsnum = -1;
2335 			default_sec.sc_nfsnum = -1;
2336 			(void) nfs_getseconfig_default(&default_sec);
2337 
2338 			/*
2339 			 * Look for clients default auth in servers list.
2340 			 */
2341 			if (default_sec.sc_nfsnum != -1) {
2342 				for (i = 0; i < count; i++) {
2343 					if (auths[i] == default_sec.sc_nfsnum) {
2344 						sec_opt++;
2345 						nfs_sec = default_sec;
2346 						break;
2347 					}
2348 				}
2349 			}
2350 
2351 			/*
2352 			 * Could not find clients default auth in servers list.
2353 			 * Pick the first auth from servers list that is
2354 			 * also supported on the client.
2355 			 */
2356 			if (nfs_sec.sc_nfsnum == -1) {
2357 				for (i = 0; i < count; i++) {
2358 					if (!nfs_getseconfig_bynumber(auths[i],
2359 					    &nfs_sec)) {
2360 						sec_opt++;
2361 						break;
2362 
2363 					}
2364 				}
2365 			}
2366 
2367 			if (i == count)
2368 				goto autherr;
2369 		}
2370 		break;
2371 	default:
2372 		pr_err(gettext("%s:%s: Unknown MOUNT version %d\n"),
2373 		    fshost, fspath, outvers);
2374 		clnt_destroy(cl);
2375 		return (RET_ERR);
2376 	}
2377 
2378 	clnt_destroy(cl);
2379 	return (RET_OK);
2380 
2381 autherr:
2382 	pr_err(gettext(
2383 	    "security mode does not match the server exporting %s:%s\n"),
2384 	    fshost, fspath);
2385 	clnt_destroy(cl);
2386 	return (RET_ERR);
2387 }
2388 
2389 /*
2390  * Fill in the address for the server's NFS service and
2391  * fill in a knetconfig structure for the transport that
2392  * the service is available on.
2393  */
2394 static int
2395 getaddr_nfs(struct nfs_args *args, char *fshost, struct netconfig **nconfp,
2396 	    bool_t get_pubfh, char *fspath, ushort_t port, err_ret_t *error,
2397 	    bool_t print_rpcerror)
2398 {
2399 	struct stat sb;
2400 	struct netconfig *nconf;
2401 	struct knetconfig *knconfp;
2402 	static int printed = 0;
2403 	struct t_info tinfo;
2404 	err_ret_t addr_error;
2405 
2406 	SET_ERR_RET(error, ERR_PROTO_NONE, 0);
2407 	SET_ERR_RET(&addr_error, ERR_PROTO_NONE, 0);
2408 
2409 	if (nfs_proto) {
2410 		/*
2411 		 * If a proto is specified and its rdma try this. The kernel
2412 		 * will later do the reachablity test and fail form there
2413 		 * if rdma transport is not available to kernel rpc
2414 		 */
2415 		if (strcmp(nfs_proto, "rdma") == 0) {
2416 			args->addr = get_addr(fshost, NFS_PROGRAM,
2417 			    nfsvers_to_use, nconfp, NULL, port, &tinfo,
2418 			    &args->fh, get_pubfh, fspath, &addr_error);
2419 
2420 			args->flags |= NFSMNT_DORDMA;
2421 		} else {
2422 			args->addr = get_addr(fshost, NFS_PROGRAM,
2423 			    nfsvers_to_use, nconfp, nfs_proto, port, &tinfo,
2424 			    &args->fh, get_pubfh, fspath, &addr_error);
2425 		}
2426 	} else {
2427 		args->addr = get_addr(fshost, NFS_PROGRAM, nfsvers_to_use,
2428 		    nconfp, nfs_proto, port, &tinfo, &args->fh, get_pubfh,
2429 		    fspath, &addr_error);
2430 		/*
2431 		 * If no proto is specified set this flag.
2432 		 * Kernel mount code will try to use RDMA if its on the
2433 		 * system, otherwise it will keep on using the protocol
2434 		 * selected here, through the above get_addr call.
2435 		 */
2436 		if (nfs_proto == NULL)
2437 			args->flags |= NFSMNT_TRYRDMA;
2438 	}
2439 
2440 	if (args->addr == NULL) {
2441 		/*
2442 		 * We could have failed because the server had no public
2443 		 * file handle support. So don't print a message and don't
2444 		 * retry.
2445 		 */
2446 		if (get_pubfh == TRUE)
2447 			return (RET_ERR);
2448 
2449 		if (!printed) {
2450 			switch (addr_error.error_type) {
2451 			case 0:
2452 				printed = 1;
2453 				break;
2454 			case ERR_RPCERROR:
2455 				if (!print_rpcerror)
2456 					/* no error print at this time */
2457 					break;
2458 				pr_err(gettext("%s NFS service not"
2459 				    " available %s\n"), fshost,
2460 				    clnt_sperrno(addr_error.error_value));
2461 				printed = 1;
2462 				break;
2463 			case ERR_NETPATH:
2464 				pr_err(gettext("%s: Error in NETPATH.\n"),
2465 				    fshost);
2466 				printed = 1;
2467 				break;
2468 			case ERR_PROTO_INVALID:
2469 				pr_err(gettext("%s: NFS service does not"
2470 				    " recognize protocol: %s.\n"), fshost,
2471 				    nfs_proto);
2472 				printed = 1;
2473 				break;
2474 			case ERR_PROTO_UNSUPP:
2475 				if (nfsvers || nfsvers_to_use == NFS_VERSMIN) {
2476 					/*
2477 					 * Don't set "printed" here. Since we
2478 					 * have to keep checking here till we
2479 					 * exhaust transport errors on all vers.
2480 					 *
2481 					 * Print this message if:
2482 					 * 1. After we have tried all versions
2483 					 *    of NFS and none support the asked
2484 					 *    transport.
2485 					 *
2486 					 * 2. If a version is specified and it
2487 					 *    does'nt support the asked
2488 					 *    transport.
2489 					 *
2490 					 * Otherwise we decrement the version
2491 					 * and retry below.
2492 					 */
2493 					pr_err(gettext("%s: NFS service does"
2494 					    " not support protocol: %s.\n"),
2495 					    fshost, nfs_proto);
2496 				}
2497 				break;
2498 			case ERR_NOHOST:
2499 				pr_err("%s: %s\n", fshost, "Unknown host");
2500 				printed = 1;
2501 				break;
2502 			default:
2503 				/* case ERR_PROTO_NONE falls through */
2504 				pr_err(gettext("%s: NFS service not responding"
2505 				    "\n"), fshost);
2506 				printed = 1;
2507 				break;
2508 			}
2509 		}
2510 		SET_ERR_RET(error,
2511 		    addr_error.error_type, addr_error.error_value);
2512 		if (addr_error.error_type == ERR_PROTO_NONE)
2513 			return (RET_RETRY);
2514 		else if (addr_error.error_type == ERR_RPCERROR &&
2515 		    !IS_UNRECOVERABLE_RPC(addr_error.error_value)) {
2516 			return (RET_RETRY);
2517 		} else if (nfsvers == 0 && addr_error.error_type ==
2518 		    ERR_PROTO_UNSUPP && nfsvers_to_use != NFS_VERSMIN) {
2519 			/*
2520 			 * If no version is specified, and the error is due
2521 			 * to an unsupported transport, then decrement the
2522 			 * version and retry.
2523 			 */
2524 			return (RET_RETRY);
2525 		} else
2526 			return (RET_ERR);
2527 	}
2528 	nconf = *nconfp;
2529 
2530 	if (stat(nconf->nc_device, &sb) < 0) {
2531 		pr_err(gettext("getaddr_nfs: couldn't stat: %s: %s\n"),
2532 		    nconf->nc_device, strerror(errno));
2533 		return (RET_ERR);
2534 	}
2535 
2536 	knconfp = (struct knetconfig *)malloc(sizeof (*knconfp));
2537 	if (!knconfp) {
2538 		pr_err(gettext("no memory\n"));
2539 		return (RET_ERR);
2540 	}
2541 	knconfp->knc_semantics = nconf->nc_semantics;
2542 	knconfp->knc_protofmly = nconf->nc_protofmly;
2543 	knconfp->knc_proto = nconf->nc_proto;
2544 	knconfp->knc_rdev = sb.st_rdev;
2545 
2546 	/* make sure we don't overload the transport */
2547 	if (tinfo.tsdu > 0 && tinfo.tsdu < NFS_MAXDATA + NFS_RPC_HDR) {
2548 		args->flags |= (NFSMNT_RSIZE | NFSMNT_WSIZE);
2549 		if (args->rsize == 0 || args->rsize > tinfo.tsdu - NFS_RPC_HDR)
2550 			args->rsize = tinfo.tsdu - NFS_RPC_HDR;
2551 		if (args->wsize == 0 || args->wsize > tinfo.tsdu - NFS_RPC_HDR)
2552 			args->wsize = tinfo.tsdu - NFS_RPC_HDR;
2553 	}
2554 
2555 	args->flags |= NFSMNT_KNCONF;
2556 	args->knconf = knconfp;
2557 	return (RET_OK);
2558 }
2559 
2560 static int
2561 retry(struct mnttab *mntp, int ro)
2562 {
2563 	int delay = 5;
2564 	int count = retries;
2565 	int r;
2566 
2567 	/*
2568 	 * Please see comments on nfsretry_vers in the beginning of this file
2569 	 * and in main() routine.
2570 	 */
2571 
2572 	if (bg) {
2573 		if (fork() > 0)
2574 			return (RET_OK);
2575 		backgrounded = 1;
2576 		pr_err(gettext("backgrounding: %s\n"), mntp->mnt_mountp);
2577 	} else {
2578 		if (!nfsretry_vers)
2579 			pr_err(gettext("retrying: %s\n"), mntp->mnt_mountp);
2580 	}
2581 
2582 	while (count--) {
2583 		if ((r = mount_nfs(mntp, ro, NULL)) == RET_OK) {
2584 			pr_err(gettext("%s: mounted OK\n"), mntp->mnt_mountp);
2585 			return (RET_OK);
2586 		}
2587 		if (r != RET_RETRY)
2588 			break;
2589 
2590 		if (count > 0) {
2591 			(void) sleep(delay);
2592 			delay *= 2;
2593 			if (delay > 120)
2594 				delay = 120;
2595 		}
2596 	}
2597 
2598 	if (!nfsretry_vers)
2599 		pr_err(gettext("giving up on: %s\n"), mntp->mnt_mountp);
2600 
2601 	return (RET_ERR);
2602 }
2603 
2604 /*
2605  * Read the /etc/default/nfs configuration file to determine if the
2606  * client has been configured for a new min/max for the NFS version to
2607  * use.
2608  */
2609 static void
2610 read_default(void)
2611 {
2612 	char *defval;
2613 	int errno;
2614 	int tmp;
2615 
2616 	/* Fail silently if error in opening the default nfs config file */
2617 	if ((defopen(NFSADMIN)) == 0) {
2618 		if ((defval = defread("NFS_CLIENT_VERSMIN=")) != NULL) {
2619 			errno = 0;
2620 			tmp = strtol(defval, (char **)NULL, 10);
2621 			if (errno == 0) {
2622 				vers_min_default = tmp;
2623 			}
2624 		}
2625 		if ((defval = defread("NFS_CLIENT_VERSMAX=")) != NULL) {
2626 			errno = 0;
2627 			tmp = strtol(defval, (char **)NULL, 10);
2628 			if (errno == 0) {
2629 				vers_max_default = tmp;
2630 			}
2631 		}
2632 		/* close defaults file */
2633 		defopen(NULL);
2634 	}
2635 }
2636 
2637 static void
2638 sigusr1(int s)
2639 {
2640 }
2641