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