xref: /illumos-gate/usr/src/stand/lib/fs/nfs/mount.c (revision b531f6d1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59f1fc992Sss  * Common Development and Distribution License (the "License").
69f1fc992Sss  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
219f1fc992Sss 
227c478bd9Sstevel@tonic-gate /*
239f1fc992Sss  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /*	All Rights Reserved */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate #include <netinet/in.h>
347c478bd9Sstevel@tonic-gate #include <rpc/types.h>
357c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
367c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
377c478bd9Sstevel@tonic-gate #include <netdb.h>
387c478bd9Sstevel@tonic-gate #include "clnt.h"
397c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
407c478bd9Sstevel@tonic-gate #include <rpc/rpc_msg.h>
417c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
427c478bd9Sstevel@tonic-gate #include "brpc.h"
437c478bd9Sstevel@tonic-gate #include "auth_inet.h"
447c478bd9Sstevel@tonic-gate #include "pmap.h"
457c478bd9Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
467c478bd9Sstevel@tonic-gate #include <rpcsvc/nfs4_prot.h>
477c478bd9Sstevel@tonic-gate #include "nfs_inet.h"
487c478bd9Sstevel@tonic-gate #include <rpcsvc/bootparam.h>
497c478bd9Sstevel@tonic-gate #include <dhcp_impl.h>
507c478bd9Sstevel@tonic-gate #include <rpcsvc/mount.h>
517c478bd9Sstevel@tonic-gate #include <sys/promif.h>
527c478bd9Sstevel@tonic-gate #include <sys/salib.h>
537c478bd9Sstevel@tonic-gate #include "socket_inet.h"
547c478bd9Sstevel@tonic-gate #include "ipv4.h"
557c478bd9Sstevel@tonic-gate #include "mac.h"
567c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h>
577c478bd9Sstevel@tonic-gate #include <errno.h>
587c478bd9Sstevel@tonic-gate #include "dhcpv4.h"
597c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
607c478bd9Sstevel@tonic-gate 
619f1fc992Sss /* ARP timeout in milliseconds for BOOTP/RARP */
629f1fc992Sss #define	ARP_INETBOOT_TIMEOUT 1000
639f1fc992Sss 
647c478bd9Sstevel@tonic-gate struct nfs_file		roothandle;			/* root file handle */
657c478bd9Sstevel@tonic-gate static char		root_hostname[SYS_NMLN];	/* server hostname */
667c478bd9Sstevel@tonic-gate static char		my_hostname[MAXHOSTNAMELEN];
677c478bd9Sstevel@tonic-gate static char		root_pathbuf[NFS_MAXPATHLEN];	/* the root's path */
687c478bd9Sstevel@tonic-gate static char		root_boot_file[NFS_MAXPATHLEN];	/* optional boot file */
697c478bd9Sstevel@tonic-gate static struct sockaddr_in root_to;			/* server sock ip */
707c478bd9Sstevel@tonic-gate 							/* in network order */
717c478bd9Sstevel@tonic-gate CLIENT			*root_CLIENT = NULL;		/* CLIENT handle */
727c478bd9Sstevel@tonic-gate int dontroute = FALSE;	/* In case rarp/bootparams was selected */
737c478bd9Sstevel@tonic-gate char			rootopts[MAX_PATH_LEN];
747c478bd9Sstevel@tonic-gate static gid_t		fake_gids = 1;	/* fake gids list for auth_unix */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate extern void set_default_filename(char *);	/* boot.c */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * xdr routines used by mount.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate bool_t
xdr_fhstatus(XDR * xdrs,struct fhstatus * fhsp)837c478bd9Sstevel@tonic-gate xdr_fhstatus(XDR *xdrs, struct fhstatus *fhsp)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	if (!xdr_int(xdrs, (int *)&fhsp->fhs_status))
867c478bd9Sstevel@tonic-gate 		return (FALSE);
877c478bd9Sstevel@tonic-gate 	if (fhsp->fhs_status == 0) {
887c478bd9Sstevel@tonic-gate 		return (xdr_fhandle(xdrs, fhsp->fhstatus_u.fhs_fhandle));
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	return (TRUE);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate bool_t
xdr_fhandle(XDR * xdrs,fhandle fhp)947c478bd9Sstevel@tonic-gate xdr_fhandle(XDR *xdrs, fhandle fhp)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	return (xdr_opaque(xdrs, (char *)fhp, NFS_FHSIZE));
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate bool_t
xdr_path(XDR * xdrs,char ** pathp)1007c478bd9Sstevel@tonic-gate xdr_path(XDR *xdrs, char **pathp)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	return (xdr_string(xdrs, pathp, MNTPATHLEN));
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate bool_t
xdr_fhandle3(XDR * xdrs,fhandle3 * objp)1067c478bd9Sstevel@tonic-gate xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	return (xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
1097c478bd9Sstevel@tonic-gate 				(uint_t *)&objp->fhandle3_len, FHSIZE3));
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate bool_t
xdr_mountstat3(XDR * xdrs,mountstat3 * objp)1137c478bd9Sstevel@tonic-gate xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	return (xdr_enum(xdrs, (enum_t *)objp));
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate bool_t
xdr_mountres3_ok(XDR * xdrs,mountres3_ok * objp)1197c478bd9Sstevel@tonic-gate xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	if (!xdr_fhandle3(xdrs, &objp->fhandle))
1227c478bd9Sstevel@tonic-gate 		return (FALSE);
1237c478bd9Sstevel@tonic-gate 	return (xdr_array(xdrs, (char **)&objp->auth_flavors.auth_flavors_val,
1247c478bd9Sstevel@tonic-gate 			(uint_t *)&objp->auth_flavors.auth_flavors_len, ~0,
1257c478bd9Sstevel@tonic-gate 			sizeof (int), (xdrproc_t)xdr_int));
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate bool_t
xdr_mountres3(XDR * xdrs,mountres3 * objp)1297c478bd9Sstevel@tonic-gate xdr_mountres3(XDR *xdrs, mountres3 *objp)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	if (!xdr_mountstat3(xdrs, &objp->fhs_status))
1327c478bd9Sstevel@tonic-gate 		return (FALSE);
1337c478bd9Sstevel@tonic-gate 	if (objp->fhs_status == MNT_OK)
1347c478bd9Sstevel@tonic-gate 		return (xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo));
1357c478bd9Sstevel@tonic-gate 	return (TRUE);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static int
nfsmountroot(char * path,struct nfs_file * filep)1397c478bd9Sstevel@tonic-gate nfsmountroot(char *path, struct nfs_file *filep)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	int		rexmit;
1427c478bd9Sstevel@tonic-gate 	int		resp_wait;
1437c478bd9Sstevel@tonic-gate 	enum clnt_stat	status;
1447c478bd9Sstevel@tonic-gate 	struct fhstatus	root_tmp;			/* to pass to rpc/xdr */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Wait up to 16 secs for first response, retransmitting expon.
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	rexmit = 0;	/* default retransmission interval */
1507c478bd9Sstevel@tonic-gate 	resp_wait = 16;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	do {
1537c478bd9Sstevel@tonic-gate 		status = brpc_call((rpcprog_t)MOUNTPROG, (rpcvers_t)MOUNTVERS,
1547c478bd9Sstevel@tonic-gate 		    (rpcproc_t)MOUNTPROC_MNT, xdr_path, (caddr_t)&path,
1557c478bd9Sstevel@tonic-gate 		    xdr_fhstatus, (caddr_t)&(root_tmp), rexmit, resp_wait,
1567c478bd9Sstevel@tonic-gate 		    &root_to, NULL, AUTH_UNIX);
1577c478bd9Sstevel@tonic-gate 		if (status == RPC_TIMEDOUT) {
1587c478bd9Sstevel@tonic-gate 			dprintf("boot: %s:%s mount server not responding.\n",
1597c478bd9Sstevel@tonic-gate 			    root_hostname, path);
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		rexmit = resp_wait;
1627c478bd9Sstevel@tonic-gate 		resp_wait = 0;	/* use default wait time. */
1637c478bd9Sstevel@tonic-gate 	} while (status == RPC_TIMEDOUT);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if ((status != RPC_SUCCESS) || (root_tmp.fhs_status != 0)) {
1667c478bd9Sstevel@tonic-gate 		nfs_error(root_tmp.fhs_status);
1677c478bd9Sstevel@tonic-gate 		root_to.sin_port = 0;
1687c478bd9Sstevel@tonic-gate 		return (-1);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Since the mount succeeded, we'll mark the filep's
1737c478bd9Sstevel@tonic-gate 	 * status as NFS_OK, and its type as NFDIR. If these
1747c478bd9Sstevel@tonic-gate 	 * points aren't the case, then we wouldn't be here.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	bcopy(&root_tmp.fhstatus_u.fhs_fhandle, &filep->fh.fh2, FHSIZE);
1777c478bd9Sstevel@tonic-gate 	filep->ftype.type2 = NFDIR;
1787c478bd9Sstevel@tonic-gate 	filep->version = NFS_VERSION;
1797c478bd9Sstevel@tonic-gate 	nfs_readsize = nfs_readsize <  NFS_MAXDATA ? nfs_readsize : NFS_MAXDATA;
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * Set a reasonable lower limit on readsize
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
1847c478bd9Sstevel@tonic-gate 							512 : nfs_readsize;
1857c478bd9Sstevel@tonic-gate 	return (0);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate int
setup_root_vars(void)1897c478bd9Sstevel@tonic-gate setup_root_vars(void)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	size_t		buflen;
1927c478bd9Sstevel@tonic-gate 	uint16_t	readsize;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * Root server name. Required.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	buflen = sizeof (root_hostname);
1987c478bd9Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTSRVR_NAME, 0,
1997c478bd9Sstevel@tonic-gate 	    root_hostname, &buflen)) {
2007c478bd9Sstevel@tonic-gate 		root_hostname[buflen] = '\0';
2017c478bd9Sstevel@tonic-gate 	} else {
2027c478bd9Sstevel@tonic-gate 		dprintf("BOUND: Missing Root Server Name Option\n");
2037c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2047c478bd9Sstevel@tonic-gate 		return (-1);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * Root server IP. Required.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	buflen = sizeof (root_to.sin_addr);
2117c478bd9Sstevel@tonic-gate 	if (!dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTSRVR_IP, 0,
2127c478bd9Sstevel@tonic-gate 	    &root_to.sin_addr, &buflen)) {
2137c478bd9Sstevel@tonic-gate 		dprintf("BOUND: Missing Root Server IP Option\n");
2147c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2157c478bd9Sstevel@tonic-gate 		return (-1);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Root path Required.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	buflen = sizeof (root_pathbuf);
2227c478bd9Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTPATH, 0,
2237c478bd9Sstevel@tonic-gate 	    root_pathbuf, &buflen)) {
2247c478bd9Sstevel@tonic-gate 		root_pathbuf[buflen] = '\0';
2257c478bd9Sstevel@tonic-gate 	} else {
2267c478bd9Sstevel@tonic-gate 		dprintf("BOUND: Missing Root Path Option\n");
2277c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2287c478bd9Sstevel@tonic-gate 		return (-1);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * Optional Bootfile path.
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	buflen = sizeof (root_boot_file);
2357c478bd9Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_BOOTFILE, 0,
2367c478bd9Sstevel@tonic-gate 		    root_boot_file, &buflen)) {
2377c478bd9Sstevel@tonic-gate 		root_boot_file[buflen] = '\0';
2387c478bd9Sstevel@tonic-gate 		dprintf("BOUND: Optional Boot File is: %s\n", root_boot_file);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/* if we got a boot file name, use it as the default */
2427c478bd9Sstevel@tonic-gate 	if (root_boot_file[0] != '\0')
2437c478bd9Sstevel@tonic-gate 		set_default_filename(root_boot_file);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * Set the NFS read size. The mount code will adjust it to
2477c478bd9Sstevel@tonic-gate 	 * the maximum size.
2487c478bd9Sstevel@tonic-gate 	 */
2497c478bd9Sstevel@tonic-gate 	buflen = sizeof (readsize);
2507c478bd9Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_BOOT_NFS_READSIZE, 0,
2517c478bd9Sstevel@tonic-gate 	    &readsize, &buflen)) {
2527c478bd9Sstevel@tonic-gate 		nfs_readsize = ntohs(readsize);
2537c478bd9Sstevel@tonic-gate 		if (boothowto & RB_VERBOSE) {
2547c478bd9Sstevel@tonic-gate 			printf("Boot NFS read size: %d\n", nfs_readsize);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
2597c478bd9Sstevel@tonic-gate 	 * Optional rootopts.
2607c478bd9Sstevel@tonic-gate 	 */
2617c478bd9Sstevel@tonic-gate 	buflen = sizeof (rootopts);
2627c478bd9Sstevel@tonic-gate 	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTOPTS, 0,
2637c478bd9Sstevel@tonic-gate 	    rootopts, &buflen)) {
2647c478bd9Sstevel@tonic-gate 		rootopts[buflen] = '\0';
2657c478bd9Sstevel@tonic-gate 		dprintf("BOUND: Optional Rootopts is: %s\n", rootopts);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	return (0);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate static void
mnt3_error(enum mountstat3 status)2727c478bd9Sstevel@tonic-gate mnt3_error(enum mountstat3 status)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	if (!(boothowto & RB_DEBUG))
2757c478bd9Sstevel@tonic-gate 		return;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	switch (status) {
2787c478bd9Sstevel@tonic-gate 	case MNT_OK:
2797c478bd9Sstevel@tonic-gate 		printf("Mount: No error.\n");
2807c478bd9Sstevel@tonic-gate 		break;
2817c478bd9Sstevel@tonic-gate 	case MNT3ERR_PERM:
2827c478bd9Sstevel@tonic-gate 		printf("Mount: Not owner.\n");
2837c478bd9Sstevel@tonic-gate 		break;
2847c478bd9Sstevel@tonic-gate 	case MNT3ERR_NOENT:
2857c478bd9Sstevel@tonic-gate 		printf("Mount: No such file or directory.\n");
2867c478bd9Sstevel@tonic-gate 		break;
2877c478bd9Sstevel@tonic-gate 	case MNT3ERR_IO:
2887c478bd9Sstevel@tonic-gate 		printf("Mount: I/O error.\n");
2897c478bd9Sstevel@tonic-gate 		break;
2907c478bd9Sstevel@tonic-gate 	case MNT3ERR_ACCES:
2917c478bd9Sstevel@tonic-gate 		printf("Mount: Permission denied.\n");
2927c478bd9Sstevel@tonic-gate 		break;
2937c478bd9Sstevel@tonic-gate 	case MNT3ERR_NOTDIR:
2947c478bd9Sstevel@tonic-gate 		printf("Mount: Not a directory.\n");
2957c478bd9Sstevel@tonic-gate 		break;
2967c478bd9Sstevel@tonic-gate 	case MNT3ERR_INVAL:
2977c478bd9Sstevel@tonic-gate 		printf("Mount: Invalid argument.\n");
2987c478bd9Sstevel@tonic-gate 		break;
2997c478bd9Sstevel@tonic-gate 	case MNT3ERR_NAMETOOLONG:
3007c478bd9Sstevel@tonic-gate 		printf("Mount: File name too long.\n");
3017c478bd9Sstevel@tonic-gate 		break;
3027c478bd9Sstevel@tonic-gate 	case MNT3ERR_NOTSUPP:
3037c478bd9Sstevel@tonic-gate 		printf("Mount: Operation not supported.\n");
3047c478bd9Sstevel@tonic-gate 		break;
3057c478bd9Sstevel@tonic-gate 	case MNT3ERR_SERVERFAULT:
3067c478bd9Sstevel@tonic-gate 		printf("Mount: Server fault.\n");
3077c478bd9Sstevel@tonic-gate 		break;
3087c478bd9Sstevel@tonic-gate 	default:
3097c478bd9Sstevel@tonic-gate 		printf("Mount: unknown error.\n");
3107c478bd9Sstevel@tonic-gate 		break;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate static int
nfs3mountroot(char * path,struct nfs_file * filep)3157c478bd9Sstevel@tonic-gate nfs3mountroot(char *path, struct nfs_file *filep)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	int		rexmit;
3187c478bd9Sstevel@tonic-gate 	int		resp_wait;
3197c478bd9Sstevel@tonic-gate 	struct mountres3 res3;
3207c478bd9Sstevel@tonic-gate 	enum clnt_stat	status;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Wait up to 16 secs for first response, retransmitting expon.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	rexmit = 0;	/* default retransmission interval */
3267c478bd9Sstevel@tonic-gate 	resp_wait = 16;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Try to mount using V3
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	do {
3327c478bd9Sstevel@tonic-gate 		bzero(&res3, sizeof (struct mountres3));
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		status = brpc_call((rpcprog_t)MOUNTPROG, (rpcvers_t)MOUNTVERS3,
3357c478bd9Sstevel@tonic-gate 		    (rpcproc_t)MOUNTPROC_MNT, xdr_path, (caddr_t)&path,
3367c478bd9Sstevel@tonic-gate 		    xdr_mountres3, (caddr_t)&res3, rexmit, resp_wait,
3377c478bd9Sstevel@tonic-gate 		    &root_to, NULL, AUTH_UNIX);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		if (status != RPC_TIMEDOUT)
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		dprintf("boot: %s:%s mount server not responding.\n",
3437c478bd9Sstevel@tonic-gate 			    root_hostname, path);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		rexmit = resp_wait;
3467c478bd9Sstevel@tonic-gate 		resp_wait = 0;	/* use default wait time. */
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		xdr_free(xdr_mountres3, (caddr_t)&res3);
3497c478bd9Sstevel@tonic-gate 	} while (status == RPC_TIMEDOUT);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if ((status != RPC_SUCCESS) || (res3.fhs_status != MNT_OK)) {
3527c478bd9Sstevel@tonic-gate 		mnt3_error(res3.fhs_status);
3537c478bd9Sstevel@tonic-gate 		root_to.sin_port = 0;
3547c478bd9Sstevel@tonic-gate 		return (-1);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * Since the mount succeeded, we'll mark the filep's
3597c478bd9Sstevel@tonic-gate 	 * status as NFS_OK, and its type as NF3DIR. If these
3607c478bd9Sstevel@tonic-gate 	 * points aren't the case, then we wouldn't be here.
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	filep->fh.fh3.len = res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
3637c478bd9Sstevel@tonic-gate 	bcopy(res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
3647c478bd9Sstevel@tonic-gate 			filep->fh.fh3.data,
3657c478bd9Sstevel@tonic-gate 			filep->fh.fh3.len);
3667c478bd9Sstevel@tonic-gate 	filep->ftype.type3 = NF3DIR;
3677c478bd9Sstevel@tonic-gate 	filep->version = NFS_V3;
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * Hardwire in a known reasonable upper limit of 32K
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	nfs_readsize = nfs_readsize <  32 * 1024 ? nfs_readsize : 32 * 1024;
3727c478bd9Sstevel@tonic-gate 	/*
3737c478bd9Sstevel@tonic-gate 	 * Set a reasonable lower limit on readsize
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
3767c478bd9Sstevel@tonic-gate 							512 : nfs_readsize;
3777c478bd9Sstevel@tonic-gate 	xdr_free(xdr_mountres3, (caddr_t)&res3);
3787c478bd9Sstevel@tonic-gate 	return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Setup v4 client for inetboot
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate static int
nfs4init(char * path,uint16_t nfs_port)3857c478bd9Sstevel@tonic-gate nfs4init(char *path, uint16_t nfs_port)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	struct timeval	wait;
3887c478bd9Sstevel@tonic-gate 	int		fd = -1;
3897c478bd9Sstevel@tonic-gate 	int		error = 0;
3907c478bd9Sstevel@tonic-gate 	enum clnt_stat	rpc_stat;
3917c478bd9Sstevel@tonic-gate 	struct nfs_file	rootpath;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	wait.tv_sec = RPC_RCVWAIT_MSEC / 1000;
3947c478bd9Sstevel@tonic-gate 	wait.tv_usec = 0;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * If we haven't explicitly set the port number, set to the standard
3987c478bd9Sstevel@tonic-gate 	 * 2049 and don't cause a rpcbind request.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	if (nfs_port == 0)
4017c478bd9Sstevel@tonic-gate 		nfs_port = 2049;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	root_to.sin_port = htons(nfs_port);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Support TCP only
4077c478bd9Sstevel@tonic-gate 	 */
4087c478bd9Sstevel@tonic-gate 	root_CLIENT = clntbtcp_create(&root_to, NFS_PROGRAM,
4097c478bd9Sstevel@tonic-gate 					NFS_V4, wait, &fd,
4107c478bd9Sstevel@tonic-gate 					NFS4BUF_SIZE, NFS4BUF_SIZE);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (root_CLIENT == NULL) {
4137c478bd9Sstevel@tonic-gate 		root_to.sin_port = 0;
4147c478bd9Sstevel@tonic-gate 		return (-1);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	root_CLIENT->cl_auth =
4187c478bd9Sstevel@tonic-gate 			authunix_create(my_hostname, 0, 1, 1, &fake_gids);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * Send NULL proc the server first to see if V4 exists
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	rpc_stat = CLNT_CALL(root_CLIENT, NFSPROC4_NULL, xdr_void, NULL,
4247c478bd9Sstevel@tonic-gate 				xdr_void, NULL, wait);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
4277c478bd9Sstevel@tonic-gate 		dprintf("boot: NULL proc failed NFSv4 service not available\n");
4287c478bd9Sstevel@tonic-gate 		AUTH_DESTROY(root_CLIENT->cl_auth);
4297c478bd9Sstevel@tonic-gate 		CLNT_DESTROY(root_CLIENT);
4307c478bd9Sstevel@tonic-gate 		root_to.sin_port = 0;
4317c478bd9Sstevel@tonic-gate 		return (-1);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/*
4357c478bd9Sstevel@tonic-gate 	 * Do a lookup to get to the root_path.  This is nice since it can
4367c478bd9Sstevel@tonic-gate 	 * handle multicomponent lookups.
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 	roothandle.version = NFS_V4;
4397c478bd9Sstevel@tonic-gate 	roothandle.ftype.type4 = NF4DIR;
4407c478bd9Sstevel@tonic-gate 	roothandle.fh.fh4.len = 0;		/* Force a PUTROOTFH */
4417c478bd9Sstevel@tonic-gate 	roothandle.offset = (uint_t)0;		/* it's a directory! */
4427c478bd9Sstevel@tonic-gate 	error = lookup(path, &rootpath, TRUE);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (error) {
4457c478bd9Sstevel@tonic-gate 		printf("boot: lookup %s failed\n", path);
4467c478bd9Sstevel@tonic-gate 		return (-1);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	roothandle = rootpath;	/* structure copy */
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * Hardwire in a known reasonable upper limit of 32K
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	nfs_readsize = nfs_readsize <  32 * 1024 ? nfs_readsize : 32 * 1024;
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * Set a reasonable lower limit on readsize
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
4587c478bd9Sstevel@tonic-gate 							512 : nfs_readsize;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	return (0);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate static int
atoi(const char * p)4647c478bd9Sstevel@tonic-gate atoi(const char *p)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	int n;
4677c478bd9Sstevel@tonic-gate 	int c, neg = 0;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (!isdigit(c = *p)) {
4707c478bd9Sstevel@tonic-gate 		while (c == ' ' || c == '\t' || c == '\n')
4717c478bd9Sstevel@tonic-gate 			c = *++p;
4727c478bd9Sstevel@tonic-gate 		switch (c) {
4737c478bd9Sstevel@tonic-gate 		case '-':
4747c478bd9Sstevel@tonic-gate 			neg++;
4757c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
4767c478bd9Sstevel@tonic-gate 		case '+':
4777c478bd9Sstevel@tonic-gate 			c = *++p;
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 		if (!isdigit(c))
4807c478bd9Sstevel@tonic-gate 			return (0);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	for (n = '0' - c; isdigit(c = *++p); ) {
4837c478bd9Sstevel@tonic-gate 		n *= 10; /* two steps to avoid unnecessary overflow */
4847c478bd9Sstevel@tonic-gate 		n += '0' - c; /* accum neg to avoid surprises at MAX */
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	return (neg ? n : -n);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * Parse suboptions from a string.
4917c478bd9Sstevel@tonic-gate  * Same as getsubopt(3C).
4927c478bd9Sstevel@tonic-gate  */
4937c478bd9Sstevel@tonic-gate static int
getsubopt(char ** optionsp,char * const * tokens,char ** valuep)4947c478bd9Sstevel@tonic-gate getsubopt(char **optionsp, char * const *tokens, char **valuep)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	char *s = *optionsp, *p;
4977c478bd9Sstevel@tonic-gate 	int i;
4987c478bd9Sstevel@tonic-gate 	size_t optlen;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	*valuep = NULL;
5017c478bd9Sstevel@tonic-gate 	if (*s == '\0')
5027c478bd9Sstevel@tonic-gate 		return (-1);
5037c478bd9Sstevel@tonic-gate 	p = strchr(s, ',');		/* find next option */
5047c478bd9Sstevel@tonic-gate 	if (p == NULL) {
5057c478bd9Sstevel@tonic-gate 		p = s + strlen(s);
5067c478bd9Sstevel@tonic-gate 	} else {
5077c478bd9Sstevel@tonic-gate 		*p++ = '\0';		/* mark end and point to next */
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 	*optionsp = p;			/* point to next option */
5107c478bd9Sstevel@tonic-gate 	p = strchr(s, '=');		/* find value */
5117c478bd9Sstevel@tonic-gate 	if (p == NULL) {
5127c478bd9Sstevel@tonic-gate 		optlen = strlen(s);
5137c478bd9Sstevel@tonic-gate 		*valuep = NULL;
5147c478bd9Sstevel@tonic-gate 	} else {
5157c478bd9Sstevel@tonic-gate 		optlen = p - s;
5167c478bd9Sstevel@tonic-gate 		*valuep = ++p;
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	for (i = 0; tokens[i] != NULL; i++) {
5197c478bd9Sstevel@tonic-gate 		if ((optlen == strlen(tokens[i])) &&
5207c478bd9Sstevel@tonic-gate 		    (strncmp(s, tokens[i], optlen) == 0))
5217c478bd9Sstevel@tonic-gate 			return (i);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 	/* no match, point value at option and return error */
5247c478bd9Sstevel@tonic-gate 	*valuep = s;
5257c478bd9Sstevel@tonic-gate 	return (-1);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * The only interesting NFS mount options for initiating the kernel
5307c478bd9Sstevel@tonic-gate  * all others are ignored.
5317c478bd9Sstevel@tonic-gate  */
5327c478bd9Sstevel@tonic-gate static char *optlist[] = {
5337c478bd9Sstevel@tonic-gate #define	OPT_RSIZE	0
5347c478bd9Sstevel@tonic-gate 	MNTOPT_RSIZE,
5357c478bd9Sstevel@tonic-gate #define	OPT_TIMEO	1
5367c478bd9Sstevel@tonic-gate 	MNTOPT_TIMEO,
5377c478bd9Sstevel@tonic-gate #define	OPT_VERS	2
5387c478bd9Sstevel@tonic-gate 	MNTOPT_VERS,
5397c478bd9Sstevel@tonic-gate #define	OPT_PROTO	3
5407c478bd9Sstevel@tonic-gate 	MNTOPT_PROTO,
5417c478bd9Sstevel@tonic-gate #define	OPT_PORT	4
5427c478bd9Sstevel@tonic-gate 	MNTOPT_PORT,
5437c478bd9Sstevel@tonic-gate 	NULL
5447c478bd9Sstevel@tonic-gate };
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  * This routine will open a device as it is known by the V2 OBP. It
5487c478bd9Sstevel@tonic-gate  * then goes thru the stuff necessary to initialize the network device,
5497c478bd9Sstevel@tonic-gate  * get our network parameters, (using DHCP or rarp/bootparams), and
5507c478bd9Sstevel@tonic-gate  * finally actually go and get the root filehandle. Sound like fun?
5517c478bd9Sstevel@tonic-gate  * Suuurrrree. Take a look.
5527c478bd9Sstevel@tonic-gate  *
5537c478bd9Sstevel@tonic-gate  * Returns 0 if things worked. -1 if we crashed and burned.
5547c478bd9Sstevel@tonic-gate  */
5557c478bd9Sstevel@tonic-gate int
boot_nfs_mountroot(char * str)5567c478bd9Sstevel@tonic-gate boot_nfs_mountroot(char *str)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	int		status;
5597c478bd9Sstevel@tonic-gate 	enum clnt_stat	rpc_stat;
5607c478bd9Sstevel@tonic-gate 	char		*root_path = &root_pathbuf[0];	/* to make XDR happy */
5617c478bd9Sstevel@tonic-gate 	struct timeval	wait;
5627c478bd9Sstevel@tonic-gate 	int		fd;
5637c478bd9Sstevel@tonic-gate 	int		bufsize;
5647c478bd9Sstevel@tonic-gate 	char		*opts, *val;
5657c478bd9Sstevel@tonic-gate 	int		nfs_version = 0;
5667c478bd9Sstevel@tonic-gate 	int		istcp = 1;
5677c478bd9Sstevel@tonic-gate 	int		nfs_port = 0;	/* Cause pmap to get port */
5687c478bd9Sstevel@tonic-gate 	struct sockaddr_in tmp_addr;	/* throw away */
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (root_CLIENT != NULL) {
5717c478bd9Sstevel@tonic-gate 		AUTH_DESTROY(root_CLIENT->cl_auth);
5727c478bd9Sstevel@tonic-gate 		CLNT_DESTROY(root_CLIENT);
5737c478bd9Sstevel@tonic-gate 		root_CLIENT = NULL;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	root_to.sin_family = AF_INET;
5777c478bd9Sstevel@tonic-gate 	root_to.sin_addr.s_addr = htonl(INADDR_ANY);
5787c478bd9Sstevel@tonic-gate 	root_to.sin_port = htons(0);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	mac_init(str);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	(void) ipv4_setpromiscuous(TRUE);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (get_netconfig_strategy() == NCT_BOOTP_DHCP) {
5857c478bd9Sstevel@tonic-gate 		if (boothowto & RB_VERBOSE)
5867c478bd9Sstevel@tonic-gate 			printf("Using BOOTP/DHCP...\n");
5877c478bd9Sstevel@tonic-gate 		if (dhcp() != 0 || setup_root_vars() != 0) {
5887c478bd9Sstevel@tonic-gate 			(void) ipv4_setpromiscuous(FALSE);
5897c478bd9Sstevel@tonic-gate 			if (boothowto & RB_VERBOSE)
5907c478bd9Sstevel@tonic-gate 				printf("BOOTP/DHCP configuration failed!\n");
5917c478bd9Sstevel@tonic-gate 			return (-1);
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		/* now that we have an IP address, turn off promiscuous mode */
5957c478bd9Sstevel@tonic-gate 		(void) ipv4_setpromiscuous(FALSE);
5967c478bd9Sstevel@tonic-gate 	} else {
5977c478bd9Sstevel@tonic-gate 		/* Use RARP/BOOTPARAMS. RARP will try forever... */
5987c478bd9Sstevel@tonic-gate 		if (boothowto & RB_VERBOSE)
5997c478bd9Sstevel@tonic-gate 			printf("Using RARP/BOOTPARAMS...\n");
6007c478bd9Sstevel@tonic-gate 		mac_call_rarp();
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		/*
6037c478bd9Sstevel@tonic-gate 		 * Since there is no way to determine our netmask, and therefore
6047c478bd9Sstevel@tonic-gate 		 * figure out if the router we got is useful, we assume all
6057c478bd9Sstevel@tonic-gate 		 * services are local. Use DHCP if this bothers you.
6067c478bd9Sstevel@tonic-gate 		 */
6077c478bd9Sstevel@tonic-gate 		dontroute = TRUE;
6089f1fc992Sss 		/*
6099f1fc992Sss 		 * We are trying to keep the ARP response
6109f1fc992Sss 		 * timeout on the lower side with BOOTP/RARP.
6119f1fc992Sss 		 * We are doing this for BOOTP/RARP where policy
6129f1fc992Sss 		 * doesn't allow to route the packets outside
6139f1fc992Sss 		 * the subnet as it has no idea about the
6149f1fc992Sss 		 * netmask. By doing so, we are reducing
6159f1fc992Sss 		 * ARP response timeout for any packet destined
6169f1fc992Sss 		 * for outside booting clients subnet. Client can
6179f1fc992Sss 		 * not expect such ARP replies and will finally
6189f1fc992Sss 		 * timeout after a long delay. This would cause
6199f1fc992Sss 		 * booting client to get stalled for a longer
6209f1fc992Sss 		 * time. We can not avoid accepting any outside
6219f1fc992Sss 		 * subnet packets accidentally destined for the
6229f1fc992Sss 		 * booting client.
6239f1fc992Sss 		 */
6249f1fc992Sss 		mac_set_arp_timeout(ARP_INETBOOT_TIMEOUT);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		/* now that we have an IP address, turn off promiscuous mode */
6277c478bd9Sstevel@tonic-gate 		(void) ipv4_setpromiscuous(FALSE);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		/* get our hostname */
6307c478bd9Sstevel@tonic-gate 		if (whoami() == FALSE)
6317c478bd9Sstevel@tonic-gate 			return (-1);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		/* get our bootparams. */
6347c478bd9Sstevel@tonic-gate 		if (getfile("root", root_hostname, &root_to.sin_addr,
6357c478bd9Sstevel@tonic-gate 		    root_pathbuf) == FALSE)
6367c478bd9Sstevel@tonic-gate 			return (-1);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		/* get our rootopts. */
6397c478bd9Sstevel@tonic-gate 		(void) getfile("rootopts", root_hostname, &tmp_addr.sin_addr,
6407c478bd9Sstevel@tonic-gate 		    rootopts);
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/* mount root */
6447c478bd9Sstevel@tonic-gate 	if (boothowto & RB_VERBOSE) {
6457c478bd9Sstevel@tonic-gate 		printf("root server: %s (%s)\n", root_hostname,
6467c478bd9Sstevel@tonic-gate 		    inet_ntoa(root_to.sin_addr));
6477c478bd9Sstevel@tonic-gate 		printf("root directory: %s\n", root_pathbuf);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * Assumes we've configured the stack and thus know our
6527c478bd9Sstevel@tonic-gate 	 * IP address/hostname, either by using DHCP or rarp/bootparams.
6537c478bd9Sstevel@tonic-gate 	 */
654*b531f6d1SToomas Soome 	(void) gethostname(my_hostname, sizeof (my_hostname));
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	wait.tv_sec = RPC_RCVWAIT_MSEC / 1000;
6577c478bd9Sstevel@tonic-gate 	wait.tv_usec = 0;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/*
6607c478bd9Sstevel@tonic-gate 	 * Parse out the interesting root options, if an invalid
6617c478bd9Sstevel@tonic-gate 	 * or unknown option is provided, silently ignore it and
6627c478bd9Sstevel@tonic-gate 	 * use the defaults.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	opts = rootopts;
6657c478bd9Sstevel@tonic-gate 	while (*opts) {
6667c478bd9Sstevel@tonic-gate 		int ival;
6677c478bd9Sstevel@tonic-gate 		switch (getsubopt(&opts, optlist, &val)) {
6687c478bd9Sstevel@tonic-gate 		case OPT_RSIZE:
6697c478bd9Sstevel@tonic-gate 			if (val == NULL || !isdigit(*val))
6707c478bd9Sstevel@tonic-gate 				break;
6717c478bd9Sstevel@tonic-gate 			nfs_readsize = atoi(val);
6727c478bd9Sstevel@tonic-gate 			break;
6737c478bd9Sstevel@tonic-gate 		case OPT_TIMEO:
6747c478bd9Sstevel@tonic-gate 			if (val == NULL || !isdigit(*val))
6757c478bd9Sstevel@tonic-gate 				break;
6767c478bd9Sstevel@tonic-gate 			ival = atoi(val);
6777c478bd9Sstevel@tonic-gate 			wait.tv_sec = ival / 10;
6787c478bd9Sstevel@tonic-gate 			wait.tv_usec = (ival % 10) * 100000;
6797c478bd9Sstevel@tonic-gate 			break;
6807c478bd9Sstevel@tonic-gate 		case OPT_VERS:
6817c478bd9Sstevel@tonic-gate 			if (val == NULL || !isdigit(*val))
6827c478bd9Sstevel@tonic-gate 				break;
6837c478bd9Sstevel@tonic-gate 			nfs_version = atoi(val);
6847c478bd9Sstevel@tonic-gate 			break;
6857c478bd9Sstevel@tonic-gate 		case OPT_PROTO:
6867c478bd9Sstevel@tonic-gate 			if (val == NULL || isdigit(*val))
6877c478bd9Sstevel@tonic-gate 				break;
6887c478bd9Sstevel@tonic-gate 			if ((strncmp(val, "udp", 3) == 0))
6897c478bd9Sstevel@tonic-gate 				istcp = 0;
6907c478bd9Sstevel@tonic-gate 			else
6917c478bd9Sstevel@tonic-gate 				istcp = 1;	/* must be tcp */
6927c478bd9Sstevel@tonic-gate 			break;
6937c478bd9Sstevel@tonic-gate 		case OPT_PORT:
6947c478bd9Sstevel@tonic-gate 			if (val == NULL || !isdigit(*val))
6957c478bd9Sstevel@tonic-gate 				break;
6967c478bd9Sstevel@tonic-gate 			nfs_port = atoi(val);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			/*
6997c478bd9Sstevel@tonic-gate 			 * Currently nfs_dlinet.c doesn't support setting
7007c478bd9Sstevel@tonic-gate 			 * the root NFS port. Delete this when it does.
7017c478bd9Sstevel@tonic-gate 			 */
7027c478bd9Sstevel@tonic-gate 			nfs_port = 0;
7037c478bd9Sstevel@tonic-gate 			break;
7047c478bd9Sstevel@tonic-gate 		default:
7057c478bd9Sstevel@tonic-gate 			/*
7067c478bd9Sstevel@tonic-gate 			 * Unknown options are silently ignored
7077c478bd9Sstevel@tonic-gate 			 */
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	/*
7137c478bd9Sstevel@tonic-gate 	 * If version is set, then try that version first.
7147c478bd9Sstevel@tonic-gate 	 */
7157c478bd9Sstevel@tonic-gate 	switch (nfs_version) {
7167c478bd9Sstevel@tonic-gate 	case NFS_VERSION:
7177c478bd9Sstevel@tonic-gate 		if (nfsmountroot(root_path, &roothandle) == 0)
7187c478bd9Sstevel@tonic-gate 			goto domount;
7197c478bd9Sstevel@tonic-gate 		break;
7207c478bd9Sstevel@tonic-gate 	case NFS_V3:
7217c478bd9Sstevel@tonic-gate 		if (nfs3mountroot(root_path, &roothandle) == 0)
7227c478bd9Sstevel@tonic-gate 			goto domount;
7237c478bd9Sstevel@tonic-gate 		break;
7247c478bd9Sstevel@tonic-gate 	case NFS_V4:
7257c478bd9Sstevel@tonic-gate 		/*
7267c478bd9Sstevel@tonic-gate 		 * With v4 we skip the mount and go straight to
7277c478bd9Sstevel@tonic-gate 		 * setting the root filehandle.  Because of this we
7287c478bd9Sstevel@tonic-gate 		 * do things slightly differently and obtain our
7297c478bd9Sstevel@tonic-gate 		 * client handle first.
7307c478bd9Sstevel@tonic-gate 		 */
7317c478bd9Sstevel@tonic-gate 		if (istcp && nfs4init(root_path, nfs_port) == 0) {
7327c478bd9Sstevel@tonic-gate 			/*
7337c478bd9Sstevel@tonic-gate 			 * If v4 init succeeded then we are done.  Just return.
7347c478bd9Sstevel@tonic-gate 			 */
7357c478bd9Sstevel@tonic-gate 			return (0);
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	/*
7407c478bd9Sstevel@tonic-gate 	 * If there was no chosen version or the chosen version failed
7417c478bd9Sstevel@tonic-gate 	 * try all versions in order, this may still fail to boot
7427c478bd9Sstevel@tonic-gate 	 * at the kernel level if the options are not right, but be
7437c478bd9Sstevel@tonic-gate 	 * generous at this early stage.
7447c478bd9Sstevel@tonic-gate 	 */
7457c478bd9Sstevel@tonic-gate 	if (istcp && nfs4init(root_path, nfs_port) == 0) {
7467c478bd9Sstevel@tonic-gate 		/*
7477c478bd9Sstevel@tonic-gate 		 * If v4 init succeeded then we are done.  Just return.
7487c478bd9Sstevel@tonic-gate 		 */
7497c478bd9Sstevel@tonic-gate 		return (0);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if (nfs3mountroot(root_path, &roothandle) == 0)
7537c478bd9Sstevel@tonic-gate 		goto domount;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if ((status = nfsmountroot(root_path, &roothandle)) != 0)
7567c478bd9Sstevel@tonic-gate 		return (status);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate domount:
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * Only v2 and v3 go on from here.
7617c478bd9Sstevel@tonic-gate 	 */
7627c478bd9Sstevel@tonic-gate 	roothandle.offset = (uint_t)0;		/* it's a directory! */
7637c478bd9Sstevel@tonic-gate 	root_to.sin_port = htons(nfs_port);	/* NFS is next after mount */
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	/*
7667c478bd9Sstevel@tonic-gate 	 * Create the CLIENT handle for NFS operations
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	if (roothandle.version == NFS_VERSION)
7697c478bd9Sstevel@tonic-gate 		bufsize = NFSBUF_SIZE;
7707c478bd9Sstevel@tonic-gate 	else
7717c478bd9Sstevel@tonic-gate 		bufsize = NFS3BUF_SIZE;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	/*
7747c478bd9Sstevel@tonic-gate 	 * First try TCP then UDP (unless UDP asked for explicitly), if mountd
7757c478bd9Sstevel@tonic-gate 	 * alows this version but neither transport is available we are stuck.
7767c478bd9Sstevel@tonic-gate 	 */
7777c478bd9Sstevel@tonic-gate 	if (istcp) {
7787c478bd9Sstevel@tonic-gate 		fd = -1;
7797c478bd9Sstevel@tonic-gate 		root_CLIENT = clntbtcp_create(&root_to, NFS_PROGRAM,
7807c478bd9Sstevel@tonic-gate 			roothandle.version, wait, &fd, bufsize, bufsize);
7817c478bd9Sstevel@tonic-gate 		if (root_CLIENT != NULL) {
7827c478bd9Sstevel@tonic-gate 			root_CLIENT->cl_auth =
7837c478bd9Sstevel@tonic-gate 			    authunix_create(my_hostname, 0, 1, 1, &fake_gids);
7847c478bd9Sstevel@tonic-gate 			/*
7857c478bd9Sstevel@tonic-gate 			 * Send NULL proc, check if the server really exists
7867c478bd9Sstevel@tonic-gate 			 */
7877c478bd9Sstevel@tonic-gate 			rpc_stat = CLNT_CALL(root_CLIENT, 0,
7887c478bd9Sstevel@tonic-gate 					xdr_void, NULL, xdr_void, NULL, wait);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 			if (rpc_stat == RPC_SUCCESS)
7917c478bd9Sstevel@tonic-gate 				return (0);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 			AUTH_DESTROY(root_CLIENT->cl_auth);
7947c478bd9Sstevel@tonic-gate 			CLNT_DESTROY(root_CLIENT);
7957c478bd9Sstevel@tonic-gate 			root_CLIENT = NULL;
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 		/* Fall through to UDP case */
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	fd = -1;
8017c478bd9Sstevel@tonic-gate 	root_CLIENT = clntbudp_bufcreate(&root_to, NFS_PROGRAM,
8027c478bd9Sstevel@tonic-gate 			roothandle.version, wait, &fd, bufsize, bufsize);
8037c478bd9Sstevel@tonic-gate 	if (root_CLIENT == NULL)
8047c478bd9Sstevel@tonic-gate 		return (-1);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	root_CLIENT->cl_auth =
8077c478bd9Sstevel@tonic-gate 			    authunix_create(my_hostname, 0, 1, 1, &fake_gids);
8087c478bd9Sstevel@tonic-gate 	/*
8097c478bd9Sstevel@tonic-gate 	 * Send NULL proc, check if the server really exists
8107c478bd9Sstevel@tonic-gate 	 */
8117c478bd9Sstevel@tonic-gate 	rpc_stat = CLNT_CALL(root_CLIENT, 0,
8127c478bd9Sstevel@tonic-gate 				xdr_void, NULL, xdr_void, NULL, wait);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	if (rpc_stat == RPC_SUCCESS)
8157c478bd9Sstevel@tonic-gate 		return (0);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	AUTH_DESTROY(root_CLIENT->cl_auth);
8187c478bd9Sstevel@tonic-gate 	CLNT_DESTROY(root_CLIENT);
8197c478bd9Sstevel@tonic-gate 	root_CLIENT = NULL;
8207c478bd9Sstevel@tonic-gate 	return (-1);
8217c478bd9Sstevel@tonic-gate }
822