1995-11-29 07:40:04 +00:00
|
|
|
/* options.c
|
|
|
|
|
|
|
|
DHCP options parsing and reassembly. */
|
|
|
|
|
|
|
|
/*
|
1998-03-16 06:19:46 +00:00
|
|
|
* 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[] =
|
1999-03-10 20:39:22 +00:00
|
|
|
"$Id: options.c,v 1.36 1999/03/10 20:39:22 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 *,
|
1998-06-25 03:02:50 +00:00
|
|
|
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")) {
|
1999-02-24 17:56:53 +00:00
|
|
|
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];
|
1996-02-29 18:32:06 +00:00
|
|
|
|
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) {
|
1999-02-24 17:56:53 +00:00
|
|
|
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
|
|
|
}
|
1998-02-06 01:18:33 +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)) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_error ("malformed agent information option.");
|
1998-11-05 18:42:47 +00:00
|
|
|
buffer_dereference (&bp,
|
|
|
|
"parse_option_buffer");
|
|
|
|
return 0;
|
1998-02-06 01:18:33 +00:00
|
|
|
}
|
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")) {
|
1999-02-24 17:56:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-02-06 01:18:33 +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");
|
1998-02-06 01:18:33 +00:00
|
|
|
if (!t)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("can't allocate space for option tag data.");
|
1998-02-06 01:18:33 +00:00
|
|
|
|
|
|
|
/* 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");
|
1998-02-06 01:18:33 +00:00
|
|
|
if (!a)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("can't allocate space for agent option structure.");
|
1998-02-06 01:18:33 +00:00
|
|
|
|
|
|
|
/* Find the tail of the list. */
|
1998-11-05 18:42:47 +00:00
|
|
|
for (tail = &packet -> options.agent_options;
|
|
|
|
*tail; tail = &((*tail) -> next))
|
1998-02-06 01:18:33 +00:00
|
|
|
;
|
|
|
|
*tail = a;
|
|
|
|
a -> next = (struct agent_options *)0;
|
|
|
|
a -> first = oth;
|
|
|
|
a -> length = len;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1996-02-29 18:32:06 +00:00
|
|
|
/* 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. */
|
|
|
|
|
1998-06-25 03:02:50 +00:00
|
|
|
int cons_options (inpacket, outpacket, mms, options,
|
1999-03-10 20:39:22 +00:00
|
|
|
agent_options, overload, terminate, bootpp, prl)
|
1996-02-29 18:32:06 +00:00
|
|
|
struct packet *inpacket;
|
1997-02-18 14:28:54 +00:00
|
|
|
struct dhcp_packet *outpacket;
|
1998-02-06 01:18:33 +00:00
|
|
|
int mms;
|
1998-06-25 03:02:50 +00:00
|
|
|
struct option_state *options;
|
1998-11-06 00:12:40 +00:00
|
|
|
struct agent_options *agent_options;
|
1996-02-29 18:32:06 +00:00
|
|
|
int overload; /* Overload flags that may be set. */
|
1996-09-11 05:52:18 +00:00
|
|
|
int terminate;
|
1997-09-16 18:15:25 +00:00
|
|
|
int bootpp;
|
1999-03-10 20:39:22 +00:00
|
|
|
struct data_string *prl;
|
1996-02-29 18:32:06 +00:00
|
|
|
{
|
1998-11-05 18:42:47 +00:00
|
|
|
#define PRIORITY_COUNT 300
|
|
|
|
int priority_list [PRIORITY_COUNT];
|
1996-02-29 18:32:06 +00:00
|
|
|
int priority_len;
|
|
|
|
unsigned char buffer [4096]; /* Really big buffer... */
|
|
|
|
int main_buffer_size;
|
1998-02-06 01:18:33 +00:00
|
|
|
int mainbufix, bufix, agentix;
|
1996-02-29 18:32:06 +00:00
|
|
|
int option_size;
|
1997-02-18 14:28:54 +00:00
|
|
|
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);
|
1996-02-29 18:32:06 +00:00
|
|
|
|
1998-02-06 01:18:33 +00:00
|
|
|
/* 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");
|
1998-02-06 01:18:33 +00:00
|
|
|
}
|
|
|
|
|
1996-02-29 18:32:06 +00:00
|
|
|
/* If the client has provided a maximum DHCP message size,
|
1997-09-16 18:15:25 +00:00
|
|
|
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. */
|
1998-02-06 01:18:33 +00:00
|
|
|
|
|
|
|
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;
|
1998-03-17 06:12:17 +00:00
|
|
|
} 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
|
1996-02-29 18:32:06 +00:00
|
|
|
main_buffer_size = 576 - DHCP_FIXED_LEN;
|
|
|
|
|
1998-02-06 01:18:33 +00:00
|
|
|
/* Set a hard limit at the size of the output buffer. */
|
|
|
|
if (main_buffer_size > sizeof buffer)
|
|
|
|
main_buffer_size = sizeof buffer;
|
|
|
|
|
1996-02-29 18:32:06 +00:00
|
|
|
/* 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;
|
1996-02-29 18:32:06 +00:00
|
|
|
|
1999-03-10 20:39:22 +00:00
|
|
|
if (prl && prl -> len > 0) {
|
|
|
|
data_string_truncate (prl, (PRIORITY_COUNT - priority_len));
|
1996-02-29 18:32:06 +00:00
|
|
|
|
1999-03-10 20:39:22 +00:00
|
|
|
for (i = 0; i < prl -> len; i++)
|
|
|
|
priority_list [priority_len++] = prl -> data [i];
|
1996-02-29 18:32:06 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
1996-02-29 18:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the options into the big buffer... */
|
1996-03-01 00:44:38 +00:00
|
|
|
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,
|
1998-06-25 03:02:50 +00:00
|
|
|
priority_list, priority_len,
|
1996-03-01 00:44:38 +00:00
|
|
|
main_buffer_size,
|
|
|
|
(main_buffer_size +
|
1996-09-11 05:52:18 +00:00
|
|
|
((overload & 1) ? DHCP_FILE_LEN : 0)),
|
|
|
|
terminate);
|
1996-02-29 18:32:06 +00:00
|
|
|
|
1996-03-01 00:44:38 +00:00
|
|
|
/* Put the cookie up front... */
|
1997-02-18 14:28:54 +00:00
|
|
|
memcpy (outpacket -> options, DHCP_OPTIONS_COOKIE, 4);
|
1996-03-01 00:44:38 +00:00
|
|
|
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) {
|
1997-02-18 14:28:54 +00:00
|
|
|
memcpy (&outpacket -> options [mainbufix],
|
1996-03-01 00:44:38 +00:00
|
|
|
buffer, option_size);
|
|
|
|
mainbufix += option_size;
|
1998-02-06 01:18:33 +00:00
|
|
|
if (mainbufix < main_buffer_size) {
|
|
|
|
agentix = mainbufix;
|
1998-11-05 18:42:47 +00:00
|
|
|
outpacket -> options [mainbufix++] = DHO_END;
|
1998-02-06 01:18:33 +00:00
|
|
|
} else
|
|
|
|
agentix = mainbufix;
|
1997-02-18 14:28:54 +00:00
|
|
|
length = DHCP_FIXED_NON_UDP + mainbufix;
|
1996-03-01 00:44:38 +00:00
|
|
|
} else {
|
1998-11-05 18:42:47 +00:00
|
|
|
outpacket -> options [mainbufix++] = DHO_DHCP_OPTION_OVERLOAD;
|
1997-02-18 14:28:54 +00:00
|
|
|
outpacket -> options [mainbufix++] = 1;
|
1996-03-01 00:44:38 +00:00
|
|
|
if (option_size > main_buffer_size - mainbufix + DHCP_FILE_LEN)
|
1997-02-18 14:28:54 +00:00
|
|
|
outpacket -> options [mainbufix++] = 3;
|
1996-03-01 00:44:38 +00:00
|
|
|
else
|
1997-02-18 14:28:54 +00:00
|
|
|
outpacket -> options [mainbufix++] = 1;
|
1996-03-01 00:44:38 +00:00
|
|
|
|
1997-02-18 14:28:54 +00:00
|
|
|
memcpy (&outpacket -> options [mainbufix],
|
1996-03-01 00:44:38 +00:00
|
|
|
buffer, main_buffer_size - mainbufix);
|
1998-02-06 01:18:33 +00:00
|
|
|
length = DHCP_FIXED_NON_UDP + main_buffer_size;
|
|
|
|
agentix = main_buffer_size;
|
|
|
|
|
1996-03-01 00:44:38 +00:00
|
|
|
bufix = main_buffer_size - mainbufix;
|
|
|
|
if (overload & 1) {
|
|
|
|
if (option_size - bufix <= DHCP_FILE_LEN) {
|
1997-02-18 14:28:54 +00:00
|
|
|
memcpy (outpacket -> file,
|
1996-03-01 00:44:38 +00:00
|
|
|
&buffer [bufix], option_size - bufix);
|
|
|
|
mainbufix = option_size - bufix;
|
|
|
|
if (mainbufix < DHCP_FILE_LEN)
|
1997-02-18 14:28:54 +00:00
|
|
|
outpacket -> file [mainbufix++]
|
1996-03-01 00:44:38 +00:00
|
|
|
= DHO_END;
|
|
|
|
while (mainbufix < DHCP_FILE_LEN)
|
1997-02-18 14:28:54 +00:00
|
|
|
outpacket -> file [mainbufix++]
|
1996-03-01 00:44:38 +00:00
|
|
|
= DHO_PAD;
|
|
|
|
} else {
|
1997-02-18 14:28:54 +00:00
|
|
|
memcpy (outpacket -> file,
|
1996-03-01 00:44:38 +00:00
|
|
|
&buffer [bufix], DHCP_FILE_LEN);
|
|
|
|
bufix += DHCP_FILE_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((overload & 2) && option_size < bufix) {
|
1997-02-18 14:28:54 +00:00
|
|
|
memcpy (outpacket -> sname,
|
1996-03-01 00:44:38 +00:00
|
|
|
&buffer [bufix], option_size - bufix);
|
|
|
|
|
|
|
|
mainbufix = option_size - bufix;
|
|
|
|
if (mainbufix < DHCP_SNAME_LEN)
|
1997-02-18 14:28:54 +00:00
|
|
|
outpacket -> file [mainbufix++]
|
1996-03-01 00:44:38 +00:00
|
|
|
= DHO_END;
|
|
|
|
while (mainbufix < DHCP_SNAME_LEN)
|
1997-02-18 14:28:54 +00:00
|
|
|
outpacket -> file [mainbufix++]
|
1996-03-01 00:44:38 +00:00
|
|
|
= DHO_PAD;
|
|
|
|
}
|
|
|
|
}
|
1998-02-06 01:18:33 +00:00
|
|
|
|
|
|
|
/* 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. */
|
1998-06-25 03:02:50 +00:00
|
|
|
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;
|
|
|
|
}
|
1998-02-06 01:18:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
1997-02-18 14:28:54 +00:00
|
|
|
return length;
|
1996-02-29 18:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Store all the requested options into the requested buffer. */
|
|
|
|
|
1996-03-01 00:44:38 +00:00
|
|
|
int store_options (buffer, buflen, options, priority_list, priority_len,
|
1996-09-11 05:52:18 +00:00
|
|
|
first_cutoff, second_cutoff, terminate)
|
1996-02-29 18:32:06 +00:00
|
|
|
unsigned char *buffer;
|
|
|
|
int buflen;
|
1998-11-05 18:42:47 +00:00
|
|
|
struct option_state *options;
|
|
|
|
int *priority_list;
|
1996-02-29 18:32:06 +00:00
|
|
|
int priority_len;
|
1996-03-01 00:44:38 +00:00
|
|
|
int first_cutoff, second_cutoff;
|
1996-09-11 05:52:18 +00:00
|
|
|
int terminate;
|
1996-02-29 18:32:06 +00:00
|
|
|
{
|
|
|
|
int bufix = 0;
|
|
|
|
int i;
|
|
|
|
int ix;
|
1996-09-11 05:52:18 +00:00
|
|
|
int tto;
|
1998-06-25 03:02:50 +00:00
|
|
|
struct data_string od;
|
1998-11-05 18:42:47 +00:00
|
|
|
struct option_cache *oc;
|
1996-02-29 18:32:06 +00:00
|
|
|
|
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--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-02-29 18:32:06 +00:00
|
|
|
|
|
|
|
/* 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];
|
1996-03-01 00:44:38 +00:00
|
|
|
int optstart;
|
1996-02-29 18:32:06 +00:00
|
|
|
|
|
|
|
/* 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))) {
|
1996-02-29 18:32:06 +00:00
|
|
|
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
|
|
|
|
1996-02-29 18:32:06 +00:00
|
|
|
/* We should now have a constant length for the option. */
|
1998-06-25 03:02:50 +00:00
|
|
|
length = od.len;
|
1996-02-29 18:32:06 +00:00
|
|
|
|
1996-09-11 05:52:18 +00:00
|
|
|
/* Do we add a NUL? */
|
|
|
|
if (terminate && dhcp_options [code].format [0] == 't') {
|
|
|
|
length++;
|
|
|
|
tto = 1;
|
|
|
|
} else {
|
|
|
|
tto = 0;
|
|
|
|
}
|
|
|
|
|
1996-03-01 00:44:38 +00:00
|
|
|
/* Try to store the option. */
|
1996-02-29 18:32:06 +00:00
|
|
|
|
|
|
|
/* If the option's length is more than 255, we must store it
|
1996-03-01 00:44:38 +00:00
|
|
|
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. */
|
1996-02-29 18:32:06 +00:00
|
|
|
|
|
|
|
ix = 0;
|
|
|
|
|
1996-03-01 00:44:38 +00:00
|
|
|
optstart = bufix;
|
1996-02-29 18:32:06 +00:00
|
|
|
while (length) {
|
|
|
|
unsigned char incr = length > 255 ? 255 : length;
|
1996-03-01 00:44:38 +00:00
|
|
|
|
|
|
|
/* 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! */
|
1996-02-29 18:32:06 +00:00
|
|
|
buffer [bufix] = code;
|
|
|
|
buffer [bufix + 1] = incr;
|
1996-09-11 05:52:18 +00:00
|
|
|
if (tto && incr == length) {
|
|
|
|
memcpy (buffer + bufix + 2,
|
1998-06-25 03:02:50 +00:00
|
|
|
od.data + ix, incr - 1);
|
1996-09-11 05:52:18 +00:00
|
|
|
buffer [bufix + 2 + incr - 1] = 0;
|
|
|
|
} else {
|
|
|
|
memcpy (buffer + bufix + 2,
|
1998-06-25 03:02:50 +00:00
|
|
|
od.data + ix, incr);
|
1996-09-11 05:52:18 +00:00
|
|
|
}
|
1996-02-29 18:32:06 +00:00
|
|
|
length -= incr;
|
|
|
|
ix += incr;
|
|
|
|
bufix += 2 + incr;
|
|
|
|
}
|
1998-11-05 18:42:47 +00:00
|
|
|
data_string_forget (&od, "store_options");
|
1996-02-29 18:32:06 +00:00
|
|
|
}
|
|
|
|
return bufix;
|
1995-11-29 07:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Format the specified option so that a human can easily read it. */
|
|
|
|
|
1997-06-02 22:32:05 +00:00
|
|
|
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;
|
1997-02-22 08:32:05 +00:00
|
|
|
int emit_commas;
|
1997-06-02 22:32:05 +00:00
|
|
|
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;
|
1997-03-06 06:55:06 +00:00
|
|
|
char comma;
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1997-06-02 22:32:05 +00:00
|
|
|
/* Code should be between 0 and 255. */
|
|
|
|
if (code > 255)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("pretty_print_option: bad code %d\n", code);
|
1997-06-02 22:32:05 +00:00
|
|
|
|
1997-03-06 06:55:06 +00:00
|
|
|
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) {
|
1999-02-24 17:56:53 +00:00
|
|
|
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;
|
1997-03-06 06:55:06 +00:00
|
|
|
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:
|
1999-02-24 17:56:53 +00:00
|
|
|
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) {
|
1999-02-24 17:56:53 +00:00
|
|
|
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)
|
1999-02-24 17:56:53 +00:00
|
|
|
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)
|
1999-02-24 17:56:53 +00:00
|
|
|
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':
|
1997-06-02 22:32:05 +00:00
|
|
|
if (emit_quotes)
|
|
|
|
*op++ = '"';
|
1998-03-16 06:19:46 +00:00
|
|
|
strcpy (op, (char *)dp);
|
|
|
|
op += strlen ((char *)dp);
|
1997-06-02 22:32:05 +00:00
|
|
|
if (emit_quotes)
|
|
|
|
*op++ = '"';
|
1997-02-22 08:32:05 +00:00
|
|
|
*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;
|
1997-03-06 06:55:06 +00:00
|
|
|
case 'x':
|
|
|
|
sprintf (op, "%x", *dp++);
|
|
|
|
break;
|
1995-11-29 07:40:04 +00:00
|
|
|
case 'f':
|
|
|
|
strcpy (op, *dp++ ? "true" : "false");
|
|
|
|
break;
|
|
|
|
default:
|
1999-02-24 17:56:53 +00:00
|
|
|
log_error ("Unexpected format code %c", fmtbuf [j]);
|
1995-11-29 07:40:04 +00:00
|
|
|
}
|
|
|
|
op += strlen (op);
|
1997-03-06 06:55:06 +00:00
|
|
|
if (j + 1 < numelem && comma != ':')
|
1997-02-19 10:52:14 +00:00
|
|
|
*op++ = ' ';
|
|
|
|
}
|
|
|
|
if (i + 1 < numhunk) {
|
1997-03-06 06:55:06 +00:00
|
|
|
*op++ = comma;
|
1995-11-29 07:40:04 +00:00
|
|
|
}
|
1997-02-19 10:52:14 +00:00
|
|
|
|
1995-11-29 07:40:04 +00:00
|
|
|
}
|
|
|
|
return optbuf;
|
|
|
|
}
|
1997-03-06 06:55:06 +00:00
|
|
|
|
1998-06-25 03:02:50 +00:00
|
|
|
void do_packet (interface, packet, len, from_port, from, hfrom)
|
1997-03-06 06:55:06 +00:00
|
|
|
struct interface_info *interface;
|
1998-06-25 03:02:50 +00:00
|
|
|
struct dhcp_packet *packet;
|
1997-03-06 06:55:06 +00:00
|
|
|
int len;
|
1998-03-16 06:19:46 +00:00
|
|
|
unsigned int from_port;
|
1997-03-06 06:55:06 +00:00
|
|
|
struct iaddr from;
|
|
|
|
struct hardware *hfrom;
|
|
|
|
{
|
|
|
|
struct packet tp;
|
1998-11-05 18:42:47 +00:00
|
|
|
int i;
|
|
|
|
struct option_cache *op;
|
1997-03-06 06:55:06 +00:00
|
|
|
|
|
|
|
memset (&tp, 0, sizeof tp);
|
1998-06-25 03:02:50 +00:00
|
|
|
tp.raw = packet;
|
1997-03-06 06:55:06 +00:00
|
|
|
tp.packet_length = len;
|
|
|
|
tp.client_port = from_port;
|
|
|
|
tp.client_addr = from;
|
|
|
|
tp.interface = interface;
|
|
|
|
tp.haddr = hfrom;
|
|
|
|
|
1998-06-25 03:02:50 +00:00
|
|
|
if (packet -> hlen > sizeof packet -> chaddr) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_info ("Discarding packet with bogus hardware address length.");
|
1998-06-25 03:02:50 +00:00
|
|
|
return;
|
|
|
|
}
|
1998-11-05 18:42:47 +00:00
|
|
|
if (!parse_options (&tp)) {
|
|
|
|
option_state_dereference (&tp.options);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-03-06 06:55:06 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
1997-03-06 06:55:06 +00:00
|
|
|
if (tp.packet_type)
|
|
|
|
dhcp (&tp);
|
1997-05-09 08:07:09 +00:00
|
|
|
else
|
1997-03-06 06:55:06 +00:00
|
|
|
bootp (&tp);
|
1998-02-06 01:18:33 +00:00
|
|
|
|
1998-11-05 18:42:47 +00:00
|
|
|
option_state_dereference (&tp.options);
|
1997-03-06 06:55:06 +00:00
|
|
|
}
|
|
|
|
|
1998-11-05 18:42:47 +00:00
|
|
|
int dhcp_option_lookup (result, options, code)
|
|
|
|
struct data_string *result;
|
|
|
|
struct option_state *options;
|
1998-04-19 23:19:14 +00:00
|
|
|
int code;
|
|
|
|
{
|
1998-11-05 18:42:47 +00:00
|
|
|
struct option_cache *oc;
|
1998-04-19 23:19:14 +00:00
|
|
|
|
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-04-19 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
1998-11-05 18:42:47 +00:00
|
|
|
int agent_suboption_lookup (result, options, code)
|
|
|
|
struct data_string *result;
|
|
|
|
struct option_state *options;
|
1998-04-19 23:19:14 +00:00
|
|
|
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)
|
|
|
|
;
|
1998-04-19 23:19:14 +00:00
|
|
|
if (ao) {
|
1998-11-05 18:42:47 +00:00
|
|
|
for (t = ao -> first; t; t = t -> next) {
|
1998-04-19 23:19:14 +00:00
|
|
|
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-04-19 23:19:14 +00:00
|
|
|
}
|
1998-11-05 18:42:47 +00:00
|
|
|
}
|
1998-04-19 23:19:14 +00:00
|
|
|
}
|
1998-11-05 18:42:47 +00:00
|
|
|
return 0;
|
1998-04-19 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
1998-11-05 18:42:47 +00:00
|
|
|
int server_option_lookup (result, options, code)
|
|
|
|
struct data_string *result;
|
|
|
|
struct option_state *options;
|
1998-06-25 03:02:50 +00:00
|
|
|
int code;
|
|
|
|
{
|
1998-11-05 18:42:47 +00:00
|
|
|
return 0;
|
1998-06-25 03:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
1998-06-25 03:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-25 03:02:50 +00:00
|
|
|
}
|
|
|
|
|
1998-11-05 18:42:47 +00:00
|
|
|
static void do_option_set (hash, option, op)
|
|
|
|
pair *hash;
|
1998-06-25 03:02:50 +00:00
|
|
|
struct option_cache *option;
|
|
|
|
enum statement_op op;
|
|
|
|
{
|
1998-11-05 18:42:47 +00:00
|
|
|
struct option_cache *oc, *noc;
|
1998-06-25 03:02:50 +00:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case if_statement:
|
|
|
|
case add_statement:
|
|
|
|
case eval_statement:
|
|
|
|
case break_statement:
|
|
|
|
default:
|
1999-02-24 17:56:53 +00:00
|
|
|
log_error ("bogus statement type in do_option_set.");
|
1998-06-25 03:02:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case default_option_statement:
|
1998-11-05 18:42:47 +00:00
|
|
|
oc = lookup_option (hash, option -> option -> code);
|
|
|
|
if (oc)
|
1998-06-25 03:02:50 +00:00
|
|
|
break;
|
1998-11-05 18:42:47 +00:00
|
|
|
save_option (hash, option);
|
1998-06-25 03:02:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case supersede_option_statement:
|
1998-11-05 18:42:47 +00:00
|
|
|
/* Install the option, replacing any existing version. */
|
|
|
|
save_option (hash, option);
|
1998-06-25 03:02:50 +00:00
|
|
|
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);
|
1998-06-25 03:02:50 +00:00
|
|
|
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")) {
|
1999-02-24 17:56:53 +00:00
|
|
|
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");
|
1998-06-25 03:02:50 +00:00
|
|
|
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) {
|
1999-02-24 17:56:53 +00:00
|
|
|
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;
|
1998-06-25 03:02:50 +00:00
|
|
|
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-06-25 03:02:50 +00:00
|
|
|
|
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) {
|
1999-02-24 17:56:53 +00:00
|
|
|
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-06-25 03:02:50 +00:00
|
|
|
}
|
1998-11-05 18:42:47 +00:00
|
|
|
*ptr = (struct option_cache *)0;
|
|
|
|
return 1;
|
|
|
|
|
1998-06-25 03:02:50 +00:00
|
|
|
}
|