xref: /netbsd-src/sys/nfs/nfs_boot.c (revision 481d3881954fd794ca5f2d880b68c53a5db8620e)
1 /*	$NetBSD: nfs_boot.c,v 1.90 2024/07/05 04:31:54 rin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass and Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Support for NFS diskless booting, specifically getting information
34  * about where to mount root from, what pathnames, etc.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_boot.c,v 1.90 2024/07/05 04:31:54 rin Exp $");
39 
40 #ifdef _KERNEL_OPT
41 #include "opt_nfs.h"
42 #include "opt_tftproot.h"
43 #include "opt_nfs_boot.h"
44 #endif
45 
46 #ifdef NFS_BOOT_TCP
47 #undef NFS_BOOT_UDP
48 #endif
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #include <sys/proc.h>
56 #include <sys/mount.h>
57 #include <sys/mbuf.h>
58 #include <sys/reboot.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 
62 #include <net/if.h>
63 #include <net/route.h>
64 #include <net/if_ether.h>
65 #include <net/if_types.h>
66 
67 #include <netinet/in.h>
68 #include <netinet/if_inarp.h>
69 
70 #include <nfs/rpcv2.h>
71 #include <nfs/krpc.h>
72 #include <nfs/xdr_subs.h>
73 
74 #include <nfs/nfsproto.h>
75 #include <nfs/nfs.h>
76 #include <nfs/nfsmount.h>
77 #include <nfs/nfsdiskless.h>
78 
79 /*
80  * There are three implementations of NFS diskless boot.
81  * One implementation uses BOOTP (RFC951, RFC1048),
82  * Sun RPC/bootparams or static configuration.  See the
83  * files:
84  *    nfs_bootdhcp.c:   BOOTP (RFC951, RFC1048)
85  *    nfs_bootparam.c:  Sun RPC/bootparams
86  *    nfs_bootstatic.c: honour config(1) description
87  */
88 #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
89 int nfs_boot_rfc951 = 1; /* BOOTP enabled (default) */
90 #endif
91 #ifdef NFS_BOOT_BOOTPARAM
92 int nfs_boot_bootparam = 1; /* BOOTPARAM enabled (default) */
93 #endif
94 #ifdef NFS_BOOT_BOOTSTATIC
95 int nfs_boot_bootstatic = 1; /* BOOTSTATIC enabled (default) */
96 #endif
97 
98 #define IP_MIN_MTU 576
99 
100 /* mountd RPC */
101 static int md_mount(struct sockaddr_in *mdsin, char *path,
102 	struct nfs_args *argp, struct lwp *l);
103 
104 static int nfs_boot_delroute_matcher(struct rtentry *, void *);
105 static void nfs_boot_defrt(struct in_addr *);
106 static  int nfs_boot_getfh(struct nfs_dlmount *ndm, struct lwp *);
107 
108 
109 /*
110  * Called with an empty nfs_diskless struct to be filled in.
111  * Find an interface, determine its ip address (etc.) and
112  * save all the boot parameters in the nfs_diskless struct.
113  */
114 int
nfs_boot_init(struct nfs_diskless * nd,struct lwp * lwp)115 nfs_boot_init(struct nfs_diskless *nd, struct lwp *lwp)
116 {
117 	struct ifnet *ifp;
118 	int error = 0;
119 	int flags __unused;
120 
121 	/* Explicitly necessary or build fails
122 	 * due to unused variable, otherwise.
123 	 */
124 	flags = 0;
125 
126 	/*
127 	 * Find the network interface.
128 	 */
129 	ifp = ifunit(device_xname(root_device));
130 	if (ifp == NULL) {
131 		printf("nfs_boot: '%s' not found\n",
132 		       device_xname(root_device));
133 		return (ENXIO);
134 	}
135 	nd->nd_ifp = ifp;
136 
137 	error = EADDRNOTAVAIL; /* ??? */
138 #if defined(NFS_BOOT_BOOTSTATIC)
139 	if (error && nfs_boot_bootstatic) {
140 		printf("nfs_boot: trying static\n");
141 		error = nfs_bootstatic(nd, lwp, &flags);
142 	}
143 #endif
144 #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
145 	if (error && nfs_boot_rfc951) {
146 #if defined(NFS_BOOT_DHCP)
147 		printf("nfs_boot: trying DHCP/BOOTP\n");
148 #else
149 		printf("nfs_boot: trying BOOTP\n");
150 #endif
151 		error = nfs_bootdhcp(nd, lwp, &flags);
152 	}
153 #endif
154 #ifdef NFS_BOOT_BOOTPARAM
155 	if (error && nfs_boot_bootparam) {
156 		printf("nfs_boot: trying RARP (and RPC/bootparam)\n");
157 		error = nfs_bootparam(nd, lwp, &flags);
158 	}
159 #endif
160 	if (error)
161 		return (error);
162 
163 	/*
164 	 * Set MTU if passed
165 	 */
166 	if (nd->nd_mtu >= IP_MIN_MTU )
167 		nfs_boot_setmtu(nd->nd_ifp, nd->nd_mtu, lwp);
168 
169 	/*
170 	 * If the gateway address is set, add a default route.
171 	 * (The mountd RPCs may go across a gateway.)
172 	 */
173 	if (nd->nd_gwip.s_addr)
174 		nfs_boot_defrt(&nd->nd_gwip);
175 
176 #ifdef TFTPROOT
177 	if (nd->nd_nomount)
178 		goto out;
179 #endif
180 	/*
181 	 * Now fetch the NFS file handles as appropriate.
182 	 */
183 	error = nfs_boot_getfh(&nd->nd_root, lwp);
184 
185 	if (error)
186 		nfs_boot_cleanup(nd, lwp);
187 
188 #ifdef TFTPROOT
189 out:
190 #endif
191 	return (error);
192 }
193 
194 void
nfs_boot_cleanup(struct nfs_diskless * nd,struct lwp * lwp)195 nfs_boot_cleanup(struct nfs_diskless *nd, struct lwp *lwp)
196 {
197 
198 	nfs_boot_deladdress(nd->nd_ifp, lwp, nd->nd_myip.s_addr);
199 	nfs_boot_ifupdown(nd->nd_ifp, lwp, 0);
200 	nfs_boot_flushrt(nd->nd_ifp);
201 }
202 
203 int
nfs_boot_ifupdown(struct ifnet * ifp,struct lwp * lwp,int up)204 nfs_boot_ifupdown(struct ifnet *ifp, struct lwp *lwp, int up)
205 {
206 	struct socket *so;
207 	struct ifreq ireq;
208 	int error;
209 
210 	memset(&ireq, 0, sizeof(ireq));
211 	memcpy(ireq.ifr_name, ifp->if_xname, IFNAMSIZ);
212 
213 	/*
214 	 * Get a socket to use for various things in here.
215 	 * After this, use "goto out" to cleanup and return.
216 	 */
217 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
218 	if (error) {
219 		printf("ifupdown: socreate, error=%d\n", error);
220 		return (error);
221 	}
222 
223 	/*
224 	 * Bring up the interface. (just set the "up" flag)
225 	 * Get the old interface flags and or IFF_UP into them so
226 	 * things like media selection flags are not clobbered.
227 	 */
228 	error = ifioctl(so, SIOCGIFFLAGS, (void *)&ireq, lwp);
229 	if (error) {
230 		printf("ifupdown: GIFFLAGS, error=%d\n", error);
231 		goto out;
232 	}
233 	if (up)
234 		ireq.ifr_flags |= IFF_UP;
235 	else
236 		ireq.ifr_flags &= ~IFF_UP;
237 	error = ifioctl(so, SIOCSIFFLAGS, &ireq, lwp);
238 	if (error) {
239 		printf("ifupdown: SIFFLAGS, error=%d\n", error);
240 		goto out;
241 	}
242 
243 	if (up)
244 		/* give the link some time to get up */
245 		tsleep(nfs_boot_ifupdown, PZERO, "nfsbif", 3 * hz);
246 out:
247 	soclose(so);
248 	return (error);
249 }
250 
251 void
nfs_boot_setmtu(struct ifnet * ifp,int mtu,struct lwp * lwp)252 nfs_boot_setmtu(struct ifnet *ifp, int mtu, struct lwp *lwp)
253 {
254 	struct socket *so;
255 	struct ifreq ireq;
256 	int error;
257 
258 	memset(&ireq, 0, sizeof(ireq));
259 	memcpy(ireq.ifr_name, ifp->if_xname, IFNAMSIZ);
260 
261 	/*
262 	 * Get a socket to use for various things in here.
263 	 * After this, use "goto out" to cleanup and return.
264 	 */
265 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
266 	if (error) {
267 		printf("setmtu: socreate, error=%d\n", error);
268 		return;
269 	}
270 
271 	/*
272 	 * Get structure, set the new MTU, push structure.
273 	 */
274 	error = ifioctl(so, SIOCGIFMTU, (void *)&ireq, lwp);
275 	if (error) {
276 		printf("setmtu: GIFMTU, error=%d\n", error);
277 		goto out;
278 	}
279 
280 	ireq.ifr_mtu = mtu;
281 
282 	error = ifioctl(so, SIOCSIFMTU, &ireq, lwp);
283 	if (error) {
284 		printf("setmtu: SIFMTU, error=%d\n", error);
285 		goto out;
286 	}
287 
288 out:
289 	soclose(so);
290 	return;
291 }
292 
293 int
nfs_boot_setaddress(struct ifnet * ifp,struct lwp * lwp,uint32_t addr,uint32_t netmask,uint32_t braddr)294 nfs_boot_setaddress(struct ifnet *ifp, struct lwp *lwp,
295 		uint32_t addr, uint32_t netmask, uint32_t braddr)
296 {
297 	struct socket *so;
298 	struct ifaliasreq iareq;
299 	struct sockaddr_in *sin;
300 	int error;
301 
302 	/*
303 	 * Get a socket to use for various things in here.
304 	 * After this, use "goto out" to cleanup and return.
305 	 */
306 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
307 	if (error) {
308 		printf("setaddress: socreate, error=%d\n", error);
309 		return (error);
310 	}
311 
312 	memset(&iareq, 0, sizeof(iareq));
313 	memcpy(iareq.ifra_name, ifp->if_xname, IFNAMSIZ);
314 
315 	/* Set the I/F address */
316 	sin = (struct sockaddr_in *)&iareq.ifra_addr;
317 	sin->sin_len = sizeof(*sin);
318 	sin->sin_family = AF_INET;
319 	sin->sin_addr.s_addr = addr;
320 
321 	/* Set the netmask */
322 	if (netmask != INADDR_ANY) {
323 		sin = (struct sockaddr_in *)&iareq.ifra_mask;
324 		sin->sin_len = sizeof(*sin);
325 		sin->sin_family = AF_INET;
326 		sin->sin_addr.s_addr = netmask;
327 	} /* else leave subnetmask unspecified (len=0) */
328 
329 	/* Set the broadcast addr. */
330 	if (braddr != INADDR_ANY) {
331 		sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
332 		sin->sin_len = sizeof(*sin);
333 		sin->sin_family = AF_INET;
334 		sin->sin_addr.s_addr = braddr;
335 	} /* else leave broadcast addr unspecified (len=0) */
336 
337 	error = ifioctl(so, SIOCAIFADDR, (void *)&iareq, lwp);
338 	if (error) {
339 		printf("setaddress, error=%d\n", error);
340 		goto out;
341 	}
342 
343 	/* give the link some time to get up */
344 	tsleep(nfs_boot_setaddress, PZERO, "nfsbtd", 3 * hz);
345 out:
346 	soclose(so);
347 	return (error);
348 }
349 
350 int
nfs_boot_deladdress(struct ifnet * ifp,struct lwp * lwp,uint32_t addr)351 nfs_boot_deladdress(struct ifnet *ifp, struct lwp *lwp, uint32_t addr)
352 {
353 	struct socket *so;
354 	struct ifreq ifr;
355 	struct sockaddr_in sin;
356 	struct in_addr ia = {.s_addr = addr};
357 	int error;
358 
359 	/*
360 	 * Get a socket to use for various things in here.
361 	 * After this, use "goto out" to cleanup and return.
362 	 */
363 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
364 	if (error) {
365 		printf("deladdress: socreate, error=%d\n", error);
366 		return (error);
367 	}
368 
369 	memset(&ifr, 0, sizeof(ifr));
370 	memcpy(ifr.ifr_name, ifp->if_xname, IFNAMSIZ);
371 
372 	sockaddr_in_init(&sin, &ia, 0);
373 	ifreq_setaddr(SIOCDIFADDR, &ifr, sintocsa(&sin));
374 
375 	error = ifioctl(so, SIOCDIFADDR, &ifr, lwp);
376 	if (error) {
377 		printf("deladdress, error=%d\n", error);
378 		goto out;
379 	}
380 
381 out:
382 	soclose(so);
383 	return (error);
384 }
385 
386 int
nfs_boot_setrecvtimo(struct socket * so)387 nfs_boot_setrecvtimo(struct socket *so)
388 {
389 	struct timeval tv;
390 
391 	tv.tv_sec = 1;
392 	tv.tv_usec = 0;
393 
394 	return (so_setsockopt(NULL, so, SOL_SOCKET, SO_RCVTIMEO, &tv,
395 	    sizeof(tv)));
396 }
397 
398 int
nfs_boot_enbroadcast(struct socket * so)399 nfs_boot_enbroadcast(struct socket *so)
400 {
401 	int32_t on;
402 
403 	on = 1;
404 	return (so_setsockopt(NULL, so, SOL_SOCKET, SO_BROADCAST, &on,
405 	    sizeof(on)));
406 }
407 
408 int
nfs_boot_sobind_ipport(struct socket * so,uint16_t port,struct lwp * l)409 nfs_boot_sobind_ipport(struct socket *so, uint16_t port, struct lwp *l)
410 {
411 	struct sockaddr_in sin;
412 	int error;
413 
414 	sin.sin_len = sizeof(sin);
415 	sin.sin_family = AF_INET;
416 	sin.sin_addr.s_addr = INADDR_ANY;
417 	sin.sin_port = htons(port);
418 	error = sobind(so, (struct sockaddr *)&sin, l);
419 	return (error);
420 }
421 
422 /*
423  * What is the longest we will wait before re-sending a request?
424  * Note this is also the frequency of "timeout" messages.
425  * The re-send loop counts up linearly to this maximum, so the
426  * first complaint will happen after (1+2+3+4+5)=15 seconds.
427  */
428 #define	MAX_RESEND_DELAY 5	/* seconds */
429 #define TOTAL_TIMEOUT   30	/* seconds */
430 
431 int
nfs_boot_sendrecv(struct socket * so,struct sockaddr_in * nam,int (* sndproc)(struct mbuf *,void *,int),struct mbuf * snd,int (* rcvproc)(struct mbuf **,void *),struct mbuf ** rcv,struct mbuf ** from_p,void * context,struct lwp * lwp)432 nfs_boot_sendrecv(struct socket *so, struct sockaddr_in *nam,
433 		int (*sndproc)(struct mbuf *, void *, int),
434 		struct mbuf *snd,
435 		int (*rcvproc)(struct mbuf **, void *),
436 		struct mbuf **rcv, struct mbuf **from_p,
437 		void *context, struct lwp *lwp)
438 {
439 	int error, rcvflg, timo, secs, waited;
440 	struct mbuf *m, *from;
441 	struct uio uio;
442 
443 	/* Free at end if not null. */
444 	from = NULL;
445 
446 	/*
447 	 * Send it, repeatedly, until a reply is received,
448 	 * but delay each re-send by an increasing amount.
449 	 * If the delay hits the maximum, start complaining.
450 	 */
451 	waited = timo = 0;
452 send_again:
453 	waited += timo;
454 	if (waited >= TOTAL_TIMEOUT)
455 		return (ETIMEDOUT);
456 
457 	/* Determine new timeout. */
458 	if (timo < MAX_RESEND_DELAY)
459 		timo++;
460 	else
461 		printf("nfs_boot: timeout...\n");
462 
463 	if (sndproc) {
464 		error = (*sndproc)(snd, context, waited);
465 		if (error)
466 			goto out;
467 	}
468 
469 	/* Send request (or re-send). */
470 	m = m_copypacket(snd, M_WAIT);
471 	if (m == NULL) {
472 		error = ENOBUFS;
473 		goto out;
474 	}
475 	error = (*so->so_send)(so, (struct sockaddr *)nam, NULL,
476 	    m, NULL, 0, lwp);
477 	if (error) {
478 		printf("nfs_boot: sosend: %d\n", error);
479 		goto out;
480 	}
481 	m = NULL;
482 
483 	/*
484 	 * Wait for up to timo seconds for a reply.
485 	 * The socket receive timeout was set to 1 second.
486 	 */
487 
488 	secs = timo;
489 	for (;;) {
490 		m_freem(from);
491 		from = NULL;
492 		m_freem(m);
493 		m = NULL;
494 		uio.uio_resid = 1 << 16; /* ??? */
495 		rcvflg = 0;
496 		error = (*so->so_receive)(so, &from, &uio, &m, NULL, &rcvflg);
497 		if (error == EWOULDBLOCK) {
498 			if (--secs <= 0)
499 				goto send_again;
500 			continue;
501 		}
502 		if (error)
503 			goto out;
504 #ifdef DIAGNOSTIC
505 		if (!m || !(m->m_flags & M_PKTHDR)
506 		    || (1 << 16) - uio.uio_resid != m->m_pkthdr.len)
507 			panic("nfs_boot_sendrecv: return size");
508 #endif
509 
510 		if ((*rcvproc)(&m, context))
511 			continue;
512 
513 		if (rcv)
514 			*rcv = m;
515 		else
516 			m_freem(m);
517 		if (from_p) {
518 			*from_p = from;
519 			from = NULL;
520 		}
521 		break;
522 	}
523 out:
524 	m_freem(from);
525 	return (error);
526 }
527 
528 /*
529  * Install a default route to the passed IP address.
530  */
531 static void
nfs_boot_defrt(struct in_addr * gw_ip)532 nfs_boot_defrt(struct in_addr *gw_ip)
533 {
534 	struct sockaddr dst, gw, mask;
535 	struct sockaddr_in *sin;
536 	int error;
537 
538 	/* Destination: (default) */
539 	memset((void *)&dst, 0, sizeof(dst));
540 	dst.sa_len = sizeof(dst);
541 	dst.sa_family = AF_INET;
542 	/* Gateway: */
543 	memset((void *)&gw, 0, sizeof(gw));
544 	sin = (struct sockaddr_in *)&gw;
545 	sin->sin_len = sizeof(*sin);
546 	sin->sin_family = AF_INET;
547 	sin->sin_addr.s_addr = gw_ip->s_addr;
548 	/* Mask: (zero length) */
549 	/* XXX - Just pass a null pointer? */
550 	memset(&mask, 0, sizeof(mask));
551 
552 	/* add, dest, gw, mask, flags, 0 */
553 	error = rtrequest(RTM_ADD, &dst, &gw, &mask,
554 			  (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
555 	if (error) {
556 		printf("nfs_boot: add route, error=%d\n", error);
557 		error = 0;
558 	}
559 }
560 
561 static int
nfs_boot_delroute_matcher(struct rtentry * rt,void * w)562 nfs_boot_delroute_matcher(struct rtentry *rt, void *w)
563 {
564 
565 	if ((void *)rt->rt_ifp != w)
566 		return 0;
567 
568 	return 1;
569 }
570 
571 void
nfs_boot_flushrt(struct ifnet * ifp)572 nfs_boot_flushrt(struct ifnet *ifp)
573 {
574 
575 	rt_delete_matched_entries(AF_INET, nfs_boot_delroute_matcher, ifp, false);
576 }
577 
578 /*
579  * Get an initial NFS file handle using Sun RPC/mountd.
580  * Separate function because we used to call it twice.
581  * (once for root and once for swap)
582  *
583  * ndm  output
584  */
585 static int
nfs_boot_getfh(struct nfs_dlmount * ndm,struct lwp * l)586 nfs_boot_getfh(struct nfs_dlmount *ndm, struct lwp *l)
587 {
588 	struct nfs_args *args;
589 	struct sockaddr_in *sin;
590 	char *pathname;
591 	int error;
592 	u_int16_t port;
593 
594 	args = &ndm->ndm_args;
595 
596 	/* Initialize mount args. */
597 	memset((void *) args, 0, sizeof(*args));
598 	args->addr     = &ndm->ndm_saddr;
599 	args->addrlen  = args->addr->sa_len;
600 #ifdef NFS_BOOT_UDP
601 	args->sotype   = SOCK_DGRAM;
602 #else
603 	args->sotype   = SOCK_STREAM;
604 #endif
605 	args->fh       = ndm->ndm_fh;
606 	args->hostname = ndm->ndm_host;
607 	args->flags    = NFSMNT_NOCONN | NFSMNT_RESVPORT;
608 
609 #ifndef NFS_V2_ONLY
610 	args->flags    |= NFSMNT_NFSV3;
611 #endif
612 #ifdef	NFS_BOOT_OPTIONS
613 	args->flags    |= NFS_BOOT_OPTIONS;
614 #endif
615 #ifdef	NFS_BOOT_RWSIZE
616 	/*
617 	 * Reduce rsize,wsize for interfaces that consistently
618 	 * drop fragments of long UDP messages.  (i.e. wd8003).
619 	 * You can always change these later via remount.
620 	 */
621 	args->flags   |= NFSMNT_WSIZE | NFSMNT_RSIZE;
622 	args->wsize    = NFS_BOOT_RWSIZE;
623 	args->rsize    = NFS_BOOT_RWSIZE;
624 #endif
625 
626 	/*
627 	 * Find the pathname part of the "server:pathname"
628 	 * string left in ndm->ndm_host by nfs_boot_init.
629 	 */
630 	pathname = strchr(ndm->ndm_host, ':');
631 	if (pathname == 0) {
632 		printf("nfs_boot: getfh - no pathname\n");
633 		return (EIO);
634 	}
635 	pathname++;
636 
637 	/*
638 	 * Get file handle using RPC to mountd/mount
639 	 */
640 	sin = (struct sockaddr_in *)&ndm->ndm_saddr;
641 	error = md_mount(sin, pathname, args, l);
642 	if (error) {
643 		printf("nfs_boot: mountd `%s', error=%d\n",
644 		       ndm->ndm_host, error);
645 		return (error);
646 	}
647 
648 	/* Set port number for NFS use. */
649 	/* XXX: NFS port is always 2049, right? */
650 retry:
651 	error = krpc_portmap(sin, NFS_PROG,
652 		    (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
653 		    (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
654 		    &port, l);
655 	if (port == htons(0))
656 		error = EIO;
657 	if (error) {
658 		if (args->sotype == SOCK_STREAM) {
659 			args->sotype = SOCK_DGRAM;
660 			goto retry;
661 		}
662 		printf("nfs_boot: portmap NFS, error=%d\n", error);
663 		return (error);
664 	}
665 	sin->sin_port = port;
666 	return (0);
667 }
668 
669 
670 /*
671  * RPC: mountd/mount
672  * Given a server pathname, get an NFS file handle.
673  * Also, sets sin->sin_port to the NFS service port.
674  *
675  * mdsin   mountd server address
676  */
677 static int
md_mount(struct sockaddr_in * mdsin,char * path,struct nfs_args * argp,struct lwp * lwp)678 md_mount(struct sockaddr_in *mdsin, char *path,
679 	 struct nfs_args *argp, struct lwp *lwp)
680 {
681 	/* The RPC structures */
682 	struct rdata {
683 		u_int32_t errno;
684 		union {
685 			u_int8_t  v2fh[NFSX_V2FH];
686 			struct {
687 				u_int32_t fhlen;
688 				u_int8_t  fh[1];
689 			} v3fh;
690 		} fh;
691 	} *rdata;
692 	struct mbuf *m;
693 	u_int8_t *fh;
694 	int minlen, error;
695 	int mntver;
696 
697 	mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2;
698 	do {
699 		/*
700 		 * Get port number for MOUNTD.
701 		 */
702 		error = krpc_portmap(mdsin, RPCPROG_MNT, mntver,
703 		                    IPPROTO_UDP, &mdsin->sin_port, lwp);
704 		if (error)
705 			continue;
706 
707 		/* This mbuf is consumed by krpc_call. */
708 		m = xdr_string_encode(path, strlen(path));
709 		if (m == NULL)
710 			return ENOMEM;
711 
712 		/* Do RPC to mountd. */
713 		error = krpc_call(mdsin, RPCPROG_MNT, mntver,
714 		                  RPCMNT_MOUNT, &m, NULL, lwp);
715 		if (error != EPROGMISMATCH)
716 			break;
717 		/* Try lower version of mountd. */
718 	} while (--mntver >= 1);
719 	if (error) {
720 		printf("nfs_boot: mountd error=%d\n", error);
721 		return error;
722 	}
723 	if (mntver != 3)
724 		argp->flags &= ~NFSMNT_NFSV3;
725 
726 	/* The reply might have only the errno. */
727 	if (m->m_len < 4)
728 		goto bad;
729 	/* Have at least errno, so check that. */
730 	rdata = mtod(m, struct rdata *);
731 	error = fxdr_unsigned(u_int32_t, rdata->errno);
732 	if (error)
733 		goto out;
734 
735 	/* Have errno==0, so the fh must be there. */
736 	if (mntver == 3) {
737 		argp->fhsize   = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
738 		if (argp->fhsize > NFSX_V3FHMAX)
739 			goto bad;
740 		minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
741 	} else {
742 		argp->fhsize   = NFSX_V2FH;
743 		minlen = sizeof(u_int32_t) + argp->fhsize;
744 	}
745 
746 	if (m->m_len < minlen) {
747 		m = m_pullup(m, minlen);
748 		if (m == NULL)
749 			return(EBADRPC);
750 		rdata = mtod(m, struct rdata *);
751 	}
752 
753 	fh = (mntver == 3) ?
754 		rdata->fh.v3fh.fh : rdata->fh.v2fh;
755 	memcpy(argp->fh, fh, argp->fhsize);
756 
757 	goto out;
758 
759 bad:
760 	error = EBADRPC;
761 
762 out:
763 	m_freem(m);
764 	return error;
765 }
766