blob: d75bc5aff60a0fc369385ea38aaab8b54bfb1da9 [file] [log] [blame]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001/*
2 * options.c -- DHCP server option packet tools
Russ Dill61fb4892002-10-14 21:41:28 +00003 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
4 */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005
Russ Dill61fb4892002-10-14 21:41:28 +00006#include <stdlib.h>
7#include <string.h>
8
Russ Dill61fb4892002-10-14 21:41:28 +00009#include "dhcpd.h"
10#include "files.h"
11#include "options.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000012#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000013
14
15/* supported options are easily added here */
Russ Dill4a9e34c2003-12-15 22:09:36 +000016struct dhcp_option dhcp_options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +000017 /* name[10] flags code */
Russ Dill1eb7a172002-12-11 21:12:45 +000018 {"subnet", OPTION_IP | OPTION_REQ, 0x01},
Russ Dill61fb4892002-10-14 21:41:28 +000019 {"timezone", OPTION_S32, 0x02},
20 {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
21 {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
22 {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
23 {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
24 {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
25 {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
26 {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
27 {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
28 {"bootsize", OPTION_U16, 0x0d},
29 {"domain", OPTION_STRING | OPTION_REQ, 0x0f},
30 {"swapsvr", OPTION_IP, 0x10},
31 {"rootpath", OPTION_STRING, 0x11},
32 {"ipttl", OPTION_U8, 0x17},
33 {"mtu", OPTION_U16, 0x1a},
34 {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
35 {"ntpsrv", OPTION_IP | OPTION_LIST, 0x2a},
36 {"wins", OPTION_IP | OPTION_LIST, 0x2c},
37 {"requestip", OPTION_IP, 0x32},
38 {"lease", OPTION_U32, 0x33},
39 {"dhcptype", OPTION_U8, 0x35},
40 {"serverid", OPTION_IP, 0x36},
41 {"message", OPTION_STRING, 0x38},
42 {"tftp", OPTION_STRING, 0x42},
43 {"bootfile", OPTION_STRING, 0x43},
44 {"", 0x00, 0x00}
45};
46
47/* Lengths of the different option types */
48int option_lengths[] = {
49 [OPTION_IP] = 4,
50 [OPTION_IP_PAIR] = 8,
51 [OPTION_BOOLEAN] = 1,
52 [OPTION_STRING] = 1,
53 [OPTION_U8] = 1,
54 [OPTION_U16] = 2,
55 [OPTION_S16] = 2,
56 [OPTION_U32] = 4,
57 [OPTION_S32] = 4
58};
59
60
61/* get an option with bounds checking (warning, not aligned). */
Eric Andersenad953732004-01-30 23:45:53 +000062uint8_t *get_option(struct dhcpMessage *packet, int code)
Russ Dill61fb4892002-10-14 21:41:28 +000063{
64 int i, length;
Eric Andersenad953732004-01-30 23:45:53 +000065 uint8_t *optionptr;
Russ Dill61fb4892002-10-14 21:41:28 +000066 int over = 0, done = 0, curr = OPTION_FIELD;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000067
Russ Dill61fb4892002-10-14 21:41:28 +000068 optionptr = packet->options;
69 i = 0;
70 length = 308;
71 while (!done) {
72 if (i >= length) {
73 LOG(LOG_WARNING, "bogus packet, option fields too long.");
74 return NULL;
75 }
76 if (optionptr[i + OPT_CODE] == code) {
77 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
78 LOG(LOG_WARNING, "bogus packet, option fields too long.");
79 return NULL;
80 }
81 return optionptr + i + 2;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000082 }
Russ Dill61fb4892002-10-14 21:41:28 +000083 switch (optionptr[i + OPT_CODE]) {
84 case DHCP_PADDING:
85 i++;
86 break;
87 case DHCP_OPTION_OVER:
88 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
89 LOG(LOG_WARNING, "bogus packet, option fields too long.");
90 return NULL;
91 }
92 over = optionptr[i + 3];
93 i += optionptr[OPT_LEN] + 2;
94 break;
95 case DHCP_END:
96 if (curr == OPTION_FIELD && over & FILE_FIELD) {
97 optionptr = packet->file;
98 i = 0;
99 length = 128;
100 curr = FILE_FIELD;
101 } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
102 optionptr = packet->sname;
103 i = 0;
104 length = 64;
105 curr = SNAME_FIELD;
106 } else done = 1;
107 break;
108 default:
109 i += optionptr[OPT_LEN + i] + 2;
110 }
111 }
112 return NULL;
113}
114
115
116/* return the position of the 'end' option (no bounds checking) */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000117int end_option(uint8_t *optionptr)
Russ Dill61fb4892002-10-14 21:41:28 +0000118{
119 int i = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000120
Russ Dill61fb4892002-10-14 21:41:28 +0000121 while (optionptr[i] != DHCP_END) {
122 if (optionptr[i] == DHCP_PADDING) i++;
123 else i += optionptr[i + OPT_LEN] + 2;
124 }
125 return i;
126}
127
128
129/* add an option string to the options (an option string contains an option code,
130 * length, then data) */
Eric Andersenad953732004-01-30 23:45:53 +0000131int add_option_string(uint8_t *optionptr, uint8_t *string)
Russ Dill61fb4892002-10-14 21:41:28 +0000132{
133 int end = end_option(optionptr);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000134
Russ Dill61fb4892002-10-14 21:41:28 +0000135 /* end position + string length + option code/length + end option */
136 if (end + string[OPT_LEN] + 2 + 1 >= 308) {
137 LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]);
138 return 0;
139 }
140 DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]);
141 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
142 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
143 return string[OPT_LEN] + 2;
144}
145
146
147/* add a one to four byte option to a packet */
Eric Andersenad953732004-01-30 23:45:53 +0000148int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
Russ Dill61fb4892002-10-14 21:41:28 +0000149{
150 char length = 0;
151 int i;
Eric Andersenad953732004-01-30 23:45:53 +0000152 uint8_t option[2 + 4];
153 uint8_t *u8;
154 uint16_t *u16;
155 uint32_t *u32;
156 uint32_t aligned;
157 u8 = (uint8_t *) &aligned;
158 u16 = (uint16_t *) &aligned;
Russ Dill61fb4892002-10-14 21:41:28 +0000159 u32 = &aligned;
160
Russ Dill4a9e34c2003-12-15 22:09:36 +0000161 for (i = 0; dhcp_options[i].code; i++)
162 if (dhcp_options[i].code == code) {
163 length = option_lengths[dhcp_options[i].flags & TYPE_MASK];
Russ Dill61fb4892002-10-14 21:41:28 +0000164 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000165
Russ Dill61fb4892002-10-14 21:41:28 +0000166 if (!length) {
167 DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
168 return 0;
169 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000170
Russ Dill61fb4892002-10-14 21:41:28 +0000171 option[OPT_CODE] = code;
172 option[OPT_LEN] = length;
173
174 switch (length) {
175 case 1: *u8 = data; break;
176 case 2: *u16 = data; break;
177 case 4: *u32 = data; break;
178 }
179 memcpy(option + 2, &aligned, length);
180 return add_option_string(optionptr, option);
181}
182
183
184/* find option 'code' in opt_list */
185struct option_set *find_option(struct option_set *opt_list, char code)
186{
187 while (opt_list && opt_list->data[OPT_CODE] < code)
188 opt_list = opt_list->next;
189
190 if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
191 else return NULL;
192}
193
194
195/* add an option to the opt_list */
196void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
197{
198 struct option_set *existing, *new, **curr;
199
200 /* add it to an existing option */
201 if ((existing = find_option(*opt_list, option->code))) {
202 DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
203 if (option->flags & OPTION_LIST) {
204 if (existing->data[OPT_LEN] + length <= 255) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000205 existing->data = realloc(existing->data,
Russ Dill61fb4892002-10-14 21:41:28 +0000206 existing->data[OPT_LEN] + length + 2);
207 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
208 existing->data[OPT_LEN] += length;
209 } /* else, ignore the data, we could put this in a second option in the future */
210 } /* else, ignore the new data */
211 } else {
212 DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000213
Russ Dill61fb4892002-10-14 21:41:28 +0000214 /* make a new option */
Russ Dill9f4395c2002-12-11 21:40:46 +0000215 new = xmalloc(sizeof(struct option_set));
216 new->data = xmalloc(length + 2);
Russ Dill61fb4892002-10-14 21:41:28 +0000217 new->data[OPT_CODE] = option->code;
218 new->data[OPT_LEN] = length;
219 memcpy(new->data + 2, buffer, length);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000220
Russ Dill61fb4892002-10-14 21:41:28 +0000221 curr = opt_list;
222 while (*curr && (*curr)->data[OPT_CODE] < option->code)
223 curr = &(*curr)->next;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000224
Russ Dill61fb4892002-10-14 21:41:28 +0000225 new->next = *curr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000226 *curr = new;
Russ Dill61fb4892002-10-14 21:41:28 +0000227 }
228}