Home | History | Annotate | Line # | Download | only in nfs
nfs_boot.c revision 1.43
      1  1.43       cgd /*	$NetBSD: nfs_boot.c,v 1.43 1998/02/28 01:45:01 cgd Exp $	*/
      2   1.5       cgd 
      3  1.35       gwr /*-
      4  1.35       gwr  * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
      5   1.1     glass  * All rights reserved.
      6   1.1     glass  *
      7  1.35       gwr  * This code is derived from software contributed to The NetBSD Foundation
      8  1.35       gwr  * by Adam Glass and Gordon W. Ross.
      9  1.35       gwr  *
     10   1.1     glass  * Redistribution and use in source and binary forms, with or without
     11   1.1     glass  * modification, are permitted provided that the following conditions
     12   1.1     glass  * are met:
     13   1.1     glass  * 1. Redistributions of source code must retain the above copyright
     14   1.1     glass  *    notice, this list of conditions and the following disclaimer.
     15   1.1     glass  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     glass  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     glass  *    documentation and/or other materials provided with the distribution.
     18  1.35       gwr  * 3. All advertising materials mentioning features or use of this software
     19  1.35       gwr  *    must display the following acknowledgement:
     20  1.35       gwr  *        This product includes software developed by the NetBSD
     21  1.35       gwr  *        Foundation, Inc. and its contributors.
     22  1.35       gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.35       gwr  *    contributors may be used to endorse or promote products derived
     24  1.35       gwr  *    from this software without specific prior written permission.
     25   1.1     glass  *
     26  1.35       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.35       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.35       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.35       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.35       gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.35       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.35       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.35       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.35       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.35       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.35       gwr  * POSSIBILITY OF SUCH DAMAGE.
     37  1.35       gwr  */
     38  1.35       gwr 
     39  1.35       gwr /*
     40  1.35       gwr  * Support for NFS diskless booting, specifically getting information
     41  1.35       gwr  * about where to mount root from, what pathnames, etc.
     42   1.1     glass  */
     43   1.1     glass 
     44  1.42    scottr #include "opt_nfs_boot.h"
     45  1.42    scottr 
     46   1.2       cgd #include <sys/param.h>
     47   1.1     glass #include <sys/systm.h>
     48   1.1     glass #include <sys/kernel.h>
     49   1.1     glass #include <sys/conf.h>
     50  1.30   thorpej #include <sys/device.h>
     51   1.1     glass #include <sys/ioctl.h>
     52   1.1     glass #include <sys/proc.h>
     53   1.1     glass #include <sys/mount.h>
     54   1.1     glass #include <sys/mbuf.h>
     55  1.16       gwr #include <sys/reboot.h>
     56   1.1     glass #include <sys/socket.h>
     57  1.16       gwr #include <sys/socketvar.h>
     58   1.1     glass 
     59   1.1     glass #include <net/if.h>
     60   1.3       gwr #include <net/route.h>
     61  1.31        is #include <net/if_ether.h>
     62  1.35       gwr #include <net/if_types.h>
     63  1.31        is 
     64   1.1     glass #include <netinet/in.h>
     65  1.31        is #include <netinet/if_inarp.h>
     66   1.1     glass 
     67   1.1     glass #include <nfs/rpcv2.h>
     68  1.37       gwr #include <nfs/krpc.h>
     69  1.37       gwr #include <nfs/xdr_subs.h>
     70  1.37       gwr 
     71  1.25      fvdl #include <nfs/nfsproto.h>
     72   1.1     glass #include <nfs/nfsdiskless.h>
     73   1.7    paulus 
     74   1.1     glass /*
     75  1.35       gwr  * There are two implementations of NFS diskless boot.
     76  1.35       gwr  * One implementation uses BOOTP (RFC951, RFC1048),
     77  1.35       gwr  * the other uses Sun RPC/bootparams.  See the files:
     78  1.35       gwr  *    nfs_bootp.c:   BOOTP (RFC951, RFC1048)
     79  1.35       gwr  *    nfs_bootsun.c: Sun RPC/bootparams
     80   1.1     glass  */
     81  1.41    scottr #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
     82  1.40  drochner int nfs_boot_rfc951 = 1; /* BOOTP enabled (default) */
     83  1.40  drochner #endif
     84  1.40  drochner #ifdef NFS_BOOT_BOOTPARAM
     85  1.40  drochner int nfs_boot_bootparam = 1; /* BOOTPARAM enabled (default) */
     86  1.40  drochner #endif
     87   1.3       gwr 
     88   1.3       gwr /* mountd RPC */
     89   1.6   deraadt static int md_mount __P((struct sockaddr_in *mdsin, char *path,
     90  1.29      fvdl 	struct nfs_args *argp));
     91   1.3       gwr 
     92  1.35       gwr static void nfs_boot_defrt __P((struct in_addr *));
     93  1.37       gwr static  int nfs_boot_getfh __P((struct nfs_dlmount *ndm));
     94  1.35       gwr 
     95  1.35       gwr 
     96   1.1     glass /*
     97   1.3       gwr  * Called with an empty nfs_diskless struct to be filled in.
     98  1.35       gwr  * Find an interface, determine its ip address (etc.) and
     99  1.35       gwr  * save all the boot parameters in the nfs_diskless struct.
    100   1.1     glass  */
    101   1.6   deraadt int
    102   1.6   deraadt nfs_boot_init(nd, procp)
    103   1.3       gwr 	struct nfs_diskless *nd;
    104   1.3       gwr 	struct proc *procp;
    105   1.1     glass {
    106   1.3       gwr 	struct ifnet *ifp;
    107  1.16       gwr 	int error;
    108   1.3       gwr 
    109   1.3       gwr 	/*
    110  1.35       gwr 	 * Find the network interface.
    111   1.3       gwr 	 */
    112  1.30   thorpej 	ifp = ifunit(root_device->dv_xname);
    113  1.30   thorpej 	if (ifp == NULL) {
    114  1.35       gwr 		printf("nfs_boot: '%s' not found\n",
    115  1.35       gwr 		       root_device->dv_xname);
    116  1.30   thorpej 		return (ENXIO);
    117  1.30   thorpej 	}
    118   1.3       gwr 
    119  1.40  drochner 	error = EADDRNOTAVAIL; /* ??? */
    120  1.41    scottr #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
    121  1.35       gwr 	if (nfs_boot_rfc951) {
    122  1.43       cgd #if defined(NFS_BOOT_DHCP)
    123  1.43       cgd 		printf("nfs_boot: trying DHCP/BOOTP\n");
    124  1.43       cgd #else
    125  1.43       cgd 		printf("nfs_boot: trying BOOTP\n");
    126  1.43       cgd #endif
    127  1.35       gwr 		error = nfs_bootdhcp(ifp, nd, procp);
    128  1.40  drochner 		if (!error)
    129  1.40  drochner 			goto ok;
    130  1.40  drochner 	}
    131  1.40  drochner #endif
    132  1.40  drochner #ifdef NFS_BOOT_BOOTPARAM
    133  1.40  drochner 	if (nfs_boot_bootparam) {
    134  1.35       gwr 		printf("nfs_boot: trying RARP (and RPC/bootparam)\n");
    135  1.35       gwr 		error = nfs_bootparam(ifp, nd, procp);
    136  1.40  drochner 		if (!error)
    137  1.40  drochner 			goto ok;
    138  1.30   thorpej 	}
    139  1.40  drochner #endif
    140  1.40  drochner 	if (0) goto ok; /* XXX stupid gcc */
    141  1.40  drochner 	return (error);
    142   1.3       gwr 
    143  1.40  drochner ok:
    144   1.3       gwr 	/*
    145   1.3       gwr 	 * If the gateway address is set, add a default route.
    146   1.3       gwr 	 * (The mountd RPCs may go across a gateway.)
    147   1.1     glass 	 */
    148  1.35       gwr 	if (nd->nd_gwip.s_addr)
    149  1.35       gwr 		nfs_boot_defrt(&nd->nd_gwip);
    150   1.3       gwr 
    151  1.37       gwr 	/*
    152  1.37       gwr 	 * Now fetch the NFS file handles as appropriate.
    153  1.37       gwr 	 */
    154  1.37       gwr 	error = nfs_boot_getfh(&nd->nd_root);
    155  1.37       gwr 	if (error)
    156  1.37       gwr 		return (error);
    157  1.37       gwr 
    158  1.37       gwr 	return (error);
    159  1.39  drochner }
    160  1.39  drochner 
    161  1.39  drochner int nfs_boot_setrecvtimo(so)
    162  1.39  drochner struct socket *so;
    163  1.39  drochner {
    164  1.39  drochner 	struct mbuf *m;
    165  1.39  drochner 	struct timeval *tv;
    166  1.39  drochner 
    167  1.39  drochner 	m = m_get(M_WAIT, MT_SOOPTS);
    168  1.39  drochner 	tv = mtod(m, struct timeval *);
    169  1.39  drochner 	m->m_len = sizeof(*tv);
    170  1.39  drochner 	tv->tv_sec = 1;
    171  1.39  drochner 	tv->tv_usec = 0;
    172  1.39  drochner 	return(sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m));
    173  1.39  drochner }
    174  1.39  drochner 
    175  1.39  drochner int nfs_boot_enbroadcast(so)
    176  1.39  drochner struct socket *so;
    177  1.39  drochner {
    178  1.39  drochner 	struct mbuf *m;
    179  1.39  drochner 	int32_t *on;
    180  1.39  drochner 
    181  1.39  drochner 	m = m_get(M_WAIT, MT_SOOPTS);
    182  1.39  drochner 	on = mtod(m, int32_t *);
    183  1.39  drochner 	m->m_len = sizeof(*on);
    184  1.39  drochner 	*on = 1;
    185  1.39  drochner 	return(sosetopt(so, SOL_SOCKET, SO_BROADCAST, m));
    186  1.39  drochner }
    187  1.39  drochner 
    188  1.39  drochner int nfs_boot_sobind_ipport(so, port)
    189  1.39  drochner struct socket *so;
    190  1.39  drochner u_int16_t port;
    191  1.39  drochner {
    192  1.39  drochner 	struct mbuf *m;
    193  1.39  drochner 	struct sockaddr_in *sin;
    194  1.39  drochner 	int error;
    195  1.39  drochner 
    196  1.39  drochner 	m = m_getclr(M_WAIT, MT_SONAME);
    197  1.39  drochner 	sin = mtod(m, struct sockaddr_in *);
    198  1.39  drochner 	sin->sin_len = m->m_len = sizeof(*sin);
    199  1.39  drochner 	sin->sin_family = AF_INET;
    200  1.39  drochner 	sin->sin_addr.s_addr = INADDR_ANY;
    201  1.39  drochner 	sin->sin_port = htons(port);
    202  1.39  drochner 	error = sobind(so, m);
    203  1.39  drochner 	m_freem(m);
    204  1.39  drochner 	return(error);
    205  1.39  drochner }
    206  1.39  drochner 
    207  1.39  drochner /*
    208  1.39  drochner  * What is the longest we will wait before re-sending a request?
    209  1.39  drochner  * Note this is also the frequency of "timeout" messages.
    210  1.39  drochner  * The re-send loop counts up linearly to this maximum, so the
    211  1.39  drochner  * first complaint will happen after (1+2+3+4+5)=15 seconds.
    212  1.39  drochner  */
    213  1.39  drochner #define	MAX_RESEND_DELAY 5	/* seconds */
    214  1.39  drochner #define TOTAL_TIMEOUT   30	/* seconds */
    215  1.39  drochner 
    216  1.39  drochner int nfs_boot_sendrecv(so, nam, sndproc, snd, rcvproc, rcv,
    217  1.39  drochner 		      from_p, context)
    218  1.39  drochner struct socket *so;
    219  1.39  drochner struct mbuf *nam;
    220  1.39  drochner int (*sndproc) __P((struct mbuf*, void*, int));
    221  1.39  drochner struct mbuf *snd;
    222  1.39  drochner int (*rcvproc) __P((struct mbuf*, void*));
    223  1.39  drochner struct mbuf **rcv, **from_p;
    224  1.39  drochner void *context;
    225  1.39  drochner {
    226  1.39  drochner 	int error, rcvflg, timo, secs, waited;
    227  1.39  drochner 	struct mbuf *m, *from;
    228  1.39  drochner 	struct uio uio;
    229  1.39  drochner 
    230  1.39  drochner 	/* Free at end if not null. */
    231  1.39  drochner 	from = NULL;
    232  1.39  drochner 
    233  1.39  drochner 	/*
    234  1.39  drochner 	 * Send it, repeatedly, until a reply is received,
    235  1.39  drochner 	 * but delay each re-send by an increasing amount.
    236  1.39  drochner 	 * If the delay hits the maximum, start complaining.
    237  1.39  drochner 	 */
    238  1.39  drochner 	waited = timo = 0;
    239  1.39  drochner send_again:
    240  1.39  drochner 	waited += timo;
    241  1.39  drochner 	if (waited >= TOTAL_TIMEOUT)
    242  1.39  drochner 		return(ETIMEDOUT);
    243  1.39  drochner 
    244  1.39  drochner 	/* Determine new timeout. */
    245  1.39  drochner 	if (timo < MAX_RESEND_DELAY)
    246  1.39  drochner 		timo++;
    247  1.39  drochner 	else
    248  1.39  drochner 		printf("nfs_boot: timeout...\n");
    249  1.39  drochner 
    250  1.39  drochner 	if (sndproc) {
    251  1.39  drochner 		error = (*sndproc)(snd, context, waited);
    252  1.39  drochner 		if (error)
    253  1.39  drochner 			goto out;
    254  1.39  drochner 	}
    255  1.39  drochner 
    256  1.39  drochner 	/* Send request (or re-send). */
    257  1.39  drochner 	m = m_copypacket(snd, M_WAIT);
    258  1.39  drochner 	if (m == NULL) {
    259  1.39  drochner 		error = ENOBUFS;
    260  1.39  drochner 		goto out;
    261  1.39  drochner 	}
    262  1.39  drochner 	error = sosend(so, nam, NULL, m, NULL, 0);
    263  1.39  drochner 	if (error) {
    264  1.39  drochner 		printf("nfs_boot: sosend: %d\n", error);
    265  1.39  drochner 		goto out;
    266  1.39  drochner 	}
    267  1.39  drochner 	m = NULL;
    268  1.39  drochner 
    269  1.39  drochner 	/*
    270  1.39  drochner 	 * Wait for up to timo seconds for a reply.
    271  1.39  drochner 	 * The socket receive timeout was set to 1 second.
    272  1.39  drochner 	 */
    273  1.39  drochner 
    274  1.39  drochner 	secs = timo;
    275  1.39  drochner 	for (;;) {
    276  1.39  drochner 		if (from) {
    277  1.39  drochner 			m_freem(from);
    278  1.39  drochner 			from = NULL;
    279  1.39  drochner 		}
    280  1.39  drochner 		if (m) {
    281  1.39  drochner 			m_freem(m);
    282  1.39  drochner 			m = NULL;
    283  1.39  drochner 		}
    284  1.39  drochner 		uio.uio_resid = 1 << 16; /* ??? */
    285  1.39  drochner 		rcvflg = 0;
    286  1.39  drochner 		error = soreceive(so, &from, &uio, &m, NULL, &rcvflg);
    287  1.39  drochner 		if (error == EWOULDBLOCK) {
    288  1.39  drochner 			if (--secs <= 0)
    289  1.39  drochner 				goto send_again;
    290  1.39  drochner 			continue;
    291  1.39  drochner 		}
    292  1.39  drochner 		if (error)
    293  1.39  drochner 			goto out;
    294  1.39  drochner #ifdef DIAGNOSTIC
    295  1.39  drochner 		if (!m || !(m->m_flags & M_PKTHDR)
    296  1.39  drochner 		    || (1 << 16) - uio.uio_resid != m->m_pkthdr.len)
    297  1.39  drochner 			panic("nfs_boot_sendrecv: return size");
    298  1.39  drochner #endif
    299  1.39  drochner 
    300  1.39  drochner 		if ((*rcvproc)(m, context))
    301  1.39  drochner 			continue;
    302  1.39  drochner 
    303  1.39  drochner 		if (rcv)
    304  1.39  drochner 			*rcv = m;
    305  1.39  drochner 		else
    306  1.39  drochner 			m_freem(m);
    307  1.39  drochner 		if (from_p) {
    308  1.39  drochner 			*from_p = from;
    309  1.39  drochner 			from = NULL;
    310  1.39  drochner 		}
    311  1.39  drochner 		break;
    312  1.39  drochner 	}
    313  1.39  drochner out:
    314  1.39  drochner 	if (from) m_freem(from);
    315  1.39  drochner 	return(error);
    316  1.35       gwr }
    317   1.1     glass 
    318  1.35       gwr /*
    319  1.35       gwr  * Install a default route to the passed IP address.
    320  1.35       gwr  */
    321  1.35       gwr static void
    322  1.35       gwr nfs_boot_defrt(gw_ip)
    323  1.35       gwr 	struct in_addr *gw_ip;
    324  1.35       gwr {
    325  1.35       gwr 	struct sockaddr dst, gw, mask;
    326  1.35       gwr 	struct sockaddr_in *sin;
    327  1.35       gwr 	int error;
    328  1.33       gwr 
    329  1.35       gwr 	/* Destination: (default) */
    330  1.35       gwr 	bzero((caddr_t)&dst, sizeof(dst));
    331  1.35       gwr 	dst.sa_len = sizeof(dst);
    332  1.35       gwr 	dst.sa_family = AF_INET;
    333  1.35       gwr 	/* Gateway: */
    334  1.35       gwr 	bzero((caddr_t)&gw, sizeof(gw));
    335  1.35       gwr 	sin = (struct sockaddr_in *)&gw;
    336  1.35       gwr 	sin->sin_len = sizeof(*sin);
    337  1.35       gwr 	sin->sin_family = AF_INET;
    338  1.35       gwr 	sin->sin_addr.s_addr = gw_ip->s_addr;
    339  1.35       gwr 	/* Mask: (zero length) */
    340  1.35       gwr 	/* XXX - Just pass a null pointer? */
    341  1.35       gwr 	bzero(&mask, sizeof(mask));
    342  1.35       gwr 
    343  1.35       gwr 	/* add, dest, gw, mask, flags, 0 */
    344  1.35       gwr 	error = rtrequest(RTM_ADD, &dst, &gw, &mask,
    345  1.35       gwr 					  (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
    346  1.33       gwr 	if (error) {
    347  1.35       gwr 		printf("nfs_boot: add route, error=%d\n", error);
    348  1.33       gwr 		error = 0;
    349  1.33       gwr 	}
    350   1.3       gwr }
    351   1.1     glass 
    352  1.35       gwr /*
    353  1.35       gwr  * Get an initial NFS file handle using Sun RPC/mountd.
    354  1.35       gwr  * Separate function because we used to call it twice.
    355  1.35       gwr  * (once for root and once for swap)
    356  1.35       gwr  */
    357  1.37       gwr static int
    358  1.33       gwr nfs_boot_getfh(ndm)
    359  1.33       gwr 	struct nfs_dlmount *ndm;	/* output */
    360   1.3       gwr {
    361  1.29      fvdl 	struct nfs_args *args;
    362  1.23       gwr 	struct sockaddr_in *sin;
    363  1.33       gwr 	char *pathname;
    364   1.3       gwr 	int error;
    365  1.35       gwr 	u_int16_t port;
    366   1.1     glass 
    367  1.33       gwr 	args = &ndm->ndm_args;
    368  1.29      fvdl 
    369  1.29      fvdl 	/* Initialize mount args. */
    370  1.29      fvdl 	bzero((caddr_t) args, sizeof(*args));
    371  1.33       gwr 	args->addr     = &ndm->ndm_saddr;
    372  1.29      fvdl 	args->addrlen  = args->addr->sa_len;
    373  1.29      fvdl #ifdef NFS_BOOT_TCP
    374  1.29      fvdl 	args->sotype   = SOCK_STREAM;
    375  1.29      fvdl #else
    376  1.29      fvdl 	args->sotype   = SOCK_DGRAM;
    377  1.29      fvdl #endif
    378  1.33       gwr 	args->fh       = ndm->ndm_fh;
    379  1.33       gwr 	args->hostname = ndm->ndm_host;
    380  1.29      fvdl 	args->flags    = NFSMNT_RESVPORT | NFSMNT_NFSV3;
    381  1.29      fvdl 
    382  1.29      fvdl #ifdef	NFS_BOOT_OPTIONS
    383  1.29      fvdl 	args->flags    |= NFS_BOOT_OPTIONS;
    384  1.29      fvdl #endif
    385  1.29      fvdl #ifdef	NFS_BOOT_RWSIZE
    386  1.29      fvdl 	/*
    387  1.29      fvdl 	 * Reduce rsize,wsize for interfaces that consistently
    388  1.29      fvdl 	 * drop fragments of long UDP messages.  (i.e. wd8003).
    389  1.29      fvdl 	 * You can always change these later via remount.
    390  1.29      fvdl 	 */
    391  1.29      fvdl 	args->flags   |= NFSMNT_WSIZE | NFSMNT_RSIZE;
    392  1.29      fvdl 	args->wsize    = NFS_BOOT_RWSIZE;
    393  1.29      fvdl 	args->rsize    = NFS_BOOT_RWSIZE;
    394  1.29      fvdl #endif
    395  1.29      fvdl 
    396   1.3       gwr 	/*
    397  1.33       gwr 	 * Find the pathname part of the "server:pathname"
    398  1.33       gwr 	 * string left in ndm->ndm_host by nfs_boot_init.
    399   1.3       gwr 	 */
    400  1.33       gwr 	pathname = strchr(ndm->ndm_host, ':');
    401  1.33       gwr 	if (pathname == 0) {
    402  1.33       gwr 		printf("nfs_boot: getfh - no pathname\n");
    403  1.33       gwr 		return (EIO);
    404  1.33       gwr 	}
    405  1.33       gwr 	pathname++;
    406   1.1     glass 
    407   1.3       gwr 	/*
    408  1.33       gwr 	 * Get file handle using RPC to mountd/mount
    409   1.3       gwr 	 */
    410  1.33       gwr 	sin = (struct sockaddr_in *)&ndm->ndm_saddr;
    411  1.29      fvdl 	error = md_mount(sin, pathname, args);
    412  1.33       gwr 	if (error) {
    413  1.33       gwr 		printf("nfs_boot: mountd `%s', error=%d\n",
    414  1.35       gwr 		       ndm->ndm_host, error);
    415  1.33       gwr 		return (error);
    416  1.33       gwr 	}
    417   1.4        pk 
    418  1.23       gwr 	/* Set port number for NFS use. */
    419  1.23       gwr 	/* XXX: NFS port is always 2049, right? */
    420  1.29      fvdl #ifdef NFS_BOOT_TCP
    421  1.29      fvdl retry:
    422  1.29      fvdl #endif
    423  1.29      fvdl 	error = krpc_portmap(sin, NFS_PROG,
    424  1.29      fvdl 		    (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
    425  1.29      fvdl 		    (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
    426  1.35       gwr 		    &port);
    427  1.35       gwr 	if (port == htons(0))
    428  1.33       gwr 		error = EIO;
    429  1.35       gwr 	if (error) {
    430  1.29      fvdl #ifdef NFS_BOOT_TCP
    431  1.29      fvdl 		if (args->sotype == SOCK_STREAM) {
    432  1.29      fvdl 			args->sotype = SOCK_DGRAM;
    433  1.29      fvdl 			goto retry;
    434  1.29      fvdl 		}
    435  1.29      fvdl #endif
    436  1.33       gwr 		printf("nfs_boot: portmap NFS, error=%d\n", error);
    437  1.35       gwr 		return (error);
    438  1.29      fvdl 	}
    439  1.35       gwr 	sin->sin_port = port;
    440  1.35       gwr 	return (0);
    441   1.3       gwr }
    442   1.1     glass 
    443   1.1     glass 
    444   1.1     glass /*
    445   1.3       gwr  * RPC: mountd/mount
    446   1.3       gwr  * Given a server pathname, get an NFS file handle.
    447   1.3       gwr  * Also, sets sin->sin_port to the NFS service port.
    448   1.1     glass  */
    449   1.3       gwr static int
    450  1.29      fvdl md_mount(mdsin, path, argp)
    451   1.3       gwr 	struct sockaddr_in *mdsin;		/* mountd server address */
    452   1.3       gwr 	char *path;
    453  1.29      fvdl 	struct nfs_args *argp;
    454   1.1     glass {
    455   1.3       gwr 	/* The RPC structures */
    456   1.3       gwr 	struct rdata {
    457  1.20       cgd 		u_int32_t errno;
    458  1.29      fvdl 		union {
    459  1.29      fvdl 			u_int8_t  v2fh[NFSX_V2FH];
    460  1.29      fvdl 			struct {
    461  1.29      fvdl 				u_int32_t fhlen;
    462  1.29      fvdl 				u_int8_t  fh[1];
    463  1.29      fvdl 			} v3fh;
    464  1.29      fvdl 		} fh;
    465   1.3       gwr 	} *rdata;
    466   1.3       gwr 	struct mbuf *m;
    467  1.29      fvdl 	u_int8_t *fh;
    468  1.29      fvdl 	int minlen, error;
    469  1.35       gwr 	int mntver;
    470   1.3       gwr 
    471  1.35       gwr 	mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2;
    472  1.35       gwr 	do {
    473  1.35       gwr 		/*
    474  1.35       gwr 		 * Get port number for MOUNTD.
    475  1.35       gwr 		 */
    476  1.35       gwr 		error = krpc_portmap(mdsin, RPCPROG_MNT, mntver,
    477  1.35       gwr 		                    IPPROTO_UDP, &mdsin->sin_port);
    478  1.35       gwr 		if (error)
    479  1.35       gwr 			continue;
    480  1.35       gwr 
    481  1.35       gwr 		/* This mbuf is consumed by krpc_call. */
    482  1.35       gwr 		m = xdr_string_encode(path, strlen(path));
    483  1.35       gwr 		if (m == NULL)
    484  1.35       gwr 			return ENOMEM;
    485  1.11       gwr 
    486  1.35       gwr 		/* Do RPC to mountd. */
    487  1.35       gwr 		error = krpc_call(mdsin, RPCPROG_MNT, mntver,
    488  1.35       gwr 		                  RPCMNT_MOUNT, &m, NULL);
    489  1.35       gwr 		if (error != EPROGMISMATCH)
    490  1.35       gwr 			break;
    491  1.35       gwr 		/* Try lower version of mountd. */
    492  1.35       gwr 	} while (--mntver >= 1);
    493  1.29      fvdl 	if (error) {
    494  1.35       gwr 		printf("nfs_boot: mountd error=%d\n", error);
    495  1.35       gwr 		return error;
    496  1.29      fvdl 	}
    497  1.35       gwr 	if (mntver != 3)
    498  1.35       gwr 		argp->flags &= ~NFSMNT_NFSV3;
    499   1.1     glass 
    500  1.23       gwr 	/* The reply might have only the errno. */
    501  1.23       gwr 	if (m->m_len < 4)
    502  1.23       gwr 		goto bad;
    503  1.23       gwr 	/* Have at least errno, so check that. */
    504  1.23       gwr 	rdata = mtod(m, struct rdata *);
    505  1.23       gwr 	error = fxdr_unsigned(u_int32_t, rdata->errno);
    506  1.23       gwr 	if (error)
    507  1.23       gwr 		goto out;
    508  1.23       gwr 
    509  1.35       gwr 	/* Have errno==0, so the fh must be there. */
    510  1.35       gwr 	if (mntver == 3) {
    511  1.29      fvdl 		argp->fhsize   = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
    512  1.29      fvdl 		if (argp->fhsize > NFSX_V3FHMAX)
    513  1.29      fvdl 			goto bad;
    514  1.29      fvdl 		minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
    515  1.29      fvdl 	} else {
    516  1.29      fvdl 		argp->fhsize   = NFSX_V2FH;
    517  1.29      fvdl 		minlen = sizeof(u_int32_t) + argp->fhsize;
    518  1.29      fvdl 	}
    519  1.29      fvdl 
    520  1.29      fvdl 	if (m->m_len < minlen) {
    521  1.29      fvdl 		m = m_pullup(m, minlen);
    522  1.16       gwr 		if (m == NULL)
    523  1.29      fvdl 			return(EBADRPC);
    524  1.23       gwr 		rdata = mtod(m, struct rdata *);
    525  1.16       gwr 	}
    526  1.29      fvdl 
    527  1.35       gwr 	fh = (mntver == 3) ?
    528  1.35       gwr 		rdata->fh.v3fh.fh : rdata->fh.v2fh;
    529  1.29      fvdl 	bcopy(fh, argp->fh, argp->fhsize);
    530  1.29      fvdl 
    531   1.3       gwr 	goto out;
    532   1.1     glass 
    533   1.6   deraadt bad:
    534   1.3       gwr 	error = EBADRPC;
    535   1.1     glass 
    536   1.6   deraadt out:
    537   1.3       gwr 	m_freem(m);
    538   1.3       gwr 	return error;
    539   1.7    paulus }
    540