Home | History | Annotate | Line # | Download | only in nfs
nfs_boot.c revision 1.37
      1  1.37      gwr /*	$NetBSD: nfs_boot.c,v 1.37 1997/09/09 21:39:17 gwr 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.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.35      gwr #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.37      gwr #include <nfs/krpc.h>
     67  1.37      gwr #include <nfs/xdr_subs.h>
     68  1.37      gwr 
     69  1.25     fvdl #include <nfs/nfsproto.h>
     70   1.1    glass #include <nfs/nfsdiskless.h>
     71   1.1    glass 
     72  1.36      gwr #include "arp.h"
     73  1.36      gwr #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.36      gwr 	printf("nfs_boot: NARP == 0\n");
     80  1.33      gwr 	return (ENXIO);
     81  1.24      gwr }
     82  1.24      gwr 
     83  1.36      gwr #else /* NARP */
     84   1.7   paulus 
     85   1.1    glass /*
     86  1.35      gwr  * There are two implementations of NFS diskless boot.
     87  1.35      gwr  * One implementation uses BOOTP (RFC951, RFC1048),
     88  1.35      gwr  * the other uses Sun RPC/bootparams.  See the files:
     89  1.35      gwr  *    nfs_bootp.c:   BOOTP (RFC951, RFC1048)
     90  1.35      gwr  *    nfs_bootsun.c: Sun RPC/bootparams
     91   1.1    glass  *
     92  1.35      gwr  * The variable nfs_boot_rfc951 selects which to use.
     93  1.35      gwr  * This is defined as BSS so machine-dependent code
     94  1.35      gwr  * may provide a data definition to override this.
     95   1.1    glass  */
     96  1.35      gwr int nfs_boot_rfc951; /* 0: BOOTP. 1: 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.35      gwr static void nfs_boot_defrt __P((struct in_addr *));
    103  1.37      gwr static  int nfs_boot_getfh __P((struct nfs_dlmount *ndm));
    104  1.35      gwr 
    105  1.35      gwr 
    106   1.1    glass /*
    107   1.3      gwr  * Called with an empty nfs_diskless struct to be filled in.
    108  1.35      gwr  * Find an interface, determine its ip address (etc.) and
    109  1.35      gwr  * 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.35      gwr 	 * 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.35      gwr 		printf("nfs_boot: '%s' not found\n",
    125  1.35      gwr 		       root_device->dv_xname);
    126  1.30  thorpej 		return (ENXIO);
    127  1.30  thorpej 	}
    128  1.35      gwr 	if (ifp->if_type != IFT_ETHER) {
    129  1.35      gwr 		printf("nfs_boot: unknown I/F type %d\n", ifp->if_type);
    130  1.35      gwr 		return (ENODEV);	/* Op. not supported by device */
    131  1.33      gwr 	}
    132   1.3      gwr 
    133  1.35      gwr 	if (nfs_boot_rfc951) {
    134  1.35      gwr 		printf("nfs_boot: trying BOOTP/DHCP\n");
    135  1.35      gwr 		error = nfs_bootdhcp(ifp, nd, procp);
    136  1.35      gwr 	} else {
    137  1.35      gwr 		printf("nfs_boot: trying RARP (and RPC/bootparam)\n");
    138  1.35      gwr 		error = nfs_bootparam(ifp, nd, procp);
    139  1.30  thorpej 	}
    140  1.35      gwr 	if (error)
    141  1.35      gwr 		return (error);
    142   1.3      gwr 
    143   1.3      gwr 	/*
    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.35      gwr 	if (nd->nd_gwip.s_addr)
    148  1.35      gwr 		nfs_boot_defrt(&nd->nd_gwip);
    149   1.3      gwr 
    150  1.37      gwr 	/*
    151  1.37      gwr 	 * Now fetch the NFS file handles as appropriate.
    152  1.37      gwr 	 */
    153  1.37      gwr 	error = nfs_boot_getfh(&nd->nd_root);
    154  1.37      gwr 	if (error)
    155  1.37      gwr 		return (error);
    156  1.37      gwr 
    157  1.37      gwr #if 0	/* swap now comes in from swapctl(2) */
    158  1.37      gwr 	if (nd->nd_swap.ndm_saddr.sa_len) {
    159  1.37      gwr 		error = nfs_boot_getfh(&nd->nd_swap);
    160  1.37      gwr 		if (error) {
    161  1.37      gwr 			printf("nfs_boot: warning: getfh(swap), error=%d\n", error);
    162  1.37      gwr 			/* Just ignore the error */
    163  1.37      gwr 			error = 0;
    164  1.37      gwr 		}
    165  1.37      gwr 	}
    166  1.37      gwr #endif
    167  1.37      gwr 
    168  1.37      gwr 	return (error);
    169  1.35      gwr }
    170   1.1    glass 
    171  1.35      gwr /*
    172  1.35      gwr  * Install a default route to the passed IP address.
    173  1.35      gwr  */
    174  1.35      gwr static void
    175  1.35      gwr nfs_boot_defrt(gw_ip)
    176  1.35      gwr 	struct in_addr *gw_ip;
    177  1.35      gwr {
    178  1.35      gwr 	struct sockaddr dst, gw, mask;
    179  1.35      gwr 	struct sockaddr_in *sin;
    180  1.35      gwr 	int error;
    181  1.33      gwr 
    182  1.35      gwr 	/* Destination: (default) */
    183  1.35      gwr 	bzero((caddr_t)&dst, sizeof(dst));
    184  1.35      gwr 	dst.sa_len = sizeof(dst);
    185  1.35      gwr 	dst.sa_family = AF_INET;
    186  1.35      gwr 	/* Gateway: */
    187  1.35      gwr 	bzero((caddr_t)&gw, sizeof(gw));
    188  1.35      gwr 	sin = (struct sockaddr_in *)&gw;
    189  1.35      gwr 	sin->sin_len = sizeof(*sin);
    190  1.35      gwr 	sin->sin_family = AF_INET;
    191  1.35      gwr 	sin->sin_addr.s_addr = gw_ip->s_addr;
    192  1.35      gwr 	/* Mask: (zero length) */
    193  1.35      gwr 	/* XXX - Just pass a null pointer? */
    194  1.35      gwr 	bzero(&mask, sizeof(mask));
    195  1.35      gwr 
    196  1.35      gwr 	/* add, dest, gw, mask, flags, 0 */
    197  1.35      gwr 	error = rtrequest(RTM_ADD, &dst, &gw, &mask,
    198  1.35      gwr 					  (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
    199  1.33      gwr 	if (error) {
    200  1.35      gwr 		printf("nfs_boot: add route, error=%d\n", error);
    201  1.33      gwr 		error = 0;
    202  1.33      gwr 	}
    203   1.3      gwr }
    204   1.1    glass 
    205  1.35      gwr /*
    206  1.35      gwr  * Get an initial NFS file handle using Sun RPC/mountd.
    207  1.35      gwr  * Separate function because we used to call it twice.
    208  1.35      gwr  * (once for root and once for swap)
    209  1.35      gwr  */
    210  1.37      gwr static int
    211  1.33      gwr nfs_boot_getfh(ndm)
    212  1.33      gwr 	struct nfs_dlmount *ndm;	/* output */
    213   1.3      gwr {
    214  1.29     fvdl 	struct nfs_args *args;
    215  1.23      gwr 	struct sockaddr_in *sin;
    216  1.33      gwr 	char *pathname;
    217   1.3      gwr 	int error;
    218  1.35      gwr 	u_int16_t port;
    219   1.1    glass 
    220  1.33      gwr 	args = &ndm->ndm_args;
    221  1.29     fvdl 
    222  1.29     fvdl 	/* Initialize mount args. */
    223  1.29     fvdl 	bzero((caddr_t) args, sizeof(*args));
    224  1.33      gwr 	args->addr     = &ndm->ndm_saddr;
    225  1.29     fvdl 	args->addrlen  = args->addr->sa_len;
    226  1.29     fvdl #ifdef NFS_BOOT_TCP
    227  1.29     fvdl 	args->sotype   = SOCK_STREAM;
    228  1.29     fvdl #else
    229  1.29     fvdl 	args->sotype   = SOCK_DGRAM;
    230  1.29     fvdl #endif
    231  1.33      gwr 	args->fh       = ndm->ndm_fh;
    232  1.33      gwr 	args->hostname = ndm->ndm_host;
    233  1.29     fvdl 	args->flags    = NFSMNT_RESVPORT | NFSMNT_NFSV3;
    234  1.29     fvdl 
    235  1.29     fvdl #ifdef	NFS_BOOT_OPTIONS
    236  1.29     fvdl 	args->flags    |= NFS_BOOT_OPTIONS;
    237  1.29     fvdl #endif
    238  1.29     fvdl #ifdef	NFS_BOOT_RWSIZE
    239  1.29     fvdl 	/*
    240  1.29     fvdl 	 * Reduce rsize,wsize for interfaces that consistently
    241  1.29     fvdl 	 * drop fragments of long UDP messages.  (i.e. wd8003).
    242  1.29     fvdl 	 * You can always change these later via remount.
    243  1.29     fvdl 	 */
    244  1.29     fvdl 	args->flags   |= NFSMNT_WSIZE | NFSMNT_RSIZE;
    245  1.29     fvdl 	args->wsize    = NFS_BOOT_RWSIZE;
    246  1.29     fvdl 	args->rsize    = NFS_BOOT_RWSIZE;
    247  1.29     fvdl #endif
    248  1.29     fvdl 
    249   1.3      gwr 	/*
    250  1.33      gwr 	 * Find the pathname part of the "server:pathname"
    251  1.33      gwr 	 * string left in ndm->ndm_host by nfs_boot_init.
    252   1.3      gwr 	 */
    253  1.33      gwr 	pathname = strchr(ndm->ndm_host, ':');
    254  1.33      gwr 	if (pathname == 0) {
    255  1.33      gwr 		printf("nfs_boot: getfh - no pathname\n");
    256  1.33      gwr 		return (EIO);
    257  1.33      gwr 	}
    258  1.33      gwr 	pathname++;
    259   1.1    glass 
    260   1.3      gwr 	/*
    261  1.33      gwr 	 * Get file handle using RPC to mountd/mount
    262   1.3      gwr 	 */
    263  1.33      gwr 	sin = (struct sockaddr_in *)&ndm->ndm_saddr;
    264  1.29     fvdl 	error = md_mount(sin, pathname, args);
    265  1.33      gwr 	if (error) {
    266  1.33      gwr 		printf("nfs_boot: mountd `%s', error=%d\n",
    267  1.35      gwr 		       ndm->ndm_host, error);
    268  1.33      gwr 		return (error);
    269  1.33      gwr 	}
    270   1.4       pk 
    271  1.23      gwr 	/* Set port number for NFS use. */
    272  1.23      gwr 	/* XXX: NFS port is always 2049, right? */
    273  1.29     fvdl #ifdef NFS_BOOT_TCP
    274  1.29     fvdl retry:
    275  1.29     fvdl #endif
    276  1.29     fvdl 	error = krpc_portmap(sin, NFS_PROG,
    277  1.29     fvdl 		    (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
    278  1.29     fvdl 		    (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
    279  1.35      gwr 		    &port);
    280  1.35      gwr 	if (port == htons(0))
    281  1.33      gwr 		error = EIO;
    282  1.35      gwr 	if (error) {
    283  1.29     fvdl #ifdef NFS_BOOT_TCP
    284  1.29     fvdl 		if (args->sotype == SOCK_STREAM) {
    285  1.29     fvdl 			args->sotype = SOCK_DGRAM;
    286  1.29     fvdl 			goto retry;
    287  1.29     fvdl 		}
    288  1.29     fvdl #endif
    289  1.33      gwr 		printf("nfs_boot: portmap NFS, error=%d\n", error);
    290  1.35      gwr 		return (error);
    291  1.29     fvdl 	}
    292  1.35      gwr 	sin->sin_port = port;
    293  1.35      gwr 	return (0);
    294   1.3      gwr }
    295   1.1    glass 
    296   1.1    glass 
    297   1.1    glass /*
    298   1.3      gwr  * RPC: mountd/mount
    299   1.3      gwr  * Given a server pathname, get an NFS file handle.
    300   1.3      gwr  * Also, sets sin->sin_port to the NFS service port.
    301   1.1    glass  */
    302   1.3      gwr static int
    303  1.29     fvdl md_mount(mdsin, path, argp)
    304   1.3      gwr 	struct sockaddr_in *mdsin;		/* mountd server address */
    305   1.3      gwr 	char *path;
    306  1.29     fvdl 	struct nfs_args *argp;
    307   1.1    glass {
    308   1.3      gwr 	/* The RPC structures */
    309   1.3      gwr 	struct rdata {
    310  1.20      cgd 		u_int32_t errno;
    311  1.29     fvdl 		union {
    312  1.29     fvdl 			u_int8_t  v2fh[NFSX_V2FH];
    313  1.29     fvdl 			struct {
    314  1.29     fvdl 				u_int32_t fhlen;
    315  1.29     fvdl 				u_int8_t  fh[1];
    316  1.29     fvdl 			} v3fh;
    317  1.29     fvdl 		} fh;
    318   1.3      gwr 	} *rdata;
    319   1.3      gwr 	struct mbuf *m;
    320  1.29     fvdl 	u_int8_t *fh;
    321  1.29     fvdl 	int minlen, error;
    322  1.35      gwr 	int mntver;
    323   1.3      gwr 
    324  1.35      gwr 	mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2;
    325  1.35      gwr 	do {
    326  1.35      gwr 		/*
    327  1.35      gwr 		 * Get port number for MOUNTD.
    328  1.35      gwr 		 */
    329  1.35      gwr 		error = krpc_portmap(mdsin, RPCPROG_MNT, mntver,
    330  1.35      gwr 		                    IPPROTO_UDP, &mdsin->sin_port);
    331  1.35      gwr 		if (error)
    332  1.35      gwr 			continue;
    333  1.35      gwr 
    334  1.35      gwr 		/* This mbuf is consumed by krpc_call. */
    335  1.35      gwr 		m = xdr_string_encode(path, strlen(path));
    336  1.35      gwr 		if (m == NULL)
    337  1.35      gwr 			return ENOMEM;
    338  1.11      gwr 
    339  1.35      gwr 		/* Do RPC to mountd. */
    340  1.35      gwr 		error = krpc_call(mdsin, RPCPROG_MNT, mntver,
    341  1.35      gwr 		                  RPCMNT_MOUNT, &m, NULL);
    342  1.35      gwr 		if (error != EPROGMISMATCH)
    343  1.35      gwr 			break;
    344  1.35      gwr 		/* Try lower version of mountd. */
    345  1.35      gwr 	} while (--mntver >= 1);
    346  1.29     fvdl 	if (error) {
    347  1.35      gwr 		printf("nfs_boot: mountd error=%d\n", error);
    348  1.35      gwr 		return error;
    349  1.29     fvdl 	}
    350  1.35      gwr 	if (mntver != 3)
    351  1.35      gwr 		argp->flags &= ~NFSMNT_NFSV3;
    352   1.1    glass 
    353  1.23      gwr 	/* The reply might have only the errno. */
    354  1.23      gwr 	if (m->m_len < 4)
    355  1.23      gwr 		goto bad;
    356  1.23      gwr 	/* Have at least errno, so check that. */
    357  1.23      gwr 	rdata = mtod(m, struct rdata *);
    358  1.23      gwr 	error = fxdr_unsigned(u_int32_t, rdata->errno);
    359  1.23      gwr 	if (error)
    360  1.23      gwr 		goto out;
    361  1.23      gwr 
    362  1.35      gwr 	/* Have errno==0, so the fh must be there. */
    363  1.35      gwr 	if (mntver == 3) {
    364  1.29     fvdl 		argp->fhsize   = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
    365  1.29     fvdl 		if (argp->fhsize > NFSX_V3FHMAX)
    366  1.29     fvdl 			goto bad;
    367  1.29     fvdl 		minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
    368  1.29     fvdl 	} else {
    369  1.29     fvdl 		argp->fhsize   = NFSX_V2FH;
    370  1.29     fvdl 		minlen = sizeof(u_int32_t) + argp->fhsize;
    371  1.29     fvdl 	}
    372  1.29     fvdl 
    373  1.29     fvdl 	if (m->m_len < minlen) {
    374  1.29     fvdl 		m = m_pullup(m, minlen);
    375  1.16      gwr 		if (m == NULL)
    376  1.29     fvdl 			return(EBADRPC);
    377  1.23      gwr 		rdata = mtod(m, struct rdata *);
    378  1.16      gwr 	}
    379  1.29     fvdl 
    380  1.35      gwr 	fh = (mntver == 3) ?
    381  1.35      gwr 		rdata->fh.v3fh.fh : rdata->fh.v2fh;
    382  1.29     fvdl 	bcopy(fh, argp->fh, argp->fhsize);
    383  1.29     fvdl 
    384   1.3      gwr 	goto out;
    385   1.1    glass 
    386   1.6  deraadt bad:
    387   1.3      gwr 	error = EBADRPC;
    388   1.1    glass 
    389   1.6  deraadt out:
    390   1.3      gwr 	m_freem(m);
    391   1.3      gwr 	return error;
    392   1.7   paulus }
    393   1.7   paulus 
    394  1.36      gwr #endif /* NARP */
    395