1999-09-04 00:06:46 +04:00
|
|
|
/* $NetBSD: nfs_boot.c,v 1.52 1999/09/03 20:06:46 drochner Exp $ */
|
1994-06-29 10:39:25 +04:00
|
|
|
|
1997-08-29 20:12:49 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
|
1994-04-18 10:18:05 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
1997-08-29 20:12:49 +04:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Adam Glass and Gordon W. Ross.
|
|
|
|
*
|
1994-04-18 10:18:05 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1997-08-29 20:12:49 +04:00
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1998-09-13 17:49:29 +04:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
1997-08-29 20:12:49 +04:00
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
1994-04-18 10:18:05 +04:00
|
|
|
*
|
1997-08-29 20:12:49 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Support for NFS diskless booting, specifically getting information
|
|
|
|
* about where to mount root from, what pathnames, etc.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
|
|
|
|
1998-01-13 00:27:09 +03:00
|
|
|
#include "opt_nfs_boot.h"
|
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
#include <sys/param.h>
|
1994-05-05 09:35:42 +04:00
|
|
|
#include <sys/systm.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/conf.h>
|
1997-01-31 05:57:31 +03:00
|
|
|
#include <sys/device.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/reboot.h>
|
1995-04-25 01:54:56 +04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
#include <net/if.h>
|
1994-06-13 19:28:55 +04:00
|
|
|
#include <net/route.h>
|
1997-03-15 21:09:08 +03:00
|
|
|
#include <net/if_ether.h>
|
1997-08-29 20:12:49 +04:00
|
|
|
#include <net/if_types.h>
|
1997-03-15 21:09:08 +03:00
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
#include <netinet/in.h>
|
1997-03-15 21:09:08 +03:00
|
|
|
#include <netinet/if_inarp.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
#include <nfs/rpcv2.h>
|
1994-09-26 19:42:29 +03:00
|
|
|
#include <nfs/krpc.h>
|
1995-05-20 05:52:49 +04:00
|
|
|
#include <nfs/xdr_subs.h>
|
1997-09-10 01:39:17 +04:00
|
|
|
|
|
|
|
#include <nfs/nfsproto.h>
|
1998-03-01 05:20:01 +03:00
|
|
|
#include <nfs/nfs.h>
|
|
|
|
#include <nfs/nfsmount.h>
|
1997-09-10 01:39:17 +04:00
|
|
|
#include <nfs/nfsdiskless.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
/*
|
1997-08-29 20:12:49 +04:00
|
|
|
* There are two implementations of NFS diskless boot.
|
|
|
|
* One implementation uses BOOTP (RFC951, RFC1048),
|
|
|
|
* the other uses Sun RPC/bootparams. See the files:
|
|
|
|
* nfs_bootp.c: BOOTP (RFC951, RFC1048)
|
|
|
|
* nfs_bootsun.c: Sun RPC/bootparams
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1998-01-11 08:55:40 +03:00
|
|
|
#if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
|
1998-01-09 18:10:37 +03:00
|
|
|
int nfs_boot_rfc951 = 1; /* BOOTP enabled (default) */
|
|
|
|
#endif
|
|
|
|
#ifdef NFS_BOOT_BOOTPARAM
|
|
|
|
int nfs_boot_bootparam = 1; /* BOOTPARAM enabled (default) */
|
|
|
|
#endif
|
1994-06-13 19:28:55 +04:00
|
|
|
|
|
|
|
/* mountd RPC */
|
1994-06-29 11:28:36 +04:00
|
|
|
static int md_mount __P((struct sockaddr_in *mdsin, char *path,
|
1996-10-20 17:13:22 +04:00
|
|
|
struct nfs_args *argp));
|
1994-06-13 19:28:55 +04:00
|
|
|
|
1997-08-29 20:12:49 +04:00
|
|
|
static void nfs_boot_defrt __P((struct in_addr *));
|
1997-09-10 01:39:17 +04:00
|
|
|
static int nfs_boot_getfh __P((struct nfs_dlmount *ndm));
|
1997-08-29 20:12:49 +04:00
|
|
|
|
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
1994-06-13 19:28:55 +04:00
|
|
|
* Called with an empty nfs_diskless struct to be filled in.
|
1997-08-29 20:12:49 +04:00
|
|
|
* Find an interface, determine its ip address (etc.) and
|
|
|
|
* save all the boot parameters in the nfs_diskless struct.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1994-06-29 11:28:36 +04:00
|
|
|
int
|
|
|
|
nfs_boot_init(nd, procp)
|
1994-06-13 19:28:55 +04:00
|
|
|
struct nfs_diskless *nd;
|
|
|
|
struct proc *procp;
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
1994-06-13 19:28:55 +04:00
|
|
|
struct ifnet *ifp;
|
1995-04-25 01:54:56 +04:00
|
|
|
int error;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
1997-08-29 20:12:49 +04:00
|
|
|
* Find the network interface.
|
1994-06-13 19:28:55 +04:00
|
|
|
*/
|
1997-01-31 05:57:31 +03:00
|
|
|
ifp = ifunit(root_device->dv_xname);
|
|
|
|
if (ifp == NULL) {
|
1997-08-29 20:12:49 +04:00
|
|
|
printf("nfs_boot: '%s' not found\n",
|
|
|
|
root_device->dv_xname);
|
1997-01-31 05:57:31 +03:00
|
|
|
return (ENXIO);
|
|
|
|
}
|
1999-02-21 18:07:49 +03:00
|
|
|
nd->nd_ifp = ifp;
|
1994-06-13 19:28:55 +04:00
|
|
|
|
1998-01-09 18:10:37 +03:00
|
|
|
error = EADDRNOTAVAIL; /* ??? */
|
1998-01-11 08:55:40 +03:00
|
|
|
#if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
|
1998-06-13 18:13:25 +04:00
|
|
|
if (error && nfs_boot_rfc951) {
|
1998-02-28 04:45:01 +03:00
|
|
|
#if defined(NFS_BOOT_DHCP)
|
|
|
|
printf("nfs_boot: trying DHCP/BOOTP\n");
|
|
|
|
#else
|
|
|
|
printf("nfs_boot: trying BOOTP\n");
|
|
|
|
#endif
|
1999-02-21 18:07:49 +03:00
|
|
|
error = nfs_bootdhcp(nd, procp);
|
1998-01-09 18:10:37 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef NFS_BOOT_BOOTPARAM
|
1998-06-13 18:13:25 +04:00
|
|
|
if (error && nfs_boot_bootparam) {
|
1997-08-29 20:12:49 +04:00
|
|
|
printf("nfs_boot: trying RARP (and RPC/bootparam)\n");
|
1999-02-21 18:07:49 +03:00
|
|
|
error = nfs_bootparam(nd, procp);
|
1997-01-31 05:57:31 +03:00
|
|
|
}
|
1998-01-09 18:10:37 +03:00
|
|
|
#endif
|
1998-06-13 08:28:46 +04:00
|
|
|
if (error)
|
|
|
|
return (error);
|
1994-06-13 19:28:55 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the gateway address is set, add a default route.
|
|
|
|
* (The mountd RPCs may go across a gateway.)
|
|
|
|
*/
|
1997-08-29 20:12:49 +04:00
|
|
|
if (nd->nd_gwip.s_addr)
|
|
|
|
nfs_boot_defrt(&nd->nd_gwip);
|
1994-06-13 19:28:55 +04:00
|
|
|
|
1997-09-10 01:39:17 +04:00
|
|
|
/*
|
|
|
|
* Now fetch the NFS file handles as appropriate.
|
|
|
|
*/
|
|
|
|
error = nfs_boot_getfh(&nd->nd_root);
|
|
|
|
|
1999-02-21 18:07:49 +03:00
|
|
|
if (error)
|
|
|
|
nfs_boot_cleanup(nd, procp);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nfs_boot_cleanup(nd, procp)
|
|
|
|
struct nfs_diskless *nd;
|
|
|
|
struct proc *procp;
|
|
|
|
{
|
|
|
|
|
|
|
|
nfs_boot_deladdress(nd->nd_ifp, procp, nd->nd_myip.s_addr);
|
|
|
|
nfs_boot_ifupdown(nd->nd_ifp, procp, 0);
|
|
|
|
nfs_boot_flushrt(nd->nd_ifp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nfs_boot_ifupdown(ifp, procp, up)
|
|
|
|
struct ifnet *ifp;
|
|
|
|
struct proc *procp;
|
|
|
|
int up;
|
|
|
|
{
|
|
|
|
struct socket *so;
|
|
|
|
struct ifreq ireq;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
memset(&ireq, 0, sizeof(ireq));
|
|
|
|
memcpy(ireq.ifr_name, ifp->if_xname, IFNAMSIZ);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a socket to use for various things in here.
|
|
|
|
* After this, use "goto out" to cleanup and return.
|
|
|
|
*/
|
|
|
|
error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
|
|
|
|
if (error) {
|
|
|
|
printf("ifupdown: socreate, error=%d\n", error);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bring up the interface. (just set the "up" flag)
|
|
|
|
* Get the old interface flags and or IFF_UP into them so
|
|
|
|
* things like media selection flags are not clobbered.
|
|
|
|
*/
|
|
|
|
error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
|
|
|
|
if (error) {
|
|
|
|
printf("ifupdown: GIFFLAGS, error=%d\n", error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (up)
|
|
|
|
ireq.ifr_flags |= IFF_UP;
|
|
|
|
else
|
|
|
|
ireq.ifr_flags &= ~IFF_UP;
|
|
|
|
error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
|
|
|
|
if (error) {
|
|
|
|
printf("ifupdown: SIFFLAGS, error=%d\n", error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
1999-09-04 00:06:46 +04:00
|
|
|
if (up)
|
|
|
|
delay(3000000); /* give the link some time to get up */
|
1999-02-21 18:07:49 +03:00
|
|
|
out:
|
|
|
|
soclose(so);
|
1997-09-10 01:39:17 +04:00
|
|
|
return (error);
|
1997-08-29 20:12:49 +04:00
|
|
|
}
|
1994-06-13 19:28:55 +04:00
|
|
|
|
1999-02-21 18:07:49 +03:00
|
|
|
int
|
|
|
|
nfs_boot_setaddress(ifp, procp, addr, netmask, braddr)
|
|
|
|
struct ifnet *ifp;
|
|
|
|
struct proc *procp;
|
|
|
|
u_int32_t addr, netmask, braddr;
|
|
|
|
{
|
|
|
|
struct socket *so;
|
|
|
|
struct ifaliasreq iareq;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a socket to use for various things in here.
|
|
|
|
* After this, use "goto out" to cleanup and return.
|
|
|
|
*/
|
|
|
|
error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
|
|
|
|
if (error) {
|
|
|
|
printf("setaddress: socreate, error=%d\n", error);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&iareq, 0, sizeof(iareq));
|
|
|
|
memcpy(iareq.ifra_name, ifp->if_xname, IFNAMSIZ);
|
|
|
|
|
|
|
|
/* Set the I/F address */
|
|
|
|
sin = (struct sockaddr_in *)&iareq.ifra_addr;
|
|
|
|
sin->sin_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = addr;
|
|
|
|
|
|
|
|
/* Set the netmask */
|
|
|
|
if (netmask != INADDR_ANY) {
|
|
|
|
sin = (struct sockaddr_in *)&iareq.ifra_mask;
|
|
|
|
sin->sin_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = netmask;
|
|
|
|
} /* else leave subnetmask unspecified (len=0) */
|
|
|
|
|
|
|
|
/* Set the broadcast addr. */
|
|
|
|
if (braddr != INADDR_ANY) {
|
|
|
|
sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
|
|
|
|
sin->sin_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = braddr;
|
|
|
|
} /* else leave broadcast addr unspecified (len=0) */
|
|
|
|
|
|
|
|
error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
|
|
|
|
if (error) {
|
|
|
|
printf("setaddress, error=%d\n", error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
1999-09-04 00:06:46 +04:00
|
|
|
delay(3000000); /* give the link some time to get up */
|
1999-02-21 18:07:49 +03:00
|
|
|
out:
|
|
|
|
soclose(so);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nfs_boot_deladdress(ifp, procp, addr)
|
|
|
|
struct ifnet *ifp;
|
|
|
|
struct proc *procp;
|
|
|
|
u_int32_t addr;
|
|
|
|
{
|
|
|
|
struct socket *so;
|
|
|
|
struct ifreq ireq;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a socket to use for various things in here.
|
|
|
|
* After this, use "goto out" to cleanup and return.
|
|
|
|
*/
|
|
|
|
error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
|
|
|
|
if (error) {
|
|
|
|
printf("deladdress: socreate, error=%d\n", error);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&ireq, 0, sizeof(ireq));
|
|
|
|
memcpy(ireq.ifr_name, ifp->if_xname, IFNAMSIZ);
|
|
|
|
|
|
|
|
sin = (struct sockaddr_in *)&ireq.ifr_addr;
|
|
|
|
sin->sin_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = addr;
|
|
|
|
|
|
|
|
error = ifioctl(so, SIOCDIFADDR, (caddr_t)&ireq, procp);
|
|
|
|
if (error) {
|
|
|
|
printf("deladdress, error=%d\n", error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
soclose(so);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nfs_boot_setrecvtimo(so)
|
|
|
|
struct socket *so;
|
1997-10-01 00:44:31 +04:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct timeval *tv;
|
|
|
|
|
|
|
|
m = m_get(M_WAIT, MT_SOOPTS);
|
|
|
|
tv = mtod(m, struct timeval *);
|
|
|
|
m->m_len = sizeof(*tv);
|
|
|
|
tv->tv_sec = 1;
|
|
|
|
tv->tv_usec = 0;
|
1999-02-21 18:07:49 +03:00
|
|
|
return (sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m));
|
1997-10-01 00:44:31 +04:00
|
|
|
}
|
|
|
|
|
1999-02-21 18:07:49 +03:00
|
|
|
int
|
|
|
|
nfs_boot_enbroadcast(so)
|
|
|
|
struct socket *so;
|
1997-10-01 00:44:31 +04:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
int32_t *on;
|
|
|
|
|
|
|
|
m = m_get(M_WAIT, MT_SOOPTS);
|
|
|
|
on = mtod(m, int32_t *);
|
|
|
|
m->m_len = sizeof(*on);
|
|
|
|
*on = 1;
|
1999-02-21 18:07:49 +03:00
|
|
|
return (sosetopt(so, SOL_SOCKET, SO_BROADCAST, m));
|
1997-10-01 00:44:31 +04:00
|
|
|
}
|
|
|
|
|
1999-02-21 18:07:49 +03:00
|
|
|
int
|
|
|
|
nfs_boot_sobind_ipport(so, port)
|
|
|
|
struct socket *so;
|
|
|
|
u_int16_t port;
|
1997-10-01 00:44:31 +04:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
m = m_getclr(M_WAIT, MT_SONAME);
|
|
|
|
sin = mtod(m, struct sockaddr_in *);
|
|
|
|
sin->sin_len = m->m_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
sin->sin_port = htons(port);
|
|
|
|
error = sobind(so, m);
|
|
|
|
m_freem(m);
|
1999-02-21 18:07:49 +03:00
|
|
|
return (error);
|
1997-10-01 00:44:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* What is the longest we will wait before re-sending a request?
|
|
|
|
* Note this is also the frequency of "timeout" messages.
|
|
|
|
* The re-send loop counts up linearly to this maximum, so the
|
|
|
|
* first complaint will happen after (1+2+3+4+5)=15 seconds.
|
|
|
|
*/
|
|
|
|
#define MAX_RESEND_DELAY 5 /* seconds */
|
|
|
|
#define TOTAL_TIMEOUT 30 /* seconds */
|
|
|
|
|
1999-02-21 18:07:49 +03:00
|
|
|
int
|
|
|
|
nfs_boot_sendrecv(so, nam, sndproc, snd, rcvproc, rcv, from_p, context)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *nam;
|
|
|
|
int (*sndproc) __P((struct mbuf*, void*, int));
|
|
|
|
struct mbuf *snd;
|
|
|
|
int (*rcvproc) __P((struct mbuf*, void*));
|
|
|
|
struct mbuf **rcv, **from_p;
|
|
|
|
void *context;
|
1997-10-01 00:44:31 +04:00
|
|
|
{
|
|
|
|
int error, rcvflg, timo, secs, waited;
|
|
|
|
struct mbuf *m, *from;
|
|
|
|
struct uio uio;
|
|
|
|
|
|
|
|
/* Free at end if not null. */
|
|
|
|
from = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send it, repeatedly, until a reply is received,
|
|
|
|
* but delay each re-send by an increasing amount.
|
|
|
|
* If the delay hits the maximum, start complaining.
|
|
|
|
*/
|
|
|
|
waited = timo = 0;
|
|
|
|
send_again:
|
|
|
|
waited += timo;
|
|
|
|
if (waited >= TOTAL_TIMEOUT)
|
1999-02-21 18:07:49 +03:00
|
|
|
return (ETIMEDOUT);
|
1997-10-01 00:44:31 +04:00
|
|
|
|
|
|
|
/* Determine new timeout. */
|
|
|
|
if (timo < MAX_RESEND_DELAY)
|
|
|
|
timo++;
|
|
|
|
else
|
|
|
|
printf("nfs_boot: timeout...\n");
|
|
|
|
|
|
|
|
if (sndproc) {
|
|
|
|
error = (*sndproc)(snd, context, waited);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send request (or re-send). */
|
|
|
|
m = m_copypacket(snd, M_WAIT);
|
|
|
|
if (m == NULL) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto out;
|
|
|
|
}
|
1998-04-25 21:41:01 +04:00
|
|
|
error = (*so->so_send)(so, nam, NULL, m, NULL, 0);
|
1997-10-01 00:44:31 +04:00
|
|
|
if (error) {
|
|
|
|
printf("nfs_boot: sosend: %d\n", error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
m = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for up to timo seconds for a reply.
|
|
|
|
* The socket receive timeout was set to 1 second.
|
|
|
|
*/
|
|
|
|
|
|
|
|
secs = timo;
|
|
|
|
for (;;) {
|
|
|
|
if (from) {
|
|
|
|
m_freem(from);
|
|
|
|
from = NULL;
|
|
|
|
}
|
|
|
|
if (m) {
|
|
|
|
m_freem(m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
uio.uio_resid = 1 << 16; /* ??? */
|
|
|
|
rcvflg = 0;
|
1998-04-25 21:41:01 +04:00
|
|
|
error = (*so->so_receive)(so, &from, &uio, &m, NULL, &rcvflg);
|
1997-10-01 00:44:31 +04:00
|
|
|
if (error == EWOULDBLOCK) {
|
|
|
|
if (--secs <= 0)
|
|
|
|
goto send_again;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (!m || !(m->m_flags & M_PKTHDR)
|
|
|
|
|| (1 << 16) - uio.uio_resid != m->m_pkthdr.len)
|
|
|
|
panic("nfs_boot_sendrecv: return size");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((*rcvproc)(m, context))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rcv)
|
|
|
|
*rcv = m;
|
|
|
|
else
|
|
|
|
m_freem(m);
|
|
|
|
if (from_p) {
|
|
|
|
*from_p = from;
|
|
|
|
from = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (from) m_freem(from);
|
1999-02-21 18:07:49 +03:00
|
|
|
return (error);
|
1997-10-01 00:44:31 +04:00
|
|
|
}
|
|
|
|
|
1997-08-29 20:12:49 +04:00
|
|
|
/*
|
|
|
|
* Install a default route to the passed IP address.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
nfs_boot_defrt(gw_ip)
|
|
|
|
struct in_addr *gw_ip;
|
|
|
|
{
|
|
|
|
struct sockaddr dst, gw, mask;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
int error;
|
1997-05-28 03:37:39 +04:00
|
|
|
|
1997-08-29 20:12:49 +04:00
|
|
|
/* Destination: (default) */
|
1998-08-10 00:51:08 +04:00
|
|
|
memset((caddr_t)&dst, 0, sizeof(dst));
|
1997-08-29 20:12:49 +04:00
|
|
|
dst.sa_len = sizeof(dst);
|
|
|
|
dst.sa_family = AF_INET;
|
|
|
|
/* Gateway: */
|
1998-08-10 00:51:08 +04:00
|
|
|
memset((caddr_t)&gw, 0, sizeof(gw));
|
1997-08-29 20:12:49 +04:00
|
|
|
sin = (struct sockaddr_in *)&gw;
|
|
|
|
sin->sin_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = gw_ip->s_addr;
|
|
|
|
/* Mask: (zero length) */
|
|
|
|
/* XXX - Just pass a null pointer? */
|
1998-08-10 00:51:08 +04:00
|
|
|
memset(&mask, 0, sizeof(mask));
|
1997-08-29 20:12:49 +04:00
|
|
|
|
|
|
|
/* add, dest, gw, mask, flags, 0 */
|
|
|
|
error = rtrequest(RTM_ADD, &dst, &gw, &mask,
|
1999-02-21 18:07:49 +03:00
|
|
|
(RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
|
1997-05-28 03:37:39 +04:00
|
|
|
if (error) {
|
1997-08-29 20:12:49 +04:00
|
|
|
printf("nfs_boot: add route, error=%d\n", error);
|
1997-05-28 03:37:39 +04:00
|
|
|
error = 0;
|
|
|
|
}
|
1994-04-18 10:18:05 +04:00
|
|
|
}
|
|
|
|
|
1999-02-21 18:07:49 +03:00
|
|
|
static int nfs_boot_delroute __P((struct radix_node *, void *));
|
|
|
|
static int
|
|
|
|
nfs_boot_delroute(rn, w)
|
|
|
|
struct radix_node *rn;
|
|
|
|
void *w;
|
|
|
|
{
|
|
|
|
struct rtentry *rt = (struct rtentry *)rn;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (rt->rt_ifp != (struct ifnet *)w)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
error = rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
|
|
|
|
if (error)
|
|
|
|
printf("nfs_boot: del route, error=%d\n", error);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nfs_boot_flushrt(ifp)
|
|
|
|
struct ifnet *ifp;
|
|
|
|
{
|
|
|
|
|
|
|
|
rn_walktree(rt_tables[AF_INET], nfs_boot_delroute, ifp);
|
|
|
|
}
|
|
|
|
|
1997-08-29 20:12:49 +04:00
|
|
|
/*
|
|
|
|
* Get an initial NFS file handle using Sun RPC/mountd.
|
|
|
|
* Separate function because we used to call it twice.
|
|
|
|
* (once for root and once for swap)
|
|
|
|
*/
|
1997-09-10 01:39:17 +04:00
|
|
|
static int
|
1997-05-28 03:37:39 +04:00
|
|
|
nfs_boot_getfh(ndm)
|
|
|
|
struct nfs_dlmount *ndm; /* output */
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
1996-10-20 17:13:22 +04:00
|
|
|
struct nfs_args *args;
|
1996-02-13 20:53:32 +03:00
|
|
|
struct sockaddr_in *sin;
|
1997-05-28 03:37:39 +04:00
|
|
|
char *pathname;
|
1994-06-13 19:28:55 +04:00
|
|
|
int error;
|
1997-08-29 20:12:49 +04:00
|
|
|
u_int16_t port;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1997-05-28 03:37:39 +04:00
|
|
|
args = &ndm->ndm_args;
|
1996-10-20 17:13:22 +04:00
|
|
|
|
|
|
|
/* Initialize mount args. */
|
1998-08-10 00:51:08 +04:00
|
|
|
memset((caddr_t) args, 0, sizeof(*args));
|
1997-05-28 03:37:39 +04:00
|
|
|
args->addr = &ndm->ndm_saddr;
|
1996-10-20 17:13:22 +04:00
|
|
|
args->addrlen = args->addr->sa_len;
|
|
|
|
#ifdef NFS_BOOT_TCP
|
|
|
|
args->sotype = SOCK_STREAM;
|
|
|
|
#else
|
|
|
|
args->sotype = SOCK_DGRAM;
|
|
|
|
#endif
|
1997-05-28 03:37:39 +04:00
|
|
|
args->fh = ndm->ndm_fh;
|
|
|
|
args->hostname = ndm->ndm_host;
|
1999-07-08 01:29:29 +04:00
|
|
|
args->flags = NFSMNT_NFSV3 | NFSMNT_NOCONN | NFSMNT_RESVPORT;
|
1996-10-20 17:13:22 +04:00
|
|
|
|
|
|
|
#ifdef NFS_BOOT_OPTIONS
|
|
|
|
args->flags |= NFS_BOOT_OPTIONS;
|
|
|
|
#endif
|
|
|
|
#ifdef NFS_BOOT_RWSIZE
|
|
|
|
/*
|
|
|
|
* Reduce rsize,wsize for interfaces that consistently
|
|
|
|
* drop fragments of long UDP messages. (i.e. wd8003).
|
|
|
|
* You can always change these later via remount.
|
|
|
|
*/
|
|
|
|
args->flags |= NFSMNT_WSIZE | NFSMNT_RSIZE;
|
|
|
|
args->wsize = NFS_BOOT_RWSIZE;
|
|
|
|
args->rsize = NFS_BOOT_RWSIZE;
|
|
|
|
#endif
|
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
1997-05-28 03:37:39 +04:00
|
|
|
* Find the pathname part of the "server:pathname"
|
|
|
|
* string left in ndm->ndm_host by nfs_boot_init.
|
1994-06-13 19:28:55 +04:00
|
|
|
*/
|
1997-05-28 03:37:39 +04:00
|
|
|
pathname = strchr(ndm->ndm_host, ':');
|
|
|
|
if (pathname == 0) {
|
|
|
|
printf("nfs_boot: getfh - no pathname\n");
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
pathname++;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
1997-05-28 03:37:39 +04:00
|
|
|
* Get file handle using RPC to mountd/mount
|
1994-06-13 19:28:55 +04:00
|
|
|
*/
|
1997-05-28 03:37:39 +04:00
|
|
|
sin = (struct sockaddr_in *)&ndm->ndm_saddr;
|
1996-10-20 17:13:22 +04:00
|
|
|
error = md_mount(sin, pathname, args);
|
1997-05-28 03:37:39 +04:00
|
|
|
if (error) {
|
|
|
|
printf("nfs_boot: mountd `%s', error=%d\n",
|
1997-08-29 20:12:49 +04:00
|
|
|
ndm->ndm_host, error);
|
1997-05-28 03:37:39 +04:00
|
|
|
return (error);
|
|
|
|
}
|
1994-06-21 12:30:20 +04:00
|
|
|
|
1996-02-13 20:53:32 +03:00
|
|
|
/* Set port number for NFS use. */
|
|
|
|
/* XXX: NFS port is always 2049, right? */
|
1996-10-20 17:13:22 +04:00
|
|
|
#ifdef NFS_BOOT_TCP
|
|
|
|
retry:
|
|
|
|
#endif
|
|
|
|
error = krpc_portmap(sin, NFS_PROG,
|
|
|
|
(args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
|
|
|
|
(args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
|
1997-08-29 20:12:49 +04:00
|
|
|
&port);
|
|
|
|
if (port == htons(0))
|
1997-05-28 03:37:39 +04:00
|
|
|
error = EIO;
|
1997-08-29 20:12:49 +04:00
|
|
|
if (error) {
|
1996-10-20 17:13:22 +04:00
|
|
|
#ifdef NFS_BOOT_TCP
|
|
|
|
if (args->sotype == SOCK_STREAM) {
|
|
|
|
args->sotype = SOCK_DGRAM;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#endif
|
1997-05-28 03:37:39 +04:00
|
|
|
printf("nfs_boot: portmap NFS, error=%d\n", error);
|
1997-08-29 20:12:49 +04:00
|
|
|
return (error);
|
1996-10-20 17:13:22 +04:00
|
|
|
}
|
1997-08-29 20:12:49 +04:00
|
|
|
sin->sin_port = port;
|
|
|
|
return (0);
|
1994-04-18 10:18:05 +04:00
|
|
|
}
|
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
1994-06-13 19:28:55 +04:00
|
|
|
* RPC: mountd/mount
|
|
|
|
* Given a server pathname, get an NFS file handle.
|
|
|
|
* Also, sets sin->sin_port to the NFS service port.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1994-06-13 19:28:55 +04:00
|
|
|
static int
|
1996-10-20 17:13:22 +04:00
|
|
|
md_mount(mdsin, path, argp)
|
1994-06-13 19:28:55 +04:00
|
|
|
struct sockaddr_in *mdsin; /* mountd server address */
|
|
|
|
char *path;
|
1996-10-20 17:13:22 +04:00
|
|
|
struct nfs_args *argp;
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
1994-06-13 19:28:55 +04:00
|
|
|
/* The RPC structures */
|
|
|
|
struct rdata {
|
1995-12-20 02:07:11 +03:00
|
|
|
u_int32_t errno;
|
1996-10-20 17:13:22 +04:00
|
|
|
union {
|
|
|
|
u_int8_t v2fh[NFSX_V2FH];
|
|
|
|
struct {
|
|
|
|
u_int32_t fhlen;
|
|
|
|
u_int8_t fh[1];
|
|
|
|
} v3fh;
|
|
|
|
} fh;
|
1994-06-13 19:28:55 +04:00
|
|
|
} *rdata;
|
|
|
|
struct mbuf *m;
|
1996-10-20 17:13:22 +04:00
|
|
|
u_int8_t *fh;
|
|
|
|
int minlen, error;
|
1997-08-29 20:12:49 +04:00
|
|
|
int mntver;
|
|
|
|
|
|
|
|
mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2;
|
|
|
|
do {
|
|
|
|
/*
|
|
|
|
* Get port number for MOUNTD.
|
|
|
|
*/
|
|
|
|
error = krpc_portmap(mdsin, RPCPROG_MNT, mntver,
|
|
|
|
IPPROTO_UDP, &mdsin->sin_port);
|
|
|
|
if (error)
|
|
|
|
continue;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1997-08-29 20:12:49 +04:00
|
|
|
/* This mbuf is consumed by krpc_call. */
|
|
|
|
m = xdr_string_encode(path, strlen(path));
|
|
|
|
if (m == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
/* Do RPC to mountd. */
|
|
|
|
error = krpc_call(mdsin, RPCPROG_MNT, mntver,
|
|
|
|
RPCMNT_MOUNT, &m, NULL);
|
|
|
|
if (error != EPROGMISMATCH)
|
|
|
|
break;
|
|
|
|
/* Try lower version of mountd. */
|
|
|
|
} while (--mntver >= 1);
|
1996-10-20 17:13:22 +04:00
|
|
|
if (error) {
|
1997-08-29 20:12:49 +04:00
|
|
|
printf("nfs_boot: mountd error=%d\n", error);
|
|
|
|
return error;
|
1996-10-20 17:13:22 +04:00
|
|
|
}
|
1997-08-29 20:12:49 +04:00
|
|
|
if (mntver != 3)
|
|
|
|
argp->flags &= ~NFSMNT_NFSV3;
|
1994-06-13 19:28:55 +04:00
|
|
|
|
1996-02-13 20:53:32 +03:00
|
|
|
/* The reply might have only the errno. */
|
|
|
|
if (m->m_len < 4)
|
|
|
|
goto bad;
|
|
|
|
/* Have at least errno, so check that. */
|
|
|
|
rdata = mtod(m, struct rdata *);
|
|
|
|
error = fxdr_unsigned(u_int32_t, rdata->errno);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
1997-08-29 20:12:49 +04:00
|
|
|
/* Have errno==0, so the fh must be there. */
|
|
|
|
if (mntver == 3) {
|
1996-10-20 17:13:22 +04:00
|
|
|
argp->fhsize = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
|
|
|
|
if (argp->fhsize > NFSX_V3FHMAX)
|
1995-04-25 01:54:56 +04:00
|
|
|
goto bad;
|
1996-10-20 17:13:22 +04:00
|
|
|
minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
|
|
|
|
} else {
|
|
|
|
argp->fhsize = NFSX_V2FH;
|
|
|
|
minlen = sizeof(u_int32_t) + argp->fhsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->m_len < minlen) {
|
|
|
|
m = m_pullup(m, minlen);
|
|
|
|
if (m == NULL)
|
|
|
|
return(EBADRPC);
|
1996-02-13 20:53:32 +03:00
|
|
|
rdata = mtod(m, struct rdata *);
|
1995-04-25 01:54:56 +04:00
|
|
|
}
|
1996-10-20 17:13:22 +04:00
|
|
|
|
1997-08-29 20:12:49 +04:00
|
|
|
fh = (mntver == 3) ?
|
|
|
|
rdata->fh.v3fh.fh : rdata->fh.v2fh;
|
1998-08-10 00:51:08 +04:00
|
|
|
memcpy(argp->fh, fh, argp->fhsize);
|
1996-10-20 17:13:22 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
goto out;
|
|
|
|
|
1994-06-29 11:28:36 +04:00
|
|
|
bad:
|
1994-06-13 19:28:55 +04:00
|
|
|
error = EBADRPC;
|
|
|
|
|
1994-06-29 11:28:36 +04:00
|
|
|
out:
|
1994-06-13 19:28:55 +04:00
|
|
|
m_freem(m);
|
|
|
|
return error;
|
1994-04-18 10:18:05 +04:00
|
|
|
}
|