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