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