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