xref: /onnv-gate/usr/src/stand/lib/fs/nfs/mount.c (revision 1288:317cceffd26d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1230Sss146032  * Common Development and Distribution License (the "License").
6*1230Sss146032  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*1230Sss146032 
220Sstevel@tonic-gate /*
23*1230Sss146032  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /*	All Rights Reserved */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/utsname.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <netinet/in.h>
360Sstevel@tonic-gate #include <rpc/types.h>
370Sstevel@tonic-gate #include <rpc/auth.h>
380Sstevel@tonic-gate #include <sys/t_lock.h>
390Sstevel@tonic-gate #include <netdb.h>
400Sstevel@tonic-gate #include "clnt.h"
410Sstevel@tonic-gate #include <rpc/xdr.h>
420Sstevel@tonic-gate #include <rpc/rpc_msg.h>
430Sstevel@tonic-gate #include <rpc/rpc.h>
440Sstevel@tonic-gate #include "brpc.h"
450Sstevel@tonic-gate #include "auth_inet.h"
460Sstevel@tonic-gate #include "pmap.h"
470Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
480Sstevel@tonic-gate #include <rpcsvc/nfs4_prot.h>
490Sstevel@tonic-gate #include "nfs_inet.h"
500Sstevel@tonic-gate #include <rpcsvc/bootparam.h>
510Sstevel@tonic-gate #include <dhcp_impl.h>
520Sstevel@tonic-gate #include <rpcsvc/mount.h>
530Sstevel@tonic-gate #include <sys/promif.h>
540Sstevel@tonic-gate #include <sys/salib.h>
550Sstevel@tonic-gate #include "socket_inet.h"
560Sstevel@tonic-gate #include "ipv4.h"
570Sstevel@tonic-gate #include "mac.h"
580Sstevel@tonic-gate #include <sys/bootdebug.h>
590Sstevel@tonic-gate #include <errno.h>
600Sstevel@tonic-gate #include "dhcpv4.h"
610Sstevel@tonic-gate #include <sys/mntent.h>
620Sstevel@tonic-gate 
63*1230Sss146032 /* ARP timeout in milliseconds for BOOTP/RARP */
64*1230Sss146032 #define	ARP_INETBOOT_TIMEOUT 1000
65*1230Sss146032 
660Sstevel@tonic-gate struct nfs_file		roothandle;			/* root file handle */
670Sstevel@tonic-gate static char		root_hostname[SYS_NMLN];	/* server hostname */
680Sstevel@tonic-gate static char		my_hostname[MAXHOSTNAMELEN];
690Sstevel@tonic-gate static char		root_pathbuf[NFS_MAXPATHLEN];	/* the root's path */
700Sstevel@tonic-gate static char		root_boot_file[NFS_MAXPATHLEN];	/* optional boot file */
710Sstevel@tonic-gate static struct sockaddr_in root_to;			/* server sock ip */
720Sstevel@tonic-gate 							/* in network order */
730Sstevel@tonic-gate CLIENT			*root_CLIENT = NULL;		/* CLIENT handle */
740Sstevel@tonic-gate int dontroute = FALSE;	/* In case rarp/bootparams was selected */
750Sstevel@tonic-gate char			rootopts[MAX_PATH_LEN];
760Sstevel@tonic-gate static gid_t		fake_gids = 1;	/* fake gids list for auth_unix */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate extern void set_default_filename(char *);	/* boot.c */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * xdr routines used by mount.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate bool_t
xdr_fhstatus(XDR * xdrs,struct fhstatus * fhsp)850Sstevel@tonic-gate xdr_fhstatus(XDR *xdrs, struct fhstatus *fhsp)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	if (!xdr_int(xdrs, (int *)&fhsp->fhs_status))
880Sstevel@tonic-gate 		return (FALSE);
890Sstevel@tonic-gate 	if (fhsp->fhs_status == 0) {
900Sstevel@tonic-gate 		return (xdr_fhandle(xdrs, fhsp->fhstatus_u.fhs_fhandle));
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 	return (TRUE);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate bool_t
xdr_fhandle(XDR * xdrs,fhandle fhp)960Sstevel@tonic-gate xdr_fhandle(XDR *xdrs, fhandle fhp)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	return (xdr_opaque(xdrs, (char *)fhp, NFS_FHSIZE));
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate bool_t
xdr_path(XDR * xdrs,char ** pathp)1020Sstevel@tonic-gate xdr_path(XDR *xdrs, char **pathp)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	return (xdr_string(xdrs, pathp, MNTPATHLEN));
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate bool_t
xdr_fhandle3(XDR * xdrs,fhandle3 * objp)1080Sstevel@tonic-gate xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	return (xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
1110Sstevel@tonic-gate 				(uint_t *)&objp->fhandle3_len, FHSIZE3));
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate bool_t
xdr_mountstat3(XDR * xdrs,mountstat3 * objp)1150Sstevel@tonic-gate xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	return (xdr_enum(xdrs, (enum_t *)objp));
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate bool_t
xdr_mountres3_ok(XDR * xdrs,mountres3_ok * objp)1210Sstevel@tonic-gate xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	if (!xdr_fhandle3(xdrs, &objp->fhandle))
1240Sstevel@tonic-gate 		return (FALSE);
1250Sstevel@tonic-gate 	return (xdr_array(xdrs, (char **)&objp->auth_flavors.auth_flavors_val,
1260Sstevel@tonic-gate 			(uint_t *)&objp->auth_flavors.auth_flavors_len, ~0,
1270Sstevel@tonic-gate 			sizeof (int), (xdrproc_t)xdr_int));
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate bool_t
xdr_mountres3(XDR * xdrs,mountres3 * objp)1310Sstevel@tonic-gate xdr_mountres3(XDR *xdrs, mountres3 *objp)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	if (!xdr_mountstat3(xdrs, &objp->fhs_status))
1340Sstevel@tonic-gate 		return (FALSE);
1350Sstevel@tonic-gate 	if (objp->fhs_status == MNT_OK)
1360Sstevel@tonic-gate 		return (xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo));
1370Sstevel@tonic-gate 	return (TRUE);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static int
nfsmountroot(char * path,struct nfs_file * filep)1410Sstevel@tonic-gate nfsmountroot(char *path, struct nfs_file *filep)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	int		rexmit;
1440Sstevel@tonic-gate 	int		resp_wait;
1450Sstevel@tonic-gate 	enum clnt_stat	status;
1460Sstevel@tonic-gate 	struct fhstatus	root_tmp;			/* to pass to rpc/xdr */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * Wait up to 16 secs for first response, retransmitting expon.
1500Sstevel@tonic-gate 	 */
1510Sstevel@tonic-gate 	rexmit = 0;	/* default retransmission interval */
1520Sstevel@tonic-gate 	resp_wait = 16;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	do {
1550Sstevel@tonic-gate 		status = brpc_call((rpcprog_t)MOUNTPROG, (rpcvers_t)MOUNTVERS,
1560Sstevel@tonic-gate 		    (rpcproc_t)MOUNTPROC_MNT, xdr_path, (caddr_t)&path,
1570Sstevel@tonic-gate 		    xdr_fhstatus, (caddr_t)&(root_tmp), rexmit, resp_wait,
1580Sstevel@tonic-gate 		    &root_to, NULL, AUTH_UNIX);
1590Sstevel@tonic-gate 		if (status == RPC_TIMEDOUT) {
1600Sstevel@tonic-gate 			dprintf("boot: %s:%s mount server not responding.\n",
1610Sstevel@tonic-gate 			    root_hostname, path);
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 		rexmit = resp_wait;
1640Sstevel@tonic-gate 		resp_wait = 0;	/* use default wait time. */
1650Sstevel@tonic-gate 	} while (status == RPC_TIMEDOUT);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if ((status != RPC_SUCCESS) || (root_tmp.fhs_status != 0)) {
1680Sstevel@tonic-gate 		nfs_error(root_tmp.fhs_status);
1690Sstevel@tonic-gate 		root_to.sin_port = 0;
1700Sstevel@tonic-gate 		return (-1);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * Since the mount succeeded, we'll mark the filep's
1750Sstevel@tonic-gate 	 * status as NFS_OK, and its type as NFDIR. If these
1760Sstevel@tonic-gate 	 * points aren't the case, then we wouldn't be here.
1770Sstevel@tonic-gate 	 */
1780Sstevel@tonic-gate 	bcopy(&root_tmp.fhstatus_u.fhs_fhandle, &filep->fh.fh2, FHSIZE);
1790Sstevel@tonic-gate 	filep->ftype.type2 = NFDIR;
1800Sstevel@tonic-gate 	filep->version = NFS_VERSION;
1810Sstevel@tonic-gate 	nfs_readsize = nfs_readsize <  NFS_MAXDATA ? nfs_readsize : NFS_MAXDATA;
1820Sstevel@tonic-gate 	/*
1830Sstevel@tonic-gate 	 * Set a reasonable lower limit on readsize
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
1860Sstevel@tonic-gate 							512 : nfs_readsize;
1870Sstevel@tonic-gate 	return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate int
setup_root_vars(void)1910Sstevel@tonic-gate setup_root_vars(void)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	size_t		buflen;
1940Sstevel@tonic-gate 	uint16_t	readsize;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * Root server name. Required.
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	buflen = sizeof (root_hostname);
2000Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTSRVR_NAME, 0,
2010Sstevel@tonic-gate 	    root_hostname, &buflen)) {
2020Sstevel@tonic-gate 		root_hostname[buflen] = '\0';
2030Sstevel@tonic-gate 	} else {
2040Sstevel@tonic-gate 		dprintf("BOUND: Missing Root Server Name Option\n");
2050Sstevel@tonic-gate 		errno = EINVAL;
2060Sstevel@tonic-gate 		return (-1);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * Root server IP. Required.
2110Sstevel@tonic-gate 	 */
2120Sstevel@tonic-gate 	buflen = sizeof (root_to.sin_addr);
2130Sstevel@tonic-gate 	if (!dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTSRVR_IP, 0,
2140Sstevel@tonic-gate 	    &root_to.sin_addr, &buflen)) {
2150Sstevel@tonic-gate 		dprintf("BOUND: Missing Root Server IP Option\n");
2160Sstevel@tonic-gate 		errno = EINVAL;
2170Sstevel@tonic-gate 		return (-1);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/*
2210Sstevel@tonic-gate 	 * Root path Required.
2220Sstevel@tonic-gate 	 */
2230Sstevel@tonic-gate 	buflen = sizeof (root_pathbuf);
2240Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTPATH, 0,
2250Sstevel@tonic-gate 	    root_pathbuf, &buflen)) {
2260Sstevel@tonic-gate 		root_pathbuf[buflen] = '\0';
2270Sstevel@tonic-gate 	} else {
2280Sstevel@tonic-gate 		dprintf("BOUND: Missing Root Path Option\n");
2290Sstevel@tonic-gate 		errno = EINVAL;
2300Sstevel@tonic-gate 		return (-1);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * Optional Bootfile path.
2350Sstevel@tonic-gate 	 */
2360Sstevel@tonic-gate 	buflen = sizeof (root_boot_file);
2370Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_BOOTFILE, 0,
2380Sstevel@tonic-gate 		    root_boot_file, &buflen)) {
2390Sstevel@tonic-gate 		root_boot_file[buflen] = '\0';
2400Sstevel@tonic-gate 		dprintf("BOUND: Optional Boot File is: %s\n", root_boot_file);
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	/* if we got a boot file name, use it as the default */
2440Sstevel@tonic-gate 	if (root_boot_file[0] != '\0')
2450Sstevel@tonic-gate 		set_default_filename(root_boot_file);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	/*
2480Sstevel@tonic-gate 	 * Set the NFS read size. The mount code will adjust it to
2490Sstevel@tonic-gate 	 * the maximum size.
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 	buflen = sizeof (readsize);
2520Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_BOOT_NFS_READSIZE, 0,
2530Sstevel@tonic-gate 	    &readsize, &buflen)) {
2540Sstevel@tonic-gate 		nfs_readsize = ntohs(readsize);
2550Sstevel@tonic-gate 		if (boothowto & RB_VERBOSE) {
2560Sstevel@tonic-gate 			printf("Boot NFS read size: %d\n", nfs_readsize);
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * Optional rootopts.
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 	buflen = sizeof (rootopts);
2640Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTOPTS, 0,
2650Sstevel@tonic-gate 	    rootopts, &buflen)) {
2660Sstevel@tonic-gate 		rootopts[buflen] = '\0';
2670Sstevel@tonic-gate 		dprintf("BOUND: Optional Rootopts is: %s\n", rootopts);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	return (0);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate static void
mnt3_error(enum mountstat3 status)2740Sstevel@tonic-gate mnt3_error(enum mountstat3 status)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	if (!(boothowto & RB_DEBUG))
2770Sstevel@tonic-gate 		return;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	switch (status) {
2800Sstevel@tonic-gate 	case MNT_OK:
2810Sstevel@tonic-gate 		printf("Mount: No error.\n");
2820Sstevel@tonic-gate 		break;
2830Sstevel@tonic-gate 	case MNT3ERR_PERM:
2840Sstevel@tonic-gate 		printf("Mount: Not owner.\n");
2850Sstevel@tonic-gate 		break;
2860Sstevel@tonic-gate 	case MNT3ERR_NOENT:
2870Sstevel@tonic-gate 		printf("Mount: No such file or directory.\n");
2880Sstevel@tonic-gate 		break;
2890Sstevel@tonic-gate 	case MNT3ERR_IO:
2900Sstevel@tonic-gate 		printf("Mount: I/O error.\n");
2910Sstevel@tonic-gate 		break;
2920Sstevel@tonic-gate 	case MNT3ERR_ACCES:
2930Sstevel@tonic-gate 		printf("Mount: Permission denied.\n");
2940Sstevel@tonic-gate 		break;
2950Sstevel@tonic-gate 	case MNT3ERR_NOTDIR:
2960Sstevel@tonic-gate 		printf("Mount: Not a directory.\n");
2970Sstevel@tonic-gate 		break;
2980Sstevel@tonic-gate 	case MNT3ERR_INVAL:
2990Sstevel@tonic-gate 		printf("Mount: Invalid argument.\n");
3000Sstevel@tonic-gate 		break;
3010Sstevel@tonic-gate 	case MNT3ERR_NAMETOOLONG:
3020Sstevel@tonic-gate 		printf("Mount: File name too long.\n");
3030Sstevel@tonic-gate 		break;
3040Sstevel@tonic-gate 	case MNT3ERR_NOTSUPP:
3050Sstevel@tonic-gate 		printf("Mount: Operation not supported.\n");
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 	case MNT3ERR_SERVERFAULT:
3080Sstevel@tonic-gate 		printf("Mount: Server fault.\n");
3090Sstevel@tonic-gate 		break;
3100Sstevel@tonic-gate 	default:
3110Sstevel@tonic-gate 		printf("Mount: unknown error.\n");
3120Sstevel@tonic-gate 		break;
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static int
nfs3mountroot(char * path,struct nfs_file * filep)3170Sstevel@tonic-gate nfs3mountroot(char *path, struct nfs_file *filep)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	int		rexmit;
3200Sstevel@tonic-gate 	int		resp_wait;
3210Sstevel@tonic-gate 	struct mountres3 res3;
3220Sstevel@tonic-gate 	enum clnt_stat	status;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	/*
3250Sstevel@tonic-gate 	 * Wait up to 16 secs for first response, retransmitting expon.
3260Sstevel@tonic-gate 	 */
3270Sstevel@tonic-gate 	rexmit = 0;	/* default retransmission interval */
3280Sstevel@tonic-gate 	resp_wait = 16;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * Try to mount using V3
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	do {
3340Sstevel@tonic-gate 		bzero(&res3, sizeof (struct mountres3));
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 		status = brpc_call((rpcprog_t)MOUNTPROG, (rpcvers_t)MOUNTVERS3,
3370Sstevel@tonic-gate 		    (rpcproc_t)MOUNTPROC_MNT, xdr_path, (caddr_t)&path,
3380Sstevel@tonic-gate 		    xdr_mountres3, (caddr_t)&res3, rexmit, resp_wait,
3390Sstevel@tonic-gate 		    &root_to, NULL, AUTH_UNIX);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		if (status != RPC_TIMEDOUT)
3420Sstevel@tonic-gate 			break;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		dprintf("boot: %s:%s mount server not responding.\n",
3450Sstevel@tonic-gate 			    root_hostname, path);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		rexmit = resp_wait;
3480Sstevel@tonic-gate 		resp_wait = 0;	/* use default wait time. */
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		xdr_free(xdr_mountres3, (caddr_t)&res3);
3510Sstevel@tonic-gate 	} while (status == RPC_TIMEDOUT);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if ((status != RPC_SUCCESS) || (res3.fhs_status != MNT_OK)) {
3540Sstevel@tonic-gate 		mnt3_error(res3.fhs_status);
3550Sstevel@tonic-gate 		root_to.sin_port = 0;
3560Sstevel@tonic-gate 		return (-1);
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * Since the mount succeeded, we'll mark the filep's
3610Sstevel@tonic-gate 	 * status as NFS_OK, and its type as NF3DIR. If these
3620Sstevel@tonic-gate 	 * points aren't the case, then we wouldn't be here.
3630Sstevel@tonic-gate 	 */
3640Sstevel@tonic-gate 	filep->fh.fh3.len = res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
3650Sstevel@tonic-gate 	bcopy(res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
3660Sstevel@tonic-gate 			filep->fh.fh3.data,
3670Sstevel@tonic-gate 			filep->fh.fh3.len);
3680Sstevel@tonic-gate 	filep->ftype.type3 = NF3DIR;
3690Sstevel@tonic-gate 	filep->version = NFS_V3;
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * Hardwire in a known reasonable upper limit of 32K
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	nfs_readsize = nfs_readsize <  32 * 1024 ? nfs_readsize : 32 * 1024;
3740Sstevel@tonic-gate 	/*
3750Sstevel@tonic-gate 	 * Set a reasonable lower limit on readsize
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
3780Sstevel@tonic-gate 							512 : nfs_readsize;
3790Sstevel@tonic-gate 	xdr_free(xdr_mountres3, (caddr_t)&res3);
3800Sstevel@tonic-gate 	return (0);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate  * Setup v4 client for inetboot
3850Sstevel@tonic-gate  */
3860Sstevel@tonic-gate static int
nfs4init(char * path,uint16_t nfs_port)3870Sstevel@tonic-gate nfs4init(char *path, uint16_t nfs_port)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	struct timeval	wait;
3900Sstevel@tonic-gate 	int		fd = -1;
3910Sstevel@tonic-gate 	int		error = 0;
3920Sstevel@tonic-gate 	enum clnt_stat	rpc_stat;
3930Sstevel@tonic-gate 	struct nfs_file	rootpath;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	wait.tv_sec = RPC_RCVWAIT_MSEC / 1000;
3960Sstevel@tonic-gate 	wait.tv_usec = 0;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * If we haven't explicitly set the port number, set to the standard
4000Sstevel@tonic-gate 	 * 2049 and don't cause a rpcbind request.
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	if (nfs_port == 0)
4030Sstevel@tonic-gate 		nfs_port = 2049;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	root_to.sin_port = htons(nfs_port);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * Support TCP only
4090Sstevel@tonic-gate 	 */
4100Sstevel@tonic-gate 	root_CLIENT = clntbtcp_create(&root_to, NFS_PROGRAM,
4110Sstevel@tonic-gate 					NFS_V4, wait, &fd,
4120Sstevel@tonic-gate 					NFS4BUF_SIZE, NFS4BUF_SIZE);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	if (root_CLIENT == NULL) {
4150Sstevel@tonic-gate 		root_to.sin_port = 0;
4160Sstevel@tonic-gate 		return (-1);
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	root_CLIENT->cl_auth =
4200Sstevel@tonic-gate 			authunix_create(my_hostname, 0, 1, 1, &fake_gids);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * Send NULL proc the server first to see if V4 exists
4240Sstevel@tonic-gate 	 */
4250Sstevel@tonic-gate 	rpc_stat = CLNT_CALL(root_CLIENT, NFSPROC4_NULL, xdr_void, NULL,
4260Sstevel@tonic-gate 				xdr_void, NULL, wait);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
4290Sstevel@tonic-gate 		dprintf("boot: NULL proc failed NFSv4 service not available\n");
4300Sstevel@tonic-gate 		AUTH_DESTROY(root_CLIENT->cl_auth);
4310Sstevel@tonic-gate 		CLNT_DESTROY(root_CLIENT);
4320Sstevel@tonic-gate 		root_to.sin_port = 0;
4330Sstevel@tonic-gate 		return (-1);
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	/*
4370Sstevel@tonic-gate 	 * Do a lookup to get to the root_path.  This is nice since it can
4380Sstevel@tonic-gate 	 * handle multicomponent lookups.
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	roothandle.version = NFS_V4;
4410Sstevel@tonic-gate 	roothandle.ftype.type4 = NF4DIR;
4420Sstevel@tonic-gate 	roothandle.fh.fh4.len = 0;		/* Force a PUTROOTFH */
4430Sstevel@tonic-gate 	roothandle.offset = (uint_t)0;		/* it's a directory! */
4440Sstevel@tonic-gate 	error = lookup(path, &rootpath, TRUE);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (error) {
4470Sstevel@tonic-gate 		printf("boot: lookup %s failed\n", path);
4480Sstevel@tonic-gate 		return (-1);
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 	roothandle = rootpath;	/* structure copy */
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	/*
4530Sstevel@tonic-gate 	 * Hardwire in a known reasonable upper limit of 32K
4540Sstevel@tonic-gate 	 */
4550Sstevel@tonic-gate 	nfs_readsize = nfs_readsize <  32 * 1024 ? nfs_readsize : 32 * 1024;
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * Set a reasonable lower limit on readsize
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
4600Sstevel@tonic-gate 							512 : nfs_readsize;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	return (0);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate static int
atoi(const char * p)4660Sstevel@tonic-gate atoi(const char *p)
4670Sstevel@tonic-gate {
4680Sstevel@tonic-gate 	int n;
4690Sstevel@tonic-gate 	int c, neg = 0;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (!isdigit(c = *p)) {
4720Sstevel@tonic-gate 		while (c == ' ' || c == '\t' || c == '\n')
4730Sstevel@tonic-gate 			c = *++p;
4740Sstevel@tonic-gate 		switch (c) {
4750Sstevel@tonic-gate 		case '-':
4760Sstevel@tonic-gate 			neg++;
4770Sstevel@tonic-gate 			/* FALLTHROUGH */
4780Sstevel@tonic-gate 		case '+':
4790Sstevel@tonic-gate 			c = *++p;
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 		if (!isdigit(c))
4820Sstevel@tonic-gate 			return (0);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 	for (n = '0' - c; isdigit(c = *++p); ) {
4850Sstevel@tonic-gate 		n *= 10; /* two steps to avoid unnecessary overflow */
4860Sstevel@tonic-gate 		n += '0' - c; /* accum neg to avoid surprises at MAX */
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	return (neg ? n : -n);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate /*
4920Sstevel@tonic-gate  * Parse suboptions from a string.
4930Sstevel@tonic-gate  * Same as getsubopt(3C).
4940Sstevel@tonic-gate  */
4950Sstevel@tonic-gate static int
getsubopt(char ** optionsp,char * const * tokens,char ** valuep)4960Sstevel@tonic-gate getsubopt(char **optionsp, char * const *tokens, char **valuep)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	char *s = *optionsp, *p;
4990Sstevel@tonic-gate 	int i;
5000Sstevel@tonic-gate 	size_t optlen;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	*valuep = NULL;
5030Sstevel@tonic-gate 	if (*s == '\0')
5040Sstevel@tonic-gate 		return (-1);
5050Sstevel@tonic-gate 	p = strchr(s, ',');		/* find next option */
5060Sstevel@tonic-gate 	if (p == NULL) {
5070Sstevel@tonic-gate 		p = s + strlen(s);
5080Sstevel@tonic-gate 	} else {
5090Sstevel@tonic-gate 		*p++ = '\0';		/* mark end and point to next */
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 	*optionsp = p;			/* point to next option */
5120Sstevel@tonic-gate 	p = strchr(s, '=');		/* find value */
5130Sstevel@tonic-gate 	if (p == NULL) {
5140Sstevel@tonic-gate 		optlen = strlen(s);
5150Sstevel@tonic-gate 		*valuep = NULL;
5160Sstevel@tonic-gate 	} else {
5170Sstevel@tonic-gate 		optlen = p - s;
5180Sstevel@tonic-gate 		*valuep = ++p;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	for (i = 0; tokens[i] != NULL; i++) {
5210Sstevel@tonic-gate 		if ((optlen == strlen(tokens[i])) &&
5220Sstevel@tonic-gate 		    (strncmp(s, tokens[i], optlen) == 0))
5230Sstevel@tonic-gate 			return (i);
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 	/* no match, point value at option and return error */
5260Sstevel@tonic-gate 	*valuep = s;
5270Sstevel@tonic-gate 	return (-1);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate  * The only interesting NFS mount options for initiating the kernel
5320Sstevel@tonic-gate  * all others are ignored.
5330Sstevel@tonic-gate  */
5340Sstevel@tonic-gate static char *optlist[] = {
5350Sstevel@tonic-gate #define	OPT_RSIZE	0
5360Sstevel@tonic-gate 	MNTOPT_RSIZE,
5370Sstevel@tonic-gate #define	OPT_TIMEO	1
5380Sstevel@tonic-gate 	MNTOPT_TIMEO,
5390Sstevel@tonic-gate #define	OPT_VERS	2
5400Sstevel@tonic-gate 	MNTOPT_VERS,
5410Sstevel@tonic-gate #define	OPT_PROTO	3
5420Sstevel@tonic-gate 	MNTOPT_PROTO,
5430Sstevel@tonic-gate #define	OPT_PORT	4
5440Sstevel@tonic-gate 	MNTOPT_PORT,
5450Sstevel@tonic-gate 	NULL
5460Sstevel@tonic-gate };
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate /*
5490Sstevel@tonic-gate  * This routine will open a device as it is known by the V2 OBP. It
5500Sstevel@tonic-gate  * then goes thru the stuff necessary to initialize the network device,
5510Sstevel@tonic-gate  * get our network parameters, (using DHCP or rarp/bootparams), and
5520Sstevel@tonic-gate  * finally actually go and get the root filehandle. Sound like fun?
5530Sstevel@tonic-gate  * Suuurrrree. Take a look.
5540Sstevel@tonic-gate  *
5550Sstevel@tonic-gate  * Returns 0 if things worked. -1 if we crashed and burned.
5560Sstevel@tonic-gate  */
5570Sstevel@tonic-gate int
boot_nfs_mountroot(char * str)5580Sstevel@tonic-gate boot_nfs_mountroot(char *str)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate 	int		status;
5610Sstevel@tonic-gate 	enum clnt_stat	rpc_stat;
5620Sstevel@tonic-gate 	char		*root_path = &root_pathbuf[0];	/* to make XDR happy */
5630Sstevel@tonic-gate 	struct timeval	wait;
5640Sstevel@tonic-gate 	int		fd;
5650Sstevel@tonic-gate 	int		bufsize;
5660Sstevel@tonic-gate 	char		*opts, *val;
5670Sstevel@tonic-gate 	int		nfs_version = 0;
5680Sstevel@tonic-gate 	int		istcp = 1;
5690Sstevel@tonic-gate 	int		nfs_port = 0;	/* Cause pmap to get port */
5700Sstevel@tonic-gate 	struct sockaddr_in tmp_addr;	/* throw away */
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (root_CLIENT != NULL) {
5730Sstevel@tonic-gate 		AUTH_DESTROY(root_CLIENT->cl_auth);
5740Sstevel@tonic-gate 		CLNT_DESTROY(root_CLIENT);
5750Sstevel@tonic-gate 		root_CLIENT = NULL;
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	root_to.sin_family = AF_INET;
5790Sstevel@tonic-gate 	root_to.sin_addr.s_addr = htonl(INADDR_ANY);
5800Sstevel@tonic-gate 	root_to.sin_port = htons(0);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	mac_init(str);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	(void) ipv4_setpromiscuous(TRUE);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (get_netconfig_strategy() == NCT_BOOTP_DHCP) {
5870Sstevel@tonic-gate 		if (boothowto & RB_VERBOSE)
5880Sstevel@tonic-gate 			printf("Using BOOTP/DHCP...\n");
5890Sstevel@tonic-gate 		if (dhcp() != 0 || setup_root_vars() != 0) {
5900Sstevel@tonic-gate 			(void) ipv4_setpromiscuous(FALSE);
5910Sstevel@tonic-gate 			if (boothowto & RB_VERBOSE)
5920Sstevel@tonic-gate 				printf("BOOTP/DHCP configuration failed!\n");
5930Sstevel@tonic-gate 			return (-1);
5940Sstevel@tonic-gate 		}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		/* now that we have an IP address, turn off promiscuous mode */
5970Sstevel@tonic-gate 		(void) ipv4_setpromiscuous(FALSE);
5980Sstevel@tonic-gate 	} else {
5990Sstevel@tonic-gate 		/* Use RARP/BOOTPARAMS. RARP will try forever... */
6000Sstevel@tonic-gate 		if (boothowto & RB_VERBOSE)
6010Sstevel@tonic-gate 			printf("Using RARP/BOOTPARAMS...\n");
6020Sstevel@tonic-gate 		mac_call_rarp();
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		/*
6050Sstevel@tonic-gate 		 * Since there is no way to determine our netmask, and therefore
6060Sstevel@tonic-gate 		 * figure out if the router we got is useful, we assume all
6070Sstevel@tonic-gate 		 * services are local. Use DHCP if this bothers you.
6080Sstevel@tonic-gate 		 */
6090Sstevel@tonic-gate 		dontroute = TRUE;
610*1230Sss146032 		/*
611*1230Sss146032 		 * We are trying to keep the ARP response
612*1230Sss146032 		 * timeout on the lower side with BOOTP/RARP.
613*1230Sss146032 		 * We are doing this for BOOTP/RARP where policy
614*1230Sss146032 		 * doesn't allow to route the packets outside
615*1230Sss146032 		 * the subnet as it has no idea about the
616*1230Sss146032 		 * netmask. By doing so, we are reducing
617*1230Sss146032 		 * ARP response timeout for any packet destined
618*1230Sss146032 		 * for outside booting clients subnet. Client can
619*1230Sss146032 		 * not expect such ARP replies and will finally
620*1230Sss146032 		 * timeout after a long delay. This would cause
621*1230Sss146032 		 * booting client to get stalled for a longer
622*1230Sss146032 		 * time. We can not avoid accepting any outside
623*1230Sss146032 		 * subnet packets accidentally destined for the
624*1230Sss146032 		 * booting client.
625*1230Sss146032 		 */
626*1230Sss146032 		mac_set_arp_timeout(ARP_INETBOOT_TIMEOUT);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 		/* now that we have an IP address, turn off promiscuous mode */
6290Sstevel@tonic-gate 		(void) ipv4_setpromiscuous(FALSE);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		/* get our hostname */
6320Sstevel@tonic-gate 		if (whoami() == FALSE)
6330Sstevel@tonic-gate 			return (-1);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		/* get our bootparams. */
6360Sstevel@tonic-gate 		if (getfile("root", root_hostname, &root_to.sin_addr,
6370Sstevel@tonic-gate 		    root_pathbuf) == FALSE)
6380Sstevel@tonic-gate 			return (-1);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		/* get our rootopts. */
6410Sstevel@tonic-gate 		(void) getfile("rootopts", root_hostname, &tmp_addr.sin_addr,
6420Sstevel@tonic-gate 		    rootopts);
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	/* mount root */
6460Sstevel@tonic-gate 	if (boothowto & RB_VERBOSE) {
6470Sstevel@tonic-gate 		printf("root server: %s (%s)\n", root_hostname,
6480Sstevel@tonic-gate 		    inet_ntoa(root_to.sin_addr));
6490Sstevel@tonic-gate 		printf("root directory: %s\n", root_pathbuf);
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/*
6530Sstevel@tonic-gate 	 * Assumes we've configured the stack and thus know our
6540Sstevel@tonic-gate 	 * IP address/hostname, either by using DHCP or rarp/bootparams.
6550Sstevel@tonic-gate 	 */
6560Sstevel@tonic-gate 	gethostname(my_hostname, sizeof (my_hostname));
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	wait.tv_sec = RPC_RCVWAIT_MSEC / 1000;
6590Sstevel@tonic-gate 	wait.tv_usec = 0;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/*
6620Sstevel@tonic-gate 	 * Parse out the interesting root options, if an invalid
6630Sstevel@tonic-gate 	 * or unknown option is provided, silently ignore it and
6640Sstevel@tonic-gate 	 * use the defaults.
6650Sstevel@tonic-gate 	 */
6660Sstevel@tonic-gate 	opts = rootopts;
6670Sstevel@tonic-gate 	while (*opts) {
6680Sstevel@tonic-gate 		int ival;
6690Sstevel@tonic-gate 		switch (getsubopt(&opts, optlist, &val)) {
6700Sstevel@tonic-gate 		case OPT_RSIZE:
6710Sstevel@tonic-gate 			if (val == NULL || !isdigit(*val))
6720Sstevel@tonic-gate 				break;
6730Sstevel@tonic-gate 			nfs_readsize = atoi(val);
6740Sstevel@tonic-gate 			break;
6750Sstevel@tonic-gate 		case OPT_TIMEO:
6760Sstevel@tonic-gate 			if (val == NULL || !isdigit(*val))
6770Sstevel@tonic-gate 				break;
6780Sstevel@tonic-gate 			ival = atoi(val);
6790Sstevel@tonic-gate 			wait.tv_sec = ival / 10;
6800Sstevel@tonic-gate 			wait.tv_usec = (ival % 10) * 100000;
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 		case OPT_VERS:
6830Sstevel@tonic-gate 			if (val == NULL || !isdigit(*val))
6840Sstevel@tonic-gate 				break;
6850Sstevel@tonic-gate 			nfs_version = atoi(val);
6860Sstevel@tonic-gate 			break;
6870Sstevel@tonic-gate 		case OPT_PROTO:
6880Sstevel@tonic-gate 			if (val == NULL || isdigit(*val))
6890Sstevel@tonic-gate 				break;
6900Sstevel@tonic-gate 			if ((strncmp(val, "udp", 3) == 0))
6910Sstevel@tonic-gate 				istcp = 0;
6920Sstevel@tonic-gate 			else
6930Sstevel@tonic-gate 				istcp = 1;	/* must be tcp */
6940Sstevel@tonic-gate 			break;
6950Sstevel@tonic-gate 		case OPT_PORT:
6960Sstevel@tonic-gate 			if (val == NULL || !isdigit(*val))
6970Sstevel@tonic-gate 				break;
6980Sstevel@tonic-gate 			nfs_port = atoi(val);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 			/*
7010Sstevel@tonic-gate 			 * Currently nfs_dlinet.c doesn't support setting
7020Sstevel@tonic-gate 			 * the root NFS port. Delete this when it does.
7030Sstevel@tonic-gate 			 */
7040Sstevel@tonic-gate 			nfs_port = 0;
7050Sstevel@tonic-gate 			break;
7060Sstevel@tonic-gate 		default:
7070Sstevel@tonic-gate 			/*
7080Sstevel@tonic-gate 			 * Unknown options are silently ignored
7090Sstevel@tonic-gate 			 */
7100Sstevel@tonic-gate 			break;
7110Sstevel@tonic-gate 		}
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/*
7150Sstevel@tonic-gate 	 * If version is set, then try that version first.
7160Sstevel@tonic-gate 	 */
7170Sstevel@tonic-gate 	switch (nfs_version) {
7180Sstevel@tonic-gate 	case NFS_VERSION:
7190Sstevel@tonic-gate 		if (nfsmountroot(root_path, &roothandle) == 0)
7200Sstevel@tonic-gate 			goto domount;
7210Sstevel@tonic-gate 		break;
7220Sstevel@tonic-gate 	case NFS_V3:
7230Sstevel@tonic-gate 		if (nfs3mountroot(root_path, &roothandle) == 0)
7240Sstevel@tonic-gate 			goto domount;
7250Sstevel@tonic-gate 		break;
7260Sstevel@tonic-gate 	case NFS_V4:
7270Sstevel@tonic-gate 		/*
7280Sstevel@tonic-gate 		 * With v4 we skip the mount and go straight to
7290Sstevel@tonic-gate 		 * setting the root filehandle.  Because of this we
7300Sstevel@tonic-gate 		 * do things slightly differently and obtain our
7310Sstevel@tonic-gate 		 * client handle first.
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		if (istcp && nfs4init(root_path, nfs_port) == 0) {
7340Sstevel@tonic-gate 			/*
7350Sstevel@tonic-gate 			 * If v4 init succeeded then we are done.  Just return.
7360Sstevel@tonic-gate 			 */
7370Sstevel@tonic-gate 			return (0);
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	/*
7420Sstevel@tonic-gate 	 * If there was no chosen version or the chosen version failed
7430Sstevel@tonic-gate 	 * try all versions in order, this may still fail to boot
7440Sstevel@tonic-gate 	 * at the kernel level if the options are not right, but be
7450Sstevel@tonic-gate 	 * generous at this early stage.
7460Sstevel@tonic-gate 	 */
7470Sstevel@tonic-gate 	if (istcp && nfs4init(root_path, nfs_port) == 0) {
7480Sstevel@tonic-gate 		/*
7490Sstevel@tonic-gate 		 * If v4 init succeeded then we are done.  Just return.
7500Sstevel@tonic-gate 		 */
7510Sstevel@tonic-gate 		return (0);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (nfs3mountroot(root_path, &roothandle) == 0)
7550Sstevel@tonic-gate 		goto domount;
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if ((status = nfsmountroot(root_path, &roothandle)) != 0)
7580Sstevel@tonic-gate 		return (status);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate domount:
7610Sstevel@tonic-gate 	/*
7620Sstevel@tonic-gate 	 * Only v2 and v3 go on from here.
7630Sstevel@tonic-gate 	 */
7640Sstevel@tonic-gate 	roothandle.offset = (uint_t)0;		/* it's a directory! */
7650Sstevel@tonic-gate 	root_to.sin_port = htons(nfs_port);	/* NFS is next after mount */
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	/*
7680Sstevel@tonic-gate 	 * Create the CLIENT handle for NFS operations
7690Sstevel@tonic-gate 	 */
7700Sstevel@tonic-gate 	if (roothandle.version == NFS_VERSION)
7710Sstevel@tonic-gate 		bufsize = NFSBUF_SIZE;
7720Sstevel@tonic-gate 	else
7730Sstevel@tonic-gate 		bufsize = NFS3BUF_SIZE;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * First try TCP then UDP (unless UDP asked for explicitly), if mountd
7770Sstevel@tonic-gate 	 * alows this version but neither transport is available we are stuck.
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	if (istcp) {
7800Sstevel@tonic-gate 		fd = -1;
7810Sstevel@tonic-gate 		root_CLIENT = clntbtcp_create(&root_to, NFS_PROGRAM,
7820Sstevel@tonic-gate 			roothandle.version, wait, &fd, bufsize, bufsize);
7830Sstevel@tonic-gate 		if (root_CLIENT != NULL) {
7840Sstevel@tonic-gate 			root_CLIENT->cl_auth =
7850Sstevel@tonic-gate 			    authunix_create(my_hostname, 0, 1, 1, &fake_gids);
7860Sstevel@tonic-gate 			/*
7870Sstevel@tonic-gate 			 * Send NULL proc, check if the server really exists
7880Sstevel@tonic-gate 			 */
7890Sstevel@tonic-gate 			rpc_stat = CLNT_CALL(root_CLIENT, 0,
7900Sstevel@tonic-gate 					xdr_void, NULL, xdr_void, NULL, wait);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 			if (rpc_stat == RPC_SUCCESS)
7930Sstevel@tonic-gate 				return (0);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 			AUTH_DESTROY(root_CLIENT->cl_auth);
7960Sstevel@tonic-gate 			CLNT_DESTROY(root_CLIENT);
7970Sstevel@tonic-gate 			root_CLIENT = NULL;
7980Sstevel@tonic-gate 		}
7990Sstevel@tonic-gate 		/* Fall through to UDP case */
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	fd = -1;
8030Sstevel@tonic-gate 	root_CLIENT = clntbudp_bufcreate(&root_to, NFS_PROGRAM,
8040Sstevel@tonic-gate 			roothandle.version, wait, &fd, bufsize, bufsize);
8050Sstevel@tonic-gate 	if (root_CLIENT == NULL)
8060Sstevel@tonic-gate 		return (-1);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	root_CLIENT->cl_auth =
8090Sstevel@tonic-gate 			    authunix_create(my_hostname, 0, 1, 1, &fake_gids);
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * Send NULL proc, check if the server really exists
8120Sstevel@tonic-gate 	 */
8130Sstevel@tonic-gate 	rpc_stat = CLNT_CALL(root_CLIENT, 0,
8140Sstevel@tonic-gate 				xdr_void, NULL, xdr_void, NULL, wait);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	if (rpc_stat == RPC_SUCCESS)
8170Sstevel@tonic-gate 		return (0);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	AUTH_DESTROY(root_CLIENT->cl_auth);
8200Sstevel@tonic-gate 	CLNT_DESTROY(root_CLIENT);
8210Sstevel@tonic-gate 	root_CLIENT = NULL;
8220Sstevel@tonic-gate 	return (-1);
8230Sstevel@tonic-gate }
824