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