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