#include <stddef.h>
#include <sys/types.h>
#include <sys/limits.h>
#include <sys/endian.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <string.h>
#define SUPPORT_DHCP
#define DHCP_ENV_NOVENDOR 1
#define DHCP_ENV_PXE 10
#define DHCP_ENV_FREEBSD 11
#define DHCP_ENV DHCP_ENV_NO_VENDOR
#include "stand.h"
#include "net.h"
#include "netif.h"
#include "bootp.h"
struct in_addr servip;
static time_t bot;
static char vm_rfc1048[4] = VM_RFC1048;
#ifdef BOOTP_VEND_CMU
static char vm_cmu[4] = VM_CMU;
#endif
static ssize_t bootpsend(struct iodesc *, void *, size_t);
static ssize_t bootprecv(struct iodesc *, void **, void **, time_t, void *);
static int vend_rfc1048(u_char *, u_int);
#ifdef BOOTP_VEND_CMU
static void vend_cmu(u_char *);
#endif
#ifdef DHCP_ENV
struct dhcp_opt;
static void setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts);
#else
#define setenv_(a, b, c)
#endif
#ifdef SUPPORT_DHCP
static char expected_dhcpmsgtype = -1, dhcp_ok;
struct in_addr dhcp_serverip;
#endif
struct bootp *bootp_response;
size_t bootp_response_size;
static void
bootp_fill_request(unsigned char *bp_vend)
{
bp_vend[0] = TAG_CLASSID;
bp_vend[1] = 9;
bcopy("PXEClient", &bp_vend[2], 9);
bp_vend[11] = TAG_USER_CLASS;
bp_vend[12] = 8;
bp_vend[13] = 7;
bcopy("FreeBSD", &bp_vend[14], 7);
bp_vend[21] = TAG_PARAM_REQ;
bp_vend[22] = 7;
bp_vend[23] = TAG_ROOTPATH;
bp_vend[24] = TAG_HOSTNAME;
bp_vend[25] = TAG_SWAPSERVER;
bp_vend[26] = TAG_GATEWAY;
bp_vend[27] = TAG_SUBNET_MASK;
bp_vend[28] = TAG_INTF_MTU;
bp_vend[29] = TAG_SERVERID;
bp_vend[30] = TAG_END;
}
void
bootp(int sock)
{
void *pkt;
struct iodesc *d;
struct bootp *bp;
struct {
u_char header[HEADER_SIZE];
struct bootp wbootp;
} wbuf;
struct bootp *rbootp;
DEBUG_PRINTF(1, ("bootp: socket=%d\n", sock));
if (!bot)
bot = getsecs();
if (!(d = socktodesc(sock))) {
printf("bootp: bad socket. %d\n", sock);
return;
}
DEBUG_PRINTF(1, ("bootp: socktodesc=%lx\n", (long)d));
bp = &wbuf.wbootp;
bzero(bp, sizeof(*bp));
bp->bp_op = BOOTREQUEST;
bp->bp_htype = 1;
bp->bp_hlen = 6;
bp->bp_xid = htonl(d->xid);
MACPY(d->myea, bp->bp_chaddr);
strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file));
bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
#ifdef SUPPORT_DHCP
bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
bp->bp_vend[5] = 1;
bp->bp_vend[6] = DHCPDISCOVER;
bootp_fill_request(&bp->bp_vend[7]);
#else
bp->bp_vend[4] = TAG_END;
#endif
d->myip.s_addr = INADDR_ANY;
d->myport = htons(IPPORT_BOOTPC);
d->destip.s_addr = INADDR_BROADCAST;
d->destport = htons(IPPORT_BOOTPS);
#ifdef SUPPORT_DHCP
expected_dhcpmsgtype = DHCPOFFER;
dhcp_ok = 0;
#endif
if(sendrecv(d,
bootpsend, bp, sizeof(*bp),
bootprecv, &pkt, (void **)&rbootp, NULL) == -1) {
printf("bootp: no reply\n");
return;
}
#ifdef SUPPORT_DHCP
if(dhcp_ok) {
uint32_t leasetime;
bp->bp_vend[6] = DHCPREQUEST;
bp->bp_vend[7] = TAG_REQ_ADDR;
bp->bp_vend[8] = 4;
bcopy(&rbootp->bp_yiaddr, &bp->bp_vend[9], 4);
bp->bp_vend[13] = TAG_SERVERID;
bp->bp_vend[14] = 4;
bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4);
bp->bp_vend[19] = TAG_LEASETIME;
bp->bp_vend[20] = 4;
leasetime = htonl(300);
bcopy(&leasetime, &bp->bp_vend[21], 4);
bootp_fill_request(&bp->bp_vend[25]);
expected_dhcpmsgtype = DHCPACK;
free(pkt);
if(sendrecv(d,
bootpsend, bp, sizeof(*bp),
bootprecv, &pkt, (void **)&rbootp, NULL) == -1) {
printf("DHCPREQUEST failed\n");
return;
}
}
#endif
myip = d->myip = rbootp->bp_yiaddr;
servip = rbootp->bp_siaddr;
if (rootip.s_addr == INADDR_ANY)
rootip = servip;
bcopy(rbootp->bp_file, bootfile, sizeof(bootfile));
bootfile[sizeof(bootfile) - 1] = '\0';
if (!netmask) {
if (IN_CLASSA(ntohl(myip.s_addr)))
netmask = htonl(IN_CLASSA_NET);
else if (IN_CLASSB(ntohl(myip.s_addr)))
netmask = htonl(IN_CLASSB_NET);
else
netmask = htonl(IN_CLASSC_NET);
DEBUG_PRINTF(1, ("'native netmask' is %s\n", intoa(netmask)));
}
DEBUG_PRINTF(1,("rootip: %s\n", inet_ntoa(rootip)));
DEBUG_PRINTF(1,("mask: %s\n", intoa(netmask)));
if (!SAMENET(myip, rootip, netmask)) {
DEBUG_PRINTF(1,("need gateway for root ip\n"));
}
if (!SAMENET(myip, gateip, netmask)) {
DEBUG_PRINTF(1,("gateway ip (%s) bad\n", inet_ntoa(gateip)));
gateip.s_addr = 0;
}
++d->xid;
free(pkt);
}
static ssize_t
bootpsend(struct iodesc *d, void *pkt, size_t len)
{
struct bootp *bp;
DEBUG_PRINTF(1,("bootpsend: d=%lx called.\n", (long)d));
bp = pkt;
bp->bp_secs = htons((u_short)(getsecs() - bot));
DEBUG_PRINTF(1,("bootpsend: calling sendudp\n"));
return (sendudp(d, pkt, len));
}
static ssize_t
bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft,
void *extra)
{
ssize_t n;
struct bootp *bp;
void *ptr;
DEBUG_PRINTF(1,("bootp_recvoffer: called\n"));
ptr = NULL;
n = readudp(d, &ptr, (void **)&bp, tleft);
if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
goto bad;
DEBUG_PRINTF(1,("bootprecv: checked. bp = %p, n = %zd\n", bp, n));
if (bp->bp_xid != htonl(d->xid)) {
DEBUG_PRINTF(1,("bootprecv: expected xid 0x%lx, got 0x%x\n",
d->xid, ntohl(bp->bp_xid)));
goto bad;
}
DEBUG_PRINTF(1,("bootprecv: got one!\n"));
if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
int vsize = n - offsetof(struct bootp, bp_vend);
if (vend_rfc1048(bp->bp_vend, vsize) != 0)
goto bad;
if (bp->bp_op == BOOTREPLY &&
((dhcp_ok == 1 && expected_dhcpmsgtype == DHCPACK) ||
dhcp_ok == 0)) {
free(bootp_response);
bootp_response = malloc(n);
if (bootp_response != NULL) {
bootp_response_size = n;
bcopy(bp, bootp_response, bootp_response_size);
}
}
}
#ifdef BOOTP_VEND_CMU
else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
vend_cmu(bp->bp_vend);
#endif
else
printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
*pkt = ptr;
*payload = bp;
return (n);
bad:
free(ptr);
errno = 0;
return (-1);
}
static int
vend_rfc1048(u_char *cp, u_int len)
{
u_char *ep;
int size;
u_char tag;
const char *val;
DEBUG_PRINTF(1,("vend_rfc1048 bootp info. len=%d\n", len));
ep = cp + len;
cp += sizeof(int);
setenv_(cp, ep, NULL);
while (cp < ep) {
tag = *cp++;
size = *cp++;
if (tag == TAG_END)
break;
if (tag == TAG_SUBNET_MASK) {
bcopy(cp, &netmask, sizeof(netmask));
}
if (tag == TAG_GATEWAY) {
bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
}
if (tag == TAG_SWAPSERVER) {
bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
}
if (tag == TAG_ROOTPATH) {
if ((val = getenv("dhcp.root-path")) == NULL)
val = (const char *)cp;
strlcpy(rootpath, val, sizeof(rootpath));
}
if (tag == TAG_HOSTNAME) {
if ((val = getenv("dhcp.host-name")) == NULL)
val = (const char *)cp;
strlcpy(hostname, val, sizeof(hostname));
}
if (tag == TAG_INTF_MTU) {
intf_mtu = 0;
if ((val = getenv("dhcp.interface-mtu")) != NULL) {
unsigned long tmp;
char *end;
errno = 0;
tmp = strtoul(val, &end, 0);
if (errno != 0 ||
*val == '\0' || *end != '\0' ||
tmp > USHRT_MAX) {
printf("%s: bad value: \"%s\", "
"ignoring\n",
"dhcp.interface-mtu", val);
} else {
intf_mtu = (u_int)tmp;
}
}
if (intf_mtu <= 0)
intf_mtu = be16dec(cp);
}
#ifdef SUPPORT_DHCP
if (tag == TAG_DHCP_MSGTYPE) {
if(*cp != expected_dhcpmsgtype)
return(-1);
dhcp_ok = 1;
}
if (tag == TAG_SERVERID) {
bcopy(cp, &dhcp_serverip.s_addr,
sizeof(dhcp_serverip.s_addr));
}
#endif
cp += size;
}
return(0);
}
#ifdef BOOTP_VEND_CMU
static void
vend_cmu(u_char *cp)
{
struct cmu_vend *vp;
DEBUG_PRINTF(1,("vend_cmu bootp info.\n"));
vp = (struct cmu_vend *)cp;
if (vp->v_smask.s_addr != 0) {
netmask = vp->v_smask.s_addr;
}
if (vp->v_dgate.s_addr != 0) {
gateip = vp->v_dgate;
}
}
#endif
#ifdef DHCP_ENV
enum opt_fmt { __NONE = 0,
__8 = 1, __16 = 2, __32 = 4,
__IP,
__TXT,
__BYTES,
__INDIR,
__ILIST,
__VE,
};
struct dhcp_opt {
uint8_t tag;
uint8_t fmt;
const char *desc;
};
static struct dhcp_opt vndr_opt[] = {
#if DHCP_ENV == DHCP_ENV_FREEBSD
{0, 0, "FreeBSD"},
{1, __TXT, "kernel"},
{2, __TXT, "kernelname"},
{3, __TXT, "kernel_options"},
{4, __IP, "usr-ip"},
{5, __TXT, "conf-path"},
{6, __TXT, "rc.conf0"},
{7, __TXT, "rc.conf1"},
{8, __TXT, "rc.conf2"},
{9, __TXT, "rc.conf3"},
{10, __TXT, "rc.conf4"},
{11, __TXT, "rc.conf5"},
{12, __TXT, "rc.conf6"},
{13, __TXT, "rc.conf7"},
{14, __TXT, "rc.conf8"},
{15, __TXT, "rc.conf9"},
{20, __TXT, "boot.nfsroot.options"},
{245, __INDIR, ""},
{246, __INDIR, ""},
{247, __INDIR, ""},
{248, __INDIR, ""},
{249, __INDIR, ""},
{250, __INDIR, ""},
{251, __INDIR, ""},
{252, __INDIR, ""},
{253, __INDIR, ""},
{254, __INDIR, ""},
#elif DHCP_ENV == DHCP_ENV_PXE
{0, 0, "pxe"},
{93, __16, "system-architecture"},
{94, __BYTES, "network-interface"},
{97, __BYTES, "machine-identifier"},
#else
{0, 0, "dhcp.vendor."},
#endif
{0, __TXT, "%soption-%d"}
};
static struct dhcp_opt dhcp_opt[] = {
{0, 0, "dhcp."},
{1, __IP, "subnet-mask"},
{2, __32, "time-offset"},
{3, __IP, "routers"},
{4, __IP, "time-servers"},
{5, __IP, "ien116-name-servers"},
{6, __IP, "domain-name-servers"},
{7, __IP, "log-servers"},
{8, __IP, "cookie-servers"},
{9, __IP, "lpr-servers"},
{10, __IP, "impress-servers"},
{11, __IP, "resource-location-servers"},
{12, __TXT, "host-name"},
{13, __16, "boot-size"},
{14, __TXT, "merit-dump"},
{15, __TXT, "domain-name"},
{16, __IP, "swap-server"},
{17, __TXT, "root-path"},
{18, __TXT, "extensions-path"},
{19, __8, "ip-forwarding"},
{20, __8, "non-local-source-routing"},
{21, __IP, "policy-filter"},
{22, __16, "max-dgram-reassembly"},
{23, __8, "default-ip-ttl"},
{24, __32, "path-mtu-aging-timeout"},
{25, __16, "path-mtu-plateau-table"},
{26, __16, "interface-mtu"},
{27, __8, "all-subnets-local"},
{28, __IP, "broadcast-address"},
{29, __8, "perform-mask-discovery"},
{30, __8, "mask-supplier"},
{31, __8, "perform-router-discovery"},
{32, __IP, "router-solicitation-address"},
{33, __IP, "static-routes"},
{34, __8, "trailer-encapsulation"},
{35, __32, "arp-cache-timeout"},
{36, __8, "ieee802-3-encapsulation"},
{37, __8, "default-tcp-ttl"},
{38, __32, "tcp-keepalive-interval"},
{39, __8, "tcp-keepalive-garbage"},
{40, __TXT, "nis-domain"},
{41, __IP, "nis-servers"},
{42, __IP, "ntp-servers"},
{43, __VE, "vendor-encapsulated-options"},
{44, __IP, "netbios-name-servers"},
{45, __IP, "netbios-dd-server"},
{46, __8, "netbios-node-type"},
{47, __TXT, "netbios-scope"},
{48, __IP, "x-font-servers"},
{49, __IP, "x-display-managers"},
{50, __IP, "dhcp-requested-address"},
{51, __32, "dhcp-lease-time"},
{52, __8, "dhcp-option-overload"},
{53, __8, "dhcp-message-type"},
{54, __IP, "dhcp-server-identifier"},
{55, __8, "dhcp-parameter-request-list"},
{56, __TXT, "dhcp-message"},
{57, __16, "dhcp-max-message-size"},
{58, __32, "dhcp-renewal-time"},
{59, __32, "dhcp-rebinding-time"},
{60, __TXT, "vendor-class-identifier"},
{61, __TXT, "dhcp-client-identifier"},
{64, __TXT, "nisplus-domain"},
{65, __IP, "nisplus-servers"},
{66, __TXT, "tftp-server-name"},
{67, __TXT, "bootfile-name"},
{68, __IP, "mobile-ip-home-agent"},
{69, __IP, "smtp-server"},
{70, __IP, "pop-server"},
{71, __IP, "nntp-server"},
{72, __IP, "www-server"},
{73, __IP, "finger-server"},
{74, __IP, "irc-server"},
{75, __IP, "streettalk-server"},
{76, __IP, "streettalk-directory-assistance-server"},
{77, __TXT, "user-class"},
{85, __IP, "nds-servers"},
{86, __TXT, "nds-tree-name"},
{87, __TXT, "nds-context"},
{210, __TXT, "authenticate"},
{246, __ILIST, ""},
{247, __ILIST, ""},
{248, __ILIST, ""},
{249, __ILIST, ""},
{250, __INDIR, ""},
{251, __INDIR, ""},
{252, __INDIR, ""},
{253, __INDIR, ""},
{254, __INDIR, ""},
{0, __TXT, "%soption-%d"}
};
static void
setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts)
{
u_char *ncp;
u_char tag;
char tags[512], *tp;
#define FLD_SEP ','
ncp = cp;
tp = tags;
if (opts == NULL)
opts = dhcp_opt;
while (ncp < ep) {
unsigned int size;
char *vp, *endv, buf[256];
struct dhcp_opt *op;
tag = *ncp++;
size = *ncp++;
cp = ncp;
ncp += size;
if (tag == TAG_END)
break;
if (tag == 0)
continue;
for (op = opts+1; op->tag && op->tag != tag; op++)
;
vp = buf;
*vp = '\0';
endv = buf + sizeof(buf) - 1 - 16;
switch(op->fmt) {
case __NONE:
break;
case __VE:
setenv_(cp, cp+size, vndr_opt);
break;
case __IP:
for (; size > 0 && vp < endv; size -= 4, cp += 4) {
struct in_addr in_ip;
if (vp != buf)
*vp++ = FLD_SEP;
bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr));
snprintf(vp, endv - vp, "%s", inet_ntoa(in_ip));
vp += strlen(vp);
}
break;
case __BYTES:
for (; size > 0 && vp < endv; size -= 1, cp += 1) {
snprintf(vp, endv - vp, "%02x", *cp);
vp += strlen(vp);
}
break;
case __TXT:
bcopy(cp, buf, size);
buf[size] = 0;
break;
case __32:
case __16:
case __8:
for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) {
uint32_t v;
if (op->fmt == __32)
v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3];
else if (op->fmt == __16)
v = (cp[0]<<8) + cp[1];
else
v = cp[0];
if (vp != buf)
*vp++ = FLD_SEP;
snprintf(vp, endv - vp, "%u", v);
vp += strlen(vp);
}
break;
case __INDIR:
case __ILIST:
bcopy(cp, buf, size);
buf[size] = '\0';
for (endv = buf; endv; endv = vp) {
char *s = NULL;
while (*endv && strchr(" \t\n\r", *endv))
endv++;
vp = strchr(endv, '=');
if (!vp)
break;
*vp++ = 0;
if (op->fmt == __ILIST && (s = strchr(vp, ';')))
*s++ = '\0';
setenv(endv, vp, 1);
vp = s;
}
buf[0] = '\0';
break;
}
if (tp - tags < sizeof(tags) - 5) {
if (tp != tags)
*tp++ = FLD_SEP;
snprintf(tp, sizeof(tags) - (tp - tags), "%d", tag);
tp += strlen(tp);
}
if (buf[0]) {
char env[128];
if (op->tag == 0)
snprintf(env, sizeof(env), op->desc, opts[0].desc, tag);
else
snprintf(env, sizeof(env), "%s%s", opts[0].desc, op->desc);
setenv(env, buf, 0);
}
}
if (tp != tags) {
char env[128];
snprintf(env, sizeof(env), "%stags", opts[0].desc);
setenv(env, tags, 1);
}
}
#endif