xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_dlinet.c (revision cd277642e0bdc71a7f57c550df1279b0c091d6e2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/cred.h>
30 #include <sys/user.h>
31 #include <sys/file.h>
32 #include <sys/stream.h>
33 #include <sys/strsubr.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/debug.h>
37 #include <sys/tiuser.h>
38 #include <sys/sockio.h>
39 #include <sys/socket.h>
40 #include <sys/t_kuser.h>
41 #include <sys/utsname.h>
42 #include <sys/systeminfo.h>
43 #include <sys/netconfig.h>
44 #include <sys/ethernet.h>
45 #include <sys/dlpi.h>
46 #include <sys/vfs.h>
47 #include <sys/sysmacros.h>
48 #include <sys/bootconf.h>
49 #include <sys/bootprops.h>
50 #include <sys/cmn_err.h>
51 #include <sys/promif.h>
52 #include <sys/mount.h>
53 
54 #include <net/if.h>
55 #include <net/route.h>
56 
57 #include <netinet/in.h>
58 #include <netinet/arp.h>
59 #include <netinet/dhcp.h>
60 #include <netinet/inetutil.h>
61 #include <dhcp_impl.h>
62 #include <sys/sunos_dhcp_class.h>
63 
64 #include <rpc/types.h>
65 #include <rpc/rpc.h>
66 #include <rpc/xdr.h>
67 #include <rpc/auth.h>
68 #include <rpc/clnt.h>
69 #include <rpc/pmap_clnt.h>
70 #include <rpc/pmap_rmt.h>
71 #include <rpc/pmap_prot.h>
72 #include <rpc/bootparam.h>
73 #include <rpc/rpcb_prot.h>
74 
75 #include <nfs/nfs.h>
76 #include <nfs/nfs4.h>
77 #include <nfs/nfs_clnt.h>
78 #include <nfs/mount.h>
79 #include <sys/mntent.h>
80 
81 #include <sys/kstr.h>
82 #include <sys/sunddi.h>
83 #include <sys/sunldi.h>
84 #include <sys/esunddi.h>
85 
86 #include <sys/errno.h>
87 #include <sys/modctl.h>
88 
89 /*
90  * RPC timers and retries
91  */
92 #define	PMAP_RETRIES	5
93 #define	DEFAULT_RETRIES	3
94 #define	GETFILE_RETRIES	2
95 
96 #define	DEFAULT_TIMEO	3
97 #define	WHOAMI_TIMEO	20
98 #define	REVARP_TIMEO	5
99 #define	GETFILE_TIMEO	1
100 
101 /*
102  * These are from the rpcgen'd version of mount.h XXX
103  */
104 #define	MOUNTPROG 100005
105 #define	MOUNTPROC_MNT		1
106 #define	MOUNTVERS		1
107 #define	MOUNTVERS_POSIX		2
108 #define	MOUNTVERS3		3
109 
110 struct fhstatus {
111 	int fhs_status;
112 	fhandle_t fhs_fh;
113 };
114 
115 #define	FHSIZE3 64
116 
117 struct fhandle3 {
118 	uint_t fhandle3_len;
119 	char *fhandle3_val;
120 };
121 
122 enum mountstat3 {
123 	MNT_OK = 0,
124 	MNT3ERR_PERM = 1,
125 	MNT3ERR_NOENT = 2,
126 	MNT3ERR_IO = 5,
127 	MNT3ERR_ACCES = 13,
128 	MNT3ERR_NOTDIR = 20,
129 	MNT3ERR_INVAL = 22,
130 	MNT3ERR_NAMETOOLONG = 63,
131 	MNT3ERR_NOTSUPP = 10004,
132 	MNT3ERR_SERVERFAULT = 10006
133 };
134 
135 struct mountres3_ok {
136 	struct fhandle3 fhandle;
137 	struct {
138 		uint_t auth_flavors_len;
139 		int *auth_flavors_val;
140 	} auth_flavors;
141 };
142 
143 struct mountres3 {
144 	enum mountstat3 fhs_status;
145 	union {
146 		struct mountres3_ok mountinfo;
147 	} mountres3_u;
148 };
149 
150 /*
151  * DLPI address format.
152  */
153 struct	dladdr {
154 	uchar_t		dl_phys[6];
155 	ushort_t	dl_sap;
156 };
157 
158 static struct modlmisc modlmisc = {
159 	&mod_miscops, "Boot diskless"
160 };
161 
162 static struct modlinkage modlinkage = {
163 	MODREV_1, (void *)&modlmisc, NULL
164 };
165 
166 static int	dldebug;
167 
168 int
169 _init(void)
170 {
171 	return (mod_install(&modlinkage));
172 }
173 
174 int
175 _fini(void)
176 {
177 	return (mod_remove(&modlinkage));
178 }
179 
180 int
181 _info(struct modinfo *modinfop)
182 {
183 	return (mod_info(&modlinkage, modinfop));
184 }
185 
186 
187 static enum clnt_stat	pmap_rmt_call(struct knetconfig *, struct netbuf *,
188 			    bool_t, rpcprog_t, rpcvers_t, rpcproc_t, xdrproc_t,
189 			    caddr_t, xdrproc_t, caddr_t, struct timeval,
190 			    struct netbuf *);
191 static bool_t		myxdr_rmtcall_args(XDR *, struct rmtcallargs *);
192 static bool_t		myxdr_rmtcallres(XDR *, struct rmtcallres *);
193 static bool_t		myxdr_pmap(XDR *, struct pmap *);
194 static bool_t		myxdr_fhstatus(XDR *xdrs, struct fhstatus *fhsp);
195 static bool_t		myxdr_fhandle(XDR *xdrs, fhandle_t *fh);
196 static bool_t		myxdr_mountres3(XDR *xdrs, struct mountres3 *objp);
197 static bool_t		myxdr_mountstat3(XDR *xdrs, enum mountstat3 *objp);
198 static bool_t		myxdr_mountres3_ok(XDR *xdrs,
199 			    struct mountres3_ok *objp);
200 static bool_t		myxdr_fhandle3(XDR *xdrs, struct fhandle3 *objp);
201 static enum clnt_stat	pmap_kgetport(struct knetconfig *, struct netbuf *,
202 			    rpcprog_t, rpcvers_t, rpcprot_t);
203 static enum clnt_stat	mycallrpc(struct knetconfig *, struct netbuf *,
204 			    rpcprog_t, rpcvers_t, rpcproc_t, xdrproc_t,
205 			    char *, xdrproc_t, char *, int, int);
206 static int		ifioctl(TIUSER *, int, struct netbuf *);
207 static int		getfile(char *, char *, struct netbuf *, char *);
208 static int		ping_prog(struct netbuf *, uint_t prog, uint_t vers,
209 			    int proto, enum clnt_stat *);
210 static int		mountnfs(struct netbuf *, char *, char *,
211 			    fhandle_t *, int *);
212 static int		mountnfs3(struct netbuf *, char *, char *,
213 			    nfs_fh3 *, int *);
214 static int		init_mountopts(struct nfs_args *, int,
215 			    struct knetconfig **, int *);
216 static int		revarp_myaddr(TIUSER *);
217 static void		revarp_start(ldi_handle_t, struct netbuf *);
218 static void		revarpinput(ldi_handle_t, struct netbuf *);
219 static void		init_netbuf(struct netbuf *);
220 static void		free_netbuf(struct netbuf *);
221 static int		rtioctl(TIUSER *, int, struct rtentry *);
222 static void		init_config(void);
223 
224 static void		cacheinit(void);
225 static int		cacheinfo(char *, int, struct netbuf *, char *, int);
226 static int		dlifconfig(TIUSER *, struct in_addr *, struct in_addr *,
227 			    struct in_addr *, uint_t);
228 static int		setifflags(TIUSER *, uint_t);
229 
230 static char		*inet_ntoa(struct in_addr);
231 static int		inet_aton(char *, uchar_t *);
232 static int		isdigit(int);
233 
234 /*
235  * Should be in some common
236  * ethernet source file.
237  */
238 static struct ether_addr etherbroadcastaddr = {
239 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
240 };
241 
242 static struct ether_addr myether;
243 
244 /*
245  * "ifname" is the interface name/unit as read from the boot
246  * arguments.
247  * "ndev" is the major device number of the network interface
248  * used to boot from.
249  * "ifunit" it the physical point of attachment for the network
250  * interface used to boot from.
251  *
252  * Both of these are initialized in "init_config()".
253  */
254 
255 static char	ifname[IFNAMSIZ];
256 static char	ndev_path[MAXPATHLEN];
257 static int	ifunit;
258 
259 /*
260  * XXX these should be shared
261  */
262 static struct knetconfig dl_udp_netconf = {
263 	NC_TPI_CLTS,			/* semantics */
264 	NC_INET,			/* family */
265 	NC_UDP,				/* protocol */
266 	0,				/* device */
267 };
268 
269 static struct knetconfig dl_tcp_netconf = {
270 	NC_TPI_COTS,			/* semantics */
271 	NC_INET,			/* family */
272 	NC_TCP,				/* protocol */
273 	0,				/* device */
274 };
275 
276 /* parameters from DHCP or bootparamd */
277 static PKT_LIST	*pl = NULL;
278 static uchar_t server_ip[4];
279 static uchar_t dhcp_server_ip[4];
280 static char *server_name_c, *server_path_c;
281 static char rootopts[256];
282 
283 /*
284  * XXX Until we get the nfsmapid deadlocks all fixed, don't allow
285  * XXX a v4 root mount.
286  */
287 int nfs4_no_diskless_root_support = 1;
288 
289 int
290 mount_root(char *name, char *path, int version, struct nfs_args *args,
291     int *vfsflags)
292 {
293 	int rc;
294 	int proto;
295 	struct knetconfig *dl_cf;
296 	static int init_done = 0;
297 	enum clnt_stat stat;
298 
299 	if (dldebug)
300 		printf("mount_root: name=%s\n", name);
301 
302 	if (init_done == 0) {
303 		init_config();
304 		init_done = 1;
305 	}
306 
307 	init_netbuf(args->addr);
308 
309 	do {
310 		rc = getfile(name, args->hostname, args->addr, path);
311 	} while (rc == ETIMEDOUT);
312 
313 	if (rc) {
314 		free_netbuf(args->addr);
315 		return (rc);
316 	}
317 
318 	ASSERT(args->knconf->knc_protofmly != NULL);
319 	ASSERT(args->knconf->knc_proto != NULL);
320 
321 	switch (version) {
322 	case NFS_VERSION:
323 		rc = mountnfs(args->addr, args->hostname, path,
324 		    (fhandle_t *)args->fh, &proto);
325 		break;
326 	case NFS_V3:
327 		rc = mountnfs3(args->addr, args->hostname, path,
328 		    (nfs_fh3 *)args->fh, &proto);
329 		break;
330 	case NFS_V4:
331 		((struct sockaddr_in *)args->addr->buf)->sin_port =
332 		    htons(NFS_PORT);
333 		if (ping_prog(args->addr, NFS_PROGRAM, NFS_V4, IPPROTO_TCP,
334 		    &stat)) {
335 			proto = IPPROTO_TCP;
336 			rc = 0;
337 		} else {
338 			switch (stat) {
339 			case RPC_PROGVERSMISMATCH:
340 			case RPC_XPRTFAILED:
341 				/*
342 				 * Common failures if v4 unsupported or no TCP
343 				 */
344 				rc = EPROTONOSUPPORT;
345 				break;
346 			default:
347 				rc = ENXIO;
348 			}
349 		}
350 		if (nfs4_no_diskless_root_support)
351 			rc = EPROTONOSUPPORT;
352 		break;
353 	default:
354 		rc = EPROTONOSUPPORT;
355 		break;
356 	}
357 
358 	if (rc)
359 		goto errout;
360 
361 	switch (proto) {
362 	case IPPROTO_TCP:
363 		dl_cf = &dl_tcp_netconf;
364 		break;
365 	case IPPROTO_UDP:
366 	default:
367 		dl_cf = &dl_udp_netconf;
368 		break;
369 	}
370 
371 	rc = init_mountopts(args, version, &dl_cf, vfsflags);
372 
373 	/*
374 	 * Copy knetconfig information from the template, note that the
375 	 * rdev field has been set by init_config above.
376 	 */
377 	args->knconf->knc_semantics = dl_cf->knc_semantics;
378 	args->knconf->knc_rdev = dl_cf->knc_rdev;
379 	(void) strcpy(args->knconf->knc_protofmly, dl_cf->knc_protofmly);
380 	(void) strcpy(args->knconf->knc_proto, dl_cf->knc_proto);
381 
382 errout:
383 	if (dldebug) {
384 		if (rc)
385 			nfs_perror(rc, "mount_root: mount %s:%s failed: %m\n",
386 			    args->hostname, path);
387 		else
388 			printf("mount_root: leaving\n");
389 	}
390 
391 	return (rc);
392 }
393 
394 /*
395  * Call mount daemon on server `sa' to mount path.
396  * `port' is set to nfs port and fh is the fhandle
397  * returned from the server.
398  */
399 static int
400 mountnfs(struct netbuf *sa, char *server,
401     char *path, fhandle_t *fh, int *proto)
402 {
403 	struct fhstatus fhs;
404 	enum clnt_stat stat;
405 
406 	if (dldebug)
407 		printf("mountnfs: entered\n");
408 
409 	/*
410 	 * Get the port number for the mount program.
411 	 * pmap_kgetport first tries a SunOS portmapper
412 	 * and, if no reply is received, will try a
413 	 * SVR4 rpcbind. Either way, `sa' is set to
414 	 * the correct address.
415 	 */
416 	do {
417 		stat = pmap_kgetport(&dl_udp_netconf, sa, (rpcprog_t)MOUNTPROG,
418 		    (rpcvers_t)MOUNTVERS, (rpcprot_t)IPPROTO_UDP);
419 
420 		if (stat == RPC_TIMEDOUT) {
421 			cmn_err(CE_WARN,
422 			    "mountnfs: %s:%s portmap not responding",
423 			    server, path);
424 		} else if (stat != RPC_SUCCESS) {
425 			cmn_err(CE_WARN,
426 			    "mountnfs: pmap_kgetport RPC error %d (%s).",
427 			    stat, clnt_sperrno(stat));
428 			return (ENXIO);	/* XXX */
429 		}
430 	} while (stat == RPC_TIMEDOUT);
431 
432 	/*
433 	 * The correct port number has been
434 	 * put into `sa' by pmap_kgetport().
435 	 */
436 	do {
437 		stat = mycallrpc(&dl_udp_netconf, sa, (rpcprog_t)MOUNTPROG,
438 		    (rpcvers_t)MOUNTVERS, (rpcproc_t)MOUNTPROC_MNT,
439 		    xdr_bp_path_t, (char *)&path,
440 		    myxdr_fhstatus, (char *)&fhs,
441 		    DEFAULT_TIMEO, DEFAULT_RETRIES);
442 		if (stat == RPC_TIMEDOUT) {
443 			cmn_err(CE_WARN,
444 			    "mountnfs: %s:%s mount server not responding",
445 			    server, path);
446 		}
447 	} while (stat == RPC_TIMEDOUT);
448 
449 	if (stat != RPC_SUCCESS) {
450 		cmn_err(CE_WARN, "mountnfs: RPC failed: error %d (%s).",
451 		    stat, clnt_sperrno(stat));
452 		return (ENXIO);	/* XXX */
453 	}
454 
455 	((struct sockaddr_in *)sa->buf)->sin_port = htons(NFS_PORT);
456 
457 	*fh = fhs.fhs_fh;
458 	if (fhs.fhs_status != 0) {
459 		if (dldebug)
460 			printf("mountnfs: fhs_status %d\n", fhs.fhs_status);
461 		return (ENXIO);		/* XXX */
462 	}
463 
464 	*proto = IPPROTO_UDP;
465 
466 	if (ping_prog(sa, NFS_PROGRAM, NFS_VERSION, IPPROTO_TCP, NULL))
467 		*proto = IPPROTO_TCP;
468 
469 	if (dldebug)
470 		printf("mountnfs: leaving\n");
471 	return (0);
472 }
473 
474 /*
475  * Call mount daemon on server `sa' to mount path.
476  * `port' is set to nfs port and fh is the fhandle
477  * returned from the server.
478  */
479 static int
480 mountnfs3(struct netbuf *sa, char *server,
481     char *path, nfs_fh3 *fh, int *proto)
482 {
483 	struct mountres3 mountres3;
484 	enum clnt_stat stat;
485 	int ret = 0;
486 
487 	if (dldebug)
488 		printf("mountnfs3: entered\n");
489 
490 	/*
491 	 * Get the port number for the mount program.
492 	 * pmap_kgetport first tries a SunOS portmapper
493 	 * and, if no reply is received, will try a
494 	 * SVR4 rpcbind. Either way, `sa' is set to
495 	 * the correct address.
496 	 */
497 	do {
498 		stat = pmap_kgetport(&dl_udp_netconf, sa, (rpcprog_t)MOUNTPROG,
499 		    (rpcvers_t)MOUNTVERS3, (rpcprot_t)IPPROTO_UDP);
500 
501 		if (stat == RPC_PROGVERSMISMATCH) {
502 			if (dldebug)
503 				printf("mountnfs3: program/version mismatch\n");
504 			return (EPROTONOSUPPORT); /* XXX */
505 		} else if (stat == RPC_TIMEDOUT) {
506 			cmn_err(CE_WARN,
507 			    "mountnfs3: %s:%s portmap not responding",
508 			    server, path);
509 		} else if (stat != RPC_SUCCESS) {
510 			cmn_err(CE_WARN,
511 			    "mountnfs3: pmap_kgetport RPC error %d (%s).",
512 			    stat, clnt_sperrno(stat));
513 			return (ENXIO);	/* XXX */
514 		}
515 	} while (stat == RPC_TIMEDOUT);
516 
517 	mountres3.mountres3_u.mountinfo.fhandle.fhandle3_val = NULL;
518 	mountres3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val = NULL;
519 
520 	/*
521 	 * The correct port number has been
522 	 * put into `sa' by pmap_kgetport().
523 	 */
524 	do {
525 		stat = mycallrpc(&dl_udp_netconf, sa, (rpcprog_t)MOUNTPROG,
526 		    (rpcvers_t)MOUNTVERS3, (rpcproc_t)MOUNTPROC_MNT,
527 		    xdr_bp_path_t, (char *)&path,
528 		    myxdr_mountres3, (char *)&mountres3,
529 		    DEFAULT_TIMEO, DEFAULT_RETRIES);
530 		if (stat == RPC_TIMEDOUT) {
531 			cmn_err(CE_WARN,
532 			    "mountnfs3: %s:%s mount server not responding",
533 			    server, path);
534 		}
535 	} while (stat == RPC_TIMEDOUT);
536 
537 	if (stat == RPC_PROGVERSMISMATCH) {
538 		if (dldebug)
539 			printf("mountnfs3: program/version mismatch\n");
540 		ret = EPROTONOSUPPORT;
541 		goto out;
542 	}
543 	if (stat != RPC_SUCCESS) {
544 		cmn_err(CE_WARN, "mountnfs3: RPC failed: error %d (%s).",
545 		    stat, clnt_sperrno(stat));
546 		ret = ENXIO;	/* XXX */
547 		goto out;
548 	}
549 
550 	if (mountres3.fhs_status != MNT_OK) {
551 		if (dldebug)
552 			printf("mountnfs3: fhs_status %d\n",
553 			    mountres3.fhs_status);
554 		ret = ENXIO;	/* XXX */
555 		goto out;
556 	}
557 
558 	((struct sockaddr_in *)sa->buf)->sin_port = htons(NFS_PORT);
559 
560 	*proto = IPPROTO_UDP;
561 
562 	if (ping_prog(sa, NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NULL)) {
563 		*proto = IPPROTO_TCP;
564 	}
565 
566 	fh->fh3_length = mountres3.mountres3_u.mountinfo.fhandle.fhandle3_len;
567 	bcopy(mountres3.mountres3_u.mountinfo.fhandle.fhandle3_val,
568 	    fh->fh3_u.data, fh->fh3_length);
569 
570 out:
571 	xdr_free(myxdr_mountres3, (caddr_t)&mountres3);
572 
573 	if (dldebug)
574 		printf("mountnfs3: leaving\n");
575 	return (ret);
576 }
577 
578 static int
579 ping_prog(struct netbuf *call_addr, uint_t prog, uint_t vers, int proto,
580     enum clnt_stat *statp)
581 {
582 	struct knetconfig *knconf;
583 	enum clnt_stat stat;
584 	int retries = DEFAULT_RETRIES;
585 
586 	switch (proto) {
587 	case IPPROTO_TCP:
588 		knconf = &dl_tcp_netconf;
589 		break;
590 	case IPPROTO_UDP:
591 		knconf = &dl_udp_netconf;
592 		break;
593 	default:
594 		return (0);
595 	}
596 
597 	do {
598 		stat = mycallrpc(knconf, call_addr, prog, vers, NULLPROC,
599 		    xdr_void, NULL, xdr_void, NULL,
600 		    DEFAULT_TIMEO, DEFAULT_RETRIES);
601 
602 		if (dldebug)
603 			printf("ping_prog: %d return %d (%s)\n", proto, stat,
604 			    clnt_sperrno(stat));
605 		/*
606 		 * Special case for TCP, it may "timeout" because it failed
607 		 * to establish an initial connection but it doesn't
608 		 * actually retry, so we do the retry.
609 		 * Persistence pays in diskless.
610 		 */
611 	} while (stat == RPC_TIMEDOUT && proto == IPPROTO_TCP && retries--);
612 
613 	if (statp != NULL)
614 		*statp = stat;
615 
616 	if (stat != RPC_SUCCESS)
617 		return (0);
618 	return (1);
619 }
620 
621 static struct netbuf bootparam_addr;
622 
623 /*
624  * Returns after filling in the following global variables:
625  *	bootparam_addr,
626  *	utsname.nodename,
627  *	srpc_domain.
628  */
629 static int
630 whoami(void)
631 {
632 	TIUSER *tiptr;
633 	struct netbuf sa;
634 	struct netbuf req;
635 	struct bp_whoami_arg arg;
636 	struct bp_whoami_res res;
637 	struct timeval tv;
638 	enum clnt_stat stat;
639 	int rc;
640 	size_t namelen;
641 	int printed_waiting_msg;
642 
643 	if ((rc = t_kopen((file_t *)NULL, dl_udp_netconf.knc_rdev,
644 	    FREAD|FWRITE, &tiptr, CRED())) != 0) {
645 		nfs_perror(rc, "whoami: t_kopen udp failed: %m.\n");
646 	}
647 
648 	/*
649 	 * Find out our local (IP) address.
650 	 */
651 	if (rc = revarp_myaddr(tiptr)) {
652 		nfs_perror(rc, "whoami: revarp_myaddr failed: %m.\n");
653 		(void) t_kclose(tiptr, 0);
654 		return (rc);
655 	}
656 
657 	/* explicitly use the limited broadcast address */
658 	init_netbuf(&sa);
659 	((struct sockaddr_in *)sa.buf)->sin_family = AF_INET;
660 	((struct sockaddr_in *)sa.buf)->sin_addr.s_addr =
661 	    htonl(INADDR_BROADCAST);
662 	sa.len = sizeof (struct sockaddr_in);
663 
664 	/*
665 	 * Pick up our local (IP) address.
666 	 */
667 	init_netbuf(&req);
668 	if (rc = ifioctl(tiptr, SIOCGIFADDR, &req)) {
669 		nfs_perror(rc,
670 		    "whoami: couldn't get my IP address: %m.\n");
671 		free_netbuf(&sa);
672 		free_netbuf(&req);
673 		(void) t_kclose(tiptr, 0);
674 		return (rc);
675 	}
676 
677 	/*
678 	 * Set up the arguments expected by bootparamd.
679 	 */
680 	arg.client_address.address_type = IP_ADDR_TYPE;
681 	bcopy(&((struct sockaddr_in *)req.buf)->sin_addr,
682 	    &arg.client_address.bp_address.ip_addr, sizeof (struct in_addr));
683 
684 	free_netbuf(&req);
685 
686 	init_netbuf(&bootparam_addr);
687 
688 	/*
689 	 * Initial retransmission interval
690 	 */
691 	tv.tv_sec = DEFAULT_TIMEO;
692 	tv.tv_usec = 0;
693 	res.client_name = kmem_alloc(MAX_MACHINE_NAME + 1, KM_SLEEP);
694 	res.domain_name = kmem_alloc(MAX_MACHINE_NAME + 1, KM_SLEEP);
695 
696 	/*
697 	 * Do a broadcast call to find a bootparam daemon that
698 	 * will tell us our hostname, domainname and any
699 	 * router that we have to use to talk to our NFS server.
700 	 */
701 	printed_waiting_msg = 0;
702 	do {
703 		/*
704 		 * pmap_rmt_call will first try the SunOS portmapper
705 		 * and if no reply is received will then try the SVR4
706 		 * rpcbind.
707 		 * Either way, `bootparam_addr' will be set to the
708 		 * correct address for the bootparamd that responds.
709 		 */
710 		stat = pmap_rmt_call(&dl_udp_netconf, &sa, TRUE, BOOTPARAMPROG,
711 		    BOOTPARAMVERS, BOOTPARAMPROC_WHOAMI,
712 		    xdr_bp_whoami_arg, (caddr_t)&arg,
713 		    xdr_bp_whoami_res, (caddr_t)&res,
714 		    tv, &bootparam_addr);
715 		if (stat == RPC_TIMEDOUT && !printed_waiting_msg) {
716 			cmn_err(CE_WARN,
717 			    "No bootparam server responding; still trying");
718 			printed_waiting_msg = 1;
719 		}
720 		/*
721 		 * Retransmission interval for second and subsequent tries.
722 		 * We expect first pmap_rmt_call to retransmit and backoff to
723 		 * at least this value.
724 		 */
725 		tv.tv_sec = WHOAMI_TIMEO;
726 		tv.tv_usec = 0;
727 	} while (stat == RPC_TIMEDOUT);
728 
729 	if (printed_waiting_msg)
730 		printf("Bootparam response received\n");
731 
732 	if (stat != RPC_SUCCESS) {
733 		/* XXX should get real error here */
734 		rc = ENXIO;
735 		cmn_err(CE_WARN,
736 		    "whoami: bootparam RPC failed: error %d (%s).",
737 		    stat, clnt_sperrno(stat));
738 		goto done;
739 	}
740 
741 	namelen = strlen(res.client_name);
742 	if (namelen > sizeof (utsname.nodename)) {
743 		printf("whoami: hostname too long");
744 		rc = ENAMETOOLONG;
745 		goto done;
746 	}
747 	if (namelen != 0) {
748 		bcopy(res.client_name, &utsname.nodename, namelen);
749 		cmn_err(CE_CONT, "?hostname: %s\n", utsname.nodename);
750 	} else {
751 		printf("whoami: no host name\n");
752 		rc = ENXIO;
753 		goto done;
754 	}
755 
756 	namelen = strlen(res.domain_name);
757 	if (namelen != 0) {
758 		if (namelen > SYS_NMLN) {
759 			printf("whoami: domainname too long");
760 			rc = ENAMETOOLONG;
761 			goto done;
762 		}
763 		bcopy(res.domain_name, &srpc_domain, namelen);
764 		cmn_err(CE_CONT, "?domainname: %s\n", srpc_domain);
765 	} else {
766 		printf("whoami: no domain name\n");
767 	}
768 
769 	if (res.router_address.address_type == IP_ADDR_TYPE) {
770 		struct rtentry		rtentry;
771 		struct sockaddr_in	*sin;
772 		struct in_addr		ipaddr;
773 
774 		bcopy(&res.router_address.bp_address.ip_addr, &ipaddr,
775 		    sizeof (struct in_addr));
776 
777 		if (ipaddr.s_addr != (uint32_t)0) {
778 			sin = (struct sockaddr_in *)&rtentry.rt_dst;
779 			bzero(sin, sizeof (*sin));
780 			sin->sin_family = AF_INET;
781 
782 			sin = (struct sockaddr_in *)&rtentry.rt_gateway;
783 			bzero(sin, sizeof (*sin));
784 			sin->sin_family = AF_INET;
785 			sin->sin_addr.s_addr = ipaddr.s_addr;
786 
787 			rtentry.rt_flags = RTF_GATEWAY | RTF_UP;
788 
789 			if (rc = rtioctl(tiptr, SIOCADDRT, &rtentry)) {
790 				nfs_perror(rc,
791 				    "whoami: couldn't add route: %m.\n");
792 				goto done;
793 			}
794 		}
795 	} else {
796 		printf("whoami: unknown gateway addr family %d\n",
797 		    res.router_address.address_type);
798 	}
799 done:
800 	kmem_free(res.client_name, MAX_MACHINE_NAME + 1);
801 	kmem_free(res.domain_name, MAX_MACHINE_NAME + 1);
802 	free_netbuf(&sa);
803 	(void) t_kclose(tiptr, 0);
804 	return (rc);
805 }
806 
807 /*
808  * Returns:
809  *	1) The ascii form of our root servers name in `server_name'.
810  *	2) Actual network address of our root server in `server_address'.
811  *	3) Whatever BOOTPARAMPROC_GETFILE returns for the fileid key, in
812  *	   `server_path'.  If fileid is "root", it is the pathname of our
813  *	   root on the server.
814  */
815 static int
816 getfile(char *fileid,
817     char *server_name, struct netbuf *server_address, char *server_path)
818 {
819 	struct bp_getfile_arg arg;
820 	struct bp_getfile_res res;
821 	enum clnt_stat stat;
822 	int root = FALSE;
823 	static int using_cache = FALSE;
824 	struct in_addr ipaddr;
825 	int timeo = DEFAULT_TIMEO;
826 	int retries = DEFAULT_RETRIES;
827 
828 	if (dldebug)
829 		printf("getfile: entered\n");
830 
831 	/*
832 	 * Call cacheinfo() to see whether we can satisfy this request by using
833 	 * the information cached in memory by the boot program's DHCP
834 	 * implementation or boot properties rather than consult BOOTPARAMS,
835 	 * but while preserving the semantics of getfile(). We know that
836 	 * the server name is SYS_NMLN in length, and server_path is
837 	 * MAXPATHLEN (pn_alloc).
838 	 */
839 	if (strcmp(fileid, "root") == 0) {
840 		if (cacheinfo(server_name, SYS_NMLN, server_address,
841 		    server_path, MAXPATHLEN) == 0) {
842 			using_cache = TRUE;
843 			return (0);
844 		}
845 		root = TRUE;
846 	}
847 
848 	/*
849 	 * If using cache, rootopts is already available.
850 	 */
851 	if (strcmp(fileid, "rootopts") == 0 && using_cache == TRUE) {
852 		return (rootopts[0] != 0 ? 0 : ENXIO);
853 	}
854 
855 	if (bootparam_addr.len == 0) {
856 		return (ENXIO);
857 	}
858 	arg.client_name = (caddr_t)&utsname.nodename;
859 	arg.file_id = fileid;
860 
861 	bzero(&res, sizeof (res));
862 	res.server_name = kmem_alloc(MAX_MACHINE_NAME + 1, KM_SLEEP);
863 	res.server_path = kmem_alloc(MAX_MACHINE_NAME + 1, KM_SLEEP);
864 
865 	/*
866 	 * If we are not looking up the root file, we are looking
867 	 * up a non-critical option that should timeout quickly.
868 	 */
869 	if (!root) {
870 		timeo = GETFILE_TIMEO;
871 		retries = GETFILE_RETRIES;
872 	}
873 
874 	/*
875 	 * bootparam_addr was filled in by the call to
876 	 * whoami(), so now send an rpc message to the
877 	 * bootparam daemon requesting our server information.
878 	 * Use UDP to talk to bootparms.
879 	 */
880 	stat = mycallrpc(&dl_udp_netconf, &bootparam_addr,
881 	    (rpcprog_t)BOOTPARAMPROG, (rpcvers_t)BOOTPARAMVERS,
882 	    (rpcproc_t)BOOTPARAMPROC_GETFILE,
883 	    xdr_bp_getfile_arg, (caddr_t)&arg,
884 	    xdr_bp_getfile_res, (caddr_t)&res,
885 	    timeo, retries);
886 
887 	if (stat == RPC_SUCCESS) {
888 		(void) strcpy(server_name, res.server_name);
889 		(void) strcpy(server_path, res.server_path);
890 	}
891 
892 	kmem_free(res.server_name, MAX_MACHINE_NAME + 1);
893 	kmem_free(res.server_path, MAX_MACHINE_NAME + 1);
894 
895 	if (stat != RPC_SUCCESS) {
896 		if (root)
897 			cmn_err(CE_WARN, "getfile: RPC failed: error %d (%s).",
898 			    stat, clnt_sperrno(stat));
899 		return ((stat == RPC_TIMEDOUT) ? ETIMEDOUT : ENXIO); /* XXX */
900 	}
901 
902 	if (*server_path == '\0')
903 		return (EINVAL);
904 
905 	/*
906 	 * If the fileid is "root", we must get back a server name, for
907 	 * other parameters a server name is not required
908 	 */
909 	if (!root) {
910 		if (dldebug)
911 			printf("getfile: leaving: non-root\n");
912 		return (0);
913 	}
914 
915 	if (*server_name == '\0')
916 		return (EINVAL);
917 
918 	switch (res.server_address.address_type) {
919 	case IP_ADDR_TYPE:
920 		/*
921 		 * server_address is where we will get our root
922 		 * from.
923 		 */
924 		((struct sockaddr_in *)server_address->buf)->sin_family =
925 		    AF_INET;
926 		bcopy(&res.server_address.bp_address.ip_addr,
927 		    &ipaddr, sizeof (ipaddr));
928 		if (ipaddr.s_addr == 0)
929 			return (EINVAL);
930 
931 		((struct sockaddr_in *)server_address->buf)->sin_addr.s_addr =
932 		    ipaddr.s_addr;
933 		server_address->len = sizeof (struct sockaddr_in);
934 		break;
935 
936 	default:
937 		printf("getfile: unknown address type %d\n",
938 		    res.server_address.address_type);
939 		return (EPROTONOSUPPORT);
940 	}
941 	if (dldebug)
942 		printf("getfile: leaving\n");
943 	return (0);
944 }
945 
946 /*
947  * If the boot property "bootp-response" exists, then OBP performed a
948  * successful DHCP lease acquisition for us and left the resultant ACK packet
949  * encoded at that location.
950  *
951  * If no such property exists (or the information is incomplete or garbled),
952  * the function returns -1.
953  */
954 int
955 dhcpinit(void)
956 {
957 	int rc, i;
958 	char *p;
959 	struct in_addr braddr;
960 	struct in_addr subnet;
961 	DHCP_OPT *doptp;
962 	TIUSER *tiptr;
963 	struct sockaddr_in *sin;
964 	static int once_only = 0;
965 
966 	if (once_only == 1) {
967 		return (0);
968 	}
969 	once_only = 1;
970 
971 	if (dhcack == NULL) {
972 		return (-1);
973 	}
974 
975 	if (dldebug) {
976 		printf("dhcp:  dhcack %p, len %d\n", (void *)dhcack,
977 		    dhcacklen);
978 	}
979 
980 	pl = kmem_alloc(sizeof (PKT_LIST), KM_SLEEP);
981 	pl->len = dhcacklen;
982 	pl->pkt = kmem_alloc(pl->len, KM_SLEEP);
983 	bcopy(dhcack, pl->pkt, dhcacklen);
984 
985 	/*
986 	 * For x86, ifname is not initialized
987 	 * in the netinstall case and dhcack interface name is
988 	 * set in strplumb(). So we only copy the name if ifname
989 	 * is set properly.
990 	 */
991 	if (ifname[0])
992 		(void) strlcpy(dhcifname, ifname, sizeof (dhcifname));
993 
994 	/* remember the server_ip in dhcack */
995 	bcopy((uchar_t *)pl->pkt + 20, dhcp_server_ip, 4);
996 	bzero(pl->opts, (DHCP_LAST_OPT + 1) * sizeof (DHCP_OPT *));
997 	bzero(pl->vs, (VS_OPTION_END - VS_OPTION_START + 1) *
998 	    sizeof (DHCP_OPT *));
999 
1000 	if (dhcp_options_scan(pl, B_TRUE) != 0) {
1001 		/* garbled packet */
1002 		cmn_err(CE_WARN, "dhcp: DHCP packet parsing failed");
1003 		kmem_free(pl->pkt, pl->len);
1004 		kmem_free(pl, sizeof (PKT_LIST));
1005 		pl = NULL;
1006 		return (-1);
1007 	}
1008 
1009 	/* set node name */
1010 	if (pl->opts[CD_HOSTNAME] != NULL) {
1011 		doptp = pl->opts[CD_HOSTNAME];
1012 		i = doptp->len;
1013 		if (i >= SYS_NMLN) {
1014 			cmn_err(CE_WARN, "dhcp: Hostname is too long");
1015 		} else {
1016 			bcopy(doptp->value, utsname.nodename, i);
1017 			utsname.nodename[i] = '\0';
1018 			if (dldebug) {
1019 				printf("hostname is %s\n",
1020 				    utsname.nodename);
1021 			}
1022 		}
1023 	}
1024 
1025 	/* Set NIS domain name. */
1026 	p = NULL;
1027 	if (pl->opts[CD_NIS_DOMAIN] != NULL) {
1028 		doptp = pl->opts[CD_NIS_DOMAIN];
1029 		i = doptp->len;
1030 		p = (caddr_t)doptp->value;
1031 	}
1032 	if (p != NULL) {
1033 		if (i > SYS_NMLN) {
1034 			cmn_err(CE_WARN,
1035 			    "dhcp: NIS domainname too long.");
1036 		} else {
1037 			bcopy(p, srpc_domain, i);
1038 			srpc_domain[i] = '\0';
1039 			if (dldebug)
1040 				printf("dhcp: NIS domain name is %s\n",
1041 				    srpc_domain);
1042 		}
1043 	}
1044 
1045 	/* fetch netmask */
1046 	if (pl->opts[CD_SUBNETMASK] != NULL) {
1047 		doptp = pl->opts[CD_SUBNETMASK];
1048 		if (doptp->len != sizeof (struct in_addr)) {
1049 			pl->opts[CD_SUBNETMASK] = NULL;
1050 			cmn_err(CE_WARN, "dhcp: netmask option malformed");
1051 		} else {
1052 			bcopy(doptp->value, &subnet, sizeof (struct in_addr));
1053 			if (dldebug)
1054 				printf("dhcp:  setting netmask to: %s\n",
1055 				    inet_ntoa(subnet));
1056 		}
1057 	} else {
1058 		struct in_addr myIPaddr;
1059 
1060 		myIPaddr.s_addr = pl->pkt->yiaddr.s_addr;
1061 		cmn_err(CE_WARN, "dhcp:  no subnet mask supplied - inferring");
1062 		if (IN_CLASSA(ntohl(myIPaddr.s_addr)))
1063 			subnet.s_addr = htonl(IN_CLASSA_NET);
1064 		else if (IN_CLASSB(ntohl(myIPaddr.s_addr)))
1065 			subnet.s_addr = htonl(IN_CLASSB_NET);
1066 		else if (IN_CLASSC(ntohl(myIPaddr.s_addr)))
1067 			subnet.s_addr = htonl(IN_CLASSC_NET);
1068 		else if (IN_CLASSD(ntohl(myIPaddr.s_addr)))
1069 			cmn_err(CE_WARN, "dhcp:  bad IP address (%s)",
1070 			    inet_ntoa(myIPaddr));
1071 		else
1072 			subnet.s_addr = htonl(IN_CLASSE_NET);
1073 	}
1074 	/* and broadcast address */
1075 	if (pl->opts[CD_BROADCASTADDR] != NULL) {
1076 		doptp = pl->opts[CD_BROADCASTADDR];
1077 		if (doptp->len != sizeof (struct in_addr)) {
1078 			pl->opts[CD_BROADCASTADDR] = NULL;
1079 			if (dldebug)
1080 				printf("dhcp:  broadcast address len %d\n",
1081 				    doptp->len);
1082 		} else {
1083 			bcopy(doptp->value, &braddr, sizeof (struct in_addr));
1084 			if (dldebug)
1085 				printf("dhcp:  setting broadcast addr to: %s\n",
1086 				    inet_ntoa(braddr));
1087 		}
1088 	} else {
1089 		if (dldebug)
1090 			printf("dhcp:  no broadcast address supplied\n");
1091 		braddr.s_addr = htonl(INADDR_BROADCAST);
1092 	}
1093 	/* and plumb and initialize interface */
1094 	if ((rc = t_kopen((file_t *)NULL, dl_udp_netconf.knc_rdev,
1095 	    FREAD|FWRITE, &tiptr, CRED())) == 0) {
1096 		if (rc = dlifconfig(tiptr, &pl->pkt->yiaddr, &subnet,
1097 		    &braddr, IFF_DHCPRUNNING)) {
1098 			nfs_perror(rc, "dhcp: dlifconfig failed: %m\n");
1099 			kmem_free(pl->pkt, pl->len);
1100 			kmem_free(pl, sizeof (PKT_LIST));
1101 			pl = NULL;
1102 			(void) t_kclose(tiptr, 0);
1103 			return (-1);
1104 		}
1105 
1106 		/* add routes */
1107 		if (pl->opts[CD_ROUTER] != NULL) {
1108 			doptp = pl->opts[CD_ROUTER];
1109 			if ((doptp->len % sizeof (struct in_addr)) != 0) {
1110 				pl->opts[CD_ROUTER] = NULL;
1111 			} else {
1112 				int nrouters;
1113 				uchar_t *tp;
1114 
1115 				nrouters = doptp->len / sizeof (struct in_addr);
1116 				for (tp = doptp->value, i = 0; i < nrouters;
1117 				    i++) {
1118 					struct in_addr defr;
1119 					struct rtentry	rtentry;
1120 
1121 					bcopy(tp, &defr,
1122 					    sizeof (struct in_addr));
1123 					if (defr.s_addr == 0)
1124 						continue;
1125 
1126 					sin = (struct
1127 					    sockaddr_in *)&rtentry.rt_dst;
1128 
1129 					bzero(sin, sizeof (*sin));
1130 					sin->sin_family = AF_INET;
1131 
1132 					sin = (struct
1133 					    sockaddr_in *)&rtentry.rt_gateway;
1134 					bzero(sin, sizeof (*sin));
1135 					sin->sin_family = AF_INET;
1136 					sin->sin_addr = defr;
1137 
1138 					rtentry.rt_flags = RTF_GATEWAY | RTF_UP;
1139 
1140 					if (rc = rtioctl(tiptr, SIOCADDRT,
1141 					    &rtentry)) {
1142 						nfs_perror(rc,
1143 						    "dhcp: couldn't add route "
1144 						    "to %s: %m.\n",
1145 						    inet_ntoa(defr));
1146 							continue;
1147 					}
1148 					if (dldebug) {
1149 						printf("dhcp: added route %s\n",
1150 						    inet_ntoa(defr));
1151 					}
1152 					tp += sizeof (struct in_addr);
1153 				}
1154 			}
1155 		}
1156 
1157 		(void) t_kclose(tiptr, 0);
1158 	}
1159 
1160 	if (dldebug)
1161 		printf("dhcpinit: leaving\n");
1162 
1163 	return (0);
1164 }
1165 
1166 /*
1167  * Initialize nfs mount info from properties and dhcp response.
1168  */
1169 static void
1170 cacheinit(void)
1171 {
1172 	char *str;
1173 	DHCP_OPT *doptp;
1174 
1175 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1176 	    DDI_PROP_DONTPASS, BP_SERVER_PATH, &server_path_c);
1177 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1178 	    DDI_PROP_DONTPASS, BP_SERVER_NAME, &server_name_c);
1179 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1180 	    DDI_PROP_DONTPASS, BP_SERVER_ROOTOPTS, &str) == DDI_SUCCESS) {
1181 		(void) strncpy(rootopts, str, 255);
1182 		ddi_prop_free(str);
1183 	}
1184 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1185 	    DDI_PROP_DONTPASS, BP_SERVER_IP, &str) == DDI_SUCCESS) {
1186 		if (inet_aton(str, server_ip) != 0)
1187 			cmn_err(CE_NOTE, "server_ipaddr %s is invalid",
1188 			    str);
1189 		ddi_prop_free(str);
1190 		if (dldebug)
1191 			printf("server ip is %s\n",
1192 			    inet_ntoa(*(struct in_addr *)server_ip));
1193 	}
1194 
1195 	if (pl == NULL)
1196 		return;
1197 
1198 	/* extract root path in server_path */
1199 	if (server_path_c == NULL) {
1200 		doptp = pl->vs[VS_NFSMNT_ROOTPATH];
1201 		if (doptp == NULL)
1202 			doptp = pl->opts[CD_ROOT_PATH];
1203 		if (doptp != NULL) {
1204 			int len;
1205 			str = NULL;
1206 			for (len = 0; len < doptp->len; len++) {
1207 				if (doptp->value[len] == ':') {
1208 					str = (char *)(&doptp->value[++len]);
1209 					break;
1210 				}
1211 			}
1212 			if (str != NULL) {
1213 				/* Do not override server_ip from property. */
1214 				if ((*(uint_t *)server_ip) == 0) {
1215 					char *ip = kmem_alloc(len, KM_SLEEP);
1216 					bcopy(doptp->value, ip, len);
1217 					ip[len - 1] = '\0';
1218 					if (inet_aton((ip), server_ip) != 0) {
1219 						cmn_err(CE_NOTE,
1220 						    "server_ipaddr %s is "
1221 						    "invalid", ip);
1222 					}
1223 					kmem_free(ip, len);
1224 					if (dldebug) {
1225 						printf("server ip is %s\n",
1226 						    inet_ntoa(
1227 						    *(struct in_addr *)
1228 						    server_ip));
1229 					}
1230 				}
1231 				len = doptp->len - len;
1232 			} else {
1233 				str = (char *)doptp->value;
1234 				len = doptp->len;
1235 			}
1236 			server_path_c = kmem_alloc(len + 1, KM_SLEEP);
1237 			bcopy(str, server_path_c, len);
1238 			server_path_c[len] = '\0';
1239 			if (dldebug)
1240 				printf("dhcp:  root path %s\n", server_path_c);
1241 		} else {
1242 			cmn_err(CE_WARN, "dhcp: root server path missing");
1243 		}
1244 	}
1245 
1246 	/* set server_name */
1247 	if (server_name_c == NULL) {
1248 		doptp = pl->vs[VS_NFSMNT_ROOTSRVR_NAME];
1249 		if (doptp != NULL) {
1250 			server_name_c = kmem_alloc(doptp->len + 1, KM_SLEEP);
1251 			bcopy(doptp->value, server_name_c, doptp->len);
1252 			server_name_c[doptp->len] = '\0';
1253 			if (dldebug)
1254 				printf("dhcp: root server name %s\n",
1255 				    server_name_c);
1256 		} else {
1257 			cmn_err(CE_WARN, "dhcp: root server name missing");
1258 		}
1259 	}
1260 
1261 	/* set root server_address */
1262 	if ((*(uint_t *)server_ip) == 0) {
1263 		doptp = pl->vs[VS_NFSMNT_ROOTSRVR_IP];
1264 		if (doptp) {
1265 			bcopy(doptp->value, server_ip, sizeof (server_ip));
1266 			if (dldebug) {
1267 				printf("dhcp:  root server IP address %s\n",
1268 				    inet_ntoa(*(struct in_addr *)server_ip));
1269 			}
1270 		} else {
1271 			if (dldebug)
1272 				cmn_err(CE_CONT,
1273 				    "dhcp: file server ip address missing,"
1274 				    " fallback to dhcp server as file server");
1275 			bcopy(dhcp_server_ip, server_ip, sizeof (server_ip));
1276 		}
1277 	}
1278 
1279 	/* set root file system mount options */
1280 	if (rootopts[0] == 0) {
1281 		doptp = pl->vs[VS_NFSMNT_ROOTOPTS];
1282 		if (doptp != NULL && doptp->len < 255) {
1283 			bcopy(doptp->value, rootopts, doptp->len);
1284 			rootopts[doptp->len] = '\0';
1285 			if (dldebug)
1286 				printf("dhcp:  rootopts %s\n", rootopts);
1287 		} else if (dldebug) {
1288 			printf("dhcp:  no rootopts or too long\n");
1289 			/* not an error */
1290 		}
1291 	}
1292 
1293 	/* now we are done with pl, just free it */
1294 	kmem_free(pl->pkt, pl->len);
1295 	kmem_free(pl, sizeof (PKT_LIST));
1296 	pl = NULL;
1297 }
1298 
1299 static int
1300 cacheinfo(char *name, int namelen,
1301     struct netbuf *server_address, char *rootpath, int pathlen)
1302 {
1303 	static int init_done = 0;
1304 	struct sockaddr_in *sin;
1305 
1306 	if (init_done == 0) {
1307 		cacheinit();
1308 		init_done = 1;
1309 	}
1310 
1311 	/* server_path is a reliable indicator of cache availability */
1312 	if (server_path_c == NULL)
1313 		return (-1);
1314 
1315 	(void) strncpy(rootpath, server_path_c, pathlen);
1316 	if (server_name_c) {
1317 		(void) strncpy(name, server_name_c, namelen);
1318 	} else {
1319 		(void) strncpy(name, "unknown", namelen);
1320 	}
1321 
1322 	sin = (struct sockaddr_in *)server_address->buf;
1323 	sin->sin_family = AF_INET;
1324 	server_address->len = sizeof (struct sockaddr_in);
1325 	bcopy(server_ip, &sin->sin_addr, sizeof (struct in_addr));
1326 	return (0);
1327 }
1328 
1329 /*
1330  *	Set this interface's IP address and netmask, and bring it up.
1331  */
1332 static int
1333 dlifconfig(TIUSER *tiptr, struct in_addr *myIPaddr, struct in_addr *mymask,
1334     struct in_addr *mybraddr, uint_t flags)
1335 {
1336 	int rc;
1337 	struct netbuf sbuf;
1338 	struct sockaddr_in sin;
1339 
1340 	if (dldebug) {
1341 		printf("dlifconfig:  entered\n");
1342 		printf("dlifconfig:  addr %s\n", inet_ntoa(*myIPaddr));
1343 		printf("dlifconfig:  mask %s\n", inet_ntoa(*mymask));
1344 		printf("dlifconfig:  broadcast %s\n", inet_ntoa(*mybraddr));
1345 	}
1346 
1347 	bcopy(myIPaddr, &sin.sin_addr, sizeof (struct in_addr));
1348 	sin.sin_family = AF_INET;
1349 	sbuf.buf = (caddr_t)&sin;
1350 	sbuf.maxlen = sbuf.len = sizeof (sin);
1351 	if (rc = ifioctl(tiptr, SIOCSIFADDR, &sbuf)) {
1352 		nfs_perror(rc,
1353 		    "dlifconfig: couldn't set interface net address: %m\n");
1354 		return (rc);
1355 	}
1356 
1357 	if (mybraddr->s_addr != INADDR_BROADCAST) {
1358 		bcopy(mybraddr, &sin.sin_addr, sizeof (struct in_addr));
1359 		sin.sin_family = AF_INET;
1360 		sbuf.buf = (caddr_t)&sin;
1361 		sbuf.maxlen = sbuf.len = sizeof (sin);
1362 		if (rc = ifioctl(tiptr, SIOCSIFBRDADDR, &sbuf)) {
1363 			nfs_perror(rc,
1364 		    "dlifconfig: couldn't set interface broadcast addr: %m\n");
1365 			return (rc);
1366 		}
1367 	}
1368 
1369 	bcopy(mymask, &sin.sin_addr, sizeof (struct in_addr));
1370 	sin.sin_family = AF_INET;
1371 	sbuf.buf = (caddr_t)&sin;
1372 	sbuf.maxlen = sbuf.len = sizeof (sin);
1373 	if (rc = ifioctl(tiptr, SIOCSIFNETMASK, &sbuf)) {
1374 		nfs_perror(rc,
1375 		    "dlifconfig: couldn't set interface net address: %m\n");
1376 		return (rc);
1377 	}
1378 
1379 	/*
1380 	 * Now turn on the interface.
1381 	 */
1382 	if (rc = setifflags(tiptr, IFF_UP | flags)) {
1383 		nfs_perror(rc,
1384 		    "dlifconfig: couldn't enable network interface: %m\n");
1385 		return (rc);
1386 	}
1387 
1388 	if (dldebug)
1389 		printf("dlifconfig:  returned\n");
1390 	return (0);
1391 }
1392 
1393 static char *
1394 inet_ntoa(struct in_addr in)
1395 {
1396 	static char b[18];
1397 	unsigned char *p;
1398 
1399 	p = (unsigned char *)&in;
1400 	(void) sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
1401 	return (b);
1402 }
1403 
1404 /* We only deal with a.b.c.d decimal format. ip points to 4 byte storage */
1405 static int
1406 inet_aton(char *ipstr, uchar_t *ip)
1407 {
1408 	int i = 0;
1409 	uchar_t val[4] = {0};
1410 	char c = *ipstr;
1411 
1412 	for (;;) {
1413 		if (!isdigit(c))
1414 			return (-1);
1415 		for (;;) {
1416 			if (!isdigit(c))
1417 				break;
1418 			val[i] = val[i] * 10 + (c - '0');
1419 			c = *++ipstr;
1420 		}
1421 		i++;
1422 		if (i == 4)
1423 			break;
1424 		if (c != '.')
1425 			return (-1);
1426 		c = *++ipstr;
1427 	}
1428 	if (c != 0)
1429 		return (-1);
1430 	bcopy(val, ip, 4);
1431 	return (0);
1432 }
1433 
1434 #define	MAX_ADDR_SIZE	128
1435 
1436 /*
1437  * Initialize a netbuf suitable for
1438  * describing an address for the
1439  * transport defined by `tiptr'.
1440  */
1441 static void
1442 init_netbuf(struct netbuf *nbuf)
1443 {
1444 	nbuf->buf = kmem_zalloc(MAX_ADDR_SIZE, KM_SLEEP);
1445 	nbuf->maxlen = MAX_ADDR_SIZE;
1446 	nbuf->len = 0;
1447 }
1448 
1449 static void
1450 free_netbuf(struct netbuf *nbuf)
1451 {
1452 	kmem_free(nbuf->buf, nbuf->maxlen);
1453 	nbuf->buf = NULL;
1454 	nbuf->maxlen = 0;
1455 	nbuf->len = 0;
1456 }
1457 
1458 static int
1459 rtioctl(TIUSER *tiptr, int cmd, struct rtentry *rtentry)
1460 {
1461 	struct strioctl iocb;
1462 	int rc;
1463 	vnode_t *vp;
1464 
1465 	iocb.ic_cmd = cmd;
1466 	iocb.ic_timout = 0;
1467 	iocb.ic_len = sizeof (struct rtentry);
1468 	iocb.ic_dp = (caddr_t)rtentry;
1469 
1470 	vp = tiptr->fp->f_vnode;
1471 	rc = kstr_ioctl(vp, I_STR, (intptr_t)&iocb);
1472 	if (rc)
1473 		nfs_perror(rc, "rtioctl: kstr_ioctl failed: %m\n");
1474 	return (rc);
1475 }
1476 
1477 /*
1478  * Send an ioctl down the stream defined
1479  * by `tiptr'.
1480  *
1481  * We isolate the ifreq dependencies in here. The
1482  * ioctl really ought to take a netbuf and be of
1483  * type TRANSPARENT - one day.
1484  */
1485 static int
1486 ifioctl(TIUSER *tiptr, int cmd, struct netbuf *nbuf)
1487 {
1488 	struct strioctl iocb;
1489 	int rc;
1490 	vnode_t *vp;
1491 	struct ifreq ifr;
1492 
1493 	/*
1494 	 * Now do the one requested.
1495 	 */
1496 	if (nbuf->len)
1497 		ifr.ifr_addr = *(struct sockaddr *)nbuf->buf;
1498 	(void) strncpy((caddr_t)&ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1499 	iocb.ic_cmd = cmd;
1500 	iocb.ic_timout = 0;
1501 	iocb.ic_len = sizeof (ifr);
1502 	iocb.ic_dp = (caddr_t)&ifr;
1503 
1504 	vp = tiptr->fp->f_vnode;
1505 	rc = kstr_ioctl(vp, I_STR, (intptr_t)&iocb);
1506 	if (rc) {
1507 		nfs_perror(rc, "ifioctl: kstr_ioctl failed: %m\n");
1508 		return (rc);
1509 	}
1510 
1511 	/*
1512 	 * Set reply length.
1513 	 */
1514 	if (nbuf->len == 0) {
1515 		/*
1516 		 * GET type.
1517 		 */
1518 		nbuf->len = sizeof (struct sockaddr);
1519 		*(struct sockaddr *)nbuf->buf = ifr.ifr_addr;
1520 	}
1521 
1522 	return (0);
1523 }
1524 
1525 static int
1526 setifflags(TIUSER *tiptr, uint_t value)
1527 {
1528 	struct ifreq ifr;
1529 	int rc;
1530 	struct strioctl iocb;
1531 
1532 	(void) strncpy((caddr_t)&ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1533 	iocb.ic_cmd = SIOCGIFFLAGS;
1534 	iocb.ic_timout = 0;
1535 	iocb.ic_len = sizeof (ifr);
1536 	iocb.ic_dp = (caddr_t)&ifr;
1537 	if (rc = kstr_ioctl(tiptr->fp->f_vnode, I_STR, (intptr_t)&iocb))
1538 		return (rc);
1539 
1540 	ifr.ifr_flags |= value;
1541 	iocb.ic_cmd = SIOCSIFFLAGS;
1542 	return (kstr_ioctl(tiptr->fp->f_vnode, I_STR, (intptr_t)&iocb));
1543 }
1544 
1545 /*
1546  * REVerse Address Resolution Protocol (revarp)
1547  * is used by a diskless client to find out its
1548  * IP address when all it knows is its Ethernet address.
1549  *
1550  * Open the ethernet driver, attach and bind
1551  * (DL_BIND_REQ) it, and then format a broadcast RARP
1552  * message for it to send. We pick up the reply and
1553  * let the caller set the interface address using SIOCSIFADDR.
1554  */
1555 static int
1556 revarp_myaddr(TIUSER *tiptr)
1557 {
1558 	int			rc;
1559 	dl_info_ack_t		info;
1560 	struct sockaddr_in	sin;
1561 	struct netbuf		sbuf;
1562 	ldi_handle_t		lh;
1563 	ldi_ident_t		li;
1564 	struct netbuf		myaddr = {0, 0, NULL};
1565 
1566 	if (dldebug)
1567 		printf("revarp_myaddr: entered\n");
1568 
1569 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
1570 		nfs_perror(rc,
1571 		    "revarp_myaddr: ldi_ident_from_mod failed: %m\n");
1572 		return (rc);
1573 	}
1574 
1575 	rc = ldi_open_by_name(ndev_path, FREAD|FWRITE, CRED(), &lh, li);
1576 	ldi_ident_release(li);
1577 	if (rc) {
1578 		nfs_perror(rc,
1579 		    "revarp_myaddr: ldi_open_by_name failed: %m\n");
1580 		return (rc);
1581 	}
1582 
1583 	if (rc = dl_attach(lh, ifunit, NULL)) {
1584 		nfs_perror(rc, "revarp_myaddr: dl_attach failed: %m\n");
1585 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
1586 		return (rc);
1587 	}
1588 
1589 	if (rc = dl_bind(lh, ETHERTYPE_REVARP, NULL)) {
1590 		nfs_perror(rc, "revarp_myaddr: dl_bind failed: %m\n");
1591 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
1592 		return (rc);
1593 	}
1594 
1595 	if (rc = dl_info(lh, &info, NULL, NULL, NULL)) {
1596 		nfs_perror(rc, "revarp_myaddr: dl_info failed: %m\n");
1597 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
1598 		return (rc);
1599 	}
1600 
1601 	/* Initialize myaddr */
1602 	myaddr.maxlen = info.dl_addr_length;
1603 	myaddr.buf = kmem_alloc(myaddr.maxlen, KM_SLEEP);
1604 
1605 	revarp_start(lh, &myaddr);
1606 
1607 	bcopy(myaddr.buf, &sin.sin_addr, myaddr.len);
1608 	sin.sin_family = AF_INET;
1609 
1610 	sbuf.buf = (caddr_t)&sin;
1611 	sbuf.maxlen = sbuf.len = sizeof (sin);
1612 	if (rc = ifioctl(tiptr, SIOCSIFADDR, &sbuf)) {
1613 		nfs_perror(rc,
1614 		    "revarp_myaddr: couldn't set interface net address: %m\n");
1615 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
1616 		kmem_free(myaddr.buf, myaddr.maxlen);
1617 		return (rc);
1618 	}
1619 
1620 	/* Now turn on the interface */
1621 	if (rc = setifflags(tiptr, IFF_UP)) {
1622 		nfs_perror(rc,
1623 		    "revarp_myaddr: couldn't enable network interface: %m\n");
1624 	}
1625 
1626 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
1627 	kmem_free(myaddr.buf, myaddr.maxlen);
1628 	return (rc);
1629 }
1630 
1631 static void
1632 revarp_start(ldi_handle_t lh, struct netbuf *myaddr)
1633 {
1634 	struct ether_arp *ea;
1635 	int rc;
1636 	dl_unitdata_req_t *dl_udata;
1637 	mblk_t *bp;
1638 	mblk_t *mp;
1639 	struct dladdr *dlsap;
1640 	static int done = 0;
1641 	size_t addrlen = ETHERADDRL;
1642 
1643 	if (dl_phys_addr(lh, (uchar_t *)&myether, &addrlen, NULL) != 0 ||
1644 	    addrlen != ETHERADDRL) {
1645 		/* Fallback using per-node address */
1646 		(void) localetheraddr((struct ether_addr *)NULL, &myether);
1647 		cmn_err(CE_CONT, "?DLPI failed to get Ethernet address. Using "
1648 		    "system wide Ethernet address %s\n",
1649 		    ether_sprintf(&myether));
1650 	}
1651 
1652 getreply:
1653 	if (myaddr->len != 0) {
1654 		cmn_err(CE_CONT, "?Found my IP address: %x (%d.%d.%d.%d)\n",
1655 		    *(int *)myaddr->buf,
1656 		    (uchar_t)myaddr->buf[0], (uchar_t)myaddr->buf[1],
1657 		    (uchar_t)myaddr->buf[2], (uchar_t)myaddr->buf[3]);
1658 		return;
1659 	}
1660 
1661 	if (done++ == 0)
1662 		cmn_err(CE_CONT, "?Requesting Internet address for %s\n",
1663 		    ether_sprintf(&myether));
1664 
1665 	/*
1666 	 * Send another RARP request.
1667 	 */
1668 	if ((mp = allocb(sizeof (dl_unitdata_req_t) + sizeof (*dlsap),
1669 	    BPRI_HI)) == NULL) {
1670 		cmn_err(CE_WARN, "revarp_myaddr: allocb no memory");
1671 		return;
1672 	}
1673 	if ((bp = allocb(sizeof (struct ether_arp), BPRI_HI)) == NULL) {
1674 		cmn_err(CE_WARN, "revarp_myaddr: allocb no memory");
1675 		return;
1676 	}
1677 
1678 	/*
1679 	 * Format the transmit request part.
1680 	 */
1681 	mp->b_datap->db_type = M_PROTO;
1682 	dl_udata = (dl_unitdata_req_t *)mp->b_wptr;
1683 	mp->b_wptr += sizeof (dl_unitdata_req_t) + sizeof (*dlsap);
1684 	dl_udata->dl_primitive = DL_UNITDATA_REQ;
1685 	dl_udata->dl_dest_addr_length = sizeof (*dlsap);
1686 	dl_udata->dl_dest_addr_offset = sizeof (*dl_udata);
1687 	dl_udata->dl_priority.dl_min = 0;
1688 	dl_udata->dl_priority.dl_max = 0;
1689 
1690 	dlsap = (struct dladdr *)(mp->b_rptr + sizeof (*dl_udata));
1691 	bcopy(&etherbroadcastaddr, &dlsap->dl_phys,
1692 	    sizeof (etherbroadcastaddr));
1693 	dlsap->dl_sap = ETHERTYPE_REVARP;
1694 
1695 	/*
1696 	 * Format the actual REVARP request.
1697 	 */
1698 	bzero(bp->b_wptr, sizeof (struct ether_arp));
1699 	ea = (struct ether_arp *)bp->b_wptr;
1700 	bp->b_wptr += sizeof (struct ether_arp);
1701 	ea->arp_hrd = htons(ARPHRD_ETHER);
1702 	ea->arp_pro = htons(ETHERTYPE_IP);
1703 	ea->arp_hln = sizeof (ea->arp_sha);	/* hardware address length */
1704 	ea->arp_pln = sizeof (ea->arp_spa);	/* protocol address length */
1705 	ea->arp_op = htons(REVARP_REQUEST);
1706 	ether_copy(&myether, &ea->arp_sha);
1707 	ether_copy(&myether, &ea->arp_tha);
1708 
1709 	mp->b_cont = bp;
1710 
1711 	if ((rc = ldi_putmsg(lh, mp)) != 0) {
1712 		nfs_perror(rc, "revarp_start: ldi_putmsg failed: %m\n");
1713 		return;
1714 	}
1715 	revarpinput(lh, myaddr);
1716 
1717 	goto getreply;
1718 }
1719 
1720 /*
1721  * Client side Reverse-ARP input
1722  * Server side is handled by user level server
1723  */
1724 static void
1725 revarpinput(ldi_handle_t lh, struct netbuf *myaddr)
1726 {
1727 	struct ether_arp *ea;
1728 	mblk_t *bp;
1729 	mblk_t *mp;
1730 	int rc;
1731 	timestruc_t tv, give_up, now;
1732 
1733 	/*
1734 	 * Choose the time at which we will give up, and resend our
1735 	 * request.
1736 	 */
1737 	gethrestime(&give_up);
1738 	give_up.tv_sec += REVARP_TIMEO;
1739 wait:
1740 	/*
1741 	 * Compute new timeout value.
1742 	 */
1743 	tv = give_up;
1744 	gethrestime(&now);
1745 	timespecsub(&tv, &now);
1746 	/*
1747 	 * If we don't have at least one full second remaining, give up.
1748 	 * This means we might wait only just over 4.0 seconds, but that's
1749 	 * okay.
1750 	 */
1751 	if (tv.tv_sec <= 0)
1752 		return;
1753 	rc = ldi_getmsg(lh, &mp, &tv);
1754 	if (rc == ETIME) {
1755 		goto out;
1756 	} else if (rc != 0) {
1757 		nfs_perror(rc, "revarpinput: ldi_getmsg failed: %m\n");
1758 		return;
1759 	}
1760 
1761 	if (mp->b_cont == NULL) {
1762 		printf("revarpinput: b_cont == NULL\n");
1763 		goto out;
1764 	}
1765 
1766 	if (mp->b_datap->db_type != M_PROTO) {
1767 		printf("revarpinput: bad header type %d\n",
1768 		    mp->b_datap->db_type);
1769 		goto out;
1770 	}
1771 
1772 	bp = mp->b_cont;
1773 
1774 	if (bp->b_wptr - bp->b_rptr < sizeof (*ea)) {
1775 		printf("revarpinput: bad data len %d, expect %d\n",
1776 		    (int)(bp->b_wptr - bp->b_rptr),  (int)sizeof (*ea));
1777 		goto out;
1778 	}
1779 
1780 	ea = (struct ether_arp *)bp->b_rptr;
1781 
1782 	if ((ushort_t)ntohs(ea->arp_pro) != ETHERTYPE_IP) {
1783 		/* We could have received another broadcast arp packet. */
1784 		if (dldebug)
1785 			printf("revarpinput: bad type %x\n",
1786 			    (ushort_t)ntohs(ea->arp_pro));
1787 		freemsg(mp);
1788 		goto wait;
1789 	}
1790 	if ((ushort_t)ntohs(ea->arp_op) != REVARP_REPLY) {
1791 		/* We could have received a broadcast arp request. */
1792 		if (dldebug)
1793 			printf("revarpinput: bad op %x\n",
1794 			    (ushort_t)ntohs(ea->arp_op));
1795 		freemsg(mp);
1796 		goto wait;
1797 	}
1798 
1799 	if (!ether_cmp(&ea->arp_tha, &myether)) {
1800 		bcopy(&ea->arp_tpa, myaddr->buf, sizeof (ea->arp_tpa));
1801 		myaddr->len = sizeof (ea->arp_tpa);
1802 	} else {
1803 		/* We could have gotten a broadcast arp response. */
1804 		if (dldebug)
1805 			printf("revarpinput: got reply, but not my address\n");
1806 		freemsg(mp);
1807 		goto wait;
1808 	}
1809 out:
1810 	freemsg(mp);
1811 }
1812 
1813 /*
1814  * From rpcsvc/mountxdr.c in SunOS. We can't
1815  * put this into the rpc directory because
1816  * it calls xdr_fhandle() which is in a
1817  * loadable module.
1818  */
1819 static bool_t
1820 myxdr_fhstatus(XDR *xdrs, struct fhstatus *fhsp)
1821 {
1822 
1823 	if (!xdr_int(xdrs, &fhsp->fhs_status))
1824 		return (FALSE);
1825 	if (fhsp->fhs_status == 0) {
1826 		if (!myxdr_fhandle(xdrs, &fhsp->fhs_fh))
1827 			return (FALSE);
1828 	}
1829 	return (TRUE);
1830 }
1831 
1832 /*
1833  * From nfs_xdr.c.
1834  *
1835  * File access handle
1836  * The fhandle struct is treated a opaque data on the wire
1837  */
1838 static bool_t
1839 myxdr_fhandle(XDR *xdrs, fhandle_t *fh)
1840 {
1841 	return (xdr_opaque(xdrs, (caddr_t)fh, NFS_FHSIZE));
1842 }
1843 
1844 static bool_t
1845 myxdr_mountres3(XDR *xdrs, struct mountres3 *objp)
1846 {
1847 	if (!myxdr_mountstat3(xdrs, &objp->fhs_status))
1848 		return (FALSE);
1849 	switch (objp->fhs_status) {
1850 	case MNT_OK:
1851 		if (!myxdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
1852 			return (FALSE);
1853 		break;
1854 	default:
1855 		break;
1856 	}
1857 	return (TRUE);
1858 }
1859 
1860 static bool_t
1861 myxdr_mountstat3(XDR *xdrs, enum mountstat3 *objp)
1862 {
1863 	return (xdr_enum(xdrs, (enum_t *)objp));
1864 }
1865 
1866 static bool_t
1867 myxdr_mountres3_ok(XDR *xdrs, struct mountres3_ok *objp)
1868 {
1869 	if (!myxdr_fhandle3(xdrs, &objp->fhandle))
1870 		return (FALSE);
1871 	if (!xdr_array(xdrs, (char **)&objp->auth_flavors.auth_flavors_val,
1872 	    (uint_t *)&objp->auth_flavors.auth_flavors_len, ~0,
1873 	    sizeof (int), (xdrproc_t)xdr_int))
1874 		return (FALSE);
1875 	return (TRUE);
1876 }
1877 
1878 static bool_t
1879 myxdr_fhandle3(XDR *xdrs, struct fhandle3 *objp)
1880 {
1881 	return (xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
1882 	    (uint_t *)&objp->fhandle3_len, FHSIZE3));
1883 }
1884 
1885 /*
1886  * From SunOS pmap_clnt.c
1887  *
1888  * Port mapper routines:
1889  *	pmap_kgetport() - get port number.
1890  *	pmap_rmt_call()  - indirect call via port mapper.
1891  *
1892  */
1893 static enum clnt_stat
1894 pmap_kgetport(struct knetconfig *knconf, struct netbuf *call_addr,
1895     rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
1896 {
1897 	ushort_t port;
1898 	int tries;
1899 	enum clnt_stat stat;
1900 	struct pmap	pmap_parms;
1901 	RPCB		rpcb_parms;
1902 	char		*ua = NULL;
1903 
1904 	port = 0;
1905 
1906 	((struct sockaddr_in *)call_addr->buf)->sin_port = htons(PMAPPORT);
1907 
1908 	pmap_parms.pm_prog = prog;
1909 	pmap_parms.pm_vers = vers;
1910 	pmap_parms.pm_prot = prot;
1911 	pmap_parms.pm_port = 0;
1912 	for (tries = 0; tries < 5; tries++) {
1913 		stat = mycallrpc(knconf, call_addr,
1914 		    PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
1915 		    myxdr_pmap, (char *)&pmap_parms,
1916 		    xdr_u_short, (char *)&port,
1917 		    DEFAULT_TIMEO, DEFAULT_RETRIES);
1918 
1919 		if (stat != RPC_TIMEDOUT)
1920 			break;
1921 		cmn_err(CE_WARN,
1922 		    "pmap_kgetport: Portmapper not responding; still trying");
1923 	}
1924 
1925 	if (stat == RPC_PROGUNAVAIL) {
1926 		cmn_err(CE_WARN,
1927 		    "pmap_kgetport: Portmapper failed - trying rpcbind");
1928 
1929 		rpcb_parms.r_prog = prog;
1930 		rpcb_parms.r_vers = vers;
1931 		rpcb_parms.r_netid = knconf->knc_proto;
1932 		rpcb_parms.r_addr = rpcb_parms.r_owner = "";
1933 
1934 		for (tries = 0; tries < 5; tries++) {
1935 			stat = mycallrpc(knconf, call_addr,
1936 			    RPCBPROG, RPCBVERS, RPCBPROC_GETADDR,
1937 			    xdr_rpcb, (char *)&rpcb_parms,
1938 			    xdr_wrapstring, (char *)&ua,
1939 			    DEFAULT_TIMEO, DEFAULT_RETRIES);
1940 
1941 			if (stat != RPC_TIMEDOUT)
1942 				break;
1943 			cmn_err(CE_WARN,
1944 			"pmap_kgetport: rpcbind not responding; still trying");
1945 		}
1946 
1947 		if (stat == RPC_SUCCESS) {
1948 			if ((ua != NULL) && (ua[0] != NULL)) {
1949 				port = rpc_uaddr2port(AF_INET, ua);
1950 			} else {
1951 				/* Address unknown */
1952 				stat = RPC_PROGUNAVAIL;
1953 			}
1954 		}
1955 	}
1956 
1957 	if (stat == RPC_SUCCESS)
1958 		((struct sockaddr_in *)call_addr->buf)->sin_port = ntohs(port);
1959 
1960 	return (stat);
1961 }
1962 
1963 /*
1964  * pmapper remote-call-service interface.
1965  * This routine is used to call the pmapper remote call service
1966  * which will look up a service program in the port maps, and then
1967  * remotely call that routine with the given parameters.  This allows
1968  * programs to do a lookup and call in one step. In addition to the call_addr,
1969  * the caller provides a boolean hint about the destination address (TRUE if
1970  * address is a broadcast address, FALSE otherwise).
1971  *
1972  * On return, `call addr' contains the port number for the
1973  * service requested, and `resp_addr' contains its IP address.
1974  */
1975 static enum clnt_stat
1976 pmap_rmt_call(struct knetconfig *knconf, struct netbuf *call_addr,
1977     bool_t bcast, rpcprog_t progn, rpcvers_t versn, rpcproc_t procn,
1978     xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
1979     struct timeval tout, struct netbuf *resp_addr)
1980 {
1981 	CLIENT *cl;
1982 	enum clnt_stat stat;
1983 	rpcport_t port;
1984 	int rc;
1985 	struct rmtcallargs	pmap_args;
1986 	struct rmtcallres	pmap_res;
1987 	struct rpcb_rmtcallargs	rpcb_args;
1988 	struct rpcb_rmtcallres	rpcb_res;
1989 	char			ua[100];	/* XXX */
1990 
1991 	((struct sockaddr_in *)call_addr->buf)->sin_port = htons(PMAPPORT);
1992 
1993 	rc = clnt_tli_kcreate(knconf, call_addr, PMAPPROG, PMAPVERS,
1994 	    0, PMAP_RETRIES, CRED(), &cl);
1995 	if (rc != 0) {
1996 		nfs_perror(rc,
1997 		    "pmap_rmt_call: clnt_tli_kcreate failed: %m\n");
1998 		return (RPC_SYSTEMERROR);	/* XXX */
1999 	}
2000 	if (cl == (CLIENT *)NULL) {
2001 		panic("pmap_rmt_call: clnt_tli_kcreate failed");
2002 		/* NOTREACHED */
2003 	}
2004 
2005 	(void) CLNT_CONTROL(cl, CLSET_BCAST, (char *)&bcast);
2006 
2007 	pmap_args.prog = progn;
2008 	pmap_args.vers = versn;
2009 	pmap_args.proc = procn;
2010 	pmap_args.args_ptr = argsp;
2011 	pmap_args.xdr_args = xdrargs;
2012 	pmap_res.port_ptr = &port;
2013 	pmap_res.results_ptr = resp;
2014 	pmap_res.xdr_results = xdrres;
2015 	stat = clnt_clts_kcallit_addr(cl, PMAPPROC_CALLIT,
2016 	    myxdr_rmtcall_args, (caddr_t)&pmap_args,
2017 	    myxdr_rmtcallres, (caddr_t)&pmap_res,
2018 	    tout, resp_addr);
2019 
2020 	if (stat == RPC_SUCCESS) {
2021 		((struct sockaddr_in *)resp_addr->buf)->sin_port =
2022 		    htons((ushort_t)port);
2023 	}
2024 	CLNT_DESTROY(cl);
2025 
2026 	if (stat != RPC_PROGUNAVAIL)
2027 		return (stat);
2028 
2029 	cmn_err(CE_WARN, "pmap_rmt_call: Portmapper failed - trying rpcbind");
2030 
2031 	rc = clnt_tli_kcreate(knconf, call_addr, RPCBPROG, RPCBVERS,
2032 	    0, PMAP_RETRIES, CRED(), &cl);
2033 	if (rc != 0) {
2034 		nfs_perror(rc, "pmap_rmt_call: clnt_tli_kcreate failed: %m\n");
2035 		return (RPC_SYSTEMERROR);	/* XXX */
2036 	}
2037 
2038 	if (cl == NULL) {
2039 		panic("pmap_rmt_call: clnt_tli_kcreate failed");
2040 		/* NOTREACHED */
2041 	}
2042 
2043 	rpcb_args.prog = progn;
2044 	rpcb_args.vers = versn;
2045 	rpcb_args.proc = procn;
2046 	rpcb_args.args_ptr = argsp;
2047 	rpcb_args.xdr_args = xdrargs;
2048 	rpcb_res.addr_ptr = ua;
2049 	rpcb_res.results_ptr = resp;
2050 	rpcb_res.xdr_results = xdrres;
2051 	stat = clnt_clts_kcallit_addr(cl, PMAPPROC_CALLIT,
2052 	    xdr_rpcb_rmtcallargs, (caddr_t)&rpcb_args,
2053 	    xdr_rpcb_rmtcallres, (caddr_t)&rpcb_res,
2054 	    tout, resp_addr);
2055 
2056 	if (stat == RPC_SUCCESS)
2057 		((struct sockaddr_in *)resp_addr->buf)->sin_port =
2058 		    rpc_uaddr2port(AF_INET, ua);
2059 	CLNT_DESTROY(cl);
2060 
2061 	return (stat);
2062 }
2063 
2064 /*
2065  * XDR remote call arguments
2066  * written for XDR_ENCODE direction only
2067  */
2068 static bool_t
2069 myxdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
2070 {
2071 	uint_t lenposition;
2072 	uint_t argposition;
2073 	uint_t position;
2074 
2075 	if (xdr_rpcprog(xdrs, &(cap->prog)) &&
2076 	    xdr_rpcvers(xdrs, &(cap->vers)) &&
2077 	    xdr_rpcproc(xdrs, &(cap->proc))) {
2078 		lenposition = XDR_GETPOS(xdrs);
2079 		if (!xdr_u_int(xdrs, &cap->arglen))
2080 			return (FALSE);
2081 		argposition = XDR_GETPOS(xdrs);
2082 		if (!(*(cap->xdr_args))(xdrs, cap->args_ptr))
2083 			return (FALSE);
2084 		position = XDR_GETPOS(xdrs);
2085 		cap->arglen = (uint_t)position - (uint_t)argposition;
2086 		XDR_SETPOS(xdrs, lenposition);
2087 		if (!xdr_u_int(xdrs, &cap->arglen))
2088 			return (FALSE);
2089 		XDR_SETPOS(xdrs, position);
2090 		return (TRUE);
2091 	}
2092 	return (FALSE);
2093 }
2094 
2095 /*
2096  * XDR remote call results
2097  * written for XDR_DECODE direction only
2098  */
2099 static bool_t
2100 myxdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
2101 {
2102 	caddr_t port_ptr;
2103 
2104 	port_ptr = (caddr_t)crp->port_ptr;
2105 	if (xdr_reference(xdrs, &port_ptr, sizeof (uint_t), xdr_u_int) &&
2106 	    xdr_u_int(xdrs, &crp->resultslen)) {
2107 		crp->port_ptr = (rpcport_t *)port_ptr;
2108 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
2109 	}
2110 	return (FALSE);
2111 }
2112 
2113 static bool_t
2114 myxdr_pmap(XDR *xdrs, struct pmap *regs)
2115 {
2116 	if (xdr_rpcprog(xdrs, &regs->pm_prog) &&
2117 	    xdr_rpcvers(xdrs, &regs->pm_vers) &&
2118 	    xdr_rpcprot(xdrs, &regs->pm_prot))
2119 		return (xdr_rpcport(xdrs, &regs->pm_port));
2120 
2121 	return (FALSE);
2122 }
2123 
2124 /*
2125  * From SunOS callrpc.c
2126  */
2127 static enum clnt_stat
2128 mycallrpc(struct knetconfig *knconf, struct netbuf *call_addr,
2129     rpcprog_t prognum, rpcvers_t versnum, rpcproc_t procnum,
2130     xdrproc_t inproc, char *in, xdrproc_t outproc, char *out,
2131     int timeo, int retries)
2132 {
2133 	CLIENT *cl;
2134 	struct timeval tv;
2135 	enum clnt_stat cl_stat;
2136 	int rc;
2137 
2138 	rc = clnt_tli_kcreate(knconf, call_addr, prognum, versnum,
2139 	    0, retries, CRED(), &cl);
2140 	if (rc) {
2141 		nfs_perror(rc, "mycallrpc: clnt_tli_kcreate failed: %m\n");
2142 		return (RPC_SYSTEMERROR);	/* XXX */
2143 	}
2144 	tv.tv_sec = timeo;
2145 	tv.tv_usec = 0;
2146 	cl_stat = CLNT_CALL(cl, procnum, inproc, in, outproc, out, tv);
2147 	AUTH_DESTROY(cl->cl_auth);
2148 	CLNT_DESTROY(cl);
2149 	return (cl_stat);
2150 }
2151 
2152 /*
2153  * Configure the 'default' interface based on existing boot properties.
2154  */
2155 static int
2156 bp_netconfig(void)
2157 {
2158 	char *str;
2159 	struct in_addr my_ip, my_netmask, my_router, my_broadcast;
2160 	struct sockaddr_in *sin;
2161 	TIUSER *tiptr;
2162 	int rc;
2163 	struct rtentry rtentry;
2164 
2165 	my_ip.s_addr = my_netmask.s_addr = my_router.s_addr = 0;
2166 
2167 	/*
2168 	 * No way of getting this right now.  Collude with dlifconfig()
2169 	 * to let the protocol stack choose.
2170 	 */
2171 	my_broadcast.s_addr = INADDR_BROADCAST;
2172 
2173 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2174 	    DDI_PROP_DONTPASS, BP_HOST_IP, &str) == DDI_SUCCESS) {
2175 		if (inet_aton(str, (uchar_t *)&my_ip) != 0)
2176 			cmn_err(CE_NOTE, "host-ip %s is invalid\n",
2177 			    str);
2178 		ddi_prop_free(str);
2179 		if (dldebug)
2180 			printf("host ip is %s\n",
2181 			    inet_ntoa(my_ip));
2182 	}
2183 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2184 	    DDI_PROP_DONTPASS, BP_SUBNET_MASK, &str) == DDI_SUCCESS) {
2185 		if (inet_aton(str, (uchar_t *)&my_netmask) != 0)
2186 			cmn_err(CE_NOTE, "subnet-mask %s is invalid\n",
2187 			    str);
2188 		ddi_prop_free(str);
2189 		if (dldebug)
2190 			printf("subnet mask is %s\n",
2191 			    inet_ntoa(my_netmask));
2192 	}
2193 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2194 	    DDI_PROP_DONTPASS, BP_ROUTER_IP, &str) == DDI_SUCCESS) {
2195 		if (inet_aton(str, (uchar_t *)&my_router) != 0)
2196 			cmn_err(CE_NOTE, "router-ip %s is invalid\n",
2197 			    str);
2198 		ddi_prop_free(str);
2199 		if (dldebug)
2200 			printf("router ip is %s\n",
2201 			    inet_ntoa(my_router));
2202 	}
2203 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2204 	    DDI_PROP_DONTPASS, BP_SERVER_PATH, &server_path_c);
2205 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2206 	    DDI_PROP_DONTPASS, BP_SERVER_NAME, &server_name_c);
2207 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2208 	    DDI_PROP_DONTPASS, BP_SERVER_ROOTOPTS, &str) == DDI_SUCCESS) {
2209 		(void) strlcpy(rootopts, str, sizeof (rootopts));
2210 		ddi_prop_free(str);
2211 	}
2212 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2213 	    DDI_PROP_DONTPASS, BP_SERVER_IP, &str) == DDI_SUCCESS) {
2214 		if (inet_aton(str, server_ip) != 0)
2215 			cmn_err(CE_NOTE, "server-ip %s is invalid\n",
2216 			    str);
2217 		ddi_prop_free(str);
2218 		if (dldebug)
2219 			printf("server ip is %s\n",
2220 			    inet_ntoa(*(struct in_addr *)server_ip));
2221 	}
2222 
2223 	/*
2224 	 * We need all of these to configure based on properties.
2225 	 */
2226 	if ((my_ip.s_addr == 0) ||
2227 	    (my_netmask.s_addr == 0) ||
2228 	    (server_path_c == NULL) ||
2229 	    (server_name_c == NULL) ||
2230 	    (*(uint_t *)server_ip == 0))
2231 		return (-1);
2232 
2233 	cmn_err(CE_CONT, "?IP address: %s\n", inet_ntoa(my_ip));
2234 	cmn_err(CE_CONT, "?IP netmask: %s\n", inet_ntoa(my_netmask));
2235 	if (my_router.s_addr != 0)
2236 		cmn_err(CE_CONT, "?IP router: %s\n", inet_ntoa(my_router));
2237 	cmn_err(CE_CONT, "?NFS server: %s (%s)\n", server_name_c,
2238 	    inet_ntoa(*(struct in_addr *)server_ip));
2239 	cmn_err(CE_CONT, "?NFS path: %s\n", server_path_c);
2240 
2241 	/*
2242 	 * Configure the interface.
2243 	 */
2244 	if ((rc = t_kopen((file_t *)NULL, dl_udp_netconf.knc_rdev,
2245 	    FREAD|FWRITE, &tiptr, CRED())) != 0) {
2246 		nfs_perror(rc, "bp_netconfig: t_kopen udp failed: %m.\n");
2247 		return (rc);
2248 	}
2249 
2250 	if ((rc = dlifconfig(tiptr, &my_ip, &my_netmask, &my_broadcast,
2251 	    0)) < 0) {
2252 		nfs_perror(rc, "bp_netconfig: dlifconfig failed: %m.\n");
2253 		(void) t_kclose(tiptr, 0);
2254 		return (rc);
2255 	}
2256 
2257 	if (my_router.s_addr != 0) {
2258 		/*
2259 		 * Add a default route.
2260 		 */
2261 		sin = (struct sockaddr_in *)&rtentry.rt_dst;
2262 		bzero(sin, sizeof (*sin));
2263 		sin->sin_family = AF_INET;
2264 
2265 		sin = (struct sockaddr_in *)&rtentry.rt_gateway;
2266 		bzero(sin, sizeof (*sin));
2267 		sin->sin_family = AF_INET;
2268 		sin->sin_addr = my_router;
2269 
2270 		rtentry.rt_flags = RTF_GATEWAY | RTF_UP;
2271 
2272 		if ((rc = rtioctl(tiptr, SIOCADDRT, &rtentry)) != 0) {
2273 			nfs_perror(rc,
2274 			    "bp_netconfig: couldn't add route: %m.\n");
2275 			(void) t_kclose(tiptr, 0);
2276 			return (rc);
2277 		}
2278 	}
2279 
2280 	(void) t_kclose(tiptr, 0);
2281 
2282 	return (0);
2283 }
2284 
2285 /*
2286  * The network device we will use to boot from is plumbed. Extract the details
2287  * from rootfs.
2288  */
2289 static void
2290 init_config(void)
2291 {
2292 	(void) strlcpy(ndev_path, rootfs.bo_devname, sizeof (ndev_path));
2293 	(void) strlcpy(ifname, rootfs.bo_ifname, sizeof (ifname));
2294 	ifunit = rootfs.bo_ppa;
2295 
2296 	/*
2297 	 * Assumes only one linkage array element.
2298 	 */
2299 	dl_udp_netconf.knc_rdev =
2300 	    makedevice(clone_major, ddi_name_to_major("udp"));
2301 	dl_tcp_netconf.knc_rdev =
2302 	    makedevice(clone_major, ddi_name_to_major("tcp"));
2303 
2304 	/*
2305 	 * Now we bringup the interface.
2306 	 * Try cached dhcp response first. If it fails, do rarp.
2307 	 */
2308 	if ((bp_netconfig() != 0) &&
2309 	    (dhcpinit() != 0) &&
2310 	    (whoami() != 0))
2311 		cmn_err(CE_WARN,
2312 		    "%s: no response from interface", ifname);
2313 	else if (dldebug)
2314 		printf("init_config: ifname %s is up\n", ifname);
2315 }
2316 
2317 /*
2318  * These options are duplicated in cmd/fs.d/nfs/mount/mount.c
2319  * Changes must be made to both lists.
2320  */
2321 static char *optlist[] = {
2322 #define	OPT_RO		0
2323 	MNTOPT_RO,
2324 #define	OPT_RW		1
2325 	MNTOPT_RW,
2326 #define	OPT_QUOTA	2
2327 	MNTOPT_QUOTA,
2328 #define	OPT_NOQUOTA	3
2329 	MNTOPT_NOQUOTA,
2330 #define	OPT_SOFT	4
2331 	MNTOPT_SOFT,
2332 #define	OPT_HARD	5
2333 	MNTOPT_HARD,
2334 #define	OPT_SUID	6
2335 	MNTOPT_SUID,
2336 #define	OPT_NOSUID	7
2337 	MNTOPT_NOSUID,
2338 #define	OPT_GRPID	8
2339 	MNTOPT_GRPID,
2340 #define	OPT_REMOUNT	9
2341 	MNTOPT_REMOUNT,
2342 #define	OPT_NOSUB	10
2343 	MNTOPT_NOSUB,
2344 #define	OPT_INTR	11
2345 	MNTOPT_INTR,
2346 #define	OPT_NOINTR	12
2347 	MNTOPT_NOINTR,
2348 #define	OPT_PORT	13
2349 	MNTOPT_PORT,
2350 #define	OPT_SECURE	14
2351 	MNTOPT_SECURE,
2352 #define	OPT_RSIZE	15
2353 	MNTOPT_RSIZE,
2354 #define	OPT_WSIZE	16
2355 	MNTOPT_WSIZE,
2356 #define	OPT_TIMEO	17
2357 	MNTOPT_TIMEO,
2358 #define	OPT_RETRANS	18
2359 	MNTOPT_RETRANS,
2360 #define	OPT_ACTIMEO	19
2361 	MNTOPT_ACTIMEO,
2362 #define	OPT_ACREGMIN	20
2363 	MNTOPT_ACREGMIN,
2364 #define	OPT_ACREGMAX	21
2365 	MNTOPT_ACREGMAX,
2366 #define	OPT_ACDIRMIN	22
2367 	MNTOPT_ACDIRMIN,
2368 #define	OPT_ACDIRMAX	23
2369 	MNTOPT_ACDIRMAX,
2370 #define	OPT_BG		24
2371 	MNTOPT_BG,
2372 #define	OPT_FG		25
2373 	MNTOPT_FG,
2374 #define	OPT_RETRY	26
2375 	MNTOPT_RETRY,
2376 #define	OPT_NOAC	27
2377 	MNTOPT_NOAC,
2378 #define	OPT_NOCTO	28
2379 	MNTOPT_NOCTO,
2380 #define	OPT_LLOCK	29
2381 	MNTOPT_LLOCK,
2382 #define	OPT_POSIX	30
2383 	MNTOPT_POSIX,
2384 #define	OPT_VERS	31
2385 	MNTOPT_VERS,
2386 #define	OPT_PROTO	32
2387 	MNTOPT_PROTO,
2388 #define	OPT_SEMISOFT	33
2389 	MNTOPT_SEMISOFT,
2390 #define	OPT_NOPRINT	34
2391 	MNTOPT_NOPRINT,
2392 #define	OPT_SEC		35
2393 	MNTOPT_SEC,
2394 #define	OPT_LARGEFILES	36
2395 	MNTOPT_LARGEFILES,
2396 #define	OPT_NOLARGEFILES	37
2397 	MNTOPT_NOLARGEFILES,
2398 #define	OPT_PUBLIC	38
2399 	MNTOPT_PUBLIC,
2400 #define	OPT_DIRECTIO	39
2401 	MNTOPT_FORCEDIRECTIO,
2402 #define	OPT_NODIRECTIO	40
2403 	MNTOPT_NOFORCEDIRECTIO,
2404 #define	OPT_XATTR	41
2405 	MNTOPT_XATTR,
2406 #define	OPT_NOXATTR	42
2407 	MNTOPT_NOXATTR,
2408 #define	OPT_DEVICES	43
2409 	MNTOPT_DEVICES,
2410 #define	OPT_NODEVICES	44
2411 	MNTOPT_NODEVICES,
2412 #define	OPT_SETUID	45
2413 	MNTOPT_SETUID,
2414 #define	OPT_NOSETUID	46
2415 	MNTOPT_NOSETUID,
2416 #define	OPT_EXEC	47
2417 	MNTOPT_EXEC,
2418 #define	OPT_NOEXEC	48
2419 	MNTOPT_NOEXEC,
2420 	NULL
2421 };
2422 
2423 static int
2424 isdigit(int ch)
2425 {
2426 	return (ch >= '0' && ch <= '9');
2427 }
2428 
2429 #define	isspace(c)	((c) == ' ' || (c) == '\t' || (c) == '\n')
2430 #define	bad(val)	(val == NULL || !isdigit(*val))
2431 
2432 static int
2433 atoi(const char *p)
2434 {
2435 	int n;
2436 	int c, neg = 0;
2437 
2438 	if (!isdigit(c = *p)) {
2439 		while (isspace(c))
2440 			c = *++p;
2441 		switch (c) {
2442 		case '-':
2443 			neg++;
2444 			/* FALLTHROUGH */
2445 		case '+':
2446 			c = *++p;
2447 		}
2448 		if (!isdigit(c))
2449 			return (0);
2450 	}
2451 	for (n = '0' - c; isdigit(c = *++p); ) {
2452 		n *= 10; /* two steps to avoid unnecessary overflow */
2453 		n += '0' - c; /* accum neg to avoid surprises at MAX */
2454 	}
2455 	return (neg ? n : -n);
2456 }
2457 
2458 /*
2459  * Default root read tsize XXX
2460  */
2461 int nfs_root_rsize = 8 * 1024;		/* conservative for dumb NICs */
2462 int nfs4_root_rsize = 32 * 1024;	/* only runs on TCP be aggressive */
2463 
2464 /*
2465  * Default flags: NFSMNT_NOCTO|NFSMNT_LLOCK|NFSMNT_INT
2466  */
2467 int nfs_rootopts = NFSMNT_NOCTO|NFSMNT_LLOCK|NFSMNT_INT;
2468 
2469 static int
2470 init_mountopts(struct nfs_args *args, int version, struct knetconfig **dl_cf,
2471     int *vfsflags)
2472 {
2473 	char servername[SYS_NMLN];
2474 	static int first = 0;
2475 	struct netbuf server_address;
2476 	char *opts, *val;
2477 	int vers;
2478 	struct knetconfig *cf = *dl_cf;
2479 	char rootoptsbuf[256];
2480 
2481 	/*
2482 	 * Set default mount options
2483 	 */
2484 	args->flags = nfs_rootopts;
2485 	args->rsize = 0;
2486 	args->flags |= NFSMNT_ACREGMIN;
2487 	args->acregmin = ACMINMAX;
2488 	args->flags |= NFSMNT_ACREGMAX;
2489 	args->acregmax = ACMAXMAX;
2490 	args->flags |= NFSMNT_ACDIRMIN;
2491 	args->acdirmin = ACMINMAX;
2492 	args->flags |= NFSMNT_ACDIRMAX;
2493 	args->acdirmax = ACMAXMAX;
2494 
2495 	*vfsflags = 0;
2496 
2497 	/*
2498 	 * Only look up the rootopts the first time, we store this in
2499 	 * a static buffer but we are guaranteed to be single threaded
2500 	 * and not reentrant.
2501 	 */
2502 	if (first == 0) {
2503 		first++;
2504 
2505 		init_netbuf(&server_address);
2506 
2507 		if (getfile("rootopts", servername, &server_address,
2508 		    rootopts)) {
2509 			rootopts[0] = '\0';
2510 			free_netbuf(&server_address);
2511 			goto sanity;
2512 		}
2513 		free_netbuf(&server_address);
2514 	}
2515 
2516 	if (dldebug)
2517 		printf("rootopts = %s\n", rootopts);
2518 
2519 	/*
2520 	 * We have to preserve rootopts for second time.
2521 	 */
2522 	(void) strncpy(rootoptsbuf, rootopts, sizeof (rootoptsbuf));
2523 	rootoptsbuf[sizeof (rootoptsbuf) - 1] = '\0';
2524 	opts = rootoptsbuf;
2525 	while (*opts) {
2526 		int opt;
2527 
2528 		switch (opt = getsubopt(&opts, optlist, &val)) {
2529 		/*
2530 		 * Options that are defaults or meaningless so ignored
2531 		 */
2532 		case OPT_QUOTA:
2533 		case OPT_NOQUOTA:
2534 		case OPT_SUID:
2535 		case OPT_DEVICES:
2536 		case OPT_SETUID:
2537 		case OPT_BG:
2538 		case OPT_FG:
2539 		case OPT_RETRY:
2540 		case OPT_POSIX:
2541 		case OPT_LARGEFILES:
2542 		case OPT_XATTR:
2543 		case OPT_NOXATTR:
2544 		case OPT_EXEC:
2545 			break;
2546 		case OPT_RO:
2547 			*vfsflags |= MS_RDONLY;
2548 			break;
2549 		case OPT_RW:
2550 			*vfsflags &= ~(MS_RDONLY);
2551 			break;
2552 		case OPT_SOFT:
2553 			args->flags |= NFSMNT_SOFT;
2554 			args->flags &= ~(NFSMNT_SEMISOFT);
2555 			break;
2556 		case OPT_SEMISOFT:
2557 			args->flags |= NFSMNT_SOFT;
2558 			args->flags |= NFSMNT_SEMISOFT;
2559 			break;
2560 		case OPT_HARD:
2561 			args->flags &= ~(NFSMNT_SOFT);
2562 			args->flags &= ~(NFSMNT_SEMISOFT);
2563 			break;
2564 		case OPT_NOSUID:
2565 		case OPT_NODEVICES:
2566 		case OPT_NOSETUID:
2567 		case OPT_NOEXEC:
2568 			cmn_err(CE_WARN,
2569 			    "nfs_dlboot: may not set root partition %s",
2570 			    optlist[opt]);
2571 			break;
2572 		case OPT_GRPID:
2573 			args->flags |= NFSMNT_GRPID;
2574 			break;
2575 		case OPT_REMOUNT:
2576 			cmn_err(CE_WARN,
2577 			    "nfs_dlboot: may not remount root partition");
2578 			break;
2579 		case OPT_INTR:
2580 			args->flags |= NFSMNT_INT;
2581 			break;
2582 		case OPT_NOINTR:
2583 			args->flags &= ~(NFSMNT_INT);
2584 			break;
2585 		case OPT_NOAC:
2586 			args->flags |= NFSMNT_NOAC;
2587 			break;
2588 		case OPT_PORT:
2589 			cmn_err(CE_WARN,
2590 			    "nfs_dlboot: may not change root port number");
2591 			break;
2592 		case OPT_SECURE:
2593 			cmn_err(CE_WARN,
2594 			"nfs_dlboot: root mounted auth_unix, secure ignored");
2595 			break;
2596 		case OPT_NOCTO:
2597 			args->flags |= NFSMNT_NOCTO;
2598 			break;
2599 		case OPT_RSIZE:
2600 			if (bad(val)) {
2601 				cmn_err(CE_WARN,
2602 				    "nfs_dlboot: invalid option: rsize");
2603 				break;
2604 			}
2605 			args->flags |= NFSMNT_RSIZE;
2606 			args->rsize = atoi(val);
2607 			break;
2608 		case OPT_WSIZE:
2609 			if (bad(val)) {
2610 				cmn_err(CE_WARN,
2611 				    "nfs_dlboot: invalid option: wsize");
2612 				break;
2613 			}
2614 			args->flags |= NFSMNT_WSIZE;
2615 			args->wsize = atoi(val);
2616 			break;
2617 		case OPT_TIMEO:
2618 			if (bad(val)) {
2619 				cmn_err(CE_WARN,
2620 				    "nfs_dlboot: invalid option: timeo");
2621 				break;
2622 			}
2623 			args->flags |= NFSMNT_TIMEO;
2624 			args->timeo = atoi(val);
2625 			break;
2626 		case OPT_RETRANS:
2627 			if (bad(val)) {
2628 				cmn_err(CE_WARN,
2629 				    "nfs_dlboot: invalid option: retrans");
2630 				break;
2631 			}
2632 			args->flags |= NFSMNT_RETRANS;
2633 			args->retrans = atoi(val);
2634 			break;
2635 		case OPT_ACTIMEO:
2636 			if (bad(val)) {
2637 				cmn_err(CE_WARN,
2638 				    "nfs_dlboot: invalid option: actimeo");
2639 				break;
2640 			}
2641 			args->flags |= NFSMNT_ACDIRMAX;
2642 			args->flags |= NFSMNT_ACREGMAX;
2643 			args->flags |= NFSMNT_ACDIRMIN;
2644 			args->flags |= NFSMNT_ACREGMIN;
2645 			args->acdirmin = args->acregmin = args->acdirmax =
2646 			    args->acregmax = atoi(val);
2647 			break;
2648 		case OPT_ACREGMIN:
2649 			if (bad(val)) {
2650 				cmn_err(CE_WARN,
2651 				    "nfs_dlboot: invalid option: acregmin");
2652 				break;
2653 			}
2654 			args->flags |= NFSMNT_ACREGMIN;
2655 			args->acregmin = atoi(val);
2656 			break;
2657 		case OPT_ACREGMAX:
2658 			if (bad(val)) {
2659 				cmn_err(CE_WARN,
2660 				    "nfs_dlboot: invalid option: acregmax");
2661 				break;
2662 			}
2663 			args->flags |= NFSMNT_ACREGMAX;
2664 			args->acregmax = atoi(val);
2665 			break;
2666 		case OPT_ACDIRMIN:
2667 			if (bad(val)) {
2668 				cmn_err(CE_WARN,
2669 				    "nfs_dlboot: invalid option: acdirmin");
2670 				break;
2671 			}
2672 			args->flags |= NFSMNT_ACDIRMIN;
2673 			args->acdirmin = atoi(val);
2674 			break;
2675 		case OPT_ACDIRMAX:
2676 			if (bad(val)) {
2677 				cmn_err(CE_WARN,
2678 				    "nfs_dlboot: invalid option: acdirmax");
2679 				break;
2680 			}
2681 			args->flags |= NFSMNT_ACDIRMAX;
2682 			args->acdirmax = atoi(val);
2683 			break;
2684 		case OPT_LLOCK:
2685 			args->flags |= NFSMNT_LLOCK;
2686 			break;
2687 		case OPT_VERS:
2688 			if (bad(val)) {
2689 				cmn_err(CE_WARN,
2690 				    "nfs_dlboot: invalid option: vers");
2691 				break;
2692 			}
2693 			vers = atoi(val);
2694 			/*
2695 			 * If the requested version is less than what we
2696 			 * chose, pretend the chosen version doesn't exist
2697 			 */
2698 			if (vers < version) {
2699 				return (EPROTONOSUPPORT);
2700 			}
2701 			if (vers > version) {
2702 				cmn_err(CE_WARN,
2703 				    "nfs_dlboot: version %d unavailable",
2704 				    vers);
2705 				return (EINVAL);
2706 			}
2707 			break;
2708 		case OPT_PROTO:
2709 			/*
2710 			 * NFSv4 can only run over TCP, if they requested
2711 			 * UDP pretend v4 doesn't exist, they might not have
2712 			 * specified a version allowing a fallback to v2 or v3.
2713 			 */
2714 			if (version == NFS_V4 && strcmp(val, NC_UDP) == 0)
2715 				return (EPROTONOSUPPORT);
2716 			/*
2717 			 * TCP is always chosen over UDP, so if the
2718 			 * requested is the same as the chosen either
2719 			 * they chose TCP when available or UDP on a UDP
2720 			 * only server.
2721 			 */
2722 			if (strcmp(cf->knc_proto, val) == 0)
2723 				break;
2724 			/*
2725 			 * If we chose UDP, they must have requested TCP
2726 			 */
2727 			if (strcmp(cf->knc_proto, NC_TCP) != 0) {
2728 				cmn_err(CE_WARN,
2729 				    "nfs_dlboot: TCP protocol unavailable");
2730 				return (EINVAL);
2731 			}
2732 			/*
2733 			 * They can only have requested UDP
2734 			 */
2735 			if (strcmp(val, NC_UDP) != 0) {
2736 				cmn_err(CE_WARN,
2737 				    "nfs_dlboot: unknown protocol");
2738 				return (EINVAL);
2739 			}
2740 			*dl_cf = &dl_udp_netconf;
2741 			break;
2742 		case OPT_NOPRINT:
2743 			args->flags |= NFSMNT_NOPRINT;
2744 			break;
2745 		case OPT_NOLARGEFILES:
2746 			cmn_err(CE_WARN,
2747 			    "nfs_dlboot: NFS can't support nolargefiles");
2748 			break;
2749 		case OPT_SEC:
2750 			cmn_err(CE_WARN,
2751 			    "nfs_dlboot: root mounted auth_unix, sec ignored");
2752 			break;
2753 
2754 		case OPT_DIRECTIO:
2755 			args->flags |= NFSMNT_DIRECTIO;
2756 			break;
2757 
2758 		case OPT_NODIRECTIO:
2759 			args->flags &= ~(NFSMNT_DIRECTIO);
2760 			break;
2761 
2762 		default:
2763 			cmn_err(CE_WARN,
2764 			    "nfs_dlboot: ignoring invalid option \"%s\"", val);
2765 			break;
2766 		}
2767 	}
2768 sanity:
2769 	/*
2770 	 * Set some sane limits on read size
2771 	 */
2772 	if (!(args->flags & NFSMNT_RSIZE) || args->rsize == 0) {
2773 		/*
2774 		 * Establish defaults
2775 		 */
2776 		args->flags |= NFSMNT_RSIZE;
2777 		if (version == NFS_V4)
2778 			args->rsize = nfs4_root_rsize;
2779 		else
2780 			args->rsize = nfs_root_rsize;
2781 		return (0);
2782 	}
2783 	/*
2784 	 * No less than 512 bytes, otherwise it will take forever to boot
2785 	 */
2786 	if (args->rsize < 512)
2787 		args->rsize = 512;
2788 	/*
2789 	 * If we are running over UDP, we cannot exceed 64KB, trim
2790 	 * to 56KB to allow room for headers.
2791 	 */
2792 	if (*dl_cf == &dl_udp_netconf && args->rsize > (56 * 1024))
2793 		args->rsize = 56 * 1024;
2794 	return (0);
2795 }
2796