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	{"sroute",	OPTION_STRING  | OPTION_LIST | OPTION_REQ,	0x21}, /* static-routes, foxconn added by EricHuang, 03/12/2008 */
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	{"tftp",	OPTION_STRING,				0x42},
46	{"bootfile",	OPTION_STRING,				0x43},
47	{"rfc3442", OPTION_STRING  | OPTION_LIST | OPTION_REQ,	0x79}, /* classless-static-routes, foxconn added by EricHuang, 12/20/2007 */
48	{"rfc3442", OPTION_STRING  | OPTION_LIST | OPTION_REQ,	0xf9}, /* foxconn added, 03/08/2010, @option249 */
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	/* foxconn modified start, zacker, 09/18/2009, @big_size_pkt */
76	//length = 308;
77	/* Foxconn modified start pling 05/11/2012 */
78	/* Fix a DHCP server crash issue:
79	 * DHCP server packet was not modified to receive large size packet.
80	 * So we only check options for LEN_OPTIONS long.
81	 */
82	if (packet->op == BOOTREQUEST)
83		length = LEN_OPTIONS;
84	else
85		length = LEN_OPTIONS + LEN_PADDING;
86	/* Foxconn modified end pling 05/11/2012 */
87	/* foxconn modified end, zacker, 09/18/2009, @big_size_pkt */
88	while (!done) {
89		if (i >= length) {
90			LOG(LOG_WARNING, "bogus packet, option fields too long.");
91			return NULL;
92		}
93		if (optionptr[i + OPT_CODE] == code) {
94			if (i + 1 + optionptr[i + OPT_LEN] >= length) {
95				LOG(LOG_WARNING, "bogus packet, option fields too long.");
96				return NULL;
97			}
98			return optionptr + i + 2;
99		}
100		switch (optionptr[i + OPT_CODE]) {
101		case DHCP_PADDING:
102			i++;
103			break;
104		case DHCP_OPTION_OVER:
105			if (i + 1 + optionptr[i + OPT_LEN] >= length) {
106				LOG(LOG_WARNING, "bogus packet, option fields too long.");
107				return NULL;
108			}
109			over = optionptr[i + 3];
110			i += optionptr[OPT_LEN] + 2;
111			break;
112		case DHCP_END:
113			if (curr == OPTION_FIELD && over & FILE_FIELD) {
114				optionptr = packet->file;
115				i = 0;
116				length = 128;
117				curr = FILE_FIELD;
118			} else if (curr == FILE_FIELD && over & SNAME_FIELD) {
119				optionptr = packet->sname;
120				i = 0;
121				length = 64;
122				curr = SNAME_FIELD;
123			} else done = 1;
124			break;
125		default:
126			i += optionptr[OPT_LEN + i] + 2;
127		}
128	}
129	return NULL;
130}
131
132
133/* return the position of the 'end' option (no bounds checking) */
134int end_option(unsigned char *optionptr)
135{
136	int i = 0;
137
138	while (optionptr[i] != DHCP_END) {
139		if (optionptr[i] == DHCP_PADDING) i++;
140		else i += optionptr[i + OPT_LEN] + 2;
141	}
142	return i;
143}
144
145
146/* add an option string to the options (an option string contains an option code,
147 * length, then data) */
148int add_option_string(unsigned char *optionptr, unsigned char *string)
149{
150	int end = end_option(optionptr);
151
152	/* end position + string length + option code/length + end option */
153	if (end + string[OPT_LEN] + 2 + 1 >= 308) {
154		LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]);
155		return 0;
156	}
157	DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]);
158	memcpy(optionptr + end, string, string[OPT_LEN] + 2);
159	optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
160	return string[OPT_LEN] + 2;
161}
162
163
164/* add a one to four byte option to a packet */
165int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data)
166{
167	char length = 0;
168	int i;
169	unsigned char option[2 + 4];
170	unsigned char *u8;
171	u_int16_t *u16;
172	u_int32_t *u32;
173	u_int32_t aligned;
174	u8 = (unsigned char *) &aligned;
175	u16 = (u_int16_t *) &aligned;
176	u32 = &aligned;
177
178	for (i = 0; options[i].code; i++)
179		if (options[i].code == code) {
180			length = option_lengths[options[i].flags & TYPE_MASK];
181		}
182
183	if (!length) {
184		DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
185		return 0;
186	}
187
188	option[OPT_CODE] = code;
189	option[OPT_LEN] = length;
190
191	switch (length) {
192		case 1: *u8 =  data; break;
193		case 2: *u16 = data; break;
194		case 4: *u32 = data; break;
195	}
196	memcpy(option + 2, &aligned, length);
197	return add_option_string(optionptr, option);
198}
199
200
201/* find option 'code' in opt_list */
202struct option_set *find_option(struct option_set *opt_list, char code)
203{
204	while (opt_list && opt_list->data[OPT_CODE] < code)
205		opt_list = opt_list->next;
206
207	if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
208	else return NULL;
209}
210
211
212/* add an option to the opt_list */
213void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
214{
215	struct option_set *existing, *new, **curr;
216
217	/* add it to an existing option */
218	if ((existing = find_option(*opt_list, option->code))) {
219		DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
220		if (option->flags & OPTION_LIST) {
221			if (existing->data[OPT_LEN] + length <= 255) {
222				existing->data = realloc(existing->data,
223						existing->data[OPT_LEN] + length + 2);
224				memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
225				existing->data[OPT_LEN] += length;
226			} /* else, ignore the data, we could put this in a second option in the future */
227		} /* else, ignore the new data */
228	} else {
229		DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
230
231		/* make a new option */
232		new = malloc(sizeof(struct option_set));
233		new->data = malloc(length + 2);
234		new->data[OPT_CODE] = option->code;
235		new->data[OPT_LEN] = length;
236		memcpy(new->data + 2, buffer, length);
237
238		curr = opt_list;
239		while (*curr && (*curr)->data[OPT_CODE] < option->code)
240			curr = &(*curr)->next;
241
242		new->next = *curr;
243		*curr = new;
244	}
245}
246