#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dhcp.h"
#include "tree.h"
#include "dhcpd.h"
#include "log.h"
int bad_options = 0;
int bad_options_max = 5;
void parse_options(struct packet *);
void parse_option_buffer(struct packet *, unsigned char *, int);
void create_priority_list(unsigned char *, unsigned char *, int);
int store_option_fragment(unsigned char *, int, unsigned char,
int, unsigned char *);
int store_options(unsigned char *, int, struct tree_cache **,
unsigned char *, int, int);
void
parse_options(struct packet *packet)
{
memset(packet->options, 0, sizeof(packet->options));
if (memcmp(packet->raw->options, DHCP_OPTIONS_COOKIE, 4)) {
packet->options_valid = 0;
return;
}
parse_option_buffer(packet, &packet->raw->options[4],
packet->packet_length - DHCP_FIXED_NON_UDP - 4);
if (packet->options_valid &&
packet->options[DHO_DHCP_OPTION_OVERLOAD].data) {
if (packet->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 1)
parse_option_buffer(packet,
(unsigned char *)packet->raw->file,
sizeof(packet->raw->file));
if (packet->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 2)
parse_option_buffer(packet,
(unsigned char *)packet->raw->sname,
sizeof(packet->raw->sname));
}
}
void
parse_option_buffer(struct packet *packet,
unsigned char *buffer, int length)
{
unsigned char *s, *t;
unsigned char *end = buffer + length;
int len;
int code;
for (s = buffer; *s != DHO_END && s < end; ) {
code = s[0];
if (code == DHO_PAD) {
s++;
continue;
}
if (s + 2 > end) {
len = 65536;
goto bogus;
}
len = s[1];
if (s + len + 2 > end) {
bogus:
bad_options++;
log_warnx("option %s (%d) %s.",
dhcp_options[code].name, len,
"larger than buffer");
if (bad_options == bad_options_max) {
packet->options_valid = 1;
bad_options = 0;
log_warnx("Many bogus options seen in "
"offers.");
log_warnx("Taking this offer in spite of "
"bogus");
log_warnx("options - hope for the best!");
} else {
log_warnx("rejecting bogus offer.");
packet->options_valid = 0;
}
return;
}
if (!packet->options[code].data) {
t = calloc(1, len + 1);
if (!t)
fatalx("Can't allocate storage for option %s.",
dhcp_options[code].name);
memcpy(t, &s[2], len);
t[len] = 0;
packet->options[code].len = len;
packet->options[code].data = t;
} else {
t = calloc(1, len + packet->options[code].len + 1);
if (!t)
fatalx("Can't expand storage for option %s.",
dhcp_options[code].name);
memcpy(t, packet->options[code].data,
packet->options[code].len);
memcpy(t + packet->options[code].len,
&s[2], len);
packet->options[code].len += len;
t[packet->options[code].len] = 0;
free(packet->options[code].data);
packet->options[code].data = t;
}
s += len + 2;
}
packet->options_valid = 1;
}
void
create_priority_list(unsigned char *priority_list, unsigned char *prl,
int prl_len)
{
unsigned char stored_list[OPTIONS_LEN];
int i, priority_len = 0;
memset(&stored_list, 0, sizeof(stored_list));
stored_list[DHO_PAD] = 1;
stored_list[DHO_END] = 1;
for(i = 0; dhcp_option_default_priority_list[i] != DHO_END; i++) {
priority_list[priority_len++] =
dhcp_option_default_priority_list[i];
stored_list[dhcp_option_default_priority_list[i]] = 1;
}
if (!prl)
prl_len = 0;
for(i = 0; i < prl_len; i++) {
if (prl[i] == DHO_CLASSLESS_STATIC_ROUTES ||
prl[i] == DHO_CLASSLESS_MS_STATIC_ROUTES) {
if (stored_list[prl[i]] ||
priority_len >= OPTIONS_LEN)
continue;
priority_list[priority_len++] = prl[i];
stored_list[prl[i]] = 1;
}
}
for(i = 0; i < prl_len; i++) {
if (stored_list[prl[i]] ||
priority_len >= OPTIONS_LEN)
continue;
priority_list[priority_len++] = prl[i];
stored_list[prl[i]] = 1;
}
prl = dhcp_option_default_priority_list;
for(i = 0; i < OPTIONS_LEN; i++) {
if (stored_list[prl[i]] ||
priority_len >= OPTIONS_LEN)
continue;
priority_list[priority_len++] = prl[i];
stored_list[prl[i]] = 1;
}
}
int
cons_options(struct packet *inpacket, struct dhcp_packet *outpacket,
int mms, struct tree_cache **options,
int overload,
int terminate, int bootpp, u_int8_t *prl, int prl_len)
{
unsigned char priority_list[OPTIONS_LEN];
unsigned char buffer[4096];
int bufix, main_buffer_size, option_size;
if (!mms &&
inpacket &&
inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].data &&
(inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].len >=
sizeof(u_int16_t))) {
mms = getUShort(
inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].data);
}
if (mms) {
if (mms < 576)
mms = 576;
main_buffer_size = mms - DHCP_FIXED_LEN;
} else if (bootpp)
main_buffer_size = 64;
else
main_buffer_size = 576 - DHCP_FIXED_LEN;
if (main_buffer_size > sizeof(outpacket->options))
main_buffer_size = sizeof(outpacket->options);
memset(outpacket->options, DHO_PAD, sizeof(outpacket->options));
if (overload & 1)
memset(outpacket->file, DHO_PAD, DHCP_FILE_LEN);
if (overload & 2)
memset(outpacket->sname, DHO_PAD, DHCP_SNAME_LEN);
if (bootpp)
overload = 0;
memset(&priority_list, 0, sizeof(priority_list));
if (inpacket &&
inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].data)
create_priority_list(priority_list,
inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].data,
inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].len);
else if (prl)
create_priority_list(priority_list, prl, prl_len);
else
create_priority_list(priority_list, NULL, 0);
option_size = store_options(buffer, main_buffer_size, options,
priority_list, overload, terminate);
if (option_size == 0)
return (DHCP_FIXED_NON_UDP);
memcpy(&outpacket->options[0], buffer, main_buffer_size);
if (option_size <= main_buffer_size)
return (DHCP_FIXED_NON_UDP + option_size);
bufix = main_buffer_size;
if (overload & 1) {
memcpy(outpacket->file, &buffer[bufix], DHCP_FILE_LEN);
bufix += DHCP_FILE_LEN;
}
if (overload & 2)
memcpy(outpacket->sname, &buffer[bufix], DHCP_SNAME_LEN);
return (DHCP_FIXED_NON_UDP + main_buffer_size);
}
int
store_option_fragment(unsigned char *buffer, int buffer_size,
unsigned char code, int length, unsigned char *data)
{
buffer_size -= 2;
if (buffer_size < 1)
return (0);
if (buffer_size > 255)
buffer_size = 255;
if (length > buffer_size)
length = buffer_size;
buffer[0] = code;
buffer[1] = length;
memcpy(&buffer[2], data, length);
return (length + 2);
}
int
store_options(unsigned char *buffer, int main_buffer_size,
struct tree_cache **options, unsigned char *priority_list, int overload,
int terminate)
{
int buflen, code, cutoff, i, incr, ix, length, optstart, overflow;
int second_cutoff;
int bufix = 0;
int stored_classless = 0;
overload &= 3;
cutoff = main_buffer_size;
second_cutoff = cutoff + ((overload & 1) ? DHCP_FILE_LEN : 0);
buflen = second_cutoff + ((overload & 2) ? DHCP_SNAME_LEN : 0);
memset(buffer, DHO_PAD, buflen);
memcpy(buffer, DHCP_OPTIONS_COOKIE, 4);
if (overload)
bufix = 7;
else
bufix = 4;
for (i = 0; i < OPTIONS_LEN; i++) {
code = priority_list[i];
if (code == DHO_PAD || code == DHO_END)
continue;
if (!options[code] || !tree_evaluate(options[code]))
continue;
if ((code == DHO_ROUTERS || code == DHO_STATIC_ROUTES) &&
stored_classless)
continue;
length = options[code]->len;
optstart = bufix;
ix = 0;
while (length) {
incr = store_option_fragment(&buffer[bufix],
cutoff - bufix, code, length,
options[code]->value + ix);
if (incr > 0) {
bufix += incr;
length -= incr - 2;
ix += incr - 2;
continue;
}
memset(&buffer[bufix], DHO_PAD, cutoff - bufix);
bufix = cutoff;
if (cutoff < second_cutoff)
cutoff = second_cutoff;
else if (cutoff < buflen)
cutoff = buflen;
else
break;
}
if (length > 0) {
zapfrags:
memset(&buffer[optstart], DHO_PAD, buflen - optstart);
bufix = optstart;
} else if (terminate && dhcp_options[code].format[0] == 't') {
if (bufix < cutoff)
buffer[bufix++] = '\0';
else
goto zapfrags;
}
if (code == DHO_CLASSLESS_STATIC_ROUTES ||
code == DHO_CLASSLESS_MS_STATIC_ROUTES)
stored_classless = 1;
}
if (bufix == (4 + (overload ? 3 : 0)))
return (0);
if (bufix < buflen)
buffer[bufix++] = DHO_END;
if (overload) {
overflow = bufix - main_buffer_size;
if (overflow > 0) {
buffer[4] = DHO_DHCP_OPTION_OVERLOAD;
buffer[5] = 1;
if (overload & 1) {
buffer[6] |= 1;
overflow -= DHCP_FILE_LEN;
}
if ((overload & 2) && overflow > 0)
buffer[6] |= 2;
} else {
memmove(&buffer[4], &buffer[7], buflen - 7);
bufix -= 3;
memset(&buffer[bufix], DHO_PAD, 3);
}
}
return (bufix);
}
void
do_packet(struct interface_info *interface, struct dhcp_packet *packet,
int len, unsigned int from_port, struct iaddr from, struct hardware *hfrom)
{
struct packet tp;
int i;
if (packet->hlen > sizeof(packet->chaddr)) {
log_info("Discarding packet with invalid hlen.");
return;
}
memset(&tp, 0, sizeof(tp));
tp.raw = packet;
tp.packet_length = len;
tp.client_port = from_port;
tp.client_addr = from;
tp.interface = interface;
tp.haddr = hfrom;
parse_options(&tp);
if (tp.options_valid &&
tp.options[DHO_DHCP_MESSAGE_TYPE].data)
tp.packet_type = tp.options[DHO_DHCP_MESSAGE_TYPE].data[0];
if (tp.packet_type)
dhcp(&tp, interface->is_udpsock);
else
bootp(&tp);
for (i = 0; i < OPTIONS_LEN; i++)
free(tp.options[i].data);
}