#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nfs_bootdhcp.c,v 1.60 2024/10/20 14:01:52 mlelstv Exp $");
#ifdef _KERNEL_OPT
#include "opt_nfs_boot.h"
#include "opt_tftproot.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.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 <net/if.h>
#include <net/if_types.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfs/nfs.h>
#include <nfs/nfsmount.h>
#include <nfs/nfsdiskless.h>
#define BP_CHADDR_LEN 16
#define BP_SNAME_LEN 64
#define BP_FILE_LEN 128
#define BP_VEND_LEN 64
struct bootp {
u_int8_t bp_op;
u_int8_t bp_htype;
u_int8_t bp_hlen;
u_int8_t bp_hops;
u_int32_t bp_xid;
u_int16_t bp_secs;
u_int16_t bp_flags;
struct in_addr bp_ciaddr;
struct in_addr bp_yiaddr;
struct in_addr bp_siaddr;
struct in_addr bp_giaddr;
u_int8_t bp_chaddr[BP_CHADDR_LEN];
char bp_sname[BP_SNAME_LEN];
char bp_file[BP_FILE_LEN];
u_int8_t bp_vend[BP_VEND_LEN];
};
#define IPPORT_BOOTPS 67
#define IPPORT_BOOTPC 68
#define BOOTREQUEST 1
#define BOOTREPLY 2
#define HTYPE_ETHERNET 1
#define HTYPE_IEEE802 6
static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
#define TAG_END ((unsigned char) 255)
#define TAG_PAD ((unsigned char) 0)
#define TAG_SUBNET_MASK ((unsigned char) 1)
#define TAG_TIME_OFFSET ((unsigned char) 2)
#define TAG_GATEWAY ((unsigned char) 3)
#define TAG_TIME_SERVER ((unsigned char) 4)
#define TAG_NAME_SERVER ((unsigned char) 5)
#define TAG_DOMAIN_SERVER ((unsigned char) 6)
#define TAG_LOG_SERVER ((unsigned char) 7)
#define TAG_COOKIE_SERVER ((unsigned char) 8)
#define TAG_LPR_SERVER ((unsigned char) 9)
#define TAG_IMPRESS_SERVER ((unsigned char) 10)
#define TAG_RLP_SERVER ((unsigned char) 11)
#define TAG_HOST_NAME ((unsigned char) 12)
#define TAG_BOOT_SIZE ((unsigned char) 13)
#define TAG_DUMP_FILE ((unsigned char) 14)
#define TAG_DOMAIN_NAME ((unsigned char) 15)
#define TAG_SWAP_SERVER ((unsigned char) 16)
#define TAG_ROOT_PATH ((unsigned char) 17)
#define TAG_INTERFACE_MTU ((unsigned char) 26)
#ifdef NFS_BOOT_DHCP
#define TAG_REQ_ADDR ((unsigned char) 50)
#define TAG_LEASETIME ((unsigned char) 51)
#define TAG_OVERLOAD ((unsigned char) 52)
#define TAG_DHCP_MSGTYPE ((unsigned char) 53)
#define TAG_SERVERID ((unsigned char) 54)
#define TAG_PARAM_REQ ((unsigned char) 55)
#define TAG_MSG ((unsigned char) 56)
#define TAG_MAXSIZE ((unsigned char) 57)
#define TAG_T1 ((unsigned char) 58)
#define TAG_T2 ((unsigned char) 59)
#define TAG_CLASSID ((unsigned char) 60)
#define TAG_CLIENTID ((unsigned char) 61)
#endif
#ifdef NFS_BOOT_DHCP
#define DHCPDISCOVER 1
#define DHCPOFFER 2
#define DHCPREQUEST 3
#define DHCPDECLINE 4
#define DHCPACK 5
#define DHCPNAK 6
#define DHCPRELEASE 7
#endif
#define IP_MIN_MTU 576
#ifdef NFS_BOOT_DHCP
#define BOOTP_SIZE_MAX (sizeof(struct bootp)+312-64)
#else
#define BOOTP_SIZE_MAX (sizeof(struct bootp)+256-64)
#endif
#define BOOTP_SIZE_MIN (sizeof(struct bootp))
#define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
static int bootpc_call (struct nfs_diskless *, struct lwp *, int *);
static void bootp_extract (struct bootp *, int, struct nfs_diskless *, int *);
#ifdef DEBUG_NFS_BOOT_DHCP
#define DPRINTF(s) printf s
#else
#define DPRINTF(s)
#endif
int
nfs_bootdhcp(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
{
struct ifnet *ifp = nd->nd_ifp;
int error;
error = nfs_boot_setaddress(ifp, lwp,
*flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
*flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
INADDR_BROADCAST);
if (error) {
printf("nfs_boot: set ifaddr zero, error=%d\n", error);
return (error);
}
error = bootpc_call(nd, lwp, flags);
(void) nfs_boot_deladdress(ifp, lwp, INADDR_ANY);
if (error)
goto out;
error = nfs_boot_setaddress(ifp, lwp, nd->nd_myip.s_addr,
nd->nd_mask.s_addr, INADDR_ANY);
if (error) {
printf("nfs_boot: set ifaddr real, error=%d\n", error);
goto out;
}
if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO) {
printf("nfs_boot: missing options (need IP, netmask, "
"gateway, next-server, root-path)\n");
return EADDRNOTAVAIL;
}
out:
if (error) {
(void) nfs_boot_ifupdown(ifp, lwp, 0);
nfs_boot_flushrt(ifp);
}
return (error);
}
struct bootpcontext {
int xid;
const u_char *haddr;
u_char halen;
struct bootp *replybuf;
int replylen;
#ifdef NFS_BOOT_DHCP
char expected_dhcpmsgtype, dhcp_ok;
struct in_addr dhcp_serverip;
#endif
};
static int bootpset (struct mbuf*, void*, int);
static int bootpcheck (struct mbuf**, void*);
static int
bootpset(struct mbuf *m, void *context, int waited)
{
struct bootp *bootp;
bootp = mtod(m, struct bootp*);
bootp->bp_secs = htons(waited);
return (0);
}
static int
bootpcheck(struct mbuf **mp, void *context)
{
struct bootp *bootp;
struct bootpcontext *bpc = context;
struct mbuf *m = *mp;
u_int tag, len;
u_char *p, *limit;
if (m->m_pkthdr.len < BOOTP_SIZE_MIN) {
DPRINTF(("bootpcheck: short packet %d < %zu\n",
m->m_pkthdr.len, BOOTP_SIZE_MIN));
return (-1);
}
if (m->m_pkthdr.len > BOOTP_SIZE_MAX) {
DPRINTF(("Bootpcheck: long packet %d > %zu\n",
m->m_pkthdr.len, BOOTP_SIZE_MAX));
return (-1);
}
if (m->m_len < offsetof(struct bootp, bp_sname)) {
m = *mp = m_pullup(m, offsetof(struct bootp, bp_sname));
if (m == NULL) {
DPRINTF(("bootpcheck: m_pullup failed\n"));
return (-1);
}
}
bootp = mtod(m, struct bootp*);
if (bootp->bp_op != BOOTREPLY) {
DPRINTF(("bootpcheck: op %d is not reply\n", bootp->bp_op));
return (-1);
}
if (bootp->bp_hlen != bpc->halen) {
DPRINTF(("bootpcheck: hlen %d != %d\n", bootp->bp_hlen,
bpc->halen));
return (-1);
}
if (memcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) {
#ifdef DEBUG_NFS_BOOT_DHCP
char *bp_chaddr, *haddr;
bp_chaddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
haddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
DPRINTF(("bootpcheck: incorrect hwaddr %s != %s\n",
ether_snprintf(bp_chaddr, 3 * bpc->halen,
bootp->bp_chaddr),
ether_snprintf(haddr, 3 * bpc->halen, bpc->haddr)));
free(bp_chaddr, M_TEMP);
free(haddr, M_TEMP);
#endif
return (-1);
}
if (bootp->bp_xid != bpc->xid) {
DPRINTF(("bootpcheck: xid %d != %d\n", bootp->bp_xid,
bpc->xid));
return (-1);
}
bpc->replylen = m->m_pkthdr.len;
m_copydata(m, 0, bpc->replylen, (void *)bpc->replybuf);
bootp = bpc->replybuf;
if (bootp->bp_yiaddr.s_addr == INADDR_ANY ||
bootp->bp_yiaddr.s_addr == INADDR_BROADCAST) {
printf("nfs_boot: wrong IP addr %s",
inet_ntoa(bootp->bp_yiaddr));
goto warn;
}
if (memcmp(bootp->bp_vend, vm_rfc1048, 4)) {
printf("nfs_boot: reply missing options");
goto warn;
}
p = &bootp->bp_vend[4];
limit = ((u_char*)bootp) + bpc->replylen;
while (p < limit) {
tag = *p++;
if (tag == TAG_END)
break;
if (tag == TAG_PAD)
continue;
len = *p++;
if ((p + len) > limit) {
printf("nfs_boot: option %d too long", tag);
goto warn;
}
switch (tag) {
#ifdef NFS_BOOT_DHCP
case TAG_DHCP_MSGTYPE:
if (*p != bpc->expected_dhcpmsgtype)
return (-1);
bpc->dhcp_ok = 1;
break;
case TAG_SERVERID:
memcpy(&bpc->dhcp_serverip.s_addr, p,
sizeof(bpc->dhcp_serverip.s_addr));
break;
#endif
default:
break;
}
p += len;
}
return (0);
warn:
printf(" (bad reply from %s)\n", inet_ntoa(bootp->bp_siaddr));
return (-1);
}
static void
bootp_addvend(u_char *area)
{
#ifdef NFS_BOOT_DHCP
char vci[64];
int vcilen;
*area++ = TAG_PARAM_REQ;
*area++ = 7;
*area++ = TAG_SUBNET_MASK;
*area++ = TAG_GATEWAY;
*area++ = TAG_HOST_NAME;
*area++ = TAG_DOMAIN_NAME;
*area++ = TAG_ROOT_PATH;
*area++ = TAG_SWAP_SERVER;
*area++ = TAG_INTERFACE_MTU;
snprintf(vci, sizeof(vci), "%s:%s:kernel:%s", ostype, MACHINE,
osrelease);
vcilen = strlen(vci);
*area++ = TAG_CLASSID;
*area++ = vcilen;
(void)memcpy(area, vci, vcilen);
area += vcilen;
#endif
*area = TAG_END;
}
static int
bootpc_call(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
{
struct socket *so;
struct ifnet *ifp = nd->nd_ifp;
static u_int32_t xid = ~0xFF;
struct bootp *bootp;
struct mbuf *m;
struct sockaddr_in sin;
int error;
const u_char *haddr;
u_char hafmt, halen;
struct bootpcontext bpc;
unsigned int index;
error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
if (error) {
printf("bootp: socreate, error=%d\n", error);
return (error);
}
bpc.replybuf = NULL;
m = NULL;
{ const struct sockaddr_dl *sdl = ifp->if_sadl;
switch (sdl->sdl_type) {
case IFT_ISO88025:
hafmt = HTYPE_IEEE802;
break;
case IFT_ETHER:
case IFT_FDDI:
hafmt = HTYPE_ETHERNET;
break;
default:
printf("bootp: unsupported interface type %d\n",
sdl->sdl_type);
error = EINVAL;
goto out;
}
halen = sdl->sdl_alen;
haddr = (const unsigned char *)CLLADDR(sdl);
}
{ int32_t opt;
opt = 1;
error = so_setsockopt(NULL, so, SOL_SOCKET, SO_DONTROUTE, &opt,
sizeof(opt));
}
if (error) {
DPRINTF(("bootpc_call: SO_DONTROUTE failed %d\n", error));
goto out;
}
if ((error = nfs_boot_enbroadcast(so))) {
DPRINTF(("bootpc_call: SO_BROADCAST failed %d\n", error));
goto out;
}
{ u_char opt;
opt = 7;
error = so_setsockopt(NULL, so, IPPROTO_IP, IP_MULTICAST_TTL,
&opt, sizeof(opt));
}
if (error) {
DPRINTF(("bootpc_call: IP_MULTICAST_TTL failed %d\n", error));
goto out;
}
if ((error = nfs_boot_setrecvtimo(so))) {
DPRINTF(("bootpc_call: SO_RCVTIMEO failed %d\n", error));
goto out;
}
if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC, lwp))) {
DPRINTF(("bootpc_call: bind failed %d\n", error));
goto out;
}
sin.sin_len = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_BROADCAST;
sin.sin_port = htons(IPPORT_BOOTPS);
m = m_gethdr(M_WAIT, MT_DATA);
MCLAIM(m, &nfs_mowner);
m_clget(m, M_WAIT);
bootp = mtod(m, struct bootp*);
m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX;
m_reset_rcvif(m);
memset((void *)bootp, 0, BOOTP_SIZE_MAX);
bootp->bp_op = BOOTREQUEST;
bootp->bp_htype = hafmt;
bootp->bp_hlen = halen;
bootp->bp_xid = ++xid;
memcpy(bootp->bp_chaddr, haddr, halen);
#ifdef NFS_BOOT_BOOTP_REQFILE
strncpy(bootp->bp_file, NFS_BOOT_BOOTP_REQFILE, sizeof(bootp->bp_file));
#endif
memcpy(bootp->bp_vend, vm_rfc1048, 4);
index = 4;
#ifdef NFS_BOOT_DHCP
bootp->bp_vend[index++] = TAG_DHCP_MSGTYPE;
bootp->bp_vend[index++] = 1;
bootp->bp_vend[index++] = DHCPDISCOVER;
#endif
bootp_addvend(&bootp->bp_vend[index]);
bpc.xid = xid;
bpc.haddr = haddr;
bpc.halen = halen;
bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
if (bpc.replybuf == NULL)
panic("nfs_boot: malloc reply buf");
#ifdef NFS_BOOT_DHCP
bpc.expected_dhcpmsgtype = DHCPOFFER;
bpc.dhcp_ok = 0;
#endif
error = nfs_boot_sendrecv(so, &sin, bootpset, m,
bootpcheck, NULL, NULL, &bpc, lwp);
if (error)
goto out;
#ifdef NFS_BOOT_DHCP
if (bpc.dhcp_ok) {
u_int32_t leasetime;
index = 6;
bootp->bp_vend[index++] = DHCPREQUEST;
bootp->bp_vend[index++] = TAG_REQ_ADDR;
bootp->bp_vend[index++] = 4;
memcpy(&bootp->bp_vend[index], &bpc.replybuf->bp_yiaddr, 4);
index += 4;
bootp->bp_vend[index++] = TAG_SERVERID;
bootp->bp_vend[index++] = 4;
memcpy(&bootp->bp_vend[index], &bpc.dhcp_serverip.s_addr, 4);
index += 4;
bootp->bp_vend[index++] = TAG_LEASETIME;
bootp->bp_vend[index++] = 4;
leasetime = htonl(300);
memcpy(&bootp->bp_vend[index], &leasetime, 4);
index += 4;
bootp_addvend(&bootp->bp_vend[index]);
bpc.expected_dhcpmsgtype = DHCPACK;
error = nfs_boot_sendrecv(so, &sin, bootpset, m,
bootpcheck, NULL, NULL, &bpc, lwp);
if (error)
goto out;
}
#endif
#ifdef NFS_BOOT_DHCP
printf("nfs_boot: %s next-server: %s\n",
(bpc.dhcp_ok ? "DHCP" : "BOOTP"),
#else
printf("nfs_boot: BOOTP next-server: %s\n",
#endif
inet_ntoa(bpc.replybuf->bp_siaddr));
bootp_extract(bpc.replybuf, bpc.replylen, nd, flags);
out:
if (bpc.replybuf)
free(bpc.replybuf, M_DEVBUF);
m_freem(m);
soclose(so);
return (error);
}
static void
bootp_extract(struct bootp *bootp, int replylen,
struct nfs_diskless *nd, int *flags)
{
struct sockaddr_in *sin;
struct in_addr netmask;
struct in_addr gateway;
struct in_addr rootserver;
char *myname;
char *mydomain;
char *rootpath;
uint16_t myinterfacemtu;
int mynamelen;
int mydomainlen;
int rootpathlen;
int overloaded;
u_int tag, len;
u_char *p, *limit;
netmask.s_addr = 0;
gateway.s_addr = 0;
mydomain = myname = rootpath = NULL;
mydomainlen = mynamelen = rootpathlen = 0;
rootserver = bootp->bp_siaddr;
overloaded = 0;
myinterfacemtu = 0;
p = &bootp->bp_vend[4];
limit = ((u_char*)bootp) + replylen;
while (p < limit) {
tag = *p++;
if (tag == TAG_END)
break;
if (tag == TAG_PAD)
continue;
len = *p++;
#if 0
if ((p + len) > limit) {
printf("nfs_boot: option %d too long\n", tag);
break;
}
#endif
switch (tag) {
case TAG_SUBNET_MASK:
if (len < 4) {
printf("nfs_boot: subnet mask < 4 bytes\n");
break;
}
memcpy(&netmask, p, 4);
break;
case TAG_GATEWAY:
if (len < 4) {
printf("nfs_boot: routers < 4 bytes\n");
break;
}
memcpy(&gateway, p, 4);
break;
case TAG_HOST_NAME:
if (len >= sizeof(hostname)) {
printf("nfs_boot: host name >= %lu bytes\n",
(u_long)sizeof(hostname));
break;
}
myname = p;
mynamelen = len;
break;
case TAG_DOMAIN_NAME:
if (len >= sizeof(domainname)) {
printf("nfs_boot: domain name >= %lu bytes\n",
(u_long)sizeof(domainname));
break;
}
mydomain = p;
mydomainlen = len;
break;
case TAG_ROOT_PATH:
if (len >= (MNAMELEN-10)) {
printf("nfs_boot: rootpath >= %d bytes\n",
(MNAMELEN-10));
break;
}
rootpath = p;
rootpathlen = len;
break;
case TAG_INTERFACE_MTU:
if (len != 2) {
printf("nfs_boot: interface-mtu len != 2 (%d)",
len);
break;
}
memcpy(&myinterfacemtu, p, 2);
myinterfacemtu = ntohs(myinterfacemtu);
break;
case TAG_SWAP_SERVER:
if (len < 4) {
printf("nfs_boot: swap server < 4 bytes\n");
break;
}
memcpy(&rootserver, p, 4);
break;
#ifdef NFS_BOOT_DHCP
case TAG_OVERLOAD:
if (len > 0 && ((*p & 0x02) != 0))
overloaded = 1;
break;
#endif
default:
break;
}
p += len;
}
if (myname) {
myname[mynamelen] = '\0';
strncpy(hostname, myname, sizeof(hostname));
hostnamelen = mynamelen;
printf("nfs_boot: my_name=%s\n", hostname);
}
if (mydomain) {
mydomain[mydomainlen] = '\0';
strncpy(domainname, mydomain, sizeof(domainname));
domainnamelen = mydomainlen;
printf("nfs_boot: my_domain=%s\n", domainname);
}
if (!(*flags & NFS_BOOT_HAS_MYIP)) {
nd->nd_myip = bootp->bp_yiaddr;
printf("nfs_boot: my_addr=%s\n", inet_ntoa(nd->nd_myip));
*flags |= NFS_BOOT_HAS_MYIP;
}
if (!(*flags & NFS_BOOT_HAS_MASK)) {
nd->nd_mask = netmask;
printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
*flags |= NFS_BOOT_HAS_MASK;
}
if (!(*flags & NFS_BOOT_HAS_GWIP)) {
nd->nd_gwip = gateway;
printf("nfs_boot: gateway=%s\n", inet_ntoa(nd->nd_gwip));
*flags |= NFS_BOOT_HAS_GWIP;
}
if (myinterfacemtu >= IP_MIN_MTU) {
nd->nd_mtu = myinterfacemtu;
printf("nfs_boot: mtu=%d\n", nd->nd_mtu);
}
do {
struct nfs_dlmount *ndm = &nd->nd_root;
if (!(*flags & NFS_BOOT_HAS_SERVADDR)) {
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 = rootserver;
*flags |= NFS_BOOT_HAS_SERVADDR;
}
if (!(*flags & NFS_BOOT_HAS_SERVER)) {
if (!overloaded && bootp->bp_sname[0] != 0 &&
!memcmp(&rootserver, &bootp->bp_siaddr,
sizeof(struct in_addr)))
{
strncpy(ndm->ndm_host, bootp->bp_sname,
BP_SNAME_LEN-1);
*flags |= NFS_BOOT_HAS_SERVER;
} else {
strncpy(ndm->ndm_host, inet_ntoa(rootserver),
BP_SNAME_LEN-1);
*flags |= NFS_BOOT_HAS_SERVER;
}
}
if (!(*flags & NFS_BOOT_HAS_ROOTPATH)) {
len = strlen(ndm->ndm_host);
if (rootpath &&
len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host))
{
ndm->ndm_host[len++] = ':';
strncpy(ndm->ndm_host + len,
rootpath, rootpathlen);
ndm->ndm_host[len + rootpathlen] = '\0';
*flags |= NFS_BOOT_HAS_ROOTPATH;
}
}
} while(0);
#ifdef TFTPROOT
#if BP_FILE_LEN > MNAMELEN
#define BOOTFILELEN MNAMELEN
#else
#define BOOTFILELEN BP_FILE_LEN
#endif
strncpy(nd->nd_bootfile, bootp->bp_file, BOOTFILELEN);
nd->nd_bootfile[BOOTFILELEN - 1] = '\0';
#undef BOOTFILELEN
#endif
}