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