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