#include <sys/types.h>
#include "dhcp_impl.h"
#if defined(_KERNEL) && !defined(_BOOT)
#include <sys/sunddi.h>
#else
#include <strings.h>
#endif
static uint8_t bootmagic[] = BOOTMAGIC;
static void
field_scan(uint8_t *start, uint8_t *end, DHCP_OPT **options,
uint8_t last_option)
{
uint8_t *current;
while (start < end) {
if (*start == CD_PAD) {
start++;
continue;
}
if (*start == CD_END)
break;
if (*start > last_option) {
if (++start < end)
start += *start + 1;
continue;
}
current = start;
if (++start < end)
start += *start + 1;
if ((current + 1) >= end)
continue;
if (options[*current] == NULL) {
options[*current] = (DHCP_OPT *)current;
if ((current + options[*current]->len + 1) >= end) {
options[*current] = NULL;
continue;
}
}
}
}
static void
vendor_scan(PKT_LIST *pl)
{
uint8_t *start, *end, len;
if (pl->opts[CD_VENDOR_SPEC] == NULL)
return;
len = pl->opts[CD_VENDOR_SPEC]->len;
start = pl->opts[CD_VENDOR_SPEC]->value;
if (((start - (uint8_t *)pl->pkt) + len) > pl->len)
return;
end = start + len;
field_scan(start, end, pl->vs, VS_OPTION_END);
}
int
dhcp_options_scan(PKT_LIST *pl, boolean_t scan_vendor)
{
PKT *pkt = pl->pkt;
uint_t opt_size = pl->len - BASE_PKT_SIZE;
if (pl->len < BASE_PKT_SIZE ||
bcmp(pl->pkt->cookie, bootmagic, sizeof (pl->pkt->cookie)) != 0) {
pl->rfc1048 = 0;
return (0);
}
pl->rfc1048 = 1;
field_scan(pkt->options, &pkt->options[opt_size], pl->opts,
DHCP_LAST_OPT);
if (scan_vendor && (pl->opts[CD_VENDOR_SPEC] != NULL))
vendor_scan(pl);
if (pl->opts[CD_DHCP_TYPE] == NULL)
return (0);
if (pl->opts[CD_DHCP_TYPE]->len != 1)
return (DHCP_GARBLED_MSG_TYPE);
if (*pl->opts[CD_DHCP_TYPE]->value < DISCOVER ||
*pl->opts[CD_DHCP_TYPE]->value > INFORM)
return (DHCP_WRONG_MSG_TYPE);
if (pl->opts[CD_OPTION_OVERLOAD]) {
if (pl->opts[CD_OPTION_OVERLOAD]->len != 1) {
pl->opts[CD_OPTION_OVERLOAD] = NULL;
return (DHCP_BAD_OPT_OVLD);
}
switch (*pl->opts[CD_OPTION_OVERLOAD]->value) {
case 1:
field_scan(pkt->file, &pkt->cookie[0], pl->opts,
DHCP_LAST_OPT);
break;
case 2:
field_scan(pkt->sname, &pkt->file[0], pl->opts,
DHCP_LAST_OPT);
break;
case 3:
field_scan(pkt->file, &pkt->cookie[0], pl->opts,
DHCP_LAST_OPT);
field_scan(pkt->sname, &pkt->file[0], pl->opts,
DHCP_LAST_OPT);
break;
default:
pl->opts[CD_OPTION_OVERLOAD] = NULL;
return (DHCP_BAD_OPT_OVLD);
}
}
return (0);
}
dhcpv6_option_t *
dhcpv6_find_option(const void *buffer, size_t buflen,
const dhcpv6_option_t *oldopt, uint16_t codenum, uint_t *retlenp)
{
const uchar_t *bp;
dhcpv6_option_t d6o;
uint_t olen;
codenum = htons(codenum);
bp = buffer;
while (buflen >= sizeof (dhcpv6_option_t)) {
(void) memcpy(&d6o, bp, sizeof (d6o));
olen = ntohs(d6o.d6o_len) + sizeof (d6o);
if (olen > buflen)
break;
if (d6o.d6o_code != codenum ||
(oldopt != NULL && bp <= (const uchar_t *)oldopt)) {
bp += olen;
buflen -= olen;
continue;
}
if (retlenp != NULL)
*retlenp = olen;
return ((dhcpv6_option_t *)bp);
}
return (NULL);
}
dhcpv6_option_t *
dhcpv6_pkt_option(const PKT_LIST *plp, const dhcpv6_option_t *oldopt,
uint16_t codenum, uint_t *retlenp)
{
const dhcpv6_message_t *d6m;
if (plp == NULL || plp->pkt == NULL || plp->len < sizeof (*d6m))
return (NULL);
d6m = (const dhcpv6_message_t *)plp->pkt;
return (dhcpv6_find_option(d6m + 1, plp->len - sizeof (*d6m), oldopt,
codenum, retlenp));
}