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