17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5843e198johnlev * Common Development and Distribution License (the "License").
6843e198johnlev * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22d62bc4byz * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
2615c07adJohn Levon/*
2715c07adJohn Levon * Copyright (c) 2018, Joyent, Inc.
2815c07adJohn Levon */
2915c07adJohn Levon
307c478bdstevel@tonic-gate#include <sys/param.h>
317c478bdstevel@tonic-gate#include <sys/types.h>
327c478bdstevel@tonic-gate#include <sys/systm.h>
337c478bdstevel@tonic-gate#include <sys/cred.h>
347c478bdstevel@tonic-gate#include <sys/user.h>
357c478bdstevel@tonic-gate#include <sys/file.h>
367c478bdstevel@tonic-gate#include <sys/stream.h>
377c478bdstevel@tonic-gate#include <sys/strsubr.h>
387c478bdstevel@tonic-gate#include <sys/stropts.h>
397c478bdstevel@tonic-gate#include <sys/strsun.h>
407c478bdstevel@tonic-gate#include <sys/debug.h>
417c478bdstevel@tonic-gate#include <sys/tiuser.h>
427c478bdstevel@tonic-gate#include <sys/sockio.h>
437c478bdstevel@tonic-gate#include <sys/socket.h>
447c478bdstevel@tonic-gate#include <sys/t_kuser.h>
457c478bdstevel@tonic-gate#include <sys/utsname.h>
467c478bdstevel@tonic-gate#include <sys/systeminfo.h>
477c478bdstevel@tonic-gate#include <sys/netconfig.h>
487c478bdstevel@tonic-gate#include <sys/ethernet.h>
497c478bdstevel@tonic-gate#include <sys/dlpi.h>
507c478bdstevel@tonic-gate#include <sys/vfs.h>
517c478bdstevel@tonic-gate#include <sys/sysmacros.h>
527c478bdstevel@tonic-gate#include <sys/bootconf.h>
537c478bdstevel@tonic-gate#include <sys/bootprops.h>
547c478bdstevel@tonic-gate#include <sys/cmn_err.h>
557c478bdstevel@tonic-gate#include <sys/promif.h>
567c478bdstevel@tonic-gate#include <sys/mount.h>
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate#include <net/if.h>
597c478bdstevel@tonic-gate#include <net/route.h>
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate#include <netinet/in.h>
627c478bdstevel@tonic-gate#include <netinet/arp.h>
637c478bdstevel@tonic-gate#include <netinet/dhcp.h>
647c478bdstevel@tonic-gate#include <netinet/inetutil.h>
657c478bdstevel@tonic-gate#include <dhcp_impl.h>
667c478bdstevel@tonic-gate#include <sys/sunos_dhcp_class.h>
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate#include <rpc/types.h>
697c478bdstevel@tonic-gate#include <rpc/rpc.h>
707c478bdstevel@tonic-gate#include <rpc/xdr.h>
717c478bdstevel@tonic-gate#include <rpc/auth.h>
727c478bdstevel@tonic-gate#include <rpc/clnt.h>
737c478bdstevel@tonic-gate#include <rpc/pmap_clnt.h>
747c478bdstevel@tonic-gate#include <rpc/pmap_rmt.h>
757c478bdstevel@tonic-gate#include <rpc/pmap_prot.h>
767c478bdstevel@tonic-gate#include <rpc/bootparam.h>
777c478bdstevel@tonic-gate#include <rpc/rpcb_prot.h>
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate#include <nfs/nfs.h>
807c478bdstevel@tonic-gate#include <nfs/nfs4.h>
817c478bdstevel@tonic-gate#include <nfs/nfs_clnt.h>
827c478bdstevel@tonic-gate#include <nfs/mount.h>
837c478bdstevel@tonic-gate#include <sys/mntent.h>
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate#include <sys/kstr.h>
867c478bdstevel@tonic-gate#include <sys/sunddi.h>
877c478bdstevel@tonic-gate#include <sys/sunldi.h>
887c478bdstevel@tonic-gate#include <sys/esunddi.h>
897c478bdstevel@tonic-gate
907c478bdstevel@tonic-gate#include <sys/errno.h>
917c478bdstevel@tonic-gate#include <sys/modctl.h>
927c478bdstevel@tonic-gate
937c478bdstevel@tonic-gate/*
947c478bdstevel@tonic-gate * RPC timers and retries
957c478bdstevel@tonic-gate */
967c478bdstevel@tonic-gate#define	PMAP_RETRIES	5
977c478bdstevel@tonic-gate#define	DEFAULT_RETRIES	3
987c478bdstevel@tonic-gate#define	GETFILE_RETRIES	2
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gate#define	DEFAULT_TIMEO	3
1017c478bdstevel@tonic-gate#define	WHOAMI_TIMEO	20
1027c478bdstevel@tonic-gate#define	REVARP_TIMEO	5
1037c478bdstevel@tonic-gate#define	GETFILE_TIMEO	1
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gate/*
1067c478bdstevel@tonic-gate * These are from the rpcgen'd version of mount.h XXX
1077c478bdstevel@tonic-gate */
1087c478bdstevel@tonic-gate#define	MOUNTPROG 100005
1097c478bdstevel@tonic-gate#define	MOUNTPROC_MNT		1
1107c478bdstevel@tonic-gate#define	MOUNTVERS		1
1117c478bdstevel@tonic-gate#define	MOUNTVERS_POSIX		2
1127c478bdstevel@tonic-gate#define	MOUNTVERS3		3
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gatestruct fhstatus {
1157c478bdstevel@tonic-gate	int fhs_status;
1167c478bdstevel@tonic-gate	fhandle_t fhs_fh;
1177c478bdstevel@tonic-gate};
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate#define	FHSIZE3 64
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gatestruct fhandle3 {
1227c478bdstevel@tonic-gate	uint_t fhandle3_len;
1237c478bdstevel@tonic-gate	char *fhandle3_val;
1247c478bdstevel@tonic-gate};
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gateenum mountstat3 {
1277c478bdstevel@tonic-gate	MNT_OK = 0,
1287c478bdstevel@tonic-gate	MNT3ERR_PERM = 1,
1297c478bdstevel@tonic-gate	MNT3ERR_NOENT = 2,
1307c478bdstevel@tonic-gate	MNT3ERR_IO = 5,
1317c478bdstevel@tonic-gate	MNT3ERR_ACCES = 13,
1327c478bdstevel@tonic-gate	MNT3ERR_NOTDIR = 20,
1337c478bdstevel@tonic-gate	MNT3ERR_INVAL = 22,
1347c478bdstevel@tonic-gate	MNT3ERR_NAMETOOLONG = 63,
1357c478bdstevel@tonic-gate	MNT3ERR_NOTSUPP = 10004,
1367c478bdstevel@tonic-gate	MNT3ERR_SERVERFAULT = 10006
1377c478bdstevel@tonic-gate};
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gatestruct mountres3_ok {
1407c478bdstevel@tonic-gate	struct fhandle3 fhandle;
1417c478bdstevel@tonic-gate	struct {
1427c478bdstevel@tonic-gate		uint_t auth_flavors_len;
1437c478bdstevel@tonic-gate		int *auth_flavors_val;
1447c478bdstevel@tonic-gate	} auth_flavors;
1457c478bdstevel@tonic-gate};
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gatestruct mountres3 {
1487c478bdstevel@tonic-gate	enum mountstat3 fhs_status;
1497c478bdstevel@tonic-gate	union {
1507c478bdstevel@tonic-gate		struct mountres3_ok mountinfo;
1517c478bdstevel@tonic-gate	} mountres3_u;
1527c478bdstevel@tonic-gate};
1537c478bdstevel@tonic-gate
1547c478bdstevel@tonic-gate/*
1557c478bdstevel@tonic-gate * DLPI address format.
1567c478bdstevel@tonic-gate */
1577c478bdstevel@tonic-gatestruct	dladdr {
1587c478bdstevel@tonic-gate	uchar_t		dl_phys[6];
1597c478bdstevel@tonic-gate	ushort_t	dl_sap;
1607c478bdstevel@tonic-gate};
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gatestatic struct modlmisc modlmisc = {
1637c478bdstevel@tonic-gate	&mod_miscops, "Boot diskless"
1647c478bdstevel@tonic-gate};
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gatestatic struct modlinkage modlinkage = {
1677c478bdstevel@tonic-gate	MODREV_1, (void *)&modlmisc, NULL
1687c478bdstevel@tonic-gate};
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gatestatic int	dldebug;
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gateint
1737c478bdstevel@tonic-gate_init(void)
1747c478bdstevel@tonic-gate{
1757c478bdstevel@tonic-gate	return (mod_install(&modlinkage));
1767c478bdstevel@tonic-gate}
1777c478bdstevel@tonic-gate
1787c478bdstevel@tonic-gateint
1797c478bdstevel@tonic-gate_fini(void)
1807c478bdstevel@tonic-gate{
1817c478bdstevel@tonic-gate	return (mod_remove(&modlinkage));
1827c478bdstevel@tonic-gate}
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gateint
1857c478bdstevel@tonic-gate_info(struct modinfo *modinfop)
1867c478bdstevel@tonic-gate{
1877c478bdstevel@tonic-gate	return (mod_info(&modlinkage, modinfop));
1887c478bdstevel@tonic-gate}
1897c478bdstevel@tonic-gate
190cc2b7f0dduvall
1917c478bdstevel@tonic-gatestatic enum clnt_stat	pmap_rmt_call(struct knetconfig *, struct netbuf *,
1927c478bdstevel@tonic-gate			    bool_t, rpcprog_t, rpcvers_t, rpcproc_t, xdrproc_t,
1937c478bdstevel@tonic-gate			    caddr_t, xdrproc_t, caddr_t, struct timeval,
1947c478bdstevel@tonic-gate			    struct netbuf *);
1957c478bdstevel@tonic-gatestatic bool_t		myxdr_rmtcall_args(XDR *, struct rmtcallargs *);
1967c478bdstevel@tonic-gatestatic bool_t		myxdr_rmtcallres(XDR *, struct rmtcallres *);
1977c478bdstevel@tonic-gatestatic bool_t		myxdr_pmap(XDR *, struct pmap *);
1987c478bdstevel@tonic-gatestatic bool_t		myxdr_fhstatus(XDR *xdrs, struct fhstatus *fhsp);
1997c478bdstevel@tonic-gatestatic bool_t		myxdr_fhandle(XDR *xdrs, fhandle_t *fh);
2007c478bdstevel@tonic-gatestatic bool_t		myxdr_mountres3(XDR *xdrs, struct mountres3 *objp);
2017c478bdstevel@tonic-gatestatic bool_t		myxdr_mountstat3(XDR *xdrs, enum mountstat3 *objp);
2027c478bdstevel@tonic-gatestatic bool_t		myxdr_mountres3_ok(XDR *xdrs,
2037c478bdstevel@tonic-gate			    struct mountres3_ok *objp);
2047c478bdstevel@tonic-gatestatic bool_t		myxdr_fhandle3(XDR *xdrs, struct fhandle3 *objp);
2057c478bdstevel@tonic-gatestatic enum clnt_stat	pmap_kgetport(struct knetconfig *, struct netbuf *,
2067c478bdstevel@tonic-gate			    rpcprog_t, rpcvers_t, rpcprot_t);
2077c478bdstevel@tonic-gatestatic enum clnt_stat	mycallrpc(struct knetconfig *, struct netbuf *,
2087c478bdstevel@tonic-gate			    rpcprog_t, rpcvers_t, rpcproc_t, xdrproc_t,
2097c478bdstevel@tonic-gate			    char *, xdrproc_t, char *, int, int);
2107c478bdstevel@tonic-gatestatic int		ifioctl(TIUSER *, int, struct netbuf *);
2117c478bdstevel@tonic-gatestatic int		getfile(char *, char *, struct netbuf *, char *);
2127c478bdstevel@tonic-gatestatic int		ping_prog(struct netbuf *, uint_t prog, uint_t vers,
2137c478bdstevel@tonic-gate			    int proto, enum clnt_stat *);
2147c478bdstevel@tonic-gatestatic int		mountnfs(struct netbuf *, char *, char *,
2157c478bdstevel@tonic-gate			    fhandle_t *, int *);
2167c478bdstevel@tonic-gatestatic int		mountnfs3(struct netbuf *, char *, char *,
2177c478bdstevel@tonic-gate			    nfs_fh3 *, int *);
2187c478bdstevel@tonic-gatestatic int		init_mountopts(struct nfs_args *, int,
2197c478bdstevel@tonic-gate			    struct knetconfig **, int *);
2207c478bdstevel@tonic-gatestatic int		revarp_myaddr(TIUSER *);
2217c478bdstevel@tonic-gatestatic void		revarp_start(ldi_handle_t, struct netbuf *);
2227c478bdstevel@tonic-gatestatic void		revarpinput(ldi_handle_t, struct netbuf *);
2237c478bdstevel@tonic-gatestatic void		init_netbuf(struct netbuf *);
2247c478bdstevel@tonic-gatestatic void		free_netbuf(struct netbuf *);
2257c478bdstevel@tonic-gatestatic int		rtioctl(TIUSER *, int, struct rtentry *);
2267c478bdstevel@tonic-gatestatic void		init_config(void);
2277c478bdstevel@tonic-gate
2287c478bdstevel@tonic-gatestatic void		cacheinit(void);
2297c478bdstevel@tonic-gatestatic int		cacheinfo(char *, int, struct netbuf *, char *, int);
2307c478bdstevel@tonic-gatestatic int		dlifconfig(TIUSER *, struct in_addr *, struct in_addr *,
231843e198johnlev			    struct in_addr *, uint_t);
2327c478bdstevel@tonic-gatestatic int		setifflags(TIUSER *, uint_t);
2337c478bdstevel@tonic-gate
2347c478bdstevel@tonic-gatestatic char		*inet_ntoa(struct in_addr);
2357c478bdstevel@tonic-gatestatic int		inet_aton(char *, uchar_t *);
2367c478bdstevel@tonic-gatestatic int		isdigit(int);
2377c478bdstevel@tonic-gate
2387c478bdstevel@tonic-gate/*
2397c478bdstevel@tonic-gate * Should be in some common
2407c478bdstevel@tonic-gate * ethernet source file.
2417c478bdstevel@tonic-gate */
2427c478bdstevel@tonic-gatestatic struct ether_addr etherbroadcastaddr = {
2437c478bdstevel@tonic-gate	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2447c478bdstevel@tonic-gate};
2457c478bdstevel@tonic-gate
2467c478bdstevel@tonic-gatestatic struct ether_addr myether;
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gate/*
2497c478bdstevel@tonic-gate * "ifname" is the interface name/unit as read from the boot
2507c478bdstevel@tonic-gate * arguments.
2517c478bdstevel@tonic-gate * "ndev" is the major device number of the network interface
2527c478bdstevel@tonic-gate * used to boot from.
2537c478bdstevel@tonic-gate * "ifunit" it the physical point of attachment for the network
2547c478bdstevel@tonic-gate * interface used to boot from.
2557c478bdstevel@tonic-gate *
2567c478bdstevel@tonic-gate * Both of these are initialized in "init_config()".
2577c478bdstevel@tonic-gate */
2587c478bdstevel@tonic-gate
2597c478bdstevel@tonic-gatestatic char	ifname[IFNAMSIZ];
2607c478bdstevel@tonic-gatestatic char	ndev_path[MAXPATHLEN];
2617c478bdstevel@tonic-gatestatic int	ifunit;
2627c478bdstevel@tonic-gate
2637c478bdstevel@tonic-gate/*
2647c478bdstevel@tonic-gate * XXX these should be shared
2657c478bdstevel@tonic-gate */
2667c478bdstevel@tonic-gatestatic struct knetconfig dl_udp_netconf = {
2677c478bdstevel@tonic-gate	NC_TPI_CLTS,			/* semantics */
2687c478bdstevel@tonic-gate	NC_INET,			/* family */
2697c478bdstevel@tonic-gate	NC_UDP,				/* protocol */
2707c478bdstevel@tonic-gate	0,				/* device */
2717c478bdstevel@tonic-gate};
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gatestatic struct knetconfig dl_tcp_netconf = {
2747c478bdstevel@tonic-gate	NC_TPI_COTS,			/* semantics */
2757c478bdstevel@tonic-gate	NC_INET,			/* family */
2767c478bdstevel@tonic-gate	NC_TCP,				/* protocol */
2777c478bdstevel@tonic-gate	0,				/* device */
2787c478bdstevel@tonic-gate};
2797c478bdstevel@tonic-gate
2807c478bdstevel@tonic-gate/* parameters from DHCP or bootparamd */
2817c478bdstevel@tonic-gatestatic PKT_LIST	*pl = NULL;
2827c478bdstevel@tonic-gatestatic uchar_t server_ip[4];
2837c478bdstevel@tonic-gatestatic uchar_t dhcp_server_ip[4];
2847c478bdstevel@tonic-gatestatic char *server_name_c, *server_path_c;
2857c478bdstevel@tonic-gatestatic char rootopts[256];
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gate/*
2887c478bdstevel@tonic-gate * XXX Until we get the nfsmapid deadlocks all fixed, don't allow
2897c478bdstevel@tonic-gate * XXX a v4 root mount.
2907c478bdstevel@tonic-gate */
2917c478bdstevel@tonic-gateint nfs4_no_diskless_root_support = 1;
2927c478bdstevel@tonic-gate
2937c478bdstevel@tonic-gateint
2947c478bdstevel@tonic-gatemount_root(char *name, char *path, int version, struct nfs_args *args,
295f3de0ddToomas Soome    int *vfsflags)
2967c478bdstevel@tonic-gate{
2977c478bdstevel@tonic-gate	int rc;
2987c478bdstevel@tonic-gate	int proto;
2997c478bdstevel@tonic-gate	struct knetconfig *dl_cf;
3007c478bdstevel@tonic-gate	static int init_done = 0;
3017c478bdstevel@tonic-gate	enum clnt_stat stat;
3027c478bdstevel@tonic-gate
3037c478bdstevel@tonic-gate	if (dldebug)
3047c478bdstevel@tonic-gate		printf("mount_root: name=%s\n", name);
3057c478bdstevel@tonic-gate
3067c478bdstevel@tonic-gate	if (init_done == 0) {
3077c478bdstevel@tonic-gate		init_config();
3087c478bdstevel@tonic-gate		init_done = 1;
3097c478bdstevel@tonic-gate	}
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate	init_netbuf(args->addr);
3127c478bdstevel@tonic-gate
3137c478bdstevel@tonic-gate	do {
3147c478bdstevel@tonic-gate		rc = getfile(name, args->hostname, args->addr, path);
3157c478bdstevel@tonic-gate	} while (rc == ETIMEDOUT);
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gate	if (rc) {
3187c478bdstevel@tonic-gate		free_netbuf(args->addr);
3197c478bdstevel@tonic-gate		return (rc);
3207c478bdstevel@tonic-gate	}
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gate	ASSERT(args->knconf->knc_protofmly != NULL);
3237c478bdstevel@tonic-gate	ASSERT(args->knconf->knc_proto != NULL);
3247c478bdstevel@tonic-gate
3257c478bdstevel@tonic-gate	switch (version) {
3267c478bdstevel@tonic-gate	case NFS_VERSION:
3277c478bdstevel@tonic-gate		rc = mountnfs(args->addr, args->hostname, path,
3287c478bdstevel@tonic-gate		    (fhandle_t *)args->fh, &proto);
3297c478bdstevel@tonic-gate		break;
3307c478bdstevel@tonic-gate	case NFS_V3:
3317c478bdstevel@tonic-gate		rc = mountnfs3(args->addr, args->hostname, path,
3327c478bdstevel@tonic-gate		    (nfs_fh3 *)args->fh, &proto);
3337c478bdstevel@tonic-gate		break;
3347c478bdstevel@tonic-gate	case NFS_V4:
3357c478bdstevel@tonic-gate		((struct sockaddr_in *)args->addr->buf)->sin_port =
336843e198johnlev		    htons(NFS_PORT);
3377c478bdstevel@tonic-gate		if (ping_prog(args->addr, NFS_PROGRAM, NFS_V4, IPPROTO_TCP,
338843e198johnlev		    &stat)) {
3397c478bdstevel@tonic-gate			proto = IPPROTO_TCP;
3407c478bdstevel@tonic-gate			rc = 0;
3417c478bdstevel@tonic-gate		} else {
3427c478bdstevel@tonic-gate			switch (stat) {
3437c478bdstevel@tonic-gate			case RPC_PROGVERSMISMATCH:
3447c478bdstevel@tonic-gate			case RPC_XPRTFAILED:
3457c478bdstevel@tonic-gate				/*
3467c478bdstevel@tonic-gate				 * Common failures if v4 unsupported or no TCP
3477c478bdstevel@tonic-gate				 */
3487c478bdstevel@tonic-gate				rc = EPROTONOSUPPORT;
3497c478bdstevel@tonic-gate				break;
3507c478bdstevel@tonic-gate			default:
3517c478bdstevel@tonic-gate				rc = ENXIO;
3527c478bdstevel@tonic-gate			}
3537c478bdstevel@tonic-gate		}
3547c478bdstevel@tonic-gate		if (nfs4_no_diskless_root_support)
3557c478bdstevel@tonic-gate			rc = EPROTONOSUPPORT;
3567c478bdstevel@tonic-gate		break;
3577c478bdstevel@tonic-gate	default:
3587c478bdstevel@tonic-gate		rc = EPROTONOSUPPORT;
3597c478bdstevel@tonic-gate		break;
3607c478bdstevel@tonic-gate	}
3617c478bdstevel@tonic-gate
3627c478bdstevel@tonic-gate	if (rc)
3637c478bdstevel@tonic-gate		goto errout;
3647c478bdstevel@tonic-gate
3657c478bdstevel@tonic-gate	switch (proto) {
3667c478bdstevel@tonic-gate	case IPPROTO_TCP:
3677c478bdstevel@tonic-gate		dl_cf = &dl_tcp_netconf;
3687c478bdstevel@tonic-gate		break;
3697c478bdstevel@tonic-gate	case IPPROTO_UDP:
3707c478bdstevel@tonic-gate	default:
3717c478bdstevel@tonic-gate		dl_cf = &dl_udp_netconf;
3727c478bdstevel@tonic-gate		break;
3737c478bdstevel@tonic-gate	}
3747c478bdstevel@tonic-gate
3757c478bdstevel@tonic-gate	rc = init_mountopts(args, version, &dl_cf, vfsflags);
3767c478bdstevel@tonic-gate
3777c478bdstevel@tonic-gate	/*
3787c478bdstevel@tonic-gate	 * Copy knetconfig information from the template, note that the
3797c478bdstevel@tonic-gate	 * rdev field has been set by init_config above.
3807c478bdstevel@tonic-gate	 */
3817c478bdstevel@tonic-gate	args->knconf->knc_semantics = dl_cf->knc_semantics;
3827c478bdstevel@tonic-gate	args->knconf->knc_rdev = dl_cf->knc_rdev;
3837c478bdstevel@tonic-gate	(void) strcpy(args->knconf->knc_protofmly, dl_cf->knc_protofmly);
3847c478bdstevel@tonic-gate	(void) strcpy(args->knconf->knc_proto, dl_cf->knc_proto);
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gateerrout:
3877c478bdstevel@tonic-gate	if (dldebug) {
3887c478bdstevel@tonic-gate		if (rc)
3897c478bdstevel@tonic-gate			nfs_perror(rc, "mount_root: mount %s:%s failed: %m\n",
3907c478bdstevel@tonic-gate			    args->hostname, path);
3917c478bdstevel@tonic-gate		else
3927c478bdstevel@tonic-gate			printf("mount_root: leaving\n");
3937c478bdstevel@tonic-gate	}
3947c478bdstevel@tonic-gate
3957c478bdstevel@tonic-gate	return (rc);
3967c478bdstevel@tonic-gate}
3977c478bdstevel@tonic-gate
3987c478bdstevel@tonic-gate/*
3997c478bdstevel@tonic-gate * Call mount daemon on server `sa' to mount path.
4007c478bdstevel@tonic-gate * `port' is set to nfs port and fh is the fhandle
4017c478bdstevel@tonic-gate * returned from the server.
4027c478bdstevel@tonic-gate */
4037c478bdstevel@tonic-gatestatic int
4047c478bdstevel@tonic-gatemountnfs(struct netbuf *sa, char *server,
405f3de0ddToomas Soome    char *path, fhandle_t *fh, int *proto)
4067c478bdstevel@tonic-gate{
4077c478bdstevel@tonic-gate	struct fhstatus fhs;
4087c478bdstevel@tonic-gate	enum clnt_stat stat;
4097c478bdstevel@tonic-gate
4107c478bdstevel@tonic-gate	if (dldebug)
4117c478bdstevel@tonic-gate		printf("mountnfs: entered\n");
4127c478bdstevel@tonic-gate
4137c478bdstevel@tonic-gate	/*
4147c478bdstevel@tonic-gate	 * Get the port number for the mount program.
4157c478bdstevel@tonic-gate	 * pmap_kgetport first tries a SunOS portmapper
4167c478bdstevel@tonic-gate	 * and, if no reply is received, will try a
4177c478bdstevel@tonic-gate	 * SVR4 rpcbind. Either way, `sa' is set to
4187c478bdstevel@tonic-gate	 * the correct address.
4197c478bdstevel@tonic-gate	 */
4207c478bdstevel@tonic-gate	do {
4217c478bdstevel@tonic-gate		stat = pmap_kgetport(&dl_udp_netconf, sa, (rpcprog_t)MOUNTPROG,
4227c478bdstevel@tonic-gate		    (rpcvers_t)MOUNTVERS, (rpcprot_t)IPPROTO_UDP);
4237c478bdstevel@tonic-gate
4247c478bdstevel@tonic-gate		if (stat == RPC_TIMEDOUT) {
4257c478bdstevel@tonic-gate			cmn_err(CE_WARN,
4267c478bdstevel@tonic-gate			    "mountnfs: %s:%s portmap not responding",
4277c478bdstevel@tonic-gate			    server, path);
4287c478bdstevel@tonic-gate		} else if (stat != RPC_SUCCESS) {
4297c478bdstevel@tonic-gate			cmn_err(CE_WARN,
4307c478bdstevel@tonic-gate			    "mountnfs: pmap_kgetport RPC error %d (%s).",
4317c478bdstevel@tonic-gate			    stat, clnt_sperrno(stat));
4327c478bdstevel@tonic-gate			return (ENXIO);	/* XXX */
4337c478bdstevel@tonic-gate		}
4347c478bdstevel@tonic-gate	} while (stat == RPC_TIMEDOUT);
4357c478bdstevel@tonic-gate
4367c478bdstevel@tonic-gate	/*
4377c478bdstevel@tonic-gate	 * The correct port number has been
4387c478bdstevel@tonic-gate	 * put into `sa' by pmap_kgetport().
4397c478bdstevel@tonic-gate	 */
4407c478bdstevel@tonic-gate	do {
4417c478bdstevel@tonic-gate		stat = mycallrpc(&dl_udp_netconf, sa, (rpcprog_t)MOUNTPROG,
4427c478bdstevel@tonic-gate		    (rpcvers_t)MOUNTVERS, (rpcproc_t)MOUNTPROC_MNT,
4437c478bdstevel@tonic-gate		    xdr_bp_path_t, (char *)&path,
4447c478bdstevel@tonic-gate		    myxdr_fhstatus, (char *)&fhs,
4457c478bdstevel@tonic-gate		    DEFAULT_TIMEO, DEFAULT_RETRIES);
4467c478bdstevel@tonic-gate		if (stat == RPC_TIMEDOUT) {
4477c478bdstevel@tonic-gate			cmn_err(CE_WARN,
4487c478bdstevel@tonic-gate			    "mountnfs: %s:%s mount server not responding",
4497c478bdstevel@tonic-gate			    server, path);
4507c478bdstevel@tonic-gate		}
4517c478bdstevel@tonic-gate	} while (stat == RPC_TIMEDOUT);
4527c478bdstevel@tonic-gate
4537c478bdstevel@tonic-gate	if (stat != RPC_SUCCESS) {
4547c478bdstevel@tonic-gate		cmn_err(CE_WARN, "mountnfs: RPC failed: error %d (%s).",
4557c478bdstevel@tonic-gate		    stat, clnt_sperrno(stat));
4567c478bdstevel@tonic-gate		return (ENXIO);	/* XXX */
4577c478bdstevel@tonic-gate	}
4587c478bdstevel@tonic-gate
4597c478bdstevel@tonic-gate	((struct sockaddr_in *)sa->buf)->sin_port = htons(NFS_PORT);
4607c478bdstevel@tonic-gate
4617c478bdstevel@tonic-gate	*fh = fhs.fhs_fh;
4627c478bdstevel@tonic-gate	if (fhs.fhs_status != 0) {
4637c478bdstevel@tonic-gate		if (dldebug)
4647c478bdstevel@tonic-gate			printf("mountnfs: fhs_status %d\n", fhs.fhs_status);
4657c478bdstevel@tonic-gate		return (ENXIO);		/* XXX */
4667c478bdstevel@tonic-gate	}
4677c478bdstevel@tonic-gate
4687c478bdstevel@tonic-gate	*proto = IPPROTO_UDP;
4697c478bdstevel@tonic-gate
4707c478bdstevel@tonic-gate	if (ping_prog(sa, NFS_PROGRAM, NFS_VERSION, IPPROTO_TCP, NULL))
4717c478bdstevel@tonic-gate		*proto = IPPROTO_TCP;
4727c478bdstevel@tonic-gate
4737c478bdstevel@tonic-gate	if (dldebug)
4747c478bdstevel@tonic-gate		printf("mountnfs: leaving\n");
4757c478bdstevel@tonic-gate	return (0);
4767c478bdstevel@tonic-gate}
4777c478bdstevel@tonic-gate
4787c478bdstevel@tonic-gate/*
4797c478bdstevel@tonic-gate * Call mount daemon on server `sa' to mount path.
4807c478bdstevel@tonic-gate * `port' is set to nfs port and fh is the fhandle
4817c478bdstevel@tonic-gate * returned from the server.
4827c478bdstevel@tonic-gate */
4837c478bdstevel@tonic-gatestatic int
4847c478bdstevel@tonic-gatemountnfs3(struct netbuf *sa, char *server,
485f3de0ddToomas Soome    char *path, nfs_fh3 *fh, int *proto)
4867c478bdstevel@tonic-gate{
4877c478bdstevel@tonic-gate	struct mountres3 mountres3;
4887c478bdstevel@tonic-gate	enum clnt_stat stat;
4897c478bdstevel@tonic-gate	int ret = 0;
4907c478bdstevel@tonic-gate
4917c478bdstevel@tonic-gate	if (dldebug)
4927c478bdstevel@tonic-gate		printf("mountnfs3: entered\n");
4937c478bdstevel@tonic-gate
4947c478bdstevel@tonic-gate	/*
4957c478bdstevel@tonic-gate	 * Get the port number for the mount program.
4967c478bdstevel@tonic-gate	 * pmap_kgetport first tries a SunOS portmapper
4977c478bdstevel@tonic-gate	 * and, if no reply is received, will try a
4987c478bdstevel@tonic-gate	 * SVR4 rpcbind. Either way, `sa' is set to
4997c478bdstevel@tonic-gate	 * the correct address.
5007c478bdstevel@tonic-gate	 */
5017c478bdstevel@tonic-gate	do {
5027c478bdstevel@tonic-gate		stat = pmap_kgetport(&dl_udp_netconf, sa, (rpcprog_t)MOUNTPROG,
5037c478bdstevel@tonic-gate		    (rpcvers_t)MOUNTVERS3, (rpcprot_t)IPPROTO_UDP);
5047c478bdstevel@tonic-gate
5057c478bdstevel@tonic-gate		if (stat == RPC_PROGVERSMISMATCH) {
5067c478bdstevel@tonic-gate			if (dldebug)
5077c478bdstevel@tonic-gate				printf("mountnfs3: program/version mismatch\n");
5087c478bdstevel@tonic-gate			return (EPROTONOSUPPORT); /* XXX */
5097c478bdstevel@tonic-gate		} else if (stat == RPC_TIMEDOUT) {
5107c478bdstevel@tonic-gate			cmn_err(CE_WARN,
5117c478bdstevel@tonic-gate			    "mountnfs3: %s:%s portmap not responding",
5127c478bdstevel@tonic-gate			    server, path);
5137c478bdstevel@tonic-gate		} else if (stat != RPC_SUCCESS) {
5147c478bdstevel@tonic-gate			cmn_err(CE_WARN,
5157c478bdstevel@tonic-gate			    "mountnfs3: pmap_kgetport RPC error %d (%s).",
5167c478bdstevel@tonic-gate			    stat, clnt_sperrno(stat));
5177c478bdstevel@tonic-gate			return (ENXIO);	/* XXX */
5187c478bdstevel@tonic-gate		}
5197c478bdstevel@tonic-gate	} while (stat == RPC_TIMEDOUT);
5207c478bdstevel@tonic-gate
5217c478bdstevel@tonic-gate	mountres3.mountres3_u.mountinfo.fhandle.fhandle3_val = NULL;
5227c478bdstevel@tonic-gate	mountres3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val = NULL;
5237c478bdstevel@tonic-gate
5247c478bdstevel@tonic-gate	/*
5257c478bdstevel@tonic-gate	 * The correct port number has been
5267c478bdstevel@tonic-gate	 * put into `sa' by pmap_kgetport().
5277c478bdstevel@tonic-gate	 */
5287c478bdstevel@tonic-gate	do {
5297c478bdstevel@tonic-gate		stat = mycallrpc(&dl_udp_netconf, sa, (rpcprog_t)MOUNTPROG,
5307c478bdstevel@tonic-gate		    (rpcvers_t)MOUNTVERS3, (rpcproc_t)MOUNTPROC_MNT,
5317c478bdstevel@tonic-gate		    xdr_bp_path_t, (char *)&path,
5327c478bdstevel@tonic-gate		    myxdr_mountres3, (char *)&mountres3,
5337c478bdstevel@tonic-gate		    DEFAULT_TIMEO, DEFAULT_RETRIES);
5347c478bdstevel@tonic-gate		if (stat == RPC_TIMEDOUT) {
5357c478bdstevel@tonic-gate			cmn_err(CE_WARN,
5367c478bdstevel@tonic-gate			    "mountnfs3: %s:%s mount server not responding",
5377c478bdstevel@tonic-gate			    server, path);
5387c478bdstevel@tonic-gate		}
5397c478bdstevel@tonic-gate	} while (stat == RPC_TIMEDOUT);
5407c478bdstevel@tonic-gate
5417c478bdstevel@tonic-gate	if (stat == RPC_PROGVERSMISMATCH) {
5427c478bdstevel@tonic-gate		if (dldebug)
5437c478bdstevel@tonic-gate			printf("mountnfs3: program/version mismatch\n");
5447c478bdstevel@tonic-gate		ret = EPROTONOSUPPORT;
5457c478bdstevel@tonic-gate		goto out;
5467c478bdstevel@tonic-gate	}
5477c478bdstevel@tonic-gate	if (stat != RPC_SUCCESS) {
5487c478bdstevel@tonic-gate		cmn_err(CE_WARN, "mountnfs3: RPC failed: error %d (%s).",
5497c478bdstevel@tonic-gate		    stat, clnt_sperrno(stat));
5507c478bdstevel@tonic-gate		ret = ENXIO;	/* XXX */
5517c478bdstevel@tonic-gate		goto out;
5527c478bdstevel@tonic-gate	}
5537c478bdstevel@tonic-gate
5547c478bdstevel@tonic-gate	if (mountres3.fhs_status != MNT_OK) {
5557c478bdstevel@tonic-gate		if (dldebug)
5567c478bdstevel@tonic-gate			printf("mountnfs3: fhs_status %d\n",
557843e198johnlev			    mountres3.fhs_status);
5587c478bdstevel@tonic-gate		ret = ENXIO;	/* XXX */
5597c478bdstevel@tonic-gate		goto out;
5607c478bdstevel@tonic-gate	}
5617c478bdstevel@tonic-gate
5627c478bdstevel@tonic-gate	((struct sockaddr_in *)sa->buf)->sin_port = htons(NFS_PORT);
5637c478bdstevel@tonic-gate
5647c478bdstevel@tonic-gate	*proto = IPPROTO_UDP;
5657c478bdstevel@tonic-gate
5667c478bdstevel@tonic-gate	if (ping_prog(sa, NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NULL)) {
5677c478bdstevel@tonic-gate		*proto = IPPROTO_TCP;
5687c478bdstevel@tonic-gate	}
5697c478bdstevel@tonic-gate
5707c478bdstevel@tonic-gate	fh->fh3_length = mountres3.mountres3_u.mountinfo.fhandle.fhandle3_len;
5717c478bdstevel@tonic-gate	bcopy(mountres3.mountres3_u.mountinfo.fhandle.fhandle3_val,
5727c478bdstevel@tonic-gate	    fh->fh3_u.data, fh->fh3_length);
5737c478bdstevel@tonic-gate
5747c478bdstevel@tonic-gateout:
5757c478bdstevel@tonic-gate	xdr_free(myxdr_mountres3, (caddr_t)&mountres3);
5767c478bdstevel@tonic-gate
5777c478bdstevel@tonic-gate	if (dldebug)
5787c478bdstevel@tonic-gate		printf("mountnfs3: leaving\n");
5797c478bdstevel@tonic-gate	return (ret);
5807c478bdstevel@tonic-gate}
5817c478bdstevel@tonic-gate
5827c478bdstevel@tonic-gatestatic int
5837c478bdstevel@tonic-gateping_prog(struct netbuf *call_addr, uint_t prog, uint_t vers, int proto,
584f3de0ddToomas Soome    enum clnt_stat *statp)
5857c478bdstevel@tonic-gate{
5867c478bdstevel@tonic-gate	struct knetconfig *knconf;
5877c478bdstevel@tonic-gate	enum clnt_stat stat;
5887c478bdstevel@tonic-gate	int retries = DEFAULT_RETRIES;
5897c478bdstevel@tonic-gate
5907c478bdstevel@tonic-gate	switch (proto) {
5917c478bdstevel@tonic-gate	case IPPROTO_TCP:
5927c478bdstevel@tonic-gate		knconf = &dl_tcp_netconf;
5937c478bdstevel@tonic-gate		break;
5947c478bdstevel@tonic-gate	case IPPROTO_UDP:
5957c478bdstevel@tonic-gate		knconf = &dl_udp_netconf;
5967c478bdstevel@tonic-gate		break;
5977c478bdstevel@tonic-gate	default:
5987c478bdstevel@tonic-gate		return (0);
5997c478bdstevel@tonic-gate	}
6007c478bdstevel@tonic-gate
6017c478bdstevel@tonic-gate	do {
6027c478bdstevel@tonic-gate		stat = mycallrpc(knconf, call_addr, prog, vers, NULLPROC,
6037c478bdstevel@tonic-gate		    xdr_void, NULL, xdr_void, NULL,
6047c478bdstevel@tonic-gate		    DEFAULT_TIMEO, DEFAULT_RETRIES);
6057c478bdstevel@tonic-gate
6067c478bdstevel@tonic-gate		if (dldebug)
6077c478bdstevel@tonic-gate			printf("ping_prog: %d return %d (%s)\n", proto, stat,
6087c478bdstevel@tonic-gate			    clnt_sperrno(stat));
6097c478bdstevel@tonic-gate		/*
6107c478bdstevel@tonic-gate		 * Special case for TCP, it may "timeout" because it failed
6117c478bdstevel@tonic-gate		 * to establish an initial connection but it doesn't
6127c478bdstevel@tonic-gate		 * actually retry, so we do the retry.
6137c478bdstevel@tonic-gate		 * Persistence pays in diskless.
6147c478bdstevel@tonic-gate		 */
6157c478bdstevel@tonic-gate	} while (stat == RPC_TIMEDOUT && proto == IPPROTO_TCP && retries--);
6167c478bdstevel@tonic-gate
6177c478bdstevel@tonic-gate	if (statp != NULL)
6187c478bdstevel@tonic-gate		*statp = stat;
6197c478bdstevel@tonic-gate
6207c478bdstevel@tonic-gate	if (stat != RPC_SUCCESS)
6217c478bdstevel@tonic-gate		return (0);
6227c478bdstevel@tonic-gate	return (1);
6237c478bdstevel@tonic-gate}
6247c478bdstevel@tonic-gate
6257c478bdstevel@tonic-gatestatic struct netbuf bootparam_addr;
6267c478bdstevel@tonic-gate
6277c478bdstevel@tonic-gate/*
6287c478bdstevel@tonic-gate * Returns after filling in the following global variables:
6297c478bdstevel@tonic-gate *	bootparam_addr,
6307c478bdstevel@tonic-gate *	utsname.nodename,
6317c478bdstevel@tonic-gate *	srpc_domain.
6327c478bdstevel@tonic-gate */
6337c478bdstevel@tonic-gatestatic int
6347c478bdstevel@tonic-gatewhoami(void)
6357c478bdstevel@tonic-gate{
636cc2b7f0dduvall	TIUSER *tiptr;
637cc2b7f0dduvall	struct netbuf sa;
638cc2b7f0dduvall	struct netbuf req;
639cc2b7f0dduvall	struct bp_whoami_arg arg;
640cc2b7f0dduvall	struct bp_whoami_res res;
641cc2b7f0dduvall	struct timeval tv;
642cc2b7f0dduvall	enum clnt_stat stat;
643cc2b7f0dduvall	int rc;
644cc2b7f0dduvall	size_t namelen;
645cc2b7f0dduvall	int printed_waiting_msg;
646cc2b7f0dduvall
647cc2b7f0dduvall	if ((rc = t_kopen((file_t *)NULL, dl_udp_netconf.knc_rdev,
648cc2b7f0dduvall	    FREAD|FWRITE, &tiptr, CRED())) != 0) {
6497c478bdstevel@tonic-gate		nfs_perror(rc, "whoami: t_kopen udp failed: %m.\n");
6507c478bdstevel@tonic-gate	}
6517c478bdstevel@tonic-gate
6527c478bdstevel@tonic-gate	/*
6537c478bdstevel@tonic-gate	 * Find out our local (IP) address.
6547c478bdstevel@tonic-gate	 */
6557c478bdstevel@tonic-gate	if (rc = revarp_myaddr(tiptr)) {
6567c478bdstevel@tonic-gate		nfs_perror(rc, "whoami: revarp_myaddr failed: %m.\n");
6577c478bdstevel@tonic-gate		(void) t_kclose(tiptr, 0);
6587c478bdstevel@tonic-gate		return (rc);
6597c478bdstevel@tonic-gate	}
6607c478bdstevel@tonic-gate
6617c478bdstevel@tonic-gate	/* explicitly use the limited broadcast address */
6627c478bdstevel@tonic-gate	init_netbuf(&sa);
6637c478bdstevel@tonic-gate	((struct sockaddr_in *)sa.buf)->sin_family = AF_INET;
6647c478bdstevel@tonic-gate	((struct sockaddr_in *)sa.buf)->sin_addr.s_addr =
6657c478bdstevel@tonic-gate	    htonl(INADDR_BROADCAST);
6667c478bdstevel@tonic-gate	sa.len = sizeof (struct sockaddr_in);
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate	/*
6697c478bdstevel@tonic-gate	 * Pick up our local (IP) address.
6707c478bdstevel@tonic-gate	 */
6717c478bdstevel@tonic-gate	init_netbuf(&req);
6727c478bdstevel@tonic-gate	if (rc = ifioctl(tiptr, SIOCGIFADDR, &req)) {
6737c478bdstevel@tonic-gate		nfs_perror(rc,
6747c478bdstevel@tonic-gate		    "whoami: couldn't get my IP address: %m.\n");
6757c478bdstevel@tonic-gate		free_netbuf(&sa);
6767c478bdstevel@tonic-gate		free_netbuf(&req);
6777c478bdstevel@tonic-gate		(void) t_kclose(tiptr, 0);
6787c478bdstevel@tonic-gate		return (rc);
6797c478bdstevel@tonic-gate	}
680cc2b7f0dduvall
6817c478bdstevel@tonic-gate	/*
6827c478bdstevel@tonic-gate	 * Set up the arguments expected by bootparamd.
6837c478bdstevel@tonic-gate	 */
6847c478bdstevel@tonic-gate	arg.client_address.address_type = IP_ADDR_TYPE;
6857c478bdstevel@tonic-gate	bcopy(&((struct sockaddr_in *)req.buf)->sin_addr,
6867c478bdstevel@tonic-gate	    &arg.client_address.bp_address.ip_addr, sizeof (struct in_addr));
6877c478bdstevel@tonic-gate
6887c478bdstevel@tonic-gate	free_netbuf(&req);
6897c478bdstevel@tonic-gate
6907c478bdstevel@tonic-gate	init_netbuf(&bootparam_addr);
6917c478bdstevel@tonic-gate
6927c478bdstevel@tonic-gate	/*
6937c478bdstevel@tonic-gate	 * Initial retransmission interval
6947c478bdstevel@tonic-gate	 */
6957c478bdstevel@tonic-gate	tv.tv_sec = DEFAULT_TIMEO;
6967c478bdstevel@tonic-gate	tv.tv_usec = 0;
6977c478bdstevel@tonic-gate	res.client_name = kmem_alloc(MAX_MACHINE_NAME + 1, KM_SLEEP);
6987c478bdstevel@tonic-gate	res.domain_name = kmem_alloc(MAX_MACHINE_NAME + 1, KM_SLEEP);
6997c478bdstevel@tonic-gate
7007c478bdstevel@tonic-gate	/*
7017c478bdstevel@tonic-gate	 * Do a broadcast call to find a bootparam daemon that
7027c478bdstevel@tonic-gate	 * will tell us our hostname, domainname and any
7037c478bdstevel@tonic-gate	 * router that we have to use to talk to our NFS server.
7047c478bdstevel@tonic-gate	 */
7057c478bdstevel@tonic-gate	printed_waiting_msg = 0;
7067c478bdstevel@tonic-gate	do {
7077c478bdstevel@tonic-gate		/*
7087c478bdstevel@tonic-gate		 * pmap_rmt_call will first try the SunOS portmapper
7097c478bdstevel@tonic-gate		 * and if no reply is received will then try the SVR4
7107c478bdstevel@tonic-gate		 * rpcbind.
7117c478bdstevel@tonic-gate		 * Either way, `bootparam_addr' will be set to the
7127c478bdstevel@tonic-gate		 * correct address for the bootparamd that responds.
7137c478bdstevel@tonic-gate		 */
7147c478bdstevel@tonic-gate		stat = pmap_rmt_call(&dl_udp_netconf, &sa, TRUE, BOOTPARAMPROG,
7157c478bdstevel@tonic-gate		    BOOTPARAMVERS, BOOTPARAMPROC_WHOAMI,
7167c478bdstevel@tonic-gate		    xdr_bp_whoami_arg, (caddr_t)&arg,
7177c478bdstevel@tonic-gate		    xdr_bp_whoami_res, (caddr_t)&res,
7187c478bdstevel@tonic-gate		    tv, &bootparam_addr);
7197c478bdstevel@tonic-gate		if (stat == RPC_TIMEDOUT && !printed_waiting_msg) {
7207c478bdstevel@tonic-gate			cmn_err(CE_WARN,
7217c478bdstevel@tonic-gate			    "No bootparam server responding; still trying");
7227c478bdstevel@tonic-gate			printed_waiting_msg = 1;
7237c478bdstevel@tonic-gate		}
7247c478bdstevel@tonic-gate		/*
7257c478bdstevel@tonic-gate		 * Retransmission interval for second and subsequent tries.
7267c478bdstevel@tonic-gate		 * We expect first pmap_rmt_call to retransmit and backoff to
7277c478bdstevel@tonic-gate		 * at least this value.
7287c478bdstevel@tonic-gate		 */
7297c478bdstevel@tonic-gate		tv.tv_sec = WHOAMI_TIMEO;
7307c478bdstevel@tonic-gate		tv.tv_usec = 0;
7317c478bdstevel@tonic-gate	} while (stat == RPC_TIMEDOUT);
7327c478bdstevel@tonic-gate
7337c478bdstevel@tonic-gate	if (printed_waiting_msg)
7347c478bdstevel@tonic-gate		printf("Bootparam response received\n");
7357c478bdstevel@tonic-gate
7367c478bdstevel@tonic-gate	if (stat != RPC_SUCCESS) {
7377c478bdstevel@tonic-gate		/* XXX should get real error here */
7387c478bdstevel@tonic-gate		rc = ENXIO;
7397c478bdstevel@tonic-gate		cmn_err(CE_WARN,
7407c478bdstevel@tonic-gate		    "whoami: bootparam RPC failed: error %d (%s).",
7417c478bdstevel@tonic-gate		    stat, clnt_sperrno(stat));
7427c478bdstevel@tonic-gate		goto done;
7437c478bdstevel@tonic-gate	}
744cc2b7f0dduvall
7457c478bdstevel@tonic-gate	namelen = strlen(res.client_name);
7467c478bdstevel@tonic-gate	if (namelen > sizeof (utsname.nodename)) {
7477c478bdstevel@tonic-gate		printf("whoami: hostname too long");
7487c478bdstevel@tonic-gate		rc = ENAMETOOLONG;
7497c478bdstevel@tonic-gate		goto done;
7507c478bdstevel@tonic-gate	}
7517c478bdstevel@tonic-gate	if (namelen != 0) {
7527c478bdstevel@tonic-gate		bcopy(res.client_name, &utsname.nodename, namelen);
7537c478bdstevel@tonic-gate		cmn_err(CE_CONT, "?hostname: %s\n", utsname.nodename);
7547c478bdstevel@tonic-gate	} else {
7557c478bdstevel@tonic-gate		printf("whoami: no host name\n");
7567c478bdstevel@tonic-gate		rc = ENXIO;
7577c478bdstevel@tonic-gate		goto done;
7587c478bdstevel@tonic-gate	}
7597c478bdstevel@tonic-gate
7607c478bdstevel@tonic-gate	namelen = strlen(res.domain_name);
7617c478bdstevel@tonic-gate	if (namelen != 0) {
7627c478bdstevel@tonic-gate		if (namelen > SYS_NMLN) {
7637c478bdstevel@tonic-gate			printf("whoami: domainname too long");
7647c478bdstevel@tonic-gate			rc = ENAMETOOLONG;
7657c478bdstevel@tonic-gate			goto done;
7667c478bdstevel@tonic-gate		}
7677c478bdstevel@tonic-gate		bcopy(res.domain_name, &srpc_domain, namelen);
7687c478bdstevel@tonic-gate		cmn_err(CE_CONT, "?domainname: %s\n", srpc_domain);
7697c478bdstevel@tonic-gate	} else {
7707c478bdstevel@tonic-gate		printf("whoami: no domain name\n");
7717c478bdstevel@tonic-gate	}
7727c478bdstevel@tonic-gate
7737c478bdstevel@tonic-gate	if (res.router_address.address_type == IP_ADDR_TYPE) {
7747c478bdstevel@tonic-gate		struct rtentry		rtentry;
7757c478bdstevel@tonic-gate		struct sockaddr_in	*sin;
776cc2b7f0dduvall		struct in_addr		ipaddr;
7777c478bdstevel@tonic-gate
7787c478bdstevel@tonic-gate		bcopy(&res.router_address.bp_address.ip_addr, &ipaddr,
779cc2b7f0dduvall		    sizeof (struct in_addr));
7807c478bdstevel@tonic-gate
781cc2b7f0dduvall		if (ipaddr.s_addr != (uint32_t)0) {
7827c478bdstevel@tonic-gate			sin = (struct sockaddr_in *)&rtentry.rt_dst;
7837c478bdstevel@tonic-gate			bzero(sin, sizeof (*sin));
7847c478bdstevel@tonic-gate			sin->sin_family = AF_INET;
785cc2b7f0dduvall
7867c478bdstevel@tonic-gate			sin = (struct sockaddr_in *)&rtentry.rt_gateway;
7877c478bdstevel@tonic-gate			bzero(sin, sizeof (*sin));
7887c478bdstevel@tonic-gate			sin->sin_family = AF_INET;
789cc2b7f0dduvall			sin->sin_addr.s_addr = ipaddr.s_addr;
790cc2b7f0dduvall
7917c478bdstevel@tonic-gate			rtentry.rt_flags = RTF_GATEWAY | RTF_UP;
792cc2b7f0dduvall
7937c478bdstevel@tonic-gate			if (rc = rtioctl(tiptr, SIOCADDRT, &rtentry)) {
7947c478bdstevel@tonic-gate				nfs_perror(rc,
7957c478bdstevel@tonic-gate				    "whoami: couldn't add route: %m.\n");
7967c478bdstevel@tonic-gate				goto done;
7977c478bdstevel@tonic-gate			}
7987c478bdstevel@tonic-gate		}
7997c478bdstevel@tonic-gate	} else {
8007c478bdstevel@tonic-gate		printf("whoami: unknown gateway addr family %d\n",
8017c478bdstevel@tonic-gate		    res.router_address.address_type);
8027c478bdstevel@tonic-gate	}
8037c478bdstevel@tonic-gatedone:
8047c478bdstevel@tonic-gate	kmem_free(res.client_name, MAX_MACHINE_NAME + 1);
8057c478bdstevel@tonic-gate	kmem_free(res.domain_name, MAX_MACHINE_NAME + 1);
8067c478bdstevel@tonic-gate	free_netbuf(&sa);
8077c478bdstevel@tonic-gate	(void) t_kclose(tiptr, 0);
8087c478bdstevel@tonic-gate	return (rc);
8097c478bdstevel@tonic-gate}
8107c478bdstevel@tonic-gate
8117c478bdstevel@tonic-gate/*
8127c478bdstevel@tonic-gate * Returns:
8137c478bdstevel@tonic-gate *	1) The ascii form of our root servers name in `server_name'.
8147c478bdstevel@tonic-gate *	2) Actual network address of our root server in `server_address'.
8157c478bdstevel@tonic-gate *	3) Whatever BOOTPARAMPROC_GETFILE returns for the fileid key, in
8167c478bdstevel@tonic-gate *	   `server_path'.  If fileid is "root", it is the pathname of our
8177c478bdstevel@tonic-gate *	   root on the server.
8187c478bdstevel@tonic-gate */
8197c478bdstevel@tonic-gatestatic int
8207c478bdstevel@tonic-gategetfile(char *fileid,
821f3de0ddToomas Soome    char *server_name, struct netbuf *server_address, char *server_path)
8227c478bdstevel@tonic-gate{
8237c478bdstevel@tonic-gate	struct bp_getfile_arg arg;
8247c478bdstevel@tonic-gate	struct bp_getfile_res res;
8257c478bdstevel@tonic-gate	enum clnt_stat stat;
8267c478bdstevel@tonic-gate	int root = FALSE;
8277c478bdstevel@tonic-gate	static int using_cache = FALSE;
828cc2b7f0dduvall	struct in_addr ipaddr;
8297c478bdstevel@tonic-gate	int timeo = DEFAULT_TIMEO;
8307c478bdstevel@tonic-gate	int retries = DEFAULT_RETRIES;
8317c478bdstevel@tonic-gate
8327c478bdstevel@tonic-gate	if (dldebug)
8337c478bdstevel@tonic-gate		printf("getfile: entered\n");
8347c478bdstevel@tonic-gate
8357c478bdstevel@tonic-gate	/*
8367c478bdstevel@tonic-gate	 * Call cacheinfo() to see whether we can satisfy this request by using
8377c478bdstevel@tonic-gate	 * the information cached in memory by the boot program's DHCP
8387c478bdstevel@tonic-gate	 * implementation or boot properties rather than consult BOOTPARAMS,
8397c478bdstevel@tonic-gate	 * but while preserving the semantics of getfile(). We know that
8407c478bdstevel@tonic-gate	 * the server name is SYS_NMLN in length, and server_path is
8417c478bdstevel@tonic-gate	 * MAXPATHLEN (pn_alloc).
8427c478bdstevel@tonic-gate	 */
8437c478bdstevel@tonic-gate	if (strcmp(fileid, "root") == 0) {
8447c478bdstevel@tonic-gate		if (cacheinfo(server_name, SYS_NMLN, server_address,
8457c478bdstevel@tonic-gate		    server_path, MAXPATHLEN) == 0) {
8467c478bdstevel@tonic-gate			using_cache = TRUE;
8477c478bdstevel@tonic-gate			return (0);
8487c478bdstevel@tonic-gate		}
8497c478bdstevel@tonic-gate		root = TRUE;
8507c478bdstevel@tonic-gate	}
8517c478bdstevel@tonic-gate
8527c478bdstevel@tonic-gate	/*
8537c478bdstevel@tonic-gate	 * If using cache, rootopts is already available.
8547c478bdstevel@tonic-gate	 */
8557c478bdstevel@tonic-gate	if (strcmp(fileid, "rootopts") == 0 && using_cache == TRUE) {
8567c478bdstevel@tonic-gate		return (rootopts[0] != 0 ? 0 : ENXIO);
8577c478bdstevel@tonic-gate	}
8587c478bdstevel@tonic-gate
8597c478bdstevel@tonic-gate	if (bootparam_addr.len == 0) {
8607c478bdstevel@tonic-gate		return (ENXIO);
8617c478bdstevel@tonic-gate	}
8627c478bdstevel@tonic-gate	arg.client_name = (caddr_t)&utsname.nodename;
8637c478bdstevel@tonic-gate	arg.file_id = fileid;
8647c478bdstevel@tonic-gate
8657c478bdstevel@tonic-gate	bzero(&res, sizeof (res));
8667c478bdstevel@tonic-gate	res.server_name = kmem_alloc(MAX_MACHINE_NAME + 1, KM_SLEEP);
8677c478bdstevel@tonic-gate	res.server_path = kmem_alloc(MAX_MACHINE_NAME + 1, KM_SLEEP);
8687c478bdstevel@tonic-gate
8697c478bdstevel@tonic-gate	/*
8707c478bdstevel@tonic-gate	 * If we are not looking up the root file, we are looking
8717c478bdstevel@tonic-gate	 * up a non-critical option that should timeout quickly.
8727c478bdstevel@tonic-gate	 */
8737c478bdstevel@tonic-gate	if (!root) {
8747c478bdstevel@tonic-gate		timeo = GETFILE_TIMEO;
8757c478bdstevel@tonic-gate		retries = GETFILE_RETRIES;
8767c478bdstevel@tonic-gate	}
8777c478bdstevel@tonic-gate
8787c478bdstevel@tonic-gate	/*
8797c478bdstevel@tonic-gate	 * bootparam_addr was filled in by the call to
8807c478bdstevel@tonic-gate	 * whoami(), so now send an rpc message to the
8817c478bdstevel@tonic-gate	 * bootparam daemon requesting our server information.
8827c478bdstevel@tonic-gate	 * Use UDP to talk to bootparms.
8837c478bdstevel@tonic-gate	 */
8847c478bdstevel@tonic-gate	stat = mycallrpc(&dl_udp_netconf, &bootparam_addr,
8857c478bdstevel@tonic-gate	    (rpcprog_t)BOOTPARAMPROG, (rpcvers_t)BOOTPARAMVERS,
8867c478bdstevel@tonic-gate	    (rpcproc_t)BOOTPARAMPROC_GETFILE,
8877c478bdstevel@tonic-gate	    xdr_bp_getfile_arg, (caddr_t)&arg,
8887c478bdstevel@tonic-gate	    xdr_bp_getfile_res, (caddr_t)&res,
8897c478bdstevel@tonic-gate	    timeo, retries);
8907c478bdstevel@tonic-gate
8917c478bdstevel@tonic-gate	if (stat == RPC_SUCCESS) {
8927c478bdstevel@tonic-gate		(void) strcpy(server_name, res.server_name);
8937c478bdstevel@tonic-gate		(void) strcpy(server_path, res.server_path);
8947c478bdstevel@tonic-gate	}
8957c478bdstevel@tonic-gate
8967c478bdstevel@tonic-gate	kmem_free(res.server_name, MAX_MACHINE_NAME + 1);
8977c478bdstevel@tonic-gate	kmem_free(res.server_path, MAX_MACHINE_NAME + 1);
8987c478bdstevel@tonic-gate
8997c478bdstevel@tonic-gate	if (stat != RPC_SUCCESS) {
9007c478bdstevel@tonic-gate		if (root)
9017c478bdstevel@tonic-gate			cmn_err(CE_WARN, "getfile: RPC failed: error %d (%s).",
9027c478bdstevel@tonic-gate			    stat, clnt_sperrno(stat));
9037c478bdstevel@tonic-gate		return ((stat == RPC_TIMEDOUT) ? ETIMEDOUT : ENXIO); /* XXX */
9047c478bdstevel@tonic-gate	}
9057c478bdstevel@tonic-gate
9067c478bdstevel@tonic-gate	if (*server_path == '\0')
9077c478bdstevel@tonic-gate		return (EINVAL);
9087c478bdstevel@tonic-gate
9097c478bdstevel@tonic-gate	/*
9107c478bdstevel@tonic-gate	 * If the fileid is "root", we must get back a server name, for
9117c478bdstevel@tonic-gate	 * other parameters a server name is not required
9127c478bdstevel@tonic-gate	 */
9137c478bdstevel@tonic-gate	if (!root) {
9147c478bdstevel@tonic-gate		if (dldebug)
9157c478bdstevel@tonic-gate			printf("getfile: leaving: non-root\n");
9167c478bdstevel@tonic-gate		return (0);
9177c478bdstevel@tonic-gate	}
9187c478bdstevel@tonic-gate
9197c478bdstevel@tonic-gate	if (*server_name == '\0')
9207c478bdstevel@tonic-gate		return (EINVAL);
9217c478bdstevel@tonic-gate
9227c478bdstevel@tonic-gate	switch (res.server_address.address_type) {
9237c478bdstevel@tonic-gate	case IP_ADDR_TYPE:
9247c478bdstevel@tonic-gate		/*
9257c478bdstevel@tonic-gate		 * server_address is where we will get our root
9267c478bdstevel@tonic-gate		 * from.
9277c478bdstevel@tonic-gate		 */
9287c478bdstevel@tonic-gate		((struct sockaddr_in *)server_address->buf)->sin_family =
9297c478bdstevel@tonic-gate		    AF_INET;
9307c478bdstevel@tonic-gate		bcopy(&res.server_address.bp_address.ip_addr,
9317c478bdstevel@tonic-gate		    &ipaddr, sizeof (ipaddr));
932cc2b7f0dduvall		if (ipaddr.s_addr == 0)
9337c478bdstevel@tonic-gate			return (EINVAL);
9347c478bdstevel@tonic-gate
9357c478bdstevel@tonic-gate		((struct sockaddr_in *)server_address->buf)->sin_addr.s_addr =
936cc2b7f0dduvall		    ipaddr.s_addr;
9377c478bdstevel@tonic-gate		server_address->len = sizeof (struct sockaddr_in);
9387c478bdstevel@tonic-gate		break;
9397c478bdstevel@tonic-gate
9407c478bdstevel@tonic-gate	default:
9417c478bdstevel@tonic-gate		printf("getfile: unknown address type %d\n",
9427c478bdstevel@tonic-gate		    res.server_address.address_type);
9437c478bdstevel@tonic-gate		return (EPROTONOSUPPORT);
9447c478bdstevel@tonic-gate	}
9457c478bdstevel@tonic-gate	if (dldebug)
9467c478bdstevel@tonic-gate		printf("getfile: leaving\n");
9477c478bdstevel@tonic-gate	return (0);
9487c478bdstevel@tonic-gate}
9497c478bdstevel@tonic-gate
9507c478bdstevel@tonic-gate/*
951986fd29setje * If the boot property "bootp-response" exists, then OBP performed a
9527c478bdstevel@tonic-gate * successful DHCP lease acquisition for us and left the resultant ACK packet
9537c478bdstevel@tonic-gate * encoded at that location.
9547c478bdstevel@tonic-gate *
9557c478bdstevel@tonic-gate * If no such property exists (or the information is incomplete or garbled),
9567c478bdstevel@tonic-gate * the function returns -1.
9577c478bdstevel@tonic-gate */
9587c478bdstevel@tonic-gateint
9597c478bdstevel@tonic-gatedhcpinit(void)
9607c478bdstevel@tonic-gate{
9617c478bdstevel@tonic-gate	int rc, i;
9627c478bdstevel@tonic-gate	char *p;
9637c478bdstevel@tonic-gate	struct in_addr braddr;
9647c478bdstevel@tonic-gate	struct in_addr subnet;
9657c478bdstevel@tonic-gate	DHCP_OPT *doptp;
9667c478bdstevel@tonic-gate	TIUSER *tiptr;
9677c478bdstevel@tonic-gate	struct sockaddr_in *sin;
9687c478bdstevel@tonic-gate	static int once_only = 0;
9697c478bdstevel@tonic-gate
9707c478bdstevel@tonic-gate	if (once_only == 1) {
9717c478bdstevel@tonic-gate		return (0);
9727c478bdstevel@tonic-gate	}
9737c478bdstevel@tonic-gate	once_only = 1;
9747c478bdstevel@tonic-gate
9757c478bdstevel@tonic-gate	if (dhcack == NULL) {
9767c478bdstevel@tonic-gate		return (-1);
9777c478bdstevel@tonic-gate	}
9787c478bdstevel@tonic-gate
9797c478bdstevel@tonic-gate	if (dldebug) {
9807c478bdstevel@tonic-gate		printf("dhcp:  dhcack %p, len %d\n", (void *)dhcack,
981986fd29setje		    dhcacklen);
9827c478bdstevel@tonic-gate	}
9837c478bdstevel@tonic-gate
9847c478bdstevel@tonic-gate	pl = kmem_alloc(sizeof (PKT_LIST), KM_SLEEP);
985986fd29setje	pl->len = dhcacklen;
9867c478bdstevel@tonic-gate	pl->pkt = kmem_alloc(pl->len, KM_SLEEP);
987986fd29setje	bcopy(dhcack, pl->pkt, dhcacklen);
9887c478bdstevel@tonic-gate
9897c478bdstevel@tonic-gate	/*
990986fd29setje	 * For x86, ifname is not initialized
9917c478bdstevel@tonic-gate	 * in the netinstall case and dhcack interface name is
9927c478bdstevel@tonic-gate	 * set in strplumb(). So we only copy the name if ifname
9937c478bdstevel@tonic-gate	 * is set properly.
9947c478bdstevel@tonic-gate	 */
9957c478bdstevel@tonic-gate	if (ifname[0])
996986fd29setje		(void) strlcpy(dhcifname, ifname, sizeof (dhcifname));
9977c478bdstevel@tonic-gate
9987c478bdstevel@tonic-gate	/* remember the server_ip in dhcack */
9997c478bdstevel@tonic-gate	bcopy((uchar_t *)pl->pkt + 20, dhcp_server_ip, 4);
10007c478bdstevel@tonic-gate	bzero(pl->opts, (DHCP_LAST_OPT + 1) * sizeof (DHCP_OPT *));
10017c478bdstevel@tonic-gate	bzero(pl->vs, (VS_OPTION_END - VS_OPTION_START + 1) *
10027c478bdstevel@tonic-gate	    sizeof (DHCP_OPT *));
10037c478bdstevel@tonic-gate
10047c478bdstevel@tonic-gate	if (dhcp_options_scan(pl, B_TRUE) != 0) {
10057c478bdstevel@tonic-gate		/* garbled packet */
10067c478bdstevel@tonic-gate		cmn_err(CE_WARN, "dhcp: DHCP packet parsing failed");
10077c478bdstevel@tonic-gate		kmem_free(pl->pkt, pl->len);
10087c478bdstevel@tonic-gate		kmem_free(pl, sizeof (PKT_LIST));
10097c478bdstevel@tonic-gate		pl = NULL;
10107c478bdstevel@tonic-gate		return (-1);
10117c478bdstevel@tonic-gate	}
10127c478bdstevel@tonic-gate
10137c478bdstevel@tonic-gate	/* set node name */
10147c478bdstevel@tonic-gate	if (pl->opts[CD_HOSTNAME] != NULL) {
10157c478bdstevel@tonic-gate		doptp = pl->opts[CD_HOSTNAME];
10167c478bdstevel@tonic-gate		i = doptp->len;
10177c478bdstevel@tonic-gate		if (i >= SYS_NMLN) {
10187c478bdstevel@tonic-gate			cmn_err(CE_WARN, "dhcp: Hostname is too long");
10197c478bdstevel@tonic-gate		} else {
10207c478bdstevel@tonic-gate			bcopy(doptp->value, utsname.nodename, i);
10217c478bdstevel@tonic-gate			utsname.nodename[i] = '\0';
10227c478bdstevel@tonic-gate			if (dldebug) {
10237c478bdstevel@tonic-gate				printf("hostname is %s\n",
10247c478bdstevel@tonic-gate				    utsname.nodename);
10257c478bdstevel@tonic-gate			}
10267c478bdstevel@tonic-gate		}
10277c478bdstevel@tonic-gate	}
10287c478bdstevel@tonic-gate
10297c478bdstevel@tonic-gate	/* Set NIS domain name. */
10307c478bdstevel@tonic-gate	p = NULL;
10317c478bdstevel@tonic-gate	if (pl->opts[CD_NIS_DOMAIN] != NULL) {
10327c478bdstevel@tonic-gate		doptp = pl->opts[CD_NIS_DOMAIN];
10337c478bdstevel@tonic-gate		i = doptp->len;
10347c478bdstevel@tonic-gate		p = (caddr_t)doptp->value;
10357c478bdstevel@tonic-gate	}
10367c478bdstevel@tonic-gate	if (p != NULL) {
10377c478bdstevel@tonic-gate		if (i > SYS_NMLN) {
10387c478bdstevel@tonic-gate			cmn_err(CE_WARN,
10397c478bdstevel@tonic-gate			    "dhcp: NIS domainname too long.");
10407c478bdstevel@tonic-gate		} else {
10417c478bdstevel@tonic-gate			bcopy(p, srpc_domain, i);
10427c478bdstevel@tonic-gate			srpc_domain[i] = '\0';
10437c478bdstevel@tonic-gate			if (dldebug)
10447c478bdstevel@tonic-gate				printf("dhcp: NIS domain name is %s\n",
10457c478bdstevel@tonic-gate				    srpc_domain);
10467c478bdstevel@tonic-gate		}
10477c478bdstevel@tonic-gate	}
10487c478bdstevel@tonic-gate
10497c478bdstevel@tonic-gate	/* fetch netmask */
10507c478bdstevel@tonic-gate	if (pl->opts[CD_SUBNETMASK] != NULL) {
10517c478bdstevel@tonic-gate		doptp = pl->opts[CD_SUBNETMASK];
10527c478bdstevel@tonic-gate		if (doptp->len != sizeof (struct in_addr)) {
10537c478bdstevel@tonic-gate			pl->opts[CD_SUBNETMASK] = NULL;
10547c478bdstevel@tonic-gate			cmn_err(CE_WARN, "dhcp: netmask option malformed");
10557c478bdstevel@tonic-gate		} else {
10567c478bdstevel@tonic-gate			bcopy(doptp->value, &subnet, sizeof (struct in_addr));
10577c478bdstevel@tonic-gate			if (dldebug)
10587c478bdstevel@tonic-gate				printf("dhcp:  setting netmask to: %s\n",
10597c478bdstevel@tonic-gate				    inet_ntoa(subnet));
10607c478bdstevel@tonic-gate		}
10617c478bdstevel@tonic-gate	} else {
10627c478bdstevel@tonic-gate		struct in_addr myIPaddr;
10637c478bdstevel@tonic-gate
10647c478bdstevel@tonic-gate		myIPaddr.s_addr = pl->pkt->yiaddr.s_addr;
10657c478bdstevel@tonic-gate		cmn_err(CE_WARN, "dhcp:  no subnet mask supplied - inferring");
10667c478bdstevel@tonic-gate		if (IN_CLASSA(ntohl(myIPaddr.s_addr)))
10677c478bdstevel@tonic-gate			subnet.s_addr = htonl(IN_CLASSA_NET);
10687c478bdstevel@tonic-gate		else if (IN_CLASSB(ntohl(myIPaddr.s_addr)))
10697c478bdstevel@tonic-gate			subnet.s_addr = htonl(IN_CLASSB_NET);
10707c478bdstevel@tonic-gate		else if (IN_CLASSC(ntohl(myIPaddr.s_addr)))
10717c478bdstevel@tonic-gate			subnet.s_addr = htonl(IN_CLASSC_NET);
10722a9459bsangeeta		else if (IN_CLASSD(ntohl(myIPaddr.s_addr)))
10737c478bdstevel@tonic-gate			cmn_err(CE_WARN, "dhcp:  bad IP address (%s)",
10747c478bdstevel@tonic-gate			    inet_ntoa(myIPaddr));
10752a9459bsangeeta		else
10762a9459bsangeeta			subnet.s_addr = htonl(IN_CLASSE_NET);
10777c478bdstevel@tonic-gate	}
10787c478bdstevel@tonic-gate	/* and broadcast address */
10797c478bdstevel@tonic-gate	if (pl->opts[CD_BROADCASTADDR] != NULL) {
10807c478bdstevel@tonic-gate		doptp = pl->opts[CD_BROADCASTADDR];
10817c478bdstevel@tonic-gate		if (doptp->len != sizeof (struct in_addr)) {
10827c478bdstevel@tonic-gate			pl->opts[CD_BROADCASTADDR] = NULL;
10837c478bdstevel@tonic-gate			if (dldebug)
10847c478bdstevel@tonic-gate				printf("dhcp:  broadcast address len %d\n",
10857c478bdstevel@tonic-gate				    doptp->len);
10867c478bdstevel@tonic-gate		} else {
10877c478bdstevel@tonic-gate			bcopy(doptp->value, &braddr, sizeof (struct in_addr));
10887c478bdstevel@tonic-gate			if (dldebug)
10897c478bdstevel@tonic-gate				printf("dhcp:  setting broadcast addr to: %s\n",
10907c478bdstevel@tonic-gate				    inet_ntoa(braddr));
10917c478bdstevel@tonic-gate		}
10927c478bdstevel@tonic-gate	} else {
10937c478bdstevel@tonic-gate		if (dldebug)
10947c478bdstevel@tonic-gate			printf("dhcp:  no broadcast address supplied\n");
10957c478bdstevel@tonic-gate		braddr.s_addr = htonl(INADDR_BROADCAST);
10967c478bdstevel@tonic-gate	}
10977c478bdstevel@tonic-gate	/* and plumb and initialize interface */
10987c478bdstevel@tonic-gate	if ((rc = t_kopen((file_t *)NULL, dl_udp_netconf.knc_rdev,
10997c478bdstevel@tonic-gate	    FREAD|FWRITE, &tiptr, CRED())) == 0) {
11007c478bdstevel@tonic-gate		if (rc = dlifconfig(tiptr, &pl->pkt->yiaddr, &subnet,
1101843e198johnlev		    &braddr, IFF_DHCPRUNNING)) {
11027c478bdstevel@tonic-gate			nfs_perror(rc, "dhcp: dlifconfig failed: %m\n");
11037c478bdstevel@tonic-gate			kmem_free(pl->pkt, pl->len);
11047c478bdstevel@tonic-gate			kmem_free(pl, sizeof (PKT_LIST));
11057c478bdstevel@tonic-gate			pl = NULL;
11067c478bdstevel@tonic-gate			(void) t_kclose(tiptr, 0);
11077c478bdstevel@tonic-gate			return (-1);
11087c478bdstevel@tonic-gate		}
11097c478bdstevel@tonic-gate
11107c478bdstevel@tonic-gate		/* add routes */
11117c478bdstevel@tonic-gate		if (pl->opts[CD_ROUTER] != NULL) {
11127c478bdstevel@tonic-gate			doptp = pl->opts[CD_ROUTER];
11137c478bdstevel@tonic-gate			if ((doptp->len % sizeof (struct in_addr)) != 0) {
11147c478bdstevel@tonic-gate				pl->opts[CD_ROUTER] = NULL;
11157c478bdstevel@tonic-gate			} else {
11167c478bdstevel@tonic-gate				int nrouters;
11177c478bdstevel@tonic-gate				uchar_t *tp;
11187c478bdstevel@tonic-gate
11197c478bdstevel@tonic-gate				nrouters = doptp->len / sizeof (struct in_addr);
11207c478bdstevel@tonic-gate				for (tp = doptp->value, i = 0; i < nrouters;
11217c478bdstevel@tonic-gate				    i++) {
11227c478bdstevel@tonic-gate					struct in_addr defr;
11237c478bdstevel@tonic-gate					struct rtentry	rtentry;
11247c478bdstevel@tonic-gate
11257c478bdstevel@tonic-gate					bcopy(tp, &defr,
11267c478bdstevel@tonic-gate					    sizeof (struct in_addr));
11277c478bdstevel@tonic-gate					if (defr.s_addr == 0)
11287c478bdstevel@tonic-gate						continue;
11297c478bdstevel@tonic-gate
11307c478bdstevel@tonic-gate					sin = (struct
11317c478bdstevel@tonic-gate					    sockaddr_in *)&rtentry.rt_dst;
11327c478bdstevel@tonic-gate
11337c478bdstevel@tonic-gate					bzero(sin, sizeof (*sin));
11347c478bdstevel@tonic-gate					sin->sin_family = AF_INET;
11357c478bdstevel@tonic-gate
11367c478bdstevel@tonic-gate					sin = (struct
11377c478bdstevel@tonic-gate					    sockaddr_in *)&rtentry.rt_gateway;
11387c478bdstevel@tonic-gate					bzero(sin, sizeof (*sin));
11397c478bdstevel@tonic-gate					sin->sin_family = AF_INET;
11407c478bdstevel@tonic-gate					sin->sin_addr = defr;
11417c478bdstevel@tonic-gate
11427c478bdstevel@tonic-gate					rtentry.rt_flags = RTF_GATEWAY | RTF_UP;
11437c478bdstevel@tonic-gate
11447c478bdstevel@tonic-gate					if (rc = rtioctl(tiptr, SIOCADDRT,
11457c478bdstevel@tonic-gate					    &rtentry)) {
11467c478bdstevel@tonic-gate						nfs_perror(rc,
11477c478bdstevel@tonic-gate						    "dhcp: couldn't add route "
11487c478bdstevel@tonic-gate						    "to %s: %m.\n",
11497c478bdstevel@tonic-gate						    inet_ntoa(defr));
115015c07adJohn Levon						continue;
11517c478bdstevel@tonic-gate					}
11527c478bdstevel@tonic-gate					if (dldebug) {
11537c478bdstevel@tonic-gate						printf("dhcp: added route %s\n",
11547c478bdstevel@tonic-gate						    inet_ntoa(defr));
11557c478bdstevel@tonic-gate					}
11567c478bdstevel@tonic-gate					tp += sizeof (struct in_addr);
11577c478bdstevel@tonic-gate				}
11587c478bdstevel@tonic-gate			}
11597c478bdstevel@tonic-gate		}
11607c478bdstevel@tonic-gate
11617c478bdstevel@tonic-gate		(void) t_kclose(tiptr, 0);
11627c478bdstevel@tonic-gate	}
11637c478bdstevel@tonic-gate
11647c478bdstevel@tonic-gate	if (dldebug)
11657c478bdstevel@tonic-gate		printf("dhcpinit: leaving\n");
11667c478bdstevel@tonic-gate
11677c478bdstevel@tonic-gate	return (0);
11687c478bdstevel@tonic-gate}
11697c478bdstevel@tonic-gate
11707c478bdstevel@tonic-gate/*
11717c478bdstevel@tonic-gate * Initialize nfs mount info from properties and dhcp response.
11727c478bdstevel@tonic-gate */
11737c478bdstevel@tonic-gatestatic void
11747c478bdstevel@tonic-gatecacheinit(void)
11757c478bdstevel@tonic-gate{
11767c478bdstevel@tonic-gate	char *str;
11777c478bdstevel@tonic-gate	DHCP_OPT *doptp;
11787c478bdstevel@tonic-gate
11797c478bdstevel@tonic-gate	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
11807c478bdstevel@tonic-gate	    DDI_PROP_DONTPASS, BP_SERVER_PATH, &server_path_c);
11817c478bdstevel@tonic-gate	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
11827c478bdstevel@tonic-gate	    DDI_PROP_DONTPASS, BP_SERVER_NAME, &server_name_c);
11837c478bdstevel@tonic-gate	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
11847c478bdstevel@tonic-gate	    DDI_PROP_DONTPASS, BP_SERVER_ROOTOPTS, &str) == DDI_SUCCESS) {
11857c478bdstevel@tonic-gate		(void) strncpy(rootopts, str, 255);
11867c478bdstevel@tonic-gate		ddi_prop_free(str);
11877c478bdstevel@tonic-gate	}
11887c478bdstevel@tonic-gate	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
11897c478bdstevel@tonic-gate	    DDI_PROP_DONTPASS, BP_SERVER_IP, &str) == DDI_SUCCESS) {
11907c478bdstevel@tonic-gate		if (inet_aton(str, server_ip) != 0)
1191f3de0ddToomas Soome			cmn_err(CE_NOTE, "server_ipaddr %s is invalid",
11927c478bdstevel@tonic-gate			    str);
11937c478bdstevel@tonic-gate		ddi_prop_free(str);
11947c478bdstevel@tonic-gate		if (dldebug)
11957c478bdstevel@tonic-gate			printf("server ip is %s\n",
11967c478bdstevel@tonic-gate			    inet_ntoa(*(struct in_addr *)server_ip));
11977c478bdstevel@tonic-gate	}
11987c478bdstevel@tonic-gate
11997c478bdstevel@tonic-gate	if (pl == NULL)
12007c478bdstevel@tonic-gate		return;
12017c478bdstevel@tonic-gate
12027c478bdstevel@tonic-gate	/* extract root path in server_path */
12037c478bdstevel@tonic-gate	if (server_path_c == NULL) {
12047c478bdstevel@tonic-gate		doptp = pl->vs[VS_NFSMNT_ROOTPATH];
1205f3de0ddToomas Soome		if (doptp == NULL)
1206f3de0ddToomas Soome			doptp = pl->opts[CD_ROOT_PATH];
12077c478bdstevel@tonic-gate		if (doptp != NULL) {
12084837313Toomas Soome			int len, size;
12094837313Toomas Soome			uint8_t c, *source;
12104837313Toomas Soome
1211f3de0ddToomas Soome			str = NULL;
12124837313Toomas Soome			source = doptp->value;
12134837313Toomas Soome			size = doptp->len;
12144837313Toomas Soome			c = ':';
12154837313Toomas Soome
12164837313Toomas Soome			/*
12174837313Toomas Soome			 * We have to consider three cases for root path:
12184837313Toomas Soome			 * "nfs://server_ip/path"
12194837313Toomas Soome			 * "server_ip:/path"
12204837313Toomas Soome			 * "/path"
12214837313Toomas Soome			 */
12224837313Toomas Soome			if (bcmp(source, "nfs://", 6) == 0) {
12234837313Toomas Soome				source += 6;
12244837313Toomas Soome				size -= 6;
12254837313Toomas Soome				c = '/';
12264837313Toomas Soome			}
12274837313Toomas Soome			/*
12284837313Toomas Soome			 * Search for next char after ':' or first '/'.
12294837313Toomas Soome			 * Note, the '/' is part of the path, but we do
12304837313Toomas Soome			 * not need to preserve the ':'.
12314837313Toomas Soome			 */
12324837313Toomas Soome			for (len = 0; len < size; len++) {
12334837313Toomas Soome				if (source[len] == c) {
12344837313Toomas Soome					if (c == ':') {
12354837313Toomas Soome						str = (char *)(&source[++len]);
12364837313Toomas Soome					} else {
12374837313Toomas Soome						str = (char *)(&source[len++]);
12384837313Toomas Soome						size++;
12394837313Toomas Soome					}
1240f3de0ddToomas Soome					break;
1241f3de0ddToomas Soome				}
1242f3de0ddToomas Soome			}
1243f3de0ddToomas Soome			if (str != NULL) {
1244f3de0ddToomas Soome				/* Do not override server_ip from property. */
1245f3de0ddToomas Soome				if ((*(uint_t *)server_ip) == 0) {
1246f3de0ddToomas Soome					char *ip = kmem_alloc(len, KM_SLEEP);
12474837313Toomas Soome					bcopy(source, ip, len);
1248f3de0ddToomas Soome					ip[len - 1] = '\0';
1249f3de0ddToomas Soome					if (inet_aton((ip), server_ip) != 0) {
1250f3de0ddToomas Soome						cmn_err(CE_NOTE,
1251f3de0ddToomas Soome						    "server_ipaddr %s is "
1252f3de0ddToomas Soome						    "invalid", ip);
1253f3de0ddToomas Soome					}
1254f3de0ddToomas Soome					kmem_free(ip, len);
1255f3de0ddToomas Soome					if (dldebug) {
1256f3de0ddToomas Soome						printf("server ip is %s\n",
1257f3de0ddToomas Soome						    inet_ntoa(
1258f3de0ddToomas Soome						    *(struct in_addr *)
1259f3de0ddToomas Soome						    server_ip));
1260f3de0ddToomas Soome					}
1261f3de0ddToomas Soome				}
12624837313Toomas Soome				len = size - len;
1263f3de0ddToomas Soome			} else {
1264f3de0ddToomas Soome				str = (char *)doptp->value;
1265f3de0ddToomas Soome				len = doptp->len;
1266f3de0ddToomas Soome			}
1267f3de0ddToomas Soome			server_path_c = kmem_alloc(len + 1, KM_SLEEP);
1268f3de0ddToomas Soome			bcopy(str, server_path_c, len);
1269f3de0ddToomas Soome			server_path_c[len] = '\0';
12707c478bdstevel@tonic-gate			if (dldebug)
12717c478bdstevel@tonic-gate				printf("dhcp:  root path %s\n", server_path_c);
12727c478bdstevel@tonic-gate		} else {
12737c478bdstevel@tonic-gate			cmn_err(CE_WARN, "dhcp: root server path missing");
12747c478bdstevel@tonic-gate		}
12757c478bdstevel@tonic-gate	}
12767c478bdstevel@tonic-gate
12777c478bdstevel@tonic-gate	/* set server_name */
12787c478bdstevel@tonic-gate	if (server_name_c == NULL) {
12797c478bdstevel@tonic-gate		doptp = pl->vs[VS_NFSMNT_ROOTSRVR_NAME];
12807c478bdstevel@tonic-gate		if (doptp != NULL) {
12817c478bdstevel@tonic-gate			server_name_c = kmem_alloc(doptp->len + 1, KM_SLEEP);
12827c478bdstevel@tonic-gate			bcopy(doptp->value, server_name_c, doptp->len);
12837c478bdstevel@tonic-gate			server_name_c[doptp->len] = '\0';
12847c478bdstevel@tonic-gate			if (dldebug)
12857c478bdstevel@tonic-gate				printf("dhcp: root server name %s\n",
12867c478bdstevel@tonic-gate				    server_name_c);
12877c478bdstevel@tonic-gate		} else {
12887c478bdstevel@tonic-gate			cmn_err(CE_WARN, "dhcp: root server name missing");
12897c478bdstevel@tonic-gate		}
12907c478bdstevel@tonic-gate	}
12917c478bdstevel@tonic-gate
12927c478bdstevel@tonic-gate	/* set root server_address */
12937c478bdstevel@tonic-gate	if ((*(uint_t *)server_ip) == 0) {
12947c478bdstevel@tonic-gate		doptp = pl->vs[VS_NFSMNT_ROOTSRVR_IP];
12957c478bdstevel@tonic-gate		if (doptp) {
12967c478bdstevel@tonic-gate			bcopy(doptp->value, server_ip, sizeof (server_ip));
12977c478bdstevel@tonic-gate			if (dldebug) {
12987c478bdstevel@tonic-gate				printf("dhcp:  root server IP address %s\n",
12997c478bdstevel@tonic-gate				    inet_ntoa(*(struct in_addr *)server_ip));
13007c478bdstevel@tonic-gate			}
13017c478bdstevel@tonic-gate		} else {
13027c478bdstevel@tonic-gate			if (dldebug)
13037c478bdstevel@tonic-gate				cmn_err(CE_CONT,
13047c478bdstevel@tonic-gate				    "dhcp: file server ip address missing,"
13057c478bdstevel@tonic-gate				    " fallback to dhcp server as file server");
13067c478bdstevel@tonic-gate			bcopy(dhcp_server_ip, server_ip, sizeof (server_ip));
13077c478bdstevel@tonic-gate		}
13087c478bdstevel@tonic-gate	}
13097c478bdstevel@tonic-gate
13107c478bdstevel@tonic-gate	/* set root file system mount options */
13117c478bdstevel@tonic-gate	if (rootopts[0] == 0) {
13127c478bdstevel@tonic-gate		doptp = pl->vs[VS_NFSMNT_ROOTOPTS];
13137c478bdstevel@tonic-gate		if (doptp != NULL && doptp->len < 255) {
13147c478bdstevel@tonic-gate			bcopy(doptp->value, rootopts, doptp->len);
13157c478bdstevel@tonic-gate			rootopts[doptp->len] = '\0';
13167c478bdstevel@tonic-gate			if (dldebug)
13177c478bdstevel@tonic-gate				printf("dhcp:  rootopts %s\n", rootopts);
13187c478bdstevel@tonic-gate		} else if (dldebug) {
13197c478bdstevel@tonic-gate			printf("dhcp:  no rootopts or too long\n");
13207c478bdstevel@tonic-gate			/* not an error */
13217c478bdstevel@tonic-gate		}
13227c478bdstevel@tonic-gate	}
13237c478bdstevel@tonic-gate
13247c478bdstevel@tonic-gate	/* now we are done with pl, just free it */
13257c478bdstevel@tonic-gate	kmem_free(pl->pkt, pl->len);
13267c478bdstevel@tonic-gate	kmem_free(pl, sizeof (PKT_LIST));
13277c478bdstevel@tonic-gate	pl = NULL;
13287c478bdstevel@tonic-gate}
13297c478bdstevel@tonic-gate
13307c478bdstevel@tonic-gatestatic int
13317c478bdstevel@tonic-gatecacheinfo(char *name, int namelen,
13327c478bdstevel@tonic-gate    struct netbuf *server_address, char *rootpath, int pathlen)
13337c478bdstevel@tonic-gate{
13347c478bdstevel@tonic-gate	static int init_done = 0;
13357c478bdstevel@tonic-gate	struct sockaddr_in *sin;
13367c478bdstevel@tonic-gate
13377c478bdstevel@tonic-gate	if (init_done == 0) {
13387c478bdstevel@tonic-gate		cacheinit();
13397c478bdstevel@tonic-gate		init_done = 1;
13407c478bdstevel@tonic-gate	}
13417c478bdstevel@tonic-gate
13427c478bdstevel@tonic-gate	/* server_path is a reliable indicator of cache availability */
13437c478bdstevel@tonic-gate	if (server_path_c == NULL)
13447c478bdstevel@tonic-gate		return (-1);
13457c478bdstevel@tonic-gate
13467c478bdstevel@tonic-gate	(void) strncpy(rootpath, server_path_c, pathlen);
13477c478bdstevel@tonic-gate	if (server_name_c) {
13487c478bdstevel@tonic-gate		(void) strncpy(name, server_name_c, namelen);
13497c478bdstevel@tonic-gate	} else {
13507c478bdstevel@tonic-gate		(void) strncpy(name, "unknown", namelen);
13517c478bdstevel@tonic-gate	}
13527c478bdstevel@tonic-gate
13537c478bdstevel@tonic-gate	sin = (struct sockaddr_in *)server_address->buf;
13547c478bdstevel@tonic-gate	sin->sin_family = AF_INET;
13557c478bdstevel@tonic-gate	server_address->len = sizeof (struct sockaddr_in);
13567c478bdstevel@tonic-gate	bcopy(server_ip, &sin->sin_addr, sizeof (struct in_addr));
13577c478bdstevel@tonic-gate	return (0);
13587c478bdstevel@tonic-gate}
13597c478bdstevel@tonic-gate
13607c478bdstevel@tonic-gate/*
13617c478bdstevel@tonic-gate *	Set this interface's IP address and netmask, and bring it up.
13627c478bdstevel@tonic-gate */
13637c478bdstevel@tonic-gatestatic int
13647c478bdstevel@tonic-gatedlifconfig(TIUSER *tiptr, struct in_addr *myIPaddr, struct in_addr *mymask,
1365843e198johnlev    struct in_addr *mybraddr, uint_t flags)
13667c478bdstevel@tonic-gate{
13677c478bdstevel@tonic-gate	int rc;
13687c478bdstevel@tonic-gate	struct netbuf sbuf;
13697c478bdstevel@tonic-gate	struct sockaddr_in sin;
13707c478bdstevel@tonic-gate
13717c478bdstevel@tonic-gate	if (dldebug) {
13727c478bdstevel@tonic-gate		printf("dlifconfig:  entered\n");
13737c478bdstevel@tonic-gate		printf("dlifconfig:  addr %s\n", inet_ntoa(*myIPaddr));
13747c478bdstevel@tonic-gate		printf("dlifconfig:  mask %s\n", inet_ntoa(*mymask));
13757c478bdstevel@tonic-gate		printf("dlifconfig:  broadcast %s\n", inet_ntoa(*mybraddr));
13767c478bdstevel@tonic-gate	}
13777c478bdstevel@tonic-gate
13787c478bdstevel@tonic-gate	bcopy(myIPaddr, &sin.sin_addr, sizeof (struct in_addr));
13797c478bdstevel@tonic-gate	sin.sin_family = AF_INET;
13807c478bdstevel@tonic-gate	sbuf.buf = (caddr_t)&sin;
13817c478bdstevel@tonic-gate	sbuf.maxlen = sbuf.len = sizeof (sin);
13827c478bdstevel@tonic-gate	if (rc = ifioctl(tiptr, SIOCSIFADDR, &sbuf)) {
13837c478bdstevel@tonic-gate		nfs_perror(rc,
13847c478bdstevel@tonic-gate		    "dlifconfig: couldn't set interface net address: %m\n");
13857c478bdstevel@tonic-gate		return (rc);
13867c478bdstevel@tonic-gate	}
13877c478bdstevel@tonic-gate
13887c478bdstevel@tonic-gate	if (mybraddr->s_addr != INADDR_BROADCAST) {
13897c478bdstevel@tonic-gate		bcopy(mybraddr, &sin.sin_addr, sizeof (struct in_addr));
13907c478bdstevel@tonic-gate		sin.sin_family = AF_INET;
13917c478bdstevel@tonic-gate		sbuf.buf = (caddr_t)&sin;
13927c478bdstevel@tonic-gate		sbuf.maxlen = sbuf.len = sizeof (sin);
13937c478bdstevel@tonic-gate		if (rc = ifioctl(tiptr, SIOCSIFBRDADDR, &sbuf)) {
13947c478bdstevel@tonic-gate			nfs_perror(rc,
13957c478bdstevel@tonic-gate		    "dlifconfig: couldn't set interface broadcast addr: %m\n");
13967c478bdstevel@tonic-gate			return (rc);
13977c478bdstevel@tonic-gate		}
13987c478bdstevel@tonic-gate	}
13997c478bdstevel@tonic-gate
14007c478bdstevel@tonic-gate	bcopy(mymask, &sin.sin_addr, sizeof (struct in_addr));
14017c478bdstevel@tonic-gate	sin.sin_family = AF_INET;
14027c478bdstevel@tonic-gate	sbuf.buf = (caddr_t)&sin;
14037c478bdstevel@tonic-gate	sbuf.maxlen = sbuf.len = sizeof (sin);
14047c478bdstevel@tonic-gate	if (rc = ifioctl(tiptr, SIOCSIFNETMASK, &sbuf)) {
14057c478bdstevel@tonic-gate		nfs_perror(rc,
14067c478bdstevel@tonic-gate		    "dlifconfig: couldn't set interface net address: %m\n");
14077c478bdstevel@tonic-gate		return (rc);
14087c478bdstevel@tonic-gate	}
14097c478bdstevel@tonic-gate
14107c478bdstevel@tonic-gate	/*
14117c478bdstevel@tonic-gate	 * Now turn on the interface.
14127c478bdstevel@tonic-gate	 */
1413843e198johnlev	if (rc = setifflags(tiptr, IFF_UP | flags)) {
14147c478bdstevel@tonic-gate		nfs_perror(rc,
14157c478bdstevel@tonic-gate		    "dlifconfig: couldn't enable network interface: %m\n");
14167c478bdstevel@tonic-gate		return (rc);
14177c478bdstevel@tonic-gate	}
14187c478bdstevel@tonic-gate
14197c478bdstevel@tonic-gate	if (dldebug)
14207c478bdstevel@tonic-gate		printf("dlifconfig:  returned\n");
14217c478bdstevel@tonic-gate	return (0);
14227c478bdstevel@tonic-gate}
14237c478bdstevel@tonic-gate
14247c478bdstevel@tonic-gatestatic char *
14257c478bdstevel@tonic-gateinet_ntoa(struct in_addr in)
14267c478bdstevel@tonic-gate{
14277c478bdstevel@tonic-gate	static char b[18];
14287c478bdstevel@tonic-gate	unsigned char *p;
14297c478bdstevel@tonic-gate
14307c478bdstevel@tonic-gate	p = (unsigned char *)&in;
14317c478bdstevel@tonic-gate	(void) sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
14327c478bdstevel@tonic-gate	return (b);
14337c478bdstevel@tonic-gate}
14347c478bdstevel@tonic-gate
14357c478bdstevel@tonic-gate/* We only deal with a.b.c.d decimal format. ip points to 4 byte storage */
14367c478bdstevel@tonic-gatestatic int
14377c478bdstevel@tonic-gateinet_aton(char *ipstr, uchar_t *ip)
14387c478bdstevel@tonic-gate{
14397c478bdstevel@tonic-gate	int i = 0;
14407c478bdstevel@tonic-gate	uchar_t val[4] = {0};
14417c478bdstevel@tonic-gate	char c = *ipstr;
14427c478bdstevel@tonic-gate
14437c478bdstevel@tonic-gate	for (;;) {
14447c478bdstevel@tonic-gate		if (!isdigit(c))
14457c478bdstevel@tonic-gate			return (-1);
14467c478bdstevel@tonic-gate		for (;;) {
14477c478bdstevel@tonic-gate			if (!isdigit(c))
14487c478bdstevel@tonic-gate				break;
14497c478bdstevel@tonic-gate			val[i] = val[i] * 10 + (c - '0');
14507c478bdstevel@tonic-gate			c = *++ipstr;
14517c478bdstevel@tonic-gate		}
14527c478bdstevel@tonic-gate		i++;
14537c478bdstevel@tonic-gate		if (i == 4)
14547c478bdstevel@tonic-gate			break;
14557c478bdstevel@tonic-gate		if (c != '.')
14567c478bdstevel@tonic-gate			return (-1);
14577c478bdstevel@tonic-gate		c = *++ipstr;
14587c478bdstevel@tonic-gate	}
14597c478bdstevel@tonic-gate	if (c != 0)
14607c478bdstevel@tonic-gate		return (-1);
14617c478bdstevel@tonic-gate	bcopy(val, ip, 4);
14627c478bdstevel@tonic-gate	return (0);
14637c478bdstevel@tonic-gate}
14647c478bdstevel@tonic-gate
14657c478bdstevel@tonic-gate#define	MAX_ADDR_SIZE	128
14667c478bdstevel@tonic-gate
14677c478bdstevel@tonic-gate/*
14687c478bdstevel@tonic-gate * Initialize a netbuf suitable for
14697c478bdstevel@tonic-gate * describing an address for the
14707c478bdstevel@tonic-gate * transport defined by `tiptr'.
14717c478bdstevel@tonic-gate */
14727c478bdstevel@tonic-gatestatic void
14737c478bdstevel@tonic-gateinit_netbuf(struct netbuf *nbuf)
14747c478bdstevel@tonic-gate{
14757c478bdstevel@tonic-gate	nbuf->buf = kmem_zalloc(MAX_ADDR_SIZE, KM_SLEEP);
14767c478bdstevel@tonic-gate	nbuf->maxlen = MAX_ADDR_SIZE;
14777c478bdstevel@tonic-gate	nbuf->len = 0;
14787c478bdstevel@tonic-gate}
14797c478bdstevel@tonic-gate
14807c478bdstevel@tonic-gatestatic void
14817c478bdstevel@tonic-gatefree_netbuf(struct netbuf *nbuf)
14827c478bdstevel@tonic-gate{
14837c478bdstevel@tonic-gate	kmem_free(nbuf->buf, nbuf->maxlen);
14847c478bdstevel@tonic-gate	nbuf->buf = NULL;
14857c478bdstevel@tonic-gate	nbuf->maxlen = 0;
14867c478bdstevel@tonic-gate	nbuf->len = 0;
14877c478bdstevel@tonic-gate}
14887c478bdstevel@tonic-gate
14897c478bdstevel@tonic-gatestatic int
14907c478bdstevel@tonic-gatertioctl(TIUSER *tiptr, int cmd, struct rtentry *rtentry)
14917c478bdstevel@tonic-gate{
14927c478bdstevel@tonic-gate	struct strioctl iocb;
14937c478bdstevel@tonic-gate	int rc;
14947c478bdstevel@tonic-gate	vnode_t *vp;
14957c478bdstevel@tonic-gate
14967c478bdstevel@tonic-gate	iocb.ic_cmd = cmd;
14977c478bdstevel@tonic-gate	iocb.ic_timout = 0;
14987c478bdstevel@tonic-gate	iocb.ic_len = sizeof (struct rtentry);
14997c478bdstevel@tonic-gate	iocb.ic_dp = (caddr_t)rtentry;
15007c478bdstevel@tonic-gate
15017c478bdstevel@tonic-gate	vp = tiptr->fp->f_vnode;
15027c478bdstevel@tonic-gate	rc = kstr_ioctl(vp, I_STR, (intptr_t)&iocb);
15037c478bdstevel@tonic-gate	if (rc)
15047c478bdstevel@tonic-gate		nfs_perror(rc, "rtioctl: kstr_ioctl failed: %m\n");
15057c478bdstevel@tonic-gate	return (rc);
15067c478bdstevel@tonic-gate}
15077c478bdstevel@tonic-gate
15087c478bdstevel@tonic-gate/*
15097c478bdstevel@tonic-gate * Send an ioctl down the stream defined
15107c478bdstevel@tonic-gate * by `tiptr'.
15117c478bdstevel@tonic-gate *
15127c478bdstevel@tonic-gate * We isolate the ifreq dependencies in here. The
15137c478bdstevel@tonic-gate * ioctl really ought to take a netbuf and be of
15147c478bdstevel@tonic-gate * type TRANSPARENT - one day.
15157c478bdstevel@tonic-gate */
15167c478bdstevel@tonic-gatestatic int
15177c478bdstevel@tonic-gateifioctl(TIUSER *tiptr, int cmd, struct netbuf *nbuf)
15187c478bdstevel@tonic-gate{
15197c478bdstevel@tonic-gate	struct strioctl iocb;
15207c478bdstevel@tonic-gate	int rc;
15217c478bdstevel@tonic-gate	vnode_t *vp;
15227c478bdstevel@tonic-gate	struct ifreq ifr;
15237c478bdstevel@tonic-gate
15247c478bdstevel@tonic-gate	/*
15257c478bdstevel@tonic-gate	 * Now do the one requested.
15267c478bdstevel@tonic-gate	 */
15277c478bdstevel@tonic-gate	if (nbuf->len)
15287c478bdstevel@tonic-gate		ifr.ifr_addr = *(struct sockaddr *)nbuf->buf;
15297c478bdstevel@tonic-gate	(void) strncpy((caddr_t)&ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
15307c478bdstevel@tonic-gate	iocb.ic_cmd = cmd;
15317c478bdstevel@tonic-gate	iocb.ic_timout = 0;
15327c478bdstevel@tonic-gate	iocb.ic_len = sizeof (ifr);
15337c478bdstevel@tonic-gate	iocb.ic_dp = (caddr_t)&ifr;
15347c478bdstevel@tonic-gate
15357c478bdstevel@tonic-gate	vp = tiptr->fp->f_vnode;
15367c478bdstevel@tonic-gate	rc = kstr_ioctl(vp, I_STR, (intptr_t)&iocb);
15377c478bdstevel@tonic-gate	if (rc) {
15387c478bdstevel@tonic-gate		nfs_perror(rc, "ifioctl: kstr_ioctl failed: %m\n");
15397c478bdstevel@tonic-gate		return (rc);
15407c478bdstevel@tonic-gate	}
15417c478bdstevel@tonic-gate
15427c478bdstevel@tonic-gate	/*
15437c478bdstevel@tonic-gate	 * Set reply length.
15447c478bdstevel@tonic-gate	 */
15457c478bdstevel@tonic-gate	if (nbuf->len == 0) {
15467c478bdstevel@tonic-gate		/*
15477c478bdstevel@tonic-gate		 * GET type.
15487c478bdstevel@tonic-gate		 */
15497c478bdstevel@tonic-gate		nbuf->len = sizeof (struct sockaddr);
15507c478bdstevel@tonic-gate		*(struct sockaddr *)nbuf->buf = ifr.ifr_addr;
15517c478bdstevel@tonic-gate	}
15527c478bdstevel@tonic-gate
15537c478bdstevel@tonic-gate	return (0);
15547c478bdstevel@tonic-gate}
15557c478bdstevel@tonic-gate
15567c478bdstevel@tonic-gatestatic int
15577c478bdstevel@tonic-gatesetifflags(TIUSER *tiptr, uint_t value)
15587c478bdstevel@tonic-gate{
15597c478bdstevel@tonic-gate	struct ifreq ifr;
15607c478bdstevel@tonic-gate	int rc;
15617c478bdstevel@tonic-gate	struct strioctl iocb;
15627c478bdstevel@tonic-gate
15637c478bdstevel@tonic-gate	(void) strncpy((caddr_t)&ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
15647c478bdstevel@tonic-gate	iocb.ic_cmd = SIOCGIFFLAGS;
15657c478bdstevel@tonic-gate	iocb.ic_timout = 0;
15667c478bdstevel@tonic-gate	iocb.ic_len = sizeof (ifr);
15677c478bdstevel@tonic-gate	iocb.ic_dp = (caddr_t)&ifr;
15687c478bdstevel@tonic-gate	if (rc = kstr_ioctl(tiptr->fp->f_vnode, I_STR, (intptr_t)&iocb))
15697c478bdstevel@tonic-gate		return (rc);
15707c478bdstevel@tonic-gate
15717c478bdstevel@tonic-gate	ifr.ifr_flags |= value;
15727c478bdstevel@tonic-gate	iocb.ic_cmd = SIOCSIFFLAGS;
15737c478bdstevel@tonic-gate	return (kstr_ioctl(tiptr->fp->f_vnode, I_STR, (intptr_t)&iocb));
15747c478bdstevel@tonic-gate}
15757c478bdstevel@tonic-gate
15767c478bdstevel@tonic-gate/*
15777c478bdstevel@tonic-gate * REVerse Address Resolution Protocol (revarp)
15787c478bdstevel@tonic-gate * is used by a diskless client to find out its
15797c478bdstevel@tonic-gate * IP address when all it knows is its Ethernet address.
15807c478bdstevel@tonic-gate *
15817c478bdstevel@tonic-gate * Open the ethernet driver, attach and bind
15827c478bdstevel@tonic-gate * (DL_BIND_REQ) it, and then format a broadcast RARP
15837c478bdstevel@tonic-gate * message for it to send. We pick up the reply and
15847c478bdstevel@tonic-gate * let the caller set the interface address using SIOCSIFADDR.
15857c478bdstevel@tonic-gate */
15867c478bdstevel@tonic-gatestatic int
15877c478bdstevel@tonic-gaterevarp_myaddr(TIUSER *tiptr)
15887c478bdstevel@tonic-gate{
15897c478bdstevel@tonic-gate	int			rc;
15907c478bdstevel@tonic-gate	dl_info_ack_t		info;
15917c478bdstevel@tonic-gate	struct sockaddr_in	sin;
15927c478bdstevel@tonic-gate	struct netbuf		sbuf;
15937c478bdstevel@tonic-gate	ldi_handle_t		lh;
15947c478bdstevel@tonic-gate	ldi_ident_t		li;
15957c478bdstevel@tonic-gate	struct netbuf		myaddr = {0, 0, NULL};
15967c478bdstevel@tonic-gate
15977c478bdstevel@tonic-gate	if (dldebug)
15987c478bdstevel@tonic-gate		printf("revarp_myaddr: entered\n");
15997c478bdstevel@tonic-gate
16007c478bdstevel@tonic-gate	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
16017c478bdstevel@tonic-gate		nfs_perror(rc,
16027c478bdstevel@tonic-gate		    "revarp_myaddr: ldi_ident_from_mod failed: %m\n");
16037c478bdstevel@tonic-gate		return (rc);
16047c478bdstevel@tonic-gate	}
16057c478bdstevel@tonic-gate
16067c478bdstevel@tonic-gate	rc = ldi_open_by_name(ndev_path, FREAD|FWRITE, CRED(), &lh, li);
16077c478bdstevel@tonic-gate	ldi_ident_release(li);
16087c478bdstevel@tonic-gate	if (rc) {
16097c478bdstevel@tonic-gate		nfs_perror(rc,
16107c478bdstevel@tonic-gate		    "revarp_myaddr: ldi_open_by_name failed: %m\n");
16117c478bdstevel@tonic-gate		return (rc);
16127c478bdstevel@tonic-gate	}
16137c478bdstevel@tonic-gate
1614d62bc4byz	if (rc = dl_attach(lh, ifunit, NULL)) {
16157c478bdstevel@tonic-gate		nfs_perror(rc, "revarp_myaddr: dl_attach failed: %m\n");
16167c478bdstevel@tonic-gate		(void) ldi_close(lh, FREAD|FWRITE, CRED());
16177c478bdstevel@tonic-gate		return (rc);
16187c478bdstevel@tonic-gate	}
16197c478bdstevel@tonic-gate
1620d62bc4byz	if (rc = dl_bind(lh, ETHERTYPE_REVARP, NULL)) {
16217c478bdstevel@tonic-gate		nfs_perror(rc, "revarp_myaddr: dl_bind failed: %m\n");
16227c478bdstevel@tonic-gate		(void) ldi_close(lh, FREAD|FWRITE, CRED());
16237c478bdstevel@tonic-gate		return (rc);
16247c478bdstevel@tonic-gate	}
16257c478bdstevel@tonic-gate
1626d62bc4byz	if (rc = dl_info(lh, &info, NULL, NULL, NULL)) {
16277c478bdstevel@tonic-gate		nfs_perror(rc, "revarp_myaddr: dl_info failed: %m\n");
16287c478bdstevel@tonic-gate		(void) ldi_close(lh, FREAD|FWRITE, CRED());
16297c478bdstevel@tonic-gate		return (rc);
16307c478bdstevel@tonic-gate	}
16317c478bdstevel@tonic-gate
16327c478bdstevel@tonic-gate	/* Initialize myaddr */
16337c478bdstevel@tonic-gate	myaddr.maxlen = info.dl_addr_length;
16347c478bdstevel@tonic-gate	myaddr.buf = kmem_alloc(myaddr.maxlen, KM_SLEEP);
16357c478bdstevel@tonic-gate
16367c478bdstevel@tonic-gate	revarp_start(lh, &myaddr);
16377c478bdstevel@tonic-gate
16387c478bdstevel@tonic-gate	bcopy(myaddr.buf, &sin.sin_addr, myaddr.len);
16397c478bdstevel@tonic-gate	sin.sin_family = AF_INET;
16407c478bdstevel@tonic-gate
16417c478bdstevel@tonic-gate	sbuf.buf = (caddr_t)&sin;
16427c478bdstevel@tonic-gate	sbuf.maxlen = sbuf.len = sizeof (sin);
16437c478bdstevel@tonic-gate	if (rc = ifioctl(tiptr, SIOCSIFADDR, &sbuf)) {
16447c478bdstevel@tonic-gate		nfs_perror(rc,
16457c478bdstevel@tonic-gate		    "revarp_myaddr: couldn't set interface net address: %m\n");
16467c478bdstevel@tonic-gate		(void) ldi_close(lh, FREAD|FWRITE, CRED());
16477c478bdstevel@tonic-gate		kmem_free(myaddr.buf, myaddr.maxlen);
16487c478bdstevel@tonic-gate		return (rc);
16497c478bdstevel@tonic-gate	}
16507c478bdstevel@tonic-gate
16517c478bdstevel@tonic-gate	/* Now turn on the interface */
16527c478bdstevel@tonic-gate	if (rc = setifflags(tiptr, IFF_UP)) {
16537c478bdstevel@tonic-gate		nfs_perror(rc,
16547c478bdstevel@tonic-gate		    "revarp_myaddr: couldn't enable network interface: %m\n");
16557c478bdstevel@tonic-gate	}
16567c478bdstevel@tonic-gate
16577c478bdstevel@tonic-gate	(void) ldi_close(lh, FREAD|FWRITE, CRED());
16587c478bdstevel@tonic-gate	kmem_free(myaddr.buf, myaddr.maxlen);
16597c478bdstevel@tonic-gate	return (rc);
16607c478bdstevel@tonic-gate}
16617c478bdstevel@tonic-gate
16627c478bdstevel@tonic-gatestatic void
16637c478bdstevel@tonic-gaterevarp_start(ldi_handle_t lh, struct netbuf *myaddr)
16647c478bdstevel@tonic-gate{
16657c478bdstevel@tonic-gate	struct ether_arp *ea;
16667c478bdstevel@tonic-gate	int rc;
16677c478bdstevel@tonic-gate	dl_unitdata_req_t *dl_udata;
16687c478bdstevel@tonic-gate	mblk_t *bp;
16697c478bdstevel@tonic-gate	mblk_t *mp;
16707c478bdstevel@tonic-gate	struct dladdr *dlsap;
16717c478bdstevel@tonic-gate	static int done = 0;
1672d62bc4byz	size_t addrlen = ETHERADDRL;
16737c478bdstevel@tonic-gate
1674d62bc4byz	if (dl_phys_addr(lh, (uchar_t *)&myether, &addrlen, NULL) != 0 ||
1675d62bc4byz	    addrlen != ETHERADDRL) {
16767c478bdstevel@tonic-gate		/* Fallback using per-node address */
16777c478bdstevel@tonic-gate		(void) localetheraddr((struct ether_addr *)NULL, &myether);
16787c478bdstevel@tonic-gate		cmn_err(CE_CONT, "?DLPI failed to get Ethernet address. Using "
1679843e198johnlev		    "system wide Ethernet address %s\n",
1680843e198johnlev		    ether_sprintf(&myether));
16817c478bdstevel@tonic-gate	}
16827c478bdstevel@tonic-gate
16837c478bdstevel@tonic-gategetreply:
16847c478bdstevel@tonic-gate	if (myaddr->len != 0) {
16857c478bdstevel@tonic-gate		cmn_err(CE_CONT, "?Found my IP address: %x (%d.%d.%d.%d)\n",
16867c478bdstevel@tonic-gate		    *(int *)myaddr->buf,
16877c478bdstevel@tonic-gate		    (uchar_t)myaddr->buf[0], (uchar_t)myaddr->buf[1],
16887c478bdstevel@tonic-gate		    (uchar_t)myaddr->buf[2], (uchar_t)myaddr->buf[3]);
16897c478bdstevel@tonic-gate		return;
16907c478bdstevel@tonic-gate	}
16917c478bdstevel@tonic-gate
16927c478bdstevel@tonic-gate	if (done++ == 0)
16937c478bdstevel@tonic-gate		cmn_err(CE_CONT, "?Requesting Internet address for %s\n",
16947c478bdstevel@tonic-gate		    ether_sprintf(&myether));
16957c478bdstevel@tonic-gate
16967c478bdstevel@tonic-gate	/*
16977c478bdstevel@tonic-gate	 * Send another RARP request.
16987c478bdstevel@tonic-gate	 */
16997c478bdstevel@tonic-gate	if ((mp = allocb(sizeof (dl_unitdata_req_t) + sizeof (*dlsap),
17007c478bdstevel@tonic-gate	    BPRI_HI)) == NULL) {
17017c478bdstevel@tonic-gate		cmn_err(CE_WARN, "revarp_myaddr: allocb no memory");
17027c478bdstevel@tonic-gate		return;
17037c478bdstevel@tonic-gate	}
17047c478bdstevel@tonic-gate	if ((bp = allocb(sizeof (struct ether_arp), BPRI_HI)) == NULL) {
17057c478bdstevel@tonic-gate		cmn_err(CE_WARN, "revarp_myaddr: allocb no memory");
17067c478bdstevel@tonic-gate		return;
17077c478bdstevel@tonic-gate	}
17087c478bdstevel@tonic-gate
17097c478bdstevel@tonic-gate	/*
17107c478bdstevel@tonic-gate	 * Format the transmit request part.
17117c478bdstevel@tonic-gate	 */
17127c478bdstevel@tonic-gate	mp->b_datap->db_type = M_PROTO;
17137c478bdstevel@tonic-gate	dl_udata = (dl_unitdata_req_t *)mp->b_wptr;
17147c478bdstevel@tonic-gate	mp->b_wptr += sizeof (dl_unitdata_req_t) + sizeof (*dlsap);
17157c478bdstevel@tonic-gate	dl_udata->dl_primitive = DL_UNITDATA_REQ;
17167c478bdstevel@tonic-gate	dl_udata->dl_dest_addr_length = sizeof (*dlsap);
17177c478bdstevel@tonic-gate	dl_udata->dl_dest_addr_offset = sizeof (*dl_udata);
17187c478bdstevel@tonic-gate	dl_udata->dl_priority.dl_min = 0;
17197c478bdstevel@tonic-gate	dl_udata->dl_priority.dl_max = 0;
17207c478bdstevel@tonic-gate
17217c478bdstevel@tonic-gate	dlsap = (struct dladdr *)(mp->b_rptr + sizeof (*dl_udata));
17227c478bdstevel@tonic-gate	bcopy(&etherbroadcastaddr, &dlsap->dl_phys,
17237c478bdstevel@tonic-gate	    sizeof (etherbroadcastaddr));
17247c478bdstevel@tonic-gate	dlsap->dl_sap = ETHERTYPE_REVARP;
17257c478bdstevel@tonic-gate
17267c478bdstevel@tonic-gate	/*
17277c478bdstevel@tonic-gate	 * Format the actual REVARP request.
17287c478bdstevel@tonic-gate	 */
17297c478bdstevel@tonic-gate	bzero(bp->b_wptr, sizeof (struct ether_arp));
17307c478bdstevel@tonic-gate	ea = (struct ether_arp *)bp->b_wptr;
17317c478bdstevel@tonic-gate	bp->b_wptr += sizeof (struct ether_arp);
17327c478bdstevel@tonic-gate	ea->arp_hrd = htons(ARPHRD_ETHER);
17337c478bdstevel@tonic-gate	ea->arp_pro = htons(ETHERTYPE_IP);
17347c478bdstevel@tonic-gate	ea->arp_hln = sizeof (ea->arp_sha);	/* hardware address length */
17357c478bdstevel@tonic-gate	ea->arp_pln = sizeof (ea->arp_spa);	/* protocol address length */
17367c478bdstevel@tonic-gate	ea->arp_op = htons(REVARP_REQUEST);
17377c478bdstevel@tonic-gate	ether_copy(&myether, &ea->arp_sha);
17387c478bdstevel@tonic-gate	ether_copy(&myether, &ea->arp_tha);
17397c478bdstevel@tonic-gate
17407c478bdstevel@tonic-gate	mp->b_cont = bp;
17417c478bdstevel@tonic-gate
17427c478bdstevel@tonic-gate	if ((rc = ldi_putmsg(lh, mp)) != 0) {
17437c478bdstevel@tonic-gate		nfs_perror(rc, "revarp_start: ldi_putmsg failed: %m\n");
17447c478bdstevel@tonic-gate		return;
17457c478bdstevel@tonic-gate	}
17467c478bdstevel@tonic-gate	revarpinput(lh, myaddr);
17477c478bdstevel@tonic-gate
17487c478bdstevel@tonic-gate	goto getreply;
17497c478bdstevel@tonic-gate}
17507c478bdstevel@tonic-gate
17517c478bdstevel@tonic-gate/*
17527c478bdstevel@tonic-gate * Client side Reverse-ARP input
17537c478bdstevel@tonic-gate * Server side is handled by user level server
17547c478bdstevel@tonic-gate */
17557c478bdstevel@tonic-gatestatic void
17567c478bdstevel@tonic-gaterevarpinput(ldi_handle_t lh, struct netbuf *myaddr)
17577c478bdstevel@tonic-gate{
17587c478bdstevel@tonic-gate	struct ether_arp *ea;
17597c478bdstevel@tonic-gate	mblk_t *bp;
17607c478bdstevel@tonic-gate	mblk_t *mp;
17617c478bdstevel@tonic-gate	int rc;
17627c478bdstevel@tonic-gate	timestruc_t tv, give_up, now;
17637c478bdstevel@tonic-gate
17647c478bdstevel@tonic-gate	/*
17657c478bdstevel@tonic-gate	 * Choose the time at which we will give up, and resend our
17667c478bdstevel@tonic-gate	 * request.
17677c478bdstevel@tonic-gate	 */
17687c478bdstevel@tonic-gate	gethrestime(&give_up);
17697c478bdstevel@tonic-gate	give_up.tv_sec += REVARP_TIMEO;
17707c478bdstevel@tonic-gatewait:
17717c478bdstevel@tonic-gate	/*
17727c478bdstevel@tonic-gate	 * Compute new timeout value.
17737c478bdstevel@tonic-gate	 */
17747c478bdstevel@tonic-gate	tv = give_up;
17757c478bdstevel@tonic-gate	gethrestime(&now);
17767c478bdstevel@tonic-gate	timespecsub(&tv, &now);
17777c478bdstevel@tonic-gate	/*
17787c478bdstevel@tonic-gate	 * If we don't have at least one full second remaining, give up.
17797c478bdstevel@tonic-gate	 * This means we might wait only just over 4.0 seconds, but that's
17807c478bdstevel@tonic-gate	 * okay.
17817c478bdstevel@tonic-gate	 */
17827c478bdstevel@tonic-gate	if (tv.tv_sec <= 0)
17837c478bdstevel@tonic-gate		return;
17847c478bdstevel@tonic-gate	rc = ldi_getmsg(lh, &mp, &tv);
17857c478bdstevel@tonic-gate	if (rc == ETIME) {
17867c478bdstevel@tonic-gate		goto out;
17877c478bdstevel@tonic-gate	} else if (rc != 0) {
17887c478bdstevel@tonic-gate		nfs_perror(rc, "revarpinput: ldi_getmsg failed: %m\n");
17897c478bdstevel@tonic-gate		return;
17907c478bdstevel@tonic-gate	}
17917c478bdstevel@tonic-gate
17927c478bdstevel@tonic-gate	if (mp->b_cont == NULL) {
17937c478bdstevel@tonic-gate		printf("revarpinput: b_cont == NULL\n");
17947c478bdstevel@tonic-gate		goto out;
17957c478bdstevel@tonic-gate	}
17967c478bdstevel@tonic-gate
17977c478bdstevel@tonic-gate	if (mp->b_datap->db_type != M_PROTO) {
17987c478bdstevel@tonic-gate		printf("revarpinput: bad header type %d\n",
17997c478bdstevel@tonic-gate		    mp->b_datap->db_type);
18007c478bdstevel@tonic-gate		goto out;
18017c478bdstevel@tonic-gate	}
18027c478bdstevel@tonic-gate
18037c478bdstevel@tonic-gate	bp = mp->b_cont;
18047c478bdstevel@tonic-gate
18057c478bdstevel@tonic-gate	if (bp->b_wptr - bp->b_rptr < sizeof (*ea)) {
18067c478bdstevel@tonic-gate		printf("revarpinput: bad data len %d, expect %d\n",
18077c478bdstevel@tonic-gate		    (int)(bp->b_wptr - bp->b_rptr),  (int)sizeof (*ea));
18087c478bdstevel@tonic-gate		goto out;
18097c478bdstevel@tonic-gate	}
18107c478bdstevel@tonic-gate
18117c478bdstevel@tonic-gate	ea = (struct ether_arp *)bp->b_rptr;
18127c478bdstevel@tonic-gate
18137c478bdstevel@tonic-gate	if ((ushort_t)ntohs(ea->arp_pro) != ETHERTYPE_IP) {
18147c478bdstevel@tonic-gate		/* We could have received another broadcast arp packet. */
18157c478bdstevel@tonic-gate		if (dldebug)
18167c478bdstevel@tonic-gate			printf("revarpinput: bad type %x\n",
18177c478bdstevel@tonic-gate			    (ushort_t)ntohs(ea->arp_pro));
18187c478bdstevel@tonic-gate		freemsg(mp);
18197c478bdstevel@tonic-gate		goto wait;
18207c478bdstevel@tonic-gate	}
18217c478bdstevel@tonic-gate	if ((ushort_t)ntohs(ea->arp_op) != REVARP_REPLY) {
18227c478bdstevel@tonic-gate		/* We could have received a broadcast arp request. */
18237c478bdstevel@tonic-gate		if (dldebug)
18247c478bdstevel@tonic-gate			printf("revarpinput: bad op %x\n",
18257c478bdstevel@tonic-gate			    (ushort_t)ntohs(ea->arp_op));
18267c478bdstevel@tonic-gate		freemsg(mp);
18277c478bdstevel@tonic-gate		goto wait;
18287c478bdstevel@tonic-gate	}
18297c478bdstevel@tonic-gate
18307c478bdstevel@tonic-gate	if (!ether_cmp(&ea->arp_tha, &myether)) {
18317c478bdstevel@tonic-gate		bcopy(&ea->arp_tpa, myaddr->buf, sizeof (ea->arp_tpa));
18327c478bdstevel@tonic-gate		myaddr->len = sizeof (ea->arp_tpa);
18337c478bdstevel@tonic-gate	} else {
18347c478bdstevel@tonic-gate		/* We could have gotten a broadcast arp response. */
18357c478bdstevel@tonic-gate		if (dldebug)
18367c478bdstevel@tonic-gate			printf("revarpinput: got reply, but not my address\n");
18377c478bdstevel@tonic-gate		freemsg(mp);
18387c478bdstevel@tonic-gate		goto wait;
18397c478bdstevel@tonic-gate	}
18407c478bdstevel@tonic-gateout:
18417c478bdstevel@tonic-gate	freemsg(mp);
18427c478bdstevel@tonic-gate}
18437c478bdstevel@tonic-gate
18447c478bdstevel@tonic-gate/*
18457c478bdstevel@tonic-gate * From rpcsvc/mountxdr.c in SunOS. We can't
18467c478bdstevel@tonic-gate * put this into the rpc directory because
18477c478bdstevel@tonic-gate * it calls xdr_fhandle() which is in a
18487c478bdstevel@tonic-gate * loadable module.
18497c478bdstevel@tonic-gate */
18507c478bdstevel@tonic-gatestatic bool_t
18517c478bdstevel@tonic-gatemyxdr_fhstatus(XDR *xdrs, struct fhstatus *fhsp)
18527c478bdstevel@tonic-gate{
18537c478bdstevel@tonic-gate
18547c478bdstevel@tonic-gate	if (!xdr_int(xdrs, &fhsp->fhs_status))
18557c478bdstevel@tonic-gate		return (FALSE);
18567c478bdstevel@tonic-gate	if (fhsp->fhs_status == 0) {
18577c478bdstevel@tonic-gate		if (!myxdr_fhandle(xdrs, &fhsp->fhs_fh))
18587c478bdstevel@tonic-gate			return (FALSE);
18597c478bdstevel@tonic-gate	}
18607c478bdstevel@tonic-gate	return (TRUE);
18617c478bdstevel@tonic-gate}
18627c478bdstevel@tonic-gate
18637c478bdstevel@tonic-gate/*
18647c478bdstevel@tonic-gate * From nfs_xdr.c.
18657c478bdstevel@tonic-gate *
18667c478bdstevel@tonic-gate * File access handle
18677c478bdstevel@tonic-gate * The fhandle struct is treated a opaque data on the wire
18687c478bdstevel@tonic-gate */
18697c478bdstevel@tonic-gatestatic bool_t
18707c478bdstevel@tonic-gatemyxdr_fhandle(XDR *xdrs, fhandle_t *fh)
18717c478bdstevel@tonic-gate{
18727c478bdstevel@tonic-gate	return (xdr_opaque(xdrs, (caddr_t)fh, NFS_FHSIZE));
18737c478bdstevel@tonic-gate}
18747c478bdstevel@tonic-gate
18757c478bdstevel@tonic-gatestatic bool_t
18767c478bdstevel@tonic-gatemyxdr_mountres3(XDR *xdrs, struct mountres3 *objp)
18777c478bdstevel@tonic-gate{
18787c478bdstevel@tonic-gate	if (!myxdr_mountstat3(xdrs, &objp->fhs_status))
18797c478bdstevel@tonic-gate		return (FALSE);
18807c478bdstevel@tonic-gate	switch (objp->fhs_status) {
18817c478bdstevel@tonic-gate	case MNT_OK:
18827c478bdstevel@tonic-gate		if (!myxdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
18837c478bdstevel@tonic-gate			return (FALSE);
18847c478bdstevel@tonic-gate		break;
18857c478bdstevel@tonic-gate	default:
18867c478bdstevel@tonic-gate		break;
18877c478bdstevel@tonic-gate	}
18887c478bdstevel@tonic-gate	return (TRUE);
18897c478bdstevel@tonic-gate}
18907c478bdstevel@tonic-gate
18917c478bdstevel@tonic-gatestatic bool_t
18927c478bdstevel@tonic-gatemyxdr_mountstat3(XDR *xdrs, enum mountstat3 *objp)
18937c478bdstevel@tonic-gate{
18947c478bdstevel@tonic-gate	return (xdr_enum(xdrs, (enum_t *)objp));
18957c478bdstevel@tonic-gate}
18967c478bdstevel@tonic-gate
18977c478bdstevel@tonic-gatestatic bool_t
18987c478bdstevel@tonic-gatemyxdr_mountres3_ok(XDR *xdrs, struct mountres3_ok *objp)
18997c478bdstevel@tonic-gate{
19007c478bdstevel@tonic-gate	if (!myxdr_fhandle3(xdrs, &objp->fhandle))
19017c478bdstevel@tonic-gate		return (FALSE);
19027c478bdstevel@tonic-gate	if (!xdr_array(xdrs, (char **)&objp->auth_flavors.auth_flavors_val,
1903843e198johnlev	    (uint_t *)&objp->auth_flavors.auth_flavors_len, ~0,
1904843e198johnlev	    sizeof (int), (xdrproc_t)xdr_int))
19057c478bdstevel@tonic-gate		return (FALSE);
19067c478bdstevel@tonic-gate	return (TRUE);
19077c478bdstevel@tonic-gate}
19087c478bdstevel@tonic-gate
19097c478bdstevel@tonic-gatestatic bool_t
19107c478bdstevel@tonic-gatemyxdr_fhandle3(XDR *xdrs, struct fhandle3 *objp)
19117c478bdstevel@tonic-gate{
19127c478bdstevel@tonic-gate	return (xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
19137c478bdstevel@tonic-gate	    (uint_t *)&objp->fhandle3_len, FHSIZE3));
19147c478bdstevel@tonic-gate}
19157c478bdstevel@tonic-gate
19167c478bdstevel@tonic-gate/*
19177c478bdstevel@tonic-gate * From SunOS pmap_clnt.c
19187c478bdstevel@tonic-gate *
19197c478bdstevel@tonic-gate * Port mapper routines:
19207c478bdstevel@tonic-gate *	pmap_kgetport() - get port number.
19217c478bdstevel@tonic-gate *	pmap_rmt_call()  - indirect call via port mapper.
19227c478bdstevel@tonic-gate *
19237c478bdstevel@tonic-gate */
19247c478bdstevel@tonic-gatestatic enum clnt_stat
19257c478bdstevel@tonic-gatepmap_kgetport(struct knetconfig *knconf, struct netbuf *call_addr,
1926f3de0ddToomas Soome    rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
19277c478bdstevel@tonic-gate{
19287c478bdstevel@tonic-gate	ushort_t port;
19297c478bdstevel@tonic-gate	int tries;
19307c478bdstevel@tonic-gate	enum clnt_stat stat;
19317c478bdstevel@tonic-gate	struct pmap	pmap_parms;
19327c478bdstevel@tonic-gate	RPCB		rpcb_parms;
19337c478bdstevel@tonic-gate	char		*ua = NULL;
19347c478bdstevel@tonic-gate
19357c478bdstevel@tonic-gate	port = 0;
19367c478bdstevel@tonic-gate
19377c478bdstevel@tonic-gate	((struct sockaddr_in *)call_addr->buf)->sin_port = htons(PMAPPORT);
19387c478bdstevel@tonic-gate
19397c478bdstevel@tonic-gate	pmap_parms.pm_prog = prog;
19407c478bdstevel@tonic-gate	pmap_parms.pm_vers = vers;
19417c478bdstevel@tonic-gate	pmap_parms.pm_prot = prot;
19427c478bdstevel@tonic-gate	pmap_parms.pm_port = 0;
19437c478bdstevel@tonic-gate	for (tries = 0; tries < 5; tries++) {
19447c478bdstevel@tonic-gate		stat = mycallrpc(knconf, call_addr,
19457c478bdstevel@tonic-gate		    PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
19467c478bdstevel@tonic-gate		    myxdr_pmap, (char *)&pmap_parms,
19477c478bdstevel@tonic-gate		    xdr_u_short, (char *)&port,
19487c478bdstevel@tonic-gate		    DEFAULT_TIMEO, DEFAULT_RETRIES);
19497c478bdstevel@tonic-gate
19507c478bdstevel@tonic-gate		if (stat != RPC_TIMEDOUT)
19517c478bdstevel@tonic-gate			break;
19527c478bdstevel@tonic-gate		cmn_err(CE_WARN,
19537c478bdstevel@tonic-gate		    "pmap_kgetport: Portmapper not responding; still trying");
19547c478bdstevel@tonic-gate	}
19557c478bdstevel@tonic-gate
19567c478bdstevel@tonic-gate	if (stat == RPC_PROGUNAVAIL) {
19577c478bdstevel@tonic-gate		cmn_err(CE_WARN,
19587c478bdstevel@tonic-gate		    "pmap_kgetport: Portmapper failed - trying rpcbind");
19597c478bdstevel@tonic-gate
19607c478bdstevel@tonic-gate		rpcb_parms.r_prog = prog;
19617c478bdstevel@tonic-gate		rpcb_parms.r_vers = vers;
19627c478bdstevel@tonic-gate		rpcb_parms.r_netid = knconf->knc_proto;
19637c478bdstevel@tonic-gate		rpcb_parms.r_addr = rpcb_parms.r_owner = "";
19647c478bdstevel@tonic-gate
19657c478bdstevel@tonic-gate		for (tries = 0; tries < 5; tries++) {
19667c478bdstevel@tonic-gate			stat = mycallrpc(knconf, call_addr,
19677c478bdstevel@tonic-gate			    RPCBPROG, RPCBVERS, RPCBPROC_GETADDR,
19687c478bdstevel@tonic-gate			    xdr_rpcb, (char *)&rpcb_parms,
19697c478bdstevel@tonic-gate			    xdr_wrapstring, (char *)&ua,
19707c478bdstevel@tonic-gate			    DEFAULT_TIMEO, DEFAULT_RETRIES);
19717c478bdstevel@tonic-gate
19727c478bdstevel@tonic-gate			if (stat != RPC_TIMEDOUT)
19737c478bdstevel@tonic-gate				break;
19747c478bdstevel@tonic-gate			cmn_err(CE_WARN,
19757c478bdstevel@tonic-gate			"pmap_kgetport: rpcbind not responding; still trying");
19767c478bdstevel@tonic-gate		}
19777c478bdstevel@tonic-gate
19787c478bdstevel@tonic-gate		if (stat == RPC_SUCCESS) {
1979ad25685Toomas Soome			if ((ua != NULL) && (ua[0] != '\0')) {
19807c478bdstevel@tonic-gate				port = rpc_uaddr2port(AF_INET, ua);
19817c478bdstevel@tonic-gate			} else {
19827c478bdstevel@tonic-gate				/* Address unknown */
19837c478bdstevel@tonic-gate				stat = RPC_PROGUNAVAIL;
19847c478bdstevel@tonic-gate			}
19857c478bdstevel@tonic-gate		}
19867c478bdstevel@tonic-gate	}
19877c478bdstevel@tonic-gate
19887c478bdstevel@tonic-gate	if (stat == RPC_SUCCESS)
19897c478bdstevel@tonic-gate		((struct sockaddr_in *)call_addr->buf)->sin_port = ntohs(port);
19907c478bdstevel@tonic-gate
19917c478bdstevel@tonic-gate	return (stat);
19927c478bdstevel@tonic-gate}
19937c478bdstevel@tonic-gate
19947c478bdstevel@tonic-gate/*
19957c478bdstevel@tonic-gate * pmapper remote-call-service interface.
19967c478bdstevel@tonic-gate * This routine is used to call the pmapper remote call service
19977c478bdstevel@tonic-gate * which will look up a service program in the port maps, and then
19987c478bdstevel@tonic-gate * remotely call that routine with the given parameters.  This allows
19997c478bdstevel@tonic-gate * programs to do a lookup and call in one step. In addition to the call_addr,
20007c478bdstevel@tonic-gate * the caller provides a boolean hint about the destination address (TRUE if
20017c478bdstevel@tonic-gate * address is a broadcast address, FALSE otherwise).
20027c478bdstevel@tonic-gate *
20037c478bdstevel@tonic-gate * On return, `call addr' contains the port number for the
20047c478bdstevel@tonic-gate * service requested, and `resp_addr' contains its IP address.
20057c478bdstevel@tonic-gate */
20067c478bdstevel@tonic-gatestatic enum clnt_stat
20077c478bdstevel@tonic-gatepmap_rmt_call(struct knetconfig *knconf, struct netbuf *call_addr,
2008f3de0ddToomas Soome    bool_t bcast, rpcprog_t progn, rpcvers_t versn, rpcproc_t procn,
2009f3de0ddToomas Soome    xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
2010f3de0ddToomas Soome    struct timeval tout, struct netbuf *resp_addr)
20117c478bdstevel@tonic-gate{
20127c478bdstevel@tonic-gate	CLIENT *cl;
20137c478bdstevel@tonic-gate	enum clnt_stat stat;
20147c478bdstevel@tonic-gate	rpcport_t port;
20157c478bdstevel@tonic-gate	int rc;
20167c478bdstevel@tonic-gate	struct rmtcallargs	pmap_args;
20177c478bdstevel@tonic-gate	struct rmtcallres	pmap_res;
20187c478bdstevel@tonic-gate	struct rpcb_rmtcallargs	rpcb_args;
20197c478bdstevel@tonic-gate	struct rpcb_rmtcallres	rpcb_res;
20207c478bdstevel@tonic-gate	char			ua[100];	/* XXX */
20217c478bdstevel@tonic-gate
20227c478bdstevel@tonic-gate	((struct sockaddr_in *)call_addr->buf)->sin_port = htons(PMAPPORT);
20237c478bdstevel@tonic-gate
20247c478bdstevel@tonic-gate	rc = clnt_tli_kcreate(knconf, call_addr, PMAPPROG, PMAPVERS,
20257c478bdstevel@tonic-gate	    0, PMAP_RETRIES, CRED(), &cl);
20267c478bdstevel@tonic-gate	if (rc != 0) {
20277c478bdstevel@tonic-gate		nfs_perror(rc,
20287c478bdstevel@tonic-gate		    "pmap_rmt_call: clnt_tli_kcreate failed: %m\n");
20297c478bdstevel@tonic-gate		return (RPC_SYSTEMERROR);	/* XXX */
20307c478bdstevel@tonic-gate	}
20317c478bdstevel@tonic-gate	if (cl == (CLIENT *)NULL) {
20327c478bdstevel@tonic-gate		panic("pmap_rmt_call: clnt_tli_kcreate failed");
20337c478bdstevel@tonic-gate		/* NOTREACHED */
20347c478bdstevel@tonic-gate	}
20357c478bdstevel@tonic-gate
20367c478bdstevel@tonic-gate	(void) CLNT_CONTROL(cl, CLSET_BCAST, (char *)&bcast);
20377c478bdstevel@tonic-gate
20387c478bdstevel@tonic-gate	pmap_args.prog = progn;
20397c478bdstevel@tonic-gate	pmap_args.vers = versn;
20407c478bdstevel@tonic-gate	pmap_args.proc = procn;
20417c478bdstevel@tonic-gate	pmap_args.args_ptr = argsp;
20427c478bdstevel@tonic-gate	pmap_args.xdr_args = xdrargs;
20437c478bdstevel@tonic-gate	pmap_res.port_ptr = &port;
20447c478bdstevel@tonic-gate	pmap_res.results_ptr = resp;
20457c478bdstevel@tonic-gate	pmap_res.xdr_results = xdrres;
20467c478bdstevel@tonic-gate	stat = clnt_clts_kcallit_addr(cl, PMAPPROC_CALLIT,
20477c478bdstevel@tonic-gate	    myxdr_rmtcall_args, (caddr_t)&pmap_args,
20487c478bdstevel@tonic-gate	    myxdr_rmtcallres, (caddr_t)&pmap_res,
20497c478bdstevel@tonic-gate	    tout, resp_addr);
20507c478bdstevel@tonic-gate
20517c478bdstevel@tonic-gate	if (stat == RPC_SUCCESS) {
20527c478bdstevel@tonic-gate		((struct sockaddr_in *)resp_addr->buf)->sin_port =
20537c478bdstevel@tonic-gate		    htons((ushort_t)port);
20547c478bdstevel@tonic-gate	}
20557c478bdstevel@tonic-gate	CLNT_DESTROY(cl);
20567c478bdstevel@tonic-gate
20577c478bdstevel@tonic-gate	if (stat != RPC_PROGUNAVAIL)
20587c478bdstevel@tonic-gate		return (stat);
20597c478bdstevel@tonic-gate
20607c478bdstevel@tonic-gate	cmn_err(CE_WARN, "pmap_rmt_call: Portmapper failed - trying rpcbind");
20617c478bdstevel@tonic-gate
20627c478bdstevel@tonic-gate	rc = clnt_tli_kcreate(knconf, call_addr, RPCBPROG, RPCBVERS,
20637c478bdstevel@tonic-gate	    0, PMAP_RETRIES, CRED(), &cl);
20647c478bdstevel@tonic-gate	if (rc != 0) {
20657c478bdstevel@tonic-gate		nfs_perror(rc, "pmap_rmt_call: clnt_tli_kcreate failed: %m\n");
20667c478bdstevel@tonic-gate		return (RPC_SYSTEMERROR);	/* XXX */
20677c478bdstevel@tonic-gate	}
20687c478bdstevel@tonic-gate
20697c478bdstevel@tonic-gate	if (cl == NULL) {
20707c478bdstevel@tonic-gate		panic("pmap_rmt_call: clnt_tli_kcreate failed");
20717c478bdstevel@tonic-gate		/* NOTREACHED */
20727c478bdstevel@tonic-gate	}
20737c478bdstevel@tonic-gate
20747c478bdstevel@tonic-gate	rpcb_args.prog = progn;
20757c478bdstevel@tonic-gate	rpcb_args.vers = versn;
20767c478bdstevel@tonic-gate	rpcb_args.proc = procn;
20777c478bdstevel@tonic-gate	rpcb_args.args_ptr = argsp;
20787c478bdstevel@tonic-gate	rpcb_args.xdr_args = xdrargs;
20797c478bdstevel@tonic-gate	rpcb_res.addr_ptr = ua;
20807c478bdstevel@tonic-gate	rpcb_res.results_ptr = resp;
20817c478bdstevel@tonic-gate	rpcb_res.xdr_results = xdrres;
20827c478bdstevel@tonic-gate	stat = clnt_clts_kcallit_addr(cl, PMAPPROC_CALLIT,
20837c478bdstevel@tonic-gate	    xdr_rpcb_rmtcallargs, (caddr_t)&rpcb_args,
20847c478bdstevel@tonic-gate	    xdr_rpcb_rmtcallres, (caddr_t)&rpcb_res,
20857c478bdstevel@tonic-gate	    tout, resp_addr);
20867c478bdstevel@tonic-gate
20877c478bdstevel@tonic-gate	if (stat == RPC_SUCCESS)
20887c478bdstevel@tonic-gate		((struct sockaddr_in *)resp_addr->buf)->sin_port =
20897c478bdstevel@tonic-gate		    rpc_uaddr2port(AF_INET, ua);
20907c478bdstevel@tonic-gate	CLNT_DESTROY(cl);
20917c478bdstevel@tonic-gate
20927c478bdstevel@tonic-gate	return (stat);
20937c478bdstevel@tonic-gate}
20947c478bdstevel@tonic-gate
20957c478bdstevel@tonic-gate/*
20967c478bdstevel@tonic-gate * XDR remote call arguments
20977c478bdstevel@tonic-gate * written for XDR_ENCODE direction only
20987c478bdstevel@tonic-gate */
20997c478bdstevel@tonic-gatestatic bool_t
21007c478bdstevel@tonic-gatemyxdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
21017c478bdstevel@tonic-gate{
21027c478bdstevel@tonic-gate	uint_t lenposition;
21037c478bdstevel@tonic-gate	uint_t argposition;
21047c478bdstevel@tonic-gate	uint_t position;
21057c478bdstevel@tonic-gate
21067c478bdstevel@tonic-gate	if (xdr_rpcprog(xdrs, &(cap->prog)) &&
21077c478bdstevel@tonic-gate	    xdr_rpcvers(xdrs, &(cap->vers)) &&
21087c478bdstevel@tonic-gate	    xdr_rpcproc(xdrs, &(cap->proc))) {
21097c478bdstevel@tonic-gate		lenposition = XDR_GETPOS(xdrs);
21107c478bdstevel@tonic-gate		if (!xdr_u_int(xdrs, &cap->arglen))
21117c478bdstevel@tonic-gate			return (FALSE);
21127c478bdstevel@tonic-gate		argposition = XDR_GETPOS(xdrs);
21137c478bdstevel@tonic-gate		if (!(*(cap->xdr_args))(xdrs, cap->args_ptr))
21147c478bdstevel@tonic-gate			return (FALSE);
21157c478bdstevel@tonic-gate		position = XDR_GETPOS(xdrs);
21167c478bdstevel@tonic-gate		cap->arglen = (uint_t)position - (uint_t)argposition;
21177c478bdstevel@tonic-gate		XDR_SETPOS(xdrs, lenposition);
21187c478bdstevel@tonic-gate		if (!xdr_u_int(xdrs, &cap->arglen))
21197c478bdstevel@tonic-gate			return (FALSE);
21207c478bdstevel@tonic-gate		XDR_SETPOS(xdrs, position);
21217c478bdstevel@tonic-gate		return (TRUE);
21227c478bdstevel@tonic-gate	}
21237c478bdstevel@tonic-gate	return (FALSE);
21247c478bdstevel@tonic-gate}
21257c478bdstevel@tonic-gate
21267c478bdstevel@tonic-gate/*
21277c478bdstevel@tonic-gate * XDR remote call results
21287c478bdstevel@tonic-gate * written for XDR_DECODE direction only
21297c478bdstevel@tonic-gate */
21307c478bdstevel@tonic-gatestatic bool_t
21317c478bdstevel@tonic-gatemyxdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
21327c478bdstevel@tonic-gate{
21337c478bdstevel@tonic-gate	caddr_t port_ptr;
21347c478bdstevel@tonic-gate
21357c478bdstevel@tonic-gate	port_ptr = (caddr_t)crp->port_ptr;
21367c478bdstevel@tonic-gate	if (xdr_reference(xdrs, &port_ptr, sizeof (uint_t), xdr_u_int) &&
21377c478bdstevel@tonic-gate	    xdr_u_int(xdrs, &crp->resultslen)) {
21387c478bdstevel@tonic-gate		crp->port_ptr = (rpcport_t *)port_ptr;
21397c478bdstevel@tonic-gate		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
21407c478bdstevel@tonic-gate	}
21417c478bdstevel@tonic-gate	return (FALSE);
21427c478bdstevel@tonic-gate}
21437c478bdstevel@tonic-gate
21447c478bdstevel@tonic-gatestatic bool_t
21457c478bdstevel@tonic-gatemyxdr_pmap(XDR *xdrs, struct pmap *regs)
21467c478bdstevel@tonic-gate{
21477c478bdstevel@tonic-gate	if (xdr_rpcprog(xdrs, &regs->pm_prog) &&
21487c478bdstevel@tonic-gate	    xdr_rpcvers(xdrs, &regs->pm_vers) &&
21497c478bdstevel@tonic-gate	    xdr_rpcprot(xdrs, &regs->pm_prot))
21507c478bdstevel@tonic-gate		return (xdr_rpcport(xdrs, &regs->pm_port));
21517c478bdstevel@tonic-gate
21527c478bdstevel@tonic-gate	return (FALSE);
21537c478bdstevel@tonic-gate}
21547c478bdstevel@tonic-gate
21557c478bdstevel@tonic-gate/*
21567c478bdstevel@tonic-gate * From SunOS callrpc.c
21577c478bdstevel@tonic-gate */
21587c478bdstevel@tonic-gatestatic enum clnt_stat
21597c478bdstevel@tonic-gatemycallrpc(struct knetconfig *knconf, struct netbuf *call_addr,
2160f3de0ddToomas Soome    rpcprog_t prognum, rpcvers_t versnum, rpcproc_t procnum,
2161f3de0ddToomas Soome    xdrproc_t inproc, char *in, xdrproc_t outproc, char *out,
2162f3de0ddToomas Soome    int timeo, int retries)
21637c478bdstevel@tonic-gate{
21647c478bdstevel@tonic-gate	CLIENT *cl;
21657c478bdstevel@tonic-gate	struct timeval tv;
21667c478bdstevel@tonic-gate	enum clnt_stat cl_stat;
21677c478bdstevel@tonic-gate	int rc;
21687c478bdstevel@tonic-gate
21697c478bdstevel@tonic-gate	rc = clnt_tli_kcreate(knconf, call_addr, prognum, versnum,
21707c478bdstevel@tonic-gate	    0, retries, CRED(), &cl);
21717c478bdstevel@tonic-gate	if (rc) {
21727c478bdstevel@tonic-gate		nfs_perror(rc, "mycallrpc: clnt_tli_kcreate failed: %m\n");
21737c478bdstevel@tonic-gate		return (RPC_SYSTEMERROR);	/* XXX */
21747c478bdstevel@tonic-gate	}
21757c478bdstevel@tonic-gate	tv.tv_sec = timeo;
21767c478bdstevel@tonic-gate	tv.tv_usec = 0;
21777c478bdstevel@tonic-gate	cl_stat = CLNT_CALL(