1/*
2 * options.c -- DHCP server option packet tools
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#include "debug.h"
11#include "dhcpd.h"
12#include "files.h"
13#include "options.h"
14#include "leases.h"
15
16
17/* supported options are easily added here */
18struct dhcp_option options[] = {
19	/* name[10]	flags					code */
20	{"subnet",	OPTION_IP | OPTION_REQ,			0x01},
21	{"timezone",	OPTION_S32,				0x02},
22	{"router",	OPTION_IP | OPTION_LIST | OPTION_REQ,	0x03},
23	{"timesvr",	OPTION_IP | OPTION_LIST,		0x04},
24	{"namesvr",	OPTION_IP | OPTION_LIST,		0x05},
25	{"dns",		OPTION_IP | OPTION_LIST | OPTION_REQ,	0x06},
26	{"logsvr",	OPTION_IP | OPTION_LIST,		0x07},
27	{"cookiesvr",	OPTION_IP | OPTION_LIST,		0x08},
28	{"lprsvr",	OPTION_IP | OPTION_LIST,		0x09},
29	{"hostname",	OPTION_STRING | OPTION_REQ,		0x0c},
30	{"bootsize",	OPTION_U16,				0x0d},
31	{"domain",	OPTION_STRING | OPTION_REQ,		0x0f},
32	{"swapsvr",	OPTION_IP,				0x10},
33	{"rootpath",	OPTION_STRING,				0x11},
34	{"ipttl",	OPTION_U8,				0x17},
35	{"mtu",		OPTION_U16,				0x1a},
36	{"broadcast",	OPTION_IP | OPTION_REQ,			0x1c},
37	{"routes",	OPTION_IP | OPTION_LIST | OPTION_REQ,	0x21},
38	{"ntpsrv",	OPTION_IP | OPTION_LIST,		0x2a},
39	{"wins",	OPTION_IP | OPTION_LIST | OPTION_REQ,	0x2c},
40	{"requestip",	OPTION_IP,				0x32},
41	{"lease",	OPTION_U32,				0x33},
42	{"dhcptype",	OPTION_U8,				0x35},
43	{"serverid",	OPTION_IP,				0x36},
44	{"message",	OPTION_STRING,				0x38},
45	{"max-size",	OPTION_U16,				0x39},
46	{"tftp",	OPTION_STRING,				0x42},
47	{"bootfile",	OPTION_STRING,				0x43},
48	{"msroutes",	OPTION_U8 | OPTION_LIST | OPTION_REQ,	0xF9},
49	{"",		0x00,				0x00}
50};
51
52/* Lengths of the different option types */
53int option_lengths[] = {
54	[OPTION_IP] =		4,
55	[OPTION_IP_PAIR] =	8,
56	[OPTION_BOOLEAN] =	1,
57	[OPTION_STRING] =	1,
58	[OPTION_U8] =		1,
59	[OPTION_U16] =		2,
60	[OPTION_S16] =		2,
61	[OPTION_U32] =		4,
62	[OPTION_S32] =		4
63};
64
65
66/* get an option with bounds checking (warning, not aligned). */
67unsigned char *get_option(struct dhcpMessage *packet, int code)
68{
69	int i, length;
70	unsigned char *optionptr;
71	int over = 0, done = 0, curr = OPTION_FIELD;
72
73	optionptr = packet->options;
74	i = 0;
75	length = 308;
76	while (!done) {
77		if (i >= length) {
78			LOG(LOG_WARNING, "bogus packet, option fields too long.");
79			return NULL;
80		}
81		if (optionptr[i + OPT_CODE] == code) {
82			if (i + 1 + optionptr[i + OPT_LEN] >= length) {
83				LOG(LOG_WARNING, "bogus packet, option fields too long.");
84				return NULL;
85			}
86			return optionptr + i + 2;
87		}
88		switch (optionptr[i + OPT_CODE]) {
89		case DHCP_PADDING:
90			i++;
91			break;
92		case DHCP_OPTION_OVER:
93			if (i + 1 + optionptr[i + OPT_LEN] >= length) {
94				LOG(LOG_WARNING, "bogus packet, option fields too long.");
95				return NULL;
96			}
97			over = optionptr[i + 3];
98			i += optionptr[OPT_LEN] + 2;
99			break;
100		case DHCP_END:
101			if (curr == OPTION_FIELD && over & FILE_FIELD) {
102				optionptr = packet->file;
103				i = 0;
104				length = 128;
105				curr = FILE_FIELD;
106			} else if (curr == FILE_FIELD && over & SNAME_FIELD) {
107				optionptr = packet->sname;
108				i = 0;
109				length = 64;
110				curr = SNAME_FIELD;
111			} else done = 1;
112			break;
113		default:
114			i += optionptr[OPT_LEN + i] + 2;
115		}
116	}
117	return NULL;
118}
119
120
121/* return the position of the 'end' option (no bounds checking) */
122int end_option(unsigned char *optionptr)
123{
124	int i = 0;
125
126	while (optionptr[i] != DHCP_END) {
127		if (optionptr[i] == DHCP_PADDING) i++;
128		else i += optionptr[i + OPT_LEN] + 2;
129	}
130	return i;
131}
132
133
134/* add an option string to the options (an option string contains an option code,
135 * length, then data) */
136int add_option_string(unsigned char *optionptr, unsigned char *string)
137{
138	int end = end_option(optionptr);
139
140	/* end position + string length + option code/length + end option */
141	if (end + string[OPT_LEN] + 2 + 1 >= 308) {
142		LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]);
143		return 0;
144	}
145	DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]);
146	memcpy(optionptr + end, string, string[OPT_LEN] + 2);
147	optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
148	return string[OPT_LEN] + 2;
149}
150
151
152/* add a one to four byte option to a packet */
153int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data)
154{
155	char length = 0;
156	int i;
157	unsigned char option[2 + 4];
158	unsigned char *u8;
159	u_int16_t *u16;
160	u_int32_t *u32;
161	u_int32_t aligned;
162	u8 = (unsigned char *) &aligned;
163	u16 = (u_int16_t *) &aligned;
164	u32 = &aligned;
165
166	for (i = 0; options[i].code; i++)
167		if (options[i].code == code) {
168			length = option_lengths[options[i].flags & TYPE_MASK];
169		}
170
171	if (!length) {
172		DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
173		return 0;
174	}
175
176	option[OPT_CODE] = code;
177	option[OPT_LEN] = length;
178
179	switch (length) {
180		case 1: *u8 =  data; break;
181		case 2: *u16 = data; break;
182		case 4: *u32 = data; break;
183	}
184	memcpy(option + 2, &aligned, length);
185	return add_option_string(optionptr, option);
186}
187
188
189/* find option 'code' in opt_list */
190struct option_set *find_option(struct option_set *opt_list, char code)
191{
192	while (opt_list && opt_list->data[OPT_CODE] < code)
193		opt_list = opt_list->next;
194
195	if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
196	else return NULL;
197}
198
199
200/* add an option to the opt_list */
201void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
202{
203	struct option_set *existing, *new, **curr;
204
205	/* add it to an existing option */
206	if ((existing = find_option(*opt_list, option->code))) {
207		DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
208		if (option->flags & OPTION_LIST) {
209			if (existing->data[OPT_LEN] + length <= 255) {
210				existing->data = realloc(existing->data,
211						existing->data[OPT_LEN] + length + 2);
212				memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
213				existing->data[OPT_LEN] += length;
214			} /* else, ignore the data, we could put this in a second option in the future */
215		} /* else, ignore the new data */
216	} else {
217		DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
218
219		/* make a new option */
220		new = malloc(sizeof(struct option_set));
221		new->data = malloc(length + 2);
222		new->data[OPT_CODE] = option->code;
223		new->data[OPT_LEN] = length;
224		memcpy(new->data + 2, buffer, length);
225
226		curr = opt_list;
227		while (*curr && (*curr)->data[OPT_CODE] < option->code)
228			curr = &(*curr)->next;
229
230		new->next = *curr;
231		*curr = new;
232	}
233}
234