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