1994-09-26 19:42:29 +03:00
|
|
|
/* $NetBSD: nfs_boot.c,v 1.11 1994/09/26 16:42:33 gwr Exp $ */
|
1994-06-29 10:39:25 +04:00
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994 Adam Glass, Gordon Ross
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
1994-06-13 19:28:55 +04:00
|
|
|
* 3. The name of the authors may not be used to endorse or promote products
|
1994-04-18 10:18:05 +04:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
1994-06-13 19:28:55 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.
|
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>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
1994-06-13 19:28:55 +04:00
|
|
|
#include <net/route.h>
|
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
#include <netinet/in.h>
|
1994-06-13 19:28:55 +04:00
|
|
|
#include <netinet/if_ether.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
#include <nfs/rpcv2.h>
|
|
|
|
#include <nfs/nfsv2.h>
|
|
|
|
#include <nfs/nfs.h>
|
|
|
|
#include <nfs/nfsdiskless.h>
|
1994-09-26 19:42:29 +03:00
|
|
|
#include <nfs/krpc.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-07-16 15:43:12 +04:00
|
|
|
#include "ether.h"
|
1994-09-26 19:42:29 +03:00
|
|
|
#if NETHER == 0
|
|
|
|
|
|
|
|
int nfs_boot_init(nd, procp)
|
|
|
|
struct nfs_diskless *nd;
|
|
|
|
struct proc *procp;
|
|
|
|
{
|
|
|
|
panic("nfs_boot_init: no ether");
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* NETHER */
|
1994-07-16 15:43:12 +04:00
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
|
|
|
* Support for NFS diskless booting, specifically getting information
|
|
|
|
* about where to boot from, what pathnames, etc.
|
|
|
|
*
|
1994-06-13 19:28:55 +04:00
|
|
|
* This implememtation uses RARP and the bootparam RPC.
|
|
|
|
* We are forced to implement RPC anyway (to get file handles)
|
|
|
|
* so we might as well take advantage of it for bootparam too.
|
1994-04-18 10:18:05 +04:00
|
|
|
*
|
1994-06-13 19:28:55 +04:00
|
|
|
* The diskless boot sequence goes as follows:
|
|
|
|
* (1) Get our interface address using RARP
|
|
|
|
* (also save the address of the RARP server)
|
|
|
|
* (2) Get our hostname using RPC/bootparam/whoami
|
|
|
|
* (all boopararms RPCs to the RARP server)
|
|
|
|
* (3) Get the root path using RPC/bootparam/getfile
|
|
|
|
* (4) Get the root file handle using RPC/mountd
|
|
|
|
* (5) Get the swap path using RPC/bootparam/getfile
|
|
|
|
* (6) Get the swap file handle using RPC/mountd
|
1994-04-18 10:18:05 +04:00
|
|
|
*
|
1994-06-13 19:28:55 +04:00
|
|
|
* (This happens to be the way Sun does it too.)
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/* bootparam RPC */
|
1994-06-29 11:28:36 +04:00
|
|
|
static int bp_whoami __P((struct sockaddr_in *bpsin,
|
|
|
|
struct in_addr *my_ip, struct in_addr *gw_ip));
|
|
|
|
static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
|
|
|
|
struct sockaddr_in *mdsin, char *servname, char *path));
|
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,
|
|
|
|
u_char *fh));
|
1994-06-13 19:28:55 +04:00
|
|
|
|
|
|
|
/* other helpers */
|
1994-06-29 11:28:36 +04:00
|
|
|
static void get_path_and_handle __P((struct sockaddr_in *bpsin,
|
|
|
|
char *key, struct nfs_dlmount *ndmntp));
|
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.
|
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 ifreq ireq;
|
1994-09-26 19:42:29 +03:00
|
|
|
struct in_addr my_ip, gw_ip;
|
1994-06-13 19:28:55 +04:00
|
|
|
struct sockaddr_in bp_sin;
|
1994-04-18 10:18:05 +04:00
|
|
|
struct sockaddr_in *sin;
|
1994-06-13 19:28:55 +04:00
|
|
|
struct ifnet *ifp;
|
|
|
|
struct socket *so;
|
|
|
|
int error, len;
|
|
|
|
u_short port;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
#if 0
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
1994-06-13 19:28:55 +04:00
|
|
|
* XXX time must be non-zero when we init the interface or else
|
|
|
|
* the arp code will wedge... (Fixed in if_ether.c -gwr)
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1994-06-13 19:28:55 +04:00
|
|
|
if (time.tv_sec == 0)
|
|
|
|
time.tv_sec = 1;
|
|
|
|
#endif
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
|
|
|
* Find an interface, rarp for its ip address, stuff it, the
|
|
|
|
* implied broadcast addr, and netmask into a nfs_diskless struct.
|
|
|
|
*
|
|
|
|
* This was moved here from nfs_vfsops.c because this procedure
|
|
|
|
* would be quite different if someone decides to write (i.e.) a
|
|
|
|
* BOOTP version of this file (might not use RARP, etc.) -gwr
|
|
|
|
*/
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
|
|
|
* Find a network interface.
|
|
|
|
* XXX - This should use the specified boot device.
|
|
|
|
*/
|
|
|
|
for (ifp = ifnet; ifp; ifp = ifp->if_next)
|
|
|
|
if ((ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
|
|
|
|
break;
|
|
|
|
if (ifp == NULL)
|
|
|
|
panic("nfs_boot: no suitable interface");
|
|
|
|
sprintf(ireq.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
|
|
|
|
printf("nfs_boot: using network interface '%s'\n",
|
1994-06-29 11:28:36 +04:00
|
|
|
ireq.ifr_name);
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
|
|
|
* Bring up the interface.
|
|
|
|
*/
|
|
|
|
if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0)
|
|
|
|
panic("nfs_boot: socreate, error=%d", error);
|
|
|
|
ireq.ifr_flags = IFF_UP;
|
|
|
|
error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
|
1994-06-29 11:28:36 +04:00
|
|
|
if (error)
|
|
|
|
panic("nfs_boot: SIFFLAGS, error=%d", error);
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
|
|
|
* Do RARP for the interface address. Also
|
|
|
|
* save the server address for bootparam RPC.
|
|
|
|
*/
|
1994-09-26 19:42:29 +03:00
|
|
|
if ((error = revarpwhoami(&my_ip, ifp)) != 0)
|
1994-06-13 19:28:55 +04:00
|
|
|
panic("revarp failed, error=%d", error);
|
1994-09-26 19:42:29 +03:00
|
|
|
printf("nfs_boot: client_addr=0x%x\n", ntohl(my_ip.s_addr));
|
1994-06-13 19:28:55 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do enough of ifconfig(8) so that the chosen interface can
|
|
|
|
* talk to the server(s). (also get brcast addr and netmask)
|
|
|
|
*/
|
|
|
|
/* Set interface address. */
|
|
|
|
sin = (struct sockaddr_in *)&ireq.ifr_addr;
|
1994-08-12 03:47:51 +04:00
|
|
|
bzero((caddr_t)sin, sizeof(*sin));
|
1994-06-13 19:28:55 +04:00
|
|
|
sin->sin_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = my_ip.s_addr;
|
|
|
|
error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
|
1994-06-29 11:28:36 +04:00
|
|
|
if (error)
|
|
|
|
panic("nfs_boot: set if addr, error=%d", error);
|
1994-06-13 19:28:55 +04:00
|
|
|
|
|
|
|
soclose(so);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get client name and gateway address.
|
|
|
|
* RPC: bootparam/whoami
|
1994-09-26 19:42:29 +03:00
|
|
|
* XXX - Using old broadcast addr. for WHOAMI,
|
|
|
|
* then it is replaced with the BP server addr.
|
1994-06-13 19:28:55 +04:00
|
|
|
*/
|
1994-08-12 03:47:51 +04:00
|
|
|
bzero((caddr_t)&bp_sin, sizeof(bp_sin));
|
1994-06-13 19:28:55 +04:00
|
|
|
bp_sin.sin_len = sizeof(bp_sin);
|
|
|
|
bp_sin.sin_family = AF_INET;
|
1994-09-26 19:42:29 +03:00
|
|
|
bp_sin.sin_addr.s_addr = ~0; /* XXX */
|
1994-06-13 19:28:55 +04:00
|
|
|
hostnamelen = MAXHOSTNAMELEN;
|
1994-06-29 11:28:36 +04:00
|
|
|
|
|
|
|
/* this returns gateway IP address */
|
|
|
|
error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
|
1994-06-13 19:28:55 +04:00
|
|
|
if (error)
|
|
|
|
panic("nfs_boot: bootparam whoami, error=%d", error);
|
1994-09-26 19:42:29 +03:00
|
|
|
printf("nfs_boot: server_addr=0x%x\n",
|
|
|
|
ntohl(bp_sin.sin_addr.s_addr));
|
1994-06-13 19:28:55 +04:00
|
|
|
printf("nfs_boot: hostname=%s\n", hostname);
|
|
|
|
|
|
|
|
#ifdef NFS_BOOT_GATEWAY
|
|
|
|
/*
|
1994-07-19 06:23:27 +04:00
|
|
|
* XXX - This code is conditionally compiled only because
|
|
|
|
* many bootparam servers (in particular, SunOS 4.1.3)
|
|
|
|
* always set the gateway address to their own address.
|
|
|
|
* The bootparam server is not necessarily the gateway.
|
|
|
|
* We could just believe the server, and at worst you would
|
|
|
|
* need to delete the incorrect default route before adding
|
|
|
|
* the correct one, but for simplicity, ignore the gateway.
|
1994-06-13 19:28:55 +04:00
|
|
|
* If your server is OK, you can turn on this option.
|
|
|
|
*
|
|
|
|
* If the gateway address is set, add a default route.
|
|
|
|
* (The mountd RPCs may go across a gateway.)
|
|
|
|
*/
|
|
|
|
if (gw_ip.s_addr) {
|
1994-07-19 06:23:27 +04:00
|
|
|
struct sockaddr dst, gw, mask;
|
1994-06-13 19:28:55 +04:00
|
|
|
/* Destination: (default) */
|
1994-08-12 03:47:51 +04:00
|
|
|
bzero((caddr_t)&dst, sizeof(dst));
|
1994-07-19 06:23:27 +04:00
|
|
|
dst.sa_len = sizeof(dst);
|
|
|
|
dst.sa_family = AF_INET;
|
1994-06-13 19:28:55 +04:00
|
|
|
/* Gateway: */
|
1994-08-12 03:47:51 +04:00
|
|
|
bzero((caddr_t)&gw, sizeof(gw));
|
1994-07-19 06:23:27 +04:00
|
|
|
sin = (struct sockaddr_in *)&gw;
|
|
|
|
sin->sin_len = sizeof(gw);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = gw_ip.s_addr;
|
|
|
|
/* Mask: (zero length) */
|
|
|
|
bzero(&mask, sizeof(mask));
|
|
|
|
|
|
|
|
printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
|
1994-06-13 19:28:55 +04:00
|
|
|
/* add, dest, gw, mask, flags, 0 */
|
1994-07-19 06:23:27 +04:00
|
|
|
error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
|
|
|
|
&mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
|
1994-06-13 19:28:55 +04:00
|
|
|
if (error)
|
|
|
|
printf("nfs_boot: add route, error=%d\n", error);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
get_path_and_handle(&bp_sin, "root", &nd->nd_root);
|
|
|
|
get_path_and_handle(&bp_sin, "swap", &nd->nd_swap);
|
|
|
|
|
|
|
|
return (0);
|
1994-04-18 10:18:05 +04:00
|
|
|
}
|
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
static void
|
|
|
|
get_path_and_handle(bpsin, key, ndmntp)
|
|
|
|
struct sockaddr_in *bpsin; /* bootparam server */
|
1994-06-29 11:28:36 +04:00
|
|
|
char *key; /* root or swap */
|
|
|
|
struct nfs_dlmount *ndmntp; /* output */
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
1994-06-13 19:28:55 +04:00
|
|
|
char pathname[MAXPATHLEN];
|
1994-06-21 12:30:20 +04:00
|
|
|
char *sp, *dp, *endp;
|
1994-06-13 19:28:55 +04:00
|
|
|
int error;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
|
|
|
* Get server:pathname for "key" (root or swap)
|
|
|
|
* using RPC to bootparam/getfile
|
|
|
|
*/
|
1994-06-29 11:28:36 +04:00
|
|
|
error = bp_getfile(bpsin, key, &ndmntp->ndm_saddr,
|
|
|
|
ndmntp->ndm_host, pathname);
|
1994-06-13 19:28:55 +04:00
|
|
|
if (error)
|
|
|
|
panic("nfs_boot: bootparam get %s: %d", key, error);
|
|
|
|
printf("%s on %s:%s\n", key, ndmntp->ndm_host, pathname);
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
|
|
|
* Get file handle for "key" (root or swap)
|
|
|
|
* using RPC to mountd/mount
|
|
|
|
*/
|
1994-06-29 11:28:36 +04:00
|
|
|
error = md_mount(&ndmntp->ndm_saddr, pathname, ndmntp->ndm_fh);
|
1994-04-18 10:18:05 +04:00
|
|
|
if (error)
|
1994-06-13 19:28:55 +04:00
|
|
|
panic("nfs_boot: mountd %s, error=%d", key, error);
|
1994-06-21 12:30:20 +04:00
|
|
|
|
|
|
|
/* Construct remote path (for getmntinfo(3)) */
|
|
|
|
dp = ndmntp->ndm_host;
|
|
|
|
endp = dp + MNAMELEN - 1;
|
|
|
|
dp += strlen(dp);
|
|
|
|
*dp++ = ':';
|
|
|
|
for (sp = pathname; *sp && dp < endp;)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
1994-06-29 11:28:36 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get an mbuf with the given length, and
|
|
|
|
* initialize the pkthdr length field.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
m_get_len(int msg_len)
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
m = m_gethdr(M_WAIT, MT_DATA);
|
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (msg_len > MHLEN) {
|
|
|
|
if (msg_len > MCLBYTES)
|
|
|
|
panic("nfs_boot: msg_len > MCLBYTES");
|
|
|
|
MCLGET(m, M_WAIT);
|
1994-04-18 10:18:05 +04:00
|
|
|
if (m == NULL)
|
1994-06-13 19:28:55 +04:00
|
|
|
return NULL;
|
1994-04-18 10:18:05 +04:00
|
|
|
}
|
1994-06-13 19:28:55 +04:00
|
|
|
m->m_len = msg_len;
|
|
|
|
m->m_pkthdr.len = m->m_len;
|
|
|
|
return (m);
|
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
|
|
|
* String representation for RPC.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1994-06-13 19:28:55 +04:00
|
|
|
struct rpc_string {
|
|
|
|
u_long len; /* length without null or padding */
|
|
|
|
u_char data[4]; /* data (longer, of course) */
|
|
|
|
/* data is padded to a long-word boundary */
|
|
|
|
};
|
|
|
|
/* Compute space used given string length. */
|
|
|
|
#define RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inet address in RPC messages
|
|
|
|
* (Note, really four longs, NOT chars. Blech.)
|
|
|
|
*/
|
|
|
|
struct bp_inaddr {
|
|
|
|
u_long atype;
|
|
|
|
long addr[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RPC: bootparam/whoami
|
|
|
|
* Given client IP address, get:
|
|
|
|
* client name (hostname)
|
|
|
|
* domain name (domainname)
|
|
|
|
* gateway address
|
|
|
|
*
|
|
|
|
* Setting the hostname and domainname here may be somewhat
|
|
|
|
* controvercial, but it is so easy to do it here. -gwr
|
1994-09-26 19:42:29 +03:00
|
|
|
*
|
|
|
|
* Note - bpsin is initialized to the broadcast address,
|
|
|
|
* and will be replaced with the bootparam server address
|
|
|
|
* after this call is complete. Have to use PMAP_PROC_CALL
|
|
|
|
* to make sure we get responses only from a servers that
|
|
|
|
* know about us (don't want to broadcast a getport call).
|
1994-06-13 19:28:55 +04:00
|
|
|
*/
|
|
|
|
static int
|
1994-06-29 11:28:36 +04:00
|
|
|
bp_whoami(bpsin, my_ip, gw_ip)
|
|
|
|
struct sockaddr_in *bpsin;
|
|
|
|
struct in_addr *my_ip;
|
|
|
|
struct in_addr *gw_ip;
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
1994-09-26 19:42:29 +03:00
|
|
|
/* RPC structures for PMAPPROC_CALLIT */
|
|
|
|
struct whoami_call {
|
|
|
|
u_long call_prog;
|
|
|
|
u_long call_vers;
|
|
|
|
u_long call_proc;
|
|
|
|
u_long call_arglen;
|
|
|
|
struct bp_inaddr call_ia;
|
|
|
|
} *call;
|
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
struct rpc_string *str;
|
1994-09-26 19:42:29 +03:00
|
|
|
struct bp_inaddr *bia;
|
|
|
|
struct mbuf *m, *from;
|
1994-06-13 19:28:55 +04:00
|
|
|
struct sockaddr_in *sin;
|
|
|
|
int error, msg_len;
|
|
|
|
int cn_len, dn_len;
|
|
|
|
u_char *p;
|
1994-09-26 19:42:29 +03:00
|
|
|
long *lp;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
|
|
|
* Get message buffer of sufficient size.
|
|
|
|
*/
|
1994-09-26 19:42:29 +03:00
|
|
|
msg_len = sizeof(*call);
|
1994-06-13 19:28:55 +04:00
|
|
|
m = m_get_len(msg_len);
|
|
|
|
if (m == NULL)
|
|
|
|
return ENOBUFS;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
1994-09-26 19:42:29 +03:00
|
|
|
* Build request message for PMAPPROC_CALLIT.
|
1994-06-13 19:28:55 +04:00
|
|
|
*/
|
1994-09-26 19:42:29 +03:00
|
|
|
call = mtod(m, struct whoami_call *);
|
|
|
|
call->call_prog = BOOTPARAM_PROG;
|
|
|
|
call->call_vers = BOOTPARAM_VERS;
|
|
|
|
call->call_proc = BOOTPARAM_WHOAMI;
|
|
|
|
call->call_arglen = sizeof(struct bp_inaddr);
|
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/* client IP address */
|
1994-09-26 19:42:29 +03:00
|
|
|
call->call_ia.atype = htonl(1);
|
|
|
|
p = (u_char*)my_ip;
|
|
|
|
lp = call->call_ia.addr;
|
|
|
|
*lp++ = htonl(*p); p++;
|
|
|
|
*lp++ = htonl(*p); p++;
|
|
|
|
*lp++ = htonl(*p); p++;
|
|
|
|
*lp++ = htonl(*p); p++;
|
|
|
|
|
|
|
|
/* RPC: portmap/callit */
|
|
|
|
bpsin->sin_port = htons(PMAPPORT);
|
|
|
|
from = NULL;
|
|
|
|
error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
|
|
|
|
PMAPPROC_CALLIT, &m, &from);
|
1994-04-18 10:18:05 +04:00
|
|
|
if (error)
|
|
|
|
return error;
|
1994-06-13 19:28:55 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse result message.
|
|
|
|
*/
|
|
|
|
msg_len = m->m_len;
|
1994-09-26 19:42:29 +03:00
|
|
|
lp = mtod(m, long *);
|
|
|
|
|
|
|
|
/* bootparam server port (also grab from address). */
|
|
|
|
if (msg_len < sizeof(*lp))
|
|
|
|
goto bad;
|
|
|
|
msg_len -= sizeof(*lp);
|
|
|
|
bpsin->sin_port = (short) *lp++;
|
|
|
|
sin = mtod(from, struct sockaddr_in *);
|
|
|
|
bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
|
|
|
|
|
|
|
|
/* length of encapsulated results */
|
|
|
|
if (msg_len < (*lp + sizeof(*lp)))
|
|
|
|
goto bad;
|
|
|
|
msg_len = *lp++;
|
|
|
|
p = (char*)lp;
|
1994-06-13 19:28:55 +04:00
|
|
|
|
|
|
|
/* client name */
|
|
|
|
if (msg_len < sizeof(*str))
|
|
|
|
goto bad;
|
|
|
|
str = (struct rpc_string *)p;
|
|
|
|
cn_len = ntohl(str->len);
|
|
|
|
if (msg_len < cn_len)
|
|
|
|
goto bad;
|
|
|
|
if (cn_len >= MAXHOSTNAMELEN)
|
|
|
|
goto bad;
|
|
|
|
bcopy(str->data, hostname, cn_len);
|
|
|
|
hostname[cn_len] = '\0';
|
|
|
|
hostnamelen = cn_len;
|
|
|
|
p += RPC_STR_SIZE(cn_len);
|
|
|
|
msg_len -= RPC_STR_SIZE(cn_len);
|
|
|
|
|
|
|
|
/* domain name */
|
|
|
|
if (msg_len < sizeof(*str))
|
|
|
|
goto bad;
|
|
|
|
str = (struct rpc_string *)p;
|
|
|
|
dn_len = ntohl(str->len);
|
|
|
|
if (msg_len < dn_len)
|
|
|
|
goto bad;
|
|
|
|
if (dn_len >= MAXHOSTNAMELEN)
|
|
|
|
goto bad;
|
|
|
|
bcopy(str->data, domainname, dn_len);
|
|
|
|
domainname[dn_len] = '\0';
|
|
|
|
domainnamelen = dn_len;
|
|
|
|
p += RPC_STR_SIZE(dn_len);
|
|
|
|
msg_len -= RPC_STR_SIZE(dn_len);
|
|
|
|
|
|
|
|
/* gateway address */
|
|
|
|
if (msg_len < sizeof(*bia))
|
|
|
|
goto bad;
|
|
|
|
bia = (struct bp_inaddr *)p;
|
|
|
|
if (bia->atype != htonl(1))
|
|
|
|
goto bad;
|
|
|
|
p = (u_char*)gw_ip;
|
|
|
|
*p++ = ntohl(bia->addr[0]);
|
|
|
|
*p++ = ntohl(bia->addr[1]);
|
|
|
|
*p++ = ntohl(bia->addr[2]);
|
|
|
|
*p++ = ntohl(bia->addr[3]);
|
|
|
|
goto out;
|
|
|
|
|
1994-06-29 11:28:36 +04:00
|
|
|
bad:
|
1994-06-13 19:28:55 +04:00
|
|
|
printf("nfs_boot: bootparam_whoami: bad reply\n");
|
|
|
|
error = EBADRPC;
|
|
|
|
|
1994-06-29 11:28:36 +04:00
|
|
|
out:
|
1994-09-26 19:42:29 +03:00
|
|
|
if (from)
|
|
|
|
m_freem(from);
|
1994-06-13 19:28:55 +04:00
|
|
|
m_freem(m);
|
|
|
|
return(error);
|
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: bootparam/getfile
|
|
|
|
* Given client name and file "key", get:
|
|
|
|
* server name
|
|
|
|
* server IP address
|
|
|
|
* server pathname
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1994-06-13 19:28:55 +04:00
|
|
|
static int
|
1994-06-29 11:28:36 +04:00
|
|
|
bp_getfile(bpsin, key, md_sin, serv_name, pathname)
|
|
|
|
struct sockaddr_in *bpsin;
|
|
|
|
char *key;
|
|
|
|
struct sockaddr_in *md_sin;
|
|
|
|
char *serv_name;
|
|
|
|
char *pathname;
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
1994-06-13 19:28:55 +04:00
|
|
|
struct rpc_string *str;
|
|
|
|
struct mbuf *m;
|
|
|
|
struct bp_inaddr *bia;
|
1994-04-18 10:18:05 +04:00
|
|
|
struct sockaddr_in *sin;
|
1994-06-13 19:28:55 +04:00
|
|
|
u_char *p, *q;
|
|
|
|
int error, msg_len;
|
|
|
|
int cn_len, key_len, sn_len, path_len;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/*
|
|
|
|
* Get message buffer of sufficient size.
|
|
|
|
*/
|
|
|
|
cn_len = hostnamelen;
|
|
|
|
key_len = strlen(key);
|
|
|
|
msg_len = 0;
|
|
|
|
msg_len += RPC_STR_SIZE(cn_len);
|
|
|
|
msg_len += RPC_STR_SIZE(key_len);
|
|
|
|
m = m_get_len(msg_len);
|
|
|
|
if (m == NULL)
|
|
|
|
return ENOBUFS;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
/*
|
1994-06-13 19:28:55 +04:00
|
|
|
* Build request message.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1994-06-13 19:28:55 +04:00
|
|
|
p = mtod(m, u_char *);
|
|
|
|
bzero(p, msg_len);
|
|
|
|
/* client name (hostname) */
|
|
|
|
str = (struct rpc_string *)p;
|
|
|
|
str->len = htonl(cn_len);
|
|
|
|
bcopy(hostname, str->data, cn_len);
|
|
|
|
p += RPC_STR_SIZE(cn_len);
|
|
|
|
/* key name (root or swap) */
|
|
|
|
str = (struct rpc_string *)p;
|
|
|
|
str->len = htonl(key_len);
|
|
|
|
bcopy(key, str->data, key_len);
|
|
|
|
|
|
|
|
/* RPC: bootparam/getfile */
|
1994-09-26 19:42:29 +03:00
|
|
|
error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
|
|
|
|
BOOTPARAM_GETFILE, &m, NULL);
|
1994-06-13 19:28:55 +04:00
|
|
|
if (error)
|
1994-04-18 10:18:05 +04:00
|
|
|
return error;
|
|
|
|
|
|
|
|
/*
|
1994-06-13 19:28:55 +04:00
|
|
|
* Parse result message.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1994-06-13 19:28:55 +04:00
|
|
|
p = mtod(m, u_char *);
|
|
|
|
msg_len = m->m_len;
|
|
|
|
|
|
|
|
/* server name */
|
|
|
|
if (msg_len < sizeof(*str))
|
|
|
|
goto bad;
|
|
|
|
str = (struct rpc_string *)p;
|
|
|
|
sn_len = ntohl(str->len);
|
|
|
|
if (msg_len < sn_len)
|
|
|
|
goto bad;
|
|
|
|
if (sn_len >= MNAMELEN)
|
|
|
|
goto bad;
|
|
|
|
bcopy(str->data, serv_name, sn_len);
|
|
|
|
serv_name[sn_len] = '\0';
|
|
|
|
p += RPC_STR_SIZE(sn_len);
|
|
|
|
msg_len -= RPC_STR_SIZE(sn_len);
|
|
|
|
|
|
|
|
/* server IP address (mountd) */
|
|
|
|
if (msg_len < sizeof(*bia))
|
|
|
|
goto bad;
|
|
|
|
bia = (struct bp_inaddr *)p;
|
|
|
|
if (bia->atype != htonl(1))
|
|
|
|
goto bad;
|
|
|
|
sin = md_sin;
|
1994-08-12 03:47:51 +04:00
|
|
|
bzero((caddr_t)sin, sizeof(*sin));
|
1994-06-13 19:28:55 +04:00
|
|
|
sin->sin_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
q = (u_char*) &sin->sin_addr;
|
|
|
|
*q++ = ntohl(bia->addr[0]);
|
|
|
|
*q++ = ntohl(bia->addr[1]);
|
|
|
|
*q++ = ntohl(bia->addr[2]);
|
|
|
|
*q++ = ntohl(bia->addr[3]);
|
|
|
|
p += sizeof(*bia);
|
|
|
|
msg_len -= sizeof(*bia);
|
|
|
|
|
|
|
|
/* server pathname */
|
|
|
|
if (msg_len < sizeof(*str))
|
|
|
|
goto bad;
|
|
|
|
str = (struct rpc_string *)p;
|
|
|
|
path_len = ntohl(str->len);
|
|
|
|
if (msg_len < path_len)
|
|
|
|
goto bad;
|
|
|
|
if (path_len >= MAXPATHLEN)
|
|
|
|
goto bad;
|
|
|
|
bcopy(str->data, pathname, path_len);
|
|
|
|
pathname[path_len] = '\0';
|
|
|
|
goto out;
|
|
|
|
|
1994-06-29 11:28:36 +04:00
|
|
|
bad:
|
1994-06-13 19:28:55 +04:00
|
|
|
printf("nfs_boot: bootparam_getfile: bad reply\n");
|
|
|
|
error = EBADRPC;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-29 11:28:36 +04:00
|
|
|
out:
|
1994-06-13 19:28:55 +04:00
|
|
|
m_freem(m);
|
|
|
|
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
|
|
|
|
md_mount(mdsin, path, fhp)
|
|
|
|
struct sockaddr_in *mdsin; /* mountd server address */
|
|
|
|
char *path;
|
|
|
|
u_char *fhp;
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
1994-06-13 19:28:55 +04:00
|
|
|
/* The RPC structures */
|
|
|
|
struct rpc_string *str;
|
|
|
|
struct rdata {
|
|
|
|
u_long errno;
|
|
|
|
u_char fh[NFS_FHSIZE];
|
|
|
|
} *rdata;
|
|
|
|
struct mbuf *m;
|
|
|
|
int error, mlen, slen;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-09-26 19:42:29 +03:00
|
|
|
/* Get port number for MOUNTD. */
|
|
|
|
error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
|
|
|
|
&mdsin->sin_port);
|
|
|
|
if (error) return error;
|
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
slen = strlen(path);
|
|
|
|
mlen = RPC_STR_SIZE(slen);
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
m = m_get_len(mlen);
|
|
|
|
if (m == NULL)
|
|
|
|
return ENOBUFS;
|
|
|
|
str = mtod(m, struct rpc_string *);
|
|
|
|
str->len = htonl(slen);
|
|
|
|
bcopy(path, str->data, slen);
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/* Do RPC to mountd. */
|
1994-09-26 19:42:29 +03:00
|
|
|
error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
|
|
|
|
RPCMNT_MOUNT, &m, NULL);
|
1994-06-29 11:28:36 +04:00
|
|
|
if (error)
|
1994-06-13 19:28:55 +04:00
|
|
|
return error; /* message already freed */
|
|
|
|
|
|
|
|
mlen = m->m_len;
|
|
|
|
if (mlen < sizeof(*rdata))
|
|
|
|
goto bad;
|
|
|
|
rdata = mtod(m, struct rdata *);
|
|
|
|
error = ntohl(rdata->errno);
|
1994-04-18 10:18:05 +04:00
|
|
|
if (error)
|
1994-06-13 19:28:55 +04:00
|
|
|
goto bad;
|
|
|
|
bcopy(rdata->fh, fhp, NFS_FHSIZE);
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1994-06-13 19:28:55 +04:00
|
|
|
/* Set port number for NFS use. */
|
1994-09-26 19:42:29 +03:00
|
|
|
error = krpc_portmap(mdsin, NFS_PROG, NFS_VER2,
|
|
|
|
&mdsin->sin_port);
|
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
|
|
|
}
|
1994-07-16 15:43:12 +04:00
|
|
|
|
|
|
|
#endif /* NETHER */
|