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