#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nfs_bootparam.c,v 1.40 2024/07/05 04:31:54 rin Exp $");
#ifdef _KERNEL_OPT
#include "opt_nfs_boot.h"
#include "arp.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/mbuf.h>
#include <sys/reboot.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/vnode.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/if_ether.h>
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#include <nfs/rpcv2.h>
#include <nfs/krpc.h>
#include <nfs/xdr_subs.h>
#include <nfs/nfsproto.h>
#include <nfs/nfs.h>
#include <nfs/nfsmount.h>
#include <nfs/nfsdiskless.h>
#include <nfs/nfs_var.h>
static int bp_whoami (struct sockaddr_in *bpsin,
struct in_addr *my_ip, struct in_addr *gw_ip, struct lwp *l);
static int bp_getfile (struct sockaddr_in *bpsin, const char *key,
struct nfs_dlmount *ndm, struct lwp *l);
int
nfs_bootparam(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
{
struct ifnet *ifp = nd->nd_ifp;
struct in_addr my_ip, arps_ip, gw_ip;
struct sockaddr_in bp_sin;
struct sockaddr_in *sin;
#ifndef NFS_BOOTPARAM_NOGATEWAY
struct nfs_dlmount *gw_ndm = 0;
char *p;
u_int32_t mask;
#endif
int error;
error = nfs_boot_ifupdown(ifp, lwp, 1);
if (error) {
printf("nfs_boot: SIFFLAGS, error=%d\n", error);
return (error);
}
error = EADDRNOTAVAIL;
memset(&arps_ip, 0, sizeof(arps_ip));
#if NARP > 0
if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) {
error = revarpwhoarewe(ifp, &arps_ip, &my_ip);
}
#endif
if (error) {
printf("revarp failed, error=%d\n", error);
goto out;
}
if (!(*flags & NFS_BOOT_HAS_MYIP)) {
nd->nd_myip.s_addr = my_ip.s_addr;
printf("nfs_boot: client_addr=%s", inet_ntoa(my_ip));
printf(" (RARP from %s)\n", inet_ntoa(arps_ip));
*flags |= NFS_BOOT_HAS_MYIP;
}
error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr,
INADDR_ANY, INADDR_ANY);
if (error) {
printf("nfs_boot: set ifaddr, error=%d\n", error);
goto out;
}
sin = &bp_sin;
memset((void *)sin, 0, sizeof(*sin));
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = INADDR_BROADCAST;
error = bp_whoami(sin, &my_ip, &gw_ip, lwp);
if (error) {
printf("nfs_boot: bootparam whoami, error=%d\n", error);
goto delout;
}
*flags |= NFS_BOOT_HAS_SERVADDR | NFS_BOOT_HAS_SERVER;
printf("nfs_boot: server_addr=%s\n", inet_ntoa(sin->sin_addr));
printf("nfs_boot: hostname=%s\n", hostname);
error = bp_getfile(sin, "root", &nd->nd_root, lwp);
if (error) {
printf("nfs_boot: bootparam get root: %d\n", error);
goto delout;
}
*flags |= NFS_BOOT_HAS_ROOTPATH;
#ifndef NFS_BOOTPARAM_NOGATEWAY
gw_ndm = kmem_alloc(sizeof(*gw_ndm), KM_SLEEP);
memset((void *)gw_ndm, 0, sizeof(*gw_ndm));
error = bp_getfile(sin, "gateway", gw_ndm, lwp);
if (error) {
error = 0;
goto nogwrepl;
}
sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
if (sin->sin_addr.s_addr == 0)
goto out;
printf("nfs_boot: gateway=%s\n", inet_ntoa(sin->sin_addr));
nd->nd_gwip = sin->sin_addr;
*flags |= NFS_BOOT_HAS_GWIP;
p = strchr(gw_ndm->ndm_host, ':');
if (p == 0)
goto out;
p++;
mask = inet_addr(p);
if (mask == 0)
goto out;
nd->nd_mask.s_addr = mask;
*flags |= NFS_BOOT_HAS_MASK;
printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
(void) nfs_boot_deladdress(ifp, lwp, my_ip.s_addr);
error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr,
mask, INADDR_ANY);
if (error) {
printf("nfs_boot: set ifmask, error=%d\n", error);
goto out;
}
goto gwok;
nogwrepl:
#endif
#ifdef NFS_BOOT_GATEWAY
if (gw_ip.s_addr) {
nd->nd_gwip = gw_ip;
*flags |= NFS_BOOT_HAS_GWIP;
}
#endif
delout:
if (error)
(void) nfs_boot_deladdress(ifp, lwp, my_ip.s_addr);
out:
if (error) {
(void) nfs_boot_ifupdown(ifp, lwp, 0);
nfs_boot_flushrt(ifp);
}
#ifndef NFS_BOOTPARAM_NOGATEWAY
gwok:
if (gw_ndm)
kmem_free(gw_ndm, sizeof(*gw_ndm));
#endif
if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO)
return error ? error : EADDRNOTAVAIL;
return (error);
}
static int
bp_whoami(struct sockaddr_in *bpsin, struct in_addr *my_ip,
struct in_addr *gw_ip, struct lwp *l)
{
struct whoami_call {
u_int32_t call_prog;
u_int32_t call_vers;
u_int32_t call_proc;
u_int32_t call_arglen;
} *call;
struct callit_reply {
u_int32_t port;
u_int32_t encap_len;
} *reply;
struct mbuf *m, *from;
struct sockaddr_in *sin;
int error;
int16_t port;
m = m_get(M_WAIT, MT_DATA);
call = mtod(m, struct whoami_call *);
m->m_len = sizeof(*call);
call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
m->m_next = xdr_inaddr_encode(my_ip);
call->call_arglen = txdr_unsigned(m->m_next->m_len);
bpsin->sin_port = htons(PMAPPORT);
error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
PMAPPROC_CALLIT, &m, &from, l);
if (error) {
m_freem(m);
return error;
}
if (m->m_len < sizeof(*reply)) {
m = m_pullup(m, sizeof(*reply));
if (m == NULL)
goto bad;
}
reply = mtod(m, struct callit_reply *);
port = fxdr_unsigned(u_int32_t, reply->port);
m_adj(m, sizeof(*reply));
sin = mtod(from, struct sockaddr_in *);
bpsin->sin_port = htons(port);
bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
hostnamelen = MAXHOSTNAMELEN-1;
m = xdr_string_decode(m, hostname, &hostnamelen);
if (m == NULL)
goto bad;
domainnamelen = MAXHOSTNAMELEN-1;
m = xdr_string_decode(m, domainname, &domainnamelen);
if (m == NULL)
goto bad;
m = xdr_inaddr_decode(m, gw_ip);
if (m == NULL)
goto bad;
goto out;
bad:
printf("nfs_boot: bootparam_whoami: bad reply\n");
error = EBADRPC;
out:
m_freem(from);
m_freem(m);
return(error);
}
static int
bp_getfile(struct sockaddr_in *bpsin, const char *key,
struct nfs_dlmount *ndm, struct lwp *l)
{
char pathname[MNAMELEN];
struct in_addr inaddr;
struct sockaddr_in *sin;
struct mbuf *m;
char *serv_name;
int error, sn_len, path_len;
m = xdr_string_encode(hostname, hostnamelen);
if (m == NULL)
return (ENOMEM);
m->m_next = xdr_string_encode(__UNCONST(key), strlen(key));
if (m->m_next == NULL)
return (ENOMEM);
error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
BOOTPARAM_GETFILE, &m, NULL, l);
if (error)
return error;
serv_name = &ndm->ndm_host[0];
sn_len = sizeof(ndm->ndm_host) - 1;
m = xdr_string_decode(m, serv_name, &sn_len);
if (m == NULL)
goto bad;
m = xdr_inaddr_decode(m, &inaddr);
if (m == NULL)
goto bad;
path_len = sizeof(pathname) - 1;
m = xdr_string_decode(m, pathname, &path_len);
if (m == NULL)
goto bad;
sin = (struct sockaddr_in *) &ndm->ndm_saddr;
memset((void *)sin, 0, sizeof(*sin));
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_addr = inaddr;
if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
printf("nfs_boot: getfile name too long\n");
error = EIO;
goto out;
}
ndm->ndm_host[sn_len] = ':';
memcpy(ndm->ndm_host + sn_len + 1, pathname, path_len + 1);
goto out;
bad:
printf("nfs_boot: bootparam_getfile: bad reply\n");
error = EBADRPC;
out:
m_freem(m);
return(0);
}