2
0
mirror of https://gitlab.isc.org/isc-projects/dhcp synced 2025-08-22 18:07:25 +00:00
isc-dhcp/common/options.c

1079 lines
29 KiB
C
Raw Normal View History

1995-11-29 07:40:04 +00:00
/* options.c
DHCP options parsing and reassembly. */
/*
* Copyright (c) 1995, 1996, 1998 The Internet Software Consortium.
1996-03-02 05:13:36 +00:00
* All rights reserved.
1995-11-29 07:40:04 +00:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of The Internet Software Consortium nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This software has been written for the Internet Software Consortium
* by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
* Enterprises. To learn more about the Internet Software Consortium,
* see ``http://www.vix.com/isc''. To learn more about Vixie
* Enterprises, see ``http://www.vix.com''.
*/
#ifndef lint
static char copyright[] =
"$Id: options.c,v 1.35 1999/02/24 17:56:46 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n";
1995-11-29 07:40:04 +00:00
#endif /* not lint */
#define DHCP_OPTION_DATA
#include "dhcpd.h"
1998-11-05 18:42:47 +00:00
static void do_option_set PROTO ((pair *,
struct option_cache *,
enum statement_op));
1995-11-29 07:40:04 +00:00
/* Parse all available options out of the specified packet. */
1998-11-05 18:42:47 +00:00
int parse_options (packet)
1995-11-29 07:40:04 +00:00
struct packet *packet;
{
1998-11-05 18:42:47 +00:00
int i;
struct option_cache *op = (struct option_cache *)0;
1995-11-29 07:40:04 +00:00
/* Initially, zero all option pointers. */
1998-11-05 18:42:47 +00:00
memset (&packet -> options, 0, sizeof (packet -> options));
1995-11-29 07:40:04 +00:00
/* If we don't see the magic cookie, there's nothing to parse. */
if (memcmp (packet -> raw -> options, DHCP_OPTIONS_COOKIE, 4)) {
packet -> options_valid = 0;
1998-11-05 18:42:47 +00:00
return 1;
1995-11-29 07:40:04 +00:00
}
/* Go through the options field, up to the end of the packet
or the End field. */
1998-11-05 18:42:47 +00:00
if (!parse_option_buffer (packet, &packet -> raw -> options [4],
(packet -> packet_length -
DHCP_FIXED_NON_UDP - 4)))
return 0;
1995-11-29 07:40:04 +00:00
/* If we parsed a DHCP Option Overload option, parse more
options out of the buffer(s) containing them. */
1998-11-05 18:42:47 +00:00
if (packet -> options_valid &&
(op = lookup_option (packet -> options.dhcp_hash,
DHO_DHCP_OPTION_OVERLOAD))) {
if (op -> data.data [0] & 1) {
if (!parse_option_buffer
(packet, (unsigned char *)packet -> raw -> file,
sizeof packet -> raw -> file))
return 0;
}
if (op -> data.data [0] & 2) {
if (!parse_option_buffer
(packet,
(unsigned char *)packet -> raw -> sname,
sizeof packet -> raw -> sname))
return 0;
}
1995-11-29 07:40:04 +00:00
}
1998-11-05 18:42:47 +00:00
return 1;
1995-11-29 07:40:04 +00:00
}
/* Parse options out of the specified buffer, storing addresses of option
values in packet -> options and setting packet -> options_valid if no
errors are encountered. */
1998-11-05 18:42:47 +00:00
int parse_option_buffer (packet, buffer, length)
1995-11-29 07:40:04 +00:00
struct packet *packet;
unsigned char *buffer;
int length;
{
1998-11-05 18:42:47 +00:00
unsigned char *t;
1995-11-29 07:40:04 +00:00
unsigned char *end = buffer + length;
1998-11-05 18:42:47 +00:00
int len, offset;
1995-11-29 07:40:04 +00:00
int code;
1998-11-05 18:42:47 +00:00
struct option_cache *op = (struct option_cache *)0;
struct buffer *bp = (struct buffer *)0;
1995-11-29 07:40:04 +00:00
1998-11-05 18:42:47 +00:00
if (!buffer_allocate (&bp, length, "parse_option_buffer")) {
log_error ("parse_option_buffer: no memory for option buffer.");
1998-11-05 18:42:47 +00:00
return 0;
}
memcpy (bp -> data, buffer, length);
for (offset = 0; buffer [offset] != DHO_END && offset < length; ) {
code = buffer [offset];
1995-11-29 07:40:04 +00:00
/* Pad options don't have a length - just skip them. */
if (code == DHO_PAD) {
1998-11-05 18:42:47 +00:00
++offset;
1995-11-29 07:40:04 +00:00
continue;
}
1998-11-05 18:42:47 +00:00
1995-11-29 07:40:04 +00:00
/* All other fields (except end, see above) have a
one-byte length. */
1998-11-05 18:42:47 +00:00
len = buffer [offset + 1];
1995-11-29 07:40:04 +00:00
/* If the length is outrageous, the options are bad. */
1998-11-05 18:42:47 +00:00
if (offset + len + 2 > length) {
log_error ("Option %s length %d overflows input buffer.",
1995-11-29 07:40:04 +00:00
dhcp_options [code].name,
len);
1998-11-05 18:42:47 +00:00
buffer_dereference (&bp, "parse_option_buffer");
return 0;
1995-11-29 07:40:04 +00:00
}
/* If this is a Relay Agent Information option, we must
handle it specially. */
if (code == DHO_DHCP_AGENT_OPTIONS) {
1998-11-05 18:42:47 +00:00
if (!parse_agent_information_option
(packet, len, buffer + offset + 2)) {
log_error ("malformed agent information option.");
1998-11-05 18:42:47 +00:00
buffer_dereference (&bp,
"parse_option_buffer");
return 0;
}
1995-11-29 07:40:04 +00:00
} else {
1998-11-05 18:42:47 +00:00
if (!option_cache_allocate (&op,
"parse_option_buffer")) {
log_error ("Can't allocate storage for option %s.",
1998-11-05 18:42:47 +00:00
dhcp_options [code].name);
buffer_dereference (&bp,
"parse_option_buffer");
return 0;
}
/* Reference buffer copy to option cache. */
op -> data.buffer = (struct buffer *)0;
buffer_reference (&op -> data.buffer, bp,
"parse_option_buffer");
/* Point option cache into buffer. */
op -> data.data = &bp -> data [offset + 2];
op -> data.len = len;
/* NUL terminate (we can get away with this
because we allocated one more than the
buffer size, and because the byte following
the end of an option is always the code of
the next option, which we're getting out of
the *original* buffer. */
bp -> data [offset + 2 + len] = 0;
op -> data.terminated = 1;
op -> option = &dhcp_options [code];
/* Now store the option. */
save_option (packet -> options.dhcp_hash, op);
/* And let go of our reference. */
option_cache_dereference (&op,
"parse_option_buffer");
1995-11-29 07:40:04 +00:00
}
1998-11-05 18:42:47 +00:00
offset += len + 2;
1995-11-29 07:40:04 +00:00
}
packet -> options_valid = 1;
1998-11-05 18:42:47 +00:00
buffer_dereference (&bp, "parse_option_buffer");
return 1;
1995-11-29 07:40:04 +00:00
}
/* Parse a Relay Agent Information option and put it at the end of the
list of such options on the specified packet. */
int parse_agent_information_option (packet, len, data)
struct packet *packet;
int len;
u_int8_t *data;
{
struct agent_options *a, **tail;
struct option_tag *t, *oth = 0, **ott = &oth;
u_int8_t *op = data, *max = data + len;
/* Parse the agent information option suboptions. */
while (op < max) {
/* Check for overflow. */
if (op + 1 == max || op + op [1] + 2 > max)
return 0;
/* Make space for this suboption. */
1998-11-05 18:42:47 +00:00
t = (struct option_tag *)
dmalloc (op [1] + 1 + sizeof *t,
"parse_agent_information_option");
if (!t)
log_fatal ("can't allocate space for option tag data.");
/* Link it in at the tail of the list. */
t -> next = (struct option_tag *)0;
*ott = t;
ott = &t -> next;
/* Copy the option data in in its raw form. */
memcpy (t -> data, op, op [1] + 2);
op += op [1] + 2;
}
/* Make an agent options structure to put on the list. */
1998-11-05 18:42:47 +00:00
a = (struct agent_options *)dmalloc (sizeof *a,
"parse_agent_information_option");
if (!a)
log_fatal ("can't allocate space for agent option structure.");
/* Find the tail of the list. */
1998-11-05 18:42:47 +00:00
for (tail = &packet -> options.agent_options;
*tail; tail = &((*tail) -> next))
;
*tail = a;
a -> next = (struct agent_options *)0;
a -> first = oth;
a -> length = len;
return 1;
}
/* cons options into a big buffer, and then split them out into the
three seperate buffers if needed. This allows us to cons up a set
of vendor options using the same routine. */
int cons_options (inpacket, outpacket, mms, options,
agent_options, overload, terminate, bootpp)
struct packet *inpacket;
struct dhcp_packet *outpacket;
int mms;
struct option_state *options;
struct agent_options *agent_options;
int overload; /* Overload flags that may be set. */
int terminate;
int bootpp;
{
1998-11-05 18:42:47 +00:00
#define PRIORITY_COUNT 300
int priority_list [PRIORITY_COUNT];
int priority_len;
unsigned char buffer [4096]; /* Really big buffer... */
int main_buffer_size;
int mainbufix, bufix, agentix;
int option_size;
int length;
1998-11-05 18:42:47 +00:00
int i;
struct option_cache *op;
struct data_string ds;
pair pp;
memset (&ds, 0, sizeof ds);
/* If there's a Maximum Message Size option in the incoming packet
and no alternate maximum message size has been specified, take the
one in the packet. */
1998-11-05 18:42:47 +00:00
if (!mms && inpacket &&
(op = lookup_option (inpacket -> options.dhcp_hash,
DHO_DHCP_MAX_MESSAGE_SIZE))) {
evaluate_option_cache (&ds, inpacket,
&inpacket -> options, op);
if (ds.len >= sizeof (u_int16_t))
mms = getUShort (ds.data);
data_string_forget (&ds, "cons_options");
}
/* If the client has provided a maximum DHCP message size,
use that; otherwise, if it's BOOTP, only 64 bytes; otherwise
use up to the minimum IP MTU size (576 bytes). */
/* XXX if a BOOTP client specifies a max message size, we will
honor it. */
if (mms) {
main_buffer_size = mms - DHCP_FIXED_LEN;
1996-03-16 17:50:30 +00:00
/* Enforce a minimum packet size... */
if (main_buffer_size < (576 - DHCP_FIXED_LEN))
main_buffer_size = 576 - DHCP_FIXED_LEN;
} else if (bootpp) {
if (inpacket) {
main_buffer_size =
inpacket -> packet_length - DHCP_FIXED_LEN;
if (main_buffer_size < 64)
main_buffer_size = 64;
} else
main_buffer_size = 64;
} else
main_buffer_size = 576 - DHCP_FIXED_LEN;
/* Set a hard limit at the size of the output buffer. */
if (main_buffer_size > sizeof buffer)
main_buffer_size = sizeof buffer;
/* Preload the option priority list with mandatory options. */
priority_len = 0;
priority_list [priority_len++] = DHO_DHCP_MESSAGE_TYPE;
priority_list [priority_len++] = DHO_DHCP_SERVER_IDENTIFIER;
priority_list [priority_len++] = DHO_DHCP_LEASE_TIME;
priority_list [priority_len++] = DHO_DHCP_MESSAGE;
1998-11-05 18:42:47 +00:00
priority_list [priority_len++] = DHO_DHCP_REQUESTED_ADDRESS;
/* If the client has provided a list of options that it wishes
returned, use it to prioritize. Otherwise, prioritize
based on the default priority list. */
1998-11-05 18:42:47 +00:00
if (inpacket)
op = lookup_option (inpacket -> options.dhcp_hash,
DHO_DHCP_PARAMETER_REQUEST_LIST);
else
op = (struct option_cache *)0;
if (op)
evaluate_option_cache (&ds, inpacket,
&inpacket -> options, op);
if (ds.len > 0) {
data_string_truncate (&ds,
(PRIORITY_COUNT - priority_len));
for (i = 0; i < ds.len; i++)
priority_list [priority_len++] = ds.data [i];
data_string_forget (&ds, "cons_options");
} else {
1998-11-05 18:42:47 +00:00
/* First, hardcode some more options that ought to be
sent first... */
priority_list [priority_len++] = DHO_SUBNET_MASK;
priority_list [priority_len++] = DHO_ROUTERS;
priority_list [priority_len++] = DHO_DOMAIN_NAME_SERVERS;
priority_list [priority_len++] = DHO_HOST_NAME;
/* Now just tack on the list of all the options we have,
and any duplicates will be eliminated. */
for (i = 0; i < OPTION_HASH_SIZE; i++) {
for (pp = options -> dhcp_hash [i];
pp; pp = pp -> cdr) {
op = (struct option_cache *)(pp -> car);
if (priority_len < PRIORITY_COUNT)
priority_list [priority_len++] =
op -> option -> code;
}
}
}
/* Copy the options into the big buffer... */
option_size = store_options (buffer,
(main_buffer_size - 7 +
((overload & 1) ? DHCP_FILE_LEN : 0) +
((overload & 2) ? DHCP_SNAME_LEN : 0)),
1998-11-05 18:42:47 +00:00
options,
priority_list, priority_len,
main_buffer_size,
(main_buffer_size +
((overload & 1) ? DHCP_FILE_LEN : 0)),
terminate);
/* Put the cookie up front... */
memcpy (outpacket -> options, DHCP_OPTIONS_COOKIE, 4);
mainbufix = 4;
/* If we're going to have to overload, store the overload
option at the beginning. If we can, though, just store the
whole thing in the packet's option buffer and leave it at
that. */
if (option_size <= main_buffer_size - mainbufix) {
memcpy (&outpacket -> options [mainbufix],
buffer, option_size);
mainbufix += option_size;
if (mainbufix < main_buffer_size) {
agentix = mainbufix;
1998-11-05 18:42:47 +00:00
outpacket -> options [mainbufix++] = DHO_END;
} else
agentix = mainbufix;
length = DHCP_FIXED_NON_UDP + mainbufix;
} else {
1998-11-05 18:42:47 +00:00
outpacket -> options [mainbufix++] = DHO_DHCP_OPTION_OVERLOAD;
outpacket -> options [mainbufix++] = 1;
if (option_size > main_buffer_size - mainbufix + DHCP_FILE_LEN)
outpacket -> options [mainbufix++] = 3;
else
outpacket -> options [mainbufix++] = 1;
memcpy (&outpacket -> options [mainbufix],
buffer, main_buffer_size - mainbufix);
length = DHCP_FIXED_NON_UDP + main_buffer_size;
agentix = main_buffer_size;
bufix = main_buffer_size - mainbufix;
if (overload & 1) {
if (option_size - bufix <= DHCP_FILE_LEN) {
memcpy (outpacket -> file,
&buffer [bufix], option_size - bufix);
mainbufix = option_size - bufix;
if (mainbufix < DHCP_FILE_LEN)
outpacket -> file [mainbufix++]
= DHO_END;
while (mainbufix < DHCP_FILE_LEN)
outpacket -> file [mainbufix++]
= DHO_PAD;
} else {
memcpy (outpacket -> file,
&buffer [bufix], DHCP_FILE_LEN);
bufix += DHCP_FILE_LEN;
}
}
if ((overload & 2) && option_size < bufix) {
memcpy (outpacket -> sname,
&buffer [bufix], option_size - bufix);
mainbufix = option_size - bufix;
if (mainbufix < DHCP_SNAME_LEN)
outpacket -> file [mainbufix++]
= DHO_END;
while (mainbufix < DHCP_SNAME_LEN)
outpacket -> file [mainbufix++]
= DHO_PAD;
}
}
/* We tack any agent options onto the end of the packet after
we've put it together. */
if (agent_options) {
int len = 0;
struct agent_options *a;
struct option_tag *o;
/* Cycle through the options, appending them to the
buffer. */
for (a = options -> agent_options; a; a = a -> next) {
1998-11-05 18:42:47 +00:00
if (agentix + a -> length + 3 + DHCP_FIXED_LEN <=
dhcp_max_agent_option_packet_length) {
outpacket -> options [agentix++]
= DHO_DHCP_AGENT_OPTIONS;
outpacket -> options [agentix++] = a -> length;
for (o = a -> first; o; o = o -> next) {
memcpy (&outpacket -> options [agentix],
o -> data, o -> data [1] + 2);
agentix += o -> data [1] + 2;
}
}
}
/* Reterminate the packet. */
outpacket -> options [agentix++] = DHO_END;
/* Recompute the length, which may now be higher than the
client can accept but should be okay for the relay agent. */
length = agentix + DHCP_FIXED_NON_UDP;
}
return length;
}
/* Store all the requested options into the requested buffer. */
int store_options (buffer, buflen, options, priority_list, priority_len,
first_cutoff, second_cutoff, terminate)
unsigned char *buffer;
int buflen;
1998-11-05 18:42:47 +00:00
struct option_state *options;
int *priority_list;
int priority_len;
int first_cutoff, second_cutoff;
int terminate;
{
int bufix = 0;
int i;
int ix;
int tto;
struct data_string od;
1998-11-05 18:42:47 +00:00
struct option_cache *oc;
1998-11-05 18:42:47 +00:00
memset (&od, 0, sizeof od);
/* Eliminate duplicate options in the parameter request list.
There's got to be some clever knuthian way to do this:
Eliminate all but the first occurance of a value in an array
of values without otherwise disturbing the order of the array. */
for (i = 0; i < priority_len - 1; i++) {
tto = 0;
for (ix = i + 1; ix < priority_len + tto; ix++) {
if (tto)
priority_list [ix - tto] =
priority_list [ix];
if (priority_list [i] == priority_list [ix]) {
tto++;
priority_len--;
}
}
}
/* Copy out the options in the order that they appear in the
priority list... */
for (i = 0; i < priority_len; i++) {
/* Code for next option to try to store. */
int code = priority_list [i];
int optstart;
/* Number of bytes left to store (some may already
have been stored by a previous pass). */
int length;
/* If no data is available for this option, skip it. */
1998-11-05 18:42:47 +00:00
if (!(oc = lookup_option (options -> dhcp_hash, code))) {
continue;
}
/* Find the value of the option... */
1998-11-05 18:42:47 +00:00
evaluate_option_cache (&od, (struct packet *)0,
(struct option_state *)0, oc);
if (!od.len) {
1998-04-09 04:31:59 +00:00
continue;
1998-11-05 18:42:47 +00:00
}
1998-04-09 04:31:59 +00:00
/* We should now have a constant length for the option. */
length = od.len;
/* Do we add a NUL? */
if (terminate && dhcp_options [code].format [0] == 't') {
length++;
tto = 1;
} else {
tto = 0;
}
/* Try to store the option. */
/* If the option's length is more than 255, we must store it
in multiple hunks. Store 255-byte hunks first. However,
in any case, if the option data will cross a buffer
boundary, split it across that boundary. */
ix = 0;
optstart = bufix;
while (length) {
unsigned char incr = length > 255 ? 255 : length;
/* If this hunk of the buffer will cross a
boundary, only go up to the boundary in this
pass. */
if (bufix < first_cutoff &&
bufix + incr > first_cutoff)
incr = first_cutoff - bufix;
else if (bufix < second_cutoff &&
bufix + incr > second_cutoff)
incr = second_cutoff - bufix;
/* If this option is going to overflow the buffer,
skip it. */
if (bufix + 2 + incr > buflen) {
bufix = optstart;
break;
}
/* Everything looks good - copy it in! */
buffer [bufix] = code;
buffer [bufix + 1] = incr;
if (tto && incr == length) {
memcpy (buffer + bufix + 2,
od.data + ix, incr - 1);
buffer [bufix + 2 + incr - 1] = 0;
} else {
memcpy (buffer + bufix + 2,
od.data + ix, incr);
}
length -= incr;
ix += incr;
bufix += 2 + incr;
}
1998-11-05 18:42:47 +00:00
data_string_forget (&od, "store_options");
}
return bufix;
1995-11-29 07:40:04 +00:00
}
/* Format the specified option so that a human can easily read it. */
char *pretty_print_option (code, data, len, emit_commas, emit_quotes)
unsigned int code;
1995-11-29 07:40:04 +00:00
unsigned char *data;
int len;
int emit_commas;
int emit_quotes;
1995-11-29 07:40:04 +00:00
{
static char optbuf [32768]; /* XXX */
int hunksize = 0;
int numhunk = -1;
int numelem = 0;
char fmtbuf [32];
int i, j;
char *op = optbuf;
unsigned char *dp = data;
struct in_addr foo;
char comma;
1995-11-29 07:40:04 +00:00
/* Code should be between 0 and 255. */
if (code > 255)
log_fatal ("pretty_print_option: bad code %d\n", code);
if (emit_commas)
comma = ',';
else
comma = ' ';
1995-11-29 07:40:04 +00:00
/* Figure out the size of the data. */
for (i = 0; dhcp_options [code].format [i]; i++) {
if (!numhunk) {
log_error ("%s: Excess information in format string: %s\n",
1995-11-29 07:40:04 +00:00
dhcp_options [code].name,
&(dhcp_options [code].format [i]));
break;
}
numelem++;
fmtbuf [i] = dhcp_options [code].format [i];
switch (dhcp_options [code].format [i]) {
case 'A':
--numelem;
fmtbuf [i] = 0;
numhunk = 0;
break;
case 'X':
fmtbuf [i] = 'x';
fmtbuf [i + 1] = 0;
hunksize++;
numhunk = 0;
comma = ':';
break;
1995-11-29 07:40:04 +00:00
case 't':
fmtbuf [i] = 't';
fmtbuf [i + 1] = 0;
numhunk = -2;
break;
case 'I':
case 'l':
case 'L':
hunksize += 4;
break;
case 's':
case 'S':
hunksize += 2;
break;
case 'b':
case 'B':
case 'f':
hunksize++;
break;
case 'e':
break;
default:
log_error ("%s: garbage in format string: %s\n",
1995-11-29 07:40:04 +00:00
dhcp_options [code].name,
&(dhcp_options [code].format [i]));
break;
}
}
/* Check for too few bytes... */
if (hunksize > len) {
log_error ("%s: expecting at least %d bytes; got %d",
1995-11-29 07:40:04 +00:00
dhcp_options [code].name,
hunksize, len);
return "<error>";
}
/* Check for too many bytes... */
if (numhunk == -1 && hunksize < len)
log_error ("%s: %d extra bytes",
1995-11-29 07:40:04 +00:00
dhcp_options [code].name,
len - hunksize);
/* If this is an array, compute its size. */
if (!numhunk)
numhunk = len / hunksize;
/* See if we got an exact number of hunks. */
if (numhunk > 0 && numhunk * hunksize < len)
log_error ("%s: %d extra bytes at end of array\n",
1995-11-29 07:40:04 +00:00
dhcp_options [code].name,
len - numhunk * hunksize);
/* A one-hunk array prints the same as a single hunk. */
if (numhunk < 0)
numhunk = 1;
/* Cycle through the array (or hunk) printing the data. */
for (i = 0; i < numhunk; i++) {
for (j = 0; j < numelem; j++) {
switch (fmtbuf [j]) {
case 't':
if (emit_quotes)
*op++ = '"';
strcpy (op, (char *)dp);
op += strlen ((char *)dp);
if (emit_quotes)
*op++ = '"';
*op = 0;
1995-11-29 07:40:04 +00:00
break;
case 'I':
foo.s_addr = htonl (getULong (dp));
strcpy (op, inet_ntoa (foo));
dp += 4;
break;
case 'l':
1996-09-05 23:57:33 +00:00
sprintf (op, "%ld", (long)getLong (dp));
1995-11-29 07:40:04 +00:00
dp += 4;
break;
case 'L':
1996-09-05 23:57:33 +00:00
sprintf (op, "%ld",
(unsigned long)getULong (dp));
1995-11-29 07:40:04 +00:00
dp += 4;
break;
case 's':
sprintf (op, "%d", getShort (dp));
dp += 2;
break;
case 'S':
sprintf (op, "%d", getUShort (dp));
dp += 2;
break;
case 'b':
sprintf (op, "%d", *(char *)dp++);
break;
case 'B':
sprintf (op, "%d", *dp++);
break;
case 'x':
sprintf (op, "%x", *dp++);
break;
1995-11-29 07:40:04 +00:00
case 'f':
strcpy (op, *dp++ ? "true" : "false");
break;
default:
log_error ("Unexpected format code %c", fmtbuf [j]);
1995-11-29 07:40:04 +00:00
}
op += strlen (op);
if (j + 1 < numelem && comma != ':')
*op++ = ' ';
}
if (i + 1 < numhunk) {
*op++ = comma;
1995-11-29 07:40:04 +00:00
}
1995-11-29 07:40:04 +00:00
}
return optbuf;
}
void do_packet (interface, packet, len, from_port, from, hfrom)
struct interface_info *interface;
struct dhcp_packet *packet;
int len;
unsigned int from_port;
struct iaddr from;
struct hardware *hfrom;
{
struct packet tp;
1998-11-05 18:42:47 +00:00
int i;
struct option_cache *op;
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;
if (packet -> hlen > sizeof packet -> chaddr) {
log_info ("Discarding packet with bogus hardware address length.");
return;
}
1998-11-05 18:42:47 +00:00
if (!parse_options (&tp)) {
option_state_dereference (&tp.options);
return;
}
if (tp.options_valid &&
1998-11-05 18:42:47 +00:00
(op = lookup_option (tp.options.dhcp_hash,
DHO_DHCP_MESSAGE_TYPE))) {
struct data_string dp;
memset (&dp, 0, sizeof dp);
evaluate_option_cache (&dp, &tp, &tp.options, op);
if (dp.len > 0)
tp.packet_type = dp.data [0];
else
tp.packet_type = 0;
data_string_forget (&dp, "do_packet");
}
if (tp.packet_type)
dhcp (&tp);
else
bootp (&tp);
1998-11-05 18:42:47 +00:00
option_state_dereference (&tp.options);
}
1998-11-05 18:42:47 +00:00
int dhcp_option_lookup (result, options, code)
struct data_string *result;
struct option_state *options;
int code;
{
1998-11-05 18:42:47 +00:00
struct option_cache *oc;
1998-11-05 18:42:47 +00:00
if (!(oc = lookup_option (options -> dhcp_hash, code)))
return 0;
if (!evaluate_option_cache (result, (struct packet *)0, options, oc))
return 0;
return 1;
}
1998-11-05 18:42:47 +00:00
int agent_suboption_lookup (result, options, code)
struct data_string *result;
struct option_state *options;
int code;
{
struct agent_options *ao;
struct option_tag *t;
/* Find the last set of agent options and consider it definitive. */
1998-11-05 18:42:47 +00:00
for (ao = options -> agent_options; ao -> next; ao = ao -> next)
;
if (ao) {
1998-11-05 18:42:47 +00:00
for (t = ao -> first; t; t = t -> next) {
if (t -> data [0] == code) {
1998-11-05 18:42:47 +00:00
result -> len = t -> data [1];
if (!buffer_allocate (&result -> buffer,
result -> len + 1,
"agent_suboption_lookup"
)) {
result -> len = 0;
buffer_dereference
(&result -> buffer,
"agent_suboption_lookup");
return 0;
}
result -> data = &result -> buffer -> data [0];
memcpy (result -> data,
&t -> data [2], result -> len);
result -> data [result -> len] = 0;
result -> terminated = 1;
return 1;
}
1998-11-05 18:42:47 +00:00
}
}
1998-11-05 18:42:47 +00:00
return 0;
}
1998-11-05 18:42:47 +00:00
int server_option_lookup (result, options, code)
struct data_string *result;
struct option_state *options;
int code;
{
1998-11-05 18:42:47 +00:00
return 0;
}
void dhcp_option_set (options, option, op)
struct option_state *options;
struct option_cache *option;
enum statement_op op;
{
struct option_cache *thecache;
1998-11-05 18:42:47 +00:00
do_option_set (options -> dhcp_hash, option, op);
}
void server_option_set (options, option, op)
struct option_state *options;
struct option_cache *option;
enum statement_op op;
{
1998-11-05 18:42:47 +00:00
do_option_set (options -> server_hash, option, op);
}
1998-11-05 18:42:47 +00:00
static void do_option_set (hash, option, op)
pair *hash;
struct option_cache *option;
enum statement_op op;
{
1998-11-05 18:42:47 +00:00
struct option_cache *oc, *noc;
switch (op) {
case if_statement:
case add_statement:
case eval_statement:
case break_statement:
default:
log_error ("bogus statement type in do_option_set.");
break;
case default_option_statement:
1998-11-05 18:42:47 +00:00
oc = lookup_option (hash, option -> option -> code);
if (oc)
break;
1998-11-05 18:42:47 +00:00
save_option (hash, option);
break;
case supersede_option_statement:
1998-11-05 18:42:47 +00:00
/* Install the option, replacing any existing version. */
save_option (hash, option);
break;
case append_option_statement:
case prepend_option_statement:
1998-11-05 18:42:47 +00:00
oc = lookup_option (hash, option -> option -> code);
if (!oc) {
save_option (hash, option);
break;
}
1998-11-05 18:42:47 +00:00
/* If it's not an expression, make it into one. */
if (!oc -> expression && oc -> data.len) {
if (!expression_allocate (&oc -> expression,
"do_option_set")) {
log_error ("Can't allocate const expression.");
1998-11-05 18:42:47 +00:00
break;
}
oc -> expression -> op = expr_const_data;
data_string_copy
(&oc -> expression -> data.const_data,
&oc -> data, "do_option_set");
data_string_forget (&oc -> data, "do_option_set");
}
noc = (struct option_cache *)0;
if (!option_cache_allocate (&noc, "do_option_set"))
break;
if (op == append_option_statement) {
if (!make_concat (&noc -> expression,
oc -> expression,
option -> expression)) {
option_cache_dereference (&noc,
"do_option_set");
break;
}
} else {
if (!make_concat (&noc -> expression,
option -> expression,
oc -> expression)) {
option_cache_dereference (&noc,
"do_option_set");
break;
}
}
1998-11-05 18:42:47 +00:00
noc -> option = oc -> option;
save_option (hash, noc);
option_cache_dereference (&noc, "do_option_set");
break;
}
}
struct option_cache *lookup_option (hash, code)
pair *hash;
int code;
{
int hashix;
pair bptr;
hashix = ((code & 31) + ((code >> 5) & 31)) % 17;
for (bptr = hash [hashix]; bptr; bptr = bptr -> cdr) {
if (((struct option_cache *)(bptr -> car)) -> option -> code ==
code)
return (struct option_cache *)(bptr -> car);
}
return (struct option_cache *)0;
}
void save_option (hash, oc)
pair *hash;
struct option_cache *oc;
{
int hashix;
pair bptr;
/* Try to find an existing option matching the new one. */
hashix = ((oc -> option -> code & 31) +
((oc -> option -> code >> 5) & 31)) % 17;
for (bptr = hash [hashix]; bptr; bptr = bptr -> cdr) {
if (((struct option_cache *)(bptr -> car)) -> option -> code ==
oc -> option -> code)
break;
}
/* If we find one, dereference it and put the new one in its place. */
if (bptr) {
option_cache_dereference ((struct option_cache **)&bptr -> car,
"save_option");
option_cache_reference ((struct option_cache **)&bptr -> car,
oc, "save_option");
} else {
/* Otherwise, just put the new one at the head of the list. */
bptr = new_pair ("save_option");
if (!bptr) {
log_error ("No memory for option_cache reference.");
1998-11-05 18:42:47 +00:00
return;
}
bptr -> cdr = hash [hashix];
bptr -> car = 0;
option_cache_reference ((struct option_cache **)&bptr -> car,
oc, "save_option");
hash [hashix] = bptr;
}
}
void delete_option (hash, code)
pair *hash;
int code;
{
int hashix;
pair bptr, prev = (pair)0;
/* Try to find an existing option matching the new one. */
hashix = ((code & 31) +
((code >> 5) & 31)) % 17;
for (bptr = hash [hashix]; bptr; bptr = bptr -> cdr) {
if (((struct option_cache *)(bptr -> car)) -> option -> code
== code)
break;
prev = bptr;
}
/* If we found one, wipe it out... */
if (bptr) {
if (prev)
prev -> cdr = bptr -> cdr;
else
1998-11-05 18:42:47 +00:00
hash [hashix] = bptr -> cdr;
option_cache_dereference
((struct option_cache **)(&bptr -> car),
"delete_option");
free_pair (bptr, "delete_option");
}
}
1998-11-05 18:42:47 +00:00
extern struct option_cache *free_option_caches; /* XXX */
int option_cache_dereference (ptr, name)
struct option_cache **ptr;
char *name;
{
if (!ptr || !*ptr) {
log_error ("Null pointer in option_cache_dereference: %s", name);
1998-11-05 18:42:47 +00:00
abort ();
}
(*ptr) -> refcnt--;
if (!(*ptr) -> refcnt) {
if ((*ptr) -> data.buffer)
data_string_forget (&(*ptr) -> data, name);
if ((*ptr) -> expression)
expression_dereference (&(*ptr) -> expression, name);
/* Put it back on the free list... */
(*ptr) -> expression = (struct expression *)free_option_caches;
free_option_caches = *ptr;
}
1998-11-05 18:42:47 +00:00
*ptr = (struct option_cache *)0;
return 1;
}