xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_dlinet.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
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 OBP 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 	static int once_only = 0;
972 
973 	if (once_only == 1) {
974 		return (0);
975 	}
976 	once_only = 1;
977 
978 	if (dhcack == NULL) {
979 		return (-1);
980 	}
981 
982 	if (dldebug) {
983 		printf("dhcp:  dhcack %p, len %d\n", (void *)dhcack,
984 		    dhcacklen);
985 	}
986 
987 	pl = kmem_alloc(sizeof (PKT_LIST), KM_SLEEP);
988 	pl->len = dhcacklen;
989 	pl->pkt = kmem_alloc(pl->len, KM_SLEEP);
990 	bcopy(dhcack, pl->pkt, dhcacklen);
991 
992 	/*
993 	 * For x86, ifname is not initialized
994 	 * in the netinstall case and dhcack interface name is
995 	 * set in strplumb(). So we only copy the name if ifname
996 	 * is set properly.
997 	 */
998 	if (ifname[0])
999 		(void) strlcpy(dhcifname, ifname, sizeof (dhcifname));
1000 
1001 	/* remember the server_ip in dhcack */
1002 	bcopy((uchar_t *)pl->pkt + 20, dhcp_server_ip, 4);
1003 	bzero(pl->opts, (DHCP_LAST_OPT + 1) * sizeof (DHCP_OPT *));
1004 	bzero(pl->vs, (VS_OPTION_END - VS_OPTION_START + 1) *
1005 	    sizeof (DHCP_OPT *));
1006 
1007 	if (dhcp_options_scan(pl, B_TRUE) != 0) {
1008 		/* garbled packet */
1009 		cmn_err(CE_WARN, "dhcp: DHCP packet parsing failed");
1010 		kmem_free(pl->pkt, pl->len);
1011 		kmem_free(pl, sizeof (PKT_LIST));
1012 		pl = NULL;
1013 		return (-1);
1014 	}
1015 
1016 	/* set node name */
1017 	if (pl->opts[CD_HOSTNAME] != NULL) {
1018 		doptp = pl->opts[CD_HOSTNAME];
1019 		i = doptp->len;
1020 		if (i >= SYS_NMLN) {
1021 			cmn_err(CE_WARN, "dhcp: Hostname is too long");
1022 		} else {
1023 			bcopy(doptp->value, utsname.nodename, i);
1024 			utsname.nodename[i] = '\0';
1025 			if (dldebug) {
1026 				printf("hostname is %s\n",
1027 				    utsname.nodename);
1028 			}
1029 		}
1030 	}
1031 
1032 	/* Set NIS domain name. */
1033 	p = NULL;
1034 	if (pl->opts[CD_NIS_DOMAIN] != NULL) {
1035 		doptp = pl->opts[CD_NIS_DOMAIN];
1036 		i = doptp->len;
1037 		p = (caddr_t)doptp->value;
1038 	}
1039 	if (p != NULL) {
1040 		if (i > SYS_NMLN) {
1041 			cmn_err(CE_WARN,
1042 			    "dhcp: NIS domainname too long.");
1043 		} else {
1044 			bcopy(p, srpc_domain, i);
1045 			srpc_domain[i] = '\0';
1046 			if (dldebug)
1047 				printf("dhcp: NIS domain name is %s\n",
1048 				    srpc_domain);
1049 		}
1050 	}
1051 
1052 	/* fetch netmask */
1053 	if (pl->opts[CD_SUBNETMASK] != NULL) {
1054 		doptp = pl->opts[CD_SUBNETMASK];
1055 		if (doptp->len != sizeof (struct in_addr)) {
1056 			pl->opts[CD_SUBNETMASK] = NULL;
1057 			cmn_err(CE_WARN, "dhcp: netmask option malformed");
1058 		} else {
1059 			bcopy(doptp->value, &subnet, sizeof (struct in_addr));
1060 			if (dldebug)
1061 				printf("dhcp:  setting netmask to: %s\n",
1062 				    inet_ntoa(subnet));
1063 		}
1064 	} else {
1065 		struct in_addr myIPaddr;
1066 
1067 		myIPaddr.s_addr = pl->pkt->yiaddr.s_addr;
1068 		cmn_err(CE_WARN, "dhcp:  no subnet mask supplied - inferring");
1069 		if (IN_CLASSA(ntohl(myIPaddr.s_addr)))
1070 			subnet.s_addr = htonl(IN_CLASSA_NET);
1071 		else if (IN_CLASSB(ntohl(myIPaddr.s_addr)))
1072 			subnet.s_addr = htonl(IN_CLASSB_NET);
1073 		else if (IN_CLASSC(ntohl(myIPaddr.s_addr)))
1074 			subnet.s_addr = htonl(IN_CLASSC_NET);
1075 		else if (IN_CLASSD(ntohl(myIPaddr.s_addr)))
1076 			cmn_err(CE_WARN, "dhcp:  bad IP address (%s)",
1077 			    inet_ntoa(myIPaddr));
1078 		else
1079 			subnet.s_addr = htonl(IN_CLASSE_NET);
1080 	}
1081 	/* and broadcast address */
1082 	if (pl->opts[CD_BROADCASTADDR] != NULL) {
1083 		doptp = pl->opts[CD_BROADCASTADDR];
1084 		if (doptp->len != sizeof (struct in_addr)) {
1085 			pl->opts[CD_BROADCASTADDR] = NULL;
1086 			if (dldebug)
1087 				printf("dhcp:  broadcast address len %d\n",
1088 				    doptp->len);
1089 		} else {
1090 			bcopy(doptp->value, &braddr, sizeof (struct in_addr));
1091 			if (dldebug)
1092 				printf("dhcp:  setting broadcast addr to: %s\n",
1093 				    inet_ntoa(braddr));
1094 		}
1095 	} else {
1096 		if (dldebug)
1097 			printf("dhcp:  no broadcast address supplied\n");
1098 		braddr.s_addr = htonl(INADDR_BROADCAST);
1099 	}
1100 	/* and plumb and initialize interface */
1101 	if ((rc = t_kopen((file_t *)NULL, dl_udp_netconf.knc_rdev,
1102 	    FREAD|FWRITE, &tiptr, CRED())) == 0) {
1103 		if (rc = dlifconfig(tiptr, &pl->pkt->yiaddr, &subnet,
1104 		    &braddr, IFF_DHCPRUNNING)) {
1105 			nfs_perror(rc, "dhcp: dlifconfig failed: %m\n");
1106 			kmem_free(pl->pkt, pl->len);
1107 			kmem_free(pl, sizeof (PKT_LIST));
1108 			pl = NULL;
1109 			(void) t_kclose(tiptr, 0);
1110 			return (-1);
1111 		}
1112 
1113 		/* add routes */
1114 		if (pl->opts[CD_ROUTER] != NULL) {
1115 			doptp = pl->opts[CD_ROUTER];
1116 			if ((doptp->len % sizeof (struct in_addr)) != 0) {
1117 				pl->opts[CD_ROUTER] = NULL;
1118 			} else {
1119 				int nrouters;
1120 				uchar_t *tp;
1121 
1122 				nrouters = doptp->len / sizeof (struct in_addr);
1123 				for (tp = doptp->value, i = 0; i < nrouters;
1124 				    i++) {
1125 					struct in_addr defr;
1126 					struct rtentry	rtentry;
1127 
1128 					bcopy(tp, &defr,
1129 					    sizeof (struct in_addr));
1130 					if (defr.s_addr == 0)
1131 						continue;
1132 
1133 					sin = (struct
1134 					    sockaddr_in *)&rtentry.rt_dst;
1135 
1136 					bzero(sin, sizeof (*sin));
1137 					sin->sin_family = AF_INET;
1138 
1139 					sin = (struct
1140 					    sockaddr_in *)&rtentry.rt_gateway;
1141 					bzero(sin, sizeof (*sin));
1142 					sin->sin_family = AF_INET;
1143 					sin->sin_addr = defr;
1144 
1145 					rtentry.rt_flags = RTF_GATEWAY | RTF_UP;
1146 
1147 					if (rc = rtioctl(tiptr, SIOCADDRT,
1148 					    &rtentry)) {
1149 						nfs_perror(rc,
1150 						    "dhcp: couldn't add route "
1151 						    "to %s: %m.\n",
1152 						    inet_ntoa(defr));
1153 							continue;
1154 					}
1155 					if (dldebug) {
1156 						printf("dhcp: added route %s\n",
1157 						    inet_ntoa(defr));
1158 					}
1159 					tp += sizeof (struct in_addr);
1160 				}
1161 			}
1162 		}
1163 
1164 		(void) t_kclose(tiptr, 0);
1165 	}
1166 
1167 	if (dldebug)
1168 		printf("dhcpinit: leaving\n");
1169 
1170 	return (0);
1171 }
1172 
1173 /*
1174  * Initialize nfs mount info from properties and dhcp response.
1175  */
1176 static void
1177 cacheinit(void)
1178 {
1179 	char *str;
1180 	DHCP_OPT *doptp;
1181 
1182 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1183 	    DDI_PROP_DONTPASS, BP_SERVER_PATH, &server_path_c);
1184 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1185 	    DDI_PROP_DONTPASS, BP_SERVER_NAME, &server_name_c);
1186 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1187 	    DDI_PROP_DONTPASS, BP_SERVER_ROOTOPTS, &str) == DDI_SUCCESS) {
1188 		(void) strncpy(rootopts, str, 255);
1189 		ddi_prop_free(str);
1190 	}
1191 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1192 	    DDI_PROP_DONTPASS, BP_SERVER_IP, &str) == DDI_SUCCESS) {
1193 		if (inet_aton(str, server_ip) != 0)
1194 			cmn_err(CE_NOTE, "server_ipaddr %s is invalid\n",
1195 			    str);
1196 		ddi_prop_free(str);
1197 		if (dldebug)
1198 			printf("server ip is %s\n",
1199 			    inet_ntoa(*(struct in_addr *)server_ip));
1200 	}
1201 
1202 	if (pl == NULL)
1203 		return;
1204 
1205 	/* extract root path in server_path */
1206 	if (server_path_c == NULL) {
1207 		doptp = pl->vs[VS_NFSMNT_ROOTPATH];
1208 		if (doptp != NULL) {
1209 			server_path_c = kmem_alloc(doptp->len + 1, KM_SLEEP);
1210 			bcopy(doptp->value, server_path_c, doptp->len);
1211 			server_path_c[doptp->len] = '\0';
1212 			if (dldebug)
1213 				printf("dhcp:  root path %s\n", server_path_c);
1214 		} else {
1215 			cmn_err(CE_WARN, "dhcp: root server path missing");
1216 		}
1217 	}
1218 
1219 	/* set server_name */
1220 	if (server_name_c == NULL) {
1221 		doptp = pl->vs[VS_NFSMNT_ROOTSRVR_NAME];
1222 		if (doptp != NULL) {
1223 			server_name_c = kmem_alloc(doptp->len + 1, KM_SLEEP);
1224 			bcopy(doptp->value, server_name_c, doptp->len);
1225 			server_name_c[doptp->len] = '\0';
1226 			if (dldebug)
1227 				printf("dhcp: root server name %s\n",
1228 				    server_name_c);
1229 		} else {
1230 			cmn_err(CE_WARN, "dhcp: root server name missing");
1231 		}
1232 	}
1233 
1234 	/* set root server_address */
1235 	if ((*(uint_t *)server_ip) == 0) {
1236 		doptp = pl->vs[VS_NFSMNT_ROOTSRVR_IP];
1237 		if (doptp) {
1238 			bcopy(doptp->value, server_ip, sizeof (server_ip));
1239 			if (dldebug) {
1240 				printf("dhcp:  root server IP address %s\n",
1241 				    inet_ntoa(*(struct in_addr *)server_ip));
1242 			}
1243 		} else {
1244 			if (dldebug)
1245 				cmn_err(CE_CONT,
1246 				    "dhcp: file server ip address missing,"
1247 				    " fallback to dhcp server as file server");
1248 			bcopy(dhcp_server_ip, server_ip, sizeof (server_ip));
1249 		}
1250 	}
1251 
1252 	/* set root file system mount options */
1253 	if (rootopts[0] == 0) {
1254 		doptp = pl->vs[VS_NFSMNT_ROOTOPTS];
1255 		if (doptp != NULL && doptp->len < 255) {
1256 			bcopy(doptp->value, rootopts, doptp->len);
1257 			rootopts[doptp->len] = '\0';
1258 			if (dldebug)
1259 				printf("dhcp:  rootopts %s\n", rootopts);
1260 		} else if (dldebug) {
1261 			printf("dhcp:  no rootopts or too long\n");
1262 			/* not an error */
1263 		}
1264 	}
1265 
1266 	/* now we are done with pl, just free it */
1267 	kmem_free(pl->pkt, pl->len);
1268 	kmem_free(pl, sizeof (PKT_LIST));
1269 	pl = NULL;
1270 }
1271 
1272 static int
1273 cacheinfo(char *name, int namelen,
1274     struct netbuf *server_address, char *rootpath, int pathlen)
1275 {
1276 	static int init_done = 0;
1277 	struct sockaddr_in *sin;
1278 
1279 	if (init_done == 0) {
1280 		cacheinit();
1281 		init_done = 1;
1282 	}
1283 
1284 	/* server_path is a reliable indicator of cache availability */
1285 	if (server_path_c == NULL)
1286 		return (-1);
1287 
1288 	(void) strncpy(rootpath, server_path_c, pathlen);
1289 	if (server_name_c) {
1290 		(void) strncpy(name, server_name_c, namelen);
1291 	} else {
1292 		(void) strncpy(name, "unknown", namelen);
1293 	}
1294 
1295 	sin = (struct sockaddr_in *)server_address->buf;
1296 	sin->sin_family = AF_INET;
1297 	server_address->len = sizeof (struct sockaddr_in);
1298 	bcopy(server_ip, &sin->sin_addr, sizeof (struct in_addr));
1299 	return (0);
1300 }
1301 
1302 /*
1303  *	Set this interface's IP address and netmask, and bring it up.
1304  */
1305 static int
1306 dlifconfig(TIUSER *tiptr, struct in_addr *myIPaddr, struct in_addr *mymask,
1307     struct in_addr *mybraddr, uint_t flags)
1308 {
1309 	int rc;
1310 	struct netbuf sbuf;
1311 	struct sockaddr_in sin;
1312 
1313 	if (dldebug) {
1314 		printf("dlifconfig:  entered\n");
1315 		printf("dlifconfig:  addr %s\n", inet_ntoa(*myIPaddr));
1316 		printf("dlifconfig:  mask %s\n", inet_ntoa(*mymask));
1317 		printf("dlifconfig:  broadcast %s\n", inet_ntoa(*mybraddr));
1318 	}
1319 
1320 	bcopy(myIPaddr, &sin.sin_addr, sizeof (struct in_addr));
1321 	sin.sin_family = AF_INET;
1322 	sbuf.buf = (caddr_t)&sin;
1323 	sbuf.maxlen = sbuf.len = sizeof (sin);
1324 	if (rc = ifioctl(tiptr, SIOCSIFADDR, &sbuf)) {
1325 		nfs_perror(rc,
1326 		    "dlifconfig: couldn't set interface net address: %m\n");
1327 		return (rc);
1328 	}
1329 
1330 	if (mybraddr->s_addr != INADDR_BROADCAST) {
1331 		bcopy(mybraddr, &sin.sin_addr, sizeof (struct in_addr));
1332 		sin.sin_family = AF_INET;
1333 		sbuf.buf = (caddr_t)&sin;
1334 		sbuf.maxlen = sbuf.len = sizeof (sin);
1335 		if (rc = ifioctl(tiptr, SIOCSIFBRDADDR, &sbuf)) {
1336 			nfs_perror(rc,
1337 		    "dlifconfig: couldn't set interface broadcast addr: %m\n");
1338 			return (rc);
1339 		}
1340 	}
1341 
1342 	bcopy(mymask, &sin.sin_addr, sizeof (struct in_addr));
1343 	sin.sin_family = AF_INET;
1344 	sbuf.buf = (caddr_t)&sin;
1345 	sbuf.maxlen = sbuf.len = sizeof (sin);
1346 	if (rc = ifioctl(tiptr, SIOCSIFNETMASK, &sbuf)) {
1347 		nfs_perror(rc,
1348 		    "dlifconfig: couldn't set interface net address: %m\n");
1349 		return (rc);
1350 	}
1351 
1352 	/*
1353 	 * Now turn on the interface.
1354 	 */
1355 	if (rc = setifflags(tiptr, IFF_UP | flags)) {
1356 		nfs_perror(rc,
1357 		    "dlifconfig: couldn't enable network interface: %m\n");
1358 		return (rc);
1359 	}
1360 
1361 	if (dldebug)
1362 		printf("dlifconfig:  returned\n");
1363 	return (0);
1364 }
1365 
1366 static char *
1367 inet_ntoa(struct in_addr in)
1368 {
1369 	static char b[18];
1370 	unsigned char *p;
1371 
1372 	p = (unsigned char *)&in;
1373 	(void) sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
1374 	return (b);
1375 }
1376 
1377 /* We only deal with a.b.c.d decimal format. ip points to 4 byte storage */
1378 static int
1379 inet_aton(char *ipstr, uchar_t *ip)
1380 {
1381 	int i = 0;
1382 	uchar_t val[4] = {0};
1383 	char c = *ipstr;
1384 
1385 	for (;;) {
1386 		if (!isdigit(c))
1387 			return (-1);
1388 		for (;;) {
1389 			if (!isdigit(c))
1390 				break;
1391 			val[i] = val[i] * 10 + (c - '0');
1392 			c = *++ipstr;
1393 		}
1394 		i++;
1395 		if (i == 4)
1396 			break;
1397 		if (c != '.')
1398 			return (-1);
1399 		c = *++ipstr;
1400 	}
1401 	if (c != 0)
1402 		return (-1);
1403 	bcopy(val, ip, 4);
1404 	return (0);
1405 }
1406 
1407 #define	MAX_ADDR_SIZE	128
1408 
1409 /*
1410  * Initialize a netbuf suitable for
1411  * describing an address for the
1412  * transport defined by `tiptr'.
1413  */
1414 static void
1415 init_netbuf(struct netbuf *nbuf)
1416 {
1417 	nbuf->buf = kmem_zalloc(MAX_ADDR_SIZE, KM_SLEEP);
1418 	nbuf->maxlen = MAX_ADDR_SIZE;
1419 	nbuf->len = 0;
1420 }
1421 
1422 static void
1423 free_netbuf(struct netbuf *nbuf)
1424 {
1425 	kmem_free(nbuf->buf, nbuf->maxlen);
1426 	nbuf->buf = NULL;
1427 	nbuf->maxlen = 0;
1428 	nbuf->len = 0;
1429 }
1430 
1431 static int
1432 rtioctl(TIUSER *tiptr, int cmd, struct rtentry *rtentry)
1433 {
1434 	struct strioctl iocb;
1435 	int rc;
1436 	vnode_t *vp;
1437 
1438 	iocb.ic_cmd = cmd;
1439 	iocb.ic_timout = 0;
1440 	iocb.ic_len = sizeof (struct rtentry);
1441 	iocb.ic_dp = (caddr_t)rtentry;
1442 
1443 	vp = tiptr->fp->f_vnode;
1444 	rc = kstr_ioctl(vp, I_STR, (intptr_t)&iocb);
1445 	if (rc)
1446 		nfs_perror(rc, "rtioctl: kstr_ioctl failed: %m\n");
1447 	return (rc);
1448 }
1449 
1450 /*
1451  * Send an ioctl down the stream defined
1452  * by `tiptr'.
1453  *
1454  * We isolate the ifreq dependencies in here. The
1455  * ioctl really ought to take a netbuf and be of
1456  * type TRANSPARENT - one day.
1457  */
1458 static int
1459 ifioctl(TIUSER *tiptr, int cmd, struct netbuf *nbuf)
1460 {
1461 	struct strioctl iocb;
1462 	int rc;
1463 	vnode_t *vp;
1464 	struct ifreq ifr;
1465 
1466 	/*
1467 	 * Now do the one requested.
1468 	 */
1469 	if (nbuf->len)
1470 		ifr.ifr_addr = *(struct sockaddr *)nbuf->buf;
1471 	(void) strncpy((caddr_t)&ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1472 	iocb.ic_cmd = cmd;
1473 	iocb.ic_timout = 0;
1474 	iocb.ic_len = sizeof (ifr);
1475 	iocb.ic_dp = (caddr_t)&ifr;
1476 
1477 	vp = tiptr->fp->f_vnode;
1478 	rc = kstr_ioctl(vp, I_STR, (intptr_t)&iocb);
1479 	if (rc) {
1480 		nfs_perror(rc, "ifioctl: kstr_ioctl failed: %m\n");
1481 		return (rc);
1482 	}
1483 
1484 	/*
1485 	 * Set reply length.
1486 	 */
1487 	if (nbuf->len == 0) {
1488 		/*
1489 		 * GET type.
1490 		 */
1491 		nbuf->len = sizeof (struct sockaddr);
1492 		*(struct sockaddr *)nbuf->buf = ifr.ifr_addr;
1493 	}
1494 
1495 	return (0);
1496 }
1497 
1498 static int
1499 setifflags(TIUSER *tiptr, uint_t value)
1500 {
1501 	struct ifreq ifr;
1502 	int rc;
1503 	struct strioctl iocb;
1504 
1505 	(void) strncpy((caddr_t)&ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1506 	iocb.ic_cmd = SIOCGIFFLAGS;
1507 	iocb.ic_timout = 0;
1508 	iocb.ic_len = sizeof (ifr);
1509 	iocb.ic_dp = (caddr_t)&ifr;
1510 	if (rc = kstr_ioctl(tiptr->fp->f_vnode, I_STR, (intptr_t)&iocb))
1511 		return (rc);
1512 
1513 	ifr.ifr_flags |= value;
1514 	iocb.ic_cmd = SIOCSIFFLAGS;
1515 	return (kstr_ioctl(tiptr->fp->f_vnode, I_STR, (intptr_t)&iocb));
1516 }
1517 
1518 /*
1519  * REVerse Address Resolution Protocol (revarp)
1520  * is used by a diskless client to find out its
1521  * IP address when all it knows is its Ethernet address.
1522  *
1523  * Open the ethernet driver, attach and bind
1524  * (DL_BIND_REQ) it, and then format a broadcast RARP
1525  * message for it to send. We pick up the reply and
1526  * let the caller set the interface address using SIOCSIFADDR.
1527  */
1528 static int
1529 revarp_myaddr(TIUSER *tiptr)
1530 {
1531 	int			rc;
1532 	dl_info_ack_t		info;
1533 	struct sockaddr_in	sin;
1534 	struct netbuf		sbuf;
1535 	ldi_handle_t		lh;
1536 	ldi_ident_t		li;
1537 	struct netbuf		myaddr = {0, 0, NULL};
1538 
1539 	if (dldebug)
1540 		printf("revarp_myaddr: entered\n");
1541 
1542 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
1543 		nfs_perror(rc,
1544 		    "revarp_myaddr: ldi_ident_from_mod failed: %m\n");
1545 		return (rc);
1546 	}
1547 
1548 	rc = ldi_open_by_name(ndev_path, FREAD|FWRITE, CRED(), &lh, li);
1549 	ldi_ident_release(li);
1550 	if (rc) {
1551 		nfs_perror(rc,
1552 		    "revarp_myaddr: ldi_open_by_name failed: %m\n");
1553 		return (rc);
1554 	}
1555 
1556 	if (rc = dl_attach(lh, ifunit)) {
1557 		nfs_perror(rc, "revarp_myaddr: dl_attach failed: %m\n");
1558 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
1559 		return (rc);
1560 	}
1561 
1562 	if (rc = dl_bind(lh, ETHERTYPE_REVARP, 0, DL_CLDLS, 0)) {
1563 		nfs_perror(rc, "revarp_myaddr: dl_bind failed: %m\n");
1564 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
1565 		return (rc);
1566 	}
1567 
1568 	if (rc = dl_info(lh, &info)) {
1569 		nfs_perror(rc, "revarp_myaddr: dl_info failed: %m\n");
1570 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
1571 		return (rc);
1572 	}
1573 
1574 	/* Initialize myaddr */
1575 	myaddr.maxlen = info.dl_addr_length;
1576 	myaddr.buf = kmem_alloc(myaddr.maxlen, KM_SLEEP);
1577 
1578 	revarp_start(lh, &myaddr);
1579 
1580 	bcopy(myaddr.buf, &sin.sin_addr, myaddr.len);
1581 	sin.sin_family = AF_INET;
1582 
1583 	sbuf.buf = (caddr_t)&sin;
1584 	sbuf.maxlen = sbuf.len = sizeof (sin);
1585 	if (rc = ifioctl(tiptr, SIOCSIFADDR, &sbuf)) {
1586 		nfs_perror(rc,
1587 		    "revarp_myaddr: couldn't set interface net address: %m\n");
1588 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
1589 		kmem_free(myaddr.buf, myaddr.maxlen);
1590 		return (rc);
1591 	}
1592 
1593 	/* Now turn on the interface */
1594 	if (rc = setifflags(tiptr, IFF_UP)) {
1595 		nfs_perror(rc,
1596 		    "revarp_myaddr: couldn't enable network interface: %m\n");
1597 	}
1598 
1599 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
1600 	kmem_free(myaddr.buf, myaddr.maxlen);
1601 	return (rc);
1602 }
1603 
1604 static void
1605 revarp_start(ldi_handle_t lh, struct netbuf *myaddr)
1606 {
1607 	struct ether_arp *ea;
1608 	int rc;
1609 	dl_unitdata_req_t *dl_udata;
1610 	mblk_t *bp;
1611 	mblk_t *mp;
1612 	struct dladdr *dlsap;
1613 	static int done = 0;
1614 
1615 	if (dl_phys_addr(lh, &myether) != 0) {
1616 		/* Fallback using per-node address */
1617 		(void) localetheraddr((struct ether_addr *)NULL, &myether);
1618 		cmn_err(CE_CONT, "?DLPI failed to get Ethernet address. Using "
1619 		    "system wide Ethernet address %s\n",
1620 		    ether_sprintf(&myether));
1621 	}
1622 
1623 getreply:
1624 	if (myaddr->len != 0) {
1625 		cmn_err(CE_CONT, "?Found my IP address: %x (%d.%d.%d.%d)\n",
1626 		    *(int *)myaddr->buf,
1627 		    (uchar_t)myaddr->buf[0], (uchar_t)myaddr->buf[1],
1628 		    (uchar_t)myaddr->buf[2], (uchar_t)myaddr->buf[3]);
1629 		return;
1630 	}
1631 
1632 	if (done++ == 0)
1633 		cmn_err(CE_CONT, "?Requesting Internet address for %s\n",
1634 		    ether_sprintf(&myether));
1635 
1636 	/*
1637 	 * Send another RARP request.
1638 	 */
1639 	if ((mp = allocb(sizeof (dl_unitdata_req_t) + sizeof (*dlsap),
1640 	    BPRI_HI)) == NULL) {
1641 		cmn_err(CE_WARN, "revarp_myaddr: allocb no memory");
1642 		return;
1643 	}
1644 	if ((bp = allocb(sizeof (struct ether_arp), BPRI_HI)) == NULL) {
1645 		cmn_err(CE_WARN, "revarp_myaddr: allocb no memory");
1646 		return;
1647 	}
1648 
1649 	/*
1650 	 * Format the transmit request part.
1651 	 */
1652 	mp->b_datap->db_type = M_PROTO;
1653 	dl_udata = (dl_unitdata_req_t *)mp->b_wptr;
1654 	mp->b_wptr += sizeof (dl_unitdata_req_t) + sizeof (*dlsap);
1655 	dl_udata->dl_primitive = DL_UNITDATA_REQ;
1656 	dl_udata->dl_dest_addr_length = sizeof (*dlsap);
1657 	dl_udata->dl_dest_addr_offset = sizeof (*dl_udata);
1658 	dl_udata->dl_priority.dl_min = 0;
1659 	dl_udata->dl_priority.dl_max = 0;
1660 
1661 	dlsap = (struct dladdr *)(mp->b_rptr + sizeof (*dl_udata));
1662 	bcopy(&etherbroadcastaddr, &dlsap->dl_phys,
1663 	    sizeof (etherbroadcastaddr));
1664 	dlsap->dl_sap = ETHERTYPE_REVARP;
1665 
1666 	/*
1667 	 * Format the actual REVARP request.
1668 	 */
1669 	bzero(bp->b_wptr, sizeof (struct ether_arp));
1670 	ea = (struct ether_arp *)bp->b_wptr;
1671 	bp->b_wptr += sizeof (struct ether_arp);
1672 	ea->arp_hrd = htons(ARPHRD_ETHER);
1673 	ea->arp_pro = htons(ETHERTYPE_IP);
1674 	ea->arp_hln = sizeof (ea->arp_sha);	/* hardware address length */
1675 	ea->arp_pln = sizeof (ea->arp_spa);	/* protocol address length */
1676 	ea->arp_op = htons(REVARP_REQUEST);
1677 	ether_copy(&myether, &ea->arp_sha);
1678 	ether_copy(&myether, &ea->arp_tha);
1679 
1680 	mp->b_cont = bp;
1681 
1682 	if ((rc = ldi_putmsg(lh, mp)) != 0) {
1683 		nfs_perror(rc, "revarp_start: ldi_putmsg failed: %m\n");
1684 		return;
1685 	}
1686 	revarpinput(lh, myaddr);
1687 
1688 	goto getreply;
1689 }
1690 
1691 /*
1692  * Client side Reverse-ARP input
1693  * Server side is handled by user level server
1694  */
1695 static void
1696 revarpinput(ldi_handle_t lh, struct netbuf *myaddr)
1697 {
1698 	struct ether_arp *ea;
1699 	mblk_t *bp;
1700 	mblk_t *mp;
1701 	int rc;
1702 	timestruc_t tv, give_up, now;
1703 
1704 	/*
1705 	 * Choose the time at which we will give up, and resend our
1706 	 * request.
1707 	 */
1708 	gethrestime(&give_up);
1709 	give_up.tv_sec += REVARP_TIMEO;
1710 wait:
1711 	/*
1712 	 * Compute new timeout value.
1713 	 */
1714 	tv = give_up;
1715 	gethrestime(&now);
1716 	timespecsub(&tv, &now);
1717 	/*
1718 	 * If we don't have at least one full second remaining, give up.
1719 	 * This means we might wait only just over 4.0 seconds, but that's
1720 	 * okay.
1721 	 */
1722 	if (tv.tv_sec <= 0)
1723 		return;
1724 	rc = ldi_getmsg(lh, &mp, &tv);
1725 	if (rc == ETIME) {
1726 		goto out;
1727 	} else if (rc != 0) {
1728 		nfs_perror(rc, "revarpinput: ldi_getmsg failed: %m\n");
1729 		return;
1730 	}
1731 
1732 	if (mp->b_cont == NULL) {
1733 		printf("revarpinput: b_cont == NULL\n");
1734 		goto out;
1735 	}
1736 
1737 	if (mp->b_datap->db_type != M_PROTO) {
1738 		printf("revarpinput: bad header type %d\n",
1739 		    mp->b_datap->db_type);
1740 		goto out;
1741 	}
1742 
1743 	bp = mp->b_cont;
1744 
1745 	if (bp->b_wptr - bp->b_rptr < sizeof (*ea)) {
1746 		printf("revarpinput: bad data len %d, expect %d\n",
1747 		    (int)(bp->b_wptr - bp->b_rptr),  (int)sizeof (*ea));
1748 		goto out;
1749 	}
1750 
1751 	ea = (struct ether_arp *)bp->b_rptr;
1752 
1753 	if ((ushort_t)ntohs(ea->arp_pro) != ETHERTYPE_IP) {
1754 		/* We could have received another broadcast arp packet. */
1755 		if (dldebug)
1756 			printf("revarpinput: bad type %x\n",
1757 			    (ushort_t)ntohs(ea->arp_pro));
1758 		freemsg(mp);
1759 		goto wait;
1760 	}
1761 	if ((ushort_t)ntohs(ea->arp_op) != REVARP_REPLY) {
1762 		/* We could have received a broadcast arp request. */
1763 		if (dldebug)
1764 			printf("revarpinput: bad op %x\n",
1765 			    (ushort_t)ntohs(ea->arp_op));
1766 		freemsg(mp);
1767 		goto wait;
1768 	}
1769 
1770 	if (!ether_cmp(&ea->arp_tha, &myether)) {
1771 		bcopy(&ea->arp_tpa, myaddr->buf, sizeof (ea->arp_tpa));
1772 		myaddr->len = sizeof (ea->arp_tpa);
1773 	} else {
1774 		/* We could have gotten a broadcast arp response. */
1775 		if (dldebug)
1776 			printf("revarpinput: got reply, but not my address\n");
1777 		freemsg(mp);
1778 		goto wait;
1779 	}
1780 out:
1781 	freemsg(mp);
1782 }
1783 
1784 /*
1785  * From rpcsvc/mountxdr.c in SunOS. We can't
1786  * put this into the rpc directory because
1787  * it calls xdr_fhandle() which is in a
1788  * loadable module.
1789  */
1790 static bool_t
1791 myxdr_fhstatus(XDR *xdrs, struct fhstatus *fhsp)
1792 {
1793 
1794 	if (!xdr_int(xdrs, &fhsp->fhs_status))
1795 		return (FALSE);
1796 	if (fhsp->fhs_status == 0) {
1797 		if (!myxdr_fhandle(xdrs, &fhsp->fhs_fh))
1798 			return (FALSE);
1799 	}
1800 	return (TRUE);
1801 }
1802 
1803 /*
1804  * From nfs_xdr.c.
1805  *
1806  * File access handle
1807  * The fhandle struct is treated a opaque data on the wire
1808  */
1809 static bool_t
1810 myxdr_fhandle(XDR *xdrs, fhandle_t *fh)
1811 {
1812 	return (xdr_opaque(xdrs, (caddr_t)fh, NFS_FHSIZE));
1813 }
1814 
1815 static bool_t
1816 myxdr_mountres3(XDR *xdrs, struct mountres3 *objp)
1817 {
1818 	if (!myxdr_mountstat3(xdrs, &objp->fhs_status))
1819 		return (FALSE);
1820 	switch (objp->fhs_status) {
1821 	case MNT_OK:
1822 		if (!myxdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
1823 			return (FALSE);
1824 		break;
1825 	default:
1826 		break;
1827 	}
1828 	return (TRUE);
1829 }
1830 
1831 static bool_t
1832 myxdr_mountstat3(XDR *xdrs, enum mountstat3 *objp)
1833 {
1834 	return (xdr_enum(xdrs, (enum_t *)objp));
1835 }
1836 
1837 static bool_t
1838 myxdr_mountres3_ok(XDR *xdrs, struct mountres3_ok *objp)
1839 {
1840 	if (!myxdr_fhandle3(xdrs, &objp->fhandle))
1841 		return (FALSE);
1842 	if (!xdr_array(xdrs, (char **)&objp->auth_flavors.auth_flavors_val,
1843 	    (uint_t *)&objp->auth_flavors.auth_flavors_len, ~0,
1844 	    sizeof (int), (xdrproc_t)xdr_int))
1845 		return (FALSE);
1846 	return (TRUE);
1847 }
1848 
1849 static bool_t
1850 myxdr_fhandle3(XDR *xdrs, struct fhandle3 *objp)
1851 {
1852 	return (xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
1853 	    (uint_t *)&objp->fhandle3_len, FHSIZE3));
1854 }
1855 
1856 /*
1857  * From SunOS pmap_clnt.c
1858  *
1859  * Port mapper routines:
1860  *	pmap_kgetport() - get port number.
1861  *	pmap_rmt_call()  - indirect call via port mapper.
1862  *
1863  */
1864 static enum clnt_stat
1865 pmap_kgetport(struct knetconfig *knconf, struct netbuf *call_addr,
1866 	rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
1867 {
1868 	ushort_t port;
1869 	int tries;
1870 	enum clnt_stat stat;
1871 	struct pmap	pmap_parms;
1872 	RPCB		rpcb_parms;
1873 	char		*ua = NULL;
1874 
1875 	port = 0;
1876 
1877 	((struct sockaddr_in *)call_addr->buf)->sin_port = htons(PMAPPORT);
1878 
1879 	pmap_parms.pm_prog = prog;
1880 	pmap_parms.pm_vers = vers;
1881 	pmap_parms.pm_prot = prot;
1882 	pmap_parms.pm_port = 0;
1883 	for (tries = 0; tries < 5; tries++) {
1884 		stat = mycallrpc(knconf, call_addr,
1885 		    PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
1886 		    myxdr_pmap, (char *)&pmap_parms,
1887 		    xdr_u_short, (char *)&port,
1888 		    DEFAULT_TIMEO, DEFAULT_RETRIES);
1889 
1890 		if (stat != RPC_TIMEDOUT)
1891 			break;
1892 		cmn_err(CE_WARN,
1893 		    "pmap_kgetport: Portmapper not responding; still trying");
1894 	}
1895 
1896 	if (stat == RPC_PROGUNAVAIL) {
1897 		cmn_err(CE_WARN,
1898 		    "pmap_kgetport: Portmapper failed - trying rpcbind");
1899 
1900 		rpcb_parms.r_prog = prog;
1901 		rpcb_parms.r_vers = vers;
1902 		rpcb_parms.r_netid = knconf->knc_proto;
1903 		rpcb_parms.r_addr = rpcb_parms.r_owner = "";
1904 
1905 		for (tries = 0; tries < 5; tries++) {
1906 			stat = mycallrpc(knconf, call_addr,
1907 			    RPCBPROG, RPCBVERS, RPCBPROC_GETADDR,
1908 			    xdr_rpcb, (char *)&rpcb_parms,
1909 			    xdr_wrapstring, (char *)&ua,
1910 			    DEFAULT_TIMEO, DEFAULT_RETRIES);
1911 
1912 			if (stat != RPC_TIMEDOUT)
1913 				break;
1914 			cmn_err(CE_WARN,
1915 			"pmap_kgetport: rpcbind not responding; still trying");
1916 		}
1917 
1918 		if (stat == RPC_SUCCESS) {
1919 			if ((ua != NULL) && (ua[0] != NULL)) {
1920 				port = rpc_uaddr2port(AF_INET, ua);
1921 			} else {
1922 				/* Address unknown */
1923 				stat = RPC_PROGUNAVAIL;
1924 			}
1925 		}
1926 	}
1927 
1928 	if (stat == RPC_SUCCESS)
1929 		((struct sockaddr_in *)call_addr->buf)->sin_port = ntohs(port);
1930 
1931 	return (stat);
1932 }
1933 
1934 /*
1935  * pmapper remote-call-service interface.
1936  * This routine is used to call the pmapper remote call service
1937  * which will look up a service program in the port maps, and then
1938  * remotely call that routine with the given parameters.  This allows
1939  * programs to do a lookup and call in one step. In addition to the call_addr,
1940  * the caller provides a boolean hint about the destination address (TRUE if
1941  * address is a broadcast address, FALSE otherwise).
1942  *
1943  * On return, `call addr' contains the port number for the
1944  * service requested, and `resp_addr' contains its IP address.
1945  */
1946 static enum clnt_stat
1947 pmap_rmt_call(struct knetconfig *knconf, struct netbuf *call_addr,
1948 	bool_t bcast, rpcprog_t progn, rpcvers_t versn, rpcproc_t procn,
1949 	xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
1950 	struct timeval tout, struct netbuf *resp_addr)
1951 {
1952 	CLIENT *cl;
1953 	enum clnt_stat stat;
1954 	rpcport_t port;
1955 	int rc;
1956 	struct rmtcallargs	pmap_args;
1957 	struct rmtcallres	pmap_res;
1958 	struct rpcb_rmtcallargs	rpcb_args;
1959 	struct rpcb_rmtcallres	rpcb_res;
1960 	char			ua[100];	/* XXX */
1961 
1962 	((struct sockaddr_in *)call_addr->buf)->sin_port = htons(PMAPPORT);
1963 
1964 	rc = clnt_tli_kcreate(knconf, call_addr, PMAPPROG, PMAPVERS,
1965 	    0, PMAP_RETRIES, CRED(), &cl);
1966 	if (rc != 0) {
1967 		nfs_perror(rc,
1968 		    "pmap_rmt_call: clnt_tli_kcreate failed: %m\n");
1969 		return (RPC_SYSTEMERROR);	/* XXX */
1970 	}
1971 	if (cl == (CLIENT *)NULL) {
1972 		panic("pmap_rmt_call: clnt_tli_kcreate failed");
1973 		/* NOTREACHED */
1974 	}
1975 
1976 	(void) CLNT_CONTROL(cl, CLSET_BCAST, (char *)&bcast);
1977 
1978 	pmap_args.prog = progn;
1979 	pmap_args.vers = versn;
1980 	pmap_args.proc = procn;
1981 	pmap_args.args_ptr = argsp;
1982 	pmap_args.xdr_args = xdrargs;
1983 	pmap_res.port_ptr = &port;
1984 	pmap_res.results_ptr = resp;
1985 	pmap_res.xdr_results = xdrres;
1986 	stat = clnt_clts_kcallit_addr(cl, PMAPPROC_CALLIT,
1987 	    myxdr_rmtcall_args, (caddr_t)&pmap_args,
1988 	    myxdr_rmtcallres, (caddr_t)&pmap_res,
1989 	    tout, resp_addr);
1990 
1991 	if (stat == RPC_SUCCESS) {
1992 		((struct sockaddr_in *)resp_addr->buf)->sin_port =
1993 		    htons((ushort_t)port);
1994 	}
1995 	CLNT_DESTROY(cl);
1996 
1997 	if (stat != RPC_PROGUNAVAIL)
1998 		return (stat);
1999 
2000 	cmn_err(CE_WARN, "pmap_rmt_call: Portmapper failed - trying rpcbind");
2001 
2002 	rc = clnt_tli_kcreate(knconf, call_addr, RPCBPROG, RPCBVERS,
2003 	    0, PMAP_RETRIES, CRED(), &cl);
2004 	if (rc != 0) {
2005 		nfs_perror(rc, "pmap_rmt_call: clnt_tli_kcreate failed: %m\n");
2006 		return (RPC_SYSTEMERROR);	/* XXX */
2007 	}
2008 
2009 	if (cl == NULL) {
2010 		panic("pmap_rmt_call: clnt_tli_kcreate failed");
2011 		/* NOTREACHED */
2012 	}
2013 
2014 	rpcb_args.prog = progn;
2015 	rpcb_args.vers = versn;
2016 	rpcb_args.proc = procn;
2017 	rpcb_args.args_ptr = argsp;
2018 	rpcb_args.xdr_args = xdrargs;
2019 	rpcb_res.addr_ptr = ua;
2020 	rpcb_res.results_ptr = resp;
2021 	rpcb_res.xdr_results = xdrres;
2022 	stat = clnt_clts_kcallit_addr(cl, PMAPPROC_CALLIT,
2023 	    xdr_rpcb_rmtcallargs, (caddr_t)&rpcb_args,
2024 	    xdr_rpcb_rmtcallres, (caddr_t)&rpcb_res,
2025 	    tout, resp_addr);
2026 
2027 	if (stat == RPC_SUCCESS)
2028 		((struct sockaddr_in *)resp_addr->buf)->sin_port =
2029 		    rpc_uaddr2port(AF_INET, ua);
2030 	CLNT_DESTROY(cl);
2031 
2032 	return (stat);
2033 }
2034 
2035 /*
2036  * XDR remote call arguments
2037  * written for XDR_ENCODE direction only
2038  */
2039 static bool_t
2040 myxdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
2041 {
2042 	uint_t lenposition;
2043 	uint_t argposition;
2044 	uint_t position;
2045 
2046 	if (xdr_rpcprog(xdrs, &(cap->prog)) &&
2047 	    xdr_rpcvers(xdrs, &(cap->vers)) &&
2048 	    xdr_rpcproc(xdrs, &(cap->proc))) {
2049 		lenposition = XDR_GETPOS(xdrs);
2050 		if (!xdr_u_int(xdrs, &cap->arglen))
2051 			return (FALSE);
2052 		argposition = XDR_GETPOS(xdrs);
2053 		if (!(*(cap->xdr_args))(xdrs, cap->args_ptr))
2054 			return (FALSE);
2055 		position = XDR_GETPOS(xdrs);
2056 		cap->arglen = (uint_t)position - (uint_t)argposition;
2057 		XDR_SETPOS(xdrs, lenposition);
2058 		if (!xdr_u_int(xdrs, &cap->arglen))
2059 			return (FALSE);
2060 		XDR_SETPOS(xdrs, position);
2061 		return (TRUE);
2062 	}
2063 	return (FALSE);
2064 }
2065 
2066 /*
2067  * XDR remote call results
2068  * written for XDR_DECODE direction only
2069  */
2070 static bool_t
2071 myxdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
2072 {
2073 	caddr_t port_ptr;
2074 
2075 	port_ptr = (caddr_t)crp->port_ptr;
2076 	if (xdr_reference(xdrs, &port_ptr, sizeof (uint_t), xdr_u_int) &&
2077 	    xdr_u_int(xdrs, &crp->resultslen)) {
2078 		crp->port_ptr = (rpcport_t *)port_ptr;
2079 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
2080 	}
2081 	return (FALSE);
2082 }
2083 
2084 static bool_t
2085 myxdr_pmap(XDR *xdrs, struct pmap *regs)
2086 {
2087 	if (xdr_rpcprog(xdrs, &regs->pm_prog) &&
2088 	    xdr_rpcvers(xdrs, &regs->pm_vers) &&
2089 	    xdr_rpcprot(xdrs, &regs->pm_prot))
2090 		return (xdr_rpcport(xdrs, &regs->pm_port));
2091 
2092 	return (FALSE);
2093 }
2094 
2095 
2096 /*
2097  * From SunOS callrpc.c
2098  */
2099 static enum clnt_stat
2100 mycallrpc(struct knetconfig *knconf, struct netbuf *call_addr,
2101 	rpcprog_t prognum, rpcvers_t versnum, rpcproc_t procnum,
2102 	xdrproc_t inproc, char *in, xdrproc_t outproc, char *out,
2103 	int timeo, int retries)
2104 {
2105 	CLIENT *cl;
2106 	struct timeval tv;
2107 	enum clnt_stat cl_stat;
2108 	int rc;
2109 
2110 	rc = clnt_tli_kcreate(knconf, call_addr, prognum, versnum,
2111 	    0, retries, CRED(), &cl);
2112 	if (rc) {
2113 		nfs_perror(rc, "mycallrpc: clnt_tli_kcreate failed: %m\n");
2114 		return (RPC_SYSTEMERROR);	/* XXX */
2115 	}
2116 	tv.tv_sec = timeo;
2117 	tv.tv_usec = 0;
2118 	cl_stat = CLNT_CALL(cl, procnum, inproc, in, outproc, out, tv);
2119 	AUTH_DESTROY(cl->cl_auth);
2120 	CLNT_DESTROY(cl);
2121 	return (cl_stat);
2122 }
2123 
2124 static int
2125 dl_info(ldi_handle_t lh, dl_info_ack_t *info)
2126 {
2127 	dl_info_req_t *info_req;
2128 	dl_error_ack_t *error_ack;
2129 	union DL_primitives *dl_prim;
2130 	mblk_t *mp;
2131 	int error;
2132 
2133 	if ((mp = allocb(sizeof (dl_info_req_t), BPRI_MED)) == NULL) {
2134 		cmn_err(CE_WARN, "dl_info: allocb failed");
2135 		return (ENOSR);
2136 	}
2137 	mp->b_datap->db_type = M_PROTO;
2138 
2139 	info_req = (dl_info_req_t *)mp->b_wptr;
2140 	mp->b_wptr += sizeof (dl_info_req_t);
2141 	info_req->dl_primitive = DL_INFO_REQ;
2142 
2143 	(void) ldi_putmsg(lh, mp);
2144 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
2145 		nfs_perror(error, "dl_info: ldi_getmsg failed: %m\n");
2146 		return (error);
2147 	}
2148 
2149 	dl_prim = (union DL_primitives *)mp->b_rptr;
2150 	switch (dl_prim->dl_primitive) {
2151 	case DL_INFO_ACK:
2152 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_info_ack_t)) {
2153 			printf("dl_info: DL_INFO_ACK protocol error\n");
2154 			break;
2155 		}
2156 		*info = *(dl_info_ack_t *)mp->b_rptr;
2157 		freemsg(mp);
2158 		return (0);
2159 
2160 	case DL_ERROR_ACK:
2161 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
2162 			printf("dl_info: DL_ERROR_ACK protocol error\n");
2163 			break;
2164 		}
2165 
2166 		error_ack = (dl_error_ack_t *)dl_prim;
2167 		printf("dl_info: DLPI error %u\n", error_ack->dl_errno);
2168 		break;
2169 
2170 	default:
2171 		printf("dl_bind: bad ACK header %u\n", dl_prim->dl_primitive);
2172 		break;
2173 	}
2174 
2175 	/*
2176 	 * Error return only.
2177 	 */
2178 	freemsg(mp);
2179 	return (-1);
2180 }
2181 
2182 /*
2183  * Configure the 'default' interface based on existing boot properties.
2184  */
2185 static int
2186 bp_netconfig(void)
2187 {
2188 	char *str;
2189 	struct in_addr my_ip, my_netmask, my_router, my_broadcast;
2190 	struct sockaddr_in *sin;
2191 	TIUSER *tiptr;
2192 	int rc;
2193 	struct rtentry rtentry;
2194 
2195 	my_ip.s_addr = my_netmask.s_addr = my_router.s_addr = 0;
2196 
2197 	/*
2198 	 * No way of getting this right now.  Collude with dlifconfig()
2199 	 * to let the protocol stack choose.
2200 	 */
2201 	my_broadcast.s_addr = INADDR_BROADCAST;
2202 
2203 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2204 	    DDI_PROP_DONTPASS, BP_HOST_IP, &str) == DDI_SUCCESS) {
2205 		if (inet_aton(str, (uchar_t *)&my_ip) != 0)
2206 			cmn_err(CE_NOTE, "host-ip %s is invalid\n",
2207 			    str);
2208 		ddi_prop_free(str);
2209 		if (dldebug)
2210 			printf("host ip is %s\n",
2211 			    inet_ntoa(my_ip));
2212 	}
2213 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2214 	    DDI_PROP_DONTPASS, BP_SUBNET_MASK, &str) == DDI_SUCCESS) {
2215 		if (inet_aton(str, (uchar_t *)&my_netmask) != 0)
2216 			cmn_err(CE_NOTE, "subnet-mask %s is invalid\n",
2217 			    str);
2218 		ddi_prop_free(str);
2219 		if (dldebug)
2220 			printf("subnet mask is %s\n",
2221 			    inet_ntoa(my_netmask));
2222 	}
2223 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2224 	    DDI_PROP_DONTPASS, BP_ROUTER_IP, &str) == DDI_SUCCESS) {
2225 		if (inet_aton(str, (uchar_t *)&my_router) != 0)
2226 			cmn_err(CE_NOTE, "router-ip %s is invalid\n",
2227 			    str);
2228 		ddi_prop_free(str);
2229 		if (dldebug)
2230 			printf("router ip is %s\n",
2231 			    inet_ntoa(my_router));
2232 	}
2233 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2234 	    DDI_PROP_DONTPASS, BP_SERVER_PATH, &server_path_c);
2235 	(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2236 	    DDI_PROP_DONTPASS, BP_SERVER_NAME, &server_name_c);
2237 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2238 	    DDI_PROP_DONTPASS, BP_SERVER_ROOTOPTS, &str) == DDI_SUCCESS) {
2239 		(void) strlcpy(rootopts, str, sizeof (rootopts));
2240 		ddi_prop_free(str);
2241 	}
2242 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
2243 	    DDI_PROP_DONTPASS, BP_SERVER_IP, &str) == DDI_SUCCESS) {
2244 		if (inet_aton(str, server_ip) != 0)
2245 			cmn_err(CE_NOTE, "server-ip %s is invalid\n",
2246 			    str);
2247 		ddi_prop_free(str);
2248 		if (dldebug)
2249 			printf("server ip is %s\n",
2250 			    inet_ntoa(*(struct in_addr *)server_ip));
2251 	}
2252 
2253 	/*
2254 	 * We need all of these to configure based on properties.
2255 	 */
2256 	if ((my_ip.s_addr == 0) ||
2257 	    (my_netmask.s_addr == 0) ||
2258 	    (server_path_c == NULL) ||
2259 	    (server_name_c == NULL) ||
2260 	    (*(uint_t *)server_ip == 0))
2261 		return (-1);
2262 
2263 	cmn_err(CE_CONT, "?IP address: %s\n", inet_ntoa(my_ip));
2264 	cmn_err(CE_CONT, "?IP netmask: %s\n", inet_ntoa(my_netmask));
2265 	if (my_router.s_addr != 0)
2266 		cmn_err(CE_CONT, "?IP router: %s\n", inet_ntoa(my_router));
2267 	cmn_err(CE_CONT, "?NFS server: %s (%s)\n", server_name_c,
2268 	    inet_ntoa(*(struct in_addr *)server_ip));
2269 	cmn_err(CE_CONT, "?NFS path: %s\n", server_path_c);
2270 
2271 	/*
2272 	 * Configure the interface.
2273 	 */
2274 	if ((rc = t_kopen((file_t *)NULL, dl_udp_netconf.knc_rdev,
2275 	    FREAD|FWRITE, &tiptr, CRED())) != 0) {
2276 		nfs_perror(rc, "bp_netconfig: t_kopen udp failed: %m.\n");
2277 		return (rc);
2278 	}
2279 
2280 	if ((rc = dlifconfig(tiptr, &my_ip, &my_netmask, &my_broadcast,
2281 	    0)) < 0) {
2282 		nfs_perror(rc, "bp_netconfig: dlifconfig failed: %m.\n");
2283 		(void) t_kclose(tiptr, 0);
2284 		return (rc);
2285 	}
2286 
2287 	if (my_router.s_addr != 0) {
2288 		/*
2289 		 * Add a default route.
2290 		 */
2291 		sin = (struct sockaddr_in *)&rtentry.rt_dst;
2292 		bzero(sin, sizeof (*sin));
2293 		sin->sin_family = AF_INET;
2294 
2295 		sin = (struct sockaddr_in *)&rtentry.rt_gateway;
2296 		bzero(sin, sizeof (*sin));
2297 		sin->sin_family = AF_INET;
2298 		sin->sin_addr = my_router;
2299 
2300 		rtentry.rt_flags = RTF_GATEWAY | RTF_UP;
2301 
2302 		if ((rc = rtioctl(tiptr, SIOCADDRT, &rtentry)) != 0) {
2303 			nfs_perror(rc,
2304 			    "bp_netconfig: couldn't add route: %m.\n");
2305 			(void) t_kclose(tiptr, 0);
2306 			return (rc);
2307 		}
2308 	}
2309 
2310 	(void) t_kclose(tiptr, 0);
2311 
2312 	return (0);
2313 }
2314 
2315 /*
2316  * The network device we will use to boot from is plumbed. Extract the details
2317  * from rootfs.
2318  */
2319 static void
2320 init_config(void)
2321 {
2322 	(void) strlcpy(ndev_path, rootfs.bo_devname, sizeof (ndev_path));
2323 	(void) strlcpy(ifname, rootfs.bo_ifname, sizeof (ifname));
2324 	ifunit = rootfs.bo_ppa;
2325 
2326 	/*
2327 	 * Assumes only one linkage array element.
2328 	 */
2329 	dl_udp_netconf.knc_rdev =
2330 	    makedevice(clone_major, ddi_name_to_major("udp"));
2331 	dl_tcp_netconf.knc_rdev =
2332 	    makedevice(clone_major, ddi_name_to_major("tcp"));
2333 
2334 	/*
2335 	 * Now we bringup the interface.
2336 	 * Try cached dhcp response first. If it fails, do rarp.
2337 	 */
2338 	if ((bp_netconfig() != 0) &&
2339 	    (dhcpinit() != 0) &&
2340 	    (whoami() != 0))
2341 		cmn_err(CE_WARN,
2342 		    "%s: no response from interface", ifname);
2343 	else if (dldebug)
2344 		printf("init_config: ifname %s is up\n", ifname);
2345 }
2346 
2347 /*
2348  * These options are duplicated in cmd/fs.d/nfs/mount/mount.c
2349  * Changes must be made to both lists.
2350  */
2351 static char *optlist[] = {
2352 #define	OPT_RO		0
2353 	MNTOPT_RO,
2354 #define	OPT_RW		1
2355 	MNTOPT_RW,
2356 #define	OPT_QUOTA	2
2357 	MNTOPT_QUOTA,
2358 #define	OPT_NOQUOTA	3
2359 	MNTOPT_NOQUOTA,
2360 #define	OPT_SOFT	4
2361 	MNTOPT_SOFT,
2362 #define	OPT_HARD	5
2363 	MNTOPT_HARD,
2364 #define	OPT_SUID	6
2365 	MNTOPT_SUID,
2366 #define	OPT_NOSUID	7
2367 	MNTOPT_NOSUID,
2368 #define	OPT_GRPID	8
2369 	MNTOPT_GRPID,
2370 #define	OPT_REMOUNT	9
2371 	MNTOPT_REMOUNT,
2372 #define	OPT_NOSUB	10
2373 	MNTOPT_NOSUB,
2374 #define	OPT_INTR	11
2375 	MNTOPT_INTR,
2376 #define	OPT_NOINTR	12
2377 	MNTOPT_NOINTR,
2378 #define	OPT_PORT	13
2379 	MNTOPT_PORT,
2380 #define	OPT_SECURE	14
2381 	MNTOPT_SECURE,
2382 #define	OPT_RSIZE	15
2383 	MNTOPT_RSIZE,
2384 #define	OPT_WSIZE	16
2385 	MNTOPT_WSIZE,
2386 #define	OPT_TIMEO	17
2387 	MNTOPT_TIMEO,
2388 #define	OPT_RETRANS	18
2389 	MNTOPT_RETRANS,
2390 #define	OPT_ACTIMEO	19
2391 	MNTOPT_ACTIMEO,
2392 #define	OPT_ACREGMIN	20
2393 	MNTOPT_ACREGMIN,
2394 #define	OPT_ACREGMAX	21
2395 	MNTOPT_ACREGMAX,
2396 #define	OPT_ACDIRMIN	22
2397 	MNTOPT_ACDIRMIN,
2398 #define	OPT_ACDIRMAX	23
2399 	MNTOPT_ACDIRMAX,
2400 #define	OPT_BG		24
2401 	MNTOPT_BG,
2402 #define	OPT_FG		25
2403 	MNTOPT_FG,
2404 #define	OPT_RETRY	26
2405 	MNTOPT_RETRY,
2406 #define	OPT_NOAC	27
2407 	MNTOPT_NOAC,
2408 #define	OPT_NOCTO	28
2409 	MNTOPT_NOCTO,
2410 #define	OPT_LLOCK	29
2411 	MNTOPT_LLOCK,
2412 #define	OPT_POSIX	30
2413 	MNTOPT_POSIX,
2414 #define	OPT_VERS	31
2415 	MNTOPT_VERS,
2416 #define	OPT_PROTO	32
2417 	MNTOPT_PROTO,
2418 #define	OPT_SEMISOFT	33
2419 	MNTOPT_SEMISOFT,
2420 #define	OPT_NOPRINT	34
2421 	MNTOPT_NOPRINT,
2422 #define	OPT_SEC		35
2423 	MNTOPT_SEC,
2424 #define	OPT_LARGEFILES	36
2425 	MNTOPT_LARGEFILES,
2426 #define	OPT_NOLARGEFILES	37
2427 	MNTOPT_NOLARGEFILES,
2428 #define	OPT_PUBLIC	38
2429 	MNTOPT_PUBLIC,
2430 #define	OPT_DIRECTIO	39
2431 	MNTOPT_FORCEDIRECTIO,
2432 #define	OPT_NODIRECTIO	40
2433 	MNTOPT_NOFORCEDIRECTIO,
2434 #define	OPT_XATTR	41
2435 	MNTOPT_XATTR,
2436 #define	OPT_NOXATTR	42
2437 	MNTOPT_NOXATTR,
2438 #define	OPT_DEVICES	43
2439 	MNTOPT_DEVICES,
2440 #define	OPT_NODEVICES	44
2441 	MNTOPT_NODEVICES,
2442 #define	OPT_SETUID	45
2443 	MNTOPT_SETUID,
2444 #define	OPT_NOSETUID	46
2445 	MNTOPT_NOSETUID,
2446 #define	OPT_EXEC	47
2447 	MNTOPT_EXEC,
2448 #define	OPT_NOEXEC	48
2449 	MNTOPT_NOEXEC,
2450 	NULL
2451 };
2452 
2453 static int
2454 isdigit(int ch)
2455 {
2456 	return (ch >= '0' && ch <= '9');
2457 }
2458 
2459 #define	isspace(c)	((c) == ' ' || (c) == '\t' || (c) == '\n')
2460 #define	bad(val)	(val == NULL || !isdigit(*val))
2461 
2462 static int
2463 atoi(const char *p)
2464 {
2465 	int n;
2466 	int c, neg = 0;
2467 
2468 	if (!isdigit(c = *p)) {
2469 		while (isspace(c))
2470 			c = *++p;
2471 		switch (c) {
2472 		case '-':
2473 			neg++;
2474 			/* FALLTHROUGH */
2475 		case '+':
2476 			c = *++p;
2477 		}
2478 		if (!isdigit(c))
2479 			return (0);
2480 	}
2481 	for (n = '0' - c; isdigit(c = *++p); ) {
2482 		n *= 10; /* two steps to avoid unnecessary overflow */
2483 		n += '0' - c; /* accum neg to avoid surprises at MAX */
2484 	}
2485 	return (neg ? n : -n);
2486 }
2487 
2488 /*
2489  * Default root read tsize XXX
2490  */
2491 int nfs_root_rsize = 8 * 1024;		/* conservative for dumb NICs */
2492 int nfs4_root_rsize = 32 * 1024;	/* only runs on TCP be aggressive */
2493 
2494 /*
2495  * Default flags: NFSMNT_NOCTO|NFSMNT_LLOCK|NFSMNT_INT
2496  */
2497 int nfs_rootopts = NFSMNT_NOCTO|NFSMNT_LLOCK|NFSMNT_INT;
2498 
2499 static int
2500 init_mountopts(struct nfs_args *args, int version, struct knetconfig **dl_cf,
2501 						int *vfsflags)
2502 {
2503 	char servername[SYS_NMLN];
2504 	static int first = 0;
2505 	struct netbuf server_address;
2506 	char *opts, *val;
2507 	int vers;
2508 	struct knetconfig *cf = *dl_cf;
2509 	char rootoptsbuf[256];
2510 
2511 	/*
2512 	 * Set default mount options
2513 	 */
2514 	args->flags = nfs_rootopts;
2515 	args->rsize = 0;
2516 	args->flags |= NFSMNT_ACREGMIN;
2517 	args->acregmin = ACMINMAX;
2518 	args->flags |= NFSMNT_ACREGMAX;
2519 	args->acregmax = ACMAXMAX;
2520 	args->flags |= NFSMNT_ACDIRMIN;
2521 	args->acdirmin = ACMINMAX;
2522 	args->flags |= NFSMNT_ACDIRMAX;
2523 	args->acdirmax = ACMAXMAX;
2524 
2525 	*vfsflags = 0;
2526 
2527 	/*
2528 	 * Only look up the rootopts the first time, we store this in
2529 	 * a static buffer but we are guaranteed to be single threaded
2530 	 * and not reentrant.
2531 	 */
2532 	if (first == 0) {
2533 		first++;
2534 
2535 		init_netbuf(&server_address);
2536 
2537 		if (getfile("rootopts", servername, &server_address,
2538 		    rootopts)) {
2539 			rootopts[0] = '\0';
2540 			free_netbuf(&server_address);
2541 			goto sanity;
2542 		}
2543 		free_netbuf(&server_address);
2544 	}
2545 
2546 	if (dldebug)
2547 		printf("rootopts = %s\n", rootopts);
2548 
2549 	/*
2550 	 * We have to preserve rootopts for second time.
2551 	 */
2552 	(void) strncpy(rootoptsbuf, rootopts, sizeof (rootoptsbuf));
2553 	rootoptsbuf[sizeof (rootoptsbuf) - 1] = '\0';
2554 	opts = rootoptsbuf;
2555 	while (*opts) {
2556 		int opt;
2557 
2558 		switch (opt = getsubopt(&opts, optlist, &val)) {
2559 		/*
2560 		 * Options that are defaults or meaningless so ignored
2561 		 */
2562 		case OPT_QUOTA:
2563 		case OPT_NOQUOTA:
2564 		case OPT_SUID:
2565 		case OPT_DEVICES:
2566 		case OPT_SETUID:
2567 		case OPT_BG:
2568 		case OPT_FG:
2569 		case OPT_RETRY:
2570 		case OPT_POSIX:
2571 		case OPT_LARGEFILES:
2572 		case OPT_XATTR:
2573 		case OPT_NOXATTR:
2574 		case OPT_EXEC:
2575 			break;
2576 		case OPT_RO:
2577 			*vfsflags |= MS_RDONLY;
2578 			break;
2579 		case OPT_RW:
2580 			*vfsflags &= ~(MS_RDONLY);
2581 			break;
2582 		case OPT_SOFT:
2583 			args->flags |= NFSMNT_SOFT;
2584 			args->flags &= ~(NFSMNT_SEMISOFT);
2585 			break;
2586 		case OPT_SEMISOFT:
2587 			args->flags |= NFSMNT_SOFT;
2588 			args->flags |= NFSMNT_SEMISOFT;
2589 			break;
2590 		case OPT_HARD:
2591 			args->flags &= ~(NFSMNT_SOFT);
2592 			args->flags &= ~(NFSMNT_SEMISOFT);
2593 			break;
2594 		case OPT_NOSUID:
2595 		case OPT_NODEVICES:
2596 		case OPT_NOSETUID:
2597 		case OPT_NOEXEC:
2598 			cmn_err(CE_WARN,
2599 			    "nfs_dlboot: may not set root partition %s",
2600 			    optlist[opt]);
2601 			break;
2602 		case OPT_GRPID:
2603 			args->flags |= NFSMNT_GRPID;
2604 			break;
2605 		case OPT_REMOUNT:
2606 			cmn_err(CE_WARN,
2607 			    "nfs_dlboot: may not remount root partition");
2608 			break;
2609 		case OPT_INTR:
2610 			args->flags |= NFSMNT_INT;
2611 			break;
2612 		case OPT_NOINTR:
2613 			args->flags &= ~(NFSMNT_INT);
2614 			break;
2615 		case OPT_NOAC:
2616 			args->flags |= NFSMNT_NOAC;
2617 			break;
2618 		case OPT_PORT:
2619 			cmn_err(CE_WARN,
2620 			    "nfs_dlboot: may not change root port number");
2621 			break;
2622 		case OPT_SECURE:
2623 			cmn_err(CE_WARN,
2624 			"nfs_dlboot: root mounted auth_unix, secure ignored");
2625 			break;
2626 		case OPT_NOCTO:
2627 			args->flags |= NFSMNT_NOCTO;
2628 			break;
2629 		case OPT_RSIZE:
2630 			if (bad(val)) {
2631 				cmn_err(CE_WARN,
2632 				    "nfs_dlboot: invalid option: rsize");
2633 				break;
2634 			}
2635 			args->flags |= NFSMNT_RSIZE;
2636 			args->rsize = atoi(val);
2637 			break;
2638 		case OPT_WSIZE:
2639 			if (bad(val)) {
2640 				cmn_err(CE_WARN,
2641 				    "nfs_dlboot: invalid option: wsize");
2642 				break;
2643 			}
2644 			args->flags |= NFSMNT_WSIZE;
2645 			args->wsize = atoi(val);
2646 			break;
2647 		case OPT_TIMEO:
2648 			if (bad(val)) {
2649 				cmn_err(CE_WARN,
2650 				    "nfs_dlboot: invalid option: timeo");
2651 				break;
2652 			}
2653 			args->flags |= NFSMNT_TIMEO;
2654 			args->timeo = atoi(val);
2655 			break;
2656 		case OPT_RETRANS:
2657 			if (bad(val)) {
2658 				cmn_err(CE_WARN,
2659 				    "nfs_dlboot: invalid option: retrans");
2660 				break;
2661 			}
2662 			args->flags |= NFSMNT_RETRANS;
2663 			args->retrans = atoi(val);
2664 			break;
2665 		case OPT_ACTIMEO:
2666 			if (bad(val)) {
2667 				cmn_err(CE_WARN,
2668 				    "nfs_dlboot: invalid option: actimeo");
2669 				break;
2670 			}
2671 			args->flags |= NFSMNT_ACDIRMAX;
2672 			args->flags |= NFSMNT_ACREGMAX;
2673 			args->flags |= NFSMNT_ACDIRMIN;
2674 			args->flags |= NFSMNT_ACREGMIN;
2675 			args->acdirmin = args->acregmin = args->acdirmax =
2676 			    args->acregmax = atoi(val);
2677 			break;
2678 		case OPT_ACREGMIN:
2679 			if (bad(val)) {
2680 				cmn_err(CE_WARN,
2681 				    "nfs_dlboot: invalid option: acregmin");
2682 				break;
2683 			}
2684 			args->flags |= NFSMNT_ACREGMIN;
2685 			args->acregmin = atoi(val);
2686 			break;
2687 		case OPT_ACREGMAX:
2688 			if (bad(val)) {
2689 				cmn_err(CE_WARN,
2690 				    "nfs_dlboot: invalid option: acregmax");
2691 				break;
2692 			}
2693 			args->flags |= NFSMNT_ACREGMAX;
2694 			args->acregmax = atoi(val);
2695 			break;
2696 		case OPT_ACDIRMIN:
2697 			if (bad(val)) {
2698 				cmn_err(CE_WARN,
2699 				    "nfs_dlboot: invalid option: acdirmin");
2700 				break;
2701 			}
2702 			args->flags |= NFSMNT_ACDIRMIN;
2703 			args->acdirmin = atoi(val);
2704 			break;
2705 		case OPT_ACDIRMAX:
2706 			if (bad(val)) {
2707 				cmn_err(CE_WARN,
2708 				    "nfs_dlboot: invalid option: acdirmax");
2709 				break;
2710 			}
2711 			args->flags |= NFSMNT_ACDIRMAX;
2712 			args->acdirmax = atoi(val);
2713 			break;
2714 		case OPT_LLOCK:
2715 			args->flags |= NFSMNT_LLOCK;
2716 			break;
2717 		case OPT_VERS:
2718 			if (bad(val)) {
2719 				cmn_err(CE_WARN,
2720 				    "nfs_dlboot: invalid option: vers");
2721 				break;
2722 			}
2723 			vers = atoi(val);
2724 			/*
2725 			 * If the requested version is less than what we
2726 			 * chose, pretend the chosen version doesn't exist
2727 			 */
2728 			if (vers < version) {
2729 				return (EPROTONOSUPPORT);
2730 			}
2731 			if (vers > version) {
2732 				cmn_err(CE_WARN,
2733 				    "nfs_dlboot: version %d unavailable",
2734 				    vers);
2735 				return (EINVAL);
2736 			}
2737 			break;
2738 		case OPT_PROTO:
2739 			/*
2740 			 * NFSv4 can only run over TCP, if they requested
2741 			 * UDP pretend v4 doesn't exist, they might not have
2742 			 * specified a version allowing a fallback to v2 or v3.
2743 			 */
2744 			if (version == NFS_V4 && strcmp(val, NC_UDP) == 0)
2745 				return (EPROTONOSUPPORT);
2746 			/*
2747 			 * TCP is always chosen over UDP, so if the
2748 			 * requested is the same as the chosen either
2749 			 * they chose TCP when available or UDP on a UDP
2750 			 * only server.
2751 			 */
2752 			if (strcmp(cf->knc_proto, val) == 0)
2753 				break;
2754 			/*
2755 			 * If we chose UDP, they must have requested TCP
2756 			 */
2757 			if (strcmp(cf->knc_proto, NC_TCP) != 0) {
2758 				cmn_err(CE_WARN,
2759 				    "nfs_dlboot: TCP protocol unavailable");
2760 				return (EINVAL);
2761 			}
2762 			/*
2763 			 * They can only have requested UDP
2764 			 */
2765 			if (strcmp(val, NC_UDP) != 0) {
2766 				cmn_err(CE_WARN,
2767 				    "nfs_dlboot: unknown protocol");
2768 				return (EINVAL);
2769 			}
2770 			*dl_cf = &dl_udp_netconf;
2771 			break;
2772 		case OPT_NOPRINT:
2773 			args->flags |= NFSMNT_NOPRINT;
2774 			break;
2775 		case OPT_NOLARGEFILES:
2776 			cmn_err(CE_WARN,
2777 			    "nfs_dlboot: NFS can't support nolargefiles");
2778 			break;
2779 		case OPT_SEC:
2780 			cmn_err(CE_WARN,
2781 			    "nfs_dlboot: root mounted auth_unix, sec ignored");
2782 			break;
2783 
2784 		case OPT_DIRECTIO:
2785 			args->flags |= NFSMNT_DIRECTIO;
2786 			break;
2787 
2788 		case OPT_NODIRECTIO:
2789 			args->flags &= ~(NFSMNT_DIRECTIO);
2790 			break;
2791 
2792 		default:
2793 			cmn_err(CE_WARN,
2794 			    "nfs_dlboot: ignoring invalid option \"%s\"", val);
2795 			break;
2796 		}
2797 	}
2798 sanity:
2799 	/*
2800 	 * Set some sane limits on read size
2801 	 */
2802 	if (!(args->flags & NFSMNT_RSIZE) || args->rsize == 0) {
2803 		/*
2804 		 * Establish defaults
2805 		 */
2806 		args->flags |= NFSMNT_RSIZE;
2807 		if (version == NFS_V4)
2808 			args->rsize = nfs4_root_rsize;
2809 		else
2810 			args->rsize = nfs_root_rsize;
2811 		return (0);
2812 	}
2813 	/*
2814 	 * No less than 512 bytes, otherwise it will take forever to boot
2815 	 */
2816 	if (args->rsize < 512)
2817 		args->rsize = 512;
2818 	/*
2819 	 * If we are running over UDP, we cannot exceed 64KB, trim
2820 	 * to 56KB to allow room for headers.
2821 	 */
2822 	if (*dl_cf == &dl_udp_netconf && args->rsize > (56 * 1024))
2823 		args->rsize = 56 * 1024;
2824 	return (0);
2825 }
2826